convert: mark unused parameter in null stream filter
[alt-git.git] / revision.c
blob36e31942ceec3c43c8dc690c273937aaf4d29b2c
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 "diff-merges.h"
9 #include "refs.h"
10 #include "revision.h"
11 #include "repository.h"
12 #include "graph.h"
13 #include "grep.h"
14 #include "reflog-walk.h"
15 #include "patch-ids.h"
16 #include "decorate.h"
17 #include "log-tree.h"
18 #include "string-list.h"
19 #include "line-log.h"
20 #include "mailmap.h"
21 #include "commit-slab.h"
22 #include "dir.h"
23 #include "cache-tree.h"
24 #include "bisect.h"
25 #include "packfile.h"
26 #include "worktree.h"
27 #include "strvec.h"
28 #include "commit-reach.h"
29 #include "commit-graph.h"
30 #include "prio-queue.h"
31 #include "hashmap.h"
32 #include "utf8.h"
33 #include "bloom.h"
34 #include "json-writer.h"
35 #include "list-objects-filter-options.h"
36 #include "resolve-undo.h"
38 volatile show_early_output_fn_t show_early_output;
40 static const char *term_bad;
41 static const char *term_good;
43 implement_shared_commit_slab(revision_sources, char *);
45 static inline int want_ancestry(const struct rev_info *revs);
47 void show_object_with_name(FILE *out, struct object *obj, const char *name)
49 fprintf(out, "%s ", oid_to_hex(&obj->oid));
51 * This "for (const char *p = ..." is made as a first step towards
52 * making use of such declarations elsewhere in our codebase. If
53 * it causes compilation problems on your platform, please report
54 * it to the Git mailing list at git@vger.kernel.org. In the meantime,
55 * adding -std=gnu99 to CFLAGS may help if you are with older GCC.
57 for (const char *p = name; *p && *p != '\n'; p++)
58 fputc(*p, out);
59 fputc('\n', out);
62 static void mark_blob_uninteresting(struct blob *blob)
64 if (!blob)
65 return;
66 if (blob->object.flags & UNINTERESTING)
67 return;
68 blob->object.flags |= UNINTERESTING;
71 static void mark_tree_contents_uninteresting(struct repository *r,
72 struct tree *tree)
74 struct tree_desc desc;
75 struct name_entry entry;
77 if (parse_tree_gently(tree, 1) < 0)
78 return;
80 init_tree_desc(&desc, tree->buffer, tree->size);
81 while (tree_entry(&desc, &entry)) {
82 switch (object_type(entry.mode)) {
83 case OBJ_TREE:
84 mark_tree_uninteresting(r, lookup_tree(r, &entry.oid));
85 break;
86 case OBJ_BLOB:
87 mark_blob_uninteresting(lookup_blob(r, &entry.oid));
88 break;
89 default:
90 /* Subproject commit - not in this repository */
91 break;
96 * We don't care about the tree any more
97 * after it has been marked uninteresting.
99 free_tree_buffer(tree);
102 void mark_tree_uninteresting(struct repository *r, struct tree *tree)
104 struct object *obj;
106 if (!tree)
107 return;
109 obj = &tree->object;
110 if (obj->flags & UNINTERESTING)
111 return;
112 obj->flags |= UNINTERESTING;
113 mark_tree_contents_uninteresting(r, tree);
116 struct path_and_oids_entry {
117 struct hashmap_entry ent;
118 char *path;
119 struct oidset trees;
122 static int path_and_oids_cmp(const void *hashmap_cmp_fn_data UNUSED,
123 const struct hashmap_entry *eptr,
124 const struct hashmap_entry *entry_or_key,
125 const void *keydata UNUSED)
127 const struct path_and_oids_entry *e1, *e2;
129 e1 = container_of(eptr, const struct path_and_oids_entry, ent);
130 e2 = container_of(entry_or_key, const struct path_and_oids_entry, ent);
132 return strcmp(e1->path, e2->path);
135 static void paths_and_oids_clear(struct hashmap *map)
137 struct hashmap_iter iter;
138 struct path_and_oids_entry *entry;
140 hashmap_for_each_entry(map, &iter, entry, ent /* member name */) {
141 oidset_clear(&entry->trees);
142 free(entry->path);
145 hashmap_clear_and_free(map, struct path_and_oids_entry, ent);
148 static void paths_and_oids_insert(struct hashmap *map,
149 const char *path,
150 const struct object_id *oid)
152 int hash = strhash(path);
153 struct path_and_oids_entry key;
154 struct path_and_oids_entry *entry;
156 hashmap_entry_init(&key.ent, hash);
158 /* use a shallow copy for the lookup */
159 key.path = (char *)path;
160 oidset_init(&key.trees, 0);
162 entry = hashmap_get_entry(map, &key, ent, NULL);
163 if (!entry) {
164 CALLOC_ARRAY(entry, 1);
165 hashmap_entry_init(&entry->ent, hash);
166 entry->path = xstrdup(key.path);
167 oidset_init(&entry->trees, 16);
168 hashmap_put(map, &entry->ent);
171 oidset_insert(&entry->trees, oid);
174 static void add_children_by_path(struct repository *r,
175 struct tree *tree,
176 struct hashmap *map)
178 struct tree_desc desc;
179 struct name_entry entry;
181 if (!tree)
182 return;
184 if (parse_tree_gently(tree, 1) < 0)
185 return;
187 init_tree_desc(&desc, tree->buffer, tree->size);
188 while (tree_entry(&desc, &entry)) {
189 switch (object_type(entry.mode)) {
190 case OBJ_TREE:
191 paths_and_oids_insert(map, entry.path, &entry.oid);
193 if (tree->object.flags & UNINTERESTING) {
194 struct tree *child = lookup_tree(r, &entry.oid);
195 if (child)
196 child->object.flags |= UNINTERESTING;
198 break;
199 case OBJ_BLOB:
200 if (tree->object.flags & UNINTERESTING) {
201 struct blob *child = lookup_blob(r, &entry.oid);
202 if (child)
203 child->object.flags |= UNINTERESTING;
205 break;
206 default:
207 /* Subproject commit - not in this repository */
208 break;
212 free_tree_buffer(tree);
215 void mark_trees_uninteresting_sparse(struct repository *r,
216 struct oidset *trees)
218 unsigned has_interesting = 0, has_uninteresting = 0;
219 struct hashmap map = HASHMAP_INIT(path_and_oids_cmp, NULL);
220 struct hashmap_iter map_iter;
221 struct path_and_oids_entry *entry;
222 struct object_id *oid;
223 struct oidset_iter iter;
225 oidset_iter_init(trees, &iter);
226 while ((!has_interesting || !has_uninteresting) &&
227 (oid = oidset_iter_next(&iter))) {
228 struct tree *tree = lookup_tree(r, oid);
230 if (!tree)
231 continue;
233 if (tree->object.flags & UNINTERESTING)
234 has_uninteresting = 1;
235 else
236 has_interesting = 1;
239 /* Do not walk unless we have both types of trees. */
240 if (!has_uninteresting || !has_interesting)
241 return;
243 oidset_iter_init(trees, &iter);
244 while ((oid = oidset_iter_next(&iter))) {
245 struct tree *tree = lookup_tree(r, oid);
246 add_children_by_path(r, tree, &map);
249 hashmap_for_each_entry(&map, &map_iter, entry, ent /* member name */)
250 mark_trees_uninteresting_sparse(r, &entry->trees);
252 paths_and_oids_clear(&map);
255 struct commit_stack {
256 struct commit **items;
257 size_t nr, alloc;
259 #define COMMIT_STACK_INIT { 0 }
261 static void commit_stack_push(struct commit_stack *stack, struct commit *commit)
263 ALLOC_GROW(stack->items, stack->nr + 1, stack->alloc);
264 stack->items[stack->nr++] = commit;
267 static struct commit *commit_stack_pop(struct commit_stack *stack)
269 return stack->nr ? stack->items[--stack->nr] : NULL;
272 static void commit_stack_clear(struct commit_stack *stack)
274 FREE_AND_NULL(stack->items);
275 stack->nr = stack->alloc = 0;
278 static void mark_one_parent_uninteresting(struct rev_info *revs, struct commit *commit,
279 struct commit_stack *pending)
281 struct commit_list *l;
283 if (commit->object.flags & UNINTERESTING)
284 return;
285 commit->object.flags |= UNINTERESTING;
288 * Normally we haven't parsed the parent
289 * yet, so we won't have a parent of a parent
290 * here. However, it may turn out that we've
291 * reached this commit some other way (where it
292 * wasn't uninteresting), in which case we need
293 * to mark its parents recursively too..
295 for (l = commit->parents; l; l = l->next) {
296 commit_stack_push(pending, l->item);
297 if (revs && revs->exclude_first_parent_only)
298 break;
302 void mark_parents_uninteresting(struct rev_info *revs, struct commit *commit)
304 struct commit_stack pending = COMMIT_STACK_INIT;
305 struct commit_list *l;
307 for (l = commit->parents; l; l = l->next) {
308 mark_one_parent_uninteresting(revs, l->item, &pending);
309 if (revs && revs->exclude_first_parent_only)
310 break;
313 while (pending.nr > 0)
314 mark_one_parent_uninteresting(revs, commit_stack_pop(&pending),
315 &pending);
317 commit_stack_clear(&pending);
320 static void add_pending_object_with_path(struct rev_info *revs,
321 struct object *obj,
322 const char *name, unsigned mode,
323 const char *path)
325 struct interpret_branch_name_options options = { 0 };
326 if (!obj)
327 return;
328 if (revs->no_walk && (obj->flags & UNINTERESTING))
329 revs->no_walk = 0;
330 if (revs->reflog_info && obj->type == OBJ_COMMIT) {
331 struct strbuf buf = STRBUF_INIT;
332 size_t namelen = strlen(name);
333 int len = interpret_branch_name(name, namelen, &buf, &options);
335 if (0 < len && len < namelen && buf.len)
336 strbuf_addstr(&buf, name + len);
337 add_reflog_for_walk(revs->reflog_info,
338 (struct commit *)obj,
339 buf.buf[0] ? buf.buf: name);
340 strbuf_release(&buf);
341 return; /* do not add the commit itself */
343 add_object_array_with_path(obj, name, &revs->pending, mode, path);
346 static void add_pending_object_with_mode(struct rev_info *revs,
347 struct object *obj,
348 const char *name, unsigned mode)
350 add_pending_object_with_path(revs, obj, name, mode, NULL);
353 void add_pending_object(struct rev_info *revs,
354 struct object *obj, const char *name)
356 add_pending_object_with_mode(revs, obj, name, S_IFINVALID);
359 void add_head_to_pending(struct rev_info *revs)
361 struct object_id oid;
362 struct object *obj;
363 if (get_oid("HEAD", &oid))
364 return;
365 obj = parse_object(revs->repo, &oid);
366 if (!obj)
367 return;
368 add_pending_object(revs, obj, "HEAD");
371 static struct object *get_reference(struct rev_info *revs, const char *name,
372 const struct object_id *oid,
373 unsigned int flags)
375 struct object *object;
377 object = parse_object_with_flags(revs->repo, oid,
378 revs->verify_objects ? 0 :
379 PARSE_OBJECT_SKIP_HASH_CHECK);
381 if (!object) {
382 if (revs->ignore_missing)
383 return object;
384 if (revs->exclude_promisor_objects && is_promisor_object(oid))
385 return NULL;
386 die("bad object %s", name);
388 object->flags |= flags;
389 return object;
392 void add_pending_oid(struct rev_info *revs, const char *name,
393 const struct object_id *oid, unsigned int flags)
395 struct object *object = get_reference(revs, name, oid, flags);
396 add_pending_object(revs, object, name);
399 static struct commit *handle_commit(struct rev_info *revs,
400 struct object_array_entry *entry)
402 struct object *object = entry->item;
403 const char *name = entry->name;
404 const char *path = entry->path;
405 unsigned int mode = entry->mode;
406 unsigned long flags = object->flags;
409 * Tag object? Look what it points to..
411 while (object->type == OBJ_TAG) {
412 struct tag *tag = (struct tag *) object;
413 if (revs->tag_objects && !(flags & UNINTERESTING))
414 add_pending_object(revs, object, tag->tag);
415 object = parse_object(revs->repo, get_tagged_oid(tag));
416 if (!object) {
417 if (revs->ignore_missing_links || (flags & UNINTERESTING))
418 return NULL;
419 if (revs->exclude_promisor_objects &&
420 is_promisor_object(&tag->tagged->oid))
421 return NULL;
422 die("bad object %s", oid_to_hex(&tag->tagged->oid));
424 object->flags |= flags;
426 * We'll handle the tagged object by looping or dropping
427 * through to the non-tag handlers below. Do not
428 * propagate path data from the tag's pending entry.
430 path = NULL;
431 mode = 0;
435 * Commit object? Just return it, we'll do all the complex
436 * reachability crud.
438 if (object->type == OBJ_COMMIT) {
439 struct commit *commit = (struct commit *)object;
441 if (repo_parse_commit(revs->repo, commit) < 0)
442 die("unable to parse commit %s", name);
443 if (flags & UNINTERESTING) {
444 mark_parents_uninteresting(revs, commit);
446 if (!revs->topo_order || !generation_numbers_enabled(the_repository))
447 revs->limited = 1;
449 if (revs->sources) {
450 char **slot = revision_sources_at(revs->sources, commit);
452 if (!*slot)
453 *slot = xstrdup(name);
455 return commit;
459 * Tree object? Either mark it uninteresting, or add it
460 * to the list of objects to look at later..
462 if (object->type == OBJ_TREE) {
463 struct tree *tree = (struct tree *)object;
464 if (!revs->tree_objects)
465 return NULL;
466 if (flags & UNINTERESTING) {
467 mark_tree_contents_uninteresting(revs->repo, tree);
468 return NULL;
470 add_pending_object_with_path(revs, object, name, mode, path);
471 return NULL;
475 * Blob object? You know the drill by now..
477 if (object->type == OBJ_BLOB) {
478 if (!revs->blob_objects)
479 return NULL;
480 if (flags & UNINTERESTING)
481 return NULL;
482 add_pending_object_with_path(revs, object, name, mode, path);
483 return NULL;
485 die("%s is unknown object", name);
488 static int everybody_uninteresting(struct commit_list *orig,
489 struct commit **interesting_cache)
491 struct commit_list *list = orig;
493 if (*interesting_cache) {
494 struct commit *commit = *interesting_cache;
495 if (!(commit->object.flags & UNINTERESTING))
496 return 0;
499 while (list) {
500 struct commit *commit = list->item;
501 list = list->next;
502 if (commit->object.flags & UNINTERESTING)
503 continue;
505 *interesting_cache = commit;
506 return 0;
508 return 1;
512 * A definition of "relevant" commit that we can use to simplify limited graphs
513 * by eliminating side branches.
515 * A "relevant" commit is one that is !UNINTERESTING (ie we are including it
516 * in our list), or that is a specified BOTTOM commit. Then after computing
517 * a limited list, during processing we can generally ignore boundary merges
518 * coming from outside the graph, (ie from irrelevant parents), and treat
519 * those merges as if they were single-parent. TREESAME is defined to consider
520 * only relevant parents, if any. If we are TREESAME to our on-graph parents,
521 * we don't care if we were !TREESAME to non-graph parents.
523 * Treating bottom commits as relevant ensures that a limited graph's
524 * connection to the actual bottom commit is not viewed as a side branch, but
525 * treated as part of the graph. For example:
527 * ....Z...A---X---o---o---B
528 * . /
529 * W---Y
531 * When computing "A..B", the A-X connection is at least as important as
532 * Y-X, despite A being flagged UNINTERESTING.
534 * And when computing --ancestry-path "A..B", the A-X connection is more
535 * important than Y-X, despite both A and Y being flagged UNINTERESTING.
537 static inline int relevant_commit(struct commit *commit)
539 return (commit->object.flags & (UNINTERESTING | BOTTOM)) != UNINTERESTING;
543 * Return a single relevant commit from a parent list. If we are a TREESAME
544 * commit, and this selects one of our parents, then we can safely simplify to
545 * that parent.
547 static struct commit *one_relevant_parent(const struct rev_info *revs,
548 struct commit_list *orig)
550 struct commit_list *list = orig;
551 struct commit *relevant = NULL;
553 if (!orig)
554 return NULL;
557 * For 1-parent commits, or if first-parent-only, then return that
558 * first parent (even if not "relevant" by the above definition).
559 * TREESAME will have been set purely on that parent.
561 if (revs->first_parent_only || !orig->next)
562 return orig->item;
565 * For multi-parent commits, identify a sole relevant parent, if any.
566 * If we have only one relevant parent, then TREESAME will be set purely
567 * with regard to that parent, and we can simplify accordingly.
569 * If we have more than one relevant parent, or no relevant parents
570 * (and multiple irrelevant ones), then we can't select a parent here
571 * and return NULL.
573 while (list) {
574 struct commit *commit = list->item;
575 list = list->next;
576 if (relevant_commit(commit)) {
577 if (relevant)
578 return NULL;
579 relevant = commit;
582 return relevant;
586 * The goal is to get REV_TREE_NEW as the result only if the
587 * diff consists of all '+' (and no other changes), REV_TREE_OLD
588 * if the whole diff is removal of old data, and otherwise
589 * REV_TREE_DIFFERENT (of course if the trees are the same we
590 * want REV_TREE_SAME).
592 * The only time we care about the distinction is when
593 * remove_empty_trees is in effect, in which case we care only about
594 * whether the whole change is REV_TREE_NEW, or if there's another type
595 * of change. Which means we can stop the diff early in either of these
596 * cases:
598 * 1. We're not using remove_empty_trees at all.
600 * 2. We saw anything except REV_TREE_NEW.
602 #define REV_TREE_SAME 0
603 #define REV_TREE_NEW 1 /* Only new files */
604 #define REV_TREE_OLD 2 /* Only files removed */
605 #define REV_TREE_DIFFERENT 3 /* Mixed changes */
606 static int tree_difference = REV_TREE_SAME;
608 static void file_add_remove(struct diff_options *options,
609 int addremove, unsigned mode,
610 const struct object_id *oid,
611 int oid_valid,
612 const char *fullpath, unsigned dirty_submodule)
614 int diff = addremove == '+' ? REV_TREE_NEW : REV_TREE_OLD;
615 struct rev_info *revs = options->change_fn_data;
617 tree_difference |= diff;
618 if (!revs->remove_empty_trees || tree_difference != REV_TREE_NEW)
619 options->flags.has_changes = 1;
622 static void file_change(struct diff_options *options,
623 unsigned old_mode, unsigned new_mode,
624 const struct object_id *old_oid,
625 const struct object_id *new_oid,
626 int old_oid_valid, int new_oid_valid,
627 const char *fullpath,
628 unsigned old_dirty_submodule, unsigned new_dirty_submodule)
630 tree_difference = REV_TREE_DIFFERENT;
631 options->flags.has_changes = 1;
634 static int bloom_filter_atexit_registered;
635 static unsigned int count_bloom_filter_maybe;
636 static unsigned int count_bloom_filter_definitely_not;
637 static unsigned int count_bloom_filter_false_positive;
638 static unsigned int count_bloom_filter_not_present;
640 static void trace2_bloom_filter_statistics_atexit(void)
642 struct json_writer jw = JSON_WRITER_INIT;
644 jw_object_begin(&jw, 0);
645 jw_object_intmax(&jw, "filter_not_present", count_bloom_filter_not_present);
646 jw_object_intmax(&jw, "maybe", count_bloom_filter_maybe);
647 jw_object_intmax(&jw, "definitely_not", count_bloom_filter_definitely_not);
648 jw_object_intmax(&jw, "false_positive", count_bloom_filter_false_positive);
649 jw_end(&jw);
651 trace2_data_json("bloom", the_repository, "statistics", &jw);
653 jw_release(&jw);
656 static int forbid_bloom_filters(struct pathspec *spec)
658 if (spec->has_wildcard)
659 return 1;
660 if (spec->nr > 1)
661 return 1;
662 if (spec->magic & ~PATHSPEC_LITERAL)
663 return 1;
664 if (spec->nr && (spec->items[0].magic & ~PATHSPEC_LITERAL))
665 return 1;
667 return 0;
670 static void prepare_to_use_bloom_filter(struct rev_info *revs)
672 struct pathspec_item *pi;
673 char *path_alloc = NULL;
674 const char *path, *p;
675 size_t len;
676 int path_component_nr = 1;
678 if (!revs->commits)
679 return;
681 if (forbid_bloom_filters(&revs->prune_data))
682 return;
684 repo_parse_commit(revs->repo, revs->commits->item);
686 revs->bloom_filter_settings = get_bloom_filter_settings(revs->repo);
687 if (!revs->bloom_filter_settings)
688 return;
690 if (!revs->pruning.pathspec.nr)
691 return;
693 pi = &revs->pruning.pathspec.items[0];
695 /* remove single trailing slash from path, if needed */
696 if (pi->len > 0 && pi->match[pi->len - 1] == '/') {
697 path_alloc = xmemdupz(pi->match, pi->len - 1);
698 path = path_alloc;
699 } else
700 path = pi->match;
702 len = strlen(path);
703 if (!len) {
704 revs->bloom_filter_settings = NULL;
705 free(path_alloc);
706 return;
709 p = path;
710 while (*p) {
712 * At this point, the path is normalized to use Unix-style
713 * path separators. This is required due to how the
714 * changed-path Bloom filters store the paths.
716 if (*p == '/')
717 path_component_nr++;
718 p++;
721 revs->bloom_keys_nr = path_component_nr;
722 ALLOC_ARRAY(revs->bloom_keys, revs->bloom_keys_nr);
724 fill_bloom_key(path, len, &revs->bloom_keys[0],
725 revs->bloom_filter_settings);
726 path_component_nr = 1;
728 p = path + len - 1;
729 while (p > path) {
730 if (*p == '/')
731 fill_bloom_key(path, p - path,
732 &revs->bloom_keys[path_component_nr++],
733 revs->bloom_filter_settings);
734 p--;
737 if (trace2_is_enabled() && !bloom_filter_atexit_registered) {
738 atexit(trace2_bloom_filter_statistics_atexit);
739 bloom_filter_atexit_registered = 1;
742 free(path_alloc);
745 static int check_maybe_different_in_bloom_filter(struct rev_info *revs,
746 struct commit *commit)
748 struct bloom_filter *filter;
749 int result = 1, j;
751 if (!revs->repo->objects->commit_graph)
752 return -1;
754 if (commit_graph_generation(commit) == GENERATION_NUMBER_INFINITY)
755 return -1;
757 filter = get_bloom_filter(revs->repo, commit);
759 if (!filter) {
760 count_bloom_filter_not_present++;
761 return -1;
764 for (j = 0; result && j < revs->bloom_keys_nr; j++) {
765 result = bloom_filter_contains(filter,
766 &revs->bloom_keys[j],
767 revs->bloom_filter_settings);
770 if (result)
771 count_bloom_filter_maybe++;
772 else
773 count_bloom_filter_definitely_not++;
775 return result;
778 static int rev_compare_tree(struct rev_info *revs,
779 struct commit *parent, struct commit *commit, int nth_parent)
781 struct tree *t1 = get_commit_tree(parent);
782 struct tree *t2 = get_commit_tree(commit);
783 int bloom_ret = 1;
785 if (!t1)
786 return REV_TREE_NEW;
787 if (!t2)
788 return REV_TREE_OLD;
790 if (revs->simplify_by_decoration) {
792 * If we are simplifying by decoration, then the commit
793 * is worth showing if it has a tag pointing at it.
795 if (get_name_decoration(&commit->object))
796 return REV_TREE_DIFFERENT;
798 * A commit that is not pointed by a tag is uninteresting
799 * if we are not limited by path. This means that you will
800 * see the usual "commits that touch the paths" plus any
801 * tagged commit by specifying both --simplify-by-decoration
802 * and pathspec.
804 if (!revs->prune_data.nr)
805 return REV_TREE_SAME;
808 if (revs->bloom_keys_nr && !nth_parent) {
809 bloom_ret = check_maybe_different_in_bloom_filter(revs, commit);
811 if (bloom_ret == 0)
812 return REV_TREE_SAME;
815 tree_difference = REV_TREE_SAME;
816 revs->pruning.flags.has_changes = 0;
817 diff_tree_oid(&t1->object.oid, &t2->object.oid, "", &revs->pruning);
819 if (!nth_parent)
820 if (bloom_ret == 1 && tree_difference == REV_TREE_SAME)
821 count_bloom_filter_false_positive++;
823 return tree_difference;
826 static int rev_same_tree_as_empty(struct rev_info *revs, struct commit *commit)
828 struct tree *t1 = get_commit_tree(commit);
830 if (!t1)
831 return 0;
833 tree_difference = REV_TREE_SAME;
834 revs->pruning.flags.has_changes = 0;
835 diff_tree_oid(NULL, &t1->object.oid, "", &revs->pruning);
837 return tree_difference == REV_TREE_SAME;
840 struct treesame_state {
841 unsigned int nparents;
842 unsigned char treesame[FLEX_ARRAY];
845 static struct treesame_state *initialise_treesame(struct rev_info *revs, struct commit *commit)
847 unsigned n = commit_list_count(commit->parents);
848 struct treesame_state *st = xcalloc(1, st_add(sizeof(*st), n));
849 st->nparents = n;
850 add_decoration(&revs->treesame, &commit->object, st);
851 return st;
855 * Must be called immediately after removing the nth_parent from a commit's
856 * parent list, if we are maintaining the per-parent treesame[] decoration.
857 * This does not recalculate the master TREESAME flag - update_treesame()
858 * should be called to update it after a sequence of treesame[] modifications
859 * that may have affected it.
861 static int compact_treesame(struct rev_info *revs, struct commit *commit, unsigned nth_parent)
863 struct treesame_state *st;
864 int old_same;
866 if (!commit->parents) {
868 * Have just removed the only parent from a non-merge.
869 * Different handling, as we lack decoration.
871 if (nth_parent != 0)
872 die("compact_treesame %u", nth_parent);
873 old_same = !!(commit->object.flags & TREESAME);
874 if (rev_same_tree_as_empty(revs, commit))
875 commit->object.flags |= TREESAME;
876 else
877 commit->object.flags &= ~TREESAME;
878 return old_same;
881 st = lookup_decoration(&revs->treesame, &commit->object);
882 if (!st || nth_parent >= st->nparents)
883 die("compact_treesame %u", nth_parent);
885 old_same = st->treesame[nth_parent];
886 memmove(st->treesame + nth_parent,
887 st->treesame + nth_parent + 1,
888 st->nparents - nth_parent - 1);
891 * If we've just become a non-merge commit, update TREESAME
892 * immediately, and remove the no-longer-needed decoration.
893 * If still a merge, defer update until update_treesame().
895 if (--st->nparents == 1) {
896 if (commit->parents->next)
897 die("compact_treesame parents mismatch");
898 if (st->treesame[0] && revs->dense)
899 commit->object.flags |= TREESAME;
900 else
901 commit->object.flags &= ~TREESAME;
902 free(add_decoration(&revs->treesame, &commit->object, NULL));
905 return old_same;
908 static unsigned update_treesame(struct rev_info *revs, struct commit *commit)
910 if (commit->parents && commit->parents->next) {
911 unsigned n;
912 struct treesame_state *st;
913 struct commit_list *p;
914 unsigned relevant_parents;
915 unsigned relevant_change, irrelevant_change;
917 st = lookup_decoration(&revs->treesame, &commit->object);
918 if (!st)
919 die("update_treesame %s", oid_to_hex(&commit->object.oid));
920 relevant_parents = 0;
921 relevant_change = irrelevant_change = 0;
922 for (p = commit->parents, n = 0; p; n++, p = p->next) {
923 if (relevant_commit(p->item)) {
924 relevant_change |= !st->treesame[n];
925 relevant_parents++;
926 } else
927 irrelevant_change |= !st->treesame[n];
929 if (relevant_parents ? relevant_change : irrelevant_change)
930 commit->object.flags &= ~TREESAME;
931 else
932 commit->object.flags |= TREESAME;
935 return commit->object.flags & TREESAME;
938 static inline int limiting_can_increase_treesame(const struct rev_info *revs)
941 * TREESAME is irrelevant unless prune && dense;
942 * if simplify_history is set, we can't have a mixture of TREESAME and
943 * !TREESAME INTERESTING parents (and we don't have treesame[]
944 * decoration anyway);
945 * if first_parent_only is set, then the TREESAME flag is locked
946 * against the first parent (and again we lack treesame[] decoration).
948 return revs->prune && revs->dense &&
949 !revs->simplify_history &&
950 !revs->first_parent_only;
953 static void try_to_simplify_commit(struct rev_info *revs, struct commit *commit)
955 struct commit_list **pp, *parent;
956 struct treesame_state *ts = NULL;
957 int relevant_change = 0, irrelevant_change = 0;
958 int relevant_parents, nth_parent;
961 * If we don't do pruning, everything is interesting
963 if (!revs->prune)
964 return;
966 if (!get_commit_tree(commit))
967 return;
969 if (!commit->parents) {
970 if (rev_same_tree_as_empty(revs, commit))
971 commit->object.flags |= TREESAME;
972 return;
976 * Normal non-merge commit? If we don't want to make the
977 * history dense, we consider it always to be a change..
979 if (!revs->dense && !commit->parents->next)
980 return;
982 for (pp = &commit->parents, nth_parent = 0, relevant_parents = 0;
983 (parent = *pp) != NULL;
984 pp = &parent->next, nth_parent++) {
985 struct commit *p = parent->item;
986 if (relevant_commit(p))
987 relevant_parents++;
989 if (nth_parent == 1) {
991 * This our second loop iteration - so we now know
992 * we're dealing with a merge.
994 * Do not compare with later parents when we care only about
995 * the first parent chain, in order to avoid derailing the
996 * traversal to follow a side branch that brought everything
997 * in the path we are limited to by the pathspec.
999 if (revs->first_parent_only)
1000 break;
1002 * If this will remain a potentially-simplifiable
1003 * merge, remember per-parent treesame if needed.
1004 * Initialise the array with the comparison from our
1005 * first iteration.
1007 if (revs->treesame.name &&
1008 !revs->simplify_history &&
1009 !(commit->object.flags & UNINTERESTING)) {
1010 ts = initialise_treesame(revs, commit);
1011 if (!(irrelevant_change || relevant_change))
1012 ts->treesame[0] = 1;
1015 if (repo_parse_commit(revs->repo, p) < 0)
1016 die("cannot simplify commit %s (because of %s)",
1017 oid_to_hex(&commit->object.oid),
1018 oid_to_hex(&p->object.oid));
1019 switch (rev_compare_tree(revs, p, commit, nth_parent)) {
1020 case REV_TREE_SAME:
1021 if (!revs->simplify_history || !relevant_commit(p)) {
1022 /* Even if a merge with an uninteresting
1023 * side branch brought the entire change
1024 * we are interested in, we do not want
1025 * to lose the other branches of this
1026 * merge, so we just keep going.
1028 if (ts)
1029 ts->treesame[nth_parent] = 1;
1030 continue;
1032 parent->next = NULL;
1033 commit->parents = parent;
1036 * A merge commit is a "diversion" if it is not
1037 * TREESAME to its first parent but is TREESAME
1038 * to a later parent. In the simplified history,
1039 * we "divert" the history walk to the later
1040 * parent. These commits are shown when "show_pulls"
1041 * is enabled, so do not mark the object as
1042 * TREESAME here.
1044 if (!revs->show_pulls || !nth_parent)
1045 commit->object.flags |= TREESAME;
1047 return;
1049 case REV_TREE_NEW:
1050 if (revs->remove_empty_trees &&
1051 rev_same_tree_as_empty(revs, p)) {
1052 /* We are adding all the specified
1053 * paths from this parent, so the
1054 * history beyond this parent is not
1055 * interesting. Remove its parents
1056 * (they are grandparents for us).
1057 * IOW, we pretend this parent is a
1058 * "root" commit.
1060 if (repo_parse_commit(revs->repo, p) < 0)
1061 die("cannot simplify commit %s (invalid %s)",
1062 oid_to_hex(&commit->object.oid),
1063 oid_to_hex(&p->object.oid));
1064 p->parents = NULL;
1066 /* fallthrough */
1067 case REV_TREE_OLD:
1068 case REV_TREE_DIFFERENT:
1069 if (relevant_commit(p))
1070 relevant_change = 1;
1071 else
1072 irrelevant_change = 1;
1074 if (!nth_parent)
1075 commit->object.flags |= PULL_MERGE;
1077 continue;
1079 die("bad tree compare for commit %s", oid_to_hex(&commit->object.oid));
1083 * TREESAME is straightforward for single-parent commits. For merge
1084 * commits, it is most useful to define it so that "irrelevant"
1085 * parents cannot make us !TREESAME - if we have any relevant
1086 * parents, then we only consider TREESAMEness with respect to them,
1087 * allowing irrelevant merges from uninteresting branches to be
1088 * simplified away. Only if we have only irrelevant parents do we
1089 * base TREESAME on them. Note that this logic is replicated in
1090 * update_treesame, which should be kept in sync.
1092 if (relevant_parents ? !relevant_change : !irrelevant_change)
1093 commit->object.flags |= TREESAME;
1096 static int process_parents(struct rev_info *revs, struct commit *commit,
1097 struct commit_list **list, struct prio_queue *queue)
1099 struct commit_list *parent = commit->parents;
1100 unsigned pass_flags;
1102 if (commit->object.flags & ADDED)
1103 return 0;
1104 commit->object.flags |= ADDED;
1106 if (revs->include_check &&
1107 !revs->include_check(commit, revs->include_check_data))
1108 return 0;
1111 * If the commit is uninteresting, don't try to
1112 * prune parents - we want the maximal uninteresting
1113 * set.
1115 * Normally we haven't parsed the parent
1116 * yet, so we won't have a parent of a parent
1117 * here. However, it may turn out that we've
1118 * reached this commit some other way (where it
1119 * wasn't uninteresting), in which case we need
1120 * to mark its parents recursively too..
1122 if (commit->object.flags & UNINTERESTING) {
1123 while (parent) {
1124 struct commit *p = parent->item;
1125 parent = parent->next;
1126 if (p)
1127 p->object.flags |= UNINTERESTING;
1128 if (repo_parse_commit_gently(revs->repo, p, 1) < 0)
1129 continue;
1130 if (p->parents)
1131 mark_parents_uninteresting(revs, p);
1132 if (p->object.flags & SEEN)
1133 continue;
1134 p->object.flags |= (SEEN | NOT_USER_GIVEN);
1135 if (list)
1136 commit_list_insert_by_date(p, list);
1137 if (queue)
1138 prio_queue_put(queue, p);
1139 if (revs->exclude_first_parent_only)
1140 break;
1142 return 0;
1146 * Ok, the commit wasn't uninteresting. Try to
1147 * simplify the commit history and find the parent
1148 * that has no differences in the path set if one exists.
1150 try_to_simplify_commit(revs, commit);
1152 if (revs->no_walk)
1153 return 0;
1155 pass_flags = (commit->object.flags & (SYMMETRIC_LEFT | ANCESTRY_PATH));
1157 for (parent = commit->parents; parent; parent = parent->next) {
1158 struct commit *p = parent->item;
1159 int gently = revs->ignore_missing_links ||
1160 revs->exclude_promisor_objects;
1161 if (repo_parse_commit_gently(revs->repo, p, gently) < 0) {
1162 if (revs->exclude_promisor_objects &&
1163 is_promisor_object(&p->object.oid)) {
1164 if (revs->first_parent_only)
1165 break;
1166 continue;
1168 return -1;
1170 if (revs->sources) {
1171 char **slot = revision_sources_at(revs->sources, p);
1173 if (!*slot)
1174 *slot = *revision_sources_at(revs->sources, commit);
1176 p->object.flags |= pass_flags;
1177 if (!(p->object.flags & SEEN)) {
1178 p->object.flags |= (SEEN | NOT_USER_GIVEN);
1179 if (list)
1180 commit_list_insert_by_date(p, list);
1181 if (queue)
1182 prio_queue_put(queue, p);
1184 if (revs->first_parent_only)
1185 break;
1187 return 0;
1190 static void cherry_pick_list(struct commit_list *list, struct rev_info *revs)
1192 struct commit_list *p;
1193 int left_count = 0, right_count = 0;
1194 int left_first;
1195 struct patch_ids ids;
1196 unsigned cherry_flag;
1198 /* First count the commits on the left and on the right */
1199 for (p = list; p; p = p->next) {
1200 struct commit *commit = p->item;
1201 unsigned flags = commit->object.flags;
1202 if (flags & BOUNDARY)
1204 else if (flags & SYMMETRIC_LEFT)
1205 left_count++;
1206 else
1207 right_count++;
1210 if (!left_count || !right_count)
1211 return;
1213 left_first = left_count < right_count;
1214 init_patch_ids(revs->repo, &ids);
1215 ids.diffopts.pathspec = revs->diffopt.pathspec;
1217 /* Compute patch-ids for one side */
1218 for (p = list; p; p = p->next) {
1219 struct commit *commit = p->item;
1220 unsigned flags = commit->object.flags;
1222 if (flags & BOUNDARY)
1223 continue;
1225 * If we have fewer left, left_first is set and we omit
1226 * commits on the right branch in this loop. If we have
1227 * fewer right, we skip the left ones.
1229 if (left_first != !!(flags & SYMMETRIC_LEFT))
1230 continue;
1231 add_commit_patch_id(commit, &ids);
1234 /* either cherry_mark or cherry_pick are true */
1235 cherry_flag = revs->cherry_mark ? PATCHSAME : SHOWN;
1237 /* Check the other side */
1238 for (p = list; p; p = p->next) {
1239 struct commit *commit = p->item;
1240 struct patch_id *id;
1241 unsigned flags = commit->object.flags;
1243 if (flags & BOUNDARY)
1244 continue;
1246 * If we have fewer left, left_first is set and we omit
1247 * commits on the left branch in this loop.
1249 if (left_first == !!(flags & SYMMETRIC_LEFT))
1250 continue;
1253 * Have we seen the same patch id?
1255 id = patch_id_iter_first(commit, &ids);
1256 if (!id)
1257 continue;
1259 commit->object.flags |= cherry_flag;
1260 do {
1261 id->commit->object.flags |= cherry_flag;
1262 } while ((id = patch_id_iter_next(id, &ids)));
1265 free_patch_ids(&ids);
1268 /* How many extra uninteresting commits we want to see.. */
1269 #define SLOP 5
1271 static int still_interesting(struct commit_list *src, timestamp_t date, int slop,
1272 struct commit **interesting_cache)
1275 * No source list at all? We're definitely done..
1277 if (!src)
1278 return 0;
1281 * Does the destination list contain entries with a date
1282 * before the source list? Definitely _not_ done.
1284 if (date <= src->item->date)
1285 return SLOP;
1288 * Does the source list still have interesting commits in
1289 * it? Definitely not done..
1291 if (!everybody_uninteresting(src, interesting_cache))
1292 return SLOP;
1294 /* Ok, we're closing in.. */
1295 return slop-1;
1299 * "rev-list --ancestry-path=C_0 [--ancestry-path=C_1 ...] A..B"
1300 * computes commits that are ancestors of B but not ancestors of A but
1301 * further limits the result to those that have any of C in their
1302 * ancestry path (i.e. are either ancestors of any of C, descendants
1303 * of any of C, or are any of C). If --ancestry-path is specified with
1304 * no commit, we use all bottom commits for C.
1306 * Before this function is called, ancestors of C will have already
1307 * been marked with ANCESTRY_PATH previously.
1309 * This takes the list of bottom commits and the result of "A..B"
1310 * without --ancestry-path, and limits the latter further to the ones
1311 * that have any of C in their ancestry path. Since the ancestors of C
1312 * have already been marked (a prerequisite of this function), we just
1313 * need to mark the descendants, then exclude any commit that does not
1314 * have any of these marks.
1316 static void limit_to_ancestry(struct commit_list *bottoms, struct commit_list *list)
1318 struct commit_list *p;
1319 struct commit_list *rlist = NULL;
1320 int made_progress;
1323 * Reverse the list so that it will be likely that we would
1324 * process parents before children.
1326 for (p = list; p; p = p->next)
1327 commit_list_insert(p->item, &rlist);
1329 for (p = bottoms; p; p = p->next)
1330 p->item->object.flags |= TMP_MARK;
1333 * Mark the ones that can reach bottom commits in "list",
1334 * in a bottom-up fashion.
1336 do {
1337 made_progress = 0;
1338 for (p = rlist; p; p = p->next) {
1339 struct commit *c = p->item;
1340 struct commit_list *parents;
1341 if (c->object.flags & (TMP_MARK | UNINTERESTING))
1342 continue;
1343 for (parents = c->parents;
1344 parents;
1345 parents = parents->next) {
1346 if (!(parents->item->object.flags & TMP_MARK))
1347 continue;
1348 c->object.flags |= TMP_MARK;
1349 made_progress = 1;
1350 break;
1353 } while (made_progress);
1356 * NEEDSWORK: decide if we want to remove parents that are
1357 * not marked with TMP_MARK from commit->parents for commits
1358 * in the resulting list. We may not want to do that, though.
1362 * The ones that are not marked with either TMP_MARK or
1363 * ANCESTRY_PATH are uninteresting
1365 for (p = list; p; p = p->next) {
1366 struct commit *c = p->item;
1367 if (c->object.flags & (TMP_MARK | ANCESTRY_PATH))
1368 continue;
1369 c->object.flags |= UNINTERESTING;
1372 /* We are done with TMP_MARK and ANCESTRY_PATH */
1373 for (p = list; p; p = p->next)
1374 p->item->object.flags &= ~(TMP_MARK | ANCESTRY_PATH);
1375 for (p = bottoms; p; p = p->next)
1376 p->item->object.flags &= ~(TMP_MARK | ANCESTRY_PATH);
1377 free_commit_list(rlist);
1381 * Before walking the history, add the set of "negative" refs the
1382 * caller has asked to exclude to the bottom list.
1384 * This is used to compute "rev-list --ancestry-path A..B", as we need
1385 * to filter the result of "A..B" further to the ones that can actually
1386 * reach A.
1388 static void collect_bottom_commits(struct commit_list *list,
1389 struct commit_list **bottom)
1391 struct commit_list *elem;
1392 for (elem = list; elem; elem = elem->next)
1393 if (elem->item->object.flags & BOTTOM)
1394 commit_list_insert(elem->item, bottom);
1397 /* Assumes either left_only or right_only is set */
1398 static void limit_left_right(struct commit_list *list, struct rev_info *revs)
1400 struct commit_list *p;
1402 for (p = list; p; p = p->next) {
1403 struct commit *commit = p->item;
1405 if (revs->right_only) {
1406 if (commit->object.flags & SYMMETRIC_LEFT)
1407 commit->object.flags |= SHOWN;
1408 } else /* revs->left_only is set */
1409 if (!(commit->object.flags & SYMMETRIC_LEFT))
1410 commit->object.flags |= SHOWN;
1414 static int limit_list(struct rev_info *revs)
1416 int slop = SLOP;
1417 timestamp_t date = TIME_MAX;
1418 struct commit_list *original_list = revs->commits;
1419 struct commit_list *newlist = NULL;
1420 struct commit_list **p = &newlist;
1421 struct commit *interesting_cache = NULL;
1423 if (revs->ancestry_path_implicit_bottoms) {
1424 collect_bottom_commits(original_list,
1425 &revs->ancestry_path_bottoms);
1426 if (!revs->ancestry_path_bottoms)
1427 die("--ancestry-path given but there are no bottom commits");
1430 while (original_list) {
1431 struct commit *commit = pop_commit(&original_list);
1432 struct object *obj = &commit->object;
1433 show_early_output_fn_t show;
1435 if (commit == interesting_cache)
1436 interesting_cache = NULL;
1438 if (revs->max_age != -1 && (commit->date < revs->max_age))
1439 obj->flags |= UNINTERESTING;
1440 if (process_parents(revs, commit, &original_list, NULL) < 0)
1441 return -1;
1442 if (obj->flags & UNINTERESTING) {
1443 mark_parents_uninteresting(revs, commit);
1444 slop = still_interesting(original_list, date, slop, &interesting_cache);
1445 if (slop)
1446 continue;
1447 break;
1449 if (revs->min_age != -1 && (commit->date > revs->min_age) &&
1450 !revs->line_level_traverse)
1451 continue;
1452 if (revs->max_age_as_filter != -1 &&
1453 (commit->date < revs->max_age_as_filter) && !revs->line_level_traverse)
1454 continue;
1455 date = commit->date;
1456 p = &commit_list_insert(commit, p)->next;
1458 show = show_early_output;
1459 if (!show)
1460 continue;
1462 show(revs, newlist);
1463 show_early_output = NULL;
1465 if (revs->cherry_pick || revs->cherry_mark)
1466 cherry_pick_list(newlist, revs);
1468 if (revs->left_only || revs->right_only)
1469 limit_left_right(newlist, revs);
1471 if (revs->ancestry_path)
1472 limit_to_ancestry(revs->ancestry_path_bottoms, newlist);
1475 * Check if any commits have become TREESAME by some of their parents
1476 * becoming UNINTERESTING.
1478 if (limiting_can_increase_treesame(revs)) {
1479 struct commit_list *list = NULL;
1480 for (list = newlist; list; list = list->next) {
1481 struct commit *c = list->item;
1482 if (c->object.flags & (UNINTERESTING | TREESAME))
1483 continue;
1484 update_treesame(revs, c);
1488 free_commit_list(original_list);
1489 revs->commits = newlist;
1490 return 0;
1494 * Add an entry to refs->cmdline with the specified information.
1495 * *name is copied.
1497 static void add_rev_cmdline(struct rev_info *revs,
1498 struct object *item,
1499 const char *name,
1500 int whence,
1501 unsigned flags)
1503 struct rev_cmdline_info *info = &revs->cmdline;
1504 unsigned int nr = info->nr;
1506 ALLOC_GROW(info->rev, nr + 1, info->alloc);
1507 info->rev[nr].item = item;
1508 info->rev[nr].name = xstrdup(name);
1509 info->rev[nr].whence = whence;
1510 info->rev[nr].flags = flags;
1511 info->nr++;
1514 static void add_rev_cmdline_list(struct rev_info *revs,
1515 struct commit_list *commit_list,
1516 int whence,
1517 unsigned flags)
1519 while (commit_list) {
1520 struct object *object = &commit_list->item->object;
1521 add_rev_cmdline(revs, object, oid_to_hex(&object->oid),
1522 whence, flags);
1523 commit_list = commit_list->next;
1527 struct all_refs_cb {
1528 int all_flags;
1529 int warned_bad_reflog;
1530 struct rev_info *all_revs;
1531 const char *name_for_errormsg;
1532 struct worktree *wt;
1535 int ref_excluded(struct string_list *ref_excludes, const char *path)
1537 struct string_list_item *item;
1539 if (!ref_excludes)
1540 return 0;
1541 for_each_string_list_item(item, ref_excludes) {
1542 if (!wildmatch(item->string, path, 0))
1543 return 1;
1545 return 0;
1548 static int handle_one_ref(const char *path, const struct object_id *oid,
1549 int flag UNUSED,
1550 void *cb_data)
1552 struct all_refs_cb *cb = cb_data;
1553 struct object *object;
1555 if (ref_excluded(cb->all_revs->ref_excludes, path))
1556 return 0;
1558 object = get_reference(cb->all_revs, path, oid, cb->all_flags);
1559 add_rev_cmdline(cb->all_revs, object, path, REV_CMD_REF, cb->all_flags);
1560 add_pending_object(cb->all_revs, object, path);
1561 return 0;
1564 static void init_all_refs_cb(struct all_refs_cb *cb, struct rev_info *revs,
1565 unsigned flags)
1567 cb->all_revs = revs;
1568 cb->all_flags = flags;
1569 revs->rev_input_given = 1;
1570 cb->wt = NULL;
1573 void clear_ref_exclusion(struct string_list **ref_excludes_p)
1575 if (*ref_excludes_p) {
1576 string_list_clear(*ref_excludes_p, 0);
1577 free(*ref_excludes_p);
1579 *ref_excludes_p = NULL;
1582 void add_ref_exclusion(struct string_list **ref_excludes_p, const char *exclude)
1584 if (!*ref_excludes_p) {
1585 CALLOC_ARRAY(*ref_excludes_p, 1);
1586 (*ref_excludes_p)->strdup_strings = 1;
1588 string_list_append(*ref_excludes_p, exclude);
1591 static void handle_refs(struct ref_store *refs,
1592 struct rev_info *revs, unsigned flags,
1593 int (*for_each)(struct ref_store *, each_ref_fn, void *))
1595 struct all_refs_cb cb;
1597 if (!refs) {
1598 /* this could happen with uninitialized submodules */
1599 return;
1602 init_all_refs_cb(&cb, revs, flags);
1603 for_each(refs, handle_one_ref, &cb);
1606 static void handle_one_reflog_commit(struct object_id *oid, void *cb_data)
1608 struct all_refs_cb *cb = cb_data;
1609 if (!is_null_oid(oid)) {
1610 struct object *o = parse_object(cb->all_revs->repo, oid);
1611 if (o) {
1612 o->flags |= cb->all_flags;
1613 /* ??? CMDLINEFLAGS ??? */
1614 add_pending_object(cb->all_revs, o, "");
1616 else if (!cb->warned_bad_reflog) {
1617 warning("reflog of '%s' references pruned commits",
1618 cb->name_for_errormsg);
1619 cb->warned_bad_reflog = 1;
1624 static int handle_one_reflog_ent(struct object_id *ooid, struct object_id *noid,
1625 const char *email UNUSED,
1626 timestamp_t timestamp UNUSED,
1627 int tz UNUSED,
1628 const char *message UNUSED,
1629 void *cb_data)
1631 handle_one_reflog_commit(ooid, cb_data);
1632 handle_one_reflog_commit(noid, cb_data);
1633 return 0;
1636 static int handle_one_reflog(const char *refname_in_wt,
1637 const struct object_id *oid UNUSED,
1638 int flag UNUSED, void *cb_data)
1640 struct all_refs_cb *cb = cb_data;
1641 struct strbuf refname = STRBUF_INIT;
1643 cb->warned_bad_reflog = 0;
1644 strbuf_worktree_ref(cb->wt, &refname, refname_in_wt);
1645 cb->name_for_errormsg = refname.buf;
1646 refs_for_each_reflog_ent(get_main_ref_store(the_repository),
1647 refname.buf,
1648 handle_one_reflog_ent, cb_data);
1649 strbuf_release(&refname);
1650 return 0;
1653 static void add_other_reflogs_to_pending(struct all_refs_cb *cb)
1655 struct worktree **worktrees, **p;
1657 worktrees = get_worktrees();
1658 for (p = worktrees; *p; p++) {
1659 struct worktree *wt = *p;
1661 if (wt->is_current)
1662 continue;
1664 cb->wt = wt;
1665 refs_for_each_reflog(get_worktree_ref_store(wt),
1666 handle_one_reflog,
1667 cb);
1669 free_worktrees(worktrees);
1672 void add_reflogs_to_pending(struct rev_info *revs, unsigned flags)
1674 struct all_refs_cb cb;
1676 cb.all_revs = revs;
1677 cb.all_flags = flags;
1678 cb.wt = NULL;
1679 for_each_reflog(handle_one_reflog, &cb);
1681 if (!revs->single_worktree)
1682 add_other_reflogs_to_pending(&cb);
1685 static void add_cache_tree(struct cache_tree *it, struct rev_info *revs,
1686 struct strbuf *path, unsigned int flags)
1688 size_t baselen = path->len;
1689 int i;
1691 if (it->entry_count >= 0) {
1692 struct tree *tree = lookup_tree(revs->repo, &it->oid);
1693 tree->object.flags |= flags;
1694 add_pending_object_with_path(revs, &tree->object, "",
1695 040000, path->buf);
1698 for (i = 0; i < it->subtree_nr; i++) {
1699 struct cache_tree_sub *sub = it->down[i];
1700 strbuf_addf(path, "%s%s", baselen ? "/" : "", sub->name);
1701 add_cache_tree(sub->cache_tree, revs, path, flags);
1702 strbuf_setlen(path, baselen);
1707 static void add_resolve_undo_to_pending(struct index_state *istate, struct rev_info *revs)
1709 struct string_list_item *item;
1710 struct string_list *resolve_undo = istate->resolve_undo;
1712 if (!resolve_undo)
1713 return;
1715 for_each_string_list_item(item, resolve_undo) {
1716 const char *path = item->string;
1717 struct resolve_undo_info *ru = item->util;
1718 int i;
1720 if (!ru)
1721 continue;
1722 for (i = 0; i < 3; i++) {
1723 struct blob *blob;
1725 if (!ru->mode[i] || !S_ISREG(ru->mode[i]))
1726 continue;
1728 blob = lookup_blob(revs->repo, &ru->oid[i]);
1729 if (!blob) {
1730 warning(_("resolve-undo records `%s` which is missing"),
1731 oid_to_hex(&ru->oid[i]));
1732 continue;
1734 add_pending_object_with_path(revs, &blob->object, "",
1735 ru->mode[i], path);
1740 static void do_add_index_objects_to_pending(struct rev_info *revs,
1741 struct index_state *istate,
1742 unsigned int flags)
1744 int i;
1746 /* TODO: audit for interaction with sparse-index. */
1747 ensure_full_index(istate);
1748 for (i = 0; i < istate->cache_nr; i++) {
1749 struct cache_entry *ce = istate->cache[i];
1750 struct blob *blob;
1752 if (S_ISGITLINK(ce->ce_mode))
1753 continue;
1755 blob = lookup_blob(revs->repo, &ce->oid);
1756 if (!blob)
1757 die("unable to add index blob to traversal");
1758 blob->object.flags |= flags;
1759 add_pending_object_with_path(revs, &blob->object, "",
1760 ce->ce_mode, ce->name);
1763 if (istate->cache_tree) {
1764 struct strbuf path = STRBUF_INIT;
1765 add_cache_tree(istate->cache_tree, revs, &path, flags);
1766 strbuf_release(&path);
1769 add_resolve_undo_to_pending(istate, revs);
1772 void add_index_objects_to_pending(struct rev_info *revs, unsigned int flags)
1774 struct worktree **worktrees, **p;
1776 repo_read_index(revs->repo);
1777 do_add_index_objects_to_pending(revs, revs->repo->index, flags);
1779 if (revs->single_worktree)
1780 return;
1782 worktrees = get_worktrees();
1783 for (p = worktrees; *p; p++) {
1784 struct worktree *wt = *p;
1785 struct index_state istate = { NULL };
1787 if (wt->is_current)
1788 continue; /* current index already taken care of */
1790 if (read_index_from(&istate,
1791 worktree_git_path(wt, "index"),
1792 get_worktree_git_dir(wt)) > 0)
1793 do_add_index_objects_to_pending(revs, &istate, flags);
1794 discard_index(&istate);
1796 free_worktrees(worktrees);
1799 struct add_alternate_refs_data {
1800 struct rev_info *revs;
1801 unsigned int flags;
1804 static void add_one_alternate_ref(const struct object_id *oid,
1805 void *vdata)
1807 const char *name = ".alternate";
1808 struct add_alternate_refs_data *data = vdata;
1809 struct object *obj;
1811 obj = get_reference(data->revs, name, oid, data->flags);
1812 add_rev_cmdline(data->revs, obj, name, REV_CMD_REV, data->flags);
1813 add_pending_object(data->revs, obj, name);
1816 static void add_alternate_refs_to_pending(struct rev_info *revs,
1817 unsigned int flags)
1819 struct add_alternate_refs_data data;
1820 data.revs = revs;
1821 data.flags = flags;
1822 for_each_alternate_ref(add_one_alternate_ref, &data);
1825 static int add_parents_only(struct rev_info *revs, const char *arg_, int flags,
1826 int exclude_parent)
1828 struct object_id oid;
1829 struct object *it;
1830 struct commit *commit;
1831 struct commit_list *parents;
1832 int parent_number;
1833 const char *arg = arg_;
1835 if (*arg == '^') {
1836 flags ^= UNINTERESTING | BOTTOM;
1837 arg++;
1839 if (get_oid_committish(arg, &oid))
1840 return 0;
1841 while (1) {
1842 it = get_reference(revs, arg, &oid, 0);
1843 if (!it && revs->ignore_missing)
1844 return 0;
1845 if (it->type != OBJ_TAG)
1846 break;
1847 if (!((struct tag*)it)->tagged)
1848 return 0;
1849 oidcpy(&oid, &((struct tag*)it)->tagged->oid);
1851 if (it->type != OBJ_COMMIT)
1852 return 0;
1853 commit = (struct commit *)it;
1854 if (exclude_parent &&
1855 exclude_parent > commit_list_count(commit->parents))
1856 return 0;
1857 for (parents = commit->parents, parent_number = 1;
1858 parents;
1859 parents = parents->next, parent_number++) {
1860 if (exclude_parent && parent_number != exclude_parent)
1861 continue;
1863 it = &parents->item->object;
1864 it->flags |= flags;
1865 add_rev_cmdline(revs, it, arg_, REV_CMD_PARENTS_ONLY, flags);
1866 add_pending_object(revs, it, arg);
1868 return 1;
1871 void repo_init_revisions(struct repository *r,
1872 struct rev_info *revs,
1873 const char *prefix)
1875 memset(revs, 0, sizeof(*revs));
1877 revs->repo = r;
1878 revs->abbrev = DEFAULT_ABBREV;
1879 revs->simplify_history = 1;
1880 revs->pruning.repo = r;
1881 revs->pruning.flags.recursive = 1;
1882 revs->pruning.flags.quick = 1;
1883 revs->pruning.add_remove = file_add_remove;
1884 revs->pruning.change = file_change;
1885 revs->pruning.change_fn_data = revs;
1886 revs->sort_order = REV_SORT_IN_GRAPH_ORDER;
1887 revs->dense = 1;
1888 revs->prefix = prefix;
1889 revs->max_age = -1;
1890 revs->max_age_as_filter = -1;
1891 revs->min_age = -1;
1892 revs->skip_count = -1;
1893 revs->max_count = -1;
1894 revs->max_parents = -1;
1895 revs->expand_tabs_in_log = -1;
1897 revs->commit_format = CMIT_FMT_DEFAULT;
1898 revs->expand_tabs_in_log_default = 8;
1900 grep_init(&revs->grep_filter, revs->repo);
1901 revs->grep_filter.status_only = 1;
1903 repo_diff_setup(revs->repo, &revs->diffopt);
1904 if (prefix && !revs->diffopt.prefix) {
1905 revs->diffopt.prefix = prefix;
1906 revs->diffopt.prefix_length = strlen(prefix);
1909 init_display_notes(&revs->notes_opt);
1910 list_objects_filter_init(&revs->filter);
1913 static void add_pending_commit_list(struct rev_info *revs,
1914 struct commit_list *commit_list,
1915 unsigned int flags)
1917 while (commit_list) {
1918 struct object *object = &commit_list->item->object;
1919 object->flags |= flags;
1920 add_pending_object(revs, object, oid_to_hex(&object->oid));
1921 commit_list = commit_list->next;
1925 static void prepare_show_merge(struct rev_info *revs)
1927 struct commit_list *bases;
1928 struct commit *head, *other;
1929 struct object_id oid;
1930 const char **prune = NULL;
1931 int i, prune_num = 1; /* counting terminating NULL */
1932 struct index_state *istate = revs->repo->index;
1934 if (get_oid("HEAD", &oid))
1935 die("--merge without HEAD?");
1936 head = lookup_commit_or_die(&oid, "HEAD");
1937 if (get_oid("MERGE_HEAD", &oid))
1938 die("--merge without MERGE_HEAD?");
1939 other = lookup_commit_or_die(&oid, "MERGE_HEAD");
1940 add_pending_object(revs, &head->object, "HEAD");
1941 add_pending_object(revs, &other->object, "MERGE_HEAD");
1942 bases = get_merge_bases(head, other);
1943 add_rev_cmdline_list(revs, bases, REV_CMD_MERGE_BASE, UNINTERESTING | BOTTOM);
1944 add_pending_commit_list(revs, bases, UNINTERESTING | BOTTOM);
1945 free_commit_list(bases);
1946 head->object.flags |= SYMMETRIC_LEFT;
1948 if (!istate->cache_nr)
1949 repo_read_index(revs->repo);
1950 for (i = 0; i < istate->cache_nr; i++) {
1951 const struct cache_entry *ce = istate->cache[i];
1952 if (!ce_stage(ce))
1953 continue;
1954 if (ce_path_match(istate, ce, &revs->prune_data, NULL)) {
1955 prune_num++;
1956 REALLOC_ARRAY(prune, prune_num);
1957 prune[prune_num-2] = ce->name;
1958 prune[prune_num-1] = NULL;
1960 while ((i+1 < istate->cache_nr) &&
1961 ce_same_name(ce, istate->cache[i+1]))
1962 i++;
1964 clear_pathspec(&revs->prune_data);
1965 parse_pathspec(&revs->prune_data, PATHSPEC_ALL_MAGIC & ~PATHSPEC_LITERAL,
1966 PATHSPEC_PREFER_FULL | PATHSPEC_LITERAL_PATH, "", prune);
1967 revs->limited = 1;
1970 static int dotdot_missing(const char *arg, char *dotdot,
1971 struct rev_info *revs, int symmetric)
1973 if (revs->ignore_missing)
1974 return 0;
1975 /* de-munge so we report the full argument */
1976 *dotdot = '.';
1977 die(symmetric
1978 ? "Invalid symmetric difference expression %s"
1979 : "Invalid revision range %s", arg);
1982 static int handle_dotdot_1(const char *arg, char *dotdot,
1983 struct rev_info *revs, int flags,
1984 int cant_be_filename,
1985 struct object_context *a_oc,
1986 struct object_context *b_oc)
1988 const char *a_name, *b_name;
1989 struct object_id a_oid, b_oid;
1990 struct object *a_obj, *b_obj;
1991 unsigned int a_flags, b_flags;
1992 int symmetric = 0;
1993 unsigned int flags_exclude = flags ^ (UNINTERESTING | BOTTOM);
1994 unsigned int oc_flags = GET_OID_COMMITTISH | GET_OID_RECORD_PATH;
1996 a_name = arg;
1997 if (!*a_name)
1998 a_name = "HEAD";
2000 b_name = dotdot + 2;
2001 if (*b_name == '.') {
2002 symmetric = 1;
2003 b_name++;
2005 if (!*b_name)
2006 b_name = "HEAD";
2008 if (get_oid_with_context(revs->repo, a_name, oc_flags, &a_oid, a_oc) ||
2009 get_oid_with_context(revs->repo, b_name, oc_flags, &b_oid, b_oc))
2010 return -1;
2012 if (!cant_be_filename) {
2013 *dotdot = '.';
2014 verify_non_filename(revs->prefix, arg);
2015 *dotdot = '\0';
2018 a_obj = parse_object(revs->repo, &a_oid);
2019 b_obj = parse_object(revs->repo, &b_oid);
2020 if (!a_obj || !b_obj)
2021 return dotdot_missing(arg, dotdot, revs, symmetric);
2023 if (!symmetric) {
2024 /* just A..B */
2025 b_flags = flags;
2026 a_flags = flags_exclude;
2027 } else {
2028 /* A...B -- find merge bases between the two */
2029 struct commit *a, *b;
2030 struct commit_list *exclude;
2032 a = lookup_commit_reference(revs->repo, &a_obj->oid);
2033 b = lookup_commit_reference(revs->repo, &b_obj->oid);
2034 if (!a || !b)
2035 return dotdot_missing(arg, dotdot, revs, symmetric);
2037 exclude = get_merge_bases(a, b);
2038 add_rev_cmdline_list(revs, exclude, REV_CMD_MERGE_BASE,
2039 flags_exclude);
2040 add_pending_commit_list(revs, exclude, flags_exclude);
2041 free_commit_list(exclude);
2043 b_flags = flags;
2044 a_flags = flags | SYMMETRIC_LEFT;
2047 a_obj->flags |= a_flags;
2048 b_obj->flags |= b_flags;
2049 add_rev_cmdline(revs, a_obj, a_name, REV_CMD_LEFT, a_flags);
2050 add_rev_cmdline(revs, b_obj, b_name, REV_CMD_RIGHT, b_flags);
2051 add_pending_object_with_path(revs, a_obj, a_name, a_oc->mode, a_oc->path);
2052 add_pending_object_with_path(revs, b_obj, b_name, b_oc->mode, b_oc->path);
2053 return 0;
2056 static int handle_dotdot(const char *arg,
2057 struct rev_info *revs, int flags,
2058 int cant_be_filename)
2060 struct object_context a_oc, b_oc;
2061 char *dotdot = strstr(arg, "..");
2062 int ret;
2064 if (!dotdot)
2065 return -1;
2067 memset(&a_oc, 0, sizeof(a_oc));
2068 memset(&b_oc, 0, sizeof(b_oc));
2070 *dotdot = '\0';
2071 ret = handle_dotdot_1(arg, dotdot, revs, flags, cant_be_filename,
2072 &a_oc, &b_oc);
2073 *dotdot = '.';
2075 free(a_oc.path);
2076 free(b_oc.path);
2078 return ret;
2081 static int handle_revision_arg_1(const char *arg_, struct rev_info *revs, int flags, unsigned revarg_opt)
2083 struct object_context oc;
2084 char *mark;
2085 struct object *object;
2086 struct object_id oid;
2087 int local_flags;
2088 const char *arg = arg_;
2089 int cant_be_filename = revarg_opt & REVARG_CANNOT_BE_FILENAME;
2090 unsigned get_sha1_flags = GET_OID_RECORD_PATH;
2092 flags = flags & UNINTERESTING ? flags | BOTTOM : flags & ~BOTTOM;
2094 if (!cant_be_filename && !strcmp(arg, "..")) {
2096 * Just ".."? That is not a range but the
2097 * pathspec for the parent directory.
2099 return -1;
2102 if (!handle_dotdot(arg, revs, flags, revarg_opt))
2103 return 0;
2105 mark = strstr(arg, "^@");
2106 if (mark && !mark[2]) {
2107 *mark = 0;
2108 if (add_parents_only(revs, arg, flags, 0))
2109 return 0;
2110 *mark = '^';
2112 mark = strstr(arg, "^!");
2113 if (mark && !mark[2]) {
2114 *mark = 0;
2115 if (!add_parents_only(revs, arg, flags ^ (UNINTERESTING | BOTTOM), 0))
2116 *mark = '^';
2118 mark = strstr(arg, "^-");
2119 if (mark) {
2120 int exclude_parent = 1;
2122 if (mark[2]) {
2123 char *end;
2124 exclude_parent = strtoul(mark + 2, &end, 10);
2125 if (*end != '\0' || !exclude_parent)
2126 return -1;
2129 *mark = 0;
2130 if (!add_parents_only(revs, arg, flags ^ (UNINTERESTING | BOTTOM), exclude_parent))
2131 *mark = '^';
2134 local_flags = 0;
2135 if (*arg == '^') {
2136 local_flags = UNINTERESTING | BOTTOM;
2137 arg++;
2140 if (revarg_opt & REVARG_COMMITTISH)
2141 get_sha1_flags |= GET_OID_COMMITTISH;
2143 if (get_oid_with_context(revs->repo, arg, get_sha1_flags, &oid, &oc))
2144 return revs->ignore_missing ? 0 : -1;
2145 if (!cant_be_filename)
2146 verify_non_filename(revs->prefix, arg);
2147 object = get_reference(revs, arg, &oid, flags ^ local_flags);
2148 if (!object)
2149 return revs->ignore_missing ? 0 : -1;
2150 add_rev_cmdline(revs, object, arg_, REV_CMD_REV, flags ^ local_flags);
2151 add_pending_object_with_path(revs, object, arg, oc.mode, oc.path);
2152 free(oc.path);
2153 return 0;
2156 int handle_revision_arg(const char *arg, struct rev_info *revs, int flags, unsigned revarg_opt)
2158 int ret = handle_revision_arg_1(arg, revs, flags, revarg_opt);
2159 if (!ret)
2160 revs->rev_input_given = 1;
2161 return ret;
2164 static void read_pathspec_from_stdin(struct strbuf *sb,
2165 struct strvec *prune)
2167 while (strbuf_getline(sb, stdin) != EOF)
2168 strvec_push(prune, sb->buf);
2171 static void read_revisions_from_stdin(struct rev_info *revs,
2172 struct strvec *prune)
2174 struct strbuf sb;
2175 int seen_dashdash = 0;
2176 int save_warning;
2178 save_warning = warn_on_object_refname_ambiguity;
2179 warn_on_object_refname_ambiguity = 0;
2181 strbuf_init(&sb, 1000);
2182 while (strbuf_getline(&sb, stdin) != EOF) {
2183 int len = sb.len;
2184 if (!len)
2185 break;
2186 if (sb.buf[0] == '-') {
2187 if (len == 2 && sb.buf[1] == '-') {
2188 seen_dashdash = 1;
2189 break;
2191 die("options not supported in --stdin mode");
2193 if (handle_revision_arg(sb.buf, revs, 0,
2194 REVARG_CANNOT_BE_FILENAME))
2195 die("bad revision '%s'", sb.buf);
2197 if (seen_dashdash)
2198 read_pathspec_from_stdin(&sb, prune);
2200 strbuf_release(&sb);
2201 warn_on_object_refname_ambiguity = save_warning;
2204 static void add_grep(struct rev_info *revs, const char *ptn, enum grep_pat_token what)
2206 append_grep_pattern(&revs->grep_filter, ptn, "command line", 0, what);
2209 static void add_header_grep(struct rev_info *revs, enum grep_header_field field, const char *pattern)
2211 append_header_grep_pattern(&revs->grep_filter, field, pattern);
2214 static void add_message_grep(struct rev_info *revs, const char *pattern)
2216 add_grep(revs, pattern, GREP_PATTERN_BODY);
2219 static int handle_revision_opt(struct rev_info *revs, int argc, const char **argv,
2220 int *unkc, const char **unkv,
2221 const struct setup_revision_opt* opt)
2223 const char *arg = argv[0];
2224 const char *optarg = NULL;
2225 int argcount;
2226 const unsigned hexsz = the_hash_algo->hexsz;
2228 /* pseudo revision arguments */
2229 if (!strcmp(arg, "--all") || !strcmp(arg, "--branches") ||
2230 !strcmp(arg, "--tags") || !strcmp(arg, "--remotes") ||
2231 !strcmp(arg, "--reflog") || !strcmp(arg, "--not") ||
2232 !strcmp(arg, "--no-walk") || !strcmp(arg, "--do-walk") ||
2233 !strcmp(arg, "--bisect") || starts_with(arg, "--glob=") ||
2234 !strcmp(arg, "--indexed-objects") ||
2235 !strcmp(arg, "--alternate-refs") ||
2236 starts_with(arg, "--exclude=") ||
2237 starts_with(arg, "--branches=") || starts_with(arg, "--tags=") ||
2238 starts_with(arg, "--remotes=") || starts_with(arg, "--no-walk="))
2240 unkv[(*unkc)++] = arg;
2241 return 1;
2244 if ((argcount = parse_long_opt("max-count", argv, &optarg))) {
2245 revs->max_count = atoi(optarg);
2246 revs->no_walk = 0;
2247 return argcount;
2248 } else if ((argcount = parse_long_opt("skip", argv, &optarg))) {
2249 revs->skip_count = atoi(optarg);
2250 return argcount;
2251 } else if ((*arg == '-') && isdigit(arg[1])) {
2252 /* accept -<digit>, like traditional "head" */
2253 if (strtol_i(arg + 1, 10, &revs->max_count) < 0 ||
2254 revs->max_count < 0)
2255 die("'%s': not a non-negative integer", arg + 1);
2256 revs->no_walk = 0;
2257 } else if (!strcmp(arg, "-n")) {
2258 if (argc <= 1)
2259 return error("-n requires an argument");
2260 revs->max_count = atoi(argv[1]);
2261 revs->no_walk = 0;
2262 return 2;
2263 } else if (skip_prefix(arg, "-n", &optarg)) {
2264 revs->max_count = atoi(optarg);
2265 revs->no_walk = 0;
2266 } else if ((argcount = parse_long_opt("max-age", argv, &optarg))) {
2267 revs->max_age = atoi(optarg);
2268 return argcount;
2269 } else if ((argcount = parse_long_opt("since", argv, &optarg))) {
2270 revs->max_age = approxidate(optarg);
2271 return argcount;
2272 } else if ((argcount = parse_long_opt("since-as-filter", argv, &optarg))) {
2273 revs->max_age_as_filter = approxidate(optarg);
2274 return argcount;
2275 } else if ((argcount = parse_long_opt("after", argv, &optarg))) {
2276 revs->max_age = approxidate(optarg);
2277 return argcount;
2278 } else if ((argcount = parse_long_opt("min-age", argv, &optarg))) {
2279 revs->min_age = atoi(optarg);
2280 return argcount;
2281 } else if ((argcount = parse_long_opt("before", argv, &optarg))) {
2282 revs->min_age = approxidate(optarg);
2283 return argcount;
2284 } else if ((argcount = parse_long_opt("until", argv, &optarg))) {
2285 revs->min_age = approxidate(optarg);
2286 return argcount;
2287 } else if (!strcmp(arg, "--first-parent")) {
2288 revs->first_parent_only = 1;
2289 } else if (!strcmp(arg, "--exclude-first-parent-only")) {
2290 revs->exclude_first_parent_only = 1;
2291 } else if (!strcmp(arg, "--ancestry-path")) {
2292 revs->ancestry_path = 1;
2293 revs->simplify_history = 0;
2294 revs->limited = 1;
2295 revs->ancestry_path_implicit_bottoms = 1;
2296 } else if (skip_prefix(arg, "--ancestry-path=", &optarg)) {
2297 struct commit *c;
2298 struct object_id oid;
2299 const char *msg = _("could not get commit for ancestry-path argument %s");
2301 revs->ancestry_path = 1;
2302 revs->simplify_history = 0;
2303 revs->limited = 1;
2305 if (repo_get_oid_committish(revs->repo, optarg, &oid))
2306 return error(msg, optarg);
2307 get_reference(revs, optarg, &oid, ANCESTRY_PATH);
2308 c = lookup_commit_reference(revs->repo, &oid);
2309 if (!c)
2310 return error(msg, optarg);
2311 commit_list_insert(c, &revs->ancestry_path_bottoms);
2312 } else if (!strcmp(arg, "-g") || !strcmp(arg, "--walk-reflogs")) {
2313 init_reflog_walk(&revs->reflog_info);
2314 } else if (!strcmp(arg, "--default")) {
2315 if (argc <= 1)
2316 return error("bad --default argument");
2317 revs->def = argv[1];
2318 return 2;
2319 } else if (!strcmp(arg, "--merge")) {
2320 revs->show_merge = 1;
2321 } else if (!strcmp(arg, "--topo-order")) {
2322 revs->sort_order = REV_SORT_IN_GRAPH_ORDER;
2323 revs->topo_order = 1;
2324 } else if (!strcmp(arg, "--simplify-merges")) {
2325 revs->simplify_merges = 1;
2326 revs->topo_order = 1;
2327 revs->rewrite_parents = 1;
2328 revs->simplify_history = 0;
2329 revs->limited = 1;
2330 } else if (!strcmp(arg, "--simplify-by-decoration")) {
2331 revs->simplify_merges = 1;
2332 revs->topo_order = 1;
2333 revs->rewrite_parents = 1;
2334 revs->simplify_history = 0;
2335 revs->simplify_by_decoration = 1;
2336 revs->limited = 1;
2337 revs->prune = 1;
2338 } else if (!strcmp(arg, "--date-order")) {
2339 revs->sort_order = REV_SORT_BY_COMMIT_DATE;
2340 revs->topo_order = 1;
2341 } else if (!strcmp(arg, "--author-date-order")) {
2342 revs->sort_order = REV_SORT_BY_AUTHOR_DATE;
2343 revs->topo_order = 1;
2344 } else if (!strcmp(arg, "--early-output")) {
2345 revs->early_output = 100;
2346 revs->topo_order = 1;
2347 } else if (skip_prefix(arg, "--early-output=", &optarg)) {
2348 if (strtoul_ui(optarg, 10, &revs->early_output) < 0)
2349 die("'%s': not a non-negative integer", optarg);
2350 revs->topo_order = 1;
2351 } else if (!strcmp(arg, "--parents")) {
2352 revs->rewrite_parents = 1;
2353 revs->print_parents = 1;
2354 } else if (!strcmp(arg, "--dense")) {
2355 revs->dense = 1;
2356 } else if (!strcmp(arg, "--sparse")) {
2357 revs->dense = 0;
2358 } else if (!strcmp(arg, "--in-commit-order")) {
2359 revs->tree_blobs_in_commit_order = 1;
2360 } else if (!strcmp(arg, "--remove-empty")) {
2361 revs->remove_empty_trees = 1;
2362 } else if (!strcmp(arg, "--merges")) {
2363 revs->min_parents = 2;
2364 } else if (!strcmp(arg, "--no-merges")) {
2365 revs->max_parents = 1;
2366 } else if (skip_prefix(arg, "--min-parents=", &optarg)) {
2367 revs->min_parents = atoi(optarg);
2368 } else if (!strcmp(arg, "--no-min-parents")) {
2369 revs->min_parents = 0;
2370 } else if (skip_prefix(arg, "--max-parents=", &optarg)) {
2371 revs->max_parents = atoi(optarg);
2372 } else if (!strcmp(arg, "--no-max-parents")) {
2373 revs->max_parents = -1;
2374 } else if (!strcmp(arg, "--boundary")) {
2375 revs->boundary = 1;
2376 } else if (!strcmp(arg, "--left-right")) {
2377 revs->left_right = 1;
2378 } else if (!strcmp(arg, "--left-only")) {
2379 if (revs->right_only)
2380 die("--left-only is incompatible with --right-only"
2381 " or --cherry");
2382 revs->left_only = 1;
2383 } else if (!strcmp(arg, "--right-only")) {
2384 if (revs->left_only)
2385 die(_("options '%s' and '%s' cannot be used together"), "--right-only", "--left-only");
2386 revs->right_only = 1;
2387 } else if (!strcmp(arg, "--cherry")) {
2388 if (revs->left_only)
2389 die(_("options '%s' and '%s' cannot be used together"), "--cherry", "--left-only");
2390 revs->cherry_mark = 1;
2391 revs->right_only = 1;
2392 revs->max_parents = 1;
2393 revs->limited = 1;
2394 } else if (!strcmp(arg, "--count")) {
2395 revs->count = 1;
2396 } else if (!strcmp(arg, "--cherry-mark")) {
2397 if (revs->cherry_pick)
2398 die(_("options '%s' and '%s' cannot be used together"), "--cherry-mark", "--cherry-pick");
2399 revs->cherry_mark = 1;
2400 revs->limited = 1; /* needs limit_list() */
2401 } else if (!strcmp(arg, "--cherry-pick")) {
2402 if (revs->cherry_mark)
2403 die(_("options '%s' and '%s' cannot be used together"), "--cherry-pick", "--cherry-mark");
2404 revs->cherry_pick = 1;
2405 revs->limited = 1;
2406 } else if (!strcmp(arg, "--objects")) {
2407 revs->tag_objects = 1;
2408 revs->tree_objects = 1;
2409 revs->blob_objects = 1;
2410 } else if (!strcmp(arg, "--objects-edge")) {
2411 revs->tag_objects = 1;
2412 revs->tree_objects = 1;
2413 revs->blob_objects = 1;
2414 revs->edge_hint = 1;
2415 } else if (!strcmp(arg, "--objects-edge-aggressive")) {
2416 revs->tag_objects = 1;
2417 revs->tree_objects = 1;
2418 revs->blob_objects = 1;
2419 revs->edge_hint = 1;
2420 revs->edge_hint_aggressive = 1;
2421 } else if (!strcmp(arg, "--verify-objects")) {
2422 revs->tag_objects = 1;
2423 revs->tree_objects = 1;
2424 revs->blob_objects = 1;
2425 revs->verify_objects = 1;
2426 disable_commit_graph(revs->repo);
2427 } else if (!strcmp(arg, "--unpacked")) {
2428 revs->unpacked = 1;
2429 } else if (starts_with(arg, "--unpacked=")) {
2430 die(_("--unpacked=<packfile> no longer supported"));
2431 } else if (!strcmp(arg, "--no-kept-objects")) {
2432 revs->no_kept_objects = 1;
2433 revs->keep_pack_cache_flags |= IN_CORE_KEEP_PACKS;
2434 revs->keep_pack_cache_flags |= ON_DISK_KEEP_PACKS;
2435 } else if (skip_prefix(arg, "--no-kept-objects=", &optarg)) {
2436 revs->no_kept_objects = 1;
2437 if (!strcmp(optarg, "in-core"))
2438 revs->keep_pack_cache_flags |= IN_CORE_KEEP_PACKS;
2439 if (!strcmp(optarg, "on-disk"))
2440 revs->keep_pack_cache_flags |= ON_DISK_KEEP_PACKS;
2441 } else if (!strcmp(arg, "-r")) {
2442 revs->diff = 1;
2443 revs->diffopt.flags.recursive = 1;
2444 } else if (!strcmp(arg, "-t")) {
2445 revs->diff = 1;
2446 revs->diffopt.flags.recursive = 1;
2447 revs->diffopt.flags.tree_in_recursive = 1;
2448 } else if ((argcount = diff_merges_parse_opts(revs, argv))) {
2449 return argcount;
2450 } else if (!strcmp(arg, "-v")) {
2451 revs->verbose_header = 1;
2452 } else if (!strcmp(arg, "--pretty")) {
2453 revs->verbose_header = 1;
2454 revs->pretty_given = 1;
2455 get_commit_format(NULL, revs);
2456 } else if (skip_prefix(arg, "--pretty=", &optarg) ||
2457 skip_prefix(arg, "--format=", &optarg)) {
2459 * Detached form ("--pretty X" as opposed to "--pretty=X")
2460 * not allowed, since the argument is optional.
2462 revs->verbose_header = 1;
2463 revs->pretty_given = 1;
2464 get_commit_format(optarg, revs);
2465 } else if (!strcmp(arg, "--expand-tabs")) {
2466 revs->expand_tabs_in_log = 8;
2467 } else if (!strcmp(arg, "--no-expand-tabs")) {
2468 revs->expand_tabs_in_log = 0;
2469 } else if (skip_prefix(arg, "--expand-tabs=", &arg)) {
2470 int val;
2471 if (strtol_i(arg, 10, &val) < 0 || val < 0)
2472 die("'%s': not a non-negative integer", arg);
2473 revs->expand_tabs_in_log = val;
2474 } else if (!strcmp(arg, "--show-notes") || !strcmp(arg, "--notes")) {
2475 enable_default_display_notes(&revs->notes_opt, &revs->show_notes);
2476 revs->show_notes_given = 1;
2477 } else if (!strcmp(arg, "--show-signature")) {
2478 revs->show_signature = 1;
2479 } else if (!strcmp(arg, "--no-show-signature")) {
2480 revs->show_signature = 0;
2481 } else if (!strcmp(arg, "--show-linear-break")) {
2482 revs->break_bar = " ..........";
2483 revs->track_linear = 1;
2484 revs->track_first_time = 1;
2485 } else if (skip_prefix(arg, "--show-linear-break=", &optarg)) {
2486 revs->break_bar = xstrdup(optarg);
2487 revs->track_linear = 1;
2488 revs->track_first_time = 1;
2489 } else if (skip_prefix(arg, "--show-notes=", &optarg) ||
2490 skip_prefix(arg, "--notes=", &optarg)) {
2491 if (starts_with(arg, "--show-notes=") &&
2492 revs->notes_opt.use_default_notes < 0)
2493 revs->notes_opt.use_default_notes = 1;
2494 enable_ref_display_notes(&revs->notes_opt, &revs->show_notes, optarg);
2495 revs->show_notes_given = 1;
2496 } else if (!strcmp(arg, "--no-notes")) {
2497 disable_display_notes(&revs->notes_opt, &revs->show_notes);
2498 revs->show_notes_given = 1;
2499 } else if (!strcmp(arg, "--standard-notes")) {
2500 revs->show_notes_given = 1;
2501 revs->notes_opt.use_default_notes = 1;
2502 } else if (!strcmp(arg, "--no-standard-notes")) {
2503 revs->notes_opt.use_default_notes = 0;
2504 } else if (!strcmp(arg, "--oneline")) {
2505 revs->verbose_header = 1;
2506 get_commit_format("oneline", revs);
2507 revs->pretty_given = 1;
2508 revs->abbrev_commit = 1;
2509 } else if (!strcmp(arg, "--graph")) {
2510 graph_clear(revs->graph);
2511 revs->graph = graph_init(revs);
2512 } else if (!strcmp(arg, "--no-graph")) {
2513 graph_clear(revs->graph);
2514 revs->graph = NULL;
2515 } else if (!strcmp(arg, "--encode-email-headers")) {
2516 revs->encode_email_headers = 1;
2517 } else if (!strcmp(arg, "--no-encode-email-headers")) {
2518 revs->encode_email_headers = 0;
2519 } else if (!strcmp(arg, "--root")) {
2520 revs->show_root_diff = 1;
2521 } else if (!strcmp(arg, "--no-commit-id")) {
2522 revs->no_commit_id = 1;
2523 } else if (!strcmp(arg, "--always")) {
2524 revs->always_show_header = 1;
2525 } else if (!strcmp(arg, "--no-abbrev")) {
2526 revs->abbrev = 0;
2527 } else if (!strcmp(arg, "--abbrev")) {
2528 revs->abbrev = DEFAULT_ABBREV;
2529 } else if (skip_prefix(arg, "--abbrev=", &optarg)) {
2530 revs->abbrev = strtoul(optarg, NULL, 10);
2531 if (revs->abbrev < MINIMUM_ABBREV)
2532 revs->abbrev = MINIMUM_ABBREV;
2533 else if (revs->abbrev > hexsz)
2534 revs->abbrev = hexsz;
2535 } else if (!strcmp(arg, "--abbrev-commit")) {
2536 revs->abbrev_commit = 1;
2537 revs->abbrev_commit_given = 1;
2538 } else if (!strcmp(arg, "--no-abbrev-commit")) {
2539 revs->abbrev_commit = 0;
2540 } else if (!strcmp(arg, "--full-diff")) {
2541 revs->diff = 1;
2542 revs->full_diff = 1;
2543 } else if (!strcmp(arg, "--show-pulls")) {
2544 revs->show_pulls = 1;
2545 } else if (!strcmp(arg, "--full-history")) {
2546 revs->simplify_history = 0;
2547 } else if (!strcmp(arg, "--relative-date")) {
2548 revs->date_mode.type = DATE_RELATIVE;
2549 revs->date_mode_explicit = 1;
2550 } else if ((argcount = parse_long_opt("date", argv, &optarg))) {
2551 parse_date_format(optarg, &revs->date_mode);
2552 revs->date_mode_explicit = 1;
2553 return argcount;
2554 } else if (!strcmp(arg, "--log-size")) {
2555 revs->show_log_size = 1;
2558 * Grepping the commit log
2560 else if ((argcount = parse_long_opt("author", argv, &optarg))) {
2561 add_header_grep(revs, GREP_HEADER_AUTHOR, optarg);
2562 return argcount;
2563 } else if ((argcount = parse_long_opt("committer", argv, &optarg))) {
2564 add_header_grep(revs, GREP_HEADER_COMMITTER, optarg);
2565 return argcount;
2566 } else if ((argcount = parse_long_opt("grep-reflog", argv, &optarg))) {
2567 add_header_grep(revs, GREP_HEADER_REFLOG, optarg);
2568 return argcount;
2569 } else if ((argcount = parse_long_opt("grep", argv, &optarg))) {
2570 add_message_grep(revs, optarg);
2571 return argcount;
2572 } else if (!strcmp(arg, "--basic-regexp")) {
2573 revs->grep_filter.pattern_type_option = GREP_PATTERN_TYPE_BRE;
2574 } else if (!strcmp(arg, "--extended-regexp") || !strcmp(arg, "-E")) {
2575 revs->grep_filter.pattern_type_option = GREP_PATTERN_TYPE_ERE;
2576 } else if (!strcmp(arg, "--regexp-ignore-case") || !strcmp(arg, "-i")) {
2577 revs->grep_filter.ignore_case = 1;
2578 revs->diffopt.pickaxe_opts |= DIFF_PICKAXE_IGNORE_CASE;
2579 } else if (!strcmp(arg, "--fixed-strings") || !strcmp(arg, "-F")) {
2580 revs->grep_filter.pattern_type_option = GREP_PATTERN_TYPE_FIXED;
2581 } else if (!strcmp(arg, "--perl-regexp") || !strcmp(arg, "-P")) {
2582 revs->grep_filter.pattern_type_option = GREP_PATTERN_TYPE_PCRE;
2583 } else if (!strcmp(arg, "--all-match")) {
2584 revs->grep_filter.all_match = 1;
2585 } else if (!strcmp(arg, "--invert-grep")) {
2586 revs->grep_filter.no_body_match = 1;
2587 } else if ((argcount = parse_long_opt("encoding", argv, &optarg))) {
2588 if (strcmp(optarg, "none"))
2589 git_log_output_encoding = xstrdup(optarg);
2590 else
2591 git_log_output_encoding = "";
2592 return argcount;
2593 } else if (!strcmp(arg, "--reverse")) {
2594 revs->reverse ^= 1;
2595 } else if (!strcmp(arg, "--children")) {
2596 revs->children.name = "children";
2597 revs->limited = 1;
2598 } else if (!strcmp(arg, "--ignore-missing")) {
2599 revs->ignore_missing = 1;
2600 } else if (opt && opt->allow_exclude_promisor_objects &&
2601 !strcmp(arg, "--exclude-promisor-objects")) {
2602 if (fetch_if_missing)
2603 BUG("exclude_promisor_objects can only be used when fetch_if_missing is 0");
2604 revs->exclude_promisor_objects = 1;
2605 } else {
2606 int opts = diff_opt_parse(&revs->diffopt, argv, argc, revs->prefix);
2607 if (!opts)
2608 unkv[(*unkc)++] = arg;
2609 return opts;
2612 return 1;
2615 void parse_revision_opt(struct rev_info *revs, struct parse_opt_ctx_t *ctx,
2616 const struct option *options,
2617 const char * const usagestr[])
2619 int n = handle_revision_opt(revs, ctx->argc, ctx->argv,
2620 &ctx->cpidx, ctx->out, NULL);
2621 if (n <= 0) {
2622 error("unknown option `%s'", ctx->argv[0]);
2623 usage_with_options(usagestr, options);
2625 ctx->argv += n;
2626 ctx->argc -= n;
2629 void revision_opts_finish(struct rev_info *revs)
2631 if (revs->graph && revs->track_linear)
2632 die(_("options '%s' and '%s' cannot be used together"), "--show-linear-break", "--graph");
2634 if (revs->graph) {
2635 revs->topo_order = 1;
2636 revs->rewrite_parents = 1;
2640 static int for_each_bisect_ref(struct ref_store *refs, each_ref_fn fn,
2641 void *cb_data, const char *term)
2643 struct strbuf bisect_refs = STRBUF_INIT;
2644 int status;
2645 strbuf_addf(&bisect_refs, "refs/bisect/%s", term);
2646 status = refs_for_each_fullref_in(refs, bisect_refs.buf, fn, cb_data);
2647 strbuf_release(&bisect_refs);
2648 return status;
2651 static int for_each_bad_bisect_ref(struct ref_store *refs, each_ref_fn fn, void *cb_data)
2653 return for_each_bisect_ref(refs, fn, cb_data, term_bad);
2656 static int for_each_good_bisect_ref(struct ref_store *refs, each_ref_fn fn, void *cb_data)
2658 return for_each_bisect_ref(refs, fn, cb_data, term_good);
2661 static int handle_revision_pseudo_opt(struct rev_info *revs,
2662 const char **argv, int *flags)
2664 const char *arg = argv[0];
2665 const char *optarg;
2666 struct ref_store *refs;
2667 int argcount;
2669 if (revs->repo != the_repository) {
2671 * We need some something like get_submodule_worktrees()
2672 * before we can go through all worktrees of a submodule,
2673 * .e.g with adding all HEADs from --all, which is not
2674 * supported right now, so stick to single worktree.
2676 if (!revs->single_worktree)
2677 BUG("--single-worktree cannot be used together with submodule");
2679 refs = get_main_ref_store(revs->repo);
2682 * NOTE!
2684 * Commands like "git shortlog" will not accept the options below
2685 * unless parse_revision_opt queues them (as opposed to erroring
2686 * out).
2688 * When implementing your new pseudo-option, remember to
2689 * register it in the list at the top of handle_revision_opt.
2691 if (!strcmp(arg, "--all")) {
2692 handle_refs(refs, revs, *flags, refs_for_each_ref);
2693 handle_refs(refs, revs, *flags, refs_head_ref);
2694 if (!revs->single_worktree) {
2695 struct all_refs_cb cb;
2697 init_all_refs_cb(&cb, revs, *flags);
2698 other_head_refs(handle_one_ref, &cb);
2700 clear_ref_exclusion(&revs->ref_excludes);
2701 } else if (!strcmp(arg, "--branches")) {
2702 handle_refs(refs, revs, *flags, refs_for_each_branch_ref);
2703 clear_ref_exclusion(&revs->ref_excludes);
2704 } else if (!strcmp(arg, "--bisect")) {
2705 read_bisect_terms(&term_bad, &term_good);
2706 handle_refs(refs, revs, *flags, for_each_bad_bisect_ref);
2707 handle_refs(refs, revs, *flags ^ (UNINTERESTING | BOTTOM),
2708 for_each_good_bisect_ref);
2709 revs->bisect = 1;
2710 } else if (!strcmp(arg, "--tags")) {
2711 handle_refs(refs, revs, *flags, refs_for_each_tag_ref);
2712 clear_ref_exclusion(&revs->ref_excludes);
2713 } else if (!strcmp(arg, "--remotes")) {
2714 handle_refs(refs, revs, *flags, refs_for_each_remote_ref);
2715 clear_ref_exclusion(&revs->ref_excludes);
2716 } else if ((argcount = parse_long_opt("glob", argv, &optarg))) {
2717 struct all_refs_cb cb;
2718 init_all_refs_cb(&cb, revs, *flags);
2719 for_each_glob_ref(handle_one_ref, optarg, &cb);
2720 clear_ref_exclusion(&revs->ref_excludes);
2721 return argcount;
2722 } else if ((argcount = parse_long_opt("exclude", argv, &optarg))) {
2723 add_ref_exclusion(&revs->ref_excludes, optarg);
2724 return argcount;
2725 } else if (skip_prefix(arg, "--branches=", &optarg)) {
2726 struct all_refs_cb cb;
2727 init_all_refs_cb(&cb, revs, *flags);
2728 for_each_glob_ref_in(handle_one_ref, optarg, "refs/heads/", &cb);
2729 clear_ref_exclusion(&revs->ref_excludes);
2730 } else if (skip_prefix(arg, "--tags=", &optarg)) {
2731 struct all_refs_cb cb;
2732 init_all_refs_cb(&cb, revs, *flags);
2733 for_each_glob_ref_in(handle_one_ref, optarg, "refs/tags/", &cb);
2734 clear_ref_exclusion(&revs->ref_excludes);
2735 } else if (skip_prefix(arg, "--remotes=", &optarg)) {
2736 struct all_refs_cb cb;
2737 init_all_refs_cb(&cb, revs, *flags);
2738 for_each_glob_ref_in(handle_one_ref, optarg, "refs/remotes/", &cb);
2739 clear_ref_exclusion(&revs->ref_excludes);
2740 } else if (!strcmp(arg, "--reflog")) {
2741 add_reflogs_to_pending(revs, *flags);
2742 } else if (!strcmp(arg, "--indexed-objects")) {
2743 add_index_objects_to_pending(revs, *flags);
2744 } else if (!strcmp(arg, "--alternate-refs")) {
2745 add_alternate_refs_to_pending(revs, *flags);
2746 } else if (!strcmp(arg, "--not")) {
2747 *flags ^= UNINTERESTING | BOTTOM;
2748 } else if (!strcmp(arg, "--no-walk")) {
2749 revs->no_walk = 1;
2750 } else if (skip_prefix(arg, "--no-walk=", &optarg)) {
2752 * Detached form ("--no-walk X" as opposed to "--no-walk=X")
2753 * not allowed, since the argument is optional.
2755 revs->no_walk = 1;
2756 if (!strcmp(optarg, "sorted"))
2757 revs->unsorted_input = 0;
2758 else if (!strcmp(optarg, "unsorted"))
2759 revs->unsorted_input = 1;
2760 else
2761 return error("invalid argument to --no-walk");
2762 } else if (!strcmp(arg, "--do-walk")) {
2763 revs->no_walk = 0;
2764 } else if (!strcmp(arg, "--single-worktree")) {
2765 revs->single_worktree = 1;
2766 } else if (skip_prefix(arg, ("--filter="), &arg)) {
2767 parse_list_objects_filter(&revs->filter, arg);
2768 } else if (!strcmp(arg, ("--no-filter"))) {
2769 list_objects_filter_set_no_filter(&revs->filter);
2770 } else {
2771 return 0;
2774 return 1;
2777 static void NORETURN diagnose_missing_default(const char *def)
2779 int flags;
2780 const char *refname;
2782 refname = resolve_ref_unsafe(def, 0, NULL, &flags);
2783 if (!refname || !(flags & REF_ISSYMREF) || (flags & REF_ISBROKEN))
2784 die(_("your current branch appears to be broken"));
2786 skip_prefix(refname, "refs/heads/", &refname);
2787 die(_("your current branch '%s' does not have any commits yet"),
2788 refname);
2792 * Parse revision information, filling in the "rev_info" structure,
2793 * and removing the used arguments from the argument list.
2795 * Returns the number of arguments left that weren't recognized
2796 * (which are also moved to the head of the argument list)
2798 int setup_revisions(int argc, const char **argv, struct rev_info *revs, struct setup_revision_opt *opt)
2800 int i, flags, left, seen_dashdash, revarg_opt;
2801 struct strvec prune_data = STRVEC_INIT;
2802 int seen_end_of_options = 0;
2804 /* First, search for "--" */
2805 if (opt && opt->assume_dashdash) {
2806 seen_dashdash = 1;
2807 } else {
2808 seen_dashdash = 0;
2809 for (i = 1; i < argc; i++) {
2810 const char *arg = argv[i];
2811 if (strcmp(arg, "--"))
2812 continue;
2813 if (opt && opt->free_removed_argv_elements)
2814 free((char *)argv[i]);
2815 argv[i] = NULL;
2816 argc = i;
2817 if (argv[i + 1])
2818 strvec_pushv(&prune_data, argv + i + 1);
2819 seen_dashdash = 1;
2820 break;
2824 /* Second, deal with arguments and options */
2825 flags = 0;
2826 revarg_opt = opt ? opt->revarg_opt : 0;
2827 if (seen_dashdash)
2828 revarg_opt |= REVARG_CANNOT_BE_FILENAME;
2829 for (left = i = 1; i < argc; i++) {
2830 const char *arg = argv[i];
2831 if (!seen_end_of_options && *arg == '-') {
2832 int opts;
2834 opts = handle_revision_pseudo_opt(
2835 revs, argv + i,
2836 &flags);
2837 if (opts > 0) {
2838 i += opts - 1;
2839 continue;
2842 if (!strcmp(arg, "--stdin")) {
2843 if (revs->disable_stdin) {
2844 argv[left++] = arg;
2845 continue;
2847 if (revs->read_from_stdin++)
2848 die("--stdin given twice?");
2849 read_revisions_from_stdin(revs, &prune_data);
2850 continue;
2853 if (!strcmp(arg, "--end-of-options")) {
2854 seen_end_of_options = 1;
2855 continue;
2858 opts = handle_revision_opt(revs, argc - i, argv + i,
2859 &left, argv, opt);
2860 if (opts > 0) {
2861 i += opts - 1;
2862 continue;
2864 if (opts < 0)
2865 exit(128);
2866 continue;
2870 if (handle_revision_arg(arg, revs, flags, revarg_opt)) {
2871 int j;
2872 if (seen_dashdash || *arg == '^')
2873 die("bad revision '%s'", arg);
2875 /* If we didn't have a "--":
2876 * (1) all filenames must exist;
2877 * (2) all rev-args must not be interpretable
2878 * as a valid filename.
2879 * but the latter we have checked in the main loop.
2881 for (j = i; j < argc; j++)
2882 verify_filename(revs->prefix, argv[j], j == i);
2884 strvec_pushv(&prune_data, argv + i);
2885 break;
2888 revision_opts_finish(revs);
2890 if (prune_data.nr) {
2892 * If we need to introduce the magic "a lone ':' means no
2893 * pathspec whatsoever", here is the place to do so.
2895 * if (prune_data.nr == 1 && !strcmp(prune_data[0], ":")) {
2896 * prune_data.nr = 0;
2897 * prune_data.alloc = 0;
2898 * free(prune_data.path);
2899 * prune_data.path = NULL;
2900 * } else {
2901 * terminate prune_data.alloc with NULL and
2902 * call init_pathspec() to set revs->prune_data here.
2905 parse_pathspec(&revs->prune_data, 0, 0,
2906 revs->prefix, prune_data.v);
2908 strvec_clear(&prune_data);
2910 if (!revs->def)
2911 revs->def = opt ? opt->def : NULL;
2912 if (opt && opt->tweak)
2913 opt->tweak(revs, opt);
2914 if (revs->show_merge)
2915 prepare_show_merge(revs);
2916 if (revs->def && !revs->pending.nr && !revs->rev_input_given) {
2917 struct object_id oid;
2918 struct object *object;
2919 struct object_context oc;
2920 if (get_oid_with_context(revs->repo, revs->def, 0, &oid, &oc))
2921 diagnose_missing_default(revs->def);
2922 object = get_reference(revs, revs->def, &oid, 0);
2923 add_pending_object_with_mode(revs, object, revs->def, oc.mode);
2926 /* Did the user ask for any diff output? Run the diff! */
2927 if (revs->diffopt.output_format & ~DIFF_FORMAT_NO_OUTPUT)
2928 revs->diff = 1;
2930 /* Pickaxe, diff-filter and rename following need diffs */
2931 if ((revs->diffopt.pickaxe_opts & DIFF_PICKAXE_KINDS_MASK) ||
2932 revs->diffopt.filter ||
2933 revs->diffopt.flags.follow_renames)
2934 revs->diff = 1;
2936 if (revs->diffopt.objfind)
2937 revs->simplify_history = 0;
2939 if (revs->line_level_traverse) {
2940 if (want_ancestry(revs))
2941 revs->limited = 1;
2942 revs->topo_order = 1;
2945 if (revs->topo_order && !generation_numbers_enabled(the_repository))
2946 revs->limited = 1;
2948 if (revs->prune_data.nr) {
2949 copy_pathspec(&revs->pruning.pathspec, &revs->prune_data);
2950 /* Can't prune commits with rename following: the paths change.. */
2951 if (!revs->diffopt.flags.follow_renames)
2952 revs->prune = 1;
2953 if (!revs->full_diff)
2954 copy_pathspec(&revs->diffopt.pathspec,
2955 &revs->prune_data);
2958 diff_merges_setup_revs(revs);
2960 revs->diffopt.abbrev = revs->abbrev;
2962 diff_setup_done(&revs->diffopt);
2964 if (!is_encoding_utf8(get_log_output_encoding()))
2965 revs->grep_filter.ignore_locale = 1;
2966 compile_grep_patterns(&revs->grep_filter);
2968 if (revs->reverse && revs->reflog_info)
2969 die(_("options '%s' and '%s' cannot be used together"), "--reverse", "--walk-reflogs");
2970 if (revs->reflog_info && revs->limited)
2971 die("cannot combine --walk-reflogs with history-limiting options");
2972 if (revs->rewrite_parents && revs->children.name)
2973 die(_("options '%s' and '%s' cannot be used together"), "--parents", "--children");
2974 if (revs->filter.choice && !revs->blob_objects)
2975 die(_("object filtering requires --objects"));
2978 * Limitations on the graph functionality
2980 if (revs->reverse && revs->graph)
2981 die(_("options '%s' and '%s' cannot be used together"), "--reverse", "--graph");
2983 if (revs->reflog_info && revs->graph)
2984 die(_("options '%s' and '%s' cannot be used together"), "--walk-reflogs", "--graph");
2985 if (revs->no_walk && revs->graph)
2986 die(_("options '%s' and '%s' cannot be used together"), "--no-walk", "--graph");
2987 if (!revs->reflog_info && revs->grep_filter.use_reflog_filter)
2988 die(_("the option '%s' requires '%s'"), "--grep-reflog", "--walk-reflogs");
2990 if (revs->line_level_traverse &&
2991 (revs->diffopt.output_format & ~(DIFF_FORMAT_PATCH | DIFF_FORMAT_NO_OUTPUT)))
2992 die(_("-L does not yet support diff formats besides -p and -s"));
2994 if (revs->expand_tabs_in_log < 0)
2995 revs->expand_tabs_in_log = revs->expand_tabs_in_log_default;
2997 return left;
3000 static void release_revisions_cmdline(struct rev_cmdline_info *cmdline)
3002 unsigned int i;
3004 for (i = 0; i < cmdline->nr; i++)
3005 free((char *)cmdline->rev[i].name);
3006 free(cmdline->rev);
3009 static void release_revisions_mailmap(struct string_list *mailmap)
3011 if (!mailmap)
3012 return;
3013 clear_mailmap(mailmap);
3014 free(mailmap);
3017 static void release_revisions_topo_walk_info(struct topo_walk_info *info);
3019 void release_revisions(struct rev_info *revs)
3021 free_commit_list(revs->commits);
3022 free_commit_list(revs->ancestry_path_bottoms);
3023 object_array_clear(&revs->pending);
3024 object_array_clear(&revs->boundary_commits);
3025 release_revisions_cmdline(&revs->cmdline);
3026 list_objects_filter_release(&revs->filter);
3027 clear_pathspec(&revs->prune_data);
3028 date_mode_release(&revs->date_mode);
3029 release_revisions_mailmap(revs->mailmap);
3030 free_grep_patterns(&revs->grep_filter);
3031 /* TODO (need to handle "no_free"): diff_free(&revs->diffopt) */
3032 diff_free(&revs->pruning);
3033 reflog_walk_info_release(revs->reflog_info);
3034 release_revisions_topo_walk_info(revs->topo_walk_info);
3037 static void add_child(struct rev_info *revs, struct commit *parent, struct commit *child)
3039 struct commit_list *l = xcalloc(1, sizeof(*l));
3041 l->item = child;
3042 l->next = add_decoration(&revs->children, &parent->object, l);
3045 static int remove_duplicate_parents(struct rev_info *revs, struct commit *commit)
3047 struct treesame_state *ts = lookup_decoration(&revs->treesame, &commit->object);
3048 struct commit_list **pp, *p;
3049 int surviving_parents;
3051 /* Examine existing parents while marking ones we have seen... */
3052 pp = &commit->parents;
3053 surviving_parents = 0;
3054 while ((p = *pp) != NULL) {
3055 struct commit *parent = p->item;
3056 if (parent->object.flags & TMP_MARK) {
3057 *pp = p->next;
3058 if (ts)
3059 compact_treesame(revs, commit, surviving_parents);
3060 continue;
3062 parent->object.flags |= TMP_MARK;
3063 surviving_parents++;
3064 pp = &p->next;
3066 /* clear the temporary mark */
3067 for (p = commit->parents; p; p = p->next) {
3068 p->item->object.flags &= ~TMP_MARK;
3070 /* no update_treesame() - removing duplicates can't affect TREESAME */
3071 return surviving_parents;
3074 struct merge_simplify_state {
3075 struct commit *simplified;
3078 static struct merge_simplify_state *locate_simplify_state(struct rev_info *revs, struct commit *commit)
3080 struct merge_simplify_state *st;
3082 st = lookup_decoration(&revs->merge_simplification, &commit->object);
3083 if (!st) {
3084 CALLOC_ARRAY(st, 1);
3085 add_decoration(&revs->merge_simplification, &commit->object, st);
3087 return st;
3090 static int mark_redundant_parents(struct commit *commit)
3092 struct commit_list *h = reduce_heads(commit->parents);
3093 int i = 0, marked = 0;
3094 struct commit_list *po, *pn;
3096 /* Want these for sanity-checking only */
3097 int orig_cnt = commit_list_count(commit->parents);
3098 int cnt = commit_list_count(h);
3101 * Not ready to remove items yet, just mark them for now, based
3102 * on the output of reduce_heads(). reduce_heads outputs the reduced
3103 * set in its original order, so this isn't too hard.
3105 po = commit->parents;
3106 pn = h;
3107 while (po) {
3108 if (pn && po->item == pn->item) {
3109 pn = pn->next;
3110 i++;
3111 } else {
3112 po->item->object.flags |= TMP_MARK;
3113 marked++;
3115 po=po->next;
3118 if (i != cnt || cnt+marked != orig_cnt)
3119 die("mark_redundant_parents %d %d %d %d", orig_cnt, cnt, i, marked);
3121 free_commit_list(h);
3123 return marked;
3126 static int mark_treesame_root_parents(struct commit *commit)
3128 struct commit_list *p;
3129 int marked = 0;
3131 for (p = commit->parents; p; p = p->next) {
3132 struct commit *parent = p->item;
3133 if (!parent->parents && (parent->object.flags & TREESAME)) {
3134 parent->object.flags |= TMP_MARK;
3135 marked++;
3139 return marked;
3143 * Awkward naming - this means one parent we are TREESAME to.
3144 * cf mark_treesame_root_parents: root parents that are TREESAME (to an
3145 * empty tree). Better name suggestions?
3147 static int leave_one_treesame_to_parent(struct rev_info *revs, struct commit *commit)
3149 struct treesame_state *ts = lookup_decoration(&revs->treesame, &commit->object);
3150 struct commit *unmarked = NULL, *marked = NULL;
3151 struct commit_list *p;
3152 unsigned n;
3154 for (p = commit->parents, n = 0; p; p = p->next, n++) {
3155 if (ts->treesame[n]) {
3156 if (p->item->object.flags & TMP_MARK) {
3157 if (!marked)
3158 marked = p->item;
3159 } else {
3160 if (!unmarked) {
3161 unmarked = p->item;
3162 break;
3169 * If we are TREESAME to a marked-for-deletion parent, but not to any
3170 * unmarked parents, unmark the first TREESAME parent. This is the
3171 * parent that the default simplify_history==1 scan would have followed,
3172 * and it doesn't make sense to omit that path when asking for a
3173 * simplified full history. Retaining it improves the chances of
3174 * understanding odd missed merges that took an old version of a file.
3176 * Example:
3178 * I--------*X A modified the file, but mainline merge X used
3179 * \ / "-s ours", so took the version from I. X is
3180 * `-*A--' TREESAME to I and !TREESAME to A.
3182 * Default log from X would produce "I". Without this check,
3183 * --full-history --simplify-merges would produce "I-A-X", showing
3184 * the merge commit X and that it changed A, but not making clear that
3185 * it had just taken the I version. With this check, the topology above
3186 * is retained.
3188 * Note that it is possible that the simplification chooses a different
3189 * TREESAME parent from the default, in which case this test doesn't
3190 * activate, and we _do_ drop the default parent. Example:
3192 * I------X A modified the file, but it was reverted in B,
3193 * \ / meaning mainline merge X is TREESAME to both
3194 * *A-*B parents.
3196 * Default log would produce "I" by following the first parent;
3197 * --full-history --simplify-merges will produce "I-A-B". But this is a
3198 * reasonable result - it presents a logical full history leading from
3199 * I to X, and X is not an important merge.
3201 if (!unmarked && marked) {
3202 marked->object.flags &= ~TMP_MARK;
3203 return 1;
3206 return 0;
3209 static int remove_marked_parents(struct rev_info *revs, struct commit *commit)
3211 struct commit_list **pp, *p;
3212 int nth_parent, removed = 0;
3214 pp = &commit->parents;
3215 nth_parent = 0;
3216 while ((p = *pp) != NULL) {
3217 struct commit *parent = p->item;
3218 if (parent->object.flags & TMP_MARK) {
3219 parent->object.flags &= ~TMP_MARK;
3220 *pp = p->next;
3221 free(p);
3222 removed++;
3223 compact_treesame(revs, commit, nth_parent);
3224 continue;
3226 pp = &p->next;
3227 nth_parent++;
3230 /* Removing parents can only increase TREESAMEness */
3231 if (removed && !(commit->object.flags & TREESAME))
3232 update_treesame(revs, commit);
3234 return nth_parent;
3237 static struct commit_list **simplify_one(struct rev_info *revs, struct commit *commit, struct commit_list **tail)
3239 struct commit_list *p;
3240 struct commit *parent;
3241 struct merge_simplify_state *st, *pst;
3242 int cnt;
3244 st = locate_simplify_state(revs, commit);
3247 * Have we handled this one?
3249 if (st->simplified)
3250 return tail;
3253 * An UNINTERESTING commit simplifies to itself, so does a
3254 * root commit. We do not rewrite parents of such commit
3255 * anyway.
3257 if ((commit->object.flags & UNINTERESTING) || !commit->parents) {
3258 st->simplified = commit;
3259 return tail;
3263 * Do we know what commit all of our parents that matter
3264 * should be rewritten to? Otherwise we are not ready to
3265 * rewrite this one yet.
3267 for (cnt = 0, p = commit->parents; p; p = p->next) {
3268 pst = locate_simplify_state(revs, p->item);
3269 if (!pst->simplified) {
3270 tail = &commit_list_insert(p->item, tail)->next;
3271 cnt++;
3273 if (revs->first_parent_only)
3274 break;
3276 if (cnt) {
3277 tail = &commit_list_insert(commit, tail)->next;
3278 return tail;
3282 * Rewrite our list of parents. Note that this cannot
3283 * affect our TREESAME flags in any way - a commit is
3284 * always TREESAME to its simplification.
3286 for (p = commit->parents; p; p = p->next) {
3287 pst = locate_simplify_state(revs, p->item);
3288 p->item = pst->simplified;
3289 if (revs->first_parent_only)
3290 break;
3293 if (revs->first_parent_only)
3294 cnt = 1;
3295 else
3296 cnt = remove_duplicate_parents(revs, commit);
3299 * It is possible that we are a merge and one side branch
3300 * does not have any commit that touches the given paths;
3301 * in such a case, the immediate parent from that branch
3302 * will be rewritten to be the merge base.
3304 * o----X X: the commit we are looking at;
3305 * / / o: a commit that touches the paths;
3306 * ---o----'
3308 * Further, a merge of an independent branch that doesn't
3309 * touch the path will reduce to a treesame root parent:
3311 * ----o----X X: the commit we are looking at;
3312 * / o: a commit that touches the paths;
3313 * r r: a root commit not touching the paths
3315 * Detect and simplify both cases.
3317 if (1 < cnt) {
3318 int marked = mark_redundant_parents(commit);
3319 marked += mark_treesame_root_parents(commit);
3320 if (marked)
3321 marked -= leave_one_treesame_to_parent(revs, commit);
3322 if (marked)
3323 cnt = remove_marked_parents(revs, commit);
3327 * A commit simplifies to itself if it is a root, if it is
3328 * UNINTERESTING, if it touches the given paths, or if it is a
3329 * merge and its parents don't simplify to one relevant commit
3330 * (the first two cases are already handled at the beginning of
3331 * this function).
3333 * Otherwise, it simplifies to what its sole relevant parent
3334 * simplifies to.
3336 if (!cnt ||
3337 (commit->object.flags & UNINTERESTING) ||
3338 !(commit->object.flags & TREESAME) ||
3339 (parent = one_relevant_parent(revs, commit->parents)) == NULL ||
3340 (revs->show_pulls && (commit->object.flags & PULL_MERGE)))
3341 st->simplified = commit;
3342 else {
3343 pst = locate_simplify_state(revs, parent);
3344 st->simplified = pst->simplified;
3346 return tail;
3349 static void simplify_merges(struct rev_info *revs)
3351 struct commit_list *list, *next;
3352 struct commit_list *yet_to_do, **tail;
3353 struct commit *commit;
3355 if (!revs->prune)
3356 return;
3358 /* feed the list reversed */
3359 yet_to_do = NULL;
3360 for (list = revs->commits; list; list = next) {
3361 commit = list->item;
3362 next = list->next;
3364 * Do not free(list) here yet; the original list
3365 * is used later in this function.
3367 commit_list_insert(commit, &yet_to_do);
3369 while (yet_to_do) {
3370 list = yet_to_do;
3371 yet_to_do = NULL;
3372 tail = &yet_to_do;
3373 while (list) {
3374 commit = pop_commit(&list);
3375 tail = simplify_one(revs, commit, tail);
3379 /* clean up the result, removing the simplified ones */
3380 list = revs->commits;
3381 revs->commits = NULL;
3382 tail = &revs->commits;
3383 while (list) {
3384 struct merge_simplify_state *st;
3386 commit = pop_commit(&list);
3387 st = locate_simplify_state(revs, commit);
3388 if (st->simplified == commit)
3389 tail = &commit_list_insert(commit, tail)->next;
3393 static void set_children(struct rev_info *revs)
3395 struct commit_list *l;
3396 for (l = revs->commits; l; l = l->next) {
3397 struct commit *commit = l->item;
3398 struct commit_list *p;
3400 for (p = commit->parents; p; p = p->next)
3401 add_child(revs, p->item, commit);
3405 void reset_revision_walk(void)
3407 clear_object_flags(SEEN | ADDED | SHOWN | TOPO_WALK_EXPLORED | TOPO_WALK_INDEGREE);
3410 static int mark_uninteresting(const struct object_id *oid,
3411 struct packed_git *pack,
3412 uint32_t pos,
3413 void *cb)
3415 struct rev_info *revs = cb;
3416 struct object *o = lookup_unknown_object(revs->repo, oid);
3417 o->flags |= UNINTERESTING | SEEN;
3418 return 0;
3421 define_commit_slab(indegree_slab, int);
3422 define_commit_slab(author_date_slab, timestamp_t);
3424 struct topo_walk_info {
3425 timestamp_t min_generation;
3426 struct prio_queue explore_queue;
3427 struct prio_queue indegree_queue;
3428 struct prio_queue topo_queue;
3429 struct indegree_slab indegree;
3430 struct author_date_slab author_date;
3433 static int topo_walk_atexit_registered;
3434 static unsigned int count_explore_walked;
3435 static unsigned int count_indegree_walked;
3436 static unsigned int count_topo_walked;
3438 static void trace2_topo_walk_statistics_atexit(void)
3440 struct json_writer jw = JSON_WRITER_INIT;
3442 jw_object_begin(&jw, 0);
3443 jw_object_intmax(&jw, "count_explore_walked", count_explore_walked);
3444 jw_object_intmax(&jw, "count_indegree_walked", count_indegree_walked);
3445 jw_object_intmax(&jw, "count_topo_walked", count_topo_walked);
3446 jw_end(&jw);
3448 trace2_data_json("topo_walk", the_repository, "statistics", &jw);
3450 jw_release(&jw);
3453 static inline void test_flag_and_insert(struct prio_queue *q, struct commit *c, int flag)
3455 if (c->object.flags & flag)
3456 return;
3458 c->object.flags |= flag;
3459 prio_queue_put(q, c);
3462 static void explore_walk_step(struct rev_info *revs)
3464 struct topo_walk_info *info = revs->topo_walk_info;
3465 struct commit_list *p;
3466 struct commit *c = prio_queue_get(&info->explore_queue);
3468 if (!c)
3469 return;
3471 if (repo_parse_commit_gently(revs->repo, c, 1) < 0)
3472 return;
3474 count_explore_walked++;
3476 if (revs->sort_order == REV_SORT_BY_AUTHOR_DATE)
3477 record_author_date(&info->author_date, c);
3479 if (revs->max_age != -1 && (c->date < revs->max_age))
3480 c->object.flags |= UNINTERESTING;
3482 if (process_parents(revs, c, NULL, NULL) < 0)
3483 return;
3485 if (c->object.flags & UNINTERESTING)
3486 mark_parents_uninteresting(revs, c);
3488 for (p = c->parents; p; p = p->next)
3489 test_flag_and_insert(&info->explore_queue, p->item, TOPO_WALK_EXPLORED);
3492 static void explore_to_depth(struct rev_info *revs,
3493 timestamp_t gen_cutoff)
3495 struct topo_walk_info *info = revs->topo_walk_info;
3496 struct commit *c;
3497 while ((c = prio_queue_peek(&info->explore_queue)) &&
3498 commit_graph_generation(c) >= gen_cutoff)
3499 explore_walk_step(revs);
3502 static void indegree_walk_step(struct rev_info *revs)
3504 struct commit_list *p;
3505 struct topo_walk_info *info = revs->topo_walk_info;
3506 struct commit *c = prio_queue_get(&info->indegree_queue);
3508 if (!c)
3509 return;
3511 if (repo_parse_commit_gently(revs->repo, c, 1) < 0)
3512 return;
3514 count_indegree_walked++;
3516 explore_to_depth(revs, commit_graph_generation(c));
3518 for (p = c->parents; p; p = p->next) {
3519 struct commit *parent = p->item;
3520 int *pi = indegree_slab_at(&info->indegree, parent);
3522 if (repo_parse_commit_gently(revs->repo, parent, 1) < 0)
3523 return;
3525 if (*pi)
3526 (*pi)++;
3527 else
3528 *pi = 2;
3530 test_flag_and_insert(&info->indegree_queue, parent, TOPO_WALK_INDEGREE);
3532 if (revs->first_parent_only)
3533 return;
3537 static void compute_indegrees_to_depth(struct rev_info *revs,
3538 timestamp_t gen_cutoff)
3540 struct topo_walk_info *info = revs->topo_walk_info;
3541 struct commit *c;
3542 while ((c = prio_queue_peek(&info->indegree_queue)) &&
3543 commit_graph_generation(c) >= gen_cutoff)
3544 indegree_walk_step(revs);
3547 static void release_revisions_topo_walk_info(struct topo_walk_info *info)
3549 if (!info)
3550 return;
3551 clear_prio_queue(&info->explore_queue);
3552 clear_prio_queue(&info->indegree_queue);
3553 clear_prio_queue(&info->topo_queue);
3554 clear_indegree_slab(&info->indegree);
3555 clear_author_date_slab(&info->author_date);
3556 free(info);
3559 static void reset_topo_walk(struct rev_info *revs)
3561 release_revisions_topo_walk_info(revs->topo_walk_info);
3562 revs->topo_walk_info = NULL;
3565 static void init_topo_walk(struct rev_info *revs)
3567 struct topo_walk_info *info;
3568 struct commit_list *list;
3569 if (revs->topo_walk_info)
3570 reset_topo_walk(revs);
3572 revs->topo_walk_info = xmalloc(sizeof(struct topo_walk_info));
3573 info = revs->topo_walk_info;
3574 memset(info, 0, sizeof(struct topo_walk_info));
3576 init_indegree_slab(&info->indegree);
3577 memset(&info->explore_queue, 0, sizeof(info->explore_queue));
3578 memset(&info->indegree_queue, 0, sizeof(info->indegree_queue));
3579 memset(&info->topo_queue, 0, sizeof(info->topo_queue));
3581 switch (revs->sort_order) {
3582 default: /* REV_SORT_IN_GRAPH_ORDER */
3583 info->topo_queue.compare = NULL;
3584 break;
3585 case REV_SORT_BY_COMMIT_DATE:
3586 info->topo_queue.compare = compare_commits_by_commit_date;
3587 break;
3588 case REV_SORT_BY_AUTHOR_DATE:
3589 init_author_date_slab(&info->author_date);
3590 info->topo_queue.compare = compare_commits_by_author_date;
3591 info->topo_queue.cb_data = &info->author_date;
3592 break;
3595 info->explore_queue.compare = compare_commits_by_gen_then_commit_date;
3596 info->indegree_queue.compare = compare_commits_by_gen_then_commit_date;
3598 info->min_generation = GENERATION_NUMBER_INFINITY;
3599 for (list = revs->commits; list; list = list->next) {
3600 struct commit *c = list->item;
3601 timestamp_t generation;
3603 if (repo_parse_commit_gently(revs->repo, c, 1))
3604 continue;
3606 test_flag_and_insert(&info->explore_queue, c, TOPO_WALK_EXPLORED);
3607 test_flag_and_insert(&info->indegree_queue, c, TOPO_WALK_INDEGREE);
3609 generation = commit_graph_generation(c);
3610 if (generation < info->min_generation)
3611 info->min_generation = generation;
3613 *(indegree_slab_at(&info->indegree, c)) = 1;
3615 if (revs->sort_order == REV_SORT_BY_AUTHOR_DATE)
3616 record_author_date(&info->author_date, c);
3618 compute_indegrees_to_depth(revs, info->min_generation);
3620 for (list = revs->commits; list; list = list->next) {
3621 struct commit *c = list->item;
3623 if (*(indegree_slab_at(&info->indegree, c)) == 1)
3624 prio_queue_put(&info->topo_queue, c);
3628 * This is unfortunate; the initial tips need to be shown
3629 * in the order given from the revision traversal machinery.
3631 if (revs->sort_order == REV_SORT_IN_GRAPH_ORDER)
3632 prio_queue_reverse(&info->topo_queue);
3634 if (trace2_is_enabled() && !topo_walk_atexit_registered) {
3635 atexit(trace2_topo_walk_statistics_atexit);
3636 topo_walk_atexit_registered = 1;
3640 static struct commit *next_topo_commit(struct rev_info *revs)
3642 struct commit *c;
3643 struct topo_walk_info *info = revs->topo_walk_info;
3645 /* pop next off of topo_queue */
3646 c = prio_queue_get(&info->topo_queue);
3648 if (c)
3649 *(indegree_slab_at(&info->indegree, c)) = 0;
3651 return c;
3654 static void expand_topo_walk(struct rev_info *revs, struct commit *commit)
3656 struct commit_list *p;
3657 struct topo_walk_info *info = revs->topo_walk_info;
3658 if (process_parents(revs, commit, NULL, NULL) < 0) {
3659 if (!revs->ignore_missing_links)
3660 die("Failed to traverse parents of commit %s",
3661 oid_to_hex(&commit->object.oid));
3664 count_topo_walked++;
3666 for (p = commit->parents; p; p = p->next) {
3667 struct commit *parent = p->item;
3668 int *pi;
3669 timestamp_t generation;
3671 if (parent->object.flags & UNINTERESTING)
3672 continue;
3674 if (repo_parse_commit_gently(revs->repo, parent, 1) < 0)
3675 continue;
3677 generation = commit_graph_generation(parent);
3678 if (generation < info->min_generation) {
3679 info->min_generation = generation;
3680 compute_indegrees_to_depth(revs, info->min_generation);
3683 pi = indegree_slab_at(&info->indegree, parent);
3685 (*pi)--;
3686 if (*pi == 1)
3687 prio_queue_put(&info->topo_queue, parent);
3689 if (revs->first_parent_only)
3690 return;
3694 int prepare_revision_walk(struct rev_info *revs)
3696 int i;
3697 struct object_array old_pending;
3698 struct commit_list **next = &revs->commits;
3700 memcpy(&old_pending, &revs->pending, sizeof(old_pending));
3701 revs->pending.nr = 0;
3702 revs->pending.alloc = 0;
3703 revs->pending.objects = NULL;
3704 for (i = 0; i < old_pending.nr; i++) {
3705 struct object_array_entry *e = old_pending.objects + i;
3706 struct commit *commit = handle_commit(revs, e);
3707 if (commit) {
3708 if (!(commit->object.flags & SEEN)) {
3709 commit->object.flags |= SEEN;
3710 next = commit_list_append(commit, next);
3714 object_array_clear(&old_pending);
3716 /* Signal whether we need per-parent treesame decoration */
3717 if (revs->simplify_merges ||
3718 (revs->limited && limiting_can_increase_treesame(revs)))
3719 revs->treesame.name = "treesame";
3721 if (revs->exclude_promisor_objects) {
3722 for_each_packed_object(mark_uninteresting, revs,
3723 FOR_EACH_OBJECT_PROMISOR_ONLY);
3726 if (!revs->reflog_info)
3727 prepare_to_use_bloom_filter(revs);
3728 if (!revs->unsorted_input)
3729 commit_list_sort_by_date(&revs->commits);
3730 if (revs->no_walk)
3731 return 0;
3732 if (revs->limited) {
3733 if (limit_list(revs) < 0)
3734 return -1;
3735 if (revs->topo_order)
3736 sort_in_topological_order(&revs->commits, revs->sort_order);
3737 } else if (revs->topo_order)
3738 init_topo_walk(revs);
3739 if (revs->line_level_traverse && want_ancestry(revs))
3741 * At the moment we can only do line-level log with parent
3742 * rewriting by performing this expensive pre-filtering step.
3743 * If parent rewriting is not requested, then we rather
3744 * perform the line-level log filtering during the regular
3745 * history traversal.
3747 line_log_filter(revs);
3748 if (revs->simplify_merges)
3749 simplify_merges(revs);
3750 if (revs->children.name)
3751 set_children(revs);
3753 return 0;
3756 static enum rewrite_result rewrite_one_1(struct rev_info *revs,
3757 struct commit **pp,
3758 struct prio_queue *queue)
3760 for (;;) {
3761 struct commit *p = *pp;
3762 if (!revs->limited)
3763 if (process_parents(revs, p, NULL, queue) < 0)
3764 return rewrite_one_error;
3765 if (p->object.flags & UNINTERESTING)
3766 return rewrite_one_ok;
3767 if (!(p->object.flags & TREESAME))
3768 return rewrite_one_ok;
3769 if (!p->parents)
3770 return rewrite_one_noparents;
3771 if (!(p = one_relevant_parent(revs, p->parents)))
3772 return rewrite_one_ok;
3773 *pp = p;
3777 static void merge_queue_into_list(struct prio_queue *q, struct commit_list **list)
3779 while (q->nr) {
3780 struct commit *item = prio_queue_peek(q);
3781 struct commit_list *p = *list;
3783 if (p && p->item->date >= item->date)
3784 list = &p->next;
3785 else {
3786 p = commit_list_insert(item, list);
3787 list = &p->next; /* skip newly added item */
3788 prio_queue_get(q); /* pop item */
3793 static enum rewrite_result rewrite_one(struct rev_info *revs, struct commit **pp)
3795 struct prio_queue queue = { compare_commits_by_commit_date };
3796 enum rewrite_result ret = rewrite_one_1(revs, pp, &queue);
3797 merge_queue_into_list(&queue, &revs->commits);
3798 clear_prio_queue(&queue);
3799 return ret;
3802 int rewrite_parents(struct rev_info *revs, struct commit *commit,
3803 rewrite_parent_fn_t rewrite_parent)
3805 struct commit_list **pp = &commit->parents;
3806 while (*pp) {
3807 struct commit_list *parent = *pp;
3808 switch (rewrite_parent(revs, &parent->item)) {
3809 case rewrite_one_ok:
3810 break;
3811 case rewrite_one_noparents:
3812 *pp = parent->next;
3813 continue;
3814 case rewrite_one_error:
3815 return -1;
3817 pp = &parent->next;
3819 remove_duplicate_parents(revs, commit);
3820 return 0;
3823 static int commit_match(struct commit *commit, struct rev_info *opt)
3825 int retval;
3826 const char *encoding;
3827 const char *message;
3828 struct strbuf buf = STRBUF_INIT;
3830 if (!opt->grep_filter.pattern_list && !opt->grep_filter.header_list)
3831 return 1;
3833 /* Prepend "fake" headers as needed */
3834 if (opt->grep_filter.use_reflog_filter) {
3835 strbuf_addstr(&buf, "reflog ");
3836 get_reflog_message(&buf, opt->reflog_info);
3837 strbuf_addch(&buf, '\n');
3841 * We grep in the user's output encoding, under the assumption that it
3842 * is the encoding they are most likely to write their grep pattern
3843 * for. In addition, it means we will match the "notes" encoding below,
3844 * so we will not end up with a buffer that has two different encodings
3845 * in it.
3847 encoding = get_log_output_encoding();
3848 message = logmsg_reencode(commit, NULL, encoding);
3850 /* Copy the commit to temporary if we are using "fake" headers */
3851 if (buf.len)
3852 strbuf_addstr(&buf, message);
3854 if (opt->grep_filter.header_list && opt->mailmap) {
3855 const char *commit_headers[] = { "author ", "committer ", NULL };
3857 if (!buf.len)
3858 strbuf_addstr(&buf, message);
3860 apply_mailmap_to_header(&buf, commit_headers, opt->mailmap);
3863 /* Append "fake" message parts as needed */
3864 if (opt->show_notes) {
3865 if (!buf.len)
3866 strbuf_addstr(&buf, message);
3867 format_display_notes(&commit->object.oid, &buf, encoding, 1);
3871 * Find either in the original commit message, or in the temporary.
3872 * Note that we cast away the constness of "message" here. It is
3873 * const because it may come from the cached commit buffer. That's OK,
3874 * because we know that it is modifiable heap memory, and that while
3875 * grep_buffer may modify it for speed, it will restore any
3876 * changes before returning.
3878 if (buf.len)
3879 retval = grep_buffer(&opt->grep_filter, buf.buf, buf.len);
3880 else
3881 retval = grep_buffer(&opt->grep_filter,
3882 (char *)message, strlen(message));
3883 strbuf_release(&buf);
3884 unuse_commit_buffer(commit, message);
3885 return retval;
3888 static inline int want_ancestry(const struct rev_info *revs)
3890 return (revs->rewrite_parents || revs->children.name);
3894 * Return a timestamp to be used for --since/--until comparisons for this
3895 * commit, based on the revision options.
3897 static timestamp_t comparison_date(const struct rev_info *revs,
3898 struct commit *commit)
3900 return revs->reflog_info ?
3901 get_reflog_timestamp(revs->reflog_info) :
3902 commit->date;
3905 enum commit_action get_commit_action(struct rev_info *revs, struct commit *commit)
3907 if (commit->object.flags & SHOWN)
3908 return commit_ignore;
3909 if (revs->unpacked && has_object_pack(&commit->object.oid))
3910 return commit_ignore;
3911 if (revs->no_kept_objects) {
3912 if (has_object_kept_pack(&commit->object.oid,
3913 revs->keep_pack_cache_flags))
3914 return commit_ignore;
3916 if (commit->object.flags & UNINTERESTING)
3917 return commit_ignore;
3918 if (revs->line_level_traverse && !want_ancestry(revs)) {
3920 * In case of line-level log with parent rewriting
3921 * prepare_revision_walk() already took care of all line-level
3922 * log filtering, and there is nothing left to do here.
3924 * If parent rewriting was not requested, then this is the
3925 * place to perform the line-level log filtering. Notably,
3926 * this check, though expensive, must come before the other,
3927 * cheaper filtering conditions, because the tracked line
3928 * ranges must be adjusted even when the commit will end up
3929 * being ignored based on other conditions.
3931 if (!line_log_process_ranges_arbitrary_commit(revs, commit))
3932 return commit_ignore;
3934 if (revs->min_age != -1 &&
3935 comparison_date(revs, commit) > revs->min_age)
3936 return commit_ignore;
3937 if (revs->max_age_as_filter != -1 &&
3938 comparison_date(revs, commit) < revs->max_age_as_filter)
3939 return commit_ignore;
3940 if (revs->min_parents || (revs->max_parents >= 0)) {
3941 int n = commit_list_count(commit->parents);
3942 if ((n < revs->min_parents) ||
3943 ((revs->max_parents >= 0) && (n > revs->max_parents)))
3944 return commit_ignore;
3946 if (!commit_match(commit, revs))
3947 return commit_ignore;
3948 if (revs->prune && revs->dense) {
3949 /* Commit without changes? */
3950 if (commit->object.flags & TREESAME) {
3951 int n;
3952 struct commit_list *p;
3953 /* drop merges unless we want parenthood */
3954 if (!want_ancestry(revs))
3955 return commit_ignore;
3957 if (revs->show_pulls && (commit->object.flags & PULL_MERGE))
3958 return commit_show;
3961 * If we want ancestry, then need to keep any merges
3962 * between relevant commits to tie together topology.
3963 * For consistency with TREESAME and simplification
3964 * use "relevant" here rather than just INTERESTING,
3965 * to treat bottom commit(s) as part of the topology.
3967 for (n = 0, p = commit->parents; p; p = p->next)
3968 if (relevant_commit(p->item))
3969 if (++n >= 2)
3970 return commit_show;
3971 return commit_ignore;
3974 return commit_show;
3977 define_commit_slab(saved_parents, struct commit_list *);
3979 #define EMPTY_PARENT_LIST ((struct commit_list *)-1)
3982 * You may only call save_parents() once per commit (this is checked
3983 * for non-root commits).
3985 static void save_parents(struct rev_info *revs, struct commit *commit)
3987 struct commit_list **pp;
3989 if (!revs->saved_parents_slab) {
3990 revs->saved_parents_slab = xmalloc(sizeof(struct saved_parents));
3991 init_saved_parents(revs->saved_parents_slab);
3994 pp = saved_parents_at(revs->saved_parents_slab, commit);
3997 * When walking with reflogs, we may visit the same commit
3998 * several times: once for each appearance in the reflog.
4000 * In this case, save_parents() will be called multiple times.
4001 * We want to keep only the first set of parents. We need to
4002 * store a sentinel value for an empty (i.e., NULL) parent
4003 * list to distinguish it from a not-yet-saved list, however.
4005 if (*pp)
4006 return;
4007 if (commit->parents)
4008 *pp = copy_commit_list(commit->parents);
4009 else
4010 *pp = EMPTY_PARENT_LIST;
4013 static void free_saved_parents(struct rev_info *revs)
4015 if (revs->saved_parents_slab)
4016 clear_saved_parents(revs->saved_parents_slab);
4019 struct commit_list *get_saved_parents(struct rev_info *revs, const struct commit *commit)
4021 struct commit_list *parents;
4023 if (!revs->saved_parents_slab)
4024 return commit->parents;
4026 parents = *saved_parents_at(revs->saved_parents_slab, commit);
4027 if (parents == EMPTY_PARENT_LIST)
4028 return NULL;
4029 return parents;
4032 enum commit_action simplify_commit(struct rev_info *revs, struct commit *commit)
4034 enum commit_action action = get_commit_action(revs, commit);
4036 if (action == commit_show &&
4037 revs->prune && revs->dense && want_ancestry(revs)) {
4039 * --full-diff on simplified parents is no good: it
4040 * will show spurious changes from the commits that
4041 * were elided. So we save the parents on the side
4042 * when --full-diff is in effect.
4044 if (revs->full_diff)
4045 save_parents(revs, commit);
4046 if (rewrite_parents(revs, commit, rewrite_one) < 0)
4047 return commit_error;
4049 return action;
4052 static void track_linear(struct rev_info *revs, struct commit *commit)
4054 if (revs->track_first_time) {
4055 revs->linear = 1;
4056 revs->track_first_time = 0;
4057 } else {
4058 struct commit_list *p;
4059 for (p = revs->previous_parents; p; p = p->next)
4060 if (p->item == NULL || /* first commit */
4061 oideq(&p->item->object.oid, &commit->object.oid))
4062 break;
4063 revs->linear = p != NULL;
4065 if (revs->reverse) {
4066 if (revs->linear)
4067 commit->object.flags |= TRACK_LINEAR;
4069 free_commit_list(revs->previous_parents);
4070 revs->previous_parents = copy_commit_list(commit->parents);
4073 static struct commit *get_revision_1(struct rev_info *revs)
4075 while (1) {
4076 struct commit *commit;
4078 if (revs->reflog_info)
4079 commit = next_reflog_entry(revs->reflog_info);
4080 else if (revs->topo_walk_info)
4081 commit = next_topo_commit(revs);
4082 else
4083 commit = pop_commit(&revs->commits);
4085 if (!commit)
4086 return NULL;
4088 if (revs->reflog_info)
4089 commit->object.flags &= ~(ADDED | SEEN | SHOWN);
4092 * If we haven't done the list limiting, we need to look at
4093 * the parents here. We also need to do the date-based limiting
4094 * that we'd otherwise have done in limit_list().
4096 if (!revs->limited) {
4097 if (revs->max_age != -1 &&
4098 comparison_date(revs, commit) < revs->max_age)
4099 continue;
4101 if (revs->reflog_info)
4102 try_to_simplify_commit(revs, commit);
4103 else if (revs->topo_walk_info)
4104 expand_topo_walk(revs, commit);
4105 else if (process_parents(revs, commit, &revs->commits, NULL) < 0) {
4106 if (!revs->ignore_missing_links)
4107 die("Failed to traverse parents of commit %s",
4108 oid_to_hex(&commit->object.oid));
4112 switch (simplify_commit(revs, commit)) {
4113 case commit_ignore:
4114 continue;
4115 case commit_error:
4116 die("Failed to simplify parents of commit %s",
4117 oid_to_hex(&commit->object.oid));
4118 default:
4119 if (revs->track_linear)
4120 track_linear(revs, commit);
4121 return commit;
4127 * Return true for entries that have not yet been shown. (This is an
4128 * object_array_each_func_t.)
4130 static int entry_unshown(struct object_array_entry *entry, void *cb_data_unused)
4132 return !(entry->item->flags & SHOWN);
4136 * If array is on the verge of a realloc, garbage-collect any entries
4137 * that have already been shown to try to free up some space.
4139 static void gc_boundary(struct object_array *array)
4141 if (array->nr == array->alloc)
4142 object_array_filter(array, entry_unshown, NULL);
4145 static void create_boundary_commit_list(struct rev_info *revs)
4147 unsigned i;
4148 struct commit *c;
4149 struct object_array *array = &revs->boundary_commits;
4150 struct object_array_entry *objects = array->objects;
4153 * If revs->commits is non-NULL at this point, an error occurred in
4154 * get_revision_1(). Ignore the error and continue printing the
4155 * boundary commits anyway. (This is what the code has always
4156 * done.)
4158 free_commit_list(revs->commits);
4159 revs->commits = NULL;
4162 * Put all of the actual boundary commits from revs->boundary_commits
4163 * into revs->commits
4165 for (i = 0; i < array->nr; i++) {
4166 c = (struct commit *)(objects[i].item);
4167 if (!c)
4168 continue;
4169 if (!(c->object.flags & CHILD_SHOWN))
4170 continue;
4171 if (c->object.flags & (SHOWN | BOUNDARY))
4172 continue;
4173 c->object.flags |= BOUNDARY;
4174 commit_list_insert(c, &revs->commits);
4178 * If revs->topo_order is set, sort the boundary commits
4179 * in topological order
4181 sort_in_topological_order(&revs->commits, revs->sort_order);
4184 static struct commit *get_revision_internal(struct rev_info *revs)
4186 struct commit *c = NULL;
4187 struct commit_list *l;
4189 if (revs->boundary == 2) {
4191 * All of the normal commits have already been returned,
4192 * and we are now returning boundary commits.
4193 * create_boundary_commit_list() has populated
4194 * revs->commits with the remaining commits to return.
4196 c = pop_commit(&revs->commits);
4197 if (c)
4198 c->object.flags |= SHOWN;
4199 return c;
4203 * If our max_count counter has reached zero, then we are done. We
4204 * don't simply return NULL because we still might need to show
4205 * boundary commits. But we want to avoid calling get_revision_1, which
4206 * might do a considerable amount of work finding the next commit only
4207 * for us to throw it away.
4209 * If it is non-zero, then either we don't have a max_count at all
4210 * (-1), or it is still counting, in which case we decrement.
4212 if (revs->max_count) {
4213 c = get_revision_1(revs);
4214 if (c) {
4215 while (revs->skip_count > 0) {
4216 revs->skip_count--;
4217 c = get_revision_1(revs);
4218 if (!c)
4219 break;
4223 if (revs->max_count > 0)
4224 revs->max_count--;
4227 if (c)
4228 c->object.flags |= SHOWN;
4230 if (!revs->boundary)
4231 return c;
4233 if (!c) {
4235 * get_revision_1() runs out the commits, and
4236 * we are done computing the boundaries.
4237 * switch to boundary commits output mode.
4239 revs->boundary = 2;
4242 * Update revs->commits to contain the list of
4243 * boundary commits.
4245 create_boundary_commit_list(revs);
4247 return get_revision_internal(revs);
4251 * boundary commits are the commits that are parents of the
4252 * ones we got from get_revision_1() but they themselves are
4253 * not returned from get_revision_1(). Before returning
4254 * 'c', we need to mark its parents that they could be boundaries.
4257 for (l = c->parents; l; l = l->next) {
4258 struct object *p;
4259 p = &(l->item->object);
4260 if (p->flags & (CHILD_SHOWN | SHOWN))
4261 continue;
4262 p->flags |= CHILD_SHOWN;
4263 gc_boundary(&revs->boundary_commits);
4264 add_object_array(p, NULL, &revs->boundary_commits);
4267 return c;
4270 struct commit *get_revision(struct rev_info *revs)
4272 struct commit *c;
4273 struct commit_list *reversed;
4275 if (revs->reverse) {
4276 reversed = NULL;
4277 while ((c = get_revision_internal(revs)))
4278 commit_list_insert(c, &reversed);
4279 revs->commits = reversed;
4280 revs->reverse = 0;
4281 revs->reverse_output_stage = 1;
4284 if (revs->reverse_output_stage) {
4285 c = pop_commit(&revs->commits);
4286 if (revs->track_linear)
4287 revs->linear = !!(c && c->object.flags & TRACK_LINEAR);
4288 return c;
4291 c = get_revision_internal(revs);
4292 if (c && revs->graph)
4293 graph_update(revs->graph, c);
4294 if (!c) {
4295 free_saved_parents(revs);
4296 free_commit_list(revs->previous_parents);
4297 revs->previous_parents = NULL;
4299 return c;
4302 const char *get_revision_mark(const struct rev_info *revs, const struct commit *commit)
4304 if (commit->object.flags & BOUNDARY)
4305 return "-";
4306 else if (commit->object.flags & UNINTERESTING)
4307 return "^";
4308 else if (commit->object.flags & PATCHSAME)
4309 return "=";
4310 else if (!revs || revs->left_right) {
4311 if (commit->object.flags & SYMMETRIC_LEFT)
4312 return "<";
4313 else
4314 return ">";
4315 } else if (revs->graph)
4316 return "*";
4317 else if (revs->cherry_mark)
4318 return "+";
4319 return "";
4322 void put_revision_mark(const struct rev_info *revs, const struct commit *commit)
4324 const char *mark = get_revision_mark(revs, commit);
4325 if (!strlen(mark))
4326 return;
4327 fputs(mark, stdout);
4328 putchar(' ');