From 459ded17090a366a7097271ff8ef31bbac43d794 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gon=C3=A7alo=20Val=C3=A9rio?= Date: Sun, 22 Nov 2020 00:17:22 +0000 Subject: [PATCH] step fourty four: print diff of commit --- Cargo.lock | 94 +++++++++++++++++++++++++++++++++++++++++++++++++++++ Cargo.toml | 1 + src/diff.rs | 38 +++++++++++++++++++++- 3 files changed, 132 insertions(+), 1 deletion(-) diff --git a/Cargo.lock b/Cargo.lock index fcd5089..08e54ae 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -81,6 +81,17 @@ dependencies = [ "version_check", ] +[[package]] +name = "getrandom" +version = "0.1.15" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fc587bc0ec293155d5bfa6b9891ec18a1e330c234f896ea47fbada4cadbe47e6" +dependencies = [ + "cfg-if", + "libc", + "wasi", +] + [[package]] name = "hermit-abi" version = "0.1.16" @@ -102,12 +113,75 @@ version = "0.3.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "624a8340c38c1b80fd549087862da4ba43e08858af025b236e509b6649fc13d5" +[[package]] +name = "ppv-lite86" +version = "0.2.10" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ac74c624d6b2d21f425f752262f42188365d7b8ff1aff74c82e45136510a4857" + +[[package]] +name = "rand" +version = "0.7.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6a6b1679d49b24bbfe0c803429aa1874472f50d9b363131f0e89fc356b544d03" +dependencies = [ + "getrandom", + "libc", + "rand_chacha", + "rand_core", + "rand_hc", +] + +[[package]] +name = "rand_chacha" +version = "0.2.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f4c8ed856279c9737206bf725bf36935d8666ead7aa69b52be55af369d193402" +dependencies = [ + "ppv-lite86", + "rand_core", +] + +[[package]] +name = "rand_core" +version = "0.5.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "90bde5296fc891b0cef12a6d03ddccc162ce7b2aff54160af9338f8d40df6d19" +dependencies = [ + "getrandom", +] + +[[package]] +name = "rand_hc" +version = "0.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ca3129af7b92a17112d59ad498c6f81eaf463253766b90396d39ea7a39d6613c" +dependencies = [ + "rand_core", +] + +[[package]] +name = "redox_syscall" +version = "0.1.57" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "41cc0f7e4d5d4544e8861606a285bb08d3e70712ccc7d2b84d7c0ccfaf4b05ce" + +[[package]] +name = "remove_dir_all" +version = "0.5.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3acd125665422973a33ac9d3dd2df85edad0f4ae9b00dafb1a05e43a9f5ef8e7" +dependencies = [ + "winapi", +] + [[package]] name = "rgit" version = "0.1.0" dependencies = [ "clap", "sha-1", + "tempfile", "walkdir", ] @@ -139,6 +213,20 @@ version = "0.8.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "8ea5119cdb4c55b55d432abb513a0429384878c15dde60cc77b1c99de1a95a6a" +[[package]] +name = "tempfile" +version = "3.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7a6e24d9338a0a5be79593e2fa15a648add6138caa803e2d5bc782c371732ca9" +dependencies = [ + "cfg-if", + "libc", + "rand", + "redox_syscall", + "remove_dir_all", + "winapi", +] + [[package]] name = "textwrap" version = "0.11.0" @@ -183,6 +271,12 @@ dependencies = [ "winapi-util", ] +[[package]] +name = "wasi" +version = "0.9.0+wasi-snapshot-preview1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cccddf32554fecc6acb585f82a32a72e28b48f8c4c1883ddfeeeaa96f7d8e519" + [[package]] name = "winapi" version = "0.3.9" diff --git a/Cargo.toml b/Cargo.toml index 79d85ae..ec66057 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -10,3 +10,4 @@ edition = "2018" clap = "2.33.3" sha-1 = "0.9.1" walkdir = "2" +tempfile = "3.1.0" diff --git a/src/diff.rs b/src/diff.rs index 4fdd388..fa0312b 100644 --- a/src/diff.rs +++ b/src/diff.rs @@ -1,4 +1,10 @@ use std::collections::HashMap; +use std::fs; +use std::process::{Command, Stdio}; +use tempfile::NamedTempFile; + +#[path = "data.rs"] +mod data; fn compare_trees(trees: Vec>) -> HashMap> { let len_trees = trees.len(); @@ -18,6 +24,36 @@ fn compare_trees(trees: Vec>) -> HashMap String { + let f_from = NamedTempFile::new().unwrap(); + let f_to = NamedTempFile::new().unwrap(); + + if o_from != "" { + let content = data::get_object(o_from, "blob".to_owned()); + fs::write(f_from.path(), content).unwrap(); + } + + if o_to != "" { + let content = data::get_object(o_to, "blob".to_owned()); + fs::write(f_to.path(), content).unwrap(); + } + + let output = Command::new("diff") + .arg("--unified") + .arg("--show-c-function") + .arg("--label") + .arg(format!("a/{}", path)) + .arg(f_from.path()) + .arg("--label") + .arg(format!("a/{}", path)) + .arg(f_to.path()) + .stdout(Stdio::piped()) + .output() + .expect("Failed to launch diff"); + + return String::from_utf8_lossy(&output.stdout).to_string(); +} + pub fn diff_trees(t_from: HashMap, t_to: HashMap) -> String { let mut output = "".to_owned(); let trees = vec![t_from, t_to]; @@ -25,7 +61,7 @@ pub fn diff_trees(t_from: HashMap, t_to: HashMap 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()); + output.push_str(diff_blobs(o_from, o_to, path.clone()).as_str()); } }