Skip to contentSkip to conversation

range-diff should suppress context-only changes?

PATCH In review Jeff King 4 participants 7 messages Nov 5, 2020 – Nov 17, 2020

lore
Share & tools
lore

Jeff King

author
#0 lore raw
Quoted 22 lines · Elijah Newren via GitGitGadget

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)

Quoted 3 lines

+
+/*
@@ 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

#1 lore raw
Quoted 35 lines · Jeff King
Quoted 22 lines · Elijah Newren via GitGitGadget

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)

Quoted 3 lines

+
+/*
@@ 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).

Quoted 2 lines

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.

#2 lore raw
Quoted 7 lines
Quoted 2 lines

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

#3 lore raw

PATCH 1/3 range-diff: move " ## filename ##" headers to the first column

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(-)
range-diff.c +2 -2
index 24dc435e48..72660453bd 100644
@@ -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 }
 };
 
t/t3206-range-diff.sh +21 -21

152 lines not on this page · raw message

Signature · 1 line
2.29.2
#4 lore raw

PATCH 2/3 range-diff: ignore context-only changes

In reply to 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(-)
range-diff.c +49 -9
index 72660453bd..df2147ef79 100644
@@ -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++;
 		}
t/t3206-range-diff.sh +11 -72

200 lines not on this page · raw message

Signature · 1 line
2.29.2
#5 lore raw

Re: [PATCH 2/3] range-diff: ignore context-only changes

Quoted 10 lines · 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.

Quoted 6 lines

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

range-diff.c static void get_correspondences(struct string_list *a, struct string_list *b, · line 371

Quoted 11 lines
range-diff.c +9 -0
@@ -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 = ...) {

range-diff.c static void get_correspondences(struct string_list *a, struct string_list *b, · line 380

Quoted 6 lines
range-diff.c +6 -0
+               if (!!a_eol != !!b_eol)
+                       return 0;
+
+               // Ignore context lines.
+               if (a_diff[0] == ' ' && b_diff[0] == ' ')
+                       continue;

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

range-diff.c static void get_correspondences(struct string_list *a, struct string_list *b, · line 383

Quoted 2 lines
range-diff.c +2 -0
+               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 = ...;

range-diff.c static void output_pair_header(struct diff_options *diffopt, · line 418

Quoted 7 lines
range-diff.c +4 -2
@@ -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 (... ||
        ... ||
        ...) {

range-diff.c static void output(struct string_list *a, struct string_list *b, · line 500

Quoted 8 lines
range-diff.c +7 -0
@@ -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?

#6 lore raw

PATCH 3/3 range-diff: only compute patch diff when patches are different

In reply to 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(-)
range-diff.c +5 -3
index df2147ef79..343a71d3eb 100644
@@ -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;
Signature · 1 line
2.29.2