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