aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorTeddy Wing2020-07-22 01:26:48 +0200
committerTeddy Wing2020-07-22 01:26:48 +0200
commit58e5286a1f1cb72bb3cfd1f1f6f415a4b80bdef2 (patch)
tree385f08e45cc78157d94e7aee99f8f53b574f030e /src
parent848ecbcdf2ae15a6b74bb348385709098f2c700a (diff)
downloadgit-suggestion-58e5286a1f1cb72bb3cfd1f1f6f415a4b80bdef2.tar.bz2
Try modifying diff hunk with 'unidiff'
Testing out 'unidiff' to build the patch. Trying to see if it will adjust the line numbers automatically when the patch is modified. Doesn't look like it. Seems like it has a nice interface, though. Still having trouble applying the patch due to complaints of patch format errors.
Diffstat (limited to 'src')
-rw-r--r--src/lib.rs52
1 files changed, 52 insertions, 0 deletions
diff --git a/src/lib.rs b/src/lib.rs
index 6db5c67..5ac1a74 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -147,4 +147,56 @@ mod tests {
"#,
);
}
+
+ #[test]
+ fn unified_diff() {
+ use unidiff::PatchSet;
+
+ let diff = r#"--- a/command/pr.go
++++ b/command/pr.go
+@@ -1,9 +1,11 @@
+ package command
+
+ import (
++ "bufio" // used to input comment
+ "errors"
+ "fmt"
+ "io"
++ "os" // used to input comment
+"#;
+
+ let mut patch = PatchSet::new();
+ patch.parse(diff).unwrap();
+
+ println!("{:?}", patch);
+ println!("{}", patch);
+
+ let lines = patch.files_mut()[0].hunks_mut()[0].lines_mut();
+
+ // for line in &lines {
+ // if line.is_removed() {
+ // } else if line.is_added() {
+ // line.line_type = unidiff::LINE_TYPE_CONTEXT.to_owned();
+ // }
+ // }
+
+ lines
+ .iter_mut()
+ .filter(|l| !l.is_removed())
+ // .map(|l| {
+ .for_each(|l| {
+ if l.is_added() {
+ l.line_type = unidiff::LINE_TYPE_CONTEXT.to_owned();
+ }
+ });
+
+ lines[lines.len() - 2].line_type = unidiff::LINE_TYPE_REMOVED.to_owned();
+
+ patch.files_mut()[0].hunks_mut()[0].append(unidiff::Line::new(
+ r#" "os""#,
+ unidiff::LINE_TYPE_ADDED,
+ ));
+
+ println!("{}", patch);
+ }
}