git@vger.kernel.org mailing list mirror (one of many)
 help / color / mirror / code / Atom feed
* range-diff should suppress context-only changes?
@ 2020-11-05 13:34 Jeff King
  2020-11-05 20:55 ` Junio C Hamano
  0 siblings, 1 reply; 7+ messages in thread
From: Jeff King @ 2020-11-05 13:34 UTC (permalink / raw)
  To: git; +Cc: Elijah Newren

On Thu, Nov 05, 2020 at 12:22:32AM +0000, Elijah Newren via GitGitGadget wrote:

> Range-diff vs v3:
> [...]
>   7:  42633b8d03 !  7:  5e8004c728 strmap: add more utility functions
>      @@ strmap.h: void *strmap_get(struct strmap *map, const char *str);
>       + * iterate through @map using @iter, @var is a pointer to a type strmap_entry
>       + */
>       +#define strmap_for_each_entry(mystrmap, iter, var)	\
>      -+	for (var = hashmap_iter_first_entry_offset(&(mystrmap)->map, iter, 0); \
>      -+		var; \
>      -+		var = hashmap_iter_next_entry_offset(iter, 0))
>      ++	hashmap_for_each_entry(&(mystrmap)->map, iter, var, ent)
>       +
>        #endif /* STRMAP_H */
>   8:  ea942eb803 =  8:  fd96e9fc8d strmap: enable faster clearing and reusing of strmaps
>   9:  c1d2172171 !  9:  f499934f54 strmap: add functions facilitating use as a string->int map
> [...]
>       @@ strmap.h: static inline int strmap_empty(struct strmap *map)
>      - 		var; \
>      - 		var = hashmap_iter_next_entry_offset(iter, 0))
>      + #define strmap_for_each_entry(mystrmap, iter, var)	\
>      + 	hashmap_for_each_entry(&(mystrmap)->map, iter, var, ent)
>        
>       +
>       +/*
>      @@ strmap.h: static inline int strmap_empty(struct strmap *map)

Definitely not a problem with your patches, but I noticed this curiosity
in the range-diff. Patch 7 changes the definition of the macro, but it
gets mentioned again in patch 9, even though the code wasn't touched.
The issue is that it the change from 7 ends up in the context of 9; the
actual modification in patch 9 is in those final couple lines touching a
comment (and they didn't change at all between the two versions).

I wonder if it would be reasonable to suppress range-diff hunks in which
all of the changed lines are context lines.

-Peff

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

* Re: range-diff should suppress context-only changes?
  2020-11-05 13:34 range-diff should suppress context-only changes? Jeff King
@ 2020-11-05 20:55 ` Junio C Hamano
  2020-11-17 21:35   ` Johannes Altmanninger
  0 siblings, 1 reply; 7+ messages in thread
From: Junio C Hamano @ 2020-11-05 20:55 UTC (permalink / raw)
  To: Jeff King; +Cc: git, Elijah Newren

Jeff King <peff@peff.net> writes:

> On Thu, Nov 05, 2020 at 12:22:32AM +0000, Elijah Newren via GitGitGadget wrote:
>
>> Range-diff vs v3:
>> [...]
>>   7:  42633b8d03 !  7:  5e8004c728 strmap: add more utility functions
>>      @@ strmap.h: void *strmap_get(struct strmap *map, const char *str);
>>       + * iterate through @map using @iter, @var is a pointer to a type strmap_entry
>>       + */
>>       +#define strmap_for_each_entry(mystrmap, iter, var)	\
>>      -+	for (var = hashmap_iter_first_entry_offset(&(mystrmap)->map, iter, 0); \
>>      -+		var; \
>>      -+		var = hashmap_iter_next_entry_offset(iter, 0))
>>      ++	hashmap_for_each_entry(&(mystrmap)->map, iter, var, ent)
>>       +
>>        #endif /* STRMAP_H */
>>   8:  ea942eb803 =  8:  fd96e9fc8d strmap: enable faster clearing and reusing of strmaps
>>   9:  c1d2172171 !  9:  f499934f54 strmap: add functions facilitating use as a string->int map
>> [...]
>>       @@ strmap.h: static inline int strmap_empty(struct strmap *map)
>>      - 		var; \
>>      - 		var = hashmap_iter_next_entry_offset(iter, 0))
>>      + #define strmap_for_each_entry(mystrmap, iter, var)	\
>>      + 	hashmap_for_each_entry(&(mystrmap)->map, iter, var, ent)
>>        
>>       +
>>       +/*
>>      @@ strmap.h: static inline int strmap_empty(struct strmap *map)
>
> Definitely not a problem with your patches, but I noticed this curiosity
> in the range-diff. Patch 7 changes the definition of the macro, but it
> gets mentioned again in patch 9, even though the code wasn't touched.
> The issue is that it the change from 7 ends up in the context of 9; the
> actual modification in patch 9 is in those final couple lines touching a
> comment (and they didn't change at all between the two versions).
>
> I wonder if it would be reasonable to suppress range-diff hunks in which
> all of the changed lines are context lines.

Sounds like a reasonable thing to do.  As we know the shape of what
is compared in the outer diff we should be able to accurately notice
where hunk boundaries are and a hunk whose change is only on context
lines.


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

* Re: range-diff should suppress context-only changes?
  2020-11-05 20:55 ` Junio C Hamano
