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