Skip to content

Commit 2efcc97

Browse files
byanggitster
authored andcommitted
Emit a whole line in one go
Since the graph prefix will be printed when calling emit_line, so the functions should be used to emit a complete line out once a time. No one should call emit_line to just output some strings instead of a complete line. Use a strbuf to compose the whole line, and then call emit_line to output it once. Signed-off-by: Bo Yang <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 7be5761 commit 2efcc97

File tree

1 file changed

+27
-5
lines changed

1 file changed

+27
-5
lines changed

diff.c

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -370,6 +370,9 @@ static void emit_hunk_header(struct emit_callback *ecbdata,
370370
const char *reset = diff_get_color(ecbdata->color_diff, DIFF_RESET);
371371
static const char atat[2] = { '@', '@' };
372372
const char *cp, *ep;
373+
struct strbuf msgbuf = STRBUF_INIT;
374+
int org_len = len;
375+
int i = 1;
373376

374377
/*
375378
* As a hunk header must begin with "@@ -<old>, +<new> @@",
@@ -384,17 +387,36 @@ static void emit_hunk_header(struct emit_callback *ecbdata,
384387
ep += 2; /* skip over @@ */
385388

386389
/* The hunk header in fraginfo color */
387-
emit_line(ecbdata->opt, frag, reset, line, ep - line);
390+
strbuf_add(&msgbuf, frag, strlen(frag));
391+
strbuf_add(&msgbuf, line, ep - line);
392+
strbuf_add(&msgbuf, reset, strlen(reset));
393+
394+
/*
395+
* trailing "\r\n"
396+
*/
397+
for ( ; i < 3; i++)
398+
if (line[len - i] == '\r' || line[len - i] == '\n')
399+
len--;
388400

389401
/* blank before the func header */
390402
for (cp = ep; ep - line < len; ep++)
391403
if (*ep != ' ' && *ep != '\t')
392404
break;
393-
if (ep != cp)
394-
emit_line(ecbdata->opt, plain, reset, cp, ep - cp);
405+
if (ep != cp) {
406+
strbuf_add(&msgbuf, plain, strlen(plain));
407+
strbuf_add(&msgbuf, cp, ep - cp);
408+
strbuf_add(&msgbuf, reset, strlen(reset));
409+
}
410+
411+
if (ep < line + len) {
412+
strbuf_add(&msgbuf, func, strlen(func));
413+
strbuf_add(&msgbuf, ep, line + len - ep);
414+
strbuf_add(&msgbuf, reset, strlen(reset));
415+
}
395416

396-
if (ep < line + len)
397-
emit_line(ecbdata->opt, func, reset, ep, line + len - ep);
417+
strbuf_add(&msgbuf, line + len, org_len - len);
418+
emit_line(ecbdata->opt, "", "", msgbuf.buf, msgbuf.len);
419+
strbuf_release(&msgbuf);
398420
}
399421

400422
static struct diff_tempfile *claim_diff_tempfile(void) {

0 commit comments

Comments
 (0)