t1092: stress test 'git sparse-checkout set'
[git/debian.git] / revision.c
blob7d435f80480ef1b7684fdc43b51057d20e79966f
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 date = commit->date;
1444 p = &commit_list_insert(commit, p)->next;
1446 show = show_early_output;
1447 if (!show)
1448 continue;
1450 show(revs, newlist);
1451 show_early_output = NULL;
1453 if (revs->cherry_pick || revs->cherry_mark)
1454 cherry_pick_list(newlist, revs);
1456 if (revs->left_only || revs->right_only)
1457 limit_left_right(newlist, revs);
1459 if (bottom) {
1460 limit_to_ancestry(bottom, newlist);
1461 free_commit_list(bottom);
1465 * Check if any commits have become TREESAME by some of their parents
1466 * becoming UNINTERESTING.
1468 if (limiting_can_increase_treesame(revs)) {
1469 struct commit_list *list = NULL;
1470 for (list = newlist; list; list = list->next) {
1471 struct commit *c = list->item;
1472 if (c->object.flags & (UNINTERESTING | TREESAME))
1473 continue;
1474 update_treesame(revs, c);
1478 free_commit_list(original_list);
1479 revs->commits = newlist;
1480 return 0;
1484 * Add an entry to refs->cmdline with the specified information.
1485 * *name is copied.
1487 static void add_rev_cmdline(struct rev_info *revs,
1488 struct object *item,
1489 const char *name,
1490 int whence,
1491 unsigned flags)
1493 struct rev_cmdline_info *info = &revs->cmdline;
1494 unsigned int nr = info->nr;
1496 ALLOC_GROW(info->rev, nr + 1, info->alloc);
1497 info->rev[nr].item = item;
1498 info->rev[nr].name = xstrdup(name);
1499 info->rev[nr].whence = whence;
1500 info->rev[nr].flags = flags;
1501 info->nr++;
1504 static void add_rev_cmdline_list(struct rev_info *revs,
1505 struct commit_list *commit_list,
1506 int whence,
1507 unsigned flags)
1509 while (commit_list) {
1510 struct object *object = &commit_list->item->object;
1511 add_rev_cmdline(revs, object, oid_to_hex(&object->oid),
1512 whence, flags);
1513 commit_list = commit_list->next;
1517 struct all_refs_cb {
1518 int all_flags;
1519 int warned_bad_reflog;
1520 struct rev_info *all_revs;
1521 const char *name_for_errormsg;
1522 struct worktree *wt;
1525 int ref_excluded(struct string_list *ref_excludes, const char *path)
1527 struct string_list_item *item;
1529 if (!ref_excludes)
1530 return 0;
1531 for_each_string_list_item(item, ref_excludes) {
1532 if (!wildmatch(item->string, path, 0))
1533 return 1;
1535 return 0;
1538 static int handle_one_ref(const char *path, const struct object_id *oid,
1539 int flag, void *cb_data)
1541 struct all_refs_cb *cb = cb_data;
1542 struct object *object;
1544 if (ref_excluded(cb->all_revs->ref_excludes, path))
1545 return 0;
1547 object = get_reference(cb->all_revs, path, oid, cb->all_flags);
1548 add_rev_cmdline(cb->all_revs, object, path, REV_CMD_REF, cb->all_flags);
1549 add_pending_object(cb->all_revs, object, path);
1550 return 0;
1553 static void init_all_refs_cb(struct all_refs_cb *cb, struct rev_info *revs,
1554 unsigned flags)
1556 cb->all_revs = revs;
1557 cb->all_flags = flags;
1558 revs->rev_input_given = 1;
1559 cb->wt = NULL;
1562 void clear_ref_exclusion(struct string_list **ref_excludes_p)
1564 if (*ref_excludes_p) {
1565 string_list_clear(*ref_excludes_p, 0);
1566 free(*ref_excludes_p);
1568 *ref_excludes_p = NULL;
1571 void add_ref_exclusion(struct string_list **ref_excludes_p, const char *exclude)
1573 if (!*ref_excludes_p) {
1574 CALLOC_ARRAY(*ref_excludes_p, 1);
1575 (*ref_excludes_p)->strdup_strings = 1;
1577 string_list_append(*ref_excludes_p, exclude);
1580 static void handle_refs(struct ref_store *refs,
1581 struct rev_info *revs, unsigned flags,
1582 int (*for_each)(struct ref_store *, each_ref_fn, void *))
1584 struct all_refs_cb cb;
1586 if (!refs) {
1587 /* this could happen with uninitialized submodules */
1588 return;
1591 init_all_refs_cb(&cb, revs, flags);
1592 for_each(refs, handle_one_ref, &cb);
1595 static void handle_one_reflog_commit(struct object_id *oid, void *cb_data)
1597 struct all_refs_cb *cb = cb_data;
1598 if (!is_null_oid(oid)) {
1599 struct object *o = parse_object(cb->all_revs->repo, oid);
1600 if (o) {
1601 o->flags |= cb->all_flags;
1602 /* ??? CMDLINEFLAGS ??? */
1603 add_pending_object(cb->all_revs, o, "");
1605 else if (!cb->warned_bad_reflog) {
1606 warning("reflog of '%s' references pruned commits",
1607 cb->name_for_errormsg);
1608 cb->warned_bad_reflog = 1;
1613 static int handle_one_reflog_ent(struct object_id *ooid, struct object_id *noid,
1614 const char *email, timestamp_t timestamp, int tz,
1615 const char *message, void *cb_data)
1617 handle_one_reflog_commit(ooid, cb_data);
1618 handle_one_reflog_commit(noid, cb_data);
1619 return 0;
1622 static int handle_one_reflog(const char *refname_in_wt,
1623 const struct object_id *oid,
1624 int flag, void *cb_data)
1626 struct all_refs_cb *cb = cb_data;
1627 struct strbuf refname = STRBUF_INIT;
1629 cb->warned_bad_reflog = 0;
1630 strbuf_worktree_ref(cb->wt, &refname, refname_in_wt);
1631 cb->name_for_errormsg = refname.buf;
1632 refs_for_each_reflog_ent(get_main_ref_store(the_repository),
1633 refname.buf,
1634 handle_one_reflog_ent, cb_data);
1635 strbuf_release(&refname);
1636 return 0;
1639 static void add_other_reflogs_to_pending(struct all_refs_cb *cb)
1641 struct worktree **worktrees, **p;
1643 worktrees = get_worktrees();
1644 for (p = worktrees; *p; p++) {
1645 struct worktree *wt = *p;
1647 if (wt->is_current)
1648 continue;
1650 cb->wt = wt;
1651 refs_for_each_reflog(get_worktree_ref_store(wt),
1652 handle_one_reflog,
1653 cb);
1655 free_worktrees(worktrees);
1658 void add_reflogs_to_pending(struct rev_info *revs, unsigned flags)
1660 struct all_refs_cb cb;
1662 cb.all_revs = revs;
1663 cb.all_flags = flags;
1664 cb.wt = NULL;
1665 for_each_reflog(handle_one_reflog, &cb);
1667 if (!revs->single_worktree)
1668 add_other_reflogs_to_pending(&cb);
1671 static void add_cache_tree(struct cache_tree *it, struct rev_info *revs,
1672 struct strbuf *path, unsigned int flags)
1674 size_t baselen = path->len;
1675 int i;
1677 if (it->entry_count >= 0) {
1678 struct tree *tree = lookup_tree(revs->repo, &it->oid);
1679 tree->object.flags |= flags;
1680 add_pending_object_with_path(revs, &tree->object, "",
1681 040000, path->buf);
1684 for (i = 0; i < it->subtree_nr; i++) {
1685 struct cache_tree_sub *sub = it->down[i];
1686 strbuf_addf(path, "%s%s", baselen ? "/" : "", sub->name);
1687 add_cache_tree(sub->cache_tree, revs, path, flags);
1688 strbuf_setlen(path, baselen);
1693 static void do_add_index_objects_to_pending(struct rev_info *revs,
1694 struct index_state *istate,
1695 unsigned int flags)
1697 int i;
1699 /* TODO: audit for interaction with sparse-index. */
1700 ensure_full_index(istate);
1701 for (i = 0; i < istate->cache_nr; i++) {
1702 struct cache_entry *ce = istate->cache[i];
1703 struct blob *blob;
1705 if (S_ISGITLINK(ce->ce_mode))
1706 continue;
1708 blob = lookup_blob(revs->repo, &ce->oid);
1709 if (!blob)
1710 die("unable to add index blob to traversal");
1711 blob->object.flags |= flags;
1712 add_pending_object_with_path(revs, &blob->object, "",
1713 ce->ce_mode, ce->name);
1716 if (istate->cache_tree) {
1717 struct strbuf path = STRBUF_INIT;
1718 add_cache_tree(istate->cache_tree, revs, &path, flags);
1719 strbuf_release(&path);
1723 void add_index_objects_to_pending(struct rev_info *revs, unsigned int flags)
1725 struct worktree **worktrees, **p;
1727 repo_read_index(revs->repo);
1728 do_add_index_objects_to_pending(revs, revs->repo->index, flags);
1730 if (revs->single_worktree)
1731 return;
1733 worktrees = get_worktrees();
1734 for (p = worktrees; *p; p++) {
1735 struct worktree *wt = *p;
1736 struct index_state istate = { NULL };
1738 if (wt->is_current)
1739 continue; /* current index already taken care of */
1741 if (read_index_from(&istate,
1742 worktree_git_path(wt, "index"),
1743 get_worktree_git_dir(wt)) > 0)
1744 do_add_index_objects_to_pending(revs, &istate, flags);
1745 discard_index(&istate);
1747 free_worktrees(worktrees);
1750 struct add_alternate_refs_data {
1751 struct rev_info *revs;
1752 unsigned int flags;
1755 static void add_one_alternate_ref(const struct object_id *oid,
1756 void *vdata)
1758 const char *name = ".alternate";
1759 struct add_alternate_refs_data *data = vdata;
1760 struct object *obj;
1762 obj = get_reference(data->revs, name, oid, data->flags);
1763 add_rev_cmdline(data->revs, obj, name, REV_CMD_REV, data->flags);
1764 add_pending_object(data->revs, obj, name);
1767 static void add_alternate_refs_to_pending(struct rev_info *revs,
1768 unsigned int flags)
1770 struct add_alternate_refs_data data;
1771 data.revs = revs;
1772 data.flags = flags;
1773 for_each_alternate_ref(add_one_alternate_ref, &data);
1776 static int add_parents_only(struct rev_info *revs, const char *arg_, int flags,
1777 int exclude_parent)
1779 struct object_id oid;
1780 struct object *it;
1781 struct commit *commit;
1782 struct commit_list *parents;
1783 int parent_number;
1784 const char *arg = arg_;
1786 if (*arg == '^') {
1787 flags ^= UNINTERESTING | BOTTOM;
1788 arg++;
1790 if (get_oid_committish(arg, &oid))
1791 return 0;
1792 while (1) {
1793 it = get_reference(revs, arg, &oid, 0);
1794 if (!it && revs->ignore_missing)
1795 return 0;
1796 if (it->type != OBJ_TAG)
1797 break;
1798 if (!((struct tag*)it)->tagged)
1799 return 0;
1800 oidcpy(&oid, &((struct tag*)it)->tagged->oid);
1802 if (it->type != OBJ_COMMIT)
1803 return 0;
1804 commit = (struct commit *)it;
1805 if (exclude_parent &&
1806 exclude_parent > commit_list_count(commit->parents))
1807 return 0;
1808 for (parents = commit->parents, parent_number = 1;
1809 parents;
1810 parents = parents->next, parent_number++) {
1811 if (exclude_parent && parent_number != exclude_parent)
1812 continue;
1814 it = &parents->item->object;
1815 it->flags |= flags;
1816 add_rev_cmdline(revs, it, arg_, REV_CMD_PARENTS_ONLY, flags);
1817 add_pending_object(revs, it, arg);
1819 return 1;
1822 void repo_init_revisions(struct repository *r,
1823 struct rev_info *revs,
1824 const char *prefix)
1826 memset(revs, 0, sizeof(*revs));
1828 revs->repo = r;
1829 revs->abbrev = DEFAULT_ABBREV;
1830 revs->simplify_history = 1;
1831 revs->pruning.repo = r;
1832 revs->pruning.flags.recursive = 1;
1833 revs->pruning.flags.quick = 1;
1834 revs->pruning.add_remove = file_add_remove;
1835 revs->pruning.change = file_change;
1836 revs->pruning.change_fn_data = revs;
1837 revs->sort_order = REV_SORT_IN_GRAPH_ORDER;
1838 revs->dense = 1;
1839 revs->prefix = prefix;
1840 revs->max_age = -1;
1841 revs->min_age = -1;
1842 revs->skip_count = -1;
1843 revs->max_count = -1;
1844 revs->max_parents = -1;
1845 revs->expand_tabs_in_log = -1;
1847 revs->commit_format = CMIT_FMT_DEFAULT;
1848 revs->expand_tabs_in_log_default = 8;
1850 grep_init(&revs->grep_filter, revs->repo);
1851 revs->grep_filter.status_only = 1;
1853 repo_diff_setup(revs->repo, &revs->diffopt);
1854 if (prefix && !revs->diffopt.prefix) {
1855 revs->diffopt.prefix = prefix;
1856 revs->diffopt.prefix_length = strlen(prefix);
1859 init_display_notes(&revs->notes_opt);
1862 static void add_pending_commit_list(struct rev_info *revs,
1863 struct commit_list *commit_list,
1864 unsigned int flags)
1866 while (commit_list) {
1867 struct object *object = &commit_list->item->object;
1868 object->flags |= flags;
1869 add_pending_object(revs, object, oid_to_hex(&object->oid));
1870 commit_list = commit_list->next;
1874 static void prepare_show_merge(struct rev_info *revs)
1876 struct commit_list *bases;
1877 struct commit *head, *other;
1878 struct object_id oid;
1879 const char **prune = NULL;
1880 int i, prune_num = 1; /* counting terminating NULL */
1881 struct index_state *istate = revs->repo->index;
1883 if (get_oid("HEAD", &oid))
1884 die("--merge without HEAD?");
1885 head = lookup_commit_or_die(&oid, "HEAD");
1886 if (get_oid("MERGE_HEAD", &oid))
1887 die("--merge without MERGE_HEAD?");
1888 other = lookup_commit_or_die(&oid, "MERGE_HEAD");
1889 add_pending_object(revs, &head->object, "HEAD");
1890 add_pending_object(revs, &other->object, "MERGE_HEAD");
1891 bases = get_merge_bases(head, other);
1892 add_rev_cmdline_list(revs, bases, REV_CMD_MERGE_BASE, UNINTERESTING | BOTTOM);
1893 add_pending_commit_list(revs, bases, UNINTERESTING | BOTTOM);
1894 free_commit_list(bases);
1895 head->object.flags |= SYMMETRIC_LEFT;
1897 if (!istate->cache_nr)
1898 repo_read_index(revs->repo);
1899 for (i = 0; i < istate->cache_nr; i++) {
1900 const struct cache_entry *ce = istate->cache[i];
1901 if (!ce_stage(ce))
1902 continue;
1903 if (ce_path_match(istate, ce, &revs->prune_data, NULL)) {
1904 prune_num++;
1905 REALLOC_ARRAY(prune, prune_num);
1906 prune[prune_num-2] = ce->name;
1907 prune[prune_num-1] = NULL;
1909 while ((i+1 < istate->cache_nr) &&
1910 ce_same_name(ce, istate->cache[i+1]))
1911 i++;
1913 clear_pathspec(&revs->prune_data);
1914 parse_pathspec(&revs->prune_data, PATHSPEC_ALL_MAGIC & ~PATHSPEC_LITERAL,
1915 PATHSPEC_PREFER_FULL | PATHSPEC_LITERAL_PATH, "", prune);
1916 revs->limited = 1;
1919 static int dotdot_missing(const char *arg, char *dotdot,
1920 struct rev_info *revs, int symmetric)
1922 if (revs->ignore_missing)
1923 return 0;
1924 /* de-munge so we report the full argument */
1925 *dotdot = '.';
1926 die(symmetric
1927 ? "Invalid symmetric difference expression %s"
1928 : "Invalid revision range %s", arg);
1931 static int handle_dotdot_1(const char *arg, char *dotdot,
1932 struct rev_info *revs, int flags,
1933 int cant_be_filename,
1934 struct object_context *a_oc,
1935 struct object_context *b_oc)
1937 const char *a_name, *b_name;
1938 struct object_id a_oid, b_oid;
1939 struct object *a_obj, *b_obj;
1940 unsigned int a_flags, b_flags;
1941 int symmetric = 0;
1942 unsigned int flags_exclude = flags ^ (UNINTERESTING | BOTTOM);
1943 unsigned int oc_flags = GET_OID_COMMITTISH | GET_OID_RECORD_PATH;
1945 a_name = arg;
1946 if (!*a_name)
1947 a_name = "HEAD";
1949 b_name = dotdot + 2;
1950 if (*b_name == '.') {
1951 symmetric = 1;
1952 b_name++;
1954 if (!*b_name)
1955 b_name = "HEAD";
1957 if (get_oid_with_context(revs->repo, a_name, oc_flags, &a_oid, a_oc) ||
1958 get_oid_with_context(revs->repo, b_name, oc_flags, &b_oid, b_oc))
1959 return -1;
1961 if (!cant_be_filename) {
1962 *dotdot = '.';
1963 verify_non_filename(revs->prefix, arg);
1964 *dotdot = '\0';
1967 a_obj = parse_object(revs->repo, &a_oid);
1968 b_obj = parse_object(revs->repo, &b_oid);
1969 if (!a_obj || !b_obj)
1970 return dotdot_missing(arg, dotdot, revs, symmetric);
1972 if (!symmetric) {
1973 /* just A..B */
1974 b_flags = flags;
1975 a_flags = flags_exclude;
1976 } else {
1977 /* A...B -- find merge bases between the two */
1978 struct commit *a, *b;
1979 struct commit_list *exclude;
1981 a = lookup_commit_reference(revs->repo, &a_obj->oid);
1982 b = lookup_commit_reference(revs->repo, &b_obj->oid);
1983 if (!a || !b)
1984 return dotdot_missing(arg, dotdot, revs, symmetric);
1986 exclude = get_merge_bases(a, b);
1987 add_rev_cmdline_list(revs, exclude, REV_CMD_MERGE_BASE,
1988 flags_exclude);
1989 add_pending_commit_list(revs, exclude, flags_exclude);
1990 free_commit_list(exclude);
1992 b_flags = flags;
1993 a_flags = flags | SYMMETRIC_LEFT;
1996 a_obj->flags |= a_flags;
1997 b_obj->flags |= b_flags;
1998 add_rev_cmdline(revs, a_obj, a_name, REV_CMD_LEFT, a_flags);
1999 add_rev_cmdline(revs, b_obj, b_name, REV_CMD_RIGHT, b_flags);
2000 add_pending_object_with_path(revs, a_obj, a_name, a_oc->mode, a_oc->path);
2001 add_pending_object_with_path(revs, b_obj, b_name, b_oc->mode, b_oc->path);
2002 return 0;
2005 static int handle_dotdot(const char *arg,
2006 struct rev_info *revs, int flags,
2007 int cant_be_filename)
2009 struct object_context a_oc, b_oc;
2010 char *dotdot = strstr(arg, "..");
2011 int ret;
2013 if (!dotdot)
2014 return -1;
2016 memset(&a_oc, 0, sizeof(a_oc));
2017 memset(&b_oc, 0, sizeof(b_oc));
2019 *dotdot = '\0';
2020 ret = handle_dotdot_1(arg, dotdot, revs, flags, cant_be_filename,
2021 &a_oc, &b_oc);
2022 *dotdot = '.';
2024 free(a_oc.path);
2025 free(b_oc.path);
2027 return ret;
2030 static int handle_revision_arg_1(const char *arg_, struct rev_info *revs, int flags, unsigned revarg_opt)
2032 struct object_context oc;
2033 char *mark;
2034 struct object *object;
2035 struct object_id oid;
2036 int local_flags;
2037 const char *arg = arg_;
2038 int cant_be_filename = revarg_opt & REVARG_CANNOT_BE_FILENAME;
2039 unsigned get_sha1_flags = GET_OID_RECORD_PATH;
2041 flags = flags & UNINTERESTING ? flags | BOTTOM : flags & ~BOTTOM;
2043 if (!cant_be_filename && !strcmp(arg, "..")) {
2045 * Just ".."? That is not a range but the
2046 * pathspec for the parent directory.
2048 return -1;
2051 if (!handle_dotdot(arg, revs, flags, revarg_opt))
2052 return 0;
2054 mark = strstr(arg, "^@");
2055 if (mark && !mark[2]) {
2056 *mark = 0;
2057 if (add_parents_only(revs, arg, flags, 0))
2058 return 0;
2059 *mark = '^';
2061 mark = strstr(arg, "^!");
2062 if (mark && !mark[2]) {
2063 *mark = 0;
2064 if (!add_parents_only(revs, arg, flags ^ (UNINTERESTING | BOTTOM), 0))
2065 *mark = '^';
2067 mark = strstr(arg, "^-");
2068 if (mark) {
2069 int exclude_parent = 1;
2071 if (mark[2]) {
2072 char *end;
2073 exclude_parent = strtoul(mark + 2, &end, 10);
2074 if (*end != '\0' || !exclude_parent)
2075 return -1;
2078 *mark = 0;
2079 if (!add_parents_only(revs, arg, flags ^ (UNINTERESTING | BOTTOM), exclude_parent))
2080 *mark = '^';
2083 local_flags = 0;
2084 if (*arg == '^') {
2085 local_flags = UNINTERESTING | BOTTOM;
2086 arg++;
2089 if (revarg_opt & REVARG_COMMITTISH)
2090 get_sha1_flags |= GET_OID_COMMITTISH;
2092 if (get_oid_with_context(revs->repo, arg, get_sha1_flags, &oid, &oc))
2093 return revs->ignore_missing ? 0 : -1;
2094 if (!cant_be_filename)
2095 verify_non_filename(revs->prefix, arg);
2096 object = get_reference(revs, arg, &oid, flags ^ local_flags);
2097 if (!object)
2098 return revs->ignore_missing ? 0 : -1;
2099 add_rev_cmdline(revs, object, arg_, REV_CMD_REV, flags ^ local_flags);
2100 add_pending_object_with_path(revs, object, arg, oc.mode, oc.path);
2101 free(oc.path);
2102 return 0;
2105 int handle_revision_arg(const char *arg, struct rev_info *revs, int flags, unsigned revarg_opt)
2107 int ret = handle_revision_arg_1(arg, revs, flags, revarg_opt);
2108 if (!ret)
2109 revs->rev_input_given = 1;
2110 return ret;
2113 static void read_pathspec_from_stdin(struct strbuf *sb,
2114 struct strvec *prune)
2116 while (strbuf_getline(sb, stdin) != EOF)
2117 strvec_push(prune, sb->buf);
2120 static void read_revisions_from_stdin(struct rev_info *revs,
2121 struct strvec *prune)
2123 struct strbuf sb;
2124 int seen_dashdash = 0;
2125 int save_warning;
2127 save_warning = warn_on_object_refname_ambiguity;
2128 warn_on_object_refname_ambiguity = 0;
2130 strbuf_init(&sb, 1000);
2131 while (strbuf_getline(&sb, stdin) != EOF) {
2132 int len = sb.len;
2133 if (!len)
2134 break;
2135 if (sb.buf[0] == '-') {
2136 if (len == 2 && sb.buf[1] == '-') {
2137 seen_dashdash = 1;
2138 break;
2140 die("options not supported in --stdin mode");
2142 if (handle_revision_arg(sb.buf, revs, 0,
2143 REVARG_CANNOT_BE_FILENAME))
2144 die("bad revision '%s'", sb.buf);
2146 if (seen_dashdash)
2147 read_pathspec_from_stdin(&sb, prune);
2149 strbuf_release(&sb);
2150 warn_on_object_refname_ambiguity = save_warning;
2153 static void add_grep(struct rev_info *revs, const char *ptn, enum grep_pat_token what)
2155 append_grep_pattern(&revs->grep_filter, ptn, "command line", 0, what);
2158 static void add_header_grep(struct rev_info *revs, enum grep_header_field field, const char *pattern)
2160 append_header_grep_pattern(&revs->grep_filter, field, pattern);
2163 static void add_message_grep(struct rev_info *revs, const char *pattern)
2165 add_grep(revs, pattern, GREP_PATTERN_BODY);
2168 static int handle_revision_opt(struct rev_info *revs, int argc, const char **argv,
2169 int *unkc, const char **unkv,
2170 const struct setup_revision_opt* opt)
2172 const char *arg = argv[0];
2173 const char *optarg;
2174 int argcount;
2175 const unsigned hexsz = the_hash_algo->hexsz;
2177 /* pseudo revision arguments */
2178 if (!strcmp(arg, "--all") || !strcmp(arg, "--branches") ||
2179 !strcmp(arg, "--tags") || !strcmp(arg, "--remotes") ||
2180 !strcmp(arg, "--reflog") || !strcmp(arg, "--not") ||
2181 !strcmp(arg, "--no-walk") || !strcmp(arg, "--do-walk") ||
2182 !strcmp(arg, "--bisect") || starts_with(arg, "--glob=") ||
2183 !strcmp(arg, "--indexed-objects") ||
2184 !strcmp(arg, "--alternate-refs") ||
2185 starts_with(arg, "--exclude=") ||
2186 starts_with(arg, "--branches=") || starts_with(arg, "--tags=") ||
2187 starts_with(arg, "--remotes=") || starts_with(arg, "--no-walk="))
2189 unkv[(*unkc)++] = arg;
2190 return 1;
2193 if ((argcount = parse_long_opt("max-count", argv, &optarg))) {
2194 revs->max_count = atoi(optarg);
2195 revs->no_walk = 0;
2196 return argcount;
2197 } else if ((argcount = parse_long_opt("skip", argv, &optarg))) {
2198 revs->skip_count = atoi(optarg);
2199 return argcount;
2200 } else if ((*arg == '-') && isdigit(arg[1])) {
2201 /* accept -<digit>, like traditional "head" */
2202 if (strtol_i(arg + 1, 10, &revs->max_count) < 0 ||
2203 revs->max_count < 0)
2204 die("'%s': not a non-negative integer", arg + 1);
2205 revs->no_walk = 0;
2206 } else if (!strcmp(arg, "-n")) {
2207 if (argc <= 1)
2208 return error("-n requires an argument");
2209 revs->max_count = atoi(argv[1]);
2210 revs->no_walk = 0;
2211 return 2;
2212 } else if (skip_prefix(arg, "-n", &optarg)) {
2213 revs->max_count = atoi(optarg);
2214 revs->no_walk = 0;
2215 } else if ((argcount = parse_long_opt("max-age", argv, &optarg))) {
2216 revs->max_age = atoi(optarg);
2217 return argcount;
2218 } else if ((argcount = parse_long_opt("since", argv, &optarg))) {
2219 revs->max_age = approxidate(optarg);
2220 return argcount;
2221 } else if ((argcount = parse_long_opt("after", argv, &optarg))) {
2222 revs->max_age = approxidate(optarg);
2223 return argcount;
2224 } else if ((argcount = parse_long_opt("min-age", argv, &optarg))) {
2225 revs->min_age = atoi(optarg);
2226 return argcount;
2227 } else if ((argcount = parse_long_opt("before", argv, &optarg))) {
2228 revs->min_age = approxidate(optarg);
2229 return argcount;
2230 } else if ((argcount = parse_long_opt("until", argv, &optarg))) {
2231 revs->min_age = approxidate(optarg);
2232 return argcount;
2233 } else if (!strcmp(arg, "--first-parent")) {
2234 revs->first_parent_only = 1;
2235 } else if (!strcmp(arg, "--exclude-first-parent-only")) {
2236 revs->exclude_first_parent_only = 1;
2237 } else if (!strcmp(arg, "--ancestry-path")) {
2238 revs->ancestry_path = 1;
2239 revs->simplify_history = 0;
2240 revs->limited = 1;
2241 } else if (!strcmp(arg, "-g") || !strcmp(arg, "--walk-reflogs")) {
2242 init_reflog_walk(&revs->reflog_info);
2243 } else if (!strcmp(arg, "--default")) {
2244 if (argc <= 1)
2245 return error("bad --default argument");
2246 revs->def = argv[1];
2247 return 2;
2248 } else if (!strcmp(arg, "--merge")) {
2249 revs->show_merge = 1;
2250 } else if (!strcmp(arg, "--topo-order")) {
2251 revs->sort_order = REV_SORT_IN_GRAPH_ORDER;
2252 revs->topo_order = 1;
2253 } else if (!strcmp(arg, "--simplify-merges")) {
2254 revs->simplify_merges = 1;
2255 revs->topo_order = 1;
2256 revs->rewrite_parents = 1;
2257 revs->simplify_history = 0;
2258 revs->limited = 1;
2259 } else if (!strcmp(arg, "--simplify-by-decoration")) {
2260 revs->simplify_merges = 1;
2261 revs->topo_order = 1;
2262 revs->rewrite_parents = 1;
2263 revs->simplify_history = 0;
2264 revs->simplify_by_decoration = 1;
2265 revs->limited = 1;
2266 revs->prune = 1;
2267 } else if (!strcmp(arg, "--date-order")) {
2268 revs->sort_order = REV_SORT_BY_COMMIT_DATE;
2269 revs->topo_order = 1;
2270 } else if (!strcmp(arg, "--author-date-order")) {
2271 revs->sort_order = REV_SORT_BY_AUTHOR_DATE;
2272 revs->topo_order = 1;
2273 } else if (!strcmp(arg, "--early-output")) {
2274 revs->early_output = 100;
2275 revs->topo_order = 1;
2276 } else if (skip_prefix(arg, "--early-output=", &optarg)) {
2277 if (strtoul_ui(optarg, 10, &revs->early_output) < 0)
2278 die("'%s': not a non-negative integer", optarg);
2279 revs->topo_order = 1;
2280 } else if (!strcmp(arg, "--parents")) {
2281 revs->rewrite_parents = 1;
2282 revs->print_parents = 1;
2283 } else if (!strcmp(arg, "--dense")) {
2284 revs->dense = 1;
2285 } else if (!strcmp(arg, "--sparse")) {
2286 revs->dense = 0;
2287 } else if (!strcmp(arg, "--in-commit-order")) {
2288 revs->tree_blobs_in_commit_order = 1;
2289 } else if (!strcmp(arg, "--remove-empty")) {
2290 revs->remove_empty_trees = 1;
2291 } else if (!strcmp(arg, "--merges")) {
2292 revs->min_parents = 2;
2293 } else if (!strcmp(arg, "--no-merges")) {
2294 revs->max_parents = 1;
2295 } else if (skip_prefix(arg, "--min-parents=", &optarg)) {
2296 revs->min_parents = atoi(optarg);
2297 } else if (!strcmp(arg, "--no-min-parents")) {
2298 revs->min_parents = 0;
2299 } else if (skip_prefix(arg, "--max-parents=", &optarg)) {
2300 revs->max_parents = atoi(optarg);
2301 } else if (!strcmp(arg, "--no-max-parents")) {
2302 revs->max_parents = -1;
2303 } else if (!strcmp(arg, "--boundary")) {
2304 revs->boundary = 1;
2305 } else if (!strcmp(arg, "--left-right")) {
2306 revs->left_right = 1;
2307 } else if (!strcmp(arg, "--left-only")) {
2308 if (revs->right_only)
2309 die("--left-only is incompatible with --right-only"
2310 " or --cherry");
2311 revs->left_only = 1;
2312 } else if (!strcmp(arg, "--right-only")) {
2313 if (revs->left_only)
2314 die(_("options '%s' and '%s' cannot be used together"), "--right-only", "--left-only");
2315 revs->right_only = 1;
2316 } else if (!strcmp(arg, "--cherry")) {
2317 if (revs->left_only)
2318 die(_("options '%s' and '%s' cannot be used together"), "--cherry", "--left-only");
2319 revs->cherry_mark = 1;
2320 revs->right_only = 1;
2321 revs->max_parents = 1;
2322 revs->limited = 1;
2323 } else if (!strcmp(arg, "--count")) {
2324 revs->count = 1;
2325 } else if (!strcmp(arg, "--cherry-mark")) {
2326 if (revs->cherry_pick)
2327 die(_("options '%s' and '%s' cannot be used together"), "--cherry-mark", "--cherry-pick");
2328 revs->cherry_mark = 1;
2329 revs->limited = 1; /* needs limit_list() */
2330 } else if (!strcmp(arg, "--cherry-pick")) {
2331 if (revs->cherry_mark)
2332 die(_("options '%s' and '%s' cannot be used together"), "--cherry-pick", "--cherry-mark");
2333 revs->cherry_pick = 1;
2334 revs->limited = 1;
2335 } else if (!strcmp(arg, "--objects")) {
2336 revs->tag_objects = 1;
2337 revs->tree_objects = 1;
2338 revs->blob_objects = 1;
2339 } else if (!strcmp(arg, "--objects-edge")) {
2340 revs->tag_objects = 1;
2341 revs->tree_objects = 1;
2342 revs->blob_objects = 1;
2343 revs->edge_hint = 1;
2344 } else if (!strcmp(arg, "--objects-edge-aggressive")) {
2345 revs->tag_objects = 1;
2346 revs->tree_objects = 1;
2347 revs->blob_objects = 1;
2348 revs->edge_hint = 1;
2349 revs->edge_hint_aggressive = 1;
2350 } else if (!strcmp(arg, "--verify-objects")) {
2351 revs->tag_objects = 1;
2352 revs->tree_objects = 1;
2353 revs->blob_objects = 1;
2354 revs->verify_objects = 1;
2355 } else if (!strcmp(arg, "--unpacked")) {
2356 revs->unpacked = 1;
2357 } else if (starts_with(arg, "--unpacked=")) {
2358 die(_("--unpacked=<packfile> no longer supported"));
2359 } else if (!strcmp(arg, "--no-kept-objects")) {
2360 revs->no_kept_objects = 1;
2361 revs->keep_pack_cache_flags |= IN_CORE_KEEP_PACKS;
2362 revs->keep_pack_cache_flags |= ON_DISK_KEEP_PACKS;
2363 } else if (skip_prefix(arg, "--no-kept-objects=", &optarg)) {
2364 revs->no_kept_objects = 1;
2365 if (!strcmp(optarg, "in-core"))
2366 revs->keep_pack_cache_flags |= IN_CORE_KEEP_PACKS;
2367 if (!strcmp(optarg, "on-disk"))
2368 revs->keep_pack_cache_flags |= ON_DISK_KEEP_PACKS;
2369 } else if (!strcmp(arg, "-r")) {
2370 revs->diff = 1;
2371 revs->diffopt.flags.recursive = 1;
2372 } else if (!strcmp(arg, "-t")) {
2373 revs->diff = 1;
2374 revs->diffopt.flags.recursive = 1;
2375 revs->diffopt.flags.tree_in_recursive = 1;
2376 } else if ((argcount = diff_merges_parse_opts(revs, argv))) {
2377 return argcount;
2378 } else if (!strcmp(arg, "-v")) {
2379 revs->verbose_header = 1;
2380 } else if (!strcmp(arg, "--pretty")) {
2381 revs->verbose_header = 1;
2382 revs->pretty_given = 1;
2383 get_commit_format(NULL, revs);
2384 } else if (skip_prefix(arg, "--pretty=", &optarg) ||
2385 skip_prefix(arg, "--format=", &optarg)) {
2387 * Detached form ("--pretty X" as opposed to "--pretty=X")
2388 * not allowed, since the argument is optional.
2390 revs->verbose_header = 1;
2391 revs->pretty_given = 1;
2392 get_commit_format(optarg, revs);
2393 } else if (!strcmp(arg, "--expand-tabs")) {
2394 revs->expand_tabs_in_log = 8;
2395 } else if (!strcmp(arg, "--no-expand-tabs")) {
2396 revs->expand_tabs_in_log = 0;
2397 } else if (skip_prefix(arg, "--expand-tabs=", &arg)) {
2398 int val;
2399 if (strtol_i(arg, 10, &val) < 0 || val < 0)
2400 die("'%s': not a non-negative integer", arg);
2401 revs->expand_tabs_in_log = val;
2402 } else if (!strcmp(arg, "--show-notes") || !strcmp(arg, "--notes")) {
2403 enable_default_display_notes(&revs->notes_opt, &revs->show_notes);
2404 revs->show_notes_given = 1;
2405 } else if (!strcmp(arg, "--show-signature")) {
2406 revs->show_signature = 1;
2407 } else if (!strcmp(arg, "--no-show-signature")) {
2408 revs->show_signature = 0;
2409 } else if (!strcmp(arg, "--show-linear-break")) {
2410 revs->break_bar = " ..........";
2411 revs->track_linear = 1;
2412 revs->track_first_time = 1;
2413 } else if (skip_prefix(arg, "--show-linear-break=", &optarg)) {
2414 revs->break_bar = xstrdup(optarg);
2415 revs->track_linear = 1;
2416 revs->track_first_time = 1;
2417 } else if (skip_prefix(arg, "--show-notes=", &optarg) ||
2418 skip_prefix(arg, "--notes=", &optarg)) {
2419 if (starts_with(arg, "--show-notes=") &&
2420 revs->notes_opt.use_default_notes < 0)
2421 revs->notes_opt.use_default_notes = 1;
2422 enable_ref_display_notes(&revs->notes_opt, &revs->show_notes, optarg);
2423 revs->show_notes_given = 1;
2424 } else if (!strcmp(arg, "--no-notes")) {
2425 disable_display_notes(&revs->notes_opt, &revs->show_notes);
2426 revs->show_notes_given = 1;
2427 } else if (!strcmp(arg, "--standard-notes")) {
2428 revs->show_notes_given = 1;
2429 revs->notes_opt.use_default_notes = 1;
2430 } else if (!strcmp(arg, "--no-standard-notes")) {
2431 revs->notes_opt.use_default_notes = 0;
2432 } else if (!strcmp(arg, "--oneline")) {
2433 revs->verbose_header = 1;
2434 get_commit_format("oneline", revs);
2435 revs->pretty_given = 1;
2436 revs->abbrev_commit = 1;
2437 } else if (!strcmp(arg, "--graph")) {
2438 graph_clear(revs->graph);
2439 revs->graph = graph_init(revs);
2440 } else if (!strcmp(arg, "--no-graph")) {
2441 graph_clear(revs->graph);
2442 revs->graph = NULL;
2443 } else if (!strcmp(arg, "--encode-email-headers")) {
2444 revs->encode_email_headers = 1;
2445 } else if (!strcmp(arg, "--no-encode-email-headers")) {
2446 revs->encode_email_headers = 0;
2447 } else if (!strcmp(arg, "--root")) {
2448 revs->show_root_diff = 1;
2449 } else if (!strcmp(arg, "--no-commit-id")) {
2450 revs->no_commit_id = 1;
2451 } else if (!strcmp(arg, "--always")) {
2452 revs->always_show_header = 1;
2453 } else if (!strcmp(arg, "--no-abbrev")) {
2454 revs->abbrev = 0;
2455 } else if (!strcmp(arg, "--abbrev")) {
2456 revs->abbrev = DEFAULT_ABBREV;
2457 } else if (skip_prefix(arg, "--abbrev=", &optarg)) {
2458 revs->abbrev = strtoul(optarg, NULL, 10);
2459 if (revs->abbrev < MINIMUM_ABBREV)
2460 revs->abbrev = MINIMUM_ABBREV;
2461 else if (revs->abbrev > hexsz)
2462 revs->abbrev = hexsz;
2463 } else if (!strcmp(arg, "--abbrev-commit")) {
2464 revs->abbrev_commit = 1;
2465 revs->abbrev_commit_given = 1;
2466 } else if (!strcmp(arg, "--no-abbrev-commit")) {
2467 revs->abbrev_commit = 0;
2468 } else if (!strcmp(arg, "--full-diff")) {
2469 revs->diff = 1;
2470 revs->full_diff = 1;
2471 } else if (!strcmp(arg, "--show-pulls")) {
2472 revs->show_pulls = 1;
2473 } else if (!strcmp(arg, "--full-history")) {
2474 revs->simplify_history = 0;
2475 } else if (!strcmp(arg, "--relative-date")) {
2476 revs->date_mode.type = DATE_RELATIVE;
2477 revs->date_mode_explicit = 1;
2478 } else if ((argcount = parse_long_opt("date", argv, &optarg))) {
2479 parse_date_format(optarg, &revs->date_mode);
2480 revs->date_mode_explicit = 1;
2481 return argcount;
2482 } else if (!strcmp(arg, "--log-size")) {
2483 revs->show_log_size = 1;
2486 * Grepping the commit log
2488 else if ((argcount = parse_long_opt("author", argv, &optarg))) {
2489 add_header_grep(revs, GREP_HEADER_AUTHOR, optarg);
2490 return argcount;
2491 } else if ((argcount = parse_long_opt("committer", argv, &optarg))) {
2492 add_header_grep(revs, GREP_HEADER_COMMITTER, optarg);
2493 return argcount;
2494 } else if ((argcount = parse_long_opt("grep-reflog", argv, &optarg))) {
2495 add_header_grep(revs, GREP_HEADER_REFLOG, optarg);
2496 return argcount;
2497 } else if ((argcount = parse_long_opt("grep", argv, &optarg))) {
2498 add_message_grep(revs, optarg);
2499 return argcount;
2500 } else if (!strcmp(arg, "--basic-regexp")) {
2501 revs->grep_filter.pattern_type_option = GREP_PATTERN_TYPE_BRE;
2502 } else if (!strcmp(arg, "--extended-regexp") || !strcmp(arg, "-E")) {
2503 revs->grep_filter.pattern_type_option = GREP_PATTERN_TYPE_ERE;
2504 } else if (!strcmp(arg, "--regexp-ignore-case") || !strcmp(arg, "-i")) {
2505 revs->grep_filter.ignore_case = 1;
2506 revs->diffopt.pickaxe_opts |= DIFF_PICKAXE_IGNORE_CASE;
2507 } else if (!strcmp(arg, "--fixed-strings") || !strcmp(arg, "-F")) {
2508 revs->grep_filter.pattern_type_option = GREP_PATTERN_TYPE_FIXED;
2509 } else if (!strcmp(arg, "--perl-regexp") || !strcmp(arg, "-P")) {
2510 revs->grep_filter.pattern_type_option = GREP_PATTERN_TYPE_PCRE;
2511 } else if (!strcmp(arg, "--all-match")) {
2512 revs->grep_filter.all_match = 1;
2513 } else if (!strcmp(arg, "--invert-grep")) {
2514 revs->grep_filter.no_body_match = 1;
2515 } else if ((argcount = parse_long_opt("encoding", argv, &optarg))) {
2516 if (strcmp(optarg, "none"))
2517 git_log_output_encoding = xstrdup(optarg);
2518 else
2519 git_log_output_encoding = "";
2520 return argcount;
2521 } else if (!strcmp(arg, "--reverse")) {
2522 revs->reverse ^= 1;
2523 } else if (!strcmp(arg, "--children")) {
2524 revs->children.name = "children";
2525 revs->limited = 1;
2526 } else if (!strcmp(arg, "--ignore-missing")) {
2527 revs->ignore_missing = 1;
2528 } else if (opt && opt->allow_exclude_promisor_objects &&
2529 !strcmp(arg, "--exclude-promisor-objects")) {
2530 if (fetch_if_missing)
2531 BUG("exclude_promisor_objects can only be used when fetch_if_missing is 0");
2532 revs->exclude_promisor_objects = 1;
2533 } else {
2534 int opts = diff_opt_parse(&revs->diffopt, argv, argc, revs->prefix);
2535 if (!opts)
2536 unkv[(*unkc)++] = arg;
2537 return opts;
2540 return 1;
2543 void parse_revision_opt(struct rev_info *revs, struct parse_opt_ctx_t *ctx,
2544 const struct option *options,
2545 const char * const usagestr[])
2547 int n = handle_revision_opt(revs, ctx->argc, ctx->argv,
2548 &ctx->cpidx, ctx->out, NULL);
2549 if (n <= 0) {
2550 error("unknown option `%s'", ctx->argv[0]);
2551 usage_with_options(usagestr, options);
2553 ctx->argv += n;
2554 ctx->argc -= n;
2557 void revision_opts_finish(struct rev_info *revs)
2559 if (revs->graph && revs->track_linear)
2560 die(_("options '%s' and '%s' cannot be used together"), "--show-linear-break", "--graph");
2562 if (revs->graph) {
2563 revs->topo_order = 1;
2564 revs->rewrite_parents = 1;
2568 static int for_each_bisect_ref(struct ref_store *refs, each_ref_fn fn,
2569 void *cb_data, const char *term)
2571 struct strbuf bisect_refs = STRBUF_INIT;
2572 int status;
2573 strbuf_addf(&bisect_refs, "refs/bisect/%s", term);
2574 status = refs_for_each_fullref_in(refs, bisect_refs.buf, fn, cb_data);
2575 strbuf_release(&bisect_refs);
2576 return status;
2579 static int for_each_bad_bisect_ref(struct ref_store *refs, each_ref_fn fn, void *cb_data)
2581 return for_each_bisect_ref(refs, fn, cb_data, term_bad);
2584 static int for_each_good_bisect_ref(struct ref_store *refs, each_ref_fn fn, void *cb_data)
2586 return for_each_bisect_ref(refs, fn, cb_data, term_good);
2589 static int handle_revision_pseudo_opt(struct rev_info *revs,
2590 const char **argv, int *flags)
2592 const char *arg = argv[0];
2593 const char *optarg;
2594 struct ref_store *refs;
2595 int argcount;
2597 if (revs->repo != the_repository) {
2599 * We need some something like get_submodule_worktrees()
2600 * before we can go through all worktrees of a submodule,
2601 * .e.g with adding all HEADs from --all, which is not
2602 * supported right now, so stick to single worktree.
2604 if (!revs->single_worktree)
2605 BUG("--single-worktree cannot be used together with submodule");
2607 refs = get_main_ref_store(revs->repo);
2610 * NOTE!
2612 * Commands like "git shortlog" will not accept the options below
2613 * unless parse_revision_opt queues them (as opposed to erroring
2614 * out).
2616 * When implementing your new pseudo-option, remember to
2617 * register it in the list at the top of handle_revision_opt.
2619 if (!strcmp(arg, "--all")) {
2620 handle_refs(refs, revs, *flags, refs_for_each_ref);
2621 handle_refs(refs, revs, *flags, refs_head_ref);
2622 if (!revs->single_worktree) {
2623 struct all_refs_cb cb;
2625 init_all_refs_cb(&cb, revs, *flags);
2626 other_head_refs(handle_one_ref, &cb);
2628 clear_ref_exclusion(&revs->ref_excludes);
2629 } else if (!strcmp(arg, "--branches")) {
2630 handle_refs(refs, revs, *flags, refs_for_each_branch_ref);
2631 clear_ref_exclusion(&revs->ref_excludes);
2632 } else if (!strcmp(arg, "--bisect")) {
2633 read_bisect_terms(&term_bad, &term_good);
2634 handle_refs(refs, revs, *flags, for_each_bad_bisect_ref);
2635 handle_refs(refs, revs, *flags ^ (UNINTERESTING | BOTTOM),
2636 for_each_good_bisect_ref);
2637 revs->bisect = 1;
2638 } else if (!strcmp(arg, "--tags")) {
2639 handle_refs(refs, revs, *flags, refs_for_each_tag_ref);
2640 clear_ref_exclusion(&revs->ref_excludes);
2641 } else if (!strcmp(arg, "--remotes")) {
2642 handle_refs(refs, revs, *flags, refs_for_each_remote_ref);
2643 clear_ref_exclusion(&revs->ref_excludes);
2644 } else if ((argcount = parse_long_opt("glob", argv, &optarg))) {
2645 struct all_refs_cb cb;
2646 init_all_refs_cb(&cb, revs, *flags);
2647 for_each_glob_ref(handle_one_ref, optarg, &cb);
2648 clear_ref_exclusion(&revs->ref_excludes);
2649 return argcount;
2650 } else if ((argcount = parse_long_opt("exclude", argv, &optarg))) {
2651 add_ref_exclusion(&revs->ref_excludes, optarg);
2652 return argcount;
2653 } else if (skip_prefix(arg, "--branches=", &optarg)) {
2654 struct all_refs_cb cb;
2655 init_all_refs_cb(&cb, revs, *flags);
2656 for_each_glob_ref_in(handle_one_ref, optarg, "refs/heads/", &cb);
2657 clear_ref_exclusion(&revs->ref_excludes);
2658 } else if (skip_prefix(arg, "--tags=", &optarg)) {
2659 struct all_refs_cb cb;
2660 init_all_refs_cb(&cb, revs, *flags);
2661 for_each_glob_ref_in(handle_one_ref, optarg, "refs/tags/", &cb);
2662 clear_ref_exclusion(&revs->ref_excludes);
2663 } else if (skip_prefix(arg, "--remotes=", &optarg)) {
2664 struct all_refs_cb cb;
2665 init_all_refs_cb(&cb, revs, *flags);
2666 for_each_glob_ref_in(handle_one_ref, optarg, "refs/remotes/", &cb);
2667 clear_ref_exclusion(&revs->ref_excludes);
2668 } else if (!strcmp(arg, "--reflog")) {
2669 add_reflogs_to_pending(revs, *flags);
2670 } else if (!strcmp(arg, "--indexed-objects")) {
2671 add_index_objects_to_pending(revs, *flags);
2672 } else if (!strcmp(arg, "--alternate-refs")) {
2673 add_alternate_refs_to_pending(revs, *flags);
2674 } else if (!strcmp(arg, "--not")) {
2675 *flags ^= UNINTERESTING | BOTTOM;
2676 } else if (!strcmp(arg, "--no-walk")) {
2677 revs->no_walk = 1;
2678 } else if (skip_prefix(arg, "--no-walk=", &optarg)) {
2680 * Detached form ("--no-walk X" as opposed to "--no-walk=X")
2681 * not allowed, since the argument is optional.
2683 revs->no_walk = 1;
2684 if (!strcmp(optarg, "sorted"))
2685 revs->unsorted_input = 0;
2686 else if (!strcmp(optarg, "unsorted"))
2687 revs->unsorted_input = 1;
2688 else
2689 return error("invalid argument to --no-walk");
2690 } else if (!strcmp(arg, "--do-walk")) {
2691 revs->no_walk = 0;
2692 } else if (!strcmp(arg, "--single-worktree")) {
2693 revs->single_worktree = 1;
2694 } else if (skip_prefix(arg, ("--filter="), &arg)) {
2695 parse_list_objects_filter(&revs->filter, arg);
2696 } else if (!strcmp(arg, ("--no-filter"))) {
2697 list_objects_filter_set_no_filter(&revs->filter);
2698 } else {
2699 return 0;
2702 return 1;
2705 static void NORETURN diagnose_missing_default(const char *def)
2707 int flags;
2708 const char *refname;
2710 refname = resolve_ref_unsafe(def, 0, NULL, &flags);
2711 if (!refname || !(flags & REF_ISSYMREF) || (flags & REF_ISBROKEN))
2712 die(_("your current branch appears to be broken"));
2714 skip_prefix(refname, "refs/heads/", &refname);
2715 die(_("your current branch '%s' does not have any commits yet"),
2716 refname);
2720 * Parse revision information, filling in the "rev_info" structure,
2721 * and removing the used arguments from the argument list.
2723 * Returns the number of arguments left that weren't recognized
2724 * (which are also moved to the head of the argument list)
2726 int setup_revisions(int argc, const char **argv, struct rev_info *revs, struct setup_revision_opt *opt)
2728 int i, flags, left, seen_dashdash, revarg_opt;
2729 struct strvec prune_data = STRVEC_INIT;
2730 int seen_end_of_options = 0;
2732 /* First, search for "--" */
2733 if (opt && opt->assume_dashdash) {
2734 seen_dashdash = 1;
2735 } else {
2736 seen_dashdash = 0;
2737 for (i = 1; i < argc; i++) {
2738 const char *arg = argv[i];
2739 if (strcmp(arg, "--"))
2740 continue;
2741 argv[i] = NULL;
2742 argc = i;
2743 if (argv[i + 1])
2744 strvec_pushv(&prune_data, argv + i + 1);
2745 seen_dashdash = 1;
2746 break;
2750 /* Second, deal with arguments and options */
2751 flags = 0;
2752 revarg_opt = opt ? opt->revarg_opt : 0;
2753 if (seen_dashdash)
2754 revarg_opt |= REVARG_CANNOT_BE_FILENAME;
2755 for (left = i = 1; i < argc; i++) {
2756 const char *arg = argv[i];
2757 if (!seen_end_of_options && *arg == '-') {
2758 int opts;
2760 opts = handle_revision_pseudo_opt(
2761 revs, argv + i,
2762 &flags);
2763 if (opts > 0) {
2764 i += opts - 1;
2765 continue;
2768 if (!strcmp(arg, "--stdin")) {
2769 if (revs->disable_stdin) {
2770 argv[left++] = arg;
2771 continue;
2773 if (revs->read_from_stdin++)
2774 die("--stdin given twice?");
2775 read_revisions_from_stdin(revs, &prune_data);
2776 continue;
2779 if (!strcmp(arg, "--end-of-options")) {
2780 seen_end_of_options = 1;
2781 continue;
2784 opts = handle_revision_opt(revs, argc - i, argv + i,
2785 &left, argv, opt);
2786 if (opts > 0) {
2787 i += opts - 1;
2788 continue;
2790 if (opts < 0)
2791 exit(128);
2792 continue;
2796 if (handle_revision_arg(arg, revs, flags, revarg_opt)) {
2797 int j;
2798 if (seen_dashdash || *arg == '^')
2799 die("bad revision '%s'", arg);
2801 /* If we didn't have a "--":
2802 * (1) all filenames must exist;
2803 * (2) all rev-args must not be interpretable
2804 * as a valid filename.
2805 * but the latter we have checked in the main loop.
2807 for (j = i; j < argc; j++)
2808 verify_filename(revs->prefix, argv[j], j == i);
2810 strvec_pushv(&prune_data, argv + i);
2811 break;
2814 revision_opts_finish(revs);
2816 if (prune_data.nr) {
2818 * If we need to introduce the magic "a lone ':' means no
2819 * pathspec whatsoever", here is the place to do so.
2821 * if (prune_data.nr == 1 && !strcmp(prune_data[0], ":")) {
2822 * prune_data.nr = 0;
2823 * prune_data.alloc = 0;
2824 * free(prune_data.path);
2825 * prune_data.path = NULL;
2826 * } else {
2827 * terminate prune_data.alloc with NULL and
2828 * call init_pathspec() to set revs->prune_data here.
2831 parse_pathspec(&revs->prune_data, 0, 0,
2832 revs->prefix, prune_data.v);
2834 strvec_clear(&prune_data);
2836 if (revs->def == NULL)
2837 revs->def = opt ? opt->def : NULL;
2838 if (opt && opt->tweak)
2839 opt->tweak(revs, opt);
2840 if (revs->show_merge)
2841 prepare_show_merge(revs);
2842 if (revs->def && !revs->pending.nr && !revs->rev_input_given) {
2843 struct object_id oid;
2844 struct object *object;
2845 struct object_context oc;
2846 if (get_oid_with_context(revs->repo, revs->def, 0, &oid, &oc))
2847 diagnose_missing_default(revs->def);
2848 object = get_reference(revs, revs->def, &oid, 0);
2849 add_pending_object_with_mode(revs, object, revs->def, oc.mode);
2852 /* Did the user ask for any diff output? Run the diff! */
2853 if (revs->diffopt.output_format & ~DIFF_FORMAT_NO_OUTPUT)
2854 revs->diff = 1;
2856 /* Pickaxe, diff-filter and rename following need diffs */
2857 if ((revs->diffopt.pickaxe_opts & DIFF_PICKAXE_KINDS_MASK) ||
2858 revs->diffopt.filter ||
2859 revs->diffopt.flags.follow_renames)
2860 revs->diff = 1;
2862 if (revs->diffopt.objfind)
2863 revs->simplify_history = 0;
2865 if (revs->line_level_traverse) {
2866 if (want_ancestry(revs))
2867 revs->limited = 1;
2868 revs->topo_order = 1;
2871 if (revs->topo_order && !generation_numbers_enabled(the_repository))
2872 revs->limited = 1;
2874 if (revs->prune_data.nr) {
2875 copy_pathspec(&revs->pruning.pathspec, &revs->prune_data);
2876 /* Can't prune commits with rename following: the paths change.. */
2877 if (!revs->diffopt.flags.follow_renames)
2878 revs->prune = 1;
2879 if (!revs->full_diff)
2880 copy_pathspec(&revs->diffopt.pathspec,
2881 &revs->prune_data);
2884 diff_merges_setup_revs(revs);
2886 revs->diffopt.abbrev = revs->abbrev;
2888 diff_setup_done(&revs->diffopt);
2890 if (!is_encoding_utf8(get_log_output_encoding()))
2891 revs->grep_filter.ignore_locale = 1;
2892 compile_grep_patterns(&revs->grep_filter);
2894 if (revs->reverse && revs->reflog_info)
2895 die(_("options '%s' and '%s' cannot be used together"), "--reverse", "--walk-reflogs");
2896 if (revs->reflog_info && revs->limited)
2897 die("cannot combine --walk-reflogs with history-limiting options");
2898 if (revs->rewrite_parents && revs->children.name)
2899 die(_("options '%s' and '%s' cannot be used together"), "--parents", "--children");
2900 if (revs->filter.choice && !revs->blob_objects)
2901 die(_("object filtering requires --objects"));
2904 * Limitations on the graph functionality
2906 if (revs->reverse && revs->graph)
2907 die(_("options '%s' and '%s' cannot be used together"), "--reverse", "--graph");
2909 if (revs->reflog_info && revs->graph)
2910 die(_("options '%s' and '%s' cannot be used together"), "--walk-reflogs", "--graph");
2911 if (revs->no_walk && revs->graph)
2912 die(_("options '%s' and '%s' cannot be used together"), "--no-walk", "--graph");
2913 if (!revs->reflog_info && revs->grep_filter.use_reflog_filter)
2914 die(_("the option '%s' requires '%s'"), "--grep-reflog", "--walk-reflogs");
2916 if (revs->line_level_traverse &&
2917 (revs->diffopt.output_format & ~(DIFF_FORMAT_PATCH | DIFF_FORMAT_NO_OUTPUT)))
2918 die(_("-L does not yet support diff formats besides -p and -s"));
2920 if (revs->expand_tabs_in_log < 0)
2921 revs->expand_tabs_in_log = revs->expand_tabs_in_log_default;
2923 return left;
2926 static void add_child(struct rev_info *revs, struct commit *parent, struct commit *child)
2928 struct commit_list *l = xcalloc(1, sizeof(*l));
2930 l->item = child;
2931 l->next = add_decoration(&revs->children, &parent->object, l);
2934 static int remove_duplicate_parents(struct rev_info *revs, struct commit *commit)
2936 struct treesame_state *ts = lookup_decoration(&revs->treesame, &commit->object);
2937 struct commit_list **pp, *p;
2938 int surviving_parents;
2940 /* Examine existing parents while marking ones we have seen... */
2941 pp = &commit->parents;
2942 surviving_parents = 0;
2943 while ((p = *pp) != NULL) {
2944 struct commit *parent = p->item;
2945 if (parent->object.flags & TMP_MARK) {
2946 *pp = p->next;
2947 if (ts)
2948 compact_treesame(revs, commit, surviving_parents);
2949 continue;
2951 parent->object.flags |= TMP_MARK;
2952 surviving_parents++;
2953 pp = &p->next;
2955 /* clear the temporary mark */
2956 for (p = commit->parents; p; p = p->next) {
2957 p->item->object.flags &= ~TMP_MARK;
2959 /* no update_treesame() - removing duplicates can't affect TREESAME */
2960 return surviving_parents;
2963 struct merge_simplify_state {
2964 struct commit *simplified;
2967 static struct merge_simplify_state *locate_simplify_state(struct rev_info *revs, struct commit *commit)
2969 struct merge_simplify_state *st;
2971 st = lookup_decoration(&revs->merge_simplification, &commit->object);
2972 if (!st) {
2973 CALLOC_ARRAY(st, 1);
2974 add_decoration(&revs->merge_simplification, &commit->object, st);
2976 return st;
2979 static int mark_redundant_parents(struct commit *commit)
2981 struct commit_list *h = reduce_heads(commit->parents);
2982 int i = 0, marked = 0;
2983 struct commit_list *po, *pn;
2985 /* Want these for sanity-checking only */
2986 int orig_cnt = commit_list_count(commit->parents);
2987 int cnt = commit_list_count(h);
2990 * Not ready to remove items yet, just mark them for now, based
2991 * on the output of reduce_heads(). reduce_heads outputs the reduced
2992 * set in its original order, so this isn't too hard.
2994 po = commit->parents;
2995 pn = h;
2996 while (po) {
2997 if (pn && po->item == pn->item) {
2998 pn = pn->next;
2999 i++;
3000 } else {
3001 po->item->object.flags |= TMP_MARK;
3002 marked++;
3004 po=po->next;
3007 if (i != cnt || cnt+marked != orig_cnt)
3008 die("mark_redundant_parents %d %d %d %d", orig_cnt, cnt, i, marked);
3010 free_commit_list(h);
3012 return marked;
3015 static int mark_treesame_root_parents(struct commit *commit)
3017 struct commit_list *p;
3018 int marked = 0;
3020 for (p = commit->parents; p; p = p->next) {
3021 struct commit *parent = p->item;
3022 if (!parent->parents && (parent->object.flags & TREESAME)) {
3023 parent->object.flags |= TMP_MARK;
3024 marked++;
3028 return marked;
3032 * Awkward naming - this means one parent we are TREESAME to.
3033 * cf mark_treesame_root_parents: root parents that are TREESAME (to an
3034 * empty tree). Better name suggestions?
3036 static int leave_one_treesame_to_parent(struct rev_info *revs, struct commit *commit)
3038 struct treesame_state *ts = lookup_decoration(&revs->treesame, &commit->object);
3039 struct commit *unmarked = NULL, *marked = NULL;
3040 struct commit_list *p;
3041 unsigned n;
3043 for (p = commit->parents, n = 0; p; p = p->next, n++) {
3044 if (ts->treesame[n]) {
3045 if (p->item->object.flags & TMP_MARK) {
3046 if (!marked)
3047 marked = p->item;
3048 } else {
3049 if (!unmarked) {
3050 unmarked = p->item;
3051 break;
3058 * If we are TREESAME to a marked-for-deletion parent, but not to any
3059 * unmarked parents, unmark the first TREESAME parent. This is the
3060 * parent that the default simplify_history==1 scan would have followed,
3061 * and it doesn't make sense to omit that path when asking for a
3062 * simplified full history. Retaining it improves the chances of
3063 * understanding odd missed merges that took an old version of a file.
3065 * Example:
3067 * I--------*X A modified the file, but mainline merge X used
3068 * \ / "-s ours", so took the version from I. X is
3069 * `-*A--' TREESAME to I and !TREESAME to A.
3071 * Default log from X would produce "I". Without this check,
3072 * --full-history --simplify-merges would produce "I-A-X", showing
3073 * the merge commit X and that it changed A, but not making clear that
3074 * it had just taken the I version. With this check, the topology above
3075 * is retained.
3077 * Note that it is possible that the simplification chooses a different
3078 * TREESAME parent from the default, in which case this test doesn't
3079 * activate, and we _do_ drop the default parent. Example:
3081 * I------X A modified the file, but it was reverted in B,
3082 * \ / meaning mainline merge X is TREESAME to both
3083 * *A-*B parents.
3085 * Default log would produce "I" by following the first parent;
3086 * --full-history --simplify-merges will produce "I-A-B". But this is a
3087 * reasonable result - it presents a logical full history leading from
3088 * I to X, and X is not an important merge.
3090 if (!unmarked && marked) {
3091 marked->object.flags &= ~TMP_MARK;
3092 return 1;
3095 return 0;
3098 static int remove_marked_parents(struct rev_info *revs, struct commit *commit)
3100 struct commit_list **pp, *p;
3101 int nth_parent, removed = 0;
3103 pp = &commit->parents;
3104 nth_parent = 0;
3105 while ((p = *pp) != NULL) {
3106 struct commit *parent = p->item;
3107 if (parent->object.flags & TMP_MARK) {
3108 parent->object.flags &= ~TMP_MARK;
3109 *pp = p->next;
3110 free(p);
3111 removed++;
3112 compact_treesame(revs, commit, nth_parent);
3113 continue;
3115 pp = &p->next;
3116 nth_parent++;
3119 /* Removing parents can only increase TREESAMEness */
3120 if (removed && !(commit->object.flags & TREESAME))
3121 update_treesame(revs, commit);
3123 return nth_parent;
3126 static struct commit_list **simplify_one(struct rev_info *revs, struct commit *commit, struct commit_list **tail)
3128 struct commit_list *p;
3129 struct commit *parent;
3130 struct merge_simplify_state *st, *pst;
3131 int cnt;
3133 st = locate_simplify_state(revs, commit);
3136 * Have we handled this one?
3138 if (st->simplified)
3139 return tail;
3142 * An UNINTERESTING commit simplifies to itself, so does a
3143 * root commit. We do not rewrite parents of such commit
3144 * anyway.
3146 if ((commit->object.flags & UNINTERESTING) || !commit->parents) {
3147 st->simplified = commit;
3148 return tail;
3152 * Do we know what commit all of our parents that matter
3153 * should be rewritten to? Otherwise we are not ready to
3154 * rewrite this one yet.
3156 for (cnt = 0, p = commit->parents; p; p = p->next) {
3157 pst = locate_simplify_state(revs, p->item);
3158 if (!pst->simplified) {
3159 tail = &commit_list_insert(p->item, tail)->next;
3160 cnt++;
3162 if (revs->first_parent_only)
3163 break;
3165 if (cnt) {
3166 tail = &commit_list_insert(commit, tail)->next;
3167 return tail;
3171 * Rewrite our list of parents. Note that this cannot
3172 * affect our TREESAME flags in any way - a commit is
3173 * always TREESAME to its simplification.
3175 for (p = commit->parents; p; p = p->next) {
3176 pst = locate_simplify_state(revs, p->item);
3177 p->item = pst->simplified;
3178 if (revs->first_parent_only)
3179 break;
3182 if (revs->first_parent_only)
3183 cnt = 1;
3184 else
3185 cnt = remove_duplicate_parents(revs, commit);
3188 * It is possible that we are a merge and one side branch
3189 * does not have any commit that touches the given paths;
3190 * in such a case, the immediate parent from that branch
3191 * will be rewritten to be the merge base.
3193 * o----X X: the commit we are looking at;
3194 * / / o: a commit that touches the paths;
3195 * ---o----'
3197 * Further, a merge of an independent branch that doesn't
3198 * touch the path will reduce to a treesame root parent:
3200 * ----o----X X: the commit we are looking at;
3201 * / o: a commit that touches the paths;
3202 * r r: a root commit not touching the paths
3204 * Detect and simplify both cases.
3206 if (1 < cnt) {
3207 int marked = mark_redundant_parents(commit);
3208 marked += mark_treesame_root_parents(commit);
3209 if (marked)
3210 marked -= leave_one_treesame_to_parent(revs, commit);
3211 if (marked)
3212 cnt = remove_marked_parents(revs, commit);
3216 * A commit simplifies to itself if it is a root, if it is
3217 * UNINTERESTING, if it touches the given paths, or if it is a
3218 * merge and its parents don't simplify to one relevant commit
3219 * (the first two cases are already handled at the beginning of
3220 * this function).
3222 * Otherwise, it simplifies to what its sole relevant parent
3223 * simplifies to.
3225 if (!cnt ||
3226 (commit->object.flags & UNINTERESTING) ||
3227 !(commit->object.flags & TREESAME) ||
3228 (parent = one_relevant_parent(revs, commit->parents)) == NULL ||
3229 (revs->show_pulls && (commit->object.flags & PULL_MERGE)))
3230 st->simplified = commit;
3231 else {
3232 pst = locate_simplify_state(revs, parent);
3233 st->simplified = pst->simplified;
3235 return tail;
3238 static void simplify_merges(struct rev_info *revs)
3240 struct commit_list *list, *next;
3241 struct commit_list *yet_to_do, **tail;
3242 struct commit *commit;
3244 if (!revs->prune)
3245 return;
3247 /* feed the list reversed */
3248 yet_to_do = NULL;
3249 for (list = revs->commits; list; list = next) {
3250 commit = list->item;
3251 next = list->next;
3253 * Do not free(list) here yet; the original list
3254 * is used later in this function.
3256 commit_list_insert(commit, &yet_to_do);
3258 while (yet_to_do) {
3259 list = yet_to_do;
3260 yet_to_do = NULL;
3261 tail = &yet_to_do;
3262 while (list) {
3263 commit = pop_commit(&list);
3264 tail = simplify_one(revs, commit, tail);
3268 /* clean up the result, removing the simplified ones */
3269 list = revs->commits;
3270 revs->commits = NULL;
3271 tail = &revs->commits;
3272 while (list) {
3273 struct merge_simplify_state *st;
3275 commit = pop_commit(&list);
3276 st = locate_simplify_state(revs, commit);
3277 if (st->simplified == commit)
3278 tail = &commit_list_insert(commit, tail)->next;
3282 static void set_children(struct rev_info *revs)
3284 struct commit_list *l;
3285 for (l = revs->commits; l; l = l->next) {
3286 struct commit *commit = l->item;
3287 struct commit_list *p;
3289 for (p = commit->parents; p; p = p->next)
3290 add_child(revs, p->item, commit);
3294 void reset_revision_walk(void)
3296 clear_object_flags(SEEN | ADDED | SHOWN | TOPO_WALK_EXPLORED | TOPO_WALK_INDEGREE);
3299 static int mark_uninteresting(const struct object_id *oid,
3300 struct packed_git *pack,
3301 uint32_t pos,
3302 void *cb)
3304 struct rev_info *revs = cb;
3305 struct object *o = lookup_unknown_object(revs->repo, oid);
3306 o->flags |= UNINTERESTING | SEEN;
3307 return 0;
3310 define_commit_slab(indegree_slab, int);
3311 define_commit_slab(author_date_slab, timestamp_t);
3313 struct topo_walk_info {
3314 timestamp_t min_generation;
3315 struct prio_queue explore_queue;
3316 struct prio_queue indegree_queue;
3317 struct prio_queue topo_queue;
3318 struct indegree_slab indegree;
3319 struct author_date_slab author_date;
3322 static int topo_walk_atexit_registered;
3323 static unsigned int count_explore_walked;
3324 static unsigned int count_indegree_walked;
3325 static unsigned int count_topo_walked;
3327 static void trace2_topo_walk_statistics_atexit(void)
3329 struct json_writer jw = JSON_WRITER_INIT;
3331 jw_object_begin(&jw, 0);
3332 jw_object_intmax(&jw, "count_explore_walked", count_explore_walked);
3333 jw_object_intmax(&jw, "count_indegree_walked", count_indegree_walked);
3334 jw_object_intmax(&jw, "count_topo_walked", count_topo_walked);
3335 jw_end(&jw);
3337 trace2_data_json("topo_walk", the_repository, "statistics", &jw);
3339 jw_release(&jw);
3342 static inline void test_flag_and_insert(struct prio_queue *q, struct commit *c, int flag)
3344 if (c->object.flags & flag)
3345 return;
3347 c->object.flags |= flag;
3348 prio_queue_put(q, c);
3351 static void explore_walk_step(struct rev_info *revs)
3353 struct topo_walk_info *info = revs->topo_walk_info;
3354 struct commit_list *p;
3355 struct commit *c = prio_queue_get(&info->explore_queue);
3357 if (!c)
3358 return;
3360 if (repo_parse_commit_gently(revs->repo, c, 1) < 0)
3361 return;
3363 count_explore_walked++;
3365 if (revs->sort_order == REV_SORT_BY_AUTHOR_DATE)
3366 record_author_date(&info->author_date, c);
3368 if (revs->max_age != -1 && (c->date < revs->max_age))
3369 c->object.flags |= UNINTERESTING;
3371 if (process_parents(revs, c, NULL, NULL) < 0)
3372 return;
3374 if (c->object.flags & UNINTERESTING)
3375 mark_parents_uninteresting(revs, c);
3377 for (p = c->parents; p; p = p->next)
3378 test_flag_and_insert(&info->explore_queue, p->item, TOPO_WALK_EXPLORED);
3381 static void explore_to_depth(struct rev_info *revs,
3382 timestamp_t gen_cutoff)
3384 struct topo_walk_info *info = revs->topo_walk_info;
3385 struct commit *c;
3386 while ((c = prio_queue_peek(&info->explore_queue)) &&
3387 commit_graph_generation(c) >= gen_cutoff)
3388 explore_walk_step(revs);
3391 static void indegree_walk_step(struct rev_info *revs)
3393 struct commit_list *p;
3394 struct topo_walk_info *info = revs->topo_walk_info;
3395 struct commit *c = prio_queue_get(&info->indegree_queue);
3397 if (!c)
3398 return;
3400 if (repo_parse_commit_gently(revs->repo, c, 1) < 0)
3401 return;
3403 count_indegree_walked++;
3405 explore_to_depth(revs, commit_graph_generation(c));
3407 for (p = c->parents; p; p = p->next) {
3408 struct commit *parent = p->item;
3409 int *pi = indegree_slab_at(&info->indegree, parent);
3411 if (repo_parse_commit_gently(revs->repo, parent, 1) < 0)
3412 return;
3414 if (*pi)
3415 (*pi)++;
3416 else
3417 *pi = 2;
3419 test_flag_and_insert(&info->indegree_queue, parent, TOPO_WALK_INDEGREE);
3421 if (revs->first_parent_only)
3422 return;
3426 static void compute_indegrees_to_depth(struct rev_info *revs,
3427 timestamp_t gen_cutoff)
3429 struct topo_walk_info *info = revs->topo_walk_info;
3430 struct commit *c;
3431 while ((c = prio_queue_peek(&info->indegree_queue)) &&
3432 commit_graph_generation(c) >= gen_cutoff)
3433 indegree_walk_step(revs);
3436 static void reset_topo_walk(struct rev_info *revs)
3438 struct topo_walk_info *info = revs->topo_walk_info;
3440 clear_prio_queue(&info->explore_queue);
3441 clear_prio_queue(&info->indegree_queue);
3442 clear_prio_queue(&info->topo_queue);
3443 clear_indegree_slab(&info->indegree);
3444 clear_author_date_slab(&info->author_date);
3446 FREE_AND_NULL(revs->topo_walk_info);
3449 static void init_topo_walk(struct rev_info *revs)
3451 struct topo_walk_info *info;
3452 struct commit_list *list;
3453 if (revs->topo_walk_info)
3454 reset_topo_walk(revs);
3456 revs->topo_walk_info = xmalloc(sizeof(struct topo_walk_info));
3457 info = revs->topo_walk_info;
3458 memset(info, 0, sizeof(struct topo_walk_info));
3460 init_indegree_slab(&info->indegree);
3461 memset(&info->explore_queue, 0, sizeof(info->explore_queue));
3462 memset(&info->indegree_queue, 0, sizeof(info->indegree_queue));
3463 memset(&info->topo_queue, 0, sizeof(info->topo_queue));
3465 switch (revs->sort_order) {
3466 default: /* REV_SORT_IN_GRAPH_ORDER */
3467 info->topo_queue.compare = NULL;
3468 break;
3469 case REV_SORT_BY_COMMIT_DATE:
3470 info->topo_queue.compare = compare_commits_by_commit_date;
3471 break;
3472 case REV_SORT_BY_AUTHOR_DATE:
3473 init_author_date_slab(&info->author_date);
3474 info->topo_queue.compare = compare_commits_by_author_date;
3475 info->topo_queue.cb_data = &info->author_date;
3476 break;
3479 info->explore_queue.compare = compare_commits_by_gen_then_commit_date;
3480 info->indegree_queue.compare = compare_commits_by_gen_then_commit_date;
3482 info->min_generation = GENERATION_NUMBER_INFINITY;
3483 for (list = revs->commits; list; list = list->next) {
3484 struct commit *c = list->item;
3485 timestamp_t generation;
3487 if (repo_parse_commit_gently(revs->repo, c, 1))
3488 continue;
3490 test_flag_and_insert(&info->explore_queue, c, TOPO_WALK_EXPLORED);
3491 test_flag_and_insert(&info->indegree_queue, c, TOPO_WALK_INDEGREE);
3493 generation = commit_graph_generation(c);
3494 if (generation < info->min_generation)
3495 info->min_generation = generation;
3497 *(indegree_slab_at(&info->indegree, c)) = 1;
3499 if (revs->sort_order == REV_SORT_BY_AUTHOR_DATE)
3500 record_author_date(&info->author_date, c);
3502 compute_indegrees_to_depth(revs, info->min_generation);
3504 for (list = revs->commits; list; list = list->next) {
3505 struct commit *c = list->item;
3507 if (*(indegree_slab_at(&info->indegree, c)) == 1)
3508 prio_queue_put(&info->topo_queue, c);
3512 * This is unfortunate; the initial tips need to be shown
3513 * in the order given from the revision traversal machinery.
3515 if (revs->sort_order == REV_SORT_IN_GRAPH_ORDER)
3516 prio_queue_reverse(&info->topo_queue);
3518 if (trace2_is_enabled() && !topo_walk_atexit_registered) {
3519 atexit(trace2_topo_walk_statistics_atexit);
3520 topo_walk_atexit_registered = 1;
3524 static struct commit *next_topo_commit(struct rev_info *revs)
3526 struct commit *c;
3527 struct topo_walk_info *info = revs->topo_walk_info;
3529 /* pop next off of topo_queue */
3530 c = prio_queue_get(&info->topo_queue);
3532 if (c)
3533 *(indegree_slab_at(&info->indegree, c)) = 0;
3535 return c;
3538 static void expand_topo_walk(struct rev_info *revs, struct commit *commit)
3540 struct commit_list *p;
3541 struct topo_walk_info *info = revs->topo_walk_info;
3542 if (process_parents(revs, commit, NULL, NULL) < 0) {
3543 if (!revs->ignore_missing_links)
3544 die("Failed to traverse parents of commit %s",
3545 oid_to_hex(&commit->object.oid));
3548 count_topo_walked++;
3550 for (p = commit->parents; p; p = p->next) {
3551 struct commit *parent = p->item;
3552 int *pi;
3553 timestamp_t generation;
3555 if (parent->object.flags & UNINTERESTING)
3556 continue;
3558 if (repo_parse_commit_gently(revs->repo, parent, 1) < 0)
3559 continue;
3561 generation = commit_graph_generation(parent);
3562 if (generation < info->min_generation) {
3563 info->min_generation = generation;
3564 compute_indegrees_to_depth(revs, info->min_generation);
3567 pi = indegree_slab_at(&info->indegree, parent);
3569 (*pi)--;
3570 if (*pi == 1)
3571 prio_queue_put(&info->topo_queue, parent);
3573 if (revs->first_parent_only)
3574 return;
3578 int prepare_revision_walk(struct rev_info *revs)
3580 int i;
3581 struct object_array old_pending;
3582 struct commit_list **next = &revs->commits;
3584 memcpy(&old_pending, &revs->pending, sizeof(old_pending));
3585 revs->pending.nr = 0;
3586 revs->pending.alloc = 0;
3587 revs->pending.objects = NULL;
3588 for (i = 0; i < old_pending.nr; i++) {
3589 struct object_array_entry *e = old_pending.objects + i;
3590 struct commit *commit = handle_commit(revs, e);
3591 if (commit) {
3592 if (!(commit->object.flags & SEEN)) {
3593 commit->object.flags |= SEEN;
3594 next = commit_list_append(commit, next);
3598 object_array_clear(&old_pending);
3600 /* Signal whether we need per-parent treesame decoration */
3601 if (revs->simplify_merges ||
3602 (revs->limited && limiting_can_increase_treesame(revs)))
3603 revs->treesame.name = "treesame";
3605 if (revs->exclude_promisor_objects) {
3606 for_each_packed_object(mark_uninteresting, revs,
3607 FOR_EACH_OBJECT_PROMISOR_ONLY);
3610 if (!revs->reflog_info)
3611 prepare_to_use_bloom_filter(revs);
3612 if (!revs->unsorted_input)
3613 commit_list_sort_by_date(&revs->commits);
3614 if (revs->no_walk)
3615 return 0;
3616 if (revs->limited) {
3617 if (limit_list(revs) < 0)
3618 return -1;
3619 if (revs->topo_order)
3620 sort_in_topological_order(&revs->commits, revs->sort_order);
3621 } else if (revs->topo_order)
3622 init_topo_walk(revs);
3623 if (revs->line_level_traverse && want_ancestry(revs))
3625 * At the moment we can only do line-level log with parent
3626 * rewriting by performing this expensive pre-filtering step.
3627 * If parent rewriting is not requested, then we rather
3628 * perform the line-level log filtering during the regular
3629 * history traversal.
3631 line_log_filter(revs);
3632 if (revs->simplify_merges)
3633 simplify_merges(revs);
3634 if (revs->children.name)
3635 set_children(revs);
3637 return 0;
3640 static enum rewrite_result rewrite_one_1(struct rev_info *revs,
3641 struct commit **pp,
3642 struct prio_queue *queue)
3644 for (;;) {
3645 struct commit *p = *pp;
3646 if (!revs->limited)
3647 if (process_parents(revs, p, NULL, queue) < 0)
3648 return rewrite_one_error;
3649 if (p->object.flags & UNINTERESTING)
3650 return rewrite_one_ok;
3651 if (!(p->object.flags & TREESAME))
3652 return rewrite_one_ok;
3653 if (!p->parents)
3654 return rewrite_one_noparents;
3655 if ((p = one_relevant_parent(revs, p->parents)) == NULL)
3656 return rewrite_one_ok;
3657 *pp = p;
3661 static void merge_queue_into_list(struct prio_queue *q, struct commit_list **list)
3663 while (q->nr) {
3664 struct commit *item = prio_queue_peek(q);
3665 struct commit_list *p = *list;
3667 if (p && p->item->date >= item->date)
3668 list = &p->next;
3669 else {
3670 p = commit_list_insert(item, list);
3671 list = &p->next; /* skip newly added item */
3672 prio_queue_get(q); /* pop item */
3677 static enum rewrite_result rewrite_one(struct rev_info *revs, struct commit **pp)
3679 struct prio_queue queue = { compare_commits_by_commit_date };
3680 enum rewrite_result ret = rewrite_one_1(revs, pp, &queue);
3681 merge_queue_into_list(&queue, &revs->commits);
3682 clear_prio_queue(&queue);
3683 return ret;
3686 int rewrite_parents(struct rev_info *revs, struct commit *commit,
3687 rewrite_parent_fn_t rewrite_parent)
3689 struct commit_list **pp = &commit->parents;
3690 while (*pp) {
3691 struct commit_list *parent = *pp;
3692 switch (rewrite_parent(revs, &parent->item)) {
3693 case rewrite_one_ok:
3694 break;
3695 case rewrite_one_noparents:
3696 *pp = parent->next;
3697 continue;
3698 case rewrite_one_error:
3699 return -1;
3701 pp = &parent->next;
3703 remove_duplicate_parents(revs, commit);
3704 return 0;
3707 static int commit_rewrite_person(struct strbuf *buf, const char *what, struct string_list *mailmap)
3709 char *person, *endp;
3710 size_t len, namelen, maillen;
3711 const char *name;
3712 const char *mail;
3713 struct ident_split ident;
3715 person = strstr(buf->buf, what);
3716 if (!person)
3717 return 0;
3719 person += strlen(what);
3720 endp = strchr(person, '\n');
3721 if (!endp)
3722 return 0;
3724 len = endp - person;
3726 if (split_ident_line(&ident, person, len))
3727 return 0;
3729 mail = ident.mail_begin;
3730 maillen = ident.mail_end - ident.mail_begin;
3731 name = ident.name_begin;
3732 namelen = ident.name_end - ident.name_begin;
3734 if (map_user(mailmap, &mail, &maillen, &name, &namelen)) {
3735 struct strbuf namemail = STRBUF_INIT;
3737 strbuf_addf(&namemail, "%.*s <%.*s>",
3738 (int)namelen, name, (int)maillen, mail);
3740 strbuf_splice(buf, ident.name_begin - buf->buf,
3741 ident.mail_end - ident.name_begin + 1,
3742 namemail.buf, namemail.len);
3744 strbuf_release(&namemail);
3746 return 1;
3749 return 0;
3752 static int commit_match(struct commit *commit, struct rev_info *opt)
3754 int retval;
3755 const char *encoding;
3756 const char *message;
3757 struct strbuf buf = STRBUF_INIT;
3759 if (!opt->grep_filter.pattern_list && !opt->grep_filter.header_list)
3760 return 1;
3762 /* Prepend "fake" headers as needed */
3763 if (opt->grep_filter.use_reflog_filter) {
3764 strbuf_addstr(&buf, "reflog ");
3765 get_reflog_message(&buf, opt->reflog_info);
3766 strbuf_addch(&buf, '\n');
3770 * We grep in the user's output encoding, under the assumption that it
3771 * is the encoding they are most likely to write their grep pattern
3772 * for. In addition, it means we will match the "notes" encoding below,
3773 * so we will not end up with a buffer that has two different encodings
3774 * in it.
3776 encoding = get_log_output_encoding();
3777 message = logmsg_reencode(commit, NULL, encoding);
3779 /* Copy the commit to temporary if we are using "fake" headers */
3780 if (buf.len)
3781 strbuf_addstr(&buf, message);
3783 if (opt->grep_filter.header_list && opt->mailmap) {
3784 if (!buf.len)
3785 strbuf_addstr(&buf, message);
3787 commit_rewrite_person(&buf, "\nauthor ", opt->mailmap);
3788 commit_rewrite_person(&buf, "\ncommitter ", opt->mailmap);
3791 /* Append "fake" message parts as needed */
3792 if (opt->show_notes) {
3793 if (!buf.len)
3794 strbuf_addstr(&buf, message);
3795 format_display_notes(&commit->object.oid, &buf, encoding, 1);
3799 * Find either in the original commit message, or in the temporary.
3800 * Note that we cast away the constness of "message" here. It is
3801 * const because it may come from the cached commit buffer. That's OK,
3802 * because we know that it is modifiable heap memory, and that while
3803 * grep_buffer may modify it for speed, it will restore any
3804 * changes before returning.
3806 if (buf.len)
3807 retval = grep_buffer(&opt->grep_filter, buf.buf, buf.len);
3808 else
3809 retval = grep_buffer(&opt->grep_filter,
3810 (char *)message, strlen(message));
3811 strbuf_release(&buf);
3812 unuse_commit_buffer(commit, message);
3813 return retval;
3816 static inline int want_ancestry(const struct rev_info *revs)
3818 return (revs->rewrite_parents || revs->children.name);
3822 * Return a timestamp to be used for --since/--until comparisons for this
3823 * commit, based on the revision options.
3825 static timestamp_t comparison_date(const struct rev_info *revs,
3826 struct commit *commit)
3828 return revs->reflog_info ?
3829 get_reflog_timestamp(revs->reflog_info) :
3830 commit->date;
3833 enum commit_action get_commit_action(struct rev_info *revs, struct commit *commit)
3835 if (commit->object.flags & SHOWN)
3836 return commit_ignore;
3837 if (revs->unpacked && has_object_pack(&commit->object.oid))
3838 return commit_ignore;
3839 if (revs->no_kept_objects) {
3840 if (has_object_kept_pack(&commit->object.oid,
3841 revs->keep_pack_cache_flags))
3842 return commit_ignore;
3844 if (commit->object.flags & UNINTERESTING)
3845 return commit_ignore;
3846 if (revs->line_level_traverse && !want_ancestry(revs)) {
3848 * In case of line-level log with parent rewriting
3849 * prepare_revision_walk() already took care of all line-level
3850 * log filtering, and there is nothing left to do here.
3852 * If parent rewriting was not requested, then this is the
3853 * place to perform the line-level log filtering. Notably,
3854 * this check, though expensive, must come before the other,
3855 * cheaper filtering conditions, because the tracked line
3856 * ranges must be adjusted even when the commit will end up
3857 * being ignored based on other conditions.
3859 if (!line_log_process_ranges_arbitrary_commit(revs, commit))
3860 return commit_ignore;
3862 if (revs->min_age != -1 &&
3863 comparison_date(revs, commit) > revs->min_age)
3864 return commit_ignore;
3865 if (revs->min_parents || (revs->max_parents >= 0)) {
3866 int n = commit_list_count(commit->parents);
3867 if ((n < revs->min_parents) ||
3868 ((revs->max_parents >= 0) && (n > revs->max_parents)))
3869 return commit_ignore;
3871 if (!commit_match(commit, revs))
3872 return commit_ignore;
3873 if (revs->prune && revs->dense) {
3874 /* Commit without changes? */
3875 if (commit->object.flags & TREESAME) {
3876 int n;
3877 struct commit_list *p;
3878 /* drop merges unless we want parenthood */
3879 if (!want_ancestry(revs))
3880 return commit_ignore;
3882 if (revs->show_pulls && (commit->object.flags & PULL_MERGE))
3883 return commit_show;
3886 * If we want ancestry, then need to keep any merges
3887 * between relevant commits to tie together topology.
3888 * For consistency with TREESAME and simplification
3889 * use "relevant" here rather than just INTERESTING,
3890 * to treat bottom commit(s) as part of the topology.
3892 for (n = 0, p = commit->parents; p; p = p->next)
3893 if (relevant_commit(p->item))
3894 if (++n >= 2)
3895 return commit_show;
3896 return commit_ignore;
3899 return commit_show;
3902 define_commit_slab(saved_parents, struct commit_list *);
3904 #define EMPTY_PARENT_LIST ((struct commit_list *)-1)
3907 * You may only call save_parents() once per commit (this is checked
3908 * for non-root commits).
3910 static void save_parents(struct rev_info *revs, struct commit *commit)
3912 struct commit_list **pp;
3914 if (!revs->saved_parents_slab) {
3915 revs->saved_parents_slab = xmalloc(sizeof(struct saved_parents));
3916 init_saved_parents(revs->saved_parents_slab);
3919 pp = saved_parents_at(revs->saved_parents_slab, commit);
3922 * When walking with reflogs, we may visit the same commit
3923 * several times: once for each appearance in the reflog.
3925 * In this case, save_parents() will be called multiple times.
3926 * We want to keep only the first set of parents. We need to
3927 * store a sentinel value for an empty (i.e., NULL) parent
3928 * list to distinguish it from a not-yet-saved list, however.
3930 if (*pp)
3931 return;
3932 if (commit->parents)
3933 *pp = copy_commit_list(commit->parents);
3934 else
3935 *pp = EMPTY_PARENT_LIST;
3938 static void free_saved_parents(struct rev_info *revs)
3940 if (revs->saved_parents_slab)
3941 clear_saved_parents(revs->saved_parents_slab);
3944 struct commit_list *get_saved_parents(struct rev_info *revs, const struct commit *commit)
3946 struct commit_list *parents;
3948 if (!revs->saved_parents_slab)
3949 return commit->parents;
3951 parents = *saved_parents_at(revs->saved_parents_slab, commit);
3952 if (parents == EMPTY_PARENT_LIST)
3953 return NULL;
3954 return parents;
3957 enum commit_action simplify_commit(struct rev_info *revs, struct commit *commit)
3959 enum commit_action action = get_commit_action(revs, commit);
3961 if (action == commit_show &&
3962 revs->prune && revs->dense && want_ancestry(revs)) {
3964 * --full-diff on simplified parents is no good: it
3965 * will show spurious changes from the commits that
3966 * were elided. So we save the parents on the side
3967 * when --full-diff is in effect.
3969 if (revs->full_diff)
3970 save_parents(revs, commit);
3971 if (rewrite_parents(revs, commit, rewrite_one) < 0)
3972 return commit_error;
3974 return action;
3977 static void track_linear(struct rev_info *revs, struct commit *commit)
3979 if (revs->track_first_time) {
3980 revs->linear = 1;
3981 revs->track_first_time = 0;
3982 } else {
3983 struct commit_list *p;
3984 for (p = revs->previous_parents; p; p = p->next)
3985 if (p->item == NULL || /* first commit */
3986 oideq(&p->item->object.oid, &commit->object.oid))
3987 break;
3988 revs->linear = p != NULL;
3990 if (revs->reverse) {
3991 if (revs->linear)
3992 commit->object.flags |= TRACK_LINEAR;
3994 free_commit_list(revs->previous_parents);
3995 revs->previous_parents = copy_commit_list(commit->parents);
3998 static struct commit *get_revision_1(struct rev_info *revs)
4000 while (1) {
4001 struct commit *commit;
4003 if (revs->reflog_info)
4004 commit = next_reflog_entry(revs->reflog_info);
4005 else if (revs->topo_walk_info)
4006 commit = next_topo_commit(revs);
4007 else
4008 commit = pop_commit(&revs->commits);
4010 if (!commit)
4011 return NULL;
4013 if (revs->reflog_info)
4014 commit->object.flags &= ~(ADDED | SEEN | SHOWN);
4017 * If we haven't done the list limiting, we need to look at
4018 * the parents here. We also need to do the date-based limiting
4019 * that we'd otherwise have done in limit_list().
4021 if (!revs->limited) {
4022 if (revs->max_age != -1 &&
4023 comparison_date(revs, commit) < revs->max_age)
4024 continue;
4026 if (revs->reflog_info)
4027 try_to_simplify_commit(revs, commit);
4028 else if (revs->topo_walk_info)
4029 expand_topo_walk(revs, commit);
4030 else if (process_parents(revs, commit, &revs->commits, NULL) < 0) {
4031 if (!revs->ignore_missing_links)
4032 die("Failed to traverse parents of commit %s",
4033 oid_to_hex(&commit->object.oid));
4037 switch (simplify_commit(revs, commit)) {
4038 case commit_ignore:
4039 continue;
4040 case commit_error:
4041 die("Failed to simplify parents of commit %s",
4042 oid_to_hex(&commit->object.oid));
4043 default:
4044 if (revs->track_linear)
4045 track_linear(revs, commit);
4046 return commit;
4052 * Return true for entries that have not yet been shown. (This is an
4053 * object_array_each_func_t.)
4055 static int entry_unshown(struct object_array_entry *entry, void *cb_data_unused)
4057 return !(entry->item->flags & SHOWN);
4061 * If array is on the verge of a realloc, garbage-collect any entries
4062 * that have already been shown to try to free up some space.
4064 static void gc_boundary(struct object_array *array)
4066 if (array->nr == array->alloc)
4067 object_array_filter(array, entry_unshown, NULL);
4070 static void create_boundary_commit_list(struct rev_info *revs)
4072 unsigned i;
4073 struct commit *c;
4074 struct object_array *array = &revs->boundary_commits;
4075 struct object_array_entry *objects = array->objects;
4078 * If revs->commits is non-NULL at this point, an error occurred in
4079 * get_revision_1(). Ignore the error and continue printing the
4080 * boundary commits anyway. (This is what the code has always
4081 * done.)
4083 if (revs->commits) {
4084 free_commit_list(revs->commits);
4085 revs->commits = NULL;
4089 * Put all of the actual boundary commits from revs->boundary_commits
4090 * into revs->commits
4092 for (i = 0; i < array->nr; i++) {
4093 c = (struct commit *)(objects[i].item);
4094 if (!c)
4095 continue;
4096 if (!(c->object.flags & CHILD_SHOWN))
4097 continue;
4098 if (c->object.flags & (SHOWN | BOUNDARY))
4099 continue;
4100 c->object.flags |= BOUNDARY;
4101 commit_list_insert(c, &revs->commits);
4105 * If revs->topo_order is set, sort the boundary commits
4106 * in topological order
4108 sort_in_topological_order(&revs->commits, revs->sort_order);
4111 static struct commit *get_revision_internal(struct rev_info *revs)
4113 struct commit *c = NULL;
4114 struct commit_list *l;
4116 if (revs->boundary == 2) {
4118 * All of the normal commits have already been returned,
4119 * and we are now returning boundary commits.
4120 * create_boundary_commit_list() has populated
4121 * revs->commits with the remaining commits to return.
4123 c = pop_commit(&revs->commits);
4124 if (c)
4125 c->object.flags |= SHOWN;
4126 return c;
4130 * If our max_count counter has reached zero, then we are done. We
4131 * don't simply return NULL because we still might need to show
4132 * boundary commits. But we want to avoid calling get_revision_1, which
4133 * might do a considerable amount of work finding the next commit only
4134 * for us to throw it away.
4136 * If it is non-zero, then either we don't have a max_count at all
4137 * (-1), or it is still counting, in which case we decrement.
4139 if (revs->max_count) {
4140 c = get_revision_1(revs);
4141 if (c) {
4142 while (revs->skip_count > 0) {
4143 revs->skip_count--;
4144 c = get_revision_1(revs);
4145 if (!c)
4146 break;
4150 if (revs->max_count > 0)
4151 revs->max_count--;
4154 if (c)
4155 c->object.flags |= SHOWN;
4157 if (!revs->boundary)
4158 return c;
4160 if (!c) {
4162 * get_revision_1() runs out the commits, and
4163 * we are done computing the boundaries.
4164 * switch to boundary commits output mode.
4166 revs->boundary = 2;
4169 * Update revs->commits to contain the list of
4170 * boundary commits.
4172 create_boundary_commit_list(revs);
4174 return get_revision_internal(revs);
4178 * boundary commits are the commits that are parents of the
4179 * ones we got from get_revision_1() but they themselves are
4180 * not returned from get_revision_1(). Before returning
4181 * 'c', we need to mark its parents that they could be boundaries.
4184 for (l = c->parents; l; l = l->next) {
4185 struct object *p;
4186 p = &(l->item->object);
4187 if (p->flags & (CHILD_SHOWN | SHOWN))
4188 continue;
4189 p->flags |= CHILD_SHOWN;
4190 gc_boundary(&revs->boundary_commits);
4191 add_object_array(p, NULL, &revs->boundary_commits);
4194 return c;
4197 struct commit *get_revision(struct rev_info *revs)
4199 struct commit *c;
4200 struct commit_list *reversed;
4202 if (revs->reverse) {
4203 reversed = NULL;
4204 while ((c = get_revision_internal(revs)))
4205 commit_list_insert(c, &reversed);
4206 revs->commits = reversed;
4207 revs->reverse = 0;
4208 revs->reverse_output_stage = 1;
4211 if (revs->reverse_output_stage) {
4212 c = pop_commit(&revs->commits);
4213 if (revs->track_linear)
4214 revs->linear = !!(c && c->object.flags & TRACK_LINEAR);
4215 return c;
4218 c = get_revision_internal(revs);
4219 if (c && revs->graph)
4220 graph_update(revs->graph, c);
4221 if (!c) {
4222 free_saved_parents(revs);
4223 if (revs->previous_parents) {
4224 free_commit_list(revs->previous_parents);
4225 revs->previous_parents = NULL;
4228 return c;
4231 const char *get_revision_mark(const struct rev_info *revs, const struct commit *commit)
4233 if (commit->object.flags & BOUNDARY)
4234 return "-";
4235 else if (commit->object.flags & UNINTERESTING)
4236 return "^";
4237 else if (commit->object.flags & PATCHSAME)
4238 return "=";
4239 else if (!revs || revs->left_right) {
4240 if (commit->object.flags & SYMMETRIC_LEFT)
4241 return "<";
4242 else
4243 return ">";
4244 } else if (revs->graph)
4245 return "*";
4246 else if (revs->cherry_mark)
4247 return "+";
4248 return "";
4251 void put_revision_mark(const struct rev_info *revs, const struct commit *commit)
4253 const char *mark = get_revision_mark(revs, commit);
4254 if (!strlen(mark))
4255 return;
4256 fputs(mark, stdout);
4257 putchar(' ');