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