@ 2020-11-17 21:35   ` Johannes Altmanninger
  2020-11-17 21:35     ` [PATCH 1/3] range-diff: move " ## filename ##" headers to the first column Johannes Altmanninger
                       ` (2 more replies)
  0 siblings, 3 replies; 7+ messages in thread
From: Johannes Altmanninger @ 2020-11-17 21:35 UTC (permalink / raw)
  To: gitster; +Cc: git, newren, peff

> > I wonder if it would be reasonable to suppress range-diff hunks in which
> > all of the changed lines are context lines.
> 
> Sounds like a reasonable thing to do.  As we know the shape of what
> is compared in the outer diff we should be able to accurately notice
> where hunk boundaries are and a hunk whose change is only on context
> lines.

Here are patches to ignore context-only changes in range-diff's output.
I'm not completely happy with the changes, they feel a bit too hacky.
Maybe someone has better ideas.

This still gives output like this one, that could be improved in future

	1:  7a3dac8 ! 1:  119bc78 Change
	    @@ some-other-file
	      7
	      8
	      9
	    - Old context line
	    + New context line

	    -## file ##
	    +## file => renamed-file ##
	     @@
	      1
	     -2


I think it should be

	1:  7a3dac8 ! 1:  119bc78 Change
	    -## file ##
	    +## file => renamed-file ##
	     @@
	      1
	     -2

I'm not sure if this is a feasible improvement.

"## <filename> ##" normally is a diff
section header (hence the "@@ some-other-file" hunk above) but here the
section header itself is changed..



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

* [PATCH 1/3] range-diff: move " ## filename ##" headers to the first column
  2020-11-17 21:35   ` Johannes Altmanninger
