range-diff: left-pad patch numbers

As pointed out by Elijah Newren, tbdiff has this neat little alignment trick where it outputs the commit pairs with patch numbers that are padded to the maximal patch number's width: 1: cafedead = 1: acefade first patch [...] 314: beefeada < 314: facecab up to PI! Let's do the same in range-diff, too. Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Johannes Schindelin committed Aug 13, 2018 at 04:33 UTC d1f87a2d9c4877ada003c231b92de7560eab62ed
1 file changed +9 -7
range-diff.c
+9 -7
@@ -259,6 +259,7 @@ static void get_correspondences(struct string_list *a, struct string_list *b,
259 }
260
261 static void output_pair_header(struct diff_options *diffopt,
262 + int patch_no_width,
263 struct strbuf *buf,
264 struct strbuf *dashes,
265 struct patch_util *a_util,
@@ -295,9 +296,9 @@ static void output_pair_header(struct diff_options *diffopt,
296 strbuf_reset(buf);
297 strbuf_addstr(buf, status == '!' ? color_old : color);
298 if (!a_util)
298 - strbuf_addf(buf, "-: %s ", dashes->buf);
299 + strbuf_addf(buf, "%*s: %s ", patch_no_width, "-", dashes->buf);
300 else
300 - strbuf_addf(buf, "%d: %s ", a_util->i + 1,
301 + strbuf_addf(buf, "%*d: %s ", patch_no_width, a_util->i + 1,
302 find_unique_abbrev(&a_util->oid, DEFAULT_ABBREV));
303
304 if (status == '!')
@@ -307,9 +308,9 @@ static void output_pair_header(struct diff_options *diffopt,
308 strbuf_addf(buf, "%s%s", color_reset, color_new);
309
310 if (!b_util)
310 - strbuf_addf(buf, " -: %s", dashes->buf);
311 + strbuf_addf(buf, " %*s: %s", patch_no_width, "-", dashes->buf);
312 else
312 - strbuf_addf(buf, " %d: %s", b_util->i + 1,
313 + strbuf_addf(buf, " %*d: %s", patch_no_width, b_util->i + 1,
314 find_unique_abbrev(&b_util->oid, DEFAULT_ABBREV));
315
316 commit = lookup_commit_reference(the_repository, oid);
@@ -357,6 +358,7 @@ static void output(struct string_list *a, struct string_list *b,
358 struct diff_options *diffopt)
359 {
360 struct strbuf buf = STRBUF_INIT, dashes = STRBUF_INIT;
361 + int patch_no_width = decimal_width(1 + (a->nr > b->nr ? a->nr : b->nr));
362 int i = 0, j = 0;
363
364 /*
@@ -378,7 +380,7 @@ static void output(struct string_list *a, struct string_list *b,
380
381 /* Show unmatched LHS commit whose predecessors were shown. */
382 if (i < a->nr && a_util->matching < 0) {
381 - output_pair_header(diffopt,
383 + output_pair_header(diffopt, patch_no_width,
384 &buf, &dashes, a_util, NULL);
385 i++;
386 continue;
@@ -386,7 +388,7 @@ static void output(struct string_list *a, struct string_list *b,
388
389 /* Show unmatched RHS commits. */
390 while (j < b->nr && b_util->matching < 0) {
389 - output_pair_header(diffopt,
391 + output_pair_header(diffopt, patch_no_width,
392 &buf, &dashes, NULL, b_util);
393 b_util = ++j < b->nr ? b->items[j].util : NULL;
394 }
@@ -394,7 +396,7 @@ static void output(struct string_list *a, struct string_list *b,
396 /* Show matching LHS/RHS pair. */
397 if (j < b->nr) {
398 a_util = a->items[b_util->matching].util;
397 - output_pair_header(diffopt,
399 + output_pair_header(diffopt, patch_no_width,
400 &buf, &dashes, a_util, b_util);
401 if (!(diffopt->output_format & DIFF_FORMAT_NO_OUTPUT))
402 patch_diff(a->items[b_util->matching].string,