529
return bp - buf;
530
}
531
532
+static unsigned long bound_rfc2047(unsigned long len, const char *encoding)
533
+{
534
+ /* upper bound of q encoded string of length 'len' */
535
+ unsigned long elen = strlen(encoding);
536
+
537
+ return len * 3 + elen + 100;
538
+}
539
+
540
static int add_user_info(const char *what, enum cmit_fmt fmt, char *buf,
541
const char *line, enum date_mode dmode,
542
const char *encoding)
930
return strlen(*buf_p);
931
}
932
933
+static void pp_header(enum cmit_fmt fmt,
934
+ int abbrev,
935
+ enum date_mode dmode,
936
+ const char *encoding,
937
+ const struct commit *commit,
938
+ const char **msg_p,
939
+ unsigned long *len_p,
940
+ unsigned long *ofs_p,
941
+ char **buf_p,
942
+ unsigned long *space_p)
943
+{
944
+ int parents_shown = 0;
945
+
946
+ for (;;) {
947
+ const char *line = *msg_p;
948
+ char *dst;
949
+ int linelen = get_one_line(*msg_p, *len_p);
950
+ unsigned long len;
951
+
952
+ if (!linelen)
953
+ return;
954
+ *msg_p += linelen;
955
+ *len_p -= linelen;
956
+
957
+ if (linelen == 1)
958
+ /* End of header */
959
+ return;
960
+
961
+ ALLOC_GROW(*buf_p, linelen + *ofs_p + 20, *space_p);
962
+ dst = *buf_p + *ofs_p;
963
+
964
+ if (fmt == CMIT_FMT_RAW) {
965
+ memcpy(dst, line, linelen);
966
+ *ofs_p += linelen;
967
+ continue;
968
+ }
969
+
970
+ if (!memcmp(line, "parent ", 7)) {
971
+ if (linelen != 48)
972
+ die("bad parent line in commit");
973
+ continue;
974
+ }
975
+
976
+ if (!parents_shown) {
977
+ struct commit_list *parent;
978
+ int num;
979
+ for (parent = commit->parents, num = 0;
980
+ parent;
981
+ parent = parent->next, num++)
982
+ ;
983
+ /* with enough slop */
984
+ num = *ofs_p + num * 50 + 20;
985
+ ALLOC_GROW(*buf_p, num, *space_p);
986
+ dst = *buf_p + *ofs_p;
987
+ *ofs_p += add_merge_info(fmt, dst, commit, abbrev);
988
+ parents_shown = 1;
989
+ }
990
+
991
+ /*
992
+ * MEDIUM == DEFAULT shows only author with dates.
993
+ * FULL shows both authors but not dates.
994
+ * FULLER shows both authors and dates.
995
+ */
996
+ if (!memcmp(line, "author ", 7)) {
997
+ len = linelen;
998
+ if (fmt == CMIT_FMT_EMAIL)
999
+ len = bound_rfc2047(linelen, encoding);
1000
+ ALLOC_GROW(*buf_p, *ofs_p + len, *space_p);
1001
+ dst = *buf_p + *ofs_p;
1002
+ *ofs_p += add_user_info("Author", fmt, dst,
1003
+ line + 7, dmode, encoding);
1004
+ }
1005
+
1006
+ if (!memcmp(line, "committer ", 10) &&
1007
+ (fmt == CMIT_FMT_FULL || fmt == CMIT_FMT_FULLER)) {
1008
+ len = linelen;
1009
+ if (fmt == CMIT_FMT_EMAIL)
1010
+ len = bound_rfc2047(linelen, encoding);
1011
+ ALLOC_GROW(*buf_p, *ofs_p + len, *space_p);
1012
+ dst = *buf_p + *ofs_p;
1013
+ *ofs_p += add_user_info("Commit", fmt, dst,
1014
+ line + 10, dmode, encoding);
1015
+ }
1016
+ }
1017
+}
1018
+
1019
+static void pp_title_line(enum cmit_fmt fmt,
1020
+ const char **msg_p,
1021
+ unsigned long *len_p,
1022
+ unsigned long *ofs_p,
1023
+ char **buf_p,
1024
+ unsigned long *space_p,
1025
+ int indent,
1026
+ const char *subject,
1027
+ const char *after_subject,
1028
+ const char *encoding,
1029
+ int plain_non_ascii)
1030
+{
1031
+ char *title;
1032
+ unsigned long title_alloc, title_len;
1033
+ unsigned long len;
1034
+
1035
+ title_len = 0;
1036
+ title_alloc = 80;
1037
+ title = xmalloc(title_alloc);
1038
+ for (;;) {
1039
+ const char *line = *msg_p;
1040
+ int linelen = get_one_line(line, *len_p);
1041
+ *msg_p += linelen;
1042
+ *len_p -= linelen;
1043
+
1044
+ if (!linelen || is_empty_line(line, &linelen))
1045
+ break;
1046
+
1047
+ if (title_alloc <= title_len + linelen + 2) {
1048
+ title_alloc = title_len + linelen + 80;
1049
+ title = xrealloc(title, title_alloc);
1050
+ }
1051
+ len = 0;
1052
+ if (title_len) {
1053
+ if (fmt == CMIT_FMT_EMAIL) {
1054
+ len++;
1055
+ title[title_len++] = '\n';
1056
+ }
1057
+ len++;
1058
+ title[title_len++] = ' ';
1059
+ }
1060
+ memcpy(title + title_len, line, linelen);
1061
+ title_len += linelen;
1062
+ }
1063
+
1064
+ /* Enough slop for the MIME header and rfc2047 */
1065
+ len = bound_rfc2047(title_len, encoding)+ 1000;
1066
+ if (subject)
1067
+ len += strlen(subject);
1068
+ if (after_subject)
1069
+ len += strlen(after_subject);
1070
+ if (encoding)
1071
+ len += strlen(encoding);
1072
+ ALLOC_GROW(*buf_p, title_len + *ofs_p + len, *space_p);
1073
+
1074
+ if (subject) {
1075
+ len = strlen(subject);
1076
+ memcpy(*buf_p + *ofs_p, subject, len);
1077
+ *ofs_p += len;
1078
+ *ofs_p += add_rfc2047(*buf_p + *ofs_p,
1079
+ title, title_len, encoding);
1080
+ } else {
1081
+ memcpy(*buf_p + *ofs_p, title, title_len);
1082
+ *ofs_p += title_len;
1083
+ }
1084
+ (*buf_p)[(*ofs_p)++] = '\n';
1085
+ if (plain_non_ascii) {
1086
+ const char *header_fmt =
1087
+ "MIME-Version: 1.0\n"
1088
+ "Content-Type: text/plain; charset=%s\n"
1089
+ "Content-Transfer-Encoding: 8bit\n";
1090
+ *ofs_p += snprintf(*buf_p + *ofs_p,
1091
+ *space_p - *ofs_p,
1092
+ header_fmt, encoding);
1093
+ }
1094
+ if (after_subject) {
1095
+ len = strlen(after_subject);
1096
+ memcpy(*buf_p + *ofs_p, after_subject, len);
1097
+ *ofs_p += len;
1098
+ }
1099
+ free(title);
1100
+ if (fmt == CMIT_FMT_EMAIL) {
1101
+ ALLOC_GROW(*buf_p, *ofs_p + 20, *space_p);
1102
+ (*buf_p)[(*ofs_p)++] = '\n';
1103
+ }
1104
+}
1105
+
1106
+static void pp_remainder(enum cmit_fmt fmt,
1107
+ const char **msg_p,
1108
+ unsigned long *len_p,
1109
+ unsigned long *ofs_p,
1110
+ char **buf_p,
1111
+ unsigned long *space_p,
1112
+ int indent)
1113
+{
1114
+ int first = 1;
1115
+ for (;;) {
1116
+ const char *line = *msg_p;
1117
+ int linelen = get_one_line(line, *len_p);
1118
+ *msg_p += linelen;
1119
+ *len_p -= linelen;
1120
+
1121
+ if (!linelen)
1122
+ break;
1123
+
1124
+ if (is_empty_line(line, &linelen)) {
1125
+ if (first)
1126
+ continue;
1127
+ if (fmt == CMIT_FMT_SHORT)
1128
+ break;
1129
+ }
1130
+ first = 0;
1131
+
1132
+ ALLOC_GROW(*buf_p, *ofs_p + linelen + indent + 20, *space_p);
1133
+ if (indent) {
1134
+ memset(*buf_p + *ofs_p, ' ', indent);
1135
+ *ofs_p += indent;
1136
+ }
1137
+ memcpy(*buf_p + *ofs_p, line, linelen);
1138
+ *ofs_p += linelen;
1139
+ (*buf_p)[(*ofs_p)++] = '\n';
1140
+ }
1141
+}
1142
+
1143
unsigned long pretty_print_commit(enum cmit_fmt fmt,
1144
const struct commit *commit,
1145
unsigned long len,
1148
const char *after_subject,
1149
enum date_mode dmode)
1150
{
933
- int hdr = 1, body = 0, seen_title = 0;
1151
unsigned long offset = 0;
1152
+ unsigned long beginning_of_body;
1153
int indent = 4;
936
- int parents_shown = 0;
1154
const char *msg = commit->buffer;
1155
int plain_non_ascii = 0;
1156
char *reencoded;
1157
const char *encoding;
1158
char *buf;
942
- unsigned long space, slop;
1159
1160
if (fmt == CMIT_FMT_USERFORMAT)
1161
return format_commit_message(commit, msg, buf_p, space_p);
1166
if (!encoding)
1167
encoding = "utf-8";
1168
reencoded = logmsg_reencode(commit, encoding);
953
- if (reencoded)
1169
+ if (reencoded) {
1170
msg = reencoded;
1171
+ len = strlen(reencoded);
1172
+ }
1173
1174
if (fmt == CMIT_FMT_ONELINE || fmt == CMIT_FMT_EMAIL)
1175
indent = 0;
1200
}
1201
}
1202
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;
1203
+ pp_header(fmt, abbrev, dmode, encoding,
1204
+ commit, &msg, &len,
1205
+ &offset, buf_p, space_p);
1206
+ if (fmt != CMIT_FMT_ONELINE && !subject) {
1207
+ ALLOC_GROW(*buf_p, offset + 20, *space_p);
1208
+ (*buf_p)[offset++] = '\n';
1209
}
1210
1211
+ /* Skip excess blank lines at the beginning of body, if any... */
1212
for (;;) {
1006
- const char *line = msg;
1213
int linelen = get_one_line(msg, len);
1008
-
1214
+ int ll = linelen;
1215
if (!linelen)
1216
break;
1011
-
1012
- /* 20 would cover indent and leave us some slop */
1013
- if (offset + linelen + 20 > space) {
1014
- space = offset + linelen + 20;
1015
- buf = xrealloc(buf, space);
1016
- *buf_p = buf;
1017
- *space_p = space;
1018
- }
1019
-
1217
+ if (!is_empty_line(msg, &ll))
1218
+ break;
1219
msg += linelen;
1220
len -= linelen;
1022
- if (hdr) {
1023
- if (linelen == 1) {
1024
- hdr = 0;
1025
- if ((fmt != CMIT_FMT_ONELINE) && !subject)
1026
- buf[offset++] = '\n';
1027
- continue;
1028
- }
1029
- if (fmt == CMIT_FMT_RAW) {
1030
- memcpy(buf + offset, line, linelen);
1031
- offset += linelen;
1032
- continue;
1033
- }
1034
- if (!memcmp(line, "parent ", 7)) {
1035
- if (linelen != 48)
1036
- die("bad parent line in commit");
1037
- continue;
1038
- }
1039
-
1040
- if (!parents_shown) {
1041
- offset += add_merge_info(fmt, buf + offset,
1042
- commit, abbrev);
1043
- parents_shown = 1;
1044
- continue;
1045
- }
1046
- /*
1047
- * MEDIUM == DEFAULT shows only author with dates.
1048
- * FULL shows both authors but not dates.
1049
- * FULLER shows both authors and dates.
1050
- */
1051
- if (!memcmp(line, "author ", 7))
1052
- offset += add_user_info("Author", fmt,
1053
- buf + offset,
1054
- line + 7,
1055
- dmode,
1056
- encoding);
1057
- if (!memcmp(line, "committer ", 10) &&
1058
- (fmt == CMIT_FMT_FULL || fmt == CMIT_FMT_FULLER))
1059
- offset += add_user_info("Commit", fmt,
1060
- buf + offset,
1061
- line + 10,
1062
- dmode,
1063
- encoding);
1064
- continue;
1065
- }
1221
+ }
1222
1067
- if (!subject)
1068
- body = 1;
1223
+ /* These formats treat the title line specially. */
1224
+ if (fmt == CMIT_FMT_ONELINE
1225
+ || fmt == CMIT_FMT_EMAIL)
1226
+ pp_title_line(fmt, &msg, &len, &offset,
1227
+ buf_p, space_p, indent,
1228
+ subject, after_subject, encoding,
1229
+ plain_non_ascii);
1230
1070
- if (is_empty_line(line, &linelen)) {
1071
- if (!seen_title)
1072
- continue;
1073
- if (!body)
1074
- continue;
1075
- if (subject)
1076
- continue;
1077
- if (fmt == CMIT_FMT_SHORT)
1078
- break;
1079
- }
1231
+ beginning_of_body = offset;
1232
+ if (fmt != CMIT_FMT_ONELINE)
1233
+ pp_remainder(fmt, &msg, &len, &offset,
1234
+ buf_p, space_p, indent);
1235
1081
- seen_title = 1;
1082
- if (subject) {
1083
- int slen = strlen(subject);
1084
- memcpy(buf + offset, subject, slen);
1085
- offset += slen;
1086
- offset += add_rfc2047(buf + offset, line, linelen,
1087
- encoding);
1088
- }
1089
- else {
1090
- memset(buf + offset, ' ', indent);
1091
- memcpy(buf + offset + indent, line, linelen);
1092
- offset += linelen + indent;
1093
- }
1094
- buf[offset++] = '\n';
1095
- if (fmt == CMIT_FMT_ONELINE)
1096
- break;
1097
- if (subject && plain_non_ascii) {
1098
- int sz;
1099
- char header[512];
1100
- const char *header_fmt =
1101
- "MIME-Version: 1.0\n"
1102
- "Content-Type: text/plain; charset=%s\n"
1103
- "Content-Transfer-Encoding: 8bit\n";
1104
- sz = snprintf(header, sizeof(header), header_fmt,
1105
- encoding);
1106
- if (sizeof(header) < sz)
1107
- die("Encoding name %s too long", encoding);
1108
- memcpy(buf + offset, header, sz);
1109
- offset += sz;
1110
- }
1111
- if (after_subject) {
1112
- int slen = strlen(after_subject);
1113
- if (slen > space - offset - 1)
1114
- slen = space - offset - 1;
1115
- memcpy(buf + offset, after_subject, slen);
1116
- offset += slen;
1117
- after_subject = NULL;
1118
- }
1119
- subject = NULL;
1120
- }
1121
- while (offset && isspace(buf[offset-1]))
1236
+ while (offset && isspace((*buf_p)[offset-1]))
1237
offset--;
1238
+
1239
+ ALLOC_GROW(*buf_p, offset + 20, *space_p);
1240
+ buf = *buf_p;
1241
+
1242
/* Make sure there is an EOLN for the non-oneline case */
1243
if (fmt != CMIT_FMT_ONELINE)
1244
buf[offset++] = '\n';
1245
+
1246
/*
1127
- * make sure there is another EOLN to separate the headers from whatever
1128
- * body the caller appends if we haven't already written a body
1247
+ * The caller may append additional body text in e-mail
1248
+ * format. Make sure we did not strip the blank line
1249
+ * between the header and the body.
1250
*/
1130
- if (fmt == CMIT_FMT_EMAIL && !body)
1251
+ if (fmt == CMIT_FMT_EMAIL && offset <= beginning_of_body)
1252
buf[offset++] = '\n';
1253
buf[offset] = '\0';
1133
-
1254
free(reencoded);
1255
return offset;
1256
}