format-patch: construct patch filename in one function
[git/jnareb-git.git] / builtin-log.c
blob193de3ff2e4a0dd82f3ba05dc05cb30995718152
1 /*
2 * Builtin "git log" and related commands (show, whatchanged)
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"
22 /* Set a default date-time format for git log ("log.date" config variable) */
23 static const char *default_date_mode = NULL;
25 static int default_show_root = 1;
26 static const char *fmt_patch_subject_prefix = "PATCH";
27 static const char *fmt_pretty;
29 static void cmd_log_init(int argc, const char **argv, const char *prefix,
30 struct rev_info *rev)
32 int i;
34 rev->abbrev = DEFAULT_ABBREV;
35 rev->commit_format = CMIT_FMT_DEFAULT;
36 if (fmt_pretty)
37 get_commit_format(fmt_pretty, rev);
38 rev->verbose_header = 1;
39 DIFF_OPT_SET(&rev->diffopt, RECURSIVE);
40 rev->show_root_diff = default_show_root;
41 rev->subject_prefix = fmt_patch_subject_prefix;
42 DIFF_OPT_SET(&rev->diffopt, ALLOW_TEXTCONV);
44 if (default_date_mode)
45 rev->date_mode = parse_date_format(default_date_mode);
47 argc = setup_revisions(argc, argv, rev, "HEAD");
49 if (rev->diffopt.pickaxe || rev->diffopt.filter)
50 rev->always_show_header = 0;
51 if (DIFF_OPT_TST(&rev->diffopt, FOLLOW_RENAMES)) {
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");
56 for (i = 1; i < argc; i++) {
57 const char *arg = argv[i];
58 if (!strcmp(arg, "--decorate")) {
59 load_ref_decorations();
60 rev->show_decorations = 1;
61 } else if (!strcmp(arg, "--source")) {
62 rev->show_source = 1;
63 } else
64 die("unrecognized argument: %s", arg);
69 * This gives a rough estimate for how many commits we
70 * will print out in the list.
72 static int estimate_commit_count(struct rev_info *rev, struct commit_list *list)
74 int n = 0;
76 while (list) {
77 struct commit *commit = list->item;
78 unsigned int flags = commit->object.flags;
79 list = list->next;
80 if (!(flags & (TREESAME | UNINTERESTING)))
81 n++;
83 return n;
86 static void show_early_header(struct rev_info *rev, const char *stage, int nr)
88 if (rev->shown_one) {
89 rev->shown_one = 0;
90 if (rev->commit_format != CMIT_FMT_ONELINE)
91 putchar(rev->diffopt.line_termination);
93 printf("Final output: %d %s\n", nr, stage);
96 struct itimerval early_output_timer;
98 static void log_show_early(struct rev_info *revs, struct commit_list *list)
100 int i = revs->early_output;
101 int show_header = 1;
103 sort_in_topological_order(&list, revs->lifo);
104 while (list && i) {
105 struct commit *commit = list->item;
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;
113 log_tree_commit(revs, commit);
114 i--;
115 break;
116 case commit_ignore:
117 break;
118 case commit_error:
119 return;
121 list = list->next;
124 /* Did we already get enough commits for the early output? */
125 if (!i)
126 return;
129 * ..if no, then repeat it twice a second until we
130 * do.
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!
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);
143 static void early_output(int signal)
145 show_early_output = log_show_early;
148 static void setup_early_output(struct rev_info *rev)
150 struct sigaction sa;
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.
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);
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..
170 * This is a one-time-only trigger.
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);
177 static void finish_early_output(struct rev_info *rev)
179 int n = estimate_commit_count(rev, rev->commits);
180 signal(SIGALRM, SIG_IGN);
181 show_early_header(rev, "done", n);
184 static int cmd_log_walk(struct rev_info *rev)
186 struct commit *commit;
188 if (rev->early_output)
189 setup_early_output(rev);
191 if (prepare_revision_walk(rev))
192 die("revision walk setup failed");
194 if (rev->early_output)
195 finish_early_output(rev);
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
202 while ((commit = get_revision(rev)) != NULL) {
203 log_tree_commit(rev, commit);
204 if (!rev->reflog_info) {
205 /* we allow cycles in reflog ancestry */
206 free(commit->buffer);
207 commit->buffer = NULL;
209 free_commit_list(commit->parents);
210 commit->parents = NULL;
212 if (rev->diffopt.output_format & DIFF_FORMAT_CHECKDIFF &&
213 DIFF_OPT_TST(&rev->diffopt, CHECK_FAILED)) {
214 return 02;
216 return diff_result_code(&rev->diffopt, 0);
219 static int git_log_config(const char *var, const char *value, void *cb)
221 if (!strcmp(var, "format.pretty"))
222 return git_config_string(&fmt_pretty, var, value);
223 if (!strcmp(var, "format.subjectprefix"))
224 return git_config_string(&fmt_patch_subject_prefix, var, value);
225 if (!strcmp(var, "log.date"))
226 return git_config_string(&default_date_mode, var, value);
227 if (!strcmp(var, "log.showroot")) {
228 default_show_root = git_config_bool(var, value);
229 return 0;
231 return git_diff_ui_config(var, value, cb);
234 int cmd_whatchanged(int argc, const char **argv, const char *prefix)
236 struct rev_info rev;
238 git_config(git_log_config, NULL);
240 if (diff_use_color_default == -1)
241 diff_use_color_default = git_use_color_default;
243 init_revisions(&rev, prefix);
244 rev.diff = 1;
245 rev.simplify_history = 0;
246 cmd_log_init(argc, argv, prefix, &rev);
247 if (!rev.diffopt.output_format)
248 rev.diffopt.output_format = DIFF_FORMAT_RAW;
249 return cmd_log_walk(&rev);
252 static void show_tagger(char *buf, int len, struct rev_info *rev)
254 struct strbuf out = STRBUF_INIT;
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);
263 static int show_object(const unsigned char *sha1, int show_tag_object,
264 struct rev_info *rev)
266 unsigned long size;
267 enum object_type type;
268 char *buf = read_sha1_file(sha1, &type, &size);
269 int offset = 0;
271 if (!buf)
272 return error("Could not read object %s", sha1_to_hex(sha1));
274 if (show_tag_object)
275 while (offset < size && buf[offset] != '\n') {
276 int new_offset = offset + 1;
277 while (new_offset < size && buf[new_offset++] != '\n')
278 ; /* do nothing */
279 if (!prefixcmp(buf + offset, "tagger "))
280 show_tagger(buf + offset + 7,
281 new_offset - offset - 7, rev);
282 offset = new_offset;
285 if (offset < size)
286 fwrite(buf + offset, size - offset, 1, stdout);
287 free(buf);
288 return 0;
291 static int show_tree_object(const unsigned char *sha1,
292 const char *base, int baselen,
293 const char *pathname, unsigned mode, int stage, void *context)
295 printf("%s%s\n", pathname, S_ISDIR(mode) ? "/" : "");
296 return 0;
299 int cmd_show(int argc, const char **argv, const char *prefix)
301 struct rev_info rev;
302 struct object_array_entry *objects;
303 int i, count, ret = 0;
305 git_config(git_log_config, NULL);
307 if (diff_use_color_default == -1)
308 diff_use_color_default = git_use_color_default;
310 init_revisions(&rev, prefix);
311 rev.diff = 1;
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;
317 cmd_log_init(argc, argv, prefix, &rev);
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:
326 ret = show_object(o->sha1, 0, NULL);
327 break;
328 case OBJ_TAG: {
329 struct tag *t = (struct tag *)o;
331 printf("%stag %s%s\n",
332 diff_get_color_opt(&rev.diffopt, DIFF_COMMIT),
333 t->tag,
334 diff_get_color_opt(&rev.diffopt, DIFF_RESET));
335 ret = show_object(o->sha1, 1, &rev);
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;
343 i--;
344 break;
346 case OBJ_TREE:
347 printf("%stree %s%s\n\n",
348 diff_get_color_opt(&rev.diffopt, DIFF_COMMIT),
349 name,
350 diff_get_color_opt(&rev.diffopt, DIFF_RESET));
351 read_tree_recursive((struct tree *)o, "", 0, 0, NULL,
352 show_tree_object, NULL);
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);
364 free(objects);
365 return ret;
369 * This is equivalent to "git log -g --abbrev-commit --pretty=oneline"
371 int cmd_log_reflog(int argc, const char **argv, const char *prefix)
373 struct rev_info rev;
375 git_config(git_log_config, NULL);
377 if (diff_use_color_default == -1)
378 diff_use_color_default = git_use_color_default;
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);
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.
391 rev.commit_format = CMIT_FMT_ONELINE;
392 rev.use_terminator = 1;
393 rev.always_show_header = 1;
396 * We get called through "git reflog", so unlike the other log
397 * routines, we need to set up our pager manually..
399 setup_pager();
401 return cmd_log_walk(&rev);
404 int cmd_log(int argc, const char **argv, const char *prefix)
406 struct rev_info rev;
408 git_config(git_log_config, NULL);
410 if (diff_use_color_default == -1)
411 diff_use_color_default = git_use_color_default;
413 init_revisions(&rev, prefix);
414 rev.always_show_header = 1;
415 cmd_log_init(argc, argv, prefix, &rev);
416 return cmd_log_walk(&rev);
419 /* format-patch */
420 #define FORMAT_PATCH_NAME_MAX 64
422 static const char *fmt_patch_suffix = ".patch";
423 static int numbered = 0;
424 static int auto_number = 1;
426 static char *default_attach = NULL;
428 static char **extra_hdr;
429 static int extra_hdr_nr;
430 static int extra_hdr_alloc;
432 static char **extra_to;
433 static int extra_to_nr;
434 static int extra_to_alloc;
436 static char **extra_cc;
437 static int extra_cc_nr;
438 static int extra_cc_alloc;
440 static void add_header(const char *value)
442 int len = strlen(value);
443 while (len && value[len - 1] == '\n')
444 len--;
445 if (!strncasecmp(value, "to: ", 4)) {
446 ALLOC_GROW(extra_to, extra_to_nr + 1, extra_to_alloc);
447 extra_to[extra_to_nr++] = xstrndup(value + 4, len - 4);
448 return;
450 if (!strncasecmp(value, "cc: ", 4)) {
451 ALLOC_GROW(extra_cc, extra_cc_nr + 1, extra_cc_alloc);
452 extra_cc[extra_cc_nr++] = xstrndup(value + 4, len - 4);
453 return;
455 ALLOC_GROW(extra_hdr, extra_hdr_nr + 1, extra_hdr_alloc);
456 extra_hdr[extra_hdr_nr++] = xstrndup(value, len);
459 #define THREAD_SHALLOW 1
460 #define THREAD_DEEP 2
461 static int thread = 0;
463 static int git_format_config(const char *var, const char *value, void *cb)
465 if (!strcmp(var, "format.headers")) {
466 if (!value)
467 die("format.headers without value");
468 add_header(value);
469 return 0;
471 if (!strcmp(var, "format.suffix"))
472 return git_config_string(&fmt_patch_suffix, var, value);
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;
480 if (!strcmp(var, "diff.color") || !strcmp(var, "color.diff")) {
481 return 0;
483 if (!strcmp(var, "format.numbered")) {
484 if (value && !strcasecmp(value, "auto")) {
485 auto_number = 1;
486 return 0;
488 numbered = git_config_bool(var, value);
489 auto_number = auto_number && numbered;
490 return 0;
492 if (!strcmp(var, "format.attach")) {
493 if (value && *value)
494 default_attach = xstrdup(value);
495 else
496 default_attach = xstrdup(git_version_string);
497 return 0;
499 if (!strcmp(var, "format.thread")) {
500 if (value && !strcasecmp(value, "deep")) {
501 thread = THREAD_DEEP;
502 return 0;
504 if (value && !strcasecmp(value, "shallow")) {
505 thread = THREAD_SHALLOW;
506 return 0;
508 thread = git_config_bool(var, value) && THREAD_SHALLOW;
509 return 0;
512 return git_log_config(var, value, cb);
516 static void get_patch_filename(struct commit *commit, int nr,
517 const char *suffix, struct strbuf *buf)
519 int suffix_len = strlen(suffix) + 1;
520 int start_len = buf->len;
522 strbuf_addf(buf, commit ? "%04d-" : "%d", nr);
523 if (commit) {
524 format_commit_message(commit, "%f", buf, DATE_NORMAL);
526 * Replace characters at the end with the suffix if the
527 * filename is too long
529 if (buf->len + suffix_len > FORMAT_PATCH_NAME_MAX + start_len)
530 strbuf_splice(buf,
531 start_len + FORMAT_PATCH_NAME_MAX - suffix_len,
532 suffix_len, suffix, suffix_len);
533 else
534 strbuf_addstr(buf, suffix);
538 static FILE *realstdout = NULL;
539 static const char *output_directory = NULL;
540 static int outdir_offset;
542 static int reopen_stdout(const char *oneline, struct rev_info *rev)
544 char filename[PATH_MAX];
545 int len = 0;
546 int suffix_len = strlen(fmt_patch_suffix) + 1;
548 if (output_directory) {
549 len = snprintf(filename, sizeof(filename), "%s",
550 output_directory);
551 if (len >=
552 sizeof(filename) - FORMAT_PATCH_NAME_MAX - suffix_len)
553 return error("name of output directory is too long");
554 if (filename[len - 1] != '/')
555 filename[len++] = '/';
558 strncpy(filename + len, oneline, PATH_MAX - len);
560 if (!DIFF_OPT_TST(&rev->diffopt, QUIET))
561 fprintf(realstdout, "%s\n", filename + outdir_offset);
563 if (freopen(filename, "w", stdout) == NULL)
564 return error("Cannot open patch file %s",filename);
566 return 0;
569 static void get_patch_ids(struct rev_info *rev, struct patch_ids *ids, const char *prefix)
571 struct rev_info check_rev;
572 struct commit *commit;
573 struct object *o1, *o2;
574 unsigned flags1, flags2;
576 if (rev->pending.nr != 2)
577 die("Need exactly one range.");
579 o1 = rev->pending.objects[0].item;
580 flags1 = o1->flags;
581 o2 = rev->pending.objects[1].item;
582 flags2 = o2->flags;
584 if ((flags1 & UNINTERESTING) == (flags2 & UNINTERESTING))
585 die("Not a range.");
587 init_patch_ids(ids);
589 /* given a range a..b get all patch ids for b..a */
590 init_revisions(&check_rev, prefix);
591 o1->flags ^= UNINTERESTING;
592 o2->flags ^= UNINTERESTING;
593 add_pending_object(&check_rev, o1, "o1");
594 add_pending_object(&check_rev, o2, "o2");
595 if (prepare_revision_walk(&check_rev))
596 die("revision walk setup failed");
598 while ((commit = get_revision(&check_rev)) != NULL) {
599 /* ignore merges */
600 if (commit->parents && commit->parents->next)
601 continue;
603 add_commit_patch_id(commit, ids);
606 /* reset for next revision walk */
607 clear_commit_marks((struct commit *)o1,
608 SEEN | UNINTERESTING | SHOWN | ADDED);
609 clear_commit_marks((struct commit *)o2,
610 SEEN | UNINTERESTING | SHOWN | ADDED);
611 o1->flags = flags1;
612 o2->flags = flags2;
615 static void gen_message_id(struct rev_info *info, char *base)
617 const char *committer = git_committer_info(IDENT_WARN_ON_NO_NAME);
618 const char *email_start = strrchr(committer, '<');
619 const char *email_end = strrchr(committer, '>');
620 struct strbuf buf = STRBUF_INIT;
621 if (!email_start || !email_end || email_start > email_end - 1)
622 die("Could not extract email from committer identity.");
623 strbuf_addf(&buf, "%s.%lu.git.%.*s", base,
624 (unsigned long) time(NULL),
625 (int)(email_end - email_start - 1), email_start + 1);
626 info->message_id = strbuf_detach(&buf, NULL);
629 static void make_cover_letter(struct rev_info *rev, int use_stdout,
630 int numbered, int numbered_files,
631 struct commit *origin,
632 int nr, struct commit **list, struct commit *head)
634 const char *committer;
635 char *head_sha1;
636 const char *subject_start = NULL;
637 const char *body = "*** SUBJECT HERE ***\n\n*** BLURB HERE ***\n";
638 const char *msg;
639 const char *extra_headers = rev->extra_headers;
640 struct shortlog log;
641 struct strbuf sb = STRBUF_INIT;
642 int i;
643 const char *encoding = "utf-8";
644 struct diff_options opts;
645 int need_8bit_cte = 0;
646 char filename[PATH_MAX];
648 if (rev->commit_format != CMIT_FMT_EMAIL)
649 die("Cover letter needs email format");
651 if (numbered_files)
652 sprintf(filename, "0");
653 else
654 sprintf(filename, "%04d-cover-letter%s", 0, fmt_patch_suffix);
656 if (!use_stdout && reopen_stdout(filename, rev))
657 return;
659 head_sha1 = sha1_to_hex(head->object.sha1);
661 log_write_email_headers(rev, head_sha1, &subject_start, &extra_headers,
662 &need_8bit_cte);
664 committer = git_committer_info(0);
666 msg = body;
667 pp_user_info(NULL, CMIT_FMT_EMAIL, &sb, committer, DATE_RFC2822,
668 encoding);
669 pp_title_line(CMIT_FMT_EMAIL, &msg, &sb, subject_start, extra_headers,
670 encoding, need_8bit_cte);
671 pp_remainder(CMIT_FMT_EMAIL, &msg, &sb, 0);
672 printf("%s\n", sb.buf);
674 strbuf_release(&sb);
676 shortlog_init(&log);
677 log.wrap_lines = 1;
678 log.wrap = 72;
679 log.in1 = 2;
680 log.in2 = 4;
681 for (i = 0; i < nr; i++)
682 shortlog_add_commit(&log, list[i]);
684 shortlog_output(&log);
687 * We can only do diffstat with a unique reference point
689 if (!origin)
690 return;
692 memcpy(&opts, &rev->diffopt, sizeof(opts));
693 opts.output_format = DIFF_FORMAT_SUMMARY | DIFF_FORMAT_DIFFSTAT;
695 diff_setup_done(&opts);
697 diff_tree_sha1(origin->tree->object.sha1,
698 head->tree->object.sha1,
699 "", &opts);
700 diffcore_std(&opts);
701 diff_flush(&opts);
703 printf("\n");
706 static const char *clean_message_id(const char *msg_id)
708 char ch;
709 const char *a, *z, *m;
711 m = msg_id;
712 while ((ch = *m) && (isspace(ch) || (ch == '<')))
713 m++;
714 a = m;
715 z = NULL;
716 while ((ch = *m)) {
717 if (!isspace(ch) && (ch != '>'))
718 z = m;
719 m++;
721 if (!z)
722 die("insane in-reply-to: %s", msg_id);
723 if (++z == m)
724 return a;
725 return xmemdupz(a, z - a);
728 static const char *set_outdir(const char *prefix, const char *output_directory)
730 if (output_directory && is_absolute_path(output_directory))
731 return output_directory;
733 if (!prefix || !*prefix) {
734 if (output_directory)
735 return output_directory;
736 /* The user did not explicitly ask for "./" */
737 outdir_offset = 2;
738 return "./";
741 outdir_offset = strlen(prefix);
742 if (!output_directory)
743 return prefix;
745 return xstrdup(prefix_filename(prefix, outdir_offset,
746 output_directory));
749 int cmd_format_patch(int argc, const char **argv, const char *prefix)
751 struct commit *commit;
752 struct commit **list = NULL;
753 struct rev_info rev;
754 int nr = 0, total, i, j;
755 int use_stdout = 0;
756 int start_number = -1;
757 int keep_subject = 0;
758 int numbered_files = 0; /* _just_ numbers */
759 int subject_prefix = 0;
760 int ignore_if_in_upstream = 0;
761 int cover_letter = 0;
762 int boundary_count = 0;
763 int no_binary_diff = 0;
764 struct commit *origin = NULL, *head = NULL;
765 const char *in_reply_to = NULL;
766 struct patch_ids ids;
767 char *add_signoff = NULL;
768 struct strbuf buf = STRBUF_INIT;
769 struct strbuf patch_filename = STRBUF_INIT;
771 git_config(git_format_config, NULL);
772 init_revisions(&rev, prefix);
773 rev.commit_format = CMIT_FMT_EMAIL;
774 rev.verbose_header = 1;
775 rev.diff = 1;
776 rev.combine_merges = 0;
777 rev.ignore_merges = 1;
778 DIFF_OPT_SET(&rev.diffopt, RECURSIVE);
780 rev.subject_prefix = fmt_patch_subject_prefix;
782 if (default_attach) {
783 rev.mime_boundary = default_attach;
784 rev.no_inline = 1;
788 * Parse the arguments before setup_revisions(), or something
789 * like "git format-patch -o a123 HEAD^.." may fail; a123 is
790 * possibly a valid SHA1.
792 for (i = 1, j = 1; i < argc; i++) {
793 if (!strcmp(argv[i], "--stdout"))
794 use_stdout = 1;
795 else if (!strcmp(argv[i], "-n") ||
796 !strcmp(argv[i], "--numbered"))
797 numbered = 1;
798 else if (!strcmp(argv[i], "-N") ||
799 !strcmp(argv[i], "--no-numbered")) {
800 numbered = 0;
801 auto_number = 0;
803 else if (!prefixcmp(argv[i], "--start-number="))
804 start_number = strtol(argv[i] + 15, NULL, 10);
805 else if (!strcmp(argv[i], "--numbered-files"))
806 numbered_files = 1;
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);
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);
817 else if (!strcmp(argv[i], "-k") ||
818 !strcmp(argv[i], "--keep-subject")) {
819 keep_subject = 1;
820 rev.total = -1;
822 else if (!strcmp(argv[i], "--output-directory") ||
823 !strcmp(argv[i], "-o")) {
824 i++;
825 if (argc <= i)
826 die("Which directory?");
827 if (output_directory)
828 die("Two output directories?");
829 output_directory = argv[i];
831 else if (!strcmp(argv[i], "--signoff") ||
832 !strcmp(argv[i], "-s")) {
833 const char *committer;
834 const char *endpos;
835 committer = git_committer_info(IDENT_ERROR_ON_NO_NAME);
836 endpos = strchr(committer, '>');
837 if (!endpos)
838 die("bogus committer info %s", committer);
839 add_signoff = xmemdupz(committer, endpos - committer + 1);
841 else if (!strcmp(argv[i], "--attach")) {
842 rev.mime_boundary = git_version_string;
843 rev.no_inline = 1;
845 else if (!prefixcmp(argv[i], "--attach=")) {
846 rev.mime_boundary = argv[i] + 9;
847 rev.no_inline = 1;
849 else if (!strcmp(argv[i], "--no-attach")) {
850 rev.mime_boundary = NULL;
851 rev.no_inline = 0;
853 else if (!strcmp(argv[i], "--inline")) {
854 rev.mime_boundary = git_version_string;
855 rev.no_inline = 0;
857 else if (!prefixcmp(argv[i], "--inline=")) {
858 rev.mime_boundary = argv[i] + 9;
859 rev.no_inline = 0;
861 else if (!strcmp(argv[i], "--ignore-if-in-upstream"))
862 ignore_if_in_upstream = 1;
863 else if (!strcmp(argv[i], "--thread")
864 || !strcmp(argv[i], "--thread=shallow"))
865 thread = THREAD_SHALLOW;
866 else if (!strcmp(argv[i], "--thread=deep"))
867 thread = THREAD_DEEP;
868 else if (!strcmp(argv[i], "--no-thread"))
869 thread = 0;
870 else if (!prefixcmp(argv[i], "--in-reply-to="))
871 in_reply_to = argv[i] + 14;
872 else if (!strcmp(argv[i], "--in-reply-to")) {
873 i++;
874 if (i == argc)
875 die("Need a Message-Id for --in-reply-to");
876 in_reply_to = argv[i];
877 } else if (!prefixcmp(argv[i], "--subject-prefix=")) {
878 subject_prefix = 1;
879 rev.subject_prefix = argv[i] + 17;
880 } else if (!prefixcmp(argv[i], "--suffix="))
881 fmt_patch_suffix = argv[i] + 9;
882 else if (!strcmp(argv[i], "--cover-letter"))
883 cover_letter = 1;
884 else if (!strcmp(argv[i], "--no-binary"))
885 no_binary_diff = 1;
886 else
887 argv[j++] = argv[i];
889 argc = j;
891 for (i = 0; i < extra_hdr_nr; i++) {
892 strbuf_addstr(&buf, extra_hdr[i]);
893 strbuf_addch(&buf, '\n');
896 if (extra_to_nr)
897 strbuf_addstr(&buf, "To: ");
898 for (i = 0; i < extra_to_nr; i++) {
899 if (i)
900 strbuf_addstr(&buf, " ");
901 strbuf_addstr(&buf, extra_to[i]);
902 if (i + 1 < extra_to_nr)
903 strbuf_addch(&buf, ',');
904 strbuf_addch(&buf, '\n');
907 if (extra_cc_nr)
908 strbuf_addstr(&buf, "Cc: ");
909 for (i = 0; i < extra_cc_nr; i++) {
910 if (i)
911 strbuf_addstr(&buf, " ");
912 strbuf_addstr(&buf, extra_cc[i]);
913 if (i + 1 < extra_cc_nr)
914 strbuf_addch(&buf, ',');
915 strbuf_addch(&buf, '\n');
918 rev.extra_headers = strbuf_detach(&buf, 0);
920 if (start_number < 0)
921 start_number = 1;
922 if (numbered && keep_subject)
923 die ("-n and -k are mutually exclusive.");
924 if (keep_subject && subject_prefix)
925 die ("--subject-prefix and -k are mutually exclusive.");
927 argc = setup_revisions(argc, argv, &rev, "HEAD");
928 if (argc > 1)
929 die ("unrecognized argument: %s", argv[1]);
931 if (!rev.diffopt.output_format
932 || rev.diffopt.output_format == DIFF_FORMAT_PATCH)
933 rev.diffopt.output_format = DIFF_FORMAT_DIFFSTAT | DIFF_FORMAT_SUMMARY | DIFF_FORMAT_PATCH;
935 if (!DIFF_OPT_TST(&rev.diffopt, TEXT) && !no_binary_diff)
936 DIFF_OPT_SET(&rev.diffopt, BINARY);
938 if (!use_stdout)
939 output_directory = set_outdir(prefix, output_directory);
941 if (output_directory) {
942 if (use_stdout)
943 die("standard output, or directory, which one?");
944 if (mkdir(output_directory, 0777) < 0 && errno != EEXIST)
945 die("Could not create directory %s",
946 output_directory);
949 if (rev.pending.nr == 1) {
950 if (rev.max_count < 0 && !rev.show_root_diff) {
952 * This is traditional behaviour of "git format-patch
953 * origin" that prepares what the origin side still
954 * does not have.
956 rev.pending.objects[0].item->flags |= UNINTERESTING;
957 add_head_to_pending(&rev);
960 * Otherwise, it is "format-patch -22 HEAD", and/or
961 * "format-patch --root HEAD". The user wants
962 * get_revision() to do the usual traversal.
967 * We cannot move this anywhere earlier because we do want to
968 * know if --root was given explicitly from the comand line.
970 rev.show_root_diff = 1;
972 if (cover_letter) {
973 /* remember the range */
974 int i;
975 for (i = 0; i < rev.pending.nr; i++) {
976 struct object *o = rev.pending.objects[i].item;
977 if (!(o->flags & UNINTERESTING))
978 head = (struct commit *)o;
980 /* We can't generate a cover letter without any patches */
981 if (!head)
982 return 0;
985 if (ignore_if_in_upstream)
986 get_patch_ids(&rev, &ids, prefix);
988 if (!use_stdout)
989 realstdout = xfdopen(xdup(1), "w");
991 if (prepare_revision_walk(&rev))
992 die("revision walk setup failed");
993 rev.boundary = 1;
994 while ((commit = get_revision(&rev)) != NULL) {
995 if (commit->object.flags & BOUNDARY) {
996 boundary_count++;
997 origin = (boundary_count == 1) ? commit : NULL;
998 continue;
1001 /* ignore merges */
1002 if (commit->parents && commit->parents->next)
1003 continue;
1005 if (ignore_if_in_upstream &&
1006 has_commit_patch_id(commit, &ids))
1007 continue;
1009 nr++;
1010 list = xrealloc(list, nr * sizeof(list[0]));
1011 list[nr - 1] = commit;
1013 total = nr;
1014 if (!keep_subject && auto_number && total > 1)
1015 numbered = 1;
1016 if (numbered)
1017 rev.total = total + start_number - 1;
1018 if (in_reply_to || thread || cover_letter)
1019 rev.ref_message_ids = xcalloc(1, sizeof(struct string_list));
1020 if (in_reply_to) {
1021 const char *msgid = clean_message_id(in_reply_to);
1022 string_list_append(msgid, rev.ref_message_ids);
1024 if (cover_letter) {
1025 if (thread)
1026 gen_message_id(&rev, "cover");
1027 make_cover_letter(&rev, use_stdout, numbered, numbered_files,
1028 origin, nr, list, head);
1029 total++;
1030 start_number--;
1032 rev.add_signoff = add_signoff;
1033 while (0 <= --nr) {
1034 int shown;
1035 commit = list[nr];
1036 rev.nr = total - nr + (start_number - 1);
1037 /* Make the second and subsequent mails replies to the first */
1038 if (thread) {
1039 /* Have we already had a message ID? */
1040 if (rev.message_id) {
1042 * For deep threading: make every mail
1043 * a reply to the previous one, no
1044 * matter what other options are set.
1046 * For shallow threading:
1048 * Without --cover-letter and
1049 * --in-reply-to, make every mail a
1050 * reply to the one before.
1052 * With --in-reply-to but no
1053 * --cover-letter, make every mail a
1054 * reply to the <reply-to>.
1056 * With --cover-letter, make every
1057 * mail but the cover letter a reply
1058 * to the cover letter. The cover
1059 * letter is a reply to the
1060 * --in-reply-to, if specified.
1062 if (thread == THREAD_SHALLOW
1063 && rev.ref_message_ids->nr > 0
1064 && (!cover_letter || rev.nr > 1))
1065 free(rev.message_id);
1066 else
1067 string_list_append(rev.message_id,
1068 rev.ref_message_ids);
1070 gen_message_id(&rev, sha1_to_hex(commit->object.sha1));
1073 get_patch_filename(numbered_files ? NULL : commit, rev.nr,
1074 fmt_patch_suffix, &patch_filename);
1075 if (!use_stdout && reopen_stdout(patch_filename.buf, &rev))
1076 die("Failed to create output files");
1077 strbuf_setlen(&patch_filename, 0);
1078 shown = log_tree_commit(&rev, commit);
1079 free(commit->buffer);
1080 commit->buffer = NULL;
1082 /* We put one extra blank line between formatted
1083 * patches and this flag is used by log-tree code
1084 * to see if it needs to emit a LF before showing
1085 * the log; when using one file per patch, we do
1086 * not want the extra blank line.
1088 if (!use_stdout)
1089 rev.shown_one = 0;
1090 if (shown) {
1091 if (rev.mime_boundary)
1092 printf("\n--%s%s--\n\n\n",
1093 mime_boundary_leader,
1094 rev.mime_boundary);
1095 else
1096 printf("-- \n%s\n\n", git_version_string);
1098 if (!use_stdout)
1099 fclose(stdout);
1101 strbuf_release(&patch_filename);
1102 free(list);
1103 if (ignore_if_in_upstream)
1104 free_patch_ids(&ids);
1105 return 0;
1108 static int add_pending_commit(const char *arg, struct rev_info *revs, int flags)
1110 unsigned char sha1[20];
1111 if (get_sha1(arg, sha1) == 0) {
1112 struct commit *commit = lookup_commit_reference(sha1);
1113 if (commit) {
1114 commit->object.flags |= flags;
1115 add_pending_object(revs, &commit->object, arg);
1116 return 0;
1119 return -1;
1122 static const char cherry_usage[] =
1123 "git cherry [-v] [<upstream> [<head> [<limit>]]]";
1124 int cmd_cherry(int argc, const char **argv, const char *prefix)
1126 struct rev_info revs;
1127 struct patch_ids ids;
1128 struct commit *commit;
1129 struct commit_list *list = NULL;
1130 struct branch *current_branch;
1131 const char *upstream;
1132 const char *head = "HEAD";
1133 const char *limit = NULL;
1134 int verbose = 0;
1136 if (argc > 1 && !strcmp(argv[1], "-v")) {
1137 verbose = 1;
1138 argc--;
1139 argv++;
1142 switch (argc) {
1143 case 4:
1144 limit = argv[3];
1145 /* FALLTHROUGH */
1146 case 3:
1147 head = argv[2];
1148 /* FALLTHROUGH */
1149 case 2:
1150 upstream = argv[1];
1151 break;
1152 default:
1153 current_branch = branch_get(NULL);
1154 if (!current_branch || !current_branch->merge
1155 || !current_branch->merge[0]
1156 || !current_branch->merge[0]->dst) {
1157 fprintf(stderr, "Could not find a tracked"
1158 " remote branch, please"
1159 " specify <upstream> manually.\n");
1160 usage(cherry_usage);
1163 upstream = current_branch->merge[0]->dst;
1166 init_revisions(&revs, prefix);
1167 revs.diff = 1;
1168 revs.combine_merges = 0;
1169 revs.ignore_merges = 1;
1170 DIFF_OPT_SET(&revs.diffopt, RECURSIVE);
1172 if (add_pending_commit(head, &revs, 0))
1173 die("Unknown commit %s", head);
1174 if (add_pending_commit(upstream, &revs, UNINTERESTING))
1175 die("Unknown commit %s", upstream);
1177 /* Don't say anything if head and upstream are the same. */
1178 if (revs.pending.nr == 2) {
1179 struct object_array_entry *o = revs.pending.objects;
1180 if (hashcmp(o[0].item->sha1, o[1].item->sha1) == 0)
1181 return 0;
1184 get_patch_ids(&revs, &ids, prefix);
1186 if (limit && add_pending_commit(limit, &revs, UNINTERESTING))
1187 die("Unknown commit %s", limit);
1189 /* reverse the list of commits */
1190 if (prepare_revision_walk(&revs))
1191 die("revision walk setup failed");
1192 while ((commit = get_revision(&revs)) != NULL) {
1193 /* ignore merges */
1194 if (commit->parents && commit->parents->next)
1195 continue;
1197 commit_list_insert(commit, &list);
1200 while (list) {
1201 char sign = '+';
1203 commit = list->item;
1204 if (has_commit_patch_id(commit, &ids))
1205 sign = '-';
1207 if (verbose) {
1208 struct strbuf buf = STRBUF_INIT;
1209 pretty_print_commit(CMIT_FMT_ONELINE, commit,
1210 &buf, 0, NULL, NULL, 0, 0);
1211 printf("%c %s %s\n", sign,
1212 sha1_to_hex(commit->object.sha1), buf.buf);
1213 strbuf_release(&buf);
1215 else {
1216 printf("%c %s\n", sign,
1217 sha1_to_hex(commit->object.sha1));
1220 list = list->next;
1223 free_patch_ids(&ids);
1224 return 0;