step fourty six: show changed files

This commit is contained in:
Gonçalo Valério 2020-11-23 23:12:10 +00:00
parent 4387831290
commit 84bb97e1a2
2 changed files with 34 additions and 1 deletions

View File

@ -67,3 +67,26 @@ pub fn diff_trees(t_from: HashMap<String, String>, t_to: HashMap<String, String>
return output;
}
pub fn changed_files(
t_from: HashMap<String, String>,
t_to: HashMap<String, String>,
) -> 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;
}

View File

@ -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);
}
}