doc: use 'object database' not ODB or abbreviation
[alt-git.git] / revision.c
blob0760e78936e4f147fc3170d4a7ba75b7e6e88ab5
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));
50 for (const char *p = name; *p && *p != '\n'; p++)
51 fputc(*p, out);
52 fputc('\n', out);
55 static void mark_blob_uninteresting(struct blob *blob)
57 if (!blob)
58 return;
59 if (blob->object.flags & UNINTERESTING)
60 return;
61 blob->object.flags |= UNINTERESTING;
64 static void mark_tree_contents_uninteresting(struct repository *r,
65 struct tree *tree)
67 struct tree_desc desc;
68 struct name_entry entry;
70 if (parse_tree_gently(tree, 1) < 0)
71 return;
73 init_tree_desc(&desc, tree->buffer, tree->size);
74 while (tree_entry(&desc, &entry)) {
75 switch (object_type(entry.mode)) {
76 case OBJ_TREE:
77 mark_tree_uninteresting(r, lookup_tree(r, &entry.oid));
78 break;
79 case OBJ_BLOB:
80 mark_blob_uninteresting(lookup_blob(r, &entry.oid));
81 break;
82 default:
83 /* Subproject commit - not in this repository */
84 break;
89 * We don't care about the tree any more
90 * after it has been marked uninteresting.
92 free_tree_buffer(tree);
95 void mark_tree_uninteresting(struct repository *r, struct tree *tree)
97 struct object *obj;
99 if (!tree)
100 return;
102 obj = &tree->object;
103 if (obj->flags & UNINTERESTING)
104 return;
105 obj->flags |= UNINTERESTING;
106 mark_tree_contents_uninteresting(r, tree);
109 struct path_and_oids_entry {
110 struct hashmap_entry ent;
111 char *path;
112 struct oidset trees;
115 static int path_and_oids_cmp(const void *hashmap_cmp_fn_data UNUSED,
116 const struct hashmap_entry *eptr,
117 const struct hashmap_entry *entry_or_key,
118 const void *keydata UNUSED)
120 const struct path_and_oids_entry *e1, *e2;
122 e1 = container_of(eptr, const struct path_and_oids_entry, ent);
123 e2 = container_of(entry_or_key, const struct path_and_oids_entry, ent);
125 return strcmp(e1->path, e2->path);
128 static void paths_and_oids_clear(struct hashmap *map)
130 struct hashmap_iter iter;
131 struct path_and_oids_entry *entry;
133 hashmap_for_each_entry(map, &iter, entry, ent /* member name */) {
134 oidset_clear(&entry->trees);
135 free(entry->path);
138 hashmap_clear_and_free(map, struct path_and_oids_entry, ent);
141 static void paths_and_oids_insert(struct hashmap *map,
142 const char *path,
143 const struct object_id *oid)
145 int hash = strhash(path);
146 struct path_and_oids_entry key;
147 struct path_and_oids_entry *entry;
149 hashmap_entry_init(&key.ent, hash);
151 /* use a shallow copy for the lookup */
152 key.path = (char *)path;
153 oidset_init(&key.trees, 0);
155 entry = hashmap_get_entry(map, &key, ent, NULL);
156 if (!entry) {
157 CALLOC_ARRAY(entry, 1);
158 hashmap_entry_init(&entry->ent, hash);
159 entry->path = xstrdup(key.path);
160 oidset_init(&entry->trees, 16);
161 hashmap_put(map, &entry->ent);
164 oidset_insert(&entry->trees, oid);
167 static void add_children_by_path(struct repository *r,
168 struct tree *tree,
169 struct hashmap *map)
171 struct tree_desc desc;
172 struct name_entry entry;
174 if (!tree)
175 return;
177 if (parse_tree_gently(tree, 1) < 0)
178 return;
180 init_tree_desc(&desc, tree->buffer, tree->size);
181 while (tree_entry(&desc, &entry)) {
182 switch (object_type(entry.mode)) {
183 case OBJ_TREE:
184 paths_and_oids_insert(map, entry.path, &entry.oid);
186 if (tree->object.flags & UNINTERESTING) {
187 struct tree *child = lookup_tree(r, &entry.oid);
188 if (child)
189 child->object.flags |= UNINTERESTING;
191 break;
192 case OBJ_BLOB:
193 if (tree->object.flags & UNINTERESTING) {
194 struct blob *child = lookup_blob(r, &entry.oid);
195 if (child)
196 child->object.flags |= UNINTERESTING;
198 break;
199 default:
200 /* Subproject commit - not in this repository */
201 break;
205 free_tree_buffer(tree);
208 void mark_trees_uninteresting_sparse(struct repository *r,
209 struct oidset *trees)
211 unsigned has_interesting = 0, has_uninteresting = 0;
212 struct hashmap map = HASHMAP_INIT(path_and_oids_cmp, NULL);
213 struct hashmap_iter map_iter;
214 struct path_and_oids_entry *entry;
215 struct object_id *oid;
216 struct oidset_iter iter;
218 oidset_iter_init(trees, &iter);
219 while ((!has_interesting || !has_uninteresting) &&
220 (oid = oidset_iter_next(&iter))) {
221 struct tree *tree = lookup_tree(r, oid);
223 if (!tree)
224 continue;
226 if (tree->object.flags & UNINTERESTING)
227 has_uninteresting = 1;
228 else
229 has_interesting = 1;
232 /* Do not walk unless we have both types of trees. */
233 if (!has_uninteresting || !has_interesting)
234 return;
236 oidset_iter_init(trees, &iter);
237 while ((oid = oidset_iter_next(&iter))) {
238 struct tree *tree = lookup_tree(r, oid);
239 add_children_by_path(r, tree, &map);
242 hashmap_for_each_entry(&map, &map_iter, entry, ent /* member name */)
243 mark_trees_uninteresting_sparse(r, &entry->trees);
245 paths_and_oids_clear(&map);
248 struct commit_stack {
249 struct commit **items;
250 size_t nr, alloc;
252 #define COMMIT_STACK_INIT { 0 }
254 static void commit_stack_push(struct commit_stack *stack, struct commit *commit)
256 ALLOC_GROW(stack->items, stack->nr + 1, stack->alloc);
257 stack->items[stack->nr++] = commit;
260 static struct commit *commit_stack_pop(struct commit_stack *stack)
262 return stack->nr ? stack->items[--stack->nr] : NULL;
265 static void commit_stack_clear(struct commit_stack *stack)
267 FREE_AND_NULL(stack->items);
268 stack->nr = stack->alloc = 0;
271 static void mark_one_parent_uninteresting(struct rev_info *revs, struct commit *commit,
272 struct commit_stack *pending)
274 struct commit_list *l;
276 if (commit->object.flags & UNINTERESTING)
277 return;
278 commit->object.flags |= UNINTERESTING;
281 * Normally we haven't parsed the parent
282 * yet, so we won't have a parent of a parent
283 * here. However, it may turn out that we've
284 * reached this commit some other way (where it
285 * wasn't uninteresting), in which case we need
286 * to mark its parents recursively too..
288 for (l = commit->parents; l; l = l->next) {
289 commit_stack_push(pending, l->item);
290 if (revs && revs->exclude_first_parent_only)
291 break;
295 void mark_parents_uninteresting(struct rev_info *revs, struct commit *commit)
297 struct commit_stack pending = COMMIT_STACK_INIT;
298 struct commit_list *l;
300 for (l = commit->parents; l; l = l->next) {
301 mark_one_parent_uninteresting(revs, l->item, &pending);
302 if (revs && revs->exclude_first_parent_only)
303 break;
306 while (pending.nr > 0)
307 mark_one_parent_uninteresting(revs, commit_stack_pop(&pending),
308 &pending);
310 commit_stack_clear(&pending);
313 static void add_pending_object_with_path(struct rev_info *revs,
314 struct object *obj,
315 const char *name, unsigned mode,
316 const char *path)
318 struct interpret_branch_name_options options = { 0 };
319 if (!obj)
320 return;
321 if (revs->no_walk && (obj->flags & UNINTERESTING))
322 revs->no_walk = 0;
323 if (revs->reflog_info && obj->type == OBJ_COMMIT) {
324 struct strbuf buf = STRBUF_INIT;
325 size_t namelen = strlen(name);
326 int len = interpret_branch_name(name, namelen, &buf, &options);
328 if (0 < len && len < namelen && buf.len)
329 strbuf_addstr(&buf, name + len);
330 add_reflog_for_walk(revs->reflog_info,
331 (struct commit *)obj,
332 buf.buf[0] ? buf.buf: name);
333 strbuf_release(&buf);
334 return; /* do not add the commit itself */
336 add_object_array_with_path(obj, name, &revs->pending, mode, path);
339 static void add_pending_object_with_mode(struct rev_info *revs,
340 struct object *obj,
341 const char *name, unsigned mode)
343 add_pending_object_with_path(revs, obj, name, mode, NULL);
346 void add_pending_object(struct rev_info *revs,
347 struct object *obj, const char *name)
349 add_pending_object_with_mode(revs, obj, name, S_IFINVALID);
352 void add_head_to_pending(struct rev_info *revs)
354 struct object_id oid;
355 struct object *obj;
356 if (get_oid("HEAD", &oid))
357 return;
358 obj = parse_object(revs->repo, &oid);
359 if (!obj)
360 return;
361 add_pending_object(revs, obj, "HEAD");
364 static struct object *get_reference(struct rev_info *revs, const char *name,
365 const struct object_id *oid,
366 unsigned int flags)
368 struct object *object;
370 object = parse_object_with_flags(revs->repo, oid,
371 revs->verify_objects ? 0 :
372 PARSE_OBJECT_SKIP_HASH_CHECK);
374 if (!object) {
375 if (revs->ignore_missing)
376 return object;
377 if (revs->exclude_promisor_objects && is_promisor_object(oid))
378 return NULL;
379 die("bad object %s", name);
381 object->flags |= flags;
382 return object;
385 void add_pending_oid(struct rev_info *revs, const char *name,
386 const struct object_id *oid, unsigned int flags)
388 struct object *object = get_reference(revs, name, oid, flags);
389 add_pending_object(revs, object, name);
392 static struct commit *handle_commit(struct rev_info *revs,
393 struct object_array_entry *entry)
395 struct object *object = entry->item;
396 const char *name = entry->name;
397 const char *path = entry->path;
398 unsigned int mode = entry->mode;
399 unsigned long flags = object->flags;
402 * Tag object? Look what it points to..
404 while (object->type == OBJ_TAG) {
405 struct tag *tag = (struct tag *) object;
406 if (revs->tag_objects && !(flags & UNINTERESTING))
407 add_pending_object(revs, object, tag->tag);
408 object = parse_object(revs->repo, get_tagged_oid(tag));
409 if (!object) {
410 if (revs->ignore_missing_links || (flags & UNINTERESTING))
411 return NULL;
412 if (revs->exclude_promisor_objects &&
413 is_promisor_object(&tag->tagged->oid))
414 return NULL;
415 die("bad object %s", oid_to_hex(&tag->tagged->oid));
417 object->flags |= flags;
419 * We'll handle the tagged object by looping or dropping
420 * through to the non-tag handlers below. Do not
421 * propagate path data from the tag's pending entry.
423 path = NULL;
424 mode = 0;
428 * Commit object? Just return it, we'll do all the complex
429 * reachability crud.
431 if (object->type == OBJ_COMMIT) {
432 struct commit *commit = (struct commit *)object;
434 if (repo_parse_commit(revs->repo, commit) < 0)
435 die("unable to parse commit %s", name);
436 if (flags & UNINTERESTING) {
437 mark_parents_uninteresting(revs, commit);
439 if (!revs->topo_order || !generation_numbers_enabled(the_repository))
440 revs->limited = 1;
442 if (revs->sources) {
443 char **slot = revision_sources_at(revs->sources, commit);
445 if (!*slot)
446 *slot = xstrdup(name);
448 return commit;
452 * Tree object? Either mark it uninteresting, or add it
453 * to the list of objects to look at later..
455 if (object->type == OBJ_TREE) {
456 struct tree *tree = (struct tree *)object;
457 if (!revs->tree_objects)
458 return NULL;
459 if (flags & UNINTERESTING) {
460 mark_tree_contents_uninteresting(revs->repo, tree);
461 return NULL;
463 add_pending_object_with_path(revs, object, name, mode, path);
464 return NULL;
468 * Blob object? You know the drill by now..
470 if (object->type == OBJ_BLOB) {
471 if (!revs->blob_objects)
472 return NULL;
473 if (flags & UNINTERESTING)
474 return NULL;
475 add_pending_object_with_path(revs, object, name, mode, path);
476 return NULL;
478 die("%s is unknown object", name);
481 static int everybody_uninteresting(struct commit_list *orig,
482 struct commit **interesting_cache)
484 struct commit_list *list = orig;
486 if (*interesting_cache) {
487 struct commit *commit = *interesting_cache;
488 if (!(commit->object.flags & UNINTERESTING))
489 return 0;
492 while (list) {
493 struct commit *commit = list->item;
494 list = list->next;
495 if (commit->object.flags & UNINTERESTING)
496 continue;
498 *interesting_cache = commit;
499 return 0;
501 return 1;
505 * A definition of "relevant" commit that we can use to simplify limited graphs
506 * by eliminating side branches.
508 * A "relevant" commit is one that is !UNINTERESTING (ie we are including it
509 * in our list), or that is a specified BOTTOM commit. Then after computing
510 * a limited list, during processing we can generally ignore boundary merges
511 * coming from outside the graph, (ie from irrelevant parents), and treat
512 * those merges as if they were single-parent. TREESAME is defined to consider
513 * only relevant parents, if any. If we are TREESAME to our on-graph parents,
514 * we don't care if we were !TREESAME to non-graph parents.
516 * Treating bottom commits as relevant ensures that a limited graph's
517 * connection to the actual bottom commit is not viewed as a side branch, but
518 * treated as part of the graph. For example:
520 * ....Z...A---X---o---o---B
521 * . /
522 * W---Y
524 * When computing "A..B", the A-X connection is at least as important as
525 * Y-X, despite A being flagged UNINTERESTING.
527 * And when computing --ancestry-path "A..B", the A-X connection is more
528 * important than Y-X, despite both A and Y being flagged UNINTERESTING.
530 static inline int relevant_commit(struct commit *commit)
532 return (commit->object.flags & (UNINTERESTING | BOTTOM)) != UNINTERESTING;
536 * Return a single relevant commit from a parent list. If we are a TREESAME
537 * commit, and this selects one of our parents, then we can safely simplify to
538 * that parent.
540 static struct commit *one_relevant_parent(const struct rev_info *revs,
541 struct commit_list *orig)
543 struct commit_list *list = orig;
544 struct commit *relevant = NULL;
546 if (!orig)
547 return NULL;
550 * For 1-parent commits, or if first-parent-only, then return that
551 * first parent (even if not "relevant" by the above definition).
552 * TREESAME will have been set purely on that parent.
554 if (revs->first_parent_only || !orig->next)
555 return orig->item;
558 * For multi-parent commits, identify a sole relevant parent, if any.
559 * If we have only one relevant parent, then TREESAME will be set purely
560 * with regard to that parent, and we can simplify accordingly.
562 * If we have more than one relevant parent, or no relevant parents
563 * (and multiple irrelevant ones), then we can't select a parent here
564 * and return NULL.
566 while (list) {
567 struct commit *commit = list->item;
568 list = list->next;
569 if (relevant_commit(commit)) {
570 if (relevant)
571 return NULL;
572 relevant = commit;
575 return relevant;
579 * The goal is to get REV_TREE_NEW as the result only if the
580 * diff consists of all '+' (and no other changes), REV_TREE_OLD
581 * if the whole diff is removal of old data, and otherwise
582 * REV_TREE_DIFFERENT (of course if the trees are the same we
583 * want REV_TREE_SAME).
585 * The only time we care about the distinction is when
586 * remove_empty_trees is in effect, in which case we care only about
587 * whether the whole change is REV_TREE_NEW, or if there's another type
588 * of change. Which means we can stop the diff early in either of these
589 * cases:
591 * 1. We're not using remove_empty_trees at all.
593 * 2. We saw anything except REV_TREE_NEW.
595 #define REV_TREE_SAME 0
596 #define REV_TREE_NEW 1 /* Only new files */
597 #define REV_TREE_OLD 2 /* Only files removed */
598 #define REV_TREE_DIFFERENT 3 /* Mixed changes */
599 static int tree_difference = REV_TREE_SAME;
601 static void file_add_remove(struct diff_options *options,
602 int addremove, unsigned mode,
603 const struct object_id *oid,
604 int oid_valid,
605 const char *fullpath, unsigned dirty_submodule)
607 int diff = addremove == '+' ? REV_TREE_NEW : REV_TREE_OLD;
608 struct rev_info *revs = options->change_fn_data;
610 tree_difference |= diff;
611 if (!revs->remove_empty_trees || tree_difference != REV_TREE_NEW)
612 options->flags.has_changes = 1;
615 static void file_change(struct diff_options *options,
616 unsigned old_mode, unsigned new_mode,
617 const struct object_id *old_oid,
618 const struct object_id *new_oid,
619 int old_oid_valid, int new_oid_valid,
620 const char *fullpath,
621 unsigned old_dirty_submodule, unsigned new_dirty_submodule)
623 tree_difference = REV_TREE_DIFFERENT;
624 options->flags.has_changes = 1;
627 static int bloom_filter_atexit_registered;
628 static unsigned int count_bloom_filter_maybe;
629 static unsigned int count_bloom_filter_definitely_not;
630 static unsigned int count_bloom_filter_false_positive;
631 static unsigned int count_bloom_filter_not_present;
633 static void trace2_bloom_filter_statistics_atexit(void)
635 struct json_writer jw = JSON_WRITER_INIT;
637 jw_object_begin(&jw, 0);
638 jw_object_intmax(&jw, "filter_not_present", count_bloom_filter_not_present);
639 jw_object_intmax(&jw, "maybe", count_bloom_filter_maybe);
640 jw_object_intmax(&jw, "definitely_not", count_bloom_filter_definitely_not);
641 jw_object_intmax(&jw, "false_positive", count_bloom_filter_false_positive);
642 jw_end(&jw);
644 trace2_data_json("bloom", the_repository, "statistics", &jw);
646 jw_release(&jw);
649 static int forbid_bloom_filters(struct pathspec *spec)
651 if (spec->has_wildcard)
652 return 1;
653 if (spec->nr > 1)
654 return 1;
655 if (spec->magic & ~PATHSPEC_LITERAL)
656 return 1;
657 if (spec->nr && (spec->items[0].magic & ~PATHSPEC_LITERAL))
658 return 1;
660 return 0;
663 static void prepare_to_use_bloom_filter(struct rev_info *revs)
665 struct pathspec_item *pi;
666 char *path_alloc = NULL;
667 const char *path, *p;
668 size_t len;
669 int path_component_nr = 1;
671 if (!revs->commits)
672 return;
674 if (forbid_bloom_filters(&revs->prune_data))
675 return;
677 repo_parse_commit(revs->repo, revs->commits->item);
679 revs->bloom_filter_settings = get_bloom_filter_settings(revs->repo);
680 if (!revs->bloom_filter_settings)
681 return;
683 if (!revs->pruning.pathspec.nr)
684 return;
686 pi = &revs->pruning.pathspec.items[0];
688 /* remove single trailing slash from path, if needed */
689 if (pi->len > 0 && pi->match[pi->len - 1] == '/') {
690 path_alloc = xmemdupz(pi->match, pi->len - 1);
691 path = path_alloc;
692 } else
693 path = pi->match;
695 len = strlen(path);
696 if (!len) {
697 revs->bloom_filter_settings = NULL;
698 free(path_alloc);
699 return;
702 p = path;
703 while (*p) {
705 * At this point, the path is normalized to use Unix-style
706 * path separators. This is required due to how the
707 * changed-path Bloom filters store the paths.
709 if (*p == '/')
710 path_component_nr++;
711 p++;
714 revs->bloom_keys_nr = path_component_nr;
715 ALLOC_ARRAY(revs->bloom_keys, revs->bloom_keys_nr);
717 fill_bloom_key(path, len, &revs->bloom_keys[0],
718 revs->bloom_filter_settings);
719 path_component_nr = 1;
721 p = path + len - 1;
722 while (p > path) {
723 if (*p == '/')
724 fill_bloom_key(path, p - path,
725 &revs->bloom_keys[path_component_nr++],
726 revs->bloom_filter_settings);
727 p--;
730 if (trace2_is_enabled() && !bloom_filter_atexit_registered) {
731 atexit(trace2_bloom_filter_statistics_atexit);
732 bloom_filter_atexit_registered = 1;
735 free(path_alloc);
738 static int check_maybe_different_in_bloom_filter(struct rev_info *revs,
739 struct commit *commit)
741 struct bloom_filter *filter;
742 int result = 1, j;
744 if (!revs->repo->objects->commit_graph)
745 return -1;
747 if (commit_graph_generation(commit) == GENERATION_NUMBER_INFINITY)
748 return -1;
750 filter = get_bloom_filter(revs->repo, commit);
752 if (!filter) {
753 count_bloom_filter_not_present++;
754 return -1;
757 for (j = 0; result && j < revs->bloom_keys_nr; j++) {
758 result = bloom_filter_contains(filter,
759 &revs->bloom_keys[j],
760 revs->bloom_filter_settings);
763 if (result)
764 count_bloom_filter_maybe++;
765 else
766 count_bloom_filter_definitely_not++;
768 return result;
771 static int rev_compare_tree(struct rev_info *revs,
772 struct commit *parent, struct commit *commit, int nth_parent)
774 struct tree *t1 = get_commit_tree(parent);
775 struct tree *t2 = get_commit_tree(commit);
776 int bloom_ret = 1;
778 if (!t1)
779 return REV_TREE_NEW;
780 if (!t2)
781 return REV_TREE_OLD;
783 if (revs->simplify_by_decoration) {
785 * If we are simplifying by decoration, then the commit
786 * is worth showing if it has a tag pointing at it.
788 if (get_name_decoration(&commit->object))
789 return REV_TREE_DIFFERENT;
791 * A commit that is not pointed by a tag is uninteresting
792 * if we are not limited by path. This means that you will
793 * see the usual "commits that touch the paths" plus any
794 * tagged commit by specifying both --simplify-by-decoration
795 * and pathspec.
797 if (!revs->prune_data.nr)
798 return REV_TREE_SAME;
801 if (revs->bloom_keys_nr && !nth_parent) {
802 bloom_ret = check_maybe_different_in_bloom_filter(revs, commit);
804 if (bloom_ret == 0)
805 return REV_TREE_SAME;
808 tree_difference = REV_TREE_SAME;
809 revs->pruning.flags.has_changes = 0;
810 diff_tree_oid(&t1->object.oid, &t2->object.oid, "", &revs->pruning);
812 if (!nth_parent)
813 if (bloom_ret == 1 && tree_difference == REV_TREE_SAME)
814 count_bloom_filter_false_positive++;
816 return tree_difference;
819 static int rev_same_tree_as_empty(struct rev_info *revs, struct commit *commit)
821 struct tree *t1 = get_commit_tree(commit);
823 if (!t1)
824 return 0;
826 tree_difference = REV_TREE_SAME;
827 revs->pruning.flags.has_changes = 0;
828 diff_tree_oid(NULL, &t1->object.oid, "", &revs->pruning);
830 return tree_difference == REV_TREE_SAME;
833 struct treesame_state {
834 unsigned int nparents;
835 unsigned char treesame[FLEX_ARRAY];
838 static struct treesame_state *initialise_treesame(struct rev_info *revs, struct commit *commit)
840 unsigned n = commit_list_count(commit->parents);
841 struct treesame_state *st = xcalloc(1, st_add(sizeof(*st), n));
842 st->nparents = n;
843 add_decoration(&revs->treesame, &commit->object, st);
844 return st;
848 * Must be called immediately after removing the nth_parent from a commit's
849 * parent list, if we are maintaining the per-parent treesame[] decoration.
850 * This does not recalculate the master TREESAME flag - update_treesame()
851 * should be called to update it after a sequence of treesame[] modifications
852 * that may have affected it.
854 static int compact_treesame(struct rev_info *revs, struct commit *commit, unsigned nth_parent)
856 struct treesame_state *st;
857 int old_same;
859 if (!commit->parents) {
861 * Have just removed the only parent from a non-merge.
862 * Different handling, as we lack decoration.
864 if (nth_parent != 0)
865 die("compact_treesame %u", nth_parent);
866 old_same = !!(commit->object.flags & TREESAME);
867 if (rev_same_tree_as_empty(revs, commit))
868 commit->object.flags |= TREESAME;
869 else
870 commit->object.flags &= ~TREESAME;
871 return old_same;
874 st = lookup_decoration(&revs->treesame, &commit->object);
875 if (!st || nth_parent >= st->nparents)
876 die("compact_treesame %u", nth_parent);
878 old_same = st->treesame[nth_parent];
879 memmove(st->treesame + nth_parent,
880 st->treesame + nth_parent + 1,
881 st->nparents - nth_parent - 1);
884 * If we've just become a non-merge commit, update TREESAME
885 * immediately, and remove the no-longer-needed decoration.
886 * If still a merge, defer update until update_treesame().
888 if (--st->nparents == 1) {
889 if (commit->parents->next)
890 die("compact_treesame parents mismatch");
891 if (st->treesame[0] && revs->dense)
892 commit->object.flags |= TREESAME;
893 else
894 commit->object.flags &= ~TREESAME;
895 free(add_decoration(&revs->treesame, &commit->object, NULL));
898 return old_same;
901 static unsigned update_treesame(struct rev_info *revs, struct commit *commit)
903 if (commit->parents && commit->parents->next) {
904 unsigned n;
905 struct treesame_state *st;
906 struct commit_list *p;
907 unsigned relevant_parents;
908 unsigned relevant_change, irrelevant_change;
910 st = lookup_decoration(&revs->treesame, &commit->object);
911 if (!st)
912 die("update_treesame %s", oid_to_hex(&commit->object.oid));
913 relevant_parents = 0;
914 relevant_change = irrelevant_change = 0;
915 for (p = commit->parents, n = 0; p; n++, p = p->next) {
916 if (relevant_commit(p->item)) {
917 relevant_change |= !st->treesame[n];
918 relevant_parents++;
919 } else
920 irrelevant_change |= !st->treesame[n];
922 if (relevant_parents ? relevant_change : irrelevant_change)
923 commit->object.flags &= ~TREESAME;
924 else
925 commit->object.flags |= TREESAME;
928 return commit->object.flags & TREESAME;
931 static inline int limiting_can_increase_treesame(const struct rev_info *revs)
934 * TREESAME is irrelevant unless prune && dense;
935 * if simplify_history is set, we can't have a mixture of TREESAME and
936 * !TREESAME INTERESTING parents (and we don't have treesame[]
937 * decoration anyway);
938 * if first_parent_only is set, then the TREESAME flag is locked
939 * against the first parent (and again we lack treesame[] decoration).
941 return revs->prune && revs->dense &&
942 !revs->simplify_history &&
943 !revs->first_parent_only;
946 static void try_to_simplify_commit(struct rev_info *revs, struct commit *commit)
948 struct commit_list **pp, *parent;
949 struct treesame_state *ts = NULL;
950 int relevant_change = 0, irrelevant_change = 0;
951 int relevant_parents, nth_parent;
954 * If we don't do pruning, everything is interesting
956 if (!revs->prune)
957 return;
959 if (!get_commit_tree(commit))
960 return;
962 if (!commit->parents) {
963 if (rev_same_tree_as_empty(revs, commit))
964 commit->object.flags |= TREESAME;
965 return;
969 * Normal non-merge commit? If we don't want to make the
970 * history dense, we consider it always to be a change..
972 if (!revs->dense && !commit->parents->next)
973 return;
975 for (pp = &commit->parents, nth_parent = 0, relevant_parents = 0;
976 (parent = *pp) != NULL;
977 pp = &parent->next, nth_parent++) {
978 struct commit *p = parent->item;
979 if (relevant_commit(p))
980 relevant_parents++;
982 if (nth_parent == 1) {
984 * This our second loop iteration - so we now know
985 * we're dealing with a merge.
987 * Do not compare with later parents when we care only about
988 * the first parent chain, in order to avoid derailing the
989 * traversal to follow a side branch that brought everything
990 * in the path we are limited to by the pathspec.
992 if (revs->first_parent_only)
993 break;
995 * If this will remain a potentially-simplifiable
996 * merge, remember per-parent treesame if needed.
997 * Initialise the array with the comparison from our
998 * first iteration.
1000 if (revs->treesame.name &&
1001 !revs->simplify_history &&
1002 !(commit->object.flags & UNINTERESTING)) {
1003 ts = initialise_treesame(revs, commit);
1004 if (!(irrelevant_change || relevant_change))
1005 ts->treesame[0] = 1;
1008 if (repo_parse_commit(revs->repo, p) < 0)
1009 die("cannot simplify commit %s (because of %s)",
1010 oid_to_hex(&commit->object.oid),
1011 oid_to_hex(&p->object.oid));
1012 switch (rev_compare_tree(revs, p, commit, nth_parent)) {
1013 case REV_TREE_SAME:
1014 if (!revs->simplify_history || !relevant_commit(p)) {
1015 /* Even if a merge with an uninteresting
1016 * side branch brought the entire change
1017 * we are interested in, we do not want
1018 * to lose the other branches of this
1019 * merge, so we just keep going.
1021 if (ts)
1022 ts->treesame[nth_parent] = 1;
1023 continue;
1025 parent->next = NULL;
1026 commit->parents = parent;
1029 * A merge commit is a "diversion" if it is not
1030 * TREESAME to its first parent but is TREESAME
1031 * to a later parent. In the simplified history,
1032 * we "divert" the history walk to the later
1033 * parent. These commits are shown when "show_pulls"
1034 * is enabled, so do not mark the object as
1035 * TREESAME here.
1037 if (!revs->show_pulls || !nth_parent)
1038 commit->object.flags |= TREESAME;
1040 return;
1042 case REV_TREE_NEW:
1043 if (revs->remove_empty_trees &&
1044 rev_same_tree_as_empty(revs, p)) {
1045 /* We are adding all the specified
1046 * paths from this parent, so the
1047 * history beyond this parent is not
1048 * interesting. Remove its parents
1049 * (they are grandparents for us).
1050 * IOW, we pretend this parent is a
1051 * "root" commit.
1053 if (repo_parse_commit(revs->repo, p) < 0)
1054 die("cannot simplify commit %s (invalid %s)",
1055 oid_to_hex(&commit->object.oid),
1056 oid_to_hex(&p->object.oid));
1057 p->parents = NULL;
1059 /* fallthrough */
1060 case REV_TREE_OLD:
1061 case REV_TREE_DIFFERENT:
1062 if (relevant_commit(p))
1063 relevant_change = 1;
1064 else
1065 irrelevant_change = 1;
1067 if (!nth_parent)
1068 commit->object.flags |= PULL_MERGE;
1070 continue;
1072 die("bad tree compare for commit %s", oid_to_hex(&commit->object.oid));
1076 * TREESAME is straightforward for single-parent commits. For merge
1077 * commits, it is most useful to define it so that "irrelevant"
1078 * parents cannot make us !TREESAME - if we have any relevant
1079 * parents, then we only consider TREESAMEness with respect to them,
1080 * allowing irrelevant merges from uninteresting branches to be
1081 * simplified away. Only if we have only irrelevant parents do we
1082 * base TREESAME on them. Note that this logic is replicated in
1083 * update_treesame, which should be kept in sync.
1085 if (relevant_parents ? !relevant_change : !irrelevant_change)
1086 commit->object.flags |= TREESAME;
1089 static int process_parents(struct rev_info *revs, struct commit *commit,
1090 struct commit_list **list, struct prio_queue *queue)
1092 struct commit_list *parent = commit->parents;
1093 unsigned pass_flags;
1095 if (commit->object.flags & ADDED)
1096 return 0;
1097 commit->object.flags |= ADDED;
1099 if (revs->include_check &&
1100 !revs->include_check(commit, revs->include_check_data))
1101 return 0;
1104 * If the commit is uninteresting, don't try to
1105 * prune parents - we want the maximal uninteresting
1106 * set.
1108 * Normally we haven't parsed the parent
1109 * yet, so we won't have a parent of a parent
1110 * here. However, it may turn out that we've
1111 * reached this commit some other way (where it
1112 * wasn't uninteresting), in which case we need
1113 * to mark its parents recursively too..
1115 if (commit->object.flags & UNINTERESTING) {
1116 while (parent) {
1117 struct commit *p = parent->item;
1118 parent = parent->next;
1119 if (p)
1120 p->object.flags |= UNINTERESTING;
1121 if (repo_parse_commit_gently(revs->repo, p, 1) < 0)
1122 continue;
1123 if (p->parents)
1124 mark_parents_uninteresting(revs, p);
1125 if (p->object.flags & SEEN)
1126 continue;
1127 p->object.flags |= (SEEN | NOT_USER_GIVEN);
1128 if (list)
1129 commit_list_insert_by_date(p, list);
1130 if (queue)
1131 prio_queue_put(queue, p);
1132 if (revs->exclude_first_parent_only)
1133 break;
1135 return 0;
1139 * Ok, the commit wasn't uninteresting. Try to
1140 * simplify the commit history and find the parent
1141 * that has no differences in the path set if one exists.
1143 try_to_simplify_commit(revs, commit);
1145 if (revs->no_walk)
1146 return 0;
1148 pass_flags = (commit->object.flags & (SYMMETRIC_LEFT | ANCESTRY_PATH));
1150 for (parent = commit->parents; parent; parent = parent->next) {
1151 struct commit *p = parent->item;
1152 int gently = revs->ignore_missing_links ||
1153 revs->exclude_promisor_objects;
1154 if (repo_parse_commit_gently(revs->repo, p, gently) < 0) {
1155 if (revs->exclude_promisor_objects &&
1156 is_promisor_object(&p->object.oid)) {
1157 if (revs->first_parent_only)
1158 break;
1159 continue;
1161 return -1;
1163 if (revs->sources) {
1164 char **slot = revision_sources_at(revs->sources, p);
1166 if (!*slot)
1167 *slot = *revision_sources_at(revs->sources, commit);
1169 p->object.flags |= pass_flags;
1170 if (!(p->object.flags & SEEN)) {
1171 p->object.flags |= (SEEN | NOT_USER_GIVEN);
1172 if (list)
1173 commit_list_insert_by_date(p, list);
1174 if (queue)
1175 prio_queue_put(queue, p);
1177 if (revs->first_parent_only)
1178 break;
1180 return 0;
1183 static void cherry_pick_list(struct commit_list *list, struct rev_info *revs)
1185 struct commit_list *p;
1186 int left_count = 0, right_count = 0;
1187 int left_first;
1188 struct patch_ids ids;
1189 unsigned cherry_flag;
1191 /* First count the commits on the left and on the right */
1192 for (p = list; p; p = p->next) {
1193 struct commit *commit = p->item;
1194 unsigned flags = commit->object.flags;
1195 if (flags & BOUNDARY)
1197 else if (flags & SYMMETRIC_LEFT)
1198 left_count++;
1199 else
1200 right_count++;
1203 if (!left_count || !right_count)
1204 return;
1206 left_first = left_count < right_count;
1207 init_patch_ids(revs->repo, &ids);
1208 ids.diffopts.pathspec = revs->diffopt.pathspec;
1210 /* Compute patch-ids for one side */
1211 for (p = list; p; p = p->next) {
1212 struct commit *commit = p->item;
1213 unsigned flags = commit->object.flags;
1215 if (flags & BOUNDARY)
1216 continue;
1218 * If we have fewer left, left_first is set and we omit
1219 * commits on the right branch in this loop. If we have
1220 * fewer right, we skip the left ones.
1222 if (left_first != !!(flags & SYMMETRIC_LEFT))
1223 continue;
1224 add_commit_patch_id(commit, &ids);
1227 /* either cherry_mark or cherry_pick are true */
1228 cherry_flag = revs->cherry_mark ? PATCHSAME : SHOWN;
1230 /* Check the other side */
1231 for (p = list; p; p = p->next) {
1232 struct commit *commit = p->item;
1233 struct patch_id *id;
1234 unsigned flags = commit->object.flags;
1236 if (flags & BOUNDARY)
1237 continue;
1239 * If we have fewer left, left_first is set and we omit
1240 * commits on the left branch in this loop.
1242 if (left_first == !!(flags & SYMMETRIC_LEFT))
1243 continue;
1246 * Have we seen the same patch id?
1248 id = patch_id_iter_first(commit, &ids);
1249 if (!id)
1250 continue;
1252 commit->object.flags |= cherry_flag;
1253 do {
1254 id->commit->object.flags |= cherry_flag;
1255 } while ((id = patch_id_iter_next(id, &ids)));
1258 free_patch_ids(&ids);
1261 /* How many extra uninteresting commits we want to see.. */
1262 #define SLOP 5
1264 static int still_interesting(struct commit_list *src, timestamp_t date, int slop,
1265 struct commit **interesting_cache)
1268 * No source list at all? We're definitely done..
1270 if (!src)
1271 return 0;
1274 * Does the destination list contain entries with a date
1275 * before the source list? Definitely _not_ done.
1277 if (date <= src->item->date)
1278 return SLOP;
1281 * Does the source list still have interesting commits in
1282 * it? Definitely not done..
1284 if (!everybody_uninteresting(src, interesting_cache))
1285 return SLOP;
1287 /* Ok, we're closing in.. */
1288 return slop-1;
1292 * "rev-list --ancestry-path=C_0 [--ancestry-path=C_1 ...] A..B"
1293 * computes commits that are ancestors of B but not ancestors of A but
1294 * further limits the result to those that have any of C in their
1295 * ancestry path (i.e. are either ancestors of any of C, descendants
1296 * of any of C, or are any of C). If --ancestry-path is specified with
1297 * no commit, we use all bottom commits for C.
1299 * Before this function is called, ancestors of C will have already
1300 * been marked with ANCESTRY_PATH previously.
1302 * This takes the list of bottom commits and the result of "A..B"
1303 * without --ancestry-path, and limits the latter further to the ones
1304 * that have any of C in their ancestry path. Since the ancestors of C
1305 * have already been marked (a prerequisite of this function), we just
1306 * need to mark the descendants, then exclude any commit that does not
1307 * have any of these marks.
1309 static void limit_to_ancestry(struct commit_list *bottoms, struct commit_list *list)
1311 struct commit_list *p;
1312 struct commit_list *rlist = NULL;
1313 int made_progress;
1316 * Reverse the list so that it will be likely that we would
1317 * process parents before children.
1319 for (p = list; p; p = p->next)
1320 commit_list_insert(p->item, &rlist);
1322 for (p = bottoms; p; p = p->next)
1323 p->item->object.flags |= TMP_MARK;
1326 * Mark the ones that can reach bottom commits in "list",
1327 * in a bottom-up fashion.
1329 do {
1330 made_progress = 0;
1331 for (p = rlist; p; p = p->next) {
1332 struct commit *c = p->item;
1333 struct commit_list *parents;
1334 if (c->object.flags & (TMP_MARK | UNINTERESTING))
1335 continue;
1336 for (parents = c->parents;
1337 parents;
1338 parents = parents->next) {
1339 if (!(parents->item->object.flags & TMP_MARK))
1340 continue;
1341 c->object.flags |= TMP_MARK;
1342 made_progress = 1;
1343 break;
1346 } while (made_progress);
1349 * NEEDSWORK: decide if we want to remove parents that are
1350 * not marked with TMP_MARK from commit->parents for commits
1351 * in the resulting list. We may not want to do that, though.
1355 * The ones that are not marked with either TMP_MARK or
1356 * ANCESTRY_PATH are uninteresting
1358 for (p = list; p; p = p->next) {
1359 struct commit *c = p->item;
1360 if (c->object.flags & (TMP_MARK | ANCESTRY_PATH))
1361 continue;
1362 c->object.flags |= UNINTERESTING;
1365 /* We are done with TMP_MARK and ANCESTRY_PATH */
1366 for (p = list; p; p = p->next)
1367 p->item->object.flags &= ~(TMP_MARK | ANCESTRY_PATH);
1368 for (p = bottoms; p; p = p->next)
1369 p->item->object.flags &= ~(TMP_MARK | ANCESTRY_PATH);
1370 free_commit_list(rlist);
1374 * Before walking the history, add the set of "negative" refs the
1375 * caller has asked to exclude to the bottom list.
1377 * This is used to compute "rev-list --ancestry-path A..B", as we need
1378 * to filter the result of "A..B" further to the ones that can actually
1379 * reach A.
1381 static void collect_bottom_commits(struct commit_list *list,
1382 struct commit_list **bottom)
1384 struct commit_list *elem;
1385 for (elem = list; elem; elem = elem->next)
1386 if (elem->item->object.flags & BOTTOM)
1387 commit_list_insert(elem->item, bottom);
1390 /* Assumes either left_only or right_only is set */
1391 static void limit_left_right(struct commit_list *list, struct rev_info *revs)
1393 struct commit_list *p;
1395 for (p = list; p; p = p->next) {
1396 struct commit *commit = p->item;
1398 if (revs->right_only) {
1399 if (commit->object.flags & SYMMETRIC_LEFT)
1400 commit->object.flags |= SHOWN;
1401 } else /* revs->left_only is set */
1402 if (!(commit->object.flags & SYMMETRIC_LEFT))
1403 commit->object.flags |= SHOWN;
1407 static int limit_list(struct rev_info *revs)
1409 int slop = SLOP;
1410 timestamp_t date = TIME_MAX;
1411 struct commit_list *original_list = revs->commits;
1412 struct commit_list *newlist = NULL;
1413 struct commit_list **p = &newlist;
1414 struct commit *interesting_cache = NULL;
1416 if (revs->ancestry_path_implicit_bottoms) {
1417 collect_bottom_commits(original_list,
1418 &revs->ancestry_path_bottoms);
1419 if (!revs->ancestry_path_bottoms)
1420 die("--ancestry-path given but there are no bottom commits");
1423 while (original_list) {
1424 struct commit *commit = pop_commit(&original_list);
1425 struct object *obj = &commit->object;
1426 show_early_output_fn_t show;
1428 if (commit == interesting_cache)
1429 interesting_cache = NULL;
1431 if (revs->max_age != -1 && (commit->date < revs->max_age))
1432 obj->flags |= UNINTERESTING;
1433 if (process_parents(revs, commit, &original_list, NULL) < 0)
1434 return -1;
1435 if (obj->flags & UNINTERESTING) {
1436 mark_parents_uninteresting(revs, commit);
1437 slop = still_interesting(original_list, date, slop, &interesting_cache);
1438 if (slop)
1439 continue;
1440 break;
1442 if (revs->min_age != -1 && (commit->date > revs->min_age) &&
1443 !revs->line_level_traverse)
1444 continue;
1445 if (revs->max_age_as_filter != -1 &&
1446 (commit->date < revs->max_age_as_filter) && !revs->line_level_traverse)
1447 continue;
1448 date = commit->date;
1449 p = &commit_list_insert(commit, p)->next;
1451 show = show_early_output;
1452 if (!show)
1453 continue;
1455 show(revs, newlist);
1456 show_early_output = NULL;
1458 if (revs->cherry_pick || revs->cherry_mark)
1459 cherry_pick_list(newlist, revs);
1461 if (revs->left_only || revs->right_only)
1462 limit_left_right(newlist, revs);
1464 if (revs->ancestry_path)
1465 limit_to_ancestry(revs->ancestry_path_bottoms, newlist);
1468 * Check if any commits have become TREESAME by some of their parents
1469 * becoming UNINTERESTING.
1471 if (limiting_can_increase_treesame(revs)) {
1472 struct commit_list *list = NULL;
1473 for (list = newlist; list; list = list->next) {
1474 struct commit *c = list->item;
1475 if (c->object.flags & (UNINTERESTING | TREESAME))
1476 continue;
1477 update_treesame(revs, c);
1481 free_commit_list(original_list);
1482 revs->commits = newlist;
1483 return 0;
1487 * Add an entry to refs->cmdline with the specified information.
1488 * *name is copied.
1490 static void add_rev_cmdline(struct rev_info *revs,
1491 struct object *item,
1492 const char *name,
1493 int whence,
1494 unsigned flags)
1496 struct rev_cmdline_info *info = &revs->cmdline;
1497 unsigned int nr = info->nr;
1499 ALLOC_GROW(info->rev, nr + 1, info->alloc);
1500 info->rev[nr].item = item;
1501 info->rev[nr].name = xstrdup(name);
1502 info->rev[nr].whence = whence;
1503 info->rev[nr].flags = flags;
1504 info->nr++;
1507 static void add_rev_cmdline_list(struct rev_info *revs,
1508 struct commit_list *commit_list,
1509 int whence,
1510 unsigned flags)
1512 while (commit_list) {
1513 struct object *object = &commit_list->item->object;
1514 add_rev_cmdline(revs, object, oid_to_hex(&object->oid),
1515 whence, flags);
1516 commit_list = commit_list->next;
1520 struct all_refs_cb {
1521 int all_flags;
1522 int warned_bad_reflog;
1523 struct rev_info *all_revs;
1524 const char *name_for_errormsg;
1525 struct worktree *wt;
1528 int ref_excluded(struct string_list *ref_excludes, const char *path)
1530 struct string_list_item *item;
1532 if (!ref_excludes)
1533 return 0;
1534 for_each_string_list_item(item, ref_excludes) {
1535 if (!wildmatch(item->string, path, 0))
1536 return 1;
1538 return 0;
1541 static int handle_one_ref(const char *path, const struct object_id *oid,
1542 int flag UNUSED,
1543 void *cb_data)
1545 struct all_refs_cb *cb = cb_data;
1546 struct object *object;
1548 if (ref_excluded(cb->all_revs->ref_excludes, path))
1549 return 0;
1551 object = get_reference(cb->all_revs, path, oid, cb->all_flags);
1552 add_rev_cmdline(cb->all_revs, object, path, REV_CMD_REF, cb->all_flags);
1553 add_pending_object(cb->all_revs, object, path);
1554 return 0;
1557 static void init_all_refs_cb(struct all_refs_cb *cb, struct rev_info *revs,
1558 unsigned flags)
1560 cb->all_revs = revs;
1561 cb->all_flags = flags;
1562 revs->rev_input_given = 1;
1563 cb->wt = NULL;
1566 void clear_ref_exclusion(struct string_list **ref_excludes_p)
1568 if (*ref_excludes_p) {
1569 string_list_clear(*ref_excludes_p, 0);
1570 free(*ref_excludes_p);
1572 *ref_excludes_p = NULL;
1575 void add_ref_exclusion(struct string_list **ref_excludes_p, const char *exclude)
1577 if (!*ref_excludes_p) {
1578 CALLOC_ARRAY(*ref_excludes_p, 1);
1579 (*ref_excludes_p)->strdup_strings = 1;
1581 string_list_append(*ref_excludes_p, exclude);
1584 static void handle_refs(struct ref_store *refs,
1585 struct rev_info *revs, unsigned flags,
1586 int (*for_each)(struct ref_store *, each_ref_fn, void *))
1588 struct all_refs_cb cb;
1590 if (!refs) {
1591 /* this could happen with uninitialized submodules */
1592 return;
1595 init_all_refs_cb(&cb, revs, flags);
1596 for_each(refs, handle_one_ref, &cb);
1599 static void handle_one_reflog_commit(struct object_id *oid, void *cb_data)
1601 struct all_refs_cb *cb = cb_data;
1602 if (!is_null_oid(oid)) {
1603 struct object *o = parse_object(cb->all_revs->repo, oid);
1604 if (o) {
1605 o->flags |= cb->all_flags;
1606 /* ??? CMDLINEFLAGS ??? */
1607 add_pending_object(cb->all_revs, o, "");
1609 else if (!cb->warned_bad_reflog) {
1610 warning("reflog of '%s' references pruned commits",
1611 cb->name_for_errormsg);
1612 cb->warned_bad_reflog = 1;
1617 static int handle_one_reflog_ent(struct object_id *ooid, struct object_id *noid,
1618 const char *email UNUSED,
1619 timestamp_t timestamp UNUSED,
1620 int tz UNUSED,
1621 const char *message UNUSED,
1622 void *cb_data)
1624 handle_one_reflog_commit(ooid, cb_data);
1625 handle_one_reflog_commit(noid, cb_data);
1626 return 0;
1629 static int handle_one_reflog(const char *refname_in_wt,
1630 const struct object_id *oid UNUSED,
1631 int flag UNUSED, void *cb_data)
1633 struct all_refs_cb *cb = cb_data;
1634 struct strbuf refname = STRBUF_INIT;
1636 cb->warned_bad_reflog = 0;
1637 strbuf_worktree_ref(cb->wt, &refname, refname_in_wt);
1638 cb->name_for_errormsg = refname.buf;
1639 refs_for_each_reflog_ent(get_main_ref_store(the_repository),
1640 refname.buf,
1641 handle_one_reflog_ent, cb_data);
1642 strbuf_release(&refname);
1643 return 0;
1646 static void add_other_reflogs_to_pending(struct all_refs_cb *cb)
1648 struct worktree **worktrees, **p;
1650 worktrees = get_worktrees();
1651 for (p = worktrees; *p; p++) {
1652 struct worktree *wt = *p;
1654 if (wt->is_current)
1655 continue;
1657 cb->wt = wt;
1658 refs_for_each_reflog(get_worktree_ref_store(wt),
1659 handle_one_reflog,
1660 cb);
1662 free_worktrees(worktrees);
1665 void add_reflogs_to_pending(struct rev_info *revs, unsigned flags)
1667 struct all_refs_cb cb;
1669 cb.all_revs = revs;
1670 cb.all_flags = flags;
1671 cb.wt = NULL;
1672 for_each_reflog(handle_one_reflog, &cb);
1674 if (!revs->single_worktree)
1675 add_other_reflogs_to_pending(&cb);
1678 static void add_cache_tree(struct cache_tree *it, struct rev_info *revs,
1679 struct strbuf *path, unsigned int flags)
1681 size_t baselen = path->len;
1682 int i;
1684 if (it->entry_count >= 0) {
1685 struct tree *tree = lookup_tree(revs->repo, &it->oid);
1686 tree->object.flags |= flags;
1687 add_pending_object_with_path(revs, &tree->object, "",
1688 040000, path->buf);
1691 for (i = 0; i < it->subtree_nr; i++) {
1692 struct cache_tree_sub *sub = it->down[i];
1693 strbuf_addf(path, "%s%s", baselen ? "/" : "", sub->name);
1694 add_cache_tree(sub->cache_tree, revs, path, flags);
1695 strbuf_setlen(path, baselen);
1700 static void add_resolve_undo_to_pending(struct index_state *istate, struct rev_info *revs)
1702 struct string_list_item *item;
1703 struct string_list *resolve_undo = istate->resolve_undo;
1705 if (!resolve_undo)
1706 return;
1708 for_each_string_list_item(item, resolve_undo) {
1709 const char *path = item->string;
1710 struct resolve_undo_info *ru = item->util;
1711 int i;
1713 if (!ru)
1714 continue;
1715 for (i = 0; i < 3; i++) {
1716 struct blob *blob;
1718 if (!ru->mode[i] || !S_ISREG(ru->mode[i]))
1719 continue;
1721 blob = lookup_blob(revs->repo, &ru->oid[i]);
1722 if (!blob) {
1723 warning(_("resolve-undo records `%s` which is missing"),
1724 oid_to_hex(&ru->oid[i]));
1725 continue;
1727 add_pending_object_with_path(revs, &blob->object, "",
1728 ru->mode[i], path);
1733 static void do_add_index_objects_to_pending(struct rev_info *revs,
1734 struct index_state *istate,
1735 unsigned int flags)
1737 int i;
1739 /* TODO: audit for interaction with sparse-index. */
1740 ensure_full_index(istate);
1741 for (i = 0; i < istate->cache_nr; i++) {
1742 struct cache_entry *ce = istate->cache[i];
1743 struct blob *blob;
1745 if (S_ISGITLINK(ce->ce_mode))
1746 continue;
1748 blob = lookup_blob(revs->repo, &ce->oid);
1749 if (!blob)
1750 die("unable to add index blob to traversal");
1751 blob->object.flags |= flags;
1752 add_pending_object_with_path(revs, &blob->object, "",
1753 ce->ce_mode, ce->name);
1756 if (istate->cache_tree) {
1757 struct strbuf path = STRBUF_INIT;
1758 add_cache_tree(istate->cache_tree, revs, &path, flags);
1759 strbuf_release(&path);
1762 add_resolve_undo_to_pending(istate, revs);
1765 void add_index_objects_to_pending(struct rev_info *revs, unsigned int flags)
1767 struct worktree **worktrees, **p;
1769 repo_read_index(revs->repo);
1770 do_add_index_objects_to_pending(revs, revs->repo->index, flags);
1772 if (revs->single_worktree)
1773 return;
1775 worktrees = get_worktrees();
1776 for (p = worktrees; *p; p++) {
1777 struct worktree *wt = *p;
1778 struct index_state istate = { NULL };
1780 if (wt->is_current)
1781 continue; /* current index already taken care of */
1783 if (read_index_from(&istate,
1784 worktree_git_path(wt, "index"),
1785 get_worktree_git_dir(wt)) > 0)
1786 do_add_index_objects_to_pending(revs, &istate, flags);
1787 discard_index(&istate);
1789 free_worktrees(worktrees);
1792 struct add_alternate_refs_data {
1793 struct rev_info *revs;
1794 unsigned int flags;
1797 static void add_one_alternate_ref(const struct object_id *oid,
1798 void *vdata)
1800 const char *name = ".alternate";
1801 struct add_alternate_refs_data *data = vdata;
1802 struct object *obj;
1804 obj = get_reference(data->revs, name, oid, data->flags);
1805 add_rev_cmdline(data->revs, obj, name, REV_CMD_REV, data->flags);
1806 add_pending_object(data->revs, obj, name);
1809 static void add_alternate_refs_to_pending(struct rev_info *revs,
1810 unsigned int flags)
1812 struct add_alternate_refs_data data;
1813 data.revs = revs;
1814 data.flags = flags;
1815 for_each_alternate_ref(add_one_alternate_ref, &data);
1818 static int add_parents_only(struct rev_info *revs, const char *arg_, int flags,
1819 int exclude_parent)
1821 struct object_id oid;
1822 struct object *it;
1823 struct commit *commit;
1824 struct commit_list *parents;
1825 int parent_number;
1826 const char *arg = arg_;
1828 if (*arg == '^') {
1829 flags ^= UNINTERESTING | BOTTOM;
1830 arg++;
1832 if (get_oid_committish(arg, &oid))
1833 return 0;
1834 while (1) {
1835 it = get_reference(revs, arg, &oid, 0);
1836 if (!it && revs->ignore_missing)
1837 return 0;
1838 if (it->type != OBJ_TAG)
1839 break;
1840 if (!((struct tag*)it)->tagged)
1841 return 0;
1842 oidcpy(&oid, &((struct tag*)it)->tagged->oid);
1844 if (it->type != OBJ_COMMIT)
1845 return 0;
1846 commit = (struct commit *)it;
1847 if (exclude_parent &&
1848 exclude_parent > commit_list_count(commit->parents))
1849 return 0;
1850 for (parents = commit->parents, parent_number = 1;
1851 parents;
1852 parents = parents->next, parent_number++) {
1853 if (exclude_parent && parent_number != exclude_parent)
1854 continue;
1856 it = &parents->item->object;
1857 it->flags |= flags;
1858 add_rev_cmdline(revs, it, arg_, REV_CMD_PARENTS_ONLY, flags);
1859 add_pending_object(revs, it, arg);
1861 return 1;
1864 void repo_init_revisions(struct repository *r,
1865 struct rev_info *revs,
1866 const char *prefix)
1868 memset(revs, 0, sizeof(*revs));
1870 revs->repo = r;
1871 revs->abbrev = DEFAULT_ABBREV;
1872 revs->simplify_history = 1;
1873 revs->pruning.repo = r;
1874 revs->pruning.flags.recursive = 1;
1875 revs->pruning.flags.quick = 1;
1876 revs->pruning.add_remove = file_add_remove;
1877 revs->pruning.change = file_change;
1878 revs->pruning.change_fn_data = revs;
1879 revs->sort_order = REV_SORT_IN_GRAPH_ORDER;
1880 revs->dense = 1;
1881 revs->prefix = prefix;
1882 revs->max_age = -1;
1883 revs->max_age_as_filter = -1;
1884 revs->min_age = -1;
1885 revs->skip_count = -1;
1886 revs->max_count = -1;
1887 revs->max_parents = -1;
1888 revs->expand_tabs_in_log = -1;
1890 revs->commit_format = CMIT_FMT_DEFAULT;
1891 revs->expand_tabs_in_log_default = 8;
1893 grep_init(&revs->grep_filter, revs->repo);
1894 revs->grep_filter.status_only = 1;
1896 repo_diff_setup(revs->repo, &revs->diffopt);
1897 if (prefix && !revs->diffopt.prefix) {
1898 revs->diffopt.prefix = prefix;
1899 revs->diffopt.prefix_length = strlen(prefix);
1902 init_display_notes(&revs->notes_opt);
1903 list_objects_filter_init(&revs->filter);
1906 static void add_pending_commit_list(struct rev_info *revs,
1907 struct commit_list *commit_list,
1908 unsigned int flags)
1910 while (commit_list) {
1911 struct object *object = &commit_list->item->object;
1912 object->flags |= flags;
1913 add_pending_object(revs, object, oid_to_hex(&object->oid));
1914 commit_list = commit_list->next;
1918 static void prepare_show_merge(struct rev_info *revs)
1920 struct commit_list *bases;
1921 struct commit *head, *other;
1922 struct object_id oid;
1923 const char **prune = NULL;
1924 int i, prune_num = 1; /* counting terminating NULL */
1925 struct index_state *istate = revs->repo->index;
1927 if (get_oid("HEAD", &oid))
1928 die("--merge without HEAD?");
1929 head = lookup_commit_or_die(&oid, "HEAD");
1930 if (get_oid("MERGE_HEAD", &oid))
1931 die("--merge without MERGE_HEAD?");
1932 other = lookup_commit_or_die(&oid, "MERGE_HEAD");
1933 add_pending_object(revs, &head->object, "HEAD");
1934 add_pending_object(revs, &other->object, "MERGE_HEAD");
1935 bases = get_merge_bases(head, other);
1936 add_rev_cmdline_list(revs, bases, REV_CMD_MERGE_BASE, UNINTERESTING | BOTTOM);
1937 add_pending_commit_list(revs, bases, UNINTERESTING | BOTTOM);
1938 free_commit_list(bases);
1939 head->object.flags |= SYMMETRIC_LEFT;
1941 if (!istate->cache_nr)
1942 repo_read_index(revs->repo);
1943 for (i = 0; i < istate->cache_nr; i++) {
1944 const struct cache_entry *ce = istate->cache[i];
1945 if (!ce_stage(ce))
1946 continue;
1947 if (ce_path_match(istate, ce, &revs->prune_data, NULL)) {
1948 prune_num++;
1949 REALLOC_ARRAY(prune, prune_num);
1950 prune[prune_num-2] = ce->name;
1951 prune[prune_num-1] = NULL;
1953 while ((i+1 < istate->cache_nr) &&
1954 ce_same_name(ce, istate->cache[i+1]))
1955 i++;
1957 clear_pathspec(&revs->prune_data);
1958 parse_pathspec(&revs->prune_data, PATHSPEC_ALL_MAGIC & ~PATHSPEC_LITERAL,
1959 PATHSPEC_PREFER_FULL | PATHSPEC_LITERAL_PATH, "", prune);
1960 revs->limited = 1;
1963 static int dotdot_missing(const char *arg, char *dotdot,
1964 struct rev_info *revs, int symmetric)
1966 if (revs->ignore_missing)
1967 return 0;
1968 /* de-munge so we report the full argument */
1969 *dotdot = '.';
1970 die(symmetric
1971 ? "Invalid symmetric difference expression %s"
1972 : "Invalid revision range %s", arg);
1975 static int handle_dotdot_1(const char *arg, char *dotdot,
1976 struct rev_info *revs, int flags,
1977 int cant_be_filename,
1978 struct object_context *a_oc,
1979 struct object_context *b_oc)
1981 const char *a_name, *b_name;
1982 struct object_id a_oid, b_oid;
1983 struct object *a_obj, *b_obj;
1984 unsigned int a_flags, b_flags;
1985 int symmetric = 0;
1986 unsigned int flags_exclude = flags ^ (UNINTERESTING | BOTTOM);
1987 unsigned int oc_flags = GET_OID_COMMITTISH | GET_OID_RECORD_PATH;
1989 a_name = arg;
1990 if (!*a_name)
1991 a_name = "HEAD";
1993 b_name = dotdot + 2;
1994 if (*b_name == '.') {
1995 symmetric = 1;
1996 b_name++;
1998 if (!*b_name)
1999 b_name = "HEAD";
2001 if (get_oid_with_context(revs->repo, a_name, oc_flags, &a_oid, a_oc) ||
2002 get_oid_with_context(revs->repo, b_name, oc_flags, &b_oid, b_oc))
2003 return -1;
2005 if (!cant_be_filename) {
2006 *dotdot = '.';
2007 verify_non_filename(revs->prefix, arg);
2008 *dotdot = '\0';
2011 a_obj = parse_object(revs->repo, &a_oid);
2012 b_obj = parse_object(revs->repo, &b_oid);
2013 if (!a_obj || !b_obj)
2014 return dotdot_missing(arg, dotdot, revs, symmetric);
2016 if (!symmetric) {
2017 /* just A..B */
2018 b_flags = flags;
2019 a_flags = flags_exclude;
2020 } else {
2021 /* A...B -- find merge bases between the two */
2022 struct commit *a, *b;
2023 struct commit_list *exclude;
2025 a = lookup_commit_reference(revs->repo, &a_obj->oid);
2026 b = lookup_commit_reference(revs->repo, &b_obj->oid);
2027 if (!a || !b)
2028 return dotdot_missing(arg, dotdot, revs, symmetric);
2030 exclude = get_merge_bases(a, b);
2031 add_rev_cmdline_list(revs, exclude, REV_CMD_MERGE_BASE,
2032 flags_exclude);
2033 add_pending_commit_list(revs, exclude, flags_exclude);
2034 free_commit_list(exclude);
2036 b_flags = flags;
2037 a_flags = flags | SYMMETRIC_LEFT;
2040 a_obj->flags |= a_flags;
2041 b_obj->flags |= b_flags;
2042 add_rev_cmdline(revs, a_obj, a_name, REV_CMD_LEFT, a_flags);
2043 add_rev_cmdline(revs, b_obj, b_name, REV_CMD_RIGHT, b_flags);
2044 add_pending_object_with_path(revs, a_obj, a_name, a_oc->mode, a_oc->path);
2045 add_pending_object_with_path(revs, b_obj, b_name, b_oc->mode, b_oc->path);
2046 return 0;
2049 static int handle_dotdot(const char *arg,
2050 struct rev_info *revs, int flags,
2051 int cant_be_filename)
2053 struct object_context a_oc, b_oc;
2054 char *dotdot = strstr(arg, "..");
2055 int ret;
2057 if (!dotdot)
2058 return -1;
2060 memset(&a_oc, 0, sizeof(a_oc));
2061 memset(&b_oc, 0, sizeof(b_oc));
2063 *dotdot = '\0';
2064 ret = handle_dotdot_1(arg, dotdot, revs, flags, cant_be_filename,
2065 &a_oc, &b_oc);
2066 *dotdot = '.';
2068 free(a_oc.path);
2069 free(b_oc.path);
2071 return ret;
2074 static int handle_revision_arg_1(const char *arg_, struct rev_info *revs, int flags, unsigned revarg_opt)
2076 struct object_context oc;
2077 char *mark;
2078 struct object *object;
2079 struct object_id oid;
2080 int local_flags;
2081 const char *arg = arg_;
2082 int cant_be_filename = revarg_opt & REVARG_CANNOT_BE_FILENAME;
2083 unsigned get_sha1_flags = GET_OID_RECORD_PATH;
2085 flags = flags & UNINTERESTING ? flags | BOTTOM : flags & ~BOTTOM;
2087 if (!cant_be_filename && !strcmp(arg, "..")) {
2089 * Just ".."? That is not a range but the
2090 * pathspec for the parent directory.
2092 return -1;
2095 if (!handle_dotdot(arg, revs, flags, revarg_opt))
2096 return 0;
2098 mark = strstr(arg, "^@");
2099 if (mark && !mark[2]) {
2100 *mark = 0;
2101 if (add_parents_only(revs, arg, flags, 0))
2102 return 0;
2103 *mark = '^';
2105 mark = strstr(arg, "^!");
2106 if (mark && !mark[2]) {
2107 *mark = 0;
2108 if (!add_parents_only(revs, arg, flags ^ (UNINTERESTING | BOTTOM), 0))
2109 *mark = '^';
2111 mark = strstr(arg, "^-");
2112 if (mark) {
2113 int exclude_parent = 1;
2115 if (mark[2]) {
2116 if (strtol_i(mark + 2, 10, &exclude_parent) ||
2117 exclude_parent < 1)
2118 return -1;
2121 *mark = 0;
2122 if (!add_parents_only(revs, arg, flags ^ (UNINTERESTING | BOTTOM), exclude_parent))
2123 *mark = '^';
2126 local_flags = 0;
2127 if (*arg == '^') {
2128 local_flags = UNINTERESTING | BOTTOM;
2129 arg++;
2132 if (revarg_opt & REVARG_COMMITTISH)
2133 get_sha1_flags |= GET_OID_COMMITTISH;
2135 if (get_oid_with_context(revs->repo, arg, get_sha1_flags, &oid, &oc))
2136 return revs->ignore_missing ? 0 : -1;
2137 if (!cant_be_filename)
2138 verify_non_filename(revs->prefix, arg);
2139 object = get_reference(revs, arg, &oid, flags ^ local_flags);
2140 if (!object)
2141 return revs->ignore_missing ? 0 : -1;
2142 add_rev_cmdline(revs, object, arg_, REV_CMD_REV, flags ^ local_flags);
2143 add_pending_object_with_path(revs, object, arg, oc.mode, oc.path);
2144 free(oc.path);
2145 return 0;
2148 int handle_revision_arg(const char *arg, struct rev_info *revs, int flags, unsigned revarg_opt)
2150 int ret = handle_revision_arg_1(arg, revs, flags, revarg_opt);
2151 if (!ret)
2152 revs->rev_input_given = 1;
2153 return ret;
2156 static void read_pathspec_from_stdin(struct strbuf *sb,
2157 struct strvec *prune)
2159 while (strbuf_getline(sb, stdin) != EOF)
2160 strvec_push(prune, sb->buf);
2163 static void read_revisions_from_stdin(struct rev_info *revs,
2164 struct strvec *prune)
2166 struct strbuf sb;
2167 int seen_dashdash = 0;
2168 int save_warning;
2170 save_warning = warn_on_object_refname_ambiguity;
2171 warn_on_object_refname_ambiguity = 0;
2173 strbuf_init(&sb, 1000);
2174 while (strbuf_getline(&sb, stdin) != EOF) {
2175 int len = sb.len;
2176 if (!len)
2177 break;
2178 if (sb.buf[0] == '-') {
2179 if (len == 2 && sb.buf[1] == '-') {
2180 seen_dashdash = 1;
2181 break;
2183 die("options not supported in --stdin mode");
2185 if (handle_revision_arg(sb.buf, revs, 0,
2186 REVARG_CANNOT_BE_FILENAME))
2187 die("bad revision '%s'", sb.buf);
2189 if (seen_dashdash)
2190 read_pathspec_from_stdin(&sb, prune);
2192 strbuf_release(&sb);
2193 warn_on_object_refname_ambiguity = save_warning;
2196 static void add_grep(struct rev_info *revs, const char *ptn, enum grep_pat_token what)
2198 append_grep_pattern(&revs->grep_filter, ptn, "command line", 0, what);
2201 static void add_header_grep(struct rev_info *revs, enum grep_header_field field, const char *pattern)
2203 append_header_grep_pattern(&revs->grep_filter, field, pattern);
2206 static void add_message_grep(struct rev_info *revs, const char *pattern)
2208 add_grep(revs, pattern, GREP_PATTERN_BODY);
2211 static int handle_revision_opt(struct rev_info *revs, int argc, const char **argv,
2212 int *unkc, const char **unkv,
2213 const struct setup_revision_opt* opt)
2215 const char *arg = argv[0];
2216 const char *optarg = NULL;
2217 int argcount;
2218 const unsigned hexsz = the_hash_algo->hexsz;
2220 /* pseudo revision arguments */
2221 if (!strcmp(arg, "--all") || !strcmp(arg, "--branches") ||
2222 !strcmp(arg, "--tags") || !strcmp(arg, "--remotes") ||
2223 !strcmp(arg, "--reflog") || !strcmp(arg, "--not") ||
2224 !strcmp(arg, "--no-walk") || !strcmp(arg, "--do-walk") ||
2225 !strcmp(arg, "--bisect") || starts_with(arg, "--glob=") ||
2226 !strcmp(arg, "--indexed-objects") ||
2227 !strcmp(arg, "--alternate-refs") ||
2228 starts_with(arg, "--exclude=") ||
2229 starts_with(arg, "--branches=") || starts_with(arg, "--tags=") ||
2230 starts_with(arg, "--remotes=") || starts_with(arg, "--no-walk="))
2232 unkv[(*unkc)++] = arg;
2233 return 1;
2236 if ((argcount = parse_long_opt("max-count", argv, &optarg))) {
2237 revs->max_count = atoi(optarg);
2238 revs->no_walk = 0;
2239 return argcount;
2240 } else if ((argcount = parse_long_opt("skip", argv, &optarg))) {
2241 revs->skip_count = atoi(optarg);
2242 return argcount;
2243 } else if ((*arg == '-') && isdigit(arg[1])) {
2244 /* accept -<digit>, like traditional "head" */
2245 if (strtol_i(arg + 1, 10, &revs->max_count) < 0 ||
2246 revs->max_count < 0)
2247 die("'%s': not a non-negative integer", arg + 1);
2248 revs->no_walk = 0;
2249 } else if (!strcmp(arg, "-n")) {
2250 if (argc <= 1)
2251 return error("-n requires an argument");
2252 revs->max_count = atoi(argv[1]);
2253 revs->no_walk = 0;
2254 return 2;
2255 } else if (skip_prefix(arg, "-n", &optarg)) {
2256 revs->max_count = atoi(optarg);
2257 revs->no_walk = 0;
2258 } else if ((argcount = parse_long_opt("max-age", argv, &optarg))) {
2259 revs->max_age = atoi(optarg);
2260 return argcount;
2261 } else if ((argcount = parse_long_opt("since", argv, &optarg))) {
2262 revs->max_age = approxidate(optarg);
2263 return argcount;
2264 } else if ((argcount = parse_long_opt("since-as-filter", argv, &optarg))) {
2265 revs->max_age_as_filter = approxidate(optarg);
2266 return argcount;
2267 } else if ((argcount = parse_long_opt("after", argv, &optarg))) {
2268 revs->max_age = approxidate(optarg);
2269 return argcount;
2270 } else if ((argcount = parse_long_opt("min-age", argv, &optarg))) {
2271 revs->min_age = atoi(optarg);
2272 return argcount;
2273 } else if ((argcount = parse_long_opt("before", argv, &optarg))) {
2274 revs->min_age = approxidate(optarg);
2275 return argcount;
2276 } else if ((argcount = parse_long_opt("until", argv, &optarg))) {
2277 revs->min_age = approxidate(optarg);
2278 return argcount;
2279 } else if (!strcmp(arg, "--first-parent")) {
2280 revs->first_parent_only = 1;
2281 } else if (!strcmp(arg, "--exclude-first-parent-only")) {
2282 revs->exclude_first_parent_only = 1;
2283 } else if (!strcmp(arg, "--ancestry-path")) {
2284 revs->ancestry_path = 1;
2285 revs->simplify_history = 0;
2286 revs->limited = 1;
2287 revs->ancestry_path_implicit_bottoms = 1;
2288 } else if (skip_prefix(arg, "--ancestry-path=", &optarg)) {
2289 struct commit *c;
2290 struct object_id oid;
2291 const char *msg = _("could not get commit for ancestry-path argument %s");
2293 revs->ancestry_path = 1;
2294 revs->simplify_history = 0;
2295 revs->limited = 1;
2297 if (repo_get_oid_committish(revs->repo, optarg, &oid))
2298 return error(msg, optarg);
2299 get_reference(revs, optarg, &oid, ANCESTRY_PATH);
2300 c = lookup_commit_reference(revs->repo, &oid);
2301 if (!c)
2302 return error(msg, optarg);
2303 commit_list_insert(c, &revs->ancestry_path_bottoms);
2304 } else if (!strcmp(arg, "-g") || !strcmp(arg, "--walk-reflogs")) {
2305 init_reflog_walk(&revs->reflog_info);
2306 } else if (!strcmp(arg, "--default")) {
2307 if (argc <= 1)
2308 return error("bad --default argument");
2309 revs->def = argv[1];
2310 return 2;
2311 } else if (!strcmp(arg, "--merge")) {
2312 revs->show_merge = 1;
2313 } else if (!strcmp(arg, "--topo-order")) {
2314 revs->sort_order = REV_SORT_IN_GRAPH_ORDER;
2315 revs->topo_order = 1;
2316 } else if (!strcmp(arg, "--simplify-merges")) {
2317 revs->simplify_merges = 1;
2318 revs->topo_order = 1;
2319 revs->rewrite_parents = 1;
2320 revs->simplify_history = 0;
2321 revs->limited = 1;
2322 } else if (!strcmp(arg, "--simplify-by-decoration")) {
2323 revs->simplify_merges = 1;
2324 revs->topo_order = 1;
2325 revs->rewrite_parents = 1;
2326 revs->simplify_history = 0;
2327 revs->simplify_by_decoration = 1;
2328 revs->limited = 1;
2329 revs->prune = 1;
2330 } else if (!strcmp(arg, "--date-order")) {
2331 revs->sort_order = REV_SORT_BY_COMMIT_DATE;
2332 revs->topo_order = 1;
2333 } else if (!strcmp(arg, "--author-date-order")) {
2334 revs->sort_order = REV_SORT_BY_AUTHOR_DATE;
2335 revs->topo_order = 1;
2336 } else if (!strcmp(arg, "--early-output")) {
2337 revs->early_output = 100;
2338 revs->topo_order = 1;
2339 } else if (skip_prefix(arg, "--early-output=", &optarg)) {
2340 if (strtoul_ui(optarg, 10, &revs->early_output) < 0)
2341 die("'%s': not a non-negative integer", optarg);
2342 revs->topo_order = 1;
2343 } else if (!strcmp(arg, "--parents")) {
2344 revs->rewrite_parents = 1;
2345 revs->print_parents = 1;
2346 } else if (!strcmp(arg, "--dense")) {
2347 revs->dense = 1;
2348 } else if (!strcmp(arg, "--sparse")) {
2349 revs->dense = 0;
2350 } else if (!strcmp(arg, "--in-commit-order")) {
2351 revs->tree_blobs_in_commit_order = 1;
2352 } else if (!strcmp(arg, "--remove-empty")) {
2353 revs->remove_empty_trees = 1;
2354 } else if (!strcmp(arg, "--merges")) {
2355 revs->min_parents = 2;
2356 } else if (!strcmp(arg, "--no-merges")) {
2357 revs->max_parents = 1;
2358 } else if (skip_prefix(arg, "--min-parents=", &optarg)) {
2359 revs->min_parents = atoi(optarg);
2360 } else if (!strcmp(arg, "--no-min-parents")) {
2361 revs->min_parents = 0;
2362 } else if (skip_prefix(arg, "--max-parents=", &optarg)) {
2363 revs->max_parents = atoi(optarg);
2364 } else if (!strcmp(arg, "--no-max-parents")) {
2365 revs->max_parents = -1;
2366 } else if (!strcmp(arg, "--boundary")) {
2367 revs->boundary = 1;
2368 } else if (!strcmp(arg, "--left-right")) {
2369 revs->left_right = 1;
2370 } else if (!strcmp(arg, "--left-only")) {
2371 if (revs->right_only)
2372 die("--left-only is incompatible with --right-only"
2373 " or --cherry");
2374 revs->left_only = 1;
2375 } else if (!strcmp(arg, "--right-only")) {
2376 if (revs->left_only)
2377 die(_("options '%s' and '%s' cannot be used together"), "--right-only", "--left-only");
2378 revs->right_only = 1;
2379 } else if (!strcmp(arg, "--cherry")) {
2380 if (revs->left_only)
2381 die(_("options '%s' and '%s' cannot be used together"), "--cherry", "--left-only");
2382 revs->cherry_mark = 1;
2383 revs->right_only = 1;
2384 revs->max_parents = 1;
2385 revs->limited = 1;
2386 } else if (!strcmp(arg, "--count")) {
2387 revs->count = 1;
2388 } else if (!strcmp(arg, "--cherry-mark")) {
2389 if (revs->cherry_pick)
2390 die(_("options '%s' and '%s' cannot be used together"), "--cherry-mark", "--cherry-pick");
2391 revs->cherry_mark = 1;
2392 revs->limited = 1; /* needs limit_list() */
2393 } else if (!strcmp(arg, "--cherry-pick")) {
2394 if (revs->cherry_mark)
2395 die(_("options '%s' and '%s' cannot be used together"), "--cherry-pick", "--cherry-mark");
2396 revs->cherry_pick = 1;
2397 revs->limited = 1;
2398 } else if (!strcmp(arg, "--objects")) {
2399 revs->tag_objects = 1;
2400 revs->tree_objects = 1;
2401 revs->blob_objects = 1;
2402 } else if (!strcmp(arg, "--objects-edge")) {
2403 revs->tag_objects = 1;
2404 revs->tree_objects = 1;
2405 revs->blob_objects = 1;
2406 revs->edge_hint = 1;
2407 } else if (!strcmp(arg, "--objects-edge-aggressive")) {
2408 revs->tag_objects = 1;
2409 revs->tree_objects = 1;
2410 revs->blob_objects = 1;
2411 revs->edge_hint = 1;
2412 revs->edge_hint_aggressive = 1;
2413 } else if (!strcmp(arg, "--verify-objects")) {
2414 revs->tag_objects = 1;
2415 revs->tree_objects = 1;
2416 revs->blob_objects = 1;
2417 revs->verify_objects = 1;
2418 disable_commit_graph(revs->repo);
2419 } else if (!strcmp(arg, "--unpacked")) {
2420 revs->unpacked = 1;
2421 } else if (starts_with(arg, "--unpacked=")) {
2422 die(_("--unpacked=<packfile> no longer supported"));
2423 } else if (!strcmp(arg, "--no-kept-objects")) {
2424 revs->no_kept_objects = 1;
2425 revs->keep_pack_cache_flags |= IN_CORE_KEEP_PACKS;
2426 revs->keep_pack_cache_flags |= ON_DISK_KEEP_PACKS;
2427 } else if (skip_prefix(arg, "--no-kept-objects=", &optarg)) {
2428 revs->no_kept_objects = 1;
2429 if (!strcmp(optarg, "in-core"))
2430 revs->keep_pack_cache_flags |= IN_CORE_KEEP_PACKS;
2431 if (!strcmp(optarg, "on-disk"))
2432 revs->keep_pack_cache_flags |= ON_DISK_KEEP_PACKS;
2433 } else if (!strcmp(arg, "-r")) {
2434 revs->diff = 1;
2435 revs->diffopt.flags.recursive = 1;
2436 } else if (!strcmp(arg, "-t")) {
2437 revs->diff = 1;
2438 revs->diffopt.flags.recursive = 1;
2439 revs->diffopt.flags.tree_in_recursive = 1;
2440 } else if ((argcount = diff_merges_parse_opts(revs, argv))) {
2441 return argcount;
2442 } else if (!strcmp(arg, "-v")) {
2443 revs->verbose_header = 1;
2444 } else if (!strcmp(arg, "--pretty")) {
2445 revs->verbose_header = 1;
2446 revs->pretty_given = 1;
2447 get_commit_format(NULL, revs);
2448 } else if (skip_prefix(arg, "--pretty=", &optarg) ||
2449 skip_prefix(arg, "--format=", &optarg)) {
2451 * Detached form ("--pretty X" as opposed to "--pretty=X")
2452 * not allowed, since the argument is optional.
2454 revs->verbose_header = 1;
2455 revs->pretty_given = 1;
2456 get_commit_format(optarg, revs);
2457 } else if (!strcmp(arg, "--expand-tabs")) {
2458 revs->expand_tabs_in_log = 8;
2459 } else if (!strcmp(arg, "--no-expand-tabs")) {
2460 revs->expand_tabs_in_log = 0;
2461 } else if (skip_prefix(arg, "--expand-tabs=", &arg)) {
2462 int val;
2463 if (strtol_i(arg, 10, &val) < 0 || val < 0)
2464 die("'%s': not a non-negative integer", arg);
2465 revs->expand_tabs_in_log = val;
2466 } else if (!strcmp(arg, "--show-notes") || !strcmp(arg, "--notes")) {
2467 enable_default_display_notes(&revs->notes_opt, &revs->show_notes);
2468 revs->show_notes_given = 1;
2469 } else if (!strcmp(arg, "--show-signature")) {
2470 revs->show_signature = 1;
2471 } else if (!strcmp(arg, "--no-show-signature")) {
2472 revs->show_signature = 0;
2473 } else if (!strcmp(arg, "--show-linear-break")) {
2474 revs->break_bar = " ..........";
2475 revs->track_linear = 1;
2476 revs->track_first_time = 1;
2477 } else if (skip_prefix(arg, "--show-linear-break=", &optarg)) {
2478 revs->break_bar = xstrdup(optarg);
2479 revs->track_linear = 1;
2480 revs->track_first_time = 1;
2481 } else if (skip_prefix(arg, "--show-notes=", &optarg) ||
2482 skip_prefix(arg, "--notes=", &optarg)) {
2483 if (starts_with(arg, "--show-notes=") &&
2484 revs->notes_opt.use_default_notes < 0)
2485 revs->notes_opt.use_default_notes = 1;
2486 enable_ref_display_notes(&revs->notes_opt, &revs->show_notes, optarg);
2487 revs->show_notes_given = 1;
2488 } else if (!strcmp(arg, "--no-notes")) {
2489 disable_display_notes(&revs->notes_opt, &revs->show_notes);
2490 revs->show_notes_given = 1;
2491 } else if (!strcmp(arg, "--standard-notes")) {
2492 revs->show_notes_given = 1;
2493 revs->notes_opt.use_default_notes = 1;
2494 } else if (!strcmp(arg, "--no-standard-notes")) {
2495 revs->notes_opt.use_default_notes = 0;
2496 } else if (!strcmp(arg, "--oneline")) {
2497 revs->verbose_header = 1;
2498 get_commit_format("oneline", revs);
2499 revs->pretty_given = 1;
2500 revs->abbrev_commit = 1;
2501 } else if (!strcmp(arg, "--graph")) {
2502 graph_clear(revs->graph);
2503 revs->graph = graph_init(revs);
2504 } else if (!strcmp(arg, "--no-graph")) {
2505 graph_clear(revs->graph);
2506 revs->graph = NULL;
2507 } else if (!strcmp(arg, "--encode-email-headers")) {
2508 revs->encode_email_headers = 1;
2509 } else if (!strcmp(arg, "--no-encode-email-headers")) {
2510 revs->encode_email_headers = 0;
2511 } else if (!strcmp(arg, "--root")) {
2512 revs->show_root_diff = 1;
2513 } else if (!strcmp(arg, "--no-commit-id")) {
2514 revs->no_commit_id = 1;
2515 } else if (!strcmp(arg, "--always")) {
2516 revs->always_show_header = 1;
2517 } else if (!strcmp(arg, "--no-abbrev")) {
2518 revs->abbrev = 0;
2519 } else if (!strcmp(arg, "--abbrev")) {
2520 revs->abbrev = DEFAULT_ABBREV;
2521 } else if (skip_prefix(arg, "--abbrev=", &optarg)) {
2522 revs->abbrev = strtoul(optarg, NULL, 10);
2523 if (revs->abbrev < MINIMUM_ABBREV)
2524 revs->abbrev = MINIMUM_ABBREV;
2525 else if (revs->abbrev > hexsz)
2526 revs->abbrev = hexsz;
2527 } else if (!strcmp(arg, "--abbrev-commit")) {
2528 revs->abbrev_commit = 1;
2529 revs->abbrev_commit_given = 1;
2530 } else if (!strcmp(arg, "--no-abbrev-commit")) {
2531 revs->abbrev_commit = 0;
2532 } else if (!strcmp(arg, "--full-diff")) {
2533 revs->diff = 1;
2534 revs->full_diff = 1;
2535 } else if (!strcmp(arg, "--show-pulls")) {
2536 revs->show_pulls = 1;
2537 } else if (!strcmp(arg, "--full-history")) {
2538 revs->simplify_history = 0;
2539 } else if (!strcmp(arg, "--relative-date")) {
2540 revs->date_mode.type = DATE_RELATIVE;
2541 revs->date_mode_explicit = 1;
2542 } else if ((argcount = parse_long_opt("date", argv, &optarg))) {
2543 parse_date_format(optarg, &revs->date_mode);
2544 revs->date_mode_explicit = 1;
2545 return argcount;
2546 } else if (!strcmp(arg, "--log-size")) {
2547 revs->show_log_size = 1;
2550 * Grepping the commit log
2552 else if ((argcount = parse_long_opt("author", argv, &optarg))) {
2553 add_header_grep(revs, GREP_HEADER_AUTHOR, optarg);
2554 return argcount;
2555 } else if ((argcount = parse_long_opt("committer", argv, &optarg))) {
2556 add_header_grep(revs, GREP_HEADER_COMMITTER, optarg);
2557 return argcount;
2558 } else if ((argcount = parse_long_opt("grep-reflog", argv, &optarg))) {
2559 add_header_grep(revs, GREP_HEADER_REFLOG, optarg);
2560 return argcount;
2561 } else if ((argcount = parse_long_opt("grep", argv, &optarg))) {
2562 add_message_grep(revs, optarg);
2563 return argcount;
2564 } else if (!strcmp(arg, "--basic-regexp")) {
2565 revs->grep_filter.pattern_type_option = GREP_PATTERN_TYPE_BRE;
2566 } else if (!strcmp(arg, "--extended-regexp") || !strcmp(arg, "-E")) {
2567 revs->grep_filter.pattern_type_option = GREP_PATTERN_TYPE_ERE;
2568 } else if (!strcmp(arg, "--regexp-ignore-case") || !strcmp(arg, "-i")) {
2569 revs->grep_filter.ignore_case = 1;
2570 revs->diffopt.pickaxe_opts |= DIFF_PICKAXE_IGNORE_CASE;
2571 } else if (!strcmp(arg, "--fixed-strings") || !strcmp(arg, "-F")) {
2572 revs->grep_filter.pattern_type_option = GREP_PATTERN_TYPE_FIXED;
2573 } else if (!strcmp(arg, "--perl-regexp") || !strcmp(arg, "-P")) {
2574 revs->grep_filter.pattern_type_option = GREP_PATTERN_TYPE_PCRE;
2575 } else if (!strcmp(arg, "--all-match")) {
2576 revs->grep_filter.all_match = 1;
2577 } else if (!strcmp(arg, "--invert-grep")) {
2578 revs->grep_filter.no_body_match = 1;
2579 } else if ((argcount = parse_long_opt("encoding", argv, &optarg))) {
2580 if (strcmp(optarg, "none"))
2581 git_log_output_encoding = xstrdup(optarg);
2582 else
2583 git_log_output_encoding = "";
2584 return argcount;
2585 } else if (!strcmp(arg, "--reverse")) {
2586 revs->reverse ^= 1;
2587 } else if (!strcmp(arg, "--children")) {
2588 revs->children.name = "children";
2589 revs->limited = 1;
2590 } else if (!strcmp(arg, "--ignore-missing")) {
2591 revs->ignore_missing = 1;
2592 } else if (opt && opt->allow_exclude_promisor_objects &&
2593 !strcmp(arg, "--exclude-promisor-objects")) {
2594 if (fetch_if_missing)
2595 BUG("exclude_promisor_objects can only be used when fetch_if_missing is 0");
2596 revs->exclude_promisor_objects = 1;
2597 } else {
2598 int opts = diff_opt_parse(&revs->diffopt, argv, argc, revs->prefix);
2599 if (!opts)
2600 unkv[(*unkc)++] = arg;
2601 return opts;
2604 return 1;
2607 void parse_revision_opt(struct rev_info *revs, struct parse_opt_ctx_t *ctx,
2608 const struct option *options,
2609 const char * const usagestr[])
2611 int n = handle_revision_opt(revs, ctx->argc, ctx->argv,
2612 &ctx->cpidx, ctx->out, NULL);
2613 if (n <= 0) {
2614 error("unknown option `%s'", ctx->argv[0]);
2615 usage_with_options(usagestr, options);
2617 ctx->argv += n;
2618 ctx->argc -= n;
2621 void revision_opts_finish(struct rev_info *revs)
2623 if (revs->graph && revs->track_linear)
2624 die(_("options '%s' and '%s' cannot be used together"), "--show-linear-break", "--graph");
2626 if (revs->graph) {
2627 revs->topo_order = 1;
2628 revs->rewrite_parents = 1;
2632 static int for_each_bisect_ref(struct ref_store *refs, each_ref_fn fn,
2633 void *cb_data, const char *term)
2635 struct strbuf bisect_refs = STRBUF_INIT;
2636 int status;
2637 strbuf_addf(&bisect_refs, "refs/bisect/%s", term);
2638 status = refs_for_each_fullref_in(refs, bisect_refs.buf, fn, cb_data);
2639 strbuf_release(&bisect_refs);
2640 return status;
2643 static int for_each_bad_bisect_ref(struct ref_store *refs, each_ref_fn fn, void *cb_data)
2645 return for_each_bisect_ref(refs, fn, cb_data, term_bad);
2648 static int for_each_good_bisect_ref(struct ref_store *refs, each_ref_fn fn, void *cb_data)
2650 return for_each_bisect_ref(refs, fn, cb_data, term_good);
2653 static int handle_revision_pseudo_opt(struct rev_info *revs,
2654 const char **argv, int *flags)
2656 const char *arg = argv[0];
2657 const char *optarg;
2658 struct ref_store *refs;
2659 int argcount;
2661 if (revs->repo != the_repository) {
2663 * We need some something like get_submodule_worktrees()
2664 * before we can go through all worktrees of a submodule,
2665 * .e.g with adding all HEADs from --all, which is not
2666 * supported right now, so stick to single worktree.
2668 if (!revs->single_worktree)
2669 BUG("--single-worktree cannot be used together with submodule");
2671 refs = get_main_ref_store(revs->repo);
2674 * NOTE!
2676 * Commands like "git shortlog" will not accept the options below
2677 * unless parse_revision_opt queues them (as opposed to erroring
2678 * out).
2680 * When implementing your new pseudo-option, remember to
2681 * register it in the list at the top of handle_revision_opt.
2683 if (!strcmp(arg, "--all")) {
2684 handle_refs(refs, revs, *flags, refs_for_each_ref);
2685 handle_refs(refs, revs, *flags, refs_head_ref);
2686 if (!revs->single_worktree) {
2687 struct all_refs_cb cb;
2689 init_all_refs_cb(&cb, revs, *flags);
2690 other_head_refs(handle_one_ref, &cb);
2692 clear_ref_exclusion(&revs->ref_excludes);
2693 } else if (!strcmp(arg, "--branches")) {
2694 handle_refs(refs, revs, *flags, refs_for_each_branch_ref);
2695 clear_ref_exclusion(&revs->ref_excludes);
2696 } else if (!strcmp(arg, "--bisect")) {
2697 read_bisect_terms(&term_bad, &term_good);
2698 handle_refs(refs, revs, *flags, for_each_bad_bisect_ref);
2699 handle_refs(refs, revs, *flags ^ (UNINTERESTING | BOTTOM),
2700 for_each_good_bisect_ref);
2701 revs->bisect = 1;
2702 } else if (!strcmp(arg, "--tags")) {
2703 handle_refs(refs, revs, *flags, refs_for_each_tag_ref);
2704 clear_ref_exclusion(&revs->ref_excludes);
2705 } else if (!strcmp(arg, "--remotes")) {
2706 handle_refs(refs, revs, *flags, refs_for_each_remote_ref);
2707 clear_ref_exclusion(&revs->ref_excludes);
2708 } else if ((argcount = parse_long_opt("glob", argv, &optarg))) {
2709 struct all_refs_cb cb;
2710 init_all_refs_cb(&cb, revs, *flags);
2711 for_each_glob_ref(handle_one_ref, optarg, &cb);
2712 clear_ref_exclusion(&revs->ref_excludes);
2713 return argcount;
2714 } else if ((argcount = parse_long_opt("exclude", argv, &optarg))) {
2715 add_ref_exclusion(&revs->ref_excludes, optarg);
2716 return argcount;
2717 } else if (skip_prefix(arg, "--branches=", &optarg)) {
2718 struct all_refs_cb cb;
2719 init_all_refs_cb(&cb, revs, *flags);
2720 for_each_glob_ref_in(handle_one_ref, optarg, "refs/heads/", &cb);
2721 clear_ref_exclusion(&revs->ref_excludes);
2722 } else if (skip_prefix(arg, "--tags=", &optarg)) {
2723 struct all_refs_cb cb;
2724 init_all_refs_cb(&cb, revs, *flags);
2725 for_each_glob_ref_in(handle_one_ref, optarg, "refs/tags/", &cb);
2726 clear_ref_exclusion(&revs->ref_excludes);
2727 } else if (skip_prefix(arg, "--remotes=", &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/remotes/", &cb);
2731 clear_ref_exclusion(&revs->ref_excludes);
2732 } else if (!strcmp(arg, "--reflog")) {
2733 add_reflogs_to_pending(revs, *flags);
2734 } else if (!strcmp(arg, "--indexed-objects")) {
2735 add_index_objects_to_pending(revs, *flags);
2736 } else if (!strcmp(arg, "--alternate-refs")) {
2737 add_alternate_refs_to_pending(revs, *flags);
2738 } else if (!strcmp(arg, "--not")) {
2739 *flags ^= UNINTERESTING | BOTTOM;
2740 } else if (!strcmp(arg, "--no-walk")) {
2741 revs->no_walk = 1;
2742 } else if (skip_prefix(arg, "--no-walk=", &optarg)) {
2744 * Detached form ("--no-walk X" as opposed to "--no-walk=X")
2745 * not allowed, since the argument is optional.
2747 revs->no_walk = 1;
2748 if (!strcmp(optarg, "sorted"))
2749 revs->unsorted_input = 0;
2750 else if (!strcmp(optarg, "unsorted"))
2751 revs->unsorted_input = 1;
2752 else
2753 return error("invalid argument to --no-walk");
2754 } else if (!strcmp(arg, "--do-walk")) {
2755 revs->no_walk = 0;
2756 } else if (!strcmp(arg, "--single-worktree")) {
2757 revs->single_worktree = 1;
2758 } else if (skip_prefix(arg, ("--filter="), &arg)) {
2759 parse_list_objects_filter(&revs->filter, arg);
2760 } else if (!strcmp(arg, ("--no-filter"))) {
2761 list_objects_filter_set_no_filter(&revs->filter);
2762 } else {
2763 return 0;
2766 return 1;
2769 static void NORETURN diagnose_missing_default(const char *def)
2771 int flags;
2772 const char *refname;
2774 refname = resolve_ref_unsafe(def, 0, NULL, &flags);
2775 if (!refname || !(flags & REF_ISSYMREF) || (flags & REF_ISBROKEN))
2776 die(_("your current branch appears to be broken"));
2778 skip_prefix(refname, "refs/heads/", &refname);
2779 die(_("your current branch '%s' does not have any commits yet"),
2780 refname);
2784 * Parse revision information, filling in the "rev_info" structure,
2785 * and removing the used arguments from the argument list.
2787 * Returns the number of arguments left that weren't recognized
2788 * (which are also moved to the head of the argument list)
2790 int setup_revisions(int argc, const char **argv, struct rev_info *revs, struct setup_revision_opt *opt)
2792 int i, flags, left, seen_dashdash, revarg_opt;
2793 struct strvec prune_data = STRVEC_INIT;
2794 int seen_end_of_options = 0;
2796 /* First, search for "--" */
2797 if (opt && opt->assume_dashdash) {
2798 seen_dashdash = 1;
2799 } else {
2800 seen_dashdash = 0;
2801 for (i = 1; i < argc; i++) {
2802 const char *arg = argv[i];
2803 if (strcmp(arg, "--"))
2804 continue;
2805 if (opt && opt->free_removed_argv_elements)
2806 free((char *)argv[i]);
2807 argv[i] = NULL;
2808 argc = i;
2809 if (argv[i + 1])
2810 strvec_pushv(&prune_data, argv + i + 1);
2811 seen_dashdash = 1;
2812 break;
2816 /* Second, deal with arguments and options */
2817 flags = 0;
2818 revarg_opt = opt ? opt->revarg_opt : 0;
2819 if (seen_dashdash)
2820 revarg_opt |= REVARG_CANNOT_BE_FILENAME;
2821 for (left = i = 1; i < argc; i++) {
2822 const char *arg = argv[i];
2823 if (!seen_end_of_options && *arg == '-') {
2824 int opts;
2826 opts = handle_revision_pseudo_opt(
2827 revs, argv + i,
2828 &flags);
2829 if (opts > 0) {
2830 i += opts - 1;
2831 continue;
2834 if (!strcmp(arg, "--stdin")) {
2835 if (revs->disable_stdin) {
2836 argv[left++] = arg;
2837 continue;
2839 if (revs->read_from_stdin++)
2840 die("--stdin given twice?");
2841 read_revisions_from_stdin(revs, &prune_data);
2842 continue;
2845 if (!strcmp(arg, "--end-of-options")) {
2846 seen_end_of_options = 1;
2847 continue;
2850 opts = handle_revision_opt(revs, argc - i, argv + i,
2851 &left, argv, opt);
2852 if (opts > 0) {
2853 i += opts - 1;
2854 continue;
2856 if (opts < 0)
2857 exit(128);
2858 continue;
2862 if (handle_revision_arg(arg, revs, flags, revarg_opt)) {
2863 int j;
2864 if (seen_dashdash || *arg == '^')
2865 die("bad revision '%s'", arg);
2867 /* If we didn't have a "--":
2868 * (1) all filenames must exist;
2869 * (2) all rev-args must not be interpretable
2870 * as a valid filename.
2871 * but the latter we have checked in the main loop.
2873 for (j = i; j < argc; j++)
2874 verify_filename(revs->prefix, argv[j], j == i);
2876 strvec_pushv(&prune_data, argv + i);
2877 break;
2880 revision_opts_finish(revs);
2882 if (prune_data.nr) {
2884 * If we need to introduce the magic "a lone ':' means no
2885 * pathspec whatsoever", here is the place to do so.
2887 * if (prune_data.nr == 1 && !strcmp(prune_data[0], ":")) {
2888 * prune_data.nr = 0;
2889 * prune_data.alloc = 0;
2890 * free(prune_data.path);
2891 * prune_data.path = NULL;
2892 * } else {
2893 * terminate prune_data.alloc with NULL and
2894 * call init_pathspec() to set revs->prune_data here.
2897 parse_pathspec(&revs->prune_data, 0, 0,
2898 revs->prefix, prune_data.v);
2900 strvec_clear(&prune_data);
2902 if (!revs->def)
2903 revs->def = opt ? opt->def : NULL;
2904 if (opt && opt->tweak)
2905 opt->tweak(revs, opt);
2906 if (revs->show_merge)
2907 prepare_show_merge(revs);
2908 if (revs->def && !revs->pending.nr && !revs->rev_input_given) {
2909 struct object_id oid;
2910 struct object *object;
2911 struct object_context oc;
2912 if (get_oid_with_context(revs->repo, revs->def, 0, &oid, &oc))
2913 diagnose_missing_default(revs->def);
2914 object = get_reference(revs, revs->def, &oid, 0);
2915 add_pending_object_with_mode(revs, object, revs->def, oc.mode);
2918 /* Did the user ask for any diff output? Run the diff! */
2919 if (revs->diffopt.output_format & ~DIFF_FORMAT_NO_OUTPUT)
2920 revs->diff = 1;
2922 /* Pickaxe, diff-filter and rename following need diffs */
2923 if ((revs->diffopt.pickaxe_opts & DIFF_PICKAXE_KINDS_MASK) ||
2924 revs->diffopt.filter ||
2925 revs->diffopt.flags.follow_renames)
2926 revs->diff = 1;
2928 if (revs->diffopt.objfind)
2929 revs->simplify_history = 0;
2931 if (revs->line_level_traverse) {
2932 if (want_ancestry(revs))
2933 revs->limited = 1;
2934 revs->topo_order = 1;
2937 if (revs->topo_order && !generation_numbers_enabled(the_repository))
2938 revs->limited = 1;
2940 if (revs->prune_data.nr) {
2941 copy_pathspec(&revs->pruning.pathspec, &revs->prune_data);
2942 /* Can't prune commits with rename following: the paths change.. */
2943 if (!revs->diffopt.flags.follow_renames)
2944 revs->prune = 1;
2945 if (!revs->full_diff)
2946 copy_pathspec(&revs->diffopt.pathspec,
2947 &revs->prune_data);
2950 diff_merges_setup_revs(revs);
2952 revs->diffopt.abbrev = revs->abbrev;
2954 diff_setup_done(&revs->diffopt);
2956 if (!is_encoding_utf8(get_log_output_encoding()))
2957 revs->grep_filter.ignore_locale = 1;
2958 compile_grep_patterns(&revs->grep_filter);
2960 if (revs->reverse && revs->reflog_info)
2961 die(_("options '%s' and '%s' cannot be used together"), "--reverse", "--walk-reflogs");
2962 if (revs->reflog_info && revs->limited)
2963 die("cannot combine --walk-reflogs with history-limiting options");
2964 if (revs->rewrite_parents && revs->children.name)
2965 die(_("options '%s' and '%s' cannot be used together"), "--parents", "--children");
2966 if (revs->filter.choice && !revs->blob_objects)
2967 die(_("object filtering requires --objects"));
2970 * Limitations on the graph functionality
2972 if (revs->reverse && revs->graph)
2973 die(_("options '%s' and '%s' cannot be used together"), "--reverse", "--graph");
2975 if (revs->reflog_info && revs->graph)
2976 die(_("options '%s' and '%s' cannot be used together"), "--walk-reflogs", "--graph");
2977 if (revs->no_walk && revs->graph)
2978 die(_("options '%s' and '%s' cannot be used together"), "--no-walk", "--graph");
2979 if (!revs->reflog_info && revs->grep_filter.use_reflog_filter)
2980 die(_("the option '%s' requires '%s'"), "--grep-reflog", "--walk-reflogs");
2982 if (revs->line_level_traverse &&
2983 (revs->diffopt.output_format & ~(DIFF_FORMAT_PATCH | DIFF_FORMAT_NO_OUTPUT)))
2984 die(_("-L does not yet support diff formats besides -p and -s"));
2986 if (revs->expand_tabs_in_log < 0)
2987 revs->expand_tabs_in_log = revs->expand_tabs_in_log_default;
2989 return left;
2992 static void release_revisions_cmdline(struct rev_cmdline_info *cmdline)
2994 unsigned int i;
2996 for (i = 0; i < cmdline->nr; i++)
2997 free((char *)cmdline->rev[i].name);
2998 free(cmdline->rev);
3001 static void release_revisions_mailmap(struct string_list *mailmap)
3003 if (!mailmap)
3004 return;
3005 clear_mailmap(mailmap);
3006 free(mailmap);
3009 static void release_revisions_topo_walk_info(struct topo_walk_info *info);
3011 void release_revisions(struct rev_info *revs)
3013 free_commit_list(revs->commits);
3014 free_commit_list(revs->ancestry_path_bottoms);
3015 object_array_clear(&revs->pending);
3016 object_array_clear(&revs->boundary_commits);
3017 release_revisions_cmdline(&revs->cmdline);
3018 list_objects_filter_release(&revs->filter);
3019 clear_pathspec(&revs->prune_data);
3020 date_mode_release(&revs->date_mode);
3021 release_revisions_mailmap(revs->mailmap);
3022 free_grep_patterns(&revs->grep_filter);
3023 /* TODO (need to handle "no_free"): diff_free(&revs->diffopt) */
3024 diff_free(&revs->pruning);
3025 reflog_walk_info_release(revs->reflog_info);
3026 release_revisions_topo_walk_info(revs->topo_walk_info);
3029 static void add_child(struct rev_info *revs, struct commit *parent, struct commit *child)
3031 struct commit_list *l = xcalloc(1, sizeof(*l));
3033 l->item = child;
3034 l->next = add_decoration(&revs->children, &parent->object, l);
3037 static int remove_duplicate_parents(struct rev_info *revs, struct commit *commit)
3039 struct treesame_state *ts = lookup_decoration(&revs->treesame, &commit->object);
3040 struct commit_list **pp, *p;
3041 int surviving_parents;
3043 /* Examine existing parents while marking ones we have seen... */
3044 pp = &commit->parents;
3045 surviving_parents = 0;
3046 while ((p = *pp) != NULL) {
3047 struct commit *parent = p->item;
3048 if (parent->object.flags & TMP_MARK) {
3049 *pp = p->next;
3050 if (ts)
3051 compact_treesame(revs, commit, surviving_parents);
3052 continue;
3054 parent->object.flags |= TMP_MARK;
3055 surviving_parents++;
3056 pp = &p->next;
3058 /* clear the temporary mark */
3059 for (p = commit->parents; p; p = p->next) {
3060 p->item->object.flags &= ~TMP_MARK;
3062 /* no update_treesame() - removing duplicates can't affect TREESAME */
3063 return surviving_parents;
3066 struct merge_simplify_state {
3067 struct commit *simplified;
3070 static struct merge_simplify_state *locate_simplify_state(struct rev_info *revs, struct commit *commit)
3072 struct merge_simplify_state *st;
3074 st = lookup_decoration(&revs->merge_simplification, &commit->object);
3075 if (!st) {
3076 CALLOC_ARRAY(st, 1);
3077 add_decoration(&revs->merge_simplification, &commit->object, st);
3079 return st;
3082 static int mark_redundant_parents(struct commit *commit)
3084 struct commit_list *h = reduce_heads(commit->parents);
3085 int i = 0, marked = 0;
3086 struct commit_list *po, *pn;
3088 /* Want these for sanity-checking only */
3089 int orig_cnt = commit_list_count(commit->parents);
3090 int cnt = commit_list_count(h);
3093 * Not ready to remove items yet, just mark them for now, based
3094 * on the output of reduce_heads(). reduce_heads outputs the reduced
3095 * set in its original order, so this isn't too hard.
3097 po = commit->parents;
3098 pn = h;
3099 while (po) {
3100 if (pn && po->item == pn->item) {
3101 pn = pn->next;
3102 i++;
3103 } else {
3104 po->item->object.flags |= TMP_MARK;
3105 marked++;
3107 po=po->next;
3110 if (i != cnt || cnt+marked != orig_cnt)
3111 die("mark_redundant_parents %d %d %d %d", orig_cnt, cnt, i, marked);
3113 free_commit_list(h);
3115 return marked;
3118 static int mark_treesame_root_parents(struct commit *commit)
3120 struct commit_list *p;
3121 int marked = 0;
3123 for (p = commit->parents; p; p = p->next) {
3124 struct commit *parent = p->item;
3125 if (!parent->parents && (parent->object.flags & TREESAME)) {
3126 parent->object.flags |= TMP_MARK;
3127 marked++;
3131 return marked;
3135 * Awkward naming - this means one parent we are TREESAME to.
3136 * cf mark_treesame_root_parents: root parents that are TREESAME (to an
3137 * empty tree). Better name suggestions?
3139 static int leave_one_treesame_to_parent(struct rev_info *revs, struct commit *commit)
3141 struct treesame_state *ts = lookup_decoration(&revs->treesame, &commit->object);
3142 struct commit *unmarked = NULL, *marked = NULL;
3143 struct commit_list *p;
3144 unsigned n;
3146 for (p = commit->parents, n = 0; p; p = p->next, n++) {
3147 if (ts->treesame[n]) {
3148 if (p->item->object.flags & TMP_MARK) {
3149 if (!marked)
3150 marked = p->item;
3151 } else {
3152 if (!unmarked) {
3153 unmarked = p->item;
3154 break;
3161 * If we are TREESAME to a marked-for-deletion parent, but not to any
3162 * unmarked parents, unmark the first TREESAME parent. This is the
3163 * parent that the default simplify_history==1 scan would have followed,
3164 * and it doesn't make sense to omit that path when asking for a
3165 * simplified full history. Retaining it improves the chances of
3166 * understanding odd missed merges that took an old version of a file.
3168 * Example:
3170 * I--------*X A modified the file, but mainline merge X used
3171 * \ / "-s ours", so took the version from I. X is
3172 * `-*A--' TREESAME to I and !TREESAME to A.
3174 * Default log from X would produce "I". Without this check,
3175 * --full-history --simplify-merges would produce "I-A-X", showing
3176 * the merge commit X and that it changed A, but not making clear that
3177 * it had just taken the I version. With this check, the topology above
3178 * is retained.
3180 * Note that it is possible that the simplification chooses a different
3181 * TREESAME parent from the default, in which case this test doesn't
3182 * activate, and we _do_ drop the default parent. Example:
3184 * I------X A modified the file, but it was reverted in B,
3185 * \ / meaning mainline merge X is TREESAME to both
3186 * *A-*B parents.
3188 * Default log would produce "I" by following the first parent;
3189 * --full-history --simplify-merges will produce "I-A-B". But this is a
3190 * reasonable result - it presents a logical full history leading from
3191 * I to X, and X is not an important merge.
3193 if (!unmarked && marked) {
3194 marked->object.flags &= ~TMP_MARK;
3195 return 1;
3198 return 0;
3201 static int remove_marked_parents(struct rev_info *revs, struct commit *commit)
3203 struct commit_list **pp, *p;
3204 int nth_parent, removed = 0;
3206 pp = &commit->parents;
3207 nth_parent = 0;
3208 while ((p = *pp) != NULL) {
3209 struct commit *parent = p->item;
3210 if (parent->object.flags & TMP_MARK) {
3211 parent->object.flags &= ~TMP_MARK;
3212 *pp = p->next;
3213 free(p);
3214 removed++;
3215 compact_treesame(revs, commit, nth_parent);
3216 continue;
3218 pp = &p->next;
3219 nth_parent++;
3222 /* Removing parents can only increase TREESAMEness */
3223 if (removed && !(commit->object.flags & TREESAME))
3224 update_treesame(revs, commit);
3226 return nth_parent;
3229 static struct commit_list **simplify_one(struct rev_info *revs, struct commit *commit, struct commit_list **tail)
3231 struct commit_list *p;
3232 struct commit *parent;
3233 struct merge_simplify_state *st, *pst;
3234 int cnt;
3236 st = locate_simplify_state(revs, commit);
3239 * Have we handled this one?
3241 if (st->simplified)
3242 return tail;
3245 * An UNINTERESTING commit simplifies to itself, so does a
3246 * root commit. We do not rewrite parents of such commit
3247 * anyway.
3249 if ((commit->object.flags & UNINTERESTING) || !commit->parents) {
3250 st->simplified = commit;
3251 return tail;
3255 * Do we know what commit all of our parents that matter
3256 * should be rewritten to? Otherwise we are not ready to
3257 * rewrite this one yet.
3259 for (cnt = 0, p = commit->parents; p; p = p->next) {
3260 pst = locate_simplify_state(revs, p->item);
3261 if (!pst->simplified) {
3262 tail = &commit_list_insert(p->item, tail)->next;
3263 cnt++;
3265 if (revs->first_parent_only)
3266 break;
3268 if (cnt) {
3269 tail = &commit_list_insert(commit, tail)->next;
3270 return tail;
3274 * Rewrite our list of parents. Note that this cannot
3275 * affect our TREESAME flags in any way - a commit is
3276 * always TREESAME to its simplification.
3278 for (p = commit->parents; p; p = p->next) {
3279 pst = locate_simplify_state(revs, p->item);
3280 p->item = pst->simplified;
3281 if (revs->first_parent_only)
3282 break;
3285 if (revs->first_parent_only)
3286 cnt = 1;
3287 else
3288 cnt = remove_duplicate_parents(revs, commit);
3291 * It is possible that we are a merge and one side branch
3292 * does not have any commit that touches the given paths;
3293 * in such a case, the immediate parent from that branch
3294 * will be rewritten to be the merge base.
3296 * o----X X: the commit we are looking at;
3297 * / / o: a commit that touches the paths;
3298 * ---o----'
3300 * Further, a merge of an independent branch that doesn't
3301 * touch the path will reduce to a treesame root parent:
3303 * ----o----X X: the commit we are looking at;
3304 * / o: a commit that touches the paths;
3305 * r r: a root commit not touching the paths
3307 * Detect and simplify both cases.
3309 if (1 < cnt) {
3310 int marked = mark_redundant_parents(commit);
3311 marked += mark_treesame_root_parents(commit);
3312 if (marked)
3313 marked -= leave_one_treesame_to_parent(revs, commit);
3314 if (marked)
3315 cnt = remove_marked_parents(revs, commit);
3319 * A commit simplifies to itself if it is a root, if it is
3320 * UNINTERESTING, if it touches the given paths, or if it is a
3321 * merge and its parents don't simplify to one relevant commit
3322 * (the first two cases are already handled at the beginning of
3323 * this function).
3325 * Otherwise, it simplifies to what its sole relevant parent
3326 * simplifies to.
3328 if (!cnt ||
3329 (commit->object.flags & UNINTERESTING) ||
3330 !(commit->object.flags & TREESAME) ||
3331 (parent = one_relevant_parent(revs, commit->parents)) == NULL ||
3332 (revs->show_pulls && (commit->object.flags & PULL_MERGE)))
3333 st->simplified = commit;
3334 else {
3335 pst = locate_simplify_state(revs, parent);
3336 st->simplified = pst->simplified;
3338 return tail;
3341 static void simplify_merges(struct rev_info *revs)
3343 struct commit_list *list, *next;
3344 struct commit_list *yet_to_do, **tail;
3345 struct commit *commit;
3347 if (!revs->prune)
3348 return;
3350 /* feed the list reversed */
3351 yet_to_do = NULL;
3352 for (list = revs->commits; list; list = next) {
3353 commit = list->item;
3354 next = list->next;
3356 * Do not free(list) here yet; the original list
3357 * is used later in this function.
3359 commit_list_insert(commit, &yet_to_do);
3361 while (yet_to_do) {
3362 list = yet_to_do;
3363 yet_to_do = NULL;
3364 tail = &yet_to_do;
3365 while (list) {
3366 commit = pop_commit(&list);
3367 tail = simplify_one(revs, commit, tail);
3371 /* clean up the result, removing the simplified ones */
3372 list = revs->commits;
3373 revs->commits = NULL;
3374 tail = &revs->commits;
3375 while (list) {
3376 struct merge_simplify_state *st;
3378 commit = pop_commit(&list);
3379 st = locate_simplify_state(revs, commit);
3380 if (st->simplified == commit)
3381 tail = &commit_list_insert(commit, tail)->next;
3385 static void set_children(struct rev_info *revs)
3387 struct commit_list *l;
3388 for (l = revs->commits; l; l = l->next) {
3389 struct commit *commit = l->item;
3390 struct commit_list *p;
3392 for (p = commit->parents; p; p = p->next)
3393 add_child(revs, p->item, commit);
3397 void reset_revision_walk(void)
3399 clear_object_flags(SEEN | ADDED | SHOWN | TOPO_WALK_EXPLORED | TOPO_WALK_INDEGREE);
3402 static int mark_uninteresting(const struct object_id *oid,
3403 struct packed_git *pack,
3404 uint32_t pos,
3405 void *cb)
3407 struct rev_info *revs = cb;
3408 struct object *o = lookup_unknown_object(revs->repo, oid);
3409 o->flags |= UNINTERESTING | SEEN;
3410 return 0;
3413 define_commit_slab(indegree_slab, int);
3414 define_commit_slab(author_date_slab, timestamp_t);
3416 struct topo_walk_info {
3417 timestamp_t min_generation;
3418 struct prio_queue explore_queue;
3419 struct prio_queue indegree_queue;
3420 struct prio_queue topo_queue;
3421 struct indegree_slab indegree;
3422 struct author_date_slab author_date;
3425 static int topo_walk_atexit_registered;
3426 static unsigned int count_explore_walked;
3427 static unsigned int count_indegree_walked;
3428 static unsigned int count_topo_walked;
3430 static void trace2_topo_walk_statistics_atexit(void)
3432 struct json_writer jw = JSON_WRITER_INIT;
3434 jw_object_begin(&jw, 0);
3435 jw_object_intmax(&jw, "count_explore_walked", count_explore_walked);
3436 jw_object_intmax(&jw, "count_indegree_walked", count_indegree_walked);
3437 jw_object_intmax(&jw, "count_topo_walked", count_topo_walked);
3438 jw_end(&jw);
3440 trace2_data_json("topo_walk", the_repository, "statistics", &jw);
3442 jw_release(&jw);
3445 static inline void test_flag_and_insert(struct prio_queue *q, struct commit *c, int flag)
3447 if (c->object.flags & flag)
3448 return;
3450 c->object.flags |= flag;
3451 prio_queue_put(q, c);
3454 static void explore_walk_step(struct rev_info *revs)
3456 struct topo_walk_info *info = revs->topo_walk_info;
3457 struct commit_list *p;
3458 struct commit *c = prio_queue_get(&info->explore_queue);
3460 if (!c)
3461 return;
3463 if (repo_parse_commit_gently(revs->repo, c, 1) < 0)
3464 return;
3466 count_explore_walked++;
3468 if (revs->sort_order == REV_SORT_BY_AUTHOR_DATE)
3469 record_author_date(&info->author_date, c);
3471 if (revs->max_age != -1 && (c->date < revs->max_age))
3472 c->object.flags |= UNINTERESTING;
3474 if (process_parents(revs, c, NULL, NULL) < 0)
3475 return;
3477 if (c->object.flags & UNINTERESTING)
3478 mark_parents_uninteresting(revs, c);
3480 for (p = c->parents; p; p = p->next)
3481 test_flag_and_insert(&info->explore_queue, p->item, TOPO_WALK_EXPLORED);
3484 static void explore_to_depth(struct rev_info *revs,
3485 timestamp_t gen_cutoff)
3487 struct topo_walk_info *info = revs->topo_walk_info;
3488 struct commit *c;
3489 while ((c = prio_queue_peek(&info->explore_queue)) &&
3490 commit_graph_generation(c) >= gen_cutoff)
3491 explore_walk_step(revs);
3494 static void indegree_walk_step(struct rev_info *revs)
3496 struct commit_list *p;
3497 struct topo_walk_info *info = revs->topo_walk_info;
3498 struct commit *c = prio_queue_get(&info->indegree_queue);
3500 if (!c)
3501 return;
3503 if (repo_parse_commit_gently(revs->repo, c, 1) < 0)
3504 return;
3506 count_indegree_walked++;
3508 explore_to_depth(revs, commit_graph_generation(c));
3510 for (p = c->parents; p; p = p->next) {
3511 struct commit *parent = p->item;
3512 int *pi = indegree_slab_at(&info->indegree, parent);
3514 if (repo_parse_commit_gently(revs->repo, parent, 1) < 0)
3515 return;
3517 if (*pi)
3518 (*pi)++;
3519 else
3520 *pi = 2;
3522 test_flag_and_insert(&info->indegree_queue, parent, TOPO_WALK_INDEGREE);
3524 if (revs->first_parent_only)
3525 return;
3529 static void compute_indegrees_to_depth(struct rev_info *revs,
3530 timestamp_t gen_cutoff)
3532 struct topo_walk_info *info = revs->topo_walk_info;
3533 struct commit *c;
3534 while ((c = prio_queue_peek(&info->indegree_queue)) &&
3535 commit_graph_generation(c) >= gen_cutoff)
3536 indegree_walk_step(revs);
3539 static void release_revisions_topo_walk_info(struct topo_walk_info *info)
3541 if (!info)
3542 return;
3543 clear_prio_queue(&info->explore_queue);
3544 clear_prio_queue(&info->indegree_queue);
3545 clear_prio_queue(&info->topo_queue);
3546 clear_indegree_slab(&info->indegree);
3547 clear_author_date_slab(&info->author_date);
3548 free(info);
3551 static void reset_topo_walk(struct rev_info *revs)
3553 release_revisions_topo_walk_info(revs->topo_walk_info);
3554 revs->topo_walk_info = NULL;
3557 static void init_topo_walk(struct rev_info *revs)
3559 struct topo_walk_info *info;
3560 struct commit_list *list;
3561 if (revs->topo_walk_info)
3562 reset_topo_walk(revs);
3564 revs->topo_walk_info = xmalloc(sizeof(struct topo_walk_info));
3565 info = revs->topo_walk_info;
3566 memset(info, 0, sizeof(struct topo_walk_info));
3568 init_indegree_slab(&info->indegree);
3569 memset(&info->explore_queue, 0, sizeof(info->explore_queue));
3570 memset(&info->indegree_queue, 0, sizeof(info->indegree_queue));
3571 memset(&info->topo_queue, 0, sizeof(info->topo_queue));
3573 switch (revs->sort_order) {
3574 default: /* REV_SORT_IN_GRAPH_ORDER */
3575 info->topo_queue.compare = NULL;
3576 break;
3577 case REV_SORT_BY_COMMIT_DATE:
3578 info->topo_queue.compare = compare_commits_by_commit_date;
3579 break;
3580 case REV_SORT_BY_AUTHOR_DATE:
3581 init_author_date_slab(&info->author_date);
3582 info->topo_queue.compare = compare_commits_by_author_date;
3583 info->topo_queue.cb_data = &info->author_date;
3584 break;
3587 info->explore_queue.compare = compare_commits_by_gen_then_commit_date;
3588 info->indegree_queue.compare = compare_commits_by_gen_then_commit_date;
3590 info->min_generation = GENERATION_NUMBER_INFINITY;
3591 for (list = revs->commits; list; list = list->next) {
3592 struct commit *c = list->item;
3593 timestamp_t generation;
3595 if (repo_parse_commit_gently(revs->repo, c, 1))
3596 continue;
3598 test_flag_and_insert(&info->explore_queue, c, TOPO_WALK_EXPLORED);
3599 test_flag_and_insert(&info->indegree_queue, c, TOPO_WALK_INDEGREE);
3601 generation = commit_graph_generation(c);
3602 if (generation < info->min_generation)
3603 info->min_generation = generation;
3605 *(indegree_slab_at(&info->indegree, c)) = 1;
3607 if (revs->sort_order == REV_SORT_BY_AUTHOR_DATE)
3608 record_author_date(&info->author_date, c);
3610 compute_indegrees_to_depth(revs, info->min_generation);
3612 for (list = revs->commits; list; list = list->next) {
3613 struct commit *c = list->item;
3615 if (*(indegree_slab_at(&info->indegree, c)) == 1)
3616 prio_queue_put(&info->topo_queue, c);
3620 * This is unfortunate; the initial tips need to be shown
3621 * in the order given from the revision traversal machinery.
3623 if (revs->sort_order == REV_SORT_IN_GRAPH_ORDER)
3624 prio_queue_reverse(&info->topo_queue);
3626 if (trace2_is_enabled() && !topo_walk_atexit_registered) {
3627 atexit(trace2_topo_walk_statistics_atexit);
3628 topo_walk_atexit_registered = 1;
3632 static struct commit *next_topo_commit(struct rev_info *revs)
3634 struct commit *c;
3635 struct topo_walk_info *info = revs->topo_walk_info;
3637 /* pop next off of topo_queue */
3638 c = prio_queue_get(&info->topo_queue);
3640 if (c)
3641 *(indegree_slab_at(&info->indegree, c)) = 0;
3643 return c;
3646 static void expand_topo_walk(struct rev_info *revs, struct commit *commit)
3648 struct commit_list *p;
3649 struct topo_walk_info *info = revs->topo_walk_info;
3650 if (process_parents(revs, commit, NULL, NULL) < 0) {
3651 if (!revs->ignore_missing_links)
3652 die("Failed to traverse parents of commit %s",
3653 oid_to_hex(&commit->object.oid));
3656 count_topo_walked++;
3658 for (p = commit->parents; p; p = p->next) {
3659 struct commit *parent = p->item;
3660 int *pi;
3661 timestamp_t generation;
3663 if (parent->object.flags & UNINTERESTING)
3664 continue;
3666 if (repo_parse_commit_gently(revs->repo, parent, 1) < 0)
3667 continue;
3669 generation = commit_graph_generation(parent);
3670 if (generation < info->min_generation) {
3671 info->min_generation = generation;
3672 compute_indegrees_to_depth(revs, info->min_generation);
3675 pi = indegree_slab_at(&info->indegree, parent);
3677 (*pi)--;
3678 if (*pi == 1)
3679 prio_queue_put(&info->topo_queue, parent);
3681 if (revs->first_parent_only)
3682 return;
3686 int prepare_revision_walk(struct rev_info *revs)
3688 int i;
3689 struct object_array old_pending;
3690 struct commit_list **next = &revs->commits;
3692 memcpy(&old_pending, &revs->pending, sizeof(old_pending));
3693 revs->pending.nr = 0;
3694 revs->pending.alloc = 0;
3695 revs->pending.objects = NULL;
3696 for (i = 0; i < old_pending.nr; i++) {
3697 struct object_array_entry *e = old_pending.objects + i;
3698 struct commit *commit = handle_commit(revs, e);
3699 if (commit) {
3700 if (!(commit->object.flags & SEEN)) {
3701 commit->object.flags |= SEEN;
3702 next = commit_list_append(commit, next);
3706 object_array_clear(&old_pending);
3708 /* Signal whether we need per-parent treesame decoration */
3709 if (revs->simplify_merges ||
3710 (revs->limited && limiting_can_increase_treesame(revs)))
3711 revs->treesame.name = "treesame";
3713 if (revs->exclude_promisor_objects) {
3714 for_each_packed_object(mark_uninteresting, revs,
3715 FOR_EACH_OBJECT_PROMISOR_ONLY);
3718 if (!revs->reflog_info)
3719 prepare_to_use_bloom_filter(revs);
3720 if (!revs->unsorted_input)
3721 commit_list_sort_by_date(&revs->commits);
3722 if (revs->no_walk)
3723 return 0;
3724 if (revs->limited) {
3725 if (limit_list(revs) < 0)
3726 return -1;
3727 if (revs->topo_order)
3728 sort_in_topological_order(&revs->commits, revs->sort_order);
3729 } else if (revs->topo_order)
3730 init_topo_walk(revs);
3731 if (revs->line_level_traverse && want_ancestry(revs))
3733 * At the moment we can only do line-level log with parent
3734 * rewriting by performing this expensive pre-filtering step.
3735 * If parent rewriting is not requested, then we rather
3736 * perform the line-level log filtering during the regular
3737 * history traversal.
3739 line_log_filter(revs);
3740 if (revs->simplify_merges)
3741 simplify_merges(revs);
3742 if (revs->children.name)
3743 set_children(revs);
3745 return 0;
3748 static enum rewrite_result rewrite_one_1(struct rev_info *revs,
3749 struct commit **pp,
3750 struct prio_queue *queue)
3752 for (;;) {
3753 struct commit *p = *pp;
3754 if (!revs->limited)
3755 if (process_parents(revs, p, NULL, queue) < 0)
3756 return rewrite_one_error;
3757 if (p->object.flags & UNINTERESTING)
3758 return rewrite_one_ok;
3759 if (!(p->object.flags & TREESAME))
3760 return rewrite_one_ok;
3761 if (!p->parents)
3762 return rewrite_one_noparents;
3763 if (!(p = one_relevant_parent(revs, p->parents)))
3764 return rewrite_one_ok;
3765 *pp = p;
3769 static void merge_queue_into_list(struct prio_queue *q, struct commit_list **list)
3771 while (q->nr) {
3772 struct commit *item = prio_queue_peek(q);
3773 struct commit_list *p = *list;
3775 if (p && p->item->date >= item->date)
3776 list = &p->next;
3777 else {
3778 p = commit_list_insert(item, list);
3779 list = &p->next; /* skip newly added item */
3780 prio_queue_get(q); /* pop item */
3785 static enum rewrite_result rewrite_one(struct rev_info *revs, struct commit **pp)
3787 struct prio_queue queue = { compare_commits_by_commit_date };
3788 enum rewrite_result ret = rewrite_one_1(revs, pp, &queue);
3789 merge_queue_into_list(&queue, &revs->commits);
3790 clear_prio_queue(&queue);
3791 return ret;
3794 int rewrite_parents(struct rev_info *revs, struct commit *commit,
3795 rewrite_parent_fn_t rewrite_parent)
3797 struct commit_list **pp = &commit->parents;
3798 while (*pp) {
3799 struct commit_list *parent = *pp;
3800 switch (rewrite_parent(revs, &parent->item)) {
3801 case rewrite_one_ok:
3802 break;
3803 case rewrite_one_noparents:
3804 *pp = parent->next;
3805 continue;
3806 case rewrite_one_error:
3807 return -1;
3809 pp = &parent->next;
3811 remove_duplicate_parents(revs, commit);
3812 return 0;
3815 static int commit_match(struct commit *commit, struct rev_info *opt)
3817 int retval;
3818 const char *encoding;
3819 const char *message;
3820 struct strbuf buf = STRBUF_INIT;
3822 if (!opt->grep_filter.pattern_list && !opt->grep_filter.header_list)
3823 return 1;
3825 /* Prepend "fake" headers as needed */
3826 if (opt->grep_filter.use_reflog_filter) {
3827 strbuf_addstr(&buf, "reflog ");
3828 get_reflog_message(&buf, opt->reflog_info);
3829 strbuf_addch(&buf, '\n');
3833 * We grep in the user's output encoding, under the assumption that it
3834 * is the encoding they are most likely to write their grep pattern
3835 * for. In addition, it means we will match the "notes" encoding below,
3836 * so we will not end up with a buffer that has two different encodings
3837 * in it.
3839 encoding = get_log_output_encoding();
3840 message = logmsg_reencode(commit, NULL, encoding);
3842 /* Copy the commit to temporary if we are using "fake" headers */
3843 if (buf.len)
3844 strbuf_addstr(&buf, message);
3846 if (opt->grep_filter.header_list && opt->mailmap) {
3847 const char *commit_headers[] = { "author ", "committer ", NULL };
3849 if (!buf.len)
3850 strbuf_addstr(&buf, message);
3852 apply_mailmap_to_header(&buf, commit_headers, opt->mailmap);
3855 /* Append "fake" message parts as needed */
3856 if (opt->show_notes) {
3857 if (!buf.len)
3858 strbuf_addstr(&buf, message);
3859 format_display_notes(&commit->object.oid, &buf, encoding, 1);
3863 * Find either in the original commit message, or in the temporary.
3864 * Note that we cast away the constness of "message" here. It is
3865 * const because it may come from the cached commit buffer. That's OK,
3866 * because we know that it is modifiable heap memory, and that while
3867 * grep_buffer may modify it for speed, it will restore any
3868 * changes before returning.
3870 if (buf.len)
3871 retval = grep_buffer(&opt->grep_filter, buf.buf, buf.len);
3872 else
3873 retval = grep_buffer(&opt->grep_filter,
3874 (char *)message, strlen(message));
3875 strbuf_release(&buf);
3876 unuse_commit_buffer(commit, message);
3877 return retval;
3880 static inline int want_ancestry(const struct rev_info *revs)
3882 return (revs->rewrite_parents || revs->children.name);
3886 * Return a timestamp to be used for --since/--until comparisons for this
3887 * commit, based on the revision options.
3889 static timestamp_t comparison_date(const struct rev_info *revs,
3890 struct commit *commit)
3892 return revs->reflog_info ?
3893 get_reflog_timestamp(revs->reflog_info) :
3894 commit->date;
3897 enum commit_action get_commit_action(struct rev_info *revs, struct commit *commit)
3899 if (commit->object.flags & SHOWN)
3900 return commit_ignore;
3901 if (revs->unpacked && has_object_pack(&commit->object.oid))
3902 return commit_ignore;
3903 if (revs->no_kept_objects) {
3904 if (has_object_kept_pack(&commit->object.oid,
3905 revs->keep_pack_cache_flags))
3906 return commit_ignore;
3908 if (commit->object.flags & UNINTERESTING)
3909 return commit_ignore;
3910 if (revs->line_level_traverse && !want_ancestry(revs)) {
3912 * In case of line-level log with parent rewriting
3913 * prepare_revision_walk() already took care of all line-level
3914 * log filtering, and there is nothing left to do here.
3916 * If parent rewriting was not requested, then this is the
3917 * place to perform the line-level log filtering. Notably,
3918 * this check, though expensive, must come before the other,
3919 * cheaper filtering conditions, because the tracked line
3920 * ranges must be adjusted even when the commit will end up
3921 * being ignored based on other conditions.
3923 if (!line_log_process_ranges_arbitrary_commit(revs, commit))
3924 return commit_ignore;
3926 if (revs->min_age != -1 &&
3927 comparison_date(revs, commit) > revs->min_age)
3928 return commit_ignore;
3929 if (revs->max_age_as_filter != -1 &&
3930 comparison_date(revs, commit) < revs->max_age_as_filter)
3931 return commit_ignore;
3932 if (revs->min_parents || (revs->max_parents >= 0)) {
3933 int n = commit_list_count(commit->parents);
3934 if ((n < revs->min_parents) ||
3935 ((revs->max_parents >= 0) && (n > revs->max_parents)))
3936 return commit_ignore;
3938 if (!commit_match(commit, revs))
3939 return commit_ignore;
3940 if (revs->prune && revs->dense) {
3941 /* Commit without changes? */
3942 if (commit->object.flags & TREESAME) {
3943 int n;
3944 struct commit_list *p;
3945 /* drop merges unless we want parenthood */
3946 if (!want_ancestry(revs))
3947 return commit_ignore;
3949 if (revs->show_pulls && (commit->object.flags & PULL_MERGE))
3950 return commit_show;
3953 * If we want ancestry, then need to keep any merges
3954 * between relevant commits to tie together topology.
3955 * For consistency with TREESAME and simplification
3956 * use "relevant" here rather than just INTERESTING,
3957 * to treat bottom commit(s) as part of the topology.
3959 for (n = 0, p = commit->parents; p; p = p->next)
3960 if (relevant_commit(p->item))
3961 if (++n >= 2)
3962 return commit_show;
3963 return commit_ignore;
3966 return commit_show;
3969 define_commit_slab(saved_parents, struct commit_list *);
3971 #define EMPTY_PARENT_LIST ((struct commit_list *)-1)
3974 * You may only call save_parents() once per commit (this is checked
3975 * for non-root commits).
3977 static void save_parents(struct rev_info *revs, struct commit *commit)
3979 struct commit_list **pp;
3981 if (!revs->saved_parents_slab) {
3982 revs->saved_parents_slab = xmalloc(sizeof(struct saved_parents));
3983 init_saved_parents(revs->saved_parents_slab);
3986 pp = saved_parents_at(revs->saved_parents_slab, commit);
3989 * When walking with reflogs, we may visit the same commit
3990 * several times: once for each appearance in the reflog.
3992 * In this case, save_parents() will be called multiple times.
3993 * We want to keep only the first set of parents. We need to
3994 * store a sentinel value for an empty (i.e., NULL) parent
3995 * list to distinguish it from a not-yet-saved list, however.
3997 if (*pp)
3998 return;
3999 if (commit->parents)
4000 *pp = copy_commit_list(commit->parents);
4001 else
4002 *pp = EMPTY_PARENT_LIST;
4005 static void free_saved_parents(struct rev_info *revs)
4007 if (revs->saved_parents_slab)
4008 clear_saved_parents(revs->saved_parents_slab);
4011 struct commit_list *get_saved_parents(struct rev_info *revs, const struct commit *commit)
4013 struct commit_list *parents;
4015 if (!revs->saved_parents_slab)
4016 return commit->parents;
4018 parents = *saved_parents_at(revs->saved_parents_slab, commit);
4019 if (parents == EMPTY_PARENT_LIST)
4020 return NULL;
4021 return parents;
4024 enum commit_action simplify_commit(struct rev_info *revs, struct commit *commit)
4026 enum commit_action action = get_commit_action(revs, commit);
4028 if (action == commit_show &&
4029 revs->prune && revs->dense && want_ancestry(revs)) {
4031 * --full-diff on simplified parents is no good: it
4032 * will show spurious changes from the commits that
4033 * were elided. So we save the parents on the side
4034 * when --full-diff is in effect.
4036 if (revs->full_diff)
4037 save_parents(revs, commit);
4038 if (rewrite_parents(revs, commit, rewrite_one) < 0)
4039 return commit_error;
4041 return action;
4044 static void track_linear(struct rev_info *revs, struct commit *commit)
4046 if (revs->track_first_time) {
4047 revs->linear = 1;
4048 revs->track_first_time = 0;
4049 } else {
4050 struct commit_list *p;
4051 for (p = revs->previous_parents; p; p = p->next)
4052 if (p->item == NULL || /* first commit */
4053 oideq(&p->item->object.oid, &commit->object.oid))
4054 break;
4055 revs->linear = p != NULL;
4057 if (revs->reverse) {
4058 if (revs->linear)
4059 commit->object.flags |= TRACK_LINEAR;
4061 free_commit_list(revs->previous_parents);
4062 revs->previous_parents = copy_commit_list(commit->parents);
4065 static struct commit *get_revision_1(struct rev_info *revs)
4067 while (1) {
4068 struct commit *commit;
4070 if (revs->reflog_info)
4071 commit = next_reflog_entry(revs->reflog_info);
4072 else if (revs->topo_walk_info)
4073 commit = next_topo_commit(revs);
4074 else
4075 commit = pop_commit(&revs->commits);
4077 if (!commit)
4078 return NULL;
4080 if (revs->reflog_info)
4081 commit->object.flags &= ~(ADDED | SEEN | SHOWN);
4084 * If we haven't done the list limiting, we need to look at
4085 * the parents here. We also need to do the date-based limiting
4086 * that we'd otherwise have done in limit_list().
4088 if (!revs->limited) {
4089 if (revs->max_age != -1 &&
4090 comparison_date(revs, commit) < revs->max_age)
4091 continue;
4093 if (revs->reflog_info)
4094 try_to_simplify_commit(revs, commit);
4095 else if (revs->topo_walk_info)
4096 expand_topo_walk(revs, commit);
4097 else if (process_parents(revs, commit, &revs->commits, NULL) < 0) {
4098 if (!revs->ignore_missing_links)
4099 die("Failed to traverse parents of commit %s",
4100 oid_to_hex(&commit->object.oid));
4104 switch (simplify_commit(revs, commit)) {
4105 case commit_ignore:
4106 continue;
4107 case commit_error:
4108 die("Failed to simplify parents of commit %s",
4109 oid_to_hex(&commit->object.oid));
4110 default:
4111 if (revs->track_linear)
4112 track_linear(revs, commit);
4113 return commit;
4119 * Return true for entries that have not yet been shown. (This is an
4120 * object_array_each_func_t.)
4122 static int entry_unshown(struct object_array_entry *entry, void *cb_data_unused)
4124 return !(entry->item->flags & SHOWN);
4128 * If array is on the verge of a realloc, garbage-collect any entries
4129 * that have already been shown to try to free up some space.
4131 static void gc_boundary(struct object_array *array)
4133 if (array->nr == array->alloc)
4134 object_array_filter(array, entry_unshown, NULL);
4137 static void create_boundary_commit_list(struct rev_info *revs)
4139 unsigned i;
4140 struct commit *c;
4141 struct object_array *array = &revs->boundary_commits;
4142 struct object_array_entry *objects = array->objects;
4145 * If revs->commits is non-NULL at this point, an error occurred in
4146 * get_revision_1(). Ignore the error and continue printing the
4147 * boundary commits anyway. (This is what the code has always
4148 * done.)
4150 free_commit_list(revs->commits);
4151 revs->commits = NULL;
4154 * Put all of the actual boundary commits from revs->boundary_commits
4155 * into revs->commits
4157 for (i = 0; i < array->nr; i++) {
4158 c = (struct commit *)(objects[i].item);
4159 if (!c)
4160 continue;
4161 if (!(c->object.flags & CHILD_SHOWN))
4162 continue;
4163 if (c->object.flags & (SHOWN | BOUNDARY))
4164 continue;
4165 c->object.flags |= BOUNDARY;
4166 commit_list_insert(c, &revs->commits);
4170 * If revs->topo_order is set, sort the boundary commits
4171 * in topological order
4173 sort_in_topological_order(&revs->commits, revs->sort_order);
4176 static struct commit *get_revision_internal(struct rev_info *revs)
4178 struct commit *c = NULL;
4179 struct commit_list *l;
4181 if (revs->boundary == 2) {
4183 * All of the normal commits have already been returned,
4184 * and we are now returning boundary commits.
4185 * create_boundary_commit_list() has populated
4186 * revs->commits with the remaining commits to return.
4188 c = pop_commit(&revs->commits);
4189 if (c)
4190 c->object.flags |= SHOWN;
4191 return c;
4195 * If our max_count counter has reached zero, then we are done. We
4196 * don't simply return NULL because we still might need to show
4197 * boundary commits. But we want to avoid calling get_revision_1, which
4198 * might do a considerable amount of work finding the next commit only
4199 * for us to throw it away.
4201 * If it is non-zero, then either we don't have a max_count at all
4202 * (-1), or it is still counting, in which case we decrement.
4204 if (revs->max_count) {
4205 c = get_revision_1(revs);
4206 if (c) {
4207 while (revs->skip_count > 0) {
4208 revs->skip_count--;
4209 c = get_revision_1(revs);
4210 if (!c)
4211 break;
4215 if (revs->max_count > 0)
4216 revs->max_count--;
4219 if (c)
4220 c->object.flags |= SHOWN;
4222 if (!revs->boundary)
4223 return c;
4225 if (!c) {
4227 * get_revision_1() runs out the commits, and
4228 * we are done computing the boundaries.
4229 * switch to boundary commits output mode.
4231 revs->boundary = 2;
4234 * Update revs->commits to contain the list of
4235 * boundary commits.
4237 create_boundary_commit_list(revs);
4239 return get_revision_internal(revs);
4243 * boundary commits are the commits that are parents of the
4244 * ones we got from get_revision_1() but they themselves are
4245 * not returned from get_revision_1(). Before returning
4246 * 'c', we need to mark its parents that they could be boundaries.
4249 for (l = c->parents; l; l = l->next) {
4250 struct object *p;
4251 p = &(l->item->object);
4252 if (p->flags & (CHILD_SHOWN | SHOWN))
4253 continue;
4254 p->flags |= CHILD_SHOWN;
4255 gc_boundary(&revs->boundary_commits);
4256 add_object_array(p, NULL, &revs->boundary_commits);
4259 return c;
4262 struct commit *get_revision(struct rev_info *revs)
4264 struct commit *c;
4265 struct commit_list *reversed;
4267 if (revs->reverse) {
4268 reversed = NULL;
4269 while ((c = get_revision_internal(revs)))
4270 commit_list_insert(c, &reversed);
4271 revs->commits = reversed;
4272 revs->reverse = 0;
4273 revs->reverse_output_stage = 1;
4276 if (revs->reverse_output_stage) {
4277 c = pop_commit(&revs->commits);
4278 if (revs->track_linear)
4279 revs->linear = !!(c && c->object.flags & TRACK_LINEAR);
4280 return c;
4283 c = get_revision_internal(revs);
4284 if (c && revs->graph)
4285 graph_update(revs->graph, c);
4286 if (!c) {
4287 free_saved_parents(revs);
4288 free_commit_list(revs->previous_parents);
4289 revs->previous_parents = NULL;
4291 return c;
4294 const char *get_revision_mark(const struct rev_info *revs, const struct commit *commit)
4296 if (commit->object.flags & BOUNDARY)
4297 return "-";
4298 else if (commit->object.flags & UNINTERESTING)
4299 return "^";
4300 else if (commit->object.flags & PATCHSAME)
4301 return "=";
4302 else if (!revs || revs->left_right) {
4303 if (commit->object.flags & SYMMETRIC_LEFT)
4304 return "<";
4305 else
4306 return ">";
4307 } else if (revs->graph)
4308 return "*";
4309 else if (revs->cherry_mark)
4310 return "+";
4311 return "";
4314 void put_revision_mark(const struct rev_info *revs, const struct commit *commit)
4316 const char *mark = get_revision_mark(revs, commit);
4317 if (!strlen(mark))
4318 return;
4319 fputs(mark, stdout);
4320 putchar(' ');