mirror of
https://github.com/facebook/sapling.git
synced 2024-10-07 07:17:55 +03:00
mdiff: fix diff -b/B/w on mixed whitespace hunks (issue127)
Previous code was computing hunks then checking if these hunks could be ignored when taking whitespace/blank-lines options in accounts. This approach is simple but fails with hunks containing both whitespace and non-whitespace changes, the whole hunk is emitted while it can be mostly made of whitespace. The new version normalize the whitespaces before hunk generation, and test for blank-lines afterwards.
This commit is contained in:
parent
e18a9b4ce6
commit
2f3f5f28ea
@ -57,13 +57,13 @@ class diffopts(object):
|
||||
|
||||
defaultopts = diffopts()
|
||||
|
||||
def wsclean(opts, text):
|
||||
def wsclean(opts, text, blank=True):
|
||||
if opts.ignorews:
|
||||
text = re.sub('[ \t]+', '', text)
|
||||
elif opts.ignorewsamount:
|
||||
text = re.sub('[ \t]+', ' ', text)
|
||||
text = re.sub('[ \t]+\n', '\n', text)
|
||||
if opts.ignoreblanklines:
|
||||
if blank and opts.ignoreblanklines:
|
||||
text = re.sub('\n+', '', text)
|
||||
return text
|
||||
|
||||
@ -183,6 +183,10 @@ def bunidiff(t1, t2, l1, l2, header1, header2, opts=defaultopts):
|
||||
# below finds the spaces between those matching sequences and translates
|
||||
# them into diff output.
|
||||
#
|
||||
if opts.ignorews or opts.ignorewsamount:
|
||||
t1 = wsclean(opts, t1, False)
|
||||
t2 = wsclean(opts, t2, False)
|
||||
|
||||
diff = bdiff.blocks(t1, t2)
|
||||
hunk = None
|
||||
for i, s1 in enumerate(diff):
|
||||
@ -208,7 +212,7 @@ def bunidiff(t1, t2, l1, l2, header1, header2, opts=defaultopts):
|
||||
if not old and not new:
|
||||
continue
|
||||
|
||||
if opts.ignorews or opts.ignorewsamount or opts.ignoreblanklines:
|
||||
if opts.ignoreblanklines:
|
||||
if wsclean(opts, "".join(old)) == wsclean(opts, "".join(new)):
|
||||
continue
|
||||
|
||||
|
@ -181,11 +181,9 @@ diff -r 540c40a65b78 foo
|
||||
--- a/foo
|
||||
+++ b/foo
|
||||
@@ -1,2 +1,3 @@
|
||||
-hello world
|
||||
-goodbye world
|
||||
+hello world
|
||||
hello world
|
||||
+
|
||||
+goodbye world
|
||||
goodbye world
|
||||
hg diff -Bb
|
||||
>>> four diffs showing changed whitespace <<<
|
||||
hg diff
|
||||
@ -212,18 +210,16 @@ diff -r 540c40a65b78 foo
|
||||
+++ b/foo
|
||||
@@ -1,2 +1,2 @@
|
||||
-hello world
|
||||
-goodbye world
|
||||
+helloworld
|
||||
+goodbye world
|
||||
goodbye world
|
||||
hg diff -Bb
|
||||
diff -r 540c40a65b78 foo
|
||||
--- a/foo
|
||||
+++ b/foo
|
||||
@@ -1,2 +1,2 @@
|
||||
-hello world
|
||||
-goodbye world
|
||||
+helloworld
|
||||
+goodbye world
|
||||
goodbye world
|
||||
hg diff -w
|
||||
>>> five diffs showing changed whitespace <<<
|
||||
hg diff
|
||||
@ -256,34 +252,30 @@ diff -r 540c40a65b78 foo
|
||||
+++ b/foo
|
||||
@@ -1,2 +1,5 @@
|
||||
-hello world
|
||||
-goodbye world
|
||||
+helloworld
|
||||
+
|
||||
+
|
||||
+
|
||||
+goodbye world
|
||||
goodbye world
|
||||
hg diff -Bb
|
||||
diff -r 540c40a65b78 foo
|
||||
--- a/foo
|
||||
+++ b/foo
|
||||
@@ -1,2 +1,5 @@
|
||||
-hello world
|
||||
-goodbye world
|
||||
+helloworld
|
||||
+
|
||||
+
|
||||
+
|
||||
+goodbye world
|
||||
goodbye world
|
||||
hg diff -w
|
||||
diff -r 540c40a65b78 foo
|
||||
--- a/foo
|
||||
+++ b/foo
|
||||
@@ -1,2 +1,5 @@
|
||||
-hello world
|
||||
-goodbye world
|
||||
+helloworld
|
||||
hello world
|
||||
+
|
||||
+
|
||||
+
|
||||
+goodbye world
|
||||
goodbye world
|
||||
hg diff -wB
|
||||
|
@ -58,9 +58,8 @@ diff -r 35fb829491c1 lines
|
||||
2
|
||||
3
|
||||
4
|
||||
-hello world
|
||||
hello world
|
||||
-goodbye world
|
||||
+hello world
|
||||
+ goodbye world
|
||||
7
|
||||
8
|
||||
|
Loading…
Reference in New Issue
Block a user