blob: f85444b27d4815547f7cef542291b1f6616f77d5 [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"
Johannes Schindelina97a7462009-10-09 12:21:57 +02009#include "notes.h"
Jeff Kingc0029222009-01-17 10:38:46 -050010#include "color.h"
Thomas Rast8f8f5472009-10-19 17:48:10 +020011#include "reflog-walk.h"
Johannes Schindelin93fc05e2007-11-04 19:15:06 +000012
Johannes Schindelin93fc05e2007-11-04 19:15:06 +000013static char *user_format;
Will Palmer40957892010-05-02 12:00:42 +010014static struct cmt_fmt_map {
15 const char *name;
16 enum cmit_fmt format;
17 int is_tformat;
Will Palmer2d7671e2010-05-02 12:00:43 +010018 int is_alias;
19 const char *user_format;
Will Palmer40957892010-05-02 12:00:42 +010020} *commit_formats;
Will Palmer80281842010-05-02 12:00:44 +010021static size_t builtin_formats_len;
Will Palmer40957892010-05-02 12:00:42 +010022static size_t commit_formats_len;
Will Palmer80281842010-05-02 12:00:44 +010023static size_t commit_formats_alloc;
Will Palmer40957892010-05-02 12:00:42 +010024static struct cmt_fmt_map *find_commit_format(const char *sought);
Johannes Schindelin93fc05e2007-11-04 19:15:06 +000025
Nanako Shiraishi36407542009-02-24 18:59:15 +090026static void save_user_format(struct rev_info *rev, const char *cp, int is_tformat)
27{
28 free(user_format);
29 user_format = xstrdup(cp);
30 if (is_tformat)
31 rev->use_terminator = 1;
32 rev->commit_format = CMIT_FMT_USERFORMAT;
33}
34
Will Palmer80281842010-05-02 12:00:44 +010035static int git_pretty_formats_config(const char *var, const char *value, void *cb)
36{
37 struct cmt_fmt_map *commit_format = NULL;
38 const char *name;
39 const char *fmt;
40 int i;
41
42 if (prefixcmp(var, "pretty."))
43 return 0;
44
45 name = var + strlen("pretty.");
46 for (i = 0; i < builtin_formats_len; i++) {
47 if (!strcmp(commit_formats[i].name, name))
48 return 0;
49 }
50
51 for (i = builtin_formats_len; i < commit_formats_len; i++) {
52 if (!strcmp(commit_formats[i].name, name)) {
53 commit_format = &commit_formats[i];
54 break;
55 }
56 }
57
58 if (!commit_format) {
59 ALLOC_GROW(commit_formats, commit_formats_len+1,
60 commit_formats_alloc);
61 commit_format = &commit_formats[commit_formats_len];
Jonathan Nieder95a26182010-05-08 16:07:39 -050062 memset(commit_format, 0, sizeof(*commit_format));
Will Palmer80281842010-05-02 12:00:44 +010063 commit_formats_len++;
64 }
65
66 commit_format->name = xstrdup(name);
67 commit_format->format = CMIT_FMT_USERFORMAT;
68 git_config_string(&fmt, var, value);
69 if (!prefixcmp(fmt, "format:") || !prefixcmp(fmt, "tformat:")) {
70 commit_format->is_tformat = fmt[0] == 't';
71 fmt = strchr(fmt, ':') + 1;
72 } else if (strchr(fmt, '%'))
73 commit_format->is_tformat = 1;
74 else
75 commit_format->is_alias = 1;
76 commit_format->user_format = fmt;
77
78 return 0;
79}
80
Will Palmer40957892010-05-02 12:00:42 +010081static void setup_commit_formats(void)
82{
83 struct cmt_fmt_map builtin_formats[] = {
84 { "raw", CMIT_FMT_RAW, 0 },
85 { "medium", CMIT_FMT_MEDIUM, 0 },
86 { "short", CMIT_FMT_SHORT, 0 },
87 { "email", CMIT_FMT_EMAIL, 0 },
88 { "fuller", CMIT_FMT_FULLER, 0 },
89 { "full", CMIT_FMT_FULL, 0 },
90 { "oneline", CMIT_FMT_ONELINE, 1 }
91 };
92 commit_formats_len = ARRAY_SIZE(builtin_formats);
Will Palmer80281842010-05-02 12:00:44 +010093 builtin_formats_len = commit_formats_len;
94 ALLOC_GROW(commit_formats, commit_formats_len, commit_formats_alloc);
Will Palmer40957892010-05-02 12:00:42 +010095 memcpy(commit_formats, builtin_formats,
96 sizeof(*builtin_formats)*ARRAY_SIZE(builtin_formats));
Will Palmer80281842010-05-02 12:00:44 +010097
98 git_config(git_pretty_formats_config, NULL);
Will Palmer40957892010-05-02 12:00:42 +010099}
100
Will Palmer2d7671e2010-05-02 12:00:43 +0100101static struct cmt_fmt_map *find_commit_format_recursive(const char *sought,
102 const char *original,
103 int num_redirections)
Will Palmer40957892010-05-02 12:00:42 +0100104{
105 struct cmt_fmt_map *found = NULL;
106 size_t found_match_len = 0;
107 int i;
108
Will Palmer2d7671e2010-05-02 12:00:43 +0100109 if (num_redirections >= commit_formats_len)
110 die("invalid --pretty format: "
111 "'%s' references an alias which points to itself",
112 original);
Will Palmer40957892010-05-02 12:00:42 +0100113
114 for (i = 0; i < commit_formats_len; i++) {
115 size_t match_len;
116
117 if (prefixcmp(commit_formats[i].name, sought))
118 continue;
119
120 match_len = strlen(commit_formats[i].name);
121 if (found == NULL || found_match_len > match_len) {
122 found = &commit_formats[i];
123 found_match_len = match_len;
124 }
125 }
Will Palmer2d7671e2010-05-02 12:00:43 +0100126
127 if (found && found->is_alias) {
128 found = find_commit_format_recursive(found->user_format,
129 original,
130 num_redirections+1);
131 }
132
Will Palmer40957892010-05-02 12:00:42 +0100133 return found;
134}
135
Will Palmer2d7671e2010-05-02 12:00:43 +0100136static struct cmt_fmt_map *find_commit_format(const char *sought)
137{
138 if (!commit_formats)
139 setup_commit_formats();
140
141 return find_commit_format_recursive(sought, sought, 0);
142}
143
Junio C Hamano4da45be2008-04-07 17:11:34 -0700144void get_commit_format(const char *arg, struct rev_info *rev)
Johannes Schindelin93fc05e2007-11-04 19:15:06 +0000145{
Will Palmer40957892010-05-02 12:00:42 +0100146 struct cmt_fmt_map *commit_format;
Johannes Schindelin93fc05e2007-11-04 19:15:06 +0000147
Junio C Hamano4da45be2008-04-07 17:11:34 -0700148 rev->use_terminator = 0;
149 if (!arg || !*arg) {
150 rev->commit_format = CMIT_FMT_DEFAULT;
151 return;
152 }
Junio C Hamano4da45be2008-04-07 17:11:34 -0700153 if (!prefixcmp(arg, "format:") || !prefixcmp(arg, "tformat:")) {
Nanako Shiraishi36407542009-02-24 18:59:15 +0900154 save_user_format(rev, strchr(arg, ':') + 1, arg[0] == 't');
Junio C Hamano4da45be2008-04-07 17:11:34 -0700155 return;
Johannes Schindelin93fc05e2007-11-04 19:15:06 +0000156 }
Will Palmer40957892010-05-02 12:00:42 +0100157
Nanako Shiraishi36407542009-02-24 18:59:15 +0900158 if (strchr(arg, '%')) {
159 save_user_format(rev, arg, 1);
160 return;
161 }
Johannes Schindelin93fc05e2007-11-04 19:15:06 +0000162
Will Palmer40957892010-05-02 12:00:42 +0100163 commit_format = find_commit_format(arg);
164 if (!commit_format)
165 die("invalid --pretty format: %s", arg);
166
167 rev->commit_format = commit_format->format;
168 rev->use_terminator = commit_format->is_tformat;
Will Palmer80281842010-05-02 12:00:44 +0100169 if (commit_format->format == CMIT_FMT_USERFORMAT) {
170 save_user_format(rev, commit_format->user_format,
171 commit_format->is_tformat);
172 }
Johannes Schindelin93fc05e2007-11-04 19:15:06 +0000173}
174
175/*
176 * Generic support for pretty-printing the header
177 */
178static int get_one_line(const char *msg)
179{
180 int ret = 0;
181
182 for (;;) {
183 char c = *msg++;
184 if (!c)
185 break;
186 ret++;
187 if (c == '\n')
188 break;
189 }
190 return ret;
191}
192
193/* High bit set, or ISO-2022-INT */
Junio C Hamanocc571142010-01-11 22:23:35 -0800194static int non_ascii(int ch)
Johannes Schindelin93fc05e2007-11-04 19:15:06 +0000195{
René Scharfec2e93642009-03-07 14:06:49 +0100196 return !isascii(ch) || ch == '\033';
Johannes Schindelin93fc05e2007-11-04 19:15:06 +0000197}
198
Johannes Schindelin28e9cf62009-08-10 18:22:18 +0200199int has_non_ascii(const char *s)
200{
201 int ch;
202 if (!s)
203 return 0;
204 while ((ch = *s++) != '\0') {
205 if (non_ascii(ch))
206 return 1;
207 }
208 return 0;
209}
210
Johannes Schindelin93fc05e2007-11-04 19:15:06 +0000211static int is_rfc2047_special(char ch)
212{
213 return (non_ascii(ch) || (ch == '=') || (ch == '?') || (ch == '_'));
214}
215
216static void add_rfc2047(struct strbuf *sb, const char *line, int len,
217 const char *encoding)
218{
219 int i, last;
220
221 for (i = 0; i < len; i++) {
222 int ch = line[i];
223 if (non_ascii(ch))
224 goto needquote;
225 if ((i + 1 < len) && (ch == '=' && line[i+1] == '?'))
226 goto needquote;
227 }
228 strbuf_add(sb, line, len);
229 return;
230
231needquote:
232 strbuf_grow(sb, len * 3 + strlen(encoding) + 100);
233 strbuf_addf(sb, "=?%s?q?", encoding);
234 for (i = last = 0; i < len; i++) {
235 unsigned ch = line[i] & 0xFF;
236 /*
237 * We encode ' ' using '=20' even though rfc2047
238 * allows using '_' for readability. Unfortunately,
239 * many programs do not understand this and just
240 * leave the underscore in place.
241 */
242 if (is_rfc2047_special(ch) || ch == ' ') {
243 strbuf_add(sb, line + last, i - last);
244 strbuf_addf(sb, "=%02X", ch);
245 last = i + 1;
246 }
247 }
248 strbuf_add(sb, line + last, len - last);
249 strbuf_addstr(sb, "?=");
250}
251
Daniel Barkalowb02bd652008-02-18 22:56:08 -0500252void pp_user_info(const char *what, enum cmit_fmt fmt, struct strbuf *sb,
253 const char *line, enum date_mode dmode,
254 const char *encoding)
Johannes Schindelin93fc05e2007-11-04 19:15:06 +0000255{
256 char *date;
257 int namelen;
258 unsigned long time;
259 int tz;
Johannes Schindelin93fc05e2007-11-04 19:15:06 +0000260
261 if (fmt == CMIT_FMT_ONELINE)
262 return;
263 date = strchr(line, '>');
264 if (!date)
265 return;
266 namelen = ++date - line;
267 time = strtoul(date, &date, 10);
268 tz = strtol(date, NULL, 10);
269
270 if (fmt == CMIT_FMT_EMAIL) {
271 char *name_tail = strchr(line, '<');
272 int display_name_length;
273 if (!name_tail)
274 return;
275 while (line < name_tail && isspace(name_tail[-1]))
276 name_tail--;
277 display_name_length = name_tail - line;
Johannes Schindelin93fc05e2007-11-04 19:15:06 +0000278 strbuf_addstr(sb, "From: ");
279 add_rfc2047(sb, line, display_name_length, encoding);
280 strbuf_add(sb, name_tail, namelen - display_name_length);
281 strbuf_addch(sb, '\n');
282 } else {
283 strbuf_addf(sb, "%s: %.*s%.*s\n", what,
284 (fmt == CMIT_FMT_FULLER) ? 4 : 0,
Benjamin Kramer8e76bf32009-03-13 13:51:33 +0100285 " ", namelen, line);
Johannes Schindelin93fc05e2007-11-04 19:15:06 +0000286 }
287 switch (fmt) {
288 case CMIT_FMT_MEDIUM:
289 strbuf_addf(sb, "Date: %s\n", show_date(time, tz, dmode));
290 break;
291 case CMIT_FMT_EMAIL:
292 strbuf_addf(sb, "Date: %s\n", show_date(time, tz, DATE_RFC2822));
293 break;
294 case CMIT_FMT_FULLER:
295 strbuf_addf(sb, "%sDate: %s\n", what, show_date(time, tz, dmode));
296 break;
297 default:
298 /* notin' */
299 break;
300 }
301}
302
303static int is_empty_line(const char *line, int *len_p)
304{
305 int len = *len_p;
306 while (len && isspace(line[len-1]))
307 len--;
308 *len_p = len;
309 return !len;
310}
311
René Scharfea0109662008-12-27 01:32:49 +0100312static const char *skip_empty_lines(const char *msg)
313{
314 for (;;) {
315 int linelen = get_one_line(msg);
316 int ll = linelen;
317 if (!linelen)
318 break;
319 if (!is_empty_line(msg, &ll))
320 break;
321 msg += linelen;
322 }
323 return msg;
324}
325
Johannes Schindelin93fc05e2007-11-04 19:15:06 +0000326static void add_merge_info(enum cmit_fmt fmt, struct strbuf *sb,
327 const struct commit *commit, int abbrev)
328{
329 struct commit_list *parent = commit->parents;
330
331 if ((fmt == CMIT_FMT_ONELINE) || (fmt == CMIT_FMT_EMAIL) ||
332 !parent || !parent->next)
333 return;
334
335 strbuf_addstr(sb, "Merge:");
336
337 while (parent) {
338 struct commit *p = parent->item;
339 const char *hex = NULL;
Johannes Schindelin93fc05e2007-11-04 19:15:06 +0000340 if (abbrev)
341 hex = find_unique_abbrev(p->object.sha1, abbrev);
342 if (!hex)
343 hex = sha1_to_hex(p->object.sha1);
Johannes Schindelin93fc05e2007-11-04 19:15:06 +0000344 parent = parent->next;
345
Thomas Rast7fcda922009-02-13 23:10:41 +0100346 strbuf_addf(sb, " %s", hex);
Johannes Schindelin93fc05e2007-11-04 19:15:06 +0000347 }
348 strbuf_addch(sb, '\n');
349}
350
351static char *get_header(const struct commit *commit, const char *key)
352{
353 int key_len = strlen(key);
354 const char *line = commit->buffer;
355
356 for (;;) {
357 const char *eol = strchr(line, '\n'), *next;
358
359 if (line == eol)
360 return NULL;
361 if (!eol) {
362 eol = line + strlen(line);
363 next = NULL;
364 } else
365 next = eol + 1;
366 if (eol - line > key_len &&
367 !strncmp(line, key, key_len) &&
368 line[key_len] == ' ') {
369 return xmemdupz(line + key_len + 1, eol - line - key_len - 1);
370 }
371 line = next;
372 }
373}
374
375static char *replace_encoding_header(char *buf, const char *encoding)
376{
Brandon Caseyf285a2d2008-10-09 14:12:12 -0500377 struct strbuf tmp = STRBUF_INIT;
Johannes Schindelin93fc05e2007-11-04 19:15:06 +0000378 size_t start, len;
379 char *cp = buf;
380
381 /* guess if there is an encoding header before a \n\n */
382 while (strncmp(cp, "encoding ", strlen("encoding "))) {
383 cp = strchr(cp, '\n');
384 if (!cp || *++cp == '\n')
385 return buf;
386 }
387 start = cp - buf;
388 cp = strchr(cp, '\n');
389 if (!cp)
390 return buf; /* should not happen but be defensive */
391 len = cp + 1 - (buf + start);
392
Johannes Schindelin93fc05e2007-11-04 19:15:06 +0000393 strbuf_attach(&tmp, buf, strlen(buf), strlen(buf) + 1);
394 if (is_encoding_utf8(encoding)) {
395 /* we have re-coded to UTF-8; drop the header */
396 strbuf_remove(&tmp, start, len);
397 } else {
398 /* just replaces XXXX in 'encoding XXXX\n' */
399 strbuf_splice(&tmp, start + strlen("encoding "),
400 len - strlen("encoding \n"),
401 encoding, strlen(encoding));
402 }
403 return strbuf_detach(&tmp, NULL);
404}
405
406static char *logmsg_reencode(const struct commit *commit,
407 const char *output_encoding)
408{
Brandon Casey330db182009-05-18 18:44:39 -0500409 static const char *utf8 = "UTF-8";
Johannes Schindelin93fc05e2007-11-04 19:15:06 +0000410 const char *use_encoding;
411 char *encoding;
412 char *out;
413
414 if (!*output_encoding)
415 return NULL;
416 encoding = get_header(commit, "encoding");
417 use_encoding = encoding ? encoding : utf8;
418 if (!strcmp(use_encoding, output_encoding))
419 if (encoding) /* we'll strip encoding header later */
420 out = xstrdup(commit->buffer);
421 else
422 return NULL; /* nothing to do */
423 else
424 out = reencode_string(commit->buffer,
425 output_encoding, use_encoding);
426 if (out)
427 out = replace_encoding_header(out, output_encoding);
428
429 free(encoding);
430 return out;
431}
432
Marius Storm-Olsend20d6542009-02-08 15:34:30 +0100433static int mailmap_name(char *email, int email_len, char *name, int name_len)
Johannes Schindeline0cbc392008-07-12 00:28:18 +0100434{
Johannes Schindelinc455c872008-07-21 19:03:49 +0100435 static struct string_list *mail_map;
Johannes Schindeline0cbc392008-07-12 00:28:18 +0100436 if (!mail_map) {
437 mail_map = xcalloc(1, sizeof(*mail_map));
Marius Storm-Olsend551a482009-02-08 15:34:27 +0100438 read_mailmap(mail_map, NULL);
Johannes Schindeline0cbc392008-07-12 00:28:18 +0100439 }
Marius Storm-Olsend20d6542009-02-08 15:34:30 +0100440 return mail_map->nr && map_user(mail_map, email, email_len, name, name_len);
Johannes Schindeline0cbc392008-07-12 00:28:18 +0100441}
442
Marco Costalbac3a670d2008-02-09 15:40:19 +0100443static size_t format_person_part(struct strbuf *sb, char part,
Jeff Kingd36f8672008-08-28 20:54:59 -0400444 const char *msg, int len, enum date_mode dmode)
Johannes Schindelin93fc05e2007-11-04 19:15:06 +0000445{
Marco Costalbac3a670d2008-02-09 15:40:19 +0100446 /* currently all placeholders have same length */
447 const int placeholder_len = 2;
Johannes Schindelin93fc05e2007-11-04 19:15:06 +0000448 int start, end, tz = 0;
Marco Costalbac3a670d2008-02-09 15:40:19 +0100449 unsigned long date = 0;
Johannes Schindelin93fc05e2007-11-04 19:15:06 +0000450 char *ep;
Marius Storm-Olsend20d6542009-02-08 15:34:30 +0100451 const char *name_start, *name_end, *mail_start, *mail_end, *msg_end = msg+len;
452 char person_name[1024];
453 char person_mail[1024];
Johannes Schindelin93fc05e2007-11-04 19:15:06 +0000454
Marco Costalbac3a670d2008-02-09 15:40:19 +0100455 /* advance 'end' to point to email start delimiter */
Johannes Schindelin93fc05e2007-11-04 19:15:06 +0000456 for (end = 0; end < len && msg[end] != '<'; end++)
457 ; /* do nothing */
Johannes Schindelin93fc05e2007-11-04 19:15:06 +0000458
Marco Costalbac3a670d2008-02-09 15:40:19 +0100459 /*
460 * When end points at the '<' that we found, it should have
461 * matching '>' later, which means 'end' must be strictly
462 * below len - 1.
463 */
464 if (end >= len - 2)
465 goto skip;
466
Marius Storm-Olsend20d6542009-02-08 15:34:30 +0100467 /* Seek for both name and email part */
468 name_start = msg;
469 name_end = msg+end;
470 while (name_end > name_start && isspace(*(name_end-1)))
471 name_end--;
472 mail_start = msg+end+1;
473 mail_end = mail_start;
474 while (mail_end < msg_end && *mail_end != '>')
475 mail_end++;
476 if (mail_end == msg_end)
477 goto skip;
478 end = mail_end-msg;
479
480 if (part == 'N' || part == 'E') { /* mailmap lookup */
481 strlcpy(person_name, name_start, name_end-name_start+1);
482 strlcpy(person_mail, mail_start, mail_end-mail_start+1);
483 mailmap_name(person_mail, sizeof(person_mail), person_name, sizeof(person_name));
484 name_start = person_name;
485 name_end = name_start + strlen(person_name);
486 mail_start = person_mail;
487 mail_end = mail_start + strlen(person_mail);
488 }
Johannes Schindeline0cbc392008-07-12 00:28:18 +0100489 if (part == 'n' || part == 'N') { /* name */
Marius Storm-Olsend20d6542009-02-08 15:34:30 +0100490 strbuf_add(sb, name_start, name_end-name_start);
Marco Costalbac3a670d2008-02-09 15:40:19 +0100491 return placeholder_len;
492 }
Marius Storm-Olsend20d6542009-02-08 15:34:30 +0100493 if (part == 'e' || part == 'E') { /* email */
494 strbuf_add(sb, mail_start, mail_end-mail_start);
Marco Costalbac3a670d2008-02-09 15:40:19 +0100495 return placeholder_len;
René Scharfecde75e52007-11-09 01:49:42 +0100496 }
Johannes Schindelin93fc05e2007-11-04 19:15:06 +0000497
Marco Costalbac3a670d2008-02-09 15:40:19 +0100498 /* advance 'start' to point to date start delimiter */
Johannes Schindelin93fc05e2007-11-04 19:15:06 +0000499 for (start = end + 1; start < len && isspace(msg[start]); start++)
500 ; /* do nothing */
501 if (start >= len)
Marco Costalbac3a670d2008-02-09 15:40:19 +0100502 goto skip;
Johannes Schindelin93fc05e2007-11-04 19:15:06 +0000503 date = strtoul(msg + start, &ep, 10);
504 if (msg + start == ep)
Marco Costalbac3a670d2008-02-09 15:40:19 +0100505 goto skip;
Johannes Schindelin93fc05e2007-11-04 19:15:06 +0000506
René Scharfecde75e52007-11-09 01:49:42 +0100507 if (part == 't') { /* date, UNIX timestamp */
508 strbuf_add(sb, msg + start, ep - (msg + start));
Marco Costalbac3a670d2008-02-09 15:40:19 +0100509 return placeholder_len;
René Scharfecde75e52007-11-09 01:49:42 +0100510 }
Johannes Schindelin93fc05e2007-11-04 19:15:06 +0000511
512 /* parse tz */
513 for (start = ep - msg + 1; start < len && isspace(msg[start]); start++)
514 ; /* do nothing */
515 if (start + 1 < len) {
516 tz = strtoul(msg + start + 1, NULL, 10);
517 if (msg[start] == '-')
518 tz = -tz;
519 }
520
René Scharfecde75e52007-11-09 01:49:42 +0100521 switch (part) {
522 case 'd': /* date */
Jeff Kingd36f8672008-08-28 20:54:59 -0400523 strbuf_addstr(sb, show_date(date, tz, dmode));
Marco Costalbac3a670d2008-02-09 15:40:19 +0100524 return placeholder_len;
René Scharfecde75e52007-11-09 01:49:42 +0100525 case 'D': /* date, RFC2822 style */
526 strbuf_addstr(sb, show_date(date, tz, DATE_RFC2822));
Marco Costalbac3a670d2008-02-09 15:40:19 +0100527 return placeholder_len;
René Scharfecde75e52007-11-09 01:49:42 +0100528 case 'r': /* date, relative */
529 strbuf_addstr(sb, show_date(date, tz, DATE_RELATIVE));
Marco Costalbac3a670d2008-02-09 15:40:19 +0100530 return placeholder_len;
René Scharfecde75e52007-11-09 01:49:42 +0100531 case 'i': /* date, ISO 8601 */
532 strbuf_addstr(sb, show_date(date, tz, DATE_ISO8601));
Marco Costalbac3a670d2008-02-09 15:40:19 +0100533 return placeholder_len;
René Scharfecde75e52007-11-09 01:49:42 +0100534 }
Marco Costalbac3a670d2008-02-09 15:40:19 +0100535
536skip:
537 /*
538 * bogus commit, 'sb' cannot be updated, but we still need to
539 * compute a valid return value.
540 */
541 if (part == 'n' || part == 'e' || part == 't' || part == 'd'
542 || part == 'D' || part == 'r' || part == 'i')
543 return placeholder_len;
544
545 return 0; /* unknown placeholder */
Johannes Schindelin93fc05e2007-11-04 19:15:06 +0000546}
547
René Scharfef29d5952007-11-10 12:14:20 +0100548struct chunk {
549 size_t off;
550 size_t len;
551};
552
553struct format_commit_context {
554 const struct commit *commit;
Thomas Rastdd2e7942009-10-19 17:48:08 +0200555 const struct pretty_print_context *pretty_ctx;
René Scharfef53bd742008-12-27 01:49:21 +0100556 unsigned commit_header_parsed:1;
557 unsigned commit_message_parsed:1;
René Scharfe02edd562009-10-17 23:04:19 +0200558 size_t width, indent1, indent2;
René Scharfef29d5952007-11-10 12:14:20 +0100559
560 /* These offsets are relative to the start of the commit message. */
René Scharfef29d5952007-11-10 12:14:20 +0100561 struct chunk author;
562 struct chunk committer;
563 struct chunk encoding;
René Scharfef53bd742008-12-27 01:49:21 +0100564 size_t message_off;
565 size_t subject_off;
René Scharfef29d5952007-11-10 12:14:20 +0100566 size_t body_off;
René Scharfeb9c62322007-11-10 12:18:26 +0100567
568 /* The following ones are relative to the result struct strbuf. */
569 struct chunk abbrev_commit_hash;
570 struct chunk abbrev_tree_hash;
571 struct chunk abbrev_parent_hashes;
René Scharfe02edd562009-10-17 23:04:19 +0200572 size_t wrap_start;
René Scharfef29d5952007-11-10 12:14:20 +0100573};
574
René Scharfeb9c62322007-11-10 12:18:26 +0100575static int add_again(struct strbuf *sb, struct chunk *chunk)
576{
577 if (chunk->len) {
578 strbuf_adddup(sb, chunk->off, chunk->len);
579 return 1;
580 }
581
582 /*
583 * We haven't seen this chunk before. Our caller is surely
584 * going to add it the hard way now. Remember the most likely
585 * start of the to-be-added chunk: the current end of the
586 * struct strbuf.
587 */
588 chunk->off = sb->len;
589 return 0;
590}
591
René Scharfef29d5952007-11-10 12:14:20 +0100592static void parse_commit_header(struct format_commit_context *context)
593{
594 const char *msg = context->commit->buffer;
595 int i;
René Scharfef29d5952007-11-10 12:14:20 +0100596
René Scharfef53bd742008-12-27 01:49:21 +0100597 for (i = 0; msg[i]; i++) {
René Scharfef29d5952007-11-10 12:14:20 +0100598 int eol;
599 for (eol = i; msg[eol] && msg[eol] != '\n'; eol++)
600 ; /* do nothing */
601
René Scharfef29d5952007-11-10 12:14:20 +0100602 if (i == eol) {
René Scharfef53bd742008-12-27 01:49:21 +0100603 break;
René Scharfef29d5952007-11-10 12:14:20 +0100604 } else if (!prefixcmp(msg + i, "author ")) {
605 context->author.off = i + 7;
606 context->author.len = eol - i - 7;
607 } else if (!prefixcmp(msg + i, "committer ")) {
608 context->committer.off = i + 10;
609 context->committer.len = eol - i - 10;
610 } else if (!prefixcmp(msg + i, "encoding ")) {
611 context->encoding.off = i + 9;
612 context->encoding.len = eol - i - 9;
613 }
614 i = eol;
615 }
René Scharfef53bd742008-12-27 01:49:21 +0100616 context->message_off = i;
René Scharfef29d5952007-11-10 12:14:20 +0100617 context->commit_header_parsed = 1;
618}
619
Stephen Boyd46d164b2009-03-22 19:14:01 -0700620static int istitlechar(char c)
621{
622 return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') ||
623 (c >= '0' && c <= '9') || c == '.' || c == '_';
624}
625
626static void format_sanitized_subject(struct strbuf *sb, const char *msg)
627{
628 size_t trimlen;
Stephen Boyd871d21d2009-03-31 16:24:38 -0700629 size_t start_len = sb->len;
Stephen Boyd46d164b2009-03-22 19:14:01 -0700630 int space = 2;
631
632 for (; *msg && *msg != '\n'; msg++) {
633 if (istitlechar(*msg)) {
634 if (space == 1)
635 strbuf_addch(sb, '-');
636 space = 0;
637 strbuf_addch(sb, *msg);
638 if (*msg == '.')
639 while (*(msg+1) == '.')
640 msg++;
641 } else
642 space |= 1;
643 }
644
645 /* trim any trailing '.' or '-' characters */
646 trimlen = 0;
Stephen Boyd871d21d2009-03-31 16:24:38 -0700647 while (sb->len - trimlen > start_len &&
648 (sb->buf[sb->len - 1 - trimlen] == '.'
649 || sb->buf[sb->len - 1 - trimlen] == '-'))
Stephen Boyd46d164b2009-03-22 19:14:01 -0700650 trimlen++;
651 strbuf_remove(sb, sb->len - trimlen, trimlen);
652}
653
René Scharfecec08712009-01-06 21:41:06 +0100654const char *format_subject(struct strbuf *sb, const char *msg,
655 const char *line_separator)
René Scharfe88c44732008-12-27 01:39:35 +0100656{
657 int first = 1;
658
659 for (;;) {
660 const char *line = msg;
661 int linelen = get_one_line(line);
662
663 msg += linelen;
664 if (!linelen || is_empty_line(line, &linelen))
665 break;
666
René Scharfef53bd742008-12-27 01:49:21 +0100667 if (!sb)
668 continue;
René Scharfe88c44732008-12-27 01:39:35 +0100669 strbuf_grow(sb, linelen + 2);
670 if (!first)
671 strbuf_addstr(sb, line_separator);
672 strbuf_add(sb, line, linelen);
673 first = 0;
674 }
675 return msg;
676}
677
René Scharfef53bd742008-12-27 01:49:21 +0100678static void parse_commit_message(struct format_commit_context *c)
679{
680 const char *msg = c->commit->buffer + c->message_off;
681 const char *start = c->commit->buffer;
682
683 msg = skip_empty_lines(msg);
684 c->subject_off = msg - start;
685
686 msg = format_subject(NULL, msg, NULL);
687 msg = skip_empty_lines(msg);
688 c->body_off = msg - start;
689
690 c->commit_message_parsed = 1;
691}
692
René Scharfe3b3d4432008-09-04 23:40:03 +0200693static void format_decoration(struct strbuf *sb, const struct commit *commit)
694{
695 struct name_decoration *d;
696 const char *prefix = " (";
697
Lars Hjemli33e70182009-08-15 16:23:12 +0200698 load_ref_decorations(DECORATE_SHORT_REFS);
René Scharfe3b3d4432008-09-04 23:40:03 +0200699 d = lookup_decoration(&name_decoration, &commit->object);
700 while (d) {
701 strbuf_addstr(sb, prefix);
702 prefix = ", ";
703 strbuf_addstr(sb, d->name);
704 d = d->next;
705 }
706 if (prefix[0] == ',')
707 strbuf_addch(sb, ')');
708}
709
René Scharfe02edd562009-10-17 23:04:19 +0200710static void strbuf_wrap(struct strbuf *sb, size_t pos,
711 size_t width, size_t indent1, size_t indent2)
712{
713 struct strbuf tmp = STRBUF_INIT;
714
715 if (pos)
716 strbuf_add(&tmp, sb->buf, pos);
717 strbuf_add_wrapped_text(&tmp, sb->buf + pos,
718 (int) indent1, (int) indent2, (int) width);
719 strbuf_swap(&tmp, sb);
720 strbuf_release(&tmp);
721}
722
723static void rewrap_message_tail(struct strbuf *sb,
724 struct format_commit_context *c,
725 size_t new_width, size_t new_indent1,
726 size_t new_indent2)
727{
728 if (c->width == new_width && c->indent1 == new_indent1 &&
729 c->indent2 == new_indent2)
730 return;
René Scharfe32ca4242009-11-08 02:04:21 +0100731 if (c->wrap_start < sb->len)
René Scharfe02edd562009-10-17 23:04:19 +0200732 strbuf_wrap(sb, c->wrap_start, c->width, c->indent1, c->indent2);
733 c->wrap_start = sb->len;
734 c->width = new_width;
735 c->indent1 = new_indent1;
736 c->indent2 = new_indent2;
737}
738
Junio C Hamano9fa708d2009-10-04 23:43:32 -0700739static size_t format_commit_one(struct strbuf *sb, const char *placeholder,
740 void *context)
Johannes Schindelin93fc05e2007-11-04 19:15:06 +0000741{
René Scharfef29d5952007-11-10 12:14:20 +0100742 struct format_commit_context *c = context;
743 const struct commit *commit = c->commit;
Johannes Schindelin93fc05e2007-11-04 19:15:06 +0000744 const char *msg = commit->buffer;
René Scharfef29d5952007-11-10 12:14:20 +0100745 struct commit_list *p;
Govind Salinas42c8c742008-03-21 10:05:06 -0500746 int h1, h2;
Johannes Schindelin93fc05e2007-11-04 19:15:06 +0000747
Johannes Schindelin93fc05e2007-11-04 19:15:06 +0000748 /* these are independent of the commit */
René Scharfecde75e52007-11-09 01:49:42 +0100749 switch (placeholder[0]) {
750 case 'C':
Jeff Kingc0029222009-01-17 10:38:46 -0500751 if (placeholder[1] == '(') {
752 const char *end = strchr(placeholder + 2, ')');
753 char color[COLOR_MAXLEN];
754 if (!end)
755 return 0;
756 color_parse_mem(placeholder + 2,
757 end - (placeholder + 2),
758 "--pretty format", color);
759 strbuf_addstr(sb, color);
760 return end - placeholder + 1;
761 }
Marco Costalbac3a670d2008-02-09 15:40:19 +0100762 if (!prefixcmp(placeholder + 1, "red")) {
Arjen Laarhovendc6ebd42009-02-13 22:53:40 +0100763 strbuf_addstr(sb, GIT_COLOR_RED);
Marco Costalbac3a670d2008-02-09 15:40:19 +0100764 return 4;
765 } else if (!prefixcmp(placeholder + 1, "green")) {
Arjen Laarhovendc6ebd42009-02-13 22:53:40 +0100766 strbuf_addstr(sb, GIT_COLOR_GREEN);
Marco Costalbac3a670d2008-02-09 15:40:19 +0100767 return 6;
768 } else if (!prefixcmp(placeholder + 1, "blue")) {
Arjen Laarhovendc6ebd42009-02-13 22:53:40 +0100769 strbuf_addstr(sb, GIT_COLOR_BLUE);
Marco Costalbac3a670d2008-02-09 15:40:19 +0100770 return 5;
771 } else if (!prefixcmp(placeholder + 1, "reset")) {
Arjen Laarhovendc6ebd42009-02-13 22:53:40 +0100772 strbuf_addstr(sb, GIT_COLOR_RESET);
Marco Costalbac3a670d2008-02-09 15:40:19 +0100773 return 6;
774 } else
775 return 0;
René Scharfecde75e52007-11-09 01:49:42 +0100776 case 'n': /* newline */
777 strbuf_addch(sb, '\n');
Marco Costalbac3a670d2008-02-09 15:40:19 +0100778 return 1;
Govind Salinas42c8c742008-03-21 10:05:06 -0500779 case 'x':
780 /* %x00 == NUL, %x0a == LF, etc. */
781 if (0 <= (h1 = hexval_table[0xff & placeholder[1]]) &&
782 h1 <= 16 &&
783 0 <= (h2 = hexval_table[0xff & placeholder[2]]) &&
784 h2 <= 16) {
785 strbuf_addch(sb, (h1<<4)|h2);
786 return 3;
787 } else
788 return 0;
René Scharfe02edd562009-10-17 23:04:19 +0200789 case 'w':
790 if (placeholder[1] == '(') {
791 unsigned long width = 0, indent1 = 0, indent2 = 0;
792 char *next;
793 const char *start = placeholder + 2;
794 const char *end = strchr(start, ')');
795 if (!end)
796 return 0;
797 if (end > start) {
798 width = strtoul(start, &next, 10);
799 if (*next == ',') {
800 indent1 = strtoul(next + 1, &next, 10);
801 if (*next == ',') {
802 indent2 = strtoul(next + 1,
803 &next, 10);
804 }
805 }
806 if (*next != ')')
807 return 0;
808 }
809 rewrap_message_tail(sb, c, width, indent1, indent2);
810 return end - placeholder + 1;
811 } else
812 return 0;
René Scharfecde75e52007-11-09 01:49:42 +0100813 }
Johannes Schindelin93fc05e2007-11-04 19:15:06 +0000814
815 /* these depend on the commit */
816 if (!commit->object.parsed)
817 parse_object(commit->object.sha1);
Johannes Schindelin93fc05e2007-11-04 19:15:06 +0000818
René Scharfecde75e52007-11-09 01:49:42 +0100819 switch (placeholder[0]) {
820 case 'H': /* commit hash */
821 strbuf_addstr(sb, sha1_to_hex(commit->object.sha1));
Marco Costalbac3a670d2008-02-09 15:40:19 +0100822 return 1;
René Scharfecde75e52007-11-09 01:49:42 +0100823 case 'h': /* abbreviated commit hash */
René Scharfeb9c62322007-11-10 12:18:26 +0100824 if (add_again(sb, &c->abbrev_commit_hash))
Marco Costalbac3a670d2008-02-09 15:40:19 +0100825 return 1;
René Scharfecde75e52007-11-09 01:49:42 +0100826 strbuf_addstr(sb, find_unique_abbrev(commit->object.sha1,
Will Palmerc1977022010-05-03 22:18:57 -0500827 c->pretty_ctx->abbrev));
René Scharfeb9c62322007-11-10 12:18:26 +0100828 c->abbrev_commit_hash.len = sb->len - c->abbrev_commit_hash.off;
Marco Costalbac3a670d2008-02-09 15:40:19 +0100829 return 1;
René Scharfecde75e52007-11-09 01:49:42 +0100830 case 'T': /* tree hash */
831 strbuf_addstr(sb, sha1_to_hex(commit->tree->object.sha1));
Marco Costalbac3a670d2008-02-09 15:40:19 +0100832 return 1;
René Scharfecde75e52007-11-09 01:49:42 +0100833 case 't': /* abbreviated tree hash */
René Scharfeb9c62322007-11-10 12:18:26 +0100834 if (add_again(sb, &c->abbrev_tree_hash))
Marco Costalbac3a670d2008-02-09 15:40:19 +0100835 return 1;
René Scharfecde75e52007-11-09 01:49:42 +0100836 strbuf_addstr(sb, find_unique_abbrev(commit->tree->object.sha1,
Will Palmerc1977022010-05-03 22:18:57 -0500837 c->pretty_ctx->abbrev));
René Scharfeb9c62322007-11-10 12:18:26 +0100838 c->abbrev_tree_hash.len = sb->len - c->abbrev_tree_hash.off;
Marco Costalbac3a670d2008-02-09 15:40:19 +0100839 return 1;
René Scharfecde75e52007-11-09 01:49:42 +0100840 case 'P': /* parent hashes */
841 for (p = commit->parents; p; p = p->next) {
842 if (p != commit->parents)
843 strbuf_addch(sb, ' ');
844 strbuf_addstr(sb, sha1_to_hex(p->item->object.sha1));
845 }
Marco Costalbac3a670d2008-02-09 15:40:19 +0100846 return 1;
René Scharfecde75e52007-11-09 01:49:42 +0100847 case 'p': /* abbreviated parent hashes */
René Scharfeb9c62322007-11-10 12:18:26 +0100848 if (add_again(sb, &c->abbrev_parent_hashes))
Marco Costalbac3a670d2008-02-09 15:40:19 +0100849 return 1;
René Scharfecde75e52007-11-09 01:49:42 +0100850 for (p = commit->parents; p; p = p->next) {
851 if (p != commit->parents)
852 strbuf_addch(sb, ' ');
853 strbuf_addstr(sb, find_unique_abbrev(
Will Palmerc1977022010-05-03 22:18:57 -0500854 p->item->object.sha1,
855 c->pretty_ctx->abbrev));
René Scharfecde75e52007-11-09 01:49:42 +0100856 }
René Scharfeb9c62322007-11-10 12:18:26 +0100857 c->abbrev_parent_hashes.len = sb->len -
858 c->abbrev_parent_hashes.off;
Marco Costalbac3a670d2008-02-09 15:40:19 +0100859 return 1;
René Scharfecde75e52007-11-09 01:49:42 +0100860 case 'm': /* left/right/bottom */
861 strbuf_addch(sb, (commit->object.flags & BOUNDARY)
862 ? '-'
863 : (commit->object.flags & SYMMETRIC_LEFT)
864 ? '<'
865 : '>');
Marco Costalbac3a670d2008-02-09 15:40:19 +0100866 return 1;
René Scharfe3b3d4432008-09-04 23:40:03 +0200867 case 'd':
868 format_decoration(sb, commit);
869 return 1;
Thomas Rast8f8f5472009-10-19 17:48:10 +0200870 case 'g': /* reflog info */
871 switch(placeholder[1]) {
872 case 'd': /* reflog selector */
873 case 'D':
874 if (c->pretty_ctx->reflog_info)
875 get_reflog_selector(sb,
876 c->pretty_ctx->reflog_info,
877 c->pretty_ctx->date_mode,
878 (placeholder[1] == 'd'));
879 return 2;
880 case 's': /* reflog message */
881 if (c->pretty_ctx->reflog_info)
882 get_reflog_message(sb, c->pretty_ctx->reflog_info);
883 return 2;
884 }
885 return 0; /* unknown %g placeholder */
Johannes Schindelin8b208f02009-10-09 12:22:05 +0200886 case 'N':
Johannes Gilger5b163602010-04-13 22:31:12 +0200887 if (c->pretty_ctx->show_notes) {
888 format_display_notes(commit->object.sha1, sb,
889 git_log_output_encoding ? git_log_output_encoding
890 : git_commit_encoding, 0);
891 return 1;
892 }
893 return 0;
René Scharfecde75e52007-11-09 01:49:42 +0100894 }
Johannes Schindelin93fc05e2007-11-04 19:15:06 +0000895
René Scharfecde75e52007-11-09 01:49:42 +0100896 /* For the rest we have to parse the commit header. */
René Scharfef29d5952007-11-10 12:14:20 +0100897 if (!c->commit_header_parsed)
898 parse_commit_header(c);
Johannes Schindelin93fc05e2007-11-04 19:15:06 +0000899
René Scharfef29d5952007-11-10 12:14:20 +0100900 switch (placeholder[0]) {
Marco Costalbac3a670d2008-02-09 15:40:19 +0100901 case 'a': /* author ... */
902 return format_person_part(sb, placeholder[1],
Jeff Kingd36f8672008-08-28 20:54:59 -0400903 msg + c->author.off, c->author.len,
Thomas Rastdd2e7942009-10-19 17:48:08 +0200904 c->pretty_ctx->date_mode);
Marco Costalbac3a670d2008-02-09 15:40:19 +0100905 case 'c': /* committer ... */
906 return format_person_part(sb, placeholder[1],
Jeff Kingd36f8672008-08-28 20:54:59 -0400907 msg + c->committer.off, c->committer.len,
Thomas Rastdd2e7942009-10-19 17:48:08 +0200908 c->pretty_ctx->date_mode);
Marco Costalbac3a670d2008-02-09 15:40:19 +0100909 case 'e': /* encoding */
René Scharfef29d5952007-11-10 12:14:20 +0100910 strbuf_add(sb, msg + c->encoding.off, c->encoding.len);
Marco Costalbac3a670d2008-02-09 15:40:19 +0100911 return 1;
Eli Barzilay1367b122010-03-24 22:51:52 -0400912 case 'B': /* raw body */
913 /* message_off is always left at the initial newline */
914 strbuf_addstr(sb, msg + c->message_off + 1);
915 return 1;
René Scharfef53bd742008-12-27 01:49:21 +0100916 }
917
918 /* Now we need to parse the commit message. */
919 if (!c->commit_message_parsed)
920 parse_commit_message(c);
921
922 switch (placeholder[0]) {
923 case 's': /* subject */
924 format_subject(sb, msg + c->subject_off, " ");
925 return 1;
Stephen Boyd46d164b2009-03-22 19:14:01 -0700926 case 'f': /* sanitized subject */
927 format_sanitized_subject(sb, msg + c->subject_off);
928 return 1;
Marco Costalbac3a670d2008-02-09 15:40:19 +0100929 case 'b': /* body */
René Scharfef29d5952007-11-10 12:14:20 +0100930 strbuf_addstr(sb, msg + c->body_off);
Marco Costalbac3a670d2008-02-09 15:40:19 +0100931 return 1;
Johannes Schindelin93fc05e2007-11-04 19:15:06 +0000932 }
Marco Costalbac3a670d2008-02-09 15:40:19 +0100933 return 0; /* unknown placeholder */
René Scharfecde75e52007-11-09 01:49:42 +0100934}
Johannes Schindelin93fc05e2007-11-04 19:15:06 +0000935
Junio C Hamano9fa708d2009-10-04 23:43:32 -0700936static size_t format_commit_item(struct strbuf *sb, const char *placeholder,
937 void *context)
938{
939 int consumed;
940 size_t orig_len;
941 enum {
942 NO_MAGIC,
943 ADD_LF_BEFORE_NON_EMPTY,
944 DEL_LF_BEFORE_EMPTY,
Junio C Hamano223a9232010-06-22 09:45:22 -0700945 ADD_SP_BEFORE_NON_EMPTY
Junio C Hamano9fa708d2009-10-04 23:43:32 -0700946 } magic = NO_MAGIC;
947
948 switch (placeholder[0]) {
949 case '-':
950 magic = DEL_LF_BEFORE_EMPTY;
951 break;
952 case '+':
953 magic = ADD_LF_BEFORE_NON_EMPTY;
954 break;
Michael J Gruber7b881762010-06-14 18:12:29 +0200955 case ' ':
956 magic = ADD_SP_BEFORE_NON_EMPTY;
957 break;
Junio C Hamano9fa708d2009-10-04 23:43:32 -0700958 default:
959 break;
960 }
961 if (magic != NO_MAGIC)
962 placeholder++;
963
964 orig_len = sb->len;
965 consumed = format_commit_one(sb, placeholder, context);
966 if (magic == NO_MAGIC)
967 return consumed;
968
969 if ((orig_len == sb->len) && magic == DEL_LF_BEFORE_EMPTY) {
970 while (sb->len && sb->buf[sb->len - 1] == '\n')
971 strbuf_setlen(sb, sb->len - 1);
Michael J Gruber7b881762010-06-14 18:12:29 +0200972 } else if (orig_len != sb->len) {
973 if (magic == ADD_LF_BEFORE_NON_EMPTY)
974 strbuf_insert(sb, orig_len, "\n", 1);
975 else if (magic == ADD_SP_BEFORE_NON_EMPTY)
976 strbuf_insert(sb, orig_len, " ", 1);
Junio C Hamano9fa708d2009-10-04 23:43:32 -0700977 }
978 return consumed + 1;
979}
980
Johannes Gilger5b163602010-04-13 22:31:12 +0200981static size_t userformat_want_item(struct strbuf *sb, const char *placeholder,
982 void *context)
983{
984 struct userformat_want *w = context;
985
Michael J Gruber7b881762010-06-14 18:12:29 +0200986 if (*placeholder == '+' || *placeholder == '-' || *placeholder == ' ')
Johannes Gilger5b163602010-04-13 22:31:12 +0200987 placeholder++;
988
989 switch (*placeholder) {
990 case 'N':
991 w->notes = 1;
992 break;
993 }
994 return 0;
995}
996
997void userformat_find_requirements(const char *fmt, struct userformat_want *w)
998{
999 struct strbuf dummy = STRBUF_INIT;
1000
1001 if (!fmt) {
1002 if (!user_format)
1003 return;
1004 fmt = user_format;
1005 }
1006 strbuf_expand(&dummy, user_format, userformat_want_item, w);
1007 strbuf_release(&dummy);
1008}
1009
René Scharfecde75e52007-11-09 01:49:42 +01001010void format_commit_message(const struct commit *commit,
Junio C Hamano7f98ebc2009-10-15 22:59:41 -07001011 const char *format, struct strbuf *sb,
Thomas Rastdd2e7942009-10-19 17:48:08 +02001012 const struct pretty_print_context *pretty_ctx)
René Scharfecde75e52007-11-09 01:49:42 +01001013{
René Scharfef29d5952007-11-10 12:14:20 +01001014 struct format_commit_context context;
1015
1016 memset(&context, 0, sizeof(context));
1017 context.commit = commit;
Thomas Rastdd2e7942009-10-19 17:48:08 +02001018 context.pretty_ctx = pretty_ctx;
René Scharfe02edd562009-10-17 23:04:19 +02001019 context.wrap_start = sb->len;
Marco Costalbac3a670d2008-02-09 15:40:19 +01001020 strbuf_expand(sb, format, format_commit_item, &context);
René Scharfe02edd562009-10-17 23:04:19 +02001021 rewrap_message_tail(sb, &context, 0, 0, 0);
Johannes Schindelin93fc05e2007-11-04 19:15:06 +00001022}
1023
1024static void pp_header(enum cmit_fmt fmt,
1025 int abbrev,
1026 enum date_mode dmode,
1027 const char *encoding,
1028 const struct commit *commit,
1029 const char **msg_p,
1030 struct strbuf *sb)
1031{
1032 int parents_shown = 0;
1033
1034 for (;;) {
1035 const char *line = *msg_p;
1036 int linelen = get_one_line(*msg_p);
1037
1038 if (!linelen)
1039 return;
1040 *msg_p += linelen;
1041
1042 if (linelen == 1)
1043 /* End of header */
1044 return;
1045
1046 if (fmt == CMIT_FMT_RAW) {
1047 strbuf_add(sb, line, linelen);
1048 continue;
1049 }
1050
1051 if (!memcmp(line, "parent ", 7)) {
1052 if (linelen != 48)
1053 die("bad parent line in commit");
1054 continue;
1055 }
1056
1057 if (!parents_shown) {
1058 struct commit_list *parent;
1059 int num;
1060 for (parent = commit->parents, num = 0;
1061 parent;
1062 parent = parent->next, num++)
1063 ;
1064 /* with enough slop */
1065 strbuf_grow(sb, num * 50 + 20);
1066 add_merge_info(fmt, sb, commit, abbrev);
1067 parents_shown = 1;
1068 }
1069
1070 /*
1071 * MEDIUM == DEFAULT shows only author with dates.
1072 * FULL shows both authors but not dates.
1073 * FULLER shows both authors and dates.
1074 */
1075 if (!memcmp(line, "author ", 7)) {
1076 strbuf_grow(sb, linelen + 80);
Daniel Barkalowb02bd652008-02-18 22:56:08 -05001077 pp_user_info("Author", fmt, sb, line + 7, dmode, encoding);
Johannes Schindelin93fc05e2007-11-04 19:15:06 +00001078 }
1079 if (!memcmp(line, "committer ", 10) &&
1080 (fmt == CMIT_FMT_FULL || fmt == CMIT_FMT_FULLER)) {
1081 strbuf_grow(sb, linelen + 80);
Daniel Barkalowb02bd652008-02-18 22:56:08 -05001082 pp_user_info("Commit", fmt, sb, line + 10, dmode, encoding);
Johannes Schindelin93fc05e2007-11-04 19:15:06 +00001083 }
1084 }
1085}
1086
Daniel Barkalowb02bd652008-02-18 22:56:08 -05001087void pp_title_line(enum cmit_fmt fmt,
1088 const char **msg_p,
1089 struct strbuf *sb,
1090 const char *subject,
1091 const char *after_subject,
1092 const char *encoding,
Junio C Hamano267123b2008-03-15 00:09:20 -07001093 int need_8bit_cte)
Johannes Schindelin93fc05e2007-11-04 19:15:06 +00001094{
René Scharfe88c44732008-12-27 01:39:35 +01001095 const char *line_separator = (fmt == CMIT_FMT_EMAIL) ? "\n " : " ";
Johannes Schindelin93fc05e2007-11-04 19:15:06 +00001096 struct strbuf title;
1097
1098 strbuf_init(&title, 80);
René Scharfe88c44732008-12-27 01:39:35 +01001099 *msg_p = format_subject(&title, *msg_p, line_separator);
Johannes Schindelin93fc05e2007-11-04 19:15:06 +00001100
1101 strbuf_grow(sb, title.len + 1024);
1102 if (subject) {
1103 strbuf_addstr(sb, subject);
1104 add_rfc2047(sb, title.buf, title.len, encoding);
1105 } else {
1106 strbuf_addbuf(sb, &title);
1107 }
1108 strbuf_addch(sb, '\n');
1109
Junio C Hamano6bf4f1b2008-03-14 17:10:09 -07001110 if (need_8bit_cte > 0) {
Johannes Schindelin93fc05e2007-11-04 19:15:06 +00001111 const char *header_fmt =
1112 "MIME-Version: 1.0\n"
1113 "Content-Type: text/plain; charset=%s\n"
1114 "Content-Transfer-Encoding: 8bit\n";
1115 strbuf_addf(sb, header_fmt, encoding);
1116 }
1117 if (after_subject) {
1118 strbuf_addstr(sb, after_subject);
1119 }
1120 if (fmt == CMIT_FMT_EMAIL) {
1121 strbuf_addch(sb, '\n');
1122 }
1123 strbuf_release(&title);
1124}
1125
Daniel Barkalowb02bd652008-02-18 22:56:08 -05001126void pp_remainder(enum cmit_fmt fmt,
1127 const char **msg_p,
1128 struct strbuf *sb,
1129 int indent)
Johannes Schindelin93fc05e2007-11-04 19:15:06 +00001130{
1131 int first = 1;
1132 for (;;) {
1133 const char *line = *msg_p;
1134 int linelen = get_one_line(line);
1135 *msg_p += linelen;
1136
1137 if (!linelen)
1138 break;
1139
1140 if (is_empty_line(line, &linelen)) {
1141 if (first)
1142 continue;
1143 if (fmt == CMIT_FMT_SHORT)
1144 break;
1145 }
1146 first = 0;
1147
1148 strbuf_grow(sb, linelen + indent + 20);
1149 if (indent) {
1150 memset(sb->buf + sb->len, ' ', indent);
1151 strbuf_setlen(sb, sb->len + indent);
1152 }
1153 strbuf_add(sb, line, linelen);
1154 strbuf_addch(sb, '\n');
1155 }
1156}
1157
Alexander Gavrilov69cd8f62008-10-22 00:55:57 +04001158char *reencode_commit_message(const struct commit *commit, const char **encoding_p)
1159{
1160 const char *encoding;
1161
1162 encoding = (git_log_output_encoding
1163 ? git_log_output_encoding
1164 : git_commit_encoding);
1165 if (!encoding)
Brandon Casey330db182009-05-18 18:44:39 -05001166 encoding = "UTF-8";
Alexander Gavrilov69cd8f62008-10-22 00:55:57 +04001167 if (encoding_p)
1168 *encoding_p = encoding;
1169 return logmsg_reencode(commit, encoding);
1170}
1171
Johannes Schindelin93fc05e2007-11-04 19:15:06 +00001172void pretty_print_commit(enum cmit_fmt fmt, const struct commit *commit,
Thomas Rastdd2e7942009-10-19 17:48:08 +02001173 struct strbuf *sb,
1174 const struct pretty_print_context *context)
Johannes Schindelin93fc05e2007-11-04 19:15:06 +00001175{
1176 unsigned long beginning_of_body;
1177 int indent = 4;
1178 const char *msg = commit->buffer;
1179 char *reencoded;
1180 const char *encoding;
Thomas Rastdd2e7942009-10-19 17:48:08 +02001181 int need_8bit_cte = context->need_8bit_cte;
Johannes Schindelin93fc05e2007-11-04 19:15:06 +00001182
1183 if (fmt == CMIT_FMT_USERFORMAT) {
Thomas Rastdd2e7942009-10-19 17:48:08 +02001184 format_commit_message(commit, user_format, sb, context);
Johannes Schindelin93fc05e2007-11-04 19:15:06 +00001185 return;
1186 }
1187
Alexander Gavrilov69cd8f62008-10-22 00:55:57 +04001188 reencoded = reencode_commit_message(commit, &encoding);
Johannes Schindelin93fc05e2007-11-04 19:15:06 +00001189 if (reencoded) {
1190 msg = reencoded;
1191 }
1192
1193 if (fmt == CMIT_FMT_ONELINE || fmt == CMIT_FMT_EMAIL)
1194 indent = 0;
1195
Junio C Hamano6bf4f1b2008-03-14 17:10:09 -07001196 /*
1197 * We need to check and emit Content-type: to mark it
1198 * as 8-bit if we haven't done so.
Johannes Schindelin93fc05e2007-11-04 19:15:06 +00001199 */
Junio C Hamano6bf4f1b2008-03-14 17:10:09 -07001200 if (fmt == CMIT_FMT_EMAIL && need_8bit_cte == 0) {
Johannes Schindelin93fc05e2007-11-04 19:15:06 +00001201 int i, ch, in_body;
1202
1203 for (in_body = i = 0; (ch = msg[i]); i++) {
1204 if (!in_body) {
1205 /* author could be non 7-bit ASCII but
1206 * the log may be so; skip over the
1207 * header part first.
1208 */
1209 if (ch == '\n' && msg[i+1] == '\n')
1210 in_body = 1;
1211 }
1212 else if (non_ascii(ch)) {
Junio C Hamano6bf4f1b2008-03-14 17:10:09 -07001213 need_8bit_cte = 1;
Johannes Schindelin93fc05e2007-11-04 19:15:06 +00001214 break;
1215 }
1216 }
1217 }
1218
Thomas Rastdd2e7942009-10-19 17:48:08 +02001219 pp_header(fmt, context->abbrev, context->date_mode, encoding,
1220 commit, &msg, sb);
1221 if (fmt != CMIT_FMT_ONELINE && !context->subject) {
Johannes Schindelin93fc05e2007-11-04 19:15:06 +00001222 strbuf_addch(sb, '\n');
1223 }
1224
1225 /* Skip excess blank lines at the beginning of body, if any... */
René Scharfea0109662008-12-27 01:32:49 +01001226 msg = skip_empty_lines(msg);
Johannes Schindelin93fc05e2007-11-04 19:15:06 +00001227
1228 /* These formats treat the title line specially. */
1229 if (fmt == CMIT_FMT_ONELINE || fmt == CMIT_FMT_EMAIL)
Thomas Rastdd2e7942009-10-19 17:48:08 +02001230 pp_title_line(fmt, &msg, sb, context->subject,
1231 context->after_subject, encoding, need_8bit_cte);
Johannes Schindelin93fc05e2007-11-04 19:15:06 +00001232
1233 beginning_of_body = sb->len;
1234 if (fmt != CMIT_FMT_ONELINE)
1235 pp_remainder(fmt, &msg, sb, indent);
1236 strbuf_rtrim(sb);
1237
1238 /* Make sure there is an EOLN for the non-oneline case */
1239 if (fmt != CMIT_FMT_ONELINE)
1240 strbuf_addch(sb, '\n');
1241
1242 /*
1243 * The caller may append additional body text in e-mail
1244 * format. Make sure we did not strip the blank line
1245 * between the header and the body.
1246 */
1247 if (fmt == CMIT_FMT_EMAIL && sb->len <= beginning_of_body)
1248 strbuf_addch(sb, '\n');
Johannes Schindelina97a7462009-10-09 12:21:57 +02001249
Junio C Hamano66b2ed02010-01-20 13:59:36 -08001250 if (context->show_notes)
Thomas Rast894a9d32010-03-12 18:04:26 +01001251 format_display_notes(commit->object.sha1, sb, encoding,
1252 NOTES_SHOW_HEADER | NOTES_INDENT);
Johannes Schindelina97a7462009-10-09 12:21:57 +02001253
Johannes Schindelin93fc05e2007-11-04 19:15:06 +00001254 free(reencoded);
1255}