diff.c: rewrite emit_line_0 more understandably
authorStefan Beller <sbeller@google.com>
Tue, 14 Aug 2018 01:41:22 +0000 (18:41 -0700)
committerJunio C Hamano <gitster@pobox.com>
Tue, 14 Aug 2018 21:03:05 +0000 (14:03 -0700)
Rewrite emit_line_0 to have fewer (nested) conditions.

The change in 'emit_line' makes sure that 'first' is never user data,
but always under our control, a sign or special character in the
beginning of the line (or 0, in which case we ignore it).
So from now on, let's pass only a diff marker or 0 as the 'first'
character of the line.

Signed-off-by: Stefan Beller <sbeller@google.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
diff.c
diff --git a/diff.c b/diff.c
index 7a23adf254d24a1b25c9d78eaac54f0721998a62..c5c7739ce3452a8de97d6b6baf882cea3cbb9ef5 100644 (file)
--- a/diff.c
+++ b/diff.c
@@ -626,43 +626,50 @@ static void emit_line_0(struct diff_options *o,
                        int first, const char *line, int len)
 {
        int has_trailing_newline, has_trailing_carriage_return;
-       int nofirst;
+       int needs_reset = 0; /* at the end of the line */
        FILE *file = o->file;
 
        fputs(diff_line_prefix(o), file);
 
-       if (len == 0) {
-               has_trailing_newline = (first == '\n');
-               has_trailing_carriage_return = (!has_trailing_newline &&
-                                               (first == '\r'));
-               nofirst = has_trailing_newline || has_trailing_carriage_return;
-       } else {
-               has_trailing_newline = (len > 0 && line[len-1] == '\n');
-               if (has_trailing_newline)
-                       len--;
-               has_trailing_carriage_return = (len > 0 && line[len-1] == '\r');
-               if (has_trailing_carriage_return)
-                       len--;
-               nofirst = 0;
-       }
-
-       if (len || !nofirst) {
-               if (reverse && want_color(o->use_color))
-                       fputs(GIT_COLOR_REVERSE, file);
-               if (set_sign)
-                       fputs(set_sign, file);
-               if (first && !nofirst)
-                       fputc(first, file);
-               if (len) {
-                       if (set && set != set_sign) {
-                               if (set_sign)
-                                       fputs(reset, file);
-                               fputs(set, file);
-                       }
-                       fwrite(line, len, 1, file);
-               }
-               fputs(reset, file);
+       has_trailing_newline = (len > 0 && line[len-1] == '\n');
+       if (has_trailing_newline)
+               len--;
+
+       has_trailing_carriage_return = (len > 0 && line[len-1] == '\r');
+       if (has_trailing_carriage_return)
+               len--;
+
+       if (!len && !first)
+               goto end_of_line;
+
+       if (reverse && want_color(o->use_color)) {
+               fputs(GIT_COLOR_REVERSE, file);
+               needs_reset = 1;
+       }
+
+       if (set_sign) {
+               fputs(set_sign, file);
+               needs_reset = 1;
        }
+
+       if (first)
+               fputc(first, file);
+
+       if (!len)
+               goto end_of_line;
+
+       if (set) {
+               if (set_sign && set != set_sign)
+                       fputs(reset, file);
+               fputs(set, file);
+               needs_reset = 1;
+       }
+       fwrite(line, len, 1, file);
+       needs_reset = 1; /* 'line' may contain color codes. */
+
+end_of_line:
+       if (needs_reset)
+               fputs(reset, file);
        if (has_trailing_carriage_return)
                fputc('\r', file);
        if (has_trailing_newline)
@@ -672,7 +679,7 @@ static void emit_line_0(struct diff_options *o,
 static void emit_line(struct diff_options *o, const char *set, const char *reset,
                      const char *line, int len)
 {
-       emit_line_0(o, set, NULL, 0, reset, line[0], line+1, len-1);
+       emit_line_0(o, set, NULL, 0, reset, 0, line, len);
 }
 
 enum diff_symbol {