step thirty eight: print current branch

This commit is contained in:
Gonçalo Valério 2020-11-14 12:30:35 +00:00
parent e0392ead71
commit 99d6be3834
2 changed files with 21 additions and 0 deletions

View File

@ -228,6 +228,16 @@ pub fn init() -> std::io::Result<()> {
Ok(())
}
pub fn get_branch_name() -> String {
let head = data::get_ref("HEAD".to_owned(), false);
if !head.symbolic {
return "".to_owned();
}
assert!(head.value.starts_with("refs/heads/"));
let ref_items: Vec<&str> = head.value.splitn(3, "/").collect();
return (*ref_items.last().unwrap()).to_owned();
}
fn is_ignored(path: &String) -> bool {
if path.contains(".rgit") {
true

View File

@ -64,6 +64,7 @@ fn main() {
.arg(Arg::with_name("name").index(1).required(true))
.arg(Arg::with_name("start_point").index(2).default_value("@")),
)
.subcommand(SubCommand::with_name("status").about("check current branch"))
.get_matches();
match matches.subcommand_name() {
@ -78,6 +79,7 @@ fn main() {
Some("tag") => tag(matches),
Some("k") => k(),
Some("branch") => branch(matches),
Some("status") => status(),
_ => println!("unknown sub command"),
}
}
@ -212,3 +214,12 @@ fn branch(matches: ArgMatches) {
println!("Branch {} created_at {}", name, oid);
}
}
fn status() {
let branch = base::get_branch_name();
if branch != "".to_owned() {
println!("On branch {}", branch);
} else {
println!("HEAD detached at {}", &base::get_oid("@".to_owned())[1..10])
}
}