surf: improve Moved type for Diff's

In some cases a moved file can also be modified and in these cases
there will be a diff as well as new Oid's for the two sides of the
diff.

Add DiffFile to the Moved type, changing the insert_moved method to
accept the DiffFile's and the DiffContent.

When serializing a Moved, if the old and the new are the same then the
serialization of the diff files and content are skipped -- same as the
old behaviour. Otherwise, the two sides are serialized along with the
content.

Signed-off-by: Fintan Halpenny <fintan.halpenny@gmail.com>
X-Clacks-Overhead: GNU Terry Pratchett
This commit is contained in:
Fintan Halpenny
2023-08-30 17:10:25 +01:00
parent bc06ee0773
commit 6ee2815cbf
3 changed files with 255 additions and 15 deletions
+23 -7
View File
@@ -235,7 +235,7 @@ impl<'a> TryFrom<git2::Diff<'a>> for Diff {
Delta::Added => created(&mut diff, &git_diff, idx, &delta)?,
Delta::Deleted => deleted(&mut diff, &git_diff, idx, &delta)?,
Delta::Modified => modified(&mut diff, &git_diff, idx, &delta)?,
Delta::Renamed => renamed(&mut diff, &delta)?,
Delta::Renamed => renamed(&mut diff, &git_diff, idx, &delta)?,
Delta::Copied => copied(&mut diff, &delta)?,
status => {
return Err(error::Diff::DeltaUnhandled(status));
@@ -323,17 +323,33 @@ fn modified(
}
}
fn renamed(diff: &mut Diff, delta: &git2::DiffDelta<'_>) -> Result<(), error::Diff> {
let old = delta
fn renamed(
diff: &mut Diff,
git_diff: &git2::Diff<'_>,
idx: usize,
delta: &git2::DiffDelta<'_>,
) -> Result<(), error::Diff> {
let old_path = delta
.old_file()
.path()
.ok_or(error::Diff::PathUnavailable)?;
let new = delta
.ok_or(error::Diff::PathUnavailable)?
.to_path_buf();
let new_path = delta
.new_file()
.path()
.ok_or(error::Diff::PathUnavailable)?;
.ok_or(error::Diff::PathUnavailable)?
.to_path_buf();
let patch = git2::Patch::from_diff(git_diff, idx)?;
let old = DiffFile::try_from(delta.old_file())?;
let new = DiffFile::try_from(delta.new_file())?;
diff.insert_moved(old.to_path_buf(), new.to_path_buf());
if let Some(patch) = patch {
diff.insert_moved(old_path, new_path, old, new, DiffContent::try_from(patch)?);
} else if delta.new_file().is_binary() {
diff.insert_moved(old_path, new_path, old, new, DiffContent::Binary);
} else {
diff.insert_moved(old_path, new_path, old, new, DiffContent::Empty);
}
Ok(())
}