{"thread":{"id":"13988","subject":"[PATCH] diff: reset color before printing newline","startedAt":"2008-06-16T22:00:02Z","lastAt":"2008-06-17T00:59:44Z","messageCount":4,"participants":["SZEDER Gábor","Junio C Hamano"],"isPatch":true,"patchVersion":1,"patchTotal":null},"messages":[{"id":"80079","messageId":"1213653602-31307-1-git-send-email-szeder@ira.uka.de","threadId":"13988","inReplyTo":null,"subject":"[PATCH] diff: reset color before printing newline","fromName":"SZEDER Gábor","fromEmail":"szeder@ira.uka.de","sentAt":"2008-06-16T22:00:02Z","receivedAt":"2008-06-16T22:00:02Z","isPatch":true,"sender":{"key":"szeder.dev@gmail.com","avatar":"https://avatars.githubusercontent.com/u/116324?v=4"},"body":"It worked that way since commit 50f575fc (Tweak diff colors,\n2006-06-22), but commit c1795bb0 (Unify whitespace checking, 2007-12-13)\nchanged it.  This patch restores the old behaviour.\n\nBesides Linus' arguments in the log message of 50f575fc, resetting color\nbefore printing newline is also important to keep 'git add --patch'\nhappy.  If the last line(s) of a file are removed, then that hunk will\nend with a colored line.  However, if the newline comes before the color\nreset, then the diff output will have an additional line at the end\ncontaining only the reset sequence.  This causes trouble in\ngit-add--interactive.perl's parse_diff function, because @colored will\nhave one more element than @diff, and that last element will contain the\ncolor reset.  The elements of these arrays will then be copied to @hunk,\nbut only as many as the number of elements in @diff.  As a result the\nlast color reset is lost and all subsequent terminal output will be\nprinted in color.\n\nSigned-off-by: SZEDER Gábor <szeder@ira.uka.de>\n---\n\nI'm not sure about putting a newline unconditionally at the end of the\nline, but this was the status quo before c1795bb0.\n\n diff.c |    4 ++++\n 1 files changed, 4 insertions(+), 0 deletions(-)\n\ndiff --git a/diff.c b/diff.c\nindex 62fdc54..f23657b 100644\n--- a/diff.c\n+++ b/diff.c\n@@ -514,9 +514,13 @@ const char *diff_get_color(int diff_use_color, enum color_diff ix)\n \n static void emit_line(FILE *file, const char *set, const char *reset, const char *line, int len)\n {\n+\tif (len > 0 && line[len-1] == '\\n')\n+\t\tlen--;\n+\n \tfputs(set, file);\n \tfwrite(line, len, 1, file);\n \tfputs(reset, file);\n+\tfputc('\\n', file);\n }\n \n static void emit_add_line(const char *reset, struct emit_callback *ecbdata, const char *line, int len)\n-- \n1.5.6.rc2.55.g9b8c\n"},{"id":"80086","messageId":"7vprqh7yn3.fsf@gitster.siamese.dyndns.org","threadId":"13988","inReplyTo":"1213653602-31307-1-git-send-email-szeder@ira.uka.de","subject":"Re: [PATCH] diff: reset color before printing newline","fromName":"Junio C Hamano","fromEmail":"gitster@pobox.com","sentAt":"2008-06-16T23:23:12Z","receivedAt":"2008-06-16T23:23:12Z","isPatch":true,"sender":{"key":"gitster@pobox.com","avatar":"https://avatars.githubusercontent.com/u/54884?v=4"},"body":"Thanks; makes sense.\n"},{"id":"80090","messageId":"20080617002205.GD7003@neumann","threadId":"13988","inReplyTo":"1213653602-31307-1-git-send-email-szeder@ira.uka.de","subject":"Re: [PATCH] diff: reset color before printing newline","fromName":"SZEDER Gábor","fromEmail":"szeder@ira.uka.de","sentAt":"2008-06-17T00:22:05Z","receivedAt":"2008-06-17T00:22:05Z","isPatch":true,"sender":{"key":"szeder.dev@gmail.com","avatar":"https://avatars.githubusercontent.com/u/116324?v=4"},"body":"Junio,\n\nOn Tue, Jun 17, 2008 at 12:00:02AM +0200, SZEDER Gábor wrote:\n> I'm not sure about putting a newline unconditionally at the end of the\n> line, but this was the status quo before c1795bb0.\nwe _must_ not print a newline when there was none, as it breaks\n\"normal\" colored diffs very badly.\n\nSince c1795bb0, for whatever reason, checkdiff_consume() first calls\nemit_line() to print only the first '+' character of the line, without\ntrailing newline, and then calls check_and_emit_line() to print the\nrest of the line.  Now if we restore the status quo prior to c1795bb0\nregarding unconditional newlines in emit_line(), then there is always\na newline between the the '+' and the rest of the line, which is bad,\nbad, bad.\n\nSo this should be squashed to the previous patch.\n\n---\n diff.c |    9 +++++++--\n 1 files changed, 7 insertions(+), 2 deletions(-)\n\ndiff --git a/diff.c b/diff.c\nindex f23657b..722f577 100644\n--- a/diff.c\n+++ b/diff.c\n@@ -514,13 +514,18 @@ const char *diff_get_color(int diff_use_color, enum color_diff ix)\n \n static void emit_line(FILE *file, const char *set, const char *reset, const char *line, int len)\n {\n-\tif (len > 0 && line[len-1] == '\\n')\n+\tint trailing_newline = 0;\n+\n+\tif (len > 0 && line[len-1] == '\\n') {\n \t\tlen--;\n+\t\ttrailing_newline = 1;\n+\t}\n \n \tfputs(set, file);\n \tfwrite(line, len, 1, file);\n \tfputs(reset, file);\n-\tfputc('\\n', file);\n+\tif (trailing_newline)\n+\t\tfputc('\\n', file);\n }\n \n static void emit_add_line(const char *reset, struct emit_callback *ecbdata, const char *line, int len)\n"},{"id":"80093","messageId":"7vhcbs98qn.fsf@gitster.siamese.dyndns.org","threadId":"13988","inReplyTo":"20080617002205.GD7003@neumann","subject":"Re: [PATCH] diff: reset color before printing newline","fromName":"Junio C Hamano","fromEmail":"gitster@pobox.com","sentAt":"2008-06-17T00:59:44Z","receivedAt":"2008-06-17T00:59:44Z","isPatch":true,"sender":{"key":"gitster@pobox.com","avatar":"https://avatars.githubusercontent.com/u/54884?v=4"},"body":"SZEDER Gábor <szeder@ira.uka.de> writes:\n\n> So this should be squashed to the previous patch.\n\nYeah, I noticed the breakage after I pushed the results out and made an\nequivalent quickfix and re-pushed out.\n"}]}