bmwill / diffy

Tools for finding and manipulating differences between files
Apache License 2.0
75 stars 22 forks source link

empty context lines don't have the prefixing space char #35

Open correabuscar opened 2 months ago

correabuscar commented 2 months ago

on gnu diff and git diff every empty context line has one space as the prefix for the line, thus the line isn't empty in the patch itself, for example:

(to see this, start selecting with the mouse from 3 to -4, u'll see the extra space)

gnu diff:

--- /tmp/a  2024-08-08 22:29:37.810523528 +0200
+++ /tmp/b  2024-08-08 22:29:41.627190227 +0200
@@ -2,4 +2,4 @@
 2
 3

-4
+5

git diff:

--- /tmp/a  2024-08-08 22:29:37.810523528 +0200
+++ /tmp/b  2024-08-08 22:29:41.627190227 +0200
@@ -2,4 +2,4 @@
 2
 3

-4
+5

diffy:

--- /tmp/a
+++ /tmp/b
@@ -2,4 +2,4 @@
 2
 3

-4
+5

However applying the patch works fine with either empty context line or prefixed with space context line. (at least, gnu patch does)

correabuscar commented 2 months ago

ok I see this was on purpose, so what I want (which I'll do locally) is:

diff --git a/src/patch/format.rs b/src/patch/format.rs
index ec998a80f076016f..2a606b3e321f0349 100644
--- a/src/patch/format.rs
+++ b/src/patch/format.rs
@@ -231,12 +231,8 @@
             write!(w, "{}", style.prefix())?;
         }

-        if sign == ' ' && line == b"\n" {
-            w.write_all(line)?;
-        } else {
             write!(w, "{}", sign)?;
             w.write_all(line)?;
-        }

         if self.f.with_color {
             write!(w, "{}", style.suffix())?;
@@ -263,11 +259,7 @@
             write!(f, "{}", style.prefix())?;
         }

-        if sign == ' ' && *line == "\n" {
-            write!(f, "{}", line)?;
-        } else {
             write!(f, "{}{}", sign, line)?;
-        }

         if self.f.with_color {
             write!(f, "{}", style.suffix())?;