Merge branch 'ps/remote-helper-repo-initialization-fix'
[git/gitster.git] / revision.c
blob0b18b3aa5fa5609ed073aa9d7fcaff80b5e0a206
1 #include "git-compat-util.h"
2 #include "config.h"
3 #include "environment.h"
4 #include "gettext.h"
5 #include "hex.h"
6 #include "object-name.h"
7 #include "object-file.h"
8 #include "object-store-ll.h"
9 #include "oidset.h"
10 #include "tag.h"
11 #include "blob.h"
12 #include "tree.h"
13 #include "commit.h"
14 #include "diff.h"
15 #include "diff-merges.h"
16 #include "refs.h"
17 #include "revision.h"
18 #include "repository.h"
19 #include "graph.h"
20 #include "grep.h"
21 #include "reflog-walk.h"
22 #include "patch-ids.h"
23 #include "decorate.h"
24 #include "string-list.h"
25 #include "line-log.h"
26 #include "mailmap.h"
27 #include "commit-slab.h"
28 #include "cache-tree.h"
29 #include "bisect.h"
30 #include "packfile.h"
31 #include "worktree.h"
32 #include "read-cache.h"
33 #include "setup.h"
34 #include "sparse-index.h"
35 #include "strvec.h"
36 #include "trace2.h"
37 #include "commit-reach.h"
38 #include "commit-graph.h"
39 #include "prio-queue.h"
40 #include "hashmap.h"
41 #include "utf8.h"
42 #include "bloom.h"
43 #include "json-writer.h"
44 #include "list-objects-filter-options.h"
45 #include "resolve-undo.h"
46 #include "parse-options.h"
47 #include "wildmatch.h"
49 volatile show_early_output_fn_t show_early_output;
51 static const char *term_bad;
52 static const char *term_good;
54 implement_shared_commit_slab(revision_sources, char *);
56 static inline int want_ancestry(const struct rev_info *revs);
58 void show_object_with_name(FILE *out, struct object *obj, const char *name)
60 fprintf(out, "%s ", oid_to_hex(&obj->oid));
61 for (const char *p = name; *p && *p != '\n'; p++)
62 fputc(*p, out);
63 fputc('\n', out);
66 static void mark_blob_uninteresting(struct blob *blob)
68 if (!blob)
69 return;
70 if (blob->object.flags & UNINTERESTING)
71 return;
72 blob->object.flags |= UNINTERESTING;
75 static void mark_tree_contents_uninteresting(struct repository *r,
76 struct tree *tree)
78 struct tree_desc desc;
79 struct name_entry entry;
81 if (parse_tree_gently(tree, 1) < 0)
82 return;
84 init_tree_desc(&desc, tree->buffer, tree->size);
85 while (tree_entry(&desc, &entry)) {
86 switch (object_type(entry.mode)) {
87 case OBJ_TREE:
88 mark_tree_uninteresting(r, lookup_tree(r, &entry.oid));
89 break;
90 case OBJ_BLOB:
91 mark_blob_uninteresting(lookup_blob(r, &entry.oid));
92 break;
93 default:
94 /* Subproject commit - not in this repository */
95 break;
100 * We don't care about the tree any more
101 * after it has been marked uninteresting.
103 free_tree_buffer(tree);
106 void mark_tree_uninteresting(struct repository *r, struct tree *tree)
108 struct object *obj;
110 if (!tree)
111 return;
113 obj = &tree->object;
114 if (obj->flags & UNINTERESTING)
115 return;
116 obj->flags |= UNINTERESTING;
117 mark_tree_contents_uninteresting(r, tree);
120 struct path_and_oids_entry {
121 struct hashmap_entry ent;
122 char *path;
123 struct oidset trees;
126 static int path_and_oids_cmp(const void *hashmap_cmp_fn_data UNUSED,
127 const struct hashmap_entry *eptr,
128 const struct hashmap_entry *entry_or_key,
129 const void *keydata UNUSED)
131 const struct path_and_oids_entry *e1, *e2;
133 e1 = container_of(eptr, const struct path_and_oids_entry, ent);
134 e2 = container_of(entry_or_key, const struct path_and_oids_entry, ent);
136 return strcmp(e1->path, e2->path);
139 static void paths_and_oids_clear(struct hashmap *map)
141 struct hashmap_iter iter;
142 struct path_and_oids_entry *entry;
144 hashmap_for_each_entry(map, &iter, entry, ent /* member name */) {
145 oidset_clear(&entry->trees);
146 free(entry->path);
149 hashmap_clear_and_free(map, struct path_and_oids_entry, ent);
152 static void paths_and_oids_insert(struct hashmap *map,
153 const char *path,
154 const struct object_id *oid)
156 int hash = strhash(path);
157 struct path_and_oids_entry key;
158 struct path_and_oids_entry *entry;
160 hashmap_entry_init(&key.ent, hash);
162 /* use a shallow copy for the lookup */
163 key.path = (char *)path;
164 oidset_init(&key.trees, 0);
166 entry = hashmap_get_entry(map, &key, ent, NULL);
167 if (!entry) {
168 CALLOC_ARRAY(entry, 1);
169 hashmap_entry_init(&entry->ent, hash);
170 entry->path = xstrdup(key.path);
171 oidset_init(&entry->trees, 16);
172 hashmap_put(map, &entry->ent);
175 oidset_insert(&entry->trees, oid);
178 static void add_children_by_path(struct repository *r,
179 struct tree *tree,
180 struct hashmap *map)
182 struct tree_desc desc;
183 struct name_entry entry;
185 if (!tree)
186 return;
188 if (parse_tree_gently(tree, 1) < 0)
189 return;
191 init_tree_desc(&desc, tree->buffer, tree->size);
192 while (tree_entry(&desc, &entry)) {
193 switch (object_type(entry.mode)) {
194 case OBJ_TREE:
195 paths_and_oids_insert(map, entry.path, &entry.oid);
197 if (tree->object.flags & UNINTERESTING) {
198 struct tree *child = lookup_tree(r, &entry.oid);
199 if (child)
200 child->object.flags |= UNINTERESTING;
202 break;
203 case OBJ_BLOB:
204 if (tree->object.flags & UNINTERESTING) {
205 struct blob *child = lookup_blob(r, &entry.oid);
206 if (child)
207 child->object.flags |= UNINTERESTING;
209 break;
210 default:
211 /* Subproject commit - not in this repository */
212 break;
216 free_tree_buffer(tree);
219 void mark_trees_uninteresting_sparse(struct repository *r,
220 struct oidset *trees)
222 unsigned has_interesting = 0, has_uninteresting = 0;
223 struct hashmap map = HASHMAP_INIT(path_and_oids_cmp, NULL);
224 struct hashmap_iter map_iter;
225 struct path_and_oids_entry *entry;
226 struct object_id *oid;
227 struct oidset_iter iter;
229 oidset_iter_init(trees, &iter);
230 while ((!has_interesting || !has_uninteresting) &&
231 (oid = oidset_iter_next(&iter))) {
232 struct tree *tree = lookup_tree(r, oid);
234 if (!tree)
235 continue;
237 if (tree->object.flags & UNINTERESTING)
238 has_uninteresting = 1;
239 else
240 has_interesting = 1;
243 /* Do not walk unless we have both types of trees. */
244 if (!has_uninteresting || !has_interesting)
245 return;
247 oidset_iter_init(trees, &iter);
248 while ((oid = oidset_iter_next(&iter))) {
249 struct tree *tree = lookup_tree(r, oid);
250 add_children_by_path(r, tree, &map);
253 hashmap_for_each_entry(&map, &map_iter, entry, ent /* member name */)
254 mark_trees_uninteresting_sparse(r, &entry->trees);
256 paths_and_oids_clear(&map);
259 struct commit_stack {
260 struct commit **items;
261 size_t nr, alloc;
263 #define COMMIT_STACK_INIT { 0 }
265 static void commit_stack_push(struct commit_stack *stack, struct commit *commit)
267 ALLOC_GROW(stack->items, stack->nr + 1, stack->alloc);
268 stack->items[stack->nr++] = commit;
271 static struct commit *commit_stack_pop(struct commit_stack *stack)
273 return stack->nr ? stack->items[--stack->nr] : NULL;
276 static void commit_stack_clear(struct commit_stack *stack)
278 FREE_AND_NULL(stack->items);
279 stack->nr = stack->alloc = 0;
282 static void mark_one_parent_uninteresting(struct rev_info *revs, struct commit *commit,
283 struct commit_stack *pending)
285 struct commit_list *l;
287 if (commit->object.flags & UNINTERESTING)
288 return;
289 commit->object.flags |= UNINTERESTING;
292 * Normally we haven't parsed the parent
293 * yet, so we won't have a parent of a parent
294 * here. However, it may turn out that we've
295 * reached this commit some other way (where it
296 * wasn't uninteresting), in which case we need
297 * to mark its parents recursively too..
299 for (l = commit->parents; l; l = l->next) {
300 commit_stack_push(pending, l->item);
301 if (revs && revs->exclude_first_parent_only)
302 break;
306 void mark_parents_uninteresting(struct rev_info *revs, struct commit *commit)
308 struct commit_stack pending = COMMIT_STACK_INIT;
309 struct commit_list *l;
311 for (l = commit->parents; l; l = l->next) {
312 mark_one_parent_uninteresting(revs, l->item, &pending);
313 if (revs && revs->exclude_first_parent_only)
314 break;
317 while (pending.nr > 0)
318 mark_one_parent_uninteresting(revs, commit_stack_pop(&pending),
319 &pending);
321 commit_stack_clear(&pending);
324 static void add_pending_object_with_path(struct rev_info *revs,
325 struct object *obj,
326 const char *name, unsigned mode,
327 const char *path)
329 struct interpret_branch_name_options options = { 0 };
330 if (!obj)
331 return;
332 if (revs->no_walk && (obj->flags & UNINTERESTING))
333 revs->no_walk = 0;
334 if (revs->reflog_info && obj->type == OBJ_COMMIT) {
335 struct strbuf buf = STRBUF_INIT;
336 size_t namelen = strlen(name);
337 int len = repo_interpret_branch_name(the_repository, name,
338 namelen, &buf, &options);
340 if (0 < len && len < namelen && buf.len)
341 strbuf_addstr(&buf, name + len);
342 add_reflog_for_walk(revs->reflog_info,
343 (struct commit *)obj,
344 buf.buf[0] ? buf.buf: name);
345 strbuf_release(&buf);
346 return; /* do not add the commit itself */
348 add_object_array_with_path(obj, name, &revs->pending, mode, path);
351 static void add_pending_object_with_mode(struct rev_info *revs,
352 struct object *obj,
353 const char *name, unsigned mode)
355 add_pending_object_with_path(revs, obj, name, mode, NULL);
358 void add_pending_object(struct rev_info *revs,
359 struct object *obj, const char *name)
361 add_pending_object_with_mode(revs, obj, name, S_IFINVALID);
364 void add_head_to_pending(struct rev_info *revs)
366 struct object_id oid;
367 struct object *obj;
368 if (repo_get_oid(the_repository, "HEAD", &oid))
369 return;
370 obj = parse_object(revs->repo, &oid);
371 if (!obj)
372 return;
373 add_pending_object(revs, obj, "HEAD");
376 static struct object *get_reference(struct rev_info *revs, const char *name,
377 const struct object_id *oid,
378 unsigned int flags)
380 struct object *object;
382 object = parse_object_with_flags(revs->repo, oid,
383 revs->verify_objects ? 0 :
384 PARSE_OBJECT_SKIP_HASH_CHECK);
386 if (!object) {
387 if (revs->ignore_missing)
388 return NULL;
389 if (revs->exclude_promisor_objects && is_promisor_object(oid))
390 return NULL;
391 if (revs->do_not_die_on_missing_objects) {
392 oidset_insert(&revs->missing_commits, oid);
393 return NULL;
395 die("bad object %s", name);
397 object->flags |= flags;
398 return object;
401 void add_pending_oid(struct rev_info *revs, const char *name,
402 const struct object_id *oid, unsigned int flags)
404 struct object *object = get_reference(revs, name, oid, flags);
405 add_pending_object(revs, object, name);
408 static struct commit *handle_commit(struct rev_info *revs,
409 struct object_array_entry *entry)
411 struct object *object = entry->item;
412 const char *name = entry->name;
413 const char *path = entry->path;
414 unsigned int mode = entry->mode;
415 unsigned long flags = object->flags;
418 * Tag object? Look what it points to..
420 while (object->type == OBJ_TAG) {
421 struct tag *tag = (struct tag *) object;
422 struct object_id *oid;
423 if (revs->tag_objects && !(flags & UNINTERESTING))
424 add_pending_object(revs, object, tag->tag);
425 oid = get_tagged_oid(tag);
426 object = parse_object(revs->repo, oid);
427 if (!object) {
428 if (revs->ignore_missing_links || (flags & UNINTERESTING))
429 return NULL;
430 if (revs->exclude_promisor_objects &&
431 is_promisor_object(&tag->tagged->oid))
432 return NULL;
433 if (revs->do_not_die_on_missing_objects && oid) {
434 oidset_insert(&revs->missing_commits, oid);
435 return NULL;
437 die("bad object %s", oid_to_hex(&tag->tagged->oid));
439 object->flags |= flags;
441 * We'll handle the tagged object by looping or dropping
442 * through to the non-tag handlers below. Do not
443 * propagate path data from the tag's pending entry.
445 path = NULL;
446 mode = 0;
450 * Commit object? Just return it, we'll do all the complex
451 * reachability crud.
453 if (object->type == OBJ_COMMIT) {
454 struct commit *commit = (struct commit *)object;
456 if (repo_parse_commit(revs->repo, commit) < 0)
457 die("unable to parse commit %s", name);
458 if (flags & UNINTERESTING) {
459 mark_parents_uninteresting(revs, commit);
461 if (!revs->topo_order || !generation_numbers_enabled(the_repository))
462 revs->limited = 1;
464 if (revs->sources) {
465 char **slot = revision_sources_at(revs->sources, commit);
467 if (!*slot)
468 *slot = xstrdup(name);
470 return commit;
474 * Tree object? Either mark it uninteresting, or add it
475 * to the list of objects to look at later..
477 if (object->type == OBJ_TREE) {
478 struct tree *tree = (struct tree *)object;
479 if (!revs->tree_objects)
480 return NULL;
481 if (flags & UNINTERESTING) {
482 mark_tree_contents_uninteresting(revs->repo, tree);
483 return NULL;
485 add_pending_object_with_path(revs, object, name, mode, path);
486 return NULL;
490 * Blob object? You know the drill by now..
492 if (object->type == OBJ_BLOB) {
493 if (!revs->blob_objects)
494 return NULL;
495 if (flags & UNINTERESTING)
496 return NULL;
497 add_pending_object_with_path(revs, object, name, mode, path);
498 return NULL;
500 die("%s is unknown object", name);
503 static int everybody_uninteresting(struct commit_list *orig,
504 struct commit **interesting_cache)
506 struct commit_list *list = orig;
508 if (*interesting_cache) {
509 struct commit *commit = *interesting_cache;
510 if (!(commit->object.flags & UNINTERESTING))
511 return 0;
514 while (list) {
515 struct commit *commit = list->item;
516 list = list->next;
517 if (commit->object.flags & UNINTERESTING)
518 continue;
520 *interesting_cache = commit;
521 return 0;
523 return 1;
527 * A definition of "relevant" commit that we can use to simplify limited graphs
528 * by eliminating side branches.
530 * A "relevant" commit is one that is !UNINTERESTING (ie we are including it
531 * in our list), or that is a specified BOTTOM commit. Then after computing
532 * a limited list, during processing we can generally ignore boundary merges
533 * coming from outside the graph, (ie from irrelevant parents), and treat
534 * those merges as if they were single-parent. TREESAME is defined to consider
535 * only relevant parents, if any. If we are TREESAME to our on-graph parents,
536 * we don't care if we were !TREESAME to non-graph parents.
538 * Treating bottom commits as relevant ensures that a limited graph's
539 * connection to the actual bottom commit is not viewed as a side branch, but
540 * treated as part of the graph. For example:
542 * ....Z...A---X---o---o---B
543 * . /
544 * W---Y
546 * When computing "A..B", the A-X connection is at least as important as
547 * Y-X, despite A being flagged UNINTERESTING.
549 * And when computing --ancestry-path "A..B", the A-X connection is more
550 * important than Y-X, despite both A and Y being flagged UNINTERESTING.
552 static inline int relevant_commit(struct commit *commit)
554 return (commit->object.flags & (UNINTERESTING | BOTTOM)) != UNINTERESTING;
558 * Return a single relevant commit from a parent list. If we are a TREESAME
559 * commit, and this selects one of our parents, then we can safely simplify to
560 * that parent.
562 static struct commit *one_relevant_parent(const struct rev_info *revs,
563 struct commit_list *orig)
565 struct commit_list *list = orig;
566 struct commit *relevant = NULL;
568 if (!orig)
569 return NULL;
572 * For 1-parent commits, or if first-parent-only, then return that
573 * first parent (even if not "relevant" by the above definition).
574 * TREESAME will have been set purely on that parent.
576 if (revs->first_parent_only || !orig->next)
577 return orig->item;
580 * For multi-parent commits, identify a sole relevant parent, if any.
581 * If we have only one relevant parent, then TREESAME will be set purely
582 * with regard to that parent, and we can simplify accordingly.
584 * If we have more than one relevant parent, or no relevant parents
585 * (and multiple irrelevant ones), then we can't select a parent here
586 * and return NULL.
588 while (list) {
589 struct commit *commit = list->item;
590 list = list->next;
591 if (relevant_commit(commit)) {
592 if (relevant)
593 return NULL;
594 relevant = commit;
597 return relevant;
601 * The goal is to get REV_TREE_NEW as the result only if the
602 * diff consists of all '+' (and no other changes), REV_TREE_OLD
603 * if the whole diff is removal of old data, and otherwise
604 * REV_TREE_DIFFERENT (of course if the trees are the same we
605 * want REV_TREE_SAME).
607 * The only time we care about the distinction is when
608 * remove_empty_trees is in effect, in which case we care only about
609 * whether the whole change is REV_TREE_NEW, or if there's another type
610 * of change. Which means we can stop the diff early in either of these
611 * cases:
613 * 1. We're not using remove_empty_trees at all.
615 * 2. We saw anything except REV_TREE_NEW.
617 #define REV_TREE_SAME 0
618 #define REV_TREE_NEW 1 /* Only new files */
619 #define REV_TREE_OLD 2 /* Only files removed */
620 #define REV_TREE_DIFFERENT 3 /* Mixed changes */
621 static int tree_difference = REV_TREE_SAME;
623 static void file_add_remove(struct diff_options *options,
624 int addremove,
625 unsigned mode UNUSED,
626 const struct object_id *oid UNUSED,
627 int oid_valid UNUSED,
628 const char *fullpath UNUSED,
629 unsigned dirty_submodule UNUSED)
631 int diff = addremove == '+' ? REV_TREE_NEW : REV_TREE_OLD;
632 struct rev_info *revs = options->change_fn_data;
634 tree_difference |= diff;
635 if (!revs->remove_empty_trees || tree_difference != REV_TREE_NEW)
636 options->flags.has_changes = 1;
639 static void file_change(struct diff_options *options,
640 unsigned old_mode UNUSED,
641 unsigned new_mode UNUSED,
642 const struct object_id *old_oid UNUSED,
643 const struct object_id *new_oid UNUSED,
644 int old_oid_valid UNUSED,
645 int new_oid_valid UNUSED,
646 const char *fullpath UNUSED,
647 unsigned old_dirty_submodule UNUSED,
648 unsigned new_dirty_submodule UNUSED)
650 tree_difference = REV_TREE_DIFFERENT;
651 options->flags.has_changes = 1;
654 static int bloom_filter_atexit_registered;
655 static unsigned int count_bloom_filter_maybe;
656 static unsigned int count_bloom_filter_definitely_not;
657 static unsigned int count_bloom_filter_false_positive;
658 static unsigned int count_bloom_filter_not_present;
660 static void trace2_bloom_filter_statistics_atexit(void)
662 struct json_writer jw = JSON_WRITER_INIT;
664 jw_object_begin(&jw, 0);
665 jw_object_intmax(&jw, "filter_not_present", count_bloom_filter_not_present);
666 jw_object_intmax(&jw, "maybe", count_bloom_filter_maybe);
667 jw_object_intmax(&jw, "definitely_not", count_bloom_filter_definitely_not);
668 jw_object_intmax(&jw, "false_positive", count_bloom_filter_false_positive);
669 jw_end(&jw);
671 trace2_data_json("bloom", the_repository, "statistics", &jw);
673 jw_release(&jw);
676 static int forbid_bloom_filters(struct pathspec *spec)
678 if (spec->has_wildcard)
679 return 1;
680 if (spec->nr > 1)
681 return 1;
682 if (spec->magic & ~PATHSPEC_LITERAL)
683 return 1;
684 if (spec->nr && (spec->items[0].magic & ~PATHSPEC_LITERAL))
685 return 1;
687 return 0;
690 static void prepare_to_use_bloom_filter(struct rev_info *revs)
692 struct pathspec_item *pi;
693 char *path_alloc = NULL;
694 const char *path, *p;
695 size_t len;
696 int path_component_nr = 1;
698 if (!revs->commits)
699 return;
701 if (forbid_bloom_filters(&revs->prune_data))
702 return;
704 repo_parse_commit(revs->repo, revs->commits->item);
706 revs->bloom_filter_settings = get_bloom_filter_settings(revs->repo);
707 if (!revs->bloom_filter_settings)
708 return;
710 if (!revs->pruning.pathspec.nr)
711 return;
713 pi = &revs->pruning.pathspec.items[0];
715 /* remove single trailing slash from path, if needed */
716 if (pi->len > 0 && pi->match[pi->len - 1] == '/') {
717 path_alloc = xmemdupz(pi->match, pi->len - 1);
718 path = path_alloc;
719 } else
720 path = pi->match;
722 len = strlen(path);
723 if (!len) {
724 revs->bloom_filter_settings = NULL;
725 free(path_alloc);
726 return;
729 p = path;
730 while (*p) {
732 * At this point, the path is normalized to use Unix-style
733 * path separators. This is required due to how the
734 * changed-path Bloom filters store the paths.
736 if (*p == '/')
737 path_component_nr++;
738 p++;
741 revs->bloom_keys_nr = path_component_nr;
742 ALLOC_ARRAY(revs->bloom_keys, revs->bloom_keys_nr);
744 fill_bloom_key(path, len, &revs->bloom_keys[0],
745 revs->bloom_filter_settings);
746 path_component_nr = 1;
748 p = path + len - 1;
749 while (p > path) {
750 if (*p == '/')
751 fill_bloom_key(path, p - path,
752 &revs->bloom_keys[path_component_nr++],
753 revs->bloom_filter_settings);
754 p--;
757 if (trace2_is_enabled() && !bloom_filter_atexit_registered) {
758 atexit(trace2_bloom_filter_statistics_atexit);
759 bloom_filter_atexit_registered = 1;
762 free(path_alloc);
765 static int check_maybe_different_in_bloom_filter(struct rev_info *revs,
766 struct commit *commit)
768 struct bloom_filter *filter;
769 int result = 1, j;
771 if (!revs->repo->objects->commit_graph)
772 return -1;
774 if (commit_graph_generation(commit) == GENERATION_NUMBER_INFINITY)
775 return -1;
777 filter = get_bloom_filter(revs->repo, commit);
779 if (!filter) {
780 count_bloom_filter_not_present++;
781 return -1;
784 for (j = 0; result && j < revs->bloom_keys_nr; j++) {
785 result = bloom_filter_contains(filter,
786 &revs->bloom_keys[j],
787 revs->bloom_filter_settings);
790 if (result)
791 count_bloom_filter_maybe++;
792 else
793 count_bloom_filter_definitely_not++;
795 return result;
798 static int rev_compare_tree(struct rev_info *revs,
799 struct commit *parent, struct commit *commit, int nth_parent)
801 struct tree *t1 = repo_get_commit_tree(the_repository, parent);
802 struct tree *t2 = repo_get_commit_tree(the_repository, commit);
803 int bloom_ret = 1;
805 if (!t1)
806 return REV_TREE_NEW;
807 if (!t2)
808 return REV_TREE_OLD;
810 if (revs->simplify_by_decoration) {
812 * If we are simplifying by decoration, then the commit
813 * is worth showing if it has a tag pointing at it.
815 if (get_name_decoration(&commit->object))
816 return REV_TREE_DIFFERENT;
818 * A commit that is not pointed by a tag is uninteresting
819 * if we are not limited by path. This means that you will
820 * see the usual "commits that touch the paths" plus any
821 * tagged commit by specifying both --simplify-by-decoration
822 * and pathspec.
824 if (!revs->prune_data.nr)
825 return REV_TREE_SAME;
828 if (revs->bloom_keys_nr && !nth_parent) {
829 bloom_ret = check_maybe_different_in_bloom_filter(revs, commit);
831 if (bloom_ret == 0)
832 return REV_TREE_SAME;
835 tree_difference = REV_TREE_SAME;
836 revs->pruning.flags.has_changes = 0;
837 diff_tree_oid(&t1->object.oid, &t2->object.oid, "", &revs->pruning);
839 if (!nth_parent)
840 if (bloom_ret == 1 && tree_difference == REV_TREE_SAME)
841 count_bloom_filter_false_positive++;
843 return tree_difference;
846 static int rev_same_tree_as_empty(struct rev_info *revs, struct commit *commit)
848 struct tree *t1 = repo_get_commit_tree(the_repository, commit);
850 if (!t1)
851 return 0;
853 tree_difference = REV_TREE_SAME;
854 revs->pruning.flags.has_changes = 0;
855 diff_tree_oid(NULL, &t1->object.oid, "", &revs->pruning);
857 return tree_difference == REV_TREE_SAME;
860 struct treesame_state {
861 unsigned int nparents;
862 unsigned char treesame[FLEX_ARRAY];
865 static struct treesame_state *initialise_treesame(struct rev_info *revs, struct commit *commit)
867 unsigned n = commit_list_count(commit->parents);
868 struct treesame_state *st = xcalloc(1, st_add(sizeof(*st), n));
869 st->nparents = n;
870 add_decoration(&revs->treesame, &commit->object, st);
871 return st;
875 * Must be called immediately after removing the nth_parent from a commit's
876 * parent list, if we are maintaining the per-parent treesame[] decoration.
877 * This does not recalculate the master TREESAME flag - update_treesame()
878 * should be called to update it after a sequence of treesame[] modifications
879 * that may have affected it.
881 static int compact_treesame(struct rev_info *revs, struct commit *commit, unsigned nth_parent)
883 struct treesame_state *st;
884 int old_same;
886 if (!commit->parents) {
888 * Have just removed the only parent from a non-merge.
889 * Different handling, as we lack decoration.
891 if (nth_parent != 0)
892 die("compact_treesame %u", nth_parent);
893 old_same = !!(commit->object.flags & TREESAME);
894 if (rev_same_tree_as_empty(revs, commit))
895 commit->object.flags |= TREESAME;
896 else
897 commit->object.flags &= ~TREESAME;
898 return old_same;
901 st = lookup_decoration(&revs->treesame, &commit->object);
902 if (!st || nth_parent >= st->nparents)
903 die("compact_treesame %u", nth_parent);
905 old_same = st->treesame[nth_parent];
906 memmove(st->treesame + nth_parent,
907 st->treesame + nth_parent + 1,
908 st->nparents - nth_parent - 1);
911 * If we've just become a non-merge commit, update TREESAME
912 * immediately, and remove the no-longer-needed decoration.
913 * If still a merge, defer update until update_treesame().
915 if (--st->nparents == 1) {
916 if (commit->parents->next)
917 die("compact_treesame parents mismatch");
918 if (st->treesame[0] && revs->dense)
919 commit->object.flags |= TREESAME;
920 else
921 commit->object.flags &= ~TREESAME;
922 free(add_decoration(&revs->treesame, &commit->object, NULL));
925 return old_same;
928 static unsigned update_treesame(struct rev_info *revs, struct commit *commit)
930 if (commit->parents && commit->parents->next) {
931 unsigned n;
932 struct treesame_state *st;
933 struct commit_list *p;
934 unsigned relevant_parents;
935 unsigned relevant_change, irrelevant_change;
937 st = lookup_decoration(&revs->treesame, &commit->object);
938 if (!st)
939 die("update_treesame %s", oid_to_hex(&commit->object.oid));
940 relevant_parents = 0;
941 relevant_change = irrelevant_change = 0;
942 for (p = commit->parents, n = 0; p; n++, p = p->next) {
943 if (relevant_commit(p->item)) {
944 relevant_change |= !st->treesame[n];
945 relevant_parents++;
946 } else
947 irrelevant_change |= !st->treesame[n];
949 if (relevant_parents ? relevant_change : irrelevant_change)
950 commit->object.flags &= ~TREESAME;
951 else
952 commit->object.flags |= TREESAME;
955 return commit->object.flags & TREESAME;
958 static inline int limiting_can_increase_treesame(const struct rev_info *revs)
961 * TREESAME is irrelevant unless prune && dense;
962 * if simplify_history is set, we can't have a mixture of TREESAME and
963 * !TREESAME INTERESTING parents (and we don't have treesame[]
964 * decoration anyway);
965 * if first_parent_only is set, then the TREESAME flag is locked
966 * against the first parent (and again we lack treesame[] decoration).
968 return revs->prune && revs->dense &&
969 !revs->simplify_history &&
970 !revs->first_parent_only;
973 static void try_to_simplify_commit(struct rev_info *revs, struct commit *commit)
975 struct commit_list **pp, *parent;
976 struct treesame_state *ts = NULL;
977 int relevant_change = 0, irrelevant_change = 0;
978 int relevant_parents, nth_parent;
981 * If we don't do pruning, everything is interesting
983 if (!revs->prune)
984 return;
986 if (!repo_get_commit_tree(the_repository, commit))
987 return;
989 if (!commit->parents) {
990 if (rev_same_tree_as_empty(revs, commit))
991 commit->object.flags |= TREESAME;
992 return;
996 * Normal non-merge commit? If we don't want to make the
997 * history dense, we consider it always to be a change..
999 if (!revs->dense && !commit->parents->next)
1000 return;
1002 for (pp = &commit->parents, nth_parent = 0, relevant_parents = 0;
1003 (parent = *pp) != NULL;
1004 pp = &parent->next, nth_parent++) {
1005 struct commit *p = parent->item;
1006 if (relevant_commit(p))
1007 relevant_parents++;
1009 if (nth_parent == 1) {
1011 * This our second loop iteration - so we now know
1012 * we're dealing with a merge.
1014 * Do not compare with later parents when we care only about
1015 * the first parent chain, in order to avoid derailing the
1016 * traversal to follow a side branch that brought everything
1017 * in the path we are limited to by the pathspec.
1019 if (revs->first_parent_only)
1020 break;
1022 * If this will remain a potentially-simplifiable
1023 * merge, remember per-parent treesame if needed.
1024 * Initialise the array with the comparison from our
1025 * first iteration.
1027 if (revs->treesame.name &&
1028 !revs->simplify_history &&
1029 !(commit->object.flags & UNINTERESTING)) {
1030 ts = initialise_treesame(revs, commit);
1031 if (!(irrelevant_change || relevant_change))
1032 ts->treesame[0] = 1;
1035 if (repo_parse_commit(revs->repo, p) < 0)
1036 die("cannot simplify commit %s (because of %s)",
1037 oid_to_hex(&commit->object.oid),
1038 oid_to_hex(&p->object.oid));
1039 switch (rev_compare_tree(revs, p, commit, nth_parent)) {
1040 case REV_TREE_SAME:
1041 if (!revs->simplify_history || !relevant_commit(p)) {
1042 /* Even if a merge with an uninteresting
1043 * side branch brought the entire change
1044 * we are interested in, we do not want
1045 * to lose the other branches of this
1046 * merge, so we just keep going.
1048 if (ts)
1049 ts->treesame[nth_parent] = 1;
1050 continue;
1052 parent->next = NULL;
1053 commit->parents = parent;
1056 * A merge commit is a "diversion" if it is not
1057 * TREESAME to its first parent but is TREESAME
1058 * to a later parent. In the simplified history,
1059 * we "divert" the history walk to the later
1060 * parent. These commits are shown when "show_pulls"
1061 * is enabled, so do not mark the object as
1062 * TREESAME here.
1064 if (!revs->show_pulls || !nth_parent)
1065 commit->object.flags |= TREESAME;
1067 return;
1069 case REV_TREE_NEW:
1070 if (revs->remove_empty_trees &&
1071 rev_same_tree_as_empty(revs, p)) {
1072 /* We are adding all the specified
1073 * paths from this parent, so the
1074 * history beyond this parent is not
1075 * interesting. Remove its parents
1076 * (they are grandparents for us).
1077 * IOW, we pretend this parent is a
1078 * "root" commit.
1080 if (repo_parse_commit(revs->repo, p) < 0)
1081 die("cannot simplify commit %s (invalid %s)",
1082 oid_to_hex(&commit->object.oid),
1083 oid_to_hex(&p->object.oid));
1084 p->parents = NULL;
1086 /* fallthrough */
1087 case REV_TREE_OLD:
1088 case REV_TREE_DIFFERENT:
1089 if (relevant_commit(p))
1090 relevant_change = 1;
1091 else
1092 irrelevant_change = 1;
1094 if (!nth_parent)
1095 commit->object.flags |= PULL_MERGE;
1097 continue;
1099 die("bad tree compare for commit %s", oid_to_hex(&commit->object.oid));
1103 * TREESAME is straightforward for single-parent commits. For merge
1104 * commits, it is most useful to define it so that "irrelevant"
1105 * parents cannot make us !TREESAME - if we have any relevant
1106 * parents, then we only consider TREESAMEness with respect to them,
1107 * allowing irrelevant merges from uninteresting branches to be
1108 * simplified away. Only if we have only irrelevant parents do we
1109 * base TREESAME on them. Note that this logic is replicated in
1110 * update_treesame, which should be kept in sync.
1112 if (relevant_parents ? !relevant_change : !irrelevant_change)
1113 commit->object.flags |= TREESAME;
1116 static int process_parents(struct rev_info *revs, struct commit *commit,
1117 struct commit_list **list, struct prio_queue *queue)
1119 struct commit_list *parent = commit->parents;
1120 unsigned pass_flags;
1122 if (commit->object.flags & ADDED)
1123 return 0;
1124 if (revs->do_not_die_on_missing_objects &&
1125 oidset_contains(&revs->missing_commits, &commit->object.oid))
1126 return 0;
1127 commit->object.flags |= ADDED;
1129 if (revs->include_check &&
1130 !revs->include_check(commit, revs->include_check_data))
1131 return 0;
1134 * If the commit is uninteresting, don't try to
1135 * prune parents - we want the maximal uninteresting
1136 * set.
1138 * Normally we haven't parsed the parent
1139 * yet, so we won't have a parent of a parent
1140 * here. However, it may turn out that we've
1141 * reached this commit some other way (where it
1142 * wasn't uninteresting), in which case we need
1143 * to mark its parents recursively too..
1145 if (commit->object.flags & UNINTERESTING) {
1146 while (parent) {
1147 struct commit *p = parent->item;
1148 parent = parent->next;
1149 if (p)
1150 p->object.flags |= UNINTERESTING;
1151 if (repo_parse_commit_gently(revs->repo, p, 1) < 0)
1152 continue;
1153 if (p->parents)
1154 mark_parents_uninteresting(revs, p);
1155 if (p->object.flags & SEEN)
1156 continue;
1157 p->object.flags |= (SEEN | NOT_USER_GIVEN);
1158 if (list)
1159 commit_list_insert_by_date(p, list);
1160 if (queue)
1161 prio_queue_put(queue, p);
1162 if (revs->exclude_first_parent_only)
1163 break;
1165 return 0;
1169 * Ok, the commit wasn't uninteresting. Try to
1170 * simplify the commit history and find the parent
1171 * that has no differences in the path set if one exists.
1173 try_to_simplify_commit(revs, commit);
1175 if (revs->no_walk)
1176 return 0;
1178 pass_flags = (commit->object.flags & (SYMMETRIC_LEFT | ANCESTRY_PATH));
1180 for (parent = commit->parents; parent; parent = parent->next) {
1181 struct commit *p = parent->item;
1182 int gently = revs->ignore_missing_links ||
1183 revs->exclude_promisor_objects ||
1184 revs->do_not_die_on_missing_objects;
1185 if (repo_parse_commit_gently(revs->repo, p, gently) < 0) {
1186 if (revs->exclude_promisor_objects &&
1187 is_promisor_object(&p->object.oid)) {
1188 if (revs->first_parent_only)
1189 break;
1190 continue;
1193 if (revs->do_not_die_on_missing_objects)
1194 oidset_insert(&revs->missing_commits, &p->object.oid);
1195 else
1196 return -1; /* corrupt repository */
1198 if (revs->sources) {
1199 char **slot = revision_sources_at(revs->sources, p);
1201 if (!*slot)
1202 *slot = *revision_sources_at(revs->sources, commit);
1204 p->object.flags |= pass_flags;
1205 if (!(p->object.flags & SEEN)) {
1206 p->object.flags |= (SEEN | NOT_USER_GIVEN);
1207 if (list)
1208 commit_list_insert_by_date(p, list);
1209 if (queue)
1210 prio_queue_put(queue, p);
1212 if (revs->first_parent_only)
1213 break;
1215 return 0;
1218 static void cherry_pick_list(struct commit_list *list, struct rev_info *revs)
1220 struct commit_list *p;
1221 int left_count = 0, right_count = 0;
1222 int left_first;
1223 struct patch_ids ids;
1224 unsigned cherry_flag;
1226 /* First count the commits on the left and on the right */
1227 for (p = list; p; p = p->next) {
1228 struct commit *commit = p->item;
1229 unsigned flags = commit->object.flags;
1230 if (flags & BOUNDARY)
1232 else if (flags & SYMMETRIC_LEFT)
1233 left_count++;
1234 else
1235 right_count++;
1238 if (!left_count || !right_count)
1239 return;
1241 left_first = left_count < right_count;
1242 init_patch_ids(revs->repo, &ids);
1243 ids.diffopts.pathspec = revs->diffopt.pathspec;
1245 /* Compute patch-ids for one side */
1246 for (p = list; p; p = p->next) {
1247 struct commit *commit = p->item;
1248 unsigned flags = commit->object.flags;
1250 if (flags & BOUNDARY)
1251 continue;
1253 * If we have fewer left, left_first is set and we omit
1254 * commits on the right branch in this loop. If we have
1255 * fewer right, we skip the left ones.
1257 if (left_first != !!(flags & SYMMETRIC_LEFT))
1258 continue;
1259 add_commit_patch_id(commit, &ids);
1262 /* either cherry_mark or cherry_pick are true */
1263 cherry_flag = revs->cherry_mark ? PATCHSAME : SHOWN;
1265 /* Check the other side */
1266 for (p = list; p; p = p->next) {
1267 struct commit *commit = p->item;
1268 struct patch_id *id;
1269 unsigned flags = commit->object.flags;
1271 if (flags & BOUNDARY)
1272 continue;
1274 * If we have fewer left, left_first is set and we omit
1275 * commits on the left branch in this loop.
1277 if (left_first == !!(flags & SYMMETRIC_LEFT))
1278 continue;
1281 * Have we seen the same patch id?
1283 id = patch_id_iter_first(commit, &ids);
1284 if (!id)
1285 continue;
1287 commit->object.flags |= cherry_flag;
1288 do {
1289 id->commit->object.flags |= cherry_flag;
1290 } while ((id = patch_id_iter_next(id, &ids)));
1293 free_patch_ids(&ids);
1296 /* How many extra uninteresting commits we want to see.. */
1297 #define SLOP 5
1299 static int still_interesting(struct commit_list *src, timestamp_t date, int slop,
1300 struct commit **interesting_cache)
1303 * No source list at all? We're definitely done..
1305 if (!src)
1306 return 0;
1309 * Does the destination list contain entries with a date
1310 * before the source list? Definitely _not_ done.
1312 if (date <= src->item->date)
1313 return SLOP;
1316 * Does the source list still have interesting commits in
1317 * it? Definitely not done..
1319 if (!everybody_uninteresting(src, interesting_cache))
1320 return SLOP;
1322 /* Ok, we're closing in.. */
1323 return slop-1;
1327 * "rev-list --ancestry-path=C_0 [--ancestry-path=C_1 ...] A..B"
1328 * computes commits that are ancestors of B but not ancestors of A but
1329 * further limits the result to those that have any of C in their
1330 * ancestry path (i.e. are either ancestors of any of C, descendants
1331 * of any of C, or are any of C). If --ancestry-path is specified with
1332 * no commit, we use all bottom commits for C.
1334 * Before this function is called, ancestors of C will have already
1335 * been marked with ANCESTRY_PATH previously.
1337 * This takes the list of bottom commits and the result of "A..B"
1338 * without --ancestry-path, and limits the latter further to the ones
1339 * that have any of C in their ancestry path. Since the ancestors of C
1340 * have already been marked (a prerequisite of this function), we just
1341 * need to mark the descendants, then exclude any commit that does not
1342 * have any of these marks.
1344 static void limit_to_ancestry(struct commit_list *bottoms, struct commit_list *list)
1346 struct commit_list *p;
1347 struct commit_list *rlist = NULL;
1348 int made_progress;
1351 * Reverse the list so that it will be likely that we would
1352 * process parents before children.
1354 for (p = list; p; p = p->next)
1355 commit_list_insert(p->item, &rlist);
1357 for (p = bottoms; p; p = p->next)
1358 p->item->object.flags |= TMP_MARK;
1361 * Mark the ones that can reach bottom commits in "list",
1362 * in a bottom-up fashion.
1364 do {
1365 made_progress = 0;
1366 for (p = rlist; p; p = p->next) {
1367 struct commit *c = p->item;
1368 struct commit_list *parents;
1369 if (c->object.flags & (TMP_MARK | UNINTERESTING))
1370 continue;
1371 for (parents = c->parents;
1372 parents;
1373 parents = parents->next) {
1374 if (!(parents->item->object.flags & TMP_MARK))
1375 continue;
1376 c->object.flags |= TMP_MARK;
1377 made_progress = 1;
1378 break;
1381 } while (made_progress);
1384 * NEEDSWORK: decide if we want to remove parents that are
1385 * not marked with TMP_MARK from commit->parents for commits
1386 * in the resulting list. We may not want to do that, though.
1390 * The ones that are not marked with either TMP_MARK or
1391 * ANCESTRY_PATH are uninteresting
1393 for (p = list; p; p = p->next) {
1394 struct commit *c = p->item;
1395 if (c->object.flags & (TMP_MARK | ANCESTRY_PATH))
1396 continue;
1397 c->object.flags |= UNINTERESTING;
1400 /* We are done with TMP_MARK and ANCESTRY_PATH */
1401 for (p = list; p; p = p->next)
1402 p->item->object.flags &= ~(TMP_MARK | ANCESTRY_PATH);
1403 for (p = bottoms; p; p = p->next)
1404 p->item->object.flags &= ~(TMP_MARK | ANCESTRY_PATH);
1405 free_commit_list(rlist);
1409 * Before walking the history, add the set of "negative" refs the
1410 * caller has asked to exclude to the bottom list.
1412 * This is used to compute "rev-list --ancestry-path A..B", as we need
1413 * to filter the result of "A..B" further to the ones that can actually
1414 * reach A.
1416 static void collect_bottom_commits(struct commit_list *list,
1417 struct commit_list **bottom)
1419 struct commit_list *elem;
1420 for (elem = list; elem; elem = elem->next)
1421 if (elem->item->object.flags & BOTTOM)
1422 commit_list_insert(elem->item, bottom);
1425 /* Assumes either left_only or right_only is set */
1426 static void limit_left_right(struct commit_list *list, struct rev_info *revs)
1428 struct commit_list *p;
1430 for (p = list; p; p = p->next) {
1431 struct commit *commit = p->item;
1433 if (revs->right_only) {
1434 if (commit->object.flags & SYMMETRIC_LEFT)
1435 commit->object.flags |= SHOWN;
1436 } else /* revs->left_only is set */
1437 if (!(commit->object.flags & SYMMETRIC_LEFT))
1438 commit->object.flags |= SHOWN;
1442 static int limit_list(struct rev_info *revs)
1444 int slop = SLOP;
1445 timestamp_t date = TIME_MAX;
1446 struct commit_list *original_list = revs->commits;
1447 struct commit_list *newlist = NULL;
1448 struct commit_list **p = &newlist;
1449 struct commit *interesting_cache = NULL;
1451 if (revs->ancestry_path_implicit_bottoms) {
1452 collect_bottom_commits(original_list,
1453 &revs->ancestry_path_bottoms);
1454 if (!revs->ancestry_path_bottoms)
1455 die("--ancestry-path given but there are no bottom commits");
1458 while (original_list) {
1459 struct commit *commit = pop_commit(&original_list);
1460 struct object *obj = &commit->object;
1461 show_early_output_fn_t show;
1463 if (commit == interesting_cache)
1464 interesting_cache = NULL;
1466 if (revs->max_age != -1 && (commit->date < revs->max_age))
1467 obj->flags |= UNINTERESTING;
1468 if (process_parents(revs, commit, &original_list, NULL) < 0)
1469 return -1;
1470 if (obj->flags & UNINTERESTING) {
1471 mark_parents_uninteresting(revs, commit);
1472 slop = still_interesting(original_list, date, slop, &interesting_cache);
1473 if (slop)
1474 continue;
1475 break;
1477 if (revs->min_age != -1 && (commit->date > revs->min_age) &&
1478 !revs->line_level_traverse)
1479 continue;
1480 if (revs->max_age_as_filter != -1 &&
1481 (commit->date < revs->max_age_as_filter) && !revs->line_level_traverse)
1482 continue;
1483 date = commit->date;
1484 p = &commit_list_insert(commit, p)->next;
1486 show = show_early_output;
1487 if (!show)
1488 continue;
1490 show(revs, newlist);
1491 show_early_output = NULL;
1493 if (revs->cherry_pick || revs->cherry_mark)
1494 cherry_pick_list(newlist, revs);
1496 if (revs->left_only || revs->right_only)
1497 limit_left_right(newlist, revs);
1499 if (revs->ancestry_path)
1500 limit_to_ancestry(revs->ancestry_path_bottoms, newlist);
1503 * Check if any commits have become TREESAME by some of their parents
1504 * becoming UNINTERESTING.
1506 if (limiting_can_increase_treesame(revs)) {
1507 struct commit_list *list = NULL;
1508 for (list = newlist; list; list = list->next) {
1509 struct commit *c = list->item;
1510 if (c->object.flags & (UNINTERESTING | TREESAME))
1511 continue;
1512 update_treesame(revs, c);
1516 free_commit_list(original_list);
1517 revs->commits = newlist;
1518 return 0;
1522 * Add an entry to refs->cmdline with the specified information.
1523 * *name is copied.
1525 static void add_rev_cmdline(struct rev_info *revs,
1526 struct object *item,
1527 const char *name,
1528 int whence,
1529 unsigned flags)
1531 struct rev_cmdline_info *info = &revs->cmdline;
1532 unsigned int nr = info->nr;
1534 ALLOC_GROW(info->rev, nr + 1, info->alloc);
1535 info->rev[nr].item = item;
1536 info->rev[nr].name = xstrdup(name);
1537 info->rev[nr].whence = whence;
1538 info->rev[nr].flags = flags;
1539 info->nr++;
1542 static void add_rev_cmdline_list(struct rev_info *revs,
1543 struct commit_list *commit_list,
1544 int whence,
1545 unsigned flags)
1547 while (commit_list) {
1548 struct object *object = &commit_list->item->object;
1549 add_rev_cmdline(revs, object, oid_to_hex(&object->oid),
1550 whence, flags);
1551 commit_list = commit_list->next;
1555 int ref_excluded(const struct ref_exclusions *exclusions, const char *path)
1557 const char *stripped_path = strip_namespace(path);
1558 struct string_list_item *item;
1560 for_each_string_list_item(item, &exclusions->excluded_refs) {
1561 if (!wildmatch(item->string, path, 0))
1562 return 1;
1565 if (ref_is_hidden(stripped_path, path, &exclusions->hidden_refs))
1566 return 1;
1568 return 0;
1571 void init_ref_exclusions(struct ref_exclusions *exclusions)
1573 struct ref_exclusions blank = REF_EXCLUSIONS_INIT;
1574 memcpy(exclusions, &blank, sizeof(*exclusions));
1577 void clear_ref_exclusions(struct ref_exclusions *exclusions)
1579 string_list_clear(&exclusions->excluded_refs, 0);
1580 strvec_clear(&exclusions->hidden_refs);
1581 exclusions->hidden_refs_configured = 0;
1584 void add_ref_exclusion(struct ref_exclusions *exclusions, const char *exclude)
1586 string_list_append(&exclusions->excluded_refs, exclude);
1589 struct exclude_hidden_refs_cb {
1590 struct ref_exclusions *exclusions;
1591 const char *section;
1594 static int hide_refs_config(const char *var, const char *value,
1595 const struct config_context *ctx UNUSED,
1596 void *cb_data)
1598 struct exclude_hidden_refs_cb *cb = cb_data;
1599 cb->exclusions->hidden_refs_configured = 1;
1600 return parse_hide_refs_config(var, value, cb->section,
1601 &cb->exclusions->hidden_refs);
1604 void exclude_hidden_refs(struct ref_exclusions *exclusions, const char *section)
1606 struct exclude_hidden_refs_cb cb;
1608 if (strcmp(section, "fetch") && strcmp(section, "receive") &&
1609 strcmp(section, "uploadpack"))
1610 die(_("unsupported section for hidden refs: %s"), section);
1612 if (exclusions->hidden_refs_configured)
1613 die(_("--exclude-hidden= passed more than once"));
1615 cb.exclusions = exclusions;
1616 cb.section = section;
1618 git_config(hide_refs_config, &cb);
1621 struct all_refs_cb {
1622 int all_flags;
1623 int warned_bad_reflog;
1624 struct rev_info *all_revs;
1625 const char *name_for_errormsg;
1626 struct worktree *wt;
1629 static int handle_one_ref(const char *path, const struct object_id *oid,
1630 int flag UNUSED,
1631 void *cb_data)
1633 struct all_refs_cb *cb = cb_data;
1634 struct object *object;
1636 if (ref_excluded(&cb->all_revs->ref_excludes, path))
1637 return 0;
1639 object = get_reference(cb->all_revs, path, oid, cb->all_flags);
1640 add_rev_cmdline(cb->all_revs, object, path, REV_CMD_REF, cb->all_flags);
1641 add_pending_object(cb->all_revs, object, path);
1642 return 0;
1645 static void init_all_refs_cb(struct all_refs_cb *cb, struct rev_info *revs,
1646 unsigned flags)
1648 cb->all_revs = revs;
1649 cb->all_flags = flags;
1650 revs->rev_input_given = 1;
1651 cb->wt = NULL;
1654 static void handle_refs(struct ref_store *refs,
1655 struct rev_info *revs, unsigned flags,
1656 int (*for_each)(struct ref_store *, each_ref_fn, void *))
1658 struct all_refs_cb cb;
1660 if (!refs) {
1661 /* this could happen with uninitialized submodules */
1662 return;
1665 init_all_refs_cb(&cb, revs, flags);
1666 for_each(refs, handle_one_ref, &cb);
1669 static void handle_one_reflog_commit(struct object_id *oid, void *cb_data)
1671 struct all_refs_cb *cb = cb_data;
1672 if (!is_null_oid(oid)) {
1673 struct object *o = parse_object(cb->all_revs->repo, oid);
1674 if (o) {
1675 o->flags |= cb->all_flags;
1676 /* ??? CMDLINEFLAGS ??? */
1677 add_pending_object(cb->all_revs, o, "");
1679 else if (!cb->warned_bad_reflog) {
1680 warning("reflog of '%s' references pruned commits",
1681 cb->name_for_errormsg);
1682 cb->warned_bad_reflog = 1;
1687 static int handle_one_reflog_ent(struct object_id *ooid, struct object_id *noid,
1688 const char *email UNUSED,
1689 timestamp_t timestamp UNUSED,
1690 int tz UNUSED,
1691 const char *message UNUSED,
1692 void *cb_data)
1694 handle_one_reflog_commit(ooid, cb_data);
1695 handle_one_reflog_commit(noid, cb_data);
1696 return 0;
1699 static int handle_one_reflog(const char *refname_in_wt, void *cb_data)
1701 struct all_refs_cb *cb = cb_data;
1702 struct strbuf refname = STRBUF_INIT;
1704 cb->warned_bad_reflog = 0;
1705 strbuf_worktree_ref(cb->wt, &refname, refname_in_wt);
1706 cb->name_for_errormsg = refname.buf;
1707 refs_for_each_reflog_ent(get_main_ref_store(the_repository),
1708 refname.buf,
1709 handle_one_reflog_ent, cb_data);
1710 strbuf_release(&refname);
1711 return 0;
1714 static void add_other_reflogs_to_pending(struct all_refs_cb *cb)
1716 struct worktree **worktrees, **p;
1718 worktrees = get_worktrees();
1719 for (p = worktrees; *p; p++) {
1720 struct worktree *wt = *p;
1722 if (wt->is_current)
1723 continue;
1725 cb->wt = wt;
1726 refs_for_each_reflog(get_worktree_ref_store(wt),
1727 handle_one_reflog,
1728 cb);
1730 free_worktrees(worktrees);
1733 void add_reflogs_to_pending(struct rev_info *revs, unsigned flags)
1735 struct all_refs_cb cb;
1737 cb.all_revs = revs;
1738 cb.all_flags = flags;
1739 cb.wt = NULL;
1740 for_each_reflog(handle_one_reflog, &cb);
1742 if (!revs->single_worktree)
1743 add_other_reflogs_to_pending(&cb);
1746 static void add_cache_tree(struct cache_tree *it, struct rev_info *revs,
1747 struct strbuf *path, unsigned int flags)
1749 size_t baselen = path->len;
1750 int i;
1752 if (it->entry_count >= 0) {
1753 struct tree *tree = lookup_tree(revs->repo, &it->oid);
1754 tree->object.flags |= flags;
1755 add_pending_object_with_path(revs, &tree->object, "",
1756 040000, path->buf);
1759 for (i = 0; i < it->subtree_nr; i++) {
1760 struct cache_tree_sub *sub = it->down[i];
1761 strbuf_addf(path, "%s%s", baselen ? "/" : "", sub->name);
1762 add_cache_tree(sub->cache_tree, revs, path, flags);
1763 strbuf_setlen(path, baselen);
1768 static void add_resolve_undo_to_pending(struct index_state *istate, struct rev_info *revs)
1770 struct string_list_item *item;
1771 struct string_list *resolve_undo = istate->resolve_undo;
1773 if (!resolve_undo)
1774 return;
1776 for_each_string_list_item(item, resolve_undo) {
1777 const char *path = item->string;
1778 struct resolve_undo_info *ru = item->util;
1779 int i;
1781 if (!ru)
1782 continue;
1783 for (i = 0; i < 3; i++) {
1784 struct blob *blob;
1786 if (!ru->mode[i] || !S_ISREG(ru->mode[i]))
1787 continue;
1789 blob = lookup_blob(revs->repo, &ru->oid[i]);
1790 if (!blob) {
1791 warning(_("resolve-undo records `%s` which is missing"),
1792 oid_to_hex(&ru->oid[i]));
1793 continue;
1795 add_pending_object_with_path(revs, &blob->object, "",
1796 ru->mode[i], path);
1801 static void do_add_index_objects_to_pending(struct rev_info *revs,
1802 struct index_state *istate,
1803 unsigned int flags)
1805 int i;
1807 /* TODO: audit for interaction with sparse-index. */
1808 ensure_full_index(istate);
1809 for (i = 0; i < istate->cache_nr; i++) {
1810 struct cache_entry *ce = istate->cache[i];
1811 struct blob *blob;
1813 if (S_ISGITLINK(ce->ce_mode))
1814 continue;
1816 blob = lookup_blob(revs->repo, &ce->oid);
1817 if (!blob)
1818 die("unable to add index blob to traversal");
1819 blob->object.flags |= flags;
1820 add_pending_object_with_path(revs, &blob->object, "",
1821 ce->ce_mode, ce->name);
1824 if (istate->cache_tree) {
1825 struct strbuf path = STRBUF_INIT;
1826 add_cache_tree(istate->cache_tree, revs, &path, flags);
1827 strbuf_release(&path);
1830 add_resolve_undo_to_pending(istate, revs);
1833 void add_index_objects_to_pending(struct rev_info *revs, unsigned int flags)
1835 struct worktree **worktrees, **p;
1837 repo_read_index(revs->repo);
1838 do_add_index_objects_to_pending(revs, revs->repo->index, flags);
1840 if (revs->single_worktree)
1841 return;
1843 worktrees = get_worktrees();
1844 for (p = worktrees; *p; p++) {
1845 struct worktree *wt = *p;
1846 struct index_state istate = INDEX_STATE_INIT(revs->repo);
1848 if (wt->is_current)
1849 continue; /* current index already taken care of */
1851 if (read_index_from(&istate,
1852 worktree_git_path(wt, "index"),
1853 get_worktree_git_dir(wt)) > 0)
1854 do_add_index_objects_to_pending(revs, &istate, flags);
1855 discard_index(&istate);
1857 free_worktrees(worktrees);
1860 struct add_alternate_refs_data {
1861 struct rev_info *revs;
1862 unsigned int flags;
1865 static void add_one_alternate_ref(const struct object_id *oid,
1866 void *vdata)
1868 const char *name = ".alternate";
1869 struct add_alternate_refs_data *data = vdata;
1870 struct object *obj;
1872 obj = get_reference(data->revs, name, oid, data->flags);
1873 add_rev_cmdline(data->revs, obj, name, REV_CMD_REV, data->flags);
1874 add_pending_object(data->revs, obj, name);
1877 static void add_alternate_refs_to_pending(struct rev_info *revs,
1878 unsigned int flags)
1880 struct add_alternate_refs_data data;
1881 data.revs = revs;
1882 data.flags = flags;
1883 for_each_alternate_ref(add_one_alternate_ref, &data);
1886 static int add_parents_only(struct rev_info *revs, const char *arg_, int flags,
1887 int exclude_parent)
1889 struct object_id oid;
1890 struct object *it;
1891 struct commit *commit;
1892 struct commit_list *parents;
1893 int parent_number;
1894 const char *arg = arg_;
1896 if (*arg == '^') {
1897 flags ^= UNINTERESTING | BOTTOM;
1898 arg++;
1900 if (repo_get_oid_committish(the_repository, arg, &oid))
1901 return 0;
1902 while (1) {
1903 it = get_reference(revs, arg, &oid, 0);
1904 if (!it && revs->ignore_missing)
1905 return 0;
1906 if (it->type != OBJ_TAG)
1907 break;
1908 if (!((struct tag*)it)->tagged)
1909 return 0;
1910 oidcpy(&oid, &((struct tag*)it)->tagged->oid);
1912 if (it->type != OBJ_COMMIT)
1913 return 0;
1914 commit = (struct commit *)it;
1915 if (exclude_parent &&
1916 exclude_parent > commit_list_count(commit->parents))
1917 return 0;
1918 for (parents = commit->parents, parent_number = 1;
1919 parents;
1920 parents = parents->next, parent_number++) {
1921 if (exclude_parent && parent_number != exclude_parent)
1922 continue;
1924 it = &parents->item->object;
1925 it->flags |= flags;
1926 add_rev_cmdline(revs, it, arg_, REV_CMD_PARENTS_ONLY, flags);
1927 add_pending_object(revs, it, arg);
1929 return 1;
1932 void repo_init_revisions(struct repository *r,
1933 struct rev_info *revs,
1934 const char *prefix)
1936 struct rev_info blank = REV_INFO_INIT;
1937 memcpy(revs, &blank, sizeof(*revs));
1939 revs->repo = r;
1940 revs->pruning.repo = r;
1941 revs->pruning.add_remove = file_add_remove;
1942 revs->pruning.change = file_change;
1943 revs->pruning.change_fn_data = revs;
1944 revs->prefix = prefix;
1946 grep_init(&revs->grep_filter, revs->repo);
1947 revs->grep_filter.status_only = 1;
1949 repo_diff_setup(revs->repo, &revs->diffopt);
1950 if (prefix && !revs->diffopt.prefix) {
1951 revs->diffopt.prefix = prefix;
1952 revs->diffopt.prefix_length = strlen(prefix);
1955 init_display_notes(&revs->notes_opt);
1956 list_objects_filter_init(&revs->filter);
1957 init_ref_exclusions(&revs->ref_excludes);
1958 oidset_init(&revs->missing_commits, 0);
1961 static void add_pending_commit_list(struct rev_info *revs,
1962 struct commit_list *commit_list,
1963 unsigned int flags)
1965 while (commit_list) {
1966 struct object *object = &commit_list->item->object;
1967 object->flags |= flags;
1968 add_pending_object(revs, object, oid_to_hex(&object->oid));
1969 commit_list = commit_list->next;
1973 static const char *lookup_other_head(struct object_id *oid)
1975 int i;
1976 static const char *const other_head[] = {
1977 "MERGE_HEAD", "CHERRY_PICK_HEAD", "REVERT_HEAD", "REBASE_HEAD"
1980 for (i = 0; i < ARRAY_SIZE(other_head); i++)
1981 if (!read_ref_full(other_head[i],
1982 RESOLVE_REF_READING | RESOLVE_REF_NO_RECURSE,
1983 oid, NULL)) {
1984 if (is_null_oid(oid))
1985 die(_("%s exists but is a symbolic ref"), other_head[i]);
1986 return other_head[i];
1989 die(_("--merge requires one of the pseudorefs MERGE_HEAD, CHERRY_PICK_HEAD, REVERT_HEAD or REBASE_HEAD"));
1992 static void prepare_show_merge(struct rev_info *revs)
1994 struct commit_list *bases;
1995 struct commit *head, *other;
1996 struct object_id oid;
1997 const char *other_name;
1998 const char **prune = NULL;
1999 int i, prune_num = 1; /* counting terminating NULL */
2000 struct index_state *istate = revs->repo->index;
2002 if (repo_get_oid(the_repository, "HEAD", &oid))
2003 die("--merge without HEAD?");
2004 head = lookup_commit_or_die(&oid, "HEAD");
2005 other_name = lookup_other_head(&oid);
2006 other = lookup_commit_or_die(&oid, other_name);
2007 add_pending_object(revs, &head->object, "HEAD");
2008 add_pending_object(revs, &other->object, other_name);
2009 bases = repo_get_merge_bases(the_repository, head, other);
2010 add_rev_cmdline_list(revs, bases, REV_CMD_MERGE_BASE, UNINTERESTING | BOTTOM);
2011 add_pending_commit_list(revs, bases, UNINTERESTING | BOTTOM);
2012 free_commit_list(bases);
2013 head->object.flags |= SYMMETRIC_LEFT;
2015 if (!istate->cache_nr)
2016 repo_read_index(revs->repo);
2017 for (i = 0; i < istate->cache_nr; i++) {
2018 const struct cache_entry *ce = istate->cache[i];
2019 if (!ce_stage(ce))
2020 continue;
2021 if (ce_path_match(istate, ce, &revs->prune_data, NULL)) {
2022 prune_num++;
2023 REALLOC_ARRAY(prune, prune_num);
2024 prune[prune_num-2] = ce->name;
2025 prune[prune_num-1] = NULL;
2027 while ((i+1 < istate->cache_nr) &&
2028 ce_same_name(ce, istate->cache[i+1]))
2029 i++;
2031 clear_pathspec(&revs->prune_data);
2032 parse_pathspec(&revs->prune_data, PATHSPEC_ALL_MAGIC & ~PATHSPEC_LITERAL,
2033 PATHSPEC_PREFER_FULL | PATHSPEC_LITERAL_PATH, "", prune);
2034 revs->limited = 1;
2037 static int dotdot_missing(const char *arg, char *dotdot,
2038 struct rev_info *revs, int symmetric)
2040 if (revs->ignore_missing)
2041 return 0;
2042 /* de-munge so we report the full argument */
2043 *dotdot = '.';
2044 die(symmetric
2045 ? "Invalid symmetric difference expression %s"
2046 : "Invalid revision range %s", arg);
2049 static int handle_dotdot_1(const char *arg, char *dotdot,
2050 struct rev_info *revs, int flags,
2051 int cant_be_filename,
2052 struct object_context *a_oc,
2053 struct object_context *b_oc)
2055 const char *a_name, *b_name;
2056 struct object_id a_oid, b_oid;
2057 struct object *a_obj, *b_obj;
2058 unsigned int a_flags, b_flags;
2059 int symmetric = 0;
2060 unsigned int flags_exclude = flags ^ (UNINTERESTING | BOTTOM);
2061 unsigned int oc_flags = GET_OID_COMMITTISH | GET_OID_RECORD_PATH;
2063 a_name = arg;
2064 if (!*a_name)
2065 a_name = "HEAD";
2067 b_name = dotdot + 2;
2068 if (*b_name == '.') {
2069 symmetric = 1;
2070 b_name++;
2072 if (!*b_name)
2073 b_name = "HEAD";
2075 if (get_oid_with_context(revs->repo, a_name, oc_flags, &a_oid, a_oc) ||
2076 get_oid_with_context(revs->repo, b_name, oc_flags, &b_oid, b_oc))
2077 return -1;
2079 if (!cant_be_filename) {
2080 *dotdot = '.';
2081 verify_non_filename(revs->prefix, arg);
2082 *dotdot = '\0';
2085 a_obj = parse_object(revs->repo, &a_oid);
2086 b_obj = parse_object(revs->repo, &b_oid);
2087 if (!a_obj || !b_obj)
2088 return dotdot_missing(arg, dotdot, revs, symmetric);
2090 if (!symmetric) {
2091 /* just A..B */
2092 b_flags = flags;
2093 a_flags = flags_exclude;
2094 } else {
2095 /* A...B -- find merge bases between the two */
2096 struct commit *a, *b;
2097 struct commit_list *exclude;
2099 a = lookup_commit_reference(revs->repo, &a_obj->oid);
2100 b = lookup_commit_reference(revs->repo, &b_obj->oid);
2101 if (!a || !b)
2102 return dotdot_missing(arg, dotdot, revs, symmetric);
2104 exclude = repo_get_merge_bases(the_repository, a, b);
2105 add_rev_cmdline_list(revs, exclude, REV_CMD_MERGE_BASE,
2106 flags_exclude);
2107 add_pending_commit_list(revs, exclude, flags_exclude);
2108 free_commit_list(exclude);
2110 b_flags = flags;
2111 a_flags = flags | SYMMETRIC_LEFT;
2114 a_obj->flags |= a_flags;
2115 b_obj->flags |= b_flags;
2116 add_rev_cmdline(revs, a_obj, a_name, REV_CMD_LEFT, a_flags);
2117 add_rev_cmdline(revs, b_obj, b_name, REV_CMD_RIGHT, b_flags);
2118 add_pending_object_with_path(revs, a_obj, a_name, a_oc->mode, a_oc->path);
2119 add_pending_object_with_path(revs, b_obj, b_name, b_oc->mode, b_oc->path);
2120 return 0;
2123 static int handle_dotdot(const char *arg,
2124 struct rev_info *revs, int flags,
2125 int cant_be_filename)
2127 struct object_context a_oc, b_oc;
2128 char *dotdot = strstr(arg, "..");
2129 int ret;
2131 if (!dotdot)
2132 return -1;
2134 memset(&a_oc, 0, sizeof(a_oc));
2135 memset(&b_oc, 0, sizeof(b_oc));
2137 *dotdot = '\0';
2138 ret = handle_dotdot_1(arg, dotdot, revs, flags, cant_be_filename,
2139 &a_oc, &b_oc);
2140 *dotdot = '.';
2142 free(a_oc.path);
2143 free(b_oc.path);
2145 return ret;
2148 static int handle_revision_arg_1(const char *arg_, struct rev_info *revs, int flags, unsigned revarg_opt)
2150 struct object_context oc;
2151 char *mark;
2152 struct object *object;
2153 struct object_id oid;
2154 int local_flags;
2155 const char *arg = arg_;
2156 int cant_be_filename = revarg_opt & REVARG_CANNOT_BE_FILENAME;
2157 unsigned get_sha1_flags = GET_OID_RECORD_PATH;
2159 flags = flags & UNINTERESTING ? flags | BOTTOM : flags & ~BOTTOM;
2161 if (!cant_be_filename && !strcmp(arg, "..")) {
2163 * Just ".."? That is not a range but the
2164 * pathspec for the parent directory.
2166 return -1;
2169 if (!handle_dotdot(arg, revs, flags, revarg_opt))
2170 return 0;
2172 mark = strstr(arg, "^@");
2173 if (mark && !mark[2]) {
2174 *mark = 0;
2175 if (add_parents_only(revs, arg, flags, 0))
2176 return 0;
2177 *mark = '^';
2179 mark = strstr(arg, "^!");
2180 if (mark && !mark[2]) {
2181 *mark = 0;
2182 if (!add_parents_only(revs, arg, flags ^ (UNINTERESTING | BOTTOM), 0))
2183 *mark = '^';
2185 mark = strstr(arg, "^-");
2186 if (mark) {
2187 int exclude_parent = 1;
2189 if (mark[2]) {
2190 if (strtol_i(mark + 2, 10, &exclude_parent) ||
2191 exclude_parent < 1)
2192 return -1;
2195 *mark = 0;
2196 if (!add_parents_only(revs, arg, flags ^ (UNINTERESTING | BOTTOM), exclude_parent))
2197 *mark = '^';
2200 local_flags = 0;
2201 if (*arg == '^') {
2202 local_flags = UNINTERESTING | BOTTOM;
2203 arg++;
2206 if (revarg_opt & REVARG_COMMITTISH)
2207 get_sha1_flags |= GET_OID_COMMITTISH;
2210 * Even if revs->do_not_die_on_missing_objects is set, we
2211 * should error out if we can't even get an oid, as
2212 * `--missing=print` should be able to report missing oids.
2214 if (get_oid_with_context(revs->repo, arg, get_sha1_flags, &oid, &oc))
2215 return revs->ignore_missing ? 0 : -1;
2216 if (!cant_be_filename)
2217 verify_non_filename(revs->prefix, arg);
2218 object = get_reference(revs, arg, &oid, flags ^ local_flags);
2219 if (!object)
2220 return (revs->ignore_missing || revs->do_not_die_on_missing_objects) ? 0 : -1;
2221 add_rev_cmdline(revs, object, arg_, REV_CMD_REV, flags ^ local_flags);
2222 add_pending_object_with_path(revs, object, arg, oc.mode, oc.path);
2223 free(oc.path);
2224 return 0;
2227 int handle_revision_arg(const char *arg, struct rev_info *revs, int flags, unsigned revarg_opt)
2229 int ret = handle_revision_arg_1(arg, revs, flags, revarg_opt);
2230 if (!ret)
2231 revs->rev_input_given = 1;
2232 return ret;
2235 static void read_pathspec_from_stdin(struct strbuf *sb,
2236 struct strvec *prune)
2238 while (strbuf_getline(sb, stdin) != EOF)
2239 strvec_push(prune, sb->buf);
2242 static void add_grep(struct rev_info *revs, const char *ptn, enum grep_pat_token what)
2244 append_grep_pattern(&revs->grep_filter, ptn, "command line", 0, what);
2247 static void add_header_grep(struct rev_info *revs, enum grep_header_field field, const char *pattern)
2249 append_header_grep_pattern(&revs->grep_filter, field, pattern);
2252 static void add_message_grep(struct rev_info *revs, const char *pattern)
2254 add_grep(revs, pattern, GREP_PATTERN_BODY);
2257 static int parse_count(const char *arg)
2259 int count;
2261 if (strtol_i(arg, 10, &count) < 0)
2262 die("'%s': not an integer", arg);
2263 return count;
2266 static timestamp_t parse_age(const char *arg)
2268 timestamp_t num;
2269 char *p;
2271 errno = 0;
2272 num = parse_timestamp(arg, &p, 10);
2273 if (errno || *p || p == arg)
2274 die("'%s': not a number of seconds since epoch", arg);
2275 return num;
2278 static int handle_revision_opt(struct rev_info *revs, int argc, const char **argv,
2279 int *unkc, const char **unkv,
2280 const struct setup_revision_opt* opt)
2282 const char *arg = argv[0];
2283 const char *optarg = NULL;
2284 int argcount;
2285 const unsigned hexsz = the_hash_algo->hexsz;
2287 /* pseudo revision arguments */
2288 if (!strcmp(arg, "--all") || !strcmp(arg, "--branches") ||
2289 !strcmp(arg, "--tags") || !strcmp(arg, "--remotes") ||
2290 !strcmp(arg, "--reflog") || !strcmp(arg, "--not") ||
2291 !strcmp(arg, "--no-walk") || !strcmp(arg, "--do-walk") ||
2292 !strcmp(arg, "--bisect") || starts_with(arg, "--glob=") ||
2293 !strcmp(arg, "--indexed-objects") ||
2294 !strcmp(arg, "--alternate-refs") ||
2295 starts_with(arg, "--exclude=") || starts_with(arg, "--exclude-hidden=") ||
2296 starts_with(arg, "--branches=") || starts_with(arg, "--tags=") ||
2297 starts_with(arg, "--remotes=") || starts_with(arg, "--no-walk="))
2299 unkv[(*unkc)++] = arg;
2300 return 1;
2303 if ((argcount = parse_long_opt("max-count", argv, &optarg))) {
2304 revs->max_count = parse_count(optarg);
2305 revs->no_walk = 0;
2306 return argcount;
2307 } else if ((argcount = parse_long_opt("skip", argv, &optarg))) {
2308 revs->skip_count = parse_count(optarg);
2309 return argcount;
2310 } else if ((*arg == '-') && isdigit(arg[1])) {
2311 /* accept -<digit>, like traditional "head" */
2312 revs->max_count = parse_count(arg + 1);
2313 revs->no_walk = 0;
2314 } else if (!strcmp(arg, "-n")) {
2315 if (argc <= 1)
2316 return error("-n requires an argument");
2317 revs->max_count = parse_count(argv[1]);
2318 revs->no_walk = 0;
2319 return 2;
2320 } else if (skip_prefix(arg, "-n", &optarg)) {
2321 revs->max_count = parse_count(optarg);
2322 revs->no_walk = 0;
2323 } else if ((argcount = parse_long_opt("max-age", argv, &optarg))) {
2324 revs->max_age = parse_age(optarg);
2325 return argcount;
2326 } else if ((argcount = parse_long_opt("since", argv, &optarg))) {
2327 revs->max_age = approxidate(optarg);
2328 return argcount;
2329 } else if ((argcount = parse_long_opt("since-as-filter", argv, &optarg))) {
2330 revs->max_age_as_filter = approxidate(optarg);
2331 return argcount;
2332 } else if ((argcount = parse_long_opt("after", argv, &optarg))) {
2333 revs->max_age = approxidate(optarg);
2334 return argcount;
2335 } else if ((argcount = parse_long_opt("min-age", argv, &optarg))) {
2336 revs->min_age = parse_age(optarg);
2337 return argcount;
2338 } else if ((argcount = parse_long_opt("before", argv, &optarg))) {
2339 revs->min_age = approxidate(optarg);
2340 return argcount;
2341 } else if ((argcount = parse_long_opt("until", argv, &optarg))) {
2342 revs->min_age = approxidate(optarg);
2343 return argcount;
2344 } else if (!strcmp(arg, "--first-parent")) {
2345 revs->first_parent_only = 1;
2346 } else if (!strcmp(arg, "--exclude-first-parent-only")) {
2347 revs->exclude_first_parent_only = 1;
2348 } else if (!strcmp(arg, "--ancestry-path")) {
2349 revs->ancestry_path = 1;
2350 revs->simplify_history = 0;
2351 revs->limited = 1;
2352 revs->ancestry_path_implicit_bottoms = 1;
2353 } else if (skip_prefix(arg, "--ancestry-path=", &optarg)) {
2354 struct commit *c;
2355 struct object_id oid;
2356 const char *msg = _("could not get commit for ancestry-path argument %s");
2358 revs->ancestry_path = 1;
2359 revs->simplify_history = 0;
2360 revs->limited = 1;
2362 if (repo_get_oid_committish(revs->repo, optarg, &oid))
2363 return error(msg, optarg);
2364 get_reference(revs, optarg, &oid, ANCESTRY_PATH);
2365 c = lookup_commit_reference(revs->repo, &oid);
2366 if (!c)
2367 return error(msg, optarg);
2368 commit_list_insert(c, &revs->ancestry_path_bottoms);
2369 } else if (!strcmp(arg, "-g") || !strcmp(arg, "--walk-reflogs")) {
2370 init_reflog_walk(&revs->reflog_info);
2371 } else if (!strcmp(arg, "--default")) {
2372 if (argc <= 1)
2373 return error("bad --default argument");
2374 revs->def = argv[1];
2375 return 2;
2376 } else if (!strcmp(arg, "--merge")) {
2377 revs->show_merge = 1;
2378 } else if (!strcmp(arg, "--topo-order")) {
2379 revs->sort_order = REV_SORT_IN_GRAPH_ORDER;
2380 revs->topo_order = 1;
2381 } else if (!strcmp(arg, "--simplify-merges")) {
2382 revs->simplify_merges = 1;
2383 revs->topo_order = 1;
2384 revs->rewrite_parents = 1;
2385 revs->simplify_history = 0;
2386 revs->limited = 1;
2387 } else if (!strcmp(arg, "--simplify-by-decoration")) {
2388 revs->simplify_merges = 1;
2389 revs->topo_order = 1;
2390 revs->rewrite_parents = 1;
2391 revs->simplify_history = 0;
2392 revs->simplify_by_decoration = 1;
2393 revs->limited = 1;
2394 revs->prune = 1;
2395 } else if (!strcmp(arg, "--date-order")) {
2396 revs->sort_order = REV_SORT_BY_COMMIT_DATE;
2397 revs->topo_order = 1;
2398 } else if (!strcmp(arg, "--author-date-order")) {
2399 revs->sort_order = REV_SORT_BY_AUTHOR_DATE;
2400 revs->topo_order = 1;
2401 } else if (!strcmp(arg, "--early-output")) {
2402 revs->early_output = 100;
2403 revs->topo_order = 1;
2404 } else if (skip_prefix(arg, "--early-output=", &optarg)) {
2405 if (strtoul_ui(optarg, 10, &revs->early_output) < 0)
2406 die("'%s': not a non-negative integer", optarg);
2407 revs->topo_order = 1;
2408 } else if (!strcmp(arg, "--parents")) {
2409 revs->rewrite_parents = 1;
2410 revs->print_parents = 1;
2411 } else if (!strcmp(arg, "--dense")) {
2412 revs->dense = 1;
2413 } else if (!strcmp(arg, "--sparse")) {
2414 revs->dense = 0;
2415 } else if (!strcmp(arg, "--in-commit-order")) {
2416 revs->tree_blobs_in_commit_order = 1;
2417 } else if (!strcmp(arg, "--remove-empty")) {
2418 revs->remove_empty_trees = 1;
2419 } else if (!strcmp(arg, "--merges")) {
2420 revs->min_parents = 2;
2421 } else if (!strcmp(arg, "--no-merges")) {
2422 revs->max_parents = 1;
2423 } else if (skip_prefix(arg, "--min-parents=", &optarg)) {
2424 revs->min_parents = parse_count(optarg);
2425 } else if (!strcmp(arg, "--no-min-parents")) {
2426 revs->min_parents = 0;
2427 } else if (skip_prefix(arg, "--max-parents=", &optarg)) {
2428 revs->max_parents = parse_count(optarg);
2429 } else if (!strcmp(arg, "--no-max-parents")) {
2430 revs->max_parents = -1;
2431 } else if (!strcmp(arg, "--boundary")) {
2432 revs->boundary = 1;
2433 } else if (!strcmp(arg, "--left-right")) {
2434 revs->left_right = 1;
2435 } else if (!strcmp(arg, "--left-only")) {
2436 if (revs->right_only)
2437 die(_("options '%s' and '%s' cannot be used together"),
2438 "--left-only", "--right-only/--cherry");
2439 revs->left_only = 1;
2440 } else if (!strcmp(arg, "--right-only")) {
2441 if (revs->left_only)
2442 die(_("options '%s' and '%s' cannot be used together"), "--right-only", "--left-only");
2443 revs->right_only = 1;
2444 } else if (!strcmp(arg, "--cherry")) {
2445 if (revs->left_only)
2446 die(_("options '%s' and '%s' cannot be used together"), "--cherry", "--left-only");
2447 revs->cherry_mark = 1;
2448 revs->right_only = 1;
2449 revs->max_parents = 1;
2450 revs->limited = 1;
2451 } else if (!strcmp(arg, "--count")) {
2452 revs->count = 1;
2453 } else if (!strcmp(arg, "--cherry-mark")) {
2454 if (revs->cherry_pick)
2455 die(_("options '%s' and '%s' cannot be used together"), "--cherry-mark", "--cherry-pick");
2456 revs->cherry_mark = 1;
2457 revs->limited = 1; /* needs limit_list() */
2458 } else if (!strcmp(arg, "--cherry-pick")) {
2459 if (revs->cherry_mark)
2460 die(_("options '%s' and '%s' cannot be used together"), "--cherry-pick", "--cherry-mark");
2461 revs->cherry_pick = 1;
2462 revs->limited = 1;
2463 } else if (!strcmp(arg, "--objects")) {
2464 revs->tag_objects = 1;
2465 revs->tree_objects = 1;
2466 revs->blob_objects = 1;
2467 } else if (!strcmp(arg, "--objects-edge")) {
2468 revs->tag_objects = 1;
2469 revs->tree_objects = 1;
2470 revs->blob_objects = 1;
2471 revs->edge_hint = 1;
2472 } else if (!strcmp(arg, "--objects-edge-aggressive")) {
2473 revs->tag_objects = 1;
2474 revs->tree_objects = 1;
2475 revs->blob_objects = 1;
2476 revs->edge_hint = 1;
2477 revs->edge_hint_aggressive = 1;
2478 } else if (!strcmp(arg, "--verify-objects")) {
2479 revs->tag_objects = 1;
2480 revs->tree_objects = 1;
2481 revs->blob_objects = 1;
2482 revs->verify_objects = 1;
2483 disable_commit_graph(revs->repo);
2484 } else if (!strcmp(arg, "--unpacked")) {
2485 revs->unpacked = 1;
2486 } else if (starts_with(arg, "--unpacked=")) {
2487 die(_("--unpacked=<packfile> no longer supported"));
2488 } else if (!strcmp(arg, "--no-kept-objects")) {
2489 revs->no_kept_objects = 1;
2490 revs->keep_pack_cache_flags |= IN_CORE_KEEP_PACKS;
2491 revs->keep_pack_cache_flags |= ON_DISK_KEEP_PACKS;
2492 } else if (skip_prefix(arg, "--no-kept-objects=", &optarg)) {
2493 revs->no_kept_objects = 1;
2494 if (!strcmp(optarg, "in-core"))
2495 revs->keep_pack_cache_flags |= IN_CORE_KEEP_PACKS;
2496 if (!strcmp(optarg, "on-disk"))
2497 revs->keep_pack_cache_flags |= ON_DISK_KEEP_PACKS;
2498 } else if (!strcmp(arg, "-r")) {
2499 revs->diff = 1;
2500 revs->diffopt.flags.recursive = 1;
2501 } else if (!strcmp(arg, "-t")) {
2502 revs->diff = 1;
2503 revs->diffopt.flags.recursive = 1;
2504 revs->diffopt.flags.tree_in_recursive = 1;
2505 } else if ((argcount = diff_merges_parse_opts(revs, argv))) {
2506 return argcount;
2507 } else if (!strcmp(arg, "-v")) {
2508 revs->verbose_header = 1;
2509 } else if (!strcmp(arg, "--pretty")) {
2510 revs->verbose_header = 1;
2511 revs->pretty_given = 1;
2512 get_commit_format(NULL, revs);
2513 } else if (skip_prefix(arg, "--pretty=", &optarg) ||
2514 skip_prefix(arg, "--format=", &optarg)) {
2516 * Detached form ("--pretty X" as opposed to "--pretty=X")
2517 * not allowed, since the argument is optional.
2519 revs->verbose_header = 1;
2520 revs->pretty_given = 1;
2521 get_commit_format(optarg, revs);
2522 } else if (!strcmp(arg, "--expand-tabs")) {
2523 revs->expand_tabs_in_log = 8;
2524 } else if (!strcmp(arg, "--no-expand-tabs")) {
2525 revs->expand_tabs_in_log = 0;
2526 } else if (skip_prefix(arg, "--expand-tabs=", &arg)) {
2527 int val;
2528 if (strtol_i(arg, 10, &val) < 0 || val < 0)
2529 die("'%s': not a non-negative integer", arg);
2530 revs->expand_tabs_in_log = val;
2531 } else if (!strcmp(arg, "--show-notes") || !strcmp(arg, "--notes")) {
2532 enable_default_display_notes(&revs->notes_opt, &revs->show_notes);
2533 revs->show_notes_given = 1;
2534 } else if (!strcmp(arg, "--show-signature")) {
2535 revs->show_signature = 1;
2536 } else if (!strcmp(arg, "--no-show-signature")) {
2537 revs->show_signature = 0;
2538 } else if (!strcmp(arg, "--show-linear-break")) {
2539 revs->break_bar = " ..........";
2540 revs->track_linear = 1;
2541 revs->track_first_time = 1;
2542 } else if (skip_prefix(arg, "--show-linear-break=", &optarg)) {
2543 revs->break_bar = xstrdup(optarg);
2544 revs->track_linear = 1;
2545 revs->track_first_time = 1;
2546 } else if (!strcmp(arg, "--show-notes-by-default")) {
2547 revs->show_notes_by_default = 1;
2548 } else if (skip_prefix(arg, "--show-notes=", &optarg) ||
2549 skip_prefix(arg, "--notes=", &optarg)) {
2550 if (starts_with(arg, "--show-notes=") &&
2551 revs->notes_opt.use_default_notes < 0)
2552 revs->notes_opt.use_default_notes = 1;
2553 enable_ref_display_notes(&revs->notes_opt, &revs->show_notes, optarg);
2554 revs->show_notes_given = 1;
2555 } else if (!strcmp(arg, "--no-notes")) {
2556 disable_display_notes(&revs->notes_opt, &revs->show_notes);
2557 revs->show_notes_given = 1;
2558 } else if (!strcmp(arg, "--standard-notes")) {
2559 revs->show_notes_given = 1;
2560 revs->notes_opt.use_default_notes = 1;
2561 } else if (!strcmp(arg, "--no-standard-notes")) {
2562 revs->notes_opt.use_default_notes = 0;
2563 } else if (!strcmp(arg, "--oneline")) {
2564 revs->verbose_header = 1;
2565 get_commit_format("oneline", revs);
2566 revs->pretty_given = 1;
2567 revs->abbrev_commit = 1;
2568 } else if (!strcmp(arg, "--graph")) {
2569 graph_clear(revs->graph);
2570 revs->graph = graph_init(revs);
2571 } else if (!strcmp(arg, "--no-graph")) {
2572 graph_clear(revs->graph);
2573 revs->graph = NULL;
2574 } else if (!strcmp(arg, "--encode-email-headers")) {
2575 revs->encode_email_headers = 1;
2576 } else if (!strcmp(arg, "--no-encode-email-headers")) {
2577 revs->encode_email_headers = 0;
2578 } else if (!strcmp(arg, "--root")) {
2579 revs->show_root_diff = 1;
2580 } else if (!strcmp(arg, "--no-commit-id")) {
2581 revs->no_commit_id = 1;
2582 } else if (!strcmp(arg, "--always")) {
2583 revs->always_show_header = 1;
2584 } else if (!strcmp(arg, "--no-abbrev")) {
2585 revs->abbrev = 0;
2586 } else if (!strcmp(arg, "--abbrev")) {
2587 revs->abbrev = DEFAULT_ABBREV;
2588 } else if (skip_prefix(arg, "--abbrev=", &optarg)) {
2589 revs->abbrev = strtoul(optarg, NULL, 10);
2590 if (revs->abbrev < MINIMUM_ABBREV)
2591 revs->abbrev = MINIMUM_ABBREV;
2592 else if (revs->abbrev > hexsz)
2593 revs->abbrev = hexsz;
2594 } else if (!strcmp(arg, "--abbrev-commit")) {
2595 revs->abbrev_commit = 1;
2596 revs->abbrev_commit_given = 1;
2597 } else if (!strcmp(arg, "--no-abbrev-commit")) {
2598 revs->abbrev_commit = 0;
2599 } else if (!strcmp(arg, "--full-diff")) {
2600 revs->diff = 1;
2601 revs->full_diff = 1;
2602 } else if (!strcmp(arg, "--show-pulls")) {
2603 revs->show_pulls = 1;
2604 } else if (!strcmp(arg, "--full-history")) {
2605 revs->simplify_history = 0;
2606 } else if (!strcmp(arg, "--relative-date")) {
2607 revs->date_mode.type = DATE_RELATIVE;
2608 revs->date_mode_explicit = 1;
2609 } else if ((argcount = parse_long_opt("date", argv, &optarg))) {
2610 parse_date_format(optarg, &revs->date_mode);
2611 revs->date_mode_explicit = 1;
2612 return argcount;
2613 } else if (!strcmp(arg, "--log-size")) {
2614 revs->show_log_size = 1;
2617 * Grepping the commit log
2619 else if ((argcount = parse_long_opt("author", argv, &optarg))) {
2620 add_header_grep(revs, GREP_HEADER_AUTHOR, optarg);
2621 return argcount;
2622 } else if ((argcount = parse_long_opt("committer", argv, &optarg))) {
2623 add_header_grep(revs, GREP_HEADER_COMMITTER, optarg);
2624 return argcount;
2625 } else if ((argcount = parse_long_opt("grep-reflog", argv, &optarg))) {
2626 add_header_grep(revs, GREP_HEADER_REFLOG, optarg);
2627 return argcount;
2628 } else if ((argcount = parse_long_opt("grep", argv, &optarg))) {
2629 add_message_grep(revs, optarg);
2630 return argcount;
2631 } else if (!strcmp(arg, "--basic-regexp")) {
2632 revs->grep_filter.pattern_type_option = GREP_PATTERN_TYPE_BRE;
2633 } else if (!strcmp(arg, "--extended-regexp") || !strcmp(arg, "-E")) {
2634 revs->grep_filter.pattern_type_option = GREP_PATTERN_TYPE_ERE;
2635 } else if (!strcmp(arg, "--regexp-ignore-case") || !strcmp(arg, "-i")) {
2636 revs->grep_filter.ignore_case = 1;
2637 revs->diffopt.pickaxe_opts |= DIFF_PICKAXE_IGNORE_CASE;
2638 } else if (!strcmp(arg, "--fixed-strings") || !strcmp(arg, "-F")) {
2639 revs->grep_filter.pattern_type_option = GREP_PATTERN_TYPE_FIXED;
2640 } else if (!strcmp(arg, "--perl-regexp") || !strcmp(arg, "-P")) {
2641 revs->grep_filter.pattern_type_option = GREP_PATTERN_TYPE_PCRE;
2642 } else if (!strcmp(arg, "--all-match")) {
2643 revs->grep_filter.all_match = 1;
2644 } else if (!strcmp(arg, "--invert-grep")) {
2645 revs->grep_filter.no_body_match = 1;
2646 } else if ((argcount = parse_long_opt("encoding", argv, &optarg))) {
2647 if (strcmp(optarg, "none"))
2648 git_log_output_encoding = xstrdup(optarg);
2649 else
2650 git_log_output_encoding = "";
2651 return argcount;
2652 } else if (!strcmp(arg, "--reverse")) {
2653 revs->reverse ^= 1;
2654 } else if (!strcmp(arg, "--children")) {
2655 revs->children.name = "children";
2656 revs->limited = 1;
2657 } else if (!strcmp(arg, "--ignore-missing")) {
2658 revs->ignore_missing = 1;
2659 } else if (opt && opt->allow_exclude_promisor_objects &&
2660 !strcmp(arg, "--exclude-promisor-objects")) {
2661 if (fetch_if_missing)
2662 BUG("exclude_promisor_objects can only be used when fetch_if_missing is 0");
2663 revs->exclude_promisor_objects = 1;
2664 } else {
2665 int opts = diff_opt_parse(&revs->diffopt, argv, argc, revs->prefix);
2666 if (!opts)
2667 unkv[(*unkc)++] = arg;
2668 return opts;
2671 return 1;
2674 void parse_revision_opt(struct rev_info *revs, struct parse_opt_ctx_t *ctx,
2675 const struct option *options,
2676 const char * const usagestr[])
2678 int n = handle_revision_opt(revs, ctx->argc, ctx->argv,
2679 &ctx->cpidx, ctx->out, NULL);
2680 if (n <= 0) {
2681 error("unknown option `%s'", ctx->argv[0]);
2682 usage_with_options(usagestr, options);
2684 ctx->argv += n;
2685 ctx->argc -= n;
2688 void revision_opts_finish(struct rev_info *revs)
2690 if (revs->graph && revs->track_linear)
2691 die(_("options '%s' and '%s' cannot be used together"), "--show-linear-break", "--graph");
2693 if (revs->graph) {
2694 revs->topo_order = 1;
2695 revs->rewrite_parents = 1;
2699 static int for_each_bisect_ref(struct ref_store *refs, each_ref_fn fn,
2700 void *cb_data, const char *term)
2702 struct strbuf bisect_refs = STRBUF_INIT;
2703 int status;
2704 strbuf_addf(&bisect_refs, "refs/bisect/%s", term);
2705 status = refs_for_each_fullref_in(refs, bisect_refs.buf, NULL, fn, cb_data);
2706 strbuf_release(&bisect_refs);
2707 return status;
2710 static int for_each_bad_bisect_ref(struct ref_store *refs, each_ref_fn fn, void *cb_data)
2712 return for_each_bisect_ref(refs, fn, cb_data, term_bad);
2715 static int for_each_good_bisect_ref(struct ref_store *refs, each_ref_fn fn, void *cb_data)
2717 return for_each_bisect_ref(refs, fn, cb_data, term_good);
2720 static int handle_revision_pseudo_opt(struct rev_info *revs,
2721 const char **argv, int *flags)
2723 const char *arg = argv[0];
2724 const char *optarg;
2725 struct ref_store *refs;
2726 int argcount;
2728 if (revs->repo != the_repository) {
2730 * We need some something like get_submodule_worktrees()
2731 * before we can go through all worktrees of a submodule,
2732 * .e.g with adding all HEADs from --all, which is not
2733 * supported right now, so stick to single worktree.
2735 if (!revs->single_worktree)
2736 BUG("--single-worktree cannot be used together with submodule");
2738 refs = get_main_ref_store(revs->repo);
2741 * NOTE!
2743 * Commands like "git shortlog" will not accept the options below
2744 * unless parse_revision_opt queues them (as opposed to erroring
2745 * out).
2747 * When implementing your new pseudo-option, remember to
2748 * register it in the list at the top of handle_revision_opt.
2750 if (!strcmp(arg, "--all")) {
2751 handle_refs(refs, revs, *flags, refs_for_each_ref);
2752 handle_refs(refs, revs, *flags, refs_head_ref);
2753 if (!revs->single_worktree) {
2754 struct all_refs_cb cb;
2756 init_all_refs_cb(&cb, revs, *flags);
2757 other_head_refs(handle_one_ref, &cb);
2759 clear_ref_exclusions(&revs->ref_excludes);
2760 } else if (!strcmp(arg, "--branches")) {
2761 if (revs->ref_excludes.hidden_refs_configured)
2762 return error(_("options '%s' and '%s' cannot be used together"),
2763 "--exclude-hidden", "--branches");
2764 handle_refs(refs, revs, *flags, refs_for_each_branch_ref);
2765 clear_ref_exclusions(&revs->ref_excludes);
2766 } else if (!strcmp(arg, "--bisect")) {
2767 read_bisect_terms(&term_bad, &term_good);
2768 handle_refs(refs, revs, *flags, for_each_bad_bisect_ref);
2769 handle_refs(refs, revs, *flags ^ (UNINTERESTING | BOTTOM),
2770 for_each_good_bisect_ref);
2771 revs->bisect = 1;
2772 } else if (!strcmp(arg, "--tags")) {
2773 if (revs->ref_excludes.hidden_refs_configured)
2774 return error(_("options '%s' and '%s' cannot be used together"),
2775 "--exclude-hidden", "--tags");
2776 handle_refs(refs, revs, *flags, refs_for_each_tag_ref);
2777 clear_ref_exclusions(&revs->ref_excludes);
2778 } else if (!strcmp(arg, "--remotes")) {
2779 if (revs->ref_excludes.hidden_refs_configured)
2780 return error(_("options '%s' and '%s' cannot be used together"),
2781 "--exclude-hidden", "--remotes");
2782 handle_refs(refs, revs, *flags, refs_for_each_remote_ref);
2783 clear_ref_exclusions(&revs->ref_excludes);
2784 } else if ((argcount = parse_long_opt("glob", argv, &optarg))) {
2785 struct all_refs_cb cb;
2786 init_all_refs_cb(&cb, revs, *flags);
2787 for_each_glob_ref(handle_one_ref, optarg, &cb);
2788 clear_ref_exclusions(&revs->ref_excludes);
2789 return argcount;
2790 } else if ((argcount = parse_long_opt("exclude", argv, &optarg))) {
2791 add_ref_exclusion(&revs->ref_excludes, optarg);
2792 return argcount;
2793 } else if ((argcount = parse_long_opt("exclude-hidden", argv, &optarg))) {
2794 exclude_hidden_refs(&revs->ref_excludes, optarg);
2795 return argcount;
2796 } else if (skip_prefix(arg, "--branches=", &optarg)) {
2797 struct all_refs_cb cb;
2798 if (revs->ref_excludes.hidden_refs_configured)
2799 return error(_("options '%s' and '%s' cannot be used together"),
2800 "--exclude-hidden", "--branches");
2801 init_all_refs_cb(&cb, revs, *flags);
2802 for_each_glob_ref_in(handle_one_ref, optarg, "refs/heads/", &cb);
2803 clear_ref_exclusions(&revs->ref_excludes);
2804 } else if (skip_prefix(arg, "--tags=", &optarg)) {
2805 struct all_refs_cb cb;
2806 if (revs->ref_excludes.hidden_refs_configured)
2807 return error(_("options '%s' and '%s' cannot be used together"),
2808 "--exclude-hidden", "--tags");
2809 init_all_refs_cb(&cb, revs, *flags);
2810 for_each_glob_ref_in(handle_one_ref, optarg, "refs/tags/", &cb);
2811 clear_ref_exclusions(&revs->ref_excludes);
2812 } else if (skip_prefix(arg, "--remotes=", &optarg)) {
2813 struct all_refs_cb cb;
2814 if (revs->ref_excludes.hidden_refs_configured)
2815 return error(_("options '%s' and '%s' cannot be used together"),
2816 "--exclude-hidden", "--remotes");
2817 init_all_refs_cb(&cb, revs, *flags);
2818 for_each_glob_ref_in(handle_one_ref, optarg, "refs/remotes/", &cb);
2819 clear_ref_exclusions(&revs->ref_excludes);
2820 } else if (!strcmp(arg, "--reflog")) {
2821 add_reflogs_to_pending(revs, *flags);
2822 } else if (!strcmp(arg, "--indexed-objects")) {
2823 add_index_objects_to_pending(revs, *flags);
2824 } else if (!strcmp(arg, "--alternate-refs")) {
2825 add_alternate_refs_to_pending(revs, *flags);
2826 } else if (!strcmp(arg, "--not")) {
2827 *flags ^= UNINTERESTING | BOTTOM;
2828 } else if (!strcmp(arg, "--no-walk")) {
2829 revs->no_walk = 1;
2830 } else if (skip_prefix(arg, "--no-walk=", &optarg)) {
2832 * Detached form ("--no-walk X" as opposed to "--no-walk=X")
2833 * not allowed, since the argument is optional.
2835 revs->no_walk = 1;
2836 if (!strcmp(optarg, "sorted"))
2837 revs->unsorted_input = 0;
2838 else if (!strcmp(optarg, "unsorted"))
2839 revs->unsorted_input = 1;
2840 else
2841 return error("invalid argument to --no-walk");
2842 } else if (!strcmp(arg, "--do-walk")) {
2843 revs->no_walk = 0;
2844 } else if (!strcmp(arg, "--single-worktree")) {
2845 revs->single_worktree = 1;
2846 } else if (skip_prefix(arg, ("--filter="), &arg)) {
2847 parse_list_objects_filter(&revs->filter, arg);
2848 } else if (!strcmp(arg, ("--no-filter"))) {
2849 list_objects_filter_set_no_filter(&revs->filter);
2850 } else {
2851 return 0;
2854 return 1;
2857 static void read_revisions_from_stdin(struct rev_info *revs,
2858 struct strvec *prune)
2860 struct strbuf sb;
2861 int seen_dashdash = 0;
2862 int seen_end_of_options = 0;
2863 int save_warning;
2864 int flags = 0;
2866 save_warning = warn_on_object_refname_ambiguity;
2867 warn_on_object_refname_ambiguity = 0;
2869 strbuf_init(&sb, 1000);
2870 while (strbuf_getline(&sb, stdin) != EOF) {
2871 if (!sb.len)
2872 break;
2874 if (!strcmp(sb.buf, "--")) {
2875 seen_dashdash = 1;
2876 break;
2879 if (!seen_end_of_options && sb.buf[0] == '-') {
2880 const char *argv[] = { sb.buf, NULL };
2882 if (!strcmp(sb.buf, "--end-of-options")) {
2883 seen_end_of_options = 1;
2884 continue;
2887 if (handle_revision_pseudo_opt(revs, argv, &flags) > 0)
2888 continue;
2890 die(_("invalid option '%s' in --stdin mode"), sb.buf);
2893 if (handle_revision_arg(sb.buf, revs, flags,
2894 REVARG_CANNOT_BE_FILENAME))
2895 die("bad revision '%s'", sb.buf);
2897 if (seen_dashdash)
2898 read_pathspec_from_stdin(&sb, prune);
2900 strbuf_release(&sb);
2901 warn_on_object_refname_ambiguity = save_warning;
2904 static void NORETURN diagnose_missing_default(const char *def)
2906 int flags;
2907 const char *refname;
2909 refname = resolve_ref_unsafe(def, 0, NULL, &flags);
2910 if (!refname || !(flags & REF_ISSYMREF) || (flags & REF_ISBROKEN))
2911 die(_("your current branch appears to be broken"));
2913 skip_prefix(refname, "refs/heads/", &refname);
2914 die(_("your current branch '%s' does not have any commits yet"),
2915 refname);
2919 * Parse revision information, filling in the "rev_info" structure,
2920 * and removing the used arguments from the argument list.
2922 * Returns the number of arguments left that weren't recognized
2923 * (which are also moved to the head of the argument list)
2925 int setup_revisions(int argc, const char **argv, struct rev_info *revs, struct setup_revision_opt *opt)
2927 int i, flags, left, seen_dashdash, revarg_opt;
2928 struct strvec prune_data = STRVEC_INIT;
2929 int seen_end_of_options = 0;
2931 /* First, search for "--" */
2932 if (opt && opt->assume_dashdash) {
2933 seen_dashdash = 1;
2934 } else {
2935 seen_dashdash = 0;
2936 for (i = 1; i < argc; i++) {
2937 const char *arg = argv[i];
2938 if (strcmp(arg, "--"))
2939 continue;
2940 if (opt && opt->free_removed_argv_elements)
2941 free((char *)argv[i]);
2942 argv[i] = NULL;
2943 argc = i;
2944 if (argv[i + 1])
2945 strvec_pushv(&prune_data, argv + i + 1);
2946 seen_dashdash = 1;
2947 break;
2951 /* Second, deal with arguments and options */
2952 flags = 0;
2953 revarg_opt = opt ? opt->revarg_opt : 0;
2954 if (seen_dashdash)
2955 revarg_opt |= REVARG_CANNOT_BE_FILENAME;
2956 for (left = i = 1; i < argc; i++) {
2957 const char *arg = argv[i];
2958 if (!seen_end_of_options && *arg == '-') {
2959 int opts;
2961 opts = handle_revision_pseudo_opt(
2962 revs, argv + i,
2963 &flags);
2964 if (opts > 0) {
2965 i += opts - 1;
2966 continue;
2969 if (!strcmp(arg, "--stdin")) {
2970 if (revs->disable_stdin) {
2971 argv[left++] = arg;
2972 continue;
2974 if (revs->read_from_stdin++)
2975 die("--stdin given twice?");
2976 read_revisions_from_stdin(revs, &prune_data);
2977 continue;
2980 if (!strcmp(arg, "--end-of-options")) {
2981 seen_end_of_options = 1;
2982 continue;
2985 opts = handle_revision_opt(revs, argc - i, argv + i,
2986 &left, argv, opt);
2987 if (opts > 0) {
2988 i += opts - 1;
2989 continue;
2991 if (opts < 0)
2992 exit(128);
2993 continue;
2997 if (handle_revision_arg(arg, revs, flags, revarg_opt)) {
2998 int j;
2999 if (seen_dashdash || *arg == '^')
3000 die("bad revision '%s'", arg);
3002 /* If we didn't have a "--":
3003 * (1) all filenames must exist;
3004 * (2) all rev-args must not be interpretable
3005 * as a valid filename.
3006 * but the latter we have checked in the main loop.
3008 for (j = i; j < argc; j++)
3009 verify_filename(revs->prefix, argv[j], j == i);
3011 strvec_pushv(&prune_data, argv + i);
3012 break;
3015 revision_opts_finish(revs);
3017 if (prune_data.nr) {
3019 * If we need to introduce the magic "a lone ':' means no
3020 * pathspec whatsoever", here is the place to do so.
3022 * if (prune_data.nr == 1 && !strcmp(prune_data[0], ":")) {
3023 * prune_data.nr = 0;
3024 * prune_data.alloc = 0;
3025 * free(prune_data.path);
3026 * prune_data.path = NULL;
3027 * } else {
3028 * terminate prune_data.alloc with NULL and
3029 * call init_pathspec() to set revs->prune_data here.
3032 parse_pathspec(&revs->prune_data, 0, 0,
3033 revs->prefix, prune_data.v);
3035 strvec_clear(&prune_data);
3037 if (!revs->def)
3038 revs->def = opt ? opt->def : NULL;
3039 if (opt && opt->tweak)
3040 opt->tweak(revs);
3041 if (revs->show_merge)
3042 prepare_show_merge(revs);
3043 if (revs->def && !revs->pending.nr && !revs->rev_input_given) {
3044 struct object_id oid;
3045 struct object *object;
3046 struct object_context oc;
3047 if (get_oid_with_context(revs->repo, revs->def, 0, &oid, &oc))
3048 diagnose_missing_default(revs->def);
3049 object = get_reference(revs, revs->def, &oid, 0);
3050 add_pending_object_with_mode(revs, object, revs->def, oc.mode);
3053 /* Did the user ask for any diff output? Run the diff! */
3054 if (revs->diffopt.output_format & ~DIFF_FORMAT_NO_OUTPUT)
3055 revs->diff = 1;
3057 /* Pickaxe, diff-filter and rename following need diffs */
3058 if ((revs->diffopt.pickaxe_opts & DIFF_PICKAXE_KINDS_MASK) ||
3059 revs->diffopt.filter ||
3060 revs->diffopt.flags.follow_renames)
3061 revs->diff = 1;
3063 if (revs->diffopt.objfind)
3064 revs->simplify_history = 0;
3066 if (revs->line_level_traverse) {
3067 if (want_ancestry(revs))
3068 revs->limited = 1;
3069 revs->topo_order = 1;
3072 if (revs->topo_order && !generation_numbers_enabled(the_repository))
3073 revs->limited = 1;
3075 if (revs->prune_data.nr) {
3076 copy_pathspec(&revs->pruning.pathspec, &revs->prune_data);
3077 /* Can't prune commits with rename following: the paths change.. */
3078 if (!revs->diffopt.flags.follow_renames)
3079 revs->prune = 1;
3080 if (!revs->full_diff)
3081 copy_pathspec(&revs->diffopt.pathspec,
3082 &revs->prune_data);
3085 diff_merges_setup_revs(revs);
3087 revs->diffopt.abbrev = revs->abbrev;
3089 diff_setup_done(&revs->diffopt);
3091 if (!is_encoding_utf8(get_log_output_encoding()))
3092 revs->grep_filter.ignore_locale = 1;
3093 compile_grep_patterns(&revs->grep_filter);
3095 if (revs->reflog_info && revs->limited)
3096 die("cannot combine --walk-reflogs with history-limiting options");
3097 if (revs->rewrite_parents && revs->children.name)
3098 die(_("options '%s' and '%s' cannot be used together"), "--parents", "--children");
3099 if (revs->filter.choice && !revs->blob_objects)
3100 die(_("object filtering requires --objects"));
3103 * Limitations on the graph functionality
3105 die_for_incompatible_opt3(!!revs->graph, "--graph",
3106 !!revs->reverse, "--reverse",
3107 !!revs->reflog_info, "--walk-reflogs");
3109 if (revs->no_walk && revs->graph)
3110 die(_("options '%s' and '%s' cannot be used together"), "--no-walk", "--graph");
3111 if (!revs->reflog_info && revs->grep_filter.use_reflog_filter)
3112 die(_("the option '%s' requires '%s'"), "--grep-reflog", "--walk-reflogs");
3114 if (revs->line_level_traverse &&
3115 (revs->diffopt.output_format & ~(DIFF_FORMAT_PATCH | DIFF_FORMAT_NO_OUTPUT)))
3116 die(_("-L does not yet support diff formats besides -p and -s"));
3118 if (revs->expand_tabs_in_log < 0)
3119 revs->expand_tabs_in_log = revs->expand_tabs_in_log_default;
3121 if (!revs->show_notes_given && revs->show_notes_by_default) {
3122 enable_default_display_notes(&revs->notes_opt, &revs->show_notes);
3123 revs->show_notes_given = 1;
3126 return left;
3129 static void release_revisions_cmdline(struct rev_cmdline_info *cmdline)
3131 unsigned int i;
3133 for (i = 0; i < cmdline->nr; i++)
3134 free((char *)cmdline->rev[i].name);
3135 free(cmdline->rev);
3138 static void release_revisions_mailmap(struct string_list *mailmap)
3140 if (!mailmap)
3141 return;
3142 clear_mailmap(mailmap);
3143 free(mailmap);
3146 static void release_revisions_topo_walk_info(struct topo_walk_info *info);
3148 static void free_void_commit_list(void *list)
3150 free_commit_list(list);
3153 void release_revisions(struct rev_info *revs)
3155 free_commit_list(revs->commits);
3156 free_commit_list(revs->ancestry_path_bottoms);
3157 object_array_clear(&revs->pending);
3158 object_array_clear(&revs->boundary_commits);
3159 release_revisions_cmdline(&revs->cmdline);
3160 list_objects_filter_release(&revs->filter);
3161 clear_pathspec(&revs->prune_data);
3162 date_mode_release(&revs->date_mode);
3163 release_revisions_mailmap(revs->mailmap);
3164 free_grep_patterns(&revs->grep_filter);
3165 graph_clear(revs->graph);
3166 /* TODO (need to handle "no_free"): diff_free(&revs->diffopt) */
3167 diff_free(&revs->pruning);
3168 reflog_walk_info_release(revs->reflog_info);
3169 release_revisions_topo_walk_info(revs->topo_walk_info);
3170 clear_decoration(&revs->children, free_void_commit_list);
3171 clear_decoration(&revs->merge_simplification, free);
3172 clear_decoration(&revs->treesame, free);
3173 line_log_free(revs);
3174 oidset_clear(&revs->missing_commits);
3177 static void add_child(struct rev_info *revs, struct commit *parent, struct commit *child)
3179 struct commit_list *l = xcalloc(1, sizeof(*l));
3181 l->item = child;
3182 l->next = add_decoration(&revs->children, &parent->object, l);
3185 static int remove_duplicate_parents(struct rev_info *revs, struct commit *commit)
3187 struct treesame_state *ts = lookup_decoration(&revs->treesame, &commit->object);
3188 struct commit_list **pp, *p;
3189 int surviving_parents;
3191 /* Examine existing parents while marking ones we have seen... */
3192 pp = &commit->parents;
3193 surviving_parents = 0;
3194 while ((p = *pp) != NULL) {
3195 struct commit *parent = p->item;
3196 if (parent->object.flags & TMP_MARK) {
3197 *pp = p->next;
3198 if (ts)
3199 compact_treesame(revs, commit, surviving_parents);
3200 continue;
3202 parent->object.flags |= TMP_MARK;
3203 surviving_parents++;
3204 pp = &p->next;
3206 /* clear the temporary mark */
3207 for (p = commit->parents; p; p = p->next) {
3208 p->item->object.flags &= ~TMP_MARK;
3210 /* no update_treesame() - removing duplicates can't affect TREESAME */
3211 return surviving_parents;
3214 struct merge_simplify_state {
3215 struct commit *simplified;
3218 static struct merge_simplify_state *locate_simplify_state(struct rev_info *revs, struct commit *commit)
3220 struct merge_simplify_state *st;
3222 st = lookup_decoration(&revs->merge_simplification, &commit->object);
3223 if (!st) {
3224 CALLOC_ARRAY(st, 1);
3225 add_decoration(&revs->merge_simplification, &commit->object, st);
3227 return st;
3230 static int mark_redundant_parents(struct commit *commit)
3232 struct commit_list *h = reduce_heads(commit->parents);
3233 int i = 0, marked = 0;
3234 struct commit_list *po, *pn;
3236 /* Want these for sanity-checking only */
3237 int orig_cnt = commit_list_count(commit->parents);
3238 int cnt = commit_list_count(h);
3241 * Not ready to remove items yet, just mark them for now, based
3242 * on the output of reduce_heads(). reduce_heads outputs the reduced
3243 * set in its original order, so this isn't too hard.
3245 po = commit->parents;
3246 pn = h;
3247 while (po) {
3248 if (pn && po->item == pn->item) {
3249 pn = pn->next;
3250 i++;
3251 } else {
3252 po->item->object.flags |= TMP_MARK;
3253 marked++;
3255 po=po->next;
3258 if (i != cnt || cnt+marked != orig_cnt)
3259 die("mark_redundant_parents %d %d %d %d", orig_cnt, cnt, i, marked);
3261 free_commit_list(h);
3263 return marked;
3266 static int mark_treesame_root_parents(struct commit *commit)
3268 struct commit_list *p;
3269 int marked = 0;
3271 for (p = commit->parents; p; p = p->next) {
3272 struct commit *parent = p->item;
3273 if (!parent->parents && (parent->object.flags & TREESAME)) {
3274 parent->object.flags |= TMP_MARK;
3275 marked++;
3279 return marked;
3283 * Awkward naming - this means one parent we are TREESAME to.
3284 * cf mark_treesame_root_parents: root parents that are TREESAME (to an
3285 * empty tree). Better name suggestions?
3287 static int leave_one_treesame_to_parent(struct rev_info *revs, struct commit *commit)
3289 struct treesame_state *ts = lookup_decoration(&revs->treesame, &commit->object);
3290 struct commit *unmarked = NULL, *marked = NULL;
3291 struct commit_list *p;
3292 unsigned n;
3294 for (p = commit->parents, n = 0; p; p = p->next, n++) {
3295 if (ts->treesame[n]) {
3296 if (p->item->object.flags & TMP_MARK) {
3297 if (!marked)
3298 marked = p->item;
3299 } else {
3300 if (!unmarked) {
3301 unmarked = p->item;
3302 break;
3309 * If we are TREESAME to a marked-for-deletion parent, but not to any
3310 * unmarked parents, unmark the first TREESAME parent. This is the
3311 * parent that the default simplify_history==1 scan would have followed,
3312 * and it doesn't make sense to omit that path when asking for a
3313 * simplified full history. Retaining it improves the chances of
3314 * understanding odd missed merges that took an old version of a file.
3316 * Example:
3318 * I--------*X A modified the file, but mainline merge X used
3319 * \ / "-s ours", so took the version from I. X is
3320 * `-*A--' TREESAME to I and !TREESAME to A.
3322 * Default log from X would produce "I". Without this check,
3323 * --full-history --simplify-merges would produce "I-A-X", showing
3324 * the merge commit X and that it changed A, but not making clear that
3325 * it had just taken the I version. With this check, the topology above
3326 * is retained.
3328 * Note that it is possible that the simplification chooses a different
3329 * TREESAME parent from the default, in which case this test doesn't
3330 * activate, and we _do_ drop the default parent. Example:
3332 * I------X A modified the file, but it was reverted in B,
3333 * \ / meaning mainline merge X is TREESAME to both
3334 * *A-*B parents.
3336 * Default log would produce "I" by following the first parent;
3337 * --full-history --simplify-merges will produce "I-A-B". But this is a
3338 * reasonable result - it presents a logical full history leading from
3339 * I to X, and X is not an important merge.
3341 if (!unmarked && marked) {
3342 marked->object.flags &= ~TMP_MARK;
3343 return 1;
3346 return 0;
3349 static int remove_marked_parents(struct rev_info *revs, struct commit *commit)
3351 struct commit_list **pp, *p;
3352 int nth_parent, removed = 0;
3354 pp = &commit->parents;
3355 nth_parent = 0;
3356 while ((p = *pp) != NULL) {
3357 struct commit *parent = p->item;
3358 if (parent->object.flags & TMP_MARK) {
3359 parent->object.flags &= ~TMP_MARK;
3360 *pp = p->next;
3361 free(p);
3362 removed++;
3363 compact_treesame(revs, commit, nth_parent);
3364 continue;
3366 pp = &p->next;
3367 nth_parent++;
3370 /* Removing parents can only increase TREESAMEness */
3371 if (removed && !(commit->object.flags & TREESAME))
3372 update_treesame(revs, commit);
3374 return nth_parent;
3377 static struct commit_list **simplify_one(struct rev_info *revs, struct commit *commit, struct commit_list **tail)
3379 struct commit_list *p;
3380 struct commit *parent;
3381 struct merge_simplify_state *st, *pst;
3382 int cnt;
3384 st = locate_simplify_state(revs, commit);
3387 * Have we handled this one?
3389 if (st->simplified)
3390 return tail;
3393 * An UNINTERESTING commit simplifies to itself, so does a
3394 * root commit. We do not rewrite parents of such commit
3395 * anyway.
3397 if ((commit->object.flags & UNINTERESTING) || !commit->parents) {
3398 st->simplified = commit;
3399 return tail;
3403 * Do we know what commit all of our parents that matter
3404 * should be rewritten to? Otherwise we are not ready to
3405 * rewrite this one yet.
3407 for (cnt = 0, p = commit->parents; p; p = p->next) {
3408 pst = locate_simplify_state(revs, p->item);
3409 if (!pst->simplified) {
3410 tail = &commit_list_insert(p->item, tail)->next;
3411 cnt++;
3413 if (revs->first_parent_only)
3414 break;
3416 if (cnt) {
3417 tail = &commit_list_insert(commit, tail)->next;
3418 return tail;
3422 * Rewrite our list of parents. Note that this cannot
3423 * affect our TREESAME flags in any way - a commit is
3424 * always TREESAME to its simplification.
3426 for (p = commit->parents; p; p = p->next) {
3427 pst = locate_simplify_state(revs, p->item);
3428 p->item = pst->simplified;
3429 if (revs->first_parent_only)
3430 break;
3433 if (revs->first_parent_only)
3434 cnt = 1;
3435 else
3436 cnt = remove_duplicate_parents(revs, commit);
3439 * It is possible that we are a merge and one side branch
3440 * does not have any commit that touches the given paths;
3441 * in such a case, the immediate parent from that branch
3442 * will be rewritten to be the merge base.
3444 * o----X X: the commit we are looking at;
3445 * / / o: a commit that touches the paths;
3446 * ---o----'
3448 * Further, a merge of an independent branch that doesn't
3449 * touch the path will reduce to a treesame root parent:
3451 * ----o----X X: the commit we are looking at;
3452 * / o: a commit that touches the paths;
3453 * r r: a root commit not touching the paths
3455 * Detect and simplify both cases.
3457 if (1 < cnt) {
3458 int marked = mark_redundant_parents(commit);
3459 marked += mark_treesame_root_parents(commit);
3460 if (marked)
3461 marked -= leave_one_treesame_to_parent(revs, commit);
3462 if (marked)
3463 cnt = remove_marked_parents(revs, commit);
3467 * A commit simplifies to itself if it is a root, if it is
3468 * UNINTERESTING, if it touches the given paths, or if it is a
3469 * merge and its parents don't simplify to one relevant commit
3470 * (the first two cases are already handled at the beginning of
3471 * this function).
3473 * Otherwise, it simplifies to what its sole relevant parent
3474 * simplifies to.
3476 if (!cnt ||
3477 (commit->object.flags & UNINTERESTING) ||
3478 !(commit->object.flags & TREESAME) ||
3479 (parent = one_relevant_parent(revs, commit->parents)) == NULL ||
3480 (revs->show_pulls && (commit->object.flags & PULL_MERGE)))
3481 st->simplified = commit;
3482 else {
3483 pst = locate_simplify_state(revs, parent);
3484 st->simplified = pst->simplified;
3486 return tail;
3489 static void simplify_merges(struct rev_info *revs)
3491 struct commit_list *list, *next;
3492 struct commit_list *yet_to_do, **tail;
3493 struct commit *commit;
3495 if (!revs->prune)
3496 return;
3498 /* feed the list reversed */
3499 yet_to_do = NULL;
3500 for (list = revs->commits; list; list = next) {
3501 commit = list->item;
3502 next = list->next;
3504 * Do not free(list) here yet; the original list
3505 * is used later in this function.
3507 commit_list_insert(commit, &yet_to_do);
3509 while (yet_to_do) {
3510 list = yet_to_do;
3511 yet_to_do = NULL;
3512 tail = &yet_to_do;
3513 while (list) {
3514 commit = pop_commit(&list);
3515 tail = simplify_one(revs, commit, tail);
3519 /* clean up the result, removing the simplified ones */
3520 list = revs->commits;
3521 revs->commits = NULL;
3522 tail = &revs->commits;
3523 while (list) {
3524 struct merge_simplify_state *st;
3526 commit = pop_commit(&list);
3527 st = locate_simplify_state(revs, commit);
3528 if (st->simplified == commit)
3529 tail = &commit_list_insert(commit, tail)->next;
3533 static void set_children(struct rev_info *revs)
3535 struct commit_list *l;
3536 for (l = revs->commits; l; l = l->next) {
3537 struct commit *commit = l->item;
3538 struct commit_list *p;
3540 for (p = commit->parents; p; p = p->next)
3541 add_child(revs, p->item, commit);
3545 void reset_revision_walk(void)
3547 clear_object_flags(SEEN | ADDED | SHOWN | TOPO_WALK_EXPLORED | TOPO_WALK_INDEGREE);
3550 static int mark_uninteresting(const struct object_id *oid,
3551 struct packed_git *pack UNUSED,
3552 uint32_t pos UNUSED,
3553 void *cb)
3555 struct rev_info *revs = cb;
3556 struct object *o = lookup_unknown_object(revs->repo, oid);
3557 o->flags |= UNINTERESTING | SEEN;
3558 return 0;
3561 define_commit_slab(indegree_slab, int);
3562 define_commit_slab(author_date_slab, timestamp_t);
3564 struct topo_walk_info {
3565 timestamp_t min_generation;
3566 struct prio_queue explore_queue;
3567 struct prio_queue indegree_queue;
3568 struct prio_queue topo_queue;
3569 struct indegree_slab indegree;
3570 struct author_date_slab author_date;
3573 static int topo_walk_atexit_registered;
3574 static unsigned int count_explore_walked;
3575 static unsigned int count_indegree_walked;
3576 static unsigned int count_topo_walked;
3578 static void trace2_topo_walk_statistics_atexit(void)
3580 struct json_writer jw = JSON_WRITER_INIT;
3582 jw_object_begin(&jw, 0);
3583 jw_object_intmax(&jw, "count_explore_walked", count_explore_walked);
3584 jw_object_intmax(&jw, "count_indegree_walked", count_indegree_walked);
3585 jw_object_intmax(&jw, "count_topo_walked", count_topo_walked);
3586 jw_end(&jw);
3588 trace2_data_json("topo_walk", the_repository, "statistics", &jw);
3590 jw_release(&jw);
3593 static inline void test_flag_and_insert(struct prio_queue *q, struct commit *c, int flag)
3595 if (c->object.flags & flag)
3596 return;
3598 c->object.flags |= flag;
3599 prio_queue_put(q, c);
3602 static void explore_walk_step(struct rev_info *revs)
3604 struct topo_walk_info *info = revs->topo_walk_info;
3605 struct commit_list *p;
3606 struct commit *c = prio_queue_get(&info->explore_queue);
3608 if (!c)
3609 return;
3611 if (repo_parse_commit_gently(revs->repo, c, 1) < 0)
3612 return;
3614 count_explore_walked++;
3616 if (revs->sort_order == REV_SORT_BY_AUTHOR_DATE)
3617 record_author_date(&info->author_date, c);
3619 if (revs->max_age != -1 && (c->date < revs->max_age))
3620 c->object.flags |= UNINTERESTING;
3622 if (process_parents(revs, c, NULL, NULL) < 0)
3623 return;
3625 if (c->object.flags & UNINTERESTING)
3626 mark_parents_uninteresting(revs, c);
3628 for (p = c->parents; p; p = p->next)
3629 test_flag_and_insert(&info->explore_queue, p->item, TOPO_WALK_EXPLORED);
3632 static void explore_to_depth(struct rev_info *revs,
3633 timestamp_t gen_cutoff)
3635 struct topo_walk_info *info = revs->topo_walk_info;
3636 struct commit *c;
3637 while ((c = prio_queue_peek(&info->explore_queue)) &&
3638 commit_graph_generation(c) >= gen_cutoff)
3639 explore_walk_step(revs);
3642 static void indegree_walk_step(struct rev_info *revs)
3644 struct commit_list *p;
3645 struct topo_walk_info *info = revs->topo_walk_info;
3646 struct commit *c = prio_queue_get(&info->indegree_queue);
3648 if (!c)
3649 return;
3651 if (repo_parse_commit_gently(revs->repo, c, 1) < 0)
3652 return;
3654 count_indegree_walked++;
3656 explore_to_depth(revs, commit_graph_generation(c));
3658 for (p = c->parents; p; p = p->next) {
3659 struct commit *parent = p->item;
3660 int *pi = indegree_slab_at(&info->indegree, parent);
3662 if (repo_parse_commit_gently(revs->repo, parent, 1) < 0)
3663 return;
3665 if (*pi)
3666 (*pi)++;
3667 else
3668 *pi = 2;
3670 test_flag_and_insert(&info->indegree_queue, parent, TOPO_WALK_INDEGREE);
3672 if (revs->first_parent_only)
3673 return;
3677 static void compute_indegrees_to_depth(struct rev_info *revs,
3678 timestamp_t gen_cutoff)
3680 struct topo_walk_info *info = revs->topo_walk_info;
3681 struct commit *c;
3682 while ((c = prio_queue_peek(&info->indegree_queue)) &&
3683 commit_graph_generation(c) >= gen_cutoff)
3684 indegree_walk_step(revs);
3687 static void release_revisions_topo_walk_info(struct topo_walk_info *info)
3689 if (!info)
3690 return;
3691 clear_prio_queue(&info->explore_queue);
3692 clear_prio_queue(&info->indegree_queue);
3693 clear_prio_queue(&info->topo_queue);
3694 clear_indegree_slab(&info->indegree);
3695 clear_author_date_slab(&info->author_date);
3696 free(info);
3699 static void reset_topo_walk(struct rev_info *revs)
3701 release_revisions_topo_walk_info(revs->topo_walk_info);
3702 revs->topo_walk_info = NULL;
3705 static void init_topo_walk(struct rev_info *revs)
3707 struct topo_walk_info *info;
3708 struct commit_list *list;
3709 if (revs->topo_walk_info)
3710 reset_topo_walk(revs);
3712 revs->topo_walk_info = xmalloc(sizeof(struct topo_walk_info));
3713 info = revs->topo_walk_info;
3714 memset(info, 0, sizeof(struct topo_walk_info));
3716 init_indegree_slab(&info->indegree);
3717 memset(&info->explore_queue, 0, sizeof(info->explore_queue));
3718 memset(&info->indegree_queue, 0, sizeof(info->indegree_queue));
3719 memset(&info->topo_queue, 0, sizeof(info->topo_queue));
3721 switch (revs->sort_order) {
3722 default: /* REV_SORT_IN_GRAPH_ORDER */
3723 info->topo_queue.compare = NULL;
3724 break;
3725 case REV_SORT_BY_COMMIT_DATE:
3726 info->topo_queue.compare = compare_commits_by_commit_date;
3727 break;
3728 case REV_SORT_BY_AUTHOR_DATE:
3729 init_author_date_slab(&info->author_date);
3730 info->topo_queue.compare = compare_commits_by_author_date;
3731 info->topo_queue.cb_data = &info->author_date;
3732 break;
3735 info->explore_queue.compare = compare_commits_by_gen_then_commit_date;
3736 info->indegree_queue.compare = compare_commits_by_gen_then_commit_date;
3738 info->min_generation = GENERATION_NUMBER_INFINITY;
3739 for (list = revs->commits; list; list = list->next) {
3740 struct commit *c = list->item;
3741 timestamp_t generation;
3743 if (repo_parse_commit_gently(revs->repo, c, 1))
3744 continue;
3746 test_flag_and_insert(&info->explore_queue, c, TOPO_WALK_EXPLORED);
3747 test_flag_and_insert(&info->indegree_queue, c, TOPO_WALK_INDEGREE);
3749 generation = commit_graph_generation(c);
3750 if (generation < info->min_generation)
3751 info->min_generation = generation;
3753 *(indegree_slab_at(&info->indegree, c)) = 1;
3755 if (revs->sort_order == REV_SORT_BY_AUTHOR_DATE)
3756 record_author_date(&info->author_date, c);
3758 compute_indegrees_to_depth(revs, info->min_generation);
3760 for (list = revs->commits; list; list = list->next) {
3761 struct commit *c = list->item;
3763 if (*(indegree_slab_at(&info->indegree, c)) == 1)
3764 prio_queue_put(&info->topo_queue, c);
3768 * This is unfortunate; the initial tips need to be shown
3769 * in the order given from the revision traversal machinery.
3771 if (revs->sort_order == REV_SORT_IN_GRAPH_ORDER)
3772 prio_queue_reverse(&info->topo_queue);
3774 if (trace2_is_enabled() && !topo_walk_atexit_registered) {
3775 atexit(trace2_topo_walk_statistics_atexit);
3776 topo_walk_atexit_registered = 1;
3780 static struct commit *next_topo_commit(struct rev_info *revs)
3782 struct commit *c;
3783 struct topo_walk_info *info = revs->topo_walk_info;
3785 /* pop next off of topo_queue */
3786 c = prio_queue_get(&info->topo_queue);
3788 if (c)
3789 *(indegree_slab_at(&info->indegree, c)) = 0;
3791 return c;
3794 static void expand_topo_walk(struct rev_info *revs, struct commit *commit)
3796 struct commit_list *p;
3797 struct topo_walk_info *info = revs->topo_walk_info;
3798 if (process_parents(revs, commit, NULL, NULL) < 0) {
3799 if (!revs->ignore_missing_links)
3800 die("Failed to traverse parents of commit %s",
3801 oid_to_hex(&commit->object.oid));
3804 count_topo_walked++;
3806 for (p = commit->parents; p; p = p->next) {
3807 struct commit *parent = p->item;
3808 int *pi;
3809 timestamp_t generation;
3811 if (parent->object.flags & UNINTERESTING)
3812 continue;
3814 if (repo_parse_commit_gently(revs->repo, parent, 1) < 0)
3815 continue;
3817 generation = commit_graph_generation(parent);
3818 if (generation < info->min_generation) {
3819 info->min_generation = generation;
3820 compute_indegrees_to_depth(revs, info->min_generation);
3823 pi = indegree_slab_at(&info->indegree, parent);
3825 (*pi)--;
3826 if (*pi == 1)
3827 prio_queue_put(&info->topo_queue, parent);
3829 if (revs->first_parent_only)
3830 return;
3834 int prepare_revision_walk(struct rev_info *revs)
3836 int i;
3837 struct object_array old_pending;
3838 struct commit_list **next = &revs->commits;
3840 memcpy(&old_pending, &revs->pending, sizeof(old_pending));
3841 revs->pending.nr = 0;
3842 revs->pending.alloc = 0;
3843 revs->pending.objects = NULL;
3844 for (i = 0; i < old_pending.nr; i++) {
3845 struct object_array_entry *e = old_pending.objects + i;
3846 struct commit *commit = handle_commit(revs, e);
3847 if (commit) {
3848 if (!(commit->object.flags & SEEN)) {
3849 commit->object.flags |= SEEN;
3850 next = commit_list_append(commit, next);
3854 object_array_clear(&old_pending);
3856 /* Signal whether we need per-parent treesame decoration */
3857 if (revs->simplify_merges ||
3858 (revs->limited && limiting_can_increase_treesame(revs)))
3859 revs->treesame.name = "treesame";
3861 if (revs->exclude_promisor_objects) {
3862 for_each_packed_object(mark_uninteresting, revs,
3863 FOR_EACH_OBJECT_PROMISOR_ONLY);
3866 if (!revs->reflog_info)
3867 prepare_to_use_bloom_filter(revs);
3868 if (!revs->unsorted_input)
3869 commit_list_sort_by_date(&revs->commits);
3870 if (revs->no_walk)
3871 return 0;
3872 if (revs->limited) {
3873 if (limit_list(revs) < 0)
3874 return -1;
3875 if (revs->topo_order)
3876 sort_in_topological_order(&revs->commits, revs->sort_order);
3877 } else if (revs->topo_order)
3878 init_topo_walk(revs);
3879 if (revs->line_level_traverse && want_ancestry(revs))
3881 * At the moment we can only do line-level log with parent
3882 * rewriting by performing this expensive pre-filtering step.
3883 * If parent rewriting is not requested, then we rather
3884 * perform the line-level log filtering during the regular
3885 * history traversal.
3887 line_log_filter(revs);
3888 if (revs->simplify_merges)
3889 simplify_merges(revs);
3890 if (revs->children.name)
3891 set_children(revs);
3893 return 0;
3896 static enum rewrite_result rewrite_one_1(struct rev_info *revs,
3897 struct commit **pp,
3898 struct prio_queue *queue)
3900 for (;;) {
3901 struct commit *p = *pp;
3902 if (!revs->limited)
3903 if (process_parents(revs, p, NULL, queue) < 0)
3904 return rewrite_one_error;
3905 if (p->object.flags & UNINTERESTING)
3906 return rewrite_one_ok;
3907 if (!(p->object.flags & TREESAME))
3908 return rewrite_one_ok;
3909 if (!p->parents)
3910 return rewrite_one_noparents;
3911 if (!(p = one_relevant_parent(revs, p->parents)))
3912 return rewrite_one_ok;
3913 *pp = p;
3917 static void merge_queue_into_list(struct prio_queue *q, struct commit_list **list)
3919 while (q->nr) {
3920 struct commit *item = prio_queue_peek(q);
3921 struct commit_list *p = *list;
3923 if (p && p->item->date >= item->date)
3924 list = &p->next;
3925 else {
3926 p = commit_list_insert(item, list);
3927 list = &p->next; /* skip newly added item */
3928 prio_queue_get(q); /* pop item */
3933 static enum rewrite_result rewrite_one(struct rev_info *revs, struct commit **pp)
3935 struct prio_queue queue = { compare_commits_by_commit_date };
3936 enum rewrite_result ret = rewrite_one_1(revs, pp, &queue);
3937 merge_queue_into_list(&queue, &revs->commits);
3938 clear_prio_queue(&queue);
3939 return ret;
3942 int rewrite_parents(struct rev_info *revs, struct commit *commit,
3943 rewrite_parent_fn_t rewrite_parent)
3945 struct commit_list **pp = &commit->parents;
3946 while (*pp) {
3947 struct commit_list *parent = *pp;
3948 switch (rewrite_parent(revs, &parent->item)) {
3949 case rewrite_one_ok:
3950 break;
3951 case rewrite_one_noparents:
3952 *pp = parent->next;
3953 continue;
3954 case rewrite_one_error:
3955 return -1;
3957 pp = &parent->next;
3959 remove_duplicate_parents(revs, commit);
3960 return 0;
3963 static int commit_match(struct commit *commit, struct rev_info *opt)
3965 int retval;
3966 const char *encoding;
3967 const char *message;
3968 struct strbuf buf = STRBUF_INIT;
3970 if (!opt->grep_filter.pattern_list && !opt->grep_filter.header_list)
3971 return 1;
3973 /* Prepend "fake" headers as needed */
3974 if (opt->grep_filter.use_reflog_filter) {
3975 strbuf_addstr(&buf, "reflog ");
3976 get_reflog_message(&buf, opt->reflog_info);
3977 strbuf_addch(&buf, '\n');
3981 * We grep in the user's output encoding, under the assumption that it
3982 * is the encoding they are most likely to write their grep pattern
3983 * for. In addition, it means we will match the "notes" encoding below,
3984 * so we will not end up with a buffer that has two different encodings
3985 * in it.
3987 encoding = get_log_output_encoding();
3988 message = repo_logmsg_reencode(the_repository, commit, NULL, encoding);
3990 /* Copy the commit to temporary if we are using "fake" headers */
3991 if (buf.len)
3992 strbuf_addstr(&buf, message);
3994 if (opt->grep_filter.header_list && opt->mailmap) {
3995 const char *commit_headers[] = { "author ", "committer ", NULL };
3997 if (!buf.len)
3998 strbuf_addstr(&buf, message);
4000 apply_mailmap_to_header(&buf, commit_headers, opt->mailmap);
4003 /* Append "fake" message parts as needed */
4004 if (opt->show_notes) {
4005 if (!buf.len)
4006 strbuf_addstr(&buf, message);
4007 format_display_notes(&commit->object.oid, &buf, encoding, 1);
4011 * Find either in the original commit message, or in the temporary.
4012 * Note that we cast away the constness of "message" here. It is
4013 * const because it may come from the cached commit buffer. That's OK,
4014 * because we know that it is modifiable heap memory, and that while
4015 * grep_buffer may modify it for speed, it will restore any
4016 * changes before returning.
4018 if (buf.len)
4019 retval = grep_buffer(&opt->grep_filter, buf.buf, buf.len);
4020 else
4021 retval = grep_buffer(&opt->grep_filter,
4022 (char *)message, strlen(message));
4023 strbuf_release(&buf);
4024 repo_unuse_commit_buffer(the_repository, commit, message);
4025 return retval;
4028 static inline int want_ancestry(const struct rev_info *revs)
4030 return (revs->rewrite_parents || revs->children.name);
4034 * Return a timestamp to be used for --since/--until comparisons for this
4035 * commit, based on the revision options.
4037 static timestamp_t comparison_date(const struct rev_info *revs,
4038 struct commit *commit)
4040 return revs->reflog_info ?
4041 get_reflog_timestamp(revs->reflog_info) :
4042 commit->date;
4045 enum commit_action get_commit_action(struct rev_info *revs, struct commit *commit)
4047 if (commit->object.flags & SHOWN)
4048 return commit_ignore;
4049 if (revs->unpacked && has_object_pack(&commit->object.oid))
4050 return commit_ignore;
4051 if (revs->no_kept_objects) {
4052 if (has_object_kept_pack(&commit->object.oid,
4053 revs->keep_pack_cache_flags))
4054 return commit_ignore;
4056 if (commit->object.flags & UNINTERESTING)
4057 return commit_ignore;
4058 if (revs->line_level_traverse && !want_ancestry(revs)) {
4060 * In case of line-level log with parent rewriting
4061 * prepare_revision_walk() already took care of all line-level
4062 * log filtering, and there is nothing left to do here.
4064 * If parent rewriting was not requested, then this is the
4065 * place to perform the line-level log filtering. Notably,
4066 * this check, though expensive, must come before the other,
4067 * cheaper filtering conditions, because the tracked line
4068 * ranges must be adjusted even when the commit will end up
4069 * being ignored based on other conditions.
4071 if (!line_log_process_ranges_arbitrary_commit(revs, commit))
4072 return commit_ignore;
4074 if (revs->min_age != -1 &&
4075 comparison_date(revs, commit) > revs->min_age)
4076 return commit_ignore;
4077 if (revs->max_age_as_filter != -1 &&
4078 comparison_date(revs, commit) < revs->max_age_as_filter)
4079 return commit_ignore;
4080 if (revs->min_parents || (revs->max_parents >= 0)) {
4081 int n = commit_list_count(commit->parents);
4082 if ((n < revs->min_parents) ||
4083 ((revs->max_parents >= 0) && (n > revs->max_parents)))
4084 return commit_ignore;
4086 if (!commit_match(commit, revs))
4087 return commit_ignore;
4088 if (revs->prune && revs->dense) {
4089 /* Commit without changes? */
4090 if (commit->object.flags & TREESAME) {
4091 int n;
4092 struct commit_list *p;
4093 /* drop merges unless we want parenthood */
4094 if (!want_ancestry(revs))
4095 return commit_ignore;
4097 if (revs->show_pulls && (commit->object.flags & PULL_MERGE))
4098 return commit_show;
4101 * If we want ancestry, then need to keep any merges
4102 * between relevant commits to tie together topology.
4103 * For consistency with TREESAME and simplification
4104 * use "relevant" here rather than just INTERESTING,
4105 * to treat bottom commit(s) as part of the topology.
4107 for (n = 0, p = commit->parents; p; p = p->next)
4108 if (relevant_commit(p->item))
4109 if (++n >= 2)
4110 return commit_show;
4111 return commit_ignore;
4114 return commit_show;
4117 define_commit_slab(saved_parents, struct commit_list *);
4119 #define EMPTY_PARENT_LIST ((struct commit_list *)-1)
4122 * You may only call save_parents() once per commit (this is checked
4123 * for non-root commits).
4125 static void save_parents(struct rev_info *revs, struct commit *commit)
4127 struct commit_list **pp;
4129 if (!revs->saved_parents_slab) {
4130 revs->saved_parents_slab = xmalloc(sizeof(struct saved_parents));
4131 init_saved_parents(revs->saved_parents_slab);
4134 pp = saved_parents_at(revs->saved_parents_slab, commit);
4137 * When walking with reflogs, we may visit the same commit
4138 * several times: once for each appearance in the reflog.
4140 * In this case, save_parents() will be called multiple times.
4141 * We want to keep only the first set of parents. We need to
4142 * store a sentinel value for an empty (i.e., NULL) parent
4143 * list to distinguish it from a not-yet-saved list, however.
4145 if (*pp)
4146 return;
4147 if (commit->parents)
4148 *pp = copy_commit_list(commit->parents);
4149 else
4150 *pp = EMPTY_PARENT_LIST;
4153 static void free_saved_parents(struct rev_info *revs)
4155 if (revs->saved_parents_slab)
4156 clear_saved_parents(revs->saved_parents_slab);
4159 struct commit_list *get_saved_parents(struct rev_info *revs, const struct commit *commit)
4161 struct commit_list *parents;
4163 if (!revs->saved_parents_slab)
4164 return commit->parents;
4166 parents = *saved_parents_at(revs->saved_parents_slab, commit);
4167 if (parents == EMPTY_PARENT_LIST)
4168 return NULL;
4169 return parents;
4172 enum commit_action simplify_commit(struct rev_info *revs, struct commit *commit)
4174 enum commit_action action = get_commit_action(revs, commit);
4176 if (action == commit_show &&
4177 revs->prune && revs->dense && want_ancestry(revs)) {
4179 * --full-diff on simplified parents is no good: it
4180 * will show spurious changes from the commits that
4181 * were elided. So we save the parents on the side
4182 * when --full-diff is in effect.
4184 if (revs->full_diff)
4185 save_parents(revs, commit);
4186 if (rewrite_parents(revs, commit, rewrite_one) < 0)
4187 return commit_error;
4189 return action;
4192 static void track_linear(struct rev_info *revs, struct commit *commit)
4194 if (revs->track_first_time) {
4195 revs->linear = 1;
4196 revs->track_first_time = 0;
4197 } else {
4198 struct commit_list *p;
4199 for (p = revs->previous_parents; p; p = p->next)
4200 if (p->item == NULL || /* first commit */
4201 oideq(&p->item->object.oid, &commit->object.oid))
4202 break;
4203 revs->linear = p != NULL;
4205 if (revs->reverse) {
4206 if (revs->linear)
4207 commit->object.flags |= TRACK_LINEAR;
4209 free_commit_list(revs->previous_parents);
4210 revs->previous_parents = copy_commit_list(commit->parents);
4213 static struct commit *get_revision_1(struct rev_info *revs)
4215 while (1) {
4216 struct commit *commit;
4218 if (revs->reflog_info)
4219 commit = next_reflog_entry(revs->reflog_info);
4220 else if (revs->topo_walk_info)
4221 commit = next_topo_commit(revs);
4222 else
4223 commit = pop_commit(&revs->commits);
4225 if (!commit)
4226 return NULL;
4228 if (revs->reflog_info)
4229 commit->object.flags &= ~(ADDED | SEEN | SHOWN);
4232 * If we haven't done the list limiting, we need to look at
4233 * the parents here. We also need to do the date-based limiting
4234 * that we'd otherwise have done in limit_list().
4236 if (!revs->limited) {
4237 if (revs->max_age != -1 &&
4238 comparison_date(revs, commit) < revs->max_age)
4239 continue;
4241 if (revs->reflog_info)
4242 try_to_simplify_commit(revs, commit);
4243 else if (revs->topo_walk_info)
4244 expand_topo_walk(revs, commit);
4245 else if (process_parents(revs, commit, &revs->commits, NULL) < 0) {
4246 if (!revs->ignore_missing_links)
4247 die("Failed to traverse parents of commit %s",
4248 oid_to_hex(&commit->object.oid));
4252 switch (simplify_commit(revs, commit)) {
4253 case commit_ignore:
4254 continue;
4255 case commit_error:
4256 die("Failed to simplify parents of commit %s",
4257 oid_to_hex(&commit->object.oid));
4258 default:
4259 if (revs->track_linear)
4260 track_linear(revs, commit);
4261 return commit;
4267 * Return true for entries that have not yet been shown. (This is an
4268 * object_array_each_func_t.)
4270 static int entry_unshown(struct object_array_entry *entry, void *cb_data UNUSED)
4272 return !(entry->item->flags & SHOWN);
4276 * If array is on the verge of a realloc, garbage-collect any entries
4277 * that have already been shown to try to free up some space.
4279 static void gc_boundary(struct object_array *array)
4281 if (array->nr == array->alloc)
4282 object_array_filter(array, entry_unshown, NULL);
4285 static void create_boundary_commit_list(struct rev_info *revs)
4287 unsigned i;
4288 struct commit *c;
4289 struct object_array *array = &revs->boundary_commits;
4290 struct object_array_entry *objects = array->objects;
4293 * If revs->commits is non-NULL at this point, an error occurred in
4294 * get_revision_1(). Ignore the error and continue printing the
4295 * boundary commits anyway. (This is what the code has always
4296 * done.)
4298 free_commit_list(revs->commits);
4299 revs->commits = NULL;
4302 * Put all of the actual boundary commits from revs->boundary_commits
4303 * into revs->commits
4305 for (i = 0; i < array->nr; i++) {
4306 c = (struct commit *)(objects[i].item);
4307 if (!c)
4308 continue;
4309 if (!(c->object.flags & CHILD_SHOWN))
4310 continue;
4311 if (c->object.flags & (SHOWN | BOUNDARY))
4312 continue;
4313 c->object.flags |= BOUNDARY;
4314 commit_list_insert(c, &revs->commits);
4318 * If revs->topo_order is set, sort the boundary commits
4319 * in topological order
4321 sort_in_topological_order(&revs->commits, revs->sort_order);
4324 static struct commit *get_revision_internal(struct rev_info *revs)
4326 struct commit *c = NULL;
4327 struct commit_list *l;
4329 if (revs->boundary == 2) {
4331 * All of the normal commits have already been returned,
4332 * and we are now returning boundary commits.
4333 * create_boundary_commit_list() has populated
4334 * revs->commits with the remaining commits to return.
4336 c = pop_commit(&revs->commits);
4337 if (c)
4338 c->object.flags |= SHOWN;
4339 return c;
4343 * If our max_count counter has reached zero, then we are done. We
4344 * don't simply return NULL because we still might need to show
4345 * boundary commits. But we want to avoid calling get_revision_1, which
4346 * might do a considerable amount of work finding the next commit only
4347 * for us to throw it away.
4349 * If it is non-zero, then either we don't have a max_count at all
4350 * (-1), or it is still counting, in which case we decrement.
4352 if (revs->max_count) {
4353 c = get_revision_1(revs);
4354 if (c) {
4355 while (revs->skip_count > 0) {
4356 revs->skip_count--;
4357 c = get_revision_1(revs);
4358 if (!c)
4359 break;
4363 if (revs->max_count > 0)
4364 revs->max_count--;
4367 if (c)
4368 c->object.flags |= SHOWN;
4370 if (!revs->boundary)
4371 return c;
4373 if (!c) {
4375 * get_revision_1() runs out the commits, and
4376 * we are done computing the boundaries.
4377 * switch to boundary commits output mode.
4379 revs->boundary = 2;
4382 * Update revs->commits to contain the list of
4383 * boundary commits.
4385 create_boundary_commit_list(revs);
4387 return get_revision_internal(revs);
4391 * boundary commits are the commits that are parents of the
4392 * ones we got from get_revision_1() but they themselves are
4393 * not returned from get_revision_1(). Before returning
4394 * 'c', we need to mark its parents that they could be boundaries.
4397 for (l = c->parents; l; l = l->next) {
4398 struct object *p;
4399 p = &(l->item->object);
4400 if (p->flags & (CHILD_SHOWN | SHOWN))
4401 continue;
4402 p->flags |= CHILD_SHOWN;
4403 gc_boundary(&revs->boundary_commits);
4404 add_object_array(p, NULL, &revs->boundary_commits);
4407 return c;
4410 struct commit *get_revision(struct rev_info *revs)
4412 struct commit *c;
4413 struct commit_list *reversed;
4415 if (revs->reverse) {
4416 reversed = NULL;
4417 while ((c = get_revision_internal(revs)))
4418 commit_list_insert(c, &reversed);
4419 revs->commits = reversed;
4420 revs->reverse = 0;
4421 revs->reverse_output_stage = 1;
4424 if (revs->reverse_output_stage) {
4425 c = pop_commit(&revs->commits);
4426 if (revs->track_linear)
4427 revs->linear = !!(c && c->object.flags & TRACK_LINEAR);
4428 return c;
4431 c = get_revision_internal(revs);
4432 if (c && revs->graph)
4433 graph_update(revs->graph, c);
4434 if (!c) {
4435 free_saved_parents(revs);
4436 free_commit_list(revs->previous_parents);
4437 revs->previous_parents = NULL;
4439 return c;
4442 const char *get_revision_mark(const struct rev_info *revs, const struct commit *commit)
4444 if (commit->object.flags & BOUNDARY)
4445 return "-";
4446 else if (commit->object.flags & UNINTERESTING)
4447 return "^";
4448 else if (commit->object.flags & PATCHSAME)
4449 return "=";
4450 else if (!revs || revs->left_right) {
4451 if (commit->object.flags & SYMMETRIC_LEFT)
4452 return "<";
4453 else
4454 return ">";
4455 } else if (revs->graph)
4456 return "*";
4457 else if (revs->cherry_mark)
4458 return "+";
4459 return "";
4462 void put_revision_mark(const struct rev_info *revs, const struct commit *commit)
4464 const char *mark = get_revision_mark(revs, commit);
4465 if (!strlen(mark))
4466 return;
4467 fputs(mark, stdout);
4468 putchar(' ');