Skip to content

Commit 07ec215

Browse files
committed
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 <[email protected]>
1 parent 9b36323 commit 07ec215

File tree

1 file changed

+9
-7
lines changed

1 file changed

+9
-7
lines changed

range-diff.c

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -259,6 +259,7 @@ static void get_correspondences(struct string_list *a, struct string_list *b,
259259
}
260260

261261
static void output_pair_header(struct diff_options *diffopt,
262+
int patch_no_width,
262263
struct strbuf *buf,
263264
struct strbuf *dashes,
264265
struct patch_util *a_util,
@@ -295,9 +296,9 @@ static void output_pair_header(struct diff_options *diffopt,
295296
strbuf_reset(buf);
296297
strbuf_addstr(buf, status == '!' ? color_old : color);
297298
if (!a_util)
298-
strbuf_addf(buf, "-: %s ", dashes->buf);
299+
strbuf_addf(buf, "%*s: %s ", patch_no_width, "-", dashes->buf);
299300
else
300-
strbuf_addf(buf, "%d: %s ", a_util->i + 1,
301+
strbuf_addf(buf, "%*d: %s ", patch_no_width, a_util->i + 1,
301302
find_unique_abbrev(&a_util->oid, DEFAULT_ABBREV));
302303

303304
if (status == '!')
@@ -307,9 +308,9 @@ static void output_pair_header(struct diff_options *diffopt,
307308
strbuf_addf(buf, "%s%s", color_reset, color_new);
308309

309310
if (!b_util)
310-
strbuf_addf(buf, " -: %s", dashes->buf);
311+
strbuf_addf(buf, " %*s: %s", patch_no_width, "-", dashes->buf);
311312
else
312-
strbuf_addf(buf, " %d: %s", b_util->i + 1,
313+
strbuf_addf(buf, " %*d: %s", patch_no_width, b_util->i + 1,
313314
find_unique_abbrev(&b_util->oid, DEFAULT_ABBREV));
314315

315316
commit = lookup_commit_reference(oid);
@@ -362,6 +363,7 @@ static void output(struct string_list *a, struct string_list *b,
362363
struct diff_options *diffopt)
363364
{
364365
struct strbuf buf = STRBUF_INIT, dashes = STRBUF_INIT;
366+
int patch_no_width = decimal_width(1 + (a->nr > b->nr ? a->nr : b->nr));
365367
int i = 0, j = 0;
366368

367369
/*
@@ -383,23 +385,23 @@ static void output(struct string_list *a, struct string_list *b,
383385

384386
/* Show unmatched LHS commit whose predecessors were shown. */
385387
if (i < a->nr && a_util->matching < 0) {
386-
output_pair_header(diffopt,
388+
output_pair_header(diffopt, patch_no_width,
387389
&buf, &dashes, a_util, NULL);
388390
i++;
389391
continue;
390392
}
391393

392394
/* Show unmatched RHS commits. */
393395
while (j < b->nr && b_util->matching < 0) {
394-
output_pair_header(diffopt,
396+
output_pair_header(diffopt, patch_no_width,
395397
&buf, &dashes, NULL, b_util);
396398
b_util = ++j < b->nr ? b->items[j].util : NULL;
397399
}
398400

399401
/* Show matching LHS/RHS pair. */
400402
if (j < b->nr) {
401403
a_util = a->items[b_util->matching].util;
402-
output_pair_header(diffopt,
404+
output_pair_header(diffopt, patch_no_width,
403405
&buf, &dashes, a_util, b_util);
404406
if (!(diffopt->output_format & DIFF_FORMAT_NO_OUTPUT))
405407
patch_diff(a->items[b_util->matching].string,

0 commit comments

Comments
 (0)