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>
Johannes Schindelin committed
Aug 13, 2018 at 04:33 UTC
faa1df86dc033ce1e6136ed89ad55df1bd479099
1 file changed
+38
-13
range-diff.c
+38
-13
@@ -258,34 +258,53 @@ static void get_correspondences(struct string_list *a, struct string_list *b,
258
free(b2a);
259
}
260
261
-static void output_pair_header(struct strbuf *buf,
261
+static void output_pair_header(struct diff_options *diffopt,
262
+ struct strbuf *buf,
263
struct strbuf *dashes,
264
struct patch_util *a_util,
265
struct patch_util *b_util)
266
{
267
struct object_id *oid = a_util ? &a_util->oid : &b_util->oid;
268
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;
275
276
if (!dashes->len)
277
strbuf_addchars(dashes, '-',
278
strlen(find_unique_abbrev(oid,
279
DEFAULT_ABBREV)));
280
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
+
295
strbuf_reset(buf);
296
+ strbuf_addstr(buf, status == '!' ? color_old : color);
297
if (!a_util)
298
strbuf_addf(buf, "-: %s ", dashes->buf);
299
else
300
strbuf_addf(buf, "%d: %s ", a_util->i + 1,
301
find_unique_abbrev(&a_util->oid, DEFAULT_ABBREV));
302
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);
308
309
if (!b_util)
310
strbuf_addf(buf, " -: %s", dashes->buf);
@@ -295,10 +314,13 @@ static void output_pair_header(struct strbuf *buf,
314
315
commit = lookup_commit_reference(the_repository, oid);
316
if (commit) {
317
+ if (status == '!')
318
+ strbuf_addf(buf, "%s%s", color_reset, color);
319
+
320
strbuf_addch(buf, ' ');
321
pp_commit_easy(CMIT_FMT_ONELINE, commit, buf);
322
}
301
- strbuf_addch(buf, '\n');
323
+ strbuf_addf(buf, "%s\n", color_reset);
324
325
fwrite(buf->buf, buf->len, 1, stdout);
326
}
@@ -356,21 +378,24 @@ static void output(struct string_list *a, struct string_list *b,
378
379
/* Show unmatched LHS commit whose predecessors were shown. */
380
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);
383
i++;
384
continue;
385
}
386
387
/* Show unmatched RHS commits. */
388
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);
391
b_util = ++j < b->nr ? b->items[j].util : NULL;
392
}
393
394
/* Show matching LHS/RHS pair. */
395
if (j < b->nr) {
396
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);
399
if (!(diffopt->output_format & DIFF_FORMAT_NO_OUTPUT))
400
patch_diff(a->items[b_util->matching].string,
401
b->items[j].string, diffopt);