From 84bb97e1a2213d46fe5ff8284822dfb58c097a63 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gon=C3=A7alo=20Val=C3=A9rio?= Date: Mon, 23 Nov 2020 23:12:10 +0000 Subject: [PATCH] step fourty six: show changed files --- src/diff.rs | 23 +++++++++++++++++++++++ src/main.rs | 12 +++++++++++- 2 files changed, 34 insertions(+), 1 deletion(-) diff --git a/src/diff.rs b/src/diff.rs index fa0312b..f9b3e4e 100644 --- a/src/diff.rs +++ b/src/diff.rs @@ -67,3 +67,26 @@ pub fn diff_trees(t_from: HashMap, t_to: HashMap return output; } + +pub fn changed_files( + t_from: HashMap, + t_to: HashMap, +) -> Vec<(String, String)> { + let mut result = vec![]; + let trees = vec![t_from, t_to]; + for (path, oids) in compare_trees(trees).iter() { + let o_from = oids[0].clone(); + let o_to = oids[1].clone(); + if o_from != o_to { + let action = if o_from == "" { + "new file" + } else if o_to == "" { + "deleted" + } else { + "mofified" + }; + result.push((path.clone(), action.to_owned())) + } + } + return result; +} diff --git a/src/main.rs b/src/main.rs index ec7ed96..6e90e52 100644 --- a/src/main.rs +++ b/src/main.rs @@ -252,10 +252,20 @@ fn branch(matches: ArgMatches) { fn status() { let branch = base::get_branch_name(); + let head = base::get_oid("@".to_owned()); if branch != "".to_owned() { println!("On branch {}", branch); } else { - println!("HEAD detached at {}", &base::get_oid("@".to_owned())[1..10]) + println!("HEAD detached at {}", &head[1..10]) + } + + println!("Changes to be committed:\n"); + let head_commit = base::get_commit(head); + for (path, action) in diff::changed_files( + base::get_tree(head_commit.tree, "".to_owned()), + base::get_working_tree(), + ) { + println!("{:>12}: {}", action, path); } }