Skip to content

Commit 4389809

Browse files
committed
interdiff: add --color option
1 parent 984d19b commit 4389809

File tree

2 files changed

+52
-19
lines changed

2 files changed

+52
-19
lines changed

doc/patchutils.xml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@
8282
<arg>-w</arg>
8383
<arg>--ignore-all-space</arg>
8484
</group>
85+
<arg choice="opt">--color=<replaceable>WHEN</replaceable></arg>
8586
<group choice="opt">
8687
<arg>--interpolate</arg>
8788
<arg>--combine</arg>
@@ -238,6 +239,14 @@
238239
</listitem>
239240
</varlistentry>
240241

242+
<varlistentry>
243+
<term><option>--color=<replaceable>WHEN</replaceable></option></term>
244+
<listitem>
245+
<para>colorize the output; <replaceable>WHEN</replaceable>
246+
can be 'never', 'always', or 'auto' (the default)</para>
247+
</listitem>
248+
</varlistentry>
249+
241250
<varlistentry>
242251
<term><option>-z</option>,
243252
<option>--decompress</option></term>

src/interdiff.c

Lines changed: 43 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,8 @@ struct lines_info {
9090
};
9191

9292
static int human_readable = 1;
93-
static char diff_opts[4];
93+
static char *diff_opts[100];
94+
static int num_diff_opts = 0;
9495
static unsigned int max_context_real = 3, max_context = 3;
9596
static int context_specified = 0;
9697
static int ignore_components = 0;
@@ -708,7 +709,7 @@ output_patch1_only (FILE *p1, FILE *out, int not_reverted)
708709
int diff_is_empty = 1;
709710
unsigned int use_context = max_context;
710711

711-
if (diff_opts[0] == '\0' && !context_specified)
712+
if (!num_diff_opts && !context_specified)
712713
return do_output_patch1_only (p1, out, not_reverted);
713714

714715
/* We want to redo the diff using the supplied options. */
@@ -757,9 +758,9 @@ output_patch1_only (FILE *p1, FILE *out, int not_reverted)
757758
use_context = file_orig.min_context;
758759

759760
if (use_context == 3)
760-
sprintf(options, "-%su", diff_opts);
761+
strcpy (options, "-u");
761762
else
762-
sprintf (options, "-%sU%d", diff_opts, use_context);
763+
sprintf (options, "-U%d", use_context);
763764

764765
/* Write it out. */
765766
write_file (&file_orig, tmpp1fd);
@@ -770,7 +771,11 @@ output_patch1_only (FILE *p1, FILE *out, int not_reverted)
770771
clear_lines_info (&file_new);
771772

772773
fflush (NULL);
773-
in = xpipe (DIFF, &child, "r", (char **) (const char *[]) { DIFF, options, tmpp1, tmpp2, NULL });
774+
char *argv[2 + num_diff_opts + 2 + 1];
775+
memcpy (argv, (const char *[]) { DIFF, options }, 2 * sizeof (char *));
776+
memcpy (argv + 2, diff_opts, num_diff_opts * sizeof (char *));
777+
memcpy (argv + 2 + num_diff_opts, (char *[]) { tmpp1, tmpp2, NULL }, (2 + 1) * sizeof (char *));
778+
in = xpipe (DIFF, &child, "r", argv);
774779

