Skip to content

Commit 250f799

Browse files
committed
diff.c: split emit_line() from the first char and the rest of the line
A new helper function emit_line_0() takes the first line of diff output (typically "-", " ", or "+") separately from the remainder of the line. No other functional changes. This change will make it easier to reuse the logic when emitting the rewrite diff, as we do not want to copy a line only to add "+"/"-"/" " immediately before its first character when we produce rewrite diff output. Signed-off-by: Junio C Hamano <[email protected]>
1 parent 6957eb9 commit 250f799

File tree

1 file changed

+26
-7
lines changed

1 file changed

+26
-7
lines changed

diff.c

Lines changed: 26 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -377,18 +377,31 @@ static void check_blank_at_eof(mmfile_t *mf1, mmfile_t *mf2,
377377
ecbdata->blank_at_eof_in_postimage = (at - l2) + 1;
378378
}
379379

380-
static void emit_line(FILE *file, const char *set, const char *reset, const char *line, int len)
380+
static void emit_line_0(FILE *file, const char *set, const char *reset,
381+
int first, const char *line, int len)
381382
{
382383
int has_trailing_newline, has_trailing_carriage_return;
384+
int nofirst;
383385

384-
has_trailing_newline = (len > 0 && line[len-1] == '\n');
385-
if (has_trailing_newline)
386-
len--;
387-
has_trailing_carriage_return = (len > 0 && line[len-1] == '\r');
388-
if (has_trailing_carriage_return)
389-
len--;
386+
if (len == 0) {
387+
has_trailing_newline = (first == '\n');
388+
has_trailing_carriage_return = (!has_trailing_newline &&
389+
(first == '\r'));
390+
nofirst = has_trailing_newline || has_trailing_carriage_return;
391+
} else {
392+
has_trailing_newline = (len > 0 && line[len-1] == '\n');
393+
if (has_trailing_newline)
394+
len--;
395+
has_trailing_carriage_return = (len > 0 && line[len-1] == '\r');
396+
if (has_trailing_carriage_return)
397+
len--;
398+
nofirst = 0;
399+
}
390400

391401
fputs(set, file);
402+
403+
if (!nofirst)
404+
fputc(first, file);
392405
fwrite(line, len, 1, file);
393406
fputs(reset, file);
394407
if (has_trailing_carriage_return)
@@ -397,6 +410,12 @@ static void emit_line(FILE *file, const char *set, const char *reset, const char
397410
fputc('\n', file);
398411
}
399412

413+
static void emit_line(FILE *file, const char *set, const char *reset,
414+
const char *line, int len)
415+
{
416+
emit_line_0(file, set, reset, line[0], line+1, len-1);
417+
}
418+
400419
static int new_blank_line_at_eof(struct emit_callback *ecbdata, const char *line, int len)
401420
{
402421
if (!((ecbdata->ws_rule & WS_BLANK_AT_EOF) &&

0 commit comments

Comments
 (0)