Skip to content

Commit

Permalink
No commit message
Browse files Browse the repository at this point in the history
  • Loading branch information
NikolaRHristov committed Aug 2, 2024
1 parent b10686f commit 6806e97
Show file tree
Hide file tree
Showing 5 changed files with 969 additions and 4,147 deletions.
2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ num_cpus = "1.16.0"
regex = "1.10.5"
dashmap = "6.0.1"
itertools = "0.13.0"
diff = "0.1.13"
prettytable-rs = "0.10.0"

[build-dependencies]
serde = { version = "1.0.204", features = ["derive"] }
Expand Down
83 changes: 79 additions & 4 deletions Source/Fn/Summary/Difference.rs
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,8 @@ pub fn Fn(
Options.show_binary(false);
Options.force_binary(false);

let mut Output = String::new();
let mut FileChanges = HashMap::new();
let mut CurrentFile = String::new();

Repository
.diff_tree_to_tree(
Expand All @@ -134,9 +135,15 @@ pub fn Fn(
if !Regex.is_match(Path.0) && !Regex.is_match(Path.1) {
if let Ok(Content) = std::str::from_utf8(Line.content()) {
match Line.origin() {
'F' => Output.push_str(&format!("{}", Content)),
'+' => Output.push_str(&format!("+ {}", Content)),
'-' => Output.push_str(&format!("- {}", Content)),
'F' => {
CurrentFile = format!("{} - {}", Path.0, Path.1);
}
'+' | '-' => {
FileChanges
.entry(CurrentFile.clone())
.or_insert_with(Vec::new)
.push((Line.origin(), Content.trim_end().to_string()));
}
_ => (),
}
};
Expand All @@ -145,5 +152,73 @@ pub fn Fn(
true
})?;

let mut Output = String::new();

for (File, Changes) in FileChanges {
Output.push_str(&format!("In file '{}':\n", File));
let (OldContent, NewContent): (Vec<_>, Vec<_>) =
Changes.iter().partition(|&(origin, _)| *origin == '-');

let OldText: String =
OldContent.iter().map(|(_, content)| content.as_str()).collect::<Vec<_>>().join("\n");
let NewText: String =
NewContent.iter().map(|(_, content)| content.as_str()).collect::<Vec<_>>().join("\n");

let Diff = diff::lines(&OldText, &NewText);
let mut Additions = Vec::new();
let mut Deletions = Vec::new();
let mut Modifications = Vec::new();

for d in Diff {
match d {
diff::Result::Left(l) => Deletions.push(l.to_string()),
diff::Result::Right(r) => Additions.push(r.to_string()),
diff::Result::Both(l, r) if l != r => {
Modifications.push((l.to_string(), r.to_string()))
}
_ => {}
}
}

if !Additions.is_empty() {
Output.push_str(&format!("- Added {} line(s):\n", Additions.len()));
for (i, addition) in Additions.iter().enumerate().take(3) {
Output.push_str(&format!(" {}. {}\n", i + 1, addition));
}
if Additions.len() > 3 {
Output.push_str(&format!(" ... and {} more additions\n", Additions.len() - 3));
}
}

if !Deletions.is_empty() {
Output.push_str(&format!("- Removed {} line(s):\n", Deletions.len()));
for (i, deletion) in Deletions.iter().enumerate().take(3) {
Output.push_str(&format!(" {}. {}\n", i + 1, deletion));
}
if Deletions.len() > 3 {
Output.push_str(&format!(" ... and {} more deletions\n", Deletions.len() - 3));
}
}

if !Modifications.is_empty() {
Output.push_str(&format!("- Modified {} line(s):\n", Modifications.len()));
for (i, (old, new)) in Modifications.iter().enumerate().take(3) {
Output.push_str(&format!(" {}. Changed from \"{}\" to \"{}\"\n", i + 1, old, new));
}
if Modifications.len() > 3 {
Output.push_str(&format!(
" ... and {} more modifications\n",
Modifications.len() - 3
));
}
}

Output.push_str("\n");
}

Ok(Output)
}

use diff;
use std::collections::HashMap;
use prettytable::{Table, Row, Cell};

Check warning on line 224 in Source/Fn/Summary/Difference.rs

View workflow job for this annotation

GitHub Actions / Build (stable)

unused imports: `Cell`, `Row`, and `Table`

Check warning on line 224 in Source/Fn/Summary/Difference.rs

View workflow job for this annotation

GitHub Actions / Build (nightly)

unused imports: `Cell`, `Row`, `Table`
Loading

0 comments on commit 6806e97

Please sign in to comment.