Skip to contentSkip to conversation

Allow combined diff to ignore white-spaces

PATCH In review Antoine Pelisse 3 participants 13 messages Jun 15, 2016

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 Allow combined diff to ignore white-spaces

Currently, it's not possible to use the space-ignoring options (-b, -w, --ignore-space-at-eol) with combined diff. It makes it pretty impossible to read a merge between a branch that changed all tabs to spaces, and a branch with functional changes.

Pass diff flags to diff engine, so that combined diff behaves as normal diff does with spaces.

It also means that a conflict-less merge done using a ignore-* strategy option will not show any conflict if shown in combined-diff using the same option.

---
That should be reviewed carefully as I'm not exactly sure that does make sense with the way combined-diff works.
Still it seems natural to me to be able to remove the space in combined diff as we do with normal diff. Especially as I unfortunately have to deal with many space + feature merges that are very hard to analyze/handle if space differences can't be hidden.

Cheers,
Antoine

 combine-diff.c |    6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
combine-diff.c +3 -3
index 35d41cd..7ca0a72 100644
@@ -215,7 +215,7 @@ static void combine_diff(const unsigned char *parent, unsigned int mode,
 			 struct sline *sline, unsigned int cnt, int n,
 			 int num_parent, int result_deleted,
 			 struct userdiff_driver *textconv,
-			 const char *path)
+			 const char *path, long flags)
 {
 	unsigned int p_lno, lno;
 	unsigned long nmask = (1UL << n);
@@ -231,7 +231,7 @@ static void combine_diff(const unsigned char *parent, unsigned int mode,
 	parent_file.ptr = grab_blob(parent, mode, &sz, textconv, path);
 	parent_file.size = sz;
 	memset(&xpp, 0, sizeof(xpp));
-	xpp.flags = 0;
+	xpp.flags = flags;
 	memset(&xecfg, 0, sizeof(xecfg));
 	memset(&state, 0, sizeof(state));
 	state.nmask = nmask;
@@ -962,7 +962,7 @@ static void show_patch_diff(struct combine_diff_path *elem, int num_parent,
 				     elem->parent[i].mode,
 				     &result_file, sline,
 				     cnt, i, num_parent, result_deleted,
-				     textconv, elem->path);
+				     textconv, elem->path, opt->xdl_opts);
 	}
 
 	show_hunks = make_hunks(sline, cnt, num_parent, dense);

--
1.7.9.5

#1 lore raw
Quoted 12 lines · Antoine Pelisse

Currently, it's not possible to use the space-ignoring options (-b, -w, --ignore-space-at-eol) with combined diff. It makes it pretty impossible to read a merge between a branch that changed all tabs to spaces, and a branch with functional changes.

Pass diff flags to diff engine, so that combined diff behaves as normal diff does with spaces.

It also means that a conflict-less merge done using a ignore-* strategy option will not show any conflict if shown in combined-diff using the same option.

Quoted 8 lines

---
That should be reviewed carefully as I'm not exactly sure that does make sense with the way combined-diff works.
Still it seems natural to me to be able to remove the space in combined diff as we do with normal diff. Especially as I unfortunately have to deal with many space + feature merges that are very hard to analyze/handle if space differences can't be hidden.

Assuming "That" at the beginning of the off-line comment refers to "this patch", I actually I do not think this patch needs to be reviewed carefully at all.

The change in your patch to make the internal pairwise diff to ignore whitespaces is an obvious and a sensible first step for your stated goal. With it, a block of lines where the postimage makes no change other than whitespace changes from one parent and matches with the other parent will see no hunks in either of the pair-wise diff, and such a hunk will not appear in the final output, which is exactly what you want.

What needs to be audited carefully is the part of combine diff logic that this patch does *not* touch.

An example. After collecting pairwise diffs between the shared postimage and individual parents, we align the hunks and coalesce "identical" preimage lines to form shared "-" (removed) lines. I think that step is done with byte-for-byte comparison. If the postimage removes a line from one parent and a corresponding line from another parent, and if these corresponding lines in the parents differ only by whitespaces in a way the user told us to ignore with -b/-w/etc., you would need to tweak the logic to coalesce corresponding "lost" lines in the append_lost() function. Otherwise, you will see two " -" and "- " lines that remove these corresponding lines from the parents that are identical when you ignore whitespaces.

You should add some tests; it would help you think about these issues through.

For example, in this history:

git init
seq 11 >ten
git add ten
git commit -m initial

seq 10 | sed -e 's/6/6 six/' -e 's/2/2 two/' >ten echo 11 >>ten
git commit -m ten -a

git checkout -b side HEAD^
seq 10 | sed -e 's/^/ /' | sed -e 's/2/2 dos/' >ten echo 11 >>ten
git commit -m indent -a

git merge master

seq 10 | sed -e 's/5/5 go/' -e 's/2/2 dois/' >ten git commit -m merged -a

one side indents the original and tweaks some lines, while the other side tweaks some other lines without reindenting. Most notably, the fifth line on one side is "5" while on the other side is " 5", and this line is rewritten to "5 go" in the final. The lost lines are
not coalesced to a single "-- 5" (nor "-- 5") but shows that two
preimages were different only by whitespaces. Compare that with what happens to the final line "11" that gets lost in the result.

Thanks.

Quoted 39 lines

Cheers,
Antoine

 combine-diff.c |    6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
