mirror of
https://seed.radicle.dev/rad:z6cFWeWpnZNHh9rUW8phgA3b5yGt.git
synced 2026-11-29 06:00:28 +08:00
radicle-surf: refactor diff
This is an overhaul of the diff module. The major changes are as follows. It shouldn't be possible for an added or deleted file to have any other kind line changes other than additions or deletions. So there are now 3 different kinds of changes, Addition, Deletion, or Modification. Hunk and Hunks now take a generic type parameter to allow for the three types of changes. As a consequence a FileDiff does the same. For the actual file representations, they fill in their parameters as Addition, Deletion, or Modification. During this refactor some renaming was also made: * CreateFile -> Added -- as well as the created field being renamed to added. * DeleteFile -> Deleted * MoveFile -> Moved * CopyFile -> Copied * ModifiedFile -> Modified The Stats are also now carried in the Diff instead of being calculated. They can be retrieved from the `git2::Diff`. The final change was to remove the `source::Commit::stats` field in favour of accessing the `Diff::stats` instead. The above changes mean that the serialization of `Diff` and `Commit` are not backwards compatible. Signed-off-by: Fintan Halpenny <fintan.halpenny@gmail.com> X-Clacks-Overhead: GNU Terry Pratchett
This commit is contained in:
@@ -1,129 +0,0 @@
|
||||
// Copyright © 2022 The Radicle Git Contributors
|
||||
// SPDX-License-Identifier: GPL-3.0-or-later
|
||||
|
||||
//! Unit tests for radicle_surf::diff
|
||||
|
||||
use pretty_assertions::assert_eq;
|
||||
use radicle_surf::diff::*;
|
||||
|
||||
/* TODO(fintan): Move is not detected yet
|
||||
#[test]
|
||||
fn test_moved_file() {
|
||||
let mut directory = Directory::root();
|
||||
directory.insert_file(&unsound::path::new("mod.rs"), File::new(b"use banana"));
|
||||
|
||||
let mut new_directory = Directory::root();
|
||||
new_directory.insert_file(&unsound::path::new("banana.rs"), File::new(b"use banana"));
|
||||
|
||||
let diff = Diff::diff(directory, new_directory).expect("diff failed");
|
||||
|
||||
assert_eq!(diff, Diff::new())
|
||||
}
|
||||
*/
|
||||
|
||||
/* TODO(fintan): Tricky stuff
|
||||
#[test]
|
||||
fn test_disjoint_directories() {
|
||||
let mut directory = Directory::root();
|
||||
directory.insert_file(
|
||||
&unsound::path::new("foo/src/banana.rs"),
|
||||
File::new(b"use banana"),
|
||||
);
|
||||
|
||||
let mut other_directory = Directory::root();
|
||||
other_directory.insert_file(
|
||||
&unsound::path::new("bar/src/pineapple.rs"),
|
||||
File::new(b"use pineapple"),
|
||||
);
|
||||
|
||||
let diff = Diff::diff(directory, other_directory).expect("diff failed");
|
||||
|
||||
let expected_diff = Diff {
|
||||
created: vec![CreateFile(Path::from_labels(
|
||||
unsound::label::new("bar"),
|
||||
&[
|
||||
unsound::label::new("src"),
|
||||
unsound::label::new("pineapple.rs"),
|
||||
],
|
||||
))],
|
||||
deleted: vec![DeleteFile(Path::from_labels(
|
||||
unsound::label::new("foo"),
|
||||
&[unsound::label::new("src"), unsound::label::new("banana.rs")],
|
||||
))],
|
||||
moved: vec![],
|
||||
modified: vec![],
|
||||
};
|
||||
|
||||
assert_eq!(diff, expected_diff)
|
||||
}
|
||||
*/
|
||||
|
||||
#[test]
|
||||
fn test_both_missing_eof_newline() {
|
||||
let buf = r#"
|
||||
diff --git a/.env b/.env
|
||||
index f89e4c0..7c56eb7 100644
|
||||
--- a/.env
|
||||
+++ b/.env
|
||||
@@ -1 +1 @@
|
||||
-hello=123
|
||||
\ No newline at end of file
|
||||
+hello=1234
|
||||
\ No newline at end of file
|
||||
"#;
|
||||
let diff = git2::Diff::from_buffer(buf.as_bytes()).unwrap();
|
||||
let diff = Diff::try_from(diff).unwrap();
|
||||
assert_eq!(diff.modified[0].eof, Some(EofNewLine::BothMissing));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_none_missing_eof_newline() {
|
||||
let buf = r#"
|
||||
diff --git a/.env b/.env
|
||||
index f89e4c0..7c56eb7 100644
|
||||
--- a/.env
|
||||
+++ b/.env
|
||||
@@ -1 +1 @@
|
||||
-hello=123
|
||||
+hello=1234
|
||||
"#;
|
||||
let diff = git2::Diff::from_buffer(buf.as_bytes()).unwrap();
|
||||
let diff = Diff::try_from(diff).unwrap();
|
||||
assert_eq!(diff.modified[0].eof, None);
|
||||
}
|
||||
|
||||
// TODO(xphoniex): uncomment once libgit2 has fixed the bug
|
||||
//#[test]
|
||||
// fn test_old_missing_eof_newline() {
|
||||
// let buf = r#"
|
||||
// diff --git a/.env b/.env
|
||||
// index f89e4c0..7c56eb7 100644
|
||||
// --- a/.env
|
||||
// +++ b/.env
|
||||
// @@ -1 +1 @@
|
||||
// -hello=123
|
||||
// \ No newline at end of file
|
||||
// +hello=1234
|
||||
// "#;
|
||||
// let diff = git2::Diff::from_buffer(buf.as_bytes()).unwrap();
|
||||
// let diff = Diff::try_from(diff).unwrap();
|
||||
// assert_eq!(diff.modified[0].eof, Some(EofNewLine::OldMissing));
|
||||
// }
|
||||
|
||||
// TODO(xphoniex): uncomment once libgit2 has fixed the bug
|
||||
//#[test]
|
||||
// fn test_new_missing_eof_newline() {
|
||||
// let buf = r#"
|
||||
// diff --git a/.env b/.env
|
||||
// index f89e4c0..7c56eb7 100644
|
||||
// --- a/.env
|
||||
// +++ b/.env
|
||||
// @@ -1 +1 @@
|
||||
// -hello=123
|
||||
// +hello=1234
|
||||
// \ No newline at end of file
|
||||
// "#;
|
||||
// let diff = git2::Diff::from_buffer(buf.as_bytes()).unwrap();
|
||||
// let diff = Diff::try_from(diff).unwrap();
|
||||
// assert_eq!(diff.modified[0].eof, Some(EofNewLine::NewMissing));
|
||||
// }
|
||||
Reference in New Issue
Block a user