coccinelle: avoid wrong transformation suggestions from commit.cocci
[git/debian.git] / revision.c
blobd64d2a859e9750a1902994c091d02e9cd6743c2f
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 (!has_object_file(&commit->object.oid))
117 commit->object.parsed = 1;
119 if (commit->object.flags & UNINTERESTING)
120 break;
122 commit->object.flags |= UNINTERESTING;
125 * Normally we haven't parsed the parent
126 * yet, so we won't have a parent of a parent
127 * here. However, it may turn out that we've
128 * reached this commit some other way (where it
129 * wasn't uninteresting), in which case we need
130 * to mark its parents recursively too..
132 if (!commit->parents)
133 break;
135 for (l = commit->parents->next; l; l = l->next)
136 commit_list_insert(l->item, &parents);
137 commit = commit->parents->item;
142 static void add_pending_object_with_path(struct rev_info *revs,
143 struct object *obj,
144 const char *name, unsigned mode,
145 const char *path)
147 if (!obj)
148 return;
149 if (revs->no_walk && (obj->flags & UNINTERESTING))
150 revs->no_walk = 0;
151 if (revs->reflog_info && obj->type == OBJ_COMMIT) {
152 struct strbuf buf = STRBUF_INIT;
153 int len = interpret_branch_name(name, 0, &buf, 0);
155 if (0 < len && name[len] && buf.len)
156 strbuf_addstr(&buf, name + len);
157 add_reflog_for_walk(revs->reflog_info,
158 (struct commit *)obj,
159 buf.buf[0] ? buf.buf: name);
160 strbuf_release(&buf);
161 return; /* do not add the commit itself */
163 add_object_array_with_path(obj, name, &revs->pending, mode, path);
166 static void add_pending_object_with_mode(struct rev_info *revs,
167 struct object *obj,
168 const char *name, unsigned mode)
170 add_pending_object_with_path(revs, obj, name, mode, NULL);
173 void add_pending_object(struct rev_info *revs,
174 struct object *obj, const char *name)
176 add_pending_object_with_mode(revs, obj, name, S_IFINVALID);
179 void add_head_to_pending(struct rev_info *revs)
181 struct object_id oid;
182 struct object *obj;
183 if (get_oid("HEAD", &oid))
184 return;
185 obj = parse_object(&oid);
186 if (!obj)
187 return;
188 add_pending_object(revs, obj, "HEAD");
191 static struct object *get_reference(struct rev_info *revs, const char *name,
192 const struct object_id *oid,
193 unsigned int flags)
195 struct object *object;
197 object = parse_object(oid);
198 if (!object) {
199 if (revs->ignore_missing)
200 return object;
201 if (revs->exclude_promisor_objects && is_promisor_object(oid))
202 return NULL;
203 die("bad object %s", name);
205 object->flags |= flags;
206 return object;
209 void add_pending_oid(struct rev_info *revs, const char *name,
210 const struct object_id *oid, unsigned int flags)
212 struct object *object = get_reference(revs, name, oid, flags);
213 add_pending_object(revs, object, name);
216 static struct commit *handle_commit(struct rev_info *revs,
217 struct object_array_entry *entry)
219 struct object *object = entry->item;
220 const char *name = entry->name;
221 const char *path = entry->path;
222 unsigned int mode = entry->mode;
223 unsigned long flags = object->flags;
226 * Tag object? Look what it points to..
228 while (object->type == OBJ_TAG) {
229 struct tag *tag = (struct tag *) object;
230 if (revs->tag_objects && !(flags & UNINTERESTING))
231 add_pending_object(revs, object, tag->tag);
232 if (!tag->tagged)
233 die("bad tag");
234 object = parse_object(&tag->tagged->oid);
235 if (!object) {
236 if (revs->ignore_missing_links || (flags & UNINTERESTING))
237 return NULL;
238 die("bad object %s", oid_to_hex(&tag->tagged->oid));
240 object->flags |= flags;
242 * We'll handle the tagged object by looping or dropping
243 * through to the non-tag handlers below. Do not
244 * propagate path data from the tag's pending entry.
246 path = NULL;
247 mode = 0;
251 * Commit object? Just return it, we'll do all the complex
252 * reachability crud.
254 if (object->type == OBJ_COMMIT) {
255 struct commit *commit = (struct commit *)object;
256 if (parse_commit(commit) < 0)
257 die("unable to parse commit %s", name);
258 if (flags & UNINTERESTING) {
259 mark_parents_uninteresting(commit);
260 revs->limited = 1;
262 if (revs->show_source && !commit->util)
263 commit->util = xstrdup(name);
264 return commit;
268 * Tree object? Either mark it uninteresting, or add it
269 * to the list of objects to look at later..
271 if (object->type == OBJ_TREE) {
272 struct tree *tree = (struct tree *)object;
273 if (!revs->tree_objects)
274 return NULL;
275 if (flags & UNINTERESTING) {
276 mark_tree_contents_uninteresting(tree);
277 return NULL;
279 add_pending_object_with_path(revs, object, name, mode, path);
280 return NULL;
284 * Blob object? You know the drill by now..
286 if (object->type == OBJ_BLOB) {
287 if (!revs->blob_objects)
288 return NULL;
289 if (flags & UNINTERESTING)
290 return NULL;
291 add_pending_object_with_path(revs, object, name, mode, path);
292 return NULL;
294 die("%s is unknown object", name);
297 static int everybody_uninteresting(struct commit_list *orig,
298 struct commit **interesting_cache)
300 struct commit_list *list = orig;
302 if (*interesting_cache) {
303 struct commit *commit = *interesting_cache;
304 if (!(commit->object.flags & UNINTERESTING))
305 return 0;
308 while (list) {
309 struct commit *commit = list->item;
310 list = list->next;
311 if (commit->object.flags & UNINTERESTING)
312 continue;
314 *interesting_cache = commit;
315 return 0;
317 return 1;
321 * A definition of "relevant" commit that we can use to simplify limited graphs
322 * by eliminating side branches.
324 * A "relevant" commit is one that is !UNINTERESTING (ie we are including it
325 * in our list), or that is a specified BOTTOM commit. Then after computing
326 * a limited list, during processing we can generally ignore boundary merges
327 * coming from outside the graph, (ie from irrelevant parents), and treat
328 * those merges as if they were single-parent. TREESAME is defined to consider
329 * only relevant parents, if any. If we are TREESAME to our on-graph parents,
330 * we don't care if we were !TREESAME to non-graph parents.
332 * Treating bottom commits as relevant ensures that a limited graph's
333 * connection to the actual bottom commit is not viewed as a side branch, but
334 * treated as part of the graph. For example:
336 * ....Z...A---X---o---o---B
337 * . /
338 * W---Y
340 * When computing "A..B", the A-X connection is at least as important as
341 * Y-X, despite A being flagged UNINTERESTING.
343 * And when computing --ancestry-path "A..B", the A-X connection is more
344 * important than Y-X, despite both A and Y being flagged UNINTERESTING.
346 static inline int relevant_commit(struct commit *commit)
348 return (commit->object.flags & (UNINTERESTING | BOTTOM)) != UNINTERESTING;
352 * Return a single relevant commit from a parent list. If we are a TREESAME
353 * commit, and this selects one of our parents, then we can safely simplify to
354 * that parent.
356 static struct commit *one_relevant_parent(const struct rev_info *revs,
357 struct commit_list *orig)
359 struct commit_list *list = orig;
360 struct commit *relevant = NULL;
362 if (!orig)
363 return NULL;
366 * For 1-parent commits, or if first-parent-only, then return that
367 * first parent (even if not "relevant" by the above definition).
368 * TREESAME will have been set purely on that parent.
370 if (revs->first_parent_only || !orig->next)
371 return orig->item;
374 * For multi-parent commits, identify a sole relevant parent, if any.
375 * If we have only one relevant parent, then TREESAME will be set purely
376 * with regard to that parent, and we can simplify accordingly.
378 * If we have more than one relevant parent, or no relevant parents
379 * (and multiple irrelevant ones), then we can't select a parent here
380 * and return NULL.
382 while (list) {
383 struct commit *commit = list->item;
384 list = list->next;
385 if (relevant_commit(commit)) {
386 if (relevant)
387 return NULL;
388 relevant = commit;
391 return relevant;
395 * The goal is to get REV_TREE_NEW as the result only if the
396 * diff consists of all '+' (and no other changes), REV_TREE_OLD
397 * if the whole diff is removal of old data, and otherwise
398 * REV_TREE_DIFFERENT (of course if the trees are the same we
399 * want REV_TREE_SAME).
401 * The only time we care about the distinction is when
402 * remove_empty_trees is in effect, in which case we care only about
403 * whether the whole change is REV_TREE_NEW, or if there's another type
404 * of change. Which means we can stop the diff early in either of these
405 * cases:
407 * 1. We're not using remove_empty_trees at all.
409 * 2. We saw anything except REV_TREE_NEW.
411 static int tree_difference = REV_TREE_SAME;
413 static void file_add_remove(struct diff_options *options,
414 int addremove, unsigned mode,
415 const struct object_id *oid,
416 int oid_valid,
417 const char *fullpath, unsigned dirty_submodule)
419 int diff = addremove == '+' ? REV_TREE_NEW : REV_TREE_OLD;
420 struct rev_info *revs = options->change_fn_data;
422 tree_difference |= diff;
423 if (!revs->remove_empty_trees || tree_difference != REV_TREE_NEW)
424 options->flags.has_changes = 1;
427 static void file_change(struct diff_options *options,
428 unsigned old_mode, unsigned new_mode,
429 const struct object_id *old_oid,
430 const struct object_id *new_oid,
431 int old_oid_valid, int new_oid_valid,
432 const char *fullpath,
433 unsigned old_dirty_submodule, unsigned new_dirty_submodule)
435 tree_difference = REV_TREE_DIFFERENT;
436 options->flags.has_changes = 1;
439 static int rev_compare_tree(struct rev_info *revs,
440 struct commit *parent, struct commit *commit)
442 struct tree *t1 = get_commit_tree(parent);
443 struct tree *t2 = get_commit_tree(commit);
445 if (!t1)
446 return REV_TREE_NEW;
447 if (!t2)
448 return REV_TREE_OLD;
450 if (revs->simplify_by_decoration) {
452 * If we are simplifying by decoration, then the commit
453 * is worth showing if it has a tag pointing at it.
455 if (get_name_decoration(&commit->object))
456 return REV_TREE_DIFFERENT;
458 * A commit that is not pointed by a tag is uninteresting
459 * if we are not limited by path. This means that you will
460 * see the usual "commits that touch the paths" plus any
461 * tagged commit by specifying both --simplify-by-decoration
462 * and pathspec.
464 if (!revs->prune_data.nr)
465 return REV_TREE_SAME;
468 tree_difference = REV_TREE_SAME;
469 revs->pruning.flags.has_changes = 0;
470 if (diff_tree_oid(&t1->object.oid, &t2->object.oid, "",
471 &revs->pruning) < 0)
472 return REV_TREE_DIFFERENT;
473 return tree_difference;
476 static int rev_same_tree_as_empty(struct rev_info *revs, struct commit *commit)
478 int retval;
479 struct tree *t1 = get_commit_tree(commit);
481 if (!t1)
482 return 0;
484 tree_difference = REV_TREE_SAME;
485 revs->pruning.flags.has_changes = 0;
486 retval = diff_tree_oid(NULL, &t1->object.oid, "", &revs->pruning);
488 return retval >= 0 && (tree_difference == REV_TREE_SAME);
491 struct treesame_state {
492 unsigned int nparents;
493 unsigned char treesame[FLEX_ARRAY];
496 static struct treesame_state *initialise_treesame(struct rev_info *revs, struct commit *commit)
498 unsigned n = commit_list_count(commit->parents);
499 struct treesame_state *st = xcalloc(1, st_add(sizeof(*st), n));
500 st->nparents = n;
501 add_decoration(&revs->treesame, &commit->object, st);
502 return st;
506 * Must be called immediately after removing the nth_parent from a commit's
507 * parent list, if we are maintaining the per-parent treesame[] decoration.
508 * This does not recalculate the master TREESAME flag - update_treesame()
509 * should be called to update it after a sequence of treesame[] modifications
510 * that may have affected it.
512 static int compact_treesame(struct rev_info *revs, struct commit *commit, unsigned nth_parent)
514 struct treesame_state *st;
515 int old_same;
517 if (!commit->parents) {
519 * Have just removed the only parent from a non-merge.
520 * Different handling, as we lack decoration.
522 if (nth_parent != 0)
523 die("compact_treesame %u", nth_parent);
524 old_same = !!(commit->object.flags & TREESAME);
525 if (rev_same_tree_as_empty(revs, commit))
526 commit->object.flags |= TREESAME;
527 else
528 commit->object.flags &= ~TREESAME;
529 return old_same;
532 st = lookup_decoration(&revs->treesame, &commit->object);
533 if (!st || nth_parent >= st->nparents)
534 die("compact_treesame %u", nth_parent);
536 old_same = st->treesame[nth_parent];
537 memmove(st->treesame + nth_parent,
538 st->treesame + nth_parent + 1,
539 st->nparents - nth_parent - 1);
542 * If we've just become a non-merge commit, update TREESAME
543 * immediately, and remove the no-longer-needed decoration.
544 * If still a merge, defer update until update_treesame().
546 if (--st->nparents == 1) {
547 if (commit->parents->next)
548 die("compact_treesame parents mismatch");
549 if (st->treesame[0] && revs->dense)
550 commit->object.flags |= TREESAME;
551 else
552 commit->object.flags &= ~TREESAME;
553 free(add_decoration(&revs->treesame, &commit->object, NULL));
556 return old_same;
559 static unsigned update_treesame(struct rev_info *revs, struct commit *commit)
561 if (commit->parents && commit->parents->next) {
562 unsigned n;
563 struct treesame_state *st;
564 struct commit_list *p;
565 unsigned relevant_parents;
566 unsigned relevant_change, irrelevant_change;
568 st = lookup_decoration(&revs->treesame, &commit->object);
569 if (!st)
570 die("update_treesame %s", oid_to_hex(&commit->object.oid));
571 relevant_parents = 0;
572 relevant_change = irrelevant_change = 0;
573 for (p = commit->parents, n = 0; p; n++, p = p->next) {
574 if (relevant_commit(p->item)) {
575 relevant_change |= !st->treesame[n];
576 relevant_parents++;
577 } else
578 irrelevant_change |= !st->treesame[n];
580 if (relevant_parents ? relevant_change : irrelevant_change)
581 commit->object.flags &= ~TREESAME;
582 else
583 commit->object.flags |= TREESAME;
586 return commit->object.flags & TREESAME;
589 static inline int limiting_can_increase_treesame(const struct rev_info *revs)
592 * TREESAME is irrelevant unless prune && dense;
593 * if simplify_history is set, we can't have a mixture of TREESAME and
594 * !TREESAME INTERESTING parents (and we don't have treesame[]
595 * decoration anyway);
596 * if first_parent_only is set, then the TREESAME flag is locked
597 * against the first parent (and again we lack treesame[] decoration).
599 return revs->prune && revs->dense &&
600 !revs->simplify_history &&
601 !revs->first_parent_only;
604 static void try_to_simplify_commit(struct rev_info *revs, struct commit *commit)
606 struct commit_list **pp, *parent;
607 struct treesame_state *ts = NULL;
608 int relevant_change = 0, irrelevant_change = 0;
609 int relevant_parents, nth_parent;
612 * If we don't do pruning, everything is interesting
614 if (!revs->prune)
615 return;
617 if (!get_commit_tree(commit))
618 return;
620 if (!commit->parents) {
621 if (rev_same_tree_as_empty(revs, commit))
622 commit->object.flags |= TREESAME;
623 return;
627 * Normal non-merge commit? If we don't want to make the
628 * history dense, we consider it always to be a change..
630 if (!revs->dense && !commit->parents->next)
631 return;
633 for (pp = &commit->parents, nth_parent = 0, relevant_parents = 0;
634 (parent = *pp) != NULL;
635 pp = &parent->next, nth_parent++) {
636 struct commit *p = parent->item;
637 if (relevant_commit(p))
638 relevant_parents++;
640 if (nth_parent == 1) {
642 * This our second loop iteration - so we now know
643 * we're dealing with a merge.
645 * Do not compare with later parents when we care only about
646 * the first parent chain, in order to avoid derailing the
647 * traversal to follow a side branch that brought everything
648 * in the path we are limited to by the pathspec.
650 if (revs->first_parent_only)
651 break;
653 * If this will remain a potentially-simplifiable
654 * merge, remember per-parent treesame if needed.
655 * Initialise the array with the comparison from our
656 * first iteration.
658 if (revs->treesame.name &&
659 !revs->simplify_history &&
660 !(commit->object.flags & UNINTERESTING)) {
661 ts = initialise_treesame(revs, commit);
662 if (!(irrelevant_change || relevant_change))
663 ts->treesame[0] = 1;
666 if (parse_commit(p) < 0)
667 die("cannot simplify commit %s (because of %s)",
668 oid_to_hex(&commit->object.oid),
669 oid_to_hex(&p->object.oid));
670 switch (rev_compare_tree(revs, p, commit)) {
671 case REV_TREE_SAME:
672 if (!revs->simplify_history || !relevant_commit(p)) {
673 /* Even if a merge with an uninteresting
674 * side branch brought the entire change
675 * we are interested in, we do not want
676 * to lose the other branches of this
677 * merge, so we just keep going.
679 if (ts)
680 ts->treesame[nth_parent] = 1;
681 continue;
683 parent->next = NULL;
684 commit->parents = parent;
685 commit->object.flags |= TREESAME;
686 return;
688 case REV_TREE_NEW:
689 if (revs->remove_empty_trees &&
690 rev_same_tree_as_empty(revs, p)) {
691 /* We are adding all the specified
692 * paths from this parent, so the
693 * history beyond this parent is not
694 * interesting. Remove its parents
695 * (they are grandparents for us).
696 * IOW, we pretend this parent is a
697 * "root" commit.
699 if (parse_commit(p) < 0)
700 die("cannot simplify commit %s (invalid %s)",
701 oid_to_hex(&commit->object.oid),
702 oid_to_hex(&p->object.oid));
703 p->parents = NULL;
705 /* fallthrough */
706 case REV_TREE_OLD:
707 case REV_TREE_DIFFERENT:
708 if (relevant_commit(p))
709 relevant_change = 1;
710 else
711 irrelevant_change = 1;
712 continue;
714 die("bad tree compare for commit %s", oid_to_hex(&commit->object.oid));
718 * TREESAME is straightforward for single-parent commits. For merge
719 * commits, it is most useful to define it so that "irrelevant"
720 * parents cannot make us !TREESAME - if we have any relevant
721 * parents, then we only consider TREESAMEness with respect to them,
722 * allowing irrelevant merges from uninteresting branches to be
723 * simplified away. Only if we have only irrelevant parents do we
724 * base TREESAME on them. Note that this logic is replicated in
725 * update_treesame, which should be kept in sync.
727 if (relevant_parents ? !relevant_change : !irrelevant_change)
728 commit->object.flags |= TREESAME;
731 static void commit_list_insert_by_date_cached(struct commit *p, struct commit_list **head,
732 struct commit_list *cached_base, struct commit_list **cache)
734 struct commit_list *new_entry;
736 if (cached_base && p->date < cached_base->item->date)
737 new_entry = commit_list_insert_by_date(p, &cached_base->next);
738 else
739 new_entry = commit_list_insert_by_date(p, head);
741 if (cache && (!*cache || p->date < (*cache)->item->date))
742 *cache = new_entry;
745 static int add_parents_to_list(struct rev_info *revs, struct commit *commit,
746 struct commit_list **list, struct commit_list **cache_ptr)
748 struct commit_list *parent = commit->parents;
749 unsigned left_flag;
750 struct commit_list *cached_base = cache_ptr ? *cache_ptr : NULL;
752 if (commit->object.flags & ADDED)
753 return 0;
754 commit->object.flags |= ADDED;
756 if (revs->include_check &&
757 !revs->include_check(commit, revs->include_check_data))
758 return 0;
761 * If the commit is uninteresting, don't try to
762 * prune parents - we want the maximal uninteresting
763 * set.
765 * Normally we haven't parsed the parent
766 * yet, so we won't have a parent of a parent
767 * here. However, it may turn out that we've
768 * reached this commit some other way (where it
769 * wasn't uninteresting), in which case we need
770 * to mark its parents recursively too..
772 if (commit->object.flags & UNINTERESTING) {
773 while (parent) {
774 struct commit *p = parent->item;
775 parent = parent->next;
776 if (p)
777 p->object.flags |= UNINTERESTING;
778 if (parse_commit_gently(p, 1) < 0)
779 continue;
780 if (p->parents)
781 mark_parents_uninteresting(p);
782 if (p->object.flags & SEEN)
783 continue;
784 p->object.flags |= SEEN;
785 commit_list_insert_by_date_cached(p, list, cached_base, cache_ptr);
787 return 0;
791 * Ok, the commit wasn't uninteresting. Try to
792 * simplify the commit history and find the parent
793 * that has no differences in the path set if one exists.
795 try_to_simplify_commit(revs, commit);
797 if (revs->no_walk)
798 return 0;
800 left_flag = (commit->object.flags & SYMMETRIC_LEFT);
802 for (parent = commit->parents; parent; parent = parent->next) {
803 struct commit *p = parent->item;
804 int gently = revs->ignore_missing_links ||
805 revs->exclude_promisor_objects;
806 if (parse_commit_gently(p, gently) < 0) {
807 if (revs->exclude_promisor_objects &&
808 is_promisor_object(&p->object.oid)) {
809 if (revs->first_parent_only)
810 break;
811 continue;
813 return -1;
815 if (revs->show_source && !p->util)
816 p->util = commit->util;
817 p->object.flags |= left_flag;
818 if (!(p->object.flags & SEEN)) {
819 p->object.flags |= SEEN;
820 commit_list_insert_by_date_cached(p, list, cached_base, cache_ptr);
822 if (revs->first_parent_only)
823 break;
825 return 0;
828 static void cherry_pick_list(struct commit_list *list, struct rev_info *revs)
830 struct commit_list *p;
831 int left_count = 0, right_count = 0;
832 int left_first;
833 struct patch_ids ids;
834 unsigned cherry_flag;
836 /* First count the commits on the left and on the right */
837 for (p = list; p; p = p->next) {
838 struct commit *commit = p->item;
839 unsigned flags = commit->object.flags;
840 if (flags & BOUNDARY)
842 else if (flags & SYMMETRIC_LEFT)
843 left_count++;
844 else
845 right_count++;
848 if (!left_count || !right_count)
849 return;
851 left_first = left_count < right_count;
852 init_patch_ids(&ids);
853 ids.diffopts.pathspec = revs->diffopt.pathspec;
855 /* Compute patch-ids for one side */
856 for (p = list; p; p = p->next) {
857 struct commit *commit = p->item;
858 unsigned flags = commit->object.flags;
860 if (flags & BOUNDARY)
861 continue;
863 * If we have fewer left, left_first is set and we omit
864 * commits on the right branch in this loop. If we have
865 * fewer right, we skip the left ones.
867 if (left_first != !!(flags & SYMMETRIC_LEFT))
868 continue;
869 add_commit_patch_id(commit, &ids);
872 /* either cherry_mark or cherry_pick are true */
873 cherry_flag = revs->cherry_mark ? PATCHSAME : SHOWN;
875 /* Check the other side */
876 for (p = list; p; p = p->next) {
877 struct commit *commit = p->item;
878 struct patch_id *id;
879 unsigned flags = commit->object.flags;
881 if (flags & BOUNDARY)
882 continue;
884 * If we have fewer left, left_first is set and we omit
885 * commits on the left branch in this loop.
887 if (left_first == !!(flags & SYMMETRIC_LEFT))
888 continue;
891 * Have we seen the same patch id?
893 id = has_commit_patch_id(commit, &ids);
894 if (!id)
895 continue;
897 commit->object.flags |= cherry_flag;
898 id->commit->object.flags |= cherry_flag;
901 free_patch_ids(&ids);
904 /* How many extra uninteresting commits we want to see.. */
905 #define SLOP 5
907 static int still_interesting(struct commit_list *src, timestamp_t date, int slop,
908 struct commit **interesting_cache)
911 * No source list at all? We're definitely done..
913 if (!src)
914 return 0;
917 * Does the destination list contain entries with a date
918 * before the source list? Definitely _not_ done.
920 if (date <= src->item->date)
921 return SLOP;
924 * Does the source list still have interesting commits in
925 * it? Definitely not done..
927 if (!everybody_uninteresting(src, interesting_cache))
928 return SLOP;
930 /* Ok, we're closing in.. */
931 return slop-1;
935 * "rev-list --ancestry-path A..B" computes commits that are ancestors
936 * of B but not ancestors of A but further limits the result to those
937 * that are descendants of A. This takes the list of bottom commits and
938 * the result of "A..B" without --ancestry-path, and limits the latter
939 * further to the ones that can reach one of the commits in "bottom".
941 static void limit_to_ancestry(struct commit_list *bottom, struct commit_list *list)
943 struct commit_list *p;
944 struct commit_list *rlist = NULL;
945 int made_progress;
948 * Reverse the list so that it will be likely that we would
949 * process parents before children.
951 for (p = list; p; p = p->next)
952 commit_list_insert(p->item, &rlist);
954 for (p = bottom; p; p = p->next)
955 p->item->object.flags |= TMP_MARK;
958 * Mark the ones that can reach bottom commits in "list",
959 * in a bottom-up fashion.
961 do {
962 made_progress = 0;
963 for (p = rlist; p; p = p->next) {
964 struct commit *c = p->item;
965 struct commit_list *parents;
966 if (c->object.flags & (TMP_MARK | UNINTERESTING))
967 continue;
968 for (parents = c->parents;
969 parents;
970 parents = parents->next) {
971 if (!(parents->item->object.flags & TMP_MARK))
972 continue;
973 c->object.flags |= TMP_MARK;
974 made_progress = 1;
975 break;
978 } while (made_progress);
981 * NEEDSWORK: decide if we want to remove parents that are
982 * not marked with TMP_MARK from commit->parents for commits
983 * in the resulting list. We may not want to do that, though.
987 * The ones that are not marked with TMP_MARK are uninteresting
989 for (p = list; p; p = p->next) {
990 struct commit *c = p->item;
991 if (c->object.flags & TMP_MARK)
992 continue;
993 c->object.flags |= UNINTERESTING;
996 /* We are done with the TMP_MARK */
997 for (p = list; p; p = p->next)
998 p->item->object.flags &= ~TMP_MARK;
999 for (p = bottom; p; p = p->next)
1000 p->item->object.flags &= ~TMP_MARK;
1001 free_commit_list(rlist);
1005 * Before walking the history, keep the set of "negative" refs the
1006 * caller has asked to exclude.
1008 * This is used to compute "rev-list --ancestry-path A..B", as we need
1009 * to filter the result of "A..B" further to the ones that can actually
1010 * reach A.
1012 static struct commit_list *collect_bottom_commits(struct commit_list *list)
1014 struct commit_list *elem, *bottom = NULL;
1015 for (elem = list; elem; elem = elem->next)
1016 if (elem->item->object.flags & BOTTOM)
1017 commit_list_insert(elem->item, &bottom);
1018 return bottom;
1021 /* Assumes either left_only or right_only is set */
1022 static void limit_left_right(struct commit_list *list, struct rev_info *revs)
1024 struct commit_list *p;
1026 for (p = list; p; p = p->next) {
1027 struct commit *commit = p->item;
1029 if (revs->right_only) {
1030 if (commit->object.flags & SYMMETRIC_LEFT)
1031 commit->object.flags |= SHOWN;
1032 } else /* revs->left_only is set */
1033 if (!(commit->object.flags & SYMMETRIC_LEFT))
1034 commit->object.flags |= SHOWN;
1038 static int limit_list(struct rev_info *revs)
1040 int slop = SLOP;
1041 timestamp_t date = TIME_MAX;
1042 struct commit_list *list = revs->commits;
1043 struct commit_list *newlist = NULL;
1044 struct commit_list **p = &newlist;
1045 struct commit_list *bottom = NULL;
1046 struct commit *interesting_cache = NULL;
1048 if (revs->ancestry_path) {
1049 bottom = collect_bottom_commits(list);
1050 if (!bottom)
1051 die("--ancestry-path given but there are no bottom commits");
1054 while (list) {
1055 struct commit *commit = pop_commit(&list);
1056 struct object *obj = &commit->object;
1057 show_early_output_fn_t show;
1059 if (commit == interesting_cache)
1060 interesting_cache = NULL;
1062 if (revs->max_age != -1 && (commit->date < revs->max_age))
1063 obj->flags |= UNINTERESTING;
1064 if (add_parents_to_list(revs, commit, &list, NULL) < 0)
1065 return -1;
1066 if (obj->flags & UNINTERESTING) {
1067 mark_parents_uninteresting(commit);
1068 slop = still_interesting(list, date, slop, &interesting_cache);
1069 if (slop)
1070 continue;
1071 break;
1073 if (revs->min_age != -1 && (commit->date > revs->min_age))
1074 continue;
1075 date = commit->date;
1076 p = &commit_list_insert(commit, p)->next;
1078 show = show_early_output;
1079 if (!show)
1080 continue;
1082 show(revs, newlist);
1083 show_early_output = NULL;
1085 if (revs->cherry_pick || revs->cherry_mark)
1086 cherry_pick_list(newlist, revs);
1088 if (revs->left_only || revs->right_only)
1089 limit_left_right(newlist, revs);
1091 if (bottom) {
1092 limit_to_ancestry(bottom, newlist);
1093 free_commit_list(bottom);
1097 * Check if any commits have become TREESAME by some of their parents
1098 * becoming UNINTERESTING.
1100 if (limiting_can_increase_treesame(revs))
1101 for (list = newlist; list; list = list->next) {
1102 struct commit *c = list->item;
1103 if (c->object.flags & (UNINTERESTING | TREESAME))
1104 continue;
1105 update_treesame(revs, c);
1108 revs->commits = newlist;
1109 return 0;
1113 * Add an entry to refs->cmdline with the specified information.
1114 * *name is copied.
1116 static void add_rev_cmdline(struct rev_info *revs,
1117 struct object *item,
1118 const char *name,
1119 int whence,
1120 unsigned flags)
1122 struct rev_cmdline_info *info = &revs->cmdline;
1123 unsigned int nr = info->nr;
1125 ALLOC_GROW(info->rev, nr + 1, info->alloc);
1126 info->rev[nr].item = item;
1127 info->rev[nr].name = xstrdup(name);
1128 info->rev[nr].whence = whence;
1129 info->rev[nr].flags = flags;
1130 info->nr++;
1133 static void add_rev_cmdline_list(struct rev_info *revs,
1134 struct commit_list *commit_list,
1135 int whence,
1136 unsigned flags)
1138 while (commit_list) {
1139 struct object *object = &commit_list->item->object;
1140 add_rev_cmdline(revs, object, oid_to_hex(&object->oid),
1141 whence, flags);
1142 commit_list = commit_list->next;
1146 struct all_refs_cb {
1147 int all_flags;
1148 int warned_bad_reflog;
1149 struct rev_info *all_revs;
1150 const char *name_for_errormsg;
1151 struct ref_store *refs;
1154 int ref_excluded(struct string_list *ref_excludes, const char *path)
1156 struct string_list_item *item;
1158 if (!ref_excludes)
1159 return 0;
1160 for_each_string_list_item(item, ref_excludes) {
1161 if (!wildmatch(item->string, path, 0))
1162 return 1;
1164 return 0;
1167 static int handle_one_ref(const char *path, const struct object_id *oid,
1168 int flag, void *cb_data)
1170 struct all_refs_cb *cb = cb_data;
1171 struct object *object;
1173 if (ref_excluded(cb->all_revs->ref_excludes, path))
1174 return 0;
1176 object = get_reference(cb->all_revs, path, oid, cb->all_flags);
1177 add_rev_cmdline(cb->all_revs, object, path, REV_CMD_REF, cb->all_flags);
1178 add_pending_oid(cb->all_revs, path, oid, cb->all_flags);
1179 return 0;
1182 static void init_all_refs_cb(struct all_refs_cb *cb, struct rev_info *revs,
1183 unsigned flags)
1185 cb->all_revs = revs;
1186 cb->all_flags = flags;
1187 revs->rev_input_given = 1;
1188 cb->refs = NULL;
1191 void clear_ref_exclusion(struct string_list **ref_excludes_p)
1193 if (*ref_excludes_p) {
1194 string_list_clear(*ref_excludes_p, 0);
1195 free(*ref_excludes_p);
1197 *ref_excludes_p = NULL;
1200 void add_ref_exclusion(struct string_list **ref_excludes_p, const char *exclude)
1202 if (!*ref_excludes_p) {
1203 *ref_excludes_p = xcalloc(1, sizeof(**ref_excludes_p));
1204 (*ref_excludes_p)->strdup_strings = 1;
1206 string_list_append(*ref_excludes_p, exclude);
1209 static void handle_refs(struct ref_store *refs,
1210 struct rev_info *revs, unsigned flags,
1211 int (*for_each)(struct ref_store *, each_ref_fn, void *))
1213 struct all_refs_cb cb;
1215 if (!refs) {
1216 /* this could happen with uninitialized submodules */
1217 return;
1220 init_all_refs_cb(&cb, revs, flags);
1221 for_each(refs, handle_one_ref, &cb);
1224 static void handle_one_reflog_commit(struct object_id *oid, void *cb_data)
1226 struct all_refs_cb *cb = cb_data;
1227 if (!is_null_oid(oid)) {
1228 struct object *o = parse_object(oid);
1229 if (o) {
1230 o->flags |= cb->all_flags;
1231 /* ??? CMDLINEFLAGS ??? */
1232 add_pending_object(cb->all_revs, o, "");
1234 else if (!cb->warned_bad_reflog) {
1235 warning("reflog of '%s' references pruned commits",
1236 cb->name_for_errormsg);
1237 cb->warned_bad_reflog = 1;
1242 static int handle_one_reflog_ent(struct object_id *ooid, struct object_id *noid,
1243 const char *email, timestamp_t timestamp, int tz,
1244 const char *message, void *cb_data)
1246 handle_one_reflog_commit(ooid, cb_data);
1247 handle_one_reflog_commit(noid, cb_data);
1248 return 0;
1251 static int handle_one_reflog(const char *path, const struct object_id *oid,
1252 int flag, void *cb_data)
1254 struct all_refs_cb *cb = cb_data;
1255 cb->warned_bad_reflog = 0;
1256 cb->name_for_errormsg = path;
1257 refs_for_each_reflog_ent(cb->refs, path,
1258 handle_one_reflog_ent, cb_data);
1259 return 0;
1262 static void add_other_reflogs_to_pending(struct all_refs_cb *cb)
1264 struct worktree **worktrees, **p;
1266 worktrees = get_worktrees(0);
1267 for (p = worktrees; *p; p++) {
1268 struct worktree *wt = *p;
1270 if (wt->is_current)
1271 continue;
1273 cb->refs = get_worktree_ref_store(wt);
1274 refs_for_each_reflog(cb->refs,
1275 handle_one_reflog,
1276 cb);
1278 free_worktrees(worktrees);
1281 void add_reflogs_to_pending(struct rev_info *revs, unsigned flags)
1283 struct all_refs_cb cb;
1285 cb.all_revs = revs;
1286 cb.all_flags = flags;
1287 cb.refs = get_main_ref_store();
1288 for_each_reflog(handle_one_reflog, &cb);
1290 if (!revs->single_worktree)
1291 add_other_reflogs_to_pending(&cb);
1294 static void add_cache_tree(struct cache_tree *it, struct rev_info *revs,
1295 struct strbuf *path)
1297 size_t baselen = path->len;
1298 int i;
1300 if (it->entry_count >= 0) {
1301 struct tree *tree = lookup_tree(&it->oid);
1302 add_pending_object_with_path(revs, &tree->object, "",
1303 040000, path->buf);
1306 for (i = 0; i < it->subtree_nr; i++) {
1307 struct cache_tree_sub *sub = it->down[i];
1308 strbuf_addf(path, "%s%s", baselen ? "/" : "", sub->name);
1309 add_cache_tree(sub->cache_tree, revs, path);
1310 strbuf_setlen(path, baselen);
1315 static void do_add_index_objects_to_pending(struct rev_info *revs,
1316 struct index_state *istate)
1318 int i;
1320 for (i = 0; i < istate->cache_nr; i++) {
1321 struct cache_entry *ce = istate->cache[i];
1322 struct blob *blob;
1324 if (S_ISGITLINK(ce->ce_mode))
1325 continue;
1327 blob = lookup_blob(&ce->oid);
1328 if (!blob)
1329 die("unable to add index blob to traversal");
1330 add_pending_object_with_path(revs, &blob->object, "",
1331 ce->ce_mode, ce->name);
1334 if (istate->cache_tree) {
1335 struct strbuf path = STRBUF_INIT;
1336 add_cache_tree(istate->cache_tree, revs, &path);
1337 strbuf_release(&path);
1341 void add_index_objects_to_pending(struct rev_info *revs, unsigned int flags)
1343 struct worktree **worktrees, **p;
1345 read_cache();
1346 do_add_index_objects_to_pending(revs, &the_index);
1348 if (revs->single_worktree)
1349 return;
1351 worktrees = get_worktrees(0);
1352 for (p = worktrees; *p; p++) {
1353 struct worktree *wt = *p;
1354 struct index_state istate = { NULL };
1356 if (wt->is_current)
1357 continue; /* current index already taken care of */
1359 if (read_index_from(&istate,
1360 worktree_git_path(wt, "index"),
1361 get_worktree_git_dir(wt)) > 0)
1362 do_add_index_objects_to_pending(revs, &istate);
1363 discard_index(&istate);
1365 free_worktrees(worktrees);
1368 static int add_parents_only(struct rev_info *revs, const char *arg_, int flags,
1369 int exclude_parent)
1371 struct object_id oid;
1372 struct object *it;
1373 struct commit *commit;
1374 struct commit_list *parents;
1375 int parent_number;
1376 const char *arg = arg_;
1378 if (*arg == '^') {
1379 flags ^= UNINTERESTING | BOTTOM;
1380 arg++;
1382 if (get_oid_committish(arg, &oid))
1383 return 0;
1384 while (1) {
1385 it = get_reference(revs, arg, &oid, 0);
1386 if (!it && revs->ignore_missing)
1387 return 0;
1388 if (it->type != OBJ_TAG)
1389 break;
1390 if (!((struct tag*)it)->tagged)
1391 return 0;
1392 oidcpy(&oid, &((struct tag*)it)->tagged->oid);
1394 if (it->type != OBJ_COMMIT)
1395 return 0;
1396 commit = (struct commit *)it;
1397 if (exclude_parent &&
1398 exclude_parent > commit_list_count(commit->parents))
1399 return 0;
1400 for (parents = commit->parents, parent_number = 1;
1401 parents;
1402 parents = parents->next, parent_number++) {
1403 if (exclude_parent && parent_number != exclude_parent)
1404 continue;
1406 it = &parents->item->object;
1407 it->flags |= flags;
1408 add_rev_cmdline(revs, it, arg_, REV_CMD_PARENTS_ONLY, flags);
1409 add_pending_object(revs, it, arg);
1411 return 1;
1414 void init_revisions(struct rev_info *revs, const char *prefix)
1416 memset(revs, 0, sizeof(*revs));
1418 revs->abbrev = DEFAULT_ABBREV;
1419 revs->ignore_merges = 1;
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->ignore_merges = 0;
1943 } else if (!strcmp(arg, "-c")) {
1944 revs->diff = 1;
1945 revs->dense_combined_merges = 0;
1946 revs->combine_merges = 1;
1947 } else if (!strcmp(arg, "--cc")) {
1948 revs->diff = 1;
1949 revs->dense_combined_merges = 1;
1950 revs->combine_merges = 1;
1951 } else if (!strcmp(arg, "-v")) {
1952 revs->verbose_header = 1;
1953 } else if (!strcmp(arg, "--pretty")) {
1954 revs->verbose_header = 1;
1955 revs->pretty_given = 1;
1956 get_commit_format(NULL, revs);
1957 } else if (skip_prefix(arg, "--pretty=", &optarg) ||
1958 skip_prefix(arg, "--format=", &optarg)) {
1960 * Detached form ("--pretty X" as opposed to "--pretty=X")
1961 * not allowed, since the argument is optional.
1963 revs->verbose_header = 1;
1964 revs->pretty_given = 1;
1965 get_commit_format(optarg, revs);
1966 } else if (!strcmp(arg, "--expand-tabs")) {
1967 revs->expand_tabs_in_log = 8;
1968 } else if (!strcmp(arg, "--no-expand-tabs")) {
1969 revs->expand_tabs_in_log = 0;
1970 } else if (skip_prefix(arg, "--expand-tabs=", &arg)) {
1971 int val;
1972 if (strtol_i(arg, 10, &val) < 0 || val < 0)
1973 die("'%s': not a non-negative integer", arg);
1974 revs->expand_tabs_in_log = val;
1975 } else if (!strcmp(arg, "--show-notes") || !strcmp(arg, "--notes")) {
1976 revs->show_notes = 1;
1977 revs->show_notes_given = 1;
1978 revs->notes_opt.use_default_notes = 1;
1979 } else if (!strcmp(arg, "--show-signature")) {
1980 revs->show_signature = 1;
1981 } else if (!strcmp(arg, "--no-show-signature")) {
1982 revs->show_signature = 0;
1983 } else if (!strcmp(arg, "--show-linear-break")) {
1984 revs->break_bar = " ..........";
1985 revs->track_linear = 1;
1986 revs->track_first_time = 1;
1987 } else if (skip_prefix(arg, "--show-linear-break=", &optarg)) {
1988 revs->break_bar = xstrdup(optarg);
1989 revs->track_linear = 1;
1990 revs->track_first_time = 1;
1991 } else if (skip_prefix(arg, "--show-notes=", &optarg) ||
1992 skip_prefix(arg, "--notes=", &optarg)) {
1993 struct strbuf buf = STRBUF_INIT;
1994 revs->show_notes = 1;
1995 revs->show_notes_given = 1;
1996 if (starts_with(arg, "--show-notes=") &&
1997 revs->notes_opt.use_default_notes < 0)
1998 revs->notes_opt.use_default_notes = 1;
1999 strbuf_addstr(&buf, optarg);
2000 expand_notes_ref(&buf);
2001 string_list_append(&revs->notes_opt.extra_notes_refs,
2002 strbuf_detach(&buf, NULL));
2003 } else if (!strcmp(arg, "--no-notes")) {
2004 revs->show_notes = 0;
2005 revs->show_notes_given = 1;
2006 revs->notes_opt.use_default_notes = -1;
2007 /* we have been strdup'ing ourselves, so trick
2008 * string_list into free()ing strings */
2009 revs->notes_opt.extra_notes_refs.strdup_strings = 1;
2010 string_list_clear(&revs->notes_opt.extra_notes_refs, 0);
2011 revs->notes_opt.extra_notes_refs.strdup_strings = 0;
2012 } else if (!strcmp(arg, "--standard-notes")) {
2013 revs->show_notes_given = 1;
2014 revs->notes_opt.use_default_notes = 1;
2015 } else if (!strcmp(arg, "--no-standard-notes")) {
2016 revs->notes_opt.use_default_notes = 0;
2017 } else if (!strcmp(arg, "--oneline")) {
2018 revs->verbose_header = 1;
2019 get_commit_format("oneline", revs);
2020 revs->pretty_given = 1;
2021 revs->abbrev_commit = 1;
2022 } else if (!strcmp(arg, "--graph")) {
2023 revs->topo_order = 1;
2024 revs->rewrite_parents = 1;
2025 revs->graph = graph_init(revs);
2026 } else if (!strcmp(arg, "--root")) {
2027 revs->show_root_diff = 1;
2028 } else if (!strcmp(arg, "--no-commit-id")) {
2029 revs->no_commit_id = 1;
2030 } else if (!strcmp(arg, "--always")) {
2031 revs->always_show_header = 1;
2032 } else if (!strcmp(arg, "--no-abbrev")) {
2033 revs->abbrev = 0;
2034 } else if (!strcmp(arg, "--abbrev")) {
2035 revs->abbrev = DEFAULT_ABBREV;
2036 } else if (skip_prefix(arg, "--abbrev=", &optarg)) {
2037 revs->abbrev = strtoul(optarg, NULL, 10);
2038 if (revs->abbrev < MINIMUM_ABBREV)
2039 revs->abbrev = MINIMUM_ABBREV;
2040 else if (revs->abbrev > 40)
2041 revs->abbrev = 40;
2042 } else if (!strcmp(arg, "--abbrev-commit")) {
2043 revs->abbrev_commit = 1;
2044 revs->abbrev_commit_given = 1;
2045 } else if (!strcmp(arg, "--no-abbrev-commit")) {
2046 revs->abbrev_commit = 0;
2047 } else if (!strcmp(arg, "--full-diff")) {
2048 revs->diff = 1;
2049 revs->full_diff = 1;
2050 } else if (!strcmp(arg, "--full-history")) {
2051 revs->simplify_history = 0;
2052 } else if (!strcmp(arg, "--relative-date")) {
2053 revs->date_mode.type = DATE_RELATIVE;
2054 revs->date_mode_explicit = 1;
2055 } else if ((argcount = parse_long_opt("date", argv, &optarg))) {
2056 parse_date_format(optarg, &revs->date_mode);
2057 revs->date_mode_explicit = 1;
2058 return argcount;
2059 } else if (!strcmp(arg, "--log-size")) {
2060 revs->show_log_size = 1;
2063 * Grepping the commit log
2065 else if ((argcount = parse_long_opt("author", argv, &optarg))) {
2066 add_header_grep(revs, GREP_HEADER_AUTHOR, optarg);
2067 return argcount;
2068 } else if ((argcount = parse_long_opt("committer", argv, &optarg))) {
2069 add_header_grep(revs, GREP_HEADER_COMMITTER, optarg);
2070 return argcount;
2071 } else if ((argcount = parse_long_opt("grep-reflog", argv, &optarg))) {
2072 add_header_grep(revs, GREP_HEADER_REFLOG, optarg);
2073 return argcount;
2074 } else if ((argcount = parse_long_opt("grep", argv, &optarg))) {
2075 add_message_grep(revs, optarg);
2076 return argcount;
2077 } else if (!strcmp(arg, "--grep-debug")) {
2078 revs->grep_filter.debug = 1;
2079 } else if (!strcmp(arg, "--basic-regexp")) {
2080 revs->grep_filter.pattern_type_option = GREP_PATTERN_TYPE_BRE;
2081 } else if (!strcmp(arg, "--extended-regexp") || !strcmp(arg, "-E")) {
2082 revs->grep_filter.pattern_type_option = GREP_PATTERN_TYPE_ERE;
2083 } else if (!strcmp(arg, "--regexp-ignore-case") || !strcmp(arg, "-i")) {
2084 revs->grep_filter.ignore_case = 1;
2085 revs->diffopt.pickaxe_opts |= DIFF_PICKAXE_IGNORE_CASE;
2086 } else if (!strcmp(arg, "--fixed-strings") || !strcmp(arg, "-F")) {
2087 revs->grep_filter.pattern_type_option = GREP_PATTERN_TYPE_FIXED;
2088 } else if (!strcmp(arg, "--perl-regexp") || !strcmp(arg, "-P")) {
2089 revs->grep_filter.pattern_type_option = GREP_PATTERN_TYPE_PCRE;
2090 } else if (!strcmp(arg, "--all-match")) {
2091 revs->grep_filter.all_match = 1;
2092 } else if (!strcmp(arg, "--invert-grep")) {
2093 revs->invert_grep = 1;
2094 } else if ((argcount = parse_long_opt("encoding", argv, &optarg))) {
2095 if (strcmp(optarg, "none"))
2096 git_log_output_encoding = xstrdup(optarg);
2097 else
2098 git_log_output_encoding = "";
2099 return argcount;
2100 } else if (!strcmp(arg, "--reverse")) {
2101 revs->reverse ^= 1;
2102 } else if (!strcmp(arg, "--children")) {
2103 revs->children.name = "children";
2104 revs->limited = 1;
2105 } else if (!strcmp(arg, "--ignore-missing")) {
2106 revs->ignore_missing = 1;
2107 } else if (!strcmp(arg, "--exclude-promisor-objects")) {
2108 if (fetch_if_missing)
2109 die("BUG: exclude_promisor_objects can only be used when fetch_if_missing is 0");
2110 revs->exclude_promisor_objects = 1;
2111 } else {
2112 int opts = diff_opt_parse(&revs->diffopt, argv, argc, revs->prefix);
2113 if (!opts)
2114 unkv[(*unkc)++] = arg;
2115 return opts;
2117 if (revs->graph && revs->track_linear)
2118 die("--show-linear-break and --graph are incompatible");
2120 return 1;
2123 void parse_revision_opt(struct rev_info *revs, struct parse_opt_ctx_t *ctx,
2124 const struct option *options,
2125 const char * const usagestr[])
2127 int n = handle_revision_opt(revs, ctx->argc, ctx->argv,
2128 &ctx->cpidx, ctx->out);
2129 if (n <= 0) {
2130 error("unknown option `%s'", ctx->argv[0]);
2131 usage_with_options(usagestr, options);
2133 ctx->argv += n;
2134 ctx->argc -= n;
2137 static int for_each_bisect_ref(struct ref_store *refs, each_ref_fn fn,
2138 void *cb_data, const char *term)
2140 struct strbuf bisect_refs = STRBUF_INIT;
2141 int status;
2142 strbuf_addf(&bisect_refs, "refs/bisect/%s", term);
2143 status = refs_for_each_fullref_in(refs, bisect_refs.buf, fn, cb_data, 0);
2144 strbuf_release(&bisect_refs);
2145 return status;
2148 static int for_each_bad_bisect_ref(struct ref_store *refs, each_ref_fn fn, void *cb_data)
2150 return for_each_bisect_ref(refs, fn, cb_data, term_bad);
2153 static int for_each_good_bisect_ref(struct ref_store *refs, each_ref_fn fn, void *cb_data)
2155 return for_each_bisect_ref(refs, fn, cb_data, term_good);
2158 static int handle_revision_pseudo_opt(const char *submodule,
2159 struct rev_info *revs,
2160 int argc, const char **argv, int *flags)
2162 const char *arg = argv[0];
2163 const char *optarg;
2164 struct ref_store *refs;
2165 int argcount;
2167 if (submodule) {
2169 * We need some something like get_submodule_worktrees()
2170 * before we can go through all worktrees of a submodule,
2171 * .e.g with adding all HEADs from --all, which is not
2172 * supported right now, so stick to single worktree.
2174 if (!revs->single_worktree)
2175 die("BUG: --single-worktree cannot be used together with submodule");
2176 refs = get_submodule_ref_store(submodule);
2177 } else
2178 refs = get_main_ref_store();
2181 * NOTE!
2183 * Commands like "git shortlog" will not accept the options below
2184 * unless parse_revision_opt queues them (as opposed to erroring
2185 * out).
2187 * When implementing your new pseudo-option, remember to
2188 * register it in the list at the top of handle_revision_opt.
2190 if (!strcmp(arg, "--all")) {
2191 handle_refs(refs, revs, *flags, refs_for_each_ref);
2192 handle_refs(refs, revs, *flags, refs_head_ref);
2193 if (!revs->single_worktree) {
2194 struct all_refs_cb cb;
2196 init_all_refs_cb(&cb, revs, *flags);
2197 other_head_refs(handle_one_ref, &cb);
2199 clear_ref_exclusion(&revs->ref_excludes);
2200 } else if (!strcmp(arg, "--branches")) {
2201 handle_refs(refs, revs, *flags, refs_for_each_branch_ref);
2202 clear_ref_exclusion(&revs->ref_excludes);
2203 } else if (!strcmp(arg, "--bisect")) {
2204 read_bisect_terms(&term_bad, &term_good);
2205 handle_refs(refs, revs, *flags, for_each_bad_bisect_ref);
2206 handle_refs(refs, revs, *flags ^ (UNINTERESTING | BOTTOM),
2207 for_each_good_bisect_ref);
2208 revs->bisect = 1;
2209 } else if (!strcmp(arg, "--tags")) {
2210 handle_refs(refs, revs, *flags, refs_for_each_tag_ref);
2211 clear_ref_exclusion(&revs->ref_excludes);
2212 } else if (!strcmp(arg, "--remotes")) {
2213 handle_refs(refs, revs, *flags, refs_for_each_remote_ref);
2214 clear_ref_exclusion(&revs->ref_excludes);
2215 } else if ((argcount = parse_long_opt("glob", argv, &optarg))) {
2216 struct all_refs_cb cb;
2217 init_all_refs_cb(&cb, revs, *flags);
2218 for_each_glob_ref(handle_one_ref, optarg, &cb);
2219 clear_ref_exclusion(&revs->ref_excludes);
2220 return argcount;
2221 } else if ((argcount = parse_long_opt("exclude", argv, &optarg))) {
2222 add_ref_exclusion(&revs->ref_excludes, optarg);
2223 return argcount;
2224 } else if (skip_prefix(arg, "--branches=", &optarg)) {
2225 struct all_refs_cb cb;
2226 init_all_refs_cb(&cb, revs, *flags);
2227 for_each_glob_ref_in(handle_one_ref, optarg, "refs/heads/", &cb);
2228 clear_ref_exclusion(&revs->ref_excludes);
2229 } else if (skip_prefix(arg, "--tags=", &optarg)) {
2230 struct all_refs_cb cb;
2231 init_all_refs_cb(&cb, revs, *flags);
2232 for_each_glob_ref_in(handle_one_ref, optarg, "refs/tags/", &cb);
2233 clear_ref_exclusion(&revs->ref_excludes);
2234 } else if (skip_prefix(arg, "--remotes=", &optarg)) {
2235 struct all_refs_cb cb;
2236 init_all_refs_cb(&cb, revs, *flags);
2237 for_each_glob_ref_in(handle_one_ref, optarg, "refs/remotes/", &cb);
2238 clear_ref_exclusion(&revs->ref_excludes);
2239 } else if (!strcmp(arg, "--reflog")) {
2240 add_reflogs_to_pending(revs, *flags);
2241 } else if (!strcmp(arg, "--indexed-objects")) {
2242 add_index_objects_to_pending(revs, *flags);
2243 } else if (!strcmp(arg, "--not")) {
2244 *flags ^= UNINTERESTING | BOTTOM;
2245 } else if (!strcmp(arg, "--no-walk")) {
2246 revs->no_walk = REVISION_WALK_NO_WALK_SORTED;
2247 } else if (skip_prefix(arg, "--no-walk=", &optarg)) {
2249 * Detached form ("--no-walk X" as opposed to "--no-walk=X")
2250 * not allowed, since the argument is optional.
2252 if (!strcmp(optarg, "sorted"))
2253 revs->no_walk = REVISION_WALK_NO_WALK_SORTED;
2254 else if (!strcmp(optarg, "unsorted"))
2255 revs->no_walk = REVISION_WALK_NO_WALK_UNSORTED;
2256 else
2257 return error("invalid argument to --no-walk");
2258 } else if (!strcmp(arg, "--do-walk")) {
2259 revs->no_walk = 0;
2260 } else if (!strcmp(arg, "--single-worktree")) {
2261 revs->single_worktree = 1;
2262 } else {
2263 return 0;
2266 return 1;
2269 static void NORETURN diagnose_missing_default(const char *def)
2271 int flags;
2272 const char *refname;
2274 refname = resolve_ref_unsafe(def, 0, NULL, &flags);
2275 if (!refname || !(flags & REF_ISSYMREF) || (flags & REF_ISBROKEN))
2276 die(_("your current branch appears to be broken"));
2278 skip_prefix(refname, "refs/heads/", &refname);
2279 die(_("your current branch '%s' does not have any commits yet"),
2280 refname);
2284 * Parse revision information, filling in the "rev_info" structure,
2285 * and removing the used arguments from the argument list.
2287 * Returns the number of arguments left that weren't recognized
2288 * (which are also moved to the head of the argument list)
2290 int setup_revisions(int argc, const char **argv, struct rev_info *revs, struct setup_revision_opt *opt)
2292 int i, flags, left, seen_dashdash, read_from_stdin, got_rev_arg = 0, revarg_opt;
2293 struct argv_array prune_data = ARGV_ARRAY_INIT;
2294 const char *submodule = NULL;
2296 if (opt)
2297 submodule = opt->submodule;
2299 /* First, search for "--" */
2300 if (opt && opt->assume_dashdash) {
2301 seen_dashdash = 1;
2302 } else {
2303 seen_dashdash = 0;
2304 for (i = 1; i < argc; i++) {
2305 const char *arg = argv[i];
2306 if (strcmp(arg, "--"))
2307 continue;
2308 argv[i] = NULL;
2309 argc = i;
2310 if (argv[i + 1])
2311 argv_array_pushv(&prune_data, argv + i + 1);
2312 seen_dashdash = 1;
2313 break;
2317 /* Second, deal with arguments and options */
2318 flags = 0;
2319 revarg_opt = opt ? opt->revarg_opt : 0;
2320 if (seen_dashdash)
2321 revarg_opt |= REVARG_CANNOT_BE_FILENAME;
2322 read_from_stdin = 0;
2323 for (left = i = 1; i < argc; i++) {
2324 const char *arg = argv[i];
2325 if (*arg == '-') {
2326 int opts;
2328 opts = handle_revision_pseudo_opt(submodule,
2329 revs, argc - i, argv + i,
2330 &flags);
2331 if (opts > 0) {
2332 i += opts - 1;
2333 continue;
2336 if (!strcmp(arg, "--stdin")) {
2337 if (revs->disable_stdin) {
2338 argv[left++] = arg;
2339 continue;
2341 if (read_from_stdin++)
2342 die("--stdin given twice?");
2343 read_revisions_from_stdin(revs, &prune_data);
2344 continue;
2347 opts = handle_revision_opt(revs, argc - i, argv + i, &left, argv);
2348 if (opts > 0) {
2349 i += opts - 1;
2350 continue;
2352 if (opts < 0)
2353 exit(128);
2354 continue;
2358 if (handle_revision_arg(arg, revs, flags, revarg_opt)) {
2359 int j;
2360 if (seen_dashdash || *arg == '^')
2361 die("bad revision '%s'", arg);
2363 /* If we didn't have a "--":
2364 * (1) all filenames must exist;
2365 * (2) all rev-args must not be interpretable
2366 * as a valid filename.
2367 * but the latter we have checked in the main loop.
2369 for (j = i; j < argc; j++)
2370 verify_filename(revs->prefix, argv[j], j == i);
2372 argv_array_pushv(&prune_data, argv + i);
2373 break;
2375 else
2376 got_rev_arg = 1;
2379 if (prune_data.argc) {
2381 * If we need to introduce the magic "a lone ':' means no
2382 * pathspec whatsoever", here is the place to do so.
2384 * if (prune_data.nr == 1 && !strcmp(prune_data[0], ":")) {
2385 * prune_data.nr = 0;
2386 * prune_data.alloc = 0;
2387 * free(prune_data.path);
2388 * prune_data.path = NULL;
2389 * } else {
2390 * terminate prune_data.alloc with NULL and
2391 * call init_pathspec() to set revs->prune_data here.
2394 parse_pathspec(&revs->prune_data, 0, 0,
2395 revs->prefix, prune_data.argv);
2397 argv_array_clear(&prune_data);
2399 if (revs->def == NULL)
2400 revs->def = opt ? opt->def : NULL;
2401 if (opt && opt->tweak)
2402 opt->tweak(revs, opt);
2403 if (revs->show_merge)
2404 prepare_show_merge(revs);
2405 if (revs->def && !revs->pending.nr && !revs->rev_input_given && !got_rev_arg) {
2406 struct object_id oid;
2407 struct object *object;
2408 struct object_context oc;
2409 if (get_oid_with_context(revs->def, 0, &oid, &oc))
2410 diagnose_missing_default(revs->def);
2411 object = get_reference(revs, revs->def, &oid, 0);
2412 add_pending_object_with_mode(revs, object, revs->def, oc.mode);
2415 /* Did the user ask for any diff output? Run the diff! */
2416 if (revs->diffopt.output_format & ~DIFF_FORMAT_NO_OUTPUT)
2417 revs->diff = 1;
2419 /* Pickaxe, diff-filter and rename following need diffs */
2420 if ((revs->diffopt.pickaxe_opts & DIFF_PICKAXE_KINDS_MASK) ||
2421 revs->diffopt.filter ||
2422 revs->diffopt.flags.follow_renames)
2423 revs->diff = 1;
2425 if (revs->diffopt.objfind)
2426 revs->simplify_history = 0;
2428 if (revs->topo_order)
2429 revs->limited = 1;
2431 if (revs->prune_data.nr) {
2432 copy_pathspec(&revs->pruning.pathspec, &revs->prune_data);
2433 /* Can't prune commits with rename following: the paths change.. */
2434 if (!revs->diffopt.flags.follow_renames)
2435 revs->prune = 1;
2436 if (!revs->full_diff)
2437 copy_pathspec(&revs->diffopt.pathspec,
2438 &revs->prune_data);
2440 if (revs->combine_merges)
2441 revs->ignore_merges = 0;
2442 revs->diffopt.abbrev = revs->abbrev;
2444 if (revs->line_level_traverse) {
2445 revs->limited = 1;
2446 revs->topo_order = 1;
2449 diff_setup_done(&revs->diffopt);
2451 grep_commit_pattern_type(GREP_PATTERN_TYPE_UNSPECIFIED,
2452 &revs->grep_filter);
2453 compile_grep_patterns(&revs->grep_filter);
2455 if (revs->reverse && revs->reflog_info)
2456 die("cannot combine --reverse with --walk-reflogs");
2457 if (revs->reflog_info && revs->limited)
2458 die("cannot combine --walk-reflogs with history-limiting options");
2459 if (revs->rewrite_parents && revs->children.name)
2460 die("cannot combine --parents and --children");
2463 * Limitations on the graph functionality
2465 if (revs->reverse && revs->graph)
2466 die("cannot combine --reverse with --graph");
2468 if (revs->reflog_info && revs->graph)
2469 die("cannot combine --walk-reflogs with --graph");
2470 if (revs->no_walk && revs->graph)
2471 die("cannot combine --no-walk with --graph");
2472 if (!revs->reflog_info && revs->grep_filter.use_reflog_filter)
2473 die("cannot use --grep-reflog without --walk-reflogs");
2475 if (revs->first_parent_only && revs->bisect)
2476 die(_("--first-parent is incompatible with --bisect"));
2478 if (revs->expand_tabs_in_log < 0)
2479 revs->expand_tabs_in_log = revs->expand_tabs_in_log_default;
2481 return left;
2484 static void add_child(struct rev_info *revs, struct commit *parent, struct commit *child)
2486 struct commit_list *l = xcalloc(1, sizeof(*l));
2488 l->item = child;
2489 l->next = add_decoration(&revs->children, &parent->object, l);
2492 static int remove_duplicate_parents(struct rev_info *revs, struct commit *commit)
2494 struct treesame_state *ts = lookup_decoration(&revs->treesame, &commit->object);
2495 struct commit_list **pp, *p;
2496 int surviving_parents;
2498 /* Examine existing parents while marking ones we have seen... */
2499 pp = &commit->parents;
2500 surviving_parents = 0;
2501 while ((p = *pp) != NULL) {
2502 struct commit *parent = p->item;
2503 if (parent->object.flags & TMP_MARK) {
2504 *pp = p->next;
2505 if (ts)
2506 compact_treesame(revs, commit, surviving_parents);
2507 continue;
2509 parent->object.flags |= TMP_MARK;
2510 surviving_parents++;
2511 pp = &p->next;
2513 /* clear the temporary mark */
2514 for (p = commit->parents; p; p = p->next) {
2515 p->item->object.flags &= ~TMP_MARK;
2517 /* no update_treesame() - removing duplicates can't affect TREESAME */
2518 return surviving_parents;
2521 struct merge_simplify_state {
2522 struct commit *simplified;
2525 static struct merge_simplify_state *locate_simplify_state(struct rev_info *revs, struct commit *commit)
2527 struct merge_simplify_state *st;
2529 st = lookup_decoration(&revs->merge_simplification, &commit->object);
2530 if (!st) {
2531 st = xcalloc(1, sizeof(*st));
2532 add_decoration(&revs->merge_simplification, &commit->object, st);
2534 return st;
2537 static int mark_redundant_parents(struct rev_info *revs, struct commit *commit)
2539 struct commit_list *h = reduce_heads(commit->parents);
2540 int i = 0, marked = 0;
2541 struct commit_list *po, *pn;
2543 /* Want these for sanity-checking only */
2544 int orig_cnt = commit_list_count(commit->parents);
2545 int cnt = commit_list_count(h);
2548 * Not ready to remove items yet, just mark them for now, based
2549 * on the output of reduce_heads(). reduce_heads outputs the reduced
2550 * set in its original order, so this isn't too hard.
2552 po = commit->parents;
2553 pn = h;
2554 while (po) {
2555 if (pn && po->item == pn->item) {
2556 pn = pn->next;
2557 i++;
2558 } else {
2559 po->item->object.flags |= TMP_MARK;
2560 marked++;
2562 po=po->next;
2565 if (i != cnt || cnt+marked != orig_cnt)
2566 die("mark_redundant_parents %d %d %d %d", orig_cnt, cnt, i, marked);
2568 free_commit_list(h);
2570 return marked;
2573 static int mark_treesame_root_parents(struct rev_info *revs, struct commit *commit)
2575 struct commit_list *p;
2576 int marked = 0;
2578 for (p = commit->parents; p; p = p->next) {
2579 struct commit *parent = p->item;
2580 if (!parent->parents && (parent->object.flags & TREESAME)) {
2581 parent->object.flags |= TMP_MARK;
2582 marked++;
2586 return marked;
2590 * Awkward naming - this means one parent we are TREESAME to.
2591 * cf mark_treesame_root_parents: root parents that are TREESAME (to an
2592 * empty tree). Better name suggestions?
2594 static int leave_one_treesame_to_parent(struct rev_info *revs, struct commit *commit)
2596 struct treesame_state *ts = lookup_decoration(&revs->treesame, &commit->object);
2597 struct commit *unmarked = NULL, *marked = NULL;
2598 struct commit_list *p;
2599 unsigned n;
2601 for (p = commit->parents, n = 0; p; p = p->next, n++) {
2602 if (ts->treesame[n]) {
2603 if (p->item->object.flags & TMP_MARK) {
2604 if (!marked)
2605 marked = p->item;
2606 } else {
2607 if (!unmarked) {
2608 unmarked = p->item;
2609 break;
2616 * If we are TREESAME to a marked-for-deletion parent, but not to any
2617 * unmarked parents, unmark the first TREESAME parent. This is the
2618 * parent that the default simplify_history==1 scan would have followed,
2619 * and it doesn't make sense to omit that path when asking for a
2620 * simplified full history. Retaining it improves the chances of
2621 * understanding odd missed merges that took an old version of a file.
2623 * Example:
2625 * I--------*X A modified the file, but mainline merge X used
2626 * \ / "-s ours", so took the version from I. X is
2627 * `-*A--' TREESAME to I and !TREESAME to A.
2629 * Default log from X would produce "I". Without this check,
2630 * --full-history --simplify-merges would produce "I-A-X", showing
2631 * the merge commit X and that it changed A, but not making clear that
2632 * it had just taken the I version. With this check, the topology above
2633 * is retained.
2635 * Note that it is possible that the simplification chooses a different
2636 * TREESAME parent from the default, in which case this test doesn't
2637 * activate, and we _do_ drop the default parent. Example:
2639 * I------X A modified the file, but it was reverted in B,
2640 * \ / meaning mainline merge X is TREESAME to both
2641 * *A-*B parents.
2643 * Default log would produce "I" by following the first parent;
2644 * --full-history --simplify-merges will produce "I-A-B". But this is a
2645 * reasonable result - it presents a logical full history leading from
2646 * I to X, and X is not an important merge.
2648 if (!unmarked && marked) {
2649 marked->object.flags &= ~TMP_MARK;
2650 return 1;
2653 return 0;
2656 static int remove_marked_parents(struct rev_info *revs, struct commit *commit)
2658 struct commit_list **pp, *p;
2659 int nth_parent, removed = 0;
2661 pp = &commit->parents;
2662 nth_parent = 0;
2663 while ((p = *pp) != NULL) {
2664 struct commit *parent = p->item;
2665 if (parent->object.flags & TMP_MARK) {
2666 parent->object.flags &= ~TMP_MARK;
2667 *pp = p->next;
2668 free(p);
2669 removed++;
2670 compact_treesame(revs, commit, nth_parent);
2671 continue;
2673 pp = &p->next;
2674 nth_parent++;
2677 /* Removing parents can only increase TREESAMEness */
2678 if (removed && !(commit->object.flags & TREESAME))
2679 update_treesame(revs, commit);
2681 return nth_parent;
2684 static struct commit_list **simplify_one(struct rev_info *revs, struct commit *commit, struct commit_list **tail)
2686 struct commit_list *p;
2687 struct commit *parent;
2688 struct merge_simplify_state *st, *pst;
2689 int cnt;
2691 st = locate_simplify_state(revs, commit);
2694 * Have we handled this one?
2696 if (st->simplified)
2697 return tail;
2700 * An UNINTERESTING commit simplifies to itself, so does a
2701 * root commit. We do not rewrite parents of such commit
2702 * anyway.
2704 if ((commit->object.flags & UNINTERESTING) || !commit->parents) {
2705 st->simplified = commit;
2706 return tail;
2710 * Do we know what commit all of our parents that matter
2711 * should be rewritten to? Otherwise we are not ready to
2712 * rewrite this one yet.
2714 for (cnt = 0, p = commit->parents; p; p = p->next) {
2715 pst = locate_simplify_state(revs, p->item);
2716 if (!pst->simplified) {
2717 tail = &commit_list_insert(p->item, tail)->next;
2718 cnt++;
2720 if (revs->first_parent_only)
2721 break;
2723 if (cnt) {
2724 tail = &commit_list_insert(commit, tail)->next;
2725 return tail;
2729 * Rewrite our list of parents. Note that this cannot
2730 * affect our TREESAME flags in any way - a commit is
2731 * always TREESAME to its simplification.
2733 for (p = commit->parents; p; p = p->next) {
2734 pst = locate_simplify_state(revs, p->item);
2735 p->item = pst->simplified;
2736 if (revs->first_parent_only)
2737 break;
2740 if (revs->first_parent_only)
2741 cnt = 1;
2742 else
2743 cnt = remove_duplicate_parents(revs, commit);
2746 * It is possible that we are a merge and one side branch
2747 * does not have any commit that touches the given paths;
2748 * in such a case, the immediate parent from that branch
2749 * will be rewritten to be the merge base.
2751 * o----X X: the commit we are looking at;
2752 * / / o: a commit that touches the paths;
2753 * ---o----'
2755 * Further, a merge of an independent branch that doesn't
2756 * touch the path will reduce to a treesame root parent:
2758 * ----o----X X: the commit we are looking at;
2759 * / o: a commit that touches the paths;
2760 * r r: a root commit not touching the paths
2762 * Detect and simplify both cases.
2764 if (1 < cnt) {
2765 int marked = mark_redundant_parents(revs, commit);
2766 marked += mark_treesame_root_parents(revs, commit);
2767 if (marked)
2768 marked -= leave_one_treesame_to_parent(revs, commit);
2769 if (marked)
2770 cnt = remove_marked_parents(revs, commit);
2774 * A commit simplifies to itself if it is a root, if it is
2775 * UNINTERESTING, if it touches the given paths, or if it is a
2776 * merge and its parents don't simplify to one relevant commit
2777 * (the first two cases are already handled at the beginning of
2778 * this function).
2780 * Otherwise, it simplifies to what its sole relevant parent
2781 * simplifies to.
2783 if (!cnt ||
2784 (commit->object.flags & UNINTERESTING) ||
2785 !(commit->object.flags & TREESAME) ||
2786 (parent = one_relevant_parent(revs, commit->parents)) == NULL)
2787 st->simplified = commit;
2788 else {
2789 pst = locate_simplify_state(revs, parent);
2790 st->simplified = pst->simplified;
2792 return tail;
2795 static void simplify_merges(struct rev_info *revs)
2797 struct commit_list *list, *next;
2798 struct commit_list *yet_to_do, **tail;
2799 struct commit *commit;
2801 if (!revs->prune)
2802 return;
2804 /* feed the list reversed */
2805 yet_to_do = NULL;
2806 for (list = revs->commits; list; list = next) {
2807 commit = list->item;
2808 next = list->next;
2810 * Do not free(list) here yet; the original list
2811 * is used later in this function.
2813 commit_list_insert(commit, &yet_to_do);
2815 while (yet_to_do) {
2816 list = yet_to_do;
2817 yet_to_do = NULL;
2818 tail = &yet_to_do;
2819 while (list) {
2820 commit = pop_commit(&list);
2821 tail = simplify_one(revs, commit, tail);
2825 /* clean up the result, removing the simplified ones */
2826 list = revs->commits;
2827 revs->commits = NULL;
2828 tail = &revs->commits;
2829 while (list) {
2830 struct merge_simplify_state *st;
2832 commit = pop_commit(&list);
2833 st = locate_simplify_state(revs, commit);
2834 if (st->simplified == commit)
2835 tail = &commit_list_insert(commit, tail)->next;
2839 static void set_children(struct rev_info *revs)
2841 struct commit_list *l;
2842 for (l = revs->commits; l; l = l->next) {
2843 struct commit *commit = l->item;
2844 struct commit_list *p;
2846 for (p = commit->parents; p; p = p->next)
2847 add_child(revs, p->item, commit);
2851 void reset_revision_walk(void)
2853 clear_object_flags(SEEN | ADDED | SHOWN);
2856 static int mark_uninteresting(const struct object_id *oid,
2857 struct packed_git *pack,
2858 uint32_t pos,
2859 void *unused)
2861 struct object *o = parse_object(oid);
2862 o->flags |= UNINTERESTING | SEEN;
2863 return 0;
2866 int prepare_revision_walk(struct rev_info *revs)
2868 int i;
2869 struct object_array old_pending;
2870 struct commit_list **next = &revs->commits;
2872 memcpy(&old_pending, &revs->pending, sizeof(old_pending));
2873 revs->pending.nr = 0;
2874 revs->pending.alloc = 0;
2875 revs->pending.objects = NULL;
2876 for (i = 0; i < old_pending.nr; i++) {
2877 struct object_array_entry *e = old_pending.objects + i;
2878 struct commit *commit = handle_commit(revs, e);
2879 if (commit) {
2880 if (!(commit->object.flags & SEEN)) {
2881 commit->object.flags |= SEEN;
2882 next = commit_list_append(commit, next);
2886 object_array_clear(&old_pending);
2888 /* Signal whether we need per-parent treesame decoration */
2889 if (revs->simplify_merges ||
2890 (revs->limited && limiting_can_increase_treesame(revs)))
2891 revs->treesame.name = "treesame";
2893 if (revs->exclude_promisor_objects) {
2894 for_each_packed_object(mark_uninteresting, NULL,
2895 FOR_EACH_OBJECT_PROMISOR_ONLY);
2898 if (revs->no_walk != REVISION_WALK_NO_WALK_UNSORTED)
2899 commit_list_sort_by_date(&revs->commits);
2900 if (revs->no_walk)
2901 return 0;
2902 if (revs->limited)
2903 if (limit_list(revs) < 0)
2904 return -1;
2905 if (revs->topo_order)
2906 sort_in_topological_order(&revs->commits, revs->sort_order);
2907 if (revs->line_level_traverse)
2908 line_log_filter(revs);
2909 if (revs->simplify_merges)
2910 simplify_merges(revs);
2911 if (revs->children.name)
2912 set_children(revs);
2913 return 0;
2916 static enum rewrite_result rewrite_one(struct rev_info *revs, struct commit **pp)
2918 struct commit_list *cache = NULL;
2920 for (;;) {
2921 struct commit *p = *pp;
2922 if (!revs->limited)
2923 if (add_parents_to_list(revs, p, &revs->commits, &cache) < 0)
2924 return rewrite_one_error;
2925 if (p->object.flags & UNINTERESTING)
2926 return rewrite_one_ok;
2927 if (!(p->object.flags & TREESAME))
2928 return rewrite_one_ok;
2929 if (!p->parents)
2930 return rewrite_one_noparents;
2931 if ((p = one_relevant_parent(revs, p->parents)) == NULL)
2932 return rewrite_one_ok;
2933 *pp = p;
2937 int rewrite_parents(struct rev_info *revs, struct commit *commit,
2938 rewrite_parent_fn_t rewrite_parent)
2940 struct commit_list **pp = &commit->parents;
2941 while (*pp) {
2942 struct commit_list *parent = *pp;
2943 switch (rewrite_parent(revs, &parent->item)) {
2944 case rewrite_one_ok:
2945 break;
2946 case rewrite_one_noparents:
2947 *pp = parent->next;
2948 continue;
2949 case rewrite_one_error:
2950 return -1;
2952 pp = &parent->next;
2954 remove_duplicate_parents(revs, commit);
2955 return 0;
2958 static int commit_rewrite_person(struct strbuf *buf, const char *what, struct string_list *mailmap)
2960 char *person, *endp;
2961 size_t len, namelen, maillen;
2962 const char *name;
2963 const char *mail;
2964 struct ident_split ident;
2966 person = strstr(buf->buf, what);
2967 if (!person)
2968 return 0;
2970 person += strlen(what);
2971 endp = strchr(person, '\n');
2972 if (!endp)
2973 return 0;
2975 len = endp - person;
2977 if (split_ident_line(&ident, person, len))
2978 return 0;
2980 mail = ident.mail_begin;
2981 maillen = ident.mail_end - ident.mail_begin;
2982 name = ident.name_begin;
2983 namelen = ident.name_end - ident.name_begin;
2985 if (map_user(mailmap, &mail, &maillen, &name, &namelen)) {
2986 struct strbuf namemail = STRBUF_INIT;
2988 strbuf_addf(&namemail, "%.*s <%.*s>",
2989 (int)namelen, name, (int)maillen, mail);
2991 strbuf_splice(buf, ident.name_begin - buf->buf,
2992 ident.mail_end - ident.name_begin + 1,
2993 namemail.buf, namemail.len);
2995 strbuf_release(&namemail);
2997 return 1;
3000 return 0;
3003 static int commit_match(struct commit *commit, struct rev_info *opt)
3005 int retval;
3006 const char *encoding;
3007 const char *message;
3008 struct strbuf buf = STRBUF_INIT;
3010 if (!opt->grep_filter.pattern_list && !opt->grep_filter.header_list)
3011 return 1;
3013 /* Prepend "fake" headers as needed */
3014 if (opt->grep_filter.use_reflog_filter) {
3015 strbuf_addstr(&buf, "reflog ");
3016 get_reflog_message(&buf, opt->reflog_info);
3017 strbuf_addch(&buf, '\n');
3021 * We grep in the user's output encoding, under the assumption that it
3022 * is the encoding they are most likely to write their grep pattern
3023 * for. In addition, it means we will match the "notes" encoding below,
3024 * so we will not end up with a buffer that has two different encodings
3025 * in it.
3027 encoding = get_log_output_encoding();
3028 message = logmsg_reencode(commit, NULL, encoding);
3030 /* Copy the commit to temporary if we are using "fake" headers */
3031 if (buf.len)
3032 strbuf_addstr(&buf, message);
3034 if (opt->grep_filter.header_list && opt->mailmap) {
3035 if (!buf.len)
3036 strbuf_addstr(&buf, message);
3038 commit_rewrite_person(&buf, "\nauthor ", opt->mailmap);
3039 commit_rewrite_person(&buf, "\ncommitter ", opt->mailmap);
3042 /* Append "fake" message parts as needed */
3043 if (opt->show_notes) {
3044 if (!buf.len)
3045 strbuf_addstr(&buf, message);
3046 format_display_notes(&commit->object.oid, &buf, encoding, 1);
3050 * Find either in the original commit message, or in the temporary.
3051 * Note that we cast away the constness of "message" here. It is
3052 * const because it may come from the cached commit buffer. That's OK,
3053 * because we know that it is modifiable heap memory, and that while
3054 * grep_buffer may modify it for speed, it will restore any
3055 * changes before returning.
3057 if (buf.len)
3058 retval = grep_buffer(&opt->grep_filter, buf.buf, buf.len);
3059 else
3060 retval = grep_buffer(&opt->grep_filter,
3061 (char *)message, strlen(message));
3062 strbuf_release(&buf);
3063 unuse_commit_buffer(commit, message);
3064 return opt->invert_grep ? !retval : retval;
3067 static inline int want_ancestry(const struct rev_info *revs)
3069 return (revs->rewrite_parents || revs->children.name);
3073 * Return a timestamp to be used for --since/--until comparisons for this
3074 * commit, based on the revision options.
3076 static timestamp_t comparison_date(const struct rev_info *revs,
3077 struct commit *commit)
3079 return revs->reflog_info ?
3080 get_reflog_timestamp(revs->reflog_info) :
3081 commit->date;
3084 enum commit_action get_commit_action(struct rev_info *revs, struct commit *commit)
3086 if (commit->object.flags & SHOWN)
3087 return commit_ignore;
3088 if (revs->unpacked && has_sha1_pack(commit->object.oid.hash))
3089 return commit_ignore;
3090 if (commit->object.flags & UNINTERESTING)
3091 return commit_ignore;
3092 if (revs->min_age != -1 &&
3093 comparison_date(revs, commit) > revs->min_age)
3094 return commit_ignore;
3095 if (revs->min_parents || (revs->max_parents >= 0)) {
3096 int n = commit_list_count(commit->parents);
3097 if ((n < revs->min_parents) ||
3098 ((revs->max_parents >= 0) && (n > revs->max_parents)))
3099 return commit_ignore;
3101 if (!commit_match(commit, revs))
3102 return commit_ignore;
3103 if (revs->prune && revs->dense) {
3104 /* Commit without changes? */
3105 if (commit->object.flags & TREESAME) {
3106 int n;
3107 struct commit_list *p;
3108 /* drop merges unless we want parenthood */
3109 if (!want_ancestry(revs))
3110 return commit_ignore;
3112 * If we want ancestry, then need to keep any merges
3113 * between relevant commits to tie together topology.
3114 * For consistency with TREESAME and simplification
3115 * use "relevant" here rather than just INTERESTING,
3116 * to treat bottom commit(s) as part of the topology.
3118 for (n = 0, p = commit->parents; p; p = p->next)
3119 if (relevant_commit(p->item))
3120 if (++n >= 2)
3121 return commit_show;
3122 return commit_ignore;
3125 return commit_show;
3128 define_commit_slab(saved_parents, struct commit_list *);
3130 #define EMPTY_PARENT_LIST ((struct commit_list *)-1)
3133 * You may only call save_parents() once per commit (this is checked
3134 * for non-root commits).
3136 static void save_parents(struct rev_info *revs, struct commit *commit)
3138 struct commit_list **pp;
3140 if (!revs->saved_parents_slab) {
3141 revs->saved_parents_slab = xmalloc(sizeof(struct saved_parents));
3142 init_saved_parents(revs->saved_parents_slab);
3145 pp = saved_parents_at(revs->saved_parents_slab, commit);
3148 * When walking with reflogs, we may visit the same commit
3149 * several times: once for each appearance in the reflog.
3151 * In this case, save_parents() will be called multiple times.
3152 * We want to keep only the first set of parents. We need to
3153 * store a sentinel value for an empty (i.e., NULL) parent
3154 * list to distinguish it from a not-yet-saved list, however.
3156 if (*pp)
3157 return;
3158 if (commit->parents)
3159 *pp = copy_commit_list(commit->parents);
3160 else
3161 *pp = EMPTY_PARENT_LIST;
3164 static void free_saved_parents(struct rev_info *revs)
3166 if (revs->saved_parents_slab)
3167 clear_saved_parents(revs->saved_parents_slab);
3170 struct commit_list *get_saved_parents(struct rev_info *revs, const struct commit *commit)
3172 struct commit_list *parents;
3174 if (!revs->saved_parents_slab)
3175 return commit->parents;
3177 parents = *saved_parents_at(revs->saved_parents_slab, commit);
3178 if (parents == EMPTY_PARENT_LIST)
3179 return NULL;
3180 return parents;
3183 enum commit_action simplify_commit(struct rev_info *revs, struct commit *commit)
3185 enum commit_action action = get_commit_action(revs, commit);
3187 if (action == commit_show &&
3188 revs->prune && revs->dense && want_ancestry(revs)) {
3190 * --full-diff on simplified parents is no good: it
3191 * will show spurious changes from the commits that
3192 * were elided. So we save the parents on the side
3193 * when --full-diff is in effect.
3195 if (revs->full_diff)
3196 save_parents(revs, commit);
3197 if (rewrite_parents(revs, commit, rewrite_one) < 0)
3198 return commit_error;
3200 return action;
3203 static void track_linear(struct rev_info *revs, struct commit *commit)
3205 if (revs->track_first_time) {
3206 revs->linear = 1;
3207 revs->track_first_time = 0;
3208 } else {
3209 struct commit_list *p;
3210 for (p = revs->previous_parents; p; p = p->next)
3211 if (p->item == NULL || /* first commit */
3212 !oidcmp(&p->item->object.oid, &commit->object.oid))
3213 break;
3214 revs->linear = p != NULL;
3216 if (revs->reverse) {
3217 if (revs->linear)
3218 commit->object.flags |= TRACK_LINEAR;
3220 free_commit_list(revs->previous_parents);
3221 revs->previous_parents = copy_commit_list(commit->parents);
3224 static struct commit *get_revision_1(struct rev_info *revs)
3226 while (1) {
3227 struct commit *commit;
3229 if (revs->reflog_info)
3230 commit = next_reflog_entry(revs->reflog_info);
3231 else
3232 commit = pop_commit(&revs->commits);
3234 if (!commit)
3235 return NULL;
3237 if (revs->reflog_info)
3238 commit->object.flags &= ~(ADDED | SEEN | SHOWN);
3241 * If we haven't done the list limiting, we need to look at
3242 * the parents here. We also need to do the date-based limiting
3243 * that we'd otherwise have done in limit_list().
3245 if (!revs->limited) {
3246 if (revs->max_age != -1 &&
3247 comparison_date(revs, commit) < revs->max_age)
3248 continue;
3250 if (revs->reflog_info)
3251 try_to_simplify_commit(revs, commit);
3252 else if (add_parents_to_list(revs, commit, &revs->commits, NULL) < 0) {
3253 if (!revs->ignore_missing_links)
3254 die("Failed to traverse parents of commit %s",
3255 oid_to_hex(&commit->object.oid));
3259 switch (simplify_commit(revs, commit)) {
3260 case commit_ignore:
3261 continue;
3262 case commit_error:
3263 die("Failed to simplify parents of commit %s",
3264 oid_to_hex(&commit->object.oid));
3265 default:
3266 if (revs->track_linear)
3267 track_linear(revs, commit);
3268 return commit;
3274 * Return true for entries that have not yet been shown. (This is an
3275 * object_array_each_func_t.)
3277 static int entry_unshown(struct object_array_entry *entry, void *cb_data_unused)
3279 return !(entry->item->flags & SHOWN);
3283 * If array is on the verge of a realloc, garbage-collect any entries
3284 * that have already been shown to try to free up some space.
3286 static void gc_boundary(struct object_array *array)
3288 if (array->nr == array->alloc)
3289 object_array_filter(array, entry_unshown, NULL);
3292 static void create_boundary_commit_list(struct rev_info *revs)
3294 unsigned i;
3295 struct commit *c;
3296 struct object_array *array = &revs->boundary_commits;
3297 struct object_array_entry *objects = array->objects;
3300 * If revs->commits is non-NULL at this point, an error occurred in
3301 * get_revision_1(). Ignore the error and continue printing the
3302 * boundary commits anyway. (This is what the code has always
3303 * done.)
3305 if (revs->commits) {
3306 free_commit_list(revs->commits);
3307 revs->commits = NULL;
3311 * Put all of the actual boundary commits from revs->boundary_commits
3312 * into revs->commits
3314 for (i = 0; i < array->nr; i++) {
3315 c = (struct commit *)(objects[i].item);
3316 if (!c)
3317 continue;
3318 if (!(c->object.flags & CHILD_SHOWN))
3319 continue;
3320 if (c->object.flags & (SHOWN | BOUNDARY))
3321 continue;
3322 c->object.flags |= BOUNDARY;
3323 commit_list_insert(c, &revs->commits);
3327 * If revs->topo_order is set, sort the boundary commits
3328 * in topological order
3330 sort_in_topological_order(&revs->commits, revs->sort_order);
3333 static struct commit *get_revision_internal(struct rev_info *revs)
3335 struct commit *c = NULL;
3336 struct commit_list *l;
3338 if (revs->boundary == 2) {
3340 * All of the normal commits have already been returned,
3341 * and we are now returning boundary commits.
3342 * create_boundary_commit_list() has populated
3343 * revs->commits with the remaining commits to return.
3345 c = pop_commit(&revs->commits);
3346 if (c)
3347 c->object.flags |= SHOWN;
3348 return c;
3352 * If our max_count counter has reached zero, then we are done. We
3353 * don't simply return NULL because we still might need to show
3354 * boundary commits. But we want to avoid calling get_revision_1, which
3355 * might do a considerable amount of work finding the next commit only
3356 * for us to throw it away.
3358 * If it is non-zero, then either we don't have a max_count at all
3359 * (-1), or it is still counting, in which case we decrement.
3361 if (revs->max_count) {
3362 c = get_revision_1(revs);
3363 if (c) {
3364 while (revs->skip_count > 0) {
3365 revs->skip_count--;
3366 c = get_revision_1(revs);
3367 if (!c)
3368 break;
3372 if (revs->max_count > 0)
3373 revs->max_count--;
3376 if (c)
3377 c->object.flags |= SHOWN;
3379 if (!revs->boundary)
3380 return c;
3382 if (!c) {
3384 * get_revision_1() runs out the commits, and
3385 * we are done computing the boundaries.
3386 * switch to boundary commits output mode.
3388 revs->boundary = 2;
3391 * Update revs->commits to contain the list of
3392 * boundary commits.
3394 create_boundary_commit_list(revs);
3396 return get_revision_internal(revs);
3400 * boundary commits are the commits that are parents of the
3401 * ones we got from get_revision_1() but they themselves are
3402 * not returned from get_revision_1(). Before returning
3403 * 'c', we need to mark its parents that they could be boundaries.
3406 for (l = c->parents; l; l = l->next) {
3407 struct object *p;
3408 p = &(l->item->object);
3409 if (p->flags & (CHILD_SHOWN | SHOWN))
3410 continue;
3411 p->flags |= CHILD_SHOWN;
3412 gc_boundary(&revs->boundary_commits);
3413 add_object_array(p, NULL, &revs->boundary_commits);
3416 return c;
3419 struct commit *get_revision(struct rev_info *revs)
3421 struct commit *c;
3422 struct commit_list *reversed;
3424 if (revs->reverse) {
3425 reversed = NULL;
3426 while ((c = get_revision_internal(revs)))
3427 commit_list_insert(c, &reversed);
3428 revs->commits = reversed;
3429 revs->reverse = 0;
3430 revs->reverse_output_stage = 1;
3433 if (revs->reverse_output_stage) {
3434 c = pop_commit(&revs->commits);
3435 if (revs->track_linear)
3436 revs->linear = !!(c && c->object.flags & TRACK_LINEAR);
3437 return c;
3440 c = get_revision_internal(revs);
3441 if (c && revs->graph)
3442 graph_update(revs->graph, c);
3443 if (!c) {
3444 free_saved_parents(revs);
3445 if (revs->previous_parents) {
3446 free_commit_list(revs->previous_parents);
3447 revs->previous_parents = NULL;
3450 return c;
3453 char *get_revision_mark(const struct rev_info *revs, const struct commit *commit)
3455 if (commit->object.flags & BOUNDARY)
3456 return "-";
3457 else if (commit->object.flags & UNINTERESTING)
3458 return "^";
3459 else if (commit->object.flags & PATCHSAME)
3460 return "=";
3461 else if (!revs || revs->left_right) {
3462 if (commit->object.flags & SYMMETRIC_LEFT)
3463 return "<";
3464 else
3465 return ">";
3466 } else if (revs->graph)
3467 return "*";
3468 else if (revs->cherry_mark)
3469 return "+";
3470 return "";
3473 void put_revision_mark(const struct rev_info *revs, const struct commit *commit)
3475 char *mark = get_revision_mark(revs, commit);
3476 if (!strlen(mark))
3477 return;
3478 fputs(mark, stdout);
3479 putchar(' ');