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