combine-diff: use color
Using the same mechanism as the regular diffs, color combined diff output. Signed-off-by: Junio C Hamano <junkio@cox.net>
Junio C Hamano committed
Aug 10, 2006 at 00:30 UTC
567a03d14cb624b0edc76be9cffda11edac2bea3
1 file changed
+34
-16
combine-diff.c
+34
-16
@@ -497,11 +497,17 @@ static void show_parent_lno(struct sline *sline, unsigned long l0, unsigned long
497
printf(" -%lu,%lu", l0, l1-l0);
498
}
499
500
-static void dump_sline(struct sline *sline, unsigned long cnt, int num_parent)
500
+static void dump_sline(struct sline *sline, unsigned long cnt, int num_parent,
501
+ int use_color)
502
{
503
unsigned long mark = (1UL<<num_parent);
504
int i;
505
unsigned long lno = 0;
506
+ const char *c_frag = diff_get_color(use_color, DIFF_FRAGINFO);
507
+ const char *c_new = diff_get_color(use_color, DIFF_FILE_NEW);
508
+ const char *c_old = diff_get_color(use_color, DIFF_FILE_OLD);
509
+ const char *c_plain = diff_get_color(use_color, DIFF_PLAIN);
510
+ const char *c_reset = diff_get_color(use_color, DIFF_RESET);
511
512
if (!cnt)
513
return; /* result deleted */
@@ -522,12 +528,13 @@ static void dump_sline(struct sline *sline, unsigned long cnt, int num_parent)
528
rlines = hunk_end - lno;
529
if (cnt < hunk_end)
530
rlines--; /* pointing at the last delete hunk */
531
+ fputs(c_frag, stdout);
532
for (i = 0; i <= num_parent; i++) putchar(combine_marker);
533
for (i = 0; i < num_parent; i++)
534
show_parent_lno(sline, lno, hunk_end, i);
535
printf(" +%lu,%lu ", lno+1, rlines);
536
for (i = 0; i <= num_parent; i++) putchar(combine_marker);
530
- putchar('\n');
537
+ printf("%s\n", c_reset);
538
while (lno < hunk_end) {
539
struct lline *ll;
540
int j;
@@ -535,18 +542,23 @@ static void dump_sline(struct sline *sline, unsigned long cnt, int num_parent)
542
sl = &sline[lno++];
543
ll = sl->lost_head;
544
while (ll) {
545
+ fputs(c_old, stdout);
546
for (j = 0; j < num_parent; j++) {
547
if (ll->parent_map & (1UL<<j))
548
putchar('-');
549
else
550
putchar(' ');
551
}
544
- puts(ll->line);
552
+ printf("%s%s\n", ll->line, c_reset);
553
ll = ll->next;
554
}
555
if (cnt < lno)
556
break;
557
p_mask = 1;
558
+ if (!(sl->flag & (mark-1)))
559
+ fputs(c_plain, stdout);
560
+ else
561
+ fputs(c_new, stdout);
562
for (j = 0; j < num_parent; j++) {
563
if (p_mask & sl->flag)
564
putchar('+');
@@ -554,7 +566,7 @@ static void dump_sline(struct sline *sline, unsigned long cnt, int num_parent)
566
putchar(' ');
567
p_mask <<= 1;
568
}
557
- printf("%.*s\n", sl->len, sl->bol);
569
+ printf("%.*s%s\n", sl->len, sl->bol, c_reset);
570
}
571
}
572
}
@@ -586,14 +598,15 @@ static void reuse_combine_diff(struct sline *sline, unsigned long cnt,
598
sline->p_lno[i] = sline->p_lno[j];
599
}
600
589
-static void dump_quoted_path(const char *prefix, const char *path)
601
+static void dump_quoted_path(const char *prefix, const char *path,
602
+ const char *c_meta, const char *c_reset)
603
{
591
- fputs(prefix, stdout);
604
+ printf("%s%s", c_meta, prefix);
605
if (quote_c_style(path, NULL, NULL, 0))
606
quote_c_style(path, NULL, stdout, 0);
607
else
608
printf("%s", path);
596
- putchar('\n');
609
+ printf("%s\n", c_reset);
610
}
611
612
static int show_patch_diff(struct combine_diff_path *elem, int num_parent,
@@ -699,18 +712,22 @@ static int show_patch_diff(struct combine_diff_path *elem, int num_parent,
712
713
if (show_hunks || mode_differs || working_tree_file) {
714
const char *abb;
715
+ int use_color = opt->color_diff;
716
+ const char *c_meta = diff_get_color(use_color, DIFF_METAINFO);
717
+ const char *c_reset = diff_get_color(use_color, DIFF_RESET);
718
719
if (rev->loginfo)
720
show_log(rev, opt->msg_sep);
705
- dump_quoted_path(dense ? "diff --cc " : "diff --combined ", elem->path);
706
- printf("index ");
721
+ dump_quoted_path(dense ? "diff --cc " : "diff --combined ",
722
+ elem->path, c_meta, c_reset);
723
+ printf("%sindex ", c_meta);
724
for (i = 0; i < num_parent; i++) {
725
abb = find_unique_abbrev(elem->parent[i].sha1,
726
abbrev);
727
printf("%s%s", i ? "," : "", abb);
728
}
729
abb = find_unique_abbrev(elem->sha1, abbrev);
713
- printf("..%s\n", abb);
730
+ printf("..%s%s\n", abb, c_reset);
731
732
if (mode_differs) {
733
int added = !!elem->mode;
@@ -719,10 +736,11 @@ static int show_patch_diff(struct combine_diff_path *elem, int num_parent,
736
DIFF_STATUS_ADDED)
737
added = 0;
738
if (added)
722
- printf("new file mode %06o", elem->mode);
739
+ printf("%snew file mode %06o",
740
+ c_meta, elem->mode);
741
else {
742
if (!elem->mode)
725
- printf("deleted file ");
743
+ printf("%sdeleted file ", c_meta);
744
printf("mode ");
745
for (i = 0; i < num_parent; i++) {
746
printf("%s%06o", i ? "," : "",
@@ -731,11 +749,11 @@ static int show_patch_diff(struct combine_diff_path *elem, int num_parent,
749
if (elem->mode)
750
printf("..%06o", elem->mode);
751
}
734
- putchar('\n');
752
+ printf("%s\n", c_reset);
753
}
736
- dump_quoted_path("--- a/", elem->path);
737
- dump_quoted_path("+++ b/", elem->path);
738
- dump_sline(sline, cnt, num_parent);
754
+ dump_quoted_path("--- a/", elem->path, c_meta, c_reset);
755
+ dump_quoted_path("+++ b/", elem->path, c_meta, c_reset);
756
+ dump_sline(sline, cnt, num_parent, opt->color_diff);
757
}
758
free(result);
759