blob: 587101f846b945d7de219f468699e75565a7e9c0 [file] [log] [blame]
Johannes Schindelin93fc05e2007-11-04 19:15:06 +00001#include "cache.h"
2#include "commit.h"
Johannes Schindelin93fc05e2007-11-04 19:15:06 +00003#include "utf8.h"
4#include "diff.h"
5#include "revision.h"
Johannes Schindelinc455c872008-07-21 19:03:49 +01006#include "string-list.h"
Johannes Schindeline0cbc392008-07-12 00:28:18 +01007#include "mailmap.h"
René Scharfe3b3d4432008-09-04 23:40:03 +02008#include "log-tree.h"
Jeff Kingc0029222009-01-17 10:38:46 -05009#include "color.h"
Johannes Schindelin93fc05e2007-11-04 19:15:06 +000010
Johannes Schindelin93fc05e2007-11-04 19:15:06 +000011static char *user_format;
12
Nanako Shiraishi36407542009-02-24 18:59:15 +090013static void save_user_format(struct rev_info *rev, const char *cp, int is_tformat)
14{
15 free(user_format);
16 user_format = xstrdup(cp);
17 if (is_tformat)
18 rev->use_terminator = 1;
19 rev->commit_format = CMIT_FMT_USERFORMAT;
20}
21
Junio C Hamano4da45be2008-04-07 17:11:34 -070022void get_commit_format(const char *arg, struct rev_info *rev)
Johannes Schindelin93fc05e2007-11-04 19:15:06 +000023{
24 int i;
Junio C Hamano4da45be2008-04-07 17:11:34 -070025 static struct cmt_fmt_map {
26 const char *n;
27 size_t cmp_len;
28 enum cmit_fmt v;
29 } cmt_fmts[] = {
30 { "raw", 1, CMIT_FMT_RAW },
31 { "medium", 1, CMIT_FMT_MEDIUM },
32 { "short", 1, CMIT_FMT_SHORT },
33 { "email", 1, CMIT_FMT_EMAIL },
34 { "full", 5, CMIT_FMT_FULL },
35 { "fuller", 5, CMIT_FMT_FULLER },
36 { "oneline", 1, CMIT_FMT_ONELINE },
37 };
Johannes Schindelin93fc05e2007-11-04 19:15:06 +000038
Junio C Hamano4da45be2008-04-07 17:11:34 -070039 rev->use_terminator = 0;
40 if (!arg || !*arg) {
41 rev->commit_format = CMIT_FMT_DEFAULT;
42 return;
43 }
Junio C Hamano4da45be2008-04-07 17:11:34 -070044 if (!prefixcmp(arg, "format:") || !prefixcmp(arg, "tformat:")) {
Nanako Shiraishi36407542009-02-24 18:59:15 +090045 save_user_format(rev, strchr(arg, ':') + 1, arg[0] == 't');
Junio C Hamano4da45be2008-04-07 17:11:34 -070046 return;
Johannes Schindelin93fc05e2007-11-04 19:15:06 +000047 }
48 for (i = 0; i < ARRAY_SIZE(cmt_fmts); i++) {
49 if (!strncmp(arg, cmt_fmts[i].n, cmt_fmts[i].cmp_len) &&
Junio C Hamano4da45be2008-04-07 17:11:34 -070050 !strncmp(arg, cmt_fmts[i].n, strlen(arg))) {
51 if (cmt_fmts[i].v == CMIT_FMT_ONELINE)
52 rev->use_terminator = 1;
53 rev->commit_format = cmt_fmts[i].v;
54 return;
55 }
Johannes Schindelin93fc05e2007-11-04 19:15:06 +000056 }
Nanako Shiraishi36407542009-02-24 18:59:15 +090057 if (strchr(arg, '%')) {
58 save_user_format(rev, arg, 1);
59 return;
60 }
Johannes Schindelin93fc05e2007-11-04 19:15:06 +000061
62 die("invalid --pretty format: %s", arg);
63}
64
65/*
66 * Generic support for pretty-printing the header
67 */
68static int get_one_line(const char *msg)
69{
70 int ret = 0;
71
72 for (;;) {
73 char c = *msg++;
74 if (!c)
75 break;
76 ret++;
77 if (c == '\n')
78 break;
79 }
80 return ret;
81}
82
83/* High bit set, or ISO-2022-INT */
84int non_ascii(int ch)
85{
René Scharfec2e93642009-03-07 14:06:49 +010086 return !isascii(ch) || ch == '\033';
Johannes Schindelin93fc05e2007-11-04 19:15:06 +000087}
88
Johannes Schindelin28e9cf62009-08-10 18:22:18 +020089int has_non_ascii(const char *s)
90{
91 int ch;
92 if (!s)
93 return 0;
94 while ((ch = *s++) != '\0') {
95 if (non_ascii(ch))
96 return 1;
97 }
98 return 0;
99}
100
Johannes Schindelin93fc05e2007-11-04 19:15:06 +0000101static int is_rfc2047_special(char ch)
102{
103 return (non_ascii(ch) || (ch == '=') || (ch == '?') || (ch == '_'));
104}
105
106static void add_rfc2047(struct strbuf *sb, const char *line, int len,
107 const char *encoding)
108{
109 int i, last;
110
111 for (i = 0; i < len; i++) {
112 int ch = line[i];
113 if (non_ascii(ch))
114 goto needquote;
115 if ((i + 1 < len) && (ch == '=' && line[i+1] == '?'))
116 goto needquote;
117 }
118 strbuf_add(sb, line, len);
119 return;
120
121needquote:
122 strbuf_grow(sb, len * 3 + strlen(encoding) + 100);
123 strbuf_addf(sb, "=?%s?q?", encoding);
124 for (i = last = 0; i < len; i++) {
125 unsigned ch = line[i] & 0xFF;
126 /*
127 * We encode ' ' using '=20' even though rfc2047
128 * allows using '_' for readability. Unfortunately,
129 * many programs do not understand this and just
130 * leave the underscore in place.
131 */
132 if (is_rfc2047_special(ch) || ch == ' ') {
133 strbuf_add(sb, line + last, i - last);
134 strbuf_addf(sb, "=%02X", ch);
135 last = i + 1;
136 }
137 }
138 strbuf_add(sb, line + last, len - last);
139 strbuf_addstr(sb, "?=");
140}
141
Daniel Barkalowb02bd652008-02-18 22:56:08 -0500142void pp_user_info(const char *what, enum cmit_fmt fmt, struct strbuf *sb,
143 const char *line, enum date_mode dmode,
144 const char *encoding)
Johannes Schindelin93fc05e2007-11-04 19:15:06 +0000145{
146 char *date;
147 int namelen;
148 unsigned long time;
149 int tz;
Johannes Schindelin93fc05e2007-11-04 19:15:06 +0000150
151 if (fmt == CMIT_FMT_ONELINE)
152 return;
153 date = strchr(line, '>');
154 if (!date)
155 return;
156 namelen = ++date - line;
157 time = strtoul(date, &date, 10);
158 tz = strtol(date, NULL, 10);
159
160 if (fmt == CMIT_FMT_EMAIL) {
161 char *name_tail = strchr(line, '<');
162 int display_name_length;
163 if (!name_tail)
164 return;
165 while (line < name_tail && isspace(name_tail[-1]))
166 name_tail--;
167 display_name_length = name_tail - line;
Johannes Schindelin93fc05e2007-11-04 19:15:06 +0000168 strbuf_addstr(sb, "From: ");
169 add_rfc2047(sb, line, display_name_length, encoding);
170 strbuf_add(sb, name_tail, namelen - display_name_length);
171 strbuf_addch(sb, '\n');
172 } else {
173 strbuf_addf(sb, "%s: %.*s%.*s\n", what,
174 (fmt == CMIT_FMT_FULLER) ? 4 : 0,
Benjamin Kramer8e76bf32009-03-13 13:51:33 +0100175 " ", namelen, line);
Johannes Schindelin93fc05e2007-11-04 19:15:06 +0000176 }
177 switch (fmt) {
178 case CMIT_FMT_MEDIUM:
179 strbuf_addf(sb, "Date: %s\n", show_date(time, tz, dmode));
180 break;
181 case CMIT_FMT_EMAIL:
182 strbuf_addf(sb, "Date: %s\n", show_date(time, tz, DATE_RFC2822));
183 break;
184 case CMIT_FMT_FULLER:
185 strbuf_addf(sb, "%sDate: %s\n", what, show_date(time, tz, dmode));
186 break;
187 default:
188 /* notin' */
189 break;
190 }
191}
192
193static int is_empty_line(const char *line, int *len_p)
194{
195 int len = *len_p;
196 while (len && isspace(line[len-1]))
197 len--;
198 *len_p = len;
199 return !len;
200}
201
René Scharfea0109662008-12-27 01:32:49 +0100202static const char *skip_empty_lines(const char *msg)
203{
204 for (;;) {
205 int linelen = get_one_line(msg);
206 int ll = linelen;
207 if (!linelen)
208 break;
209 if (!is_empty_line(msg, &ll))
210 break;
211 msg += linelen;
212 }
213 return msg;
214}
215
Johannes Schindelin93fc05e2007-11-04 19:15:06 +0000216static void add_merge_info(enum cmit_fmt fmt, struct strbuf *sb,
217 const struct commit *commit, int abbrev)
218{
219 struct commit_list *parent = commit->parents;
220
221 if ((fmt == CMIT_FMT_ONELINE) || (fmt == CMIT_FMT_EMAIL) ||
222 !parent || !parent->next)
223 return;
224
225 strbuf_addstr(sb, "Merge:");
226
227 while (parent) {
228 struct commit *p = parent->item;
229 const char *hex = NULL;
Johannes Schindelin93fc05e2007-11-04 19:15:06 +0000230 if (abbrev)
231 hex = find_unique_abbrev(p->object.sha1, abbrev);
232 if (!hex)
233 hex = sha1_to_hex(p->object.sha1);
Johannes Schindelin93fc05e2007-11-04 19:15:06 +0000234 parent = parent->next;
235
Thomas Rast7fcda922009-02-13 23:10:41 +0100236 strbuf_addf(sb, " %s", hex);
Johannes Schindelin93fc05e2007-11-04 19:15:06 +0000237 }
238 strbuf_addch(sb, '\n');
239}
240
241static char *get_header(const struct commit *commit, const char *key)
242{
243 int key_len = strlen(key);
244 const char *line = commit->buffer;
245
246 for (;;) {
247 const char *eol = strchr(line, '\n'), *next;
248
249 if (line == eol)
250 return NULL;
251 if (!eol) {
252 eol = line + strlen(line);
253 next = NULL;
254 } else
255 next = eol + 1;
256 if (eol - line > key_len &&
257 !strncmp(line, key, key_len) &&
258 line[key_len] == ' ') {
259 return xmemdupz(line + key_len + 1, eol - line - key_len - 1);
260 }
261 line = next;
262 }
263}
264
265static char *replace_encoding_header(char *buf, const char *encoding)
266{
Brandon Caseyf285a2d2008-10-09 14:12:12 -0500267 struct strbuf tmp = STRBUF_INIT;
Johannes Schindelin93fc05e2007-11-04 19:15:06 +0000268 size_t start, len;
269 char *cp = buf;
270
271 /* guess if there is an encoding header before a \n\n */
272 while (strncmp(cp, "encoding ", strlen("encoding "))) {
273 cp = strchr(cp, '\n');
274 if (!cp || *++cp == '\n')
275 return buf;
276 }
277 start = cp - buf;
278 cp = strchr(cp, '\n');
279 if (!cp)
280 return buf; /* should not happen but be defensive */
281 len = cp + 1 - (buf + start);
282
Johannes Schindelin93fc05e2007-11-04 19:15:06 +0000283 strbuf_attach(&tmp, buf, strlen(buf), strlen(buf) + 1);
284 if (is_encoding_utf8(encoding)) {
285 /* we have re-coded to UTF-8; drop the header */
286 strbuf_remove(&tmp, start, len);
287 } else {
288 /* just replaces XXXX in 'encoding XXXX\n' */
289 strbuf_splice(&tmp, start + strlen("encoding "),
290 len - strlen("encoding \n"),
291 encoding, strlen(encoding));
292 }
293 return strbuf_detach(&tmp, NULL);
294}
295
296static char *logmsg_reencode(const struct commit *commit,
297 const char *output_encoding)
298{
Brandon Casey330db182009-05-18 18:44:39 -0500299 static const char *utf8 = "UTF-8";
Johannes Schindelin93fc05e2007-11-04 19:15:06 +0000300 const char *use_encoding;
301 char *encoding;
302 char *out;
303
304 if (!*output_encoding)
305 return NULL;
306 encoding = get_header(commit, "encoding");
307 use_encoding = encoding ? encoding : utf8;
308 if (!strcmp(use_encoding, output_encoding))
309 if (encoding) /* we'll strip encoding header later */
310 out = xstrdup(commit->buffer);
311 else
312 return NULL; /* nothing to do */
313 else
314 out = reencode_string(commit->buffer,
315 output_encoding, use_encoding);
316 if (out)
317 out = replace_encoding_header(out, output_encoding);
318
319 free(encoding);
320 return out;
321}
322
Marius Storm-Olsend20d6542009-02-08 15:34:30 +0100323static int mailmap_name(char *email, int email_len, char *name, int name_len)
Johannes Schindeline0cbc392008-07-12 00:28:18 +0100324{
Johannes Schindelinc455c872008-07-21 19:03:49 +0100325 static struct string_list *mail_map;
Johannes Schindeline0cbc392008-07-12 00:28:18 +0100326 if (!mail_map) {
327 mail_map = xcalloc(1, sizeof(*mail_map));
Marius Storm-Olsend551a482009-02-08 15:34:27 +0100328 read_mailmap(mail_map, NULL);
Johannes Schindeline0cbc392008-07-12 00:28:18 +0100329 }
Marius Storm-Olsend20d6542009-02-08 15:34:30 +0100330 return mail_map->nr && map_user(mail_map, email, email_len, name, name_len);
Johannes Schindeline0cbc392008-07-12 00:28:18 +0100331}
332
Marco Costalbac3a670d2008-02-09 15:40:19 +0100333static size_t format_person_part(struct strbuf *sb, char part,
Jeff Kingd36f8672008-08-28 20:54:59 -0400334 const char *msg, int len, enum date_mode dmode)
Johannes Schindelin93fc05e2007-11-04 19:15:06 +0000335{
Marco Costalbac3a670d2008-02-09 15:40:19 +0100336 /* currently all placeholders have same length */
337 const int placeholder_len = 2;
Johannes Schindelin93fc05e2007-11-04 19:15:06 +0000338 int start, end, tz = 0;
Marco Costalbac3a670d2008-02-09 15:40:19 +0100339 unsigned long date = 0;
Johannes Schindelin93fc05e2007-11-04 19:15:06 +0000340 char *ep;
Marius Storm-Olsend20d6542009-02-08 15:34:30 +0100341 const char *name_start, *name_end, *mail_start, *mail_end, *msg_end = msg+len;
342 char person_name[1024];
343 char person_mail[1024];
Johannes Schindelin93fc05e2007-11-04 19:15:06 +0000344
Marco Costalbac3a670d2008-02-09 15:40:19 +0100345 /* advance 'end' to point to email start delimiter */
Johannes Schindelin93fc05e2007-11-04 19:15:06 +0000346 for (end = 0; end < len && msg[end] != '<'; end++)
347 ; /* do nothing */
Johannes Schindelin93fc05e2007-11-04 19:15:06 +0000348
Marco Costalbac3a670d2008-02-09 15:40:19 +0100349 /*
350 * When end points at the '<' that we found, it should have
351 * matching '>' later, which means 'end' must be strictly
352 * below len - 1.
353 */
354 if (end >= len - 2)
355 goto skip;
356
Marius Storm-Olsend20d6542009-02-08 15:34:30 +0100357 /* Seek for both name and email part */
358 name_start = msg;
359 name_end = msg+end;
360 while (name_end > name_start && isspace(*(name_end-1)))
361 name_end--;
362 mail_start = msg+end+1;
363 mail_end = mail_start;
364 while (mail_end < msg_end && *mail_end != '>')
365 mail_end++;
366 if (mail_end == msg_end)
367 goto skip;
368 end = mail_end-msg;
369
370 if (part == 'N' || part == 'E') { /* mailmap lookup */
371 strlcpy(person_name, name_start, name_end-name_start+1);
372 strlcpy(person_mail, mail_start, mail_end-mail_start+1);
373 mailmap_name(person_mail, sizeof(person_mail), person_name, sizeof(person_name));
374 name_start = person_name;
375 name_end = name_start + strlen(person_name);
376 mail_start = person_mail;
377 mail_end = mail_start + strlen(person_mail);
378 }
Johannes Schindeline0cbc392008-07-12 00:28:18 +0100379 if (part == 'n' || part == 'N') { /* name */
Marius Storm-Olsend20d6542009-02-08 15:34:30 +0100380 strbuf_add(sb, name_start, name_end-name_start);
Marco Costalbac3a670d2008-02-09 15:40:19 +0100381 return placeholder_len;
382 }
Marius Storm-Olsend20d6542009-02-08 15:34:30 +0100383 if (part == 'e' || part == 'E') { /* email */
384 strbuf_add(sb, mail_start, mail_end-mail_start);
Marco Costalbac3a670d2008-02-09 15:40:19 +0100385 return placeholder_len;
René Scharfecde75e52007-11-09 01:49:42 +0100386 }
Johannes Schindelin93fc05e2007-11-04 19:15:06 +0000387
Marco Costalbac3a670d2008-02-09 15:40:19 +0100388 /* advance 'start' to point to date start delimiter */
Johannes Schindelin93fc05e2007-11-04 19:15:06 +0000389 for (start = end + 1; start < len && isspace(msg[start]); start++)
390 ; /* do nothing */
391 if (start >= len)
Marco Costalbac3a670d2008-02-09 15:40:19 +0100392 goto skip;
Johannes Schindelin93fc05e2007-11-04 19:15:06 +0000393 date = strtoul(msg + start, &ep, 10);
394 if (msg + start == ep)
Marco Costalbac3a670d2008-02-09 15:40:19 +0100395 goto skip;
Johannes Schindelin93fc05e2007-11-04 19:15:06 +0000396
René Scharfecde75e52007-11-09 01:49:42 +0100397 if (part == 't') { /* date, UNIX timestamp */
398 strbuf_add(sb, msg + start, ep - (msg + start));
Marco Costalbac3a670d2008-02-09 15:40:19 +0100399 return placeholder_len;
René Scharfecde75e52007-11-09 01:49:42 +0100400 }
Johannes Schindelin93fc05e2007-11-04 19:15:06 +0000401
402 /* parse tz */
403 for (start = ep - msg + 1; start < len && isspace(msg[start]); start++)
404 ; /* do nothing */
405 if (start + 1 < len) {
406 tz = strtoul(msg + start + 1, NULL, 10);
407 if (msg[start] == '-')
408 tz = -tz;
409 }
410
René Scharfecde75e52007-11-09 01:49:42 +0100411 switch (part) {
412 case 'd': /* date */
Jeff Kingd36f8672008-08-28 20:54:59 -0400413 strbuf_addstr(sb, show_date(date, tz, dmode));
Marco Costalbac3a670d2008-02-09 15:40:19 +0100414 return placeholder_len;
René Scharfecde75e52007-11-09 01:49:42 +0100415 case 'D': /* date, RFC2822 style */
416 strbuf_addstr(sb, show_date(date, tz, DATE_RFC2822));
Marco Costalbac3a670d2008-02-09 15:40:19 +0100417 return placeholder_len;
René Scharfecde75e52007-11-09 01:49:42 +0100418 case 'r': /* date, relative */
419 strbuf_addstr(sb, show_date(date, tz, DATE_RELATIVE));
Marco Costalbac3a670d2008-02-09 15:40:19 +0100420 return placeholder_len;
René Scharfecde75e52007-11-09 01:49:42 +0100421 case 'i': /* date, ISO 8601 */
422 strbuf_addstr(sb, show_date(date, tz, DATE_ISO8601));
Marco Costalbac3a670d2008-02-09 15:40:19 +0100423 return placeholder_len;
René Scharfecde75e52007-11-09 01:49:42 +0100424 }
Marco Costalbac3a670d2008-02-09 15:40:19 +0100425
426skip:
427 /*
428 * bogus commit, 'sb' cannot be updated, but we still need to
429 * compute a valid return value.
430 */
431 if (part == 'n' || part == 'e' || part == 't' || part == 'd'
432 || part == 'D' || part == 'r' || part == 'i')
433 return placeholder_len;
434
435 return 0; /* unknown placeholder */
Johannes Schindelin93fc05e2007-11-04 19:15:06 +0000436}
437
René Scharfef29d5952007-11-10 12:14:20 +0100438struct chunk {
439 size_t off;
440 size_t len;
441};
442
443struct format_commit_context {
444 const struct commit *commit;
Jeff Kingd36f8672008-08-28 20:54:59 -0400445 enum date_mode dmode;
René Scharfef53bd742008-12-27 01:49:21 +0100446 unsigned commit_header_parsed:1;
447 unsigned commit_message_parsed:1;
René Scharfef29d5952007-11-10 12:14:20 +0100448
449 /* These offsets are relative to the start of the commit message. */
René Scharfef29d5952007-11-10 12:14:20 +0100450 struct chunk author;
451 struct chunk committer;
452 struct chunk encoding;
René Scharfef53bd742008-12-27 01:49:21 +0100453 size_t message_off;
454 size_t subject_off;
René Scharfef29d5952007-11-10 12:14:20 +0100455 size_t body_off;
René Scharfeb9c62322007-11-10 12:18:26 +0100456
457 /* The following ones are relative to the result struct strbuf. */
458 struct chunk abbrev_commit_hash;
459 struct chunk abbrev_tree_hash;
460 struct chunk abbrev_parent_hashes;
René Scharfef29d5952007-11-10 12:14:20 +0100461};
462
René Scharfeb9c62322007-11-10 12:18:26 +0100463static int add_again(struct strbuf *sb, struct chunk *chunk)
464{
465 if (chunk->len) {
466 strbuf_adddup(sb, chunk->off, chunk->len);
467 return 1;
468 }
469
470 /*
471 * We haven't seen this chunk before. Our caller is surely
472 * going to add it the hard way now. Remember the most likely
473 * start of the to-be-added chunk: the current end of the
474 * struct strbuf.
475 */
476 chunk->off = sb->len;
477 return 0;
478}
479
René Scharfef29d5952007-11-10 12:14:20 +0100480static void parse_commit_header(struct format_commit_context *context)
481{
482 const char *msg = context->commit->buffer;
483 int i;
René Scharfef29d5952007-11-10 12:14:20 +0100484
René Scharfef53bd742008-12-27 01:49:21 +0100485 for (i = 0; msg[i]; i++) {
René Scharfef29d5952007-11-10 12:14:20 +0100486 int eol;
487 for (eol = i; msg[eol] && msg[eol] != '\n'; eol++)
488 ; /* do nothing */
489
René Scharfef29d5952007-11-10 12:14:20 +0100490 if (i == eol) {
René Scharfef53bd742008-12-27 01:49:21 +0100491 break;
René Scharfef29d5952007-11-10 12:14:20 +0100492 } else if (!prefixcmp(msg + i, "author ")) {
493 context->author.off = i + 7;
494 context->author.len = eol - i - 7;
495 } else if (!prefixcmp(msg + i, "committer ")) {
496 context->committer.off = i + 10;
497 context->committer.len = eol - i - 10;
498 } else if (!prefixcmp(msg + i, "encoding ")) {
499 context->encoding.off = i + 9;
500 context->encoding.len = eol - i - 9;
501 }
502 i = eol;
503 }
René Scharfef53bd742008-12-27 01:49:21 +0100504 context->message_off = i;
René Scharfef29d5952007-11-10 12:14:20 +0100505 context->commit_header_parsed = 1;
506}
507
Stephen Boyd46d164b2009-03-22 19:14:01 -0700508static int istitlechar(char c)
509{
510 return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') ||
511 (c >= '0' && c <= '9') || c == '.' || c == '_';
512}
513
514static void format_sanitized_subject(struct strbuf *sb, const char *msg)
515{
516 size_t trimlen;
Stephen Boyd871d21d2009-03-31 16:24:38 -0700517 size_t start_len = sb->len;
Stephen Boyd46d164b2009-03-22 19:14:01 -0700518 int space = 2;
519
520 for (; *msg && *msg != '\n'; msg++) {
521 if (istitlechar(*msg)) {
522 if (space == 1)
523 strbuf_addch(sb, '-');
524 space = 0;
525 strbuf_addch(sb, *msg);
526 if (*msg == '.')
527 while (*(msg+1) == '.')
528 msg++;
529 } else
530 space |= 1;
531 }
532
533 /* trim any trailing '.' or '-' characters */
534 trimlen = 0;
Stephen Boyd871d21d2009-03-31 16:24:38 -0700535 while (sb->len - trimlen > start_len &&
536 (sb->buf[sb->len - 1 - trimlen] == '.'
537 || sb->buf[sb->len - 1 - trimlen] == '-'))
Stephen Boyd46d164b2009-03-22 19:14:01 -0700538 trimlen++;
539 strbuf_remove(sb, sb->len - trimlen, trimlen);
540}
541
René Scharfecec08712009-01-06 21:41:06 +0100542const char *format_subject(struct strbuf *sb, const char *msg,
543 const char *line_separator)
René Scharfe88c44732008-12-27 01:39:35 +0100544{
545 int first = 1;
546
547 for (;;) {
548 const char *line = msg;
549 int linelen = get_one_line(line);
550
551 msg += linelen;
552 if (!linelen || is_empty_line(line, &linelen))
553 break;
554
René Scharfef53bd742008-12-27 01:49:21 +0100555 if (!sb)
556 continue;
René Scharfe88c44732008-12-27 01:39:35 +0100557 strbuf_grow(sb, linelen + 2);
558 if (!first)
559 strbuf_addstr(sb, line_separator);
560 strbuf_add(sb, line, linelen);
561 first = 0;
562 }
563 return msg;
564}
565
René Scharfef53bd742008-12-27 01:49:21 +0100566static void parse_commit_message(struct format_commit_context *c)
567{
568 const char *msg = c->commit->buffer + c->message_off;
569 const char *start = c->commit->buffer;
570
571 msg = skip_empty_lines(msg);
572 c->subject_off = msg - start;
573
574 msg = format_subject(NULL, msg, NULL);
575 msg = skip_empty_lines(msg);
576 c->body_off = msg - start;
577
578 c->commit_message_parsed = 1;
579}
580
René Scharfe3b3d4432008-09-04 23:40:03 +0200581static void format_decoration(struct strbuf *sb, const struct commit *commit)
582{
583 struct name_decoration *d;
584 const char *prefix = " (";
585
Lars Hjemli33e70182009-08-15 16:23:12 +0200586 load_ref_decorations(DECORATE_SHORT_REFS);
René Scharfe3b3d4432008-09-04 23:40:03 +0200587 d = lookup_decoration(&name_decoration, &commit->object);
588 while (d) {
589 strbuf_addstr(sb, prefix);
590 prefix = ", ";
591 strbuf_addstr(sb, d->name);
592 d = d->next;
593 }
594 if (prefix[0] == ',')
595 strbuf_addch(sb, ')');
596}
597
Marco Costalbac3a670d2008-02-09 15:40:19 +0100598static size_t format_commit_item(struct strbuf *sb, const char *placeholder,
René Scharfecde75e52007-11-09 01:49:42 +0100599 void *context)
Johannes Schindelin93fc05e2007-11-04 19:15:06 +0000600{
René Scharfef29d5952007-11-10 12:14:20 +0100601 struct format_commit_context *c = context;
602 const struct commit *commit = c->commit;
Johannes Schindelin93fc05e2007-11-04 19:15:06 +0000603 const char *msg = commit->buffer;
René Scharfef29d5952007-11-10 12:14:20 +0100604 struct commit_list *p;
Govind Salinas42c8c742008-03-21 10:05:06 -0500605 int h1, h2;
Johannes Schindelin93fc05e2007-11-04 19:15:06 +0000606
Johannes Schindelin93fc05e2007-11-04 19:15:06 +0000607 /* these are independent of the commit */
René Scharfecde75e52007-11-09 01:49:42 +0100608 switch (placeholder[0]) {
609 case 'C':
Jeff Kingc0029222009-01-17 10:38:46 -0500610 if (placeholder[1] == '(') {
611 const char *end = strchr(placeholder + 2, ')');
612 char color[COLOR_MAXLEN];
613 if (!end)
614 return 0;
615 color_parse_mem(placeholder + 2,
616 end - (placeholder + 2),
617 "--pretty format", color);
618 strbuf_addstr(sb, color);
619 return end - placeholder + 1;
620 }
Marco Costalbac3a670d2008-02-09 15:40:19 +0100621 if (!prefixcmp(placeholder + 1, "red")) {
Arjen Laarhovendc6ebd42009-02-13 22:53:40 +0100622 strbuf_addstr(sb, GIT_COLOR_RED);
Marco Costalbac3a670d2008-02-09 15:40:19 +0100623 return 4;
624 } else if (!prefixcmp(placeholder + 1, "green")) {
Arjen Laarhovendc6ebd42009-02-13 22:53:40 +0100625 strbuf_addstr(sb, GIT_COLOR_GREEN);
Marco Costalbac3a670d2008-02-09 15:40:19 +0100626 return 6;
627 } else if (!prefixcmp(placeholder + 1, "blue")) {
Arjen Laarhovendc6ebd42009-02-13 22:53:40 +0100628 strbuf_addstr(sb, GIT_COLOR_BLUE);
Marco Costalbac3a670d2008-02-09 15:40:19 +0100629 return 5;
630 } else if (!prefixcmp(placeholder + 1, "reset")) {
Arjen Laarhovendc6ebd42009-02-13 22:53:40 +0100631 strbuf_addstr(sb, GIT_COLOR_RESET);
Marco Costalbac3a670d2008-02-09 15:40:19 +0100632 return 6;
633 } else
634 return 0;
René Scharfecde75e52007-11-09 01:49:42 +0100635 case 'n': /* newline */
636 strbuf_addch(sb, '\n');
Marco Costalbac3a670d2008-02-09 15:40:19 +0100637 return 1;
Govind Salinas42c8c742008-03-21 10:05:06 -0500638 case 'x':
639 /* %x00 == NUL, %x0a == LF, etc. */
640 if (0 <= (h1 = hexval_table[0xff & placeholder[1]]) &&
641 h1 <= 16 &&
642 0 <= (h2 = hexval_table[0xff & placeholder[2]]) &&
643 h2 <= 16) {
644 strbuf_addch(sb, (h1<<4)|h2);
645 return 3;
646 } else
647 return 0;
René Scharfecde75e52007-11-09 01:49:42 +0100648 }
Johannes Schindelin93fc05e2007-11-04 19:15:06 +0000649
650 /* these depend on the commit */
651 if (!commit->object.parsed)
652 parse_object(commit->object.sha1);
Johannes Schindelin93fc05e2007-11-04 19:15:06 +0000653
René Scharfecde75e52007-11-09 01:49:42 +0100654 switch (placeholder[0]) {
655 case 'H': /* commit hash */
656 strbuf_addstr(sb, sha1_to_hex(commit->object.sha1));
Marco Costalbac3a670d2008-02-09 15:40:19 +0100657 return 1;
René Scharfecde75e52007-11-09 01:49:42 +0100658 case 'h': /* abbreviated commit hash */
René Scharfeb9c62322007-11-10 12:18:26 +0100659 if (add_again(sb, &c->abbrev_commit_hash))
Marco Costalbac3a670d2008-02-09 15:40:19 +0100660 return 1;
René Scharfecde75e52007-11-09 01:49:42 +0100661 strbuf_addstr(sb, find_unique_abbrev(commit->object.sha1,
662 DEFAULT_ABBREV));
René Scharfeb9c62322007-11-10 12:18:26 +0100663 c->abbrev_commit_hash.len = sb->len - c->abbrev_commit_hash.off;
Marco Costalbac3a670d2008-02-09 15:40:19 +0100664 return 1;
René Scharfecde75e52007-11-09 01:49:42 +0100665 case 'T': /* tree hash */
666 strbuf_addstr(sb, sha1_to_hex(commit->tree->object.sha1));
Marco Costalbac3a670d2008-02-09 15:40:19 +0100667 return 1;
René Scharfecde75e52007-11-09 01:49:42 +0100668 case 't': /* abbreviated tree hash */
René Scharfeb9c62322007-11-10 12:18:26 +0100669 if (add_again(sb, &c->abbrev_tree_hash))
Marco Costalbac3a670d2008-02-09 15:40:19 +0100670 return 1;
René Scharfecde75e52007-11-09 01:49:42 +0100671 strbuf_addstr(sb, find_unique_abbrev(commit->tree->object.sha1,
672 DEFAULT_ABBREV));
René Scharfeb9c62322007-11-10 12:18:26 +0100673 c->abbrev_tree_hash.len = sb->len - c->abbrev_tree_hash.off;
Marco Costalbac3a670d2008-02-09 15:40:19 +0100674 return 1;
René Scharfecde75e52007-11-09 01:49:42 +0100675 case 'P': /* parent hashes */
676 for (p = commit->parents; p; p = p->next) {
677 if (p != commit->parents)
678 strbuf_addch(sb, ' ');
679 strbuf_addstr(sb, sha1_to_hex(p->item->object.sha1));
680 }
Marco Costalbac3a670d2008-02-09 15:40:19 +0100681 return 1;
René Scharfecde75e52007-11-09 01:49:42 +0100682 case 'p': /* abbreviated parent hashes */
René Scharfeb9c62322007-11-10 12:18:26 +0100683 if (add_again(sb, &c->abbrev_parent_hashes))
Marco Costalbac3a670d2008-02-09 15:40:19 +0100684 return 1;
René Scharfecde75e52007-11-09 01:49:42 +0100685 for (p = commit->parents; p; p = p->next) {
686 if (p != commit->parents)
687 strbuf_addch(sb, ' ');
688 strbuf_addstr(sb, find_unique_abbrev(
689 p->item->object.sha1, DEFAULT_ABBREV));
690 }
René Scharfeb9c62322007-11-10 12:18:26 +0100691 c->abbrev_parent_hashes.len = sb->len -
692 c->abbrev_parent_hashes.off;
Marco Costalbac3a670d2008-02-09 15:40:19 +0100693 return 1;
René Scharfecde75e52007-11-09 01:49:42 +0100694 case 'm': /* left/right/bottom */
695 strbuf_addch(sb, (commit->object.flags & BOUNDARY)
696 ? '-'
697 : (commit->object.flags & SYMMETRIC_LEFT)
698 ? '<'
699 : '>');
Marco Costalbac3a670d2008-02-09 15:40:19 +0100700 return 1;
René Scharfe3b3d4432008-09-04 23:40:03 +0200701 case 'd':
702 format_decoration(sb, commit);
703 return 1;
René Scharfecde75e52007-11-09 01:49:42 +0100704 }
Johannes Schindelin93fc05e2007-11-04 19:15:06 +0000705
René Scharfecde75e52007-11-09 01:49:42 +0100706 /* For the rest we have to parse the commit header. */
René Scharfef29d5952007-11-10 12:14:20 +0100707 if (!c->commit_header_parsed)
708 parse_commit_header(c);
Johannes Schindelin93fc05e2007-11-04 19:15:06 +0000709
René Scharfef29d5952007-11-10 12:14:20 +0100710 switch (placeholder[0]) {
Marco Costalbac3a670d2008-02-09 15:40:19 +0100711 case 'a': /* author ... */
712 return format_person_part(sb, placeholder[1],
Jeff Kingd36f8672008-08-28 20:54:59 -0400713 msg + c->author.off, c->author.len,
714 c->dmode);
Marco Costalbac3a670d2008-02-09 15:40:19 +0100715 case 'c': /* committer ... */
716 return format_person_part(sb, placeholder[1],
Jeff Kingd36f8672008-08-28 20:54:59 -0400717 msg + c->committer.off, c->committer.len,
718 c->dmode);
Marco Costalbac3a670d2008-02-09 15:40:19 +0100719 case 'e': /* encoding */
René Scharfef29d5952007-11-10 12:14:20 +0100720 strbuf_add(sb, msg + c->encoding.off, c->encoding.len);
Marco Costalbac3a670d2008-02-09 15:40:19 +0100721 return 1;
René Scharfef53bd742008-12-27 01:49:21 +0100722 }
723
724 /* Now we need to parse the commit message. */
725 if (!c->commit_message_parsed)
726 parse_commit_message(c);
727
728 switch (placeholder[0]) {
729 case 's': /* subject */
730 format_subject(sb, msg + c->subject_off, " ");
731 return 1;
Stephen Boyd46d164b2009-03-22 19:14:01 -0700732 case 'f': /* sanitized subject */
733 format_sanitized_subject(sb, msg + c->subject_off);
734 return 1;
Marco Costalbac3a670d2008-02-09 15:40:19 +0100735 case 'b': /* body */
René Scharfef29d5952007-11-10 12:14:20 +0100736 strbuf_addstr(sb, msg + c->body_off);
Marco Costalbac3a670d2008-02-09 15:40:19 +0100737 return 1;
Johannes Schindelin93fc05e2007-11-04 19:15:06 +0000738 }
Marco Costalbac3a670d2008-02-09 15:40:19 +0100739 return 0; /* unknown placeholder */
René Scharfecde75e52007-11-09 01:49:42 +0100740}
Johannes Schindelin93fc05e2007-11-04 19:15:06 +0000741
René Scharfecde75e52007-11-09 01:49:42 +0100742void format_commit_message(const struct commit *commit,
Junio C Hamano7f98ebc2009-10-15 22:59:41 -0700743 const char *format, struct strbuf *sb,
Jeff Kingd36f8672008-08-28 20:54:59 -0400744 enum date_mode dmode)
René Scharfecde75e52007-11-09 01:49:42 +0100745{
René Scharfef29d5952007-11-10 12:14:20 +0100746 struct format_commit_context context;
747
748 memset(&context, 0, sizeof(context));
749 context.commit = commit;
Jeff Kingd36f8672008-08-28 20:54:59 -0400750 context.dmode = dmode;
Marco Costalbac3a670d2008-02-09 15:40:19 +0100751 strbuf_expand(sb, format, format_commit_item, &context);
Johannes Schindelin93fc05e2007-11-04 19:15:06 +0000752}
753
754static void pp_header(enum cmit_fmt fmt,
755 int abbrev,
756 enum date_mode dmode,
757 const char *encoding,
758 const struct commit *commit,
759 const char **msg_p,
760 struct strbuf *sb)
761{
762 int parents_shown = 0;
763
764 for (;;) {
765 const char *line = *msg_p;
766 int linelen = get_one_line(*msg_p);
767
768 if (!linelen)
769 return;
770 *msg_p += linelen;
771
772 if (linelen == 1)
773 /* End of header */
774 return;
775
776 if (fmt == CMIT_FMT_RAW) {
777 strbuf_add(sb, line, linelen);
778 continue;
779 }
780
781 if (!memcmp(line, "parent ", 7)) {
782 if (linelen != 48)
783 die("bad parent line in commit");
784 continue;
785 }
786
787 if (!parents_shown) {
788 struct commit_list *parent;
789 int num;
790 for (parent = commit->parents, num = 0;
791 parent;
792 parent = parent->next, num++)
793 ;
794 /* with enough slop */
795 strbuf_grow(sb, num * 50 + 20);
796 add_merge_info(fmt, sb, commit, abbrev);
797 parents_shown = 1;
798 }
799
800 /*
801 * MEDIUM == DEFAULT shows only author with dates.
802 * FULL shows both authors but not dates.
803 * FULLER shows both authors and dates.
804 */
805 if (!memcmp(line, "author ", 7)) {
806 strbuf_grow(sb, linelen + 80);
Daniel Barkalowb02bd652008-02-18 22:56:08 -0500807 pp_user_info("Author", fmt, sb, line + 7, dmode, encoding);
Johannes Schindelin93fc05e2007-11-04 19:15:06 +0000808 }
809 if (!memcmp(line, "committer ", 10) &&
810 (fmt == CMIT_FMT_FULL || fmt == CMIT_FMT_FULLER)) {
811 strbuf_grow(sb, linelen + 80);
Daniel Barkalowb02bd652008-02-18 22:56:08 -0500812 pp_user_info("Commit", fmt, sb, line + 10, dmode, encoding);
Johannes Schindelin93fc05e2007-11-04 19:15:06 +0000813 }
814 }
815}
816
Daniel Barkalowb02bd652008-02-18 22:56:08 -0500817void pp_title_line(enum cmit_fmt fmt,
818 const char **msg_p,
819 struct strbuf *sb,
820 const char *subject,
821 const char *after_subject,
822 const char *encoding,
Junio C Hamano267123b2008-03-15 00:09:20 -0700823 int need_8bit_cte)
Johannes Schindelin93fc05e2007-11-04 19:15:06 +0000824{
René Scharfe88c44732008-12-27 01:39:35 +0100825 const char *line_separator = (fmt == CMIT_FMT_EMAIL) ? "\n " : " ";
Johannes Schindelin93fc05e2007-11-04 19:15:06 +0000826 struct strbuf title;
827
828 strbuf_init(&title, 80);
René Scharfe88c44732008-12-27 01:39:35 +0100829 *msg_p = format_subject(&title, *msg_p, line_separator);
Johannes Schindelin93fc05e2007-11-04 19:15:06 +0000830
831 strbuf_grow(sb, title.len + 1024);
832 if (subject) {
833 strbuf_addstr(sb, subject);
834 add_rfc2047(sb, title.buf, title.len, encoding);
835 } else {
836 strbuf_addbuf(sb, &title);
837 }
838 strbuf_addch(sb, '\n');
839
Junio C Hamano6bf4f1b2008-03-14 17:10:09 -0700840 if (need_8bit_cte > 0) {
Johannes Schindelin93fc05e2007-11-04 19:15:06 +0000841 const char *header_fmt =
842 "MIME-Version: 1.0\n"
843 "Content-Type: text/plain; charset=%s\n"
844 "Content-Transfer-Encoding: 8bit\n";
845 strbuf_addf(sb, header_fmt, encoding);
846 }
847 if (after_subject) {
848 strbuf_addstr(sb, after_subject);
849 }
850 if (fmt == CMIT_FMT_EMAIL) {
851 strbuf_addch(sb, '\n');
852 }
853 strbuf_release(&title);
854}
855
Daniel Barkalowb02bd652008-02-18 22:56:08 -0500856void pp_remainder(enum cmit_fmt fmt,
857 const char **msg_p,
858 struct strbuf *sb,
859 int indent)
Johannes Schindelin93fc05e2007-11-04 19:15:06 +0000860{
861 int first = 1;
862 for (;;) {
863 const char *line = *msg_p;
864 int linelen = get_one_line(line);
865 *msg_p += linelen;
866
867 if (!linelen)
868 break;
869
870 if (is_empty_line(line, &linelen)) {
871 if (first)
872 continue;
873 if (fmt == CMIT_FMT_SHORT)
874 break;
875 }
876 first = 0;
877
878 strbuf_grow(sb, linelen + indent + 20);
879 if (indent) {
880 memset(sb->buf + sb->len, ' ', indent);
881 strbuf_setlen(sb, sb->len + indent);
882 }
883 strbuf_add(sb, line, linelen);
884 strbuf_addch(sb, '\n');
885 }
886}
887
Alexander Gavrilov69cd8f62008-10-22 00:55:57 +0400888char *reencode_commit_message(const struct commit *commit, const char **encoding_p)
889{
890 const char *encoding;
891
892 encoding = (git_log_output_encoding
893 ? git_log_output_encoding
894 : git_commit_encoding);
895 if (!encoding)
Brandon Casey330db182009-05-18 18:44:39 -0500896 encoding = "UTF-8";
Alexander Gavrilov69cd8f62008-10-22 00:55:57 +0400897 if (encoding_p)
898 *encoding_p = encoding;
899 return logmsg_reencode(commit, encoding);
900}
901
Johannes Schindelin93fc05e2007-11-04 19:15:06 +0000902void pretty_print_commit(enum cmit_fmt fmt, const struct commit *commit,
Junio C Hamano6bf4f1b2008-03-14 17:10:09 -0700903 struct strbuf *sb, int abbrev,
904 const char *subject, const char *after_subject,
905 enum date_mode dmode, int need_8bit_cte)
Johannes Schindelin93fc05e2007-11-04 19:15:06 +0000906{
907 unsigned long beginning_of_body;
908 int indent = 4;
909 const char *msg = commit->buffer;
910 char *reencoded;
911 const char *encoding;
912
913 if (fmt == CMIT_FMT_USERFORMAT) {
Jeff Kingd36f8672008-08-28 20:54:59 -0400914 format_commit_message(commit, user_format, sb, dmode);
Johannes Schindelin93fc05e2007-11-04 19:15:06 +0000915 return;
916 }
917
Alexander Gavrilov69cd8f62008-10-22 00:55:57 +0400918 reencoded = reencode_commit_message(commit, &encoding);
Johannes Schindelin93fc05e2007-11-04 19:15:06 +0000919 if (reencoded) {
920 msg = reencoded;
921 }
922
923 if (fmt == CMIT_FMT_ONELINE || fmt == CMIT_FMT_EMAIL)
924 indent = 0;
925
Junio C Hamano6bf4f1b2008-03-14 17:10:09 -0700926 /*
927 * We need to check and emit Content-type: to mark it
928 * as 8-bit if we haven't done so.
Johannes Schindelin93fc05e2007-11-04 19:15:06 +0000929 */
Junio C Hamano6bf4f1b2008-03-14 17:10:09 -0700930 if (fmt == CMIT_FMT_EMAIL && need_8bit_cte == 0) {
Johannes Schindelin93fc05e2007-11-04 19:15:06 +0000931 int i, ch, in_body;
932
933 for (in_body = i = 0; (ch = msg[i]); i++) {
934 if (!in_body) {
935 /* author could be non 7-bit ASCII but
936 * the log may be so; skip over the
937 * header part first.
938 */
939 if (ch == '\n' && msg[i+1] == '\n')
940 in_body = 1;
941 }
942 else if (non_ascii(ch)) {
Junio C Hamano6bf4f1b2008-03-14 17:10:09 -0700943 need_8bit_cte = 1;
Johannes Schindelin93fc05e2007-11-04 19:15:06 +0000944 break;
945 }
946 }
947 }
948
949 pp_header(fmt, abbrev, dmode, encoding, commit, &msg, sb);
950 if (fmt != CMIT_FMT_ONELINE && !subject) {
951 strbuf_addch(sb, '\n');
952 }
953
954 /* Skip excess blank lines at the beginning of body, if any... */
René Scharfea0109662008-12-27 01:32:49 +0100955 msg = skip_empty_lines(msg);
Johannes Schindelin93fc05e2007-11-04 19:15:06 +0000956
957 /* These formats treat the title line specially. */
958 if (fmt == CMIT_FMT_ONELINE || fmt == CMIT_FMT_EMAIL)
959 pp_title_line(fmt, &msg, sb, subject,
Junio C Hamano6bf4f1b2008-03-14 17:10:09 -0700960 after_subject, encoding, need_8bit_cte);
Johannes Schindelin93fc05e2007-11-04 19:15:06 +0000961
962 beginning_of_body = sb->len;
963 if (fmt != CMIT_FMT_ONELINE)
964 pp_remainder(fmt, &msg, sb, indent);
965 strbuf_rtrim(sb);
966
967 /* Make sure there is an EOLN for the non-oneline case */
968 if (fmt != CMIT_FMT_ONELINE)
969 strbuf_addch(sb, '\n');
970
971 /*
972 * The caller may append additional body text in e-mail
973 * format. Make sure we did not strip the blank line
974 * between the header and the body.
975 */
976 if (fmt == CMIT_FMT_EMAIL && sb->len <= beginning_of_body)
977 strbuf_addch(sb, '\n');
Johannes Schindelin93fc05e2007-11-04 19:15:06 +0000978 free(reencoded);
979}