Skip to content

Commit faa1df8

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

File tree

1 file changed

+38
-13
lines changed

1 file changed

+38
-13
lines changed

range-diff.c

Lines changed: 38 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -258,34 +258,53 @@ static void get_correspondences(struct string_list *a, struct string_list *b,
258258
free(b2a);
259259
}
260260

261-
static void output_pair_header(struct strbuf *buf,
261+
static void output_pair_header(struct diff_options *diffopt,
262+
struct strbuf *buf,
262263
struct strbuf *dashes,
263264
struct patch_util *a_util,
264265
struct patch_util *b_util)
265266
{
266267
struct object_id *oid = a_util ? &a_util->oid : &b_util->oid;
267268
struct commit *commit;
269+
char status;
270+
const char *color_reset = diff_get_color_opt(diffopt, DIFF_RESET);
271+
const char *color_old = diff_get_color_opt(diffopt, DIFF_FILE_OLD);
272+
const char *color_new = diff_get_color_opt(diffopt, DIFF_FILE_NEW);
273+
const char *color_commit = diff_get_color_opt(diffopt, DIFF_COMMIT);
274+
const char *color;
268275

269276
if (!dashes->len)
270277
strbuf_addchars(dashes, '-',
271278
strlen(find_unique_abbrev(oid,
272279
DEFAULT_ABBREV)));
273280

281+
if (!b_util) {
282+
color = color_old;
283+
status = '<';
284+
} else if (!a_util) {
285+
color = color_new;
286+
status = '>';
287+
} else if (strcmp(a_util->patch, b_util->patch)) {
288+
color = color_commit;
289+
status = '!';
290+
} else {
291+
color = color_commit;
292+
status = '=';
293+
}
294+
274295
strbuf_reset(buf);
296+
strbuf_addstr(buf, status == '!' ? color_old : color);
275297
if (!a_util)
276298
strbuf_addf(buf, "-: %s ", dashes->buf);
277299
else
278300
strbuf_addf(buf, "%d: %s ", a_util->i + 1,
279301
find_unique_abbrev(&a_util->oid, DEFAULT_ABBREV));
280302

281-
if (!a_util)
282-
strbuf_addch(buf, '>');
283-
else if (!b_util)
284-
strbuf_addch(buf, '<');
285-
else if (strcmp(a_util->patch, b_util->patch))
286-
strbuf_addch(buf, '!');
287-
else
288-
strbuf_addch(buf, '=');
303+
if (status == '!')
304+
strbuf_addf(buf, "%s%s", color_reset, color);
305+
strbuf_addch(buf, status);
306+
if (status == '!')
307+
strbuf_addf(buf, "%s%s", color_reset, color_new);
289308

290309
if (!b_util)
291310
strbuf_addf(buf, " -: %s", dashes->buf);
@@ -295,10 +314,13 @@ static void output_pair_header(struct strbuf *buf,
295314

296315
commit = lookup_commit_reference(the_repository, oid);
297316
if (commit) {
317+
if (status == '!')
318+
strbuf_addf(buf, "%s%s", color_reset, color);
319+
298320
strbuf_addch(buf, ' ');
299321
pp_commit_easy(CMIT_FMT_ONELINE, commit, buf);
300322
}
301-
strbuf_addch(buf, '\n');
323+
strbuf_addf(buf, "%s\n", color_reset);
302324

303325
fwrite(buf->buf, buf->len, 1, stdout);
304326
}
@@ -356,21 +378,24 @@ static void output(struct string_list *a, struct string_list *b,
356378

357379
/* Show unmatched LHS commit whose predecessors were shown. */
358380
if (i < a->nr && a_util->matching < 0) {
359-
output_pair_header(&buf, &dashes, a_util, NULL);
381+
output_pair_header(diffopt,
382+
&buf, &dashes, a_util, NULL);
360383
i++;
361384
continue;
362385
}
363386

364387
/* Show unmatched RHS commits. */
365388
while (j < b->nr && b_util->matching < 0) {
366-
output_pair_header(&buf, &dashes, NULL, b_util);
389+
output_pair_header(diffopt,
390+
&buf, &dashes, NULL, b_util);
367391
b_util = ++j < b->nr ? b->items[j].util : NULL;
368392
}
369393

370394
/* Show matching LHS/RHS pair. */
371395
if (j < b->nr) {
372396
a_util = a->items[b_util->matching].util;
373-
output_pair_header(&buf, &dashes, a_util, b_util);
397+
output_pair_header(diffopt,
398+
&buf, &dashes, a_util, b_util);
374399
if (!(diffopt->output_format & DIFF_FORMAT_NO_OUTPUT))
375400
patch_diff(a->items[b_util->matching].string,
376401
b->items[j].string, diffopt);

0 commit comments

Comments
 (0)