step thirty eight: print current branch
This commit is contained in:
parent
e0392ead71
commit
99d6be3834
10
src/base.rs
10
src/base.rs
|
@ -228,6 +228,16 @@ pub fn init() -> std::io::Result<()> {
|
||||||
Ok(())
|
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 {
|
fn is_ignored(path: &String) -> bool {
|
||||||
if path.contains(".rgit") {
|
if path.contains(".rgit") {
|
||||||
true
|
true
|
||||||
|
|
11
src/main.rs
11
src/main.rs
|
@ -64,6 +64,7 @@ fn main() {
|
||||||
.arg(Arg::with_name("name").index(1).required(true))
|
.arg(Arg::with_name("name").index(1).required(true))
|
||||||
.arg(Arg::with_name("start_point").index(2).default_value("@")),
|
.arg(Arg::with_name("start_point").index(2).default_value("@")),
|
||||||
)
|
)
|
||||||
|
.subcommand(SubCommand::with_name("status").about("check current branch"))
|
||||||
.get_matches();
|
.get_matches();
|
||||||
|
|
||||||
match matches.subcommand_name() {
|
match matches.subcommand_name() {
|
||||||
|
@ -78,6 +79,7 @@ fn main() {
|
||||||
Some("tag") => tag(matches),
|
Some("tag") => tag(matches),
|
||||||
Some("k") => k(),
|
Some("k") => k(),
|
||||||
Some("branch") => branch(matches),
|
Some("branch") => branch(matches),
|
||||||
|
Some("status") => status(),
|
||||||
_ => println!("unknown sub command"),
|
_ => println!("unknown sub command"),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -212,3 +214,12 @@ fn branch(matches: ArgMatches) {
|
||||||
println!("Branch {} created_at {}", name, oid);
|
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])
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in New Issue