Rework pretty_print_commit to use strbufs instead of custom buffers.
Also remove the "len" parameter, as: (1) it was used as a max boundary, and every caller used ~0u (2) we check for final NUL no matter what, so it doesn't help for speed. As a result most of the pp_* function takes 3 arguments less, and we need a lot less local variables, this makes the code way more readable, and easier to extend if needed. This patch also fixes some spacing and cosmetic issues. This patch also fixes (as a side effect) a memory leak intoruced in builtin-archive.c at commit df4a394f (fmt was xmalloc'ed and not free'd) Signed-off-by: Pierre Habouzit <madcoder@debian.org> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Pierre Habouzit committed
Sep 10, 2007 at 12:35 UTC
674d1727305211f7ade4ade70440220f74f55162
8 files changed
+191
-299
builtin-archive.c
+16
-23
@@ -84,14 +84,16 @@ static int run_remote_archiver(const char *remote, int argc,
84
static void *format_subst(const struct commit *commit, const char *format,
85
unsigned long *sizep)
86
{
87
- unsigned long len = *sizep, result_len = 0;
87
+ unsigned long len = *sizep;
88
const char *a = format;
89
- char *result = NULL;
89
+ struct strbuf result;
90
+ struct strbuf fmt;
91
+
92
+ strbuf_init(&result, 0);
93
+ strbuf_init(&fmt, 0);
94
95
for (;;) {
96
const char *b, *c;
93
- char *fmt, *formatted = NULL;
94
- unsigned long a_len, fmt_len, formatted_len, allocated = 0;
97
98
b = memmem(a, len, "$Format:", 8);
99
if (!b || a + len < b + 9)
@@ -100,32 +102,23 @@ static void *format_subst(const struct commit *commit, const char *format,
102
if (!c)
103
break;
104
103
- a_len = b - a;
104
- fmt_len = c - b - 8;
105
- fmt = xmalloc(fmt_len + 1);
106
- memcpy(fmt, b + 8, fmt_len);
107
- fmt[fmt_len] = '\0';
108
-
109
- formatted_len = format_commit_message(commit, fmt, &formatted,
110
- &allocated);
111
- free(fmt);
112
- result = xrealloc(result, result_len + a_len + formatted_len);
113
- memcpy(result + result_len, a, a_len);
114
- memcpy(result + result_len + a_len, formatted, formatted_len);
115
- result_len += a_len + formatted_len;
105
+ strbuf_reset(&fmt);
106
+ strbuf_add(&fmt, b + 8, c - b - 8);
107
+
108
+ strbuf_add(&result, a, b - a);
109
+ format_commit_message(commit, fmt.buf, &result);
110
len -= c + 1 - a;
111
a = c + 1;
112
}
113
120
- if (result && len) {
121
- result = xrealloc(result, result_len + len);
122
- memcpy(result + result_len, a, len);
123
- result_len += len;
114
+ if (result.len && len) {
115
+ strbuf_add(&result, a, len);
116
}
117
126
- *sizep = result_len;
118
+ *sizep = result.len;
119
128
- return result;
120
+ strbuf_release(&fmt);
121
+ return strbuf_detach(&result);
122
}
123
124
static void *convert_to_archive(const char *path,
builtin-branch.c
+7
-8
@@ -268,23 +268,22 @@ static void print_ref_item(struct ref_item *item, int maxwidth, int verbose,
268
}
269
270
if (verbose) {
271
- char *subject = NULL;
272
- unsigned long subject_len = 0;
271
+ struct strbuf subject;
272
const char *sub = " **** invalid ref ****";
273
274
+ strbuf_init(&subject, 0);
275
+
276
commit = lookup_commit(item->sha1);
277
if (commit && !parse_commit(commit)) {
277
- pretty_print_commit(CMIT_FMT_ONELINE, commit, ~0,
278
- &subject, &subject_len, 0,
279
- NULL, NULL, 0);
280
- sub = subject;
278
+ pretty_print_commit(CMIT_FMT_ONELINE, commit,
279
+ &subject, 0, NULL, NULL, 0);
280
+ sub = subject.buf;
281
}
282
printf("%c %s%-*s%s %s %s\n", c, branch_get_color(color),
283
maxwidth, item->name,
284
branch_get_color(COLOR_BRANCH_RESET),
285
find_unique_abbrev(item->sha1, abbrev), sub);
286
- if (subject)
287
- free(subject);
286
+ strbuf_release(&subject);
287
} else {
288
printf("%c %s%s%s\n", c, branch_get_color(color), item->name,
289
branch_get_color(COLOR_BRANCH_RESET));
builtin-log.c
+6
-6
@@ -763,13 +763,13 @@ int cmd_cherry(int argc, const char **argv, const char *prefix)
763
sign = '-';
764
765
if (verbose) {
766
- char *buf = NULL;
767
- unsigned long buflen = 0;
768
- pretty_print_commit(CMIT_FMT_ONELINE, commit, ~0,
769
- &buf, &buflen, 0, NULL, NULL, 0);
766
+ struct strbuf buf;
767
+ strbuf_init(&buf, 0);
768
+ pretty_print_commit(CMIT_FMT_ONELINE, commit,
769
+ &buf, 0, NULL, NULL, 0);
770
printf("%c %s %s\n", sign,
771
- sha1_to_hex(commit->object.sha1), buf);
772
- free(buf);
771
+ sha1_to_hex(commit->object.sha1), buf.buf);
772
+ strbuf_release(&buf);
773
}
774
else {
775
printf("%c %s\n", sign,
builtin-rev-list.c
+6
-7
@@ -80,13 +80,12 @@ static void show_commit(struct commit *commit)
80
putchar('\n');
81
82
if (revs.verbose_header) {
83
- char *buf = NULL;
84
- unsigned long buflen = 0;
85
- pretty_print_commit(revs.commit_format, commit, ~0,
86
- &buf, &buflen,
87
- revs.abbrev, NULL, NULL, revs.date_mode);
88
- printf("%s%c", buf, hdr_termination);
89
- free(buf);
83
+ struct strbuf buf;
84
+ strbuf_init(&buf, 0);
85
+ pretty_print_commit(revs.commit_format, commit,
86
+ &buf, revs.abbrev, NULL, NULL, revs.date_mode);
87
+ printf("%s%c", buf.buf, hdr_termination);
88
+ strbuf_release(&buf);
89
}
90
maybe_flush_or_die(stdout, "stdout");
91
if (commit->parents) {
builtin-show-branch.c
+6
-7
@@ -259,16 +259,15 @@ static void join_revs(struct commit_list **list_p,
259
260
static void show_one_commit(struct commit *commit, int no_name)
261
{
262
- char *pretty = NULL;
262
+ struct strbuf pretty;
263
const char *pretty_str = "(unavailable)";
264
- unsigned long pretty_len = 0;
264
struct commit_name *name = commit->util;
265
266
+ strbuf_init(&pretty, 0);
267
if (commit->object.parsed) {
268
- pretty_print_commit(CMIT_FMT_ONELINE, commit, ~0,
269
- &pretty, &pretty_len,
270
- 0, NULL, NULL, 0);
271
- pretty_str = pretty;
268
+ pretty_print_commit(CMIT_FMT_ONELINE, commit,
269
+ &pretty, 0, NULL, NULL, 0);
270
+ pretty_str = pretty.buf;
271
}
272
if (!prefixcmp(pretty_str, "[PATCH] "))
273
pretty_str += 8;
@@ -289,7 +288,7 @@ static void show_one_commit(struct commit *commit, int no_name)
288
find_unique_abbrev(commit->object.sha1, 7));
289
}
290
puts(pretty_str);
292
- free(pretty);
291
+ strbuf_release(&pretty);
292
}
293
294
static char *ref_name[MAX_REVS + 1];
commit.c
+124
-209
@@ -458,11 +458,11 @@ void clear_commit_marks(struct commit *commit, unsigned int mark)
458
/*
459
* Generic support for pretty-printing the header
460
*/
461
-static int get_one_line(const char *msg, unsigned long len)
461
+static int get_one_line(const char *msg)
462
{
463
int ret = 0;
464
465
- while (len--) {
465
+ for (;;) {
466
char c = *msg++;
467
if (!c)
468
break;
@@ -485,31 +485,24 @@ static int is_rfc2047_special(char ch)
485
return (non_ascii(ch) || (ch == '=') || (ch == '?') || (ch == '_'));
486
}
487
488
-static int add_rfc2047(char *buf, const char *line, int len,
488
+static void add_rfc2047(struct strbuf *sb, const char *line, int len,
489
const char *encoding)
490
{
491
- char *bp = buf;
492
- int i, needquote;
493
- char q_encoding[128];
494
- const char *q_encoding_fmt = "=?%s?q?";
491
+ int i, last;
492
496
- for (i = needquote = 0; !needquote && i < len; i++) {
493
+ for (i = 0; i < len; i++) {
494
int ch = line[i];
495
if (non_ascii(ch))
499
- needquote++;
500
- if ((i + 1 < len) &&
501
- (ch == '=' && line[i+1] == '?'))
502
- needquote++;
496
+ goto needquote;
497
+ if ((i + 1 < len) && (ch == '=' && line[i+1] == '?'))
498
+ goto needquote;
499
}
504
- if (!needquote)
505
- return sprintf(buf, "%.*s", len, line);
506
-
507
- i = snprintf(q_encoding, sizeof(q_encoding), q_encoding_fmt, encoding);
508
- if (sizeof(q_encoding) < i)
509
- die("Insanely long encoding name %s", encoding);
510
- memcpy(bp, q_encoding, i);
511
- bp += i;
512
- for (i = 0; i < len; i++) {
500
+ strbuf_add(sb, line, len);
501
+ return;
502
+
503
+needquote:
504
+ strbuf_addf(sb, "=?%s?q?", encoding);
505
+ for (i = last = 0; i < len; i++) {
506
unsigned ch = line[i] & 0xFF;
507
/*
508
* We encode ' ' using '=20' even though rfc2047
@@ -518,15 +511,13 @@ static int add_rfc2047(char *buf, const char *line, int len,
511
* leave the underscore in place.
512
*/
513
if (is_rfc2047_special(ch) || ch == ' ') {
521
- sprintf(bp, "=%02X", ch);
522
- bp += 3;
514
+ strbuf_add(sb, line + last, i - last);
515
+ strbuf_addf(sb, "=%02X", ch);
516
+ last = i + 1;
517
}
524
- else
525
- *bp++ = ch;
518
}
527
- memcpy(bp, "?=", 2);
528
- bp += 2;
529
- return bp - buf;
519
+ strbuf_add(sb, line + last, len - last);
520
+ strbuf_addstr(sb, "?=");
521
}
522
523
static unsigned long bound_rfc2047(unsigned long len, const char *encoding)
@@ -537,21 +528,21 @@ static unsigned long bound_rfc2047(unsigned long len, const char *encoding)
528
return len * 3 + elen + 100;
529
}
530
540
-static int add_user_info(const char *what, enum cmit_fmt fmt, char *buf,
531
+static void add_user_info(const char *what, enum cmit_fmt fmt, struct strbuf *sb,
532
const char *line, enum date_mode dmode,
533
const char *encoding)
534
{
535
char *date;
536
int namelen;
537
unsigned long time;
547
- int tz, ret;
538
+ int tz;
539
const char *filler = " ";
540
541
if (fmt == CMIT_FMT_ONELINE)
551
- return 0;
542
+ return;
543
date = strchr(line, '>');
544
if (!date)
554
- return 0;
545
+ return;
546
namelen = ++date - line;
547
time = strtoul(date, &date, 10);
548
tz = strtol(date, NULL, 10);
@@ -560,42 +551,35 @@ static int add_user_info(const char *what, enum cmit_fmt fmt, char *buf,
551
char *name_tail = strchr(line, '<');
552
int display_name_length;
553
if (!name_tail)
563
- return 0;
554
+ return;
555
while (line < name_tail && isspace(name_tail[-1]))
556
name_tail--;
557
display_name_length = name_tail - line;
558
filler = "";
568
- strcpy(buf, "From: ");
569
- ret = strlen(buf);
570
- ret += add_rfc2047(buf + ret, line, display_name_length,
571
- encoding);
572
- memcpy(buf + ret, name_tail, namelen - display_name_length);
573
- ret += namelen - display_name_length;
574
- buf[ret++] = '\n';
559
+ strbuf_addstr(sb, "From: ");
560
+ add_rfc2047(sb, line, display_name_length, encoding);
561
+ strbuf_add(sb, name_tail, namelen - display_name_length);
562
+ strbuf_addch(sb, '\n');
563
}
564
else {
577
- ret = sprintf(buf, "%s: %.*s%.*s\n", what,
565
+ strbuf_addf(sb, "%s: %.*s%.*s\n", what,
566
(fmt == CMIT_FMT_FULLER) ? 4 : 0,
567
filler, namelen, line);
568
}
569
switch (fmt) {
570
case CMIT_FMT_MEDIUM:
583
- ret += sprintf(buf + ret, "Date: %s\n",
584
- show_date(time, tz, dmode));
571
+ strbuf_addf(sb, "Date: %s\n", show_date(time, tz, dmode));
572
break;
573
case CMIT_FMT_EMAIL:
587
- ret += sprintf(buf + ret, "Date: %s\n",
588
- show_date(time, tz, DATE_RFC2822));
574
+ strbuf_addf(sb, "Date: %s\n", show_date(time, tz, DATE_RFC2822));
575
break;
576
case CMIT_FMT_FULLER:
591
- ret += sprintf(buf + ret, "%sDate: %s\n", what,
592
- show_date(time, tz, dmode));
577
+ strbuf_addf(sb, "%sDate: %s\n", what, show_date(time, tz, dmode));
578
break;
579
default:
580
/* notin' */
581
break;
582
}
598
- return ret;
583
}
584
585
static int is_empty_line(const char *line, int *len_p)
@@ -607,16 +591,16 @@ static int is_empty_line(const char *line, int *len_p)
591
return !len;
592
}
593
610
-static int add_merge_info(enum cmit_fmt fmt, char *buf, const struct commit *commit, int abbrev)
594
+static void add_merge_info(enum cmit_fmt fmt, struct strbuf *sb,
595
+ const struct commit *commit, int abbrev)
596
{
597
struct commit_list *parent = commit->parents;
613
- int offset;
598
599
if ((fmt == CMIT_FMT_ONELINE) || (fmt == CMIT_FMT_EMAIL) ||
600
!parent || !parent->next)
617
- return 0;
601
+ return;
602
619
- offset = sprintf(buf, "Merge:");
603
+ strbuf_addstr(sb, "Merge:");
604
605
while (parent) {
606
struct commit *p = parent->item;
@@ -629,10 +613,9 @@ static int add_merge_info(enum cmit_fmt fmt, char *buf, const struct commit *com
613
dots = (abbrev && strlen(hex) != 40) ? "..." : "";
614
parent = parent->next;
615
632
- offset += sprintf(buf + offset, " %s%s", hex, dots);
616
+ strbuf_addf(sb, " %s%s", hex, dots);
617
}
634
- buf[offset++] = '\n';
635
- return offset;
618
+ strbuf_addch(sb, '\n');
619
}
620
621
static char *get_header(const struct commit *commit, const char *key)
@@ -787,8 +770,8 @@ static void fill_person(struct interp *table, const char *msg, int len)
770
interp_set_entry(table, 6, show_date(date, tz, DATE_ISO8601));
771
}
772
790
-long format_commit_message(const struct commit *commit, const void *format,
791
- char **buf_p, unsigned long *space_p)
773
+void format_commit_message(const struct commit *commit,
774
+ const void *format, struct strbuf *sb)
775
{
776
struct interp table[] = {
777
{ "%H" }, /* commit hash */
@@ -841,6 +824,7 @@ long format_commit_message(const struct commit *commit, const void *format,
824
};
825
struct commit_list *p;
826
char parents[1024];
827
+ unsigned long len;
828
int i;
829
enum { HEADER, SUBJECT, BODY } state;
830
const char *msg = commit->buffer;
@@ -921,20 +905,15 @@ long format_commit_message(const struct commit *commit, const void *format,
905
if (!table[i].value)
906
interp_set_entry(table, i, "<unknown>");
907
924
- do {
925
- char *buf = *buf_p;
926
- unsigned long len;
927
-
928
- len = interpolate(buf, *space_p, format,
929
- table, ARRAY_SIZE(table));
930
- if (len < *space_p)
931
- break;
932
- ALLOC_GROW(buf, len + 1, *space_p);
933
- *buf_p = buf;
934
- } while (1);
908
+ len = interpolate(sb->buf + sb->len, strbuf_avail(sb),
909
+ format, table, ARRAY_SIZE(table));
910
+ if (len > strbuf_avail(sb)) {
911
+ strbuf_grow(sb, len);
912
+ interpolate(sb->buf + sb->len, strbuf_avail(sb) + 1,
913
+ format, table, ARRAY_SIZE(table));
914
+ }
915
+ strbuf_setlen(sb, sb->len + len);
916
interp_clear_table(table, ARRAY_SIZE(table));
936
-
937
- return strlen(*buf_p);
917
}
918
919
static void pp_header(enum cmit_fmt fmt,
@@ -943,34 +922,24 @@ static void pp_header(enum cmit_fmt fmt,
922
const char *encoding,
923
const struct commit *commit,
924
const char **msg_p,
946
- unsigned long *len_p,
947
- unsigned long *ofs_p,
948
- char **buf_p,
949
- unsigned long *space_p)
925
+ struct strbuf *sb)
926
{
927
int parents_shown = 0;
928
929
for (;;) {
930
const char *line = *msg_p;
955
- char *dst;
956
- int linelen = get_one_line(*msg_p, *len_p);
957
- unsigned long len;
931
+ int linelen = get_one_line(*msg_p);
932
933
if (!linelen)
934
return;
935
*msg_p += linelen;
962
- *len_p -= linelen;
936
937
if (linelen == 1)
938
/* End of header */
939
return;
940
968
- ALLOC_GROW(*buf_p, linelen + *ofs_p + 20, *space_p);
969
- dst = *buf_p + *ofs_p;
970
-
941
if (fmt == CMIT_FMT_RAW) {
972
- memcpy(dst, line, linelen);
973
- *ofs_p += linelen;
942
+ strbuf_add(sb, line, linelen);
943
continue;
944
}
945
@@ -988,10 +957,8 @@ static void pp_header(enum cmit_fmt fmt,
957
parent = parent->next, num++)
958
;
959
/* with enough slop */
991
- num = *ofs_p + num * 50 + 20;
992
- ALLOC_GROW(*buf_p, num, *space_p);
993
- dst = *buf_p + *ofs_p;
994
- *ofs_p += add_merge_info(fmt, dst, commit, abbrev);
960
+ strbuf_grow(sb, num * 50 + 20);
961
+ add_merge_info(fmt, sb, commit, abbrev);
962
parents_shown = 1;
963
}
964
@@ -1001,129 +968,99 @@ static void pp_header(enum cmit_fmt fmt,
968
* FULLER shows both authors and dates.
969
*/
970
if (!memcmp(line, "author ", 7)) {
1004
- len = linelen;
971
+ unsigned long len = linelen;
972
if (fmt == CMIT_FMT_EMAIL)
973
len = bound_rfc2047(linelen, encoding);
1007
- ALLOC_GROW(*buf_p, *ofs_p + len + 80, *space_p);
1008
- dst = *buf_p + *ofs_p;
1009
- *ofs_p += add_user_info("Author", fmt, dst,
1010
- line + 7, dmode, encoding);
974
+ strbuf_grow(sb, len + 80);
975
+ add_user_info("Author", fmt, sb, line + 7, dmode, encoding);
976
}
977
978
if (!memcmp(line, "committer ", 10) &&
979
(fmt == CMIT_FMT_FULL || fmt == CMIT_FMT_FULLER)) {
1015
- len = linelen;
980
+ unsigned long len = linelen;
981
if (fmt == CMIT_FMT_EMAIL)
982
len = bound_rfc2047(linelen, encoding);
1018
- ALLOC_GROW(*buf_p, *ofs_p + len + 80, *space_p);
1019
- dst = *buf_p + *ofs_p;
1020
- *ofs_p += add_user_info("Commit", fmt, dst,
1021
- line + 10, dmode, encoding);
983
+ strbuf_grow(sb, len + 80);
984
+ add_user_info("Commit", fmt, sb, line + 10, dmode, encoding);
985
}
986
}
987
}
988
989
static void pp_title_line(enum cmit_fmt fmt,
990
const char **msg_p,
1028
- unsigned long *len_p,
1029
- unsigned long *ofs_p,
1030
- char **buf_p,
1031
- unsigned long *space_p,
1032
- int indent,
991
+ struct strbuf *sb,
992
const char *subject,
993
const char *after_subject,
994
const char *encoding,
995
int plain_non_ascii)
996
{
1038
- char *title;
1039
- unsigned long title_alloc, title_len;
997
+ struct strbuf title;
998
unsigned long len;
999
1042
- title_len = 0;
1043
- title_alloc = 80;
1044
- title = xmalloc(title_alloc);
1000
+ strbuf_init(&title, 80);
1001
+
1002
for (;;) {
1003
const char *line = *msg_p;
1047
- int linelen = get_one_line(line, *len_p);
1048
- *msg_p += linelen;
1049
- *len_p -= linelen;
1004
+ int linelen = get_one_line(line);
1005
1006
+ *msg_p += linelen;
1007
if (!linelen || is_empty_line(line, &linelen))
1008
break;
1009
1054
- if (title_alloc <= title_len + linelen + 2) {
1055
- title_alloc = title_len + linelen + 80;
1056
- title = xrealloc(title, title_alloc);
1057
- }
1058
- len = 0;
1059
- if (title_len) {
1010
+ strbuf_grow(&title, linelen + 2);
1011
+ if (title.len) {
1012
if (fmt == CMIT_FMT_EMAIL) {
1061
- len++;
1062
- title[title_len++] = '\n';
1013
+ strbuf_addch(&title, '\n');
1014
}
1064
- len++;
1065
- title[title_len++] = ' ';
1015
+ strbuf_addch(&title, ' ');
1016
}
1067
- memcpy(title + title_len, line, linelen);
1068
- title_len += linelen;
1017
+ strbuf_add(&title, line, linelen);
1018
}
1019
1020
/* Enough slop for the MIME header and rfc2047 */
1072
- len = bound_rfc2047(title_len, encoding)+ 1000;
1021
+ len = bound_rfc2047(title.len, encoding) + 1000;
1022
if (subject)
1023
len += strlen(subject);
1024
if (after_subject)
1025
len += strlen(after_subject);
1026
if (encoding)
1027
len += strlen(encoding);
1079
- ALLOC_GROW(*buf_p, title_len + *ofs_p + len, *space_p);
1028
1029
+ strbuf_grow(sb, title.len + len);
1030
if (subject) {
1082
- len = strlen(subject);
1083
- memcpy(*buf_p + *ofs_p, subject, len);
1084
- *ofs_p += len;
1085
- *ofs_p += add_rfc2047(*buf_p + *ofs_p,
1086
- title, title_len, encoding);
1031
+ strbuf_addstr(sb, subject);
1032
+ add_rfc2047(sb, title.buf, title.len, encoding);
1033
} else {
1088
- memcpy(*buf_p + *ofs_p, title, title_len);
1089
- *ofs_p += title_len;
1034
+ strbuf_addbuf(sb, &title);
1035
}
1091
- (*buf_p)[(*ofs_p)++] = '\n';
1036
+ strbuf_addch(sb, '\n');
1037
+
1038
if (plain_non_ascii) {
1039
const char *header_fmt =
1040
"MIME-Version: 1.0\n"
1041
"Content-Type: text/plain; charset=%s\n"
1042
"Content-Transfer-Encoding: 8bit\n";
1097
- *ofs_p += snprintf(*buf_p + *ofs_p,
1098
- *space_p - *ofs_p,
1099
- header_fmt, encoding);
1043
+ strbuf_addf(sb, header_fmt, encoding);
1044
}
1045
if (after_subject) {
1102
- len = strlen(after_subject);
1103
- memcpy(*buf_p + *ofs_p, after_subject, len);
1104
- *ofs_p += len;
1046
+ strbuf_addstr(sb, after_subject);
1047
}
1106
- free(title);
1048
if (fmt == CMIT_FMT_EMAIL) {
1108
- ALLOC_GROW(*buf_p, *ofs_p + 20, *space_p);
1109
- (*buf_p)[(*ofs_p)++] = '\n';
1049
+ strbuf_addch(sb, '\n');
1050
}
1051
+ strbuf_release(&title);
1052
}
1053
1054
static void pp_remainder(enum cmit_fmt fmt,
1055
const char **msg_p,
1115
- unsigned long *len_p,
1116
- unsigned long *ofs_p,
1117
- char **buf_p,
1118
- unsigned long *space_p,
1056
+ struct strbuf *sb,
1057
int indent)
1058
{
1059
int first = 1;
1060
for (;;) {
1061
const char *line = *msg_p;
1124
- int linelen = get_one_line(line, *len_p);
1062
+ int linelen = get_one_line(line);
1063
*msg_p += linelen;
1126
- *len_p -= linelen;
1064
1065
if (!linelen)
1066
break;
@@ -1136,36 +1073,32 @@ static void pp_remainder(enum cmit_fmt fmt,
1073
}
1074
first = 0;
1075
1139
- ALLOC_GROW(*buf_p, *ofs_p + linelen + indent + 20, *space_p);
1076
+ strbuf_grow(sb, linelen + indent + 20);
1077
if (indent) {
1141
- memset(*buf_p + *ofs_p, ' ', indent);
1142
- *ofs_p += indent;
1078
+ memset(sb->buf + sb->len, ' ', indent);
1079
+ strbuf_setlen(sb, sb->len + indent);
1080
}
1144
- memcpy(*buf_p + *ofs_p, line, linelen);
1145
- *ofs_p += linelen;
1146
- (*buf_p)[(*ofs_p)++] = '\n';
1081
+ strbuf_add(sb, line, linelen);
1082
+ strbuf_addch(sb, '\n');
1083
}
1084
}
1085
1150
-unsigned long pretty_print_commit(enum cmit_fmt fmt,
1151
- const struct commit *commit,
1152
- unsigned long len,
1153
- char **buf_p, unsigned long *space_p,
1154
- int abbrev, const char *subject,
1155
- const char *after_subject,
1086
+void pretty_print_commit(enum cmit_fmt fmt, const struct commit *commit,
1087
+ struct strbuf *sb, int abbrev,
1088
+ const char *subject, const char *after_subject,
1089
enum date_mode dmode)
1090
{
1158
- unsigned long offset = 0;
1091
unsigned long beginning_of_body;
1092
int indent = 4;
1093
const char *msg = commit->buffer;
1094
int plain_non_ascii = 0;
1095
char *reencoded;
1096
const char *encoding;
1165
- char *buf;
1097
1167
- if (fmt == CMIT_FMT_USERFORMAT)
1168
- return format_commit_message(commit, user_format, buf_p, space_p);
1098
+ if (fmt == CMIT_FMT_USERFORMAT) {
1099
+ format_commit_message(commit, user_format, sb);
1100
+ return;
1101
+ }
1102
1103
encoding = (git_log_output_encoding
1104
? git_log_output_encoding
@@ -1175,7 +1108,6 @@ unsigned long pretty_print_commit(enum cmit_fmt fmt,
1108
reencoded = logmsg_reencode(commit, encoding);
1109
if (reencoded) {
1110
msg = reencoded;
1178
- len = strlen(reencoded);
1111
}
1112
1113
if (fmt == CMIT_FMT_ONELINE || fmt == CMIT_FMT_EMAIL)
@@ -1190,14 +1122,13 @@ unsigned long pretty_print_commit(enum cmit_fmt fmt,
1122
if (fmt == CMIT_FMT_EMAIL && !after_subject) {
1123
int i, ch, in_body;
1124
1193
- for (in_body = i = 0; (ch = msg[i]) && i < len; i++) {
1125
+ for (in_body = i = 0; (ch = msg[i]); i++) {
1126
if (!in_body) {
1127
/* author could be non 7-bit ASCII but
1128
* the log may be so; skip over the
1129
* header part first.
1130
*/
1199
- if (ch == '\n' &&
1200
- i + 1 < len && msg[i+1] == '\n')
1131
+ if (ch == '\n' && msg[i+1] == '\n')
1132
in_body = 1;
1133
}
1134
else if (non_ascii(ch)) {
@@ -1207,59 +1138,44 @@ unsigned long pretty_print_commit(enum cmit_fmt fmt,
1138
}
1139
}
1140
1210
- pp_header(fmt, abbrev, dmode, encoding,
1211
- commit, &msg, &len,
1212
- &offset, buf_p, space_p);
1141
+ pp_header(fmt, abbrev, dmode, encoding, commit, &msg, sb);
1142
if (fmt != CMIT_FMT_ONELINE && !subject) {
1214
- ALLOC_GROW(*buf_p, offset + 20, *space_p);
1215
- (*buf_p)[offset++] = '\n';
1143
+ strbuf_addch(sb, '\n');
1144
}
1145
1146
/* Skip excess blank lines at the beginning of body, if any... */
1147
for (;;) {
1220
- int linelen = get_one_line(msg, len);
1148
+ int linelen = get_one_line(msg);
1149
int ll = linelen;
1150
if (!linelen)
1151
break;
1152
if (!is_empty_line(msg, &ll))
1153
break;
1154
msg += linelen;
1227
- len -= linelen;
1155
}
1156
1157
/* These formats treat the title line specially. */
1231
- if (fmt == CMIT_FMT_ONELINE
1232
- || fmt == CMIT_FMT_EMAIL)
1233
- pp_title_line(fmt, &msg, &len, &offset,
1234
- buf_p, space_p, indent,
1235
- subject, after_subject, encoding,
1236
- plain_non_ascii);
1237
-
1238
- beginning_of_body = offset;
1239
- if (fmt != CMIT_FMT_ONELINE)
1240
- pp_remainder(fmt, &msg, &len, &offset,
1241
- buf_p, space_p, indent);
1242
-
1243
- while (offset && isspace((*buf_p)[offset-1]))
1244
- offset--;
1158
+ if (fmt == CMIT_FMT_ONELINE || fmt == CMIT_FMT_EMAIL)
1159
+ pp_title_line(fmt, &msg, sb, subject,
1160
+ after_subject, encoding, plain_non_ascii);
1161
1246
- ALLOC_GROW(*buf_p, offset + 20, *space_p);
1247
- buf = *buf_p;
1162
+ beginning_of_body = sb->len;
1163
+ if (fmt != CMIT_FMT_ONELINE)
1164
+ pp_remainder(fmt, &msg, sb, indent);
1165
+ strbuf_rtrim(sb);
1166
1167
/* Make sure there is an EOLN for the non-oneline case */
1168
if (fmt != CMIT_FMT_ONELINE)
1251
- buf[offset++] = '\n';
1169
+ strbuf_addch(sb, '\n');
1170
1171
/*
1172
* The caller may append additional body text in e-mail
1173
* format. Make sure we did not strip the blank line
1174
* between the header and the body.
1175
*/
1258
- if (fmt == CMIT_FMT_EMAIL && offset <= beginning_of_body)
1259
- buf[offset++] = '\n';
1260
- buf[offset] = '\0';
1176
+ if (fmt == CMIT_FMT_EMAIL && sb->len <= beginning_of_body)
1177
+ strbuf_addch(sb, '\n');
1178
free(reencoded);
1262
- return offset;
1179
}
1180
1181
struct commit *pop_commit(struct commit_list **stack)
@@ -1338,12 +1254,12 @@ void sort_in_topological_order_fn(struct commit_list ** list, int lifo,
1254
next=next->next;
1255
}
1256
/*
1341
- * find the tips
1342
- *
1343
- * tips are nodes not reachable from any other node in the list
1344
- *
1345
- * the tips serve as a starting set for the work queue.
1346
- */
1257
+ * find the tips
1258
+ *
1259
+ * tips are nodes not reachable from any other node in the list
1260
+ *
1261
+ * the tips serve as a starting set for the work queue.
1262
+ */
1263
next=*list;
1264
insert = &work;
1265
while (next) {
@@ -1370,9 +1286,9 @@ void sort_in_topological_order_fn(struct commit_list ** list, int lifo,
1286
if (pn) {
1287
/*
1288
* parents are only enqueued for emission
1373
- * when all their children have been emitted thereby
1374
- * guaranteeing topological order.
1375
- */
1289
+ * when all their children have been emitted thereby
1290
+ * guaranteeing topological order.
1291
+ */
1292
pn->indegree--;
1293
if (!pn->indegree) {
1294
if (!lifo)
@@ -1384,9 +1300,9 @@ void sort_in_topological_order_fn(struct commit_list ** list, int lifo,
1300
parents=parents->next;
1301
}
1302
/*
1387
- * work_item is a commit all of whose children
1388
- * have already been emitted. we can emit it now.
1389
- */
1303
+ * work_item is a commit all of whose children
1304
+ * have already been emitted. we can emit it now.
1305
+ */
1306
*pptr = work_node->list_item;
1307
pptr = &(*pptr)->next;
1308
*pptr = NULL;
@@ -1482,8 +1398,7 @@ static struct commit_list *merge_bases(struct commit *one, struct commit *two)
1398
}
1399
1400
struct commit_list *get_merge_bases(struct commit *one,
1485
- struct commit *two,
1486
- int cleanup)
1401
+ struct commit *two, int cleanup)
1402
{
1403
struct commit_list *list;
1404
struct commit **rslt;
commit.h
+7
-2
@@ -3,6 +3,7 @@
3
4
#include "object.h"
5
#include "tree.h"
6
+#include "strbuf.h"
7
#include "decorate.h"
8
9
struct commit_list {
@@ -61,8 +62,12 @@ enum cmit_fmt {
62
};
63
64
extern enum cmit_fmt get_commit_format(const char *arg);
64
-extern long format_commit_message(const struct commit *commit, const void *template, char **buf_p, unsigned long *space_p);
65
-extern unsigned long pretty_print_commit(enum cmit_fmt fmt, const struct commit *, unsigned long len, char **buf_p, unsigned long *space_p, int abbrev, const char *subject, const char *after_subject, enum date_mode dmode);
65
+extern void format_commit_message(const struct commit *commit,
66
+ const void *format, struct strbuf *sb);
67
+extern void pretty_print_commit(enum cmit_fmt fmt, const struct commit*,
68
+ struct strbuf *,
69
+ int abbrev, const char *subject,
70
+ const char *after_subject, enum date_mode);
71
72
/** Removes the first commit from a list sorted by date, and adds all
73
* of its parents.
log-tree.c
+19
-37
@@ -79,25 +79,14 @@ static int detect_any_signoff(char *letter, int size)
79
return seen_head && seen_name;
80
}
81
82
-static unsigned long append_signoff(char **buf_p, unsigned long *buf_sz_p,
83
- unsigned long at, const char *signoff)
82
+static void append_signoff(struct strbuf *sb, const char *signoff)
83
{
84
static const char signed_off_by[] = "Signed-off-by: ";
85
size_t signoff_len = strlen(signoff);
86
int has_signoff = 0;
87
char *cp;
89
- char *buf;
90
- unsigned long buf_sz;
91
-
92
- buf = *buf_p;
93
- buf_sz = *buf_sz_p;
94
- if (buf_sz <= at + strlen(signed_off_by) + signoff_len + 3) {
95
- buf_sz += strlen(signed_off_by) + signoff_len + 3;
96
- buf = xrealloc(buf, buf_sz);
97
- *buf_p = buf;
98
- *buf_sz_p = buf_sz;
99
- }
100
- cp = buf;
88
+
89
+ cp = sb->buf;
90
91
/* First see if we already have the sign-off by the signer */
92
while ((cp = strstr(cp, signed_off_by))) {
@@ -105,29 +94,25 @@ static unsigned long append_signoff(char **buf_p, unsigned long *buf_sz_p,
94
has_signoff = 1;
95
96
cp += strlen(signed_off_by);
108
- if (cp + signoff_len >= buf + at)
97
+ if (cp + signoff_len >= sb->buf + sb->len)
98
break;
99
if (strncmp(cp, signoff, signoff_len))
100
continue;
101
if (!isspace(cp[signoff_len]))
102
continue;
103
/* we already have him */
115
- return at;
104
+ return;
105
}
106
107
if (!has_signoff)
119
- has_signoff = detect_any_signoff(buf, at);
108
+ has_signoff = detect_any_signoff(sb->buf, sb->len);
109
110
if (!has_signoff)
122
- buf[at++] = '\n';
123
-
124
- strcpy(buf + at, signed_off_by);
125
- at += strlen(signed_off_by);
126
- strcpy(buf + at, signoff);
127
- at += signoff_len;
128
- buf[at++] = '\n';
129
- buf[at] = 0;
130
- return at;
111
+ strbuf_addch(sb, '\n');
112
+
113
+ strbuf_addstr(sb, signed_off_by);
114
+ strbuf_add(sb, signoff, signoff_len);
115
+ strbuf_addch(sb, '\n');
116
}
117
118
static unsigned int digits_in_number(unsigned int number)
@@ -142,14 +127,12 @@ static unsigned int digits_in_number(unsigned int number)
127
128
void show_log(struct rev_info *opt, const char *sep)
129
{
145
- char *msgbuf = NULL;
146
- unsigned long msgbuf_len = 0;
130
+ struct strbuf msgbuf;
131
struct log_info *log = opt->loginfo;
132
struct commit *commit = log->commit, *parent = log->parent;
133
int abbrev = opt->diffopt.abbrev;
134
int abbrev_commit = opt->abbrev_commit ? opt->abbrev : 40;
135
const char *extra;
152
- int len;
136
const char *subject = NULL, *extra_headers = opt->extra_headers;
137
138
opt->loginfo = NULL;
@@ -288,18 +271,17 @@ void show_log(struct rev_info *opt, const char *sep)
271
/*
272
* And then the pretty-printed message itself
273
*/
291
- len = pretty_print_commit(opt->commit_format, commit, ~0u,
292
- &msgbuf, &msgbuf_len, abbrev, subject,
293
- extra_headers, opt->date_mode);
274
+ strbuf_init(&msgbuf, 0);
275
+ pretty_print_commit(opt->commit_format, commit, &msgbuf,
276
+ abbrev, subject, extra_headers, opt->date_mode);
277
278
if (opt->add_signoff)
296
- len = append_signoff(&msgbuf, &msgbuf_len, len,
297
- opt->add_signoff);
279
+ append_signoff(&msgbuf, opt->add_signoff);
280
if (opt->show_log_size)
299
- printf("log size %i\n", len);
281
+ printf("log size %i\n", (int)msgbuf.len);
282
301
- printf("%s%s%s", msgbuf, extra, sep);
302
- free(msgbuf);
283
+ printf("%s%s%s", msgbuf.buf, extra, sep);
284
+ strbuf_release(&msgbuf);
285
}
286
287
int log_tree_diff_flush(struct rev_info *opt)