format-patch: track several references
[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
RR
419/* format-patch */
420#define FORMAT_PATCH_NAME_MAX 64
421
0377db77
JS
422static int istitlechar(char c)
423{
424 return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') ||
425 (c >= '0' && c <= '9') || c == '.' || c == '_';
426}
427
917a8f89 428static const char *fmt_patch_suffix = ".patch";
49604a4d 429static int numbered = 0;
a567fdcb 430static int auto_number = 1;
20ff0680 431
3ee79d9f
DB
432static char **extra_hdr;
433static int extra_hdr_nr;
434static int extra_hdr_alloc;
435
436static char **extra_to;
437static int extra_to_nr;
438static int extra_to_alloc;
439
440static char **extra_cc;
441static int extra_cc_nr;
442static int extra_cc_alloc;
443
444static void add_header(const char *value)
445{
446 int len = strlen(value);
c8c4450e 447 while (len && value[len - 1] == '\n')
3ee79d9f
DB
448 len--;
449 if (!strncasecmp(value, "to: ", 4)) {
450 ALLOC_GROW(extra_to, extra_to_nr + 1, extra_to_alloc);
451 extra_to[extra_to_nr++] = xstrndup(value + 4, len - 4);
452 return;
453 }
454 if (!strncasecmp(value, "cc: ", 4)) {
455 ALLOC_GROW(extra_cc, extra_cc_nr + 1, extra_cc_alloc);
456 extra_cc[extra_cc_nr++] = xstrndup(value + 4, len - 4);
457 return;
458 }
459 ALLOC_GROW(extra_hdr, extra_hdr_nr + 1, extra_hdr_alloc);
460 extra_hdr[extra_hdr_nr++] = xstrndup(value, len);
461}
462
ef90d6d4 463static int git_format_config(const char *var, const char *value, void *cb)
20ff0680
JS
464{
465 if (!strcmp(var, "format.headers")) {
d7fb91c6
JH
466 if (!value)
467 die("format.headers without value");
3ee79d9f 468 add_header(value);
20ff0680
JS
469 return 0;
470 }
70cff3ac
BH
471 if (!strcmp(var, "format.suffix"))
472 return git_config_string(&fmt_patch_suffix, var, value);
fe8928e6
MV
473 if (!strcmp(var, "format.cc")) {
474 if (!value)
475 return config_error_nonbool(var);
476 ALLOC_GROW(extra_cc, extra_cc_nr + 1, extra_cc_alloc);
477 extra_cc[extra_cc_nr++] = xstrdup(value);
478 return 0;
479 }
a159ca0c 480 if (!strcmp(var, "diff.color") || !strcmp(var, "color.diff")) {
f3aafa4d
RA
481 return 0;
482 }
49604a4d 483 if (!strcmp(var, "format.numbered")) {
90f5c186 484 if (value && !strcasecmp(value, "auto")) {
49604a4d
BG
485 auto_number = 1;
486 return 0;
487 }
49604a4d 488 numbered = git_config_bool(var, value);
a567fdcb 489 auto_number = auto_number && numbered;
49604a4d
BG
490 return 0;
491 }
dbd21447 492
ef90d6d4 493 return git_log_config(var, value, cb);
20ff0680
JS
494}
495
496
a5a27c79
DB
497static const char *get_oneline_for_filename(struct commit *commit,
498 int keep_subject)
499{
500 static char filename[PATH_MAX];
501 char *sol;
502 int len = 0;
503 int suffix_len = strlen(fmt_patch_suffix) + 1;
504
505 sol = strstr(commit->buffer, "\n\n");
506 if (!sol)
507 filename[0] = '\0';
508 else {
509 int j, space = 0;
510
511 sol += 2;
512 /* strip [PATCH] or [PATCH blabla] */
513 if (!keep_subject && !prefixcmp(sol, "[PATCH")) {
514 char *eos = strchr(sol + 6, ']');
515 if (eos) {
516 while (isspace(*eos))
517 eos++;
518 sol = eos;
519 }
520 }
521
522 for (j = 0;
523 j < FORMAT_PATCH_NAME_MAX - suffix_len - 5 &&
524 len < sizeof(filename) - suffix_len &&
525 sol[j] && sol[j] != '\n';
526 j++) {
527 if (istitlechar(sol[j])) {
528 if (space) {
529 filename[len++] = '-';
530 space = 0;
531 }
532 filename[len++] = sol[j];
533 if (sol[j] == '.')
534 while (sol[j + 1] == '.')
535 j++;
536 } else
537 space = 1;
538 }
539 while (filename[len - 1] == '.'
540 || filename[len - 1] == '-')
541 len--;
542 filename[len] = '\0';
543 }
544 return filename;
545}
546
81f3a188 547static FILE *realstdout = NULL;
efd02016 548static const char *output_directory = NULL;
9800a754 549static int outdir_offset;
81f3a188 550
a5a27c79 551static int reopen_stdout(const char *oneline, int nr, int total)
0377db77 552{
c06d2daa 553 char filename[PATH_MAX];
2448482b 554 int len = 0;
c06d2daa 555 int suffix_len = strlen(fmt_patch_suffix) + 1;
0377db77 556
2448482b 557 if (output_directory) {
a5a27c79
DB
558 len = snprintf(filename, sizeof(filename), "%s",
559 output_directory);
560 if (len >=
c06d2daa
RR
561 sizeof(filename) - FORMAT_PATCH_NAME_MAX - suffix_len)
562 return error("name of output directory is too long");
2448482b
JS
563 if (filename[len - 1] != '/')
564 filename[len++] = '/';
565 }
0377db77 566
a5a27c79
DB
567 if (!oneline)
568 len += sprintf(filename + len, "%d", nr);
569 else {
570 len += sprintf(filename + len, "%04d-", nr);
571 len += snprintf(filename + len, sizeof(filename) - len - 1
572 - suffix_len, "%s", oneline);
e6ff0f42 573 strcpy(filename + len, fmt_patch_suffix);
0377db77 574 }
e6ff0f42 575
9800a754 576 fprintf(realstdout, "%s\n", filename + outdir_offset);
c06d2daa
RR
577 if (freopen(filename, "w", stdout) == NULL)
578 return error("Cannot open patch file %s",filename);
c06d2daa 579
e6ff0f42 580 return 0;
0377db77
JS
581}
582
5d23e133 583static void get_patch_ids(struct rev_info *rev, struct patch_ids *ids, const char *prefix)
9c6efa36
JS
584{
585 struct rev_info check_rev;
586 struct commit *commit;
587 struct object *o1, *o2;
588 unsigned flags1, flags2;
9c6efa36
JS
589
590 if (rev->pending.nr != 2)
591 die("Need exactly one range.");
592
593 o1 = rev->pending.objects[0].item;
594 flags1 = o1->flags;
595 o2 = rev->pending.objects[1].item;
596 flags2 = o2->flags;
597
598 if ((flags1 & UNINTERESTING) == (flags2 & UNINTERESTING))
599 die("Not a range.");
600
5d23e133 601 init_patch_ids(ids);
9c6efa36
JS
602
603 /* given a range a..b get all patch ids for b..a */
db6296a5 604 init_revisions(&check_rev, prefix);
9c6efa36
JS
605 o1->flags ^= UNINTERESTING;
606 o2->flags ^= UNINTERESTING;
607 add_pending_object(&check_rev, o1, "o1");
608 add_pending_object(&check_rev, o2, "o2");
3d51e1b5
MK
609 if (prepare_revision_walk(&check_rev))
610 die("revision walk setup failed");
9c6efa36
JS
611
612 while ((commit = get_revision(&check_rev)) != NULL) {
613 /* ignore merges */
614 if (commit->parents && commit->parents->next)
615 continue;
616
5d23e133 617 add_commit_patch_id(commit, ids);
9c6efa36
JS
618 }
619
620 /* reset for next revision walk */
81db0941
JS
621 clear_commit_marks((struct commit *)o1,
622 SEEN | UNINTERESTING | SHOWN | ADDED);
623 clear_commit_marks((struct commit *)o2,
624 SEEN | UNINTERESTING | SHOWN | ADDED);
9c6efa36
JS
625 o1->flags = flags1;
626 o2->flags = flags2;
627}
628
e1a37346 629static void gen_message_id(struct rev_info *info, char *base)
d1566f78 630{
774751a8 631 const char *committer = git_committer_info(IDENT_WARN_ON_NO_NAME);
d1566f78
JT
632 const char *email_start = strrchr(committer, '<');
633 const char *email_end = strrchr(committer, '>');
f285a2d7 634 struct strbuf buf = STRBUF_INIT;
e1a37346 635 if (!email_start || !email_end || email_start > email_end - 1)
d1566f78 636 die("Could not extract email from committer identity.");
e1a37346
DB
637 strbuf_addf(&buf, "%s.%lu.git.%.*s", base,
638 (unsigned long) time(NULL),
639 (int)(email_end - email_start - 1), email_start + 1);
640 info->message_id = strbuf_detach(&buf, NULL);
d1566f78
JT
641}
642
2bda2cf4
DB
643static void make_cover_letter(struct rev_info *rev, int use_stdout,
644 int numbered, int numbered_files,
645 struct commit *origin,
646 int nr, struct commit **list, struct commit *head)
a5a27c79
DB
647{
648 const char *committer;
39fe578b 649 char *head_sha1;
a5a27c79
DB
650 const char *subject_start = NULL;
651 const char *body = "*** SUBJECT HERE ***\n\n*** BLURB HERE ***\n";
652 const char *msg;
653 const char *extra_headers = rev->extra_headers;
2bda2cf4 654 struct shortlog log;
f285a2d7 655 struct strbuf sb = STRBUF_INIT;
2bda2cf4 656 int i;
a5a27c79 657 const char *encoding = "utf-8";
39fe578b 658 struct diff_options opts;
267123b4 659 int need_8bit_cte = 0;
a5a27c79
DB
660
661 if (rev->commit_format != CMIT_FMT_EMAIL)
662 die("Cover letter needs email format");
663
664 if (!use_stdout && reopen_stdout(numbered_files ?
665 NULL : "cover-letter", 0, rev->total))
666 return;
667
a5a27c79
DB
668 head_sha1 = sha1_to_hex(head->object.sha1);
669
267123b4
JH
670 log_write_email_headers(rev, head_sha1, &subject_start, &extra_headers,
671 &need_8bit_cte);
a5a27c79
DB
672
673 committer = git_committer_info(0);
674
675 msg = body;
a5a27c79
DB
676 pp_user_info(NULL, CMIT_FMT_EMAIL, &sb, committer, DATE_RFC2822,
677 encoding);
678 pp_title_line(CMIT_FMT_EMAIL, &msg, &sb, subject_start, extra_headers,
267123b4 679 encoding, need_8bit_cte);
a5a27c79
DB
680 pp_remainder(CMIT_FMT_EMAIL, &msg, &sb, 0);
681 printf("%s\n", sb.buf);
682
683 strbuf_release(&sb);
684
2bda2cf4 685 shortlog_init(&log);
859c4fbe
JS
686 log.wrap_lines = 1;
687 log.wrap = 72;
688 log.in1 = 2;
689 log.in2 = 4;
2bda2cf4
DB
690 for (i = 0; i < nr; i++)
691 shortlog_add_commit(&log, list[i]);
692
693 shortlog_output(&log);
694
a5a27c79 695 /*
2bda2cf4 696 * We can only do diffstat with a unique reference point
a5a27c79
DB
697 */
698 if (!origin)
699 return;
700
5d02294c
JS
701 memcpy(&opts, &rev->diffopt, sizeof(opts));
702 opts.output_format = DIFF_FORMAT_SUMMARY | DIFF_FORMAT_DIFFSTAT;
a5a27c79 703
39fe578b
DB
704 diff_setup_done(&opts);
705
706 diff_tree_sha1(origin->tree->object.sha1,
707 head->tree->object.sha1,
708 "", &opts);
709 diffcore_std(&opts);
710 diff_flush(&opts);
a5a27c79 711
a5a27c79 712 printf("\n");
d1566f78
JT
713}
714
8419d2ee
JH
715static const char *clean_message_id(const char *msg_id)
716{
717 char ch;
718 const char *a, *z, *m;
8419d2ee
JH
719
720 m = msg_id;
721 while ((ch = *m) && (isspace(ch) || (ch == '<')))
722 m++;
723 a = m;
724 z = NULL;
725 while ((ch = *m)) {
726 if (!isspace(ch) && (ch != '>'))
727 z = m;
728 m++;
729 }
730 if (!z)
731 die("insane in-reply-to: %s", msg_id);
732 if (++z == m)
733 return a;
182af834 734 return xmemdupz(a, z - a);
8419d2ee
JH
735}
736
9800a754
JH
737static const char *set_outdir(const char *prefix, const char *output_directory)
738{
739 if (output_directory && is_absolute_path(output_directory))
740 return output_directory;
741
742 if (!prefix || !*prefix) {
743 if (output_directory)
744 return output_directory;
745 /* The user did not explicitly ask for "./" */
746 outdir_offset = 2;
747 return "./";
748 }
749
750 outdir_offset = strlen(prefix);
751 if (!output_directory)
752 return prefix;
753
754 return xstrdup(prefix_filename(prefix, outdir_offset,
755 output_directory));
756}
757
a633fca0 758int cmd_format_patch(int argc, const char **argv, const char *prefix)
91efcf60
JH
759{
760 struct commit *commit;
761 struct commit **list = NULL;
762 struct rev_info rev;
2448482b 763 int nr = 0, total, i, j;
0377db77 764 int use_stdout = 0;
fa0f02df 765 int start_number = -1;
8ac80a57 766 int keep_subject = 0;
e6ff0f42 767 int numbered_files = 0; /* _just_ numbers */
2d9e4a47 768 int subject_prefix = 0;
9c6efa36 769 int ignore_if_in_upstream = 0;
cc35de84 770 int thread = 0;
a5a27c79 771 int cover_letter = 0;
2bda2cf4 772 int boundary_count = 0;
37c22a4b 773 int no_binary_diff = 0;
a5a27c79 774 struct commit *origin = NULL, *head = NULL;
76af0734 775 const char *in_reply_to = NULL;
5d23e133 776 struct patch_ids ids;
cf2251b6 777 char *add_signoff = NULL;
f285a2d7 778 struct strbuf buf = STRBUF_INIT;
91efcf60 779
ef90d6d4 780 git_config(git_format_config, NULL);
db6296a5 781 init_revisions(&rev, prefix);
91efcf60
JH
782 rev.commit_format = CMIT_FMT_EMAIL;
783 rev.verbose_header = 1;
784 rev.diff = 1;
91efcf60
JH
785 rev.combine_merges = 0;
786 rev.ignore_merges = 1;
8f67f8ae 787 DIFF_OPT_SET(&rev.diffopt, RECURSIVE);
91efcf60 788
dbd21447 789 rev.subject_prefix = fmt_patch_subject_prefix;
20ff0680 790
2448482b
JS
791 /*
792 * Parse the arguments before setup_revisions(), or something
2dc189a3 793 * like "git format-patch -o a123 HEAD^.." may fail; a123 is
2448482b
JS
794 * possibly a valid SHA1.
795 */
796 for (i = 1, j = 1; i < argc; i++) {
797 if (!strcmp(argv[i], "--stdout"))
0377db77 798 use_stdout = 1;
596524b3
JS
799 else if (!strcmp(argv[i], "-n") ||
800 !strcmp(argv[i], "--numbered"))
801 numbered = 1;
49604a4d
BG
802 else if (!strcmp(argv[i], "-N") ||
803 !strcmp(argv[i], "--no-numbered")) {
804 numbered = 0;
805 auto_number = 0;
806 }
cc44c765 807 else if (!prefixcmp(argv[i], "--start-number="))
fa0f02df 808 start_number = strtol(argv[i] + 15, NULL, 10);
e6ff0f42
JL
809 else if (!strcmp(argv[i], "--numbered-files"))
810 numbered_files = 1;
fa0f02df
JS
811 else if (!strcmp(argv[i], "--start-number")) {
812 i++;
813 if (i == argc)
814 die("Need a number for --start-number");
815 start_number = strtol(argv[i], NULL, 10);
cf2251b6 816 }
736cc67d
DB
817 else if (!prefixcmp(argv[i], "--cc=")) {
818 ALLOC_GROW(extra_cc, extra_cc_nr + 1, extra_cc_alloc);
819 extra_cc[extra_cc_nr++] = xstrdup(argv[i] + 5);
820 }
cf2251b6 821 else if (!strcmp(argv[i], "-k") ||
8ac80a57
JS
822 !strcmp(argv[i], "--keep-subject")) {
823 keep_subject = 1;
824 rev.total = -1;
cf2251b6 825 }
efd02016
JH
826 else if (!strcmp(argv[i], "--output-directory") ||
827 !strcmp(argv[i], "-o")) {
2448482b 828 i++;
efd02016
JH
829 if (argc <= i)
830 die("Which directory?");
831 if (output_directory)
832 die("Two output directories?");
833 output_directory = argv[i];
698ce6f8 834 }
cf2251b6
JH
835 else if (!strcmp(argv[i], "--signoff") ||
836 !strcmp(argv[i], "-s")) {
6c4cca1c
EB
837 const char *committer;
838 const char *endpos;
774751a8 839 committer = git_committer_info(IDENT_ERROR_ON_NO_NAME);
6c4cca1c 840 endpos = strchr(committer, '>');
cf2251b6 841 if (!endpos)
d7530708 842 die("bogus committer info %s", committer);
182af834 843 add_signoff = xmemdupz(committer, endpos - committer + 1);
cf2251b6 844 }
c112f689 845 else if (!strcmp(argv[i], "--attach")) {
698ce6f8 846 rev.mime_boundary = git_version_string;
c112f689
JS
847 rev.no_inline = 1;
848 }
849 else if (!prefixcmp(argv[i], "--attach=")) {
850 rev.mime_boundary = argv[i] + 9;
851 rev.no_inline = 1;
852 }
853 else if (!strcmp(argv[i], "--inline")) {
854 rev.mime_boundary = git_version_string;
855 rev.no_inline = 0;
856 }
857 else if (!prefixcmp(argv[i], "--inline=")) {
698ce6f8 858 rev.mime_boundary = argv[i] + 9;
c112f689
JS
859 rev.no_inline = 0;
860 }
9c6efa36
JS
861 else if (!strcmp(argv[i], "--ignore-if-in-upstream"))
862 ignore_if_in_upstream = 1;
cc35de84
JT
863 else if (!strcmp(argv[i], "--thread"))
864 thread = 1;
cc44c765 865 else if (!prefixcmp(argv[i], "--in-reply-to="))
da56645d
JT
866 in_reply_to = argv[i] + 14;
867 else if (!strcmp(argv[i], "--in-reply-to")) {
868 i++;
869 if (i == argc)
870 die("Need a Message-Id for --in-reply-to");
871 in_reply_to = argv[i];
2d9e4a47
RJ
872 } else if (!prefixcmp(argv[i], "--subject-prefix=")) {
873 subject_prefix = 1;
874 rev.subject_prefix = argv[i] + 17;
875 } else if (!prefixcmp(argv[i], "--suffix="))
03eeaeae 876 fmt_patch_suffix = argv[i] + 9;
a5a27c79
DB
877 else if (!strcmp(argv[i], "--cover-letter"))
878 cover_letter = 1;
37c22a4b
CMOF
879 else if (!strcmp(argv[i], "--no-binary"))
880 no_binary_diff = 1;
698ce6f8 881 else
2448482b 882 argv[j++] = argv[i];
0377db77 883 }
2448482b
JS
884 argc = j;
885
3ee79d9f
DB
886 for (i = 0; i < extra_hdr_nr; i++) {
887 strbuf_addstr(&buf, extra_hdr[i]);
888 strbuf_addch(&buf, '\n');
889 }
890
891 if (extra_to_nr)
892 strbuf_addstr(&buf, "To: ");
893 for (i = 0; i < extra_to_nr; i++) {
894 if (i)
895 strbuf_addstr(&buf, " ");
896 strbuf_addstr(&buf, extra_to[i]);
897 if (i + 1 < extra_to_nr)
898 strbuf_addch(&buf, ',');
899 strbuf_addch(&buf, '\n');
900 }
901
902 if (extra_cc_nr)
903 strbuf_addstr(&buf, "Cc: ");
904 for (i = 0; i < extra_cc_nr; i++) {
905 if (i)
906 strbuf_addstr(&buf, " ");
907 strbuf_addstr(&buf, extra_cc[i]);
908 if (i + 1 < extra_cc_nr)
909 strbuf_addch(&buf, ',');
910 strbuf_addch(&buf, '\n');
911 }
912
913 rev.extra_headers = strbuf_detach(&buf, 0);
914
add5c8a5 915 if (start_number < 0)
fa0f02df 916 start_number = 1;
63b398a4 917 if (numbered && keep_subject)
8ac80a57 918 die ("-n and -k are mutually exclusive.");
2d9e4a47
RJ
919 if (keep_subject && subject_prefix)
920 die ("--subject-prefix and -k are mutually exclusive.");
e6ff0f42
JL
921 if (numbered_files && use_stdout)
922 die ("--numbered-files and --stdout are mutually exclusive.");
8ac80a57 923
2448482b
JS
924 argc = setup_revisions(argc, argv, &rev, "HEAD");
925 if (argc > 1)
926 die ("unrecognized argument: %s", argv[1]);
0377db77 927
68daa64d
JK
928 if (!rev.diffopt.output_format
929 || rev.diffopt.output_format == DIFF_FORMAT_PATCH)
c261e438 930 rev.diffopt.output_format = DIFF_FORMAT_DIFFSTAT | DIFF_FORMAT_SUMMARY | DIFF_FORMAT_PATCH;
c9b5ef99 931
37c22a4b 932 if (!DIFF_OPT_TST(&rev.diffopt, TEXT) && !no_binary_diff)
8f67f8ae 933 DIFF_OPT_SET(&rev.diffopt, BINARY);
e47f306d 934
9800a754
JH
935 if (!use_stdout)
936 output_directory = set_outdir(prefix, output_directory);
77e565d8 937
efd02016
JH
938 if (output_directory) {
939 if (use_stdout)
940 die("standard output, or directory, which one?");
941 if (mkdir(output_directory, 0777) < 0 && errno != EEXIST)
942 die("Could not create directory %s",
943 output_directory);
944 }
945
1f1e895f 946 if (rev.pending.nr == 1) {
8a1d076e
JH
947 if (rev.max_count < 0 && !rev.show_root_diff) {
948 /*
949 * This is traditional behaviour of "git format-patch
950 * origin" that prepares what the origin side still
951 * does not have.
952 */
7c496280 953 rev.pending.objects[0].item->flags |= UNINTERESTING;
3384a2df 954 add_head_to_pending(&rev);
7c496280 955 }
8a1d076e
JH
956 /*
957 * Otherwise, it is "format-patch -22 HEAD", and/or
958 * "format-patch --root HEAD". The user wants
959 * get_revision() to do the usual traversal.
7c496280 960 */
e686eb98 961 }
68c2ec7f
JH
962
963 /*
964 * We cannot move this anywhere earlier because we do want to
965 * know if --root was given explicitly from the comand line.
966 */
967 rev.show_root_diff = 1;
968
a5a27c79
DB
969 if (cover_letter) {
970 /* remember the range */
a5a27c79
DB
971 int i;
972 for (i = 0; i < rev.pending.nr; i++) {
973 struct object *o = rev.pending.objects[i].item;
2bda2cf4 974 if (!(o->flags & UNINTERESTING))
a5a27c79
DB
975 head = (struct commit *)o;
976 }
a5a27c79
DB
977 /* We can't generate a cover letter without any patches */
978 if (!head)
979 return 0;
980 }
e686eb98 981
9c6efa36 982 if (ignore_if_in_upstream)
5d23e133 983 get_patch_ids(&rev, &ids, prefix);
9c6efa36 984
81f3a188 985 if (!use_stdout)
f5788250 986 realstdout = xfdopen(xdup(1), "w");
81f3a188 987
3d51e1b5
MK
988 if (prepare_revision_walk(&rev))
989 die("revision walk setup failed");
2bda2cf4 990 rev.boundary = 1;
91efcf60 991 while ((commit = get_revision(&rev)) != NULL) {
2bda2cf4 992 if (commit->object.flags & BOUNDARY) {
2bda2cf4
DB
993 boundary_count++;
994 origin = (boundary_count == 1) ? commit : NULL;
995 continue;
996 }
997
0377db77
JS
998 /* ignore merges */
999 if (commit->parents && commit->parents->next)
1000 continue;
9c6efa36
JS
1001
1002 if (ignore_if_in_upstream &&
5d23e133 1003 has_commit_patch_id(commit, &ids))
9c6efa36
JS
1004 continue;
1005
91efcf60 1006 nr++;
83572c1a 1007 list = xrealloc(list, nr * sizeof(list[0]));
91efcf60
JH
1008 list[nr - 1] = commit;
1009 }
0377db77 1010 total = nr;
49604a4d
BG
1011 if (!keep_subject && auto_number && total > 1)
1012 numbered = 1;
596524b3 1013 if (numbered)
fa0f02df 1014 rev.total = total + start_number - 1;
b079c50e
TR
1015 if (in_reply_to || thread || cover_letter)
1016 rev.ref_message_ids = xcalloc(1, sizeof(struct string_list));
1017 if (in_reply_to) {
1018 const char *msgid = clean_message_id(in_reply_to);
1019 string_list_append(msgid, rev.ref_message_ids);
1020 }
a5a27c79
DB
1021 if (cover_letter) {
1022 if (thread)
1023 gen_message_id(&rev, "cover");
1024 make_cover_letter(&rev, use_stdout, numbered, numbered_files,
2bda2cf4 1025 origin, nr, list, head);
a5a27c79
DB
1026 total++;
1027 start_number--;
1028 }
1029 rev.add_signoff = add_signoff;
91efcf60
JH
1030 while (0 <= --nr) {
1031 int shown;
1032 commit = list[nr];
add5c8a5 1033 rev.nr = total - nr + (start_number - 1);
d1566f78 1034 /* Make the second and subsequent mails replies to the first */
cc35de84 1035 if (thread) {
a5a27c79 1036 /* Have we already had a message ID? */
e1a37346 1037 if (rev.message_id) {
a5a27c79
DB
1038 /*
1039 * If we've got the ID to be a reply
1040 * to, discard the current ID;
1041 * otherwise, make everything a reply
1042 * to that.
1043 */
b079c50e 1044 if (rev.ref_message_ids->nr > 0)
e1a37346
DB
1045 free(rev.message_id);
1046 else
b079c50e
TR
1047 string_list_append(rev.message_id,
1048 rev.ref_message_ids);
cc35de84 1049 }
e1a37346 1050 gen_message_id(&rev, sha1_to_hex(commit->object.sha1));
d1566f78 1051 }
a5a27c79
DB
1052 if (!use_stdout && reopen_stdout(numbered_files ? NULL :
1053 get_oneline_for_filename(commit, keep_subject),
1054 rev.nr, rev.total))
1055 die("Failed to create output files");
91efcf60
JH
1056 shown = log_tree_commit(&rev, commit);
1057 free(commit->buffer);
1058 commit->buffer = NULL;
add5c8a5
JH
1059
1060 /* We put one extra blank line between formatted
1061 * patches and this flag is used by log-tree code
1062 * to see if it needs to emit a LF before showing
1063 * the log; when using one file per patch, we do
1064 * not want the extra blank line.
1065 */
1066 if (!use_stdout)
1067 rev.shown_one = 0;
698ce6f8
JS
1068 if (shown) {
1069 if (rev.mime_boundary)
1070 printf("\n--%s%s--\n\n\n",
1071 mime_boundary_leader,
1072 rev.mime_boundary);
1073 else
1074 printf("-- \n%s\n\n", git_version_string);
1075 }
0377db77
JS
1076 if (!use_stdout)
1077 fclose(stdout);
91efcf60
JH
1078 }
1079 free(list);
5d23e133
JH
1080 if (ignore_if_in_upstream)
1081 free_patch_ids(&ids);
91efcf60
JH
1082 return 0;
1083}
1084
e827633a
RS
1085static int add_pending_commit(const char *arg, struct rev_info *revs, int flags)
1086{
1087 unsigned char sha1[20];
1088 if (get_sha1(arg, sha1) == 0) {
1089 struct commit *commit = lookup_commit_reference(sha1);
1090 if (commit) {
1091 commit->object.flags |= flags;
1092 add_pending_object(revs, &commit->object, arg);
1093 return 0;
1094 }
1095 }
1096 return -1;
1097}
1098
1099static const char cherry_usage[] =
3bc52d7a 1100"git cherry [-v] [<upstream> [<head> [<limit>]]]";
e827633a
RS
1101int cmd_cherry(int argc, const char **argv, const char *prefix)
1102{
1103 struct rev_info revs;
5d23e133 1104 struct patch_ids ids;
e827633a
RS
1105 struct commit *commit;
1106 struct commit_list *list = NULL;
f2968022 1107 struct branch *current_branch;
e827633a
RS
1108 const char *upstream;
1109 const char *head = "HEAD";
1110 const char *limit = NULL;
1111 int verbose = 0;
1112
1113 if (argc > 1 && !strcmp(argv[1], "-v")) {
1114 verbose = 1;
1115 argc--;
1116 argv++;
1117 }
1118
1119 switch (argc) {
1120 case 4:
1121 limit = argv[3];
1122 /* FALLTHROUGH */
1123 case 3:
1124 head = argv[2];
1125 /* FALLTHROUGH */
1126 case 2:
1127 upstream = argv[1];
1128 break;
1129 default:
f2968022
MH
1130 current_branch = branch_get(NULL);
1131 if (!current_branch || !current_branch->merge
1132 || !current_branch->merge[0]
1133 || !current_branch->merge[0]->dst) {
1134 fprintf(stderr, "Could not find a tracked"
1135 " remote branch, please"
1136 " specify <upstream> manually.\n");
1137 usage(cherry_usage);
1138 }
1139
1140 upstream = current_branch->merge[0]->dst;
e827633a
RS
1141 }
1142
1143 init_revisions(&revs, prefix);
1144 revs.diff = 1;
1145 revs.combine_merges = 0;
1146 revs.ignore_merges = 1;
8f67f8ae 1147 DIFF_OPT_SET(&revs.diffopt, RECURSIVE);
e827633a
RS
1148
1149 if (add_pending_commit(head, &revs, 0))
1150 die("Unknown commit %s", head);
1151 if (add_pending_commit(upstream, &revs, UNINTERESTING))
1152 die("Unknown commit %s", upstream);
1153
1154 /* Don't say anything if head and upstream are the same. */
1155 if (revs.pending.nr == 2) {
1156 struct object_array_entry *o = revs.pending.objects;
1157 if (hashcmp(o[0].item->sha1, o[1].item->sha1) == 0)
1158 return 0;
1159 }
1160
5d23e133 1161 get_patch_ids(&revs, &ids, prefix);
e827633a
RS
1162
1163 if (limit && add_pending_commit(limit, &revs, UNINTERESTING))
1164 die("Unknown commit %s", limit);
1165
1166 /* reverse the list of commits */
3d51e1b5
MK
1167 if (prepare_revision_walk(&revs))
1168 die("revision walk setup failed");
e827633a
RS
1169 while ((commit = get_revision(&revs)) != NULL) {
1170 /* ignore merges */
1171 if (commit->parents && commit->parents->next)
1172 continue;
1173
1174 commit_list_insert(commit, &list);
1175 }
1176
1177 while (list) {
e827633a
RS
1178 char sign = '+';
1179
1180 commit = list->item;
5d23e133 1181 if (has_commit_patch_id(commit, &ids))
e827633a
RS
1182 sign = '-';
1183
1184 if (verbose) {
f285a2d7 1185 struct strbuf buf = STRBUF_INIT;
674d1727 1186 pretty_print_commit(CMIT_FMT_ONELINE, commit,
4593fb84 1187 &buf, 0, NULL, NULL, 0, 0);
e827633a 1188 printf("%c %s %s\n", sign,
674d1727
PH
1189 sha1_to_hex(commit->object.sha1), buf.buf);
1190 strbuf_release(&buf);
e827633a
RS
1191 }
1192 else {
1193 printf("%c %s\n", sign,
1194 sha1_to_hex(commit->object.sha1));
1195 }
1196
1197 list = list->next;
1198 }
1199
5d23e133 1200 free_patch_ids(&ids);
e827633a
RS
1201 return 0;
1202}