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