l10n: bg.po: Updated Bulgarian translation (5482t)
[alt-git.git] / revision.c
blobd5f4463cb6b789c28c86c68e39aa621cd50d26ed
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);
1912 static void add_pending_commit_list(struct rev_info *revs,
1913 struct commit_list *commit_list,
1914 unsigned int flags)
1916 while (commit_list) {
1917 struct object *object = &commit_list->item->object;
1918 object->flags |= flags;
1919 add_pending_object(revs, object, oid_to_hex(&object->oid));
1920 commit_list = commit_list->next;
1924 static void prepare_show_merge(struct rev_info *revs)
1926 struct commit_list *bases;
1927 struct commit *head, *other;
1928 struct object_id oid;
1929 const char **prune = NULL;
1930 int i, prune_num = 1; /* counting terminating NULL */
1931 struct index_state *istate = revs->repo->index;
1933 if (get_oid("HEAD", &oid))
1934 die("--merge without HEAD?");
1935 head = lookup_commit_or_die(&oid, "HEAD");
1936 if (get_oid("MERGE_HEAD", &oid))
1937 die("--merge without MERGE_HEAD?");
1938 other = lookup_commit_or_die(&oid, "MERGE_HEAD");
1939 add_pending_object(revs, &head->object, "HEAD");
1940 add_pending_object(revs, &other->object, "MERGE_HEAD");
1941 bases = get_merge_bases(head, other);
1942 add_rev_cmdline_list(revs, bases, REV_CMD_MERGE_BASE, UNINTERESTING | BOTTOM);
1943 add_pending_commit_list(revs, bases, UNINTERESTING | BOTTOM);
1944 free_commit_list(bases);
1945 head->object.flags |= SYMMETRIC_LEFT;
1947 if (!istate->cache_nr)
1948 repo_read_index(revs->repo);
1949 for (i = 0; i < istate->cache_nr; i++) {
1950 const struct cache_entry *ce = istate->cache[i];
1951 if (!ce_stage(ce))
1952 continue;
1953 if (ce_path_match(istate, ce, &revs->prune_data, NULL)) {
1954 prune_num++;
1955 REALLOC_ARRAY(prune, prune_num);
1956 prune[prune_num-2] = ce->name;
1957 prune[prune_num-1] = NULL;
1959 while ((i+1 < istate->cache_nr) &&
1960 ce_same_name(ce, istate->cache[i+1]))
1961 i++;
1963 clear_pathspec(&revs->prune_data);
1964 parse_pathspec(&revs->prune_data, PATHSPEC_ALL_MAGIC & ~PATHSPEC_LITERAL,
1965 PATHSPEC_PREFER_FULL | PATHSPEC_LITERAL_PATH, "", prune);
1966 revs->limited = 1;
1969 static int dotdot_missing(const char *arg, char *dotdot,
1970 struct rev_info *revs, int symmetric)
1972 if (revs->ignore_missing)
1973 return 0;
1974 /* de-munge so we report the full argument */
1975 *dotdot = '.';
1976 die(symmetric
1977 ? "Invalid symmetric difference expression %s"
1978 : "Invalid revision range %s", arg);
1981 static int handle_dotdot_1(const char *arg, char *dotdot,
1982 struct rev_info *revs, int flags,
1983 int cant_be_filename,
1984 struct object_context *a_oc,
1985 struct object_context *b_oc)
1987 const char *a_name, *b_name;
1988 struct object_id a_oid, b_oid;
1989 struct object *a_obj, *b_obj;
1990 unsigned int a_flags, b_flags;
1991 int symmetric = 0;
1992 unsigned int flags_exclude = flags ^ (UNINTERESTING | BOTTOM);
1993 unsigned int oc_flags = GET_OID_COMMITTISH | GET_OID_RECORD_PATH;
1995 a_name = arg;
1996 if (!*a_name)
1997 a_name = "HEAD";
1999 b_name = dotdot + 2;
2000 if (*b_name == '.') {
2001 symmetric = 1;
2002 b_name++;
2004 if (!*b_name)
2005 b_name = "HEAD";
2007 if (get_oid_with_context(revs->repo, a_name, oc_flags, &a_oid, a_oc) ||
2008 get_oid_with_context(revs->repo, b_name, oc_flags, &b_oid, b_oc))
2009 return -1;
2011 if (!cant_be_filename) {
2012 *dotdot = '.';
2013 verify_non_filename(revs->prefix, arg);
2014 *dotdot = '\0';
2017 a_obj = parse_object(revs->repo, &a_oid);
2018 b_obj = parse_object(revs->repo, &b_oid);
2019 if (!a_obj || !b_obj)
2020 return dotdot_missing(arg, dotdot, revs, symmetric);
2022 if (!symmetric) {
2023 /* just A..B */
2024 b_flags = flags;
2025 a_flags = flags_exclude;
2026 } else {
2027 /* A...B -- find merge bases between the two */
2028 struct commit *a, *b;
2029 struct commit_list *exclude;
2031 a = lookup_commit_reference(revs->repo, &a_obj->oid);
2032 b = lookup_commit_reference(revs->repo, &b_obj->oid);
2033 if (!a || !b)
2034 return dotdot_missing(arg, dotdot, revs, symmetric);
2036 exclude = get_merge_bases(a, b);
2037 add_rev_cmdline_list(revs, exclude, REV_CMD_MERGE_BASE,
2038 flags_exclude);
2039 add_pending_commit_list(revs, exclude, flags_exclude);
2040 free_commit_list(exclude);
2042 b_flags = flags;
2043 a_flags = flags | SYMMETRIC_LEFT;
2046 a_obj->flags |= a_flags;
2047 b_obj->flags |= b_flags;
2048 add_rev_cmdline(revs, a_obj, a_name, REV_CMD_LEFT, a_flags);
2049 add_rev_cmdline(revs, b_obj, b_name, REV_CMD_RIGHT, b_flags);
2050 add_pending_object_with_path(revs, a_obj, a_name, a_oc->mode, a_oc->path);
2051 add_pending_object_with_path(revs, b_obj, b_name, b_oc->mode, b_oc->path);
2052 return 0;
2055 static int handle_dotdot(const char *arg,
2056 struct rev_info *revs, int flags,
2057 int cant_be_filename)
2059 struct object_context a_oc, b_oc;
2060 char *dotdot = strstr(arg, "..");
2061 int ret;
2063 if (!dotdot)
2064 return -1;
2066 memset(&a_oc, 0, sizeof(a_oc));
2067 memset(&b_oc, 0, sizeof(b_oc));
2069 *dotdot = '\0';
2070 ret = handle_dotdot_1(arg, dotdot, revs, flags, cant_be_filename,
2071 &a_oc, &b_oc);
2072 *dotdot = '.';
2074 free(a_oc.path);
2075 free(b_oc.path);
2077 return ret;
2080 static int handle_revision_arg_1(const char *arg_, struct rev_info *revs, int flags, unsigned revarg_opt)
2082 struct object_context oc;
2083 char *mark;
2084 struct object *object;
2085 struct object_id oid;
2086 int local_flags;
2087 const char *arg = arg_;
2088 int cant_be_filename = revarg_opt & REVARG_CANNOT_BE_FILENAME;
2089 unsigned get_sha1_flags = GET_OID_RECORD_PATH;
2091 flags = flags & UNINTERESTING ? flags | BOTTOM : flags & ~BOTTOM;
2093 if (!cant_be_filename && !strcmp(arg, "..")) {
2095 * Just ".."? That is not a range but the
2096 * pathspec for the parent directory.
2098 return -1;
2101 if (!handle_dotdot(arg, revs, flags, revarg_opt))
2102 return 0;
2104 mark = strstr(arg, "^@");
2105 if (mark && !mark[2]) {
2106 *mark = 0;
2107 if (add_parents_only(revs, arg, flags, 0))
2108 return 0;
2109 *mark = '^';
2111 mark = strstr(arg, "^!");
2112 if (mark && !mark[2]) {
2113 *mark = 0;
2114 if (!add_parents_only(revs, arg, flags ^ (UNINTERESTING | BOTTOM), 0))
2115 *mark = '^';
2117 mark = strstr(arg, "^-");
2118 if (mark) {
2119 int exclude_parent = 1;
2121 if (mark[2]) {
2122 char *end;
2123 exclude_parent = strtoul(mark + 2, &end, 10);
2124 if (*end != '\0' || !exclude_parent)
2125 return -1;
2128 *mark = 0;
2129 if (!add_parents_only(revs, arg, flags ^ (UNINTERESTING | BOTTOM), exclude_parent))
2130 *mark = '^';
2133 local_flags = 0;
2134 if (*arg == '^') {
2135 local_flags = UNINTERESTING | BOTTOM;
2136 arg++;
2139 if (revarg_opt & REVARG_COMMITTISH)
2140 get_sha1_flags |= GET_OID_COMMITTISH;
2142 if (get_oid_with_context(revs->repo, arg, get_sha1_flags, &oid, &oc))
2143 return revs->ignore_missing ? 0 : -1;
2144 if (!cant_be_filename)
2145 verify_non_filename(revs->prefix, arg);
2146 object = get_reference(revs, arg, &oid, flags ^ local_flags);
2147 if (!object)
2148 return revs->ignore_missing ? 0 : -1;
2149 add_rev_cmdline(revs, object, arg_, REV_CMD_REV, flags ^ local_flags);
2150 add_pending_object_with_path(revs, object, arg, oc.mode, oc.path);
2151 free(oc.path);
2152 return 0;
2155 int handle_revision_arg(const char *arg, struct rev_info *revs, int flags, unsigned revarg_opt)
2157 int ret = handle_revision_arg_1(arg, revs, flags, revarg_opt);
2158 if (!ret)
2159 revs->rev_input_given = 1;
2160 return ret;
2163 static void read_pathspec_from_stdin(struct strbuf *sb,
2164 struct strvec *prune)
2166 while (strbuf_getline(sb, stdin) != EOF)
2167 strvec_push(prune, sb->buf);
2170 static void read_revisions_from_stdin(struct rev_info *revs,
2171 struct strvec *prune)
2173 struct strbuf sb;
2174 int seen_dashdash = 0;
2175 int save_warning;
2177 save_warning = warn_on_object_refname_ambiguity;
2178 warn_on_object_refname_ambiguity = 0;
2180 strbuf_init(&sb, 1000);
2181 while (strbuf_getline(&sb, stdin) != EOF) {
2182 int len = sb.len;
2183 if (!len)
2184 break;
2185 if (sb.buf[0] == '-') {
2186 if (len == 2 && sb.buf[1] == '-') {
2187 seen_dashdash = 1;
2188 break;
2190 die("options not supported in --stdin mode");
2192 if (handle_revision_arg(sb.buf, revs, 0,
2193 REVARG_CANNOT_BE_FILENAME))
2194 die("bad revision '%s'", sb.buf);
2196 if (seen_dashdash)
2197 read_pathspec_from_stdin(&sb, prune);
2199 strbuf_release(&sb);
2200 warn_on_object_refname_ambiguity = save_warning;
2203 static void add_grep(struct rev_info *revs, const char *ptn, enum grep_pat_token what)
2205 append_grep_pattern(&revs->grep_filter, ptn, "command line", 0, what);
2208 static void add_header_grep(struct rev_info *revs, enum grep_header_field field, const char *pattern)
2210 append_header_grep_pattern(&revs->grep_filter, field, pattern);
2213 static void add_message_grep(struct rev_info *revs, const char *pattern)
2215 add_grep(revs, pattern, GREP_PATTERN_BODY);
2218 static int handle_revision_opt(struct rev_info *revs, int argc, const char **argv,
2219 int *unkc, const char **unkv,
2220 const struct setup_revision_opt* opt)
2222 const char *arg = argv[0];
2223 const char *optarg = NULL;
2224 int argcount;
2225 const unsigned hexsz = the_hash_algo->hexsz;
2227 /* pseudo revision arguments */
2228 if (!strcmp(arg, "--all") || !strcmp(arg, "--branches") ||
2229 !strcmp(arg, "--tags") || !strcmp(arg, "--remotes") ||
2230 !strcmp(arg, "--reflog") || !strcmp(arg, "--not") ||
2231 !strcmp(arg, "--no-walk") || !strcmp(arg, "--do-walk") ||
2232 !strcmp(arg, "--bisect") || starts_with(arg, "--glob=") ||
2233 !strcmp(arg, "--indexed-objects") ||
2234 !strcmp(arg, "--alternate-refs") ||
2235 starts_with(arg, "--exclude=") ||
2236 starts_with(arg, "--branches=") || starts_with(arg, "--tags=") ||
2237 starts_with(arg, "--remotes=") || starts_with(arg, "--no-walk="))
2239 unkv[(*unkc)++] = arg;
2240 return 1;
2243 if ((argcount = parse_long_opt("max-count", argv, &optarg))) {
2244 revs->max_count = atoi(optarg);
2245 revs->no_walk = 0;
2246 return argcount;
2247 } else if ((argcount = parse_long_opt("skip", argv, &optarg))) {
2248 revs->skip_count = atoi(optarg);
2249 return argcount;
2250 } else if ((*arg == '-') && isdigit(arg[1])) {
2251 /* accept -<digit>, like traditional "head" */
2252 if (strtol_i(arg + 1, 10, &revs->max_count) < 0 ||
2253 revs->max_count < 0)
2254 die("'%s': not a non-negative integer", arg + 1);
2255 revs->no_walk = 0;
2256 } else if (!strcmp(arg, "-n")) {
2257 if (argc <= 1)
2258 return error("-n requires an argument");
2259 revs->max_count = atoi(argv[1]);
2260 revs->no_walk = 0;
2261 return 2;
2262 } else if (skip_prefix(arg, "-n", &optarg)) {
2263 revs->max_count = atoi(optarg);
2264 revs->no_walk = 0;
2265 } else if ((argcount = parse_long_opt("max-age", argv, &optarg))) {
2266 revs->max_age = atoi(optarg);
2267 return argcount;
2268 } else if ((argcount = parse_long_opt("since", argv, &optarg))) {
2269 revs->max_age = approxidate(optarg);
2270 return argcount;
2271 } else if ((argcount = parse_long_opt("since-as-filter", argv, &optarg))) {
2272 revs->max_age_as_filter = approxidate(optarg);
2273 return argcount;
2274 } else if ((argcount = parse_long_opt("after", argv, &optarg))) {
2275 revs->max_age = approxidate(optarg);
2276 return argcount;
2277 } else if ((argcount = parse_long_opt("min-age", argv, &optarg))) {
2278 revs->min_age = atoi(optarg);
2279 return argcount;
2280 } else if ((argcount = parse_long_opt("before", argv, &optarg))) {
2281 revs->min_age = approxidate(optarg);
2282 return argcount;
2283 } else if ((argcount = parse_long_opt("until", argv, &optarg))) {
2284 revs->min_age = approxidate(optarg);
2285 return argcount;
2286 } else if (!strcmp(arg, "--first-parent")) {
2287 revs->first_parent_only = 1;
2288 } else if (!strcmp(arg, "--exclude-first-parent-only")) {
2289 revs->exclude_first_parent_only = 1;
2290 } else if (!strcmp(arg, "--ancestry-path")) {
2291 revs->ancestry_path = 1;
2292 revs->simplify_history = 0;
2293 revs->limited = 1;
2294 revs->ancestry_path_implicit_bottoms = 1;
2295 } else if (skip_prefix(arg, "--ancestry-path=", &optarg)) {
2296 struct commit *c;
2297 struct object_id oid;
2298 const char *msg = _("could not get commit for ancestry-path argument %s");
2300 revs->ancestry_path = 1;
2301 revs->simplify_history = 0;
2302 revs->limited = 1;
2304 if (repo_get_oid_committish(revs->repo, optarg, &oid))
2305 return error(msg, optarg);
2306 get_reference(revs, optarg, &oid, ANCESTRY_PATH);
2307 c = lookup_commit_reference(revs->repo, &oid);
2308 if (!c)
2309 return error(msg, optarg);
2310 commit_list_insert(c, &revs->ancestry_path_bottoms);
2311 } else if (!strcmp(arg, "-g") || !strcmp(arg, "--walk-reflogs")) {
2312 init_reflog_walk(&revs->reflog_info);
2313 } else if (!strcmp(arg, "--default")) {
2314 if (argc <= 1)
2315 return error("bad --default argument");
2316 revs->def = argv[1];
2317 return 2;
2318 } else if (!strcmp(arg, "--merge")) {
2319 revs->show_merge = 1;
2320 } else if (!strcmp(arg, "--topo-order")) {
2321 revs->sort_order = REV_SORT_IN_GRAPH_ORDER;
2322 revs->topo_order = 1;
2323 } else if (!strcmp(arg, "--simplify-merges")) {
2324 revs->simplify_merges = 1;
2325 revs->topo_order = 1;
2326 revs->rewrite_parents = 1;
2327 revs->simplify_history = 0;
2328 revs->limited = 1;
2329 } else if (!strcmp(arg, "--simplify-by-decoration")) {
2330 revs->simplify_merges = 1;
2331 revs->topo_order = 1;
2332 revs->rewrite_parents = 1;
2333 revs->simplify_history = 0;
2334 revs->simplify_by_decoration = 1;
2335 revs->limited = 1;
2336 revs->prune = 1;
2337 } else if (!strcmp(arg, "--date-order")) {
2338 revs->sort_order = REV_SORT_BY_COMMIT_DATE;
2339 revs->topo_order = 1;
2340 } else if (!strcmp(arg, "--author-date-order")) {
2341 revs->sort_order = REV_SORT_BY_AUTHOR_DATE;
2342 revs->topo_order = 1;
2343 } else if (!strcmp(arg, "--early-output")) {
2344 revs->early_output = 100;
2345 revs->topo_order = 1;
2346 } else if (skip_prefix(arg, "--early-output=", &optarg)) {
2347 if (strtoul_ui(optarg, 10, &revs->early_output) < 0)
2348 die("'%s': not a non-negative integer", optarg);
2349 revs->topo_order = 1;
2350 } else if (!strcmp(arg, "--parents")) {
2351 revs->rewrite_parents = 1;
2352 revs->print_parents = 1;
2353 } else if (!strcmp(arg, "--dense")) {
2354 revs->dense = 1;
2355 } else if (!strcmp(arg, "--sparse")) {
2356 revs->dense = 0;
2357 } else if (!strcmp(arg, "--in-commit-order")) {
2358 revs->tree_blobs_in_commit_order = 1;
2359 } else if (!strcmp(arg, "--remove-empty")) {
2360 revs->remove_empty_trees = 1;
2361 } else if (!strcmp(arg, "--merges")) {
2362 revs->min_parents = 2;
2363 } else if (!strcmp(arg, "--no-merges")) {
2364 revs->max_parents = 1;
2365 } else if (skip_prefix(arg, "--min-parents=", &optarg)) {
2366 revs->min_parents = atoi(optarg);
2367 } else if (!strcmp(arg, "--no-min-parents")) {
2368 revs->min_parents = 0;
2369 } else if (skip_prefix(arg, "--max-parents=", &optarg)) {
2370 revs->max_parents = atoi(optarg);
2371 } else if (!strcmp(arg, "--no-max-parents")) {
2372 revs->max_parents = -1;
2373 } else if (!strcmp(arg, "--boundary")) {
2374 revs->boundary = 1;
2375 } else if (!strcmp(arg, "--left-right")) {
2376 revs->left_right = 1;
2377 } else if (!strcmp(arg, "--left-only")) {
2378 if (revs->right_only)
2379 die("--left-only is incompatible with --right-only"
2380 " or --cherry");
2381 revs->left_only = 1;
2382 } else if (!strcmp(arg, "--right-only")) {
2383 if (revs->left_only)
2384 die(_("options '%s' and '%s' cannot be used together"), "--right-only", "--left-only");
2385 revs->right_only = 1;
2386 } else if (!strcmp(arg, "--cherry")) {
2387 if (revs->left_only)
2388 die(_("options '%s' and '%s' cannot be used together"), "--cherry", "--left-only");
2389 revs->cherry_mark = 1;
2390 revs->right_only = 1;
2391 revs->max_parents = 1;
2392 revs->limited = 1;
2393 } else if (!strcmp(arg, "--count")) {
2394 revs->count = 1;
2395 } else if (!strcmp(arg, "--cherry-mark")) {
2396 if (revs->cherry_pick)
2397 die(_("options '%s' and '%s' cannot be used together"), "--cherry-mark", "--cherry-pick");
2398 revs->cherry_mark = 1;
2399 revs->limited = 1; /* needs limit_list() */
2400 } else if (!strcmp(arg, "--cherry-pick")) {
2401 if (revs->cherry_mark)
2402 die(_("options '%s' and '%s' cannot be used together"), "--cherry-pick", "--cherry-mark");
2403 revs->cherry_pick = 1;
2404 revs->limited = 1;
2405 } else if (!strcmp(arg, "--objects")) {
2406 revs->tag_objects = 1;
2407 revs->tree_objects = 1;
2408 revs->blob_objects = 1;
2409 } else if (!strcmp(arg, "--objects-edge")) {
2410 revs->tag_objects = 1;
2411 revs->tree_objects = 1;
2412 revs->blob_objects = 1;
2413 revs->edge_hint = 1;
2414 } else if (!strcmp(arg, "--objects-edge-aggressive")) {
2415 revs->tag_objects = 1;
2416 revs->tree_objects = 1;
2417 revs->blob_objects = 1;
2418 revs->edge_hint = 1;
2419 revs->edge_hint_aggressive = 1;
2420 } else if (!strcmp(arg, "--verify-objects")) {
2421 revs->tag_objects = 1;
2422 revs->tree_objects = 1;
2423 revs->blob_objects = 1;
2424 revs->verify_objects = 1;
2425 disable_commit_graph(revs->repo);
2426 } else if (!strcmp(arg, "--unpacked")) {
2427 revs->unpacked = 1;
2428 } else if (starts_with(arg, "--unpacked=")) {
2429 die(_("--unpacked=<packfile> no longer supported"));
2430 } else if (!strcmp(arg, "--no-kept-objects")) {
2431 revs->no_kept_objects = 1;
2432 revs->keep_pack_cache_flags |= IN_CORE_KEEP_PACKS;
2433 revs->keep_pack_cache_flags |= ON_DISK_KEEP_PACKS;
2434 } else if (skip_prefix(arg, "--no-kept-objects=", &optarg)) {
2435 revs->no_kept_objects = 1;
2436 if (!strcmp(optarg, "in-core"))
2437 revs->keep_pack_cache_flags |= IN_CORE_KEEP_PACKS;
2438 if (!strcmp(optarg, "on-disk"))
2439 revs->keep_pack_cache_flags |= ON_DISK_KEEP_PACKS;
2440 } else if (!strcmp(arg, "-r")) {
2441 revs->diff = 1;
2442 revs->diffopt.flags.recursive = 1;
2443 } else if (!strcmp(arg, "-t")) {
2444 revs->diff = 1;
2445 revs->diffopt.flags.recursive = 1;
2446 revs->diffopt.flags.tree_in_recursive = 1;
2447 } else if ((argcount = diff_merges_parse_opts(revs, argv))) {
2448 return argcount;
2449 } else if (!strcmp(arg, "-v")) {
2450 revs->verbose_header = 1;
2451 } else if (!strcmp(arg, "--pretty")) {
2452 revs->verbose_header = 1;
2453 revs->pretty_given = 1;
2454 get_commit_format(NULL, revs);
2455 } else if (skip_prefix(arg, "--pretty=", &optarg) ||
2456 skip_prefix(arg, "--format=", &optarg)) {
2458 * Detached form ("--pretty X" as opposed to "--pretty=X")
2459 * not allowed, since the argument is optional.
2461 revs->verbose_header = 1;
2462 revs->pretty_given = 1;
2463 get_commit_format(optarg, revs);
2464 } else if (!strcmp(arg, "--expand-tabs")) {
2465 revs->expand_tabs_in_log = 8;
2466 } else if (!strcmp(arg, "--no-expand-tabs")) {
2467 revs->expand_tabs_in_log = 0;
2468 } else if (skip_prefix(arg, "--expand-tabs=", &arg)) {
2469 int val;
2470 if (strtol_i(arg, 10, &val) < 0 || val < 0)
2471 die("'%s': not a non-negative integer", arg);
2472 revs->expand_tabs_in_log = val;
2473 } else if (!strcmp(arg, "--show-notes") || !strcmp(arg, "--notes")) {
2474 enable_default_display_notes(&revs->notes_opt, &revs->show_notes);
2475 revs->show_notes_given = 1;
2476 } else if (!strcmp(arg, "--show-signature")) {
2477 revs->show_signature = 1;
2478 } else if (!strcmp(arg, "--no-show-signature")) {
2479 revs->show_signature = 0;
2480 } else if (!strcmp(arg, "--show-linear-break")) {
2481 revs->break_bar = " ..........";
2482 revs->track_linear = 1;
2483 revs->track_first_time = 1;
2484 } else if (skip_prefix(arg, "--show-linear-break=", &optarg)) {
2485 revs->break_bar = xstrdup(optarg);
2486 revs->track_linear = 1;
2487 revs->track_first_time = 1;
2488 } else if (skip_prefix(arg, "--show-notes=", &optarg) ||
2489 skip_prefix(arg, "--notes=", &optarg)) {
2490 if (starts_with(arg, "--show-notes=") &&
2491 revs->notes_opt.use_default_notes < 0)
2492 revs->notes_opt.use_default_notes = 1;
2493 enable_ref_display_notes(&revs->notes_opt, &revs->show_notes, optarg);
2494 revs->show_notes_given = 1;
2495 } else if (!strcmp(arg, "--no-notes")) {
2496 disable_display_notes(&revs->notes_opt, &revs->show_notes);
2497 revs->show_notes_given = 1;
2498 } else if (!strcmp(arg, "--standard-notes")) {
2499 revs->show_notes_given = 1;
2500 revs->notes_opt.use_default_notes = 1;
2501 } else if (!strcmp(arg, "--no-standard-notes")) {
2502 revs->notes_opt.use_default_notes = 0;
2503 } else if (!strcmp(arg, "--oneline")) {
2504 revs->verbose_header = 1;
2505 get_commit_format("oneline", revs);
2506 revs->pretty_given = 1;
2507 revs->abbrev_commit = 1;
2508 } else if (!strcmp(arg, "--graph")) {
2509 graph_clear(revs->graph);
2510 revs->graph = graph_init(revs);
2511 } else if (!strcmp(arg, "--no-graph")) {
2512 graph_clear(revs->graph);
2513 revs->graph = NULL;
2514 } else if (!strcmp(arg, "--encode-email-headers")) {
2515 revs->encode_email_headers = 1;
2516 } else if (!strcmp(arg, "--no-encode-email-headers")) {
2517 revs->encode_email_headers = 0;
2518 } else if (!strcmp(arg, "--root")) {
2519 revs->show_root_diff = 1;
2520 } else if (!strcmp(arg, "--no-commit-id")) {
2521 revs->no_commit_id = 1;
2522 } else if (!strcmp(arg, "--always")) {
2523 revs->always_show_header = 1;
2524 } else if (!strcmp(arg, "--no-abbrev")) {
2525 revs->abbrev = 0;
2526 } else if (!strcmp(arg, "--abbrev")) {
2527 revs->abbrev = DEFAULT_ABBREV;
2528 } else if (skip_prefix(arg, "--abbrev=", &optarg)) {
2529 revs->abbrev = strtoul(optarg, NULL, 10);
2530 if (revs->abbrev < MINIMUM_ABBREV)
2531 revs->abbrev = MINIMUM_ABBREV;
2532 else if (revs->abbrev > hexsz)
2533 revs->abbrev = hexsz;
2534 } else if (!strcmp(arg, "--abbrev-commit")) {
2535 revs->abbrev_commit = 1;
2536 revs->abbrev_commit_given = 1;
2537 } else if (!strcmp(arg, "--no-abbrev-commit")) {
2538 revs->abbrev_commit = 0;
2539 } else if (!strcmp(arg, "--full-diff")) {
2540 revs->diff = 1;
2541 revs->full_diff = 1;
2542 } else if (!strcmp(arg, "--show-pulls")) {
2543 revs->show_pulls = 1;
2544 } else if (!strcmp(arg, "--full-history")) {
2545 revs->simplify_history = 0;
2546 } else if (!strcmp(arg, "--relative-date")) {
2547 revs->date_mode.type = DATE_RELATIVE;
2548 revs->date_mode_explicit = 1;
2549 } else if ((argcount = parse_long_opt("date", argv, &optarg))) {
2550 parse_date_format(optarg, &revs->date_mode);
2551 revs->date_mode_explicit = 1;
2552 return argcount;
2553 } else if (!strcmp(arg, "--log-size")) {
2554 revs->show_log_size = 1;
2557 * Grepping the commit log
2559 else if ((argcount = parse_long_opt("author", argv, &optarg))) {
2560 add_header_grep(revs, GREP_HEADER_AUTHOR, optarg);
2561 return argcount;
2562 } else if ((argcount = parse_long_opt("committer", argv, &optarg))) {
2563 add_header_grep(revs, GREP_HEADER_COMMITTER, optarg);
2564 return argcount;
2565 } else if ((argcount = parse_long_opt("grep-reflog", argv, &optarg))) {
2566 add_header_grep(revs, GREP_HEADER_REFLOG, optarg);
2567 return argcount;
2568 } else if ((argcount = parse_long_opt("grep", argv, &optarg))) {
2569 add_message_grep(revs, optarg);
2570 return argcount;
2571 } else if (!strcmp(arg, "--basic-regexp")) {
2572 revs->grep_filter.pattern_type_option = GREP_PATTERN_TYPE_BRE;
2573 } else if (!strcmp(arg, "--extended-regexp") || !strcmp(arg, "-E")) {
2574 revs->grep_filter.pattern_type_option = GREP_PATTERN_TYPE_ERE;
2575 } else if (!strcmp(arg, "--regexp-ignore-case") || !strcmp(arg, "-i")) {
2576 revs->grep_filter.ignore_case = 1;
2577 revs->diffopt.pickaxe_opts |= DIFF_PICKAXE_IGNORE_CASE;
2578 } else if (!strcmp(arg, "--fixed-strings") || !strcmp(arg, "-F")) {
2579 revs->grep_filter.pattern_type_option = GREP_PATTERN_TYPE_FIXED;
2580 } else if (!strcmp(arg, "--perl-regexp") || !strcmp(arg, "-P")) {
2581 revs->grep_filter.pattern_type_option = GREP_PATTERN_TYPE_PCRE;
2582 } else if (!strcmp(arg, "--all-match")) {
2583 revs->grep_filter.all_match = 1;
2584 } else if (!strcmp(arg, "--invert-grep")) {
2585 revs->grep_filter.no_body_match = 1;
2586 } else if ((argcount = parse_long_opt("encoding", argv, &optarg))) {
2587 if (strcmp(optarg, "none"))
2588 git_log_output_encoding = xstrdup(optarg);
2589 else
2590 git_log_output_encoding = "";
2591 return argcount;
2592 } else if (!strcmp(arg, "--reverse")) {
2593 revs->reverse ^= 1;
2594 } else if (!strcmp(arg, "--children")) {
2595 revs->children.name = "children";
2596 revs->limited = 1;
2597 } else if (!strcmp(arg, "--ignore-missing")) {
2598 revs->ignore_missing = 1;
2599 } else if (opt && opt->allow_exclude_promisor_objects &&
2600 !strcmp(arg, "--exclude-promisor-objects")) {
2601 if (fetch_if_missing)
2602 BUG("exclude_promisor_objects can only be used when fetch_if_missing is 0");
2603 revs->exclude_promisor_objects = 1;
2604 } else {
2605 int opts = diff_opt_parse(&revs->diffopt, argv, argc, revs->prefix);
2606 if (!opts)
2607 unkv[(*unkc)++] = arg;
2608 return opts;
2611 return 1;
2614 void parse_revision_opt(struct rev_info *revs, struct parse_opt_ctx_t *ctx,
2615 const struct option *options,
2616 const char * const usagestr[])
2618 int n = handle_revision_opt(revs, ctx->argc, ctx->argv,
2619 &ctx->cpidx, ctx->out, NULL);
2620 if (n <= 0) {
2621 error("unknown option `%s'", ctx->argv[0]);
2622 usage_with_options(usagestr, options);
2624 ctx->argv += n;
2625 ctx->argc -= n;
2628 void revision_opts_finish(struct rev_info *revs)
2630 if (revs->graph && revs->track_linear)
2631 die(_("options '%s' and '%s' cannot be used together"), "--show-linear-break", "--graph");
2633 if (revs->graph) {
2634 revs->topo_order = 1;
2635 revs->rewrite_parents = 1;
2639 static int for_each_bisect_ref(struct ref_store *refs, each_ref_fn fn,
2640 void *cb_data, const char *term)
2642 struct strbuf bisect_refs = STRBUF_INIT;
2643 int status;
2644 strbuf_addf(&bisect_refs, "refs/bisect/%s", term);
2645 status = refs_for_each_fullref_in(refs, bisect_refs.buf, fn, cb_data);
2646 strbuf_release(&bisect_refs);
2647 return status;
2650 static int for_each_bad_bisect_ref(struct ref_store *refs, each_ref_fn fn, void *cb_data)
2652 return for_each_bisect_ref(refs, fn, cb_data, term_bad);
2655 static int for_each_good_bisect_ref(struct ref_store *refs, each_ref_fn fn, void *cb_data)
2657 return for_each_bisect_ref(refs, fn, cb_data, term_good);
2660 static int handle_revision_pseudo_opt(struct rev_info *revs,
2661 const char **argv, int *flags)
2663 const char *arg = argv[0];
2664 const char *optarg;
2665 struct ref_store *refs;
2666 int argcount;
2668 if (revs->repo != the_repository) {
2670 * We need some something like get_submodule_worktrees()
2671 * before we can go through all worktrees of a submodule,
2672 * .e.g with adding all HEADs from --all, which is not
2673 * supported right now, so stick to single worktree.
2675 if (!revs->single_worktree)
2676 BUG("--single-worktree cannot be used together with submodule");
2678 refs = get_main_ref_store(revs->repo);
2681 * NOTE!
2683 * Commands like "git shortlog" will not accept the options below
2684 * unless parse_revision_opt queues them (as opposed to erroring
2685 * out).
2687 * When implementing your new pseudo-option, remember to
2688 * register it in the list at the top of handle_revision_opt.
2690 if (!strcmp(arg, "--all")) {
2691 handle_refs(refs, revs, *flags, refs_for_each_ref);
2692 handle_refs(refs, revs, *flags, refs_head_ref);
2693 if (!revs->single_worktree) {
2694 struct all_refs_cb cb;
2696 init_all_refs_cb(&cb, revs, *flags);
2697 other_head_refs(handle_one_ref, &cb);
2699 clear_ref_exclusion(&revs->ref_excludes);
2700 } else if (!strcmp(arg, "--branches")) {
2701 handle_refs(refs, revs, *flags, refs_for_each_branch_ref);
2702 clear_ref_exclusion(&revs->ref_excludes);
2703 } else if (!strcmp(arg, "--bisect")) {
2704 read_bisect_terms(&term_bad, &term_good);
2705 handle_refs(refs, revs, *flags, for_each_bad_bisect_ref);
2706 handle_refs(refs, revs, *flags ^ (UNINTERESTING | BOTTOM),
2707 for_each_good_bisect_ref);
2708 revs->bisect = 1;
2709 } else if (!strcmp(arg, "--tags")) {
2710 handle_refs(refs, revs, *flags, refs_for_each_tag_ref);
2711 clear_ref_exclusion(&revs->ref_excludes);
2712 } else if (!strcmp(arg, "--remotes")) {
2713 handle_refs(refs, revs, *flags, refs_for_each_remote_ref);
2714 clear_ref_exclusion(&revs->ref_excludes);
2715 } else if ((argcount = parse_long_opt("glob", argv, &optarg))) {
2716 struct all_refs_cb cb;
2717 init_all_refs_cb(&cb, revs, *flags);
2718 for_each_glob_ref(handle_one_ref, optarg, &cb);
2719 clear_ref_exclusion(&revs->ref_excludes);
2720 return argcount;
2721 } else if ((argcount = parse_long_opt("exclude", argv, &optarg))) {
2722 add_ref_exclusion(&revs->ref_excludes, optarg);
2723 return argcount;
2724 } else if (skip_prefix(arg, "--branches=", &optarg)) {
2725 struct all_refs_cb cb;
2726 init_all_refs_cb(&cb, revs, *flags);
2727 for_each_glob_ref_in(handle_one_ref, optarg, "refs/heads/", &cb);
2728 clear_ref_exclusion(&revs->ref_excludes);
2729 } else if (skip_prefix(arg, "--tags=", &optarg)) {
2730 struct all_refs_cb cb;
2731 init_all_refs_cb(&cb, revs, *flags);
2732 for_each_glob_ref_in(handle_one_ref, optarg, "refs/tags/", &cb);
2733 clear_ref_exclusion(&revs->ref_excludes);
2734 } else if (skip_prefix(arg, "--remotes=", &optarg)) {
2735 struct all_refs_cb cb;
2736 init_all_refs_cb(&cb, revs, *flags);
2737 for_each_glob_ref_in(handle_one_ref, optarg, "refs/remotes/", &cb);
2738 clear_ref_exclusion(&revs->ref_excludes);
2739 } else if (!strcmp(arg, "--reflog")) {
2740 add_reflogs_to_pending(revs, *flags);
2741 } else if (!strcmp(arg, "--indexed-objects")) {
2742 add_index_objects_to_pending(revs, *flags);
2743 } else if (!strcmp(arg, "--alternate-refs")) {
2744 add_alternate_refs_to_pending(revs, *flags);
2745 } else if (!strcmp(arg, "--not")) {
2746 *flags ^= UNINTERESTING | BOTTOM;
2747 } else if (!strcmp(arg, "--no-walk")) {
2748 revs->no_walk = 1;
2749 } else if (skip_prefix(arg, "--no-walk=", &optarg)) {
2751 * Detached form ("--no-walk X" as opposed to "--no-walk=X")
2752 * not allowed, since the argument is optional.
2754 revs->no_walk = 1;
2755 if (!strcmp(optarg, "sorted"))
2756 revs->unsorted_input = 0;
2757 else if (!strcmp(optarg, "unsorted"))
2758 revs->unsorted_input = 1;
2759 else
2760 return error("invalid argument to --no-walk");
2761 } else if (!strcmp(arg, "--do-walk")) {
2762 revs->no_walk = 0;
2763 } else if (!strcmp(arg, "--single-worktree")) {
2764 revs->single_worktree = 1;
2765 } else if (skip_prefix(arg, ("--filter="), &arg)) {
2766 parse_list_objects_filter(&revs->filter, arg);
2767 } else if (!strcmp(arg, ("--no-filter"))) {
2768 list_objects_filter_set_no_filter(&revs->filter);
2769 } else {
2770 return 0;
2773 return 1;
2776 static void NORETURN diagnose_missing_default(const char *def)
2778 int flags;
2779 const char *refname;
2781 refname = resolve_ref_unsafe(def, 0, NULL, &flags);
2782 if (!refname || !(flags & REF_ISSYMREF) || (flags & REF_ISBROKEN))
2783 die(_("your current branch appears to be broken"));
2785 skip_prefix(refname, "refs/heads/", &refname);
2786 die(_("your current branch '%s' does not have any commits yet"),
2787 refname);
2791 * Parse revision information, filling in the "rev_info" structure,
2792 * and removing the used arguments from the argument list.
2794 * Returns the number of arguments left that weren't recognized
2795 * (which are also moved to the head of the argument list)
2797 int setup_revisions(int argc, const char **argv, struct rev_info *revs, struct setup_revision_opt *opt)
2799 int i, flags, left, seen_dashdash, revarg_opt;
2800 struct strvec prune_data = STRVEC_INIT;
2801 int seen_end_of_options = 0;
2803 /* First, search for "--" */
2804 if (opt && opt->assume_dashdash) {
2805 seen_dashdash = 1;
2806 } else {
2807 seen_dashdash = 0;
2808 for (i = 1; i < argc; i++) {
2809 const char *arg = argv[i];
2810 if (strcmp(arg, "--"))
2811 continue;
2812 if (opt && opt->free_removed_argv_elements)
2813 free((char *)argv[i]);
2814 argv[i] = NULL;
2815 argc = i;
2816 if (argv[i + 1])
2817 strvec_pushv(&prune_data, argv + i + 1);
2818 seen_dashdash = 1;
2819 break;
2823 /* Second, deal with arguments and options */
2824 flags = 0;
2825 revarg_opt = opt ? opt->revarg_opt : 0;
2826 if (seen_dashdash)
2827 revarg_opt |= REVARG_CANNOT_BE_FILENAME;
2828 for (left = i = 1; i < argc; i++) {
2829 const char *arg = argv[i];
2830 if (!seen_end_of_options && *arg == '-') {
2831 int opts;
2833 opts = handle_revision_pseudo_opt(
2834 revs, argv + i,
2835 &flags);
2836 if (opts > 0) {
2837 i += opts - 1;
2838 continue;
2841 if (!strcmp(arg, "--stdin")) {
2842 if (revs->disable_stdin) {
2843 argv[left++] = arg;
2844 continue;
2846 if (revs->read_from_stdin++)
2847 die("--stdin given twice?");
2848 read_revisions_from_stdin(revs, &prune_data);
2849 continue;
2852 if (!strcmp(arg, "--end-of-options")) {
2853 seen_end_of_options = 1;
2854 continue;
2857 opts = handle_revision_opt(revs, argc - i, argv + i,
2858 &left, argv, opt);
2859 if (opts > 0) {
2860 i += opts - 1;
2861 continue;
2863 if (opts < 0)
2864 exit(128);
2865 continue;
2869 if (handle_revision_arg(arg, revs, flags, revarg_opt)) {
2870 int j;
2871 if (seen_dashdash || *arg == '^')
2872 die("bad revision '%s'", arg);
2874 /* If we didn't have a "--":
2875 * (1) all filenames must exist;
2876 * (2) all rev-args must not be interpretable
2877 * as a valid filename.
2878 * but the latter we have checked in the main loop.
2880 for (j = i; j < argc; j++)
2881 verify_filename(revs->prefix, argv[j], j == i);
2883 strvec_pushv(&prune_data, argv + i);
2884 break;
2887 revision_opts_finish(revs);
2889 if (prune_data.nr) {
2891 * If we need to introduce the magic "a lone ':' means no
2892 * pathspec whatsoever", here is the place to do so.
2894 * if (prune_data.nr == 1 && !strcmp(prune_data[0], ":")) {
2895 * prune_data.nr = 0;
2896 * prune_data.alloc = 0;
2897 * free(prune_data.path);
2898 * prune_data.path = NULL;
2899 * } else {
2900 * terminate prune_data.alloc with NULL and
2901 * call init_pathspec() to set revs->prune_data here.
2904 parse_pathspec(&revs->prune_data, 0, 0,
2905 revs->prefix, prune_data.v);
2907 strvec_clear(&prune_data);
2909 if (!revs->def)
2910 revs->def = opt ? opt->def : NULL;
2911 if (opt && opt->tweak)
2912 opt->tweak(revs, opt);
2913 if (revs->show_merge)
2914 prepare_show_merge(revs);
2915 if (revs->def && !revs->pending.nr && !revs->rev_input_given) {
2916 struct object_id oid;
2917 struct object *object;
2918 struct object_context oc;
2919 if (get_oid_with_context(revs->repo, revs->def, 0, &oid, &oc))
2920 diagnose_missing_default(revs->def);
2921 object = get_reference(revs, revs->def, &oid, 0);
2922 add_pending_object_with_mode(revs, object, revs->def, oc.mode);
2925 /* Did the user ask for any diff output? Run the diff! */
2926 if (revs->diffopt.output_format & ~DIFF_FORMAT_NO_OUTPUT)
2927 revs->diff = 1;
2929 /* Pickaxe, diff-filter and rename following need diffs */
2930 if ((revs->diffopt.pickaxe_opts & DIFF_PICKAXE_KINDS_MASK) ||
2931 revs->diffopt.filter ||
2932 revs->diffopt.flags.follow_renames)
2933 revs->diff = 1;
2935 if (revs->diffopt.objfind)
2936 revs->simplify_history = 0;
2938 if (revs->line_level_traverse) {
2939 if (want_ancestry(revs))
2940 revs->limited = 1;
2941 revs->topo_order = 1;
2944 if (revs->topo_order && !generation_numbers_enabled(the_repository))
2945 revs->limited = 1;
2947 if (revs->prune_data.nr) {
2948 copy_pathspec(&revs->pruning.pathspec, &revs->prune_data);
2949 /* Can't prune commits with rename following: the paths change.. */
2950 if (!revs->diffopt.flags.follow_renames)
2951 revs->prune = 1;
2952 if (!revs->full_diff)
2953 copy_pathspec(&revs->diffopt.pathspec,
2954 &revs->prune_data);
2957 diff_merges_setup_revs(revs);
2959 revs->diffopt.abbrev = revs->abbrev;
2961 diff_setup_done(&revs->diffopt);
2963 if (!is_encoding_utf8(get_log_output_encoding()))
2964 revs->grep_filter.ignore_locale = 1;
2965 compile_grep_patterns(&revs->grep_filter);
2967 if (revs->reverse && revs->reflog_info)
2968 die(_("options '%s' and '%s' cannot be used together"), "--reverse", "--walk-reflogs");
2969 if (revs->reflog_info && revs->limited)
2970 die("cannot combine --walk-reflogs with history-limiting options");
2971 if (revs->rewrite_parents && revs->children.name)
2972 die(_("options '%s' and '%s' cannot be used together"), "--parents", "--children");
2973 if (revs->filter.choice && !revs->blob_objects)
2974 die(_("object filtering requires --objects"));
2977 * Limitations on the graph functionality
2979 if (revs->reverse && revs->graph)
2980 die(_("options '%s' and '%s' cannot be used together"), "--reverse", "--graph");
2982 if (revs->reflog_info && revs->graph)
2983 die(_("options '%s' and '%s' cannot be used together"), "--walk-reflogs", "--graph");
2984 if (revs->no_walk && revs->graph)
2985 die(_("options '%s' and '%s' cannot be used together"), "--no-walk", "--graph");
2986 if (!revs->reflog_info && revs->grep_filter.use_reflog_filter)
2987 die(_("the option '%s' requires '%s'"), "--grep-reflog", "--walk-reflogs");
2989 if (revs->line_level_traverse &&
2990 (revs->diffopt.output_format & ~(DIFF_FORMAT_PATCH | DIFF_FORMAT_NO_OUTPUT)))
2991 die(_("-L does not yet support diff formats besides -p and -s"));
2993 if (revs->expand_tabs_in_log < 0)
2994 revs->expand_tabs_in_log = revs->expand_tabs_in_log_default;
2996 return left;
2999 static void release_revisions_cmdline(struct rev_cmdline_info *cmdline)
3001 unsigned int i;
3003 for (i = 0; i < cmdline->nr; i++)
3004 free((char *)cmdline->rev[i].name);
3005 free(cmdline->rev);
3008 static void release_revisions_mailmap(struct string_list *mailmap)
3010 if (!mailmap)
3011 return;
3012 clear_mailmap(mailmap);
3013 free(mailmap);
3016 static void release_revisions_topo_walk_info(struct topo_walk_info *info);
3018 void release_revisions(struct rev_info *revs)
3020 free_commit_list(revs->commits);
3021 free_commit_list(revs->ancestry_path_bottoms);
3022 object_array_clear(&revs->pending);
3023 object_array_clear(&revs->boundary_commits);
3024 release_revisions_cmdline(&revs->cmdline);
3025 list_objects_filter_release(&revs->filter);
3026 clear_pathspec(&revs->prune_data);
3027 date_mode_release(&revs->date_mode);
3028 release_revisions_mailmap(revs->mailmap);
3029 free_grep_patterns(&revs->grep_filter);
3030 /* TODO (need to handle "no_free"): diff_free(&revs->diffopt) */
3031 diff_free(&revs->pruning);
3032 reflog_walk_info_release(revs->reflog_info);
3033 release_revisions_topo_walk_info(revs->topo_walk_info);
3036 static void add_child(struct rev_info *revs, struct commit *parent, struct commit *child)
3038 struct commit_list *l = xcalloc(1, sizeof(*l));
3040 l->item = child;
3041 l->next = add_decoration(&revs->children, &parent->object, l);
3044 static int remove_duplicate_parents(struct rev_info *revs, struct commit *commit)
3046 struct treesame_state *ts = lookup_decoration(&revs->treesame, &commit->object);
3047 struct commit_list **pp, *p;
3048 int surviving_parents;
3050 /* Examine existing parents while marking ones we have seen... */
3051 pp = &commit->parents;
3052 surviving_parents = 0;
3053 while ((p = *pp) != NULL) {
3054 struct commit *parent = p->item;
3055 if (parent->object.flags & TMP_MARK) {
3056 *pp = p->next;
3057 if (ts)
3058 compact_treesame(revs, commit, surviving_parents);
3059 continue;
3061 parent->object.flags |= TMP_MARK;
3062 surviving_parents++;
3063 pp = &p->next;
3065 /* clear the temporary mark */
3066 for (p = commit->parents; p; p = p->next) {
3067 p->item->object.flags &= ~TMP_MARK;
3069 /* no update_treesame() - removing duplicates can't affect TREESAME */
3070 return surviving_parents;
3073 struct merge_simplify_state {
3074 struct commit *simplified;
3077 static struct merge_simplify_state *locate_simplify_state(struct rev_info *revs, struct commit *commit)
3079 struct merge_simplify_state *st;
3081 st = lookup_decoration(&revs->merge_simplification, &commit->object);
3082 if (!st) {
3083 CALLOC_ARRAY(st, 1);
3084 add_decoration(&revs->merge_simplification, &commit->object, st);
3086 return st;
3089 static int mark_redundant_parents(struct commit *commit)
3091 struct commit_list *h = reduce_heads(commit->parents);
3092 int i = 0, marked = 0;
3093 struct commit_list *po, *pn;
3095 /* Want these for sanity-checking only */
3096 int orig_cnt = commit_list_count(commit->parents);
3097 int cnt = commit_list_count(h);
3100 * Not ready to remove items yet, just mark them for now, based
3101 * on the output of reduce_heads(). reduce_heads outputs the reduced
3102 * set in its original order, so this isn't too hard.
3104 po = commit->parents;
3105 pn = h;
3106 while (po) {
3107 if (pn && po->item == pn->item) {
3108 pn = pn->next;
3109 i++;
3110 } else {
3111 po->item->object.flags |= TMP_MARK;
3112 marked++;
3114 po=po->next;
3117 if (i != cnt || cnt+marked != orig_cnt)
3118 die("mark_redundant_parents %d %d %d %d", orig_cnt, cnt, i, marked);
3120 free_commit_list(h);
3122 return marked;
3125 static int mark_treesame_root_parents(struct commit *commit)
3127 struct commit_list *p;
3128 int marked = 0;
3130 for (p = commit->parents; p; p = p->next) {
3131 struct commit *parent = p->item;
3132 if (!parent->parents && (parent->object.flags & TREESAME)) {
3133 parent->object.flags |= TMP_MARK;
3134 marked++;
3138 return marked;
3142 * Awkward naming - this means one parent we are TREESAME to.
3143 * cf mark_treesame_root_parents: root parents that are TREESAME (to an
3144 * empty tree). Better name suggestions?
3146 static int leave_one_treesame_to_parent(struct rev_info *revs, struct commit *commit)
3148 struct treesame_state *ts = lookup_decoration(&revs->treesame, &commit->object);
3149 struct commit *unmarked = NULL, *marked = NULL;
3150 struct commit_list *p;
3151 unsigned n;
3153 for (p = commit->parents, n = 0; p; p = p->next, n++) {
3154 if (ts->treesame[n]) {
3155 if (p->item->object.flags & TMP_MARK) {
3156 if (!marked)
3157 marked = p->item;
3158 } else {
3159 if (!unmarked) {
3160 unmarked = p->item;
3161 break;
3168 * If we are TREESAME to a marked-for-deletion parent, but not to any
3169 * unmarked parents, unmark the first TREESAME parent. This is the
3170 * parent that the default simplify_history==1 scan would have followed,
3171 * and it doesn't make sense to omit that path when asking for a
3172 * simplified full history. Retaining it improves the chances of
3173 * understanding odd missed merges that took an old version of a file.
3175 * Example:
3177 * I--------*X A modified the file, but mainline merge X used
3178 * \ / "-s ours", so took the version from I. X is
3179 * `-*A--' TREESAME to I and !TREESAME to A.
3181 * Default log from X would produce "I". Without this check,
3182 * --full-history --simplify-merges would produce "I-A-X", showing
3183 * the merge commit X and that it changed A, but not making clear that
3184 * it had just taken the I version. With this check, the topology above
3185 * is retained.
3187 * Note that it is possible that the simplification chooses a different
3188 * TREESAME parent from the default, in which case this test doesn't
3189 * activate, and we _do_ drop the default parent. Example:
3191 * I------X A modified the file, but it was reverted in B,
3192 * \ / meaning mainline merge X is TREESAME to both
3193 * *A-*B parents.
3195 * Default log would produce "I" by following the first parent;
3196 * --full-history --simplify-merges will produce "I-A-B". But this is a
3197 * reasonable result - it presents a logical full history leading from
3198 * I to X, and X is not an important merge.
3200 if (!unmarked && marked) {
3201 marked->object.flags &= ~TMP_MARK;
3202 return 1;
3205 return 0;
3208 static int remove_marked_parents(struct rev_info *revs, struct commit *commit)
3210 struct commit_list **pp, *p;
3211 int nth_parent, removed = 0;
3213 pp = &commit->parents;
3214 nth_parent = 0;
3215 while ((p = *pp) != NULL) {
3216 struct commit *parent = p->item;
3217 if (parent->object.flags & TMP_MARK) {
3218 parent->object.flags &= ~TMP_MARK;
3219 *pp = p->next;
3220 free(p);
3221 removed++;
3222 compact_treesame(revs, commit, nth_parent);
3223 continue;
3225 pp = &p->next;
3226 nth_parent++;
3229 /* Removing parents can only increase TREESAMEness */
3230 if (removed && !(commit->object.flags & TREESAME))
3231 update_treesame(revs, commit);
3233 return nth_parent;
3236 static struct commit_list **simplify_one(struct rev_info *revs, struct commit *commit, struct commit_list **tail)
3238 struct commit_list *p;
3239 struct commit *parent;
3240 struct merge_simplify_state *st, *pst;
3241 int cnt;
3243 st = locate_simplify_state(revs, commit);
3246 * Have we handled this one?
3248 if (st->simplified)
3249 return tail;
3252 * An UNINTERESTING commit simplifies to itself, so does a
3253 * root commit. We do not rewrite parents of such commit
3254 * anyway.
3256 if ((commit->object.flags & UNINTERESTING) || !commit->parents) {
3257 st->simplified = commit;
3258 return tail;
3262 * Do we know what commit all of our parents that matter
3263 * should be rewritten to? Otherwise we are not ready to
3264 * rewrite this one yet.
3266 for (cnt = 0, p = commit->parents; p; p = p->next) {
3267 pst = locate_simplify_state(revs, p->item);
3268 if (!pst->simplified) {
3269 tail = &commit_list_insert(p->item, tail)->next;
3270 cnt++;
3272 if (revs->first_parent_only)
3273 break;
3275 if (cnt) {
3276 tail = &commit_list_insert(commit, tail)->next;
3277 return tail;
3281 * Rewrite our list of parents. Note that this cannot
3282 * affect our TREESAME flags in any way - a commit is
3283 * always TREESAME to its simplification.
3285 for (p = commit->parents; p; p = p->next) {
3286 pst = locate_simplify_state(revs, p->item);
3287 p->item = pst->simplified;
3288 if (revs->first_parent_only)
3289 break;
3292 if (revs->first_parent_only)
3293 cnt = 1;
3294 else
3295 cnt = remove_duplicate_parents(revs, commit);
3298 * It is possible that we are a merge and one side branch
3299 * does not have any commit that touches the given paths;
3300 * in such a case, the immediate parent from that branch
3301 * will be rewritten to be the merge base.
3303 * o----X X: the commit we are looking at;
3304 * / / o: a commit that touches the paths;
3305 * ---o----'
3307 * Further, a merge of an independent branch that doesn't
3308 * touch the path will reduce to a treesame root parent:
3310 * ----o----X X: the commit we are looking at;
3311 * / o: a commit that touches the paths;
3312 * r r: a root commit not touching the paths
3314 * Detect and simplify both cases.
3316 if (1 < cnt) {
3317 int marked = mark_redundant_parents(commit);
3318 marked += mark_treesame_root_parents(commit);
3319 if (marked)
3320 marked -= leave_one_treesame_to_parent(revs, commit);
3321 if (marked)
3322 cnt = remove_marked_parents(revs, commit);
3326 * A commit simplifies to itself if it is a root, if it is
3327 * UNINTERESTING, if it touches the given paths, or if it is a
3328 * merge and its parents don't simplify to one relevant commit
3329 * (the first two cases are already handled at the beginning of
3330 * this function).
3332 * Otherwise, it simplifies to what its sole relevant parent
3333 * simplifies to.
3335 if (!cnt ||
3336 (commit->object.flags & UNINTERESTING) ||
3337 !(commit->object.flags & TREESAME) ||
3338 (parent = one_relevant_parent(revs, commit->parents)) == NULL ||
3339 (revs->show_pulls && (commit->object.flags & PULL_MERGE)))
3340 st->simplified = commit;
3341 else {
3342 pst = locate_simplify_state(revs, parent);
3343 st->simplified = pst->simplified;
3345 return tail;
3348 static void simplify_merges(struct rev_info *revs)
3350 struct commit_list *list, *next;
3351 struct commit_list *yet_to_do, **tail;
3352 struct commit *commit;
3354 if (!revs->prune)
3355 return;
3357 /* feed the list reversed */
3358 yet_to_do = NULL;
3359 for (list = revs->commits; list; list = next) {
3360 commit = list->item;
3361 next = list->next;
3363 * Do not free(list) here yet; the original list
3364 * is used later in this function.
3366 commit_list_insert(commit, &yet_to_do);
3368 while (yet_to_do) {
3369 list = yet_to_do;
3370 yet_to_do = NULL;
3371 tail = &yet_to_do;
3372 while (list) {
3373 commit = pop_commit(&list);
3374 tail = simplify_one(revs, commit, tail);
3378 /* clean up the result, removing the simplified ones */
3379 list = revs->commits;
3380 revs->commits = NULL;
3381 tail = &revs->commits;
3382 while (list) {
3383 struct merge_simplify_state *st;
3385 commit = pop_commit(&list);
3386 st = locate_simplify_state(revs, commit);
3387 if (st->simplified == commit)
3388 tail = &commit_list_insert(commit, tail)->next;
3392 static void set_children(struct rev_info *revs)
3394 struct commit_list *l;
3395 for (l = revs->commits; l; l = l->next) {
3396 struct commit *commit = l->item;
3397 struct commit_list *p;
3399 for (p = commit->parents; p; p = p->next)
3400 add_child(revs, p->item, commit);
3404 void reset_revision_walk(void)
3406 clear_object_flags(SEEN | ADDED | SHOWN | TOPO_WALK_EXPLORED | TOPO_WALK_INDEGREE);
3409 static int mark_uninteresting(const struct object_id *oid,
3410 struct packed_git *pack,
3411 uint32_t pos,
3412 void *cb)
3414 struct rev_info *revs = cb;
3415 struct object *o = lookup_unknown_object(revs->repo, oid);
3416 o->flags |= UNINTERESTING | SEEN;
3417 return 0;
3420 define_commit_slab(indegree_slab, int);
3421 define_commit_slab(author_date_slab, timestamp_t);
3423 struct topo_walk_info {
3424 timestamp_t min_generation;
3425 struct prio_queue explore_queue;
3426 struct prio_queue indegree_queue;
3427 struct prio_queue topo_queue;
3428 struct indegree_slab indegree;
3429 struct author_date_slab author_date;
3432 static int topo_walk_atexit_registered;
3433 static unsigned int count_explore_walked;
3434 static unsigned int count_indegree_walked;
3435 static unsigned int count_topo_walked;
3437 static void trace2_topo_walk_statistics_atexit(void)
3439 struct json_writer jw = JSON_WRITER_INIT;
3441 jw_object_begin(&jw, 0);
3442 jw_object_intmax(&jw, "count_explore_walked", count_explore_walked);
3443 jw_object_intmax(&jw, "count_indegree_walked", count_indegree_walked);
3444 jw_object_intmax(&jw, "count_topo_walked", count_topo_walked);
3445 jw_end(&jw);
3447 trace2_data_json("topo_walk", the_repository, "statistics", &jw);
3449 jw_release(&jw);
3452 static inline void test_flag_and_insert(struct prio_queue *q, struct commit *c, int flag)
3454 if (c->object.flags & flag)
3455 return;
3457 c->object.flags |= flag;
3458 prio_queue_put(q, c);
3461 static void explore_walk_step(struct rev_info *revs)
3463 struct topo_walk_info *info = revs->topo_walk_info;
3464 struct commit_list *p;
3465 struct commit *c = prio_queue_get(&info->explore_queue);
3467 if (!c)
3468 return;
3470 if (repo_parse_commit_gently(revs->repo, c, 1) < 0)
3471 return;
3473 count_explore_walked++;
3475 if (revs->sort_order == REV_SORT_BY_AUTHOR_DATE)
3476 record_author_date(&info->author_date, c);
3478 if (revs->max_age != -1 && (c->date < revs->max_age))
3479 c->object.flags |= UNINTERESTING;
3481 if (process_parents(revs, c, NULL, NULL) < 0)
3482 return;
3484 if (c->object.flags & UNINTERESTING)
3485 mark_parents_uninteresting(revs, c);
3487 for (p = c->parents; p; p = p->next)
3488 test_flag_and_insert(&info->explore_queue, p->item, TOPO_WALK_EXPLORED);
3491 static void explore_to_depth(struct rev_info *revs,
3492 timestamp_t gen_cutoff)
3494 struct topo_walk_info *info = revs->topo_walk_info;
3495 struct commit *c;
3496 while ((c = prio_queue_peek(&info->explore_queue)) &&
3497 commit_graph_generation(c) >= gen_cutoff)
3498 explore_walk_step(revs);
3501 static void indegree_walk_step(struct rev_info *revs)
3503 struct commit_list *p;
3504 struct topo_walk_info *info = revs->topo_walk_info;
3505 struct commit *c = prio_queue_get(&info->indegree_queue);
3507 if (!c)
3508 return;
3510 if (repo_parse_commit_gently(revs->repo, c, 1) < 0)
3511 return;
3513 count_indegree_walked++;
3515 explore_to_depth(revs, commit_graph_generation(c));
3517 for (p = c->parents; p; p = p->next) {
3518 struct commit *parent = p->item;
3519 int *pi = indegree_slab_at(&info->indegree, parent);
3521 if (repo_parse_commit_gently(revs->repo, parent, 1) < 0)
3522 return;
3524 if (*pi)
3525 (*pi)++;
3526 else
3527 *pi = 2;
3529 test_flag_and_insert(&info->indegree_queue, parent, TOPO_WALK_INDEGREE);
3531 if (revs->first_parent_only)
3532 return;
3536 static void compute_indegrees_to_depth(struct rev_info *revs,
3537 timestamp_t gen_cutoff)
3539 struct topo_walk_info *info = revs->topo_walk_info;
3540 struct commit *c;
3541 while ((c = prio_queue_peek(&info->indegree_queue)) &&
3542 commit_graph_generation(c) >= gen_cutoff)
3543 indegree_walk_step(revs);
3546 static void release_revisions_topo_walk_info(struct topo_walk_info *info)
3548 if (!info)
3549 return;
3550 clear_prio_queue(&info->explore_queue);
3551 clear_prio_queue(&info->indegree_queue);
3552 clear_prio_queue(&info->topo_queue);
3553 clear_indegree_slab(&info->indegree);
3554 clear_author_date_slab(&info->author_date);
3555 free(info);
3558 static void reset_topo_walk(struct rev_info *revs)
3560 release_revisions_topo_walk_info(revs->topo_walk_info);
3561 revs->topo_walk_info = NULL;
3564 static void init_topo_walk(struct rev_info *revs)
3566 struct topo_walk_info *info;
3567 struct commit_list *list;
3568 if (revs->topo_walk_info)
3569 reset_topo_walk(revs);
3571 revs->topo_walk_info = xmalloc(sizeof(struct topo_walk_info));
3572 info = revs->topo_walk_info;
3573 memset(info, 0, sizeof(struct topo_walk_info));
3575 init_indegree_slab(&info->indegree);
3576 memset(&info->explore_queue, 0, sizeof(info->explore_queue));
3577 memset(&info->indegree_queue, 0, sizeof(info->indegree_queue));
3578 memset(&info->topo_queue, 0, sizeof(info->topo_queue));
3580 switch (revs->sort_order) {
3581 default: /* REV_SORT_IN_GRAPH_ORDER */
3582 info->topo_queue.compare = NULL;
3583 break;
3584 case REV_SORT_BY_COMMIT_DATE:
3585 info->topo_queue.compare = compare_commits_by_commit_date;
3586 break;
3587 case REV_SORT_BY_AUTHOR_DATE:
3588 init_author_date_slab(&info->author_date);
3589 info->topo_queue.compare = compare_commits_by_author_date;
3590 info->topo_queue.cb_data = &info->author_date;
3591 break;
3594 info->explore_queue.compare = compare_commits_by_gen_then_commit_date;
3595 info->indegree_queue.compare = compare_commits_by_gen_then_commit_date;
3597 info->min_generation = GENERATION_NUMBER_INFINITY;
3598 for (list = revs->commits; list; list = list->next) {
3599 struct commit *c = list->item;
3600 timestamp_t generation;
3602 if (repo_parse_commit_gently(revs->repo, c, 1))
3603 continue;
3605 test_flag_and_insert(&info->explore_queue, c, TOPO_WALK_EXPLORED);
3606 test_flag_and_insert(&info->indegree_queue, c, TOPO_WALK_INDEGREE);
3608 generation = commit_graph_generation(c);
3609 if (generation < info->min_generation)
3610 info->min_generation = generation;
3612 *(indegree_slab_at(&info->indegree, c)) = 1;
3614 if (revs->sort_order == REV_SORT_BY_AUTHOR_DATE)
3615 record_author_date(&info->author_date, c);
3617 compute_indegrees_to_depth(revs, info->min_generation);
3619 for (list = revs->commits; list; list = list->next) {
3620 struct commit *c = list->item;
3622 if (*(indegree_slab_at(&info->indegree, c)) == 1)
3623 prio_queue_put(&info->topo_queue, c);
3627 * This is unfortunate; the initial tips need to be shown
3628 * in the order given from the revision traversal machinery.
3630 if (revs->sort_order == REV_SORT_IN_GRAPH_ORDER)
3631 prio_queue_reverse(&info->topo_queue);
3633 if (trace2_is_enabled() && !topo_walk_atexit_registered) {
3634 atexit(trace2_topo_walk_statistics_atexit);
3635 topo_walk_atexit_registered = 1;
3639 static struct commit *next_topo_commit(struct rev_info *revs)
3641 struct commit *c;
3642 struct topo_walk_info *info = revs->topo_walk_info;
3644 /* pop next off of topo_queue */
3645 c = prio_queue_get(&info->topo_queue);
3647 if (c)
3648 *(indegree_slab_at(&info->indegree, c)) = 0;
3650 return c;
3653 static void expand_topo_walk(struct rev_info *revs, struct commit *commit)
3655 struct commit_list *p;
3656 struct topo_walk_info *info = revs->topo_walk_info;
3657 if (process_parents(revs, commit, NULL, NULL) < 0) {
3658 if (!revs->ignore_missing_links)
3659 die("Failed to traverse parents of commit %s",
3660 oid_to_hex(&commit->object.oid));
3663 count_topo_walked++;
3665 for (p = commit->parents; p; p = p->next) {
3666 struct commit *parent = p->item;
3667 int *pi;
3668 timestamp_t generation;
3670 if (parent->object.flags & UNINTERESTING)
3671 continue;
3673 if (repo_parse_commit_gently(revs->repo, parent, 1) < 0)
3674 continue;
3676 generation = commit_graph_generation(parent);
3677 if (generation < info->min_generation) {
3678 info->min_generation = generation;
3679 compute_indegrees_to_depth(revs, info->min_generation);
3682 pi = indegree_slab_at(&info->indegree, parent);
3684 (*pi)--;
3685 if (*pi == 1)
3686 prio_queue_put(&info->topo_queue, parent);
3688 if (revs->first_parent_only)
3689 return;
3693 int prepare_revision_walk(struct rev_info *revs)
3695 int i;
3696 struct object_array old_pending;
3697 struct commit_list **next = &revs->commits;
3699 memcpy(&old_pending, &revs->pending, sizeof(old_pending));
3700 revs->pending.nr = 0;
3701 revs->pending.alloc = 0;
3702 revs->pending.objects = NULL;
3703 for (i = 0; i < old_pending.nr; i++) {
3704 struct object_array_entry *e = old_pending.objects + i;
3705 struct commit *commit = handle_commit(revs, e);
3706 if (commit) {
3707 if (!(commit->object.flags & SEEN)) {
3708 commit->object.flags |= SEEN;
3709 next = commit_list_append(commit, next);
3713 object_array_clear(&old_pending);
3715 /* Signal whether we need per-parent treesame decoration */
3716 if (revs->simplify_merges ||
3717 (revs->limited && limiting_can_increase_treesame(revs)))
3718 revs->treesame.name = "treesame";
3720 if (revs->exclude_promisor_objects) {
3721 for_each_packed_object(mark_uninteresting, revs,
3722 FOR_EACH_OBJECT_PROMISOR_ONLY);
3725 if (!revs->reflog_info)
3726 prepare_to_use_bloom_filter(revs);
3727 if (!revs->unsorted_input)
3728 commit_list_sort_by_date(&revs->commits);
3729 if (revs->no_walk)
3730 return 0;
3731 if (revs->limited) {
3732 if (limit_list(revs) < 0)
3733 return -1;
3734 if (revs->topo_order)
3735 sort_in_topological_order(&revs->commits, revs->sort_order);
3736 } else if (revs->topo_order)
3737 init_topo_walk(revs);
3738 if (revs->line_level_traverse && want_ancestry(revs))
3740 * At the moment we can only do line-level log with parent
3741 * rewriting by performing this expensive pre-filtering step.
3742 * If parent rewriting is not requested, then we rather
3743 * perform the line-level log filtering during the regular
3744 * history traversal.
3746 line_log_filter(revs);
3747 if (revs->simplify_merges)
3748 simplify_merges(revs);
3749 if (revs->children.name)
3750 set_children(revs);
3752 return 0;
3755 static enum rewrite_result rewrite_one_1(struct rev_info *revs,
3756 struct commit **pp,
3757 struct prio_queue *queue)
3759 for (;;) {
3760 struct commit *p = *pp;
3761 if (!revs->limited)
3762 if (process_parents(revs, p, NULL, queue) < 0)
3763 return rewrite_one_error;
3764 if (p->object.flags & UNINTERESTING)
3765 return rewrite_one_ok;
3766 if (!(p->object.flags & TREESAME))
3767 return rewrite_one_ok;
3768 if (!p->parents)
3769 return rewrite_one_noparents;
3770 if (!(p = one_relevant_parent(revs, p->parents)))
3771 return rewrite_one_ok;
3772 *pp = p;
3776 static void merge_queue_into_list(struct prio_queue *q, struct commit_list **list)
3778 while (q->nr) {
3779 struct commit *item = prio_queue_peek(q);
3780 struct commit_list *p = *list;
3782 if (p && p->item->date >= item->date)
3783 list = &p->next;
3784 else {
3785 p = commit_list_insert(item, list);
3786 list = &p->next; /* skip newly added item */
3787 prio_queue_get(q); /* pop item */
3792 static enum rewrite_result rewrite_one(struct rev_info *revs, struct commit **pp)
3794 struct prio_queue queue = { compare_commits_by_commit_date };
3795 enum rewrite_result ret = rewrite_one_1(revs, pp, &queue);
3796 merge_queue_into_list(&queue, &revs->commits);
3797 clear_prio_queue(&queue);
3798 return ret;
3801 int rewrite_parents(struct rev_info *revs, struct commit *commit,
3802 rewrite_parent_fn_t rewrite_parent)
3804 struct commit_list **pp = &commit->parents;
3805 while (*pp) {
3806 struct commit_list *parent = *pp;
3807 switch (rewrite_parent(revs, &parent->item)) {
3808 case rewrite_one_ok:
3809 break;
3810 case rewrite_one_noparents:
3811 *pp = parent->next;
3812 continue;
3813 case rewrite_one_error:
3814 return -1;
3816 pp = &parent->next;
3818 remove_duplicate_parents(revs, commit);
3819 return 0;
3822 static int commit_match(struct commit *commit, struct rev_info *opt)
3824 int retval;
3825 const char *encoding;
3826 const char *message;
3827 struct strbuf buf = STRBUF_INIT;
3829 if (!opt->grep_filter.pattern_list && !opt->grep_filter.header_list)
3830 return 1;
3832 /* Prepend "fake" headers as needed */
3833 if (opt->grep_filter.use_reflog_filter) {
3834 strbuf_addstr(&buf, "reflog ");
3835 get_reflog_message(&buf, opt->reflog_info);
3836 strbuf_addch(&buf, '\n');
3840 * We grep in the user's output encoding, under the assumption that it
3841 * is the encoding they are most likely to write their grep pattern
3842 * for. In addition, it means we will match the "notes" encoding below,
3843 * so we will not end up with a buffer that has two different encodings
3844 * in it.
3846 encoding = get_log_output_encoding();
3847 message = logmsg_reencode(commit, NULL, encoding);
3849 /* Copy the commit to temporary if we are using "fake" headers */
3850 if (buf.len)
3851 strbuf_addstr(&buf, message);
3853 if (opt->grep_filter.header_list && opt->mailmap) {
3854 const char *commit_headers[] = { "author ", "committer ", NULL };
3856 if (!buf.len)
3857 strbuf_addstr(&buf, message);
3859 apply_mailmap_to_header(&buf, commit_headers, opt->mailmap);
3862 /* Append "fake" message parts as needed */
3863 if (opt->show_notes) {
3864 if (!buf.len)
3865 strbuf_addstr(&buf, message);
3866 format_display_notes(&commit->object.oid, &buf, encoding, 1);
3870 * Find either in the original commit message, or in the temporary.
3871 * Note that we cast away the constness of "message" here. It is
3872 * const because it may come from the cached commit buffer. That's OK,
3873 * because we know that it is modifiable heap memory, and that while
3874 * grep_buffer may modify it for speed, it will restore any
3875 * changes before returning.
3877 if (buf.len)
3878 retval = grep_buffer(&opt->grep_filter, buf.buf, buf.len);
3879 else
3880 retval = grep_buffer(&opt->grep_filter,
3881 (char *)message, strlen(message));
3882 strbuf_release(&buf);
3883 unuse_commit_buffer(commit, message);
3884 return retval;
3887 static inline int want_ancestry(const struct rev_info *revs)
3889 return (revs->rewrite_parents || revs->children.name);
3893 * Return a timestamp to be used for --since/--until comparisons for this
3894 * commit, based on the revision options.
3896 static timestamp_t comparison_date(const struct rev_info *revs,
3897 struct commit *commit)
3899 return revs->reflog_info ?
3900 get_reflog_timestamp(revs->reflog_info) :
3901 commit->date;
3904 enum commit_action get_commit_action(struct rev_info *revs, struct commit *commit)
3906 if (commit->object.flags & SHOWN)
3907 return commit_ignore;
3908 if (revs->unpacked && has_object_pack(&commit->object.oid))
3909 return commit_ignore;
3910 if (revs->no_kept_objects) {
3911 if (has_object_kept_pack(&commit->object.oid,
3912 revs->keep_pack_cache_flags))
3913 return commit_ignore;
3915 if (commit->object.flags & UNINTERESTING)
3916 return commit_ignore;
3917 if (revs->line_level_traverse && !want_ancestry(revs)) {
3919 * In case of line-level log with parent rewriting
3920 * prepare_revision_walk() already took care of all line-level
3921 * log filtering, and there is nothing left to do here.
3923 * If parent rewriting was not requested, then this is the
3924 * place to perform the line-level log filtering. Notably,
3925 * this check, though expensive, must come before the other,
3926 * cheaper filtering conditions, because the tracked line
3927 * ranges must be adjusted even when the commit will end up
3928 * being ignored based on other conditions.
3930 if (!line_log_process_ranges_arbitrary_commit(revs, commit))
3931 return commit_ignore;
3933 if (revs->min_age != -1 &&
3934 comparison_date(revs, commit) > revs->min_age)
3935 return commit_ignore;
3936 if (revs->max_age_as_filter != -1 &&
3937 comparison_date(revs, commit) < revs->max_age_as_filter)
3938 return commit_ignore;
3939 if (revs->min_parents || (revs->max_parents >= 0)) {
3940 int n = commit_list_count(commit->parents);
3941 if ((n < revs->min_parents) ||
3942 ((revs->max_parents >= 0) && (n > revs->max_parents)))
3943 return commit_ignore;
3945 if (!commit_match(commit, revs))
3946 return commit_ignore;
3947 if (revs->prune && revs->dense) {
3948 /* Commit without changes? */
3949 if (commit->object.flags & TREESAME) {
3950 int n;
3951 struct commit_list *p;
3952 /* drop merges unless we want parenthood */
3953 if (!want_ancestry(revs))
3954 return commit_ignore;
3956 if (revs->show_pulls && (commit->object.flags & PULL_MERGE))
3957 return commit_show;
3960 * If we want ancestry, then need to keep any merges
3961 * between relevant commits to tie together topology.
3962 * For consistency with TREESAME and simplification
3963 * use "relevant" here rather than just INTERESTING,
3964 * to treat bottom commit(s) as part of the topology.
3966 for (n = 0, p = commit->parents; p; p = p->next)
3967 if (relevant_commit(p->item))
3968 if (++n >= 2)
3969 return commit_show;
3970 return commit_ignore;
3973 return commit_show;
3976 define_commit_slab(saved_parents, struct commit_list *);
3978 #define EMPTY_PARENT_LIST ((struct commit_list *)-1)
3981 * You may only call save_parents() once per commit (this is checked
3982 * for non-root commits).
3984 static void save_parents(struct rev_info *revs, struct commit *commit)
3986 struct commit_list **pp;
3988 if (!revs->saved_parents_slab) {
3989 revs->saved_parents_slab = xmalloc(sizeof(struct saved_parents));
3990 init_saved_parents(revs->saved_parents_slab);
3993 pp = saved_parents_at(revs->saved_parents_slab, commit);
3996 * When walking with reflogs, we may visit the same commit
3997 * several times: once for each appearance in the reflog.
3999 * In this case, save_parents() will be called multiple times.
4000 * We want to keep only the first set of parents. We need to
4001 * store a sentinel value for an empty (i.e., NULL) parent
4002 * list to distinguish it from a not-yet-saved list, however.
4004 if (*pp)
4005 return;
4006 if (commit->parents)
4007 *pp = copy_commit_list(commit->parents);
4008 else
4009 *pp = EMPTY_PARENT_LIST;
4012 static void free_saved_parents(struct rev_info *revs)
4014 if (revs->saved_parents_slab)
4015 clear_saved_parents(revs->saved_parents_slab);
4018 struct commit_list *get_saved_parents(struct rev_info *revs, const struct commit *commit)
4020 struct commit_list *parents;
4022 if (!revs->saved_parents_slab)
4023 return commit->parents;
4025 parents = *saved_parents_at(revs->saved_parents_slab, commit);
4026 if (parents == EMPTY_PARENT_LIST)
4027 return NULL;
4028 return parents;
4031 enum commit_action simplify_commit(struct rev_info *revs, struct commit *commit)
4033 enum commit_action action = get_commit_action(revs, commit);
4035 if (action == commit_show &&
4036 revs->prune && revs->dense && want_ancestry(revs)) {
4038 * --full-diff on simplified parents is no good: it
4039 * will show spurious changes from the commits that
4040 * were elided. So we save the parents on the side
4041 * when --full-diff is in effect.
4043 if (revs->full_diff)
4044 save_parents(revs, commit);
4045 if (rewrite_parents(revs, commit, rewrite_one) < 0)
4046 return commit_error;
4048 return action;
4051 static void track_linear(struct rev_info *revs, struct commit *commit)
4053 if (revs->track_first_time) {
4054 revs->linear = 1;
4055 revs->track_first_time = 0;
4056 } else {
4057 struct commit_list *p;
4058 for (p = revs->previous_parents; p; p = p->next)
4059 if (p->item == NULL || /* first commit */
4060 oideq(&p->item->object.oid, &commit->object.oid))
4061 break;
4062 revs->linear = p != NULL;
4064 if (revs->reverse) {
4065 if (revs->linear)
4066 commit->object.flags |= TRACK_LINEAR;
4068 free_commit_list(revs->previous_parents);
4069 revs->previous_parents = copy_commit_list(commit->parents);
4072 static struct commit *get_revision_1(struct rev_info *revs)
4074 while (1) {
4075 struct commit *commit;
4077 if (revs->reflog_info)
4078 commit = next_reflog_entry(revs->reflog_info);
4079 else if (revs->topo_walk_info)
4080 commit = next_topo_commit(revs);
4081 else
4082 commit = pop_commit(&revs->commits);
4084 if (!commit)
4085 return NULL;
4087 if (revs->reflog_info)
4088 commit->object.flags &= ~(ADDED | SEEN | SHOWN);
4091 * If we haven't done the list limiting, we need to look at
4092 * the parents here. We also need to do the date-based limiting
4093 * that we'd otherwise have done in limit_list().
4095 if (!revs->limited) {
4096 if (revs->max_age != -1 &&
4097 comparison_date(revs, commit) < revs->max_age)
4098 continue;
4100 if (revs->reflog_info)
4101 try_to_simplify_commit(revs, commit);
4102 else if (revs->topo_walk_info)
4103 expand_topo_walk(revs, commit);
4104 else if (process_parents(revs, commit, &revs->commits, NULL) < 0) {
4105 if (!revs->ignore_missing_links)
4106 die("Failed to traverse parents of commit %s",
4107 oid_to_hex(&commit->object.oid));
4111 switch (simplify_commit(revs, commit)) {
4112 case commit_ignore:
4113 continue;
4114 case commit_error:
4115 die("Failed to simplify parents of commit %s",
4116 oid_to_hex(&commit->object.oid));
4117 default:
4118 if (revs->track_linear)
4119 track_linear(revs, commit);
4120 return commit;
4126 * Return true for entries that have not yet been shown. (This is an
4127 * object_array_each_func_t.)
4129 static int entry_unshown(struct object_array_entry *entry, void *cb_data_unused)
4131 return !(entry->item->flags & SHOWN);
4135 * If array is on the verge of a realloc, garbage-collect any entries
4136 * that have already been shown to try to free up some space.
4138 static void gc_boundary(struct object_array *array)
4140 if (array->nr == array->alloc)
4141 object_array_filter(array, entry_unshown, NULL);
4144 static void create_boundary_commit_list(struct rev_info *revs)
4146 unsigned i;
4147 struct commit *c;
4148 struct object_array *array = &revs->boundary_commits;
4149 struct object_array_entry *objects = array->objects;
4152 * If revs->commits is non-NULL at this point, an error occurred in
4153 * get_revision_1(). Ignore the error and continue printing the
4154 * boundary commits anyway. (This is what the code has always
4155 * done.)
4157 free_commit_list(revs->commits);
4158 revs->commits = NULL;
4161 * Put all of the actual boundary commits from revs->boundary_commits
4162 * into revs->commits
4164 for (i = 0; i < array->nr; i++) {
4165 c = (struct commit *)(objects[i].item);
4166 if (!c)
4167 continue;
4168 if (!(c->object.flags & CHILD_SHOWN))
4169 continue;
4170 if (c->object.flags & (SHOWN | BOUNDARY))
4171 continue;
4172 c->object.flags |= BOUNDARY;
4173 commit_list_insert(c, &revs->commits);
4177 * If revs->topo_order is set, sort the boundary commits
4178 * in topological order
4180 sort_in_topological_order(&revs->commits, revs->sort_order);
4183 static struct commit *get_revision_internal(struct rev_info *revs)
4185 struct commit *c = NULL;
4186 struct commit_list *l;
4188 if (revs->boundary == 2) {
4190 * All of the normal commits have already been returned,
4191 * and we are now returning boundary commits.
4192 * create_boundary_commit_list() has populated
4193 * revs->commits with the remaining commits to return.
4195 c = pop_commit(&revs->commits);
4196 if (c)
4197 c->object.flags |= SHOWN;
4198 return c;
4202 * If our max_count counter has reached zero, then we are done. We
4203 * don't simply return NULL because we still might need to show
4204 * boundary commits. But we want to avoid calling get_revision_1, which
4205 * might do a considerable amount of work finding the next commit only
4206 * for us to throw it away.
4208 * If it is non-zero, then either we don't have a max_count at all
4209 * (-1), or it is still counting, in which case we decrement.
4211 if (revs->max_count) {
4212 c = get_revision_1(revs);
4213 if (c) {
4214 while (revs->skip_count > 0) {
4215 revs->skip_count--;
4216 c = get_revision_1(revs);
4217 if (!c)
4218 break;
4222 if (revs->max_count > 0)
4223 revs->max_count--;
4226 if (c)
4227 c->object.flags |= SHOWN;
4229 if (!revs->boundary)
4230 return c;
4232 if (!c) {
4234 * get_revision_1() runs out the commits, and
4235 * we are done computing the boundaries.
4236 * switch to boundary commits output mode.
4238 revs->boundary = 2;
4241 * Update revs->commits to contain the list of
4242 * boundary commits.
4244 create_boundary_commit_list(revs);
4246 return get_revision_internal(revs);
4250 * boundary commits are the commits that are parents of the
4251 * ones we got from get_revision_1() but they themselves are
4252 * not returned from get_revision_1(). Before returning
4253 * 'c', we need to mark its parents that they could be boundaries.
4256 for (l = c->parents; l; l = l->next) {
4257 struct object *p;
4258 p = &(l->item->object);
4259 if (p->flags & (CHILD_SHOWN | SHOWN))
4260 continue;
4261 p->flags |= CHILD_SHOWN;
4262 gc_boundary(&revs->boundary_commits);
4263 add_object_array(p, NULL, &revs->boundary_commits);
4266 return c;
4269 struct commit *get_revision(struct rev_info *revs)
4271 struct commit *c;
4272 struct commit_list *reversed;
4274 if (revs->reverse) {
4275 reversed = NULL;
4276 while ((c = get_revision_internal(revs)))
4277 commit_list_insert(c, &reversed);
4278 revs->commits = reversed;
4279 revs->reverse = 0;
4280 revs->reverse_output_stage = 1;
4283 if (revs->reverse_output_stage) {
4284 c = pop_commit(&revs->commits);
4285 if (revs->track_linear)
4286 revs->linear = !!(c && c->object.flags & TRACK_LINEAR);
4287 return c;
4290 c = get_revision_internal(revs);
4291 if (c && revs->graph)
4292 graph_update(revs->graph, c);
4293 if (!c) {
4294 free_saved_parents(revs);
4295 free_commit_list(revs->previous_parents);
4296 revs->previous_parents = NULL;
4298 return c;
4301 const char *get_revision_mark(const struct rev_info *revs, const struct commit *commit)
4303 if (commit->object.flags & BOUNDARY)
4304 return "-";
4305 else if (commit->object.flags & UNINTERESTING)
4306 return "^";
4307 else if (commit->object.flags & PATCHSAME)
4308 return "=";
4309 else if (!revs || revs->left_right) {
4310 if (commit->object.flags & SYMMETRIC_LEFT)
4311 return "<";
4312 else
4313 return ">";
4314 } else if (revs->graph)
4315 return "*";
4316 else if (revs->cherry_mark)
4317 return "+";
4318 return "";
4321 void put_revision_mark(const struct rev_info *revs, const struct commit *commit)
4323 const char *mark = get_revision_mark(revs, commit);
4324 if (!strlen(mark))
4325 return;
4326 fputs(mark, stdout);
4327 putchar(' ');