All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH] Allow combined diff to ignore white-spaces
@ 2013-03-02 15:04 Antoine Pelisse
  2013-03-03  7:45 ` Junio C Hamano
  0 siblings, 1 reply; 13+ messages in thread
From: Antoine Pelisse @ 2013-03-02 15:04 UTC (permalink / raw)
  To: git; +Cc: 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.

Signed-off-by: Antoine Pelisse <apelisse@gmail.com>
---
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(-)

diff --git a/combine-diff.c b/combine-diff.c
index 35d41cd..7ca0a72 100644
--- a/combine-diff.c
+++ b/combine-diff.c
@@ -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

^ permalink raw reply related	[flat|nested] 13+ messages in thread

* Re: [PATCH] Allow combined diff to ignore white-spaces
  2013-03-02 15:04 [PATCH] Allow combined diff to ignore white-spaces Antoine Pelisse
@ 2013-03-03  7:45 ` Junio C Hamano
  2013-03-04 18:17   ` Antoine Pelisse
  2013-03-13 21:21   ` Antoine Pelisse
  0 siblings, 2 replies; 13+ messages in thread
From: Junio C Hamano @ 2013-03-03  7:45 UTC (permalink / raw)
  To: Antoine Pelisse; +Cc: git

Antoine Pelisse <apelisse@gmail.com> writes:

> 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.
>
> Signed-off-by: Antoine Pelisse <apelisse@gmail.com>
> ---
> 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.

> Cheers,
> Antoine
>
>  combine-diff.c |    6 +++---
>  1 file changed, 3 insertions(+), 3 deletions(-)
>
> diff --git a/combine-diff.c b/combine-diff.c
> index 35d41cd..7ca0a72 100644
> --- a/combine-diff.c
> +++ b/combine-diff.c
> @@ -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

^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: [PATCH] Allow combined diff to ignore white-spaces
  2013-03-03  7:45 ` Junio C Hamano
@ 2013-03-04 18:17   ` Antoine Pelisse
  2013-03-04 18:36     ` Junio C Hamano
  2013-03-13 21:21   ` Antoine Pelisse
  1 sibling, 1 reply; 13+ messages in thread
From: Antoine Pelisse @ 2013-03-04 18:17 UTC (permalink / raw)
  To: Junio C Hamano; +Cc: git

>> 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 ;)

> 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.

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

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

I will definitely add some tests.

> 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.

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 ?

diff --cc ten
index d213a99,ed40ab2..37c2af2
--- a/ten
+++ b/ten
@@@ -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

^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: [PATCH] Allow combined diff to ignore white-spaces
  2013-03-04 18:17   ` Antoine Pelisse
@ 2013-03-04 18:36     ` Junio C Hamano
  2013-03-04 18:50       ` Antoine Pelisse
  0 siblings, 1 reply; 13+ messages in thread
From: Junio C Hamano @ 2013-03-04 18:36 UTC (permalink / raw)
  To: Antoine Pelisse; +Cc: git

Antoine Pelisse <apelisse@gmail.com> writes:

> 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?

> 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?

^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: [PATCH] Allow combined diff to ignore white-spaces
  2013-03-04 18:36     ` Junio C Hamano
@ 2013-03-04 18:50       ` Antoine Pelisse
  0 siblings, 0 replies; 13+ messages in thread
From: Antoine Pelisse @ 2013-03-04 18:50 UTC (permalink / raw)
  To: Junio C Hamano; +Cc: git

On Mon, Mar 4, 2013 at 7:36 PM, Junio C Hamano <gitster@pobox.com> wrote:
> Antoine Pelisse <apelisse@gmail.com> writes:
>
>> 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?

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

>> 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.

^ permalink raw reply	[flat|nested] 13+ messages in thread

* [PATCH] Allow combined diff to ignore white-spaces
  2013-03-03  7:45 ` Junio C Hamano
  2013-03-04 18:17   ` Antoine Pelisse
@ 2013-03-13 21:21   ` Antoine Pelisse
  2013-03-13 23:42     ` Junio C Hamano
                       ` (2 more replies)
  1 sibling, 3 replies; 13+ messages in thread
From: Antoine Pelisse @ 2013-03-13 21:21 UTC (permalink / raw)
  To: git; +Cc: Junio C Hamano, 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.

Signed-off-by: Antoine Pelisse <apelisse@gmail.com>
---
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(-)

diff --git a/combine-diff.c b/combine-diff.c
index 35d41cd..0f33983 100644
--- a/combine-diff.c
+++ b/combine-diff.c
@@ -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);
diff --git a/t/t4038-diff-combined.sh b/t/t4038-diff-combined.sh
index 614425a..ba8a56b 100755
--- a/t/t4038-diff-combined.sh
+++ b/t/t4038-diff-combined.sh
@@ -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

^ permalink raw reply related	[flat|nested] 13+ messages in thread

