@matheusbsbs / matheus-bagliano / commits / 80583c0ef6
Fork 0

Lift 16kB limit of log message output

Traditionally we had 16kB limit when formatting log messages for output, because it was easier to arrange for the caller to have a reasonably big buffer and pass it down without ever worrying about reallocating. This changes the calling convention of pretty_print_commit() to lift this limit. Instead of the buffer and remaining length, it now takes a pointer to the pointer that points at the allocated buffer, and another pointer to the location that stores the allocated length, and reallocates the buffer as necessary. To support the user format, the error return of interpolate() needed to be changed. It used to return a bool telling "Ok the result fits", or "Sorry, I had to truncate it". Now it returns 0 on success, and returns the size of the buffer it wants in order to fit the whole result. Signed-off-by: Junio C Hamano <gitster@pobox.com>

Junio C Hamano committed Jun 11, 2007 at 00:34 UTC 80583c0ef61cc966c7eee79cf3623a83197e19b8
9 files changed +124 -74
builtin-branch.c
+11 -6
@@ -242,7 +242,6 @@ static void print_ref_item(struct ref_item *item, int maxwidth, int verbose,
242 char c;
243 int color;
244 struct commit *commit;
245 - char subject[256];
245
246 switch (item->kind) {
247 case REF_LOCAL_BRANCH:
@@ -263,17 +262,23 @@ static void print_ref_item(struct ref_item *item, int maxwidth, int verbose,
262 }
263
264 if (verbose) {
265 + char *subject = NULL;
266 + unsigned long subject_len = 0;
267 + const char *sub = " **** invalid ref ****";
268 +
269 commit = lookup_commit(item->sha1);
267 - if (commit && !parse_commit(commit))
270 + if (commit && !parse_commit(commit)) {
271 pretty_print_commit(CMIT_FMT_ONELINE, commit, ~0,
269 - subject, sizeof(subject), 0,
272 + &subject, &subject_len, 0,
273 NULL, NULL, 0);
271 - else
272 - strcpy(subject, " **** invalid ref ****");
274 + sub = subject;
275 + }
276 printf("%c %s%-*s%s %s %s\n", c, branch_get_color(color),
277 maxwidth, item->name,
278 branch_get_color(COLOR_BRANCH_RESET),
276 - find_unique_abbrev(item->sha1, abbrev), subject);
279 + find_unique_abbrev(item->sha1, abbrev), sub);
280 + if (subject)
281 + free(subject);
282 } else {
283 printf("%c %s%s%s\n", c, branch_get_color(color), item->name,
284 branch_get_color(COLOR_BRANCH_RESET));
builtin-log.c
+4 -2
@@ -742,11 +742,13 @@ int cmd_cherry(int argc, const char **argv, const char *prefix)
742 sign = '-';
743
744 if (verbose) {
745 - static char buf[16384];
745 + char *buf = NULL;
746 + unsigned long buflen = 0;
747 pretty_print_commit(CMIT_FMT_ONELINE, commit, ~0,
747 - buf, sizeof(buf), 0, NULL, NULL, 0);
748 + &buf, &buflen, 0, NULL, NULL, 0);
749 printf("%c %s %s\n", sign,
750 sha1_to_hex(commit->object.sha1), buf);
751 + free(buf);
752 }
753 else {
754 printf("%c %s\n", sign,
builtin-rev-list.c
+5 -3
@@ -92,11 +92,13 @@ static void show_commit(struct commit *commit)
92 putchar('\n');
93
94 if (revs.verbose_header) {
95 - static char pretty_header[16384];
95 + char *buf = NULL;
96 + unsigned long buflen = 0;
97 pretty_print_commit(revs.commit_format, commit, ~0,
97 - pretty_header, sizeof(pretty_header),
98 + &buf, &buflen,
99 revs.abbrev, NULL, NULL, revs.date_mode);
99 - printf("%s%c", pretty_header, hdr_termination);
100 + printf("%s%c", buf, hdr_termination);
101 + free(buf);
102 }
103 fflush(stdout);
104 if (commit->parents) {
builtin-show-branch.c
+13 -10
@@ -259,17 +259,19 @@ 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[256], *cp;
262 + char *pretty = NULL;
263 + const char *pretty_str = "(unavailable)";
264 + unsigned long pretty_len = 0;
265 struct commit_name *name = commit->util;
264 - if (commit->object.parsed)
266 +
267 + if (commit->object.parsed) {
268 pretty_print_commit(CMIT_FMT_ONELINE, commit, ~0,
266 - pretty, sizeof(pretty), 0, NULL, NULL, 0);
267 - else
268 - strcpy(pretty, "(unavailable)");
269 - if (!prefixcmp(pretty, "[PATCH] "))
270 - cp = pretty + 8;
271 - else
272 - cp = pretty;
269 + &pretty, &pretty_len,
270 + 0, NULL, NULL, 0);
271 + pretty_str = pretty;
272 + }
273 + if (!prefixcmp(pretty_str, "[PATCH] "))
274 + pretty_str += 8;
275
276 if (!no_name) {
277 if (name && name->head_name) {
@@ -286,7 +288,8 @@ static void show_one_commit(struct commit *commit, int no_name)
288 printf("[%s] ",
289 find_unique_abbrev(commit->object.sha1, 7));
290 }
289 - puts(cp);
291 + puts(pretty_str);
292 + free(pretty);
293 }
294
295 static char *ref_name[MAX_REVS + 1];
commit.c
+43 -12
@@ -776,7 +776,7 @@ static void fill_person(struct interp *table, const char *msg, int len)
776 }
777
778 static long format_commit_message(const struct commit *commit,
779 - const char *msg, char *buf, unsigned long space)
779 + const char *msg, char **buf_p, unsigned long *space_p)
780 {
781 struct interp table[] = {
782 { "%H" }, /* commit hash */
@@ -905,16 +905,27 @@ static long format_commit_message(const struct commit *commit,
905 if (!table[i].value)
906 interp_set_entry(table, i, "<unknown>");
907
908 - interpolate(buf, space, user_format, table, ARRAY_SIZE(table));
908 + do {
909 + char *buf = *buf_p;
910 + unsigned long space = *space_p;
911 +
912 + space = interpolate(buf, space, user_format,
913 + table, ARRAY_SIZE(table));
914 + if (!space)
915 + break;
916 + buf = xrealloc(buf, space);
917 + *buf_p = buf;
918 + *space_p = space;
919 + } while (1);
920 interp_clear_table(table, ARRAY_SIZE(table));
921
911 - return strlen(buf);
922 + return strlen(*buf_p);
923 }
924
925 unsigned long pretty_print_commit(enum cmit_fmt fmt,
926 const struct commit *commit,
927 unsigned long len,
917 - char *buf, unsigned long space,
928 + char **buf_p, unsigned long *space_p,
929 int abbrev, const char *subject,
930 const char *after_subject,
931 enum date_mode dmode)
@@ -927,9 +938,11 @@ unsigned long pretty_print_commit(enum cmit_fmt fmt,
938 int plain_non_ascii = 0;
939 char *reencoded;
940 const char *encoding;
941 + char *buf;
942 + unsigned long space, slop;
943
944 if (fmt == CMIT_FMT_USERFORMAT)
932 - return format_commit_message(commit, msg, buf, space);
945 + return format_commit_message(commit, msg, buf_p, space_p);
946
947 encoding = (git_log_output_encoding
948 ? git_log_output_encoding
@@ -969,6 +982,26 @@ unsigned long pretty_print_commit(enum cmit_fmt fmt,
982 }
983 }
984
985 + space = *space_p;
986 + buf = *buf_p;
987 +
988 + /*
989 + * We do not want to repeatedly realloc below, so
990 + * preallocate with enough slop to hold MIME headers,
991 + * "Subject: " prefix, etc.
992 + */
993 + slop = 1000;
994 + if (subject)
995 + slop += strlen(subject);
996 + if (after_subject)
997 + slop += strlen(after_subject);
998 + if (space < strlen(msg) + slop) {
999 + space = strlen(msg) + slop;
1000 + buf = xrealloc(buf, space);
1001 + *space_p = space;
1002 + *buf_p = buf;
1003 + }
1004 +
1005 for (;;) {
1006 const char *line = msg;
1007 int linelen = get_one_line(msg, len);
@@ -976,14 +1009,12 @@ unsigned long pretty_print_commit(enum cmit_fmt fmt,
1009 if (!linelen)
1010 break;
1011
979 - /*
980 - * We want some slop for indentation and a possible
981 - * final "...". Thus the "+ 20".
982 - */
1012 + /* 20 would cover indent and leave us some slop */
1013 if (offset + linelen + 20 > space) {
984 - memcpy(buf + offset, " ...\n", 8);
985 - offset += 8;
986 - break;
1014 + space = offset + linelen + 20;
1015 + buf = xrealloc(buf, space);
1016 + *buf_p = buf;
1017 + *space_p = space;
1018 }
1019
1020 msg += linelen;
commit.h
+1 -1
@@ -61,7 +61,7 @@ enum cmit_fmt {
61 };
62
63 extern enum cmit_fmt get_commit_format(const char *arg);
64 -extern unsigned long pretty_print_commit(enum cmit_fmt fmt, const struct commit *, unsigned long len, char *buf, unsigned long space, int abbrev, const char *subject, const char *after_subject, enum date_mode dmode);
64 +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
66 /** Removes the first commit from a list sorted by date, and adds all
67 * of its parents.
interpolate.c
+21 -25
@@ -44,33 +44,33 @@ void interp_clear_table(struct interp *table, int ninterps)
44 * { "%%", "%"},
45 * }
46 *
47 - * Returns 1 on a successful substitution pass that fits in result,
48 - * Returns 0 on a failed or overflowing substitution pass.
47 + * Returns 0 on a successful substitution pass that fits in result,
48 + * Returns a number of bytes needed to hold the full substituted
49 + * string otherwise.
50 */
51
51 -int interpolate(char *result, int reslen,
52 +unsigned long interpolate(char *result, unsigned long reslen,
53 const char *orig,
54 const struct interp *interps, int ninterps)
55 {
56 const char *src = orig;
57 char *dest = result;
57 - int newlen = 0;
58 + unsigned long newlen = 0;
59 const char *name, *value;
59 - int namelen, valuelen;
60 + unsigned long namelen, valuelen;
61 int i;
62 char c;
63
64 memset(result, 0, reslen);
65
65 - while ((c = *src) && newlen < reslen - 1) {
66 + while ((c = *src)) {
67 if (c == '%') {
68 /* Try to match an interpolation string. */
69 for (i = 0; i < ninterps; i++) {
70 name = interps[i].name;
71 namelen = strlen(name);
71 - if (strncmp(src, name, namelen) == 0) {
72 + if (strncmp(src, name, namelen) == 0)
73 break;
73 - }
74 }
75
76 /* Check for valid interpolation. */
@@ -78,29 +78,25 @@ int interpolate(char *result, int reslen,
78 value = interps[i].value;
79 valuelen = strlen(value);
80
81 - if (newlen + valuelen < reslen - 1) {
81 + if (newlen + valuelen + 1 < reslen) {
82 /* Substitute. */
83 strncpy(dest, value, valuelen);
84 - newlen += valuelen;
84 dest += valuelen;
86 - src += namelen;
87 - } else {
88 - /* Something's not fitting. */
89 - return 0;
85 }
91 -
92 - } else {
93 - /* Skip bogus interpolation. */
94 - *dest++ = *src++;
95 - newlen++;
86 + newlen += valuelen;
87 + src += namelen;
88 + continue;
89 }
97 -
98 - } else {
99 - /* Straight copy one non-interpolation character. */
100 - *dest++ = *src++;
101 - newlen++;
90 }
91 + /* Straight copy one non-interpolation character. */
92 + if (newlen + 1 < reslen)
93 + *dest++ = *src;
94 + src++;
95 + newlen++;
96 }
97
105 - return newlen < reslen - 1;
98 + if (newlen + 1 < reslen)
99 + return 0;
100 + else
101 + return newlen + 2;
102 }
interpolate.h
+3 -3
@@ -19,8 +19,8 @@ struct interp {
19 extern void interp_set_entry(struct interp *table, int slot, const char *value);
20 extern void interp_clear_table(struct interp *table, int ninterps);
21
22 -extern int interpolate(char *result, int reslen,
23 - const char *orig,
24 - const struct interp *interps, int ninterps);
22 +extern unsigned long interpolate(char *result, unsigned long reslen,
23 + const char *orig,
24 + const struct interp *interps, int ninterps);
25
26 #endif /* INTERPOLATE_H */
log-tree.c
+23 -12
@@ -79,16 +79,25 @@ static int detect_any_signoff(char *letter, int size)
79 return seen_head && seen_name;
80 }
81
82 -static int append_signoff(char *buf, int buf_sz, int at, const char *signoff)
82 +static unsigned long append_signoff(char **buf_p, unsigned long *buf_sz_p,
83 + unsigned long at, const char *signoff)
84 {
85 static const char signed_off_by[] = "Signed-off-by: ";
85 - int signoff_len = strlen(signoff);
86 + size_t signoff_len = strlen(signoff);
87 int has_signoff = 0;
87 - char *cp = buf;
88 -
89 - /* Do we have enough space to add it? */
90 - if (buf_sz - at <= strlen(signed_off_by) + signoff_len + 3)
91 - return at;
88 + 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;
101
102 /* First see if we already have the sign-off by the signer */
103 while ((cp = strstr(cp, signed_off_by))) {
@@ -133,7 +142,8 @@ static unsigned int digits_in_number(unsigned int number)
142
143 void show_log(struct rev_info *opt, const char *sep)
144 {
136 - static char this_header[16384];
145 + char *msgbuf = NULL;
146 + unsigned long msgbuf_len = 0;
147 struct log_info *log = opt->loginfo;
148 struct commit *commit = log->commit, *parent = log->parent;
149 int abbrev = opt->diffopt.abbrev;
@@ -278,14 +288,15 @@ void show_log(struct rev_info *opt, const char *sep)
288 /*
289 * And then the pretty-printed message itself
290 */
281 - len = pretty_print_commit(opt->commit_format, commit, ~0u, this_header,
282 - sizeof(this_header), abbrev, subject,
291 + len = pretty_print_commit(opt->commit_format, commit, ~0u,
292 + &msgbuf, &msgbuf_len, abbrev, subject,
293 extra_headers, opt->date_mode);
294
295 if (opt->add_signoff)
286 - len = append_signoff(this_header, sizeof(this_header), len,
296 + len = append_signoff(&msgbuf, &msgbuf_len, len,
297 opt->add_signoff);
288 - printf("%s%s%s", this_header, extra, sep);
298 + printf("%s%s%s", msgbuf, extra, sep);
299 + free(msgbuf);
300 }
301
302 int log_tree_diff_flush(struct rev_info *opt)