Skip to content

Commit afc676f

Browse files
dschogitster
authored andcommitted
diff: do not color output when --color=auto and --output=<file> is given
"git diff --output=<file> --color=auto" used to show the ANSI color sequence in the resulting file when the standard output is connected to a terminal, because --color=auto check always checks the standard output, not the actual file that receives the output. We could correct this by using freopen(3) to redirect the standard output to the specified file, which is in like with how format-patch used to match the world order, but following the same reasoning as the earlier "format-patch: explicitly switch off color when writing to files", let's be more strict by bypassing the "auto" check when the --output=<file> option is in use. Strictly speaking, this is a backwards-incompatible change, but it is highly unlikely that any user would want to see ANSI color sequences in a file. The reason this was not caught earlier is most likely that either --output=<file> is not used, or only when stdout is redirected anyway. Users can still give --color=always if they want a colored diff in the resulting file. Signed-off-by: Johannes Schindelin <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent c149693 commit afc676f

File tree

1 file changed

+2
-0
lines changed

1 file changed

+2
-0
lines changed

diff.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3977,6 +3977,8 @@ int diff_opt_parse(struct diff_options *options,
39773977
if (!options->file)
39783978
die_errno("Could not open '%s'", path);
39793979
options->close_file = 1;
3980+
if (options->use_color != GIT_COLOR_ALWAYS)
3981+
options->use_color = GIT_COLOR_NEVER;
39803982
return argcount;
39813983
} else
39823984
return 0;

0 commit comments

Comments
 (0)