* Re: [PATCH] Allow combined diff to ignore white-spaces
  2013-03-13 21:21   ` Antoine Pelisse
@ 2013-03-13 23:42     ` Junio C Hamano
  2013-03-13 23:53     ` Junio C Hamano
  2013-03-14  7:08     ` Johannes Sixt
  2 siblings, 0 replies; 13+ messages in thread
From: Junio C Hamano @ 2013-03-13 23:42 UTC (permalink / raw)
  To: Antoine Pelisse; +Cc: git

Antoine Pelisse <apelisse@gmail.com> writes:

> 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.
>
> Signed-off-by: Antoine Pelisse <apelisse@gmail.com>
> ---
> 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(-)
>
> diff --git a/combine-diff.c b/combine-diff.c
> index 35d41cd..0f33983 100644
> --- a/combine-diff.c
> +++ b/combine-diff.c
> @@ -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--);
> +	}

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.

> +	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.

> +	while (len1 > 0 && len2 > 0) {
> +		len1--;
> +		len2--;

Scanning from the tail end of the strings...

> +		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.

> +			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.

> +		}
> +		if (line1[len1] != line2[len2])
> +			return 0;

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

> +	}
> +
> +	if (flags & XDF_IGNORE_WHITESPACE) {
> +		// Consume remaining spaces

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

> +		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.

> +	}
> +
> +	// We matched full line1 and line2

Likewise for "//".

> +	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;

Nicely done.

Thanks, will queue with comment style fix-ups.

> diff --git a/t/t4038-diff-combined.sh b/t/t4038-diff-combined.sh
> index 614425a..ba8a56b 100755
> --- a/t/t4038-diff-combined.sh
> +++ b/t/t4038-diff-combined.sh
> @@ -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

^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: [PATCH] Allow combined diff to ignore white-spaces
  2013-03-13 21:21   ` Antoine Pelisse
  2013-03-13 23:42     ` Junio C Hamano
@ 2013-03-13 23:53     ` Junio C Hamano
  2013-03-14  7:08     ` Johannes Sixt
  2 siblings, 0 replies; 13+ messages in thread
From: Junio C Hamano @ 2013-03-13 23:53 UTC (permalink / raw)
  To: Antoine Pelisse; +Cc: git

Antoine Pelisse <apelisse@gmail.com> writes:

> 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?

> +test_expect_success 'check combined output (no ignore space)' '
> +	git show | test_i18ngrep "^-\s*eol spaces" &&

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

^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: [PATCH] Allow combined diff to ignore white-spaces
  2013-03-13 21:21   ` Antoine Pelisse
  2013-03-13 23:42     ` Junio C Hamano
  2013-03-13 23:53     ` Junio C Hamano
@ 2013-03-14  7:08     ` Johannes Sixt
  2013-03-14 15:31       ` Junio C Hamano
  2 siblings, 1 reply; 13+ messages in thread
From: Johannes Sixt @ 2013-03-14  7:08 UTC (permalink / raw)
  To: Antoine Pelisse; +Cc: git, Junio C Hamano

Am 3/13/2013 22:21, schrieb 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.
> 
> Signed-off-by: Antoine Pelisse <apelisse@gmail.com>
> ---
> 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(-)
> 
> diff --git a/combine-diff.c b/combine-diff.c
> index 35d41cd..0f33983 100644
> --- a/combine-diff.c
> +++ b/combine-diff.c
> @@ -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);
> diff --git a/t/t4038-diff-combined.sh b/t/t4038-diff-combined.sh
> index 614425a..ba8a56b 100755
> --- a/t/t4038-diff-combined.sh
> +++ b/t/t4038-diff-combined.sh
> @@ -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 &&

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

> +	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 &&

Ditto.

> +	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

^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: [PATCH] Allow combined diff to ignore white-spaces
  2013-03-14  7:08     ` Johannes Sixt
@ 2013-03-14 15:31       ` Junio C Hamano
  2013-03-14 15:51         ` Antoine Pelisse
  2013-03-14 21:03         ` [PATCH v3] " Antoine Pelisse
  0 siblings, 2 replies; 13+ messages in thread
From: Junio C Hamano @ 2013-03-14 15:31 UTC (permalink / raw)
  To: Johannes Sixt; +Cc: Antoine Pelisse, git

Johannes Sixt <j.sixt@viscovery.net> writes:

> This form of 'echo' is not sufficiently portable. How about:
>
> 	tr -d Q <<-\EOF >test &&
>
> 	always coalesce
> 	eol space coalesce Q
> ...
> 	EOF

Much better.

>> +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.)
>
> 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.

^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: [PATCH] Allow combined diff to ignore white-spaces
  2013-03-14 15:31       ` Junio C Hamano
@ 2013-03-14 15:51         ` Antoine Pelisse
  2013-03-14 21:03         ` [PATCH v3] " Antoine Pelisse
  1 sibling, 0 replies; 13+ messages in thread
From: Antoine Pelisse @ 2013-03-14 15:51 UTC (permalink / raw)
  To: Junio C Hamano; +Cc: Johannes Sixt, git

