git/list[1] front-page[2] threads[3] people[4] search[5] about
 

[PATCH] No color diff when redirecting to file

From
Sascha Peilicke <saschpe@gmx.de>
Date
Jan 23, 2011, 13:10 UTC
Message-ID
<201101231410.48528.saschpe@gmx.de>

Previously, when having color diffs enabled and redirecting 'git diff' into a file, one ends up with a messed up file containing termcap color stuff. This change disables color when diff output is redirected into a file.

Signed-off-by: Sascha Peilicke <saschpe@gmx.de>
---
 diff.c |    5 +++++
 1 files changed, 5 insertions(+), 0 deletions(-)
diff --git a/diff.c b/diff.c
index 5422c43..cd1ecb0 100644
--- a/diff.c
+++ b/diff.c
@@ -2854,6 +2854,11 @@ void diff_setup(struct diff_options *options)
 		DIFF_OPT_SET(options, COLOR_DIFF);
 	options->detect_rename = diff_detect_rename_default;
 
+	struct stat buf;
+	if (fstat(fileno(options->file), &buf) != -1 && S_ISREG(buf.st_mode)) {
+		DIFF_OPT_CLR(options, COLOR_DIFF);
+	}
+
 	if (diff_no_prefix) {
 		options->a_prefix = options->b_prefix = "";
 	} else if (!diff_mnemonic_prefix) {
-- 
1.7.3.4

-- 
Mit freundlichen Grüßen,
Sascha Peilicke
http://saschpe.wordpress.com
Next: Johannes Sixt
Message 1 of 7 in “No color diff when redirecting to file”
  1. No color diff when redirecting to fileSascha Peilicke, Jan 23, 2011
  2. Johannes SixtJan 23, 2011
  3. Matthieu MoyJan 23, 2011
  4. Junio C HamanoJan 23, 2011
  5. Sascha PeilickeJan 24, 2011
  6. Matthieu MoyJan 24, 2011
  7. Sascha PeilickeJan 24, 2011

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.