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