threads / patch / 51436

v2, 14 partsapply: replace marc.info link with public-inbox

Subject: [PATCH v2 01/14] apply: replace marc.info link with public-inbox

## tl;dr

61 messages between Jul 5, 2019 and Jul 12, 2019. Diffs are folded; open one to read it.

replies: 60people: 4as markdown or json

Thomas Gummerer· Jul 5, 2019, 17:06 UTC · lore

[PATCH v2 00/14] output improvements for git range-diff

It's been quite a while since I sent the RFC [1] (thanks all for the comments on that), and the series changed shapes quite a bit since the last round.

Since it's been such a long time, just to remind everyone, the goal of this series is to make the range-diff output clearer, by showing information about the filenames to which the current diff belongs.

In the previous round, we did this using "section headers" that include information about the current file and adding that to the outer diff's hunk headers.

In this round we still keep the section headers (with slightly more information), but in addition we also add the filename to the inner diff hunk headers. In the outer diff hunk headers we then display either the section header or the inner diff hunk header using a userdiff pattern.

In terms of code changes the biggest change is that we're now re-using the 'parse_git_header' function from the apply code to parse the diff headers, instead of trying to parse them with some hacky parsing code in range-diff.c. This way we are sure that the diff headers are properly parsed.

I had also considered just outputting the section headers directly from 'git log', but then decided against that. Parsing the headers allows a future enhancement of range-diff, where we would allow parsing an mbox file [2].

I split the "only pass required data" commits up, in the hopes of making them easier to review, but I'm also happy to squash them if people feel like that makes it easier to review them.

An added advantage of this is that we're also getting rid of things like the similarity index, which are not important in the range-diff, and are thus not represented in the "section header".

One thing that did not change is that the new/deleted strings are not translated in this version either. This is similar to the regular diff output, where we also don't translate these. We can still consider translating them in the future though.

[1]: https://public-inbox.org/git/20190414210933.20875-1-t.gummerer@gmail.com/ [2]: https://github.com/gitgitgadget/git/issues/207

I'm including the range-diff between this version of the series and the RFC, and a diff between the range diff and the range-diff without these changes below. Probably not useful in reviewing the code, but good to show off the changes made in this series.

range-diff:
 -:  ---------- >  1:  ef2245edda apply: replace marc.info link with public-inbox
 -:  ---------- >  2:  94578fa45c apply: only pass required data to skip_tree_prefix
 -:  ---------- >  3:  988269a68e apply: only pass required data to git_header_name
 -:  ---------- >  4:  a2c1ef3f5f apply: only pass required data to check_header_line
 -:  ---------- >  5:  0f4cfe21cb apply: only pass required data to find_name_*
 -:  ---------- >  6:  7f1d7a4569 apply: only pass required data to gitdiff_* functions
 -:  ---------- >  7:  a5af8b0845 apply: make parse_git_header public
 1:  0e678d222c =  8:  1f25bb1002 range-diff: fix function parameter indentation
 -:  ---------- >  9:  01ed0f2a6a range-diff: split lines manually
 2:  48716230fc ! 10:  044a79868b range-diff: don't remove funcname from inner diff
    @@ range-diff.c: static int read_patches(const char *range, struct string_list *lis
      				strbuf_addch(&buf, '\n');
      			}
      			continue;
    --		} else if (starts_with(line.buf, "@@ "))
    +-		} else if (starts_with(line, "@@ "))
     -			strbuf_addstr(&buf, "@@");
    --		else if (!line.buf[0] || starts_with(line.buf, "index "))
    -+		} else if (starts_with(line.buf, "@@ ")) {
    -+			char *skip_lineno = strstr(line.buf + 3, "@@");
    -+			strbuf_remove(&line, 0, skip_lineno - line.buf);
    -+			strbuf_addch(&buf, ' ');
    -+			strbuf_addbuf(&buf, &line);
    -+		} else if (!line.buf[0] || starts_with(line.buf, "index "))
    +-		else if (!line[0] || starts_with(line, "index "))
    ++		} else if (skip_prefix(line, "@@ ", &p)) {
    ++			if (!(p = strstr(p, "@@")))
    ++				die(_("invalid hunk header in inner diff"));
    ++			strbuf_addstr(&buf, p);
    ++		} else if (!line[0] || starts_with(line, "index "))
      			/*
      			 * A completely blank (not ' \n', which is context)
      			 * line is not valid in a diff.  We skip it
    +
    + ## t/t3206-range-diff.sh ##
    +@@ t/t3206-range-diff.sh: test_expect_success 'changed commit' '
    + 	      14
    + 	4:  a63e992 ! 4:  d966c5c s/12/B/
    + 	    @@ -8,7 +8,7 @@
    +-	     @@
    ++	     @@ A
    + 	      9
    + 	      10
    + 	    - B
    +@@ t/t3206-range-diff.sh: test_expect_success 'changed commit with sm config' '
    + 	      14
    + 	4:  a63e992 ! 4:  d966c5c s/12/B/
    + 	    @@ -8,7 +8,7 @@
    +-	     @@
    ++	     @@ A
    + 	      9
    + 	      10
    + 	    - B
    +@@ t/t3206-range-diff.sh: test_expect_success 'dual-coloring' '
    + 	:      14<RESET>
    + 	:<RED>4:  d966c5c <RESET><YELLOW>!<RESET><GREEN> 4:  8add5f1<RESET><YELLOW> s/12/B/<RESET>
    + 	:    <REVERSE><CYAN>@@ -8,7 +8,7 @@<RESET>
    +-	:    <CYAN> @@<RESET>
    ++	:    <CYAN> @@ A<RESET>
    + 	:      9<RESET>
    + 	:      10<RESET>
    + 	:    <REVERSE><RED>-<RESET><FAINT> BB<RESET>
 3:  71679cb747 <  -:  ---------- range-diff: add section header instead of diff header
 -:  ---------- > 11:  69654fe76d range-diff: suppress line count in outer diff
 -:  ---------- > 12:  c38f929b9a range-diff: add section header instead of diff header
 -:  ---------- > 13:  6df03ecdcf range-diff: add filename to inner diff
 4:  8f45b6a995 ! 14:  5ceef49035 range-diff: add section headers to the outer hunk header
    @@ Metadata
     Author: Thomas Gummerer <t.gummerer@gmail.com>
     
      ## Commit message ##
    -    range-diff: add section headers to the outer hunk header
    +    range-diff: add headers to the outer hunk header
     
    -    Add the section headers we introduced in the previous commit to the
    -    outer diff's hunk headers.  This makes it easier to understand which
    -    change we are actually looking at.  For example an outer hunk header
    -    might now look like:
    +    Add the section headers/hunk headers we introduced in the previous
    +    commits to the outer diff's hunk headers.  This makes it easier to
    +    understand which change we are actually looking at.  For example an
    +    outer hunk header might now look like:
     
    -        @@ -77,15 +78,43 @@ modified file Documentation/config/interactive.txt
    +        @@  Documentation/config/interactive.txt
     
         while previously it would have only been
     
    -        @@ -77,15 +78,43 @@
    +        @@
     
         which doesn't give a lot of context for the change that follows.
     
    @@ Commit message
     
      ## range-diff.c ##
     @@ range-diff.c: static int read_patches(const char *range, struct string_list *list)
    - 			strbuf_addstr(&buf, " ##\n");
    + 			strbuf_addstr(&buf, " ##");
      		} else if (in_header) {
    - 			if (starts_with(line.buf, "Author: ")) {
    + 			if (starts_with(line, "Author: ")) {
     +				strbuf_addstr(&buf, " ## Metadata ##\n");
    - 				strbuf_addbuf(&buf, &line);
    + 				strbuf_addstr(&buf, line);
      				strbuf_addstr(&buf, "\n\n");
     +				strbuf_addstr(&buf, " ## Commit message ##\n");
    - 			} else if (starts_with(line.buf, "    ")) {
    - 				strbuf_rtrim(&line);
    - 				strbuf_addbuf(&buf, &line);
    + 			} else if (starts_with(line, "    ")) {
    + 				p = line + len - 2;
    + 				while (isspace(*p) && p >= line)
     @@ range-diff.c: static void output_pair_header(struct diff_options *diffopt,
      	fwrite(buf->buf, buf->len, 1, diffopt->file);
      }
    @@ range-diff.c: static void output_pair_header(struct diff_options *diffopt,
     -static struct userdiff_driver no_func_name = {
     -	.funcname = { "$^", 0 }
     +static struct userdiff_driver section_headers = {
    -+	.funcname = { "^ ## (.*) ##$", REG_EXTENDED }
    ++	.funcname = { "^ ## (.*) ##$\n"
    ++		      "^.?@@ (.*)$", REG_EXTENDED }
      };
      
      static struct diff_filespec *get_filespec(const char *name, const char *p)
    @@ range-diff.c: static struct diff_filespec *get_filespec(const char *name, const
      
      	return spec;
      }
    +
    + ## t/t3206-range-diff.sh ##
    +@@ t/t3206-range-diff.sh: test_expect_success 'changed commit' '
    + 	1:  4de457d = 1:  a4b3333 s/5/A/
    + 	2:  fccce22 = 2:  f51d370 s/4/A/
    + 	3:  147e64e ! 3:  0559556 s/11/B/
    +-	    @@
    ++	    @@ file: A
    + 	      9
    + 	      10
    + 	     -11
    +@@ t/t3206-range-diff.sh: test_expect_success 'changed commit' '
    + 	      13
    + 	      14
    + 	4:  a63e992 ! 4:  d966c5c s/12/B/
    +-	    @@
    ++	    @@ file
    + 	     @@ file: A
    + 	      9
    + 	      10
    +@@ t/t3206-range-diff.sh: test_expect_success 'changed commit with sm config' '
    + 	1:  4de457d = 1:  a4b3333 s/5/A/
    + 	2:  fccce22 = 2:  f51d370 s/4/A/
    + 	3:  147e64e ! 3:  0559556 s/11/B/
    +-	    @@
    ++	    @@ file: A
    + 	      9
    + 	      10
    + 	     -11
    +@@ t/t3206-range-diff.sh: test_expect_success 'changed commit with sm config' '
    + 	      13
    + 	      14
    + 	4:  a63e992 ! 4:  d966c5c s/12/B/
    +-	    @@
    ++	    @@ file
    + 	     @@ file: A
    + 	      9
    + 	      10
    +@@ t/t3206-range-diff.sh: test_expect_success 'renamed file' '
    + 	sed s/Z/\ /g >expected <<-EOF &&
    + 	1:  4de457d = 1:  f258d75 s/5/A/
    + 	2:  fccce22 ! 2:  017b62d s/4/A/
    +-	    @@
    ++	    @@ Metadata
    + 	    ZAuthor: Thomas Rast <trast@inf.ethz.ch>
    + 	    Z
    ++	    Z ## Commit message ##
    + 	    -    s/4/A/
    + 	    +    s/4/A/ + rename file
    + 	    Z
    +@@ t/t3206-range-diff.sh: test_expect_success 'renamed file' '
    + 	    Z 1
    + 	    Z 2
    + 	3:  147e64e ! 3:  3ce7af6 s/11/B/
    +-	    @@
    +-	    Z
    ++	    @@ Metadata
    ++	    Z ## Commit message ##
    + 	    Z    s/11/B/
    + 	    Z
    + 	    - ## file ##
    +@@ t/t3206-range-diff.sh: test_expect_success 'renamed file' '
    + 	    Z 9
    + 	    Z 10
    + 	4:  a63e992 ! 4:  1e6226b s/12/B/
    +-	    @@
    +-	    Z
    ++	    @@ Metadata
    ++	    Z ## Commit message ##
    + 	    Z    s/12/B/
    + 	    Z
    + 	    - ## file ##
    +@@ t/t3206-range-diff.sh: test_expect_success 'file added and later removed' '
    + 	sed s/Z/\ /g >expected <<-EOF &&
    + 	1:  4de457d = 1:  096b1ba s/5/A/
    + 	2:  fccce22 ! 2:  d92e698 s/4/A/
    +-	    @@
    ++	    @@ Metadata
    + 	    ZAuthor: Thomas Rast <trast@inf.ethz.ch>
    + 	    Z
    ++	    Z ## Commit message ##
    + 	    -    s/4/A/
    + 	    +    s/4/A/ + new-file
    + 	    Z
    + 	    Z ## file ##
    + 	    Z@@
    +-	    @@
    ++	    @@ file
    + 	    Z A
    + 	    Z 6
    + 	    Z 7
    + 	    +
    + 	    + ## new-file (new) ##
    + 	3:  147e64e ! 3:  9a1db4d s/11/B/
    +-	    @@
    ++	    @@ Metadata
    + 	    ZAuthor: Thomas Rast <trast@inf.ethz.ch>
    + 	    Z
    ++	    Z ## Commit message ##
    + 	    -    s/11/B/
    + 	    +    s/11/B/ + remove file
    + 	    Z
    + 	    Z ## file ##
    + 	    Z@@ file: A
    +-	    @@
    ++	    @@ file: A
    + 	    Z 12
    + 	    Z 13
    + 	    Z 14
    +@@ t/t3206-range-diff.sh: test_expect_success 'changed message' '
    + 	sed s/Z/\ /g >expected <<-EOF &&
    + 	1:  4de457d = 1:  f686024 s/5/A/
    + 	2:  fccce22 ! 2:  4ab067d s/4/A/
    +-	    @@
    +-	    Z
    ++	    @@ Metadata
    ++	    Z ## Commit message ##
    + 	    Z    s/4/A/
    + 	    Z
    + 	    +    Also a silly comment here!
    +@@ t/t3206-range-diff.sh: test_expect_success 'dual-coloring' '
    + 	sed -e "s|^:||" >expect <<-\EOF &&
    + 	:<YELLOW>1:  a4b3333 = 1:  f686024 s/5/A/<RESET>
    + 	:<RED>2:  f51d370 <RESET><YELLOW>!<RESET><GREEN> 2:  4ab067d<RESET><YELLOW> s/4/A/<RESET>
    +-	:    <REVERSE><CYAN>@@<RESET>
    +-	:     <RESET>
    ++	:    <REVERSE><CYAN>@@<RESET> <RESET>Metadata<RESET>
    ++	:      ## Commit message ##<RESET>
    + 	:         s/4/A/<RESET>
    + 	:     <RESET>
    + 	:    <REVERSE><GREEN>+<RESET><BOLD>    Also a silly comment here!<RESET>
    +@@ t/t3206-range-diff.sh: test_expect_success 'dual-coloring' '
    + 	:    <CYAN> @@<RESET>
    + 	:      1<RESET>
    + 	:<RED>3:  0559556 <RESET><YELLOW>!<RESET><GREEN> 3:  b9cb956<RESET><YELLOW> s/11/B/<RESET>
    +-	:    <REVERSE><CYAN>@@<RESET>
    ++	:    <REVERSE><CYAN>@@<RESET> <RESET>file: A<RESET>
    + 	:      9<RESET>
    + 	:      10<RESET>
    + 	:    <RED> -11<RESET>
    +@@ t/t3206-range-diff.sh: test_expect_success 'dual-coloring' '
    + 	:      13<RESET>
    + 	:      14<RESET>
    + 	:<RED>4:  d966c5c <RESET><YELLOW>!<RESET><GREEN> 4:  8add5f1<RESET><YELLOW> s/12/B/<RESET>
    +-	:    <REVERSE><CYAN>@@<RESET>
    ++	:    <REVERSE><CYAN>@@<RESET> <RESET>file<RESET>
    + 	:    <CYAN> @@ file: A<RESET>
    + 	:      9<RESET>
    + 	:      10<RESET>

diff between range-diffs (a diff between range-diffs seems a bit insane, though with some coloring I find it actually somewhat readable):

Show changes to 2 files +27 −30

rd1, rd2

diff --git a/rd1 b/rd2
index 5fa847fbab..d6f99d1548 100644
--- a/rd1
+++ b/rd2
@@ -8,7 +8,7 @@
  1:  0e678d222c =  8:  1f25bb1002 range-diff: fix function parameter indentation
  -:  ---------- >  9:  01ed0f2a6a range-diff: split lines manually
  2:  48716230fc ! 10:  044a79868b range-diff: don't remove funcname from inner diff
-    @@ -27,15 +27,45 @@
+    @@ range-diff.c: static int read_patches(const char *range, struct string_list *lis
       				strbuf_addch(&buf, '\n');
       			}
       			continue;
@@ -32,10 +32,8 @@
       			 * A completely blank (not ' \n', which is context)
       			 * line is not valid in a diff.  We skip it
     +
-    + diff --git a/t/t3206-range-diff.sh b/t/t3206-range-diff.sh
-    + --- a/t/t3206-range-diff.sh
-    + +++ b/t/t3206-range-diff.sh
-    +@@
+    + ## t/t3206-range-diff.sh ##
+    +@@ t/t3206-range-diff.sh: test_expect_success 'changed commit' '
     + 	      14
     + 	4:  a63e992 ! 4:  d966c5c s/12/B/
     + 	    @@ -8,7 +8,7 @@
@@ -44,7 +42,7 @@
     + 	      9
     + 	      10
     + 	    - B
-    +@@
+    +@@ t/t3206-range-diff.sh: test_expect_success 'changed commit with sm config' '
     + 	      14
     + 	4:  a63e992 ! 4:  d966c5c s/12/B/
     + 	    @@ -8,7 +8,7 @@
@@ -53,7 +51,7 @@
     + 	      9
     + 	      10
     + 	    - B
-    +@@
+    +@@ t/t3206-range-diff.sh: test_expect_success 'dual-coloring' '
     + 	:      14<RESET>
     + 	:<RED>4:  d966c5c <RESET><YELLOW>!<RESET><GREEN> 4:  8add5f1<RESET><YELLOW> s/12/B/<RESET>
     + 	:    <REVERSE><CYAN>@@ -8,7 +8,7 @@<RESET>
@@ -67,9 +65,10 @@
  -:  ---------- > 12:  c38f929b9a range-diff: add section header instead of diff header
  -:  ---------- > 13:  6df03ecdcf range-diff: add filename to inner diff
  4:  8f45b6a995 ! 14:  5ceef49035 range-diff: add section headers to the outer hunk header
-    @@ -1,17 +1,17 @@
+    @@ Metadata
      Author: Thomas Gummerer <t.gummerer@gmail.com>
      
+      ## Commit message ##
     -    range-diff: add section headers to the outer hunk header
     +    range-diff: add headers to the outer hunk header
      
@@ -92,10 +91,10 @@
      
          which doesn't give a lot of context for the change that follows.
      
-    @@ -24,16 +24,16 @@
-      --- a/range-diff.c
-      +++ b/range-diff.c
-     @@
+    @@ Commit message
+     
+      ## range-diff.c ##
+     @@ range-diff.c: static int read_patches(const char *range, struct string_list *list)
     - 			strbuf_addstr(&buf, " ##\n");
     + 			strbuf_addstr(&buf, " ##");
       		} else if (in_header) {
@@ -112,10 +111,10 @@
     + 			} else if (starts_with(line, "    ")) {
     + 				p = line + len - 2;
     + 				while (isspace(*p) && p >= line)
-     @@
+     @@ range-diff.c: static void output_pair_header(struct diff_options *diffopt,
       	fwrite(buf->buf, buf->len, 1, diffopt->file);
       }
-    @@ -41,7 +41,8 @@
+    @@ range-diff.c: static void output_pair_header(struct diff_options *diffopt,
      -static struct userdiff_driver no_func_name = {
      -	.funcname = { "$^", 0 }
      +static struct userdiff_driver section_headers = {
@@ -125,15 +124,13 @@
       };
       
       static struct diff_filespec *get_filespec(const char *name, const char *p)
-    @@ -54,3 +55,154 @@ $^
+    @@ range-diff.c: static struct diff_filespec *get_filespec(const char *name, const
       
       	return spec;
       }
     +
-    + diff --git a/t/t3206-range-diff.sh b/t/t3206-range-diff.sh
-    + --- a/t/t3206-range-diff.sh
-    + +++ b/t/t3206-range-diff.sh
-    +@@
+    + ## t/t3206-range-diff.sh ##
+    +@@ t/t3206-range-diff.sh: test_expect_success 'changed commit' '
     + 	1:  4de457d = 1:  a4b3333 s/5/A/
     + 	2:  fccce22 = 2:  f51d370 s/4/A/
     + 	3:  147e64e ! 3:  0559556 s/11/B/
@@ -142,7 +139,7 @@
     + 	      9
     + 	      10
     + 	     -11
-    +@@
+    +@@ t/t3206-range-diff.sh: test_expect_success 'changed commit' '
     + 	      13
     + 	      14
     + 	4:  a63e992 ! 4:  d966c5c s/12/B/
@@ -151,7 +148,7 @@
     + 	     @@ file: A
     + 	      9
     + 	      10
-    +@@
+    +@@ t/t3206-range-diff.sh: test_expect_success 'changed commit with sm config' '
     + 	1:  4de457d = 1:  a4b3333 s/5/A/
     + 	2:  fccce22 = 2:  f51d370 s/4/A/
     + 	3:  147e64e ! 3:  0559556 s/11/B/
@@ -160,7 +157,7 @@
     + 	      9
     + 	      10
     + 	     -11
-    +@@
+    +@@ t/t3206-range-diff.sh: test_expect_success 'changed commit with sm config' '
     + 	      13
     + 	      14
     + 	4:  a63e992 ! 4:  d966c5c s/12/B/
@@ -169,7 +166,7 @@
     + 	     @@ file: A
     + 	      9
     + 	      10
-    +@@
+    +@@ t/t3206-range-diff.sh: test_expect_success 'renamed file' '
     + 	sed s/Z/\ /g >expected <<-EOF &&
     + 	1:  4de457d = 1:  f258d75 s/5/A/
     + 	2:  fccce22 ! 2:  017b62d s/4/A/
@@ -181,7 +178,7 @@
     + 	    -    s/4/A/
     + 	    +    s/4/A/ + rename file
     + 	    Z
-    +@@
+    +@@ t/t3206-range-diff.sh: test_expect_success 'renamed file' '
     + 	    Z 1
     + 	    Z 2
     + 	3:  147e64e ! 3:  3ce7af6 s/11/B/
@@ -192,7 +189,7 @@
     + 	    Z    s/11/B/
     + 	    Z
     + 	    - ## file ##
-    +@@
+    +@@ t/t3206-range-diff.sh: test_expect_success 'renamed file' '
     + 	    Z 9
     + 	    Z 10
     + 	4:  a63e992 ! 4:  1e6226b s/12/B/
@@ -203,7 +200,7 @@
     + 	    Z    s/12/B/
     + 	    Z
     + 	    - ## file ##
-    +@@
+    +@@ t/t3206-range-diff.sh: test_expect_success 'file added and later removed' '
     + 	sed s/Z/\ /g >expected <<-EOF &&
     + 	1:  4de457d = 1:  096b1ba s/5/A/
     + 	2:  fccce22 ! 2:  d92e698 s/4/A/
@@ -240,7 +237,7 @@
     + 	    Z 12
     + 	    Z 13
     + 	    Z 14
-    +@@
+    +@@ t/t3206-range-diff.sh: test_expect_success 'changed message' '
     + 	sed s/Z/\ /g >expected <<-EOF &&
     + 	1:  4de457d = 1:  f686024 s/5/A/
     + 	2:  fccce22 ! 2:  4ab067d s/4/A/
@@ -251,7 +248,7 @@
     + 	    Z    s/4/A/
     + 	    Z
     + 	    +    Also a silly comment here!
-    +@@
+    +@@ t/t3206-range-diff.sh: test_expect_success 'dual-coloring' '
     + 	sed -e "s|^:||" >expect <<-\EOF &&
     + 	:<YELLOW>1:  a4b3333 = 1:  f686024 s/5/A/<RESET>
     + 	:<RED>2:  f51d370 <RESET><YELLOW>!<RESET><GREEN> 2:  4ab067d<RESET><YELLOW> s/4/A/<RESET>
@@ -262,7 +259,7 @@
     + 	:         s/4/A/<RESET>
     + 	:     <RESET>
     + 	:    <REVERSE><GREEN>+<RESET><BOLD>    Also a silly comment here!<RESET>
-    +@@
+    +@@ t/t3206-range-diff.sh: test_expect_success 'dual-coloring' '
     + 	:    <CYAN> @@<RESET>
     + 	:      1<RESET>
     + 	:<RED>3:  0559556 <RESET><YELLOW>!<RESET><GREEN> 3:  b9cb956<RESET><YELLOW> s/11/B/<RESET>
@@ -271,7 +268,7 @@
     + 	:      9<RESET>
     + 	:      10<RESET>
     + 	:    <RED> -11<RESET>
-    +@@
+    +@@ t/t3206-range-diff.sh: test_expect_success 'dual-coloring' '
     + 	:      13<RESET>
     + 	:      14<RESET>
     + 	:<RED>4:  d966c5c <RESET><YELLOW>!<RESET><GREEN> 4:  8add5f1<RESET><YELLOW> s/12/B/<RESET>

Thomas Gummerer (14):
  apply: replace marc.info link with public-inbox
  apply: only pass required data to skip_tree_prefix
  apply: only pass required data to git_header_name
  apply: only pass required data to check_header_line
  apply: only pass required data to find_name_*
  apply: only pass required data to gitdiff_* functions
  apply: make parse_git_header public
  range-diff: fix function parameter indentation
  range-diff: split lines manually
  range-diff: don't remove funcname from inner diff
  range-diff: suppress line count in outer diff
  range-diff: add section header instead of diff header
  range-diff: add filename to inner diff
  range-diff: add headers to the outer hunk header

 apply.c                | 185 ++++++++++++++++++-----------------------
 apply.h                |  48 +++++++++++
 diff.c                 |   5 +-
 diff.h                 |   1 +
 range-diff.c           | 126 +++++++++++++++++++---------
 t/t3206-range-diff.sh  | 124 ++++++++++++++++++++++-----
 t/t3206/history.export |  84 ++++++++++++++++++-
 7 files changed, 407 insertions(+), 166 deletions(-)
-- 
2.22.0.510.g264f2c817a
Thomas Gummerer· Jul 5, 2019, 17:06 UTC · re: Thomas Gummerer · lore

public-inbox.org links include the whole message ID by default. This means the message can still be found even if the site goes away, which is not the case with the marc.info link. Replace the marc.info link with a more future proof one.

Signed-off-by: Thomas Gummerer <t.gummerer@gmail.com>
---
 apply.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
Show changes to apply.c +1 −1
diff --git a/apply.c b/apply.c
index f15afa9f6a..599cf8956f 100644
--- a/apply.c
+++ b/apply.c
@@ -478,7 +478,7 @@ static char *find_name_gnu(struct apply_state *state,
 
 	/*
 	 * Proposed "new-style" GNU patch/diff format; see
-	 * http://marc.info/?l=git&m=112927316408690&w=2
+	 * https://public-inbox.org/git/7vll0wvb2a.fsf@assigned-by-dhcp.cox.net/
 	 */
 	if (unquote_c_style(&name, line, NULL)) {
 		strbuf_release(&name);
-- 
2.22.0.510.g264f2c817a
Thomas Gummerer· Jul 5, 2019, 17:06 UTC · re: Thomas Gummerer · lore

[PATCH v2 03/14] apply: only pass required data to git_header_name

Currently the 'git_header_name()' function takes 'struct apply_state' as parameter, even though it only needs the p_value from that struct.

This function is in the callchain of 'parse_git_header()', which we want to make more generally useful in a subsequent commit. To make that happen we only want to pass in the required data to 'parse_git_header()', and not the whole 'struct apply_state', and thus we want functions in the callchain of 'parse_git_header()' to only take arguments they really need.

Signed-off-by: Thomas Gummerer <t.gummerer@gmail.com>
---
 apply.c | 16 ++++++++--------
 1 file changed, 8 insertions(+), 8 deletions(-)
Show changes to apply.c +8 −8
diff --git a/apply.c b/apply.c
index fc7083fcbc..ac668e754d 100644
--- a/apply.c
+++ b/apply.c
@@ -1164,7 +1164,7 @@ static const char *skip_tree_prefix(int p_value,
  * creation or deletion of an empty file.  In any of these cases,
  * both sides are the same name under a/ and b/ respectively.
  */
-static char *git_header_name(struct apply_state *state,
+static char *git_header_name(int p_value,
 			     const char *line,
 			     int llen)
 {
@@ -1184,7 +1184,7 @@ static char *git_header_name(struct apply_state *state,
 			goto free_and_fail1;
 
 		/* strip the a/b prefix including trailing slash */
-		cp = skip_tree_prefix(state->p_value, first.buf, first.len);
+		cp = skip_tree_prefix(p_value, first.buf, first.len);
 		if (!cp)
 			goto free_and_fail1;
 		strbuf_remove(&first, 0, cp - first.buf);
@@ -1201,7 +1201,7 @@ static char *git_header_name(struct apply_state *state,
 		if (*second == '"') {
 			if (unquote_c_style(&sp, second, NULL))
 				goto free_and_fail1;
-			cp = skip_tree_prefix(state->p_value, sp.buf, sp.len);
+			cp = skip_tree_prefix(p_value, sp.buf, sp.len);
 			if (!cp)
 				goto free_and_fail1;
 			/* They must match, otherwise ignore */
@@ -1212,7 +1212,7 @@ static char *git_header_name(struct apply_state *state,
 		}
 
 		/* unquoted second */
-		cp = skip_tree_prefix(state->p_value, second, line + llen - second);
+		cp = skip_tree_prefix(p_value, second, line + llen - second);
 		if (!cp)
 			goto free_and_fail1;
 		if (line + llen - cp != first.len ||
@@ -1227,7 +1227,7 @@ static char *git_header_name(struct apply_state *state,
 	}
 
 	/* unquoted first name */
-	name = skip_tree_prefix(state->p_value, line, llen);
+	name = skip_tree_prefix(p_value, line, llen);
 	if (!name)
 		return NULL;
 
@@ -1243,7 +1243,7 @@ static char *git_header_name(struct apply_state *state,
 			if (unquote_c_style(&sp, second, NULL))
 				goto free_and_fail2;
 
-			np = skip_tree_prefix(state->p_value, sp.buf, sp.len);
+			np = skip_tree_prefix(p_value, sp.buf, sp.len);
 			if (!np)
 				goto free_and_fail2;
 
@@ -1287,7 +1287,7 @@ static char *git_header_name(struct apply_state *state,
 			 */
 			if (!name[len + 1])
 				return NULL; /* no postimage name */
-			second = skip_tree_prefix(state->p_value, name + len + 1,
+			second = skip_tree_prefix(p_value, name + len + 1,
 						  line_len - (len + 1));
 			if (!second)
 				return NULL;
@@ -1333,7 +1333,7 @@ static int parse_git_header(struct apply_state *state,
 	 * or removing or adding empty files), so we get
 	 * the default name from the header.
 	 */
-	patch->def_name = git_header_name(state, line, len);
+	patch->def_name = git_header_name(state->p_value, line, len);
 	if (patch->def_name && state->root.len) {
 		char *s = xstrfmt("%s%s", state->root.buf, patch->def_name);
 		free(patch->def_name);
-- 
2.22.0.510.g264f2c817a
Thomas Gummerer· Jul 5, 2019, 17:06 UTC · re: Thomas Gummerer · lore

[PATCH v2 02/14] apply: only pass required data to skip_tree_prefix

Currently the 'skip_tree_prefix()' function takes 'struct apply_state' as parameter, even though it only needs the p_value from that struct.

This function is in the callchain of 'parse_git_header()', which we want to make more generally useful in a subsequent commit. To make that happen we only want to pass in the required data to 'parse_git_header()', and not the whole 'struct apply_state', and thus we want functions in the callchain of 'parse_git_header()' to only take arguments they really need.

Signed-off-by: Thomas Gummerer <t.gummerer@gmail.com>
---
 apply.c | 18 +++++++++---------
 1 file changed, 9 insertions(+), 9 deletions(-)
Show changes to apply.c +9 −9
diff --git a/apply.c b/apply.c
index 599cf8956f..fc7083fcbc 100644
--- a/apply.c
+++ b/apply.c
@@ -1137,17 +1137,17 @@ static int gitdiff_unrecognized(struct apply_state *state,
  * Skip p_value leading components from "line"; as we do not accept
  * absolute paths, return NULL in that case.
  */
-static const char *skip_tree_prefix(struct apply_state *state,
+static const char *skip_tree_prefix(int p_value,
 				    const char *line,
 				    int llen)
 {
 	int nslash;
 	int i;
 
-	if (!state->p_value)
+	if (!p_value)
 		return (llen && line[0] == '/') ? NULL : line;
 
-	nslash = state->p_value;
+	nslash = p_value;
 	for (i = 0; i < llen; i++) {
 		int ch = line[i];
 		if (ch == '/' && --nslash <= 0)
@@ -1184,7 +1184,7 @@ static char *git_header_name(struct apply_state *state,
 			goto free_and_fail1;
 
 		/* strip the a/b prefix including trailing slash */
-		cp = skip_tree_prefix(state, first.buf, first.len);
+		cp = skip_tree_prefix(state->p_value, first.buf, first.len);
 		if (!cp)
 			goto free_and_fail1;
 		strbuf_remove(&first, 0, cp - first.buf);
@@ -1201,7 +1201,7 @@ static char *git_header_name(struct apply_state *state,
 		if (*second == '"') {
 			if (unquote_c_style(&sp, second, NULL))
 				goto free_and_fail1;
-			cp = skip_tree_prefix(state, sp.buf, sp.len);
+			cp = skip_tree_prefix(state->p_value, sp.buf, sp.len);
 			if (!cp)
 				goto free_and_fail1;
 			/* They must match, otherwise ignore */
@@ -1212,7 +1212,7 @@ static char *git_header_name(struct apply_state *state,
 		}
 
 		/* unquoted second */
-		cp = skip_tree_prefix(state, second, line + llen - second);
+		cp = skip_tree_prefix(state->p_value, second, line + llen - second);
 		if (!cp)
 			goto free_and_fail1;
 		if (line + llen - cp != first.len ||
@@ -1227,7 +1227,7 @@ static char *git_header_name(struct apply_state *state,
 	}
 
 	/* unquoted first name */
-	name = skip_tree_prefix(state, line, llen);
+	name = skip_tree_prefix(state->p_value, line, llen);
 	if (!name)
 		return NULL;
 
@@ -1243,7 +1243,7 @@ static char *git_header_name(struct apply_state *state,
 			if (unquote_c_style(&sp, second, NULL))
 				goto free_and_fail2;
 
-			np = skip_tree_prefix(state, sp.buf, sp.len);
+			np = skip_tree_prefix(state->p_value, sp.buf, sp.len);
 			if (!np)
 				goto free_and_fail2;
 
@@ -1287,7 +1287,7 @@ static char *git_header_name(struct apply_state *state,
 			 */
 			if (!name[len + 1])
 				return NULL; /* no postimage name */
-			second = skip_tree_prefix(state, name + len + 1,
+			second = skip_tree_prefix(state->p_value, name + len + 1,
 						  line_len - (len + 1));
 			if (!second)
 				return NULL;
-- 
2.22.0.510.g264f2c817a
Thomas Gummerer· Jul 5, 2019, 17:06 UTC · re: Thomas Gummerer · lore

[PATCH v2 04/14] apply: only pass required data to check_header_line

Currently the 'check_header_line()' function takes 'struct apply_state' as parameter, even though it only needs the linenr from that struct.

This function is in the callchain of 'parse_git_header()', which we want to make more generally useful in a subsequent commit. To make that happen we only want to pass in the required data to 'parse_git_header()', and not the whole 'struct apply_state', and thus we want functions in the callchain of 'parse_git_header()' to only take arguments they really need.

Signed-off-by: Thomas Gummerer <t.gummerer@gmail.com>
---
 apply.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)
Show changes to apply.c +4 −4
diff --git a/apply.c b/apply.c
index ac668e754d..1602fd5db0 100644
--- a/apply.c
+++ b/apply.c
@@ -1302,15 +1302,15 @@ static char *git_header_name(int p_value,
 	}
 }
 
-static int check_header_line(struct apply_state *state, struct patch *patch)
+static int check_header_line(int linenr, struct patch *patch)
 {
 	int extensions = (patch->is_delete == 1) + (patch->is_new == 1) +
 			 (patch->is_rename == 1) + (patch->is_copy == 1);
 	if (extensions > 1)
 		return error(_("inconsistent header lines %d and %d"),
-			     patch->extension_linenr, state->linenr);
+			     patch->extension_linenr, linenr);
 	if (extensions && !patch->extension_linenr)
-		patch->extension_linenr = state->linenr;
+		patch->extension_linenr = linenr;
 	return 0;
 }
 
@@ -1380,7 +1380,7 @@ static int parse_git_header(struct apply_state *state,
 			res = p->fn(state, line + oplen, patch);
 			if (res < 0)
 				return -1;
-			if (check_header_line(state, patch))
+			if (check_header_line(state->linenr, patch))
 				return -1;
 			if (res > 0)
 				return offset;
-- 
2.22.0.510.g264f2c817a
Thomas Gummerer· Jul 5, 2019, 17:06 UTC · re: Thomas Gummerer · lore

[PATCH v2 05/14] apply: only pass required data to find_name_*

Currently the 'find_name_*()' functions take 'struct apply_state' as parameter, even though they only need the 'root' member from that struct.

These functions are in the callchain of 'parse_git_header()', which we want to make more generally useful in a subsequent commit. To make that happen we only want to pass in the required data to 'parse_git_header()', and not the whole 'struct apply_state', and thus we want functions in the callchain of 'parse_git_header()' to only take arguments they really need.

Signed-off-by: Thomas Gummerer <t.gummerer@gmail.com>
---
 apply.c | 48 ++++++++++++++++++++++++------------------------
 1 file changed, 24 insertions(+), 24 deletions(-)
Show changes to apply.c +24 −24
diff --git a/apply.c b/apply.c
index 1602fd5db0..3cd4e3d3b3 100644
--- a/apply.c
+++ b/apply.c
@@ -469,7 +469,7 @@ static char *squash_slash(char *name)
 	return name;
 }
 
-static char *find_name_gnu(struct apply_state *state,
+static char *find_name_gnu(struct strbuf *root,
 			   const char *line,
 			   int p_value)
 {
@@ -495,8 +495,8 @@ static char *find_name_gnu(struct apply_state *state,
 	}
 
 	strbuf_remove(&name, 0, cp - name.buf);
-	if (state->root.len)
-		strbuf_insert(&name, 0, state->root.buf, state->root.len);
+	if (root->len)
+		strbuf_insert(&name, 0, root->buf, root->len);
 	return squash_slash(strbuf_detach(&name, NULL));
 }
 
@@ -659,7 +659,7 @@ static size_t diff_timestamp_len(const char *line, size_t len)
 	return line + len - end;
 }
 
-static char *find_name_common(struct apply_state *state,
+static char *find_name_common(struct strbuf *root,
 			      const char *line,
 			      const char *def,
 			      int p_value,
@@ -702,30 +702,30 @@ static char *find_name_common(struct apply_state *state,
 			return squash_slash(xstrdup(def));
 	}
 
-	if (state->root.len) {
-		char *ret = xstrfmt("%s%.*s", state->root.buf, len, start);
+	if (root->len) {
+		char *ret = xstrfmt("%s%.*s", root->buf, len, start);
 		return squash_slash(ret);
 	}
 
 	return squash_slash(xmemdupz(start, len));
 }
 
-static char *find_name(struct apply_state *state,
+static char *find_name(struct strbuf *root,
 		       const char *line,
 		       char *def,
 		       int p_value,
 		       int terminate)
 {
 	if (*line == '"') {
-		char *name = find_name_gnu(state, line, p_value);
+		char *name = find_name_gnu(root, line, p_value);
 		if (name)
 			return name;
 	}
 
-	return find_name_common(state, line, def, p_value, NULL, terminate);
+	return find_name_common(root, line, def, p_value, NULL, terminate);
 }
 
-static char *find_name_traditional(struct apply_state *state,
+static char *find_name_traditional(struct strbuf *root,
 				   const char *line,
 				   char *def,
 				   int p_value)
@@ -734,7 +734,7 @@ static char *find_name_traditional(struct apply_state *state,
 	size_t date_len;
 
 	if (*line == '"') {
-		char *name = find_name_gnu(state, line, p_value);
+		char *name = find_name_gnu(root, line, p_value);
 		if (name)
 			return name;
 	}
@@ -742,10 +742,10 @@ static char *find_name_traditional(struct apply_state *state,
 	len = strchrnul(line, '\n') - line;
 	date_len = diff_timestamp_len(line, len);
 	if (!date_len)
-		return find_name_common(state, line, def, p_value, NULL, TERM_TAB);
+		return find_name_common(root, line, def, p_value, NULL, TERM_TAB);
 	len -= date_len;
 
-	return find_name_common(state, line, def, p_value, line + len, 0);
+	return find_name_common(root, line, def, p_value, line + len, 0);
 }
 
 /*
@@ -759,7 +759,7 @@ static int guess_p_value(struct apply_state *state, const char *nameline)
 
 	if (is_dev_null(nameline))
 		return -1;
-	name = find_name_traditional(state, nameline, NULL, 0);
+	name = find_name_traditional(&state->root, nameline, NULL, 0);
 	if (!name)
 		return -1;
 	cp = strchr(name, '/');
@@ -883,17 +883,17 @@ static int parse_traditional_patch(struct apply_state *state,
 	if (is_dev_null(first)) {
 		patch->is_new = 1;
 		patch->is_delete = 0;
-		name = find_name_traditional(state, second, NULL, state->p_value);
+		name = find_name_traditional(&state->root, second, NULL, state->p_value);
 		patch->new_name = name;
 	} else if (is_dev_null(second)) {
 		patch->is_new = 0;
 		patch->is_delete = 1;
-		name = find_name_traditional(state, first, NULL, state->p_value);
+		name = find_name_traditional(&state->root, first, NULL, state->p_value);
 		patch->old_name = name;
 	} else {
 		char *first_name;
-		first_name = find_name_traditional(state, first, NULL, state->p_value);
-		name = find_name_traditional(state, second, first_name, state->p_value);
+		first_name = find_name_traditional(&state->root, first, NULL, state->p_value);
+		name = find_name_traditional(&state->root, second, first_name, state->p_value);
 		free(first_name);
 		if (has_epoch_timestamp(first)) {
 			patch->is_new = 1;
@@ -940,7 +940,7 @@ static int gitdiff_verify_name(struct apply_state *state,
 			       int side)
 {
 	if (!*name && !isnull) {
-		*name = find_name(state, line, NULL, state->p_value, TERM_TAB);
+		*name = find_name(&state->root, line, NULL, state->p_value, TERM_TAB);
 		return 0;
 	}
 
@@ -949,7 +949,7 @@ static int gitdiff_verify_name(struct apply_state *state,
 		if (isnull)
 			return error(_("git apply: bad git-diff - expected /dev/null, got %s on line %d"),
 				     *name, state->linenr);
-		another = find_name(state, line, NULL, state->p_value, TERM_TAB);
+		another = find_name(&state->root, line, NULL, state->p_value, TERM_TAB);
 		if (!another || strcmp(another, *name)) {
 			free(another);
 			return error((side == DIFF_NEW_NAME) ?
@@ -1032,7 +1032,7 @@ static int gitdiff_copysrc(struct apply_state *state,
 {
 	patch->is_copy = 1;
 	free(patch->old_name);
-	patch->old_name = find_name(state, line, NULL, state->p_value ? state->p_value - 1 : 0, 0);
+	patch->old_name = find_name(&state->root, line, NULL, state->p_value ? state->p_value - 1 : 0, 0);
 	return 0;
 }
 
@@ -1042,7 +1042,7 @@ static int gitdiff_copydst(struct apply_state *state,
 {
 	patch->is_copy = 1;
 	free(patch->new_name);
-	patch->new_name = find_name(state, line, NULL, state->p_value ? state->p_value - 1 : 0, 0);
+	patch->new_name = find_name(&state->root, line, NULL, state->p_value ? state->p_value - 1 : 0, 0);
 	return 0;
 }
 
@@ -1052,7 +1052,7 @@ static int gitdiff_renamesrc(struct apply_state *state,
 {
 	patch->is_rename = 1;
 	free(patch->old_name);
-	patch->old_name = find_name(state, line, NULL, state->p_value ? state->p_value - 1 : 0, 0);
+	patch->old_name = find_name(&state->root, line, NULL, state->p_value ? state->p_value - 1 : 0, 0);
 	return 0;
 }
 
@@ -1062,7 +1062,7 @@ static int gitdiff_renamedst(struct apply_state *state,
 {
 	patch->is_rename = 1;
 	free(patch->new_name);
-	patch->new_name = find_name(state, line, NULL, state->p_value ? state->p_value - 1 : 0, 0);
+	patch->new_name = find_name(&state->root, line, NULL, state->p_value ? state->p_value - 1 : 0, 0);
 	return 0;
 }
 
-- 
2.22.0.510.g264f2c817a
Thomas Gummerer· Jul 5, 2019, 17:06 UTC · re: Thomas Gummerer · lore

[PATCH v2 07/14] apply: make parse_git_header public

Make parse_git_header a "public" function in apply.h, so we can re-use it in range-diff in a subsequent commit.

Signed-off-by: Thomas Gummerer <t.gummerer@gmail.com>
---

I considered creating a separate struct for only the metadata here, and embedding that in 'struct patch'. As struct patch is mostly metadata fields though, I decided against that to avoid more code churn here.

 apply.c | 68 ++++++++++++++++-----------------------------------------
 apply.h | 48 ++++++++++++++++++++++++++++++++++++++++
 2 files changed, 67 insertions(+), 49 deletions(-)
Show changes to 2 files +67 −49

apply.c, apply.h

diff --git a/apply.c b/apply.c
index 468f1d3fee..04319c233f 100644
--- a/apply.c
+++ b/apply.c
@@ -207,40 +207,6 @@ struct fragment {
 #define BINARY_DELTA_DEFLATED	1
 #define BINARY_LITERAL_DEFLATED 2
 
-/*
- * This represents a "patch" to a file, both metainfo changes
- * such as creation/deletion, filemode and content changes represented
- * as a series of fragments.
- */
-struct patch {
-	char *new_name, *old_name, *def_name;
-	unsigned int old_mode, new_mode;
-	int is_new, is_delete;	/* -1 = unknown, 0 = false, 1 = true */
-	int rejected;
-	unsigned ws_rule;
-	int lines_added, lines_deleted;
-	int score;
-	int extension_linenr; /* first line specifying delete/new/rename/copy */
-	unsigned int is_toplevel_relative:1;
-	unsigned int inaccurate_eof:1;
-	unsigned int is_binary:1;
-	unsigned int is_copy:1;
-	unsigned int is_rename:1;
-	unsigned int recount:1;
-	unsigned int conflicted_threeway:1;
-	unsigned int direct_to_threeway:1;
-	unsigned int crlf_in_old:1;
-	struct fragment *fragments;
-	char *result;
-	size_t resultsize;
-	char old_oid_prefix[GIT_MAX_HEXSZ + 1];
-	char new_oid_prefix[GIT_MAX_HEXSZ + 1];
-	struct patch *next;
-
-	/* three-way fallback result */
-	struct object_id threeway_stage[3];
-};
-
 static void free_fragment_list(struct fragment *list)
 {
 	while (list) {
@@ -1321,11 +1287,13 @@ static int check_header_line(int linenr, struct patch *patch)
 }
 
 /* Verify that we recognize the lines following a git header */
-static int parse_git_header(struct apply_state *state,
-			    const char *line,
-			    int len,
-			    unsigned int size,
-			    struct patch *patch)
+int parse_git_header(struct strbuf *root,
+		     int *linenr,
+		     int p_value,
+		     const char *line,
+		     int len,
+		     unsigned int size,
+		     struct patch *patch)
 {
 	unsigned long offset;
 	struct parse_git_header_state parse_hdr_state;
@@ -1340,21 +1308,21 @@ static int parse_git_header(struct apply_state *state,
 	 * or removing or adding empty files), so we get
 	 * the default name from the header.
 	 */
-	patch->def_name = git_header_name(state->p_value, line, len);
-	if (patch->def_name && state->root.len) {
-		char *s = xstrfmt("%s%s", state->root.buf, patch->def_name);
+	patch->def_name = git_header_name(p_value, line, len);
+	if (patch->def_name && root->len) {
+		char *s = xstrfmt("%s%s", root->buf, patch->def_name);
 		free(patch->def_name);
 		patch->def_name = s;
 	}
 
 	line += len;
 	size -= len;
-	state->linenr++;
-	parse_hdr_state.root = &state->root;
-	parse_hdr_state.linenr = state->linenr;
-	parse_hdr_state.p_value = state->p_value;
+	(*linenr)++;
+	parse_hdr_state.root = root;
+	parse_hdr_state.linenr = *linenr;
+	parse_hdr_state.p_value = p_value;
 
-	for (offset = len ; size > 0 ; offset += len, size -= len, line += len, state->linenr++) {
+	for (offset = len ; size > 0 ; offset += len, size -= len, line += len, (*linenr)++) {
 		static const struct opentry {
 			const char *str;
 			int (*fn)(struct parse_git_header_state *, const char *, struct patch *);
@@ -1391,7 +1359,7 @@ static int parse_git_header(struct apply_state *state,
 			res = p->fn(&parse_hdr_state, line + oplen, patch);
 			if (res < 0)
 				return -1;
-			if (check_header_line(state->linenr, patch))
+			if (check_header_line(*linenr, patch))
 				return -1;
 			if (res > 0)
 				return offset;
@@ -1572,7 +1540,9 @@ static int find_header(struct apply_state *state,
 		 * or mode change, so we handle that specially
 		 */
 		if (!memcmp("diff --git ", line, 11)) {
-			int git_hdr_len = parse_git_header(state, line, len, size, patch);
+			int git_hdr_len = parse_git_header(&state->root, &state->linenr,
+							   state->p_value, line, len,
+							   size, patch);
 			if (git_hdr_len < 0)
 				return -128;
 			if (git_hdr_len <= len)
diff --git a/apply.h b/apply.h
index 5948348133..e7a1586b17 100644
--- a/apply.h
+++ b/apply.h
@@ -117,6 +117,40 @@ struct apply_state {
 	int applied_after_fixing_ws;
 };
 
+/*
+ * This represents a "patch" to a file, both metainfo changes
+ * such as creation/deletion, filemode and content changes represented
+ * as a series of fragments.
+ */
+struct patch {
+	char *new_name, *old_name, *def_name;
+	unsigned int old_mode, new_mode;
+	int is_new, is_delete;	/* -1 = unknown, 0 = false, 1 = true */
+	int rejected;
+	unsigned ws_rule;
+	int lines_added, lines_deleted;
+	int score;
+	int extension_linenr; /* first line specifying delete/new/rename/copy */
+	unsigned int is_toplevel_relative:1;
+	unsigned int inaccurate_eof:1;
+	unsigned int is_binary:1;
+	unsigned int is_copy:1;
+	unsigned int is_rename:1;
+	unsigned int recount:1;
+	unsigned int conflicted_threeway:1;
+	unsigned int direct_to_threeway:1;
+	unsigned int crlf_in_old:1;
+	struct fragment *fragments;
+	char *result;
+	size_t resultsize;
+	char old_oid_prefix[GIT_MAX_HEXSZ + 1];
+	char new_oid_prefix[GIT_MAX_HEXSZ + 1];
+	struct patch *next;
+
+	/* three-way fallback result */
+	struct object_id threeway_stage[3];
+};
+
 int apply_parse_options(int argc, const char **argv,
 			struct apply_state *state,
 			int *force_apply, int *options,
@@ -127,6 +161,20 @@ int init_apply_state(struct apply_state *state,
 void clear_apply_state(struct apply_state *state);
 int check_apply_state(struct apply_state *state, int force_apply);
 
+/*
+ * Parse a get header, starting at line.  Fills the relevant metadata
+ * information in 'struct patch'.
+ *
+ * Returns -1 on failure, the length of the parsed header otherwise.
+ */
+int parse_git_header(struct strbuf *root,
+		     int *linenr,
+		     int p_value,
+		     const char *line,
+		     int len,
+		     unsigned int size,
+		     struct patch *patch);
+
 /*
  * Some aspects of the apply behavior are controlled by the following
  * bits in the "options" parameter passed to apply_all_patches().
-- 
2.22.0.510.g264f2c817a
Johannes Schindelin· Jul 5, 2019, 18:48 UTC · re: Thomas Gummerer · lore

Re: [PATCH v2 07/14] apply: make parse_git_header public

Hi Thomas,
On Fri, 5 Jul 2019, Thomas Gummerer wrote:
> Make parse_git_header a "public" function in apply.h, so we can re-use
> it in range-diff in a subsequent commit.

This function probably wants a different name when it becomes public, as it was relatively obvious inside `apply.c` that it was talking about a Git _diff_ header. Maybe `parse_git_diff_header()`?

Show 6 quoted lines
> @@ -127,6 +161,20 @@ int init_apply_state(struct apply_state *state,
>  void clear_apply_state(struct apply_state *state);
>  int check_apply_state(struct apply_state *state, int force_apply);
>
> +/*
> + * Parse a get header, starting at line.  Fills the relevant metadata
s/get/git/

Ciao, Dscho

Show 19 quoted lines
> + * information in 'struct patch'.
> + *
> + * Returns -1 on failure, the length of the parsed header otherwise.
> + */
> +int parse_git_header(struct strbuf *root,
> +		     int *linenr,
> +		     int p_value,
> +		     const char *line,
> +		     int len,
> +		     unsigned int size,
> +		     struct patch *patch);
> +
>  /*
>   * Some aspects of the apply behavior are controlled by the following
>   * bits in the "options" parameter passed to apply_all_patches().
> --
> 2.22.0.510.g264f2c817a
>
>
Thomas Gummerer· Jul 5, 2019, 17:06 UTC · re: Thomas Gummerer · lore

[PATCH v2 06/14] apply: only pass required data to gitdiff_* functions

Currently the 'gitdiff_*()' functions take 'struct apply_state' as parameter, even though they only needs the root, linenr and p_value from that struct.

These functions are in the callchain of 'parse_git_header()', which we want to make more generally useful in a subsequent commit. To make that happen we only want to pass in the required data to 'parse_git_header()', and not the whole 'struct apply_state', and thus we want functions in the callchain of 'parse_git_header()' to only take arguments they really need.

Signed-off-by: Thomas Gummerer <t.gummerer@gmail.com>
---
 apply.c | 59 ++++++++++++++++++++++++++++++++++-----------------------
 1 file changed, 35 insertions(+), 24 deletions(-)
Show changes to apply.c +35 −24
diff --git a/apply.c b/apply.c
index 3cd4e3d3b3..468f1d3fee 100644
--- a/apply.c
+++ b/apply.c
@@ -22,6 +22,12 @@
 #include "rerere.h"
 #include "apply.h"
 
+struct parse_git_header_state {
+	struct strbuf *root;
+	int linenr;
+	int p_value;
+};
+
 static void git_apply_config(void)
 {
 	git_config_get_string_const("apply.whitespace", &apply_default_whitespace);
@@ -914,7 +920,7 @@ static int parse_traditional_patch(struct apply_state *state,
 	return 0;
 }
 
-static int gitdiff_hdrend(struct apply_state *state,
+static int gitdiff_hdrend(struct parse_git_header_state *state,
 			  const char *line,
 			  struct patch *patch)
 {
@@ -933,14 +939,14 @@ static int gitdiff_hdrend(struct apply_state *state,
 #define DIFF_OLD_NAME 0
 #define DIFF_NEW_NAME 1
 
-static int gitdiff_verify_name(struct apply_state *state,
+static int gitdiff_verify_name(struct parse_git_header_state *state,
 			       const char *line,
 			       int isnull,
 			       char **name,
 			       int side)
 {
 	if (!*name && !isnull) {
-		*name = find_name(&state->root, line, NULL, state->p_value, TERM_TAB);
+		*name = find_name(state->root, line, NULL, state->p_value, TERM_TAB);
 		return 0;
 	}
 
@@ -949,7 +955,7 @@ static int gitdiff_verify_name(struct apply_state *state,
 		if (isnull)
 			return error(_("git apply: bad git-diff - expected /dev/null, got %s on line %d"),
 				     *name, state->linenr);
-		another = find_name(&state->root, line, NULL, state->p_value, TERM_TAB);
+		another = find_name(state->root, line, NULL, state->p_value, TERM_TAB);
 		if (!another || strcmp(another, *name)) {
 			free(another);
 			return error((side == DIFF_NEW_NAME) ?
@@ -965,7 +971,7 @@ static int gitdiff_verify_name(struct apply_state *state,
 	return 0;
 }
 
-static int gitdiff_oldname(struct apply_state *state,
+static int gitdiff_oldname(struct parse_git_header_state *state,
 			   const char *line,
 			   struct patch *patch)
 {
@@ -974,7 +980,7 @@ static int gitdiff_oldname(struct apply_state *state,
 				   DIFF_OLD_NAME);
 }
 
-static int gitdiff_newname(struct apply_state *state,
+static int gitdiff_newname(struct parse_git_header_state *state,
 			   const char *line,
 			   struct patch *patch)
 {
@@ -992,21 +998,21 @@ static int parse_mode_line(const char *line, int linenr, unsigned int *mode)
 	return 0;
 }
 
-static int gitdiff_oldmode(struct apply_state *state,
+static int gitdiff_oldmode(struct parse_git_header_state *state,
 			   const char *line,
 			   struct patch *patch)
 {
 	return parse_mode_line(line, state->linenr, &patch->old_mode);
 }
 
-static int gitdiff_newmode(struct apply_state *state,
+static int gitdiff_newmode(struct parse_git_header_state *state,
 			   const char *line,
 			   struct patch *patch)
 {
 	return parse_mode_line(line, state->linenr, &patch->new_mode);
 }
 
-static int gitdiff_delete(struct apply_state *state,
+static int gitdiff_delete(struct parse_git_header_state *state,
 			  const char *line,
 			  struct patch *patch)
 {
@@ -1016,7 +1022,7 @@ static int gitdiff_delete(struct apply_state *state,
 	return gitdiff_oldmode(state, line, patch);
 }
 
-static int gitdiff_newfile(struct apply_state *state,
+static int gitdiff_newfile(struct parse_git_header_state *state,
 			   const char *line,
 			   struct patch *patch)
 {
@@ -1026,47 +1032,47 @@ static int gitdiff_newfile(struct apply_state *state,
 	return gitdiff_newmode(state, line, patch);
 }
 
-static int gitdiff_copysrc(struct apply_state *state,
+static int gitdiff_copysrc(struct parse_git_header_state *state,
 			   const char *line,
 			   struct patch *patch)
 {
 	patch->is_copy = 1;
 	free(patch->old_name);
-	patch->old_name = find_name(&state->root, line, NULL, state->p_value ? state->p_value - 1 : 0, 0);
+	patch->old_name = find_name(state->root, line, NULL, state->p_value ? state->p_value - 1 : 0, 0);
 	return 0;
 }
 
-static int gitdiff_copydst(struct apply_state *state,
+static int gitdiff_copydst(struct parse_git_header_state *state,
 			   const char *line,
 			   struct patch *patch)
 {
 	patch->is_copy = 1;
 	free(patch->new_name);
-	patch->new_name = find_name(&state->root, line, NULL, state->p_value ? state->p_value - 1 : 0, 0);
+	patch->new_name = find_name(state->root, line, NULL, state->p_value ? state->p_value - 1 : 0, 0);
 	return 0;
 }
 
-static int gitdiff_renamesrc(struct apply_state *state,
+static int gitdiff_renamesrc(struct parse_git_header_state *state,
 			     const char *line,
 			     struct patch *patch)
 {
 	patch->is_rename = 1;
 	free(patch->old_name);
-	patch->old_name = find_name(&state->root, line, NULL, state->p_value ? state->p_value - 1 : 0, 0);
+	patch->old_name = find_name(state->root, line, NULL, state->p_value ? state->p_value - 1 : 0, 0);
 	return 0;
 }
 
-static int gitdiff_renamedst(struct apply_state *state,
+static int gitdiff_renamedst(struct parse_git_header_state *state,
 			     const char *line,
 			     struct patch *patch)
 {
 	patch->is_rename = 1;
 	free(patch->new_name);
-	patch->new_name = find_name(&state->root, line, NULL, state->p_value ? state->p_value - 1 : 0, 0);
+	patch->new_name = find_name(state->root, line, NULL, state->p_value ? state->p_value - 1 : 0, 0);
 	return 0;
 }
 
-static int gitdiff_similarity(struct apply_state *state,
+static int gitdiff_similarity(struct parse_git_header_state *state,
 			      const char *line,
 			      struct patch *patch)
 {
@@ -1076,7 +1082,7 @@ static int gitdiff_similarity(struct apply_state *state,
 	return 0;
 }
 
-static int gitdiff_dissimilarity(struct apply_state *state,
+static int gitdiff_dissimilarity(struct parse_git_header_state *state,
 				 const char *line,
 				 struct patch *patch)
 {
@@ -1086,7 +1092,7 @@ static int gitdiff_dissimilarity(struct apply_state *state,
 	return 0;
 }
 
-static int gitdiff_index(struct apply_state *state,
+static int gitdiff_index(struct parse_git_header_state *state,
 			 const char *line,
 			 struct patch *patch)
 {
@@ -1126,7 +1132,7 @@ static int gitdiff_index(struct apply_state *state,
  * This is normal for a diff that doesn't change anything: we'll fall through
  * into the next diff. Tell the parser to break out.
  */
-static int gitdiff_unrecognized(struct apply_state *state,
+static int gitdiff_unrecognized(struct parse_git_header_state *state,
 				const char *line,
 				struct patch *patch)
 {
@@ -1322,6 +1328,7 @@ static int parse_git_header(struct apply_state *state,
 			    struct patch *patch)
 {
 	unsigned long offset;
+	struct parse_git_header_state parse_hdr_state;
 
 	/* A git diff has explicit new/delete information, so we don't guess */
 	patch->is_new = 0;
@@ -1343,10 +1350,14 @@ static int parse_git_header(struct apply_state *state,
 	line += len;
 	size -= len;
 	state->linenr++;
+	parse_hdr_state.root = &state->root;
+	parse_hdr_state.linenr = state->linenr;
+	parse_hdr_state.p_value = state->p_value;
+
 	for (offset = len ; size > 0 ; offset += len, size -= len, line += len, state->linenr++) {
 		static const struct opentry {
 			const char *str;
-			int (*fn)(struct apply_state *, const char *, struct patch *);
+			int (*fn)(struct parse_git_header_state *, const char *, struct patch *);
 		} optable[] = {
 			{ "@@ -", gitdiff_hdrend },
 			{ "--- ", gitdiff_oldname },
@@ -1377,7 +1388,7 @@ static int parse_git_header(struct apply_state *state,
 			int res;
 			if (len < oplen || memcmp(p->str, line, oplen))
 				continue;
-			res = p->fn(state, line + oplen, patch);
+			res = p->fn(&parse_hdr_state, line + oplen, patch);
 			if (res < 0)
 				return -1;
 			if (check_header_line(state->linenr, patch))
-- 
2.22.0.510.g264f2c817a
Johannes Schindelin· Jul 5, 2019, 18:51 UTC · re: Thomas Gummerer · lore

Re: [PATCH v2 06/14] apply: only pass required data to gitdiff_* functions

Hi Thomas,
On Fri, 5 Jul 2019, Thomas Gummerer wrote:
Show 10 quoted lines
> Currently the 'gitdiff_*()' functions take 'struct apply_state' as
> parameter, even though they only needs the root, linenr and p_value
> from that struct.
>
> These functions are in the callchain of 'parse_git_header()', which we
> want to make more generally useful in a subsequent commit.  To make
> that happen we only want to pass in the required data to
> 'parse_git_header()', and not the whole 'struct apply_state', and thus
> we want functions in the callchain of 'parse_git_header()' to only
> take arguments they really need.

This commit message follows the exact pattern of the previous patches (and they were pretty convincing to me), but...

Show 23 quoted lines
> diff --git a/apply.c b/apply.c
> index 3cd4e3d3b3..468f1d3fee 100644
> --- a/apply.c
> +++ b/apply.c
> @@ -22,6 +22,12 @@
>  #include "rerere.h"
>  #include "apply.h"
>
> +struct parse_git_header_state {
> +	struct strbuf *root;
> +	int linenr;
> +	int p_value;
> +};
> +
>  static void git_apply_config(void)
>  {
>  	git_config_get_string_const("apply.whitespace", &apply_default_whitespace);
> @@ -914,7 +920,7 @@ static int parse_traditional_patch(struct apply_state *state,
>  	return 0;
>  }
>
> -static int gitdiff_hdrend(struct apply_state *state,
> +static int gitdiff_hdrend(struct parse_git_header_state *state,

... here we pass a different, newly-invented struct instead of passing the relevant information explicitly.

My guess is that the code would look horribly verbose if we started passing three instead of one parameter? If that is the case, I think a little side note in the commit message might be warranted.

Ciao, Dscho

Thomas Gummerer· Jul 5, 2019, 17:06 UTC · re: Thomas Gummerer · lore

[PATCH v2 08/14] range-diff: fix function parameter indentation

Fix the indentation of the function parameters for a couple of functions, to match the style in the rest of the file.

Signed-off-by: Thomas Gummerer <t.gummerer@gmail.com>
---
 range-diff.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
Show changes to range-diff.c +2 −2
diff --git a/range-diff.c b/range-diff.c
index 48b0e1b4ce..9242b8975f 100644
--- a/range-diff.c
+++ b/range-diff.c
@@ -148,7 +148,7 @@ static int read_patches(const char *range, struct string_list *list)
 }
 
 static int patch_util_cmp(const void *dummy, const struct patch_util *a,
-		     const struct patch_util *b, const char *keydata)
+			  const struct patch_util *b, const char *keydata)
 {
 	return strcmp(a->diff, keydata ? keydata : b->diff);
 }
@@ -373,7 +373,7 @@ static struct diff_filespec *get_filespec(const char *name, const char *p)
 }
 
 static void patch_diff(const char *a, const char *b,
-			      struct diff_options *diffopt)
+		       struct diff_options *diffopt)
 {
 	diff_queue(&diff_queued_diff,
 		   get_filespec("a", a), get_filespec("b", b));
-- 
2.22.0.510.g264f2c817a
Thomas Gummerer· Jul 5, 2019, 17:06 UTC · re: Thomas Gummerer · lore

[PATCH v2 09/14] range-diff: split lines manually

Currently range-diff uses the 'strbuf_getline()' function for doing its line by line processing. In a future patch we want to do parts of that parsing using the 'parse_git_header()' function, which does requires reading parts of the input from that function, which doesn't use strbufs.

Switch range-diff to do our own line by line parsing, so we can re-use the parse_git_header function later.

Signed-off-by: Thomas Gummerer <t.gummerer@gmail.com>
---

Longer term it might be better to have both range-diff and apply code use strbufs. However I didn't feel it's worth making that change for this patch series.

 range-diff.c | 69 +++++++++++++++++++++++++++++-----------------------
 1 file changed, 39 insertions(+), 30 deletions(-)
Show changes to range-diff.c +39 −30
diff --git a/range-diff.c b/range-diff.c
index 9242b8975f..916afa44c0 100644
--- a/range-diff.c
+++ b/range-diff.c
@@ -24,6 +24,17 @@ struct patch_util {
 	struct object_id oid;
 };
 
+static unsigned long linelen(const char *buffer, unsigned long size)
+{
+	unsigned long len = 0;
+	while (size--) {
+		len++;
+		if (*buffer++ == '\n')
+			break;
+	}
+	return len;
+}
+
 /*
  * Reads the patches into a string list, with the `util` field being populated
  * as struct object_id (will need to be free()d).
@@ -31,10 +42,12 @@ struct patch_util {
 static int read_patches(const char *range, struct string_list *list)
 {
 	struct child_process cp = CHILD_PROCESS_INIT;
-	FILE *in;
-	struct strbuf buf = STRBUF_INIT, line = STRBUF_INIT;
+	struct strbuf buf = STRBUF_INIT, file = STRBUF_INIT;
 	struct patch_util *util = NULL;
 	int in_header = 1;
+	char *line;
+	int offset, len;
+	size_t size;
 
 	argv_array_pushl(&cp.args, "log", "--no-color", "-p", "--no-merges",
 			"--reverse", "--date-order", "--decorate=no",
@@ -54,17 +67,15 @@ static int read_patches(const char *range, struct string_list *list)
 
 	if (start_command(&cp))
 		return error_errno(_("could not start `log`"));
-	in = fdopen(cp.out, "r");
-	if (!in) {
-		error_errno(_("could not read `log` output"));
-		finish_command(&cp);
-		return -1;
-	}
+	strbuf_read(&file, cp.out, 0);
 
-	while (strbuf_getline(&line, in) != EOF) {
+	line = strbuf_detach(&file, &size);
+	for (offset = 0; size > 0; offset += len, size -= len, line += len) {
 		const char *p;
 
-		if (skip_prefix(line.buf, "commit ", &p)) {
+		len = linelen(line, size);
+		line[len - 1] = '\0';
+		if (skip_prefix(line, "commit ", &p)) {
 			if (util) {
 				string_list_append(list, buf.buf)->util = util;
 				strbuf_reset(&buf);
@@ -75,8 +86,6 @@ static int read_patches(const char *range, struct string_list *list)
 				free(util);
 				string_list_clear(list, 1);
 				strbuf_release(&buf);
-				strbuf_release(&line);
-				fclose(in);
 				finish_command(&cp);
 				return -1;
 			}
@@ -85,26 +94,28 @@ static int read_patches(const char *range, struct string_list *list)
 			continue;
 		}
 
-		if (starts_with(line.buf, "diff --git")) {
+		if (starts_with(line, "diff --git")) {
 			in_header = 0;
 			strbuf_addch(&buf, '\n');
 			if (!util->diff_offset)
 				util->diff_offset = buf.len;
 			strbuf_addch(&buf, ' ');
-			strbuf_addbuf(&buf, &line);
+			strbuf_addstr(&buf, line);
 		} else if (in_header) {
-			if (starts_with(line.buf, "Author: ")) {
-				strbuf_addbuf(&buf, &line);
+			if (starts_with(line, "Author: ")) {
+				strbuf_addstr(&buf, line);
 				strbuf_addstr(&buf, "\n\n");
-			} else if (starts_with(line.buf, "    ")) {
-				strbuf_rtrim(&line);
-				strbuf_addbuf(&buf, &line);
+			} else if (starts_with(line, "    ")) {
+				p = line + len - 2;
+				while (isspace(*p) && p >= line)
+					p--;
+				strbuf_add(&buf, line, p - line + 1);
 				strbuf_addch(&buf, '\n');
 			}
 			continue;
-		} else if (starts_with(line.buf, "@@ "))
+		} else if (starts_with(line, "@@ "))
 			strbuf_addstr(&buf, "@@");
-		else if (!line.buf[0] || starts_with(line.buf, "index "))
+		else if (!line[0] || starts_with(line, "index "))
 			/*
 			 * A completely blank (not ' \n', which is context)
 			 * line is not valid in a diff.  We skip it
@@ -117,25 +128,23 @@ static int read_patches(const char *range, struct string_list *list)
 			 * we are not interested.
 			 */
 			continue;
-		else if (line.buf[0] == '>') {
+		else if (line[0] == '>') {
 			strbuf_addch(&buf, '+');
-			strbuf_add(&buf, line.buf + 1, line.len - 1);
-		} else if (line.buf[0] == '<') {
+			strbuf_addstr(&buf, line + 1);
+		} else if (line[0] == '<') {
 			strbuf_addch(&buf, '-');
-			strbuf_add(&buf, line.buf + 1, line.len - 1);
-		} else if (line.buf[0] == '#') {
+			strbuf_addstr(&buf, line + 1);
+		} else if (line[0] == '#') {
 			strbuf_addch(&buf, ' ');
-			strbuf_add(&buf, line.buf + 1, line.len - 1);
+			strbuf_addstr(&buf, line + 1);
 		} else {
 			strbuf_addch(&buf, ' ');
-			strbuf_addbuf(&buf, &line);
+			strbuf_addstr(&buf, line);
 		}
 
 		strbuf_addch(&buf, '\n');
 		util->diffsize++;
 	}
-	fclose(in);
-	strbuf_release(&line);
 
 	if (util)
 		string_list_append(list, buf.buf)->util = util;
-- 
2.22.0.510.g264f2c817a
Johannes Schindelin· Jul 5, 2019, 19:05 UTC · re: Thomas Gummerer · lore

Re: [PATCH v2 09/14] range-diff: split lines manually

Hi Thomas,
On Fri, 5 Jul 2019, Thomas Gummerer wrote:
> Currently range-diff uses the 'strbuf_getline()' function for doing
> its line by line processing.  In a future patch we want to do parts of
> that parsing using the 'parse_git_header()' function, which does

If you like my suggestion in patch 7/14, this commit message needs to talk about the new name, too.

> requires reading parts of the input from that function, which doesn't
s/requires/require/
Show 11 quoted lines
> use strbufs.
>
> Switch range-diff to do our own line by line parsing, so we can re-use
> the parse_git_header function later.
>
> Signed-off-by: Thomas Gummerer <t.gummerer@gmail.com>
> ---
>
> Longer term it might be better to have both range-diff and apply code
> use strbufs.  However I didn't feel it's worth making that change for
> this patch series.
Makes sense.
Show 12 quoted lines
>  range-diff.c | 69 +++++++++++++++++++++++++++++-----------------------
>  1 file changed, 39 insertions(+), 30 deletions(-)
>
> diff --git a/range-diff.c b/range-diff.c
> index 9242b8975f..916afa44c0 100644
> --- a/range-diff.c
> +++ b/range-diff.c
> @@ -24,6 +24,17 @@ struct patch_util {
>  	struct object_id oid;
>  };
>
> +static unsigned long linelen(const char *buffer, unsigned long size)
Shouldn't this be `size_t`?
> +{
> +	unsigned long len = 0;
Likewise.
Show 6 quoted lines
> +	while (size--) {
> +		len++;
> +		if (*buffer++ == '\n')
> +			break;
> +	}
> +	return len;
How about
	const char *eol = memchr(buffer, '\n', size);
	return !eol ? size : eol + 1 - buffer;
instead?

For an extra brownie point, you could even rename this function to `find_end_of_line()` and replace the LF by a NUL:

	if (!eol)
		return size;
	*eol = '\0';
	return eol + 1 - buffer;
Show 12 quoted lines
> +}
> +
>  /*
>   * Reads the patches into a string list, with the `util` field being populated
>   * as struct object_id (will need to be free()d).
> @@ -31,10 +42,12 @@ struct patch_util {
>  static int read_patches(const char *range, struct string_list *list)
>  {
>  	struct child_process cp = CHILD_PROCESS_INIT;
> -	FILE *in;
> -	struct strbuf buf = STRBUF_INIT, line = STRBUF_INIT;
> +	struct strbuf buf = STRBUF_INIT, file = STRBUF_INIT;
This puzzled me. I'd like to suggest s/file/contents/
Show 19 quoted lines
>  	struct patch_util *util = NULL;
>  	int in_header = 1;
> +	char *line;
> +	int offset, len;
> +	size_t size;
>
>  	argv_array_pushl(&cp.args, "log", "--no-color", "-p", "--no-merges",
>  			"--reverse", "--date-order", "--decorate=no",
> @@ -54,17 +67,15 @@ static int read_patches(const char *range, struct string_list *list)
>
>  	if (start_command(&cp))
>  		return error_errno(_("could not start `log`"));
> -	in = fdopen(cp.out, "r");
> -	if (!in) {
> -		error_errno(_("could not read `log` output"));
> -		finish_command(&cp);
> -		return -1;
> -	}
> +	strbuf_read(&file, cp.out, 0);

Shouldn't we handle a negative return value here, erroring out with "could not read `log` output" as before?

>
> -	while (strbuf_getline(&line, in) != EOF) {
> +	line = strbuf_detach(&file, &size);

I strongly suspect this to leak, given that `line` is subsequently advanced, and there is no backup copy.

Maybe
	line = file.buf;
	size = file.len;
would make more sense here?
Show 16 quoted lines
> +	for (offset = 0; size > 0; offset += len, size -= len, line += len) {
>  		const char *p;
>
> -		if (skip_prefix(line.buf, "commit ", &p)) {
> +		len = linelen(line, size);
> +		line[len - 1] = '\0';
> +		if (skip_prefix(line, "commit ", &p)) {
>  			if (util) {
>  				string_list_append(list, buf.buf)->util = util;
>  				strbuf_reset(&buf);
> @@ -75,8 +86,6 @@ static int read_patches(const char *range, struct string_list *list)
>  				free(util);
>  				string_list_clear(list, 1);
>  				strbuf_release(&buf);
> -				strbuf_release(&line);
> -				fclose(in);

We should release the file contents in `file` (or `contents`, if you like my suggestions) here.

Show 71 quoted lines
>  				finish_command(&cp);
>  				return -1;
>  			}
> @@ -85,26 +94,28 @@ static int read_patches(const char *range, struct string_list *list)
>  			continue;
>  		}
>
> -		if (starts_with(line.buf, "diff --git")) {
> +		if (starts_with(line, "diff --git")) {
>  			in_header = 0;
>  			strbuf_addch(&buf, '\n');
>  			if (!util->diff_offset)
>  				util->diff_offset = buf.len;
>  			strbuf_addch(&buf, ' ');
> -			strbuf_addbuf(&buf, &line);
> +			strbuf_addstr(&buf, line);
>  		} else if (in_header) {
> -			if (starts_with(line.buf, "Author: ")) {
> -				strbuf_addbuf(&buf, &line);
> +			if (starts_with(line, "Author: ")) {
> +				strbuf_addstr(&buf, line);
>  				strbuf_addstr(&buf, "\n\n");
> -			} else if (starts_with(line.buf, "    ")) {
> -				strbuf_rtrim(&line);
> -				strbuf_addbuf(&buf, &line);
> +			} else if (starts_with(line, "    ")) {
> +				p = line + len - 2;
> +				while (isspace(*p) && p >= line)
> +					p--;
> +				strbuf_add(&buf, line, p - line + 1);
>  				strbuf_addch(&buf, '\n');
>  			}
>  			continue;
> -		} else if (starts_with(line.buf, "@@ "))
> +		} else if (starts_with(line, "@@ "))
>  			strbuf_addstr(&buf, "@@");
> -		else if (!line.buf[0] || starts_with(line.buf, "index "))
> +		else if (!line[0] || starts_with(line, "index "))
>  			/*
>  			 * A completely blank (not ' \n', which is context)
>  			 * line is not valid in a diff.  We skip it
> @@ -117,25 +128,23 @@ static int read_patches(const char *range, struct string_list *list)
>  			 * we are not interested.
>  			 */
>  			continue;
> -		else if (line.buf[0] == '>') {
> +		else if (line[0] == '>') {
>  			strbuf_addch(&buf, '+');
> -			strbuf_add(&buf, line.buf + 1, line.len - 1);
> -		} else if (line.buf[0] == '<') {
> +			strbuf_addstr(&buf, line + 1);
> +		} else if (line[0] == '<') {
>  			strbuf_addch(&buf, '-');
> -			strbuf_add(&buf, line.buf + 1, line.len - 1);
> -		} else if (line.buf[0] == '#') {
> +			strbuf_addstr(&buf, line + 1);
> +		} else if (line[0] == '#') {
>  			strbuf_addch(&buf, ' ');
> -			strbuf_add(&buf, line.buf + 1, line.len - 1);
> +			strbuf_addstr(&buf, line + 1);
>  		} else {
>  			strbuf_addch(&buf, ' ');
> -			strbuf_addbuf(&buf, &line);
> +			strbuf_addstr(&buf, line);
>  		}
>
>  		strbuf_addch(&buf, '\n');
>  		util->diffsize++;
>  	}
> -	fclose(in);
> -	strbuf_release(&line);
We should release the file contents we previously read via `strbuf_read()` here.

Ciao, Dscho

Show 7 quoted lines
>
>  	if (util)
>  		string_list_append(list, buf.buf)->util = util;
> --
> 2.22.0.510.g264f2c817a
>
>
Thomas Gummerer· Jul 8, 2019, 11:24 UTC · re: Johannes Schindelin · lore

Re: [PATCH v2 09/14] range-diff: split lines manually

On 07/05, Johannes Schindelin wrote:
Show 11 quoted lines
> Hi Thomas,
> 
> 
> On Fri, 5 Jul 2019, Thomas Gummerer wrote:
> 
> > Currently range-diff uses the 'strbuf_getline()' function for doing
> > its line by line processing.  In a future patch we want to do parts of
> > that parsing using the 'parse_git_header()' function, which does
> 
> If you like my suggestion in patch 7/14, this commit message needs to talk
> about the new name, too.

Thanks for the reminder here! I do indeed like the new name, but would probably have forgotten to change it in the commit message here.

Show 61 quoted lines
> > requires reading parts of the input from that function, which doesn't
> 
> s/requires/require/
> 
> > use strbufs.
> >
> > Switch range-diff to do our own line by line parsing, so we can re-use
> > the parse_git_header function later.
> >
> > Signed-off-by: Thomas Gummerer <t.gummerer@gmail.com>
> > ---
> >
> > Longer term it might be better to have both range-diff and apply code
> > use strbufs.  However I didn't feel it's worth making that change for
> > this patch series.
> 
> Makes sense.
> 
> >  range-diff.c | 69 +++++++++++++++++++++++++++++-----------------------
> >  1 file changed, 39 insertions(+), 30 deletions(-)
> >
> > diff --git a/range-diff.c b/range-diff.c
> > index 9242b8975f..916afa44c0 100644
> > --- a/range-diff.c
> > +++ b/range-diff.c
> > @@ -24,6 +24,17 @@ struct patch_util {
> >  	struct object_id oid;
> >  };
> >
> > +static unsigned long linelen(const char *buffer, unsigned long size)
> 
> Shouldn't this be `size_t`?
> 
> > +{
> > +	unsigned long len = 0;
> 
> Likewise.
> 
> > +	while (size--) {
> > +		len++;
> > +		if (*buffer++ == '\n')
> > +			break;
> > +	}
> > +	return len;
> 
> How about
> 
> 	const char *eol = memchr(buffer, '\n', size);
> 
> 	return !eol ? size : eol + 1 - buffer;
> 
> instead?
> 
> For an extra brownie point, you could even rename this function to
> `find_end_of_line()` and replace the LF by a NUL:
> 
> 	if (!eol)
> 		return size;
> 
> 	*eol = '\0';
> 	return eol + 1 - buffer;
I like this, thank you!
Show 14 quoted lines
> > +}
> > +
> >  /*
> >   * Reads the patches into a string list, with the `util` field being populated
> >   * as struct object_id (will need to be free()d).
> > @@ -31,10 +42,12 @@ struct patch_util {
> >  static int read_patches(const char *range, struct string_list *list)
> >  {
> >  	struct child_process cp = CHILD_PROCESS_INIT;
> > -	FILE *in;
> > -	struct strbuf buf = STRBUF_INIT, line = STRBUF_INIT;
> > +	struct strbuf buf = STRBUF_INIT, file = STRBUF_INIT;
> 
> This puzzled me. I'd like to suggest s/file/contents/
Thanks, will change.
Show 22 quoted lines
> >  	struct patch_util *util = NULL;
> >  	int in_header = 1;
> > +	char *line;
> > +	int offset, len;
> > +	size_t size;
> >
> >  	argv_array_pushl(&cp.args, "log", "--no-color", "-p", "--no-merges",
> >  			"--reverse", "--date-order", "--decorate=no",
> > @@ -54,17 +67,15 @@ static int read_patches(const char *range, struct string_list *list)
> >
> >  	if (start_command(&cp))
> >  		return error_errno(_("could not start `log`"));
> > -	in = fdopen(cp.out, "r");
> > -	if (!in) {
> > -		error_errno(_("could not read `log` output"));
> > -		finish_command(&cp);
> > -		return -1;
> > -	}
> > +	strbuf_read(&file, cp.out, 0);
> 
> Shouldn't we handle a negative return value here, erroring out with "could
> not read `log` output" as before?

Yeah, that was an oversight, we should definitely still handle errors here.

Show 13 quoted lines
> >
> > -	while (strbuf_getline(&line, in) != EOF) {
> > +	line = strbuf_detach(&file, &size);
> 
> I strongly suspect this to leak, given that `line` is subsequently
> advanced, and there is no backup copy.
> 
> Maybe
> 
> 	line = file.buf;
> 	size = file.len;
> 
> would make more sense here?
Hmm good point, that makes more sense indeed.
Show 19 quoted lines
> > +	for (offset = 0; size > 0; offset += len, size -= len, line += len) {
> >  		const char *p;
> >
> > -		if (skip_prefix(line.buf, "commit ", &p)) {
> > +		len = linelen(line, size);
> > +		line[len - 1] = '\0';
> > +		if (skip_prefix(line, "commit ", &p)) {
> >  			if (util) {
> >  				string_list_append(list, buf.buf)->util = util;
> >  				strbuf_reset(&buf);
> > @@ -75,8 +86,6 @@ static int read_patches(const char *range, struct string_list *list)
> >  				free(util);
> >  				string_list_clear(list, 1);
> >  				strbuf_release(&buf);
> > -				strbuf_release(&line);
> > -				fclose(in);
> 
> We should release the file contents in `file` (or `contents`, if you like
> my suggestions) here.

Yeah, I thought it was no longer necessary because of the 'strbuf_detach()' earlier, but that obviously leaks in a different way as you pointed out. Will release 'contents' here and below.

Show 84 quoted lines
> >  				finish_command(&cp);
> >  				return -1;
> >  			}
> > @@ -85,26 +94,28 @@ static int read_patches(const char *range, struct string_list *list)
> >  			continue;
> >  		}
> >
> > -		if (starts_with(line.buf, "diff --git")) {
> > +		if (starts_with(line, "diff --git")) {
> >  			in_header = 0;
> >  			strbuf_addch(&buf, '\n');
> >  			if (!util->diff_offset)
> >  				util->diff_offset = buf.len;
> >  			strbuf_addch(&buf, ' ');
> > -			strbuf_addbuf(&buf, &line);
> > +			strbuf_addstr(&buf, line);
> >  		} else if (in_header) {
> > -			if (starts_with(line.buf, "Author: ")) {
> > -				strbuf_addbuf(&buf, &line);
> > +			if (starts_with(line, "Author: ")) {
> > +				strbuf_addstr(&buf, line);
> >  				strbuf_addstr(&buf, "\n\n");
> > -			} else if (starts_with(line.buf, "    ")) {
> > -				strbuf_rtrim(&line);
> > -				strbuf_addbuf(&buf, &line);
> > +			} else if (starts_with(line, "    ")) {
> > +				p = line + len - 2;
> > +				while (isspace(*p) && p >= line)
> > +					p--;
> > +				strbuf_add(&buf, line, p - line + 1);
> >  				strbuf_addch(&buf, '\n');
> >  			}
> >  			continue;
> > -		} else if (starts_with(line.buf, "@@ "))
> > +		} else if (starts_with(line, "@@ "))
> >  			strbuf_addstr(&buf, "@@");
> > -		else if (!line.buf[0] || starts_with(line.buf, "index "))
> > +		else if (!line[0] || starts_with(line, "index "))
> >  			/*
> >  			 * A completely blank (not ' \n', which is context)
> >  			 * line is not valid in a diff.  We skip it
> > @@ -117,25 +128,23 @@ static int read_patches(const char *range, struct string_list *list)
> >  			 * we are not interested.
> >  			 */
> >  			continue;
> > -		else if (line.buf[0] == '>') {
> > +		else if (line[0] == '>') {
> >  			strbuf_addch(&buf, '+');
> > -			strbuf_add(&buf, line.buf + 1, line.len - 1);
> > -		} else if (line.buf[0] == '<') {
> > +			strbuf_addstr(&buf, line + 1);
> > +		} else if (line[0] == '<') {
> >  			strbuf_addch(&buf, '-');
> > -			strbuf_add(&buf, line.buf + 1, line.len - 1);
> > -		} else if (line.buf[0] == '#') {
> > +			strbuf_addstr(&buf, line + 1);
> > +		} else if (line[0] == '#') {
> >  			strbuf_addch(&buf, ' ');
> > -			strbuf_add(&buf, line.buf + 1, line.len - 1);
> > +			strbuf_addstr(&buf, line + 1);
> >  		} else {
> >  			strbuf_addch(&buf, ' ');
> > -			strbuf_addbuf(&buf, &line);
> > +			strbuf_addstr(&buf, line);
> >  		}
> >
> >  		strbuf_addch(&buf, '\n');
> >  		util->diffsize++;
> >  	}
> > -	fclose(in);
> > -	strbuf_release(&line);
> 
> We should release the file contents we previously read via `strbuf_read()` here.
> 
> Ciao,
> Dscho
> 
> >
> >  	if (util)
> >  		string_list_append(list, buf.buf)->util = util;
> > --
> > 2.22.0.510.g264f2c817a
> >
> >
Thomas Gummerer· Jul 5, 2019, 17:06 UTC · re: Thomas Gummerer · lore

[PATCH v2 10/14] range-diff: don't remove funcname from inner diff

When postprocessing the inner diff in range-diff, we currently replace the whole hunk header line with just "@@". This matches how 'git tbdiff' used to handle hunk headers as well.

Most likely this is being done because line numbers in the hunk header are not relevant without other changes. They can for example easily change if a range is rebased, and lines are added/removed before a change that we actually care about in our ranges.

However it can still be useful to have the function name that 'git diff' extracts as additional context for the change.

Note that it is not guaranteed that the hunk header actually shows up in the range-diff, and this change only aims to improve the case where a hunk header would already be included in the final output.

Signed-off-by: Thomas Gummerer <t.gummerer@gmail.com>
---
 range-diff.c          | 8 +++++---
 t/t3206-range-diff.sh | 6 +++---
 2 files changed, 8 insertions(+), 6 deletions(-)
Show changes to 2 files +8 −6

range-diff.c, t/t3206-range-diff.sh

diff --git a/range-diff.c b/range-diff.c
index 916afa44c0..484b1ec5a9 100644
--- a/range-diff.c
+++ b/range-diff.c
@@ -113,9 +113,11 @@ static int read_patches(const char *range, struct string_list *list)
 				strbuf_addch(&buf, '\n');
 			}
 			continue;
-		} else if (starts_with(line, "@@ "))
-			strbuf_addstr(&buf, "@@");
-		else if (!line[0] || starts_with(line, "index "))
+		} else if (skip_prefix(line, "@@ ", &p)) {
+			if (!(p = strstr(p, "@@")))
+				die(_("invalid hunk header in inner diff"));
+			strbuf_addstr(&buf, p);
+		} else if (!line[0] || starts_with(line, "index "))
 			/*
 			 * A completely blank (not ' \n', which is context)
 			 * line is not valid in a diff.  We skip it
diff --git a/t/t3206-range-diff.sh b/t/t3206-range-diff.sh
index 048feaf6dd..aebd4e3693 100755
--- a/t/t3206-range-diff.sh
+++ b/t/t3206-range-diff.sh
@@ -110,7 +110,7 @@ test_expect_success 'changed commit' '
 	      14
 	4:  a63e992 ! 4:  d966c5c s/12/B/
 	    @@ -8,7 +8,7 @@
-	     @@
+	     @@ A
 	      9
 	      10
 	    - B
@@ -169,7 +169,7 @@ test_expect_success 'changed commit with sm config' '
 	      14
 	4:  a63e992 ! 4:  d966c5c s/12/B/
 	    @@ -8,7 +8,7 @@
-	     @@
+	     @@ A
 	      9
 	      10
 	    - B
@@ -231,7 +231,7 @@ test_expect_success 'dual-coloring' '
 	:      14<RESET>
 	:<RED>4:  d966c5c <RESET><YELLOW>!<RESET><GREEN> 4:  8add5f1<RESET><YELLOW> s/12/B/<RESET>
 	:    <REVERSE><CYAN>@@ -8,7 +8,7 @@<RESET>
-	:    <CYAN> @@<RESET>
+	:    <CYAN> @@ A<RESET>
 	:      9<RESET>
 	:      10<RESET>
 	:    <REVERSE><RED>-<RESET><FAINT> BB<RESET>
-- 
2.22.0.510.g264f2c817a
Johannes Schindelin· Jul 5, 2019, 19:09 UTC · re: Thomas Gummerer · lore

Re: [PATCH v2 10/14] range-diff: don't remove funcname from inner diff

Hi Thomas,
On Fri, 5 Jul 2019, Thomas Gummerer wrote:
> When postprocessing the inner diff in range-diff, we currently replace
> the whole hunk header line with just "@@".  This matches how 'git
> tbdiff' used to handle hunk headers as well.
Right, that's why I did it this way: `tbdiff` did it.
> Most likely this is being done because line numbers in the hunk header
> are not relevant without other changes.
Yep, and even worse, it also leads to uninteresting differences.
> They can for example easily change if a range is rebased, and lines are
> added/removed before a change that we actually care about in our ranges.
Exactly.
Show 6 quoted lines
> However it can still be useful to have the function name that 'git
> diff' extracts as additional context for the change.
>
> Note that it is not guaranteed that the hunk header actually shows up
> in the range-diff, and this change only aims to improve the case where
> a hunk header would already be included in the final output.
Very important paragraph here, thank you!
Show 20 quoted lines
> Signed-off-by: Thomas Gummerer <t.gummerer@gmail.com>
> ---
>  range-diff.c          | 8 +++++---
>  t/t3206-range-diff.sh | 6 +++---
>  2 files changed, 8 insertions(+), 6 deletions(-)
>
> diff --git a/range-diff.c b/range-diff.c
> index 916afa44c0..484b1ec5a9 100644
> --- a/range-diff.c
> +++ b/range-diff.c
> @@ -113,9 +113,11 @@ static int read_patches(const char *range, struct string_list *list)
>  				strbuf_addch(&buf, '\n');
>  			}
>  			continue;
> -		} else if (starts_with(line, "@@ "))
> -			strbuf_addstr(&buf, "@@");
> -		else if (!line[0] || starts_with(line, "index "))
> +		} else if (skip_prefix(line, "@@ ", &p)) {
> +			if (!(p = strstr(p, "@@")))
> +				die(_("invalid hunk header in inner diff"));
That's a bit more strict than the preimage. How about
			p = strstr(p, "@@");
			strbuf_addstr(&buf, p ? p : "@@");
instead?

The rest of the patch looks fine to me, Dscho

Show 40 quoted lines
> +			strbuf_addstr(&buf, p);
> +		} else if (!line[0] || starts_with(line, "index "))
>  			/*
>  			 * A completely blank (not ' \n', which is context)
>  			 * line is not valid in a diff.  We skip it
> diff --git a/t/t3206-range-diff.sh b/t/t3206-range-diff.sh
> index 048feaf6dd..aebd4e3693 100755
> --- a/t/t3206-range-diff.sh
> +++ b/t/t3206-range-diff.sh
> @@ -110,7 +110,7 @@ test_expect_success 'changed commit' '
>  	      14
>  	4:  a63e992 ! 4:  d966c5c s/12/B/
>  	    @@ -8,7 +8,7 @@
> -	     @@
> +	     @@ A
>  	      9
>  	      10
>  	    - B
> @@ -169,7 +169,7 @@ test_expect_success 'changed commit with sm config' '
>  	      14
>  	4:  a63e992 ! 4:  d966c5c s/12/B/
>  	    @@ -8,7 +8,7 @@
> -	     @@
> +	     @@ A
>  	      9
>  	      10
>  	    - B
> @@ -231,7 +231,7 @@ test_expect_success 'dual-coloring' '
>  	:      14<RESET>
>  	:<RED>4:  d966c5c <RESET><YELLOW>!<RESET><GREEN> 4:  8add5f1<RESET><YELLOW> s/12/B/<RESET>
>  	:    <REVERSE><CYAN>@@ -8,7 +8,7 @@<RESET>
> -	:    <CYAN> @@<RESET>
> +	:    <CYAN> @@ A<RESET>
>  	:      9<RESET>
>  	:      10<RESET>
>  	:    <REVERSE><RED>-<RESET><FAINT> BB<RESET>
> --
> 2.22.0.510.g264f2c817a
>
>
Thomas Gummerer· Jul 5, 2019, 17:06 UTC · re: Thomas Gummerer · lore

[PATCH v2 11/14] range-diff: suppress line count in outer diff

The line count in the outer diff's hunk headers of a range diff is not all that interesting. It merely shows how far along the inner diff are on both sides. That number is of no use for human readers, and range-diffs are not meant to be machine readable.

In a subsequent commit we're going to add some more contextual information such as the filename corresponding to the diff to the hunk headers. Remove the unnecessary information, and just keep the "@@" to indicate that a new hunk of the outer diff is starting.

Signed-off-by: Thomas Gummerer <t.gummerer@gmail.com>
---
 diff.c                |  5 ++++-
 diff.h                |  1 +
 range-diff.c          |  1 +
 t/t3206-range-diff.sh | 16 ++++++++--------
 4 files changed, 14 insertions(+), 9 deletions(-)
Show changes to 4 files +14 −9

diff.c, diff.h, range-diff.c, t/t3206-range-diff.sh

diff --git a/diff.c b/diff.c
index ec5c095199..9c28ff0a92 100644
--- a/diff.c
+++ b/diff.c
@@ -1672,7 +1672,10 @@ static void emit_hunk_header(struct emit_callback *ecbdata,
 	if (ecbdata->opt->flags.dual_color_diffed_diffs)
 		strbuf_addstr(&msgbuf, reverse);
 	strbuf_addstr(&msgbuf, frag);
-	strbuf_add(&msgbuf, line, ep - line);
+	if (ecbdata->opt->flags.suppress_hunk_header_line_count)
+		strbuf_add(&msgbuf, atat, sizeof(atat));
+	else
+		strbuf_add(&msgbuf, line, ep - line);
 	strbuf_addstr(&msgbuf, reset);
 
 	/*
diff --git a/diff.h b/diff.h
index c9db9825bb..49913049f9 100644
--- a/diff.h
+++ b/diff.h
@@ -98,6 +98,7 @@ struct diff_flags {
 	unsigned stat_with_summary;
 	unsigned suppress_diff_headers;
 	unsigned dual_color_diffed_diffs;
+	unsigned suppress_hunk_header_line_count;
 };
 
 static inline void diff_flags_or(struct diff_flags *a,
diff --git a/range-diff.c b/range-diff.c
index 484b1ec5a9..b31fbab026 100644
--- a/range-diff.c
+++ b/range-diff.c
@@ -480,6 +480,7 @@ int show_range_diff(const char *range1, const char *range2,
 			opts.output_format = DIFF_FORMAT_PATCH;
 		opts.flags.suppress_diff_headers = 1;
 		opts.flags.dual_color_diffed_diffs = dual_color;
+		opts.flags.suppress_hunk_header_line_count = 1;
 		opts.output_prefix = output_prefix_cb;
 		strbuf_addstr(&indent, "    ");
 		opts.output_prefix_data = &indent;
diff --git a/t/t3206-range-diff.sh b/t/t3206-range-diff.sh
index aebd4e3693..9f89af7178 100755
--- a/t/t3206-range-diff.sh
+++ b/t/t3206-range-diff.sh
@@ -99,7 +99,7 @@ test_expect_success 'changed commit' '
 	1:  4de457d = 1:  a4b3333 s/5/A/
 	2:  fccce22 = 2:  f51d370 s/4/A/
 	3:  147e64e ! 3:  0559556 s/11/B/
-	    @@ -10,7 +10,7 @@
+	    @@
 	      9
 	      10
 	     -11
@@ -109,7 +109,7 @@ test_expect_success 'changed commit' '
 	      13
 	      14
 	4:  a63e992 ! 4:  d966c5c s/12/B/
-	    @@ -8,7 +8,7 @@
+	    @@
 	     @@ A
 	      9
 	      10
@@ -158,7 +158,7 @@ test_expect_success 'changed commit with sm config' '
 	1:  4de457d = 1:  a4b3333 s/5/A/
 	2:  fccce22 = 2:  f51d370 s/4/A/
 	3:  147e64e ! 3:  0559556 s/11/B/
-	    @@ -10,7 +10,7 @@
+	    @@
 	      9
 	      10
 	     -11
@@ -168,7 +168,7 @@ test_expect_success 'changed commit with sm config' '
 	      13
 	      14
 	4:  a63e992 ! 4:  d966c5c s/12/B/
-	    @@ -8,7 +8,7 @@
+	    @@
 	     @@ A
 	      9
 	      10
@@ -191,7 +191,7 @@ test_expect_success 'changed message' '
 	sed s/Z/\ /g >expected <<-EOF &&
 	1:  4de457d = 1:  f686024 s/5/A/
 	2:  fccce22 ! 2:  4ab067d s/4/A/
-	    @@ -2,6 +2,8 @@
+	    @@
 	    Z
 	    Z    s/4/A/
 	    Z
@@ -210,7 +210,7 @@ test_expect_success 'dual-coloring' '
 	sed -e "s|^:||" >expect <<-\EOF &&
 	:<YELLOW>1:  a4b3333 = 1:  f686024 s/5/A/<RESET>
 	:<RED>2:  f51d370 <RESET><YELLOW>!<RESET><GREEN> 2:  4ab067d<RESET><YELLOW> s/4/A/<RESET>
-	:    <REVERSE><CYAN>@@ -2,6 +2,8 @@<RESET>
+	:    <REVERSE><CYAN>@@<RESET>
 	:     <RESET>
 	:         s/4/A/<RESET>
 	:     <RESET>
@@ -220,7 +220,7 @@ test_expect_success 'dual-coloring' '
 	:      --- a/file<RESET>
 	:      +++ b/file<RESET>
 	:<RED>3:  0559556 <RESET><YELLOW>!<RESET><GREEN> 3:  b9cb956<RESET><YELLOW> s/11/B/<RESET>
-	:    <REVERSE><CYAN>@@ -10,7 +10,7 @@<RESET>
+	:    <REVERSE><CYAN>@@<RESET>
 	:      9<RESET>
 	:      10<RESET>
 	:    <RED> -11<RESET>
@@ -230,7 +230,7 @@ test_expect_success 'dual-coloring' '
 	:      13<RESET>
 	:      14<RESET>
 	:<RED>4:  d966c5c <RESET><YELLOW>!<RESET><GREEN> 4:  8add5f1<RESET><YELLOW> s/12/B/<RESET>
-	:    <REVERSE><CYAN>@@ -8,7 +8,7 @@<RESET>
+	:    <REVERSE><CYAN>@@<RESET>
 	:    <CYAN> @@ A<RESET>
 	:      9<RESET>
 	:      10<RESET>
-- 
2.22.0.510.g264f2c817a
Thomas Gummerer· Jul 5, 2019, 17:06 UTC · re: Thomas Gummerer · lore

[PATCH v2 12/14] range-diff: add section header instead of diff header

Currently range-diff keeps the diff header of the inner diff intact (apart from stripping lines starting with index). This diff header is somewhat useful, especially when files get different names in different ranges.

However there is no real need to keep the whole diff header for that. The main reason we currently do that is probably because it is easy to do.

Introduce a new range diff hunk header, that's enclosed by "##", similar to how line numbers in diff hunks are enclosed by "@@", and give human readable information of what exactly happened to the file, including the file name.

This improves the readability of the range-diff by giving more concise information to the users. For example if a file was renamed in one iteration, but not in another, the diff of the headers would be quite noisy. However the diff of a single line is concise and should be easier to understand.

Additionaly, this allows us to add these range diff section headers to the outer diffs hunk headers using a custom userdiff pattern, which should help making the range-diff more readable.

Signed-off-by: Thomas Gummerer <t.gummerer@gmail.com>
---
 range-diff.c           | 35 ++++++++++++----
 t/t3206-range-diff.sh  | 91 +++++++++++++++++++++++++++++++++++++++---
 t/t3206/history.export | 84 ++++++++++++++++++++++++++++++++++++--
 3 files changed, 193 insertions(+), 17 deletions(-)
Show changes to 3 files +193 −17

range-diff.c, t/t3206-range-diff.sh, t/t3206/history.export

diff --git a/range-diff.c b/range-diff.c
index b31fbab026..cc01f7f573 100644
--- a/range-diff.c
+++ b/range-diff.c
@@ -10,6 +10,7 @@
 #include "commit.h"
 #include "pretty.h"
 #include "userdiff.h"
+#include "apply.h"
 
 struct patch_util {
 	/* For the search for an exact match */
@@ -95,12 +96,36 @@ static int read_patches(const char *range, struct string_list *list)
 		}
 
 		if (starts_with(line, "diff --git")) {
+			struct patch patch;
+			struct strbuf root = STRBUF_INIT;
+			int linenr = 0;
+
 			in_header = 0;
 			strbuf_addch(&buf, '\n');
 			if (!util->diff_offset)
 				util->diff_offset = buf.len;
-			strbuf_addch(&buf, ' ');
-			strbuf_addstr(&buf, line);
+			memset(&patch, 0, sizeof(patch));
+			line[len - 1] = '\n';
+			len = parse_git_header(&root, &linenr, 1, line,
+					       len, size, &patch);
+			if (len < 0)
+				die(_("could not parse git header"));
+			strbuf_addstr(&buf, " ## ");
+			if (patch.is_new > 0)
+				strbuf_addf(&buf, "%s (new)", patch.new_name);
+			else if (patch.is_delete > 0)
+				strbuf_addf(&buf, "%s (deleted)", patch.old_name);
+			else if (patch.is_rename)
+				strbuf_addf(&buf, "%s => %s", patch.old_name, patch.new_name);
+			else
+				strbuf_addstr(&buf, patch.new_name);
+
+			if (patch.new_mode && patch.old_mode &&
+			    patch.old_mode != patch.new_mode)
+				strbuf_addf(&buf, " (mode change %06o => %06o)",
+					    patch.old_mode, patch.new_mode);
+
+			strbuf_addstr(&buf, " ##");
 		} else if (in_header) {
 			if (starts_with(line, "Author: ")) {
 				strbuf_addstr(&buf, line);
@@ -117,17 +142,13 @@ static int read_patches(const char *range, struct string_list *list)
 			if (!(p = strstr(p, "@@")))
 				die(_("invalid hunk header in inner diff"));
 			strbuf_addstr(&buf, p);
-		} else if (!line[0] || starts_with(line, "index "))
+		} else if (!line[0])
 			/*
 			 * A completely blank (not ' \n', which is context)
 			 * line is not valid in a diff.  We skip it
 			 * silently, because this neatly handles the blank
 			 * separator line between commits in git-log
 			 * output.
-			 *
-			 * We also want to ignore the diff's `index` lines
-			 * because they contain exact blob hashes in which
-			 * we are not interested.
 			 */
 			continue;
 		else if (line[0] == '>') {
diff --git a/t/t3206-range-diff.sh b/t/t3206-range-diff.sh
index 9f89af7178..c277756057 100755
--- a/t/t3206-range-diff.sh
+++ b/t/t3206-range-diff.sh
@@ -181,6 +181,85 @@ test_expect_success 'changed commit with sm config' '
 	test_cmp expected actual
 '
 
+test_expect_success 'renamed file' '
+	git range-diff --no-color --submodule=log topic...renamed-file >actual &&
+	sed s/Z/\ /g >expected <<-EOF &&
+	1:  4de457d = 1:  f258d75 s/5/A/
+	2:  fccce22 ! 2:  017b62d s/4/A/
+	    @@
+	    ZAuthor: Thomas Rast <trast@inf.ethz.ch>
+	    Z
+	    -    s/4/A/
+	    +    s/4/A/ + rename file
+	    Z
+	    - ## file ##
+	    + ## file => renamed-file ##
+	    Z@@
+	    Z 1
+	    Z 2
+	3:  147e64e ! 3:  3ce7af6 s/11/B/
+	    @@
+	    Z
+	    Z    s/11/B/
+	    Z
+	    - ## file ##
+	    + ## renamed-file ##
+	    Z@@ A
+	    Z 8
+	    Z 9
+	4:  a63e992 ! 4:  1e6226b s/12/B/
+	    @@
+	    Z
+	    Z    s/12/B/
+	    Z
+	    - ## file ##
+	    + ## renamed-file ##
+	    Z@@ A
+	    Z 9
+	    Z 10
+	EOF
+	test_cmp expected actual
+'
+
+test_expect_success 'file added and later removed' '
+	git range-diff --no-color --submodule=log topic...added-removed >actual &&
+	sed s/Z/\ /g >expected <<-EOF &&
+	1:  4de457d = 1:  096b1ba s/5/A/
+	2:  fccce22 ! 2:  d92e698 s/4/A/
+	    @@
+	    ZAuthor: Thomas Rast <trast@inf.ethz.ch>
+	    Z
+	    -    s/4/A/
+	    +    s/4/A/ + new-file
+	    Z
+	    Z ## file ##
+	    Z@@
+	    @@
+	    Z A
+	    Z 6
+	    Z 7
+	    +
+	    + ## new-file (new) ##
+	3:  147e64e ! 3:  9a1db4d s/11/B/
+	    @@
+	    ZAuthor: Thomas Rast <trast@inf.ethz.ch>
+	    Z
+	    -    s/11/B/
+	    +    s/11/B/ + remove file
+	    Z
+	    Z ## file ##
+	    Z@@ A
+	    @@
+	    Z 12
+	    Z 13
+	    Z 14
+	    +
+	    + ## new-file (deleted) ##
+	4:  a63e992 = 4:  fea3b5c s/12/B/
+	EOF
+	test_cmp expected actual
+'
+
 test_expect_success 'no commits on one side' '
 	git commit --amend -m "new message" &&
 	git range-diff master HEAD@{1} HEAD
@@ -197,9 +276,9 @@ test_expect_success 'changed message' '
 	    Z
 	    +    Also a silly comment here!
 	    +
-	    Z diff --git a/file b/file
-	    Z --- a/file
-	    Z +++ b/file
+	    Z ## file ##
+	    Z@@
+	    Z 1
 	3:  147e64e = 3:  b9cb956 s/11/B/
 	4:  a63e992 = 4:  8add5f1 s/12/B/
 	EOF
@@ -216,9 +295,9 @@ test_expect_success 'dual-coloring' '
 	:     <RESET>
 	:    <REVERSE><GREEN>+<RESET><BOLD>    Also a silly comment here!<RESET>
 	:    <REVERSE><GREEN>+<RESET>
-	:      diff --git a/file b/file<RESET>
-	:      --- a/file<RESET>
-	:      +++ b/file<RESET>
+	:      ## file ##<RESET>
+	:    <CYAN> @@<RESET>
+	:      1<RESET>
 	:<RED>3:  0559556 <RESET><YELLOW>!<RESET><GREEN> 3:  b9cb956<RESET><YELLOW> s/11/B/<RESET>
 	:    <REVERSE><CYAN>@@<RESET>
 	:      9<RESET>
diff --git a/t/t3206/history.export b/t/t3206/history.export
index b8ffff0940..7bb3814962 100644
--- a/t/t3206/history.export
+++ b/t/t3206/history.export
@@ -22,8 +22,8 @@ data 51
 19
 20
 
-reset refs/heads/removed
-commit refs/heads/removed
+reset refs/heads/renamed-file
+commit refs/heads/renamed-file
 mark :2
 author Thomas Rast <trast@inf.ethz.ch> 1374424921 +0200
 committer Thomas Rast <trast@inf.ethz.ch> 1374484724 +0200
@@ -599,6 +599,82 @@ s/12/B/
 from :46
 M 100644 :28 file
 
-reset refs/heads/removed
-from :47
+commit refs/heads/added-removed
+mark :48
+author Thomas Rast <trast@inf.ethz.ch> 1374485014 +0200
+committer Thomas Gummerer <t.gummerer@gmail.com> 1556574151 +0100
+data 7
+s/5/A/
+from :2
+M 100644 :3 file
+
+blob
+mark :49
+data 0
+
+commit refs/heads/added-removed
+mark :50
+author Thomas Rast <trast@inf.ethz.ch> 1374485024 +0200
+committer Thomas Gummerer <t.gummerer@gmail.com> 1556574177 +0100
+data 18
+s/4/A/ + new-file
+from :48
+M 100644 :5 file
+M 100644 :49 new-file
+
+commit refs/heads/added-removed
+mark :51
+author Thomas Rast <trast@inf.ethz.ch> 1374485036 +0200
+committer Thomas Gummerer <t.gummerer@gmail.com> 1556574177 +0100
+data 22
+s/11/B/ + remove file
+from :50
+M 100644 :7 file
+D new-file
+
+commit refs/heads/added-removed
+mark :52
+author Thomas Rast <trast@inf.ethz.ch> 1374485044 +0200
+committer Thomas Gummerer <t.gummerer@gmail.com> 1556574177 +0100
+data 8
+s/12/B/
+from :51
+M 100644 :9 file
+
+commit refs/heads/renamed-file
+mark :53
+author Thomas Rast <trast@inf.ethz.ch> 1374485014 +0200
+committer Thomas Gummerer <t.gummerer@gmail.com> 1556574309 +0100
+data 7
+s/5/A/
+from :2
+M 100644 :3 file
+
+commit refs/heads/renamed-file
+mark :54
+author Thomas Rast <trast@inf.ethz.ch> 1374485024 +0200
+committer Thomas Gummerer <t.gummerer@gmail.com> 1556574312 +0100
+data 21
+s/4/A/ + rename file
+from :53
+D file
+M 100644 :5 renamed-file
+
+commit refs/heads/renamed-file
+mark :55
+author Thomas Rast <trast@inf.ethz.ch> 1374485036 +0200
+committer Thomas Gummerer <t.gummerer@gmail.com> 1556574319 +0100
+data 8
+s/11/B/
+from :54
+M 100644 :7 renamed-file
+
+commit refs/heads/renamed-file
+mark :56
+author Thomas Rast <trast@inf.ethz.ch> 1374485044 +0200
+committer Thomas Gummerer <t.gummerer@gmail.com> 1556574319 +0100
+data 8
+s/12/B/
+from :55
+M 100644 :9 renamed-file
 
-- 
2.22.0.510.g264f2c817a
Johannes Schindelin· Jul 5, 2019, 19:35 UTC · re: Thomas Gummerer · lore

Re: [PATCH v2 12/14] range-diff: add section header instead of diff header

Hi Thomas,
On Fri, 5 Jul 2019, Thomas Gummerer wrote:
Show 21 quoted lines
> Currently range-diff keeps the diff header of the inner diff
> intact (apart from stripping lines starting with index).  This diff
> header is somewhat useful, especially when files get different
> names in different ranges.
>
> However there is no real need to keep the whole diff header for that.
> The main reason we currently do that is probably because it is easy to
> do.
>
> Introduce a new range diff hunk header, that's enclosed by "##",
> similar to how line numbers in diff hunks are enclosed by "@@", and
> give human readable information of what exactly happened to the file,
> including the file name.
>
> This improves the readability of the range-diff by giving more concise
> information to the users.  For example if a file was renamed in one
> iteration, but not in another, the diff of the headers would be quite
> noisy.  However the diff of a single line is concise and should be
> easier to understand.
>
> Additionaly, this allows us to add these range diff section headers to
s/Additionaly/Additionally/
> the outer diffs hunk headers using a custom userdiff pattern, which
> should help making the range-diff more readable.
Makes sense.
Show 24 quoted lines
> Signed-off-by: Thomas Gummerer <t.gummerer@gmail.com>
> ---
>  range-diff.c           | 35 ++++++++++++----
>  t/t3206-range-diff.sh  | 91 +++++++++++++++++++++++++++++++++++++++---
>  t/t3206/history.export | 84 ++++++++++++++++++++++++++++++++++++--
>  3 files changed, 193 insertions(+), 17 deletions(-)
>
> diff --git a/range-diff.c b/range-diff.c
> index b31fbab026..cc01f7f573 100644
> --- a/range-diff.c
> +++ b/range-diff.c
> @@ -10,6 +10,7 @@
>  #include "commit.h"
>  #include "pretty.h"
>  #include "userdiff.h"
> +#include "apply.h"
>
>  struct patch_util {
>  	/* For the search for an exact match */
> @@ -95,12 +96,36 @@ static int read_patches(const char *range, struct string_list *list)
>  		}
>
>  		if (starts_with(line, "diff --git")) {
> +			struct patch patch;

If you append ` = { 0 }` (or ` = { NULL }`, depending on the first field of that struct, you don't need that `memset()` later.

Show 11 quoted lines
> +			struct strbuf root = STRBUF_INIT;
> +			int linenr = 0;
> +
>  			in_header = 0;
>  			strbuf_addch(&buf, '\n');
>  			if (!util->diff_offset)
>  				util->diff_offset = buf.len;
> -			strbuf_addch(&buf, ' ');
> -			strbuf_addstr(&buf, line);
> +			memset(&patch, 0, sizeof(patch));
> +			line[len - 1] = '\n';
I guess `parse_git_header()` cannot handle lines ending in a NUL?
> +			len = parse_git_header(&root, &linenr, 1, line,
> +					       len, size, &patch);
> +			if (len < 0)
> +				die(_("could not parse git header"));
Maybe include the line's contents, like ` '%.*s'", (int)len, line`?
Show 35 quoted lines
> +			strbuf_addstr(&buf, " ## ");
> +			if (patch.is_new > 0)
> +				strbuf_addf(&buf, "%s (new)", patch.new_name);
> +			else if (patch.is_delete > 0)
> +				strbuf_addf(&buf, "%s (deleted)", patch.old_name);
> +			else if (patch.is_rename)
> +				strbuf_addf(&buf, "%s => %s", patch.old_name, patch.new_name);
> +			else
> +				strbuf_addstr(&buf, patch.new_name);
> +
> +			if (patch.new_mode && patch.old_mode &&
> +			    patch.old_mode != patch.new_mode)
> +				strbuf_addf(&buf, " (mode change %06o => %06o)",
> +					    patch.old_mode, patch.new_mode);
> +
> +			strbuf_addstr(&buf, " ##");
>  		} else if (in_header) {
>  			if (starts_with(line, "Author: ")) {
>  				strbuf_addstr(&buf, line);
> @@ -117,17 +142,13 @@ static int read_patches(const char *range, struct string_list *list)
>  			if (!(p = strstr(p, "@@")))
>  				die(_("invalid hunk header in inner diff"));
>  			strbuf_addstr(&buf, p);
> -		} else if (!line[0] || starts_with(line, "index "))
> +		} else if (!line[0])
>  			/*
>  			 * A completely blank (not ' \n', which is context)
>  			 * line is not valid in a diff.  We skip it
>  			 * silently, because this neatly handles the blank
>  			 * separator line between commits in git-log
>  			 * output.
> -			 *
> -			 * We also want to ignore the diff's `index` lines
> -			 * because they contain exact blob hashes in which
> -			 * we are not interested.
Oh, are we interested in them again?
Show 24 quoted lines
>  			 */
>  			continue;
>  		else if (line[0] == '>') {
> diff --git a/t/t3206-range-diff.sh b/t/t3206-range-diff.sh
> index 9f89af7178..c277756057 100755
> --- a/t/t3206-range-diff.sh
> +++ b/t/t3206-range-diff.sh
> @@ -181,6 +181,85 @@ test_expect_success 'changed commit with sm config' '
>  	test_cmp expected actual
>  '
>
> +test_expect_success 'renamed file' '
> +	git range-diff --no-color --submodule=log topic...renamed-file >actual &&
> +	sed s/Z/\ /g >expected <<-EOF &&
> +	1:  4de457d = 1:  f258d75 s/5/A/
> +	2:  fccce22 ! 2:  017b62d s/4/A/
> +	    @@
> +	    ZAuthor: Thomas Rast <trast@inf.ethz.ch>
> +	    Z
> +	    -    s/4/A/
> +	    +    s/4/A/ + rename file
> +	    Z
> +	    - ## file ##
> +	    + ## file => renamed-file ##

I guess there is no good way to suppress the `- ## file ##` line in this case? It is a bit distracting...

Show 92 quoted lines
> +	    Z@@
> +	    Z 1
> +	    Z 2
> +	3:  147e64e ! 3:  3ce7af6 s/11/B/
> +	    @@
> +	    Z
> +	    Z    s/11/B/
> +	    Z
> +	    - ## file ##
> +	    + ## renamed-file ##
> +	    Z@@ A
> +	    Z 8
> +	    Z 9
> +	4:  a63e992 ! 4:  1e6226b s/12/B/
> +	    @@
> +	    Z
> +	    Z    s/12/B/
> +	    Z
> +	    - ## file ##
> +	    + ## renamed-file ##
> +	    Z@@ A
> +	    Z 9
> +	    Z 10
> +	EOF
> +	test_cmp expected actual
> +'
> +
> +test_expect_success 'file added and later removed' '
> +	git range-diff --no-color --submodule=log topic...added-removed >actual &&
> +	sed s/Z/\ /g >expected <<-EOF &&
> +	1:  4de457d = 1:  096b1ba s/5/A/
> +	2:  fccce22 ! 2:  d92e698 s/4/A/
> +	    @@
> +	    ZAuthor: Thomas Rast <trast@inf.ethz.ch>
> +	    Z
> +	    -    s/4/A/
> +	    +    s/4/A/ + new-file
> +	    Z
> +	    Z ## file ##
> +	    Z@@
> +	    @@
> +	    Z A
> +	    Z 6
> +	    Z 7
> +	    +
> +	    + ## new-file (new) ##
> +	3:  147e64e ! 3:  9a1db4d s/11/B/
> +	    @@
> +	    ZAuthor: Thomas Rast <trast@inf.ethz.ch>
> +	    Z
> +	    -    s/11/B/
> +	    +    s/11/B/ + remove file
> +	    Z
> +	    Z ## file ##
> +	    Z@@ A
> +	    @@
> +	    Z 12
> +	    Z 13
> +	    Z 14
> +	    +
> +	    + ## new-file (deleted) ##
> +	4:  a63e992 = 4:  fea3b5c s/12/B/
> +	EOF
> +	test_cmp expected actual
> +'
> +
>  test_expect_success 'no commits on one side' '
>  	git commit --amend -m "new message" &&
>  	git range-diff master HEAD@{1} HEAD
> @@ -197,9 +276,9 @@ test_expect_success 'changed message' '
>  	    Z
>  	    +    Also a silly comment here!
>  	    +
> -	    Z diff --git a/file b/file
> -	    Z --- a/file
> -	    Z +++ b/file
> +	    Z ## file ##
> +	    Z@@
> +	    Z 1
>  	3:  147e64e = 3:  b9cb956 s/11/B/
>  	4:  a63e992 = 4:  8add5f1 s/12/B/
>  	EOF
> @@ -216,9 +295,9 @@ test_expect_success 'dual-coloring' '
>  	:     <RESET>
>  	:    <REVERSE><GREEN>+<RESET><BOLD>    Also a silly comment here!<RESET>
>  	:    <REVERSE><GREEN>+<RESET>
> -	:      diff --git a/file b/file<RESET>
> -	:      --- a/file<RESET>
> -	:      +++ b/file<RESET>
> +	:      ## file ##<RESET>
> +	:    <CYAN> @@<RESET>
> +	:      1<RESET>

I am a bit confused where these last two lines come from all of a sudden... They were not there before, and I do not see any code change in this patch that would be responsible for them, either...

Could you help me understand?
Show 15 quoted lines
>  	:<RED>3:  0559556 <RESET><YELLOW>!<RESET><GREEN> 3:  b9cb956<RESET><YELLOW> s/11/B/<RESET>
>  	:    <REVERSE><CYAN>@@<RESET>
>  	:      9<RESET>
> diff --git a/t/t3206/history.export b/t/t3206/history.export
> index b8ffff0940..7bb3814962 100644
> --- a/t/t3206/history.export
> +++ b/t/t3206/history.export
> @@ -22,8 +22,8 @@ data 51
>  19
>  20
>
> -reset refs/heads/removed
> -commit refs/heads/removed
> +reset refs/heads/renamed-file
> +commit refs/heads/renamed-file

Hmm. Is the `removed` ref no longer required by the 'removed a commit' test case?

Show 13 quoted lines
>  mark :2
>  author Thomas Rast <trast@inf.ethz.ch> 1374424921 +0200
>  committer Thomas Rast <trast@inf.ethz.ch> 1374484724 +0200
> @@ -599,6 +599,82 @@ s/12/B/
>  from :46
>  M 100644 :28 file
>
> -reset refs/heads/removed
> -from :47
> +commit refs/heads/added-removed
> +mark :48
> +author Thomas Rast <trast@inf.ethz.ch> 1374485014 +0200
> +committer Thomas Gummerer <t.gummerer@gmail.com> 1556574151 +0100
Neat ;-)
Show 74 quoted lines
> +data 7
> +s/5/A/
> +from :2
> +M 100644 :3 file
> +
> +blob
> +mark :49
> +data 0
> +
> +commit refs/heads/added-removed
> +mark :50
> +author Thomas Rast <trast@inf.ethz.ch> 1374485024 +0200
> +committer Thomas Gummerer <t.gummerer@gmail.com> 1556574177 +0100
> +data 18
> +s/4/A/ + new-file
> +from :48
> +M 100644 :5 file
> +M 100644 :49 new-file
> +
> +commit refs/heads/added-removed
> +mark :51
> +author Thomas Rast <trast@inf.ethz.ch> 1374485036 +0200
> +committer Thomas Gummerer <t.gummerer@gmail.com> 1556574177 +0100
> +data 22
> +s/11/B/ + remove file
> +from :50
> +M 100644 :7 file
> +D new-file
> +
> +commit refs/heads/added-removed
> +mark :52
> +author Thomas Rast <trast@inf.ethz.ch> 1374485044 +0200
> +committer Thomas Gummerer <t.gummerer@gmail.com> 1556574177 +0100
> +data 8
> +s/12/B/
> +from :51
> +M 100644 :9 file
> +
> +commit refs/heads/renamed-file
> +mark :53
> +author Thomas Rast <trast@inf.ethz.ch> 1374485014 +0200
> +committer Thomas Gummerer <t.gummerer@gmail.com> 1556574309 +0100
> +data 7
> +s/5/A/
> +from :2
> +M 100644 :3 file
> +
> +commit refs/heads/renamed-file
> +mark :54
> +author Thomas Rast <trast@inf.ethz.ch> 1374485024 +0200
> +committer Thomas Gummerer <t.gummerer@gmail.com> 1556574312 +0100
> +data 21
> +s/4/A/ + rename file
> +from :53
> +D file
> +M 100644 :5 renamed-file
> +
> +commit refs/heads/renamed-file
> +mark :55
> +author Thomas Rast <trast@inf.ethz.ch> 1374485036 +0200
> +committer Thomas Gummerer <t.gummerer@gmail.com> 1556574319 +0100
> +data 8
> +s/11/B/
> +from :54
> +M 100644 :7 renamed-file
> +
> +commit refs/heads/renamed-file
> +mark :56
> +author Thomas Rast <trast@inf.ethz.ch> 1374485044 +0200
> +committer Thomas Gummerer <t.gummerer@gmail.com> 1556574319 +0100
> +data 8
> +s/12/B/
> +from :55
> +M 100644 :9 renamed-file

I have to admit that I allowed myself not to study this script too closely, trusting that the range-diff explains better what commit history it creates than the fast-import script.

Thanks, Dscho

Show 5 quoted lines
>
> --
> 2.22.0.510.g264f2c817a
>
>
Thomas Gummerer· Jul 8, 2019, 11:44 UTC · re: Johannes Schindelin · lore

Re: [PATCH v2 12/14] range-diff: add section header instead of diff header

On 07/05, Johannes Schindelin wrote:
Show 25 quoted lines
> >  range-diff.c           | 35 ++++++++++++----
> >  t/t3206-range-diff.sh  | 91 +++++++++++++++++++++++++++++++++++++++---
> >  t/t3206/history.export | 84 ++++++++++++++++++++++++++++++++++++--
> >  3 files changed, 193 insertions(+), 17 deletions(-)
> >
> > diff --git a/range-diff.c b/range-diff.c
> > index b31fbab026..cc01f7f573 100644
> > --- a/range-diff.c
> > +++ b/range-diff.c
> > @@ -10,6 +10,7 @@
> >  #include "commit.h"
> >  #include "pretty.h"
> >  #include "userdiff.h"
> > +#include "apply.h"
> >
> >  struct patch_util {
> >  	/* For the search for an exact match */
> > @@ -95,12 +96,36 @@ static int read_patches(const char *range, struct string_list *list)
> >  		}
> >
> >  		if (starts_with(line, "diff --git")) {
> > +			struct patch patch;
> 
> If you append ` = { 0 }` (or ` = { NULL }`, depending on the first field
> of that struct, you don't need that `memset()` later.
Thanks!
Show 13 quoted lines
> > +			struct strbuf root = STRBUF_INIT;
> > +			int linenr = 0;
> > +
> >  			in_header = 0;
> >  			strbuf_addch(&buf, '\n');
> >  			if (!util->diff_offset)
> >  				util->diff_offset = buf.len;
> > -			strbuf_addch(&buf, ' ');
> > -			strbuf_addstr(&buf, line);
> > +			memset(&patch, 0, sizeof(patch));
> > +			line[len - 1] = '\n';
> 
> I guess `parse_git_header()` cannot handle lines ending in a NUL?

Yeah, it doesn't deal well with them. We might be able to change that, but I haven't looked into that tbh.

Show 6 quoted lines
> > +			len = parse_git_header(&root, &linenr, 1, line,
> > +					       len, size, &patch);
> > +			if (len < 0)
> > +				die(_("could not parse git header"));
> 
> Maybe include the line's contents, like ` '%.*s'", (int)len, line`?
Will do.
Show 18 quoted lines
> > @@ -117,17 +142,13 @@ static int read_patches(const char *range, struct string_list *list)
> >  			if (!(p = strstr(p, "@@")))
> >  				die(_("invalid hunk header in inner diff"));
> >  			strbuf_addstr(&buf, p);
> > -		} else if (!line[0] || starts_with(line, "index "))
> > +		} else if (!line[0])
> >  			/*
> >  			 * A completely blank (not ' \n', which is context)
> >  			 * line is not valid in a diff.  We skip it
> >  			 * silently, because this neatly handles the blank
> >  			 * separator line between commits in git-log
> >  			 * output.
> > -			 *
> > -			 * We also want to ignore the diff's `index` lines
> > -			 * because they contain exact blob hashes in which
> > -			 * we are not interested.
> 
> Oh, are we interested in them again?

Not really, but we no longer need to ignore them explicitly here. The 'index' lines are just another part of the diff header, which we parse using 'parse_git_diff_header()' now, and then replace completely.

Previously we singled that line from the git diff headers out, because they would result in a particularly noisy range diff, as the blob hashes would change in a lot of cases, but are not really interesting. We could have ignored things such as the "similarity index" as well, as for example a 1% change in the similarity wouldn't matter that much, but I assume we just didn't because that's much less likely to change between different versions of a patch series.

Show 27 quoted lines
> >  			 */
> >  			continue;
> >  		else if (line[0] == '>') {
> > diff --git a/t/t3206-range-diff.sh b/t/t3206-range-diff.sh
> > index 9f89af7178..c277756057 100755
> > --- a/t/t3206-range-diff.sh
> > +++ b/t/t3206-range-diff.sh
> > @@ -181,6 +181,85 @@ test_expect_success 'changed commit with sm config' '
> >  	test_cmp expected actual
> >  '
> >
> > +test_expect_success 'renamed file' '
> > +	git range-diff --no-color --submodule=log topic...renamed-file >actual &&
> > +	sed s/Z/\ /g >expected <<-EOF &&
> > +	1:  4de457d = 1:  f258d75 s/5/A/
> > +	2:  fccce22 ! 2:  017b62d s/4/A/
> > +	    @@
> > +	    ZAuthor: Thomas Rast <trast@inf.ethz.ch>
> > +	    Z
> > +	    -    s/4/A/
> > +	    +    s/4/A/ + rename file
> > +	    Z
> > +	    - ## file ##
> > +	    + ## file => renamed-file ##
> 
> I guess there is no good way to suppress the `- ## file ##` line in this
> case? It is a bit distracting...

No, I can't think of a good way. I'm also not sure it would be right to remove it. In this case it means that in the previous version this was only called 'file', while in the new version it was renamend in this patch to 'renamed-file', so it does give some useful information.

Not sure how else we could represent that. If we just had a '## file => renamed-file ##' section header, I'd expect that 'file' has been renamed to 'renamed-file' in both versions.

Show 16 quoted lines
> > @@ -216,9 +295,9 @@ test_expect_success 'dual-coloring' '
> >  	:     <RESET>
> >  	:    <REVERSE><GREEN>+<RESET><BOLD>    Also a silly comment here!<RESET>
> >  	:    <REVERSE><GREEN>+<RESET>
> > -	:      diff --git a/file b/file<RESET>
> > -	:      --- a/file<RESET>
> > -	:      +++ b/file<RESET>
> > +	:      ## file ##<RESET>
> > +	:    <CYAN> @@<RESET>
> > +	:      1<RESET>
> 
> I am a bit confused where these last two lines come from all of a
> sudden... They were not there before, and I do not see any code change in
> this patch that would be responsible for them, either...
> 
> Could you help me understand?

Sure. The actual change (in the range-diff) here is that "Also a silly comment here!" was added to the commit message. The diff header is context lines after that.

We now replace the diff header with the new "section header", which is only a single line, so we get a couple of additional lines of the context of the subsequent inner diff.

Show 18 quoted lines
> >  	:<RED>3:  0559556 <RESET><YELLOW>!<RESET><GREEN> 3:  b9cb956<RESET><YELLOW> s/11/B/<RESET>
> >  	:    <REVERSE><CYAN>@@<RESET>
> >  	:      9<RESET>
> > diff --git a/t/t3206/history.export b/t/t3206/history.export
> > index b8ffff0940..7bb3814962 100644
> > --- a/t/t3206/history.export
> > +++ b/t/t3206/history.export
> > @@ -22,8 +22,8 @@ data 51
> >  19
> >  20
> >
> > -reset refs/heads/removed
> > -commit refs/heads/removed
> > +reset refs/heads/renamed-file
> > +commit refs/heads/renamed-file
> 
> Hmm. Is the `removed` ref no longer required by the 'removed a commit'
> test case?

It is, and it still exists. I'm not entirely familar with the format for fast-export/fast-import scripts. What I did was just fast-import the existing script, create the new refs that were required for the tests and then fast-export'ed it again.

So not sure exactly why this changed, but the 'removed' ref still exists :)

Show 103 quoted lines
> >  mark :2
> >  author Thomas Rast <trast@inf.ethz.ch> 1374424921 +0200
> >  committer Thomas Rast <trast@inf.ethz.ch> 1374484724 +0200
> > @@ -599,6 +599,82 @@ s/12/B/
> >  from :46
> >  M 100644 :28 file
> >
> > -reset refs/heads/removed
> > -from :47
> > +commit refs/heads/added-removed
> > +mark :48
> > +author Thomas Rast <trast@inf.ethz.ch> 1374485014 +0200
> > +committer Thomas Gummerer <t.gummerer@gmail.com> 1556574151 +0100
> 
> Neat ;-)
> 
> > +data 7
> > +s/5/A/
> > +from :2
> > +M 100644 :3 file
> > +
> > +blob
> > +mark :49
> > +data 0
> > +
> > +commit refs/heads/added-removed
> > +mark :50
> > +author Thomas Rast <trast@inf.ethz.ch> 1374485024 +0200
> > +committer Thomas Gummerer <t.gummerer@gmail.com> 1556574177 +0100
> > +data 18
> > +s/4/A/ + new-file
> > +from :48
> > +M 100644 :5 file
> > +M 100644 :49 new-file
> > +
> > +commit refs/heads/added-removed
> > +mark :51
> > +author Thomas Rast <trast@inf.ethz.ch> 1374485036 +0200
> > +committer Thomas Gummerer <t.gummerer@gmail.com> 1556574177 +0100
> > +data 22
> > +s/11/B/ + remove file
> > +from :50
> > +M 100644 :7 file
> > +D new-file
> > +
> > +commit refs/heads/added-removed
> > +mark :52
> > +author Thomas Rast <trast@inf.ethz.ch> 1374485044 +0200
> > +committer Thomas Gummerer <t.gummerer@gmail.com> 1556574177 +0100
> > +data 8
> > +s/12/B/
> > +from :51
> > +M 100644 :9 file
> > +
> > +commit refs/heads/renamed-file
> > +mark :53
> > +author Thomas Rast <trast@inf.ethz.ch> 1374485014 +0200
> > +committer Thomas Gummerer <t.gummerer@gmail.com> 1556574309 +0100
> > +data 7
> > +s/5/A/
> > +from :2
> > +M 100644 :3 file
> > +
> > +commit refs/heads/renamed-file
> > +mark :54
> > +author Thomas Rast <trast@inf.ethz.ch> 1374485024 +0200
> > +committer Thomas Gummerer <t.gummerer@gmail.com> 1556574312 +0100
> > +data 21
> > +s/4/A/ + rename file
> > +from :53
> > +D file
> > +M 100644 :5 renamed-file
> > +
> > +commit refs/heads/renamed-file
> > +mark :55
> > +author Thomas Rast <trast@inf.ethz.ch> 1374485036 +0200
> > +committer Thomas Gummerer <t.gummerer@gmail.com> 1556574319 +0100
> > +data 8
> > +s/11/B/
> > +from :54
> > +M 100644 :7 renamed-file
> > +
> > +commit refs/heads/renamed-file
> > +mark :56
> > +author Thomas Rast <trast@inf.ethz.ch> 1374485044 +0200
> > +committer Thomas Gummerer <t.gummerer@gmail.com> 1556574319 +0100
> > +data 8
> > +s/12/B/
> > +from :55
> > +M 100644 :9 renamed-file
> 
> I have to admit that I allowed myself not to study this script too
> closely, trusting that the range-diff explains better what commit history
> it creates than the fast-import script.
> 
> Thanks,
> Dscho
> 
> >
> > --
> > 2.22.0.510.g264f2c817a
> >
> >
Johannes Schindelin· Jul 8, 2019, 13:12 UTC · re: Thomas Gummerer · lore

Re: [PATCH v2 12/14] range-diff: add section header instead of diff header

Hi Thomas,
On Mon, 8 Jul 2019, Thomas Gummerer wrote:
Show 34 quoted lines
> On 07/05, Johannes Schindelin wrote:
>
> > >  			 */
> > >  			continue;
> > >  		else if (line[0] == '>') {
> > > diff --git a/t/t3206-range-diff.sh b/t/t3206-range-diff.sh
> > > index 9f89af7178..c277756057 100755
> > > --- a/t/t3206-range-diff.sh
> > > +++ b/t/t3206-range-diff.sh
> > > @@ -181,6 +181,85 @@ test_expect_success 'changed commit with sm config' '
> > >  	test_cmp expected actual
> > >  '
> > >
> > > +test_expect_success 'renamed file' '
> > > +	git range-diff --no-color --submodule=log topic...renamed-file >actual &&
> > > +	sed s/Z/\ /g >expected <<-EOF &&
> > > +	1:  4de457d = 1:  f258d75 s/5/A/
> > > +	2:  fccce22 ! 2:  017b62d s/4/A/
> > > +	    @@
> > > +	    ZAuthor: Thomas Rast <trast@inf.ethz.ch>
> > > +	    Z
> > > +	    -    s/4/A/
> > > +	    +    s/4/A/ + rename file
> > > +	    Z
> > > +	    - ## file ##
> > > +	    + ## file => renamed-file ##
> >
> > I guess there is no good way to suppress the `- ## file ##` line in this
> > case? It is a bit distracting...
>
> No, I can't think of a good way.  I'm also not sure it would be right
> to remove it.  In this case it means that in the previous version this
> was only called 'file', while in the new version it was renamend in
> this patch to 'renamed-file', so it does give some useful information.

Oh, I misunderstood! You're right, this is useful information, and I just have to learn how to read that variant of the range-diffs.

In other words: please leave this part of your patch series as-is.
Show 24 quoted lines
> > > @@ -216,9 +295,9 @@ test_expect_success 'dual-coloring' '
> > >  	:     <RESET>
> > >  	:    <REVERSE><GREEN>+<RESET><BOLD>    Also a silly comment here!<RESET>
> > >  	:    <REVERSE><GREEN>+<RESET>
> > > -	:      diff --git a/file b/file<RESET>
> > > -	:      --- a/file<RESET>
> > > -	:      +++ b/file<RESET>
> > > +	:      ## file ##<RESET>
> > > +	:    <CYAN> @@<RESET>
> > > +	:      1<RESET>
> >
> > I am a bit confused where these last two lines come from all of a
> > sudden... They were not there before, and I do not see any code change in
> > this patch that would be responsible for them, either...
> >
> > Could you help me understand?
>
> Sure.  The actual change (in the range-diff) here is that "Also a
> silly comment here!" was added to the commit message.  The diff header
> is context lines after that.
>
> We now replace the diff header with the new "section header", which is
> only a single line, so we get a couple of additional lines of the
> context of the subsequent inner diff.

You know what? This is my typical mistake when reading uncolored range-diffs: _of course_ I missed that this is talking about context lines. I really thought they were added by the new iteration. My bad. And thanks for explaining this patiently to me.

Show 26 quoted lines
> > >  	:<RED>3:  0559556 <RESET><YELLOW>!<RESET><GREEN> 3:  b9cb956<RESET><YELLOW> s/11/B/<RESET>
> > >  	:    <REVERSE><CYAN>@@<RESET>
> > >  	:      9<RESET>
> > > diff --git a/t/t3206/history.export b/t/t3206/history.export
> > > index b8ffff0940..7bb3814962 100644
> > > --- a/t/t3206/history.export
> > > +++ b/t/t3206/history.export
> > > @@ -22,8 +22,8 @@ data 51
> > >  19
> > >  20
> > >
> > > -reset refs/heads/removed
> > > -commit refs/heads/removed
> > > +reset refs/heads/renamed-file
> > > +commit refs/heads/renamed-file
> >
> > Hmm. Is the `removed` ref no longer required by the 'removed a commit'
> > test case?
>
> It is, and it still exists.  I'm not entirely familar with the format
> for fast-export/fast-import scripts.  What I did was just fast-import
> the existing script, create the new refs that were required for the
> tests and then fast-export'ed it again.
>
> So not sure exactly why this changed, but the 'removed' ref still
> exists :)

Right, it would still exist because earlier parts of the script would have created that ref (otherwise `reset refs/heads/removed` would have failed).

Your strategy to update the script sounds like the best way to go, it just runs afoul of topological ordering that somehow makes this patch look as if you had removed parts of the commit history. I believe you when you say that you didn't, of course.

Thanks, Dscho

Thomas Gummerer· Jul 5, 2019, 17:06 UTC · re: Thomas Gummerer · lore

[PATCH v2 13/14] range-diff: add filename to inner diff

In a range-diff it's not always clear which file a certain funcname of the inner diff belongs to, because the diff header (or section header as added in a previous commit) is not always visible in the range-diff.

Add the filename to the inner diffs header, so it's always visible to users.

This also allows us to add the filename + the funcname to the outer diffs hunk headers using a custom userdiff pattern, which will be done in the next commit.

Signed-off-by: Thomas Gummerer <t.gummerer@gmail.com>
---
 range-diff.c          | 14 ++++++++++++--
 t/t3206-range-diff.sh | 16 ++++++++++------
 2 files changed, 22 insertions(+), 8 deletions(-)
Show changes to 2 files +22 −8

range-diff.c, t/t3206-range-diff.sh

diff --git a/range-diff.c b/range-diff.c
index cc01f7f573..09cb1ddbb1 100644
--- a/range-diff.c
+++ b/range-diff.c
@@ -46,7 +46,7 @@ static int read_patches(const char *range, struct string_list *list)
 	struct strbuf buf = STRBUF_INIT, file = STRBUF_INIT;
 	struct patch_util *util = NULL;
 	int in_header = 1;
-	char *line;
+	char *line, *current_filename = NULL;
 	int offset, len;
 	size_t size;
 
@@ -120,6 +120,12 @@ static int read_patches(const char *range, struct string_list *list)
 			else
 				strbuf_addstr(&buf, patch.new_name);
 
+			free(current_filename);
+			if (patch.is_delete > 0)
+				current_filename = xstrdup(patch.old_name);
+			else
+				current_filename = xstrdup(patch.new_name);
+
 			if (patch.new_mode && patch.old_mode &&
 			    patch.old_mode != patch.new_mode)
 				strbuf_addf(&buf, " (mode change %06o => %06o)",
@@ -141,7 +147,10 @@ static int read_patches(const char *range, struct string_list *list)
 		} else if (skip_prefix(line, "@@ ", &p)) {
 			if (!(p = strstr(p, "@@")))
 				die(_("invalid hunk header in inner diff"));
-			strbuf_addstr(&buf, p);
+			strbuf_addstr(&buf, "@@");
+			if (current_filename && p[2])
+				strbuf_addf(&buf, " %s:", current_filename);
+			strbuf_addstr(&buf, p + 2);
 		} else if (!line[0])
 			/*
 			 * A completely blank (not ' \n', which is context)
@@ -172,6 +181,7 @@ static int read_patches(const char *range, struct string_list *list)
 	if (util)
 		string_list_append(list, buf.buf)->util = util;
 	strbuf_release(&buf);
+	free(current_filename);
 
 	if (finish_command(&cp))
 		return -1;
diff --git a/t/t3206-range-diff.sh b/t/t3206-range-diff.sh
index c277756057..d4de270979 100755
--- a/t/t3206-range-diff.sh
+++ b/t/t3206-range-diff.sh
@@ -110,7 +110,7 @@ test_expect_success 'changed commit' '
 	      14
 	4:  a63e992 ! 4:  d966c5c s/12/B/
 	    @@
-	     @@ A
+	     @@ file: A
 	      9
 	      10
 	    - B
@@ -169,7 +169,7 @@ test_expect_success 'changed commit with sm config' '
 	      14
 	4:  a63e992 ! 4:  d966c5c s/12/B/
 	    @@
-	     @@ A
+	     @@ file: A
 	      9
 	      10
 	    - B
@@ -203,20 +203,24 @@ test_expect_success 'renamed file' '
 	    Z    s/11/B/
 	    Z
 	    - ## file ##
+	    -@@ file: A
 	    + ## renamed-file ##
-	    Z@@ A
+	    +@@ renamed-file: A
 	    Z 8
 	    Z 9
+	    Z 10
 	4:  a63e992 ! 4:  1e6226b s/12/B/
 	    @@
 	    Z
 	    Z    s/12/B/
 	    Z
 	    - ## file ##
+	    -@@ file: A
 	    + ## renamed-file ##
-	    Z@@ A
+	    +@@ renamed-file: A
 	    Z 9
 	    Z 10
+	    Z B
 	EOF
 	test_cmp expected actual
 '
@@ -248,7 +252,7 @@ test_expect_success 'file added and later removed' '
 	    +    s/11/B/ + remove file
 	    Z
 	    Z ## file ##
-	    Z@@ A
+	    Z@@ file: A
 	    @@
 	    Z 12
 	    Z 13
@@ -310,7 +314,7 @@ test_expect_success 'dual-coloring' '
 	:      14<RESET>
 	:<RED>4:  d966c5c <RESET><YELLOW>!<RESET><GREEN> 4:  8add5f1<RESET><YELLOW> s/12/B/<RESET>
 	:    <REVERSE><CYAN>@@<RESET>
-	:    <CYAN> @@ A<RESET>
+	:    <CYAN> @@ file: A<RESET>
 	:      9<RESET>
 	:      10<RESET>
 	:    <REVERSE><RED>-<RESET><FAINT> BB<RESET>
-- 
2.22.0.510.g264f2c817a
Thomas Gummerer· Jul 5, 2019, 17:06 UTC · re: Thomas Gummerer · lore

[PATCH v2 14/14] range-diff: add headers to the outer hunk header

Add the section headers/hunk headers we introduced in the previous commits to the outer diff's hunk headers. This makes it easier to understand which change we are actually looking at. For example an outer hunk header might now look like:

    @@  Documentation/config/interactive.txt
while previously it would have only been
    @@
which doesn't give a lot of context for the change that follows.

For completeness also add section headers for the commit metadata and the commit message, although they are arguably less important.

Signed-off-by: Thomas Gummerer <t.gummerer@gmail.com>
---
 range-diff.c          |  9 ++++++---
 t/t3206-range-diff.sh | 41 ++++++++++++++++++++++-------------------
 2 files changed, 28 insertions(+), 22 deletions(-)
Show changes to 2 files +28 −22

range-diff.c, t/t3206-range-diff.sh

diff --git a/range-diff.c b/range-diff.c
index 09cb1ddbb1..f43b229031 100644
--- a/range-diff.c
+++ b/range-diff.c
@@ -134,8 +134,10 @@ static int read_patches(const char *range, struct string_list *list)
 			strbuf_addstr(&buf, " ##");
 		} else if (in_header) {
 			if (starts_with(line, "Author: ")) {
+				strbuf_addstr(&buf, " ## Metadata ##\n");
 				strbuf_addstr(&buf, line);
 				strbuf_addstr(&buf, "\n\n");
+				strbuf_addstr(&buf, " ## Commit message ##\n");
 			} else if (starts_with(line, "    ")) {
 				p = line + len - 2;
 				while (isspace(*p) && p >= line)
@@ -396,8 +398,9 @@ static void output_pair_header(struct diff_options *diffopt,
 	fwrite(buf->buf, buf->len, 1, diffopt->file);
 }
 
-static struct userdiff_driver no_func_name = {
-	.funcname = { "$^", 0 }
+static struct userdiff_driver section_headers = {
+	.funcname = { "^ ## (.*) ##$\n"
+		      "^.?@@ (.*)$", REG_EXTENDED }
 };
 
 static struct diff_filespec *get_filespec(const char *name, const char *p)
@@ -409,7 +412,7 @@ static struct diff_filespec *get_filespec(const char *name, const char *p)
 	spec->size = strlen(p);
 	spec->should_munmap = 0;
 	spec->is_stdin = 1;
-	spec->driver = &no_func_name;
+	spec->driver = &section_headers;
 
 	return spec;
 }
diff --git a/t/t3206-range-diff.sh b/t/t3206-range-diff.sh
index d4de270979..ec548654ce 100755
--- a/t/t3206-range-diff.sh
+++ b/t/t3206-range-diff.sh
@@ -99,7 +99,7 @@ test_expect_success 'changed commit' '
 	1:  4de457d = 1:  a4b3333 s/5/A/
 	2:  fccce22 = 2:  f51d370 s/4/A/
 	3:  147e64e ! 3:  0559556 s/11/B/
-	    @@
+	    @@ file: A
 	      9
 	      10
 	     -11
@@ -109,7 +109,7 @@ test_expect_success 'changed commit' '
 	      13
 	      14
 	4:  a63e992 ! 4:  d966c5c s/12/B/
-	    @@
+	    @@ file
 	     @@ file: A
 	      9
 	      10
@@ -158,7 +158,7 @@ test_expect_success 'changed commit with sm config' '
 	1:  4de457d = 1:  a4b3333 s/5/A/
 	2:  fccce22 = 2:  f51d370 s/4/A/
 	3:  147e64e ! 3:  0559556 s/11/B/
-	    @@
+	    @@ file: A
 	      9
 	      10
 	     -11
@@ -168,7 +168,7 @@ test_expect_success 'changed commit with sm config' '
 	      13
 	      14
 	4:  a63e992 ! 4:  d966c5c s/12/B/
-	    @@
+	    @@ file
 	     @@ file: A
 	      9
 	      10
@@ -186,9 +186,10 @@ test_expect_success 'renamed file' '
 	sed s/Z/\ /g >expected <<-EOF &&
 	1:  4de457d = 1:  f258d75 s/5/A/
 	2:  fccce22 ! 2:  017b62d s/4/A/
-	    @@
+	    @@ Metadata
 	    ZAuthor: Thomas Rast <trast@inf.ethz.ch>
 	    Z
+	    Z ## Commit message ##
 	    -    s/4/A/
 	    +    s/4/A/ + rename file
 	    Z
@@ -198,8 +199,8 @@ test_expect_success 'renamed file' '
 	    Z 1
 	    Z 2
 	3:  147e64e ! 3:  3ce7af6 s/11/B/
-	    @@
-	    Z
+	    @@ Metadata
+	    Z ## Commit message ##
 	    Z    s/11/B/
 	    Z
 	    - ## file ##
@@ -210,8 +211,8 @@ test_expect_success 'renamed file' '
 	    Z 9
 	    Z 10
 	4:  a63e992 ! 4:  1e6226b s/12/B/
-	    @@
-	    Z
+	    @@ Metadata
+	    Z ## Commit message ##
 	    Z    s/12/B/
 	    Z
 	    - ## file ##
@@ -230,30 +231,32 @@ test_expect_success 'file added and later removed' '
 	sed s/Z/\ /g >expected <<-EOF &&
 	1:  4de457d = 1:  096b1ba s/5/A/
 	2:  fccce22 ! 2:  d92e698 s/4/A/
-	    @@
+	    @@ Metadata
 	    ZAuthor: Thomas Rast <trast@inf.ethz.ch>
 	    Z
+	    Z ## Commit message ##
 	    -    s/4/A/
 	    +    s/4/A/ + new-file
 	    Z
 	    Z ## file ##
 	    Z@@
-	    @@
+	    @@ file
 	    Z A
 	    Z 6
 	    Z 7
 	    +
 	    + ## new-file (new) ##
 	3:  147e64e ! 3:  9a1db4d s/11/B/
-	    @@
+	    @@ Metadata
 	    ZAuthor: Thomas Rast <trast@inf.ethz.ch>
 	    Z
+	    Z ## Commit message ##
 	    -    s/11/B/
 	    +    s/11/B/ + remove file
 	    Z
 	    Z ## file ##
 	    Z@@ file: A
-	    @@
+	    @@ file: A
 	    Z 12
 	    Z 13
 	    Z 14
@@ -274,8 +277,8 @@ test_expect_success 'changed message' '
 	sed s/Z/\ /g >expected <<-EOF &&
 	1:  4de457d = 1:  f686024 s/5/A/
 	2:  fccce22 ! 2:  4ab067d s/4/A/
-	    @@
-	    Z
+	    @@ Metadata
+	    Z ## Commit message ##
 	    Z    s/4/A/
 	    Z
 	    +    Also a silly comment here!
@@ -293,8 +296,8 @@ test_expect_success 'dual-coloring' '
 	sed -e "s|^:||" >expect <<-\EOF &&
 	:<YELLOW>1:  a4b3333 = 1:  f686024 s/5/A/<RESET>
 	:<RED>2:  f51d370 <RESET><YELLOW>!<RESET><GREEN> 2:  4ab067d<RESET><YELLOW> s/4/A/<RESET>
-	:    <REVERSE><CYAN>@@<RESET>
-	:     <RESET>
+	:    <REVERSE><CYAN>@@<RESET> <RESET>Metadata<RESET>
+	:      ## Commit message ##<RESET>
 	:         s/4/A/<RESET>
 	:     <RESET>
 	:    <REVERSE><GREEN>+<RESET><BOLD>    Also a silly comment here!<RESET>
@@ -303,7 +306,7 @@ test_expect_success 'dual-coloring' '
 	:    <CYAN> @@<RESET>
 	:      1<RESET>
 	:<RED>3:  0559556 <RESET><YELLOW>!<RESET><GREEN> 3:  b9cb956<RESET><YELLOW> s/11/B/<RESET>
-	:    <REVERSE><CYAN>@@<RESET>
+	:    <REVERSE><CYAN>@@<RESET> <RESET>file: A<RESET>
 	:      9<RESET>
 	:      10<RESET>
 	:    <RED> -11<RESET>
@@ -313,7 +316,7 @@ test_expect_success 'dual-coloring' '
 	:      13<RESET>
 	:      14<RESET>
 	:<RED>4:  d966c5c <RESET><YELLOW>!<RESET><GREEN> 4:  8add5f1<RESET><YELLOW> s/12/B/<RESET>
-	:    <REVERSE><CYAN>@@<RESET>
+	:    <REVERSE><CYAN>@@<RESET> <RESET>file<RESET>
 	:    <CYAN> @@ file: A<RESET>
 	:      9<RESET>
 	:      10<RESET>
-- 
2.22.0.510.g264f2c817a
Johannes Schindelin· Jul 5, 2019, 19:48 UTC · re: Thomas Gummerer · lore

Re: [PATCH v2 00/14] output improvements for git range-diff

Hi Thomas,
On Fri, 5 Jul 2019, Thomas Gummerer wrote:
Show 7 quoted lines
> It's been quite a while since I sent the RFC [1] (thanks all for the
> comments on that), and the series changed shapes quite a bit since the
> last round.
>
> Since it's been such a long time, just to remind everyone, the goal of
> this series is to make the range-diff output clearer, by showing
> information about the filenames to which the current diff belongs.
Thank you for that reminder ;-)
Show 9 quoted lines
> In the previous round, we did this using "section headers" that
> include information about the current file and adding that to the
> outer diff's hunk headers.
>
> In this round we still keep the section headers (with slightly more
> information), but in addition we also add the filename to the inner
> diff hunk headers.  In the outer diff hunk headers we then display
> either the section header or the inner diff hunk header using a
> userdiff pattern.
I like this idea!
Show 5 quoted lines
> In terms of code changes the biggest change is that we're now re-using
> the 'parse_git_header' function from the apply code to parse the diff
> headers, instead of trying to parse them with some hacky parsing code
> in range-diff.c.  This way we are sure that the diff headers are
> properly parsed.
Yep, very good.
> I had also considered just outputting the section headers directly
> from 'git log', but then decided against that.  Parsing the headers
> allows a future enhancement of range-diff, where we would allow
> parsing an mbox file [2].

Thanks you for your consideration; I still would like to have the option at some stage to compare a patch series from public-inbox.org/git to the commits in `pu`, without having to fiddle with finding a valid base commit to apply the patches on.

> I split the "only pass required data" commits up, in the hopes of
> making them easier to review, but I'm also happy to squash them if
> people feel like that makes it easier to review them.

I found it very easy to review in the current form, thank you for putting in the extra effort.

Show 16 quoted lines
> An added advantage of this is that we're also getting rid of things
> like the similarity index, which are not important in the range-diff,
> and are thus not represented in the "section header".
>
> One thing that did not change is that the new/deleted strings are not
> translated in this version either.  This is similar to the regular
> diff output, where we also don't translate these.  We can still
> consider translating them in the future though.
>
> [1]: https://public-inbox.org/git/20190414210933.20875-1-t.gummerer@gmail.com/
> [2]: https://github.com/gitgitgadget/git/issues/207
>
> I'm including the range-diff between this version of the series and
> the RFC, and a diff between the range diff and the range-diff without
> these changes below.  Probably not useful in reviewing the code, but
> good to show off the changes made in this series.
Indeed!

I very much like the idea, and the current iteration. I offered a couple of minor suggestions, in the hope that you find them helpful.

Thank you, Dscho

Thomas Gummerer· Jul 8, 2019, 11:45 UTC · re: Johannes Schindelin · lore

Re: [PATCH v2 00/14] output improvements for git range-diff

On 07/05, Johannes Schindelin wrote:
Show 44 quoted lines
> Hi Thomas,
> 
> On Fri, 5 Jul 2019, Thomas Gummerer wrote:
> 
> > It's been quite a while since I sent the RFC [1] (thanks all for the
> > comments on that), and the series changed shapes quite a bit since the
> > last round.
> >
> > Since it's been such a long time, just to remind everyone, the goal of
> > this series is to make the range-diff output clearer, by showing
> > information about the filenames to which the current diff belongs.
> 
> Thank you for that reminder ;-)
> 
> > In the previous round, we did this using "section headers" that
> > include information about the current file and adding that to the
> > outer diff's hunk headers.
> >
> > In this round we still keep the section headers (with slightly more
> > information), but in addition we also add the filename to the inner
> > diff hunk headers.  In the outer diff hunk headers we then display
> > either the section header or the inner diff hunk header using a
> > userdiff pattern.
> 
> 
> I like this idea!
> 
> > In terms of code changes the biggest change is that we're now re-using
> > the 'parse_git_header' function from the apply code to parse the diff
> > headers, instead of trying to parse them with some hacky parsing code
> > in range-diff.c.  This way we are sure that the diff headers are
> > properly parsed.
> 
> Yep, very good.
> 
> > I had also considered just outputting the section headers directly
> > from 'git log', but then decided against that.  Parsing the headers
> > allows a future enhancement of range-diff, where we would allow
> > parsing an mbox file [2].
> 
> Thanks you for your consideration; I still would like to have the option
> at some stage to compare a patch series from public-inbox.org/git to the
> commits in `pu`, without having to fiddle with finding a valid base commit
> to apply the patches on.
Yeah, I would like that as well ;)
Show 28 quoted lines
> > I split the "only pass required data" commits up, in the hopes of
> > making them easier to review, but I'm also happy to squash them if
> > people feel like that makes it easier to review them.
> 
> I found it very easy to review in the current form, thank you for putting
> in the extra effort.
> 
> > An added advantage of this is that we're also getting rid of things
> > like the similarity index, which are not important in the range-diff,
> > and are thus not represented in the "section header".
> >
> > One thing that did not change is that the new/deleted strings are not
> > translated in this version either.  This is similar to the regular
> > diff output, where we also don't translate these.  We can still
> > consider translating them in the future though.
> >
> > [1]: https://public-inbox.org/git/20190414210933.20875-1-t.gummerer@gmail.com/
> > [2]: https://github.com/gitgitgadget/git/issues/207
> >
> > I'm including the range-diff between this version of the series and
> > the RFC, and a diff between the range diff and the range-diff without
> > these changes below.  Probably not useful in reviewing the code, but
> > good to show off the changes made in this series.
> 
> Indeed!
> 
> I very much like the idea, and the current iteration. I offered a couple
> of minor suggestions, in the hope that you find them helpful.

Thanks for your review! I did find the suggestions very helpful indeed :)

Thomas Gummerer· Jul 8, 2019, 16:33 UTC · re: Thomas Gummerer · lore

[PATCH v3 00/14] output improvements for git range-diff

Thanks Dscho for the review of the previous round [1]. This rounds addresses all the comments from that review.

In particular
- update commit messages where necessary.
- rename the function in apply.c to 'parse_git_diff_header()'
- code cleanups in 09/14
- fix a memory leak introduced in 09/14
- be less strict about parsing hunk headers, so the new code isn't
  more strict than it was before
- give more information when we are unable to parse the git diff
  header
[1]: https://public-inbox.org/git/20190705170630.27500-1-t.gummerer@gmail.com/
Thomas Gummerer (14):
  apply: replace marc.info link with public-inbox
  apply: only pass required data to skip_tree_prefix
  apply: only pass required data to git_header_name
  apply: only pass required data to check_header_line
  apply: only pass required data to find_name_*
  apply: only pass required data to gitdiff_* functions
  apply: make parse_git_header public
  range-diff: fix function parameter indentation
  range-diff: split lines manually
  range-diff: don't remove funcname from inner diff
  range-diff: suppress line count in outer diff
  range-diff: add section header instead of diff header
  range-diff: add filename to inner diff
  range-diff: add headers to the outer hunk header
 apply.c                | 186 ++++++++++++++++++-----------------------
 apply.h                |  48 +++++++++++
 diff.c                 |   5 +-
 diff.h                 |   1 +
 range-diff.c           | 124 +++++++++++++++++++--------
 t/t3206-range-diff.sh  | 124 ++++++++++++++++++++++-----
 t/t3206/history.export |  84 ++++++++++++++++++-
 7 files changed, 409 insertions(+), 163 deletions(-)
Range-diff against v2:
 1:  ef2245edda =  1:  ef2245edda apply: replace marc.info link with public-inbox
 2:  94578fa45c =  2:  94578fa45c apply: only pass required data to skip_tree_prefix
 3:  988269a68e =  3:  988269a68e apply: only pass required data to git_header_name
 4:  a2c1ef3f5f =  4:  a2c1ef3f5f apply: only pass required data to check_header_line
 5:  0f4cfe21cb =  5:  0f4cfe21cb apply: only pass required data to find_name_*
 6:  7f1d7a4569 !  6:  07a271518d apply: only pass required data to gitdiff_* functions
    @@ Commit message
         we want functions in the callchain of 'parse_git_header()' to only
         take arguments they really need.
     
    +    As these functions are called in a loop using their function pointers,
    +    each function needs to be passed all the parameters even if only one
    +    of the functions actually needs it.  We therefore pass this data along
    +    in a struct to avoid adding too many unused parameters to each
    +    function and making the code very verbose in the process.
    +
         Signed-off-by: Thomas Gummerer <t.gummerer@gmail.com>
     
      ## apply.c ##
 7:  a5af8b0845 !  7:  9cb6732a5f apply: make parse_git_header public
    @@ apply.c: struct fragment {
      {
      	while (list) {
     @@ apply.c: static int check_header_line(int linenr, struct patch *patch)
    + 	return 0;
      }
      
    - /* Verify that we recognize the lines following a git header */
    +-/* Verify that we recognize the lines following a git header */
     -static int parse_git_header(struct apply_state *state,
     -			    const char *line,
     -			    int len,
     -			    unsigned int size,
     -			    struct patch *patch)
    -+int parse_git_header(struct strbuf *root,
    -+		     int *linenr,
    -+		     int p_value,
    -+		     const char *line,
    -+		     int len,
    -+		     unsigned int size,
    -+		     struct patch *patch)
    ++int parse_git_diff_header(struct strbuf *root,
    ++			  int *linenr,
    ++			  int p_value,
    ++			  const char *line,
    ++			  int len,
    ++			  unsigned int size,
    ++			  struct patch *patch)
      {
      	unsigned long offset;
      	struct parse_git_header_state parse_hdr_state;
    @@ apply.c: static int find_header(struct apply_state *state,
      		 */
      		if (!memcmp("diff --git ", line, 11)) {
     -			int git_hdr_len = parse_git_header(state, line, len, size, patch);
    -+			int git_hdr_len = parse_git_header(&state->root, &state->linenr,
    -+							   state->p_value, line, len,
    -+							   size, patch);
    ++			int git_hdr_len = parse_git_diff_header(&state->root, &state->linenr,
    ++								state->p_value, line, len,
    ++								size, patch);
      			if (git_hdr_len < 0)
      				return -128;
      			if (git_hdr_len <= len)
    @@ apply.h: int init_apply_state(struct apply_state *state,
      int check_apply_state(struct apply_state *state, int force_apply);
      
     +/*
    -+ * Parse a get header, starting at line.  Fills the relevant metadata
    -+ * information in 'struct patch'.
    ++ * Parse a git diff header, starting at line.  Fills the relevant
    ++ * metadata information in 'struct patch'.
     + *
     + * Returns -1 on failure, the length of the parsed header otherwise.
     + */
    -+int parse_git_header(struct strbuf *root,
    -+		     int *linenr,
    -+		     int p_value,
    -+		     const char *line,
    -+		     int len,
    -+		     unsigned int size,
    -+		     struct patch *patch);
    ++int parse_git_diff_header(struct strbuf *root,
    ++			  int *linenr,
    ++			  int p_value,
    ++			  const char *line,
    ++			  int len,
    ++			  unsigned int size,
    ++			  struct patch *patch);
     +
      /*
       * Some aspects of the apply behavior are controlled by the following
 8:  1f25bb1002 =  8:  76a11ce995 range-diff: fix function parameter indentation
 9:  01ed0f2a6a !  9:  6f70e7faa6 range-diff: split lines manually
    @@ Commit message
     
         Currently range-diff uses the 'strbuf_getline()' function for doing
         its line by line processing.  In a future patch we want to do parts of
    -    that parsing using the 'parse_git_header()' function, which does
    -    requires reading parts of the input from that function, which doesn't
    -    use strbufs.
    +    that parsing using the 'parse_git_diff_header()' function.  That
    +    function does its own line by line reading of the input, and doesn't
    +    use strbufs.  This doesn't match with how we do the line-by-line
    +    processing in range-diff currently.
     
         Switch range-diff to do our own line by line parsing, so we can re-use
    -    the parse_git_header function later.
    +    the 'parse_git_diff_header()' function later.
     
         Signed-off-by: Thomas Gummerer <t.gummerer@gmail.com>
     
    @@ range-diff.c: struct patch_util {
      	struct object_id oid;
      };
      
    -+static unsigned long linelen(const char *buffer, unsigned long size)
    ++static size_t find_end_of_line(char *buffer, unsigned long size)
     +{
    -+	unsigned long len = 0;
    -+	while (size--) {
    -+		len++;
    -+		if (*buffer++ == '\n')
    -+			break;
    -+	}
    -+	return len;
    ++	char *eol = memchr(buffer, '\n', size);
    ++
    ++	if (!eol)
    ++		return size;
    ++
    ++	*eol = '\0';
    ++	return eol + 1 - buffer;
     +}
     +
      /*
    @@ range-diff.c: struct patch_util {
      	struct child_process cp = CHILD_PROCESS_INIT;
     -	FILE *in;
     -	struct strbuf buf = STRBUF_INIT, line = STRBUF_INIT;
    -+	struct strbuf buf = STRBUF_INIT, file = STRBUF_INIT;
    ++	struct strbuf buf = STRBUF_INIT, contents = STRBUF_INIT;
      	struct patch_util *util = NULL;
      	int in_header = 1;
     +	char *line;
    @@ range-diff.c: static int read_patches(const char *range, struct string_list *lis
      		return error_errno(_("could not start `log`"));
     -	in = fdopen(cp.out, "r");
     -	if (!in) {
    --		error_errno(_("could not read `log` output"));
    --		finish_command(&cp);
    --		return -1;
    --	}
    -+	strbuf_read(&file, cp.out, 0);
    ++	if (strbuf_read(&contents, cp.out, 0) < 0) {
    + 		error_errno(_("could not read `log` output"));
    + 		finish_command(&cp);
    + 		return -1;
    + 	}
      
     -	while (strbuf_getline(&line, in) != EOF) {
    -+	line = strbuf_detach(&file, &size);
    ++	line = contents.buf;
    ++	size = contents.len;
     +	for (offset = 0; size > 0; offset += len, size -= len, line += len) {
      		const char *p;
      
     -		if (skip_prefix(line.buf, "commit ", &p)) {
    -+		len = linelen(line, size);
    ++		len = find_end_of_line(line, size);
     +		line[len - 1] = '\0';
     +		if (skip_prefix(line, "commit ", &p)) {
      			if (util) {
    @@ range-diff.c: static int read_patches(const char *range, struct string_list *lis
      				strbuf_release(&buf);
     -				strbuf_release(&line);
     -				fclose(in);
    ++				strbuf_release(&contents);
      				finish_command(&cp);
      				return -1;
      			}
    @@ range-diff.c: static int read_patches(const char *range, struct string_list *lis
      	}
     -	fclose(in);
     -	strbuf_release(&line);
    ++	strbuf_release(&contents);
      
      	if (util)
      		string_list_append(list, buf.buf)->util = util;
10:  044a79868b ! 10:  6618cdff2c range-diff: don't remove funcname from inner diff
    @@ range-diff.c: static int read_patches(const char *range, struct string_list *lis
     -			strbuf_addstr(&buf, "@@");
     -		else if (!line[0] || starts_with(line, "index "))
     +		} else if (skip_prefix(line, "@@ ", &p)) {
    -+			if (!(p = strstr(p, "@@")))
    -+				die(_("invalid hunk header in inner diff"));
    -+			strbuf_addstr(&buf, p);
    ++			p = strstr(p, "@@");
    ++			strbuf_addstr(&buf, p ? p : "@@");
     +		} else if (!line[0] || starts_with(line, "index "))
      			/*
      			 * A completely blank (not ' \n', which is context)
11:  69654fe76d = 11:  2667df4fa5 range-diff: suppress line count in outer diff
12:  c38f929b9a ! 12:  47cd8c6733 range-diff: add section header instead of diff header
    @@ Commit message
         noisy.  However the diff of a single line is concise and should be
         easier to understand.
     
    -    Additionaly, this allows us to add these range diff section headers to
    +    Additionally, this allows us to add these range diff section headers to
         the outer diffs hunk headers using a custom userdiff pattern, which
         should help making the range-diff more readable.
     
    @@ range-diff.c: static int read_patches(const char *range, struct string_list *lis
      		}
      
      		if (starts_with(line, "diff --git")) {
    -+			struct patch patch;
    ++			struct patch patch = { 0 };
     +			struct strbuf root = STRBUF_INIT;
     +			int linenr = 0;
     +
    @@ range-diff.c: static int read_patches(const char *range, struct string_list *lis
      				util->diff_offset = buf.len;
     -			strbuf_addch(&buf, ' ');
     -			strbuf_addstr(&buf, line);
    -+			memset(&patch, 0, sizeof(patch));
     +			line[len - 1] = '\n';
    -+			len = parse_git_header(&root, &linenr, 1, line,
    -+					       len, size, &patch);
    ++			len = parse_git_diff_header(&root, &linenr, 1, line,
    ++						    len, size, &patch);
     +			if (len < 0)
    -+				die(_("could not parse git header"));
    ++				die(_("could not parse git header '%.*s'"), (int)len, line);
     +			strbuf_addstr(&buf, " ## ");
     +			if (patch.is_new > 0)
     +				strbuf_addf(&buf, "%s (new)", patch.new_name);
    @@ range-diff.c: static int read_patches(const char *range, struct string_list *lis
      			if (starts_with(line, "Author: ")) {
      				strbuf_addstr(&buf, line);
     @@ range-diff.c: static int read_patches(const char *range, struct string_list *list)
    - 			if (!(p = strstr(p, "@@")))
    - 				die(_("invalid hunk header in inner diff"));
    - 			strbuf_addstr(&buf, p);
    + 		} else if (skip_prefix(line, "@@ ", &p)) {
    + 			p = strstr(p, "@@");
    + 			strbuf_addstr(&buf, p ? p : "@@");
     -		} else if (!line[0] || starts_with(line, "index "))
     +		} else if (!line[0])
      			/*
13:  6df03ecdcf ! 13:  f67fd5dd9a range-diff: add filename to inner diff
    @@ Commit message
     
      ## range-diff.c ##
     @@ range-diff.c: static int read_patches(const char *range, struct string_list *list)
    - 	struct strbuf buf = STRBUF_INIT, file = STRBUF_INIT;
    + 	struct strbuf buf = STRBUF_INIT, contents = STRBUF_INIT;
      	struct patch_util *util = NULL;
      	int in_header = 1;
     -	char *line;
    @@ range-diff.c: static int read_patches(const char *range, struct string_list *lis
      			    patch.old_mode != patch.new_mode)
      				strbuf_addf(&buf, " (mode change %06o => %06o)",
     @@ range-diff.c: static int read_patches(const char *range, struct string_list *list)
    + 			continue;
      		} else if (skip_prefix(line, "@@ ", &p)) {
    - 			if (!(p = strstr(p, "@@")))
    - 				die(_("invalid hunk header in inner diff"));
    --			strbuf_addstr(&buf, p);
    + 			p = strstr(p, "@@");
    +-			strbuf_addstr(&buf, p ? p : "@@");
     +			strbuf_addstr(&buf, "@@");
     +			if (current_filename && p[2])
     +				strbuf_addf(&buf, " %s:", current_filename);
    -+			strbuf_addstr(&buf, p + 2);
    ++			if (p)
    ++				strbuf_addstr(&buf, p + 2);
      		} else if (!line[0])
      			/*
      			 * A completely blank (not ' \n', which is context)
14:  5ceef49035 = 14:  812893a5dc range-diff: add headers to the outer hunk header
-- 
2.22.0.510.g264f2c817a
Thomas Gummerer· Jul 8, 2019, 16:33 UTC · re: Thomas Gummerer · lore

[PATCH v3 01/14] apply: replace marc.info link with public-inbox

public-inbox.org links include the whole message ID by default. This means the message can still be found even if the site goes away, which is not the case with the marc.info link. Replace the marc.info link with a more future proof one.

Signed-off-by: Thomas Gummerer <t.gummerer@gmail.com>
---
 apply.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
Show changes to apply.c +1 −1
diff --git a/apply.c b/apply.c
index f15afa9f6a..599cf8956f 100644
--- a/apply.c
+++ b/apply.c
@@ -478,7 +478,7 @@ static char *find_name_gnu(struct apply_state *state,
 
 	/*
 	 * Proposed "new-style" GNU patch/diff format; see
-	 * http://marc.info/?l=git&m=112927316408690&w=2
+	 * https://public-inbox.org/git/7vll0wvb2a.fsf@assigned-by-dhcp.cox.net/
 	 */
 	if (unquote_c_style(&name, line, NULL)) {
 		strbuf_release(&name);
-- 
2.22.0.510.g264f2c817a
Thomas Gummerer· Jul 8, 2019, 16:33 UTC · re: Thomas Gummerer · lore

[PATCH v3 02/14] apply: only pass required data to skip_tree_prefix

Currently the 'skip_tree_prefix()' function takes 'struct apply_state' as parameter, even though it only needs the p_value from that struct.

This function is in the callchain of 'parse_git_header()', which we want to make more generally useful in a subsequent commit. To make that happen we only want to pass in the required data to 'parse_git_header()', and not the whole 'struct apply_state', and thus we want functions in the callchain of 'parse_git_header()' to only take arguments they really need.

Signed-off-by: Thomas Gummerer <t.gummerer@gmail.com>
---
 apply.c | 18 +++++++++---------
 1 file changed, 9 insertions(+), 9 deletions(-)
Show changes to apply.c +9 −9
diff --git a/apply.c b/apply.c
index 599cf8956f..fc7083fcbc 100644
--- a/apply.c
+++ b/apply.c
@@ -1137,17 +1137,17 @@ static int gitdiff_unrecognized(struct apply_state *state,
  * Skip p_value leading components from "line"; as we do not accept
  * absolute paths, return NULL in that case.
  */
-static const char *skip_tree_prefix(struct apply_state *state,
+static const char *skip_tree_prefix(int p_value,
 				    const char *line,
 				    int llen)
 {
 	int nslash;
 	int i;
 
-	if (!state->p_value)
+	if (!p_value)
 		return (llen && line[0] == '/') ? NULL : line;
 
-	nslash = state->p_value;
+	nslash = p_value;
 	for (i = 0; i < llen; i++) {
 		int ch = line[i];
 		if (ch == '/' && --nslash <= 0)
@@ -1184,7 +1184,7 @@ static char *git_header_name(struct apply_state *state,
 			goto free_and_fail1;
 
 		/* strip the a/b prefix including trailing slash */
-		cp = skip_tree_prefix(state, first.buf, first.len);
+		cp = skip_tree_prefix(state->p_value, first.buf, first.len);
 		if (!cp)
 			goto free_and_fail1;
 		strbuf_remove(&first, 0, cp - first.buf);
@@ -1201,7 +1201,7 @@ static char *git_header_name(struct apply_state *state,
 		if (*second == '"') {
 			if (unquote_c_style(&sp, second, NULL))
 				goto free_and_fail1;
-			cp = skip_tree_prefix(state, sp.buf, sp.len);
+			cp = skip_tree_prefix(state->p_value, sp.buf, sp.len);
 			if (!cp)
 				goto free_and_fail1;
 			/* They must match, otherwise ignore */
@@ -1212,7 +1212,7 @@ static char *git_header_name(struct apply_state *state,
 		}
 
 		/* unquoted second */
-		cp = skip_tree_prefix(state, second, line + llen - second);
+		cp = skip_tree_prefix(state->p_value, second, line + llen - second);
 		if (!cp)
 			goto free_and_fail1;
 		if (line + llen - cp != first.len ||
@@ -1227,7 +1227,7 @@ static char *git_header_name(struct apply_state *state,
 	}
 
 	/* unquoted first name */
-	name = skip_tree_prefix(state, line, llen);
+	name = skip_tree_prefix(state->p_value, line, llen);
 	if (!name)
 		return NULL;
 
@@ -1243,7 +1243,7 @@ static char *git_header_name(struct apply_state *state,
 			if (unquote_c_style(&sp, second, NULL))
 				goto free_and_fail2;
 
-			np = skip_tree_prefix(state, sp.buf, sp.len);
+			np = skip_tree_prefix(state->p_value, sp.buf, sp.len);
 			if (!np)
 				goto free_and_fail2;
 
@@ -1287,7 +1287,7 @@ static char *git_header_name(struct apply_state *state,
 			 */
 			if (!name[len + 1])
 				return NULL; /* no postimage name */
-			second = skip_tree_prefix(state, name + len + 1,
+			second = skip_tree_prefix(state->p_value, name + len + 1,
 						  line_len - (len + 1));
 			if (!second)
 				return NULL;
-- 
2.22.0.510.g264f2c817a
Thomas Gummerer· Jul 8, 2019, 16:33 UTC · re: Thomas Gummerer · lore

[PATCH v3 03/14] apply: only pass required data to git_header_name

Currently the 'git_header_name()' function takes 'struct apply_state' as parameter, even though it only needs the p_value from that struct.

This function is in the callchain of 'parse_git_header()', which we want to make more generally useful in a subsequent commit. To make that happen we only want to pass in the required data to 'parse_git_header()', and not the whole 'struct apply_state', and thus we want functions in the callchain of 'parse_git_header()' to only take arguments they really need.

Signed-off-by: Thomas Gummerer <t.gummerer@gmail.com>
---
 apply.c | 16 ++++++++--------
 1 file changed, 8 insertions(+), 8 deletions(-)
Show changes to apply.c +8 −8
diff --git a/apply.c b/apply.c
index fc7083fcbc..ac668e754d 100644
--- a/apply.c
+++ b/apply.c
@@ -1164,7 +1164,7 @@ static const char *skip_tree_prefix(int p_value,
  * creation or deletion of an empty file.  In any of these cases,
  * both sides are the same name under a/ and b/ respectively.
  */
-static char *git_header_name(struct apply_state *state,
+static char *git_header_name(int p_value,
 			     const char *line,
 			     int llen)
 {
@@ -1184,7 +1184,7 @@ static char *git_header_name(struct apply_state *state,
 			goto free_and_fail1;
 
 		/* strip the a/b prefix including trailing slash */
-		cp = skip_tree_prefix(state->p_value, first.buf, first.len);
+		cp = skip_tree_prefix(p_value, first.buf, first.len);
 		if (!cp)
 			goto free_and_fail1;
 		strbuf_remove(&first, 0, cp - first.buf);
@@ -1201,7 +1201,7 @@ static char *git_header_name(struct apply_state *state,
 		if (*second == '"') {
 			if (unquote_c_style(&sp, second, NULL))
 				goto free_and_fail1;
-			cp = skip_tree_prefix(state->p_value, sp.buf, sp.len);
+			cp = skip_tree_prefix(p_value, sp.buf, sp.len);
 			if (!cp)
 				goto free_and_fail1;
 			/* They must match, otherwise ignore */
@@ -1212,7 +1212,7 @@ static char *git_header_name(struct apply_state *state,
 		}
 
 		/* unquoted second */
-		cp = skip_tree_prefix(state->p_value, second, line + llen - second);
+		cp = skip_tree_prefix(p_value, second, line + llen - second);
 		if (!cp)
 			goto free_and_fail1;
 		if (line + llen - cp != first.len ||
@@ -1227,7 +1227,7 @@ static char *git_header_name(struct apply_state *state,
 	}
 
 	/* unquoted first name */
-	name = skip_tree_prefix(state->p_value, line, llen);
+	name = skip_tree_prefix(p_value, line, llen);
 	if (!name)
 		return NULL;
 
@@ -1243,7 +1243,7 @@ static char *git_header_name(struct apply_state *state,
 			if (unquote_c_style(&sp, second, NULL))
 				goto free_and_fail2;
 
-			np = skip_tree_prefix(state->p_value, sp.buf, sp.len);
+			np = skip_tree_prefix(p_value, sp.buf, sp.len);
 			if (!np)
 				goto free_and_fail2;
 
@@ -1287,7 +1287,7 @@ static char *git_header_name(struct apply_state *state,
 			 */
 			if (!name[len + 1])
 				return NULL; /* no postimage name */
-			second = skip_tree_prefix(state->p_value, name + len + 1,
+			second = skip_tree_prefix(p_value, name + len + 1,
 						  line_len - (len + 1));
 			if (!second)
 				return NULL;
@@ -1333,7 +1333,7 @@ static int parse_git_header(struct apply_state *state,
 	 * or removing or adding empty files), so we get
 	 * the default name from the header.
 	 */
-	patch->def_name = git_header_name(state, line, len);
+	patch->def_name = git_header_name(state->p_value, line, len);
 	if (patch->def_name && state->root.len) {
 		char *s = xstrfmt("%s%s", state->root.buf, patch->def_name);
 		free(patch->def_name);
-- 
2.22.0.510.g264f2c817a
Thomas Gummerer· Jul 8, 2019, 16:33 UTC · re: Thomas Gummerer · lore

[PATCH v3 04/14] apply: only pass required data to check_header_line

Currently the 'check_header_line()' function takes 'struct apply_state' as parameter, even though it only needs the linenr from that struct.

This function is in the callchain of 'parse_git_header()', which we want to make more generally useful in a subsequent commit. To make that happen we only want to pass in the required data to 'parse_git_header()', and not the whole 'struct apply_state', and thus we want functions in the callchain of 'parse_git_header()' to only take arguments they really need.

Signed-off-by: Thomas Gummerer <t.gummerer@gmail.com>
---
 apply.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)
Show changes to apply.c +4 −4
diff --git a/apply.c b/apply.c
index ac668e754d..1602fd5db0 100644
--- a/apply.c
+++ b/apply.c
@@ -1302,15 +1302,15 @@ static char *git_header_name(int p_value,
 	}
 }
 
-static int check_header_line(struct apply_state *state, struct patch *patch)
+static int check_header_line(int linenr, struct patch *patch)
 {
 	int extensions = (patch->is_delete == 1) + (patch->is_new == 1) +
 			 (patch->is_rename == 1) + (patch->is_copy == 1);
 	if (extensions > 1)
 		return error(_("inconsistent header lines %d and %d"),
-			     patch->extension_linenr, state->linenr);
+			     patch->extension_linenr, linenr);
 	if (extensions && !patch->extension_linenr)
-		patch->extension_linenr = state->linenr;
+		patch->extension_linenr = linenr;
 	return 0;
 }
 
@@ -1380,7 +1380,7 @@ static int parse_git_header(struct apply_state *state,
 			res = p->fn(state, line + oplen, patch);
 			if (res < 0)
 				return -1;
-			if (check_header_line(state, patch))
+			if (check_header_line(state->linenr, patch))
 				return -1;
 			if (res > 0)
 				return offset;
-- 
2.22.0.510.g264f2c817a
Thomas Gummerer· Jul 8, 2019, 16:33 UTC · re: Thomas Gummerer · lore

[PATCH v3 05/14] apply: only pass required data to find_name_*

Currently the 'find_name_*()' functions take 'struct apply_state' as parameter, even though they only need the 'root' member from that struct.

These functions are in the callchain of 'parse_git_header()', which we want to make more generally useful in a subsequent commit. To make that happen we only want to pass in the required data to 'parse_git_header()', and not the whole 'struct apply_state', and thus we want functions in the callchain of 'parse_git_header()' to only take arguments they really need.

Signed-off-by: Thomas Gummerer <t.gummerer@gmail.com>
---
 apply.c | 48 ++++++++++++++++++++++++------------------------
 1 file changed, 24 insertions(+), 24 deletions(-)
Show changes to apply.c +24 −24
diff --git a/apply.c b/apply.c
index 1602fd5db0..3cd4e3d3b3 100644
--- a/apply.c
+++ b/apply.c
@@ -469,7 +469,7 @@ static char *squash_slash(char *name)
 	return name;
 }
 
-static char *find_name_gnu(struct apply_state *state,
+static char *find_name_gnu(struct strbuf *root,
 			   const char *line,
 			   int p_value)
 {
@@ -495,8 +495,8 @@ static char *find_name_gnu(struct apply_state *state,
 	}
 
 	strbuf_remove(&name, 0, cp - name.buf);
-	if (state->root.len)
-		strbuf_insert(&name, 0, state->root.buf, state->root.len);
+	if (root->len)
+		strbuf_insert(&name, 0, root->buf, root->len);
 	return squash_slash(strbuf_detach(&name, NULL));
 }
 
@@ -659,7 +659,7 @@ static size_t diff_timestamp_len(const char *line, size_t len)
 	return line + len - end;
 }
 
-static char *find_name_common(struct apply_state *state,
+static char *find_name_common(struct strbuf *root,
 			      const char *line,
 			      const char *def,
 			      int p_value,
@@ -702,30 +702,30 @@ static char *find_name_common(struct apply_state *state,
 			return squash_slash(xstrdup(def));
 	}
 
-	if (state->root.len) {
-		char *ret = xstrfmt("%s%.*s", state->root.buf, len, start);
+	if (root->len) {
+		char *ret = xstrfmt("%s%.*s", root->buf, len, start);
 		return squash_slash(ret);
 	}
 
 	return squash_slash(xmemdupz(start, len));
 }
 
-static char *find_name(struct apply_state *state,
+static char *find_name(struct strbuf *root,
 		       const char *line,
 		       char *def,
 		       int p_value,
 		       int terminate)
 {
 	if (*line == '"') {
-		char *name = find_name_gnu(state, line, p_value);
+		char *name = find_name_gnu(root, line, p_value);
 		if (name)
 			return name;
 	}
 
-	return find_name_common(state, line, def, p_value, NULL, terminate);
+	return find_name_common(root, line, def, p_value, NULL, terminate);
 }
 
-static char *find_name_traditional(struct apply_state *state,
+static char *find_name_traditional(struct strbuf *root,
 				   const char *line,
 				   char *def,
 				   int p_value)
@@ -734,7 +734,7 @@ static char *find_name_traditional(struct apply_state *state,
 	size_t date_len;
 
 	if (*line == '"') {
-		char *name = find_name_gnu(state, line, p_value);
+		char *name = find_name_gnu(root, line, p_value);
 		if (name)
 			return name;
 	}
@@ -742,10 +742,10 @@ static char *find_name_traditional(struct apply_state *state,
 	len = strchrnul(line, '\n') - line;
 	date_len = diff_timestamp_len(line, len);
 	if (!date_len)
-		return find_name_common(state, line, def, p_value, NULL, TERM_TAB);
+		return find_name_common(root, line, def, p_value, NULL, TERM_TAB);
 	len -= date_len;
 
-	return find_name_common(state, line, def, p_value, line + len, 0);
+	return find_name_common(root, line, def, p_value, line + len, 0);
 }
 
 /*
@@ -759,7 +759,7 @@ static int guess_p_value(struct apply_state *state, const char *nameline)
 
 	if (is_dev_null(nameline))
 		return -1;
-	name = find_name_traditional(state, nameline, NULL, 0);
+	name = find_name_traditional(&state->root, nameline, NULL, 0);
 	if (!name)
 		return -1;
 	cp = strchr(name, '/');
@@ -883,17 +883,17 @@ static int parse_traditional_patch(struct apply_state *state,
 	if (is_dev_null(first)) {
 		patch->is_new = 1;
 		patch->is_delete = 0;
-		name = find_name_traditional(state, second, NULL, state->p_value);
+		name = find_name_traditional(&state->root, second, NULL, state->p_value);
 		patch->new_name = name;
 	} else if (is_dev_null(second)) {
 		patch->is_new = 0;
 		patch->is_delete = 1;
-		name = find_name_traditional(state, first, NULL, state->p_value);
+		name = find_name_traditional(&state->root, first, NULL, state->p_value);
 		patch->old_name = name;
 	} else {
 		char *first_name;
-		first_name = find_name_traditional(state, first, NULL, state->p_value);
-		name = find_name_traditional(state, second, first_name, state->p_value);
+		first_name = find_name_traditional(&state->root, first, NULL, state->p_value);
+		name = find_name_traditional(&state->root, second, first_name, state->p_value);
 		free(first_name);
 		if (has_epoch_timestamp(first)) {
 			patch->is_new = 1;
@@ -940,7 +940,7 @@ static int gitdiff_verify_name(struct apply_state *state,
 			       int side)
 {
 	if (!*name && !isnull) {
-		*name = find_name(state, line, NULL, state->p_value, TERM_TAB);
+		*name = find_name(&state->root, line, NULL, state->p_value, TERM_TAB);
 		return 0;
 	}
 
@@ -949,7 +949,7 @@ static int gitdiff_verify_name(struct apply_state *state,
 		if (isnull)
 			return error(_("git apply: bad git-diff - expected /dev/null, got %s on line %d"),
 				     *name, state->linenr);
-		another = find_name(state, line, NULL, state->p_value, TERM_TAB);
+		another = find_name(&state->root, line, NULL, state->p_value, TERM_TAB);
 		if (!another || strcmp(another, *name)) {
 			free(another);
 			return error((side == DIFF_NEW_NAME) ?
@@ -1032,7 +1032,7 @@ static int gitdiff_copysrc(struct apply_state *state,
 {
 	patch->is_copy = 1;
 	free(patch->old_name);
-	patch->old_name = find_name(state, line, NULL, state->p_value ? state->p_value - 1 : 0, 0);
+	patch->old_name = find_name(&state->root, line, NULL, state->p_value ? state->p_value - 1 : 0, 0);
 	return 0;
 }
 
@@ -1042,7 +1042,7 @@ static int gitdiff_copydst(struct apply_state *state,
 {
 	patch->is_copy = 1;
 	free(patch->new_name);
-	patch->new_name = find_name(state, line, NULL, state->p_value ? state->p_value - 1 : 0, 0);
+	patch->new_name = find_name(&state->root, line, NULL, state->p_value ? state->p_value - 1 : 0, 0);
 	return 0;
 }
 
@@ -1052,7 +1052,7 @@ static int gitdiff_renamesrc(struct apply_state *state,
 {
 	patch->is_rename = 1;
 	free(patch->old_name);
-	patch->old_name = find_name(state, line, NULL, state->p_value ? state->p_value - 1 : 0, 0);
+	patch->old_name = find_name(&state->root, line, NULL, state->p_value ? state->p_value - 1 : 0, 0);
 	return 0;
 }
 
@@ -1062,7 +1062,7 @@ static int gitdiff_renamedst(struct apply_state *state,
 {
 	patch->is_rename = 1;
 	free(patch->new_name);
-	patch->new_name = find_name(state, line, NULL, state->p_value ? state->p_value - 1 : 0, 0);
+	patch->new_name = find_name(&state->root, line, NULL, state->p_value ? state->p_value - 1 : 0, 0);
 	return 0;
 }
 
-- 
2.22.0.510.g264f2c817a
Thomas Gummerer· Jul 8, 2019, 16:33 UTC · re: Thomas Gummerer · lore

[PATCH v3 06/14] apply: only pass required data to gitdiff_* functions

Currently the 'gitdiff_*()' functions take 'struct apply_state' as parameter, even though they only needs the root, linenr and p_value from that struct.

These functions are in the callchain of 'parse_git_header()', which we want to make more generally useful in a subsequent commit. To make that happen we only want to pass in the required data to 'parse_git_header()', and not the whole 'struct apply_state', and thus we want functions in the callchain of 'parse_git_header()' to only take arguments they really need.

As these functions are called in a loop using their function pointers, each function needs to be passed all the parameters even if only one of the functions actually needs it. We therefore pass this data along in a struct to avoid adding too many unused parameters to each function and making the code very verbose in the process.

Signed-off-by: Thomas Gummerer <t.gummerer@gmail.com>
---
 apply.c | 59 ++++++++++++++++++++++++++++++++++-----------------------
 1 file changed, 35 insertions(+), 24 deletions(-)
Show changes to apply.c +35 −24
diff --git a/apply.c b/apply.c
index 3cd4e3d3b3..468f1d3fee 100644
--- a/apply.c
+++ b/apply.c
@@ -22,6 +22,12 @@
 #include "rerere.h"
 #include "apply.h"
 
+struct parse_git_header_state {
+	struct strbuf *root;
+	int linenr;
+	int p_value;
+};
+
 static void git_apply_config(void)
 {
 	git_config_get_string_const("apply.whitespace", &apply_default_whitespace);
@@ -914,7 +920,7 @@ static int parse_traditional_patch(struct apply_state *state,
 	return 0;
 }
 
-static int gitdiff_hdrend(struct apply_state *state,
+static int gitdiff_hdrend(struct parse_git_header_state *state,
 			  const char *line,
 			  struct patch *patch)
 {
@@ -933,14 +939,14 @@ static int gitdiff_hdrend(struct apply_state *state,
 #define DIFF_OLD_NAME 0
 #define DIFF_NEW_NAME 1
 
-static int gitdiff_verify_name(struct apply_state *state,
+static int gitdiff_verify_name(struct parse_git_header_state *state,
 			       const char *line,
 			       int isnull,
 			       char **name,
 			       int side)
 {
 	if (!*name && !isnull) {
-		*name = find_name(&state->root, line, NULL, state->p_value, TERM_TAB);
+		*name = find_name(state->root, line, NULL, state->p_value, TERM_TAB);
 		return 0;
 	}
 
@@ -949,7 +955,7 @@ static int gitdiff_verify_name(struct apply_state *state,
 		if (isnull)
 			return error(_("git apply: bad git-diff - expected /dev/null, got %s on line %d"),
 				     *name, state->linenr);
-		another = find_name(&state->root, line, NULL, state->p_value, TERM_TAB);
+		another = find_name(state->root, line, NULL, state->p_value, TERM_TAB);
 		if (!another || strcmp(another, *name)) {
 			free(another);
 			return error((side == DIFF_NEW_NAME) ?
@@ -965,7 +971,7 @@ static int gitdiff_verify_name(struct apply_state *state,
 	return 0;
 }
 
-static int gitdiff_oldname(struct apply_state *state,
+static int gitdiff_oldname(struct parse_git_header_state *state,
 			   const char *line,
 			   struct patch *patch)
 {
@@ -974,7 +980,7 @@ static int gitdiff_oldname(struct apply_state *state,
 				   DIFF_OLD_NAME);
 }
 
-static int gitdiff_newname(struct apply_state *state,
+static int gitdiff_newname(struct parse_git_header_state *state,
 			   const char *line,
 			   struct patch *patch)
 {
@@ -992,21 +998,21 @@ static int parse_mode_line(const char *line, int linenr, unsigned int *mode)
 	return 0;
 }
 
-static int gitdiff_oldmode(struct apply_state *state,
+static int gitdiff_oldmode(struct parse_git_header_state *state,
 			   const char *line,
 			   struct patch *patch)
 {
 	return parse_mode_line(line, state->linenr, &patch->old_mode);
 }
 
-static int gitdiff_newmode(struct apply_state *state,
+static int gitdiff_newmode(struct parse_git_header_state *state,
 			   const char *line,
 			   struct patch *patch)
 {
 	return parse_mode_line(line, state->linenr, &patch->new_mode);
 }
 
-static int gitdiff_delete(struct apply_state *state,
+static int gitdiff_delete(struct parse_git_header_state *state,
 			  const char *line,
 			  struct patch *patch)
 {
@@ -1016,7 +1022,7 @@ static int gitdiff_delete(struct apply_state *state,
 	return gitdiff_oldmode(state, line, patch);
 }
 
-static int gitdiff_newfile(struct apply_state *state,
+static int gitdiff_newfile(struct parse_git_header_state *state,
 			   const char *line,
 			   struct patch *patch)
 {
@@ -1026,47 +1032,47 @@ static int gitdiff_newfile(struct apply_state *state,
 	return gitdiff_newmode(state, line, patch);
 }
 
-static int gitdiff_copysrc(struct apply_state *state,
+static int gitdiff_copysrc(struct parse_git_header_state *state,
 			   const char *line,
 			   struct patch *patch)
 {
 	patch->is_copy = 1;
 	free(patch->old_name);
-	patch->old_name = find_name(&state->root, line, NULL, state->p_value ? state->p_value - 1 : 0, 0);
+	patch->old_name = find_name(state->root, line, NULL, state->p_value ? state->p_value - 1 : 0, 0);
 	return 0;
 }
 
-static int gitdiff_copydst(struct apply_state *state,
+static int gitdiff_copydst(struct parse_git_header_state *state,
 			   const char *line,
 			   struct patch *patch)
 {
 	patch->is_copy = 1;
 	free(patch->new_name);
-	patch->new_name = find_name(&state->root, line, NULL, state->p_value ? state->p_value - 1 : 0, 0);
+	patch->new_name = find_name(state->root, line, NULL, state->p_value ? state->p_value - 1 : 0, 0);
 	return 0;
 }
 
-static int gitdiff_renamesrc(struct apply_state *state,
+static int gitdiff_renamesrc(struct parse_git_header_state *state,
 			     const char *line,
 			     struct patch *patch)
 {
 	patch->is_rename = 1;
 	free(patch->old_name);
-	patch->old_name = find_name(&state->root, line, NULL, state->p_value ? state->p_value - 1 : 0, 0);
+	patch->old_name = find_name(state->root, line, NULL, state->p_value ? state->p_value - 1 : 0, 0);
 	return 0;
 }
 
-static int gitdiff_renamedst(struct apply_state *state,
+static int gitdiff_renamedst(struct parse_git_header_state *state,
 			     const char *line,
 			     struct patch *patch)
 {
 	patch->is_rename = 1;
 	free(patch->new_name);
-	patch->new_name = find_name(&state->root, line, NULL, state->p_value ? state->p_value - 1 : 0, 0);
+	patch->new_name = find_name(state->root, line, NULL, state->p_value ? state->p_value - 1 : 0, 0);
 	return 0;
 }
 
-static int gitdiff_similarity(struct apply_state *state,
+static int gitdiff_similarity(struct parse_git_header_state *state,
 			      const char *line,
 			      struct patch *patch)
 {
@@ -1076,7 +1082,7 @@ static int gitdiff_similarity(struct apply_state *state,
 	return 0;
 }
 
-static int gitdiff_dissimilarity(struct apply_state *state,
+static int gitdiff_dissimilarity(struct parse_git_header_state *state,
 				 const char *line,
 				 struct patch *patch)
 {
@@ -1086,7 +1092,7 @@ static int gitdiff_dissimilarity(struct apply_state *state,
 	return 0;
 }
 
-static int gitdiff_index(struct apply_state *state,
+static int gitdiff_index(struct parse_git_header_state *state,
 			 const char *line,
 			 struct patch *patch)
 {
@@ -1126,7 +1132,7 @@ static int gitdiff_index(struct apply_state *state,
  * This is normal for a diff that doesn't change anything: we'll fall through
  * into the next diff. Tell the parser to break out.
  */
-static int gitdiff_unrecognized(struct apply_state *state,
+static int gitdiff_unrecognized(struct parse_git_header_state *state,
 				const char *line,
 				struct patch *patch)
 {
@@ -1322,6 +1328,7 @@ static int parse_git_header(struct apply_state *state,
 			    struct patch *patch)
 {
 	unsigned long offset;
+	struct parse_git_header_state parse_hdr_state;
 
 	/* A git diff has explicit new/delete information, so we don't guess */
 	patch->is_new = 0;
@@ -1343,10 +1350,14 @@ static int parse_git_header(struct apply_state *state,
 	line += len;
 	size -= len;
 	state->linenr++;
+	parse_hdr_state.root = &state->root;
+	parse_hdr_state.linenr = state->linenr;
+	parse_hdr_state.p_value = state->p_value;
+
 	for (offset = len ; size > 0 ; offset += len, size -= len, line += len, state->linenr++) {
 		static const struct opentry {
 			const char *str;
-			int (*fn)(struct apply_state *, const char *, struct patch *);
+			int (*fn)(struct parse_git_header_state *, const char *, struct patch *);
 		} optable[] = {
 			{ "@@ -", gitdiff_hdrend },
 			{ "--- ", gitdiff_oldname },
@@ -1377,7 +1388,7 @@ static int parse_git_header(struct apply_state *state,
 			int res;
 			if (len < oplen || memcmp(p->str, line, oplen))
 				continue;
-			res = p->fn(state, line + oplen, patch);
+			res = p->fn(&parse_hdr_state, line + oplen, patch);
 			if (res < 0)
 				return -1;
 			if (check_header_line(state->linenr, patch))
-- 
2.22.0.510.g264f2c817a
Thomas Gummerer· Jul 8, 2019, 16:33 UTC · re: Thomas Gummerer · lore

[PATCH v3 07/14] apply: make parse_git_header public

Make parse_git_header a "public" function in apply.h, so we can re-use it in range-diff in a subsequent commit.

Signed-off-by: Thomas Gummerer <t.gummerer@gmail.com>
---
 apply.c | 69 ++++++++++++++++-----------------------------------------
 apply.h | 48 +++++++++++++++++++++++++++++++++++++++
 2 files changed, 67 insertions(+), 50 deletions(-)
Show changes to 2 files +67 −50

apply.c, apply.h

diff --git a/apply.c b/apply.c
index 468f1d3fee..32b5b072ee 100644
--- a/apply.c
+++ b/apply.c
@@ -207,40 +207,6 @@ struct fragment {
 #define BINARY_DELTA_DEFLATED	1
 #define BINARY_LITERAL_DEFLATED 2
 
-/*
- * This represents a "patch" to a file, both metainfo changes
- * such as creation/deletion, filemode and content changes represented
- * as a series of fragments.
- */
-struct patch {
-	char *new_name, *old_name, *def_name;
-	unsigned int old_mode, new_mode;
-	int is_new, is_delete;	/* -1 = unknown, 0 = false, 1 = true */
-	int rejected;
-	unsigned ws_rule;
-	int lines_added, lines_deleted;
-	int score;
-	int extension_linenr; /* first line specifying delete/new/rename/copy */
-	unsigned int is_toplevel_relative:1;
-	unsigned int inaccurate_eof:1;
-	unsigned int is_binary:1;
-	unsigned int is_copy:1;
-	unsigned int is_rename:1;
-	unsigned int recount:1;
-	unsigned int conflicted_threeway:1;
-	unsigned int direct_to_threeway:1;
-	unsigned int crlf_in_old:1;
-	struct fragment *fragments;
-	char *result;
-	size_t resultsize;
-	char old_oid_prefix[GIT_MAX_HEXSZ + 1];
-	char new_oid_prefix[GIT_MAX_HEXSZ + 1];
-	struct patch *next;
-
-	/* three-way fallback result */
-	struct object_id threeway_stage[3];
-};
-
 static void free_fragment_list(struct fragment *list)
 {
 	while (list) {
@@ -1320,12 +1286,13 @@ static int check_header_line(int linenr, struct patch *patch)
 	return 0;
 }
 
-/* Verify that we recognize the lines following a git header */
-static int parse_git_header(struct apply_state *state,
-			    const char *line,
-			    int len,
-			    unsigned int size,
-			    struct patch *patch)
+int parse_git_diff_header(struct strbuf *root,
+			  int *linenr,
+			  int p_value,
+			  const char *line,
+			  int len,
+			  unsigned int size,
+			  struct patch *patch)
 {
 	unsigned long offset;
 	struct parse_git_header_state parse_hdr_state;
@@ -1340,21 +1307,21 @@ static int parse_git_header(struct apply_state *state,
 	 * or removing or adding empty files), so we get
 	 * the default name from the header.
 	 */
-	patch->def_name = git_header_name(state->p_value, line, len);
-	if (patch->def_name && state->root.len) {
-		char *s = xstrfmt("%s%s", state->root.buf, patch->def_name);
+	patch->def_name = git_header_name(p_value, line, len);
+	if (patch->def_name && root->len) {
+		char *s = xstrfmt("%s%s", root->buf, patch->def_name);
 		free(patch->def_name);
 		patch->def_name = s;
 	}
 
 	line += len;
 	size -= len;
-	state->linenr++;
-	parse_hdr_state.root = &state->root;
-	parse_hdr_state.linenr = state->linenr;
-	parse_hdr_state.p_value = state->p_value;
+	(*linenr)++;
+	parse_hdr_state.root = root;
+	parse_hdr_state.linenr = *linenr;
+	parse_hdr_state.p_value = p_value;
 
-	for (offset = len ; size > 0 ; offset += len, size -= len, line += len, state->linenr++) {
+	for (offset = len ; size > 0 ; offset += len, size -= len, line += len, (*linenr)++) {
 		static const struct opentry {
 			const char *str;
 			int (*fn)(struct parse_git_header_state *, const char *, struct patch *);
@@ -1391,7 +1358,7 @@ static int parse_git_header(struct apply_state *state,
 			res = p->fn(&parse_hdr_state, line + oplen, patch);
 			if (res < 0)
 				return -1;
-			if (check_header_line(state->linenr, patch))
+			if (check_header_line(*linenr, patch))
 				return -1;
 			if (res > 0)
 				return offset;
@@ -1572,7 +1539,9 @@ static int find_header(struct apply_state *state,
 		 * or mode change, so we handle that specially
 		 */
 		if (!memcmp("diff --git ", line, 11)) {
-			int git_hdr_len = parse_git_header(state, line, len, size, patch);
+			int git_hdr_len = parse_git_diff_header(&state->root, &state->linenr,
+								state->p_value, line, len,
+								size, patch);
 			if (git_hdr_len < 0)
 				return -128;
 			if (git_hdr_len <= len)
diff --git a/apply.h b/apply.h
index 5948348133..ade50f66c5 100644
--- a/apply.h
+++ b/apply.h
@@ -117,6 +117,40 @@ struct apply_state {
 	int applied_after_fixing_ws;
 };
 
+/*
+ * This represents a "patch" to a file, both metainfo changes
+ * such as creation/deletion, filemode and content changes represented
+ * as a series of fragments.
+ */
+struct patch {
+	char *new_name, *old_name, *def_name;
+	unsigned int old_mode, new_mode;
+	int is_new, is_delete;	/* -1 = unknown, 0 = false, 1 = true */
+	int rejected;
+	unsigned ws_rule;
+	int lines_added, lines_deleted;
+	int score;
+	int extension_linenr; /* first line specifying delete/new/rename/copy */
+	unsigned int is_toplevel_relative:1;
+	unsigned int inaccurate_eof:1;
+	unsigned int is_binary:1;
+	unsigned int is_copy:1;
+	unsigned int is_rename:1;
+	unsigned int recount:1;
+	unsigned int conflicted_threeway:1;
+	unsigned int direct_to_threeway:1;
+	unsigned int crlf_in_old:1;
+	struct fragment *fragments;
+	char *result;
+	size_t resultsize;
+	char old_oid_prefix[GIT_MAX_HEXSZ + 1];
+	char new_oid_prefix[GIT_MAX_HEXSZ + 1];
+	struct patch *next;
+
+	/* three-way fallback result */
+	struct object_id threeway_stage[3];
+};
+
 int apply_parse_options(int argc, const char **argv,
 			struct apply_state *state,
 			int *force_apply, int *options,
@@ -127,6 +161,20 @@ int init_apply_state(struct apply_state *state,
 void clear_apply_state(struct apply_state *state);
 int check_apply_state(struct apply_state *state, int force_apply);
 
+/*
+ * Parse a get header, starting at line.  Fills the relevant metadata
+ * information in 'struct patch'.
+ *
+ * Returns -1 on failure, the length of the parsed header otherwise.
+ */
+int parse_git_diff_header(struct strbuf *root,
+			  int *linenr,
+			  int p_value,
+			  const char *line,
+			  int len,
+			  unsigned int size,
+			  struct patch *patch);
+
 /*
  * Some aspects of the apply behavior are controlled by the following
  * bits in the "options" parameter passed to apply_all_patches().
-- 
2.22.0.510.g264f2c817a
Junio C Hamano· Jul 9, 2019, 19:39 UTC · re: Thomas Gummerer · lore

Re: [PATCH v3 07/14] apply: make parse_git_header public

Thomas Gummerer <t.gummerer@gmail.com> writes:
Show 5 quoted lines
> Make parse_git_header a "public" function in apply.h, so we can re-use
> it in range-diff in a subsequent commit.
>
> Signed-off-by: Thomas Gummerer <t.gummerer@gmail.com>
> ---

Thanks for these refactoring patches on "apply" machinery in the early part of the series. I noticed two small things, though.

 - The apply_state instance *does* represent a state and various
   fields get updated as we read and process the patch.  The smaller
   structure you invented, on the other hand, does not carry any
   "state" at all.  Even its "linenr" field does not get incremented
   as we read/process---you create a new copy to take a snapshot of
   the current state from apply_state.  parse_git_header_data may
   have been a name that reflects the nature of the structure
   better.
 - I wonder if it makes the concept clearer if you did not create a
   new instance outside the apply_state, but instead replaced the
   three fields in the apply_state with an instance of this new
   structure.  When you call an API function with shrunk interface,
   you'd pass a pointer to a field inside the apply_state instance,
   instead of copying three fields manually.

But other than that, I think these patches are generally moving bits in the right direction.

I do not have strong opinions on the later part of the series on range-diff proper.

Thanks.
Thomas Gummerer· Jul 9, 2019, 21:23 UTC · re: Junio C Hamano · lore

Re: [PATCH v3 07/14] apply: make parse_git_header public

On 07/09, Junio C Hamano wrote:
> Thomas Gummerer <t.gummerer@gmail.com> writes:
> 
> > Make parse_git_header a "public" function in apply.h, so we can re-use
> > it in range-diff in a subsequent commit.

Eek, I just noticed that I forgot updating the name here. This and the Subject should say 'parse_git_diff_header()' now, instead of parse_git_header of course. Will fix that in the reroll.

Show 14 quoted lines
> > Signed-off-by: Thomas Gummerer <t.gummerer@gmail.com>
> > ---
> 
> Thanks for these refactoring patches on "apply" machinery in the
> early part of the series.  I noticed two small things, though.
> 
>  - The apply_state instance *does* represent a state and various
>    fields get updated as we read and process the patch.  The smaller
>    structure you invented, on the other hand, does not carry any
>    "state" at all.  Even its "linenr" field does not get incremented
>    as we read/process---you create a new copy to take a snapshot of
>    the current state from apply_state.  parse_git_header_data may
>    have been a name that reflects the nature of the structure
>    better.
Yeah, I think that's better.  Will change, thanks!

Maybe it would be even better to name it 'struct gitdiff_data', as it's really only used for those few functions?

Show 6 quoted lines
>  - I wonder if it makes the concept clearer if you did not create a
>    new instance outside the apply_state, but instead replaced the
>    three fields in the apply_state with an instance of this new
>    structure.  When you call an API function with shrunk interface,
>    you'd pass a pointer to a field inside the apply_state instance,
>    instead of copying three fields manually.

I had considered that. However I struggled to come up with a name that makes sense in both as an interface to 'parse_git_diff_header()', and inside 'struct apply_state'. 'linenr' is not specific to parsing git diff headers (or even parsing any type of diff header), but is used all over the apply code. So 'parse_git_header_data' doesn't make sense as a name anymore (and gets complicated to explain to the readers of the code I think).

At that point the name should also be <something>_state again, because we do update the linenr inside 'parse_git_diff_header()', just not inside any of the 'gitdiff_*' functions, though that is only a minor point.

So unless there's a good name for this struct that I couldn't think of, I think it's better to pass in the variables separately to 'parse_git_diff_header()', and then pass the struct just to the 'gitdiff_*' functions, as it's done currently.

> But other than that, I think these patches are generally moving bits
> in the right direction.
Thanks for the review!
> I do not have strong opinions on the later part of the series on
> range-diff proper.
> 
> Thanks.
Junio C Hamano· Jul 9, 2019, 23:22 UTC · re: Thomas Gummerer · lore

Re: [PATCH v3 07/14] apply: make parse_git_header public

Thomas Gummerer <t.gummerer@gmail.com> writes:
> Maybe it would be even better to name it 'struct gitdiff_data', as
> it's really only used for those few functions?

Is it really the case where "these three are only used by the codepath you made public"? If so, I agree that "gitdiff_data" is a perfectly good name for it.

I however had an impression that it is the oppposite, i.e. "the codepath you made public only needs these three, but these three are used by other (still private) parts, too." If this is the case, then "gitdiff_data" is a misnomer, if we were to embed an instance inside apply_state.

It seems that it is not a good idea to do such embedding, and if that is the case, "gitdiff_data" is a fine for the three-field struct.

Thanks.
Thomas Gummerer· Jul 10, 2019, 08:48 UTC · re: Junio C Hamano · lore

Re: [PATCH v3 07/14] apply: make parse_git_header public

On 07/09, Junio C Hamano wrote:
Show 14 quoted lines
> Thomas Gummerer <t.gummerer@gmail.com> writes:
> 
> > Maybe it would be even better to name it 'struct gitdiff_data', as
> > it's really only used for those few functions?
> 
> Is it really the case where "these three are only used by the
> codepath you made public"?  If so, I agree that "gitdiff_data" is a
> perfectly good name for it.
> 
> I however had an impression that it is the oppposite, i.e. "the
> codepath you made public only needs these three, but these three are
> used by other (still private) parts, too."  If this is the case,
> then "gitdiff_data" is a misnomer, if we were to embed an instance
> inside apply_state.

Yeah, that's correct. What I meant was that since we're only using this struct for the private 'gitdiff_*()' functions, which are called from 'parse_git_diff_header()', 'struct gitdiff_data' would be a better name than 'struct parse_git_diff_header_data'.

I do agree that it wouldn't be a good name if we were to embed it inside 'struct apply_state', and as mentioned in the previous email I'd have a hard time coming up with a good name if we were to do that.

> It seems that it is not a good idea to do such embedding, and if
> that is the case, "gitdiff_data" is a fine for the three-field
> struct.
Yeah, I think that's the best way forward, thanks.
> Thanks.
Thomas Gummerer· Jul 8, 2019, 16:33 UTC · re: Thomas Gummerer · lore

[PATCH v3 09/14] range-diff: split lines manually

Currently range-diff uses the 'strbuf_getline()' function for doing its line by line processing. In a future patch we want to do parts of that parsing using the 'parse_git_diff_header()' function. That function does its own line by line reading of the input, and doesn't use strbufs. This doesn't match with how we do the line-by-line processing in range-diff currently.

Switch range-diff to do our own line by line parsing, so we can re-use the 'parse_git_diff_header()' function later.

Signed-off-by: Thomas Gummerer <t.gummerer@gmail.com>
---
 range-diff.c | 68 ++++++++++++++++++++++++++++++++--------------------
 1 file changed, 42 insertions(+), 26 deletions(-)
Show changes to range-diff.c +42 −26
diff --git a/range-diff.c b/range-diff.c
index 9242b8975f..784fac301b 100644
--- a/range-diff.c
+++ b/range-diff.c
@@ -24,6 +24,17 @@ struct patch_util {
 	struct object_id oid;
 };
 
+static size_t find_end_of_line(char *buffer, unsigned long size)
+{
+	char *eol = memchr(buffer, '\n', size);
+
+	if (!eol)
+		return size;
+
+	*eol = '\0';
+	return eol + 1 - buffer;
+}
+
 /*
  * Reads the patches into a string list, with the `util` field being populated
  * as struct object_id (will need to be free()d).
@@ -31,10 +42,12 @@ struct patch_util {
 static int read_patches(const char *range, struct string_list *list)
 {
 	struct child_process cp = CHILD_PROCESS_INIT;
-	FILE *in;
-	struct strbuf buf = STRBUF_INIT, line = STRBUF_INIT;
+	struct strbuf buf = STRBUF_INIT, contents = STRBUF_INIT;
 	struct patch_util *util = NULL;
 	int in_header = 1;
+	char *line;
+	int offset, len;
+	size_t size;
 
 	argv_array_pushl(&cp.args, "log", "--no-color", "-p", "--no-merges",
 			"--reverse", "--date-order", "--decorate=no",
@@ -54,17 +67,20 @@ static int read_patches(const char *range, struct string_list *list)
 
 	if (start_command(&cp))
 		return error_errno(_("could not start `log`"));
-	in = fdopen(cp.out, "r");
-	if (!in) {
+	if (strbuf_read(&contents, cp.out, 0) < 0) {
 		error_errno(_("could not read `log` output"));
 		finish_command(&cp);
 		return -1;
 	}
 
-	while (strbuf_getline(&line, in) != EOF) {
+	line = contents.buf;
+	size = contents.len;
+	for (offset = 0; size > 0; offset += len, size -= len, line += len) {
 		const char *p;
 
-		if (skip_prefix(line.buf, "commit ", &p)) {
+		len = find_end_of_line(line, size);
+		line[len - 1] = '\0';
+		if (skip_prefix(line, "commit ", &p)) {
 			if (util) {
 				string_list_append(list, buf.buf)->util = util;
 				strbuf_reset(&buf);
@@ -75,8 +91,7 @@ static int read_patches(const char *range, struct string_list *list)
 				free(util);
 				string_list_clear(list, 1);
 				strbuf_release(&buf);
-				strbuf_release(&line);
-				fclose(in);
+				strbuf_release(&contents);
 				finish_command(&cp);
 				return -1;
 			}
@@ -85,26 +100,28 @@ static int read_patches(const char *range, struct string_list *list)
 			continue;
 		}
 
-		if (starts_with(line.buf, "diff --git")) {
+		if (starts_with(line, "diff --git")) {
 			in_header = 0;
 			strbuf_addch(&buf, '\n');
 			if (!util->diff_offset)
 				util->diff_offset = buf.len;
 			strbuf_addch(&buf, ' ');
-			strbuf_addbuf(&buf, &line);
+			strbuf_addstr(&buf, line);
 		} else if (in_header) {
-			if (starts_with(line.buf, "Author: ")) {
-				strbuf_addbuf(&buf, &line);
+			if (starts_with(line, "Author: ")) {
+				strbuf_addstr(&buf, line);
 				strbuf_addstr(&buf, "\n\n");
-			} else if (starts_with(line.buf, "    ")) {
-				strbuf_rtrim(&line);
-				strbuf_addbuf(&buf, &line);
+			} else if (starts_with(line, "    ")) {
+				p = line + len - 2;
+				while (isspace(*p) && p >= line)
+					p--;
+				strbuf_add(&buf, line, p - line + 1);
 				strbuf_addch(&buf, '\n');
 			}
 			continue;
-		} else if (starts_with(line.buf, "@@ "))
+		} else if (starts_with(line, "@@ "))
 			strbuf_addstr(&buf, "@@");
-		else if (!line.buf[0] || starts_with(line.buf, "index "))
+		else if (!line[0] || starts_with(line, "index "))
 			/*
 			 * A completely blank (not ' \n', which is context)
 			 * line is not valid in a diff.  We skip it
@@ -117,25 +134,24 @@ static int read_patches(const char *range, struct string_list *list)
 			 * we are not interested.
 			 */
 			continue;
-		else if (line.buf[0] == '>') {
+		else if (line[0] == '>') {
 			strbuf_addch(&buf, '+');
-			strbuf_add(&buf, line.buf + 1, line.len - 1);
-		} else if (line.buf[0] == '<') {
+			strbuf_addstr(&buf, line + 1);
+		} else if (line[0] == '<') {
 			strbuf_addch(&buf, '-');
-			strbuf_add(&buf, line.buf + 1, line.len - 1);
-		} else if (line.buf[0] == '#') {
+			strbuf_addstr(&buf, line + 1);
+		} else if (line[0] == '#') {
 			strbuf_addch(&buf, ' ');
-			strbuf_add(&buf, line.buf + 1, line.len - 1);
+			strbuf_addstr(&buf, line + 1);
 		} else {
 			strbuf_addch(&buf, ' ');
-			strbuf_addbuf(&buf, &line);
+			strbuf_addstr(&buf, line);
 		}
 
 		strbuf_addch(&buf, '\n');
 		util->diffsize++;
 	}
-	fclose(in);
-	strbuf_release(&line);
+	strbuf_release(&contents);
 
 	if (util)
 		string_list_append(list, buf.buf)->util = util;
-- 
2.22.0.510.g264f2c817a
Thomas Gummerer· Jul 8, 2019, 16:33 UTC · re: Thomas Gummerer · lore

[PATCH v3 08/14] range-diff: fix function parameter indentation

Fix the indentation of the function parameters for a couple of functions, to match the style in the rest of the file.

Signed-off-by: Thomas Gummerer <t.gummerer@gmail.com>
---
 range-diff.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
Show changes to range-diff.c +2 −2
diff --git a/range-diff.c b/range-diff.c
index 48b0e1b4ce..9242b8975f 100644
--- a/range-diff.c
+++ b/range-diff.c
@@ -148,7 +148,7 @@ static int read_patches(const char *range, struct string_list *list)
 }
 
 static int patch_util_cmp(const void *dummy, const struct patch_util *a,
-		     const struct patch_util *b, const char *keydata)
+			  const struct patch_util *b, const char *keydata)
 {
 	return strcmp(a->diff, keydata ? keydata : b->diff);
 }
@@ -373,7 +373,7 @@ static struct diff_filespec *get_filespec(const char *name, const char *p)
 }
 
 static void patch_diff(const char *a, const char *b,
-			      struct diff_options *diffopt)
+		       struct diff_options *diffopt)
 {
 	diff_queue(&diff_queued_diff,
 		   get_filespec("a", a), get_filespec("b", b));
-- 
2.22.0.510.g264f2c817a
Thomas Gummerer· Jul 8, 2019, 16:33 UTC · re: Thomas Gummerer · lore

[PATCH v3 10/14] range-diff: don't remove funcname from inner diff

When postprocessing the inner diff in range-diff, we currently replace the whole hunk header line with just "@@". This matches how 'git tbdiff' used to handle hunk headers as well.

Most likely this is being done because line numbers in the hunk header are not relevant without other changes. They can for example easily change if a range is rebased, and lines are added/removed before a change that we actually care about in our ranges.

However it can still be useful to have the function name that 'git diff' extracts as additional context for the change.

Note that it is not guaranteed that the hunk header actually shows up in the range-diff, and this change only aims to improve the case where a hunk header would already be included in the final output.

Signed-off-by: Thomas Gummerer <t.gummerer@gmail.com>
---
 range-diff.c          | 7 ++++---
 t/t3206-range-diff.sh | 6 +++---
 2 files changed, 7 insertions(+), 6 deletions(-)
Show changes to 2 files +7 −6

range-diff.c, t/t3206-range-diff.sh

diff --git a/range-diff.c b/range-diff.c
index 784fac301b..a5202d8b6c 100644
--- a/range-diff.c
+++ b/range-diff.c
@@ -119,9 +119,10 @@ static int read_patches(const char *range, struct string_list *list)
 				strbuf_addch(&buf, '\n');
 			}
 			continue;
-		} else if (starts_with(line, "@@ "))
-			strbuf_addstr(&buf, "@@");
-		else if (!line[0] || starts_with(line, "index "))
+		} else if (skip_prefix(line, "@@ ", &p)) {
+			p = strstr(p, "@@");
+			strbuf_addstr(&buf, p ? p : "@@");
+		} else if (!line[0] || starts_with(line, "index "))
 			/*
 			 * A completely blank (not ' \n', which is context)
 			 * line is not valid in a diff.  We skip it
diff --git a/t/t3206-range-diff.sh b/t/t3206-range-diff.sh
index 048feaf6dd..aebd4e3693 100755
--- a/t/t3206-range-diff.sh
+++ b/t/t3206-range-diff.sh
@@ -110,7 +110,7 @@ test_expect_success 'changed commit' '
 	      14
 	4:  a63e992 ! 4:  d966c5c s/12/B/
 	    @@ -8,7 +8,7 @@
-	     @@
+	     @@ A
 	      9
 	      10
 	    - B
@@ -169,7 +169,7 @@ test_expect_success 'changed commit with sm config' '
 	      14
 	4:  a63e992 ! 4:  d966c5c s/12/B/
 	    @@ -8,7 +8,7 @@
-	     @@
+	     @@ A
 	      9
 	      10
 	    - B
@@ -231,7 +231,7 @@ test_expect_success 'dual-coloring' '
 	:      14<RESET>
 	:<RED>4:  d966c5c <RESET><YELLOW>!<RESET><GREEN> 4:  8add5f1<RESET><YELLOW> s/12/B/<RESET>
 	:    <REVERSE><CYAN>@@ -8,7 +8,7 @@<RESET>
-	:    <CYAN> @@<RESET>
+	:    <CYAN> @@ A<RESET>
 	:      9<RESET>
 	:      10<RESET>
 	:    <REVERSE><RED>-<RESET><FAINT> BB<RESET>
-- 
2.22.0.510.g264f2c817a
Thomas Gummerer· Jul 8, 2019, 16:33 UTC · re: Thomas Gummerer · lore

[PATCH v3 11/14] range-diff: suppress line count in outer diff

The line count in the outer diff's hunk headers of a range diff is not all that interesting. It merely shows how far along the inner diff are on both sides. That number is of no use for human readers, and range-diffs are not meant to be machine readable.

In a subsequent commit we're going to add some more contextual information such as the filename corresponding to the diff to the hunk headers. Remove the unnecessary information, and just keep the "@@" to indicate that a new hunk of the outer diff is starting.

Signed-off-by: Thomas Gummerer <t.gummerer@gmail.com>
---
 diff.c                |  5 ++++-
 diff.h                |  1 +
 range-diff.c          |  1 +
 t/t3206-range-diff.sh | 16 ++++++++--------
 4 files changed, 14 insertions(+), 9 deletions(-)
Show changes to 4 files +14 −9

diff.c, diff.h, range-diff.c, t/t3206-range-diff.sh

diff --git a/diff.c b/diff.c
index ec5c095199..9c28ff0a92 100644
--- a/diff.c
+++ b/diff.c
@@ -1672,7 +1672,10 @@ static void emit_hunk_header(struct emit_callback *ecbdata,
 	if (ecbdata->opt->flags.dual_color_diffed_diffs)
 		strbuf_addstr(&msgbuf, reverse);
 	strbuf_addstr(&msgbuf, frag);
-	strbuf_add(&msgbuf, line, ep - line);
+	if (ecbdata->opt->flags.suppress_hunk_header_line_count)
+		strbuf_add(&msgbuf, atat, sizeof(atat));
+	else
+		strbuf_add(&msgbuf, line, ep - line);
 	strbuf_addstr(&msgbuf, reset);
 
 	/*
diff --git a/diff.h b/diff.h
index c9db9825bb..49913049f9 100644
--- a/diff.h
+++ b/diff.h
@@ -98,6 +98,7 @@ struct diff_flags {
 	unsigned stat_with_summary;
 	unsigned suppress_diff_headers;
 	unsigned dual_color_diffed_diffs;
+	unsigned suppress_hunk_header_line_count;
 };
 
 static inline void diff_flags_or(struct diff_flags *a,
diff --git a/range-diff.c b/range-diff.c
index a5202d8b6c..f4a90b33b8 100644
--- a/range-diff.c
+++ b/range-diff.c
@@ -486,6 +486,7 @@ int show_range_diff(const char *range1, const char *range2,
 			opts.output_format = DIFF_FORMAT_PATCH;
 		opts.flags.suppress_diff_headers = 1;
 		opts.flags.dual_color_diffed_diffs = dual_color;
+		opts.flags.suppress_hunk_header_line_count = 1;
 		opts.output_prefix = output_prefix_cb;
 		strbuf_addstr(&indent, "    ");
 		opts.output_prefix_data = &indent;
diff --git a/t/t3206-range-diff.sh b/t/t3206-range-diff.sh
index aebd4e3693..9f89af7178 100755
--- a/t/t3206-range-diff.sh
+++ b/t/t3206-range-diff.sh
@@ -99,7 +99,7 @@ test_expect_success 'changed commit' '
 	1:  4de457d = 1:  a4b3333 s/5/A/
 	2:  fccce22 = 2:  f51d370 s/4/A/
 	3:  147e64e ! 3:  0559556 s/11/B/
-	    @@ -10,7 +10,7 @@
+	    @@
 	      9
 	      10
 	     -11
@@ -109,7 +109,7 @@ test_expect_success 'changed commit' '
 	      13
 	      14
 	4:  a63e992 ! 4:  d966c5c s/12/B/
-	    @@ -8,7 +8,7 @@
+	    @@
 	     @@ A
 	      9
 	      10
@@ -158,7 +158,7 @@ test_expect_success 'changed commit with sm config' '
 	1:  4de457d = 1:  a4b3333 s/5/A/
 	2:  fccce22 = 2:  f51d370 s/4/A/
 	3:  147e64e ! 3:  0559556 s/11/B/
-	    @@ -10,7 +10,7 @@
+	    @@
 	      9
 	      10
 	     -11
@@ -168,7 +168,7 @@ test_expect_success 'changed commit with sm config' '
 	      13
 	      14
 	4:  a63e992 ! 4:  d966c5c s/12/B/
-	    @@ -8,7 +8,7 @@
+	    @@
 	     @@ A
 	      9
 	      10
@@ -191,7 +191,7 @@ test_expect_success 'changed message' '
 	sed s/Z/\ /g >expected <<-EOF &&
 	1:  4de457d = 1:  f686024 s/5/A/
 	2:  fccce22 ! 2:  4ab067d s/4/A/
-	    @@ -2,6 +2,8 @@
+	    @@
 	    Z
 	    Z    s/4/A/
 	    Z
@@ -210,7 +210,7 @@ test_expect_success 'dual-coloring' '
 	sed -e "s|^:||" >expect <<-\EOF &&
 	:<YELLOW>1:  a4b3333 = 1:  f686024 s/5/A/<RESET>
 	:<RED>2:  f51d370 <RESET><YELLOW>!<RESET><GREEN> 2:  4ab067d<RESET><YELLOW> s/4/A/<RESET>
-	:    <REVERSE><CYAN>@@ -2,6 +2,8 @@<RESET>
+	:    <REVERSE><CYAN>@@<RESET>
 	:     <RESET>
 	:         s/4/A/<RESET>
 	:     <RESET>
@@ -220,7 +220,7 @@ test_expect_success 'dual-coloring' '
 	:      --- a/file<RESET>
 	:      +++ b/file<RESET>
 	:<RED>3:  0559556 <RESET><YELLOW>!<RESET><GREEN> 3:  b9cb956<RESET><YELLOW> s/11/B/<RESET>
-	:    <REVERSE><CYAN>@@ -10,7 +10,7 @@<RESET>
+	:    <REVERSE><CYAN>@@<RESET>
 	:      9<RESET>
 	:      10<RESET>
 	:    <RED> -11<RESET>
@@ -230,7 +230,7 @@ test_expect_success 'dual-coloring' '
 	:      13<RESET>
 	:      14<RESET>
 	:<RED>4:  d966c5c <RESET><YELLOW>!<RESET><GREEN> 4:  8add5f1<RESET><YELLOW> s/12/B/<RESET>
-	:    <REVERSE><CYAN>@@ -8,7 +8,7 @@<RESET>
+	:    <REVERSE><CYAN>@@<RESET>
 	:    <CYAN> @@ A<RESET>
 	:      9<RESET>
 	:      10<RESET>
-- 
2.22.0.510.g264f2c817a
Thomas Gummerer· Jul 8, 2019, 16:33 UTC · re: Thomas Gummerer · lore

[PATCH v3 12/14] range-diff: add section header instead of diff header

Currently range-diff keeps the diff header of the inner diff intact (apart from stripping lines starting with index). This diff header is somewhat useful, especially when files get different names in different ranges.

However there is no real need to keep the whole diff header for that. The main reason we currently do that is probably because it is easy to do.

Introduce a new range diff hunk header, that's enclosed by "##", similar to how line numbers in diff hunks are enclosed by "@@", and give human readable information of what exactly happened to the file, including the file name.

This improves the readability of the range-diff by giving more concise information to the users. For example if a file was renamed in one iteration, but not in another, the diff of the headers would be quite noisy. However the diff of a single line is concise and should be easier to understand.

Additionally, this allows us to add these range diff section headers to the outer diffs hunk headers using a custom userdiff pattern, which should help making the range-diff more readable.

Signed-off-by: Thomas Gummerer <t.gummerer@gmail.com>
---
 range-diff.c           | 34 ++++++++++++----
 t/t3206-range-diff.sh  | 91 +++++++++++++++++++++++++++++++++++++++---
 t/t3206/history.export | 84 ++++++++++++++++++++++++++++++++++++--
 3 files changed, 192 insertions(+), 17 deletions(-)
Show changes to 3 files +192 −17

range-diff.c, t/t3206-range-diff.sh, t/t3206/history.export

diff --git a/range-diff.c b/range-diff.c
index f4a90b33b8..5f64380fe4 100644
--- a/range-diff.c
+++ b/range-diff.c
@@ -10,6 +10,7 @@
 #include "commit.h"
 #include "pretty.h"
 #include "userdiff.h"
+#include "apply.h"
 
 struct patch_util {
 	/* For the search for an exact match */
@@ -101,12 +102,35 @@ static int read_patches(const char *range, struct string_list *list)
 		}
 
 		if (starts_with(line, "diff --git")) {
+			struct patch patch = { 0 };
+			struct strbuf root = STRBUF_INIT;
+			int linenr = 0;
+
 			in_header = 0;
 			strbuf_addch(&buf, '\n');
 			if (!util->diff_offset)
 				util->diff_offset = buf.len;
-			strbuf_addch(&buf, ' ');
-			strbuf_addstr(&buf, line);
+			line[len - 1] = '\n';
+			len = parse_git_diff_header(&root, &linenr, 1, line,
+						    len, size, &patch);
+			if (len < 0)
+				die(_("could not parse git header '%.*s'"), (int)len, line);
+			strbuf_addstr(&buf, " ## ");
+			if (patch.is_new > 0)
+				strbuf_addf(&buf, "%s (new)", patch.new_name);
+			else if (patch.is_delete > 0)
+				strbuf_addf(&buf, "%s (deleted)", patch.old_name);
+			else if (patch.is_rename)
+				strbuf_addf(&buf, "%s => %s", patch.old_name, patch.new_name);
+			else
+				strbuf_addstr(&buf, patch.new_name);
+
+			if (patch.new_mode && patch.old_mode &&
+			    patch.old_mode != patch.new_mode)
+				strbuf_addf(&buf, " (mode change %06o => %06o)",
+					    patch.old_mode, patch.new_mode);
+
+			strbuf_addstr(&buf, " ##");
 		} else if (in_header) {
 			if (starts_with(line, "Author: ")) {
 				strbuf_addstr(&buf, line);
@@ -122,17 +146,13 @@ static int read_patches(const char *range, struct string_list *list)
 		} else if (skip_prefix(line, "@@ ", &p)) {
 			p = strstr(p, "@@");
 			strbuf_addstr(&buf, p ? p : "@@");
-		} else if (!line[0] || starts_with(line, "index "))
+		} else if (!line[0])
 			/*
 			 * A completely blank (not ' \n', which is context)
 			 * line is not valid in a diff.  We skip it
 			 * silently, because this neatly handles the blank
 			 * separator line between commits in git-log
 			 * output.
-			 *
-			 * We also want to ignore the diff's `index` lines
-			 * because they contain exact blob hashes in which
-			 * we are not interested.
 			 */
 			continue;
 		else if (line[0] == '>') {
diff --git a/t/t3206-range-diff.sh b/t/t3206-range-diff.sh
index 9f89af7178..c277756057 100755
--- a/t/t3206-range-diff.sh
+++ b/t/t3206-range-diff.sh
@@ -181,6 +181,85 @@ test_expect_success 'changed commit with sm config' '
 	test_cmp expected actual
 '
 
+test_expect_success 'renamed file' '
+	git range-diff --no-color --submodule=log topic...renamed-file >actual &&
+	sed s/Z/\ /g >expected <<-EOF &&
+	1:  4de457d = 1:  f258d75 s/5/A/
+	2:  fccce22 ! 2:  017b62d s/4/A/
+	    @@
+	    ZAuthor: Thomas Rast <trast@inf.ethz.ch>
+	    Z
+	    -    s/4/A/
+	    +    s/4/A/ + rename file
+	    Z
+	    - ## file ##
+	    + ## file => renamed-file ##
+	    Z@@
+	    Z 1
+	    Z 2
+	3:  147e64e ! 3:  3ce7af6 s/11/B/
+	    @@
+	    Z
+	    Z    s/11/B/
+	    Z
+	    - ## file ##
+	    + ## renamed-file ##
+	    Z@@ A
+	    Z 8
+	    Z 9
+	4:  a63e992 ! 4:  1e6226b s/12/B/
+	    @@
+	    Z
+	    Z    s/12/B/
+	    Z
+	    - ## file ##
+	    + ## renamed-file ##
+	    Z@@ A
+	    Z 9
+	    Z 10
+	EOF
+	test_cmp expected actual
+'
+
+test_expect_success 'file added and later removed' '
+	git range-diff --no-color --submodule=log topic...added-removed >actual &&
+	sed s/Z/\ /g >expected <<-EOF &&
+	1:  4de457d = 1:  096b1ba s/5/A/
+	2:  fccce22 ! 2:  d92e698 s/4/A/
+	    @@
+	    ZAuthor: Thomas Rast <trast@inf.ethz.ch>
+	    Z
+	    -    s/4/A/
+	    +    s/4/A/ + new-file
+	    Z
+	    Z ## file ##
+	    Z@@
+	    @@
+	    Z A
+	    Z 6
+	    Z 7
+	    +
+	    + ## new-file (new) ##
+	3:  147e64e ! 3:  9a1db4d s/11/B/
+	    @@
+	    ZAuthor: Thomas Rast <trast@inf.ethz.ch>
+	    Z
+	    -    s/11/B/
+	    +    s/11/B/ + remove file
+	    Z
+	    Z ## file ##
+	    Z@@ A
+	    @@
+	    Z 12
+	    Z 13
+	    Z 14
+	    +
+	    + ## new-file (deleted) ##
+	4:  a63e992 = 4:  fea3b5c s/12/B/
+	EOF
+	test_cmp expected actual
+'
+
 test_expect_success 'no commits on one side' '
 	git commit --amend -m "new message" &&
 	git range-diff master HEAD@{1} HEAD
@@ -197,9 +276,9 @@ test_expect_success 'changed message' '
 	    Z
 	    +    Also a silly comment here!
 	    +
-	    Z diff --git a/file b/file
-	    Z --- a/file
-	    Z +++ b/file
+	    Z ## file ##
+	    Z@@
+	    Z 1
 	3:  147e64e = 3:  b9cb956 s/11/B/
 	4:  a63e992 = 4:  8add5f1 s/12/B/
 	EOF
@@ -216,9 +295,9 @@ test_expect_success 'dual-coloring' '
 	:     <RESET>
 	:    <REVERSE><GREEN>+<RESET><BOLD>    Also a silly comment here!<RESET>
 	:    <REVERSE><GREEN>+<RESET>
-	:      diff --git a/file b/file<RESET>
-	:      --- a/file<RESET>
-	:      +++ b/file<RESET>
+	:      ## file ##<RESET>
+	:    <CYAN> @@<RESET>
+	:      1<RESET>
 	:<RED>3:  0559556 <RESET><YELLOW>!<RESET><GREEN> 3:  b9cb956<RESET><YELLOW> s/11/B/<RESET>
 	:    <REVERSE><CYAN>@@<RESET>
 	:      9<RESET>
diff --git a/t/t3206/history.export b/t/t3206/history.export
index b8ffff0940..7bb3814962 100644
--- a/t/t3206/history.export
+++ b/t/t3206/history.export
@@ -22,8 +22,8 @@ data 51
 19
 20
 
-reset refs/heads/removed
-commit refs/heads/removed
+reset refs/heads/renamed-file
+commit refs/heads/renamed-file
 mark :2
 author Thomas Rast <trast@inf.ethz.ch> 1374424921 +0200
 committer Thomas Rast <trast@inf.ethz.ch> 1374484724 +0200
@@ -599,6 +599,82 @@ s/12/B/
 from :46
 M 100644 :28 file
 
-reset refs/heads/removed
-from :47
+commit refs/heads/added-removed
+mark :48
+author Thomas Rast <trast@inf.ethz.ch> 1374485014 +0200
+committer Thomas Gummerer <t.gummerer@gmail.com> 1556574151 +0100
+data 7
+s/5/A/
+from :2
+M 100644 :3 file
+
+blob
+mark :49
+data 0
+
+commit refs/heads/added-removed
+mark :50
+author Thomas Rast <trast@inf.ethz.ch> 1374485024 +0200
+committer Thomas Gummerer <t.gummerer@gmail.com> 1556574177 +0100
+data 18
+s/4/A/ + new-file
+from :48
+M 100644 :5 file
+M 100644 :49 new-file
+
+commit refs/heads/added-removed
+mark :51
+author Thomas Rast <trast@inf.ethz.ch> 1374485036 +0200
+committer Thomas Gummerer <t.gummerer@gmail.com> 1556574177 +0100
+data 22
+s/11/B/ + remove file
+from :50
+M 100644 :7 file
+D new-file
+
+commit refs/heads/added-removed
+mark :52
+author Thomas Rast <trast@inf.ethz.ch> 1374485044 +0200
+committer Thomas Gummerer <t.gummerer@gmail.com> 1556574177 +0100
+data 8
+s/12/B/
+from :51
+M 100644 :9 file
+
+commit refs/heads/renamed-file
+mark :53
+author Thomas Rast <trast@inf.ethz.ch> 1374485014 +0200
+committer Thomas Gummerer <t.gummerer@gmail.com> 1556574309 +0100
+data 7
+s/5/A/
+from :2
+M 100644 :3 file
+
+commit refs/heads/renamed-file
+mark :54
+author Thomas Rast <trast@inf.ethz.ch> 1374485024 +0200
+committer Thomas Gummerer <t.gummerer@gmail.com> 1556574312 +0100
+data 21
+s/4/A/ + rename file
+from :53
+D file
+M 100644 :5 renamed-file
+
+commit refs/heads/renamed-file
+mark :55
+author Thomas Rast <trast@inf.ethz.ch> 1374485036 +0200
+committer Thomas Gummerer <t.gummerer@gmail.com> 1556574319 +0100
+data 8
+s/11/B/
+from :54
+M 100644 :7 renamed-file
+
+commit refs/heads/renamed-file
+mark :56
+author Thomas Rast <trast@inf.ethz.ch> 1374485044 +0200
+committer Thomas Gummerer <t.gummerer@gmail.com> 1556574319 +0100
+data 8
+s/12/B/
+from :55
+M 100644 :9 renamed-file
 
-- 
2.22.0.510.g264f2c817a
Thomas Gummerer· Jul 8, 2019, 16:33 UTC · re: Thomas Gummerer · lore

[PATCH v3 14/14] range-diff: add headers to the outer hunk header

Add the section headers/hunk headers we introduced in the previous commits to the outer diff's hunk headers. This makes it easier to understand which change we are actually looking at. For example an outer hunk header might now look like:

    @@  Documentation/config/interactive.txt
while previously it would have only been
    @@
which doesn't give a lot of context for the change that follows.

For completeness also add section headers for the commit metadata and the commit message, although they are arguably less important.

Signed-off-by: Thomas Gummerer <t.gummerer@gmail.com>
---
 range-diff.c          |  9 ++++++---
 t/t3206-range-diff.sh | 41 ++++++++++++++++++++++-------------------
 2 files changed, 28 insertions(+), 22 deletions(-)
Show changes to 2 files +28 −22

range-diff.c, t/t3206-range-diff.sh

diff --git a/range-diff.c b/range-diff.c
index 7a96a587f1..ba1e9a4265 100644
--- a/range-diff.c
+++ b/range-diff.c
@@ -139,8 +139,10 @@ static int read_patches(const char *range, struct string_list *list)
 			strbuf_addstr(&buf, " ##");
 		} else if (in_header) {
 			if (starts_with(line, "Author: ")) {
+				strbuf_addstr(&buf, " ## Metadata ##\n");
 				strbuf_addstr(&buf, line);
 				strbuf_addstr(&buf, "\n\n");
+				strbuf_addstr(&buf, " ## Commit message ##\n");
 			} else if (starts_with(line, "    ")) {
 				p = line + len - 2;
 				while (isspace(*p) && p >= line)
@@ -402,8 +404,9 @@ static void output_pair_header(struct diff_options *diffopt,
 	fwrite(buf->buf, buf->len, 1, diffopt->file);
 }
 
-static struct userdiff_driver no_func_name = {
-	.funcname = { "$^", 0 }
+static struct userdiff_driver section_headers = {
+	.funcname = { "^ ## (.*) ##$\n"
+		      "^.?@@ (.*)$", REG_EXTENDED }
 };
 
 static struct diff_filespec *get_filespec(const char *name, const char *p)
@@ -415,7 +418,7 @@ static struct diff_filespec *get_filespec(const char *name, const char *p)
 	spec->size = strlen(p);
 	spec->should_munmap = 0;
 	spec->is_stdin = 1;
-	spec->driver = &no_func_name;
+	spec->driver = &section_headers;
 
 	return spec;
 }
diff --git a/t/t3206-range-diff.sh b/t/t3206-range-diff.sh
index d4de270979..ec548654ce 100755
--- a/t/t3206-range-diff.sh
+++ b/t/t3206-range-diff.sh
@@ -99,7 +99,7 @@ test_expect_success 'changed commit' '
 	1:  4de457d = 1:  a4b3333 s/5/A/
 	2:  fccce22 = 2:  f51d370 s/4/A/
 	3:  147e64e ! 3:  0559556 s/11/B/
-	    @@
+	    @@ file: A
 	      9
 	      10
 	     -11
@@ -109,7 +109,7 @@ test_expect_success 'changed commit' '
 	      13
 	      14
 	4:  a63e992 ! 4:  d966c5c s/12/B/
-	    @@
+	    @@ file
 	     @@ file: A
 	      9
 	      10
@@ -158,7 +158,7 @@ test_expect_success 'changed commit with sm config' '
 	1:  4de457d = 1:  a4b3333 s/5/A/
 	2:  fccce22 = 2:  f51d370 s/4/A/
 	3:  147e64e ! 3:  0559556 s/11/B/
-	    @@
+	    @@ file: A
 	      9
 	      10
 	     -11
@@ -168,7 +168,7 @@ test_expect_success 'changed commit with sm config' '
 	      13
 	      14
 	4:  a63e992 ! 4:  d966c5c s/12/B/
-	    @@
+	    @@ file
 	     @@ file: A
 	      9
 	      10
@@ -186,9 +186,10 @@ test_expect_success 'renamed file' '
 	sed s/Z/\ /g >expected <<-EOF &&
 	1:  4de457d = 1:  f258d75 s/5/A/
 	2:  fccce22 ! 2:  017b62d s/4/A/
-	    @@
+	    @@ Metadata
 	    ZAuthor: Thomas Rast <trast@inf.ethz.ch>
 	    Z
+	    Z ## Commit message ##
 	    -    s/4/A/
 	    +    s/4/A/ + rename file
 	    Z
@@ -198,8 +199,8 @@ test_expect_success 'renamed file' '
 	    Z 1
 	    Z 2
 	3:  147e64e ! 3:  3ce7af6 s/11/B/
-	    @@
-	    Z
+	    @@ Metadata
+	    Z ## Commit message ##
 	    Z    s/11/B/
 	    Z
 	    - ## file ##
@@ -210,8 +211,8 @@ test_expect_success 'renamed file' '
 	    Z 9
 	    Z 10
 	4:  a63e992 ! 4:  1e6226b s/12/B/
-	    @@
-	    Z
+	    @@ Metadata
+	    Z ## Commit message ##
 	    Z    s/12/B/
 	    Z
 	    - ## file ##
@@ -230,30 +231,32 @@ test_expect_success 'file added and later removed' '
 	sed s/Z/\ /g >expected <<-EOF &&
 	1:  4de457d = 1:  096b1ba s/5/A/
 	2:  fccce22 ! 2:  d92e698 s/4/A/
-	    @@
+	    @@ Metadata
 	    ZAuthor: Thomas Rast <trast@inf.ethz.ch>
 	    Z
+	    Z ## Commit message ##
 	    -    s/4/A/
 	    +    s/4/A/ + new-file
 	    Z
 	    Z ## file ##
 	    Z@@
-	    @@
+	    @@ file
 	    Z A
 	    Z 6
 	    Z 7
 	    +
 	    + ## new-file (new) ##
 	3:  147e64e ! 3:  9a1db4d s/11/B/
-	    @@
+	    @@ Metadata
 	    ZAuthor: Thomas Rast <trast@inf.ethz.ch>
 	    Z
+	    Z ## Commit message ##
 	    -    s/11/B/
 	    +    s/11/B/ + remove file
 	    Z
 	    Z ## file ##
 	    Z@@ file: A
-	    @@
+	    @@ file: A
 	    Z 12
 	    Z 13
 	    Z 14
@@ -274,8 +277,8 @@ test_expect_success 'changed message' '
 	sed s/Z/\ /g >expected <<-EOF &&
 	1:  4de457d = 1:  f686024 s/5/A/
 	2:  fccce22 ! 2:  4ab067d s/4/A/
-	    @@
-	    Z
+	    @@ Metadata
+	    Z ## Commit message ##
 	    Z    s/4/A/
 	    Z
 	    +    Also a silly comment here!
@@ -293,8 +296,8 @@ test_expect_success 'dual-coloring' '
 	sed -e "s|^:||" >expect <<-\EOF &&
 	:<YELLOW>1:  a4b3333 = 1:  f686024 s/5/A/<RESET>
 	:<RED>2:  f51d370 <RESET><YELLOW>!<RESET><GREEN> 2:  4ab067d<RESET><YELLOW> s/4/A/<RESET>
-	:    <REVERSE><CYAN>@@<RESET>
-	:     <RESET>
+	:    <REVERSE><CYAN>@@<RESET> <RESET>Metadata<RESET>
+	:      ## Commit message ##<RESET>
 	:         s/4/A/<RESET>
 	:     <RESET>
 	:    <REVERSE><GREEN>+<RESET><BOLD>    Also a silly comment here!<RESET>
@@ -303,7 +306,7 @@ test_expect_success 'dual-coloring' '
 	:    <CYAN> @@<RESET>
 	:      1<RESET>
 	:<RED>3:  0559556 <RESET><YELLOW>!<RESET><GREEN> 3:  b9cb956<RESET><YELLOW> s/11/B/<RESET>
-	:    <REVERSE><CYAN>@@<RESET>
+	:    <REVERSE><CYAN>@@<RESET> <RESET>file: A<RESET>
 	:      9<RESET>
 	:      10<RESET>
 	:    <RED> -11<RESET>
@@ -313,7 +316,7 @@ test_expect_success 'dual-coloring' '
 	:      13<RESET>
 	:      14<RESET>
 	:<RED>4:  d966c5c <RESET><YELLOW>!<RESET><GREEN> 4:  8add5f1<RESET><YELLOW> s/12/B/<RESET>
-	:    <REVERSE><CYAN>@@<RESET>
+	:    <REVERSE><CYAN>@@<RESET> <RESET>file<RESET>
 	:    <CYAN> @@ file: A<RESET>
 	:      9<RESET>
 	:      10<RESET>
-- 
2.22.0.510.g264f2c817a
Thomas Gummerer· Jul 8, 2019, 16:33 UTC · re: Thomas Gummerer · lore

[PATCH v3 13/14] range-diff: add filename to inner diff

In a range-diff it's not always clear which file a certain funcname of the inner diff belongs to, because the diff header (or section header as added in a previous commit) is not always visible in the range-diff.

Add the filename to the inner diffs header, so it's always visible to users.

This also allows us to add the filename + the funcname to the outer diffs hunk headers using a custom userdiff pattern, which will be done in the next commit.

Signed-off-by: Thomas Gummerer <t.gummerer@gmail.com>
---
 range-diff.c          | 15 +++++++++++++--
 t/t3206-range-diff.sh | 16 ++++++++++------
 2 files changed, 23 insertions(+), 8 deletions(-)
Show changes to 2 files +23 −8

range-diff.c, t/t3206-range-diff.sh

diff --git a/range-diff.c b/range-diff.c
index 5f64380fe4..7a96a587f1 100644
--- a/range-diff.c
+++ b/range-diff.c
@@ -46,7 +46,7 @@ static int read_patches(const char *range, struct string_list *list)
 	struct strbuf buf = STRBUF_INIT, contents = STRBUF_INIT;
 	struct patch_util *util = NULL;
 	int in_header = 1;
-	char *line;
+	char *line, *current_filename = NULL;
 	int offset, len;
 	size_t size;
 
@@ -125,6 +125,12 @@ static int read_patches(const char *range, struct string_list *list)
 			else
 				strbuf_addstr(&buf, patch.new_name);
 
+			free(current_filename);
+			if (patch.is_delete > 0)
+				current_filename = xstrdup(patch.old_name);
+			else
+				current_filename = xstrdup(patch.new_name);
+
 			if (patch.new_mode && patch.old_mode &&
 			    patch.old_mode != patch.new_mode)
 				strbuf_addf(&buf, " (mode change %06o => %06o)",
@@ -145,7 +151,11 @@ static int read_patches(const char *range, struct string_list *list)
 			continue;
 		} else if (skip_prefix(line, "@@ ", &p)) {
 			p = strstr(p, "@@");
-			strbuf_addstr(&buf, p ? p : "@@");
+			strbuf_addstr(&buf, "@@");
+			if (current_filename && p[2])
+				strbuf_addf(&buf, " %s:", current_filename);
+			if (p)
+				strbuf_addstr(&buf, p + 2);
 		} else if (!line[0])
 			/*
 			 * A completely blank (not ' \n', which is context)
@@ -177,6 +187,7 @@ static int read_patches(const char *range, struct string_list *list)
 	if (util)
 		string_list_append(list, buf.buf)->util = util;
 	strbuf_release(&buf);
+	free(current_filename);
 
 	if (finish_command(&cp))
 		return -1;
diff --git a/t/t3206-range-diff.sh b/t/t3206-range-diff.sh
index c277756057..d4de270979 100755
--- a/t/t3206-range-diff.sh
+++ b/t/t3206-range-diff.sh
@@ -110,7 +110,7 @@ test_expect_success 'changed commit' '
 	      14
 	4:  a63e992 ! 4:  d966c5c s/12/B/
 	    @@
-	     @@ A
+	     @@ file: A
 	      9
 	      10
 	    - B
@@ -169,7 +169,7 @@ test_expect_success 'changed commit with sm config' '
 	      14
 	4:  a63e992 ! 4:  d966c5c s/12/B/
 	    @@
-	     @@ A
+	     @@ file: A
 	      9
 	      10
 	    - B
@@ -203,20 +203,24 @@ test_expect_success 'renamed file' '
 	    Z    s/11/B/
 	    Z
 	    - ## file ##
+	    -@@ file: A
 	    + ## renamed-file ##
-	    Z@@ A
+	    +@@ renamed-file: A
 	    Z 8
 	    Z 9
+	    Z 10
 	4:  a63e992 ! 4:  1e6226b s/12/B/
 	    @@
 	    Z
 	    Z    s/12/B/
 	    Z
 	    - ## file ##
+	    -@@ file: A
 	    + ## renamed-file ##
-	    Z@@ A
+	    +@@ renamed-file: A
 	    Z 9
 	    Z 10
+	    Z B
 	EOF
 	test_cmp expected actual
 '
@@ -248,7 +252,7 @@ test_expect_success 'file added and later removed' '
 	    +    s/11/B/ + remove file
 	    Z
 	    Z ## file ##
-	    Z@@ A
+	    Z@@ file: A
 	    @@
 	    Z 12
 	    Z 13
@@ -310,7 +314,7 @@ test_expect_success 'dual-coloring' '
 	:      14<RESET>
 	:<RED>4:  d966c5c <RESET><YELLOW>!<RESET><GREEN> 4:  8add5f1<RESET><YELLOW> s/12/B/<RESET>
 	:    <REVERSE><CYAN>@@<RESET>
-	:    <CYAN> @@ A<RESET>
+	:    <CYAN> @@ file: A<RESET>
 	:      9<RESET>
 	:      10<RESET>
 	:    <REVERSE><RED>-<RESET><FAINT> BB<RESET>
-- 
2.22.0.510.g264f2c817a
Thomas Gummerer· Jul 11, 2019, 16:08 UTC · re: Thomas Gummerer · lore

[PATCH v4 00/14] output improvements for git range-diff

Thanks Junio for the comment on the previous round [1]. This round reanmes the struct we're using in apply.c to 'struct gitdiff_data', and updates the commit message of 7/14 to reflect the new name of the renamed function.

[1]: https://public-inbox.org/git/20190708163315.29912-1-t.gummerer@gmail.com/
Thomas Gummerer (14):
  apply: replace marc.info link with public-inbox
  apply: only pass required data to skip_tree_prefix
  apply: only pass required data to git_header_name
  apply: only pass required data to check_header_line
  apply: only pass required data to find_name_*
  apply: only pass required data to gitdiff_* functions
  apply: make parse_git_diff_header public
  range-diff: fix function parameter indentation
  range-diff: split lines manually
  range-diff: don't remove funcname from inner diff
  range-diff: suppress line count in outer diff
  range-diff: add section header instead of diff header
  range-diff: add filename to inner diff
  range-diff: add headers to the outer hunk header
 apply.c                | 186 ++++++++++++++++++-----------------------
 apply.h                |  48 +++++++++++
 diff.c                 |   5 +-
 diff.h                 |   1 +
 range-diff.c           | 124 +++++++++++++++++++--------
 t/t3206-range-diff.sh  | 124 ++++++++++++++++++++++-----
 t/t3206/history.export |  84 ++++++++++++++++++-
 7 files changed, 409 insertions(+), 163 deletions(-)
Range-diff against v3:
 1:  ef2245edda =  1:  ef2245edda apply: replace marc.info link with public-inbox
 2:  94578fa45c =  2:  94578fa45c apply: only pass required data to skip_tree_prefix
 3:  988269a68e =  3:  988269a68e apply: only pass required data to git_header_name
 4:  a2c1ef3f5f =  4:  a2c1ef3f5f apply: only pass required data to check_header_line
 5:  0f4cfe21cb =  5:  0f4cfe21cb apply: only pass required data to find_name_*
 6:  07a271518d !  6:  42665e5295 apply: only pass required data to gitdiff_* functions
    @@ -28,7 +28,7 @@
      #include "rerere.h"
      #include "apply.h"
      
    -+struct parse_git_header_state {
    ++struct gitdiff_data {
     +	struct strbuf *root;
     +	int linenr;
     +	int p_value;
    @@ -42,7 +42,7 @@
      }
      
     -static int gitdiff_hdrend(struct apply_state *state,
    -+static int gitdiff_hdrend(struct parse_git_header_state *state,
    ++static int gitdiff_hdrend(struct gitdiff_data *state,
      			  const char *line,
      			  struct patch *patch)
      {
    @@ -51,7 +51,7 @@
      #define DIFF_NEW_NAME 1
      
     -static int gitdiff_verify_name(struct apply_state *state,
    -+static int gitdiff_verify_name(struct parse_git_header_state *state,
    ++static int gitdiff_verify_name(struct gitdiff_data *state,
      			       const char *line,
      			       int isnull,
      			       char **name,
    @@ -77,7 +77,7 @@
      }
      
     -static int gitdiff_oldname(struct apply_state *state,
    -+static int gitdiff_oldname(struct parse_git_header_state *state,
    ++static int gitdiff_oldname(struct gitdiff_data *state,
      			   const char *line,
      			   struct patch *patch)
      {
    @@ -86,7 +86,7 @@
      }
      
     -static int gitdiff_newname(struct apply_state *state,
    -+static int gitdiff_newname(struct parse_git_header_state *state,
    ++static int gitdiff_newname(struct gitdiff_data *state,
      			   const char *line,
      			   struct patch *patch)
      {
    @@ -95,7 +95,7 @@
      }
      
     -static int gitdiff_oldmode(struct apply_state *state,
    -+static int gitdiff_oldmode(struct parse_git_header_state *state,
    ++static int gitdiff_oldmode(struct gitdiff_data *state,
      			   const char *line,
      			   struct patch *patch)
      {
    @@ -103,7 +103,7 @@
      }
      
     -static int gitdiff_newmode(struct apply_state *state,
    -+static int gitdiff_newmode(struct parse_git_header_state *state,
    ++static int gitdiff_newmode(struct gitdiff_data *state,
      			   const char *line,
      			   struct patch *patch)
      {
    @@ -111,7 +111,7 @@
      }
      
     -static int gitdiff_delete(struct apply_state *state,
    -+static int gitdiff_delete(struct parse_git_header_state *state,
    ++static int gitdiff_delete(struct gitdiff_data *state,
      			  const char *line,
      			  struct patch *patch)
      {
    @@ -120,7 +120,7 @@
      }
      
     -static int gitdiff_newfile(struct apply_state *state,
    -+static int gitdiff_newfile(struct parse_git_header_state *state,
    ++static int gitdiff_newfile(struct gitdiff_data *state,
      			   const char *line,
      			   struct patch *patch)
      {
    @@ -129,7 +129,7 @@
      }
      
     -static int gitdiff_copysrc(struct apply_state *state,
    -+static int gitdiff_copysrc(struct parse_git_header_state *state,
    ++static int gitdiff_copysrc(struct gitdiff_data *state,
      			   const char *line,
      			   struct patch *patch)
      {
    @@ -141,7 +141,7 @@
      }
      
     -static int gitdiff_copydst(struct apply_state *state,
    -+static int gitdiff_copydst(struct parse_git_header_state *state,
    ++static int gitdiff_copydst(struct gitdiff_data *state,
      			   const char *line,
      			   struct patch *patch)
      {
    @@ -153,7 +153,7 @@
      }
      
     -static int gitdiff_renamesrc(struct apply_state *state,
    -+static int gitdiff_renamesrc(struct parse_git_header_state *state,
    ++static int gitdiff_renamesrc(struct gitdiff_data *state,
      			     const char *line,
      			     struct patch *patch)
      {
    @@ -165,7 +165,7 @@
      }
      
     -static int gitdiff_renamedst(struct apply_state *state,
    -+static int gitdiff_renamedst(struct parse_git_header_state *state,
    ++static int gitdiff_renamedst(struct gitdiff_data *state,
      			     const char *line,
      			     struct patch *patch)
      {
    @@ -177,7 +177,7 @@
      }
      
     -static int gitdiff_similarity(struct apply_state *state,
    -+static int gitdiff_similarity(struct parse_git_header_state *state,
    ++static int gitdiff_similarity(struct gitdiff_data *state,
      			      const char *line,
      			      struct patch *patch)
      {
    @@ -186,7 +186,7 @@
      }
      
     -static int gitdiff_dissimilarity(struct apply_state *state,
    -+static int gitdiff_dissimilarity(struct parse_git_header_state *state,
    ++static int gitdiff_dissimilarity(struct gitdiff_data *state,
      				 const char *line,
      				 struct patch *patch)
      {
    @@ -195,7 +195,7 @@
      }
      
     -static int gitdiff_index(struct apply_state *state,
    -+static int gitdiff_index(struct parse_git_header_state *state,
    ++static int gitdiff_index(struct gitdiff_data *state,
      			 const char *line,
      			 struct patch *patch)
      {
    @@ -204,7 +204,7 @@
       * into the next diff. Tell the parser to break out.
       */
     -static int gitdiff_unrecognized(struct apply_state *state,
    -+static int gitdiff_unrecognized(struct parse_git_header_state *state,
    ++static int gitdiff_unrecognized(struct gitdiff_data *state,
      				const char *line,
      				struct patch *patch)
      {
    @@ -212,7 +212,7 @@
      			    struct patch *patch)
      {
      	unsigned long offset;
    -+	struct parse_git_header_state parse_hdr_state;
    ++	struct gitdiff_data parse_hdr_state;
      
      	/* A git diff has explicit new/delete information, so we don't guess */
      	patch->is_new = 0;
    @@ -228,7 +228,7 @@
      		static const struct opentry {
      			const char *str;
     -			int (*fn)(struct apply_state *, const char *, struct patch *);
    -+			int (*fn)(struct parse_git_header_state *, const char *, struct patch *);
    ++			int (*fn)(struct gitdiff_data *, const char *, struct patch *);
      		} optable[] = {
      			{ "@@ -", gitdiff_hdrend },
      			{ "--- ", gitdiff_oldname },
 7:  9cb6732a5f !  7:  3068fda8a9 apply: make parse_git_header public
    @@ -1,9 +1,12 @@
     Author: Thomas Gummerer <t.gummerer@gmail.com>
     
    -    apply: make parse_git_header public
    +    apply: make parse_git_diff_header public
     
    -    Make parse_git_header a "public" function in apply.h, so we can re-use
    -    it in range-diff in a subsequent commit.
    +    Make 'parse_git_header()' (renamed to 'parse_git_diff_header()') a
    +    "public" function in apply.h, so we can re-use it in range-diff in a
    +    subsequent commit.  We're renaming the function to make it clearer in
    +    other parts of the codebase that we're talking about a diff header and
    +    not just any header.
     
         Signed-off-by: Thomas Gummerer <t.gummerer@gmail.com>
     
    @@ -70,7 +73,7 @@
     +			  struct patch *patch)
      {
      	unsigned long offset;
    - 	struct parse_git_header_state parse_hdr_state;
    + 	struct gitdiff_data parse_hdr_state;
     @@
      	 * or removing or adding empty files), so we get
      	 * the default name from the header.
    @@ -100,7 +103,7 @@
     +	for (offset = len ; size > 0 ; offset += len, size -= len, line += len, (*linenr)++) {
      		static const struct opentry {
      			const char *str;
    - 			int (*fn)(struct parse_git_header_state *, const char *, struct patch *);
    + 			int (*fn)(struct gitdiff_data *, const char *, struct patch *);
     @@
      			res = p->fn(&parse_hdr_state, line + oplen, patch);
      			if (res < 0)
 8:  76a11ce995 =  8:  781d054cab range-diff: fix function parameter indentation
 9:  6f70e7faa6 =  9:  68a2953310 range-diff: split lines manually
10:  6618cdff2c = 10:  8ae95d053b range-diff: don't remove funcname from inner diff
11:  2667df4fa5 = 11:  e572510c52 range-diff: suppress line count in outer diff
12:  47cd8c6733 = 12:  45605db760 range-diff: add section header instead of diff header
13:  f67fd5dd9a = 13:  89a27fbeaa range-diff: add filename to inner diff
14:  812893a5dc = 14:  8bee2c525f range-diff: add headers to the outer hunk header
-- 
2.22.0.510.g264f2c817a
Thomas Gummerer· Jul 11, 2019, 16:08 UTC · re: Thomas Gummerer · lore

[PATCH v4 01/14] apply: replace marc.info link with public-inbox

public-inbox.org links include the whole message ID by default. This means the message can still be found even if the site goes away, which is not the case with the marc.info link. Replace the marc.info link with a more future proof one.

Signed-off-by: Thomas Gummerer <t.gummerer@gmail.com>
---
 apply.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
Show changes to apply.c +1 −1
diff --git a/apply.c b/apply.c
index f15afa9f6a..599cf8956f 100644
--- a/apply.c
+++ b/apply.c
@@ -478,7 +478,7 @@ static char *find_name_gnu(struct apply_state *state,
 
 	/*
 	 * Proposed "new-style" GNU patch/diff format; see
-	 * http://marc.info/?l=git&m=112927316408690&w=2
+	 * https://public-inbox.org/git/7vll0wvb2a.fsf@assigned-by-dhcp.cox.net/
 	 */
 	if (unquote_c_style(&name, line, NULL)) {
 		strbuf_release(&name);
-- 
2.22.0.510.g264f2c817a
Thomas Gummerer· Jul 11, 2019, 16:08 UTC · re: Thomas Gummerer · lore

[PATCH v4 02/14] apply: only pass required data to skip_tree_prefix

Currently the 'skip_tree_prefix()' function takes 'struct apply_state' as parameter, even though it only needs the p_value from that struct.

This function is in the callchain of 'parse_git_header()', which we want to make more generally useful in a subsequent commit. To make that happen we only want to pass in the required data to 'parse_git_header()', and not the whole 'struct apply_state', and thus we want functions in the callchain of 'parse_git_header()' to only take arguments they really need.

Signed-off-by: Thomas Gummerer <t.gummerer@gmail.com>
---
 apply.c | 18 +++++++++---------
 1 file changed, 9 insertions(+), 9 deletions(-)
Show changes to apply.c +9 −9
diff --git a/apply.c b/apply.c
index 599cf8956f..fc7083fcbc 100644
--- a/apply.c
+++ b/apply.c
@@ -1137,17 +1137,17 @@ static int gitdiff_unrecognized(struct apply_state *state,
  * Skip p_value leading components from "line"; as we do not accept
  * absolute paths, return NULL in that case.
  */
-static const char *skip_tree_prefix(struct apply_state *state,
+static const char *skip_tree_prefix(int p_value,
 				    const char *line,
 				    int llen)
 {
 	int nslash;
 	int i;
 
-	if (!state->p_value)
+	if (!p_value)
 		return (llen && line[0] == '/') ? NULL : line;
 
-	nslash = state->p_value;
+	nslash = p_value;
 	for (i = 0; i < llen; i++) {
 		int ch = line[i];
 		if (ch == '/' && --nslash <= 0)
@@ -1184,7 +1184,7 @@ static char *git_header_name(struct apply_state *state,
 			goto free_and_fail1;
 
 		/* strip the a/b prefix including trailing slash */
-		cp = skip_tree_prefix(state, first.buf, first.len);
+		cp = skip_tree_prefix(state->p_value, first.buf, first.len);
 		if (!cp)
 			goto free_and_fail1;
 		strbuf_remove(&first, 0, cp - first.buf);
@@ -1201,7 +1201,7 @@ static char *git_header_name(struct apply_state *state,
 		if (*second == '"') {
 			if (unquote_c_style(&sp, second, NULL))
 				goto free_and_fail1;
-			cp = skip_tree_prefix(state, sp.buf, sp.len);
+			cp = skip_tree_prefix(state->p_value, sp.buf, sp.len);
 			if (!cp)
 				goto free_and_fail1;
 			/* They must match, otherwise ignore */
@@ -1212,7 +1212,7 @@ static char *git_header_name(struct apply_state *state,
 		}
 
 		/* unquoted second */
-		cp = skip_tree_prefix(state, second, line + llen - second);
+		cp = skip_tree_prefix(state->p_value, second, line + llen - second);
 		if (!cp)
 			goto free_and_fail1;
 		if (line + llen - cp != first.len ||
@@ -1227,7 +1227,7 @@ static char *git_header_name(struct apply_state *state,
 	}
 
 	/* unquoted first name */
-	name = skip_tree_prefix(state, line, llen);
+	name = skip_tree_prefix(state->p_value, line, llen);
 	if (!name)
 		return NULL;
 
@@ -1243,7 +1243,7 @@ static char *git_header_name(struct apply_state *state,
 			if (unquote_c_style(&sp, second, NULL))
 				goto free_and_fail2;
 
-			np = skip_tree_prefix(state, sp.buf, sp.len);
+			np = skip_tree_prefix(state->p_value, sp.buf, sp.len);
 			if (!np)
 				goto free_and_fail2;
 
@@ -1287,7 +1287,7 @@ static char *git_header_name(struct apply_state *state,
 			 */
 			if (!name[len + 1])
 				return NULL; /* no postimage name */
-			second = skip_tree_prefix(state, name + len + 1,
+			second = skip_tree_prefix(state->p_value, name + len + 1,
 						  line_len - (len + 1));
 			if (!second)
 				return NULL;
-- 
2.22.0.510.g264f2c817a
Thomas Gummerer· Jul 11, 2019, 16:08 UTC · re: Thomas Gummerer · lore

[PATCH v4 04/14] apply: only pass required data to check_header_line

Currently the 'check_header_line()' function takes 'struct apply_state' as parameter, even though it only needs the linenr from that struct.

This function is in the callchain of 'parse_git_header()', which we want to make more generally useful in a subsequent commit. To make that happen we only want to pass in the required data to 'parse_git_header()', and not the whole 'struct apply_state', and thus we want functions in the callchain of 'parse_git_header()' to only take arguments they really need.

Signed-off-by: Thomas Gummerer <t.gummerer@gmail.com>
---
 apply.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)
Show changes to apply.c +4 −4
diff --git a/apply.c b/apply.c
index ac668e754d..1602fd5db0 100644
--- a/apply.c
+++ b/apply.c
@@ -1302,15 +1302,15 @@ static char *git_header_name(int p_value,
 	}
 }
 
-static int check_header_line(struct apply_state *state, struct patch *patch)
+static int check_header_line(int linenr, struct patch *patch)
 {
 	int extensions = (patch->is_delete == 1) + (patch->is_new == 1) +
 			 (patch->is_rename == 1) + (patch->is_copy == 1);
 	if (extensions > 1)
 		return error(_("inconsistent header lines %d and %d"),
-			     patch->extension_linenr, state->linenr);
+			     patch->extension_linenr, linenr);
 	if (extensions && !patch->extension_linenr)
-		patch->extension_linenr = state->linenr;
+		patch->extension_linenr = linenr;
 	return 0;
 }
 
@@ -1380,7 +1380,7 @@ static int parse_git_header(struct apply_state *state,
 			res = p->fn(state, line + oplen, patch);
 			if (res < 0)
 				return -1;
-			if (check_header_line(state, patch))
+			if (check_header_line(state->linenr, patch))
 				return -1;
 			if (res > 0)
 				return offset;
-- 
2.22.0.510.g264f2c817a
Thomas Gummerer· Jul 11, 2019, 16:08 UTC · re: Thomas Gummerer · lore

[PATCH v4 03/14] apply: only pass required data to git_header_name

Currently the 'git_header_name()' function takes 'struct apply_state' as parameter, even though it only needs the p_value from that struct.

This function is in the callchain of 'parse_git_header()', which we want to make more generally useful in a subsequent commit. To make that happen we only want to pass in the required data to 'parse_git_header()', and not the whole 'struct apply_state', and thus we want functions in the callchain of 'parse_git_header()' to only take arguments they really need.

Signed-off-by: Thomas Gummerer <t.gummerer@gmail.com>
---
 apply.c | 16 ++++++++--------
 1 file changed, 8 insertions(+), 8 deletions(-)
Show changes to apply.c +8 −8
diff --git a/apply.c b/apply.c
index fc7083fcbc..ac668e754d 100644
--- a/apply.c
+++ b/apply.c
@@ -1164,7 +1164,7 @@ static const char *skip_tree_prefix(int p_value,
  * creation or deletion of an empty file.  In any of these cases,
  * both sides are the same name under a/ and b/ respectively.
  */
-static char *git_header_name(struct apply_state *state,
+static char *git_header_name(int p_value,
 			     const char *line,
 			     int llen)
 {
@@ -1184,7 +1184,7 @@ static char *git_header_name(struct apply_state *state,
 			goto free_and_fail1;
 
 		/* strip the a/b prefix including trailing slash */
-		cp = skip_tree_prefix(state->p_value, first.buf, first.len);
+		cp = skip_tree_prefix(p_value, first.buf, first.len);
 		if (!cp)
 			goto free_and_fail1;
 		strbuf_remove(&first, 0, cp - first.buf);
@@ -1201,7 +1201,7 @@ static char *git_header_name(struct apply_state *state,
 		if (*second == '"') {
 			if (unquote_c_style(&sp, second, NULL))
 				goto free_and_fail1;
-			cp = skip_tree_prefix(state->p_value, sp.buf, sp.len);
+			cp = skip_tree_prefix(p_value, sp.buf, sp.len);
 			if (!cp)
 				goto free_and_fail1;
 			/* They must match, otherwise ignore */
@@ -1212,7 +1212,7 @@ static char *git_header_name(struct apply_state *state,
 		}
 
 		/* unquoted second */
-		cp = skip_tree_prefix(state->p_value, second, line + llen - second);
+		cp = skip_tree_prefix(p_value, second, line + llen - second);
 		if (!cp)
 			goto free_and_fail1;
 		if (line + llen - cp != first.len ||
@@ -1227,7 +1227,7 @@ static char *git_header_name(struct apply_state *state,
 	}
 
 	/* unquoted first name */
-	name = skip_tree_prefix(state->p_value, line, llen);
+	name = skip_tree_prefix(p_value, line, llen);
 	if (!name)
 		return NULL;
 
@@ -1243,7 +1243,7 @@ static char *git_header_name(struct apply_state *state,
 			if (unquote_c_style(&sp, second, NULL))
 				goto free_and_fail2;
 
-			np = skip_tree_prefix(state->p_value, sp.buf, sp.len);
+			np = skip_tree_prefix(p_value, sp.buf, sp.len);
 			if (!np)
 				goto free_and_fail2;
 
@@ -1287,7 +1287,7 @@ static char *git_header_name(struct apply_state *state,
 			 */
 			if (!name[len + 1])
 				return NULL; /* no postimage name */
-			second = skip_tree_prefix(state->p_value, name + len + 1,
+			second = skip_tree_prefix(p_value, name + len + 1,
 						  line_len - (len + 1));
 			if (!second)
 				return NULL;
@@ -1333,7 +1333,7 @@ static int parse_git_header(struct apply_state *state,
 	 * or removing or adding empty files), so we get
 	 * the default name from the header.
 	 */
-	patch->def_name = git_header_name(state, line, len);
+	patch->def_name = git_header_name(state->p_value, line, len);
 	if (patch->def_name && state->root.len) {
 		char *s = xstrfmt("%s%s", state->root.buf, patch->def_name);
 		free(patch->def_name);
-- 
2.22.0.510.g264f2c817a
Thomas Gummerer· Jul 11, 2019, 16:08 UTC · re: Thomas Gummerer · lore

[PATCH v4 05/14] apply: only pass required data to find_name_*

Currently the 'find_name_*()' functions take 'struct apply_state' as parameter, even though they only need the 'root' member from that struct.

These functions are in the callchain of 'parse_git_header()', which we want to make more generally useful in a subsequent commit. To make that happen we only want to pass in the required data to 'parse_git_header()', and not the whole 'struct apply_state', and thus we want functions in the callchain of 'parse_git_header()' to only take arguments they really need.

Signed-off-by: Thomas Gummerer <t.gummerer@gmail.com>
---
 apply.c | 48 ++++++++++++++++++++++++------------------------
 1 file changed, 24 insertions(+), 24 deletions(-)
Show changes to apply.c +24 −24
diff --git a/apply.c b/apply.c
index 1602fd5db0..3cd4e3d3b3 100644
--- a/apply.c
+++ b/apply.c
@@ -469,7 +469,7 @@ static char *squash_slash(char *name)
 	return name;
 }
 
-static char *find_name_gnu(struct apply_state *state,
+static char *find_name_gnu(struct strbuf *root,
 			   const char *line,
 			   int p_value)
 {
@@ -495,8 +495,8 @@ static char *find_name_gnu(struct apply_state *state,
 	}
 
 	strbuf_remove(&name, 0, cp - name.buf);
-	if (state->root.len)
-		strbuf_insert(&name, 0, state->root.buf, state->root.len);
+	if (root->len)
+		strbuf_insert(&name, 0, root->buf, root->len);
 	return squash_slash(strbuf_detach(&name, NULL));
 }
 
@@ -659,7 +659,7 @@ static size_t diff_timestamp_len(const char *line, size_t len)
 	return line + len - end;
 }
 
-static char *find_name_common(struct apply_state *state,
+static char *find_name_common(struct strbuf *root,
 			      const char *line,
 			      const char *def,
 			      int p_value,
@@ -702,30 +702,30 @@ static char *find_name_common(struct apply_state *state,
 			return squash_slash(xstrdup(def));
 	}
 
-	if (state->root.len) {
-		char *ret = xstrfmt("%s%.*s", state->root.buf, len, start);
+	if (root->len) {
+		char *ret = xstrfmt("%s%.*s", root->buf, len, start);
 		return squash_slash(ret);
 	}
 
 	return squash_slash(xmemdupz(start, len));
 }
 
-static char *find_name(struct apply_state *state,
+static char *find_name(struct strbuf *root,
 		       const char *line,
 		       char *def,
 		       int p_value,
 		       int terminate)
 {
 	if (*line == '"') {
-		char *name = find_name_gnu(state, line, p_value);
+		char *name = find_name_gnu(root, line, p_value);
 		if (name)
 			return name;
 	}
 
-	return find_name_common(state, line, def, p_value, NULL, terminate);
+	return find_name_common(root, line, def, p_value, NULL, terminate);
 }
 
-static char *find_name_traditional(struct apply_state *state,
+static char *find_name_traditional(struct strbuf *root,
 				   const char *line,
 				   char *def,
 				   int p_value)
@@ -734,7 +734,7 @@ static char *find_name_traditional(struct apply_state *state,
 	size_t date_len;
 
 	if (*line == '"') {
-		char *name = find_name_gnu(state, line, p_value);
+		char *name = find_name_gnu(root, line, p_value);
 		if (name)
 			return name;
 	}
@@ -742,10 +742,10 @@ static char *find_name_traditional(struct apply_state *state,
 	len = strchrnul(line, '\n') - line;
 	date_len = diff_timestamp_len(line, len);
 	if (!date_len)
-		return find_name_common(state, line, def, p_value, NULL, TERM_TAB);
+		return find_name_common(root, line, def, p_value, NULL, TERM_TAB);
 	len -= date_len;
 
-	return find_name_common(state, line, def, p_value, line + len, 0);
+	return find_name_common(root, line, def, p_value, line + len, 0);
 }
 
 /*
@@ -759,7 +759,7 @@ static int guess_p_value(struct apply_state *state, const char *nameline)
 
 	if (is_dev_null(nameline))
 		return -1;
-	name = find_name_traditional(state, nameline, NULL, 0);
+	name = find_name_traditional(&state->root, nameline, NULL, 0);
 	if (!name)
 		return -1;
 	cp = strchr(name, '/');
@@ -883,17 +883,17 @@ static int parse_traditional_patch(struct apply_state *state,
 	if (is_dev_null(first)) {
 		patch->is_new = 1;
 		patch->is_delete = 0;
-		name = find_name_traditional(state, second, NULL, state->p_value);
+		name = find_name_traditional(&state->root, second, NULL, state->p_value);
 		patch->new_name = name;
 	} else if (is_dev_null(second)) {
 		patch->is_new = 0;
 		patch->is_delete = 1;
-		name = find_name_traditional(state, first, NULL, state->p_value);
+		name = find_name_traditional(&state->root, first, NULL, state->p_value);
 		patch->old_name = name;
 	} else {
 		char *first_name;
-		first_name = find_name_traditional(state, first, NULL, state->p_value);
-		name = find_name_traditional(state, second, first_name, state->p_value);
+		first_name = find_name_traditional(&state->root, first, NULL, state->p_value);
+		name = find_name_traditional(&state->root, second, first_name, state->p_value);
 		free(first_name);
 		if (has_epoch_timestamp(first)) {
 			patch->is_new = 1;
@@ -940,7 +940,7 @@ static int gitdiff_verify_name(struct apply_state *state,
 			       int side)
 {
 	if (!*name && !isnull) {
-		*name = find_name(state, line, NULL, state->p_value, TERM_TAB);
+		*name = find_name(&state->root, line, NULL, state->p_value, TERM_TAB);
 		return 0;
 	}
 
@@ -949,7 +949,7 @@ static int gitdiff_verify_name(struct apply_state *state,
 		if (isnull)
 			return error(_("git apply: bad git-diff - expected /dev/null, got %s on line %d"),
 				     *name, state->linenr);
-		another = find_name(state, line, NULL, state->p_value, TERM_TAB);
+		another = find_name(&state->root, line, NULL, state->p_value, TERM_TAB);
 		if (!another || strcmp(another, *name)) {
 			free(another);
 			return error((side == DIFF_NEW_NAME) ?
@@ -1032,7 +1032,7 @@ static int gitdiff_copysrc(struct apply_state *state,
 {
 	patch->is_copy = 1;
 	free(patch->old_name);
-	patch->old_name = find_name(state, line, NULL, state->p_value ? state->p_value - 1 : 0, 0);
+	patch->old_name = find_name(&state->root, line, NULL, state->p_value ? state->p_value - 1 : 0, 0);
 	return 0;
 }
 
@@ -1042,7 +1042,7 @@ static int gitdiff_copydst(struct apply_state *state,
 {
 	patch->is_copy = 1;
 	free(patch->new_name);
-	patch->new_name = find_name(state, line, NULL, state->p_value ? state->p_value - 1 : 0, 0);
+	patch->new_name = find_name(&state->root, line, NULL, state->p_value ? state->p_value - 1 : 0, 0);
 	return 0;
 }
 
@@ -1052,7 +1052,7 @@ static int gitdiff_renamesrc(struct apply_state *state,
 {
 	patch->is_rename = 1;
 	free(patch->old_name);
-	patch->old_name = find_name(state, line, NULL, state->p_value ? state->p_value - 1 : 0, 0);
+	patch->old_name = find_name(&state->root, line, NULL, state->p_value ? state->p_value - 1 : 0, 0);
 	return 0;
 }
 
@@ -1062,7 +1062,7 @@ static int gitdiff_renamedst(struct apply_state *state,
 {
 	patch->is_rename = 1;
 	free(patch->new_name);
-	patch->new_name = find_name(state, line, NULL, state->p_value ? state->p_value - 1 : 0, 0);
+	patch->new_name = find_name(&state->root, line, NULL, state->p_value ? state->p_value - 1 : 0, 0);
 	return 0;
 }
 
-- 
2.22.0.510.g264f2c817a
Thomas Gummerer· Jul 11, 2019, 16:08 UTC · re: Thomas Gummerer · lore

[PATCH v4 06/14] apply: only pass required data to gitdiff_* functions

Currently the 'gitdiff_*()' functions take 'struct apply_state' as parameter, even though they only needs the root, linenr and p_value from that struct.

These functions are in the callchain of 'parse_git_header()', which we want to make more generally useful in a subsequent commit. To make that happen we only want to pass in the required data to 'parse_git_header()', and not the whole 'struct apply_state', and thus we want functions in the callchain of 'parse_git_header()' to only take arguments they really need.

As these functions are called in a loop using their function pointers, each function needs to be passed all the parameters even if only one of the functions actually needs it. We therefore pass this data along in a struct to avoid adding too many unused parameters to each function and making the code very verbose in the process.

Signed-off-by: Thomas Gummerer <t.gummerer@gmail.com>
---
 apply.c | 59 ++++++++++++++++++++++++++++++++++-----------------------
 1 file changed, 35 insertions(+), 24 deletions(-)
Show changes to apply.c +35 −24
diff --git a/apply.c b/apply.c
index 3cd4e3d3b3..034d134dd5 100644
--- a/apply.c
+++ b/apply.c
@@ -22,6 +22,12 @@
 #include "rerere.h"
 #include "apply.h"
 
+struct gitdiff_data {
+	struct strbuf *root;
+	int linenr;
+	int p_value;
+};
+
 static void git_apply_config(void)
 {
 	git_config_get_string_const("apply.whitespace", &apply_default_whitespace);
@@ -914,7 +920,7 @@ static int parse_traditional_patch(struct apply_state *state,
 	return 0;
 }
 
-static int gitdiff_hdrend(struct apply_state *state,
+static int gitdiff_hdrend(struct gitdiff_data *state,
 			  const char *line,
 			  struct patch *patch)
 {
@@ -933,14 +939,14 @@ static int gitdiff_hdrend(struct apply_state *state,
 #define DIFF_OLD_NAME 0
 #define DIFF_NEW_NAME 1
 
-static int gitdiff_verify_name(struct apply_state *state,
+static int gitdiff_verify_name(struct gitdiff_data *state,
 			       const char *line,
 			       int isnull,
 			       char **name,
 			       int side)
 {
 	if (!*name && !isnull) {
-		*name = find_name(&state->root, line, NULL, state->p_value, TERM_TAB);
+		*name = find_name(state->root, line, NULL, state->p_value, TERM_TAB);
 		return 0;
 	}
 
@@ -949,7 +955,7 @@ static int gitdiff_verify_name(struct apply_state *state,
 		if (isnull)
 			return error(_("git apply: bad git-diff - expected /dev/null, got %s on line %d"),
 				     *name, state->linenr);
-		another = find_name(&state->root, line, NULL, state->p_value, TERM_TAB);
+		another = find_name(state->root, line, NULL, state->p_value, TERM_TAB);
 		if (!another || strcmp(another, *name)) {
 			free(another);
 			return error((side == DIFF_NEW_NAME) ?
@@ -965,7 +971,7 @@ static int gitdiff_verify_name(struct apply_state *state,
 	return 0;
 }
 
-static int gitdiff_oldname(struct apply_state *state,
+static int gitdiff_oldname(struct gitdiff_data *state,
 			   const char *line,
 			   struct patch *patch)
 {
@@ -974,7 +980,7 @@ static int gitdiff_oldname(struct apply_state *state,
 				   DIFF_OLD_NAME);
 }
 
-static int gitdiff_newname(struct apply_state *state,
+static int gitdiff_newname(struct gitdiff_data *state,
 			   const char *line,
 			   struct patch *patch)
 {
@@ -992,21 +998,21 @@ static int parse_mode_line(const char *line, int linenr, unsigned int *mode)
 	return 0;
 }
 
-static int gitdiff_oldmode(struct apply_state *state,
+static int gitdiff_oldmode(struct gitdiff_data *state,
 			   const char *line,
 			   struct patch *patch)
 {
 	return parse_mode_line(line, state->linenr, &patch->old_mode);
 }
 
-static int gitdiff_newmode(struct apply_state *state,
+static int gitdiff_newmode(struct gitdiff_data *state,
 			   const char *line,
 			   struct patch *patch)
 {
 	return parse_mode_line(line, state->linenr, &patch->new_mode);
 }
 
-static int gitdiff_delete(struct apply_state *state,
+static int gitdiff_delete(struct gitdiff_data *state,
 			  const char *line,
 			  struct patch *patch)
 {
@@ -1016,7 +1022,7 @@ static int gitdiff_delete(struct apply_state *state,
 	return gitdiff_oldmode(state, line, patch);
 }
 
-static int gitdiff_newfile(struct apply_state *state,
+static int gitdiff_newfile(struct gitdiff_data *state,
 			   const char *line,
 			   struct patch *patch)
 {
@@ -1026,47 +1032,47 @@ static int gitdiff_newfile(struct apply_state *state,
 	return gitdiff_newmode(state, line, patch);
 }
 
-static int gitdiff_copysrc(struct apply_state *state,
+static int gitdiff_copysrc(struct gitdiff_data *state,
 			   const char *line,
 			   struct patch *patch)
 {
 	patch->is_copy = 1;
 	free(patch->old_name);
-	patch->old_name = find_name(&state->root, line, NULL, state->p_value ? state->p_value - 1 : 0, 0);
+	patch->old_name = find_name(state->root, line, NULL, state->p_value ? state->p_value - 1 : 0, 0);
 	return 0;
 }
 
-static int gitdiff_copydst(struct apply_state *state,
+static int gitdiff_copydst(struct gitdiff_data *state,
 			   const char *line,
 			   struct patch *patch)
 {
 	patch->is_copy = 1;
 	free(patch->new_name);
-	patch->new_name = find_name(&state->root, line, NULL, state->p_value ? state->p_value - 1 : 0, 0);
+	patch->new_name = find_name(state->root, line, NULL, state->p_value ? state->p_value - 1 : 0, 0);
 	return 0;
 }
 
-static int gitdiff_renamesrc(struct apply_state *state,
+static int gitdiff_renamesrc(struct gitdiff_data *state,
 			     const char *line,
 			     struct patch *patch)
 {
 	patch->is_rename = 1;
 	free(patch->old_name);
-	patch->old_name = find_name(&state->root, line, NULL, state->p_value ? state->p_value - 1 : 0, 0);
+	patch->old_name = find_name(state->root, line, NULL, state->p_value ? state->p_value - 1 : 0, 0);
 	return 0;
 }
 
-static int gitdiff_renamedst(struct apply_state *state,
+static int gitdiff_renamedst(struct gitdiff_data *state,
 			     const char *line,
 			     struct patch *patch)
 {
 	patch->is_rename = 1;
 	free(patch->new_name);
-	patch->new_name = find_name(&state->root, line, NULL, state->p_value ? state->p_value - 1 : 0, 0);
+	patch->new_name = find_name(state->root, line, NULL, state->p_value ? state->p_value - 1 : 0, 0);
 	return 0;
 }
 
-static int gitdiff_similarity(struct apply_state *state,
+static int gitdiff_similarity(struct gitdiff_data *state,
 			      const char *line,
 			      struct patch *patch)
 {
@@ -1076,7 +1082,7 @@ static int gitdiff_similarity(struct apply_state *state,
 	return 0;
 }
 
-static int gitdiff_dissimilarity(struct apply_state *state,
+static int gitdiff_dissimilarity(struct gitdiff_data *state,
 				 const char *line,
 				 struct patch *patch)
 {
@@ -1086,7 +1092,7 @@ static int gitdiff_dissimilarity(struct apply_state *state,
 	return 0;
 }
 
-static int gitdiff_index(struct apply_state *state,
+static int gitdiff_index(struct gitdiff_data *state,
 			 const char *line,
 			 struct patch *patch)
 {
@@ -1126,7 +1132,7 @@ static int gitdiff_index(struct apply_state *state,
  * This is normal for a diff that doesn't change anything: we'll fall through
  * into the next diff. Tell the parser to break out.
  */
-static int gitdiff_unrecognized(struct apply_state *state,
+static int gitdiff_unrecognized(struct gitdiff_data *state,
 				const char *line,
 				struct patch *patch)
 {
@@ -1322,6 +1328,7 @@ static int parse_git_header(struct apply_state *state,
 			    struct patch *patch)
 {
 	unsigned long offset;
+	struct gitdiff_data parse_hdr_state;
 
 	/* A git diff has explicit new/delete information, so we don't guess */
 	patch->is_new = 0;
@@ -1343,10 +1350,14 @@ static int parse_git_header(struct apply_state *state,
 	line += len;
 	size -= len;
 	state->linenr++;
+	parse_hdr_state.root = &state->root;
+	parse_hdr_state.linenr = state->linenr;
+	parse_hdr_state.p_value = state->p_value;
+
 	for (offset = len ; size > 0 ; offset += len, size -= len, line += len, state->linenr++) {
 		static const struct opentry {
 			const char *str;
-			int (*fn)(struct apply_state *, const char *, struct patch *);
+			int (*fn)(struct gitdiff_data *, const char *, struct patch *);
 		} optable[] = {
 			{ "@@ -", gitdiff_hdrend },
 			{ "--- ", gitdiff_oldname },
@@ -1377,7 +1388,7 @@ static int parse_git_header(struct apply_state *state,
 			int res;
 			if (len < oplen || memcmp(p->str, line, oplen))
 				continue;
-			res = p->fn(state, line + oplen, patch);
+			res = p->fn(&parse_hdr_state, line + oplen, patch);
 			if (res < 0)
 				return -1;
 			if (check_header_line(state->linenr, patch))
-- 
2.22.0.510.g264f2c817a
Thomas Gummerer· Jul 11, 2019, 16:08 UTC · re: Thomas Gummerer · lore

[PATCH v4 08/14] range-diff: fix function parameter indentation

Fix the indentation of the function parameters for a couple of functions, to match the style in the rest of the file.

Signed-off-by: Thomas Gummerer <t.gummerer@gmail.com>
---
 range-diff.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
Show changes to range-diff.c +2 −2
diff --git a/range-diff.c b/range-diff.c
index 48b0e1b4ce..9242b8975f 100644
--- a/range-diff.c
+++ b/range-diff.c
@@ -148,7 +148,7 @@ static int read_patches(const char *range, struct string_list *list)
 }
 
 static int patch_util_cmp(const void *dummy, const struct patch_util *a,
-		     const struct patch_util *b, const char *keydata)
+			  const struct patch_util *b, const char *keydata)
 {
 	return strcmp(a->diff, keydata ? keydata : b->diff);
 }
@@ -373,7 +373,7 @@ static struct diff_filespec *get_filespec(const char *name, const char *p)
 }
 
 static void patch_diff(const char *a, const char *b,
-			      struct diff_options *diffopt)
+		       struct diff_options *diffopt)
 {
 	diff_queue(&diff_queued_diff,
 		   get_filespec("a", a), get_filespec("b", b));
-- 
2.22.0.510.g264f2c817a
Thomas Gummerer· Jul 11, 2019, 16:08 UTC · re: Thomas Gummerer · lore

[PATCH v4 07/14] apply: make parse_git_diff_header public

Make 'parse_git_header()' (renamed to 'parse_git_diff_header()') a "public" function in apply.h, so we can re-use it in range-diff in a subsequent commit. We're renaming the function to make it clearer in other parts of the codebase that we're talking about a diff header and not just any header.

Signed-off-by: Thomas Gummerer <t.gummerer@gmail.com>
---
 apply.c | 69 ++++++++++++++++-----------------------------------------
 apply.h | 48 +++++++++++++++++++++++++++++++++++++++
 2 files changed, 67 insertions(+), 50 deletions(-)
Show changes to 2 files +67 −50

apply.c, apply.h

diff --git a/apply.c b/apply.c
index 034d134dd5..d68a6acad7 100644
--- a/apply.c
+++ b/apply.c
@@ -207,40 +207,6 @@ struct fragment {
 #define BINARY_DELTA_DEFLATED	1
 #define BINARY_LITERAL_DEFLATED 2
 
-/*
- * This represents a "patch" to a file, both metainfo changes
- * such as creation/deletion, filemode and content changes represented
- * as a series of fragments.
- */
-struct patch {
-	char *new_name, *old_name, *def_name;
-	unsigned int old_mode, new_mode;
-	int is_new, is_delete;	/* -1 = unknown, 0 = false, 1 = true */
-	int rejected;
-	unsigned ws_rule;
-	int lines_added, lines_deleted;
-	int score;
-	int extension_linenr; /* first line specifying delete/new/rename/copy */
-	unsigned int is_toplevel_relative:1;
-	unsigned int inaccurate_eof:1;
-	unsigned int is_binary:1;
-	unsigned int is_copy:1;
-	unsigned int is_rename:1;
-	unsigned int recount:1;
-	unsigned int conflicted_threeway:1;
-	unsigned int direct_to_threeway:1;
-	unsigned int crlf_in_old:1;
-	struct fragment *fragments;
-	char *result;
-	size_t resultsize;
-	char old_oid_prefix[GIT_MAX_HEXSZ + 1];
-	char new_oid_prefix[GIT_MAX_HEXSZ + 1];
-	struct patch *next;
-
-	/* three-way fallback result */
-	struct object_id threeway_stage[3];
-};
-
 static void free_fragment_list(struct fragment *list)
 {
 	while (list) {
@@ -1320,12 +1286,13 @@ static int check_header_line(int linenr, struct patch *patch)
 	return 0;
 }
 
-/* Verify that we recognize the lines following a git header */
-static int parse_git_header(struct apply_state *state,
-			    const char *line,
-			    int len,
-			    unsigned int size,
-			    struct patch *patch)
+int parse_git_diff_header(struct strbuf *root,
+			  int *linenr,
+			  int p_value,
+			  const char *line,
+			  int len,
+			  unsigned int size,
+			  struct patch *patch)
 {
 	unsigned long offset;
 	struct gitdiff_data parse_hdr_state;
@@ -1340,21 +1307,21 @@ static int parse_git_header(struct apply_state *state,
 	 * or removing or adding empty files), so we get
 	 * the default name from the header.
 	 */
-	patch->def_name = git_header_name(state->p_value, line, len);
-	if (patch->def_name && state->root.len) {
-		char *s = xstrfmt("%s%s", state->root.buf, patch->def_name);
+	patch->def_name = git_header_name(p_value, line, len);
+	if (patch->def_name && root->len) {
+		char *s = xstrfmt("%s%s", root->buf, patch->def_name);
 		free(patch->def_name);
 		patch->def_name = s;
 	}
 
 	line += len;
 	size -= len;
-	state->linenr++;
-	parse_hdr_state.root = &state->root;
-	parse_hdr_state.linenr = state->linenr;
-	parse_hdr_state.p_value = state->p_value;
+	(*linenr)++;
+	parse_hdr_state.root = root;
+	parse_hdr_state.linenr = *linenr;
+	parse_hdr_state.p_value = p_value;
 
-	for (offset = len ; size > 0 ; offset += len, size -= len, line += len, state->linenr++) {
+	for (offset = len ; size > 0 ; offset += len, size -= len, line += len, (*linenr)++) {
 		static const struct opentry {
 			const char *str;
 			int (*fn)(struct gitdiff_data *, const char *, struct patch *);
@@ -1391,7 +1358,7 @@ static int parse_git_header(struct apply_state *state,
 			res = p->fn(&parse_hdr_state, line + oplen, patch);
 			if (res < 0)
 				return -1;
-			if (check_header_line(state->linenr, patch))
+			if (check_header_line(*linenr, patch))
 				return -1;
 			if (res > 0)
 				return offset;
@@ -1572,7 +1539,9 @@ static int find_header(struct apply_state *state,
 		 * or mode change, so we handle that specially
 		 */
 		if (!memcmp("diff --git ", line, 11)) {
-			int git_hdr_len = parse_git_header(state, line, len, size, patch);
+			int git_hdr_len = parse_git_diff_header(&state->root, &state->linenr,
+								state->p_value, line, len,
+								size, patch);
 			if (git_hdr_len < 0)
 				return -128;
 			if (git_hdr_len <= len)
diff --git a/apply.h b/apply.h
index 5948348133..a795193435 100644
--- a/apply.h
+++ b/apply.h
@@ -117,6 +117,40 @@ struct apply_state {
 	int applied_after_fixing_ws;
 };
 
+/*
+ * This represents a "patch" to a file, both metainfo changes
+ * such as creation/deletion, filemode and content changes represented
+ * as a series of fragments.
+ */
+struct patch {
+	char *new_name, *old_name, *def_name;
+	unsigned int old_mode, new_mode;
+	int is_new, is_delete;	/* -1 = unknown, 0 = false, 1 = true */
+	int rejected;
+	unsigned ws_rule;
+	int lines_added, lines_deleted;
+	int score;
+	int extension_linenr; /* first line specifying delete/new/rename/copy */
+	unsigned int is_toplevel_relative:1;
+	unsigned int inaccurate_eof:1;
+	unsigned int is_binary:1;
+	unsigned int is_copy:1;
+	unsigned int is_rename:1;
+	unsigned int recount:1;
+	unsigned int conflicted_threeway:1;
+	unsigned int direct_to_threeway:1;
+	unsigned int crlf_in_old:1;
+	struct fragment *fragments;
+	char *result;
+	size_t resultsize;
+	char old_oid_prefix[GIT_MAX_HEXSZ + 1];
+	char new_oid_prefix[GIT_MAX_HEXSZ + 1];
+	struct patch *next;
+
+	/* three-way fallback result */
+	struct object_id threeway_stage[3];
+};
+
 int apply_parse_options(int argc, const char **argv,
 			struct apply_state *state,
 			int *force_apply, int *options,
@@ -127,6 +161,20 @@ int init_apply_state(struct apply_state *state,
 void clear_apply_state(struct apply_state *state);
 int check_apply_state(struct apply_state *state, int force_apply);
 
+/*
+ * Parse a git diff header, starting at line.  Fills the relevant
+ * metadata information in 'struct patch'.
+ *
+ * Returns -1 on failure, the length of the parsed header otherwise.
+ */
+int parse_git_diff_header(struct strbuf *root,
+			  int *linenr,
+			  int p_value,
+			  const char *line,
+			  int len,
+			  unsigned int size,
+			  struct patch *patch);
+
 /*
  * Some aspects of the apply behavior are controlled by the following
  * bits in the "options" parameter passed to apply_all_patches().
-- 
2.22.0.510.g264f2c817a
Thomas Gummerer· Jul 11, 2019, 16:08 UTC · re: Thomas Gummerer · lore

[PATCH v4 09/14] range-diff: split lines manually

Currently range-diff uses the 'strbuf_getline()' function for doing its line by line processing. In a future patch we want to do parts of that parsing using the 'parse_git_diff_header()' function. That function does its own line by line reading of the input, and doesn't use strbufs. This doesn't match with how we do the line-by-line processing in range-diff currently.

Switch range-diff to do our own line by line parsing, so we can re-use the 'parse_git_diff_header()' function later.

Signed-off-by: Thomas Gummerer <t.gummerer@gmail.com>
---
 range-diff.c | 68 ++++++++++++++++++++++++++++++++--------------------
 1 file changed, 42 insertions(+), 26 deletions(-)
Show changes to range-diff.c +42 −26
diff --git a/range-diff.c b/range-diff.c
index 9242b8975f..784fac301b 100644
--- a/range-diff.c
+++ b/range-diff.c
@@ -24,6 +24,17 @@ struct patch_util {
 	struct object_id oid;
 };
 
+static size_t find_end_of_line(char *buffer, unsigned long size)
+{
+	char *eol = memchr(buffer, '\n', size);
+
+	if (!eol)
+		return size;
+
+	*eol = '\0';
+	return eol + 1 - buffer;
+}
+
 /*
  * Reads the patches into a string list, with the `util` field being populated
  * as struct object_id (will need to be free()d).
@@ -31,10 +42,12 @@ struct patch_util {
 static int read_patches(const char *range, struct string_list *list)
 {
 	struct child_process cp = CHILD_PROCESS_INIT;
-	FILE *in;
-	struct strbuf buf = STRBUF_INIT, line = STRBUF_INIT;
+	struct strbuf buf = STRBUF_INIT, contents = STRBUF_INIT;
 	struct patch_util *util = NULL;
 	int in_header = 1;
+	char *line;
+	int offset, len;
+	size_t size;
 
 	argv_array_pushl(&cp.args, "log", "--no-color", "-p", "--no-merges",
 			"--reverse", "--date-order", "--decorate=no",
@@ -54,17 +67,20 @@ static int read_patches(const char *range, struct string_list *list)
 
 	if (start_command(&cp))
 		return error_errno(_("could not start `log`"));
-	in = fdopen(cp.out, "r");
-	if (!in) {
+	if (strbuf_read(&contents, cp.out, 0) < 0) {
 		error_errno(_("could not read `log` output"));
 		finish_command(&cp);
 		return -1;
 	}
 
-	while (strbuf_getline(&line, in) != EOF) {
+	line = contents.buf;
+	size = contents.len;
+	for (offset = 0; size > 0; offset += len, size -= len, line += len) {
 		const char *p;
 
-		if (skip_prefix(line.buf, "commit ", &p)) {
+		len = find_end_of_line(line, size);
+		line[len - 1] = '\0';
+		if (skip_prefix(line, "commit ", &p)) {
 			if (util) {
 				string_list_append(list, buf.buf)->util = util;
 				strbuf_reset(&buf);
@@ -75,8 +91,7 @@ static int read_patches(const char *range, struct string_list *list)
 				free(util);
 				string_list_clear(list, 1);
 				strbuf_release(&buf);
-				strbuf_release(&line);
-				fclose(in);
+				strbuf_release(&contents);
 				finish_command(&cp);
 				return -1;
 			}
@@ -85,26 +100,28 @@ static int read_patches(const char *range, struct string_list *list)
 			continue;
 		}
 
-		if (starts_with(line.buf, "diff --git")) {
+		if (starts_with(line, "diff --git")) {
 			in_header = 0;
 			strbuf_addch(&buf, '\n');
 			if (!util->diff_offset)
 				util->diff_offset = buf.len;
 			strbuf_addch(&buf, ' ');
-			strbuf_addbuf(&buf, &line);
+			strbuf_addstr(&buf, line);
 		} else if (in_header) {
-			if (starts_with(line.buf, "Author: ")) {
-				strbuf_addbuf(&buf, &line);
+			if (starts_with(line, "Author: ")) {
+				strbuf_addstr(&buf, line);
 				strbuf_addstr(&buf, "\n\n");
-			} else if (starts_with(line.buf, "    ")) {
-				strbuf_rtrim(&line);
-				strbuf_addbuf(&buf, &line);
+			} else if (starts_with(line, "    ")) {
+				p = line + len - 2;
+				while (isspace(*p) && p >= line)
+					p--;
+				strbuf_add(&buf, line, p - line + 1);
 				strbuf_addch(&buf, '\n');
 			}
 			continue;
-		} else if (starts_with(line.buf, "@@ "))
+		} else if (starts_with(line, "@@ "))
 			strbuf_addstr(&buf, "@@");
-		else if (!line.buf[0] || starts_with(line.buf, "index "))
+		else if (!line[0] || starts_with(line, "index "))
 			/*
 			 * A completely blank (not ' \n', which is context)
 			 * line is not valid in a diff.  We skip it
@@ -117,25 +134,24 @@ static int read_patches(const char *range, struct string_list *list)
 			 * we are not interested.
 			 */
 			continue;
-		else if (line.buf[0] == '>') {
+		else if (line[0] == '>') {
 			strbuf_addch(&buf, '+');
-			strbuf_add(&buf, line.buf + 1, line.len - 1);
-		} else if (line.buf[0] == '<') {
+			strbuf_addstr(&buf, line + 1);
+		} else if (line[0] == '<') {
 			strbuf_addch(&buf, '-');
-			strbuf_add(&buf, line.buf + 1, line.len - 1);
-		} else if (line.buf[0] == '#') {
+			strbuf_addstr(&buf, line + 1);
+		} else if (line[0] == '#') {
 			strbuf_addch(&buf, ' ');
-			strbuf_add(&buf, line.buf + 1, line.len - 1);
+			strbuf_addstr(&buf, line + 1);
 		} else {
 			strbuf_addch(&buf, ' ');
-			strbuf_addbuf(&buf, &line);
+			strbuf_addstr(&buf, line);
 		}
 
 		strbuf_addch(&buf, '\n');
 		util->diffsize++;
 	}
-	fclose(in);
-	strbuf_release(&line);
+	strbuf_release(&contents);
 
 	if (util)
 		string_list_append(list, buf.buf)->util = util;
-- 
2.22.0.510.g264f2c817a
Thomas Gummerer· Jul 11, 2019, 16:08 UTC · re: Thomas Gummerer · lore

[PATCH v4 10/14] range-diff: don't remove funcname from inner diff

When postprocessing the inner diff in range-diff, we currently replace the whole hunk header line with just "@@". This matches how 'git tbdiff' used to handle hunk headers as well.

Most likely this is being done because line numbers in the hunk header are not relevant without other changes. They can for example easily change if a range is rebased, and lines are added/removed before a change that we actually care about in our ranges.

However it can still be useful to have the function name that 'git diff' extracts as additional context for the change.

Note that it is not guaranteed that the hunk header actually shows up in the range-diff, and this change only aims to improve the case where a hunk header would already be included in the final output.

Signed-off-by: Thomas Gummerer <t.gummerer@gmail.com>
---
 range-diff.c          | 7 ++++---
 t/t3206-range-diff.sh | 6 +++---
 2 files changed, 7 insertions(+), 6 deletions(-)
Show changes to 2 files +7 −6

range-diff.c, t/t3206-range-diff.sh

diff --git a/range-diff.c b/range-diff.c
index 784fac301b..a5202d8b6c 100644
--- a/range-diff.c
+++ b/range-diff.c
@@ -119,9 +119,10 @@ static int read_patches(const char *range, struct string_list *list)
 				strbuf_addch(&buf, '\n');
 			}
 			continue;
-		} else if (starts_with(line, "@@ "))
-			strbuf_addstr(&buf, "@@");
-		else if (!line[0] || starts_with(line, "index "))
+		} else if (skip_prefix(line, "@@ ", &p)) {
+			p = strstr(p, "@@");
+			strbuf_addstr(&buf, p ? p : "@@");
+		} else if (!line[0] || starts_with(line, "index "))
 			/*
 			 * A completely blank (not ' \n', which is context)
 			 * line is not valid in a diff.  We skip it
diff --git a/t/t3206-range-diff.sh b/t/t3206-range-diff.sh
index 048feaf6dd..aebd4e3693 100755
--- a/t/t3206-range-diff.sh
+++ b/t/t3206-range-diff.sh
@@ -110,7 +110,7 @@ test_expect_success 'changed commit' '
 	      14
 	4:  a63e992 ! 4:  d966c5c s/12/B/
 	    @@ -8,7 +8,7 @@
-	     @@
+	     @@ A
 	      9
 	      10
 	    - B
@@ -169,7 +169,7 @@ test_expect_success 'changed commit with sm config' '
 	      14
 	4:  a63e992 ! 4:  d966c5c s/12/B/
 	    @@ -8,7 +8,7 @@
-	     @@
+	     @@ A
 	      9
 	      10
 	    - B
@@ -231,7 +231,7 @@ test_expect_success 'dual-coloring' '
 	:      14<RESET>
 	:<RED>4:  d966c5c <RESET><YELLOW>!<RESET><GREEN> 4:  8add5f1<RESET><YELLOW> s/12/B/<RESET>
 	:    <REVERSE><CYAN>@@ -8,7 +8,7 @@<RESET>
-	:    <CYAN> @@<RESET>
+	:    <CYAN> @@ A<RESET>
 	:      9<RESET>
 	:      10<RESET>
 	:    <REVERSE><RED>-<RESET><FAINT> BB<RESET>
-- 
2.22.0.510.g264f2c817a
Thomas Gummerer· Jul 11, 2019, 16:08 UTC · re: Thomas Gummerer · lore

[PATCH v4 12/14] range-diff: add section header instead of diff header

Currently range-diff keeps the diff header of the inner diff intact (apart from stripping lines starting with index). This diff header is somewhat useful, especially when files get different names in different ranges.

However there is no real need to keep the whole diff header for that. The main reason we currently do that is probably because it is easy to do.

Introduce a new range diff hunk header, that's enclosed by "##", similar to how line numbers in diff hunks are enclosed by "@@", and give human readable information of what exactly happened to the file, including the file name.

This improves the readability of the range-diff by giving more concise information to the users. For example if a file was renamed in one iteration, but not in another, the diff of the headers would be quite noisy. However the diff of a single line is concise and should be easier to understand.

Additionally, this allows us to add these range diff section headers to the outer diffs hunk headers using a custom userdiff pattern, which should help making the range-diff more readable.

Signed-off-by: Thomas Gummerer <t.gummerer@gmail.com>
---
 range-diff.c           | 34 ++++++++++++----
 t/t3206-range-diff.sh  | 91 +++++++++++++++++++++++++++++++++++++++---
 t/t3206/history.export | 84 ++++++++++++++++++++++++++++++++++++--
 3 files changed, 192 insertions(+), 17 deletions(-)
Show changes to 3 files +192 −17

range-diff.c, t/t3206-range-diff.sh, t/t3206/history.export

diff --git a/range-diff.c b/range-diff.c
index f4a90b33b8..5f64380fe4 100644
--- a/range-diff.c
+++ b/range-diff.c
@@ -10,6 +10,7 @@
 #include "commit.h"
 #include "pretty.h"
 #include "userdiff.h"
+#include "apply.h"
 
 struct patch_util {
 	/* For the search for an exact match */
@@ -101,12 +102,35 @@ static int read_patches(const char *range, struct string_list *list)
 		}
 
 		if (starts_with(line, "diff --git")) {
+			struct patch patch = { 0 };
+			struct strbuf root = STRBUF_INIT;
+			int linenr = 0;
+
 			in_header = 0;
 			strbuf_addch(&buf, '\n');
 			if (!util->diff_offset)
 				util->diff_offset = buf.len;
-			strbuf_addch(&buf, ' ');
-			strbuf_addstr(&buf, line);
+			line[len - 1] = '\n';
+			len = parse_git_diff_header(&root, &linenr, 1, line,
+						    len, size, &patch);
+			if (len < 0)
+				die(_("could not parse git header '%.*s'"), (int)len, line);
+			strbuf_addstr(&buf, " ## ");
+			if (patch.is_new > 0)
+				strbuf_addf(&buf, "%s (new)", patch.new_name);
+			else if (patch.is_delete > 0)
+				strbuf_addf(&buf, "%s (deleted)", patch.old_name);
+			else if (patch.is_rename)
+				strbuf_addf(&buf, "%s => %s", patch.old_name, patch.new_name);
+			else
+				strbuf_addstr(&buf, patch.new_name);
+
+			if (patch.new_mode && patch.old_mode &&
+			    patch.old_mode != patch.new_mode)
+				strbuf_addf(&buf, " (mode change %06o => %06o)",
+					    patch.old_mode, patch.new_mode);
+
+			strbuf_addstr(&buf, " ##");
 		} else if (in_header) {
 			if (starts_with(line, "Author: ")) {
 				strbuf_addstr(&buf, line);
@@ -122,17 +146,13 @@ static int read_patches(const char *range, struct string_list *list)
 		} else if (skip_prefix(line, "@@ ", &p)) {
 			p = strstr(p, "@@");
 			strbuf_addstr(&buf, p ? p : "@@");
-		} else if (!line[0] || starts_with(line, "index "))
+		} else if (!line[0])
 			/*
 			 * A completely blank (not ' \n', which is context)
 			 * line is not valid in a diff.  We skip it
 			 * silently, because this neatly handles the blank
 			 * separator line between commits in git-log
 			 * output.
-			 *
-			 * We also want to ignore the diff's `index` lines
-			 * because they contain exact blob hashes in which
-			 * we are not interested.
 			 */
 			continue;
 		else if (line[0] == '>') {
diff --git a/t/t3206-range-diff.sh b/t/t3206-range-diff.sh
index 9f89af7178..c277756057 100755
--- a/t/t3206-range-diff.sh
+++ b/t/t3206-range-diff.sh
@@ -181,6 +181,85 @@ test_expect_success 'changed commit with sm config' '
 	test_cmp expected actual
 '
 
+test_expect_success 'renamed file' '
+	git range-diff --no-color --submodule=log topic...renamed-file >actual &&
+	sed s/Z/\ /g >expected <<-EOF &&
+	1:  4de457d = 1:  f258d75 s/5/A/
+	2:  fccce22 ! 2:  017b62d s/4/A/
+	    @@
+	    ZAuthor: Thomas Rast <trast@inf.ethz.ch>
+	    Z
+	    -    s/4/A/
+	    +    s/4/A/ + rename file
+	    Z
+	    - ## file ##
+	    + ## file => renamed-file ##
+	    Z@@
+	    Z 1
+	    Z 2
+	3:  147e64e ! 3:  3ce7af6 s/11/B/
+	    @@
+	    Z
+	    Z    s/11/B/
+	    Z
+	    - ## file ##
+	    + ## renamed-file ##
+	    Z@@ A
+	    Z 8
+	    Z 9
+	4:  a63e992 ! 4:  1e6226b s/12/B/
+	    @@
+	    Z
+	    Z    s/12/B/
+	    Z
+	    - ## file ##
+	    + ## renamed-file ##
+	    Z@@ A
+	    Z 9
+	    Z 10
+	EOF
+	test_cmp expected actual
+'
+
+test_expect_success 'file added and later removed' '
+	git range-diff --no-color --submodule=log topic...added-removed >actual &&
+	sed s/Z/\ /g >expected <<-EOF &&
+	1:  4de457d = 1:  096b1ba s/5/A/
+	2:  fccce22 ! 2:  d92e698 s/4/A/
+	    @@
+	    ZAuthor: Thomas Rast <trast@inf.ethz.ch>
+	    Z
+	    -    s/4/A/
+	    +    s/4/A/ + new-file
+	    Z
+	    Z ## file ##
+	    Z@@
+	    @@
+	    Z A
+	    Z 6
+	    Z 7
+	    +
+	    + ## new-file (new) ##
+	3:  147e64e ! 3:  9a1db4d s/11/B/
+	    @@
+	    ZAuthor: Thomas Rast <trast@inf.ethz.ch>
+	    Z
+	    -    s/11/B/
+	    +    s/11/B/ + remove file
+	    Z
+	    Z ## file ##
+	    Z@@ A
+	    @@
+	    Z 12
+	    Z 13
+	    Z 14
+	    +
+	    + ## new-file (deleted) ##
+	4:  a63e992 = 4:  fea3b5c s/12/B/
+	EOF
+	test_cmp expected actual
+'
+
 test_expect_success 'no commits on one side' '
 	git commit --amend -m "new message" &&
 	git range-diff master HEAD@{1} HEAD
@@ -197,9 +276,9 @@ test_expect_success 'changed message' '
 	    Z
 	    +    Also a silly comment here!
 	    +
-	    Z diff --git a/file b/file
-	    Z --- a/file
-	    Z +++ b/file
+	    Z ## file ##
+	    Z@@
+	    Z 1
 	3:  147e64e = 3:  b9cb956 s/11/B/
 	4:  a63e992 = 4:  8add5f1 s/12/B/
 	EOF
@@ -216,9 +295,9 @@ test_expect_success 'dual-coloring' '
 	:     <RESET>
 	:    <REVERSE><GREEN>+<RESET><BOLD>    Also a silly comment here!<RESET>
 	:    <REVERSE><GREEN>+<RESET>
-	:      diff --git a/file b/file<RESET>
-	:      --- a/file<RESET>
-	:      +++ b/file<RESET>
+	:      ## file ##<RESET>
+	:    <CYAN> @@<RESET>
+	:      1<RESET>
 	:<RED>3:  0559556 <RESET><YELLOW>!<RESET><GREEN> 3:  b9cb956<RESET><YELLOW> s/11/B/<RESET>
 	:    <REVERSE><CYAN>@@<RESET>
 	:      9<RESET>
diff --git a/t/t3206/history.export b/t/t3206/history.export
index b8ffff0940..7bb3814962 100644
--- a/t/t3206/history.export
+++ b/t/t3206/history.export
@@ -22,8 +22,8 @@ data 51
 19
 20
 
-reset refs/heads/removed
-commit refs/heads/removed
+reset refs/heads/renamed-file
+commit refs/heads/renamed-file
 mark :2
 author Thomas Rast <trast@inf.ethz.ch> 1374424921 +0200
 committer Thomas Rast <trast@inf.ethz.ch> 1374484724 +0200
@@ -599,6 +599,82 @@ s/12/B/
 from :46
 M 100644 :28 file
 
-reset refs/heads/removed
-from :47
+commit refs/heads/added-removed
+mark :48
+author Thomas Rast <trast@inf.ethz.ch> 1374485014 +0200
+committer Thomas Gummerer <t.gummerer@gmail.com> 1556574151 +0100
+data 7
+s/5/A/
+from :2
+M 100644 :3 file
+
+blob
+mark :49
+data 0
+
+commit refs/heads/added-removed
+mark :50
+author Thomas Rast <trast@inf.ethz.ch> 1374485024 +0200
+committer Thomas Gummerer <t.gummerer@gmail.com> 1556574177 +0100
+data 18
+s/4/A/ + new-file
+from :48
+M 100644 :5 file
+M 100644 :49 new-file
+
+commit refs/heads/added-removed
+mark :51
+author Thomas Rast <trast@inf.ethz.ch> 1374485036 +0200
+committer Thomas Gummerer <t.gummerer@gmail.com> 1556574177 +0100
+data 22
+s/11/B/ + remove file
+from :50
+M 100644 :7 file
+D new-file
+
+commit refs/heads/added-removed
+mark :52
+author Thomas Rast <trast@inf.ethz.ch> 1374485044 +0200
+committer Thomas Gummerer <t.gummerer@gmail.com> 1556574177 +0100
+data 8
+s/12/B/
+from :51
+M 100644 :9 file
+
+commit refs/heads/renamed-file
+mark :53
+author Thomas Rast <trast@inf.ethz.ch> 1374485014 +0200
+committer Thomas Gummerer <t.gummerer@gmail.com> 1556574309 +0100
+data 7
+s/5/A/
+from :2
+M 100644 :3 file
+
+commit refs/heads/renamed-file
+mark :54
+author Thomas Rast <trast@inf.ethz.ch> 1374485024 +0200
+committer Thomas Gummerer <t.gummerer@gmail.com> 1556574312 +0100
+data 21
+s/4/A/ + rename file
+from :53
+D file
+M 100644 :5 renamed-file
+
+commit refs/heads/renamed-file
+mark :55
+author Thomas Rast <trast@inf.ethz.ch> 1374485036 +0200
+committer Thomas Gummerer <t.gummerer@gmail.com> 1556574319 +0100
+data 8
+s/11/B/
+from :54
+M 100644 :7 renamed-file
+
+commit refs/heads/renamed-file
+mark :56
+author Thomas Rast <trast@inf.ethz.ch> 1374485044 +0200
+committer Thomas Gummerer <t.gummerer@gmail.com> 1556574319 +0100
+data 8
+s/12/B/
+from :55
+M 100644 :9 renamed-file
 
-- 
2.22.0.510.g264f2c817a
Thomas Gummerer· Jul 11, 2019, 16:08 UTC · re: Thomas Gummerer · lore

[PATCH v4 11/14] range-diff: suppress line count in outer diff

The line count in the outer diff's hunk headers of a range diff is not all that interesting. It merely shows how far along the inner diff are on both sides. That number is of no use for human readers, and range-diffs are not meant to be machine readable.

In a subsequent commit we're going to add some more contextual information such as the filename corresponding to the diff to the hunk headers. Remove the unnecessary information, and just keep the "@@" to indicate that a new hunk of the outer diff is starting.

Signed-off-by: Thomas Gummerer <t.gummerer@gmail.com>
---
 diff.c                |  5 ++++-
 diff.h                |  1 +
 range-diff.c          |  1 +
 t/t3206-range-diff.sh | 16 ++++++++--------
 4 files changed, 14 insertions(+), 9 deletions(-)
Show changes to 4 files +14 −9

diff.c, diff.h, range-diff.c, t/t3206-range-diff.sh

diff --git a/diff.c b/diff.c
index ec5c095199..9c28ff0a92 100644
--- a/diff.c
+++ b/diff.c
@@ -1672,7 +1672,10 @@ static void emit_hunk_header(struct emit_callback *ecbdata,
 	if (ecbdata->opt->flags.dual_color_diffed_diffs)
 		strbuf_addstr(&msgbuf, reverse);
 	strbuf_addstr(&msgbuf, frag);
-	strbuf_add(&msgbuf, line, ep - line);
+	if (ecbdata->opt->flags.suppress_hunk_header_line_count)
+		strbuf_add(&msgbuf, atat, sizeof(atat));
+	else
+		strbuf_add(&msgbuf, line, ep - line);
 	strbuf_addstr(&msgbuf, reset);
 
 	/*
diff --git a/diff.h b/diff.h
index c9db9825bb..49913049f9 100644
--- a/diff.h
+++ b/diff.h
@@ -98,6 +98,7 @@ struct diff_flags {
 	unsigned stat_with_summary;
 	unsigned suppress_diff_headers;
 	unsigned dual_color_diffed_diffs;
+	unsigned suppress_hunk_header_line_count;
 };
 
 static inline void diff_flags_or(struct diff_flags *a,
diff --git a/range-diff.c b/range-diff.c
index a5202d8b6c..f4a90b33b8 100644
--- a/range-diff.c
+++ b/range-diff.c
@@ -486,6 +486,7 @@ int show_range_diff(const char *range1, const char *range2,
 			opts.output_format = DIFF_FORMAT_PATCH;
 		opts.flags.suppress_diff_headers = 1;
 		opts.flags.dual_color_diffed_diffs = dual_color;
+		opts.flags.suppress_hunk_header_line_count = 1;
 		opts.output_prefix = output_prefix_cb;
 		strbuf_addstr(&indent, "    ");
 		opts.output_prefix_data = &indent;
diff --git a/t/t3206-range-diff.sh b/t/t3206-range-diff.sh
index aebd4e3693..9f89af7178 100755
--- a/t/t3206-range-diff.sh
+++ b/t/t3206-range-diff.sh
@@ -99,7 +99,7 @@ test_expect_success 'changed commit' '
 	1:  4de457d = 1:  a4b3333 s/5/A/
 	2:  fccce22 = 2:  f51d370 s/4/A/
 	3:  147e64e ! 3:  0559556 s/11/B/
-	    @@ -10,7 +10,7 @@
+	    @@
 	      9
 	      10
 	     -11
@@ -109,7 +109,7 @@ test_expect_success 'changed commit' '
 	      13
 	      14
 	4:  a63e992 ! 4:  d966c5c s/12/B/
-	    @@ -8,7 +8,7 @@
+	    @@
 	     @@ A
 	      9
 	      10
@@ -158,7 +158,7 @@ test_expect_success 'changed commit with sm config' '
 	1:  4de457d = 1:  a4b3333 s/5/A/
 	2:  fccce22 = 2:  f51d370 s/4/A/
 	3:  147e64e ! 3:  0559556 s/11/B/
-	    @@ -10,7 +10,7 @@
+	    @@
 	      9
 	      10
 	     -11
@@ -168,7 +168,7 @@ test_expect_success 'changed commit with sm config' '
 	      13
 	      14
 	4:  a63e992 ! 4:  d966c5c s/12/B/
-	    @@ -8,7 +8,7 @@
+	    @@
 	     @@ A
 	      9
 	      10
@@ -191,7 +191,7 @@ test_expect_success 'changed message' '
 	sed s/Z/\ /g >expected <<-EOF &&
 	1:  4de457d = 1:  f686024 s/5/A/
 	2:  fccce22 ! 2:  4ab067d s/4/A/
-	    @@ -2,6 +2,8 @@
+	    @@
 	    Z
 	    Z    s/4/A/
 	    Z
@@ -210,7 +210,7 @@ test_expect_success 'dual-coloring' '
 	sed -e "s|^:||" >expect <<-\EOF &&
 	:<YELLOW>1:  a4b3333 = 1:  f686024 s/5/A/<RESET>
 	:<RED>2:  f51d370 <RESET><YELLOW>!<RESET><GREEN> 2:  4ab067d<RESET><YELLOW> s/4/A/<RESET>
-	:    <REVERSE><CYAN>@@ -2,6 +2,8 @@<RESET>
+	:    <REVERSE><CYAN>@@<RESET>
 	:     <RESET>
 	:         s/4/A/<RESET>
 	:     <RESET>
@@ -220,7 +220,7 @@ test_expect_success 'dual-coloring' '
 	:      --- a/file<RESET>
 	:      +++ b/file<RESET>
 	:<RED>3:  0559556 <RESET><YELLOW>!<RESET><GREEN> 3:  b9cb956<RESET><YELLOW> s/11/B/<RESET>
-	:    <REVERSE><CYAN>@@ -10,7 +10,7 @@<RESET>
+	:    <REVERSE><CYAN>@@<RESET>
 	:      9<RESET>
 	:      10<RESET>
 	:    <RED> -11<RESET>
@@ -230,7 +230,7 @@ test_expect_success 'dual-coloring' '
 	:      13<RESET>
 	:      14<RESET>
 	:<RED>4:  d966c5c <RESET><YELLOW>!<RESET><GREEN> 4:  8add5f1<RESET><YELLOW> s/12/B/<RESET>
-	:    <REVERSE><CYAN>@@ -8,7 +8,7 @@<RESET>
+	:    <REVERSE><CYAN>@@<RESET>
 	:    <CYAN> @@ A<RESET>
 	:      9<RESET>
 	:      10<RESET>
-- 
2.22.0.510.g264f2c817a
Thomas Gummerer· Jul 11, 2019, 16:08 UTC · re: Thomas Gummerer · lore

[PATCH v4 13/14] range-diff: add filename to inner diff

In a range-diff it's not always clear which file a certain funcname of the inner diff belongs to, because the diff header (or section header as added in a previous commit) is not always visible in the range-diff.

Add the filename to the inner diffs header, so it's always visible to users.

This also allows us to add the filename + the funcname to the outer diffs hunk headers using a custom userdiff pattern, which will be done in the next commit.

Signed-off-by: Thomas Gummerer <t.gummerer@gmail.com>
---
 range-diff.c          | 15 +++++++++++++--
 t/t3206-range-diff.sh | 16 ++++++++++------
 2 files changed, 23 insertions(+), 8 deletions(-)
Show changes to 2 files +23 −8

range-diff.c, t/t3206-range-diff.sh

diff --git a/range-diff.c b/range-diff.c
index 5f64380fe4..7a96a587f1 100644
--- a/range-diff.c
+++ b/range-diff.c
@@ -46,7 +46,7 @@ static int read_patches(const char *range, struct string_list *list)
 	struct strbuf buf = STRBUF_INIT, contents = STRBUF_INIT;
 	struct patch_util *util = NULL;
 	int in_header = 1;
-	char *line;
+	char *line, *current_filename = NULL;
 	int offset, len;
 	size_t size;
 
@@ -125,6 +125,12 @@ static int read_patches(const char *range, struct string_list *list)
 			else
 				strbuf_addstr(&buf, patch.new_name);
 
+			free(current_filename);
+			if (patch.is_delete > 0)
+				current_filename = xstrdup(patch.old_name);
+			else
+				current_filename = xstrdup(patch.new_name);
+
 			if (patch.new_mode && patch.old_mode &&
 			    patch.old_mode != patch.new_mode)
 				strbuf_addf(&buf, " (mode change %06o => %06o)",
@@ -145,7 +151,11 @@ static int read_patches(const char *range, struct string_list *list)
 			continue;
 		} else if (skip_prefix(line, "@@ ", &p)) {
 			p = strstr(p, "@@");
-			strbuf_addstr(&buf, p ? p : "@@");
+			strbuf_addstr(&buf, "@@");
+			if (current_filename && p[2])
+				strbuf_addf(&buf, " %s:", current_filename);
+			if (p)
+				strbuf_addstr(&buf, p + 2);
 		} else if (!line[0])
 			/*
 			 * A completely blank (not ' \n', which is context)
@@ -177,6 +187,7 @@ static int read_patches(const char *range, struct string_list *list)
 	if (util)
 		string_list_append(list, buf.buf)->util = util;
 	strbuf_release(&buf);
+	free(current_filename);
 
 	if (finish_command(&cp))
 		return -1;
diff --git a/t/t3206-range-diff.sh b/t/t3206-range-diff.sh
index c277756057..d4de270979 100755
--- a/t/t3206-range-diff.sh
+++ b/t/t3206-range-diff.sh
@@ -110,7 +110,7 @@ test_expect_success 'changed commit' '
 	      14
 	4:  a63e992 ! 4:  d966c5c s/12/B/
 	    @@
-	     @@ A
+	     @@ file: A
 	      9
 	      10
 	    - B
@@ -169,7 +169,7 @@ test_expect_success 'changed commit with sm config' '
 	      14
 	4:  a63e992 ! 4:  d966c5c s/12/B/
 	    @@
-	     @@ A
+	     @@ file: A
 	      9
 	      10
 	    - B
@@ -203,20 +203,24 @@ test_expect_success 'renamed file' '
 	    Z    s/11/B/
 	    Z
 	    - ## file ##
+	    -@@ file: A
 	    + ## renamed-file ##
-	    Z@@ A
+	    +@@ renamed-file: A
 	    Z 8
 	    Z 9
+	    Z 10
 	4:  a63e992 ! 4:  1e6226b s/12/B/
 	    @@
 	    Z
 	    Z    s/12/B/
 	    Z
 	    - ## file ##
+	    -@@ file: A
 	    + ## renamed-file ##
-	    Z@@ A
+	    +@@ renamed-file: A
 	    Z 9
 	    Z 10
+	    Z B
 	EOF
 	test_cmp expected actual
 '
@@ -248,7 +252,7 @@ test_expect_success 'file added and later removed' '
 	    +    s/11/B/ + remove file
 	    Z
 	    Z ## file ##
-	    Z@@ A
+	    Z@@ file: A
 	    @@
 	    Z 12
 	    Z 13
@@ -310,7 +314,7 @@ test_expect_success 'dual-coloring' '
 	:      14<RESET>
 	:<RED>4:  d966c5c <RESET><YELLOW>!<RESET><GREEN> 4:  8add5f1<RESET><YELLOW> s/12/B/<RESET>
 	:    <REVERSE><CYAN>@@<RESET>
-	:    <CYAN> @@ A<RESET>
+	:    <CYAN> @@ file: A<RESET>
 	:      9<RESET>
 	:      10<RESET>
 	:    <REVERSE><RED>-<RESET><FAINT> BB<RESET>
-- 
2.22.0.510.g264f2c817a
Thomas Gummerer· Jul 11, 2019, 16:08 UTC · re: Thomas Gummerer · lore

[PATCH v4 14/14] range-diff: add headers to the outer hunk header

Add the section headers/hunk headers we introduced in the previous commits to the outer diff's hunk headers. This makes it easier to understand which change we are actually looking at. For example an outer hunk header might now look like:

    @@  Documentation/config/interactive.txt
while previously it would have only been
    @@
which doesn't give a lot of context for the change that follows.

For completeness also add section headers for the commit metadata and the commit message, although they are arguably less important.

Signed-off-by: Thomas Gummerer <t.gummerer@gmail.com>
---
 range-diff.c          |  9 ++++++---
 t/t3206-range-diff.sh | 41 ++++++++++++++++++++++-------------------
 2 files changed, 28 insertions(+), 22 deletions(-)
Show changes to 2 files +28 −22

range-diff.c, t/t3206-range-diff.sh

diff --git a/range-diff.c b/range-diff.c
index 7a96a587f1..ba1e9a4265 100644
--- a/range-diff.c
+++ b/range-diff.c
@@ -139,8 +139,10 @@ static int read_patches(const char *range, struct string_list *list)
 			strbuf_addstr(&buf, " ##");
 		} else if (in_header) {
 			if (starts_with(line, "Author: ")) {
+				strbuf_addstr(&buf, " ## Metadata ##\n");
 				strbuf_addstr(&buf, line);
 				strbuf_addstr(&buf, "\n\n");
+				strbuf_addstr(&buf, " ## Commit message ##\n");
 			} else if (starts_with(line, "    ")) {
 				p = line + len - 2;
 				while (isspace(*p) && p >= line)
@@ -402,8 +404,9 @@ static void output_pair_header(struct diff_options *diffopt,
 	fwrite(buf->buf, buf->len, 1, diffopt->file);
 }
 
-static struct userdiff_driver no_func_name = {
-	.funcname = { "$^", 0 }
+static struct userdiff_driver section_headers = {
+	.funcname = { "^ ## (.*) ##$\n"
+		      "^.?@@ (.*)$", REG_EXTENDED }
 };
 
 static struct diff_filespec *get_filespec(const char *name, const char *p)
@@ -415,7 +418,7 @@ static struct diff_filespec *get_filespec(const char *name, const char *p)
 	spec->size = strlen(p);
 	spec->should_munmap = 0;
 	spec->is_stdin = 1;
-	spec->driver = &no_func_name;
+	spec->driver = &section_headers;
 
 	return spec;
 }
diff --git a/t/t3206-range-diff.sh b/t/t3206-range-diff.sh
index d4de270979..ec548654ce 100755
--- a/t/t3206-range-diff.sh
+++ b/t/t3206-range-diff.sh
@@ -99,7 +99,7 @@ test_expect_success 'changed commit' '
 	1:  4de457d = 1:  a4b3333 s/5/A/
 	2:  fccce22 = 2:  f51d370 s/4/A/
 	3:  147e64e ! 3:  0559556 s/11/B/
-	    @@
+	    @@ file: A
 	      9
 	      10
 	     -11
@@ -109,7 +109,7 @@ test_expect_success 'changed commit' '
 	      13
 	      14
 	4:  a63e992 ! 4:  d966c5c s/12/B/
-	    @@
+	    @@ file
 	     @@ file: A
 	      9
 	      10
@@ -158,7 +158,7 @@ test_expect_success 'changed commit with sm config' '
 	1:  4de457d = 1:  a4b3333 s/5/A/
 	2:  fccce22 = 2:  f51d370 s/4/A/
 	3:  147e64e ! 3:  0559556 s/11/B/
-	    @@
+	    @@ file: A
 	      9
 	      10
 	     -11
@@ -168,7 +168,7 @@ test_expect_success 'changed commit with sm config' '
 	      13
 	      14
 	4:  a63e992 ! 4:  d966c5c s/12/B/
-	    @@
+	    @@ file
 	     @@ file: A
 	      9
 	      10
@@ -186,9 +186,10 @@ test_expect_success 'renamed file' '
 	sed s/Z/\ /g >expected <<-EOF &&
 	1:  4de457d = 1:  f258d75 s/5/A/
 	2:  fccce22 ! 2:  017b62d s/4/A/
-	    @@
+	    @@ Metadata
 	    ZAuthor: Thomas Rast <trast@inf.ethz.ch>
 	    Z
+	    Z ## Commit message ##
 	    -    s/4/A/
 	    +    s/4/A/ + rename file
 	    Z
@@ -198,8 +199,8 @@ test_expect_success 'renamed file' '
 	    Z 1
 	    Z 2
 	3:  147e64e ! 3:  3ce7af6 s/11/B/
-	    @@
-	    Z
+	    @@ Metadata
+	    Z ## Commit message ##
 	    Z    s/11/B/
 	    Z
 	    - ## file ##
@@ -210,8 +211,8 @@ test_expect_success 'renamed file' '
 	    Z 9
 	    Z 10
 	4:  a63e992 ! 4:  1e6226b s/12/B/
-	    @@
-	    Z
+	    @@ Metadata
+	    Z ## Commit message ##
 	    Z    s/12/B/
 	    Z
 	    - ## file ##
@@ -230,30 +231,32 @@ test_expect_success 'file added and later removed' '
 	sed s/Z/\ /g >expected <<-EOF &&
 	1:  4de457d = 1:  096b1ba s/5/A/
 	2:  fccce22 ! 2:  d92e698 s/4/A/
-	    @@
+	    @@ Metadata
 	    ZAuthor: Thomas Rast <trast@inf.ethz.ch>
 	    Z
+	    Z ## Commit message ##
 	    -    s/4/A/
 	    +    s/4/A/ + new-file
 	    Z
 	    Z ## file ##
 	    Z@@
-	    @@
+	    @@ file
 	    Z A
 	    Z 6
 	    Z 7
 	    +
 	    + ## new-file (new) ##
 	3:  147e64e ! 3:  9a1db4d s/11/B/
-	    @@
+	    @@ Metadata
 	    ZAuthor: Thomas Rast <trast@inf.ethz.ch>
 	    Z
+	    Z ## Commit message ##
 	    -    s/11/B/
 	    +    s/11/B/ + remove file
 	    Z
 	    Z ## file ##
 	    Z@@ file: A
-	    @@
+	    @@ file: A
 	    Z 12
 	    Z 13
 	    Z 14
@@ -274,8 +277,8 @@ test_expect_success 'changed message' '
 	sed s/Z/\ /g >expected <<-EOF &&
 	1:  4de457d = 1:  f686024 s/5/A/
 	2:  fccce22 ! 2:  4ab067d s/4/A/
-	    @@
-	    Z
+	    @@ Metadata
+	    Z ## Commit message ##
 	    Z    s/4/A/
 	    Z
 	    +    Also a silly comment here!
@@ -293,8 +296,8 @@ test_expect_success 'dual-coloring' '
 	sed -e "s|^:||" >expect <<-\EOF &&
 	:<YELLOW>1:  a4b3333 = 1:  f686024 s/5/A/<RESET>
 	:<RED>2:  f51d370 <RESET><YELLOW>!<RESET><GREEN> 2:  4ab067d<RESET><YELLOW> s/4/A/<RESET>
-	:    <REVERSE><CYAN>@@<RESET>
-	:     <RESET>
+	:    <REVERSE><CYAN>@@<RESET> <RESET>Metadata<RESET>
+	:      ## Commit message ##<RESET>
 	:         s/4/A/<RESET>
 	:     <RESET>
 	:    <REVERSE><GREEN>+<RESET><BOLD>    Also a silly comment here!<RESET>
@@ -303,7 +306,7 @@ test_expect_success 'dual-coloring' '
 	:    <CYAN> @@<RESET>
 	:      1<RESET>
 	:<RED>3:  0559556 <RESET><YELLOW>!<RESET><GREEN> 3:  b9cb956<RESET><YELLOW> s/11/B/<RESET>
-	:    <REVERSE><CYAN>@@<RESET>
+	:    <REVERSE><CYAN>@@<RESET> <RESET>file: A<RESET>
 	:      9<RESET>
 	:      10<RESET>
 	:    <RED> -11<RESET>
@@ -313,7 +316,7 @@ test_expect_success 'dual-coloring' '
 	:      13<RESET>
 	:      14<RESET>
 	:<RED>4:  d966c5c <RESET><YELLOW>!<RESET><GREEN> 4:  8add5f1<RESET><YELLOW> s/12/B/<RESET>
-	:    <REVERSE><CYAN>@@<RESET>
+	:    <REVERSE><CYAN>@@<RESET> <RESET>file<RESET>
 	:    <CYAN> @@ file: A<RESET>
 	:      9<RESET>
 	:      10<RESET>
-- 
2.22.0.510.g264f2c817a
Ramsay Jones· Jul 11, 2019, 22:09 UTC · re: Thomas Gummerer · lore

Re: [PATCH v4 00/14] output improvements for git range-diff

On 11/07/2019 17:08, Thomas Gummerer wrote:
Show 31 quoted lines
> Thanks Junio for the comment on the previous round [1].  This round
> reanmes the struct we're using in apply.c to 'struct gitdiff_data',
> and updates the commit message of 7/14 to reflect the new name of the
> renamed function.
> 
> [1]: https://public-inbox.org/git/20190708163315.29912-1-t.gummerer@gmail.com/
> 
> Thomas Gummerer (14):
>   apply: replace marc.info link with public-inbox
>   apply: only pass required data to skip_tree_prefix
>   apply: only pass required data to git_header_name
>   apply: only pass required data to check_header_line
>   apply: only pass required data to find_name_*
>   apply: only pass required data to gitdiff_* functions
>   apply: make parse_git_diff_header public
>   range-diff: fix function parameter indentation
>   range-diff: split lines manually
>   range-diff: don't remove funcname from inner diff
>   range-diff: suppress line count in outer diff
>   range-diff: add section header instead of diff header
>   range-diff: add filename to inner diff
>   range-diff: add headers to the outer hunk header
> 
>  apply.c                | 186 ++++++++++++++++++-----------------------
>  apply.h                |  48 +++++++++++
>  diff.c                 |   5 +-
>  diff.h                 |   1 +
>  range-diff.c           | 124 +++++++++++++++++++--------
>  t/t3206-range-diff.sh  | 124 ++++++++++++++++++++++-----
>  t/t3206/history.export |  84 ++++++++++++++++++-
>  7 files changed, 409 insertions(+), 163 deletions(-)

Yes, the patch I just sent related to the previous version of this series. However, I believe it still applies to this version (looking at the range-diff below).

ATB, Ramsay Jones

Show 221 quoted lines
> Range-diff against v3:
>  1:  ef2245edda =  1:  ef2245edda apply: replace marc.info link with public-inbox
>  2:  94578fa45c =  2:  94578fa45c apply: only pass required data to skip_tree_prefix
>  3:  988269a68e =  3:  988269a68e apply: only pass required data to git_header_name
>  4:  a2c1ef3f5f =  4:  a2c1ef3f5f apply: only pass required data to check_header_line
>  5:  0f4cfe21cb =  5:  0f4cfe21cb apply: only pass required data to find_name_*
>  6:  07a271518d !  6:  42665e5295 apply: only pass required data to gitdiff_* functions
>     @@ -28,7 +28,7 @@
>       #include "rerere.h"
>       #include "apply.h"
>       
>     -+struct parse_git_header_state {
>     ++struct gitdiff_data {
>      +	struct strbuf *root;
>      +	int linenr;
>      +	int p_value;
>     @@ -42,7 +42,7 @@
>       }
>       
>      -static int gitdiff_hdrend(struct apply_state *state,
>     -+static int gitdiff_hdrend(struct parse_git_header_state *state,
>     ++static int gitdiff_hdrend(struct gitdiff_data *state,
>       			  const char *line,
>       			  struct patch *patch)
>       {
>     @@ -51,7 +51,7 @@
>       #define DIFF_NEW_NAME 1
>       
>      -static int gitdiff_verify_name(struct apply_state *state,
>     -+static int gitdiff_verify_name(struct parse_git_header_state *state,
>     ++static int gitdiff_verify_name(struct gitdiff_data *state,
>       			       const char *line,
>       			       int isnull,
>       			       char **name,
>     @@ -77,7 +77,7 @@
>       }
>       
>      -static int gitdiff_oldname(struct apply_state *state,
>     -+static int gitdiff_oldname(struct parse_git_header_state *state,
>     ++static int gitdiff_oldname(struct gitdiff_data *state,
>       			   const char *line,
>       			   struct patch *patch)
>       {
>     @@ -86,7 +86,7 @@
>       }
>       
>      -static int gitdiff_newname(struct apply_state *state,
>     -+static int gitdiff_newname(struct parse_git_header_state *state,
>     ++static int gitdiff_newname(struct gitdiff_data *state,
>       			   const char *line,
>       			   struct patch *patch)
>       {
>     @@ -95,7 +95,7 @@
>       }
>       
>      -static int gitdiff_oldmode(struct apply_state *state,
>     -+static int gitdiff_oldmode(struct parse_git_header_state *state,
>     ++static int gitdiff_oldmode(struct gitdiff_data *state,
>       			   const char *line,
>       			   struct patch *patch)
>       {
>     @@ -103,7 +103,7 @@
>       }
>       
>      -static int gitdiff_newmode(struct apply_state *state,
>     -+static int gitdiff_newmode(struct parse_git_header_state *state,
>     ++static int gitdiff_newmode(struct gitdiff_data *state,
>       			   const char *line,
>       			   struct patch *patch)
>       {
>     @@ -111,7 +111,7 @@
>       }
>       
>      -static int gitdiff_delete(struct apply_state *state,
>     -+static int gitdiff_delete(struct parse_git_header_state *state,
>     ++static int gitdiff_delete(struct gitdiff_data *state,
>       			  const char *line,
>       			  struct patch *patch)
>       {
>     @@ -120,7 +120,7 @@
>       }
>       
>      -static int gitdiff_newfile(struct apply_state *state,
>     -+static int gitdiff_newfile(struct parse_git_header_state *state,
>     ++static int gitdiff_newfile(struct gitdiff_data *state,
>       			   const char *line,
>       			   struct patch *patch)
>       {
>     @@ -129,7 +129,7 @@
>       }
>       
>      -static int gitdiff_copysrc(struct apply_state *state,
>     -+static int gitdiff_copysrc(struct parse_git_header_state *state,
>     ++static int gitdiff_copysrc(struct gitdiff_data *state,
>       			   const char *line,
>       			   struct patch *patch)
>       {
>     @@ -141,7 +141,7 @@
>       }
>       
>      -static int gitdiff_copydst(struct apply_state *state,
>     -+static int gitdiff_copydst(struct parse_git_header_state *state,
>     ++static int gitdiff_copydst(struct gitdiff_data *state,
>       			   const char *line,
>       			   struct patch *patch)
>       {
>     @@ -153,7 +153,7 @@
>       }
>       
>      -static int gitdiff_renamesrc(struct apply_state *state,
>     -+static int gitdiff_renamesrc(struct parse_git_header_state *state,
>     ++static int gitdiff_renamesrc(struct gitdiff_data *state,
>       			     const char *line,
>       			     struct patch *patch)
>       {
>     @@ -165,7 +165,7 @@
>       }
>       
>      -static int gitdiff_renamedst(struct apply_state *state,
>     -+static int gitdiff_renamedst(struct parse_git_header_state *state,
>     ++static int gitdiff_renamedst(struct gitdiff_data *state,
>       			     const char *line,
>       			     struct patch *patch)
>       {
>     @@ -177,7 +177,7 @@
>       }
>       
>      -static int gitdiff_similarity(struct apply_state *state,
>     -+static int gitdiff_similarity(struct parse_git_header_state *state,
>     ++static int gitdiff_similarity(struct gitdiff_data *state,
>       			      const char *line,
>       			      struct patch *patch)
>       {
>     @@ -186,7 +186,7 @@
>       }
>       
>      -static int gitdiff_dissimilarity(struct apply_state *state,
>     -+static int gitdiff_dissimilarity(struct parse_git_header_state *state,
>     ++static int gitdiff_dissimilarity(struct gitdiff_data *state,
>       				 const char *line,
>       				 struct patch *patch)
>       {
>     @@ -195,7 +195,7 @@
>       }
>       
>      -static int gitdiff_index(struct apply_state *state,
>     -+static int gitdiff_index(struct parse_git_header_state *state,
>     ++static int gitdiff_index(struct gitdiff_data *state,
>       			 const char *line,
>       			 struct patch *patch)
>       {
>     @@ -204,7 +204,7 @@
>        * into the next diff. Tell the parser to break out.
>        */
>      -static int gitdiff_unrecognized(struct apply_state *state,
>     -+static int gitdiff_unrecognized(struct parse_git_header_state *state,
>     ++static int gitdiff_unrecognized(struct gitdiff_data *state,
>       				const char *line,
>       				struct patch *patch)
>       {
>     @@ -212,7 +212,7 @@
>       			    struct patch *patch)
>       {
>       	unsigned long offset;
>     -+	struct parse_git_header_state parse_hdr_state;
>     ++	struct gitdiff_data parse_hdr_state;
>       
>       	/* A git diff has explicit new/delete information, so we don't guess */
>       	patch->is_new = 0;
>     @@ -228,7 +228,7 @@
>       		static const struct opentry {
>       			const char *str;
>      -			int (*fn)(struct apply_state *, const char *, struct patch *);
>     -+			int (*fn)(struct parse_git_header_state *, const char *, struct patch *);
>     ++			int (*fn)(struct gitdiff_data *, const char *, struct patch *);
>       		} optable[] = {
>       			{ "@@ -", gitdiff_hdrend },
>       			{ "--- ", gitdiff_oldname },
>  7:  9cb6732a5f !  7:  3068fda8a9 apply: make parse_git_header public
>     @@ -1,9 +1,12 @@
>      Author: Thomas Gummerer <t.gummerer@gmail.com>
>      
>     -    apply: make parse_git_header public
>     +    apply: make parse_git_diff_header public
>      
>     -    Make parse_git_header a "public" function in apply.h, so we can re-use
>     -    it in range-diff in a subsequent commit.
>     +    Make 'parse_git_header()' (renamed to 'parse_git_diff_header()') a
>     +    "public" function in apply.h, so we can re-use it in range-diff in a
>     +    subsequent commit.  We're renaming the function to make it clearer in
>     +    other parts of the codebase that we're talking about a diff header and
>     +    not just any header.
>      
>          Signed-off-by: Thomas Gummerer <t.gummerer@gmail.com>
>      
>     @@ -70,7 +73,7 @@
>      +			  struct patch *patch)
>       {
>       	unsigned long offset;
>     - 	struct parse_git_header_state parse_hdr_state;
>     + 	struct gitdiff_data parse_hdr_state;
>      @@
>       	 * or removing or adding empty files), so we get
>       	 * the default name from the header.
>     @@ -100,7 +103,7 @@
>      +	for (offset = len ; size > 0 ; offset += len, size -= len, line += len, (*linenr)++) {
>       		static const struct opentry {
>       			const char *str;
>     - 			int (*fn)(struct parse_git_header_state *, const char *, struct patch *);
>     + 			int (*fn)(struct gitdiff_data *, const char *, struct patch *);
>      @@
>       			res = p->fn(&parse_hdr_state, line + oplen, patch);
>       			if (res < 0)
>  8:  76a11ce995 =  8:  781d054cab range-diff: fix function parameter indentation
>  9:  6f70e7faa6 =  9:  68a2953310 range-diff: split lines manually
> 10:  6618cdff2c = 10:  8ae95d053b range-diff: don't remove funcname from inner diff
> 11:  2667df4fa5 = 11:  e572510c52 range-diff: suppress line count in outer diff
> 12:  47cd8c6733 = 12:  45605db760 range-diff: add section header instead of diff header
> 13:  f67fd5dd9a = 13:  89a27fbeaa range-diff: add filename to inner diff
> 14:  812893a5dc = 14:  8bee2c525f range-diff: add headers to the outer hunk header
> 
Johannes Schindelin· Jul 12, 2019, 10:44 UTC · re: Thomas Gummerer · lore

Re: [PATCH v4 00/14] output improvements for git range-diff

Hi Thomas,
On Thu, 11 Jul 2019, Thomas Gummerer wrote:
> Thanks Junio for the comment on the previous round [1].  This round
> reanmes the struct we're using in apply.c to 'struct gitdiff_data',
> and updates the commit message of 7/14 to reflect the new name of the
> renamed function.
Yep, and the range-diff looks good to me, too.

Thank you! Dscho

Show 252 quoted lines
>
> [1]: https://public-inbox.org/git/20190708163315.29912-1-t.gummerer@gmail.com/
>
> Thomas Gummerer (14):
>   apply: replace marc.info link with public-inbox
>   apply: only pass required data to skip_tree_prefix
>   apply: only pass required data to git_header_name
>   apply: only pass required data to check_header_line
>   apply: only pass required data to find_name_*
>   apply: only pass required data to gitdiff_* functions
>   apply: make parse_git_diff_header public
>   range-diff: fix function parameter indentation
>   range-diff: split lines manually
>   range-diff: don't remove funcname from inner diff
>   range-diff: suppress line count in outer diff
>   range-diff: add section header instead of diff header
>   range-diff: add filename to inner diff
>   range-diff: add headers to the outer hunk header
>
>  apply.c                | 186 ++++++++++++++++++-----------------------
>  apply.h                |  48 +++++++++++
>  diff.c                 |   5 +-
>  diff.h                 |   1 +
>  range-diff.c           | 124 +++++++++++++++++++--------
>  t/t3206-range-diff.sh  | 124 ++++++++++++++++++++++-----
>  t/t3206/history.export |  84 ++++++++++++++++++-
>  7 files changed, 409 insertions(+), 163 deletions(-)
>
> Range-diff against v3:
>  1:  ef2245edda =  1:  ef2245edda apply: replace marc.info link with public-inbox
>  2:  94578fa45c =  2:  94578fa45c apply: only pass required data to skip_tree_prefix
>  3:  988269a68e =  3:  988269a68e apply: only pass required data to git_header_name
>  4:  a2c1ef3f5f =  4:  a2c1ef3f5f apply: only pass required data to check_header_line
>  5:  0f4cfe21cb =  5:  0f4cfe21cb apply: only pass required data to find_name_*
>  6:  07a271518d !  6:  42665e5295 apply: only pass required data to gitdiff_* functions
>     @@ -28,7 +28,7 @@
>       #include "rerere.h"
>       #include "apply.h"
>
>     -+struct parse_git_header_state {
>     ++struct gitdiff_data {
>      +	struct strbuf *root;
>      +	int linenr;
>      +	int p_value;
>     @@ -42,7 +42,7 @@
>       }
>
>      -static int gitdiff_hdrend(struct apply_state *state,
>     -+static int gitdiff_hdrend(struct parse_git_header_state *state,
>     ++static int gitdiff_hdrend(struct gitdiff_data *state,
>       			  const char *line,
>       			  struct patch *patch)
>       {
>     @@ -51,7 +51,7 @@
>       #define DIFF_NEW_NAME 1
>
>      -static int gitdiff_verify_name(struct apply_state *state,
>     -+static int gitdiff_verify_name(struct parse_git_header_state *state,
>     ++static int gitdiff_verify_name(struct gitdiff_data *state,
>       			       const char *line,
>       			       int isnull,
>       			       char **name,
>     @@ -77,7 +77,7 @@
>       }
>
>      -static int gitdiff_oldname(struct apply_state *state,
>     -+static int gitdiff_oldname(struct parse_git_header_state *state,
>     ++static int gitdiff_oldname(struct gitdiff_data *state,
>       			   const char *line,
>       			   struct patch *patch)
>       {
>     @@ -86,7 +86,7 @@
>       }
>
>      -static int gitdiff_newname(struct apply_state *state,
>     -+static int gitdiff_newname(struct parse_git_header_state *state,
>     ++static int gitdiff_newname(struct gitdiff_data *state,
>       			   const char *line,
>       			   struct patch *patch)
>       {
>     @@ -95,7 +95,7 @@
>       }
>
>      -static int gitdiff_oldmode(struct apply_state *state,
>     -+static int gitdiff_oldmode(struct parse_git_header_state *state,
>     ++static int gitdiff_oldmode(struct gitdiff_data *state,
>       			   const char *line,
>       			   struct patch *patch)
>       {
>     @@ -103,7 +103,7 @@
>       }
>
>      -static int gitdiff_newmode(struct apply_state *state,
>     -+static int gitdiff_newmode(struct parse_git_header_state *state,
>     ++static int gitdiff_newmode(struct gitdiff_data *state,
>       			   const char *line,
>       			   struct patch *patch)
>       {
>     @@ -111,7 +111,7 @@
>       }
>
>      -static int gitdiff_delete(struct apply_state *state,
>     -+static int gitdiff_delete(struct parse_git_header_state *state,
>     ++static int gitdiff_delete(struct gitdiff_data *state,
>       			  const char *line,
>       			  struct patch *patch)
>       {
>     @@ -120,7 +120,7 @@
>       }
>
>      -static int gitdiff_newfile(struct apply_state *state,
>     -+static int gitdiff_newfile(struct parse_git_header_state *state,
>     ++static int gitdiff_newfile(struct gitdiff_data *state,
>       			   const char *line,
>       			   struct patch *patch)
>       {
>     @@ -129,7 +129,7 @@
>       }
>
>      -static int gitdiff_copysrc(struct apply_state *state,
>     -+static int gitdiff_copysrc(struct parse_git_header_state *state,
>     ++static int gitdiff_copysrc(struct gitdiff_data *state,
>       			   const char *line,
>       			   struct patch *patch)
>       {
>     @@ -141,7 +141,7 @@
>       }
>
>      -static int gitdiff_copydst(struct apply_state *state,
>     -+static int gitdiff_copydst(struct parse_git_header_state *state,
>     ++static int gitdiff_copydst(struct gitdiff_data *state,
>       			   const char *line,
>       			   struct patch *patch)
>       {
>     @@ -153,7 +153,7 @@
>       }
>
>      -static int gitdiff_renamesrc(struct apply_state *state,
>     -+static int gitdiff_renamesrc(struct parse_git_header_state *state,
>     ++static int gitdiff_renamesrc(struct gitdiff_data *state,
>       			     const char *line,
>       			     struct patch *patch)
>       {
>     @@ -165,7 +165,7 @@
>       }
>
>      -static int gitdiff_renamedst(struct apply_state *state,
>     -+static int gitdiff_renamedst(struct parse_git_header_state *state,
>     ++static int gitdiff_renamedst(struct gitdiff_data *state,
>       			     const char *line,
>       			     struct patch *patch)
>       {
>     @@ -177,7 +177,7 @@
>       }
>
>      -static int gitdiff_similarity(struct apply_state *state,
>     -+static int gitdiff_similarity(struct parse_git_header_state *state,
>     ++static int gitdiff_similarity(struct gitdiff_data *state,
>       			      const char *line,
>       			      struct patch *patch)
>       {
>     @@ -186,7 +186,7 @@
>       }
>
>      -static int gitdiff_dissimilarity(struct apply_state *state,
>     -+static int gitdiff_dissimilarity(struct parse_git_header_state *state,
>     ++static int gitdiff_dissimilarity(struct gitdiff_data *state,
>       				 const char *line,
>       				 struct patch *patch)
>       {
>     @@ -195,7 +195,7 @@
>       }
>
>      -static int gitdiff_index(struct apply_state *state,
>     -+static int gitdiff_index(struct parse_git_header_state *state,
>     ++static int gitdiff_index(struct gitdiff_data *state,
>       			 const char *line,
>       			 struct patch *patch)
>       {
>     @@ -204,7 +204,7 @@
>        * into the next diff. Tell the parser to break out.
>        */
>      -static int gitdiff_unrecognized(struct apply_state *state,
>     -+static int gitdiff_unrecognized(struct parse_git_header_state *state,
>     ++static int gitdiff_unrecognized(struct gitdiff_data *state,
>       				const char *line,
>       				struct patch *patch)
>       {
>     @@ -212,7 +212,7 @@
>       			    struct patch *patch)
>       {
>       	unsigned long offset;
>     -+	struct parse_git_header_state parse_hdr_state;
>     ++	struct gitdiff_data parse_hdr_state;
>
>       	/* A git diff has explicit new/delete information, so we don't guess */
>       	patch->is_new = 0;
>     @@ -228,7 +228,7 @@
>       		static const struct opentry {
>       			const char *str;
>      -			int (*fn)(struct apply_state *, const char *, struct patch *);
>     -+			int (*fn)(struct parse_git_header_state *, const char *, struct patch *);
>     ++			int (*fn)(struct gitdiff_data *, const char *, struct patch *);
>       		} optable[] = {
>       			{ "@@ -", gitdiff_hdrend },
>       			{ "--- ", gitdiff_oldname },
>  7:  9cb6732a5f !  7:  3068fda8a9 apply: make parse_git_header public
>     @@ -1,9 +1,12 @@
>      Author: Thomas Gummerer <t.gummerer@gmail.com>
>
>     -    apply: make parse_git_header public
>     +    apply: make parse_git_diff_header public
>
>     -    Make parse_git_header a "public" function in apply.h, so we can re-use
>     -    it in range-diff in a subsequent commit.
>     +    Make 'parse_git_header()' (renamed to 'parse_git_diff_header()') a
>     +    "public" function in apply.h, so we can re-use it in range-diff in a
>     +    subsequent commit.  We're renaming the function to make it clearer in
>     +    other parts of the codebase that we're talking about a diff header and
>     +    not just any header.
>
>          Signed-off-by: Thomas Gummerer <t.gummerer@gmail.com>
>
>     @@ -70,7 +73,7 @@
>      +			  struct patch *patch)
>       {
>       	unsigned long offset;
>     - 	struct parse_git_header_state parse_hdr_state;
>     + 	struct gitdiff_data parse_hdr_state;
>      @@
>       	 * or removing or adding empty files), so we get
>       	 * the default name from the header.
>     @@ -100,7 +103,7 @@
>      +	for (offset = len ; size > 0 ; offset += len, size -= len, line += len, (*linenr)++) {
>       		static const struct opentry {
>       			const char *str;
>     - 			int (*fn)(struct parse_git_header_state *, const char *, struct patch *);
>     + 			int (*fn)(struct gitdiff_data *, const char *, struct patch *);
>      @@
>       			res = p->fn(&parse_hdr_state, line + oplen, patch);
>       			if (res < 0)
>  8:  76a11ce995 =  8:  781d054cab range-diff: fix function parameter indentation
>  9:  6f70e7faa6 =  9:  68a2953310 range-diff: split lines manually
> 10:  6618cdff2c = 10:  8ae95d053b range-diff: don't remove funcname from inner diff
> 11:  2667df4fa5 = 11:  e572510c52 range-diff: suppress line count in outer diff
> 12:  47cd8c6733 = 12:  45605db760 range-diff: add section header instead of diff header
> 13:  f67fd5dd9a = 13:  89a27fbeaa range-diff: add filename to inner diff
> 14:  812893a5dc = 14:  8bee2c525f range-diff: add headers to the outer hunk header
>
> --
> 2.22.0.510.g264f2c817a
>

← back to recent threads