diff --git a/src/base.rs b/src/base.rs index 9b1849c..621e4f8 100644 --- a/src/base.rs +++ b/src/base.rs @@ -109,6 +109,12 @@ pub fn get_commit(oid: String) -> Commit { }; } +pub fn checkout(oid: String) { + let commit = get_commit(oid.clone()); + read_tree(commit.tree); + data::set_head(oid); +} + fn is_ignored(path: &String) -> bool { if path.contains(".rgit") { true diff --git a/src/main.rs b/src/main.rs index c2dfeb5..151a23a 100644 --- a/src/main.rs +++ b/src/main.rs @@ -43,6 +43,11 @@ fn main() { .about("List all commits") .arg(Arg::with_name("oid").index(1).required(false)), ) + .subcommand( + SubCommand::with_name("checkout") + .about("Move the current content and HEAD to given commit") + .arg(Arg::with_name("oid").index(1).required(true)), + ) .get_matches(); match matches.subcommand_name() { @@ -53,6 +58,7 @@ fn main() { Some("read-tree") => read_tree(matches), Some("commit") => commit(matches), Some("log") => log_commits(matches), + Some("checkout") => checkout(matches), _ => println!("unknown sub command"), } } @@ -126,3 +132,10 @@ fn log_commits(matches: ArgMatches) { } } } + +fn checkout(matches: ArgMatches) { + if let Some(cmd_matches) = matches.subcommand_matches("checkout") { + let oid = cmd_matches.value_of("oid").unwrap().to_owned(); + base::checkout(oid); + } +}