Merge branch 'maint-1.5.6' into maint-1.6.0
[git/git.git] / builtin-log.c
CommitLineData
70827b15
LT
1/*
2 * Builtin "git log" and related commands (show, whatchanged)
3 *
4 * (C) Copyright 2006 Linus Torvalds
5 * 2006 Junio Hamano
6 */
7#include "cache.h"
6b2f2d98 8#include "color.h"
70827b15
LT
9#include "commit.h"
10#include "diff.h"
11#include "revision.h"
12#include "log-tree.h"
91efcf60 13#include "builtin.h"
5d7eeee2 14#include "tag.h"
cf39f54e 15#include "reflog-walk.h"
5d23e133 16#include "patch-ids.h"
ca135e7a 17#include "refs.h"
a5a27c79 18#include "run-command.h"
2bda2cf4 19#include "shortlog.h"
70827b15 20
dd0ffd5b
HO
21/* Set a default date-time format for git log ("log.date" config variable) */
22static const char *default_date_mode = NULL;
23
0f03ca94 24static int default_show_root = 1;
d54276f2 25static const char *fmt_patch_subject_prefix = "PATCH";
94c22a5e 26static const char *fmt_pretty;
0f03ca94 27
ca135e7a
LT
28static void add_name_decoration(const char *prefix, const char *name, struct object *obj)
29{
30 int plen = strlen(prefix);
31 int nlen = strlen(name);
32 struct name_decoration *res = xmalloc(sizeof(struct name_decoration) + plen + nlen);
33 memcpy(res->name, prefix, plen);
34 memcpy(res->name + plen, name, nlen + 1);
35 res->next = add_decoration(&name_decoration, obj, res);
36}
37
38static int add_ref_decoration(const char *refname, const unsigned char *sha1, int flags, void *cb_data)
39{
40 struct object *obj = parse_object(sha1);
41 if (!obj)
42 return 0;
43 add_name_decoration("", refname, obj);
44 while (obj->type == OBJ_TAG) {
45 obj = ((struct tag *)obj)->tagged;
46 if (!obj)
47 break;
48 add_name_decoration("tag: ", refname, obj);
49 }
50 return 0;
51}
52
a633fca0 53static void cmd_log_init(int argc, const char **argv, const char *prefix,
70827b15
LT
54 struct rev_info *rev)
55{
52883fbd 56 int i;
ca135e7a 57 int decorate = 0;
52883fbd 58
70827b15
LT
59 rev->abbrev = DEFAULT_ABBREV;
60 rev->commit_format = CMIT_FMT_DEFAULT;
94c22a5e 61 if (fmt_pretty)
4da45bef 62 get_commit_format(fmt_pretty, rev);
70827b15 63 rev->verbose_header = 1;
8f67f8ae 64 DIFF_OPT_SET(&rev->diffopt, RECURSIVE);
0f03ca94 65 rev->show_root_diff = default_show_root;
d54276f2 66 rev->subject_prefix = fmt_patch_subject_prefix;
dd0ffd5b
HO
67
68 if (default_date_mode)
69 rev->date_mode = parse_date_format(default_date_mode);
70
70827b15 71 argc = setup_revisions(argc, argv, rev, "HEAD");
dd0ffd5b 72
9dafea26
TH
73 if (rev->diffopt.pickaxe || rev->diffopt.filter)
74 rev->always_show_header = 0;
8f67f8ae 75 if (DIFF_OPT_TST(&rev->diffopt, FOLLOW_RENAMES)) {
750f7b66
LT
76 rev->always_show_header = 0;
77 if (rev->diffopt.nr_paths != 1)
78 usage("git logs can only follow renames on one pathname at a time");
79 }
52883fbd
JH
80 for (i = 1; i < argc; i++) {
81 const char *arg = argv[i];
66688334 82 if (!strcmp(arg, "--decorate")) {
ca135e7a
LT
83 if (!decorate)
84 for_each_ref(add_ref_decoration, NULL);
85 decorate = 1;
86 } else
52883fbd
JH
87 die("unrecognized argument: %s", arg);
88 }
9dafea26
TH
89}
90
252a7c02
LT
91/*
92 * This gives a rough estimate for how many commits we
93 * will print out in the list.
94 */
95static int estimate_commit_count(struct rev_info *rev, struct commit_list *list)
96{
97 int n = 0;
98
99 while (list) {
100 struct commit *commit = list->item;
101 unsigned int flags = commit->object.flags;
252a7c02 102 list = list->next;
7dc0fe3b 103 if (!(flags & (TREESAME | UNINTERESTING)))
53b2c823 104 n++;
252a7c02
LT
105 }
106 return n;
107}
108
109static void show_early_header(struct rev_info *rev, const char *stage, int nr)
110{
111 if (rev->shown_one) {
112 rev->shown_one = 0;
113 if (rev->commit_format != CMIT_FMT_ONELINE)
114 putchar(rev->diffopt.line_termination);
115 }
116 printf("Final output: %d %s\n", nr, stage);
117}
118
119struct itimerval early_output_timer;
120
cdcefbc9
LT
121static void log_show_early(struct rev_info *revs, struct commit_list *list)
122{
123 int i = revs->early_output;
252a7c02 124 int show_header = 1;
cdcefbc9
LT
125
126 sort_in_topological_order(&list, revs->lifo);
127 while (list && i) {
128 struct commit *commit = list->item;
252a7c02
LT
129 switch (simplify_commit(revs, commit)) {
130 case commit_show:
131 if (show_header) {
132 int n = estimate_commit_count(revs, list);
133 show_early_header(revs, "incomplete", n);
134 show_header = 0;
135 }
136 log_tree_commit(revs, commit);
137 i--;
138 break;
139 case commit_ignore:
140 break;
141 case commit_error:
142 return;
143 }
cdcefbc9 144 list = list->next;
cdcefbc9 145 }
252a7c02
LT
146
147 /* Did we already get enough commits for the early output? */
148 if (!i)
149 return;
150
151 /*
152 * ..if no, then repeat it twice a second until we
153 * do.
154 *
155 * NOTE! We don't use "it_interval", because if the
156 * reader isn't listening, we want our output to be
157 * throttled by the writing, and not have the timer
158 * trigger every second even if we're blocked on a
159 * reader!
160 */
161 early_output_timer.it_value.tv_sec = 0;
162 early_output_timer.it_value.tv_usec = 500000;
163 setitimer(ITIMER_REAL, &early_output_timer, NULL);
cdcefbc9
LT
164}
165
166static void early_output(int signal)
167{
168 show_early_output = log_show_early;
169}
170
171static void setup_early_output(struct rev_info *rev)
172{
173 struct sigaction sa;
cdcefbc9
LT
174
175 /*
176 * Set up the signal handler, minimally intrusively:
177 * we only set a single volatile integer word (not
178 * using sigatomic_t - trying to avoid unnecessary
179 * system dependencies and headers), and using
180 * SA_RESTART.
181 */
182 memset(&sa, 0, sizeof(sa));
183 sa.sa_handler = early_output;
184 sigemptyset(&sa.sa_mask);
185 sa.sa_flags = SA_RESTART;
186 sigaction(SIGALRM, &sa, NULL);
187
188 /*
189 * If we can get the whole output in less than a
190 * tenth of a second, don't even bother doing the
191 * early-output thing..
192 *
193 * This is a one-time-only trigger.
194 */
252a7c02
LT
195 early_output_timer.it_value.tv_sec = 0;
196 early_output_timer.it_value.tv_usec = 100000;
197 setitimer(ITIMER_REAL, &early_output_timer, NULL);
cdcefbc9
LT
198}
199
200static void finish_early_output(struct rev_info *rev)
201{
252a7c02 202 int n = estimate_commit_count(rev, rev->commits);
cdcefbc9 203 signal(SIGALRM, SIG_IGN);
252a7c02 204 show_early_header(rev, "done", n);
cdcefbc9
LT
205}
206
9dafea26
TH
207static int cmd_log_walk(struct rev_info *rev)
208{
209 struct commit *commit;
70827b15 210
cdcefbc9
LT
211 if (rev->early_output)
212 setup_early_output(rev);
213
3d51e1b5
MK
214 if (prepare_revision_walk(rev))
215 die("revision walk setup failed");
cdcefbc9
LT
216
217 if (rev->early_output)
218 finish_early_output(rev);
219
70827b15
LT
220 while ((commit = get_revision(rev)) != NULL) {
221 log_tree_commit(rev, commit);
a6c73064
JS
222 if (!rev->reflog_info) {
223 /* we allow cycles in reflog ancestry */
224 free(commit->buffer);
225 commit->buffer = NULL;
226 }
cb115748
LT
227 free_commit_list(commit->parents);
228 commit->parents = NULL;
70827b15
LT
229 }
230 return 0;
231}
232
ef90d6d4 233static int git_log_config(const char *var, const char *value, void *cb)
0f03ca94 234{
94c22a5e
CR
235 if (!strcmp(var, "format.pretty"))
236 return git_config_string(&fmt_pretty, var, value);
70cff3ac
BH
237 if (!strcmp(var, "format.subjectprefix"))
238 return git_config_string(&fmt_patch_subject_prefix, var, value);
dd0ffd5b
HO
239 if (!strcmp(var, "log.date"))
240 return git_config_string(&default_date_mode, var, value);
0f03ca94
PB
241 if (!strcmp(var, "log.showroot")) {
242 default_show_root = git_config_bool(var, value);
243 return 0;
244 }
ef90d6d4 245 return git_diff_ui_config(var, value, cb);
0f03ca94
PB
246}
247
a633fca0 248int cmd_whatchanged(int argc, const char **argv, const char *prefix)
70827b15
LT
249{
250 struct rev_info rev;
251
ef90d6d4 252 git_config(git_log_config, NULL);
6b2f2d98
MK
253
254 if (diff_use_color_default == -1)
255 diff_use_color_default = git_use_color_default;
256
db6296a5 257 init_revisions(&rev, prefix);
70827b15 258 rev.diff = 1;
9202434c 259 rev.simplify_history = 0;
a633fca0 260 cmd_log_init(argc, argv, prefix, &rev);
9dafea26
TH
261 if (!rev.diffopt.output_format)
262 rev.diffopt.output_format = DIFF_FORMAT_RAW;
263 return cmd_log_walk(&rev);
70827b15
LT
264}
265
56122ed8
JS
266static void show_tagger(char *buf, int len, struct rev_info *rev)
267{
268 char *email_end, *p;
269 unsigned long date;
270 int tz;
271
272 email_end = memchr(buf, '>', len);
273 if (!email_end)
274 return;
275 p = ++email_end;
276 while (isspace(*p))
277 p++;
278 date = strtoul(p, &p, 10);
279 while (isspace(*p))
280 p++;
281 tz = (int)strtol(p, NULL, 10);
282 printf("Tagger: %.*s\nDate: %s\n", (int)(email_end - buf), buf,
283 show_date(date, tz, rev->date_mode));
284}
285
286static int show_object(const unsigned char *sha1, int show_tag_object,
287 struct rev_info *rev)
5d7eeee2
JS
288{
289 unsigned long size;
21666f1a
NP
290 enum object_type type;
291 char *buf = read_sha1_file(sha1, &type, &size);
5d7eeee2
JS
292 int offset = 0;
293
294 if (!buf)
295 return error("Could not read object %s", sha1_to_hex(sha1));
296
56122ed8
JS
297 if (show_tag_object)
298 while (offset < size && buf[offset] != '\n') {
299 int new_offset = offset + 1;
5d7eeee2
JS
300 while (new_offset < size && buf[new_offset++] != '\n')
301 ; /* do nothing */
56122ed8
JS
302 if (!prefixcmp(buf + offset, "tagger "))
303 show_tagger(buf + offset + 7,
304 new_offset - offset - 7, rev);
5d7eeee2
JS
305 offset = new_offset;
306 }
307
308 if (offset < size)
309 fwrite(buf + offset, size - offset, 1, stdout);
310 free(buf);
311 return 0;
312}
313
314static int show_tree_object(const unsigned char *sha1,
315 const char *base, int baselen,
671f0707 316 const char *pathname, unsigned mode, int stage, void *context)
5d7eeee2
JS
317{
318 printf("%s%s\n", pathname, S_ISDIR(mode) ? "/" : "");
319 return 0;
320}
321
a633fca0 322int cmd_show(int argc, const char **argv, const char *prefix)
70827b15
LT
323{
324 struct rev_info rev;
5d7eeee2
JS
325 struct object_array_entry *objects;
326 int i, count, ret = 0;
70827b15 327
ef90d6d4 328 git_config(git_log_config, NULL);
6b2f2d98
MK
329
330 if (diff_use_color_default == -1)
331 diff_use_color_default = git_use_color_default;
332
db6296a5 333 init_revisions(&rev, prefix);
70827b15 334 rev.diff = 1;
70827b15
LT
335 rev.combine_merges = 1;
336 rev.dense_combined_merges = 1;
337 rev.always_show_header = 1;
338 rev.ignore_merges = 0;
339 rev.no_walk = 1;
a633fca0 340 cmd_log_init(argc, argv, prefix, &rev);
5d7eeee2
JS
341
342 count = rev.pending.nr;
343 objects = rev.pending.objects;
344 for (i = 0; i < count && !ret; i++) {
345 struct object *o = objects[i].item;
346 const char *name = objects[i].name;
347 switch (o->type) {
348 case OBJ_BLOB:
56122ed8 349 ret = show_object(o->sha1, 0, NULL);
5d7eeee2
JS
350 break;
351 case OBJ_TAG: {
352 struct tag *t = (struct tag *)o;
353
56122ed8 354 printf("%stag %s%s\n",
8f67f8ae 355 diff_get_color_opt(&rev.diffopt, DIFF_COMMIT),
5d7eeee2 356 t->tag,
8f67f8ae 357 diff_get_color_opt(&rev.diffopt, DIFF_RESET));
56122ed8 358 ret = show_object(o->sha1, 1, &rev);
d2dadfe8
JH
359 if (ret)
360 break;
361 o = parse_object(t->tagged->sha1);
362 if (!o)
363 ret = error("Could not read object %s",
364 sha1_to_hex(t->tagged->sha1));
365 objects[i].item = o;
5d7eeee2
JS
366 i--;
367 break;
368 }
369 case OBJ_TREE:
370 printf("%stree %s%s\n\n",
8f67f8ae 371 diff_get_color_opt(&rev.diffopt, DIFF_COMMIT),
5d7eeee2 372 name,
8f67f8ae 373 diff_get_color_opt(&rev.diffopt, DIFF_RESET));
5d7eeee2 374 read_tree_recursive((struct tree *)o, "", 0, 0, NULL,
671f0707 375 show_tree_object, NULL);
5d7eeee2
JS
376 break;
377 case OBJ_COMMIT:
378 rev.pending.nr = rev.pending.alloc = 0;
379 rev.pending.objects = NULL;
380 add_object_array(o, name, &rev.pending);
381 ret = cmd_log_walk(&rev);
382 break;
383 default:
384 ret = error("Unknown type: %d", o->type);
385 }
386 }
387 free(objects);
388 return ret;
70827b15
LT
389}
390
cf39f54e
LT
391/*
392 * This is equivalent to "git log -g --abbrev-commit --pretty=oneline"
393 */
394int cmd_log_reflog(int argc, const char **argv, const char *prefix)
395{
396 struct rev_info rev;
397
ef90d6d4 398 git_config(git_log_config, NULL);
6b2f2d98
MK
399
400 if (diff_use_color_default == -1)
401 diff_use_color_default = git_use_color_default;
402
cf39f54e
LT
403 init_revisions(&rev, prefix);
404 init_reflog_walk(&rev.reflog_info);
405 rev.abbrev_commit = 1;
406 rev.verbose_header = 1;
407 cmd_log_init(argc, argv, prefix, &rev);
408
409 /*
410 * This means that we override whatever commit format the user gave
411 * on the cmd line. Sad, but cmd_log_init() currently doesn't
412 * allow us to set a different default.
413 */
414 rev.commit_format = CMIT_FMT_ONELINE;
4da45bef 415 rev.use_terminator = 1;
cf39f54e
LT
416 rev.always_show_header = 1;
417
418 /*
419 * We get called through "git reflog", so unlike the other log
420 * routines, we need to set up our pager manually..
421 */
422 setup_pager();
423
424 return cmd_log_walk(&rev);
425}
426
a633fca0 427int cmd_log(int argc, const char **argv, const char *prefix)
70827b15
LT
428{
429 struct rev_info rev;
430
ef90d6d4 431 git_config(git_log_config, NULL);
6b2f2d98
MK
432
433 if (diff_use_color_default == -1)
434 diff_use_color_default = git_use_color_default;
435
db6296a5 436 init_revisions(&rev, prefix);
70827b15 437 rev.always_show_header = 1;
a633fca0 438 cmd_log_init(argc, argv, prefix, &rev);
9dafea26 439 return cmd_log_walk(&rev);
70827b15 440}
91efcf60 441
c06d2daa
RR
442/* format-patch */
443#define FORMAT_PATCH_NAME_MAX 64
444
0377db77
JS
445static int istitlechar(char c)
446{
447 return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') ||
448 (c >= '0' && c <= '9') || c == '.' || c == '_';
449}
450
917a8f89 451static const char *fmt_patch_suffix = ".patch";
49604a4d
BG
452static int numbered = 0;
453static int auto_number = 0;
20ff0680 454
3ee79d9f
DB
455static char **extra_hdr;
456static int extra_hdr_nr;
457static int extra_hdr_alloc;
458
459static char **extra_to;
460static int extra_to_nr;
461static int extra_to_alloc;
462
463static char **extra_cc;
464static int extra_cc_nr;
465static int extra_cc_alloc;
466
467static void add_header(const char *value)
468{
469 int len = strlen(value);
c8c4450e 470 while (len && value[len - 1] == '\n')
3ee79d9f
DB
471 len--;
472 if (!strncasecmp(value, "to: ", 4)) {
473 ALLOC_GROW(extra_to, extra_to_nr + 1, extra_to_alloc);
474 extra_to[extra_to_nr++] = xstrndup(value + 4, len - 4);
475 return;
476 }
477 if (!strncasecmp(value, "cc: ", 4)) {
478 ALLOC_GROW(extra_cc, extra_cc_nr + 1, extra_cc_alloc);
479 extra_cc[extra_cc_nr++] = xstrndup(value + 4, len - 4);
480 return;
481 }
482 ALLOC_GROW(extra_hdr, extra_hdr_nr + 1, extra_hdr_alloc);
483 extra_hdr[extra_hdr_nr++] = xstrndup(value, len);
484}
485
ef90d6d4 486static int git_format_config(const char *var, const char *value, void *cb)
20ff0680
JS
487{
488 if (!strcmp(var, "format.headers")) {
d7fb91c6
JH
489 if (!value)
490 die("format.headers without value");
3ee79d9f 491 add_header(value);
20ff0680
JS
492 return 0;
493 }
70cff3ac
BH
494 if (!strcmp(var, "format.suffix"))
495 return git_config_string(&fmt_patch_suffix, var, value);
fe8928e6
MV
496 if (!strcmp(var, "format.cc")) {
497 if (!value)
498 return config_error_nonbool(var);
499 ALLOC_GROW(extra_cc, extra_cc_nr + 1, extra_cc_alloc);
500 extra_cc[extra_cc_nr++] = xstrdup(value);
501 return 0;
502 }
a159ca0c 503 if (!strcmp(var, "diff.color") || !strcmp(var, "color.diff")) {
f3aafa4d
RA
504 return 0;
505 }
49604a4d 506 if (!strcmp(var, "format.numbered")) {
90f5c186 507 if (value && !strcasecmp(value, "auto")) {
49604a4d
BG
508 auto_number = 1;
509 return 0;
510 }
49604a4d
BG
511 numbered = git_config_bool(var, value);
512 return 0;
513 }
dbd21447 514
ef90d6d4 515 return git_log_config(var, value, cb);
20ff0680
JS
516}
517
518
a5a27c79
DB
519static const char *get_oneline_for_filename(struct commit *commit,
520 int keep_subject)
521{
522 static char filename[PATH_MAX];
523 char *sol;
524 int len = 0;
525 int suffix_len = strlen(fmt_patch_suffix) + 1;
526
527 sol = strstr(commit->buffer, "\n\n");
528 if (!sol)
529 filename[0] = '\0';
530 else {
531 int j, space = 0;
532
533 sol += 2;
534 /* strip [PATCH] or [PATCH blabla] */
535 if (!keep_subject && !prefixcmp(sol, "[PATCH")) {
536 char *eos = strchr(sol + 6, ']');
537 if (eos) {
538 while (isspace(*eos))
539 eos++;
540 sol = eos;
541 }
542 }
543
544 for (j = 0;
545 j < FORMAT_PATCH_NAME_MAX - suffix_len - 5 &&
546 len < sizeof(filename) - suffix_len &&
547 sol[j] && sol[j] != '\n';
548 j++) {
549 if (istitlechar(sol[j])) {
550 if (space) {
551 filename[len++] = '-';
552 space = 0;
553 }
554 filename[len++] = sol[j];
555 if (sol[j] == '.')
556 while (sol[j + 1] == '.')
557 j++;
558 } else
559 space = 1;
560 }
561 while (filename[len - 1] == '.'
562 || filename[len - 1] == '-')
563 len--;
564 filename[len] = '\0';
565 }
566 return filename;
567}
568
81f3a188 569static FILE *realstdout = NULL;
efd02016 570static const char *output_directory = NULL;
81f3a188 571
a5a27c79 572static int reopen_stdout(const char *oneline, int nr, int total)
0377db77 573{
c06d2daa 574 char filename[PATH_MAX];
2448482b 575 int len = 0;
c06d2daa 576 int suffix_len = strlen(fmt_patch_suffix) + 1;
0377db77 577
2448482b 578 if (output_directory) {
a5a27c79
DB
579 len = snprintf(filename, sizeof(filename), "%s",
580 output_directory);
581 if (len >=
c06d2daa
RR
582 sizeof(filename) - FORMAT_PATCH_NAME_MAX - suffix_len)
583 return error("name of output directory is too long");
2448482b
JS
584 if (filename[len - 1] != '/')
585 filename[len++] = '/';
586 }
0377db77 587
a5a27c79
DB
588 if (!oneline)
589 len += sprintf(filename + len, "%d", nr);
590 else {
591 len += sprintf(filename + len, "%04d-", nr);
592 len += snprintf(filename + len, sizeof(filename) - len - 1
593 - suffix_len, "%s", oneline);
e6ff0f42 594 strcpy(filename + len, fmt_patch_suffix);
0377db77 595 }
e6ff0f42 596
81f3a188 597 fprintf(realstdout, "%s\n", filename);
c06d2daa
RR
598 if (freopen(filename, "w", stdout) == NULL)
599 return error("Cannot open patch file %s",filename);
c06d2daa 600
e6ff0f42 601 return 0;
0377db77
JS
602}
603
5d23e133 604static void get_patch_ids(struct rev_info *rev, struct patch_ids *ids, const char *prefix)
9c6efa36
JS
605{
606 struct rev_info check_rev;
607 struct commit *commit;
608 struct object *o1, *o2;
609 unsigned flags1, flags2;
9c6efa36
JS
610
611 if (rev->pending.nr != 2)
612 die("Need exactly one range.");
613
614 o1 = rev->pending.objects[0].item;
615 flags1 = o1->flags;
616 o2 = rev->pending.objects[1].item;
617 flags2 = o2->flags;
618
619 if ((flags1 & UNINTERESTING) == (flags2 & UNINTERESTING))
620 die("Not a range.");
621
5d23e133 622 init_patch_ids(ids);
9c6efa36
JS
623
624 /* given a range a..b get all patch ids for b..a */
db6296a5 625 init_revisions(&check_rev, prefix);
9c6efa36
JS
626 o1->flags ^= UNINTERESTING;
627 o2->flags ^= UNINTERESTING;
628 add_pending_object(&check_rev, o1, "o1");
629 add_pending_object(&check_rev, o2, "o2");
3d51e1b5
MK
630 if (prepare_revision_walk(&check_rev))
631 die("revision walk setup failed");
9c6efa36
JS
632
633 while ((commit = get_revision(&check_rev)) != NULL) {
634 /* ignore merges */
635 if (commit->parents && commit->parents->next)
636 continue;
637
5d23e133 638 add_commit_patch_id(commit, ids);
9c6efa36
JS
639 }
640
641 /* reset for next revision walk */
81db0941
JS
642 clear_commit_marks((struct commit *)o1,
643 SEEN | UNINTERESTING | SHOWN | ADDED);
644 clear_commit_marks((struct commit *)o2,
645 SEEN | UNINTERESTING | SHOWN | ADDED);
9c6efa36
JS
646 o1->flags = flags1;
647 o2->flags = flags2;
648}
649
e1a37346 650static void gen_message_id(struct rev_info *info, char *base)
d1566f78 651{
774751a8 652 const char *committer = git_committer_info(IDENT_WARN_ON_NO_NAME);
d1566f78
JT
653 const char *email_start = strrchr(committer, '<');
654 const char *email_end = strrchr(committer, '>');
e1a37346
DB
655 struct strbuf buf;
656 if (!email_start || !email_end || email_start > email_end - 1)
d1566f78 657 die("Could not extract email from committer identity.");
e1a37346
DB
658 strbuf_init(&buf, 0);
659 strbuf_addf(&buf, "%s.%lu.git.%.*s", base,
660 (unsigned long) time(NULL),
661 (int)(email_end - email_start - 1), email_start + 1);
662 info->message_id = strbuf_detach(&buf, NULL);
d1566f78
JT
663}
664
2bda2cf4
DB
665static void make_cover_letter(struct rev_info *rev, int use_stdout,
666 int numbered, int numbered_files,
667 struct commit *origin,
668 int nr, struct commit **list, struct commit *head)
a5a27c79
DB
669{
670 const char *committer;
39fe578b 671 char *head_sha1;
a5a27c79
DB
672 const char *subject_start = NULL;
673 const char *body = "*** SUBJECT HERE ***\n\n*** BLURB HERE ***\n";
674 const char *msg;
675 const char *extra_headers = rev->extra_headers;
2bda2cf4 676 struct shortlog log;
a5a27c79 677 struct strbuf sb;
2bda2cf4 678 int i;
a5a27c79 679 const char *encoding = "utf-8";
39fe578b 680 struct diff_options opts;
267123b4 681 int need_8bit_cte = 0;
a5a27c79
DB
682
683 if (rev->commit_format != CMIT_FMT_EMAIL)
684 die("Cover letter needs email format");
685
686 if (!use_stdout && reopen_stdout(numbered_files ?
687 NULL : "cover-letter", 0, rev->total))
688 return;
689
a5a27c79
DB
690 head_sha1 = sha1_to_hex(head->object.sha1);
691
267123b4
JH
692 log_write_email_headers(rev, head_sha1, &subject_start, &extra_headers,
693 &need_8bit_cte);
a5a27c79
DB
694
695 committer = git_committer_info(0);
696
697 msg = body;
698 strbuf_init(&sb, 0);
699 pp_user_info(NULL, CMIT_FMT_EMAIL, &sb, committer, DATE_RFC2822,
700 encoding);
701 pp_title_line(CMIT_FMT_EMAIL, &msg, &sb, subject_start, extra_headers,
267123b4 702 encoding, need_8bit_cte);
a5a27c79
DB
703 pp_remainder(CMIT_FMT_EMAIL, &msg, &sb, 0);
704 printf("%s\n", sb.buf);
705
706 strbuf_release(&sb);
707
2bda2cf4 708 shortlog_init(&log);
859c4fbe
JS
709 log.wrap_lines = 1;
710 log.wrap = 72;
711 log.in1 = 2;
712 log.in2 = 4;
2bda2cf4
DB
713 for (i = 0; i < nr; i++)
714 shortlog_add_commit(&log, list[i]);
715
716 shortlog_output(&log);
717
a5a27c79 718 /*
2bda2cf4 719 * We can only do diffstat with a unique reference point
a5a27c79
DB
720 */
721 if (!origin)
722 return;
723
5d02294c
JS
724 memcpy(&opts, &rev->diffopt, sizeof(opts));
725 opts.output_format = DIFF_FORMAT_SUMMARY | DIFF_FORMAT_DIFFSTAT;
a5a27c79 726
39fe578b
DB
727 diff_setup_done(&opts);
728
729 diff_tree_sha1(origin->tree->object.sha1,
730 head->tree->object.sha1,
731 "", &opts);
732 diffcore_std(&opts);
733 diff_flush(&opts);
a5a27c79 734
a5a27c79 735 printf("\n");
d1566f78
JT
736}
737
8419d2ee
JH
738static const char *clean_message_id(const char *msg_id)
739{
740 char ch;
741 const char *a, *z, *m;
8419d2ee
JH
742
743 m = msg_id;
744 while ((ch = *m) && (isspace(ch) || (ch == '<')))
745 m++;
746 a = m;
747 z = NULL;
748 while ((ch = *m)) {
749 if (!isspace(ch) && (ch != '>'))
750 z = m;
751 m++;
752 }
753 if (!z)
754 die("insane in-reply-to: %s", msg_id);
755 if (++z == m)
756 return a;
182af834 757 return xmemdupz(a, z - a);
8419d2ee
JH
758}
759
a633fca0 760int cmd_format_patch(int argc, const char **argv, const char *prefix)
91efcf60
JH
761{
762 struct commit *commit;
763 struct commit **list = NULL;
764 struct rev_info rev;
2448482b 765 int nr = 0, total, i, j;
0377db77 766 int use_stdout = 0;
fa0f02df 767 int start_number = -1;
8ac80a57 768 int keep_subject = 0;
e6ff0f42 769 int numbered_files = 0; /* _just_ numbers */
2d9e4a47 770 int subject_prefix = 0;
9c6efa36 771 int ignore_if_in_upstream = 0;
cc35de84 772 int thread = 0;
a5a27c79 773 int cover_letter = 0;
2bda2cf4 774 int boundary_count = 0;
37c22a4b 775 int no_binary_diff = 0;
a5a27c79 776 struct commit *origin = NULL, *head = NULL;
76af0734 777 const char *in_reply_to = NULL;
5d23e133 778 struct patch_ids ids;
cf2251b6 779 char *add_signoff = NULL;
3ee79d9f 780 struct strbuf buf;
91efcf60 781
ef90d6d4 782 git_config(git_format_config, NULL);
db6296a5 783 init_revisions(&rev, prefix);
91efcf60
JH
784 rev.commit_format = CMIT_FMT_EMAIL;
785 rev.verbose_header = 1;
786 rev.diff = 1;
91efcf60
JH
787 rev.combine_merges = 0;
788 rev.ignore_merges = 1;
8f67f8ae 789 DIFF_OPT_SET(&rev.diffopt, RECURSIVE);
91efcf60 790
dbd21447 791 rev.subject_prefix = fmt_patch_subject_prefix;
20ff0680 792
2448482b
JS
793 /*
794 * Parse the arguments before setup_revisions(), or something
2dc189a3 795 * like "git format-patch -o a123 HEAD^.." may fail; a123 is
2448482b
JS
796 * possibly a valid SHA1.
797 */
798 for (i = 1, j = 1; i < argc; i++) {
799 if (!strcmp(argv[i], "--stdout"))
0377db77 800 use_stdout = 1;
596524b3
JS
801 else if (!strcmp(argv[i], "-n") ||
802 !strcmp(argv[i], "--numbered"))
803 numbered = 1;
49604a4d
BG
804 else if (!strcmp(argv[i], "-N") ||
805 !strcmp(argv[i], "--no-numbered")) {
806 numbered = 0;
807 auto_number = 0;
808 }
cc44c765 809 else if (!prefixcmp(argv[i], "--start-number="))
fa0f02df 810 start_number = strtol(argv[i] + 15, NULL, 10);
e6ff0f42
JL
811 else if (!strcmp(argv[i], "--numbered-files"))
812 numbered_files = 1;
fa0f02df
JS
813 else if (!strcmp(argv[i], "--start-number")) {
814 i++;
815 if (i == argc)
816 die("Need a number for --start-number");
817 start_number = strtol(argv[i], NULL, 10);
cf2251b6 818 }
736cc67d
DB
819 else if (!prefixcmp(argv[i], "--cc=")) {
820 ALLOC_GROW(extra_cc, extra_cc_nr + 1, extra_cc_alloc);
821 extra_cc[extra_cc_nr++] = xstrdup(argv[i] + 5);
822 }
cf2251b6 823 else if (!strcmp(argv[i], "-k") ||
8ac80a57
JS
824 !strcmp(argv[i], "--keep-subject")) {
825 keep_subject = 1;
826 rev.total = -1;
cf2251b6 827 }
efd02016
JH
828 else if (!strcmp(argv[i], "--output-directory") ||
829 !strcmp(argv[i], "-o")) {
2448482b 830 i++;
efd02016
JH
831 if (argc <= i)
832 die("Which directory?");
833 if (output_directory)
834 die("Two output directories?");
835 output_directory = argv[i];
698ce6f8 836 }
cf2251b6
JH
837 else if (!strcmp(argv[i], "--signoff") ||
838 !strcmp(argv[i], "-s")) {
6c4cca1c
EB
839 const char *committer;
840 const char *endpos;
774751a8 841 committer = git_committer_info(IDENT_ERROR_ON_NO_NAME);
6c4cca1c 842 endpos = strchr(committer, '>');
cf2251b6 843 if (!endpos)
597faa00 844 die("bogus committer info %s\n", committer);
182af834 845 add_signoff = xmemdupz(committer, endpos - committer + 1);
cf2251b6 846 }
c112f689 847 else if (!strcmp(argv[i], "--attach")) {
698ce6f8 848 rev.mime_boundary = git_version_string;
c112f689
JS
849 rev.no_inline = 1;
850 }
851 else if (!prefixcmp(argv[i], "--attach=")) {
852 rev.mime_boundary = argv[i] + 9;
853 rev.no_inline = 1;
854 }
855 else if (!strcmp(argv[i], "--inline")) {
856 rev.mime_boundary = git_version_string;
857 rev.no_inline = 0;
858 }
859 else if (!prefixcmp(argv[i], "--inline=")) {
698ce6f8 860 rev.mime_boundary = argv[i] + 9;
c112f689
JS
861 rev.no_inline = 0;
862 }
9c6efa36
JS
863 else if (!strcmp(argv[i], "--ignore-if-in-upstream"))
864 ignore_if_in_upstream = 1;
cc35de84
JT
865 else if (!strcmp(argv[i], "--thread"))
866 thread = 1;
cc44c765 867 else if (!prefixcmp(argv[i], "--in-reply-to="))
da56645d
JT
868 in_reply_to = argv[i] + 14;
869 else if (!strcmp(argv[i], "--in-reply-to")) {
870 i++;
871 if (i == argc)
872 die("Need a Message-Id for --in-reply-to");
873 in_reply_to = argv[i];
2d9e4a47
RJ
874 } else if (!prefixcmp(argv[i], "--subject-prefix=")) {
875 subject_prefix = 1;
876 rev.subject_prefix = argv[i] + 17;
877 } else if (!prefixcmp(argv[i], "--suffix="))
03eeaeae 878 fmt_patch_suffix = argv[i] + 9;
a5a27c79
DB
879 else if (!strcmp(argv[i], "--cover-letter"))
880 cover_letter = 1;
37c22a4b
CMOF
881 else if (!strcmp(argv[i], "--no-binary"))
882 no_binary_diff = 1;
698ce6f8 883 else
2448482b 884 argv[j++] = argv[i];
0377db77 885 }
2448482b
JS
886 argc = j;
887
3ee79d9f
DB
888 strbuf_init(&buf, 0);
889
890 for (i = 0; i < extra_hdr_nr; i++) {
891 strbuf_addstr(&buf, extra_hdr[i]);
892 strbuf_addch(&buf, '\n');
893 }
894
895 if (extra_to_nr)
896 strbuf_addstr(&buf, "To: ");
897 for (i = 0; i < extra_to_nr; i++) {
898 if (i)
899 strbuf_addstr(&buf, " ");
900 strbuf_addstr(&buf, extra_to[i]);
901 if (i + 1 < extra_to_nr)
902 strbuf_addch(&buf, ',');
903 strbuf_addch(&buf, '\n');
904 }
905
906 if (extra_cc_nr)
907 strbuf_addstr(&buf, "Cc: ");
908 for (i = 0; i < extra_cc_nr; i++) {
909 if (i)
910 strbuf_addstr(&buf, " ");
911 strbuf_addstr(&buf, extra_cc[i]);
912 if (i + 1 < extra_cc_nr)
913 strbuf_addch(&buf, ',');
914 strbuf_addch(&buf, '\n');
915 }
916
917 rev.extra_headers = strbuf_detach(&buf, 0);
918
add5c8a5 919 if (start_number < 0)
fa0f02df 920 start_number = 1;
63b398a4 921 if (numbered && keep_subject)
8ac80a57 922 die ("-n and -k are mutually exclusive.");
2d9e4a47
RJ
923 if (keep_subject && subject_prefix)
924 die ("--subject-prefix and -k are mutually exclusive.");
e6ff0f42
JL
925 if (numbered_files && use_stdout)
926 die ("--numbered-files and --stdout are mutually exclusive.");
8ac80a57 927
2448482b
JS
928 argc = setup_revisions(argc, argv, &rev, "HEAD");
929 if (argc > 1)
930 die ("unrecognized argument: %s", argv[1]);
0377db77 931
c9b5ef99 932 if (!rev.diffopt.output_format)
c261e438 933 rev.diffopt.output_format = DIFF_FORMAT_DIFFSTAT | DIFF_FORMAT_SUMMARY | DIFF_FORMAT_PATCH;
c9b5ef99 934
37c22a4b 935 if (!DIFF_OPT_TST(&rev.diffopt, TEXT) && !no_binary_diff)
8f67f8ae 936 DIFF_OPT_SET(&rev.diffopt, BINARY);
e47f306d 937
90f70a91 938 if (!output_directory && !use_stdout)
77e565d8
ML
939 output_directory = prefix;
940
efd02016
JH
941 if (output_directory) {
942 if (use_stdout)
943 die("standard output, or directory, which one?");
944 if (mkdir(output_directory, 0777) < 0 && errno != EEXIST)
945 die("Could not create directory %s",
946 output_directory);
947 }
948
1f1e895f 949 if (rev.pending.nr == 1) {
8a1d076e
JH
950 if (rev.max_count < 0 && !rev.show_root_diff) {
951 /*
952 * This is traditional behaviour of "git format-patch
953 * origin" that prepares what the origin side still
954 * does not have.
955 */
7c496280 956 rev.pending.objects[0].item->flags |= UNINTERESTING;
3384a2df 957 add_head_to_pending(&rev);
7c496280 958 }
8a1d076e
JH
959 /*
960 * Otherwise, it is "format-patch -22 HEAD", and/or
961 * "format-patch --root HEAD". The user wants
962 * get_revision() to do the usual traversal.
7c496280 963 */
e686eb98 964 }
a5a27c79
DB
965 if (cover_letter) {
966 /* remember the range */
a5a27c79
DB
967 int i;
968 for (i = 0; i < rev.pending.nr; i++) {
969 struct object *o = rev.pending.objects[i].item;
2bda2cf4 970 if (!(o->flags & UNINTERESTING))
a5a27c79
DB
971 head = (struct commit *)o;
972 }
a5a27c79
DB
973 /* We can't generate a cover letter without any patches */
974 if (!head)
975 return 0;
976 }
e686eb98 977
9c6efa36 978 if (ignore_if_in_upstream)
5d23e133 979 get_patch_ids(&rev, &ids, prefix);
9c6efa36 980
81f3a188 981 if (!use_stdout)
f5788250 982 realstdout = xfdopen(xdup(1), "w");
81f3a188 983
3d51e1b5
MK
984 if (prepare_revision_walk(&rev))
985 die("revision walk setup failed");
2bda2cf4 986 rev.boundary = 1;
91efcf60 987 while ((commit = get_revision(&rev)) != NULL) {
2bda2cf4 988 if (commit->object.flags & BOUNDARY) {
2bda2cf4
DB
989 boundary_count++;
990 origin = (boundary_count == 1) ? commit : NULL;
991 continue;
992 }
993
0377db77
JS
994 /* ignore merges */
995 if (commit->parents && commit->parents->next)
996 continue;
9c6efa36
JS
997
998 if (ignore_if_in_upstream &&
5d23e133 999 has_commit_patch_id(commit, &ids))
9c6efa36
JS
1000 continue;
1001
91efcf60 1002 nr++;
83572c1a 1003 list = xrealloc(list, nr * sizeof(list[0]));
91efcf60
JH
1004 list[nr - 1] = commit;
1005 }
0377db77 1006 total = nr;
49604a4d
BG
1007 if (!keep_subject && auto_number && total > 1)
1008 numbered = 1;
596524b3 1009 if (numbered)
fa0f02df 1010 rev.total = total + start_number - 1;
8419d2ee
JH
1011 if (in_reply_to)
1012 rev.ref_message_id = clean_message_id(in_reply_to);
a5a27c79
DB
1013 if (cover_letter) {
1014 if (thread)
1015 gen_message_id(&rev, "cover");
1016 make_cover_letter(&rev, use_stdout, numbered, numbered_files,
2bda2cf4 1017 origin, nr, list, head);
a5a27c79
DB
1018 total++;
1019 start_number--;
1020 }
1021 rev.add_signoff = add_signoff;
91efcf60
JH
1022 while (0 <= --nr) {
1023 int shown;
1024 commit = list[nr];
add5c8a5 1025 rev.nr = total - nr + (start_number - 1);
d1566f78 1026 /* Make the second and subsequent mails replies to the first */
cc35de84 1027 if (thread) {
a5a27c79 1028 /* Have we already had a message ID? */
e1a37346 1029 if (rev.message_id) {
a5a27c79
DB
1030 /*
1031 * If we've got the ID to be a reply
1032 * to, discard the current ID;
1033 * otherwise, make everything a reply
1034 * to that.
1035 */
e1a37346
DB
1036 if (rev.ref_message_id)
1037 free(rev.message_id);
1038 else
1039 rev.ref_message_id = rev.message_id;
cc35de84 1040 }
e1a37346 1041 gen_message_id(&rev, sha1_to_hex(commit->object.sha1));
d1566f78 1042 }
a5a27c79
DB
1043 if (!use_stdout && reopen_stdout(numbered_files ? NULL :
1044 get_oneline_for_filename(commit, keep_subject),
1045 rev.nr, rev.total))
1046 die("Failed to create output files");
91efcf60
JH
1047 shown = log_tree_commit(&rev, commit);
1048 free(commit->buffer);
1049 commit->buffer = NULL;
add5c8a5
JH
1050
1051 /* We put one extra blank line between formatted
1052 * patches and this flag is used by log-tree code
1053 * to see if it needs to emit a LF before showing
1054 * the log; when using one file per patch, we do
1055 * not want the extra blank line.
1056 */
1057 if (!use_stdout)
1058 rev.shown_one = 0;
698ce6f8
JS
1059 if (shown) {
1060 if (rev.mime_boundary)
1061 printf("\n--%s%s--\n\n\n",
1062 mime_boundary_leader,
1063 rev.mime_boundary);
1064 else
1065 printf("-- \n%s\n\n", git_version_string);
1066 }
0377db77
JS
1067 if (!use_stdout)
1068 fclose(stdout);
91efcf60
JH
1069 }
1070 free(list);
5d23e133
JH
1071 if (ignore_if_in_upstream)
1072 free_patch_ids(&ids);
91efcf60
JH
1073 return 0;
1074}
1075
e827633a
RS
1076static int add_pending_commit(const char *arg, struct rev_info *revs, int flags)
1077{
1078 unsigned char sha1[20];
1079 if (get_sha1(arg, sha1) == 0) {
1080 struct commit *commit = lookup_commit_reference(sha1);
1081 if (commit) {
1082 commit->object.flags |= flags;
1083 add_pending_object(revs, &commit->object, arg);
1084 return 0;
1085 }
1086 }
1087 return -1;
1088}
1089
1090static const char cherry_usage[] =
1b1dd23f 1091"git cherry [-v] <upstream> [<head>] [<limit>]";
e827633a
RS
1092int cmd_cherry(int argc, const char **argv, const char *prefix)
1093{
1094 struct rev_info revs;
5d23e133 1095 struct patch_ids ids;
e827633a
RS
1096 struct commit *commit;
1097 struct commit_list *list = NULL;
1098 const char *upstream;
1099 const char *head = "HEAD";
1100 const char *limit = NULL;
1101 int verbose = 0;
1102
1103 if (argc > 1 && !strcmp(argv[1], "-v")) {
1104 verbose = 1;
1105 argc--;
1106 argv++;
1107 }
1108
1109 switch (argc) {
1110 case 4:
1111 limit = argv[3];
1112 /* FALLTHROUGH */
1113 case 3:
1114 head = argv[2];
1115 /* FALLTHROUGH */
1116 case 2:
1117 upstream = argv[1];
1118 break;
1119 default:
1120 usage(cherry_usage);
1121 }
1122
1123 init_revisions(&revs, prefix);
1124 revs.diff = 1;
1125 revs.combine_merges = 0;
1126 revs.ignore_merges = 1;
8f67f8ae 1127 DIFF_OPT_SET(&revs.diffopt, RECURSIVE);
e827633a
RS
1128
1129 if (add_pending_commit(head, &revs, 0))
1130 die("Unknown commit %s", head);
1131 if (add_pending_commit(upstream, &revs, UNINTERESTING))
1132 die("Unknown commit %s", upstream);
1133
1134 /* Don't say anything if head and upstream are the same. */
1135 if (revs.pending.nr == 2) {
1136 struct object_array_entry *o = revs.pending.objects;
1137 if (hashcmp(o[0].item->sha1, o[1].item->sha1) == 0)
1138 return 0;
1139 }
1140
5d23e133 1141 get_patch_ids(&revs, &ids, prefix);
e827633a
RS
1142
1143 if (limit && add_pending_commit(limit, &revs, UNINTERESTING))
1144 die("Unknown commit %s", limit);
1145
1146 /* reverse the list of commits */
3d51e1b5
MK
1147 if (prepare_revision_walk(&revs))
1148 die("revision walk setup failed");
e827633a
RS
1149 while ((commit = get_revision(&revs)) != NULL) {
1150 /* ignore merges */
1151 if (commit->parents && commit->parents->next)
1152 continue;
1153
1154 commit_list_insert(commit, &list);
1155 }
1156
1157 while (list) {
e827633a
RS
1158 char sign = '+';
1159
1160 commit = list->item;
5d23e133 1161 if (has_commit_patch_id(commit, &ids))
e827633a
RS
1162 sign = '-';
1163
1164 if (verbose) {
674d1727
PH
1165 struct strbuf buf;
1166 strbuf_init(&buf, 0);
1167 pretty_print_commit(CMIT_FMT_ONELINE, commit,
4593fb84 1168 &buf, 0, NULL, NULL, 0, 0);
e827633a 1169 printf("%c %s %s\n", sign,
674d1727
PH
1170 sha1_to_hex(commit->object.sha1), buf.buf);
1171 strbuf_release(&buf);
e827633a
RS
1172 }
1173 else {
1174 printf("%c %s\n", sign,
1175 sha1_to_hex(commit->object.sha1));
1176 }
1177
1178 list = list->next;
1179 }
1180
5d23e133 1181 free_patch_ids(&ids);
e827633a
RS
1182 return 0;
1183}