range-diff: use color for the commit pairs

Arguably the most important part of `git range-diff`'s output is the
list of commits in the two branches, together with their relationships.

For that reason, tbdiff introduced color-coding that is pretty
intuitive, especially for unchanged patches (all dim yellow, like the
first line in `git show`'s output) vs modified patches (old commit is
red, new commit is green). Let's imitate that color scheme.

Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
This commit is contained in:
Johannes Schindelin
2018-08-13 04:33:18 -07:00
committed by Junio C Hamano
parent 8884cf15fb
commit faa1df86dc

View File

@ -258,34 +258,53 @@ static void get_correspondences(struct string_list *a, struct string_list *b,
free(b2a); free(b2a);
} }
static void output_pair_header(struct strbuf *buf, static void output_pair_header(struct diff_options *diffopt,
struct strbuf *buf,
struct strbuf *dashes, struct strbuf *dashes,
struct patch_util *a_util, struct patch_util *a_util,
struct patch_util *b_util) struct patch_util *b_util)
{ {
struct object_id *oid = a_util ? &a_util->oid : &b_util->oid; struct object_id *oid = a_util ? &a_util->oid : &b_util->oid;
struct commit *commit; struct commit *commit;
char status;
const char *color_reset = diff_get_color_opt(diffopt, DIFF_RESET);
const char *color_old = diff_get_color_opt(diffopt, DIFF_FILE_OLD);
const char *color_new = diff_get_color_opt(diffopt, DIFF_FILE_NEW);
const char *color_commit = diff_get_color_opt(diffopt, DIFF_COMMIT);
const char *color;
if (!dashes->len) if (!dashes->len)
strbuf_addchars(dashes, '-', strbuf_addchars(dashes, '-',
strlen(find_unique_abbrev(oid, strlen(find_unique_abbrev(oid,
DEFAULT_ABBREV))); DEFAULT_ABBREV)));
if (!b_util) {
color = color_old;
status = '<';
} else if (!a_util) {
color = color_new;
status = '>';
} else if (strcmp(a_util->patch, b_util->patch)) {
color = color_commit;
status = '!';
} else {
color = color_commit;
status = '=';
}
strbuf_reset(buf); strbuf_reset(buf);
strbuf_addstr(buf, status == '!' ? color_old : color);
if (!a_util) if (!a_util)
strbuf_addf(buf, "-: %s ", dashes->buf); strbuf_addf(buf, "-: %s ", dashes->buf);
else else
strbuf_addf(buf, "%d: %s ", a_util->i + 1, strbuf_addf(buf, "%d: %s ", a_util->i + 1,
find_unique_abbrev(&a_util->oid, DEFAULT_ABBREV)); find_unique_abbrev(&a_util->oid, DEFAULT_ABBREV));
if (!a_util) if (status == '!')
strbuf_addch(buf, '>'); strbuf_addf(buf, "%s%s", color_reset, color);
else if (!b_util) strbuf_addch(buf, status);
strbuf_addch(buf, '<'); if (status == '!')
else if (strcmp(a_util->patch, b_util->patch)) strbuf_addf(buf, "%s%s", color_reset, color_new);
strbuf_addch(buf, '!');
else
strbuf_addch(buf, '=');
if (!b_util) if (!b_util)
strbuf_addf(buf, " -: %s", dashes->buf); strbuf_addf(buf, " -: %s", dashes->buf);
@ -295,10 +314,13 @@ static void output_pair_header(struct strbuf *buf,
commit = lookup_commit_reference(the_repository, oid); commit = lookup_commit_reference(the_repository, oid);
if (commit) { if (commit) {
if (status == '!')
strbuf_addf(buf, "%s%s", color_reset, color);
strbuf_addch(buf, ' '); strbuf_addch(buf, ' ');
pp_commit_easy(CMIT_FMT_ONELINE, commit, buf); pp_commit_easy(CMIT_FMT_ONELINE, commit, buf);
} }
strbuf_addch(buf, '\n'); strbuf_addf(buf, "%s\n", color_reset);
fwrite(buf->buf, buf->len, 1, stdout); fwrite(buf->buf, buf->len, 1, stdout);
} }
@ -356,21 +378,24 @@ static void output(struct string_list *a, struct string_list *b,
/* Show unmatched LHS commit whose predecessors were shown. */ /* Show unmatched LHS commit whose predecessors were shown. */
if (i < a->nr && a_util->matching < 0) { if (i < a->nr && a_util->matching < 0) {
output_pair_header(&buf, &dashes, a_util, NULL); output_pair_header(diffopt,
&buf, &dashes, a_util, NULL);
i++; i++;
continue; continue;
} }
/* Show unmatched RHS commits. */ /* Show unmatched RHS commits. */
while (j < b->nr && b_util->matching < 0) { while (j < b->nr && b_util->matching < 0) {
output_pair_header(&buf, &dashes, NULL, b_util); output_pair_header(diffopt,
&buf, &dashes, NULL, b_util);
b_util = ++j < b->nr ? b->items[j].util : NULL; b_util = ++j < b->nr ? b->items[j].util : NULL;
} }
/* Show matching LHS/RHS pair. */ /* Show matching LHS/RHS pair. */
if (j < b->nr) { if (j < b->nr) {
a_util = a->items[b_util->matching].util; a_util = a->items[b_util->matching].util;
output_pair_header(&buf, &dashes, a_util, b_util); output_pair_header(diffopt,
&buf, &dashes, a_util, b_util);
if (!(diffopt->output_format & DIFF_FORMAT_NO_OUTPUT)) if (!(diffopt->output_format & DIFF_FORMAT_NO_OUTPUT))
patch_diff(a->items[b_util->matching].string, patch_diff(a->items[b_util->matching].string,
b->items[j].string, diffopt); b->items[j].string, diffopt);