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
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)
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
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);
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;