775780
/* Eat the first line */
776781
for (;;) {
@@ -1043,9 +1048,9 @@ output_delta (FILE *p1, FILE *p2, FILE *out)
10431048
memcpy (tmpp2 + tmplen, tail2, sizeof (tail2));
10441049

10451050
if (max_context == 3)
1046-
sprintf(options, "-%su", diff_opts);
1051+
strcpy (options, "-u");
10471052
else
1048-
sprintf (options, "-%sU%d", diff_opts, max_context);
1053+
sprintf (options, "-U%d", max_context);
10491054

10501055
tmpp1fd = xmkstemp (tmpp1);
10511056
tmpp2fd = xmkstemp (tmpp2);
@@ -1102,7 +1107,11 @@ output_delta (FILE *p1, FILE *p2, FILE *out)
11021107

11031108
fflush (NULL);
11041109

1105-
in = xpipe(DIFF, &child, "r", (char **) (const char *[]) { DIFF, options, tmpp1, tmpp2, NULL });
1110+
char *argv[2 + num_diff_opts + 2 + 1];
1111+
memcpy (argv, (const char *[]) { DIFF, options }, 2 * sizeof (char *));
1112+
memcpy (argv + 2, diff_opts, num_diff_opts * sizeof (char *));
1113+
memcpy (argv + 2 + num_diff_opts, (char *[]) { tmpp1, tmpp2, NULL }, (2 + 1) * sizeof (char *));
1114+
in = xpipe (DIFF, &child, "r", argv);
11061115

11071116
/* Eat the first line */
11081117
for (;;) {
@@ -1500,14 +1509,23 @@ take_diff (const char *f1, const char *f2, char *headers[2],
15001509
FILE *in;
15011510

15021511
if (max_context == 3)
1503-
sprintf (options, "-%su", diff_opts);
1512+
strcpy (options, "-u");
15041513
else
1505-
sprintf (options, "-%sU%d", diff_opts, max_context);
1506-
1507-
if (debug)
1508-
printf ("+ " DIFF " %s %s %s\n", options, f1, f2);
1514+
sprintf (options, "-U%d", max_context);
1515+
1516+
char *argv[2 + num_diff_opts + 2 + 1];
1517+
memcpy (argv, (const char *[]) { DIFF, options }, 2 * sizeof (char *));
1518+
memcpy (argv + 2, diff_opts, num_diff_opts * sizeof (char *));
1519+
memcpy (argv + 2 + num_diff_opts, (const char *[]) { f1, f2, NULL }, (2 + 1) * sizeof (char *));
1520+
if (debug) {
1521+
fputs ("+", stdout);
1522+
for (int i = 0; argv[i]; i++) {
1523+
printf (" %s", argv[i]);
1524+
}
1525+
puts ("");
1526+
}
15091527

1510-
in = xpipe (DIFF, &child, "r", (char **) (const char *[]) { DIFF, options, f1, f2, NULL });
1528+
in = xpipe (DIFF, &child, "r", argv);
15111529

15121530
/* Eat the first line */
15131531
for (;;) {
@@ -1996,6 +2014,9 @@ syntax (int err)
19962014
" ignore changes in the amount of whitespace\n"
19972015
" -B, --ignore-blank-lines\n"
19982016
" ignore changes whose lines are all blank\n"
2017+
" --color[=WHEN]\n"
2018+
" colorize the output; WHEN can be 'never', 'always',\n"
2019+
" or 'auto' (the default)\n"
19992020
" -p N, --strip-match=N\n"
20002021
" pathname components to ignore\n"
20012022
" -q, --quiet\n"
@@ -2061,11 +2082,9 @@ int
20612082
main (int argc, char *argv[])
20622083
{
20632084
FILE *p1, *p2;
2064-
int num_diff_opts = 0;
20652085
int ret;
20662086

20672087
get_mode_from_name (argv[0]);
2068-
diff_opts[0] = '\0';
20692088
for (;;) {
20702089
static struct option long_options[] = {
20712090
{"help", 0, 0, 1000 + 'H'},
@@ -2083,6 +2102,7 @@ main (int argc, char *argv[])
20832102
{"ignore-space-change", 0, 0, 'b'},
20842103
{"ignore-case", 0, 0, 'i'},
20852104
{"ignore-all-space", 0, 0, 'w'},
2105+
{"color", 2, 0, 1000 + 'c'},
20862106
{"decompress", 0, 0, 'z'},
20872107
{"quiet", 0, 0, 'q'},
20882108
{0, 0, 0, 0}
@@ -2127,9 +2147,13 @@ main (int argc, char *argv[])
21272147
case 'b':
21282148
case 'i':
21292149
case 'w':
2130-
if (!memchr (diff_opts, c, num_diff_opts)) {
2131-
diff_opts[num_diff_opts++] = c;
2132-
diff_opts[num_diff_opts] = '\0';
2150+
asprintf (diff_opts + num_diff_opts++, "-%c", c);
2151+
break;
2152+
case 1000 + 'c':
2153+
if (optarg) {
2154+
asprintf (diff_opts + num_diff_opts++, "--color=%s", optarg);
2155+
} else {
2156+
diff_opts[num_diff_opts++] = "--color";
21332157
}
21342158
break;
21352159
case 1000 + 'I':

0 commit comments

Comments
 (0)