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