Skip to content

Commit 1443630

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 d05b54c commit 1443630

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, DEFAULT_ABBREV));
300301

301302
if (status == '!')
@@ -305,9 +306,9 @@ static void output_pair_header(struct diff_options *diffopt, struct strbuf *buf,
305306
strbuf_addf(buf, "%s%s", color_reset, color_new);
306307

307308
if (!b_util)
308-
strbuf_addf(buf, " -: %s", dashes);
309+
strbuf_addf(buf, " %*s: %s", patch_no_width, "-", dashes);
309310
else
310-
strbuf_addf(buf, " %d: %s", b_util->i + 1,
311+
strbuf_addf(buf, " %*d: %s", patch_no_width, b_util->i + 1,
311312
find_unique_abbrev(&b_util->oid, DEFAULT_ABBREV));
312313

313314
commit = lookup_commit_reference(oid);
@@ -360,6 +361,7 @@ static void output(struct string_list *a, struct string_list *b,
360361
struct diff_options *diffopt)
361362
{
362363
struct strbuf buf = STRBUF_INIT;
364+
int patch_no_width = decimal_width(1 + (a->nr > b->nr ? a->nr : b->nr));
363365
int i = 0, j = 0;
364366

365367
/*
@@ -381,21 +383,24 @@ static void output(struct string_list *a, struct string_list *b,
381383

382384
/* Show unmatched LHS commit whose predecessors were shown. */
383385
if (i < a->nr && a_util->matching < 0) {
384-
output_pair_header(diffopt, &buf, a_util, NULL);
386+
output_pair_header(diffopt, patch_no_width, &buf,
387+
a_util, NULL);
385388
i++;
386389
continue;
387390
}
388391

389392
/* Show unmatched RHS commits. */
390393
while (j < b->nr && b_util->matching < 0) {
391-
output_pair_header(diffopt, &buf, NULL, b_util);
394+
output_pair_header(diffopt, patch_no_width, &buf,
395+
NULL, b_util);
392396
b_util = ++j < b->nr ? b->items[j].util : NULL;
393397
}
394398

395399
/* Show matching LHS/RHS pair. */
396400
if (j < b->nr) {
397401
a_util = a->items[b_util->matching].util;
398-
output_pair_header(diffopt, &buf, a_util, b_util);
402+
output_pair_header(diffopt, patch_no_width, &buf,
403+
a_util, b_util);
399404
if (!(diffopt->output_format & DIFF_FORMAT_NO_OUTPUT))
400405
patch_diff(a->items[b_util->matching].string,
401406
b->items[j].string, diffopt);

0 commit comments

Comments
 (0)