strbuf: add a helper function to call the editor "on an strbuf"
[git/debian.git] / revision.c
blob0e39b2b8a59e7ab47d729602aa9bf2231691d133
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"
28 #include "commit-graph.h"
29 #include "prio-queue.h"
30 #include "hashmap.h"
31 #include "utf8.h"
33 volatile show_early_output_fn_t show_early_output;
35 static const char *term_bad;
36 static const char *term_good;
38 implement_shared_commit_slab(revision_sources, char *);
40 void show_object_with_name(FILE *out, struct object *obj, const char *name)
42 const char *p;
44 fprintf(out, "%s ", oid_to_hex(&obj->oid));
45 for (p = name; *p && *p != '\n'; p++)
46 fputc(*p, out);
47 fputc('\n', out);
50 static void mark_blob_uninteresting(struct blob *blob)
52 if (!blob)
53 return;
54 if (blob->object.flags & UNINTERESTING)
55 return;
56 blob->object.flags |= UNINTERESTING;
59 static void mark_tree_contents_uninteresting(struct repository *r,
60 struct tree *tree)
62 struct tree_desc desc;
63 struct name_entry entry;
65 if (parse_tree_gently(tree, 1) < 0)
66 return;
68 init_tree_desc(&desc, tree->buffer, tree->size);
69 while (tree_entry(&desc, &entry)) {
70 switch (object_type(entry.mode)) {
71 case OBJ_TREE:
72 mark_tree_uninteresting(r, lookup_tree(r, &entry.oid));
73 break;
74 case OBJ_BLOB:
75 mark_blob_uninteresting(lookup_blob(r, &entry.oid));
76 break;
77 default:
78 /* Subproject commit - not in this repository */
79 break;
84 * We don't care about the tree any more
85 * after it has been marked uninteresting.
87 free_tree_buffer(tree);
90 void mark_tree_uninteresting(struct repository *r, struct tree *tree)
92 struct object *obj;
94 if (!tree)
95 return;
97 obj = &tree->object;
98 if (obj->flags & UNINTERESTING)
99 return;
100 obj->flags |= UNINTERESTING;
101 mark_tree_contents_uninteresting(r, tree);
104 struct path_and_oids_entry {
105 struct hashmap_entry ent;
106 char *path;
107 struct oidset trees;
110 static int path_and_oids_cmp(const void *hashmap_cmp_fn_data,
111 const struct hashmap_entry *eptr,
112 const struct hashmap_entry *entry_or_key,
113 const void *keydata)
115 const struct path_and_oids_entry *e1, *e2;
117 e1 = container_of(eptr, const struct path_and_oids_entry, ent);
118 e2 = container_of(entry_or_key, const struct path_and_oids_entry, ent);
120 return strcmp(e1->path, e2->path);
123 static void paths_and_oids_init(struct hashmap *map)
125 hashmap_init(map, path_and_oids_cmp, NULL, 0);
128 static void paths_and_oids_clear(struct hashmap *map)
130 struct hashmap_iter iter;
131 struct path_and_oids_entry *entry;
133 hashmap_for_each_entry(map, &iter, entry, ent /* member name */) {
134 oidset_clear(&entry->trees);
135 free(entry->path);
138 hashmap_free_entries(map, struct path_and_oids_entry, ent);
141 static void paths_and_oids_insert(struct hashmap *map,
142 const char *path,
143 const struct object_id *oid)
145 int hash = strhash(path);
146 struct path_and_oids_entry key;
147 struct path_and_oids_entry *entry;
149 hashmap_entry_init(&key.ent, hash);
151 /* use a shallow copy for the lookup */
152 key.path = (char *)path;
153 oidset_init(&key.trees, 0);
155 entry = hashmap_get_entry(map, &key, ent, NULL);
156 if (!entry) {
157 entry = xcalloc(1, sizeof(struct path_and_oids_entry));
158 hashmap_entry_init(&entry->ent, hash);
159 entry->path = xstrdup(key.path);
160 oidset_init(&entry->trees, 16);
161 hashmap_put(map, &entry->ent);
164 oidset_insert(&entry->trees, oid);
167 static void add_children_by_path(struct repository *r,
168 struct tree *tree,
169 struct hashmap *map)
171 struct tree_desc desc;
172 struct name_entry entry;
174 if (!tree)
175 return;
177 if (parse_tree_gently(tree, 1) < 0)
178 return;
180 init_tree_desc(&desc, tree->buffer, tree->size);
181 while (tree_entry(&desc, &entry)) {
182 switch (object_type(entry.mode)) {
183 case OBJ_TREE:
184 paths_and_oids_insert(map, entry.path, &entry.oid);
186 if (tree->object.flags & UNINTERESTING) {
187 struct tree *child = lookup_tree(r, &entry.oid);
188 if (child)
189 child->object.flags |= UNINTERESTING;
191 break;
192 case OBJ_BLOB:
193 if (tree->object.flags & UNINTERESTING) {
194 struct blob *child = lookup_blob(r, &entry.oid);
195 if (child)
196 child->object.flags |= UNINTERESTING;
198 break;
199 default:
200 /* Subproject commit - not in this repository */
201 break;
205 free_tree_buffer(tree);
208 void mark_trees_uninteresting_sparse(struct repository *r,
209 struct oidset *trees)
211 unsigned has_interesting = 0, has_uninteresting = 0;
212 struct hashmap map;
213 struct hashmap_iter map_iter;
214 struct path_and_oids_entry *entry;
215 struct object_id *oid;
216 struct oidset_iter iter;
218 oidset_iter_init(trees, &iter);
219 while ((!has_interesting || !has_uninteresting) &&
220 (oid = oidset_iter_next(&iter))) {
221 struct tree *tree = lookup_tree(r, oid);
223 if (!tree)
224 continue;
226 if (tree->object.flags & UNINTERESTING)
227 has_uninteresting = 1;
228 else
229 has_interesting = 1;
232 /* Do not walk unless we have both types of trees. */
233 if (!has_uninteresting || !has_interesting)
234 return;
236 paths_and_oids_init(&map);
238 oidset_iter_init(trees, &iter);
239 while ((oid = oidset_iter_next(&iter))) {
240 struct tree *tree = lookup_tree(r, oid);
241 add_children_by_path(r, tree, &map);
244 hashmap_for_each_entry(&map, &map_iter, entry, ent /* member name */)
245 mark_trees_uninteresting_sparse(r, &entry->trees);
247 paths_and_oids_clear(&map);
250 struct commit_stack {
251 struct commit **items;
252 size_t nr, alloc;
254 #define COMMIT_STACK_INIT { NULL, 0, 0 }
256 static void commit_stack_push(struct commit_stack *stack, struct commit *commit)
258 ALLOC_GROW(stack->items, stack->nr + 1, stack->alloc);
259 stack->items[stack->nr++] = commit;
262 static struct commit *commit_stack_pop(struct commit_stack *stack)
264 return stack->nr ? stack->items[--stack->nr] : NULL;
267 static void commit_stack_clear(struct commit_stack *stack)
269 FREE_AND_NULL(stack->items);
270 stack->nr = stack->alloc = 0;
273 static void mark_one_parent_uninteresting(struct commit *commit,
274 struct commit_stack *pending)
276 struct commit_list *l;
278 if (commit->object.flags & UNINTERESTING)
279 return;
280 commit->object.flags |= UNINTERESTING;
283 * Normally we haven't parsed the parent
284 * yet, so we won't have a parent of a parent
285 * here. However, it may turn out that we've
286 * reached this commit some other way (where it
287 * wasn't uninteresting), in which case we need
288 * to mark its parents recursively too..
290 for (l = commit->parents; l; l = l->next)
291 commit_stack_push(pending, l->item);
294 void mark_parents_uninteresting(struct commit *commit)
296 struct commit_stack pending = COMMIT_STACK_INIT;
297 struct commit_list *l;
299 for (l = commit->parents; l; l = l->next)
300 mark_one_parent_uninteresting(l->item, &pending);
302 while (pending.nr > 0)
303 mark_one_parent_uninteresting(commit_stack_pop(&pending),
304 &pending);
306 commit_stack_clear(&pending);
309 static void add_pending_object_with_path(struct rev_info *revs,
310 struct object *obj,
311 const char *name, unsigned mode,
312 const char *path)
314 if (!obj)
315 return;
316 if (revs->no_walk && (obj->flags & UNINTERESTING))
317 revs->no_walk = 0;
318 if (revs->reflog_info && obj->type == OBJ_COMMIT) {
319 struct strbuf buf = STRBUF_INIT;
320 int len = interpret_branch_name(name, 0, &buf, 0);
322 if (0 < len && name[len] && buf.len)
323 strbuf_addstr(&buf, name + len);
324 add_reflog_for_walk(revs->reflog_info,
325 (struct commit *)obj,
326 buf.buf[0] ? buf.buf: name);
327 strbuf_release(&buf);
328 return; /* do not add the commit itself */
330 add_object_array_with_path(obj, name, &revs->pending, mode, path);
333 static void add_pending_object_with_mode(struct rev_info *revs,
334 struct object *obj,
335 const char *name, unsigned mode)
337 add_pending_object_with_path(revs, obj, name, mode, NULL);
340 void add_pending_object(struct rev_info *revs,
341 struct object *obj, const char *name)
343 add_pending_object_with_mode(revs, obj, name, S_IFINVALID);
346 void add_head_to_pending(struct rev_info *revs)
348 struct object_id oid;
349 struct object *obj;
350 if (get_oid("HEAD", &oid))
351 return;
352 obj = parse_object(revs->repo, &oid);
353 if (!obj)
354 return;
355 add_pending_object(revs, obj, "HEAD");
358 static struct object *get_reference(struct rev_info *revs, const char *name,
359 const struct object_id *oid,
360 unsigned int flags)
362 struct object *object;
365 * If the repository has commit graphs, repo_parse_commit() avoids
366 * reading the object buffer, so use it whenever possible.
368 if (oid_object_info(revs->repo, oid, NULL) == OBJ_COMMIT) {
369 struct commit *c = lookup_commit(revs->repo, oid);
370 if (!repo_parse_commit(revs->repo, c))
371 object = (struct object *) c;
372 else
373 object = NULL;
374 } else {
375 object = parse_object(revs->repo, oid);
378 if (!object) {
379 if (revs->ignore_missing)
380 return object;
381 if (revs->exclude_promisor_objects && is_promisor_object(oid))
382 return NULL;
383 die("bad object %s", name);
385 object->flags |= flags;
386 return object;
389 void add_pending_oid(struct rev_info *revs, const char *name,
390 const struct object_id *oid, unsigned int flags)
392 struct object *object = get_reference(revs, name, oid, flags);
393 add_pending_object(revs, object, name);
396 static struct commit *handle_commit(struct rev_info *revs,
397 struct object_array_entry *entry)
399 struct object *object = entry->item;
400 const char *name = entry->name;
401 const char *path = entry->path;
402 unsigned int mode = entry->mode;
403 unsigned long flags = object->flags;
406 * Tag object? Look what it points to..
408 while (object->type == OBJ_TAG) {
409 struct tag *tag = (struct tag *) object;
410 if (revs->tag_objects && !(flags & UNINTERESTING))
411 add_pending_object(revs, object, tag->tag);
412 object = parse_object(revs->repo, get_tagged_oid(tag));
413 if (!object) {
414 if (revs->ignore_missing_links || (flags & UNINTERESTING))
415 return NULL;
416 if (revs->exclude_promisor_objects &&
417 is_promisor_object(&tag->tagged->oid))
418 return NULL;
419 die("bad object %s", oid_to_hex(&tag->tagged->oid));
421 object->flags |= flags;
423 * We'll handle the tagged object by looping or dropping
424 * through to the non-tag handlers below. Do not
425 * propagate path data from the tag's pending entry.
427 path = NULL;
428 mode = 0;
432 * Commit object? Just return it, we'll do all the complex
433 * reachability crud.
435 if (object->type == OBJ_COMMIT) {
436 struct commit *commit = (struct commit *)object;
438 if (parse_commit(commit) < 0)
439 die("unable to parse commit %s", name);
440 if (flags & UNINTERESTING) {
441 mark_parents_uninteresting(commit);
443 if (!revs->topo_order || !generation_numbers_enabled(the_repository))
444 revs->limited = 1;
446 if (revs->sources) {
447 char **slot = revision_sources_at(revs->sources, commit);
449 if (!*slot)
450 *slot = xstrdup(name);
452 return commit;
456 * Tree object? Either mark it uninteresting, or add it
457 * to the list of objects to look at later..
459 if (object->type == OBJ_TREE) {
460 struct tree *tree = (struct tree *)object;
461 if (!revs->tree_objects)
462 return NULL;
463 if (flags & UNINTERESTING) {
464 mark_tree_contents_uninteresting(revs->repo, tree);
465 return NULL;
467 add_pending_object_with_path(revs, object, name, mode, path);
468 return NULL;
472 * Blob object? You know the drill by now..
474 if (object->type == OBJ_BLOB) {
475 if (!revs->blob_objects)
476 return NULL;
477 if (flags & UNINTERESTING)
478 return NULL;
479 add_pending_object_with_path(revs, object, name, mode, path);
480 return NULL;
482 die("%s is unknown object", name);
485 static int everybody_uninteresting(struct commit_list *orig,
486 struct commit **interesting_cache)
488 struct commit_list *list = orig;
490 if (*interesting_cache) {
491 struct commit *commit = *interesting_cache;
492 if (!(commit->object.flags & UNINTERESTING))
493 return 0;
496 while (list) {
497 struct commit *commit = list->item;
498 list = list->next;
499 if (commit->object.flags & UNINTERESTING)
500 continue;
502 *interesting_cache = commit;
503 return 0;
505 return 1;
509 * A definition of "relevant" commit that we can use to simplify limited graphs
510 * by eliminating side branches.
512 * A "relevant" commit is one that is !UNINTERESTING (ie we are including it
513 * in our list), or that is a specified BOTTOM commit. Then after computing
514 * a limited list, during processing we can generally ignore boundary merges
515 * coming from outside the graph, (ie from irrelevant parents), and treat
516 * those merges as if they were single-parent. TREESAME is defined to consider
517 * only relevant parents, if any. If we are TREESAME to our on-graph parents,
518 * we don't care if we were !TREESAME to non-graph parents.
520 * Treating bottom commits as relevant ensures that a limited graph's
521 * connection to the actual bottom commit is not viewed as a side branch, but
522 * treated as part of the graph. For example:
524 * ....Z...A---X---o---o---B
525 * . /
526 * W---Y
528 * When computing "A..B", the A-X connection is at least as important as
529 * Y-X, despite A being flagged UNINTERESTING.
531 * And when computing --ancestry-path "A..B", the A-X connection is more
532 * important than Y-X, despite both A and Y being flagged UNINTERESTING.
534 static inline int relevant_commit(struct commit *commit)
536 return (commit->object.flags & (UNINTERESTING | BOTTOM)) != UNINTERESTING;
540 * Return a single relevant commit from a parent list. If we are a TREESAME
541 * commit, and this selects one of our parents, then we can safely simplify to
542 * that parent.
544 static struct commit *one_relevant_parent(const struct rev_info *revs,
545 struct commit_list *orig)
547 struct commit_list *list = orig;
548 struct commit *relevant = NULL;
550 if (!orig)
551 return NULL;
554 * For 1-parent commits, or if first-parent-only, then return that
555 * first parent (even if not "relevant" by the above definition).
556 * TREESAME will have been set purely on that parent.
558 if (revs->first_parent_only || !orig->next)
559 return orig->item;
562 * For multi-parent commits, identify a sole relevant parent, if any.
563 * If we have only one relevant parent, then TREESAME will be set purely
564 * with regard to that parent, and we can simplify accordingly.
566 * If we have more than one relevant parent, or no relevant parents
567 * (and multiple irrelevant ones), then we can't select a parent here
568 * and return NULL.
570 while (list) {
571 struct commit *commit = list->item;
572 list = list->next;
573 if (relevant_commit(commit)) {
574 if (relevant)
575 return NULL;
576 relevant = commit;
579 return relevant;
583 * The goal is to get REV_TREE_NEW as the result only if the
584 * diff consists of all '+' (and no other changes), REV_TREE_OLD
585 * if the whole diff is removal of old data, and otherwise
586 * REV_TREE_DIFFERENT (of course if the trees are the same we
587 * want REV_TREE_SAME).
589 * The only time we care about the distinction is when
590 * remove_empty_trees is in effect, in which case we care only about
591 * whether the whole change is REV_TREE_NEW, or if there's another type
592 * of change. Which means we can stop the diff early in either of these
593 * cases:
595 * 1. We're not using remove_empty_trees at all.
597 * 2. We saw anything except REV_TREE_NEW.
599 static int tree_difference = REV_TREE_SAME;
601 static void file_add_remove(struct diff_options *options,
602 int addremove, unsigned mode,
603 const struct object_id *oid,
604 int oid_valid,
605 const char *fullpath, unsigned dirty_submodule)
607 int diff = addremove == '+' ? REV_TREE_NEW : REV_TREE_OLD;
608 struct rev_info *revs = options->change_fn_data;
610 tree_difference |= diff;
611 if (!revs->remove_empty_trees || tree_difference != REV_TREE_NEW)
612 options->flags.has_changes = 1;
615 static void file_change(struct diff_options *options,
616 unsigned old_mode, unsigned new_mode,
617 const struct object_id *old_oid,
618 const struct object_id *new_oid,
619 int old_oid_valid, int new_oid_valid,
620 const char *fullpath,
621 unsigned old_dirty_submodule, unsigned new_dirty_submodule)
623 tree_difference = REV_TREE_DIFFERENT;
624 options->flags.has_changes = 1;
627 static int rev_compare_tree(struct rev_info *revs,
628 struct commit *parent, struct commit *commit)
630 struct tree *t1 = get_commit_tree(parent);
631 struct tree *t2 = get_commit_tree(commit);
633 if (!t1)
634 return REV_TREE_NEW;
635 if (!t2)
636 return REV_TREE_OLD;
638 if (revs->simplify_by_decoration) {
640 * If we are simplifying by decoration, then the commit
641 * is worth showing if it has a tag pointing at it.
643 if (get_name_decoration(&commit->object))
644 return REV_TREE_DIFFERENT;
646 * A commit that is not pointed by a tag is uninteresting
647 * if we are not limited by path. This means that you will
648 * see the usual "commits that touch the paths" plus any
649 * tagged commit by specifying both --simplify-by-decoration
650 * and pathspec.
652 if (!revs->prune_data.nr)
653 return REV_TREE_SAME;
656 tree_difference = REV_TREE_SAME;
657 revs->pruning.flags.has_changes = 0;
658 if (diff_tree_oid(&t1->object.oid, &t2->object.oid, "",
659 &revs->pruning) < 0)
660 return REV_TREE_DIFFERENT;
661 return tree_difference;
664 static int rev_same_tree_as_empty(struct rev_info *revs, struct commit *commit)
666 int retval;
667 struct tree *t1 = get_commit_tree(commit);
669 if (!t1)
670 return 0;
672 tree_difference = REV_TREE_SAME;
673 revs->pruning.flags.has_changes = 0;
674 retval = diff_tree_oid(NULL, &t1->object.oid, "", &revs->pruning);
676 return retval >= 0 && (tree_difference == REV_TREE_SAME);
679 struct treesame_state {
680 unsigned int nparents;
681 unsigned char treesame[FLEX_ARRAY];
684 static struct treesame_state *initialise_treesame(struct rev_info *revs, struct commit *commit)
686 unsigned n = commit_list_count(commit->parents);
687 struct treesame_state *st = xcalloc(1, st_add(sizeof(*st), n));
688 st->nparents = n;
689 add_decoration(&revs->treesame, &commit->object, st);
690 return st;
694 * Must be called immediately after removing the nth_parent from a commit's
695 * parent list, if we are maintaining the per-parent treesame[] decoration.
696 * This does not recalculate the master TREESAME flag - update_treesame()
697 * should be called to update it after a sequence of treesame[] modifications
698 * that may have affected it.
700 static int compact_treesame(struct rev_info *revs, struct commit *commit, unsigned nth_parent)
702 struct treesame_state *st;
703 int old_same;
705 if (!commit->parents) {
707 * Have just removed the only parent from a non-merge.
708 * Different handling, as we lack decoration.
710 if (nth_parent != 0)
711 die("compact_treesame %u", nth_parent);
712 old_same = !!(commit->object.flags & TREESAME);
713 if (rev_same_tree_as_empty(revs, commit))
714 commit->object.flags |= TREESAME;
715 else
716 commit->object.flags &= ~TREESAME;
717 return old_same;
720 st = lookup_decoration(&revs->treesame, &commit->object);
721 if (!st || nth_parent >= st->nparents)
722 die("compact_treesame %u", nth_parent);
724 old_same = st->treesame[nth_parent];
725 memmove(st->treesame + nth_parent,
726 st->treesame + nth_parent + 1,
727 st->nparents - nth_parent - 1);
730 * If we've just become a non-merge commit, update TREESAME
731 * immediately, and remove the no-longer-needed decoration.
732 * If still a merge, defer update until update_treesame().
734 if (--st->nparents == 1) {
735 if (commit->parents->next)
736 die("compact_treesame parents mismatch");
737 if (st->treesame[0] && revs->dense)
738 commit->object.flags |= TREESAME;
739 else
740 commit->object.flags &= ~TREESAME;
741 free(add_decoration(&revs->treesame, &commit->object, NULL));
744 return old_same;
747 static unsigned update_treesame(struct rev_info *revs, struct commit *commit)
749 if (commit->parents && commit->parents->next) {
750 unsigned n;
751 struct treesame_state *st;
752 struct commit_list *p;
753 unsigned relevant_parents;
754 unsigned relevant_change, irrelevant_change;
756 st = lookup_decoration(&revs->treesame, &commit->object);
757 if (!st)
758 die("update_treesame %s", oid_to_hex(&commit->object.oid));
759 relevant_parents = 0;
760 relevant_change = irrelevant_change = 0;
761 for (p = commit->parents, n = 0; p; n++, p = p->next) {
762 if (relevant_commit(p->item)) {
763 relevant_change |= !st->treesame[n];
764 relevant_parents++;
765 } else
766 irrelevant_change |= !st->treesame[n];
768 if (relevant_parents ? relevant_change : irrelevant_change)
769 commit->object.flags &= ~TREESAME;
770 else
771 commit->object.flags |= TREESAME;
774 return commit->object.flags & TREESAME;
777 static inline int limiting_can_increase_treesame(const struct rev_info *revs)
780 * TREESAME is irrelevant unless prune && dense;
781 * if simplify_history is set, we can't have a mixture of TREESAME and
782 * !TREESAME INTERESTING parents (and we don't have treesame[]
783 * decoration anyway);
784 * if first_parent_only is set, then the TREESAME flag is locked
785 * against the first parent (and again we lack treesame[] decoration).
787 return revs->prune && revs->dense &&
788 !revs->simplify_history &&
789 !revs->first_parent_only;
792 static void try_to_simplify_commit(struct rev_info *revs, struct commit *commit)
794 struct commit_list **pp, *parent;
795 struct treesame_state *ts = NULL;
796 int relevant_change = 0, irrelevant_change = 0;
797 int relevant_parents, nth_parent;
800 * If we don't do pruning, everything is interesting
802 if (!revs->prune)
803 return;
805 if (!get_commit_tree(commit))
806 return;
808 if (!commit->parents) {
809 if (rev_same_tree_as_empty(revs, commit))
810 commit->object.flags |= TREESAME;
811 return;
815 * Normal non-merge commit? If we don't want to make the
816 * history dense, we consider it always to be a change..
818 if (!revs->dense && !commit->parents->next)
819 return;
821 for (pp = &commit->parents, nth_parent = 0, relevant_parents = 0;
822 (parent = *pp) != NULL;
823 pp = &parent->next, nth_parent++) {
824 struct commit *p = parent->item;
825 if (relevant_commit(p))
826 relevant_parents++;
828 if (nth_parent == 1) {
830 * This our second loop iteration - so we now know
831 * we're dealing with a merge.
833 * Do not compare with later parents when we care only about
834 * the first parent chain, in order to avoid derailing the
835 * traversal to follow a side branch that brought everything
836 * in the path we are limited to by the pathspec.
838 if (revs->first_parent_only)
839 break;
841 * If this will remain a potentially-simplifiable
842 * merge, remember per-parent treesame if needed.
843 * Initialise the array with the comparison from our
844 * first iteration.
846 if (revs->treesame.name &&
847 !revs->simplify_history &&
848 !(commit->object.flags & UNINTERESTING)) {
849 ts = initialise_treesame(revs, commit);
850 if (!(irrelevant_change || relevant_change))
851 ts->treesame[0] = 1;
854 if (parse_commit(p) < 0)
855 die("cannot simplify commit %s (because of %s)",
856 oid_to_hex(&commit->object.oid),
857 oid_to_hex(&p->object.oid));
858 switch (rev_compare_tree(revs, p, commit)) {
859 case REV_TREE_SAME:
860 if (!revs->simplify_history || !relevant_commit(p)) {
861 /* Even if a merge with an uninteresting
862 * side branch brought the entire change
863 * we are interested in, we do not want
864 * to lose the other branches of this
865 * merge, so we just keep going.
867 if (ts)
868 ts->treesame[nth_parent] = 1;
869 continue;
871 parent->next = NULL;
872 commit->parents = parent;
873 commit->object.flags |= TREESAME;
874 return;
876 case REV_TREE_NEW:
877 if (revs->remove_empty_trees &&
878 rev_same_tree_as_empty(revs, p)) {
879 /* We are adding all the specified
880 * paths from this parent, so the
881 * history beyond this parent is not
882 * interesting. Remove its parents
883 * (they are grandparents for us).
884 * IOW, we pretend this parent is a
885 * "root" commit.
887 if (parse_commit(p) < 0)
888 die("cannot simplify commit %s (invalid %s)",
889 oid_to_hex(&commit->object.oid),
890 oid_to_hex(&p->object.oid));
891 p->parents = NULL;
893 /* fallthrough */
894 case REV_TREE_OLD:
895 case REV_TREE_DIFFERENT:
896 if (relevant_commit(p))
897 relevant_change = 1;
898 else
899 irrelevant_change = 1;
900 continue;
902 die("bad tree compare for commit %s", oid_to_hex(&commit->object.oid));
906 * TREESAME is straightforward for single-parent commits. For merge
907 * commits, it is most useful to define it so that "irrelevant"
908 * parents cannot make us !TREESAME - if we have any relevant
909 * parents, then we only consider TREESAMEness with respect to them,
910 * allowing irrelevant merges from uninteresting branches to be
911 * simplified away. Only if we have only irrelevant parents do we
912 * base TREESAME on them. Note that this logic is replicated in
913 * update_treesame, which should be kept in sync.
915 if (relevant_parents ? !relevant_change : !irrelevant_change)
916 commit->object.flags |= TREESAME;
919 static int process_parents(struct rev_info *revs, struct commit *commit,
920 struct commit_list **list, struct prio_queue *queue)
922 struct commit_list *parent = commit->parents;
923 unsigned left_flag;
925 if (commit->object.flags & ADDED)
926 return 0;
927 commit->object.flags |= ADDED;
929 if (revs->include_check &&
930 !revs->include_check(commit, revs->include_check_data))
931 return 0;
934 * If the commit is uninteresting, don't try to
935 * prune parents - we want the maximal uninteresting
936 * set.
938 * Normally we haven't parsed the parent
939 * yet, so we won't have a parent of a parent
940 * here. However, it may turn out that we've
941 * reached this commit some other way (where it
942 * wasn't uninteresting), in which case we need
943 * to mark its parents recursively too..
945 if (commit->object.flags & UNINTERESTING) {
946 while (parent) {
947 struct commit *p = parent->item;
948 parent = parent->next;
949 if (p)
950 p->object.flags |= UNINTERESTING;
951 if (parse_commit_gently(p, 1) < 0)
952 continue;
953 if (p->parents)
954 mark_parents_uninteresting(p);
955 if (p->object.flags & SEEN)
956 continue;
957 p->object.flags |= SEEN;
958 if (list)
959 commit_list_insert_by_date(p, list);
960 if (queue)
961 prio_queue_put(queue, p);
963 return 0;
967 * Ok, the commit wasn't uninteresting. Try to
968 * simplify the commit history and find the parent
969 * that has no differences in the path set if one exists.
971 try_to_simplify_commit(revs, commit);
973 if (revs->no_walk)
974 return 0;
976 left_flag = (commit->object.flags & SYMMETRIC_LEFT);
978 for (parent = commit->parents; parent; parent = parent->next) {
979 struct commit *p = parent->item;
980 int gently = revs->ignore_missing_links ||
981 revs->exclude_promisor_objects;
982 if (parse_commit_gently(p, gently) < 0) {
983 if (revs->exclude_promisor_objects &&
984 is_promisor_object(&p->object.oid)) {
985 if (revs->first_parent_only)
986 break;
987 continue;
989 return -1;
991 if (revs->sources) {
992 char **slot = revision_sources_at(revs->sources, p);
994 if (!*slot)
995 *slot = *revision_sources_at(revs->sources, commit);
997 p->object.flags |= left_flag;
998 if (!(p->object.flags & SEEN)) {
999 p->object.flags |= SEEN;
1000 if (list)
1001 commit_list_insert_by_date(p, list);
1002 if (queue)
1003 prio_queue_put(queue, p);
1005 if (revs->first_parent_only)
1006 break;
1008 return 0;
1011 static void cherry_pick_list(struct commit_list *list, struct rev_info *revs)
1013 struct commit_list *p;
1014 int left_count = 0, right_count = 0;
1015 int left_first;
1016 struct patch_ids ids;
1017 unsigned cherry_flag;
1019 /* First count the commits on the left and on the right */
1020 for (p = list; p; p = p->next) {
1021 struct commit *commit = p->item;
1022 unsigned flags = commit->object.flags;
1023 if (flags & BOUNDARY)
1025 else if (flags & SYMMETRIC_LEFT)
1026 left_count++;
1027 else
1028 right_count++;
1031 if (!left_count || !right_count)
1032 return;
1034 left_first = left_count < right_count;
1035 init_patch_ids(revs->repo, &ids);
1036 ids.diffopts.pathspec = revs->diffopt.pathspec;
1038 /* Compute patch-ids for one side */
1039 for (p = list; p; p = p->next) {
1040 struct commit *commit = p->item;
1041 unsigned flags = commit->object.flags;
1043 if (flags & BOUNDARY)
1044 continue;
1046 * If we have fewer left, left_first is set and we omit
1047 * commits on the right branch in this loop. If we have
1048 * fewer right, we skip the left ones.
1050 if (left_first != !!(flags & SYMMETRIC_LEFT))
1051 continue;
1052 add_commit_patch_id(commit, &ids);
1055 /* either cherry_mark or cherry_pick are true */
1056 cherry_flag = revs->cherry_mark ? PATCHSAME : SHOWN;
1058 /* Check the other side */
1059 for (p = list; p; p = p->next) {
1060 struct commit *commit = p->item;
1061 struct patch_id *id;
1062 unsigned flags = commit->object.flags;
1064 if (flags & BOUNDARY)
1065 continue;
1067 * If we have fewer left, left_first is set and we omit
1068 * commits on the left branch in this loop.
1070 if (left_first == !!(flags & SYMMETRIC_LEFT))
1071 continue;
1074 * Have we seen the same patch id?
1076 id = has_commit_patch_id(commit, &ids);
1077 if (!id)
1078 continue;
1080 commit->object.flags |= cherry_flag;
1081 id->commit->object.flags |= cherry_flag;
1084 free_patch_ids(&ids);
1087 /* How many extra uninteresting commits we want to see.. */
1088 #define SLOP 5
1090 static int still_interesting(struct commit_list *src, timestamp_t date, int slop,
1091 struct commit **interesting_cache)
1094 * No source list at all? We're definitely done..
1096 if (!src)
1097 return 0;
1100 * Does the destination list contain entries with a date
1101 * before the source list? Definitely _not_ done.
1103 if (date <= src->item->date)
1104 return SLOP;
1107 * Does the source list still have interesting commits in
1108 * it? Definitely not done..
1110 if (!everybody_uninteresting(src, interesting_cache))
1111 return SLOP;
1113 /* Ok, we're closing in.. */
1114 return slop-1;
1118 * "rev-list --ancestry-path A..B" computes commits that are ancestors
1119 * of B but not ancestors of A but further limits the result to those
1120 * that are descendants of A. This takes the list of bottom commits and
1121 * the result of "A..B" without --ancestry-path, and limits the latter
1122 * further to the ones that can reach one of the commits in "bottom".
1124 static void limit_to_ancestry(struct commit_list *bottom, struct commit_list *list)
1126 struct commit_list *p;
1127 struct commit_list *rlist = NULL;
1128 int made_progress;
1131 * Reverse the list so that it will be likely that we would
1132 * process parents before children.
1134 for (p = list; p; p = p->next)
1135 commit_list_insert(p->item, &rlist);
1137 for (p = bottom; p; p = p->next)
1138 p->item->object.flags |= TMP_MARK;
1141 * Mark the ones that can reach bottom commits in "list",
1142 * in a bottom-up fashion.
1144 do {
1145 made_progress = 0;
1146 for (p = rlist; p; p = p->next) {
1147 struct commit *c = p->item;
1148 struct commit_list *parents;
1149 if (c->object.flags & (TMP_MARK | UNINTERESTING))
1150 continue;
1151 for (parents = c->parents;
1152 parents;
1153 parents = parents->next) {
1154 if (!(parents->item->object.flags & TMP_MARK))
1155 continue;
1156 c->object.flags |= TMP_MARK;
1157 made_progress = 1;
1158 break;
1161 } while (made_progress);
1164 * NEEDSWORK: decide if we want to remove parents that are
1165 * not marked with TMP_MARK from commit->parents for commits
1166 * in the resulting list. We may not want to do that, though.
1170 * The ones that are not marked with TMP_MARK are uninteresting
1172 for (p = list; p; p = p->next) {
1173 struct commit *c = p->item;
1174 if (c->object.flags & TMP_MARK)
1175 continue;
1176 c->object.flags |= UNINTERESTING;
1179 /* We are done with the TMP_MARK */
1180 for (p = list; p; p = p->next)
1181 p->item->object.flags &= ~TMP_MARK;
1182 for (p = bottom; p; p = p->next)
1183 p->item->object.flags &= ~TMP_MARK;
1184 free_commit_list(rlist);
1188 * Before walking the history, keep the set of "negative" refs the
1189 * caller has asked to exclude.
1191 * This is used to compute "rev-list --ancestry-path A..B", as we need
1192 * to filter the result of "A..B" further to the ones that can actually
1193 * reach A.
1195 static struct commit_list *collect_bottom_commits(struct commit_list *list)
1197 struct commit_list *elem, *bottom = NULL;
1198 for (elem = list; elem; elem = elem->next)
1199 if (elem->item->object.flags & BOTTOM)
1200 commit_list_insert(elem->item, &bottom);
1201 return bottom;
1204 /* Assumes either left_only or right_only is set */
1205 static void limit_left_right(struct commit_list *list, struct rev_info *revs)
1207 struct commit_list *p;
1209 for (p = list; p; p = p->next) {
1210 struct commit *commit = p->item;
1212 if (revs->right_only) {
1213 if (commit->object.flags & SYMMETRIC_LEFT)
1214 commit->object.flags |= SHOWN;
1215 } else /* revs->left_only is set */
1216 if (!(commit->object.flags & SYMMETRIC_LEFT))
1217 commit->object.flags |= SHOWN;
1221 static int limit_list(struct rev_info *revs)
1223 int slop = SLOP;
1224 timestamp_t date = TIME_MAX;
1225 struct commit_list *list = revs->commits;
1226 struct commit_list *newlist = NULL;
1227 struct commit_list **p = &newlist;
1228 struct commit_list *bottom = NULL;
1229 struct commit *interesting_cache = NULL;
1231 if (revs->ancestry_path) {
1232 bottom = collect_bottom_commits(list);
1233 if (!bottom)
1234 die("--ancestry-path given but there are no bottom commits");
1237 while (list) {
1238 struct commit *commit = pop_commit(&list);
1239 struct object *obj = &commit->object;
1240 show_early_output_fn_t show;
1242 if (commit == interesting_cache)
1243 interesting_cache = NULL;
1245 if (revs->max_age != -1 && (commit->date < revs->max_age))
1246 obj->flags |= UNINTERESTING;
1247 if (process_parents(revs, commit, &list, NULL) < 0)
1248 return -1;
1249 if (obj->flags & UNINTERESTING) {
1250 mark_parents_uninteresting(commit);
1251 slop = still_interesting(list, date, slop, &interesting_cache);
1252 if (slop)
1253 continue;
1254 break;
1256 if (revs->min_age != -1 && (commit->date > revs->min_age))
1257 continue;
1258 date = commit->date;
1259 p = &commit_list_insert(commit, p)->next;
1261 show = show_early_output;
1262 if (!show)
1263 continue;
1265 show(revs, newlist);
1266 show_early_output = NULL;
1268 if (revs->cherry_pick || revs->cherry_mark)
1269 cherry_pick_list(newlist, revs);
1271 if (revs->left_only || revs->right_only)
1272 limit_left_right(newlist, revs);
1274 if (bottom) {
1275 limit_to_ancestry(bottom, newlist);
1276 free_commit_list(bottom);
1280 * Check if any commits have become TREESAME by some of their parents
1281 * becoming UNINTERESTING.
1283 if (limiting_can_increase_treesame(revs))
1284 for (list = newlist; list; list = list->next) {
1285 struct commit *c = list->item;
1286 if (c->object.flags & (UNINTERESTING | TREESAME))
1287 continue;
1288 update_treesame(revs, c);
1291 revs->commits = newlist;
1292 return 0;
1296 * Add an entry to refs->cmdline with the specified information.
1297 * *name is copied.
1299 static void add_rev_cmdline(struct rev_info *revs,
1300 struct object *item,
1301 const char *name,
1302 int whence,
1303 unsigned flags)
1305 struct rev_cmdline_info *info = &revs->cmdline;
1306 unsigned int nr = info->nr;
1308 ALLOC_GROW(info->rev, nr + 1, info->alloc);
1309 info->rev[nr].item = item;
1310 info->rev[nr].name = xstrdup(name);
1311 info->rev[nr].whence = whence;
1312 info->rev[nr].flags = flags;
1313 info->nr++;
1316 static void add_rev_cmdline_list(struct rev_info *revs,
1317 struct commit_list *commit_list,
1318 int whence,
1319 unsigned flags)
1321 while (commit_list) {
1322 struct object *object = &commit_list->item->object;
1323 add_rev_cmdline(revs, object, oid_to_hex(&object->oid),
1324 whence, flags);
1325 commit_list = commit_list->next;
1329 struct all_refs_cb {
1330 int all_flags;
1331 int warned_bad_reflog;
1332 struct rev_info *all_revs;
1333 const char *name_for_errormsg;
1334 struct worktree *wt;
1337 int ref_excluded(struct string_list *ref_excludes, const char *path)
1339 struct string_list_item *item;
1341 if (!ref_excludes)
1342 return 0;
1343 for_each_string_list_item(item, ref_excludes) {
1344 if (!wildmatch(item->string, path, 0))
1345 return 1;
1347 return 0;
1350 static int handle_one_ref(const char *path, const struct object_id *oid,
1351 int flag, void *cb_data)
1353 struct all_refs_cb *cb = cb_data;
1354 struct object *object;
1356 if (ref_excluded(cb->all_revs->ref_excludes, path))
1357 return 0;
1359 object = get_reference(cb->all_revs, path, oid, cb->all_flags);
1360 add_rev_cmdline(cb->all_revs, object, path, REV_CMD_REF, cb->all_flags);
1361 add_pending_oid(cb->all_revs, path, oid, cb->all_flags);
1362 return 0;
1365 static void init_all_refs_cb(struct all_refs_cb *cb, struct rev_info *revs,
1366 unsigned flags)
1368 cb->all_revs = revs;
1369 cb->all_flags = flags;
1370 revs->rev_input_given = 1;
1371 cb->wt = NULL;
1374 void clear_ref_exclusion(struct string_list **ref_excludes_p)
1376 if (*ref_excludes_p) {
1377 string_list_clear(*ref_excludes_p, 0);
1378 free(*ref_excludes_p);
1380 *ref_excludes_p = NULL;
1383 void add_ref_exclusion(struct string_list **ref_excludes_p, const char *exclude)
1385 if (!*ref_excludes_p) {
1386 *ref_excludes_p = xcalloc(1, sizeof(**ref_excludes_p));
1387 (*ref_excludes_p)->strdup_strings = 1;
1389 string_list_append(*ref_excludes_p, exclude);
1392 static void handle_refs(struct ref_store *refs,
1393 struct rev_info *revs, unsigned flags,
1394 int (*for_each)(struct ref_store *, each_ref_fn, void *))
1396 struct all_refs_cb cb;
1398 if (!refs) {
1399 /* this could happen with uninitialized submodules */
1400 return;
1403 init_all_refs_cb(&cb, revs, flags);
1404 for_each(refs, handle_one_ref, &cb);
1407 static void handle_one_reflog_commit(struct object_id *oid, void *cb_data)
1409 struct all_refs_cb *cb = cb_data;
1410 if (!is_null_oid(oid)) {
1411 struct object *o = parse_object(cb->all_revs->repo, oid);
1412 if (o) {
1413 o->flags |= cb->all_flags;
1414 /* ??? CMDLINEFLAGS ??? */
1415 add_pending_object(cb->all_revs, o, "");
1417 else if (!cb->warned_bad_reflog) {
1418 warning("reflog of '%s' references pruned commits",
1419 cb->name_for_errormsg);
1420 cb->warned_bad_reflog = 1;
1425 static int handle_one_reflog_ent(struct object_id *ooid, struct object_id *noid,
1426 const char *email, timestamp_t timestamp, int tz,
1427 const char *message, void *cb_data)
1429 handle_one_reflog_commit(ooid, cb_data);
1430 handle_one_reflog_commit(noid, cb_data);
1431 return 0;
1434 static int handle_one_reflog(const char *refname_in_wt,
1435 const struct object_id *oid,
1436 int flag, void *cb_data)
1438 struct all_refs_cb *cb = cb_data;
1439 struct strbuf refname = STRBUF_INIT;
1441 cb->warned_bad_reflog = 0;
1442 strbuf_worktree_ref(cb->wt, &refname, refname_in_wt);
1443 cb->name_for_errormsg = refname.buf;
1444 refs_for_each_reflog_ent(get_main_ref_store(the_repository),
1445 refname.buf,
1446 handle_one_reflog_ent, cb_data);
1447 strbuf_release(&refname);
1448 return 0;
1451 static void add_other_reflogs_to_pending(struct all_refs_cb *cb)
1453 struct worktree **worktrees, **p;
1455 worktrees = get_worktrees(0);
1456 for (p = worktrees; *p; p++) {
1457 struct worktree *wt = *p;
1459 if (wt->is_current)
1460 continue;
1462 cb->wt = wt;
1463 refs_for_each_reflog(get_worktree_ref_store(wt),
1464 handle_one_reflog,
1465 cb);
1467 free_worktrees(worktrees);
1470 void add_reflogs_to_pending(struct rev_info *revs, unsigned flags)
1472 struct all_refs_cb cb;
1474 cb.all_revs = revs;
1475 cb.all_flags = flags;
1476 cb.wt = NULL;
1477 for_each_reflog(handle_one_reflog, &cb);
1479 if (!revs->single_worktree)
1480 add_other_reflogs_to_pending(&cb);
1483 static void add_cache_tree(struct cache_tree *it, struct rev_info *revs,
1484 struct strbuf *path, unsigned int flags)
1486 size_t baselen = path->len;
1487 int i;
1489 if (it->entry_count >= 0) {
1490 struct tree *tree = lookup_tree(revs->repo, &it->oid);
1491 tree->object.flags |= flags;
1492 add_pending_object_with_path(revs, &tree->object, "",
1493 040000, path->buf);
1496 for (i = 0; i < it->subtree_nr; i++) {
1497 struct cache_tree_sub *sub = it->down[i];
1498 strbuf_addf(path, "%s%s", baselen ? "/" : "", sub->name);
1499 add_cache_tree(sub->cache_tree, revs, path, flags);
1500 strbuf_setlen(path, baselen);
1505 static void do_add_index_objects_to_pending(struct rev_info *revs,
1506 struct index_state *istate,
1507 unsigned int flags)
1509 int i;
1511 for (i = 0; i < istate->cache_nr; i++) {
1512 struct cache_entry *ce = istate->cache[i];
1513 struct blob *blob;
1515 if (S_ISGITLINK(ce->ce_mode))
1516 continue;
1518 blob = lookup_blob(revs->repo, &ce->oid);
1519 if (!blob)
1520 die("unable to add index blob to traversal");
1521 blob->object.flags |= flags;
1522 add_pending_object_with_path(revs, &blob->object, "",
1523 ce->ce_mode, ce->name);
1526 if (istate->cache_tree) {
1527 struct strbuf path = STRBUF_INIT;
1528 add_cache_tree(istate->cache_tree, revs, &path, flags);
1529 strbuf_release(&path);
1533 void add_index_objects_to_pending(struct rev_info *revs, unsigned int flags)
1535 struct worktree **worktrees, **p;
1537 repo_read_index(revs->repo);
1538 do_add_index_objects_to_pending(revs, revs->repo->index, flags);
1540 if (revs->single_worktree)
1541 return;
1543 worktrees = get_worktrees(0);
1544 for (p = worktrees; *p; p++) {
1545 struct worktree *wt = *p;
1546 struct index_state istate = { NULL };
1548 if (wt->is_current)
1549 continue; /* current index already taken care of */
1551 if (read_index_from(&istate,
1552 worktree_git_path(wt, "index"),
1553 get_worktree_git_dir(wt)) > 0)
1554 do_add_index_objects_to_pending(revs, &istate, flags);
1555 discard_index(&istate);
1557 free_worktrees(worktrees);
1560 struct add_alternate_refs_data {
1561 struct rev_info *revs;
1562 unsigned int flags;
1565 static void add_one_alternate_ref(const struct object_id *oid,
1566 void *vdata)
1568 const char *name = ".alternate";
1569 struct add_alternate_refs_data *data = vdata;
1570 struct object *obj;
1572 obj = get_reference(data->revs, name, oid, data->flags);
1573 add_rev_cmdline(data->revs, obj, name, REV_CMD_REV, data->flags);
1574 add_pending_object(data->revs, obj, name);
1577 static void add_alternate_refs_to_pending(struct rev_info *revs,
1578 unsigned int flags)
1580 struct add_alternate_refs_data data;
1581 data.revs = revs;
1582 data.flags = flags;
1583 for_each_alternate_ref(add_one_alternate_ref, &data);
1586 static int add_parents_only(struct rev_info *revs, const char *arg_, int flags,
1587 int exclude_parent)
1589 struct object_id oid;
1590 struct object *it;
1591 struct commit *commit;
1592 struct commit_list *parents;
1593 int parent_number;
1594 const char *arg = arg_;
1596 if (*arg == '^') {
1597 flags ^= UNINTERESTING | BOTTOM;
1598 arg++;
1600 if (get_oid_committish(arg, &oid))
1601 return 0;
1602 while (1) {
1603 it = get_reference(revs, arg, &oid, 0);
1604 if (!it && revs->ignore_missing)
1605 return 0;
1606 if (it->type != OBJ_TAG)
1607 break;
1608 if (!((struct tag*)it)->tagged)
1609 return 0;
1610 oidcpy(&oid, &((struct tag*)it)->tagged->oid);
1612 if (it->type != OBJ_COMMIT)
1613 return 0;
1614 commit = (struct commit *)it;
1615 if (exclude_parent &&
1616 exclude_parent > commit_list_count(commit->parents))
1617 return 0;
1618 for (parents = commit->parents, parent_number = 1;
1619 parents;
1620 parents = parents->next, parent_number++) {
1621 if (exclude_parent && parent_number != exclude_parent)
1622 continue;
1624 it = &parents->item->object;
1625 it->flags |= flags;
1626 add_rev_cmdline(revs, it, arg_, REV_CMD_PARENTS_ONLY, flags);
1627 add_pending_object(revs, it, arg);
1629 return 1;
1632 void repo_init_revisions(struct repository *r,
1633 struct rev_info *revs,
1634 const char *prefix)
1636 memset(revs, 0, sizeof(*revs));
1638 revs->repo = r;
1639 revs->abbrev = DEFAULT_ABBREV;
1640 revs->ignore_merges = 1;
1641 revs->simplify_history = 1;
1642 revs->pruning.repo = r;
1643 revs->pruning.flags.recursive = 1;
1644 revs->pruning.flags.quick = 1;
1645 revs->pruning.add_remove = file_add_remove;
1646 revs->pruning.change = file_change;
1647 revs->pruning.change_fn_data = revs;
1648 revs->sort_order = REV_SORT_IN_GRAPH_ORDER;
1649 revs->dense = 1;
1650 revs->prefix = prefix;
1651 revs->max_age = -1;
1652 revs->min_age = -1;
1653 revs->skip_count = -1;
1654 revs->max_count = -1;
1655 revs->max_parents = -1;
1656 revs->expand_tabs_in_log = -1;
1658 revs->commit_format = CMIT_FMT_DEFAULT;
1659 revs->expand_tabs_in_log_default = 8;
1661 init_grep_defaults(revs->repo);
1662 grep_init(&revs->grep_filter, revs->repo, prefix);
1663 revs->grep_filter.status_only = 1;
1665 repo_diff_setup(revs->repo, &revs->diffopt);
1666 if (prefix && !revs->diffopt.prefix) {
1667 revs->diffopt.prefix = prefix;
1668 revs->diffopt.prefix_length = strlen(prefix);
1671 revs->notes_opt.use_default_notes = -1;
1674 static void add_pending_commit_list(struct rev_info *revs,
1675 struct commit_list *commit_list,
1676 unsigned int flags)
1678 while (commit_list) {
1679 struct object *object = &commit_list->item->object;
1680 object->flags |= flags;
1681 add_pending_object(revs, object, oid_to_hex(&object->oid));
1682 commit_list = commit_list->next;
1686 static void prepare_show_merge(struct rev_info *revs)
1688 struct commit_list *bases;
1689 struct commit *head, *other;
1690 struct object_id oid;
1691 const char **prune = NULL;
1692 int i, prune_num = 1; /* counting terminating NULL */
1693 struct index_state *istate = revs->repo->index;
1695 if (get_oid("HEAD", &oid))
1696 die("--merge without HEAD?");
1697 head = lookup_commit_or_die(&oid, "HEAD");
1698 if (get_oid("MERGE_HEAD", &oid))
1699 die("--merge without MERGE_HEAD?");
1700 other = lookup_commit_or_die(&oid, "MERGE_HEAD");
1701 add_pending_object(revs, &head->object, "HEAD");
1702 add_pending_object(revs, &other->object, "MERGE_HEAD");
1703 bases = get_merge_bases(head, other);
1704 add_rev_cmdline_list(revs, bases, REV_CMD_MERGE_BASE, UNINTERESTING | BOTTOM);
1705 add_pending_commit_list(revs, bases, UNINTERESTING | BOTTOM);
1706 free_commit_list(bases);
1707 head->object.flags |= SYMMETRIC_LEFT;
1709 if (!istate->cache_nr)
1710 repo_read_index(revs->repo);
1711 for (i = 0; i < istate->cache_nr; i++) {
1712 const struct cache_entry *ce = istate->cache[i];
1713 if (!ce_stage(ce))
1714 continue;
1715 if (ce_path_match(istate, ce, &revs->prune_data, NULL)) {
1716 prune_num++;
1717 REALLOC_ARRAY(prune, prune_num);
1718 prune[prune_num-2] = ce->name;
1719 prune[prune_num-1] = NULL;
1721 while ((i+1 < istate->cache_nr) &&
1722 ce_same_name(ce, istate->cache[i+1]))
1723 i++;
1725 clear_pathspec(&revs->prune_data);
1726 parse_pathspec(&revs->prune_data, PATHSPEC_ALL_MAGIC & ~PATHSPEC_LITERAL,
1727 PATHSPEC_PREFER_FULL | PATHSPEC_LITERAL_PATH, "", prune);
1728 revs->limited = 1;
1731 static int dotdot_missing(const char *arg, char *dotdot,
1732 struct rev_info *revs, int symmetric)
1734 if (revs->ignore_missing)
1735 return 0;
1736 /* de-munge so we report the full argument */
1737 *dotdot = '.';
1738 die(symmetric
1739 ? "Invalid symmetric difference expression %s"
1740 : "Invalid revision range %s", arg);
1743 static int handle_dotdot_1(const char *arg, char *dotdot,
1744 struct rev_info *revs, int flags,
1745 int cant_be_filename,
1746 struct object_context *a_oc,
1747 struct object_context *b_oc)
1749 const char *a_name, *b_name;
1750 struct object_id a_oid, b_oid;
1751 struct object *a_obj, *b_obj;
1752 unsigned int a_flags, b_flags;
1753 int symmetric = 0;
1754 unsigned int flags_exclude = flags ^ (UNINTERESTING | BOTTOM);
1755 unsigned int oc_flags = GET_OID_COMMITTISH | GET_OID_RECORD_PATH;
1757 a_name = arg;
1758 if (!*a_name)
1759 a_name = "HEAD";
1761 b_name = dotdot + 2;
1762 if (*b_name == '.') {
1763 symmetric = 1;
1764 b_name++;
1766 if (!*b_name)
1767 b_name = "HEAD";
1769 if (get_oid_with_context(revs->repo, a_name, oc_flags, &a_oid, a_oc) ||
1770 get_oid_with_context(revs->repo, b_name, oc_flags, &b_oid, b_oc))
1771 return -1;
1773 if (!cant_be_filename) {
1774 *dotdot = '.';
1775 verify_non_filename(revs->prefix, arg);
1776 *dotdot = '\0';
1779 a_obj = parse_object(revs->repo, &a_oid);
1780 b_obj = parse_object(revs->repo, &b_oid);
1781 if (!a_obj || !b_obj)
1782 return dotdot_missing(arg, dotdot, revs, symmetric);
1784 if (!symmetric) {
1785 /* just A..B */
1786 b_flags = flags;
1787 a_flags = flags_exclude;
1788 } else {
1789 /* A...B -- find merge bases between the two */
1790 struct commit *a, *b;
1791 struct commit_list *exclude;
1793 a = lookup_commit_reference(revs->repo, &a_obj->oid);
1794 b = lookup_commit_reference(revs->repo, &b_obj->oid);
1795 if (!a || !b)
1796 return dotdot_missing(arg, dotdot, revs, symmetric);
1798 exclude = get_merge_bases(a, b);
1799 add_rev_cmdline_list(revs, exclude, REV_CMD_MERGE_BASE,
1800 flags_exclude);
1801 add_pending_commit_list(revs, exclude, flags_exclude);
1802 free_commit_list(exclude);
1804 b_flags = flags;
1805 a_flags = flags | SYMMETRIC_LEFT;
1808 a_obj->flags |= a_flags;
1809 b_obj->flags |= b_flags;
1810 add_rev_cmdline(revs, a_obj, a_name, REV_CMD_LEFT, a_flags);
1811 add_rev_cmdline(revs, b_obj, b_name, REV_CMD_RIGHT, b_flags);
1812 add_pending_object_with_path(revs, a_obj, a_name, a_oc->mode, a_oc->path);
1813 add_pending_object_with_path(revs, b_obj, b_name, b_oc->mode, b_oc->path);
1814 return 0;
1817 static int handle_dotdot(const char *arg,
1818 struct rev_info *revs, int flags,
1819 int cant_be_filename)
1821 struct object_context a_oc, b_oc;
1822 char *dotdot = strstr(arg, "..");
1823 int ret;
1825 if (!dotdot)
1826 return -1;
1828 memset(&a_oc, 0, sizeof(a_oc));
1829 memset(&b_oc, 0, sizeof(b_oc));
1831 *dotdot = '\0';
1832 ret = handle_dotdot_1(arg, dotdot, revs, flags, cant_be_filename,
1833 &a_oc, &b_oc);
1834 *dotdot = '.';
1836 free(a_oc.path);
1837 free(b_oc.path);
1839 return ret;
1842 int handle_revision_arg(const char *arg_, struct rev_info *revs, int flags, unsigned revarg_opt)
1844 struct object_context oc;
1845 char *mark;
1846 struct object *object;
1847 struct object_id oid;
1848 int local_flags;
1849 const char *arg = arg_;
1850 int cant_be_filename = revarg_opt & REVARG_CANNOT_BE_FILENAME;
1851 unsigned get_sha1_flags = GET_OID_RECORD_PATH;
1853 flags = flags & UNINTERESTING ? flags | BOTTOM : flags & ~BOTTOM;
1855 if (!cant_be_filename && !strcmp(arg, "..")) {
1857 * Just ".."? That is not a range but the
1858 * pathspec for the parent directory.
1860 return -1;
1863 if (!handle_dotdot(arg, revs, flags, revarg_opt))
1864 return 0;
1866 mark = strstr(arg, "^@");
1867 if (mark && !mark[2]) {
1868 *mark = 0;
1869 if (add_parents_only(revs, arg, flags, 0))
1870 return 0;
1871 *mark = '^';
1873 mark = strstr(arg, "^!");
1874 if (mark && !mark[2]) {
1875 *mark = 0;
1876 if (!add_parents_only(revs, arg, flags ^ (UNINTERESTING | BOTTOM), 0))
1877 *mark = '^';
1879 mark = strstr(arg, "^-");
1880 if (mark) {
1881 int exclude_parent = 1;
1883 if (mark[2]) {
1884 char *end;
1885 exclude_parent = strtoul(mark + 2, &end, 10);
1886 if (*end != '\0' || !exclude_parent)
1887 return -1;
1890 *mark = 0;
1891 if (!add_parents_only(revs, arg, flags ^ (UNINTERESTING | BOTTOM), exclude_parent))
1892 *mark = '^';
1895 local_flags = 0;
1896 if (*arg == '^') {
1897 local_flags = UNINTERESTING | BOTTOM;
1898 arg++;
1901 if (revarg_opt & REVARG_COMMITTISH)
1902 get_sha1_flags |= GET_OID_COMMITTISH;
1904 if (get_oid_with_context(revs->repo, arg, get_sha1_flags, &oid, &oc))
1905 return revs->ignore_missing ? 0 : -1;
1906 if (!cant_be_filename)
1907 verify_non_filename(revs->prefix, arg);
1908 object = get_reference(revs, arg, &oid, flags ^ local_flags);
1909 if (!object)
1910 return revs->ignore_missing ? 0 : -1;
1911 add_rev_cmdline(revs, object, arg_, REV_CMD_REV, flags ^ local_flags);
1912 add_pending_object_with_path(revs, object, arg, oc.mode, oc.path);
1913 free(oc.path);
1914 return 0;
1917 static void read_pathspec_from_stdin(struct strbuf *sb,
1918 struct argv_array *prune)
1920 while (strbuf_getline(sb, stdin) != EOF)
1921 argv_array_push(prune, sb->buf);
1924 static void read_revisions_from_stdin(struct rev_info *revs,
1925 struct argv_array *prune)
1927 struct strbuf sb;
1928 int seen_dashdash = 0;
1929 int save_warning;
1931 save_warning = warn_on_object_refname_ambiguity;
1932 warn_on_object_refname_ambiguity = 0;
1934 strbuf_init(&sb, 1000);
1935 while (strbuf_getline(&sb, stdin) != EOF) {
1936 int len = sb.len;
1937 if (!len)
1938 break;
1939 if (sb.buf[0] == '-') {
1940 if (len == 2 && sb.buf[1] == '-') {
1941 seen_dashdash = 1;
1942 break;
1944 die("options not supported in --stdin mode");
1946 if (handle_revision_arg(sb.buf, revs, 0,
1947 REVARG_CANNOT_BE_FILENAME))
1948 die("bad revision '%s'", sb.buf);
1950 if (seen_dashdash)
1951 read_pathspec_from_stdin(&sb, prune);
1953 strbuf_release(&sb);
1954 warn_on_object_refname_ambiguity = save_warning;
1957 static void add_grep(struct rev_info *revs, const char *ptn, enum grep_pat_token what)
1959 append_grep_pattern(&revs->grep_filter, ptn, "command line", 0, what);
1962 static void add_header_grep(struct rev_info *revs, enum grep_header_field field, const char *pattern)
1964 append_header_grep_pattern(&revs->grep_filter, field, pattern);
1967 static void add_message_grep(struct rev_info *revs, const char *pattern)
1969 add_grep(revs, pattern, GREP_PATTERN_BODY);
1972 static int handle_revision_opt(struct rev_info *revs, int argc, const char **argv,
1973 int *unkc, const char **unkv,
1974 const struct setup_revision_opt* opt)
1976 const char *arg = argv[0];
1977 const char *optarg;
1978 int argcount;
1979 const unsigned hexsz = the_hash_algo->hexsz;
1981 /* pseudo revision arguments */
1982 if (!strcmp(arg, "--all") || !strcmp(arg, "--branches") ||
1983 !strcmp(arg, "--tags") || !strcmp(arg, "--remotes") ||
1984 !strcmp(arg, "--reflog") || !strcmp(arg, "--not") ||
1985 !strcmp(arg, "--no-walk") || !strcmp(arg, "--do-walk") ||
1986 !strcmp(arg, "--bisect") || starts_with(arg, "--glob=") ||
1987 !strcmp(arg, "--indexed-objects") ||
1988 !strcmp(arg, "--alternate-refs") ||
1989 starts_with(arg, "--exclude=") ||
1990 starts_with(arg, "--branches=") || starts_with(arg, "--tags=") ||
1991 starts_with(arg, "--remotes=") || starts_with(arg, "--no-walk="))
1993 unkv[(*unkc)++] = arg;
1994 return 1;
1997 if ((argcount = parse_long_opt("max-count", argv, &optarg))) {
1998 revs->max_count = atoi(optarg);
1999 revs->no_walk = 0;
2000 return argcount;
2001 } else if ((argcount = parse_long_opt("skip", argv, &optarg))) {
2002 revs->skip_count = atoi(optarg);
2003 return argcount;
2004 } else if ((*arg == '-') && isdigit(arg[1])) {
2005 /* accept -<digit>, like traditional "head" */
2006 if (strtol_i(arg + 1, 10, &revs->max_count) < 0 ||
2007 revs->max_count < 0)
2008 die("'%s': not a non-negative integer", arg + 1);
2009 revs->no_walk = 0;
2010 } else if (!strcmp(arg, "-n")) {
2011 if (argc <= 1)
2012 return error("-n requires an argument");
2013 revs->max_count = atoi(argv[1]);
2014 revs->no_walk = 0;
2015 return 2;
2016 } else if (skip_prefix(arg, "-n", &optarg)) {
2017 revs->max_count = atoi(optarg);
2018 revs->no_walk = 0;
2019 } else if ((argcount = parse_long_opt("max-age", argv, &optarg))) {
2020 revs->max_age = atoi(optarg);
2021 return argcount;
2022 } else if ((argcount = parse_long_opt("since", argv, &optarg))) {
2023 revs->max_age = approxidate(optarg);
2024 return argcount;
2025 } else if ((argcount = parse_long_opt("after", argv, &optarg))) {
2026 revs->max_age = approxidate(optarg);
2027 return argcount;
2028 } else if ((argcount = parse_long_opt("min-age", argv, &optarg))) {
2029 revs->min_age = atoi(optarg);
2030 return argcount;
2031 } else if ((argcount = parse_long_opt("before", argv, &optarg))) {
2032 revs->min_age = approxidate(optarg);
2033 return argcount;
2034 } else if ((argcount = parse_long_opt("until", argv, &optarg))) {
2035 revs->min_age = approxidate(optarg);
2036 return argcount;
2037 } else if (!strcmp(arg, "--first-parent")) {
2038 revs->first_parent_only = 1;
2039 } else if (!strcmp(arg, "--ancestry-path")) {
2040 revs->ancestry_path = 1;
2041 revs->simplify_history = 0;
2042 revs->limited = 1;
2043 } else if (!strcmp(arg, "-g") || !strcmp(arg, "--walk-reflogs")) {
2044 init_reflog_walk(&revs->reflog_info);
2045 } else if (!strcmp(arg, "--default")) {
2046 if (argc <= 1)
2047 return error("bad --default argument");
2048 revs->def = argv[1];
2049 return 2;
2050 } else if (!strcmp(arg, "--merge")) {
2051 revs->show_merge = 1;
2052 } else if (!strcmp(arg, "--topo-order")) {
2053 revs->sort_order = REV_SORT_IN_GRAPH_ORDER;
2054 revs->topo_order = 1;
2055 } else if (!strcmp(arg, "--simplify-merges")) {
2056 revs->simplify_merges = 1;
2057 revs->topo_order = 1;
2058 revs->rewrite_parents = 1;
2059 revs->simplify_history = 0;
2060 revs->limited = 1;
2061 } else if (!strcmp(arg, "--simplify-by-decoration")) {
2062 revs->simplify_merges = 1;
2063 revs->topo_order = 1;
2064 revs->rewrite_parents = 1;
2065 revs->simplify_history = 0;
2066 revs->simplify_by_decoration = 1;
2067 revs->limited = 1;
2068 revs->prune = 1;
2069 } else if (!strcmp(arg, "--date-order")) {
2070 revs->sort_order = REV_SORT_BY_COMMIT_DATE;
2071 revs->topo_order = 1;
2072 } else if (!strcmp(arg, "--author-date-order")) {
2073 revs->sort_order = REV_SORT_BY_AUTHOR_DATE;
2074 revs->topo_order = 1;
2075 } else if (!strcmp(arg, "--early-output")) {
2076 revs->early_output = 100;
2077 revs->topo_order = 1;
2078 } else if (skip_prefix(arg, "--early-output=", &optarg)) {
2079 if (strtoul_ui(optarg, 10, &revs->early_output) < 0)
2080 die("'%s': not a non-negative integer", optarg);
2081 revs->topo_order = 1;
2082 } else if (!strcmp(arg, "--parents")) {
2083 revs->rewrite_parents = 1;
2084 revs->print_parents = 1;
2085 } else if (!strcmp(arg, "--dense")) {
2086 revs->dense = 1;
2087 } else if (!strcmp(arg, "--sparse")) {
2088 revs->dense = 0;
2089 } else if (!strcmp(arg, "--in-commit-order")) {
2090 revs->tree_blobs_in_commit_order = 1;
2091 } else if (!strcmp(arg, "--remove-empty")) {
2092 revs->remove_empty_trees = 1;
2093 } else if (!strcmp(arg, "--merges")) {
2094 revs->min_parents = 2;
2095 } else if (!strcmp(arg, "--no-merges")) {
2096 revs->max_parents = 1;
2097 } else if (skip_prefix(arg, "--min-parents=", &optarg)) {
2098 revs->min_parents = atoi(optarg);
2099 } else if (!strcmp(arg, "--no-min-parents")) {
2100 revs->min_parents = 0;
2101 } else if (skip_prefix(arg, "--max-parents=", &optarg)) {
2102 revs->max_parents = atoi(optarg);
2103 } else if (!strcmp(arg, "--no-max-parents")) {
2104 revs->max_parents = -1;
2105 } else if (!strcmp(arg, "--boundary")) {
2106 revs->boundary = 1;
2107 } else if (!strcmp(arg, "--left-right")) {
2108 revs->left_right = 1;
2109 } else if (!strcmp(arg, "--left-only")) {
2110 if (revs->right_only)
2111 die("--left-only is incompatible with --right-only"
2112 " or --cherry");
2113 revs->left_only = 1;
2114 } else if (!strcmp(arg, "--right-only")) {
2115 if (revs->left_only)
2116 die("--right-only is incompatible with --left-only");
2117 revs->right_only = 1;
2118 } else if (!strcmp(arg, "--cherry")) {
2119 if (revs->left_only)
2120 die("--cherry is incompatible with --left-only");
2121 revs->cherry_mark = 1;
2122 revs->right_only = 1;
2123 revs->max_parents = 1;
2124 revs->limited = 1;
2125 } else if (!strcmp(arg, "--count")) {
2126 revs->count = 1;
2127 } else if (!strcmp(arg, "--cherry-mark")) {
2128 if (revs->cherry_pick)
2129 die("--cherry-mark is incompatible with --cherry-pick");
2130 revs->cherry_mark = 1;
2131 revs->limited = 1; /* needs limit_list() */
2132 } else if (!strcmp(arg, "--cherry-pick")) {
2133 if (revs->cherry_mark)
2134 die("--cherry-pick is incompatible with --cherry-mark");
2135 revs->cherry_pick = 1;
2136 revs->limited = 1;
2137 } else if (!strcmp(arg, "--objects")) {
2138 revs->tag_objects = 1;
2139 revs->tree_objects = 1;
2140 revs->blob_objects = 1;
2141 } else if (!strcmp(arg, "--objects-edge")) {
2142 revs->tag_objects = 1;
2143 revs->tree_objects = 1;
2144 revs->blob_objects = 1;
2145 revs->edge_hint = 1;
2146 } else if (!strcmp(arg, "--objects-edge-aggressive")) {
2147 revs->tag_objects = 1;
2148 revs->tree_objects = 1;
2149 revs->blob_objects = 1;
2150 revs->edge_hint = 1;
2151 revs->edge_hint_aggressive = 1;
2152 } else if (!strcmp(arg, "--verify-objects")) {
2153 revs->tag_objects = 1;
2154 revs->tree_objects = 1;
2155 revs->blob_objects = 1;
2156 revs->verify_objects = 1;
2157 } else if (!strcmp(arg, "--unpacked")) {
2158 revs->unpacked = 1;
2159 } else if (starts_with(arg, "--unpacked=")) {
2160 die("--unpacked=<packfile> no longer supported.");
2161 } else if (!strcmp(arg, "-r")) {
2162 revs->diff = 1;
2163 revs->diffopt.flags.recursive = 1;
2164 } else if (!strcmp(arg, "-t")) {
2165 revs->diff = 1;
2166 revs->diffopt.flags.recursive = 1;
2167 revs->diffopt.flags.tree_in_recursive = 1;
2168 } else if (!strcmp(arg, "-m")) {
2169 revs->ignore_merges = 0;
2170 } else if (!strcmp(arg, "-c")) {
2171 revs->diff = 1;
2172 revs->dense_combined_merges = 0;
2173 revs->combine_merges = 1;
2174 } else if (!strcmp(arg, "--combined-all-paths")) {
2175 revs->diff = 1;
2176 revs->combined_all_paths = 1;
2177 } else if (!strcmp(arg, "--cc")) {
2178 revs->diff = 1;
2179 revs->dense_combined_merges = 1;
2180 revs->combine_merges = 1;
2181 } else if (!strcmp(arg, "-v")) {
2182 revs->verbose_header = 1;
2183 } else if (!strcmp(arg, "--pretty")) {
2184 revs->verbose_header = 1;
2185 revs->pretty_given = 1;
2186 get_commit_format(NULL, revs);
2187 } else if (skip_prefix(arg, "--pretty=", &optarg) ||
2188 skip_prefix(arg, "--format=", &optarg)) {
2190 * Detached form ("--pretty X" as opposed to "--pretty=X")
2191 * not allowed, since the argument is optional.
2193 revs->verbose_header = 1;
2194 revs->pretty_given = 1;
2195 get_commit_format(optarg, revs);
2196 } else if (!strcmp(arg, "--expand-tabs")) {
2197 revs->expand_tabs_in_log = 8;
2198 } else if (!strcmp(arg, "--no-expand-tabs")) {
2199 revs->expand_tabs_in_log = 0;
2200 } else if (skip_prefix(arg, "--expand-tabs=", &arg)) {
2201 int val;
2202 if (strtol_i(arg, 10, &val) < 0 || val < 0)
2203 die("'%s': not a non-negative integer", arg);
2204 revs->expand_tabs_in_log = val;
2205 } else if (!strcmp(arg, "--show-notes") || !strcmp(arg, "--notes")) {
2206 revs->show_notes = 1;
2207 revs->show_notes_given = 1;
2208 revs->notes_opt.use_default_notes = 1;
2209 } else if (!strcmp(arg, "--show-signature")) {
2210 revs->show_signature = 1;
2211 } else if (!strcmp(arg, "--no-show-signature")) {
2212 revs->show_signature = 0;
2213 } else if (!strcmp(arg, "--show-linear-break")) {
2214 revs->break_bar = " ..........";
2215 revs->track_linear = 1;
2216 revs->track_first_time = 1;
2217 } else if (skip_prefix(arg, "--show-linear-break=", &optarg)) {
2218 revs->break_bar = xstrdup(optarg);
2219 revs->track_linear = 1;
2220 revs->track_first_time = 1;
2221 } else if (skip_prefix(arg, "--show-notes=", &optarg) ||
2222 skip_prefix(arg, "--notes=", &optarg)) {
2223 struct strbuf buf = STRBUF_INIT;
2224 revs->show_notes = 1;
2225 revs->show_notes_given = 1;
2226 if (starts_with(arg, "--show-notes=") &&
2227 revs->notes_opt.use_default_notes < 0)
2228 revs->notes_opt.use_default_notes = 1;
2229 strbuf_addstr(&buf, optarg);
2230 expand_notes_ref(&buf);
2231 string_list_append(&revs->notes_opt.extra_notes_refs,
2232 strbuf_detach(&buf, NULL));
2233 } else if (!strcmp(arg, "--no-notes")) {
2234 revs->show_notes = 0;
2235 revs->show_notes_given = 1;
2236 revs->notes_opt.use_default_notes = -1;
2237 /* we have been strdup'ing ourselves, so trick
2238 * string_list into free()ing strings */
2239 revs->notes_opt.extra_notes_refs.strdup_strings = 1;
2240 string_list_clear(&revs->notes_opt.extra_notes_refs, 0);
2241 revs->notes_opt.extra_notes_refs.strdup_strings = 0;
2242 } else if (!strcmp(arg, "--standard-notes")) {
2243 revs->show_notes_given = 1;
2244 revs->notes_opt.use_default_notes = 1;
2245 } else if (!strcmp(arg, "--no-standard-notes")) {
2246 revs->notes_opt.use_default_notes = 0;
2247 } else if (!strcmp(arg, "--oneline")) {
2248 revs->verbose_header = 1;
2249 get_commit_format("oneline", revs);
2250 revs->pretty_given = 1;
2251 revs->abbrev_commit = 1;
2252 } else if (!strcmp(arg, "--graph")) {
2253 revs->topo_order = 1;
2254 revs->rewrite_parents = 1;
2255 revs->graph = graph_init(revs);
2256 } else if (!strcmp(arg, "--root")) {
2257 revs->show_root_diff = 1;
2258 } else if (!strcmp(arg, "--no-commit-id")) {
2259 revs->no_commit_id = 1;
2260 } else if (!strcmp(arg, "--always")) {
2261 revs->always_show_header = 1;
2262 } else if (!strcmp(arg, "--no-abbrev")) {
2263 revs->abbrev = 0;
2264 } else if (!strcmp(arg, "--abbrev")) {
2265 revs->abbrev = DEFAULT_ABBREV;
2266 } else if (skip_prefix(arg, "--abbrev=", &optarg)) {
2267 revs->abbrev = strtoul(optarg, NULL, 10);
2268 if (revs->abbrev < MINIMUM_ABBREV)
2269 revs->abbrev = MINIMUM_ABBREV;
2270 else if (revs->abbrev > hexsz)
2271 revs->abbrev = hexsz;
2272 } else if (!strcmp(arg, "--abbrev-commit")) {
2273 revs->abbrev_commit = 1;
2274 revs->abbrev_commit_given = 1;
2275 } else if (!strcmp(arg, "--no-abbrev-commit")) {
2276 revs->abbrev_commit = 0;
2277 } else if (!strcmp(arg, "--full-diff")) {
2278 revs->diff = 1;
2279 revs->full_diff = 1;
2280 } else if (!strcmp(arg, "--full-history")) {
2281 revs->simplify_history = 0;
2282 } else if (!strcmp(arg, "--relative-date")) {
2283 revs->date_mode.type = DATE_RELATIVE;
2284 revs->date_mode_explicit = 1;
2285 } else if ((argcount = parse_long_opt("date", argv, &optarg))) {
2286 parse_date_format(optarg, &revs->date_mode);
2287 revs->date_mode_explicit = 1;
2288 return argcount;
2289 } else if (!strcmp(arg, "--log-size")) {
2290 revs->show_log_size = 1;
2293 * Grepping the commit log
2295 else if ((argcount = parse_long_opt("author", argv, &optarg))) {
2296 add_header_grep(revs, GREP_HEADER_AUTHOR, optarg);
2297 return argcount;
2298 } else if ((argcount = parse_long_opt("committer", argv, &optarg))) {
2299 add_header_grep(revs, GREP_HEADER_COMMITTER, optarg);
2300 return argcount;
2301 } else if ((argcount = parse_long_opt("grep-reflog", argv, &optarg))) {
2302 add_header_grep(revs, GREP_HEADER_REFLOG, optarg);
2303 return argcount;
2304 } else if ((argcount = parse_long_opt("grep", argv, &optarg))) {
2305 add_message_grep(revs, optarg);
2306 return argcount;
2307 } else if (!strcmp(arg, "--grep-debug")) {
2308 revs->grep_filter.debug = 1;
2309 } else if (!strcmp(arg, "--basic-regexp")) {
2310 revs->grep_filter.pattern_type_option = GREP_PATTERN_TYPE_BRE;
2311 } else if (!strcmp(arg, "--extended-regexp") || !strcmp(arg, "-E")) {
2312 revs->grep_filter.pattern_type_option = GREP_PATTERN_TYPE_ERE;
2313 } else if (!strcmp(arg, "--regexp-ignore-case") || !strcmp(arg, "-i")) {
2314 revs->grep_filter.ignore_case = 1;
2315 revs->diffopt.pickaxe_opts |= DIFF_PICKAXE_IGNORE_CASE;
2316 } else if (!strcmp(arg, "--fixed-strings") || !strcmp(arg, "-F")) {
2317 revs->grep_filter.pattern_type_option = GREP_PATTERN_TYPE_FIXED;
2318 } else if (!strcmp(arg, "--perl-regexp") || !strcmp(arg, "-P")) {
2319 revs->grep_filter.pattern_type_option = GREP_PATTERN_TYPE_PCRE;
2320 } else if (!strcmp(arg, "--all-match")) {
2321 revs->grep_filter.all_match = 1;
2322 } else if (!strcmp(arg, "--invert-grep")) {
2323 revs->invert_grep = 1;
2324 } else if ((argcount = parse_long_opt("encoding", argv, &optarg))) {
2325 if (strcmp(optarg, "none"))
2326 git_log_output_encoding = xstrdup(optarg);
2327 else
2328 git_log_output_encoding = "";
2329 return argcount;
2330 } else if (!strcmp(arg, "--reverse")) {
2331 revs->reverse ^= 1;
2332 } else if (!strcmp(arg, "--children")) {
2333 revs->children.name = "children";
2334 revs->limited = 1;
2335 } else if (!strcmp(arg, "--ignore-missing")) {
2336 revs->ignore_missing = 1;
2337 } else if (opt && opt->allow_exclude_promisor_objects &&
2338 !strcmp(arg, "--exclude-promisor-objects")) {
2339 if (fetch_if_missing)
2340 BUG("exclude_promisor_objects can only be used when fetch_if_missing is 0");
2341 revs->exclude_promisor_objects = 1;
2342 } else {
2343 int opts = diff_opt_parse(&revs->diffopt, argv, argc, revs->prefix);
2344 if (!opts)
2345 unkv[(*unkc)++] = arg;
2346 return opts;
2348 if (revs->graph && revs->track_linear)
2349 die("--show-linear-break and --graph are incompatible");
2351 return 1;
2354 void parse_revision_opt(struct rev_info *revs, struct parse_opt_ctx_t *ctx,
2355 const struct option *options,
2356 const char * const usagestr[])
2358 int n = handle_revision_opt(revs, ctx->argc, ctx->argv,
2359 &ctx->cpidx, ctx->out, NULL);
2360 if (n <= 0) {
2361 error("unknown option `%s'", ctx->argv[0]);
2362 usage_with_options(usagestr, options);
2364 ctx->argv += n;
2365 ctx->argc -= n;
2368 static int for_each_bisect_ref(struct ref_store *refs, each_ref_fn fn,
2369 void *cb_data, const char *term)
2371 struct strbuf bisect_refs = STRBUF_INIT;
2372 int status;
2373 strbuf_addf(&bisect_refs, "refs/bisect/%s", term);
2374 status = refs_for_each_fullref_in(refs, bisect_refs.buf, fn, cb_data, 0);
2375 strbuf_release(&bisect_refs);
2376 return status;
2379 static int for_each_bad_bisect_ref(struct ref_store *refs, each_ref_fn fn, void *cb_data)
2381 return for_each_bisect_ref(refs, fn, cb_data, term_bad);
2384 static int for_each_good_bisect_ref(struct ref_store *refs, each_ref_fn fn, void *cb_data)
2386 return for_each_bisect_ref(refs, fn, cb_data, term_good);
2389 static int handle_revision_pseudo_opt(const char *submodule,
2390 struct rev_info *revs,
2391 int argc, const char **argv, int *flags)
2393 const char *arg = argv[0];
2394 const char *optarg;
2395 struct ref_store *refs;
2396 int argcount;
2398 if (submodule) {
2400 * We need some something like get_submodule_worktrees()
2401 * before we can go through all worktrees of a submodule,
2402 * .e.g with adding all HEADs from --all, which is not
2403 * supported right now, so stick to single worktree.
2405 if (!revs->single_worktree)
2406 BUG("--single-worktree cannot be used together with submodule");
2407 refs = get_submodule_ref_store(submodule);
2408 } else
2409 refs = get_main_ref_store(revs->repo);
2412 * NOTE!
2414 * Commands like "git shortlog" will not accept the options below
2415 * unless parse_revision_opt queues them (as opposed to erroring
2416 * out).
2418 * When implementing your new pseudo-option, remember to
2419 * register it in the list at the top of handle_revision_opt.
2421 if (!strcmp(arg, "--all")) {
2422 handle_refs(refs, revs, *flags, refs_for_each_ref);
2423 handle_refs(refs, revs, *flags, refs_head_ref);
2424 if (!revs->single_worktree) {
2425 struct all_refs_cb cb;
2427 init_all_refs_cb(&cb, revs, *flags);
2428 other_head_refs(handle_one_ref, &cb);
2430 clear_ref_exclusion(&revs->ref_excludes);
2431 } else if (!strcmp(arg, "--branches")) {
2432 handle_refs(refs, revs, *flags, refs_for_each_branch_ref);
2433 clear_ref_exclusion(&revs->ref_excludes);
2434 } else if (!strcmp(arg, "--bisect")) {
2435 read_bisect_terms(&term_bad, &term_good);
2436 handle_refs(refs, revs, *flags, for_each_bad_bisect_ref);
2437 handle_refs(refs, revs, *flags ^ (UNINTERESTING | BOTTOM),
2438 for_each_good_bisect_ref);
2439 revs->bisect = 1;
2440 } else if (!strcmp(arg, "--tags")) {
2441 handle_refs(refs, revs, *flags, refs_for_each_tag_ref);
2442 clear_ref_exclusion(&revs->ref_excludes);
2443 } else if (!strcmp(arg, "--remotes")) {
2444 handle_refs(refs, revs, *flags, refs_for_each_remote_ref);
2445 clear_ref_exclusion(&revs->ref_excludes);
2446 } else if ((argcount = parse_long_opt("glob", argv, &optarg))) {
2447 struct all_refs_cb cb;
2448 init_all_refs_cb(&cb, revs, *flags);
2449 for_each_glob_ref(handle_one_ref, optarg, &cb);
2450 clear_ref_exclusion(&revs->ref_excludes);
2451 return argcount;
2452 } else if ((argcount = parse_long_opt("exclude", argv, &optarg))) {
2453 add_ref_exclusion(&revs->ref_excludes, optarg);
2454 return argcount;
2455 } else if (skip_prefix(arg, "--branches=", &optarg)) {
2456 struct all_refs_cb cb;
2457 init_all_refs_cb(&cb, revs, *flags);
2458 for_each_glob_ref_in(handle_one_ref, optarg, "refs/heads/", &cb);
2459 clear_ref_exclusion(&revs->ref_excludes);
2460 } else if (skip_prefix(arg, "--tags=", &optarg)) {
2461 struct all_refs_cb cb;
2462 init_all_refs_cb(&cb, revs, *flags);
2463 for_each_glob_ref_in(handle_one_ref, optarg, "refs/tags/", &cb);
2464 clear_ref_exclusion(&revs->ref_excludes);
2465 } else if (skip_prefix(arg, "--remotes=", &optarg)) {
2466 struct all_refs_cb cb;
2467 init_all_refs_cb(&cb, revs, *flags);
2468 for_each_glob_ref_in(handle_one_ref, optarg, "refs/remotes/", &cb);
2469 clear_ref_exclusion(&revs->ref_excludes);
2470 } else if (!strcmp(arg, "--reflog")) {
2471 add_reflogs_to_pending(revs, *flags);
2472 } else if (!strcmp(arg, "--indexed-objects")) {
2473 add_index_objects_to_pending(revs, *flags);
2474 } else if (!strcmp(arg, "--alternate-refs")) {
2475 add_alternate_refs_to_pending(revs, *flags);
2476 } else if (!strcmp(arg, "--not")) {
2477 *flags ^= UNINTERESTING | BOTTOM;
2478 } else if (!strcmp(arg, "--no-walk")) {
2479 revs->no_walk = REVISION_WALK_NO_WALK_SORTED;
2480 } else if (skip_prefix(arg, "--no-walk=", &optarg)) {
2482 * Detached form ("--no-walk X" as opposed to "--no-walk=X")
2483 * not allowed, since the argument is optional.
2485 if (!strcmp(optarg, "sorted"))
2486 revs->no_walk = REVISION_WALK_NO_WALK_SORTED;
2487 else if (!strcmp(optarg, "unsorted"))
2488 revs->no_walk = REVISION_WALK_NO_WALK_UNSORTED;
2489 else
2490 return error("invalid argument to --no-walk");
2491 } else if (!strcmp(arg, "--do-walk")) {
2492 revs->no_walk = 0;
2493 } else if (!strcmp(arg, "--single-worktree")) {
2494 revs->single_worktree = 1;
2495 } else {
2496 return 0;
2499 return 1;
2502 static void NORETURN diagnose_missing_default(const char *def)
2504 int flags;
2505 const char *refname;
2507 refname = resolve_ref_unsafe(def, 0, NULL, &flags);
2508 if (!refname || !(flags & REF_ISSYMREF) || (flags & REF_ISBROKEN))
2509 die(_("your current branch appears to be broken"));
2511 skip_prefix(refname, "refs/heads/", &refname);
2512 die(_("your current branch '%s' does not have any commits yet"),
2513 refname);
2517 * Parse revision information, filling in the "rev_info" structure,
2518 * and removing the used arguments from the argument list.
2520 * Returns the number of arguments left that weren't recognized
2521 * (which are also moved to the head of the argument list)
2523 int setup_revisions(int argc, const char **argv, struct rev_info *revs, struct setup_revision_opt *opt)
2525 int i, flags, left, seen_dashdash, got_rev_arg = 0, revarg_opt;
2526 struct argv_array prune_data = ARGV_ARRAY_INIT;
2527 const char *submodule = NULL;
2528 int seen_end_of_options = 0;
2530 if (opt)
2531 submodule = opt->submodule;
2533 /* First, search for "--" */
2534 if (opt && opt->assume_dashdash) {
2535 seen_dashdash = 1;
2536 } else {
2537 seen_dashdash = 0;
2538 for (i = 1; i < argc; i++) {
2539 const char *arg = argv[i];
2540 if (strcmp(arg, "--"))
2541 continue;
2542 argv[i] = NULL;
2543 argc = i;
2544 if (argv[i + 1])
2545 argv_array_pushv(&prune_data, argv + i + 1);
2546 seen_dashdash = 1;
2547 break;
2551 /* Second, deal with arguments and options */
2552 flags = 0;
2553 revarg_opt = opt ? opt->revarg_opt : 0;
2554 if (seen_dashdash)
2555 revarg_opt |= REVARG_CANNOT_BE_FILENAME;
2556 for (left = i = 1; i < argc; i++) {
2557 const char *arg = argv[i];
2558 if (!seen_end_of_options && *arg == '-') {
2559 int opts;
2561 opts = handle_revision_pseudo_opt(submodule,
2562 revs, argc - i, argv + i,
2563 &flags);
2564 if (opts > 0) {
2565 i += opts - 1;
2566 continue;
2569 if (!strcmp(arg, "--stdin")) {
2570 if (revs->disable_stdin) {
2571 argv[left++] = arg;
2572 continue;
2574 if (revs->read_from_stdin++)
2575 die("--stdin given twice?");
2576 read_revisions_from_stdin(revs, &prune_data);
2577 continue;
2580 if (!strcmp(arg, "--end-of-options")) {
2581 seen_end_of_options = 1;
2582 continue;
2585 opts = handle_revision_opt(revs, argc - i, argv + i,
2586 &left, argv, opt);
2587 if (opts > 0) {
2588 i += opts - 1;
2589 continue;
2591 if (opts < 0)
2592 exit(128);
2593 continue;
2597 if (handle_revision_arg(arg, revs, flags, revarg_opt)) {
2598 int j;
2599 if (seen_dashdash || *arg == '^')
2600 die("bad revision '%s'", arg);
2602 /* If we didn't have a "--":
2603 * (1) all filenames must exist;
2604 * (2) all rev-args must not be interpretable
2605 * as a valid filename.
2606 * but the latter we have checked in the main loop.
2608 for (j = i; j < argc; j++)
2609 verify_filename(revs->prefix, argv[j], j == i);
2611 argv_array_pushv(&prune_data, argv + i);
2612 break;
2614 else
2615 got_rev_arg = 1;
2618 if (prune_data.argc) {
2620 * If we need to introduce the magic "a lone ':' means no
2621 * pathspec whatsoever", here is the place to do so.
2623 * if (prune_data.nr == 1 && !strcmp(prune_data[0], ":")) {
2624 * prune_data.nr = 0;
2625 * prune_data.alloc = 0;
2626 * free(prune_data.path);
2627 * prune_data.path = NULL;
2628 * } else {
2629 * terminate prune_data.alloc with NULL and
2630 * call init_pathspec() to set revs->prune_data here.
2633 parse_pathspec(&revs->prune_data, 0, 0,
2634 revs->prefix, prune_data.argv);
2636 argv_array_clear(&prune_data);
2638 if (revs->def == NULL)
2639 revs->def = opt ? opt->def : NULL;
2640 if (opt && opt->tweak)
2641 opt->tweak(revs, opt);
2642 if (revs->show_merge)
2643 prepare_show_merge(revs);
2644 if (revs->def && !revs->pending.nr && !revs->rev_input_given && !got_rev_arg) {
2645 struct object_id oid;
2646 struct object *object;
2647 struct object_context oc;
2648 if (get_oid_with_context(revs->repo, revs->def, 0, &oid, &oc))
2649 diagnose_missing_default(revs->def);
2650 object = get_reference(revs, revs->def, &oid, 0);
2651 add_pending_object_with_mode(revs, object, revs->def, oc.mode);
2654 /* Did the user ask for any diff output? Run the diff! */
2655 if (revs->diffopt.output_format & ~DIFF_FORMAT_NO_OUTPUT)
2656 revs->diff = 1;
2658 /* Pickaxe, diff-filter and rename following need diffs */
2659 if ((revs->diffopt.pickaxe_opts & DIFF_PICKAXE_KINDS_MASK) ||
2660 revs->diffopt.filter ||
2661 revs->diffopt.flags.follow_renames)
2662 revs->diff = 1;
2664 if (revs->diffopt.objfind)
2665 revs->simplify_history = 0;
2667 if (revs->topo_order && !generation_numbers_enabled(the_repository))
2668 revs->limited = 1;
2670 if (revs->prune_data.nr) {
2671 copy_pathspec(&revs->pruning.pathspec, &revs->prune_data);
2672 /* Can't prune commits with rename following: the paths change.. */
2673 if (!revs->diffopt.flags.follow_renames)
2674 revs->prune = 1;
2675 if (!revs->full_diff)
2676 copy_pathspec(&revs->diffopt.pathspec,
2677 &revs->prune_data);
2679 if (revs->combine_merges)
2680 revs->ignore_merges = 0;
2681 if (revs->combined_all_paths && !revs->combine_merges)
2682 die("--combined-all-paths makes no sense without -c or --cc");
2684 revs->diffopt.abbrev = revs->abbrev;
2686 if (revs->line_level_traverse) {
2687 revs->limited = 1;
2688 revs->topo_order = 1;
2691 diff_setup_done(&revs->diffopt);
2693 grep_commit_pattern_type(GREP_PATTERN_TYPE_UNSPECIFIED,
2694 &revs->grep_filter);
2695 if (!is_encoding_utf8(get_log_output_encoding()))
2696 revs->grep_filter.ignore_locale = 1;
2697 compile_grep_patterns(&revs->grep_filter);
2699 if (revs->reverse && revs->reflog_info)
2700 die("cannot combine --reverse with --walk-reflogs");
2701 if (revs->reflog_info && revs->limited)
2702 die("cannot combine --walk-reflogs with history-limiting options");
2703 if (revs->rewrite_parents && revs->children.name)
2704 die("cannot combine --parents and --children");
2707 * Limitations on the graph functionality
2709 if (revs->reverse && revs->graph)
2710 die("cannot combine --reverse with --graph");
2712 if (revs->reflog_info && revs->graph)
2713 die("cannot combine --walk-reflogs with --graph");
2714 if (revs->no_walk && revs->graph)
2715 die("cannot combine --no-walk with --graph");
2716 if (!revs->reflog_info && revs->grep_filter.use_reflog_filter)
2717 die("cannot use --grep-reflog without --walk-reflogs");
2719 if (revs->first_parent_only && revs->bisect)
2720 die(_("--first-parent is incompatible with --bisect"));
2722 if (revs->line_level_traverse &&
2723 (revs->diffopt.output_format & ~(DIFF_FORMAT_PATCH | DIFF_FORMAT_NO_OUTPUT)))
2724 die(_("-L does not yet support diff formats besides -p and -s"));
2726 if (revs->expand_tabs_in_log < 0)
2727 revs->expand_tabs_in_log = revs->expand_tabs_in_log_default;
2729 return left;
2732 static void add_child(struct rev_info *revs, struct commit *parent, struct commit *child)
2734 struct commit_list *l = xcalloc(1, sizeof(*l));
2736 l->item = child;
2737 l->next = add_decoration(&revs->children, &parent->object, l);
2740 static int remove_duplicate_parents(struct rev_info *revs, struct commit *commit)
2742 struct treesame_state *ts = lookup_decoration(&revs->treesame, &commit->object);
2743 struct commit_list **pp, *p;
2744 int surviving_parents;
2746 /* Examine existing parents while marking ones we have seen... */
2747 pp = &commit->parents;
2748 surviving_parents = 0;
2749 while ((p = *pp) != NULL) {
2750 struct commit *parent = p->item;
2751 if (parent->object.flags & TMP_MARK) {
2752 *pp = p->next;
2753 if (ts)
2754 compact_treesame(revs, commit, surviving_parents);
2755 continue;
2757 parent->object.flags |= TMP_MARK;
2758 surviving_parents++;
2759 pp = &p->next;
2761 /* clear the temporary mark */
2762 for (p = commit->parents; p; p = p->next) {
2763 p->item->object.flags &= ~TMP_MARK;
2765 /* no update_treesame() - removing duplicates can't affect TREESAME */
2766 return surviving_parents;
2769 struct merge_simplify_state {
2770 struct commit *simplified;
2773 static struct merge_simplify_state *locate_simplify_state(struct rev_info *revs, struct commit *commit)
2775 struct merge_simplify_state *st;
2777 st = lookup_decoration(&revs->merge_simplification, &commit->object);
2778 if (!st) {
2779 st = xcalloc(1, sizeof(*st));
2780 add_decoration(&revs->merge_simplification, &commit->object, st);
2782 return st;
2785 static int mark_redundant_parents(struct commit *commit)
2787 struct commit_list *h = reduce_heads(commit->parents);
2788 int i = 0, marked = 0;
2789 struct commit_list *po, *pn;
2791 /* Want these for sanity-checking only */
2792 int orig_cnt = commit_list_count(commit->parents);
2793 int cnt = commit_list_count(h);
2796 * Not ready to remove items yet, just mark them for now, based
2797 * on the output of reduce_heads(). reduce_heads outputs the reduced
2798 * set in its original order, so this isn't too hard.
2800 po = commit->parents;
2801 pn = h;
2802 while (po) {
2803 if (pn && po->item == pn->item) {
2804 pn = pn->next;
2805 i++;
2806 } else {
2807 po->item->object.flags |= TMP_MARK;
2808 marked++;
2810 po=po->next;
2813 if (i != cnt || cnt+marked != orig_cnt)
2814 die("mark_redundant_parents %d %d %d %d", orig_cnt, cnt, i, marked);
2816 free_commit_list(h);
2818 return marked;
2821 static int mark_treesame_root_parents(struct commit *commit)
2823 struct commit_list *p;
2824 int marked = 0;
2826 for (p = commit->parents; p; p = p->next) {
2827 struct commit *parent = p->item;
2828 if (!parent->parents && (parent->object.flags & TREESAME)) {
2829 parent->object.flags |= TMP_MARK;
2830 marked++;
2834 return marked;
2838 * Awkward naming - this means one parent we are TREESAME to.
2839 * cf mark_treesame_root_parents: root parents that are TREESAME (to an
2840 * empty tree). Better name suggestions?
2842 static int leave_one_treesame_to_parent(struct rev_info *revs, struct commit *commit)
2844 struct treesame_state *ts = lookup_decoration(&revs->treesame, &commit->object);
2845 struct commit *unmarked = NULL, *marked = NULL;
2846 struct commit_list *p;
2847 unsigned n;
2849 for (p = commit->parents, n = 0; p; p = p->next, n++) {
2850 if (ts->treesame[n]) {
2851 if (p->item->object.flags & TMP_MARK) {
2852 if (!marked)
2853 marked = p->item;
2854 } else {
2855 if (!unmarked) {
2856 unmarked = p->item;
2857 break;
2864 * If we are TREESAME to a marked-for-deletion parent, but not to any
2865 * unmarked parents, unmark the first TREESAME parent. This is the
2866 * parent that the default simplify_history==1 scan would have followed,
2867 * and it doesn't make sense to omit that path when asking for a
2868 * simplified full history. Retaining it improves the chances of
2869 * understanding odd missed merges that took an old version of a file.
2871 * Example:
2873 * I--------*X A modified the file, but mainline merge X used
2874 * \ / "-s ours", so took the version from I. X is
2875 * `-*A--' TREESAME to I and !TREESAME to A.
2877 * Default log from X would produce "I". Without this check,
2878 * --full-history --simplify-merges would produce "I-A-X", showing
2879 * the merge commit X and that it changed A, but not making clear that
2880 * it had just taken the I version. With this check, the topology above
2881 * is retained.
2883 * Note that it is possible that the simplification chooses a different
2884 * TREESAME parent from the default, in which case this test doesn't
2885 * activate, and we _do_ drop the default parent. Example:
2887 * I------X A modified the file, but it was reverted in B,
2888 * \ / meaning mainline merge X is TREESAME to both
2889 * *A-*B parents.
2891 * Default log would produce "I" by following the first parent;
2892 * --full-history --simplify-merges will produce "I-A-B". But this is a
2893 * reasonable result - it presents a logical full history leading from
2894 * I to X, and X is not an important merge.
2896 if (!unmarked && marked) {
2897 marked->object.flags &= ~TMP_MARK;
2898 return 1;
2901 return 0;
2904 static int remove_marked_parents(struct rev_info *revs, struct commit *commit)
2906 struct commit_list **pp, *p;
2907 int nth_parent, removed = 0;
2909 pp = &commit->parents;
2910 nth_parent = 0;
2911 while ((p = *pp) != NULL) {
2912 struct commit *parent = p->item;
2913 if (parent->object.flags & TMP_MARK) {
2914 parent->object.flags &= ~TMP_MARK;
2915 *pp = p->next;
2916 free(p);
2917 removed++;
2918 compact_treesame(revs, commit, nth_parent);
2919 continue;
2921 pp = &p->next;
2922 nth_parent++;
2925 /* Removing parents can only increase TREESAMEness */
2926 if (removed && !(commit->object.flags & TREESAME))
2927 update_treesame(revs, commit);
2929 return nth_parent;
2932 static struct commit_list **simplify_one(struct rev_info *revs, struct commit *commit, struct commit_list **tail)
2934 struct commit_list *p;
2935 struct commit *parent;
2936 struct merge_simplify_state *st, *pst;
2937 int cnt;
2939 st = locate_simplify_state(revs, commit);
2942 * Have we handled this one?
2944 if (st->simplified)
2945 return tail;
2948 * An UNINTERESTING commit simplifies to itself, so does a
2949 * root commit. We do not rewrite parents of such commit
2950 * anyway.
2952 if ((commit->object.flags & UNINTERESTING) || !commit->parents) {
2953 st->simplified = commit;
2954 return tail;
2958 * Do we know what commit all of our parents that matter
2959 * should be rewritten to? Otherwise we are not ready to
2960 * rewrite this one yet.
2962 for (cnt = 0, p = commit->parents; p; p = p->next) {
2963 pst = locate_simplify_state(revs, p->item);
2964 if (!pst->simplified) {
2965 tail = &commit_list_insert(p->item, tail)->next;
2966 cnt++;
2968 if (revs->first_parent_only)
2969 break;
2971 if (cnt) {
2972 tail = &commit_list_insert(commit, tail)->next;
2973 return tail;
2977 * Rewrite our list of parents. Note that this cannot
2978 * affect our TREESAME flags in any way - a commit is
2979 * always TREESAME to its simplification.
2981 for (p = commit->parents; p; p = p->next) {
2982 pst = locate_simplify_state(revs, p->item);
2983 p->item = pst->simplified;
2984 if (revs->first_parent_only)
2985 break;
2988 if (revs->first_parent_only)
2989 cnt = 1;
2990 else
2991 cnt = remove_duplicate_parents(revs, commit);
2994 * It is possible that we are a merge and one side branch
2995 * does not have any commit that touches the given paths;
2996 * in such a case, the immediate parent from that branch
2997 * will be rewritten to be the merge base.
2999 * o----X X: the commit we are looking at;
3000 * / / o: a commit that touches the paths;
3001 * ---o----'
3003 * Further, a merge of an independent branch that doesn't
3004 * touch the path will reduce to a treesame root parent:
3006 * ----o----X X: the commit we are looking at;
3007 * / o: a commit that touches the paths;
3008 * r r: a root commit not touching the paths
3010 * Detect and simplify both cases.
3012 if (1 < cnt) {
3013 int marked = mark_redundant_parents(commit);
3014 marked += mark_treesame_root_parents(commit);
3015 if (marked)
3016 marked -= leave_one_treesame_to_parent(revs, commit);
3017 if (marked)
3018 cnt = remove_marked_parents(revs, commit);
3022 * A commit simplifies to itself if it is a root, if it is
3023 * UNINTERESTING, if it touches the given paths, or if it is a
3024 * merge and its parents don't simplify to one relevant commit
3025 * (the first two cases are already handled at the beginning of
3026 * this function).
3028 * Otherwise, it simplifies to what its sole relevant parent
3029 * simplifies to.
3031 if (!cnt ||
3032 (commit->object.flags & UNINTERESTING) ||
3033 !(commit->object.flags & TREESAME) ||
3034 (parent = one_relevant_parent(revs, commit->parents)) == NULL)
3035 st->simplified = commit;
3036 else {
3037 pst = locate_simplify_state(revs, parent);
3038 st->simplified = pst->simplified;
3040 return tail;
3043 static void simplify_merges(struct rev_info *revs)
3045 struct commit_list *list, *next;
3046 struct commit_list *yet_to_do, **tail;
3047 struct commit *commit;
3049 if (!revs->prune)
3050 return;
3052 /* feed the list reversed */
3053 yet_to_do = NULL;
3054 for (list = revs->commits; list; list = next) {
3055 commit = list->item;
3056 next = list->next;
3058 * Do not free(list) here yet; the original list
3059 * is used later in this function.
3061 commit_list_insert(commit, &yet_to_do);
3063 while (yet_to_do) {
3064 list = yet_to_do;
3065 yet_to_do = NULL;
3066 tail = &yet_to_do;
3067 while (list) {
3068 commit = pop_commit(&list);
3069 tail = simplify_one(revs, commit, tail);
3073 /* clean up the result, removing the simplified ones */
3074 list = revs->commits;
3075 revs->commits = NULL;
3076 tail = &revs->commits;
3077 while (list) {
3078 struct merge_simplify_state *st;
3080 commit = pop_commit(&list);
3081 st = locate_simplify_state(revs, commit);
3082 if (st->simplified == commit)
3083 tail = &commit_list_insert(commit, tail)->next;
3087 static void set_children(struct rev_info *revs)
3089 struct commit_list *l;
3090 for (l = revs->commits; l; l = l->next) {
3091 struct commit *commit = l->item;
3092 struct commit_list *p;
3094 for (p = commit->parents; p; p = p->next)
3095 add_child(revs, p->item, commit);
3099 void reset_revision_walk(void)
3101 clear_object_flags(SEEN | ADDED | SHOWN);
3104 static int mark_uninteresting(const struct object_id *oid,
3105 struct packed_git *pack,
3106 uint32_t pos,
3107 void *cb)
3109 struct rev_info *revs = cb;
3110 struct object *o = parse_object(revs->repo, oid);
3111 o->flags |= UNINTERESTING | SEEN;
3112 return 0;
3115 define_commit_slab(indegree_slab, int);
3116 define_commit_slab(author_date_slab, timestamp_t);
3118 struct topo_walk_info {
3119 uint32_t min_generation;
3120 struct prio_queue explore_queue;
3121 struct prio_queue indegree_queue;
3122 struct prio_queue topo_queue;
3123 struct indegree_slab indegree;
3124 struct author_date_slab author_date;
3127 static inline void test_flag_and_insert(struct prio_queue *q, struct commit *c, int flag)
3129 if (c->object.flags & flag)
3130 return;
3132 c->object.flags |= flag;
3133 prio_queue_put(q, c);
3136 static void explore_walk_step(struct rev_info *revs)
3138 struct topo_walk_info *info = revs->topo_walk_info;
3139 struct commit_list *p;
3140 struct commit *c = prio_queue_get(&info->explore_queue);
3142 if (!c)
3143 return;
3145 if (parse_commit_gently(c, 1) < 0)
3146 return;
3148 if (revs->sort_order == REV_SORT_BY_AUTHOR_DATE)
3149 record_author_date(&info->author_date, c);
3151 if (revs->max_age != -1 && (c->date < revs->max_age))
3152 c->object.flags |= UNINTERESTING;
3154 if (process_parents(revs, c, NULL, NULL) < 0)
3155 return;
3157 if (c->object.flags & UNINTERESTING)
3158 mark_parents_uninteresting(c);
3160 for (p = c->parents; p; p = p->next)
3161 test_flag_and_insert(&info->explore_queue, p->item, TOPO_WALK_EXPLORED);
3164 static void explore_to_depth(struct rev_info *revs,
3165 uint32_t gen_cutoff)
3167 struct topo_walk_info *info = revs->topo_walk_info;
3168 struct commit *c;
3169 while ((c = prio_queue_peek(&info->explore_queue)) &&
3170 c->generation >= gen_cutoff)
3171 explore_walk_step(revs);
3174 static void indegree_walk_step(struct rev_info *revs)
3176 struct commit_list *p;
3177 struct topo_walk_info *info = revs->topo_walk_info;
3178 struct commit *c = prio_queue_get(&info->indegree_queue);
3180 if (!c)
3181 return;
3183 if (parse_commit_gently(c, 1) < 0)
3184 return;
3186 explore_to_depth(revs, c->generation);
3188 for (p = c->parents; p; p = p->next) {
3189 struct commit *parent = p->item;
3190 int *pi = indegree_slab_at(&info->indegree, parent);
3192 if (*pi)
3193 (*pi)++;
3194 else
3195 *pi = 2;
3197 test_flag_and_insert(&info->indegree_queue, parent, TOPO_WALK_INDEGREE);
3199 if (revs->first_parent_only)
3200 return;
3204 static void compute_indegrees_to_depth(struct rev_info *revs,
3205 uint32_t gen_cutoff)
3207 struct topo_walk_info *info = revs->topo_walk_info;
3208 struct commit *c;
3209 while ((c = prio_queue_peek(&info->indegree_queue)) &&
3210 c->generation >= gen_cutoff)
3211 indegree_walk_step(revs);
3214 static void init_topo_walk(struct rev_info *revs)
3216 struct topo_walk_info *info;
3217 struct commit_list *list;
3218 revs->topo_walk_info = xmalloc(sizeof(struct topo_walk_info));
3219 info = revs->topo_walk_info;
3220 memset(info, 0, sizeof(struct topo_walk_info));
3222 init_indegree_slab(&info->indegree);
3223 memset(&info->explore_queue, 0, sizeof(info->explore_queue));
3224 memset(&info->indegree_queue, 0, sizeof(info->indegree_queue));
3225 memset(&info->topo_queue, 0, sizeof(info->topo_queue));
3227 switch (revs->sort_order) {
3228 default: /* REV_SORT_IN_GRAPH_ORDER */
3229 info->topo_queue.compare = NULL;
3230 break;
3231 case REV_SORT_BY_COMMIT_DATE:
3232 info->topo_queue.compare = compare_commits_by_commit_date;
3233 break;
3234 case REV_SORT_BY_AUTHOR_DATE:
3235 init_author_date_slab(&info->author_date);
3236 info->topo_queue.compare = compare_commits_by_author_date;
3237 info->topo_queue.cb_data = &info->author_date;
3238 break;
3241 info->explore_queue.compare = compare_commits_by_gen_then_commit_date;
3242 info->indegree_queue.compare = compare_commits_by_gen_then_commit_date;
3244 info->min_generation = GENERATION_NUMBER_INFINITY;
3245 for (list = revs->commits; list; list = list->next) {
3246 struct commit *c = list->item;
3248 if (parse_commit_gently(c, 1))
3249 continue;
3251 test_flag_and_insert(&info->explore_queue, c, TOPO_WALK_EXPLORED);
3252 test_flag_and_insert(&info->indegree_queue, c, TOPO_WALK_INDEGREE);
3254 if (c->generation < info->min_generation)
3255 info->min_generation = c->generation;
3257 *(indegree_slab_at(&info->indegree, c)) = 1;
3259 if (revs->sort_order == REV_SORT_BY_AUTHOR_DATE)
3260 record_author_date(&info->author_date, c);
3262 compute_indegrees_to_depth(revs, info->min_generation);
3264 for (list = revs->commits; list; list = list->next) {
3265 struct commit *c = list->item;
3267 if (*(indegree_slab_at(&info->indegree, c)) == 1)
3268 prio_queue_put(&info->topo_queue, c);
3272 * This is unfortunate; the initial tips need to be shown
3273 * in the order given from the revision traversal machinery.
3275 if (revs->sort_order == REV_SORT_IN_GRAPH_ORDER)
3276 prio_queue_reverse(&info->topo_queue);
3279 static struct commit *next_topo_commit(struct rev_info *revs)
3281 struct commit *c;
3282 struct topo_walk_info *info = revs->topo_walk_info;
3284 /* pop next off of topo_queue */
3285 c = prio_queue_get(&info->topo_queue);
3287 if (c)
3288 *(indegree_slab_at(&info->indegree, c)) = 0;
3290 return c;
3293 static void expand_topo_walk(struct rev_info *revs, struct commit *commit)
3295 struct commit_list *p;
3296 struct topo_walk_info *info = revs->topo_walk_info;
3297 if (process_parents(revs, commit, NULL, NULL) < 0) {
3298 if (!revs->ignore_missing_links)
3299 die("Failed to traverse parents of commit %s",
3300 oid_to_hex(&commit->object.oid));
3303 for (p = commit->parents; p; p = p->next) {
3304 struct commit *parent = p->item;
3305 int *pi;
3307 if (parent->object.flags & UNINTERESTING)
3308 continue;
3310 if (parse_commit_gently(parent, 1) < 0)
3311 continue;
3313 if (parent->generation < info->min_generation) {
3314 info->min_generation = parent->generation;
3315 compute_indegrees_to_depth(revs, info->min_generation);
3318 pi = indegree_slab_at(&info->indegree, parent);
3320 (*pi)--;
3321 if (*pi == 1)
3322 prio_queue_put(&info->topo_queue, parent);
3324 if (revs->first_parent_only)
3325 return;
3329 int prepare_revision_walk(struct rev_info *revs)
3331 int i;
3332 struct object_array old_pending;
3333 struct commit_list **next = &revs->commits;
3335 memcpy(&old_pending, &revs->pending, sizeof(old_pending));
3336 revs->pending.nr = 0;
3337 revs->pending.alloc = 0;
3338 revs->pending.objects = NULL;
3339 for (i = 0; i < old_pending.nr; i++) {
3340 struct object_array_entry *e = old_pending.objects + i;
3341 struct commit *commit = handle_commit(revs, e);
3342 if (commit) {
3343 if (!(commit->object.flags & SEEN)) {
3344 commit->object.flags |= SEEN;
3345 next = commit_list_append(commit, next);
3349 object_array_clear(&old_pending);
3351 /* Signal whether we need per-parent treesame decoration */
3352 if (revs->simplify_merges ||
3353 (revs->limited && limiting_can_increase_treesame(revs)))
3354 revs->treesame.name = "treesame";
3356 if (revs->exclude_promisor_objects) {
3357 for_each_packed_object(mark_uninteresting, revs,
3358 FOR_EACH_OBJECT_PROMISOR_ONLY);
3361 if (revs->no_walk != REVISION_WALK_NO_WALK_UNSORTED)
3362 commit_list_sort_by_date(&revs->commits);
3363 if (revs->no_walk)
3364 return 0;
3365 if (revs->limited) {
3366 if (limit_list(revs) < 0)
3367 return -1;
3368 if (revs->topo_order)
3369 sort_in_topological_order(&revs->commits, revs->sort_order);
3370 } else if (revs->topo_order)
3371 init_topo_walk(revs);
3372 if (revs->line_level_traverse)
3373 line_log_filter(revs);
3374 if (revs->simplify_merges)
3375 simplify_merges(revs);
3376 if (revs->children.name)
3377 set_children(revs);
3378 return 0;
3381 static enum rewrite_result rewrite_one_1(struct rev_info *revs,
3382 struct commit **pp,
3383 struct prio_queue *queue)
3385 for (;;) {
3386 struct commit *p = *pp;
3387 if (!revs->limited)
3388 if (process_parents(revs, p, NULL, queue) < 0)
3389 return rewrite_one_error;
3390 if (p->object.flags & UNINTERESTING)
3391 return rewrite_one_ok;
3392 if (!(p->object.flags & TREESAME))
3393 return rewrite_one_ok;
3394 if (!p->parents)
3395 return rewrite_one_noparents;
3396 if ((p = one_relevant_parent(revs, p->parents)) == NULL)
3397 return rewrite_one_ok;
3398 *pp = p;
3402 static void merge_queue_into_list(struct prio_queue *q, struct commit_list **list)
3404 while (q->nr) {
3405 struct commit *item = prio_queue_peek(q);
3406 struct commit_list *p = *list;
3408 if (p && p->item->date >= item->date)
3409 list = &p->next;
3410 else {
3411 p = commit_list_insert(item, list);
3412 list = &p->next; /* skip newly added item */
3413 prio_queue_get(q); /* pop item */
3418 static enum rewrite_result rewrite_one(struct rev_info *revs, struct commit **pp)
3420 struct prio_queue queue = { compare_commits_by_commit_date };
3421 enum rewrite_result ret = rewrite_one_1(revs, pp, &queue);
3422 merge_queue_into_list(&queue, &revs->commits);
3423 clear_prio_queue(&queue);
3424 return ret;
3427 int rewrite_parents(struct rev_info *revs, struct commit *commit,
3428 rewrite_parent_fn_t rewrite_parent)
3430 struct commit_list **pp = &commit->parents;
3431 while (*pp) {
3432 struct commit_list *parent = *pp;
3433 switch (rewrite_parent(revs, &parent->item)) {
3434 case rewrite_one_ok:
3435 break;
3436 case rewrite_one_noparents:
3437 *pp = parent->next;
3438 continue;
3439 case rewrite_one_error:
3440 return -1;
3442 pp = &parent->next;
3444 remove_duplicate_parents(revs, commit);
3445 return 0;
3448 static int commit_rewrite_person(struct strbuf *buf, const char *what, struct string_list *mailmap)
3450 char *person, *endp;
3451 size_t len, namelen, maillen;
3452 const char *name;
3453 const char *mail;
3454 struct ident_split ident;
3456 person = strstr(buf->buf, what);
3457 if (!person)
3458 return 0;
3460 person += strlen(what);
3461 endp = strchr(person, '\n');
3462 if (!endp)
3463 return 0;
3465 len = endp - person;
3467 if (split_ident_line(&ident, person, len))
3468 return 0;
3470 mail = ident.mail_begin;
3471 maillen = ident.mail_end - ident.mail_begin;
3472 name = ident.name_begin;
3473 namelen = ident.name_end - ident.name_begin;
3475 if (map_user(mailmap, &mail, &maillen, &name, &namelen)) {
3476 struct strbuf namemail = STRBUF_INIT;
3478 strbuf_addf(&namemail, "%.*s <%.*s>",
3479 (int)namelen, name, (int)maillen, mail);
3481 strbuf_splice(buf, ident.name_begin - buf->buf,
3482 ident.mail_end - ident.name_begin + 1,
3483 namemail.buf, namemail.len);
3485 strbuf_release(&namemail);
3487 return 1;
3490 return 0;
3493 static int commit_match(struct commit *commit, struct rev_info *opt)
3495 int retval;
3496 const char *encoding;
3497 const char *message;
3498 struct strbuf buf = STRBUF_INIT;
3500 if (!opt->grep_filter.pattern_list && !opt->grep_filter.header_list)
3501 return 1;
3503 /* Prepend "fake" headers as needed */
3504 if (opt->grep_filter.use_reflog_filter) {
3505 strbuf_addstr(&buf, "reflog ");
3506 get_reflog_message(&buf, opt->reflog_info);
3507 strbuf_addch(&buf, '\n');
3511 * We grep in the user's output encoding, under the assumption that it
3512 * is the encoding they are most likely to write their grep pattern
3513 * for. In addition, it means we will match the "notes" encoding below,
3514 * so we will not end up with a buffer that has two different encodings
3515 * in it.
3517 encoding = get_log_output_encoding();
3518 message = logmsg_reencode(commit, NULL, encoding);
3520 /* Copy the commit to temporary if we are using "fake" headers */
3521 if (buf.len)
3522 strbuf_addstr(&buf, message);
3524 if (opt->grep_filter.header_list && opt->mailmap) {
3525 if (!buf.len)
3526 strbuf_addstr(&buf, message);
3528 commit_rewrite_person(&buf, "\nauthor ", opt->mailmap);
3529 commit_rewrite_person(&buf, "\ncommitter ", opt->mailmap);
3532 /* Append "fake" message parts as needed */
3533 if (opt->show_notes) {
3534 if (!buf.len)
3535 strbuf_addstr(&buf, message);
3536 format_display_notes(&commit->object.oid, &buf, encoding, 1);
3540 * Find either in the original commit message, or in the temporary.
3541 * Note that we cast away the constness of "message" here. It is
3542 * const because it may come from the cached commit buffer. That's OK,
3543 * because we know that it is modifiable heap memory, and that while
3544 * grep_buffer may modify it for speed, it will restore any
3545 * changes before returning.
3547 if (buf.len)
3548 retval = grep_buffer(&opt->grep_filter, buf.buf, buf.len);
3549 else
3550 retval = grep_buffer(&opt->grep_filter,
3551 (char *)message, strlen(message));
3552 strbuf_release(&buf);
3553 unuse_commit_buffer(commit, message);
3554 return opt->invert_grep ? !retval : retval;
3557 static inline int want_ancestry(const struct rev_info *revs)
3559 return (revs->rewrite_parents || revs->children.name);
3563 * Return a timestamp to be used for --since/--until comparisons for this
3564 * commit, based on the revision options.
3566 static timestamp_t comparison_date(const struct rev_info *revs,
3567 struct commit *commit)
3569 return revs->reflog_info ?
3570 get_reflog_timestamp(revs->reflog_info) :
3571 commit->date;
3574 enum commit_action get_commit_action(struct rev_info *revs, struct commit *commit)
3576 if (commit->object.flags & SHOWN)
3577 return commit_ignore;
3578 if (revs->unpacked && has_object_pack(&commit->object.oid))
3579 return commit_ignore;
3580 if (commit->object.flags & UNINTERESTING)
3581 return commit_ignore;
3582 if (revs->min_age != -1 &&
3583 comparison_date(revs, commit) > revs->min_age)
3584 return commit_ignore;
3585 if (revs->min_parents || (revs->max_parents >= 0)) {
3586 int n = commit_list_count(commit->parents);
3587 if ((n < revs->min_parents) ||
3588 ((revs->max_parents >= 0) && (n > revs->max_parents)))
3589 return commit_ignore;
3591 if (!commit_match(commit, revs))
3592 return commit_ignore;
3593 if (revs->prune && revs->dense) {
3594 /* Commit without changes? */
3595 if (commit->object.flags & TREESAME) {
3596 int n;
3597 struct commit_list *p;
3598 /* drop merges unless we want parenthood */
3599 if (!want_ancestry(revs))
3600 return commit_ignore;
3602 * If we want ancestry, then need to keep any merges
3603 * between relevant commits to tie together topology.
3604 * For consistency with TREESAME and simplification
3605 * use "relevant" here rather than just INTERESTING,
3606 * to treat bottom commit(s) as part of the topology.
3608 for (n = 0, p = commit->parents; p; p = p->next)
3609 if (relevant_commit(p->item))
3610 if (++n >= 2)
3611 return commit_show;
3612 return commit_ignore;
3615 return commit_show;
3618 define_commit_slab(saved_parents, struct commit_list *);
3620 #define EMPTY_PARENT_LIST ((struct commit_list *)-1)
3623 * You may only call save_parents() once per commit (this is checked
3624 * for non-root commits).
3626 static void save_parents(struct rev_info *revs, struct commit *commit)
3628 struct commit_list **pp;
3630 if (!revs->saved_parents_slab) {
3631 revs->saved_parents_slab = xmalloc(sizeof(struct saved_parents));
3632 init_saved_parents(revs->saved_parents_slab);
3635 pp = saved_parents_at(revs->saved_parents_slab, commit);
3638 * When walking with reflogs, we may visit the same commit
3639 * several times: once for each appearance in the reflog.
3641 * In this case, save_parents() will be called multiple times.
3642 * We want to keep only the first set of parents. We need to
3643 * store a sentinel value for an empty (i.e., NULL) parent
3644 * list to distinguish it from a not-yet-saved list, however.
3646 if (*pp)
3647 return;
3648 if (commit->parents)
3649 *pp = copy_commit_list(commit->parents);
3650 else
3651 *pp = EMPTY_PARENT_LIST;
3654 static void free_saved_parents(struct rev_info *revs)
3656 if (revs->saved_parents_slab)
3657 clear_saved_parents(revs->saved_parents_slab);
3660 struct commit_list *get_saved_parents(struct rev_info *revs, const struct commit *commit)
3662 struct commit_list *parents;
3664 if (!revs->saved_parents_slab)
3665 return commit->parents;
3667 parents = *saved_parents_at(revs->saved_parents_slab, commit);
3668 if (parents == EMPTY_PARENT_LIST)
3669 return NULL;
3670 return parents;
3673 enum commit_action simplify_commit(struct rev_info *revs, struct commit *commit)
3675 enum commit_action action = get_commit_action(revs, commit);
3677 if (action == commit_show &&
3678 revs->prune && revs->dense && want_ancestry(revs)) {
3680 * --full-diff on simplified parents is no good: it
3681 * will show spurious changes from the commits that
3682 * were elided. So we save the parents on the side
3683 * when --full-diff is in effect.
3685 if (revs->full_diff)
3686 save_parents(revs, commit);
3687 if (rewrite_parents(revs, commit, rewrite_one) < 0)
3688 return commit_error;
3690 return action;
3693 static void track_linear(struct rev_info *revs, struct commit *commit)
3695 if (revs->track_first_time) {
3696 revs->linear = 1;
3697 revs->track_first_time = 0;
3698 } else {
3699 struct commit_list *p;
3700 for (p = revs->previous_parents; p; p = p->next)
3701 if (p->item == NULL || /* first commit */
3702 oideq(&p->item->object.oid, &commit->object.oid))
3703 break;
3704 revs->linear = p != NULL;
3706 if (revs->reverse) {
3707 if (revs->linear)
3708 commit->object.flags |= TRACK_LINEAR;
3710 free_commit_list(revs->previous_parents);
3711 revs->previous_parents = copy_commit_list(commit->parents);
3714 static struct commit *get_revision_1(struct rev_info *revs)
3716 while (1) {
3717 struct commit *commit;
3719 if (revs->reflog_info)
3720 commit = next_reflog_entry(revs->reflog_info);
3721 else if (revs->topo_walk_info)
3722 commit = next_topo_commit(revs);
3723 else
3724 commit = pop_commit(&revs->commits);
3726 if (!commit)
3727 return NULL;
3729 if (revs->reflog_info)
3730 commit->object.flags &= ~(ADDED | SEEN | SHOWN);
3733 * If we haven't done the list limiting, we need to look at
3734 * the parents here. We also need to do the date-based limiting
3735 * that we'd otherwise have done in limit_list().
3737 if (!revs->limited) {
3738 if (revs->max_age != -1 &&
3739 comparison_date(revs, commit) < revs->max_age)
3740 continue;
3742 if (revs->reflog_info)
3743 try_to_simplify_commit(revs, commit);
3744 else if (revs->topo_walk_info)
3745 expand_topo_walk(revs, commit);
3746 else if (process_parents(revs, commit, &revs->commits, NULL) < 0) {
3747 if (!revs->ignore_missing_links)
3748 die("Failed to traverse parents of commit %s",
3749 oid_to_hex(&commit->object.oid));
3753 switch (simplify_commit(revs, commit)) {
3754 case commit_ignore:
3755 continue;
3756 case commit_error:
3757 die("Failed to simplify parents of commit %s",
3758 oid_to_hex(&commit->object.oid));
3759 default:
3760 if (revs->track_linear)
3761 track_linear(revs, commit);
3762 return commit;
3768 * Return true for entries that have not yet been shown. (This is an
3769 * object_array_each_func_t.)
3771 static int entry_unshown(struct object_array_entry *entry, void *cb_data_unused)
3773 return !(entry->item->flags & SHOWN);
3777 * If array is on the verge of a realloc, garbage-collect any entries
3778 * that have already been shown to try to free up some space.
3780 static void gc_boundary(struct object_array *array)
3782 if (array->nr == array->alloc)
3783 object_array_filter(array, entry_unshown, NULL);
3786 static void create_boundary_commit_list(struct rev_info *revs)
3788 unsigned i;
3789 struct commit *c;
3790 struct object_array *array = &revs->boundary_commits;
3791 struct object_array_entry *objects = array->objects;
3794 * If revs->commits is non-NULL at this point, an error occurred in
3795 * get_revision_1(). Ignore the error and continue printing the
3796 * boundary commits anyway. (This is what the code has always
3797 * done.)
3799 if (revs->commits) {
3800 free_commit_list(revs->commits);
3801 revs->commits = NULL;
3805 * Put all of the actual boundary commits from revs->boundary_commits
3806 * into revs->commits
3808 for (i = 0; i < array->nr; i++) {
3809 c = (struct commit *)(objects[i].item);
3810 if (!c)
3811 continue;
3812 if (!(c->object.flags & CHILD_SHOWN))
3813 continue;
3814 if (c->object.flags & (SHOWN | BOUNDARY))
3815 continue;
3816 c->object.flags |= BOUNDARY;
3817 commit_list_insert(c, &revs->commits);
3821 * If revs->topo_order is set, sort the boundary commits
3822 * in topological order
3824 sort_in_topological_order(&revs->commits, revs->sort_order);
3827 static struct commit *get_revision_internal(struct rev_info *revs)
3829 struct commit *c = NULL;
3830 struct commit_list *l;
3832 if (revs->boundary == 2) {
3834 * All of the normal commits have already been returned,
3835 * and we are now returning boundary commits.
3836 * create_boundary_commit_list() has populated
3837 * revs->commits with the remaining commits to return.
3839 c = pop_commit(&revs->commits);
3840 if (c)
3841 c->object.flags |= SHOWN;
3842 return c;
3846 * If our max_count counter has reached zero, then we are done. We
3847 * don't simply return NULL because we still might need to show
3848 * boundary commits. But we want to avoid calling get_revision_1, which
3849 * might do a considerable amount of work finding the next commit only
3850 * for us to throw it away.
3852 * If it is non-zero, then either we don't have a max_count at all
3853 * (-1), or it is still counting, in which case we decrement.
3855 if (revs->max_count) {
3856 c = get_revision_1(revs);
3857 if (c) {
3858 while (revs->skip_count > 0) {
3859 revs->skip_count--;
3860 c = get_revision_1(revs);
3861 if (!c)
3862 break;
3866 if (revs->max_count > 0)
3867 revs->max_count--;
3870 if (c)
3871 c->object.flags |= SHOWN;
3873 if (!revs->boundary)
3874 return c;
3876 if (!c) {
3878 * get_revision_1() runs out the commits, and
3879 * we are done computing the boundaries.
3880 * switch to boundary commits output mode.
3882 revs->boundary = 2;
3885 * Update revs->commits to contain the list of
3886 * boundary commits.
3888 create_boundary_commit_list(revs);
3890 return get_revision_internal(revs);
3894 * boundary commits are the commits that are parents of the
3895 * ones we got from get_revision_1() but they themselves are
3896 * not returned from get_revision_1(). Before returning
3897 * 'c', we need to mark its parents that they could be boundaries.
3900 for (l = c->parents; l; l = l->next) {
3901 struct object *p;
3902 p = &(l->item->object);
3903 if (p->flags & (CHILD_SHOWN | SHOWN))
3904 continue;
3905 p->flags |= CHILD_SHOWN;
3906 gc_boundary(&revs->boundary_commits);
3907 add_object_array(p, NULL, &revs->boundary_commits);
3910 return c;
3913 struct commit *get_revision(struct rev_info *revs)
3915 struct commit *c;
3916 struct commit_list *reversed;
3918 if (revs->reverse) {
3919 reversed = NULL;
3920 while ((c = get_revision_internal(revs)))
3921 commit_list_insert(c, &reversed);
3922 revs->commits = reversed;
3923 revs->reverse = 0;
3924 revs->reverse_output_stage = 1;
3927 if (revs->reverse_output_stage) {
3928 c = pop_commit(&revs->commits);
3929 if (revs->track_linear)
3930 revs->linear = !!(c && c->object.flags & TRACK_LINEAR);
3931 return c;
3934 c = get_revision_internal(revs);
3935 if (c && revs->graph)
3936 graph_update(revs->graph, c);
3937 if (!c) {
3938 free_saved_parents(revs);
3939 if (revs->previous_parents) {
3940 free_commit_list(revs->previous_parents);
3941 revs->previous_parents = NULL;
3944 return c;
3947 char *get_revision_mark(const struct rev_info *revs, const struct commit *commit)
3949 if (commit->object.flags & BOUNDARY)
3950 return "-";
3951 else if (commit->object.flags & UNINTERESTING)
3952 return "^";
3953 else if (commit->object.flags & PATCHSAME)
3954 return "=";
3955 else if (!revs || revs->left_right) {
3956 if (commit->object.flags & SYMMETRIC_LEFT)
3957 return "<";
3958 else
3959 return ">";
3960 } else if (revs->graph)
3961 return "*";
3962 else if (revs->cherry_mark)
3963 return "+";
3964 return "";
3967 void put_revision_mark(const struct rev_info *revs, const struct commit *commit)
3969 char *mark = get_revision_mark(revs, commit);
3970 if (!strlen(mark))
3971 return;
3972 fputs(mark, stdout);
3973 putchar(' ');