step twenty four: try different directories when searching for a ref

This commit is contained in:
Gonçalo Valério 2020-11-06 00:19:39 +00:00
parent 5ddf692f07
commit 3bdddbc28e
2 changed files with 33 additions and 9 deletions

View File

@ -120,10 +120,32 @@ pub fn create_tag(name: String, oid: String) {
} }
pub fn get_oid(name: String) -> String { pub fn get_oid(name: String) -> String {
match data::get_ref(name.clone()) { let refs_to_try: [String; 4] = [
Ok(oid) => return oid, format!("{}", name),
_ => return name, format!("refs/{}", name),
format!("refs/tags/{}", name),
format!("refs/heads/{}", name),
];
for reference in refs_to_try.into_iter() {
match data::get_ref(reference.clone()) {
Ok(oid) => return oid,
_ => continue,
}
} }
let mut is_hex = true;
for c in name.chars() {
if !c.is_ascii_hexdigit() {
is_hex = false;
}
}
if name.len() == 40 && is_hex {
return name;
}
panic!(format!("Unknown name {}", name));
} }
fn is_ignored(path: &String) -> bool { fn is_ignored(path: &String) -> bool {

View File

@ -114,12 +114,12 @@ fn commit(matches: ArgMatches) {
fn log_commits(matches: ArgMatches) { fn log_commits(matches: ArgMatches) {
if let Some(cmd_matches) = matches.subcommand_matches("log") { if let Some(cmd_matches) = matches.subcommand_matches("log") {
let provided_oid = base::get_oid(cmd_matches.value_of("oid").unwrap_or("").to_owned()); let provided_ref = cmd_matches.value_of("oid").unwrap_or("").to_owned();
let mut oid; let mut oid;
if provided_oid == "" { if provided_ref == "" {
oid = data::get_ref("HEAD".to_owned()).expect("Cannot read HEAD file"); oid = base::get_oid("HEAD".to_owned());
} else { } else {
oid = provided_oid.to_owned(); oid = base::get_oid(provided_ref.to_owned());
} }
loop { loop {
@ -148,9 +148,11 @@ fn checkout(matches: ArgMatches) {
fn tag(matches: ArgMatches) { fn tag(matches: ArgMatches) {
if let Some(cmd_matches) = matches.subcommand_matches("tag") { if let Some(cmd_matches) = matches.subcommand_matches("tag") {
let name = cmd_matches.value_of("name").unwrap().to_owned(); let name = cmd_matches.value_of("name").unwrap().to_owned();
let mut oid = base::get_oid(cmd_matches.value_of("oid").unwrap_or("").to_owned()); let mut oid = cmd_matches.value_of("oid").unwrap_or("").to_owned();
if oid == "" { if oid == "" {
oid = data::get_ref("HEAD".to_owned()).expect("Cannot read HEAD"); oid = base::get_oid("HEAD".to_owned());
} else {
oid = base::get_oid(name.clone());
} }
base::create_tag(name, oid); base::create_tag(name, oid);
} }