Skip to content

Commit ab6e271

Browse files
committed
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]>
1 parent 4c71ec8 commit ab6e271

File tree

1 file changed

+34
-13
lines changed

1 file changed

+34
-13
lines changed

range-diff.c

Lines changed: 34 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -254,13 +254,19 @@ static void get_correspondences(struct string_list *a, struct string_list *b,
254254
free(b2a);
255255
}
256256

257-
static void output_pair_header(struct strbuf *buf,
257+
static void output_pair_header(struct diff_options *diffopt, struct strbuf *buf,
258258
struct patch_util *a_util,
259259
struct patch_util *b_util)
260260
{
261261
static char *dashes;
262262
struct object_id *oid = a_util ? &a_util->oid : &b_util->oid;
263263
struct commit *commit;
264+
char status;
265+
const char *color_reset = diff_get_color_opt(diffopt, DIFF_RESET);
266+
const char *color_old = diff_get_color_opt(diffopt, DIFF_FILE_OLD);
267+
const char *color_new = diff_get_color_opt(diffopt, DIFF_FILE_NEW);
268+
const char *color_commit = diff_get_color_opt(diffopt, DIFF_COMMIT);
269+
const char *color;
264270

265271
if (!dashes) {
266272
char *p;
@@ -270,22 +276,34 @@ static void output_pair_header(struct strbuf *buf,
270276
*p = '-';
271277
}
272278

279+
if (!b_util) {
280+
color = color_old;
281+
status = '<';
282+
} else if (!a_util) {
283+
color = color_new;
284+
status = '>';
285+
} else if (strcmp(a_util->patch, b_util->patch)) {
286+
color = color_commit;
287+
status = '!';
288+
} else {
289+
color = color_commit;
290+
status = '=';
291+
}
292+
273293
strbuf_reset(buf);
294+
strbuf_addstr(buf, status == '!' ? color_old : color);
274295
if (!a_util)
275296
strbuf_addf(buf, "-: %s ", dashes);
276297
else
277298
strbuf_addf(buf, "%d: %s ", a_util->i + 1,
278299
find_unique_abbrev(a_util->oid.hash,
279300
DEFAULT_ABBREV));
280301

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, '=');
302+
if (status == '!')
303+
strbuf_addf(buf, "%s%s", color_reset, color);
304+
strbuf_addch(buf, status);
305+
if (status == '!')
306+
strbuf_addf(buf, "%s%s", color_reset, color_new);
289307

290308
if (!b_util)
291309
strbuf_addf(buf, " -: %s", dashes);
@@ -299,12 +317,15 @@ static void output_pair_header(struct strbuf *buf,
299317
const char *commit_buffer = get_commit_buffer(commit, NULL);
300318
const char *subject;
301319

320+
if (status == '!')
321+
strbuf_addf(buf, "%s%s", color_reset, color);
322+
302323
find_commit_subject(commit_buffer, &subject);
303324
strbuf_addch(buf, ' ');
304325
format_subject(buf, subject, " ");
305326
unuse_commit_buffer(commit, commit_buffer);
306327
}
307-
strbuf_addch(buf, '\n');
328+
strbuf_addf(buf, "%s\n", color_reset);
308329

309330
fwrite(buf->buf, buf->len, 1, stdout);
310331
}
@@ -362,21 +383,21 @@ static void output(struct string_list *a, struct string_list *b,
362383

363384
/* Show unmatched LHS commit whose predecessors were shown. */
364385
if (i < a->nr && a_util->matching < 0) {
365-
output_pair_header(&buf, a_util, NULL);
386+
output_pair_header(diffopt, &buf, a_util, NULL);
366387
i++;
367388
continue;
368389
}
369390

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

376397
/* Show matching LHS/RHS pair. */
377398
if (j < b->nr) {
378399
a_util = a->items[b_util->matching].util;
379-
output_pair_header(&buf, a_util, b_util);
400+
output_pair_header(diffopt, &buf, a_util, b_util);
380401
if (!(diffopt->output_format & DIFF_FORMAT_NO_OUTPUT))
381402
patch_diff(a->items[b_util->matching].string,
382403
b->items[j].string, diffopt);

0 commit comments

Comments
 (0)