From 41cf24d2bd6ce805f5b37b471a8dff779a25b61b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gon=C3=A7alo=20Val=C3=A9rio?= Date: Mon, 2 Nov 2020 23:01:31 +0000 Subject: [PATCH] step nineteen: checkout, read tree and move HEAD --- src/base.rs | 6 ++++++ src/main.rs | 13 +++++++++++++ 2 files changed, 19 insertions(+) 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); + } +}