diff mbox series

[RFC,3/4] range-diff: add section header instead of diff header

Message ID 20190414210933.20875-4-t.gummerer@gmail.com (mailing list archive)
State New, archived
Headers show
Series output improvements for git range-diff | expand

Commit Message

Thomas Gummerer April 14, 2019, 9:09 p.m. UTC
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.

At this point, this is only a marginal improvement in readability of
the range-diff output.  More interestingly however, this allows us to
add these range diff hunk 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 ++++++++++++++++++++++++++++++++--
 1 file changed, 32 insertions(+), 2 deletions(-)

Comments

Eric Sunshine April 14, 2019, 11:29 p.m. UTC | #1
On Sun, Apr 14, 2019 at 5:10 PM Thomas Gummerer <t.gummerer@gmail.com> wrote:
> [...]
> 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.
> [...]
> Signed-off-by: Thomas Gummerer <t.gummerer@gmail.com>
> ---
> diff --git a/range-diff.c b/range-diff.c
> @@ -90,8 +91,37 @@ static int read_patches(const char *range, struct string_list *list)
> +               } else if (starts_with(line.buf, "--- ")) {
> +                       if (!strcmp(line.buf, "--- /dev/null"))
> +                               strbuf_remove(&line, 0, 4);
> +                       else
> +                               strbuf_remove(&line, 0, 6);
> +                       strbuf_rtrim(&line);
> +                       strbuf_reset(&filename_a);
> +                       strbuf_addbuf(&filename_a, &line);
> +               } else if (starts_with(line.buf, "+++ ")) {

At this point, we know that line.buf starts with "+++"...

> +                       strbuf_addstr(&buf, " ## ");
> +                       if (!strcmp(line.buf, "--- /dev/null"))

so, it seems unlikely that it's ever going to match "--- /dev/null".

> +                               strbuf_remove(&line, 0, 4);
> +                       if (!strcmp(filename_a.buf, "/dev/null")) {
> +                               strbuf_addstr(&buf, "new file ");
> +                               strbuf_addbuf(&buf, &line);
> +                       } else if (!strcmp(line.buf, "/dev/null")) {
> +                               strbuf_addstr(&buf, "removed file ");
> +                               strbuf_addbuf(&buf, &line);
> +                       } else if (strbuf_cmp(&filename_a, &line)) {
> +                               strbuf_addstr(&buf, "renamed file ");
> +                               strbuf_addbuf(&buf, &filename_a);
> +                               strbuf_addstr(&buf, " -> ");
> +                               strbuf_addbuf(&buf, &line);
> +                       } else {
> +                               strbuf_addstr(&buf, "modified file ");
> +                               strbuf_addbuf(&buf, &line);
> +                       }

All of these disposition strings end with "file", which seems
redundant. Short and sweet "new", "removed", "renamed", "modified"
provide just as much useful information.

Also, should these strings be localizable? Alternately, rather than
using prose to describe the disposition, perhaps do so symbolically
(thus universally), say with "+", "-", "->", "*" (or ""),
respectively?
Johannes Sixt April 15, 2019, 6:28 a.m. UTC | #2
Am 15.04.19 um 01:29 schrieb Eric Sunshine:
> On Sun, Apr 14, 2019 at 5:10 PM Thomas Gummerer <t.gummerer@gmail.com> wrote:
>> +                               strbuf_remove(&line, 0, 4);
>> +                       if (!strcmp(filename_a.buf, "/dev/null")) {
>> +                               strbuf_addstr(&buf, "new file ");
>> +                               strbuf_addbuf(&buf, &line);
>> +                       } else if (!strcmp(line.buf, "/dev/null")) {
>> +                               strbuf_addstr(&buf, "removed file ");
>> +                               strbuf_addbuf(&buf, &line);
>> +                       } else if (strbuf_cmp(&filename_a, &line)) {
>> +                               strbuf_addstr(&buf, "renamed file ");
>> +                               strbuf_addbuf(&buf, &filename_a);
>> +                               strbuf_addstr(&buf, " -> ");
>> +                               strbuf_addbuf(&buf, &line);
>> +                       } else {
>> +                               strbuf_addstr(&buf, "modified file ");
>> +                               strbuf_addbuf(&buf, &line);
>> +                       }
> 
> All of these disposition strings end with "file", which seems
> redundant. Short and sweet "new", "removed", "renamed", "modified"
> provide just as much useful information.
> 
> Also, should these strings be localizable? Alternately, rather than
> using prose to describe the disposition, perhaps do so symbolically
> (thus universally), say with "+", "-", "->", "*" (or ""),
> respectively?
When the strings are translated, it would be preferable to not do this
sentence lego at all and have format strings that are filled with the
values.

Then the noise word "file" is probably not that bad, in particular, when
translators tend to not add words that they don't see in the original.
For example, German translations of "new %s" I anticipate "neues %s",
"%s (neu)", "neu %s", all of which are awkward, but translations of "new
file %s" lends itself naturally to "neue Datei %s", which is fine.

-- Hannes
Johannes Schindelin April 15, 2019, 1:01 p.m. UTC | #3
Hi Eric,

On Sun, 14 Apr 2019, Eric Sunshine wrote:

> On Sun, Apr 14, 2019 at 5:10 PM Thomas Gummerer <t.gummerer@gmail.com> wrote:
> > [...]
> > 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.
> > [...]
> > Signed-off-by: Thomas Gummerer <t.gummerer@gmail.com>
> > ---
> > diff --git a/range-diff.c b/range-diff.c
> > @@ -90,8 +91,37 @@ static int read_patches(const char *range, struct string_list *list)
> > +               } else if (starts_with(line.buf, "--- ")) {
> > +                       if (!strcmp(line.buf, "--- /dev/null"))
> > +                               strbuf_remove(&line, 0, 4);
> > +                       else
> > +                               strbuf_remove(&line, 0, 6);
> > +                       strbuf_rtrim(&line);
> > +                       strbuf_reset(&filename_a);
> > +                       strbuf_addbuf(&filename_a, &line);
> > +               } else if (starts_with(line.buf, "+++ ")) {
>
> At this point, we know that line.buf starts with "+++"...
>
> > +                       strbuf_addstr(&buf, " ## ");
> > +                       if (!strcmp(line.buf, "--- /dev/null"))
>
> so, it seems unlikely that it's ever going to match "--- /dev/null".
>
> > +                               strbuf_remove(&line, 0, 4);
> > +                       if (!strcmp(filename_a.buf, "/dev/null")) {
> > +                               strbuf_addstr(&buf, "new file ");
> > +                               strbuf_addbuf(&buf, &line);
> > +                       } else if (!strcmp(line.buf, "/dev/null")) {
> > +                               strbuf_addstr(&buf, "removed file ");
> > +                               strbuf_addbuf(&buf, &line);
> > +                       } else if (strbuf_cmp(&filename_a, &line)) {
> > +                               strbuf_addstr(&buf, "renamed file ");
> > +                               strbuf_addbuf(&buf, &filename_a);
> > +                               strbuf_addstr(&buf, " -> ");
> > +                               strbuf_addbuf(&buf, &line);
> > +                       } else {
> > +                               strbuf_addstr(&buf, "modified file ");
> > +                               strbuf_addbuf(&buf, &line);
> > +                       }
>
> All of these disposition strings end with "file", which seems
> redundant. Short and sweet "new", "removed", "renamed", "modified"
> provide just as much useful information.
>
> Also, should these strings be localizable?

I'd rather not.

> Alternately, rather than using prose to describe the disposition,
> perhaps do so symbolically (thus universally), say with "+", "-", "->",
> "*" (or ""), respectively?

Or maybe streamline the common case (modified) by *not* saying anything,
then? I.e.

	@@ Documentation/Makefile

for a modified file,

	@@ builtin/psuh.c (new)

for a new file,

	@@ git-add--interactive.perl (deleted)

for a removed one, and

	@@ builtin/serve.c -> t/helper/test-serve-v2.c

for a renamed one.

That should also give us a bit of wiggle room to append the function name
part of the inner hunk header, if any.

Ciao,
Dscho
Thomas Gummerer April 15, 2019, 7:09 p.m. UTC | #4
On 04/15, Johannes Schindelin wrote:
> Hi Eric,
> 
> On Sun, 14 Apr 2019, Eric Sunshine wrote:
> 
> > On Sun, Apr 14, 2019 at 5:10 PM Thomas Gummerer <t.gummerer@gmail.com> wrote:
> > > [...]
> > > 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.
> > > [...]
> > > Signed-off-by: Thomas Gummerer <t.gummerer@gmail.com>
> > > ---
> > > diff --git a/range-diff.c b/range-diff.c
> > > @@ -90,8 +91,37 @@ static int read_patches(const char *range, struct string_list *list)
> > > +               } else if (starts_with(line.buf, "--- ")) {
> > > +                       if (!strcmp(line.buf, "--- /dev/null"))
> > > +                               strbuf_remove(&line, 0, 4);
> > > +                       else
> > > +                               strbuf_remove(&line, 0, 6);
> > > +                       strbuf_rtrim(&line);
> > > +                       strbuf_reset(&filename_a);
> > > +                       strbuf_addbuf(&filename_a, &line);
> > > +               } else if (starts_with(line.buf, "+++ ")) {
> >
> > At this point, we know that line.buf starts with "+++"...
> >
> > > +                       strbuf_addstr(&buf, " ## ");
> > > +                       if (!strcmp(line.buf, "--- /dev/null"))
> >
> > so, it seems unlikely that it's ever going to match "--- /dev/null".

Ouch yup, this is some bad copy pasta, thanks for catching!

> > > +                               strbuf_remove(&line, 0, 4);
> > > +                       if (!strcmp(filename_a.buf, "/dev/null")) {
> > > +                               strbuf_addstr(&buf, "new file ");
> > > +                               strbuf_addbuf(&buf, &line);
> > > +                       } else if (!strcmp(line.buf, "/dev/null")) {
> > > +                               strbuf_addstr(&buf, "removed file ");
> > > +                               strbuf_addbuf(&buf, &line);
> > > +                       } else if (strbuf_cmp(&filename_a, &line)) {
> > > +                               strbuf_addstr(&buf, "renamed file ");
> > > +                               strbuf_addbuf(&buf, &filename_a);
> > > +                               strbuf_addstr(&buf, " -> ");
> > > +                               strbuf_addbuf(&buf, &line);
> > > +                       } else {
> > > +                               strbuf_addstr(&buf, "modified file ");
> > > +                               strbuf_addbuf(&buf, &line);
> > > +                       }
> >
> > All of these disposition strings end with "file", which seems
> > redundant. Short and sweet "new", "removed", "renamed", "modified"
> > provide just as much useful information.
> >
> > Also, should these strings be localizable?
> 
> I'd rather not.

Dunno, why do you think they should not be localizable?  I'm tend to
agree with Eric that they could be made localizable, after all this
output is not supposed to be machine readable either way.  I don't
have a strong opinion here though.

> > Alternately, rather than using prose to describe the disposition,
> > perhaps do so symbolically (thus universally), say with "+", "-", "->",
> > "*" (or ""), respectively?
> 
> Or maybe streamline the common case (modified) by *not* saying anything,
> then? I.e.
> 
> 	@@ Documentation/Makefile
> 
> for a modified file,
> 
> 	@@ builtin/psuh.c (new)
> 
> for a new file,
> 
> 	@@ git-add--interactive.perl (deleted)
> 
> for a removed one, and
> 
> 	@@ builtin/serve.c -> t/helper/test-serve-v2.c
> 
> for a renamed one.

This looks like a good suggestion to me, thanks!

> That should also give us a bit of wiggle room to append the function name
> part of the inner hunk header, if any.
> 
> Ciao,
> Dscho
diff mbox series

Patch

diff --git a/range-diff.c b/range-diff.c
index f365141ade..aa466060ef 100644
--- a/range-diff.c
+++ b/range-diff.c
@@ -33,6 +33,7 @@  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 filename_a = STRBUF_INIT;
 	struct patch_util *util = NULL;
 	int in_header = 1;
 
@@ -90,8 +91,37 @@  static int read_patches(const char *range, struct string_list *list)
 			strbuf_addch(&buf, '\n');
 			if (!util->diff_offset)
 				util->diff_offset = buf.len;
-			strbuf_addch(&buf, ' ');
-			strbuf_addbuf(&buf, &line);
+		} else if (starts_with(line.buf, "--- ")) {
+			if (!strcmp(line.buf, "--- /dev/null"))
+				strbuf_remove(&line, 0, 4);
+			else
+				strbuf_remove(&line, 0, 6);
+			strbuf_rtrim(&line);
+			strbuf_reset(&filename_a);
+			strbuf_addbuf(&filename_a, &line);
+		} else if (starts_with(line.buf, "+++ ")) {
+			strbuf_addstr(&buf, " ## ");
+			if (!strcmp(line.buf, "--- /dev/null"))
+				strbuf_remove(&line, 0, 4);
+			else
+				strbuf_remove(&line, 0, 6);
+			strbuf_rtrim(&line);
+			if (!strcmp(filename_a.buf, "/dev/null")) {
+				strbuf_addstr(&buf, "new file ");
+				strbuf_addbuf(&buf, &line);
+			} else if (!strcmp(line.buf, "/dev/null")) {
+				strbuf_addstr(&buf, "removed file ");
+				strbuf_addbuf(&buf, &line);
+			} else if (strbuf_cmp(&filename_a, &line)) {
+				strbuf_addstr(&buf, "renamed file ");
+				strbuf_addbuf(&buf, &filename_a);
+				strbuf_addstr(&buf, " -> ");
+				strbuf_addbuf(&buf, &line);
+			} else {
+				strbuf_addstr(&buf, "modified file ");
+				strbuf_addbuf(&buf, &line);
+			}
+			strbuf_addstr(&buf, " ##\n");
 		} else if (in_header) {
 			if (starts_with(line.buf, "Author: ")) {
 				strbuf_addbuf(&buf, &line);