Handle missing prefix for "Subject:" as if no prefix given
[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"
70827b15 17
0f03ca94
PB
18static int default_show_root = 1;
19
e686eb98
JS
20/* this is in builtin-diff.c */
21void add_head(struct rev_info *revs);
22
ca135e7a
LT
23static void add_name_decoration(const char *prefix, const char *name, struct object *obj)
24{
25 int plen = strlen(prefix);
26 int nlen = strlen(name);
27 struct name_decoration *res = xmalloc(sizeof(struct name_decoration) + plen + nlen);
28 memcpy(res->name, prefix, plen);
29 memcpy(res->name + plen, name, nlen + 1);
30 res->next = add_decoration(&name_decoration, obj, res);
31}
32
33static int add_ref_decoration(const char *refname, const unsigned char *sha1, int flags, void *cb_data)
34{
35 struct object *obj = parse_object(sha1);
36 if (!obj)
37 return 0;
38 add_name_decoration("", refname, obj);
39 while (obj->type == OBJ_TAG) {
40 obj = ((struct tag *)obj)->tagged;
41 if (!obj)
42 break;
43 add_name_decoration("tag: ", refname, obj);
44 }
45 return 0;
46}
47
a633fca0 48static void cmd_log_init(int argc, const char **argv, const char *prefix,
70827b15
LT
49 struct rev_info *rev)
50{
52883fbd 51 int i;
ca135e7a 52 int decorate = 0;
52883fbd 53
70827b15
LT
54 rev->abbrev = DEFAULT_ABBREV;
55 rev->commit_format = CMIT_FMT_DEFAULT;
56 rev->verbose_header = 1;
0f03ca94 57 rev->show_root_diff = default_show_root;
70827b15 58 argc = setup_revisions(argc, argv, rev, "HEAD");
9dafea26
TH
59 if (rev->diffopt.pickaxe || rev->diffopt.filter)
60 rev->always_show_header = 0;
750f7b66
LT
61 if (rev->diffopt.follow_renames) {
62 rev->always_show_header = 0;
63 if (rev->diffopt.nr_paths != 1)
64 usage("git logs can only follow renames on one pathname at a time");
65 }
52883fbd
JH
66 for (i = 1; i < argc; i++) {
67 const char *arg = argv[i];
66688334 68 if (!strcmp(arg, "--decorate")) {
ca135e7a
LT
69 if (!decorate)
70 for_each_ref(add_ref_decoration, NULL);
71 decorate = 1;
72 } else
52883fbd
JH
73 die("unrecognized argument: %s", arg);
74 }
9dafea26
TH
75}
76
77static int cmd_log_walk(struct rev_info *rev)
78{
79 struct commit *commit;
70827b15
LT
80
81 prepare_revision_walk(rev);
70827b15
LT
82 while ((commit = get_revision(rev)) != NULL) {
83 log_tree_commit(rev, commit);
a6c73064
JS
84 if (!rev->reflog_info) {
85 /* we allow cycles in reflog ancestry */
86 free(commit->buffer);
87 commit->buffer = NULL;
88 }
cb115748
LT
89 free_commit_list(commit->parents);
90 commit->parents = NULL;
70827b15
LT
91 }
92 return 0;
93}
94
0f03ca94
PB
95static int git_log_config(const char *var, const char *value)
96{
97 if (!strcmp(var, "log.showroot")) {
98 default_show_root = git_config_bool(var, value);
99 return 0;
100 }
101 return git_diff_ui_config(var, value);
102}
103
a633fca0 104int cmd_whatchanged(int argc, const char **argv, const char *prefix)
70827b15
LT
105{
106 struct rev_info rev;
107
0f03ca94 108 git_config(git_log_config);
db6296a5 109 init_revisions(&rev, prefix);
70827b15
LT
110 rev.diff = 1;
111 rev.diffopt.recursive = 1;
9202434c 112 rev.simplify_history = 0;
a633fca0 113 cmd_log_init(argc, argv, prefix, &rev);
9dafea26
TH
114 if (!rev.diffopt.output_format)
115 rev.diffopt.output_format = DIFF_FORMAT_RAW;
116 return cmd_log_walk(&rev);
70827b15
LT
117}
118
5d7eeee2
JS
119static int show_object(const unsigned char *sha1, int suppress_header)
120{
121 unsigned long size;
21666f1a
NP
122 enum object_type type;
123 char *buf = read_sha1_file(sha1, &type, &size);
5d7eeee2
JS
124 int offset = 0;
125
126 if (!buf)
127 return error("Could not read object %s", sha1_to_hex(sha1));
128
129 if (suppress_header)
130 while (offset < size && buf[offset++] != '\n') {
131 int new_offset = offset;
132 while (new_offset < size && buf[new_offset++] != '\n')
133 ; /* do nothing */
134 offset = new_offset;
135 }
136
137 if (offset < size)
138 fwrite(buf + offset, size - offset, 1, stdout);
139 free(buf);
140 return 0;
141}
142
143static int show_tree_object(const unsigned char *sha1,
144 const char *base, int baselen,
145 const char *pathname, unsigned mode, int stage)
146{
147 printf("%s%s\n", pathname, S_ISDIR(mode) ? "/" : "");
148 return 0;
149}
150
a633fca0 151int cmd_show(int argc, const char **argv, const char *prefix)
70827b15
LT
152{
153 struct rev_info rev;
5d7eeee2
JS
154 struct object_array_entry *objects;
155 int i, count, ret = 0;
70827b15 156
0f03ca94 157 git_config(git_log_config);
db6296a5 158 init_revisions(&rev, prefix);
70827b15
LT
159 rev.diff = 1;
160 rev.diffopt.recursive = 1;
161 rev.combine_merges = 1;
162 rev.dense_combined_merges = 1;
163 rev.always_show_header = 1;
164 rev.ignore_merges = 0;
165 rev.no_walk = 1;
a633fca0 166 cmd_log_init(argc, argv, prefix, &rev);
5d7eeee2
JS
167
168 count = rev.pending.nr;
169 objects = rev.pending.objects;
170 for (i = 0; i < count && !ret; i++) {
171 struct object *o = objects[i].item;
172 const char *name = objects[i].name;
173 switch (o->type) {
174 case OBJ_BLOB:
175 ret = show_object(o->sha1, 0);
176 break;
177 case OBJ_TAG: {
178 struct tag *t = (struct tag *)o;
179
180 printf("%stag %s%s\n\n",
181 diff_get_color(rev.diffopt.color_diff,
182 DIFF_COMMIT),
183 t->tag,
184 diff_get_color(rev.diffopt.color_diff,
185 DIFF_RESET));
186 ret = show_object(o->sha1, 1);
187 objects[i].item = (struct object *)t->tagged;
188 i--;
189 break;
190 }
191 case OBJ_TREE:
192 printf("%stree %s%s\n\n",
193 diff_get_color(rev.diffopt.color_diff,
194 DIFF_COMMIT),
195 name,
196 diff_get_color(rev.diffopt.color_diff,
197 DIFF_RESET));
198 read_tree_recursive((struct tree *)o, "", 0, 0, NULL,
199 show_tree_object);
200 break;
201 case OBJ_COMMIT:
202 rev.pending.nr = rev.pending.alloc = 0;
203 rev.pending.objects = NULL;
204 add_object_array(o, name, &rev.pending);
205 ret = cmd_log_walk(&rev);
206 break;
207 default:
208 ret = error("Unknown type: %d", o->type);
209 }
210 }
211 free(objects);
212 return ret;
70827b15
LT
213}
214
cf39f54e
LT
215/*
216 * This is equivalent to "git log -g --abbrev-commit --pretty=oneline"
217 */
218int cmd_log_reflog(int argc, const char **argv, const char *prefix)
219{
220 struct rev_info rev;
221
222 git_config(git_log_config);
223 init_revisions(&rev, prefix);
224 init_reflog_walk(&rev.reflog_info);
225 rev.abbrev_commit = 1;
226 rev.verbose_header = 1;
227 cmd_log_init(argc, argv, prefix, &rev);
228
229 /*
230 * This means that we override whatever commit format the user gave
231 * on the cmd line. Sad, but cmd_log_init() currently doesn't
232 * allow us to set a different default.
233 */
234 rev.commit_format = CMIT_FMT_ONELINE;
235 rev.always_show_header = 1;
236
237 /*
238 * We get called through "git reflog", so unlike the other log
239 * routines, we need to set up our pager manually..
240 */
241 setup_pager();
242
243 return cmd_log_walk(&rev);
244}
245
a633fca0 246int cmd_log(int argc, const char **argv, const char *prefix)
70827b15
LT
247{
248 struct rev_info rev;
249
0f03ca94 250 git_config(git_log_config);
db6296a5 251 init_revisions(&rev, prefix);
70827b15 252 rev.always_show_header = 1;
a633fca0 253 cmd_log_init(argc, argv, prefix, &rev);
9dafea26 254 return cmd_log_walk(&rev);
70827b15 255}
91efcf60 256
c06d2daa
RR
257/* format-patch */
258#define FORMAT_PATCH_NAME_MAX 64
259
0377db77
JS
260static int istitlechar(char c)
261{
262 return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') ||
263 (c >= '0' && c <= '9') || c == '.' || c == '_';
264}
265
20ff0680
JS
266static char *extra_headers = NULL;
267static int extra_headers_size = 0;
dbd21447 268static const char *fmt_patch_subject_prefix = "PATCH";
917a8f89 269static const char *fmt_patch_suffix = ".patch";
20ff0680
JS
270
271static int git_format_config(const char *var, const char *value)
272{
273 if (!strcmp(var, "format.headers")) {
d7fb91c6
JH
274 int len;
275
276 if (!value)
277 die("format.headers without value");
278 len = strlen(value);
20ff0680 279 extra_headers_size += len + 1;
83572c1a 280 extra_headers = xrealloc(extra_headers, extra_headers_size);
20ff0680
JS
281 extra_headers[extra_headers_size - len - 1] = 0;
282 strcat(extra_headers, value);
283 return 0;
284 }
03eeaeae
JH
285 if (!strcmp(var, "format.suffix")) {
286 if (!value)
287 die("format.suffix without value");
288 fmt_patch_suffix = xstrdup(value);
289 return 0;
290 }
a159ca0c 291 if (!strcmp(var, "diff.color") || !strcmp(var, "color.diff")) {
f3aafa4d
RA
292 return 0;
293 }
dbd21447
AR
294 if (!strcmp(var, "format.subjectprefix")) {
295 if (!value)
296 die("format.subjectprefix without value");
297 fmt_patch_subject_prefix = xstrdup(value);
298 return 0;
299 }
300
0f03ca94 301 return git_log_config(var, value);
20ff0680
JS
302}
303
304
81f3a188 305static FILE *realstdout = NULL;
efd02016 306static const char *output_directory = NULL;
81f3a188 307
e6ff0f42
JL
308static int reopen_stdout(struct commit *commit, int nr, int keep_subject,
309 int numbered_files)
0377db77 310{
c06d2daa 311 char filename[PATH_MAX];
0377db77 312 char *sol;
2448482b 313 int len = 0;
c06d2daa 314 int suffix_len = strlen(fmt_patch_suffix) + 1;
0377db77 315
2448482b 316 if (output_directory) {
c06d2daa
RR
317 if (strlen(output_directory) >=
318 sizeof(filename) - FORMAT_PATCH_NAME_MAX - suffix_len)
319 return error("name of output directory is too long");
320 strlcpy(filename, output_directory, sizeof(filename) - suffix_len);
2448482b
JS
321 len = strlen(filename);
322 if (filename[len - 1] != '/')
323 filename[len++] = '/';
324 }
0377db77 325
e6ff0f42
JL
326 if (numbered_files) {
327 sprintf(filename + len, "%d", nr);
328 len = strlen(filename);
0377db77 329
e6ff0f42
JL
330 } else {
331 sprintf(filename + len, "%04d", nr);
332 len = strlen(filename);
333
334 sol = strstr(commit->buffer, "\n\n");
335 if (sol) {
336 int j, space = 1;
337
338 sol += 2;
339 /* strip [PATCH] or [PATCH blabla] */
340 if (!keep_subject && !prefixcmp(sol, "[PATCH")) {
341 char *eos = strchr(sol + 6, ']');
342 if (eos) {
343 while (isspace(*eos))
344 eos++;
345 sol = eos;
0377db77 346 }
e6ff0f42
JL
347 }
348
349 for (j = 0;
350 j < FORMAT_PATCH_NAME_MAX - suffix_len - 5 &&
351 len < sizeof(filename) - suffix_len &&
352 sol[j] && sol[j] != '\n';
353 j++) {
354 if (istitlechar(sol[j])) {
355 if (space) {
356 filename[len++] = '-';
357 space = 0;
358 }
359 filename[len++] = sol[j];
360 if (sol[j] == '.')
361 while (sol[j + 1] == '.')
362 j++;
363 } else
364 space = 1;
365 }
366 while (filename[len - 1] == '.'
367 || filename[len - 1] == '-')
368 len--;
369 filename[len] = 0;
0377db77 370 }
e6ff0f42
JL
371 if (len + suffix_len >= sizeof(filename))
372 return error("Patch pathname too long");
373 strcpy(filename + len, fmt_patch_suffix);
0377db77 374 }
e6ff0f42 375
81f3a188 376 fprintf(realstdout, "%s\n", filename);
c06d2daa
RR
377 if (freopen(filename, "w", stdout) == NULL)
378 return error("Cannot open patch file %s",filename);
c06d2daa 379
e6ff0f42 380 return 0;
0377db77
JS
381}
382
5d23e133 383static void get_patch_ids(struct rev_info *rev, struct patch_ids *ids, const char *prefix)
9c6efa36
JS
384{
385 struct rev_info check_rev;
386 struct commit *commit;
387 struct object *o1, *o2;
388 unsigned flags1, flags2;
9c6efa36
JS
389
390 if (rev->pending.nr != 2)
391 die("Need exactly one range.");
392
393 o1 = rev->pending.objects[0].item;
394 flags1 = o1->flags;
395 o2 = rev->pending.objects[1].item;
396 flags2 = o2->flags;
397
398 if ((flags1 & UNINTERESTING) == (flags2 & UNINTERESTING))
399 die("Not a range.");
400
5d23e133 401 init_patch_ids(ids);
9c6efa36
JS
402
403 /* given a range a..b get all patch ids for b..a */
db6296a5 404 init_revisions(&check_rev, prefix);
9c6efa36
JS
405 o1->flags ^= UNINTERESTING;
406 o2->flags ^= UNINTERESTING;
407 add_pending_object(&check_rev, o1, "o1");
408 add_pending_object(&check_rev, o2, "o2");
409 prepare_revision_walk(&check_rev);
410
411 while ((commit = get_revision(&check_rev)) != NULL) {
412 /* ignore merges */
413 if (commit->parents && commit->parents->next)
414 continue;
415
5d23e133 416 add_commit_patch_id(commit, ids);
9c6efa36
JS
417 }
418
419 /* reset for next revision walk */
81db0941
JS
420 clear_commit_marks((struct commit *)o1,
421 SEEN | UNINTERESTING | SHOWN | ADDED);
422 clear_commit_marks((struct commit *)o2,
423 SEEN | UNINTERESTING | SHOWN | ADDED);
9c6efa36
JS
424 o1->flags = flags1;
425 o2->flags = flags2;
426}
427
d1566f78
JT
428static void gen_message_id(char *dest, unsigned int length, char *base)
429{
cb280e10 430 const char *committer = git_committer_info(-1);
d1566f78
JT
431 const char *email_start = strrchr(committer, '<');
432 const char *email_end = strrchr(committer, '>');
433 if(!email_start || !email_end || email_start > email_end - 1)
434 die("Could not extract email from committer identity.");
76af0734
JH
435 snprintf(dest, length, "%s.%lu.git.%.*s", base,
436 (unsigned long) time(NULL),
437 (int)(email_end - email_start - 1), email_start + 1);
d1566f78
JT
438}
439
a633fca0 440int cmd_format_patch(int argc, const char **argv, const char *prefix)
91efcf60
JH
441{
442 struct commit *commit;
443 struct commit **list = NULL;
444 struct rev_info rev;
2448482b 445 int nr = 0, total, i, j;
0377db77 446 int use_stdout = 0;
596524b3 447 int numbered = 0;
fa0f02df 448 int start_number = -1;
8ac80a57 449 int keep_subject = 0;
e6ff0f42 450 int numbered_files = 0; /* _just_ numbers */
2d9e4a47 451 int subject_prefix = 0;
9c6efa36 452 int ignore_if_in_upstream = 0;
cc35de84 453 int thread = 0;
76af0734 454 const char *in_reply_to = NULL;
5d23e133 455 struct patch_ids ids;
cf2251b6 456 char *add_signoff = NULL;
d1566f78
JT
457 char message_id[1024];
458 char ref_message_id[1024];
91efcf60 459
97beb812 460 git_config(git_format_config);
db6296a5 461 init_revisions(&rev, prefix);
91efcf60
JH
462 rev.commit_format = CMIT_FMT_EMAIL;
463 rev.verbose_header = 1;
464 rev.diff = 1;
91efcf60
JH
465 rev.combine_merges = 0;
466 rev.ignore_merges = 1;
27e1b127
JH
467 rev.diffopt.msg_sep = "";
468 rev.diffopt.recursive = 1;
91efcf60 469
dbd21447 470 rev.subject_prefix = fmt_patch_subject_prefix;
20ff0680
JS
471 rev.extra_headers = extra_headers;
472
2448482b
JS
473 /*
474 * Parse the arguments before setup_revisions(), or something
2dc189a3 475 * like "git format-patch -o a123 HEAD^.." may fail; a123 is
2448482b
JS
476 * possibly a valid SHA1.
477 */
478 for (i = 1, j = 1; i < argc; i++) {
479 if (!strcmp(argv[i], "--stdout"))
0377db77 480 use_stdout = 1;
596524b3
JS
481 else if (!strcmp(argv[i], "-n") ||
482 !strcmp(argv[i], "--numbered"))
483 numbered = 1;
cc44c765 484 else if (!prefixcmp(argv[i], "--start-number="))
fa0f02df 485 start_number = strtol(argv[i] + 15, NULL, 10);
e6ff0f42
JL
486 else if (!strcmp(argv[i], "--numbered-files"))
487 numbered_files = 1;
fa0f02df
JS
488 else if (!strcmp(argv[i], "--start-number")) {
489 i++;
490 if (i == argc)
491 die("Need a number for --start-number");
492 start_number = strtol(argv[i], NULL, 10);
cf2251b6
JH
493 }
494 else if (!strcmp(argv[i], "-k") ||
8ac80a57
JS
495 !strcmp(argv[i], "--keep-subject")) {
496 keep_subject = 1;
497 rev.total = -1;
cf2251b6 498 }
efd02016
JH
499 else if (!strcmp(argv[i], "--output-directory") ||
500 !strcmp(argv[i], "-o")) {
2448482b 501 i++;
efd02016
JH
502 if (argc <= i)
503 die("Which directory?");
504 if (output_directory)
505 die("Two output directories?");
506 output_directory = argv[i];
698ce6f8 507 }
cf2251b6
JH
508 else if (!strcmp(argv[i], "--signoff") ||
509 !strcmp(argv[i], "-s")) {
6c4cca1c
EB
510 const char *committer;
511 const char *endpos;
6c4cca1c
EB
512 committer = git_committer_info(1);
513 endpos = strchr(committer, '>');
cf2251b6
JH
514 if (!endpos)
515 die("bogos committer info %s\n", committer);
516 add_signoff = xmalloc(endpos - committer + 2);
517 memcpy(add_signoff, committer, endpos - committer + 1);
518 add_signoff[endpos - committer + 1] = 0;
519 }
c112f689 520 else if (!strcmp(argv[i], "--attach")) {
698ce6f8 521 rev.mime_boundary = git_version_string;
c112f689
JS
522 rev.no_inline = 1;
523 }
524 else if (!prefixcmp(argv[i], "--attach=")) {
525 rev.mime_boundary = argv[i] + 9;
526 rev.no_inline = 1;
527 }
528 else if (!strcmp(argv[i], "--inline")) {
529 rev.mime_boundary = git_version_string;
530 rev.no_inline = 0;
531 }
532 else if (!prefixcmp(argv[i], "--inline=")) {
698ce6f8 533 rev.mime_boundary = argv[i] + 9;
c112f689
JS
534 rev.no_inline = 0;
535 }
9c6efa36
JS
536 else if (!strcmp(argv[i], "--ignore-if-in-upstream"))
537 ignore_if_in_upstream = 1;
cc35de84
JT
538 else if (!strcmp(argv[i], "--thread"))
539 thread = 1;
cc44c765 540 else if (!prefixcmp(argv[i], "--in-reply-to="))
da56645d
JT
541 in_reply_to = argv[i] + 14;
542 else if (!strcmp(argv[i], "--in-reply-to")) {
543 i++;
544 if (i == argc)
545 die("Need a Message-Id for --in-reply-to");
546 in_reply_to = argv[i];
2d9e4a47
RJ
547 } else if (!prefixcmp(argv[i], "--subject-prefix=")) {
548 subject_prefix = 1;
549 rev.subject_prefix = argv[i] + 17;
550 } else if (!prefixcmp(argv[i], "--suffix="))
03eeaeae 551 fmt_patch_suffix = argv[i] + 9;
698ce6f8 552 else
2448482b 553 argv[j++] = argv[i];
0377db77 554 }
2448482b
JS
555 argc = j;
556
add5c8a5 557 if (start_number < 0)
fa0f02df 558 start_number = 1;
63b398a4 559 if (numbered && keep_subject)
8ac80a57 560 die ("-n and -k are mutually exclusive.");
2d9e4a47
RJ
561 if (keep_subject && subject_prefix)
562 die ("--subject-prefix and -k are mutually exclusive.");
e6ff0f42
JL
563 if (numbered_files && use_stdout)
564 die ("--numbered-files and --stdout are mutually exclusive.");
8ac80a57 565
2448482b
JS
566 argc = setup_revisions(argc, argv, &rev, "HEAD");
567 if (argc > 1)
568 die ("unrecognized argument: %s", argv[1]);
0377db77 569
c9b5ef99 570 if (!rev.diffopt.output_format)
c261e438 571 rev.diffopt.output_format = DIFF_FORMAT_DIFFSTAT | DIFF_FORMAT_SUMMARY | DIFF_FORMAT_PATCH;
c9b5ef99 572
e47f306d
JH
573 if (!rev.diffopt.text)
574 rev.diffopt.binary = 1;
575
90f70a91 576 if (!output_directory && !use_stdout)
77e565d8
ML
577 output_directory = prefix;
578
efd02016
JH
579 if (output_directory) {
580 if (use_stdout)
581 die("standard output, or directory, which one?");
582 if (mkdir(output_directory, 0777) < 0 && errno != EEXIST)
583 die("Could not create directory %s",
584 output_directory);
585 }
586
1f1e895f 587 if (rev.pending.nr == 1) {
7c496280
JH
588 if (rev.max_count < 0) {
589 rev.pending.objects[0].item->flags |= UNINTERESTING;
590 add_head(&rev);
591 }
592 /* Otherwise, it is "format-patch -22 HEAD", and
593 * get_revision() would return only the specified count.
594 */
e686eb98
JS
595 }
596
9c6efa36 597 if (ignore_if_in_upstream)
5d23e133 598 get_patch_ids(&rev, &ids, prefix);
9c6efa36 599
81f3a188 600 if (!use_stdout)
f5788250 601 realstdout = xfdopen(xdup(1), "w");
81f3a188 602
91efcf60
JH
603 prepare_revision_walk(&rev);
604 while ((commit = get_revision(&rev)) != NULL) {
0377db77
JS
605 /* ignore merges */
606 if (commit->parents && commit->parents->next)
607 continue;
9c6efa36
JS
608
609 if (ignore_if_in_upstream &&
5d23e133 610 has_commit_patch_id(commit, &ids))
9c6efa36
JS
611 continue;
612
91efcf60 613 nr++;
83572c1a 614 list = xrealloc(list, nr * sizeof(list[0]));
91efcf60
JH
615 list[nr - 1] = commit;
616 }
0377db77 617 total = nr;
596524b3 618 if (numbered)
fa0f02df 619 rev.total = total + start_number - 1;
cf2251b6 620 rev.add_signoff = add_signoff;
da56645d 621 rev.ref_message_id = in_reply_to;
91efcf60
JH
622 while (0 <= --nr) {
623 int shown;
624 commit = list[nr];
add5c8a5 625 rev.nr = total - nr + (start_number - 1);
d1566f78 626 /* Make the second and subsequent mails replies to the first */
cc35de84
JT
627 if (thread) {
628 if (nr == (total - 2)) {
629 strncpy(ref_message_id, message_id,
630 sizeof(ref_message_id));
631 ref_message_id[sizeof(ref_message_id)-1]='\0';
632 rev.ref_message_id = ref_message_id;
633 }
634 gen_message_id(message_id, sizeof(message_id),
635 sha1_to_hex(commit->object.sha1));
636 rev.message_id = message_id;
d1566f78 637 }
0377db77 638 if (!use_stdout)
e6ff0f42
JL
639 if (reopen_stdout(commit, rev.nr, keep_subject,
640 numbered_files))
c06d2daa 641 die("Failed to create output files");
91efcf60
JH
642 shown = log_tree_commit(&rev, commit);
643 free(commit->buffer);
644 commit->buffer = NULL;
add5c8a5
JH
645
646 /* We put one extra blank line between formatted
647 * patches and this flag is used by log-tree code
648 * to see if it needs to emit a LF before showing
649 * the log; when using one file per patch, we do
650 * not want the extra blank line.
651 */
652 if (!use_stdout)
653 rev.shown_one = 0;
698ce6f8
JS
654 if (shown) {
655 if (rev.mime_boundary)
656 printf("\n--%s%s--\n\n\n",
657 mime_boundary_leader,
658 rev.mime_boundary);
659 else
660 printf("-- \n%s\n\n", git_version_string);
661 }
0377db77
JS
662 if (!use_stdout)
663 fclose(stdout);
91efcf60
JH
664 }
665 free(list);
5d23e133
JH
666 if (ignore_if_in_upstream)
667 free_patch_ids(&ids);
91efcf60
JH
668 return 0;
669}
670
e827633a
RS
671static int add_pending_commit(const char *arg, struct rev_info *revs, int flags)
672{
673 unsigned char sha1[20];
674 if (get_sha1(arg, sha1) == 0) {
675 struct commit *commit = lookup_commit_reference(sha1);
676 if (commit) {
677 commit->object.flags |= flags;
678 add_pending_object(revs, &commit->object, arg);
679 return 0;
680 }
681 }
682 return -1;
683}
684
685static const char cherry_usage[] =
686"git-cherry [-v] <upstream> [<head>] [<limit>]";
687int cmd_cherry(int argc, const char **argv, const char *prefix)
688{
689 struct rev_info revs;
5d23e133 690 struct patch_ids ids;
e827633a
RS
691 struct commit *commit;
692 struct commit_list *list = NULL;
693 const char *upstream;
694 const char *head = "HEAD";
695 const char *limit = NULL;
696 int verbose = 0;
697
698 if (argc > 1 && !strcmp(argv[1], "-v")) {
699 verbose = 1;
700 argc--;
701 argv++;
702 }
703
704 switch (argc) {
705 case 4:
706 limit = argv[3];
707 /* FALLTHROUGH */
708 case 3:
709 head = argv[2];
710 /* FALLTHROUGH */
711 case 2:
712 upstream = argv[1];
713 break;
714 default:
715 usage(cherry_usage);
716 }
717
718 init_revisions(&revs, prefix);
719 revs.diff = 1;
720 revs.combine_merges = 0;
721 revs.ignore_merges = 1;
722 revs.diffopt.recursive = 1;
723
724 if (add_pending_commit(head, &revs, 0))
725 die("Unknown commit %s", head);
726 if (add_pending_commit(upstream, &revs, UNINTERESTING))
727 die("Unknown commit %s", upstream);
728
729 /* Don't say anything if head and upstream are the same. */
730 if (revs.pending.nr == 2) {
731 struct object_array_entry *o = revs.pending.objects;
732 if (hashcmp(o[0].item->sha1, o[1].item->sha1) == 0)
733 return 0;
734 }
735
5d23e133 736 get_patch_ids(&revs, &ids, prefix);
e827633a
RS
737
738 if (limit && add_pending_commit(limit, &revs, UNINTERESTING))
739 die("Unknown commit %s", limit);
740
741 /* reverse the list of commits */
742 prepare_revision_walk(&revs);
743 while ((commit = get_revision(&revs)) != NULL) {
744 /* ignore merges */
745 if (commit->parents && commit->parents->next)
746 continue;
747
748 commit_list_insert(commit, &list);
749 }
750
751 while (list) {
e827633a
RS
752 char sign = '+';
753
754 commit = list->item;
5d23e133 755 if (has_commit_patch_id(commit, &ids))
e827633a
RS
756 sign = '-';
757
758 if (verbose) {
80583c0e
JH
759 char *buf = NULL;
760 unsigned long buflen = 0;
e827633a 761 pretty_print_commit(CMIT_FMT_ONELINE, commit, ~0,
80583c0e 762 &buf, &buflen, 0, NULL, NULL, 0);
e827633a
RS
763 printf("%c %s %s\n", sign,
764 sha1_to_hex(commit->object.sha1), buf);
80583c0e 765 free(buf);
e827633a
RS
766 }
767 else {
768 printf("%c %s\n", sign,
769 sha1_to_hex(commit->object.sha1));
770 }
771
772 list = list->next;
773 }
774
5d23e133 775 free_patch_ids(&ids);
e827633a
RS
776 return 0;
777}