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