step thirty nine: show all branches
This commit is contained in:
parent
99d6be3834
commit
57d5f13acc
|
@ -238,6 +238,15 @@ pub fn get_branch_name() -> String {
|
|||
return (*ref_items.last().unwrap()).to_owned();
|
||||
}
|
||||
|
||||
pub fn iter_branch_names() -> Vec<String> {
|
||||
let mut branches = vec![];
|
||||
for entry in data::iter_refs("refs/heads/", true) {
|
||||
let name: Vec<&str> = entry.0.splitn(2, "refs/heads/").collect();
|
||||
branches.push((*name.last().unwrap()).to_owned());
|
||||
}
|
||||
branches
|
||||
}
|
||||
|
||||
fn is_ignored(path: &String) -> bool {
|
||||
if path.contains(".rgit") {
|
||||
true
|
||||
|
|
19
src/data.rs
19
src/data.rs
|
@ -73,9 +73,12 @@ pub fn get_ref(reference: String, deref: bool) -> RefValue {
|
|||
return get_ref_internal(reference, deref).1;
|
||||
}
|
||||
|
||||
pub fn iter_refs(deref: bool) -> Vec<(String, RefValue)> {
|
||||
pub fn iter_refs(prefix: &str, deref: bool) -> Vec<(String, RefValue)> {
|
||||
let mut refs: Vec<(String, RefValue)> = vec![];
|
||||
refs.push(("HEAD".to_owned(), get_ref("HEAD".to_owned(), deref)));
|
||||
|
||||
if prefix == "" || prefix.starts_with("HEAD") {
|
||||
refs.push(("HEAD".to_owned(), get_ref("HEAD".to_owned(), deref)));
|
||||
}
|
||||
|
||||
for entry in WalkDir::new(format!("{}/refs/", RGIT_DIR)) {
|
||||
let item = entry.unwrap();
|
||||
|
@ -83,10 +86,12 @@ pub fn iter_refs(deref: bool) -> Vec<(String, RefValue)> {
|
|||
|
||||
if metadata.is_file() {
|
||||
let relative_path = item.path().strip_prefix(RGIT_DIR).unwrap();
|
||||
refs.push((
|
||||
relative_path.to_str().unwrap().to_owned(),
|
||||
get_ref(relative_path.to_str().unwrap().to_owned(), deref),
|
||||
));
|
||||
if relative_path.starts_with(prefix) {
|
||||
refs.push((
|
||||
relative_path.to_str().unwrap().to_owned(),
|
||||
get_ref(relative_path.to_str().unwrap().to_owned(), deref),
|
||||
));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -99,7 +104,7 @@ pub fn get_ref_internal(reference: String, deref: bool) -> (String, RefValue) {
|
|||
let symbolic = !value.is_empty() && value.starts_with("ref:");
|
||||
|
||||
if symbolic {
|
||||
let new_ref: Vec<&str> = value.splitn(2, ":").collect();
|
||||
let new_ref: Vec<&str> = value.splitn(2, ": ").collect();
|
||||
value = new_ref[1].to_owned();
|
||||
if deref {
|
||||
return get_ref_internal(value, deref);
|
||||
|
|
20
src/main.rs
20
src/main.rs
|
@ -61,7 +61,7 @@ fn main() {
|
|||
.subcommand(
|
||||
SubCommand::with_name("branch")
|
||||
.about("Create a new branch")
|
||||
.arg(Arg::with_name("name").index(1).required(true))
|
||||
.arg(Arg::with_name("name").index(1).required(false))
|
||||
.arg(Arg::with_name("start_point").index(2).default_value("@")),
|
||||
)
|
||||
.subcommand(SubCommand::with_name("status").about("check current branch"))
|
||||
|
@ -166,7 +166,7 @@ fn tag(matches: ArgMatches) {
|
|||
fn k() {
|
||||
let mut dot = "digraph commits {\n".to_owned();
|
||||
let mut oids = VecDeque::new();
|
||||
for refinfo in data::iter_refs(false) {
|
||||
for refinfo in data::iter_refs("", false) {
|
||||
dot.push_str(&format!("\"{}\" [shape=note]\n", refinfo.0));
|
||||
dot.push_str(&format!("\"{}\" -> \"{}\"", refinfo.0, refinfo.1.value));
|
||||
if !refinfo.1.symbolic {
|
||||
|
@ -207,11 +207,19 @@ fn k() {
|
|||
|
||||
fn branch(matches: ArgMatches) {
|
||||
if let Some(cmd_matches) = matches.subcommand_matches("branch") {
|
||||
let name = cmd_matches.value_of("name").unwrap().to_owned();
|
||||
let name = cmd_matches.value_of("name").unwrap_or("").to_owned();
|
||||
let provided_ref = cmd_matches.value_of("start_point").unwrap().to_owned();
|
||||
let oid = base::get_oid(provided_ref.clone());
|
||||
base::create_branch(name.clone(), oid.clone());
|
||||
println!("Branch {} created_at {}", name, oid);
|
||||
if name == "" {
|
||||
let current = base::get_branch_name();
|
||||
for branch in base::iter_branch_names() {
|
||||
let prefix = if branch == current { "*" } else { " " };
|
||||
println!("{} {}", prefix, branch);
|
||||
}
|
||||
} else {
|
||||
let oid = base::get_oid(provided_ref.clone());
|
||||
base::create_branch(name.clone(), oid.clone());
|
||||
println!("Branch {} created_at {}", name, oid);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue