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