combine-diff.c +3 -4
index 35d41cd..7ca0a72 100644
@@ -215,7 +215,7 @@ static void combine_diff(const unsigned char *parent, unsigned int mode,
 			 struct sline *sline, unsigned int cnt, int n,
 			 int num_parent, int result_deleted,
 			 struct userdiff_driver *textconv,
-			 const char *path)
+			 const char *path, long flags)
 {
 	unsigned int p_lno, lno;
 	unsigned long nmask = (1UL << n);
@@ -231,7 +231,7 @@ static void combine_diff(const unsigned char *parent, unsigned int mode,
 	parent_file.ptr = grab_blob(parent, mode, &sz, textconv, path);
 	parent_file.size = sz;
 	memset(&xpp, 0, sizeof(xpp));
-	xpp.flags = 0;
+	xpp.flags = flags;
 	memset(&xecfg, 0, sizeof(xecfg));
 	memset(&state, 0, sizeof(state));
 	state.nmask = nmask;
@@ -962,7 +962,7 @@ static void show_patch_diff(struct combine_diff_path *elem, int num_parent,
 				     elem->parent[i].mode,
 				     &result_file, sline,
 				     cnt, i, num_parent, result_deleted,
-				     textconv, elem->path);
+				     textconv, elem->path, opt->xdl_opts);
 	}
 
 	show_hunks = make_hunks(sline, cnt, num_parent, dense);
--

1.7.9.5

#2 lore raw
Quoted 10 lines
Quoted 6 lines

That should be reviewed carefully as I'm not exactly sure that does make sense with the way combined-diff works.
Still it seems natural to me to be able to remove the space in combined diff as we do with normal diff. Especially as I unfortunately have to deal with many space + feature merges that are very hard to analyze/handle if space differences can't be hidden.

Assuming "That" at the beginning of the off-line comment refers to "this patch", I actually I do not think this patch needs to be reviewed carefully at all.

I actually meant: "is that patch enough or is there anything I missed". Considering your answer, it looks like I did ;)

Quoted 4 lines

An example. After collecting pairwise diffs between the shared postimage and individual parents, we align the hunks and coalesce "identical" preimage lines to form shared "-" (removed) lines. I think that step is done with byte-for-byte comparison. If the

Quoted 8 lines

postimage removes a line from one parent and a corresponding line from another parent, and if these corresponding lines in the parents differ only by whitespaces in a way the user told us to ignore with -b/-w/etc., you would need to tweak the logic to coalesce corresponding "lost" lines in the append_lost() function. Otherwise, you will see two " -" and "- " lines that remove these corresponding lines from the parents that are identical when you ignore whitespaces.

That is the part I missed, and it's nicely explained.

Quoted 2 lines

You should add some tests; it would help you think about these issues through.

I will definitely add some tests.

Quoted 21 lines

For example, in this history:

git init
seq 11 >ten
git add ten
git commit -m initial

seq 10 | sed -e 's/6/6 six/' -e 's/2/2 two/' >ten echo 11 >>ten
git commit -m ten -a

git checkout -b side HEAD^
seq 10 | sed -e 's/^/ /' | sed -e 's/2/2 dos/' >ten echo 11 >>ten
git commit -m indent -a

git merge master

seq 10 | sed -e 's/5/5 go/' -e 's/2/2 dois/' >ten git commit -m merged -a

Quoted 7 lines

one side indents the original and tweaks some lines, while the other side tweaks some other lines without reindenting. Most notably, the fifth line on one side is "5" while on the other side is " 5", and this line is rewritten to "5 go" in the final. The lost lines are
not coalesced to a single "-- 5" (nor "-- 5") but shows that two
preimages were different only by whitespaces. Compare that with what happens to the final line "11" that gets lost in the result.

It feels incorrect to me to coalsesce "- 5" and "- 5" as it might look incorrect to the user. But still the idea is appealing. I have an implementation for that that requires more testing.

Using the exact example you gave, and running the latest next, I have this output, where 11 is not coalesced.
Is that a bug ?

ten +0 -0
diff --cc ten
index d213a99,ed40ab2..37c2af2
@@@ -1,11 -1,11 +1,10 @@@
-   1
-   2 dos
-   3
-   4
-   5
-   6
-   7
-   8
-   9
-   10
- 11
+ 1
 -2 two
++2 dois
+ 3
+ 4
 -5
 -6 six
++5 go
++6
+ 7
+ 8
+ 9
+ 10
 -11
#3 lore raw
Quoted 2 lines · Antoine Pelisse

It feels incorrect to me to coalsesce "- 5" and "- 5" as it might look incorrect to the user. But still the idea is appealing.

The users already need to see that when reading a regular patch with one or more context lines and -b/-w/etc., anyway. The context lines are made into context only because whitespace differences were ignored, and in the regular unified patch format we can show only one version, either from preimage or from postimage, and have to pick one. Coalescing "- 5" and "- 5" into "--5" or "-- 5" by picking one or the other is the same thing, no?

Quoted 3 lines

Using the exact example you gave, and running the latest next, I have this output, where 11 is not coalesced.
Is that a bug ?

It could be tickling a corner case because the removal is at the end of the file. Perhaps adding 12 that is all common across three versions and see what happens?

#4 lore raw
Quoted 5 lines · Junio C Hamano
Quoted 2 lines · Antoine Pelisse

It feels incorrect to me to coalsesce "- 5" and "- 5" as it might look incorrect to the user. But still the idea is appealing.

Quoted 7 lines

The users already need to see that when reading a regular patch with one or more context lines and -b/-w/etc., anyway. The context lines are made into context only because whitespace differences were ignored, and in the regular unified patch format we can show only one version, either from preimage or from postimage, and have to pick one. Coalescing "- 5" and "- 5" into "--5" or "-- 5" by picking one or the other is the same thing, no?

That's all I needed to be convinced. I obviously don't care which one we pick.

Quoted 7 lines
Quoted 3 lines

Using the exact example you gave, and running the latest next, I have this output, where 11 is not coalesced.
Is that a bug ?

It could be tickling a corner case because the removal is at the end of the file. Perhaps adding 12 that is all common across three versions and see what happens?

Doesn't make a difference. Still have "- 11" and " -11". I will try to have a look at it.

#5 lore raw

PATCH 1/1 Allow combined diff to ignore white-spaces

