step thirty six: switch branches

This commit is contained in:
Gonçalo Valério 2020-11-13 20:17:20 +00:00
parent 0e813bb9e7
commit b54604ac72
2 changed files with 23 additions and 11 deletions

View File

@ -137,17 +137,25 @@ pub fn iter_commits_and_parents(mut oids: VecDeque<String>) -> Vec<String> {
return oid_sequence;
}
pub fn checkout(oid: String) {
pub fn checkout(name: String) {
let oid = get_oid(name.clone());
let commit = get_commit(oid.clone());
let head;
read_tree(commit.tree);
data::update_ref(
"HEAD".to_owned(),
data::RefValue {
value: oid,
if is_branch(name.clone()) {
head = data::RefValue {
symbolic: true,
value: format!("refs/heads/{}", name),
};
} else {
head = data::RefValue {
symbolic: false,
},
true,
);
value: oid,
};
}
data::update_ref("HEAD".to_owned(), head, false);
}
pub fn create_tag(name: String, oid: String) {
@ -269,3 +277,7 @@ fn empty_current_directory(dir: &str) -> io::Result<()> {
}
Ok(())
}
fn is_branch(name: String) -> bool {
return data::get_ref(name, true).value != "";
}

View File

@ -49,7 +49,7 @@ fn main() {
.subcommand(
SubCommand::with_name("checkout")
.about("Move the current content and HEAD to given commit")
.arg(Arg::with_name("oid").index(1).required(true)),
.arg(Arg::with_name("commit").index(1).required(true)),
)
.subcommand(
SubCommand::with_name("tag")
@ -147,8 +147,8 @@ fn log_commits(matches: ArgMatches) {
fn checkout(matches: ArgMatches) {
if let Some(cmd_matches) = matches.subcommand_matches("checkout") {
let oid = base::get_oid(cmd_matches.value_of("oid").unwrap().to_owned());
base::checkout(oid);
let name = cmd_matches.value_of("commit").unwrap().to_owned();
base::checkout(name);
}
}