step fourty three: list changed files in commit

This commit is contained in:
Gonçalo Valério 2020-11-21 17:08:47 +00:00
parent b79d4d0d2a
commit 284086a234
3 changed files with 54 additions and 4 deletions

View File

@ -181,7 +181,7 @@ pub fn get_oid(mut name: String) -> String {
format!("refs/heads/{}", name),
];
for reference in refs_to_try.into_iter() {
for reference in refs_to_try.iter() {
let found = data::get_ref(reference.clone(), false);
if found.value != "" {
return found.value;
@ -268,6 +268,11 @@ fn is_ignored(path: &String) -> bool {
fn tree_entries(oid: String) -> Vec<(String, String, String)> {
let mut entries: Vec<(String, String, String)> = vec![];
if oid == "".to_owned() {
return entries;
}
let tree_data = data::get_object(oid, "tree".to_owned());
for line in tree_data.split_terminator("\n") {
let items: Vec<&str> = line.splitn(3, " ").collect();
@ -280,7 +285,7 @@ fn tree_entries(oid: String) -> Vec<(String, String, String)> {
return entries;
}
fn get_tree(oid: String, base_path: String) -> HashMap<String, String> {
pub fn get_tree(oid: String, base_path: String) -> HashMap<String, String> {
let mut result = HashMap::new();
for entry in tree_entries(oid) {
// _type, oid, name

33
src/diff.rs Normal file
View File

@ -0,0 +1,33 @@
use std::collections::HashMap;
fn compare_trees(trees: Vec<HashMap<String, String>>) -> HashMap<String, Vec<String>> {
let len_trees = trees.len();
let mut entries: HashMap<String, Vec<String>> = HashMap::new();
for (i, tree) in trees.iter().enumerate() {
for (path, oid) in tree {
if entries.contains_key(path) {
entries.get_mut(path).unwrap()[i] = oid.clone();
} else {
entries.insert(path.clone(), vec!["".to_owned(); len_trees]);
entries.get_mut(path).unwrap()[i] = oid.clone();
}
}
}
return entries;
}
pub fn diff_trees(t_from: HashMap<String, String>, t_to: HashMap<String, String>) -> String {
let mut output = "".to_owned();
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 {
output.push_str(format!("changed {}\n", path).as_str());
}
}
return output;
}

View File

@ -1,10 +1,11 @@
use clap::{App, Arg, ArgMatches, SubCommand};
use std::collections::{HashMap, HashSet, VecDeque};
use std::collections::{HashMap, VecDeque};
use std::fs;
use std::io::Write;
use std::process::{Command, Stdio};
mod base;
mod data;
mod diff;
fn main() {
let matches = App::new("rgit vcs")
@ -264,7 +265,18 @@ fn show(matches: ArgMatches) {
let oid = base::get_oid(cmd_matches.value_of("oid").unwrap().to_owned());
let commit = base::get_commit(oid.clone());
let refs: HashMap<String, Vec<String>> = HashMap::new();
print_commit(oid, &commit, refs)
let parent_tree = if commit.parent != "".to_owned() {
base::get_commit(commit.parent.clone()).tree
} else {
"".to_owned()
};
print_commit(oid, &commit, refs);
let result = diff::diff_trees(
base::get_tree(parent_tree, "".to_owned()),
base::get_tree(commit.tree, "".to_owned()),
);
println!("{}", result);
}
}