add -p (built-in): imitate `xdl_format_hunk_hdr()` generating hunk headers
In libxdiff, imitating GNU diff, the hunk headers only show the line count if it is different from 1. When splitting hunks, the Perl version of `git add -p` already imitates this. Let's do the same in the built-in version of said command. Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Johannes Schindelin committed
Nov 11, 2020 at 12:28 UTC
decc9ee4eaf2c33c28e2958439d276904a2ce279
1 file changed
+8
-3
add-patch.c
+8
-3
@@ -661,9 +661,14 @@ static void render_hunk(struct add_p_state *s, struct hunk *hunk,
661
else
662
new_offset += delta;
663
664
- strbuf_addf(out, "@@ -%lu,%lu +%lu,%lu @@",
665
- old_offset, header->old_count,
666
- new_offset, header->new_count);
664
+ strbuf_addf(out, "@@ -%lu", old_offset);
665
+ if (header->old_count != 1)
666
+ strbuf_addf(out, ",%lu", header->old_count);
667
+ strbuf_addf(out, " +%lu", new_offset);
668
+ if (header->new_count != 1)
669
+ strbuf_addf(out, ",%lu", header->new_count);
670
+ strbuf_addstr(out, " @@");
671
+
672
if (len)
673
strbuf_add(out, p, len);
674
else if (colored)