t4104: modernize and simplify quoting
[git/debian.git] / revision.c
blob6de29cdf7a1cbb6138ba0e9bbdd1216085674e76
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 "refs.h"
9 #include "revision.h"
10 #include "repository.h"
11 #include "graph.h"
12 #include "grep.h"
13 #include "reflog-walk.h"
14 #include "patch-ids.h"
15 #include "decorate.h"
16 #include "log-tree.h"
17 #include "string-list.h"
18 #include "line-log.h"
19 #include "mailmap.h"
20 #include "commit-slab.h"
21 #include "dir.h"
22 #include "cache-tree.h"
23 #include "bisect.h"
24 #include "packfile.h"
25 #include "worktree.h"
26 #include "argv-array.h"
27 #include "commit-reach.h"
28 #include "commit-graph.h"
29 #include "prio-queue.h"
30 #include "hashmap.h"
31 #include "utf8.h"
32 #include "bloom.h"
33 #include "json-writer.h"
35 volatile show_early_output_fn_t show_early_output;
37 static const char *term_bad;
38 static const char *term_good;
40 implement_shared_commit_slab(revision_sources, char *);
42 static inline int want_ancestry(const struct rev_info *revs);
44 void show_object_with_name(FILE *out, struct object *obj, const char *name)
46 const char *p;
48 fprintf(out, "%s ", oid_to_hex(&obj->oid));
49 for (p = name; *p && *p != '\n'; p++)
50 fputc(*p, out);
51 fputc('\n', out);
54 static void mark_blob_uninteresting(struct blob *blob)
56 if (!blob)
57 return;
58 if (blob->object.flags & UNINTERESTING)
59 return;
60 blob->object.flags |= UNINTERESTING;
63 static void mark_tree_contents_uninteresting(struct repository *r,
64 struct tree *tree)
66 struct tree_desc desc;
67 struct name_entry entry;
69 if (parse_tree_gently(tree, 1) < 0)
70 return;
72 init_tree_desc(&desc, tree->buffer, tree->size);
73 while (tree_entry(&desc, &entry)) {
74 switch (object_type(entry.mode)) {
75 case OBJ_TREE:
76 mark_tree_uninteresting(r, lookup_tree(r, &entry.oid));
77 break;
78 case OBJ_BLOB:
79 mark_blob_uninteresting(lookup_blob(r, &entry.oid));
80 break;
81 default:
82 /* Subproject commit - not in this repository */
83 break;
88 * We don't care about the tree any more
89 * after it has been marked uninteresting.
91 free_tree_buffer(tree);
94 void mark_tree_uninteresting(struct repository *r, struct tree *tree)
96 struct object *obj;
98 if (!tree)
99 return;
101 obj = &tree->object;
102 if (obj->flags & UNINTERESTING)
103 return;
104 obj->flags |= UNINTERESTING;
105 mark_tree_contents_uninteresting(r, tree);
108 struct path_and_oids_entry {
109 struct hashmap_entry ent;
110 char *path;
111 struct oidset trees;
114 static int path_and_oids_cmp(const void *hashmap_cmp_fn_data,
115 const struct hashmap_entry *eptr,
116 const struct hashmap_entry *entry_or_key,
117 const void *keydata)
119 const struct path_and_oids_entry *e1, *e2;
121 e1 = container_of(eptr, const struct path_and_oids_entry, ent);
122 e2 = container_of(entry_or_key, const struct path_and_oids_entry, ent);
124 return strcmp(e1->path, e2->path);
127 static void paths_and_oids_init(struct hashmap *map)
129 hashmap_init(map, path_and_oids_cmp, NULL, 0);
132 static void paths_and_oids_clear(struct hashmap *map)
134 struct hashmap_iter iter;
135 struct path_and_oids_entry *entry;
137 hashmap_for_each_entry(map, &iter, entry, ent /* member name */) {
138 oidset_clear(&entry->trees);
139 free(entry->path);
142 hashmap_free_entries(map, struct path_and_oids_entry, ent);
145 static void paths_and_oids_insert(struct hashmap *map,
146 const char *path,
147 const struct object_id *oid)
149 int hash = strhash(path);
150 struct path_and_oids_entry key;
151 struct path_and_oids_entry *entry;
153 hashmap_entry_init(&key.ent, hash);
155 /* use a shallow copy for the lookup */
156 key.path = (char *)path;
157 oidset_init(&key.trees, 0);
159 entry = hashmap_get_entry(map, &key, ent, NULL);
160 if (!entry) {
161 entry = xcalloc(1, sizeof(struct path_and_oids_entry));
162 hashmap_entry_init(&entry->ent, hash);
163 entry->path = xstrdup(key.path);
164 oidset_init(&entry->trees, 16);
165 hashmap_put(map, &entry->ent);
168 oidset_insert(&entry->trees, oid);
171 static void add_children_by_path(struct repository *r,
172 struct tree *tree,
173 struct hashmap *map)
175 struct tree_desc desc;
176 struct name_entry entry;
178 if (!tree)
179 return;
181 if (parse_tree_gently(tree, 1) < 0)
182 return;
184 init_tree_desc(&desc, tree->buffer, tree->size);
185 while (tree_entry(&desc, &entry)) {
186 switch (object_type(entry.mode)) {
187 case OBJ_TREE:
188 paths_and_oids_insert(map, entry.path, &entry.oid);
190 if (tree->object.flags & UNINTERESTING) {
191 struct tree *child = lookup_tree(r, &entry.oid);
192 if (child)
193 child->object.flags |= UNINTERESTING;
195 break;
196 case OBJ_BLOB:
197 if (tree->object.flags & UNINTERESTING) {
198 struct blob *child = lookup_blob(r, &entry.oid);
199 if (child)
200 child->object.flags |= UNINTERESTING;
202 break;
203 default:
204 /* Subproject commit - not in this repository */
205 break;
209 free_tree_buffer(tree);
212 void mark_trees_uninteresting_sparse(struct repository *r,
213 struct oidset *trees)
215 unsigned has_interesting = 0, has_uninteresting = 0;
216 struct hashmap map;
217 struct hashmap_iter map_iter;
218 struct path_and_oids_entry *entry;
219 struct object_id *oid;
220 struct oidset_iter iter;
222 oidset_iter_init(trees, &iter);
223 while ((!has_interesting || !has_uninteresting) &&
224 (oid = oidset_iter_next(&iter))) {
225 struct tree *tree = lookup_tree(r, oid);
227 if (!tree)
228 continue;
230 if (tree->object.flags & UNINTERESTING)
231 has_uninteresting = 1;
232 else
233 has_interesting = 1;
236 /* Do not walk unless we have both types of trees. */
237 if (!has_uninteresting || !has_interesting)
238 return;
240 paths_and_oids_init(&map);
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 { NULL, 0, 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 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);
298 void mark_parents_uninteresting(struct commit *commit)
300 struct commit_stack pending = COMMIT_STACK_INIT;
301 struct commit_list *l;
303 for (l = commit->parents; l; l = l->next)
304 mark_one_parent_uninteresting(l->item, &pending);
306 while (pending.nr > 0)
307 mark_one_parent_uninteresting(commit_stack_pop(&pending),
308 &pending);
310 commit_stack_clear(&pending);
313 static void add_pending_object_with_path(struct rev_info *revs,
314 struct object *obj,
315 const char *name, unsigned mode,
316 const char *path)
318 if (!obj)
319 return;
320 if (revs->no_walk && (obj->flags & UNINTERESTING))
321 revs->no_walk = 0;
322 if (revs->reflog_info && obj->type == OBJ_COMMIT) {
323 struct strbuf buf = STRBUF_INIT;
324 int len = interpret_branch_name(name, 0, &buf, 0);
326 if (0 < len && name[len] && buf.len)
327 strbuf_addstr(&buf, name + len);
328 add_reflog_for_walk(revs->reflog_info,
329 (struct commit *)obj,
330 buf.buf[0] ? buf.buf: name);
331 strbuf_release(&buf);
332 return; /* do not add the commit itself */
334 add_object_array_with_path(obj, name, &revs->pending, mode, path);
337 static void add_pending_object_with_mode(struct rev_info *revs,
338 struct object *obj,
339 const char *name, unsigned mode)
341 add_pending_object_with_path(revs, obj, name, mode, NULL);
344 void add_pending_object(struct rev_info *revs,
345 struct object *obj, const char *name)
347 add_pending_object_with_mode(revs, obj, name, S_IFINVALID);
350 void add_head_to_pending(struct rev_info *revs)
352 struct object_id oid;
353 struct object *obj;
354 if (get_oid("HEAD", &oid))
355 return;
356 obj = parse_object(revs->repo, &oid);
357 if (!obj)
358 return;
359 add_pending_object(revs, obj, "HEAD");
362 static struct object *get_reference(struct rev_info *revs, const char *name,
363 const struct object_id *oid,
364 unsigned int flags)
366 struct object *object;
369 * If the repository has commit graphs, repo_parse_commit() avoids
370 * reading the object buffer, so use it whenever possible.
372 if (oid_object_info(revs->repo, oid, NULL) == OBJ_COMMIT) {
373 struct commit *c = lookup_commit(revs->repo, oid);
374 if (!repo_parse_commit(revs->repo, c))
375 object = (struct object *) c;
376 else
377 object = NULL;
378 } else {
379 object = parse_object(revs->repo, oid);
382 if (!object) {
383 if (revs->ignore_missing)
384 return object;
385 if (revs->exclude_promisor_objects && is_promisor_object(oid))
386 return NULL;
387 die("bad object %s", name);
389 object->flags |= flags;
390 return object;
393 void add_pending_oid(struct rev_info *revs, const char *name,
394 const struct object_id *oid, unsigned int flags)
396 struct object *object = get_reference(revs, name, oid, flags);
397 add_pending_object(revs, object, name);
400 static struct commit *handle_commit(struct rev_info *revs,
401 struct object_array_entry *entry)
403 struct object *object = entry->item;
404 const char *name = entry->name;
405 const char *path = entry->path;
406 unsigned int mode = entry->mode;
407 unsigned long flags = object->flags;
410 * Tag object? Look what it points to..
412 while (object->type == OBJ_TAG) {
413 struct tag *tag = (struct tag *) object;
414 if (revs->tag_objects && !(flags & UNINTERESTING))
415 add_pending_object(revs, object, tag->tag);
416 object = parse_object(revs->repo, get_tagged_oid(tag));
417 if (!object) {
418 if (revs->ignore_missing_links || (flags & UNINTERESTING))
419 return NULL;
420 if (revs->exclude_promisor_objects &&
421 is_promisor_object(&tag->tagged->oid))
422 return NULL;
423 die("bad object %s", oid_to_hex(&tag->tagged->oid));
425 object->flags |= flags;
427 * We'll handle the tagged object by looping or dropping
428 * through to the non-tag handlers below. Do not
429 * propagate path data from the tag's pending entry.
431 path = NULL;
432 mode = 0;
436 * Commit object? Just return it, we'll do all the complex
437 * reachability crud.
439 if (object->type == OBJ_COMMIT) {
440 struct commit *commit = (struct commit *)object;
442 if (parse_commit(commit) < 0)
443 die("unable to parse commit %s", name);
444 if (flags & UNINTERESTING) {
445 mark_parents_uninteresting(commit);
447 if (!revs->topo_order || !generation_numbers_enabled(the_repository))
448 revs->limited = 1;
450 if (revs->sources) {
451 char **slot = revision_sources_at(revs->sources, commit);
453 if (!*slot)
454 *slot = xstrdup(name);
456 return commit;
460 * Tree object? Either mark it uninteresting, or add it
461 * to the list of objects to look at later..
463 if (object->type == OBJ_TREE) {
464 struct tree *tree = (struct tree *)object;
465 if (!revs->tree_objects)
466 return NULL;
467 if (flags & UNINTERESTING) {
468 mark_tree_contents_uninteresting(revs->repo, tree);
469 return NULL;
471 add_pending_object_with_path(revs, object, name, mode, path);
472 return NULL;
476 * Blob object? You know the drill by now..
478 if (object->type == OBJ_BLOB) {
479 if (!revs->blob_objects)
480 return NULL;
481 if (flags & UNINTERESTING)
482 return NULL;
483 add_pending_object_with_path(revs, object, name, mode, path);
484 return NULL;
486 die("%s is unknown object", name);
489 static int everybody_uninteresting(struct commit_list *orig,
490 struct commit **interesting_cache)
492 struct commit_list *list = orig;
494 if (*interesting_cache) {
495 struct commit *commit = *interesting_cache;
496 if (!(commit->object.flags & UNINTERESTING))
497 return 0;
500 while (list) {
501 struct commit *commit = list->item;
502 list = list->next;
503 if (commit->object.flags & UNINTERESTING)
504 continue;
506 *interesting_cache = commit;
507 return 0;
509 return 1;
513 * A definition of "relevant" commit that we can use to simplify limited graphs
514 * by eliminating side branches.
516 * A "relevant" commit is one that is !UNINTERESTING (ie we are including it
517 * in our list), or that is a specified BOTTOM commit. Then after computing
518 * a limited list, during processing we can generally ignore boundary merges
519 * coming from outside the graph, (ie from irrelevant parents), and treat
520 * those merges as if they were single-parent. TREESAME is defined to consider
521 * only relevant parents, if any. If we are TREESAME to our on-graph parents,
522 * we don't care if we were !TREESAME to non-graph parents.
524 * Treating bottom commits as relevant ensures that a limited graph's
525 * connection to the actual bottom commit is not viewed as a side branch, but
526 * treated as part of the graph. For example:
528 * ....Z...A---X---o---o---B
529 * . /
530 * W---Y
532 * When computing "A..B", the A-X connection is at least as important as
533 * Y-X, despite A being flagged UNINTERESTING.
535 * And when computing --ancestry-path "A..B", the A-X connection is more
536 * important than Y-X, despite both A and Y being flagged UNINTERESTING.
538 static inline int relevant_commit(struct commit *commit)
540 return (commit->object.flags & (UNINTERESTING | BOTTOM)) != UNINTERESTING;
544 * Return a single relevant commit from a parent list. If we are a TREESAME
545 * commit, and this selects one of our parents, then we can safely simplify to
546 * that parent.
548 static struct commit *one_relevant_parent(const struct rev_info *revs,
549 struct commit_list *orig)
551 struct commit_list *list = orig;
552 struct commit *relevant = NULL;
554 if (!orig)
555 return NULL;
558 * For 1-parent commits, or if first-parent-only, then return that
559 * first parent (even if not "relevant" by the above definition).
560 * TREESAME will have been set purely on that parent.
562 if (revs->first_parent_only || !orig->next)
563 return orig->item;
566 * For multi-parent commits, identify a sole relevant parent, if any.
567 * If we have only one relevant parent, then TREESAME will be set purely
568 * with regard to that parent, and we can simplify accordingly.
570 * If we have more than one relevant parent, or no relevant parents
571 * (and multiple irrelevant ones), then we can't select a parent here
572 * and return NULL.
574 while (list) {
575 struct commit *commit = list->item;
576 list = list->next;
577 if (relevant_commit(commit)) {
578 if (relevant)
579 return NULL;
580 relevant = commit;
583 return relevant;
587 * The goal is to get REV_TREE_NEW as the result only if the
588 * diff consists of all '+' (and no other changes), REV_TREE_OLD
589 * if the whole diff is removal of old data, and otherwise
590 * REV_TREE_DIFFERENT (of course if the trees are the same we
591 * want REV_TREE_SAME).
593 * The only time we care about the distinction is when
594 * remove_empty_trees is in effect, in which case we care only about
595 * whether the whole change is REV_TREE_NEW, or if there's another type
596 * of change. Which means we can stop the diff early in either of these
597 * cases:
599 * 1. We're not using remove_empty_trees at all.
601 * 2. We saw anything except REV_TREE_NEW.
603 static int tree_difference = REV_TREE_SAME;
605 static void file_add_remove(struct diff_options *options,
606 int addremove, unsigned mode,
607 const struct object_id *oid,
608 int oid_valid,
609 const char *fullpath, unsigned dirty_submodule)
611 int diff = addremove == '+' ? REV_TREE_NEW : REV_TREE_OLD;
612 struct rev_info *revs = options->change_fn_data;
614 tree_difference |= diff;
615 if (!revs->remove_empty_trees || tree_difference != REV_TREE_NEW)
616 options->flags.has_changes = 1;
619 static void file_change(struct diff_options *options,
620 unsigned old_mode, unsigned new_mode,
621 const struct object_id *old_oid,
622 const struct object_id *new_oid,
623 int old_oid_valid, int new_oid_valid,
624 const char *fullpath,
625 unsigned old_dirty_submodule, unsigned new_dirty_submodule)
627 tree_difference = REV_TREE_DIFFERENT;
628 options->flags.has_changes = 1;
631 static int bloom_filter_atexit_registered;
632 static unsigned int count_bloom_filter_maybe;
633 static unsigned int count_bloom_filter_definitely_not;
634 static unsigned int count_bloom_filter_false_positive;
635 static unsigned int count_bloom_filter_not_present;
637 static void trace2_bloom_filter_statistics_atexit(void)
639 struct json_writer jw = JSON_WRITER_INIT;
641 jw_object_begin(&jw, 0);
642 jw_object_intmax(&jw, "filter_not_present", count_bloom_filter_not_present);
643 jw_object_intmax(&jw, "maybe", count_bloom_filter_maybe);
644 jw_object_intmax(&jw, "definitely_not", count_bloom_filter_definitely_not);
645 jw_object_intmax(&jw, "false_positive", count_bloom_filter_false_positive);
646 jw_end(&jw);
648 trace2_data_json("bloom", the_repository, "statistics", &jw);
650 jw_release(&jw);
653 static int forbid_bloom_filters(struct pathspec *spec)
655 if (spec->has_wildcard)
656 return 1;
657 if (spec->nr > 1)
658 return 1;
659 if (spec->magic & ~PATHSPEC_LITERAL)
660 return 1;
661 if (spec->nr && (spec->items[0].magic & ~PATHSPEC_LITERAL))
662 return 1;
664 return 0;
667 static void prepare_to_use_bloom_filter(struct rev_info *revs)
669 struct pathspec_item *pi;
670 char *path_alloc = NULL;
671 const char *path, *p;
672 int last_index;
673 size_t len;
674 int path_component_nr = 1;
676 if (!revs->commits)
677 return;
679 if (forbid_bloom_filters(&revs->prune_data))
680 return;
682 repo_parse_commit(revs->repo, revs->commits->item);
684 if (!revs->repo->objects->commit_graph)
685 return;
687 revs->bloom_filter_settings = revs->repo->objects->commit_graph->bloom_filter_settings;
688 if (!revs->bloom_filter_settings)
689 return;
691 if (!revs->pruning.pathspec.nr)
692 return;
694 pi = &revs->pruning.pathspec.items[0];
695 last_index = pi->len - 1;
697 /* remove single trailing slash from path, if needed */
698 if (pi->match[last_index] == '/') {
699 path_alloc = xstrdup(pi->match);
700 path_alloc[last_index] = '\0';
701 path = path_alloc;
702 } else
703 path = pi->match;
705 len = strlen(path);
706 if (!len) {
707 revs->bloom_filter_settings = NULL;
708 return;
711 p = path;
712 while (*p) {
714 * At this point, the path is normalized to use Unix-style
715 * path separators. This is required due to how the
716 * changed-path Bloom filters store the paths.
718 if (*p == '/')
719 path_component_nr++;
720 p++;
723 revs->bloom_keys_nr = path_component_nr;
724 ALLOC_ARRAY(revs->bloom_keys, revs->bloom_keys_nr);
726 fill_bloom_key(path, len, &revs->bloom_keys[0],
727 revs->bloom_filter_settings);
728 path_component_nr = 1;
730 p = path + len - 1;
731 while (p > path) {
732 if (*p == '/')
733 fill_bloom_key(path, p - path,
734 &revs->bloom_keys[path_component_nr++],
735 revs->bloom_filter_settings);
736 p--;
739 if (trace2_is_enabled() && !bloom_filter_atexit_registered) {
740 atexit(trace2_bloom_filter_statistics_atexit);
741 bloom_filter_atexit_registered = 1;
744 free(path_alloc);
747 static int check_maybe_different_in_bloom_filter(struct rev_info *revs,
748 struct commit *commit)
750 struct bloom_filter *filter;
751 int result = 1, j;
753 if (!revs->repo->objects->commit_graph)
754 return -1;
756 if (commit_graph_generation(commit) == GENERATION_NUMBER_INFINITY)
757 return -1;
759 filter = get_bloom_filter(revs->repo, commit, 0);
761 if (!filter) {
762 count_bloom_filter_not_present++;
763 return -1;
766 for (j = 0; result && j < revs->bloom_keys_nr; j++) {
767 result = bloom_filter_contains(filter,
768 &revs->bloom_keys[j],
769 revs->bloom_filter_settings);
772 if (result)
773 count_bloom_filter_maybe++;
774 else
775 count_bloom_filter_definitely_not++;
777 return result;
780 static int rev_compare_tree(struct rev_info *revs,
781 struct commit *parent, struct commit *commit, int nth_parent)
783 struct tree *t1 = get_commit_tree(parent);
784 struct tree *t2 = get_commit_tree(commit);
785 int bloom_ret = 1;
787 if (!t1)
788 return REV_TREE_NEW;
789 if (!t2)
790 return REV_TREE_OLD;
792 if (revs->simplify_by_decoration) {
794 * If we are simplifying by decoration, then the commit
795 * is worth showing if it has a tag pointing at it.
797 if (get_name_decoration(&commit->object))
798 return REV_TREE_DIFFERENT;
800 * A commit that is not pointed by a tag is uninteresting
801 * if we are not limited by path. This means that you will
802 * see the usual "commits that touch the paths" plus any
803 * tagged commit by specifying both --simplify-by-decoration
804 * and pathspec.
806 if (!revs->prune_data.nr)
807 return REV_TREE_SAME;
810 if (revs->bloom_keys_nr && !nth_parent) {
811 bloom_ret = check_maybe_different_in_bloom_filter(revs, commit);
813 if (bloom_ret == 0)
814 return REV_TREE_SAME;
817 tree_difference = REV_TREE_SAME;
818 revs->pruning.flags.has_changes = 0;
819 diff_tree_oid(&t1->object.oid, &t2->object.oid, "", &revs->pruning);
821 if (!nth_parent)
822 if (bloom_ret == 1 && tree_difference == REV_TREE_SAME)
823 count_bloom_filter_false_positive++;
825 return tree_difference;
828 static int rev_same_tree_as_empty(struct rev_info *revs, struct commit *commit)
830 struct tree *t1 = get_commit_tree(commit);
832 if (!t1)
833 return 0;
835 tree_difference = REV_TREE_SAME;
836 revs->pruning.flags.has_changes = 0;
837 diff_tree_oid(NULL, &t1->object.oid, "", &revs->pruning);
839 return tree_difference == REV_TREE_SAME;
842 struct treesame_state {
843 unsigned int nparents;
844 unsigned char treesame[FLEX_ARRAY];
847 static struct treesame_state *initialise_treesame(struct rev_info *revs, struct commit *commit)
849 unsigned n = commit_list_count(commit->parents);
850 struct treesame_state *st = xcalloc(1, st_add(sizeof(*st), n));
851 st->nparents = n;
852 add_decoration(&revs->treesame, &commit->object, st);
853 return st;
857 * Must be called immediately after removing the nth_parent from a commit's
858 * parent list, if we are maintaining the per-parent treesame[] decoration.
859 * This does not recalculate the master TREESAME flag - update_treesame()
860 * should be called to update it after a sequence of treesame[] modifications
861 * that may have affected it.
863 static int compact_treesame(struct rev_info *revs, struct commit *commit, unsigned nth_parent)
865 struct treesame_state *st;
866 int old_same;
868 if (!commit->parents) {
870 * Have just removed the only parent from a non-merge.
871 * Different handling, as we lack decoration.
873 if (nth_parent != 0)
874 die("compact_treesame %u", nth_parent);
875 old_same = !!(commit->object.flags & TREESAME);
876 if (rev_same_tree_as_empty(revs, commit))
877 commit->object.flags |= TREESAME;
878 else
879 commit->object.flags &= ~TREESAME;
880 return old_same;
883 st = lookup_decoration(&revs->treesame, &commit->object);
884 if (!st || nth_parent >= st->nparents)
885 die("compact_treesame %u", nth_parent);
887 old_same = st->treesame[nth_parent];
888 memmove(st->treesame + nth_parent,
889 st->treesame + nth_parent + 1,
890 st->nparents - nth_parent - 1);
893 * If we've just become a non-merge commit, update TREESAME
894 * immediately, and remove the no-longer-needed decoration.
895 * If still a merge, defer update until update_treesame().
897 if (--st->nparents == 1) {
898 if (commit->parents->next)
899 die("compact_treesame parents mismatch");
900 if (st->treesame[0] && revs->dense)
901 commit->object.flags |= TREESAME;
902 else
903 commit->object.flags &= ~TREESAME;
904 free(add_decoration(&revs->treesame, &commit->object, NULL));
907 return old_same;
910 static unsigned update_treesame(struct rev_info *revs, struct commit *commit)
912 if (commit->parents && commit->parents->next) {
913 unsigned n;
914 struct treesame_state *st;
915 struct commit_list *p;
916 unsigned relevant_parents;
917 unsigned relevant_change, irrelevant_change;
919 st = lookup_decoration(&revs->treesame, &commit->object);
920 if (!st)
921 die("update_treesame %s", oid_to_hex(&commit->object.oid));
922 relevant_parents = 0;
923 relevant_change = irrelevant_change = 0;
924 for (p = commit->parents, n = 0; p; n++, p = p->next) {
925 if (relevant_commit(p->item)) {
926 relevant_change |= !st->treesame[n];
927 relevant_parents++;
928 } else
929 irrelevant_change |= !st->treesame[n];
931 if (relevant_parents ? relevant_change : irrelevant_change)
932 commit->object.flags &= ~TREESAME;
933 else
934 commit->object.flags |= TREESAME;
937 return commit->object.flags & TREESAME;
940 static inline int limiting_can_increase_treesame(const struct rev_info *revs)
943 * TREESAME is irrelevant unless prune && dense;
944 * if simplify_history is set, we can't have a mixture of TREESAME and
945 * !TREESAME INTERESTING parents (and we don't have treesame[]
946 * decoration anyway);
947 * if first_parent_only is set, then the TREESAME flag is locked
948 * against the first parent (and again we lack treesame[] decoration).
950 return revs->prune && revs->dense &&
951 !revs->simplify_history &&
952 !revs->first_parent_only;
955 static void try_to_simplify_commit(struct rev_info *revs, struct commit *commit)
957 struct commit_list **pp, *parent;
958 struct treesame_state *ts = NULL;
959 int relevant_change = 0, irrelevant_change = 0;
960 int relevant_parents, nth_parent;
963 * If we don't do pruning, everything is interesting
965 if (!revs->prune)
966 return;
968 if (!get_commit_tree(commit))
969 return;
971 if (!commit->parents) {
972 if (rev_same_tree_as_empty(revs, commit))
973 commit->object.flags |= TREESAME;
974 return;
978 * Normal non-merge commit? If we don't want to make the
979 * history dense, we consider it always to be a change..
981 if (!revs->dense && !commit->parents->next)
982 return;
984 for (pp = &commit->parents, nth_parent = 0, relevant_parents = 0;
985 (parent = *pp) != NULL;
986 pp = &parent->next, nth_parent++) {
987 struct commit *p = parent->item;
988 if (relevant_commit(p))
989 relevant_parents++;
991 if (nth_parent == 1) {
993 * This our second loop iteration - so we now know
994 * we're dealing with a merge.
996 * Do not compare with later parents when we care only about
997 * the first parent chain, in order to avoid derailing the
998 * traversal to follow a side branch that brought everything
999 * in the path we are limited to by the pathspec.
1001 if (revs->first_parent_only)
1002 break;
1004 * If this will remain a potentially-simplifiable
1005 * merge, remember per-parent treesame if needed.
1006 * Initialise the array with the comparison from our
1007 * first iteration.
1009 if (revs->treesame.name &&
1010 !revs->simplify_history &&
1011 !(commit->object.flags & UNINTERESTING)) {
1012 ts = initialise_treesame(revs, commit);
1013 if (!(irrelevant_change || relevant_change))
1014 ts->treesame[0] = 1;
1017 if (parse_commit(p) < 0)
1018 die("cannot simplify commit %s (because of %s)",
1019 oid_to_hex(&commit->object.oid),
1020 oid_to_hex(&p->object.oid));
1021 switch (rev_compare_tree(revs, p, commit, nth_parent)) {
1022 case REV_TREE_SAME:
1023 if (!revs->simplify_history || !relevant_commit(p)) {
1024 /* Even if a merge with an uninteresting
1025 * side branch brought the entire change
1026 * we are interested in, we do not want
1027 * to lose the other branches of this
1028 * merge, so we just keep going.
1030 if (ts)
1031 ts->treesame[nth_parent] = 1;
1032 continue;
1034 parent->next = NULL;
1035 commit->parents = parent;
1038 * A merge commit is a "diversion" if it is not
1039 * TREESAME to its first parent but is TREESAME
1040 * to a later parent. In the simplified history,
1041 * we "divert" the history walk to the later
1042 * parent. These commits are shown when "show_pulls"
1043 * is enabled, so do not mark the object as
1044 * TREESAME here.
1046 if (!revs->show_pulls || !nth_parent)
1047 commit->object.flags |= TREESAME;
1049 return;
1051 case REV_TREE_NEW:
1052 if (revs->remove_empty_trees &&
1053 rev_same_tree_as_empty(revs, p)) {
1054 /* We are adding all the specified
1055 * paths from this parent, so the
1056 * history beyond this parent is not
1057 * interesting. Remove its parents
1058 * (they are grandparents for us).
1059 * IOW, we pretend this parent is a
1060 * "root" commit.
1062 if (parse_commit(p) < 0)
1063 die("cannot simplify commit %s (invalid %s)",
1064 oid_to_hex(&commit->object.oid),
1065 oid_to_hex(&p->object.oid));
1066 p->parents = NULL;
1068 /* fallthrough */
1069 case REV_TREE_OLD:
1070 case REV_TREE_DIFFERENT:
1071 if (relevant_commit(p))
1072 relevant_change = 1;
1073 else
1074 irrelevant_change = 1;
1076 if (!nth_parent)
1077 commit->object.flags |= PULL_MERGE;
1079 continue;
1081 die("bad tree compare for commit %s", oid_to_hex(&commit->object.oid));
1085 * TREESAME is straightforward for single-parent commits. For merge
1086 * commits, it is most useful to define it so that "irrelevant"
1087 * parents cannot make us !TREESAME - if we have any relevant
1088 * parents, then we only consider TREESAMEness with respect to them,
1089 * allowing irrelevant merges from uninteresting branches to be
1090 * simplified away. Only if we have only irrelevant parents do we
1091 * base TREESAME on them. Note that this logic is replicated in
1092 * update_treesame, which should be kept in sync.
1094 if (relevant_parents ? !relevant_change : !irrelevant_change)
1095 commit->object.flags |= TREESAME;
1098 static int process_parents(struct rev_info *revs, struct commit *commit,
1099 struct commit_list **list, struct prio_queue *queue)
1101 struct commit_list *parent = commit->parents;
1102 unsigned left_flag;
1104 if (commit->object.flags & ADDED)
1105 return 0;
1106 commit->object.flags |= ADDED;
1108 if (revs->include_check &&
1109 !revs->include_check(commit, revs->include_check_data))
1110 return 0;
1113 * If the commit is uninteresting, don't try to
1114 * prune parents - we want the maximal uninteresting
1115 * set.
1117 * Normally we haven't parsed the parent
1118 * yet, so we won't have a parent of a parent
1119 * here. However, it may turn out that we've
1120 * reached this commit some other way (where it
1121 * wasn't uninteresting), in which case we need
1122 * to mark its parents recursively too..
1124 if (commit->object.flags & UNINTERESTING) {
1125 while (parent) {
1126 struct commit *p = parent->item;
1127 parent = parent->next;
1128 if (p)
1129 p->object.flags |= UNINTERESTING;
1130 if (parse_commit_gently(p, 1) < 0)
1131 continue;
1132 if (p->parents)
1133 mark_parents_uninteresting(p);
1134 if (p->object.flags & SEEN)
1135 continue;
1136 p->object.flags |= SEEN;
1137 if (list)
1138 commit_list_insert_by_date(p, list);
1139 if (queue)
1140 prio_queue_put(queue, p);
1142 return 0;
1146 * Ok, the commit wasn't uninteresting. Try to
1147 * simplify the commit history and find the parent
1148 * that has no differences in the path set if one exists.
1150 try_to_simplify_commit(revs, commit);
1152 if (revs->no_walk)
1153 return 0;
1155 left_flag = (commit->object.flags & SYMMETRIC_LEFT);
1157 for (parent = commit->parents; parent; parent = parent->next) {
1158 struct commit *p = parent->item;
1159 int gently = revs->ignore_missing_links ||
1160 revs->exclude_promisor_objects;
1161 if (parse_commit_gently(p, gently) < 0) {
1162 if (revs->exclude_promisor_objects &&
1163 is_promisor_object(&p->object.oid)) {
1164 if (revs->first_parent_only)
1165 break;
1166 continue;
1168 return -1;
1170 if (revs->sources) {
1171 char **slot = revision_sources_at(revs->sources, p);
1173 if (!*slot)
1174 *slot = *revision_sources_at(revs->sources, commit);
1176 p->object.flags |= left_flag;
1177 if (!(p->object.flags & SEEN)) {
1178 p->object.flags |= SEEN;
1179 if (list)
1180 commit_list_insert_by_date(p, list);
1181 if (queue)
1182 prio_queue_put(queue, p);
1184 if (revs->first_parent_only)
1185 break;
1187 return 0;
1190 static void cherry_pick_list(struct commit_list *list, struct rev_info *revs)
1192 struct commit_list *p;
1193 int left_count = 0, right_count = 0;
1194 int left_first;
1195 struct patch_ids ids;
1196 unsigned cherry_flag;
1198 /* First count the commits on the left and on the right */
1199 for (p = list; p; p = p->next) {
1200 struct commit *commit = p->item;
1201 unsigned flags = commit->object.flags;
1202 if (flags & BOUNDARY)
1204 else if (flags & SYMMETRIC_LEFT)
1205 left_count++;
1206 else
1207 right_count++;
1210 if (!left_count || !right_count)
1211 return;
1213 left_first = left_count < right_count;
1214 init_patch_ids(revs->repo, &ids);
1215 ids.diffopts.pathspec = revs->diffopt.pathspec;
1217 /* Compute patch-ids for one side */
1218 for (p = list; p; p = p->next) {
1219 struct commit *commit = p->item;
1220 unsigned flags = commit->object.flags;
1222 if (flags & BOUNDARY)
1223 continue;
1225 * If we have fewer left, left_first is set and we omit
1226 * commits on the right branch in this loop. If we have
1227 * fewer right, we skip the left ones.
1229 if (left_first != !!(flags & SYMMETRIC_LEFT))
1230 continue;
1231 add_commit_patch_id(commit, &ids);
1234 /* either cherry_mark or cherry_pick are true */
1235 cherry_flag = revs->cherry_mark ? PATCHSAME : SHOWN;
1237 /* Check the other side */
1238 for (p = list; p; p = p->next) {
1239 struct commit *commit = p->item;
1240 struct patch_id *id;
1241 unsigned flags = commit->object.flags;
1243 if (flags & BOUNDARY)
1244 continue;
1246 * If we have fewer left, left_first is set and we omit
1247 * commits on the left branch in this loop.
1249 if (left_first == !!(flags & SYMMETRIC_LEFT))
1250 continue;
1253 * Have we seen the same patch id?
1255 id = has_commit_patch_id(commit, &ids);
1256 if (!id)
1257 continue;
1259 commit->object.flags |= cherry_flag;
1260 id->commit->object.flags |= cherry_flag;
1263 free_patch_ids(&ids);
1266 /* How many extra uninteresting commits we want to see.. */
1267 #define SLOP 5
1269 static int still_interesting(struct commit_list *src, timestamp_t date, int slop,
1270 struct commit **interesting_cache)
1273 * No source list at all? We're definitely done..
1275 if (!src)
1276 return 0;
1279 * Does the destination list contain entries with a date
1280 * before the source list? Definitely _not_ done.
1282 if (date <= src->item->date)
1283 return SLOP;
1286 * Does the source list still have interesting commits in
1287 * it? Definitely not done..
1289 if (!everybody_uninteresting(src, interesting_cache))
1290 return SLOP;
1292 /* Ok, we're closing in.. */
1293 return slop-1;
1297 * "rev-list --ancestry-path A..B" computes commits that are ancestors
1298 * of B but not ancestors of A but further limits the result to those
1299 * that are descendants of A. This takes the list of bottom commits and
1300 * the result of "A..B" without --ancestry-path, and limits the latter
1301 * further to the ones that can reach one of the commits in "bottom".
1303 static void limit_to_ancestry(struct commit_list *bottom, struct commit_list *list)
1305 struct commit_list *p;
1306 struct commit_list *rlist = NULL;
1307 int made_progress;
1310 * Reverse the list so that it will be likely that we would
1311 * process parents before children.
1313 for (p = list; p; p = p->next)
1314 commit_list_insert(p->item, &rlist);
1316 for (p = bottom; p; p = p->next)
1317 p->item->object.flags |= TMP_MARK;
1320 * Mark the ones that can reach bottom commits in "list",
1321 * in a bottom-up fashion.
1323 do {
1324 made_progress = 0;
1325 for (p = rlist; p; p = p->next) {
1326 struct commit *c = p->item;
1327 struct commit_list *parents;
1328 if (c->object.flags & (TMP_MARK | UNINTERESTING))
1329 continue;
1330 for (parents = c->parents;
1331 parents;
1332 parents = parents->next) {
1333 if (!(parents->item->object.flags & TMP_MARK))
1334 continue;
1335 c->object.flags |= TMP_MARK;
1336 made_progress = 1;
1337 break;
1340 } while (made_progress);
1343 * NEEDSWORK: decide if we want to remove parents that are
1344 * not marked with TMP_MARK from commit->parents for commits
1345 * in the resulting list. We may not want to do that, though.
1349 * The ones that are not marked with TMP_MARK are uninteresting
1351 for (p = list; p; p = p->next) {
1352 struct commit *c = p->item;
1353 if (c->object.flags & TMP_MARK)
1354 continue;
1355 c->object.flags |= UNINTERESTING;
1358 /* We are done with the TMP_MARK */
1359 for (p = list; p; p = p->next)
1360 p->item->object.flags &= ~TMP_MARK;
1361 for (p = bottom; p; p = p->next)
1362 p->item->object.flags &= ~TMP_MARK;
1363 free_commit_list(rlist);
1367 * Before walking the history, keep the set of "negative" refs the
1368 * caller has asked to exclude.
1370 * This is used to compute "rev-list --ancestry-path A..B", as we need
1371 * to filter the result of "A..B" further to the ones that can actually
1372 * reach A.
1374 static struct commit_list *collect_bottom_commits(struct commit_list *list)
1376 struct commit_list *elem, *bottom = NULL;
1377 for (elem = list; elem; elem = elem->next)
1378 if (elem->item->object.flags & BOTTOM)
1379 commit_list_insert(elem->item, &bottom);
1380 return bottom;
1383 /* Assumes either left_only or right_only is set */
1384 static void limit_left_right(struct commit_list *list, struct rev_info *revs)
1386 struct commit_list *p;
1388 for (p = list; p; p = p->next) {
1389 struct commit *commit = p->item;
1391 if (revs->right_only) {
1392 if (commit->object.flags & SYMMETRIC_LEFT)
1393 commit->object.flags |= SHOWN;
1394 } else /* revs->left_only is set */
1395 if (!(commit->object.flags & SYMMETRIC_LEFT))
1396 commit->object.flags |= SHOWN;
1400 static int limit_list(struct rev_info *revs)
1402 int slop = SLOP;
1403 timestamp_t date = TIME_MAX;
1404 struct commit_list *list = revs->commits;
1405 struct commit_list *newlist = NULL;
1406 struct commit_list **p = &newlist;
1407 struct commit_list *bottom = NULL;
1408 struct commit *interesting_cache = NULL;
1410 if (revs->ancestry_path) {
1411 bottom = collect_bottom_commits(list);
1412 if (!bottom)
1413 die("--ancestry-path given but there are no bottom commits");
1416 while (list) {
1417 struct commit *commit = pop_commit(&list);
1418 struct object *obj = &commit->object;
1419 show_early_output_fn_t show;
1421 if (commit == interesting_cache)
1422 interesting_cache = NULL;
1424 if (revs->max_age != -1 && (commit->date < revs->max_age))
1425 obj->flags |= UNINTERESTING;
1426 if (process_parents(revs, commit, &list, NULL) < 0)
1427 return -1;
1428 if (obj->flags & UNINTERESTING) {
1429 mark_parents_uninteresting(commit);
1430 slop = still_interesting(list, date, slop, &interesting_cache);
1431 if (slop)
1432 continue;
1433 break;
1435 if (revs->min_age != -1 && (commit->date > revs->min_age) &&
1436 !revs->line_level_traverse)
1437 continue;
1438 date = commit->date;
1439 p = &commit_list_insert(commit, p)->next;
1441 show = show_early_output;
1442 if (!show)
1443 continue;
1445 show(revs, newlist);
1446 show_early_output = NULL;
1448 if (revs->cherry_pick || revs->cherry_mark)
1449 cherry_pick_list(newlist, revs);
1451 if (revs->left_only || revs->right_only)
1452 limit_left_right(newlist, revs);
1454 if (bottom) {
1455 limit_to_ancestry(bottom, newlist);
1456 free_commit_list(bottom);
1460 * Check if any commits have become TREESAME by some of their parents
1461 * becoming UNINTERESTING.
1463 if (limiting_can_increase_treesame(revs))
1464 for (list = newlist; list; list = list->next) {
1465 struct commit *c = list->item;
1466 if (c->object.flags & (UNINTERESTING | TREESAME))
1467 continue;
1468 update_treesame(revs, c);
1471 revs->commits = newlist;
1472 return 0;
1476 * Add an entry to refs->cmdline with the specified information.
1477 * *name is copied.
1479 static void add_rev_cmdline(struct rev_info *revs,
1480 struct object *item,
1481 const char *name,
1482 int whence,
1483 unsigned flags)
1485 struct rev_cmdline_info *info = &revs->cmdline;
1486 unsigned int nr = info->nr;
1488 ALLOC_GROW(info->rev, nr + 1, info->alloc);
1489 info->rev[nr].item = item;
1490 info->rev[nr].name = xstrdup(name);
1491 info->rev[nr].whence = whence;
1492 info->rev[nr].flags = flags;
1493 info->nr++;
1496 static void add_rev_cmdline_list(struct rev_info *revs,
1497 struct commit_list *commit_list,
1498 int whence,
1499 unsigned flags)
1501 while (commit_list) {
1502 struct object *object = &commit_list->item->object;
1503 add_rev_cmdline(revs, object, oid_to_hex(&object->oid),
1504 whence, flags);
1505 commit_list = commit_list->next;
1509 struct all_refs_cb {
1510 int all_flags;
1511 int warned_bad_reflog;
1512 struct rev_info *all_revs;
1513 const char *name_for_errormsg;
1514 struct worktree *wt;
1517 int ref_excluded(struct string_list *ref_excludes, const char *path)
1519 struct string_list_item *item;
1521 if (!ref_excludes)
1522 return 0;
1523 for_each_string_list_item(item, ref_excludes) {
1524 if (!wildmatch(item->string, path, 0))
1525 return 1;
1527 return 0;
1530 static int handle_one_ref(const char *path, const struct object_id *oid,
1531 int flag, void *cb_data)
1533 struct all_refs_cb *cb = cb_data;
1534 struct object *object;
1536 if (ref_excluded(cb->all_revs->ref_excludes, path))
1537 return 0;
1539 object = get_reference(cb->all_revs, path, oid, cb->all_flags);
1540 add_rev_cmdline(cb->all_revs, object, path, REV_CMD_REF, cb->all_flags);
1541 add_pending_oid(cb->all_revs, path, oid, cb->all_flags);
1542 return 0;
1545 static void init_all_refs_cb(struct all_refs_cb *cb, struct rev_info *revs,
1546 unsigned flags)
1548 cb->all_revs = revs;
1549 cb->all_flags = flags;
1550 revs->rev_input_given = 1;
1551 cb->wt = NULL;
1554 void clear_ref_exclusion(struct string_list **ref_excludes_p)
1556 if (*ref_excludes_p) {
1557 string_list_clear(*ref_excludes_p, 0);
1558 free(*ref_excludes_p);
1560 *ref_excludes_p = NULL;
1563 void add_ref_exclusion(struct string_list **ref_excludes_p, const char *exclude)
1565 if (!*ref_excludes_p) {
1566 *ref_excludes_p = xcalloc(1, sizeof(**ref_excludes_p));
1567 (*ref_excludes_p)->strdup_strings = 1;
1569 string_list_append(*ref_excludes_p, exclude);
1572 static void handle_refs(struct ref_store *refs,
1573 struct rev_info *revs, unsigned flags,
1574 int (*for_each)(struct ref_store *, each_ref_fn, void *))
1576 struct all_refs_cb cb;
1578 if (!refs) {
1579 /* this could happen with uninitialized submodules */
1580 return;
1583 init_all_refs_cb(&cb, revs, flags);
1584 for_each(refs, handle_one_ref, &cb);
1587 static void handle_one_reflog_commit(struct object_id *oid, void *cb_data)
1589 struct all_refs_cb *cb = cb_data;
1590 if (!is_null_oid(oid)) {
1591 struct object *o = parse_object(cb->all_revs->repo, oid);
1592 if (o) {
1593 o->flags |= cb->all_flags;
1594 /* ??? CMDLINEFLAGS ??? */
1595 add_pending_object(cb->all_revs, o, "");
1597 else if (!cb->warned_bad_reflog) {
1598 warning("reflog of '%s' references pruned commits",
1599 cb->name_for_errormsg);
1600 cb->warned_bad_reflog = 1;
1605 static int handle_one_reflog_ent(struct object_id *ooid, struct object_id *noid,
1606 const char *email, timestamp_t timestamp, int tz,
1607 const char *message, void *cb_data)
1609 handle_one_reflog_commit(ooid, cb_data);
1610 handle_one_reflog_commit(noid, cb_data);
1611 return 0;
1614 static int handle_one_reflog(const char *refname_in_wt,
1615 const struct object_id *oid,
1616 int flag, void *cb_data)
1618 struct all_refs_cb *cb = cb_data;
1619 struct strbuf refname = STRBUF_INIT;
1621 cb->warned_bad_reflog = 0;
1622 strbuf_worktree_ref(cb->wt, &refname, refname_in_wt);
1623 cb->name_for_errormsg = refname.buf;
1624 refs_for_each_reflog_ent(get_main_ref_store(the_repository),
1625 refname.buf,
1626 handle_one_reflog_ent, cb_data);
1627 strbuf_release(&refname);
1628 return 0;
1631 static void add_other_reflogs_to_pending(struct all_refs_cb *cb)
1633 struct worktree **worktrees, **p;
1635 worktrees = get_worktrees();
1636 for (p = worktrees; *p; p++) {
1637 struct worktree *wt = *p;
1639 if (wt->is_current)
1640 continue;
1642 cb->wt = wt;
1643 refs_for_each_reflog(get_worktree_ref_store(wt),
1644 handle_one_reflog,
1645 cb);
1647 free_worktrees(worktrees);
1650 void add_reflogs_to_pending(struct rev_info *revs, unsigned flags)
1652 struct all_refs_cb cb;
1654 cb.all_revs = revs;
1655 cb.all_flags = flags;
1656 cb.wt = NULL;
1657 for_each_reflog(handle_one_reflog, &cb);
1659 if (!revs->single_worktree)
1660 add_other_reflogs_to_pending(&cb);
1663 static void add_cache_tree(struct cache_tree *it, struct rev_info *revs,
1664 struct strbuf *path, unsigned int flags)
1666 size_t baselen = path->len;
1667 int i;
1669 if (it->entry_count >= 0) {
1670 struct tree *tree = lookup_tree(revs->repo, &it->oid);
1671 tree->object.flags |= flags;
1672 add_pending_object_with_path(revs, &tree->object, "",
1673 040000, path->buf);
1676 for (i = 0; i < it->subtree_nr; i++) {
1677 struct cache_tree_sub *sub = it->down[i];
1678 strbuf_addf(path, "%s%s", baselen ? "/" : "", sub->name);
1679 add_cache_tree(sub->cache_tree, revs, path, flags);
1680 strbuf_setlen(path, baselen);
1685 static void do_add_index_objects_to_pending(struct rev_info *revs,
1686 struct index_state *istate,
1687 unsigned int flags)
1689 int i;
1691 for (i = 0; i < istate->cache_nr; i++) {
1692 struct cache_entry *ce = istate->cache[i];
1693 struct blob *blob;
1695 if (S_ISGITLINK(ce->ce_mode))
1696 continue;
1698 blob = lookup_blob(revs->repo, &ce->oid);
1699 if (!blob)
1700 die("unable to add index blob to traversal");
1701 blob->object.flags |= flags;
1702 add_pending_object_with_path(revs, &blob->object, "",
1703 ce->ce_mode, ce->name);
1706 if (istate->cache_tree) {
1707 struct strbuf path = STRBUF_INIT;
1708 add_cache_tree(istate->cache_tree, revs, &path, flags);
1709 strbuf_release(&path);
1713 void add_index_objects_to_pending(struct rev_info *revs, unsigned int flags)
1715 struct worktree **worktrees, **p;
1717 repo_read_index(revs->repo);
1718 do_add_index_objects_to_pending(revs, revs->repo->index, flags);
1720 if (revs->single_worktree)
1721 return;
1723 worktrees = get_worktrees();
1724 for (p = worktrees; *p; p++) {
1725 struct worktree *wt = *p;
1726 struct index_state istate = { NULL };
1728 if (wt->is_current)
1729 continue; /* current index already taken care of */
1731 if (read_index_from(&istate,
1732 worktree_git_path(wt, "index"),
1733 get_worktree_git_dir(wt)) > 0)
1734 do_add_index_objects_to_pending(revs, &istate, flags);
1735 discard_index(&istate);
1737 free_worktrees(worktrees);
1740 struct add_alternate_refs_data {
1741 struct rev_info *revs;
1742 unsigned int flags;
1745 static void add_one_alternate_ref(const struct object_id *oid,
1746 void *vdata)
1748 const char *name = ".alternate";
1749 struct add_alternate_refs_data *data = vdata;
1750 struct object *obj;
1752 obj = get_reference(data->revs, name, oid, data->flags);
1753 add_rev_cmdline(data->revs, obj, name, REV_CMD_REV, data->flags);
1754 add_pending_object(data->revs, obj, name);
1757 static void add_alternate_refs_to_pending(struct rev_info *revs,
1758 unsigned int flags)
1760 struct add_alternate_refs_data data;
1761 data.revs = revs;
1762 data.flags = flags;
1763 for_each_alternate_ref(add_one_alternate_ref, &data);
1766 static int add_parents_only(struct rev_info *revs, const char *arg_, int flags,
1767 int exclude_parent)
1769 struct object_id oid;
1770 struct object *it;
1771 struct commit *commit;
1772 struct commit_list *parents;
1773 int parent_number;
1774 const char *arg = arg_;
1776 if (*arg == '^') {
1777 flags ^= UNINTERESTING | BOTTOM;
1778 arg++;
1780 if (get_oid_committish(arg, &oid))
1781 return 0;
1782 while (1) {
1783 it = get_reference(revs, arg, &oid, 0);
1784 if (!it && revs->ignore_missing)
1785 return 0;
1786 if (it->type != OBJ_TAG)
1787 break;
1788 if (!((struct tag*)it)->tagged)
1789 return 0;
1790 oidcpy(&oid, &((struct tag*)it)->tagged->oid);
1792 if (it->type != OBJ_COMMIT)
1793 return 0;
1794 commit = (struct commit *)it;
1795 if (exclude_parent &&
1796 exclude_parent > commit_list_count(commit->parents))
1797 return 0;
1798 for (parents = commit->parents, parent_number = 1;
1799 parents;
1800 parents = parents->next, parent_number++) {
1801 if (exclude_parent && parent_number != exclude_parent)
1802 continue;
1804 it = &parents->item->object;
1805 it->flags |= flags;
1806 add_rev_cmdline(revs, it, arg_, REV_CMD_PARENTS_ONLY, flags);
1807 add_pending_object(revs, it, arg);
1809 return 1;
1812 void repo_init_revisions(struct repository *r,
1813 struct rev_info *revs,
1814 const char *prefix)
1816 memset(revs, 0, sizeof(*revs));
1818 revs->repo = r;
1819 revs->abbrev = DEFAULT_ABBREV;
1820 revs->ignore_merges = 1;
1821 revs->simplify_history = 1;
1822 revs->pruning.repo = r;
1823 revs->pruning.flags.recursive = 1;
1824 revs->pruning.flags.quick = 1;
1825 revs->pruning.add_remove = file_add_remove;
1826 revs->pruning.change = file_change;
1827 revs->pruning.change_fn_data = revs;
1828 revs->sort_order = REV_SORT_IN_GRAPH_ORDER;
1829 revs->dense = 1;
1830 revs->prefix = prefix;
1831 revs->max_age = -1;
1832 revs->min_age = -1;
1833 revs->skip_count = -1;
1834 revs->max_count = -1;
1835 revs->max_parents = -1;
1836 revs->expand_tabs_in_log = -1;
1838 revs->commit_format = CMIT_FMT_DEFAULT;
1839 revs->expand_tabs_in_log_default = 8;
1841 init_grep_defaults(revs->repo);
1842 grep_init(&revs->grep_filter, revs->repo, prefix);
1843 revs->grep_filter.status_only = 1;
1845 repo_diff_setup(revs->repo, &revs->diffopt);
1846 if (prefix && !revs->diffopt.prefix) {
1847 revs->diffopt.prefix = prefix;
1848 revs->diffopt.prefix_length = strlen(prefix);
1851 init_display_notes(&revs->notes_opt);
1854 static void add_pending_commit_list(struct rev_info *revs,
1855 struct commit_list *commit_list,
1856 unsigned int flags)
1858 while (commit_list) {
1859 struct object *object = &commit_list->item->object;
1860 object->flags |= flags;
1861 add_pending_object(revs, object, oid_to_hex(&object->oid));
1862 commit_list = commit_list->next;
1866 static void prepare_show_merge(struct rev_info *revs)
1868 struct commit_list *bases;
1869 struct commit *head, *other;
1870 struct object_id oid;
1871 const char **prune = NULL;
1872 int i, prune_num = 1; /* counting terminating NULL */
1873 struct index_state *istate = revs->repo->index;
1875 if (get_oid("HEAD", &oid))
1876 die("--merge without HEAD?");
1877 head = lookup_commit_or_die(&oid, "HEAD");
1878 if (get_oid("MERGE_HEAD", &oid))
1879 die("--merge without MERGE_HEAD?");
1880 other = lookup_commit_or_die(&oid, "MERGE_HEAD");
1881 add_pending_object(revs, &head->object, "HEAD");
1882 add_pending_object(revs, &other->object, "MERGE_HEAD");
1883 bases = get_merge_bases(head, other);
1884 add_rev_cmdline_list(revs, bases, REV_CMD_MERGE_BASE, UNINTERESTING | BOTTOM);
1885 add_pending_commit_list(revs, bases, UNINTERESTING | BOTTOM);
1886 free_commit_list(bases);
1887 head->object.flags |= SYMMETRIC_LEFT;
1889 if (!istate->cache_nr)
1890 repo_read_index(revs->repo);
1891 for (i = 0; i < istate->cache_nr; i++) {
1892 const struct cache_entry *ce = istate->cache[i];
1893 if (!ce_stage(ce))
1894 continue;
1895 if (ce_path_match(istate, ce, &revs->prune_data, NULL)) {
1896 prune_num++;
1897 REALLOC_ARRAY(prune, prune_num);
1898 prune[prune_num-2] = ce->name;
1899 prune[prune_num-1] = NULL;
1901 while ((i+1 < istate->cache_nr) &&
1902 ce_same_name(ce, istate->cache[i+1]))
1903 i++;
1905 clear_pathspec(&revs->prune_data);
1906 parse_pathspec(&revs->prune_data, PATHSPEC_ALL_MAGIC & ~PATHSPEC_LITERAL,
1907 PATHSPEC_PREFER_FULL | PATHSPEC_LITERAL_PATH, "", prune);
1908 revs->limited = 1;
1911 static int dotdot_missing(const char *arg, char *dotdot,
1912 struct rev_info *revs, int symmetric)
1914 if (revs->ignore_missing)
1915 return 0;
1916 /* de-munge so we report the full argument */
1917 *dotdot = '.';
1918 die(symmetric
1919 ? "Invalid symmetric difference expression %s"
1920 : "Invalid revision range %s", arg);
1923 static int handle_dotdot_1(const char *arg, char *dotdot,
1924 struct rev_info *revs, int flags,
1925 int cant_be_filename,
1926 struct object_context *a_oc,
1927 struct object_context *b_oc)
1929 const char *a_name, *b_name;
1930 struct object_id a_oid, b_oid;
1931 struct object *a_obj, *b_obj;
1932 unsigned int a_flags, b_flags;
1933 int symmetric = 0;
1934 unsigned int flags_exclude = flags ^ (UNINTERESTING | BOTTOM);
1935 unsigned int oc_flags = GET_OID_COMMITTISH | GET_OID_RECORD_PATH;
1937 a_name = arg;
1938 if (!*a_name)
1939 a_name = "HEAD";
1941 b_name = dotdot + 2;
1942 if (*b_name == '.') {
1943 symmetric = 1;
1944 b_name++;
1946 if (!*b_name)
1947 b_name = "HEAD";
1949 if (get_oid_with_context(revs->repo, a_name, oc_flags, &a_oid, a_oc) ||
1950 get_oid_with_context(revs->repo, b_name, oc_flags, &b_oid, b_oc))
1951 return -1;
1953 if (!cant_be_filename) {
1954 *dotdot = '.';
1955 verify_non_filename(revs->prefix, arg);
1956 *dotdot = '\0';
1959 a_obj = parse_object(revs->repo, &a_oid);
1960 b_obj = parse_object(revs->repo, &b_oid);
1961 if (!a_obj || !b_obj)
1962 return dotdot_missing(arg, dotdot, revs, symmetric);
1964 if (!symmetric) {
1965 /* just A..B */
1966 b_flags = flags;
1967 a_flags = flags_exclude;
1968 } else {
1969 /* A...B -- find merge bases between the two */
1970 struct commit *a, *b;
1971 struct commit_list *exclude;
1973 a = lookup_commit_reference(revs->repo, &a_obj->oid);
1974 b = lookup_commit_reference(revs->repo, &b_obj->oid);
1975 if (!a || !b)
1976 return dotdot_missing(arg, dotdot, revs, symmetric);
1978 exclude = get_merge_bases(a, b);
1979 add_rev_cmdline_list(revs, exclude, REV_CMD_MERGE_BASE,
1980 flags_exclude);
1981 add_pending_commit_list(revs, exclude, flags_exclude);
1982 free_commit_list(exclude);
1984 b_flags = flags;
1985 a_flags = flags | SYMMETRIC_LEFT;
1988 a_obj->flags |= a_flags;
1989 b_obj->flags |= b_flags;
1990 add_rev_cmdline(revs, a_obj, a_name, REV_CMD_LEFT, a_flags);
1991 add_rev_cmdline(revs, b_obj, b_name, REV_CMD_RIGHT, b_flags);
1992 add_pending_object_with_path(revs, a_obj, a_name, a_oc->mode, a_oc->path);
1993 add_pending_object_with_path(revs, b_obj, b_name, b_oc->mode, b_oc->path);
1994 return 0;
1997 static int handle_dotdot(const char *arg,
1998 struct rev_info *revs, int flags,
1999 int cant_be_filename)
2001 struct object_context a_oc, b_oc;
2002 char *dotdot = strstr(arg, "..");
2003 int ret;
2005 if (!dotdot)
2006 return -1;
2008 memset(&a_oc, 0, sizeof(a_oc));
2009 memset(&b_oc, 0, sizeof(b_oc));
2011 *dotdot = '\0';
2012 ret = handle_dotdot_1(arg, dotdot, revs, flags, cant_be_filename,
2013 &a_oc, &b_oc);
2014 *dotdot = '.';
2016 free(a_oc.path);
2017 free(b_oc.path);
2019 return ret;
2022 int handle_revision_arg(const char *arg_, struct rev_info *revs, int flags, unsigned revarg_opt)
2024 struct object_context oc;
2025 char *mark;
2026 struct object *object;
2027 struct object_id oid;
2028 int local_flags;
2029 const char *arg = arg_;
2030 int cant_be_filename = revarg_opt & REVARG_CANNOT_BE_FILENAME;
2031 unsigned get_sha1_flags = GET_OID_RECORD_PATH;
2033 flags = flags & UNINTERESTING ? flags | BOTTOM : flags & ~BOTTOM;
2035 if (!cant_be_filename && !strcmp(arg, "..")) {
2037 * Just ".."? That is not a range but the
2038 * pathspec for the parent directory.
2040 return -1;
2043 if (!handle_dotdot(arg, revs, flags, revarg_opt))
2044 return 0;
2046 mark = strstr(arg, "^@");
2047 if (mark && !mark[2]) {
2048 *mark = 0;
2049 if (add_parents_only(revs, arg, flags, 0))
2050 return 0;
2051 *mark = '^';
2053 mark = strstr(arg, "^!");
2054 if (mark && !mark[2]) {
2055 *mark = 0;
2056 if (!add_parents_only(revs, arg, flags ^ (UNINTERESTING | BOTTOM), 0))
2057 *mark = '^';
2059 mark = strstr(arg, "^-");
2060 if (mark) {
2061 int exclude_parent = 1;
2063 if (mark[2]) {
2064 char *end;
2065 exclude_parent = strtoul(mark + 2, &end, 10);
2066 if (*end != '\0' || !exclude_parent)
2067 return -1;
2070 *mark = 0;
2071 if (!add_parents_only(revs, arg, flags ^ (UNINTERESTING | BOTTOM), exclude_parent))
2072 *mark = '^';
2075 local_flags = 0;
2076 if (*arg == '^') {
2077 local_flags = UNINTERESTING | BOTTOM;
2078 arg++;
2081 if (revarg_opt & REVARG_COMMITTISH)
2082 get_sha1_flags |= GET_OID_COMMITTISH;
2084 if (get_oid_with_context(revs->repo, arg, get_sha1_flags, &oid, &oc))
2085 return revs->ignore_missing ? 0 : -1;
2086 if (!cant_be_filename)
2087 verify_non_filename(revs->prefix, arg);
2088 object = get_reference(revs, arg, &oid, flags ^ local_flags);
2089 if (!object)
2090 return revs->ignore_missing ? 0 : -1;
2091 add_rev_cmdline(revs, object, arg_, REV_CMD_REV, flags ^ local_flags);
2092 add_pending_object_with_path(revs, object, arg, oc.mode, oc.path);
2093 free(oc.path);
2094 return 0;
2097 static void read_pathspec_from_stdin(struct strbuf *sb,
2098 struct argv_array *prune)
2100 while (strbuf_getline(sb, stdin) != EOF)
2101 argv_array_push(prune, sb->buf);
2104 static void read_revisions_from_stdin(struct rev_info *revs,
2105 struct argv_array *prune)
2107 struct strbuf sb;
2108 int seen_dashdash = 0;
2109 int save_warning;
2111 save_warning = warn_on_object_refname_ambiguity;
2112 warn_on_object_refname_ambiguity = 0;
2114 strbuf_init(&sb, 1000);
2115 while (strbuf_getline(&sb, stdin) != EOF) {
2116 int len = sb.len;
2117 if (!len)
2118 break;
2119 if (sb.buf[0] == '-') {
2120 if (len == 2 && sb.buf[1] == '-') {
2121 seen_dashdash = 1;
2122 break;
2124 die("options not supported in --stdin mode");
2126 if (handle_revision_arg(sb.buf, revs, 0,
2127 REVARG_CANNOT_BE_FILENAME))
2128 die("bad revision '%s'", sb.buf);
2130 if (seen_dashdash)
2131 read_pathspec_from_stdin(&sb, prune);
2133 strbuf_release(&sb);
2134 warn_on_object_refname_ambiguity = save_warning;
2137 static void add_grep(struct rev_info *revs, const char *ptn, enum grep_pat_token what)
2139 append_grep_pattern(&revs->grep_filter, ptn, "command line", 0, what);
2142 static void add_header_grep(struct rev_info *revs, enum grep_header_field field, const char *pattern)
2144 append_header_grep_pattern(&revs->grep_filter, field, pattern);
2147 static void add_message_grep(struct rev_info *revs, const char *pattern)
2149 add_grep(revs, pattern, GREP_PATTERN_BODY);
2152 static int handle_revision_opt(struct rev_info *revs, int argc, const char **argv,
2153 int *unkc, const char **unkv,
2154 const struct setup_revision_opt* opt)
2156 const char *arg = argv[0];
2157 const char *optarg;
2158 int argcount;
2159 const unsigned hexsz = the_hash_algo->hexsz;
2161 /* pseudo revision arguments */
2162 if (!strcmp(arg, "--all") || !strcmp(arg, "--branches") ||
2163 !strcmp(arg, "--tags") || !strcmp(arg, "--remotes") ||
2164 !strcmp(arg, "--reflog") || !strcmp(arg, "--not") ||
2165 !strcmp(arg, "--no-walk") || !strcmp(arg, "--do-walk") ||
2166 !strcmp(arg, "--bisect") || starts_with(arg, "--glob=") ||
2167 !strcmp(arg, "--indexed-objects") ||
2168 !strcmp(arg, "--alternate-refs") ||
2169 starts_with(arg, "--exclude=") ||
2170 starts_with(arg, "--branches=") || starts_with(arg, "--tags=") ||
2171 starts_with(arg, "--remotes=") || starts_with(arg, "--no-walk="))
2173 unkv[(*unkc)++] = arg;
2174 return 1;
2177 if ((argcount = parse_long_opt("max-count", argv, &optarg))) {
2178 revs->max_count = atoi(optarg);
2179 revs->no_walk = 0;
2180 return argcount;
2181 } else if ((argcount = parse_long_opt("skip", argv, &optarg))) {
2182 revs->skip_count = atoi(optarg);
2183 return argcount;
2184 } else if ((*arg == '-') && isdigit(arg[1])) {
2185 /* accept -<digit>, like traditional "head" */
2186 if (strtol_i(arg + 1, 10, &revs->max_count) < 0 ||
2187 revs->max_count < 0)
2188 die("'%s': not a non-negative integer", arg + 1);
2189 revs->no_walk = 0;
2190 } else if (!strcmp(arg, "-n")) {
2191 if (argc <= 1)
2192 return error("-n requires an argument");
2193 revs->max_count = atoi(argv[1]);
2194 revs->no_walk = 0;
2195 return 2;
2196 } else if (skip_prefix(arg, "-n", &optarg)) {
2197 revs->max_count = atoi(optarg);
2198 revs->no_walk = 0;
2199 } else if ((argcount = parse_long_opt("max-age", argv, &optarg))) {
2200 revs->max_age = atoi(optarg);
2201 return argcount;
2202 } else if ((argcount = parse_long_opt("since", argv, &optarg))) {
2203 revs->max_age = approxidate(optarg);
2204 return argcount;
2205 } else if ((argcount = parse_long_opt("after", argv, &optarg))) {
2206 revs->max_age = approxidate(optarg);
2207 return argcount;
2208 } else if ((argcount = parse_long_opt("min-age", argv, &optarg))) {
2209 revs->min_age = atoi(optarg);
2210 return argcount;
2211 } else if ((argcount = parse_long_opt("before", argv, &optarg))) {
2212 revs->min_age = approxidate(optarg);
2213 return argcount;
2214 } else if ((argcount = parse_long_opt("until", argv, &optarg))) {
2215 revs->min_age = approxidate(optarg);
2216 return argcount;
2217 } else if (!strcmp(arg, "--first-parent")) {
2218 revs->first_parent_only = 1;
2219 } else if (!strcmp(arg, "--ancestry-path")) {
2220 revs->ancestry_path = 1;
2221 revs->simplify_history = 0;
2222 revs->limited = 1;
2223 } else if (!strcmp(arg, "-g") || !strcmp(arg, "--walk-reflogs")) {
2224 init_reflog_walk(&revs->reflog_info);
2225 } else if (!strcmp(arg, "--default")) {
2226 if (argc <= 1)
2227 return error("bad --default argument");
2228 revs->def = argv[1];
2229 return 2;
2230 } else if (!strcmp(arg, "--merge")) {
2231 revs->show_merge = 1;
2232 } else if (!strcmp(arg, "--topo-order")) {
2233 revs->sort_order = REV_SORT_IN_GRAPH_ORDER;
2234 revs->topo_order = 1;
2235 } else if (!strcmp(arg, "--simplify-merges")) {
2236 revs->simplify_merges = 1;
2237 revs->topo_order = 1;
2238 revs->rewrite_parents = 1;
2239 revs->simplify_history = 0;
2240 revs->limited = 1;
2241 } else if (!strcmp(arg, "--simplify-by-decoration")) {
2242 revs->simplify_merges = 1;
2243 revs->topo_order = 1;
2244 revs->rewrite_parents = 1;
2245 revs->simplify_history = 0;
2246 revs->simplify_by_decoration = 1;
2247 revs->limited = 1;
2248 revs->prune = 1;
2249 } else if (!strcmp(arg, "--date-order")) {
2250 revs->sort_order = REV_SORT_BY_COMMIT_DATE;
2251 revs->topo_order = 1;
2252 } else if (!strcmp(arg, "--author-date-order")) {
2253 revs->sort_order = REV_SORT_BY_AUTHOR_DATE;
2254 revs->topo_order = 1;
2255 } else if (!strcmp(arg, "--early-output")) {
2256 revs->early_output = 100;
2257 revs->topo_order = 1;
2258 } else if (skip_prefix(arg, "--early-output=", &optarg)) {
2259 if (strtoul_ui(optarg, 10, &revs->early_output) < 0)
2260 die("'%s': not a non-negative integer", optarg);
2261 revs->topo_order = 1;
2262 } else if (!strcmp(arg, "--parents")) {
2263 revs->rewrite_parents = 1;
2264 revs->print_parents = 1;
2265 } else if (!strcmp(arg, "--dense")) {
2266 revs->dense = 1;
2267 } else if (!strcmp(arg, "--sparse")) {
2268 revs->dense = 0;
2269 } else if (!strcmp(arg, "--in-commit-order")) {
2270 revs->tree_blobs_in_commit_order = 1;
2271 } else if (!strcmp(arg, "--remove-empty")) {
2272 revs->remove_empty_trees = 1;
2273 } else if (!strcmp(arg, "--merges")) {
2274 revs->min_parents = 2;
2275 } else if (!strcmp(arg, "--no-merges")) {
2276 revs->max_parents = 1;
2277 } else if (skip_prefix(arg, "--min-parents=", &optarg)) {
2278 revs->min_parents = atoi(optarg);
2279 } else if (!strcmp(arg, "--no-min-parents")) {
2280 revs->min_parents = 0;
2281 } else if (skip_prefix(arg, "--max-parents=", &optarg)) {
2282 revs->max_parents = atoi(optarg);
2283 } else if (!strcmp(arg, "--no-max-parents")) {
2284 revs->max_parents = -1;
2285 } else if (!strcmp(arg, "--boundary")) {
2286 revs->boundary = 1;
2287 } else if (!strcmp(arg, "--left-right")) {
2288 revs->left_right = 1;
2289 } else if (!strcmp(arg, "--left-only")) {
2290 if (revs->right_only)
2291 die("--left-only is incompatible with --right-only"
2292 " or --cherry");
2293 revs->left_only = 1;
2294 } else if (!strcmp(arg, "--right-only")) {
2295 if (revs->left_only)
2296 die("--right-only is incompatible with --left-only");
2297 revs->right_only = 1;
2298 } else if (!strcmp(arg, "--cherry")) {
2299 if (revs->left_only)
2300 die("--cherry is incompatible with --left-only");
2301 revs->cherry_mark = 1;
2302 revs->right_only = 1;
2303 revs->max_parents = 1;
2304 revs->limited = 1;
2305 } else if (!strcmp(arg, "--count")) {
2306 revs->count = 1;
2307 } else if (!strcmp(arg, "--cherry-mark")) {
2308 if (revs->cherry_pick)
2309 die("--cherry-mark is incompatible with --cherry-pick");
2310 revs->cherry_mark = 1;
2311 revs->limited = 1; /* needs limit_list() */
2312 } else if (!strcmp(arg, "--cherry-pick")) {
2313 if (revs->cherry_mark)
2314 die("--cherry-pick is incompatible with --cherry-mark");
2315 revs->cherry_pick = 1;
2316 revs->limited = 1;
2317 } else if (!strcmp(arg, "--objects")) {
2318 revs->tag_objects = 1;
2319 revs->tree_objects = 1;
2320 revs->blob_objects = 1;
2321 } else if (!strcmp(arg, "--objects-edge")) {
2322 revs->tag_objects = 1;
2323 revs->tree_objects = 1;
2324 revs->blob_objects = 1;
2325 revs->edge_hint = 1;
2326 } else if (!strcmp(arg, "--objects-edge-aggressive")) {
2327 revs->tag_objects = 1;
2328 revs->tree_objects = 1;
2329 revs->blob_objects = 1;
2330 revs->edge_hint = 1;
2331 revs->edge_hint_aggressive = 1;
2332 } else if (!strcmp(arg, "--verify-objects")) {
2333 revs->tag_objects = 1;
2334 revs->tree_objects = 1;
2335 revs->blob_objects = 1;
2336 revs->verify_objects = 1;
2337 } else if (!strcmp(arg, "--unpacked")) {
2338 revs->unpacked = 1;
2339 } else if (starts_with(arg, "--unpacked=")) {
2340 die("--unpacked=<packfile> no longer supported.");
2341 } else if (!strcmp(arg, "-r")) {
2342 revs->diff = 1;
2343 revs->diffopt.flags.recursive = 1;
2344 } else if (!strcmp(arg, "-t")) {
2345 revs->diff = 1;
2346 revs->diffopt.flags.recursive = 1;
2347 revs->diffopt.flags.tree_in_recursive = 1;
2348 } else if (!strcmp(arg, "-m")) {
2349 revs->ignore_merges = 0;
2350 } else if (!strcmp(arg, "-c")) {
2351 revs->diff = 1;
2352 revs->dense_combined_merges = 0;
2353 revs->combine_merges = 1;
2354 } else if (!strcmp(arg, "--combined-all-paths")) {
2355 revs->diff = 1;
2356 revs->combined_all_paths = 1;
2357 } else if (!strcmp(arg, "--cc")) {
2358 revs->diff = 1;
2359 revs->dense_combined_merges = 1;
2360 revs->combine_merges = 1;
2361 } else if (!strcmp(arg, "-v")) {
2362 revs->verbose_header = 1;
2363 } else if (!strcmp(arg, "--pretty")) {
2364 revs->verbose_header = 1;
2365 revs->pretty_given = 1;
2366 get_commit_format(NULL, revs);
2367 } else if (skip_prefix(arg, "--pretty=", &optarg) ||
2368 skip_prefix(arg, "--format=", &optarg)) {
2370 * Detached form ("--pretty X" as opposed to "--pretty=X")
2371 * not allowed, since the argument is optional.
2373 revs->verbose_header = 1;
2374 revs->pretty_given = 1;
2375 get_commit_format(optarg, revs);
2376 } else if (!strcmp(arg, "--expand-tabs")) {
2377 revs->expand_tabs_in_log = 8;
2378 } else if (!strcmp(arg, "--no-expand-tabs")) {
2379 revs->expand_tabs_in_log = 0;
2380 } else if (skip_prefix(arg, "--expand-tabs=", &arg)) {
2381 int val;
2382 if (strtol_i(arg, 10, &val) < 0 || val < 0)
2383 die("'%s': not a non-negative integer", arg);
2384 revs->expand_tabs_in_log = val;
2385 } else if (!strcmp(arg, "--show-notes") || !strcmp(arg, "--notes")) {
2386 enable_default_display_notes(&revs->notes_opt, &revs->show_notes);
2387 revs->show_notes_given = 1;
2388 } else if (!strcmp(arg, "--show-signature")) {
2389 revs->show_signature = 1;
2390 } else if (!strcmp(arg, "--no-show-signature")) {
2391 revs->show_signature = 0;
2392 } else if (!strcmp(arg, "--show-linear-break")) {
2393 revs->break_bar = " ..........";
2394 revs->track_linear = 1;
2395 revs->track_first_time = 1;
2396 } else if (skip_prefix(arg, "--show-linear-break=", &optarg)) {
2397 revs->break_bar = xstrdup(optarg);
2398 revs->track_linear = 1;
2399 revs->track_first_time = 1;
2400 } else if (skip_prefix(arg, "--show-notes=", &optarg) ||
2401 skip_prefix(arg, "--notes=", &optarg)) {
2402 if (starts_with(arg, "--show-notes=") &&
2403 revs->notes_opt.use_default_notes < 0)
2404 revs->notes_opt.use_default_notes = 1;
2405 enable_ref_display_notes(&revs->notes_opt, &revs->show_notes, optarg);
2406 revs->show_notes_given = 1;
2407 } else if (!strcmp(arg, "--no-notes")) {
2408 disable_display_notes(&revs->notes_opt, &revs->show_notes);
2409 revs->show_notes_given = 1;
2410 } else if (!strcmp(arg, "--standard-notes")) {
2411 revs->show_notes_given = 1;
2412 revs->notes_opt.use_default_notes = 1;
2413 } else if (!strcmp(arg, "--no-standard-notes")) {
2414 revs->notes_opt.use_default_notes = 0;
2415 } else if (!strcmp(arg, "--oneline")) {
2416 revs->verbose_header = 1;
2417 get_commit_format("oneline", revs);
2418 revs->pretty_given = 1;
2419 revs->abbrev_commit = 1;
2420 } else if (!strcmp(arg, "--graph")) {
2421 revs->topo_order = 1;
2422 revs->rewrite_parents = 1;
2423 revs->graph = graph_init(revs);
2424 } else if (!strcmp(arg, "--encode-email-headers")) {
2425 revs->encode_email_headers = 1;
2426 } else if (!strcmp(arg, "--no-encode-email-headers")) {
2427 revs->encode_email_headers = 0;
2428 } else if (!strcmp(arg, "--root")) {
2429 revs->show_root_diff = 1;
2430 } else if (!strcmp(arg, "--no-commit-id")) {
2431 revs->no_commit_id = 1;
2432 } else if (!strcmp(arg, "--always")) {
2433 revs->always_show_header = 1;
2434 } else if (!strcmp(arg, "--no-abbrev")) {
2435 revs->abbrev = 0;
2436 } else if (!strcmp(arg, "--abbrev")) {
2437 revs->abbrev = DEFAULT_ABBREV;
2438 } else if (skip_prefix(arg, "--abbrev=", &optarg)) {
2439 revs->abbrev = strtoul(optarg, NULL, 10);
2440 if (revs->abbrev < MINIMUM_ABBREV)
2441 revs->abbrev = MINIMUM_ABBREV;
2442 else if (revs->abbrev > hexsz)
2443 revs->abbrev = hexsz;
2444 } else if (!strcmp(arg, "--abbrev-commit")) {
2445 revs->abbrev_commit = 1;
2446 revs->abbrev_commit_given = 1;
2447 } else if (!strcmp(arg, "--no-abbrev-commit")) {
2448 revs->abbrev_commit = 0;
2449 } else if (!strcmp(arg, "--full-diff")) {
2450 revs->diff = 1;
2451 revs->full_diff = 1;
2452 } else if (!strcmp(arg, "--show-pulls")) {
2453 revs->show_pulls = 1;
2454 } else if (!strcmp(arg, "--full-history")) {
2455 revs->simplify_history = 0;
2456 } else if (!strcmp(arg, "--relative-date")) {
2457 revs->date_mode.type = DATE_RELATIVE;
2458 revs->date_mode_explicit = 1;
2459 } else if ((argcount = parse_long_opt("date", argv, &optarg))) {
2460 parse_date_format(optarg, &revs->date_mode);
2461 revs->date_mode_explicit = 1;
2462 return argcount;
2463 } else if (!strcmp(arg, "--log-size")) {
2464 revs->show_log_size = 1;
2467 * Grepping the commit log
2469 else if ((argcount = parse_long_opt("author", argv, &optarg))) {
2470 add_header_grep(revs, GREP_HEADER_AUTHOR, optarg);
2471 return argcount;
2472 } else if ((argcount = parse_long_opt("committer", argv, &optarg))) {
2473 add_header_grep(revs, GREP_HEADER_COMMITTER, optarg);
2474 return argcount;
2475 } else if ((argcount = parse_long_opt("grep-reflog", argv, &optarg))) {
2476 add_header_grep(revs, GREP_HEADER_REFLOG, optarg);
2477 return argcount;
2478 } else if ((argcount = parse_long_opt("grep", argv, &optarg))) {
2479 add_message_grep(revs, optarg);
2480 return argcount;
2481 } else if (!strcmp(arg, "--grep-debug")) {
2482 revs->grep_filter.debug = 1;
2483 } else if (!strcmp(arg, "--basic-regexp")) {
2484 revs->grep_filter.pattern_type_option = GREP_PATTERN_TYPE_BRE;
2485 } else if (!strcmp(arg, "--extended-regexp") || !strcmp(arg, "-E")) {
2486 revs->grep_filter.pattern_type_option = GREP_PATTERN_TYPE_ERE;
2487 } else if (!strcmp(arg, "--regexp-ignore-case") || !strcmp(arg, "-i")) {
2488 revs->grep_filter.ignore_case = 1;
2489 revs->diffopt.pickaxe_opts |= DIFF_PICKAXE_IGNORE_CASE;
2490 } else if (!strcmp(arg, "--fixed-strings") || !strcmp(arg, "-F")) {
2491 revs->grep_filter.pattern_type_option = GREP_PATTERN_TYPE_FIXED;
2492 } else if (!strcmp(arg, "--perl-regexp") || !strcmp(arg, "-P")) {
2493 revs->grep_filter.pattern_type_option = GREP_PATTERN_TYPE_PCRE;
2494 } else if (!strcmp(arg, "--all-match")) {
2495 revs->grep_filter.all_match = 1;
2496 } else if (!strcmp(arg, "--invert-grep")) {
2497 revs->invert_grep = 1;
2498 } else if ((argcount = parse_long_opt("encoding", argv, &optarg))) {
2499 if (strcmp(optarg, "none"))
2500 git_log_output_encoding = xstrdup(optarg);
2501 else
2502 git_log_output_encoding = "";
2503 return argcount;
2504 } else if (!strcmp(arg, "--reverse")) {
2505 revs->reverse ^= 1;
2506 } else if (!strcmp(arg, "--children")) {
2507 revs->children.name = "children";
2508 revs->limited = 1;
2509 } else if (!strcmp(arg, "--ignore-missing")) {
2510 revs->ignore_missing = 1;
2511 } else if (opt && opt->allow_exclude_promisor_objects &&
2512 !strcmp(arg, "--exclude-promisor-objects")) {
2513 if (fetch_if_missing)
2514 BUG("exclude_promisor_objects can only be used when fetch_if_missing is 0");
2515 revs->exclude_promisor_objects = 1;
2516 } else {
2517 int opts = diff_opt_parse(&revs->diffopt, argv, argc, revs->prefix);
2518 if (!opts)
2519 unkv[(*unkc)++] = arg;
2520 return opts;
2522 if (revs->graph && revs->track_linear)
2523 die("--show-linear-break and --graph are incompatible");
2525 return 1;
2528 void parse_revision_opt(struct rev_info *revs, struct parse_opt_ctx_t *ctx,
2529 const struct option *options,
2530 const char * const usagestr[])
2532 int n = handle_revision_opt(revs, ctx->argc, ctx->argv,
2533 &ctx->cpidx, ctx->out, NULL);
2534 if (n <= 0) {
2535 error("unknown option `%s'", ctx->argv[0]);
2536 usage_with_options(usagestr, options);
2538 ctx->argv += n;
2539 ctx->argc -= n;
2542 static int for_each_bisect_ref(struct ref_store *refs, each_ref_fn fn,
2543 void *cb_data, const char *term)
2545 struct strbuf bisect_refs = STRBUF_INIT;
2546 int status;
2547 strbuf_addf(&bisect_refs, "refs/bisect/%s", term);
2548 status = refs_for_each_fullref_in(refs, bisect_refs.buf, fn, cb_data, 0);
2549 strbuf_release(&bisect_refs);
2550 return status;
2553 static int for_each_bad_bisect_ref(struct ref_store *refs, each_ref_fn fn, void *cb_data)
2555 return for_each_bisect_ref(refs, fn, cb_data, term_bad);
2558 static int for_each_good_bisect_ref(struct ref_store *refs, each_ref_fn fn, void *cb_data)
2560 return for_each_bisect_ref(refs, fn, cb_data, term_good);
2563 static int handle_revision_pseudo_opt(const char *submodule,
2564 struct rev_info *revs,
2565 int argc, const char **argv, int *flags)
2567 const char *arg = argv[0];
2568 const char *optarg;
2569 struct ref_store *refs;
2570 int argcount;
2572 if (submodule) {
2574 * We need some something like get_submodule_worktrees()
2575 * before we can go through all worktrees of a submodule,
2576 * .e.g with adding all HEADs from --all, which is not
2577 * supported right now, so stick to single worktree.
2579 if (!revs->single_worktree)
2580 BUG("--single-worktree cannot be used together with submodule");
2581 refs = get_submodule_ref_store(submodule);
2582 } else
2583 refs = get_main_ref_store(revs->repo);
2586 * NOTE!
2588 * Commands like "git shortlog" will not accept the options below
2589 * unless parse_revision_opt queues them (as opposed to erroring
2590 * out).
2592 * When implementing your new pseudo-option, remember to
2593 * register it in the list at the top of handle_revision_opt.
2595 if (!strcmp(arg, "--all")) {
2596 handle_refs(refs, revs, *flags, refs_for_each_ref);
2597 handle_refs(refs, revs, *flags, refs_head_ref);
2598 if (!revs->single_worktree) {
2599 struct all_refs_cb cb;
2601 init_all_refs_cb(&cb, revs, *flags);
2602 other_head_refs(handle_one_ref, &cb);
2604 clear_ref_exclusion(&revs->ref_excludes);
2605 } else if (!strcmp(arg, "--branches")) {
2606 handle_refs(refs, revs, *flags, refs_for_each_branch_ref);
2607 clear_ref_exclusion(&revs->ref_excludes);
2608 } else if (!strcmp(arg, "--bisect")) {
2609 read_bisect_terms(&term_bad, &term_good);
2610 handle_refs(refs, revs, *flags, for_each_bad_bisect_ref);
2611 handle_refs(refs, revs, *flags ^ (UNINTERESTING | BOTTOM),
2612 for_each_good_bisect_ref);
2613 revs->bisect = 1;
2614 } else if (!strcmp(arg, "--tags")) {
2615 handle_refs(refs, revs, *flags, refs_for_each_tag_ref);
2616 clear_ref_exclusion(&revs->ref_excludes);
2617 } else if (!strcmp(arg, "--remotes")) {
2618 handle_refs(refs, revs, *flags, refs_for_each_remote_ref);
2619 clear_ref_exclusion(&revs->ref_excludes);
2620 } else if ((argcount = parse_long_opt("glob", argv, &optarg))) {
2621 struct all_refs_cb cb;
2622 init_all_refs_cb(&cb, revs, *flags);
2623 for_each_glob_ref(handle_one_ref, optarg, &cb);
2624 clear_ref_exclusion(&revs->ref_excludes);
2625 return argcount;
2626 } else if ((argcount = parse_long_opt("exclude", argv, &optarg))) {
2627 add_ref_exclusion(&revs->ref_excludes, optarg);
2628 return argcount;
2629 } else if (skip_prefix(arg, "--branches=", &optarg)) {
2630 struct all_refs_cb cb;
2631 init_all_refs_cb(&cb, revs, *flags);
2632 for_each_glob_ref_in(handle_one_ref, optarg, "refs/heads/", &cb);
2633 clear_ref_exclusion(&revs->ref_excludes);
2634 } else if (skip_prefix(arg, "--tags=", &optarg)) {
2635 struct all_refs_cb cb;
2636 init_all_refs_cb(&cb, revs, *flags);
2637 for_each_glob_ref_in(handle_one_ref, optarg, "refs/tags/", &cb);
2638 clear_ref_exclusion(&revs->ref_excludes);
2639 } else if (skip_prefix(arg, "--remotes=", &optarg)) {
2640 struct all_refs_cb cb;
2641 init_all_refs_cb(&cb, revs, *flags);
2642 for_each_glob_ref_in(handle_one_ref, optarg, "refs/remotes/", &cb);
2643 clear_ref_exclusion(&revs->ref_excludes);
2644 } else if (!strcmp(arg, "--reflog")) {
2645 add_reflogs_to_pending(revs, *flags);
2646 } else if (!strcmp(arg, "--indexed-objects")) {
2647 add_index_objects_to_pending(revs, *flags);
2648 } else if (!strcmp(arg, "--alternate-refs")) {
2649 add_alternate_refs_to_pending(revs, *flags);
2650 } else if (!strcmp(arg, "--not")) {
2651 *flags ^= UNINTERESTING | BOTTOM;
2652 } else if (!strcmp(arg, "--no-walk")) {
2653 revs->no_walk = REVISION_WALK_NO_WALK_SORTED;
2654 } else if (skip_prefix(arg, "--no-walk=", &optarg)) {
2656 * Detached form ("--no-walk X" as opposed to "--no-walk=X")
2657 * not allowed, since the argument is optional.
2659 if (!strcmp(optarg, "sorted"))
2660 revs->no_walk = REVISION_WALK_NO_WALK_SORTED;
2661 else if (!strcmp(optarg, "unsorted"))
2662 revs->no_walk = REVISION_WALK_NO_WALK_UNSORTED;
2663 else
2664 return error("invalid argument to --no-walk");
2665 } else if (!strcmp(arg, "--do-walk")) {
2666 revs->no_walk = 0;
2667 } else if (!strcmp(arg, "--single-worktree")) {
2668 revs->single_worktree = 1;
2669 } else {
2670 return 0;
2673 return 1;
2676 static void NORETURN diagnose_missing_default(const char *def)
2678 int flags;
2679 const char *refname;
2681 refname = resolve_ref_unsafe(def, 0, NULL, &flags);
2682 if (!refname || !(flags & REF_ISSYMREF) || (flags & REF_ISBROKEN))
2683 die(_("your current branch appears to be broken"));
2685 skip_prefix(refname, "refs/heads/", &refname);
2686 die(_("your current branch '%s' does not have any commits yet"),
2687 refname);
2691 * Parse revision information, filling in the "rev_info" structure,
2692 * and removing the used arguments from the argument list.
2694 * Returns the number of arguments left that weren't recognized
2695 * (which are also moved to the head of the argument list)
2697 int setup_revisions(int argc, const char **argv, struct rev_info *revs, struct setup_revision_opt *opt)
2699 int i, flags, left, seen_dashdash, got_rev_arg = 0, revarg_opt;
2700 struct argv_array prune_data = ARGV_ARRAY_INIT;
2701 const char *submodule = NULL;
2702 int seen_end_of_options = 0;
2704 if (opt)
2705 submodule = opt->submodule;
2707 /* First, search for "--" */
2708 if (opt && opt->assume_dashdash) {
2709 seen_dashdash = 1;
2710 } else {
2711 seen_dashdash = 0;
2712 for (i = 1; i < argc; i++) {
2713 const char *arg = argv[i];
2714 if (strcmp(arg, "--"))
2715 continue;
2716 argv[i] = NULL;
2717 argc = i;
2718 if (argv[i + 1])
2719 argv_array_pushv(&prune_data, argv + i + 1);
2720 seen_dashdash = 1;
2721 break;
2725 /* Second, deal with arguments and options */
2726 flags = 0;
2727 revarg_opt = opt ? opt->revarg_opt : 0;
2728 if (seen_dashdash)
2729 revarg_opt |= REVARG_CANNOT_BE_FILENAME;
2730 for (left = i = 1; i < argc; i++) {
2731 const char *arg = argv[i];
2732 if (!seen_end_of_options && *arg == '-') {
2733 int opts;
2735 opts = handle_revision_pseudo_opt(submodule,
2736 revs, argc - i, argv + i,
2737 &flags);
2738 if (opts > 0) {
2739 i += opts - 1;
2740 continue;
2743 if (!strcmp(arg, "--stdin")) {
2744 if (revs->disable_stdin) {
2745 argv[left++] = arg;
2746 continue;
2748 if (revs->read_from_stdin++)
2749 die("--stdin given twice?");
2750 read_revisions_from_stdin(revs, &prune_data);
2751 continue;
2754 if (!strcmp(arg, "--end-of-options")) {
2755 seen_end_of_options = 1;
2756 continue;
2759 opts = handle_revision_opt(revs, argc - i, argv + i,
2760 &left, argv, opt);
2761 if (opts > 0) {
2762 i += opts - 1;
2763 continue;
2765 if (opts < 0)
2766 exit(128);
2767 continue;
2771 if (handle_revision_arg(arg, revs, flags, revarg_opt)) {
2772 int j;
2773 if (seen_dashdash || *arg == '^')
2774 die("bad revision '%s'", arg);
2776 /* If we didn't have a "--":
2777 * (1) all filenames must exist;
2778 * (2) all rev-args must not be interpretable
2779 * as a valid filename.
2780 * but the latter we have checked in the main loop.
2782 for (j = i; j < argc; j++)
2783 verify_filename(revs->prefix, argv[j], j == i);
2785 argv_array_pushv(&prune_data, argv + i);
2786 break;
2788 else
2789 got_rev_arg = 1;
2792 if (prune_data.argc) {
2794 * If we need to introduce the magic "a lone ':' means no
2795 * pathspec whatsoever", here is the place to do so.
2797 * if (prune_data.nr == 1 && !strcmp(prune_data[0], ":")) {
2798 * prune_data.nr = 0;
2799 * prune_data.alloc = 0;
2800 * free(prune_data.path);
2801 * prune_data.path = NULL;
2802 * } else {
2803 * terminate prune_data.alloc with NULL and
2804 * call init_pathspec() to set revs->prune_data here.
2807 parse_pathspec(&revs->prune_data, 0, 0,
2808 revs->prefix, prune_data.argv);
2810 argv_array_clear(&prune_data);
2812 if (revs->def == NULL)
2813 revs->def = opt ? opt->def : NULL;
2814 if (opt && opt->tweak)
2815 opt->tweak(revs, opt);
2816 if (revs->show_merge)
2817 prepare_show_merge(revs);
2818 if (revs->def && !revs->pending.nr && !revs->rev_input_given && !got_rev_arg) {
2819 struct object_id oid;
2820 struct object *object;
2821 struct object_context oc;
2822 if (get_oid_with_context(revs->repo, revs->def, 0, &oid, &oc))
2823 diagnose_missing_default(revs->def);
2824 object = get_reference(revs, revs->def, &oid, 0);
2825 add_pending_object_with_mode(revs, object, revs->def, oc.mode);
2828 /* Did the user ask for any diff output? Run the diff! */
2829 if (revs->diffopt.output_format & ~DIFF_FORMAT_NO_OUTPUT)
2830 revs->diff = 1;
2832 /* Pickaxe, diff-filter and rename following need diffs */
2833 if ((revs->diffopt.pickaxe_opts & DIFF_PICKAXE_KINDS_MASK) ||
2834 revs->diffopt.filter ||
2835 revs->diffopt.flags.follow_renames)
2836 revs->diff = 1;
2838 if (revs->diffopt.objfind)
2839 revs->simplify_history = 0;
2841 if (revs->line_level_traverse) {
2842 if (want_ancestry(revs))
2843 revs->limited = 1;
2844 revs->topo_order = 1;
2847 if (revs->topo_order && !generation_numbers_enabled(the_repository))
2848 revs->limited = 1;
2850 if (revs->prune_data.nr) {
2851 copy_pathspec(&revs->pruning.pathspec, &revs->prune_data);
2852 /* Can't prune commits with rename following: the paths change.. */
2853 if (!revs->diffopt.flags.follow_renames)
2854 revs->prune = 1;
2855 if (!revs->full_diff)
2856 copy_pathspec(&revs->diffopt.pathspec,
2857 &revs->prune_data);
2859 if (revs->combine_merges)
2860 revs->ignore_merges = 0;
2861 if (revs->combined_all_paths && !revs->combine_merges)
2862 die("--combined-all-paths makes no sense without -c or --cc");
2864 revs->diffopt.abbrev = revs->abbrev;
2866 diff_setup_done(&revs->diffopt);
2868 grep_commit_pattern_type(GREP_PATTERN_TYPE_UNSPECIFIED,
2869 &revs->grep_filter);
2870 if (!is_encoding_utf8(get_log_output_encoding()))
2871 revs->grep_filter.ignore_locale = 1;
2872 compile_grep_patterns(&revs->grep_filter);
2874 if (revs->reverse && revs->reflog_info)
2875 die("cannot combine --reverse with --walk-reflogs");
2876 if (revs->reflog_info && revs->limited)
2877 die("cannot combine --walk-reflogs with history-limiting options");
2878 if (revs->rewrite_parents && revs->children.name)
2879 die("cannot combine --parents and --children");
2882 * Limitations on the graph functionality
2884 if (revs->reverse && revs->graph)
2885 die("cannot combine --reverse with --graph");
2887 if (revs->reflog_info && revs->graph)
2888 die("cannot combine --walk-reflogs with --graph");
2889 if (revs->no_walk && revs->graph)
2890 die("cannot combine --no-walk with --graph");
2891 if (!revs->reflog_info && revs->grep_filter.use_reflog_filter)
2892 die("cannot use --grep-reflog without --walk-reflogs");
2894 if (revs->first_parent_only && revs->bisect)
2895 die(_("--first-parent is incompatible with --bisect"));
2897 if (revs->line_level_traverse &&
2898 (revs->diffopt.output_format & ~(DIFF_FORMAT_PATCH | DIFF_FORMAT_NO_OUTPUT)))
2899 die(_("-L does not yet support diff formats besides -p and -s"));
2901 if (revs->expand_tabs_in_log < 0)
2902 revs->expand_tabs_in_log = revs->expand_tabs_in_log_default;
2904 return left;
2907 static void add_child(struct rev_info *revs, struct commit *parent, struct commit *child)
2909 struct commit_list *l = xcalloc(1, sizeof(*l));
2911 l->item = child;
2912 l->next = add_decoration(&revs->children, &parent->object, l);
2915 static int remove_duplicate_parents(struct rev_info *revs, struct commit *commit)
2917 struct treesame_state *ts = lookup_decoration(&revs->treesame, &commit->object);
2918 struct commit_list **pp, *p;
2919 int surviving_parents;
2921 /* Examine existing parents while marking ones we have seen... */
2922 pp = &commit->parents;
2923 surviving_parents = 0;
2924 while ((p = *pp) != NULL) {
2925 struct commit *parent = p->item;
2926 if (parent->object.flags & TMP_MARK) {
2927 *pp = p->next;
2928 if (ts)
2929 compact_treesame(revs, commit, surviving_parents);
2930 continue;
2932 parent->object.flags |= TMP_MARK;
2933 surviving_parents++;
2934 pp = &p->next;
2936 /* clear the temporary mark */
2937 for (p = commit->parents; p; p = p->next) {
2938 p->item->object.flags &= ~TMP_MARK;
2940 /* no update_treesame() - removing duplicates can't affect TREESAME */
2941 return surviving_parents;
2944 struct merge_simplify_state {
2945 struct commit *simplified;
2948 static struct merge_simplify_state *locate_simplify_state(struct rev_info *revs, struct commit *commit)
2950 struct merge_simplify_state *st;
2952 st = lookup_decoration(&revs->merge_simplification, &commit->object);
2953 if (!st) {
2954 st = xcalloc(1, sizeof(*st));
2955 add_decoration(&revs->merge_simplification, &commit->object, st);
2957 return st;
2960 static int mark_redundant_parents(struct commit *commit)
2962 struct commit_list *h = reduce_heads(commit->parents);
2963 int i = 0, marked = 0;
2964 struct commit_list *po, *pn;
2966 /* Want these for sanity-checking only */
2967 int orig_cnt = commit_list_count(commit->parents);
2968 int cnt = commit_list_count(h);
2971 * Not ready to remove items yet, just mark them for now, based
2972 * on the output of reduce_heads(). reduce_heads outputs the reduced
2973 * set in its original order, so this isn't too hard.
2975 po = commit->parents;
2976 pn = h;
2977 while (po) {
2978 if (pn && po->item == pn->item) {
2979 pn = pn->next;
2980 i++;
2981 } else {
2982 po->item->object.flags |= TMP_MARK;
2983 marked++;
2985 po=po->next;
2988 if (i != cnt || cnt+marked != orig_cnt)
2989 die("mark_redundant_parents %d %d %d %d", orig_cnt, cnt, i, marked);
2991 free_commit_list(h);
2993 return marked;
2996 static int mark_treesame_root_parents(struct commit *commit)
2998 struct commit_list *p;
2999 int marked = 0;
3001 for (p = commit->parents; p; p = p->next) {
3002 struct commit *parent = p->item;
3003 if (!parent->parents && (parent->object.flags & TREESAME)) {
3004 parent->object.flags |= TMP_MARK;
3005 marked++;
3009 return marked;
3013 * Awkward naming - this means one parent we are TREESAME to.
3014 * cf mark_treesame_root_parents: root parents that are TREESAME (to an
3015 * empty tree). Better name suggestions?
3017 static int leave_one_treesame_to_parent(struct rev_info *revs, struct commit *commit)
3019 struct treesame_state *ts = lookup_decoration(&revs->treesame, &commit->object);
3020 struct commit *unmarked = NULL, *marked = NULL;
3021 struct commit_list *p;
3022 unsigned n;
3024 for (p = commit->parents, n = 0; p; p = p->next, n++) {
3025 if (ts->treesame[n]) {
3026 if (p->item->object.flags & TMP_MARK) {
3027 if (!marked)
3028 marked = p->item;
3029 } else {
3030 if (!unmarked) {
3031 unmarked = p->item;
3032 break;
3039 * If we are TREESAME to a marked-for-deletion parent, but not to any
3040 * unmarked parents, unmark the first TREESAME parent. This is the
3041 * parent that the default simplify_history==1 scan would have followed,
3042 * and it doesn't make sense to omit that path when asking for a
3043 * simplified full history. Retaining it improves the chances of
3044 * understanding odd missed merges that took an old version of a file.
3046 * Example:
3048 * I--------*X A modified the file, but mainline merge X used
3049 * \ / "-s ours", so took the version from I. X is
3050 * `-*A--' TREESAME to I and !TREESAME to A.
3052 * Default log from X would produce "I". Without this check,
3053 * --full-history --simplify-merges would produce "I-A-X", showing
3054 * the merge commit X and that it changed A, but not making clear that
3055 * it had just taken the I version. With this check, the topology above
3056 * is retained.
3058 * Note that it is possible that the simplification chooses a different
3059 * TREESAME parent from the default, in which case this test doesn't
3060 * activate, and we _do_ drop the default parent. Example:
3062 * I------X A modified the file, but it was reverted in B,
3063 * \ / meaning mainline merge X is TREESAME to both
3064 * *A-*B parents.
3066 * Default log would produce "I" by following the first parent;
3067 * --full-history --simplify-merges will produce "I-A-B". But this is a
3068 * reasonable result - it presents a logical full history leading from
3069 * I to X, and X is not an important merge.
3071 if (!unmarked && marked) {
3072 marked->object.flags &= ~TMP_MARK;
3073 return 1;
3076 return 0;
3079 static int remove_marked_parents(struct rev_info *revs, struct commit *commit)
3081 struct commit_list **pp, *p;
3082 int nth_parent, removed = 0;
3084 pp = &commit->parents;
3085 nth_parent = 0;
3086 while ((p = *pp) != NULL) {
3087 struct commit *parent = p->item;
3088 if (parent->object.flags & TMP_MARK) {
3089 parent->object.flags &= ~TMP_MARK;
3090 *pp = p->next;
3091 free(p);
3092 removed++;
3093 compact_treesame(revs, commit, nth_parent);
3094 continue;
3096 pp = &p->next;
3097 nth_parent++;
3100 /* Removing parents can only increase TREESAMEness */
3101 if (removed && !(commit->object.flags & TREESAME))
3102 update_treesame(revs, commit);
3104 return nth_parent;
3107 static struct commit_list **simplify_one(struct rev_info *revs, struct commit *commit, struct commit_list **tail)
3109 struct commit_list *p;
3110 struct commit *parent;
3111 struct merge_simplify_state *st, *pst;
3112 int cnt;
3114 st = locate_simplify_state(revs, commit);
3117 * Have we handled this one?
3119 if (st->simplified)
3120 return tail;
3123 * An UNINTERESTING commit simplifies to itself, so does a
3124 * root commit. We do not rewrite parents of such commit
3125 * anyway.
3127 if ((commit->object.flags & UNINTERESTING) || !commit->parents) {
3128 st->simplified = commit;
3129 return tail;
3133 * Do we know what commit all of our parents that matter
3134 * should be rewritten to? Otherwise we are not ready to
3135 * rewrite this one yet.
3137 for (cnt = 0, p = commit->parents; p; p = p->next) {
3138 pst = locate_simplify_state(revs, p->item);
3139 if (!pst->simplified) {
3140 tail = &commit_list_insert(p->item, tail)->next;
3141 cnt++;
3143 if (revs->first_parent_only)
3144 break;
3146 if (cnt) {
3147 tail = &commit_list_insert(commit, tail)->next;
3148 return tail;
3152 * Rewrite our list of parents. Note that this cannot
3153 * affect our TREESAME flags in any way - a commit is
3154 * always TREESAME to its simplification.
3156 for (p = commit->parents; p; p = p->next) {
3157 pst = locate_simplify_state(revs, p->item);
3158 p->item = pst->simplified;
3159 if (revs->first_parent_only)
3160 break;
3163 if (revs->first_parent_only)
3164 cnt = 1;
3165 else
3166 cnt = remove_duplicate_parents(revs, commit);
3169 * It is possible that we are a merge and one side branch
3170 * does not have any commit that touches the given paths;
3171 * in such a case, the immediate parent from that branch
3172 * will be rewritten to be the merge base.
3174 * o----X X: the commit we are looking at;
3175 * / / o: a commit that touches the paths;
3176 * ---o----'
3178 * Further, a merge of an independent branch that doesn't
3179 * touch the path will reduce to a treesame root parent:
3181 * ----o----X X: the commit we are looking at;
3182 * / o: a commit that touches the paths;
3183 * r r: a root commit not touching the paths
3185 * Detect and simplify both cases.
3187 if (1 < cnt) {
3188 int marked = mark_redundant_parents(commit);
3189 marked += mark_treesame_root_parents(commit);
3190 if (marked)
3191 marked -= leave_one_treesame_to_parent(revs, commit);
3192 if (marked)
3193 cnt = remove_marked_parents(revs, commit);
3197 * A commit simplifies to itself if it is a root, if it is
3198 * UNINTERESTING, if it touches the given paths, or if it is a
3199 * merge and its parents don't simplify to one relevant commit
3200 * (the first two cases are already handled at the beginning of
3201 * this function).
3203 * Otherwise, it simplifies to what its sole relevant parent
3204 * simplifies to.
3206 if (!cnt ||
3207 (commit->object.flags & UNINTERESTING) ||
3208 !(commit->object.flags & TREESAME) ||
3209 (parent = one_relevant_parent(revs, commit->parents)) == NULL ||
3210 (revs->show_pulls && (commit->object.flags & PULL_MERGE)))
3211 st->simplified = commit;
3212 else {
3213 pst = locate_simplify_state(revs, parent);
3214 st->simplified = pst->simplified;
3216 return tail;
3219 static void simplify_merges(struct rev_info *revs)
3221 struct commit_list *list, *next;
3222 struct commit_list *yet_to_do, **tail;
3223 struct commit *commit;
3225 if (!revs->prune)
3226 return;
3228 /* feed the list reversed */
3229 yet_to_do = NULL;
3230 for (list = revs->commits; list; list = next) {
3231 commit = list->item;
3232 next = list->next;
3234 * Do not free(list) here yet; the original list
3235 * is used later in this function.
3237 commit_list_insert(commit, &yet_to_do);
3239 while (yet_to_do) {
3240 list = yet_to_do;
3241 yet_to_do = NULL;
3242 tail = &yet_to_do;
3243 while (list) {
3244 commit = pop_commit(&list);
3245 tail = simplify_one(revs, commit, tail);
3249 /* clean up the result, removing the simplified ones */
3250 list = revs->commits;
3251 revs->commits = NULL;
3252 tail = &revs->commits;
3253 while (list) {
3254 struct merge_simplify_state *st;
3256 commit = pop_commit(&list);
3257 st = locate_simplify_state(revs, commit);
3258 if (st->simplified == commit)
3259 tail = &commit_list_insert(commit, tail)->next;
3263 static void set_children(struct rev_info *revs)
3265 struct commit_list *l;
3266 for (l = revs->commits; l; l = l->next) {
3267 struct commit *commit = l->item;
3268 struct commit_list *p;
3270 for (p = commit->parents; p; p = p->next)
3271 add_child(revs, p->item, commit);
3275 void reset_revision_walk(void)
3277 clear_object_flags(SEEN | ADDED | SHOWN | TOPO_WALK_EXPLORED | TOPO_WALK_INDEGREE);
3280 static int mark_uninteresting(const struct object_id *oid,
3281 struct packed_git *pack,
3282 uint32_t pos,
3283 void *cb)
3285 struct rev_info *revs = cb;
3286 struct object *o = parse_object(revs->repo, oid);
3287 o->flags |= UNINTERESTING | SEEN;
3288 return 0;
3291 define_commit_slab(indegree_slab, int);
3292 define_commit_slab(author_date_slab, timestamp_t);
3294 struct topo_walk_info {
3295 uint32_t min_generation;
3296 struct prio_queue explore_queue;
3297 struct prio_queue indegree_queue;
3298 struct prio_queue topo_queue;
3299 struct indegree_slab indegree;
3300 struct author_date_slab author_date;
3303 static inline void test_flag_and_insert(struct prio_queue *q, struct commit *c, int flag)
3305 if (c->object.flags & flag)
3306 return;
3308 c->object.flags |= flag;
3309 prio_queue_put(q, c);
3312 static void explore_walk_step(struct rev_info *revs)
3314 struct topo_walk_info *info = revs->topo_walk_info;
3315 struct commit_list *p;
3316 struct commit *c = prio_queue_get(&info->explore_queue);
3318 if (!c)
3319 return;
3321 if (parse_commit_gently(c, 1) < 0)
3322 return;
3324 if (revs->sort_order == REV_SORT_BY_AUTHOR_DATE)
3325 record_author_date(&info->author_date, c);
3327 if (revs->max_age != -1 && (c->date < revs->max_age))
3328 c->object.flags |= UNINTERESTING;
3330 if (process_parents(revs, c, NULL, NULL) < 0)
3331 return;
3333 if (c->object.flags & UNINTERESTING)
3334 mark_parents_uninteresting(c);
3336 for (p = c->parents; p; p = p->next)
3337 test_flag_and_insert(&info->explore_queue, p->item, TOPO_WALK_EXPLORED);
3340 static void explore_to_depth(struct rev_info *revs,
3341 uint32_t gen_cutoff)
3343 struct topo_walk_info *info = revs->topo_walk_info;
3344 struct commit *c;
3345 while ((c = prio_queue_peek(&info->explore_queue)) &&
3346 commit_graph_generation(c) >= gen_cutoff)
3347 explore_walk_step(revs);
3350 static void indegree_walk_step(struct rev_info *revs)
3352 struct commit_list *p;
3353 struct topo_walk_info *info = revs->topo_walk_info;
3354 struct commit *c = prio_queue_get(&info->indegree_queue);
3356 if (!c)
3357 return;
3359 if (parse_commit_gently(c, 1) < 0)
3360 return;
3362 explore_to_depth(revs, commit_graph_generation(c));
3364 for (p = c->parents; p; p = p->next) {
3365 struct commit *parent = p->item;
3366 int *pi = indegree_slab_at(&info->indegree, parent);
3368 if (*pi)
3369 (*pi)++;
3370 else
3371 *pi = 2;
3373 test_flag_and_insert(&info->indegree_queue, parent, TOPO_WALK_INDEGREE);
3375 if (revs->first_parent_only)
3376 return;
3380 static void compute_indegrees_to_depth(struct rev_info *revs,
3381 uint32_t gen_cutoff)
3383 struct topo_walk_info *info = revs->topo_walk_info;
3384 struct commit *c;
3385 while ((c = prio_queue_peek(&info->indegree_queue)) &&
3386 commit_graph_generation(c) >= gen_cutoff)
3387 indegree_walk_step(revs);
3390 static void reset_topo_walk(struct rev_info *revs)
3392 struct topo_walk_info *info = revs->topo_walk_info;
3394 clear_prio_queue(&info->explore_queue);
3395 clear_prio_queue(&info->indegree_queue);
3396 clear_prio_queue(&info->topo_queue);
3397 clear_indegree_slab(&info->indegree);
3398 clear_author_date_slab(&info->author_date);
3400 FREE_AND_NULL(revs->topo_walk_info);
3403 static void init_topo_walk(struct rev_info *revs)
3405 struct topo_walk_info *info;
3406 struct commit_list *list;
3407 if (revs->topo_walk_info)
3408 reset_topo_walk(revs);
3410 revs->topo_walk_info = xmalloc(sizeof(struct topo_walk_info));
3411 info = revs->topo_walk_info;
3412 memset(info, 0, sizeof(struct topo_walk_info));
3414 init_indegree_slab(&info->indegree);
3415 memset(&info->explore_queue, 0, sizeof(info->explore_queue));
3416 memset(&info->indegree_queue, 0, sizeof(info->indegree_queue));
3417 memset(&info->topo_queue, 0, sizeof(info->topo_queue));
3419 switch (revs->sort_order) {
3420 default: /* REV_SORT_IN_GRAPH_ORDER */
3421 info->topo_queue.compare = NULL;
3422 break;
3423 case REV_SORT_BY_COMMIT_DATE:
3424 info->topo_queue.compare = compare_commits_by_commit_date;
3425 break;
3426 case REV_SORT_BY_AUTHOR_DATE:
3427 init_author_date_slab(&info->author_date);
3428 info->topo_queue.compare = compare_commits_by_author_date;
3429 info->topo_queue.cb_data = &info->author_date;
3430 break;
3433 info->explore_queue.compare = compare_commits_by_gen_then_commit_date;
3434 info->indegree_queue.compare = compare_commits_by_gen_then_commit_date;
3436 info->min_generation = GENERATION_NUMBER_INFINITY;
3437 for (list = revs->commits; list; list = list->next) {
3438 struct commit *c = list->item;
3439 uint32_t generation;
3441 if (parse_commit_gently(c, 1))
3442 continue;
3444 test_flag_and_insert(&info->explore_queue, c, TOPO_WALK_EXPLORED);
3445 test_flag_and_insert(&info->indegree_queue, c, TOPO_WALK_INDEGREE);
3447 generation = commit_graph_generation(c);
3448 if (generation < info->min_generation)
3449 info->min_generation = generation;
3451 *(indegree_slab_at(&info->indegree, c)) = 1;
3453 if (revs->sort_order == REV_SORT_BY_AUTHOR_DATE)
3454 record_author_date(&info->author_date, c);
3456 compute_indegrees_to_depth(revs, info->min_generation);
3458 for (list = revs->commits; list; list = list->next) {
3459 struct commit *c = list->item;
3461 if (*(indegree_slab_at(&info->indegree, c)) == 1)
3462 prio_queue_put(&info->topo_queue, c);
3466 * This is unfortunate; the initial tips need to be shown
3467 * in the order given from the revision traversal machinery.
3469 if (revs->sort_order == REV_SORT_IN_GRAPH_ORDER)
3470 prio_queue_reverse(&info->topo_queue);
3473 static struct commit *next_topo_commit(struct rev_info *revs)
3475 struct commit *c;
3476 struct topo_walk_info *info = revs->topo_walk_info;
3478 /* pop next off of topo_queue */
3479 c = prio_queue_get(&info->topo_queue);
3481 if (c)
3482 *(indegree_slab_at(&info->indegree, c)) = 0;
3484 return c;
3487 static void expand_topo_walk(struct rev_info *revs, struct commit *commit)
3489 struct commit_list *p;
3490 struct topo_walk_info *info = revs->topo_walk_info;
3491 if (process_parents(revs, commit, NULL, NULL) < 0) {
3492 if (!revs->ignore_missing_links)
3493 die("Failed to traverse parents of commit %s",
3494 oid_to_hex(&commit->object.oid));
3497 for (p = commit->parents; p; p = p->next) {
3498 struct commit *parent = p->item;
3499 int *pi;
3500 uint32_t generation;
3502 if (parent->object.flags & UNINTERESTING)
3503 continue;
3505 if (parse_commit_gently(parent, 1) < 0)
3506 continue;
3508 generation = commit_graph_generation(parent);
3509 if (generation < info->min_generation) {
3510 info->min_generation = generation;
3511 compute_indegrees_to_depth(revs, info->min_generation);
3514 pi = indegree_slab_at(&info->indegree, parent);
3516 (*pi)--;
3517 if (*pi == 1)
3518 prio_queue_put(&info->topo_queue, parent);
3520 if (revs->first_parent_only)
3521 return;
3525 int prepare_revision_walk(struct rev_info *revs)
3527 int i;
3528 struct object_array old_pending;
3529 struct commit_list **next = &revs->commits;
3531 memcpy(&old_pending, &revs->pending, sizeof(old_pending));
3532 revs->pending.nr = 0;
3533 revs->pending.alloc = 0;
3534 revs->pending.objects = NULL;
3535 for (i = 0; i < old_pending.nr; i++) {
3536 struct object_array_entry *e = old_pending.objects + i;
3537 struct commit *commit = handle_commit(revs, e);
3538 if (commit) {
3539 if (!(commit->object.flags & SEEN)) {
3540 commit->object.flags |= SEEN;
3541 next = commit_list_append(commit, next);
3545 object_array_clear(&old_pending);
3547 /* Signal whether we need per-parent treesame decoration */
3548 if (revs->simplify_merges ||
3549 (revs->limited && limiting_can_increase_treesame(revs)))
3550 revs->treesame.name = "treesame";
3552 if (revs->exclude_promisor_objects) {
3553 for_each_packed_object(mark_uninteresting, revs,
3554 FOR_EACH_OBJECT_PROMISOR_ONLY);
3557 if (!revs->reflog_info)
3558 prepare_to_use_bloom_filter(revs);
3559 if (revs->no_walk != REVISION_WALK_NO_WALK_UNSORTED)
3560 commit_list_sort_by_date(&revs->commits);
3561 if (revs->no_walk)
3562 return 0;
3563 if (revs->limited) {
3564 if (limit_list(revs) < 0)
3565 return -1;
3566 if (revs->topo_order)
3567 sort_in_topological_order(&revs->commits, revs->sort_order);
3568 } else if (revs->topo_order)
3569 init_topo_walk(revs);
3570 if (revs->line_level_traverse && want_ancestry(revs))
3572 * At the moment we can only do line-level log with parent
3573 * rewriting by performing this expensive pre-filtering step.
3574 * If parent rewriting is not requested, then we rather
3575 * perform the line-level log filtering during the regular
3576 * history traversal.
3578 line_log_filter(revs);
3579 if (revs->simplify_merges)
3580 simplify_merges(revs);
3581 if (revs->children.name)
3582 set_children(revs);
3584 return 0;
3587 static enum rewrite_result rewrite_one_1(struct rev_info *revs,
3588 struct commit **pp,
3589 struct prio_queue *queue)
3591 for (;;) {
3592 struct commit *p = *pp;
3593 if (!revs->limited)
3594 if (process_parents(revs, p, NULL, queue) < 0)
3595 return rewrite_one_error;
3596 if (p->object.flags & UNINTERESTING)
3597 return rewrite_one_ok;
3598 if (!(p->object.flags & TREESAME))
3599 return rewrite_one_ok;
3600 if (!p->parents)
3601 return rewrite_one_noparents;
3602 if ((p = one_relevant_parent(revs, p->parents)) == NULL)
3603 return rewrite_one_ok;
3604 *pp = p;
3608 static void merge_queue_into_list(struct prio_queue *q, struct commit_list **list)
3610 while (q->nr) {
3611 struct commit *item = prio_queue_peek(q);
3612 struct commit_list *p = *list;
3614 if (p && p->item->date >= item->date)
3615 list = &p->next;
3616 else {
3617 p = commit_list_insert(item, list);
3618 list = &p->next; /* skip newly added item */
3619 prio_queue_get(q); /* pop item */
3624 static enum rewrite_result rewrite_one(struct rev_info *revs, struct commit **pp)
3626 struct prio_queue queue = { compare_commits_by_commit_date };
3627 enum rewrite_result ret = rewrite_one_1(revs, pp, &queue);
3628 merge_queue_into_list(&queue, &revs->commits);
3629 clear_prio_queue(&queue);
3630 return ret;
3633 int rewrite_parents(struct rev_info *revs, struct commit *commit,
3634 rewrite_parent_fn_t rewrite_parent)
3636 struct commit_list **pp = &commit->parents;
3637 while (*pp) {
3638 struct commit_list *parent = *pp;
3639 switch (rewrite_parent(revs, &parent->item)) {
3640 case rewrite_one_ok:
3641 break;
3642 case rewrite_one_noparents:
3643 *pp = parent->next;
3644 continue;
3645 case rewrite_one_error:
3646 return -1;
3648 pp = &parent->next;
3650 remove_duplicate_parents(revs, commit);
3651 return 0;
3654 static int commit_rewrite_person(struct strbuf *buf, const char *what, struct string_list *mailmap)
3656 char *person, *endp;
3657 size_t len, namelen, maillen;
3658 const char *name;
3659 const char *mail;
3660 struct ident_split ident;
3662 person = strstr(buf->buf, what);
3663 if (!person)
3664 return 0;
3666 person += strlen(what);
3667 endp = strchr(person, '\n');
3668 if (!endp)
3669 return 0;
3671 len = endp - person;
3673 if (split_ident_line(&ident, person, len))
3674 return 0;
3676 mail = ident.mail_begin;
3677 maillen = ident.mail_end - ident.mail_begin;
3678 name = ident.name_begin;
3679 namelen = ident.name_end - ident.name_begin;
3681 if (map_user(mailmap, &mail, &maillen, &name, &namelen)) {
3682 struct strbuf namemail = STRBUF_INIT;
3684 strbuf_addf(&namemail, "%.*s <%.*s>",
3685 (int)namelen, name, (int)maillen, mail);
3687 strbuf_splice(buf, ident.name_begin - buf->buf,
3688 ident.mail_end - ident.name_begin + 1,
3689 namemail.buf, namemail.len);
3691 strbuf_release(&namemail);
3693 return 1;
3696 return 0;
3699 static int commit_match(struct commit *commit, struct rev_info *opt)
3701 int retval;
3702 const char *encoding;
3703 const char *message;
3704 struct strbuf buf = STRBUF_INIT;
3706 if (!opt->grep_filter.pattern_list && !opt->grep_filter.header_list)
3707 return 1;
3709 /* Prepend "fake" headers as needed */
3710 if (opt->grep_filter.use_reflog_filter) {
3711 strbuf_addstr(&buf, "reflog ");
3712 get_reflog_message(&buf, opt->reflog_info);
3713 strbuf_addch(&buf, '\n');
3717 * We grep in the user's output encoding, under the assumption that it
3718 * is the encoding they are most likely to write their grep pattern
3719 * for. In addition, it means we will match the "notes" encoding below,
3720 * so we will not end up with a buffer that has two different encodings
3721 * in it.
3723 encoding = get_log_output_encoding();
3724 message = logmsg_reencode(commit, NULL, encoding);
3726 /* Copy the commit to temporary if we are using "fake" headers */
3727 if (buf.len)
3728 strbuf_addstr(&buf, message);
3730 if (opt->grep_filter.header_list && opt->mailmap) {
3731 if (!buf.len)
3732 strbuf_addstr(&buf, message);
3734 commit_rewrite_person(&buf, "\nauthor ", opt->mailmap);
3735 commit_rewrite_person(&buf, "\ncommitter ", opt->mailmap);
3738 /* Append "fake" message parts as needed */
3739 if (opt->show_notes) {
3740 if (!buf.len)
3741 strbuf_addstr(&buf, message);
3742 format_display_notes(&commit->object.oid, &buf, encoding, 1);
3746 * Find either in the original commit message, or in the temporary.
3747 * Note that we cast away the constness of "message" here. It is
3748 * const because it may come from the cached commit buffer. That's OK,
3749 * because we know that it is modifiable heap memory, and that while
3750 * grep_buffer may modify it for speed, it will restore any
3751 * changes before returning.
3753 if (buf.len)
3754 retval = grep_buffer(&opt->grep_filter, buf.buf, buf.len);
3755 else
3756 retval = grep_buffer(&opt->grep_filter,
3757 (char *)message, strlen(message));
3758 strbuf_release(&buf);
3759 unuse_commit_buffer(commit, message);
3760 return opt->invert_grep ? !retval : retval;
3763 static inline int want_ancestry(const struct rev_info *revs)
3765 return (revs->rewrite_parents || revs->children.name);
3769 * Return a timestamp to be used for --since/--until comparisons for this
3770 * commit, based on the revision options.
3772 static timestamp_t comparison_date(const struct rev_info *revs,
3773 struct commit *commit)
3775 return revs->reflog_info ?
3776 get_reflog_timestamp(revs->reflog_info) :
3777 commit->date;
3780 enum commit_action get_commit_action(struct rev_info *revs, struct commit *commit)
3782 if (commit->object.flags & SHOWN)
3783 return commit_ignore;
3784 if (revs->unpacked && has_object_pack(&commit->object.oid))
3785 return commit_ignore;
3786 if (commit->object.flags & UNINTERESTING)
3787 return commit_ignore;
3788 if (revs->line_level_traverse && !want_ancestry(revs)) {
3790 * In case of line-level log with parent rewriting
3791 * prepare_revision_walk() already took care of all line-level
3792 * log filtering, and there is nothing left to do here.
3794 * If parent rewriting was not requested, then this is the
3795 * place to perform the line-level log filtering. Notably,
3796 * this check, though expensive, must come before the other,
3797 * cheaper filtering conditions, because the tracked line
3798 * ranges must be adjusted even when the commit will end up
3799 * being ignored based on other conditions.
3801 if (!line_log_process_ranges_arbitrary_commit(revs, commit))
3802 return commit_ignore;
3804 if (revs->min_age != -1 &&
3805 comparison_date(revs, commit) > revs->min_age)
3806 return commit_ignore;
3807 if (revs->min_parents || (revs->max_parents >= 0)) {
3808 int n = commit_list_count(commit->parents);
3809 if ((n < revs->min_parents) ||
3810 ((revs->max_parents >= 0) && (n > revs->max_parents)))
3811 return commit_ignore;
3813 if (!commit_match(commit, revs))
3814 return commit_ignore;
3815 if (revs->prune && revs->dense) {
3816 /* Commit without changes? */
3817 if (commit->object.flags & TREESAME) {
3818 int n;
3819 struct commit_list *p;
3820 /* drop merges unless we want parenthood */
3821 if (!want_ancestry(revs))
3822 return commit_ignore;
3824 if (revs->show_pulls && (commit->object.flags & PULL_MERGE))
3825 return commit_show;
3828 * If we want ancestry, then need to keep any merges
3829 * between relevant commits to tie together topology.
3830 * For consistency with TREESAME and simplification
3831 * use "relevant" here rather than just INTERESTING,
3832 * to treat bottom commit(s) as part of the topology.
3834 for (n = 0, p = commit->parents; p; p = p->next)
3835 if (relevant_commit(p->item))
3836 if (++n >= 2)
3837 return commit_show;
3838 return commit_ignore;
3841 return commit_show;
3844 define_commit_slab(saved_parents, struct commit_list *);
3846 #define EMPTY_PARENT_LIST ((struct commit_list *)-1)
3849 * You may only call save_parents() once per commit (this is checked
3850 * for non-root commits).
3852 static void save_parents(struct rev_info *revs, struct commit *commit)
3854 struct commit_list **pp;
3856 if (!revs->saved_parents_slab) {
3857 revs->saved_parents_slab = xmalloc(sizeof(struct saved_parents));
3858 init_saved_parents(revs->saved_parents_slab);
3861 pp = saved_parents_at(revs->saved_parents_slab, commit);
3864 * When walking with reflogs, we may visit the same commit
3865 * several times: once for each appearance in the reflog.
3867 * In this case, save_parents() will be called multiple times.
3868 * We want to keep only the first set of parents. We need to
3869 * store a sentinel value for an empty (i.e., NULL) parent
3870 * list to distinguish it from a not-yet-saved list, however.
3872 if (*pp)
3873 return;
3874 if (commit->parents)
3875 *pp = copy_commit_list(commit->parents);
3876 else
3877 *pp = EMPTY_PARENT_LIST;
3880 static void free_saved_parents(struct rev_info *revs)
3882 if (revs->saved_parents_slab)
3883 clear_saved_parents(revs->saved_parents_slab);
3886 struct commit_list *get_saved_parents(struct rev_info *revs, const struct commit *commit)
3888 struct commit_list *parents;
3890 if (!revs->saved_parents_slab)
3891 return commit->parents;
3893 parents = *saved_parents_at(revs->saved_parents_slab, commit);
3894 if (parents == EMPTY_PARENT_LIST)
3895 return NULL;
3896 return parents;
3899 enum commit_action simplify_commit(struct rev_info *revs, struct commit *commit)
3901 enum commit_action action = get_commit_action(revs, commit);
3903 if (action == commit_show &&
3904 revs->prune && revs->dense && want_ancestry(revs)) {
3906 * --full-diff on simplified parents is no good: it
3907 * will show spurious changes from the commits that
3908 * were elided. So we save the parents on the side
3909 * when --full-diff is in effect.
3911 if (revs->full_diff)
3912 save_parents(revs, commit);
3913 if (rewrite_parents(revs, commit, rewrite_one) < 0)
3914 return commit_error;
3916 return action;
3919 static void track_linear(struct rev_info *revs, struct commit *commit)
3921 if (revs->track_first_time) {
3922 revs->linear = 1;
3923 revs->track_first_time = 0;
3924 } else {
3925 struct commit_list *p;
3926 for (p = revs->previous_parents; p; p = p->next)
3927 if (p->item == NULL || /* first commit */
3928 oideq(&p->item->object.oid, &commit->object.oid))
3929 break;
3930 revs->linear = p != NULL;
3932 if (revs->reverse) {
3933 if (revs->linear)
3934 commit->object.flags |= TRACK_LINEAR;
3936 free_commit_list(revs->previous_parents);
3937 revs->previous_parents = copy_commit_list(commit->parents);
3940 static struct commit *get_revision_1(struct rev_info *revs)
3942 while (1) {
3943 struct commit *commit;
3945 if (revs->reflog_info)
3946 commit = next_reflog_entry(revs->reflog_info);
3947 else if (revs->topo_walk_info)
3948 commit = next_topo_commit(revs);
3949 else
3950 commit = pop_commit(&revs->commits);
3952 if (!commit)
3953 return NULL;
3955 if (revs->reflog_info)
3956 commit->object.flags &= ~(ADDED | SEEN | SHOWN);
3959 * If we haven't done the list limiting, we need to look at
3960 * the parents here. We also need to do the date-based limiting
3961 * that we'd otherwise have done in limit_list().
3963 if (!revs->limited) {
3964 if (revs->max_age != -1 &&
3965 comparison_date(revs, commit) < revs->max_age)
3966 continue;
3968 if (revs->reflog_info)
3969 try_to_simplify_commit(revs, commit);
3970 else if (revs->topo_walk_info)
3971 expand_topo_walk(revs, commit);
3972 else if (process_parents(revs, commit, &revs->commits, NULL) < 0) {
3973 if (!revs->ignore_missing_links)
3974 die("Failed to traverse parents of commit %s",
3975 oid_to_hex(&commit->object.oid));
3979 switch (simplify_commit(revs, commit)) {
3980 case commit_ignore:
3981 continue;
3982 case commit_error:
3983 die("Failed to simplify parents of commit %s",
3984 oid_to_hex(&commit->object.oid));
3985 default:
3986 if (revs->track_linear)
3987 track_linear(revs, commit);
3988 return commit;
3994 * Return true for entries that have not yet been shown. (This is an
3995 * object_array_each_func_t.)
3997 static int entry_unshown(struct object_array_entry *entry, void *cb_data_unused)
3999 return !(entry->item->flags & SHOWN);
4003 * If array is on the verge of a realloc, garbage-collect any entries
4004 * that have already been shown to try to free up some space.
4006 static void gc_boundary(struct object_array *array)
4008 if (array->nr == array->alloc)
4009 object_array_filter(array, entry_unshown, NULL);
4012 static void create_boundary_commit_list(struct rev_info *revs)
4014 unsigned i;
4015 struct commit *c;
4016 struct object_array *array = &revs->boundary_commits;
4017 struct object_array_entry *objects = array->objects;
4020 * If revs->commits is non-NULL at this point, an error occurred in
4021 * get_revision_1(). Ignore the error and continue printing the
4022 * boundary commits anyway. (This is what the code has always
4023 * done.)
4025 if (revs->commits) {
4026 free_commit_list(revs->commits);
4027 revs->commits = NULL;
4031 * Put all of the actual boundary commits from revs->boundary_commits
4032 * into revs->commits
4034 for (i = 0; i < array->nr; i++) {
4035 c = (struct commit *)(objects[i].item);
4036 if (!c)
4037 continue;
4038 if (!(c->object.flags & CHILD_SHOWN))
4039 continue;
4040 if (c->object.flags & (SHOWN | BOUNDARY))
4041 continue;
4042 c->object.flags |= BOUNDARY;
4043 commit_list_insert(c, &revs->commits);
4047 * If revs->topo_order is set, sort the boundary commits
4048 * in topological order
4050 sort_in_topological_order(&revs->commits, revs->sort_order);
4053 static struct commit *get_revision_internal(struct rev_info *revs)
4055 struct commit *c = NULL;
4056 struct commit_list *l;
4058 if (revs->boundary == 2) {
4060 * All of the normal commits have already been returned,
4061 * and we are now returning boundary commits.
4062 * create_boundary_commit_list() has populated
4063 * revs->commits with the remaining commits to return.
4065 c = pop_commit(&revs->commits);
4066 if (c)
4067 c->object.flags |= SHOWN;
4068 return c;
4072 * If our max_count counter has reached zero, then we are done. We
4073 * don't simply return NULL because we still might need to show
4074 * boundary commits. But we want to avoid calling get_revision_1, which
4075 * might do a considerable amount of work finding the next commit only
4076 * for us to throw it away.
4078 * If it is non-zero, then either we don't have a max_count at all
4079 * (-1), or it is still counting, in which case we decrement.
4081 if (revs->max_count) {
4082 c = get_revision_1(revs);
4083 if (c) {
4084 while (revs->skip_count > 0) {
4085 revs->skip_count--;
4086 c = get_revision_1(revs);
4087 if (!c)
4088 break;
4092 if (revs->max_count > 0)
4093 revs->max_count--;
4096 if (c)
4097 c->object.flags |= SHOWN;
4099 if (!revs->boundary)
4100 return c;
4102 if (!c) {
4104 * get_revision_1() runs out the commits, and
4105 * we are done computing the boundaries.
4106 * switch to boundary commits output mode.
4108 revs->boundary = 2;
4111 * Update revs->commits to contain the list of
4112 * boundary commits.
4114 create_boundary_commit_list(revs);
4116 return get_revision_internal(revs);
4120 * boundary commits are the commits that are parents of the
4121 * ones we got from get_revision_1() but they themselves are
4122 * not returned from get_revision_1(). Before returning
4123 * 'c', we need to mark its parents that they could be boundaries.
4126 for (l = c->parents; l; l = l->next) {
4127 struct object *p;
4128 p = &(l->item->object);
4129 if (p->flags & (CHILD_SHOWN | SHOWN))
4130 continue;
4131 p->flags |= CHILD_SHOWN;
4132 gc_boundary(&revs->boundary_commits);
4133 add_object_array(p, NULL, &revs->boundary_commits);
4136 return c;
4139 struct commit *get_revision(struct rev_info *revs)
4141 struct commit *c;
4142 struct commit_list *reversed;
4144 if (revs->reverse) {
4145 reversed = NULL;
4146 while ((c = get_revision_internal(revs)))
4147 commit_list_insert(c, &reversed);
4148 revs->commits = reversed;
4149 revs->reverse = 0;
4150 revs->reverse_output_stage = 1;
4153 if (revs->reverse_output_stage) {
4154 c = pop_commit(&revs->commits);
4155 if (revs->track_linear)
4156 revs->linear = !!(c && c->object.flags & TRACK_LINEAR);
4157 return c;
4160 c = get_revision_internal(revs);
4161 if (c && revs->graph)
4162 graph_update(revs->graph, c);
4163 if (!c) {
4164 free_saved_parents(revs);
4165 if (revs->previous_parents) {
4166 free_commit_list(revs->previous_parents);
4167 revs->previous_parents = NULL;
4170 return c;
4173 const char *get_revision_mark(const struct rev_info *revs, const struct commit *commit)
4175 if (commit->object.flags & BOUNDARY)
4176 return "-";
4177 else if (commit->object.flags & UNINTERESTING)
4178 return "^";
4179 else if (commit->object.flags & PATCHSAME)
4180 return "=";
4181 else if (!revs || revs->left_right) {
4182 if (commit->object.flags & SYMMETRIC_LEFT)
4183 return "<";
4184 else
4185 return ">";
4186 } else if (revs->graph)
4187 return "*";
4188 else if (revs->cherry_mark)
4189 return "+";
4190 return "";
4193 void put_revision_mark(const struct rev_info *revs, const struct commit *commit)
4195 const char *mark = get_revision_mark(revs, commit);
4196 if (!strlen(mark))
4197 return;
4198 fputs(mark, stdout);
4199 putchar(' ');