On Thu, Mar 14, 2013 at 4:31 PM, Junio C Hamano <gitster@pobox.com> wrote:
> Johannes Sixt <j.sixt@viscovery.net> writes:
>
>> This form of 'echo' is not sufficiently portable. How about:
>>
>>       tr -d Q <<-\EOF >test &&
>>
>>       always coalesce
>>       eol space coalesce Q
>> ...
>>       EOF
>
> Much better.
>
>>> +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.)
>>
>> 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,

^ permalink raw reply	[flat|nested] 13+ messages in thread

* [PATCH v3] Allow combined diff to ignore white-spaces
  2013-03-14 15:31       ` Junio C Hamano
  2013-03-14 15:51         ` Antoine Pelisse
@ 2013-03-14 21:03         ` Antoine Pelisse
  2013-03-14 21:43           ` Junio C Hamano
  1 sibling, 1 reply; 13+ messages in thread
From: Antoine Pelisse @ 2013-03-14 21:03 UTC (permalink / raw)
  To: git; +Cc: Junio C Hamano, Johannes Sixt, 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.

Signed-off-by: Antoine Pelisse <apelisse@gmail.com>
---
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(-)

diff --git a/combine-diff.c b/combine-diff.c
index 35d41cd..6288485 100644
--- a/combine-diff.c
+++ b/combine-diff.c
@@ -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);
diff --git a/t/t4038-diff-combined.sh b/t/t4038-diff-combined.sh
index 614425a..b7e16a7 100755
--- a/t/t4038-diff-combined.sh
+++ b/t/t4038-diff-combined.sh
@@ -3,6 +3,7 @@
 test_description='combined diff'

 . ./test-lib.sh
+. "$TEST_DIRECTORY"/diff-lib.sh

 setup_helper () {
 	one=$1 branch=$2 side=$3 &&
@@ -113,4 +114,114 @@ 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 &&
+
+	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
+	git commit -m "test space change" -a &&
+
+	git checkout -b side HEAD^ &&
+	tr -d Q <<-\EOF >test &&
+	always coalesce
+	eol space coalesce
+	space change coalesce
+	all spaces coalesce
+	eol spaces
+	space change
+	all spaces
+	EOF
+	git commit -m "test other space changes" -a &&
+
+	test_must_fail git merge master &&
+	tr -d Q <<-\EOF >test &&
+	eol spaces Q
+	space  change
+	all spa ces
+	EOF
+	git commit -m merged -a
+'
+
+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
+	- 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
+'
+
+test_expect_success 'check combined output (ignore space at eol)' '
+	git show --ignore-space-at-eol >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
+	 -space  change coalesce
+	 -all spa ces coalesce
+	  eol spaces Q
+	- space change
+	- all spaces
+	+ space  change
+	+ all spa ces
+	EOF
+	compare_diff_patch expected actual
+'
+
+test_expect_success 'check combined output (ignore space change)' '
+	git show -b >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
+	 -all spa ces coalesce
+	  eol spaces Q
+	  space  change
+	- all spaces
+	+ all spa ces
+	EOF
+	compare_diff_patch expected actual
+'
+
+test_expect_success 'check combined output (ignore all spaces)' '
+	git show -w >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 Q
+	  space  change
+	  all spa ces
+	EOF
+	compare_diff_patch expected actual
+'
+
 test_done
--
1.7.9.5

^ permalink raw reply related	[flat|nested] 13+ messages in thread

* Re: [PATCH v3] Allow combined diff to ignore white-spaces
  2013-03-14 21:03         ` [PATCH v3] " Antoine Pelisse
@ 2013-03-14 21:43           ` Junio C Hamano
  0 siblings, 0 replies; 13+ messages in thread
From: Junio C Hamano @ 2013-03-14 21:43 UTC (permalink / raw)
  To: Antoine Pelisse; +Cc: git, Johannes Sixt

Antoine Pelisse <apelisse@gmail.com> writes:

> +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
> +	- 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.

^ permalink raw reply	[flat|nested] 13+ messages in thread

end of thread, other threads:[~2013-03-14 21:43 UTC | newest]

Thread overview: 13+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2013-03-02 15:04 [PATCH] Allow combined diff to ignore white-spaces Antoine Pelisse
2013-03-03  7:45 ` Junio C Hamano
2013-03-04 18:17   ` Antoine Pelisse
2013-03-04 18:36     ` Junio C Hamano
2013-03-04 18:50       ` Antoine Pelisse
2013-03-13 21:21   ` Antoine Pelisse
2013-03-13 23:42     ` Junio C Hamano
2013-03-13 23:53     ` Junio C Hamano
2013-03-14  7:08     ` Johannes Sixt
2013-03-14 15:31       ` Junio C Hamano
2013-03-14 15:51         ` Antoine Pelisse
2013-03-14 21:03         ` [PATCH v3] " Antoine Pelisse
2013-03-14 21:43           ` Junio C Hamano

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.