Merge branch 'sg/index-format-doc-update' into maint
[git/debian.git] / revision.c
blob211352795c5529a7ee7efed0162b65ed85158c5b
1 #include "cache.h"
2 #include "object-store.h"
3 #include "tag.h"
4 #include "blob.h"
5 #include "tree.h"
6 #include "commit.h"
7 #include "diff.h"
8 #include "diff-merges.h"
9 #include "refs.h"
10 #include "revision.h"
11 #include "repository.h"
12 #include "graph.h"
13 #include "grep.h"
14 #include "reflog-walk.h"
15 #include "patch-ids.h"
16 #include "decorate.h"
17 #include "log-tree.h"
18 #include "string-list.h"
19 #include "line-log.h"
20 #include "mailmap.h"
21 #include "commit-slab.h"
22 #include "dir.h"
23 #include "cache-tree.h"
24 #include "bisect.h"
25 #include "packfile.h"
26 #include "worktree.h"
27 #include "strvec.h"
28 #include "commit-reach.h"
29 #include "commit-graph.h"
30 #include "prio-queue.h"
31 #include "hashmap.h"
32 #include "utf8.h"
33 #include "bloom.h"
34 #include "json-writer.h"
35 #include "list-objects-filter-options.h"
37 volatile show_early_output_fn_t show_early_output;
39 static const char *term_bad;
40 static const char *term_good;
42 implement_shared_commit_slab(revision_sources, char *);
44 static inline int want_ancestry(const struct rev_info *revs);
46 void show_object_with_name(FILE *out, struct object *obj, const char *name)
48 fprintf(out, "%s ", oid_to_hex(&obj->oid));
50 * This "for (const char *p = ..." is made as a first step towards
51 * making use of such declarations elsewhere in our codebase. If
52 * it causes compilation problems on your platform, please report
53 * it to the Git mailing list at git@vger.kernel.org. In the meantime,
54 * adding -std=gnu99 to CFLAGS may help if you are with older GCC.
56 for (const char *p = name; *p && *p != '\n'; p++)
57 fputc(*p, out);
58 fputc('\n', out);
61 static void mark_blob_uninteresting(struct blob *blob)
63 if (!blob)
64 return;
65 if (blob->object.flags & UNINTERESTING)
66 return;
67 blob->object.flags |= UNINTERESTING;
70 static void mark_tree_contents_uninteresting(struct repository *r,
71 struct tree *tree)
73 struct tree_desc desc;
74 struct name_entry entry;
76 if (parse_tree_gently(tree, 1) < 0)
77 return;
79 init_tree_desc(&desc, tree->buffer, tree->size);
80 while (tree_entry(&desc, &entry)) {
81 switch (object_type(entry.mode)) {
82 case OBJ_TREE:
83 mark_tree_uninteresting(r, lookup_tree(r, &entry.oid));
84 break;
85 case OBJ_BLOB:
86 mark_blob_uninteresting(lookup_blob(r, &entry.oid));
87 break;
88 default:
89 /* Subproject commit - not in this repository */
90 break;
95 * We don't care about the tree any more
96 * after it has been marked uninteresting.
98 free_tree_buffer(tree);
101 void mark_tree_uninteresting(struct repository *r, struct tree *tree)
103 struct object *obj;
105 if (!tree)
106 return;
108 obj = &tree->object;
109 if (obj->flags & UNINTERESTING)
110 return;
111 obj->flags |= UNINTERESTING;
112 mark_tree_contents_uninteresting(r, tree);
115 struct path_and_oids_entry {
116 struct hashmap_entry ent;
117 char *path;
118 struct oidset trees;
121 static int path_and_oids_cmp(const void *hashmap_cmp_fn_data,
122 const struct hashmap_entry *eptr,
123 const struct hashmap_entry *entry_or_key,
124 const void *keydata)
126 const struct path_and_oids_entry *e1, *e2;
128 e1 = container_of(eptr, const struct path_and_oids_entry, ent);
129 e2 = container_of(entry_or_key, const struct path_and_oids_entry, ent);
131 return strcmp(e1->path, e2->path);
134 static void paths_and_oids_clear(struct hashmap *map)
136 struct hashmap_iter iter;
137 struct path_and_oids_entry *entry;
139 hashmap_for_each_entry(map, &iter, entry, ent /* member name */) {
140 oidset_clear(&entry->trees);
141 free(entry->path);
144 hashmap_clear_and_free(map, struct path_and_oids_entry, ent);
147 static void paths_and_oids_insert(struct hashmap *map,
148 const char *path,
149 const struct object_id *oid)
151 int hash = strhash(path);
152 struct path_and_oids_entry key;
153 struct path_and_oids_entry *entry;
155 hashmap_entry_init(&key.ent, hash);
157 /* use a shallow copy for the lookup */
158 key.path = (char *)path;
159 oidset_init(&key.trees, 0);
161 entry = hashmap_get_entry(map, &key, ent, NULL);
162 if (!entry) {
163 CALLOC_ARRAY(entry, 1);
164 hashmap_entry_init(&entry->ent, hash);
165 entry->path = xstrdup(key.path);
166 oidset_init(&entry->trees, 16);
167 hashmap_put(map, &entry->ent);
170 oidset_insert(&entry->trees, oid);
173 static void add_children_by_path(struct repository *r,
174 struct tree *tree,
175 struct hashmap *map)
177 struct tree_desc desc;
178 struct name_entry entry;
180 if (!tree)
181 return;
183 if (parse_tree_gently(tree, 1) < 0)
184 return;
186 init_tree_desc(&desc, tree->buffer, tree->size);
187 while (tree_entry(&desc, &entry)) {
188 switch (object_type(entry.mode)) {
189 case OBJ_TREE:
190 paths_and_oids_insert(map, entry.path, &entry.oid);
192 if (tree->object.flags & UNINTERESTING) {
193 struct tree *child = lookup_tree(r, &entry.oid);
194 if (child)
195 child->object.flags |= UNINTERESTING;
197 break;
198 case OBJ_BLOB:
199 if (tree->object.flags & UNINTERESTING) {
200 struct blob *child = lookup_blob(r, &entry.oid);
201 if (child)
202 child->object.flags |= UNINTERESTING;
204 break;
205 default:
206 /* Subproject commit - not in this repository */
207 break;
211 free_tree_buffer(tree);
214 void mark_trees_uninteresting_sparse(struct repository *r,
215 struct oidset *trees)
217 unsigned has_interesting = 0, has_uninteresting = 0;
218 struct hashmap map = HASHMAP_INIT(path_and_oids_cmp, NULL);
219 struct hashmap_iter map_iter;
220 struct path_and_oids_entry *entry;
221 struct object_id *oid;
222 struct oidset_iter iter;
224 oidset_iter_init(trees, &iter);
225 while ((!has_interesting || !has_uninteresting) &&
226 (oid = oidset_iter_next(&iter))) {
227 struct tree *tree = lookup_tree(r, oid);
229 if (!tree)
230 continue;
232 if (tree->object.flags & UNINTERESTING)
233 has_uninteresting = 1;
234 else
235 has_interesting = 1;
238 /* Do not walk unless we have both types of trees. */
239 if (!has_uninteresting || !has_interesting)
240 return;
242 oidset_iter_init(trees, &iter);
243 while ((oid = oidset_iter_next(&iter))) {
244 struct tree *tree = lookup_tree(r, oid);
245 add_children_by_path(r, tree, &map);
248 hashmap_for_each_entry(&map, &map_iter, entry, ent /* member name */)
249 mark_trees_uninteresting_sparse(r, &entry->trees);
251 paths_and_oids_clear(&map);
254 struct commit_stack {
255 struct commit **items;
256 size_t nr, alloc;
258 #define COMMIT_STACK_INIT { 0 }
260 static void commit_stack_push(struct commit_stack *stack, struct commit *commit)
262 ALLOC_GROW(stack->items, stack->nr + 1, stack->alloc);
263 stack->items[stack->nr++] = commit;
266 static struct commit *commit_stack_pop(struct commit_stack *stack)
268 return stack->nr ? stack->items[--stack->nr] : NULL;
271 static void commit_stack_clear(struct commit_stack *stack)
273 FREE_AND_NULL(stack->items);
274 stack->nr = stack->alloc = 0;
277 static void mark_one_parent_uninteresting(struct rev_info *revs, struct commit *commit,
278 struct commit_stack *pending)
280 struct commit_list *l;
282 if (commit->object.flags & UNINTERESTING)
283 return;
284 commit->object.flags |= UNINTERESTING;
287 * Normally we haven't parsed the parent
288 * yet, so we won't have a parent of a parent
289 * here. However, it may turn out that we've
290 * reached this commit some other way (where it
291 * wasn't uninteresting), in which case we need
292 * to mark its parents recursively too..
294 for (l = commit->parents; l; l = l->next) {
295 commit_stack_push(pending, l->item);
296 if (revs && revs->exclude_first_parent_only)
297 break;
301 void mark_parents_uninteresting(struct rev_info *revs, struct commit *commit)
303 struct commit_stack pending = COMMIT_STACK_INIT;
304 struct commit_list *l;
306 for (l = commit->parents; l; l = l->next) {
307 mark_one_parent_uninteresting(revs, l->item, &pending);
308 if (revs && revs->exclude_first_parent_only)
309 break;
312 while (pending.nr > 0)
313 mark_one_parent_uninteresting(revs, commit_stack_pop(&pending),
314 &pending);
316 commit_stack_clear(&pending);
319 static void add_pending_object_with_path(struct rev_info *revs,
320 struct object *obj,
321 const char *name, unsigned mode,
322 const char *path)
324 struct interpret_branch_name_options options = { 0 };
325 if (!obj)
326 return;
327 if (revs->no_walk && (obj->flags & UNINTERESTING))
328 revs->no_walk = 0;
329 if (revs->reflog_info && obj->type == OBJ_COMMIT) {
330 struct strbuf buf = STRBUF_INIT;
331 size_t namelen = strlen(name);
332 int len = interpret_branch_name(name, namelen, &buf, &options);
334 if (0 < len && len < namelen && buf.len)
335 strbuf_addstr(&buf, name + len);
336 add_reflog_for_walk(revs->reflog_info,
337 (struct commit *)obj,
338 buf.buf[0] ? buf.buf: name);
339 strbuf_release(&buf);
340 return; /* do not add the commit itself */
342 add_object_array_with_path(obj, name, &revs->pending, mode, path);
345 static void add_pending_object_with_mode(struct rev_info *revs,
346 struct object *obj,
347 const char *name, unsigned mode)
349 add_pending_object_with_path(revs, obj, name, mode, NULL);
352 void add_pending_object(struct rev_info *revs,
353 struct object *obj, const char *name)
355 add_pending_object_with_mode(revs, obj, name, S_IFINVALID);
358 void add_head_to_pending(struct rev_info *revs)
360 struct object_id oid;
361 struct object *obj;
362 if (get_oid("HEAD", &oid))
363 return;
364 obj = parse_object(revs->repo, &oid);
365 if (!obj)
366 return;
367 add_pending_object(revs, obj, "HEAD");
370 static struct object *get_reference(struct rev_info *revs, const char *name,
371 const struct object_id *oid,
372 unsigned int flags)
374 struct object *object;
375 struct commit *commit;
378 * If the repository has commit graphs, we try to opportunistically
379 * look up the object ID in those graphs. Like this, we can avoid
380 * parsing commit data from disk.
382 commit = lookup_commit_in_graph(revs->repo, oid);
383 if (commit)
384 object = &commit->object;
385 else
386 object = parse_object(revs->repo, oid);
388 if (!object) {
389 if (revs->ignore_missing)
390 return object;
391 if (revs->exclude_promisor_objects && is_promisor_object(oid))
392 return NULL;
393 die("bad object %s", name);
395 object->flags |= flags;
396 return object;
399 void add_pending_oid(struct rev_info *revs, const char *name,
400 const struct object_id *oid, unsigned int flags)
402 struct object *object = get_reference(revs, name, oid, flags);
403 add_pending_object(revs, object, name);
406 static struct commit *handle_commit(struct rev_info *revs,
407 struct object_array_entry *entry)
409 struct object *object = entry->item;
410 const char *name = entry->name;
411 const char *path = entry->path;
412 unsigned int mode = entry->mode;
413 unsigned long flags = object->flags;
416 * Tag object? Look what it points to..
418 while (object->type == OBJ_TAG) {
419 struct tag *tag = (struct tag *) object;
420 if (revs->tag_objects && !(flags & UNINTERESTING))
421 add_pending_object(revs, object, tag->tag);
422 object = parse_object(revs->repo, get_tagged_oid(tag));
423 if (!object) {
424 if (revs->ignore_missing_links || (flags & UNINTERESTING))
425 return NULL;
426 if (revs->exclude_promisor_objects &&
427 is_promisor_object(&tag->tagged->oid))
428 return NULL;
429 die("bad object %s", oid_to_hex(&tag->tagged->oid));
431 object->flags |= flags;
433 * We'll handle the tagged object by looping or dropping
434 * through to the non-tag handlers below. Do not
435 * propagate path data from the tag's pending entry.
437 path = NULL;
438 mode = 0;
442 * Commit object? Just return it, we'll do all the complex
443 * reachability crud.
445 if (object->type == OBJ_COMMIT) {
446 struct commit *commit = (struct commit *)object;
448 if (repo_parse_commit(revs->repo, commit) < 0)
449 die("unable to parse commit %s", name);
450 if (flags & UNINTERESTING) {
451 mark_parents_uninteresting(revs, commit);
453 if (!revs->topo_order || !generation_numbers_enabled(the_repository))
454 revs->limited = 1;
456 if (revs->sources) {
457 char **slot = revision_sources_at(revs->sources, commit);
459 if (!*slot)
460 *slot = xstrdup(name);
462 return commit;
466 * Tree object? Either mark it uninteresting, or add it
467 * to the list of objects to look at later..
469 if (object->type == OBJ_TREE) {
470 struct tree *tree = (struct tree *)object;
471 if (!revs->tree_objects)
472 return NULL;
473 if (flags & UNINTERESTING) {
474 mark_tree_contents_uninteresting(revs->repo, tree);
475 return NULL;
477 add_pending_object_with_path(revs, object, name, mode, path);
478 return NULL;
482 * Blob object? You know the drill by now..
484 if (object->type == OBJ_BLOB) {
485 if (!revs->blob_objects)
486 return NULL;
487 if (flags & UNINTERESTING)
488 return NULL;
489 add_pending_object_with_path(revs, object, name, mode, path);
490 return NULL;
492 die("%s is unknown object", name);
495 static int everybody_uninteresting(struct commit_list *orig,
496 struct commit **interesting_cache)
498 struct commit_list *list = orig;
500 if (*interesting_cache) {
501 struct commit *commit = *interesting_cache;
502 if (!(commit->object.flags & UNINTERESTING))
503 return 0;
506 while (list) {
507 struct commit *commit = list->item;
508 list = list->next;
509 if (commit->object.flags & UNINTERESTING)
510 continue;
512 *interesting_cache = commit;
513 return 0;
515 return 1;
519 * A definition of "relevant" commit that we can use to simplify limited graphs
520 * by eliminating side branches.
522 * A "relevant" commit is one that is !UNINTERESTING (ie we are including it
523 * in our list), or that is a specified BOTTOM commit. Then after computing
524 * a limited list, during processing we can generally ignore boundary merges
525 * coming from outside the graph, (ie from irrelevant parents), and treat
526 * those merges as if they were single-parent. TREESAME is defined to consider
527 * only relevant parents, if any. If we are TREESAME to our on-graph parents,
528 * we don't care if we were !TREESAME to non-graph parents.
530 * Treating bottom commits as relevant ensures that a limited graph's
531 * connection to the actual bottom commit is not viewed as a side branch, but
532 * treated as part of the graph. For example:
534 * ....Z...A---X---o---o---B
535 * . /
536 * W---Y
538 * When computing "A..B", the A-X connection is at least as important as
539 * Y-X, despite A being flagged UNINTERESTING.
541 * And when computing --ancestry-path "A..B", the A-X connection is more
542 * important than Y-X, despite both A and Y being flagged UNINTERESTING.
544 static inline int relevant_commit(struct commit *commit)
546 return (commit->object.flags & (UNINTERESTING | BOTTOM)) != UNINTERESTING;
550 * Return a single relevant commit from a parent list. If we are a TREESAME
551 * commit, and this selects one of our parents, then we can safely simplify to
552 * that parent.
554 static struct commit *one_relevant_parent(const struct rev_info *revs,
555 struct commit_list *orig)
557 struct commit_list *list = orig;
558 struct commit *relevant = NULL;
560 if (!orig)
561 return NULL;
564 * For 1-parent commits, or if first-parent-only, then return that
565 * first parent (even if not "relevant" by the above definition).
566 * TREESAME will have been set purely on that parent.
568 if (revs->first_parent_only || !orig->next)
569 return orig->item;
572 * For multi-parent commits, identify a sole relevant parent, if any.
573 * If we have only one relevant parent, then TREESAME will be set purely
574 * with regard to that parent, and we can simplify accordingly.
576 * If we have more than one relevant parent, or no relevant parents
577 * (and multiple irrelevant ones), then we can't select a parent here
578 * and return NULL.
580 while (list) {
581 struct commit *commit = list->item;
582 list = list->next;
583 if (relevant_commit(commit)) {
584 if (relevant)
585 return NULL;
586 relevant = commit;
589 return relevant;
593 * The goal is to get REV_TREE_NEW as the result only if the
594 * diff consists of all '+' (and no other changes), REV_TREE_OLD
595 * if the whole diff is removal of old data, and otherwise
596 * REV_TREE_DIFFERENT (of course if the trees are the same we
597 * want REV_TREE_SAME).
599 * The only time we care about the distinction is when
600 * remove_empty_trees is in effect, in which case we care only about
601 * whether the whole change is REV_TREE_NEW, or if there's another type
602 * of change. Which means we can stop the diff early in either of these
603 * cases:
605 * 1. We're not using remove_empty_trees at all.
607 * 2. We saw anything except REV_TREE_NEW.
609 #define REV_TREE_SAME 0
610 #define REV_TREE_NEW 1 /* Only new files */
611 #define REV_TREE_OLD 2 /* Only files removed */
612 #define REV_TREE_DIFFERENT 3 /* Mixed changes */
613 static int tree_difference = REV_TREE_SAME;
615 static void file_add_remove(struct diff_options *options,
616 int addremove, unsigned mode,
617 const struct object_id *oid,
618 int oid_valid,
619 const char *fullpath, unsigned dirty_submodule)
621 int diff = addremove == '+' ? REV_TREE_NEW : REV_TREE_OLD;
622 struct rev_info *revs = options->change_fn_data;
624 tree_difference |= diff;
625 if (!revs->remove_empty_trees || tree_difference != REV_TREE_NEW)
626 options->flags.has_changes = 1;
629 static void file_change(struct diff_options *options,
630 unsigned old_mode, unsigned new_mode,
631 const struct object_id *old_oid,
632 const struct object_id *new_oid,
633 int old_oid_valid, int new_oid_valid,
634 const char *fullpath,
635 unsigned old_dirty_submodule, unsigned new_dirty_submodule)
637 tree_difference = REV_TREE_DIFFERENT;
638 options->flags.has_changes = 1;
641 static int bloom_filter_atexit_registered;
642 static unsigned int count_bloom_filter_maybe;
643 static unsigned int count_bloom_filter_definitely_not;
644 static unsigned int count_bloom_filter_false_positive;
645 static unsigned int count_bloom_filter_not_present;
647 static void trace2_bloom_filter_statistics_atexit(void)
649 struct json_writer jw = JSON_WRITER_INIT;
651 jw_object_begin(&jw, 0);
652 jw_object_intmax(&jw, "filter_not_present", count_bloom_filter_not_present);
653 jw_object_intmax(&jw, "maybe", count_bloom_filter_maybe);
654 jw_object_intmax(&jw, "definitely_not", count_bloom_filter_definitely_not);
655 jw_object_intmax(&jw, "false_positive", count_bloom_filter_false_positive);
656 jw_end(&jw);
658 trace2_data_json("bloom", the_repository, "statistics", &jw);
660 jw_release(&jw);
663 static int forbid_bloom_filters(struct pathspec *spec)
665 if (spec->has_wildcard)
666 return 1;
667 if (spec->nr > 1)
668 return 1;
669 if (spec->magic & ~PATHSPEC_LITERAL)
670 return 1;
671 if (spec->nr && (spec->items[0].magic & ~PATHSPEC_LITERAL))
672 return 1;
674 return 0;
677 static void prepare_to_use_bloom_filter(struct rev_info *revs)
679 struct pathspec_item *pi;
680 char *path_alloc = NULL;
681 const char *path, *p;
682 size_t len;
683 int path_component_nr = 1;
685 if (!revs->commits)
686 return;
688 if (forbid_bloom_filters(&revs->prune_data))
689 return;
691 repo_parse_commit(revs->repo, revs->commits->item);
693 revs->bloom_filter_settings = get_bloom_filter_settings(revs->repo);
694 if (!revs->bloom_filter_settings)
695 return;
697 if (!revs->pruning.pathspec.nr)
698 return;
700 pi = &revs->pruning.pathspec.items[0];
702 /* remove single trailing slash from path, if needed */
703 if (pi->len > 0 && pi->match[pi->len - 1] == '/') {
704 path_alloc = xmemdupz(pi->match, pi->len - 1);
705 path = path_alloc;
706 } else
707 path = pi->match;
709 len = strlen(path);
710 if (!len) {
711 revs->bloom_filter_settings = NULL;
712 free(path_alloc);
713 return;
716 p = path;
717 while (*p) {
719 * At this point, the path is normalized to use Unix-style
720 * path separators. This is required due to how the
721 * changed-path Bloom filters store the paths.
723 if (*p == '/')
724 path_component_nr++;
725 p++;
728 revs->bloom_keys_nr = path_component_nr;
729 ALLOC_ARRAY(revs->bloom_keys, revs->bloom_keys_nr);
731 fill_bloom_key(path, len, &revs->bloom_keys[0],
732 revs->bloom_filter_settings);
733 path_component_nr = 1;
735 p = path + len - 1;
736 while (p > path) {
737 if (*p == '/')
738 fill_bloom_key(path, p - path,
739 &revs->bloom_keys[path_component_nr++],
740 revs->bloom_filter_settings);
741 p--;
744 if (trace2_is_enabled() && !bloom_filter_atexit_registered) {
745 atexit(trace2_bloom_filter_statistics_atexit);
746 bloom_filter_atexit_registered = 1;
749 free(path_alloc);
752 static int check_maybe_different_in_bloom_filter(struct rev_info *revs,
753 struct commit *commit)
755 struct bloom_filter *filter;
756 int result = 1, j;
758 if (!revs->repo->objects->commit_graph)
759 return -1;
761 if (commit_graph_generation(commit) == GENERATION_NUMBER_INFINITY)
762 return -1;
764 filter = get_bloom_filter(revs->repo, commit);
766 if (!filter) {
767 count_bloom_filter_not_present++;
768 return -1;
771 for (j = 0; result && j < revs->bloom_keys_nr; j++) {
772 result = bloom_filter_contains(filter,
773 &revs->bloom_keys[j],
774 revs->bloom_filter_settings);
777 if (result)
778 count_bloom_filter_maybe++;
779 else
780 count_bloom_filter_definitely_not++;
782 return result;
785 static int rev_compare_tree(struct rev_info *revs,
786 struct commit *parent, struct commit *commit, int nth_parent)
788 struct tree *t1 = get_commit_tree(parent);
789 struct tree *t2 = get_commit_tree(commit);
790 int bloom_ret = 1;
792 if (!t1)
793 return REV_TREE_NEW;
794 if (!t2)
795 return REV_TREE_OLD;
797 if (revs->simplify_by_decoration) {
799 * If we are simplifying by decoration, then the commit
800 * is worth showing if it has a tag pointing at it.
802 if (get_name_decoration(&commit->object))
803 return REV_TREE_DIFFERENT;
805 * A commit that is not pointed by a tag is uninteresting
806 * if we are not limited by path. This means that you will
807 * see the usual "commits that touch the paths" plus any
808 * tagged commit by specifying both --simplify-by-decoration
809 * and pathspec.
811 if (!revs->prune_data.nr)
812 return REV_TREE_SAME;
815 if (revs->bloom_keys_nr && !nth_parent) {
816 bloom_ret = check_maybe_different_in_bloom_filter(revs, commit);
818 if (bloom_ret == 0)
819 return REV_TREE_SAME;
822 tree_difference = REV_TREE_SAME;
823 revs->pruning.flags.has_changes = 0;
824 diff_tree_oid(&t1->object.oid, &t2->object.oid, "", &revs->pruning);
826 if (!nth_parent)
827 if (bloom_ret == 1 && tree_difference == REV_TREE_SAME)
828 count_bloom_filter_false_positive++;
830 return tree_difference;
833 static int rev_same_tree_as_empty(struct rev_info *revs, struct commit *commit)
835 struct tree *t1 = get_commit_tree(commit);
837 if (!t1)
838 return 0;
840 tree_difference = REV_TREE_SAME;
841 revs->pruning.flags.has_changes = 0;
842 diff_tree_oid(NULL, &t1->object.oid, "", &revs->pruning);
844 return tree_difference == REV_TREE_SAME;
847 struct treesame_state {
848 unsigned int nparents;
849 unsigned char treesame[FLEX_ARRAY];
852 static struct treesame_state *initialise_treesame(struct rev_info *revs, struct commit *commit)
854 unsigned n = commit_list_count(commit->parents);
855 struct treesame_state *st = xcalloc(1, st_add(sizeof(*st), n));
856 st->nparents = n;
857 add_decoration(&revs->treesame, &commit->object, st);
858 return st;
862 * Must be called immediately after removing the nth_parent from a commit's
863 * parent list, if we are maintaining the per-parent treesame[] decoration.
864 * This does not recalculate the master TREESAME flag - update_treesame()
865 * should be called to update it after a sequence of treesame[] modifications
866 * that may have affected it.
868 static int compact_treesame(struct rev_info *revs, struct commit *commit, unsigned nth_parent)
870 struct treesame_state *st;
871 int old_same;
873 if (!commit->parents) {
875 * Have just removed the only parent from a non-merge.
876 * Different handling, as we lack decoration.
878 if (nth_parent != 0)
879 die("compact_treesame %u", nth_parent);
880 old_same = !!(commit->object.flags & TREESAME);
881 if (rev_same_tree_as_empty(revs, commit))
882 commit->object.flags |= TREESAME;
883 else
884 commit->object.flags &= ~TREESAME;
885 return old_same;
888 st = lookup_decoration(&revs->treesame, &commit->object);
889 if (!st || nth_parent >= st->nparents)
890 die("compact_treesame %u", nth_parent);
892 old_same = st->treesame[nth_parent];
893 memmove(st->treesame + nth_parent,
894 st->treesame + nth_parent + 1,
895 st->nparents - nth_parent - 1);
898 * If we've just become a non-merge commit, update TREESAME
899 * immediately, and remove the no-longer-needed decoration.
900 * If still a merge, defer update until update_treesame().
902 if (--st->nparents == 1) {
903 if (commit->parents->next)
904 die("compact_treesame parents mismatch");
905 if (st->treesame[0] && revs->dense)
906 commit->object.flags |= TREESAME;
907 else
908 commit->object.flags &= ~TREESAME;
909 free(add_decoration(&revs->treesame, &commit->object, NULL));
912 return old_same;
915 static unsigned update_treesame(struct rev_info *revs, struct commit *commit)
917 if (commit->parents && commit->parents->next) {
918 unsigned n;
919 struct treesame_state *st;
920 struct commit_list *p;
921 unsigned relevant_parents;
922 unsigned relevant_change, irrelevant_change;
924 st = lookup_decoration(&revs->treesame, &commit->object);
925 if (!st)
926 die("update_treesame %s", oid_to_hex(&commit->object.oid));
927 relevant_parents = 0;
928 relevant_change = irrelevant_change = 0;
929 for (p = commit->parents, n = 0; p; n++, p = p->next) {
930 if (relevant_commit(p->item)) {
931 relevant_change |= !st->treesame[n];
932 relevant_parents++;
933 } else
934 irrelevant_change |= !st->treesame[n];
936 if (relevant_parents ? relevant_change : irrelevant_change)
937 commit->object.flags &= ~TREESAME;
938 else
939 commit->object.flags |= TREESAME;
942 return commit->object.flags & TREESAME;
945 static inline int limiting_can_increase_treesame(const struct rev_info *revs)
948 * TREESAME is irrelevant unless prune && dense;
949 * if simplify_history is set, we can't have a mixture of TREESAME and
950 * !TREESAME INTERESTING parents (and we don't have treesame[]
951 * decoration anyway);
952 * if first_parent_only is set, then the TREESAME flag is locked
953 * against the first parent (and again we lack treesame[] decoration).
955 return revs->prune && revs->dense &&
956 !revs->simplify_history &&
957 !revs->first_parent_only;
960 static void try_to_simplify_commit(struct rev_info *revs, struct commit *commit)
962 struct commit_list **pp, *parent;
963 struct treesame_state *ts = NULL;
964 int relevant_change = 0, irrelevant_change = 0;
965 int relevant_parents, nth_parent;
968 * If we don't do pruning, everything is interesting
970 if (!revs->prune)
971 return;
973 if (!get_commit_tree(commit))
974 return;
976 if (!commit->parents) {
977 if (rev_same_tree_as_empty(revs, commit))
978 commit->object.flags |= TREESAME;
979 return;
983 * Normal non-merge commit? If we don't want to make the
984 * history dense, we consider it always to be a change..
986 if (!revs->dense && !commit->parents->next)
987 return;
989 for (pp = &commit->parents, nth_parent = 0, relevant_parents = 0;
990 (parent = *pp) != NULL;
991 pp = &parent->next, nth_parent++) {
992 struct commit *p = parent->item;
993 if (relevant_commit(p))
994 relevant_parents++;
996 if (nth_parent == 1) {
998 * This our second loop iteration - so we now know
999 * we're dealing with a merge.
1001 * Do not compare with later parents when we care only about
1002 * the first parent chain, in order to avoid derailing the
1003 * traversal to follow a side branch that brought everything
1004 * in the path we are limited to by the pathspec.
1006 if (revs->first_parent_only)
1007 break;
1009 * If this will remain a potentially-simplifiable
1010 * merge, remember per-parent treesame if needed.
1011 * Initialise the array with the comparison from our
1012 * first iteration.
1014 if (revs->treesame.name &&
1015 !revs->simplify_history &&
1016 !(commit->object.flags & UNINTERESTING)) {
1017 ts = initialise_treesame(revs, commit);
1018 if (!(irrelevant_change || relevant_change))
1019 ts->treesame[0] = 1;
1022 if (repo_parse_commit(revs->repo, p) < 0)
1023 die("cannot simplify commit %s (because of %s)",
1024 oid_to_hex(&commit->object.oid),
1025 oid_to_hex(&p->object.oid));
1026 switch (rev_compare_tree(revs, p, commit, nth_parent)) {
1027 case REV_TREE_SAME:
1028 if (!revs->simplify_history || !relevant_commit(p)) {
1029 /* Even if a merge with an uninteresting
1030 * side branch brought the entire change
1031 * we are interested in, we do not want
1032 * to lose the other branches of this
1033 * merge, so we just keep going.
1035 if (ts)
1036 ts->treesame[nth_parent] = 1;
1037 continue;
1039 parent->next = NULL;
1040 commit->parents = parent;
1043 * A merge commit is a "diversion" if it is not
1044 * TREESAME to its first parent but is TREESAME
1045 * to a later parent. In the simplified history,
1046 * we "divert" the history walk to the later
1047 * parent. These commits are shown when "show_pulls"
1048 * is enabled, so do not mark the object as
1049 * TREESAME here.
1051 if (!revs->show_pulls || !nth_parent)
1052 commit->object.flags |= TREESAME;
1054 return;
1056 case REV_TREE_NEW:
1057 if (revs->remove_empty_trees &&
1058 rev_same_tree_as_empty(revs, p)) {
1059 /* We are adding all the specified
1060 * paths from this parent, so the
1061 * history beyond this parent is not
1062 * interesting. Remove its parents
1063 * (they are grandparents for us).
1064 * IOW, we pretend this parent is a
1065 * "root" commit.
1067 if (repo_parse_commit(revs->repo, p) < 0)
1068 die("cannot simplify commit %s (invalid %s)",
1069 oid_to_hex(&commit->object.oid),
1070 oid_to_hex(&p->object.oid));
1071 p->parents = NULL;
1073 /* fallthrough */
1074 case REV_TREE_OLD:
1075 case REV_TREE_DIFFERENT:
1076 if (relevant_commit(p))
1077 relevant_change = 1;
1078 else
1079 irrelevant_change = 1;
1081 if (!nth_parent)
1082 commit->object.flags |= PULL_MERGE;
1084 continue;
1086 die("bad tree compare for commit %s", oid_to_hex(&commit->object.oid));
1090 * TREESAME is straightforward for single-parent commits. For merge
1091 * commits, it is most useful to define it so that "irrelevant"
1092 * parents cannot make us !TREESAME - if we have any relevant
1093 * parents, then we only consider TREESAMEness with respect to them,
1094 * allowing irrelevant merges from uninteresting branches to be
1095 * simplified away. Only if we have only irrelevant parents do we
1096 * base TREESAME on them. Note that this logic is replicated in
1097 * update_treesame, which should be kept in sync.
1099 if (relevant_parents ? !relevant_change : !irrelevant_change)
1100 commit->object.flags |= TREESAME;
1103 static int process_parents(struct rev_info *revs, struct commit *commit,
1104 struct commit_list **list, struct prio_queue *queue)
1106 struct commit_list *parent = commit->parents;
1107 unsigned left_flag;
1109 if (commit->object.flags & ADDED)
1110 return 0;
1111 commit->object.flags |= ADDED;
1113 if (revs->include_check &&
1114 !revs->include_check(commit, revs->include_check_data))
1115 return 0;
1118 * If the commit is uninteresting, don't try to
1119 * prune parents - we want the maximal uninteresting
1120 * set.
1122 * Normally we haven't parsed the parent
1123 * yet, so we won't have a parent of a parent
1124 * here. However, it may turn out that we've
1125 * reached this commit some other way (where it
1126 * wasn't uninteresting), in which case we need
1127 * to mark its parents recursively too..
1129 if (commit->object.flags & UNINTERESTING) {
1130 while (parent) {
1131 struct commit *p = parent->item;
1132 parent = parent->next;
1133 if (p)
1134 p->object.flags |= UNINTERESTING;
1135 if (repo_parse_commit_gently(revs->repo, p, 1) < 0)
1136 continue;
1137 if (p->parents)
1138 mark_parents_uninteresting(revs, p);
1139 if (p->object.flags & SEEN)
1140 continue;
1141 p->object.flags |= (SEEN | NOT_USER_GIVEN);
1142 if (list)
1143 commit_list_insert_by_date(p, list);
1144 if (queue)
1145 prio_queue_put(queue, p);
1146 if (revs->exclude_first_parent_only)
1147 break;
1149 return 0;
1153 * Ok, the commit wasn't uninteresting. Try to
1154 * simplify the commit history and find the parent
1155 * that has no differences in the path set if one exists.
1157 try_to_simplify_commit(revs, commit);
1159 if (revs->no_walk)
1160 return 0;
1162 left_flag = (commit->object.flags & SYMMETRIC_LEFT);
1164 for (parent = commit->parents; parent; parent = parent->next) {
1165 struct commit *p = parent->item;
1166 int gently = revs->ignore_missing_links ||
1167 revs->exclude_promisor_objects;
1168 if (repo_parse_commit_gently(revs->repo, p, gently) < 0) {
1169 if (revs->exclude_promisor_objects &&
1170 is_promisor_object(&p->object.oid)) {
1171 if (revs->first_parent_only)
1172 break;
1173 continue;
1175 return -1;
1177 if (revs->sources) {
1178 char **slot = revision_sources_at(revs->sources, p);
1180 if (!*slot)
1181 *slot = *revision_sources_at(revs->sources, commit);
1183 p->object.flags |= left_flag;
1184 if (!(p->object.flags & SEEN)) {
1185 p->object.flags |= (SEEN | NOT_USER_GIVEN);
1186 if (list)
1187 commit_list_insert_by_date(p, list);
1188 if (queue)
1189 prio_queue_put(queue, p);
1191 if (revs->first_parent_only)
1192 break;
1194 return 0;
1197 static void cherry_pick_list(struct commit_list *list, struct rev_info *revs)
1199 struct commit_list *p;
1200 int left_count = 0, right_count = 0;
1201 int left_first;
1202 struct patch_ids ids;
1203 unsigned cherry_flag;
1205 /* First count the commits on the left and on the right */
1206 for (p = list; p; p = p->next) {
1207 struct commit *commit = p->item;
1208 unsigned flags = commit->object.flags;
1209 if (flags & BOUNDARY)
1211 else if (flags & SYMMETRIC_LEFT)
1212 left_count++;
1213 else
1214 right_count++;
1217 if (!left_count || !right_count)
1218 return;
1220 left_first = left_count < right_count;
1221 init_patch_ids(revs->repo, &ids);
1222 ids.diffopts.pathspec = revs->diffopt.pathspec;
1224 /* Compute patch-ids for one side */
1225 for (p = list; p; p = p->next) {
1226 struct commit *commit = p->item;
1227 unsigned flags = commit->object.flags;
1229 if (flags & BOUNDARY)
1230 continue;
1232 * If we have fewer left, left_first is set and we omit
1233 * commits on the right branch in this loop. If we have
1234 * fewer right, we skip the left ones.
1236 if (left_first != !!(flags & SYMMETRIC_LEFT))
1237 continue;
1238 add_commit_patch_id(commit, &ids);
1241 /* either cherry_mark or cherry_pick are true */
1242 cherry_flag = revs->cherry_mark ? PATCHSAME : SHOWN;
1244 /* Check the other side */
1245 for (p = list; p; p = p->next) {
1246 struct commit *commit = p->item;
1247 struct patch_id *id;
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 left branch in this loop.
1256 if (left_first == !!(flags & SYMMETRIC_LEFT))
1257 continue;
1260 * Have we seen the same patch id?
1262 id = patch_id_iter_first(commit, &ids);
1263 if (!id)
1264 continue;
1266 commit->object.flags |= cherry_flag;
1267 do {
1268 id->commit->object.flags |= cherry_flag;
1269 } while ((id = patch_id_iter_next(id, &ids)));
1272 free_patch_ids(&ids);
1275 /* How many extra uninteresting commits we want to see.. */
1276 #define SLOP 5
1278 static int still_interesting(struct commit_list *src, timestamp_t date, int slop,
1279 struct commit **interesting_cache)
1282 * No source list at all? We're definitely done..
1284 if (!src)
1285 return 0;
1288 * Does the destination list contain entries with a date
1289 * before the source list? Definitely _not_ done.
1291 if (date <= src->item->date)
1292 return SLOP;
1295 * Does the source list still have interesting commits in
1296 * it? Definitely not done..
1298 if (!everybody_uninteresting(src, interesting_cache))
1299 return SLOP;
1301 /* Ok, we're closing in.. */
1302 return slop-1;
1306 * "rev-list --ancestry-path A..B" computes commits that are ancestors
1307 * of B but not ancestors of A but further limits the result to those
1308 * that are descendants of A. This takes the list of bottom commits and
1309 * the result of "A..B" without --ancestry-path, and limits the latter
1310 * further to the ones that can reach one of the commits in "bottom".
1312 static void limit_to_ancestry(struct commit_list *bottom, struct commit_list *list)
1314 struct commit_list *p;
1315 struct commit_list *rlist = NULL;
1316 int made_progress;
1319 * Reverse the list so that it will be likely that we would
1320 * process parents before children.
1322 for (p = list; p; p = p->next)
1323 commit_list_insert(p->item, &rlist);
1325 for (p = bottom; p; p = p->next)
1326 p->item->object.flags |= TMP_MARK;
1329 * Mark the ones that can reach bottom commits in "list",
1330 * in a bottom-up fashion.
1332 do {
1333 made_progress = 0;
1334 for (p = rlist; p; p = p->next) {
1335 struct commit *c = p->item;
1336 struct commit_list *parents;
1337 if (c->object.flags & (TMP_MARK | UNINTERESTING))
1338 continue;
1339 for (parents = c->parents;
1340 parents;
1341 parents = parents->next) {
1342 if (!(parents->item->object.flags & TMP_MARK))
1343 continue;
1344 c->object.flags |= TMP_MARK;
1345 made_progress = 1;
1346 break;
1349 } while (made_progress);
1352 * NEEDSWORK: decide if we want to remove parents that are
1353 * not marked with TMP_MARK from commit->parents for commits
1354 * in the resulting list. We may not want to do that, though.
1358 * The ones that are not marked with TMP_MARK are uninteresting
1360 for (p = list; p; p = p->next) {
1361 struct commit *c = p->item;
1362 if (c->object.flags & TMP_MARK)
1363 continue;
1364 c->object.flags |= UNINTERESTING;
1367 /* We are done with the TMP_MARK */
1368 for (p = list; p; p = p->next)
1369 p->item->object.flags &= ~TMP_MARK;
1370 for (p = bottom; p; p = p->next)
1371 p->item->object.flags &= ~TMP_MARK;
1372 free_commit_list(rlist);
1376 * Before walking the history, keep the set of "negative" refs the
1377 * caller has asked to exclude.
1379 * This is used to compute "rev-list --ancestry-path A..B", as we need
1380 * to filter the result of "A..B" further to the ones that can actually
1381 * reach A.
1383 static struct commit_list *collect_bottom_commits(struct commit_list *list)
1385 struct commit_list *elem, *bottom = NULL;
1386 for (elem = list; elem; elem = elem->next)
1387 if (elem->item->object.flags & BOTTOM)
1388 commit_list_insert(elem->item, &bottom);
1389 return bottom;
1392 /* Assumes either left_only or right_only is set */
1393 static void limit_left_right(struct commit_list *list, struct rev_info *revs)
1395 struct commit_list *p;
1397 for (p = list; p; p = p->next) {
1398 struct commit *commit = p->item;
1400 if (revs->right_only) {
1401 if (commit->object.flags & SYMMETRIC_LEFT)
1402 commit->object.flags |= SHOWN;
1403 } else /* revs->left_only is set */
1404 if (!(commit->object.flags & SYMMETRIC_LEFT))
1405 commit->object.flags |= SHOWN;
1409 static int limit_list(struct rev_info *revs)
1411 int slop = SLOP;
1412 timestamp_t date = TIME_MAX;
1413 struct commit_list *original_list = revs->commits;
1414 struct commit_list *newlist = NULL;
1415 struct commit_list **p = &newlist;
1416 struct commit_list *bottom = NULL;
1417 struct commit *interesting_cache = NULL;
1419 if (revs->ancestry_path) {
1420 bottom = collect_bottom_commits(original_list);
1421 if (!bottom)
1422 die("--ancestry-path given but there are no bottom commits");
1425 while (original_list) {
1426 struct commit *commit = pop_commit(&original_list);
1427 struct object *obj = &commit->object;
1428 show_early_output_fn_t show;
1430 if (commit == interesting_cache)
1431 interesting_cache = NULL;
1433 if (revs->max_age != -1 && (commit->date < revs->max_age))
1434 obj->flags |= UNINTERESTING;
1435 if (process_parents(revs, commit, &original_list, NULL) < 0)
1436 return -1;
1437 if (obj->flags & UNINTERESTING) {
1438 mark_parents_uninteresting(revs, commit);
1439 slop = still_interesting(original_list, date, slop, &interesting_cache);
1440 if (slop)
1441 continue;
1442 break;
1444 if (revs->min_age != -1 && (commit->date > revs->min_age) &&
1445 !revs->line_level_traverse)
1446 continue;
1447 if (revs->max_age_as_filter != -1 &&
1448 (commit->date < revs->max_age_as_filter) && !revs->line_level_traverse)
1449 continue;
1450 date = commit->date;
1451 p = &commit_list_insert(commit, p)->next;
1453 show = show_early_output;
1454 if (!show)
1455 continue;
1457 show(revs, newlist);
1458 show_early_output = NULL;
1460 if (revs->cherry_pick || revs->cherry_mark)
1461 cherry_pick_list(newlist, revs);
1463 if (revs->left_only || revs->right_only)
1464 limit_left_right(newlist, revs);
1466 if (bottom)
1467 limit_to_ancestry(bottom, newlist);
1468 free_commit_list(bottom);
1471 * Check if any commits have become TREESAME by some of their parents
1472 * becoming UNINTERESTING.
1474 if (limiting_can_increase_treesame(revs)) {
1475 struct commit_list *list = NULL;
1476 for (list = newlist; list; list = list->next) {
1477 struct commit *c = list->item;
1478 if (c->object.flags & (UNINTERESTING | TREESAME))
1479 continue;
1480 update_treesame(revs, c);
1484 free_commit_list(original_list);
1485 revs->commits = newlist;
1486 return 0;
1490 * Add an entry to refs->cmdline with the specified information.
1491 * *name is copied.
1493 static void add_rev_cmdline(struct rev_info *revs,
1494 struct object *item,
1495 const char *name,
1496 int whence,
1497 unsigned flags)
1499 struct rev_cmdline_info *info = &revs->cmdline;
1500 unsigned int nr = info->nr;
1502 ALLOC_GROW(info->rev, nr + 1, info->alloc);
1503 info->rev[nr].item = item;
1504 info->rev[nr].name = xstrdup(name);
1505 info->rev[nr].whence = whence;
1506 info->rev[nr].flags = flags;
1507 info->nr++;
1510 static void add_rev_cmdline_list(struct rev_info *revs,
1511 struct commit_list *commit_list,
1512 int whence,
1513 unsigned flags)
1515 while (commit_list) {
1516 struct object *object = &commit_list->item->object;
1517 add_rev_cmdline(revs, object, oid_to_hex(&object->oid),
1518 whence, flags);
1519 commit_list = commit_list->next;
1523 struct all_refs_cb {
1524 int all_flags;
1525 int warned_bad_reflog;
1526 struct rev_info *all_revs;
1527 const char *name_for_errormsg;
1528 struct worktree *wt;
1531 int ref_excluded(struct string_list *ref_excludes, const char *path)
1533 struct string_list_item *item;
1535 if (!ref_excludes)
1536 return 0;
1537 for_each_string_list_item(item, ref_excludes) {
1538 if (!wildmatch(item->string, path, 0))
1539 return 1;
1541 return 0;
1544 static int handle_one_ref(const char *path, const struct object_id *oid,
1545 int flag, void *cb_data)
1547 struct all_refs_cb *cb = cb_data;
1548 struct object *object;
1550 if (ref_excluded(cb->all_revs->ref_excludes, path))
1551 return 0;
1553 object = get_reference(cb->all_revs, path, oid, cb->all_flags);
1554 add_rev_cmdline(cb->all_revs, object, path, REV_CMD_REF, cb->all_flags);
1555 add_pending_object(cb->all_revs, object, path);
1556 return 0;
1559 static void init_all_refs_cb(struct all_refs_cb *cb, struct rev_info *revs,
1560 unsigned flags)
1562 cb->all_revs = revs;
1563 cb->all_flags = flags;
1564 revs->rev_input_given = 1;
1565 cb->wt = NULL;
1568 void clear_ref_exclusion(struct string_list **ref_excludes_p)
1570 if (*ref_excludes_p) {
1571 string_list_clear(*ref_excludes_p, 0);
1572 free(*ref_excludes_p);
1574 *ref_excludes_p = NULL;
1577 void add_ref_exclusion(struct string_list **ref_excludes_p, const char *exclude)
1579 if (!*ref_excludes_p) {
1580 CALLOC_ARRAY(*ref_excludes_p, 1);
1581 (*ref_excludes_p)->strdup_strings = 1;
1583 string_list_append(*ref_excludes_p, exclude);
1586 static void handle_refs(struct ref_store *refs,
1587 struct rev_info *revs, unsigned flags,
1588 int (*for_each)(struct ref_store *, each_ref_fn, void *))
1590 struct all_refs_cb cb;
1592 if (!refs) {
1593 /* this could happen with uninitialized submodules */
1594 return;
1597 init_all_refs_cb(&cb, revs, flags);
1598 for_each(refs, handle_one_ref, &cb);
1601 static void handle_one_reflog_commit(struct object_id *oid, void *cb_data)
1603 struct all_refs_cb *cb = cb_data;
1604 if (!is_null_oid(oid)) {
1605 struct object *o = parse_object(cb->all_revs->repo, oid);
1606 if (o) {
1607 o->flags |= cb->all_flags;
1608 /* ??? CMDLINEFLAGS ??? */
1609 add_pending_object(cb->all_revs, o, "");
1611 else if (!cb->warned_bad_reflog) {
1612 warning("reflog of '%s' references pruned commits",
1613 cb->name_for_errormsg);
1614 cb->warned_bad_reflog = 1;
1619 static int handle_one_reflog_ent(struct object_id *ooid, struct object_id *noid,
1620 const char *email, timestamp_t timestamp, int tz,
1621 const char *message, void *cb_data)
1623 handle_one_reflog_commit(ooid, cb_data);
1624 handle_one_reflog_commit(noid, cb_data);
1625 return 0;
1628 static int handle_one_reflog(const char *refname_in_wt,
1629 const struct object_id *oid,
1630 int flag, void *cb_data)
1632 struct all_refs_cb *cb = cb_data;
1633 struct strbuf refname = STRBUF_INIT;
1635 cb->warned_bad_reflog = 0;
1636 strbuf_worktree_ref(cb->wt, &refname, refname_in_wt);
1637 cb->name_for_errormsg = refname.buf;
1638 refs_for_each_reflog_ent(get_main_ref_store(the_repository),
1639 refname.buf,
1640 handle_one_reflog_ent, cb_data);
1641 strbuf_release(&refname);
1642 return 0;
1645 static void add_other_reflogs_to_pending(struct all_refs_cb *cb)
1647 struct worktree **worktrees, **p;
1649 worktrees = get_worktrees();
1650 for (p = worktrees; *p; p++) {
1651 struct worktree *wt = *p;
1653 if (wt->is_current)
1654 continue;
1656 cb->wt = wt;
1657 refs_for_each_reflog(get_worktree_ref_store(wt),
1658 handle_one_reflog,
1659 cb);
1661 free_worktrees(worktrees);
1664 void add_reflogs_to_pending(struct rev_info *revs, unsigned flags)
1666 struct all_refs_cb cb;
1668 cb.all_revs = revs;
1669 cb.all_flags = flags;
1670 cb.wt = NULL;
1671 for_each_reflog(handle_one_reflog, &cb);
1673 if (!revs->single_worktree)
1674 add_other_reflogs_to_pending(&cb);
1677 static void add_cache_tree(struct cache_tree *it, struct rev_info *revs,
1678 struct strbuf *path, unsigned int flags)
1680 size_t baselen = path->len;
1681 int i;
1683 if (it->entry_count >= 0) {
1684 struct tree *tree = lookup_tree(revs->repo, &it->oid);
1685 tree->object.flags |= flags;
1686 add_pending_object_with_path(revs, &tree->object, "",
1687 040000, path->buf);
1690 for (i = 0; i < it->subtree_nr; i++) {
1691 struct cache_tree_sub *sub = it->down[i];
1692 strbuf_addf(path, "%s%s", baselen ? "/" : "", sub->name);
1693 add_cache_tree(sub->cache_tree, revs, path, flags);
1694 strbuf_setlen(path, baselen);
1699 static void do_add_index_objects_to_pending(struct rev_info *revs,
1700 struct index_state *istate,
1701 unsigned int flags)
1703 int i;
1705 /* TODO: audit for interaction with sparse-index. */
1706 ensure_full_index(istate);
1707 for (i = 0; i < istate->cache_nr; i++) {
1708 struct cache_entry *ce = istate->cache[i];
1709 struct blob *blob;
1711 if (S_ISGITLINK(ce->ce_mode))
1712 continue;
1714 blob = lookup_blob(revs->repo, &ce->oid);
1715 if (!blob)
1716 die("unable to add index blob to traversal");
1717 blob->object.flags |= flags;
1718 add_pending_object_with_path(revs, &blob->object, "",
1719 ce->ce_mode, ce->name);
1722 if (istate->cache_tree) {
1723 struct strbuf path = STRBUF_INIT;
1724 add_cache_tree(istate->cache_tree, revs, &path, flags);
1725 strbuf_release(&path);
1729 void add_index_objects_to_pending(struct rev_info *revs, unsigned int flags)
1731 struct worktree **worktrees, **p;
1733 repo_read_index(revs->repo);
1734 do_add_index_objects_to_pending(revs, revs->repo->index, flags);
1736 if (revs->single_worktree)
1737 return;
1739 worktrees = get_worktrees();
1740 for (p = worktrees; *p; p++) {
1741 struct worktree *wt = *p;
1742 struct index_state istate = { NULL };
1744 if (wt->is_current)
1745 continue; /* current index already taken care of */
1747 if (read_index_from(&istate,
1748 worktree_git_path(wt, "index"),
1749 get_worktree_git_dir(wt)) > 0)
1750 do_add_index_objects_to_pending(revs, &istate, flags);
1751 discard_index(&istate);
1753 free_worktrees(worktrees);
1756 struct add_alternate_refs_data {
1757 struct rev_info *revs;
1758 unsigned int flags;
1761 static void add_one_alternate_ref(const struct object_id *oid,
1762 void *vdata)
1764 const char *name = ".alternate";
1765 struct add_alternate_refs_data *data = vdata;
1766 struct object *obj;
1768 obj = get_reference(data->revs, name, oid, data->flags);
1769 add_rev_cmdline(data->revs, obj, name, REV_CMD_REV, data->flags);
1770 add_pending_object(data->revs, obj, name);
1773 static void add_alternate_refs_to_pending(struct rev_info *revs,
1774 unsigned int flags)
1776 struct add_alternate_refs_data data;
1777 data.revs = revs;
1778 data.flags = flags;
1779 for_each_alternate_ref(add_one_alternate_ref, &data);
1782 static int add_parents_only(struct rev_info *revs, const char *arg_, int flags,
1783 int exclude_parent)
1785 struct object_id oid;
1786 struct object *it;
1787 struct commit *commit;
1788 struct commit_list *parents;
1789 int parent_number;
1790 const char *arg = arg_;
1792 if (*arg == '^') {
1793 flags ^= UNINTERESTING | BOTTOM;
1794 arg++;
1796 if (get_oid_committish(arg, &oid))
1797 return 0;
1798 while (1) {
1799 it = get_reference(revs, arg, &oid, 0);
1800 if (!it && revs->ignore_missing)
1801 return 0;
1802 if (it->type != OBJ_TAG)
1803 break;
1804 if (!((struct tag*)it)->tagged)
1805 return 0;
1806 oidcpy(&oid, &((struct tag*)it)->tagged->oid);
1808 if (it->type != OBJ_COMMIT)
1809 return 0;
1810 commit = (struct commit *)it;
1811 if (exclude_parent &&
1812 exclude_parent > commit_list_count(commit->parents))
1813 return 0;
1814 for (parents = commit->parents, parent_number = 1;
1815 parents;
1816 parents = parents->next, parent_number++) {
1817 if (exclude_parent && parent_number != exclude_parent)
1818 continue;
1820 it = &parents->item->object;
1821 it->flags |= flags;
1822 add_rev_cmdline(revs, it, arg_, REV_CMD_PARENTS_ONLY, flags);
1823 add_pending_object(revs, it, arg);
1825 return 1;
1828 void repo_init_revisions(struct repository *r,
1829 struct rev_info *revs,
1830 const char *prefix)
1832 memset(revs, 0, sizeof(*revs));
1834 revs->repo = r;
1835 revs->abbrev = DEFAULT_ABBREV;
1836 revs->simplify_history = 1;
1837 revs->pruning.repo = r;
1838 revs->pruning.flags.recursive = 1;
1839 revs->pruning.flags.quick = 1;
1840 revs->pruning.add_remove = file_add_remove;
1841 revs->pruning.change = file_change;
1842 revs->pruning.change_fn_data = revs;
1843 revs->sort_order = REV_SORT_IN_GRAPH_ORDER;
1844 revs->dense = 1;
1845 revs->prefix = prefix;
1846 revs->max_age = -1;
1847 revs->max_age_as_filter = -1;
1848 revs->min_age = -1;
1849 revs->skip_count = -1;
1850 revs->max_count = -1;
1851 revs->max_parents = -1;
1852 revs->expand_tabs_in_log = -1;
1854 revs->commit_format = CMIT_FMT_DEFAULT;
1855 revs->expand_tabs_in_log_default = 8;
1857 grep_init(&revs->grep_filter, revs->repo);
1858 revs->grep_filter.status_only = 1;
1860 repo_diff_setup(revs->repo, &revs->diffopt);
1861 if (prefix && !revs->diffopt.prefix) {
1862 revs->diffopt.prefix = prefix;
1863 revs->diffopt.prefix_length = strlen(prefix);
1866 init_display_notes(&revs->notes_opt);
1869 static void add_pending_commit_list(struct rev_info *revs,
1870 struct commit_list *commit_list,
1871 unsigned int flags)
1873 while (commit_list) {
1874 struct object *object = &commit_list->item->object;
1875 object->flags |= flags;
1876 add_pending_object(revs, object, oid_to_hex(&object->oid));
1877 commit_list = commit_list->next;
1881 static void prepare_show_merge(struct rev_info *revs)
1883 struct commit_list *bases;
1884 struct commit *head, *other;
1885 struct object_id oid;
1886 const char **prune = NULL;
1887 int i, prune_num = 1; /* counting terminating NULL */
1888 struct index_state *istate = revs->repo->index;
1890 if (get_oid("HEAD", &oid))
1891 die("--merge without HEAD?");
1892 head = lookup_commit_or_die(&oid, "HEAD");
1893 if (get_oid("MERGE_HEAD", &oid))
1894 die("--merge without MERGE_HEAD?");
1895 other = lookup_commit_or_die(&oid, "MERGE_HEAD");
1896 add_pending_object(revs, &head->object, "HEAD");
1897 add_pending_object(revs, &other->object, "MERGE_HEAD");
1898 bases = get_merge_bases(head, other);
1899 add_rev_cmdline_list(revs, bases, REV_CMD_MERGE_BASE, UNINTERESTING | BOTTOM);
1900 add_pending_commit_list(revs, bases, UNINTERESTING | BOTTOM);
1901 free_commit_list(bases);
1902 head->object.flags |= SYMMETRIC_LEFT;
1904 if (!istate->cache_nr)
1905 repo_read_index(revs->repo);
1906 for (i = 0; i < istate->cache_nr; i++) {
1907 const struct cache_entry *ce = istate->cache[i];
1908 if (!ce_stage(ce))
1909 continue;
1910 if (ce_path_match(istate, ce, &revs->prune_data, NULL)) {
1911 prune_num++;
1912 REALLOC_ARRAY(prune, prune_num);
1913 prune[prune_num-2] = ce->name;
1914 prune[prune_num-1] = NULL;
1916 while ((i+1 < istate->cache_nr) &&
1917 ce_same_name(ce, istate->cache[i+1]))
1918 i++;
1920 clear_pathspec(&revs->prune_data);
1921 parse_pathspec(&revs->prune_data, PATHSPEC_ALL_MAGIC & ~PATHSPEC_LITERAL,
1922 PATHSPEC_PREFER_FULL | PATHSPEC_LITERAL_PATH, "", prune);
1923 revs->limited = 1;
1926 static int dotdot_missing(const char *arg, char *dotdot,
1927 struct rev_info *revs, int symmetric)
1929 if (revs->ignore_missing)
1930 return 0;
1931 /* de-munge so we report the full argument */
1932 *dotdot = '.';
1933 die(symmetric
1934 ? "Invalid symmetric difference expression %s"
1935 : "Invalid revision range %s", arg);
1938 static int handle_dotdot_1(const char *arg, char *dotdot,
1939 struct rev_info *revs, int flags,
1940 int cant_be_filename,
1941 struct object_context *a_oc,
1942 struct object_context *b_oc)
1944 const char *a_name, *b_name;
1945 struct object_id a_oid, b_oid;
1946 struct object *a_obj, *b_obj;
1947 unsigned int a_flags, b_flags;
1948 int symmetric = 0;
1949 unsigned int flags_exclude = flags ^ (UNINTERESTING | BOTTOM);
1950 unsigned int oc_flags = GET_OID_COMMITTISH | GET_OID_RECORD_PATH;
1952 a_name = arg;
1953 if (!*a_name)
1954 a_name = "HEAD";
1956 b_name = dotdot + 2;
1957 if (*b_name == '.') {
1958 symmetric = 1;
1959 b_name++;
1961 if (!*b_name)
1962 b_name = "HEAD";
1964 if (get_oid_with_context(revs->repo, a_name, oc_flags, &a_oid, a_oc) ||
1965 get_oid_with_context(revs->repo, b_name, oc_flags, &b_oid, b_oc))
1966 return -1;
1968 if (!cant_be_filename) {
1969 *dotdot = '.';
1970 verify_non_filename(revs->prefix, arg);
1971 *dotdot = '\0';
1974 a_obj = parse_object(revs->repo, &a_oid);
1975 b_obj = parse_object(revs->repo, &b_oid);
1976 if (!a_obj || !b_obj)
1977 return dotdot_missing(arg, dotdot, revs, symmetric);
1979 if (!symmetric) {
1980 /* just A..B */
1981 b_flags = flags;
1982 a_flags = flags_exclude;
1983 } else {
1984 /* A...B -- find merge bases between the two */
1985 struct commit *a, *b;
1986 struct commit_list *exclude;
1988 a = lookup_commit_reference(revs->repo, &a_obj->oid);
1989 b = lookup_commit_reference(revs->repo, &b_obj->oid);
1990 if (!a || !b)
1991 return dotdot_missing(arg, dotdot, revs, symmetric);
1993 exclude = get_merge_bases(a, b);
1994 add_rev_cmdline_list(revs, exclude, REV_CMD_MERGE_BASE,
1995 flags_exclude);
1996 add_pending_commit_list(revs, exclude, flags_exclude);
1997 free_commit_list(exclude);
1999 b_flags = flags;
2000 a_flags = flags | SYMMETRIC_LEFT;
2003 a_obj->flags |= a_flags;
2004 b_obj->flags |= b_flags;
2005 add_rev_cmdline(revs, a_obj, a_name, REV_CMD_LEFT, a_flags);
2006 add_rev_cmdline(revs, b_obj, b_name, REV_CMD_RIGHT, b_flags);
2007 add_pending_object_with_path(revs, a_obj, a_name, a_oc->mode, a_oc->path);
2008 add_pending_object_with_path(revs, b_obj, b_name, b_oc->mode, b_oc->path);
2009 return 0;
2012 static int handle_dotdot(const char *arg,
2013 struct rev_info *revs, int flags,
2014 int cant_be_filename)
2016 struct object_context a_oc, b_oc;
2017 char *dotdot = strstr(arg, "..");
2018 int ret;
2020 if (!dotdot)
2021 return -1;
2023 memset(&a_oc, 0, sizeof(a_oc));
2024 memset(&b_oc, 0, sizeof(b_oc));
2026 *dotdot = '\0';
2027 ret = handle_dotdot_1(arg, dotdot, revs, flags, cant_be_filename,
2028 &a_oc, &b_oc);
2029 *dotdot = '.';
2031 free(a_oc.path);
2032 free(b_oc.path);
2034 return ret;
2037 static int handle_revision_arg_1(const char *arg_, struct rev_info *revs, int flags, unsigned revarg_opt)
2039 struct object_context oc;
2040 char *mark;
2041 struct object *object;
2042 struct object_id oid;
2043 int local_flags;
2044 const char *arg = arg_;
2045 int cant_be_filename = revarg_opt & REVARG_CANNOT_BE_FILENAME;
2046 unsigned get_sha1_flags = GET_OID_RECORD_PATH;
2048 flags = flags & UNINTERESTING ? flags | BOTTOM : flags & ~BOTTOM;
2050 if (!cant_be_filename && !strcmp(arg, "..")) {
2052 * Just ".."? That is not a range but the
2053 * pathspec for the parent directory.
2055 return -1;
2058 if (!handle_dotdot(arg, revs, flags, revarg_opt))
2059 return 0;
2061 mark = strstr(arg, "^@");
2062 if (mark && !mark[2]) {
2063 *mark = 0;
2064 if (add_parents_only(revs, arg, flags, 0))
2065 return 0;
2066 *mark = '^';
2068 mark = strstr(arg, "^!");
2069 if (mark && !mark[2]) {
2070 *mark = 0;
2071 if (!add_parents_only(revs, arg, flags ^ (UNINTERESTING | BOTTOM), 0))
2072 *mark = '^';
2074 mark = strstr(arg, "^-");
2075 if (mark) {
2076 int exclude_parent = 1;
2078 if (mark[2]) {
2079 char *end;
2080 exclude_parent = strtoul(mark + 2, &end, 10);
2081 if (*end != '\0' || !exclude_parent)
2082 return -1;
2085 *mark = 0;
2086 if (!add_parents_only(revs, arg, flags ^ (UNINTERESTING | BOTTOM), exclude_parent))
2087 *mark = '^';
2090 local_flags = 0;
2091 if (*arg == '^') {
2092 local_flags = UNINTERESTING | BOTTOM;
2093 arg++;
2096 if (revarg_opt & REVARG_COMMITTISH)
2097 get_sha1_flags |= GET_OID_COMMITTISH;
2099 if (get_oid_with_context(revs->repo, arg, get_sha1_flags, &oid, &oc))
2100 return revs->ignore_missing ? 0 : -1;
2101 if (!cant_be_filename)
2102 verify_non_filename(revs->prefix, arg);
2103 object = get_reference(revs, arg, &oid, flags ^ local_flags);
2104 if (!object)
2105 return revs->ignore_missing ? 0 : -1;
2106 add_rev_cmdline(revs, object, arg_, REV_CMD_REV, flags ^ local_flags);
2107 add_pending_object_with_path(revs, object, arg, oc.mode, oc.path);
2108 free(oc.path);
2109 return 0;
2112 int handle_revision_arg(const char *arg, struct rev_info *revs, int flags, unsigned revarg_opt)
2114 int ret = handle_revision_arg_1(arg, revs, flags, revarg_opt);
2115 if (!ret)
2116 revs->rev_input_given = 1;
2117 return ret;
2120 static void read_pathspec_from_stdin(struct strbuf *sb,
2121 struct strvec *prune)
2123 while (strbuf_getline(sb, stdin) != EOF)
2124 strvec_push(prune, sb->buf);
2127 static void read_revisions_from_stdin(struct rev_info *revs,
2128 struct strvec *prune)
2130 struct strbuf sb;
2131 int seen_dashdash = 0;
2132 int save_warning;
2134 save_warning = warn_on_object_refname_ambiguity;
2135 warn_on_object_refname_ambiguity = 0;
2137 strbuf_init(&sb, 1000);
2138 while (strbuf_getline(&sb, stdin) != EOF) {
2139 int len = sb.len;
2140 if (!len)
2141 break;
2142 if (sb.buf[0] == '-') {
2143 if (len == 2 && sb.buf[1] == '-') {
2144 seen_dashdash = 1;
2145 break;
2147 die("options not supported in --stdin mode");
2149 if (handle_revision_arg(sb.buf, revs, 0,
2150 REVARG_CANNOT_BE_FILENAME))
2151 die("bad revision '%s'", sb.buf);
2153 if (seen_dashdash)
2154 read_pathspec_from_stdin(&sb, prune);
2156 strbuf_release(&sb);
2157 warn_on_object_refname_ambiguity = save_warning;
2160 static void add_grep(struct rev_info *revs, const char *ptn, enum grep_pat_token what)
2162 append_grep_pattern(&revs->grep_filter, ptn, "command line", 0, what);
2165 static void add_header_grep(struct rev_info *revs, enum grep_header_field field, const char *pattern)
2167 append_header_grep_pattern(&revs->grep_filter, field, pattern);
2170 static void add_message_grep(struct rev_info *revs, const char *pattern)
2172 add_grep(revs, pattern, GREP_PATTERN_BODY);
2175 static int handle_revision_opt(struct rev_info *revs, int argc, const char **argv,
2176 int *unkc, const char **unkv,
2177 const struct setup_revision_opt* opt)
2179 const char *arg = argv[0];
2180 const char *optarg;
2181 int argcount;
2182 const unsigned hexsz = the_hash_algo->hexsz;
2184 /* pseudo revision arguments */
2185 if (!strcmp(arg, "--all") || !strcmp(arg, "--branches") ||
2186 !strcmp(arg, "--tags") || !strcmp(arg, "--remotes") ||
2187 !strcmp(arg, "--reflog") || !strcmp(arg, "--not") ||
2188 !strcmp(arg, "--no-walk") || !strcmp(arg, "--do-walk") ||
2189 !strcmp(arg, "--bisect") || starts_with(arg, "--glob=") ||
2190 !strcmp(arg, "--indexed-objects") ||
2191 !strcmp(arg, "--alternate-refs") ||
2192 starts_with(arg, "--exclude=") ||
2193 starts_with(arg, "--branches=") || starts_with(arg, "--tags=") ||
2194 starts_with(arg, "--remotes=") || starts_with(arg, "--no-walk="))
2196 unkv[(*unkc)++] = arg;
2197 return 1;
2200 if ((argcount = parse_long_opt("max-count", argv, &optarg))) {
2201 revs->max_count = atoi(optarg);
2202 revs->no_walk = 0;
2203 return argcount;
2204 } else if ((argcount = parse_long_opt("skip", argv, &optarg))) {
2205 revs->skip_count = atoi(optarg);
2206 return argcount;
2207 } else if ((*arg == '-') && isdigit(arg[1])) {
2208 /* accept -<digit>, like traditional "head" */
2209 if (strtol_i(arg + 1, 10, &revs->max_count) < 0 ||
2210 revs->max_count < 0)
2211 die("'%s': not a non-negative integer", arg + 1);
2212 revs->no_walk = 0;
2213 } else if (!strcmp(arg, "-n")) {
2214 if (argc <= 1)
2215 return error("-n requires an argument");
2216 revs->max_count = atoi(argv[1]);
2217 revs->no_walk = 0;
2218 return 2;
2219 } else if (skip_prefix(arg, "-n", &optarg)) {
2220 revs->max_count = atoi(optarg);
2221 revs->no_walk = 0;
2222 } else if ((argcount = parse_long_opt("max-age", argv, &optarg))) {
2223 revs->max_age = atoi(optarg);
2224 return argcount;
2225 } else if ((argcount = parse_long_opt("since", argv, &optarg))) {
2226 revs->max_age = approxidate(optarg);
2227 return argcount;
2228 } else if ((argcount = parse_long_opt("since-as-filter", argv, &optarg))) {
2229 revs->max_age_as_filter = approxidate(optarg);
2230 return argcount;
2231 } else if ((argcount = parse_long_opt("after", argv, &optarg))) {
2232 revs->max_age = approxidate(optarg);
2233 return argcount;
2234 } else if ((argcount = parse_long_opt("min-age", argv, &optarg))) {
2235 revs->min_age = atoi(optarg);
2236 return argcount;
2237 } else if ((argcount = parse_long_opt("before", argv, &optarg))) {
2238 revs->min_age = approxidate(optarg);
2239 return argcount;
2240 } else if ((argcount = parse_long_opt("until", argv, &optarg))) {
2241 revs->min_age = approxidate(optarg);
2242 return argcount;
2243 } else if (!strcmp(arg, "--first-parent")) {
2244 revs->first_parent_only = 1;
2245 } else if (!strcmp(arg, "--exclude-first-parent-only")) {
2246 revs->exclude_first_parent_only = 1;
2247 } else if (!strcmp(arg, "--ancestry-path")) {
2248 revs->ancestry_path = 1;
2249 revs->simplify_history = 0;
2250 revs->limited = 1;
2251 } else if (!strcmp(arg, "-g") || !strcmp(arg, "--walk-reflogs")) {
2252 init_reflog_walk(&revs->reflog_info);
2253 } else if (!strcmp(arg, "--default")) {
2254 if (argc <= 1)
2255 return error("bad --default argument");
2256 revs->def = argv[1];
2257 return 2;
2258 } else if (!strcmp(arg, "--merge")) {
2259 revs->show_merge = 1;
2260 } else if (!strcmp(arg, "--topo-order")) {
2261 revs->sort_order = REV_SORT_IN_GRAPH_ORDER;
2262 revs->topo_order = 1;
2263 } else if (!strcmp(arg, "--simplify-merges")) {
2264 revs->simplify_merges = 1;
2265 revs->topo_order = 1;
2266 revs->rewrite_parents = 1;
2267 revs->simplify_history = 0;
2268 revs->limited = 1;
2269 } else if (!strcmp(arg, "--simplify-by-decoration")) {
2270 revs->simplify_merges = 1;
2271 revs->topo_order = 1;
2272 revs->rewrite_parents = 1;
2273 revs->simplify_history = 0;
2274 revs->simplify_by_decoration = 1;
2275 revs->limited = 1;
2276 revs->prune = 1;
2277 } else if (!strcmp(arg, "--date-order")) {
2278 revs->sort_order = REV_SORT_BY_COMMIT_DATE;
2279 revs->topo_order = 1;
2280 } else if (!strcmp(arg, "--author-date-order")) {
2281 revs->sort_order = REV_SORT_BY_AUTHOR_DATE;
2282 revs->topo_order = 1;
2283 } else if (!strcmp(arg, "--early-output")) {
2284 revs->early_output = 100;
2285 revs->topo_order = 1;
2286 } else if (skip_prefix(arg, "--early-output=", &optarg)) {
2287 if (strtoul_ui(optarg, 10, &revs->early_output) < 0)
2288 die("'%s': not a non-negative integer", optarg);
2289 revs->topo_order = 1;
2290 } else if (!strcmp(arg, "--parents")) {
2291 revs->rewrite_parents = 1;
2292 revs->print_parents = 1;
2293 } else if (!strcmp(arg, "--dense")) {
2294 revs->dense = 1;
2295 } else if (!strcmp(arg, "--sparse")) {
2296 revs->dense = 0;
2297 } else if (!strcmp(arg, "--in-commit-order")) {
2298 revs->tree_blobs_in_commit_order = 1;
2299 } else if (!strcmp(arg, "--remove-empty")) {
2300 revs->remove_empty_trees = 1;
2301 } else if (!strcmp(arg, "--merges")) {
2302 revs->min_parents = 2;
2303 } else if (!strcmp(arg, "--no-merges")) {
2304 revs->max_parents = 1;
2305 } else if (skip_prefix(arg, "--min-parents=", &optarg)) {
2306 revs->min_parents = atoi(optarg);
2307 } else if (!strcmp(arg, "--no-min-parents")) {
2308 revs->min_parents = 0;
2309 } else if (skip_prefix(arg, "--max-parents=", &optarg)) {
2310 revs->max_parents = atoi(optarg);
2311 } else if (!strcmp(arg, "--no-max-parents")) {
2312 revs->max_parents = -1;
2313 } else if (!strcmp(arg, "--boundary")) {
2314 revs->boundary = 1;
2315 } else if (!strcmp(arg, "--left-right")) {
2316 revs->left_right = 1;
2317 } else if (!strcmp(arg, "--left-only")) {
2318 if (revs->right_only)
2319 die("--left-only is incompatible with --right-only"
2320 " or --cherry");
2321 revs->left_only = 1;
2322 } else if (!strcmp(arg, "--right-only")) {
2323 if (revs->left_only)
2324 die(_("options '%s' and '%s' cannot be used together"), "--right-only", "--left-only");
2325 revs->right_only = 1;
2326 } else if (!strcmp(arg, "--cherry")) {
2327 if (revs->left_only)
2328 die(_("options '%s' and '%s' cannot be used together"), "--cherry", "--left-only");
2329 revs->cherry_mark = 1;
2330 revs->right_only = 1;
2331 revs->max_parents = 1;
2332 revs->limited = 1;
2333 } else if (!strcmp(arg, "--count")) {
2334 revs->count = 1;
2335 } else if (!strcmp(arg, "--cherry-mark")) {
2336 if (revs->cherry_pick)
2337 die(_("options '%s' and '%s' cannot be used together"), "--cherry-mark", "--cherry-pick");
2338 revs->cherry_mark = 1;
2339 revs->limited = 1; /* needs limit_list() */
2340 } else if (!strcmp(arg, "--cherry-pick")) {
2341 if (revs->cherry_mark)
2342 die(_("options '%s' and '%s' cannot be used together"), "--cherry-pick", "--cherry-mark");
2343 revs->cherry_pick = 1;
2344 revs->limited = 1;
2345 } else if (!strcmp(arg, "--objects")) {
2346 revs->tag_objects = 1;
2347 revs->tree_objects = 1;
2348 revs->blob_objects = 1;
2349 } else if (!strcmp(arg, "--objects-edge")) {
2350 revs->tag_objects = 1;
2351 revs->tree_objects = 1;
2352 revs->blob_objects = 1;
2353 revs->edge_hint = 1;
2354 } else if (!strcmp(arg, "--objects-edge-aggressive")) {
2355 revs->tag_objects = 1;
2356 revs->tree_objects = 1;
2357 revs->blob_objects = 1;
2358 revs->edge_hint = 1;
2359 revs->edge_hint_aggressive = 1;
2360 } else if (!strcmp(arg, "--verify-objects")) {
2361 revs->tag_objects = 1;
2362 revs->tree_objects = 1;
2363 revs->blob_objects = 1;
2364 revs->verify_objects = 1;
2365 } else if (!strcmp(arg, "--unpacked")) {
2366 revs->unpacked = 1;
2367 } else if (starts_with(arg, "--unpacked=")) {
2368 die(_("--unpacked=<packfile> no longer supported"));
2369 } else if (!strcmp(arg, "--no-kept-objects")) {
2370 revs->no_kept_objects = 1;
2371 revs->keep_pack_cache_flags |= IN_CORE_KEEP_PACKS;
2372 revs->keep_pack_cache_flags |= ON_DISK_KEEP_PACKS;
2373 } else if (skip_prefix(arg, "--no-kept-objects=", &optarg)) {
2374 revs->no_kept_objects = 1;
2375 if (!strcmp(optarg, "in-core"))
2376 revs->keep_pack_cache_flags |= IN_CORE_KEEP_PACKS;
2377 if (!strcmp(optarg, "on-disk"))
2378 revs->keep_pack_cache_flags |= ON_DISK_KEEP_PACKS;
2379 } else if (!strcmp(arg, "-r")) {
2380 revs->diff = 1;
2381 revs->diffopt.flags.recursive = 1;
2382 } else if (!strcmp(arg, "-t")) {
2383 revs->diff = 1;
2384 revs->diffopt.flags.recursive = 1;
2385 revs->diffopt.flags.tree_in_recursive = 1;
2386 } else if ((argcount = diff_merges_parse_opts(revs, argv))) {
2387 return argcount;
2388 } else if (!strcmp(arg, "-v")) {
2389 revs->verbose_header = 1;
2390 } else if (!strcmp(arg, "--pretty")) {
2391 revs->verbose_header = 1;
2392 revs->pretty_given = 1;
2393 get_commit_format(NULL, revs);
2394 } else if (skip_prefix(arg, "--pretty=", &optarg) ||
2395 skip_prefix(arg, "--format=", &optarg)) {
2397 * Detached form ("--pretty X" as opposed to "--pretty=X")
2398 * not allowed, since the argument is optional.
2400 revs->verbose_header = 1;
2401 revs->pretty_given = 1;
2402 get_commit_format(optarg, revs);
2403 } else if (!strcmp(arg, "--expand-tabs")) {
2404 revs->expand_tabs_in_log = 8;
2405 } else if (!strcmp(arg, "--no-expand-tabs")) {
2406 revs->expand_tabs_in_log = 0;
2407 } else if (skip_prefix(arg, "--expand-tabs=", &arg)) {
2408 int val;
2409 if (strtol_i(arg, 10, &val) < 0 || val < 0)
2410 die("'%s': not a non-negative integer", arg);
2411 revs->expand_tabs_in_log = val;
2412 } else if (!strcmp(arg, "--show-notes") || !strcmp(arg, "--notes")) {
2413 enable_default_display_notes(&revs->notes_opt, &revs->show_notes);
2414 revs->show_notes_given = 1;
2415 } else if (!strcmp(arg, "--show-signature")) {
2416 revs->show_signature = 1;
2417 } else if (!strcmp(arg, "--no-show-signature")) {
2418 revs->show_signature = 0;
2419 } else if (!strcmp(arg, "--show-linear-break")) {
2420 revs->break_bar = " ..........";
2421 revs->track_linear = 1;
2422 revs->track_first_time = 1;
2423 } else if (skip_prefix(arg, "--show-linear-break=", &optarg)) {
2424 revs->break_bar = xstrdup(optarg);
2425 revs->track_linear = 1;
2426 revs->track_first_time = 1;
2427 } else if (skip_prefix(arg, "--show-notes=", &optarg) ||
2428 skip_prefix(arg, "--notes=", &optarg)) {
2429 if (starts_with(arg, "--show-notes=") &&
2430 revs->notes_opt.use_default_notes < 0)
2431 revs->notes_opt.use_default_notes = 1;
2432 enable_ref_display_notes(&revs->notes_opt, &revs->show_notes, optarg);
2433 revs->show_notes_given = 1;
2434 } else if (!strcmp(arg, "--no-notes")) {
2435 disable_display_notes(&revs->notes_opt, &revs->show_notes);
2436 revs->show_notes_given = 1;
2437 } else if (!strcmp(arg, "--standard-notes")) {
2438 revs->show_notes_given = 1;
2439 revs->notes_opt.use_default_notes = 1;
2440 } else if (!strcmp(arg, "--no-standard-notes")) {
2441 revs->notes_opt.use_default_notes = 0;
2442 } else if (!strcmp(arg, "--oneline")) {
2443 revs->verbose_header = 1;
2444 get_commit_format("oneline", revs);
2445 revs->pretty_given = 1;
2446 revs->abbrev_commit = 1;
2447 } else if (!strcmp(arg, "--graph")) {
2448 graph_clear(revs->graph);
2449 revs->graph = graph_init(revs);
2450 } else if (!strcmp(arg, "--no-graph")) {
2451 graph_clear(revs->graph);
2452 revs->graph = NULL;
2453 } else if (!strcmp(arg, "--encode-email-headers")) {
2454 revs->encode_email_headers = 1;
2455 } else if (!strcmp(arg, "--no-encode-email-headers")) {
2456 revs->encode_email_headers = 0;
2457 } else if (!strcmp(arg, "--root")) {
2458 revs->show_root_diff = 1;
2459 } else if (!strcmp(arg, "--no-commit-id")) {
2460 revs->no_commit_id = 1;
2461 } else if (!strcmp(arg, "--always")) {
2462 revs->always_show_header = 1;
2463 } else if (!strcmp(arg, "--no-abbrev")) {
2464 revs->abbrev = 0;
2465 } else if (!strcmp(arg, "--abbrev")) {
2466 revs->abbrev = DEFAULT_ABBREV;
2467 } else if (skip_prefix(arg, "--abbrev=", &optarg)) {
2468 revs->abbrev = strtoul(optarg, NULL, 10);
2469 if (revs->abbrev < MINIMUM_ABBREV)
2470 revs->abbrev = MINIMUM_ABBREV;
2471 else if (revs->abbrev > hexsz)
2472 revs->abbrev = hexsz;
2473 } else if (!strcmp(arg, "--abbrev-commit")) {
2474 revs->abbrev_commit = 1;
2475 revs->abbrev_commit_given = 1;
2476 } else if (!strcmp(arg, "--no-abbrev-commit")) {
2477 revs->abbrev_commit = 0;
2478 } else if (!strcmp(arg, "--full-diff")) {
2479 revs->diff = 1;
2480 revs->full_diff = 1;
2481 } else if (!strcmp(arg, "--show-pulls")) {
2482 revs->show_pulls = 1;
2483 } else if (!strcmp(arg, "--full-history")) {
2484 revs->simplify_history = 0;
2485 } else if (!strcmp(arg, "--relative-date")) {
2486 revs->date_mode.type = DATE_RELATIVE;
2487 revs->date_mode_explicit = 1;
2488 } else if ((argcount = parse_long_opt("date", argv, &optarg))) {
2489 parse_date_format(optarg, &revs->date_mode);
2490 revs->date_mode_explicit = 1;
2491 return argcount;
2492 } else if (!strcmp(arg, "--log-size")) {
2493 revs->show_log_size = 1;
2496 * Grepping the commit log
2498 else if ((argcount = parse_long_opt("author", argv, &optarg))) {
2499 add_header_grep(revs, GREP_HEADER_AUTHOR, optarg);
2500 return argcount;
2501 } else if ((argcount = parse_long_opt("committer", argv, &optarg))) {
2502 add_header_grep(revs, GREP_HEADER_COMMITTER, optarg);
2503 return argcount;
2504 } else if ((argcount = parse_long_opt("grep-reflog", argv, &optarg))) {
2505 add_header_grep(revs, GREP_HEADER_REFLOG, optarg);
2506 return argcount;
2507 } else if ((argcount = parse_long_opt("grep", argv, &optarg))) {
2508 add_message_grep(revs, optarg);
2509 return argcount;
2510 } else if (!strcmp(arg, "--basic-regexp")) {
2511 revs->grep_filter.pattern_type_option = GREP_PATTERN_TYPE_BRE;
2512 } else if (!strcmp(arg, "--extended-regexp") || !strcmp(arg, "-E")) {
2513 revs->grep_filter.pattern_type_option = GREP_PATTERN_TYPE_ERE;
2514 } else if (!strcmp(arg, "--regexp-ignore-case") || !strcmp(arg, "-i")) {
2515 revs->grep_filter.ignore_case = 1;
2516 revs->diffopt.pickaxe_opts |= DIFF_PICKAXE_IGNORE_CASE;
2517 } else if (!strcmp(arg, "--fixed-strings") || !strcmp(arg, "-F")) {
2518 revs->grep_filter.pattern_type_option = GREP_PATTERN_TYPE_FIXED;
2519 } else if (!strcmp(arg, "--perl-regexp") || !strcmp(arg, "-P")) {
2520 revs->grep_filter.pattern_type_option = GREP_PATTERN_TYPE_PCRE;
2521 } else if (!strcmp(arg, "--all-match")) {
2522 revs->grep_filter.all_match = 1;
2523 } else if (!strcmp(arg, "--invert-grep")) {
2524 revs->grep_filter.no_body_match = 1;
2525 } else if ((argcount = parse_long_opt("encoding", argv, &optarg))) {
2526 if (strcmp(optarg, "none"))
2527 git_log_output_encoding = xstrdup(optarg);
2528 else
2529 git_log_output_encoding = "";
2530 return argcount;
2531 } else if (!strcmp(arg, "--reverse")) {
2532 revs->reverse ^= 1;
2533 } else if (!strcmp(arg, "--children")) {
2534 revs->children.name = "children";
2535 revs->limited = 1;
2536 } else if (!strcmp(arg, "--ignore-missing")) {
2537 revs->ignore_missing = 1;
2538 } else if (opt && opt->allow_exclude_promisor_objects &&
2539 !strcmp(arg, "--exclude-promisor-objects")) {
2540 if (fetch_if_missing)
2541 BUG("exclude_promisor_objects can only be used when fetch_if_missing is 0");
2542 revs->exclude_promisor_objects = 1;
2543 } else {
2544 int opts = diff_opt_parse(&revs->diffopt, argv, argc, revs->prefix);
2545 if (!opts)
2546 unkv[(*unkc)++] = arg;
2547 return opts;
2550 return 1;
2553 void parse_revision_opt(struct rev_info *revs, struct parse_opt_ctx_t *ctx,
2554 const struct option *options,
2555 const char * const usagestr[])
2557 int n = handle_revision_opt(revs, ctx->argc, ctx->argv,
2558 &ctx->cpidx, ctx->out, NULL);
2559 if (n <= 0) {
2560 error("unknown option `%s'", ctx->argv[0]);
2561 usage_with_options(usagestr, options);
2563 ctx->argv += n;
2564 ctx->argc -= n;
2567 void revision_opts_finish(struct rev_info *revs)
2569 if (revs->graph && revs->track_linear)
2570 die(_("options '%s' and '%s' cannot be used together"), "--show-linear-break", "--graph");
2572 if (revs->graph) {
2573 revs->topo_order = 1;
2574 revs->rewrite_parents = 1;
2578 static int for_each_bisect_ref(struct ref_store *refs, each_ref_fn fn,
2579 void *cb_data, const char *term)
2581 struct strbuf bisect_refs = STRBUF_INIT;
2582 int status;
2583 strbuf_addf(&bisect_refs, "refs/bisect/%s", term);
2584 status = refs_for_each_fullref_in(refs, bisect_refs.buf, fn, cb_data);
2585 strbuf_release(&bisect_refs);
2586 return status;
2589 static int for_each_bad_bisect_ref(struct ref_store *refs, each_ref_fn fn, void *cb_data)
2591 return for_each_bisect_ref(refs, fn, cb_data, term_bad);
2594 static int for_each_good_bisect_ref(struct ref_store *refs, each_ref_fn fn, void *cb_data)
2596 return for_each_bisect_ref(refs, fn, cb_data, term_good);
2599 static int handle_revision_pseudo_opt(struct rev_info *revs,
2600 const char **argv, int *flags)
2602 const char *arg = argv[0];
2603 const char *optarg;
2604 struct ref_store *refs;
2605 int argcount;
2607 if (revs->repo != the_repository) {
2609 * We need some something like get_submodule_worktrees()
2610 * before we can go through all worktrees of a submodule,
2611 * .e.g with adding all HEADs from --all, which is not
2612 * supported right now, so stick to single worktree.
2614 if (!revs->single_worktree)
2615 BUG("--single-worktree cannot be used together with submodule");
2617 refs = get_main_ref_store(revs->repo);
2620 * NOTE!
2622 * Commands like "git shortlog" will not accept the options below
2623 * unless parse_revision_opt queues them (as opposed to erroring
2624 * out).
2626 * When implementing your new pseudo-option, remember to
2627 * register it in the list at the top of handle_revision_opt.
2629 if (!strcmp(arg, "--all")) {
2630 handle_refs(refs, revs, *flags, refs_for_each_ref);
2631 handle_refs(refs, revs, *flags, refs_head_ref);
2632 if (!revs->single_worktree) {
2633 struct all_refs_cb cb;
2635 init_all_refs_cb(&cb, revs, *flags);
2636 other_head_refs(handle_one_ref, &cb);
2638 clear_ref_exclusion(&revs->ref_excludes);
2639 } else if (!strcmp(arg, "--branches")) {
2640 handle_refs(refs, revs, *flags, refs_for_each_branch_ref);
2641 clear_ref_exclusion(&revs->ref_excludes);
2642 } else if (!strcmp(arg, "--bisect")) {
2643 read_bisect_terms(&term_bad, &term_good);
2644 handle_refs(refs, revs, *flags, for_each_bad_bisect_ref);
2645 handle_refs(refs, revs, *flags ^ (UNINTERESTING | BOTTOM),
2646 for_each_good_bisect_ref);
2647 revs->bisect = 1;
2648 } else if (!strcmp(arg, "--tags")) {
2649 handle_refs(refs, revs, *flags, refs_for_each_tag_ref);
2650 clear_ref_exclusion(&revs->ref_excludes);
2651 } else if (!strcmp(arg, "--remotes")) {
2652 handle_refs(refs, revs, *flags, refs_for_each_remote_ref);
2653 clear_ref_exclusion(&revs->ref_excludes);
2654 } else if ((argcount = parse_long_opt("glob", argv, &optarg))) {
2655 struct all_refs_cb cb;
2656 init_all_refs_cb(&cb, revs, *flags);
2657 for_each_glob_ref(handle_one_ref, optarg, &cb);
2658 clear_ref_exclusion(&revs->ref_excludes);
2659 return argcount;
2660 } else if ((argcount = parse_long_opt("exclude", argv, &optarg))) {
2661 add_ref_exclusion(&revs->ref_excludes, optarg);
2662 return argcount;
2663 } else if (skip_prefix(arg, "--branches=", &optarg)) {
2664 struct all_refs_cb cb;
2665 init_all_refs_cb(&cb, revs, *flags);
2666 for_each_glob_ref_in(handle_one_ref, optarg, "refs/heads/", &cb);
2667 clear_ref_exclusion(&revs->ref_excludes);
2668 } else if (skip_prefix(arg, "--tags=", &optarg)) {
2669 struct all_refs_cb cb;
2670 init_all_refs_cb(&cb, revs, *flags);
2671 for_each_glob_ref_in(handle_one_ref, optarg, "refs/tags/", &cb);
2672 clear_ref_exclusion(&revs->ref_excludes);
2673 } else if (skip_prefix(arg, "--remotes=", &optarg)) {
2674 struct all_refs_cb cb;
2675 init_all_refs_cb(&cb, revs, *flags);
2676 for_each_glob_ref_in(handle_one_ref, optarg, "refs/remotes/", &cb);
2677 clear_ref_exclusion(&revs->ref_excludes);
2678 } else if (!strcmp(arg, "--reflog")) {
2679 add_reflogs_to_pending(revs, *flags);
2680 } else if (!strcmp(arg, "--indexed-objects")) {
2681 add_index_objects_to_pending(revs, *flags);
2682 } else if (!strcmp(arg, "--alternate-refs")) {
2683 add_alternate_refs_to_pending(revs, *flags);
2684 } else if (!strcmp(arg, "--not")) {
2685 *flags ^= UNINTERESTING | BOTTOM;
2686 } else if (!strcmp(arg, "--no-walk")) {
2687 revs->no_walk = 1;
2688 } else if (skip_prefix(arg, "--no-walk=", &optarg)) {
2690 * Detached form ("--no-walk X" as opposed to "--no-walk=X")
2691 * not allowed, since the argument is optional.
2693 revs->no_walk = 1;
2694 if (!strcmp(optarg, "sorted"))
2695 revs->unsorted_input = 0;
2696 else if (!strcmp(optarg, "unsorted"))
2697 revs->unsorted_input = 1;
2698 else
2699 return error("invalid argument to --no-walk");
2700 } else if (!strcmp(arg, "--do-walk")) {
2701 revs->no_walk = 0;
2702 } else if (!strcmp(arg, "--single-worktree")) {
2703 revs->single_worktree = 1;
2704 } else if (skip_prefix(arg, ("--filter="), &arg)) {
2705 parse_list_objects_filter(&revs->filter, arg);
2706 } else if (!strcmp(arg, ("--no-filter"))) {
2707 list_objects_filter_set_no_filter(&revs->filter);
2708 } else {
2709 return 0;
2712 return 1;
2715 static void NORETURN diagnose_missing_default(const char *def)
2717 int flags;
2718 const char *refname;
2720 refname = resolve_ref_unsafe(def, 0, NULL, &flags);
2721 if (!refname || !(flags & REF_ISSYMREF) || (flags & REF_ISBROKEN))
2722 die(_("your current branch appears to be broken"));
2724 skip_prefix(refname, "refs/heads/", &refname);
2725 die(_("your current branch '%s' does not have any commits yet"),
2726 refname);
2730 * Parse revision information, filling in the "rev_info" structure,
2731 * and removing the used arguments from the argument list.
2733 * Returns the number of arguments left that weren't recognized
2734 * (which are also moved to the head of the argument list)
2736 int setup_revisions(int argc, const char **argv, struct rev_info *revs, struct setup_revision_opt *opt)
2738 int i, flags, left, seen_dashdash, revarg_opt;
2739 struct strvec prune_data = STRVEC_INIT;
2740 int seen_end_of_options = 0;
2742 /* First, search for "--" */
2743 if (opt && opt->assume_dashdash) {
2744 seen_dashdash = 1;
2745 } else {
2746 seen_dashdash = 0;
2747 for (i = 1; i < argc; i++) {
2748 const char *arg = argv[i];
2749 if (strcmp(arg, "--"))
2750 continue;
2751 argv[i] = NULL;
2752 argc = i;
2753 if (argv[i + 1])
2754 strvec_pushv(&prune_data, argv + i + 1);
2755 seen_dashdash = 1;
2756 break;
2760 /* Second, deal with arguments and options */
2761 flags = 0;
2762 revarg_opt = opt ? opt->revarg_opt : 0;
2763 if (seen_dashdash)
2764 revarg_opt |= REVARG_CANNOT_BE_FILENAME;
2765 for (left = i = 1; i < argc; i++) {
2766 const char *arg = argv[i];
2767 if (!seen_end_of_options && *arg == '-') {
2768 int opts;
2770 opts = handle_revision_pseudo_opt(
2771 revs, argv + i,
2772 &flags);
2773 if (opts > 0) {
2774 i += opts - 1;
2775 continue;
2778 if (!strcmp(arg, "--stdin")) {
2779 if (revs->disable_stdin) {
2780 argv[left++] = arg;
2781 continue;
2783 if (revs->read_from_stdin++)
2784 die("--stdin given twice?");
2785 read_revisions_from_stdin(revs, &prune_data);
2786 continue;
2789 if (!strcmp(arg, "--end-of-options")) {
2790 seen_end_of_options = 1;
2791 continue;
2794 opts = handle_revision_opt(revs, argc - i, argv + i,
2795 &left, argv, opt);
2796 if (opts > 0) {
2797 i += opts - 1;
2798 continue;
2800 if (opts < 0)
2801 exit(128);
2802 continue;
2806 if (handle_revision_arg(arg, revs, flags, revarg_opt)) {
2807 int j;
2808 if (seen_dashdash || *arg == '^')
2809 die("bad revision '%s'", arg);
2811 /* If we didn't have a "--":
2812 * (1) all filenames must exist;
2813 * (2) all rev-args must not be interpretable
2814 * as a valid filename.
2815 * but the latter we have checked in the main loop.
2817 for (j = i; j < argc; j++)
2818 verify_filename(revs->prefix, argv[j], j == i);
2820 strvec_pushv(&prune_data, argv + i);
2821 break;
2824 revision_opts_finish(revs);
2826 if (prune_data.nr) {
2828 * If we need to introduce the magic "a lone ':' means no
2829 * pathspec whatsoever", here is the place to do so.
2831 * if (prune_data.nr == 1 && !strcmp(prune_data[0], ":")) {
2832 * prune_data.nr = 0;
2833 * prune_data.alloc = 0;
2834 * free(prune_data.path);
2835 * prune_data.path = NULL;
2836 * } else {
2837 * terminate prune_data.alloc with NULL and
2838 * call init_pathspec() to set revs->prune_data here.
2841 parse_pathspec(&revs->prune_data, 0, 0,
2842 revs->prefix, prune_data.v);
2844 strvec_clear(&prune_data);
2846 if (!revs->def)
2847 revs->def = opt ? opt->def : NULL;
2848 if (opt && opt->tweak)
2849 opt->tweak(revs, opt);
2850 if (revs->show_merge)
2851 prepare_show_merge(revs);
2852 if (revs->def && !revs->pending.nr && !revs->rev_input_given) {
2853 struct object_id oid;
2854 struct object *object;
2855 struct object_context oc;
2856 if (get_oid_with_context(revs->repo, revs->def, 0, &oid, &oc))
2857 diagnose_missing_default(revs->def);
2858 object = get_reference(revs, revs->def, &oid, 0);
2859 add_pending_object_with_mode(revs, object, revs->def, oc.mode);
2862 /* Did the user ask for any diff output? Run the diff! */
2863 if (revs->diffopt.output_format & ~DIFF_FORMAT_NO_OUTPUT)
2864 revs->diff = 1;
2866 /* Pickaxe, diff-filter and rename following need diffs */
2867 if ((revs->diffopt.pickaxe_opts & DIFF_PICKAXE_KINDS_MASK) ||
2868 revs->diffopt.filter ||
2869 revs->diffopt.flags.follow_renames)
2870 revs->diff = 1;
2872 if (revs->diffopt.objfind)
2873 revs->simplify_history = 0;
2875 if (revs->line_level_traverse) {
2876 if (want_ancestry(revs))
2877 revs->limited = 1;
2878 revs->topo_order = 1;
2881 if (revs->topo_order && !generation_numbers_enabled(the_repository))
2882 revs->limited = 1;
2884 if (revs->prune_data.nr) {
2885 copy_pathspec(&revs->pruning.pathspec, &revs->prune_data);
2886 /* Can't prune commits with rename following: the paths change.. */
2887 if (!revs->diffopt.flags.follow_renames)
2888 revs->prune = 1;
2889 if (!revs->full_diff)
2890 copy_pathspec(&revs->diffopt.pathspec,
2891 &revs->prune_data);
2894 diff_merges_setup_revs(revs);
2896 revs->diffopt.abbrev = revs->abbrev;
2898 diff_setup_done(&revs->diffopt);
2900 if (!is_encoding_utf8(get_log_output_encoding()))
2901 revs->grep_filter.ignore_locale = 1;
2902 compile_grep_patterns(&revs->grep_filter);
2904 if (revs->reverse && revs->reflog_info)
2905 die(_("options '%s' and '%s' cannot be used together"), "--reverse", "--walk-reflogs");
2906 if (revs->reflog_info && revs->limited)
2907 die("cannot combine --walk-reflogs with history-limiting options");
2908 if (revs->rewrite_parents && revs->children.name)
2909 die(_("options '%s' and '%s' cannot be used together"), "--parents", "--children");
2910 if (revs->filter.choice && !revs->blob_objects)
2911 die(_("object filtering requires --objects"));
2914 * Limitations on the graph functionality
2916 if (revs->reverse && revs->graph)
2917 die(_("options '%s' and '%s' cannot be used together"), "--reverse", "--graph");
2919 if (revs->reflog_info && revs->graph)
2920 die(_("options '%s' and '%s' cannot be used together"), "--walk-reflogs", "--graph");
2921 if (revs->no_walk && revs->graph)
2922 die(_("options '%s' and '%s' cannot be used together"), "--no-walk", "--graph");
2923 if (!revs->reflog_info && revs->grep_filter.use_reflog_filter)
2924 die(_("the option '%s' requires '%s'"), "--grep-reflog", "--walk-reflogs");
2926 if (revs->line_level_traverse &&
2927 (revs->diffopt.output_format & ~(DIFF_FORMAT_PATCH | DIFF_FORMAT_NO_OUTPUT)))
2928 die(_("-L does not yet support diff formats besides -p and -s"));
2930 if (revs->expand_tabs_in_log < 0)
2931 revs->expand_tabs_in_log = revs->expand_tabs_in_log_default;
2933 return left;
2936 static void release_revisions_cmdline(struct rev_cmdline_info *cmdline)
2938 unsigned int i;
2940 for (i = 0; i < cmdline->nr; i++)
2941 free((char *)cmdline->rev[i].name);
2942 free(cmdline->rev);
2945 static void release_revisions_mailmap(struct string_list *mailmap)
2947 if (!mailmap)
2948 return;
2949 clear_mailmap(mailmap);
2950 free(mailmap);
2953 static void release_revisions_topo_walk_info(struct topo_walk_info *info);
2955 void release_revisions(struct rev_info *revs)
2957 free_commit_list(revs->commits);
2958 object_array_clear(&revs->pending);
2959 object_array_clear(&revs->boundary_commits);
2960 release_revisions_cmdline(&revs->cmdline);
2961 list_objects_filter_release(&revs->filter);
2962 clear_pathspec(&revs->prune_data);
2963 date_mode_release(&revs->date_mode);
2964 release_revisions_mailmap(revs->mailmap);
2965 free_grep_patterns(&revs->grep_filter);
2966 /* TODO (need to handle "no_free"): diff_free(&revs->diffopt) */
2967 diff_free(&revs->pruning);
2968 reflog_walk_info_release(revs->reflog_info);
2969 release_revisions_topo_walk_info(revs->topo_walk_info);
2972 static void add_child(struct rev_info *revs, struct commit *parent, struct commit *child)
2974 struct commit_list *l = xcalloc(1, sizeof(*l));
2976 l->item = child;
2977 l->next = add_decoration(&revs->children, &parent->object, l);
2980 static int remove_duplicate_parents(struct rev_info *revs, struct commit *commit)
2982 struct treesame_state *ts = lookup_decoration(&revs->treesame, &commit->object);
2983 struct commit_list **pp, *p;
2984 int surviving_parents;
2986 /* Examine existing parents while marking ones we have seen... */
2987 pp = &commit->parents;
2988 surviving_parents = 0;
2989 while ((p = *pp) != NULL) {
2990 struct commit *parent = p->item;
2991 if (parent->object.flags & TMP_MARK) {
2992 *pp = p->next;
2993 if (ts)
2994 compact_treesame(revs, commit, surviving_parents);
2995 continue;
2997 parent->object.flags |= TMP_MARK;
2998 surviving_parents++;
2999 pp = &p->next;
3001 /* clear the temporary mark */
3002 for (p = commit->parents; p; p = p->next) {
3003 p->item->object.flags &= ~TMP_MARK;
3005 /* no update_treesame() - removing duplicates can't affect TREESAME */
3006 return surviving_parents;
3009 struct merge_simplify_state {
3010 struct commit *simplified;
3013 static struct merge_simplify_state *locate_simplify_state(struct rev_info *revs, struct commit *commit)
3015 struct merge_simplify_state *st;
3017 st = lookup_decoration(&revs->merge_simplification, &commit->object);
3018 if (!st) {
3019 CALLOC_ARRAY(st, 1);
3020 add_decoration(&revs->merge_simplification, &commit->object, st);
3022 return st;
3025 static int mark_redundant_parents(struct commit *commit)
3027 struct commit_list *h = reduce_heads(commit->parents);
3028 int i = 0, marked = 0;
3029 struct commit_list *po, *pn;
3031 /* Want these for sanity-checking only */
3032 int orig_cnt = commit_list_count(commit->parents);
3033 int cnt = commit_list_count(h);
3036 * Not ready to remove items yet, just mark them for now, based
3037 * on the output of reduce_heads(). reduce_heads outputs the reduced
3038 * set in its original order, so this isn't too hard.
3040 po = commit->parents;
3041 pn = h;
3042 while (po) {
3043 if (pn && po->item == pn->item) {
3044 pn = pn->next;
3045 i++;
3046 } else {
3047 po->item->object.flags |= TMP_MARK;
3048 marked++;
3050 po=po->next;
3053 if (i != cnt || cnt+marked != orig_cnt)
3054 die("mark_redundant_parents %d %d %d %d", orig_cnt, cnt, i, marked);
3056 free_commit_list(h);
3058 return marked;
3061 static int mark_treesame_root_parents(struct commit *commit)
3063 struct commit_list *p;
3064 int marked = 0;
3066 for (p = commit->parents; p; p = p->next) {
3067 struct commit *parent = p->item;
3068 if (!parent->parents && (parent->object.flags & TREESAME)) {
3069 parent->object.flags |= TMP_MARK;
3070 marked++;
3074 return marked;
3078 * Awkward naming - this means one parent we are TREESAME to.
3079 * cf mark_treesame_root_parents: root parents that are TREESAME (to an
3080 * empty tree). Better name suggestions?
3082 static int leave_one_treesame_to_parent(struct rev_info *revs, struct commit *commit)
3084 struct treesame_state *ts = lookup_decoration(&revs->treesame, &commit->object);
3085 struct commit *unmarked = NULL, *marked = NULL;
3086 struct commit_list *p;
3087 unsigned n;
3089 for (p = commit->parents, n = 0; p; p = p->next, n++) {
3090 if (ts->treesame[n]) {
3091 if (p->item->object.flags & TMP_MARK) {
3092 if (!marked)
3093 marked = p->item;
3094 } else {
3095 if (!unmarked) {
3096 unmarked = p->item;
3097 break;
3104 * If we are TREESAME to a marked-for-deletion parent, but not to any
3105 * unmarked parents, unmark the first TREESAME parent. This is the
3106 * parent that the default simplify_history==1 scan would have followed,
3107 * and it doesn't make sense to omit that path when asking for a
3108 * simplified full history. Retaining it improves the chances of
3109 * understanding odd missed merges that took an old version of a file.
3111 * Example:
3113 * I--------*X A modified the file, but mainline merge X used
3114 * \ / "-s ours", so took the version from I. X is
3115 * `-*A--' TREESAME to I and !TREESAME to A.
3117 * Default log from X would produce "I". Without this check,
3118 * --full-history --simplify-merges would produce "I-A-X", showing
3119 * the merge commit X and that it changed A, but not making clear that
3120 * it had just taken the I version. With this check, the topology above
3121 * is retained.
3123 * Note that it is possible that the simplification chooses a different
3124 * TREESAME parent from the default, in which case this test doesn't
3125 * activate, and we _do_ drop the default parent. Example:
3127 * I------X A modified the file, but it was reverted in B,
3128 * \ / meaning mainline merge X is TREESAME to both
3129 * *A-*B parents.
3131 * Default log would produce "I" by following the first parent;
3132 * --full-history --simplify-merges will produce "I-A-B". But this is a
3133 * reasonable result - it presents a logical full history leading from
3134 * I to X, and X is not an important merge.
3136 if (!unmarked && marked) {
3137 marked->object.flags &= ~TMP_MARK;
3138 return 1;
3141 return 0;
3144 static int remove_marked_parents(struct rev_info *revs, struct commit *commit)
3146 struct commit_list **pp, *p;
3147 int nth_parent, removed = 0;
3149 pp = &commit->parents;
3150 nth_parent = 0;
3151 while ((p = *pp) != NULL) {
3152 struct commit *parent = p->item;
3153 if (parent->object.flags & TMP_MARK) {
3154 parent->object.flags &= ~TMP_MARK;
3155 *pp = p->next;
3156 free(p);
3157 removed++;
3158 compact_treesame(revs, commit, nth_parent);
3159 continue;
3161 pp = &p->next;
3162 nth_parent++;
3165 /* Removing parents can only increase TREESAMEness */
3166 if (removed && !(commit->object.flags & TREESAME))
3167 update_treesame(revs, commit);
3169 return nth_parent;
3172 static struct commit_list **simplify_one(struct rev_info *revs, struct commit *commit, struct commit_list **tail)
3174 struct commit_list *p;
3175 struct commit *parent;
3176 struct merge_simplify_state *st, *pst;
3177 int cnt;
3179 st = locate_simplify_state(revs, commit);
3182 * Have we handled this one?
3184 if (st->simplified)
3185 return tail;
3188 * An UNINTERESTING commit simplifies to itself, so does a
3189 * root commit. We do not rewrite parents of such commit
3190 * anyway.
3192 if ((commit->object.flags & UNINTERESTING) || !commit->parents) {
3193 st->simplified = commit;
3194 return tail;
3198 * Do we know what commit all of our parents that matter
3199 * should be rewritten to? Otherwise we are not ready to
3200 * rewrite this one yet.
3202 for (cnt = 0, p = commit->parents; p; p = p->next) {
3203 pst = locate_simplify_state(revs, p->item);
3204 if (!pst->simplified) {
3205 tail = &commit_list_insert(p->item, tail)->next;
3206 cnt++;
3208 if (revs->first_parent_only)
3209 break;
3211 if (cnt) {
3212 tail = &commit_list_insert(commit, tail)->next;
3213 return tail;
3217 * Rewrite our list of parents. Note that this cannot
3218 * affect our TREESAME flags in any way - a commit is
3219 * always TREESAME to its simplification.
3221 for (p = commit->parents; p; p = p->next) {
3222 pst = locate_simplify_state(revs, p->item);
3223 p->item = pst->simplified;
3224 if (revs->first_parent_only)
3225 break;
3228 if (revs->first_parent_only)
3229 cnt = 1;
3230 else
3231 cnt = remove_duplicate_parents(revs, commit);
3234 * It is possible that we are a merge and one side branch
3235 * does not have any commit that touches the given paths;
3236 * in such a case, the immediate parent from that branch
3237 * will be rewritten to be the merge base.
3239 * o----X X: the commit we are looking at;
3240 * / / o: a commit that touches the paths;
3241 * ---o----'
3243 * Further, a merge of an independent branch that doesn't
3244 * touch the path will reduce to a treesame root parent:
3246 * ----o----X X: the commit we are looking at;
3247 * / o: a commit that touches the paths;
3248 * r r: a root commit not touching the paths
3250 * Detect and simplify both cases.
3252 if (1 < cnt) {
3253 int marked = mark_redundant_parents(commit);
3254 marked += mark_treesame_root_parents(commit);
3255 if (marked)
3256 marked -= leave_one_treesame_to_parent(revs, commit);
3257 if (marked)
3258 cnt = remove_marked_parents(revs, commit);
3262 * A commit simplifies to itself if it is a root, if it is
3263 * UNINTERESTING, if it touches the given paths, or if it is a
3264 * merge and its parents don't simplify to one relevant commit
3265 * (the first two cases are already handled at the beginning of
3266 * this function).
3268 * Otherwise, it simplifies to what its sole relevant parent
3269 * simplifies to.
3271 if (!cnt ||
3272 (commit->object.flags & UNINTERESTING) ||
3273 !(commit->object.flags & TREESAME) ||
3274 (parent = one_relevant_parent(revs, commit->parents)) == NULL ||
3275 (revs->show_pulls && (commit->object.flags & PULL_MERGE)))
3276 st->simplified = commit;
3277 else {
3278 pst = locate_simplify_state(revs, parent);
3279 st->simplified = pst->simplified;
3281 return tail;
3284 static void simplify_merges(struct rev_info *revs)
3286 struct commit_list *list, *next;
3287 struct commit_list *yet_to_do, **tail;
3288 struct commit *commit;
3290 if (!revs->prune)
3291 return;
3293 /* feed the list reversed */
3294 yet_to_do = NULL;
3295 for (list = revs->commits; list; list = next) {
3296 commit = list->item;
3297 next = list->next;
3299 * Do not free(list) here yet; the original list
3300 * is used later in this function.
3302 commit_list_insert(commit, &yet_to_do);
3304 while (yet_to_do) {
3305 list = yet_to_do;
3306 yet_to_do = NULL;
3307 tail = &yet_to_do;
3308 while (list) {
3309 commit = pop_commit(&list);
3310 tail = simplify_one(revs, commit, tail);
3314 /* clean up the result, removing the simplified ones */
3315 list = revs->commits;
3316 revs->commits = NULL;
3317 tail = &revs->commits;
3318 while (list) {
3319 struct merge_simplify_state *st;
3321 commit = pop_commit(&list);
3322 st = locate_simplify_state(revs, commit);
3323 if (st->simplified == commit)
3324 tail = &commit_list_insert(commit, tail)->next;
3328 static void set_children(struct rev_info *revs)
3330 struct commit_list *l;
3331 for (l = revs->commits; l; l = l->next) {
3332 struct commit *commit = l->item;
3333 struct commit_list *p;
3335 for (p = commit->parents; p; p = p->next)
3336 add_child(revs, p->item, commit);
3340 void reset_revision_walk(void)
3342 clear_object_flags(SEEN | ADDED | SHOWN | TOPO_WALK_EXPLORED | TOPO_WALK_INDEGREE);
3345 static int mark_uninteresting(const struct object_id *oid,
3346 struct packed_git *pack,
3347 uint32_t pos,
3348 void *cb)
3350 struct rev_info *revs = cb;
3351 struct object *o = lookup_unknown_object(revs->repo, oid);
3352 o->flags |= UNINTERESTING | SEEN;
3353 return 0;
3356 define_commit_slab(indegree_slab, int);
3357 define_commit_slab(author_date_slab, timestamp_t);
3359 struct topo_walk_info {
3360 timestamp_t min_generation;
3361 struct prio_queue explore_queue;
3362 struct prio_queue indegree_queue;
3363 struct prio_queue topo_queue;
3364 struct indegree_slab indegree;
3365 struct author_date_slab author_date;
3368 static int topo_walk_atexit_registered;
3369 static unsigned int count_explore_walked;
3370 static unsigned int count_indegree_walked;
3371 static unsigned int count_topo_walked;
3373 static void trace2_topo_walk_statistics_atexit(void)
3375 struct json_writer jw = JSON_WRITER_INIT;
3377 jw_object_begin(&jw, 0);
3378 jw_object_intmax(&jw, "count_explore_walked", count_explore_walked);
3379 jw_object_intmax(&jw, "count_indegree_walked", count_indegree_walked);
3380 jw_object_intmax(&jw, "count_topo_walked", count_topo_walked);
3381 jw_end(&jw);
3383 trace2_data_json("topo_walk", the_repository, "statistics", &jw);
3385 jw_release(&jw);
3388 static inline void test_flag_and_insert(struct prio_queue *q, struct commit *c, int flag)
3390 if (c->object.flags & flag)
3391 return;
3393 c->object.flags |= flag;
3394 prio_queue_put(q, c);
3397 static void explore_walk_step(struct rev_info *revs)
3399 struct topo_walk_info *info = revs->topo_walk_info;
3400 struct commit_list *p;
3401 struct commit *c = prio_queue_get(&info->explore_queue);
3403 if (!c)
3404 return;
3406 if (repo_parse_commit_gently(revs->repo, c, 1) < 0)
3407 return;
3409 count_explore_walked++;
3411 if (revs->sort_order == REV_SORT_BY_AUTHOR_DATE)
3412 record_author_date(&info->author_date, c);
3414 if (revs->max_age != -1 && (c->date < revs->max_age))
3415 c->object.flags |= UNINTERESTING;
3417 if (process_parents(revs, c, NULL, NULL) < 0)
3418 return;
3420 if (c->object.flags & UNINTERESTING)
3421 mark_parents_uninteresting(revs, c);
3423 for (p = c->parents; p; p = p->next)
3424 test_flag_and_insert(&info->explore_queue, p->item, TOPO_WALK_EXPLORED);
3427 static void explore_to_depth(struct rev_info *revs,
3428 timestamp_t gen_cutoff)
3430 struct topo_walk_info *info = revs->topo_walk_info;
3431 struct commit *c;
3432 while ((c = prio_queue_peek(&info->explore_queue)) &&
3433 commit_graph_generation(c) >= gen_cutoff)
3434 explore_walk_step(revs);
3437 static void indegree_walk_step(struct rev_info *revs)
3439 struct commit_list *p;
3440 struct topo_walk_info *info = revs->topo_walk_info;
3441 struct commit *c = prio_queue_get(&info->indegree_queue);
3443 if (!c)
3444 return;
3446 if (repo_parse_commit_gently(revs->repo, c, 1) < 0)
3447 return;
3449 count_indegree_walked++;
3451 explore_to_depth(revs, commit_graph_generation(c));
3453 for (p = c->parents; p; p = p->next) {
3454 struct commit *parent = p->item;
3455 int *pi = indegree_slab_at(&info->indegree, parent);
3457 if (repo_parse_commit_gently(revs->repo, parent, 1) < 0)
3458 return;
3460 if (*pi)
3461 (*pi)++;
3462 else
3463 *pi = 2;
3465 test_flag_and_insert(&info->indegree_queue, parent, TOPO_WALK_INDEGREE);
3467 if (revs->first_parent_only)
3468 return;
3472 static void compute_indegrees_to_depth(struct rev_info *revs,
3473 timestamp_t gen_cutoff)
3475 struct topo_walk_info *info = revs->topo_walk_info;
3476 struct commit *c;
3477 while ((c = prio_queue_peek(&info->indegree_queue)) &&
3478 commit_graph_generation(c) >= gen_cutoff)
3479 indegree_walk_step(revs);
3482 static void release_revisions_topo_walk_info(struct topo_walk_info *info)
3484 if (!info)
3485 return;
3486 clear_prio_queue(&info->explore_queue);
3487 clear_prio_queue(&info->indegree_queue);
3488 clear_prio_queue(&info->topo_queue);
3489 clear_indegree_slab(&info->indegree);
3490 clear_author_date_slab(&info->author_date);
3491 free(info);
3494 static void reset_topo_walk(struct rev_info *revs)
3496 release_revisions_topo_walk_info(revs->topo_walk_info);
3497 revs->topo_walk_info = NULL;
3500 static void init_topo_walk(struct rev_info *revs)
3502 struct topo_walk_info *info;
3503 struct commit_list *list;
3504 if (revs->topo_walk_info)
3505 reset_topo_walk(revs);
3507 revs->topo_walk_info = xmalloc(sizeof(struct topo_walk_info));
3508 info = revs->topo_walk_info;
3509 memset(info, 0, sizeof(struct topo_walk_info));
3511 init_indegree_slab(&info->indegree);
3512 memset(&info->explore_queue, 0, sizeof(info->explore_queue));
3513 memset(&info->indegree_queue, 0, sizeof(info->indegree_queue));
3514 memset(&info->topo_queue, 0, sizeof(info->topo_queue));
3516 switch (revs->sort_order) {
3517 default: /* REV_SORT_IN_GRAPH_ORDER */
3518 info->topo_queue.compare = NULL;
3519 break;
3520 case REV_SORT_BY_COMMIT_DATE:
3521 info->topo_queue.compare = compare_commits_by_commit_date;
3522 break;
3523 case REV_SORT_BY_AUTHOR_DATE:
3524 init_author_date_slab(&info->author_date);
3525 info->topo_queue.compare = compare_commits_by_author_date;
3526 info->topo_queue.cb_data = &info->author_date;
3527 break;
3530 info->explore_queue.compare = compare_commits_by_gen_then_commit_date;
3531 info->indegree_queue.compare = compare_commits_by_gen_then_commit_date;
3533 info->min_generation = GENERATION_NUMBER_INFINITY;
3534 for (list = revs->commits; list; list = list->next) {
3535 struct commit *c = list->item;
3536 timestamp_t generation;
3538 if (repo_parse_commit_gently(revs->repo, c, 1))
3539 continue;
3541 test_flag_and_insert(&info->explore_queue, c, TOPO_WALK_EXPLORED);
3542 test_flag_and_insert(&info->indegree_queue, c, TOPO_WALK_INDEGREE);
3544 generation = commit_graph_generation(c);
3545 if (generation < info->min_generation)
3546 info->min_generation = generation;
3548 *(indegree_slab_at(&info->indegree, c)) = 1;
3550 if (revs->sort_order == REV_SORT_BY_AUTHOR_DATE)
3551 record_author_date(&info->author_date, c);
3553 compute_indegrees_to_depth(revs, info->min_generation);
3555 for (list = revs->commits; list; list = list->next) {
3556 struct commit *c = list->item;
3558 if (*(indegree_slab_at(&info->indegree, c)) == 1)
3559 prio_queue_put(&info->topo_queue, c);
3563 * This is unfortunate; the initial tips need to be shown
3564 * in the order given from the revision traversal machinery.
3566 if (revs->sort_order == REV_SORT_IN_GRAPH_ORDER)
3567 prio_queue_reverse(&info->topo_queue);
3569 if (trace2_is_enabled() && !topo_walk_atexit_registered) {
3570 atexit(trace2_topo_walk_statistics_atexit);
3571 topo_walk_atexit_registered = 1;
3575 static struct commit *next_topo_commit(struct rev_info *revs)
3577 struct commit *c;
3578 struct topo_walk_info *info = revs->topo_walk_info;
3580 /* pop next off of topo_queue */
3581 c = prio_queue_get(&info->topo_queue);
3583 if (c)
3584 *(indegree_slab_at(&info->indegree, c)) = 0;
3586 return c;
3589 static void expand_topo_walk(struct rev_info *revs, struct commit *commit)
3591 struct commit_list *p;
3592 struct topo_walk_info *info = revs->topo_walk_info;
3593 if (process_parents(revs, commit, NULL, NULL) < 0) {
3594 if (!revs->ignore_missing_links)
3595 die("Failed to traverse parents of commit %s",
3596 oid_to_hex(&commit->object.oid));
3599 count_topo_walked++;
3601 for (p = commit->parents; p; p = p->next) {
3602 struct commit *parent = p->item;
3603 int *pi;
3604 timestamp_t generation;
3606 if (parent->object.flags & UNINTERESTING)
3607 continue;
3609 if (repo_parse_commit_gently(revs->repo, parent, 1) < 0)
3610 continue;
3612 generation = commit_graph_generation(parent);
3613 if (generation < info->min_generation) {
3614 info->min_generation = generation;
3615 compute_indegrees_to_depth(revs, info->min_generation);
3618 pi = indegree_slab_at(&info->indegree, parent);
3620 (*pi)--;
3621 if (*pi == 1)
3622 prio_queue_put(&info->topo_queue, parent);
3624 if (revs->first_parent_only)
3625 return;
3629 int prepare_revision_walk(struct rev_info *revs)
3631 int i;
3632 struct object_array old_pending;
3633 struct commit_list **next = &revs->commits;
3635 memcpy(&old_pending, &revs->pending, sizeof(old_pending));
3636 revs->pending.nr = 0;
3637 revs->pending.alloc = 0;
3638 revs->pending.objects = NULL;
3639 for (i = 0; i < old_pending.nr; i++) {
3640 struct object_array_entry *e = old_pending.objects + i;
3641 struct commit *commit = handle_commit(revs, e);
3642 if (commit) {
3643 if (!(commit->object.flags & SEEN)) {
3644 commit->object.flags |= SEEN;
3645 next = commit_list_append(commit, next);
3649 object_array_clear(&old_pending);
3651 /* Signal whether we need per-parent treesame decoration */
3652 if (revs->simplify_merges ||
3653 (revs->limited && limiting_can_increase_treesame(revs)))
3654 revs->treesame.name = "treesame";
3656 if (revs->exclude_promisor_objects) {
3657 for_each_packed_object(mark_uninteresting, revs,
3658 FOR_EACH_OBJECT_PROMISOR_ONLY);
3661 if (!revs->reflog_info)
3662 prepare_to_use_bloom_filter(revs);
3663 if (!revs->unsorted_input)
3664 commit_list_sort_by_date(&revs->commits);
3665 if (revs->no_walk)
3666 return 0;
3667 if (revs->limited) {
3668 if (limit_list(revs) < 0)
3669 return -1;
3670 if (revs->topo_order)
3671 sort_in_topological_order(&revs->commits, revs->sort_order);
3672 } else if (revs->topo_order)
3673 init_topo_walk(revs);
3674 if (revs->line_level_traverse && want_ancestry(revs))
3676 * At the moment we can only do line-level log with parent
3677 * rewriting by performing this expensive pre-filtering step.
3678 * If parent rewriting is not requested, then we rather
3679 * perform the line-level log filtering during the regular
3680 * history traversal.
3682 line_log_filter(revs);
3683 if (revs->simplify_merges)
3684 simplify_merges(revs);
3685 if (revs->children.name)
3686 set_children(revs);
3688 return 0;
3691 static enum rewrite_result rewrite_one_1(struct rev_info *revs,
3692 struct commit **pp,
3693 struct prio_queue *queue)
3695 for (;;) {
3696 struct commit *p = *pp;
3697 if (!revs->limited)
3698 if (process_parents(revs, p, NULL, queue) < 0)
3699 return rewrite_one_error;
3700 if (p->object.flags & UNINTERESTING)
3701 return rewrite_one_ok;
3702 if (!(p->object.flags & TREESAME))
3703 return rewrite_one_ok;
3704 if (!p->parents)
3705 return rewrite_one_noparents;
3706 if (!(p = one_relevant_parent(revs, p->parents)))
3707 return rewrite_one_ok;
3708 *pp = p;
3712 static void merge_queue_into_list(struct prio_queue *q, struct commit_list **list)
3714 while (q->nr) {
3715 struct commit *item = prio_queue_peek(q);
3716 struct commit_list *p = *list;
3718 if (p && p->item->date >= item->date)
3719 list = &p->next;
3720 else {
3721 p = commit_list_insert(item, list);
3722 list = &p->next; /* skip newly added item */
3723 prio_queue_get(q); /* pop item */
3728 static enum rewrite_result rewrite_one(struct rev_info *revs, struct commit **pp)
3730 struct prio_queue queue = { compare_commits_by_commit_date };
3731 enum rewrite_result ret = rewrite_one_1(revs, pp, &queue);
3732 merge_queue_into_list(&queue, &revs->commits);
3733 clear_prio_queue(&queue);
3734 return ret;
3737 int rewrite_parents(struct rev_info *revs, struct commit *commit,
3738 rewrite_parent_fn_t rewrite_parent)
3740 struct commit_list **pp = &commit->parents;
3741 while (*pp) {
3742 struct commit_list *parent = *pp;
3743 switch (rewrite_parent(revs, &parent->item)) {
3744 case rewrite_one_ok:
3745 break;
3746 case rewrite_one_noparents:
3747 *pp = parent->next;
3748 continue;
3749 case rewrite_one_error:
3750 return -1;
3752 pp = &parent->next;
3754 remove_duplicate_parents(revs, commit);
3755 return 0;
3758 static int commit_rewrite_person(struct strbuf *buf, const char *what, struct string_list *mailmap)
3760 char *person, *endp;
3761 size_t len, namelen, maillen;
3762 const char *name;
3763 const char *mail;
3764 struct ident_split ident;
3766 person = strstr(buf->buf, what);
3767 if (!person)
3768 return 0;
3770 person += strlen(what);
3771 endp = strchr(person, '\n');
3772 if (!endp)
3773 return 0;
3775 len = endp - person;
3777 if (split_ident_line(&ident, person, len))
3778 return 0;
3780 mail = ident.mail_begin;
3781 maillen = ident.mail_end - ident.mail_begin;
3782 name = ident.name_begin;
3783 namelen = ident.name_end - ident.name_begin;
3785 if (map_user(mailmap, &mail, &maillen, &name, &namelen)) {
3786 struct strbuf namemail = STRBUF_INIT;
3788 strbuf_addf(&namemail, "%.*s <%.*s>",
3789 (int)namelen, name, (int)maillen, mail);
3791 strbuf_splice(buf, ident.name_begin - buf->buf,
3792 ident.mail_end - ident.name_begin + 1,
3793 namemail.buf, namemail.len);
3795 strbuf_release(&namemail);
3797 return 1;
3800 return 0;
3803 static int commit_match(struct commit *commit, struct rev_info *opt)
3805 int retval;
3806 const char *encoding;
3807 const char *message;
3808 struct strbuf buf = STRBUF_INIT;
3810 if (!opt->grep_filter.pattern_list && !opt->grep_filter.header_list)
3811 return 1;
3813 /* Prepend "fake" headers as needed */
3814 if (opt->grep_filter.use_reflog_filter) {
3815 strbuf_addstr(&buf, "reflog ");
3816 get_reflog_message(&buf, opt->reflog_info);
3817 strbuf_addch(&buf, '\n');
3821 * We grep in the user's output encoding, under the assumption that it
3822 * is the encoding they are most likely to write their grep pattern
3823 * for. In addition, it means we will match the "notes" encoding below,
3824 * so we will not end up with a buffer that has two different encodings
3825 * in it.
3827 encoding = get_log_output_encoding();
3828 message = logmsg_reencode(commit, NULL, encoding);
3830 /* Copy the commit to temporary if we are using "fake" headers */
3831 if (buf.len)
3832 strbuf_addstr(&buf, message);
3834 if (opt->grep_filter.header_list && opt->mailmap) {
3835 if (!buf.len)
3836 strbuf_addstr(&buf, message);
3838 commit_rewrite_person(&buf, "\nauthor ", opt->mailmap);
3839 commit_rewrite_person(&buf, "\ncommitter ", opt->mailmap);
3842 /* Append "fake" message parts as needed */
3843 if (opt->show_notes) {
3844 if (!buf.len)
3845 strbuf_addstr(&buf, message);
3846 format_display_notes(&commit->object.oid, &buf, encoding, 1);
3850 * Find either in the original commit message, or in the temporary.
3851 * Note that we cast away the constness of "message" here. It is
3852 * const because it may come from the cached commit buffer. That's OK,
3853 * because we know that it is modifiable heap memory, and that while
3854 * grep_buffer may modify it for speed, it will restore any
3855 * changes before returning.
3857 if (buf.len)
3858 retval = grep_buffer(&opt->grep_filter, buf.buf, buf.len);
3859 else
3860 retval = grep_buffer(&opt->grep_filter,
3861 (char *)message, strlen(message));
3862 strbuf_release(&buf);
3863 unuse_commit_buffer(commit, message);
3864 return retval;
3867 static inline int want_ancestry(const struct rev_info *revs)
3869 return (revs->rewrite_parents || revs->children.name);
3873 * Return a timestamp to be used for --since/--until comparisons for this
3874 * commit, based on the revision options.
3876 static timestamp_t comparison_date(const struct rev_info *revs,
3877 struct commit *commit)
3879 return revs->reflog_info ?
3880 get_reflog_timestamp(revs->reflog_info) :
3881 commit->date;
3884 enum commit_action get_commit_action(struct rev_info *revs, struct commit *commit)
3886 if (commit->object.flags & SHOWN)
3887 return commit_ignore;
3888 if (revs->unpacked && has_object_pack(&commit->object.oid))
3889 return commit_ignore;
3890 if (revs->no_kept_objects) {
3891 if (has_object_kept_pack(&commit->object.oid,
3892 revs->keep_pack_cache_flags))
3893 return commit_ignore;
3895 if (commit->object.flags & UNINTERESTING)
3896 return commit_ignore;
3897 if (revs->line_level_traverse && !want_ancestry(revs)) {
3899 * In case of line-level log with parent rewriting
3900 * prepare_revision_walk() already took care of all line-level
3901 * log filtering, and there is nothing left to do here.
3903 * If parent rewriting was not requested, then this is the
3904 * place to perform the line-level log filtering. Notably,
3905 * this check, though expensive, must come before the other,
3906 * cheaper filtering conditions, because the tracked line
3907 * ranges must be adjusted even when the commit will end up
3908 * being ignored based on other conditions.
3910 if (!line_log_process_ranges_arbitrary_commit(revs, commit))
3911 return commit_ignore;
3913 if (revs->min_age != -1 &&
3914 comparison_date(revs, commit) > revs->min_age)
3915 return commit_ignore;
3916 if (revs->max_age_as_filter != -1 &&
3917 comparison_date(revs, commit) < revs->max_age_as_filter)
3918 return commit_ignore;
3919 if (revs->min_parents || (revs->max_parents >= 0)) {
3920 int n = commit_list_count(commit->parents);
3921 if ((n < revs->min_parents) ||
3922 ((revs->max_parents >= 0) && (n > revs->max_parents)))
3923 return commit_ignore;
3925 if (!commit_match(commit, revs))
3926 return commit_ignore;
3927 if (revs->prune && revs->dense) {
3928 /* Commit without changes? */
3929 if (commit->object.flags & TREESAME) {
3930 int n;
3931 struct commit_list *p;
3932 /* drop merges unless we want parenthood */
3933 if (!want_ancestry(revs))
3934 return commit_ignore;
3936 if (revs->show_pulls && (commit->object.flags & PULL_MERGE))
3937 return commit_show;
3940 * If we want ancestry, then need to keep any merges
3941 * between relevant commits to tie together topology.
3942 * For consistency with TREESAME and simplification
3943 * use "relevant" here rather than just INTERESTING,
3944 * to treat bottom commit(s) as part of the topology.
3946 for (n = 0, p = commit->parents; p; p = p->next)
3947 if (relevant_commit(p->item))
3948 if (++n >= 2)
3949 return commit_show;
3950 return commit_ignore;
3953 return commit_show;
3956 define_commit_slab(saved_parents, struct commit_list *);
3958 #define EMPTY_PARENT_LIST ((struct commit_list *)-1)
3961 * You may only call save_parents() once per commit (this is checked
3962 * for non-root commits).
3964 static void save_parents(struct rev_info *revs, struct commit *commit)
3966 struct commit_list **pp;
3968 if (!revs->saved_parents_slab) {
3969 revs->saved_parents_slab = xmalloc(sizeof(struct saved_parents));
3970 init_saved_parents(revs->saved_parents_slab);
3973 pp = saved_parents_at(revs->saved_parents_slab, commit);
3976 * When walking with reflogs, we may visit the same commit
3977 * several times: once for each appearance in the reflog.
3979 * In this case, save_parents() will be called multiple times.
3980 * We want to keep only the first set of parents. We need to
3981 * store a sentinel value for an empty (i.e., NULL) parent
3982 * list to distinguish it from a not-yet-saved list, however.
3984 if (*pp)
3985 return;
3986 if (commit->parents)
3987 *pp = copy_commit_list(commit->parents);
3988 else
3989 *pp = EMPTY_PARENT_LIST;
3992 static void free_saved_parents(struct rev_info *revs)
3994 if (revs->saved_parents_slab)
3995 clear_saved_parents(revs->saved_parents_slab);
3998 struct commit_list *get_saved_parents(struct rev_info *revs, const struct commit *commit)
4000 struct commit_list *parents;
4002 if (!revs->saved_parents_slab)
4003 return commit->parents;
4005 parents = *saved_parents_at(revs->saved_parents_slab, commit);
4006 if (parents == EMPTY_PARENT_LIST)
4007 return NULL;
4008 return parents;
4011 enum commit_action simplify_commit(struct rev_info *revs, struct commit *commit)
4013 enum commit_action action = get_commit_action(revs, commit);
4015 if (action == commit_show &&
4016 revs->prune && revs->dense && want_ancestry(revs)) {
4018 * --full-diff on simplified parents is no good: it
4019 * will show spurious changes from the commits that
4020 * were elided. So we save the parents on the side
4021 * when --full-diff is in effect.
4023 if (revs->full_diff)
4024 save_parents(revs, commit);
4025 if (rewrite_parents(revs, commit, rewrite_one) < 0)
4026 return commit_error;
4028 return action;
4031 static void track_linear(struct rev_info *revs, struct commit *commit)
4033 if (revs->track_first_time) {
4034 revs->linear = 1;
4035 revs->track_first_time = 0;
4036 } else {
4037 struct commit_list *p;
4038 for (p = revs->previous_parents; p; p = p->next)
4039 if (p->item == NULL || /* first commit */
4040 oideq(&p->item->object.oid, &commit->object.oid))
4041 break;
4042 revs->linear = p != NULL;
4044 if (revs->reverse) {
4045 if (revs->linear)
4046 commit->object.flags |= TRACK_LINEAR;
4048 free_commit_list(revs->previous_parents);
4049 revs->previous_parents = copy_commit_list(commit->parents);
4052 static struct commit *get_revision_1(struct rev_info *revs)
4054 while (1) {
4055 struct commit *commit;
4057 if (revs->reflog_info)
4058 commit = next_reflog_entry(revs->reflog_info);
4059 else if (revs->topo_walk_info)
4060 commit = next_topo_commit(revs);
4061 else
4062 commit = pop_commit(&revs->commits);
4064 if (!commit)
4065 return NULL;
4067 if (revs->reflog_info)
4068 commit->object.flags &= ~(ADDED | SEEN | SHOWN);
4071 * If we haven't done the list limiting, we need to look at
4072 * the parents here. We also need to do the date-based limiting
4073 * that we'd otherwise have done in limit_list().
4075 if (!revs->limited) {
4076 if (revs->max_age != -1 &&
4077 comparison_date(revs, commit) < revs->max_age)
4078 continue;
4080 if (revs->reflog_info)
4081 try_to_simplify_commit(revs, commit);
4082 else if (revs->topo_walk_info)
4083 expand_topo_walk(revs, commit);
4084 else if (process_parents(revs, commit, &revs->commits, NULL) < 0) {
4085 if (!revs->ignore_missing_links)
4086 die("Failed to traverse parents of commit %s",
4087 oid_to_hex(&commit->object.oid));
4091 switch (simplify_commit(revs, commit)) {
4092 case commit_ignore:
4093 continue;
4094 case commit_error:
4095 die("Failed to simplify parents of commit %s",
4096 oid_to_hex(&commit->object.oid));
4097 default:
4098 if (revs->track_linear)
4099 track_linear(revs, commit);
4100 return commit;
4106 * Return true for entries that have not yet been shown. (This is an
4107 * object_array_each_func_t.)
4109 static int entry_unshown(struct object_array_entry *entry, void *cb_data_unused)
4111 return !(entry->item->flags & SHOWN);
4115 * If array is on the verge of a realloc, garbage-collect any entries
4116 * that have already been shown to try to free up some space.
4118 static void gc_boundary(struct object_array *array)
4120 if (array->nr == array->alloc)
4121 object_array_filter(array, entry_unshown, NULL);
4124 static void create_boundary_commit_list(struct rev_info *revs)
4126 unsigned i;
4127 struct commit *c;
4128 struct object_array *array = &revs->boundary_commits;
4129 struct object_array_entry *objects = array->objects;
4132 * If revs->commits is non-NULL at this point, an error occurred in
4133 * get_revision_1(). Ignore the error and continue printing the
4134 * boundary commits anyway. (This is what the code has always
4135 * done.)
4137 free_commit_list(revs->commits);
4138 revs->commits = NULL;
4141 * Put all of the actual boundary commits from revs->boundary_commits
4142 * into revs->commits
4144 for (i = 0; i < array->nr; i++) {
4145 c = (struct commit *)(objects[i].item);
4146 if (!c)
4147 continue;
4148 if (!(c->object.flags & CHILD_SHOWN))
4149 continue;
4150 if (c->object.flags & (SHOWN | BOUNDARY))
4151 continue;
4152 c->object.flags |= BOUNDARY;
4153 commit_list_insert(c, &revs->commits);
4157 * If revs->topo_order is set, sort the boundary commits
4158 * in topological order
4160 sort_in_topological_order(&revs->commits, revs->sort_order);
4163 static struct commit *get_revision_internal(struct rev_info *revs)
4165 struct commit *c = NULL;
4166 struct commit_list *l;
4168 if (revs->boundary == 2) {
4170 * All of the normal commits have already been returned,
4171 * and we are now returning boundary commits.
4172 * create_boundary_commit_list() has populated
4173 * revs->commits with the remaining commits to return.
4175 c = pop_commit(&revs->commits);
4176 if (c)
4177 c->object.flags |= SHOWN;
4178 return c;
4182 * If our max_count counter has reached zero, then we are done. We
4183 * don't simply return NULL because we still might need to show
4184 * boundary commits. But we want to avoid calling get_revision_1, which
4185 * might do a considerable amount of work finding the next commit only
4186 * for us to throw it away.
4188 * If it is non-zero, then either we don't have a max_count at all
4189 * (-1), or it is still counting, in which case we decrement.
4191 if (revs->max_count) {
4192 c = get_revision_1(revs);
4193 if (c) {
4194 while (revs->skip_count > 0) {
4195 revs->skip_count--;
4196 c = get_revision_1(revs);
4197 if (!c)
4198 break;
4202 if (revs->max_count > 0)
4203 revs->max_count--;
4206 if (c)
4207 c->object.flags |= SHOWN;
4209 if (!revs->boundary)
4210 return c;
4212 if (!c) {
4214 * get_revision_1() runs out the commits, and
4215 * we are done computing the boundaries.
4216 * switch to boundary commits output mode.
4218 revs->boundary = 2;
4221 * Update revs->commits to contain the list of
4222 * boundary commits.
4224 create_boundary_commit_list(revs);
4226 return get_revision_internal(revs);
4230 * boundary commits are the commits that are parents of the
4231 * ones we got from get_revision_1() but they themselves are
4232 * not returned from get_revision_1(). Before returning
4233 * 'c', we need to mark its parents that they could be boundaries.
4236 for (l = c->parents; l; l = l->next) {
4237 struct object *p;
4238 p = &(l->item->object);
4239 if (p->flags & (CHILD_SHOWN | SHOWN))
4240 continue;
4241 p->flags |= CHILD_SHOWN;
4242 gc_boundary(&revs->boundary_commits);
4243 add_object_array(p, NULL, &revs->boundary_commits);
4246 return c;
4249 struct commit *get_revision(struct rev_info *revs)
4251 struct commit *c;
4252 struct commit_list *reversed;
4254 if (revs->reverse) {
4255 reversed = NULL;
4256 while ((c = get_revision_internal(revs)))
4257 commit_list_insert(c, &reversed);
4258 revs->commits = reversed;
4259 revs->reverse = 0;
4260 revs->reverse_output_stage = 1;
4263 if (revs->reverse_output_stage) {
4264 c = pop_commit(&revs->commits);
4265 if (revs->track_linear)
4266 revs->linear = !!(c && c->object.flags & TRACK_LINEAR);
4267 return c;
4270 c = get_revision_internal(revs);
4271 if (c && revs->graph)
4272 graph_update(revs->graph, c);
4273 if (!c) {
4274 free_saved_parents(revs);
4275 free_commit_list(revs->previous_parents);
4276 revs->previous_parents = NULL;
4278 return c;
4281 const char *get_revision_mark(const struct rev_info *revs, const struct commit *commit)
4283 if (commit->object.flags & BOUNDARY)
4284 return "-";
4285 else if (commit->object.flags & UNINTERESTING)
4286 return "^";
4287 else if (commit->object.flags & PATCHSAME)
4288 return "=";
4289 else if (!revs || revs->left_right) {
4290 if (commit->object.flags & SYMMETRIC_LEFT)
4291 return "<";
4292 else
4293 return ">";
4294 } else if (revs->graph)
4295 return "*";
4296 else if (revs->cherry_mark)
4297 return "+";
4298 return "";
4301 void put_revision_mark(const struct rev_info *revs, const struct commit *commit)
4303 const char *mark = get_revision_mark(revs, commit);
4304 if (!strlen(mark))
4305 return;
4306 fputs(mark, stdout);
4307 putchar(' ');