cocci: remove 'unused.cocci'
[git.git] / revision.c
blob106ca1ce6c98f59e29af6c63731c75e9121d6b0a
1 #include "git-compat-util.h"
2 #include "alloc.h"
3 #include "config.h"
4 #include "environment.h"
5 #include "gettext.h"
6 #include "hex.h"
7 #include "object-store.h"
8 #include "tag.h"
9 #include "blob.h"
10 #include "tree.h"
11 #include "commit.h"
12 #include "diff.h"
13 #include "diff-merges.h"
14 #include "refs.h"
15 #include "revision.h"
16 #include "repository.h"
17 #include "graph.h"
18 #include "grep.h"
19 #include "reflog-walk.h"
20 #include "patch-ids.h"
21 #include "decorate.h"
22 #include "log-tree.h"
23 #include "string-list.h"
24 #include "line-log.h"
25 #include "mailmap.h"
26 #include "commit-slab.h"
27 #include "dir.h"
28 #include "cache-tree.h"
29 #include "bisect.h"
30 #include "packfile.h"
31 #include "worktree.h"
32 #include "setup.h"
33 #include "strvec.h"
34 #include "commit-reach.h"
35 #include "commit-graph.h"
36 #include "prio-queue.h"
37 #include "hashmap.h"
38 #include "utf8.h"
39 #include "bloom.h"
40 #include "json-writer.h"
41 #include "list-objects-filter-options.h"
42 #include "resolve-undo.h"
43 #include "parse-options.h"
45 volatile show_early_output_fn_t show_early_output;
47 static const char *term_bad;
48 static const char *term_good;
50 implement_shared_commit_slab(revision_sources, char *);
52 static inline int want_ancestry(const struct rev_info *revs);
54 void show_object_with_name(FILE *out, struct object *obj, const char *name)
56 fprintf(out, "%s ", oid_to_hex(&obj->oid));
57 for (const char *p = name; *p && *p != '\n'; p++)
58 fputc(*p, out);
59 fputc('\n', out);
62 static void mark_blob_uninteresting(struct blob *blob)
64 if (!blob)
65 return;
66 if (blob->object.flags & UNINTERESTING)
67 return;
68 blob->object.flags |= UNINTERESTING;
71 static void mark_tree_contents_uninteresting(struct repository *r,
72 struct tree *tree)
74 struct tree_desc desc;
75 struct name_entry entry;
77 if (parse_tree_gently(tree, 1) < 0)
78 return;
80 init_tree_desc(&desc, tree->buffer, tree->size);
81 while (tree_entry(&desc, &entry)) {
82 switch (object_type(entry.mode)) {
83 case OBJ_TREE:
84 mark_tree_uninteresting(r, lookup_tree(r, &entry.oid));
85 break;
86 case OBJ_BLOB:
87 mark_blob_uninteresting(lookup_blob(r, &entry.oid));
88 break;
89 default:
90 /* Subproject commit - not in this repository */
91 break;
96 * We don't care about the tree any more
97 * after it has been marked uninteresting.
99 free_tree_buffer(tree);
102 void mark_tree_uninteresting(struct repository *r, struct tree *tree)
104 struct object *obj;
106 if (!tree)
107 return;
109 obj = &tree->object;
110 if (obj->flags & UNINTERESTING)
111 return;
112 obj->flags |= UNINTERESTING;
113 mark_tree_contents_uninteresting(r, tree);
116 struct path_and_oids_entry {
117 struct hashmap_entry ent;
118 char *path;
119 struct oidset trees;
122 static int path_and_oids_cmp(const void *hashmap_cmp_fn_data UNUSED,
123 const struct hashmap_entry *eptr,
124 const struct hashmap_entry *entry_or_key,
125 const void *keydata UNUSED)
127 const struct path_and_oids_entry *e1, *e2;
129 e1 = container_of(eptr, const struct path_and_oids_entry, ent);
130 e2 = container_of(entry_or_key, const struct path_and_oids_entry, ent);
132 return strcmp(e1->path, e2->path);
135 static void paths_and_oids_clear(struct hashmap *map)
137 struct hashmap_iter iter;
138 struct path_and_oids_entry *entry;
140 hashmap_for_each_entry(map, &iter, entry, ent /* member name */) {
141 oidset_clear(&entry->trees);
142 free(entry->path);
145 hashmap_clear_and_free(map, struct path_and_oids_entry, ent);
148 static void paths_and_oids_insert(struct hashmap *map,
149 const char *path,
150 const struct object_id *oid)
152 int hash = strhash(path);
153 struct path_and_oids_entry key;
154 struct path_and_oids_entry *entry;
156 hashmap_entry_init(&key.ent, hash);
158 /* use a shallow copy for the lookup */
159 key.path = (char *)path;
160 oidset_init(&key.trees, 0);
162 entry = hashmap_get_entry(map, &key, ent, NULL);
163 if (!entry) {
164 CALLOC_ARRAY(entry, 1);
165 hashmap_entry_init(&entry->ent, hash);
166 entry->path = xstrdup(key.path);
167 oidset_init(&entry->trees, 16);
168 hashmap_put(map, &entry->ent);
171 oidset_insert(&entry->trees, oid);
174 static void add_children_by_path(struct repository *r,
175 struct tree *tree,
176 struct hashmap *map)
178 struct tree_desc desc;
179 struct name_entry entry;
181 if (!tree)
182 return;
184 if (parse_tree_gently(tree, 1) < 0)
185 return;
187 init_tree_desc(&desc, tree->buffer, tree->size);
188 while (tree_entry(&desc, &entry)) {
189 switch (object_type(entry.mode)) {
190 case OBJ_TREE:
191 paths_and_oids_insert(map, entry.path, &entry.oid);
193 if (tree->object.flags & UNINTERESTING) {
194 struct tree *child = lookup_tree(r, &entry.oid);
195 if (child)
196 child->object.flags |= UNINTERESTING;
198 break;
199 case OBJ_BLOB:
200 if (tree->object.flags & UNINTERESTING) {
201 struct blob *child = lookup_blob(r, &entry.oid);
202 if (child)
203 child->object.flags |= UNINTERESTING;
205 break;
206 default:
207 /* Subproject commit - not in this repository */
208 break;
212 free_tree_buffer(tree);
215 void mark_trees_uninteresting_sparse(struct repository *r,
216 struct oidset *trees)
218 unsigned has_interesting = 0, has_uninteresting = 0;
219 struct hashmap map = HASHMAP_INIT(path_and_oids_cmp, NULL);
220 struct hashmap_iter map_iter;
221 struct path_and_oids_entry *entry;
222 struct object_id *oid;
223 struct oidset_iter iter;
225 oidset_iter_init(trees, &iter);
226 while ((!has_interesting || !has_uninteresting) &&
227 (oid = oidset_iter_next(&iter))) {
228 struct tree *tree = lookup_tree(r, oid);
230 if (!tree)
231 continue;
233 if (tree->object.flags & UNINTERESTING)
234 has_uninteresting = 1;
235 else
236 has_interesting = 1;
239 /* Do not walk unless we have both types of trees. */
240 if (!has_uninteresting || !has_interesting)
241 return;
243 oidset_iter_init(trees, &iter);
244 while ((oid = oidset_iter_next(&iter))) {
245 struct tree *tree = lookup_tree(r, oid);
246 add_children_by_path(r, tree, &map);
249 hashmap_for_each_entry(&map, &map_iter, entry, ent /* member name */)
250 mark_trees_uninteresting_sparse(r, &entry->trees);
252 paths_and_oids_clear(&map);
255 struct commit_stack {
256 struct commit **items;
257 size_t nr, alloc;
259 #define COMMIT_STACK_INIT { 0 }
261 static void commit_stack_push(struct commit_stack *stack, struct commit *commit)
263 ALLOC_GROW(stack->items, stack->nr + 1, stack->alloc);
264 stack->items[stack->nr++] = commit;
267 static struct commit *commit_stack_pop(struct commit_stack *stack)
269 return stack->nr ? stack->items[--stack->nr] : NULL;
272 static void commit_stack_clear(struct commit_stack *stack)
274 FREE_AND_NULL(stack->items);
275 stack->nr = stack->alloc = 0;
278 static void mark_one_parent_uninteresting(struct rev_info *revs, struct commit *commit,
279 struct commit_stack *pending)
281 struct commit_list *l;
283 if (commit->object.flags & UNINTERESTING)
284 return;
285 commit->object.flags |= UNINTERESTING;
288 * Normally we haven't parsed the parent
289 * yet, so we won't have a parent of a parent
290 * here. However, it may turn out that we've
291 * reached this commit some other way (where it
292 * wasn't uninteresting), in which case we need
293 * to mark its parents recursively too..
295 for (l = commit->parents; l; l = l->next) {
296 commit_stack_push(pending, l->item);
297 if (revs && revs->exclude_first_parent_only)
298 break;
302 void mark_parents_uninteresting(struct rev_info *revs, struct commit *commit)
304 struct commit_stack pending = COMMIT_STACK_INIT;
305 struct commit_list *l;
307 for (l = commit->parents; l; l = l->next) {
308 mark_one_parent_uninteresting(revs, l->item, &pending);
309 if (revs && revs->exclude_first_parent_only)
310 break;
313 while (pending.nr > 0)
314 mark_one_parent_uninteresting(revs, commit_stack_pop(&pending),
315 &pending);
317 commit_stack_clear(&pending);
320 static void add_pending_object_with_path(struct rev_info *revs,
321 struct object *obj,
322 const char *name, unsigned mode,
323 const char *path)
325 struct interpret_branch_name_options options = { 0 };
326 if (!obj)
327 return;
328 if (revs->no_walk && (obj->flags & UNINTERESTING))
329 revs->no_walk = 0;
330 if (revs->reflog_info && obj->type == OBJ_COMMIT) {
331 struct strbuf buf = STRBUF_INIT;
332 size_t namelen = strlen(name);
333 int len = repo_interpret_branch_name(the_repository, name,
334 namelen, &buf, &options);
336 if (0 < len && len < namelen && buf.len)
337 strbuf_addstr(&buf, name + len);
338 add_reflog_for_walk(revs->reflog_info,
339 (struct commit *)obj,
340 buf.buf[0] ? buf.buf: name);
341 strbuf_release(&buf);
342 return; /* do not add the commit itself */
344 add_object_array_with_path(obj, name, &revs->pending, mode, path);
347 static void add_pending_object_with_mode(struct rev_info *revs,
348 struct object *obj,
349 const char *name, unsigned mode)
351 add_pending_object_with_path(revs, obj, name, mode, NULL);
354 void add_pending_object(struct rev_info *revs,
355 struct object *obj, const char *name)
357 add_pending_object_with_mode(revs, obj, name, S_IFINVALID);
360 void add_head_to_pending(struct rev_info *revs)
362 struct object_id oid;
363 struct object *obj;
364 if (repo_get_oid(the_repository, "HEAD", &oid))
365 return;
366 obj = parse_object(revs->repo, &oid);
367 if (!obj)
368 return;
369 add_pending_object(revs, obj, "HEAD");
372 static struct object *get_reference(struct rev_info *revs, const char *name,
373 const struct object_id *oid,
374 unsigned int flags)
376 struct object *object;
378 object = parse_object_with_flags(revs->repo, oid,
379 revs->verify_objects ? 0 :
380 PARSE_OBJECT_SKIP_HASH_CHECK);
382 if (!object) {
383 if (revs->ignore_missing)
384 return object;
385 if (revs->exclude_promisor_objects && is_promisor_object(oid))
386 return NULL;
387 die("bad object %s", name);
389 object->flags |= flags;
390 return object;
393 void add_pending_oid(struct rev_info *revs, const char *name,
394 const struct object_id *oid, unsigned int flags)
396 struct object *object = get_reference(revs, name, oid, flags);
397 add_pending_object(revs, object, name);
400 static struct commit *handle_commit(struct rev_info *revs,
401 struct object_array_entry *entry)
403 struct object *object = entry->item;
404 const char *name = entry->name;
405 const char *path = entry->path;
406 unsigned int mode = entry->mode;
407 unsigned long flags = object->flags;
410 * Tag object? Look what it points to..
412 while (object->type == OBJ_TAG) {
413 struct tag *tag = (struct tag *) object;
414 if (revs->tag_objects && !(flags & UNINTERESTING))
415 add_pending_object(revs, object, tag->tag);
416 object = parse_object(revs->repo, get_tagged_oid(tag));
417 if (!object) {
418 if (revs->ignore_missing_links || (flags & UNINTERESTING))
419 return NULL;
420 if (revs->exclude_promisor_objects &&
421 is_promisor_object(&tag->tagged->oid))
422 return NULL;
423 die("bad object %s", oid_to_hex(&tag->tagged->oid));
425 object->flags |= flags;
427 * We'll handle the tagged object by looping or dropping
428 * through to the non-tag handlers below. Do not
429 * propagate path data from the tag's pending entry.
431 path = NULL;
432 mode = 0;
436 * Commit object? Just return it, we'll do all the complex
437 * reachability crud.
439 if (object->type == OBJ_COMMIT) {
440 struct commit *commit = (struct commit *)object;
442 if (repo_parse_commit(revs->repo, commit) < 0)
443 die("unable to parse commit %s", name);
444 if (flags & UNINTERESTING) {
445 mark_parents_uninteresting(revs, commit);
447 if (!revs->topo_order || !generation_numbers_enabled(the_repository))
448 revs->limited = 1;
450 if (revs->sources) {
451 char **slot = revision_sources_at(revs->sources, commit);
453 if (!*slot)
454 *slot = xstrdup(name);
456 return commit;
460 * Tree object? Either mark it uninteresting, or add it
461 * to the list of objects to look at later..
463 if (object->type == OBJ_TREE) {
464 struct tree *tree = (struct tree *)object;
465 if (!revs->tree_objects)
466 return NULL;
467 if (flags & UNINTERESTING) {
468 mark_tree_contents_uninteresting(revs->repo, tree);
469 return NULL;
471 add_pending_object_with_path(revs, object, name, mode, path);
472 return NULL;
476 * Blob object? You know the drill by now..
478 if (object->type == OBJ_BLOB) {
479 if (!revs->blob_objects)
480 return NULL;
481 if (flags & UNINTERESTING)
482 return NULL;
483 add_pending_object_with_path(revs, object, name, mode, path);
484 return NULL;
486 die("%s is unknown object", name);
489 static int everybody_uninteresting(struct commit_list *orig,
490 struct commit **interesting_cache)
492 struct commit_list *list = orig;
494 if (*interesting_cache) {
495 struct commit *commit = *interesting_cache;
496 if (!(commit->object.flags & UNINTERESTING))
497 return 0;
500 while (list) {
501 struct commit *commit = list->item;
502 list = list->next;
503 if (commit->object.flags & UNINTERESTING)
504 continue;
506 *interesting_cache = commit;
507 return 0;
509 return 1;
513 * A definition of "relevant" commit that we can use to simplify limited graphs
514 * by eliminating side branches.
516 * A "relevant" commit is one that is !UNINTERESTING (ie we are including it
517 * in our list), or that is a specified BOTTOM commit. Then after computing
518 * a limited list, during processing we can generally ignore boundary merges
519 * coming from outside the graph, (ie from irrelevant parents), and treat
520 * those merges as if they were single-parent. TREESAME is defined to consider
521 * only relevant parents, if any. If we are TREESAME to our on-graph parents,
522 * we don't care if we were !TREESAME to non-graph parents.
524 * Treating bottom commits as relevant ensures that a limited graph's
525 * connection to the actual bottom commit is not viewed as a side branch, but
526 * treated as part of the graph. For example:
528 * ....Z...A---X---o---o---B
529 * . /
530 * W---Y
532 * When computing "A..B", the A-X connection is at least as important as
533 * Y-X, despite A being flagged UNINTERESTING.
535 * And when computing --ancestry-path "A..B", the A-X connection is more
536 * important than Y-X, despite both A and Y being flagged UNINTERESTING.
538 static inline int relevant_commit(struct commit *commit)
540 return (commit->object.flags & (UNINTERESTING | BOTTOM)) != UNINTERESTING;
544 * Return a single relevant commit from a parent list. If we are a TREESAME
545 * commit, and this selects one of our parents, then we can safely simplify to
546 * that parent.
548 static struct commit *one_relevant_parent(const struct rev_info *revs,
549 struct commit_list *orig)
551 struct commit_list *list = orig;
552 struct commit *relevant = NULL;
554 if (!orig)
555 return NULL;
558 * For 1-parent commits, or if first-parent-only, then return that
559 * first parent (even if not "relevant" by the above definition).
560 * TREESAME will have been set purely on that parent.
562 if (revs->first_parent_only || !orig->next)
563 return orig->item;
566 * For multi-parent commits, identify a sole relevant parent, if any.
567 * If we have only one relevant parent, then TREESAME will be set purely
568 * with regard to that parent, and we can simplify accordingly.
570 * If we have more than one relevant parent, or no relevant parents
571 * (and multiple irrelevant ones), then we can't select a parent here
572 * and return NULL.
574 while (list) {
575 struct commit *commit = list->item;
576 list = list->next;
577 if (relevant_commit(commit)) {
578 if (relevant)
579 return NULL;
580 relevant = commit;
583 return relevant;
587 * The goal is to get REV_TREE_NEW as the result only if the
588 * diff consists of all '+' (and no other changes), REV_TREE_OLD
589 * if the whole diff is removal of old data, and otherwise
590 * REV_TREE_DIFFERENT (of course if the trees are the same we
591 * want REV_TREE_SAME).
593 * The only time we care about the distinction is when
594 * remove_empty_trees is in effect, in which case we care only about
595 * whether the whole change is REV_TREE_NEW, or if there's another type
596 * of change. Which means we can stop the diff early in either of these
597 * cases:
599 * 1. We're not using remove_empty_trees at all.
601 * 2. We saw anything except REV_TREE_NEW.
603 #define REV_TREE_SAME 0
604 #define REV_TREE_NEW 1 /* Only new files */
605 #define REV_TREE_OLD 2 /* Only files removed */
606 #define REV_TREE_DIFFERENT 3 /* Mixed changes */
607 static int tree_difference = REV_TREE_SAME;
609 static void file_add_remove(struct diff_options *options,
610 int addremove,
611 unsigned mode UNUSED,
612 const struct object_id *oid UNUSED,
613 int oid_valid UNUSED,
614 const char *fullpath UNUSED,
615 unsigned dirty_submodule UNUSED)
617 int diff = addremove == '+' ? REV_TREE_NEW : REV_TREE_OLD;
618 struct rev_info *revs = options->change_fn_data;
620 tree_difference |= diff;
621 if (!revs->remove_empty_trees || tree_difference != REV_TREE_NEW)
622 options->flags.has_changes = 1;
625 static void file_change(struct diff_options *options,
626 unsigned old_mode UNUSED,
627 unsigned new_mode UNUSED,
628 const struct object_id *old_oid UNUSED,
629 const struct object_id *new_oid UNUSED,
630 int old_oid_valid UNUSED,
631 int new_oid_valid UNUSED,
632 const char *fullpath UNUSED,
633 unsigned old_dirty_submodule UNUSED,
634 unsigned new_dirty_submodule UNUSED)
636 tree_difference = REV_TREE_DIFFERENT;
637 options->flags.has_changes = 1;
640 static int bloom_filter_atexit_registered;
641 static unsigned int count_bloom_filter_maybe;
642 static unsigned int count_bloom_filter_definitely_not;
643 static unsigned int count_bloom_filter_false_positive;
644 static unsigned int count_bloom_filter_not_present;
646 static void trace2_bloom_filter_statistics_atexit(void)
648 struct json_writer jw = JSON_WRITER_INIT;
650 jw_object_begin(&jw, 0);
651 jw_object_intmax(&jw, "filter_not_present", count_bloom_filter_not_present);
652 jw_object_intmax(&jw, "maybe", count_bloom_filter_maybe);
653 jw_object_intmax(&jw, "definitely_not", count_bloom_filter_definitely_not);
654 jw_object_intmax(&jw, "false_positive", count_bloom_filter_false_positive);
655 jw_end(&jw);
657 trace2_data_json("bloom", the_repository, "statistics", &jw);
659 jw_release(&jw);
662 static int forbid_bloom_filters(struct pathspec *spec)
664 if (spec->has_wildcard)
665 return 1;
666 if (spec->nr > 1)
667 return 1;
668 if (spec->magic & ~PATHSPEC_LITERAL)
669 return 1;
670 if (spec->nr && (spec->items[0].magic & ~PATHSPEC_LITERAL))
671 return 1;
673 return 0;
676 static void prepare_to_use_bloom_filter(struct rev_info *revs)
678 struct pathspec_item *pi;
679 char *path_alloc = NULL;
680 const char *path, *p;
681 size_t len;
682 int path_component_nr = 1;
684 if (!revs->commits)
685 return;
687 if (forbid_bloom_filters(&revs->prune_data))
688 return;
690 repo_parse_commit(revs->repo, revs->commits->item);
692 revs->bloom_filter_settings = get_bloom_filter_settings(revs->repo);
693 if (!revs->bloom_filter_settings)
694 return;
696 if (!revs->pruning.pathspec.nr)
697 return;
699 pi = &revs->pruning.pathspec.items[0];
701 /* remove single trailing slash from path, if needed */
702 if (pi->len > 0 && pi->match[pi->len - 1] == '/') {
703 path_alloc = xmemdupz(pi->match, pi->len - 1);
704 path = path_alloc;
705 } else
706 path = pi->match;
708 len = strlen(path);
709 if (!len) {
710 revs->bloom_filter_settings = NULL;
711 free(path_alloc);
712 return;
715 p = path;
716 while (*p) {
718 * At this point, the path is normalized to use Unix-style
719 * path separators. This is required due to how the
720 * changed-path Bloom filters store the paths.
722 if (*p == '/')
723 path_component_nr++;
724 p++;
727 revs->bloom_keys_nr = path_component_nr;
728 ALLOC_ARRAY(revs->bloom_keys, revs->bloom_keys_nr);
730 fill_bloom_key(path, len, &revs->bloom_keys[0],
731 revs->bloom_filter_settings);
732 path_component_nr = 1;
734 p = path + len - 1;
735 while (p > path) {
736 if (*p == '/')
737 fill_bloom_key(path, p - path,
738 &revs->bloom_keys[path_component_nr++],
739 revs->bloom_filter_settings);
740 p--;
743 if (trace2_is_enabled() && !bloom_filter_atexit_registered) {
744 atexit(trace2_bloom_filter_statistics_atexit);
745 bloom_filter_atexit_registered = 1;
748 free(path_alloc);
751 static int check_maybe_different_in_bloom_filter(struct rev_info *revs,
752 struct commit *commit)
754 struct bloom_filter *filter;
755 int result = 1, j;
757 if (!revs->repo->objects->commit_graph)
758 return -1;
760 if (commit_graph_generation(commit) == GENERATION_NUMBER_INFINITY)
761 return -1;
763 filter = get_bloom_filter(revs->repo, commit);
765 if (!filter) {
766 count_bloom_filter_not_present++;
767 return -1;
770 for (j = 0; result && j < revs->bloom_keys_nr; j++) {
771 result = bloom_filter_contains(filter,
772 &revs->bloom_keys[j],
773 revs->bloom_filter_settings);
776 if (result)
777 count_bloom_filter_maybe++;
778 else
779 count_bloom_filter_definitely_not++;
781 return result;
784 static int rev_compare_tree(struct rev_info *revs,
785 struct commit *parent, struct commit *commit, int nth_parent)
787 struct tree *t1 = repo_get_commit_tree(the_repository, parent);
788 struct tree *t2 = repo_get_commit_tree(the_repository, commit);
789 int bloom_ret = 1;
791 if (!t1)
792 return REV_TREE_NEW;
793 if (!t2)
794 return REV_TREE_OLD;
796 if (revs->simplify_by_decoration) {
798 * If we are simplifying by decoration, then the commit
799 * is worth showing if it has a tag pointing at it.
801 if (get_name_decoration(&commit->object))
802 return REV_TREE_DIFFERENT;
804 * A commit that is not pointed by a tag is uninteresting
805 * if we are not limited by path. This means that you will
806 * see the usual "commits that touch the paths" plus any
807 * tagged commit by specifying both --simplify-by-decoration
808 * and pathspec.
810 if (!revs->prune_data.nr)
811 return REV_TREE_SAME;
814 if (revs->bloom_keys_nr && !nth_parent) {
815 bloom_ret = check_maybe_different_in_bloom_filter(revs, commit);
817 if (bloom_ret == 0)
818 return REV_TREE_SAME;
821 tree_difference = REV_TREE_SAME;
822 revs->pruning.flags.has_changes = 0;
823 diff_tree_oid(&t1->object.oid, &t2->object.oid, "", &revs->pruning);
825 if (!nth_parent)
826 if (bloom_ret == 1 && tree_difference == REV_TREE_SAME)
827 count_bloom_filter_false_positive++;
829 return tree_difference;
832 static int rev_same_tree_as_empty(struct rev_info *revs, struct commit *commit)
834 struct tree *t1 = repo_get_commit_tree(the_repository, commit);
836 if (!t1)
837 return 0;
839 tree_difference = REV_TREE_SAME;
840 revs->pruning.flags.has_changes = 0;
841 diff_tree_oid(NULL, &t1->object.oid, "", &revs->pruning);
843 return tree_difference == REV_TREE_SAME;
846 struct treesame_state {
847 unsigned int nparents;
848 unsigned char treesame[FLEX_ARRAY];
851 static struct treesame_state *initialise_treesame(struct rev_info *revs, struct commit *commit)
853 unsigned n = commit_list_count(commit->parents);
854 struct treesame_state *st = xcalloc(1, st_add(sizeof(*st), n));
855 st->nparents = n;
856 add_decoration(&revs->treesame, &commit->object, st);
857 return st;
861 * Must be called immediately after removing the nth_parent from a commit's
862 * parent list, if we are maintaining the per-parent treesame[] decoration.
863 * This does not recalculate the master TREESAME flag - update_treesame()
864 * should be called to update it after a sequence of treesame[] modifications
865 * that may have affected it.
867 static int compact_treesame(struct rev_info *revs, struct commit *commit, unsigned nth_parent)
869 struct treesame_state *st;
870 int old_same;
872 if (!commit->parents) {
874 * Have just removed the only parent from a non-merge.
875 * Different handling, as we lack decoration.
877 if (nth_parent != 0)
878 die("compact_treesame %u", nth_parent);
879 old_same = !!(commit->object.flags & TREESAME);
880 if (rev_same_tree_as_empty(revs, commit))
881 commit->object.flags |= TREESAME;
882 else
883 commit->object.flags &= ~TREESAME;
884 return old_same;
887 st = lookup_decoration(&revs->treesame, &commit->object);
888 if (!st || nth_parent >= st->nparents)
889 die("compact_treesame %u", nth_parent);
891 old_same = st->treesame[nth_parent];
892 memmove(st->treesame + nth_parent,
893 st->treesame + nth_parent + 1,
894 st->nparents - nth_parent - 1);
897 * If we've just become a non-merge commit, update TREESAME
898 * immediately, and remove the no-longer-needed decoration.
899 * If still a merge, defer update until update_treesame().
901 if (--st->nparents == 1) {
902 if (commit->parents->next)
903 die("compact_treesame parents mismatch");
904 if (st->treesame[0] && revs->dense)
905 commit->object.flags |= TREESAME;
906 else
907 commit->object.flags &= ~TREESAME;
908 free(add_decoration(&revs->treesame, &commit->object, NULL));
911 return old_same;
914 static unsigned update_treesame(struct rev_info *revs, struct commit *commit)
916 if (commit->parents && commit->parents->next) {
917 unsigned n;
918 struct treesame_state *st;
919 struct commit_list *p;
920 unsigned relevant_parents;
921 unsigned relevant_change, irrelevant_change;
923 st = lookup_decoration(&revs->treesame, &commit->object);
924 if (!st)
925 die("update_treesame %s", oid_to_hex(&commit->object.oid));
926 relevant_parents = 0;
927 relevant_change = irrelevant_change = 0;
928 for (p = commit->parents, n = 0; p; n++, p = p->next) {
929 if (relevant_commit(p->item)) {
930 relevant_change |= !st->treesame[n];
931 relevant_parents++;
932 } else
933 irrelevant_change |= !st->treesame[n];
935 if (relevant_parents ? relevant_change : irrelevant_change)
936 commit->object.flags &= ~TREESAME;
937 else
938 commit->object.flags |= TREESAME;
941 return commit->object.flags & TREESAME;
944 static inline int limiting_can_increase_treesame(const struct rev_info *revs)
947 * TREESAME is irrelevant unless prune && dense;
948 * if simplify_history is set, we can't have a mixture of TREESAME and
949 * !TREESAME INTERESTING parents (and we don't have treesame[]
950 * decoration anyway);
951 * if first_parent_only is set, then the TREESAME flag is locked
952 * against the first parent (and again we lack treesame[] decoration).
954 return revs->prune && revs->dense &&
955 !revs->simplify_history &&
956 !revs->first_parent_only;
959 static void try_to_simplify_commit(struct rev_info *revs, struct commit *commit)
961 struct commit_list **pp, *parent;
962 struct treesame_state *ts = NULL;
963 int relevant_change = 0, irrelevant_change = 0;
964 int relevant_parents, nth_parent;
967 * If we don't do pruning, everything is interesting
969 if (!revs->prune)
970 return;
972 if (!repo_get_commit_tree(the_repository, commit))
973 return;
975 if (!commit->parents) {
976 if (rev_same_tree_as_empty(revs, commit))
977 commit->object.flags |= TREESAME;
978 return;
982 * Normal non-merge commit? If we don't want to make the
983 * history dense, we consider it always to be a change..
985 if (!revs->dense && !commit->parents->next)
986 return;
988 for (pp = &commit->parents, nth_parent = 0, relevant_parents = 0;
989 (parent = *pp) != NULL;
990 pp = &parent->next, nth_parent++) {
991 struct commit *p = parent->item;
992 if (relevant_commit(p))
993 relevant_parents++;
995 if (nth_parent == 1) {
997 * This our second loop iteration - so we now know
998 * we're dealing with a merge.
1000 * Do not compare with later parents when we care only about
1001 * the first parent chain, in order to avoid derailing the
1002 * traversal to follow a side branch that brought everything
1003 * in the path we are limited to by the pathspec.
1005 if (revs->first_parent_only)
1006 break;
1008 * If this will remain a potentially-simplifiable
1009 * merge, remember per-parent treesame if needed.
1010 * Initialise the array with the comparison from our
1011 * first iteration.
1013 if (revs->treesame.name &&
1014 !revs->simplify_history &&
1015 !(commit->object.flags & UNINTERESTING)) {
1016 ts = initialise_treesame(revs, commit);
1017 if (!(irrelevant_change || relevant_change))
1018 ts->treesame[0] = 1;
1021 if (repo_parse_commit(revs->repo, p) < 0)
1022 die("cannot simplify commit %s (because of %s)",
1023 oid_to_hex(&commit->object.oid),
1024 oid_to_hex(&p->object.oid));
1025 switch (rev_compare_tree(revs, p, commit, nth_parent)) {
1026 case REV_TREE_SAME:
1027 if (!revs->simplify_history || !relevant_commit(p)) {
1028 /* Even if a merge with an uninteresting
1029 * side branch brought the entire change
1030 * we are interested in, we do not want
1031 * to lose the other branches of this
1032 * merge, so we just keep going.
1034 if (ts)
1035 ts->treesame[nth_parent] = 1;
1036 continue;
1038 parent->next = NULL;
1039 commit->parents = parent;
1042 * A merge commit is a "diversion" if it is not
1043 * TREESAME to its first parent but is TREESAME
1044 * to a later parent. In the simplified history,
1045 * we "divert" the history walk to the later
1046 * parent. These commits are shown when "show_pulls"
1047 * is enabled, so do not mark the object as
1048 * TREESAME here.
1050 if (!revs->show_pulls || !nth_parent)
1051 commit->object.flags |= TREESAME;
1053 return;
1055 case REV_TREE_NEW:
1056 if (revs->remove_empty_trees &&
1057 rev_same_tree_as_empty(revs, p)) {
1058 /* We are adding all the specified
1059 * paths from this parent, so the
1060 * history beyond this parent is not
1061 * interesting. Remove its parents
1062 * (they are grandparents for us).
1063 * IOW, we pretend this parent is a
1064 * "root" commit.
1066 if (repo_parse_commit(revs->repo, p) < 0)
1067 die("cannot simplify commit %s (invalid %s)",
1068 oid_to_hex(&commit->object.oid),
1069 oid_to_hex(&p->object.oid));
1070 p->parents = NULL;
1072 /* fallthrough */
1073 case REV_TREE_OLD:
1074 case REV_TREE_DIFFERENT:
1075 if (relevant_commit(p))
1076 relevant_change = 1;
1077 else
1078 irrelevant_change = 1;
1080 if (!nth_parent)
1081 commit->object.flags |= PULL_MERGE;
1083 continue;
1085 die("bad tree compare for commit %s", oid_to_hex(&commit->object.oid));
1089 * TREESAME is straightforward for single-parent commits. For merge
1090 * commits, it is most useful to define it so that "irrelevant"
1091 * parents cannot make us !TREESAME - if we have any relevant
1092 * parents, then we only consider TREESAMEness with respect to them,
1093 * allowing irrelevant merges from uninteresting branches to be
1094 * simplified away. Only if we have only irrelevant parents do we
1095 * base TREESAME on them. Note that this logic is replicated in
1096 * update_treesame, which should be kept in sync.
1098 if (relevant_parents ? !relevant_change : !irrelevant_change)
1099 commit->object.flags |= TREESAME;
1102 static int process_parents(struct rev_info *revs, struct commit *commit,
1103 struct commit_list **list, struct prio_queue *queue)
1105 struct commit_list *parent = commit->parents;
1106 unsigned pass_flags;
1108 if (commit->object.flags & ADDED)
1109 return 0;
1110 commit->object.flags |= ADDED;
1112 if (revs->include_check &&
1113 !revs->include_check(commit, revs->include_check_data))
1114 return 0;
1117 * If the commit is uninteresting, don't try to
1118 * prune parents - we want the maximal uninteresting
1119 * set.
1121 * Normally we haven't parsed the parent
1122 * yet, so we won't have a parent of a parent
1123 * here. However, it may turn out that we've
1124 * reached this commit some other way (where it
1125 * wasn't uninteresting), in which case we need
1126 * to mark its parents recursively too..
1128 if (commit->object.flags & UNINTERESTING) {
1129 while (parent) {
1130 struct commit *p = parent->item;
1131 parent = parent->next;
1132 if (p)
1133 p->object.flags |= UNINTERESTING;
1134 if (repo_parse_commit_gently(revs->repo, p, 1) < 0)
1135 continue;
1136 if (p->parents)
1137 mark_parents_uninteresting(revs, p);
1138 if (p->object.flags & SEEN)
1139 continue;
1140 p->object.flags |= (SEEN | NOT_USER_GIVEN);
1141 if (list)
1142 commit_list_insert_by_date(p, list);
1143 if (queue)
1144 prio_queue_put(queue, p);
1145 if (revs->exclude_first_parent_only)
1146 break;
1148 return 0;
1152 * Ok, the commit wasn't uninteresting. Try to
1153 * simplify the commit history and find the parent
1154 * that has no differences in the path set if one exists.
1156 try_to_simplify_commit(revs, commit);
1158 if (revs->no_walk)
1159 return 0;
1161 pass_flags = (commit->object.flags & (SYMMETRIC_LEFT | ANCESTRY_PATH));
1163 for (parent = commit->parents; parent; parent = parent->next) {
1164 struct commit *p = parent->item;
1165 int gently = revs->ignore_missing_links ||
1166 revs->exclude_promisor_objects;
1167 if (repo_parse_commit_gently(revs->repo, p, gently) < 0) {
1168 if (revs->exclude_promisor_objects &&
1169 is_promisor_object(&p->object.oid)) {
1170 if (revs->first_parent_only)
1171 break;
1172 continue;
1174 return -1;
1176 if (revs->sources) {
1177 char **slot = revision_sources_at(revs->sources, p);
1179 if (!*slot)
1180 *slot = *revision_sources_at(revs->sources, commit);
1182 p->object.flags |= pass_flags;
1183 if (!(p->object.flags & SEEN)) {
1184 p->object.flags |= (SEEN | NOT_USER_GIVEN);
1185 if (list)
1186 commit_list_insert_by_date(p, list);
1187 if (queue)
1188 prio_queue_put(queue, p);
1190 if (revs->first_parent_only)
1191 break;
1193 return 0;
1196 static void cherry_pick_list(struct commit_list *list, struct rev_info *revs)
1198 struct commit_list *p;
1199 int left_count = 0, right_count = 0;
1200 int left_first;
1201 struct patch_ids ids;
1202 unsigned cherry_flag;
1204 /* First count the commits on the left and on the right */
1205 for (p = list; p; p = p->next) {
1206 struct commit *commit = p->item;
1207 unsigned flags = commit->object.flags;
1208 if (flags & BOUNDARY)
1210 else if (flags & SYMMETRIC_LEFT)
1211 left_count++;
1212 else
1213 right_count++;
1216 if (!left_count || !right_count)
1217 return;
1219 left_first = left_count < right_count;
1220 init_patch_ids(revs->repo, &ids);
1221 ids.diffopts.pathspec = revs->diffopt.pathspec;
1223 /* Compute patch-ids for one side */
1224 for (p = list; p; p = p->next) {
1225 struct commit *commit = p->item;
1226 unsigned flags = commit->object.flags;
1228 if (flags & BOUNDARY)
1229 continue;
1231 * If we have fewer left, left_first is set and we omit
1232 * commits on the right branch in this loop. If we have
1233 * fewer right, we skip the left ones.
1235 if (left_first != !!(flags & SYMMETRIC_LEFT))
1236 continue;
1237 add_commit_patch_id(commit, &ids);
1240 /* either cherry_mark or cherry_pick are true */
1241 cherry_flag = revs->cherry_mark ? PATCHSAME : SHOWN;
1243 /* Check the other side */
1244 for (p = list; p; p = p->next) {
1245 struct commit *commit = p->item;
1246 struct patch_id *id;
1247 unsigned flags = commit->object.flags;
1249 if (flags & BOUNDARY)
1250 continue;
1252 * If we have fewer left, left_first is set and we omit
1253 * commits on the left branch in this loop.
1255 if (left_first == !!(flags & SYMMETRIC_LEFT))
1256 continue;
1259 * Have we seen the same patch id?
1261 id = patch_id_iter_first(commit, &ids);
1262 if (!id)
1263 continue;
1265 commit->object.flags |= cherry_flag;
1266 do {
1267 id->commit->object.flags |= cherry_flag;
1268 } while ((id = patch_id_iter_next(id, &ids)));
1271 free_patch_ids(&ids);
1274 /* How many extra uninteresting commits we want to see.. */
1275 #define SLOP 5
1277 static int still_interesting(struct commit_list *src, timestamp_t date, int slop,
1278 struct commit **interesting_cache)
1281 * No source list at all? We're definitely done..
1283 if (!src)
1284 return 0;
1287 * Does the destination list contain entries with a date
1288 * before the source list? Definitely _not_ done.
1290 if (date <= src->item->date)
1291 return SLOP;
1294 * Does the source list still have interesting commits in
1295 * it? Definitely not done..
1297 if (!everybody_uninteresting(src, interesting_cache))
1298 return SLOP;
1300 /* Ok, we're closing in.. */
1301 return slop-1;
1305 * "rev-list --ancestry-path=C_0 [--ancestry-path=C_1 ...] A..B"
1306 * computes commits that are ancestors of B but not ancestors of A but
1307 * further limits the result to those that have any of C in their
1308 * ancestry path (i.e. are either ancestors of any of C, descendants
1309 * of any of C, or are any of C). If --ancestry-path is specified with
1310 * no commit, we use all bottom commits for C.
1312 * Before this function is called, ancestors of C will have already
1313 * been marked with ANCESTRY_PATH previously.
1315 * This takes the list of bottom commits and the result of "A..B"
1316 * without --ancestry-path, and limits the latter further to the ones
1317 * that have any of C in their ancestry path. Since the ancestors of C
1318 * have already been marked (a prerequisite of this function), we just
1319 * need to mark the descendants, then exclude any commit that does not
1320 * have any of these marks.
1322 static void limit_to_ancestry(struct commit_list *bottoms, struct commit_list *list)
1324 struct commit_list *p;
1325 struct commit_list *rlist = NULL;
1326 int made_progress;
1329 * Reverse the list so that it will be likely that we would
1330 * process parents before children.
1332 for (p = list; p; p = p->next)
1333 commit_list_insert(p->item, &rlist);
1335 for (p = bottoms; p; p = p->next)
1336 p->item->object.flags |= TMP_MARK;
1339 * Mark the ones that can reach bottom commits in "list",
1340 * in a bottom-up fashion.
1342 do {
1343 made_progress = 0;
1344 for (p = rlist; p; p = p->next) {
1345 struct commit *c = p->item;
1346 struct commit_list *parents;
1347 if (c->object.flags & (TMP_MARK | UNINTERESTING))
1348 continue;
1349 for (parents = c->parents;
1350 parents;
1351 parents = parents->next) {
1352 if (!(parents->item->object.flags & TMP_MARK))
1353 continue;
1354 c->object.flags |= TMP_MARK;
1355 made_progress = 1;
1356 break;
1359 } while (made_progress);
1362 * NEEDSWORK: decide if we want to remove parents that are
1363 * not marked with TMP_MARK from commit->parents for commits
1364 * in the resulting list. We may not want to do that, though.
1368 * The ones that are not marked with either TMP_MARK or
1369 * ANCESTRY_PATH are uninteresting
1371 for (p = list; p; p = p->next) {
1372 struct commit *c = p->item;
1373 if (c->object.flags & (TMP_MARK | ANCESTRY_PATH))
1374 continue;
1375 c->object.flags |= UNINTERESTING;
1378 /* We are done with TMP_MARK and ANCESTRY_PATH */
1379 for (p = list; p; p = p->next)
1380 p->item->object.flags &= ~(TMP_MARK | ANCESTRY_PATH);
1381 for (p = bottoms; p; p = p->next)
1382 p->item->object.flags &= ~(TMP_MARK | ANCESTRY_PATH);
1383 free_commit_list(rlist);
1387 * Before walking the history, add the set of "negative" refs the
1388 * caller has asked to exclude to the bottom list.
1390 * This is used to compute "rev-list --ancestry-path A..B", as we need
1391 * to filter the result of "A..B" further to the ones that can actually
1392 * reach A.
1394 static void collect_bottom_commits(struct commit_list *list,
1395 struct commit_list **bottom)
1397 struct commit_list *elem;
1398 for (elem = list; elem; elem = elem->next)
1399 if (elem->item->object.flags & BOTTOM)
1400 commit_list_insert(elem->item, bottom);
1403 /* Assumes either left_only or right_only is set */
1404 static void limit_left_right(struct commit_list *list, struct rev_info *revs)
1406 struct commit_list *p;
1408 for (p = list; p; p = p->next) {
1409 struct commit *commit = p->item;
1411 if (revs->right_only) {
1412 if (commit->object.flags & SYMMETRIC_LEFT)
1413 commit->object.flags |= SHOWN;
1414 } else /* revs->left_only is set */
1415 if (!(commit->object.flags & SYMMETRIC_LEFT))
1416 commit->object.flags |= SHOWN;
1420 static int limit_list(struct rev_info *revs)
1422 int slop = SLOP;
1423 timestamp_t date = TIME_MAX;
1424 struct commit_list *original_list = revs->commits;
1425 struct commit_list *newlist = NULL;
1426 struct commit_list **p = &newlist;
1427 struct commit *interesting_cache = NULL;
1429 if (revs->ancestry_path_implicit_bottoms) {
1430 collect_bottom_commits(original_list,
1431 &revs->ancestry_path_bottoms);
1432 if (!revs->ancestry_path_bottoms)
1433 die("--ancestry-path given but there are no bottom commits");
1436 while (original_list) {
1437 struct commit *commit = pop_commit(&original_list);
1438 struct object *obj = &commit->object;
1439 show_early_output_fn_t show;
1441 if (commit == interesting_cache)
1442 interesting_cache = NULL;
1444 if (revs->max_age != -1 && (commit->date < revs->max_age))
1445 obj->flags |= UNINTERESTING;
1446 if (process_parents(revs, commit, &original_list, NULL) < 0)
1447 return -1;
1448 if (obj->flags & UNINTERESTING) {
1449 mark_parents_uninteresting(revs, commit);
1450 slop = still_interesting(original_list, date, slop, &interesting_cache);
1451 if (slop)
1452 continue;
1453 break;
1455 if (revs->min_age != -1 && (commit->date > revs->min_age) &&
1456 !revs->line_level_traverse)
1457 continue;
1458 if (revs->max_age_as_filter != -1 &&
1459 (commit->date < revs->max_age_as_filter) && !revs->line_level_traverse)
1460 continue;
1461 date = commit->date;
1462 p = &commit_list_insert(commit, p)->next;
1464 show = show_early_output;
1465 if (!show)
1466 continue;
1468 show(revs, newlist);
1469 show_early_output = NULL;
1471 if (revs->cherry_pick || revs->cherry_mark)
1472 cherry_pick_list(newlist, revs);
1474 if (revs->left_only || revs->right_only)
1475 limit_left_right(newlist, revs);
1477 if (revs->ancestry_path)
1478 limit_to_ancestry(revs->ancestry_path_bottoms, newlist);
1481 * Check if any commits have become TREESAME by some of their parents
1482 * becoming UNINTERESTING.
1484 if (limiting_can_increase_treesame(revs)) {
1485 struct commit_list *list = NULL;
1486 for (list = newlist; list; list = list->next) {
1487 struct commit *c = list->item;
1488 if (c->object.flags & (UNINTERESTING | TREESAME))
1489 continue;
1490 update_treesame(revs, c);
1494 free_commit_list(original_list);
1495 revs->commits = newlist;
1496 return 0;
1500 * Add an entry to refs->cmdline with the specified information.
1501 * *name is copied.
1503 static void add_rev_cmdline(struct rev_info *revs,
1504 struct object *item,
1505 const char *name,
1506 int whence,
1507 unsigned flags)
1509 struct rev_cmdline_info *info = &revs->cmdline;
1510 unsigned int nr = info->nr;
1512 ALLOC_GROW(info->rev, nr + 1, info->alloc);
1513 info->rev[nr].item = item;
1514 info->rev[nr].name = xstrdup(name);
1515 info->rev[nr].whence = whence;
1516 info->rev[nr].flags = flags;
1517 info->nr++;
1520 static void add_rev_cmdline_list(struct rev_info *revs,
1521 struct commit_list *commit_list,
1522 int whence,
1523 unsigned flags)
1525 while (commit_list) {
1526 struct object *object = &commit_list->item->object;
1527 add_rev_cmdline(revs, object, oid_to_hex(&object->oid),
1528 whence, flags);
1529 commit_list = commit_list->next;
1533 int ref_excluded(const struct ref_exclusions *exclusions, const char *path)
1535 const char *stripped_path = strip_namespace(path);
1536 struct string_list_item *item;
1538 for_each_string_list_item(item, &exclusions->excluded_refs) {
1539 if (!wildmatch(item->string, path, 0))
1540 return 1;
1543 if (ref_is_hidden(stripped_path, path, &exclusions->hidden_refs))
1544 return 1;
1546 return 0;
1549 void init_ref_exclusions(struct ref_exclusions *exclusions)
1551 struct ref_exclusions blank = REF_EXCLUSIONS_INIT;
1552 memcpy(exclusions, &blank, sizeof(*exclusions));
1555 void clear_ref_exclusions(struct ref_exclusions *exclusions)
1557 string_list_clear(&exclusions->excluded_refs, 0);
1558 string_list_clear(&exclusions->hidden_refs, 0);
1559 exclusions->hidden_refs_configured = 0;
1562 void add_ref_exclusion(struct ref_exclusions *exclusions, const char *exclude)
1564 string_list_append(&exclusions->excluded_refs, exclude);
1567 struct exclude_hidden_refs_cb {
1568 struct ref_exclusions *exclusions;
1569 const char *section;
1572 static int hide_refs_config(const char *var, const char *value, void *cb_data)
1574 struct exclude_hidden_refs_cb *cb = cb_data;
1575 cb->exclusions->hidden_refs_configured = 1;
1576 return parse_hide_refs_config(var, value, cb->section,
1577 &cb->exclusions->hidden_refs);
1580 void exclude_hidden_refs(struct ref_exclusions *exclusions, const char *section)
1582 struct exclude_hidden_refs_cb cb;
1584 if (strcmp(section, "fetch") && strcmp(section, "receive") &&
1585 strcmp(section, "uploadpack"))
1586 die(_("unsupported section for hidden refs: %s"), section);
1588 if (exclusions->hidden_refs_configured)
1589 die(_("--exclude-hidden= passed more than once"));
1591 cb.exclusions = exclusions;
1592 cb.section = section;
1594 git_config(hide_refs_config, &cb);
1597 struct all_refs_cb {
1598 int all_flags;
1599 int warned_bad_reflog;
1600 struct rev_info *all_revs;
1601 const char *name_for_errormsg;
1602 struct worktree *wt;
1605 static int handle_one_ref(const char *path, const struct object_id *oid,
1606 int flag UNUSED,
1607 void *cb_data)
1609 struct all_refs_cb *cb = cb_data;
1610 struct object *object;
1612 if (ref_excluded(&cb->all_revs->ref_excludes, path))
1613 return 0;
1615 object = get_reference(cb->all_revs, path, oid, cb->all_flags);
1616 add_rev_cmdline(cb->all_revs, object, path, REV_CMD_REF, cb->all_flags);
1617 add_pending_object(cb->all_revs, object, path);
1618 return 0;
1621 static void init_all_refs_cb(struct all_refs_cb *cb, struct rev_info *revs,
1622 unsigned flags)
1624 cb->all_revs = revs;
1625 cb->all_flags = flags;
1626 revs->rev_input_given = 1;
1627 cb->wt = NULL;
1630 static void handle_refs(struct ref_store *refs,
1631 struct rev_info *revs, unsigned flags,
1632 int (*for_each)(struct ref_store *, each_ref_fn, void *))
1634 struct all_refs_cb cb;
1636 if (!refs) {
1637 /* this could happen with uninitialized submodules */
1638 return;
1641 init_all_refs_cb(&cb, revs, flags);
1642 for_each(refs, handle_one_ref, &cb);
1645 static void handle_one_reflog_commit(struct object_id *oid, void *cb_data)
1647 struct all_refs_cb *cb = cb_data;
1648 if (!is_null_oid(oid)) {
1649 struct object *o = parse_object(cb->all_revs->repo, oid);
1650 if (o) {
1651 o->flags |= cb->all_flags;
1652 /* ??? CMDLINEFLAGS ??? */
1653 add_pending_object(cb->all_revs, o, "");
1655 else if (!cb->warned_bad_reflog) {
1656 warning("reflog of '%s' references pruned commits",
1657 cb->name_for_errormsg);
1658 cb->warned_bad_reflog = 1;
1663 static int handle_one_reflog_ent(struct object_id *ooid, struct object_id *noid,
1664 const char *email UNUSED,
1665 timestamp_t timestamp UNUSED,
1666 int tz UNUSED,
1667 const char *message UNUSED,
1668 void *cb_data)
1670 handle_one_reflog_commit(ooid, cb_data);
1671 handle_one_reflog_commit(noid, cb_data);
1672 return 0;
1675 static int handle_one_reflog(const char *refname_in_wt,
1676 const struct object_id *oid UNUSED,
1677 int flag UNUSED, void *cb_data)
1679 struct all_refs_cb *cb = cb_data;
1680 struct strbuf refname = STRBUF_INIT;
1682 cb->warned_bad_reflog = 0;
1683 strbuf_worktree_ref(cb->wt, &refname, refname_in_wt);
1684 cb->name_for_errormsg = refname.buf;
1685 refs_for_each_reflog_ent(get_main_ref_store(the_repository),
1686 refname.buf,
1687 handle_one_reflog_ent, cb_data);
1688 strbuf_release(&refname);
1689 return 0;
1692 static void add_other_reflogs_to_pending(struct all_refs_cb *cb)
1694 struct worktree **worktrees, **p;
1696 worktrees = get_worktrees();
1697 for (p = worktrees; *p; p++) {
1698 struct worktree *wt = *p;
1700 if (wt->is_current)
1701 continue;
1703 cb->wt = wt;
1704 refs_for_each_reflog(get_worktree_ref_store(wt),
1705 handle_one_reflog,
1706 cb);
1708 free_worktrees(worktrees);
1711 void add_reflogs_to_pending(struct rev_info *revs, unsigned flags)
1713 struct all_refs_cb cb;
1715 cb.all_revs = revs;
1716 cb.all_flags = flags;
1717 cb.wt = NULL;
1718 for_each_reflog(handle_one_reflog, &cb);
1720 if (!revs->single_worktree)
1721 add_other_reflogs_to_pending(&cb);
1724 static void add_cache_tree(struct cache_tree *it, struct rev_info *revs,
1725 struct strbuf *path, unsigned int flags)
1727 size_t baselen = path->len;
1728 int i;
1730 if (it->entry_count >= 0) {
1731 struct tree *tree = lookup_tree(revs->repo, &it->oid);
1732 tree->object.flags |= flags;
1733 add_pending_object_with_path(revs, &tree->object, "",
1734 040000, path->buf);
1737 for (i = 0; i < it->subtree_nr; i++) {
1738 struct cache_tree_sub *sub = it->down[i];
1739 strbuf_addf(path, "%s%s", baselen ? "/" : "", sub->name);
1740 add_cache_tree(sub->cache_tree, revs, path, flags);
1741 strbuf_setlen(path, baselen);
1746 static void add_resolve_undo_to_pending(struct index_state *istate, struct rev_info *revs)
1748 struct string_list_item *item;
1749 struct string_list *resolve_undo = istate->resolve_undo;
1751 if (!resolve_undo)
1752 return;
1754 for_each_string_list_item(item, resolve_undo) {
1755 const char *path = item->string;
1756 struct resolve_undo_info *ru = item->util;
1757 int i;
1759 if (!ru)
1760 continue;
1761 for (i = 0; i < 3; i++) {
1762 struct blob *blob;
1764 if (!ru->mode[i] || !S_ISREG(ru->mode[i]))
1765 continue;
1767 blob = lookup_blob(revs->repo, &ru->oid[i]);
1768 if (!blob) {
1769 warning(_("resolve-undo records `%s` which is missing"),
1770 oid_to_hex(&ru->oid[i]));
1771 continue;
1773 add_pending_object_with_path(revs, &blob->object, "",
1774 ru->mode[i], path);
1779 static void do_add_index_objects_to_pending(struct rev_info *revs,
1780 struct index_state *istate,
1781 unsigned int flags)
1783 int i;
1785 /* TODO: audit for interaction with sparse-index. */
1786 ensure_full_index(istate);
1787 for (i = 0; i < istate->cache_nr; i++) {
1788 struct cache_entry *ce = istate->cache[i];
1789 struct blob *blob;
1791 if (S_ISGITLINK(ce->ce_mode))
1792 continue;
1794 blob = lookup_blob(revs->repo, &ce->oid);
1795 if (!blob)
1796 die("unable to add index blob to traversal");
1797 blob->object.flags |= flags;
1798 add_pending_object_with_path(revs, &blob->object, "",
1799 ce->ce_mode, ce->name);
1802 if (istate->cache_tree) {
1803 struct strbuf path = STRBUF_INIT;
1804 add_cache_tree(istate->cache_tree, revs, &path, flags);
1805 strbuf_release(&path);
1808 add_resolve_undo_to_pending(istate, revs);
1811 void add_index_objects_to_pending(struct rev_info *revs, unsigned int flags)
1813 struct worktree **worktrees, **p;
1815 repo_read_index(revs->repo);
1816 do_add_index_objects_to_pending(revs, revs->repo->index, flags);
1818 if (revs->single_worktree)
1819 return;
1821 worktrees = get_worktrees();
1822 for (p = worktrees; *p; p++) {
1823 struct worktree *wt = *p;
1824 struct index_state istate = INDEX_STATE_INIT(revs->repo);
1826 if (wt->is_current)
1827 continue; /* current index already taken care of */
1829 if (read_index_from(&istate,
1830 worktree_git_path(wt, "index"),
1831 get_worktree_git_dir(wt)) > 0)
1832 do_add_index_objects_to_pending(revs, &istate, flags);
1833 discard_index(&istate);
1835 free_worktrees(worktrees);
1838 struct add_alternate_refs_data {
1839 struct rev_info *revs;
1840 unsigned int flags;
1843 static void add_one_alternate_ref(const struct object_id *oid,
1844 void *vdata)
1846 const char *name = ".alternate";
1847 struct add_alternate_refs_data *data = vdata;
1848 struct object *obj;
1850 obj = get_reference(data->revs, name, oid, data->flags);
1851 add_rev_cmdline(data->revs, obj, name, REV_CMD_REV, data->flags);
1852 add_pending_object(data->revs, obj, name);
1855 static void add_alternate_refs_to_pending(struct rev_info *revs,
1856 unsigned int flags)
1858 struct add_alternate_refs_data data;
1859 data.revs = revs;
1860 data.flags = flags;
1861 for_each_alternate_ref(add_one_alternate_ref, &data);
1864 static int add_parents_only(struct rev_info *revs, const char *arg_, int flags,
1865 int exclude_parent)
1867 struct object_id oid;
1868 struct object *it;
1869 struct commit *commit;
1870 struct commit_list *parents;
1871 int parent_number;
1872 const char *arg = arg_;
1874 if (*arg == '^') {
1875 flags ^= UNINTERESTING | BOTTOM;
1876 arg++;
1878 if (repo_get_oid_committish(the_repository, arg, &oid))
1879 return 0;
1880 while (1) {
1881 it = get_reference(revs, arg, &oid, 0);
1882 if (!it && revs->ignore_missing)
1883 return 0;
1884 if (it->type != OBJ_TAG)
1885 break;
1886 if (!((struct tag*)it)->tagged)
1887 return 0;
1888 oidcpy(&oid, &((struct tag*)it)->tagged->oid);
1890 if (it->type != OBJ_COMMIT)
1891 return 0;
1892 commit = (struct commit *)it;
1893 if (exclude_parent &&
1894 exclude_parent > commit_list_count(commit->parents))
1895 return 0;
1896 for (parents = commit->parents, parent_number = 1;
1897 parents;
1898 parents = parents->next, parent_number++) {
1899 if (exclude_parent && parent_number != exclude_parent)
1900 continue;
1902 it = &parents->item->object;
1903 it->flags |= flags;
1904 add_rev_cmdline(revs, it, arg_, REV_CMD_PARENTS_ONLY, flags);
1905 add_pending_object(revs, it, arg);
1907 return 1;
1910 void repo_init_revisions(struct repository *r,
1911 struct rev_info *revs,
1912 const char *prefix)
1914 struct rev_info blank = REV_INFO_INIT;
1915 memcpy(revs, &blank, sizeof(*revs));
1917 revs->repo = r;
1918 revs->pruning.repo = r;
1919 revs->pruning.add_remove = file_add_remove;
1920 revs->pruning.change = file_change;
1921 revs->pruning.change_fn_data = revs;
1922 revs->prefix = prefix;
1924 grep_init(&revs->grep_filter, revs->repo);
1925 revs->grep_filter.status_only = 1;
1927 repo_diff_setup(revs->repo, &revs->diffopt);
1928 if (prefix && !revs->diffopt.prefix) {
1929 revs->diffopt.prefix = prefix;
1930 revs->diffopt.prefix_length = strlen(prefix);
1933 init_display_notes(&revs->notes_opt);
1934 list_objects_filter_init(&revs->filter);
1935 init_ref_exclusions(&revs->ref_excludes);
1938 static void add_pending_commit_list(struct rev_info *revs,
1939 struct commit_list *commit_list,
1940 unsigned int flags)
1942 while (commit_list) {
1943 struct object *object = &commit_list->item->object;
1944 object->flags |= flags;
1945 add_pending_object(revs, object, oid_to_hex(&object->oid));
1946 commit_list = commit_list->next;
1950 static void prepare_show_merge(struct rev_info *revs)
1952 struct commit_list *bases;
1953 struct commit *head, *other;
1954 struct object_id oid;
1955 const char **prune = NULL;
1956 int i, prune_num = 1; /* counting terminating NULL */
1957 struct index_state *istate = revs->repo->index;
1959 if (repo_get_oid(the_repository, "HEAD", &oid))
1960 die("--merge without HEAD?");
1961 head = lookup_commit_or_die(&oid, "HEAD");
1962 if (repo_get_oid(the_repository, "MERGE_HEAD", &oid))
1963 die("--merge without MERGE_HEAD?");
1964 other = lookup_commit_or_die(&oid, "MERGE_HEAD");
1965 add_pending_object(revs, &head->object, "HEAD");
1966 add_pending_object(revs, &other->object, "MERGE_HEAD");
1967 bases = repo_get_merge_bases(the_repository, head, other);
1968 add_rev_cmdline_list(revs, bases, REV_CMD_MERGE_BASE, UNINTERESTING | BOTTOM);
1969 add_pending_commit_list(revs, bases, UNINTERESTING | BOTTOM);
1970 free_commit_list(bases);
1971 head->object.flags |= SYMMETRIC_LEFT;
1973 if (!istate->cache_nr)
1974 repo_read_index(revs->repo);
1975 for (i = 0; i < istate->cache_nr; i++) {
1976 const struct cache_entry *ce = istate->cache[i];
1977 if (!ce_stage(ce))
1978 continue;
1979 if (ce_path_match(istate, ce, &revs->prune_data, NULL)) {
1980 prune_num++;
1981 REALLOC_ARRAY(prune, prune_num);
1982 prune[prune_num-2] = ce->name;
1983 prune[prune_num-1] = NULL;
1985 while ((i+1 < istate->cache_nr) &&
1986 ce_same_name(ce, istate->cache[i+1]))
1987 i++;
1989 clear_pathspec(&revs->prune_data);
1990 parse_pathspec(&revs->prune_data, PATHSPEC_ALL_MAGIC & ~PATHSPEC_LITERAL,
1991 PATHSPEC_PREFER_FULL | PATHSPEC_LITERAL_PATH, "", prune);
1992 revs->limited = 1;
1995 static int dotdot_missing(const char *arg, char *dotdot,
1996 struct rev_info *revs, int symmetric)
1998 if (revs->ignore_missing)
1999 return 0;
2000 /* de-munge so we report the full argument */
2001 *dotdot = '.';
2002 die(symmetric
2003 ? "Invalid symmetric difference expression %s"
2004 : "Invalid revision range %s", arg);
2007 static int handle_dotdot_1(const char *arg, char *dotdot,
2008 struct rev_info *revs, int flags,
2009 int cant_be_filename,
2010 struct object_context *a_oc,
2011 struct object_context *b_oc)
2013 const char *a_name, *b_name;
2014 struct object_id a_oid, b_oid;
2015 struct object *a_obj, *b_obj;
2016 unsigned int a_flags, b_flags;
2017 int symmetric = 0;
2018 unsigned int flags_exclude = flags ^ (UNINTERESTING | BOTTOM);
2019 unsigned int oc_flags = GET_OID_COMMITTISH | GET_OID_RECORD_PATH;
2021 a_name = arg;
2022 if (!*a_name)
2023 a_name = "HEAD";
2025 b_name = dotdot + 2;
2026 if (*b_name == '.') {
2027 symmetric = 1;
2028 b_name++;
2030 if (!*b_name)
2031 b_name = "HEAD";
2033 if (get_oid_with_context(revs->repo, a_name, oc_flags, &a_oid, a_oc) ||
2034 get_oid_with_context(revs->repo, b_name, oc_flags, &b_oid, b_oc))
2035 return -1;
2037 if (!cant_be_filename) {
2038 *dotdot = '.';
2039 verify_non_filename(revs->prefix, arg);
2040 *dotdot = '\0';
2043 a_obj = parse_object(revs->repo, &a_oid);
2044 b_obj = parse_object(revs->repo, &b_oid);
2045 if (!a_obj || !b_obj)
2046 return dotdot_missing(arg, dotdot, revs, symmetric);
2048 if (!symmetric) {
2049 /* just A..B */
2050 b_flags = flags;
2051 a_flags = flags_exclude;
2052 } else {
2053 /* A...B -- find merge bases between the two */
2054 struct commit *a, *b;
2055 struct commit_list *exclude;
2057 a = lookup_commit_reference(revs->repo, &a_obj->oid);
2058 b = lookup_commit_reference(revs->repo, &b_obj->oid);
2059 if (!a || !b)
2060 return dotdot_missing(arg, dotdot, revs, symmetric);
2062 exclude = repo_get_merge_bases(the_repository, a, b);
2063 add_rev_cmdline_list(revs, exclude, REV_CMD_MERGE_BASE,
2064 flags_exclude);
2065 add_pending_commit_list(revs, exclude, flags_exclude);
2066 free_commit_list(exclude);
2068 b_flags = flags;
2069 a_flags = flags | SYMMETRIC_LEFT;
2072 a_obj->flags |= a_flags;
2073 b_obj->flags |= b_flags;
2074 add_rev_cmdline(revs, a_obj, a_name, REV_CMD_LEFT, a_flags);
2075 add_rev_cmdline(revs, b_obj, b_name, REV_CMD_RIGHT, b_flags);
2076 add_pending_object_with_path(revs, a_obj, a_name, a_oc->mode, a_oc->path);
2077 add_pending_object_with_path(revs, b_obj, b_name, b_oc->mode, b_oc->path);
2078 return 0;
2081 static int handle_dotdot(const char *arg,
2082 struct rev_info *revs, int flags,
2083 int cant_be_filename)
2085 struct object_context a_oc, b_oc;
2086 char *dotdot = strstr(arg, "..");
2087 int ret;
2089 if (!dotdot)
2090 return -1;
2092 memset(&a_oc, 0, sizeof(a_oc));
2093 memset(&b_oc, 0, sizeof(b_oc));
2095 *dotdot = '\0';
2096 ret = handle_dotdot_1(arg, dotdot, revs, flags, cant_be_filename,
2097 &a_oc, &b_oc);
2098 *dotdot = '.';
2100 free(a_oc.path);
2101 free(b_oc.path);
2103 return ret;
2106 static int handle_revision_arg_1(const char *arg_, struct rev_info *revs, int flags, unsigned revarg_opt)
2108 struct object_context oc;
2109 char *mark;
2110 struct object *object;
2111 struct object_id oid;
2112 int local_flags;
2113 const char *arg = arg_;
2114 int cant_be_filename = revarg_opt & REVARG_CANNOT_BE_FILENAME;
2115 unsigned get_sha1_flags = GET_OID_RECORD_PATH;
2117 flags = flags & UNINTERESTING ? flags | BOTTOM : flags & ~BOTTOM;
2119 if (!cant_be_filename && !strcmp(arg, "..")) {
2121 * Just ".."? That is not a range but the
2122 * pathspec for the parent directory.
2124 return -1;
2127 if (!handle_dotdot(arg, revs, flags, revarg_opt))
2128 return 0;
2130 mark = strstr(arg, "^@");
2131 if (mark && !mark[2]) {
2132 *mark = 0;
2133 if (add_parents_only(revs, arg, flags, 0))
2134 return 0;
2135 *mark = '^';
2137 mark = strstr(arg, "^!");
2138 if (mark && !mark[2]) {
2139 *mark = 0;
2140 if (!add_parents_only(revs, arg, flags ^ (UNINTERESTING | BOTTOM), 0))
2141 *mark = '^';
2143 mark = strstr(arg, "^-");
2144 if (mark) {
2145 int exclude_parent = 1;
2147 if (mark[2]) {
2148 if (strtol_i(mark + 2, 10, &exclude_parent) ||
2149 exclude_parent < 1)
2150 return -1;
2153 *mark = 0;
2154 if (!add_parents_only(revs, arg, flags ^ (UNINTERESTING | BOTTOM), exclude_parent))
2155 *mark = '^';
2158 local_flags = 0;
2159 if (*arg == '^') {
2160 local_flags = UNINTERESTING | BOTTOM;
2161 arg++;
2164 if (revarg_opt & REVARG_COMMITTISH)
2165 get_sha1_flags |= GET_OID_COMMITTISH;
2167 if (get_oid_with_context(revs->repo, arg, get_sha1_flags, &oid, &oc))
2168 return revs->ignore_missing ? 0 : -1;
2169 if (!cant_be_filename)
2170 verify_non_filename(revs->prefix, arg);
2171 object = get_reference(revs, arg, &oid, flags ^ local_flags);
2172 if (!object)
2173 return revs->ignore_missing ? 0 : -1;
2174 add_rev_cmdline(revs, object, arg_, REV_CMD_REV, flags ^ local_flags);
2175 add_pending_object_with_path(revs, object, arg, oc.mode, oc.path);
2176 free(oc.path);
2177 return 0;
2180 int handle_revision_arg(const char *arg, struct rev_info *revs, int flags, unsigned revarg_opt)
2182 int ret = handle_revision_arg_1(arg, revs, flags, revarg_opt);
2183 if (!ret)
2184 revs->rev_input_given = 1;
2185 return ret;
2188 static void read_pathspec_from_stdin(struct strbuf *sb,
2189 struct strvec *prune)
2191 while (strbuf_getline(sb, stdin) != EOF)
2192 strvec_push(prune, sb->buf);
2195 static void read_revisions_from_stdin(struct rev_info *revs,
2196 struct strvec *prune)
2198 struct strbuf sb;
2199 int seen_dashdash = 0;
2200 int save_warning;
2202 save_warning = warn_on_object_refname_ambiguity;
2203 warn_on_object_refname_ambiguity = 0;
2205 strbuf_init(&sb, 1000);
2206 while (strbuf_getline(&sb, stdin) != EOF) {
2207 int len = sb.len;
2208 if (!len)
2209 break;
2210 if (sb.buf[0] == '-') {
2211 if (len == 2 && sb.buf[1] == '-') {
2212 seen_dashdash = 1;
2213 break;
2215 die("options not supported in --stdin mode");
2217 if (handle_revision_arg(sb.buf, revs, 0,
2218 REVARG_CANNOT_BE_FILENAME))
2219 die("bad revision '%s'", sb.buf);
2221 if (seen_dashdash)
2222 read_pathspec_from_stdin(&sb, prune);
2224 strbuf_release(&sb);
2225 warn_on_object_refname_ambiguity = save_warning;
2228 static void add_grep(struct rev_info *revs, const char *ptn, enum grep_pat_token what)
2230 append_grep_pattern(&revs->grep_filter, ptn, "command line", 0, what);
2233 static void add_header_grep(struct rev_info *revs, enum grep_header_field field, const char *pattern)
2235 append_header_grep_pattern(&revs->grep_filter, field, pattern);
2238 static void add_message_grep(struct rev_info *revs, const char *pattern)
2240 add_grep(revs, pattern, GREP_PATTERN_BODY);
2243 static int handle_revision_opt(struct rev_info *revs, int argc, const char **argv,
2244 int *unkc, const char **unkv,
2245 const struct setup_revision_opt* opt)
2247 const char *arg = argv[0];
2248 const char *optarg = NULL;
2249 int argcount;
2250 const unsigned hexsz = the_hash_algo->hexsz;
2252 /* pseudo revision arguments */
2253 if (!strcmp(arg, "--all") || !strcmp(arg, "--branches") ||
2254 !strcmp(arg, "--tags") || !strcmp(arg, "--remotes") ||
2255 !strcmp(arg, "--reflog") || !strcmp(arg, "--not") ||
2256 !strcmp(arg, "--no-walk") || !strcmp(arg, "--do-walk") ||
2257 !strcmp(arg, "--bisect") || starts_with(arg, "--glob=") ||
2258 !strcmp(arg, "--indexed-objects") ||
2259 !strcmp(arg, "--alternate-refs") ||
2260 starts_with(arg, "--exclude=") || starts_with(arg, "--exclude-hidden=") ||
2261 starts_with(arg, "--branches=") || starts_with(arg, "--tags=") ||
2262 starts_with(arg, "--remotes=") || starts_with(arg, "--no-walk="))
2264 unkv[(*unkc)++] = arg;
2265 return 1;
2268 if ((argcount = parse_long_opt("max-count", argv, &optarg))) {
2269 revs->max_count = atoi(optarg);
2270 revs->no_walk = 0;
2271 return argcount;
2272 } else if ((argcount = parse_long_opt("skip", argv, &optarg))) {
2273 revs->skip_count = atoi(optarg);
2274 return argcount;
2275 } else if ((*arg == '-') && isdigit(arg[1])) {
2276 /* accept -<digit>, like traditional "head" */
2277 if (strtol_i(arg + 1, 10, &revs->max_count) < 0 ||
2278 revs->max_count < 0)
2279 die("'%s': not a non-negative integer", arg + 1);
2280 revs->no_walk = 0;
2281 } else if (!strcmp(arg, "-n")) {
2282 if (argc <= 1)
2283 return error("-n requires an argument");
2284 revs->max_count = atoi(argv[1]);
2285 revs->no_walk = 0;
2286 return 2;
2287 } else if (skip_prefix(arg, "-n", &optarg)) {
2288 revs->max_count = atoi(optarg);
2289 revs->no_walk = 0;
2290 } else if ((argcount = parse_long_opt("max-age", argv, &optarg))) {
2291 revs->max_age = atoi(optarg);
2292 return argcount;
2293 } else if ((argcount = parse_long_opt("since", argv, &optarg))) {
2294 revs->max_age = approxidate(optarg);
2295 return argcount;
2296 } else if ((argcount = parse_long_opt("since-as-filter", argv, &optarg))) {
2297 revs->max_age_as_filter = approxidate(optarg);
2298 return argcount;
2299 } else if ((argcount = parse_long_opt("after", argv, &optarg))) {
2300 revs->max_age = approxidate(optarg);
2301 return argcount;
2302 } else if ((argcount = parse_long_opt("min-age", argv, &optarg))) {
2303 revs->min_age = atoi(optarg);
2304 return argcount;
2305 } else if ((argcount = parse_long_opt("before", argv, &optarg))) {
2306 revs->min_age = approxidate(optarg);
2307 return argcount;
2308 } else if ((argcount = parse_long_opt("until", argv, &optarg))) {
2309 revs->min_age = approxidate(optarg);
2310 return argcount;
2311 } else if (!strcmp(arg, "--first-parent")) {
2312 revs->first_parent_only = 1;
2313 } else if (!strcmp(arg, "--exclude-first-parent-only")) {
2314 revs->exclude_first_parent_only = 1;
2315 } else if (!strcmp(arg, "--ancestry-path")) {
2316 revs->ancestry_path = 1;
2317 revs->simplify_history = 0;
2318 revs->limited = 1;
2319 revs->ancestry_path_implicit_bottoms = 1;
2320 } else if (skip_prefix(arg, "--ancestry-path=", &optarg)) {
2321 struct commit *c;
2322 struct object_id oid;
2323 const char *msg = _("could not get commit for ancestry-path argument %s");
2325 revs->ancestry_path = 1;
2326 revs->simplify_history = 0;
2327 revs->limited = 1;
2329 if (repo_get_oid_committish(revs->repo, optarg, &oid))
2330 return error(msg, optarg);
2331 get_reference(revs, optarg, &oid, ANCESTRY_PATH);
2332 c = lookup_commit_reference(revs->repo, &oid);
2333 if (!c)
2334 return error(msg, optarg);
2335 commit_list_insert(c, &revs->ancestry_path_bottoms);
2336 } else if (!strcmp(arg, "-g") || !strcmp(arg, "--walk-reflogs")) {
2337 init_reflog_walk(&revs->reflog_info);
2338 } else if (!strcmp(arg, "--default")) {
2339 if (argc <= 1)
2340 return error("bad --default argument");
2341 revs->def = argv[1];
2342 return 2;
2343 } else if (!strcmp(arg, "--merge")) {
2344 revs->show_merge = 1;
2345 } else if (!strcmp(arg, "--topo-order")) {
2346 revs->sort_order = REV_SORT_IN_GRAPH_ORDER;
2347 revs->topo_order = 1;
2348 } else if (!strcmp(arg, "--simplify-merges")) {
2349 revs->simplify_merges = 1;
2350 revs->topo_order = 1;
2351 revs->rewrite_parents = 1;
2352 revs->simplify_history = 0;
2353 revs->limited = 1;
2354 } else if (!strcmp(arg, "--simplify-by-decoration")) {
2355 revs->simplify_merges = 1;
2356 revs->topo_order = 1;
2357 revs->rewrite_parents = 1;
2358 revs->simplify_history = 0;
2359 revs->simplify_by_decoration = 1;
2360 revs->limited = 1;
2361 revs->prune = 1;
2362 } else if (!strcmp(arg, "--date-order")) {
2363 revs->sort_order = REV_SORT_BY_COMMIT_DATE;
2364 revs->topo_order = 1;
2365 } else if (!strcmp(arg, "--author-date-order")) {
2366 revs->sort_order = REV_SORT_BY_AUTHOR_DATE;
2367 revs->topo_order = 1;
2368 } else if (!strcmp(arg, "--early-output")) {
2369 revs->early_output = 100;
2370 revs->topo_order = 1;
2371 } else if (skip_prefix(arg, "--early-output=", &optarg)) {
2372 if (strtoul_ui(optarg, 10, &revs->early_output) < 0)
2373 die("'%s': not a non-negative integer", optarg);
2374 revs->topo_order = 1;
2375 } else if (!strcmp(arg, "--parents")) {
2376 revs->rewrite_parents = 1;
2377 revs->print_parents = 1;
2378 } else if (!strcmp(arg, "--dense")) {
2379 revs->dense = 1;
2380 } else if (!strcmp(arg, "--sparse")) {
2381 revs->dense = 0;
2382 } else if (!strcmp(arg, "--in-commit-order")) {
2383 revs->tree_blobs_in_commit_order = 1;
2384 } else if (!strcmp(arg, "--remove-empty")) {
2385 revs->remove_empty_trees = 1;
2386 } else if (!strcmp(arg, "--merges")) {
2387 revs->min_parents = 2;
2388 } else if (!strcmp(arg, "--no-merges")) {
2389 revs->max_parents = 1;
2390 } else if (skip_prefix(arg, "--min-parents=", &optarg)) {
2391 revs->min_parents = atoi(optarg);
2392 } else if (!strcmp(arg, "--no-min-parents")) {
2393 revs->min_parents = 0;
2394 } else if (skip_prefix(arg, "--max-parents=", &optarg)) {
2395 revs->max_parents = atoi(optarg);
2396 } else if (!strcmp(arg, "--no-max-parents")) {
2397 revs->max_parents = -1;
2398 } else if (!strcmp(arg, "--boundary")) {
2399 revs->boundary = 1;
2400 } else if (!strcmp(arg, "--left-right")) {
2401 revs->left_right = 1;
2402 } else if (!strcmp(arg, "--left-only")) {
2403 if (revs->right_only)
2404 die("--left-only is incompatible with --right-only"
2405 " or --cherry");
2406 revs->left_only = 1;
2407 } else if (!strcmp(arg, "--right-only")) {
2408 if (revs->left_only)
2409 die(_("options '%s' and '%s' cannot be used together"), "--right-only", "--left-only");
2410 revs->right_only = 1;
2411 } else if (!strcmp(arg, "--cherry")) {
2412 if (revs->left_only)
2413 die(_("options '%s' and '%s' cannot be used together"), "--cherry", "--left-only");
2414 revs->cherry_mark = 1;
2415 revs->right_only = 1;
2416 revs->max_parents = 1;
2417 revs->limited = 1;
2418 } else if (!strcmp(arg, "--count")) {
2419 revs->count = 1;
2420 } else if (!strcmp(arg, "--cherry-mark")) {
2421 if (revs->cherry_pick)
2422 die(_("options '%s' and '%s' cannot be used together"), "--cherry-mark", "--cherry-pick");
2423 revs->cherry_mark = 1;
2424 revs->limited = 1; /* needs limit_list() */
2425 } else if (!strcmp(arg, "--cherry-pick")) {
2426 if (revs->cherry_mark)
2427 die(_("options '%s' and '%s' cannot be used together"), "--cherry-pick", "--cherry-mark");
2428 revs->cherry_pick = 1;
2429 revs->limited = 1;
2430 } else if (!strcmp(arg, "--objects")) {
2431 revs->tag_objects = 1;
2432 revs->tree_objects = 1;
2433 revs->blob_objects = 1;
2434 } else if (!strcmp(arg, "--objects-edge")) {
2435 revs->tag_objects = 1;
2436 revs->tree_objects = 1;
2437 revs->blob_objects = 1;
2438 revs->edge_hint = 1;
2439 } else if (!strcmp(arg, "--objects-edge-aggressive")) {
2440 revs->tag_objects = 1;
2441 revs->tree_objects = 1;
2442 revs->blob_objects = 1;
2443 revs->edge_hint = 1;
2444 revs->edge_hint_aggressive = 1;
2445 } else if (!strcmp(arg, "--verify-objects")) {
2446 revs->tag_objects = 1;
2447 revs->tree_objects = 1;
2448 revs->blob_objects = 1;
2449 revs->verify_objects = 1;
2450 disable_commit_graph(revs->repo);
2451 } else if (!strcmp(arg, "--unpacked")) {
2452 revs->unpacked = 1;
2453 } else if (starts_with(arg, "--unpacked=")) {
2454 die(_("--unpacked=<packfile> no longer supported"));
2455 } else if (!strcmp(arg, "--no-kept-objects")) {
2456 revs->no_kept_objects = 1;
2457 revs->keep_pack_cache_flags |= IN_CORE_KEEP_PACKS;
2458 revs->keep_pack_cache_flags |= ON_DISK_KEEP_PACKS;
2459 } else if (skip_prefix(arg, "--no-kept-objects=", &optarg)) {
2460 revs->no_kept_objects = 1;
2461 if (!strcmp(optarg, "in-core"))
2462 revs->keep_pack_cache_flags |= IN_CORE_KEEP_PACKS;
2463 if (!strcmp(optarg, "on-disk"))
2464 revs->keep_pack_cache_flags |= ON_DISK_KEEP_PACKS;
2465 } else if (!strcmp(arg, "-r")) {
2466 revs->diff = 1;
2467 revs->diffopt.flags.recursive = 1;
2468 } else if (!strcmp(arg, "-t")) {
2469 revs->diff = 1;
2470 revs->diffopt.flags.recursive = 1;
2471 revs->diffopt.flags.tree_in_recursive = 1;
2472 } else if ((argcount = diff_merges_parse_opts(revs, argv))) {
2473 return argcount;
2474 } else if (!strcmp(arg, "-v")) {
2475 revs->verbose_header = 1;
2476 } else if (!strcmp(arg, "--pretty")) {
2477 revs->verbose_header = 1;
2478 revs->pretty_given = 1;
2479 get_commit_format(NULL, revs);
2480 } else if (skip_prefix(arg, "--pretty=", &optarg) ||
2481 skip_prefix(arg, "--format=", &optarg)) {
2483 * Detached form ("--pretty X" as opposed to "--pretty=X")
2484 * not allowed, since the argument is optional.
2486 revs->verbose_header = 1;
2487 revs->pretty_given = 1;
2488 get_commit_format(optarg, revs);
2489 } else if (!strcmp(arg, "--expand-tabs")) {
2490 revs->expand_tabs_in_log = 8;
2491 } else if (!strcmp(arg, "--no-expand-tabs")) {
2492 revs->expand_tabs_in_log = 0;
2493 } else if (skip_prefix(arg, "--expand-tabs=", &arg)) {
2494 int val;
2495 if (strtol_i(arg, 10, &val) < 0 || val < 0)
2496 die("'%s': not a non-negative integer", arg);
2497 revs->expand_tabs_in_log = val;
2498 } else if (!strcmp(arg, "--show-notes") || !strcmp(arg, "--notes")) {
2499 enable_default_display_notes(&revs->notes_opt, &revs->show_notes);
2500 revs->show_notes_given = 1;
2501 } else if (!strcmp(arg, "--show-signature")) {
2502 revs->show_signature = 1;
2503 } else if (!strcmp(arg, "--no-show-signature")) {
2504 revs->show_signature = 0;
2505 } else if (!strcmp(arg, "--show-linear-break")) {
2506 revs->break_bar = " ..........";
2507 revs->track_linear = 1;
2508 revs->track_first_time = 1;
2509 } else if (skip_prefix(arg, "--show-linear-break=", &optarg)) {
2510 revs->break_bar = xstrdup(optarg);
2511 revs->track_linear = 1;
2512 revs->track_first_time = 1;
2513 } else if (skip_prefix(arg, "--show-notes=", &optarg) ||
2514 skip_prefix(arg, "--notes=", &optarg)) {
2515 if (starts_with(arg, "--show-notes=") &&
2516 revs->notes_opt.use_default_notes < 0)
2517 revs->notes_opt.use_default_notes = 1;
2518 enable_ref_display_notes(&revs->notes_opt, &revs->show_notes, optarg);
2519 revs->show_notes_given = 1;
2520 } else if (!strcmp(arg, "--no-notes")) {
2521 disable_display_notes(&revs->notes_opt, &revs->show_notes);
2522 revs->show_notes_given = 1;
2523 } else if (!strcmp(arg, "--standard-notes")) {
2524 revs->show_notes_given = 1;
2525 revs->notes_opt.use_default_notes = 1;
2526 } else if (!strcmp(arg, "--no-standard-notes")) {
2527 revs->notes_opt.use_default_notes = 0;
2528 } else if (!strcmp(arg, "--oneline")) {
2529 revs->verbose_header = 1;
2530 get_commit_format("oneline", revs);
2531 revs->pretty_given = 1;
2532 revs->abbrev_commit = 1;
2533 } else if (!strcmp(arg, "--graph")) {
2534 graph_clear(revs->graph);
2535 revs->graph = graph_init(revs);
2536 } else if (!strcmp(arg, "--no-graph")) {
2537 graph_clear(revs->graph);
2538 revs->graph = NULL;
2539 } else if (!strcmp(arg, "--encode-email-headers")) {
2540 revs->encode_email_headers = 1;
2541 } else if (!strcmp(arg, "--no-encode-email-headers")) {
2542 revs->encode_email_headers = 0;
2543 } else if (!strcmp(arg, "--root")) {
2544 revs->show_root_diff = 1;
2545 } else if (!strcmp(arg, "--no-commit-id")) {
2546 revs->no_commit_id = 1;
2547 } else if (!strcmp(arg, "--always")) {
2548 revs->always_show_header = 1;
2549 } else if (!strcmp(arg, "--no-abbrev")) {
2550 revs->abbrev = 0;
2551 } else if (!strcmp(arg, "--abbrev")) {
2552 revs->abbrev = DEFAULT_ABBREV;
2553 } else if (skip_prefix(arg, "--abbrev=", &optarg)) {
2554 revs->abbrev = strtoul(optarg, NULL, 10);
2555 if (revs->abbrev < MINIMUM_ABBREV)
2556 revs->abbrev = MINIMUM_ABBREV;
2557 else if (revs->abbrev > hexsz)
2558 revs->abbrev = hexsz;
2559 } else if (!strcmp(arg, "--abbrev-commit")) {
2560 revs->abbrev_commit = 1;
2561 revs->abbrev_commit_given = 1;
2562 } else if (!strcmp(arg, "--no-abbrev-commit")) {
2563 revs->abbrev_commit = 0;
2564 } else if (!strcmp(arg, "--full-diff")) {
2565 revs->diff = 1;
2566 revs->full_diff = 1;
2567 } else if (!strcmp(arg, "--show-pulls")) {
2568 revs->show_pulls = 1;
2569 } else if (!strcmp(arg, "--full-history")) {
2570 revs->simplify_history = 0;
2571 } else if (!strcmp(arg, "--relative-date")) {
2572 revs->date_mode.type = DATE_RELATIVE;
2573 revs->date_mode_explicit = 1;
2574 } else if ((argcount = parse_long_opt("date", argv, &optarg))) {
2575 parse_date_format(optarg, &revs->date_mode);
2576 revs->date_mode_explicit = 1;
2577 return argcount;
2578 } else if (!strcmp(arg, "--log-size")) {
2579 revs->show_log_size = 1;
2582 * Grepping the commit log
2584 else if ((argcount = parse_long_opt("author", argv, &optarg))) {
2585 add_header_grep(revs, GREP_HEADER_AUTHOR, optarg);
2586 return argcount;
2587 } else if ((argcount = parse_long_opt("committer", argv, &optarg))) {
2588 add_header_grep(revs, GREP_HEADER_COMMITTER, optarg);
2589 return argcount;
2590 } else if ((argcount = parse_long_opt("grep-reflog", argv, &optarg))) {
2591 add_header_grep(revs, GREP_HEADER_REFLOG, optarg);
2592 return argcount;
2593 } else if ((argcount = parse_long_opt("grep", argv, &optarg))) {
2594 add_message_grep(revs, optarg);
2595 return argcount;
2596 } else if (!strcmp(arg, "--basic-regexp")) {
2597 revs->grep_filter.pattern_type_option = GREP_PATTERN_TYPE_BRE;
2598 } else if (!strcmp(arg, "--extended-regexp") || !strcmp(arg, "-E")) {
2599 revs->grep_filter.pattern_type_option = GREP_PATTERN_TYPE_ERE;
2600 } else if (!strcmp(arg, "--regexp-ignore-case") || !strcmp(arg, "-i")) {
2601 revs->grep_filter.ignore_case = 1;
2602 revs->diffopt.pickaxe_opts |= DIFF_PICKAXE_IGNORE_CASE;
2603 } else if (!strcmp(arg, "--fixed-strings") || !strcmp(arg, "-F")) {
2604 revs->grep_filter.pattern_type_option = GREP_PATTERN_TYPE_FIXED;
2605 } else if (!strcmp(arg, "--perl-regexp") || !strcmp(arg, "-P")) {
2606 revs->grep_filter.pattern_type_option = GREP_PATTERN_TYPE_PCRE;
2607 } else if (!strcmp(arg, "--all-match")) {
2608 revs->grep_filter.all_match = 1;
2609 } else if (!strcmp(arg, "--invert-grep")) {
2610 revs->grep_filter.no_body_match = 1;
2611 } else if ((argcount = parse_long_opt("encoding", argv, &optarg))) {
2612 if (strcmp(optarg, "none"))
2613 git_log_output_encoding = xstrdup(optarg);
2614 else
2615 git_log_output_encoding = "";
2616 return argcount;
2617 } else if (!strcmp(arg, "--reverse")) {
2618 revs->reverse ^= 1;
2619 } else if (!strcmp(arg, "--children")) {
2620 revs->children.name = "children";
2621 revs->limited = 1;
2622 } else if (!strcmp(arg, "--ignore-missing")) {
2623 revs->ignore_missing = 1;
2624 } else if (opt && opt->allow_exclude_promisor_objects &&
2625 !strcmp(arg, "--exclude-promisor-objects")) {
2626 if (fetch_if_missing)
2627 BUG("exclude_promisor_objects can only be used when fetch_if_missing is 0");
2628 revs->exclude_promisor_objects = 1;
2629 } else {
2630 int opts = diff_opt_parse(&revs->diffopt, argv, argc, revs->prefix);
2631 if (!opts)
2632 unkv[(*unkc)++] = arg;
2633 return opts;
2636 return 1;
2639 void parse_revision_opt(struct rev_info *revs, struct parse_opt_ctx_t *ctx,
2640 const struct option *options,
2641 const char * const usagestr[])
2643 int n = handle_revision_opt(revs, ctx->argc, ctx->argv,
2644 &ctx->cpidx, ctx->out, NULL);
2645 if (n <= 0) {
2646 error("unknown option `%s'", ctx->argv[0]);
2647 usage_with_options(usagestr, options);
2649 ctx->argv += n;
2650 ctx->argc -= n;
2653 void revision_opts_finish(struct rev_info *revs)
2655 if (revs->graph && revs->track_linear)
2656 die(_("options '%s' and '%s' cannot be used together"), "--show-linear-break", "--graph");
2658 if (revs->graph) {
2659 revs->topo_order = 1;
2660 revs->rewrite_parents = 1;
2664 static int for_each_bisect_ref(struct ref_store *refs, each_ref_fn fn,
2665 void *cb_data, const char *term)
2667 struct strbuf bisect_refs = STRBUF_INIT;
2668 int status;
2669 strbuf_addf(&bisect_refs, "refs/bisect/%s", term);
2670 status = refs_for_each_fullref_in(refs, bisect_refs.buf, fn, cb_data);
2671 strbuf_release(&bisect_refs);
2672 return status;
2675 static int for_each_bad_bisect_ref(struct ref_store *refs, each_ref_fn fn, void *cb_data)
2677 return for_each_bisect_ref(refs, fn, cb_data, term_bad);
2680 static int for_each_good_bisect_ref(struct ref_store *refs, each_ref_fn fn, void *cb_data)
2682 return for_each_bisect_ref(refs, fn, cb_data, term_good);
2685 static int handle_revision_pseudo_opt(struct rev_info *revs,
2686 const char **argv, int *flags)
2688 const char *arg = argv[0];
2689 const char *optarg;
2690 struct ref_store *refs;
2691 int argcount;
2693 if (revs->repo != the_repository) {
2695 * We need some something like get_submodule_worktrees()
2696 * before we can go through all worktrees of a submodule,
2697 * .e.g with adding all HEADs from --all, which is not
2698 * supported right now, so stick to single worktree.
2700 if (!revs->single_worktree)
2701 BUG("--single-worktree cannot be used together with submodule");
2703 refs = get_main_ref_store(revs->repo);
2706 * NOTE!
2708 * Commands like "git shortlog" will not accept the options below
2709 * unless parse_revision_opt queues them (as opposed to erroring
2710 * out).
2712 * When implementing your new pseudo-option, remember to
2713 * register it in the list at the top of handle_revision_opt.
2715 if (!strcmp(arg, "--all")) {
2716 handle_refs(refs, revs, *flags, refs_for_each_ref);
2717 handle_refs(refs, revs, *flags, refs_head_ref);
2718 if (!revs->single_worktree) {
2719 struct all_refs_cb cb;
2721 init_all_refs_cb(&cb, revs, *flags);
2722 other_head_refs(handle_one_ref, &cb);
2724 clear_ref_exclusions(&revs->ref_excludes);
2725 } else if (!strcmp(arg, "--branches")) {
2726 if (revs->ref_excludes.hidden_refs_configured)
2727 return error(_("--exclude-hidden cannot be used together with --branches"));
2728 handle_refs(refs, revs, *flags, refs_for_each_branch_ref);
2729 clear_ref_exclusions(&revs->ref_excludes);
2730 } else if (!strcmp(arg, "--bisect")) {
2731 read_bisect_terms(&term_bad, &term_good);
2732 handle_refs(refs, revs, *flags, for_each_bad_bisect_ref);
2733 handle_refs(refs, revs, *flags ^ (UNINTERESTING | BOTTOM),
2734 for_each_good_bisect_ref);
2735 revs->bisect = 1;
2736 } else if (!strcmp(arg, "--tags")) {
2737 if (revs->ref_excludes.hidden_refs_configured)
2738 return error(_("--exclude-hidden cannot be used together with --tags"));
2739 handle_refs(refs, revs, *flags, refs_for_each_tag_ref);
2740 clear_ref_exclusions(&revs->ref_excludes);
2741 } else if (!strcmp(arg, "--remotes")) {
2742 if (revs->ref_excludes.hidden_refs_configured)
2743 return error(_("--exclude-hidden cannot be used together with --remotes"));
2744 handle_refs(refs, revs, *flags, refs_for_each_remote_ref);
2745 clear_ref_exclusions(&revs->ref_excludes);
2746 } else if ((argcount = parse_long_opt("glob", argv, &optarg))) {
2747 struct all_refs_cb cb;
2748 init_all_refs_cb(&cb, revs, *flags);
2749 for_each_glob_ref(handle_one_ref, optarg, &cb);
2750 clear_ref_exclusions(&revs->ref_excludes);
2751 return argcount;
2752 } else if ((argcount = parse_long_opt("exclude", argv, &optarg))) {
2753 add_ref_exclusion(&revs->ref_excludes, optarg);
2754 return argcount;
2755 } else if ((argcount = parse_long_opt("exclude-hidden", argv, &optarg))) {
2756 exclude_hidden_refs(&revs->ref_excludes, optarg);
2757 return argcount;
2758 } else if (skip_prefix(arg, "--branches=", &optarg)) {
2759 struct all_refs_cb cb;
2760 if (revs->ref_excludes.hidden_refs_configured)
2761 return error(_("--exclude-hidden cannot be used together with --branches"));
2762 init_all_refs_cb(&cb, revs, *flags);
2763 for_each_glob_ref_in(handle_one_ref, optarg, "refs/heads/", &cb);
2764 clear_ref_exclusions(&revs->ref_excludes);
2765 } else if (skip_prefix(arg, "--tags=", &optarg)) {
2766 struct all_refs_cb cb;
2767 if (revs->ref_excludes.hidden_refs_configured)
2768 return error(_("--exclude-hidden cannot be used together with --tags"));
2769 init_all_refs_cb(&cb, revs, *flags);
2770 for_each_glob_ref_in(handle_one_ref, optarg, "refs/tags/", &cb);
2771 clear_ref_exclusions(&revs->ref_excludes);
2772 } else if (skip_prefix(arg, "--remotes=", &optarg)) {
2773 struct all_refs_cb cb;
2774 if (revs->ref_excludes.hidden_refs_configured)
2775 return error(_("--exclude-hidden cannot be used together with --remotes"));
2776 init_all_refs_cb(&cb, revs, *flags);
2777 for_each_glob_ref_in(handle_one_ref, optarg, "refs/remotes/", &cb);
2778 clear_ref_exclusions(&revs->ref_excludes);
2779 } else if (!strcmp(arg, "--reflog")) {
2780 add_reflogs_to_pending(revs, *flags);
2781 } else if (!strcmp(arg, "--indexed-objects")) {
2782 add_index_objects_to_pending(revs, *flags);
2783 } else if (!strcmp(arg, "--alternate-refs")) {
2784 add_alternate_refs_to_pending(revs, *flags);
2785 } else if (!strcmp(arg, "--not")) {
2786 *flags ^= UNINTERESTING | BOTTOM;
2787 } else if (!strcmp(arg, "--no-walk")) {
2788 revs->no_walk = 1;
2789 } else if (skip_prefix(arg, "--no-walk=", &optarg)) {
2791 * Detached form ("--no-walk X" as opposed to "--no-walk=X")
2792 * not allowed, since the argument is optional.
2794 revs->no_walk = 1;
2795 if (!strcmp(optarg, "sorted"))
2796 revs->unsorted_input = 0;
2797 else if (!strcmp(optarg, "unsorted"))
2798 revs->unsorted_input = 1;
2799 else
2800 return error("invalid argument to --no-walk");
2801 } else if (!strcmp(arg, "--do-walk")) {
2802 revs->no_walk = 0;
2803 } else if (!strcmp(arg, "--single-worktree")) {
2804 revs->single_worktree = 1;
2805 } else if (skip_prefix(arg, ("--filter="), &arg)) {
2806 parse_list_objects_filter(&revs->filter, arg);
2807 } else if (!strcmp(arg, ("--no-filter"))) {
2808 list_objects_filter_set_no_filter(&revs->filter);
2809 } else {
2810 return 0;
2813 return 1;
2816 static void NORETURN diagnose_missing_default(const char *def)
2818 int flags;
2819 const char *refname;
2821 refname = resolve_ref_unsafe(def, 0, NULL, &flags);
2822 if (!refname || !(flags & REF_ISSYMREF) || (flags & REF_ISBROKEN))
2823 die(_("your current branch appears to be broken"));
2825 skip_prefix(refname, "refs/heads/", &refname);
2826 die(_("your current branch '%s' does not have any commits yet"),
2827 refname);
2831 * Parse revision information, filling in the "rev_info" structure,
2832 * and removing the used arguments from the argument list.
2834 * Returns the number of arguments left that weren't recognized
2835 * (which are also moved to the head of the argument list)
2837 int setup_revisions(int argc, const char **argv, struct rev_info *revs, struct setup_revision_opt *opt)
2839 int i, flags, left, seen_dashdash, revarg_opt;
2840 struct strvec prune_data = STRVEC_INIT;
2841 int seen_end_of_options = 0;
2843 /* First, search for "--" */
2844 if (opt && opt->assume_dashdash) {
2845 seen_dashdash = 1;
2846 } else {
2847 seen_dashdash = 0;
2848 for (i = 1; i < argc; i++) {
2849 const char *arg = argv[i];
2850 if (strcmp(arg, "--"))
2851 continue;
2852 if (opt && opt->free_removed_argv_elements)
2853 free((char *)argv[i]);
2854 argv[i] = NULL;
2855 argc = i;
2856 if (argv[i + 1])
2857 strvec_pushv(&prune_data, argv + i + 1);
2858 seen_dashdash = 1;
2859 break;
2863 /* Second, deal with arguments and options */
2864 flags = 0;
2865 revarg_opt = opt ? opt->revarg_opt : 0;
2866 if (seen_dashdash)
2867 revarg_opt |= REVARG_CANNOT_BE_FILENAME;
2868 for (left = i = 1; i < argc; i++) {
2869 const char *arg = argv[i];
2870 if (!seen_end_of_options && *arg == '-') {
2871 int opts;
2873 opts = handle_revision_pseudo_opt(
2874 revs, argv + i,
2875 &flags);
2876 if (opts > 0) {
2877 i += opts - 1;
2878 continue;
2881 if (!strcmp(arg, "--stdin")) {
2882 if (revs->disable_stdin) {
2883 argv[left++] = arg;
2884 continue;
2886 if (revs->read_from_stdin++)
2887 die("--stdin given twice?");
2888 read_revisions_from_stdin(revs, &prune_data);
2889 continue;
2892 if (!strcmp(arg, "--end-of-options")) {
2893 seen_end_of_options = 1;
2894 continue;
2897 opts = handle_revision_opt(revs, argc - i, argv + i,
2898 &left, argv, opt);
2899 if (opts > 0) {
2900 i += opts - 1;
2901 continue;
2903 if (opts < 0)
2904 exit(128);
2905 continue;
2909 if (handle_revision_arg(arg, revs, flags, revarg_opt)) {
2910 int j;
2911 if (seen_dashdash || *arg == '^')
2912 die("bad revision '%s'", arg);
2914 /* If we didn't have a "--":
2915 * (1) all filenames must exist;
2916 * (2) all rev-args must not be interpretable
2917 * as a valid filename.
2918 * but the latter we have checked in the main loop.
2920 for (j = i; j < argc; j++)
2921 verify_filename(revs->prefix, argv[j], j == i);
2923 strvec_pushv(&prune_data, argv + i);
2924 break;
2927 revision_opts_finish(revs);
2929 if (prune_data.nr) {
2931 * If we need to introduce the magic "a lone ':' means no
2932 * pathspec whatsoever", here is the place to do so.
2934 * if (prune_data.nr == 1 && !strcmp(prune_data[0], ":")) {
2935 * prune_data.nr = 0;
2936 * prune_data.alloc = 0;
2937 * free(prune_data.path);
2938 * prune_data.path = NULL;
2939 * } else {
2940 * terminate prune_data.alloc with NULL and
2941 * call init_pathspec() to set revs->prune_data here.
2944 parse_pathspec(&revs->prune_data, 0, 0,
2945 revs->prefix, prune_data.v);
2947 strvec_clear(&prune_data);
2949 if (!revs->def)
2950 revs->def = opt ? opt->def : NULL;
2951 if (opt && opt->tweak)
2952 opt->tweak(revs, opt);
2953 if (revs->show_merge)
2954 prepare_show_merge(revs);
2955 if (revs->def && !revs->pending.nr && !revs->rev_input_given) {
2956 struct object_id oid;
2957 struct object *object;
2958 struct object_context oc;
2959 if (get_oid_with_context(revs->repo, revs->def, 0, &oid, &oc))
2960 diagnose_missing_default(revs->def);
2961 object = get_reference(revs, revs->def, &oid, 0);
2962 add_pending_object_with_mode(revs, object, revs->def, oc.mode);
2965 /* Did the user ask for any diff output? Run the diff! */
2966 if (revs->diffopt.output_format & ~DIFF_FORMAT_NO_OUTPUT)
2967 revs->diff = 1;
2969 /* Pickaxe, diff-filter and rename following need diffs */
2970 if ((revs->diffopt.pickaxe_opts & DIFF_PICKAXE_KINDS_MASK) ||
2971 revs->diffopt.filter ||
2972 revs->diffopt.flags.follow_renames)
2973 revs->diff = 1;
2975 if (revs->diffopt.objfind)
2976 revs->simplify_history = 0;
2978 if (revs->line_level_traverse) {
2979 if (want_ancestry(revs))
2980 revs->limited = 1;
2981 revs->topo_order = 1;
2984 if (revs->topo_order && !generation_numbers_enabled(the_repository))
2985 revs->limited = 1;
2987 if (revs->prune_data.nr) {
2988 copy_pathspec(&revs->pruning.pathspec, &revs->prune_data);
2989 /* Can't prune commits with rename following: the paths change.. */
2990 if (!revs->diffopt.flags.follow_renames)
2991 revs->prune = 1;
2992 if (!revs->full_diff)
2993 copy_pathspec(&revs->diffopt.pathspec,
2994 &revs->prune_data);
2997 diff_merges_setup_revs(revs);
2999 revs->diffopt.abbrev = revs->abbrev;
3001 diff_setup_done(&revs->diffopt);
3003 if (!is_encoding_utf8(get_log_output_encoding()))
3004 revs->grep_filter.ignore_locale = 1;
3005 compile_grep_patterns(&revs->grep_filter);
3007 if (revs->reverse && revs->reflog_info)
3008 die(_("options '%s' and '%s' cannot be used together"), "--reverse", "--walk-reflogs");
3009 if (revs->reflog_info && revs->limited)
3010 die("cannot combine --walk-reflogs with history-limiting options");
3011 if (revs->rewrite_parents && revs->children.name)
3012 die(_("options '%s' and '%s' cannot be used together"), "--parents", "--children");
3013 if (revs->filter.choice && !revs->blob_objects)
3014 die(_("object filtering requires --objects"));
3017 * Limitations on the graph functionality
3019 if (revs->reverse && revs->graph)
3020 die(_("options '%s' and '%s' cannot be used together"), "--reverse", "--graph");
3022 if (revs->reflog_info && revs->graph)
3023 die(_("options '%s' and '%s' cannot be used together"), "--walk-reflogs", "--graph");
3024 if (revs->no_walk && revs->graph)
3025 die(_("options '%s' and '%s' cannot be used together"), "--no-walk", "--graph");
3026 if (!revs->reflog_info && revs->grep_filter.use_reflog_filter)
3027 die(_("the option '%s' requires '%s'"), "--grep-reflog", "--walk-reflogs");
3029 if (revs->line_level_traverse &&
3030 (revs->diffopt.output_format & ~(DIFF_FORMAT_PATCH | DIFF_FORMAT_NO_OUTPUT)))
3031 die(_("-L does not yet support diff formats besides -p and -s"));
3033 if (revs->expand_tabs_in_log < 0)
3034 revs->expand_tabs_in_log = revs->expand_tabs_in_log_default;
3036 return left;
3039 static void release_revisions_cmdline(struct rev_cmdline_info *cmdline)
3041 unsigned int i;
3043 for (i = 0; i < cmdline->nr; i++)
3044 free((char *)cmdline->rev[i].name);
3045 free(cmdline->rev);
3048 static void release_revisions_mailmap(struct string_list *mailmap)
3050 if (!mailmap)
3051 return;
3052 clear_mailmap(mailmap);
3053 free(mailmap);
3056 static void release_revisions_topo_walk_info(struct topo_walk_info *info);
3058 void release_revisions(struct rev_info *revs)
3060 free_commit_list(revs->commits);
3061 free_commit_list(revs->ancestry_path_bottoms);
3062 object_array_clear(&revs->pending);
3063 object_array_clear(&revs->boundary_commits);
3064 release_revisions_cmdline(&revs->cmdline);
3065 list_objects_filter_release(&revs->filter);
3066 clear_pathspec(&revs->prune_data);
3067 date_mode_release(&revs->date_mode);
3068 release_revisions_mailmap(revs->mailmap);
3069 free_grep_patterns(&revs->grep_filter);
3070 graph_clear(revs->graph);
3071 /* TODO (need to handle "no_free"): diff_free(&revs->diffopt) */
3072 diff_free(&revs->pruning);
3073 reflog_walk_info_release(revs->reflog_info);
3074 release_revisions_topo_walk_info(revs->topo_walk_info);
3077 static void add_child(struct rev_info *revs, struct commit *parent, struct commit *child)
3079 struct commit_list *l = xcalloc(1, sizeof(*l));
3081 l->item = child;
3082 l->next = add_decoration(&revs->children, &parent->object, l);
3085 static int remove_duplicate_parents(struct rev_info *revs, struct commit *commit)
3087 struct treesame_state *ts = lookup_decoration(&revs->treesame, &commit->object);
3088 struct commit_list **pp, *p;
3089 int surviving_parents;
3091 /* Examine existing parents while marking ones we have seen... */
3092 pp = &commit->parents;
3093 surviving_parents = 0;
3094 while ((p = *pp) != NULL) {
3095 struct commit *parent = p->item;
3096 if (parent->object.flags & TMP_MARK) {
3097 *pp = p->next;
3098 if (ts)
3099 compact_treesame(revs, commit, surviving_parents);
3100 continue;
3102 parent->object.flags |= TMP_MARK;
3103 surviving_parents++;
3104 pp = &p->next;
3106 /* clear the temporary mark */
3107 for (p = commit->parents; p; p = p->next) {
3108 p->item->object.flags &= ~TMP_MARK;
3110 /* no update_treesame() - removing duplicates can't affect TREESAME */
3111 return surviving_parents;
3114 struct merge_simplify_state {
3115 struct commit *simplified;
3118 static struct merge_simplify_state *locate_simplify_state(struct rev_info *revs, struct commit *commit)
3120 struct merge_simplify_state *st;
3122 st = lookup_decoration(&revs->merge_simplification, &commit->object);
3123 if (!st) {
3124 CALLOC_ARRAY(st, 1);
3125 add_decoration(&revs->merge_simplification, &commit->object, st);
3127 return st;
3130 static int mark_redundant_parents(struct commit *commit)
3132 struct commit_list *h = reduce_heads(commit->parents);
3133 int i = 0, marked = 0;
3134 struct commit_list *po, *pn;
3136 /* Want these for sanity-checking only */
3137 int orig_cnt = commit_list_count(commit->parents);
3138 int cnt = commit_list_count(h);
3141 * Not ready to remove items yet, just mark them for now, based
3142 * on the output of reduce_heads(). reduce_heads outputs the reduced
3143 * set in its original order, so this isn't too hard.
3145 po = commit->parents;
3146 pn = h;
3147 while (po) {
3148 if (pn && po->item == pn->item) {
3149 pn = pn->next;
3150 i++;
3151 } else {
3152 po->item->object.flags |= TMP_MARK;
3153 marked++;
3155 po=po->next;
3158 if (i != cnt || cnt+marked != orig_cnt)
3159 die("mark_redundant_parents %d %d %d %d", orig_cnt, cnt, i, marked);
3161 free_commit_list(h);
3163 return marked;
3166 static int mark_treesame_root_parents(struct commit *commit)
3168 struct commit_list *p;
3169 int marked = 0;
3171 for (p = commit->parents; p; p = p->next) {
3172 struct commit *parent = p->item;
3173 if (!parent->parents && (parent->object.flags & TREESAME)) {
3174 parent->object.flags |= TMP_MARK;
3175 marked++;
3179 return marked;
3183 * Awkward naming - this means one parent we are TREESAME to.
3184 * cf mark_treesame_root_parents: root parents that are TREESAME (to an
3185 * empty tree). Better name suggestions?
3187 static int leave_one_treesame_to_parent(struct rev_info *revs, struct commit *commit)
3189 struct treesame_state *ts = lookup_decoration(&revs->treesame, &commit->object);
3190 struct commit *unmarked = NULL, *marked = NULL;
3191 struct commit_list *p;
3192 unsigned n;
3194 for (p = commit->parents, n = 0; p; p = p->next, n++) {
3195 if (ts->treesame[n]) {
3196 if (p->item->object.flags & TMP_MARK) {
3197 if (!marked)
3198 marked = p->item;
3199 } else {
3200 if (!unmarked) {
3201 unmarked = p->item;
3202 break;
3209 * If we are TREESAME to a marked-for-deletion parent, but not to any
3210 * unmarked parents, unmark the first TREESAME parent. This is the
3211 * parent that the default simplify_history==1 scan would have followed,
3212 * and it doesn't make sense to omit that path when asking for a
3213 * simplified full history. Retaining it improves the chances of
3214 * understanding odd missed merges that took an old version of a file.
3216 * Example:
3218 * I--------*X A modified the file, but mainline merge X used
3219 * \ / "-s ours", so took the version from I. X is
3220 * `-*A--' TREESAME to I and !TREESAME to A.
3222 * Default log from X would produce "I". Without this check,
3223 * --full-history --simplify-merges would produce "I-A-X", showing
3224 * the merge commit X and that it changed A, but not making clear that
3225 * it had just taken the I version. With this check, the topology above
3226 * is retained.
3228 * Note that it is possible that the simplification chooses a different
3229 * TREESAME parent from the default, in which case this test doesn't
3230 * activate, and we _do_ drop the default parent. Example:
3232 * I------X A modified the file, but it was reverted in B,
3233 * \ / meaning mainline merge X is TREESAME to both
3234 * *A-*B parents.
3236 * Default log would produce "I" by following the first parent;
3237 * --full-history --simplify-merges will produce "I-A-B". But this is a
3238 * reasonable result - it presents a logical full history leading from
3239 * I to X, and X is not an important merge.
3241 if (!unmarked && marked) {
3242 marked->object.flags &= ~TMP_MARK;
3243 return 1;
3246 return 0;
3249 static int remove_marked_parents(struct rev_info *revs, struct commit *commit)
3251 struct commit_list **pp, *p;
3252 int nth_parent, removed = 0;
3254 pp = &commit->parents;
3255 nth_parent = 0;
3256 while ((p = *pp) != NULL) {
3257 struct commit *parent = p->item;
3258 if (parent->object.flags & TMP_MARK) {
3259 parent->object.flags &= ~TMP_MARK;
3260 *pp = p->next;
3261 free(p);
3262 removed++;
3263 compact_treesame(revs, commit, nth_parent);
3264 continue;
3266 pp = &p->next;
3267 nth_parent++;
3270 /* Removing parents can only increase TREESAMEness */
3271 if (removed && !(commit->object.flags & TREESAME))
3272 update_treesame(revs, commit);
3274 return nth_parent;
3277 static struct commit_list **simplify_one(struct rev_info *revs, struct commit *commit, struct commit_list **tail)
3279 struct commit_list *p;
3280 struct commit *parent;
3281 struct merge_simplify_state *st, *pst;
3282 int cnt;
3284 st = locate_simplify_state(revs, commit);
3287 * Have we handled this one?
3289 if (st->simplified)
3290 return tail;
3293 * An UNINTERESTING commit simplifies to itself, so does a
3294 * root commit. We do not rewrite parents of such commit
3295 * anyway.
3297 if ((commit->object.flags & UNINTERESTING) || !commit->parents) {
3298 st->simplified = commit;
3299 return tail;
3303 * Do we know what commit all of our parents that matter
3304 * should be rewritten to? Otherwise we are not ready to
3305 * rewrite this one yet.
3307 for (cnt = 0, p = commit->parents; p; p = p->next) {
3308 pst = locate_simplify_state(revs, p->item);
3309 if (!pst->simplified) {
3310 tail = &commit_list_insert(p->item, tail)->next;
3311 cnt++;
3313 if (revs->first_parent_only)
3314 break;
3316 if (cnt) {
3317 tail = &commit_list_insert(commit, tail)->next;
3318 return tail;
3322 * Rewrite our list of parents. Note that this cannot
3323 * affect our TREESAME flags in any way - a commit is
3324 * always TREESAME to its simplification.
3326 for (p = commit->parents; p; p = p->next) {
3327 pst = locate_simplify_state(revs, p->item);
3328 p->item = pst->simplified;
3329 if (revs->first_parent_only)
3330 break;
3333 if (revs->first_parent_only)
3334 cnt = 1;
3335 else
3336 cnt = remove_duplicate_parents(revs, commit);
3339 * It is possible that we are a merge and one side branch
3340 * does not have any commit that touches the given paths;
3341 * in such a case, the immediate parent from that branch
3342 * will be rewritten to be the merge base.
3344 * o----X X: the commit we are looking at;
3345 * / / o: a commit that touches the paths;
3346 * ---o----'
3348 * Further, a merge of an independent branch that doesn't
3349 * touch the path will reduce to a treesame root parent:
3351 * ----o----X X: the commit we are looking at;
3352 * / o: a commit that touches the paths;
3353 * r r: a root commit not touching the paths
3355 * Detect and simplify both cases.
3357 if (1 < cnt) {
3358 int marked = mark_redundant_parents(commit);
3359 marked += mark_treesame_root_parents(commit);
3360 if (marked)
3361 marked -= leave_one_treesame_to_parent(revs, commit);
3362 if (marked)
3363 cnt = remove_marked_parents(revs, commit);
3367 * A commit simplifies to itself if it is a root, if it is
3368 * UNINTERESTING, if it touches the given paths, or if it is a
3369 * merge and its parents don't simplify to one relevant commit
3370 * (the first two cases are already handled at the beginning of
3371 * this function).
3373 * Otherwise, it simplifies to what its sole relevant parent
3374 * simplifies to.
3376 if (!cnt ||
3377 (commit->object.flags & UNINTERESTING) ||
3378 !(commit->object.flags & TREESAME) ||
3379 (parent = one_relevant_parent(revs, commit->parents)) == NULL ||
3380 (revs->show_pulls && (commit->object.flags & PULL_MERGE)))
3381 st->simplified = commit;
3382 else {
3383 pst = locate_simplify_state(revs, parent);
3384 st->simplified = pst->simplified;
3386 return tail;
3389 static void simplify_merges(struct rev_info *revs)
3391 struct commit_list *list, *next;
3392 struct commit_list *yet_to_do, **tail;
3393 struct commit *commit;
3395 if (!revs->prune)
3396 return;
3398 /* feed the list reversed */
3399 yet_to_do = NULL;
3400 for (list = revs->commits; list; list = next) {
3401 commit = list->item;
3402 next = list->next;
3404 * Do not free(list) here yet; the original list
3405 * is used later in this function.
3407 commit_list_insert(commit, &yet_to_do);
3409 while (yet_to_do) {
3410 list = yet_to_do;
3411 yet_to_do = NULL;
3412 tail = &yet_to_do;
3413 while (list) {
3414 commit = pop_commit(&list);
3415 tail = simplify_one(revs, commit, tail);
3419 /* clean up the result, removing the simplified ones */
3420 list = revs->commits;
3421 revs->commits = NULL;
3422 tail = &revs->commits;
3423 while (list) {
3424 struct merge_simplify_state *st;
3426 commit = pop_commit(&list);
3427 st = locate_simplify_state(revs, commit);
3428 if (st->simplified == commit)
3429 tail = &commit_list_insert(commit, tail)->next;
3433 static void set_children(struct rev_info *revs)
3435 struct commit_list *l;
3436 for (l = revs->commits; l; l = l->next) {
3437 struct commit *commit = l->item;
3438 struct commit_list *p;
3440 for (p = commit->parents; p; p = p->next)
3441 add_child(revs, p->item, commit);
3445 void reset_revision_walk(void)
3447 clear_object_flags(SEEN | ADDED | SHOWN | TOPO_WALK_EXPLORED | TOPO_WALK_INDEGREE);
3450 static int mark_uninteresting(const struct object_id *oid,
3451 struct packed_git *pack UNUSED,
3452 uint32_t pos UNUSED,
3453 void *cb)
3455 struct rev_info *revs = cb;
3456 struct object *o = lookup_unknown_object(revs->repo, oid);
3457 o->flags |= UNINTERESTING | SEEN;
3458 return 0;
3461 define_commit_slab(indegree_slab, int);
3462 define_commit_slab(author_date_slab, timestamp_t);
3464 struct topo_walk_info {
3465 timestamp_t min_generation;
3466 struct prio_queue explore_queue;
3467 struct prio_queue indegree_queue;
3468 struct prio_queue topo_queue;
3469 struct indegree_slab indegree;
3470 struct author_date_slab author_date;
3473 static int topo_walk_atexit_registered;
3474 static unsigned int count_explore_walked;
3475 static unsigned int count_indegree_walked;
3476 static unsigned int count_topo_walked;
3478 static void trace2_topo_walk_statistics_atexit(void)
3480 struct json_writer jw = JSON_WRITER_INIT;
3482 jw_object_begin(&jw, 0);
3483 jw_object_intmax(&jw, "count_explore_walked", count_explore_walked);
3484 jw_object_intmax(&jw, "count_indegree_walked", count_indegree_walked);
3485 jw_object_intmax(&jw, "count_topo_walked", count_topo_walked);
3486 jw_end(&jw);
3488 trace2_data_json("topo_walk", the_repository, "statistics", &jw);
3490 jw_release(&jw);
3493 static inline void test_flag_and_insert(struct prio_queue *q, struct commit *c, int flag)
3495 if (c->object.flags & flag)
3496 return;
3498 c->object.flags |= flag;
3499 prio_queue_put(q, c);
3502 static void explore_walk_step(struct rev_info *revs)
3504 struct topo_walk_info *info = revs->topo_walk_info;
3505 struct commit_list *p;
3506 struct commit *c = prio_queue_get(&info->explore_queue);
3508 if (!c)
3509 return;
3511 if (repo_parse_commit_gently(revs->repo, c, 1) < 0)
3512 return;
3514 count_explore_walked++;
3516 if (revs->sort_order == REV_SORT_BY_AUTHOR_DATE)
3517 record_author_date(&info->author_date, c);
3519 if (revs->max_age != -1 && (c->date < revs->max_age))
3520 c->object.flags |= UNINTERESTING;
3522 if (process_parents(revs, c, NULL, NULL) < 0)
3523 return;
3525 if (c->object.flags & UNINTERESTING)
3526 mark_parents_uninteresting(revs, c);
3528 for (p = c->parents; p; p = p->next)
3529 test_flag_and_insert(&info->explore_queue, p->item, TOPO_WALK_EXPLORED);
3532 static void explore_to_depth(struct rev_info *revs,
3533 timestamp_t gen_cutoff)
3535 struct topo_walk_info *info = revs->topo_walk_info;
3536 struct commit *c;
3537 while ((c = prio_queue_peek(&info->explore_queue)) &&
3538 commit_graph_generation(c) >= gen_cutoff)
3539 explore_walk_step(revs);
3542 static void indegree_walk_step(struct rev_info *revs)
3544 struct commit_list *p;
3545 struct topo_walk_info *info = revs->topo_walk_info;
3546 struct commit *c = prio_queue_get(&info->indegree_queue);
3548 if (!c)
3549 return;
3551 if (repo_parse_commit_gently(revs->repo, c, 1) < 0)
3552 return;
3554 count_indegree_walked++;
3556 explore_to_depth(revs, commit_graph_generation(c));
3558 for (p = c->parents; p; p = p->next) {
3559 struct commit *parent = p->item;
3560 int *pi = indegree_slab_at(&info->indegree, parent);
3562 if (repo_parse_commit_gently(revs->repo, parent, 1) < 0)
3563 return;
3565 if (*pi)
3566 (*pi)++;
3567 else
3568 *pi = 2;
3570 test_flag_and_insert(&info->indegree_queue, parent, TOPO_WALK_INDEGREE);
3572 if (revs->first_parent_only)
3573 return;
3577 static void compute_indegrees_to_depth(struct rev_info *revs,
3578 timestamp_t gen_cutoff)
3580 struct topo_walk_info *info = revs->topo_walk_info;
3581 struct commit *c;
3582 while ((c = prio_queue_peek(&info->indegree_queue)) &&
3583 commit_graph_generation(c) >= gen_cutoff)
3584 indegree_walk_step(revs);
3587 static void release_revisions_topo_walk_info(struct topo_walk_info *info)
3589 if (!info)
3590 return;
3591 clear_prio_queue(&info->explore_queue);
3592 clear_prio_queue(&info->indegree_queue);
3593 clear_prio_queue(&info->topo_queue);
3594 clear_indegree_slab(&info->indegree);
3595 clear_author_date_slab(&info->author_date);
3596 free(info);
3599 static void reset_topo_walk(struct rev_info *revs)
3601 release_revisions_topo_walk_info(revs->topo_walk_info);
3602 revs->topo_walk_info = NULL;
3605 static void init_topo_walk(struct rev_info *revs)
3607 struct topo_walk_info *info;
3608 struct commit_list *list;
3609 if (revs->topo_walk_info)
3610 reset_topo_walk(revs);
3612 revs->topo_walk_info = xmalloc(sizeof(struct topo_walk_info));
3613 info = revs->topo_walk_info;
3614 memset(info, 0, sizeof(struct topo_walk_info));
3616 init_indegree_slab(&info->indegree);
3617 memset(&info->explore_queue, 0, sizeof(info->explore_queue));
3618 memset(&info->indegree_queue, 0, sizeof(info->indegree_queue));
3619 memset(&info->topo_queue, 0, sizeof(info->topo_queue));
3621 switch (revs->sort_order) {
3622 default: /* REV_SORT_IN_GRAPH_ORDER */
3623 info->topo_queue.compare = NULL;
3624 break;
3625 case REV_SORT_BY_COMMIT_DATE:
3626 info->topo_queue.compare = compare_commits_by_commit_date;
3627 break;
3628 case REV_SORT_BY_AUTHOR_DATE:
3629 init_author_date_slab(&info->author_date);
3630 info->topo_queue.compare = compare_commits_by_author_date;
3631 info->topo_queue.cb_data = &info->author_date;
3632 break;
3635 info->explore_queue.compare = compare_commits_by_gen_then_commit_date;
3636 info->indegree_queue.compare = compare_commits_by_gen_then_commit_date;
3638 info->min_generation = GENERATION_NUMBER_INFINITY;
3639 for (list = revs->commits; list; list = list->next) {
3640 struct commit *c = list->item;
3641 timestamp_t generation;
3643 if (repo_parse_commit_gently(revs->repo, c, 1))
3644 continue;
3646 test_flag_and_insert(&info->explore_queue, c, TOPO_WALK_EXPLORED);
3647 test_flag_and_insert(&info->indegree_queue, c, TOPO_WALK_INDEGREE);
3649 generation = commit_graph_generation(c);
3650 if (generation < info->min_generation)
3651 info->min_generation = generation;
3653 *(indegree_slab_at(&info->indegree, c)) = 1;
3655 if (revs->sort_order == REV_SORT_BY_AUTHOR_DATE)
3656 record_author_date(&info->author_date, c);
3658 compute_indegrees_to_depth(revs, info->min_generation);
3660 for (list = revs->commits; list; list = list->next) {
3661 struct commit *c = list->item;
3663 if (*(indegree_slab_at(&info->indegree, c)) == 1)
3664 prio_queue_put(&info->topo_queue, c);
3668 * This is unfortunate; the initial tips need to be shown
3669 * in the order given from the revision traversal machinery.
3671 if (revs->sort_order == REV_SORT_IN_GRAPH_ORDER)
3672 prio_queue_reverse(&info->topo_queue);
3674 if (trace2_is_enabled() && !topo_walk_atexit_registered) {
3675 atexit(trace2_topo_walk_statistics_atexit);
3676 topo_walk_atexit_registered = 1;
3680 static struct commit *next_topo_commit(struct rev_info *revs)
3682 struct commit *c;
3683 struct topo_walk_info *info = revs->topo_walk_info;
3685 /* pop next off of topo_queue */
3686 c = prio_queue_get(&info->topo_queue);
3688 if (c)
3689 *(indegree_slab_at(&info->indegree, c)) = 0;
3691 return c;
3694 static void expand_topo_walk(struct rev_info *revs, struct commit *commit)
3696 struct commit_list *p;
3697 struct topo_walk_info *info = revs->topo_walk_info;
3698 if (process_parents(revs, commit, NULL, NULL) < 0) {
3699 if (!revs->ignore_missing_links)
3700 die("Failed to traverse parents of commit %s",
3701 oid_to_hex(&commit->object.oid));
3704 count_topo_walked++;
3706 for (p = commit->parents; p; p = p->next) {
3707 struct commit *parent = p->item;
3708 int *pi;
3709 timestamp_t generation;
3711 if (parent->object.flags & UNINTERESTING)
3712 continue;
3714 if (repo_parse_commit_gently(revs->repo, parent, 1) < 0)
3715 continue;
3717 generation = commit_graph_generation(parent);
3718 if (generation < info->min_generation) {
3719 info->min_generation = generation;
3720 compute_indegrees_to_depth(revs, info->min_generation);
3723 pi = indegree_slab_at(&info->indegree, parent);
3725 (*pi)--;
3726 if (*pi == 1)
3727 prio_queue_put(&info->topo_queue, parent);
3729 if (revs->first_parent_only)
3730 return;
3734 int prepare_revision_walk(struct rev_info *revs)
3736 int i;
3737 struct object_array old_pending;
3738 struct commit_list **next = &revs->commits;
3740 memcpy(&old_pending, &revs->pending, sizeof(old_pending));
3741 revs->pending.nr = 0;
3742 revs->pending.alloc = 0;
3743 revs->pending.objects = NULL;
3744 for (i = 0; i < old_pending.nr; i++) {
3745 struct object_array_entry *e = old_pending.objects + i;
3746 struct commit *commit = handle_commit(revs, e);
3747 if (commit) {
3748 if (!(commit->object.flags & SEEN)) {
3749 commit->object.flags |= SEEN;
3750 next = commit_list_append(commit, next);
3754 object_array_clear(&old_pending);
3756 /* Signal whether we need per-parent treesame decoration */
3757 if (revs->simplify_merges ||
3758 (revs->limited && limiting_can_increase_treesame(revs)))
3759 revs->treesame.name = "treesame";
3761 if (revs->exclude_promisor_objects) {
3762 for_each_packed_object(mark_uninteresting, revs,
3763 FOR_EACH_OBJECT_PROMISOR_ONLY);
3766 if (!revs->reflog_info)
3767 prepare_to_use_bloom_filter(revs);
3768 if (!revs->unsorted_input)
3769 commit_list_sort_by_date(&revs->commits);
3770 if (revs->no_walk)
3771 return 0;
3772 if (revs->limited) {
3773 if (limit_list(revs) < 0)
3774 return -1;
3775 if (revs->topo_order)
3776 sort_in_topological_order(&revs->commits, revs->sort_order);
3777 } else if (revs->topo_order)
3778 init_topo_walk(revs);
3779 if (revs->line_level_traverse && want_ancestry(revs))
3781 * At the moment we can only do line-level log with parent
3782 * rewriting by performing this expensive pre-filtering step.
3783 * If parent rewriting is not requested, then we rather
3784 * perform the line-level log filtering during the regular
3785 * history traversal.
3787 line_log_filter(revs);
3788 if (revs->simplify_merges)
3789 simplify_merges(revs);
3790 if (revs->children.name)
3791 set_children(revs);
3793 return 0;
3796 static enum rewrite_result rewrite_one_1(struct rev_info *revs,
3797 struct commit **pp,
3798 struct prio_queue *queue)
3800 for (;;) {
3801 struct commit *p = *pp;
3802 if (!revs->limited)
3803 if (process_parents(revs, p, NULL, queue) < 0)
3804 return rewrite_one_error;
3805 if (p->object.flags & UNINTERESTING)
3806 return rewrite_one_ok;
3807 if (!(p->object.flags & TREESAME))
3808 return rewrite_one_ok;
3809 if (!p->parents)
3810 return rewrite_one_noparents;
3811 if (!(p = one_relevant_parent(revs, p->parents)))
3812 return rewrite_one_ok;
3813 *pp = p;
3817 static void merge_queue_into_list(struct prio_queue *q, struct commit_list **list)
3819 while (q->nr) {
3820 struct commit *item = prio_queue_peek(q);
3821 struct commit_list *p = *list;
3823 if (p && p->item->date >= item->date)
3824 list = &p->next;
3825 else {
3826 p = commit_list_insert(item, list);
3827 list = &p->next; /* skip newly added item */
3828 prio_queue_get(q); /* pop item */
3833 static enum rewrite_result rewrite_one(struct rev_info *revs, struct commit **pp)
3835 struct prio_queue queue = { compare_commits_by_commit_date };
3836 enum rewrite_result ret = rewrite_one_1(revs, pp, &queue);
3837 merge_queue_into_list(&queue, &revs->commits);
3838 clear_prio_queue(&queue);
3839 return ret;
3842 int rewrite_parents(struct rev_info *revs, struct commit *commit,
3843 rewrite_parent_fn_t rewrite_parent)
3845 struct commit_list **pp = &commit->parents;
3846 while (*pp) {
3847 struct commit_list *parent = *pp;
3848 switch (rewrite_parent(revs, &parent->item)) {
3849 case rewrite_one_ok:
3850 break;
3851 case rewrite_one_noparents:
3852 *pp = parent->next;
3853 continue;
3854 case rewrite_one_error:
3855 return -1;
3857 pp = &parent->next;
3859 remove_duplicate_parents(revs, commit);
3860 return 0;
3863 static int commit_match(struct commit *commit, struct rev_info *opt)
3865 int retval;
3866 const char *encoding;
3867 const char *message;
3868 struct strbuf buf = STRBUF_INIT;
3870 if (!opt->grep_filter.pattern_list && !opt->grep_filter.header_list)
3871 return 1;
3873 /* Prepend "fake" headers as needed */
3874 if (opt->grep_filter.use_reflog_filter) {
3875 strbuf_addstr(&buf, "reflog ");
3876 get_reflog_message(&buf, opt->reflog_info);
3877 strbuf_addch(&buf, '\n');
3881 * We grep in the user's output encoding, under the assumption that it
3882 * is the encoding they are most likely to write their grep pattern
3883 * for. In addition, it means we will match the "notes" encoding below,
3884 * so we will not end up with a buffer that has two different encodings
3885 * in it.
3887 encoding = get_log_output_encoding();
3888 message = repo_logmsg_reencode(the_repository, commit, NULL, encoding);
3890 /* Copy the commit to temporary if we are using "fake" headers */
3891 if (buf.len)
3892 strbuf_addstr(&buf, message);
3894 if (opt->grep_filter.header_list && opt->mailmap) {
3895 const char *commit_headers[] = { "author ", "committer ", NULL };
3897 if (!buf.len)
3898 strbuf_addstr(&buf, message);
3900 apply_mailmap_to_header(&buf, commit_headers, opt->mailmap);
3903 /* Append "fake" message parts as needed */
3904 if (opt->show_notes) {
3905 if (!buf.len)
3906 strbuf_addstr(&buf, message);
3907 format_display_notes(&commit->object.oid, &buf, encoding, 1);
3911 * Find either in the original commit message, or in the temporary.
3912 * Note that we cast away the constness of "message" here. It is
3913 * const because it may come from the cached commit buffer. That's OK,
3914 * because we know that it is modifiable heap memory, and that while
3915 * grep_buffer may modify it for speed, it will restore any
3916 * changes before returning.
3918 if (buf.len)
3919 retval = grep_buffer(&opt->grep_filter, buf.buf, buf.len);
3920 else
3921 retval = grep_buffer(&opt->grep_filter,
3922 (char *)message, strlen(message));
3923 strbuf_release(&buf);
3924 repo_unuse_commit_buffer(the_repository, commit, message);
3925 return retval;
3928 static inline int want_ancestry(const struct rev_info *revs)
3930 return (revs->rewrite_parents || revs->children.name);
3934 * Return a timestamp to be used for --since/--until comparisons for this
3935 * commit, based on the revision options.
3937 static timestamp_t comparison_date(const struct rev_info *revs,
3938 struct commit *commit)
3940 return revs->reflog_info ?
3941 get_reflog_timestamp(revs->reflog_info) :
3942 commit->date;
3945 enum commit_action get_commit_action(struct rev_info *revs, struct commit *commit)
3947 if (commit->object.flags & SHOWN)
3948 return commit_ignore;
3949 if (revs->unpacked && has_object_pack(&commit->object.oid))
3950 return commit_ignore;
3951 if (revs->no_kept_objects) {
3952 if (has_object_kept_pack(&commit->object.oid,
3953 revs->keep_pack_cache_flags))
3954 return commit_ignore;
3956 if (commit->object.flags & UNINTERESTING)
3957 return commit_ignore;
3958 if (revs->line_level_traverse && !want_ancestry(revs)) {
3960 * In case of line-level log with parent rewriting
3961 * prepare_revision_walk() already took care of all line-level
3962 * log filtering, and there is nothing left to do here.
3964 * If parent rewriting was not requested, then this is the
3965 * place to perform the line-level log filtering. Notably,
3966 * this check, though expensive, must come before the other,
3967 * cheaper filtering conditions, because the tracked line
3968 * ranges must be adjusted even when the commit will end up
3969 * being ignored based on other conditions.
3971 if (!line_log_process_ranges_arbitrary_commit(revs, commit))
3972 return commit_ignore;
3974 if (revs->min_age != -1 &&
3975 comparison_date(revs, commit) > revs->min_age)
3976 return commit_ignore;
3977 if (revs->max_age_as_filter != -1 &&
3978 comparison_date(revs, commit) < revs->max_age_as_filter)
3979 return commit_ignore;
3980 if (revs->min_parents || (revs->max_parents >= 0)) {
3981 int n = commit_list_count(commit->parents);
3982 if ((n < revs->min_parents) ||
3983 ((revs->max_parents >= 0) && (n > revs->max_parents)))
3984 return commit_ignore;
3986 if (!commit_match(commit, revs))
3987 return commit_ignore;
3988 if (revs->prune && revs->dense) {
3989 /* Commit without changes? */
3990 if (commit->object.flags & TREESAME) {
3991 int n;
3992 struct commit_list *p;
3993 /* drop merges unless we want parenthood */
3994 if (!want_ancestry(revs))
3995 return commit_ignore;
3997 if (revs->show_pulls && (commit->object.flags & PULL_MERGE))
3998 return commit_show;
4001 * If we want ancestry, then need to keep any merges
4002 * between relevant commits to tie together topology.
4003 * For consistency with TREESAME and simplification
4004 * use "relevant" here rather than just INTERESTING,
4005 * to treat bottom commit(s) as part of the topology.
4007 for (n = 0, p = commit->parents; p; p = p->next)
4008 if (relevant_commit(p->item))
4009 if (++n >= 2)
4010 return commit_show;
4011 return commit_ignore;
4014 return commit_show;
4017 define_commit_slab(saved_parents, struct commit_list *);
4019 #define EMPTY_PARENT_LIST ((struct commit_list *)-1)
4022 * You may only call save_parents() once per commit (this is checked
4023 * for non-root commits).
4025 static void save_parents(struct rev_info *revs, struct commit *commit)
4027 struct commit_list **pp;
4029 if (!revs->saved_parents_slab) {
4030 revs->saved_parents_slab = xmalloc(sizeof(struct saved_parents));
4031 init_saved_parents(revs->saved_parents_slab);
4034 pp = saved_parents_at(revs->saved_parents_slab, commit);
4037 * When walking with reflogs, we may visit the same commit
4038 * several times: once for each appearance in the reflog.
4040 * In this case, save_parents() will be called multiple times.
4041 * We want to keep only the first set of parents. We need to
4042 * store a sentinel value for an empty (i.e., NULL) parent
4043 * list to distinguish it from a not-yet-saved list, however.
4045 if (*pp)
4046 return;
4047 if (commit->parents)
4048 *pp = copy_commit_list(commit->parents);
4049 else
4050 *pp = EMPTY_PARENT_LIST;
4053 static void free_saved_parents(struct rev_info *revs)
4055 if (revs->saved_parents_slab)
4056 clear_saved_parents(revs->saved_parents_slab);
4059 struct commit_list *get_saved_parents(struct rev_info *revs, const struct commit *commit)
4061 struct commit_list *parents;
4063 if (!revs->saved_parents_slab)
4064 return commit->parents;
4066 parents = *saved_parents_at(revs->saved_parents_slab, commit);
4067 if (parents == EMPTY_PARENT_LIST)
4068 return NULL;
4069 return parents;
4072 enum commit_action simplify_commit(struct rev_info *revs, struct commit *commit)
4074 enum commit_action action = get_commit_action(revs, commit);
4076 if (action == commit_show &&
4077 revs->prune && revs->dense && want_ancestry(revs)) {
4079 * --full-diff on simplified parents is no good: it
4080 * will show spurious changes from the commits that
4081 * were elided. So we save the parents on the side
4082 * when --full-diff is in effect.
4084 if (revs->full_diff)
4085 save_parents(revs, commit);
4086 if (rewrite_parents(revs, commit, rewrite_one) < 0)
4087 return commit_error;
4089 return action;
4092 static void track_linear(struct rev_info *revs, struct commit *commit)
4094 if (revs->track_first_time) {
4095 revs->linear = 1;
4096 revs->track_first_time = 0;
4097 } else {
4098 struct commit_list *p;
4099 for (p = revs->previous_parents; p; p = p->next)
4100 if (p->item == NULL || /* first commit */
4101 oideq(&p->item->object.oid, &commit->object.oid))
4102 break;
4103 revs->linear = p != NULL;
4105 if (revs->reverse) {
4106 if (revs->linear)
4107 commit->object.flags |= TRACK_LINEAR;
4109 free_commit_list(revs->previous_parents);
4110 revs->previous_parents = copy_commit_list(commit->parents);
4113 static struct commit *get_revision_1(struct rev_info *revs)
4115 while (1) {
4116 struct commit *commit;
4118 if (revs->reflog_info)
4119 commit = next_reflog_entry(revs->reflog_info);
4120 else if (revs->topo_walk_info)
4121 commit = next_topo_commit(revs);
4122 else
4123 commit = pop_commit(&revs->commits);
4125 if (!commit)
4126 return NULL;
4128 if (revs->reflog_info)
4129 commit->object.flags &= ~(ADDED | SEEN | SHOWN);
4132 * If we haven't done the list limiting, we need to look at
4133 * the parents here. We also need to do the date-based limiting
4134 * that we'd otherwise have done in limit_list().
4136 if (!revs->limited) {
4137 if (revs->max_age != -1 &&
4138 comparison_date(revs, commit) < revs->max_age)
4139 continue;
4141 if (revs->reflog_info)
4142 try_to_simplify_commit(revs, commit);
4143 else if (revs->topo_walk_info)
4144 expand_topo_walk(revs, commit);
4145 else if (process_parents(revs, commit, &revs->commits, NULL) < 0) {
4146 if (!revs->ignore_missing_links)
4147 die("Failed to traverse parents of commit %s",
4148 oid_to_hex(&commit->object.oid));
4152 switch (simplify_commit(revs, commit)) {
4153 case commit_ignore:
4154 continue;
4155 case commit_error:
4156 die("Failed to simplify parents of commit %s",
4157 oid_to_hex(&commit->object.oid));
4158 default:
4159 if (revs->track_linear)
4160 track_linear(revs, commit);
4161 return commit;
4167 * Return true for entries that have not yet been shown. (This is an
4168 * object_array_each_func_t.)
4170 static int entry_unshown(struct object_array_entry *entry, void *cb_data UNUSED)
4172 return !(entry->item->flags & SHOWN);
4176 * If array is on the verge of a realloc, garbage-collect any entries
4177 * that have already been shown to try to free up some space.
4179 static void gc_boundary(struct object_array *array)
4181 if (array->nr == array->alloc)
4182 object_array_filter(array, entry_unshown, NULL);
4185 static void create_boundary_commit_list(struct rev_info *revs)
4187 unsigned i;
4188 struct commit *c;
4189 struct object_array *array = &revs->boundary_commits;
4190 struct object_array_entry *objects = array->objects;
4193 * If revs->commits is non-NULL at this point, an error occurred in
4194 * get_revision_1(). Ignore the error and continue printing the
4195 * boundary commits anyway. (This is what the code has always
4196 * done.)
4198 free_commit_list(revs->commits);
4199 revs->commits = NULL;
4202 * Put all of the actual boundary commits from revs->boundary_commits
4203 * into revs->commits
4205 for (i = 0; i < array->nr; i++) {
4206 c = (struct commit *)(objects[i].item);
4207 if (!c)
4208 continue;
4209 if (!(c->object.flags & CHILD_SHOWN))
4210 continue;
4211 if (c->object.flags & (SHOWN | BOUNDARY))
4212 continue;
4213 c->object.flags |= BOUNDARY;
4214 commit_list_insert(c, &revs->commits);
4218 * If revs->topo_order is set, sort the boundary commits
4219 * in topological order
4221 sort_in_topological_order(&revs->commits, revs->sort_order);
4224 static struct commit *get_revision_internal(struct rev_info *revs)
4226 struct commit *c = NULL;
4227 struct commit_list *l;
4229 if (revs->boundary == 2) {
4231 * All of the normal commits have already been returned,
4232 * and we are now returning boundary commits.
4233 * create_boundary_commit_list() has populated
4234 * revs->commits with the remaining commits to return.
4236 c = pop_commit(&revs->commits);
4237 if (c)
4238 c->object.flags |= SHOWN;
4239 return c;
4243 * If our max_count counter has reached zero, then we are done. We
4244 * don't simply return NULL because we still might need to show
4245 * boundary commits. But we want to avoid calling get_revision_1, which
4246 * might do a considerable amount of work finding the next commit only
4247 * for us to throw it away.
4249 * If it is non-zero, then either we don't have a max_count at all
4250 * (-1), or it is still counting, in which case we decrement.
4252 if (revs->max_count) {
4253 c = get_revision_1(revs);
4254 if (c) {
4255 while (revs->skip_count > 0) {
4256 revs->skip_count--;
4257 c = get_revision_1(revs);
4258 if (!c)
4259 break;
4263 if (revs->max_count > 0)
4264 revs->max_count--;
4267 if (c)
4268 c->object.flags |= SHOWN;
4270 if (!revs->boundary)
4271 return c;
4273 if (!c) {
4275 * get_revision_1() runs out the commits, and
4276 * we are done computing the boundaries.
4277 * switch to boundary commits output mode.
4279 revs->boundary = 2;
4282 * Update revs->commits to contain the list of
4283 * boundary commits.
4285 create_boundary_commit_list(revs);
4287 return get_revision_internal(revs);
4291 * boundary commits are the commits that are parents of the
4292 * ones we got from get_revision_1() but they themselves are
4293 * not returned from get_revision_1(). Before returning
4294 * 'c', we need to mark its parents that they could be boundaries.
4297 for (l = c->parents; l; l = l->next) {
4298 struct object *p;
4299 p = &(l->item->object);
4300 if (p->flags & (CHILD_SHOWN | SHOWN))
4301 continue;
4302 p->flags |= CHILD_SHOWN;
4303 gc_boundary(&revs->boundary_commits);
4304 add_object_array(p, NULL, &revs->boundary_commits);
4307 return c;
4310 struct commit *get_revision(struct rev_info *revs)
4312 struct commit *c;
4313 struct commit_list *reversed;
4315 if (revs->reverse) {
4316 reversed = NULL;
4317 while ((c = get_revision_internal(revs)))
4318 commit_list_insert(c, &reversed);
4319 revs->commits = reversed;
4320 revs->reverse = 0;
4321 revs->reverse_output_stage = 1;
4324 if (revs->reverse_output_stage) {
4325 c = pop_commit(&revs->commits);
4326 if (revs->track_linear)
4327 revs->linear = !!(c && c->object.flags & TRACK_LINEAR);
4328 return c;
4331 c = get_revision_internal(revs);
4332 if (c && revs->graph)
4333 graph_update(revs->graph, c);
4334 if (!c) {
4335 free_saved_parents(revs);
4336 free_commit_list(revs->previous_parents);
4337 revs->previous_parents = NULL;
4339 return c;
4342 const char *get_revision_mark(const struct rev_info *revs, const struct commit *commit)
4344 if (commit->object.flags & BOUNDARY)
4345 return "-";
4346 else if (commit->object.flags & UNINTERESTING)
4347 return "^";
4348 else if (commit->object.flags & PATCHSAME)
4349 return "=";
4350 else if (!revs || revs->left_right) {
4351 if (commit->object.flags & SYMMETRIC_LEFT)
4352 return "<";
4353 else
4354 return ">";
4355 } else if (revs->graph)
4356 return "*";
4357 else if (revs->cherry_mark)
4358 return "+";
4359 return "";
4362 void put_revision_mark(const struct rev_info *revs, const struct commit *commit)
4364 const char *mark = get_revision_mark(revs, commit);
4365 if (!strlen(mark))
4366 return;
4367 fputs(mark, stdout);
4368 putchar(' ');