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