]> git.ipfire.org Git - thirdparty/git.git/blame - revision.c
revision.c: allow handle_revision_arg() to take other flags
[thirdparty/git.git] / revision.c
CommitLineData
ae563542
LT
1#include "cache.h"
2#include "tag.h"
3#include "blob.h"
4#include "tree.h"
5#include "commit.h"
a4a88b2b 6#include "diff.h"
ae563542
LT
7#include "refs.h"
8#include "revision.h"
7fefda5c 9#include "graph.h"
8ecae9b0 10#include "grep.h"
8860fd42 11#include "reflog-walk.h"
d7a17cad 12#include "patch-ids.h"
f35f5603 13#include "decorate.h"
78892e32 14#include "log-tree.h"
894a9d33 15#include "string-list.h"
ae563542 16
cdcefbc9
LT
17volatile show_early_output_fn_t show_early_output;
18
cf2ab916 19char *path_name(const struct name_path *path, const char *name)
ae563542 20{
cf2ab916 21 const struct name_path *p;
ae563542
LT
22 char *n, *m;
23 int nlen = strlen(name);
24 int len = nlen + 1;
25
26 for (p = path; p; p = p->up) {
27 if (p->elem_len)
28 len += p->elem_len + 1;
29 }
30 n = xmalloc(len);
31 m = n + len - (nlen + 1);
32 strcpy(m, name);
33 for (p = path; p; p = p->up) {
34 if (p->elem_len) {
35 m -= p->elem_len + 1;
36 memcpy(m, p->elem, p->elem_len);
37 m[p->elem_len] = '/';
38 }
39 }
40 return n;
41}
42
beba25ab
JH
43static int show_path_component_truncated(FILE *out, const char *name, int len)
44{
45 int cnt;
46 for (cnt = 0; cnt < len; cnt++) {
47 int ch = name[cnt];
48 if (!ch || ch == '\n')
49 return -1;
50 fputc(ch, out);
51 }
52 return len;
53}
54
55static int show_path_truncated(FILE *out, const struct name_path *path)
56{
57 int emitted, ours;
58
59 if (!path)
60 return 0;
61 emitted = show_path_truncated(out, path->up);
62 if (emitted < 0)
63 return emitted;
64 if (emitted)
65 fputc('/', out);
66 ours = show_path_component_truncated(out, path->elem, path->elem_len);
67 if (ours < 0)
68 return ours;
69 return ours || emitted;
70}
71
91f17516
JH
72void show_object_with_name(FILE *out, struct object *obj, const struct name_path *path, const char *component)
73{
beba25ab
JH
74 struct name_path leaf;
75 leaf.up = (struct name_path *)path;
76 leaf.elem = component;
77 leaf.elem_len = strlen(component);
91f17516 78
beba25ab
JH
79 fprintf(out, "%s ", sha1_to_hex(obj->sha1));
80 show_path_truncated(out, &leaf);
81 fputc('\n', out);
91f17516
JH
82}
83
1f1e895f
LT
84void add_object(struct object *obj,
85 struct object_array *p,
86 struct name_path *path,
87 const char *name)
ae563542 88{
1f1e895f 89 add_object_array(obj, path_name(path, name), p);
ae563542
LT
90}
91
92static void mark_blob_uninteresting(struct blob *blob)
93{
c1ee9013
MK
94 if (!blob)
95 return;
ae563542
LT
96 if (blob->object.flags & UNINTERESTING)
97 return;
98 blob->object.flags |= UNINTERESTING;
99}
100
101void mark_tree_uninteresting(struct tree *tree)
102{
f75e53ed 103 struct tree_desc desc;
4c068a98 104 struct name_entry entry;
ae563542 105 struct object *obj = &tree->object;
ae563542 106
c1ee9013
MK
107 if (!tree)
108 return;
ae563542
LT
109 if (obj->flags & UNINTERESTING)
110 return;
111 obj->flags |= UNINTERESTING;
112 if (!has_sha1_file(obj->sha1))
113 return;
114 if (parse_tree(tree) < 0)
115 die("bad tree %s", sha1_to_hex(obj->sha1));
f75e53ed 116
6fda5e51 117 init_tree_desc(&desc, tree->buffer, tree->size);
4c068a98 118 while (tree_entry(&desc, &entry)) {
4d1012c3
LT
119 switch (object_type(entry.mode)) {
120 case OBJ_TREE:
4c068a98 121 mark_tree_uninteresting(lookup_tree(entry.sha1));
4d1012c3
LT
122 break;
123 case OBJ_BLOB:
4c068a98 124 mark_blob_uninteresting(lookup_blob(entry.sha1));
4d1012c3
LT
125 break;
126 default:
127 /* Subproject commit - not in this repository */
128 break;
129 }
ae563542 130 }
f75e53ed
LT
131
132 /*
133 * We don't care about the tree any more
134 * after it has been marked uninteresting.
135 */
136 free(tree->buffer);
137 tree->buffer = NULL;
ae563542
LT
138}
139
140void mark_parents_uninteresting(struct commit *commit)
141{
142 struct commit_list *parents = commit->parents;
143
144 while (parents) {
145 struct commit *commit = parents->item;
d2c4af73
MU
146 if (!(commit->object.flags & UNINTERESTING)) {
147 commit->object.flags |= UNINTERESTING;
ae563542 148
d2c4af73
MU
149 /*
150 * Normally we haven't parsed the parent
151 * yet, so we won't have a parent of a parent
152 * here. However, it may turn out that we've
153 * reached this commit some other way (where it
154 * wasn't uninteresting), in which case we need
155 * to mark its parents recursively too..
156 */
157 if (commit->parents)
158 mark_parents_uninteresting(commit);
159 }
ae563542
LT
160
161 /*
162 * A missing commit is ok iff its parent is marked
163 * uninteresting.
164 *
165 * We just mark such a thing parsed, so that when
166 * it is popped next time around, we won't be trying
167 * to parse it and get an error.
168 */
169 if (!has_sha1_file(commit->object.sha1))
170 commit->object.parsed = 1;
171 parents = parents->next;
172 }
173}
174
2d93b9fa 175static void add_pending_object_with_mode(struct rev_info *revs, struct object *obj, const char *name, unsigned mode)
ae563542 176{
cc243c3c
JH
177 if (!obj)
178 return;
aa27e461 179 if (revs->no_walk && (obj->flags & UNINTERESTING))
f222abde 180 revs->no_walk = 0;
105e4733
JH
181 if (revs->reflog_info && obj->type == OBJ_COMMIT) {
182 struct strbuf buf = STRBUF_INIT;
183 int len = interpret_branch_name(name, &buf);
184 int st;
185
186 if (0 < len && name[len] && buf.len)
187 strbuf_addstr(&buf, name + len);
188 st = add_reflog_for_walk(revs->reflog_info,
189 (struct commit *)obj,
190 buf.buf[0] ? buf.buf: name);
191 strbuf_release(&buf);
192 if (st)
193 return;
194 }
bb6c2fba 195 add_object_array_with_mode(obj, name, &revs->pending, mode);
ae563542
LT
196}
197
2d93b9fa
JH
198void add_pending_object(struct rev_info *revs, struct object *obj, const char *name)
199{
200 add_pending_object_with_mode(revs, obj, name, S_IFINVALID);
201}
202
3384a2df
JH
203void add_head_to_pending(struct rev_info *revs)
204{
205 unsigned char sha1[20];
206 struct object *obj;
207 if (get_sha1("HEAD", sha1))
208 return;
209 obj = parse_object(sha1);
210 if (!obj)
211 return;
212 add_pending_object(revs, obj, "HEAD");
213}
214
cd2bdc53 215static struct object *get_reference(struct rev_info *revs, const char *name, const unsigned char *sha1, unsigned int flags)
ae563542
LT
216{
217 struct object *object;
218
219 object = parse_object(sha1);
cc243c3c
JH
220 if (!object) {
221 if (revs->ignore_missing)
222 return object;
ae563542 223 die("bad object %s", name);
cc243c3c 224 }
cd2bdc53
LT
225 object->flags |= flags;
226 return object;
227}
228
26c3177e
RS
229void add_pending_sha1(struct rev_info *revs, const char *name,
230 const unsigned char *sha1, unsigned int flags)
231{
232 struct object *object = get_reference(revs, name, sha1, flags);
233 add_pending_object(revs, object, name);
234}
235
cd2bdc53
LT
236static struct commit *handle_commit(struct rev_info *revs, struct object *object, const char *name)
237{
238 unsigned long flags = object->flags;
ae563542
LT
239
240 /*
241 * Tag object? Look what it points to..
242 */
1974632c 243 while (object->type == OBJ_TAG) {
ae563542 244 struct tag *tag = (struct tag *) object;
cd2bdc53 245 if (revs->tag_objects && !(flags & UNINTERESTING))
ae563542 246 add_pending_object(revs, object, tag->tag);
9684afd9
MK
247 if (!tag->tagged)
248 die("bad tag");
ae563542 249 object = parse_object(tag->tagged->sha1);
aeeae1b7
JH
250 if (!object) {
251 if (flags & UNINTERESTING)
252 return NULL;
ae563542 253 die("bad object %s", sha1_to_hex(tag->tagged->sha1));
aeeae1b7 254 }
ae563542
LT
255 }
256
257 /*
258 * Commit object? Just return it, we'll do all the complex
259 * reachability crud.
260 */
1974632c 261 if (object->type == OBJ_COMMIT) {
ae563542 262 struct commit *commit = (struct commit *)object;
ae563542
LT
263 if (parse_commit(commit) < 0)
264 die("unable to parse commit %s", name);
d9a83684 265 if (flags & UNINTERESTING) {
4262c1b0 266 commit->object.flags |= UNINTERESTING;
ae563542 267 mark_parents_uninteresting(commit);
d9a83684
LT
268 revs->limited = 1;
269 }
0f3a290b
LT
270 if (revs->show_source && !commit->util)
271 commit->util = (void *) name;
ae563542
LT
272 return commit;
273 }
274
275 /*
3ea3c215 276 * Tree object? Either mark it uninteresting, or add it
ae563542
LT
277 * to the list of objects to look at later..
278 */
1974632c 279 if (object->type == OBJ_TREE) {
ae563542
LT
280 struct tree *tree = (struct tree *)object;
281 if (!revs->tree_objects)
282 return NULL;
283 if (flags & UNINTERESTING) {
284 mark_tree_uninteresting(tree);
285 return NULL;
286 }
287 add_pending_object(revs, object, "");
288 return NULL;
289 }
290
291 /*
292 * Blob object? You know the drill by now..
293 */
1974632c 294 if (object->type == OBJ_BLOB) {
ae563542
LT
295 struct blob *blob = (struct blob *)object;
296 if (!revs->blob_objects)
297 return NULL;
298 if (flags & UNINTERESTING) {
299 mark_blob_uninteresting(blob);
300 return NULL;
301 }
302 add_pending_object(revs, object, "");
303 return NULL;
304 }
305 die("%s is unknown object", name);
306}
307
a4a88b2b
LT
308static int everybody_uninteresting(struct commit_list *orig)
309{
310 struct commit_list *list = orig;
311 while (list) {
312 struct commit *commit = list->item;
313 list = list->next;
314 if (commit->object.flags & UNINTERESTING)
315 continue;
316 return 0;
317 }
318 return 1;
319}
320
0a4ba7f8
JH
321/*
322 * The goal is to get REV_TREE_NEW as the result only if the
ceff8e7a
LT
323 * diff consists of all '+' (and no other changes), REV_TREE_OLD
324 * if the whole diff is removal of old data, and otherwise
325 * REV_TREE_DIFFERENT (of course if the trees are the same we
326 * want REV_TREE_SAME).
327 * That means that once we get to REV_TREE_DIFFERENT, we do not
328 * have to look any further.
0a4ba7f8 329 */
8efdc326 330static int tree_difference = REV_TREE_SAME;
a4a88b2b
LT
331
332static void file_add_remove(struct diff_options *options,
333 int addremove, unsigned mode,
334 const unsigned char *sha1,
e3d42c47 335 const char *fullpath, unsigned dirty_submodule)
a4a88b2b 336{
ceff8e7a 337 int diff = addremove == '+' ? REV_TREE_NEW : REV_TREE_OLD;
a4a88b2b 338
ceff8e7a 339 tree_difference |= diff;
dd47aa31 340 if (tree_difference == REV_TREE_DIFFERENT)
8f67f8ae 341 DIFF_OPT_SET(options, HAS_CHANGES);
a4a88b2b
LT
342}
343
344static void file_change(struct diff_options *options,
345 unsigned old_mode, unsigned new_mode,
346 const unsigned char *old_sha1,
347 const unsigned char *new_sha1,
e3d42c47
JL
348 const char *fullpath,
349 unsigned old_dirty_submodule, unsigned new_dirty_submodule)
a4a88b2b 350{
8efdc326 351 tree_difference = REV_TREE_DIFFERENT;
8f67f8ae 352 DIFF_OPT_SET(options, HAS_CHANGES);
a4a88b2b
LT
353}
354
3a5e8608 355static int rev_compare_tree(struct rev_info *revs, struct commit *parent, struct commit *commit)
a4a88b2b 356{
3a5e8608
LT
357 struct tree *t1 = parent->tree;
358 struct tree *t2 = commit->tree;
359
a4a88b2b 360 if (!t1)
8efdc326 361 return REV_TREE_NEW;
ceff8e7a
LT
362 if (!t2)
363 return REV_TREE_OLD;
78892e32
LT
364
365 if (revs->simplify_by_decoration) {
366 /*
367 * If we are simplifying by decoration, then the commit
368 * is worth showing if it has a tag pointing at it.
369 */
370 if (lookup_decoration(&name_decoration, &commit->object))
371 return REV_TREE_DIFFERENT;
372 /*
373 * A commit that is not pointed by a tag is uninteresting
374 * if we are not limited by path. This means that you will
375 * see the usual "commits that touch the paths" plus any
376 * tagged commit by specifying both --simplify-by-decoration
377 * and pathspec.
378 */
afe069d1 379 if (!revs->prune_data.nr)
78892e32
LT
380 return REV_TREE_SAME;
381 }
ceff8e7a 382
8efdc326 383 tree_difference = REV_TREE_SAME;
8f67f8ae 384 DIFF_OPT_CLR(&revs->pruning, HAS_CHANGES);
c4e05b1a 385 if (diff_tree_sha1(t1->object.sha1, t2->object.sha1, "",
cd2bdc53 386 &revs->pruning) < 0)
8efdc326 387 return REV_TREE_DIFFERENT;
a4a88b2b
LT
388 return tree_difference;
389}
390
3a5e8608 391static int rev_same_tree_as_empty(struct rev_info *revs, struct commit *commit)
a4a88b2b
LT
392{
393 int retval;
394 void *tree;
6fda5e51 395 unsigned long size;
a4a88b2b 396 struct tree_desc empty, real;
3a5e8608 397 struct tree *t1 = commit->tree;
a4a88b2b
LT
398
399 if (!t1)
400 return 0;
401
6fda5e51 402 tree = read_object_with_reference(t1->object.sha1, tree_type, &size, NULL);
a4a88b2b
LT
403 if (!tree)
404 return 0;
6fda5e51
LT
405 init_tree_desc(&real, tree, size);
406 init_tree_desc(&empty, "", 0);
a4a88b2b 407
0a4ba7f8 408 tree_difference = REV_TREE_SAME;
8f67f8ae 409 DIFF_OPT_CLR(&revs->pruning, HAS_CHANGES);
cd2bdc53 410 retval = diff_tree(&empty, &real, "", &revs->pruning);
a4a88b2b
LT
411 free(tree);
412
0a4ba7f8 413 return retval >= 0 && (tree_difference == REV_TREE_SAME);
a4a88b2b
LT
414}
415
416static void try_to_simplify_commit(struct rev_info *revs, struct commit *commit)
417{
418 struct commit_list **pp, *parent;
6631c736 419 int tree_changed = 0, tree_same = 0;
a4a88b2b 420
53b2c823
LT
421 /*
422 * If we don't do pruning, everything is interesting
423 */
7dc0fe3b 424 if (!revs->prune)
53b2c823 425 return;
53b2c823 426
a4a88b2b
LT
427 if (!commit->tree)
428 return;
429
430 if (!commit->parents) {
3a5e8608 431 if (rev_same_tree_as_empty(revs, commit))
7dc0fe3b 432 commit->object.flags |= TREESAME;
a4a88b2b
LT
433 return;
434 }
435
53b2c823
LT
436 /*
437 * Normal non-merge commit? If we don't want to make the
438 * history dense, we consider it always to be a change..
439 */
7dc0fe3b 440 if (!revs->dense && !commit->parents->next)
53b2c823 441 return;
53b2c823 442
a4a88b2b
LT
443 pp = &commit->parents;
444 while ((parent = *pp) != NULL) {
445 struct commit *p = parent->item;
446
cc0e6c5a
AR
447 if (parse_commit(p) < 0)
448 die("cannot simplify commit %s (because of %s)",
449 sha1_to_hex(commit->object.sha1),
450 sha1_to_hex(p->object.sha1));
3a5e8608 451 switch (rev_compare_tree(revs, p, commit)) {
8efdc326 452 case REV_TREE_SAME:
6631c736 453 tree_same = 1;
9202434c 454 if (!revs->simplify_history || (p->object.flags & UNINTERESTING)) {
f3219fbb
JH
455 /* Even if a merge with an uninteresting
456 * side branch brought the entire change
457 * we are interested in, we do not want
458 * to lose the other branches of this
459 * merge, so we just keep going.
460 */
461 pp = &parent->next;
462 continue;
463 }
a4a88b2b
LT
464 parent->next = NULL;
465 commit->parents = parent;
7dc0fe3b 466 commit->object.flags |= TREESAME;
a4a88b2b
LT
467 return;
468
8efdc326
FK
469 case REV_TREE_NEW:
470 if (revs->remove_empty_trees &&
3a5e8608 471 rev_same_tree_as_empty(revs, p)) {
c348f31a
JH
472 /* We are adding all the specified
473 * paths from this parent, so the
474 * history beyond this parent is not
475 * interesting. Remove its parents
476 * (they are grandparents for us).
477 * IOW, we pretend this parent is a
478 * "root" commit.
a41e109c 479 */
cc0e6c5a
AR
480 if (parse_commit(p) < 0)
481 die("cannot simplify commit %s (invalid %s)",
482 sha1_to_hex(commit->object.sha1),
483 sha1_to_hex(p->object.sha1));
c348f31a 484 p->parents = NULL;
a4a88b2b
LT
485 }
486 /* fallthrough */
ceff8e7a 487 case REV_TREE_OLD:
8efdc326 488 case REV_TREE_DIFFERENT:
f3219fbb 489 tree_changed = 1;
a4a88b2b
LT
490 pp = &parent->next;
491 continue;
492 }
493 die("bad tree compare for commit %s", sha1_to_hex(commit->object.sha1));
494 }
6631c736 495 if (tree_changed && !tree_same)
7dc0fe3b
LT
496 return;
497 commit->object.flags |= TREESAME;
a4a88b2b
LT
498}
499
47e44ed1 500static void commit_list_insert_by_date_cached(struct commit *p, struct commit_list **head,
fce87ae5
AG
501 struct commit_list *cached_base, struct commit_list **cache)
502{
503 struct commit_list *new_entry;
504
505 if (cached_base && p->date < cached_base->item->date)
47e44ed1 506 new_entry = commit_list_insert_by_date(p, &cached_base->next);
fce87ae5 507 else
47e44ed1 508 new_entry = commit_list_insert_by_date(p, head);
fce87ae5
AG
509
510 if (cache && (!*cache || p->date < (*cache)->item->date))
511 *cache = new_entry;
512}
513
514static int add_parents_to_list(struct rev_info *revs, struct commit *commit,
515 struct commit_list **list, struct commit_list **cache_ptr)
a4a88b2b
LT
516{
517 struct commit_list *parent = commit->parents;
577ed5c2 518 unsigned left_flag;
fce87ae5 519 struct commit_list *cached_base = cache_ptr ? *cache_ptr : NULL;
a4a88b2b 520
3381c790 521 if (commit->object.flags & ADDED)
cc0e6c5a 522 return 0;
3381c790
LT
523 commit->object.flags |= ADDED;
524
a4a88b2b
LT
525 /*
526 * If the commit is uninteresting, don't try to
527 * prune parents - we want the maximal uninteresting
528 * set.
529 *
530 * Normally we haven't parsed the parent
531 * yet, so we won't have a parent of a parent
532 * here. However, it may turn out that we've
533 * reached this commit some other way (where it
534 * wasn't uninteresting), in which case we need
535 * to mark its parents recursively too..
536 */
537 if (commit->object.flags & UNINTERESTING) {
538 while (parent) {
539 struct commit *p = parent->item;
540 parent = parent->next;
aeeae1b7
JH
541 if (p)
542 p->object.flags |= UNINTERESTING;
cc0e6c5a 543 if (parse_commit(p) < 0)
aeeae1b7 544 continue;
a4a88b2b
LT
545 if (p->parents)
546 mark_parents_uninteresting(p);
547 if (p->object.flags & SEEN)
548 continue;
549 p->object.flags |= SEEN;
47e44ed1 550 commit_list_insert_by_date_cached(p, list, cached_base, cache_ptr);
a4a88b2b 551 }
cc0e6c5a 552 return 0;
a4a88b2b
LT
553 }
554
555 /*
556 * Ok, the commit wasn't uninteresting. Try to
557 * simplify the commit history and find the parent
558 * that has no differences in the path set if one exists.
559 */
53b2c823 560 try_to_simplify_commit(revs, commit);
a4a88b2b 561
ba1d4505 562 if (revs->no_walk)
cc0e6c5a 563 return 0;
ba1d4505 564
577ed5c2 565 left_flag = (commit->object.flags & SYMMETRIC_LEFT);
0053e902 566
d9c292e8 567 for (parent = commit->parents; parent; parent = parent->next) {
a4a88b2b
LT
568 struct commit *p = parent->item;
569
cc0e6c5a
AR
570 if (parse_commit(p) < 0)
571 return -1;
0f3a290b
LT
572 if (revs->show_source && !p->util)
573 p->util = commit->util;
577ed5c2 574 p->object.flags |= left_flag;
ad1012eb
LH
575 if (!(p->object.flags & SEEN)) {
576 p->object.flags |= SEEN;
47e44ed1 577 commit_list_insert_by_date_cached(p, list, cached_base, cache_ptr);
ad1012eb 578 }
60d30b02 579 if (revs->first_parent_only)
d9c292e8 580 break;
a4a88b2b 581 }
cc0e6c5a 582 return 0;
a4a88b2b
LT
583}
584
36d56de6 585static void cherry_pick_list(struct commit_list *list, struct rev_info *revs)
d7a17cad
JH
586{
587 struct commit_list *p;
588 int left_count = 0, right_count = 0;
589 int left_first;
590 struct patch_ids ids;
adbbb31e 591 unsigned cherry_flag;
d7a17cad
JH
592
593 /* First count the commits on the left and on the right */
594 for (p = list; p; p = p->next) {
595 struct commit *commit = p->item;
596 unsigned flags = commit->object.flags;
597 if (flags & BOUNDARY)
598 ;
599 else if (flags & SYMMETRIC_LEFT)
600 left_count++;
601 else
602 right_count++;
603 }
604
36c07975
TR
605 if (!left_count || !right_count)
606 return;
607
d7a17cad
JH
608 left_first = left_count < right_count;
609 init_patch_ids(&ids);
66f13625 610 ids.diffopts.pathspec = revs->diffopt.pathspec;
d7a17cad
JH
611
612 /* Compute patch-ids for one side */
613 for (p = list; p; p = p->next) {
614 struct commit *commit = p->item;
615 unsigned flags = commit->object.flags;
616
617 if (flags & BOUNDARY)
618 continue;
619 /*
620 * If we have fewer left, left_first is set and we omit
621 * commits on the right branch in this loop. If we have
622 * fewer right, we skip the left ones.
623 */
624 if (left_first != !!(flags & SYMMETRIC_LEFT))
625 continue;
626 commit->util = add_commit_patch_id(commit, &ids);
627 }
628
adbbb31e
MG
629 /* either cherry_mark or cherry_pick are true */
630 cherry_flag = revs->cherry_mark ? PATCHSAME : SHOWN;
631
d7a17cad
JH
632 /* Check the other side */
633 for (p = list; p; p = p->next) {
634 struct commit *commit = p->item;
635 struct patch_id *id;
636 unsigned flags = commit->object.flags;
637
638 if (flags & BOUNDARY)
639 continue;
640 /*
641 * If we have fewer left, left_first is set and we omit
642 * commits on the left branch in this loop.
643 */
644 if (left_first == !!(flags & SYMMETRIC_LEFT))
645 continue;
646
647 /*
648 * Have we seen the same patch id?
649 */
650 id = has_commit_patch_id(commit, &ids);
651 if (!id)
652 continue;
653 id->seen = 1;
adbbb31e 654 commit->object.flags |= cherry_flag;
d7a17cad
JH
655 }
656
657 /* Now check the original side for seen ones */
658 for (p = list; p; p = p->next) {
659 struct commit *commit = p->item;
660 struct patch_id *ent;
661
662 ent = commit->util;
663 if (!ent)
664 continue;
665 if (ent->seen)
adbbb31e 666 commit->object.flags |= cherry_flag;
d7a17cad
JH
667 commit->util = NULL;
668 }
669
670 free_patch_ids(&ids);
671}
672
7d004199
LT
673/* How many extra uninteresting commits we want to see.. */
674#define SLOP 5
675
676static int still_interesting(struct commit_list *src, unsigned long date, int slop)
3131b713 677{
7d004199
LT
678 /*
679 * No source list at all? We're definitely done..
680 */
681 if (!src)
682 return 0;
683
684 /*
685 * Does the destination list contain entries with a date
686 * before the source list? Definitely _not_ done.
687 */
688 if (date < src->item->date)
689 return SLOP;
690
691 /*
692 * Does the source list still have interesting commits in
693 * it? Definitely not done..
694 */
695 if (!everybody_uninteresting(src))
696 return SLOP;
697
698 /* Ok, we're closing in.. */
699 return slop-1;
3131b713
LT
700}
701
ebdc94f3
JH
702/*
703 * "rev-list --ancestry-path A..B" computes commits that are ancestors
704 * of B but not ancestors of A but further limits the result to those
705 * that are descendants of A. This takes the list of bottom commits and
706 * the result of "A..B" without --ancestry-path, and limits the latter
707 * further to the ones that can reach one of the commits in "bottom".
708 */
709static void limit_to_ancestry(struct commit_list *bottom, struct commit_list *list)
710{
711 struct commit_list *p;
712 struct commit_list *rlist = NULL;
713 int made_progress;
714
715 /*
716 * Reverse the list so that it will be likely that we would
717 * process parents before children.
718 */
719 for (p = list; p; p = p->next)
720 commit_list_insert(p->item, &rlist);
721
722 for (p = bottom; p; p = p->next)
723 p->item->object.flags |= TMP_MARK;
724
725 /*
726 * Mark the ones that can reach bottom commits in "list",
727 * in a bottom-up fashion.
728 */
729 do {
730 made_progress = 0;
731 for (p = rlist; p; p = p->next) {
732 struct commit *c = p->item;
733 struct commit_list *parents;
734 if (c->object.flags & (TMP_MARK | UNINTERESTING))
735 continue;
736 for (parents = c->parents;
737 parents;
738 parents = parents->next) {
739 if (!(parents->item->object.flags & TMP_MARK))
740 continue;
741 c->object.flags |= TMP_MARK;
742 made_progress = 1;
743 break;
744 }
745 }
746 } while (made_progress);
747
748 /*
749 * NEEDSWORK: decide if we want to remove parents that are
750 * not marked with TMP_MARK from commit->parents for commits
751 * in the resulting list. We may not want to do that, though.
752 */
753
754 /*
755 * The ones that are not marked with TMP_MARK are uninteresting
756 */
757 for (p = list; p; p = p->next) {
758 struct commit *c = p->item;
759 if (c->object.flags & TMP_MARK)
760 continue;
761 c->object.flags |= UNINTERESTING;
762 }
763
764 /* We are done with the TMP_MARK */
765 for (p = list; p; p = p->next)
766 p->item->object.flags &= ~TMP_MARK;
767 for (p = bottom; p; p = p->next)
768 p->item->object.flags &= ~TMP_MARK;
769 free_commit_list(rlist);
770}
771
772/*
773 * Before walking the history, keep the set of "negative" refs the
774 * caller has asked to exclude.
775 *
776 * This is used to compute "rev-list --ancestry-path A..B", as we need
777 * to filter the result of "A..B" further to the ones that can actually
778 * reach A.
779 */
c3502fa8 780static struct commit_list *collect_bottom_commits(struct rev_info *revs)
ebdc94f3 781{
c3502fa8
JH
782 struct commit_list *bottom = NULL;
783 int i;
784 for (i = 0; i < revs->cmdline.nr; i++) {
785 struct rev_cmdline_entry *elem = &revs->cmdline.rev[i];
786 if ((elem->flags & UNINTERESTING) &&
787 elem->item->type == OBJ_COMMIT)
788 commit_list_insert((struct commit *)elem->item, &bottom);
789 }
ebdc94f3
JH
790 return bottom;
791}
792
60adf7d7
MG
793/* Assumes either left_only or right_only is set */
794static void limit_left_right(struct commit_list *list, struct rev_info *revs)
795{
796 struct commit_list *p;
797
798 for (p = list; p; p = p->next) {
799 struct commit *commit = p->item;
800
801 if (revs->right_only) {
802 if (commit->object.flags & SYMMETRIC_LEFT)
803 commit->object.flags |= SHOWN;
804 } else /* revs->left_only is set */
805 if (!(commit->object.flags & SYMMETRIC_LEFT))
806 commit->object.flags |= SHOWN;
807 }
808}
809
cc0e6c5a 810static int limit_list(struct rev_info *revs)
a4a88b2b 811{
7d004199
LT
812 int slop = SLOP;
813 unsigned long date = ~0ul;
a4a88b2b
LT
814 struct commit_list *list = revs->commits;
815 struct commit_list *newlist = NULL;
816 struct commit_list **p = &newlist;
ebdc94f3
JH
817 struct commit_list *bottom = NULL;
818
819 if (revs->ancestry_path) {
c3502fa8 820 bottom = collect_bottom_commits(revs);
ebdc94f3 821 if (!bottom)
97b03c35 822 die("--ancestry-path given but there are no bottom commits");
ebdc94f3 823 }
a4a88b2b
LT
824
825 while (list) {
826 struct commit_list *entry = list;
827 struct commit *commit = list->item;
828 struct object *obj = &commit->object;
cdcefbc9 829 show_early_output_fn_t show;
a4a88b2b
LT
830
831 list = list->next;
832 free(entry);
833
834 if (revs->max_age != -1 && (commit->date < revs->max_age))
835 obj->flags |= UNINTERESTING;
fce87ae5 836 if (add_parents_to_list(revs, commit, &list, NULL) < 0)
cc0e6c5a 837 return -1;
a4a88b2b
LT
838 if (obj->flags & UNINTERESTING) {
839 mark_parents_uninteresting(commit);
7d004199
LT
840 if (revs->show_all)
841 p = &commit_list_insert(commit, p)->next;
842 slop = still_interesting(list, date, slop);
843 if (slop)
3131b713 844 continue;
7d004199
LT
845 /* If showing all, add the whole pending list to the end */
846 if (revs->show_all)
847 *p = list;
848 break;
a4a88b2b
LT
849 }
850 if (revs->min_age != -1 && (commit->date > revs->min_age))
851 continue;
7d004199 852 date = commit->date;
a4a88b2b 853 p = &commit_list_insert(commit, p)->next;
cdcefbc9
LT
854
855 show = show_early_output;
856 if (!show)
857 continue;
858
859 show(revs, newlist);
860 show_early_output = NULL;
a4a88b2b 861 }
adbbb31e 862 if (revs->cherry_pick || revs->cherry_mark)
36d56de6 863 cherry_pick_list(newlist, revs);
d7a17cad 864
60adf7d7
MG
865 if (revs->left_only || revs->right_only)
866 limit_left_right(newlist, revs);
867
ebdc94f3
JH
868 if (bottom) {
869 limit_to_ancestry(bottom, newlist);
870 free_commit_list(bottom);
871 }
872
a4a88b2b 873 revs->commits = newlist;
cc0e6c5a 874 return 0;
a4a88b2b
LT
875}
876
281eee47
JH
877static void add_rev_cmdline(struct rev_info *revs,
878 struct object *item,
879 const char *name,
880 int whence,
881 unsigned flags)
882{
883 struct rev_cmdline_info *info = &revs->cmdline;
884 int nr = info->nr;
885
886 ALLOC_GROW(info->rev, nr + 1, info->alloc);
887 info->rev[nr].item = item;
888 info->rev[nr].name = name;
889 info->rev[nr].whence = whence;
890 info->rev[nr].flags = flags;
891 info->nr++;
892}
893
63049292
JH
894struct all_refs_cb {
895 int all_flags;
71b03b42 896 int warned_bad_reflog;
63049292
JH
897 struct rev_info *all_revs;
898 const char *name_for_errormsg;
899};
ae563542 900
8da19775 901static int handle_one_ref(const char *path, const unsigned char *sha1, int flag, void *cb_data)
ae563542 902{
63049292
JH
903 struct all_refs_cb *cb = cb_data;
904 struct object *object = get_reference(cb->all_revs, path, sha1,
905 cb->all_flags);
281eee47 906 add_rev_cmdline(cb->all_revs, object, path, REV_CMD_REF, cb->all_flags);
26c3177e 907 add_pending_sha1(cb->all_revs, path, sha1, cb->all_flags);
ae563542
LT
908 return 0;
909}
910
d08bae7e
IL
911static void init_all_refs_cb(struct all_refs_cb *cb, struct rev_info *revs,
912 unsigned flags)
913{
914 cb->all_revs = revs;
915 cb->all_flags = flags;
916}
917
9ef6aeb0
HV
918static void handle_refs(const char *submodule, struct rev_info *revs, unsigned flags,
919 int (*for_each)(const char *, each_ref_fn, void *))
ae563542 920{
63049292 921 struct all_refs_cb cb;
d08bae7e 922 init_all_refs_cb(&cb, revs, flags);
9ef6aeb0 923 for_each(submodule, handle_one_ref, &cb);
63049292
JH
924}
925
71b03b42 926static void handle_one_reflog_commit(unsigned char *sha1, void *cb_data)
63049292
JH
927{
928 struct all_refs_cb *cb = cb_data;
71b03b42
SP
929 if (!is_null_sha1(sha1)) {
930 struct object *o = parse_object(sha1);
931 if (o) {
932 o->flags |= cb->all_flags;
281eee47 933 /* ??? CMDLINEFLAGS ??? */
71b03b42
SP
934 add_pending_object(cb->all_revs, o, "");
935 }
936 else if (!cb->warned_bad_reflog) {
46efd2d9 937 warning("reflog of '%s' references pruned commits",
71b03b42
SP
938 cb->name_for_errormsg);
939 cb->warned_bad_reflog = 1;
940 }
63049292 941 }
71b03b42
SP
942}
943
883d60fa
JS
944static int handle_one_reflog_ent(unsigned char *osha1, unsigned char *nsha1,
945 const char *email, unsigned long timestamp, int tz,
946 const char *message, void *cb_data)
71b03b42
SP
947{
948 handle_one_reflog_commit(osha1, cb_data);
949 handle_one_reflog_commit(nsha1, cb_data);
63049292
JH
950 return 0;
951}
952
953static int handle_one_reflog(const char *path, const unsigned char *sha1, int flag, void *cb_data)
954{
955 struct all_refs_cb *cb = cb_data;
71b03b42 956 cb->warned_bad_reflog = 0;
63049292
JH
957 cb->name_for_errormsg = path;
958 for_each_reflog_ent(path, handle_one_reflog_ent, cb_data);
959 return 0;
960}
961
962static void handle_reflog(struct rev_info *revs, unsigned flags)
963{
964 struct all_refs_cb cb;
965 cb.all_revs = revs;
966 cb.all_flags = flags;
25b51e2c 967 for_each_reflog(handle_one_reflog, &cb);
ae563542
LT
968}
969
281eee47 970static int add_parents_only(struct rev_info *revs, const char *arg_, int flags)
ea4a19e1
JH
971{
972 unsigned char sha1[20];
973 struct object *it;
974 struct commit *commit;
975 struct commit_list *parents;
281eee47 976 const char *arg = arg_;
ea4a19e1
JH
977
978 if (*arg == '^') {
979 flags ^= UNINTERESTING;
980 arg++;
981 }
cd74e473 982 if (get_sha1_committish(arg, sha1))
ea4a19e1
JH
983 return 0;
984 while (1) {
985 it = get_reference(revs, arg, sha1, 0);
cc243c3c
JH
986 if (!it && revs->ignore_missing)
987 return 0;
1974632c 988 if (it->type != OBJ_TAG)
ea4a19e1 989 break;
9684afd9
MK
990 if (!((struct tag*)it)->tagged)
991 return 0;
e702496e 992 hashcpy(sha1, ((struct tag*)it)->tagged->sha1);
ea4a19e1 993 }
1974632c 994 if (it->type != OBJ_COMMIT)
ea4a19e1
JH
995 return 0;
996 commit = (struct commit *)it;
997 for (parents = commit->parents; parents; parents = parents->next) {
998 it = &parents->item->object;
999 it->flags |= flags;
281eee47 1000 add_rev_cmdline(revs, it, arg_, REV_CMD_PARENTS_ONLY, flags);
ea4a19e1
JH
1001 add_pending_object(revs, it, arg);
1002 }
1003 return 1;
1004}
1005
db6296a5 1006void init_revisions(struct rev_info *revs, const char *prefix)
8efdc326
FK
1007{
1008 memset(revs, 0, sizeof(*revs));
8e8f9987 1009
6b9c58f4 1010 revs->abbrev = DEFAULT_ABBREV;
8e8f9987 1011 revs->ignore_merges = 1;
9202434c 1012 revs->simplify_history = 1;
8f67f8ae 1013 DIFF_OPT_SET(&revs->pruning, RECURSIVE);
90b19941 1014 DIFF_OPT_SET(&revs->pruning, QUICK);
cd2bdc53
LT
1015 revs->pruning.add_remove = file_add_remove;
1016 revs->pruning.change = file_change;
8efdc326
FK
1017 revs->lifo = 1;
1018 revs->dense = 1;
db6296a5 1019 revs->prefix = prefix;
8efdc326
FK
1020 revs->max_age = -1;
1021 revs->min_age = -1;
d5db6c9e 1022 revs->skip_count = -1;
8efdc326 1023 revs->max_count = -1;
ad5aeede 1024 revs->max_parents = -1;
8efdc326 1025
cd2bdc53
LT
1026 revs->commit_format = CMIT_FMT_DEFAULT;
1027
0843acfd
JK
1028 revs->grep_filter.status_only = 1;
1029 revs->grep_filter.pattern_tail = &(revs->grep_filter.pattern_list);
80235ba7 1030 revs->grep_filter.header_tail = &(revs->grep_filter.header_list);
0843acfd
JK
1031 revs->grep_filter.regflags = REG_NEWLINE;
1032
cd2bdc53 1033 diff_setup(&revs->diffopt);
c0cb4a06 1034 if (prefix && !revs->diffopt.prefix) {
cd676a51
JH
1035 revs->diffopt.prefix = prefix;
1036 revs->diffopt.prefix_length = strlen(prefix);
1037 }
3a03cf6b
JK
1038
1039 revs->notes_opt.use_default_notes = -1;
8efdc326
FK
1040}
1041
0d2c9d67
RS
1042static void add_pending_commit_list(struct rev_info *revs,
1043 struct commit_list *commit_list,
1044 unsigned int flags)
1045{
1046 while (commit_list) {
1047 struct object *object = &commit_list->item->object;
1048 object->flags |= flags;
1049 add_pending_object(revs, object, sha1_to_hex(object->sha1));
1050 commit_list = commit_list->next;
1051 }
1052}
1053
ae3e5e1e
JH
1054static void prepare_show_merge(struct rev_info *revs)
1055{
1056 struct commit_list *bases;
1057 struct commit *head, *other;
1058 unsigned char sha1[20];
1059 const char **prune = NULL;
1060 int i, prune_num = 1; /* counting terminating NULL */
1061
baf18fc2 1062 if (get_sha1("HEAD", sha1))
ae3e5e1e 1063 die("--merge without HEAD?");
baf18fc2
NTND
1064 head = lookup_commit_or_die(sha1, "HEAD");
1065 if (get_sha1("MERGE_HEAD", sha1))
ae3e5e1e 1066 die("--merge without MERGE_HEAD?");
baf18fc2 1067 other = lookup_commit_or_die(sha1, "MERGE_HEAD");
ae3e5e1e
JH
1068 add_pending_object(revs, &head->object, "HEAD");
1069 add_pending_object(revs, &other->object, "MERGE_HEAD");
1070 bases = get_merge_bases(head, other, 1);
e82447b1
JH
1071 add_pending_commit_list(revs, bases, UNINTERESTING);
1072 free_commit_list(bases);
1073 head->object.flags |= SYMMETRIC_LEFT;
ae3e5e1e
JH
1074
1075 if (!active_nr)
1076 read_cache();
1077 for (i = 0; i < active_nr; i++) {
1078 struct cache_entry *ce = active_cache[i];
1079 if (!ce_stage(ce))
1080 continue;
eb9cb55b 1081 if (ce_path_match(ce, &revs->prune_data)) {
ae3e5e1e
JH
1082 prune_num++;
1083 prune = xrealloc(prune, sizeof(*prune) * prune_num);
1084 prune[prune_num-2] = ce->name;
1085 prune[prune_num-1] = NULL;
1086 }
1087 while ((i+1 < active_nr) &&
1088 ce_same_name(ce, active_cache[i+1]))
1089 i++;
1090 }
afe069d1
NTND
1091 free_pathspec(&revs->prune_data);
1092 init_pathspec(&revs->prune_data, prune);
e82447b1 1093 revs->limited = 1;
ae3e5e1e
JH
1094}
1095
8e676e8b 1096int handle_revision_arg(const char *arg_, struct rev_info *revs, int flags, unsigned revarg_opt)
5d6f0935 1097{
249c8f4a 1098 struct object_context oc;
5d6f0935
JH
1099 char *dotdot;
1100 struct object *object;
1101 unsigned char sha1[20];
1102 int local_flags;
281eee47 1103 const char *arg = arg_;
8e676e8b 1104 int cant_be_filename = revarg_opt & REVARG_CANNOT_BE_FILENAME;
5d6f0935
JH
1105
1106 dotdot = strstr(arg, "..");
1107 if (dotdot) {
1108 unsigned char from_sha1[20];
1109 const char *next = dotdot + 2;
1110 const char *this = arg;
1111 int symmetric = *next == '.';
1112 unsigned int flags_exclude = flags ^ UNINTERESTING;
281eee47 1113 unsigned int a_flags;
5d6f0935
JH
1114
1115 *dotdot = 0;
1116 next += symmetric;
1117
1118 if (!*next)
1119 next = "HEAD";
1120 if (dotdot == arg)
1121 this = "HEAD";
cd74e473
JH
1122 if (!get_sha1_committish(this, from_sha1) &&
1123 !get_sha1_committish(next, sha1)) {
5d6f0935
JH
1124 struct commit *a, *b;
1125 struct commit_list *exclude;
1126
1127 a = lookup_commit_reference(from_sha1);
1128 b = lookup_commit_reference(sha1);
1129 if (!a || !b) {
cc243c3c
JH
1130 if (revs->ignore_missing)
1131 return 0;
5d6f0935
JH
1132 die(symmetric ?
1133 "Invalid symmetric difference expression %s...%s" :
1134 "Invalid revision range %s..%s",
1135 arg, next);
1136 }
1137
1138 if (!cant_be_filename) {
1139 *dotdot = '.';
1140 verify_non_filename(revs->prefix, arg);
1141 }
1142
1143 if (symmetric) {
1144 exclude = get_merge_bases(a, b, 1);
1145 add_pending_commit_list(revs, exclude,
1146 flags_exclude);
1147 free_commit_list(exclude);
281eee47 1148 a_flags = flags | SYMMETRIC_LEFT;
5d6f0935 1149 } else
281eee47
JH
1150 a_flags = flags_exclude;
1151 a->object.flags |= a_flags;
5d6f0935 1152 b->object.flags |= flags;
281eee47
JH
1153 add_rev_cmdline(revs, &a->object, this,
1154 REV_CMD_LEFT, a_flags);
1155 add_rev_cmdline(revs, &b->object, next,
1156 REV_CMD_RIGHT, flags);
5d6f0935
JH
1157 add_pending_object(revs, &a->object, this);
1158 add_pending_object(revs, &b->object, next);
1159 return 0;
1160 }
1161 *dotdot = '.';
1162 }
1163 dotdot = strstr(arg, "^@");
1164 if (dotdot && !dotdot[2]) {
1165 *dotdot = 0;
1166 if (add_parents_only(revs, arg, flags))
1167 return 0;
1168 *dotdot = '^';
1169 }
62476c8e
JH
1170 dotdot = strstr(arg, "^!");
1171 if (dotdot && !dotdot[2]) {
1172 *dotdot = 0;
1173 if (!add_parents_only(revs, arg, flags ^ UNINTERESTING))
1174 *dotdot = '^';
1175 }
1176
5d6f0935
JH
1177 local_flags = 0;
1178 if (*arg == '^') {
1179 local_flags = UNINTERESTING;
1180 arg++;
1181 }
33bd598c 1182 if (get_sha1_with_context(arg, 0, sha1, &oc))
cc243c3c 1183 return revs->ignore_missing ? 0 : -1;
5d6f0935
JH
1184 if (!cant_be_filename)
1185 verify_non_filename(revs->prefix, arg);
1186 object = get_reference(revs, arg, sha1, flags ^ local_flags);
281eee47 1187 add_rev_cmdline(revs, object, arg_, REV_CMD_REV, flags ^ local_flags);
249c8f4a 1188 add_pending_object_with_mode(revs, object, arg, oc.mode);
5d6f0935
JH
1189 return 0;
1190}
1191
4da5af31
JH
1192struct cmdline_pathspec {
1193 int alloc;
1194 int nr;
1195 const char **path;
1196};
1fc561d1 1197
4da5af31
JH
1198static void append_prune_data(struct cmdline_pathspec *prune, const char **av)
1199{
1200 while (*av) {
1201 ALLOC_GROW(prune->path, prune->nr+1, prune->alloc);
1202 prune->path[prune->nr++] = *(av++);
1203 }
1204}
60da8b15 1205
4da5af31
JH
1206static void read_pathspec_from_stdin(struct rev_info *revs, struct strbuf *sb,
1207 struct cmdline_pathspec *prune)
1208{
60da8b15
JH
1209 while (strbuf_getwholeline(sb, stdin, '\n') != EOF) {
1210 int len = sb->len;
1211 if (len && sb->buf[len - 1] == '\n')
1212 sb->buf[--len] = '\0';
4da5af31
JH
1213 ALLOC_GROW(prune->path, prune->nr+1, prune->alloc);
1214 prune->path[prune->nr++] = xstrdup(sb->buf);
60da8b15 1215 }
60da8b15
JH
1216}
1217
4da5af31
JH
1218static void read_revisions_from_stdin(struct rev_info *revs,
1219 struct cmdline_pathspec *prune)
1fc561d1 1220{
63d564b3 1221 struct strbuf sb;
60da8b15 1222 int seen_dashdash = 0;
1fc561d1 1223
63d564b3
JH
1224 strbuf_init(&sb, 1000);
1225 while (strbuf_getwholeline(&sb, stdin, '\n') != EOF) {
1226 int len = sb.len;
1227 if (len && sb.buf[len - 1] == '\n')
1228 sb.buf[--len] = '\0';
1fc561d1
AB
1229 if (!len)
1230 break;
60da8b15
JH
1231 if (sb.buf[0] == '-') {
1232 if (len == 2 && sb.buf[1] == '-') {
1233 seen_dashdash = 1;
1234 break;
1235 }
1fc561d1 1236 die("options not supported in --stdin mode");
60da8b15 1237 }
8e676e8b 1238 if (handle_revision_arg(sb.buf, revs, 0, REVARG_CANNOT_BE_FILENAME))
63d564b3 1239 die("bad revision '%s'", sb.buf);
1fc561d1 1240 }
60da8b15
JH
1241 if (seen_dashdash)
1242 read_pathspec_from_stdin(revs, &sb, prune);
63d564b3 1243 strbuf_release(&sb);
1fc561d1
AB
1244}
1245
2d10c555 1246static void add_grep(struct rev_info *revs, const char *ptn, enum grep_pat_token what)
bd95fcd3 1247{
0843acfd 1248 append_grep_pattern(&revs->grep_filter, ptn, "command line", 0, what);
2d10c555
JH
1249}
1250
a4d7d2c6 1251static void add_header_grep(struct rev_info *revs, enum grep_header_field field, const char *pattern)
2d10c555 1252{
a4d7d2c6 1253 append_header_grep_pattern(&revs->grep_filter, field, pattern);
bd95fcd3
JH
1254}
1255
1256static void add_message_grep(struct rev_info *revs, const char *pattern)
1257{
2d10c555 1258 add_grep(revs, pattern, GREP_PATTERN_BODY);
bd95fcd3
JH
1259}
1260
6b61ec05
PH
1261static int handle_revision_opt(struct rev_info *revs, int argc, const char **argv,
1262 int *unkc, const char **unkv)
02e54220
PH
1263{
1264 const char *arg = argv[0];
7d7b86f7
MM
1265 const char *optarg;
1266 int argcount;
02e54220
PH
1267
1268 /* pseudo revision arguments */
1269 if (!strcmp(arg, "--all") || !strcmp(arg, "--branches") ||
1270 !strcmp(arg, "--tags") || !strcmp(arg, "--remotes") ||
1271 !strcmp(arg, "--reflog") || !strcmp(arg, "--not") ||
ad3f9a71 1272 !strcmp(arg, "--no-walk") || !strcmp(arg, "--do-walk") ||
0fc63ec4
JN
1273 !strcmp(arg, "--bisect") || !prefixcmp(arg, "--glob=") ||
1274 !prefixcmp(arg, "--branches=") || !prefixcmp(arg, "--tags=") ||
1275 !prefixcmp(arg, "--remotes="))
02e54220
PH
1276 {
1277 unkv[(*unkc)++] = arg;
0fe8c138 1278 return 1;
02e54220
PH
1279 }
1280
7d7b86f7
MM
1281 if ((argcount = parse_long_opt("max-count", argv, &optarg))) {
1282 revs->max_count = atoi(optarg);
5853caec 1283 revs->no_walk = 0;
7d7b86f7
MM
1284 return argcount;
1285 } else if ((argcount = parse_long_opt("skip", argv, &optarg))) {
1286 revs->skip_count = atoi(optarg);
1287 return argcount;
02e54220
PH
1288 } else if ((*arg == '-') && isdigit(arg[1])) {
1289 /* accept -<digit>, like traditional "head" */
1290 revs->max_count = atoi(arg + 1);
5853caec 1291 revs->no_walk = 0;
02e54220
PH
1292 } else if (!strcmp(arg, "-n")) {
1293 if (argc <= 1)
1294 return error("-n requires an argument");
1295 revs->max_count = atoi(argv[1]);
5853caec 1296 revs->no_walk = 0;
02e54220
PH
1297 return 2;
1298 } else if (!prefixcmp(arg, "-n")) {
1299 revs->max_count = atoi(arg + 2);
5853caec 1300 revs->no_walk = 0;
7d7b86f7
MM
1301 } else if ((argcount = parse_long_opt("max-age", argv, &optarg))) {
1302 revs->max_age = atoi(optarg);
1303 return argcount;
1304 } else if ((argcount = parse_long_opt("since", argv, &optarg))) {
1305 revs->max_age = approxidate(optarg);
1306 return argcount;
1307 } else if ((argcount = parse_long_opt("after", argv, &optarg))) {
1308 revs->max_age = approxidate(optarg);
1309 return argcount;
1310 } else if ((argcount = parse_long_opt("min-age", argv, &optarg))) {
1311 revs->min_age = atoi(optarg);
1312 return argcount;
1313 } else if ((argcount = parse_long_opt("before", argv, &optarg))) {
1314 revs->min_age = approxidate(optarg);
1315 return argcount;
1316 } else if ((argcount = parse_long_opt("until", argv, &optarg))) {
1317 revs->min_age = approxidate(optarg);
1318 return argcount;
02e54220
PH
1319 } else if (!strcmp(arg, "--first-parent")) {
1320 revs->first_parent_only = 1;
ebdc94f3
JH
1321 } else if (!strcmp(arg, "--ancestry-path")) {
1322 revs->ancestry_path = 1;
cb7529e1 1323 revs->simplify_history = 0;
ebdc94f3 1324 revs->limited = 1;
02e54220
PH
1325 } else if (!strcmp(arg, "-g") || !strcmp(arg, "--walk-reflogs")) {
1326 init_reflog_walk(&revs->reflog_info);
1327 } else if (!strcmp(arg, "--default")) {
1328 if (argc <= 1)
1329 return error("bad --default argument");
1330 revs->def = argv[1];
1331 return 2;
1332 } else if (!strcmp(arg, "--merge")) {
1333 revs->show_merge = 1;
1334 } else if (!strcmp(arg, "--topo-order")) {
1335 revs->lifo = 1;
1336 revs->topo_order = 1;
6546b593
JH
1337 } else if (!strcmp(arg, "--simplify-merges")) {
1338 revs->simplify_merges = 1;
1339 revs->rewrite_parents = 1;
1340 revs->simplify_history = 0;
1341 revs->limited = 1;
78892e32
LT
1342 } else if (!strcmp(arg, "--simplify-by-decoration")) {
1343 revs->simplify_merges = 1;
1344 revs->rewrite_parents = 1;
1345 revs->simplify_history = 0;
1346 revs->simplify_by_decoration = 1;
1347 revs->limited = 1;
1348 revs->prune = 1;
33e7018c 1349 load_ref_decorations(DECORATE_SHORT_REFS);
02e54220
PH
1350 } else if (!strcmp(arg, "--date-order")) {
1351 revs->lifo = 0;
1352 revs->topo_order = 1;
1353 } else if (!prefixcmp(arg, "--early-output")) {
1354 int count = 100;
1355 switch (arg[14]) {
1356 case '=':
1357 count = atoi(arg+15);
1358 /* Fallthrough */
1359 case 0:
1360 revs->topo_order = 1;
1361 revs->early_output = count;
1362 }
1363 } else if (!strcmp(arg, "--parents")) {
1364 revs->rewrite_parents = 1;
1365 revs->print_parents = 1;
1366 } else if (!strcmp(arg, "--dense")) {
1367 revs->dense = 1;
1368 } else if (!strcmp(arg, "--sparse")) {
1369 revs->dense = 0;
1370 } else if (!strcmp(arg, "--show-all")) {
1371 revs->show_all = 1;
1372 } else if (!strcmp(arg, "--remove-empty")) {
1373 revs->remove_empty_trees = 1;
b8e8db28 1374 } else if (!strcmp(arg, "--merges")) {
ad5aeede 1375 revs->min_parents = 2;
02e54220 1376 } else if (!strcmp(arg, "--no-merges")) {
ad5aeede
MG
1377 revs->max_parents = 1;
1378 } else if (!prefixcmp(arg, "--min-parents=")) {
1379 revs->min_parents = atoi(arg+14);
1380 } else if (!prefixcmp(arg, "--no-min-parents")) {
1381 revs->min_parents = 0;
1382 } else if (!prefixcmp(arg, "--max-parents=")) {
1383 revs->max_parents = atoi(arg+14);
1384 } else if (!prefixcmp(arg, "--no-max-parents")) {
1385 revs->max_parents = -1;
02e54220
PH
1386 } else if (!strcmp(arg, "--boundary")) {
1387 revs->boundary = 1;
1388 } else if (!strcmp(arg, "--left-right")) {
1389 revs->left_right = 1;
60adf7d7 1390 } else if (!strcmp(arg, "--left-only")) {
24852d91 1391 if (revs->right_only)
94f605ec
MG
1392 die("--left-only is incompatible with --right-only"
1393 " or --cherry");
60adf7d7
MG
1394 revs->left_only = 1;
1395 } else if (!strcmp(arg, "--right-only")) {
24852d91
JH
1396 if (revs->left_only)
1397 die("--right-only is incompatible with --left-only");
60adf7d7 1398 revs->right_only = 1;
94f605ec
MG
1399 } else if (!strcmp(arg, "--cherry")) {
1400 if (revs->left_only)
1401 die("--cherry is incompatible with --left-only");
1402 revs->cherry_mark = 1;
1403 revs->right_only = 1;
ad5aeede 1404 revs->max_parents = 1;
94f605ec 1405 revs->limited = 1;
f69c5018
TR
1406 } else if (!strcmp(arg, "--count")) {
1407 revs->count = 1;
adbbb31e
MG
1408 } else if (!strcmp(arg, "--cherry-mark")) {
1409 if (revs->cherry_pick)
1410 die("--cherry-mark is incompatible with --cherry-pick");
1411 revs->cherry_mark = 1;
1412 revs->limited = 1; /* needs limit_list() */
02e54220 1413 } else if (!strcmp(arg, "--cherry-pick")) {
adbbb31e
MG
1414 if (revs->cherry_mark)
1415 die("--cherry-pick is incompatible with --cherry-mark");
02e54220
PH
1416 revs->cherry_pick = 1;
1417 revs->limited = 1;
1418 } else if (!strcmp(arg, "--objects")) {
1419 revs->tag_objects = 1;
1420 revs->tree_objects = 1;
1421 revs->blob_objects = 1;
1422 } else if (!strcmp(arg, "--objects-edge")) {
1423 revs->tag_objects = 1;
1424 revs->tree_objects = 1;
1425 revs->blob_objects = 1;
1426 revs->edge_hint = 1;
5a48d240
JH
1427 } else if (!strcmp(arg, "--verify-objects")) {
1428 revs->tag_objects = 1;
1429 revs->tree_objects = 1;
1430 revs->blob_objects = 1;
1431 revs->verify_objects = 1;
02e54220
PH
1432 } else if (!strcmp(arg, "--unpacked")) {
1433 revs->unpacked = 1;
03a9683d
JH
1434 } else if (!prefixcmp(arg, "--unpacked=")) {
1435 die("--unpacked=<packfile> no longer supported.");
02e54220
PH
1436 } else if (!strcmp(arg, "-r")) {
1437 revs->diff = 1;
1438 DIFF_OPT_SET(&revs->diffopt, RECURSIVE);
1439 } else if (!strcmp(arg, "-t")) {
1440 revs->diff = 1;
1441 DIFF_OPT_SET(&revs->diffopt, RECURSIVE);
1442 DIFF_OPT_SET(&revs->diffopt, TREE_IN_RECURSIVE);
1443 } else if (!strcmp(arg, "-m")) {
1444 revs->ignore_merges = 0;
1445 } else if (!strcmp(arg, "-c")) {
1446 revs->diff = 1;
1447 revs->dense_combined_merges = 0;
1448 revs->combine_merges = 1;
1449 } else if (!strcmp(arg, "--cc")) {
1450 revs->diff = 1;
1451 revs->dense_combined_merges = 1;
1452 revs->combine_merges = 1;
1453 } else if (!strcmp(arg, "-v")) {
1454 revs->verbose_header = 1;
1455 } else if (!strcmp(arg, "--pretty")) {
1456 revs->verbose_header = 1;
66b2ed09 1457 revs->pretty_given = 1;
02e54220 1458 get_commit_format(arg+8, revs);
3a4c1a5e 1459 } else if (!prefixcmp(arg, "--pretty=") || !prefixcmp(arg, "--format=")) {
7d7b86f7
MM
1460 /*
1461 * Detached form ("--pretty X" as opposed to "--pretty=X")
1462 * not allowed, since the argument is optional.
1463 */
02e54220 1464 revs->verbose_header = 1;
66b2ed09 1465 revs->pretty_given = 1;
02e54220 1466 get_commit_format(arg+9, revs);
7249e912 1467 } else if (!strcmp(arg, "--show-notes") || !strcmp(arg, "--notes")) {
66b2ed09
JH
1468 revs->show_notes = 1;
1469 revs->show_notes_given = 1;
3a03cf6b 1470 revs->notes_opt.use_default_notes = 1;
0c37f1fc
JH
1471 } else if (!strcmp(arg, "--show-signature")) {
1472 revs->show_signature = 1;
7249e912
JK
1473 } else if (!prefixcmp(arg, "--show-notes=") ||
1474 !prefixcmp(arg, "--notes=")) {
894a9d33
TR
1475 struct strbuf buf = STRBUF_INIT;
1476 revs->show_notes = 1;
1477 revs->show_notes_given = 1;
7249e912
JK
1478 if (!prefixcmp(arg, "--show-notes")) {
1479 if (revs->notes_opt.use_default_notes < 0)
1480 revs->notes_opt.use_default_notes = 1;
1481 strbuf_addstr(&buf, arg+13);
1482 }
1483 else
1484 strbuf_addstr(&buf, arg+8);
c063f0a9 1485 expand_notes_ref(&buf);
304cc11c 1486 string_list_append(&revs->notes_opt.extra_notes_refs,
1d2f80fa 1487 strbuf_detach(&buf, NULL));
66b2ed09
JH
1488 } else if (!strcmp(arg, "--no-notes")) {
1489 revs->show_notes = 0;
1490 revs->show_notes_given = 1;
92e0d425
JK
1491 revs->notes_opt.use_default_notes = -1;
1492 /* we have been strdup'ing ourselves, so trick
1493 * string_list into free()ing strings */
1494 revs->notes_opt.extra_notes_refs.strdup_strings = 1;
1495 string_list_clear(&revs->notes_opt.extra_notes_refs, 0);
1496 revs->notes_opt.extra_notes_refs.strdup_strings = 0;
894a9d33
TR
1497 } else if (!strcmp(arg, "--standard-notes")) {
1498 revs->show_notes_given = 1;
3a03cf6b 1499 revs->notes_opt.use_default_notes = 1;
894a9d33 1500 } else if (!strcmp(arg, "--no-standard-notes")) {
3a03cf6b 1501 revs->notes_opt.use_default_notes = 0;
de84accc
NS
1502 } else if (!strcmp(arg, "--oneline")) {
1503 revs->verbose_header = 1;
1504 get_commit_format("oneline", revs);
7dccadf3 1505 revs->pretty_given = 1;
de84accc 1506 revs->abbrev_commit = 1;
02e54220
PH
1507 } else if (!strcmp(arg, "--graph")) {
1508 revs->topo_order = 1;
1509 revs->rewrite_parents = 1;
1510 revs->graph = graph_init(revs);
1511 } else if (!strcmp(arg, "--root")) {
1512 revs->show_root_diff = 1;
1513 } else if (!strcmp(arg, "--no-commit-id")) {
1514 revs->no_commit_id = 1;
1515 } else if (!strcmp(arg, "--always")) {
1516 revs->always_show_header = 1;
1517 } else if (!strcmp(arg, "--no-abbrev")) {
1518 revs->abbrev = 0;
1519 } else if (!strcmp(arg, "--abbrev")) {
1520 revs->abbrev = DEFAULT_ABBREV;
1521 } else if (!prefixcmp(arg, "--abbrev=")) {
1522 revs->abbrev = strtoul(arg + 9, NULL, 10);
1523 if (revs->abbrev < MINIMUM_ABBREV)
1524 revs->abbrev = MINIMUM_ABBREV;
1525 else if (revs->abbrev > 40)
1526 revs->abbrev = 40;
1527 } else if (!strcmp(arg, "--abbrev-commit")) {
1528 revs->abbrev_commit = 1;
0c47695a
JS
1529 revs->abbrev_commit_given = 1;
1530 } else if (!strcmp(arg, "--no-abbrev-commit")) {
1531 revs->abbrev_commit = 0;
02e54220
PH
1532 } else if (!strcmp(arg, "--full-diff")) {
1533 revs->diff = 1;
1534 revs->full_diff = 1;
1535 } else if (!strcmp(arg, "--full-history")) {
1536 revs->simplify_history = 0;
1537 } else if (!strcmp(arg, "--relative-date")) {
1538 revs->date_mode = DATE_RELATIVE;
f4ea32f0 1539 revs->date_mode_explicit = 1;
7d7b86f7
MM
1540 } else if ((argcount = parse_long_opt("date", argv, &optarg))) {
1541 revs->date_mode = parse_date_format(optarg);
f4ea32f0 1542 revs->date_mode_explicit = 1;
7d7b86f7 1543 return argcount;
02e54220
PH
1544 } else if (!strcmp(arg, "--log-size")) {
1545 revs->show_log_size = 1;
1546 }
1547 /*
1548 * Grepping the commit log
1549 */
7d7b86f7
MM
1550 else if ((argcount = parse_long_opt("author", argv, &optarg))) {
1551 add_header_grep(revs, GREP_HEADER_AUTHOR, optarg);
1552 return argcount;
1553 } else if ((argcount = parse_long_opt("committer", argv, &optarg))) {
1554 add_header_grep(revs, GREP_HEADER_COMMITTER, optarg);
1555 return argcount;
1556 } else if ((argcount = parse_long_opt("grep", argv, &optarg))) {
1557 add_message_grep(revs, optarg);
1558 return argcount;
02e54220 1559 } else if (!strcmp(arg, "--extended-regexp") || !strcmp(arg, "-E")) {
0843acfd 1560 revs->grep_filter.regflags |= REG_EXTENDED;
02e54220 1561 } else if (!strcmp(arg, "--regexp-ignore-case") || !strcmp(arg, "-i")) {
0843acfd 1562 revs->grep_filter.regflags |= REG_ICASE;
02e54220 1563 } else if (!strcmp(arg, "--fixed-strings") || !strcmp(arg, "-F")) {
0843acfd 1564 revs->grep_filter.fixed = 1;
02e54220 1565 } else if (!strcmp(arg, "--all-match")) {
0843acfd 1566 revs->grep_filter.all_match = 1;
7d7b86f7
MM
1567 } else if ((argcount = parse_long_opt("encoding", argv, &optarg))) {
1568 if (strcmp(optarg, "none"))
1569 git_log_output_encoding = xstrdup(optarg);
02e54220
PH
1570 else
1571 git_log_output_encoding = "";
7d7b86f7 1572 return argcount;
02e54220
PH
1573 } else if (!strcmp(arg, "--reverse")) {
1574 revs->reverse ^= 1;
1575 } else if (!strcmp(arg, "--children")) {
1576 revs->children.name = "children";
1577 revs->limited = 1;
cc243c3c
JH
1578 } else if (!strcmp(arg, "--ignore-missing")) {
1579 revs->ignore_missing = 1;
02e54220
PH
1580 } else {
1581 int opts = diff_opt_parse(&revs->diffopt, argv, argc);
1582 if (!opts)
1583 unkv[(*unkc)++] = arg;
1584 return opts;
1585 }
1586
1587 return 1;
1588}
1589
6b61ec05
PH
1590void parse_revision_opt(struct rev_info *revs, struct parse_opt_ctx_t *ctx,
1591 const struct option *options,
1592 const char * const usagestr[])
1593{
1594 int n = handle_revision_opt(revs, ctx->argc, ctx->argv,
1595 &ctx->cpidx, ctx->out);
1596 if (n <= 0) {
1597 error("unknown option `%s'", ctx->argv[0]);
1598 usage_with_options(usagestr, options);
1599 }
1600 ctx->argv += n;
1601 ctx->argc -= n;
1602}
1603
9ef6aeb0 1604static int for_each_bad_bisect_ref(const char *submodule, each_ref_fn fn, void *cb_data)
ad3f9a71 1605{
9ef6aeb0 1606 return for_each_ref_in_submodule(submodule, "refs/bisect/bad", fn, cb_data);
ad3f9a71
LT
1607}
1608
9ef6aeb0 1609static int for_each_good_bisect_ref(const char *submodule, each_ref_fn fn, void *cb_data)
ad3f9a71 1610{
9ef6aeb0 1611 return for_each_ref_in_submodule(submodule, "refs/bisect/good", fn, cb_data);
ad3f9a71
LT
1612}
1613
f6aca0dc
JN
1614static int handle_revision_pseudo_opt(const char *submodule,
1615 struct rev_info *revs,
1616 int argc, const char **argv, int *flags)
1617{
1618 const char *arg = argv[0];
1619 const char *optarg;
1620 int argcount;
1621
0fc63ec4
JN
1622 /*
1623 * NOTE!
1624 *
1625 * Commands like "git shortlog" will not accept the options below
1626 * unless parse_revision_opt queues them (as opposed to erroring
1627 * out).
1628 *
1629 * When implementing your new pseudo-option, remember to
1630 * register it in the list at the top of handle_revision_opt.
1631 */
f6aca0dc
JN
1632 if (!strcmp(arg, "--all")) {
1633 handle_refs(submodule, revs, *flags, for_each_ref_submodule);
1634 handle_refs(submodule, revs, *flags, head_ref_submodule);
1635 } else if (!strcmp(arg, "--branches")) {
1636 handle_refs(submodule, revs, *flags, for_each_branch_ref_submodule);
1637 } else if (!strcmp(arg, "--bisect")) {
1638 handle_refs(submodule, revs, *flags, for_each_bad_bisect_ref);
1639 handle_refs(submodule, revs, *flags ^ UNINTERESTING, for_each_good_bisect_ref);
1640 revs->bisect = 1;
1641 } else if (!strcmp(arg, "--tags")) {
1642 handle_refs(submodule, revs, *flags, for_each_tag_ref_submodule);
1643 } else if (!strcmp(arg, "--remotes")) {
1644 handle_refs(submodule, revs, *flags, for_each_remote_ref_submodule);
1645 } else if ((argcount = parse_long_opt("glob", argv, &optarg))) {
1646 struct all_refs_cb cb;
1647 init_all_refs_cb(&cb, revs, *flags);
1648 for_each_glob_ref(handle_one_ref, optarg, &cb);
1649 return argcount;
1650 } else if (!prefixcmp(arg, "--branches=")) {
1651 struct all_refs_cb cb;
1652 init_all_refs_cb(&cb, revs, *flags);
1653 for_each_glob_ref_in(handle_one_ref, arg + 11, "refs/heads/", &cb);
1654 } else if (!prefixcmp(arg, "--tags=")) {
1655 struct all_refs_cb cb;
1656 init_all_refs_cb(&cb, revs, *flags);
1657 for_each_glob_ref_in(handle_one_ref, arg + 7, "refs/tags/", &cb);
1658 } else if (!prefixcmp(arg, "--remotes=")) {
1659 struct all_refs_cb cb;
1660 init_all_refs_cb(&cb, revs, *flags);
1661 for_each_glob_ref_in(handle_one_ref, arg + 10, "refs/remotes/", &cb);
1662 } else if (!strcmp(arg, "--reflog")) {
1663 handle_reflog(revs, *flags);
1664 } else if (!strcmp(arg, "--not")) {
1665 *flags ^= UNINTERESTING;
1666 } else if (!strcmp(arg, "--no-walk")) {
1667 revs->no_walk = 1;
1668 } else if (!strcmp(arg, "--do-walk")) {
1669 revs->no_walk = 0;
1670 } else {
1671 return 0;
1672 }
1673
1674 return 1;
1675}
1676
ae563542
LT
1677/*
1678 * Parse revision information, filling in the "rev_info" structure,
1679 * and removing the used arguments from the argument list.
1680 *
765ac8ec
LT
1681 * Returns the number of arguments left that weren't recognized
1682 * (which are also moved to the head of the argument list)
ae563542 1683 */
32962c9b 1684int setup_revisions(int argc, const char **argv, struct rev_info *revs, struct setup_revision_opt *opt)
ae563542 1685{
8e676e8b 1686 int i, flags, left, seen_dashdash, read_from_stdin, got_rev_arg = 0, revarg_opt;
4da5af31 1687 struct cmdline_pathspec prune_data;
9ef6aeb0
HV
1688 const char *submodule = NULL;
1689
4da5af31 1690 memset(&prune_data, 0, sizeof(prune_data));
9ef6aeb0
HV
1691 if (opt)
1692 submodule = opt->submodule;
ae563542 1693
ae563542
LT
1694 /* First, search for "--" */
1695 seen_dashdash = 0;
1696 for (i = 1; i < argc; i++) {
1697 const char *arg = argv[i];
1698 if (strcmp(arg, "--"))
1699 continue;
1700 argv[i] = NULL;
1701 argc = i;
a65f2005 1702 if (argv[i + 1])
4da5af31 1703 append_prune_data(&prune_data, argv + i + 1);
ae563542
LT
1704 seen_dashdash = 1;
1705 break;
1706 }
1707
02e54220
PH
1708 /* Second, deal with arguments and options */
1709 flags = 0;
8e676e8b 1710 revarg_opt = seen_dashdash ? REVARG_CANNOT_BE_FILENAME : 0;
8b3dce56 1711 read_from_stdin = 0;
02e54220 1712 for (left = i = 1; i < argc; i++) {
ae563542 1713 const char *arg = argv[i];
ae563542 1714 if (*arg == '-') {
cd2bdc53 1715 int opts;
02e54220 1716
f6aca0dc
JN
1717 opts = handle_revision_pseudo_opt(submodule,
1718 revs, argc - i, argv + i,
1719 &flags);
1720 if (opts > 0) {
1721 i += opts - 1;
8e64006e
JS
1722 continue;
1723 }
f6aca0dc 1724
8b3dce56
JH
1725 if (!strcmp(arg, "--stdin")) {
1726 if (revs->disable_stdin) {
1727 argv[left++] = arg;
1728 continue;
1729 }
1730 if (read_from_stdin++)
1731 die("--stdin given twice?");
60da8b15 1732 read_revisions_from_stdin(revs, &prune_data);
8b3dce56
JH
1733 continue;
1734 }
2d10c555 1735
02e54220 1736 opts = handle_revision_opt(revs, argc - i, argv + i, &left, argv);
cd2bdc53 1737 if (opts > 0) {
cd2bdc53
LT
1738 i += opts - 1;
1739 continue;
1740 }
02e54220
PH
1741 if (opts < 0)
1742 exit(128);
ae563542
LT
1743 continue;
1744 }
ae563542 1745
8e676e8b
JH
1746
1747 if (handle_revision_arg(arg, revs, flags, revarg_opt)) {
5d6f0935
JH
1748 int j;
1749 if (seen_dashdash || *arg == '^')
ae563542
LT
1750 die("bad revision '%s'", arg);
1751
ea92f41f
JH
1752 /* If we didn't have a "--":
1753 * (1) all filenames must exist;
1754 * (2) all rev-args must not be interpretable
1755 * as a valid filename.
1756 * but the latter we have checked in the main loop.
1757 */
e23d0b4a
LT
1758 for (j = i; j < argc; j++)
1759 verify_filename(revs->prefix, argv[j]);
1760
60da8b15 1761 append_prune_data(&prune_data, argv + i);
ae563542
LT
1762 break;
1763 }
8fcaca3f
DO
1764 else
1765 got_rev_arg = 1;
ae563542 1766 }
5d6f0935 1767
4da5af31 1768 if (prune_data.nr) {
93e7d672
JH
1769 /*
1770 * If we need to introduce the magic "a lone ':' means no
1771 * pathspec whatsoever", here is the place to do so.
1772 *
1773 * if (prune_data.nr == 1 && !strcmp(prune_data[0], ":")) {
1774 * prune_data.nr = 0;
1775 * prune_data.alloc = 0;
1776 * free(prune_data.path);
1777 * prune_data.path = NULL;
1778 * } else {
1779 * terminate prune_data.alloc with NULL and
1780 * call init_pathspec() to set revs->prune_data here.
1781 * }
1782 */
4da5af31
JH
1783 ALLOC_GROW(prune_data.path, prune_data.nr+1, prune_data.alloc);
1784 prune_data.path[prune_data.nr++] = NULL;
8f2d4b19
JH
1785 init_pathspec(&revs->prune_data,
1786 get_pathspec(revs->prefix, prune_data.path));
4da5af31 1787 }
5486ef0e 1788
02e54220 1789 if (revs->def == NULL)
32962c9b 1790 revs->def = opt ? opt->def : NULL;
b4490059
JH
1791 if (opt && opt->tweak)
1792 opt->tweak(revs, opt);
02e54220 1793 if (revs->show_merge)
ae3e5e1e 1794 prepare_show_merge(revs);
8fcaca3f 1795 if (revs->def && !revs->pending.nr && !got_rev_arg) {
ae563542 1796 unsigned char sha1[20];
cd2bdc53 1797 struct object *object;
249c8f4a 1798 struct object_context oc;
33bd598c 1799 if (get_sha1_with_context(revs->def, 0, sha1, &oc))
02e54220
PH
1800 die("bad default revision '%s'", revs->def);
1801 object = get_reference(revs, revs->def, sha1, 0);
249c8f4a 1802 add_pending_object_with_mode(revs, object, revs->def, oc.mode);
ae563542 1803 }
8efdc326 1804
b7bb760d
LT
1805 /* Did the user ask for any diff output? Run the diff! */
1806 if (revs->diffopt.output_format & ~DIFF_FORMAT_NO_OUTPUT)
1807 revs->diff = 1;
1808
0faf2da7
AL
1809 /* Pickaxe, diff-filter and rename following need diffs */
1810 if (revs->diffopt.pickaxe ||
1811 revs->diffopt.filter ||
1812 DIFF_OPT_TST(&revs->diffopt, FOLLOW_RENAMES))
b7bb760d
LT
1813 revs->diff = 1;
1814
9dad9d2e 1815 if (revs->topo_order)
53069686
JH
1816 revs->limited = 1;
1817
afe069d1
NTND
1818 if (revs->prune_data.nr) {
1819 diff_tree_setup_paths(revs->prune_data.raw, &revs->pruning);
750f7b66 1820 /* Can't prune commits with rename following: the paths change.. */
8f67f8ae 1821 if (!DIFF_OPT_TST(&revs->diffopt, FOLLOW_RENAMES))
53b2c823 1822 revs->prune = 1;
cd2bdc53 1823 if (!revs->full_diff)
afe069d1 1824 diff_tree_setup_paths(revs->prune_data.raw, &revs->diffopt);
8efdc326 1825 }
b4490059 1826 if (revs->combine_merges)
cd2bdc53 1827 revs->ignore_merges = 0;
cd2bdc53 1828 revs->diffopt.abbrev = revs->abbrev;
72ee96c0
JH
1829 if (diff_setup_done(&revs->diffopt) < 0)
1830 die("diff_setup_done failed");
8efdc326 1831
0843acfd 1832 compile_grep_patterns(&revs->grep_filter);
bd95fcd3 1833
d56651c0
SP
1834 if (revs->reverse && revs->reflog_info)
1835 die("cannot combine --reverse with --walk-reflogs");
8bb65883 1836 if (revs->rewrite_parents && revs->children.name)
f35f5603 1837 die("cannot combine --parents and --children");
d56651c0 1838
7fefda5c
AS
1839 /*
1840 * Limitations on the graph functionality
1841 */
1842 if (revs->reverse && revs->graph)
1843 die("cannot combine --reverse with --graph");
1844
1845 if (revs->reflog_info && revs->graph)
1846 die("cannot combine --walk-reflogs with --graph");
1847
ae563542
LT
1848 return left;
1849}
a4a88b2b 1850
f35f5603
JH
1851static void add_child(struct rev_info *revs, struct commit *parent, struct commit *child)
1852{
1853 struct commit_list *l = xcalloc(1, sizeof(*l));
1854
1855 l->item = child;
1856 l->next = add_decoration(&revs->children, &parent->object, l);
1857}
1858
6546b593
JH
1859static int remove_duplicate_parents(struct commit *commit)
1860{
1861 struct commit_list **pp, *p;
1862 int surviving_parents;
1863
1864 /* Examine existing parents while marking ones we have seen... */
1865 pp = &commit->parents;
1866 while ((p = *pp) != NULL) {
1867 struct commit *parent = p->item;
1868 if (parent->object.flags & TMP_MARK) {
1869 *pp = p->next;
1870 continue;
1871 }
1872 parent->object.flags |= TMP_MARK;
1873 pp = &p->next;
1874 }
1875 /* count them while clearing the temporary mark */
1876 surviving_parents = 0;
1877 for (p = commit->parents; p; p = p->next) {
1878 p->item->object.flags &= ~TMP_MARK;
1879 surviving_parents++;
1880 }
1881 return surviving_parents;
1882}
1883
faf0156b
JH
1884struct merge_simplify_state {
1885 struct commit *simplified;
1886};
1887
1888static struct merge_simplify_state *locate_simplify_state(struct rev_info *revs, struct commit *commit)
1889{
1890 struct merge_simplify_state *st;
1891
1892 st = lookup_decoration(&revs->merge_simplification, &commit->object);
1893 if (!st) {
1894 st = xcalloc(1, sizeof(*st));
1895 add_decoration(&revs->merge_simplification, &commit->object, st);
1896 }
1897 return st;
1898}
1899
1900static struct commit_list **simplify_one(struct rev_info *revs, struct commit *commit, struct commit_list **tail)
6546b593
JH
1901{
1902 struct commit_list *p;
faf0156b 1903 struct merge_simplify_state *st, *pst;
6546b593
JH
1904 int cnt;
1905
faf0156b
JH
1906 st = locate_simplify_state(revs, commit);
1907
6546b593 1908 /*
6546b593
JH
1909 * Have we handled this one?
1910 */
faf0156b 1911 if (st->simplified)
6546b593
JH
1912 return tail;
1913
1914 /*
1915 * An UNINTERESTING commit simplifies to itself, so does a
1916 * root commit. We do not rewrite parents of such commit
1917 * anyway.
1918 */
1919 if ((commit->object.flags & UNINTERESTING) || !commit->parents) {
faf0156b 1920 st->simplified = commit;
6546b593
JH
1921 return tail;
1922 }
1923
1924 /*
1925 * Do we know what commit all of our parents should be rewritten to?
1926 * Otherwise we are not ready to rewrite this one yet.
1927 */
1928 for (cnt = 0, p = commit->parents; p; p = p->next) {
faf0156b
JH
1929 pst = locate_simplify_state(revs, p->item);
1930 if (!pst->simplified) {
6546b593
JH
1931 tail = &commit_list_insert(p->item, tail)->next;
1932 cnt++;
1933 }
1934 }
53030f8d
JH
1935 if (cnt) {
1936 tail = &commit_list_insert(commit, tail)->next;
6546b593 1937 return tail;
53030f8d 1938 }
6546b593
JH
1939
1940 /*
1941 * Rewrite our list of parents.
1942 */
faf0156b
JH
1943 for (p = commit->parents; p; p = p->next) {
1944 pst = locate_simplify_state(revs, p->item);
1945 p->item = pst->simplified;
1946 }
6546b593
JH
1947 cnt = remove_duplicate_parents(commit);
1948
1949 /*
1950 * It is possible that we are a merge and one side branch
1951 * does not have any commit that touches the given paths;
1952 * in such a case, the immediate parents will be rewritten
1953 * to different commits.
1954 *
1955 * o----X X: the commit we are looking at;
1956 * / / o: a commit that touches the paths;
1957 * ---o----'
1958 *
1959 * Further reduce the parents by removing redundant parents.
1960 */
1961 if (1 < cnt) {
1962 struct commit_list *h = reduce_heads(commit->parents);
1963 cnt = commit_list_count(h);
1964 free_commit_list(commit->parents);
1965 commit->parents = h;
1966 }
1967
1968 /*
1969 * A commit simplifies to itself if it is a root, if it is
1970 * UNINTERESTING, if it touches the given paths, or if it is a
1971 * merge and its parents simplifies to more than one commits
1972 * (the first two cases are already handled at the beginning of
1973 * this function).
1974 *
1975 * Otherwise, it simplifies to what its sole parent simplifies to.
1976 */
1977 if (!cnt ||
1978 (commit->object.flags & UNINTERESTING) ||
1979 !(commit->object.flags & TREESAME) ||
1980 (1 < cnt))
faf0156b
JH
1981 st->simplified = commit;
1982 else {
1983 pst = locate_simplify_state(revs, commit->parents->item);
1984 st->simplified = pst->simplified;
1985 }
6546b593
JH
1986 return tail;
1987}
1988
1989static void simplify_merges(struct rev_info *revs)
1990{
1991 struct commit_list *list;
1992 struct commit_list *yet_to_do, **tail;
1993
5eac739e
JH
1994 if (!revs->topo_order)
1995 sort_in_topological_order(&revs->commits, revs->lifo);
1996 if (!revs->prune)
1997 return;
65347030 1998
6546b593
JH
1999 /* feed the list reversed */
2000 yet_to_do = NULL;
2001 for (list = revs->commits; list; list = list->next)
2002 commit_list_insert(list->item, &yet_to_do);
2003 while (yet_to_do) {
2004 list = yet_to_do;
2005 yet_to_do = NULL;
2006 tail = &yet_to_do;
2007 while (list) {
2008 struct commit *commit = list->item;
2009 struct commit_list *next = list->next;
2010 free(list);
2011 list = next;
faf0156b 2012 tail = simplify_one(revs, commit, tail);
6546b593
JH
2013 }
2014 }
2015
2016 /* clean up the result, removing the simplified ones */
2017 list = revs->commits;
2018 revs->commits = NULL;
2019 tail = &revs->commits;
2020 while (list) {
2021 struct commit *commit = list->item;
2022 struct commit_list *next = list->next;
faf0156b 2023 struct merge_simplify_state *st;
6546b593
JH
2024 free(list);
2025 list = next;
faf0156b
JH
2026 st = locate_simplify_state(revs, commit);
2027 if (st->simplified == commit)
6546b593
JH
2028 tail = &commit_list_insert(commit, tail)->next;
2029 }
6546b593
JH
2030}
2031
f35f5603
JH
2032static void set_children(struct rev_info *revs)
2033{
2034 struct commit_list *l;
2035 for (l = revs->commits; l; l = l->next) {
2036 struct commit *commit = l->item;
2037 struct commit_list *p;
2038
2039 for (p = commit->parents; p; p = p->next)
2040 add_child(revs, p->item, commit);
2041 }
2042}
2043
cc0e6c5a 2044int prepare_revision_walk(struct rev_info *revs)
a4a88b2b 2045{
1f1e895f 2046 int nr = revs->pending.nr;
94d23673 2047 struct object_array_entry *e, *list;
cd2bdc53 2048
94d23673 2049 e = list = revs->pending.objects;
1f1e895f
LT
2050 revs->pending.nr = 0;
2051 revs->pending.alloc = 0;
2052 revs->pending.objects = NULL;
2053 while (--nr >= 0) {
94d23673 2054 struct commit *commit = handle_commit(revs, e->item, e->name);
cd2bdc53
LT
2055 if (commit) {
2056 if (!(commit->object.flags & SEEN)) {
2057 commit->object.flags |= SEEN;
47e44ed1 2058 commit_list_insert_by_date(commit, &revs->commits);
cd2bdc53
LT
2059 }
2060 }
94d23673 2061 e++;
cd2bdc53 2062 }
4a43d374
RS
2063 if (!revs->leak_pending)
2064 free(list);
cd2bdc53 2065
ba1d4505 2066 if (revs->no_walk)
cc0e6c5a 2067 return 0;
a4a88b2b 2068 if (revs->limited)
cc0e6c5a
AR
2069 if (limit_list(revs) < 0)
2070 return -1;
a4a88b2b 2071 if (revs->topo_order)
23c17d4a 2072 sort_in_topological_order(&revs->commits, revs->lifo);
6546b593
JH
2073 if (revs->simplify_merges)
2074 simplify_merges(revs);
f35f5603
JH
2075 if (revs->children.name)
2076 set_children(revs);
cc0e6c5a 2077 return 0;
a4a88b2b
LT
2078}
2079
cc0e6c5a
AR
2080enum rewrite_result {
2081 rewrite_one_ok,
2082 rewrite_one_noparents,
4b05548f 2083 rewrite_one_error
cc0e6c5a
AR
2084};
2085
2086static enum rewrite_result rewrite_one(struct rev_info *revs, struct commit **pp)
765ac8ec 2087{
fce87ae5
AG
2088 struct commit_list *cache = NULL;
2089
765ac8ec
LT
2090 for (;;) {
2091 struct commit *p = *pp;
3381c790 2092 if (!revs->limited)
fce87ae5 2093 if (add_parents_to_list(revs, p, &revs->commits, &cache) < 0)
cc0e6c5a 2094 return rewrite_one_error;
6631c736 2095 if (p->parents && p->parents->next)
cc0e6c5a 2096 return rewrite_one_ok;
7dc0fe3b
LT
2097 if (p->object.flags & UNINTERESTING)
2098 return rewrite_one_ok;
2099 if (!(p->object.flags & TREESAME))
cc0e6c5a 2100 return rewrite_one_ok;
765ac8ec 2101 if (!p->parents)
cc0e6c5a 2102 return rewrite_one_noparents;
765ac8ec
LT
2103 *pp = p->parents->item;
2104 }
2105}
2106
cc0e6c5a 2107static int rewrite_parents(struct rev_info *revs, struct commit *commit)
765ac8ec
LT
2108{
2109 struct commit_list **pp = &commit->parents;
2110 while (*pp) {
2111 struct commit_list *parent = *pp;
cc0e6c5a
AR
2112 switch (rewrite_one(revs, &parent->item)) {
2113 case rewrite_one_ok:
2114 break;
2115 case rewrite_one_noparents:
765ac8ec
LT
2116 *pp = parent->next;
2117 continue;
cc0e6c5a
AR
2118 case rewrite_one_error:
2119 return -1;
765ac8ec
LT
2120 }
2121 pp = &parent->next;
2122 }
11d65967 2123 remove_duplicate_parents(commit);
cc0e6c5a 2124 return 0;
765ac8ec
LT
2125}
2126
8ecae9b0
JH
2127static int commit_match(struct commit *commit, struct rev_info *opt)
2128{
80235ba7 2129 if (!opt->grep_filter.pattern_list && !opt->grep_filter.header_list)
8ecae9b0 2130 return 1;
0843acfd 2131 return grep_buffer(&opt->grep_filter,
2d10c555
JH
2132 NULL, /* we say nothing, not even filename */
2133 commit->buffer, strlen(commit->buffer));
8ecae9b0
JH
2134}
2135
f35f5603
JH
2136static inline int want_ancestry(struct rev_info *revs)
2137{
8bb65883 2138 return (revs->rewrite_parents || revs->children.name);
f35f5603
JH
2139}
2140
beb5af43 2141enum commit_action get_commit_action(struct rev_info *revs, struct commit *commit)
252a7c02
LT
2142{
2143 if (commit->object.flags & SHOWN)
2144 return commit_ignore;
4d6acb70 2145 if (revs->unpacked && has_sha1_pack(commit->object.sha1))
252a7c02 2146 return commit_ignore;
3131b713
LT
2147 if (revs->show_all)
2148 return commit_show;
252a7c02
LT
2149 if (commit->object.flags & UNINTERESTING)
2150 return commit_ignore;
2151 if (revs->min_age != -1 && (commit->date > revs->min_age))
2152 return commit_ignore;
ad5aeede
MG
2153 if (revs->min_parents || (revs->max_parents >= 0)) {
2154 int n = 0;
2155 struct commit_list *p;
2156 for (p = commit->parents; p; p = p->next)
2157 n++;
2158 if ((n < revs->min_parents) ||
2159 ((revs->max_parents >= 0) && (n > revs->max_parents)))
2160 return commit_ignore;
2161 }
252a7c02
LT
2162 if (!commit_match(commit, revs))
2163 return commit_ignore;
53b2c823 2164 if (revs->prune && revs->dense) {
252a7c02 2165 /* Commit without changes? */
7dc0fe3b 2166 if (commit->object.flags & TREESAME) {
252a7c02 2167 /* drop merges unless we want parenthood */
f35f5603 2168 if (!want_ancestry(revs))
252a7c02
LT
2169 return commit_ignore;
2170 /* non-merge - always ignore it */
2171 if (!commit->parents || !commit->parents->next)
2172 return commit_ignore;
2173 }
252a7c02
LT
2174 }
2175 return commit_show;
2176}
2177
beb5af43
AS
2178enum commit_action simplify_commit(struct rev_info *revs, struct commit *commit)
2179{
2180 enum commit_action action = get_commit_action(revs, commit);
2181
2182 if (action == commit_show &&
2183 !revs->show_all &&
2184 revs->prune && revs->dense && want_ancestry(revs)) {
2185 if (rewrite_parents(revs, commit) < 0)
2186 return commit_error;
2187 }
2188 return action;
2189}
2190
d5db6c9e 2191static struct commit *get_revision_1(struct rev_info *revs)
a4a88b2b 2192{
d5db6c9e 2193 if (!revs->commits)
a4a88b2b 2194 return NULL;
a4a88b2b 2195
765ac8ec 2196 do {
cb115748
LT
2197 struct commit_list *entry = revs->commits;
2198 struct commit *commit = entry->item;
ea5ed3ab 2199
cb115748
LT
2200 revs->commits = entry->next;
2201 free(entry);
2a0925be 2202
ffa1eeae 2203 if (revs->reflog_info) {
8860fd42 2204 fake_reflog_parent(revs->reflog_info, commit);
ffa1eeae
JK
2205 commit->object.flags &= ~(ADDED | SEEN | SHOWN);
2206 }
8860fd42 2207
2a0925be
LT
2208 /*
2209 * If we haven't done the list limiting, we need to look at
be7db6e5
LT
2210 * the parents here. We also need to do the date-based limiting
2211 * that we'd otherwise have done in limit_list().
2a0925be 2212 */
be7db6e5 2213 if (!revs->limited) {
744f4985 2214 if (revs->max_age != -1 &&
86ab4906
JH
2215 (commit->date < revs->max_age))
2216 continue;
fce87ae5 2217 if (add_parents_to_list(revs, commit, &revs->commits, NULL) < 0)
ed62089c
JH
2218 die("Failed to traverse parents of commit %s",
2219 sha1_to_hex(commit->object.sha1));
be7db6e5 2220 }
744f4985 2221
252a7c02
LT
2222 switch (simplify_commit(revs, commit)) {
2223 case commit_ignore:
8ecae9b0 2224 continue;
252a7c02 2225 case commit_error:
ed62089c
JH
2226 die("Failed to simplify parents of commit %s",
2227 sha1_to_hex(commit->object.sha1));
252a7c02
LT
2228 default:
2229 return commit;
384e99a4 2230 }
765ac8ec
LT
2231 } while (revs->commits);
2232 return NULL;
2233}
d5db6c9e 2234
86ab4906
JH
2235static void gc_boundary(struct object_array *array)
2236{
2237 unsigned nr = array->nr;
2238 unsigned alloc = array->alloc;
2239 struct object_array_entry *objects = array->objects;
2240
2241 if (alloc <= nr) {
2242 unsigned i, j;
2243 for (i = j = 0; i < nr; i++) {
2244 if (objects[i].item->flags & SHOWN)
2245 continue;
2246 if (i != j)
2247 objects[j] = objects[i];
2248 j++;
2249 }
892ae6bf 2250 for (i = j; i < nr; i++)
86ab4906
JH
2251 objects[i].item = NULL;
2252 array->nr = j;
2253 }
2254}
2255
4603ec0f
AS
2256static void create_boundary_commit_list(struct rev_info *revs)
2257{
2258 unsigned i;
2259 struct commit *c;
2260 struct object_array *array = &revs->boundary_commits;
2261 struct object_array_entry *objects = array->objects;
2262
2263 /*
2264 * If revs->commits is non-NULL at this point, an error occurred in
2265 * get_revision_1(). Ignore the error and continue printing the
2266 * boundary commits anyway. (This is what the code has always
2267 * done.)
2268 */
2269 if (revs->commits) {
2270 free_commit_list(revs->commits);
2271 revs->commits = NULL;
2272 }
2273
2274 /*
2275 * Put all of the actual boundary commits from revs->boundary_commits
2276 * into revs->commits
2277 */
2278 for (i = 0; i < array->nr; i++) {
2279 c = (struct commit *)(objects[i].item);
2280 if (!c)
2281 continue;
2282 if (!(c->object.flags & CHILD_SHOWN))
2283 continue;
2284 if (c->object.flags & (SHOWN | BOUNDARY))
2285 continue;
2286 c->object.flags |= BOUNDARY;
2287 commit_list_insert(c, &revs->commits);
2288 }
2289
2290 /*
2291 * If revs->topo_order is set, sort the boundary commits
2292 * in topological order
2293 */
2294 sort_in_topological_order(&revs->commits, revs->lifo);
2295}
2296
7fefda5c 2297static struct commit *get_revision_internal(struct rev_info *revs)
d5db6c9e
JH
2298{
2299 struct commit *c = NULL;
86ab4906
JH
2300 struct commit_list *l;
2301
2302 if (revs->boundary == 2) {
4603ec0f
AS
2303 /*
2304 * All of the normal commits have already been returned,
2305 * and we are now returning boundary commits.
2306 * create_boundary_commit_list() has populated
2307 * revs->commits with the remaining commits to return.
2308 */
2309 c = pop_commit(&revs->commits);
2310 if (c)
2311 c->object.flags |= SHOWN;
9c5e66e9
JS
2312 return c;
2313 }
2314
86ab4906
JH
2315 /*
2316 * Now pick up what they want to give us
2317 */
8839ac94
JH
2318 c = get_revision_1(revs);
2319 if (c) {
2320 while (0 < revs->skip_count) {
2321 revs->skip_count--;
2322 c = get_revision_1(revs);
2323 if (!c)
2324 break;
2325 }
86ab4906
JH
2326 }
2327
2328 /*
2329 * Check the max_count.
2330 */
d5db6c9e
JH
2331 switch (revs->max_count) {
2332 case -1:
2333 break;
2334 case 0:
86ab4906
JH
2335 c = NULL;
2336 break;
d5db6c9e
JH
2337 default:
2338 revs->max_count--;
2339 }
86ab4906 2340
c33d8593
JH
2341 if (c)
2342 c->object.flags |= SHOWN;
2343
2344 if (!revs->boundary) {
d5db6c9e 2345 return c;
c33d8593 2346 }
86ab4906
JH
2347
2348 if (!c) {
2349 /*
2350 * get_revision_1() runs out the commits, and
2351 * we are done computing the boundaries.
2352 * switch to boundary commits output mode.
2353 */
2354 revs->boundary = 2;
4603ec0f
AS
2355
2356 /*
2357 * Update revs->commits to contain the list of
2358 * boundary commits.
2359 */
2360 create_boundary_commit_list(revs);
2361
3c68d67b 2362 return get_revision_internal(revs);
86ab4906
JH
2363 }
2364
2365 /*
2366 * boundary commits are the commits that are parents of the
2367 * ones we got from get_revision_1() but they themselves are
2368 * not returned from get_revision_1(). Before returning
2369 * 'c', we need to mark its parents that they could be boundaries.
2370 */
2371
2372 for (l = c->parents; l; l = l->next) {
2373 struct object *p;
2374 p = &(l->item->object);
c33d8593 2375 if (p->flags & (CHILD_SHOWN | SHOWN))
86ab4906
JH
2376 continue;
2377 p->flags |= CHILD_SHOWN;
2378 gc_boundary(&revs->boundary_commits);
2379 add_object_array(p, NULL, &revs->boundary_commits);
2380 }
2381
2382 return c;
d5db6c9e 2383}
7fefda5c
AS
2384
2385struct commit *get_revision(struct rev_info *revs)
2386{
498bcd31
TR
2387 struct commit *c;
2388 struct commit_list *reversed;
2389
2390 if (revs->reverse) {
2391 reversed = NULL;
2392 while ((c = get_revision_internal(revs))) {
2393 commit_list_insert(c, &reversed);
2394 }
2395 revs->commits = reversed;
2396 revs->reverse = 0;
2397 revs->reverse_output_stage = 1;
2398 }
2399
2400 if (revs->reverse_output_stage)
2401 return pop_commit(&revs->commits);
2402
2403 c = get_revision_internal(revs);
7fefda5c
AS
2404 if (c && revs->graph)
2405 graph_update(revs->graph, c);
2406 return c;
2407}
1df2d656
MG
2408
2409char *get_revision_mark(const struct rev_info *revs, const struct commit *commit)
2410{
2411 if (commit->object.flags & BOUNDARY)
2412 return "-";
2413 else if (commit->object.flags & UNINTERESTING)
2414 return "^";
adbbb31e
MG
2415 else if (commit->object.flags & PATCHSAME)
2416 return "=";
1df2d656
MG
2417 else if (!revs || revs->left_right) {
2418 if (commit->object.flags & SYMMETRIC_LEFT)
2419 return "<";
2420 else
2421 return ">";
2422 } else if (revs->graph)
2423 return "*";
adbbb31e
MG
2424 else if (revs->cherry_mark)
2425 return "+";
1df2d656
MG
2426 return "";
2427}
b1b47554
MG
2428
2429void put_revision_mark(const struct rev_info *revs, const struct commit *commit)
2430{
2431 char *mark = get_revision_mark(revs, commit);
2432 if (!strlen(mark))
2433 return;
2434 fputs(mark, stdout);
2435 putchar(' ');
2436}