Documentation: clean up a few misspelled word typos
[git/debian.git] / revision.c
blobee702e498a07f6e8364e046eeb6e7d9212874c3e
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,
123 const struct hashmap_entry *eptr,
124 const struct hashmap_entry *entry_or_key,
125 const void *keydata)
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;
376 struct commit *commit;
379 * If the repository has commit graphs, we try to opportunistically
380 * look up the object ID in those graphs. Like this, we can avoid
381 * parsing commit data from disk.
383 commit = lookup_commit_in_graph(revs->repo, oid);
384 if (commit)
385 object = &commit->object;
386 else
387 object = parse_object(revs->repo, oid);
389 if (!object) {
390 if (revs->ignore_missing)
391 return object;
392 if (revs->exclude_promisor_objects && is_promisor_object(oid))
393 return NULL;
394 die("bad object %s", name);
396 object->flags |= flags;
397 return object;
400 void add_pending_oid(struct rev_info *revs, const char *name,
401 const struct object_id *oid, unsigned int flags)
403 struct object *object = get_reference(revs, name, oid, flags);
404 add_pending_object(revs, object, name);
407 static struct commit *handle_commit(struct rev_info *revs,
408 struct object_array_entry *entry)
410 struct object *object = entry->item;
411 const char *name = entry->name;
412 const char *path = entry->path;
413 unsigned int mode = entry->mode;
414 unsigned long flags = object->flags;
417 * Tag object? Look what it points to..
419 while (object->type == OBJ_TAG) {
420 struct tag *tag = (struct tag *) object;
421 if (revs->tag_objects && !(flags & UNINTERESTING))
422 add_pending_object(revs, object, tag->tag);
423 object = parse_object(revs->repo, get_tagged_oid(tag));
424 if (!object) {
425 if (revs->ignore_missing_links || (flags & UNINTERESTING))
426 return NULL;
427 if (revs->exclude_promisor_objects &&
428 is_promisor_object(&tag->tagged->oid))
429 return NULL;
430 die("bad object %s", oid_to_hex(&tag->tagged->oid));
432 object->flags |= flags;
434 * We'll handle the tagged object by looping or dropping
435 * through to the non-tag handlers below. Do not
436 * propagate path data from the tag's pending entry.
438 path = NULL;
439 mode = 0;
443 * Commit object? Just return it, we'll do all the complex
444 * reachability crud.
446 if (object->type == OBJ_COMMIT) {
447 struct commit *commit = (struct commit *)object;
449 if (repo_parse_commit(revs->repo, commit) < 0)
450 die("unable to parse commit %s", name);
451 if (flags & UNINTERESTING) {
452 mark_parents_uninteresting(revs, commit);
454 if (!revs->topo_order || !generation_numbers_enabled(the_repository))
455 revs->limited = 1;
457 if (revs->sources) {
458 char **slot = revision_sources_at(revs->sources, commit);
460 if (!*slot)
461 *slot = xstrdup(name);
463 return commit;
467 * Tree object? Either mark it uninteresting, or add it
468 * to the list of objects to look at later..
470 if (object->type == OBJ_TREE) {
471 struct tree *tree = (struct tree *)object;
472 if (!revs->tree_objects)
473 return NULL;
474 if (flags & UNINTERESTING) {
475 mark_tree_contents_uninteresting(revs->repo, tree);
476 return NULL;
478 add_pending_object_with_path(revs, object, name, mode, path);
479 return NULL;
483 * Blob object? You know the drill by now..
485 if (object->type == OBJ_BLOB) {
486 if (!revs->blob_objects)
487 return NULL;
488 if (flags & UNINTERESTING)
489 return NULL;
490 add_pending_object_with_path(revs, object, name, mode, path);
491 return NULL;
493 die("%s is unknown object", name);
496 static int everybody_uninteresting(struct commit_list *orig,
497 struct commit **interesting_cache)
499 struct commit_list *list = orig;
501 if (*interesting_cache) {
502 struct commit *commit = *interesting_cache;
503 if (!(commit->object.flags & UNINTERESTING))
504 return 0;
507 while (list) {
508 struct commit *commit = list->item;
509 list = list->next;
510 if (commit->object.flags & UNINTERESTING)
511 continue;
513 *interesting_cache = commit;
514 return 0;
516 return 1;
520 * A definition of "relevant" commit that we can use to simplify limited graphs
521 * by eliminating side branches.
523 * A "relevant" commit is one that is !UNINTERESTING (ie we are including it
524 * in our list), or that is a specified BOTTOM commit. Then after computing
525 * a limited list, during processing we can generally ignore boundary merges
526 * coming from outside the graph, (ie from irrelevant parents), and treat
527 * those merges as if they were single-parent. TREESAME is defined to consider
528 * only relevant parents, if any. If we are TREESAME to our on-graph parents,
529 * we don't care if we were !TREESAME to non-graph parents.
531 * Treating bottom commits as relevant ensures that a limited graph's
532 * connection to the actual bottom commit is not viewed as a side branch, but
533 * treated as part of the graph. For example:
535 * ....Z...A---X---o---o---B
536 * . /
537 * W---Y
539 * When computing "A..B", the A-X connection is at least as important as
540 * Y-X, despite A being flagged UNINTERESTING.
542 * And when computing --ancestry-path "A..B", the A-X connection is more
543 * important than Y-X, despite both A and Y being flagged UNINTERESTING.
545 static inline int relevant_commit(struct commit *commit)
547 return (commit->object.flags & (UNINTERESTING | BOTTOM)) != UNINTERESTING;
551 * Return a single relevant commit from a parent list. If we are a TREESAME
552 * commit, and this selects one of our parents, then we can safely simplify to
553 * that parent.
555 static struct commit *one_relevant_parent(const struct rev_info *revs,
556 struct commit_list *orig)
558 struct commit_list *list = orig;
559 struct commit *relevant = NULL;
561 if (!orig)
562 return NULL;
565 * For 1-parent commits, or if first-parent-only, then return that
566 * first parent (even if not "relevant" by the above definition).
567 * TREESAME will have been set purely on that parent.
569 if (revs->first_parent_only || !orig->next)
570 return orig->item;
573 * For multi-parent commits, identify a sole relevant parent, if any.
574 * If we have only one relevant parent, then TREESAME will be set purely
575 * with regard to that parent, and we can simplify accordingly.
577 * If we have more than one relevant parent, or no relevant parents
578 * (and multiple irrelevant ones), then we can't select a parent here
579 * and return NULL.
581 while (list) {
582 struct commit *commit = list->item;
583 list = list->next;
584 if (relevant_commit(commit)) {
585 if (relevant)
586 return NULL;
587 relevant = commit;
590 return relevant;
594 * The goal is to get REV_TREE_NEW as the result only if the
595 * diff consists of all '+' (and no other changes), REV_TREE_OLD
596 * if the whole diff is removal of old data, and otherwise
597 * REV_TREE_DIFFERENT (of course if the trees are the same we
598 * want REV_TREE_SAME).
600 * The only time we care about the distinction is when
601 * remove_empty_trees is in effect, in which case we care only about
602 * whether the whole change is REV_TREE_NEW, or if there's another type
603 * of change. Which means we can stop the diff early in either of these
604 * cases:
606 * 1. We're not using remove_empty_trees at all.
608 * 2. We saw anything except REV_TREE_NEW.
610 #define REV_TREE_SAME 0
611 #define REV_TREE_NEW 1 /* Only new files */
612 #define REV_TREE_OLD 2 /* Only files removed */
613 #define REV_TREE_DIFFERENT 3 /* Mixed changes */
614 static int tree_difference = REV_TREE_SAME;
616 static void file_add_remove(struct diff_options *options,
617 int addremove, unsigned mode,
618 const struct object_id *oid,
619 int oid_valid,
620 const char *fullpath, unsigned dirty_submodule)
622 int diff = addremove == '+' ? REV_TREE_NEW : REV_TREE_OLD;
623 struct rev_info *revs = options->change_fn_data;
625 tree_difference |= diff;
626 if (!revs->remove_empty_trees || tree_difference != REV_TREE_NEW)
627 options->flags.has_changes = 1;
630 static void file_change(struct diff_options *options,
631 unsigned old_mode, unsigned new_mode,
632 const struct object_id *old_oid,
633 const struct object_id *new_oid,
634 int old_oid_valid, int new_oid_valid,
635 const char *fullpath,
636 unsigned old_dirty_submodule, unsigned new_dirty_submodule)
638 tree_difference = REV_TREE_DIFFERENT;
639 options->flags.has_changes = 1;
642 static int bloom_filter_atexit_registered;
643 static unsigned int count_bloom_filter_maybe;
644 static unsigned int count_bloom_filter_definitely_not;
645 static unsigned int count_bloom_filter_false_positive;
646 static unsigned int count_bloom_filter_not_present;
648 static void trace2_bloom_filter_statistics_atexit(void)
650 struct json_writer jw = JSON_WRITER_INIT;
652 jw_object_begin(&jw, 0);
653 jw_object_intmax(&jw, "filter_not_present", count_bloom_filter_not_present);
654 jw_object_intmax(&jw, "maybe", count_bloom_filter_maybe);
655 jw_object_intmax(&jw, "definitely_not", count_bloom_filter_definitely_not);
656 jw_object_intmax(&jw, "false_positive", count_bloom_filter_false_positive);
657 jw_end(&jw);
659 trace2_data_json("bloom", the_repository, "statistics", &jw);
661 jw_release(&jw);
664 static int forbid_bloom_filters(struct pathspec *spec)
666 if (spec->has_wildcard)
667 return 1;
668 if (spec->nr > 1)
669 return 1;
670 if (spec->magic & ~PATHSPEC_LITERAL)
671 return 1;
672 if (spec->nr && (spec->items[0].magic & ~PATHSPEC_LITERAL))
673 return 1;
675 return 0;
678 static void prepare_to_use_bloom_filter(struct rev_info *revs)
680 struct pathspec_item *pi;
681 char *path_alloc = NULL;
682 const char *path, *p;
683 size_t len;
684 int path_component_nr = 1;
686 if (!revs->commits)
687 return;
689 if (forbid_bloom_filters(&revs->prune_data))
690 return;
692 repo_parse_commit(revs->repo, revs->commits->item);
694 revs->bloom_filter_settings = get_bloom_filter_settings(revs->repo);
695 if (!revs->bloom_filter_settings)
696 return;
698 if (!revs->pruning.pathspec.nr)
699 return;
701 pi = &revs->pruning.pathspec.items[0];
703 /* remove single trailing slash from path, if needed */
704 if (pi->len > 0 && pi->match[pi->len - 1] == '/') {
705 path_alloc = xmemdupz(pi->match, pi->len - 1);
706 path = path_alloc;
707 } else
708 path = pi->match;
710 len = strlen(path);
711 if (!len) {
712 revs->bloom_filter_settings = NULL;
713 free(path_alloc);
714 return;
717 p = path;
718 while (*p) {
720 * At this point, the path is normalized to use Unix-style
721 * path separators. This is required due to how the
722 * changed-path Bloom filters store the paths.
724 if (*p == '/')
725 path_component_nr++;
726 p++;
729 revs->bloom_keys_nr = path_component_nr;
730 ALLOC_ARRAY(revs->bloom_keys, revs->bloom_keys_nr);
732 fill_bloom_key(path, len, &revs->bloom_keys[0],
733 revs->bloom_filter_settings);
734 path_component_nr = 1;
736 p = path + len - 1;
737 while (p > path) {
738 if (*p == '/')
739 fill_bloom_key(path, p - path,
740 &revs->bloom_keys[path_component_nr++],
741 revs->bloom_filter_settings);
742 p--;
745 if (trace2_is_enabled() && !bloom_filter_atexit_registered) {
746 atexit(trace2_bloom_filter_statistics_atexit);
747 bloom_filter_atexit_registered = 1;
750 free(path_alloc);
753 static int check_maybe_different_in_bloom_filter(struct rev_info *revs,
754 struct commit *commit)
756 struct bloom_filter *filter;
757 int result = 1, j;
759 if (!revs->repo->objects->commit_graph)
760 return -1;
762 if (commit_graph_generation(commit) == GENERATION_NUMBER_INFINITY)
763 return -1;
765 filter = get_bloom_filter(revs->repo, commit);
767 if (!filter) {
768 count_bloom_filter_not_present++;
769 return -1;
772 for (j = 0; result && j < revs->bloom_keys_nr; j++) {
773 result = bloom_filter_contains(filter,
774 &revs->bloom_keys[j],
775 revs->bloom_filter_settings);
778 if (result)
779 count_bloom_filter_maybe++;
780 else
781 count_bloom_filter_definitely_not++;
783 return result;
786 static int rev_compare_tree(struct rev_info *revs,
787 struct commit *parent, struct commit *commit, int nth_parent)
789 struct tree *t1 = get_commit_tree(parent);
790 struct tree *t2 = get_commit_tree(commit);
791 int bloom_ret = 1;
793 if (!t1)
794 return REV_TREE_NEW;
795 if (!t2)
796 return REV_TREE_OLD;
798 if (revs->simplify_by_decoration) {
800 * If we are simplifying by decoration, then the commit
801 * is worth showing if it has a tag pointing at it.
803 if (get_name_decoration(&commit->object))
804 return REV_TREE_DIFFERENT;
806 * A commit that is not pointed by a tag is uninteresting
807 * if we are not limited by path. This means that you will
808 * see the usual "commits that touch the paths" plus any
809 * tagged commit by specifying both --simplify-by-decoration
810 * and pathspec.
812 if (!revs->prune_data.nr)
813 return REV_TREE_SAME;
816 if (revs->bloom_keys_nr && !nth_parent) {
817 bloom_ret = check_maybe_different_in_bloom_filter(revs, commit);
819 if (bloom_ret == 0)
820 return REV_TREE_SAME;
823 tree_difference = REV_TREE_SAME;
824 revs->pruning.flags.has_changes = 0;
825 diff_tree_oid(&t1->object.oid, &t2->object.oid, "", &revs->pruning);
827 if (!nth_parent)
828 if (bloom_ret == 1 && tree_difference == REV_TREE_SAME)
829 count_bloom_filter_false_positive++;
831 return tree_difference;
834 static int rev_same_tree_as_empty(struct rev_info *revs, struct commit *commit)
836 struct tree *t1 = get_commit_tree(commit);
838 if (!t1)
839 return 0;
841 tree_difference = REV_TREE_SAME;
842 revs->pruning.flags.has_changes = 0;
843 diff_tree_oid(NULL, &t1->object.oid, "", &revs->pruning);
845 return tree_difference == REV_TREE_SAME;
848 struct treesame_state {
849 unsigned int nparents;
850 unsigned char treesame[FLEX_ARRAY];
853 static struct treesame_state *initialise_treesame(struct rev_info *revs, struct commit *commit)
855 unsigned n = commit_list_count(commit->parents);
856 struct treesame_state *st = xcalloc(1, st_add(sizeof(*st), n));
857 st->nparents = n;
858 add_decoration(&revs->treesame, &commit->object, st);
859 return st;
863 * Must be called immediately after removing the nth_parent from a commit's
864 * parent list, if we are maintaining the per-parent treesame[] decoration.
865 * This does not recalculate the master TREESAME flag - update_treesame()
866 * should be called to update it after a sequence of treesame[] modifications
867 * that may have affected it.
869 static int compact_treesame(struct rev_info *revs, struct commit *commit, unsigned nth_parent)
871 struct treesame_state *st;
872 int old_same;
874 if (!commit->parents) {
876 * Have just removed the only parent from a non-merge.
877 * Different handling, as we lack decoration.
879 if (nth_parent != 0)
880 die("compact_treesame %u", nth_parent);
881 old_same = !!(commit->object.flags & TREESAME);
882 if (rev_same_tree_as_empty(revs, commit))
883 commit->object.flags |= TREESAME;
884 else
885 commit->object.flags &= ~TREESAME;
886 return old_same;
889 st = lookup_decoration(&revs->treesame, &commit->object);
890 if (!st || nth_parent >= st->nparents)
891 die("compact_treesame %u", nth_parent);
893 old_same = st->treesame[nth_parent];
894 memmove(st->treesame + nth_parent,
895 st->treesame + nth_parent + 1,
896 st->nparents - nth_parent - 1);
899 * If we've just become a non-merge commit, update TREESAME
900 * immediately, and remove the no-longer-needed decoration.
901 * If still a merge, defer update until update_treesame().
903 if (--st->nparents == 1) {
904 if (commit->parents->next)
905 die("compact_treesame parents mismatch");
906 if (st->treesame[0] && revs->dense)
907 commit->object.flags |= TREESAME;
908 else
909 commit->object.flags &= ~TREESAME;
910 free(add_decoration(&revs->treesame, &commit->object, NULL));
913 return old_same;
916 static unsigned update_treesame(struct rev_info *revs, struct commit *commit)
918 if (commit->parents && commit->parents->next) {
919 unsigned n;
920 struct treesame_state *st;
921 struct commit_list *p;
922 unsigned relevant_parents;
923 unsigned relevant_change, irrelevant_change;
925 st = lookup_decoration(&revs->treesame, &commit->object);
926 if (!st)
927 die("update_treesame %s", oid_to_hex(&commit->object.oid));
928 relevant_parents = 0;
929 relevant_change = irrelevant_change = 0;
930 for (p = commit->parents, n = 0; p; n++, p = p->next) {
931 if (relevant_commit(p->item)) {
932 relevant_change |= !st->treesame[n];
933 relevant_parents++;
934 } else
935 irrelevant_change |= !st->treesame[n];
937 if (relevant_parents ? relevant_change : irrelevant_change)
938 commit->object.flags &= ~TREESAME;
939 else
940 commit->object.flags |= TREESAME;
943 return commit->object.flags & TREESAME;
946 static inline int limiting_can_increase_treesame(const struct rev_info *revs)
949 * TREESAME is irrelevant unless prune && dense;
950 * if simplify_history is set, we can't have a mixture of TREESAME and
951 * !TREESAME INTERESTING parents (and we don't have treesame[]
952 * decoration anyway);
953 * if first_parent_only is set, then the TREESAME flag is locked
954 * against the first parent (and again we lack treesame[] decoration).
956 return revs->prune && revs->dense &&
957 !revs->simplify_history &&
958 !revs->first_parent_only;
961 static void try_to_simplify_commit(struct rev_info *revs, struct commit *commit)
963 struct commit_list **pp, *parent;
964 struct treesame_state *ts = NULL;
965 int relevant_change = 0, irrelevant_change = 0;
966 int relevant_parents, nth_parent;
969 * If we don't do pruning, everything is interesting
971 if (!revs->prune)
972 return;
974 if (!get_commit_tree(commit))
975 return;
977 if (!commit->parents) {
978 if (rev_same_tree_as_empty(revs, commit))
979 commit->object.flags |= TREESAME;
980 return;
984 * Normal non-merge commit? If we don't want to make the
985 * history dense, we consider it always to be a change..
987 if (!revs->dense && !commit->parents->next)
988 return;
990 for (pp = &commit->parents, nth_parent = 0, relevant_parents = 0;
991 (parent = *pp) != NULL;
992 pp = &parent->next, nth_parent++) {
993 struct commit *p = parent->item;
994 if (relevant_commit(p))
995 relevant_parents++;
997 if (nth_parent == 1) {
999 * This our second loop iteration - so we now know
1000 * we're dealing with a merge.
1002 * Do not compare with later parents when we care only about
1003 * the first parent chain, in order to avoid derailing the
1004 * traversal to follow a side branch that brought everything
1005 * in the path we are limited to by the pathspec.
1007 if (revs->first_parent_only)
1008 break;
1010 * If this will remain a potentially-simplifiable
1011 * merge, remember per-parent treesame if needed.
1012 * Initialise the array with the comparison from our
1013 * first iteration.
1015 if (revs->treesame.name &&
1016 !revs->simplify_history &&
1017 !(commit->object.flags & UNINTERESTING)) {
1018 ts = initialise_treesame(revs, commit);
1019 if (!(irrelevant_change || relevant_change))
1020 ts->treesame[0] = 1;
1023 if (repo_parse_commit(revs->repo, p) < 0)
1024 die("cannot simplify commit %s (because of %s)",
1025 oid_to_hex(&commit->object.oid),
1026 oid_to_hex(&p->object.oid));
1027 switch (rev_compare_tree(revs, p, commit, nth_parent)) {
1028 case REV_TREE_SAME:
1029 if (!revs->simplify_history || !relevant_commit(p)) {
1030 /* Even if a merge with an uninteresting
1031 * side branch brought the entire change
1032 * we are interested in, we do not want
1033 * to lose the other branches of this
1034 * merge, so we just keep going.
1036 if (ts)
1037 ts->treesame[nth_parent] = 1;
1038 continue;
1040 parent->next = NULL;
1041 commit->parents = parent;
1044 * A merge commit is a "diversion" if it is not
1045 * TREESAME to its first parent but is TREESAME
1046 * to a later parent. In the simplified history,
1047 * we "divert" the history walk to the later
1048 * parent. These commits are shown when "show_pulls"
1049 * is enabled, so do not mark the object as
1050 * TREESAME here.
1052 if (!revs->show_pulls || !nth_parent)
1053 commit->object.flags |= TREESAME;
1055 return;
1057 case REV_TREE_NEW:
1058 if (revs->remove_empty_trees &&
1059 rev_same_tree_as_empty(revs, p)) {
1060 /* We are adding all the specified
1061 * paths from this parent, so the
1062 * history beyond this parent is not
1063 * interesting. Remove its parents
1064 * (they are grandparents for us).
1065 * IOW, we pretend this parent is a
1066 * "root" commit.
1068 if (repo_parse_commit(revs->repo, p) < 0)
1069 die("cannot simplify commit %s (invalid %s)",
1070 oid_to_hex(&commit->object.oid),
1071 oid_to_hex(&p->object.oid));
1072 p->parents = NULL;
1074 /* fallthrough */
1075 case REV_TREE_OLD:
1076 case REV_TREE_DIFFERENT:
1077 if (relevant_commit(p))
1078 relevant_change = 1;
1079 else
1080 irrelevant_change = 1;
1082 if (!nth_parent)
1083 commit->object.flags |= PULL_MERGE;
1085 continue;
1087 die("bad tree compare for commit %s", oid_to_hex(&commit->object.oid));
1091 * TREESAME is straightforward for single-parent commits. For merge
1092 * commits, it is most useful to define it so that "irrelevant"
1093 * parents cannot make us !TREESAME - if we have any relevant
1094 * parents, then we only consider TREESAMEness with respect to them,
1095 * allowing irrelevant merges from uninteresting branches to be
1096 * simplified away. Only if we have only irrelevant parents do we
1097 * base TREESAME on them. Note that this logic is replicated in
1098 * update_treesame, which should be kept in sync.
1100 if (relevant_parents ? !relevant_change : !irrelevant_change)
1101 commit->object.flags |= TREESAME;
1104 static int process_parents(struct rev_info *revs, struct commit *commit,
1105 struct commit_list **list, struct prio_queue *queue)
1107 struct commit_list *parent = commit->parents;
1108 unsigned pass_flags;
1110 if (commit->object.flags & ADDED)
1111 return 0;
1112 commit->object.flags |= ADDED;
1114 if (revs->include_check &&
1115 !revs->include_check(commit, revs->include_check_data))
1116 return 0;
1119 * If the commit is uninteresting, don't try to
1120 * prune parents - we want the maximal uninteresting
1121 * set.
1123 * Normally we haven't parsed the parent
1124 * yet, so we won't have a parent of a parent
1125 * here. However, it may turn out that we've
1126 * reached this commit some other way (where it
1127 * wasn't uninteresting), in which case we need
1128 * to mark its parents recursively too..
1130 if (commit->object.flags & UNINTERESTING) {
1131 while (parent) {
1132 struct commit *p = parent->item;
1133 parent = parent->next;
1134 if (p)
1135 p->object.flags |= UNINTERESTING;
1136 if (repo_parse_commit_gently(revs->repo, p, 1) < 0)
1137 continue;
1138 if (p->parents)
1139 mark_parents_uninteresting(revs, p);
1140 if (p->object.flags & SEEN)
1141 continue;
1142 p->object.flags |= (SEEN | NOT_USER_GIVEN);
1143 if (list)
1144 commit_list_insert_by_date(p, list);
1145 if (queue)
1146 prio_queue_put(queue, p);
1147 if (revs->exclude_first_parent_only)
1148 break;
1150 return 0;
1154 * Ok, the commit wasn't uninteresting. Try to
1155 * simplify the commit history and find the parent
1156 * that has no differences in the path set if one exists.
1158 try_to_simplify_commit(revs, commit);
1160 if (revs->no_walk)
1161 return 0;
1163 pass_flags = (commit->object.flags & (SYMMETRIC_LEFT | ANCESTRY_PATH));
1165 for (parent = commit->parents; parent; parent = parent->next) {
1166 struct commit *p = parent->item;
1167 int gently = revs->ignore_missing_links ||
1168 revs->exclude_promisor_objects;
1169 if (repo_parse_commit_gently(revs->repo, p, gently) < 0) {
1170 if (revs->exclude_promisor_objects &&
1171 is_promisor_object(&p->object.oid)) {
1172 if (revs->first_parent_only)
1173 break;
1174 continue;
1176 return -1;
1178 if (revs->sources) {
1179 char **slot = revision_sources_at(revs->sources, p);
1181 if (!*slot)
1182 *slot = *revision_sources_at(revs->sources, commit);
1184 p->object.flags |= pass_flags;
1185 if (!(p->object.flags & SEEN)) {
1186 p->object.flags |= (SEEN | NOT_USER_GIVEN);
1187 if (list)
1188 commit_list_insert_by_date(p, list);
1189 if (queue)
1190 prio_queue_put(queue, p);
1192 if (revs->first_parent_only)
1193 break;
1195 return 0;
1198 static void cherry_pick_list(struct commit_list *list, struct rev_info *revs)
1200 struct commit_list *p;
1201 int left_count = 0, right_count = 0;
1202 int left_first;
1203 struct patch_ids ids;
1204 unsigned cherry_flag;
1206 /* First count the commits on the left and on the right */
1207 for (p = list; p; p = p->next) {
1208 struct commit *commit = p->item;
1209 unsigned flags = commit->object.flags;
1210 if (flags & BOUNDARY)
1212 else if (flags & SYMMETRIC_LEFT)
1213 left_count++;
1214 else
1215 right_count++;
1218 if (!left_count || !right_count)
1219 return;
1221 left_first = left_count < right_count;
1222 init_patch_ids(revs->repo, &ids);
1223 ids.diffopts.pathspec = revs->diffopt.pathspec;
1225 /* Compute patch-ids for one side */
1226 for (p = list; p; p = p->next) {
1227 struct commit *commit = p->item;
1228 unsigned flags = commit->object.flags;
1230 if (flags & BOUNDARY)
1231 continue;
1233 * If we have fewer left, left_first is set and we omit
1234 * commits on the right branch in this loop. If we have
1235 * fewer right, we skip the left ones.
1237 if (left_first != !!(flags & SYMMETRIC_LEFT))
1238 continue;
1239 add_commit_patch_id(commit, &ids);
1242 /* either cherry_mark or cherry_pick are true */
1243 cherry_flag = revs->cherry_mark ? PATCHSAME : SHOWN;
1245 /* Check the other side */
1246 for (p = list; p; p = p->next) {
1247 struct commit *commit = p->item;
1248 struct patch_id *id;
1249 unsigned flags = commit->object.flags;
1251 if (flags & BOUNDARY)
1252 continue;
1254 * If we have fewer left, left_first is set and we omit
1255 * commits on the left branch in this loop.
1257 if (left_first == !!(flags & SYMMETRIC_LEFT))
1258 continue;
1261 * Have we seen the same patch id?
1263 id = patch_id_iter_first(commit, &ids);
1264 if (!id)
1265 continue;
1267 commit->object.flags |= cherry_flag;
1268 do {
1269 id->commit->object.flags |= cherry_flag;
1270 } while ((id = patch_id_iter_next(id, &ids)));
1273 free_patch_ids(&ids);
1276 /* How many extra uninteresting commits we want to see.. */
1277 #define SLOP 5
1279 static int still_interesting(struct commit_list *src, timestamp_t date, int slop,
1280 struct commit **interesting_cache)
1283 * No source list at all? We're definitely done..
1285 if (!src)
1286 return 0;
1289 * Does the destination list contain entries with a date
1290 * before the source list? Definitely _not_ done.
1292 if (date <= src->item->date)
1293 return SLOP;
1296 * Does the source list still have interesting commits in
1297 * it? Definitely not done..
1299 if (!everybody_uninteresting(src, interesting_cache))
1300 return SLOP;
1302 /* Ok, we're closing in.. */
1303 return slop-1;
1307 * "rev-list --ancestry-path=C_0 [--ancestry-path=C_1 ...] A..B"
1308 * computes commits that are ancestors of B but not ancestors of A but
1309 * further limits the result to those that have any of C in their
1310 * ancestry path (i.e. are either ancestors of any of C, descendants
1311 * of any of C, or are any of C). If --ancestry-path is specified with
1312 * no commit, we use all bottom commits for C.
1314 * Before this function is called, ancestors of C will have already
1315 * been marked with ANCESTRY_PATH previously.
1317 * This takes the list of bottom commits and the result of "A..B"
1318 * without --ancestry-path, and limits the latter further to the ones
1319 * that have any of C in their ancestry path. Since the ancestors of C
1320 * have already been marked (a prerequisite of this function), we just
1321 * need to mark the descendants, then exclude any commit that does not
1322 * have any of these marks.
1324 static void limit_to_ancestry(struct commit_list *bottoms, struct commit_list *list)
1326 struct commit_list *p;
1327 struct commit_list *rlist = NULL;
1328 int made_progress;
1331 * Reverse the list so that it will be likely that we would
1332 * process parents before children.
1334 for (p = list; p; p = p->next)
1335 commit_list_insert(p->item, &rlist);
1337 for (p = bottoms; p; p = p->next)
1338 p->item->object.flags |= TMP_MARK;
1341 * Mark the ones that can reach bottom commits in "list",
1342 * in a bottom-up fashion.
1344 do {
1345 made_progress = 0;
1346 for (p = rlist; p; p = p->next) {
1347 struct commit *c = p->item;
1348 struct commit_list *parents;
1349 if (c->object.flags & (TMP_MARK | UNINTERESTING))
1350 continue;
1351 for (parents = c->parents;
1352 parents;
1353 parents = parents->next) {
1354 if (!(parents->item->object.flags & TMP_MARK))
1355 continue;
1356 c->object.flags |= TMP_MARK;
1357 made_progress = 1;
1358 break;
1361 } while (made_progress);
1364 * NEEDSWORK: decide if we want to remove parents that are
1365 * not marked with TMP_MARK from commit->parents for commits
1366 * in the resulting list. We may not want to do that, though.
1370 * The ones that are not marked with either TMP_MARK or
1371 * ANCESTRY_PATH are uninteresting
1373 for (p = list; p; p = p->next) {
1374 struct commit *c = p->item;
1375 if (c->object.flags & (TMP_MARK | ANCESTRY_PATH))
1376 continue;
1377 c->object.flags |= UNINTERESTING;
1380 /* We are done with TMP_MARK and ANCESTRY_PATH */
1381 for (p = list; p; p = p->next)
1382 p->item->object.flags &= ~(TMP_MARK | ANCESTRY_PATH);
1383 for (p = bottoms; p; p = p->next)
1384 p->item->object.flags &= ~(TMP_MARK | ANCESTRY_PATH);
1385 free_commit_list(rlist);
1389 * Before walking the history, add the set of "negative" refs the
1390 * caller has asked to exclude to the bottom list.
1392 * This is used to compute "rev-list --ancestry-path A..B", as we need
1393 * to filter the result of "A..B" further to the ones that can actually
1394 * reach A.
1396 static void collect_bottom_commits(struct commit_list *list,
1397 struct commit_list **bottom)
1399 struct commit_list *elem;
1400 for (elem = list; elem; elem = elem->next)
1401 if (elem->item->object.flags & BOTTOM)
1402 commit_list_insert(elem->item, bottom);
1405 /* Assumes either left_only or right_only is set */
1406 static void limit_left_right(struct commit_list *list, struct rev_info *revs)
1408 struct commit_list *p;
1410 for (p = list; p; p = p->next) {
1411 struct commit *commit = p->item;
1413 if (revs->right_only) {
1414 if (commit->object.flags & SYMMETRIC_LEFT)
1415 commit->object.flags |= SHOWN;
1416 } else /* revs->left_only is set */
1417 if (!(commit->object.flags & SYMMETRIC_LEFT))
1418 commit->object.flags |= SHOWN;
1422 static int limit_list(struct rev_info *revs)
1424 int slop = SLOP;
1425 timestamp_t date = TIME_MAX;
1426 struct commit_list *original_list = revs->commits;
1427 struct commit_list *newlist = NULL;
1428 struct commit_list **p = &newlist;
1429 struct commit *interesting_cache = NULL;
1431 if (revs->ancestry_path_implicit_bottoms) {
1432 collect_bottom_commits(original_list,
1433 &revs->ancestry_path_bottoms);
1434 if (!revs->ancestry_path_bottoms)
1435 die("--ancestry-path given but there are no bottom commits");
1438 while (original_list) {
1439 struct commit *commit = pop_commit(&original_list);
1440 struct object *obj = &commit->object;
1441 show_early_output_fn_t show;
1443 if (commit == interesting_cache)
1444 interesting_cache = NULL;
1446 if (revs->max_age != -1 && (commit->date < revs->max_age))
1447 obj->flags |= UNINTERESTING;
1448 if (process_parents(revs, commit, &original_list, NULL) < 0)
1449 return -1;
1450 if (obj->flags & UNINTERESTING) {
1451 mark_parents_uninteresting(revs, commit);
1452 slop = still_interesting(original_list, date, slop, &interesting_cache);
1453 if (slop)
1454 continue;
1455 break;
1457 if (revs->min_age != -1 && (commit->date > revs->min_age) &&
1458 !revs->line_level_traverse)
1459 continue;
1460 if (revs->max_age_as_filter != -1 &&
1461 (commit->date < revs->max_age_as_filter) && !revs->line_level_traverse)
1462 continue;
1463 date = commit->date;
1464 p = &commit_list_insert(commit, p)->next;
1466 show = show_early_output;
1467 if (!show)
1468 continue;
1470 show(revs, newlist);
1471 show_early_output = NULL;
1473 if (revs->cherry_pick || revs->cherry_mark)
1474 cherry_pick_list(newlist, revs);
1476 if (revs->left_only || revs->right_only)
1477 limit_left_right(newlist, revs);
1479 if (revs->ancestry_path)
1480 limit_to_ancestry(revs->ancestry_path_bottoms, newlist);
1483 * Check if any commits have become TREESAME by some of their parents
1484 * becoming UNINTERESTING.
1486 if (limiting_can_increase_treesame(revs)) {
1487 struct commit_list *list = NULL;
1488 for (list = newlist; list; list = list->next) {
1489 struct commit *c = list->item;
1490 if (c->object.flags & (UNINTERESTING | TREESAME))
1491 continue;
1492 update_treesame(revs, c);
1496 free_commit_list(original_list);
1497 revs->commits = newlist;
1498 return 0;
1502 * Add an entry to refs->cmdline with the specified information.
1503 * *name is copied.
1505 static void add_rev_cmdline(struct rev_info *revs,
1506 struct object *item,
1507 const char *name,
1508 int whence,
1509 unsigned flags)
1511 struct rev_cmdline_info *info = &revs->cmdline;
1512 unsigned int nr = info->nr;
1514 ALLOC_GROW(info->rev, nr + 1, info->alloc);
1515 info->rev[nr].item = item;
1516 info->rev[nr].name = xstrdup(name);
1517 info->rev[nr].whence = whence;
1518 info->rev[nr].flags = flags;
1519 info->nr++;
1522 static void add_rev_cmdline_list(struct rev_info *revs,
1523 struct commit_list *commit_list,
1524 int whence,
1525 unsigned flags)
1527 while (commit_list) {
1528 struct object *object = &commit_list->item->object;
1529 add_rev_cmdline(revs, object, oid_to_hex(&object->oid),
1530 whence, flags);
1531 commit_list = commit_list->next;
1535 struct all_refs_cb {
1536 int all_flags;
1537 int warned_bad_reflog;
1538 struct rev_info *all_revs;
1539 const char *name_for_errormsg;
1540 struct worktree *wt;
1543 int ref_excluded(struct string_list *ref_excludes, const char *path)
1545 struct string_list_item *item;
1547 if (!ref_excludes)
1548 return 0;
1549 for_each_string_list_item(item, ref_excludes) {
1550 if (!wildmatch(item->string, path, 0))
1551 return 1;
1553 return 0;
1556 static int handle_one_ref(const char *path, const struct object_id *oid,
1557 int flag, void *cb_data)
1559 struct all_refs_cb *cb = cb_data;
1560 struct object *object;
1562 if (ref_excluded(cb->all_revs->ref_excludes, path))
1563 return 0;
1565 object = get_reference(cb->all_revs, path, oid, cb->all_flags);
1566 add_rev_cmdline(cb->all_revs, object, path, REV_CMD_REF, cb->all_flags);
1567 add_pending_object(cb->all_revs, object, path);
1568 return 0;
1571 static void init_all_refs_cb(struct all_refs_cb *cb, struct rev_info *revs,
1572 unsigned flags)
1574 cb->all_revs = revs;
1575 cb->all_flags = flags;
1576 revs->rev_input_given = 1;
1577 cb->wt = NULL;
1580 void clear_ref_exclusion(struct string_list **ref_excludes_p)
1582 if (*ref_excludes_p) {
1583 string_list_clear(*ref_excludes_p, 0);
1584 free(*ref_excludes_p);
1586 *ref_excludes_p = NULL;
1589 void add_ref_exclusion(struct string_list **ref_excludes_p, const char *exclude)
1591 if (!*ref_excludes_p) {
1592 CALLOC_ARRAY(*ref_excludes_p, 1);
1593 (*ref_excludes_p)->strdup_strings = 1;
1595 string_list_append(*ref_excludes_p, exclude);
1598 static void handle_refs(struct ref_store *refs,
1599 struct rev_info *revs, unsigned flags,
1600 int (*for_each)(struct ref_store *, each_ref_fn, void *))
1602 struct all_refs_cb cb;
1604 if (!refs) {
1605 /* this could happen with uninitialized submodules */
1606 return;
1609 init_all_refs_cb(&cb, revs, flags);
1610 for_each(refs, handle_one_ref, &cb);
1613 static void handle_one_reflog_commit(struct object_id *oid, void *cb_data)
1615 struct all_refs_cb *cb = cb_data;
1616 if (!is_null_oid(oid)) {
1617 struct object *o = parse_object(cb->all_revs->repo, oid);
1618 if (o) {
1619 o->flags |= cb->all_flags;
1620 /* ??? CMDLINEFLAGS ??? */
1621 add_pending_object(cb->all_revs, o, "");
1623 else if (!cb->warned_bad_reflog) {
1624 warning("reflog of '%s' references pruned commits",
1625 cb->name_for_errormsg);
1626 cb->warned_bad_reflog = 1;
1631 static int handle_one_reflog_ent(struct object_id *ooid, struct object_id *noid,
1632 const char *email, timestamp_t timestamp, int tz,
1633 const char *message, void *cb_data)
1635 handle_one_reflog_commit(ooid, cb_data);
1636 handle_one_reflog_commit(noid, cb_data);
1637 return 0;
1640 static int handle_one_reflog(const char *refname_in_wt,
1641 const struct object_id *oid,
1642 int flag, void *cb_data)
1644 struct all_refs_cb *cb = cb_data;
1645 struct strbuf refname = STRBUF_INIT;
1647 cb->warned_bad_reflog = 0;
1648 strbuf_worktree_ref(cb->wt, &refname, refname_in_wt);
1649 cb->name_for_errormsg = refname.buf;
1650 refs_for_each_reflog_ent(get_main_ref_store(the_repository),
1651 refname.buf,
1652 handle_one_reflog_ent, cb_data);
1653 strbuf_release(&refname);
1654 return 0;
1657 static void add_other_reflogs_to_pending(struct all_refs_cb *cb)
1659 struct worktree **worktrees, **p;
1661 worktrees = get_worktrees();
1662 for (p = worktrees; *p; p++) {
1663 struct worktree *wt = *p;
1665 if (wt->is_current)
1666 continue;
1668 cb->wt = wt;
1669 refs_for_each_reflog(get_worktree_ref_store(wt),
1670 handle_one_reflog,
1671 cb);
1673 free_worktrees(worktrees);
1676 void add_reflogs_to_pending(struct rev_info *revs, unsigned flags)
1678 struct all_refs_cb cb;
1680 cb.all_revs = revs;
1681 cb.all_flags = flags;
1682 cb.wt = NULL;
1683 for_each_reflog(handle_one_reflog, &cb);
1685 if (!revs->single_worktree)
1686 add_other_reflogs_to_pending(&cb);
1689 static void add_cache_tree(struct cache_tree *it, struct rev_info *revs,
1690 struct strbuf *path, unsigned int flags)
1692 size_t baselen = path->len;
1693 int i;
1695 if (it->entry_count >= 0) {
1696 struct tree *tree = lookup_tree(revs->repo, &it->oid);
1697 tree->object.flags |= flags;
1698 add_pending_object_with_path(revs, &tree->object, "",
1699 040000, path->buf);
1702 for (i = 0; i < it->subtree_nr; i++) {
1703 struct cache_tree_sub *sub = it->down[i];
1704 strbuf_addf(path, "%s%s", baselen ? "/" : "", sub->name);
1705 add_cache_tree(sub->cache_tree, revs, path, flags);
1706 strbuf_setlen(path, baselen);
1711 static void add_resolve_undo_to_pending(struct index_state *istate, struct rev_info *revs)
1713 struct string_list_item *item;
1714 struct string_list *resolve_undo = istate->resolve_undo;
1716 if (!resolve_undo)
1717 return;
1719 for_each_string_list_item(item, resolve_undo) {
1720 const char *path = item->string;
1721 struct resolve_undo_info *ru = item->util;
1722 int i;
1724 if (!ru)
1725 continue;
1726 for (i = 0; i < 3; i++) {
1727 struct blob *blob;
1729 if (!ru->mode[i] || !S_ISREG(ru->mode[i]))
1730 continue;
1732 blob = lookup_blob(revs->repo, &ru->oid[i]);
1733 if (!blob) {
1734 warning(_("resolve-undo records `%s` which is missing"),
1735 oid_to_hex(&ru->oid[i]));
1736 continue;
1738 add_pending_object_with_path(revs, &blob->object, "",
1739 ru->mode[i], path);
1744 static void do_add_index_objects_to_pending(struct rev_info *revs,
1745 struct index_state *istate,
1746 unsigned int flags)
1748 int i;
1750 /* TODO: audit for interaction with sparse-index. */
1751 ensure_full_index(istate);
1752 for (i = 0; i < istate->cache_nr; i++) {
1753 struct cache_entry *ce = istate->cache[i];
1754 struct blob *blob;
1756 if (S_ISGITLINK(ce->ce_mode))
1757 continue;
1759 blob = lookup_blob(revs->repo, &ce->oid);
1760 if (!blob)
1761 die("unable to add index blob to traversal");
1762 blob->object.flags |= flags;
1763 add_pending_object_with_path(revs, &blob->object, "",
1764 ce->ce_mode, ce->name);
1767 if (istate->cache_tree) {
1768 struct strbuf path = STRBUF_INIT;
1769 add_cache_tree(istate->cache_tree, revs, &path, flags);
1770 strbuf_release(&path);
1773 add_resolve_undo_to_pending(istate, revs);
1776 void add_index_objects_to_pending(struct rev_info *revs, unsigned int flags)
1778 struct worktree **worktrees, **p;
1780 repo_read_index(revs->repo);
1781 do_add_index_objects_to_pending(revs, revs->repo->index, flags);
1783 if (revs->single_worktree)
1784 return;
1786 worktrees = get_worktrees();
1787 for (p = worktrees; *p; p++) {
1788 struct worktree *wt = *p;
1789 struct index_state istate = { NULL };
1791 if (wt->is_current)
1792 continue; /* current index already taken care of */
1794 if (read_index_from(&istate,
1795 worktree_git_path(wt, "index"),
1796 get_worktree_git_dir(wt)) > 0)
1797 do_add_index_objects_to_pending(revs, &istate, flags);
1798 discard_index(&istate);
1800 free_worktrees(worktrees);
1803 struct add_alternate_refs_data {
1804 struct rev_info *revs;
1805 unsigned int flags;
1808 static void add_one_alternate_ref(const struct object_id *oid,
1809 void *vdata)
1811 const char *name = ".alternate";
1812 struct add_alternate_refs_data *data = vdata;
1813 struct object *obj;
1815 obj = get_reference(data->revs, name, oid, data->flags);
1816 add_rev_cmdline(data->revs, obj, name, REV_CMD_REV, data->flags);
1817 add_pending_object(data->revs, obj, name);
1820 static void add_alternate_refs_to_pending(struct rev_info *revs,
1821 unsigned int flags)
1823 struct add_alternate_refs_data data;
1824 data.revs = revs;
1825 data.flags = flags;
1826 for_each_alternate_ref(add_one_alternate_ref, &data);
1829 static int add_parents_only(struct rev_info *revs, const char *arg_, int flags,
1830 int exclude_parent)
1832 struct object_id oid;
1833 struct object *it;
1834 struct commit *commit;
1835 struct commit_list *parents;
1836 int parent_number;
1837 const char *arg = arg_;
1839 if (*arg == '^') {
1840 flags ^= UNINTERESTING | BOTTOM;
1841 arg++;
1843 if (get_oid_committish(arg, &oid))
1844 return 0;
1845 while (1) {
1846 it = get_reference(revs, arg, &oid, 0);
1847 if (!it && revs->ignore_missing)
1848 return 0;
1849 if (it->type != OBJ_TAG)
1850 break;
1851 if (!((struct tag*)it)->tagged)
1852 return 0;
1853 oidcpy(&oid, &((struct tag*)it)->tagged->oid);
1855 if (it->type != OBJ_COMMIT)
1856 return 0;
1857 commit = (struct commit *)it;
1858 if (exclude_parent &&
1859 exclude_parent > commit_list_count(commit->parents))
1860 return 0;
1861 for (parents = commit->parents, parent_number = 1;
1862 parents;
1863 parents = parents->next, parent_number++) {
1864 if (exclude_parent && parent_number != exclude_parent)
1865 continue;
1867 it = &parents->item->object;
1868 it->flags |= flags;
1869 add_rev_cmdline(revs, it, arg_, REV_CMD_PARENTS_ONLY, flags);
1870 add_pending_object(revs, it, arg);
1872 return 1;
1875 void repo_init_revisions(struct repository *r,
1876 struct rev_info *revs,
1877 const char *prefix)
1879 memset(revs, 0, sizeof(*revs));
1881 revs->repo = r;
1882 revs->abbrev = DEFAULT_ABBREV;
1883 revs->simplify_history = 1;
1884 revs->pruning.repo = r;
1885 revs->pruning.flags.recursive = 1;
1886 revs->pruning.flags.quick = 1;
1887 revs->pruning.add_remove = file_add_remove;
1888 revs->pruning.change = file_change;
1889 revs->pruning.change_fn_data = revs;
1890 revs->sort_order = REV_SORT_IN_GRAPH_ORDER;
1891 revs->dense = 1;
1892 revs->prefix = prefix;
1893 revs->max_age = -1;
1894 revs->max_age_as_filter = -1;
1895 revs->min_age = -1;
1896 revs->skip_count = -1;
1897 revs->max_count = -1;
1898 revs->max_parents = -1;
1899 revs->expand_tabs_in_log = -1;
1901 revs->commit_format = CMIT_FMT_DEFAULT;
1902 revs->expand_tabs_in_log_default = 8;
1904 grep_init(&revs->grep_filter, revs->repo);
1905 revs->grep_filter.status_only = 1;
1907 repo_diff_setup(revs->repo, &revs->diffopt);
1908 if (prefix && !revs->diffopt.prefix) {
1909 revs->diffopt.prefix = prefix;
1910 revs->diffopt.prefix_length = strlen(prefix);
1913 init_display_notes(&revs->notes_opt);
1916 static void add_pending_commit_list(struct rev_info *revs,
1917 struct commit_list *commit_list,
1918 unsigned int flags)
1920 while (commit_list) {
1921 struct object *object = &commit_list->item->object;
1922 object->flags |= flags;
1923 add_pending_object(revs, object, oid_to_hex(&object->oid));
1924 commit_list = commit_list->next;
1928 static void prepare_show_merge(struct rev_info *revs)
1930 struct commit_list *bases;
1931 struct commit *head, *other;
1932 struct object_id oid;
1933 const char **prune = NULL;
1934 int i, prune_num = 1; /* counting terminating NULL */
1935 struct index_state *istate = revs->repo->index;
1937 if (get_oid("HEAD", &oid))
1938 die("--merge without HEAD?");
1939 head = lookup_commit_or_die(&oid, "HEAD");
1940 if (get_oid("MERGE_HEAD", &oid))
1941 die("--merge without MERGE_HEAD?");
1942 other = lookup_commit_or_die(&oid, "MERGE_HEAD");
1943 add_pending_object(revs, &head->object, "HEAD");
1944 add_pending_object(revs, &other->object, "MERGE_HEAD");
1945 bases = get_merge_bases(head, other);
1946 add_rev_cmdline_list(revs, bases, REV_CMD_MERGE_BASE, UNINTERESTING | BOTTOM);
1947 add_pending_commit_list(revs, bases, UNINTERESTING | BOTTOM);
1948 free_commit_list(bases);
1949 head->object.flags |= SYMMETRIC_LEFT;
1951 if (!istate->cache_nr)
1952 repo_read_index(revs->repo);
1953 for (i = 0; i < istate->cache_nr; i++) {
1954 const struct cache_entry *ce = istate->cache[i];
1955 if (!ce_stage(ce))
1956 continue;
1957 if (ce_path_match(istate, ce, &revs->prune_data, NULL)) {
1958 prune_num++;
1959 REALLOC_ARRAY(prune, prune_num);
1960 prune[prune_num-2] = ce->name;
1961 prune[prune_num-1] = NULL;
1963 while ((i+1 < istate->cache_nr) &&
1964 ce_same_name(ce, istate->cache[i+1]))
1965 i++;
1967 clear_pathspec(&revs->prune_data);
1968 parse_pathspec(&revs->prune_data, PATHSPEC_ALL_MAGIC & ~PATHSPEC_LITERAL,
1969 PATHSPEC_PREFER_FULL | PATHSPEC_LITERAL_PATH, "", prune);
1970 revs->limited = 1;
1973 static int dotdot_missing(const char *arg, char *dotdot,
1974 struct rev_info *revs, int symmetric)
1976 if (revs->ignore_missing)
1977 return 0;
1978 /* de-munge so we report the full argument */
1979 *dotdot = '.';
1980 die(symmetric
1981 ? "Invalid symmetric difference expression %s"
1982 : "Invalid revision range %s", arg);
1985 static int handle_dotdot_1(const char *arg, char *dotdot,
1986 struct rev_info *revs, int flags,
1987 int cant_be_filename,
1988 struct object_context *a_oc,
1989 struct object_context *b_oc)
1991 const char *a_name, *b_name;
1992 struct object_id a_oid, b_oid;
1993 struct object *a_obj, *b_obj;
1994 unsigned int a_flags, b_flags;
1995 int symmetric = 0;
1996 unsigned int flags_exclude = flags ^ (UNINTERESTING | BOTTOM);
1997 unsigned int oc_flags = GET_OID_COMMITTISH | GET_OID_RECORD_PATH;
1999 a_name = arg;
2000 if (!*a_name)
2001 a_name = "HEAD";
2003 b_name = dotdot + 2;
2004 if (*b_name == '.') {
2005 symmetric = 1;
2006 b_name++;
2008 if (!*b_name)
2009 b_name = "HEAD";
2011 if (get_oid_with_context(revs->repo, a_name, oc_flags, &a_oid, a_oc) ||
2012 get_oid_with_context(revs->repo, b_name, oc_flags, &b_oid, b_oc))
2013 return -1;
2015 if (!cant_be_filename) {
2016 *dotdot = '.';
2017 verify_non_filename(revs->prefix, arg);
2018 *dotdot = '\0';
2021 a_obj = parse_object(revs->repo, &a_oid);
2022 b_obj = parse_object(revs->repo, &b_oid);
2023 if (!a_obj || !b_obj)
2024 return dotdot_missing(arg, dotdot, revs, symmetric);
2026 if (!symmetric) {
2027 /* just A..B */
2028 b_flags = flags;
2029 a_flags = flags_exclude;
2030 } else {
2031 /* A...B -- find merge bases between the two */
2032 struct commit *a, *b;
2033 struct commit_list *exclude;
2035 a = lookup_commit_reference(revs->repo, &a_obj->oid);
2036 b = lookup_commit_reference(revs->repo, &b_obj->oid);
2037 if (!a || !b)
2038 return dotdot_missing(arg, dotdot, revs, symmetric);
2040 exclude = get_merge_bases(a, b);
2041 add_rev_cmdline_list(revs, exclude, REV_CMD_MERGE_BASE,
2042 flags_exclude);
2043 add_pending_commit_list(revs, exclude, flags_exclude);
2044 free_commit_list(exclude);
2046 b_flags = flags;
2047 a_flags = flags | SYMMETRIC_LEFT;
2050 a_obj->flags |= a_flags;
2051 b_obj->flags |= b_flags;
2052 add_rev_cmdline(revs, a_obj, a_name, REV_CMD_LEFT, a_flags);
2053 add_rev_cmdline(revs, b_obj, b_name, REV_CMD_RIGHT, b_flags);
2054 add_pending_object_with_path(revs, a_obj, a_name, a_oc->mode, a_oc->path);
2055 add_pending_object_with_path(revs, b_obj, b_name, b_oc->mode, b_oc->path);
2056 return 0;
2059 static int handle_dotdot(const char *arg,
2060 struct rev_info *revs, int flags,
2061 int cant_be_filename)
2063 struct object_context a_oc, b_oc;
2064 char *dotdot = strstr(arg, "..");
2065 int ret;
2067 if (!dotdot)
2068 return -1;
2070 memset(&a_oc, 0, sizeof(a_oc));
2071 memset(&b_oc, 0, sizeof(b_oc));
2073 *dotdot = '\0';
2074 ret = handle_dotdot_1(arg, dotdot, revs, flags, cant_be_filename,
2075 &a_oc, &b_oc);
2076 *dotdot = '.';
2078 free(a_oc.path);
2079 free(b_oc.path);
2081 return ret;
2084 static int handle_revision_arg_1(const char *arg_, struct rev_info *revs, int flags, unsigned revarg_opt)
2086 struct object_context oc;
2087 char *mark;
2088 struct object *object;
2089 struct object_id oid;
2090 int local_flags;
2091 const char *arg = arg_;
2092 int cant_be_filename = revarg_opt & REVARG_CANNOT_BE_FILENAME;
2093 unsigned get_sha1_flags = GET_OID_RECORD_PATH;
2095 flags = flags & UNINTERESTING ? flags | BOTTOM : flags & ~BOTTOM;
2097 if (!cant_be_filename && !strcmp(arg, "..")) {
2099 * Just ".."? That is not a range but the
2100 * pathspec for the parent directory.
2102 return -1;
2105 if (!handle_dotdot(arg, revs, flags, revarg_opt))
2106 return 0;
2108 mark = strstr(arg, "^@");
2109 if (mark && !mark[2]) {
2110 *mark = 0;
2111 if (add_parents_only(revs, arg, flags, 0))
2112 return 0;
2113 *mark = '^';
2115 mark = strstr(arg, "^!");
2116 if (mark && !mark[2]) {
2117 *mark = 0;
2118 if (!add_parents_only(revs, arg, flags ^ (UNINTERESTING | BOTTOM), 0))
2119 *mark = '^';
2121 mark = strstr(arg, "^-");
2122 if (mark) {
2123 int exclude_parent = 1;
2125 if (mark[2]) {
2126 char *end;
2127 exclude_parent = strtoul(mark + 2, &end, 10);
2128 if (*end != '\0' || !exclude_parent)
2129 return -1;
2132 *mark = 0;
2133 if (!add_parents_only(revs, arg, flags ^ (UNINTERESTING | BOTTOM), exclude_parent))
2134 *mark = '^';
2137 local_flags = 0;
2138 if (*arg == '^') {
2139 local_flags = UNINTERESTING | BOTTOM;
2140 arg++;
2143 if (revarg_opt & REVARG_COMMITTISH)
2144 get_sha1_flags |= GET_OID_COMMITTISH;
2146 if (get_oid_with_context(revs->repo, arg, get_sha1_flags, &oid, &oc))
2147 return revs->ignore_missing ? 0 : -1;
2148 if (!cant_be_filename)
2149 verify_non_filename(revs->prefix, arg);
2150 object = get_reference(revs, arg, &oid, flags ^ local_flags);
2151 if (!object)
2152 return revs->ignore_missing ? 0 : -1;
2153 add_rev_cmdline(revs, object, arg_, REV_CMD_REV, flags ^ local_flags);
2154 add_pending_object_with_path(revs, object, arg, oc.mode, oc.path);
2155 free(oc.path);
2156 return 0;
2159 int handle_revision_arg(const char *arg, struct rev_info *revs, int flags, unsigned revarg_opt)
2161 int ret = handle_revision_arg_1(arg, revs, flags, revarg_opt);
2162 if (!ret)
2163 revs->rev_input_given = 1;
2164 return ret;
2167 static void read_pathspec_from_stdin(struct strbuf *sb,
2168 struct strvec *prune)
2170 while (strbuf_getline(sb, stdin) != EOF)
2171 strvec_push(prune, sb->buf);
2174 static void read_revisions_from_stdin(struct rev_info *revs,
2175 struct strvec *prune)
2177 struct strbuf sb;
2178 int seen_dashdash = 0;
2179 int save_warning;
2181 save_warning = warn_on_object_refname_ambiguity;
2182 warn_on_object_refname_ambiguity = 0;
2184 strbuf_init(&sb, 1000);
2185 while (strbuf_getline(&sb, stdin) != EOF) {
2186 int len = sb.len;
2187 if (!len)
2188 break;
2189 if (sb.buf[0] == '-') {
2190 if (len == 2 && sb.buf[1] == '-') {
2191 seen_dashdash = 1;
2192 break;
2194 die("options not supported in --stdin mode");
2196 if (handle_revision_arg(sb.buf, revs, 0,
2197 REVARG_CANNOT_BE_FILENAME))
2198 die("bad revision '%s'", sb.buf);
2200 if (seen_dashdash)
2201 read_pathspec_from_stdin(&sb, prune);
2203 strbuf_release(&sb);
2204 warn_on_object_refname_ambiguity = save_warning;
2207 static void add_grep(struct rev_info *revs, const char *ptn, enum grep_pat_token what)
2209 append_grep_pattern(&revs->grep_filter, ptn, "command line", 0, what);
2212 static void add_header_grep(struct rev_info *revs, enum grep_header_field field, const char *pattern)
2214 append_header_grep_pattern(&revs->grep_filter, field, pattern);
2217 static void add_message_grep(struct rev_info *revs, const char *pattern)
2219 add_grep(revs, pattern, GREP_PATTERN_BODY);
2222 static int handle_revision_opt(struct rev_info *revs, int argc, const char **argv,
2223 int *unkc, const char **unkv,
2224 const struct setup_revision_opt* opt)
2226 const char *arg = argv[0];
2227 const char *optarg = NULL;
2228 int argcount;
2229 const unsigned hexsz = the_hash_algo->hexsz;
2231 /* pseudo revision arguments */
2232 if (!strcmp(arg, "--all") || !strcmp(arg, "--branches") ||
2233 !strcmp(arg, "--tags") || !strcmp(arg, "--remotes") ||
2234 !strcmp(arg, "--reflog") || !strcmp(arg, "--not") ||
2235 !strcmp(arg, "--no-walk") || !strcmp(arg, "--do-walk") ||
2236 !strcmp(arg, "--bisect") || starts_with(arg, "--glob=") ||
2237 !strcmp(arg, "--indexed-objects") ||
2238 !strcmp(arg, "--alternate-refs") ||
2239 starts_with(arg, "--exclude=") ||
2240 starts_with(arg, "--branches=") || starts_with(arg, "--tags=") ||
2241 starts_with(arg, "--remotes=") || starts_with(arg, "--no-walk="))
2243 unkv[(*unkc)++] = arg;
2244 return 1;
2247 if ((argcount = parse_long_opt("max-count", argv, &optarg))) {
2248 revs->max_count = atoi(optarg);
2249 revs->no_walk = 0;
2250 return argcount;
2251 } else if ((argcount = parse_long_opt("skip", argv, &optarg))) {
2252 revs->skip_count = atoi(optarg);
2253 return argcount;
2254 } else if ((*arg == '-') && isdigit(arg[1])) {
2255 /* accept -<digit>, like traditional "head" */
2256 if (strtol_i(arg + 1, 10, &revs->max_count) < 0 ||
2257 revs->max_count < 0)
2258 die("'%s': not a non-negative integer", arg + 1);
2259 revs->no_walk = 0;
2260 } else if (!strcmp(arg, "-n")) {
2261 if (argc <= 1)
2262 return error("-n requires an argument");
2263 revs->max_count = atoi(argv[1]);
2264 revs->no_walk = 0;
2265 return 2;
2266 } else if (skip_prefix(arg, "-n", &optarg)) {
2267 revs->max_count = atoi(optarg);
2268 revs->no_walk = 0;
2269 } else if ((argcount = parse_long_opt("max-age", argv, &optarg))) {
2270 revs->max_age = atoi(optarg);
2271 return argcount;
2272 } else if ((argcount = parse_long_opt("since", argv, &optarg))) {
2273 revs->max_age = approxidate(optarg);
2274 return argcount;
2275 } else if ((argcount = parse_long_opt("since-as-filter", argv, &optarg))) {
2276 revs->max_age_as_filter = approxidate(optarg);
2277 return argcount;
2278 } else if ((argcount = parse_long_opt("after", argv, &optarg))) {
2279 revs->max_age = approxidate(optarg);
2280 return argcount;
2281 } else if ((argcount = parse_long_opt("min-age", argv, &optarg))) {
2282 revs->min_age = atoi(optarg);
2283 return argcount;
2284 } else if ((argcount = parse_long_opt("before", argv, &optarg))) {
2285 revs->min_age = approxidate(optarg);
2286 return argcount;
2287 } else if ((argcount = parse_long_opt("until", argv, &optarg))) {
2288 revs->min_age = approxidate(optarg);
2289 return argcount;
2290 } else if (!strcmp(arg, "--first-parent")) {
2291 revs->first_parent_only = 1;
2292 } else if (!strcmp(arg, "--exclude-first-parent-only")) {
2293 revs->exclude_first_parent_only = 1;
2294 } else if (!strcmp(arg, "--ancestry-path")) {
2295 revs->ancestry_path = 1;
2296 revs->simplify_history = 0;
2297 revs->limited = 1;
2298 revs->ancestry_path_implicit_bottoms = 1;
2299 } else if (skip_prefix(arg, "--ancestry-path=", &optarg)) {
2300 struct commit *c;
2301 struct object_id oid;
2302 const char *msg = _("could not get commit for ancestry-path argument %s");
2304 revs->ancestry_path = 1;
2305 revs->simplify_history = 0;
2306 revs->limited = 1;
2308 if (repo_get_oid_committish(revs->repo, optarg, &oid))
2309 return error(msg, optarg);
2310 get_reference(revs, optarg, &oid, ANCESTRY_PATH);
2311 c = lookup_commit_reference(revs->repo, &oid);
2312 if (!c)
2313 return error(msg, optarg);
2314 commit_list_insert(c, &revs->ancestry_path_bottoms);
2315 } else if (!strcmp(arg, "-g") || !strcmp(arg, "--walk-reflogs")) {
2316 init_reflog_walk(&revs->reflog_info);
2317 } else if (!strcmp(arg, "--default")) {
2318 if (argc <= 1)
2319 return error("bad --default argument");
2320 revs->def = argv[1];
2321 return 2;
2322 } else if (!strcmp(arg, "--merge")) {
2323 revs->show_merge = 1;
2324 } else if (!strcmp(arg, "--topo-order")) {
2325 revs->sort_order = REV_SORT_IN_GRAPH_ORDER;
2326 revs->topo_order = 1;
2327 } else if (!strcmp(arg, "--simplify-merges")) {
2328 revs->simplify_merges = 1;
2329 revs->topo_order = 1;
2330 revs->rewrite_parents = 1;
2331 revs->simplify_history = 0;
2332 revs->limited = 1;
2333 } else if (!strcmp(arg, "--simplify-by-decoration")) {
2334 revs->simplify_merges = 1;
2335 revs->topo_order = 1;
2336 revs->rewrite_parents = 1;
2337 revs->simplify_history = 0;
2338 revs->simplify_by_decoration = 1;
2339 revs->limited = 1;
2340 revs->prune = 1;
2341 } else if (!strcmp(arg, "--date-order")) {
2342 revs->sort_order = REV_SORT_BY_COMMIT_DATE;
2343 revs->topo_order = 1;
2344 } else if (!strcmp(arg, "--author-date-order")) {
2345 revs->sort_order = REV_SORT_BY_AUTHOR_DATE;
2346 revs->topo_order = 1;
2347 } else if (!strcmp(arg, "--early-output")) {
2348 revs->early_output = 100;
2349 revs->topo_order = 1;
2350 } else if (skip_prefix(arg, "--early-output=", &optarg)) {
2351 if (strtoul_ui(optarg, 10, &revs->early_output) < 0)
2352 die("'%s': not a non-negative integer", optarg);
2353 revs->topo_order = 1;
2354 } else if (!strcmp(arg, "--parents")) {
2355 revs->rewrite_parents = 1;
2356 revs->print_parents = 1;
2357 } else if (!strcmp(arg, "--dense")) {
2358 revs->dense = 1;
2359 } else if (!strcmp(arg, "--sparse")) {
2360 revs->dense = 0;
2361 } else if (!strcmp(arg, "--in-commit-order")) {
2362 revs->tree_blobs_in_commit_order = 1;
2363 } else if (!strcmp(arg, "--remove-empty")) {
2364 revs->remove_empty_trees = 1;
2365 } else if (!strcmp(arg, "--merges")) {
2366 revs->min_parents = 2;
2367 } else if (!strcmp(arg, "--no-merges")) {
2368 revs->max_parents = 1;
2369 } else if (skip_prefix(arg, "--min-parents=", &optarg)) {
2370 revs->min_parents = atoi(optarg);
2371 } else if (!strcmp(arg, "--no-min-parents")) {
2372 revs->min_parents = 0;
2373 } else if (skip_prefix(arg, "--max-parents=", &optarg)) {
2374 revs->max_parents = atoi(optarg);
2375 } else if (!strcmp(arg, "--no-max-parents")) {
2376 revs->max_parents = -1;
2377 } else if (!strcmp(arg, "--boundary")) {
2378 revs->boundary = 1;
2379 } else if (!strcmp(arg, "--left-right")) {
2380 revs->left_right = 1;
2381 } else if (!strcmp(arg, "--left-only")) {
2382 if (revs->right_only)
2383 die("--left-only is incompatible with --right-only"
2384 " or --cherry");
2385 revs->left_only = 1;
2386 } else if (!strcmp(arg, "--right-only")) {
2387 if (revs->left_only)
2388 die(_("options '%s' and '%s' cannot be used together"), "--right-only", "--left-only");
2389 revs->right_only = 1;
2390 } else if (!strcmp(arg, "--cherry")) {
2391 if (revs->left_only)
2392 die(_("options '%s' and '%s' cannot be used together"), "--cherry", "--left-only");
2393 revs->cherry_mark = 1;
2394 revs->right_only = 1;
2395 revs->max_parents = 1;
2396 revs->limited = 1;
2397 } else if (!strcmp(arg, "--count")) {
2398 revs->count = 1;
2399 } else if (!strcmp(arg, "--cherry-mark")) {
2400 if (revs->cherry_pick)
2401 die(_("options '%s' and '%s' cannot be used together"), "--cherry-mark", "--cherry-pick");
2402 revs->cherry_mark = 1;
2403 revs->limited = 1; /* needs limit_list() */
2404 } else if (!strcmp(arg, "--cherry-pick")) {
2405 if (revs->cherry_mark)
2406 die(_("options '%s' and '%s' cannot be used together"), "--cherry-pick", "--cherry-mark");
2407 revs->cherry_pick = 1;
2408 revs->limited = 1;
2409 } else if (!strcmp(arg, "--objects")) {
2410 revs->tag_objects = 1;
2411 revs->tree_objects = 1;
2412 revs->blob_objects = 1;
2413 } else if (!strcmp(arg, "--objects-edge")) {
2414 revs->tag_objects = 1;
2415 revs->tree_objects = 1;
2416 revs->blob_objects = 1;
2417 revs->edge_hint = 1;
2418 } else if (!strcmp(arg, "--objects-edge-aggressive")) {
2419 revs->tag_objects = 1;
2420 revs->tree_objects = 1;
2421 revs->blob_objects = 1;
2422 revs->edge_hint = 1;
2423 revs->edge_hint_aggressive = 1;
2424 } else if (!strcmp(arg, "--verify-objects")) {
2425 revs->tag_objects = 1;
2426 revs->tree_objects = 1;
2427 revs->blob_objects = 1;
2428 revs->verify_objects = 1;
2429 } else if (!strcmp(arg, "--unpacked")) {
2430 revs->unpacked = 1;
2431 } else if (starts_with(arg, "--unpacked=")) {
2432 die(_("--unpacked=<packfile> no longer supported"));
2433 } else if (!strcmp(arg, "--no-kept-objects")) {
2434 revs->no_kept_objects = 1;
2435 revs->keep_pack_cache_flags |= IN_CORE_KEEP_PACKS;
2436 revs->keep_pack_cache_flags |= ON_DISK_KEEP_PACKS;
2437 } else if (skip_prefix(arg, "--no-kept-objects=", &optarg)) {
2438 revs->no_kept_objects = 1;
2439 if (!strcmp(optarg, "in-core"))
2440 revs->keep_pack_cache_flags |= IN_CORE_KEEP_PACKS;
2441 if (!strcmp(optarg, "on-disk"))
2442 revs->keep_pack_cache_flags |= ON_DISK_KEEP_PACKS;
2443 } else if (!strcmp(arg, "-r")) {
2444 revs->diff = 1;
2445 revs->diffopt.flags.recursive = 1;
2446 } else if (!strcmp(arg, "-t")) {
2447 revs->diff = 1;
2448 revs->diffopt.flags.recursive = 1;
2449 revs->diffopt.flags.tree_in_recursive = 1;
2450 } else if ((argcount = diff_merges_parse_opts(revs, argv))) {
2451 return argcount;
2452 } else if (!strcmp(arg, "-v")) {
2453 revs->verbose_header = 1;
2454 } else if (!strcmp(arg, "--pretty")) {
2455 revs->verbose_header = 1;
2456 revs->pretty_given = 1;
2457 get_commit_format(NULL, revs);
2458 } else if (skip_prefix(arg, "--pretty=", &optarg) ||
2459 skip_prefix(arg, "--format=", &optarg)) {
2461 * Detached form ("--pretty X" as opposed to "--pretty=X")
2462 * not allowed, since the argument is optional.
2464 revs->verbose_header = 1;
2465 revs->pretty_given = 1;
2466 get_commit_format(optarg, revs);
2467 } else if (!strcmp(arg, "--expand-tabs")) {
2468 revs->expand_tabs_in_log = 8;
2469 } else if (!strcmp(arg, "--no-expand-tabs")) {
2470 revs->expand_tabs_in_log = 0;
2471 } else if (skip_prefix(arg, "--expand-tabs=", &arg)) {
2472 int val;
2473 if (strtol_i(arg, 10, &val) < 0 || val < 0)
2474 die("'%s': not a non-negative integer", arg);
2475 revs->expand_tabs_in_log = val;
2476 } else if (!strcmp(arg, "--show-notes") || !strcmp(arg, "--notes")) {
2477 enable_default_display_notes(&revs->notes_opt, &revs->show_notes);
2478 revs->show_notes_given = 1;
2479 } else if (!strcmp(arg, "--show-signature")) {
2480 revs->show_signature = 1;
2481 } else if (!strcmp(arg, "--no-show-signature")) {
2482 revs->show_signature = 0;
2483 } else if (!strcmp(arg, "--show-linear-break")) {
2484 revs->break_bar = " ..........";
2485 revs->track_linear = 1;
2486 revs->track_first_time = 1;
2487 } else if (skip_prefix(arg, "--show-linear-break=", &optarg)) {
2488 revs->break_bar = xstrdup(optarg);
2489 revs->track_linear = 1;
2490 revs->track_first_time = 1;
2491 } else if (skip_prefix(arg, "--show-notes=", &optarg) ||
2492 skip_prefix(arg, "--notes=", &optarg)) {
2493 if (starts_with(arg, "--show-notes=") &&
2494 revs->notes_opt.use_default_notes < 0)
2495 revs->notes_opt.use_default_notes = 1;
2496 enable_ref_display_notes(&revs->notes_opt, &revs->show_notes, optarg);
2497 revs->show_notes_given = 1;
2498 } else if (!strcmp(arg, "--no-notes")) {
2499 disable_display_notes(&revs->notes_opt, &revs->show_notes);
2500 revs->show_notes_given = 1;
2501 } else if (!strcmp(arg, "--standard-notes")) {
2502 revs->show_notes_given = 1;
2503 revs->notes_opt.use_default_notes = 1;
2504 } else if (!strcmp(arg, "--no-standard-notes")) {
2505 revs->notes_opt.use_default_notes = 0;
2506 } else if (!strcmp(arg, "--oneline")) {
2507 revs->verbose_header = 1;
2508 get_commit_format("oneline", revs);
2509 revs->pretty_given = 1;
2510 revs->abbrev_commit = 1;
2511 } else if (!strcmp(arg, "--graph")) {
2512 graph_clear(revs->graph);
2513 revs->graph = graph_init(revs);
2514 } else if (!strcmp(arg, "--no-graph")) {
2515 graph_clear(revs->graph);
2516 revs->graph = NULL;
2517 } else if (!strcmp(arg, "--encode-email-headers")) {
2518 revs->encode_email_headers = 1;
2519 } else if (!strcmp(arg, "--no-encode-email-headers")) {
2520 revs->encode_email_headers = 0;
2521 } else if (!strcmp(arg, "--root")) {
2522 revs->show_root_diff = 1;
2523 } else if (!strcmp(arg, "--no-commit-id")) {
2524 revs->no_commit_id = 1;
2525 } else if (!strcmp(arg, "--always")) {
2526 revs->always_show_header = 1;
2527 } else if (!strcmp(arg, "--no-abbrev")) {
2528 revs->abbrev = 0;
2529 } else if (!strcmp(arg, "--abbrev")) {
2530 revs->abbrev = DEFAULT_ABBREV;
2531 } else if (skip_prefix(arg, "--abbrev=", &optarg)) {
2532 revs->abbrev = strtoul(optarg, NULL, 10);
2533 if (revs->abbrev < MINIMUM_ABBREV)
2534 revs->abbrev = MINIMUM_ABBREV;
2535 else if (revs->abbrev > hexsz)
2536 revs->abbrev = hexsz;
2537 } else if (!strcmp(arg, "--abbrev-commit")) {
2538 revs->abbrev_commit = 1;
2539 revs->abbrev_commit_given = 1;
2540 } else if (!strcmp(arg, "--no-abbrev-commit")) {
2541 revs->abbrev_commit = 0;
2542 } else if (!strcmp(arg, "--full-diff")) {
2543 revs->diff = 1;
2544 revs->full_diff = 1;
2545 } else if (!strcmp(arg, "--show-pulls")) {
2546 revs->show_pulls = 1;
2547 } else if (!strcmp(arg, "--full-history")) {
2548 revs->simplify_history = 0;
2549 } else if (!strcmp(arg, "--relative-date")) {
2550 revs->date_mode.type = DATE_RELATIVE;
2551 revs->date_mode_explicit = 1;
2552 } else if ((argcount = parse_long_opt("date", argv, &optarg))) {
2553 parse_date_format(optarg, &revs->date_mode);
2554 revs->date_mode_explicit = 1;
2555 return argcount;
2556 } else if (!strcmp(arg, "--log-size")) {
2557 revs->show_log_size = 1;
2560 * Grepping the commit log
2562 else if ((argcount = parse_long_opt("author", argv, &optarg))) {
2563 add_header_grep(revs, GREP_HEADER_AUTHOR, optarg);
2564 return argcount;
2565 } else if ((argcount = parse_long_opt("committer", argv, &optarg))) {
2566 add_header_grep(revs, GREP_HEADER_COMMITTER, optarg);
2567 return argcount;
2568 } else if ((argcount = parse_long_opt("grep-reflog", argv, &optarg))) {
2569 add_header_grep(revs, GREP_HEADER_REFLOG, optarg);
2570 return argcount;
2571 } else if ((argcount = parse_long_opt("grep", argv, &optarg))) {
2572 add_message_grep(revs, optarg);
2573 return argcount;
2574 } else if (!strcmp(arg, "--basic-regexp")) {
2575 revs->grep_filter.pattern_type_option = GREP_PATTERN_TYPE_BRE;
2576 } else if (!strcmp(arg, "--extended-regexp") || !strcmp(arg, "-E")) {
2577 revs->grep_filter.pattern_type_option = GREP_PATTERN_TYPE_ERE;
2578 } else if (!strcmp(arg, "--regexp-ignore-case") || !strcmp(arg, "-i")) {
2579 revs->grep_filter.ignore_case = 1;
2580 revs->diffopt.pickaxe_opts |= DIFF_PICKAXE_IGNORE_CASE;
2581 } else if (!strcmp(arg, "--fixed-strings") || !strcmp(arg, "-F")) {
2582 revs->grep_filter.pattern_type_option = GREP_PATTERN_TYPE_FIXED;
2583 } else if (!strcmp(arg, "--perl-regexp") || !strcmp(arg, "-P")) {
2584 revs->grep_filter.pattern_type_option = GREP_PATTERN_TYPE_PCRE;
2585 } else if (!strcmp(arg, "--all-match")) {
2586 revs->grep_filter.all_match = 1;
2587 } else if (!strcmp(arg, "--invert-grep")) {
2588 revs->grep_filter.no_body_match = 1;
2589 } else if ((argcount = parse_long_opt("encoding", argv, &optarg))) {
2590 if (strcmp(optarg, "none"))
2591 git_log_output_encoding = xstrdup(optarg);
2592 else
2593 git_log_output_encoding = "";
2594 return argcount;
2595 } else if (!strcmp(arg, "--reverse")) {
2596 revs->reverse ^= 1;
2597 } else if (!strcmp(arg, "--children")) {
2598 revs->children.name = "children";
2599 revs->limited = 1;
2600 } else if (!strcmp(arg, "--ignore-missing")) {
2601 revs->ignore_missing = 1;
2602 } else if (opt && opt->allow_exclude_promisor_objects &&
2603 !strcmp(arg, "--exclude-promisor-objects")) {
2604 if (fetch_if_missing)
2605 BUG("exclude_promisor_objects can only be used when fetch_if_missing is 0");
2606 revs->exclude_promisor_objects = 1;
2607 } else {
2608 int opts = diff_opt_parse(&revs->diffopt, argv, argc, revs->prefix);
2609 if (!opts)
2610 unkv[(*unkc)++] = arg;
2611 return opts;
2614 return 1;
2617 void parse_revision_opt(struct rev_info *revs, struct parse_opt_ctx_t *ctx,
2618 const struct option *options,
2619 const char * const usagestr[])
2621 int n = handle_revision_opt(revs, ctx->argc, ctx->argv,
2622 &ctx->cpidx, ctx->out, NULL);
2623 if (n <= 0) {
2624 error("unknown option `%s'", ctx->argv[0]);
2625 usage_with_options(usagestr, options);
2627 ctx->argv += n;
2628 ctx->argc -= n;
2631 void revision_opts_finish(struct rev_info *revs)
2633 if (revs->graph && revs->track_linear)
2634 die(_("options '%s' and '%s' cannot be used together"), "--show-linear-break", "--graph");
2636 if (revs->graph) {
2637 revs->topo_order = 1;
2638 revs->rewrite_parents = 1;
2642 static int for_each_bisect_ref(struct ref_store *refs, each_ref_fn fn,
2643 void *cb_data, const char *term)
2645 struct strbuf bisect_refs = STRBUF_INIT;
2646 int status;
2647 strbuf_addf(&bisect_refs, "refs/bisect/%s", term);
2648 status = refs_for_each_fullref_in(refs, bisect_refs.buf, fn, cb_data);
2649 strbuf_release(&bisect_refs);
2650 return status;
2653 static int for_each_bad_bisect_ref(struct ref_store *refs, each_ref_fn fn, void *cb_data)
2655 return for_each_bisect_ref(refs, fn, cb_data, term_bad);
2658 static int for_each_good_bisect_ref(struct ref_store *refs, each_ref_fn fn, void *cb_data)
2660 return for_each_bisect_ref(refs, fn, cb_data, term_good);
2663 static int handle_revision_pseudo_opt(struct rev_info *revs,
2664 const char **argv, int *flags)
2666 const char *arg = argv[0];
2667 const char *optarg;
2668 struct ref_store *refs;
2669 int argcount;
2671 if (revs->repo != the_repository) {
2673 * We need some something like get_submodule_worktrees()
2674 * before we can go through all worktrees of a submodule,
2675 * .e.g with adding all HEADs from --all, which is not
2676 * supported right now, so stick to single worktree.
2678 if (!revs->single_worktree)
2679 BUG("--single-worktree cannot be used together with submodule");
2681 refs = get_main_ref_store(revs->repo);
2684 * NOTE!
2686 * Commands like "git shortlog" will not accept the options below
2687 * unless parse_revision_opt queues them (as opposed to erroring
2688 * out).
2690 * When implementing your new pseudo-option, remember to
2691 * register it in the list at the top of handle_revision_opt.
2693 if (!strcmp(arg, "--all")) {
2694 handle_refs(refs, revs, *flags, refs_for_each_ref);
2695 handle_refs(refs, revs, *flags, refs_head_ref);
2696 if (!revs->single_worktree) {
2697 struct all_refs_cb cb;
2699 init_all_refs_cb(&cb, revs, *flags);
2700 other_head_refs(handle_one_ref, &cb);
2702 clear_ref_exclusion(&revs->ref_excludes);
2703 } else if (!strcmp(arg, "--branches")) {
2704 handle_refs(refs, revs, *flags, refs_for_each_branch_ref);
2705 clear_ref_exclusion(&revs->ref_excludes);
2706 } else if (!strcmp(arg, "--bisect")) {
2707 read_bisect_terms(&term_bad, &term_good);
2708 handle_refs(refs, revs, *flags, for_each_bad_bisect_ref);
2709 handle_refs(refs, revs, *flags ^ (UNINTERESTING | BOTTOM),
2710 for_each_good_bisect_ref);
2711 revs->bisect = 1;
2712 } else if (!strcmp(arg, "--tags")) {
2713 handle_refs(refs, revs, *flags, refs_for_each_tag_ref);
2714 clear_ref_exclusion(&revs->ref_excludes);
2715 } else if (!strcmp(arg, "--remotes")) {
2716 handle_refs(refs, revs, *flags, refs_for_each_remote_ref);
2717 clear_ref_exclusion(&revs->ref_excludes);
2718 } else if ((argcount = parse_long_opt("glob", argv, &optarg))) {
2719 struct all_refs_cb cb;
2720 init_all_refs_cb(&cb, revs, *flags);
2721 for_each_glob_ref(handle_one_ref, optarg, &cb);
2722 clear_ref_exclusion(&revs->ref_excludes);
2723 return argcount;
2724 } else if ((argcount = parse_long_opt("exclude", argv, &optarg))) {
2725 add_ref_exclusion(&revs->ref_excludes, optarg);
2726 return argcount;
2727 } else if (skip_prefix(arg, "--branches=", &optarg)) {
2728 struct all_refs_cb cb;
2729 init_all_refs_cb(&cb, revs, *flags);
2730 for_each_glob_ref_in(handle_one_ref, optarg, "refs/heads/", &cb);
2731 clear_ref_exclusion(&revs->ref_excludes);
2732 } else if (skip_prefix(arg, "--tags=", &optarg)) {
2733 struct all_refs_cb cb;
2734 init_all_refs_cb(&cb, revs, *flags);
2735 for_each_glob_ref_in(handle_one_ref, optarg, "refs/tags/", &cb);
2736 clear_ref_exclusion(&revs->ref_excludes);
2737 } else if (skip_prefix(arg, "--remotes=", &optarg)) {
2738 struct all_refs_cb cb;
2739 init_all_refs_cb(&cb, revs, *flags);
2740 for_each_glob_ref_in(handle_one_ref, optarg, "refs/remotes/", &cb);
2741 clear_ref_exclusion(&revs->ref_excludes);
2742 } else if (!strcmp(arg, "--reflog")) {
2743 add_reflogs_to_pending(revs, *flags);
2744 } else if (!strcmp(arg, "--indexed-objects")) {
2745 add_index_objects_to_pending(revs, *flags);
2746 } else if (!strcmp(arg, "--alternate-refs")) {
2747 add_alternate_refs_to_pending(revs, *flags);
2748 } else if (!strcmp(arg, "--not")) {
2749 *flags ^= UNINTERESTING | BOTTOM;
2750 } else if (!strcmp(arg, "--no-walk")) {
2751 revs->no_walk = 1;
2752 } else if (skip_prefix(arg, "--no-walk=", &optarg)) {
2754 * Detached form ("--no-walk X" as opposed to "--no-walk=X")
2755 * not allowed, since the argument is optional.
2757 revs->no_walk = 1;
2758 if (!strcmp(optarg, "sorted"))
2759 revs->unsorted_input = 0;
2760 else if (!strcmp(optarg, "unsorted"))
2761 revs->unsorted_input = 1;
2762 else
2763 return error("invalid argument to --no-walk");
2764 } else if (!strcmp(arg, "--do-walk")) {
2765 revs->no_walk = 0;
2766 } else if (!strcmp(arg, "--single-worktree")) {
2767 revs->single_worktree = 1;
2768 } else if (skip_prefix(arg, ("--filter="), &arg)) {
2769 parse_list_objects_filter(&revs->filter, arg);
2770 } else if (!strcmp(arg, ("--no-filter"))) {
2771 list_objects_filter_set_no_filter(&revs->filter);
2772 } else {
2773 return 0;
2776 return 1;
2779 static void NORETURN diagnose_missing_default(const char *def)
2781 int flags;
2782 const char *refname;
2784 refname = resolve_ref_unsafe(def, 0, NULL, &flags);
2785 if (!refname || !(flags & REF_ISSYMREF) || (flags & REF_ISBROKEN))
2786 die(_("your current branch appears to be broken"));
2788 skip_prefix(refname, "refs/heads/", &refname);
2789 die(_("your current branch '%s' does not have any commits yet"),
2790 refname);
2794 * Parse revision information, filling in the "rev_info" structure,
2795 * and removing the used arguments from the argument list.
2797 * Returns the number of arguments left that weren't recognized
2798 * (which are also moved to the head of the argument list)
2800 int setup_revisions(int argc, const char **argv, struct rev_info *revs, struct setup_revision_opt *opt)
2802 int i, flags, left, seen_dashdash, revarg_opt;
2803 struct strvec prune_data = STRVEC_INIT;
2804 int seen_end_of_options = 0;
2806 /* First, search for "--" */
2807 if (opt && opt->assume_dashdash) {
2808 seen_dashdash = 1;
2809 } else {
2810 seen_dashdash = 0;
2811 for (i = 1; i < argc; i++) {
2812 const char *arg = argv[i];
2813 if (strcmp(arg, "--"))
2814 continue;
2815 if (opt && opt->free_removed_argv_elements)
2816 free((char *)argv[i]);
2817 argv[i] = NULL;
2818 argc = i;
2819 if (argv[i + 1])
2820 strvec_pushv(&prune_data, argv + i + 1);
2821 seen_dashdash = 1;
2822 break;
2826 /* Second, deal with arguments and options */
2827 flags = 0;
2828 revarg_opt = opt ? opt->revarg_opt : 0;
2829 if (seen_dashdash)
2830 revarg_opt |= REVARG_CANNOT_BE_FILENAME;
2831 for (left = i = 1; i < argc; i++) {
2832 const char *arg = argv[i];
2833 if (!seen_end_of_options && *arg == '-') {
2834 int opts;
2836 opts = handle_revision_pseudo_opt(
2837 revs, argv + i,
2838 &flags);
2839 if (opts > 0) {
2840 i += opts - 1;
2841 continue;
2844 if (!strcmp(arg, "--stdin")) {
2845 if (revs->disable_stdin) {
2846 argv[left++] = arg;
2847 continue;
2849 if (revs->read_from_stdin++)
2850 die("--stdin given twice?");
2851 read_revisions_from_stdin(revs, &prune_data);
2852 continue;
2855 if (!strcmp(arg, "--end-of-options")) {
2856 seen_end_of_options = 1;
2857 continue;
2860 opts = handle_revision_opt(revs, argc - i, argv + i,
2861 &left, argv, opt);
2862 if (opts > 0) {
2863 i += opts - 1;
2864 continue;
2866 if (opts < 0)
2867 exit(128);
2868 continue;
2872 if (handle_revision_arg(arg, revs, flags, revarg_opt)) {
2873 int j;
2874 if (seen_dashdash || *arg == '^')
2875 die("bad revision '%s'", arg);
2877 /* If we didn't have a "--":
2878 * (1) all filenames must exist;
2879 * (2) all rev-args must not be interpretable
2880 * as a valid filename.
2881 * but the latter we have checked in the main loop.
2883 for (j = i; j < argc; j++)
2884 verify_filename(revs->prefix, argv[j], j == i);
2886 strvec_pushv(&prune_data, argv + i);
2887 break;
2890 revision_opts_finish(revs);
2892 if (prune_data.nr) {
2894 * If we need to introduce the magic "a lone ':' means no
2895 * pathspec whatsoever", here is the place to do so.
2897 * if (prune_data.nr == 1 && !strcmp(prune_data[0], ":")) {
2898 * prune_data.nr = 0;
2899 * prune_data.alloc = 0;
2900 * free(prune_data.path);
2901 * prune_data.path = NULL;
2902 * } else {
2903 * terminate prune_data.alloc with NULL and
2904 * call init_pathspec() to set revs->prune_data here.
2907 parse_pathspec(&revs->prune_data, 0, 0,
2908 revs->prefix, prune_data.v);
2910 strvec_clear(&prune_data);
2912 if (!revs->def)
2913 revs->def = opt ? opt->def : NULL;
2914 if (opt && opt->tweak)
2915 opt->tweak(revs, opt);
2916 if (revs->show_merge)
2917 prepare_show_merge(revs);
2918 if (revs->def && !revs->pending.nr && !revs->rev_input_given) {
2919 struct object_id oid;
2920 struct object *object;
2921 struct object_context oc;
2922 if (get_oid_with_context(revs->repo, revs->def, 0, &oid, &oc))
2923 diagnose_missing_default(revs->def);
2924 object = get_reference(revs, revs->def, &oid, 0);
2925 add_pending_object_with_mode(revs, object, revs->def, oc.mode);
2928 /* Did the user ask for any diff output? Run the diff! */
2929 if (revs->diffopt.output_format & ~DIFF_FORMAT_NO_OUTPUT)
2930 revs->diff = 1;
2932 /* Pickaxe, diff-filter and rename following need diffs */
2933 if ((revs->diffopt.pickaxe_opts & DIFF_PICKAXE_KINDS_MASK) ||
2934 revs->diffopt.filter ||
2935 revs->diffopt.flags.follow_renames)
2936 revs->diff = 1;
2938 if (revs->diffopt.objfind)
2939 revs->simplify_history = 0;
2941 if (revs->line_level_traverse) {
2942 if (want_ancestry(revs))
2943 revs->limited = 1;
2944 revs->topo_order = 1;
2947 if (revs->topo_order && !generation_numbers_enabled(the_repository))
2948 revs->limited = 1;
2950 if (revs->prune_data.nr) {
2951 copy_pathspec(&revs->pruning.pathspec, &revs->prune_data);
2952 /* Can't prune commits with rename following: the paths change.. */
2953 if (!revs->diffopt.flags.follow_renames)
2954 revs->prune = 1;
2955 if (!revs->full_diff)
2956 copy_pathspec(&revs->diffopt.pathspec,
2957 &revs->prune_data);
2960 diff_merges_setup_revs(revs);
2962 revs->diffopt.abbrev = revs->abbrev;
2964 diff_setup_done(&revs->diffopt);
2966 if (!is_encoding_utf8(get_log_output_encoding()))
2967 revs->grep_filter.ignore_locale = 1;
2968 compile_grep_patterns(&revs->grep_filter);
2970 if (revs->reverse && revs->reflog_info)
2971 die(_("options '%s' and '%s' cannot be used together"), "--reverse", "--walk-reflogs");
2972 if (revs->reflog_info && revs->limited)
2973 die("cannot combine --walk-reflogs with history-limiting options");
2974 if (revs->rewrite_parents && revs->children.name)
2975 die(_("options '%s' and '%s' cannot be used together"), "--parents", "--children");
2976 if (revs->filter.choice && !revs->blob_objects)
2977 die(_("object filtering requires --objects"));
2980 * Limitations on the graph functionality
2982 if (revs->reverse && revs->graph)
2983 die(_("options '%s' and '%s' cannot be used together"), "--reverse", "--graph");
2985 if (revs->reflog_info && revs->graph)
2986 die(_("options '%s' and '%s' cannot be used together"), "--walk-reflogs", "--graph");
2987 if (revs->no_walk && revs->graph)
2988 die(_("options '%s' and '%s' cannot be used together"), "--no-walk", "--graph");
2989 if (!revs->reflog_info && revs->grep_filter.use_reflog_filter)
2990 die(_("the option '%s' requires '%s'"), "--grep-reflog", "--walk-reflogs");
2992 if (revs->line_level_traverse &&
2993 (revs->diffopt.output_format & ~(DIFF_FORMAT_PATCH | DIFF_FORMAT_NO_OUTPUT)))
2994 die(_("-L does not yet support diff formats besides -p and -s"));
2996 if (revs->expand_tabs_in_log < 0)
2997 revs->expand_tabs_in_log = revs->expand_tabs_in_log_default;
2999 return left;
3002 static void release_revisions_cmdline(struct rev_cmdline_info *cmdline)
3004 unsigned int i;
3006 for (i = 0; i < cmdline->nr; i++)
3007 free((char *)cmdline->rev[i].name);
3008 free(cmdline->rev);
3011 static void release_revisions_mailmap(struct string_list *mailmap)
3013 if (!mailmap)
3014 return;
3015 clear_mailmap(mailmap);
3016 free(mailmap);
3019 static void release_revisions_topo_walk_info(struct topo_walk_info *info);
3021 void release_revisions(struct rev_info *revs)
3023 free_commit_list(revs->commits);
3024 free_commit_list(revs->ancestry_path_bottoms);
3025 object_array_clear(&revs->pending);
3026 object_array_clear(&revs->boundary_commits);
3027 release_revisions_cmdline(&revs->cmdline);
3028 list_objects_filter_release(&revs->filter);
3029 clear_pathspec(&revs->prune_data);
3030 date_mode_release(&revs->date_mode);
3031 release_revisions_mailmap(revs->mailmap);
3032 free_grep_patterns(&revs->grep_filter);
3033 /* TODO (need to handle "no_free"): diff_free(&revs->diffopt) */
3034 diff_free(&revs->pruning);
3035 reflog_walk_info_release(revs->reflog_info);
3036 release_revisions_topo_walk_info(revs->topo_walk_info);
3039 static void add_child(struct rev_info *revs, struct commit *parent, struct commit *child)
3041 struct commit_list *l = xcalloc(1, sizeof(*l));
3043 l->item = child;
3044 l->next = add_decoration(&revs->children, &parent->object, l);
3047 static int remove_duplicate_parents(struct rev_info *revs, struct commit *commit)
3049 struct treesame_state *ts = lookup_decoration(&revs->treesame, &commit->object);
3050 struct commit_list **pp, *p;
3051 int surviving_parents;
3053 /* Examine existing parents while marking ones we have seen... */
3054 pp = &commit->parents;
3055 surviving_parents = 0;
3056 while ((p = *pp) != NULL) {
3057 struct commit *parent = p->item;
3058 if (parent->object.flags & TMP_MARK) {
3059 *pp = p->next;
3060 if (ts)
3061 compact_treesame(revs, commit, surviving_parents);
3062 continue;
3064 parent->object.flags |= TMP_MARK;
3065 surviving_parents++;
3066 pp = &p->next;
3068 /* clear the temporary mark */
3069 for (p = commit->parents; p; p = p->next) {
3070 p->item->object.flags &= ~TMP_MARK;
3072 /* no update_treesame() - removing duplicates can't affect TREESAME */
3073 return surviving_parents;
3076 struct merge_simplify_state {
3077 struct commit *simplified;
3080 static struct merge_simplify_state *locate_simplify_state(struct rev_info *revs, struct commit *commit)
3082 struct merge_simplify_state *st;
3084 st = lookup_decoration(&revs->merge_simplification, &commit->object);
3085 if (!st) {
3086 CALLOC_ARRAY(st, 1);
3087 add_decoration(&revs->merge_simplification, &commit->object, st);
3089 return st;
3092 static int mark_redundant_parents(struct commit *commit)
3094 struct commit_list *h = reduce_heads(commit->parents);
3095 int i = 0, marked = 0;
3096 struct commit_list *po, *pn;
3098 /* Want these for sanity-checking only */
3099 int orig_cnt = commit_list_count(commit->parents);
3100 int cnt = commit_list_count(h);
3103 * Not ready to remove items yet, just mark them for now, based
3104 * on the output of reduce_heads(). reduce_heads outputs the reduced
3105 * set in its original order, so this isn't too hard.
3107 po = commit->parents;
3108 pn = h;
3109 while (po) {
3110 if (pn && po->item == pn->item) {
3111 pn = pn->next;
3112 i++;
3113 } else {
3114 po->item->object.flags |= TMP_MARK;
3115 marked++;
3117 po=po->next;
3120 if (i != cnt || cnt+marked != orig_cnt)
3121 die("mark_redundant_parents %d %d %d %d", orig_cnt, cnt, i, marked);
3123 free_commit_list(h);
3125 return marked;
3128 static int mark_treesame_root_parents(struct commit *commit)
3130 struct commit_list *p;
3131 int marked = 0;
3133 for (p = commit->parents; p; p = p->next) {
3134 struct commit *parent = p->item;
3135 if (!parent->parents && (parent->object.flags & TREESAME)) {
3136 parent->object.flags |= TMP_MARK;
3137 marked++;
3141 return marked;
3145 * Awkward naming - this means one parent we are TREESAME to.
3146 * cf mark_treesame_root_parents: root parents that are TREESAME (to an
3147 * empty tree). Better name suggestions?
3149 static int leave_one_treesame_to_parent(struct rev_info *revs, struct commit *commit)
3151 struct treesame_state *ts = lookup_decoration(&revs->treesame, &commit->object);
3152 struct commit *unmarked = NULL, *marked = NULL;
3153 struct commit_list *p;
3154 unsigned n;
3156 for (p = commit->parents, n = 0; p; p = p->next, n++) {
3157 if (ts->treesame[n]) {
3158 if (p->item->object.flags & TMP_MARK) {
3159 if (!marked)
3160 marked = p->item;
3161 } else {
3162 if (!unmarked) {
3163 unmarked = p->item;
3164 break;
3171 * If we are TREESAME to a marked-for-deletion parent, but not to any
3172 * unmarked parents, unmark the first TREESAME parent. This is the
3173 * parent that the default simplify_history==1 scan would have followed,
3174 * and it doesn't make sense to omit that path when asking for a
3175 * simplified full history. Retaining it improves the chances of
3176 * understanding odd missed merges that took an old version of a file.
3178 * Example:
3180 * I--------*X A modified the file, but mainline merge X used
3181 * \ / "-s ours", so took the version from I. X is
3182 * `-*A--' TREESAME to I and !TREESAME to A.
3184 * Default log from X would produce "I". Without this check,
3185 * --full-history --simplify-merges would produce "I-A-X", showing
3186 * the merge commit X and that it changed A, but not making clear that
3187 * it had just taken the I version. With this check, the topology above
3188 * is retained.
3190 * Note that it is possible that the simplification chooses a different
3191 * TREESAME parent from the default, in which case this test doesn't
3192 * activate, and we _do_ drop the default parent. Example:
3194 * I------X A modified the file, but it was reverted in B,
3195 * \ / meaning mainline merge X is TREESAME to both
3196 * *A-*B parents.
3198 * Default log would produce "I" by following the first parent;
3199 * --full-history --simplify-merges will produce "I-A-B". But this is a
3200 * reasonable result - it presents a logical full history leading from
3201 * I to X, and X is not an important merge.
3203 if (!unmarked && marked) {
3204 marked->object.flags &= ~TMP_MARK;
3205 return 1;
3208 return 0;
3211 static int remove_marked_parents(struct rev_info *revs, struct commit *commit)
3213 struct commit_list **pp, *p;
3214 int nth_parent, removed = 0;
3216 pp = &commit->parents;
3217 nth_parent = 0;
3218 while ((p = *pp) != NULL) {
3219 struct commit *parent = p->item;
3220 if (parent->object.flags & TMP_MARK) {
3221 parent->object.flags &= ~TMP_MARK;
3222 *pp = p->next;
3223 free(p);
3224 removed++;
3225 compact_treesame(revs, commit, nth_parent);
3226 continue;
3228 pp = &p->next;
3229 nth_parent++;
3232 /* Removing parents can only increase TREESAMEness */
3233 if (removed && !(commit->object.flags & TREESAME))
3234 update_treesame(revs, commit);
3236 return nth_parent;
3239 static struct commit_list **simplify_one(struct rev_info *revs, struct commit *commit, struct commit_list **tail)
3241 struct commit_list *p;
3242 struct commit *parent;
3243 struct merge_simplify_state *st, *pst;
3244 int cnt;
3246 st = locate_simplify_state(revs, commit);
3249 * Have we handled this one?
3251 if (st->simplified)
3252 return tail;
3255 * An UNINTERESTING commit simplifies to itself, so does a
3256 * root commit. We do not rewrite parents of such commit
3257 * anyway.
3259 if ((commit->object.flags & UNINTERESTING) || !commit->parents) {
3260 st->simplified = commit;
3261 return tail;
3265 * Do we know what commit all of our parents that matter
3266 * should be rewritten to? Otherwise we are not ready to
3267 * rewrite this one yet.
3269 for (cnt = 0, p = commit->parents; p; p = p->next) {
3270 pst = locate_simplify_state(revs, p->item);
3271 if (!pst->simplified) {
3272 tail = &commit_list_insert(p->item, tail)->next;
3273 cnt++;
3275 if (revs->first_parent_only)
3276 break;
3278 if (cnt) {
3279 tail = &commit_list_insert(commit, tail)->next;
3280 return tail;
3284 * Rewrite our list of parents. Note that this cannot
3285 * affect our TREESAME flags in any way - a commit is
3286 * always TREESAME to its simplification.
3288 for (p = commit->parents; p; p = p->next) {
3289 pst = locate_simplify_state(revs, p->item);
3290 p->item = pst->simplified;
3291 if (revs->first_parent_only)
3292 break;
3295 if (revs->first_parent_only)
3296 cnt = 1;
3297 else
3298 cnt = remove_duplicate_parents(revs, commit);
3301 * It is possible that we are a merge and one side branch
3302 * does not have any commit that touches the given paths;
3303 * in such a case, the immediate parent from that branch
3304 * will be rewritten to be the merge base.
3306 * o----X X: the commit we are looking at;
3307 * / / o: a commit that touches the paths;
3308 * ---o----'
3310 * Further, a merge of an independent branch that doesn't
3311 * touch the path will reduce to a treesame root parent:
3313 * ----o----X X: the commit we are looking at;
3314 * / o: a commit that touches the paths;
3315 * r r: a root commit not touching the paths
3317 * Detect and simplify both cases.
3319 if (1 < cnt) {
3320 int marked = mark_redundant_parents(commit);
3321 marked += mark_treesame_root_parents(commit);
3322 if (marked)
3323 marked -= leave_one_treesame_to_parent(revs, commit);
3324 if (marked)
3325 cnt = remove_marked_parents(revs, commit);
3329 * A commit simplifies to itself if it is a root, if it is
3330 * UNINTERESTING, if it touches the given paths, or if it is a
3331 * merge and its parents don't simplify to one relevant commit
3332 * (the first two cases are already handled at the beginning of
3333 * this function).
3335 * Otherwise, it simplifies to what its sole relevant parent
3336 * simplifies to.
3338 if (!cnt ||
3339 (commit->object.flags & UNINTERESTING) ||
3340 !(commit->object.flags & TREESAME) ||
3341 (parent = one_relevant_parent(revs, commit->parents)) == NULL ||
3342 (revs->show_pulls && (commit->object.flags & PULL_MERGE)))
3343 st->simplified = commit;
3344 else {
3345 pst = locate_simplify_state(revs, parent);
3346 st->simplified = pst->simplified;
3348 return tail;
3351 static void simplify_merges(struct rev_info *revs)
3353 struct commit_list *list, *next;
3354 struct commit_list *yet_to_do, **tail;
3355 struct commit *commit;
3357 if (!revs->prune)
3358 return;
3360 /* feed the list reversed */
3361 yet_to_do = NULL;
3362 for (list = revs->commits; list; list = next) {
3363 commit = list->item;
3364 next = list->next;
3366 * Do not free(list) here yet; the original list
3367 * is used later in this function.
3369 commit_list_insert(commit, &yet_to_do);
3371 while (yet_to_do) {
3372 list = yet_to_do;
3373 yet_to_do = NULL;
3374 tail = &yet_to_do;
3375 while (list) {
3376 commit = pop_commit(&list);
3377 tail = simplify_one(revs, commit, tail);
3381 /* clean up the result, removing the simplified ones */
3382 list = revs->commits;
3383 revs->commits = NULL;
3384 tail = &revs->commits;
3385 while (list) {
3386 struct merge_simplify_state *st;
3388 commit = pop_commit(&list);
3389 st = locate_simplify_state(revs, commit);
3390 if (st->simplified == commit)
3391 tail = &commit_list_insert(commit, tail)->next;
3395 static void set_children(struct rev_info *revs)
3397 struct commit_list *l;
3398 for (l = revs->commits; l; l = l->next) {
3399 struct commit *commit = l->item;
3400 struct commit_list *p;
3402 for (p = commit->parents; p; p = p->next)
3403 add_child(revs, p->item, commit);
3407 void reset_revision_walk(void)
3409 clear_object_flags(SEEN | ADDED | SHOWN | TOPO_WALK_EXPLORED | TOPO_WALK_INDEGREE);
3412 static int mark_uninteresting(const struct object_id *oid,
3413 struct packed_git *pack,
3414 uint32_t pos,
3415 void *cb)
3417 struct rev_info *revs = cb;
3418 struct object *o = lookup_unknown_object(revs->repo, oid);
3419 o->flags |= UNINTERESTING | SEEN;
3420 return 0;
3423 define_commit_slab(indegree_slab, int);
3424 define_commit_slab(author_date_slab, timestamp_t);
3426 struct topo_walk_info {
3427 timestamp_t min_generation;
3428 struct prio_queue explore_queue;
3429 struct prio_queue indegree_queue;
3430 struct prio_queue topo_queue;
3431 struct indegree_slab indegree;
3432 struct author_date_slab author_date;
3435 static int topo_walk_atexit_registered;
3436 static unsigned int count_explore_walked;
3437 static unsigned int count_indegree_walked;
3438 static unsigned int count_topo_walked;
3440 static void trace2_topo_walk_statistics_atexit(void)
3442 struct json_writer jw = JSON_WRITER_INIT;
3444 jw_object_begin(&jw, 0);
3445 jw_object_intmax(&jw, "count_explore_walked", count_explore_walked);
3446 jw_object_intmax(&jw, "count_indegree_walked", count_indegree_walked);
3447 jw_object_intmax(&jw, "count_topo_walked", count_topo_walked);
3448 jw_end(&jw);
3450 trace2_data_json("topo_walk", the_repository, "statistics", &jw);
3452 jw_release(&jw);
3455 static inline void test_flag_and_insert(struct prio_queue *q, struct commit *c, int flag)
3457 if (c->object.flags & flag)
3458 return;
3460 c->object.flags |= flag;
3461 prio_queue_put(q, c);
3464 static void explore_walk_step(struct rev_info *revs)
3466 struct topo_walk_info *info = revs->topo_walk_info;
3467 struct commit_list *p;
3468 struct commit *c = prio_queue_get(&info->explore_queue);
3470 if (!c)
3471 return;
3473 if (repo_parse_commit_gently(revs->repo, c, 1) < 0)
3474 return;
3476 count_explore_walked++;
3478 if (revs->sort_order == REV_SORT_BY_AUTHOR_DATE)
3479 record_author_date(&info->author_date, c);
3481 if (revs->max_age != -1 && (c->date < revs->max_age))
3482 c->object.flags |= UNINTERESTING;
3484 if (process_parents(revs, c, NULL, NULL) < 0)
3485 return;
3487 if (c->object.flags & UNINTERESTING)
3488 mark_parents_uninteresting(revs, c);
3490 for (p = c->parents; p; p = p->next)
3491 test_flag_and_insert(&info->explore_queue, p->item, TOPO_WALK_EXPLORED);
3494 static void explore_to_depth(struct rev_info *revs,
3495 timestamp_t gen_cutoff)
3497 struct topo_walk_info *info = revs->topo_walk_info;
3498 struct commit *c;
3499 while ((c = prio_queue_peek(&info->explore_queue)) &&
3500 commit_graph_generation(c) >= gen_cutoff)
3501 explore_walk_step(revs);
3504 static void indegree_walk_step(struct rev_info *revs)
3506 struct commit_list *p;
3507 struct topo_walk_info *info = revs->topo_walk_info;
3508 struct commit *c = prio_queue_get(&info->indegree_queue);
3510 if (!c)
3511 return;
3513 if (repo_parse_commit_gently(revs->repo, c, 1) < 0)
3514 return;
3516 count_indegree_walked++;
3518 explore_to_depth(revs, commit_graph_generation(c));
3520 for (p = c->parents; p; p = p->next) {
3521 struct commit *parent = p->item;
3522 int *pi = indegree_slab_at(&info->indegree, parent);
3524 if (repo_parse_commit_gently(revs->repo, parent, 1) < 0)
3525 return;
3527 if (*pi)
3528 (*pi)++;
3529 else
3530 *pi = 2;
3532 test_flag_and_insert(&info->indegree_queue, parent, TOPO_WALK_INDEGREE);
3534 if (revs->first_parent_only)
3535 return;
3539 static void compute_indegrees_to_depth(struct rev_info *revs,
3540 timestamp_t gen_cutoff)
3542 struct topo_walk_info *info = revs->topo_walk_info;
3543 struct commit *c;
3544 while ((c = prio_queue_peek(&info->indegree_queue)) &&
3545 commit_graph_generation(c) >= gen_cutoff)
3546 indegree_walk_step(revs);
3549 static void release_revisions_topo_walk_info(struct topo_walk_info *info)
3551 if (!info)
3552 return;
3553 clear_prio_queue(&info->explore_queue);
3554 clear_prio_queue(&info->indegree_queue);
3555 clear_prio_queue(&info->topo_queue);
3556 clear_indegree_slab(&info->indegree);
3557 clear_author_date_slab(&info->author_date);
3558 free(info);
3561 static void reset_topo_walk(struct rev_info *revs)
3563 release_revisions_topo_walk_info(revs->topo_walk_info);
3564 revs->topo_walk_info = NULL;
3567 static void init_topo_walk(struct rev_info *revs)
3569 struct topo_walk_info *info;
3570 struct commit_list *list;
3571 if (revs->topo_walk_info)
3572 reset_topo_walk(revs);
3574 revs->topo_walk_info = xmalloc(sizeof(struct topo_walk_info));
3575 info = revs->topo_walk_info;
3576 memset(info, 0, sizeof(struct topo_walk_info));
3578 init_indegree_slab(&info->indegree);
3579 memset(&info->explore_queue, 0, sizeof(info->explore_queue));
3580 memset(&info->indegree_queue, 0, sizeof(info->indegree_queue));
3581 memset(&info->topo_queue, 0, sizeof(info->topo_queue));
3583 switch (revs->sort_order) {
3584 default: /* REV_SORT_IN_GRAPH_ORDER */
3585 info->topo_queue.compare = NULL;
3586 break;
3587 case REV_SORT_BY_COMMIT_DATE:
3588 info->topo_queue.compare = compare_commits_by_commit_date;
3589 break;
3590 case REV_SORT_BY_AUTHOR_DATE:
3591 init_author_date_slab(&info->author_date);
3592 info->topo_queue.compare = compare_commits_by_author_date;
3593 info->topo_queue.cb_data = &info->author_date;
3594 break;
3597 info->explore_queue.compare = compare_commits_by_gen_then_commit_date;
3598 info->indegree_queue.compare = compare_commits_by_gen_then_commit_date;
3600 info->min_generation = GENERATION_NUMBER_INFINITY;
3601 for (list = revs->commits; list; list = list->next) {
3602 struct commit *c = list->item;
3603 timestamp_t generation;
3605 if (repo_parse_commit_gently(revs->repo, c, 1))
3606 continue;
3608 test_flag_and_insert(&info->explore_queue, c, TOPO_WALK_EXPLORED);
3609 test_flag_and_insert(&info->indegree_queue, c, TOPO_WALK_INDEGREE);
3611 generation = commit_graph_generation(c);
3612 if (generation < info->min_generation)
3613 info->min_generation = generation;
3615 *(indegree_slab_at(&info->indegree, c)) = 1;
3617 if (revs->sort_order == REV_SORT_BY_AUTHOR_DATE)
3618 record_author_date(&info->author_date, c);
3620 compute_indegrees_to_depth(revs, info->min_generation);
3622 for (list = revs->commits; list; list = list->next) {
3623 struct commit *c = list->item;
3625 if (*(indegree_slab_at(&info->indegree, c)) == 1)
3626 prio_queue_put(&info->topo_queue, c);
3630 * This is unfortunate; the initial tips need to be shown
3631 * in the order given from the revision traversal machinery.
3633 if (revs->sort_order == REV_SORT_IN_GRAPH_ORDER)
3634 prio_queue_reverse(&info->topo_queue);
3636 if (trace2_is_enabled() && !topo_walk_atexit_registered) {
3637 atexit(trace2_topo_walk_statistics_atexit);
3638 topo_walk_atexit_registered = 1;
3642 static struct commit *next_topo_commit(struct rev_info *revs)
3644 struct commit *c;
3645 struct topo_walk_info *info = revs->topo_walk_info;
3647 /* pop next off of topo_queue */
3648 c = prio_queue_get(&info->topo_queue);
3650 if (c)
3651 *(indegree_slab_at(&info->indegree, c)) = 0;
3653 return c;
3656 static void expand_topo_walk(struct rev_info *revs, struct commit *commit)
3658 struct commit_list *p;
3659 struct topo_walk_info *info = revs->topo_walk_info;
3660 if (process_parents(revs, commit, NULL, NULL) < 0) {
3661 if (!revs->ignore_missing_links)
3662 die("Failed to traverse parents of commit %s",
3663 oid_to_hex(&commit->object.oid));
3666 count_topo_walked++;
3668 for (p = commit->parents; p; p = p->next) {
3669 struct commit *parent = p->item;
3670 int *pi;
3671 timestamp_t generation;
3673 if (parent->object.flags & UNINTERESTING)
3674 continue;
3676 if (repo_parse_commit_gently(revs->repo, parent, 1) < 0)
3677 continue;
3679 generation = commit_graph_generation(parent);
3680 if (generation < info->min_generation) {
3681 info->min_generation = generation;
3682 compute_indegrees_to_depth(revs, info->min_generation);
3685 pi = indegree_slab_at(&info->indegree, parent);
3687 (*pi)--;
3688 if (*pi == 1)
3689 prio_queue_put(&info->topo_queue, parent);
3691 if (revs->first_parent_only)
3692 return;
3696 int prepare_revision_walk(struct rev_info *revs)
3698 int i;
3699 struct object_array old_pending;
3700 struct commit_list **next = &revs->commits;
3702 memcpy(&old_pending, &revs->pending, sizeof(old_pending));
3703 revs->pending.nr = 0;
3704 revs->pending.alloc = 0;
3705 revs->pending.objects = NULL;
3706 for (i = 0; i < old_pending.nr; i++) {
3707 struct object_array_entry *e = old_pending.objects + i;
3708 struct commit *commit = handle_commit(revs, e);
3709 if (commit) {
3710 if (!(commit->object.flags & SEEN)) {
3711 commit->object.flags |= SEEN;
3712 next = commit_list_append(commit, next);
3716 object_array_clear(&old_pending);
3718 /* Signal whether we need per-parent treesame decoration */
3719 if (revs->simplify_merges ||
3720 (revs->limited && limiting_can_increase_treesame(revs)))
3721 revs->treesame.name = "treesame";
3723 if (revs->exclude_promisor_objects) {
3724 for_each_packed_object(mark_uninteresting, revs,
3725 FOR_EACH_OBJECT_PROMISOR_ONLY);
3728 if (!revs->reflog_info)
3729 prepare_to_use_bloom_filter(revs);
3730 if (!revs->unsorted_input)
3731 commit_list_sort_by_date(&revs->commits);
3732 if (revs->no_walk)
3733 return 0;
3734 if (revs->limited) {
3735 if (limit_list(revs) < 0)
3736 return -1;
3737 if (revs->topo_order)
3738 sort_in_topological_order(&revs->commits, revs->sort_order);
3739 } else if (revs->topo_order)
3740 init_topo_walk(revs);
3741 if (revs->line_level_traverse && want_ancestry(revs))
3743 * At the moment we can only do line-level log with parent
3744 * rewriting by performing this expensive pre-filtering step.
3745 * If parent rewriting is not requested, then we rather
3746 * perform the line-level log filtering during the regular
3747 * history traversal.
3749 line_log_filter(revs);
3750 if (revs->simplify_merges)
3751 simplify_merges(revs);
3752 if (revs->children.name)
3753 set_children(revs);
3755 return 0;
3758 static enum rewrite_result rewrite_one_1(struct rev_info *revs,
3759 struct commit **pp,
3760 struct prio_queue *queue)
3762 for (;;) {
3763 struct commit *p = *pp;
3764 if (!revs->limited)
3765 if (process_parents(revs, p, NULL, queue) < 0)
3766 return rewrite_one_error;
3767 if (p->object.flags & UNINTERESTING)
3768 return rewrite_one_ok;
3769 if (!(p->object.flags & TREESAME))
3770 return rewrite_one_ok;
3771 if (!p->parents)
3772 return rewrite_one_noparents;
3773 if (!(p = one_relevant_parent(revs, p->parents)))
3774 return rewrite_one_ok;
3775 *pp = p;
3779 static void merge_queue_into_list(struct prio_queue *q, struct commit_list **list)
3781 while (q->nr) {
3782 struct commit *item = prio_queue_peek(q);
3783 struct commit_list *p = *list;
3785 if (p && p->item->date >= item->date)
3786 list = &p->next;
3787 else {
3788 p = commit_list_insert(item, list);
3789 list = &p->next; /* skip newly added item */
3790 prio_queue_get(q); /* pop item */
3795 static enum rewrite_result rewrite_one(struct rev_info *revs, struct commit **pp)
3797 struct prio_queue queue = { compare_commits_by_commit_date };
3798 enum rewrite_result ret = rewrite_one_1(revs, pp, &queue);
3799 merge_queue_into_list(&queue, &revs->commits);
3800 clear_prio_queue(&queue);
3801 return ret;
3804 int rewrite_parents(struct rev_info *revs, struct commit *commit,
3805 rewrite_parent_fn_t rewrite_parent)
3807 struct commit_list **pp = &commit->parents;
3808 while (*pp) {
3809 struct commit_list *parent = *pp;
3810 switch (rewrite_parent(revs, &parent->item)) {
3811 case rewrite_one_ok:
3812 break;
3813 case rewrite_one_noparents:
3814 *pp = parent->next;
3815 continue;
3816 case rewrite_one_error:
3817 return -1;
3819 pp = &parent->next;
3821 remove_duplicate_parents(revs, commit);
3822 return 0;
3825 static int commit_match(struct commit *commit, struct rev_info *opt)
3827 int retval;
3828 const char *encoding;
3829 const char *message;
3830 struct strbuf buf = STRBUF_INIT;
3832 if (!opt->grep_filter.pattern_list && !opt->grep_filter.header_list)
3833 return 1;
3835 /* Prepend "fake" headers as needed */
3836 if (opt->grep_filter.use_reflog_filter) {
3837 strbuf_addstr(&buf, "reflog ");
3838 get_reflog_message(&buf, opt->reflog_info);
3839 strbuf_addch(&buf, '\n');
3843 * We grep in the user's output encoding, under the assumption that it
3844 * is the encoding they are most likely to write their grep pattern
3845 * for. In addition, it means we will match the "notes" encoding below,
3846 * so we will not end up with a buffer that has two different encodings
3847 * in it.
3849 encoding = get_log_output_encoding();
3850 message = logmsg_reencode(commit, NULL, encoding);
3852 /* Copy the commit to temporary if we are using "fake" headers */
3853 if (buf.len)
3854 strbuf_addstr(&buf, message);
3856 if (opt->grep_filter.header_list && opt->mailmap) {
3857 const char *commit_headers[] = { "author ", "committer ", NULL };
3859 if (!buf.len)
3860 strbuf_addstr(&buf, message);
3862 apply_mailmap_to_header(&buf, commit_headers, opt->mailmap);
3865 /* Append "fake" message parts as needed */
3866 if (opt->show_notes) {
3867 if (!buf.len)
3868 strbuf_addstr(&buf, message);
3869 format_display_notes(&commit->object.oid, &buf, encoding, 1);
3873 * Find either in the original commit message, or in the temporary.
3874 * Note that we cast away the constness of "message" here. It is
3875 * const because it may come from the cached commit buffer. That's OK,
3876 * because we know that it is modifiable heap memory, and that while
3877 * grep_buffer may modify it for speed, it will restore any
3878 * changes before returning.
3880 if (buf.len)
3881 retval = grep_buffer(&opt->grep_filter, buf.buf, buf.len);
3882 else
3883 retval = grep_buffer(&opt->grep_filter,
3884 (char *)message, strlen(message));
3885 strbuf_release(&buf);
3886 unuse_commit_buffer(commit, message);
3887 return retval;
3890 static inline int want_ancestry(const struct rev_info *revs)
3892 return (revs->rewrite_parents || revs->children.name);
3896 * Return a timestamp to be used for --since/--until comparisons for this
3897 * commit, based on the revision options.
3899 static timestamp_t comparison_date(const struct rev_info *revs,
3900 struct commit *commit)
3902 return revs->reflog_info ?
3903 get_reflog_timestamp(revs->reflog_info) :
3904 commit->date;
3907 enum commit_action get_commit_action(struct rev_info *revs, struct commit *commit)
3909 if (commit->object.flags & SHOWN)
3910 return commit_ignore;
3911 if (revs->unpacked && has_object_pack(&commit->object.oid))
3912 return commit_ignore;
3913 if (revs->no_kept_objects) {
3914 if (has_object_kept_pack(&commit->object.oid,
3915 revs->keep_pack_cache_flags))
3916 return commit_ignore;
3918 if (commit->object.flags & UNINTERESTING)
3919 return commit_ignore;
3920 if (revs->line_level_traverse && !want_ancestry(revs)) {
3922 * In case of line-level log with parent rewriting
3923 * prepare_revision_walk() already took care of all line-level
3924 * log filtering, and there is nothing left to do here.
3926 * If parent rewriting was not requested, then this is the
3927 * place to perform the line-level log filtering. Notably,
3928 * this check, though expensive, must come before the other,
3929 * cheaper filtering conditions, because the tracked line
3930 * ranges must be adjusted even when the commit will end up
3931 * being ignored based on other conditions.
3933 if (!line_log_process_ranges_arbitrary_commit(revs, commit))
3934 return commit_ignore;
3936 if (revs->min_age != -1 &&
3937 comparison_date(revs, commit) > revs->min_age)
3938 return commit_ignore;
3939 if (revs->max_age_as_filter != -1 &&
3940 comparison_date(revs, commit) < revs->max_age_as_filter)
3941 return commit_ignore;
3942 if (revs->min_parents || (revs->max_parents >= 0)) {
3943 int n = commit_list_count(commit->parents);
3944 if ((n < revs->min_parents) ||
3945 ((revs->max_parents >= 0) && (n > revs->max_parents)))
3946 return commit_ignore;
3948 if (!commit_match(commit, revs))
3949 return commit_ignore;
3950 if (revs->prune && revs->dense) {
3951 /* Commit without changes? */
3952 if (commit->object.flags & TREESAME) {
3953 int n;
3954 struct commit_list *p;
3955 /* drop merges unless we want parenthood */
3956 if (!want_ancestry(revs))
3957 return commit_ignore;
3959 if (revs->show_pulls && (commit->object.flags & PULL_MERGE))
3960 return commit_show;
3963 * If we want ancestry, then need to keep any merges
3964 * between relevant commits to tie together topology.
3965 * For consistency with TREESAME and simplification
3966 * use "relevant" here rather than just INTERESTING,
3967 * to treat bottom commit(s) as part of the topology.
3969 for (n = 0, p = commit->parents; p; p = p->next)
3970 if (relevant_commit(p->item))
3971 if (++n >= 2)
3972 return commit_show;
3973 return commit_ignore;
3976 return commit_show;
3979 define_commit_slab(saved_parents, struct commit_list *);
3981 #define EMPTY_PARENT_LIST ((struct commit_list *)-1)
3984 * You may only call save_parents() once per commit (this is checked
3985 * for non-root commits).
3987 static void save_parents(struct rev_info *revs, struct commit *commit)
3989 struct commit_list **pp;
3991 if (!revs->saved_parents_slab) {
3992 revs->saved_parents_slab = xmalloc(sizeof(struct saved_parents));
3993 init_saved_parents(revs->saved_parents_slab);
3996 pp = saved_parents_at(revs->saved_parents_slab, commit);
3999 * When walking with reflogs, we may visit the same commit
4000 * several times: once for each appearance in the reflog.
4002 * In this case, save_parents() will be called multiple times.
4003 * We want to keep only the first set of parents. We need to
4004 * store a sentinel value for an empty (i.e., NULL) parent
4005 * list to distinguish it from a not-yet-saved list, however.
4007 if (*pp)
4008 return;
4009 if (commit->parents)
4010 *pp = copy_commit_list(commit->parents);
4011 else
4012 *pp = EMPTY_PARENT_LIST;
4015 static void free_saved_parents(struct rev_info *revs)
4017 if (revs->saved_parents_slab)
4018 clear_saved_parents(revs->saved_parents_slab);
4021 struct commit_list *get_saved_parents(struct rev_info *revs, const struct commit *commit)
4023 struct commit_list *parents;
4025 if (!revs->saved_parents_slab)
4026 return commit->parents;
4028 parents = *saved_parents_at(revs->saved_parents_slab, commit);
4029 if (parents == EMPTY_PARENT_LIST)
4030 return NULL;
4031 return parents;
4034 enum commit_action simplify_commit(struct rev_info *revs, struct commit *commit)
4036 enum commit_action action = get_commit_action(revs, commit);
4038 if (action == commit_show &&
4039 revs->prune && revs->dense && want_ancestry(revs)) {
4041 * --full-diff on simplified parents is no good: it
4042 * will show spurious changes from the commits that
4043 * were elided. So we save the parents on the side
4044 * when --full-diff is in effect.
4046 if (revs->full_diff)
4047 save_parents(revs, commit);
4048 if (rewrite_parents(revs, commit, rewrite_one) < 0)
4049 return commit_error;
4051 return action;
4054 static void track_linear(struct rev_info *revs, struct commit *commit)
4056 if (revs->track_first_time) {
4057 revs->linear = 1;
4058 revs->track_first_time = 0;
4059 } else {
4060 struct commit_list *p;
4061 for (p = revs->previous_parents; p; p = p->next)
4062 if (p->item == NULL || /* first commit */
4063 oideq(&p->item->object.oid, &commit->object.oid))
4064 break;
4065 revs->linear = p != NULL;
4067 if (revs->reverse) {
4068 if (revs->linear)
4069 commit->object.flags |= TRACK_LINEAR;
4071 free_commit_list(revs->previous_parents);
4072 revs->previous_parents = copy_commit_list(commit->parents);
4075 static struct commit *get_revision_1(struct rev_info *revs)
4077 while (1) {
4078 struct commit *commit;
4080 if (revs->reflog_info)
4081 commit = next_reflog_entry(revs->reflog_info);
4082 else if (revs->topo_walk_info)
4083 commit = next_topo_commit(revs);
4084 else
4085 commit = pop_commit(&revs->commits);
4087 if (!commit)
4088 return NULL;
4090 if (revs->reflog_info)
4091 commit->object.flags &= ~(ADDED | SEEN | SHOWN);
4094 * If we haven't done the list limiting, we need to look at
4095 * the parents here. We also need to do the date-based limiting
4096 * that we'd otherwise have done in limit_list().
4098 if (!revs->limited) {
4099 if (revs->max_age != -1 &&
4100 comparison_date(revs, commit) < revs->max_age)
4101 continue;
4103 if (revs->reflog_info)
4104 try_to_simplify_commit(revs, commit);
4105 else if (revs->topo_walk_info)
4106 expand_topo_walk(revs, commit);
4107 else if (process_parents(revs, commit, &revs->commits, NULL) < 0) {
4108 if (!revs->ignore_missing_links)
4109 die("Failed to traverse parents of commit %s",
4110 oid_to_hex(&commit->object.oid));
4114 switch (simplify_commit(revs, commit)) {
4115 case commit_ignore:
4116 continue;
4117 case commit_error:
4118 die("Failed to simplify parents of commit %s",
4119 oid_to_hex(&commit->object.oid));
4120 default:
4121 if (revs->track_linear)
4122 track_linear(revs, commit);
4123 return commit;
4129 * Return true for entries that have not yet been shown. (This is an
4130 * object_array_each_func_t.)
4132 static int entry_unshown(struct object_array_entry *entry, void *cb_data_unused)
4134 return !(entry->item->flags & SHOWN);
4138 * If array is on the verge of a realloc, garbage-collect any entries
4139 * that have already been shown to try to free up some space.
4141 static void gc_boundary(struct object_array *array)
4143 if (array->nr == array->alloc)
4144 object_array_filter(array, entry_unshown, NULL);
4147 static void create_boundary_commit_list(struct rev_info *revs)
4149 unsigned i;
4150 struct commit *c;
4151 struct object_array *array = &revs->boundary_commits;
4152 struct object_array_entry *objects = array->objects;
4155 * If revs->commits is non-NULL at this point, an error occurred in
4156 * get_revision_1(). Ignore the error and continue printing the
4157 * boundary commits anyway. (This is what the code has always
4158 * done.)
4160 free_commit_list(revs->commits);
4161 revs->commits = NULL;
4164 * Put all of the actual boundary commits from revs->boundary_commits
4165 * into revs->commits
4167 for (i = 0; i < array->nr; i++) {
4168 c = (struct commit *)(objects[i].item);
4169 if (!c)
4170 continue;
4171 if (!(c->object.flags & CHILD_SHOWN))
4172 continue;
4173 if (c->object.flags & (SHOWN | BOUNDARY))
4174 continue;
4175 c->object.flags |= BOUNDARY;
4176 commit_list_insert(c, &revs->commits);
4180 * If revs->topo_order is set, sort the boundary commits
4181 * in topological order
4183 sort_in_topological_order(&revs->commits, revs->sort_order);
4186 static struct commit *get_revision_internal(struct rev_info *revs)
4188 struct commit *c = NULL;
4189 struct commit_list *l;
4191 if (revs->boundary == 2) {
4193 * All of the normal commits have already been returned,
4194 * and we are now returning boundary commits.
4195 * create_boundary_commit_list() has populated
4196 * revs->commits with the remaining commits to return.
4198 c = pop_commit(&revs->commits);
4199 if (c)
4200 c->object.flags |= SHOWN;
4201 return c;
4205 * If our max_count counter has reached zero, then we are done. We
4206 * don't simply return NULL because we still might need to show
4207 * boundary commits. But we want to avoid calling get_revision_1, which
4208 * might do a considerable amount of work finding the next commit only
4209 * for us to throw it away.
4211 * If it is non-zero, then either we don't have a max_count at all
4212 * (-1), or it is still counting, in which case we decrement.
4214 if (revs->max_count) {
4215 c = get_revision_1(revs);
4216 if (c) {
4217 while (revs->skip_count > 0) {
4218 revs->skip_count--;
4219 c = get_revision_1(revs);
4220 if (!c)
4221 break;
4225 if (revs->max_count > 0)
4226 revs->max_count--;
4229 if (c)
4230 c->object.flags |= SHOWN;
4232 if (!revs->boundary)
4233 return c;
4235 if (!c) {
4237 * get_revision_1() runs out the commits, and
4238 * we are done computing the boundaries.
4239 * switch to boundary commits output mode.
4241 revs->boundary = 2;
4244 * Update revs->commits to contain the list of
4245 * boundary commits.
4247 create_boundary_commit_list(revs);
4249 return get_revision_internal(revs);
4253 * boundary commits are the commits that are parents of the
4254 * ones we got from get_revision_1() but they themselves are
4255 * not returned from get_revision_1(). Before returning
4256 * 'c', we need to mark its parents that they could be boundaries.
4259 for (l = c->parents; l; l = l->next) {
4260 struct object *p;
4261 p = &(l->item->object);
4262 if (p->flags & (CHILD_SHOWN | SHOWN))
4263 continue;
4264 p->flags |= CHILD_SHOWN;
4265 gc_boundary(&revs->boundary_commits);
4266 add_object_array(p, NULL, &revs->boundary_commits);
4269 return c;
4272 struct commit *get_revision(struct rev_info *revs)
4274 struct commit *c;
4275 struct commit_list *reversed;
4277 if (revs->reverse) {
4278 reversed = NULL;
4279 while ((c = get_revision_internal(revs)))
4280 commit_list_insert(c, &reversed);
4281 revs->commits = reversed;
4282 revs->reverse = 0;
4283 revs->reverse_output_stage = 1;
4286 if (revs->reverse_output_stage) {
4287 c = pop_commit(&revs->commits);
4288 if (revs->track_linear)
4289 revs->linear = !!(c && c->object.flags & TRACK_LINEAR);
4290 return c;
4293 c = get_revision_internal(revs);
4294 if (c && revs->graph)
4295 graph_update(revs->graph, c);
4296 if (!c) {
4297 free_saved_parents(revs);
4298 free_commit_list(revs->previous_parents);
4299 revs->previous_parents = NULL;
4301 return c;
4304 const char *get_revision_mark(const struct rev_info *revs, const struct commit *commit)
4306 if (commit->object.flags & BOUNDARY)
4307 return "-";
4308 else if (commit->object.flags & UNINTERESTING)
4309 return "^";
4310 else if (commit->object.flags & PATCHSAME)
4311 return "=";
4312 else if (!revs || revs->left_right) {
4313 if (commit->object.flags & SYMMETRIC_LEFT)
4314 return "<";
4315 else
4316 return ">";
4317 } else if (revs->graph)
4318 return "*";
4319 else if (revs->cherry_mark)
4320 return "+";
4321 return "";
4324 void put_revision_mark(const struct rev_info *revs, const struct commit *commit)
4326 const char *mark = get_revision_mark(revs, commit);
4327 if (!strlen(mark))
4328 return;
4329 fputs(mark, stdout);
4330 putchar(' ');