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 <struggleyb.nku@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Bo Yang committed
May 29, 2010 at 23:32 UTC
2efcc977646320123c0d461664d25c4c93aaa9ee
1 file changed
+27
-5
diff.c
+27
-5
@@ -370,6 +370,9 @@ static void emit_hunk_header(struct emit_callback *ecbdata,
370
const char *reset = diff_get_color(ecbdata->color_diff, DIFF_RESET);
371
static const char atat[2] = { '@', '@' };
372
const char *cp, *ep;
373
+ struct strbuf msgbuf = STRBUF_INIT;
374
+ int org_len = len;
375
+ int i = 1;
376
377
/*
378
* As a hunk header must begin with "@@ -<old>, +<new> @@",
@@ -384,17 +387,36 @@ static void emit_hunk_header(struct emit_callback *ecbdata,
387
ep += 2; /* skip over @@ */
388
389
/* 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--;
400
401
/* blank before the func header */
402
for (cp = ep; ep - line < len; ep++)
403
if (*ep != ' ' && *ep != '\t')
404
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
+ }
416
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);
420
}
421
422
static struct diff_tempfile *claim_diff_tempfile(void) {