Skip to content

Commit 7b16bd3

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 40ee939 commit 7b16bd3

File tree

1 file changed

+13
-8
lines changed

1 file changed

+13
-8
lines changed

range-diff.c

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -254,7 +254,8 @@ static void get_correspondences(struct string_list *a, struct string_list *b,
254254
free(b2a);
255255
}
256256

257-
static void output_pair_header(struct diff_options *diffopt, struct strbuf *buf,
257+
static void output_pair_header(struct diff_options *diffopt, int patch_no_width,
258+
struct strbuf *buf,
258259
struct patch_util *a_util,
259260
struct patch_util *b_util)
260261
{
@@ -293,9 +294,9 @@ static void output_pair_header(struct diff_options *diffopt, struct strbuf *buf,
293294
strbuf_reset(buf);
294295
strbuf_addstr(buf, status == '!' ? color_old : color);
295296
if (!a_util)
296-
strbuf_addf(buf, "-: %s ", dashes);
297+
strbuf_addf(buf, "%*s: %s ", patch_no_width, "-", dashes);
297298
else
298-
strbuf_addf(buf, "%d: %s ", a_util->i + 1,
299+
strbuf_addf(buf, "%*d: %s ", patch_no_width, a_util->i + 1,
299300
find_unique_abbrev(a_util->oid.hash,
300301
DEFAULT_ABBREV));
301302

@@ -306,9 +307,9 @@ static void output_pair_header(struct diff_options *diffopt, struct strbuf *buf,
306307
strbuf_addf(buf, "%s%s", color_reset, color_new);
307308

308309
if (!b_util)
309-
strbuf_addf(buf, " -: %s", dashes);
310+
strbuf_addf(buf, " %*s: %s", patch_no_width, "-", dashes);
310311
else
311-
strbuf_addf(buf, " %d: %s", b_util->i + 1,
312+
strbuf_addf(buf, " %*d: %s", patch_no_width, b_util->i + 1,
312313
find_unique_abbrev(b_util->oid.hash,
313314
DEFAULT_ABBREV));
314315

@@ -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;
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,21 +385,24 @@ 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, &buf, a_util, NULL);
388+
output_pair_header(diffopt, patch_no_width, &buf,
389+
a_util, NULL);
387390
i++;
388391
continue;
389392
}
390393

391394
/* Show unmatched RHS commits. */
392395
while (j < b->nr && b_util->matching < 0) {
393-
output_pair_header(diffopt, &buf, NULL, b_util);
396+
output_pair_header(diffopt, patch_no_width, &buf,
397+
NULL, b_util);
394398
b_util = ++j < b->nr ? b->items[j].util : NULL;
395399
}
396400

397401
/* Show matching LHS/RHS pair. */
398402
if (j < b->nr) {
399403
a_util = a->items[b_util->matching].util;
400-
output_pair_header(diffopt, &buf, a_util, b_util);
404+
output_pair_header(diffopt, patch_no_width, &buf,
405+
a_util, b_util);
401406
if (!(diffopt->output_format & DIFF_FORMAT_NO_OUTPUT))
402407
patch_diff(a->items[b_util->matching].string,
403408
b->items[j].string, diffopt);

0 commit comments

Comments
 (0)