@ 2020-11-17 21:35     ` Johannes Altmanninger
  2020-11-17 21:35     ` [PATCH 2/3] range-diff: ignore context-only changes Johannes Altmanninger
  2020-11-17 21:35     ` [PATCH 3/3] range-diff: only compute patch diff when patches are different Johannes Altmanninger
  2 siblings, 0 replies; 7+ messages in thread
From: Johannes Altmanninger @ 2020-11-17 21:35 UTC (permalink / raw)
  To: gitster; +Cc: git, newren, peff, Johannes Altmanninger

Output of range-diff may include comparisons of metadata like commit messages
and filenames. Metadata lines look like " ## <content> ##".

When range-diff compares two matching commits, it computes a diff of two
special commit diffs. In these commit diffs, each changed file is introduced
with a " ## filename ##" line which is followed by the diff hunks with changes
to the file's contents.

The leading space makes it hard to distinguish between file metadata lines
and context lines from a diff hunk, especially when looking only at the
output of range-diff.  Drop the space prefix to facilitate that.
---
 range-diff.c          |  4 ++--
 t/t3206-range-diff.sh | 42 +++++++++++++++++++++---------------------
 2 files changed, 23 insertions(+), 23 deletions(-)

diff --git a/range-diff.c b/range-diff.c
index 24dc435e48..72660453bd 100644
--- a/range-diff.c
+++ b/range-diff.c
@@ -136,7 +136,7 @@ static int read_patches(const char *range, struct string_list *list,
 			if (len < 0)
 				die(_("could not parse git header '%.*s'"),
 				    orig_len, line);
-			strbuf_addstr(&buf, " ## ");
+			strbuf_addstr(&buf, "## ");
 			if (patch.is_new > 0)
 				strbuf_addf(&buf, "%s (new)", patch.new_name);
 			else if (patch.is_delete > 0)
@@ -432,7 +432,7 @@ static void output_pair_header(struct diff_options *diffopt,
 }
 
 static struct userdiff_driver section_headers = {
-	.funcname = { "^ ## (.*) ##$\n"
+	.funcname = { "^ ?## (.*) ##$\n"
 		      "^.?@@ (.*)$", REG_EXTENDED }
 };
 
diff --git a/t/t3206-range-diff.sh b/t/t3206-range-diff.sh
index 6eb344be03..f875843b5e 100755
--- a/t/t3206-range-diff.sh
+++ b/t/t3206-range-diff.sh
@@ -304,8 +304,8 @@ test_expect_success 'renamed file' '
 	    -    s/4/A/
 	    +    s/4/A/ + rename file
 	    Z
-	    - ## file ##
-	    + ## file => renamed-file ##
+	    -## file ##
+	    +## file => renamed-file ##
 	    Z@@
 	    Z 1
 	    Z 2
@@ -314,9 +314,9 @@ test_expect_success 'renamed file' '
 	    Z ## Commit message ##
 	    Z    s/11/B/
 	    Z
-	    - ## file ##
+	    -## file ##
 	    -@@ file: A
-	    + ## renamed-file ##
+	    +## renamed-file ##
 	    +@@ renamed-file: A
 	    Z 8
 	    Z 9
@@ -326,9 +326,9 @@ test_expect_success 'renamed file' '
 	    Z ## Commit message ##
 	    Z    s/12/B/
 	    Z
-	    - ## file ##
+	    -## file ##
 	    -@@ file: A
-	    + ## renamed-file ##
+	    +## renamed-file ##
 	    +@@ renamed-file: A
 	    Z 9
 	    Z 10
@@ -348,14 +348,14 @@ test_expect_success 'file with mode only change' '
 	    -    s/4/A/
 	    +    s/4/A/ + add other-file
 	    Z
-	    Z ## file ##
+	    Z## file ##
 	    Z@@
 	    @@ file
 	    Z A
 	    Z 6
 	    Z 7
 	    +
-	    + ## other-file (new) ##
+	    +## other-file (new) ##
 	2:  $(test_oid t3) ! 2:  $(test_oid o2) s/11/B/
 	    @@ Metadata
 	    ZAuthor: Thomas Rast <trast@inf.ethz.ch>
@@ -364,14 +364,14 @@ test_expect_success 'file with mode only change' '
 	    -    s/11/B/
 	    +    s/11/B/ + mode change other-file
 	    Z
-	    Z ## file ##
+	    Z## file ##
 	    Z@@ file: A
 	    @@ file: A
 	    Z 12
 	    Z 13
 	    Z 14
 	    +
-	    + ## other-file (mode change 100644 => 100755) ##
+	    +## other-file (mode change 100644 => 100755) ##
 	3:  $(test_oid t4) = 3:  $(test_oid o3) s/12/B/
 	EOF
 	test_cmp expect actual
@@ -389,14 +389,14 @@ test_expect_success 'file added and later removed' '
 	    -    s/4/A/
 	    +    s/4/A/ + new-file
 	    Z
-	    Z ## file ##
+	    Z## file ##
 	    Z@@
 	    @@ file
 	    Z A
 	    Z 6
 	    Z 7
 	    +
-	    + ## new-file (new) ##
+	    +## new-file (new) ##
 	3:  $(test_oid t3) ! 3:  $(test_oid s3) s/11/B/
 	    @@ Metadata
 	    ZAuthor: Thomas Rast <trast@inf.ethz.ch>
@@ -405,14 +405,14 @@ test_expect_success 'file added and later removed' '
 	    -    s/11/B/
 	    +    s/11/B/ + remove file
 	    Z
-	    Z ## file ##
+	    Z## file ##
 	    Z@@ file: A
 	    @@ file: A
 	    Z 12
 	    Z 13
 	    Z 14
 	    +
-	    + ## new-file (deleted) ##
+	    +## new-file (deleted) ##
 	4:  $(test_oid t4) = 4:  $(test_oid s4) s/12/B/
 	EOF
 	test_cmp expect actual
@@ -434,7 +434,7 @@ test_expect_success 'changed message' '
 	    Z
 	    +    Also a silly comment here!
 	    +
-	    Z ## file ##
+	    Z## file ##
 	    Z@@
 	    Z 1
 	3:  $(test_oid t3) = 3:  $(test_oid m3) s/11/B/
@@ -453,7 +453,7 @@ test_expect_success 'dual-coloring' '
 	:     <RESET>
 	:    <REVERSE><GREEN>+<RESET><BOLD>    Also a silly comment here!<RESET>
 	:    <REVERSE><GREEN>+<RESET>
-	:      ## file ##<RESET>
+	:     ## file ##<RESET>
 	:    <CYAN> @@<RESET>
 	:      1<RESET>
 	:<RED>3:  $(test_oid c3) <RESET><YELLOW>!<RESET><GREEN> 3:  $(test_oid m3)<RESET><YELLOW> s/11/B/<RESET>
@@ -537,7 +537,7 @@ test_expect_success 'range-diff compares notes by default' '
 	    -    topic note
 	    +    unmodified note
 	    Z
-	    Z ## file ##
+	    Z## file ##
 	    Z@@ file: A
 	EOF
 	test_cmp expect actual
@@ -584,7 +584,7 @@ test_expect_success 'range-diff with multiple --notes' '
 	    -    topic note2
 	    +    unmodified note2
 	    Z
-	    Z ## file ##
+	    Z## file ##
 	    Z@@ file: A
 	EOF
 	test_cmp expect actual
@@ -645,7 +645,7 @@ test_expect_success 'format-patch --range-diff with --notes' '
 	    -    topic note
 	    +    unmodified note
 	    Z
-	    Z ## file ##
+	    Z## file ##
 	    Z@@ file: A
 	EOF
 	sed "/@@ Commit message/,/@@ file: A/!d" 0000-* >actual &&
@@ -674,7 +674,7 @@ test_expect_success 'format-patch --range-diff with format.notes config' '
 	    -    topic note
 	    +    unmodified note
 	    Z
-	    Z ## file ##
+	    Z## file ##
 	    Z@@ file: A
 	EOF
 	sed "/@@ Commit message/,/@@ file: A/!d" 0000-* >actual &&
@@ -710,7 +710,7 @@ test_expect_success 'format-patch --range-diff with multiple notes' '
 	    -    topic note2
 	    +    unmodified note2
 	    Z
-	    Z ## file ##
+	    Z## file ##
 	    Z@@ file: A
 	EOF
 	sed "/@@ Commit message/,/@@ file: A/!d" 0000-* >actual &&
-- 
2.29.2


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

* [PATCH 2/3] range-diff: ignore context-only changes
  2020-11-17 21:35   ` Johannes Altmanninger
  2020-11-17 21:35     ` [PATCH 1/3] range-diff: move " ## filename ##" headers to the first column Johannes Altmanninger
@ 2020-11-17 21:35     ` Johannes Altmanninger
  2020-11-17 22:56       ` Eric Sunshine
  2020-11-17 21:35     ` [PATCH 3/3] range-diff: only compute patch diff when patches are different Johannes Altmanninger
  2 siblings, 1 reply; 7+ messages in thread
