Merge branch 'tb/midx-race-in-pack-objects'
[git/debian.git] / revision.c
blob090a967bf46aaec77f3bce578af58ca42f42d288
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 static int tree_difference = REV_TREE_SAME;
611 static void file_add_remove(struct diff_options *options,
612 int addremove, unsigned mode,
613 const struct object_id *oid,
614 int oid_valid,
615 const char *fullpath, unsigned dirty_submodule)
617 int diff = addremove == '+' ? REV_TREE_NEW : REV_TREE_OLD;
618 struct rev_info *revs = options->change_fn_data;
620 tree_difference |= diff;
621 if (!revs->remove_empty_trees || tree_difference != REV_TREE_NEW)
622 options->flags.has_changes = 1;
625 static void file_change(struct diff_options *options,
626 unsigned old_mode, unsigned new_mode,
627 const struct object_id *old_oid,
628 const struct object_id *new_oid,
629 int old_oid_valid, int new_oid_valid,
630 const char *fullpath,
631 unsigned old_dirty_submodule, unsigned new_dirty_submodule)
633 tree_difference = REV_TREE_DIFFERENT;
634 options->flags.has_changes = 1;
637 static int bloom_filter_atexit_registered;
638 static unsigned int count_bloom_filter_maybe;
639 static unsigned int count_bloom_filter_definitely_not;
640 static unsigned int count_bloom_filter_false_positive;
641 static unsigned int count_bloom_filter_not_present;
643 static void trace2_bloom_filter_statistics_atexit(void)
645 struct json_writer jw = JSON_WRITER_INIT;
647 jw_object_begin(&jw, 0);
648 jw_object_intmax(&jw, "filter_not_present", count_bloom_filter_not_present);
649 jw_object_intmax(&jw, "maybe", count_bloom_filter_maybe);
650 jw_object_intmax(&jw, "definitely_not", count_bloom_filter_definitely_not);
651 jw_object_intmax(&jw, "false_positive", count_bloom_filter_false_positive);
652 jw_end(&jw);
654 trace2_data_json("bloom", the_repository, "statistics", &jw);
656 jw_release(&jw);
659 static int forbid_bloom_filters(struct pathspec *spec)
661 if (spec->has_wildcard)
662 return 1;
663 if (spec->nr > 1)
664 return 1;
665 if (spec->magic & ~PATHSPEC_LITERAL)
666 return 1;
667 if (spec->nr && (spec->items[0].magic & ~PATHSPEC_LITERAL))
668 return 1;
670 return 0;
673 static void prepare_to_use_bloom_filter(struct rev_info *revs)
675 struct pathspec_item *pi;
676 char *path_alloc = NULL;
677 const char *path, *p;
678 size_t len;
679 int path_component_nr = 1;
681 if (!revs->commits)
682 return;
684 if (forbid_bloom_filters(&revs->prune_data))
685 return;
687 repo_parse_commit(revs->repo, revs->commits->item);
689 revs->bloom_filter_settings = get_bloom_filter_settings(revs->repo);
690 if (!revs->bloom_filter_settings)
691 return;
693 if (!revs->pruning.pathspec.nr)
694 return;
696 pi = &revs->pruning.pathspec.items[0];
698 /* remove single trailing slash from path, if needed */
699 if (pi->len > 0 && pi->match[pi->len - 1] == '/') {
700 path_alloc = xmemdupz(pi->match, pi->len - 1);
701 path = path_alloc;
702 } else
703 path = pi->match;
705 len = strlen(path);
706 if (!len) {
707 revs->bloom_filter_settings = NULL;
708 free(path_alloc);
709 return;
712 p = path;
713 while (*p) {
715 * At this point, the path is normalized to use Unix-style
716 * path separators. This is required due to how the
717 * changed-path Bloom filters store the paths.
719 if (*p == '/')
720 path_component_nr++;
721 p++;
724 revs->bloom_keys_nr = path_component_nr;
725 ALLOC_ARRAY(revs->bloom_keys, revs->bloom_keys_nr);
727 fill_bloom_key(path, len, &revs->bloom_keys[0],
728 revs->bloom_filter_settings);
729 path_component_nr = 1;
731 p = path + len - 1;
732 while (p > path) {
733 if (*p == '/')
734 fill_bloom_key(path, p - path,
735 &revs->bloom_keys[path_component_nr++],
736 revs->bloom_filter_settings);
737 p--;
740 if (trace2_is_enabled() && !bloom_filter_atexit_registered) {
741 atexit(trace2_bloom_filter_statistics_atexit);
742 bloom_filter_atexit_registered = 1;
745 free(path_alloc);
748 static int check_maybe_different_in_bloom_filter(struct rev_info *revs,
749 struct commit *commit)
751 struct bloom_filter *filter;
752 int result = 1, j;
754 if (!revs->repo->objects->commit_graph)
755 return -1;
757 if (commit_graph_generation(commit) == GENERATION_NUMBER_INFINITY)
758 return -1;
760 filter = get_bloom_filter(revs->repo, commit);
762 if (!filter) {
763 count_bloom_filter_not_present++;
764 return -1;
767 for (j = 0; result && j < revs->bloom_keys_nr; j++) {
768 result = bloom_filter_contains(filter,
769 &revs->bloom_keys[j],
770 revs->bloom_filter_settings);
773 if (result)
774 count_bloom_filter_maybe++;
775 else
776 count_bloom_filter_definitely_not++;
778 return result;
781 static int rev_compare_tree(struct rev_info *revs,
782 struct commit *parent, struct commit *commit, int nth_parent)
784 struct tree *t1 = get_commit_tree(parent);
785 struct tree *t2 = get_commit_tree(commit);
786 int bloom_ret = 1;
788 if (!t1)
789 return REV_TREE_NEW;
790 if (!t2)
791 return REV_TREE_OLD;
793 if (revs->simplify_by_decoration) {
795 * If we are simplifying by decoration, then the commit
796 * is worth showing if it has a tag pointing at it.
798 if (get_name_decoration(&commit->object))
799 return REV_TREE_DIFFERENT;
801 * A commit that is not pointed by a tag is uninteresting
802 * if we are not limited by path. This means that you will
803 * see the usual "commits that touch the paths" plus any
804 * tagged commit by specifying both --simplify-by-decoration
805 * and pathspec.
807 if (!revs->prune_data.nr)
808 return REV_TREE_SAME;
811 if (revs->bloom_keys_nr && !nth_parent) {
812 bloom_ret = check_maybe_different_in_bloom_filter(revs, commit);
814 if (bloom_ret == 0)
815 return REV_TREE_SAME;
818 tree_difference = REV_TREE_SAME;
819 revs->pruning.flags.has_changes = 0;
820 diff_tree_oid(&t1->object.oid, &t2->object.oid, "", &revs->pruning);
822 if (!nth_parent)
823 if (bloom_ret == 1 && tree_difference == REV_TREE_SAME)
824 count_bloom_filter_false_positive++;
826 return tree_difference;
829 static int rev_same_tree_as_empty(struct rev_info *revs, struct commit *commit)
831 struct tree *t1 = get_commit_tree(commit);
833 if (!t1)
834 return 0;
836 tree_difference = REV_TREE_SAME;
837 revs->pruning.flags.has_changes = 0;
838 diff_tree_oid(NULL, &t1->object.oid, "", &revs->pruning);
840 return tree_difference == REV_TREE_SAME;
843 struct treesame_state {
844 unsigned int nparents;
845 unsigned char treesame[FLEX_ARRAY];
848 static struct treesame_state *initialise_treesame(struct rev_info *revs, struct commit *commit)
850 unsigned n = commit_list_count(commit->parents);
851 struct treesame_state *st = xcalloc(1, st_add(sizeof(*st), n));
852 st->nparents = n;
853 add_decoration(&revs->treesame, &commit->object, st);
854 return st;
858 * Must be called immediately after removing the nth_parent from a commit's
859 * parent list, if we are maintaining the per-parent treesame[] decoration.
860 * This does not recalculate the master TREESAME flag - update_treesame()
861 * should be called to update it after a sequence of treesame[] modifications
862 * that may have affected it.
864 static int compact_treesame(struct rev_info *revs, struct commit *commit, unsigned nth_parent)
866 struct treesame_state *st;
867 int old_same;
869 if (!commit->parents) {
871 * Have just removed the only parent from a non-merge.
872 * Different handling, as we lack decoration.
874 if (nth_parent != 0)
875 die("compact_treesame %u", nth_parent);
876 old_same = !!(commit->object.flags & TREESAME);
877 if (rev_same_tree_as_empty(revs, commit))
878 commit->object.flags |= TREESAME;
879 else
880 commit->object.flags &= ~TREESAME;
881 return old_same;
884 st = lookup_decoration(&revs->treesame, &commit->object);
885 if (!st || nth_parent >= st->nparents)
886 die("compact_treesame %u", nth_parent);
888 old_same = st->treesame[nth_parent];
889 memmove(st->treesame + nth_parent,
890 st->treesame + nth_parent + 1,
891 st->nparents - nth_parent - 1);
894 * If we've just become a non-merge commit, update TREESAME
895 * immediately, and remove the no-longer-needed decoration.
896 * If still a merge, defer update until update_treesame().
898 if (--st->nparents == 1) {
899 if (commit->parents->next)
900 die("compact_treesame parents mismatch");
901 if (st->treesame[0] && revs->dense)
902 commit->object.flags |= TREESAME;
903 else
904 commit->object.flags &= ~TREESAME;
905 free(add_decoration(&revs->treesame, &commit->object, NULL));
908 return old_same;
911 static unsigned update_treesame(struct rev_info *revs, struct commit *commit)
913 if (commit->parents && commit->parents->next) {
914 unsigned n;
915 struct treesame_state *st;
916 struct commit_list *p;
917 unsigned relevant_parents;
918 unsigned relevant_change, irrelevant_change;
920 st = lookup_decoration(&revs->treesame, &commit->object);
921 if (!st)
922 die("update_treesame %s", oid_to_hex(&commit->object.oid));
923 relevant_parents = 0;
924 relevant_change = irrelevant_change = 0;
925 for (p = commit->parents, n = 0; p; n++, p = p->next) {
926 if (relevant_commit(p->item)) {
927 relevant_change |= !st->treesame[n];
928 relevant_parents++;
929 } else
930 irrelevant_change |= !st->treesame[n];
932 if (relevant_parents ? relevant_change : irrelevant_change)
933 commit->object.flags &= ~TREESAME;
934 else
935 commit->object.flags |= TREESAME;
938 return commit->object.flags & TREESAME;
941 static inline int limiting_can_increase_treesame(const struct rev_info *revs)
944 * TREESAME is irrelevant unless prune && dense;
945 * if simplify_history is set, we can't have a mixture of TREESAME and
946 * !TREESAME INTERESTING parents (and we don't have treesame[]
947 * decoration anyway);
948 * if first_parent_only is set, then the TREESAME flag is locked
949 * against the first parent (and again we lack treesame[] decoration).
951 return revs->prune && revs->dense &&
952 !revs->simplify_history &&
953 !revs->first_parent_only;
956 static void try_to_simplify_commit(struct rev_info *revs, struct commit *commit)
958 struct commit_list **pp, *parent;
959 struct treesame_state *ts = NULL;
960 int relevant_change = 0, irrelevant_change = 0;
961 int relevant_parents, nth_parent;
964 * If we don't do pruning, everything is interesting
966 if (!revs->prune)
967 return;
969 if (!get_commit_tree(commit))
970 return;
972 if (!commit->parents) {
973 if (rev_same_tree_as_empty(revs, commit))
974 commit->object.flags |= TREESAME;
975 return;
979 * Normal non-merge commit? If we don't want to make the
980 * history dense, we consider it always to be a change..
982 if (!revs->dense && !commit->parents->next)
983 return;
985 for (pp = &commit->parents, nth_parent = 0, relevant_parents = 0;
986 (parent = *pp) != NULL;
987 pp = &parent->next, nth_parent++) {
988 struct commit *p = parent->item;
989 if (relevant_commit(p))
990 relevant_parents++;
992 if (nth_parent == 1) {
994 * This our second loop iteration - so we now know
995 * we're dealing with a merge.
997 * Do not compare with later parents when we care only about
998 * the first parent chain, in order to avoid derailing the
999 * traversal to follow a side branch that brought everything
1000 * in the path we are limited to by the pathspec.
1002 if (revs->first_parent_only)
1003 break;
1005 * If this will remain a potentially-simplifiable
1006 * merge, remember per-parent treesame if needed.
1007 * Initialise the array with the comparison from our
1008 * first iteration.
1010 if (revs->treesame.name &&
1011 !revs->simplify_history &&
1012 !(commit->object.flags & UNINTERESTING)) {
1013 ts = initialise_treesame(revs, commit);
1014 if (!(irrelevant_change || relevant_change))
1015 ts->treesame[0] = 1;
1018 if (repo_parse_commit(revs->repo, p) < 0)
1019 die("cannot simplify commit %s (because of %s)",
1020 oid_to_hex(&commit->object.oid),
1021 oid_to_hex(&p->object.oid));
1022 switch (rev_compare_tree(revs, p, commit, nth_parent)) {
1023 case REV_TREE_SAME:
1024 if (!revs->simplify_history || !relevant_commit(p)) {
1025 /* Even if a merge with an uninteresting
1026 * side branch brought the entire change
1027 * we are interested in, we do not want
1028 * to lose the other branches of this
1029 * merge, so we just keep going.
1031 if (ts)
1032 ts->treesame[nth_parent] = 1;
1033 continue;
1035 parent->next = NULL;
1036 commit->parents = parent;
1039 * A merge commit is a "diversion" if it is not
1040 * TREESAME to its first parent but is TREESAME
1041 * to a later parent. In the simplified history,
1042 * we "divert" the history walk to the later
1043 * parent. These commits are shown when "show_pulls"
1044 * is enabled, so do not mark the object as
1045 * TREESAME here.
1047 if (!revs->show_pulls || !nth_parent)
1048 commit->object.flags |= TREESAME;
1050 return;
1052 case REV_TREE_NEW:
1053 if (revs->remove_empty_trees &&
1054 rev_same_tree_as_empty(revs, p)) {
1055 /* We are adding all the specified
1056 * paths from this parent, so the
1057 * history beyond this parent is not
1058 * interesting. Remove its parents
1059 * (they are grandparents for us).
1060 * IOW, we pretend this parent is a
1061 * "root" commit.
1063 if (repo_parse_commit(revs->repo, p) < 0)
1064 die("cannot simplify commit %s (invalid %s)",
1065 oid_to_hex(&commit->object.oid),
1066 oid_to_hex(&p->object.oid));
1067 p->parents = NULL;
1069 /* fallthrough */
1070 case REV_TREE_OLD:
1071 case REV_TREE_DIFFERENT:
1072 if (relevant_commit(p))
1073 relevant_change = 1;
1074 else
1075 irrelevant_change = 1;
1077 if (!nth_parent)
1078 commit->object.flags |= PULL_MERGE;
1080 continue;
1082 die("bad tree compare for commit %s", oid_to_hex(&commit->object.oid));
1086 * TREESAME is straightforward for single-parent commits. For merge
1087 * commits, it is most useful to define it so that "irrelevant"
1088 * parents cannot make us !TREESAME - if we have any relevant
1089 * parents, then we only consider TREESAMEness with respect to them,
1090 * allowing irrelevant merges from uninteresting branches to be
1091 * simplified away. Only if we have only irrelevant parents do we
1092 * base TREESAME on them. Note that this logic is replicated in
1093 * update_treesame, which should be kept in sync.
1095 if (relevant_parents ? !relevant_change : !irrelevant_change)
1096 commit->object.flags |= TREESAME;
1099 static int process_parents(struct rev_info *revs, struct commit *commit,
1100 struct commit_list **list, struct prio_queue *queue)
1102 struct commit_list *parent = commit->parents;
1103 unsigned left_flag;
1105 if (commit->object.flags & ADDED)
1106 return 0;
1107 commit->object.flags |= ADDED;
1109 if (revs->include_check &&
1110 !revs->include_check(commit, revs->include_check_data))
1111 return 0;
1114 * If the commit is uninteresting, don't try to
1115 * prune parents - we want the maximal uninteresting
1116 * set.
1118 * Normally we haven't parsed the parent
1119 * yet, so we won't have a parent of a parent
1120 * here. However, it may turn out that we've
1121 * reached this commit some other way (where it
1122 * wasn't uninteresting), in which case we need
1123 * to mark its parents recursively too..
1125 if (commit->object.flags & UNINTERESTING) {
1126 while (parent) {
1127 struct commit *p = parent->item;
1128 parent = parent->next;
1129 if (p)
1130 p->object.flags |= UNINTERESTING;
1131 if (repo_parse_commit_gently(revs->repo, p, 1) < 0)
1132 continue;
1133 if (p->parents)
1134 mark_parents_uninteresting(revs, p);
1135 if (p->object.flags & SEEN)
1136 continue;
1137 p->object.flags |= (SEEN | NOT_USER_GIVEN);
1138 if (list)
1139 commit_list_insert_by_date(p, list);
1140 if (queue)
1141 prio_queue_put(queue, p);
1142 if (revs->exclude_first_parent_only)
1143 break;
1145 return 0;
1149 * Ok, the commit wasn't uninteresting. Try to
1150 * simplify the commit history and find the parent
1151 * that has no differences in the path set if one exists.
1153 try_to_simplify_commit(revs, commit);
1155 if (revs->no_walk)
1156 return 0;
1158 left_flag = (commit->object.flags & SYMMETRIC_LEFT);
1160 for (parent = commit->parents; parent; parent = parent->next) {
1161 struct commit *p = parent->item;
1162 int gently = revs->ignore_missing_links ||
1163 revs->exclude_promisor_objects;
1164 if (repo_parse_commit_gently(revs->repo, p, gently) < 0) {
1165 if (revs->exclude_promisor_objects &&
1166 is_promisor_object(&p->object.oid)) {
1167 if (revs->first_parent_only)
1168 break;
1169 continue;
1171 return -1;
1173 if (revs->sources) {
1174 char **slot = revision_sources_at(revs->sources, p);
1176 if (!*slot)
1177 *slot = *revision_sources_at(revs->sources, commit);
1179 p->object.flags |= left_flag;
1180 if (!(p->object.flags & SEEN)) {
1181 p->object.flags |= (SEEN | NOT_USER_GIVEN);
1182 if (list)
1183 commit_list_insert_by_date(p, list);
1184 if (queue)
1185 prio_queue_put(queue, p);
1187 if (revs->first_parent_only)
1188 break;
1190 return 0;
1193 static void cherry_pick_list(struct commit_list *list, struct rev_info *revs)
1195 struct commit_list *p;
1196 int left_count = 0, right_count = 0;
1197 int left_first;
1198 struct patch_ids ids;
1199 unsigned cherry_flag;
1201 /* First count the commits on the left and on the right */
1202 for (p = list; p; p = p->next) {
1203 struct commit *commit = p->item;
1204 unsigned flags = commit->object.flags;
1205 if (flags & BOUNDARY)
1207 else if (flags & SYMMETRIC_LEFT)
1208 left_count++;
1209 else
1210 right_count++;
1213 if (!left_count || !right_count)
1214 return;
1216 left_first = left_count < right_count;
1217 init_patch_ids(revs->repo, &ids);
1218 ids.diffopts.pathspec = revs->diffopt.pathspec;
1220 /* Compute patch-ids for one side */
1221 for (p = list; p; p = p->next) {
1222 struct commit *commit = p->item;
1223 unsigned flags = commit->object.flags;
1225 if (flags & BOUNDARY)
1226 continue;
1228 * If we have fewer left, left_first is set and we omit
1229 * commits on the right branch in this loop. If we have
1230 * fewer right, we skip the left ones.
1232 if (left_first != !!(flags & SYMMETRIC_LEFT))
1233 continue;
1234 add_commit_patch_id(commit, &ids);
1237 /* either cherry_mark or cherry_pick are true */
1238 cherry_flag = revs->cherry_mark ? PATCHSAME : SHOWN;
1240 /* Check the other side */
1241 for (p = list; p; p = p->next) {
1242 struct commit *commit = p->item;
1243 struct patch_id *id;
1244 unsigned flags = commit->object.flags;
1246 if (flags & BOUNDARY)
1247 continue;
1249 * If we have fewer left, left_first is set and we omit
1250 * commits on the left branch in this loop.
1252 if (left_first == !!(flags & SYMMETRIC_LEFT))
1253 continue;
1256 * Have we seen the same patch id?
1258 id = patch_id_iter_first(commit, &ids);
1259 if (!id)
1260 continue;
1262 commit->object.flags |= cherry_flag;
1263 do {
1264 id->commit->object.flags |= cherry_flag;
1265 } while ((id = patch_id_iter_next(id, &ids)));
1268 free_patch_ids(&ids);
1271 /* How many extra uninteresting commits we want to see.. */
1272 #define SLOP 5
1274 static int still_interesting(struct commit_list *src, timestamp_t date, int slop,
1275 struct commit **interesting_cache)
1278 * No source list at all? We're definitely done..
1280 if (!src)
1281 return 0;
1284 * Does the destination list contain entries with a date
1285 * before the source list? Definitely _not_ done.
1287 if (date <= src->item->date)
1288 return SLOP;
1291 * Does the source list still have interesting commits in
1292 * it? Definitely not done..
1294 if (!everybody_uninteresting(src, interesting_cache))
1295 return SLOP;
1297 /* Ok, we're closing in.. */
1298 return slop-1;
1302 * "rev-list --ancestry-path A..B" computes commits that are ancestors
1303 * of B but not ancestors of A but further limits the result to those
1304 * that are descendants of A. This takes the list of bottom commits and
1305 * the result of "A..B" without --ancestry-path, and limits the latter
1306 * further to the ones that can reach one of the commits in "bottom".
1308 static void limit_to_ancestry(struct commit_list *bottom, struct commit_list *list)
1310 struct commit_list *p;
1311 struct commit_list *rlist = NULL;
1312 int made_progress;
1315 * Reverse the list so that it will be likely that we would
1316 * process parents before children.
1318 for (p = list; p; p = p->next)
1319 commit_list_insert(p->item, &rlist);
1321 for (p = bottom; p; p = p->next)
1322 p->item->object.flags |= TMP_MARK;
1325 * Mark the ones that can reach bottom commits in "list",
1326 * in a bottom-up fashion.
1328 do {
1329 made_progress = 0;
1330 for (p = rlist; p; p = p->next) {
1331 struct commit *c = p->item;
1332 struct commit_list *parents;
1333 if (c->object.flags & (TMP_MARK | UNINTERESTING))
1334 continue;
1335 for (parents = c->parents;
1336 parents;
1337 parents = parents->next) {
1338 if (!(parents->item->object.flags & TMP_MARK))
1339 continue;
1340 c->object.flags |= TMP_MARK;
1341 made_progress = 1;
1342 break;
1345 } while (made_progress);
1348 * NEEDSWORK: decide if we want to remove parents that are
1349 * not marked with TMP_MARK from commit->parents for commits
1350 * in the resulting list. We may not want to do that, though.
1354 * The ones that are not marked with TMP_MARK are uninteresting
1356 for (p = list; p; p = p->next) {
1357 struct commit *c = p->item;
1358 if (c->object.flags & TMP_MARK)
1359 continue;
1360 c->object.flags |= UNINTERESTING;
1363 /* We are done with the TMP_MARK */
1364 for (p = list; p; p = p->next)
1365 p->item->object.flags &= ~TMP_MARK;
1366 for (p = bottom; p; p = p->next)
1367 p->item->object.flags &= ~TMP_MARK;
1368 free_commit_list(rlist);
1372 * Before walking the history, keep the set of "negative" refs the
1373 * caller has asked to exclude.
1375 * This is used to compute "rev-list --ancestry-path A..B", as we need
1376 * to filter the result of "A..B" further to the ones that can actually
1377 * reach A.
1379 static struct commit_list *collect_bottom_commits(struct commit_list *list)
1381 struct commit_list *elem, *bottom = NULL;
1382 for (elem = list; elem; elem = elem->next)
1383 if (elem->item->object.flags & BOTTOM)
1384 commit_list_insert(elem->item, &bottom);
1385 return bottom;
1388 /* Assumes either left_only or right_only is set */
1389 static void limit_left_right(struct commit_list *list, struct rev_info *revs)
1391 struct commit_list *p;
1393 for (p = list; p; p = p->next) {
1394 struct commit *commit = p->item;
1396 if (revs->right_only) {
1397 if (commit->object.flags & SYMMETRIC_LEFT)
1398 commit->object.flags |= SHOWN;
1399 } else /* revs->left_only is set */
1400 if (!(commit->object.flags & SYMMETRIC_LEFT))
1401 commit->object.flags |= SHOWN;
1405 static int limit_list(struct rev_info *revs)
1407 int slop = SLOP;
1408 timestamp_t date = TIME_MAX;
1409 struct commit_list *original_list = revs->commits;
1410 struct commit_list *newlist = NULL;
1411 struct commit_list **p = &newlist;
1412 struct commit_list *bottom = NULL;
1413 struct commit *interesting_cache = NULL;
1415 if (revs->ancestry_path) {
1416 bottom = collect_bottom_commits(original_list);
1417 if (!bottom)
1418 die("--ancestry-path given but there are no bottom commits");
1421 while (original_list) {
1422 struct commit *commit = pop_commit(&original_list);
1423 struct object *obj = &commit->object;
1424 show_early_output_fn_t show;
1426 if (commit == interesting_cache)
1427 interesting_cache = NULL;
1429 if (revs->max_age != -1 && (commit->date < revs->max_age))
1430 obj->flags |= UNINTERESTING;
1431 if (process_parents(revs, commit, &original_list, NULL) < 0)
1432 return -1;
1433 if (obj->flags & UNINTERESTING) {
1434 mark_parents_uninteresting(revs, commit);
1435 slop = still_interesting(original_list, date, slop, &interesting_cache);
1436 if (slop)
1437 continue;
1438 break;
1440 if (revs->min_age != -1 && (commit->date > revs->min_age) &&
1441 !revs->line_level_traverse)
1442 continue;
1443 if (revs->max_age_as_filter != -1 &&
1444 (commit->date < revs->max_age_as_filter) && !revs->line_level_traverse)
1445 continue;
1446 date = commit->date;
1447 p = &commit_list_insert(commit, p)->next;
1449 show = show_early_output;
1450 if (!show)
1451 continue;
1453 show(revs, newlist);
1454 show_early_output = NULL;
1456 if (revs->cherry_pick || revs->cherry_mark)
1457 cherry_pick_list(newlist, revs);
1459 if (revs->left_only || revs->right_only)
1460 limit_left_right(newlist, revs);
1462 if (bottom) {
1463 limit_to_ancestry(bottom, newlist);
1464 free_commit_list(bottom);
1468 * Check if any commits have become TREESAME by some of their parents
1469 * becoming UNINTERESTING.
1471 if (limiting_can_increase_treesame(revs)) {
1472 struct commit_list *list = NULL;
1473 for (list = newlist; list; list = list->next) {
1474 struct commit *c = list->item;
1475 if (c->object.flags & (UNINTERESTING | TREESAME))
1476 continue;
1477 update_treesame(revs, c);
1481 free_commit_list(original_list);
1482 revs->commits = newlist;
1483 return 0;
1487 * Add an entry to refs->cmdline with the specified information.
1488 * *name is copied.
1490 static void add_rev_cmdline(struct rev_info *revs,
1491 struct object *item,
1492 const char *name,
1493 int whence,
1494 unsigned flags)
1496 struct rev_cmdline_info *info = &revs->cmdline;
1497 unsigned int nr = info->nr;
1499 ALLOC_GROW(info->rev, nr + 1, info->alloc);
1500 info->rev[nr].item = item;
1501 info->rev[nr].name = xstrdup(name);
1502 info->rev[nr].whence = whence;
1503 info->rev[nr].flags = flags;
1504 info->nr++;
1507 static void add_rev_cmdline_list(struct rev_info *revs,
1508 struct commit_list *commit_list,
1509 int whence,
1510 unsigned flags)
1512 while (commit_list) {
1513 struct object *object = &commit_list->item->object;
1514 add_rev_cmdline(revs, object, oid_to_hex(&object->oid),
1515 whence, flags);
1516 commit_list = commit_list->next;
1520 struct all_refs_cb {
1521 int all_flags;
1522 int warned_bad_reflog;
1523 struct rev_info *all_revs;
1524 const char *name_for_errormsg;
1525 struct worktree *wt;
1528 int ref_excluded(struct string_list *ref_excludes, const char *path)
1530 struct string_list_item *item;
1532 if (!ref_excludes)
1533 return 0;
1534 for_each_string_list_item(item, ref_excludes) {
1535 if (!wildmatch(item->string, path, 0))
1536 return 1;
1538 return 0;
1541 static int handle_one_ref(const char *path, const struct object_id *oid,
1542 int flag, void *cb_data)
1544 struct all_refs_cb *cb = cb_data;
1545 struct object *object;
1547 if (ref_excluded(cb->all_revs->ref_excludes, path))
1548 return 0;
1550 object = get_reference(cb->all_revs, path, oid, cb->all_flags);
1551 add_rev_cmdline(cb->all_revs, object, path, REV_CMD_REF, cb->all_flags);
1552 add_pending_object(cb->all_revs, object, path);
1553 return 0;
1556 static void init_all_refs_cb(struct all_refs_cb *cb, struct rev_info *revs,
1557 unsigned flags)
1559 cb->all_revs = revs;
1560 cb->all_flags = flags;
1561 revs->rev_input_given = 1;
1562 cb->wt = NULL;
1565 void clear_ref_exclusion(struct string_list **ref_excludes_p)
1567 if (*ref_excludes_p) {
1568 string_list_clear(*ref_excludes_p, 0);
1569 free(*ref_excludes_p);
1571 *ref_excludes_p = NULL;
1574 void add_ref_exclusion(struct string_list **ref_excludes_p, const char *exclude)
1576 if (!*ref_excludes_p) {
1577 CALLOC_ARRAY(*ref_excludes_p, 1);
1578 (*ref_excludes_p)->strdup_strings = 1;
1580 string_list_append(*ref_excludes_p, exclude);
1583 static void handle_refs(struct ref_store *refs,
1584 struct rev_info *revs, unsigned flags,
1585 int (*for_each)(struct ref_store *, each_ref_fn, void *))
1587 struct all_refs_cb cb;
1589 if (!refs) {
1590 /* this could happen with uninitialized submodules */
1591 return;
1594 init_all_refs_cb(&cb, revs, flags);
1595 for_each(refs, handle_one_ref, &cb);
1598 static void handle_one_reflog_commit(struct object_id *oid, void *cb_data)
1600 struct all_refs_cb *cb = cb_data;
1601 if (!is_null_oid(oid)) {
1602 struct object *o = parse_object(cb->all_revs->repo, oid);
1603 if (o) {
1604 o->flags |= cb->all_flags;
1605 /* ??? CMDLINEFLAGS ??? */
1606 add_pending_object(cb->all_revs, o, "");
1608 else if (!cb->warned_bad_reflog) {
1609 warning("reflog of '%s' references pruned commits",
1610 cb->name_for_errormsg);
1611 cb->warned_bad_reflog = 1;
1616 static int handle_one_reflog_ent(struct object_id *ooid, struct object_id *noid,
1617 const char *email, timestamp_t timestamp, int tz,
1618 const char *message, void *cb_data)
1620 handle_one_reflog_commit(ooid, cb_data);
1621 handle_one_reflog_commit(noid, cb_data);
1622 return 0;
1625 static int handle_one_reflog(const char *refname_in_wt,
1626 const struct object_id *oid,
1627 int flag, void *cb_data)
1629 struct all_refs_cb *cb = cb_data;
1630 struct strbuf refname = STRBUF_INIT;
1632 cb->warned_bad_reflog = 0;
1633 strbuf_worktree_ref(cb->wt, &refname, refname_in_wt);
1634 cb->name_for_errormsg = refname.buf;
1635 refs_for_each_reflog_ent(get_main_ref_store(the_repository),
1636 refname.buf,
1637 handle_one_reflog_ent, cb_data);
1638 strbuf_release(&refname);
1639 return 0;
1642 static void add_other_reflogs_to_pending(struct all_refs_cb *cb)
1644 struct worktree **worktrees, **p;
1646 worktrees = get_worktrees();
1647 for (p = worktrees; *p; p++) {
1648 struct worktree *wt = *p;
1650 if (wt->is_current)
1651 continue;
1653 cb->wt = wt;
1654 refs_for_each_reflog(get_worktree_ref_store(wt),
1655 handle_one_reflog,
1656 cb);
1658 free_worktrees(worktrees);
1661 void add_reflogs_to_pending(struct rev_info *revs, unsigned flags)
1663 struct all_refs_cb cb;
1665 cb.all_revs = revs;
1666 cb.all_flags = flags;
1667 cb.wt = NULL;
1668 for_each_reflog(handle_one_reflog, &cb);
1670 if (!revs->single_worktree)
1671 add_other_reflogs_to_pending(&cb);
1674 static void add_cache_tree(struct cache_tree *it, struct rev_info *revs,
1675 struct strbuf *path, unsigned int flags)
1677 size_t baselen = path->len;
1678 int i;
1680 if (it->entry_count >= 0) {
1681 struct tree *tree = lookup_tree(revs->repo, &it->oid);
1682 tree->object.flags |= flags;
1683 add_pending_object_with_path(revs, &tree->object, "",
1684 040000, path->buf);
1687 for (i = 0; i < it->subtree_nr; i++) {
1688 struct cache_tree_sub *sub = it->down[i];
1689 strbuf_addf(path, "%s%s", baselen ? "/" : "", sub->name);
1690 add_cache_tree(sub->cache_tree, revs, path, flags);
1691 strbuf_setlen(path, baselen);
1696 static void do_add_index_objects_to_pending(struct rev_info *revs,
1697 struct index_state *istate,
1698 unsigned int flags)
1700 int i;
1702 /* TODO: audit for interaction with sparse-index. */
1703 ensure_full_index(istate);
1704 for (i = 0; i < istate->cache_nr; i++) {
1705 struct cache_entry *ce = istate->cache[i];
1706 struct blob *blob;
1708 if (S_ISGITLINK(ce->ce_mode))
1709 continue;
1711 blob = lookup_blob(revs->repo, &ce->oid);
1712 if (!blob)
1713 die("unable to add index blob to traversal");
1714 blob->object.flags |= flags;
1715 add_pending_object_with_path(revs, &blob->object, "",
1716 ce->ce_mode, ce->name);
1719 if (istate->cache_tree) {
1720 struct strbuf path = STRBUF_INIT;
1721 add_cache_tree(istate->cache_tree, revs, &path, flags);
1722 strbuf_release(&path);
1726 void add_index_objects_to_pending(struct rev_info *revs, unsigned int flags)
1728 struct worktree **worktrees, **p;
1730 repo_read_index(revs->repo);
1731 do_add_index_objects_to_pending(revs, revs->repo->index, flags);
1733 if (revs->single_worktree)
1734 return;
1736 worktrees = get_worktrees();
1737 for (p = worktrees; *p; p++) {
1738 struct worktree *wt = *p;
1739 struct index_state istate = { NULL };
1741 if (wt->is_current)
1742 continue; /* current index already taken care of */
1744 if (read_index_from(&istate,
1745 worktree_git_path(wt, "index"),
1746 get_worktree_git_dir(wt)) > 0)
1747 do_add_index_objects_to_pending(revs, &istate, flags);
1748 discard_index(&istate);
1750 free_worktrees(worktrees);
1753 struct add_alternate_refs_data {
1754 struct rev_info *revs;
1755 unsigned int flags;
1758 static void add_one_alternate_ref(const struct object_id *oid,
1759 void *vdata)
1761 const char *name = ".alternate";
1762 struct add_alternate_refs_data *data = vdata;
1763 struct object *obj;
1765 obj = get_reference(data->revs, name, oid, data->flags);
1766 add_rev_cmdline(data->revs, obj, name, REV_CMD_REV, data->flags);
1767 add_pending_object(data->revs, obj, name);
1770 static void add_alternate_refs_to_pending(struct rev_info *revs,
1771 unsigned int flags)
1773 struct add_alternate_refs_data data;
1774 data.revs = revs;
1775 data.flags = flags;
1776 for_each_alternate_ref(add_one_alternate_ref, &data);
1779 static int add_parents_only(struct rev_info *revs, const char *arg_, int flags,
1780 int exclude_parent)
1782 struct object_id oid;
1783 struct object *it;
1784 struct commit *commit;
1785 struct commit_list *parents;
1786 int parent_number;
1787 const char *arg = arg_;
1789 if (*arg == '^') {
1790 flags ^= UNINTERESTING | BOTTOM;
1791 arg++;
1793 if (get_oid_committish(arg, &oid))
1794 return 0;
1795 while (1) {
1796 it = get_reference(revs, arg, &oid, 0);
1797 if (!it && revs->ignore_missing)
1798 return 0;
1799 if (it->type != OBJ_TAG)
1800 break;
1801 if (!((struct tag*)it)->tagged)
1802 return 0;
1803 oidcpy(&oid, &((struct tag*)it)->tagged->oid);
1805 if (it->type != OBJ_COMMIT)
1806 return 0;
1807 commit = (struct commit *)it;
1808 if (exclude_parent &&
1809 exclude_parent > commit_list_count(commit->parents))
1810 return 0;
1811 for (parents = commit->parents, parent_number = 1;
1812 parents;
1813 parents = parents->next, parent_number++) {
1814 if (exclude_parent && parent_number != exclude_parent)
1815 continue;
1817 it = &parents->item->object;
1818 it->flags |= flags;
1819 add_rev_cmdline(revs, it, arg_, REV_CMD_PARENTS_ONLY, flags);
1820 add_pending_object(revs, it, arg);
1822 return 1;
1825 void repo_init_revisions(struct repository *r,
1826 struct rev_info *revs,
1827 const char *prefix)
1829 memset(revs, 0, sizeof(*revs));
1831 revs->repo = r;
1832 revs->abbrev = DEFAULT_ABBREV;
1833 revs->simplify_history = 1;
1834 revs->pruning.repo = r;
1835 revs->pruning.flags.recursive = 1;
1836 revs->pruning.flags.quick = 1;
1837 revs->pruning.add_remove = file_add_remove;
1838 revs->pruning.change = file_change;
1839 revs->pruning.change_fn_data = revs;
1840 revs->sort_order = REV_SORT_IN_GRAPH_ORDER;
1841 revs->dense = 1;
1842 revs->prefix = prefix;
1843 revs->max_age = -1;
1844 revs->max_age_as_filter = -1;
1845 revs->min_age = -1;
1846 revs->skip_count = -1;
1847 revs->max_count = -1;
1848 revs->max_parents = -1;
1849 revs->expand_tabs_in_log = -1;
1851 revs->commit_format = CMIT_FMT_DEFAULT;
1852 revs->expand_tabs_in_log_default = 8;
1854 grep_init(&revs->grep_filter, revs->repo);
1855 revs->grep_filter.status_only = 1;
1857 repo_diff_setup(revs->repo, &revs->diffopt);
1858 if (prefix && !revs->diffopt.prefix) {
1859 revs->diffopt.prefix = prefix;
1860 revs->diffopt.prefix_length = strlen(prefix);
1863 init_display_notes(&revs->notes_opt);
1866 static void add_pending_commit_list(struct rev_info *revs,
1867 struct commit_list *commit_list,
1868 unsigned int flags)
1870 while (commit_list) {
1871 struct object *object = &commit_list->item->object;
1872 object->flags |= flags;
1873 add_pending_object(revs, object, oid_to_hex(&object->oid));
1874 commit_list = commit_list->next;
1878 static void prepare_show_merge(struct rev_info *revs)
1880 struct commit_list *bases;
1881 struct commit *head, *other;
1882 struct object_id oid;
1883 const char **prune = NULL;
1884 int i, prune_num = 1; /* counting terminating NULL */
1885 struct index_state *istate = revs->repo->index;
1887 if (get_oid("HEAD", &oid))
1888 die("--merge without HEAD?");
1889 head = lookup_commit_or_die(&oid, "HEAD");
1890 if (get_oid("MERGE_HEAD", &oid))
1891 die("--merge without MERGE_HEAD?");
1892 other = lookup_commit_or_die(&oid, "MERGE_HEAD");
1893 add_pending_object(revs, &head->object, "HEAD");
1894 add_pending_object(revs, &other->object, "MERGE_HEAD");
1895 bases = get_merge_bases(head, other);
1896 add_rev_cmdline_list(revs, bases, REV_CMD_MERGE_BASE, UNINTERESTING | BOTTOM);
1897 add_pending_commit_list(revs, bases, UNINTERESTING | BOTTOM);
1898 free_commit_list(bases);
1899 head->object.flags |= SYMMETRIC_LEFT;
1901 if (!istate->cache_nr)
1902 repo_read_index(revs->repo);
1903 for (i = 0; i < istate->cache_nr; i++) {
1904 const struct cache_entry *ce = istate->cache[i];
1905 if (!ce_stage(ce))
1906 continue;
1907 if (ce_path_match(istate, ce, &revs->prune_data, NULL)) {
1908 prune_num++;
1909 REALLOC_ARRAY(prune, prune_num);
1910 prune[prune_num-2] = ce->name;
1911 prune[prune_num-1] = NULL;
1913 while ((i+1 < istate->cache_nr) &&
1914 ce_same_name(ce, istate->cache[i+1]))
1915 i++;
1917 clear_pathspec(&revs->prune_data);
1918 parse_pathspec(&revs->prune_data, PATHSPEC_ALL_MAGIC & ~PATHSPEC_LITERAL,
1919 PATHSPEC_PREFER_FULL | PATHSPEC_LITERAL_PATH, "", prune);
1920 revs->limited = 1;
1923 static int dotdot_missing(const char *arg, char *dotdot,
1924 struct rev_info *revs, int symmetric)
1926 if (revs->ignore_missing)
1927 return 0;
1928 /* de-munge so we report the full argument */
1929 *dotdot = '.';
1930 die(symmetric
1931 ? "Invalid symmetric difference expression %s"
1932 : "Invalid revision range %s", arg);
1935 static int handle_dotdot_1(const char *arg, char *dotdot,
1936 struct rev_info *revs, int flags,
1937 int cant_be_filename,
1938 struct object_context *a_oc,
1939 struct object_context *b_oc)
1941 const char *a_name, *b_name;
1942 struct object_id a_oid, b_oid;
1943 struct object *a_obj, *b_obj;
1944 unsigned int a_flags, b_flags;
1945 int symmetric = 0;
1946 unsigned int flags_exclude = flags ^ (UNINTERESTING | BOTTOM);
1947 unsigned int oc_flags = GET_OID_COMMITTISH | GET_OID_RECORD_PATH;
1949 a_name = arg;
1950 if (!*a_name)
1951 a_name = "HEAD";
1953 b_name = dotdot + 2;
1954 if (*b_name == '.') {
1955 symmetric = 1;
1956 b_name++;
1958 if (!*b_name)
1959 b_name = "HEAD";
1961 if (get_oid_with_context(revs->repo, a_name, oc_flags, &a_oid, a_oc) ||
1962 get_oid_with_context(revs->repo, b_name, oc_flags, &b_oid, b_oc))
1963 return -1;
1965 if (!cant_be_filename) {
1966 *dotdot = '.';
1967 verify_non_filename(revs->prefix, arg);
1968 *dotdot = '\0';
1971 a_obj = parse_object(revs->repo, &a_oid);
1972 b_obj = parse_object(revs->repo, &b_oid);
1973 if (!a_obj || !b_obj)
1974 return dotdot_missing(arg, dotdot, revs, symmetric);
1976 if (!symmetric) {
1977 /* just A..B */
1978 b_flags = flags;
1979 a_flags = flags_exclude;
1980 } else {
1981 /* A...B -- find merge bases between the two */
1982 struct commit *a, *b;
1983 struct commit_list *exclude;
1985 a = lookup_commit_reference(revs->repo, &a_obj->oid);
1986 b = lookup_commit_reference(revs->repo, &b_obj->oid);
1987 if (!a || !b)
1988 return dotdot_missing(arg, dotdot, revs, symmetric);
1990 exclude = get_merge_bases(a, b);
1991 add_rev_cmdline_list(revs, exclude, REV_CMD_MERGE_BASE,
1992 flags_exclude);
1993 add_pending_commit_list(revs, exclude, flags_exclude);
1994 free_commit_list(exclude);
1996 b_flags = flags;
1997 a_flags = flags | SYMMETRIC_LEFT;
2000 a_obj->flags |= a_flags;
2001 b_obj->flags |= b_flags;
2002 add_rev_cmdline(revs, a_obj, a_name, REV_CMD_LEFT, a_flags);
2003 add_rev_cmdline(revs, b_obj, b_name, REV_CMD_RIGHT, b_flags);
2004 add_pending_object_with_path(revs, a_obj, a_name, a_oc->mode, a_oc->path);
2005 add_pending_object_with_path(revs, b_obj, b_name, b_oc->mode, b_oc->path);
2006 return 0;
2009 static int handle_dotdot(const char *arg,
2010 struct rev_info *revs, int flags,
2011 int cant_be_filename)
2013 struct object_context a_oc, b_oc;
2014 char *dotdot = strstr(arg, "..");
2015 int ret;
2017 if (!dotdot)
2018 return -1;
2020 memset(&a_oc, 0, sizeof(a_oc));
2021 memset(&b_oc, 0, sizeof(b_oc));
2023 *dotdot = '\0';
2024 ret = handle_dotdot_1(arg, dotdot, revs, flags, cant_be_filename,
2025 &a_oc, &b_oc);
2026 *dotdot = '.';
2028 free(a_oc.path);
2029 free(b_oc.path);
2031 return ret;
2034 static int handle_revision_arg_1(const char *arg_, struct rev_info *revs, int flags, unsigned revarg_opt)
2036 struct object_context oc;
2037 char *mark;
2038 struct object *object;
2039 struct object_id oid;
2040 int local_flags;
2041 const char *arg = arg_;
2042 int cant_be_filename = revarg_opt & REVARG_CANNOT_BE_FILENAME;
2043 unsigned get_sha1_flags = GET_OID_RECORD_PATH;
2045 flags = flags & UNINTERESTING ? flags | BOTTOM : flags & ~BOTTOM;
2047 if (!cant_be_filename && !strcmp(arg, "..")) {
2049 * Just ".."? That is not a range but the
2050 * pathspec for the parent directory.
2052 return -1;
2055 if (!handle_dotdot(arg, revs, flags, revarg_opt))
2056 return 0;
2058 mark = strstr(arg, "^@");
2059 if (mark && !mark[2]) {
2060 *mark = 0;
2061 if (add_parents_only(revs, arg, flags, 0))
2062 return 0;
2063 *mark = '^';
2065 mark = strstr(arg, "^!");
2066 if (mark && !mark[2]) {
2067 *mark = 0;
2068 if (!add_parents_only(revs, arg, flags ^ (UNINTERESTING | BOTTOM), 0))
2069 *mark = '^';
2071 mark = strstr(arg, "^-");
2072 if (mark) {
2073 int exclude_parent = 1;
2075 if (mark[2]) {
2076 char *end;
2077 exclude_parent = strtoul(mark + 2, &end, 10);
2078 if (*end != '\0' || !exclude_parent)
2079 return -1;
2082 *mark = 0;
2083 if (!add_parents_only(revs, arg, flags ^ (UNINTERESTING | BOTTOM), exclude_parent))
2084 *mark = '^';
2087 local_flags = 0;
2088 if (*arg == '^') {
2089 local_flags = UNINTERESTING | BOTTOM;
2090 arg++;
2093 if (revarg_opt & REVARG_COMMITTISH)
2094 get_sha1_flags |= GET_OID_COMMITTISH;
2096 if (get_oid_with_context(revs->repo, arg, get_sha1_flags, &oid, &oc))
2097 return revs->ignore_missing ? 0 : -1;
2098 if (!cant_be_filename)
2099 verify_non_filename(revs->prefix, arg);
2100 object = get_reference(revs, arg, &oid, flags ^ local_flags);
2101 if (!object)
2102 return revs->ignore_missing ? 0 : -1;
2103 add_rev_cmdline(revs, object, arg_, REV_CMD_REV, flags ^ local_flags);
2104 add_pending_object_with_path(revs, object, arg, oc.mode, oc.path);
2105 free(oc.path);
2106 return 0;
2109 int handle_revision_arg(const char *arg, struct rev_info *revs, int flags, unsigned revarg_opt)
2111 int ret = handle_revision_arg_1(arg, revs, flags, revarg_opt);
2112 if (!ret)
2113 revs->rev_input_given = 1;
2114 return ret;
2117 static void read_pathspec_from_stdin(struct strbuf *sb,
2118 struct strvec *prune)
2120 while (strbuf_getline(sb, stdin) != EOF)
2121 strvec_push(prune, sb->buf);
2124 static void read_revisions_from_stdin(struct rev_info *revs,
2125 struct strvec *prune)
2127 struct strbuf sb;
2128 int seen_dashdash = 0;
2129 int save_warning;
2131 save_warning = warn_on_object_refname_ambiguity;
2132 warn_on_object_refname_ambiguity = 0;
2134 strbuf_init(&sb, 1000);
2135 while (strbuf_getline(&sb, stdin) != EOF) {
2136 int len = sb.len;
2137 if (!len)
2138 break;
2139 if (sb.buf[0] == '-') {
2140 if (len == 2 && sb.buf[1] == '-') {
2141 seen_dashdash = 1;
2142 break;
2144 die("options not supported in --stdin mode");
2146 if (handle_revision_arg(sb.buf, revs, 0,
2147 REVARG_CANNOT_BE_FILENAME))
2148 die("bad revision '%s'", sb.buf);
2150 if (seen_dashdash)
2151 read_pathspec_from_stdin(&sb, prune);
2153 strbuf_release(&sb);
2154 warn_on_object_refname_ambiguity = save_warning;
2157 static void add_grep(struct rev_info *revs, const char *ptn, enum grep_pat_token what)
2159 append_grep_pattern(&revs->grep_filter, ptn, "command line", 0, what);
2162 static void add_header_grep(struct rev_info *revs, enum grep_header_field field, const char *pattern)
2164 append_header_grep_pattern(&revs->grep_filter, field, pattern);
2167 static void add_message_grep(struct rev_info *revs, const char *pattern)
2169 add_grep(revs, pattern, GREP_PATTERN_BODY);
2172 static int handle_revision_opt(struct rev_info *revs, int argc, const char **argv,
2173 int *unkc, const char **unkv,
2174 const struct setup_revision_opt* opt)
2176 const char *arg = argv[0];
2177 const char *optarg;
2178 int argcount;
2179 const unsigned hexsz = the_hash_algo->hexsz;
2181 /* pseudo revision arguments */
2182 if (!strcmp(arg, "--all") || !strcmp(arg, "--branches") ||
2183 !strcmp(arg, "--tags") || !strcmp(arg, "--remotes") ||
2184 !strcmp(arg, "--reflog") || !strcmp(arg, "--not") ||
2185 !strcmp(arg, "--no-walk") || !strcmp(arg, "--do-walk") ||
2186 !strcmp(arg, "--bisect") || starts_with(arg, "--glob=") ||
2187 !strcmp(arg, "--indexed-objects") ||
2188 !strcmp(arg, "--alternate-refs") ||
2189 starts_with(arg, "--exclude=") ||
2190 starts_with(arg, "--branches=") || starts_with(arg, "--tags=") ||
2191 starts_with(arg, "--remotes=") || starts_with(arg, "--no-walk="))
2193 unkv[(*unkc)++] = arg;
2194 return 1;
2197 if ((argcount = parse_long_opt("max-count", argv, &optarg))) {
2198 revs->max_count = atoi(optarg);
2199 revs->no_walk = 0;
2200 return argcount;
2201 } else if ((argcount = parse_long_opt("skip", argv, &optarg))) {
2202 revs->skip_count = atoi(optarg);
2203 return argcount;
2204 } else if ((*arg == '-') && isdigit(arg[1])) {
2205 /* accept -<digit>, like traditional "head" */
2206 if (strtol_i(arg + 1, 10, &revs->max_count) < 0 ||
2207 revs->max_count < 0)
2208 die("'%s': not a non-negative integer", arg + 1);
2209 revs->no_walk = 0;
2210 } else if (!strcmp(arg, "-n")) {
2211 if (argc <= 1)
2212 return error("-n requires an argument");
2213 revs->max_count = atoi(argv[1]);
2214 revs->no_walk = 0;
2215 return 2;
2216 } else if (skip_prefix(arg, "-n", &optarg)) {
2217 revs->max_count = atoi(optarg);
2218 revs->no_walk = 0;
2219 } else if ((argcount = parse_long_opt("max-age", argv, &optarg))) {
2220 revs->max_age = atoi(optarg);
2221 return argcount;
2222 } else if ((argcount = parse_long_opt("since", argv, &optarg))) {
2223 revs->max_age = approxidate(optarg);
2224 return argcount;
2225 } else if ((argcount = parse_long_opt("since-as-filter", argv, &optarg))) {
2226 revs->max_age_as_filter = approxidate(optarg);
2227 return argcount;
2228 } else if ((argcount = parse_long_opt("after", argv, &optarg))) {
2229 revs->max_age = approxidate(optarg);
2230 return argcount;
2231 } else if ((argcount = parse_long_opt("min-age", argv, &optarg))) {
2232 revs->min_age = atoi(optarg);
2233 return argcount;
2234 } else if ((argcount = parse_long_opt("before", argv, &optarg))) {
2235 revs->min_age = approxidate(optarg);
2236 return argcount;
2237 } else if ((argcount = parse_long_opt("until", argv, &optarg))) {
2238 revs->min_age = approxidate(optarg);
2239 return argcount;
2240 } else if (!strcmp(arg, "--first-parent")) {
2241 revs->first_parent_only = 1;
2242 } else if (!strcmp(arg, "--exclude-first-parent-only")) {
2243 revs->exclude_first_parent_only = 1;
2244 } else if (!strcmp(arg, "--ancestry-path")) {
2245 revs->ancestry_path = 1;
2246 revs->simplify_history = 0;
2247 revs->limited = 1;
2248 } else if (!strcmp(arg, "-g") || !strcmp(arg, "--walk-reflogs")) {
2249 init_reflog_walk(&revs->reflog_info);
2250 } else if (!strcmp(arg, "--default")) {
2251 if (argc <= 1)
2252 return error("bad --default argument");
2253 revs->def = argv[1];
2254 return 2;
2255 } else if (!strcmp(arg, "--merge")) {
2256 revs->show_merge = 1;
2257 } else if (!strcmp(arg, "--topo-order")) {
2258 revs->sort_order = REV_SORT_IN_GRAPH_ORDER;
2259 revs->topo_order = 1;
2260 } else if (!strcmp(arg, "--simplify-merges")) {
2261 revs->simplify_merges = 1;
2262 revs->topo_order = 1;
2263 revs->rewrite_parents = 1;
2264 revs->simplify_history = 0;
2265 revs->limited = 1;
2266 } else if (!strcmp(arg, "--simplify-by-decoration")) {
2267 revs->simplify_merges = 1;
2268 revs->topo_order = 1;
2269 revs->rewrite_parents = 1;
2270 revs->simplify_history = 0;
2271 revs->simplify_by_decoration = 1;
2272 revs->limited = 1;
2273 revs->prune = 1;
2274 } else if (!strcmp(arg, "--date-order")) {
2275 revs->sort_order = REV_SORT_BY_COMMIT_DATE;
2276 revs->topo_order = 1;
2277 } else if (!strcmp(arg, "--author-date-order")) {
2278 revs->sort_order = REV_SORT_BY_AUTHOR_DATE;
2279 revs->topo_order = 1;
2280 } else if (!strcmp(arg, "--early-output")) {
2281 revs->early_output = 100;
2282 revs->topo_order = 1;
2283 } else if (skip_prefix(arg, "--early-output=", &optarg)) {
2284 if (strtoul_ui(optarg, 10, &revs->early_output) < 0)
2285 die("'%s': not a non-negative integer", optarg);
2286 revs->topo_order = 1;
2287 } else if (!strcmp(arg, "--parents")) {
2288 revs->rewrite_parents = 1;
2289 revs->print_parents = 1;
2290 } else if (!strcmp(arg, "--dense")) {
2291 revs->dense = 1;
2292 } else if (!strcmp(arg, "--sparse")) {
2293 revs->dense = 0;
2294 } else if (!strcmp(arg, "--in-commit-order")) {
2295 revs->tree_blobs_in_commit_order = 1;
2296 } else if (!strcmp(arg, "--remove-empty")) {
2297 revs->remove_empty_trees = 1;
2298 } else if (!strcmp(arg, "--merges")) {
2299 revs->min_parents = 2;
2300 } else if (!strcmp(arg, "--no-merges")) {
2301 revs->max_parents = 1;
2302 } else if (skip_prefix(arg, "--min-parents=", &optarg)) {
2303 revs->min_parents = atoi(optarg);
2304 } else if (!strcmp(arg, "--no-min-parents")) {
2305 revs->min_parents = 0;
2306 } else if (skip_prefix(arg, "--max-parents=", &optarg)) {
2307 revs->max_parents = atoi(optarg);
2308 } else if (!strcmp(arg, "--no-max-parents")) {
2309 revs->max_parents = -1;
2310 } else if (!strcmp(arg, "--boundary")) {
2311 revs->boundary = 1;
2312 } else if (!strcmp(arg, "--left-right")) {
2313 revs->left_right = 1;
2314 } else if (!strcmp(arg, "--left-only")) {
2315 if (revs->right_only)
2316 die("--left-only is incompatible with --right-only"
2317 " or --cherry");
2318 revs->left_only = 1;
2319 } else if (!strcmp(arg, "--right-only")) {
2320 if (revs->left_only)
2321 die(_("options '%s' and '%s' cannot be used together"), "--right-only", "--left-only");
2322 revs->right_only = 1;
2323 } else if (!strcmp(arg, "--cherry")) {
2324 if (revs->left_only)
2325 die(_("options '%s' and '%s' cannot be used together"), "--cherry", "--left-only");
2326 revs->cherry_mark = 1;
2327 revs->right_only = 1;
2328 revs->max_parents = 1;
2329 revs->limited = 1;
2330 } else if (!strcmp(arg, "--count")) {
2331 revs->count = 1;
2332 } else if (!strcmp(arg, "--cherry-mark")) {
2333 if (revs->cherry_pick)
2334 die(_("options '%s' and '%s' cannot be used together"), "--cherry-mark", "--cherry-pick");
2335 revs->cherry_mark = 1;
2336 revs->limited = 1; /* needs limit_list() */
2337 } else if (!strcmp(arg, "--cherry-pick")) {
2338 if (revs->cherry_mark)
2339 die(_("options '%s' and '%s' cannot be used together"), "--cherry-pick", "--cherry-mark");
2340 revs->cherry_pick = 1;
2341 revs->limited = 1;
2342 } else if (!strcmp(arg, "--objects")) {
2343 revs->tag_objects = 1;
2344 revs->tree_objects = 1;
2345 revs->blob_objects = 1;
2346 } else if (!strcmp(arg, "--objects-edge")) {
2347 revs->tag_objects = 1;
2348 revs->tree_objects = 1;
2349 revs->blob_objects = 1;
2350 revs->edge_hint = 1;
2351 } else if (!strcmp(arg, "--objects-edge-aggressive")) {
2352 revs->tag_objects = 1;
2353 revs->tree_objects = 1;
2354 revs->blob_objects = 1;
2355 revs->edge_hint = 1;
2356 revs->edge_hint_aggressive = 1;
2357 } else if (!strcmp(arg, "--verify-objects")) {
2358 revs->tag_objects = 1;
2359 revs->tree_objects = 1;
2360 revs->blob_objects = 1;
2361 revs->verify_objects = 1;
2362 } else if (!strcmp(arg, "--unpacked")) {
2363 revs->unpacked = 1;
2364 } else if (starts_with(arg, "--unpacked=")) {
2365 die(_("--unpacked=<packfile> no longer supported"));
2366 } else if (!strcmp(arg, "--no-kept-objects")) {
2367 revs->no_kept_objects = 1;
2368 revs->keep_pack_cache_flags |= IN_CORE_KEEP_PACKS;
2369 revs->keep_pack_cache_flags |= ON_DISK_KEEP_PACKS;
2370 } else if (skip_prefix(arg, "--no-kept-objects=", &optarg)) {
2371 revs->no_kept_objects = 1;
2372 if (!strcmp(optarg, "in-core"))
2373 revs->keep_pack_cache_flags |= IN_CORE_KEEP_PACKS;
2374 if (!strcmp(optarg, "on-disk"))
2375 revs->keep_pack_cache_flags |= ON_DISK_KEEP_PACKS;
2376 } else if (!strcmp(arg, "-r")) {
2377 revs->diff = 1;
2378 revs->diffopt.flags.recursive = 1;
2379 } else if (!strcmp(arg, "-t")) {
2380 revs->diff = 1;
2381 revs->diffopt.flags.recursive = 1;
2382 revs->diffopt.flags.tree_in_recursive = 1;
2383 } else if ((argcount = diff_merges_parse_opts(revs, argv))) {
2384 return argcount;
2385 } else if (!strcmp(arg, "-v")) {
2386 revs->verbose_header = 1;
2387 } else if (!strcmp(arg, "--pretty")) {
2388 revs->verbose_header = 1;
2389 revs->pretty_given = 1;
2390 get_commit_format(NULL, revs);
2391 } else if (skip_prefix(arg, "--pretty=", &optarg) ||
2392 skip_prefix(arg, "--format=", &optarg)) {
2394 * Detached form ("--pretty X" as opposed to "--pretty=X")
2395 * not allowed, since the argument is optional.
2397 revs->verbose_header = 1;
2398 revs->pretty_given = 1;
2399 get_commit_format(optarg, revs);
2400 } else if (!strcmp(arg, "--expand-tabs")) {
2401 revs->expand_tabs_in_log = 8;
2402 } else if (!strcmp(arg, "--no-expand-tabs")) {
2403 revs->expand_tabs_in_log = 0;
2404 } else if (skip_prefix(arg, "--expand-tabs=", &arg)) {
2405 int val;
2406 if (strtol_i(arg, 10, &val) < 0 || val < 0)
2407 die("'%s': not a non-negative integer", arg);
2408 revs->expand_tabs_in_log = val;
2409 } else if (!strcmp(arg, "--show-notes") || !strcmp(arg, "--notes")) {
2410 enable_default_display_notes(&revs->notes_opt, &revs->show_notes);
2411 revs->show_notes_given = 1;
2412 } else if (!strcmp(arg, "--show-signature")) {
2413 revs->show_signature = 1;
2414 } else if (!strcmp(arg, "--no-show-signature")) {
2415 revs->show_signature = 0;
2416 } else if (!strcmp(arg, "--show-linear-break")) {
2417 revs->break_bar = " ..........";
2418 revs->track_linear = 1;
2419 revs->track_first_time = 1;
2420 } else if (skip_prefix(arg, "--show-linear-break=", &optarg)) {
2421 revs->break_bar = xstrdup(optarg);
2422 revs->track_linear = 1;
2423 revs->track_first_time = 1;
2424 } else if (skip_prefix(arg, "--show-notes=", &optarg) ||
2425 skip_prefix(arg, "--notes=", &optarg)) {
2426 if (starts_with(arg, "--show-notes=") &&
2427 revs->notes_opt.use_default_notes < 0)
2428 revs->notes_opt.use_default_notes = 1;
2429 enable_ref_display_notes(&revs->notes_opt, &revs->show_notes, optarg);
2430 revs->show_notes_given = 1;
2431 } else if (!strcmp(arg, "--no-notes")) {
2432 disable_display_notes(&revs->notes_opt, &revs->show_notes);
2433 revs->show_notes_given = 1;
2434 } else if (!strcmp(arg, "--standard-notes")) {
2435 revs->show_notes_given = 1;
2436 revs->notes_opt.use_default_notes = 1;
2437 } else if (!strcmp(arg, "--no-standard-notes")) {
2438 revs->notes_opt.use_default_notes = 0;
2439 } else if (!strcmp(arg, "--oneline")) {
2440 revs->verbose_header = 1;
2441 get_commit_format("oneline", revs);
2442 revs->pretty_given = 1;
2443 revs->abbrev_commit = 1;
2444 } else if (!strcmp(arg, "--graph")) {
2445 graph_clear(revs->graph);
2446 revs->graph = graph_init(revs);
2447 } else if (!strcmp(arg, "--no-graph")) {
2448 graph_clear(revs->graph);
2449 revs->graph = NULL;
2450 } else if (!strcmp(arg, "--encode-email-headers")) {
2451 revs->encode_email_headers = 1;
2452 } else if (!strcmp(arg, "--no-encode-email-headers")) {
2453 revs->encode_email_headers = 0;
2454 } else if (!strcmp(arg, "--root")) {
2455 revs->show_root_diff = 1;
2456 } else if (!strcmp(arg, "--no-commit-id")) {
2457 revs->no_commit_id = 1;
2458 } else if (!strcmp(arg, "--always")) {
2459 revs->always_show_header = 1;
2460 } else if (!strcmp(arg, "--no-abbrev")) {
2461 revs->abbrev = 0;
2462 } else if (!strcmp(arg, "--abbrev")) {
2463 revs->abbrev = DEFAULT_ABBREV;
2464 } else if (skip_prefix(arg, "--abbrev=", &optarg)) {
2465 revs->abbrev = strtoul(optarg, NULL, 10);
2466 if (revs->abbrev < MINIMUM_ABBREV)
2467 revs->abbrev = MINIMUM_ABBREV;
2468 else if (revs->abbrev > hexsz)
2469 revs->abbrev = hexsz;
2470 } else if (!strcmp(arg, "--abbrev-commit")) {
2471 revs->abbrev_commit = 1;
2472 revs->abbrev_commit_given = 1;
2473 } else if (!strcmp(arg, "--no-abbrev-commit")) {
2474 revs->abbrev_commit = 0;
2475 } else if (!strcmp(arg, "--full-diff")) {
2476 revs->diff = 1;
2477 revs->full_diff = 1;
2478 } else if (!strcmp(arg, "--show-pulls")) {
2479 revs->show_pulls = 1;
2480 } else if (!strcmp(arg, "--full-history")) {
2481 revs->simplify_history = 0;
2482 } else if (!strcmp(arg, "--relative-date")) {
2483 revs->date_mode.type = DATE_RELATIVE;
2484 revs->date_mode_explicit = 1;
2485 } else if ((argcount = parse_long_opt("date", argv, &optarg))) {
2486 parse_date_format(optarg, &revs->date_mode);
2487 revs->date_mode_explicit = 1;
2488 return argcount;
2489 } else if (!strcmp(arg, "--log-size")) {
2490 revs->show_log_size = 1;
2493 * Grepping the commit log
2495 else if ((argcount = parse_long_opt("author", argv, &optarg))) {
2496 add_header_grep(revs, GREP_HEADER_AUTHOR, optarg);
2497 return argcount;
2498 } else if ((argcount = parse_long_opt("committer", argv, &optarg))) {
2499 add_header_grep(revs, GREP_HEADER_COMMITTER, optarg);
2500 return argcount;
2501 } else if ((argcount = parse_long_opt("grep-reflog", argv, &optarg))) {
2502 add_header_grep(revs, GREP_HEADER_REFLOG, optarg);
2503 return argcount;
2504 } else if ((argcount = parse_long_opt("grep", argv, &optarg))) {
2505 add_message_grep(revs, optarg);
2506 return argcount;
2507 } else if (!strcmp(arg, "--basic-regexp")) {
2508 revs->grep_filter.pattern_type_option = GREP_PATTERN_TYPE_BRE;
2509 } else if (!strcmp(arg, "--extended-regexp") || !strcmp(arg, "-E")) {
2510 revs->grep_filter.pattern_type_option = GREP_PATTERN_TYPE_ERE;
2511 } else if (!strcmp(arg, "--regexp-ignore-case") || !strcmp(arg, "-i")) {
2512 revs->grep_filter.ignore_case = 1;
2513 revs->diffopt.pickaxe_opts |= DIFF_PICKAXE_IGNORE_CASE;
2514 } else if (!strcmp(arg, "--fixed-strings") || !strcmp(arg, "-F")) {
2515 revs->grep_filter.pattern_type_option = GREP_PATTERN_TYPE_FIXED;
2516 } else if (!strcmp(arg, "--perl-regexp") || !strcmp(arg, "-P")) {
2517 revs->grep_filter.pattern_type_option = GREP_PATTERN_TYPE_PCRE;
2518 } else if (!strcmp(arg, "--all-match")) {
2519 revs->grep_filter.all_match = 1;
2520 } else if (!strcmp(arg, "--invert-grep")) {
2521 revs->grep_filter.no_body_match = 1;
2522 } else if ((argcount = parse_long_opt("encoding", argv, &optarg))) {
2523 if (strcmp(optarg, "none"))
2524 git_log_output_encoding = xstrdup(optarg);
2525 else
2526 git_log_output_encoding = "";
2527 return argcount;
2528 } else if (!strcmp(arg, "--reverse")) {
2529 revs->reverse ^= 1;
2530 } else if (!strcmp(arg, "--children")) {
2531 revs->children.name = "children";
2532 revs->limited = 1;
2533 } else if (!strcmp(arg, "--ignore-missing")) {
2534 revs->ignore_missing = 1;
2535 } else if (opt && opt->allow_exclude_promisor_objects &&
2536 !strcmp(arg, "--exclude-promisor-objects")) {
2537 if (fetch_if_missing)
2538 BUG("exclude_promisor_objects can only be used when fetch_if_missing is 0");
2539 revs->exclude_promisor_objects = 1;
2540 } else {
2541 int opts = diff_opt_parse(&revs->diffopt, argv, argc, revs->prefix);
2542 if (!opts)
2543 unkv[(*unkc)++] = arg;
2544 return opts;
2547 return 1;
2550 void parse_revision_opt(struct rev_info *revs, struct parse_opt_ctx_t *ctx,
2551 const struct option *options,
2552 const char * const usagestr[])
2554 int n = handle_revision_opt(revs, ctx->argc, ctx->argv,
2555 &ctx->cpidx, ctx->out, NULL);
2556 if (n <= 0) {
2557 error("unknown option `%s'", ctx->argv[0]);
2558 usage_with_options(usagestr, options);
2560 ctx->argv += n;
2561 ctx->argc -= n;
2564 void revision_opts_finish(struct rev_info *revs)
2566 if (revs->graph && revs->track_linear)
2567 die(_("options '%s' and '%s' cannot be used together"), "--show-linear-break", "--graph");
2569 if (revs->graph) {
2570 revs->topo_order = 1;
2571 revs->rewrite_parents = 1;
2575 static int for_each_bisect_ref(struct ref_store *refs, each_ref_fn fn,
2576 void *cb_data, const char *term)
2578 struct strbuf bisect_refs = STRBUF_INIT;
2579 int status;
2580 strbuf_addf(&bisect_refs, "refs/bisect/%s", term);
2581 status = refs_for_each_fullref_in(refs, bisect_refs.buf, fn, cb_data);
2582 strbuf_release(&bisect_refs);
2583 return status;
2586 static int for_each_bad_bisect_ref(struct ref_store *refs, each_ref_fn fn, void *cb_data)
2588 return for_each_bisect_ref(refs, fn, cb_data, term_bad);
2591 static int for_each_good_bisect_ref(struct ref_store *refs, each_ref_fn fn, void *cb_data)
2593 return for_each_bisect_ref(refs, fn, cb_data, term_good);
2596 static int handle_revision_pseudo_opt(struct rev_info *revs,
2597 const char **argv, int *flags)
2599 const char *arg = argv[0];
2600 const char *optarg;
2601 struct ref_store *refs;
2602 int argcount;
2604 if (revs->repo != the_repository) {
2606 * We need some something like get_submodule_worktrees()
2607 * before we can go through all worktrees of a submodule,
2608 * .e.g with adding all HEADs from --all, which is not
2609 * supported right now, so stick to single worktree.
2611 if (!revs->single_worktree)
2612 BUG("--single-worktree cannot be used together with submodule");
2614 refs = get_main_ref_store(revs->repo);
2617 * NOTE!
2619 * Commands like "git shortlog" will not accept the options below
2620 * unless parse_revision_opt queues them (as opposed to erroring
2621 * out).
2623 * When implementing your new pseudo-option, remember to
2624 * register it in the list at the top of handle_revision_opt.
2626 if (!strcmp(arg, "--all")) {
2627 handle_refs(refs, revs, *flags, refs_for_each_ref);
2628 handle_refs(refs, revs, *flags, refs_head_ref);
2629 if (!revs->single_worktree) {
2630 struct all_refs_cb cb;
2632 init_all_refs_cb(&cb, revs, *flags);
2633 other_head_refs(handle_one_ref, &cb);
2635 clear_ref_exclusion(&revs->ref_excludes);
2636 } else if (!strcmp(arg, "--branches")) {
2637 handle_refs(refs, revs, *flags, refs_for_each_branch_ref);
2638 clear_ref_exclusion(&revs->ref_excludes);
2639 } else if (!strcmp(arg, "--bisect")) {
2640 read_bisect_terms(&term_bad, &term_good);
2641 handle_refs(refs, revs, *flags, for_each_bad_bisect_ref);
2642 handle_refs(refs, revs, *flags ^ (UNINTERESTING | BOTTOM),
2643 for_each_good_bisect_ref);
2644 revs->bisect = 1;
2645 } else if (!strcmp(arg, "--tags")) {
2646 handle_refs(refs, revs, *flags, refs_for_each_tag_ref);
2647 clear_ref_exclusion(&revs->ref_excludes);
2648 } else if (!strcmp(arg, "--remotes")) {
2649 handle_refs(refs, revs, *flags, refs_for_each_remote_ref);
2650 clear_ref_exclusion(&revs->ref_excludes);
2651 } else if ((argcount = parse_long_opt("glob", argv, &optarg))) {
2652 struct all_refs_cb cb;
2653 init_all_refs_cb(&cb, revs, *flags);
2654 for_each_glob_ref(handle_one_ref, optarg, &cb);
2655 clear_ref_exclusion(&revs->ref_excludes);
2656 return argcount;
2657 } else if ((argcount = parse_long_opt("exclude", argv, &optarg))) {
2658 add_ref_exclusion(&revs->ref_excludes, optarg);
2659 return argcount;
2660 } else if (skip_prefix(arg, "--branches=", &optarg)) {
2661 struct all_refs_cb cb;
2662 init_all_refs_cb(&cb, revs, *flags);
2663 for_each_glob_ref_in(handle_one_ref, optarg, "refs/heads/", &cb);
2664 clear_ref_exclusion(&revs->ref_excludes);
2665 } else if (skip_prefix(arg, "--tags=", &optarg)) {
2666 struct all_refs_cb cb;
2667 init_all_refs_cb(&cb, revs, *flags);
2668 for_each_glob_ref_in(handle_one_ref, optarg, "refs/tags/", &cb);
2669 clear_ref_exclusion(&revs->ref_excludes);
2670 } else if (skip_prefix(arg, "--remotes=", &optarg)) {
2671 struct all_refs_cb cb;
2672 init_all_refs_cb(&cb, revs, *flags);
2673 for_each_glob_ref_in(handle_one_ref, optarg, "refs/remotes/", &cb);
2674 clear_ref_exclusion(&revs->ref_excludes);
2675 } else if (!strcmp(arg, "--reflog")) {
2676 add_reflogs_to_pending(revs, *flags);
2677 } else if (!strcmp(arg, "--indexed-objects")) {
2678 add_index_objects_to_pending(revs, *flags);
2679 } else if (!strcmp(arg, "--alternate-refs")) {
2680 add_alternate_refs_to_pending(revs, *flags);
2681 } else if (!strcmp(arg, "--not")) {
2682 *flags ^= UNINTERESTING | BOTTOM;
2683 } else if (!strcmp(arg, "--no-walk")) {
2684 revs->no_walk = 1;
2685 } else if (skip_prefix(arg, "--no-walk=", &optarg)) {
2687 * Detached form ("--no-walk X" as opposed to "--no-walk=X")
2688 * not allowed, since the argument is optional.
2690 revs->no_walk = 1;
2691 if (!strcmp(optarg, "sorted"))
2692 revs->unsorted_input = 0;
2693 else if (!strcmp(optarg, "unsorted"))
2694 revs->unsorted_input = 1;
2695 else
2696 return error("invalid argument to --no-walk");
2697 } else if (!strcmp(arg, "--do-walk")) {
2698 revs->no_walk = 0;
2699 } else if (!strcmp(arg, "--single-worktree")) {
2700 revs->single_worktree = 1;
2701 } else if (skip_prefix(arg, ("--filter="), &arg)) {
2702 parse_list_objects_filter(&revs->filter, arg);
2703 } else if (!strcmp(arg, ("--no-filter"))) {
2704 list_objects_filter_set_no_filter(&revs->filter);
2705 } else {
2706 return 0;
2709 return 1;
2712 static void NORETURN diagnose_missing_default(const char *def)
2714 int flags;
2715 const char *refname;
2717 refname = resolve_ref_unsafe(def, 0, NULL, &flags);
2718 if (!refname || !(flags & REF_ISSYMREF) || (flags & REF_ISBROKEN))
2719 die(_("your current branch appears to be broken"));
2721 skip_prefix(refname, "refs/heads/", &refname);
2722 die(_("your current branch '%s' does not have any commits yet"),
2723 refname);
2727 * Parse revision information, filling in the "rev_info" structure,
2728 * and removing the used arguments from the argument list.
2730 * Returns the number of arguments left that weren't recognized
2731 * (which are also moved to the head of the argument list)
2733 int setup_revisions(int argc, const char **argv, struct rev_info *revs, struct setup_revision_opt *opt)
2735 int i, flags, left, seen_dashdash, revarg_opt;
2736 struct strvec prune_data = STRVEC_INIT;
2737 int seen_end_of_options = 0;
2739 /* First, search for "--" */
2740 if (opt && opt->assume_dashdash) {
2741 seen_dashdash = 1;
2742 } else {
2743 seen_dashdash = 0;
2744 for (i = 1; i < argc; i++) {
2745 const char *arg = argv[i];
2746 if (strcmp(arg, "--"))
2747 continue;
2748 argv[i] = NULL;
2749 argc = i;
2750 if (argv[i + 1])
2751 strvec_pushv(&prune_data, argv + i + 1);
2752 seen_dashdash = 1;
2753 break;
2757 /* Second, deal with arguments and options */
2758 flags = 0;
2759 revarg_opt = opt ? opt->revarg_opt : 0;
2760 if (seen_dashdash)
2761 revarg_opt |= REVARG_CANNOT_BE_FILENAME;
2762 for (left = i = 1; i < argc; i++) {
2763 const char *arg = argv[i];
2764 if (!seen_end_of_options && *arg == '-') {
2765 int opts;
2767 opts = handle_revision_pseudo_opt(
2768 revs, argv + i,
2769 &flags);
2770 if (opts > 0) {
2771 i += opts - 1;
2772 continue;
2775 if (!strcmp(arg, "--stdin")) {
2776 if (revs->disable_stdin) {
2777 argv[left++] = arg;
2778 continue;
2780 if (revs->read_from_stdin++)
2781 die("--stdin given twice?");
2782 read_revisions_from_stdin(revs, &prune_data);
2783 continue;
2786 if (!strcmp(arg, "--end-of-options")) {
2787 seen_end_of_options = 1;
2788 continue;
2791 opts = handle_revision_opt(revs, argc - i, argv + i,
2792 &left, argv, opt);
2793 if (opts > 0) {
2794 i += opts - 1;
2795 continue;
2797 if (opts < 0)
2798 exit(128);
2799 continue;
2803 if (handle_revision_arg(arg, revs, flags, revarg_opt)) {
2804 int j;
2805 if (seen_dashdash || *arg == '^')
2806 die("bad revision '%s'", arg);
2808 /* If we didn't have a "--":
2809 * (1) all filenames must exist;
2810 * (2) all rev-args must not be interpretable
2811 * as a valid filename.
2812 * but the latter we have checked in the main loop.
2814 for (j = i; j < argc; j++)
2815 verify_filename(revs->prefix, argv[j], j == i);
2817 strvec_pushv(&prune_data, argv + i);
2818 break;
2821 revision_opts_finish(revs);
2823 if (prune_data.nr) {
2825 * If we need to introduce the magic "a lone ':' means no
2826 * pathspec whatsoever", here is the place to do so.
2828 * if (prune_data.nr == 1 && !strcmp(prune_data[0], ":")) {
2829 * prune_data.nr = 0;
2830 * prune_data.alloc = 0;
2831 * free(prune_data.path);
2832 * prune_data.path = NULL;
2833 * } else {
2834 * terminate prune_data.alloc with NULL and
2835 * call init_pathspec() to set revs->prune_data here.
2838 parse_pathspec(&revs->prune_data, 0, 0,
2839 revs->prefix, prune_data.v);
2841 strvec_clear(&prune_data);
2843 if (!revs->def)
2844 revs->def = opt ? opt->def : NULL;
2845 if (opt && opt->tweak)
2846 opt->tweak(revs, opt);
2847 if (revs->show_merge)
2848 prepare_show_merge(revs);
2849 if (revs->def && !revs->pending.nr && !revs->rev_input_given) {
2850 struct object_id oid;
2851 struct object *object;
2852 struct object_context oc;
2853 if (get_oid_with_context(revs->repo, revs->def, 0, &oid, &oc))
2854 diagnose_missing_default(revs->def);
2855 object = get_reference(revs, revs->def, &oid, 0);
2856 add_pending_object_with_mode(revs, object, revs->def, oc.mode);
2859 /* Did the user ask for any diff output? Run the diff! */
2860 if (revs->diffopt.output_format & ~DIFF_FORMAT_NO_OUTPUT)
2861 revs->diff = 1;
2863 /* Pickaxe, diff-filter and rename following need diffs */
2864 if ((revs->diffopt.pickaxe_opts & DIFF_PICKAXE_KINDS_MASK) ||
2865 revs->diffopt.filter ||
2866 revs->diffopt.flags.follow_renames)
2867 revs->diff = 1;
2869 if (revs->diffopt.objfind)
2870 revs->simplify_history = 0;
2872 if (revs->line_level_traverse) {
2873 if (want_ancestry(revs))
2874 revs->limited = 1;
2875 revs->topo_order = 1;
2878 if (revs->topo_order && !generation_numbers_enabled(the_repository))
2879 revs->limited = 1;
2881 if (revs->prune_data.nr) {
2882 copy_pathspec(&revs->pruning.pathspec, &revs->prune_data);
2883 /* Can't prune commits with rename following: the paths change.. */
2884 if (!revs->diffopt.flags.follow_renames)
2885 revs->prune = 1;
2886 if (!revs->full_diff)
2887 copy_pathspec(&revs->diffopt.pathspec,
2888 &revs->prune_data);
2891 diff_merges_setup_revs(revs);
2893 revs->diffopt.abbrev = revs->abbrev;
2895 diff_setup_done(&revs->diffopt);
2897 if (!is_encoding_utf8(get_log_output_encoding()))
2898 revs->grep_filter.ignore_locale = 1;
2899 compile_grep_patterns(&revs->grep_filter);
2901 if (revs->reverse && revs->reflog_info)
2902 die(_("options '%s' and '%s' cannot be used together"), "--reverse", "--walk-reflogs");
2903 if (revs->reflog_info && revs->limited)
2904 die("cannot combine --walk-reflogs with history-limiting options");
2905 if (revs->rewrite_parents && revs->children.name)
2906 die(_("options '%s' and '%s' cannot be used together"), "--parents", "--children");
2907 if (revs->filter.choice && !revs->blob_objects)
2908 die(_("object filtering requires --objects"));
2911 * Limitations on the graph functionality
2913 if (revs->reverse && revs->graph)
2914 die(_("options '%s' and '%s' cannot be used together"), "--reverse", "--graph");
2916 if (revs->reflog_info && revs->graph)
2917 die(_("options '%s' and '%s' cannot be used together"), "--walk-reflogs", "--graph");
2918 if (revs->no_walk && revs->graph)
2919 die(_("options '%s' and '%s' cannot be used together"), "--no-walk", "--graph");
2920 if (!revs->reflog_info && revs->grep_filter.use_reflog_filter)
2921 die(_("the option '%s' requires '%s'"), "--grep-reflog", "--walk-reflogs");
2923 if (revs->line_level_traverse &&
2924 (revs->diffopt.output_format & ~(DIFF_FORMAT_PATCH | DIFF_FORMAT_NO_OUTPUT)))
2925 die(_("-L does not yet support diff formats besides -p and -s"));
2927 if (revs->expand_tabs_in_log < 0)
2928 revs->expand_tabs_in_log = revs->expand_tabs_in_log_default;
2930 return left;
2933 static void add_child(struct rev_info *revs, struct commit *parent, struct commit *child)
2935 struct commit_list *l = xcalloc(1, sizeof(*l));
2937 l->item = child;
2938 l->next = add_decoration(&revs->children, &parent->object, l);
2941 static int remove_duplicate_parents(struct rev_info *revs, struct commit *commit)
2943 struct treesame_state *ts = lookup_decoration(&revs->treesame, &commit->object);
2944 struct commit_list **pp, *p;
2945 int surviving_parents;
2947 /* Examine existing parents while marking ones we have seen... */
2948 pp = &commit->parents;
2949 surviving_parents = 0;
2950 while ((p = *pp) != NULL) {
2951 struct commit *parent = p->item;
2952 if (parent->object.flags & TMP_MARK) {
2953 *pp = p->next;
2954 if (ts)
2955 compact_treesame(revs, commit, surviving_parents);
2956 continue;
2958 parent->object.flags |= TMP_MARK;
2959 surviving_parents++;
2960 pp = &p->next;
2962 /* clear the temporary mark */
2963 for (p = commit->parents; p; p = p->next) {
2964 p->item->object.flags &= ~TMP_MARK;
2966 /* no update_treesame() - removing duplicates can't affect TREESAME */
2967 return surviving_parents;
2970 struct merge_simplify_state {
2971 struct commit *simplified;
2974 static struct merge_simplify_state *locate_simplify_state(struct rev_info *revs, struct commit *commit)
2976 struct merge_simplify_state *st;
2978 st = lookup_decoration(&revs->merge_simplification, &commit->object);
2979 if (!st) {
2980 CALLOC_ARRAY(st, 1);
2981 add_decoration(&revs->merge_simplification, &commit->object, st);
2983 return st;
2986 static int mark_redundant_parents(struct commit *commit)
2988 struct commit_list *h = reduce_heads(commit->parents);
2989 int i = 0, marked = 0;
2990 struct commit_list *po, *pn;
2992 /* Want these for sanity-checking only */
2993 int orig_cnt = commit_list_count(commit->parents);
2994 int cnt = commit_list_count(h);
2997 * Not ready to remove items yet, just mark them for now, based
2998 * on the output of reduce_heads(). reduce_heads outputs the reduced
2999 * set in its original order, so this isn't too hard.
3001 po = commit->parents;
3002 pn = h;
3003 while (po) {
3004 if (pn && po->item == pn->item) {
3005 pn = pn->next;
3006 i++;
3007 } else {
3008 po->item->object.flags |= TMP_MARK;
3009 marked++;
3011 po=po->next;
3014 if (i != cnt || cnt+marked != orig_cnt)
3015 die("mark_redundant_parents %d %d %d %d", orig_cnt, cnt, i, marked);
3017 free_commit_list(h);
3019 return marked;
3022 static int mark_treesame_root_parents(struct commit *commit)
3024 struct commit_list *p;
3025 int marked = 0;
3027 for (p = commit->parents; p; p = p->next) {
3028 struct commit *parent = p->item;
3029 if (!parent->parents && (parent->object.flags & TREESAME)) {
3030 parent->object.flags |= TMP_MARK;
3031 marked++;
3035 return marked;
3039 * Awkward naming - this means one parent we are TREESAME to.
3040 * cf mark_treesame_root_parents: root parents that are TREESAME (to an
3041 * empty tree). Better name suggestions?
3043 static int leave_one_treesame_to_parent(struct rev_info *revs, struct commit *commit)
3045 struct treesame_state *ts = lookup_decoration(&revs->treesame, &commit->object);
3046 struct commit *unmarked = NULL, *marked = NULL;
3047 struct commit_list *p;
3048 unsigned n;
3050 for (p = commit->parents, n = 0; p; p = p->next, n++) {
3051 if (ts->treesame[n]) {
3052 if (p->item->object.flags & TMP_MARK) {
3053 if (!marked)
3054 marked = p->item;
3055 } else {
3056 if (!unmarked) {
3057 unmarked = p->item;
3058 break;
3065 * If we are TREESAME to a marked-for-deletion parent, but not to any
3066 * unmarked parents, unmark the first TREESAME parent. This is the
3067 * parent that the default simplify_history==1 scan would have followed,
3068 * and it doesn't make sense to omit that path when asking for a
3069 * simplified full history. Retaining it improves the chances of
3070 * understanding odd missed merges that took an old version of a file.
3072 * Example:
3074 * I--------*X A modified the file, but mainline merge X used
3075 * \ / "-s ours", so took the version from I. X is
3076 * `-*A--' TREESAME to I and !TREESAME to A.
3078 * Default log from X would produce "I". Without this check,
3079 * --full-history --simplify-merges would produce "I-A-X", showing
3080 * the merge commit X and that it changed A, but not making clear that
3081 * it had just taken the I version. With this check, the topology above
3082 * is retained.
3084 * Note that it is possible that the simplification chooses a different
3085 * TREESAME parent from the default, in which case this test doesn't
3086 * activate, and we _do_ drop the default parent. Example:
3088 * I------X A modified the file, but it was reverted in B,
3089 * \ / meaning mainline merge X is TREESAME to both
3090 * *A-*B parents.
3092 * Default log would produce "I" by following the first parent;
3093 * --full-history --simplify-merges will produce "I-A-B". But this is a
3094 * reasonable result - it presents a logical full history leading from
3095 * I to X, and X is not an important merge.
3097 if (!unmarked && marked) {
3098 marked->object.flags &= ~TMP_MARK;
3099 return 1;
3102 return 0;
3105 static int remove_marked_parents(struct rev_info *revs, struct commit *commit)
3107 struct commit_list **pp, *p;
3108 int nth_parent, removed = 0;
3110 pp = &commit->parents;
3111 nth_parent = 0;
3112 while ((p = *pp) != NULL) {
3113 struct commit *parent = p->item;
3114 if (parent->object.flags & TMP_MARK) {
3115 parent->object.flags &= ~TMP_MARK;
3116 *pp = p->next;
3117 free(p);
3118 removed++;
3119 compact_treesame(revs, commit, nth_parent);
3120 continue;
3122 pp = &p->next;
3123 nth_parent++;
3126 /* Removing parents can only increase TREESAMEness */
3127 if (removed && !(commit->object.flags & TREESAME))
3128 update_treesame(revs, commit);
3130 return nth_parent;
3133 static struct commit_list **simplify_one(struct rev_info *revs, struct commit *commit, struct commit_list **tail)
3135 struct commit_list *p;
3136 struct commit *parent;
3137 struct merge_simplify_state *st, *pst;
3138 int cnt;
3140 st = locate_simplify_state(revs, commit);
3143 * Have we handled this one?
3145 if (st->simplified)
3146 return tail;
3149 * An UNINTERESTING commit simplifies to itself, so does a
3150 * root commit. We do not rewrite parents of such commit
3151 * anyway.
3153 if ((commit->object.flags & UNINTERESTING) || !commit->parents) {
3154 st->simplified = commit;
3155 return tail;
3159 * Do we know what commit all of our parents that matter
3160 * should be rewritten to? Otherwise we are not ready to
3161 * rewrite this one yet.
3163 for (cnt = 0, p = commit->parents; p; p = p->next) {
3164 pst = locate_simplify_state(revs, p->item);
3165 if (!pst->simplified) {
3166 tail = &commit_list_insert(p->item, tail)->next;
3167 cnt++;
3169 if (revs->first_parent_only)
3170 break;
3172 if (cnt) {
3173 tail = &commit_list_insert(commit, tail)->next;
3174 return tail;
3178 * Rewrite our list of parents. Note that this cannot
3179 * affect our TREESAME flags in any way - a commit is
3180 * always TREESAME to its simplification.
3182 for (p = commit->parents; p; p = p->next) {
3183 pst = locate_simplify_state(revs, p->item);
3184 p->item = pst->simplified;
3185 if (revs->first_parent_only)
3186 break;
3189 if (revs->first_parent_only)
3190 cnt = 1;
3191 else
3192 cnt = remove_duplicate_parents(revs, commit);
3195 * It is possible that we are a merge and one side branch
3196 * does not have any commit that touches the given paths;
3197 * in such a case, the immediate parent from that branch
3198 * will be rewritten to be the merge base.
3200 * o----X X: the commit we are looking at;
3201 * / / o: a commit that touches the paths;
3202 * ---o----'
3204 * Further, a merge of an independent branch that doesn't
3205 * touch the path will reduce to a treesame root parent:
3207 * ----o----X X: the commit we are looking at;
3208 * / o: a commit that touches the paths;
3209 * r r: a root commit not touching the paths
3211 * Detect and simplify both cases.
3213 if (1 < cnt) {
3214 int marked = mark_redundant_parents(commit);
3215 marked += mark_treesame_root_parents(commit);
3216 if (marked)
3217 marked -= leave_one_treesame_to_parent(revs, commit);
3218 if (marked)
3219 cnt = remove_marked_parents(revs, commit);
3223 * A commit simplifies to itself if it is a root, if it is
3224 * UNINTERESTING, if it touches the given paths, or if it is a
3225 * merge and its parents don't simplify to one relevant commit
3226 * (the first two cases are already handled at the beginning of
3227 * this function).
3229 * Otherwise, it simplifies to what its sole relevant parent
3230 * simplifies to.
3232 if (!cnt ||
3233 (commit->object.flags & UNINTERESTING) ||
3234 !(commit->object.flags & TREESAME) ||
3235 (parent = one_relevant_parent(revs, commit->parents)) == NULL ||
3236 (revs->show_pulls && (commit->object.flags & PULL_MERGE)))
3237 st->simplified = commit;
3238 else {
3239 pst = locate_simplify_state(revs, parent);
3240 st->simplified = pst->simplified;
3242 return tail;
3245 static void simplify_merges(struct rev_info *revs)
3247 struct commit_list *list, *next;
3248 struct commit_list *yet_to_do, **tail;
3249 struct commit *commit;
3251 if (!revs->prune)
3252 return;
3254 /* feed the list reversed */
3255 yet_to_do = NULL;
3256 for (list = revs->commits; list; list = next) {
3257 commit = list->item;
3258 next = list->next;
3260 * Do not free(list) here yet; the original list
3261 * is used later in this function.
3263 commit_list_insert(commit, &yet_to_do);
3265 while (yet_to_do) {
3266 list = yet_to_do;
3267 yet_to_do = NULL;
3268 tail = &yet_to_do;
3269 while (list) {
3270 commit = pop_commit(&list);
3271 tail = simplify_one(revs, commit, tail);
3275 /* clean up the result, removing the simplified ones */
3276 list = revs->commits;
3277 revs->commits = NULL;
3278 tail = &revs->commits;
3279 while (list) {
3280 struct merge_simplify_state *st;
3282 commit = pop_commit(&list);
3283 st = locate_simplify_state(revs, commit);
3284 if (st->simplified == commit)
3285 tail = &commit_list_insert(commit, tail)->next;
3289 static void set_children(struct rev_info *revs)
3291 struct commit_list *l;
3292 for (l = revs->commits; l; l = l->next) {
3293 struct commit *commit = l->item;
3294 struct commit_list *p;
3296 for (p = commit->parents; p; p = p->next)
3297 add_child(revs, p->item, commit);
3301 void reset_revision_walk(void)
3303 clear_object_flags(SEEN | ADDED | SHOWN | TOPO_WALK_EXPLORED | TOPO_WALK_INDEGREE);
3306 static int mark_uninteresting(const struct object_id *oid,
3307 struct packed_git *pack,
3308 uint32_t pos,
3309 void *cb)
3311 struct rev_info *revs = cb;
3312 struct object *o = lookup_unknown_object(revs->repo, oid);
3313 o->flags |= UNINTERESTING | SEEN;
3314 return 0;
3317 define_commit_slab(indegree_slab, int);
3318 define_commit_slab(author_date_slab, timestamp_t);
3320 struct topo_walk_info {
3321 timestamp_t min_generation;
3322 struct prio_queue explore_queue;
3323 struct prio_queue indegree_queue;
3324 struct prio_queue topo_queue;
3325 struct indegree_slab indegree;
3326 struct author_date_slab author_date;
3329 static int topo_walk_atexit_registered;
3330 static unsigned int count_explore_walked;
3331 static unsigned int count_indegree_walked;
3332 static unsigned int count_topo_walked;
3334 static void trace2_topo_walk_statistics_atexit(void)
3336 struct json_writer jw = JSON_WRITER_INIT;
3338 jw_object_begin(&jw, 0);
3339 jw_object_intmax(&jw, "count_explore_walked", count_explore_walked);
3340 jw_object_intmax(&jw, "count_indegree_walked", count_indegree_walked);
3341 jw_object_intmax(&jw, "count_topo_walked", count_topo_walked);
3342 jw_end(&jw);
3344 trace2_data_json("topo_walk", the_repository, "statistics", &jw);
3346 jw_release(&jw);
3349 static inline void test_flag_and_insert(struct prio_queue *q, struct commit *c, int flag)
3351 if (c->object.flags & flag)
3352 return;
3354 c->object.flags |= flag;
3355 prio_queue_put(q, c);
3358 static void explore_walk_step(struct rev_info *revs)
3360 struct topo_walk_info *info = revs->topo_walk_info;
3361 struct commit_list *p;
3362 struct commit *c = prio_queue_get(&info->explore_queue);
3364 if (!c)
3365 return;
3367 if (repo_parse_commit_gently(revs->repo, c, 1) < 0)
3368 return;
3370 count_explore_walked++;
3372 if (revs->sort_order == REV_SORT_BY_AUTHOR_DATE)
3373 record_author_date(&info->author_date, c);
3375 if (revs->max_age != -1 && (c->date < revs->max_age))
3376 c->object.flags |= UNINTERESTING;
3378 if (process_parents(revs, c, NULL, NULL) < 0)
3379 return;
3381 if (c->object.flags & UNINTERESTING)
3382 mark_parents_uninteresting(revs, c);
3384 for (p = c->parents; p; p = p->next)
3385 test_flag_and_insert(&info->explore_queue, p->item, TOPO_WALK_EXPLORED);
3388 static void explore_to_depth(struct rev_info *revs,
3389 timestamp_t gen_cutoff)
3391 struct topo_walk_info *info = revs->topo_walk_info;
3392 struct commit *c;
3393 while ((c = prio_queue_peek(&info->explore_queue)) &&
3394 commit_graph_generation(c) >= gen_cutoff)
3395 explore_walk_step(revs);
3398 static void indegree_walk_step(struct rev_info *revs)
3400 struct commit_list *p;
3401 struct topo_walk_info *info = revs->topo_walk_info;
3402 struct commit *c = prio_queue_get(&info->indegree_queue);
3404 if (!c)
3405 return;
3407 if (repo_parse_commit_gently(revs->repo, c, 1) < 0)
3408 return;
3410 count_indegree_walked++;
3412 explore_to_depth(revs, commit_graph_generation(c));
3414 for (p = c->parents; p; p = p->next) {
3415 struct commit *parent = p->item;
3416 int *pi = indegree_slab_at(&info->indegree, parent);
3418 if (repo_parse_commit_gently(revs->repo, parent, 1) < 0)
3419 return;
3421 if (*pi)
3422 (*pi)++;
3423 else
3424 *pi = 2;
3426 test_flag_and_insert(&info->indegree_queue, parent, TOPO_WALK_INDEGREE);
3428 if (revs->first_parent_only)
3429 return;
3433 static void compute_indegrees_to_depth(struct rev_info *revs,
3434 timestamp_t gen_cutoff)
3436 struct topo_walk_info *info = revs->topo_walk_info;
3437 struct commit *c;
3438 while ((c = prio_queue_peek(&info->indegree_queue)) &&
3439 commit_graph_generation(c) >= gen_cutoff)
3440 indegree_walk_step(revs);
3443 static void reset_topo_walk(struct rev_info *revs)
3445 struct topo_walk_info *info = revs->topo_walk_info;
3447 clear_prio_queue(&info->explore_queue);
3448 clear_prio_queue(&info->indegree_queue);
3449 clear_prio_queue(&info->topo_queue);
3450 clear_indegree_slab(&info->indegree);
3451 clear_author_date_slab(&info->author_date);
3453 FREE_AND_NULL(revs->topo_walk_info);
3456 static void init_topo_walk(struct rev_info *revs)
3458 struct topo_walk_info *info;
3459 struct commit_list *list;
3460 if (revs->topo_walk_info)
3461 reset_topo_walk(revs);
3463 revs->topo_walk_info = xmalloc(sizeof(struct topo_walk_info));
3464 info = revs->topo_walk_info;
3465 memset(info, 0, sizeof(struct topo_walk_info));
3467 init_indegree_slab(&info->indegree);
3468 memset(&info->explore_queue, 0, sizeof(info->explore_queue));
3469 memset(&info->indegree_queue, 0, sizeof(info->indegree_queue));
3470 memset(&info->topo_queue, 0, sizeof(info->topo_queue));
3472 switch (revs->sort_order) {
3473 default: /* REV_SORT_IN_GRAPH_ORDER */
3474 info->topo_queue.compare = NULL;
3475 break;
3476 case REV_SORT_BY_COMMIT_DATE:
3477 info->topo_queue.compare = compare_commits_by_commit_date;
3478 break;
3479 case REV_SORT_BY_AUTHOR_DATE:
3480 init_author_date_slab(&info->author_date);
3481 info->topo_queue.compare = compare_commits_by_author_date;
3482 info->topo_queue.cb_data = &info->author_date;
3483 break;
3486 info->explore_queue.compare = compare_commits_by_gen_then_commit_date;
3487 info->indegree_queue.compare = compare_commits_by_gen_then_commit_date;
3489 info->min_generation = GENERATION_NUMBER_INFINITY;
3490 for (list = revs->commits; list; list = list->next) {
3491 struct commit *c = list->item;
3492 timestamp_t generation;
3494 if (repo_parse_commit_gently(revs->repo, c, 1))
3495 continue;
3497 test_flag_and_insert(&info->explore_queue, c, TOPO_WALK_EXPLORED);
3498 test_flag_and_insert(&info->indegree_queue, c, TOPO_WALK_INDEGREE);
3500 generation = commit_graph_generation(c);
3501 if (generation < info->min_generation)
3502 info->min_generation = generation;
3504 *(indegree_slab_at(&info->indegree, c)) = 1;
3506 if (revs->sort_order == REV_SORT_BY_AUTHOR_DATE)
3507 record_author_date(&info->author_date, c);
3509 compute_indegrees_to_depth(revs, info->min_generation);
3511 for (list = revs->commits; list; list = list->next) {
3512 struct commit *c = list->item;
3514 if (*(indegree_slab_at(&info->indegree, c)) == 1)
3515 prio_queue_put(&info->topo_queue, c);
3519 * This is unfortunate; the initial tips need to be shown
3520 * in the order given from the revision traversal machinery.
3522 if (revs->sort_order == REV_SORT_IN_GRAPH_ORDER)
3523 prio_queue_reverse(&info->topo_queue);
3525 if (trace2_is_enabled() && !topo_walk_atexit_registered) {
3526 atexit(trace2_topo_walk_statistics_atexit);
3527 topo_walk_atexit_registered = 1;
3531 static struct commit *next_topo_commit(struct rev_info *revs)
3533 struct commit *c;
3534 struct topo_walk_info *info = revs->topo_walk_info;
3536 /* pop next off of topo_queue */
3537 c = prio_queue_get(&info->topo_queue);
3539 if (c)
3540 *(indegree_slab_at(&info->indegree, c)) = 0;
3542 return c;
3545 static void expand_topo_walk(struct rev_info *revs, struct commit *commit)
3547 struct commit_list *p;
3548 struct topo_walk_info *info = revs->topo_walk_info;
3549 if (process_parents(revs, commit, NULL, NULL) < 0) {
3550 if (!revs->ignore_missing_links)
3551 die("Failed to traverse parents of commit %s",
3552 oid_to_hex(&commit->object.oid));
3555 count_topo_walked++;
3557 for (p = commit->parents; p; p = p->next) {
3558 struct commit *parent = p->item;
3559 int *pi;
3560 timestamp_t generation;
3562 if (parent->object.flags & UNINTERESTING)
3563 continue;
3565 if (repo_parse_commit_gently(revs->repo, parent, 1) < 0)
3566 continue;
3568 generation = commit_graph_generation(parent);
3569 if (generation < info->min_generation) {
3570 info->min_generation = generation;
3571 compute_indegrees_to_depth(revs, info->min_generation);
3574 pi = indegree_slab_at(&info->indegree, parent);
3576 (*pi)--;
3577 if (*pi == 1)
3578 prio_queue_put(&info->topo_queue, parent);
3580 if (revs->first_parent_only)
3581 return;
3585 int prepare_revision_walk(struct rev_info *revs)
3587 int i;
3588 struct object_array old_pending;
3589 struct commit_list **next = &revs->commits;
3591 memcpy(&old_pending, &revs->pending, sizeof(old_pending));
3592 revs->pending.nr = 0;
3593 revs->pending.alloc = 0;
3594 revs->pending.objects = NULL;
3595 for (i = 0; i < old_pending.nr; i++) {
3596 struct object_array_entry *e = old_pending.objects + i;
3597 struct commit *commit = handle_commit(revs, e);
3598 if (commit) {
3599 if (!(commit->object.flags & SEEN)) {
3600 commit->object.flags |= SEEN;
3601 next = commit_list_append(commit, next);
3605 object_array_clear(&old_pending);
3607 /* Signal whether we need per-parent treesame decoration */
3608 if (revs->simplify_merges ||
3609 (revs->limited && limiting_can_increase_treesame(revs)))
3610 revs->treesame.name = "treesame";
3612 if (revs->exclude_promisor_objects) {
3613 for_each_packed_object(mark_uninteresting, revs,
3614 FOR_EACH_OBJECT_PROMISOR_ONLY);
3617 if (!revs->reflog_info)
3618 prepare_to_use_bloom_filter(revs);
3619 if (!revs->unsorted_input)
3620 commit_list_sort_by_date(&revs->commits);
3621 if (revs->no_walk)
3622 return 0;
3623 if (revs->limited) {
3624 if (limit_list(revs) < 0)
3625 return -1;
3626 if (revs->topo_order)
3627 sort_in_topological_order(&revs->commits, revs->sort_order);
3628 } else if (revs->topo_order)
3629 init_topo_walk(revs);
3630 if (revs->line_level_traverse && want_ancestry(revs))
3632 * At the moment we can only do line-level log with parent
3633 * rewriting by performing this expensive pre-filtering step.
3634 * If parent rewriting is not requested, then we rather
3635 * perform the line-level log filtering during the regular
3636 * history traversal.
3638 line_log_filter(revs);
3639 if (revs->simplify_merges)
3640 simplify_merges(revs);
3641 if (revs->children.name)
3642 set_children(revs);
3644 return 0;
3647 static enum rewrite_result rewrite_one_1(struct rev_info *revs,
3648 struct commit **pp,
3649 struct prio_queue *queue)
3651 for (;;) {
3652 struct commit *p = *pp;
3653 if (!revs->limited)
3654 if (process_parents(revs, p, NULL, queue) < 0)
3655 return rewrite_one_error;
3656 if (p->object.flags & UNINTERESTING)
3657 return rewrite_one_ok;
3658 if (!(p->object.flags & TREESAME))
3659 return rewrite_one_ok;
3660 if (!p->parents)
3661 return rewrite_one_noparents;
3662 if (!(p = one_relevant_parent(revs, p->parents)))
3663 return rewrite_one_ok;
3664 *pp = p;
3668 static void merge_queue_into_list(struct prio_queue *q, struct commit_list **list)
3670 while (q->nr) {
3671 struct commit *item = prio_queue_peek(q);
3672 struct commit_list *p = *list;
3674 if (p && p->item->date >= item->date)
3675 list = &p->next;
3676 else {
3677 p = commit_list_insert(item, list);
3678 list = &p->next; /* skip newly added item */
3679 prio_queue_get(q); /* pop item */
3684 static enum rewrite_result rewrite_one(struct rev_info *revs, struct commit **pp)
3686 struct prio_queue queue = { compare_commits_by_commit_date };
3687 enum rewrite_result ret = rewrite_one_1(revs, pp, &queue);
3688 merge_queue_into_list(&queue, &revs->commits);
3689 clear_prio_queue(&queue);
3690 return ret;
3693 int rewrite_parents(struct rev_info *revs, struct commit *commit,
3694 rewrite_parent_fn_t rewrite_parent)
3696 struct commit_list **pp = &commit->parents;
3697 while (*pp) {
3698 struct commit_list *parent = *pp;
3699 switch (rewrite_parent(revs, &parent->item)) {
3700 case rewrite_one_ok:
3701 break;
3702 case rewrite_one_noparents:
3703 *pp = parent->next;
3704 continue;
3705 case rewrite_one_error:
3706 return -1;
3708 pp = &parent->next;
3710 remove_duplicate_parents(revs, commit);
3711 return 0;
3714 static int commit_rewrite_person(struct strbuf *buf, const char *what, struct string_list *mailmap)
3716 char *person, *endp;
3717 size_t len, namelen, maillen;
3718 const char *name;
3719 const char *mail;
3720 struct ident_split ident;
3722 person = strstr(buf->buf, what);
3723 if (!person)
3724 return 0;
3726 person += strlen(what);
3727 endp = strchr(person, '\n');
3728 if (!endp)
3729 return 0;
3731 len = endp - person;
3733 if (split_ident_line(&ident, person, len))
3734 return 0;
3736 mail = ident.mail_begin;
3737 maillen = ident.mail_end - ident.mail_begin;
3738 name = ident.name_begin;
3739 namelen = ident.name_end - ident.name_begin;
3741 if (map_user(mailmap, &mail, &maillen, &name, &namelen)) {
3742 struct strbuf namemail = STRBUF_INIT;
3744 strbuf_addf(&namemail, "%.*s <%.*s>",
3745 (int)namelen, name, (int)maillen, mail);
3747 strbuf_splice(buf, ident.name_begin - buf->buf,
3748 ident.mail_end - ident.name_begin + 1,
3749 namemail.buf, namemail.len);
3751 strbuf_release(&namemail);
3753 return 1;
3756 return 0;
3759 static int commit_match(struct commit *commit, struct rev_info *opt)
3761 int retval;
3762 const char *encoding;
3763 const char *message;
3764 struct strbuf buf = STRBUF_INIT;
3766 if (!opt->grep_filter.pattern_list && !opt->grep_filter.header_list)
3767 return 1;
3769 /* Prepend "fake" headers as needed */
3770 if (opt->grep_filter.use_reflog_filter) {
3771 strbuf_addstr(&buf, "reflog ");
3772 get_reflog_message(&buf, opt->reflog_info);
3773 strbuf_addch(&buf, '\n');
3777 * We grep in the user's output encoding, under the assumption that it
3778 * is the encoding they are most likely to write their grep pattern
3779 * for. In addition, it means we will match the "notes" encoding below,
3780 * so we will not end up with a buffer that has two different encodings
3781 * in it.
3783 encoding = get_log_output_encoding();
3784 message = logmsg_reencode(commit, NULL, encoding);
3786 /* Copy the commit to temporary if we are using "fake" headers */
3787 if (buf.len)
3788 strbuf_addstr(&buf, message);
3790 if (opt->grep_filter.header_list && opt->mailmap) {
3791 if (!buf.len)
3792 strbuf_addstr(&buf, message);
3794 commit_rewrite_person(&buf, "\nauthor ", opt->mailmap);
3795 commit_rewrite_person(&buf, "\ncommitter ", opt->mailmap);
3798 /* Append "fake" message parts as needed */
3799 if (opt->show_notes) {
3800 if (!buf.len)
3801 strbuf_addstr(&buf, message);
3802 format_display_notes(&commit->object.oid, &buf, encoding, 1);
3806 * Find either in the original commit message, or in the temporary.
3807 * Note that we cast away the constness of "message" here. It is
3808 * const because it may come from the cached commit buffer. That's OK,
3809 * because we know that it is modifiable heap memory, and that while
3810 * grep_buffer may modify it for speed, it will restore any
3811 * changes before returning.
3813 if (buf.len)
3814 retval = grep_buffer(&opt->grep_filter, buf.buf, buf.len);
3815 else
3816 retval = grep_buffer(&opt->grep_filter,
3817 (char *)message, strlen(message));
3818 strbuf_release(&buf);
3819 unuse_commit_buffer(commit, message);
3820 return retval;
3823 static inline int want_ancestry(const struct rev_info *revs)
3825 return (revs->rewrite_parents || revs->children.name);
3829 * Return a timestamp to be used for --since/--until comparisons for this
3830 * commit, based on the revision options.
3832 static timestamp_t comparison_date(const struct rev_info *revs,
3833 struct commit *commit)
3835 return revs->reflog_info ?
3836 get_reflog_timestamp(revs->reflog_info) :
3837 commit->date;
3840 enum commit_action get_commit_action(struct rev_info *revs, struct commit *commit)
3842 if (commit->object.flags & SHOWN)
3843 return commit_ignore;
3844 if (revs->unpacked && has_object_pack(&commit->object.oid))
3845 return commit_ignore;
3846 if (revs->no_kept_objects) {
3847 if (has_object_kept_pack(&commit->object.oid,
3848 revs->keep_pack_cache_flags))
3849 return commit_ignore;
3851 if (commit->object.flags & UNINTERESTING)
3852 return commit_ignore;
3853 if (revs->line_level_traverse && !want_ancestry(revs)) {
3855 * In case of line-level log with parent rewriting
3856 * prepare_revision_walk() already took care of all line-level
3857 * log filtering, and there is nothing left to do here.
3859 * If parent rewriting was not requested, then this is the
3860 * place to perform the line-level log filtering. Notably,
3861 * this check, though expensive, must come before the other,
3862 * cheaper filtering conditions, because the tracked line
3863 * ranges must be adjusted even when the commit will end up
3864 * being ignored based on other conditions.
3866 if (!line_log_process_ranges_arbitrary_commit(revs, commit))
3867 return commit_ignore;
3869 if (revs->min_age != -1 &&
3870 comparison_date(revs, commit) > revs->min_age)
3871 return commit_ignore;
3872 if (revs->max_age_as_filter != -1 &&
3873 comparison_date(revs, commit) < revs->max_age_as_filter)
3874 return commit_ignore;
3875 if (revs->min_parents || (revs->max_parents >= 0)) {
3876 int n = commit_list_count(commit->parents);
3877 if ((n < revs->min_parents) ||
3878 ((revs->max_parents >= 0) && (n > revs->max_parents)))
3879 return commit_ignore;
3881 if (!commit_match(commit, revs))
3882 return commit_ignore;
3883 if (revs->prune && revs->dense) {
3884 /* Commit without changes? */
3885 if (commit->object.flags & TREESAME) {
3886 int n;
3887 struct commit_list *p;
3888 /* drop merges unless we want parenthood */
3889 if (!want_ancestry(revs))
3890 return commit_ignore;
3892 if (revs->show_pulls && (commit->object.flags & PULL_MERGE))
3893 return commit_show;
3896 * If we want ancestry, then need to keep any merges
3897 * between relevant commits to tie together topology.
3898 * For consistency with TREESAME and simplification
3899 * use "relevant" here rather than just INTERESTING,
3900 * to treat bottom commit(s) as part of the topology.
3902 for (n = 0, p = commit->parents; p; p = p->next)
3903 if (relevant_commit(p->item))
3904 if (++n >= 2)
3905 return commit_show;
3906 return commit_ignore;
3909 return commit_show;
3912 define_commit_slab(saved_parents, struct commit_list *);
3914 #define EMPTY_PARENT_LIST ((struct commit_list *)-1)
3917 * You may only call save_parents() once per commit (this is checked
3918 * for non-root commits).
3920 static void save_parents(struct rev_info *revs, struct commit *commit)
3922 struct commit_list **pp;
3924 if (!revs->saved_parents_slab) {
3925 revs->saved_parents_slab = xmalloc(sizeof(struct saved_parents));
3926 init_saved_parents(revs->saved_parents_slab);
3929 pp = saved_parents_at(revs->saved_parents_slab, commit);
3932 * When walking with reflogs, we may visit the same commit
3933 * several times: once for each appearance in the reflog.
3935 * In this case, save_parents() will be called multiple times.
3936 * We want to keep only the first set of parents. We need to
3937 * store a sentinel value for an empty (i.e., NULL) parent
3938 * list to distinguish it from a not-yet-saved list, however.
3940 if (*pp)
3941 return;
3942 if (commit->parents)
3943 *pp = copy_commit_list(commit->parents);
3944 else
3945 *pp = EMPTY_PARENT_LIST;
3948 static void free_saved_parents(struct rev_info *revs)
3950 if (revs->saved_parents_slab)
3951 clear_saved_parents(revs->saved_parents_slab);
3954 struct commit_list *get_saved_parents(struct rev_info *revs, const struct commit *commit)
3956 struct commit_list *parents;
3958 if (!revs->saved_parents_slab)
3959 return commit->parents;
3961 parents = *saved_parents_at(revs->saved_parents_slab, commit);
3962 if (parents == EMPTY_PARENT_LIST)
3963 return NULL;
3964 return parents;
3967 enum commit_action simplify_commit(struct rev_info *revs, struct commit *commit)
3969 enum commit_action action = get_commit_action(revs, commit);
3971 if (action == commit_show &&
3972 revs->prune && revs->dense && want_ancestry(revs)) {
3974 * --full-diff on simplified parents is no good: it
3975 * will show spurious changes from the commits that
3976 * were elided. So we save the parents on the side
3977 * when --full-diff is in effect.
3979 if (revs->full_diff)
3980 save_parents(revs, commit);
3981 if (rewrite_parents(revs, commit, rewrite_one) < 0)
3982 return commit_error;
3984 return action;
3987 static void track_linear(struct rev_info *revs, struct commit *commit)
3989 if (revs->track_first_time) {
3990 revs->linear = 1;
3991 revs->track_first_time = 0;
3992 } else {
3993 struct commit_list *p;
3994 for (p = revs->previous_parents; p; p = p->next)
3995 if (p->item == NULL || /* first commit */
3996 oideq(&p->item->object.oid, &commit->object.oid))
3997 break;
3998 revs->linear = p != NULL;
4000 if (revs->reverse) {
4001 if (revs->linear)
4002 commit->object.flags |= TRACK_LINEAR;
4004 free_commit_list(revs->previous_parents);
4005 revs->previous_parents = copy_commit_list(commit->parents);
4008 static struct commit *get_revision_1(struct rev_info *revs)
4010 while (1) {
4011 struct commit *commit;
4013 if (revs->reflog_info)
4014 commit = next_reflog_entry(revs->reflog_info);
4015 else if (revs->topo_walk_info)
4016 commit = next_topo_commit(revs);
4017 else
4018 commit = pop_commit(&revs->commits);
4020 if (!commit)
4021 return NULL;
4023 if (revs->reflog_info)
4024 commit->object.flags &= ~(ADDED | SEEN | SHOWN);
4027 * If we haven't done the list limiting, we need to look at
4028 * the parents here. We also need to do the date-based limiting
4029 * that we'd otherwise have done in limit_list().
4031 if (!revs->limited) {
4032 if (revs->max_age != -1 &&
4033 comparison_date(revs, commit) < revs->max_age)
4034 continue;
4036 if (revs->reflog_info)
4037 try_to_simplify_commit(revs, commit);
4038 else if (revs->topo_walk_info)
4039 expand_topo_walk(revs, commit);
4040 else if (process_parents(revs, commit, &revs->commits, NULL) < 0) {
4041 if (!revs->ignore_missing_links)
4042 die("Failed to traverse parents of commit %s",
4043 oid_to_hex(&commit->object.oid));
4047 switch (simplify_commit(revs, commit)) {
4048 case commit_ignore:
4049 continue;
4050 case commit_error:
4051 die("Failed to simplify parents of commit %s",
4052 oid_to_hex(&commit->object.oid));
4053 default:
4054 if (revs->track_linear)
4055 track_linear(revs, commit);
4056 return commit;
4062 * Return true for entries that have not yet been shown. (This is an
4063 * object_array_each_func_t.)
4065 static int entry_unshown(struct object_array_entry *entry, void *cb_data_unused)
4067 return !(entry->item->flags & SHOWN);
4071 * If array is on the verge of a realloc, garbage-collect any entries
4072 * that have already been shown to try to free up some space.
4074 static void gc_boundary(struct object_array *array)
4076 if (array->nr == array->alloc)
4077 object_array_filter(array, entry_unshown, NULL);
4080 static void create_boundary_commit_list(struct rev_info *revs)
4082 unsigned i;
4083 struct commit *c;
4084 struct object_array *array = &revs->boundary_commits;
4085 struct object_array_entry *objects = array->objects;
4088 * If revs->commits is non-NULL at this point, an error occurred in
4089 * get_revision_1(). Ignore the error and continue printing the
4090 * boundary commits anyway. (This is what the code has always
4091 * done.)
4093 if (revs->commits) {
4094 free_commit_list(revs->commits);
4095 revs->commits = NULL;
4099 * Put all of the actual boundary commits from revs->boundary_commits
4100 * into revs->commits
4102 for (i = 0; i < array->nr; i++) {
4103 c = (struct commit *)(objects[i].item);
4104 if (!c)
4105 continue;
4106 if (!(c->object.flags & CHILD_SHOWN))
4107 continue;
4108 if (c->object.flags & (SHOWN | BOUNDARY))
4109 continue;
4110 c->object.flags |= BOUNDARY;
4111 commit_list_insert(c, &revs->commits);
4115 * If revs->topo_order is set, sort the boundary commits
4116 * in topological order
4118 sort_in_topological_order(&revs->commits, revs->sort_order);
4121 static struct commit *get_revision_internal(struct rev_info *revs)
4123 struct commit *c = NULL;
4124 struct commit_list *l;
4126 if (revs->boundary == 2) {
4128 * All of the normal commits have already been returned,
4129 * and we are now returning boundary commits.
4130 * create_boundary_commit_list() has populated
4131 * revs->commits with the remaining commits to return.
4133 c = pop_commit(&revs->commits);
4134 if (c)
4135 c->object.flags |= SHOWN;
4136 return c;
4140 * If our max_count counter has reached zero, then we are done. We
4141 * don't simply return NULL because we still might need to show
4142 * boundary commits. But we want to avoid calling get_revision_1, which
4143 * might do a considerable amount of work finding the next commit only
4144 * for us to throw it away.
4146 * If it is non-zero, then either we don't have a max_count at all
4147 * (-1), or it is still counting, in which case we decrement.
4149 if (revs->max_count) {
4150 c = get_revision_1(revs);
4151 if (c) {
4152 while (revs->skip_count > 0) {
4153 revs->skip_count--;
4154 c = get_revision_1(revs);
4155 if (!c)
4156 break;
4160 if (revs->max_count > 0)
4161 revs->max_count--;
4164 if (c)
4165 c->object.flags |= SHOWN;
4167 if (!revs->boundary)
4168 return c;
4170 if (!c) {
4172 * get_revision_1() runs out the commits, and
4173 * we are done computing the boundaries.
4174 * switch to boundary commits output mode.
4176 revs->boundary = 2;
4179 * Update revs->commits to contain the list of
4180 * boundary commits.
4182 create_boundary_commit_list(revs);
4184 return get_revision_internal(revs);
4188 * boundary commits are the commits that are parents of the
4189 * ones we got from get_revision_1() but they themselves are
4190 * not returned from get_revision_1(). Before returning
4191 * 'c', we need to mark its parents that they could be boundaries.
4194 for (l = c->parents; l; l = l->next) {
4195 struct object *p;
4196 p = &(l->item->object);
4197 if (p->flags & (CHILD_SHOWN | SHOWN))
4198 continue;
4199 p->flags |= CHILD_SHOWN;
4200 gc_boundary(&revs->boundary_commits);
4201 add_object_array(p, NULL, &revs->boundary_commits);
4204 return c;
4207 struct commit *get_revision(struct rev_info *revs)
4209 struct commit *c;
4210 struct commit_list *reversed;
4212 if (revs->reverse) {
4213 reversed = NULL;
4214 while ((c = get_revision_internal(revs)))
4215 commit_list_insert(c, &reversed);
4216 revs->commits = reversed;
4217 revs->reverse = 0;
4218 revs->reverse_output_stage = 1;
4221 if (revs->reverse_output_stage) {
4222 c = pop_commit(&revs->commits);
4223 if (revs->track_linear)
4224 revs->linear = !!(c && c->object.flags & TRACK_LINEAR);
4225 return c;
4228 c = get_revision_internal(revs);
4229 if (c && revs->graph)
4230 graph_update(revs->graph, c);
4231 if (!c) {
4232 free_saved_parents(revs);
4233 if (revs->previous_parents) {
4234 free_commit_list(revs->previous_parents);
4235 revs->previous_parents = NULL;
4238 return c;
4241 const char *get_revision_mark(const struct rev_info *revs, const struct commit *commit)
4243 if (commit->object.flags & BOUNDARY)
4244 return "-";
4245 else if (commit->object.flags & UNINTERESTING)
4246 return "^";
4247 else if (commit->object.flags & PATCHSAME)
4248 return "=";
4249 else if (!revs || revs->left_right) {
4250 if (commit->object.flags & SYMMETRIC_LEFT)
4251 return "<";
4252 else
4253 return ">";
4254 } else if (revs->graph)
4255 return "*";
4256 else if (revs->cherry_mark)
4257 return "+";
4258 return "";
4261 void put_revision_mark(const struct rev_info *revs, const struct commit *commit)
4263 const char *mark = get_revision_mark(revs, commit);
4264 if (!strlen(mark))
4265 return;
4266 fputs(mark, stdout);
4267 putchar(' ');