Merge branch 'sb/range-diff-colors'
The color output support for recently introduced "range-diff" command got tweaked a bit. * sb/range-diff-colors: range-diff: indent special lines as context range-diff: make use of different output indicators diff.c: add --output-indicator-{new, old, context} diff.c: rewrite emit_line_0 more understandably diff.c: omit check for line prefix in emit_line_0 diff: use emit_line_0 once per line diff.c: add set_sign to emit_line_0 diff.c: reorder arguments for emit_line_ws_markup diff.c: simplify caller of emit_line_0 t3206: add color test for range-diff --dual-color test_decode_color: understand FAINT and ITALIC
This commit is contained in:
106
diff.c
106
diff.c
@ -624,42 +624,54 @@ static void check_blank_at_eof(mmfile_t *mf1, mmfile_t *mf2,
|
||||
}
|
||||
|
||||
static void emit_line_0(struct diff_options *o,
|
||||
const char *set, unsigned reverse, const char *reset,
|
||||
const char *set_sign, const char *set, unsigned reverse, const char *reset,
|
||||
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);
|
||||
|
||||
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)
|
||||
fputs(diff_line_prefix(o), file);
|
||||
else if (!len)
|
||||
return;
|
||||
fputc(first, 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)
|
||||
goto end_of_line;
|
||||
|
||||
if (len || !nofirst) {
|
||||
if (reverse && want_color(o->use_color))
|
||||
fputs(GIT_COLOR_REVERSE, file);
|
||||
if (set) {
|
||||
if (set_sign && set != set_sign)
|
||||
fputs(reset, file);
|
||||
fputs(set, file);
|
||||
if (first && !nofirst)
|
||||
fputc(first, file);
|
||||
fwrite(line, len, 1, file);
|
||||
fputs(reset, 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)
|
||||
@ -669,7 +681,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, 0, reset, line[0], line+1, len-1);
|
||||
emit_line_0(o, set, NULL, 0, reset, 0, line, len);
|
||||
}
|
||||
|
||||
enum diff_symbol {
|
||||
@ -1187,9 +1199,9 @@ static void dim_moved_lines(struct diff_options *o)
|
||||
}
|
||||
|
||||
static void emit_line_ws_markup(struct diff_options *o,
|
||||
const char *set, const char *reset,
|
||||
const char *line, int len,
|
||||
const char *set_sign, char sign,
|
||||
const char *set_sign, const char *set,
|
||||
const char *reset,
|
||||
char sign, const char *line, int len,
|
||||
unsigned ws_rule, int blank_at_eof)
|
||||
{
|
||||
const char *ws = NULL;
|
||||
@ -1201,18 +1213,15 @@ static void emit_line_ws_markup(struct diff_options *o,
|
||||
}
|
||||
|
||||
if (!ws && !set_sign)
|
||||
emit_line_0(o, set, 0, reset, sign, line, len);
|
||||
emit_line_0(o, set, NULL, 0, reset, sign, line, len);
|
||||
else if (!ws) {
|
||||
/* Emit just the prefix, then the rest. */
|
||||
emit_line_0(o, set_sign ? set_sign : set, !!set_sign, reset,
|
||||
sign, "", 0);
|
||||
emit_line_0(o, set, 0, reset, 0, line, len);
|
||||
emit_line_0(o, set_sign, set, !!set_sign, reset, sign, line, len);
|
||||
} else if (blank_at_eof)
|
||||
/* Blank line at EOF - paint '+' as well */
|
||||
emit_line_0(o, ws, 0, reset, sign, line, len);
|
||||
emit_line_0(o, ws, NULL, 0, reset, sign, line, len);
|
||||
else {
|
||||
/* Emit just the prefix, then the rest. */
|
||||
emit_line_0(o, set_sign ? set_sign : set, !!set_sign, reset,
|
||||
emit_line_0(o, set_sign ? set_sign : set, NULL, !!set_sign, reset,
|
||||
sign, "", 0);
|
||||
ws_check_emit(line, len, ws_rule,
|
||||
o->file, set, reset, ws);
|
||||
@ -1236,7 +1245,7 @@ static void emit_diff_symbol_from_struct(struct diff_options *o,
|
||||
context = diff_get_color_opt(o, DIFF_CONTEXT);
|
||||
reset = diff_get_color_opt(o, DIFF_RESET);
|
||||
putc('\n', o->file);
|
||||
emit_line_0(o, context, 0, reset, '\\',
|
||||
emit_line_0(o, context, NULL, 0, reset, '\\',
|
||||
nneof, strlen(nneof));
|
||||
break;
|
||||
case DIFF_SYMBOL_SUBMODULE_HEADER:
|
||||
@ -1274,7 +1283,9 @@ static void emit_diff_symbol_from_struct(struct diff_options *o,
|
||||
else if (c == '-')
|
||||
set = diff_get_color_opt(o, DIFF_FILE_OLD);
|
||||
}
|
||||
emit_line_ws_markup(o, set, reset, line, len, set_sign, ' ',
|
||||
emit_line_ws_markup(o, set_sign, set, reset,
|
||||
o->output_indicators[OUTPUT_INDICATOR_CONTEXT],
|
||||
line, len,
|
||||
flags & (DIFF_SYMBOL_CONTENT_WS_MASK), 0);
|
||||
break;
|
||||
case DIFF_SYMBOL_PLUS:
|
||||
@ -1317,7 +1328,9 @@ static void emit_diff_symbol_from_struct(struct diff_options *o,
|
||||
set = diff_get_color_opt(o, DIFF_CONTEXT_BOLD);
|
||||
flags &= ~DIFF_SYMBOL_CONTENT_WS_MASK;
|
||||
}
|
||||
emit_line_ws_markup(o, set, reset, line, len, set_sign, '+',
|
||||
emit_line_ws_markup(o, set_sign, set, reset,
|
||||
o->output_indicators[OUTPUT_INDICATOR_NEW],
|
||||
line, len,
|
||||
flags & DIFF_SYMBOL_CONTENT_WS_MASK,
|
||||
flags & DIFF_SYMBOL_CONTENT_BLANK_LINE_EOF);
|
||||
break;
|
||||
@ -1360,7 +1373,9 @@ static void emit_diff_symbol_from_struct(struct diff_options *o,
|
||||
else
|
||||
set = diff_get_color_opt(o, DIFF_CONTEXT_DIM);
|
||||
}
|
||||
emit_line_ws_markup(o, set, reset, line, len, set_sign, '-',
|
||||
emit_line_ws_markup(o, set_sign, set, reset,
|
||||
o->output_indicators[OUTPUT_INDICATOR_OLD],
|
||||
line, len,
|
||||
flags & DIFF_SYMBOL_CONTENT_WS_MASK, 0);
|
||||
break;
|
||||
case DIFF_SYMBOL_WORDS_PORCELAIN:
|
||||
@ -4375,6 +4390,9 @@ void diff_setup(struct diff_options *options)
|
||||
|
||||
options->file = stdout;
|
||||
|
||||
options->output_indicators[OUTPUT_INDICATOR_NEW] = '+';
|
||||
options->output_indicators[OUTPUT_INDICATOR_OLD] = '-';
|
||||
options->output_indicators[OUTPUT_INDICATOR_CONTEXT] = ' ';
|
||||
options->abbrev = DEFAULT_ABBREV;
|
||||
options->line_termination = '\n';
|
||||
options->break_opt = -1;
|
||||
@ -4852,6 +4870,12 @@ int diff_opt_parse(struct diff_options *options,
|
||||
options->output_format |= DIFF_FORMAT_DIFFSTAT;
|
||||
} else if (!strcmp(arg, "--no-compact-summary"))
|
||||
options->flags.stat_with_summary = 0;
|
||||
else if (skip_prefix(arg, "--output-indicator-new=", &arg))
|
||||
options->output_indicators[OUTPUT_INDICATOR_NEW] = arg[0];
|
||||
else if (skip_prefix(arg, "--output-indicator-old=", &arg))
|
||||
options->output_indicators[OUTPUT_INDICATOR_OLD] = arg[0];
|
||||
else if (skip_prefix(arg, "--output-indicator-context=", &arg))
|
||||
options->output_indicators[OUTPUT_INDICATOR_CONTEXT] = arg[0];
|
||||
|
||||
/* renames options */
|
||||
else if (starts_with(arg, "-B") ||
|
||||
|
Reference in New Issue
Block a user