From: Johannes Altmanninger @ 2020-11-17 21:35 UTC (permalink / raw)
  To: gitster; +Cc: git, newren, peff, Johannes Altmanninger

range-diff compares matching commits by comparing their patches against
each other. When two patches only differ in their context lines, that
difference would still show up in range-diff's output.

This commit uses diff's new -I/--ignore-matching-lines regex logic to ignore
diff hunks that only consist of changes to context lines in the input diffs.

Thanks to the previous commit, lines like "## file => renamed-file ##"
are not considered context lines because they no longer have a leading space.

This gives some extra @@ lines because we now always calculate
two diffs: one for the patch metadata, like the commit message,
and another one for the actual file changes.
This is because the former contains lines with leading spaces that are not
context lines, so we never want to ignore them.
---
 range-diff.c          | 58 +++++++++++++++++++++++++-----
 t/t3206-range-diff.sh | 83 ++++++-------------------------------------
 2 files changed, 60 insertions(+), 81 deletions(-)

diff --git a/range-diff.c b/range-diff.c
index 72660453bd..df2147ef79 100644
--- a/range-diff.c
+++ b/range-diff.c
@@ -363,6 +363,31 @@ static void get_correspondences(struct string_list *a, struct string_list *b,
 	free(b2a);
 }
 
