userdiff: two simplifications of patterns for rust
[git/raj.git] / revision.c
blobd4aaf0ef257943029923f741b4ae383bb00d7f5f
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"
32 volatile show_early_output_fn_t show_early_output;
34 static const char *term_bad;
35 static const char *term_good;
37 implement_shared_commit_slab(revision_sources, char *);
39 void show_object_with_name(FILE *out, struct object *obj, const char *name)
41 const char *p;
43 fprintf(out, "%s ", oid_to_hex(&obj->oid));
44 for (p = name; *p && *p != '\n'; p++)
45 fputc(*p, out);
46 fputc('\n', out);
49 static void mark_blob_uninteresting(struct blob *blob)
51 if (!blob)
52 return;
53 if (blob->object.flags & UNINTERESTING)
54 return;
55 blob->object.flags |= UNINTERESTING;
58 static void mark_tree_contents_uninteresting(struct repository *r,
59 struct tree *tree)
61 struct tree_desc desc;
62 struct name_entry entry;
64 if (parse_tree_gently(tree, 1) < 0)
65 return;
67 init_tree_desc(&desc, tree->buffer, tree->size);
68 while (tree_entry(&desc, &entry)) {
69 switch (object_type(entry.mode)) {
70 case OBJ_TREE:
71 mark_tree_uninteresting(r, lookup_tree(r, &entry.oid));
72 break;
73 case OBJ_BLOB:
74 mark_blob_uninteresting(lookup_blob(r, &entry.oid));
75 break;
76 default:
77 /* Subproject commit - not in this repository */
78 break;
83 * We don't care about the tree any more
84 * after it has been marked uninteresting.
86 free_tree_buffer(tree);
89 void mark_tree_uninteresting(struct repository *r, struct tree *tree)
91 struct object *obj;
93 if (!tree)
94 return;
96 obj = &tree->object;
97 if (obj->flags & UNINTERESTING)
98 return;
99 obj->flags |= UNINTERESTING;
100 mark_tree_contents_uninteresting(r, tree);
103 struct path_and_oids_entry {
104 struct hashmap_entry ent;
105 char *path;
106 struct oidset trees;
109 static int path_and_oids_cmp(const void *hashmap_cmp_fn_data,
110 const struct path_and_oids_entry *e1,
111 const struct path_and_oids_entry *e2,
112 const void *keydata)
114 return strcmp(e1->path, e2->path);
117 static void paths_and_oids_init(struct hashmap *map)
119 hashmap_init(map, (hashmap_cmp_fn) path_and_oids_cmp, NULL, 0);
122 static void paths_and_oids_clear(struct hashmap *map)
124 struct hashmap_iter iter;
125 struct path_and_oids_entry *entry;
126 hashmap_iter_init(map, &iter);
128 while ((entry = (struct path_and_oids_entry *)hashmap_iter_next(&iter))) {
129 oidset_clear(&entry->trees);
130 free(entry->path);
133 hashmap_free(map, 1);
136 static void paths_and_oids_insert(struct hashmap *map,
137 const char *path,
138 const struct object_id *oid)
140 int hash = strhash(path);
141 struct path_and_oids_entry key;
142 struct path_and_oids_entry *entry;
144 hashmap_entry_init(&key, hash);
146 /* use a shallow copy for the lookup */
147 key.path = (char *)path;
148 oidset_init(&key.trees, 0);
150 if (!(entry = (struct path_and_oids_entry *)hashmap_get(map, &key, NULL))) {
151 entry = xcalloc(1, sizeof(struct path_and_oids_entry));
152 hashmap_entry_init(entry, hash);
153 entry->path = xstrdup(key.path);
154 oidset_init(&entry->trees, 16);
155 hashmap_put(map, entry);
158 oidset_insert(&entry->trees, oid);
161 static void add_children_by_path(struct repository *r,
162 struct tree *tree,
163 struct hashmap *map)
165 struct tree_desc desc;
166 struct name_entry entry;
168 if (!tree)
169 return;
171 if (parse_tree_gently(tree, 1) < 0)
172 return;
174 init_tree_desc(&desc, tree->buffer, tree->size);
175 while (tree_entry(&desc, &entry)) {
176 switch (object_type(entry.mode)) {
177 case OBJ_TREE:
178 paths_and_oids_insert(map, entry.path, &entry.oid);
180 if (tree->object.flags & UNINTERESTING) {
181 struct tree *child = lookup_tree(r, &entry.oid);
182 if (child)
183 child->object.flags |= UNINTERESTING;
185 break;
186 case OBJ_BLOB:
187 if (tree->object.flags & UNINTERESTING) {
188 struct blob *child = lookup_blob(r, &entry.oid);
189 if (child)
190 child->object.flags |= UNINTERESTING;
192 break;
193 default:
194 /* Subproject commit - not in this repository */
195 break;
199 free_tree_buffer(tree);
202 void mark_trees_uninteresting_sparse(struct repository *r,
203 struct oidset *trees)
205 unsigned has_interesting = 0, has_uninteresting = 0;
206 struct hashmap map;
207 struct hashmap_iter map_iter;
208 struct path_and_oids_entry *entry;
209 struct object_id *oid;
210 struct oidset_iter iter;
212 oidset_iter_init(trees, &iter);
213 while ((!has_interesting || !has_uninteresting) &&
214 (oid = oidset_iter_next(&iter))) {
215 struct tree *tree = lookup_tree(r, oid);
217 if (!tree)
218 continue;
220 if (tree->object.flags & UNINTERESTING)
221 has_uninteresting = 1;
222 else
223 has_interesting = 1;
226 /* Do not walk unless we have both types of trees. */
227 if (!has_uninteresting || !has_interesting)
228 return;
230 paths_and_oids_init(&map);
232 oidset_iter_init(trees, &iter);
233 while ((oid = oidset_iter_next(&iter))) {
234 struct tree *tree = lookup_tree(r, oid);
235 add_children_by_path(r, tree, &map);
238 hashmap_iter_init(&map, &map_iter);
239 while ((entry = hashmap_iter_next(&map_iter)))
240 mark_trees_uninteresting_sparse(r, &entry->trees);
242 paths_and_oids_clear(&map);
245 struct commit_stack {
246 struct commit **items;
247 size_t nr, alloc;
249 #define COMMIT_STACK_INIT { NULL, 0, 0 }
251 static void commit_stack_push(struct commit_stack *stack, struct commit *commit)
253 ALLOC_GROW(stack->items, stack->nr + 1, stack->alloc);
254 stack->items[stack->nr++] = commit;
257 static struct commit *commit_stack_pop(struct commit_stack *stack)
259 return stack->nr ? stack->items[--stack->nr] : NULL;
262 static void commit_stack_clear(struct commit_stack *stack)
264 FREE_AND_NULL(stack->items);
265 stack->nr = stack->alloc = 0;
268 static void mark_one_parent_uninteresting(struct commit *commit,
269 struct commit_stack *pending)
271 struct commit_list *l;
273 if (commit->object.flags & UNINTERESTING)
274 return;
275 commit->object.flags |= UNINTERESTING;
278 * Normally we haven't parsed the parent
279 * yet, so we won't have a parent of a parent
280 * here. However, it may turn out that we've
281 * reached this commit some other way (where it
282 * wasn't uninteresting), in which case we need
283 * to mark its parents recursively too..
285 for (l = commit->parents; l; l = l->next)
286 commit_stack_push(pending, l->item);
289 void mark_parents_uninteresting(struct commit *commit)
291 struct commit_stack pending = COMMIT_STACK_INIT;
292 struct commit_list *l;
294 for (l = commit->parents; l; l = l->next)
295 mark_one_parent_uninteresting(l->item, &pending);
297 while (pending.nr > 0)
298 mark_one_parent_uninteresting(commit_stack_pop(&pending),
299 &pending);
301 commit_stack_clear(&pending);
304 static void add_pending_object_with_path(struct rev_info *revs,
305 struct object *obj,
306 const char *name, unsigned mode,
307 const char *path)
309 if (!obj)
310 return;
311 if (revs->no_walk && (obj->flags & UNINTERESTING))
312 revs->no_walk = 0;
313 if (revs->reflog_info && obj->type == OBJ_COMMIT) {
314 struct strbuf buf = STRBUF_INIT;
315 int len = interpret_branch_name(name, 0, &buf, 0);
317 if (0 < len && name[len] && buf.len)
318 strbuf_addstr(&buf, name + len);
319 add_reflog_for_walk(revs->reflog_info,
320 (struct commit *)obj,
321 buf.buf[0] ? buf.buf: name);
322 strbuf_release(&buf);
323 return; /* do not add the commit itself */
325 add_object_array_with_path(obj, name, &revs->pending, mode, path);
328 static void add_pending_object_with_mode(struct rev_info *revs,
329 struct object *obj,
330 const char *name, unsigned mode)
332 add_pending_object_with_path(revs, obj, name, mode, NULL);
335 void add_pending_object(struct rev_info *revs,
336 struct object *obj, const char *name)
338 add_pending_object_with_mode(revs, obj, name, S_IFINVALID);
341 void add_head_to_pending(struct rev_info *revs)
343 struct object_id oid;
344 struct object *obj;
345 if (get_oid("HEAD", &oid))
346 return;
347 obj = parse_object(revs->repo, &oid);
348 if (!obj)
349 return;
350 add_pending_object(revs, obj, "HEAD");
353 static struct object *get_reference(struct rev_info *revs, const char *name,
354 const struct object_id *oid,
355 unsigned int flags)
357 struct object *object;
360 * If the repository has commit graphs, repo_parse_commit() avoids
361 * reading the object buffer, so use it whenever possible.
363 if (oid_object_info(revs->repo, oid, NULL) == OBJ_COMMIT) {
364 struct commit *c = lookup_commit(revs->repo, oid);
365 if (!repo_parse_commit(revs->repo, c))
366 object = (struct object *) c;
367 else
368 object = NULL;
369 } else {
370 object = parse_object(revs->repo, oid);
373 if (!object) {
374 if (revs->ignore_missing)
375 return object;
376 if (revs->exclude_promisor_objects && is_promisor_object(oid))
377 return NULL;
378 die("bad object %s", name);
380 object->flags |= flags;
381 return object;
384 void add_pending_oid(struct rev_info *revs, const char *name,
385 const struct object_id *oid, unsigned int flags)
387 struct object *object = get_reference(revs, name, oid, flags);
388 add_pending_object(revs, object, name);
391 static struct commit *handle_commit(struct rev_info *revs,
392 struct object_array_entry *entry)
394 struct object *object = entry->item;
395 const char *name = entry->name;
396 const char *path = entry->path;
397 unsigned int mode = entry->mode;
398 unsigned long flags = object->flags;
401 * Tag object? Look what it points to..
403 while (object->type == OBJ_TAG) {
404 struct tag *tag = (struct tag *) object;
405 if (revs->tag_objects && !(flags & UNINTERESTING))
406 add_pending_object(revs, object, tag->tag);
407 if (!tag->tagged)
408 die("bad tag");
409 object = parse_object(revs->repo, &tag->tagged->oid);
410 if (!object) {
411 if (revs->ignore_missing_links || (flags & UNINTERESTING))
412 return NULL;
413 if (revs->exclude_promisor_objects &&
414 is_promisor_object(&tag->tagged->oid))
415 return NULL;
416 die("bad object %s", oid_to_hex(&tag->tagged->oid));
418 object->flags |= flags;
420 * We'll handle the tagged object by looping or dropping
421 * through to the non-tag handlers below. Do not
422 * propagate path data from the tag's pending entry.
424 path = NULL;
425 mode = 0;
429 * Commit object? Just return it, we'll do all the complex
430 * reachability crud.
432 if (object->type == OBJ_COMMIT) {
433 struct commit *commit = (struct commit *)object;
435 if (parse_commit(commit) < 0)
436 die("unable to parse commit %s", name);
437 if (flags & UNINTERESTING) {
438 mark_parents_uninteresting(commit);
439 revs->limited = 1;
441 if (revs->sources) {
442 char **slot = revision_sources_at(revs->sources, commit);
444 if (!*slot)
445 *slot = xstrdup(name);
447 return commit;
451 * Tree object? Either mark it uninteresting, or add it
452 * to the list of objects to look at later..
454 if (object->type == OBJ_TREE) {
455 struct tree *tree = (struct tree *)object;
456 if (!revs->tree_objects)
457 return NULL;
458 if (flags & UNINTERESTING) {
459 mark_tree_contents_uninteresting(revs->repo, tree);
460 return NULL;
462 add_pending_object_with_path(revs, object, name, mode, path);
463 return NULL;
467 * Blob object? You know the drill by now..
469 if (object->type == OBJ_BLOB) {
470 if (!revs->blob_objects)
471 return NULL;
472 if (flags & UNINTERESTING)
473 return NULL;
474 add_pending_object_with_path(revs, object, name, mode, path);
475 return NULL;
477 die("%s is unknown object", name);
480 static int everybody_uninteresting(struct commit_list *orig,
481 struct commit **interesting_cache)
483 struct commit_list *list = orig;
485 if (*interesting_cache) {
486 struct commit *commit = *interesting_cache;
487 if (!(commit->object.flags & UNINTERESTING))
488 return 0;
491 while (list) {
492 struct commit *commit = list->item;
493 list = list->next;
494 if (commit->object.flags & UNINTERESTING)
495 continue;
497 *interesting_cache = commit;
498 return 0;
500 return 1;
504 * A definition of "relevant" commit that we can use to simplify limited graphs
505 * by eliminating side branches.
507 * A "relevant" commit is one that is !UNINTERESTING (ie we are including it
508 * in our list), or that is a specified BOTTOM commit. Then after computing
509 * a limited list, during processing we can generally ignore boundary merges
510 * coming from outside the graph, (ie from irrelevant parents), and treat
511 * those merges as if they were single-parent. TREESAME is defined to consider
512 * only relevant parents, if any. If we are TREESAME to our on-graph parents,
513 * we don't care if we were !TREESAME to non-graph parents.
515 * Treating bottom commits as relevant ensures that a limited graph's
516 * connection to the actual bottom commit is not viewed as a side branch, but
517 * treated as part of the graph. For example:
519 * ....Z...A---X---o---o---B
520 * . /
521 * W---Y
523 * When computing "A..B", the A-X connection is at least as important as
524 * Y-X, despite A being flagged UNINTERESTING.
526 * And when computing --ancestry-path "A..B", the A-X connection is more
527 * important than Y-X, despite both A and Y being flagged UNINTERESTING.
529 static inline int relevant_commit(struct commit *commit)
531 return (commit->object.flags & (UNINTERESTING | BOTTOM)) != UNINTERESTING;
535 * Return a single relevant commit from a parent list. If we are a TREESAME
536 * commit, and this selects one of our parents, then we can safely simplify to
537 * that parent.
539 static struct commit *one_relevant_parent(const struct rev_info *revs,
540 struct commit_list *orig)
542 struct commit_list *list = orig;
543 struct commit *relevant = NULL;
545 if (!orig)
546 return NULL;
549 * For 1-parent commits, or if first-parent-only, then return that
550 * first parent (even if not "relevant" by the above definition).
551 * TREESAME will have been set purely on that parent.
553 if (revs->first_parent_only || !orig->next)
554 return orig->item;
557 * For multi-parent commits, identify a sole relevant parent, if any.
558 * If we have only one relevant parent, then TREESAME will be set purely
559 * with regard to that parent, and we can simplify accordingly.
561 * If we have more than one relevant parent, or no relevant parents
562 * (and multiple irrelevant ones), then we can't select a parent here
563 * and return NULL.
565 while (list) {
566 struct commit *commit = list->item;
567 list = list->next;
568 if (relevant_commit(commit)) {
569 if (relevant)
570 return NULL;
571 relevant = commit;
574 return relevant;
578 * The goal is to get REV_TREE_NEW as the result only if the
579 * diff consists of all '+' (and no other changes), REV_TREE_OLD
580 * if the whole diff is removal of old data, and otherwise
581 * REV_TREE_DIFFERENT (of course if the trees are the same we
582 * want REV_TREE_SAME).
584 * The only time we care about the distinction is when
585 * remove_empty_trees is in effect, in which case we care only about
586 * whether the whole change is REV_TREE_NEW, or if there's another type
587 * of change. Which means we can stop the diff early in either of these
588 * cases:
590 * 1. We're not using remove_empty_trees at all.
592 * 2. We saw anything except REV_TREE_NEW.
594 static int tree_difference = REV_TREE_SAME;
596 static void file_add_remove(struct diff_options *options,
597 int addremove, unsigned mode,
598 const struct object_id *oid,
599 int oid_valid,
600 const char *fullpath, unsigned dirty_submodule)
602 int diff = addremove == '+' ? REV_TREE_NEW : REV_TREE_OLD;
603 struct rev_info *revs = options->change_fn_data;
605 tree_difference |= diff;
606 if (!revs->remove_empty_trees || tree_difference != REV_TREE_NEW)
607 options->flags.has_changes = 1;
610 static void file_change(struct diff_options *options,
611 unsigned old_mode, unsigned new_mode,
612 const struct object_id *old_oid,
613 const struct object_id *new_oid,
614 int old_oid_valid, int new_oid_valid,
615 const char *fullpath,
616 unsigned old_dirty_submodule, unsigned new_dirty_submodule)
618 tree_difference = REV_TREE_DIFFERENT;
619 options->flags.has_changes = 1;
622 static int rev_compare_tree(struct rev_info *revs,
623 struct commit *parent, struct commit *commit)
625 struct tree *t1 = get_commit_tree(parent);
626 struct tree *t2 = get_commit_tree(commit);
628 if (!t1)
629 return REV_TREE_NEW;
630 if (!t2)
631 return REV_TREE_OLD;
633 if (revs->simplify_by_decoration) {
635 * If we are simplifying by decoration, then the commit
636 * is worth showing if it has a tag pointing at it.
638 if (get_name_decoration(&commit->object))
639 return REV_TREE_DIFFERENT;
641 * A commit that is not pointed by a tag is uninteresting
642 * if we are not limited by path. This means that you will
643 * see the usual "commits that touch the paths" plus any
644 * tagged commit by specifying both --simplify-by-decoration
645 * and pathspec.
647 if (!revs->prune_data.nr)
648 return REV_TREE_SAME;
651 tree_difference = REV_TREE_SAME;
652 revs->pruning.flags.has_changes = 0;
653 if (diff_tree_oid(&t1->object.oid, &t2->object.oid, "",
654 &revs->pruning) < 0)
655 return REV_TREE_DIFFERENT;
656 return tree_difference;
659 static int rev_same_tree_as_empty(struct rev_info *revs, struct commit *commit)
661 int retval;
662 struct tree *t1 = get_commit_tree(commit);
664 if (!t1)
665 return 0;
667 tree_difference = REV_TREE_SAME;
668 revs->pruning.flags.has_changes = 0;
669 retval = diff_tree_oid(NULL, &t1->object.oid, "", &revs->pruning);
671 return retval >= 0 && (tree_difference == REV_TREE_SAME);
674 struct treesame_state {
675 unsigned int nparents;
676 unsigned char treesame[FLEX_ARRAY];
679 static struct treesame_state *initialise_treesame(struct rev_info *revs, struct commit *commit)
681 unsigned n = commit_list_count(commit->parents);
682 struct treesame_state *st = xcalloc(1, st_add(sizeof(*st), n));
683 st->nparents = n;
684 add_decoration(&revs->treesame, &commit->object, st);
685 return st;
689 * Must be called immediately after removing the nth_parent from a commit's
690 * parent list, if we are maintaining the per-parent treesame[] decoration.
691 * This does not recalculate the master TREESAME flag - update_treesame()
692 * should be called to update it after a sequence of treesame[] modifications
693 * that may have affected it.
695 static int compact_treesame(struct rev_info *revs, struct commit *commit, unsigned nth_parent)
697 struct treesame_state *st;
698 int old_same;
700 if (!commit->parents) {
702 * Have just removed the only parent from a non-merge.
703 * Different handling, as we lack decoration.
705 if (nth_parent != 0)
706 die("compact_treesame %u", nth_parent);
707 old_same = !!(commit->object.flags & TREESAME);
708 if (rev_same_tree_as_empty(revs, commit))
709 commit->object.flags |= TREESAME;
710 else
711 commit->object.flags &= ~TREESAME;
712 return old_same;
715 st = lookup_decoration(&revs->treesame, &commit->object);
716 if (!st || nth_parent >= st->nparents)
717 die("compact_treesame %u", nth_parent);
719 old_same = st->treesame[nth_parent];
720 memmove(st->treesame + nth_parent,
721 st->treesame + nth_parent + 1,
722 st->nparents - nth_parent - 1);
725 * If we've just become a non-merge commit, update TREESAME
726 * immediately, and remove the no-longer-needed decoration.
727 * If still a merge, defer update until update_treesame().
729 if (--st->nparents == 1) {
730 if (commit->parents->next)
731 die("compact_treesame parents mismatch");
732 if (st->treesame[0] && revs->dense)
733 commit->object.flags |= TREESAME;
734 else
735 commit->object.flags &= ~TREESAME;
736 free(add_decoration(&revs->treesame, &commit->object, NULL));
739 return old_same;
742 static unsigned update_treesame(struct rev_info *revs, struct commit *commit)
744 if (commit->parents && commit->parents->next) {
745 unsigned n;
746 struct treesame_state *st;
747 struct commit_list *p;
748 unsigned relevant_parents;
749 unsigned relevant_change, irrelevant_change;
751 st = lookup_decoration(&revs->treesame, &commit->object);
752 if (!st)
753 die("update_treesame %s", oid_to_hex(&commit->object.oid));
754 relevant_parents = 0;
755 relevant_change = irrelevant_change = 0;
756 for (p = commit->parents, n = 0; p; n++, p = p->next) {
757 if (relevant_commit(p->item)) {
758 relevant_change |= !st->treesame[n];
759 relevant_parents++;
760 } else
761 irrelevant_change |= !st->treesame[n];
763 if (relevant_parents ? relevant_change : irrelevant_change)
764 commit->object.flags &= ~TREESAME;
765 else
766 commit->object.flags |= TREESAME;
769 return commit->object.flags & TREESAME;
772 static inline int limiting_can_increase_treesame(const struct rev_info *revs)
775 * TREESAME is irrelevant unless prune && dense;
776 * if simplify_history is set, we can't have a mixture of TREESAME and
777 * !TREESAME INTERESTING parents (and we don't have treesame[]
778 * decoration anyway);
779 * if first_parent_only is set, then the TREESAME flag is locked
780 * against the first parent (and again we lack treesame[] decoration).
782 return revs->prune && revs->dense &&
783 !revs->simplify_history &&
784 !revs->first_parent_only;
787 static void try_to_simplify_commit(struct rev_info *revs, struct commit *commit)
789 struct commit_list **pp, *parent;
790 struct treesame_state *ts = NULL;
791 int relevant_change = 0, irrelevant_change = 0;
792 int relevant_parents, nth_parent;
795 * If we don't do pruning, everything is interesting
797 if (!revs->prune)
798 return;
800 if (!get_commit_tree(commit))
801 return;
803 if (!commit->parents) {
804 if (rev_same_tree_as_empty(revs, commit))
805 commit->object.flags |= TREESAME;
806 return;
810 * Normal non-merge commit? If we don't want to make the
811 * history dense, we consider it always to be a change..
813 if (!revs->dense && !commit->parents->next)
814 return;
816 for (pp = &commit->parents, nth_parent = 0, relevant_parents = 0;
817 (parent = *pp) != NULL;
818 pp = &parent->next, nth_parent++) {
819 struct commit *p = parent->item;
820 if (relevant_commit(p))
821 relevant_parents++;
823 if (nth_parent == 1) {
825 * This our second loop iteration - so we now know
826 * we're dealing with a merge.
828 * Do not compare with later parents when we care only about
829 * the first parent chain, in order to avoid derailing the
830 * traversal to follow a side branch that brought everything
831 * in the path we are limited to by the pathspec.
833 if (revs->first_parent_only)
834 break;
836 * If this will remain a potentially-simplifiable
837 * merge, remember per-parent treesame if needed.
838 * Initialise the array with the comparison from our
839 * first iteration.
841 if (revs->treesame.name &&
842 !revs->simplify_history &&
843 !(commit->object.flags & UNINTERESTING)) {
844 ts = initialise_treesame(revs, commit);
845 if (!(irrelevant_change || relevant_change))
846 ts->treesame[0] = 1;
849 if (parse_commit(p) < 0)
850 die("cannot simplify commit %s (because of %s)",
851 oid_to_hex(&commit->object.oid),
852 oid_to_hex(&p->object.oid));
853 switch (rev_compare_tree(revs, p, commit)) {
854 case REV_TREE_SAME:
855 if (!revs->simplify_history || !relevant_commit(p)) {
856 /* Even if a merge with an uninteresting
857 * side branch brought the entire change
858 * we are interested in, we do not want
859 * to lose the other branches of this
860 * merge, so we just keep going.
862 if (ts)
863 ts->treesame[nth_parent] = 1;
864 continue;
866 parent->next = NULL;
867 commit->parents = parent;
868 commit->object.flags |= TREESAME;
869 return;
871 case REV_TREE_NEW:
872 if (revs->remove_empty_trees &&
873 rev_same_tree_as_empty(revs, p)) {
874 /* We are adding all the specified
875 * paths from this parent, so the
876 * history beyond this parent is not
877 * interesting. Remove its parents
878 * (they are grandparents for us).
879 * IOW, we pretend this parent is a
880 * "root" commit.
882 if (parse_commit(p) < 0)
883 die("cannot simplify commit %s (invalid %s)",
884 oid_to_hex(&commit->object.oid),
885 oid_to_hex(&p->object.oid));
886 p->parents = NULL;
888 /* fallthrough */
889 case REV_TREE_OLD:
890 case REV_TREE_DIFFERENT:
891 if (relevant_commit(p))
892 relevant_change = 1;
893 else
894 irrelevant_change = 1;
895 continue;
897 die("bad tree compare for commit %s", oid_to_hex(&commit->object.oid));
901 * TREESAME is straightforward for single-parent commits. For merge
902 * commits, it is most useful to define it so that "irrelevant"
903 * parents cannot make us !TREESAME - if we have any relevant
904 * parents, then we only consider TREESAMEness with respect to them,
905 * allowing irrelevant merges from uninteresting branches to be
906 * simplified away. Only if we have only irrelevant parents do we
907 * base TREESAME on them. Note that this logic is replicated in
908 * update_treesame, which should be kept in sync.
910 if (relevant_parents ? !relevant_change : !irrelevant_change)
911 commit->object.flags |= TREESAME;
914 static int process_parents(struct rev_info *revs, struct commit *commit,
915 struct commit_list **list, struct prio_queue *queue)
917 struct commit_list *parent = commit->parents;
918 unsigned left_flag;
920 if (commit->object.flags & ADDED)
921 return 0;
922 commit->object.flags |= ADDED;
924 if (revs->include_check &&
925 !revs->include_check(commit, revs->include_check_data))
926 return 0;
929 * If the commit is uninteresting, don't try to
930 * prune parents - we want the maximal uninteresting
931 * set.
933 * Normally we haven't parsed the parent
934 * yet, so we won't have a parent of a parent
935 * here. However, it may turn out that we've
936 * reached this commit some other way (where it
937 * wasn't uninteresting), in which case we need
938 * to mark its parents recursively too..
940 if (commit->object.flags & UNINTERESTING) {
941 while (parent) {
942 struct commit *p = parent->item;
943 parent = parent->next;
944 if (p)
945 p->object.flags |= UNINTERESTING;
946 if (parse_commit_gently(p, 1) < 0)
947 continue;
948 if (p->parents)
949 mark_parents_uninteresting(p);
950 if (p->object.flags & SEEN)
951 continue;
952 p->object.flags |= SEEN;
953 if (list)
954 commit_list_insert_by_date(p, list);
955 if (queue)
956 prio_queue_put(queue, p);
958 return 0;
962 * Ok, the commit wasn't uninteresting. Try to
963 * simplify the commit history and find the parent
964 * that has no differences in the path set if one exists.
966 try_to_simplify_commit(revs, commit);
968 if (revs->no_walk)
969 return 0;
971 left_flag = (commit->object.flags & SYMMETRIC_LEFT);
973 for (parent = commit->parents; parent; parent = parent->next) {
974 struct commit *p = parent->item;
975 int gently = revs->ignore_missing_links ||
976 revs->exclude_promisor_objects;
977 if (parse_commit_gently(p, gently) < 0) {
978 if (revs->exclude_promisor_objects &&
979 is_promisor_object(&p->object.oid)) {
980 if (revs->first_parent_only)
981 break;
982 continue;
984 return -1;
986 if (revs->sources) {
987 char **slot = revision_sources_at(revs->sources, p);
989 if (!*slot)
990 *slot = *revision_sources_at(revs->sources, commit);
992 p->object.flags |= left_flag;
993 if (!(p->object.flags & SEEN)) {
994 p->object.flags |= SEEN;
995 if (list)
996 commit_list_insert_by_date(p, list);
997 if (queue)
998 prio_queue_put(queue, p);
1000 if (revs->first_parent_only)
1001 break;
1003 return 0;
1006 static void cherry_pick_list(struct commit_list *list, struct rev_info *revs)
1008 struct commit_list *p;
1009 int left_count = 0, right_count = 0;
1010 int left_first;
1011 struct patch_ids ids;
1012 unsigned cherry_flag;
1014 /* First count the commits on the left and on the right */
1015 for (p = list; p; p = p->next) {
1016 struct commit *commit = p->item;
1017 unsigned flags = commit->object.flags;
1018 if (flags & BOUNDARY)
1020 else if (flags & SYMMETRIC_LEFT)
1021 left_count++;
1022 else
1023 right_count++;
1026 if (!left_count || !right_count)
1027 return;
1029 left_first = left_count < right_count;
1030 init_patch_ids(revs->repo, &ids);
1031 ids.diffopts.pathspec = revs->diffopt.pathspec;
1033 /* Compute patch-ids for one side */
1034 for (p = list; p; p = p->next) {
1035 struct commit *commit = p->item;
1036 unsigned flags = commit->object.flags;
1038 if (flags & BOUNDARY)
1039 continue;
1041 * If we have fewer left, left_first is set and we omit
1042 * commits on the right branch in this loop. If we have
1043 * fewer right, we skip the left ones.
1045 if (left_first != !!(flags & SYMMETRIC_LEFT))
1046 continue;
1047 add_commit_patch_id(commit, &ids);
1050 /* either cherry_mark or cherry_pick are true */
1051 cherry_flag = revs->cherry_mark ? PATCHSAME : SHOWN;
1053 /* Check the other side */
1054 for (p = list; p; p = p->next) {
1055 struct commit *commit = p->item;
1056 struct patch_id *id;
1057 unsigned flags = commit->object.flags;
1059 if (flags & BOUNDARY)
1060 continue;
1062 * If we have fewer left, left_first is set and we omit
1063 * commits on the left branch in this loop.
1065 if (left_first == !!(flags & SYMMETRIC_LEFT))
1066 continue;
1069 * Have we seen the same patch id?
1071 id = has_commit_patch_id(commit, &ids);
1072 if (!id)
1073 continue;
1075 commit->object.flags |= cherry_flag;
1076 id->commit->object.flags |= cherry_flag;
1079 free_patch_ids(&ids);
1082 /* How many extra uninteresting commits we want to see.. */
1083 #define SLOP 5
1085 static int still_interesting(struct commit_list *src, timestamp_t date, int slop,
1086 struct commit **interesting_cache)
1089 * No source list at all? We're definitely done..
1091 if (!src)
1092 return 0;
1095 * Does the destination list contain entries with a date
1096 * before the source list? Definitely _not_ done.
1098 if (date <= src->item->date)
1099 return SLOP;
1102 * Does the source list still have interesting commits in
1103 * it? Definitely not done..
1105 if (!everybody_uninteresting(src, interesting_cache))
1106 return SLOP;
1108 /* Ok, we're closing in.. */
1109 return slop-1;
1113 * "rev-list --ancestry-path A..B" computes commits that are ancestors
1114 * of B but not ancestors of A but further limits the result to those
1115 * that are descendants of A. This takes the list of bottom commits and
1116 * the result of "A..B" without --ancestry-path, and limits the latter
1117 * further to the ones that can reach one of the commits in "bottom".
1119 static void limit_to_ancestry(struct commit_list *bottom, struct commit_list *list)
1121 struct commit_list *p;
1122 struct commit_list *rlist = NULL;
1123 int made_progress;
1126 * Reverse the list so that it will be likely that we would
1127 * process parents before children.
1129 for (p = list; p; p = p->next)
1130 commit_list_insert(p->item, &rlist);
1132 for (p = bottom; p; p = p->next)
1133 p->item->object.flags |= TMP_MARK;
1136 * Mark the ones that can reach bottom commits in "list",
1137 * in a bottom-up fashion.
1139 do {
1140 made_progress = 0;
1141 for (p = rlist; p; p = p->next) {
1142 struct commit *c = p->item;
1143 struct commit_list *parents;
1144 if (c->object.flags & (TMP_MARK | UNINTERESTING))
1145 continue;
1146 for (parents = c->parents;
1147 parents;
1148 parents = parents->next) {
1149 if (!(parents->item->object.flags & TMP_MARK))
1150 continue;
1151 c->object.flags |= TMP_MARK;
1152 made_progress = 1;
1153 break;
1156 } while (made_progress);
1159 * NEEDSWORK: decide if we want to remove parents that are
1160 * not marked with TMP_MARK from commit->parents for commits
1161 * in the resulting list. We may not want to do that, though.
1165 * The ones that are not marked with TMP_MARK are uninteresting
1167 for (p = list; p; p = p->next) {
1168 struct commit *c = p->item;
1169 if (c->object.flags & TMP_MARK)
1170 continue;
1171 c->object.flags |= UNINTERESTING;
1174 /* We are done with the TMP_MARK */
1175 for (p = list; p; p = p->next)
1176 p->item->object.flags &= ~TMP_MARK;
1177 for (p = bottom; p; p = p->next)
1178 p->item->object.flags &= ~TMP_MARK;
1179 free_commit_list(rlist);
1183 * Before walking the history, keep the set of "negative" refs the
1184 * caller has asked to exclude.
1186 * This is used to compute "rev-list --ancestry-path A..B", as we need
1187 * to filter the result of "A..B" further to the ones that can actually
1188 * reach A.
1190 static struct commit_list *collect_bottom_commits(struct commit_list *list)
1192 struct commit_list *elem, *bottom = NULL;
1193 for (elem = list; elem; elem = elem->next)
1194 if (elem->item->object.flags & BOTTOM)
1195 commit_list_insert(elem->item, &bottom);
1196 return bottom;
1199 /* Assumes either left_only or right_only is set */
1200 static void limit_left_right(struct commit_list *list, struct rev_info *revs)
1202 struct commit_list *p;
1204 for (p = list; p; p = p->next) {
1205 struct commit *commit = p->item;
1207 if (revs->right_only) {
1208 if (commit->object.flags & SYMMETRIC_LEFT)
1209 commit->object.flags |= SHOWN;
1210 } else /* revs->left_only is set */
1211 if (!(commit->object.flags & SYMMETRIC_LEFT))
1212 commit->object.flags |= SHOWN;
1216 static int limit_list(struct rev_info *revs)
1218 int slop = SLOP;
1219 timestamp_t date = TIME_MAX;
1220 struct commit_list *list = revs->commits;
1221 struct commit_list *newlist = NULL;
1222 struct commit_list **p = &newlist;
1223 struct commit_list *bottom = NULL;
1224 struct commit *interesting_cache = NULL;
1226 if (revs->ancestry_path) {
1227 bottom = collect_bottom_commits(list);
1228 if (!bottom)
1229 die("--ancestry-path given but there are no bottom commits");
1232 while (list) {
1233 struct commit *commit = pop_commit(&list);
1234 struct object *obj = &commit->object;
1235 show_early_output_fn_t show;
1237 if (commit == interesting_cache)
1238 interesting_cache = NULL;
1240 if (revs->max_age != -1 && (commit->date < revs->max_age))
1241 obj->flags |= UNINTERESTING;
1242 if (process_parents(revs, commit, &list, NULL) < 0)
1243 return -1;
1244 if (obj->flags & UNINTERESTING) {
1245 mark_parents_uninteresting(commit);
1246 slop = still_interesting(list, date, slop, &interesting_cache);
1247 if (slop)
1248 continue;
1249 break;
1251 if (revs->min_age != -1 && (commit->date > revs->min_age))
1252 continue;
1253 date = commit->date;
1254 p = &commit_list_insert(commit, p)->next;
1256 show = show_early_output;
1257 if (!show)
1258 continue;
1260 show(revs, newlist);
1261 show_early_output = NULL;
1263 if (revs->cherry_pick || revs->cherry_mark)
1264 cherry_pick_list(newlist, revs);
1266 if (revs->left_only || revs->right_only)
1267 limit_left_right(newlist, revs);
1269 if (bottom) {
1270 limit_to_ancestry(bottom, newlist);
1271 free_commit_list(bottom);
1275 * Check if any commits have become TREESAME by some of their parents
1276 * becoming UNINTERESTING.
1278 if (limiting_can_increase_treesame(revs))
1279 for (list = newlist; list; list = list->next) {
1280 struct commit *c = list->item;
1281 if (c->object.flags & (UNINTERESTING | TREESAME))
1282 continue;
1283 update_treesame(revs, c);
1286 revs->commits = newlist;
1287 return 0;
1291 * Add an entry to refs->cmdline with the specified information.
1292 * *name is copied.
1294 static void add_rev_cmdline(struct rev_info *revs,
1295 struct object *item,
1296 const char *name,
1297 int whence,
1298 unsigned flags)
1300 struct rev_cmdline_info *info = &revs->cmdline;
1301 unsigned int nr = info->nr;
1303 ALLOC_GROW(info->rev, nr + 1, info->alloc);
1304 info->rev[nr].item = item;
1305 info->rev[nr].name = xstrdup(name);
1306 info->rev[nr].whence = whence;
1307 info->rev[nr].flags = flags;
1308 info->nr++;
1311 static void add_rev_cmdline_list(struct rev_info *revs,
1312 struct commit_list *commit_list,
1313 int whence,
1314 unsigned flags)
1316 while (commit_list) {
1317 struct object *object = &commit_list->item->object;
1318 add_rev_cmdline(revs, object, oid_to_hex(&object->oid),
1319 whence, flags);
1320 commit_list = commit_list->next;
1324 struct all_refs_cb {
1325 int all_flags;
1326 int warned_bad_reflog;
1327 struct rev_info *all_revs;
1328 const char *name_for_errormsg;
1329 struct worktree *wt;
1332 int ref_excluded(struct string_list *ref_excludes, const char *path)
1334 struct string_list_item *item;
1336 if (!ref_excludes)
1337 return 0;
1338 for_each_string_list_item(item, ref_excludes) {
1339 if (!wildmatch(item->string, path, 0))
1340 return 1;
1342 return 0;
1345 static int handle_one_ref(const char *path, const struct object_id *oid,
1346 int flag, void *cb_data)
1348 struct all_refs_cb *cb = cb_data;
1349 struct object *object;
1351 if (ref_excluded(cb->all_revs->ref_excludes, path))
1352 return 0;
1354 object = get_reference(cb->all_revs, path, oid, cb->all_flags);
1355 add_rev_cmdline(cb->all_revs, object, path, REV_CMD_REF, cb->all_flags);
1356 add_pending_oid(cb->all_revs, path, oid, cb->all_flags);
1357 return 0;
1360 static void init_all_refs_cb(struct all_refs_cb *cb, struct rev_info *revs,
1361 unsigned flags)
1363 cb->all_revs = revs;
1364 cb->all_flags = flags;
1365 revs->rev_input_given = 1;
1366 cb->wt = NULL;
1369 void clear_ref_exclusion(struct string_list **ref_excludes_p)
1371 if (*ref_excludes_p) {
1372 string_list_clear(*ref_excludes_p, 0);
1373 free(*ref_excludes_p);
1375 *ref_excludes_p = NULL;
1378 void add_ref_exclusion(struct string_list **ref_excludes_p, const char *exclude)
1380 if (!*ref_excludes_p) {
1381 *ref_excludes_p = xcalloc(1, sizeof(**ref_excludes_p));
1382 (*ref_excludes_p)->strdup_strings = 1;
1384 string_list_append(*ref_excludes_p, exclude);
1387 static void handle_refs(struct ref_store *refs,
1388 struct rev_info *revs, unsigned flags,
1389 int (*for_each)(struct ref_store *, each_ref_fn, void *))
1391 struct all_refs_cb cb;
1393 if (!refs) {
1394 /* this could happen with uninitialized submodules */
1395 return;
1398 init_all_refs_cb(&cb, revs, flags);
1399 for_each(refs, handle_one_ref, &cb);
1402 static void handle_one_reflog_commit(struct object_id *oid, void *cb_data)
1404 struct all_refs_cb *cb = cb_data;
1405 if (!is_null_oid(oid)) {
1406 struct object *o = parse_object(cb->all_revs->repo, oid);
1407 if (o) {
1408 o->flags |= cb->all_flags;
1409 /* ??? CMDLINEFLAGS ??? */
1410 add_pending_object(cb->all_revs, o, "");
1412 else if (!cb->warned_bad_reflog) {
1413 warning("reflog of '%s' references pruned commits",
1414 cb->name_for_errormsg);
1415 cb->warned_bad_reflog = 1;
1420 static int handle_one_reflog_ent(struct object_id *ooid, struct object_id *noid,
1421 const char *email, timestamp_t timestamp, int tz,
1422 const char *message, void *cb_data)
1424 handle_one_reflog_commit(ooid, cb_data);
1425 handle_one_reflog_commit(noid, cb_data);
1426 return 0;
1429 static int handle_one_reflog(const char *refname_in_wt,
1430 const struct object_id *oid,
1431 int flag, void *cb_data)
1433 struct all_refs_cb *cb = cb_data;
1434 struct strbuf refname = STRBUF_INIT;
1436 cb->warned_bad_reflog = 0;
1437 strbuf_worktree_ref(cb->wt, &refname, refname_in_wt);
1438 cb->name_for_errormsg = refname.buf;
1439 refs_for_each_reflog_ent(get_main_ref_store(the_repository),
1440 refname.buf,
1441 handle_one_reflog_ent, cb_data);
1442 strbuf_release(&refname);
1443 return 0;
1446 static void add_other_reflogs_to_pending(struct all_refs_cb *cb)
1448 struct worktree **worktrees, **p;
1450 worktrees = get_worktrees(0);
1451 for (p = worktrees; *p; p++) {
1452 struct worktree *wt = *p;
1454 if (wt->is_current)
1455 continue;
1457 cb->wt = wt;
1458 refs_for_each_reflog(get_worktree_ref_store(wt),
1459 handle_one_reflog,
1460 cb);
1462 free_worktrees(worktrees);
1465 void add_reflogs_to_pending(struct rev_info *revs, unsigned flags)
1467 struct all_refs_cb cb;
1469 cb.all_revs = revs;
1470 cb.all_flags = flags;
1471 cb.wt = NULL;
1472 for_each_reflog(handle_one_reflog, &cb);
1474 if (!revs->single_worktree)
1475 add_other_reflogs_to_pending(&cb);
1478 static void add_cache_tree(struct cache_tree *it, struct rev_info *revs,
1479 struct strbuf *path, unsigned int flags)
1481 size_t baselen = path->len;
1482 int i;
1484 if (it->entry_count >= 0) {
1485 struct tree *tree = lookup_tree(revs->repo, &it->oid);
1486 tree->object.flags |= flags;
1487 add_pending_object_with_path(revs, &tree->object, "",
1488 040000, path->buf);
1491 for (i = 0; i < it->subtree_nr; i++) {
1492 struct cache_tree_sub *sub = it->down[i];
1493 strbuf_addf(path, "%s%s", baselen ? "/" : "", sub->name);
1494 add_cache_tree(sub->cache_tree, revs, path, flags);
1495 strbuf_setlen(path, baselen);
1500 static void do_add_index_objects_to_pending(struct rev_info *revs,
1501 struct index_state *istate,
1502 unsigned int flags)
1504 int i;
1506 for (i = 0; i < istate->cache_nr; i++) {
1507 struct cache_entry *ce = istate->cache[i];
1508 struct blob *blob;
1510 if (S_ISGITLINK(ce->ce_mode))
1511 continue;
1513 blob = lookup_blob(revs->repo, &ce->oid);
1514 if (!blob)
1515 die("unable to add index blob to traversal");
1516 blob->object.flags |= flags;
1517 add_pending_object_with_path(revs, &blob->object, "",
1518 ce->ce_mode, ce->name);
1521 if (istate->cache_tree) {
1522 struct strbuf path = STRBUF_INIT;
1523 add_cache_tree(istate->cache_tree, revs, &path, flags);
1524 strbuf_release(&path);
1528 void add_index_objects_to_pending(struct rev_info *revs, unsigned int flags)
1530 struct worktree **worktrees, **p;
1532 repo_read_index(revs->repo);
1533 do_add_index_objects_to_pending(revs, revs->repo->index, flags);
1535 if (revs->single_worktree)
1536 return;
1538 worktrees = get_worktrees(0);
1539 for (p = worktrees; *p; p++) {
1540 struct worktree *wt = *p;
1541 struct index_state istate = { NULL };
1543 if (wt->is_current)
1544 continue; /* current index already taken care of */
1546 if (read_index_from(&istate,
1547 worktree_git_path(wt, "index"),
1548 get_worktree_git_dir(wt)) > 0)
1549 do_add_index_objects_to_pending(revs, &istate, flags);
1550 discard_index(&istate);
1552 free_worktrees(worktrees);
1555 static int add_parents_only(struct rev_info *revs, const char *arg_, int flags,
1556 int exclude_parent)
1558 struct object_id oid;
1559 struct object *it;
1560 struct commit *commit;
1561 struct commit_list *parents;
1562 int parent_number;
1563 const char *arg = arg_;
1565 if (*arg == '^') {
1566 flags ^= UNINTERESTING | BOTTOM;
1567 arg++;
1569 if (get_oid_committish(arg, &oid))
1570 return 0;
1571 while (1) {
1572 it = get_reference(revs, arg, &oid, 0);
1573 if (!it && revs->ignore_missing)
1574 return 0;
1575 if (it->type != OBJ_TAG)
1576 break;
1577 if (!((struct tag*)it)->tagged)
1578 return 0;
1579 oidcpy(&oid, &((struct tag*)it)->tagged->oid);
1581 if (it->type != OBJ_COMMIT)
1582 return 0;
1583 commit = (struct commit *)it;
1584 if (exclude_parent &&
1585 exclude_parent > commit_list_count(commit->parents))
1586 return 0;
1587 for (parents = commit->parents, parent_number = 1;
1588 parents;
1589 parents = parents->next, parent_number++) {
1590 if (exclude_parent && parent_number != exclude_parent)
1591 continue;
1593 it = &parents->item->object;
1594 it->flags |= flags;
1595 add_rev_cmdline(revs, it, arg_, REV_CMD_PARENTS_ONLY, flags);
1596 add_pending_object(revs, it, arg);
1598 return 1;
1601 void repo_init_revisions(struct repository *r,
1602 struct rev_info *revs,
1603 const char *prefix)
1605 memset(revs, 0, sizeof(*revs));
1607 revs->repo = r;
1608 revs->abbrev = DEFAULT_ABBREV;
1609 revs->ignore_merges = 1;
1610 revs->simplify_history = 1;
1611 revs->pruning.repo = r;
1612 revs->pruning.flags.recursive = 1;
1613 revs->pruning.flags.quick = 1;
1614 revs->pruning.add_remove = file_add_remove;
1615 revs->pruning.change = file_change;
1616 revs->pruning.change_fn_data = revs;
1617 revs->sort_order = REV_SORT_IN_GRAPH_ORDER;
1618 revs->dense = 1;
1619 revs->prefix = prefix;
1620 revs->max_age = -1;
1621 revs->min_age = -1;
1622 revs->skip_count = -1;
1623 revs->max_count = -1;
1624 revs->max_parents = -1;
1625 revs->expand_tabs_in_log = -1;
1627 revs->commit_format = CMIT_FMT_DEFAULT;
1628 revs->expand_tabs_in_log_default = 8;
1630 init_grep_defaults(revs->repo);
1631 grep_init(&revs->grep_filter, revs->repo, prefix);
1632 revs->grep_filter.status_only = 1;
1634 repo_diff_setup(revs->repo, &revs->diffopt);
1635 if (prefix && !revs->diffopt.prefix) {
1636 revs->diffopt.prefix = prefix;
1637 revs->diffopt.prefix_length = strlen(prefix);
1640 revs->notes_opt.use_default_notes = -1;
1643 static void add_pending_commit_list(struct rev_info *revs,
1644 struct commit_list *commit_list,
1645 unsigned int flags)
1647 while (commit_list) {
1648 struct object *object = &commit_list->item->object;
1649 object->flags |= flags;
1650 add_pending_object(revs, object, oid_to_hex(&object->oid));
1651 commit_list = commit_list->next;
1655 static void prepare_show_merge(struct rev_info *revs)
1657 struct commit_list *bases;
1658 struct commit *head, *other;
1659 struct object_id oid;
1660 const char **prune = NULL;
1661 int i, prune_num = 1; /* counting terminating NULL */
1662 struct index_state *istate = revs->repo->index;
1664 if (get_oid("HEAD", &oid))
1665 die("--merge without HEAD?");
1666 head = lookup_commit_or_die(&oid, "HEAD");
1667 if (get_oid("MERGE_HEAD", &oid))
1668 die("--merge without MERGE_HEAD?");
1669 other = lookup_commit_or_die(&oid, "MERGE_HEAD");
1670 add_pending_object(revs, &head->object, "HEAD");
1671 add_pending_object(revs, &other->object, "MERGE_HEAD");
1672 bases = get_merge_bases(head, other);
1673 add_rev_cmdline_list(revs, bases, REV_CMD_MERGE_BASE, UNINTERESTING | BOTTOM);
1674 add_pending_commit_list(revs, bases, UNINTERESTING | BOTTOM);
1675 free_commit_list(bases);
1676 head->object.flags |= SYMMETRIC_LEFT;
1678 if (!istate->cache_nr)
1679 repo_read_index(revs->repo);
1680 for (i = 0; i < istate->cache_nr; i++) {
1681 const struct cache_entry *ce = istate->cache[i];
1682 if (!ce_stage(ce))
1683 continue;
1684 if (ce_path_match(istate, ce, &revs->prune_data, NULL)) {
1685 prune_num++;
1686 REALLOC_ARRAY(prune, prune_num);
1687 prune[prune_num-2] = ce->name;
1688 prune[prune_num-1] = NULL;
1690 while ((i+1 < istate->cache_nr) &&
1691 ce_same_name(ce, istate->cache[i+1]))
1692 i++;
1694 clear_pathspec(&revs->prune_data);
1695 parse_pathspec(&revs->prune_data, PATHSPEC_ALL_MAGIC & ~PATHSPEC_LITERAL,
1696 PATHSPEC_PREFER_FULL | PATHSPEC_LITERAL_PATH, "", prune);
1697 revs->limited = 1;
1700 static int dotdot_missing(const char *arg, char *dotdot,
1701 struct rev_info *revs, int symmetric)
1703 if (revs->ignore_missing)
1704 return 0;
1705 /* de-munge so we report the full argument */
1706 *dotdot = '.';
1707 die(symmetric
1708 ? "Invalid symmetric difference expression %s"
1709 : "Invalid revision range %s", arg);
1712 static int handle_dotdot_1(const char *arg, char *dotdot,
1713 struct rev_info *revs, int flags,
1714 int cant_be_filename,
1715 struct object_context *a_oc,
1716 struct object_context *b_oc)
1718 const char *a_name, *b_name;
1719 struct object_id a_oid, b_oid;
1720 struct object *a_obj, *b_obj;
1721 unsigned int a_flags, b_flags;
1722 int symmetric = 0;
1723 unsigned int flags_exclude = flags ^ (UNINTERESTING | BOTTOM);
1724 unsigned int oc_flags = GET_OID_COMMITTISH | GET_OID_RECORD_PATH;
1726 a_name = arg;
1727 if (!*a_name)
1728 a_name = "HEAD";
1730 b_name = dotdot + 2;
1731 if (*b_name == '.') {
1732 symmetric = 1;
1733 b_name++;
1735 if (!*b_name)
1736 b_name = "HEAD";
1738 if (get_oid_with_context(revs->repo, a_name, oc_flags, &a_oid, a_oc) ||
1739 get_oid_with_context(revs->repo, b_name, oc_flags, &b_oid, b_oc))
1740 return -1;
1742 if (!cant_be_filename) {
1743 *dotdot = '.';
1744 verify_non_filename(revs->prefix, arg);
1745 *dotdot = '\0';
1748 a_obj = parse_object(revs->repo, &a_oid);
1749 b_obj = parse_object(revs->repo, &b_oid);
1750 if (!a_obj || !b_obj)
1751 return dotdot_missing(arg, dotdot, revs, symmetric);
1753 if (!symmetric) {
1754 /* just A..B */
1755 b_flags = flags;
1756 a_flags = flags_exclude;
1757 } else {
1758 /* A...B -- find merge bases between the two */
1759 struct commit *a, *b;
1760 struct commit_list *exclude;
1762 a = lookup_commit_reference(revs->repo, &a_obj->oid);
1763 b = lookup_commit_reference(revs->repo, &b_obj->oid);
1764 if (!a || !b)
1765 return dotdot_missing(arg, dotdot, revs, symmetric);
1767 exclude = get_merge_bases(a, b);
1768 add_rev_cmdline_list(revs, exclude, REV_CMD_MERGE_BASE,
1769 flags_exclude);
1770 add_pending_commit_list(revs, exclude, flags_exclude);
1771 free_commit_list(exclude);
1773 b_flags = flags;
1774 a_flags = flags | SYMMETRIC_LEFT;
1777 a_obj->flags |= a_flags;
1778 b_obj->flags |= b_flags;
1779 add_rev_cmdline(revs, a_obj, a_name, REV_CMD_LEFT, a_flags);
1780 add_rev_cmdline(revs, b_obj, b_name, REV_CMD_RIGHT, b_flags);
1781 add_pending_object_with_path(revs, a_obj, a_name, a_oc->mode, a_oc->path);
1782 add_pending_object_with_path(revs, b_obj, b_name, b_oc->mode, b_oc->path);
1783 return 0;
1786 static int handle_dotdot(const char *arg,
1787 struct rev_info *revs, int flags,
1788 int cant_be_filename)
1790 struct object_context a_oc, b_oc;
1791 char *dotdot = strstr(arg, "..");
1792 int ret;
1794 if (!dotdot)
1795 return -1;
1797 memset(&a_oc, 0, sizeof(a_oc));
1798 memset(&b_oc, 0, sizeof(b_oc));
1800 *dotdot = '\0';
1801 ret = handle_dotdot_1(arg, dotdot, revs, flags, cant_be_filename,
1802 &a_oc, &b_oc);
1803 *dotdot = '.';
1805 free(a_oc.path);
1806 free(b_oc.path);
1808 return ret;
1811 int handle_revision_arg(const char *arg_, struct rev_info *revs, int flags, unsigned revarg_opt)
1813 struct object_context oc;
1814 char *mark;
1815 struct object *object;
1816 struct object_id oid;
1817 int local_flags;
1818 const char *arg = arg_;
1819 int cant_be_filename = revarg_opt & REVARG_CANNOT_BE_FILENAME;
1820 unsigned get_sha1_flags = GET_OID_RECORD_PATH;
1822 flags = flags & UNINTERESTING ? flags | BOTTOM : flags & ~BOTTOM;
1824 if (!cant_be_filename && !strcmp(arg, "..")) {
1826 * Just ".."? That is not a range but the
1827 * pathspec for the parent directory.
1829 return -1;
1832 if (!handle_dotdot(arg, revs, flags, revarg_opt))
1833 return 0;
1835 mark = strstr(arg, "^@");
1836 if (mark && !mark[2]) {
1837 *mark = 0;
1838 if (add_parents_only(revs, arg, flags, 0))
1839 return 0;
1840 *mark = '^';
1842 mark = strstr(arg, "^!");
1843 if (mark && !mark[2]) {
1844 *mark = 0;
1845 if (!add_parents_only(revs, arg, flags ^ (UNINTERESTING | BOTTOM), 0))
1846 *mark = '^';
1848 mark = strstr(arg, "^-");
1849 if (mark) {
1850 int exclude_parent = 1;
1852 if (mark[2]) {
1853 char *end;
1854 exclude_parent = strtoul(mark + 2, &end, 10);
1855 if (*end != '\0' || !exclude_parent)
1856 return -1;
1859 *mark = 0;
1860 if (!add_parents_only(revs, arg, flags ^ (UNINTERESTING | BOTTOM), exclude_parent))
1861 *mark = '^';
1864 local_flags = 0;
1865 if (*arg == '^') {
1866 local_flags = UNINTERESTING | BOTTOM;
1867 arg++;
1870 if (revarg_opt & REVARG_COMMITTISH)
1871 get_sha1_flags |= GET_OID_COMMITTISH;
1873 if (get_oid_with_context(revs->repo, arg, get_sha1_flags, &oid, &oc))
1874 return revs->ignore_missing ? 0 : -1;
1875 if (!cant_be_filename)
1876 verify_non_filename(revs->prefix, arg);
1877 object = get_reference(revs, arg, &oid, flags ^ local_flags);
1878 if (!object)
1879 return revs->ignore_missing ? 0 : -1;
1880 add_rev_cmdline(revs, object, arg_, REV_CMD_REV, flags ^ local_flags);
1881 add_pending_object_with_path(revs, object, arg, oc.mode, oc.path);
1882 free(oc.path);
1883 return 0;
1886 static void read_pathspec_from_stdin(struct strbuf *sb,
1887 struct argv_array *prune)
1889 while (strbuf_getline(sb, stdin) != EOF)
1890 argv_array_push(prune, sb->buf);
1893 static void read_revisions_from_stdin(struct rev_info *revs,
1894 struct argv_array *prune)
1896 struct strbuf sb;
1897 int seen_dashdash = 0;
1898 int save_warning;
1900 save_warning = warn_on_object_refname_ambiguity;
1901 warn_on_object_refname_ambiguity = 0;
1903 strbuf_init(&sb, 1000);
1904 while (strbuf_getline(&sb, stdin) != EOF) {
1905 int len = sb.len;
1906 if (!len)
1907 break;
1908 if (sb.buf[0] == '-') {
1909 if (len == 2 && sb.buf[1] == '-') {
1910 seen_dashdash = 1;
1911 break;
1913 die("options not supported in --stdin mode");
1915 if (handle_revision_arg(sb.buf, revs, 0,
1916 REVARG_CANNOT_BE_FILENAME))
1917 die("bad revision '%s'", sb.buf);
1919 if (seen_dashdash)
1920 read_pathspec_from_stdin(&sb, prune);
1922 strbuf_release(&sb);
1923 warn_on_object_refname_ambiguity = save_warning;
1926 static void add_grep(struct rev_info *revs, const char *ptn, enum grep_pat_token what)
1928 append_grep_pattern(&revs->grep_filter, ptn, "command line", 0, what);
1931 static void add_header_grep(struct rev_info *revs, enum grep_header_field field, const char *pattern)
1933 append_header_grep_pattern(&revs->grep_filter, field, pattern);
1936 static void add_message_grep(struct rev_info *revs, const char *pattern)
1938 add_grep(revs, pattern, GREP_PATTERN_BODY);
1941 static int handle_revision_opt(struct rev_info *revs, int argc, const char **argv,
1942 int *unkc, const char **unkv,
1943 const struct setup_revision_opt* opt)
1945 const char *arg = argv[0];
1946 const char *optarg;
1947 int argcount;
1948 const unsigned hexsz = the_hash_algo->hexsz;
1950 /* pseudo revision arguments */
1951 if (!strcmp(arg, "--all") || !strcmp(arg, "--branches") ||
1952 !strcmp(arg, "--tags") || !strcmp(arg, "--remotes") ||
1953 !strcmp(arg, "--reflog") || !strcmp(arg, "--not") ||
1954 !strcmp(arg, "--no-walk") || !strcmp(arg, "--do-walk") ||
1955 !strcmp(arg, "--bisect") || starts_with(arg, "--glob=") ||
1956 !strcmp(arg, "--indexed-objects") ||
1957 starts_with(arg, "--exclude=") ||
1958 starts_with(arg, "--branches=") || starts_with(arg, "--tags=") ||
1959 starts_with(arg, "--remotes=") || starts_with(arg, "--no-walk="))
1961 unkv[(*unkc)++] = arg;
1962 return 1;
1965 if ((argcount = parse_long_opt("max-count", argv, &optarg))) {
1966 revs->max_count = atoi(optarg);
1967 revs->no_walk = 0;
1968 return argcount;
1969 } else if ((argcount = parse_long_opt("skip", argv, &optarg))) {
1970 revs->skip_count = atoi(optarg);
1971 return argcount;
1972 } else if ((*arg == '-') && isdigit(arg[1])) {
1973 /* accept -<digit>, like traditional "head" */
1974 if (strtol_i(arg + 1, 10, &revs->max_count) < 0 ||
1975 revs->max_count < 0)
1976 die("'%s': not a non-negative integer", arg + 1);
1977 revs->no_walk = 0;
1978 } else if (!strcmp(arg, "-n")) {
1979 if (argc <= 1)
1980 return error("-n requires an argument");
1981 revs->max_count = atoi(argv[1]);
1982 revs->no_walk = 0;
1983 return 2;
1984 } else if (skip_prefix(arg, "-n", &optarg)) {
1985 revs->max_count = atoi(optarg);
1986 revs->no_walk = 0;
1987 } else if ((argcount = parse_long_opt("max-age", argv, &optarg))) {
1988 revs->max_age = atoi(optarg);
1989 return argcount;
1990 } else if ((argcount = parse_long_opt("since", argv, &optarg))) {
1991 revs->max_age = approxidate(optarg);
1992 return argcount;
1993 } else if ((argcount = parse_long_opt("after", argv, &optarg))) {
1994 revs->max_age = approxidate(optarg);
1995 return argcount;
1996 } else if ((argcount = parse_long_opt("min-age", argv, &optarg))) {
1997 revs->min_age = atoi(optarg);
1998 return argcount;
1999 } else if ((argcount = parse_long_opt("before", argv, &optarg))) {
2000 revs->min_age = approxidate(optarg);
2001 return argcount;
2002 } else if ((argcount = parse_long_opt("until", argv, &optarg))) {
2003 revs->min_age = approxidate(optarg);
2004 return argcount;
2005 } else if (!strcmp(arg, "--first-parent")) {
2006 revs->first_parent_only = 1;
2007 } else if (!strcmp(arg, "--ancestry-path")) {
2008 revs->ancestry_path = 1;
2009 revs->simplify_history = 0;
2010 revs->limited = 1;
2011 } else if (!strcmp(arg, "-g") || !strcmp(arg, "--walk-reflogs")) {
2012 init_reflog_walk(&revs->reflog_info);
2013 } else if (!strcmp(arg, "--default")) {
2014 if (argc <= 1)
2015 return error("bad --default argument");
2016 revs->def = argv[1];
2017 return 2;
2018 } else if (!strcmp(arg, "--merge")) {
2019 revs->show_merge = 1;
2020 } else if (!strcmp(arg, "--topo-order")) {
2021 revs->sort_order = REV_SORT_IN_GRAPH_ORDER;
2022 revs->topo_order = 1;
2023 } else if (!strcmp(arg, "--simplify-merges")) {
2024 revs->simplify_merges = 1;
2025 revs->topo_order = 1;
2026 revs->rewrite_parents = 1;
2027 revs->simplify_history = 0;
2028 revs->limited = 1;
2029 } else if (!strcmp(arg, "--simplify-by-decoration")) {
2030 revs->simplify_merges = 1;
2031 revs->topo_order = 1;
2032 revs->rewrite_parents = 1;
2033 revs->simplify_history = 0;
2034 revs->simplify_by_decoration = 1;
2035 revs->limited = 1;
2036 revs->prune = 1;
2037 load_ref_decorations(NULL, DECORATE_SHORT_REFS);
2038 } else if (!strcmp(arg, "--date-order")) {
2039 revs->sort_order = REV_SORT_BY_COMMIT_DATE;
2040 revs->topo_order = 1;
2041 } else if (!strcmp(arg, "--author-date-order")) {
2042 revs->sort_order = REV_SORT_BY_AUTHOR_DATE;
2043 revs->topo_order = 1;
2044 } else if (!strcmp(arg, "--early-output")) {
2045 revs->early_output = 100;
2046 revs->topo_order = 1;
2047 } else if (skip_prefix(arg, "--early-output=", &optarg)) {
2048 if (strtoul_ui(optarg, 10, &revs->early_output) < 0)
2049 die("'%s': not a non-negative integer", optarg);
2050 revs->topo_order = 1;
2051 } else if (!strcmp(arg, "--parents")) {
2052 revs->rewrite_parents = 1;
2053 revs->print_parents = 1;
2054 } else if (!strcmp(arg, "--dense")) {
2055 revs->dense = 1;
2056 } else if (!strcmp(arg, "--sparse")) {
2057 revs->dense = 0;
2058 } else if (!strcmp(arg, "--in-commit-order")) {
2059 revs->tree_blobs_in_commit_order = 1;
2060 } else if (!strcmp(arg, "--remove-empty")) {
2061 revs->remove_empty_trees = 1;
2062 } else if (!strcmp(arg, "--merges")) {
2063 revs->min_parents = 2;
2064 } else if (!strcmp(arg, "--no-merges")) {
2065 revs->max_parents = 1;
2066 } else if (skip_prefix(arg, "--min-parents=", &optarg)) {
2067 revs->min_parents = atoi(optarg);
2068 } else if (!strcmp(arg, "--no-min-parents")) {
2069 revs->min_parents = 0;
2070 } else if (skip_prefix(arg, "--max-parents=", &optarg)) {
2071 revs->max_parents = atoi(optarg);
2072 } else if (!strcmp(arg, "--no-max-parents")) {
2073 revs->max_parents = -1;
2074 } else if (!strcmp(arg, "--boundary")) {
2075 revs->boundary = 1;
2076 } else if (!strcmp(arg, "--left-right")) {
2077 revs->left_right = 1;
2078 } else if (!strcmp(arg, "--left-only")) {
2079 if (revs->right_only)
2080 die("--left-only is incompatible with --right-only"
2081 " or --cherry");
2082 revs->left_only = 1;
2083 } else if (!strcmp(arg, "--right-only")) {
2084 if (revs->left_only)
2085 die("--right-only is incompatible with --left-only");
2086 revs->right_only = 1;
2087 } else if (!strcmp(arg, "--cherry")) {
2088 if (revs->left_only)
2089 die("--cherry is incompatible with --left-only");
2090 revs->cherry_mark = 1;
2091 revs->right_only = 1;
2092 revs->max_parents = 1;
2093 revs->limited = 1;
2094 } else if (!strcmp(arg, "--count")) {
2095 revs->count = 1;
2096 } else if (!strcmp(arg, "--cherry-mark")) {
2097 if (revs->cherry_pick)
2098 die("--cherry-mark is incompatible with --cherry-pick");
2099 revs->cherry_mark = 1;
2100 revs->limited = 1; /* needs limit_list() */
2101 } else if (!strcmp(arg, "--cherry-pick")) {
2102 if (revs->cherry_mark)
2103 die("--cherry-pick is incompatible with --cherry-mark");
2104 revs->cherry_pick = 1;
2105 revs->limited = 1;
2106 } else if (!strcmp(arg, "--objects")) {
2107 revs->tag_objects = 1;
2108 revs->tree_objects = 1;
2109 revs->blob_objects = 1;
2110 } else if (!strcmp(arg, "--objects-edge")) {
2111 revs->tag_objects = 1;
2112 revs->tree_objects = 1;
2113 revs->blob_objects = 1;
2114 revs->edge_hint = 1;
2115 } else if (!strcmp(arg, "--objects-edge-aggressive")) {
2116 revs->tag_objects = 1;
2117 revs->tree_objects = 1;
2118 revs->blob_objects = 1;
2119 revs->edge_hint = 1;
2120 revs->edge_hint_aggressive = 1;
2121 } else if (!strcmp(arg, "--verify-objects")) {
2122 revs->tag_objects = 1;
2123 revs->tree_objects = 1;
2124 revs->blob_objects = 1;
2125 revs->verify_objects = 1;
2126 } else if (!strcmp(arg, "--unpacked")) {
2127 revs->unpacked = 1;
2128 } else if (starts_with(arg, "--unpacked=")) {
2129 die("--unpacked=<packfile> no longer supported.");
2130 } else if (!strcmp(arg, "-r")) {
2131 revs->diff = 1;
2132 revs->diffopt.flags.recursive = 1;
2133 } else if (!strcmp(arg, "-t")) {
2134 revs->diff = 1;
2135 revs->diffopt.flags.recursive = 1;
2136 revs->diffopt.flags.tree_in_recursive = 1;
2137 } else if (!strcmp(arg, "-m")) {
2138 revs->ignore_merges = 0;
2139 } else if (!strcmp(arg, "-c")) {
2140 revs->diff = 1;
2141 revs->dense_combined_merges = 0;
2142 revs->combine_merges = 1;
2143 } else if (!strcmp(arg, "--combined-all-paths")) {
2144 revs->diff = 1;
2145 revs->combined_all_paths = 1;
2146 } else if (!strcmp(arg, "--cc")) {
2147 revs->diff = 1;
2148 revs->dense_combined_merges = 1;
2149 revs->combine_merges = 1;
2150 } else if (!strcmp(arg, "-v")) {
2151 revs->verbose_header = 1;
2152 } else if (!strcmp(arg, "--pretty")) {
2153 revs->verbose_header = 1;
2154 revs->pretty_given = 1;
2155 get_commit_format(NULL, revs);
2156 } else if (skip_prefix(arg, "--pretty=", &optarg) ||
2157 skip_prefix(arg, "--format=", &optarg)) {
2159 * Detached form ("--pretty X" as opposed to "--pretty=X")
2160 * not allowed, since the argument is optional.
2162 revs->verbose_header = 1;
2163 revs->pretty_given = 1;
2164 get_commit_format(optarg, revs);
2165 } else if (!strcmp(arg, "--expand-tabs")) {
2166 revs->expand_tabs_in_log = 8;
2167 } else if (!strcmp(arg, "--no-expand-tabs")) {
2168 revs->expand_tabs_in_log = 0;
2169 } else if (skip_prefix(arg, "--expand-tabs=", &arg)) {
2170 int val;
2171 if (strtol_i(arg, 10, &val) < 0 || val < 0)
2172 die("'%s': not a non-negative integer", arg);
2173 revs->expand_tabs_in_log = val;
2174 } else if (!strcmp(arg, "--show-notes") || !strcmp(arg, "--notes")) {
2175 revs->show_notes = 1;
2176 revs->show_notes_given = 1;
2177 revs->notes_opt.use_default_notes = 1;
2178 } else if (!strcmp(arg, "--show-signature")) {
2179 revs->show_signature = 1;
2180 } else if (!strcmp(arg, "--no-show-signature")) {
2181 revs->show_signature = 0;
2182 } else if (!strcmp(arg, "--show-linear-break")) {
2183 revs->break_bar = " ..........";
2184 revs->track_linear = 1;
2185 revs->track_first_time = 1;
2186 } else if (skip_prefix(arg, "--show-linear-break=", &optarg)) {
2187 revs->break_bar = xstrdup(optarg);
2188 revs->track_linear = 1;
2189 revs->track_first_time = 1;
2190 } else if (skip_prefix(arg, "--show-notes=", &optarg) ||
2191 skip_prefix(arg, "--notes=", &optarg)) {
2192 struct strbuf buf = STRBUF_INIT;
2193 revs->show_notes = 1;
2194 revs->show_notes_given = 1;
2195 if (starts_with(arg, "--show-notes=") &&
2196 revs->notes_opt.use_default_notes < 0)
2197 revs->notes_opt.use_default_notes = 1;
2198 strbuf_addstr(&buf, optarg);
2199 expand_notes_ref(&buf);
2200 string_list_append(&revs->notes_opt.extra_notes_refs,
2201 strbuf_detach(&buf, NULL));
2202 } else if (!strcmp(arg, "--no-notes")) {
2203 revs->show_notes = 0;
2204 revs->show_notes_given = 1;
2205 revs->notes_opt.use_default_notes = -1;
2206 /* we have been strdup'ing ourselves, so trick
2207 * string_list into free()ing strings */
2208 revs->notes_opt.extra_notes_refs.strdup_strings = 1;
2209 string_list_clear(&revs->notes_opt.extra_notes_refs, 0);
2210 revs->notes_opt.extra_notes_refs.strdup_strings = 0;
2211 } else if (!strcmp(arg, "--standard-notes")) {
2212 revs->show_notes_given = 1;
2213 revs->notes_opt.use_default_notes = 1;
2214 } else if (!strcmp(arg, "--no-standard-notes")) {
2215 revs->notes_opt.use_default_notes = 0;
2216 } else if (!strcmp(arg, "--oneline")) {
2217 revs->verbose_header = 1;
2218 get_commit_format("oneline", revs);
2219 revs->pretty_given = 1;
2220 revs->abbrev_commit = 1;
2221 } else if (!strcmp(arg, "--graph")) {
2222 revs->topo_order = 1;
2223 revs->rewrite_parents = 1;
2224 revs->graph = graph_init(revs);
2225 } else if (!strcmp(arg, "--root")) {
2226 revs->show_root_diff = 1;
2227 } else if (!strcmp(arg, "--no-commit-id")) {
2228 revs->no_commit_id = 1;
2229 } else if (!strcmp(arg, "--always")) {
2230 revs->always_show_header = 1;
2231 } else if (!strcmp(arg, "--no-abbrev")) {
2232 revs->abbrev = 0;
2233 } else if (!strcmp(arg, "--abbrev")) {
2234 revs->abbrev = DEFAULT_ABBREV;
2235 } else if (skip_prefix(arg, "--abbrev=", &optarg)) {
2236 revs->abbrev = strtoul(optarg, NULL, 10);
2237 if (revs->abbrev < MINIMUM_ABBREV)
2238 revs->abbrev = MINIMUM_ABBREV;
2239 else if (revs->abbrev > hexsz)
2240 revs->abbrev = hexsz;
2241 } else if (!strcmp(arg, "--abbrev-commit")) {
2242 revs->abbrev_commit = 1;
2243 revs->abbrev_commit_given = 1;
2244 } else if (!strcmp(arg, "--no-abbrev-commit")) {
2245 revs->abbrev_commit = 0;
2246 } else if (!strcmp(arg, "--full-diff")) {
2247 revs->diff = 1;
2248 revs->full_diff = 1;
2249 } else if (!strcmp(arg, "--full-history")) {
2250 revs->simplify_history = 0;
2251 } else if (!strcmp(arg, "--relative-date")) {
2252 revs->date_mode.type = DATE_RELATIVE;
2253 revs->date_mode_explicit = 1;
2254 } else if ((argcount = parse_long_opt("date", argv, &optarg))) {
2255 parse_date_format(optarg, &revs->date_mode);
2256 revs->date_mode_explicit = 1;
2257 return argcount;
2258 } else if (!strcmp(arg, "--log-size")) {
2259 revs->show_log_size = 1;
2262 * Grepping the commit log
2264 else if ((argcount = parse_long_opt("author", argv, &optarg))) {
2265 add_header_grep(revs, GREP_HEADER_AUTHOR, optarg);
2266 return argcount;
2267 } else if ((argcount = parse_long_opt("committer", argv, &optarg))) {
2268 add_header_grep(revs, GREP_HEADER_COMMITTER, optarg);
2269 return argcount;
2270 } else if ((argcount = parse_long_opt("grep-reflog", argv, &optarg))) {
2271 add_header_grep(revs, GREP_HEADER_REFLOG, optarg);
2272 return argcount;
2273 } else if ((argcount = parse_long_opt("grep", argv, &optarg))) {
2274 add_message_grep(revs, optarg);
2275 return argcount;
2276 } else if (!strcmp(arg, "--grep-debug")) {
2277 revs->grep_filter.debug = 1;
2278 } else if (!strcmp(arg, "--basic-regexp")) {
2279 revs->grep_filter.pattern_type_option = GREP_PATTERN_TYPE_BRE;
2280 } else if (!strcmp(arg, "--extended-regexp") || !strcmp(arg, "-E")) {
2281 revs->grep_filter.pattern_type_option = GREP_PATTERN_TYPE_ERE;
2282 } else if (!strcmp(arg, "--regexp-ignore-case") || !strcmp(arg, "-i")) {
2283 revs->grep_filter.ignore_case = 1;
2284 revs->diffopt.pickaxe_opts |= DIFF_PICKAXE_IGNORE_CASE;
2285 } else if (!strcmp(arg, "--fixed-strings") || !strcmp(arg, "-F")) {
2286 revs->grep_filter.pattern_type_option = GREP_PATTERN_TYPE_FIXED;
2287 } else if (!strcmp(arg, "--perl-regexp") || !strcmp(arg, "-P")) {
2288 revs->grep_filter.pattern_type_option = GREP_PATTERN_TYPE_PCRE;
2289 } else if (!strcmp(arg, "--all-match")) {
2290 revs->grep_filter.all_match = 1;
2291 } else if (!strcmp(arg, "--invert-grep")) {
2292 revs->invert_grep = 1;
2293 } else if ((argcount = parse_long_opt("encoding", argv, &optarg))) {
2294 if (strcmp(optarg, "none"))
2295 git_log_output_encoding = xstrdup(optarg);
2296 else
2297 git_log_output_encoding = "";
2298 return argcount;
2299 } else if (!strcmp(arg, "--reverse")) {
2300 revs->reverse ^= 1;
2301 } else if (!strcmp(arg, "--children")) {
2302 revs->children.name = "children";
2303 revs->limited = 1;
2304 } else if (!strcmp(arg, "--ignore-missing")) {
2305 revs->ignore_missing = 1;
2306 } else if (opt && opt->allow_exclude_promisor_objects &&
2307 !strcmp(arg, "--exclude-promisor-objects")) {
2308 if (fetch_if_missing)
2309 BUG("exclude_promisor_objects can only be used when fetch_if_missing is 0");
2310 revs->exclude_promisor_objects = 1;
2311 } else {
2312 int opts = diff_opt_parse(&revs->diffopt, argv, argc, revs->prefix);
2313 if (!opts)
2314 unkv[(*unkc)++] = arg;
2315 return opts;
2317 if (revs->graph && revs->track_linear)
2318 die("--show-linear-break and --graph are incompatible");
2320 return 1;
2323 void parse_revision_opt(struct rev_info *revs, struct parse_opt_ctx_t *ctx,
2324 const struct option *options,
2325 const char * const usagestr[])
2327 int n = handle_revision_opt(revs, ctx->argc, ctx->argv,
2328 &ctx->cpidx, ctx->out, NULL);
2329 if (n <= 0) {
2330 error("unknown option `%s'", ctx->argv[0]);
2331 usage_with_options(usagestr, options);
2333 ctx->argv += n;
2334 ctx->argc -= n;
2337 static int for_each_bisect_ref(struct ref_store *refs, each_ref_fn fn,
2338 void *cb_data, const char *term)
2340 struct strbuf bisect_refs = STRBUF_INIT;
2341 int status;
2342 strbuf_addf(&bisect_refs, "refs/bisect/%s", term);
2343 status = refs_for_each_fullref_in(refs, bisect_refs.buf, fn, cb_data, 0);
2344 strbuf_release(&bisect_refs);
2345 return status;
2348 static int for_each_bad_bisect_ref(struct ref_store *refs, each_ref_fn fn, void *cb_data)
2350 return for_each_bisect_ref(refs, fn, cb_data, term_bad);
2353 static int for_each_good_bisect_ref(struct ref_store *refs, each_ref_fn fn, void *cb_data)
2355 return for_each_bisect_ref(refs, fn, cb_data, term_good);
2358 static int handle_revision_pseudo_opt(const char *submodule,
2359 struct rev_info *revs,
2360 int argc, const char **argv, int *flags)
2362 const char *arg = argv[0];
2363 const char *optarg;
2364 struct ref_store *refs;
2365 int argcount;
2367 if (submodule) {
2369 * We need some something like get_submodule_worktrees()
2370 * before we can go through all worktrees of a submodule,
2371 * .e.g with adding all HEADs from --all, which is not
2372 * supported right now, so stick to single worktree.
2374 if (!revs->single_worktree)
2375 BUG("--single-worktree cannot be used together with submodule");
2376 refs = get_submodule_ref_store(submodule);
2377 } else
2378 refs = get_main_ref_store(revs->repo);
2381 * NOTE!
2383 * Commands like "git shortlog" will not accept the options below
2384 * unless parse_revision_opt queues them (as opposed to erroring
2385 * out).
2387 * When implementing your new pseudo-option, remember to
2388 * register it in the list at the top of handle_revision_opt.
2390 if (!strcmp(arg, "--all")) {
2391 handle_refs(refs, revs, *flags, refs_for_each_ref);
2392 handle_refs(refs, revs, *flags, refs_head_ref);
2393 if (!revs->single_worktree) {
2394 struct all_refs_cb cb;
2396 init_all_refs_cb(&cb, revs, *flags);
2397 other_head_refs(handle_one_ref, &cb);
2399 clear_ref_exclusion(&revs->ref_excludes);
2400 } else if (!strcmp(arg, "--branches")) {
2401 handle_refs(refs, revs, *flags, refs_for_each_branch_ref);
2402 clear_ref_exclusion(&revs->ref_excludes);
2403 } else if (!strcmp(arg, "--bisect")) {
2404 read_bisect_terms(&term_bad, &term_good);
2405 handle_refs(refs, revs, *flags, for_each_bad_bisect_ref);
2406 handle_refs(refs, revs, *flags ^ (UNINTERESTING | BOTTOM),
2407 for_each_good_bisect_ref);
2408 revs->bisect = 1;
2409 } else if (!strcmp(arg, "--tags")) {
2410 handle_refs(refs, revs, *flags, refs_for_each_tag_ref);
2411 clear_ref_exclusion(&revs->ref_excludes);
2412 } else if (!strcmp(arg, "--remotes")) {
2413 handle_refs(refs, revs, *flags, refs_for_each_remote_ref);
2414 clear_ref_exclusion(&revs->ref_excludes);
2415 } else if ((argcount = parse_long_opt("glob", argv, &optarg))) {
2416 struct all_refs_cb cb;
2417 init_all_refs_cb(&cb, revs, *flags);
2418 for_each_glob_ref(handle_one_ref, optarg, &cb);
2419 clear_ref_exclusion(&revs->ref_excludes);
2420 return argcount;
2421 } else if ((argcount = parse_long_opt("exclude", argv, &optarg))) {
2422 add_ref_exclusion(&revs->ref_excludes, optarg);
2423 return argcount;
2424 } else if (skip_prefix(arg, "--branches=", &optarg)) {
2425 struct all_refs_cb cb;
2426 init_all_refs_cb(&cb, revs, *flags);
2427 for_each_glob_ref_in(handle_one_ref, optarg, "refs/heads/", &cb);
2428 clear_ref_exclusion(&revs->ref_excludes);
2429 } else if (skip_prefix(arg, "--tags=", &optarg)) {
2430 struct all_refs_cb cb;
2431 init_all_refs_cb(&cb, revs, *flags);
2432 for_each_glob_ref_in(handle_one_ref, optarg, "refs/tags/", &cb);
2433 clear_ref_exclusion(&revs->ref_excludes);
2434 } else if (skip_prefix(arg, "--remotes=", &optarg)) {
2435 struct all_refs_cb cb;
2436 init_all_refs_cb(&cb, revs, *flags);
2437 for_each_glob_ref_in(handle_one_ref, optarg, "refs/remotes/", &cb);
2438 clear_ref_exclusion(&revs->ref_excludes);
2439 } else if (!strcmp(arg, "--reflog")) {
2440 add_reflogs_to_pending(revs, *flags);
2441 } else if (!strcmp(arg, "--indexed-objects")) {
2442 add_index_objects_to_pending(revs, *flags);
2443 } else if (!strcmp(arg, "--not")) {
2444 *flags ^= UNINTERESTING | BOTTOM;
2445 } else if (!strcmp(arg, "--no-walk")) {
2446 revs->no_walk = REVISION_WALK_NO_WALK_SORTED;
2447 } else if (skip_prefix(arg, "--no-walk=", &optarg)) {
2449 * Detached form ("--no-walk X" as opposed to "--no-walk=X")
2450 * not allowed, since the argument is optional.
2452 if (!strcmp(optarg, "sorted"))
2453 revs->no_walk = REVISION_WALK_NO_WALK_SORTED;
2454 else if (!strcmp(optarg, "unsorted"))
2455 revs->no_walk = REVISION_WALK_NO_WALK_UNSORTED;
2456 else
2457 return error("invalid argument to --no-walk");
2458 } else if (!strcmp(arg, "--do-walk")) {
2459 revs->no_walk = 0;
2460 } else if (!strcmp(arg, "--single-worktree")) {
2461 revs->single_worktree = 1;
2462 } else {
2463 return 0;
2466 return 1;
2469 static void NORETURN diagnose_missing_default(const char *def)
2471 int flags;
2472 const char *refname;
2474 refname = resolve_ref_unsafe(def, 0, NULL, &flags);
2475 if (!refname || !(flags & REF_ISSYMREF) || (flags & REF_ISBROKEN))
2476 die(_("your current branch appears to be broken"));
2478 skip_prefix(refname, "refs/heads/", &refname);
2479 die(_("your current branch '%s' does not have any commits yet"),
2480 refname);
2484 * Parse revision information, filling in the "rev_info" structure,
2485 * and removing the used arguments from the argument list.
2487 * Returns the number of arguments left that weren't recognized
2488 * (which are also moved to the head of the argument list)
2490 int setup_revisions(int argc, const char **argv, struct rev_info *revs, struct setup_revision_opt *opt)
2492 int i, flags, left, seen_dashdash, got_rev_arg = 0, revarg_opt;
2493 struct argv_array prune_data = ARGV_ARRAY_INIT;
2494 const char *submodule = NULL;
2496 if (opt)
2497 submodule = opt->submodule;
2499 /* First, search for "--" */
2500 if (opt && opt->assume_dashdash) {
2501 seen_dashdash = 1;
2502 } else {
2503 seen_dashdash = 0;
2504 for (i = 1; i < argc; i++) {
2505 const char *arg = argv[i];
2506 if (strcmp(arg, "--"))
2507 continue;
2508 argv[i] = NULL;
2509 argc = i;
2510 if (argv[i + 1])
2511 argv_array_pushv(&prune_data, argv + i + 1);
2512 seen_dashdash = 1;
2513 break;
2517 /* Second, deal with arguments and options */
2518 flags = 0;
2519 revarg_opt = opt ? opt->revarg_opt : 0;
2520 if (seen_dashdash)
2521 revarg_opt |= REVARG_CANNOT_BE_FILENAME;
2522 for (left = i = 1; i < argc; i++) {
2523 const char *arg = argv[i];
2524 if (*arg == '-') {
2525 int opts;
2527 opts = handle_revision_pseudo_opt(submodule,
2528 revs, argc - i, argv + i,
2529 &flags);
2530 if (opts > 0) {
2531 i += opts - 1;
2532 continue;
2535 if (!strcmp(arg, "--stdin")) {
2536 if (revs->disable_stdin) {
2537 argv[left++] = arg;
2538 continue;
2540 if (revs->read_from_stdin++)
2541 die("--stdin given twice?");
2542 read_revisions_from_stdin(revs, &prune_data);
2543 continue;
2546 opts = handle_revision_opt(revs, argc - i, argv + i,
2547 &left, argv, opt);
2548 if (opts > 0) {
2549 i += opts - 1;
2550 continue;
2552 if (opts < 0)
2553 exit(128);
2554 continue;
2558 if (handle_revision_arg(arg, revs, flags, revarg_opt)) {
2559 int j;
2560 if (seen_dashdash || *arg == '^')
2561 die("bad revision '%s'", arg);
2563 /* If we didn't have a "--":
2564 * (1) all filenames must exist;
2565 * (2) all rev-args must not be interpretable
2566 * as a valid filename.
2567 * but the latter we have checked in the main loop.
2569 for (j = i; j < argc; j++)
2570 verify_filename(revs->prefix, argv[j], j == i);
2572 argv_array_pushv(&prune_data, argv + i);
2573 break;
2575 else
2576 got_rev_arg = 1;
2579 if (prune_data.argc) {
2581 * If we need to introduce the magic "a lone ':' means no
2582 * pathspec whatsoever", here is the place to do so.
2584 * if (prune_data.nr == 1 && !strcmp(prune_data[0], ":")) {
2585 * prune_data.nr = 0;
2586 * prune_data.alloc = 0;
2587 * free(prune_data.path);
2588 * prune_data.path = NULL;
2589 * } else {
2590 * terminate prune_data.alloc with NULL and
2591 * call init_pathspec() to set revs->prune_data here.
2594 parse_pathspec(&revs->prune_data, 0, 0,
2595 revs->prefix, prune_data.argv);
2597 argv_array_clear(&prune_data);
2599 if (revs->def == NULL)
2600 revs->def = opt ? opt->def : NULL;
2601 if (opt && opt->tweak)
2602 opt->tweak(revs, opt);
2603 if (revs->show_merge)
2604 prepare_show_merge(revs);
2605 if (revs->def && !revs->pending.nr && !revs->rev_input_given && !got_rev_arg) {
2606 struct object_id oid;
2607 struct object *object;
2608 struct object_context oc;
2609 if (get_oid_with_context(revs->repo, revs->def, 0, &oid, &oc))
2610 diagnose_missing_default(revs->def);
2611 object = get_reference(revs, revs->def, &oid, 0);
2612 add_pending_object_with_mode(revs, object, revs->def, oc.mode);
2615 /* Did the user ask for any diff output? Run the diff! */
2616 if (revs->diffopt.output_format & ~DIFF_FORMAT_NO_OUTPUT)
2617 revs->diff = 1;
2619 /* Pickaxe, diff-filter and rename following need diffs */
2620 if ((revs->diffopt.pickaxe_opts & DIFF_PICKAXE_KINDS_MASK) ||
2621 revs->diffopt.filter ||
2622 revs->diffopt.flags.follow_renames)
2623 revs->diff = 1;
2625 if (revs->diffopt.objfind)
2626 revs->simplify_history = 0;
2628 if (revs->topo_order && !generation_numbers_enabled(the_repository))
2629 revs->limited = 1;
2631 if (revs->prune_data.nr) {
2632 copy_pathspec(&revs->pruning.pathspec, &revs->prune_data);
2633 /* Can't prune commits with rename following: the paths change.. */
2634 if (!revs->diffopt.flags.follow_renames)
2635 revs->prune = 1;
2636 if (!revs->full_diff)
2637 copy_pathspec(&revs->diffopt.pathspec,
2638 &revs->prune_data);
2640 if (revs->combine_merges)
2641 revs->ignore_merges = 0;
2642 if (revs->combined_all_paths && !revs->combine_merges)
2643 die("--combined-all-paths makes no sense without -c or --cc");
2645 revs->diffopt.abbrev = revs->abbrev;
2647 if (revs->line_level_traverse) {
2648 revs->limited = 1;
2649 revs->topo_order = 1;
2652 diff_setup_done(&revs->diffopt);
2654 grep_commit_pattern_type(GREP_PATTERN_TYPE_UNSPECIFIED,
2655 &revs->grep_filter);
2656 compile_grep_patterns(&revs->grep_filter);
2658 if (revs->reverse && revs->reflog_info)
2659 die("cannot combine --reverse with --walk-reflogs");
2660 if (revs->reflog_info && revs->limited)
2661 die("cannot combine --walk-reflogs with history-limiting options");
2662 if (revs->rewrite_parents && revs->children.name)
2663 die("cannot combine --parents and --children");
2666 * Limitations on the graph functionality
2668 if (revs->reverse && revs->graph)
2669 die("cannot combine --reverse with --graph");
2671 if (revs->reflog_info && revs->graph)
2672 die("cannot combine --walk-reflogs with --graph");
2673 if (revs->no_walk && revs->graph)
2674 die("cannot combine --no-walk with --graph");
2675 if (!revs->reflog_info && revs->grep_filter.use_reflog_filter)
2676 die("cannot use --grep-reflog without --walk-reflogs");
2678 if (revs->first_parent_only && revs->bisect)
2679 die(_("--first-parent is incompatible with --bisect"));
2681 if (revs->line_level_traverse &&
2682 (revs->diffopt.output_format & ~(DIFF_FORMAT_PATCH | DIFF_FORMAT_NO_OUTPUT)))
2683 die(_("-L does not yet support diff formats besides -p and -s"));
2685 if (revs->expand_tabs_in_log < 0)
2686 revs->expand_tabs_in_log = revs->expand_tabs_in_log_default;
2688 return left;
2691 static void add_child(struct rev_info *revs, struct commit *parent, struct commit *child)
2693 struct commit_list *l = xcalloc(1, sizeof(*l));
2695 l->item = child;
2696 l->next = add_decoration(&revs->children, &parent->object, l);
2699 static int remove_duplicate_parents(struct rev_info *revs, struct commit *commit)
2701 struct treesame_state *ts = lookup_decoration(&revs->treesame, &commit->object);
2702 struct commit_list **pp, *p;
2703 int surviving_parents;
2705 /* Examine existing parents while marking ones we have seen... */
2706 pp = &commit->parents;
2707 surviving_parents = 0;
2708 while ((p = *pp) != NULL) {
2709 struct commit *parent = p->item;
2710 if (parent->object.flags & TMP_MARK) {
2711 *pp = p->next;
2712 if (ts)
2713 compact_treesame(revs, commit, surviving_parents);
2714 continue;
2716 parent->object.flags |= TMP_MARK;
2717 surviving_parents++;
2718 pp = &p->next;
2720 /* clear the temporary mark */
2721 for (p = commit->parents; p; p = p->next) {
2722 p->item->object.flags &= ~TMP_MARK;
2724 /* no update_treesame() - removing duplicates can't affect TREESAME */
2725 return surviving_parents;
2728 struct merge_simplify_state {
2729 struct commit *simplified;
2732 static struct merge_simplify_state *locate_simplify_state(struct rev_info *revs, struct commit *commit)
2734 struct merge_simplify_state *st;
2736 st = lookup_decoration(&revs->merge_simplification, &commit->object);
2737 if (!st) {
2738 st = xcalloc(1, sizeof(*st));
2739 add_decoration(&revs->merge_simplification, &commit->object, st);
2741 return st;
2744 static int mark_redundant_parents(struct commit *commit)
2746 struct commit_list *h = reduce_heads(commit->parents);
2747 int i = 0, marked = 0;
2748 struct commit_list *po, *pn;
2750 /* Want these for sanity-checking only */
2751 int orig_cnt = commit_list_count(commit->parents);
2752 int cnt = commit_list_count(h);
2755 * Not ready to remove items yet, just mark them for now, based
2756 * on the output of reduce_heads(). reduce_heads outputs the reduced
2757 * set in its original order, so this isn't too hard.
2759 po = commit->parents;
2760 pn = h;
2761 while (po) {
2762 if (pn && po->item == pn->item) {
2763 pn = pn->next;
2764 i++;
2765 } else {
2766 po->item->object.flags |= TMP_MARK;
2767 marked++;
2769 po=po->next;
2772 if (i != cnt || cnt+marked != orig_cnt)
2773 die("mark_redundant_parents %d %d %d %d", orig_cnt, cnt, i, marked);
2775 free_commit_list(h);
2777 return marked;
2780 static int mark_treesame_root_parents(struct commit *commit)
2782 struct commit_list *p;
2783 int marked = 0;
2785 for (p = commit->parents; p; p = p->next) {
2786 struct commit *parent = p->item;
2787 if (!parent->parents && (parent->object.flags & TREESAME)) {
2788 parent->object.flags |= TMP_MARK;
2789 marked++;
2793 return marked;
2797 * Awkward naming - this means one parent we are TREESAME to.
2798 * cf mark_treesame_root_parents: root parents that are TREESAME (to an
2799 * empty tree). Better name suggestions?
2801 static int leave_one_treesame_to_parent(struct rev_info *revs, struct commit *commit)
2803 struct treesame_state *ts = lookup_decoration(&revs->treesame, &commit->object);
2804 struct commit *unmarked = NULL, *marked = NULL;
2805 struct commit_list *p;
2806 unsigned n;
2808 for (p = commit->parents, n = 0; p; p = p->next, n++) {
2809 if (ts->treesame[n]) {
2810 if (p->item->object.flags & TMP_MARK) {
2811 if (!marked)
2812 marked = p->item;
2813 } else {
2814 if (!unmarked) {
2815 unmarked = p->item;
2816 break;
2823 * If we are TREESAME to a marked-for-deletion parent, but not to any
2824 * unmarked parents, unmark the first TREESAME parent. This is the
2825 * parent that the default simplify_history==1 scan would have followed,
2826 * and it doesn't make sense to omit that path when asking for a
2827 * simplified full history. Retaining it improves the chances of
2828 * understanding odd missed merges that took an old version of a file.
2830 * Example:
2832 * I--------*X A modified the file, but mainline merge X used
2833 * \ / "-s ours", so took the version from I. X is
2834 * `-*A--' TREESAME to I and !TREESAME to A.
2836 * Default log from X would produce "I". Without this check,
2837 * --full-history --simplify-merges would produce "I-A-X", showing
2838 * the merge commit X and that it changed A, but not making clear that
2839 * it had just taken the I version. With this check, the topology above
2840 * is retained.
2842 * Note that it is possible that the simplification chooses a different
2843 * TREESAME parent from the default, in which case this test doesn't
2844 * activate, and we _do_ drop the default parent. Example:
2846 * I------X A modified the file, but it was reverted in B,
2847 * \ / meaning mainline merge X is TREESAME to both
2848 * *A-*B parents.
2850 * Default log would produce "I" by following the first parent;
2851 * --full-history --simplify-merges will produce "I-A-B". But this is a
2852 * reasonable result - it presents a logical full history leading from
2853 * I to X, and X is not an important merge.
2855 if (!unmarked && marked) {
2856 marked->object.flags &= ~TMP_MARK;
2857 return 1;
2860 return 0;
2863 static int remove_marked_parents(struct rev_info *revs, struct commit *commit)
2865 struct commit_list **pp, *p;
2866 int nth_parent, removed = 0;
2868 pp = &commit->parents;
2869 nth_parent = 0;
2870 while ((p = *pp) != NULL) {
2871 struct commit *parent = p->item;
2872 if (parent->object.flags & TMP_MARK) {
2873 parent->object.flags &= ~TMP_MARK;
2874 *pp = p->next;
2875 free(p);
2876 removed++;
2877 compact_treesame(revs, commit, nth_parent);
2878 continue;
2880 pp = &p->next;
2881 nth_parent++;
2884 /* Removing parents can only increase TREESAMEness */
2885 if (removed && !(commit->object.flags & TREESAME))
2886 update_treesame(revs, commit);
2888 return nth_parent;
2891 static struct commit_list **simplify_one(struct rev_info *revs, struct commit *commit, struct commit_list **tail)
2893 struct commit_list *p;
2894 struct commit *parent;
2895 struct merge_simplify_state *st, *pst;
2896 int cnt;
2898 st = locate_simplify_state(revs, commit);
2901 * Have we handled this one?
2903 if (st->simplified)
2904 return tail;
2907 * An UNINTERESTING commit simplifies to itself, so does a
2908 * root commit. We do not rewrite parents of such commit
2909 * anyway.
2911 if ((commit->object.flags & UNINTERESTING) || !commit->parents) {
2912 st->simplified = commit;
2913 return tail;
2917 * Do we know what commit all of our parents that matter
2918 * should be rewritten to? Otherwise we are not ready to
2919 * rewrite this one yet.
2921 for (cnt = 0, p = commit->parents; p; p = p->next) {
2922 pst = locate_simplify_state(revs, p->item);
2923 if (!pst->simplified) {
2924 tail = &commit_list_insert(p->item, tail)->next;
2925 cnt++;
2927 if (revs->first_parent_only)
2928 break;
2930 if (cnt) {
2931 tail = &commit_list_insert(commit, tail)->next;
2932 return tail;
2936 * Rewrite our list of parents. Note that this cannot
2937 * affect our TREESAME flags in any way - a commit is
2938 * always TREESAME to its simplification.
2940 for (p = commit->parents; p; p = p->next) {
2941 pst = locate_simplify_state(revs, p->item);
2942 p->item = pst->simplified;
2943 if (revs->first_parent_only)
2944 break;
2947 if (revs->first_parent_only)
2948 cnt = 1;
2949 else
2950 cnt = remove_duplicate_parents(revs, commit);
2953 * It is possible that we are a merge and one side branch
2954 * does not have any commit that touches the given paths;
2955 * in such a case, the immediate parent from that branch
2956 * will be rewritten to be the merge base.
2958 * o----X X: the commit we are looking at;
2959 * / / o: a commit that touches the paths;
2960 * ---o----'
2962 * Further, a merge of an independent branch that doesn't
2963 * touch the path will reduce to a treesame root parent:
2965 * ----o----X X: the commit we are looking at;
2966 * / o: a commit that touches the paths;
2967 * r r: a root commit not touching the paths
2969 * Detect and simplify both cases.
2971 if (1 < cnt) {
2972 int marked = mark_redundant_parents(commit);
2973 marked += mark_treesame_root_parents(commit);
2974 if (marked)
2975 marked -= leave_one_treesame_to_parent(revs, commit);
2976 if (marked)
2977 cnt = remove_marked_parents(revs, commit);
2981 * A commit simplifies to itself if it is a root, if it is
2982 * UNINTERESTING, if it touches the given paths, or if it is a
2983 * merge and its parents don't simplify to one relevant commit
2984 * (the first two cases are already handled at the beginning of
2985 * this function).
2987 * Otherwise, it simplifies to what its sole relevant parent
2988 * simplifies to.
2990 if (!cnt ||
2991 (commit->object.flags & UNINTERESTING) ||
2992 !(commit->object.flags & TREESAME) ||
2993 (parent = one_relevant_parent(revs, commit->parents)) == NULL)
2994 st->simplified = commit;
2995 else {
2996 pst = locate_simplify_state(revs, parent);
2997 st->simplified = pst->simplified;
2999 return tail;
3002 static void simplify_merges(struct rev_info *revs)
3004 struct commit_list *list, *next;
3005 struct commit_list *yet_to_do, **tail;
3006 struct commit *commit;
3008 if (!revs->prune)
3009 return;
3011 /* feed the list reversed */
3012 yet_to_do = NULL;
3013 for (list = revs->commits; list; list = next) {
3014 commit = list->item;
3015 next = list->next;
3017 * Do not free(list) here yet; the original list
3018 * is used later in this function.
3020 commit_list_insert(commit, &yet_to_do);
3022 while (yet_to_do) {
3023 list = yet_to_do;
3024 yet_to_do = NULL;
3025 tail = &yet_to_do;
3026 while (list) {
3027 commit = pop_commit(&list);
3028 tail = simplify_one(revs, commit, tail);
3032 /* clean up the result, removing the simplified ones */
3033 list = revs->commits;
3034 revs->commits = NULL;
3035 tail = &revs->commits;
3036 while (list) {
3037 struct merge_simplify_state *st;
3039 commit = pop_commit(&list);
3040 st = locate_simplify_state(revs, commit);
3041 if (st->simplified == commit)
3042 tail = &commit_list_insert(commit, tail)->next;
3046 static void set_children(struct rev_info *revs)
3048 struct commit_list *l;
3049 for (l = revs->commits; l; l = l->next) {
3050 struct commit *commit = l->item;
3051 struct commit_list *p;
3053 for (p = commit->parents; p; p = p->next)
3054 add_child(revs, p->item, commit);
3058 void reset_revision_walk(void)
3060 clear_object_flags(SEEN | ADDED | SHOWN);
3063 static int mark_uninteresting(const struct object_id *oid,
3064 struct packed_git *pack,
3065 uint32_t pos,
3066 void *cb)
3068 struct rev_info *revs = cb;
3069 struct object *o = parse_object(revs->repo, oid);
3070 o->flags |= UNINTERESTING | SEEN;
3071 return 0;
3074 define_commit_slab(indegree_slab, int);
3075 define_commit_slab(author_date_slab, timestamp_t);
3077 struct topo_walk_info {
3078 uint32_t min_generation;
3079 struct prio_queue explore_queue;
3080 struct prio_queue indegree_queue;
3081 struct prio_queue topo_queue;
3082 struct indegree_slab indegree;
3083 struct author_date_slab author_date;
3086 static inline void test_flag_and_insert(struct prio_queue *q, struct commit *c, int flag)
3088 if (c->object.flags & flag)
3089 return;
3091 c->object.flags |= flag;
3092 prio_queue_put(q, c);
3095 static void explore_walk_step(struct rev_info *revs)
3097 struct topo_walk_info *info = revs->topo_walk_info;
3098 struct commit_list *p;
3099 struct commit *c = prio_queue_get(&info->explore_queue);
3101 if (!c)
3102 return;
3104 if (parse_commit_gently(c, 1) < 0)
3105 return;
3107 if (revs->sort_order == REV_SORT_BY_AUTHOR_DATE)
3108 record_author_date(&info->author_date, c);
3110 if (revs->max_age != -1 && (c->date < revs->max_age))
3111 c->object.flags |= UNINTERESTING;
3113 if (process_parents(revs, c, NULL, NULL) < 0)
3114 return;
3116 if (c->object.flags & UNINTERESTING)
3117 mark_parents_uninteresting(c);
3119 for (p = c->parents; p; p = p->next)
3120 test_flag_and_insert(&info->explore_queue, p->item, TOPO_WALK_EXPLORED);
3123 static void explore_to_depth(struct rev_info *revs,
3124 uint32_t gen_cutoff)
3126 struct topo_walk_info *info = revs->topo_walk_info;
3127 struct commit *c;
3128 while ((c = prio_queue_peek(&info->explore_queue)) &&
3129 c->generation >= gen_cutoff)
3130 explore_walk_step(revs);
3133 static void indegree_walk_step(struct rev_info *revs)
3135 struct commit_list *p;
3136 struct topo_walk_info *info = revs->topo_walk_info;
3137 struct commit *c = prio_queue_get(&info->indegree_queue);
3139 if (!c)
3140 return;
3142 if (parse_commit_gently(c, 1) < 0)
3143 return;
3145 explore_to_depth(revs, c->generation);
3147 for (p = c->parents; p; p = p->next) {
3148 struct commit *parent = p->item;
3149 int *pi = indegree_slab_at(&info->indegree, parent);
3151 if (*pi)
3152 (*pi)++;
3153 else
3154 *pi = 2;
3156 test_flag_and_insert(&info->indegree_queue, parent, TOPO_WALK_INDEGREE);
3158 if (revs->first_parent_only)
3159 return;
3163 static void compute_indegrees_to_depth(struct rev_info *revs,
3164 uint32_t gen_cutoff)
3166 struct topo_walk_info *info = revs->topo_walk_info;
3167 struct commit *c;
3168 while ((c = prio_queue_peek(&info->indegree_queue)) &&
3169 c->generation >= gen_cutoff)
3170 indegree_walk_step(revs);
3173 static void init_topo_walk(struct rev_info *revs)
3175 struct topo_walk_info *info;
3176 struct commit_list *list;
3177 revs->topo_walk_info = xmalloc(sizeof(struct topo_walk_info));
3178 info = revs->topo_walk_info;
3179 memset(info, 0, sizeof(struct topo_walk_info));
3181 init_indegree_slab(&info->indegree);
3182 memset(&info->explore_queue, 0, sizeof(info->explore_queue));
3183 memset(&info->indegree_queue, 0, sizeof(info->indegree_queue));
3184 memset(&info->topo_queue, 0, sizeof(info->topo_queue));
3186 switch (revs->sort_order) {
3187 default: /* REV_SORT_IN_GRAPH_ORDER */
3188 info->topo_queue.compare = NULL;
3189 break;
3190 case REV_SORT_BY_COMMIT_DATE:
3191 info->topo_queue.compare = compare_commits_by_commit_date;
3192 break;
3193 case REV_SORT_BY_AUTHOR_DATE:
3194 init_author_date_slab(&info->author_date);
3195 info->topo_queue.compare = compare_commits_by_author_date;
3196 info->topo_queue.cb_data = &info->author_date;
3197 break;
3200 info->explore_queue.compare = compare_commits_by_gen_then_commit_date;
3201 info->indegree_queue.compare = compare_commits_by_gen_then_commit_date;
3203 info->min_generation = GENERATION_NUMBER_INFINITY;
3204 for (list = revs->commits; list; list = list->next) {
3205 struct commit *c = list->item;
3207 if (parse_commit_gently(c, 1))
3208 continue;
3210 test_flag_and_insert(&info->explore_queue, c, TOPO_WALK_EXPLORED);
3211 test_flag_and_insert(&info->indegree_queue, c, TOPO_WALK_INDEGREE);
3213 if (c->generation < info->min_generation)
3214 info->min_generation = c->generation;
3216 *(indegree_slab_at(&info->indegree, c)) = 1;
3218 if (revs->sort_order == REV_SORT_BY_AUTHOR_DATE)
3219 record_author_date(&info->author_date, c);
3221 compute_indegrees_to_depth(revs, info->min_generation);
3223 for (list = revs->commits; list; list = list->next) {
3224 struct commit *c = list->item;
3226 if (*(indegree_slab_at(&info->indegree, c)) == 1)
3227 prio_queue_put(&info->topo_queue, c);
3231 * This is unfortunate; the initial tips need to be shown
3232 * in the order given from the revision traversal machinery.
3234 if (revs->sort_order == REV_SORT_IN_GRAPH_ORDER)
3235 prio_queue_reverse(&info->topo_queue);
3238 static struct commit *next_topo_commit(struct rev_info *revs)
3240 struct commit *c;
3241 struct topo_walk_info *info = revs->topo_walk_info;
3243 /* pop next off of topo_queue */
3244 c = prio_queue_get(&info->topo_queue);
3246 if (c)
3247 *(indegree_slab_at(&info->indegree, c)) = 0;
3249 return c;
3252 static void expand_topo_walk(struct rev_info *revs, struct commit *commit)
3254 struct commit_list *p;
3255 struct topo_walk_info *info = revs->topo_walk_info;
3256 if (process_parents(revs, commit, NULL, NULL) < 0) {
3257 if (!revs->ignore_missing_links)
3258 die("Failed to traverse parents of commit %s",
3259 oid_to_hex(&commit->object.oid));
3262 for (p = commit->parents; p; p = p->next) {
3263 struct commit *parent = p->item;
3264 int *pi;
3266 if (parse_commit_gently(parent, 1) < 0)
3267 continue;
3269 if (parent->generation < info->min_generation) {
3270 info->min_generation = parent->generation;
3271 compute_indegrees_to_depth(revs, info->min_generation);
3274 pi = indegree_slab_at(&info->indegree, parent);
3276 (*pi)--;
3277 if (*pi == 1)
3278 prio_queue_put(&info->topo_queue, parent);
3280 if (revs->first_parent_only)
3281 return;
3285 int prepare_revision_walk(struct rev_info *revs)
3287 int i;
3288 struct object_array old_pending;
3289 struct commit_list **next = &revs->commits;
3291 memcpy(&old_pending, &revs->pending, sizeof(old_pending));
3292 revs->pending.nr = 0;
3293 revs->pending.alloc = 0;
3294 revs->pending.objects = NULL;
3295 for (i = 0; i < old_pending.nr; i++) {
3296 struct object_array_entry *e = old_pending.objects + i;
3297 struct commit *commit = handle_commit(revs, e);
3298 if (commit) {
3299 if (!(commit->object.flags & SEEN)) {
3300 commit->object.flags |= SEEN;
3301 next = commit_list_append(commit, next);
3305 object_array_clear(&old_pending);
3307 /* Signal whether we need per-parent treesame decoration */
3308 if (revs->simplify_merges ||
3309 (revs->limited && limiting_can_increase_treesame(revs)))
3310 revs->treesame.name = "treesame";
3312 if (revs->exclude_promisor_objects) {
3313 for_each_packed_object(mark_uninteresting, revs,
3314 FOR_EACH_OBJECT_PROMISOR_ONLY);
3317 if (revs->no_walk != REVISION_WALK_NO_WALK_UNSORTED)
3318 commit_list_sort_by_date(&revs->commits);
3319 if (revs->no_walk)
3320 return 0;
3321 if (revs->limited) {
3322 if (limit_list(revs) < 0)
3323 return -1;
3324 if (revs->topo_order)
3325 sort_in_topological_order(&revs->commits, revs->sort_order);
3326 } else if (revs->topo_order)
3327 init_topo_walk(revs);
3328 if (revs->line_level_traverse)
3329 line_log_filter(revs);
3330 if (revs->simplify_merges)
3331 simplify_merges(revs);
3332 if (revs->children.name)
3333 set_children(revs);
3334 return 0;
3337 static enum rewrite_result rewrite_one_1(struct rev_info *revs,
3338 struct commit **pp,
3339 struct prio_queue *queue)
3341 for (;;) {
3342 struct commit *p = *pp;
3343 if (!revs->limited)
3344 if (process_parents(revs, p, NULL, queue) < 0)
3345 return rewrite_one_error;
3346 if (p->object.flags & UNINTERESTING)
3347 return rewrite_one_ok;
3348 if (!(p->object.flags & TREESAME))
3349 return rewrite_one_ok;
3350 if (!p->parents)
3351 return rewrite_one_noparents;
3352 if ((p = one_relevant_parent(revs, p->parents)) == NULL)
3353 return rewrite_one_ok;
3354 *pp = p;
3358 static void merge_queue_into_list(struct prio_queue *q, struct commit_list **list)
3360 while (q->nr) {
3361 struct commit *item = prio_queue_peek(q);
3362 struct commit_list *p = *list;
3364 if (p && p->item->date >= item->date)
3365 list = &p->next;
3366 else {
3367 p = commit_list_insert(item, list);
3368 list = &p->next; /* skip newly added item */
3369 prio_queue_get(q); /* pop item */
3374 static enum rewrite_result rewrite_one(struct rev_info *revs, struct commit **pp)
3376 struct prio_queue queue = { compare_commits_by_commit_date };
3377 enum rewrite_result ret = rewrite_one_1(revs, pp, &queue);
3378 merge_queue_into_list(&queue, &revs->commits);
3379 clear_prio_queue(&queue);
3380 return ret;
3383 int rewrite_parents(struct rev_info *revs, struct commit *commit,
3384 rewrite_parent_fn_t rewrite_parent)
3386 struct commit_list **pp = &commit->parents;
3387 while (*pp) {
3388 struct commit_list *parent = *pp;
3389 switch (rewrite_parent(revs, &parent->item)) {
3390 case rewrite_one_ok:
3391 break;
3392 case rewrite_one_noparents:
3393 *pp = parent->next;
3394 continue;
3395 case rewrite_one_error:
3396 return -1;
3398 pp = &parent->next;
3400 remove_duplicate_parents(revs, commit);
3401 return 0;
3404 static int commit_rewrite_person(struct strbuf *buf, const char *what, struct string_list *mailmap)
3406 char *person, *endp;
3407 size_t len, namelen, maillen;
3408 const char *name;
3409 const char *mail;
3410 struct ident_split ident;
3412 person = strstr(buf->buf, what);
3413 if (!person)
3414 return 0;
3416 person += strlen(what);
3417 endp = strchr(person, '\n');
3418 if (!endp)
3419 return 0;
3421 len = endp - person;
3423 if (split_ident_line(&ident, person, len))
3424 return 0;
3426 mail = ident.mail_begin;
3427 maillen = ident.mail_end - ident.mail_begin;
3428 name = ident.name_begin;
3429 namelen = ident.name_end - ident.name_begin;
3431 if (map_user(mailmap, &mail, &maillen, &name, &namelen)) {
3432 struct strbuf namemail = STRBUF_INIT;
3434 strbuf_addf(&namemail, "%.*s <%.*s>",
3435 (int)namelen, name, (int)maillen, mail);
3437 strbuf_splice(buf, ident.name_begin - buf->buf,
3438 ident.mail_end - ident.name_begin + 1,
3439 namemail.buf, namemail.len);
3441 strbuf_release(&namemail);
3443 return 1;
3446 return 0;
3449 static int commit_match(struct commit *commit, struct rev_info *opt)
3451 int retval;
3452 const char *encoding;
3453 const char *message;
3454 struct strbuf buf = STRBUF_INIT;
3456 if (!opt->grep_filter.pattern_list && !opt->grep_filter.header_list)
3457 return 1;
3459 /* Prepend "fake" headers as needed */
3460 if (opt->grep_filter.use_reflog_filter) {
3461 strbuf_addstr(&buf, "reflog ");
3462 get_reflog_message(&buf, opt->reflog_info);
3463 strbuf_addch(&buf, '\n');
3467 * We grep in the user's output encoding, under the assumption that it
3468 * is the encoding they are most likely to write their grep pattern
3469 * for. In addition, it means we will match the "notes" encoding below,
3470 * so we will not end up with a buffer that has two different encodings
3471 * in it.
3473 encoding = get_log_output_encoding();
3474 message = logmsg_reencode(commit, NULL, encoding);
3476 /* Copy the commit to temporary if we are using "fake" headers */
3477 if (buf.len)
3478 strbuf_addstr(&buf, message);
3480 if (opt->grep_filter.header_list && opt->mailmap) {
3481 if (!buf.len)
3482 strbuf_addstr(&buf, message);
3484 commit_rewrite_person(&buf, "\nauthor ", opt->mailmap);
3485 commit_rewrite_person(&buf, "\ncommitter ", opt->mailmap);
3488 /* Append "fake" message parts as needed */
3489 if (opt->show_notes) {
3490 if (!buf.len)
3491 strbuf_addstr(&buf, message);
3492 format_display_notes(&commit->object.oid, &buf, encoding, 1);
3496 * Find either in the original commit message, or in the temporary.
3497 * Note that we cast away the constness of "message" here. It is
3498 * const because it may come from the cached commit buffer. That's OK,
3499 * because we know that it is modifiable heap memory, and that while
3500 * grep_buffer may modify it for speed, it will restore any
3501 * changes before returning.
3503 if (buf.len)
3504 retval = grep_buffer(&opt->grep_filter, buf.buf, buf.len);
3505 else
3506 retval = grep_buffer(&opt->grep_filter,
3507 (char *)message, strlen(message));
3508 strbuf_release(&buf);
3509 unuse_commit_buffer(commit, message);
3510 return opt->invert_grep ? !retval : retval;
3513 static inline int want_ancestry(const struct rev_info *revs)
3515 return (revs->rewrite_parents || revs->children.name);
3519 * Return a timestamp to be used for --since/--until comparisons for this
3520 * commit, based on the revision options.
3522 static timestamp_t comparison_date(const struct rev_info *revs,
3523 struct commit *commit)
3525 return revs->reflog_info ?
3526 get_reflog_timestamp(revs->reflog_info) :
3527 commit->date;
3530 enum commit_action get_commit_action(struct rev_info *revs, struct commit *commit)
3532 if (commit->object.flags & SHOWN)
3533 return commit_ignore;
3534 if (revs->unpacked && has_object_pack(&commit->object.oid))
3535 return commit_ignore;
3536 if (commit->object.flags & UNINTERESTING)
3537 return commit_ignore;
3538 if (revs->min_age != -1 &&
3539 comparison_date(revs, commit) > revs->min_age)
3540 return commit_ignore;
3541 if (revs->min_parents || (revs->max_parents >= 0)) {
3542 int n = commit_list_count(commit->parents);
3543 if ((n < revs->min_parents) ||
3544 ((revs->max_parents >= 0) && (n > revs->max_parents)))
3545 return commit_ignore;
3547 if (!commit_match(commit, revs))
3548 return commit_ignore;
3549 if (revs->prune && revs->dense) {
3550 /* Commit without changes? */
3551 if (commit->object.flags & TREESAME) {
3552 int n;
3553 struct commit_list *p;
3554 /* drop merges unless we want parenthood */
3555 if (!want_ancestry(revs))
3556 return commit_ignore;
3558 * If we want ancestry, then need to keep any merges
3559 * between relevant commits to tie together topology.
3560 * For consistency with TREESAME and simplification
3561 * use "relevant" here rather than just INTERESTING,
3562 * to treat bottom commit(s) as part of the topology.
3564 for (n = 0, p = commit->parents; p; p = p->next)
3565 if (relevant_commit(p->item))
3566 if (++n >= 2)
3567 return commit_show;
3568 return commit_ignore;
3571 return commit_show;
3574 define_commit_slab(saved_parents, struct commit_list *);
3576 #define EMPTY_PARENT_LIST ((struct commit_list *)-1)
3579 * You may only call save_parents() once per commit (this is checked
3580 * for non-root commits).
3582 static void save_parents(struct rev_info *revs, struct commit *commit)
3584 struct commit_list **pp;
3586 if (!revs->saved_parents_slab) {
3587 revs->saved_parents_slab = xmalloc(sizeof(struct saved_parents));
3588 init_saved_parents(revs->saved_parents_slab);
3591 pp = saved_parents_at(revs->saved_parents_slab, commit);
3594 * When walking with reflogs, we may visit the same commit
3595 * several times: once for each appearance in the reflog.
3597 * In this case, save_parents() will be called multiple times.
3598 * We want to keep only the first set of parents. We need to
3599 * store a sentinel value for an empty (i.e., NULL) parent
3600 * list to distinguish it from a not-yet-saved list, however.
3602 if (*pp)
3603 return;
3604 if (commit->parents)
3605 *pp = copy_commit_list(commit->parents);
3606 else
3607 *pp = EMPTY_PARENT_LIST;
3610 static void free_saved_parents(struct rev_info *revs)
3612 if (revs->saved_parents_slab)
3613 clear_saved_parents(revs->saved_parents_slab);
3616 struct commit_list *get_saved_parents(struct rev_info *revs, const struct commit *commit)
3618 struct commit_list *parents;
3620 if (!revs->saved_parents_slab)
3621 return commit->parents;
3623 parents = *saved_parents_at(revs->saved_parents_slab, commit);
3624 if (parents == EMPTY_PARENT_LIST)
3625 return NULL;
3626 return parents;
3629 enum commit_action simplify_commit(struct rev_info *revs, struct commit *commit)
3631 enum commit_action action = get_commit_action(revs, commit);
3633 if (action == commit_show &&
3634 revs->prune && revs->dense && want_ancestry(revs)) {
3636 * --full-diff on simplified parents is no good: it
3637 * will show spurious changes from the commits that
3638 * were elided. So we save the parents on the side
3639 * when --full-diff is in effect.
3641 if (revs->full_diff)
3642 save_parents(revs, commit);
3643 if (rewrite_parents(revs, commit, rewrite_one) < 0)
3644 return commit_error;
3646 return action;
3649 static void track_linear(struct rev_info *revs, struct commit *commit)
3651 if (revs->track_first_time) {
3652 revs->linear = 1;
3653 revs->track_first_time = 0;
3654 } else {
3655 struct commit_list *p;
3656 for (p = revs->previous_parents; p; p = p->next)
3657 if (p->item == NULL || /* first commit */
3658 oideq(&p->item->object.oid, &commit->object.oid))
3659 break;
3660 revs->linear = p != NULL;
3662 if (revs->reverse) {
3663 if (revs->linear)
3664 commit->object.flags |= TRACK_LINEAR;
3666 free_commit_list(revs->previous_parents);
3667 revs->previous_parents = copy_commit_list(commit->parents);
3670 static struct commit *get_revision_1(struct rev_info *revs)
3672 while (1) {
3673 struct commit *commit;
3675 if (revs->reflog_info)
3676 commit = next_reflog_entry(revs->reflog_info);
3677 else if (revs->topo_walk_info)
3678 commit = next_topo_commit(revs);
3679 else
3680 commit = pop_commit(&revs->commits);
3682 if (!commit)
3683 return NULL;
3685 if (revs->reflog_info)
3686 commit->object.flags &= ~(ADDED | SEEN | SHOWN);
3689 * If we haven't done the list limiting, we need to look at
3690 * the parents here. We also need to do the date-based limiting
3691 * that we'd otherwise have done in limit_list().
3693 if (!revs->limited) {
3694 if (revs->max_age != -1 &&
3695 comparison_date(revs, commit) < revs->max_age)
3696 continue;
3698 if (revs->reflog_info)
3699 try_to_simplify_commit(revs, commit);
3700 else if (revs->topo_walk_info)
3701 expand_topo_walk(revs, commit);
3702 else if (process_parents(revs, commit, &revs->commits, NULL) < 0) {
3703 if (!revs->ignore_missing_links)
3704 die("Failed to traverse parents of commit %s",
3705 oid_to_hex(&commit->object.oid));
3709 switch (simplify_commit(revs, commit)) {
3710 case commit_ignore:
3711 continue;
3712 case commit_error:
3713 die("Failed to simplify parents of commit %s",
3714 oid_to_hex(&commit->object.oid));
3715 default:
3716 if (revs->track_linear)
3717 track_linear(revs, commit);
3718 return commit;
3724 * Return true for entries that have not yet been shown. (This is an
3725 * object_array_each_func_t.)
3727 static int entry_unshown(struct object_array_entry *entry, void *cb_data_unused)
3729 return !(entry->item->flags & SHOWN);
3733 * If array is on the verge of a realloc, garbage-collect any entries
3734 * that have already been shown to try to free up some space.
3736 static void gc_boundary(struct object_array *array)
3738 if (array->nr == array->alloc)
3739 object_array_filter(array, entry_unshown, NULL);
3742 static void create_boundary_commit_list(struct rev_info *revs)
3744 unsigned i;
3745 struct commit *c;
3746 struct object_array *array = &revs->boundary_commits;
3747 struct object_array_entry *objects = array->objects;
3750 * If revs->commits is non-NULL at this point, an error occurred in
3751 * get_revision_1(). Ignore the error and continue printing the
3752 * boundary commits anyway. (This is what the code has always
3753 * done.)
3755 if (revs->commits) {
3756 free_commit_list(revs->commits);
3757 revs->commits = NULL;
3761 * Put all of the actual boundary commits from revs->boundary_commits
3762 * into revs->commits
3764 for (i = 0; i < array->nr; i++) {
3765 c = (struct commit *)(objects[i].item);
3766 if (!c)
3767 continue;
3768 if (!(c->object.flags & CHILD_SHOWN))
3769 continue;
3770 if (c->object.flags & (SHOWN | BOUNDARY))
3771 continue;
3772 c->object.flags |= BOUNDARY;
3773 commit_list_insert(c, &revs->commits);
3777 * If revs->topo_order is set, sort the boundary commits
3778 * in topological order
3780 sort_in_topological_order(&revs->commits, revs->sort_order);
3783 static struct commit *get_revision_internal(struct rev_info *revs)
3785 struct commit *c = NULL;
3786 struct commit_list *l;
3788 if (revs->boundary == 2) {
3790 * All of the normal commits have already been returned,
3791 * and we are now returning boundary commits.
3792 * create_boundary_commit_list() has populated
3793 * revs->commits with the remaining commits to return.
3795 c = pop_commit(&revs->commits);
3796 if (c)
3797 c->object.flags |= SHOWN;
3798 return c;
3802 * If our max_count counter has reached zero, then we are done. We
3803 * don't simply return NULL because we still might need to show
3804 * boundary commits. But we want to avoid calling get_revision_1, which
3805 * might do a considerable amount of work finding the next commit only
3806 * for us to throw it away.
3808 * If it is non-zero, then either we don't have a max_count at all
3809 * (-1), or it is still counting, in which case we decrement.
3811 if (revs->max_count) {
3812 c = get_revision_1(revs);
3813 if (c) {
3814 while (revs->skip_count > 0) {
3815 revs->skip_count--;
3816 c = get_revision_1(revs);
3817 if (!c)
3818 break;
3822 if (revs->max_count > 0)
3823 revs->max_count--;
3826 if (c)
3827 c->object.flags |= SHOWN;
3829 if (!revs->boundary)
3830 return c;
3832 if (!c) {
3834 * get_revision_1() runs out the commits, and
3835 * we are done computing the boundaries.
3836 * switch to boundary commits output mode.
3838 revs->boundary = 2;
3841 * Update revs->commits to contain the list of
3842 * boundary commits.
3844 create_boundary_commit_list(revs);
3846 return get_revision_internal(revs);
3850 * boundary commits are the commits that are parents of the
3851 * ones we got from get_revision_1() but they themselves are
3852 * not returned from get_revision_1(). Before returning
3853 * 'c', we need to mark its parents that they could be boundaries.
3856 for (l = c->parents; l; l = l->next) {
3857 struct object *p;
3858 p = &(l->item->object);
3859 if (p->flags & (CHILD_SHOWN | SHOWN))
3860 continue;
3861 p->flags |= CHILD_SHOWN;
3862 gc_boundary(&revs->boundary_commits);
3863 add_object_array(p, NULL, &revs->boundary_commits);
3866 return c;
3869 struct commit *get_revision(struct rev_info *revs)
3871 struct commit *c;
3872 struct commit_list *reversed;
3874 if (revs->reverse) {
3875 reversed = NULL;
3876 while ((c = get_revision_internal(revs)))
3877 commit_list_insert(c, &reversed);
3878 revs->commits = reversed;
3879 revs->reverse = 0;
3880 revs->reverse_output_stage = 1;
3883 if (revs->reverse_output_stage) {
3884 c = pop_commit(&revs->commits);
3885 if (revs->track_linear)
3886 revs->linear = !!(c && c->object.flags & TRACK_LINEAR);
3887 return c;
3890 c = get_revision_internal(revs);
3891 if (c && revs->graph)
3892 graph_update(revs->graph, c);
3893 if (!c) {
3894 free_saved_parents(revs);
3895 if (revs->previous_parents) {
3896 free_commit_list(revs->previous_parents);
3897 revs->previous_parents = NULL;
3900 return c;
3903 char *get_revision_mark(const struct rev_info *revs, const struct commit *commit)
3905 if (commit->object.flags & BOUNDARY)
3906 return "-";
3907 else if (commit->object.flags & UNINTERESTING)
3908 return "^";
3909 else if (commit->object.flags & PATCHSAME)
3910 return "=";
3911 else if (!revs || revs->left_right) {
3912 if (commit->object.flags & SYMMETRIC_LEFT)
3913 return "<";
3914 else
3915 return ">";
3916 } else if (revs->graph)
3917 return "*";
3918 else if (revs->cherry_mark)
3919 return "+";
3920 return "";
3923 void put_revision_mark(const struct rev_info *revs, const struct commit *commit)
3925 char *mark = get_revision_mark(revs, commit);
3926 if (!strlen(mark))
3927 return;
3928 fputs(mark, stdout);
3929 putchar(' ');