Skip to content

Commit 669e751

Browse files
stefanbellergitster
authored andcommitted
diff.c: rewrite emit_line_0 more understandably
emit_line_0 has no nested conditions, but puts out all its arguments (if set) in order. There is still a slight confusion with set and set_sign, but let's defer that to a later patch. 'first' used be output always no matter if it was 0, but that got lost at "diff: add an internal option to dual-color diffs of diffs", 2018-07-21), as there we broadened the meaning of 'first' to also signal an early return. The change in 'emit_line' makes sure that 'first' is never content, but always under our control, a sign or special character in the beginning of the line (or 0, in which case we ignore it). Signed-off-by: Stefan Beller <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent d070d39 commit 669e751

File tree

1 file changed

+41
-32
lines changed

1 file changed

+41
-32
lines changed

diff.c

Lines changed: 41 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -580,43 +580,52 @@ static void emit_line_0(struct diff_options *o,
580580
int first, const char *line, int len)
581581
{
582582
int has_trailing_newline, has_trailing_carriage_return;
583-
int nofirst;
584583
int reverse = !!set && !!set_sign;
584+
int needs_reset = 0;
585+
585586
FILE *file = o->file;
586587

587588
fputs(diff_line_prefix(o), file);
588589

589-
if (len == 0) {
590-
has_trailing_newline = (first == '\n');
591-
has_trailing_carriage_return = (!has_trailing_newline &&
592-
(first == '\r'));
593-
nofirst = has_trailing_newline || has_trailing_carriage_return;
594-
} else {
595-
has_trailing_newline = (len > 0 && line[len-1] == '\n');
596-
if (has_trailing_newline)
597-
len--;
598-
has_trailing_carriage_return = (len > 0 && line[len-1] == '\r');
599-
if (has_trailing_carriage_return)
600-
len--;
601-
nofirst = 0;
602-
}
603-
604-
if (len || !nofirst) {
605-
if (reverse && want_color(o->use_color))
606-
fputs(GIT_COLOR_REVERSE, file);
607-
if (set_sign || set)
608-
fputs(set_sign ? set_sign : set, file);
609-
if (first && !nofirst)
610-
fputc(first, file);
611-
if (len) {
612-
if (set_sign && set && set != set_sign)
613-
fputs(reset, file);
614-
if (set)
615-
fputs(set, file);
616-
fwrite(line, len, 1, file);
617-
}
618-
fputs(reset, file);
590+
has_trailing_newline = (len > 0 && line[len-1] == '\n');
591+
if (has_trailing_newline)
592+
len--;
593+
594+
has_trailing_carriage_return = (len > 0 && line[len-1] == '\r');
595+
if (has_trailing_carriage_return)
596+
len--;
597+
598+
if (!len && !first)
599+
goto end_of_line;
600+
601+
if (reverse && want_color(o->use_color)) {
602+
fputs(GIT_COLOR_REVERSE, file);
603+
needs_reset = 1;
604+
}
605+
606+
if (set_sign || set) {
607+
fputs(set_sign ? set_sign : set, file);
608+
needs_reset = 1;
619609
}
610+
611+
if (first)
612+
fputc(first, file);
613+
614+
if (!len)
615+
goto end_of_line;
616+
617+
if (set) {
618+
if (set_sign && set != set_sign)
619+
fputs(reset, file);
620+
fputs(set, file);
621+
needs_reset = 1;
622+
}
623+
fwrite(line, len, 1, file);
624+
needs_reset |= len > 0;
625+
626+
end_of_line:
627+
if (needs_reset)
628+
fputs(reset, file);
620629
if (has_trailing_carriage_return)
621630
fputc('\r', file);
622631
if (has_trailing_newline)
@@ -626,7 +635,7 @@ static void emit_line_0(struct diff_options *o,
626635
static void emit_line(struct diff_options *o, const char *set, const char *reset,
627636
const char *line, int len)
628637
{
629-
emit_line_0(o, set, NULL, reset, line[0], line+1, len-1);
638+
emit_line_0(o, set, NULL, reset, 0, line, len);
630639
}
631640

632641
enum diff_symbol {

0 commit comments

Comments
 (0)