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