Skip to content

Commit

Permalink
range-diff: left-pad patch numbers
Browse files Browse the repository at this point in the history
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]>
  • Loading branch information
dscho committed Jun 1, 2018
1 parent 40ee939 commit 7b16bd3
Showing 1 changed file with 13 additions and 8 deletions.
21 changes: 13 additions & 8 deletions range-diff.c
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,8 @@ static void get_correspondences(struct string_list *a, struct string_list *b,
free(b2a);
}

static void output_pair_header(struct diff_options *diffopt, struct strbuf *buf,
static void output_pair_header(struct diff_options *diffopt, int patch_no_width,
struct strbuf *buf,
struct patch_util *a_util,
struct patch_util *b_util)
{
Expand Down Expand Up @@ -293,9 +294,9 @@ static void output_pair_header(struct diff_options *diffopt, struct strbuf *buf,
strbuf_reset(buf);
strbuf_addstr(buf, status == '!' ? color_old : color);
if (!a_util)
strbuf_addf(buf, "-: %s ", dashes);
strbuf_addf(buf, "%*s: %s ", patch_no_width, "-", dashes);
else
strbuf_addf(buf, "%d: %s ", a_util->i + 1,
strbuf_addf(buf, "%*d: %s ", patch_no_width, a_util->i + 1,
find_unique_abbrev(a_util->oid.hash,
DEFAULT_ABBREV));

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

if (!b_util)
strbuf_addf(buf, " -: %s", dashes);
strbuf_addf(buf, " %*s: %s", patch_no_width, "-", dashes);
else
strbuf_addf(buf, " %d: %s", b_util->i + 1,
strbuf_addf(buf, " %*d: %s", patch_no_width, b_util->i + 1,
find_unique_abbrev(b_util->oid.hash,
DEFAULT_ABBREV));

Expand Down Expand Up @@ -362,6 +363,7 @@ static void output(struct string_list *a, struct string_list *b,
struct diff_options *diffopt)
{
struct strbuf buf = STRBUF_INIT;
int patch_no_width = decimal_width(1 + (a->nr > b->nr ? a->nr : b->nr));
int i = 0, j = 0;

/*
Expand All @@ -383,21 +385,24 @@ static void output(struct string_list *a, struct string_list *b,

/* Show unmatched LHS commit whose predecessors were shown. */
if (i < a->nr && a_util->matching < 0) {
output_pair_header(diffopt, &buf, a_util, NULL);
output_pair_header(diffopt, patch_no_width, &buf,
a_util, NULL);
i++;
continue;
}

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

/* Show matching LHS/RHS pair. */
if (j < b->nr) {
a_util = a->items[b_util->matching].util;
output_pair_header(diffopt, &buf, a_util, b_util);
output_pair_header(diffopt, patch_no_width, &buf,
a_util, b_util);
if (!(diffopt->output_format & DIFF_FORMAT_NO_OUTPUT))
patch_diff(a->items[b_util->matching].string,
b->items[j].string, diffopt);
Expand Down

0 comments on commit 7b16bd3

Please sign in to comment.