In reply to Junio C Hamano

Currently, it's not possible to use the space-ignoring options (-b, -w, --ignore-space-at-eol) with combined diff. It makes it pretty impossible to read a merge between a branch that changed all tabs to spaces, and a branch with functional changes.

Pass diff flags to diff engine, so that combined diff behaves as normal diff does with spaces.
Also coalesce lines that are removed from both (or more) parents.

It also means that a conflict-less merge done using a ignore-* strategy option will not show any conflict if shown in combined-diff using the same option.

---
OK, I added some tests and coalesce similar lost lines (using the same flags we used for diff.

 combine-diff.c           |   57 ++++++++++++++++++++++++++++++-----
 t/t4038-diff-combined.sh |   75 ++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 125 insertions(+), 7 deletions(-)
combine-diff.c +50 -7
index 35d41cd..0f33983 100644
@@ -5,6 +5,7 @@
 #include "diffcore.h"
 #include "quote.h"
 #include "xdiff-interface.h"
+#include "xdiff/xmacros.h"
 #include "log-tree.h"
 #include "refs.h"
 #include "userdiff.h"
@@ -122,7 +123,47 @@ static char *grab_blob(const unsigned char *sha1, unsigned int mode,
 	return blob;
 }
 
-static void append_lost(struct sline *sline, int n, const char *line, int len)
+static int match_string_spaces(const char *line1, int len1,
+			       const char *line2, int len2,
+			       long flags)
+{
+	if (flags & XDF_WHITESPACE_FLAGS) {
+		for (; len1 > 0 && XDL_ISSPACE(line1[len1 - 1]); len1--);
+		for (; len2 > 0 && XDL_ISSPACE(line2[len2 - 1]); len2--);
+	}
+
+	if (!(flags & (XDF_IGNORE_WHITESPACE | XDF_IGNORE_WHITESPACE_CHANGE)))
+		return (len1 == len2 && !memcmp(line1, line2, len1));
+
+	while (len1 > 0 && len2 > 0) {
+		len1--;
+		len2--;
+		if (XDL_ISSPACE(line1[len1]) || XDL_ISSPACE(line2[len2])) {
+			if ((flags & XDF_IGNORE_WHITESPACE_CHANGE) &&
+			    (!XDL_ISSPACE(line1[len1]) || !XDL_ISSPACE(line2[len2])))
+				return 0;
+
+			for (; len1 > 0 && XDL_ISSPACE(line1[len1]); len1--);
+			for (; len2 > 0 && XDL_ISSPACE(line2[len2]); len2--);
+		}
+		if (line1[len1] != line2[len2])
+			return 0;
+	}
+
+	if (flags & XDF_IGNORE_WHITESPACE) {
+		// Consume remaining spaces
+		for (; len1 > 0 && XDL_ISSPACE(line1[len1 - 1]); len1--);
+		for (; len2 > 0 && XDL_ISSPACE(line2[len2 - 1]); len2--);
+	}
+
+	// We matched full line1 and line2
+	if (!len1 && !len2)
+		return 1;
+
+	return 0;
+}
+
+static void append_lost(struct sline *sline, int n, const char *line, int len, long flags)
 {
 	struct lline *lline;
 	unsigned long this_mask = (1UL<<n);
@@ -133,8 +174,8 @@ static void append_lost(struct sline *sline, int n, const char *line, int len)
 	if (sline->lost_head) {
 		lline = sline->next_lost;
 		while (lline) {
-			if (lline->len == len &&
-			    !memcmp(lline->line, line, len)) {
+			if (match_string_spaces(lline->line, lline->len,
+						line, len, flags)) {
 				lline->parent_map |= this_mask;
 				sline->next_lost = lline->next;
 				return;
@@ -162,6 +203,7 @@ struct combine_diff_state {
 	int n;
 	struct sline *sline;
 	struct sline *lost_bucket;
+	long flags;
 };
 
 static void consume_line(void *state_, char *line, unsigned long len)
@@ -201,7 +243,7 @@ static void consume_line(void *state_, char *line, unsigned long len)
 		return; /* not in any hunk yet */
 	switch (line[0]) {
 	case '-':
-		append_lost(state->lost_bucket, state->n, line+1, len-1);
+		append_lost(state->lost_bucket, state->n, line+1, len-1, state->flags);
 		break;
 	case '+':
 		state->sline[state->lno-1].flag |= state->nmask;
@@ -215,7 +257,7 @@ static void combine_diff(const unsigned char *parent, unsigned int mode,
 			 struct sline *sline, unsigned int cnt, int n,
 			 int num_parent, int result_deleted,
 			 struct userdiff_driver *textconv,
-			 const char *path)
+			 const char *path, long flags)
 {
 	unsigned int p_lno, lno;
 	unsigned long nmask = (1UL << n);
@@ -231,9 +273,10 @@ static void combine_diff(const unsigned char *parent, unsigned int mode,
 	parent_file.ptr = grab_blob(parent, mode, &sz, textconv, path);
 	parent_file.size = sz;
 	memset(&xpp, 0, sizeof(xpp));
-	xpp.flags = 0;
+	xpp.flags = flags;
 	memset(&xecfg, 0, sizeof(xecfg));
 	memset(&state, 0, sizeof(state));
+	state.flags = flags;
 	state.nmask = nmask;
 	state.sline = sline;
 	state.lno = 1;
@@ -962,7 +1005,7 @@ static void show_patch_diff(struct combine_diff_path *elem, int num_parent,
 				     elem->parent[i].mode,
 				     &result_file, sline,
 				     cnt, i, num_parent, result_deleted,
-				     textconv, elem->path);
+				     textconv, elem->path, opt->xdl_opts);
 	}
 
 	show_hunks = make_hunks(sline, cnt, num_parent, dense);
t/t4038-diff-combined.sh +75 -0

79 lines not on this page · raw message

--
1.7.9.5

#6 lore raw

Am 3/13/2013 22:21, schrieb Antoine Pelisse:

Quoted 153 lines

Currently, it's not possible to use the space-ignoring options (-b, -w, --ignore-space-at-eol) with combined diff. It makes it pretty impossible to read a merge between a branch that changed all tabs to spaces, and a branch with functional changes.

Pass diff flags to diff engine, so that combined diff behaves as normal diff does with spaces.
Also coalesce lines that are removed from both (or more) parents.

It also means that a conflict-less merge done using a ignore-* strategy option will not show any conflict if shown in combined-diff using the same option.

---
OK, I added some tests and coalesce similar lost lines (using the same flags we used for diff.

 combine-diff.c           |   57 ++++++++++++++++++++++++++++++-----
 t/t4038-diff-combined.sh |   75 ++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 125 insertions(+), 7 deletions(-)
combine-diff.c +50 -7
index 35d41cd..0f33983 100644
@@ -5,6 +5,7 @@
 #include "diffcore.h"
 #include "quote.h"
 #include "xdiff-interface.h"
+#include "xdiff/xmacros.h"
 #include "log-tree.h"
 #include "refs.h"
 #include "userdiff.h"
@@ -122,7 +123,47 @@ static char *grab_blob(const unsigned char *sha1, unsigned int mode,
 	return blob;
 }
 
-static void append_lost(struct sline *sline, int n, const char *line, int len)
+static int match_string_spaces(const char *line1, int len1,
+			       const char *line2, int len2,
+			       long flags)
+{
+	if (flags & XDF_WHITESPACE_FLAGS) {
+		for (; len1 > 0 && XDL_ISSPACE(line1[len1 - 1]); len1--);
+		for (; len2 > 0 && XDL_ISSPACE(line2[len2 - 1]); len2--);
+	}
+
+	if (!(flags & (XDF_IGNORE_WHITESPACE | XDF_IGNORE_WHITESPACE_CHANGE)))
+		return (len1 == len2 && !memcmp(line1, line2, len1));
+
+	while (len1 > 0 && len2 > 0) {
+		len1--;
+		len2--;
+		if (XDL_ISSPACE(line1[len1]) || XDL_ISSPACE(line2[len2])) {
+			if ((flags & XDF_IGNORE_WHITESPACE_CHANGE) &&
+			    (!XDL_ISSPACE(line1[len1]) || !XDL_ISSPACE(line2[len2])))
+				return 0;
+
+			for (; len1 > 0 && XDL_ISSPACE(line1[len1]); len1--);
+			for (; len2 > 0 && XDL_ISSPACE(line2[len2]); len2--);
+		}
+		if (line1[len1] != line2[len2])
+			return 0;
+	}
+
+	if (flags & XDF_IGNORE_WHITESPACE) {
+		// Consume remaining spaces
+		for (; len1 > 0 && XDL_ISSPACE(line1[len1 - 1]); len1--);
+		for (; len2 > 0 && XDL_ISSPACE(line2[len2 - 1]); len2--);
+	}
+
+	// We matched full line1 and line2
+	if (!len1 && !len2)
+		return 1;
+
+	return 0;
+}
+
+static void append_lost(struct sline *sline, int n, const char *line, int len, long flags)
 {
 	struct lline *lline;
 	unsigned long this_mask = (1UL<<n);
@@ -133,8 +174,8 @@ static void append_lost(struct sline *sline, int n, const char *line, int len)
 	if (sline->lost_head) {
 		lline = sline->next_lost;
 		while (lline) {
-			if (lline->len == len &&
-			    !memcmp(lline->line, line, len)) {
+			if (match_string_spaces(lline->line, lline->len,
+						line, len, flags)) {
 				lline->parent_map |= this_mask;
 				sline->next_lost = lline->next;
 				return;
@@ -162,6 +203,7 @@ struct combine_diff_state {
 	int n;
 	struct sline *sline;
 	struct sline *lost_bucket;
+	long flags;
 };
 
 static void consume_line(void *state_, char *line, unsigned long len)
@@ -201,7 +243,7 @@ static void consume_line(void *state_, char *line, unsigned long len)
 		return; /* not in any hunk yet */
 	switch (line[0]) {
 	case '-':
-		append_lost(state->lost_bucket, state->n, line+1, len-1);
+		append_lost(state->lost_bucket, state->n, line+1, len-1, state->flags);
 		break;
 	case '+':
 		state->sline[state->lno-1].flag |= state->nmask;
@@ -215,7 +257,7 @@ static void combine_diff(const unsigned char *parent, unsigned int mode,
 			 struct sline *sline, unsigned int cnt, int n,
 			 int num_parent, int result_deleted,
 			 struct userdiff_driver *textconv,
-			 const char *path)
+			 const char *path, long flags)
 {
 	unsigned int p_lno, lno;
 	unsigned long nmask = (1UL << n);
@@ -231,9 +273,10 @@ static void combine_diff(const unsigned char *parent, unsigned int mode,
 	parent_file.ptr = grab_blob(parent, mode, &sz, textconv, path);
 	parent_file.size = sz;
 	memset(&xpp, 0, sizeof(xpp));
-	xpp.flags = 0;
+	xpp.flags = flags;
 	memset(&xecfg, 0, sizeof(xecfg));
 	memset(&state, 0, sizeof(state));
+	state.flags = flags;
 	state.nmask = nmask;
 	state.sline = sline;
 	state.lno = 1;
@@ -962,7 +1005,7 @@ static void show_patch_diff(struct combine_diff_path *elem, int num_parent,
 				     elem->parent[i].mode,
 				     &result_file, sline,
 				     cnt, i, num_parent, result_deleted,
-				     textconv, elem->path);
+				     textconv, elem->path, opt->xdl_opts);
 	}
 
 	show_hunks = make_hunks(sline, cnt, num_parent, dense);
t/t4038-diff-combined.sh +4 -0
index 614425a..ba8a56b 100755
@@ -113,4 +113,79 @@ test_expect_success 'check --cc --raw with forty trees' '
 	grep "^::::::::::::::::::::::::::::::::::::::::[^:]" out
 '
 
+test_expect_success 'setup combined ignore spaces' '
+	git checkout master &&
+	>test &&
+	git add test &&

t/t4038-diff-combined.sh test_expect_success 'check --cc --raw with forty trees' ' · line 129

Quoted 10 lines
t/t4038-diff-combined.sh +10 -0
+	git commit -m initial &&
+
+	echo "
+	always coalesce
+	eol space coalesce \n\
+	space  change coalesce
+	all spa ces coalesce
+	eol spaces \n\
+	space  change
+	all spa ces" >test &&

This form of 'echo' is not sufficiently portable. How about:

tr -d Q <<-\EOF >test &&

always coalesce
eol space coalesce Q
space change coalesce
all spa ces coalesce
eol spaces Q
space change
all spa ces
EOF

Quoted 8 lines
t/t4038-diff-combined.sh +8 -0
+	git commit -m "change three" -a &&
+
+	git checkout -b side HEAD^ &&
+	echo "
+	always coalesce
+	eol space coalesce
+	space change coalesce
+	all spaces coalesce

t/t4038-diff-combined.sh test_expect_success 'check --cc --raw with forty trees' ' · line 147

Quoted 10 lines
t/t4038-diff-combined.sh +10 -0
+	eol spaces
+	space change
+	all spaces" >test &&
+	git commit -m indent -a &&
+
+	test_must_fail git merge master &&
+	echo "
+	eol spaces \n\
+	space  change
+	all spa ces" > test &&

Ditto.

t/t4038-diff-combined.sh test_expect_success 'check --cc --raw with forty trees' ' · line 158

Quoted 11 lines
t/t4038-diff-combined.sh +11 -0
+	git commit -m merged -a
+'
+
+test_expect_success 'check combined output (no ignore space)' '
+	git show | test_i18ngrep "^-\s*eol spaces" &&
+	git show | test_i18ngrep "^-\s*eol space coalesce" &&
+	git show | test_i18ngrep "^-\s*space change" &&
+	git show | test_i18ngrep "^-\s*space change coalesce" &&
+	git show | test_i18ngrep "^-\s*all spaces" &&
+	git show | test_i18ngrep "^-\s*all spaces coalesce" &&
+	git show | test_i18ngrep "^--\s*always coalesce"

This loses the exit code of git show. We usually write this as

	git show >actual &&
	grep "^- *eol spaces" &&
	grep "^- *eol space coalesce" &&
	...

(Same for later tests.)

There is nothing i18n-ish in the test patterns. Use regular grep.

BTW, there is compare_diff_patch() in diff-lib.sh. You can use it to compare diff output to expected output. Then you do not need a grep invocation for each line of the test file.

-- Hannes

#7 lore raw
Quoted 8 lines · Johannes Sixt

This form of 'echo' is not sufficiently portable. How about:

	tr -d Q <<-\EOF >test &&

	always coalesce
	eol space coalesce Q

...
EOF

Much better.

Quoted 12 lines

t/t4038-diff-combined.sh test_expect_success 'check --cc --raw with forty trees' ' · line 158

Quoted 4 lines
t/t4038-diff-combined.sh +3 -0
+test_expect_success 'check combined output (no ignore space)' '
+	git show | test_i18ngrep "^-\s*eol spaces" &&
...
+	git show | test_i18ngrep "^--\s*always coalesce"

This loses the exit code of git show. We usually write this as

	git show >actual &&
	grep "^- *eol spaces" &&
	grep "^- *eol space coalesce" &&
	...
Quoted 7 lines

(Same for later tests.)

There is nothing i18n-ish in the test patterns. Use regular grep.

BTW, there is compare_diff_patch() in diff-lib.sh. You can use it to compare diff output to expected output. Then you do not need a grep invocation for each line of the test file.

All good suggestions. Thanks.

#8 lore raw
Quoted 27 lines · Junio C Hamano
Quoted 8 lines · Johannes Sixt

This form of 'echo' is not sufficiently portable. How about:

tr -d Q <<-\EOF >test &&

always coalesce
eol space coalesce Q
...
EOF

Much better.

Quoted 14 lines

t/t4038-diff-combined.sh test_expect_success 'check --cc --raw with forty trees' ' · line 158

Quoted 4 lines
t/t4038-diff-combined.sh +3 -0
+test_expect_success 'check combined output (no ignore space)' '
+    git show | test_i18ngrep "^-\s*eol spaces" &&
...
+    git show | test_i18ngrep "^--\s*always coalesce"

This loses the exit code of git show. We usually write this as

      git show >actual &&
      grep "^- *eol spaces" &&
      grep "^- *eol space coalesce" &&
      ...

(Same for later tests.)

Quoted 7 lines
Quoted 5 lines

There is nothing i18n-ish in the test patterns. Use regular grep.

BTW, there is compare_diff_patch() in diff-lib.sh. You can use it to compare diff output to expected output. Then you do not need a grep invocation for each line of the test file.

All good suggestions. Thanks.

OK Very good,
I will resubmit tonight. I was indeed not really sure about the best way to test here. Thanks for the tips and confirmation !

Cheers,

#11 lore raw
In reply to Antoine Pelisse
Quoted 37 lines · Antoine Pelisse

Currently, it's not possible to use the space-ignoring options (-b, -w, --ignore-space-at-eol) with combined diff. It makes it pretty impossible to read a merge between a branch that changed all tabs to spaces, and a branch with functional changes.

Pass diff flags to diff engine, so that combined diff behaves as normal diff does with spaces.
Also coalesce lines that are removed from both (or more) parents.

It also means that a conflict-less merge done using a ignore-* strategy option will not show any conflict if shown in combined-diff using the same option.

---
OK, I added some tests and coalesce similar lost lines (using the same flags we used for diff.

 combine-diff.c           |   57 ++++++++++++++++++++++++++++++-----
 t/t4038-diff-combined.sh |   75 ++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 125 insertions(+), 7 deletions(-)
combine-diff.c +1 -0
index 35d41cd..0f33983 100644
@@ -5,6 +5,7 @@
 #include "diffcore.h"
 #include "quote.h"
 #include "xdiff-interface.h"
+#include "xdiff/xmacros.h"
 #include "log-tree.h"
 #include "refs.h"
 #include "userdiff.h"
@@ -122,7 +123,47 @@ static char *grab_blob(const unsigned char *sha1, unsigned int mode,
 	return blob;
 }

combine-diff.c static char *grab_blob(const unsigned char *sha1, unsigned int mode, · line 133

Quoted 10 lines
combine-diff.c +8 -1
 
-static void append_lost(struct sline *sline, int n, const char *line, int len)
+static int match_string_spaces(const char *line1, int len1,
+			       const char *line2, int len2,
+			       long flags)
+{
+	if (flags & XDF_WHITESPACE_FLAGS) {
+		for (; len1 > 0 && XDL_ISSPACE(line1[len1 - 1]); len1--);
+		for (; len2 > 0 && XDL_ISSPACE(line2[len2 - 1]); len2--);
+	}

This says "any XDF_WHITESPACE_FLAGS causes the trailing blanks ignored", which is correct: --ignore-space-at-eol, -b or -w are all we have. OK.

combine-diff.c static char *grab_blob(const unsigned char *sha1, unsigned int mode, · line 136

Quoted 2 lines
combine-diff.c +2 -0
+	if (!(flags & (XDF_IGNORE_WHITESPACE | XDF_IGNORE_WHITESPACE_CHANGE)))
+		return (len1 == len2 && !memcmp(line1, line2, len1));

If --ignore-space-at-eol is the only one given, or none of the whitespace flags is given, then we can just do memcmp(). OK.

The remainder of the function is only used when either -b or -w is (or both are) in effect.

combine-diff.c static char *grab_blob(const unsigned char *sha1, unsigned int mode, · line 140

Quoted 3 lines
combine-diff.c +3 -0
+	while (len1 > 0 && len2 > 0) {
+		len1--;
+		len2--;

Scanning from the tail end of the strings...

combine-diff.c static char *grab_blob(const unsigned char *sha1, unsigned int mode, · line 144

Quoted 4 lines
combine-diff.c +4 -0
+		if (XDL_ISSPACE(line1[len1]) || XDL_ISSPACE(line2[len2])) {
+			if ((flags & XDF_IGNORE_WHITESPACE_CHANGE) &&
+			    (!XDL_ISSPACE(line1[len1]) || !XDL_ISSPACE(line2[len2])))
+				return 0;

If --ignore-space-change and one side is blank while the other not, then these cannot be the same. If we see blank on both sides, that is fine under --ignore-whitespace, too.

combine-diff.c static char *grab_blob(const unsigned char *sha1, unsigned int mode, · line 147

Quoted 2 lines
combine-diff.c +2 -0
+			for (; len1 > 0 && XDL_ISSPACE(line1[len1]); len1--);
+			for (; len2 > 0 && XDL_ISSPACE(line2[len2]); len2--);

Then, strip the blanks, if any, so that the next comparison is done for the last non-blank char from both sides. OK.

combine-diff.c static char *grab_blob(const unsigned char *sha1, unsigned int mode, · line 150

Quoted 3 lines
combine-diff.c +3 -0
+		}
+		if (line1[len1] != line2[len2])
+			return 0;

And we can say "not same" as soon as we find differences. Rinse and repeat.

combine-diff.c static char *grab_blob(const unsigned char *sha1, unsigned int mode, · line 154

Quoted 4 lines
combine-diff.c +4 -0
+	}
+
+	if (flags & XDF_IGNORE_WHITESPACE) {
+		// Consume remaining spaces

No C++/C99 comments in our codebase, please.

combine-diff.c static char *grab_blob(const unsigned char *sha1, unsigned int mode, · line 153

Quoted 2 lines
combine-diff.c +2 -0
+		for (; len1 > 0 && XDL_ISSPACE(line1[len1 - 1]); len1--);
+		for (; len2 > 0 && XDL_ISSPACE(line2[len2 - 1]); len2--);

If we are ignoring all whitespaces, we can simply discard them. OK.

I wonder what happens when --ignore-space-change is in effect but not --ignore-all-space? The loop must have exited because one (or both) of len1 and len2 went down to zero. If both are zero, we obviously have a match, and otherwise, one has blanks at the beginning and the other does not. Wait, is that really true? Yes, because if len1 == 0 && len2 != 0, line2[len2] cannot be blank (otherwise len2 would be zero because we have a loop to eat a run of blanks).

OK, sounds good.

combine-diff.c static char *grab_blob(const unsigned char *sha1, unsigned int mode, · line 159

Quoted 3 lines
combine-diff.c +3 -0
+	}
+
+	// We matched full line1 and line2

Likewise for "//".

Quoted 12 lines
combine-diff.c +6 -0
+	if (!len1 && !len2)
+		return 1;
+
+	return 0;
+}
 
 
+static void append_lost(struct sline *sline, int n, const char *line, int len, long flags)
 {
 	struct lline *lline;
 	unsigned long this_mask = (1UL<<n);

combine-diff.c static void append_lost(struct sline *sline, int n, const char *line, int len) · line 181

Quoted 10 lines
combine-diff.c +2 -2
@@ -133,8 +174,8 @@ static void append_lost(struct sline *sline, int n, const char *line, int len)
 	if (sline->lost_head) {
 		lline = sline->next_lost;
 		while (lline) {
-			if (lline->len == len &&
-			    !memcmp(lline->line, line, len)) {
+			if (match_string_spaces(lline->line, lline->len,
+						line, len, flags)) {
 				lline->parent_map |= this_mask;
 				sline->next_lost = lline->next;
 				return;

Nicely done.

Thanks, will queue with comment style fix-ups.

Quoted 86 lines
t/t4038-diff-combined.sh +75 -1
index 614425a..ba8a56b 100755
@@ -113,4 +113,79 @@ test_expect_success 'check --cc --raw with forty trees' '
 	grep "^::::::::::::::::::::::::::::::::::::::::[^:]" out
 '
 
+test_expect_success 'setup combined ignore spaces' '
+	git checkout master &&
+	>test &&
+	git add test &&
+	git commit -m initial &&
+
+	echo "
+	always coalesce
+	eol space coalesce \n\
+	space  change coalesce
+	all spa ces coalesce
+	eol spaces \n\
+	space  change
+	all spa ces" >test &&
+	git commit -m "change three" -a &&
+
+	git checkout -b side HEAD^ &&
+	echo "
+	always coalesce
+	eol space coalesce
+	space change coalesce
+	all spaces coalesce
+	eol spaces
+	space change
+	all spaces" >test &&
+	git commit -m indent -a &&
+
+	test_must_fail git merge master &&
+	echo "
+	eol spaces \n\
+	space  change
+	all spa ces" > test &&
+	git commit -m merged -a
+'
+
+test_expect_success 'check combined output (no ignore space)' '
+	git show | test_i18ngrep "^-\s*eol spaces" &&
+	git show | test_i18ngrep "^-\s*eol space coalesce" &&
+	git show | test_i18ngrep "^-\s*space change" &&
+	git show | test_i18ngrep "^-\s*space change coalesce" &&
+	git show | test_i18ngrep "^-\s*all spaces" &&
+	git show | test_i18ngrep "^-\s*all spaces coalesce" &&
+	git show | test_i18ngrep "^--\s*always coalesce"
+'
+
+test_expect_success 'check combined output (ignore space at eol)' '
+	git show --ignore-space-at-eol | test_i18ngrep "^\s*eol spaces" &&
+	git show --ignore-space-at-eol | test_i18ngrep "^--\s*eol space coalesce" &&
+	git show --ignore-space-at-eol | test_i18ngrep "^-\s*space change" &&
+	git show --ignore-space-at-eol | test_i18ngrep "^-\s*space change coalesce" &&
+	git show --ignore-space-at-eol | test_i18ngrep "^-\s*all spaces" &&
+	git show --ignore-space-at-eol | test_i18ngrep "^-\s*all spaces coalesce" &&
+	git show --ignore-space-at-eol | test_i18ngrep "^--\s*always coalesce"
+'
+
+test_expect_success 'check combined output (ignore space change)' '
+	git show -b | test_i18ngrep "^\s*eol spaces" &&
+	git show -b | test_i18ngrep "^--\s*eol space coalesce" &&
+	git show -b | test_i18ngrep "^\s*space  change" &&
+	git show -b | test_i18ngrep "^--\s*space change coalesce" &&
+	git show -b | test_i18ngrep "^-\s*all spaces" &&
+	git show -b | test_i18ngrep "^-\s*all spaces coalesce" &&
+	git show -b | test_i18ngrep "^--\s*always coalesce"
+'
+
+test_expect_success 'check combined output (ignore all spaces)' '
+	git show -w | test_i18ngrep "^\s*eol spaces" &&
+	git show -w | test_i18ngrep "^--\s*eol space coalesce" &&
+	git show -w | test_i18ngrep "^\s*space  change" &&
+	git show -w | test_i18ngrep "^--\s*space change coalesce" &&
+	git show -w | test_i18ngrep "^\s*all spa ces" &&
+	git show -w | test_i18ngrep "^--\s*all spaces coalesce" &&
+	git show -w | test_i18ngrep "^--\s*always coalesce"
+'
+
 test_done
--

1.7.9.5

#12 lore raw
In reply to Antoine Pelisse
Quoted 2 lines · Antoine Pelisse

OK, I added some tests and coalesce similar lost lines (using the same flags we used for diff.

Hmph, why doesn't this pass its own tests?

t/t4038-diff-combined.sh test_expect_success 'check --cc --raw with forty trees' ' · line 152

Quoted 2 lines
t/t4038-diff-combined.sh +2 -0
+test_expect_success 'check combined output (no ignore space)' '
+	git show | test_i18ngrep "^-\s*eol spaces" &&

What kind of grep pattern is "\s*"?

#9 lore raw

PATCH v3 1/1 Allow combined diff to ignore white-spaces

In reply to Junio C Hamano

Currently, it's not possible to use the space-ignoring options (-b, -w, --ignore-space-at-eol) with combined diff. It makes it pretty impossible to read a merge between a branch that changed all tabs to spaces, and a branch with functional changes.

Pass diff flags to diff engine, so that combined diff behaves as normal diff does with spaces.
Also coalesce lines that are removed from both (or more) parents.

It also means that a conflict-less merge done using a ignore-* strategy option will not show any conflict if shown in combined-diff using the same option.

---
Changes:
- Fixed comments
- Fixed tests (following Johannes suggestions)

 combine-diff.c           |   57 +++++++++++++++++++++---
 t/t4038-diff-combined.sh |  111 ++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 161 insertions(+), 7 deletions(-)
combine-diff.c +50 -7
index 35d41cd..6288485 100644
@@ -5,6 +5,7 @@
 #include "diffcore.h"
 #include "quote.h"
 #include "xdiff-interface.h"
+#include "xdiff/xmacros.h"
 #include "log-tree.h"
 #include "refs.h"
 #include "userdiff.h"
@@ -122,7 +123,47 @@ static char *grab_blob(const unsigned char *sha1, unsigned int mode,
 	return blob;
 }
 
-static void append_lost(struct sline *sline, int n, const char *line, int len)
+static int match_string_spaces(const char *line1, int len1,
+			       const char *line2, int len2,
+			       long flags)
+{
+	if (flags & XDF_WHITESPACE_FLAGS) {
+		for (; len1 > 0 && XDL_ISSPACE(line1[len1 - 1]); len1--);
+		for (; len2 > 0 && XDL_ISSPACE(line2[len2 - 1]); len2--);
+	}
+
+	if (!(flags & (XDF_IGNORE_WHITESPACE | XDF_IGNORE_WHITESPACE_CHANGE)))
+		return (len1 == len2 && !memcmp(line1, line2, len1));
+
+	while (len1 > 0 && len2 > 0) {
+		len1--;
+		len2--;
+		if (XDL_ISSPACE(line1[len1]) || XDL_ISSPACE(line2[len2])) {
+			if ((flags & XDF_IGNORE_WHITESPACE_CHANGE) &&
+			    (!XDL_ISSPACE(line1[len1]) || !XDL_ISSPACE(line2[len2])))
+				return 0;
+
+			for (; len1 > 0 && XDL_ISSPACE(line1[len1]); len1--);
+			for (; len2 > 0 && XDL_ISSPACE(line2[len2]); len2--);
+		}
+		if (line1[len1] != line2[len2])
+			return 0;
+	}
+
+	if (flags & XDF_IGNORE_WHITESPACE) {
+		/* Consume remaining spaces */
+		for (; len1 > 0 && XDL_ISSPACE(line1[len1 - 1]); len1--);
+		for (; len2 > 0 && XDL_ISSPACE(line2[len2 - 1]); len2--);
+	}
+
+	/* We matched full line1 and line2 */
+	if (!len1 && !len2)
+		return 1;
+
+	return 0;
+}
+
+static void append_lost(struct sline *sline, int n, const char *line, int len, long flags)
 {
 	struct lline *lline;
 	unsigned long this_mask = (1UL<<n);
@@ -133,8 +174,8 @@ static void append_lost(struct sline *sline, int n, const char *line, int len)
 	if (sline->lost_head) {
 		lline = sline->next_lost;
 		while (lline) {
-			if (lline->len == len &&
-			    !memcmp(lline->line, line, len)) {
+			if (match_string_spaces(lline->line, lline->len,
+						line, len, flags)) {
 				lline->parent_map |= this_mask;
 				sline->next_lost = lline->next;
 				return;
@@ -162,6 +203,7 @@ struct combine_diff_state {
 	int n;
 	struct sline *sline;
 	struct sline *lost_bucket;
+	long flags;
 };
 
 static void consume_line(void *state_, char *line, unsigned long len)
@@ -201,7 +243,7 @@ static void consume_line(void *state_, char *line, unsigned long len)
 		return; /* not in any hunk yet */
 	switch (line[0]) {
 	case '-':
-		append_lost(state->lost_bucket, state->n, line+1, len-1);
+		append_lost(state->lost_bucket, state->n, line+1, len-1, state->flags);
 		break;
 	case '+':
 		state->sline[state->lno-1].flag |= state->nmask;
@@ -215,7 +257,7 @@ static void combine_diff(const unsigned char *parent, unsigned int mode,
 			 struct sline *sline, unsigned int cnt, int n,
 			 int num_parent, int result_deleted,
 			 struct userdiff_driver *textconv,
-			 const char *path)
+			 const char *path, long flags)
 {
 	unsigned int p_lno, lno;
 	unsigned long nmask = (1UL << n);
@@ -231,9 +273,10 @@ static void combine_diff(const unsigned char *parent, unsigned int mode,
 	parent_file.ptr = grab_blob(parent, mode, &sz, textconv, path);
 	parent_file.size = sz;
 	memset(&xpp, 0, sizeof(xpp));
-	xpp.flags = 0;
+	xpp.flags = flags;
 	memset(&xecfg, 0, sizeof(xecfg));
 	memset(&state, 0, sizeof(state));
+	state.flags = flags;
 	state.nmask = nmask;
 	state.sline = sline;
 	state.lno = 1;
@@ -962,7 +1005,7 @@ static void show_patch_diff(struct combine_diff_path *elem, int num_parent,
 				     elem->parent[i].mode,
 				     &result_file, sline,
 				     cnt, i, num_parent, result_deleted,
-				     textconv, elem->path);
+				     textconv, elem->path, opt->xdl_opts);
 	}
 
 	show_hunks = make_hunks(sline, cnt, num_parent, dense);
t/t4038-diff-combined.sh +111 -0

121 lines not on this page · raw message

--
1.7.9.5

#10 lore raw
Quoted 10 lines · Antoine Pelisse
t/t4038-diff-combined.sh +10 -0
+test_expect_success 'check combined output (no ignore space)' '
+	git show >actual.tmp &&
+	sed -e "1,/^@@@/d" < actual.tmp >actual &&
+	tr -d Q <<-\EOF >expected &&
+	--always coalesce
+	- eol space coalesce
+	- space change coalesce
+	- all spaces coalesce
+	- eol spaces
+	- space change

t/t4038-diff-combined.sh test_expect_success 'check --cc --raw with forty trees' ' · line 174

Quoted 10 lines
t/t4038-diff-combined.sh +10 -0
+	- all spaces
+	 -eol space coalesce Q
+	 -space  change coalesce
+	 -all spa ces coalesce
+	+ eol spaces Q
+	+ space  change
+	+ all spa ces
+	EOF
+	compare_diff_patch expected actual
+'

Nicely constructed ;-)

Thanks.