1#include "cache.h"
2#include "object-store.h"
3#include "tag.h"
4#include "blob.h"
5#include "tree.h"
6#include "commit.h"
7#include "diff.h"
8#include "refs.h"
9#include "revision.h"
10#include "repository.h"
11#include "graph.h"
12#include "grep.h"
13#include "reflog-walk.h"
14#include "patch-ids.h"
15#include "decorate.h"
16#include "log-tree.h"
17#include "string-list.h"
18#include "line-log.h"
19#include "mailmap.h"
20#include "commit-slab.h"
21#include "dir.h"
22#include "cache-tree.h"
23#include "bisect.h"
24#include "packfile.h"
25#include "worktree.h"
26#include "argv-array.h"
27
28volatile show_early_output_fn_t show_early_output;
29
30static const char *term_bad;
31static const char *term_good;
32
33implement_shared_commit_slab(revision_sources, char *);
34
35void show_object_with_name(FILE *out, struct object *obj, const char *name)
36{
37 const char *p;
38
39 fprintf(out, "%s ", oid_to_hex(&obj->oid));
40 for (p = name; *p && *p != '\n'; p++)
41 fputc(*p, out);
42 fputc('\n', out);
43}
44
45static void mark_blob_uninteresting(struct blob *blob)
46{
47 if (!blob)
48 return;
49 if (blob->object.flags & UNINTERESTING)
50 return;
51 blob->object.flags |= UNINTERESTING;
52}
53
54static void mark_tree_contents_uninteresting(struct tree *tree)
55{
56 struct tree_desc desc;
57 struct name_entry entry;
58
59 if (parse_tree_gently(tree, 1) < 0)
60 return;
61
62 init_tree_desc(&desc, tree->buffer, tree->size);
63 while (tree_entry(&desc, &entry)) {
64 switch (object_type(entry.mode)) {
65 case OBJ_TREE:
66 mark_tree_uninteresting(lookup_tree(the_repository, entry.oid));
67 break;
68 case OBJ_BLOB:
69 mark_blob_uninteresting(lookup_blob(the_repository, entry.oid));
70 break;
71 default:
72 /* Subproject commit - not in this repository */
73 break;
74 }
75 }
76
77 /*
78 * We don't care about the tree any more
79 * after it has been marked uninteresting.
80 */
81 free_tree_buffer(tree);
82}
83
84void mark_tree_uninteresting(struct tree *tree)
85{
86 struct object *obj;
87
88 if (!tree)
89 return;
90
91 obj = &tree->object;
92 if (obj->flags & UNINTERESTING)
93 return;
94 obj->flags |= UNINTERESTING;
95 mark_tree_contents_uninteresting(tree);
96}
97
98struct commit_stack {
99 struct commit **items;
100 size_t nr, alloc;
101};
102#define COMMIT_STACK_INIT { NULL, 0, 0 }
103
104static void commit_stack_push(struct commit_stack *stack, struct commit *commit)
105{
106 ALLOC_GROW(stack->items, stack->nr + 1, stack->alloc);
107 stack->items[stack->nr++] = commit;
108}
109
110static struct commit *commit_stack_pop(struct commit_stack *stack)
111{
112 return stack->nr ? stack->items[--stack->nr] : NULL;
113}
114
115static void commit_stack_clear(struct commit_stack *stack)
116{
117 FREE_AND_NULL(stack->items);
118 stack->nr = stack->alloc = 0;
119}
120
121static void mark_one_parent_uninteresting(struct commit *commit,
122 struct commit_stack *pending)
123{
124 struct commit_list *l;
125
126 if (commit->object.flags & UNINTERESTING)
127 return;
128 commit->object.flags |= UNINTERESTING;
129
130 /*
131 * Normally we haven't parsed the parent
132 * yet, so we won't have a parent of a parent
133 * here. However, it may turn out that we've
134 * reached this commit some other way (where it
135 * wasn't uninteresting), in which case we need
136 * to mark its parents recursively too..
137 */
138 for (l = commit->parents; l; l = l->next)
139 commit_stack_push(pending, l->item);
140}
141
142void mark_parents_uninteresting(struct commit *commit)
143{
144 struct commit_stack pending = COMMIT_STACK_INIT;
145 struct commit_list *l;
146
147 for (l = commit->parents; l; l = l->next)
148 mark_one_parent_uninteresting(l->item, &pending);
149
150 while (pending.nr > 0)
151 mark_one_parent_uninteresting(commit_stack_pop(&pending),
152 &pending);
153
154 commit_stack_clear(&pending);
155}
156
157static void add_pending_object_with_path(struct rev_info *revs,
158 struct object *obj,
159 const char *name, unsigned mode,
160 const char *path)
161{
162 if (!obj)
163 return;
164 if (revs->no_walk && (obj->flags & UNINTERESTING))
165 revs->no_walk = 0;
166 if (revs->reflog_info && obj->type == OBJ_COMMIT) {
167 struct strbuf buf = STRBUF_INIT;
168 int len = interpret_branch_name(name, 0, &buf, 0);
169
170 if (0 < len && name[len] && buf.len)
171 strbuf_addstr(&buf, name + len);
172 add_reflog_for_walk(revs->reflog_info,
173 (struct commit *)obj,
174 buf.buf[0] ? buf.buf: name);
175 strbuf_release(&buf);
176 return; /* do not add the commit itself */
177 }
178 obj->flags |= USER_GIVEN;
179 add_object_array_with_path(obj, name, &revs->pending, mode, path);
180}
181
182static void add_pending_object_with_mode(struct rev_info *revs,
183 struct object *obj,
184 const char *name, unsigned mode)
185{
186 add_pending_object_with_path(revs, obj, name, mode, NULL);
187}
188
189void add_pending_object(struct rev_info *revs,
190 struct object *obj, const char *name)
191{
192 add_pending_object_with_mode(revs, obj, name, S_IFINVALID);
193}
194
195void add_head_to_pending(struct rev_info *revs)
196{
197 struct object_id oid;
198 struct object *obj;
199 if (get_oid("HEAD", &oid))
200 return;
201 obj = parse_object(the_repository, &oid);
202 if (!obj)
203 return;
204 add_pending_object(revs, obj, "HEAD");
205}
206
207static struct object *get_reference(struct rev_info *revs, const char *name,
208 const struct object_id *oid,
209 unsigned int flags)
210{
211 struct object *object;
212
213 object = parse_object(the_repository, oid);
214 if (!object) {
215 if (revs->ignore_missing)
216 return object;
217 if (revs->exclude_promisor_objects && is_promisor_object(oid))
218 return NULL;
219 die("bad object %s", name);
220 }
221 object->flags |= flags;
222 return object;
223}
224
225void add_pending_oid(struct rev_info *revs, const char *name,
226 const struct object_id *oid, unsigned int flags)
227{
228 struct object *object = get_reference(revs, name, oid, flags);
229 add_pending_object(revs, object, name);
230}
231
232static struct commit *handle_commit(struct rev_info *revs,
233 struct object_array_entry *entry)
234{
235 struct object *object = entry->item;
236 const char *name = entry->name;
237 const char *path = entry->path;
238 unsigned int mode = entry->mode;
239 unsigned long flags = object->flags;
240
241 /*
242 * Tag object? Look what it points to..
243 */
244 while (object->type == OBJ_TAG) {
245 struct tag *tag = (struct tag *) object;
246 if (revs->tag_objects && !(flags & UNINTERESTING))
247 add_pending_object(revs, object, tag->tag);
248 if (!tag->tagged)
249 die("bad tag");
250 object = parse_object(the_repository, &tag->tagged->oid);
251 if (!object) {
252 if (revs->ignore_missing_links || (flags & UNINTERESTING))
253 return NULL;
254 if (revs->exclude_promisor_objects &&
255 is_promisor_object(&tag->tagged->oid))
256 return NULL;
257 die("bad object %s", oid_to_hex(&tag->tagged->oid));
258 }
259 object->flags |= flags;
260 /*
261 * We'll handle the tagged object by looping or dropping
262 * through to the non-tag handlers below. Do not
263 * propagate path data from the tag's pending entry.
264 */
265 path = NULL;
266 mode = 0;
267 }
268
269 /*
270 * Commit object? Just return it, we'll do all the complex
271 * reachability crud.
272 */
273 if (object->type == OBJ_COMMIT) {
274 struct commit *commit = (struct commit *)object;
275
276 if (parse_commit(commit) < 0)
277 die("unable to parse commit %s", name);
278 if (flags & UNINTERESTING) {
279 mark_parents_uninteresting(commit);
280 revs->limited = 1;
281 }
282 if (revs->sources) {
283 char **slot = revision_sources_at(revs->sources, commit);
284
285 if (!*slot)
286 *slot = xstrdup(name);
287 }
288 return commit;
289 }
290
291 /*
292 * Tree object? Either mark it uninteresting, or add it
293 * to the list of objects to look at later..
294 */
295 if (object->type == OBJ_TREE) {
296 struct tree *tree = (struct tree *)object;
297 if (!revs->tree_objects)
298 return NULL;
299 if (flags & UNINTERESTING) {
300 mark_tree_contents_uninteresting(tree);
301 return NULL;
302 }
303 add_pending_object_with_path(revs, object, name, mode, path);
304 return NULL;
305 }
306
307 /*
308 * Blob object? You know the drill by now..
309 */
310 if (object->type == OBJ_BLOB) {
311 if (!revs->blob_objects)
312 return NULL;
313 if (flags & UNINTERESTING)
314 return NULL;
315 add_pending_object_with_path(revs, object, name, mode, path);
316 return NULL;
317 }
318 die("%s is unknown object", name);
319}
320
321static int everybody_uninteresting(struct commit_list *orig,
322 struct commit **interesting_cache)
323{
324 struct commit_list *list = orig;
325
326 if (*interesting_cache) {
327 struct commit *commit = *interesting_cache;
328 if (!(commit->object.flags & UNINTERESTING))
329 return 0;
330 }
331
332 while (list) {
333 struct commit *commit = list->item;
334 list = list->next;
335 if (commit->object.flags & UNINTERESTING)
336 continue;
337
338 *interesting_cache = commit;
339 return 0;
340 }
341 return 1;
342}
343
344/*
345 * A definition of "relevant" commit that we can use to simplify limited graphs
346 * by eliminating side branches.
347 *
348 * A "relevant" commit is one that is !UNINTERESTING (ie we are including it
349 * in our list), or that is a specified BOTTOM commit. Then after computing
350 * a limited list, during processing we can generally ignore boundary merges
351 * coming from outside the graph, (ie from irrelevant parents), and treat
352 * those merges as if they were single-parent. TREESAME is defined to consider
353 * only relevant parents, if any. If we are TREESAME to our on-graph parents,
354 * we don't care if we were !TREESAME to non-graph parents.
355 *
356 * Treating bottom commits as relevant ensures that a limited graph's
357 * connection to the actual bottom commit is not viewed as a side branch, but
358 * treated as part of the graph. For example:
359 *
360 * ....Z...A---X---o---o---B
361 * . /
362 * W---Y
363 *
364 * When computing "A..B", the A-X connection is at least as important as
365 * Y-X, despite A being flagged UNINTERESTING.
366 *
367 * And when computing --ancestry-path "A..B", the A-X connection is more
368 * important than Y-X, despite both A and Y being flagged UNINTERESTING.
369 */
370static inline int relevant_commit(struct commit *commit)
371{
372 return (commit->object.flags & (UNINTERESTING | BOTTOM)) != UNINTERESTING;
373}
374
375/*
376 * Return a single relevant commit from a parent list. If we are a TREESAME
377 * commit, and this selects one of our parents, then we can safely simplify to
378 * that parent.
379 */
380static struct commit *one_relevant_parent(const struct rev_info *revs,
381 struct commit_list *orig)
382{
383 struct commit_list *list = orig;
384 struct commit *relevant = NULL;
385
386 if (!orig)
387 return NULL;
388
389 /*
390 * For 1-parent commits, or if first-parent-only, then return that
391 * first parent (even if not "relevant" by the above definition).
392 * TREESAME will have been set purely on that parent.
393 */
394 if (revs->first_parent_only || !orig->next)
395 return orig->item;
396
397 /*
398 * For multi-parent commits, identify a sole relevant parent, if any.
399 * If we have only one relevant parent, then TREESAME will be set purely
400 * with regard to that parent, and we can simplify accordingly.
401 *
402 * If we have more than one relevant parent, or no relevant parents
403 * (and multiple irrelevant ones), then we can't select a parent here
404 * and return NULL.
405 */
406 while (list) {
407 struct commit *commit = list->item;
408 list = list->next;
409 if (relevant_commit(commit)) {
410 if (relevant)
411 return NULL;
412 relevant = commit;
413 }
414 }
415 return relevant;
416}
417
418/*
419 * The goal is to get REV_TREE_NEW as the result only if the
420 * diff consists of all '+' (and no other changes), REV_TREE_OLD
421 * if the whole diff is removal of old data, and otherwise
422 * REV_TREE_DIFFERENT (of course if the trees are the same we
423 * want REV_TREE_SAME).
424 *
425 * The only time we care about the distinction is when
426 * remove_empty_trees is in effect, in which case we care only about
427 * whether the whole change is REV_TREE_NEW, or if there's another type
428 * of change. Which means we can stop the diff early in either of these
429 * cases:
430 *
431 * 1. We're not using remove_empty_trees at all.
432 *
433 * 2. We saw anything except REV_TREE_NEW.
434 */
435static int tree_difference = REV_TREE_SAME;
436
437static void file_add_remove(struct diff_options *options,
438 int addremove, unsigned mode,
439 const struct object_id *oid,
440 int oid_valid,
441 const char *fullpath, unsigned dirty_submodule)
442{
443 int diff = addremove == '+' ? REV_TREE_NEW : REV_TREE_OLD;
444 struct rev_info *revs = options->change_fn_data;
445
446 tree_difference |= diff;
447 if (!revs->remove_empty_trees || tree_difference != REV_TREE_NEW)
448 options->flags.has_changes = 1;
449}
450
451static void file_change(struct diff_options *options,
452 unsigned old_mode, unsigned new_mode,
453 const struct object_id *old_oid,
454 const struct object_id *new_oid,
455 int old_oid_valid, int new_oid_valid,
456 const char *fullpath,
457 unsigned old_dirty_submodule, unsigned new_dirty_submodule)
458{
459 tree_difference = REV_TREE_DIFFERENT;
460 options->flags.has_changes = 1;
461}
462
463static int rev_compare_tree(struct rev_info *revs,
464 struct commit *parent, struct commit *commit)
465{
466 struct tree *t1 = get_commit_tree(parent);
467 struct tree *t2 = get_commit_tree(commit);
468
469 if (!t1)
470 return REV_TREE_NEW;
471 if (!t2)
472 return REV_TREE_OLD;
473
474 if (revs->simplify_by_decoration) {
475 /*
476 * If we are simplifying by decoration, then the commit
477 * is worth showing if it has a tag pointing at it.
478 */
479 if (get_name_decoration(&commit->object))
480 return REV_TREE_DIFFERENT;
481 /*
482 * A commit that is not pointed by a tag is uninteresting
483 * if we are not limited by path. This means that you will
484 * see the usual "commits that touch the paths" plus any
485 * tagged commit by specifying both --simplify-by-decoration
486 * and pathspec.
487 */
488 if (!revs->prune_data.nr)
489 return REV_TREE_SAME;
490 }
491
492 tree_difference = REV_TREE_SAME;
493 revs->pruning.flags.has_changes = 0;
494 if (diff_tree_oid(&t1->object.oid, &t2->object.oid, "",
495 &revs->pruning) < 0)
496 return REV_TREE_DIFFERENT;
497 return tree_difference;
498}
499
500static int rev_same_tree_as_empty(struct rev_info *revs, struct commit *commit)
501{
502 int retval;
503 struct tree *t1 = get_commit_tree(commit);
504
505 if (!t1)
506 return 0;
507
508 tree_difference = REV_TREE_SAME;
509 revs->pruning.flags.has_changes = 0;
510 retval = diff_tree_oid(NULL, &t1->object.oid, "", &revs->pruning);
511
512 return retval >= 0 && (tree_difference == REV_TREE_SAME);
513}
514
515struct treesame_state {
516 unsigned int nparents;
517 unsigned char treesame[FLEX_ARRAY];
518};
519
520static struct treesame_state *initialise_treesame(struct rev_info *revs, struct commit *commit)
521{
522 unsigned n = commit_list_count(commit->parents);
523 struct treesame_state *st = xcalloc(1, st_add(sizeof(*st), n));
524 st->nparents = n;
525 add_decoration(&revs->treesame, &commit->object, st);
526 return st;
527}
528
529/*
530 * Must be called immediately after removing the nth_parent from a commit's
531 * parent list, if we are maintaining the per-parent treesame[] decoration.
532 * This does not recalculate the master TREESAME flag - update_treesame()
533 * should be called to update it after a sequence of treesame[] modifications
534 * that may have affected it.
535 */
536static int compact_treesame(struct rev_info *revs, struct commit *commit, unsigned nth_parent)
537{
538 struct treesame_state *st;
539 int old_same;
540
541 if (!commit->parents) {
542 /*
543 * Have just removed the only parent from a non-merge.
544 * Different handling, as we lack decoration.
545 */
546 if (nth_parent != 0)
547 die("compact_treesame %u", nth_parent);
548 old_same = !!(commit->object.flags & TREESAME);
549 if (rev_same_tree_as_empty(revs, commit))
550 commit->object.flags |= TREESAME;
551 else
552 commit->object.flags &= ~TREESAME;
553 return old_same;
554 }
555
556 st = lookup_decoration(&revs->treesame, &commit->object);
557 if (!st || nth_parent >= st->nparents)
558 die("compact_treesame %u", nth_parent);
559
560 old_same = st->treesame[nth_parent];
561 memmove(st->treesame + nth_parent,
562 st->treesame + nth_parent + 1,
563 st->nparents - nth_parent - 1);
564
565 /*
566 * If we've just become a non-merge commit, update TREESAME
567 * immediately, and remove the no-longer-needed decoration.
568 * If still a merge, defer update until update_treesame().
569 */
570 if (--st->nparents == 1) {
571 if (commit->parents->next)
572 die("compact_treesame parents mismatch");
573 if (st->treesame[0] && revs->dense)
574 commit->object.flags |= TREESAME;
575 else
576 commit->object.flags &= ~TREESAME;
577 free(add_decoration(&revs->treesame, &commit->object, NULL));
578 }
579
580 return old_same;
581}
582
583static unsigned update_treesame(struct rev_info *revs, struct commit *commit)
584{
585 if (commit->parents && commit->parents->next) {
586 unsigned n;
587 struct treesame_state *st;
588 struct commit_list *p;
589 unsigned relevant_parents;
590 unsigned relevant_change, irrelevant_change;
591
592 st = lookup_decoration(&revs->treesame, &commit->object);
593 if (!st)
594 die("update_treesame %s", oid_to_hex(&commit->object.oid));
595 relevant_parents = 0;
596 relevant_change = irrelevant_change = 0;
597 for (p = commit->parents, n = 0; p; n++, p = p->next) {
598 if (relevant_commit(p->item)) {
599 relevant_change |= !st->treesame[n];
600 relevant_parents++;
601 } else
602 irrelevant_change |= !st->treesame[n];
603 }
604 if (relevant_parents ? relevant_change : irrelevant_change)
605 commit->object.flags &= ~TREESAME;
606 else
607 commit->object.flags |= TREESAME;
608 }
609
610 return commit->object.flags & TREESAME;
611}
612
613static inline int limiting_can_increase_treesame(const struct rev_info *revs)
614{
615 /*
616 * TREESAME is irrelevant unless prune && dense;
617 * if simplify_history is set, we can't have a mixture of TREESAME and
618 * !TREESAME INTERESTING parents (and we don't have treesame[]
619 * decoration anyway);
620 * if first_parent_only is set, then the TREESAME flag is locked
621 * against the first parent (and again we lack treesame[] decoration).
622 */
623 return revs->prune && revs->dense &&
624 !revs->simplify_history &&
625 !revs->first_parent_only;
626}
627
628static void try_to_simplify_commit(struct rev_info *revs, struct commit *commit)
629{
630 struct commit_list **pp, *parent;
631 struct treesame_state *ts = NULL;
632 int relevant_change = 0, irrelevant_change = 0;
633 int relevant_parents, nth_parent;
634
635 /*
636 * If we don't do pruning, everything is interesting
637 */
638 if (!revs->prune)
639 return;
640
641 if (!get_commit_tree(commit))
642 return;
643
644 if (!commit->parents) {
645 if (rev_same_tree_as_empty(revs, commit))
646 commit->object.flags |= TREESAME;
647 return;
648 }
649
650 /*
651 * Normal non-merge commit? If we don't want to make the
652 * history dense, we consider it always to be a change..
653 */
654 if (!revs->dense && !commit->parents->next)
655 return;
656
657 for (pp = &commit->parents, nth_parent = 0, relevant_parents = 0;
658 (parent = *pp) != NULL;
659 pp = &parent->next, nth_parent++) {
660 struct commit *p = parent->item;
661 if (relevant_commit(p))
662 relevant_parents++;
663
664 if (nth_parent == 1) {
665 /*
666 * This our second loop iteration - so we now know
667 * we're dealing with a merge.
668 *
669 * Do not compare with later parents when we care only about
670 * the first parent chain, in order to avoid derailing the
671 * traversal to follow a side branch that brought everything
672 * in the path we are limited to by the pathspec.
673 */
674 if (revs->first_parent_only)
675 break;
676 /*
677 * If this will remain a potentially-simplifiable
678 * merge, remember per-parent treesame if needed.
679 * Initialise the array with the comparison from our
680 * first iteration.
681 */
682 if (revs->treesame.name &&
683 !revs->simplify_history &&
684 !(commit->object.flags & UNINTERESTING)) {
685 ts = initialise_treesame(revs, commit);
686 if (!(irrelevant_change || relevant_change))
687 ts->treesame[0] = 1;
688 }
689 }
690 if (parse_commit(p) < 0)
691 die("cannot simplify commit %s (because of %s)",
692 oid_to_hex(&commit->object.oid),
693 oid_to_hex(&p->object.oid));
694 switch (rev_compare_tree(revs, p, commit)) {
695 case REV_TREE_SAME:
696 if (!revs->simplify_history || !relevant_commit(p)) {
697 /* Even if a merge with an uninteresting
698 * side branch brought the entire change
699 * we are interested in, we do not want
700 * to lose the other branches of this
701 * merge, so we just keep going.
702 */
703 if (ts)
704 ts->treesame[nth_parent] = 1;
705 continue;
706 }
707 parent->next = NULL;
708 commit->parents = parent;
709 commit->object.flags |= TREESAME;
710 return;
711
712 case REV_TREE_NEW:
713 if (revs->remove_empty_trees &&
714 rev_same_tree_as_empty(revs, p)) {
715 /* We are adding all the specified
716 * paths from this parent, so the
717 * history beyond this parent is not
718 * interesting. Remove its parents
719 * (they are grandparents for us).
720 * IOW, we pretend this parent is a
721 * "root" commit.
722 */
723 if (parse_commit(p) < 0)
724 die("cannot simplify commit %s (invalid %s)",
725 oid_to_hex(&commit->object.oid),
726 oid_to_hex(&p->object.oid));
727 p->parents = NULL;
728 }
729 /* fallthrough */
730 case REV_TREE_OLD:
731 case REV_TREE_DIFFERENT:
732 if (relevant_commit(p))
733 relevant_change = 1;
734 else
735 irrelevant_change = 1;
736 continue;
737 }
738 die("bad tree compare for commit %s", oid_to_hex(&commit->object.oid));
739 }
740
741 /*
742 * TREESAME is straightforward for single-parent commits. For merge
743 * commits, it is most useful to define it so that "irrelevant"
744 * parents cannot make us !TREESAME - if we have any relevant
745 * parents, then we only consider TREESAMEness with respect to them,
746 * allowing irrelevant merges from uninteresting branches to be
747 * simplified away. Only if we have only irrelevant parents do we
748 * base TREESAME on them. Note that this logic is replicated in
749 * update_treesame, which should be kept in sync.
750 */
751 if (relevant_parents ? !relevant_change : !irrelevant_change)
752 commit->object.flags |= TREESAME;
753}
754
755static void commit_list_insert_by_date_cached(struct commit *p, struct commit_list **head,
756 struct commit_list *cached_base, struct commit_list **cache)
757{
758 struct commit_list *new_entry;
759
760 if (cached_base && p->date < cached_base->item->date)
761 new_entry = commit_list_insert_by_date(p, &cached_base->next);
762 else
763 new_entry = commit_list_insert_by_date(p, head);
764
765 if (cache && (!*cache || p->date < (*cache)->item->date))
766 *cache = new_entry;
767}
768
769static int add_parents_to_list(struct rev_info *revs, struct commit *commit,
770 struct commit_list **list, struct commit_list **cache_ptr)
771{
772 struct commit_list *parent = commit->parents;
773 unsigned left_flag;
774 struct commit_list *cached_base = cache_ptr ? *cache_ptr : NULL;
775
776 if (commit->object.flags & ADDED)
777 return 0;
778 commit->object.flags |= ADDED;
779
780 if (revs->include_check &&
781 !revs->include_check(commit, revs->include_check_data))
782 return 0;
783
784 /*
785 * If the commit is uninteresting, don't try to
786 * prune parents - we want the maximal uninteresting
787 * set.
788 *
789 * Normally we haven't parsed the parent
790 * yet, so we won't have a parent of a parent
791 * here. However, it may turn out that we've
792 * reached this commit some other way (where it
793 * wasn't uninteresting), in which case we need
794 * to mark its parents recursively too..
795 */
796 if (commit->object.flags & UNINTERESTING) {
797 while (parent) {
798 struct commit *p = parent->item;
799 parent = parent->next;
800 if (p)
801 p->object.flags |= UNINTERESTING;
802 if (parse_commit_gently(p, 1) < 0)
803 continue;
804 if (p->parents)
805 mark_parents_uninteresting(p);
806 if (p->object.flags & SEEN)
807 continue;
808 p->object.flags |= SEEN;
809 commit_list_insert_by_date_cached(p, list, cached_base, cache_ptr);
810 }
811 return 0;
812 }
813
814 /*
815 * Ok, the commit wasn't uninteresting. Try to
816 * simplify the commit history and find the parent
817 * that has no differences in the path set if one exists.
818 */
819 try_to_simplify_commit(revs, commit);
820
821 if (revs->no_walk)
822 return 0;
823
824 left_flag = (commit->object.flags & SYMMETRIC_LEFT);
825
826 for (parent = commit->parents; parent; parent = parent->next) {
827 struct commit *p = parent->item;
828 int gently = revs->ignore_missing_links ||
829 revs->exclude_promisor_objects;
830 if (parse_commit_gently(p, gently) < 0) {
831 if (revs->exclude_promisor_objects &&
832 is_promisor_object(&p->object.oid)) {
833 if (revs->first_parent_only)
834 break;
835 continue;
836 }
837 return -1;
838 }
839 if (revs->sources) {
840 char **slot = revision_sources_at(revs->sources, p);
841
842 if (!*slot)
843 *slot = *revision_sources_at(revs->sources, commit);
844 }
845 p->object.flags |= left_flag;
846 if (!(p->object.flags & SEEN)) {
847 p->object.flags |= SEEN;
848 commit_list_insert_by_date_cached(p, list, cached_base, cache_ptr);
849 }
850 if (revs->first_parent_only)
851 break;
852 }
853 return 0;
854}
855
856static void cherry_pick_list(struct commit_list *list, struct rev_info *revs)
857{
858 struct commit_list *p;
859 int left_count = 0, right_count = 0;
860 int left_first;
861 struct patch_ids ids;
862 unsigned cherry_flag;
863
864 /* First count the commits on the left and on the right */
865 for (p = list; p; p = p->next) {
866 struct commit *commit = p->item;
867 unsigned flags = commit->object.flags;
868 if (flags & BOUNDARY)
869 ;
870 else if (flags & SYMMETRIC_LEFT)
871 left_count++;
872 else
873 right_count++;
874 }
875
876 if (!left_count || !right_count)
877 return;
878
879 left_first = left_count < right_count;
880 init_patch_ids(&ids);
881 ids.diffopts.pathspec = revs->diffopt.pathspec;
882
883 /* Compute patch-ids for one side */
884 for (p = list; p; p = p->next) {
885 struct commit *commit = p->item;
886 unsigned flags = commit->object.flags;
887
888 if (flags & BOUNDARY)
889 continue;
890 /*
891 * If we have fewer left, left_first is set and we omit
892 * commits on the right branch in this loop. If we have
893 * fewer right, we skip the left ones.
894 */
895 if (left_first != !!(flags & SYMMETRIC_LEFT))
896 continue;
897 add_commit_patch_id(commit, &ids);
898 }
899
900 /* either cherry_mark or cherry_pick are true */
901 cherry_flag = revs->cherry_mark ? PATCHSAME : SHOWN;
902
903 /* Check the other side */
904 for (p = list; p; p = p->next) {
905 struct commit *commit = p->item;
906 struct patch_id *id;
907 unsigned flags = commit->object.flags;
908
909 if (flags & BOUNDARY)
910 continue;
911 /*
912 * If we have fewer left, left_first is set and we omit
913 * commits on the left branch in this loop.
914 */
915 if (left_first == !!(flags & SYMMETRIC_LEFT))
916 continue;
917
918 /*
919 * Have we seen the same patch id?
920 */
921 id = has_commit_patch_id(commit, &ids);
922 if (!id)
923 continue;
924
925 commit->object.flags |= cherry_flag;
926 id->commit->object.flags |= cherry_flag;
927 }
928
929 free_patch_ids(&ids);
930}
931
932/* How many extra uninteresting commits we want to see.. */
933#define SLOP 5
934
935static int still_interesting(struct commit_list *src, timestamp_t date, int slop,
936 struct commit **interesting_cache)
937{
938 /*
939 * No source list at all? We're definitely done..
940 */
941 if (!src)
942 return 0;
943
944 /*
945 * Does the destination list contain entries with a date
946 * before the source list? Definitely _not_ done.
947 */
948 if (date <= src->item->date)
949 return SLOP;
950
951 /*
952 * Does the source list still have interesting commits in
953 * it? Definitely not done..
954 */
955 if (!everybody_uninteresting(src, interesting_cache))
956 return SLOP;
957
958 /* Ok, we're closing in.. */
959 return slop-1;
960}
961
962/*
963 * "rev-list --ancestry-path A..B" computes commits that are ancestors
964 * of B but not ancestors of A but further limits the result to those
965 * that are descendants of A. This takes the list of bottom commits and
966 * the result of "A..B" without --ancestry-path, and limits the latter
967 * further to the ones that can reach one of the commits in "bottom".
968 */
969static void limit_to_ancestry(struct commit_list *bottom, struct commit_list *list)
970{
971 struct commit_list *p;
972 struct commit_list *rlist = NULL;
973 int made_progress;
974
975 /*
976 * Reverse the list so that it will be likely that we would
977 * process parents before children.
978 */
979 for (p = list; p; p = p->next)
980 commit_list_insert(p->item, &rlist);
981
982 for (p = bottom; p; p = p->next)
983 p->item->object.flags |= TMP_MARK;
984
985 /*
986 * Mark the ones that can reach bottom commits in "list",
987 * in a bottom-up fashion.
988 */
989 do {
990 made_progress = 0;
991 for (p = rlist; p; p = p->next) {
992 struct commit *c = p->item;
993 struct commit_list *parents;
994 if (c->object.flags & (TMP_MARK | UNINTERESTING))
995 continue;
996 for (parents = c->parents;
997 parents;
998 parents = parents->next) {
999 if (!(parents->item->object.flags & TMP_MARK))
1000 continue;
1001 c->object.flags |= TMP_MARK;
1002 made_progress = 1;
1003 break;
1004 }
1005 }
1006 } while (made_progress);
1007
1008 /*
1009 * NEEDSWORK: decide if we want to remove parents that are
1010 * not marked with TMP_MARK from commit->parents for commits
1011 * in the resulting list. We may not want to do that, though.
1012 */
1013
1014 /*
1015 * The ones that are not marked with TMP_MARK are uninteresting
1016 */
1017 for (p = list; p; p = p->next) {
1018 struct commit *c = p->item;
1019 if (c->object.flags & TMP_MARK)
1020 continue;
1021 c->object.flags |= UNINTERESTING;
1022 }
1023
1024 /* We are done with the TMP_MARK */
1025 for (p = list; p; p = p->next)
1026 p->item->object.flags &= ~TMP_MARK;
1027 for (p = bottom; p; p = p->next)
1028 p->item->object.flags &= ~TMP_MARK;
1029 free_commit_list(rlist);
1030}
1031
1032/*
1033 * Before walking the history, keep the set of "negative" refs the
1034 * caller has asked to exclude.
1035 *
1036 * This is used to compute "rev-list --ancestry-path A..B", as we need
1037 * to filter the result of "A..B" further to the ones that can actually
1038 * reach A.
1039 */
1040static struct commit_list *collect_bottom_commits(struct commit_list *list)
1041{
1042 struct commit_list *elem, *bottom = NULL;
1043 for (elem = list; elem; elem = elem->next)
1044 if (elem->item->object.flags & BOTTOM)
1045 commit_list_insert(elem->item, &bottom);
1046 return bottom;
1047}
1048
1049/* Assumes either left_only or right_only is set */
1050static void limit_left_right(struct commit_list *list, struct rev_info *revs)
1051{
1052 struct commit_list *p;
1053
1054 for (p = list; p; p = p->next) {
1055 struct commit *commit = p->item;
1056
1057 if (revs->right_only) {
1058 if (commit->object.flags & SYMMETRIC_LEFT)
1059 commit->object.flags |= SHOWN;
1060 } else /* revs->left_only is set */
1061 if (!(commit->object.flags & SYMMETRIC_LEFT))
1062 commit->object.flags |= SHOWN;
1063 }
1064}
1065
1066static int limit_list(struct rev_info *revs)
1067{
1068 int slop = SLOP;
1069 timestamp_t date = TIME_MAX;
1070 struct commit_list *list = revs->commits;
1071 struct commit_list *newlist = NULL;
1072 struct commit_list **p = &newlist;
1073 struct commit_list *bottom = NULL;
1074 struct commit *interesting_cache = NULL;
1075
1076 if (revs->ancestry_path) {
1077 bottom = collect_bottom_commits(list);
1078 if (!bottom)
1079 die("--ancestry-path given but there are no bottom commits");
1080 }
1081
1082 while (list) {
1083 struct commit *commit = pop_commit(&list);
1084 struct object *obj = &commit->object;
1085 show_early_output_fn_t show;
1086
1087 if (commit == interesting_cache)
1088 interesting_cache = NULL;
1089
1090 if (revs->max_age != -1 && (commit->date < revs->max_age))
1091 obj->flags |= UNINTERESTING;
1092 if (add_parents_to_list(revs, commit, &list, NULL) < 0)
1093 return -1;
1094 if (obj->flags & UNINTERESTING) {
1095 mark_parents_uninteresting(commit);
1096 slop = still_interesting(list, date, slop, &interesting_cache);
1097 if (slop)
1098 continue;
1099 break;
1100 }
1101 if (revs->min_age != -1 && (commit->date > revs->min_age))
1102 continue;
1103 date = commit->date;
1104 p = &commit_list_insert(commit, p)->next;
1105
1106 show = show_early_output;
1107 if (!show)
1108 continue;
1109
1110 show(revs, newlist);
1111 show_early_output = NULL;
1112 }
1113 if (revs->cherry_pick || revs->cherry_mark)
1114 cherry_pick_list(newlist, revs);
1115
1116 if (revs->left_only || revs->right_only)
1117 limit_left_right(newlist, revs);
1118
1119 if (bottom) {
1120 limit_to_ancestry(bottom, newlist);
1121 free_commit_list(bottom);
1122 }
1123
1124 /*
1125 * Check if any commits have become TREESAME by some of their parents
1126 * becoming UNINTERESTING.
1127 */
1128 if (limiting_can_increase_treesame(revs))
1129 for (list = newlist; list; list = list->next) {
1130 struct commit *c = list->item;
1131 if (c->object.flags & (UNINTERESTING | TREESAME))
1132 continue;
1133 update_treesame(revs, c);
1134 }
1135
1136 revs->commits = newlist;
1137 return 0;
1138}
1139
1140/*
1141 * Add an entry to refs->cmdline with the specified information.
1142 * *name is copied.
1143 */
1144static void add_rev_cmdline(struct rev_info *revs,
1145 struct object *item,
1146 const char *name,
1147 int whence,
1148 unsigned flags)
1149{
1150 struct rev_cmdline_info *info = &revs->cmdline;
1151 unsigned int nr = info->nr;
1152
1153 ALLOC_GROW(info->rev, nr + 1, info->alloc);
1154 info->rev[nr].item = item;
1155 info->rev[nr].name = xstrdup(name);
1156 info->rev[nr].whence = whence;
1157 info->rev[nr].flags = flags;
1158 info->nr++;
1159}
1160
1161static void add_rev_cmdline_list(struct rev_info *revs,
1162 struct commit_list *commit_list,
1163 int whence,
1164 unsigned flags)
1165{
1166 while (commit_list) {
1167 struct object *object = &commit_list->item->object;
1168 add_rev_cmdline(revs, object, oid_to_hex(&object->oid),
1169 whence, flags);
1170 commit_list = commit_list->next;
1171 }
1172}
1173
1174struct all_refs_cb {
1175 int all_flags;
1176 int warned_bad_reflog;
1177 struct rev_info *all_revs;
1178 const char *name_for_errormsg;
1179 struct ref_store *refs;
1180};
1181
1182int ref_excluded(struct string_list *ref_excludes, const char *path)
1183{
1184 struct string_list_item *item;
1185
1186 if (!ref_excludes)
1187 return 0;
1188 for_each_string_list_item(item, ref_excludes) {
1189 if (!wildmatch(item->string, path, 0))
1190 return 1;
1191 }
1192 return 0;
1193}
1194
1195static int handle_one_ref(const char *path, const struct object_id *oid,
1196 int flag, void *cb_data)
1197{
1198 struct all_refs_cb *cb = cb_data;
1199 struct object *object;
1200
1201 if (ref_excluded(cb->all_revs->ref_excludes, path))
1202 return 0;
1203
1204 object = get_reference(cb->all_revs, path, oid, cb->all_flags);
1205 add_rev_cmdline(cb->all_revs, object, path, REV_CMD_REF, cb->all_flags);
1206 add_pending_oid(cb->all_revs, path, oid, cb->all_flags);
1207 return 0;
1208}
1209
1210static void init_all_refs_cb(struct all_refs_cb *cb, struct rev_info *revs,
1211 unsigned flags)
1212{
1213 cb->all_revs = revs;
1214 cb->all_flags = flags;
1215 revs->rev_input_given = 1;
1216 cb->refs = NULL;
1217}
1218
1219void clear_ref_exclusion(struct string_list **ref_excludes_p)
1220{
1221 if (*ref_excludes_p) {
1222 string_list_clear(*ref_excludes_p, 0);
1223 free(*ref_excludes_p);
1224 }
1225 *ref_excludes_p = NULL;
1226}
1227
1228void add_ref_exclusion(struct string_list **ref_excludes_p, const char *exclude)
1229{
1230 if (!*ref_excludes_p) {
1231 *ref_excludes_p = xcalloc(1, sizeof(**ref_excludes_p));
1232 (*ref_excludes_p)->strdup_strings = 1;
1233 }
1234 string_list_append(*ref_excludes_p, exclude);
1235}
1236
1237static void handle_refs(struct ref_store *refs,
1238 struct rev_info *revs, unsigned flags,
1239 int (*for_each)(struct ref_store *, each_ref_fn, void *))
1240{
1241 struct all_refs_cb cb;
1242
1243 if (!refs) {
1244 /* this could happen with uninitialized submodules */
1245 return;
1246 }
1247
1248 init_all_refs_cb(&cb, revs, flags);
1249 for_each(refs, handle_one_ref, &cb);
1250}
1251
1252static void handle_one_reflog_commit(struct object_id *oid, void *cb_data)
1253{
1254 struct all_refs_cb *cb = cb_data;
1255 if (!is_null_oid(oid)) {
1256 struct object *o = parse_object(the_repository, oid);
1257 if (o) {
1258 o->flags |= cb->all_flags;
1259 /* ??? CMDLINEFLAGS ??? */
1260 add_pending_object(cb->all_revs, o, "");
1261 }
1262 else if (!cb->warned_bad_reflog) {
1263 warning("reflog of '%s' references pruned commits",
1264 cb->name_for_errormsg);
1265 cb->warned_bad_reflog = 1;
1266 }
1267 }
1268}
1269
1270static int handle_one_reflog_ent(struct object_id *ooid, struct object_id *noid,
1271 const char *email, timestamp_t timestamp, int tz,
1272 const char *message, void *cb_data)
1273{
1274 handle_one_reflog_commit(ooid, cb_data);
1275 handle_one_reflog_commit(noid, cb_data);
1276 return 0;
1277}
1278
1279static int handle_one_reflog(const char *path, const struct object_id *oid,
1280 int flag, void *cb_data)
1281{
1282 struct all_refs_cb *cb = cb_data;
1283 cb->warned_bad_reflog = 0;
1284 cb->name_for_errormsg = path;
1285 refs_for_each_reflog_ent(cb->refs, path,
1286 handle_one_reflog_ent, cb_data);
1287 return 0;
1288}
1289
1290static void add_other_reflogs_to_pending(struct all_refs_cb *cb)
1291{
1292 struct worktree **worktrees, **p;
1293
1294 worktrees = get_worktrees(0);
1295 for (p = worktrees; *p; p++) {
1296 struct worktree *wt = *p;
1297
1298 if (wt->is_current)
1299 continue;
1300
1301 cb->refs = get_worktree_ref_store(wt);
1302 refs_for_each_reflog(cb->refs,
1303 handle_one_reflog,
1304 cb);
1305 }
1306 free_worktrees(worktrees);
1307}
1308
1309void add_reflogs_to_pending(struct rev_info *revs, unsigned flags)
1310{
1311 struct all_refs_cb cb;
1312
1313 cb.all_revs = revs;
1314 cb.all_flags = flags;
1315 cb.refs = get_main_ref_store(the_repository);
1316 for_each_reflog(handle_one_reflog, &cb);
1317
1318 if (!revs->single_worktree)
1319 add_other_reflogs_to_pending(&cb);
1320}
1321
1322static void add_cache_tree(struct cache_tree *it, struct rev_info *revs,
1323 struct strbuf *path)
1324{
1325 size_t baselen = path->len;
1326 int i;
1327
1328 if (it->entry_count >= 0) {
1329 struct tree *tree = lookup_tree(the_repository, &it->oid);
1330 add_pending_object_with_path(revs, &tree->object, "",
1331 040000, path->buf);
1332 }
1333
1334 for (i = 0; i < it->subtree_nr; i++) {
1335 struct cache_tree_sub *sub = it->down[i];
1336 strbuf_addf(path, "%s%s", baselen ? "/" : "", sub->name);
1337 add_cache_tree(sub->cache_tree, revs, path);
1338 strbuf_setlen(path, baselen);
1339 }
1340
1341}
1342
1343static void do_add_index_objects_to_pending(struct rev_info *revs,
1344 struct index_state *istate)
1345{
1346 int i;
1347
1348 for (i = 0; i < istate->cache_nr; i++) {
1349 struct cache_entry *ce = istate->cache[i];
1350 struct blob *blob;
1351
1352 if (S_ISGITLINK(ce->ce_mode))
1353 continue;
1354
1355 blob = lookup_blob(the_repository, &ce->oid);
1356 if (!blob)
1357 die("unable to add index blob to traversal");
1358 add_pending_object_with_path(revs, &blob->object, "",
1359 ce->ce_mode, ce->name);
1360 }
1361
1362 if (istate->cache_tree) {
1363 struct strbuf path = STRBUF_INIT;
1364 add_cache_tree(istate->cache_tree, revs, &path);
1365 strbuf_release(&path);
1366 }
1367}
1368
1369void add_index_objects_to_pending(struct rev_info *revs, unsigned int flags)
1370{
1371 struct worktree **worktrees, **p;
1372
1373 read_cache();
1374 do_add_index_objects_to_pending(revs, &the_index);
1375
1376 if (revs->single_worktree)
1377 return;
1378
1379 worktrees = get_worktrees(0);
1380 for (p = worktrees; *p; p++) {
1381 struct worktree *wt = *p;
1382 struct index_state istate = { NULL };
1383
1384 if (wt->is_current)
1385 continue; /* current index already taken care of */
1386
1387 if (read_index_from(&istate,
1388 worktree_git_path(wt, "index"),
1389 get_worktree_git_dir(wt)) > 0)
1390 do_add_index_objects_to_pending(revs, &istate);
1391 discard_index(&istate);
1392 }
1393 free_worktrees(worktrees);
1394}
1395
1396static int add_parents_only(struct rev_info *revs, const char *arg_, int flags,
1397 int exclude_parent)
1398{
1399 struct object_id oid;
1400 struct object *it;
1401 struct commit *commit;
1402 struct commit_list *parents;
1403 int parent_number;
1404 const char *arg = arg_;
1405
1406 if (*arg == '^') {
1407 flags ^= UNINTERESTING | BOTTOM;
1408 arg++;
1409 }
1410 if (get_oid_committish(arg, &oid))
1411 return 0;
1412 while (1) {
1413 it = get_reference(revs, arg, &oid, 0);
1414 if (!it && revs->ignore_missing)
1415 return 0;
1416 if (it->type != OBJ_TAG)
1417 break;
1418 if (!((struct tag*)it)->tagged)
1419 return 0;
1420 oidcpy(&oid, &((struct tag*)it)->tagged->oid);
1421 }
1422 if (it->type != OBJ_COMMIT)
1423 return 0;
1424 commit = (struct commit *)it;
1425 if (exclude_parent &&
1426 exclude_parent > commit_list_count(commit->parents))
1427 return 0;
1428 for (parents = commit->parents, parent_number = 1;
1429 parents;
1430 parents = parents->next, parent_number++) {
1431 if (exclude_parent && parent_number != exclude_parent)
1432 continue;
1433
1434 it = &parents->item->object;
1435 it->flags |= flags;
1436 add_rev_cmdline(revs, it, arg_, REV_CMD_PARENTS_ONLY, flags);
1437 add_pending_object(revs, it, arg);
1438 }
1439 return 1;
1440}
1441
1442void init_revisions(struct rev_info *revs, const char *prefix)
1443{
1444 memset(revs, 0, sizeof(*revs));
1445
1446 revs->abbrev = DEFAULT_ABBREV;
1447 revs->ignore_merges = 1;
1448 revs->simplify_history = 1;
1449 revs->pruning.flags.recursive = 1;
1450 revs->pruning.flags.quick = 1;
1451 revs->pruning.add_remove = file_add_remove;
1452 revs->pruning.change = file_change;
1453 revs->pruning.change_fn_data = revs;
1454 revs->sort_order = REV_SORT_IN_GRAPH_ORDER;
1455 revs->dense = 1;
1456 revs->prefix = prefix;
1457 revs->max_age = -1;
1458 revs->min_age = -1;
1459 revs->skip_count = -1;
1460 revs->max_count = -1;
1461 revs->max_parents = -1;
1462 revs->expand_tabs_in_log = -1;
1463
1464 revs->commit_format = CMIT_FMT_DEFAULT;
1465 revs->expand_tabs_in_log_default = 8;
1466
1467 init_grep_defaults();
1468 grep_init(&revs->grep_filter, prefix);
1469 revs->grep_filter.status_only = 1;
1470
1471 diff_setup(&revs->diffopt);
1472 if (prefix && !revs->diffopt.prefix) {
1473 revs->diffopt.prefix = prefix;
1474 revs->diffopt.prefix_length = strlen(prefix);
1475 }
1476
1477 revs->notes_opt.use_default_notes = -1;
1478}
1479
1480static void add_pending_commit_list(struct rev_info *revs,
1481 struct commit_list *commit_list,
1482 unsigned int flags)
1483{
1484 while (commit_list) {
1485 struct object *object = &commit_list->item->object;
1486 object->flags |= flags;
1487 add_pending_object(revs, object, oid_to_hex(&object->oid));
1488 commit_list = commit_list->next;
1489 }
1490}
1491
1492static void prepare_show_merge(struct rev_info *revs)
1493{
1494 struct commit_list *bases;
1495 struct commit *head, *other;
1496 struct object_id oid;
1497 const char **prune = NULL;
1498 int i, prune_num = 1; /* counting terminating NULL */
1499
1500 if (get_oid("HEAD", &oid))
1501 die("--merge without HEAD?");
1502 head = lookup_commit_or_die(&oid, "HEAD");
1503 if (get_oid("MERGE_HEAD", &oid))
1504 die("--merge without MERGE_HEAD?");
1505 other = lookup_commit_or_die(&oid, "MERGE_HEAD");
1506 add_pending_object(revs, &head->object, "HEAD");
1507 add_pending_object(revs, &other->object, "MERGE_HEAD");
1508 bases = get_merge_bases(head, other);
1509 add_rev_cmdline_list(revs, bases, REV_CMD_MERGE_BASE, UNINTERESTING | BOTTOM);
1510 add_pending_commit_list(revs, bases, UNINTERESTING | BOTTOM);
1511 free_commit_list(bases);
1512 head->object.flags |= SYMMETRIC_LEFT;
1513
1514 if (!active_nr)
1515 read_cache();
1516 for (i = 0; i < active_nr; i++) {
1517 const struct cache_entry *ce = active_cache[i];
1518 if (!ce_stage(ce))
1519 continue;
1520 if (ce_path_match(&the_index, ce, &revs->prune_data, NULL)) {
1521 prune_num++;
1522 REALLOC_ARRAY(prune, prune_num);
1523 prune[prune_num-2] = ce->name;
1524 prune[prune_num-1] = NULL;
1525 }
1526 while ((i+1 < active_nr) &&
1527 ce_same_name(ce, active_cache[i+1]))
1528 i++;
1529 }
1530 clear_pathspec(&revs->prune_data);
1531 parse_pathspec(&revs->prune_data, PATHSPEC_ALL_MAGIC & ~PATHSPEC_LITERAL,
1532 PATHSPEC_PREFER_FULL | PATHSPEC_LITERAL_PATH, "", prune);
1533 revs->limited = 1;
1534}
1535
1536static int dotdot_missing(const char *arg, char *dotdot,
1537 struct rev_info *revs, int symmetric)
1538{
1539 if (revs->ignore_missing)
1540 return 0;
1541 /* de-munge so we report the full argument */
1542 *dotdot = '.';
1543 die(symmetric
1544 ? "Invalid symmetric difference expression %s"
1545 : "Invalid revision range %s", arg);
1546}
1547
1548static int handle_dotdot_1(const char *arg, char *dotdot,
1549 struct rev_info *revs, int flags,
1550 int cant_be_filename,
1551 struct object_context *a_oc,
1552 struct object_context *b_oc)
1553{
1554 const char *a_name, *b_name;
1555 struct object_id a_oid, b_oid;
1556 struct object *a_obj, *b_obj;
1557 unsigned int a_flags, b_flags;
1558 int symmetric = 0;
1559 unsigned int flags_exclude = flags ^ (UNINTERESTING | BOTTOM);
1560 unsigned int oc_flags = GET_OID_COMMITTISH | GET_OID_RECORD_PATH;
1561
1562 a_name = arg;
1563 if (!*a_name)
1564 a_name = "HEAD";
1565
1566 b_name = dotdot + 2;
1567 if (*b_name == '.') {
1568 symmetric = 1;
1569 b_name++;
1570 }
1571 if (!*b_name)
1572 b_name = "HEAD";
1573
1574 if (get_oid_with_context(a_name, oc_flags, &a_oid, a_oc) ||
1575 get_oid_with_context(b_name, oc_flags, &b_oid, b_oc))
1576 return -1;
1577
1578 if (!cant_be_filename) {
1579 *dotdot = '.';
1580 verify_non_filename(revs->prefix, arg);
1581 *dotdot = '\0';
1582 }
1583
1584 a_obj = parse_object(the_repository, &a_oid);
1585 b_obj = parse_object(the_repository, &b_oid);
1586 if (!a_obj || !b_obj)
1587 return dotdot_missing(arg, dotdot, revs, symmetric);
1588
1589 if (!symmetric) {
1590 /* just A..B */
1591 b_flags = flags;
1592 a_flags = flags_exclude;
1593 } else {
1594 /* A...B -- find merge bases between the two */
1595 struct commit *a, *b;
1596 struct commit_list *exclude;
1597
1598 a = lookup_commit_reference(the_repository, &a_obj->oid);
1599 b = lookup_commit_reference(the_repository, &b_obj->oid);
1600 if (!a || !b)
1601 return dotdot_missing(arg, dotdot, revs, symmetric);
1602
1603 exclude = get_merge_bases(a, b);
1604 add_rev_cmdline_list(revs, exclude, REV_CMD_MERGE_BASE,
1605 flags_exclude);
1606 add_pending_commit_list(revs, exclude, flags_exclude);
1607 free_commit_list(exclude);
1608
1609 b_flags = flags;
1610 a_flags = flags | SYMMETRIC_LEFT;
1611 }
1612
1613 a_obj->flags |= a_flags;
1614 b_obj->flags |= b_flags;
1615 add_rev_cmdline(revs, a_obj, a_name, REV_CMD_LEFT, a_flags);
1616 add_rev_cmdline(revs, b_obj, b_name, REV_CMD_RIGHT, b_flags);
1617 add_pending_object_with_path(revs, a_obj, a_name, a_oc->mode, a_oc->path);
1618 add_pending_object_with_path(revs, b_obj, b_name, b_oc->mode, b_oc->path);
1619 return 0;
1620}
1621
1622static int handle_dotdot(const char *arg,
1623 struct rev_info *revs, int flags,
1624 int cant_be_filename)
1625{
1626 struct object_context a_oc, b_oc;
1627 char *dotdot = strstr(arg, "..");
1628 int ret;
1629
1630 if (!dotdot)
1631 return -1;
1632
1633 memset(&a_oc, 0, sizeof(a_oc));
1634 memset(&b_oc, 0, sizeof(b_oc));
1635
1636 *dotdot = '\0';
1637 ret = handle_dotdot_1(arg, dotdot, revs, flags, cant_be_filename,
1638 &a_oc, &b_oc);
1639 *dotdot = '.';
1640
1641 free(a_oc.path);
1642 free(b_oc.path);
1643
1644 return ret;
1645}
1646
1647int handle_revision_arg(const char *arg_, struct rev_info *revs, int flags, unsigned revarg_opt)
1648{
1649 struct object_context oc;
1650 char *mark;
1651 struct object *object;
1652 struct object_id oid;
1653 int local_flags;
1654 const char *arg = arg_;
1655 int cant_be_filename = revarg_opt & REVARG_CANNOT_BE_FILENAME;
1656 unsigned get_sha1_flags = GET_OID_RECORD_PATH;
1657
1658 flags = flags & UNINTERESTING ? flags | BOTTOM : flags & ~BOTTOM;
1659
1660 if (!cant_be_filename && !strcmp(arg, "..")) {
1661 /*
1662 * Just ".."? That is not a range but the
1663 * pathspec for the parent directory.
1664 */
1665 return -1;
1666 }
1667
1668 if (!handle_dotdot(arg, revs, flags, revarg_opt))
1669 return 0;
1670
1671 mark = strstr(arg, "^@");
1672 if (mark && !mark[2]) {
1673 *mark = 0;
1674 if (add_parents_only(revs, arg, flags, 0))
1675 return 0;
1676 *mark = '^';
1677 }
1678 mark = strstr(arg, "^!");
1679 if (mark && !mark[2]) {
1680 *mark = 0;
1681 if (!add_parents_only(revs, arg, flags ^ (UNINTERESTING | BOTTOM), 0))
1682 *mark = '^';
1683 }
1684 mark = strstr(arg, "^-");
1685 if (mark) {
1686 int exclude_parent = 1;
1687
1688 if (mark[2]) {
1689 char *end;
1690 exclude_parent = strtoul(mark + 2, &end, 10);
1691 if (*end != '\0' || !exclude_parent)
1692 return -1;
1693 }
1694
1695 *mark = 0;
1696 if (!add_parents_only(revs, arg, flags ^ (UNINTERESTING | BOTTOM), exclude_parent))
1697 *mark = '^';
1698 }
1699
1700 local_flags = 0;
1701 if (*arg == '^') {
1702 local_flags = UNINTERESTING | BOTTOM;
1703 arg++;
1704 }
1705
1706 if (revarg_opt & REVARG_COMMITTISH)
1707 get_sha1_flags |= GET_OID_COMMITTISH;
1708
1709 if (get_oid_with_context(arg, get_sha1_flags, &oid, &oc))
1710 return revs->ignore_missing ? 0 : -1;
1711 if (!cant_be_filename)
1712 verify_non_filename(revs->prefix, arg);
1713 object = get_reference(revs, arg, &oid, flags ^ local_flags);
1714 add_rev_cmdline(revs, object, arg_, REV_CMD_REV, flags ^ local_flags);
1715 add_pending_object_with_path(revs, object, arg, oc.mode, oc.path);
1716 free(oc.path);
1717 return 0;
1718}
1719
1720static void read_pathspec_from_stdin(struct rev_info *revs, struct strbuf *sb,
1721 struct argv_array *prune)
1722{
1723 while (strbuf_getline(sb, stdin) != EOF)
1724 argv_array_push(prune, sb->buf);
1725}
1726
1727static void read_revisions_from_stdin(struct rev_info *revs,
1728 struct argv_array *prune)
1729{
1730 struct strbuf sb;
1731 int seen_dashdash = 0;
1732 int save_warning;
1733
1734 save_warning = warn_on_object_refname_ambiguity;
1735 warn_on_object_refname_ambiguity = 0;
1736
1737 strbuf_init(&sb, 1000);
1738 while (strbuf_getline(&sb, stdin) != EOF) {
1739 int len = sb.len;
1740 if (!len)
1741 break;
1742 if (sb.buf[0] == '-') {
1743 if (len == 2 && sb.buf[1] == '-') {
1744 seen_dashdash = 1;
1745 break;
1746 }
1747 die("options not supported in --stdin mode");
1748 }
1749 if (handle_revision_arg(sb.buf, revs, 0,
1750 REVARG_CANNOT_BE_FILENAME))
1751 die("bad revision '%s'", sb.buf);
1752 }
1753 if (seen_dashdash)
1754 read_pathspec_from_stdin(revs, &sb, prune);
1755
1756 strbuf_release(&sb);
1757 warn_on_object_refname_ambiguity = save_warning;
1758}
1759
1760static void add_grep(struct rev_info *revs, const char *ptn, enum grep_pat_token what)
1761{
1762 append_grep_pattern(&revs->grep_filter, ptn, "command line", 0, what);
1763}
1764
1765static void add_header_grep(struct rev_info *revs, enum grep_header_field field, const char *pattern)
1766{
1767 append_header_grep_pattern(&revs->grep_filter, field, pattern);
1768}
1769
1770static void add_message_grep(struct rev_info *revs, const char *pattern)
1771{
1772 add_grep(revs, pattern, GREP_PATTERN_BODY);
1773}
1774
1775static int handle_revision_opt(struct rev_info *revs, int argc, const char **argv,
1776 int *unkc, const char **unkv)
1777{
1778 const char *arg = argv[0];
1779 const char *optarg;
1780 int argcount;
1781 const unsigned hexsz = the_hash_algo->hexsz;
1782
1783 /* pseudo revision arguments */
1784 if (!strcmp(arg, "--all") || !strcmp(arg, "--branches") ||
1785 !strcmp(arg, "--tags") || !strcmp(arg, "--remotes") ||
1786 !strcmp(arg, "--reflog") || !strcmp(arg, "--not") ||
1787 !strcmp(arg, "--no-walk") || !strcmp(arg, "--do-walk") ||
1788 !strcmp(arg, "--bisect") || starts_with(arg, "--glob=") ||
1789 !strcmp(arg, "--indexed-objects") ||
1790 starts_with(arg, "--exclude=") ||
1791 starts_with(arg, "--branches=") || starts_with(arg, "--tags=") ||
1792 starts_with(arg, "--remotes=") || starts_with(arg, "--no-walk="))
1793 {
1794 unkv[(*unkc)++] = arg;
1795 return 1;
1796 }
1797
1798 if ((argcount = parse_long_opt("max-count", argv, &optarg))) {
1799 revs->max_count = atoi(optarg);
1800 revs->no_walk = 0;
1801 return argcount;
1802 } else if ((argcount = parse_long_opt("skip", argv, &optarg))) {
1803 revs->skip_count = atoi(optarg);
1804 return argcount;
1805 } else if ((*arg == '-') && isdigit(arg[1])) {
1806 /* accept -<digit>, like traditional "head" */
1807 if (strtol_i(arg + 1, 10, &revs->max_count) < 0 ||
1808 revs->max_count < 0)
1809 die("'%s': not a non-negative integer", arg + 1);
1810 revs->no_walk = 0;
1811 } else if (!strcmp(arg, "-n")) {
1812 if (argc <= 1)
1813 return error("-n requires an argument");
1814 revs->max_count = atoi(argv[1]);
1815 revs->no_walk = 0;
1816 return 2;
1817 } else if (skip_prefix(arg, "-n", &optarg)) {
1818 revs->max_count = atoi(optarg);
1819 revs->no_walk = 0;
1820 } else if ((argcount = parse_long_opt("max-age", argv, &optarg))) {
1821 revs->max_age = atoi(optarg);
1822 return argcount;
1823 } else if ((argcount = parse_long_opt("since", argv, &optarg))) {
1824 revs->max_age = approxidate(optarg);
1825 return argcount;
1826 } else if ((argcount = parse_long_opt("after", argv, &optarg))) {
1827 revs->max_age = approxidate(optarg);
1828 return argcount;
1829 } else if ((argcount = parse_long_opt("min-age", argv, &optarg))) {
1830 revs->min_age = atoi(optarg);
1831 return argcount;
1832 } else if ((argcount = parse_long_opt("before", argv, &optarg))) {
1833 revs->min_age = approxidate(optarg);
1834 return argcount;
1835 } else if ((argcount = parse_long_opt("until", argv, &optarg))) {
1836 revs->min_age = approxidate(optarg);
1837 return argcount;
1838 } else if (!strcmp(arg, "--first-parent")) {
1839 revs->first_parent_only = 1;
1840 } else if (!strcmp(arg, "--ancestry-path")) {
1841 revs->ancestry_path = 1;
1842 revs->simplify_history = 0;
1843 revs->limited = 1;
1844 } else if (!strcmp(arg, "-g") || !strcmp(arg, "--walk-reflogs")) {
1845 init_reflog_walk(&revs->reflog_info);
1846 } else if (!strcmp(arg, "--default")) {
1847 if (argc <= 1)
1848 return error("bad --default argument");
1849 revs->def = argv[1];
1850 return 2;
1851 } else if (!strcmp(arg, "--merge")) {
1852 revs->show_merge = 1;
1853 } else if (!strcmp(arg, "--topo-order")) {
1854 revs->sort_order = REV_SORT_IN_GRAPH_ORDER;
1855 revs->topo_order = 1;
1856 } else if (!strcmp(arg, "--simplify-merges")) {
1857 revs->simplify_merges = 1;
1858 revs->topo_order = 1;
1859 revs->rewrite_parents = 1;
1860 revs->simplify_history = 0;
1861 revs->limited = 1;
1862 } else if (!strcmp(arg, "--simplify-by-decoration")) {
1863 revs->simplify_merges = 1;
1864 revs->topo_order = 1;
1865 revs->rewrite_parents = 1;
1866 revs->simplify_history = 0;
1867 revs->simplify_by_decoration = 1;
1868 revs->limited = 1;
1869 revs->prune = 1;
1870 load_ref_decorations(NULL, DECORATE_SHORT_REFS);
1871 } else if (!strcmp(arg, "--date-order")) {
1872 revs->sort_order = REV_SORT_BY_COMMIT_DATE;
1873 revs->topo_order = 1;
1874 } else if (!strcmp(arg, "--author-date-order")) {
1875 revs->sort_order = REV_SORT_BY_AUTHOR_DATE;
1876 revs->topo_order = 1;
1877 } else if (!strcmp(arg, "--early-output")) {
1878 revs->early_output = 100;
1879 revs->topo_order = 1;
1880 } else if (skip_prefix(arg, "--early-output=", &optarg)) {
1881 if (strtoul_ui(optarg, 10, &revs->early_output) < 0)
1882 die("'%s': not a non-negative integer", optarg);
1883 revs->topo_order = 1;
1884 } else if (!strcmp(arg, "--parents")) {
1885 revs->rewrite_parents = 1;
1886 revs->print_parents = 1;
1887 } else if (!strcmp(arg, "--dense")) {
1888 revs->dense = 1;
1889 } else if (!strcmp(arg, "--sparse")) {
1890 revs->dense = 0;
1891 } else if (!strcmp(arg, "--in-commit-order")) {
1892 revs->tree_blobs_in_commit_order = 1;
1893 } else if (!strcmp(arg, "--remove-empty")) {
1894 revs->remove_empty_trees = 1;
1895 } else if (!strcmp(arg, "--merges")) {
1896 revs->min_parents = 2;
1897 } else if (!strcmp(arg, "--no-merges")) {
1898 revs->max_parents = 1;
1899 } else if (skip_prefix(arg, "--min-parents=", &optarg)) {
1900 revs->min_parents = atoi(optarg);
1901 } else if (!strcmp(arg, "--no-min-parents")) {
1902 revs->min_parents = 0;
1903 } else if (skip_prefix(arg, "--max-parents=", &optarg)) {
1904 revs->max_parents = atoi(optarg);
1905 } else if (!strcmp(arg, "--no-max-parents")) {
1906 revs->max_parents = -1;
1907 } else if (!strcmp(arg, "--boundary")) {
1908 revs->boundary = 1;
1909 } else if (!strcmp(arg, "--left-right")) {
1910 revs->left_right = 1;
1911 } else if (!strcmp(arg, "--left-only")) {
1912 if (revs->right_only)
1913 die("--left-only is incompatible with --right-only"
1914 " or --cherry");
1915 revs->left_only = 1;
1916 } else if (!strcmp(arg, "--right-only")) {
1917 if (revs->left_only)
1918 die("--right-only is incompatible with --left-only");
1919 revs->right_only = 1;
1920 } else if (!strcmp(arg, "--cherry")) {
1921 if (revs->left_only)
1922 die("--cherry is incompatible with --left-only");
1923 revs->cherry_mark = 1;
1924 revs->right_only = 1;
1925 revs->max_parents = 1;
1926 revs->limited = 1;
1927 } else if (!strcmp(arg, "--count")) {
1928 revs->count = 1;
1929 } else if (!strcmp(arg, "--cherry-mark")) {
1930 if (revs->cherry_pick)
1931 die("--cherry-mark is incompatible with --cherry-pick");
1932 revs->cherry_mark = 1;
1933 revs->limited = 1; /* needs limit_list() */
1934 } else if (!strcmp(arg, "--cherry-pick")) {
1935 if (revs->cherry_mark)
1936 die("--cherry-pick is incompatible with --cherry-mark");
1937 revs->cherry_pick = 1;
1938 revs->limited = 1;
1939 } else if (!strcmp(arg, "--objects")) {
1940 revs->tag_objects = 1;
1941 revs->tree_objects = 1;
1942 revs->blob_objects = 1;
1943 } else if (!strcmp(arg, "--objects-edge")) {
1944 revs->tag_objects = 1;
1945 revs->tree_objects = 1;
1946 revs->blob_objects = 1;
1947 revs->edge_hint = 1;
1948 } else if (!strcmp(arg, "--objects-edge-aggressive")) {
1949 revs->tag_objects = 1;
1950 revs->tree_objects = 1;
1951 revs->blob_objects = 1;
1952 revs->edge_hint = 1;
1953 revs->edge_hint_aggressive = 1;
1954 } else if (!strcmp(arg, "--verify-objects")) {
1955 revs->tag_objects = 1;
1956 revs->tree_objects = 1;
1957 revs->blob_objects = 1;
1958 revs->verify_objects = 1;
1959 } else if (!strcmp(arg, "--unpacked")) {
1960 revs->unpacked = 1;
1961 } else if (starts_with(arg, "--unpacked=")) {
1962 die("--unpacked=<packfile> no longer supported.");
1963 } else if (!strcmp(arg, "-r")) {
1964 revs->diff = 1;
1965 revs->diffopt.flags.recursive = 1;
1966 } else if (!strcmp(arg, "-t")) {
1967 revs->diff = 1;
1968 revs->diffopt.flags.recursive = 1;
1969 revs->diffopt.flags.tree_in_recursive = 1;
1970 } else if (!strcmp(arg, "-m")) {
1971 revs->ignore_merges = 0;
1972 } else if (!strcmp(arg, "-c")) {
1973 revs->diff = 1;
1974 revs->dense_combined_merges = 0;
1975 revs->combine_merges = 1;
1976 } else if (!strcmp(arg, "--cc")) {
1977 revs->diff = 1;
1978 revs->dense_combined_merges = 1;
1979 revs->combine_merges = 1;
1980 } else if (!strcmp(arg, "-v")) {
1981 revs->verbose_header = 1;
1982 } else if (!strcmp(arg, "--pretty")) {
1983 revs->verbose_header = 1;
1984 revs->pretty_given = 1;
1985 get_commit_format(NULL, revs);
1986 } else if (skip_prefix(arg, "--pretty=", &optarg) ||
1987 skip_prefix(arg, "--format=", &optarg)) {
1988 /*
1989 * Detached form ("--pretty X" as opposed to "--pretty=X")
1990 * not allowed, since the argument is optional.
1991 */
1992 revs->verbose_header = 1;
1993 revs->pretty_given = 1;
1994 get_commit_format(optarg, revs);
1995 } else if (!strcmp(arg, "--expand-tabs")) {
1996 revs->expand_tabs_in_log = 8;
1997 } else if (!strcmp(arg, "--no-expand-tabs")) {
1998 revs->expand_tabs_in_log = 0;
1999 } else if (skip_prefix(arg, "--expand-tabs=", &arg)) {
2000 int val;
2001 if (strtol_i(arg, 10, &val) < 0 || val < 0)
2002 die("'%s': not a non-negative integer", arg);
2003 revs->expand_tabs_in_log = val;
2004 } else if (!strcmp(arg, "--show-notes") || !strcmp(arg, "--notes")) {
2005 revs->show_notes = 1;
2006 revs->show_notes_given = 1;
2007 revs->notes_opt.use_default_notes = 1;
2008 } else if (!strcmp(arg, "--show-signature")) {
2009 revs->show_signature = 1;
2010 } else if (!strcmp(arg, "--no-show-signature")) {
2011 revs->show_signature = 0;
2012 } else if (!strcmp(arg, "--show-linear-break")) {
2013 revs->break_bar = " ..........";
2014 revs->track_linear = 1;
2015 revs->track_first_time = 1;
2016 } else if (skip_prefix(arg, "--show-linear-break=", &optarg)) {
2017 revs->break_bar = xstrdup(optarg);
2018 revs->track_linear = 1;
2019 revs->track_first_time = 1;
2020 } else if (skip_prefix(arg, "--show-notes=", &optarg) ||
2021 skip_prefix(arg, "--notes=", &optarg)) {
2022 struct strbuf buf = STRBUF_INIT;
2023 revs->show_notes = 1;
2024 revs->show_notes_given = 1;
2025 if (starts_with(arg, "--show-notes=") &&
2026 revs->notes_opt.use_default_notes < 0)
2027 revs->notes_opt.use_default_notes = 1;
2028 strbuf_addstr(&buf, optarg);
2029 expand_notes_ref(&buf);
2030 string_list_append(&revs->notes_opt.extra_notes_refs,
2031 strbuf_detach(&buf, NULL));
2032 } else if (!strcmp(arg, "--no-notes")) {
2033 revs->show_notes = 0;
2034 revs->show_notes_given = 1;
2035 revs->notes_opt.use_default_notes = -1;
2036 /* we have been strdup'ing ourselves, so trick
2037 * string_list into free()ing strings */
2038 revs->notes_opt.extra_notes_refs.strdup_strings = 1;
2039 string_list_clear(&revs->notes_opt.extra_notes_refs, 0);
2040 revs->notes_opt.extra_notes_refs.strdup_strings = 0;
2041 } else if (!strcmp(arg, "--standard-notes")) {
2042 revs->show_notes_given = 1;
2043 revs->notes_opt.use_default_notes = 1;
2044 } else if (!strcmp(arg, "--no-standard-notes")) {
2045 revs->notes_opt.use_default_notes = 0;
2046 } else if (!strcmp(arg, "--oneline")) {
2047 revs->verbose_header = 1;
2048 get_commit_format("oneline", revs);
2049 revs->pretty_given = 1;
2050 revs->abbrev_commit = 1;
2051 } else if (!strcmp(arg, "--graph")) {
2052 revs->topo_order = 1;
2053 revs->rewrite_parents = 1;
2054 revs->graph = graph_init(revs);
2055 } else if (!strcmp(arg, "--root")) {
2056 revs->show_root_diff = 1;
2057 } else if (!strcmp(arg, "--no-commit-id")) {
2058 revs->no_commit_id = 1;
2059 } else if (!strcmp(arg, "--always")) {
2060 revs->always_show_header = 1;
2061 } else if (!strcmp(arg, "--no-abbrev")) {
2062 revs->abbrev = 0;
2063 } else if (!strcmp(arg, "--abbrev")) {
2064 revs->abbrev = DEFAULT_ABBREV;
2065 } else if (skip_prefix(arg, "--abbrev=", &optarg)) {
2066 revs->abbrev = strtoul(optarg, NULL, 10);
2067 if (revs->abbrev < MINIMUM_ABBREV)
2068 revs->abbrev = MINIMUM_ABBREV;
2069 else if (revs->abbrev > hexsz)
2070 revs->abbrev = hexsz;
2071 } else if (!strcmp(arg, "--abbrev-commit")) {
2072 revs->abbrev_commit = 1;
2073 revs->abbrev_commit_given = 1;
2074 } else if (!strcmp(arg, "--no-abbrev-commit")) {
2075 revs->abbrev_commit = 0;
2076 } else if (!strcmp(arg, "--full-diff")) {
2077 revs->diff = 1;
2078 revs->full_diff = 1;
2079 } else if (!strcmp(arg, "--full-history")) {
2080 revs->simplify_history = 0;
2081 } else if (!strcmp(arg, "--relative-date")) {
2082 revs->date_mode.type = DATE_RELATIVE;
2083 revs->date_mode_explicit = 1;
2084 } else if ((argcount = parse_long_opt("date", argv, &optarg))) {
2085 parse_date_format(optarg, &revs->date_mode);
2086 revs->date_mode_explicit = 1;
2087 return argcount;
2088 } else if (!strcmp(arg, "--log-size")) {
2089 revs->show_log_size = 1;
2090 }
2091 /*
2092 * Grepping the commit log
2093 */
2094 else if ((argcount = parse_long_opt("author", argv, &optarg))) {
2095 add_header_grep(revs, GREP_HEADER_AUTHOR, optarg);
2096 return argcount;
2097 } else if ((argcount = parse_long_opt("committer", argv, &optarg))) {
2098 add_header_grep(revs, GREP_HEADER_COMMITTER, optarg);
2099 return argcount;
2100 } else if ((argcount = parse_long_opt("grep-reflog", argv, &optarg))) {
2101 add_header_grep(revs, GREP_HEADER_REFLOG, optarg);
2102 return argcount;
2103 } else if ((argcount = parse_long_opt("grep", argv, &optarg))) {
2104 add_message_grep(revs, optarg);
2105 return argcount;
2106 } else if (!strcmp(arg, "--grep-debug")) {
2107 revs->grep_filter.debug = 1;
2108 } else if (!strcmp(arg, "--basic-regexp")) {
2109 revs->grep_filter.pattern_type_option = GREP_PATTERN_TYPE_BRE;
2110 } else if (!strcmp(arg, "--extended-regexp") || !strcmp(arg, "-E")) {
2111 revs->grep_filter.pattern_type_option = GREP_PATTERN_TYPE_ERE;
2112 } else if (!strcmp(arg, "--regexp-ignore-case") || !strcmp(arg, "-i")) {
2113 revs->grep_filter.ignore_case = 1;
2114 revs->diffopt.pickaxe_opts |= DIFF_PICKAXE_IGNORE_CASE;
2115 } else if (!strcmp(arg, "--fixed-strings") || !strcmp(arg, "-F")) {
2116 revs->grep_filter.pattern_type_option = GREP_PATTERN_TYPE_FIXED;
2117 } else if (!strcmp(arg, "--perl-regexp") || !strcmp(arg, "-P")) {
2118 revs->grep_filter.pattern_type_option = GREP_PATTERN_TYPE_PCRE;
2119 } else if (!strcmp(arg, "--all-match")) {
2120 revs->grep_filter.all_match = 1;
2121 } else if (!strcmp(arg, "--invert-grep")) {
2122 revs->invert_grep = 1;
2123 } else if ((argcount = parse_long_opt("encoding", argv, &optarg))) {
2124 if (strcmp(optarg, "none"))
2125 git_log_output_encoding = xstrdup(optarg);
2126 else
2127 git_log_output_encoding = "";
2128 return argcount;
2129 } else if (!strcmp(arg, "--reverse")) {
2130 revs->reverse ^= 1;
2131 } else if (!strcmp(arg, "--children")) {
2132 revs->children.name = "children";
2133 revs->limited = 1;
2134 } else if (!strcmp(arg, "--ignore-missing")) {
2135 revs->ignore_missing = 1;
2136 } else if (!strcmp(arg, "--exclude-promisor-objects")) {
2137 if (fetch_if_missing)
2138 BUG("exclude_promisor_objects can only be used when fetch_if_missing is 0");
2139 revs->exclude_promisor_objects = 1;
2140 } else {
2141 int opts = diff_opt_parse(&revs->diffopt, argv, argc, revs->prefix);
2142 if (!opts)
2143 unkv[(*unkc)++] = arg;
2144 return opts;
2145 }
2146 if (revs->graph && revs->track_linear)
2147 die("--show-linear-break and --graph are incompatible");
2148
2149 return 1;
2150}
2151
2152void parse_revision_opt(struct rev_info *revs, struct parse_opt_ctx_t *ctx,
2153 const struct option *options,
2154 const char * const usagestr[])
2155{
2156 int n = handle_revision_opt(revs, ctx->argc, ctx->argv,
2157 &ctx->cpidx, ctx->out);
2158 if (n <= 0) {
2159 error("unknown option `%s'", ctx->argv[0]);
2160 usage_with_options(usagestr, options);
2161 }
2162 ctx->argv += n;
2163 ctx->argc -= n;
2164}
2165
2166static int for_each_bisect_ref(struct ref_store *refs, each_ref_fn fn,
2167 void *cb_data, const char *term)
2168{
2169 struct strbuf bisect_refs = STRBUF_INIT;
2170 int status;
2171 strbuf_addf(&bisect_refs, "refs/bisect/%s", term);
2172 status = refs_for_each_fullref_in(refs, bisect_refs.buf, fn, cb_data, 0);
2173 strbuf_release(&bisect_refs);
2174 return status;
2175}
2176
2177static int for_each_bad_bisect_ref(struct ref_store *refs, each_ref_fn fn, void *cb_data)
2178{
2179 return for_each_bisect_ref(refs, fn, cb_data, term_bad);
2180}
2181
2182static int for_each_good_bisect_ref(struct ref_store *refs, each_ref_fn fn, void *cb_data)
2183{
2184 return for_each_bisect_ref(refs, fn, cb_data, term_good);
2185}
2186
2187static int handle_revision_pseudo_opt(const char *submodule,
2188 struct rev_info *revs,
2189 int argc, const char **argv, int *flags)
2190{
2191 const char *arg = argv[0];
2192 const char *optarg;
2193 struct ref_store *refs;
2194 int argcount;
2195
2196 if (submodule) {
2197 /*
2198 * We need some something like get_submodule_worktrees()
2199 * before we can go through all worktrees of a submodule,
2200 * .e.g with adding all HEADs from --all, which is not
2201 * supported right now, so stick to single worktree.
2202 */
2203 if (!revs->single_worktree)
2204 BUG("--single-worktree cannot be used together with submodule");
2205 refs = get_submodule_ref_store(submodule);
2206 } else
2207 refs = get_main_ref_store(the_repository);
2208
2209 /*
2210 * NOTE!
2211 *
2212 * Commands like "git shortlog" will not accept the options below
2213 * unless parse_revision_opt queues them (as opposed to erroring
2214 * out).
2215 *
2216 * When implementing your new pseudo-option, remember to
2217 * register it in the list at the top of handle_revision_opt.
2218 */
2219 if (!strcmp(arg, "--all")) {
2220 handle_refs(refs, revs, *flags, refs_for_each_ref);
2221 handle_refs(refs, revs, *flags, refs_head_ref);
2222 if (!revs->single_worktree) {
2223 struct all_refs_cb cb;
2224
2225 init_all_refs_cb(&cb, revs, *flags);
2226 other_head_refs(handle_one_ref, &cb);
2227 }
2228 clear_ref_exclusion(&revs->ref_excludes);
2229 } else if (!strcmp(arg, "--branches")) {
2230 handle_refs(refs, revs, *flags, refs_for_each_branch_ref);
2231 clear_ref_exclusion(&revs->ref_excludes);
2232 } else if (!strcmp(arg, "--bisect")) {
2233 read_bisect_terms(&term_bad, &term_good);
2234 handle_refs(refs, revs, *flags, for_each_bad_bisect_ref);
2235 handle_refs(refs, revs, *flags ^ (UNINTERESTING | BOTTOM),
2236 for_each_good_bisect_ref);
2237 revs->bisect = 1;
2238 } else if (!strcmp(arg, "--tags")) {
2239 handle_refs(refs, revs, *flags, refs_for_each_tag_ref);
2240 clear_ref_exclusion(&revs->ref_excludes);
2241 } else if (!strcmp(arg, "--remotes")) {
2242 handle_refs(refs, revs, *flags, refs_for_each_remote_ref);
2243 clear_ref_exclusion(&revs->ref_excludes);
2244 } else if ((argcount = parse_long_opt("glob", argv, &optarg))) {
2245 struct all_refs_cb cb;
2246 init_all_refs_cb(&cb, revs, *flags);
2247 for_each_glob_ref(handle_one_ref, optarg, &cb);
2248 clear_ref_exclusion(&revs->ref_excludes);
2249 return argcount;
2250 } else if ((argcount = parse_long_opt("exclude", argv, &optarg))) {
2251 add_ref_exclusion(&revs->ref_excludes, optarg);
2252 return argcount;
2253 } else if (skip_prefix(arg, "--branches=", &optarg)) {
2254 struct all_refs_cb cb;
2255 init_all_refs_cb(&cb, revs, *flags);
2256 for_each_glob_ref_in(handle_one_ref, optarg, "refs/heads/", &cb);
2257 clear_ref_exclusion(&revs->ref_excludes);
2258 } else if (skip_prefix(arg, "--tags=", &optarg)) {
2259 struct all_refs_cb cb;
2260 init_all_refs_cb(&cb, revs, *flags);
2261 for_each_glob_ref_in(handle_one_ref, optarg, "refs/tags/", &cb);
2262 clear_ref_exclusion(&revs->ref_excludes);
2263 } else if (skip_prefix(arg, "--remotes=", &optarg)) {
2264 struct all_refs_cb cb;
2265 init_all_refs_cb(&cb, revs, *flags);
2266 for_each_glob_ref_in(handle_one_ref, optarg, "refs/remotes/", &cb);
2267 clear_ref_exclusion(&revs->ref_excludes);
2268 } else if (!strcmp(arg, "--reflog")) {
2269 add_reflogs_to_pending(revs, *flags);
2270 } else if (!strcmp(arg, "--indexed-objects")) {
2271 add_index_objects_to_pending(revs, *flags);
2272 } else if (!strcmp(arg, "--not")) {
2273 *flags ^= UNINTERESTING | BOTTOM;
2274 } else if (!strcmp(arg, "--no-walk")) {
2275 revs->no_walk = REVISION_WALK_NO_WALK_SORTED;
2276 } else if (skip_prefix(arg, "--no-walk=", &optarg)) {
2277 /*
2278 * Detached form ("--no-walk X" as opposed to "--no-walk=X")
2279 * not allowed, since the argument is optional.
2280 */
2281 if (!strcmp(optarg, "sorted"))
2282 revs->no_walk = REVISION_WALK_NO_WALK_SORTED;
2283 else if (!strcmp(optarg, "unsorted"))
2284 revs->no_walk = REVISION_WALK_NO_WALK_UNSORTED;
2285 else
2286 return error("invalid argument to --no-walk");
2287 } else if (!strcmp(arg, "--do-walk")) {
2288 revs->no_walk = 0;
2289 } else if (!strcmp(arg, "--single-worktree")) {
2290 revs->single_worktree = 1;
2291 } else {
2292 return 0;
2293 }
2294
2295 return 1;
2296}
2297
2298static void NORETURN diagnose_missing_default(const char *def)
2299{
2300 int flags;
2301 const char *refname;
2302
2303 refname = resolve_ref_unsafe(def, 0, NULL, &flags);
2304 if (!refname || !(flags & REF_ISSYMREF) || (flags & REF_ISBROKEN))
2305 die(_("your current branch appears to be broken"));
2306
2307 skip_prefix(refname, "refs/heads/", &refname);
2308 die(_("your current branch '%s' does not have any commits yet"),
2309 refname);
2310}
2311
2312/*
2313 * Parse revision information, filling in the "rev_info" structure,
2314 * and removing the used arguments from the argument list.
2315 *
2316 * Returns the number of arguments left that weren't recognized
2317 * (which are also moved to the head of the argument list)
2318 */
2319int setup_revisions(int argc, const char **argv, struct rev_info *revs, struct setup_revision_opt *opt)
2320{
2321 int i, flags, left, seen_dashdash, got_rev_arg = 0, revarg_opt;
2322 struct argv_array prune_data = ARGV_ARRAY_INIT;
2323 const char *submodule = NULL;
2324
2325 if (opt)
2326 submodule = opt->submodule;
2327
2328 /* First, search for "--" */
2329 if (opt && opt->assume_dashdash) {
2330 seen_dashdash = 1;
2331 } else {
2332 seen_dashdash = 0;
2333 for (i = 1; i < argc; i++) {
2334 const char *arg = argv[i];
2335 if (strcmp(arg, "--"))
2336 continue;
2337 argv[i] = NULL;
2338 argc = i;
2339 if (argv[i + 1])
2340 argv_array_pushv(&prune_data, argv + i + 1);
2341 seen_dashdash = 1;
2342 break;
2343 }
2344 }
2345
2346 /* Second, deal with arguments and options */
2347 flags = 0;
2348 revarg_opt = opt ? opt->revarg_opt : 0;
2349 if (seen_dashdash)
2350 revarg_opt |= REVARG_CANNOT_BE_FILENAME;
2351 for (left = i = 1; i < argc; i++) {
2352 const char *arg = argv[i];
2353 if (*arg == '-') {
2354 int opts;
2355
2356 opts = handle_revision_pseudo_opt(submodule,
2357 revs, argc - i, argv + i,
2358 &flags);
2359 if (opts > 0) {
2360 i += opts - 1;
2361 continue;
2362 }
2363
2364 if (!strcmp(arg, "--stdin")) {
2365 if (revs->disable_stdin) {
2366 argv[left++] = arg;
2367 continue;
2368 }
2369 if (revs->read_from_stdin++)
2370 die("--stdin given twice?");
2371 read_revisions_from_stdin(revs, &prune_data);
2372 continue;
2373 }
2374
2375 opts = handle_revision_opt(revs, argc - i, argv + i, &left, argv);
2376 if (opts > 0) {
2377 i += opts - 1;
2378 continue;
2379 }
2380 if (opts < 0)
2381 exit(128);
2382 continue;
2383 }
2384
2385
2386 if (handle_revision_arg(arg, revs, flags, revarg_opt)) {
2387 int j;
2388 if (seen_dashdash || *arg == '^')
2389 die("bad revision '%s'", arg);
2390
2391 /* If we didn't have a "--":
2392 * (1) all filenames must exist;
2393 * (2) all rev-args must not be interpretable
2394 * as a valid filename.
2395 * but the latter we have checked in the main loop.
2396 */
2397 for (j = i; j < argc; j++)
2398 verify_filename(revs->prefix, argv[j], j == i);
2399
2400 argv_array_pushv(&prune_data, argv + i);
2401 break;
2402 }
2403 else
2404 got_rev_arg = 1;
2405 }
2406
2407 if (prune_data.argc) {
2408 /*
2409 * If we need to introduce the magic "a lone ':' means no
2410 * pathspec whatsoever", here is the place to do so.
2411 *
2412 * if (prune_data.nr == 1 && !strcmp(prune_data[0], ":")) {
2413 * prune_data.nr = 0;
2414 * prune_data.alloc = 0;
2415 * free(prune_data.path);
2416 * prune_data.path = NULL;
2417 * } else {
2418 * terminate prune_data.alloc with NULL and
2419 * call init_pathspec() to set revs->prune_data here.
2420 * }
2421 */
2422 parse_pathspec(&revs->prune_data, 0, 0,
2423 revs->prefix, prune_data.argv);
2424 }
2425 argv_array_clear(&prune_data);
2426
2427 if (revs->def == NULL)
2428 revs->def = opt ? opt->def : NULL;
2429 if (opt && opt->tweak)
2430 opt->tweak(revs, opt);
2431 if (revs->show_merge)
2432 prepare_show_merge(revs);
2433 if (revs->def && !revs->pending.nr && !revs->rev_input_given && !got_rev_arg) {
2434 struct object_id oid;
2435 struct object *object;
2436 struct object_context oc;
2437 if (get_oid_with_context(revs->def, 0, &oid, &oc))
2438 diagnose_missing_default(revs->def);
2439 object = get_reference(revs, revs->def, &oid, 0);
2440 add_pending_object_with_mode(revs, object, revs->def, oc.mode);
2441 }
2442
2443 /* Did the user ask for any diff output? Run the diff! */
2444 if (revs->diffopt.output_format & ~DIFF_FORMAT_NO_OUTPUT)
2445 revs->diff = 1;
2446
2447 /* Pickaxe, diff-filter and rename following need diffs */
2448 if ((revs->diffopt.pickaxe_opts & DIFF_PICKAXE_KINDS_MASK) ||
2449 revs->diffopt.filter ||
2450 revs->diffopt.flags.follow_renames)
2451 revs->diff = 1;
2452
2453 if (revs->diffopt.objfind)
2454 revs->simplify_history = 0;
2455
2456 if (revs->topo_order)
2457 revs->limited = 1;
2458
2459 if (revs->prune_data.nr) {
2460 copy_pathspec(&revs->pruning.pathspec, &revs->prune_data);
2461 /* Can't prune commits with rename following: the paths change.. */
2462 if (!revs->diffopt.flags.follow_renames)
2463 revs->prune = 1;
2464 if (!revs->full_diff)
2465 copy_pathspec(&revs->diffopt.pathspec,
2466 &revs->prune_data);
2467 }
2468 if (revs->combine_merges)
2469 revs->ignore_merges = 0;
2470 revs->diffopt.abbrev = revs->abbrev;
2471
2472 if (revs->line_level_traverse) {
2473 revs->limited = 1;
2474 revs->topo_order = 1;
2475 }
2476
2477 diff_setup_done(&revs->diffopt);
2478
2479 grep_commit_pattern_type(GREP_PATTERN_TYPE_UNSPECIFIED,
2480 &revs->grep_filter);
2481 compile_grep_patterns(&revs->grep_filter);
2482
2483 if (revs->reverse && revs->reflog_info)
2484 die("cannot combine --reverse with --walk-reflogs");
2485 if (revs->reflog_info && revs->limited)
2486 die("cannot combine --walk-reflogs with history-limiting options");
2487 if (revs->rewrite_parents && revs->children.name)
2488 die("cannot combine --parents and --children");
2489
2490 /*
2491 * Limitations on the graph functionality
2492 */
2493 if (revs->reverse && revs->graph)
2494 die("cannot combine --reverse with --graph");
2495
2496 if (revs->reflog_info && revs->graph)
2497 die("cannot combine --walk-reflogs with --graph");
2498 if (revs->no_walk && revs->graph)
2499 die("cannot combine --no-walk with --graph");
2500 if (!revs->reflog_info && revs->grep_filter.use_reflog_filter)
2501 die("cannot use --grep-reflog without --walk-reflogs");
2502
2503 if (revs->first_parent_only && revs->bisect)
2504 die(_("--first-parent is incompatible with --bisect"));
2505
2506 if (revs->expand_tabs_in_log < 0)
2507 revs->expand_tabs_in_log = revs->expand_tabs_in_log_default;
2508
2509 return left;
2510}
2511
2512static void add_child(struct rev_info *revs, struct commit *parent, struct commit *child)
2513{
2514 struct commit_list *l = xcalloc(1, sizeof(*l));
2515
2516 l->item = child;
2517 l->next = add_decoration(&revs->children, &parent->object, l);
2518}
2519
2520static int remove_duplicate_parents(struct rev_info *revs, struct commit *commit)
2521{
2522 struct treesame_state *ts = lookup_decoration(&revs->treesame, &commit->object);
2523 struct commit_list **pp, *p;
2524 int surviving_parents;
2525
2526 /* Examine existing parents while marking ones we have seen... */
2527 pp = &commit->parents;
2528 surviving_parents = 0;
2529 while ((p = *pp) != NULL) {
2530 struct commit *parent = p->item;
2531 if (parent->object.flags & TMP_MARK) {
2532 *pp = p->next;
2533 if (ts)
2534 compact_treesame(revs, commit, surviving_parents);
2535 continue;
2536 }
2537 parent->object.flags |= TMP_MARK;
2538 surviving_parents++;
2539 pp = &p->next;
2540 }
2541 /* clear the temporary mark */
2542 for (p = commit->parents; p; p = p->next) {
2543 p->item->object.flags &= ~TMP_MARK;
2544 }
2545 /* no update_treesame() - removing duplicates can't affect TREESAME */
2546 return surviving_parents;
2547}
2548
2549struct merge_simplify_state {
2550 struct commit *simplified;
2551};
2552
2553static struct merge_simplify_state *locate_simplify_state(struct rev_info *revs, struct commit *commit)
2554{
2555 struct merge_simplify_state *st;
2556
2557 st = lookup_decoration(&revs->merge_simplification, &commit->object);
2558 if (!st) {
2559 st = xcalloc(1, sizeof(*st));
2560 add_decoration(&revs->merge_simplification, &commit->object, st);
2561 }
2562 return st;
2563}
2564
2565static int mark_redundant_parents(struct rev_info *revs, struct commit *commit)
2566{
2567 struct commit_list *h = reduce_heads(commit->parents);
2568 int i = 0, marked = 0;
2569 struct commit_list *po, *pn;
2570
2571 /* Want these for sanity-checking only */
2572 int orig_cnt = commit_list_count(commit->parents);
2573 int cnt = commit_list_count(h);
2574
2575 /*
2576 * Not ready to remove items yet, just mark them for now, based
2577 * on the output of reduce_heads(). reduce_heads outputs the reduced
2578 * set in its original order, so this isn't too hard.
2579 */
2580 po = commit->parents;
2581 pn = h;
2582 while (po) {
2583 if (pn && po->item == pn->item) {
2584 pn = pn->next;
2585 i++;
2586 } else {
2587 po->item->object.flags |= TMP_MARK;
2588 marked++;
2589 }
2590 po=po->next;
2591 }
2592
2593 if (i != cnt || cnt+marked != orig_cnt)
2594 die("mark_redundant_parents %d %d %d %d", orig_cnt, cnt, i, marked);
2595
2596 free_commit_list(h);
2597
2598 return marked;
2599}
2600
2601static int mark_treesame_root_parents(struct rev_info *revs, struct commit *commit)
2602{
2603 struct commit_list *p;
2604 int marked = 0;
2605
2606 for (p = commit->parents; p; p = p->next) {
2607 struct commit *parent = p->item;
2608 if (!parent->parents && (parent->object.flags & TREESAME)) {
2609 parent->object.flags |= TMP_MARK;
2610 marked++;
2611 }
2612 }
2613
2614 return marked;
2615}
2616
2617/*
2618 * Awkward naming - this means one parent we are TREESAME to.
2619 * cf mark_treesame_root_parents: root parents that are TREESAME (to an
2620 * empty tree). Better name suggestions?
2621 */
2622static int leave_one_treesame_to_parent(struct rev_info *revs, struct commit *commit)
2623{
2624 struct treesame_state *ts = lookup_decoration(&revs->treesame, &commit->object);
2625 struct commit *unmarked = NULL, *marked = NULL;
2626 struct commit_list *p;
2627 unsigned n;
2628
2629 for (p = commit->parents, n = 0; p; p = p->next, n++) {
2630 if (ts->treesame[n]) {
2631 if (p->item->object.flags & TMP_MARK) {
2632 if (!marked)
2633 marked = p->item;
2634 } else {
2635 if (!unmarked) {
2636 unmarked = p->item;
2637 break;
2638 }
2639 }
2640 }
2641 }
2642
2643 /*
2644 * If we are TREESAME to a marked-for-deletion parent, but not to any
2645 * unmarked parents, unmark the first TREESAME parent. This is the
2646 * parent that the default simplify_history==1 scan would have followed,
2647 * and it doesn't make sense to omit that path when asking for a
2648 * simplified full history. Retaining it improves the chances of
2649 * understanding odd missed merges that took an old version of a file.
2650 *
2651 * Example:
2652 *
2653 * I--------*X A modified the file, but mainline merge X used
2654 * \ / "-s ours", so took the version from I. X is
2655 * `-*A--' TREESAME to I and !TREESAME to A.
2656 *
2657 * Default log from X would produce "I". Without this check,
2658 * --full-history --simplify-merges would produce "I-A-X", showing
2659 * the merge commit X and that it changed A, but not making clear that
2660 * it had just taken the I version. With this check, the topology above
2661 * is retained.
2662 *
2663 * Note that it is possible that the simplification chooses a different
2664 * TREESAME parent from the default, in which case this test doesn't
2665 * activate, and we _do_ drop the default parent. Example:
2666 *
2667 * I------X A modified the file, but it was reverted in B,
2668 * \ / meaning mainline merge X is TREESAME to both
2669 * *A-*B parents.
2670 *
2671 * Default log would produce "I" by following the first parent;
2672 * --full-history --simplify-merges will produce "I-A-B". But this is a
2673 * reasonable result - it presents a logical full history leading from
2674 * I to X, and X is not an important merge.
2675 */
2676 if (!unmarked && marked) {
2677 marked->object.flags &= ~TMP_MARK;
2678 return 1;
2679 }
2680
2681 return 0;
2682}
2683
2684static int remove_marked_parents(struct rev_info *revs, struct commit *commit)
2685{
2686 struct commit_list **pp, *p;
2687 int nth_parent, removed = 0;
2688
2689 pp = &commit->parents;
2690 nth_parent = 0;
2691 while ((p = *pp) != NULL) {
2692 struct commit *parent = p->item;
2693 if (parent->object.flags & TMP_MARK) {
2694 parent->object.flags &= ~TMP_MARK;
2695 *pp = p->next;
2696 free(p);
2697 removed++;
2698 compact_treesame(revs, commit, nth_parent);
2699 continue;
2700 }
2701 pp = &p->next;
2702 nth_parent++;
2703 }
2704
2705 /* Removing parents can only increase TREESAMEness */
2706 if (removed && !(commit->object.flags & TREESAME))
2707 update_treesame(revs, commit);
2708
2709 return nth_parent;
2710}
2711
2712static struct commit_list **simplify_one(struct rev_info *revs, struct commit *commit, struct commit_list **tail)
2713{
2714 struct commit_list *p;
2715 struct commit *parent;
2716 struct merge_simplify_state *st, *pst;
2717 int cnt;
2718
2719 st = locate_simplify_state(revs, commit);
2720
2721 /*
2722 * Have we handled this one?
2723 */
2724 if (st->simplified)
2725 return tail;
2726
2727 /*
2728 * An UNINTERESTING commit simplifies to itself, so does a
2729 * root commit. We do not rewrite parents of such commit
2730 * anyway.
2731 */
2732 if ((commit->object.flags & UNINTERESTING) || !commit->parents) {
2733 st->simplified = commit;
2734 return tail;
2735 }
2736
2737 /*
2738 * Do we know what commit all of our parents that matter
2739 * should be rewritten to? Otherwise we are not ready to
2740 * rewrite this one yet.
2741 */
2742 for (cnt = 0, p = commit->parents; p; p = p->next) {
2743 pst = locate_simplify_state(revs, p->item);
2744 if (!pst->simplified) {
2745 tail = &commit_list_insert(p->item, tail)->next;
2746 cnt++;
2747 }
2748 if (revs->first_parent_only)
2749 break;
2750 }
2751 if (cnt) {
2752 tail = &commit_list_insert(commit, tail)->next;
2753 return tail;
2754 }
2755
2756 /*
2757 * Rewrite our list of parents. Note that this cannot
2758 * affect our TREESAME flags in any way - a commit is
2759 * always TREESAME to its simplification.
2760 */
2761 for (p = commit->parents; p; p = p->next) {
2762 pst = locate_simplify_state(revs, p->item);
2763 p->item = pst->simplified;
2764 if (revs->first_parent_only)
2765 break;
2766 }
2767
2768 if (revs->first_parent_only)
2769 cnt = 1;
2770 else
2771 cnt = remove_duplicate_parents(revs, commit);
2772
2773 /*
2774 * It is possible that we are a merge and one side branch
2775 * does not have any commit that touches the given paths;
2776 * in such a case, the immediate parent from that branch
2777 * will be rewritten to be the merge base.
2778 *
2779 * o----X X: the commit we are looking at;
2780 * / / o: a commit that touches the paths;
2781 * ---o----'
2782 *
2783 * Further, a merge of an independent branch that doesn't
2784 * touch the path will reduce to a treesame root parent:
2785 *
2786 * ----o----X X: the commit we are looking at;
2787 * / o: a commit that touches the paths;
2788 * r r: a root commit not touching the paths
2789 *
2790 * Detect and simplify both cases.
2791 */
2792 if (1 < cnt) {
2793 int marked = mark_redundant_parents(revs, commit);
2794 marked += mark_treesame_root_parents(revs, commit);
2795 if (marked)
2796 marked -= leave_one_treesame_to_parent(revs, commit);
2797 if (marked)
2798 cnt = remove_marked_parents(revs, commit);
2799 }
2800
2801 /*
2802 * A commit simplifies to itself if it is a root, if it is
2803 * UNINTERESTING, if it touches the given paths, or if it is a
2804 * merge and its parents don't simplify to one relevant commit
2805 * (the first two cases are already handled at the beginning of
2806 * this function).
2807 *
2808 * Otherwise, it simplifies to what its sole relevant parent
2809 * simplifies to.
2810 */
2811 if (!cnt ||
2812 (commit->object.flags & UNINTERESTING) ||
2813 !(commit->object.flags & TREESAME) ||
2814 (parent = one_relevant_parent(revs, commit->parents)) == NULL)
2815 st->simplified = commit;
2816 else {
2817 pst = locate_simplify_state(revs, parent);
2818 st->simplified = pst->simplified;
2819 }
2820 return tail;
2821}
2822
2823static void simplify_merges(struct rev_info *revs)
2824{
2825 struct commit_list *list, *next;
2826 struct commit_list *yet_to_do, **tail;
2827 struct commit *commit;
2828
2829 if (!revs->prune)
2830 return;
2831
2832 /* feed the list reversed */
2833 yet_to_do = NULL;
2834 for (list = revs->commits; list; list = next) {
2835 commit = list->item;
2836 next = list->next;
2837 /*
2838 * Do not free(list) here yet; the original list
2839 * is used later in this function.
2840 */
2841 commit_list_insert(commit, &yet_to_do);
2842 }
2843 while (yet_to_do) {
2844 list = yet_to_do;
2845 yet_to_do = NULL;
2846 tail = &yet_to_do;
2847 while (list) {
2848 commit = pop_commit(&list);
2849 tail = simplify_one(revs, commit, tail);
2850 }
2851 }
2852
2853 /* clean up the result, removing the simplified ones */
2854 list = revs->commits;
2855 revs->commits = NULL;
2856 tail = &revs->commits;
2857 while (list) {
2858 struct merge_simplify_state *st;
2859
2860 commit = pop_commit(&list);
2861 st = locate_simplify_state(revs, commit);
2862 if (st->simplified == commit)
2863 tail = &commit_list_insert(commit, tail)->next;
2864 }
2865}
2866
2867static void set_children(struct rev_info *revs)
2868{
2869 struct commit_list *l;
2870 for (l = revs->commits; l; l = l->next) {
2871 struct commit *commit = l->item;
2872 struct commit_list *p;
2873
2874 for (p = commit->parents; p; p = p->next)
2875 add_child(revs, p->item, commit);
2876 }
2877}
2878
2879void reset_revision_walk(void)
2880{
2881 clear_object_flags(SEEN | ADDED | SHOWN);
2882}
2883
2884static int mark_uninteresting(const struct object_id *oid,
2885 struct packed_git *pack,
2886 uint32_t pos,
2887 void *unused)
2888{
2889 struct object *o = parse_object(the_repository, oid);
2890 o->flags |= UNINTERESTING | SEEN;
2891 return 0;
2892}
2893
2894int prepare_revision_walk(struct rev_info *revs)
2895{
2896 int i;
2897 struct object_array old_pending;
2898 struct commit_list **next = &revs->commits;
2899
2900 memcpy(&old_pending, &revs->pending, sizeof(old_pending));
2901 revs->pending.nr = 0;
2902 revs->pending.alloc = 0;
2903 revs->pending.objects = NULL;
2904 for (i = 0; i < old_pending.nr; i++) {
2905 struct object_array_entry *e = old_pending.objects + i;
2906 struct commit *commit = handle_commit(revs, e);
2907 if (commit) {
2908 if (!(commit->object.flags & SEEN)) {
2909 commit->object.flags |= SEEN;
2910 next = commit_list_append(commit, next);
2911 }
2912 }
2913 }
2914 object_array_clear(&old_pending);
2915
2916 /* Signal whether we need per-parent treesame decoration */
2917 if (revs->simplify_merges ||
2918 (revs->limited && limiting_can_increase_treesame(revs)))
2919 revs->treesame.name = "treesame";
2920
2921 if (revs->exclude_promisor_objects) {
2922 for_each_packed_object(mark_uninteresting, NULL,
2923 FOR_EACH_OBJECT_PROMISOR_ONLY);
2924 }
2925
2926 if (revs->no_walk != REVISION_WALK_NO_WALK_UNSORTED)
2927 commit_list_sort_by_date(&revs->commits);
2928 if (revs->no_walk)
2929 return 0;
2930 if (revs->limited)
2931 if (limit_list(revs) < 0)
2932 return -1;
2933 if (revs->topo_order)
2934 sort_in_topological_order(&revs->commits, revs->sort_order);
2935 if (revs->line_level_traverse)
2936 line_log_filter(revs);
2937 if (revs->simplify_merges)
2938 simplify_merges(revs);
2939 if (revs->children.name)
2940 set_children(revs);
2941 return 0;
2942}
2943
2944static enum rewrite_result rewrite_one(struct rev_info *revs, struct commit **pp)
2945{
2946 struct commit_list *cache = NULL;
2947
2948 for (;;) {
2949 struct commit *p = *pp;
2950 if (!revs->limited)
2951 if (add_parents_to_list(revs, p, &revs->commits, &cache) < 0)
2952 return rewrite_one_error;
2953 if (p->object.flags & UNINTERESTING)
2954 return rewrite_one_ok;
2955 if (!(p->object.flags & TREESAME))
2956 return rewrite_one_ok;
2957 if (!p->parents)
2958 return rewrite_one_noparents;
2959 if ((p = one_relevant_parent(revs, p->parents)) == NULL)
2960 return rewrite_one_ok;
2961 *pp = p;
2962 }
2963}
2964
2965int rewrite_parents(struct rev_info *revs, struct commit *commit,
2966 rewrite_parent_fn_t rewrite_parent)
2967{
2968 struct commit_list **pp = &commit->parents;
2969 while (*pp) {
2970 struct commit_list *parent = *pp;
2971 switch (rewrite_parent(revs, &parent->item)) {
2972 case rewrite_one_ok:
2973 break;
2974 case rewrite_one_noparents:
2975 *pp = parent->next;
2976 continue;
2977 case rewrite_one_error:
2978 return -1;
2979 }
2980 pp = &parent->next;
2981 }
2982 remove_duplicate_parents(revs, commit);
2983 return 0;
2984}
2985
2986static int commit_rewrite_person(struct strbuf *buf, const char *what, struct string_list *mailmap)
2987{
2988 char *person, *endp;
2989 size_t len, namelen, maillen;
2990 const char *name;
2991 const char *mail;
2992 struct ident_split ident;
2993
2994 person = strstr(buf->buf, what);
2995 if (!person)
2996 return 0;
2997
2998 person += strlen(what);
2999 endp = strchr(person, '\n');
3000 if (!endp)
3001 return 0;
3002
3003 len = endp - person;
3004
3005 if (split_ident_line(&ident, person, len))
3006 return 0;
3007
3008 mail = ident.mail_begin;
3009 maillen = ident.mail_end - ident.mail_begin;
3010 name = ident.name_begin;
3011 namelen = ident.name_end - ident.name_begin;
3012
3013 if (map_user(mailmap, &mail, &maillen, &name, &namelen)) {
3014 struct strbuf namemail = STRBUF_INIT;
3015
3016 strbuf_addf(&namemail, "%.*s <%.*s>",
3017 (int)namelen, name, (int)maillen, mail);
3018
3019 strbuf_splice(buf, ident.name_begin - buf->buf,
3020 ident.mail_end - ident.name_begin + 1,
3021 namemail.buf, namemail.len);
3022
3023 strbuf_release(&namemail);
3024
3025 return 1;
3026 }
3027
3028 return 0;
3029}
3030
3031static int commit_match(struct commit *commit, struct rev_info *opt)
3032{
3033 int retval;
3034 const char *encoding;
3035 const char *message;
3036 struct strbuf buf = STRBUF_INIT;
3037
3038 if (!opt->grep_filter.pattern_list && !opt->grep_filter.header_list)
3039 return 1;
3040
3041 /* Prepend "fake" headers as needed */
3042 if (opt->grep_filter.use_reflog_filter) {
3043 strbuf_addstr(&buf, "reflog ");
3044 get_reflog_message(&buf, opt->reflog_info);
3045 strbuf_addch(&buf, '\n');
3046 }
3047
3048 /*
3049 * We grep in the user's output encoding, under the assumption that it
3050 * is the encoding they are most likely to write their grep pattern
3051 * for. In addition, it means we will match the "notes" encoding below,
3052 * so we will not end up with a buffer that has two different encodings
3053 * in it.
3054 */
3055 encoding = get_log_output_encoding();
3056 message = logmsg_reencode(commit, NULL, encoding);
3057
3058 /* Copy the commit to temporary if we are using "fake" headers */
3059 if (buf.len)
3060 strbuf_addstr(&buf, message);
3061
3062 if (opt->grep_filter.header_list && opt->mailmap) {
3063 if (!buf.len)
3064 strbuf_addstr(&buf, message);
3065
3066 commit_rewrite_person(&buf, "\nauthor ", opt->mailmap);
3067 commit_rewrite_person(&buf, "\ncommitter ", opt->mailmap);
3068 }
3069
3070 /* Append "fake" message parts as needed */
3071 if (opt->show_notes) {
3072 if (!buf.len)
3073 strbuf_addstr(&buf, message);
3074 format_display_notes(&commit->object.oid, &buf, encoding, 1);
3075 }
3076
3077 /*
3078 * Find either in the original commit message, or in the temporary.
3079 * Note that we cast away the constness of "message" here. It is
3080 * const because it may come from the cached commit buffer. That's OK,
3081 * because we know that it is modifiable heap memory, and that while
3082 * grep_buffer may modify it for speed, it will restore any
3083 * changes before returning.
3084 */
3085 if (buf.len)
3086 retval = grep_buffer(&opt->grep_filter, buf.buf, buf.len);
3087 else
3088 retval = grep_buffer(&opt->grep_filter,
3089 (char *)message, strlen(message));
3090 strbuf_release(&buf);
3091 unuse_commit_buffer(commit, message);
3092 return opt->invert_grep ? !retval : retval;
3093}
3094
3095static inline int want_ancestry(const struct rev_info *revs)
3096{
3097 return (revs->rewrite_parents || revs->children.name);
3098}
3099
3100/*
3101 * Return a timestamp to be used for --since/--until comparisons for this
3102 * commit, based on the revision options.
3103 */
3104static timestamp_t comparison_date(const struct rev_info *revs,
3105 struct commit *commit)
3106{
3107 return revs->reflog_info ?
3108 get_reflog_timestamp(revs->reflog_info) :
3109 commit->date;
3110}
3111
3112enum commit_action get_commit_action(struct rev_info *revs, struct commit *commit)
3113{
3114 if (commit->object.flags & SHOWN)
3115 return commit_ignore;
3116 if (revs->unpacked && has_object_pack(&commit->object.oid))
3117 return commit_ignore;
3118 if (commit->object.flags & UNINTERESTING)
3119 return commit_ignore;
3120 if (revs->min_age != -1 &&
3121 comparison_date(revs, commit) > revs->min_age)
3122 return commit_ignore;
3123 if (revs->min_parents || (revs->max_parents >= 0)) {
3124 int n = commit_list_count(commit->parents);
3125 if ((n < revs->min_parents) ||
3126 ((revs->max_parents >= 0) && (n > revs->max_parents)))
3127 return commit_ignore;
3128 }
3129 if (!commit_match(commit, revs))
3130 return commit_ignore;
3131 if (revs->prune && revs->dense) {
3132 /* Commit without changes? */
3133 if (commit->object.flags & TREESAME) {
3134 int n;
3135 struct commit_list *p;
3136 /* drop merges unless we want parenthood */
3137 if (!want_ancestry(revs))
3138 return commit_ignore;
3139 /*
3140 * If we want ancestry, then need to keep any merges
3141 * between relevant commits to tie together topology.
3142 * For consistency with TREESAME and simplification
3143 * use "relevant" here rather than just INTERESTING,
3144 * to treat bottom commit(s) as part of the topology.
3145 */
3146 for (n = 0, p = commit->parents; p; p = p->next)
3147 if (relevant_commit(p->item))
3148 if (++n >= 2)
3149 return commit_show;
3150 return commit_ignore;
3151 }
3152 }
3153 return commit_show;
3154}
3155
3156define_commit_slab(saved_parents, struct commit_list *);
3157
3158#define EMPTY_PARENT_LIST ((struct commit_list *)-1)
3159
3160/*
3161 * You may only call save_parents() once per commit (this is checked
3162 * for non-root commits).
3163 */
3164static void save_parents(struct rev_info *revs, struct commit *commit)
3165{
3166 struct commit_list **pp;
3167
3168 if (!revs->saved_parents_slab) {
3169 revs->saved_parents_slab = xmalloc(sizeof(struct saved_parents));
3170 init_saved_parents(revs->saved_parents_slab);
3171 }
3172
3173 pp = saved_parents_at(revs->saved_parents_slab, commit);
3174
3175 /*
3176 * When walking with reflogs, we may visit the same commit
3177 * several times: once for each appearance in the reflog.
3178 *
3179 * In this case, save_parents() will be called multiple times.
3180 * We want to keep only the first set of parents. We need to
3181 * store a sentinel value for an empty (i.e., NULL) parent
3182 * list to distinguish it from a not-yet-saved list, however.
3183 */
3184 if (*pp)
3185 return;
3186 if (commit->parents)
3187 *pp = copy_commit_list(commit->parents);
3188 else
3189 *pp = EMPTY_PARENT_LIST;
3190}
3191
3192static void free_saved_parents(struct rev_info *revs)
3193{
3194 if (revs->saved_parents_slab)
3195 clear_saved_parents(revs->saved_parents_slab);
3196}
3197
3198struct commit_list *get_saved_parents(struct rev_info *revs, const struct commit *commit)
3199{
3200 struct commit_list *parents;
3201
3202 if (!revs->saved_parents_slab)
3203 return commit->parents;
3204
3205 parents = *saved_parents_at(revs->saved_parents_slab, commit);
3206 if (parents == EMPTY_PARENT_LIST)
3207 return NULL;
3208 return parents;
3209}
3210
3211enum commit_action simplify_commit(struct rev_info *revs, struct commit *commit)
3212{
3213 enum commit_action action = get_commit_action(revs, commit);
3214
3215 if (action == commit_show &&
3216 revs->prune && revs->dense && want_ancestry(revs)) {
3217 /*
3218 * --full-diff on simplified parents is no good: it
3219 * will show spurious changes from the commits that
3220 * were elided. So we save the parents on the side
3221 * when --full-diff is in effect.
3222 */
3223 if (revs->full_diff)
3224 save_parents(revs, commit);
3225 if (rewrite_parents(revs, commit, rewrite_one) < 0)
3226 return commit_error;
3227 }
3228 return action;
3229}
3230
3231static void track_linear(struct rev_info *revs, struct commit *commit)
3232{
3233 if (revs->track_first_time) {
3234 revs->linear = 1;
3235 revs->track_first_time = 0;
3236 } else {
3237 struct commit_list *p;
3238 for (p = revs->previous_parents; p; p = p->next)
3239 if (p->item == NULL || /* first commit */
3240 !oidcmp(&p->item->object.oid, &commit->object.oid))
3241 break;
3242 revs->linear = p != NULL;
3243 }
3244 if (revs->reverse) {
3245 if (revs->linear)
3246 commit->object.flags |= TRACK_LINEAR;
3247 }
3248 free_commit_list(revs->previous_parents);
3249 revs->previous_parents = copy_commit_list(commit->parents);
3250}
3251
3252static struct commit *get_revision_1(struct rev_info *revs)
3253{
3254 while (1) {
3255 struct commit *commit;
3256
3257 if (revs->reflog_info)
3258 commit = next_reflog_entry(revs->reflog_info);
3259 else
3260 commit = pop_commit(&revs->commits);
3261
3262 if (!commit)
3263 return NULL;
3264
3265 if (revs->reflog_info)
3266 commit->object.flags &= ~(ADDED | SEEN | SHOWN);
3267
3268 /*
3269 * If we haven't done the list limiting, we need to look at
3270 * the parents here. We also need to do the date-based limiting
3271 * that we'd otherwise have done in limit_list().
3272 */
3273 if (!revs->limited) {
3274 if (revs->max_age != -1 &&
3275 comparison_date(revs, commit) < revs->max_age)
3276 continue;
3277
3278 if (revs->reflog_info)
3279 try_to_simplify_commit(revs, commit);
3280 else if (add_parents_to_list(revs, commit, &revs->commits, NULL) < 0) {
3281 if (!revs->ignore_missing_links)
3282 die("Failed to traverse parents of commit %s",
3283 oid_to_hex(&commit->object.oid));
3284 }
3285 }
3286
3287 switch (simplify_commit(revs, commit)) {
3288 case commit_ignore:
3289 continue;
3290 case commit_error:
3291 die("Failed to simplify parents of commit %s",
3292 oid_to_hex(&commit->object.oid));
3293 default:
3294 if (revs->track_linear)
3295 track_linear(revs, commit);
3296 return commit;
3297 }
3298 }
3299}
3300
3301/*
3302 * Return true for entries that have not yet been shown. (This is an
3303 * object_array_each_func_t.)
3304 */
3305static int entry_unshown(struct object_array_entry *entry, void *cb_data_unused)
3306{
3307 return !(entry->item->flags & SHOWN);
3308}
3309
3310/*
3311 * If array is on the verge of a realloc, garbage-collect any entries
3312 * that have already been shown to try to free up some space.
3313 */
3314static void gc_boundary(struct object_array *array)
3315{
3316 if (array->nr == array->alloc)
3317 object_array_filter(array, entry_unshown, NULL);
3318}
3319
3320static void create_boundary_commit_list(struct rev_info *revs)
3321{
3322 unsigned i;
3323 struct commit *c;
3324 struct object_array *array = &revs->boundary_commits;
3325 struct object_array_entry *objects = array->objects;
3326
3327 /*
3328 * If revs->commits is non-NULL at this point, an error occurred in
3329 * get_revision_1(). Ignore the error and continue printing the
3330 * boundary commits anyway. (This is what the code has always
3331 * done.)
3332 */
3333 if (revs->commits) {
3334 free_commit_list(revs->commits);
3335 revs->commits = NULL;
3336 }
3337
3338 /*
3339 * Put all of the actual boundary commits from revs->boundary_commits
3340 * into revs->commits
3341 */
3342 for (i = 0; i < array->nr; i++) {
3343 c = (struct commit *)(objects[i].item);
3344 if (!c)
3345 continue;
3346 if (!(c->object.flags & CHILD_SHOWN))
3347 continue;
3348 if (c->object.flags & (SHOWN | BOUNDARY))
3349 continue;
3350 c->object.flags |= BOUNDARY;
3351 commit_list_insert(c, &revs->commits);
3352 }
3353
3354 /*
3355 * If revs->topo_order is set, sort the boundary commits
3356 * in topological order
3357 */
3358 sort_in_topological_order(&revs->commits, revs->sort_order);
3359}
3360
3361static struct commit *get_revision_internal(struct rev_info *revs)
3362{
3363 struct commit *c = NULL;
3364 struct commit_list *l;
3365
3366 if (revs->boundary == 2) {
3367 /*
3368 * All of the normal commits have already been returned,
3369 * and we are now returning boundary commits.
3370 * create_boundary_commit_list() has populated
3371 * revs->commits with the remaining commits to return.
3372 */
3373 c = pop_commit(&revs->commits);
3374 if (c)
3375 c->object.flags |= SHOWN;
3376 return c;
3377 }
3378
3379 /*
3380 * If our max_count counter has reached zero, then we are done. We
3381 * don't simply return NULL because we still might need to show
3382 * boundary commits. But we want to avoid calling get_revision_1, which
3383 * might do a considerable amount of work finding the next commit only
3384 * for us to throw it away.
3385 *
3386 * If it is non-zero, then either we don't have a max_count at all
3387 * (-1), or it is still counting, in which case we decrement.
3388 */
3389 if (revs->max_count) {
3390 c = get_revision_1(revs);
3391 if (c) {
3392 while (revs->skip_count > 0) {
3393 revs->skip_count--;
3394 c = get_revision_1(revs);
3395 if (!c)
3396 break;
3397 }
3398 }
3399
3400 if (revs->max_count > 0)
3401 revs->max_count--;
3402 }
3403
3404 if (c)
3405 c->object.flags |= SHOWN;
3406
3407 if (!revs->boundary)
3408 return c;
3409
3410 if (!c) {
3411 /*
3412 * get_revision_1() runs out the commits, and
3413 * we are done computing the boundaries.
3414 * switch to boundary commits output mode.
3415 */
3416 revs->boundary = 2;
3417
3418 /*
3419 * Update revs->commits to contain the list of
3420 * boundary commits.
3421 */
3422 create_boundary_commit_list(revs);
3423
3424 return get_revision_internal(revs);
3425 }
3426
3427 /*
3428 * boundary commits are the commits that are parents of the
3429 * ones we got from get_revision_1() but they themselves are
3430 * not returned from get_revision_1(). Before returning
3431 * 'c', we need to mark its parents that they could be boundaries.
3432 */
3433
3434 for (l = c->parents; l; l = l->next) {
3435 struct object *p;
3436 p = &(l->item->object);
3437 if (p->flags & (CHILD_SHOWN | SHOWN))
3438 continue;
3439 p->flags |= CHILD_SHOWN;
3440 gc_boundary(&revs->boundary_commits);
3441 add_object_array(p, NULL, &revs->boundary_commits);
3442 }
3443
3444 return c;
3445}
3446
3447struct commit *get_revision(struct rev_info *revs)
3448{
3449 struct commit *c;
3450 struct commit_list *reversed;
3451
3452 if (revs->reverse) {
3453 reversed = NULL;
3454 while ((c = get_revision_internal(revs)))
3455 commit_list_insert(c, &reversed);
3456 revs->commits = reversed;
3457 revs->reverse = 0;
3458 revs->reverse_output_stage = 1;
3459 }
3460
3461 if (revs->reverse_output_stage) {
3462 c = pop_commit(&revs->commits);
3463 if (revs->track_linear)
3464 revs->linear = !!(c && c->object.flags & TRACK_LINEAR);
3465 return c;
3466 }
3467
3468 c = get_revision_internal(revs);
3469 if (c && revs->graph)
3470 graph_update(revs->graph, c);
3471 if (!c) {
3472 free_saved_parents(revs);
3473 if (revs->previous_parents) {
3474 free_commit_list(revs->previous_parents);
3475 revs->previous_parents = NULL;
3476 }
3477 }
3478 return c;
3479}
3480
3481char *get_revision_mark(const struct rev_info *revs, const struct commit *commit)
3482{
3483 if (commit->object.flags & BOUNDARY)
3484 return "-";
3485 else if (commit->object.flags & UNINTERESTING)
3486 return "^";
3487 else if (commit->object.flags & PATCHSAME)
3488 return "=";
3489 else if (!revs || revs->left_right) {
3490 if (commit->object.flags & SYMMETRIC_LEFT)
3491 return "<";
3492 else
3493 return ">";
3494 } else if (revs->graph)
3495 return "*";
3496 else if (revs->cherry_mark)
3497 return "+";
3498 return "";
3499}
3500
3501void put_revision_mark(const struct rev_info *revs, const struct commit *commit)
3502{
3503 char *mark = get_revision_mark(revs, commit);
3504 if (!strlen(mark))
3505 return;
3506 fputs(mark, stdout);
3507 putchar(' ');
3508}