step thirty six: switch branches
This commit is contained in:
parent
0e813bb9e7
commit
b54604ac72
28
src/base.rs
28
src/base.rs
|
@ -137,17 +137,25 @@ pub fn iter_commits_and_parents(mut oids: VecDeque<String>) -> Vec<String> {
|
||||||
return oid_sequence;
|
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 commit = get_commit(oid.clone());
|
||||||
|
let head;
|
||||||
read_tree(commit.tree);
|
read_tree(commit.tree);
|
||||||
data::update_ref(
|
|
||||||
"HEAD".to_owned(),
|
if is_branch(name.clone()) {
|
||||||
data::RefValue {
|
head = data::RefValue {
|
||||||
value: oid,
|
symbolic: true,
|
||||||
|
value: format!("refs/heads/{}", name),
|
||||||
|
};
|
||||||
|
} else {
|
||||||
|
head = data::RefValue {
|
||||||
symbolic: false,
|
symbolic: false,
|
||||||
},
|
value: oid,
|
||||||
true,
|
};
|
||||||
);
|
}
|
||||||
|
|
||||||
|
data::update_ref("HEAD".to_owned(), head, false);
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn create_tag(name: String, oid: String) {
|
pub fn create_tag(name: String, oid: String) {
|
||||||
|
@ -269,3 +277,7 @@ fn empty_current_directory(dir: &str) -> io::Result<()> {
|
||||||
}
|
}
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn is_branch(name: String) -> bool {
|
||||||
|
return data::get_ref(name, true).value != "";
|
||||||
|
}
|
||||||
|
|
|
@ -49,7 +49,7 @@ fn main() {
|
||||||
.subcommand(
|
.subcommand(
|
||||||
SubCommand::with_name("checkout")
|
SubCommand::with_name("checkout")
|
||||||
.about("Move the current content and HEAD to given commit")
|
.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(
|
||||||
SubCommand::with_name("tag")
|
SubCommand::with_name("tag")
|
||||||
|
@ -147,8 +147,8 @@ 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 = base::get_oid(cmd_matches.value_of("oid").unwrap().to_owned());
|
let name = cmd_matches.value_of("commit").unwrap().to_owned();
|
||||||
base::checkout(oid);
|
base::checkout(name);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue