Skip to content

Commit

Permalink
Merge remote-tracking branch 'sebastinez/add-line-stats-per-file'
Browse files Browse the repository at this point in the history
* sebastinez/add-line-stats-per-file:
  surf: Add `stats` property to each `DiffContent::Plain`

Signed-off-by: Fintan Halpenny <fintan.halpenny@gmail.com>
  • Loading branch information
FintanH committed Oct 2, 2023
2 parents 3c8e3cd + 097d013 commit e7e059e
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 5 deletions.
29 changes: 24 additions & 5 deletions radicle-surf/src/diff.rs
Original file line number Diff line number Diff line change
Expand Up @@ -288,6 +288,7 @@ pub enum DiffContent {
#[cfg_attr(feature = "serde", serde(rename_all = "camelCase"))]
Plain {
hunks: Hunks<Modification>,
stats: FileStats,
eof: EofNewLine,
},
Empty,
Expand All @@ -296,10 +297,18 @@ pub enum DiffContent {
impl DiffContent {
pub fn eof(&self) -> Option<EofNewLine> {
match self {
Self::Plain { hunks: _, eof } => Some(eof.clone()),
Self::Plain { eof, .. } => Some(eof.clone()),
_ => None,
}
}

pub fn stats(&self) -> Option<&FileStats> {
match &self {
DiffContent::Plain { stats, .. } => Some(stats),
DiffContent::Empty => None,
DiffContent::Binary => None,
}
}
}

/// File mode in a diff.
Expand Down Expand Up @@ -371,23 +380,23 @@ impl Serialize for FileDiff {
match &self {
FileDiff::Added(x) => {
state.serialize_field("path", &x.path)?;
state.serialize_field("diff", &x.diff)?
state.serialize_field("diff", &x.diff)?;
},
FileDiff::Deleted(x) => {
state.serialize_field("path", &x.path)?;
state.serialize_field("diff", &x.diff)?
state.serialize_field("diff", &x.diff)?;
},
FileDiff::Modified(x) => {
state.serialize_field("path", &x.path)?;
state.serialize_field("diff", &x.diff)?;
},
FileDiff::Moved(x) => {
state.serialize_field("oldPath", &x.old_path)?;
state.serialize_field("newPath", &x.new_path)?
state.serialize_field("newPath", &x.new_path)?;
},
FileDiff::Copied(x) => {
state.serialize_field("oldPath", &x.old_path)?;
state.serialize_field("newPath", &x.new_path)?
state.serialize_field("newPath", &x.new_path)?;
},
}
state.end()
Expand All @@ -411,6 +420,16 @@ impl Serialize for Diff {
}
}

/// Statistics describing a particular [`FileDiff`].
#[cfg_attr(feature = "serde", derive(Serialize), serde(rename_all = "camelCase"))]
#[derive(Clone, Copy, Debug, Default, Eq, PartialEq)]
pub struct FileStats {
/// Get the total number of additions in a [`FileDiff`].
pub additions: usize,
/// Get the total number of deletions in a [`FileDiff`].
pub deletions: usize,
}

/// Statistics describing a particular [`Diff`].
#[cfg_attr(feature = "serde", derive(Serialize), serde(rename_all = "camelCase"))]
#[derive(Clone, Copy, Debug, Default, Eq, PartialEq)]
Expand Down
15 changes: 15 additions & 0 deletions radicle-surf/src/diff/git.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ use super::{
DiffFile,
EofNewLine,
FileMode,
FileStats,
Hunk,
Hunks,
Line,
Expand Down Expand Up @@ -152,6 +153,8 @@ impl TryFrom<git2::Patch<'_>> for DiffContent {
let mut hunks = Vec::new();
let mut old_missing_eof = false;
let mut new_missing_eof = false;
let mut additions = 0;
let mut deletions = 0;

for h in 0..patch.num_hunks() {
let (hunk, hunk_lines) = patch.hunk(h)?;
Expand All @@ -166,11 +169,19 @@ impl TryFrom<git2::Patch<'_>> for DiffContent {
old_missing_eof = true;
continue;
},
git2::DiffLineType::Addition => {
additions += 1;
},
git2::DiffLineType::Deletion => {
deletions += 1;
},
git2::DiffLineType::AddEOFNL => {
additions += 1;
old_missing_eof = true;
continue;
},
git2::DiffLineType::DeleteEOFNL => {
deletions += 1;
new_missing_eof = true;
continue;
},
Expand All @@ -194,6 +205,10 @@ impl TryFrom<git2::Patch<'_>> for DiffContent {
};
Ok(DiffContent::Plain {
hunks: Hunks(hunks),
stats: FileStats {
additions,
deletions,
},
eof,
})
}
Expand Down
33 changes: 33 additions & 0 deletions radicle-surf/t/src/diff.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ use radicle_surf::{
EofNewLine,
FileDiff,
FileMode,
FileStats,
Hunk,
Line,
Modification,
Expand Down Expand Up @@ -48,6 +49,10 @@ fn test_initial_diff() -> Result<(), Error> {
new: 1..2,
}]
.into(),
stats: FileStats {
additions: 1,
deletions: 0,
},
eof: EofNewLine::default(),
},
new: DiffFile {
Expand Down Expand Up @@ -99,6 +104,10 @@ fn test_diff_file() -> Result<(), Error> {
new: 1..3,
}]
.into(),
stats: FileStats {
additions: 2,
deletions: 1
},
eof: EofNewLine::default(),
},
old: DiffFile {
Expand Down Expand Up @@ -137,6 +146,10 @@ fn test_diff() -> Result<(), Error> {
new: 1..3,
}]
.into(),
stats: FileStats {
additions: 2,
deletions: 1
},
eof: EofNewLine::default(),
},
old: DiffFile {
Expand Down Expand Up @@ -238,6 +251,10 @@ fn test_diff_serde() -> Result<(), Error> {
"old": { "start": 0, "end": 0 },
"new": { "start": 1, "end": 3 },
}],
"stats": {
"additions": 2,
"deletions": 0,
},
"eof": "noneMissing",
},
"new": {
Expand Down Expand Up @@ -295,6 +312,10 @@ fn test_diff_serde() -> Result<(), Error> {
"old": { "start": 1, "end": 8 },
"new": { "start": 0, "end": 0 },
}],
"stats": {
"additions": 0,
"deletions": 7,
},
"eof": "noneMissing",
},
}],
Expand All @@ -310,6 +331,10 @@ fn test_diff_serde() -> Result<(), Error> {
"diff": {
"eof": "noneMissing",
"hunks": [],
"stats": {
"additions": 0,
"deletions": 0,
},
"type": "plain",
},
"new": {
Expand Down Expand Up @@ -350,6 +375,10 @@ fn test_diff_serde() -> Result<(), Error> {
"old": { "start": 1, "end": 3 },
"new": { "start": 1, "end": 3 },
}],
"stats": {
"additions": 2,
"deletions": 2
},
"eof": "noneMissing",
},
"new": {
Expand Down Expand Up @@ -553,6 +582,10 @@ index 3f69208f3..cbc843c82 100644
},
},
],
"stats": {
"additions": 7,
"deletions": 5
},
"type": "plain",
},
"new": {
Expand Down

0 comments on commit e7e059e

Please sign in to comment.