Skip to content

Commit 06ff64a

Browse files
szedergitster
authored andcommitted
diff: reset color before printing newline
It worked that way since commit 50f575f (Tweak diff colors, 2006-06-22), but commit c1795bb (Unify whitespace checking, 2007-12-13) changed it. This patch restores the old behaviour. Besides Linus' arguments in the log message of 50f575f, resetting color before printing newline is also important to keep 'git add --patch' happy. If the last line(s) of a file are removed, then that hunk will end with a colored line. However, if the newline comes before the color reset, then the diff output will have an additional line at the end containing only the reset sequence. This causes trouble in git-add--interactive.perl's parse_diff function, because @colored will have one more element than @diff, and that last element will contain the color reset. The elements of these arrays will then be copied to @hunk, but only as many as the number of elements in @diff. As a result the last color reset is lost and all subsequent terminal output will be printed in color. Signed-off-by: SZEDER Gábor <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 2feaf4e commit 06ff64a

File tree

1 file changed

+4
-0
lines changed

1 file changed

+4
-0
lines changed

diff.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -514,9 +514,13 @@ const char *diff_get_color(int diff_use_color, enum color_diff ix)
514514

515515
static void emit_line(FILE *file, const char *set, const char *reset, const char *line, int len)
516516
{
517+
if (len > 0 && line[len-1] == '\n')
518+
len--;
519+
517520
fputs(set, file);
518521
fwrite(line, len, 1, file);
519522
fputs(reset, file);
523+
fputc('\n', file);
520524
}
521525

522526
static void emit_add_line(const char *reset, struct emit_callback *ecbdata, const char *line, int len)

0 commit comments

Comments
 (0)