Skip to content

Commit d1f87a2

Browse files
dschogitster
authored andcommitted
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]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 7190a67 commit d1f87a2

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(the_repository, oid);
@@ -357,6 +358,7 @@ static void output(struct string_list *a, struct string_list *b,
357358
struct diff_options *diffopt)
358359
{
359360
struct strbuf buf = STRBUF_INIT, dashes = STRBUF_INIT;
361+
int patch_no_width = decimal_width(1 + (a->nr > b->nr ? a->nr : b->nr));
360362
int i = 0, j = 0;
361363

362364
/*
@@ -378,23 +380,23 @@ static void output(struct string_list *a, struct string_list *b,
378380

379381
/* Show unmatched LHS commit whose predecessors were shown. */
380382
if (i < a->nr && a_util->matching < 0) {
381-
output_pair_header(diffopt,
383+
output_pair_header(diffopt, patch_no_width,
382384
&buf, &dashes, a_util, NULL);
383385
i++;
384386
continue;
385387
}
386388

387389
/* Show unmatched RHS commits. */
388390
while (j < b->nr && b_util->matching < 0) {
389-
output_pair_header(diffopt,
391+
output_pair_header(diffopt, patch_no_width,
390392
&buf, &dashes, NULL, b_util);
391393
b_util = ++j < b->nr ? b->items[j].util : NULL;
392394
}
393395

394396
/* Show matching LHS/RHS pair. */
395397
if (j < b->nr) {
396398
a_util = a->items[b_util->matching].util;
397-
output_pair_header(diffopt,
399+
output_pair_header(diffopt, patch_no_width,
398400
&buf, &dashes, a_util, b_util);
399401
if (!(diffopt->output_format & DIFF_FORMAT_NO_OUTPUT))
400402
patch_diff(a->items[b_util->matching].string,

0 commit comments

Comments
 (0)