+static int are_diffs_equivalent(const char *a_diff, const char *b_diff) {
+	for (
+		const char
+			*a_eol = strchr(a_diff, '\n'),
+			*b_eol = strchr(b_diff,	'\n');
+		(a_eol = strchr(a_diff,	'\n')) &&
+		(b_eol = strchr(b_diff,	'\n'));
+		a_diff = a_eol + 1, b_diff = b_eol + 1
+	) {
+		if (!!a_eol != !!b_eol)
+			return 0;
+
+		// Ignore context lines.
+		if (a_diff[0] == ' ' &&	b_diff[0] == ' ')
+			continue;
+
+		size_t a_len = a_eol - a_diff;
+		size_t b_len = b_eol - b_diff;
+		if (a_len != b_len || strncmp(a_diff, b_diff, a_len))
+			return 0;
+	}
+
+	return 1;
+}
+
 static void output_pair_header(struct diff_options *diffopt,
 			       int patch_no_width,
 			       struct strbuf *buf,
@@ -390,8 +415,10 @@ static void output_pair_header(struct diff_options *diffopt,
 	} else if (!a_util) {
 		color = color_new;
 		status = '>';
-	} else if (strcmp(a_util->patch, b_util->patch)) {
-		color = color_commit;
+	} else if (a_util->diff_offset != b_util->diff_offset
+		   || strncmp(a_util->patch, b_util->patch, a_util->diff_offset)
+		   || !are_diffs_equivalent(a_util->diff, b_util->diff)) {
+		color =	color_commit;
 		status = '!';
 	} else {
 		color = color_commit;
@@ -436,13 +463,13 @@ static struct userdiff_driver section_headers = {
 		      "^.?@@ (.*)$", REG_EXTENDED }
 };
 
-static struct diff_filespec *get_filespec(const char *name, const char *p)
+static struct diff_filespec *get_filespec(const char *name, const char *p, size_t size)
 {
 	struct diff_filespec *spec = alloc_filespec(name);
 
 	fill_filespec(spec, &null_oid, 0, 0100644);
 	spec->data = (char *)p;
-	spec->size = strlen(p);
+	spec->size = size;
 	spec->should_munmap = 0;
 	spec->is_stdin = 1;
 	spec->driver = &section_headers;
@@ -450,11 +477,11 @@ static struct diff_filespec *get_filespec(const char *name, const char *p)
 	return spec;
 }
 
-static void patch_diff(const char *a, const char *b,
+static void patch_diff(const char *a, size_t size_a, const char *b, size_t size_b,
 		       struct diff_options *diffopt)
 {
 	diff_queue(&diff_queued_diff,
-		   get_filespec("a", a), get_filespec("b", b));
+		   get_filespec("a", a, size_a), get_filespec("b", b, size_b));
 
 	diffcore_std(diffopt);
 	diff_flush(diffopt);
@@ -467,6 +494,14 @@ static void output(struct string_list *a, struct string_list *b,
 	int patch_no_width = decimal_width(1 + (a->nr > b->nr ? a->nr : b->nr));
 	int i = 0, j = 0;
 
+	regex_t regex;
+	if (regcomp(&regex, "^ ", REG_EXTENDED | REG_NEWLINE))
+		BUG("invalid regex");
+	ALLOC_GROW(diffopt->ignore_regex, diffopt->ignore_regex_nr + 1,
+		   diffopt->ignore_regex_alloc);
+	diffopt->ignore_regex[diffopt->ignore_regex_nr] = &regex;
+	size_t ignoring_context_only_changes = diffopt->ignore_regex_nr + 1;
+
 	/*
 	 * We assume the user is really more interested in the second argument
 	 * ("newer" version). To that end, we print the output in the order of
@@ -504,9 +539,14 @@ static void output(struct string_list *a, struct string_list *b,
 			a_util = a->items[b_util->matching].util;
 			output_pair_header(diffopt, patch_no_width,
 					   &buf, &dashes, a_util, b_util);
-			if (!(diffopt->output_format & DIFF_FORMAT_NO_OUTPUT))
-				patch_diff(a->items[b_util->matching].string,
-					   b->items[j].string, diffopt);
+			if (!(diffopt->output_format & DIFF_FORMAT_NO_OUTPUT)) {
+				patch_diff(a_util->patch, a_util->diff_offset, 
+						b_util->patch, b_util->diff_offset, diffopt);
+			        diffopt->ignore_regex_nr = ignoring_context_only_changes;
+				patch_diff(a_util->diff, strlen(a_util->diff), 
+						b_util->diff, strlen(b_util->diff), diffopt);
+			        diffopt->ignore_regex_nr = ignoring_context_only_changes - 1;
+			}
 			a_util->shown = 1;
 			j++;
 		}
diff --git a/t/t3206-range-diff.sh b/t/t3206-range-diff.sh
index f875843b5e..9a63388bee 100755
--- a/t/t3206-range-diff.sh
+++ b/t/t3206-range-diff.sh
@@ -223,16 +223,7 @@ test_expect_success 'changed commit' '
 	      12
 	      13
 	      14
-	4:  $(test_oid t4) ! 4:  $(test_oid c4) s/12/B/
-	    @@ file
-	     @@ file: A
-	      9
-	      10
-	    - B
-	    + BB
-	     -12
-	     +B
-	      13
+	4:  $(test_oid t4) = 4:  $(test_oid c4) s/12/B/
 	EOF
 	test_cmp expect actual
 '
@@ -243,7 +234,7 @@ test_expect_success 'changed commit with --no-patch diff option' '
 	1:  $(test_oid t1) = 1:  $(test_oid c1) s/5/A/
 	2:  $(test_oid t2) = 2:  $(test_oid c2) s/4/A/
 	3:  $(test_oid t3) ! 3:  $(test_oid c3) s/11/B/
-	4:  $(test_oid t4) ! 4:  $(test_oid c4) s/12/B/
+	4:  $(test_oid t4) = 4:  $(test_oid c4) s/12/B/
 	EOF
 	test_cmp expect actual
 '
@@ -256,9 +247,7 @@ test_expect_success 'changed commit with --stat diff option' '
 	3:  $(test_oid t3) ! 3:  $(test_oid c3) s/11/B/
 	     a => b | 2 +-
 	     1 file changed, 1 insertion(+), 1 deletion(-)
-	4:  $(test_oid t4) ! 4:  $(test_oid c4) s/12/B/
-	     a => b | 2 +-
-	     1 file changed, 1 insertion(+), 1 deletion(-)
+	4:  $(test_oid t4) = 4:  $(test_oid c4) s/12/B/
 	EOF
 	test_cmp expect actual
 '
@@ -278,16 +267,7 @@ test_expect_success 'changed commit with sm config' '
 	      12
 	      13
 	      14
-	4:  $(test_oid t4) ! 4:  $(test_oid c4) s/12/B/
-	    @@ file
-	     @@ file: A
-	      9
-	      10
-	    - B
-	    + BB
-	     -12
-	     +B
-	      13
+	4:  $(test_oid t4) = 4:  $(test_oid c4) s/12/B/
 	EOF
 	test_cmp expect actual
 '
@@ -304,16 +284,14 @@ test_expect_success 'renamed file' '
 	    -    s/4/A/
 	    +    s/4/A/ + rename file
 	    Z
+	    @@
 	    -## file ##
 	    +## file => renamed-file ##
 	    Z@@
 	    Z 1
 	    Z 2
 	3:  $(test_oid t3) ! 3:  $(test_oid n3) s/11/B/
-	    @@ Metadata
-	    Z ## Commit message ##
-	    Z    s/11/B/
-	    Z
+	    @@
 	    -## file ##
 	    -@@ file: A
 	    +## renamed-file ##
@@ -322,10 +300,7 @@ test_expect_success 'renamed file' '
 	    Z 9
 	    Z 10
 	4:  $(test_oid t4) ! 4:  $(test_oid n4) s/12/B/
-	    @@ Metadata
-	    Z ## Commit message ##
-	    Z    s/12/B/
-	    Z
+	    @@
 	    -## file ##
 	    -@@ file: A
 	    +## renamed-file ##
@@ -348,8 +323,6 @@ test_expect_success 'file with mode only change' '
 	    -    s/4/A/
 	    +    s/4/A/ + add other-file
 	    Z
-	    Z## file ##
-	    Z@@
 	    @@ file
 	    Z A
 	    Z 6
@@ -364,8 +337,6 @@ test_expect_success 'file with mode only change' '
 	    -    s/11/B/
 	    +    s/11/B/ + mode change other-file
 	    Z
-	    Z## file ##
-	    Z@@ file: A
 	    @@ file: A
 	    Z 12
 	    Z 13
@@ -389,8 +360,6 @@ test_expect_success 'file added and later removed' '
 	    -    s/4/A/
 	    +    s/4/A/ + new-file
 	    Z
-	    Z## file ##
-	    Z@@
 	    @@ file
 	    Z A
 	    Z 6
@@ -405,8 +374,6 @@ test_expect_success 'file added and later removed' '
 	    -    s/11/B/
 	    +    s/11/B/ + remove file
 	    Z
-	    Z## file ##
-	    Z@@ file: A
 	    @@ file: A
 	    Z 12
 	    Z 13
@@ -434,9 +401,6 @@ test_expect_success 'changed message' '
 	    Z
 	    +    Also a silly comment here!
 	    +
-	    Z## file ##
-	    Z@@
-	    Z 1
 	3:  $(test_oid t3) = 3:  $(test_oid m3) s/11/B/
 	4:  $(test_oid t4) = 4:  $(test_oid m4) s/12/B/
 	EOF
@@ -453,9 +417,6 @@ test_expect_success 'dual-coloring' '
 	:     <RESET>
 	:    <REVERSE><GREEN>+<RESET><BOLD>    Also a silly comment here!<RESET>
 	:    <REVERSE><GREEN>+<RESET>
-	:     ## file ##<RESET>
-	:    <CYAN> @@<RESET>
-	:      1<RESET>
 	:<RED>3:  $(test_oid c3) <RESET><YELLOW>!<RESET><GREEN> 3:  $(test_oid m3)<RESET><YELLOW> s/11/B/<RESET>
 	:    <REVERSE><CYAN>@@<RESET> <RESET>file: A<RESET>
 	:      9<RESET>
@@ -466,16 +427,7 @@ test_expect_success 'dual-coloring' '
 	:      12<RESET>
 	:      13<RESET>
 	:      14<RESET>
-	:<RED>4:  $(test_oid c4) <RESET><YELLOW>!<RESET><GREEN> 4:  $(test_oid m4)<RESET><YELLOW> s/12/B/<RESET>
-	:    <REVERSE><CYAN>@@<RESET> <RESET>file<RESET>
-	:    <CYAN> @@ file: A<RESET>
-	:      9<RESET>
-	:      10<RESET>
-	:    <REVERSE><RED>-<RESET><FAINT> BB<RESET>
-	:    <REVERSE><GREEN>+<RESET><BOLD> B<RESET>
-	:    <RED> -12<RESET>
-	:    <GREEN> +B<RESET>
-	:      13<RESET>
+	:<YELLOW>4:  d966c5c = 4:  8add5f1 s/12/B/<RESET>
 	EOF
 	git range-diff changed...changed-message --color --dual-color >actual.raw &&
 	test_decode_color >actual <actual.raw &&
@@ -537,8 +489,6 @@ test_expect_success 'range-diff compares notes by default' '
 	    -    topic note
 	    +    unmodified note
 	    Z
-	    Z## file ##
-	    Z@@ file: A
 	EOF
 	test_cmp expect actual
 '
@@ -584,8 +534,6 @@ test_expect_success 'range-diff with multiple --notes' '
 	    -    topic note2
 	    +    unmodified note2
 	    Z
-	    Z## file ##
-	    Z@@ file: A
 	EOF
 	test_cmp expect actual
 '
@@ -644,11 +592,8 @@ test_expect_success 'format-patch --range-diff with --notes' '
 	    Z ## Notes ##
 	    -    topic note
 	    +    unmodified note
-	    Z
-	    Z## file ##
-	    Z@@ file: A
 	EOF
-	sed "/@@ Commit message/,/@@ file: A/!d" 0000-* >actual &&
+	sed "/@@ Commit message/,/unmodified note\$/!d" 0000-* >actual &&
 	test_cmp expect actual
 '
 
@@ -673,11 +618,8 @@ test_expect_success 'format-patch --range-diff with format.notes config' '
 	    Z ## Notes ##
 	    -    topic note
 	    +    unmodified note
-	    Z
-	    Z## file ##
-	    Z@@ file: A
 	EOF
-	sed "/@@ Commit message/,/@@ file: A/!d" 0000-* >actual &&
+	sed "/@@ Commit message/,/unmodified note\$/!d" 0000-* >actual &&
 	test_cmp expect actual
 '
 
@@ -709,11 +651,8 @@ test_expect_success 'format-patch --range-diff with multiple notes' '
 	    Z ## Notes (note2) ##
 	    -    topic note2
 	    +    unmodified note2
-	    Z
-	    Z## file ##
-	    Z@@ file: A
 	EOF
-	sed "/@@ Commit message/,/@@ file: A/!d" 0000-* >actual &&
+	sed "/@@ Commit message/,/unmodified note2\$/!d" 0000-* >actual &&
 	test_cmp expect actual
 '
 
-- 
2.29.2


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

* [PATCH 3/3] range-diff: only compute patch diff when patches are different
  2020-11-17 21:35   ` Johannes Altmanninger
  2020-11-17 21:35     ` [PATCH 1/3] range-diff: move " ## filename ##" headers to the first column Johannes Altmanninger
  2020-11-17 21:35     ` [PATCH 2/3] range-diff: ignore context-only changes Johannes Altmanninger
@ 2020-11-17 21:35     ` Johannes Altmanninger
  2 siblings, 0 replies; 7+ messages in thread
From: Johannes Altmanninger @ 2020-11-17 21:35 UTC (permalink / raw)
  To: gitster; +Cc: git, newren, peff, Johannes Altmanninger

This is a pure optimization, probably with negligible impact. I'm not sure
if it is a good idea because it could obscure future bugs.
---
 range-diff.c | 8 +++++---
 1 file changed, 5 insertions(+), 3 deletions(-)

diff --git a/range-diff.c b/range-diff.c
index df2147ef79..343a71d3eb 100644
--- a/range-diff.c
+++ b/range-diff.c
@@ -388,7 +388,7 @@ static int are_diffs_equivalent(const char *a_diff, const char *b_diff) {
 	return 1;
 }
 
-static void output_pair_header(struct diff_options *diffopt,
+static char output_pair_header(struct diff_options *diffopt,
 			       int patch_no_width,
 			       struct strbuf *buf,
 			       struct strbuf *dashes,
@@ -456,6 +456,8 @@ static void output_pair_header(struct diff_options *diffopt,
 	strbuf_addf(buf, "%s\n", color_reset);
 
 	fwrite(buf->buf, buf->len, 1, diffopt->file);
+
+	return status;
 }
 
 static struct userdiff_driver section_headers = {
@@ -537,9 +539,9 @@ static void output(struct string_list *a, struct string_list *b,
 		/* Show matching LHS/RHS pair. */
 		if (j < b->nr) {
 			a_util = a->items[b_util->matching].util;
-			output_pair_header(diffopt, patch_no_width,
+			char status = output_pair_header(diffopt, patch_no_width,
 					   &buf, &dashes, a_util, b_util);
-			if (!(diffopt->output_format & DIFF_FORMAT_NO_OUTPUT)) {
+			if (!(diffopt->output_format & DIFF_FORMAT_NO_OUTPUT) && status != '=') {
 				patch_diff(a_util->patch, a_util->diff_offset, 
 						b_util->patch, b_util->diff_offset, diffopt);
 			        diffopt->ignore_regex_nr = ignoring_context_only_changes;
-- 
2.29.2


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

* Re: [PATCH 2/3] range-diff: ignore context-only changes
  2020-11-17 21:35     ` [PATCH 2/3] range-diff: ignore context-only changes Johannes Altmanninger
@ 2020-11-17 22:56       ` Eric Sunshine
  0 siblings, 0 replies; 7+ messages in thread
From: Eric Sunshine @ 2020-11-17 22:56 UTC (permalink / raw)
  To: Johannes Altmanninger; +Cc: Junio C Hamano, Git List, Elijah Newren, Jeff King

On Tue, Nov 17, 2020 at 4:38 PM Johannes Altmanninger <aclopte@gmail.com> wrote:
> range-diff compares matching commits by comparing their patches against
> each other. When two patches only differ in their context lines, that
> difference would still show up in range-diff's output.
>
> This commit uses diff's new -I/--ignore-matching-lines regex logic to ignore
> diff hunks that only consist of changes to context lines in the input diffs.
>
> Thanks to the previous commit, lines like "## file => renamed-file ##"
> are not considered context lines because they no longer have a leading space.
>
> This gives some extra @@ lines because we now always calculate
> two diffs: one for the patch metadata, like the commit message,
> and another one for the actual file changes.
> This is because the former contains lines with leading spaces that are not
> context lines, so we never want to ignore them.
> ---

Signed-off-by: is missing from all of your patches.

Just a few lightweight style-related review comments below (I didn't
read the patch any deeper than that)...

> diff --git a/range-diff.c b/range-diff.c
> @@ -363,6 +363,31 @@ static void get_correspondences(struct string_list *a, struct string_list *b,
> +static int are_diffs_equivalent(const char *a_diff, const char *b_diff) {
> +       for (
> +               const char
> +                       *a_eol = strchr(a_diff, '\n'),
> +                       *b_eol = strchr(b_diff, '\n');
> +               (a_eol = strchr(a_diff, '\n')) &&
> +               (b_eol = strchr(b_diff, '\n'));
> +               a_diff = a_eol + 1, b_diff = b_eol + 1
> +       ) {

This project doesn't yet declare variable as part of 'for', so:

    const char *a_eol = ...;
    const char *b_eol = ...;
    for ( ; (a_eol = ...) & (b_eol = ...); a_diff = ..., b_diff = ...) {

> +               if (!!a_eol != !!b_eol)
> +                       return 0;
> +
> +               // Ignore context lines.
> +               if (a_diff[0] == ' ' && b_diff[0] == ' ')
> +                       continue;

Avoid //-style comments. Use /* comments */ instead.

> +               size_t a_len = a_eol - a_diff;
> +               size_t b_len = b_eol - b_diff;

This project doesn't yet allow mixing declarations and code. Instead
place the declarations at the top of the scope:

    for (...) {
        size_t a_len;
        size_t b_len;
        ...
        a_len = ...;
        b_len = ...;

> @@ -390,8 +415,10 @@ static void output_pair_header(struct diff_options *diffopt,
> -       } else if (strcmp(a_util->patch, b_util->patch)) {
> -               color = color_commit;
> +       } else if (a_util->diff_offset != b_util->diff_offset
> +                  || strncmp(a_util->patch, b_util->patch, a_util->diff_offset)
> +                  || !are_diffs_equivalent(a_util->diff, b_util->diff)) {
> +               color = color_commit;

Style on this project is to break line after || operator rather than before:

    if (... ||
        ... ||
        ...) {

> @@ -467,6 +494,14 @@ static void output(struct string_list *a, struct string_list *b,
> +       regex_t regex;
> +       if (regcomp(&regex, "^ ", REG_EXTENDED | REG_NEWLINE))
> +               BUG("invalid regex");
> +       ALLOC_GROW(diffopt->ignore_regex, diffopt->ignore_regex_nr + 1,
> +                  diffopt->ignore_regex_alloc);
> +       diffopt->ignore_regex[diffopt->ignore_regex_nr] = &regex;
> +       size_t ignoring_context_only_changes = diffopt->ignore_regex_nr + 1;

Should you be calling regfree(&regex) at the end of the function?

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

end of thread, other threads:[~2020-11-17 23:00 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-11-05 13:34 range-diff should suppress context-only changes? Jeff King
2020-11-05 20:55 ` Junio C Hamano
2020-11-17 21:35   ` Johannes Altmanninger
2020-11-17 21:35     ` [PATCH 1/3] range-diff: move " ## filename ##" headers to the first column Johannes Altmanninger
2020-11-17 21:35     ` [PATCH 2/3] range-diff: ignore context-only changes Johannes Altmanninger
2020-11-17 22:56       ` Eric Sunshine
2020-11-17 21:35     ` [PATCH 3/3] range-diff: only compute patch diff when patches are different Johannes Altmanninger

Code repositories for project(s) associated with this public inbox

	https://80x24.org/mirrors/git.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).