step twenty three: tag,resolve name to oid

This commit is contained in:
Gonçalo Valério 2020-11-04 21:54:45 +00:00
parent e035b80888
commit 5ddf692f07
2 changed files with 14 additions and 9 deletions

View File

@ -119,6 +119,13 @@ pub fn create_tag(name: String, oid: String) {
data::update_ref(format!("refs/tags/{}", name), oid); data::update_ref(format!("refs/tags/{}", name), oid);
} }
pub fn get_oid(name: String) -> String {
match data::get_ref(name.clone()) {
Ok(oid) => return oid,
_ => return name,
}
}
fn is_ignored(path: &String) -> bool { fn is_ignored(path: &String) -> bool {
if path.contains(".rgit") { if path.contains(".rgit") {
true true

View File

@ -88,10 +88,8 @@ fn hash_object(matches: ArgMatches) {
fn cat_file(matches: ArgMatches) { fn cat_file(matches: ArgMatches) {
if let Some(cmd_matches) = matches.subcommand_matches("cat-file") { if let Some(cmd_matches) = matches.subcommand_matches("cat-file") {
let file_contents = data::get_object( let hash = base::get_oid(cmd_matches.value_of("hash").unwrap().to_owned());
cmd_matches.value_of("hash").unwrap().to_owned(), let file_contents = data::get_object(hash, "".to_owned());
"".to_owned(),
);
println!("{}", file_contents) println!("{}", file_contents)
} }
} }
@ -102,8 +100,8 @@ fn write_tree() {
fn read_tree(matches: ArgMatches) { fn read_tree(matches: ArgMatches) {
if let Some(cmd_matches) = matches.subcommand_matches("read-tree") { if let Some(cmd_matches) = matches.subcommand_matches("read-tree") {
let oid = cmd_matches.value_of("oid").unwrap(); let oid = base::get_oid(cmd_matches.value_of("oid").unwrap().to_owned());
base::read_tree(oid.to_owned()); base::read_tree(oid);
} }
} }
@ -116,7 +114,7 @@ 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 = cmd_matches.value_of("oid").unwrap_or(""); let provided_oid = base::get_oid(cmd_matches.value_of("oid").unwrap_or("").to_owned());
let mut oid; let mut oid;
if provided_oid == "" { if provided_oid == "" {
oid = data::get_ref("HEAD".to_owned()).expect("Cannot read HEAD file"); oid = data::get_ref("HEAD".to_owned()).expect("Cannot read HEAD file");
@ -142,7 +140,7 @@ fn log_commits(matches: ArgMatches) {
fn checkout(matches: ArgMatches) { fn checkout(matches: ArgMatches) {
if let Some(cmd_matches) = matches.subcommand_matches("checkout") { if let Some(cmd_matches) = matches.subcommand_matches("checkout") {
let oid = cmd_matches.value_of("oid").unwrap().to_owned(); let oid = base::get_oid(cmd_matches.value_of("oid").unwrap().to_owned());
base::checkout(oid); base::checkout(oid);
} }
} }
@ -150,7 +148,7 @@ 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 = cmd_matches.value_of("oid").unwrap_or("").to_owned(); let mut oid = base::get_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 = data::get_ref("HEAD".to_owned()).expect("Cannot read HEAD");
} }