log --remerge-diff: show what the conflict resolution changed
[git/mjg.git] / revision.c
blobcdc43de8b9d4e31d8625047cc433a32d051aff95
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;
54 struct object *obj = &tree->object;
56 if (!has_object_file(&obj->oid))
57 return;
58 if (parse_tree(tree) < 0)
59 die("bad tree %s", oid_to_hex(&obj->oid));
61 init_tree_desc(&desc, tree->buffer, tree->size);
62 while (tree_entry(&desc, &entry)) {
63 switch (object_type(entry.mode)) {
64 case OBJ_TREE:
65 mark_tree_uninteresting(lookup_tree(entry.oid));
66 break;
67 case OBJ_BLOB:
68 mark_blob_uninteresting(lookup_blob(entry.oid));
69 break;
70 default:
71 /* Subproject commit - not in this repository */
72 break;
77 * We don't care about the tree any more
78 * after it has been marked uninteresting.
80 free_tree_buffer(tree);
83 void mark_tree_uninteresting(struct tree *tree)
85 struct object *obj;
87 if (!tree)
88 return;
90 obj = &tree->object;
91 if (obj->flags & UNINTERESTING)
92 return;
93 obj->flags |= UNINTERESTING;
94 mark_tree_contents_uninteresting(tree);
97 void mark_parents_uninteresting(struct commit *commit)
99 struct commit_list *parents = NULL, *l;
101 for (l = commit->parents; l; l = l->next)
102 commit_list_insert(l->item, &parents);
104 while (parents) {
105 struct commit *commit = pop_commit(&parents);
107 while (commit) {
109 * A missing commit is ok iff its parent is marked
110 * uninteresting.
112 * We just mark such a thing parsed, so that when
113 * it is popped next time around, we won't be trying
114 * to parse it and get an error.
116 if (!commit->object.parsed &&
117 !has_object_file(&commit->object.oid))
118 commit->object.parsed = 1;
120 if (commit->object.flags & UNINTERESTING)
121 break;
123 commit->object.flags |= UNINTERESTING;
126 * Normally we haven't parsed the parent
127 * yet, so we won't have a parent of a parent
128 * here. However, it may turn out that we've
129 * reached this commit some other way (where it
130 * wasn't uninteresting), in which case we need
131 * to mark its parents recursively too..
133 if (!commit->parents)
134 break;
136 for (l = commit->parents->next; l; l = l->next)
137 commit_list_insert(l->item, &parents);
138 commit = commit->parents->item;
143 static void add_pending_object_with_path(struct rev_info *revs,
144 struct object *obj,
145 const char *name, unsigned mode,
146 const char *path)
148 if (!obj)
149 return;
150 if (revs->no_walk && (obj->flags & UNINTERESTING))
151 revs->no_walk = 0;
152 if (revs->reflog_info && obj->type == OBJ_COMMIT) {
153 struct strbuf buf = STRBUF_INIT;
154 int len = interpret_branch_name(name, 0, &buf, 0);
156 if (0 < len && name[len] && buf.len)
157 strbuf_addstr(&buf, name + len);
158 add_reflog_for_walk(revs->reflog_info,
159 (struct commit *)obj,
160 buf.buf[0] ? buf.buf: name);
161 strbuf_release(&buf);
162 return; /* do not add the commit itself */
164 add_object_array_with_path(obj, name, &revs->pending, mode, path);
167 static void add_pending_object_with_mode(struct rev_info *revs,
168 struct object *obj,
169 const char *name, unsigned mode)
171 add_pending_object_with_path(revs, obj, name, mode, NULL);
174 void add_pending_object(struct rev_info *revs,
175 struct object *obj, const char *name)
177 add_pending_object_with_mode(revs, obj, name, S_IFINVALID);
180 void add_head_to_pending(struct rev_info *revs)
182 struct object_id oid;
183 struct object *obj;
184 if (get_oid("HEAD", &oid))
185 return;
186 obj = parse_object(&oid);
187 if (!obj)
188 return;
189 add_pending_object(revs, obj, "HEAD");
192 static struct object *get_reference(struct rev_info *revs, const char *name,
193 const struct object_id *oid,
194 unsigned int flags)
196 struct object *object;
198 object = parse_object(oid);
199 if (!object) {
200 if (revs->ignore_missing)
201 return object;
202 if (revs->exclude_promisor_objects && is_promisor_object(oid))
203 return NULL;
204 die("bad object %s", name);
206 object->flags |= flags;
207 return object;
210 void add_pending_oid(struct rev_info *revs, const char *name,
211 const struct object_id *oid, unsigned int flags)
213 struct object *object = get_reference(revs, name, oid, flags);
214 add_pending_object(revs, object, name);
217 static struct commit *handle_commit(struct rev_info *revs,
218 struct object_array_entry *entry)
220 struct object *object = entry->item;
221 const char *name = entry->name;
222 const char *path = entry->path;
223 unsigned int mode = entry->mode;
224 unsigned long flags = object->flags;
227 * Tag object? Look what it points to..
229 while (object->type == OBJ_TAG) {
230 struct tag *tag = (struct tag *) object;
231 if (revs->tag_objects && !(flags & UNINTERESTING))
232 add_pending_object(revs, object, tag->tag);
233 if (!tag->tagged)
234 die("bad tag");
235 object = parse_object(&tag->tagged->oid);
236 if (!object) {
237 if (revs->ignore_missing_links || (flags & UNINTERESTING))
238 return NULL;
239 die("bad object %s", oid_to_hex(&tag->tagged->oid));
241 object->flags |= flags;
243 * We'll handle the tagged object by looping or dropping
244 * through to the non-tag handlers below. Do not
245 * propagate path data from the tag's pending entry.
247 path = NULL;
248 mode = 0;
252 * Commit object? Just return it, we'll do all the complex
253 * reachability crud.
255 if (object->type == OBJ_COMMIT) {
256 struct commit *commit = (struct commit *)object;
257 if (parse_commit(commit) < 0)
258 die("unable to parse commit %s", name);
259 if (flags & UNINTERESTING) {
260 mark_parents_uninteresting(commit);
261 revs->limited = 1;
263 if (revs->show_source && !commit->util)
264 commit->util = xstrdup(name);
265 return commit;
269 * Tree object? Either mark it uninteresting, or add it
270 * to the list of objects to look at later..
272 if (object->type == OBJ_TREE) {
273 struct tree *tree = (struct tree *)object;
274 if (!revs->tree_objects)
275 return NULL;
276 if (flags & UNINTERESTING) {
277 mark_tree_contents_uninteresting(tree);
278 return NULL;
280 add_pending_object_with_path(revs, object, name, mode, path);
281 return NULL;
285 * Blob object? You know the drill by now..
287 if (object->type == OBJ_BLOB) {
288 if (!revs->blob_objects)
289 return NULL;
290 if (flags & UNINTERESTING)
291 return NULL;
292 add_pending_object_with_path(revs, object, name, mode, path);
293 return NULL;
295 die("%s is unknown object", name);
298 static int everybody_uninteresting(struct commit_list *orig,
299 struct commit **interesting_cache)
301 struct commit_list *list = orig;
303 if (*interesting_cache) {
304 struct commit *commit = *interesting_cache;
305 if (!(commit->object.flags & UNINTERESTING))
306 return 0;
309 while (list) {
310 struct commit *commit = list->item;
311 list = list->next;
312 if (commit->object.flags & UNINTERESTING)
313 continue;
315 *interesting_cache = commit;
316 return 0;
318 return 1;
322 * A definition of "relevant" commit that we can use to simplify limited graphs
323 * by eliminating side branches.
325 * A "relevant" commit is one that is !UNINTERESTING (ie we are including it
326 * in our list), or that is a specified BOTTOM commit. Then after computing
327 * a limited list, during processing we can generally ignore boundary merges
328 * coming from outside the graph, (ie from irrelevant parents), and treat
329 * those merges as if they were single-parent. TREESAME is defined to consider
330 * only relevant parents, if any. If we are TREESAME to our on-graph parents,
331 * we don't care if we were !TREESAME to non-graph parents.
333 * Treating bottom commits as relevant ensures that a limited graph's
334 * connection to the actual bottom commit is not viewed as a side branch, but
335 * treated as part of the graph. For example:
337 * ....Z...A---X---o---o---B
338 * . /
339 * W---Y
341 * When computing "A..B", the A-X connection is at least as important as
342 * Y-X, despite A being flagged UNINTERESTING.
344 * And when computing --ancestry-path "A..B", the A-X connection is more
345 * important than Y-X, despite both A and Y being flagged UNINTERESTING.
347 static inline int relevant_commit(struct commit *commit)
349 return (commit->object.flags & (UNINTERESTING | BOTTOM)) != UNINTERESTING;
353 * Return a single relevant commit from a parent list. If we are a TREESAME
354 * commit, and this selects one of our parents, then we can safely simplify to
355 * that parent.
357 static struct commit *one_relevant_parent(const struct rev_info *revs,
358 struct commit_list *orig)
360 struct commit_list *list = orig;
361 struct commit *relevant = NULL;
363 if (!orig)
364 return NULL;
367 * For 1-parent commits, or if first-parent-only, then return that
368 * first parent (even if not "relevant" by the above definition).
369 * TREESAME will have been set purely on that parent.
371 if (revs->first_parent_only || !orig->next)
372 return orig->item;
375 * For multi-parent commits, identify a sole relevant parent, if any.
376 * If we have only one relevant parent, then TREESAME will be set purely
377 * with regard to that parent, and we can simplify accordingly.
379 * If we have more than one relevant parent, or no relevant parents
380 * (and multiple irrelevant ones), then we can't select a parent here
381 * and return NULL.
383 while (list) {
384 struct commit *commit = list->item;
385 list = list->next;
386 if (relevant_commit(commit)) {
387 if (relevant)
388 return NULL;
389 relevant = commit;
392 return relevant;
396 * The goal is to get REV_TREE_NEW as the result only if the
397 * diff consists of all '+' (and no other changes), REV_TREE_OLD
398 * if the whole diff is removal of old data, and otherwise
399 * REV_TREE_DIFFERENT (of course if the trees are the same we
400 * want REV_TREE_SAME).
402 * The only time we care about the distinction is when
403 * remove_empty_trees is in effect, in which case we care only about
404 * whether the whole change is REV_TREE_NEW, or if there's another type
405 * of change. Which means we can stop the diff early in either of these
406 * cases:
408 * 1. We're not using remove_empty_trees at all.
410 * 2. We saw anything except REV_TREE_NEW.
412 static int tree_difference = REV_TREE_SAME;
414 static void file_add_remove(struct diff_options *options,
415 int addremove, unsigned mode,
416 const struct object_id *oid,
417 int oid_valid,
418 const char *fullpath, unsigned dirty_submodule)
420 int diff = addremove == '+' ? REV_TREE_NEW : REV_TREE_OLD;
421 struct rev_info *revs = options->change_fn_data;
423 tree_difference |= diff;
424 if (!revs->remove_empty_trees || tree_difference != REV_TREE_NEW)
425 options->flags.has_changes = 1;
428 static void file_change(struct diff_options *options,
429 unsigned old_mode, unsigned new_mode,
430 const struct object_id *old_oid,
431 const struct object_id *new_oid,
432 int old_oid_valid, int new_oid_valid,
433 const char *fullpath,
434 unsigned old_dirty_submodule, unsigned new_dirty_submodule)
436 tree_difference = REV_TREE_DIFFERENT;
437 options->flags.has_changes = 1;
440 static int rev_compare_tree(struct rev_info *revs,
441 struct commit *parent, struct commit *commit)
443 struct tree *t1 = parent->tree;
444 struct tree *t2 = commit->tree;
446 if (!t1)
447 return REV_TREE_NEW;
448 if (!t2)
449 return REV_TREE_OLD;
451 if (revs->simplify_by_decoration) {
453 * If we are simplifying by decoration, then the commit
454 * is worth showing if it has a tag pointing at it.
456 if (get_name_decoration(&commit->object))
457 return REV_TREE_DIFFERENT;
459 * A commit that is not pointed by a tag is uninteresting
460 * if we are not limited by path. This means that you will
461 * see the usual "commits that touch the paths" plus any
462 * tagged commit by specifying both --simplify-by-decoration
463 * and pathspec.
465 if (!revs->prune_data.nr)
466 return REV_TREE_SAME;
469 tree_difference = REV_TREE_SAME;
470 revs->pruning.flags.has_changes = 0;
471 if (diff_tree_oid(&t1->object.oid, &t2->object.oid, "",
472 &revs->pruning) < 0)
473 return REV_TREE_DIFFERENT;
474 return tree_difference;
477 static int rev_same_tree_as_empty(struct rev_info *revs, struct commit *commit)
479 int retval;
480 struct tree *t1 = commit->tree;
482 if (!t1)
483 return 0;
485 tree_difference = REV_TREE_SAME;
486 revs->pruning.flags.has_changes = 0;
487 retval = diff_tree_oid(NULL, &t1->object.oid, "", &revs->pruning);
489 return retval >= 0 && (tree_difference == REV_TREE_SAME);
492 struct treesame_state {
493 unsigned int nparents;
494 unsigned char treesame[FLEX_ARRAY];
497 static struct treesame_state *initialise_treesame(struct rev_info *revs, struct commit *commit)
499 unsigned n = commit_list_count(commit->parents);
500 struct treesame_state *st = xcalloc(1, st_add(sizeof(*st), n));
501 st->nparents = n;
502 add_decoration(&revs->treesame, &commit->object, st);
503 return st;
507 * Must be called immediately after removing the nth_parent from a commit's
508 * parent list, if we are maintaining the per-parent treesame[] decoration.
509 * This does not recalculate the master TREESAME flag - update_treesame()
510 * should be called to update it after a sequence of treesame[] modifications
511 * that may have affected it.
513 static int compact_treesame(struct rev_info *revs, struct commit *commit, unsigned nth_parent)
515 struct treesame_state *st;
516 int old_same;
518 if (!commit->parents) {
520 * Have just removed the only parent from a non-merge.
521 * Different handling, as we lack decoration.
523 if (nth_parent != 0)
524 die("compact_treesame %u", nth_parent);
525 old_same = !!(commit->object.flags & TREESAME);
526 if (rev_same_tree_as_empty(revs, commit))
527 commit->object.flags |= TREESAME;
528 else
529 commit->object.flags &= ~TREESAME;
530 return old_same;
533 st = lookup_decoration(&revs->treesame, &commit->object);
534 if (!st || nth_parent >= st->nparents)
535 die("compact_treesame %u", nth_parent);
537 old_same = st->treesame[nth_parent];
538 memmove(st->treesame + nth_parent,
539 st->treesame + nth_parent + 1,
540 st->nparents - nth_parent - 1);
543 * If we've just become a non-merge commit, update TREESAME
544 * immediately, and remove the no-longer-needed decoration.
545 * If still a merge, defer update until update_treesame().
547 if (--st->nparents == 1) {
548 if (commit->parents->next)
549 die("compact_treesame parents mismatch");
550 if (st->treesame[0] && revs->dense)
551 commit->object.flags |= TREESAME;
552 else
553 commit->object.flags &= ~TREESAME;
554 free(add_decoration(&revs->treesame, &commit->object, NULL));
557 return old_same;
560 static unsigned update_treesame(struct rev_info *revs, struct commit *commit)
562 if (commit->parents && commit->parents->next) {
563 unsigned n;
564 struct treesame_state *st;
565 struct commit_list *p;
566 unsigned relevant_parents;
567 unsigned relevant_change, irrelevant_change;
569 st = lookup_decoration(&revs->treesame, &commit->object);
570 if (!st)
571 die("update_treesame %s", oid_to_hex(&commit->object.oid));
572 relevant_parents = 0;
573 relevant_change = irrelevant_change = 0;
574 for (p = commit->parents, n = 0; p; n++, p = p->next) {
575 if (relevant_commit(p->item)) {
576 relevant_change |= !st->treesame[n];
577 relevant_parents++;
578 } else
579 irrelevant_change |= !st->treesame[n];
581 if (relevant_parents ? relevant_change : irrelevant_change)
582 commit->object.flags &= ~TREESAME;
583 else
584 commit->object.flags |= TREESAME;
587 return commit->object.flags & TREESAME;
590 static inline int limiting_can_increase_treesame(const struct rev_info *revs)
593 * TREESAME is irrelevant unless prune && dense;
594 * if simplify_history is set, we can't have a mixture of TREESAME and
595 * !TREESAME INTERESTING parents (and we don't have treesame[]
596 * decoration anyway);
597 * if first_parent_only is set, then the TREESAME flag is locked
598 * against the first parent (and again we lack treesame[] decoration).
600 return revs->prune && revs->dense &&
601 !revs->simplify_history &&
602 !revs->first_parent_only;
605 static void try_to_simplify_commit(struct rev_info *revs, struct commit *commit)
607 struct commit_list **pp, *parent;
608 struct treesame_state *ts = NULL;
609 int relevant_change = 0, irrelevant_change = 0;
610 int relevant_parents, nth_parent;
613 * If we don't do pruning, everything is interesting
615 if (!revs->prune)
616 return;
618 if (!commit->tree)
619 return;
621 if (!commit->parents) {
622 if (rev_same_tree_as_empty(revs, commit))
623 commit->object.flags |= TREESAME;
624 return;
628 * Normal non-merge commit? If we don't want to make the
629 * history dense, we consider it always to be a change..
631 if (!revs->dense && !commit->parents->next)
632 return;
634 for (pp = &commit->parents, nth_parent = 0, relevant_parents = 0;
635 (parent = *pp) != NULL;
636 pp = &parent->next, nth_parent++) {
637 struct commit *p = parent->item;
638 if (relevant_commit(p))
639 relevant_parents++;
641 if (nth_parent == 1) {
643 * This our second loop iteration - so we now know
644 * we're dealing with a merge.
646 * Do not compare with later parents when we care only about
647 * the first parent chain, in order to avoid derailing the
648 * traversal to follow a side branch that brought everything
649 * in the path we are limited to by the pathspec.
651 if (revs->first_parent_only)
652 break;
654 * If this will remain a potentially-simplifiable
655 * merge, remember per-parent treesame if needed.
656 * Initialise the array with the comparison from our
657 * first iteration.
659 if (revs->treesame.name &&
660 !revs->simplify_history &&
661 !(commit->object.flags & UNINTERESTING)) {
662 ts = initialise_treesame(revs, commit);
663 if (!(irrelevant_change || relevant_change))
664 ts->treesame[0] = 1;
667 if (parse_commit(p) < 0)
668 die("cannot simplify commit %s (because of %s)",
669 oid_to_hex(&commit->object.oid),
670 oid_to_hex(&p->object.oid));
671 switch (rev_compare_tree(revs, p, commit)) {
672 case REV_TREE_SAME:
673 if (!revs->simplify_history || !relevant_commit(p)) {
674 /* Even if a merge with an uninteresting
675 * side branch brought the entire change
676 * we are interested in, we do not want
677 * to lose the other branches of this
678 * merge, so we just keep going.
680 if (ts)
681 ts->treesame[nth_parent] = 1;
682 continue;
684 parent->next = NULL;
685 commit->parents = parent;
686 commit->object.flags |= TREESAME;
687 return;
689 case REV_TREE_NEW:
690 if (revs->remove_empty_trees &&
691 rev_same_tree_as_empty(revs, p)) {
692 /* We are adding all the specified
693 * paths from this parent, so the
694 * history beyond this parent is not
695 * interesting. Remove its parents
696 * (they are grandparents for us).
697 * IOW, we pretend this parent is a
698 * "root" commit.
700 if (parse_commit(p) < 0)
701 die("cannot simplify commit %s (invalid %s)",
702 oid_to_hex(&commit->object.oid),
703 oid_to_hex(&p->object.oid));
704 p->parents = NULL;
706 /* fallthrough */
707 case REV_TREE_OLD:
708 case REV_TREE_DIFFERENT:
709 if (relevant_commit(p))
710 relevant_change = 1;
711 else
712 irrelevant_change = 1;
713 continue;
715 die("bad tree compare for commit %s", oid_to_hex(&commit->object.oid));
719 * TREESAME is straightforward for single-parent commits. For merge
720 * commits, it is most useful to define it so that "irrelevant"
721 * parents cannot make us !TREESAME - if we have any relevant
722 * parents, then we only consider TREESAMEness with respect to them,
723 * allowing irrelevant merges from uninteresting branches to be
724 * simplified away. Only if we have only irrelevant parents do we
725 * base TREESAME on them. Note that this logic is replicated in
726 * update_treesame, which should be kept in sync.
728 if (relevant_parents ? !relevant_change : !irrelevant_change)
729 commit->object.flags |= TREESAME;
732 static void commit_list_insert_by_date_cached(struct commit *p, struct commit_list **head,
733 struct commit_list *cached_base, struct commit_list **cache)
735 struct commit_list *new_entry;
737 if (cached_base && p->date < cached_base->item->date)
738 new_entry = commit_list_insert_by_date(p, &cached_base->next);
739 else
740 new_entry = commit_list_insert_by_date(p, head);
742 if (cache && (!*cache || p->date < (*cache)->item->date))
743 *cache = new_entry;
746 static int add_parents_to_list(struct rev_info *revs, struct commit *commit,
747 struct commit_list **list, struct commit_list **cache_ptr)
749 struct commit_list *parent = commit->parents;
750 unsigned left_flag;
751 struct commit_list *cached_base = cache_ptr ? *cache_ptr : NULL;
753 if (commit->object.flags & ADDED)
754 return 0;
755 commit->object.flags |= ADDED;
757 if (revs->include_check &&
758 !revs->include_check(commit, revs->include_check_data))
759 return 0;
762 * If the commit is uninteresting, don't try to
763 * prune parents - we want the maximal uninteresting
764 * set.
766 * Normally we haven't parsed the parent
767 * yet, so we won't have a parent of a parent
768 * here. However, it may turn out that we've
769 * reached this commit some other way (where it
770 * wasn't uninteresting), in which case we need
771 * to mark its parents recursively too..
773 if (commit->object.flags & UNINTERESTING) {
774 while (parent) {
775 struct commit *p = parent->item;
776 parent = parent->next;
777 if (p)
778 p->object.flags |= UNINTERESTING;
779 if (parse_commit_gently(p, 1) < 0)
780 continue;
781 if (p->parents)
782 mark_parents_uninteresting(p);
783 if (p->object.flags & SEEN)
784 continue;
785 p->object.flags |= SEEN;
786 commit_list_insert_by_date_cached(p, list, cached_base, cache_ptr);
788 return 0;
792 * Ok, the commit wasn't uninteresting. Try to
793 * simplify the commit history and find the parent
794 * that has no differences in the path set if one exists.
796 try_to_simplify_commit(revs, commit);
798 if (revs->no_walk)
799 return 0;
801 left_flag = (commit->object.flags & SYMMETRIC_LEFT);
803 for (parent = commit->parents; parent; parent = parent->next) {
804 struct commit *p = parent->item;
805 int gently = revs->ignore_missing_links ||
806 revs->exclude_promisor_objects;
807 if (parse_commit_gently(p, gently) < 0) {
808 if (revs->exclude_promisor_objects &&
809 is_promisor_object(&p->object.oid)) {
810 if (revs->first_parent_only)
811 break;
812 continue;
814 return -1;
816 if (revs->show_source && !p->util)
817 p->util = commit->util;
818 p->object.flags |= left_flag;
819 if (!(p->object.flags & SEEN)) {
820 p->object.flags |= SEEN;
821 commit_list_insert_by_date_cached(p, list, cached_base, cache_ptr);
823 if (revs->first_parent_only)
824 break;
826 return 0;
829 static void cherry_pick_list(struct commit_list *list, struct rev_info *revs)
831 struct commit_list *p;
832 int left_count = 0, right_count = 0;
833 int left_first;
834 struct patch_ids ids;
835 unsigned cherry_flag;
837 /* First count the commits on the left and on the right */
838 for (p = list; p; p = p->next) {
839 struct commit *commit = p->item;
840 unsigned flags = commit->object.flags;
841 if (flags & BOUNDARY)
843 else if (flags & SYMMETRIC_LEFT)
844 left_count++;
845 else
846 right_count++;
849 if (!left_count || !right_count)
850 return;
852 left_first = left_count < right_count;
853 init_patch_ids(&ids);
854 ids.diffopts.pathspec = revs->diffopt.pathspec;
856 /* Compute patch-ids for one side */
857 for (p = list; p; p = p->next) {
858 struct commit *commit = p->item;
859 unsigned flags = commit->object.flags;
861 if (flags & BOUNDARY)
862 continue;
864 * If we have fewer left, left_first is set and we omit
865 * commits on the right branch in this loop. If we have
866 * fewer right, we skip the left ones.
868 if (left_first != !!(flags & SYMMETRIC_LEFT))
869 continue;
870 add_commit_patch_id(commit, &ids);
873 /* either cherry_mark or cherry_pick are true */
874 cherry_flag = revs->cherry_mark ? PATCHSAME : SHOWN;
876 /* Check the other side */
877 for (p = list; p; p = p->next) {
878 struct commit *commit = p->item;
879 struct patch_id *id;
880 unsigned flags = commit->object.flags;
882 if (flags & BOUNDARY)
883 continue;
885 * If we have fewer left, left_first is set and we omit
886 * commits on the left branch in this loop.
888 if (left_first == !!(flags & SYMMETRIC_LEFT))
889 continue;
892 * Have we seen the same patch id?
894 id = has_commit_patch_id(commit, &ids);
895 if (!id)
896 continue;
898 commit->object.flags |= cherry_flag;
899 id->commit->object.flags |= cherry_flag;
902 free_patch_ids(&ids);
905 /* How many extra uninteresting commits we want to see.. */
906 #define SLOP 5
908 static int still_interesting(struct commit_list *src, timestamp_t date, int slop,
909 struct commit **interesting_cache)
912 * No source list at all? We're definitely done..
914 if (!src)
915 return 0;
918 * Does the destination list contain entries with a date
919 * before the source list? Definitely _not_ done.
921 if (date <= src->item->date)
922 return SLOP;
925 * Does the source list still have interesting commits in
926 * it? Definitely not done..
928 if (!everybody_uninteresting(src, interesting_cache))
929 return SLOP;
931 /* Ok, we're closing in.. */
932 return slop-1;
936 * "rev-list --ancestry-path A..B" computes commits that are ancestors
937 * of B but not ancestors of A but further limits the result to those
938 * that are descendants of A. This takes the list of bottom commits and
939 * the result of "A..B" without --ancestry-path, and limits the latter
940 * further to the ones that can reach one of the commits in "bottom".
942 static void limit_to_ancestry(struct commit_list *bottom, struct commit_list *list)
944 struct commit_list *p;
945 struct commit_list *rlist = NULL;
946 int made_progress;
949 * Reverse the list so that it will be likely that we would
950 * process parents before children.
952 for (p = list; p; p = p->next)
953 commit_list_insert(p->item, &rlist);
955 for (p = bottom; p; p = p->next)
956 p->item->object.flags |= TMP_MARK;
959 * Mark the ones that can reach bottom commits in "list",
960 * in a bottom-up fashion.
962 do {
963 made_progress = 0;
964 for (p = rlist; p; p = p->next) {
965 struct commit *c = p->item;
966 struct commit_list *parents;
967 if (c->object.flags & (TMP_MARK | UNINTERESTING))
968 continue;
969 for (parents = c->parents;
970 parents;
971 parents = parents->next) {
972 if (!(parents->item->object.flags & TMP_MARK))
973 continue;
974 c->object.flags |= TMP_MARK;
975 made_progress = 1;
976 break;
979 } while (made_progress);
982 * NEEDSWORK: decide if we want to remove parents that are
983 * not marked with TMP_MARK from commit->parents for commits
984 * in the resulting list. We may not want to do that, though.
988 * The ones that are not marked with TMP_MARK are uninteresting
990 for (p = list; p; p = p->next) {
991 struct commit *c = p->item;
992 if (c->object.flags & TMP_MARK)
993 continue;
994 c->object.flags |= UNINTERESTING;
997 /* We are done with the TMP_MARK */
998 for (p = list; p; p = p->next)
999 p->item->object.flags &= ~TMP_MARK;
1000 for (p = bottom; p; p = p->next)
1001 p->item->object.flags &= ~TMP_MARK;
1002 free_commit_list(rlist);
1006 * Before walking the history, keep the set of "negative" refs the
1007 * caller has asked to exclude.
1009 * This is used to compute "rev-list --ancestry-path A..B", as we need
1010 * to filter the result of "A..B" further to the ones that can actually
1011 * reach A.
1013 static struct commit_list *collect_bottom_commits(struct commit_list *list)
1015 struct commit_list *elem, *bottom = NULL;
1016 for (elem = list; elem; elem = elem->next)
1017 if (elem->item->object.flags & BOTTOM)
1018 commit_list_insert(elem->item, &bottom);
1019 return bottom;
1022 /* Assumes either left_only or right_only is set */
1023 static void limit_left_right(struct commit_list *list, struct rev_info *revs)
1025 struct commit_list *p;
1027 for (p = list; p; p = p->next) {
1028 struct commit *commit = p->item;
1030 if (revs->right_only) {
1031 if (commit->object.flags & SYMMETRIC_LEFT)
1032 commit->object.flags |= SHOWN;
1033 } else /* revs->left_only is set */
1034 if (!(commit->object.flags & SYMMETRIC_LEFT))
1035 commit->object.flags |= SHOWN;
1039 static int limit_list(struct rev_info *revs)
1041 int slop = SLOP;
1042 timestamp_t date = TIME_MAX;
1043 struct commit_list *list = revs->commits;
1044 struct commit_list *newlist = NULL;
1045 struct commit_list **p = &newlist;
1046 struct commit_list *bottom = NULL;
1047 struct commit *interesting_cache = NULL;
1049 if (revs->ancestry_path) {
1050 bottom = collect_bottom_commits(list);
1051 if (!bottom)
1052 die("--ancestry-path given but there are no bottom commits");
1055 while (list) {
1056 struct commit *commit = pop_commit(&list);
1057 struct object *obj = &commit->object;
1058 show_early_output_fn_t show;
1060 if (commit == interesting_cache)
1061 interesting_cache = NULL;
1063 if (revs->max_age != -1 && (commit->date < revs->max_age))
1064 obj->flags |= UNINTERESTING;
1065 if (add_parents_to_list(revs, commit, &list, NULL) < 0)
1066 return -1;
1067 if (obj->flags & UNINTERESTING) {
1068 mark_parents_uninteresting(commit);
1069 slop = still_interesting(list, date, slop, &interesting_cache);
1070 if (slop)
1071 continue;
1072 break;
1074 if (revs->min_age != -1 && (commit->date > revs->min_age))
1075 continue;
1076 date = commit->date;
1077 p = &commit_list_insert(commit, p)->next;
1079 show = show_early_output;
1080 if (!show)
1081 continue;
1083 show(revs, newlist);
1084 show_early_output = NULL;
1086 if (revs->cherry_pick || revs->cherry_mark)
1087 cherry_pick_list(newlist, revs);
1089 if (revs->left_only || revs->right_only)
1090 limit_left_right(newlist, revs);
1092 if (bottom) {
1093 limit_to_ancestry(bottom, newlist);
1094 free_commit_list(bottom);
1098 * Check if any commits have become TREESAME by some of their parents
1099 * becoming UNINTERESTING.
1101 if (limiting_can_increase_treesame(revs))
1102 for (list = newlist; list; list = list->next) {
1103 struct commit *c = list->item;
1104 if (c->object.flags & (UNINTERESTING | TREESAME))
1105 continue;
1106 update_treesame(revs, c);
1109 revs->commits = newlist;
1110 return 0;
1114 * Add an entry to refs->cmdline with the specified information.
1115 * *name is copied.
1117 static void add_rev_cmdline(struct rev_info *revs,
1118 struct object *item,
1119 const char *name,
1120 int whence,
1121 unsigned flags)
1123 struct rev_cmdline_info *info = &revs->cmdline;
1124 unsigned int nr = info->nr;
1126 ALLOC_GROW(info->rev, nr + 1, info->alloc);
1127 info->rev[nr].item = item;
1128 info->rev[nr].name = xstrdup(name);
1129 info->rev[nr].whence = whence;
1130 info->rev[nr].flags = flags;
1131 info->nr++;
1134 static void add_rev_cmdline_list(struct rev_info *revs,
1135 struct commit_list *commit_list,
1136 int whence,
1137 unsigned flags)
1139 while (commit_list) {
1140 struct object *object = &commit_list->item->object;
1141 add_rev_cmdline(revs, object, oid_to_hex(&object->oid),
1142 whence, flags);
1143 commit_list = commit_list->next;
1147 struct all_refs_cb {
1148 int all_flags;
1149 int warned_bad_reflog;
1150 struct rev_info *all_revs;
1151 const char *name_for_errormsg;
1152 struct ref_store *refs;
1155 int ref_excluded(struct string_list *ref_excludes, const char *path)
1157 struct string_list_item *item;
1159 if (!ref_excludes)
1160 return 0;
1161 for_each_string_list_item(item, ref_excludes) {
1162 if (!wildmatch(item->string, path, 0))
1163 return 1;
1165 return 0;
1168 static int handle_one_ref(const char *path, const struct object_id *oid,
1169 int flag, void *cb_data)
1171 struct all_refs_cb *cb = cb_data;
1172 struct object *object;
1174 if (ref_excluded(cb->all_revs->ref_excludes, path))
1175 return 0;
1177 object = get_reference(cb->all_revs, path, oid, cb->all_flags);
1178 add_rev_cmdline(cb->all_revs, object, path, REV_CMD_REF, cb->all_flags);
1179 add_pending_oid(cb->all_revs, path, oid, cb->all_flags);
1180 return 0;
1183 static void init_all_refs_cb(struct all_refs_cb *cb, struct rev_info *revs,
1184 unsigned flags)
1186 cb->all_revs = revs;
1187 cb->all_flags = flags;
1188 revs->rev_input_given = 1;
1189 cb->refs = NULL;
1192 void clear_ref_exclusion(struct string_list **ref_excludes_p)
1194 if (*ref_excludes_p) {
1195 string_list_clear(*ref_excludes_p, 0);
1196 free(*ref_excludes_p);
1198 *ref_excludes_p = NULL;
1201 void add_ref_exclusion(struct string_list **ref_excludes_p, const char *exclude)
1203 if (!*ref_excludes_p) {
1204 *ref_excludes_p = xcalloc(1, sizeof(**ref_excludes_p));
1205 (*ref_excludes_p)->strdup_strings = 1;
1207 string_list_append(*ref_excludes_p, exclude);
1210 static void handle_refs(struct ref_store *refs,
1211 struct rev_info *revs, unsigned flags,
1212 int (*for_each)(struct ref_store *, each_ref_fn, void *))
1214 struct all_refs_cb cb;
1216 if (!refs) {
1217 /* this could happen with uninitialized submodules */
1218 return;
1221 init_all_refs_cb(&cb, revs, flags);
1222 for_each(refs, handle_one_ref, &cb);
1225 static void handle_one_reflog_commit(struct object_id *oid, void *cb_data)
1227 struct all_refs_cb *cb = cb_data;
1228 if (!is_null_oid(oid)) {
1229 struct object *o = parse_object(oid);
1230 if (o) {
1231 o->flags |= cb->all_flags;
1232 /* ??? CMDLINEFLAGS ??? */
1233 add_pending_object(cb->all_revs, o, "");
1235 else if (!cb->warned_bad_reflog) {
1236 warning("reflog of '%s' references pruned commits",
1237 cb->name_for_errormsg);
1238 cb->warned_bad_reflog = 1;
1243 static int handle_one_reflog_ent(struct object_id *ooid, struct object_id *noid,
1244 const char *email, timestamp_t timestamp, int tz,
1245 const char *message, void *cb_data)
1247 handle_one_reflog_commit(ooid, cb_data);
1248 handle_one_reflog_commit(noid, cb_data);
1249 return 0;
1252 static int handle_one_reflog(const char *path, const struct object_id *oid,
1253 int flag, void *cb_data)
1255 struct all_refs_cb *cb = cb_data;
1256 cb->warned_bad_reflog = 0;
1257 cb->name_for_errormsg = path;
1258 refs_for_each_reflog_ent(cb->refs, path,
1259 handle_one_reflog_ent, cb_data);
1260 return 0;
1263 static void add_other_reflogs_to_pending(struct all_refs_cb *cb)
1265 struct worktree **worktrees, **p;
1267 worktrees = get_worktrees(0);
1268 for (p = worktrees; *p; p++) {
1269 struct worktree *wt = *p;
1271 if (wt->is_current)
1272 continue;
1274 cb->refs = get_worktree_ref_store(wt);
1275 refs_for_each_reflog(cb->refs,
1276 handle_one_reflog,
1277 cb);
1279 free_worktrees(worktrees);
1282 void add_reflogs_to_pending(struct rev_info *revs, unsigned flags)
1284 struct all_refs_cb cb;
1286 cb.all_revs = revs;
1287 cb.all_flags = flags;
1288 cb.refs = get_main_ref_store();
1289 for_each_reflog(handle_one_reflog, &cb);
1291 if (!revs->single_worktree)
1292 add_other_reflogs_to_pending(&cb);
1295 static void add_cache_tree(struct cache_tree *it, struct rev_info *revs,
1296 struct strbuf *path)
1298 size_t baselen = path->len;
1299 int i;
1301 if (it->entry_count >= 0) {
1302 struct tree *tree = lookup_tree(&it->oid);
1303 add_pending_object_with_path(revs, &tree->object, "",
1304 040000, path->buf);
1307 for (i = 0; i < it->subtree_nr; i++) {
1308 struct cache_tree_sub *sub = it->down[i];
1309 strbuf_addf(path, "%s%s", baselen ? "/" : "", sub->name);
1310 add_cache_tree(sub->cache_tree, revs, path);
1311 strbuf_setlen(path, baselen);
1316 static void do_add_index_objects_to_pending(struct rev_info *revs,
1317 struct index_state *istate)
1319 int i;
1321 for (i = 0; i < istate->cache_nr; i++) {
1322 struct cache_entry *ce = istate->cache[i];
1323 struct blob *blob;
1325 if (S_ISGITLINK(ce->ce_mode))
1326 continue;
1328 blob = lookup_blob(&ce->oid);
1329 if (!blob)
1330 die("unable to add index blob to traversal");
1331 add_pending_object_with_path(revs, &blob->object, "",
1332 ce->ce_mode, ce->name);
1335 if (istate->cache_tree) {
1336 struct strbuf path = STRBUF_INIT;
1337 add_cache_tree(istate->cache_tree, revs, &path);
1338 strbuf_release(&path);
1342 void add_index_objects_to_pending(struct rev_info *revs, unsigned int flags)
1344 struct worktree **worktrees, **p;
1346 read_cache();
1347 do_add_index_objects_to_pending(revs, &the_index);
1349 if (revs->single_worktree)
1350 return;
1352 worktrees = get_worktrees(0);
1353 for (p = worktrees; *p; p++) {
1354 struct worktree *wt = *p;
1355 struct index_state istate = { NULL };
1357 if (wt->is_current)
1358 continue; /* current index already taken care of */
1360 if (read_index_from(&istate,
1361 worktree_git_path(wt, "index"),
1362 get_worktree_git_dir(wt)) > 0)
1363 do_add_index_objects_to_pending(revs, &istate);
1364 discard_index(&istate);
1366 free_worktrees(worktrees);
1369 static int add_parents_only(struct rev_info *revs, const char *arg_, int flags,
1370 int exclude_parent)
1372 struct object_id oid;
1373 struct object *it;
1374 struct commit *commit;
1375 struct commit_list *parents;
1376 int parent_number;
1377 const char *arg = arg_;
1379 if (*arg == '^') {
1380 flags ^= UNINTERESTING | BOTTOM;
1381 arg++;
1383 if (get_oid_committish(arg, &oid))
1384 return 0;
1385 while (1) {
1386 it = get_reference(revs, arg, &oid, 0);
1387 if (!it && revs->ignore_missing)
1388 return 0;
1389 if (it->type != OBJ_TAG)
1390 break;
1391 if (!((struct tag*)it)->tagged)
1392 return 0;
1393 oidcpy(&oid, &((struct tag*)it)->tagged->oid);
1395 if (it->type != OBJ_COMMIT)
1396 return 0;
1397 commit = (struct commit *)it;
1398 if (exclude_parent &&
1399 exclude_parent > commit_list_count(commit->parents))
1400 return 0;
1401 for (parents = commit->parents, parent_number = 1;
1402 parents;
1403 parents = parents->next, parent_number++) {
1404 if (exclude_parent && parent_number != exclude_parent)
1405 continue;
1407 it = &parents->item->object;
1408 it->flags |= flags;
1409 add_rev_cmdline(revs, it, arg_, REV_CMD_PARENTS_ONLY, flags);
1410 add_pending_object(revs, it, arg);
1412 return 1;
1415 void init_revisions(struct rev_info *revs, const char *prefix)
1417 memset(revs, 0, sizeof(*revs));
1419 revs->abbrev = DEFAULT_ABBREV;
1420 revs->simplify_history = 1;
1421 revs->pruning.flags.recursive = 1;
1422 revs->pruning.flags.quick = 1;
1423 revs->pruning.add_remove = file_add_remove;
1424 revs->pruning.change = file_change;
1425 revs->pruning.change_fn_data = revs;
1426 revs->sort_order = REV_SORT_IN_GRAPH_ORDER;
1427 revs->dense = 1;
1428 revs->prefix = prefix;
1429 revs->max_age = -1;
1430 revs->min_age = -1;
1431 revs->skip_count = -1;
1432 revs->max_count = -1;
1433 revs->max_parents = -1;
1434 revs->expand_tabs_in_log = -1;
1436 revs->commit_format = CMIT_FMT_DEFAULT;
1437 revs->expand_tabs_in_log_default = 8;
1439 init_grep_defaults();
1440 grep_init(&revs->grep_filter, prefix);
1441 revs->grep_filter.status_only = 1;
1443 diff_setup(&revs->diffopt);
1444 if (prefix && !revs->diffopt.prefix) {
1445 revs->diffopt.prefix = prefix;
1446 revs->diffopt.prefix_length = strlen(prefix);
1449 revs->notes_opt.use_default_notes = -1;
1452 static void add_pending_commit_list(struct rev_info *revs,
1453 struct commit_list *commit_list,
1454 unsigned int flags)
1456 while (commit_list) {
1457 struct object *object = &commit_list->item->object;
1458 object->flags |= flags;
1459 add_pending_object(revs, object, oid_to_hex(&object->oid));
1460 commit_list = commit_list->next;
1464 static void prepare_show_merge(struct rev_info *revs)
1466 struct commit_list *bases;
1467 struct commit *head, *other;
1468 struct object_id oid;
1469 const char **prune = NULL;
1470 int i, prune_num = 1; /* counting terminating NULL */
1472 if (get_oid("HEAD", &oid))
1473 die("--merge without HEAD?");
1474 head = lookup_commit_or_die(&oid, "HEAD");
1475 if (get_oid("MERGE_HEAD", &oid))
1476 die("--merge without MERGE_HEAD?");
1477 other = lookup_commit_or_die(&oid, "MERGE_HEAD");
1478 add_pending_object(revs, &head->object, "HEAD");
1479 add_pending_object(revs, &other->object, "MERGE_HEAD");
1480 bases = get_merge_bases(head, other);
1481 add_rev_cmdline_list(revs, bases, REV_CMD_MERGE_BASE, UNINTERESTING | BOTTOM);
1482 add_pending_commit_list(revs, bases, UNINTERESTING | BOTTOM);
1483 free_commit_list(bases);
1484 head->object.flags |= SYMMETRIC_LEFT;
1486 if (!active_nr)
1487 read_cache();
1488 for (i = 0; i < active_nr; i++) {
1489 const struct cache_entry *ce = active_cache[i];
1490 if (!ce_stage(ce))
1491 continue;
1492 if (ce_path_match(ce, &revs->prune_data, NULL)) {
1493 prune_num++;
1494 REALLOC_ARRAY(prune, prune_num);
1495 prune[prune_num-2] = ce->name;
1496 prune[prune_num-1] = NULL;
1498 while ((i+1 < active_nr) &&
1499 ce_same_name(ce, active_cache[i+1]))
1500 i++;
1502 clear_pathspec(&revs->prune_data);
1503 parse_pathspec(&revs->prune_data, PATHSPEC_ALL_MAGIC & ~PATHSPEC_LITERAL,
1504 PATHSPEC_PREFER_FULL | PATHSPEC_LITERAL_PATH, "", prune);
1505 revs->limited = 1;
1508 static int dotdot_missing(const char *arg, char *dotdot,
1509 struct rev_info *revs, int symmetric)
1511 if (revs->ignore_missing)
1512 return 0;
1513 /* de-munge so we report the full argument */
1514 *dotdot = '.';
1515 die(symmetric
1516 ? "Invalid symmetric difference expression %s"
1517 : "Invalid revision range %s", arg);
1520 static int handle_dotdot_1(const char *arg, char *dotdot,
1521 struct rev_info *revs, int flags,
1522 int cant_be_filename,
1523 struct object_context *a_oc,
1524 struct object_context *b_oc)
1526 const char *a_name, *b_name;
1527 struct object_id a_oid, b_oid;
1528 struct object *a_obj, *b_obj;
1529 unsigned int a_flags, b_flags;
1530 int symmetric = 0;
1531 unsigned int flags_exclude = flags ^ (UNINTERESTING | BOTTOM);
1532 unsigned int oc_flags = GET_OID_COMMITTISH | GET_OID_RECORD_PATH;
1534 a_name = arg;
1535 if (!*a_name)
1536 a_name = "HEAD";
1538 b_name = dotdot + 2;
1539 if (*b_name == '.') {
1540 symmetric = 1;
1541 b_name++;
1543 if (!*b_name)
1544 b_name = "HEAD";
1546 if (get_oid_with_context(a_name, oc_flags, &a_oid, a_oc) ||
1547 get_oid_with_context(b_name, oc_flags, &b_oid, b_oc))
1548 return -1;
1550 if (!cant_be_filename) {
1551 *dotdot = '.';
1552 verify_non_filename(revs->prefix, arg);
1553 *dotdot = '\0';
1556 a_obj = parse_object(&a_oid);
1557 b_obj = parse_object(&b_oid);
1558 if (!a_obj || !b_obj)
1559 return dotdot_missing(arg, dotdot, revs, symmetric);
1561 if (!symmetric) {
1562 /* just A..B */
1563 b_flags = flags;
1564 a_flags = flags_exclude;
1565 } else {
1566 /* A...B -- find merge bases between the two */
1567 struct commit *a, *b;
1568 struct commit_list *exclude;
1570 a = lookup_commit_reference(&a_obj->oid);
1571 b = lookup_commit_reference(&b_obj->oid);
1572 if (!a || !b)
1573 return dotdot_missing(arg, dotdot, revs, symmetric);
1575 exclude = get_merge_bases(a, b);
1576 add_rev_cmdline_list(revs, exclude, REV_CMD_MERGE_BASE,
1577 flags_exclude);
1578 add_pending_commit_list(revs, exclude, flags_exclude);
1579 free_commit_list(exclude);
1581 b_flags = flags;
1582 a_flags = flags | SYMMETRIC_LEFT;
1585 a_obj->flags |= a_flags;
1586 b_obj->flags |= b_flags;
1587 add_rev_cmdline(revs, a_obj, a_name, REV_CMD_LEFT, a_flags);
1588 add_rev_cmdline(revs, b_obj, b_name, REV_CMD_RIGHT, b_flags);
1589 add_pending_object_with_path(revs, a_obj, a_name, a_oc->mode, a_oc->path);
1590 add_pending_object_with_path(revs, b_obj, b_name, b_oc->mode, b_oc->path);
1591 return 0;
1594 static int handle_dotdot(const char *arg,
1595 struct rev_info *revs, int flags,
1596 int cant_be_filename)
1598 struct object_context a_oc, b_oc;
1599 char *dotdot = strstr(arg, "..");
1600 int ret;
1602 if (!dotdot)
1603 return -1;
1605 memset(&a_oc, 0, sizeof(a_oc));
1606 memset(&b_oc, 0, sizeof(b_oc));
1608 *dotdot = '\0';
1609 ret = handle_dotdot_1(arg, dotdot, revs, flags, cant_be_filename,
1610 &a_oc, &b_oc);
1611 *dotdot = '.';
1613 free(a_oc.path);
1614 free(b_oc.path);
1616 return ret;
1619 int handle_revision_arg(const char *arg_, struct rev_info *revs, int flags, unsigned revarg_opt)
1621 struct object_context oc;
1622 char *mark;
1623 struct object *object;
1624 struct object_id oid;
1625 int local_flags;
1626 const char *arg = arg_;
1627 int cant_be_filename = revarg_opt & REVARG_CANNOT_BE_FILENAME;
1628 unsigned get_sha1_flags = GET_OID_RECORD_PATH;
1630 flags = flags & UNINTERESTING ? flags | BOTTOM : flags & ~BOTTOM;
1632 if (!cant_be_filename && !strcmp(arg, "..")) {
1634 * Just ".."? That is not a range but the
1635 * pathspec for the parent directory.
1637 return -1;
1640 if (!handle_dotdot(arg, revs, flags, revarg_opt))
1641 return 0;
1643 mark = strstr(arg, "^@");
1644 if (mark && !mark[2]) {
1645 *mark = 0;
1646 if (add_parents_only(revs, arg, flags, 0))
1647 return 0;
1648 *mark = '^';
1650 mark = strstr(arg, "^!");
1651 if (mark && !mark[2]) {
1652 *mark = 0;
1653 if (!add_parents_only(revs, arg, flags ^ (UNINTERESTING | BOTTOM), 0))
1654 *mark = '^';
1656 mark = strstr(arg, "^-");
1657 if (mark) {
1658 int exclude_parent = 1;
1660 if (mark[2]) {
1661 char *end;
1662 exclude_parent = strtoul(mark + 2, &end, 10);
1663 if (*end != '\0' || !exclude_parent)
1664 return -1;
1667 *mark = 0;
1668 if (!add_parents_only(revs, arg, flags ^ (UNINTERESTING | BOTTOM), exclude_parent))
1669 *mark = '^';
1672 local_flags = 0;
1673 if (*arg == '^') {
1674 local_flags = UNINTERESTING | BOTTOM;
1675 arg++;
1678 if (revarg_opt & REVARG_COMMITTISH)
1679 get_sha1_flags |= GET_OID_COMMITTISH;
1681 if (get_oid_with_context(arg, get_sha1_flags, &oid, &oc))
1682 return revs->ignore_missing ? 0 : -1;
1683 if (!cant_be_filename)
1684 verify_non_filename(revs->prefix, arg);
1685 object = get_reference(revs, arg, &oid, flags ^ local_flags);
1686 add_rev_cmdline(revs, object, arg_, REV_CMD_REV, flags ^ local_flags);
1687 add_pending_object_with_path(revs, object, arg, oc.mode, oc.path);
1688 free(oc.path);
1689 return 0;
1692 static void read_pathspec_from_stdin(struct rev_info *revs, struct strbuf *sb,
1693 struct argv_array *prune)
1695 while (strbuf_getline(sb, stdin) != EOF)
1696 argv_array_push(prune, sb->buf);
1699 static void read_revisions_from_stdin(struct rev_info *revs,
1700 struct argv_array *prune)
1702 struct strbuf sb;
1703 int seen_dashdash = 0;
1704 int save_warning;
1706 save_warning = warn_on_object_refname_ambiguity;
1707 warn_on_object_refname_ambiguity = 0;
1709 strbuf_init(&sb, 1000);
1710 while (strbuf_getline(&sb, stdin) != EOF) {
1711 int len = sb.len;
1712 if (!len)
1713 break;
1714 if (sb.buf[0] == '-') {
1715 if (len == 2 && sb.buf[1] == '-') {
1716 seen_dashdash = 1;
1717 break;
1719 die("options not supported in --stdin mode");
1721 if (handle_revision_arg(sb.buf, revs, 0,
1722 REVARG_CANNOT_BE_FILENAME))
1723 die("bad revision '%s'", sb.buf);
1725 if (seen_dashdash)
1726 read_pathspec_from_stdin(revs, &sb, prune);
1728 strbuf_release(&sb);
1729 warn_on_object_refname_ambiguity = save_warning;
1732 static void add_grep(struct rev_info *revs, const char *ptn, enum grep_pat_token what)
1734 append_grep_pattern(&revs->grep_filter, ptn, "command line", 0, what);
1737 static void add_header_grep(struct rev_info *revs, enum grep_header_field field, const char *pattern)
1739 append_header_grep_pattern(&revs->grep_filter, field, pattern);
1742 static void add_message_grep(struct rev_info *revs, const char *pattern)
1744 add_grep(revs, pattern, GREP_PATTERN_BODY);
1747 static int handle_revision_opt(struct rev_info *revs, int argc, const char **argv,
1748 int *unkc, const char **unkv)
1750 const char *arg = argv[0];
1751 const char *optarg;
1752 int argcount;
1754 /* pseudo revision arguments */
1755 if (!strcmp(arg, "--all") || !strcmp(arg, "--branches") ||
1756 !strcmp(arg, "--tags") || !strcmp(arg, "--remotes") ||
1757 !strcmp(arg, "--reflog") || !strcmp(arg, "--not") ||
1758 !strcmp(arg, "--no-walk") || !strcmp(arg, "--do-walk") ||
1759 !strcmp(arg, "--bisect") || starts_with(arg, "--glob=") ||
1760 !strcmp(arg, "--indexed-objects") ||
1761 starts_with(arg, "--exclude=") ||
1762 starts_with(arg, "--branches=") || starts_with(arg, "--tags=") ||
1763 starts_with(arg, "--remotes=") || starts_with(arg, "--no-walk="))
1765 unkv[(*unkc)++] = arg;
1766 return 1;
1769 if ((argcount = parse_long_opt("max-count", argv, &optarg))) {
1770 revs->max_count = atoi(optarg);
1771 revs->no_walk = 0;
1772 return argcount;
1773 } else if ((argcount = parse_long_opt("skip", argv, &optarg))) {
1774 revs->skip_count = atoi(optarg);
1775 return argcount;
1776 } else if ((*arg == '-') && isdigit(arg[1])) {
1777 /* accept -<digit>, like traditional "head" */
1778 if (strtol_i(arg + 1, 10, &revs->max_count) < 0 ||
1779 revs->max_count < 0)
1780 die("'%s': not a non-negative integer", arg + 1);
1781 revs->no_walk = 0;
1782 } else if (!strcmp(arg, "-n")) {
1783 if (argc <= 1)
1784 return error("-n requires an argument");
1785 revs->max_count = atoi(argv[1]);
1786 revs->no_walk = 0;
1787 return 2;
1788 } else if (skip_prefix(arg, "-n", &optarg)) {
1789 revs->max_count = atoi(optarg);
1790 revs->no_walk = 0;
1791 } else if ((argcount = parse_long_opt("max-age", argv, &optarg))) {
1792 revs->max_age = atoi(optarg);
1793 return argcount;
1794 } else if ((argcount = parse_long_opt("since", argv, &optarg))) {
1795 revs->max_age = approxidate(optarg);
1796 return argcount;
1797 } else if ((argcount = parse_long_opt("after", argv, &optarg))) {
1798 revs->max_age = approxidate(optarg);
1799 return argcount;
1800 } else if ((argcount = parse_long_opt("min-age", argv, &optarg))) {
1801 revs->min_age = atoi(optarg);
1802 return argcount;
1803 } else if ((argcount = parse_long_opt("before", argv, &optarg))) {
1804 revs->min_age = approxidate(optarg);
1805 return argcount;
1806 } else if ((argcount = parse_long_opt("until", argv, &optarg))) {
1807 revs->min_age = approxidate(optarg);
1808 return argcount;
1809 } else if (!strcmp(arg, "--first-parent")) {
1810 revs->first_parent_only = 1;
1811 } else if (!strcmp(arg, "--ancestry-path")) {
1812 revs->ancestry_path = 1;
1813 revs->simplify_history = 0;
1814 revs->limited = 1;
1815 } else if (!strcmp(arg, "-g") || !strcmp(arg, "--walk-reflogs")) {
1816 init_reflog_walk(&revs->reflog_info);
1817 } else if (!strcmp(arg, "--default")) {
1818 if (argc <= 1)
1819 return error("bad --default argument");
1820 revs->def = argv[1];
1821 return 2;
1822 } else if (!strcmp(arg, "--merge")) {
1823 revs->show_merge = 1;
1824 } else if (!strcmp(arg, "--topo-order")) {
1825 revs->sort_order = REV_SORT_IN_GRAPH_ORDER;
1826 revs->topo_order = 1;
1827 } else if (!strcmp(arg, "--simplify-merges")) {
1828 revs->simplify_merges = 1;
1829 revs->topo_order = 1;
1830 revs->rewrite_parents = 1;
1831 revs->simplify_history = 0;
1832 revs->limited = 1;
1833 } else if (!strcmp(arg, "--simplify-by-decoration")) {
1834 revs->simplify_merges = 1;
1835 revs->topo_order = 1;
1836 revs->rewrite_parents = 1;
1837 revs->simplify_history = 0;
1838 revs->simplify_by_decoration = 1;
1839 revs->limited = 1;
1840 revs->prune = 1;
1841 load_ref_decorations(NULL, DECORATE_SHORT_REFS);
1842 } else if (!strcmp(arg, "--date-order")) {
1843 revs->sort_order = REV_SORT_BY_COMMIT_DATE;
1844 revs->topo_order = 1;
1845 } else if (!strcmp(arg, "--author-date-order")) {
1846 revs->sort_order = REV_SORT_BY_AUTHOR_DATE;
1847 revs->topo_order = 1;
1848 } else if (!strcmp(arg, "--early-output")) {
1849 revs->early_output = 100;
1850 revs->topo_order = 1;
1851 } else if (skip_prefix(arg, "--early-output=", &optarg)) {
1852 if (strtoul_ui(optarg, 10, &revs->early_output) < 0)
1853 die("'%s': not a non-negative integer", optarg);
1854 revs->topo_order = 1;
1855 } else if (!strcmp(arg, "--parents")) {
1856 revs->rewrite_parents = 1;
1857 revs->print_parents = 1;
1858 } else if (!strcmp(arg, "--dense")) {
1859 revs->dense = 1;
1860 } else if (!strcmp(arg, "--sparse")) {
1861 revs->dense = 0;
1862 } else if (!strcmp(arg, "--in-commit-order")) {
1863 revs->tree_blobs_in_commit_order = 1;
1864 } else if (!strcmp(arg, "--remove-empty")) {
1865 revs->remove_empty_trees = 1;
1866 } else if (!strcmp(arg, "--merges")) {
1867 revs->min_parents = 2;
1868 } else if (!strcmp(arg, "--no-merges")) {
1869 revs->max_parents = 1;
1870 } else if (skip_prefix(arg, "--min-parents=", &optarg)) {
1871 revs->min_parents = atoi(optarg);
1872 } else if (!strcmp(arg, "--no-min-parents")) {
1873 revs->min_parents = 0;
1874 } else if (skip_prefix(arg, "--max-parents=", &optarg)) {
1875 revs->max_parents = atoi(optarg);
1876 } else if (!strcmp(arg, "--no-max-parents")) {
1877 revs->max_parents = -1;
1878 } else if (!strcmp(arg, "--boundary")) {
1879 revs->boundary = 1;
1880 } else if (!strcmp(arg, "--left-right")) {
1881 revs->left_right = 1;
1882 } else if (!strcmp(arg, "--left-only")) {
1883 if (revs->right_only)
1884 die("--left-only is incompatible with --right-only"
1885 " or --cherry");
1886 revs->left_only = 1;
1887 } else if (!strcmp(arg, "--right-only")) {
1888 if (revs->left_only)
1889 die("--right-only is incompatible with --left-only");
1890 revs->right_only = 1;
1891 } else if (!strcmp(arg, "--cherry")) {
1892 if (revs->left_only)
1893 die("--cherry is incompatible with --left-only");
1894 revs->cherry_mark = 1;
1895 revs->right_only = 1;
1896 revs->max_parents = 1;
1897 revs->limited = 1;
1898 } else if (!strcmp(arg, "--count")) {
1899 revs->count = 1;
1900 } else if (!strcmp(arg, "--cherry-mark")) {
1901 if (revs->cherry_pick)
1902 die("--cherry-mark is incompatible with --cherry-pick");
1903 revs->cherry_mark = 1;
1904 revs->limited = 1; /* needs limit_list() */
1905 } else if (!strcmp(arg, "--cherry-pick")) {
1906 if (revs->cherry_mark)
1907 die("--cherry-pick is incompatible with --cherry-mark");
1908 revs->cherry_pick = 1;
1909 revs->limited = 1;
1910 } else if (!strcmp(arg, "--objects")) {
1911 revs->tag_objects = 1;
1912 revs->tree_objects = 1;
1913 revs->blob_objects = 1;
1914 } else if (!strcmp(arg, "--objects-edge")) {
1915 revs->tag_objects = 1;
1916 revs->tree_objects = 1;
1917 revs->blob_objects = 1;
1918 revs->edge_hint = 1;
1919 } else if (!strcmp(arg, "--objects-edge-aggressive")) {
1920 revs->tag_objects = 1;
1921 revs->tree_objects = 1;
1922 revs->blob_objects = 1;
1923 revs->edge_hint = 1;
1924 revs->edge_hint_aggressive = 1;
1925 } else if (!strcmp(arg, "--verify-objects")) {
1926 revs->tag_objects = 1;
1927 revs->tree_objects = 1;
1928 revs->blob_objects = 1;
1929 revs->verify_objects = 1;
1930 } else if (!strcmp(arg, "--unpacked")) {
1931 revs->unpacked = 1;
1932 } else if (starts_with(arg, "--unpacked=")) {
1933 die("--unpacked=<packfile> no longer supported.");
1934 } else if (!strcmp(arg, "-r")) {
1935 revs->diff = 1;
1936 revs->diffopt.flags.recursive = 1;
1937 } else if (!strcmp(arg, "-t")) {
1938 revs->diff = 1;
1939 revs->diffopt.flags.recursive = 1;
1940 revs->diffopt.flags.tree_in_recursive = 1;
1941 } else if (!strcmp(arg, "-m")) {
1942 revs->merge_diff_mode = MERGE_DIFF_EACH;
1943 } else if (!strcmp(arg, "-c")) {
1944 revs->merge_diff_mode = MERGE_DIFF_COMBINED;
1945 } else if (!strcmp(arg, "--cc")) {
1946 revs->merge_diff_mode = MERGE_DIFF_COMBINED_CONDENSED;
1947 } else if (!strcmp(arg, "--remerge-diff")) {
1948 revs->merge_diff_mode = MERGE_DIFF_REMERGE;
1949 } else if (!strcmp(arg, "-v")) {
1950 revs->verbose_header = 1;
1951 } else if (!strcmp(arg, "--pretty")) {
1952 revs->verbose_header = 1;
1953 revs->pretty_given = 1;
1954 get_commit_format(NULL, revs);
1955 } else if (skip_prefix(arg, "--pretty=", &optarg) ||
1956 skip_prefix(arg, "--format=", &optarg)) {
1958 * Detached form ("--pretty X" as opposed to "--pretty=X")
1959 * not allowed, since the argument is optional.
1961 revs->verbose_header = 1;
1962 revs->pretty_given = 1;
1963 get_commit_format(optarg, revs);
1964 } else if (!strcmp(arg, "--expand-tabs")) {
1965 revs->expand_tabs_in_log = 8;
1966 } else if (!strcmp(arg, "--no-expand-tabs")) {
1967 revs->expand_tabs_in_log = 0;
1968 } else if (skip_prefix(arg, "--expand-tabs=", &arg)) {
1969 int val;
1970 if (strtol_i(arg, 10, &val) < 0 || val < 0)
1971 die("'%s': not a non-negative integer", arg);
1972 revs->expand_tabs_in_log = val;
1973 } else if (!strcmp(arg, "--show-notes") || !strcmp(arg, "--notes")) {
1974 revs->show_notes = 1;
1975 revs->show_notes_given = 1;
1976 revs->notes_opt.use_default_notes = 1;
1977 } else if (!strcmp(arg, "--show-signature")) {
1978 revs->show_signature = 1;
1979 } else if (!strcmp(arg, "--no-show-signature")) {
1980 revs->show_signature = 0;
1981 } else if (!strcmp(arg, "--show-linear-break")) {
1982 revs->break_bar = " ..........";
1983 revs->track_linear = 1;
1984 revs->track_first_time = 1;
1985 } else if (skip_prefix(arg, "--show-linear-break=", &optarg)) {
1986 revs->break_bar = xstrdup(optarg);
1987 revs->track_linear = 1;
1988 revs->track_first_time = 1;
1989 } else if (skip_prefix(arg, "--show-notes=", &optarg) ||
1990 skip_prefix(arg, "--notes=", &optarg)) {
1991 struct strbuf buf = STRBUF_INIT;
1992 revs->show_notes = 1;
1993 revs->show_notes_given = 1;
1994 if (starts_with(arg, "--show-notes=") &&
1995 revs->notes_opt.use_default_notes < 0)
1996 revs->notes_opt.use_default_notes = 1;
1997 strbuf_addstr(&buf, optarg);
1998 expand_notes_ref(&buf);
1999 string_list_append(&revs->notes_opt.extra_notes_refs,
2000 strbuf_detach(&buf, NULL));
2001 } else if (!strcmp(arg, "--no-notes")) {
2002 revs->show_notes = 0;
2003 revs->show_notes_given = 1;
2004 revs->notes_opt.use_default_notes = -1;
2005 /* we have been strdup'ing ourselves, so trick
2006 * string_list into free()ing strings */
2007 revs->notes_opt.extra_notes_refs.strdup_strings = 1;
2008 string_list_clear(&revs->notes_opt.extra_notes_refs, 0);
2009 revs->notes_opt.extra_notes_refs.strdup_strings = 0;
2010 } else if (!strcmp(arg, "--standard-notes")) {
2011 revs->show_notes_given = 1;
2012 revs->notes_opt.use_default_notes = 1;
2013 } else if (!strcmp(arg, "--no-standard-notes")) {
2014 revs->notes_opt.use_default_notes = 0;
2015 } else if (!strcmp(arg, "--oneline")) {
2016 revs->verbose_header = 1;
2017 get_commit_format("oneline", revs);
2018 revs->pretty_given = 1;
2019 revs->abbrev_commit = 1;
2020 } else if (!strcmp(arg, "--graph")) {
2021 revs->topo_order = 1;
2022 revs->rewrite_parents = 1;
2023 revs->graph = graph_init(revs);
2024 } else if (!strcmp(arg, "--root")) {
2025 revs->show_root_diff = 1;
2026 } else if (!strcmp(arg, "--no-commit-id")) {
2027 revs->no_commit_id = 1;
2028 } else if (!strcmp(arg, "--always")) {
2029 revs->always_show_header = 1;
2030 } else if (!strcmp(arg, "--no-abbrev")) {
2031 revs->abbrev = 0;
2032 } else if (!strcmp(arg, "--abbrev")) {
2033 revs->abbrev = DEFAULT_ABBREV;
2034 } else if (skip_prefix(arg, "--abbrev=", &optarg)) {
2035 revs->abbrev = strtoul(optarg, NULL, 10);
2036 if (revs->abbrev < MINIMUM_ABBREV)
2037 revs->abbrev = MINIMUM_ABBREV;
2038 else if (revs->abbrev > 40)
2039 revs->abbrev = 40;
2040 } else if (!strcmp(arg, "--abbrev-commit")) {
2041 revs->abbrev_commit = 1;
2042 revs->abbrev_commit_given = 1;
2043 } else if (!strcmp(arg, "--no-abbrev-commit")) {
2044 revs->abbrev_commit = 0;
2045 } else if (!strcmp(arg, "--full-diff")) {
2046 revs->diff = 1;
2047 revs->full_diff = 1;
2048 } else if (!strcmp(arg, "--full-history")) {
2049 revs->simplify_history = 0;
2050 } else if (!strcmp(arg, "--relative-date")) {
2051 revs->date_mode.type = DATE_RELATIVE;
2052 revs->date_mode_explicit = 1;
2053 } else if ((argcount = parse_long_opt("date", argv, &optarg))) {
2054 parse_date_format(optarg, &revs->date_mode);
2055 revs->date_mode_explicit = 1;
2056 return argcount;
2057 } else if (!strcmp(arg, "--log-size")) {
2058 revs->show_log_size = 1;
2061 * Grepping the commit log
2063 else if ((argcount = parse_long_opt("author", argv, &optarg))) {
2064 add_header_grep(revs, GREP_HEADER_AUTHOR, optarg);
2065 return argcount;
2066 } else if ((argcount = parse_long_opt("committer", argv, &optarg))) {
2067 add_header_grep(revs, GREP_HEADER_COMMITTER, optarg);
2068 return argcount;
2069 } else if ((argcount = parse_long_opt("grep-reflog", argv, &optarg))) {
2070 add_header_grep(revs, GREP_HEADER_REFLOG, optarg);
2071 return argcount;
2072 } else if ((argcount = parse_long_opt("grep", argv, &optarg))) {
2073 add_message_grep(revs, optarg);
2074 return argcount;
2075 } else if (!strcmp(arg, "--grep-debug")) {
2076 revs->grep_filter.debug = 1;
2077 } else if (!strcmp(arg, "--basic-regexp")) {
2078 revs->grep_filter.pattern_type_option = GREP_PATTERN_TYPE_BRE;
2079 } else if (!strcmp(arg, "--extended-regexp") || !strcmp(arg, "-E")) {
2080 revs->grep_filter.pattern_type_option = GREP_PATTERN_TYPE_ERE;
2081 } else if (!strcmp(arg, "--regexp-ignore-case") || !strcmp(arg, "-i")) {
2082 revs->grep_filter.ignore_case = 1;
2083 revs->diffopt.pickaxe_opts |= DIFF_PICKAXE_IGNORE_CASE;
2084 } else if (!strcmp(arg, "--fixed-strings") || !strcmp(arg, "-F")) {
2085 revs->grep_filter.pattern_type_option = GREP_PATTERN_TYPE_FIXED;
2086 } else if (!strcmp(arg, "--perl-regexp") || !strcmp(arg, "-P")) {
2087 revs->grep_filter.pattern_type_option = GREP_PATTERN_TYPE_PCRE;
2088 } else if (!strcmp(arg, "--all-match")) {
2089 revs->grep_filter.all_match = 1;
2090 } else if (!strcmp(arg, "--invert-grep")) {
2091 revs->invert_grep = 1;
2092 } else if ((argcount = parse_long_opt("encoding", argv, &optarg))) {
2093 if (strcmp(optarg, "none"))
2094 git_log_output_encoding = xstrdup(optarg);
2095 else
2096 git_log_output_encoding = "";
2097 return argcount;
2098 } else if (!strcmp(arg, "--reverse")) {
2099 revs->reverse ^= 1;
2100 } else if (!strcmp(arg, "--children")) {
2101 revs->children.name = "children";
2102 revs->limited = 1;
2103 } else if (!strcmp(arg, "--ignore-missing")) {
2104 revs->ignore_missing = 1;
2105 } else if (!strcmp(arg, "--exclude-promisor-objects")) {
2106 if (fetch_if_missing)
2107 die("BUG: exclude_promisor_objects can only be used when fetch_if_missing is 0");
2108 revs->exclude_promisor_objects = 1;
2109 } else {
2110 int opts = diff_opt_parse(&revs->diffopt, argv, argc, revs->prefix);
2111 if (!opts)
2112 unkv[(*unkc)++] = arg;
2113 return opts;
2115 if (revs->graph && revs->track_linear)
2116 die("--show-linear-break and --graph are incompatible");
2118 return 1;
2121 void parse_revision_opt(struct rev_info *revs, struct parse_opt_ctx_t *ctx,
2122 const struct option *options,
2123 const char * const usagestr[])
2125 int n = handle_revision_opt(revs, ctx->argc, ctx->argv,
2126 &ctx->cpidx, ctx->out);
2127 if (n <= 0) {
2128 error("unknown option `%s'", ctx->argv[0]);
2129 usage_with_options(usagestr, options);
2131 ctx->argv += n;
2132 ctx->argc -= n;
2135 static int for_each_bisect_ref(struct ref_store *refs, each_ref_fn fn,
2136 void *cb_data, const char *term)
2138 struct strbuf bisect_refs = STRBUF_INIT;
2139 int status;
2140 strbuf_addf(&bisect_refs, "refs/bisect/%s", term);
2141 status = refs_for_each_fullref_in(refs, bisect_refs.buf, fn, cb_data, 0);
2142 strbuf_release(&bisect_refs);
2143 return status;
2146 static int for_each_bad_bisect_ref(struct ref_store *refs, each_ref_fn fn, void *cb_data)
2148 return for_each_bisect_ref(refs, fn, cb_data, term_bad);
2151 static int for_each_good_bisect_ref(struct ref_store *refs, each_ref_fn fn, void *cb_data)
2153 return for_each_bisect_ref(refs, fn, cb_data, term_good);
2156 static int handle_revision_pseudo_opt(const char *submodule,
2157 struct rev_info *revs,
2158 int argc, const char **argv, int *flags)
2160 const char *arg = argv[0];
2161 const char *optarg;
2162 struct ref_store *refs;
2163 int argcount;
2165 if (submodule) {
2167 * We need some something like get_submodule_worktrees()
2168 * before we can go through all worktrees of a submodule,
2169 * .e.g with adding all HEADs from --all, which is not
2170 * supported right now, so stick to single worktree.
2172 if (!revs->single_worktree)
2173 die("BUG: --single-worktree cannot be used together with submodule");
2174 refs = get_submodule_ref_store(submodule);
2175 } else
2176 refs = get_main_ref_store();
2179 * NOTE!
2181 * Commands like "git shortlog" will not accept the options below
2182 * unless parse_revision_opt queues them (as opposed to erroring
2183 * out).
2185 * When implementing your new pseudo-option, remember to
2186 * register it in the list at the top of handle_revision_opt.
2188 if (!strcmp(arg, "--all")) {
2189 handle_refs(refs, revs, *flags, refs_for_each_ref);
2190 handle_refs(refs, revs, *flags, refs_head_ref);
2191 if (!revs->single_worktree) {
2192 struct all_refs_cb cb;
2194 init_all_refs_cb(&cb, revs, *flags);
2195 other_head_refs(handle_one_ref, &cb);
2197 clear_ref_exclusion(&revs->ref_excludes);
2198 } else if (!strcmp(arg, "--branches")) {
2199 handle_refs(refs, revs, *flags, refs_for_each_branch_ref);
2200 clear_ref_exclusion(&revs->ref_excludes);
2201 } else if (!strcmp(arg, "--bisect")) {
2202 read_bisect_terms(&term_bad, &term_good);
2203 handle_refs(refs, revs, *flags, for_each_bad_bisect_ref);
2204 handle_refs(refs, revs, *flags ^ (UNINTERESTING | BOTTOM),
2205 for_each_good_bisect_ref);
2206 revs->bisect = 1;
2207 } else if (!strcmp(arg, "--tags")) {
2208 handle_refs(refs, revs, *flags, refs_for_each_tag_ref);
2209 clear_ref_exclusion(&revs->ref_excludes);
2210 } else if (!strcmp(arg, "--remotes")) {
2211 handle_refs(refs, revs, *flags, refs_for_each_remote_ref);
2212 clear_ref_exclusion(&revs->ref_excludes);
2213 } else if ((argcount = parse_long_opt("glob", argv, &optarg))) {
2214 struct all_refs_cb cb;
2215 init_all_refs_cb(&cb, revs, *flags);
2216 for_each_glob_ref(handle_one_ref, optarg, &cb);
2217 clear_ref_exclusion(&revs->ref_excludes);
2218 return argcount;
2219 } else if ((argcount = parse_long_opt("exclude", argv, &optarg))) {
2220 add_ref_exclusion(&revs->ref_excludes, optarg);
2221 return argcount;
2222 } else if (skip_prefix(arg, "--branches=", &optarg)) {
2223 struct all_refs_cb cb;
2224 init_all_refs_cb(&cb, revs, *flags);
2225 for_each_glob_ref_in(handle_one_ref, optarg, "refs/heads/", &cb);
2226 clear_ref_exclusion(&revs->ref_excludes);
2227 } else if (skip_prefix(arg, "--tags=", &optarg)) {
2228 struct all_refs_cb cb;
2229 init_all_refs_cb(&cb, revs, *flags);
2230 for_each_glob_ref_in(handle_one_ref, optarg, "refs/tags/", &cb);
2231 clear_ref_exclusion(&revs->ref_excludes);
2232 } else if (skip_prefix(arg, "--remotes=", &optarg)) {
2233 struct all_refs_cb cb;
2234 init_all_refs_cb(&cb, revs, *flags);
2235 for_each_glob_ref_in(handle_one_ref, optarg, "refs/remotes/", &cb);
2236 clear_ref_exclusion(&revs->ref_excludes);
2237 } else if (!strcmp(arg, "--reflog")) {
2238 add_reflogs_to_pending(revs, *flags);
2239 } else if (!strcmp(arg, "--indexed-objects")) {
2240 add_index_objects_to_pending(revs, *flags);
2241 } else if (!strcmp(arg, "--not")) {
2242 *flags ^= UNINTERESTING | BOTTOM;
2243 } else if (!strcmp(arg, "--no-walk")) {
2244 revs->no_walk = REVISION_WALK_NO_WALK_SORTED;
2245 } else if (skip_prefix(arg, "--no-walk=", &optarg)) {
2247 * Detached form ("--no-walk X" as opposed to "--no-walk=X")
2248 * not allowed, since the argument is optional.
2250 if (!strcmp(optarg, "sorted"))
2251 revs->no_walk = REVISION_WALK_NO_WALK_SORTED;
2252 else if (!strcmp(optarg, "unsorted"))
2253 revs->no_walk = REVISION_WALK_NO_WALK_UNSORTED;
2254 else
2255 return error("invalid argument to --no-walk");
2256 } else if (!strcmp(arg, "--do-walk")) {
2257 revs->no_walk = 0;
2258 } else if (!strcmp(arg, "--single-worktree")) {
2259 revs->single_worktree = 1;
2260 } else {
2261 return 0;
2264 return 1;
2267 static void NORETURN diagnose_missing_default(const char *def)
2269 int flags;
2270 const char *refname;
2272 refname = resolve_ref_unsafe(def, 0, NULL, &flags);
2273 if (!refname || !(flags & REF_ISSYMREF) || (flags & REF_ISBROKEN))
2274 die(_("your current branch appears to be broken"));
2276 skip_prefix(refname, "refs/heads/", &refname);
2277 die(_("your current branch '%s' does not have any commits yet"),
2278 refname);
2282 * Parse revision information, filling in the "rev_info" structure,
2283 * and removing the used arguments from the argument list.
2285 * Returns the number of arguments left that weren't recognized
2286 * (which are also moved to the head of the argument list)
2288 int setup_revisions(int argc, const char **argv, struct rev_info *revs, struct setup_revision_opt *opt)
2290 int i, flags, left, seen_dashdash, read_from_stdin, got_rev_arg = 0, revarg_opt;
2291 struct argv_array prune_data = ARGV_ARRAY_INIT;
2292 const char *submodule = NULL;
2294 if (opt)
2295 submodule = opt->submodule;
2297 /* First, search for "--" */
2298 if (opt && opt->assume_dashdash) {
2299 seen_dashdash = 1;
2300 } else {
2301 seen_dashdash = 0;
2302 for (i = 1; i < argc; i++) {
2303 const char *arg = argv[i];
2304 if (strcmp(arg, "--"))
2305 continue;
2306 argv[i] = NULL;
2307 argc = i;
2308 if (argv[i + 1])
2309 argv_array_pushv(&prune_data, argv + i + 1);
2310 seen_dashdash = 1;
2311 break;
2315 /* Second, deal with arguments and options */
2316 flags = 0;
2317 revarg_opt = opt ? opt->revarg_opt : 0;
2318 if (seen_dashdash)
2319 revarg_opt |= REVARG_CANNOT_BE_FILENAME;
2320 read_from_stdin = 0;
2321 for (left = i = 1; i < argc; i++) {
2322 const char *arg = argv[i];
2323 if (*arg == '-') {
2324 int opts;
2326 opts = handle_revision_pseudo_opt(submodule,
2327 revs, argc - i, argv + i,
2328 &flags);
2329 if (opts > 0) {
2330 i += opts - 1;
2331 continue;
2334 if (!strcmp(arg, "--stdin")) {
2335 if (revs->disable_stdin) {
2336 argv[left++] = arg;
2337 continue;
2339 if (read_from_stdin++)
2340 die("--stdin given twice?");
2341 read_revisions_from_stdin(revs, &prune_data);
2342 continue;
2345 opts = handle_revision_opt(revs, argc - i, argv + i, &left, argv);
2346 if (opts > 0) {
2347 i += opts - 1;
2348 continue;
2350 if (opts < 0)
2351 exit(128);
2352 continue;
2356 if (handle_revision_arg(arg, revs, flags, revarg_opt)) {
2357 int j;
2358 if (seen_dashdash || *arg == '^')
2359 die("bad revision '%s'", arg);
2361 /* If we didn't have a "--":
2362 * (1) all filenames must exist;
2363 * (2) all rev-args must not be interpretable
2364 * as a valid filename.
2365 * but the latter we have checked in the main loop.
2367 for (j = i; j < argc; j++)
2368 verify_filename(revs->prefix, argv[j], j == i);
2370 argv_array_pushv(&prune_data, argv + i);
2371 break;
2373 else
2374 got_rev_arg = 1;
2377 if (prune_data.argc) {
2379 * If we need to introduce the magic "a lone ':' means no
2380 * pathspec whatsoever", here is the place to do so.
2382 * if (prune_data.nr == 1 && !strcmp(prune_data[0], ":")) {
2383 * prune_data.nr = 0;
2384 * prune_data.alloc = 0;
2385 * free(prune_data.path);
2386 * prune_data.path = NULL;
2387 * } else {
2388 * terminate prune_data.alloc with NULL and
2389 * call init_pathspec() to set revs->prune_data here.
2392 parse_pathspec(&revs->prune_data, 0, 0,
2393 revs->prefix, prune_data.argv);
2395 argv_array_clear(&prune_data);
2397 if (revs->def == NULL)
2398 revs->def = opt ? opt->def : NULL;
2399 if (opt && opt->tweak)
2400 opt->tweak(revs, opt);
2401 if (revs->show_merge)
2402 prepare_show_merge(revs);
2403 if (revs->def && !revs->pending.nr && !revs->rev_input_given && !got_rev_arg) {
2404 struct object_id oid;
2405 struct object *object;
2406 struct object_context oc;
2407 if (get_oid_with_context(revs->def, 0, &oid, &oc))
2408 diagnose_missing_default(revs->def);
2409 object = get_reference(revs, revs->def, &oid, 0);
2410 add_pending_object_with_mode(revs, object, revs->def, oc.mode);
2413 /* Did the user ask for any diff output? Run the diff! */
2414 if (revs->diffopt.output_format & ~DIFF_FORMAT_NO_OUTPUT)
2415 revs->diff = 1;
2417 /* Pickaxe, diff-filter and rename following need diffs */
2418 if ((revs->diffopt.pickaxe_opts & DIFF_PICKAXE_KINDS_MASK) ||
2419 revs->diffopt.filter ||
2420 revs->diffopt.flags.follow_renames)
2421 revs->diff = 1;
2423 if (revs->diffopt.objfind)
2424 revs->simplify_history = 0;
2426 if (revs->topo_order)
2427 revs->limited = 1;
2429 if (revs->prune_data.nr) {
2430 copy_pathspec(&revs->pruning.pathspec, &revs->prune_data);
2431 /* Can't prune commits with rename following: the paths change.. */
2432 if (!revs->diffopt.flags.follow_renames)
2433 revs->prune = 1;
2434 if (!revs->full_diff)
2435 copy_pathspec(&revs->diffopt.pathspec,
2436 &revs->prune_data);
2438 revs->diffopt.abbrev = revs->abbrev;
2440 if (revs->line_level_traverse) {
2441 revs->limited = 1;
2442 revs->topo_order = 1;
2445 diff_setup_done(&revs->diffopt);
2447 grep_commit_pattern_type(GREP_PATTERN_TYPE_UNSPECIFIED,
2448 &revs->grep_filter);
2449 compile_grep_patterns(&revs->grep_filter);
2451 if (revs->reverse && revs->reflog_info)
2452 die("cannot combine --reverse with --walk-reflogs");
2453 if (revs->reflog_info && revs->limited)
2454 die("cannot combine --walk-reflogs with history-limiting options");
2455 if (revs->rewrite_parents && revs->children.name)
2456 die("cannot combine --parents and --children");
2459 * Limitations on the graph functionality
2461 if (revs->reverse && revs->graph)
2462 die("cannot combine --reverse with --graph");
2464 if (revs->reflog_info && revs->graph)
2465 die("cannot combine --walk-reflogs with --graph");
2466 if (revs->no_walk && revs->graph)
2467 die("cannot combine --no-walk with --graph");
2468 if (!revs->reflog_info && revs->grep_filter.use_reflog_filter)
2469 die("cannot use --grep-reflog without --walk-reflogs");
2471 if (revs->first_parent_only && revs->bisect)
2472 die(_("--first-parent is incompatible with --bisect"));
2474 if (revs->expand_tabs_in_log < 0)
2475 revs->expand_tabs_in_log = revs->expand_tabs_in_log_default;
2477 return left;
2480 static void add_child(struct rev_info *revs, struct commit *parent, struct commit *child)
2482 struct commit_list *l = xcalloc(1, sizeof(*l));
2484 l->item = child;
2485 l->next = add_decoration(&revs->children, &parent->object, l);
2488 static int remove_duplicate_parents(struct rev_info *revs, struct commit *commit)
2490 struct treesame_state *ts = lookup_decoration(&revs->treesame, &commit->object);
2491 struct commit_list **pp, *p;
2492 int surviving_parents;
2494 /* Examine existing parents while marking ones we have seen... */
2495 pp = &commit->parents;
2496 surviving_parents = 0;
2497 while ((p = *pp) != NULL) {
2498 struct commit *parent = p->item;
2499 if (parent->object.flags & TMP_MARK) {
2500 *pp = p->next;
2501 if (ts)
2502 compact_treesame(revs, commit, surviving_parents);
2503 continue;
2505 parent->object.flags |= TMP_MARK;
2506 surviving_parents++;
2507 pp = &p->next;
2509 /* clear the temporary mark */
2510 for (p = commit->parents; p; p = p->next) {
2511 p->item->object.flags &= ~TMP_MARK;
2513 /* no update_treesame() - removing duplicates can't affect TREESAME */
2514 return surviving_parents;
2517 struct merge_simplify_state {
2518 struct commit *simplified;
2521 static struct merge_simplify_state *locate_simplify_state(struct rev_info *revs, struct commit *commit)
2523 struct merge_simplify_state *st;
2525 st = lookup_decoration(&revs->merge_simplification, &commit->object);
2526 if (!st) {
2527 st = xcalloc(1, sizeof(*st));
2528 add_decoration(&revs->merge_simplification, &commit->object, st);
2530 return st;
2533 static int mark_redundant_parents(struct rev_info *revs, struct commit *commit)
2535 struct commit_list *h = reduce_heads(commit->parents);
2536 int i = 0, marked = 0;
2537 struct commit_list *po, *pn;
2539 /* Want these for sanity-checking only */
2540 int orig_cnt = commit_list_count(commit->parents);
2541 int cnt = commit_list_count(h);
2544 * Not ready to remove items yet, just mark them for now, based
2545 * on the output of reduce_heads(). reduce_heads outputs the reduced
2546 * set in its original order, so this isn't too hard.
2548 po = commit->parents;
2549 pn = h;
2550 while (po) {
2551 if (pn && po->item == pn->item) {
2552 pn = pn->next;
2553 i++;
2554 } else {
2555 po->item->object.flags |= TMP_MARK;
2556 marked++;
2558 po=po->next;
2561 if (i != cnt || cnt+marked != orig_cnt)
2562 die("mark_redundant_parents %d %d %d %d", orig_cnt, cnt, i, marked);
2564 free_commit_list(h);
2566 return marked;
2569 static int mark_treesame_root_parents(struct rev_info *revs, struct commit *commit)
2571 struct commit_list *p;
2572 int marked = 0;
2574 for (p = commit->parents; p; p = p->next) {
2575 struct commit *parent = p->item;
2576 if (!parent->parents && (parent->object.flags & TREESAME)) {
2577 parent->object.flags |= TMP_MARK;
2578 marked++;
2582 return marked;
2586 * Awkward naming - this means one parent we are TREESAME to.
2587 * cf mark_treesame_root_parents: root parents that are TREESAME (to an
2588 * empty tree). Better name suggestions?
2590 static int leave_one_treesame_to_parent(struct rev_info *revs, struct commit *commit)
2592 struct treesame_state *ts = lookup_decoration(&revs->treesame, &commit->object);
2593 struct commit *unmarked = NULL, *marked = NULL;
2594 struct commit_list *p;
2595 unsigned n;
2597 for (p = commit->parents, n = 0; p; p = p->next, n++) {
2598 if (ts->treesame[n]) {
2599 if (p->item->object.flags & TMP_MARK) {
2600 if (!marked)
2601 marked = p->item;
2602 } else {
2603 if (!unmarked) {
2604 unmarked = p->item;
2605 break;
2612 * If we are TREESAME to a marked-for-deletion parent, but not to any
2613 * unmarked parents, unmark the first TREESAME parent. This is the
2614 * parent that the default simplify_history==1 scan would have followed,
2615 * and it doesn't make sense to omit that path when asking for a
2616 * simplified full history. Retaining it improves the chances of
2617 * understanding odd missed merges that took an old version of a file.
2619 * Example:
2621 * I--------*X A modified the file, but mainline merge X used
2622 * \ / "-s ours", so took the version from I. X is
2623 * `-*A--' TREESAME to I and !TREESAME to A.
2625 * Default log from X would produce "I". Without this check,
2626 * --full-history --simplify-merges would produce "I-A-X", showing
2627 * the merge commit X and that it changed A, but not making clear that
2628 * it had just taken the I version. With this check, the topology above
2629 * is retained.
2631 * Note that it is possible that the simplification chooses a different
2632 * TREESAME parent from the default, in which case this test doesn't
2633 * activate, and we _do_ drop the default parent. Example:
2635 * I------X A modified the file, but it was reverted in B,
2636 * \ / meaning mainline merge X is TREESAME to both
2637 * *A-*B parents.
2639 * Default log would produce "I" by following the first parent;
2640 * --full-history --simplify-merges will produce "I-A-B". But this is a
2641 * reasonable result - it presents a logical full history leading from
2642 * I to X, and X is not an important merge.
2644 if (!unmarked && marked) {
2645 marked->object.flags &= ~TMP_MARK;
2646 return 1;
2649 return 0;
2652 static int remove_marked_parents(struct rev_info *revs, struct commit *commit)
2654 struct commit_list **pp, *p;
2655 int nth_parent, removed = 0;
2657 pp = &commit->parents;
2658 nth_parent = 0;
2659 while ((p = *pp) != NULL) {
2660 struct commit *parent = p->item;
2661 if (parent->object.flags & TMP_MARK) {
2662 parent->object.flags &= ~TMP_MARK;
2663 *pp = p->next;
2664 free(p);
2665 removed++;
2666 compact_treesame(revs, commit, nth_parent);
2667 continue;
2669 pp = &p->next;
2670 nth_parent++;
2673 /* Removing parents can only increase TREESAMEness */
2674 if (removed && !(commit->object.flags & TREESAME))
2675 update_treesame(revs, commit);
2677 return nth_parent;
2680 static struct commit_list **simplify_one(struct rev_info *revs, struct commit *commit, struct commit_list **tail)
2682 struct commit_list *p;
2683 struct commit *parent;
2684 struct merge_simplify_state *st, *pst;
2685 int cnt;
2687 st = locate_simplify_state(revs, commit);
2690 * Have we handled this one?
2692 if (st->simplified)
2693 return tail;
2696 * An UNINTERESTING commit simplifies to itself, so does a
2697 * root commit. We do not rewrite parents of such commit
2698 * anyway.
2700 if ((commit->object.flags & UNINTERESTING) || !commit->parents) {
2701 st->simplified = commit;
2702 return tail;
2706 * Do we know what commit all of our parents that matter
2707 * should be rewritten to? Otherwise we are not ready to
2708 * rewrite this one yet.
2710 for (cnt = 0, p = commit->parents; p; p = p->next) {
2711 pst = locate_simplify_state(revs, p->item);
2712 if (!pst->simplified) {
2713 tail = &commit_list_insert(p->item, tail)->next;
2714 cnt++;
2716 if (revs->first_parent_only)
2717 break;
2719 if (cnt) {
2720 tail = &commit_list_insert(commit, tail)->next;
2721 return tail;
2725 * Rewrite our list of parents. Note that this cannot
2726 * affect our TREESAME flags in any way - a commit is
2727 * always TREESAME to its simplification.
2729 for (p = commit->parents; p; p = p->next) {
2730 pst = locate_simplify_state(revs, p->item);
2731 p->item = pst->simplified;
2732 if (revs->first_parent_only)
2733 break;
2736 if (revs->first_parent_only)
2737 cnt = 1;
2738 else
2739 cnt = remove_duplicate_parents(revs, commit);
2742 * It is possible that we are a merge and one side branch
2743 * does not have any commit that touches the given paths;
2744 * in such a case, the immediate parent from that branch
2745 * will be rewritten to be the merge base.
2747 * o----X X: the commit we are looking at;
2748 * / / o: a commit that touches the paths;
2749 * ---o----'
2751 * Further, a merge of an independent branch that doesn't
2752 * touch the path will reduce to a treesame root parent:
2754 * ----o----X X: the commit we are looking at;
2755 * / o: a commit that touches the paths;
2756 * r r: a root commit not touching the paths
2758 * Detect and simplify both cases.
2760 if (1 < cnt) {
2761 int marked = mark_redundant_parents(revs, commit);
2762 marked += mark_treesame_root_parents(revs, commit);
2763 if (marked)
2764 marked -= leave_one_treesame_to_parent(revs, commit);
2765 if (marked)
2766 cnt = remove_marked_parents(revs, commit);
2770 * A commit simplifies to itself if it is a root, if it is
2771 * UNINTERESTING, if it touches the given paths, or if it is a
2772 * merge and its parents don't simplify to one relevant commit
2773 * (the first two cases are already handled at the beginning of
2774 * this function).
2776 * Otherwise, it simplifies to what its sole relevant parent
2777 * simplifies to.
2779 if (!cnt ||
2780 (commit->object.flags & UNINTERESTING) ||
2781 !(commit->object.flags & TREESAME) ||
2782 (parent = one_relevant_parent(revs, commit->parents)) == NULL)
2783 st->simplified = commit;
2784 else {
2785 pst = locate_simplify_state(revs, parent);
2786 st->simplified = pst->simplified;
2788 return tail;
2791 static void simplify_merges(struct rev_info *revs)
2793 struct commit_list *list, *next;
2794 struct commit_list *yet_to_do, **tail;
2795 struct commit *commit;
2797 if (!revs->prune)
2798 return;
2800 /* feed the list reversed */
2801 yet_to_do = NULL;
2802 for (list = revs->commits; list; list = next) {
2803 commit = list->item;
2804 next = list->next;
2806 * Do not free(list) here yet; the original list
2807 * is used later in this function.
2809 commit_list_insert(commit, &yet_to_do);
2811 while (yet_to_do) {
2812 list = yet_to_do;
2813 yet_to_do = NULL;
2814 tail = &yet_to_do;
2815 while (list) {
2816 commit = pop_commit(&list);
2817 tail = simplify_one(revs, commit, tail);
2821 /* clean up the result, removing the simplified ones */
2822 list = revs->commits;
2823 revs->commits = NULL;
2824 tail = &revs->commits;
2825 while (list) {
2826 struct merge_simplify_state *st;
2828 commit = pop_commit(&list);
2829 st = locate_simplify_state(revs, commit);
2830 if (st->simplified == commit)
2831 tail = &commit_list_insert(commit, tail)->next;
2835 static void set_children(struct rev_info *revs)
2837 struct commit_list *l;
2838 for (l = revs->commits; l; l = l->next) {
2839 struct commit *commit = l->item;
2840 struct commit_list *p;
2842 for (p = commit->parents; p; p = p->next)
2843 add_child(revs, p->item, commit);
2847 void reset_revision_walk(void)
2849 clear_object_flags(SEEN | ADDED | SHOWN);
2852 static int mark_uninteresting(const struct object_id *oid,
2853 struct packed_git *pack,
2854 uint32_t pos,
2855 void *unused)
2857 struct object *o = parse_object(oid);
2858 o->flags |= UNINTERESTING | SEEN;
2859 return 0;
2862 int prepare_revision_walk(struct rev_info *revs)
2864 int i;
2865 struct object_array old_pending;
2866 struct commit_list **next = &revs->commits;
2868 memcpy(&old_pending, &revs->pending, sizeof(old_pending));
2869 revs->pending.nr = 0;
2870 revs->pending.alloc = 0;
2871 revs->pending.objects = NULL;
2872 for (i = 0; i < old_pending.nr; i++) {
2873 struct object_array_entry *e = old_pending.objects + i;
2874 struct commit *commit = handle_commit(revs, e);
2875 if (commit) {
2876 if (!(commit->object.flags & SEEN)) {
2877 commit->object.flags |= SEEN;
2878 next = commit_list_append(commit, next);
2882 object_array_clear(&old_pending);
2884 /* Signal whether we need per-parent treesame decoration */
2885 if (revs->simplify_merges ||
2886 (revs->limited && limiting_can_increase_treesame(revs)))
2887 revs->treesame.name = "treesame";
2889 if (revs->exclude_promisor_objects) {
2890 for_each_packed_object(mark_uninteresting, NULL,
2891 FOR_EACH_OBJECT_PROMISOR_ONLY);
2894 if (revs->no_walk != REVISION_WALK_NO_WALK_UNSORTED)
2895 commit_list_sort_by_date(&revs->commits);
2896 if (revs->no_walk)
2897 return 0;
2898 if (revs->limited)
2899 if (limit_list(revs) < 0)
2900 return -1;
2901 if (revs->topo_order)
2902 sort_in_topological_order(&revs->commits, revs->sort_order);
2903 if (revs->line_level_traverse)
2904 line_log_filter(revs);
2905 if (revs->simplify_merges)
2906 simplify_merges(revs);
2907 if (revs->children.name)
2908 set_children(revs);
2909 return 0;
2912 static enum rewrite_result rewrite_one(struct rev_info *revs, struct commit **pp)
2914 struct commit_list *cache = NULL;
2916 for (;;) {
2917 struct commit *p = *pp;
2918 if (!revs->limited)
2919 if (add_parents_to_list(revs, p, &revs->commits, &cache) < 0)
2920 return rewrite_one_error;
2921 if (p->object.flags & UNINTERESTING)
2922 return rewrite_one_ok;
2923 if (!(p->object.flags & TREESAME))
2924 return rewrite_one_ok;
2925 if (!p->parents)
2926 return rewrite_one_noparents;
2927 if ((p = one_relevant_parent(revs, p->parents)) == NULL)
2928 return rewrite_one_ok;
2929 *pp = p;
2933 int rewrite_parents(struct rev_info *revs, struct commit *commit,
2934 rewrite_parent_fn_t rewrite_parent)
2936 struct commit_list **pp = &commit->parents;
2937 while (*pp) {
2938 struct commit_list *parent = *pp;
2939 switch (rewrite_parent(revs, &parent->item)) {
2940 case rewrite_one_ok:
2941 break;
2942 case rewrite_one_noparents:
2943 *pp = parent->next;
2944 continue;
2945 case rewrite_one_error:
2946 return -1;
2948 pp = &parent->next;
2950 remove_duplicate_parents(revs, commit);
2951 return 0;
2954 static int commit_rewrite_person(struct strbuf *buf, const char *what, struct string_list *mailmap)
2956 char *person, *endp;
2957 size_t len, namelen, maillen;
2958 const char *name;
2959 const char *mail;
2960 struct ident_split ident;
2962 person = strstr(buf->buf, what);
2963 if (!person)
2964 return 0;
2966 person += strlen(what);
2967 endp = strchr(person, '\n');
2968 if (!endp)
2969 return 0;
2971 len = endp - person;
2973 if (split_ident_line(&ident, person, len))
2974 return 0;
2976 mail = ident.mail_begin;
2977 maillen = ident.mail_end - ident.mail_begin;
2978 name = ident.name_begin;
2979 namelen = ident.name_end - ident.name_begin;
2981 if (map_user(mailmap, &mail, &maillen, &name, &namelen)) {
2982 struct strbuf namemail = STRBUF_INIT;
2984 strbuf_addf(&namemail, "%.*s <%.*s>",
2985 (int)namelen, name, (int)maillen, mail);
2987 strbuf_splice(buf, ident.name_begin - buf->buf,
2988 ident.mail_end - ident.name_begin + 1,
2989 namemail.buf, namemail.len);
2991 strbuf_release(&namemail);
2993 return 1;
2996 return 0;
2999 static int commit_match(struct commit *commit, struct rev_info *opt)
3001 int retval;
3002 const char *encoding;
3003 const char *message;
3004 struct strbuf buf = STRBUF_INIT;
3006 if (!opt->grep_filter.pattern_list && !opt->grep_filter.header_list)
3007 return 1;
3009 /* Prepend "fake" headers as needed */
3010 if (opt->grep_filter.use_reflog_filter) {
3011 strbuf_addstr(&buf, "reflog ");
3012 get_reflog_message(&buf, opt->reflog_info);
3013 strbuf_addch(&buf, '\n');
3017 * We grep in the user's output encoding, under the assumption that it
3018 * is the encoding they are most likely to write their grep pattern
3019 * for. In addition, it means we will match the "notes" encoding below,
3020 * so we will not end up with a buffer that has two different encodings
3021 * in it.
3023 encoding = get_log_output_encoding();
3024 message = logmsg_reencode(commit, NULL, encoding);
3026 /* Copy the commit to temporary if we are using "fake" headers */
3027 if (buf.len)
3028 strbuf_addstr(&buf, message);
3030 if (opt->grep_filter.header_list && opt->mailmap) {
3031 if (!buf.len)
3032 strbuf_addstr(&buf, message);
3034 commit_rewrite_person(&buf, "\nauthor ", opt->mailmap);
3035 commit_rewrite_person(&buf, "\ncommitter ", opt->mailmap);
3038 /* Append "fake" message parts as needed */
3039 if (opt->show_notes) {
3040 if (!buf.len)
3041 strbuf_addstr(&buf, message);
3042 format_display_notes(&commit->object.oid, &buf, encoding, 1);
3046 * Find either in the original commit message, or in the temporary.
3047 * Note that we cast away the constness of "message" here. It is
3048 * const because it may come from the cached commit buffer. That's OK,
3049 * because we know that it is modifiable heap memory, and that while
3050 * grep_buffer may modify it for speed, it will restore any
3051 * changes before returning.
3053 if (buf.len)
3054 retval = grep_buffer(&opt->grep_filter, buf.buf, buf.len);
3055 else
3056 retval = grep_buffer(&opt->grep_filter,
3057 (char *)message, strlen(message));
3058 strbuf_release(&buf);
3059 unuse_commit_buffer(commit, message);
3060 return opt->invert_grep ? !retval : retval;
3063 static inline int want_ancestry(const struct rev_info *revs)
3065 return (revs->rewrite_parents || revs->children.name);
3069 * Return a timestamp to be used for --since/--until comparisons for this
3070 * commit, based on the revision options.
3072 static timestamp_t comparison_date(const struct rev_info *revs,
3073 struct commit *commit)
3075 return revs->reflog_info ?
3076 get_reflog_timestamp(revs->reflog_info) :
3077 commit->date;
3080 enum commit_action get_commit_action(struct rev_info *revs, struct commit *commit)
3082 if (commit->object.flags & SHOWN)
3083 return commit_ignore;
3084 if (revs->unpacked && has_sha1_pack(commit->object.oid.hash))
3085 return commit_ignore;
3086 if (commit->object.flags & UNINTERESTING)
3087 return commit_ignore;
3088 if (revs->min_age != -1 &&
3089 comparison_date(revs, commit) > revs->min_age)
3090 return commit_ignore;
3091 if (revs->min_parents || (revs->max_parents >= 0)) {
3092 int n = commit_list_count(commit->parents);
3093 if ((n < revs->min_parents) ||
3094 ((revs->max_parents >= 0) && (n > revs->max_parents)))
3095 return commit_ignore;
3097 if (!commit_match(commit, revs))
3098 return commit_ignore;
3099 if (revs->prune && revs->dense) {
3100 /* Commit without changes? */
3101 if (commit->object.flags & TREESAME) {
3102 int n;
3103 struct commit_list *p;
3104 /* drop merges unless we want parenthood */
3105 if (!want_ancestry(revs))
3106 return commit_ignore;
3108 * If we want ancestry, then need to keep any merges
3109 * between relevant commits to tie together topology.
3110 * For consistency with TREESAME and simplification
3111 * use "relevant" here rather than just INTERESTING,
3112 * to treat bottom commit(s) as part of the topology.
3114 for (n = 0, p = commit->parents; p; p = p->next)
3115 if (relevant_commit(p->item))
3116 if (++n >= 2)
3117 return commit_show;
3118 return commit_ignore;
3121 return commit_show;
3124 define_commit_slab(saved_parents, struct commit_list *);
3126 #define EMPTY_PARENT_LIST ((struct commit_list *)-1)
3129 * You may only call save_parents() once per commit (this is checked
3130 * for non-root commits).
3132 static void save_parents(struct rev_info *revs, struct commit *commit)
3134 struct commit_list **pp;
3136 if (!revs->saved_parents_slab) {
3137 revs->saved_parents_slab = xmalloc(sizeof(struct saved_parents));
3138 init_saved_parents(revs->saved_parents_slab);
3141 pp = saved_parents_at(revs->saved_parents_slab, commit);
3144 * When walking with reflogs, we may visit the same commit
3145 * several times: once for each appearance in the reflog.
3147 * In this case, save_parents() will be called multiple times.
3148 * We want to keep only the first set of parents. We need to
3149 * store a sentinel value for an empty (i.e., NULL) parent
3150 * list to distinguish it from a not-yet-saved list, however.
3152 if (*pp)
3153 return;
3154 if (commit->parents)
3155 *pp = copy_commit_list(commit->parents);
3156 else
3157 *pp = EMPTY_PARENT_LIST;
3160 static void free_saved_parents(struct rev_info *revs)
3162 if (revs->saved_parents_slab)
3163 clear_saved_parents(revs->saved_parents_slab);
3166 struct commit_list *get_saved_parents(struct rev_info *revs, const struct commit *commit)
3168 struct commit_list *parents;
3170 if (!revs->saved_parents_slab)
3171 return commit->parents;
3173 parents = *saved_parents_at(revs->saved_parents_slab, commit);
3174 if (parents == EMPTY_PARENT_LIST)
3175 return NULL;
3176 return parents;
3179 enum commit_action simplify_commit(struct rev_info *revs, struct commit *commit)
3181 enum commit_action action = get_commit_action(revs, commit);
3183 if (action == commit_show &&
3184 revs->prune && revs->dense && want_ancestry(revs)) {
3186 * --full-diff on simplified parents is no good: it
3187 * will show spurious changes from the commits that
3188 * were elided. So we save the parents on the side
3189 * when --full-diff is in effect.
3191 if (revs->full_diff)
3192 save_parents(revs, commit);
3193 if (rewrite_parents(revs, commit, rewrite_one) < 0)
3194 return commit_error;
3196 return action;
3199 static void track_linear(struct rev_info *revs, struct commit *commit)
3201 if (revs->track_first_time) {
3202 revs->linear = 1;
3203 revs->track_first_time = 0;
3204 } else {
3205 struct commit_list *p;
3206 for (p = revs->previous_parents; p; p = p->next)
3207 if (p->item == NULL || /* first commit */
3208 !oidcmp(&p->item->object.oid, &commit->object.oid))
3209 break;
3210 revs->linear = p != NULL;
3212 if (revs->reverse) {
3213 if (revs->linear)
3214 commit->object.flags |= TRACK_LINEAR;
3216 free_commit_list(revs->previous_parents);
3217 revs->previous_parents = copy_commit_list(commit->parents);
3220 static struct commit *get_revision_1(struct rev_info *revs)
3222 while (1) {
3223 struct commit *commit;
3225 if (revs->reflog_info)
3226 commit = next_reflog_entry(revs->reflog_info);
3227 else
3228 commit = pop_commit(&revs->commits);
3230 if (!commit)
3231 return NULL;
3233 if (revs->reflog_info)
3234 commit->object.flags &= ~(ADDED | SEEN | SHOWN);
3237 * If we haven't done the list limiting, we need to look at
3238 * the parents here. We also need to do the date-based limiting
3239 * that we'd otherwise have done in limit_list().
3241 if (!revs->limited) {
3242 if (revs->max_age != -1 &&
3243 comparison_date(revs, commit) < revs->max_age)
3244 continue;
3246 if (revs->reflog_info)
3247 try_to_simplify_commit(revs, commit);
3248 else if (add_parents_to_list(revs, commit, &revs->commits, NULL) < 0) {
3249 if (!revs->ignore_missing_links)
3250 die("Failed to traverse parents of commit %s",
3251 oid_to_hex(&commit->object.oid));
3255 switch (simplify_commit(revs, commit)) {
3256 case commit_ignore:
3257 continue;
3258 case commit_error:
3259 die("Failed to simplify parents of commit %s",
3260 oid_to_hex(&commit->object.oid));
3261 default:
3262 if (revs->track_linear)
3263 track_linear(revs, commit);
3264 return commit;
3270 * Return true for entries that have not yet been shown. (This is an
3271 * object_array_each_func_t.)
3273 static int entry_unshown(struct object_array_entry *entry, void *cb_data_unused)
3275 return !(entry->item->flags & SHOWN);
3279 * If array is on the verge of a realloc, garbage-collect any entries
3280 * that have already been shown to try to free up some space.
3282 static void gc_boundary(struct object_array *array)
3284 if (array->nr == array->alloc)
3285 object_array_filter(array, entry_unshown, NULL);
3288 static void create_boundary_commit_list(struct rev_info *revs)
3290 unsigned i;
3291 struct commit *c;
3292 struct object_array *array = &revs->boundary_commits;
3293 struct object_array_entry *objects = array->objects;
3296 * If revs->commits is non-NULL at this point, an error occurred in
3297 * get_revision_1(). Ignore the error and continue printing the
3298 * boundary commits anyway. (This is what the code has always
3299 * done.)
3301 if (revs->commits) {
3302 free_commit_list(revs->commits);
3303 revs->commits = NULL;
3307 * Put all of the actual boundary commits from revs->boundary_commits
3308 * into revs->commits
3310 for (i = 0; i < array->nr; i++) {
3311 c = (struct commit *)(objects[i].item);
3312 if (!c)
3313 continue;
3314 if (!(c->object.flags & CHILD_SHOWN))
3315 continue;
3316 if (c->object.flags & (SHOWN | BOUNDARY))
3317 continue;
3318 c->object.flags |= BOUNDARY;
3319 commit_list_insert(c, &revs->commits);
3323 * If revs->topo_order is set, sort the boundary commits
3324 * in topological order
3326 sort_in_topological_order(&revs->commits, revs->sort_order);
3329 static struct commit *get_revision_internal(struct rev_info *revs)
3331 struct commit *c = NULL;
3332 struct commit_list *l;
3334 if (revs->boundary == 2) {
3336 * All of the normal commits have already been returned,
3337 * and we are now returning boundary commits.
3338 * create_boundary_commit_list() has populated
3339 * revs->commits with the remaining commits to return.
3341 c = pop_commit(&revs->commits);
3342 if (c)
3343 c->object.flags |= SHOWN;
3344 return c;
3348 * If our max_count counter has reached zero, then we are done. We
3349 * don't simply return NULL because we still might need to show
3350 * boundary commits. But we want to avoid calling get_revision_1, which
3351 * might do a considerable amount of work finding the next commit only
3352 * for us to throw it away.
3354 * If it is non-zero, then either we don't have a max_count at all
3355 * (-1), or it is still counting, in which case we decrement.
3357 if (revs->max_count) {
3358 c = get_revision_1(revs);
3359 if (c) {
3360 while (revs->skip_count > 0) {
3361 revs->skip_count--;
3362 c = get_revision_1(revs);
3363 if (!c)
3364 break;
3368 if (revs->max_count > 0)
3369 revs->max_count--;
3372 if (c)
3373 c->object.flags |= SHOWN;
3375 if (!revs->boundary)
3376 return c;
3378 if (!c) {
3380 * get_revision_1() runs out the commits, and
3381 * we are done computing the boundaries.
3382 * switch to boundary commits output mode.
3384 revs->boundary = 2;
3387 * Update revs->commits to contain the list of
3388 * boundary commits.
3390 create_boundary_commit_list(revs);
3392 return get_revision_internal(revs);
3396 * boundary commits are the commits that are parents of the
3397 * ones we got from get_revision_1() but they themselves are
3398 * not returned from get_revision_1(). Before returning
3399 * 'c', we need to mark its parents that they could be boundaries.
3402 for (l = c->parents; l; l = l->next) {
3403 struct object *p;
3404 p = &(l->item->object);
3405 if (p->flags & (CHILD_SHOWN | SHOWN))
3406 continue;
3407 p->flags |= CHILD_SHOWN;
3408 gc_boundary(&revs->boundary_commits);
3409 add_object_array(p, NULL, &revs->boundary_commits);
3412 return c;
3415 struct commit *get_revision(struct rev_info *revs)
3417 struct commit *c;
3418 struct commit_list *reversed;
3420 if (revs->reverse) {
3421 reversed = NULL;
3422 while ((c = get_revision_internal(revs)))
3423 commit_list_insert(c, &reversed);
3424 revs->commits = reversed;
3425 revs->reverse = 0;
3426 revs->reverse_output_stage = 1;
3429 if (revs->reverse_output_stage) {
3430 c = pop_commit(&revs->commits);
3431 if (revs->track_linear)
3432 revs->linear = !!(c && c->object.flags & TRACK_LINEAR);
3433 return c;
3436 c = get_revision_internal(revs);
3437 if (c && revs->graph)
3438 graph_update(revs->graph, c);
3439 if (!c) {
3440 free_saved_parents(revs);
3441 if (revs->previous_parents) {
3442 free_commit_list(revs->previous_parents);
3443 revs->previous_parents = NULL;
3446 return c;
3449 char *get_revision_mark(const struct rev_info *revs, const struct commit *commit)
3451 if (commit->object.flags & BOUNDARY)
3452 return "-";
3453 else if (commit->object.flags & UNINTERESTING)
3454 return "^";
3455 else if (commit->object.flags & PATCHSAME)
3456 return "=";
3457 else if (!revs || revs->left_right) {
3458 if (commit->object.flags & SYMMETRIC_LEFT)
3459 return "<";
3460 else
3461 return ">";
3462 } else if (revs->graph)
3463 return "*";
3464 else if (revs->cherry_mark)
3465 return "+";
3466 return "";
3469 void put_revision_mark(const struct rev_info *revs, const struct commit *commit)
3471 char *mark = get_revision_mark(revs, commit);
3472 if (!strlen(mark))
3473 return;
3474 fputs(mark, stdout);
3475 putchar(' ');