diff-merges: fix -m to properly override -c/--cc
[git.git] / revision.c
blob5a0e3d6ad54251ad4f674a0e6c80070cd45efc85
1 #include "cache.h"
2 #include "object-store.h"
3 #include "tag.h"
4 #include "blob.h"
5 #include "tree.h"
6 #include "commit.h"
7 #include "diff.h"
8 #include "diff-merges.h"
9 #include "refs.h"
10 #include "revision.h"
11 #include "repository.h"
12 #include "graph.h"
13 #include "grep.h"
14 #include "reflog-walk.h"
15 #include "patch-ids.h"
16 #include "decorate.h"
17 #include "log-tree.h"
18 #include "string-list.h"
19 #include "line-log.h"
20 #include "mailmap.h"
21 #include "commit-slab.h"
22 #include "dir.h"
23 #include "cache-tree.h"
24 #include "bisect.h"
25 #include "packfile.h"
26 #include "worktree.h"
27 #include "strvec.h"
28 #include "commit-reach.h"
29 #include "commit-graph.h"
30 #include "prio-queue.h"
31 #include "hashmap.h"
32 #include "utf8.h"
33 #include "bloom.h"
34 #include "json-writer.h"
36 volatile show_early_output_fn_t show_early_output;
38 static const char *term_bad;
39 static const char *term_good;
41 implement_shared_commit_slab(revision_sources, char *);
43 static inline int want_ancestry(const struct rev_info *revs);
45 void show_object_with_name(FILE *out, struct object *obj, const char *name)
47 const char *p;
49 fprintf(out, "%s ", oid_to_hex(&obj->oid));
50 for (p = name; *p && *p != '\n'; p++)
51 fputc(*p, out);
52 fputc('\n', out);
55 static void mark_blob_uninteresting(struct blob *blob)
57 if (!blob)
58 return;
59 if (blob->object.flags & UNINTERESTING)
60 return;
61 blob->object.flags |= UNINTERESTING;
64 static void mark_tree_contents_uninteresting(struct repository *r,
65 struct tree *tree)
67 struct tree_desc desc;
68 struct name_entry entry;
70 if (parse_tree_gently(tree, 1) < 0)
71 return;
73 init_tree_desc(&desc, tree->buffer, tree->size);
74 while (tree_entry(&desc, &entry)) {
75 switch (object_type(entry.mode)) {
76 case OBJ_TREE:
77 mark_tree_uninteresting(r, lookup_tree(r, &entry.oid));
78 break;
79 case OBJ_BLOB:
80 mark_blob_uninteresting(lookup_blob(r, &entry.oid));
81 break;
82 default:
83 /* Subproject commit - not in this repository */
84 break;
89 * We don't care about the tree any more
90 * after it has been marked uninteresting.
92 free_tree_buffer(tree);
95 void mark_tree_uninteresting(struct repository *r, struct tree *tree)
97 struct object *obj;
99 if (!tree)
100 return;
102 obj = &tree->object;
103 if (obj->flags & UNINTERESTING)
104 return;
105 obj->flags |= UNINTERESTING;
106 mark_tree_contents_uninteresting(r, tree);
109 struct path_and_oids_entry {
110 struct hashmap_entry ent;
111 char *path;
112 struct oidset trees;
115 static int path_and_oids_cmp(const void *hashmap_cmp_fn_data,
116 const struct hashmap_entry *eptr,
117 const struct hashmap_entry *entry_or_key,
118 const void *keydata)
120 const struct path_and_oids_entry *e1, *e2;
122 e1 = container_of(eptr, const struct path_and_oids_entry, ent);
123 e2 = container_of(entry_or_key, const struct path_and_oids_entry, ent);
125 return strcmp(e1->path, e2->path);
128 static void paths_and_oids_init(struct hashmap *map)
130 hashmap_init(map, path_and_oids_cmp, NULL, 0);
133 static void paths_and_oids_clear(struct hashmap *map)
135 struct hashmap_iter iter;
136 struct path_and_oids_entry *entry;
138 hashmap_for_each_entry(map, &iter, entry, ent /* member name */) {
139 oidset_clear(&entry->trees);
140 free(entry->path);
143 hashmap_free_entries(map, struct path_and_oids_entry, ent);
146 static void paths_and_oids_insert(struct hashmap *map,
147 const char *path,
148 const struct object_id *oid)
150 int hash = strhash(path);
151 struct path_and_oids_entry key;
152 struct path_and_oids_entry *entry;
154 hashmap_entry_init(&key.ent, hash);
156 /* use a shallow copy for the lookup */
157 key.path = (char *)path;
158 oidset_init(&key.trees, 0);
160 entry = hashmap_get_entry(map, &key, ent, NULL);
161 if (!entry) {
162 entry = xcalloc(1, sizeof(struct path_and_oids_entry));
163 hashmap_entry_init(&entry->ent, hash);
164 entry->path = xstrdup(key.path);
165 oidset_init(&entry->trees, 16);
166 hashmap_put(map, &entry->ent);
169 oidset_insert(&entry->trees, oid);
172 static void add_children_by_path(struct repository *r,
173 struct tree *tree,
174 struct hashmap *map)
176 struct tree_desc desc;
177 struct name_entry entry;
179 if (!tree)
180 return;
182 if (parse_tree_gently(tree, 1) < 0)
183 return;
185 init_tree_desc(&desc, tree->buffer, tree->size);
186 while (tree_entry(&desc, &entry)) {
187 switch (object_type(entry.mode)) {
188 case OBJ_TREE:
189 paths_and_oids_insert(map, entry.path, &entry.oid);
191 if (tree->object.flags & UNINTERESTING) {
192 struct tree *child = lookup_tree(r, &entry.oid);
193 if (child)
194 child->object.flags |= UNINTERESTING;
196 break;
197 case OBJ_BLOB:
198 if (tree->object.flags & UNINTERESTING) {
199 struct blob *child = lookup_blob(r, &entry.oid);
200 if (child)
201 child->object.flags |= UNINTERESTING;
203 break;
204 default:
205 /* Subproject commit - not in this repository */
206 break;
210 free_tree_buffer(tree);
213 void mark_trees_uninteresting_sparse(struct repository *r,
214 struct oidset *trees)
216 unsigned has_interesting = 0, has_uninteresting = 0;
217 struct hashmap map;
218 struct hashmap_iter map_iter;
219 struct path_and_oids_entry *entry;
220 struct object_id *oid;
221 struct oidset_iter iter;
223 oidset_iter_init(trees, &iter);
224 while ((!has_interesting || !has_uninteresting) &&
225 (oid = oidset_iter_next(&iter))) {
226 struct tree *tree = lookup_tree(r, oid);
228 if (!tree)
229 continue;
231 if (tree->object.flags & UNINTERESTING)
232 has_uninteresting = 1;
233 else
234 has_interesting = 1;
237 /* Do not walk unless we have both types of trees. */
238 if (!has_uninteresting || !has_interesting)
239 return;
241 paths_and_oids_init(&map);
243 oidset_iter_init(trees, &iter);
244 while ((oid = oidset_iter_next(&iter))) {
245 struct tree *tree = lookup_tree(r, oid);
246 add_children_by_path(r, tree, &map);
249 hashmap_for_each_entry(&map, &map_iter, entry, ent /* member name */)
250 mark_trees_uninteresting_sparse(r, &entry->trees);
252 paths_and_oids_clear(&map);
255 struct commit_stack {
256 struct commit **items;
257 size_t nr, alloc;
259 #define COMMIT_STACK_INIT { NULL, 0, 0 }
261 static void commit_stack_push(struct commit_stack *stack, struct commit *commit)
263 ALLOC_GROW(stack->items, stack->nr + 1, stack->alloc);
264 stack->items[stack->nr++] = commit;
267 static struct commit *commit_stack_pop(struct commit_stack *stack)
269 return stack->nr ? stack->items[--stack->nr] : NULL;
272 static void commit_stack_clear(struct commit_stack *stack)
274 FREE_AND_NULL(stack->items);
275 stack->nr = stack->alloc = 0;
278 static void mark_one_parent_uninteresting(struct commit *commit,
279 struct commit_stack *pending)
281 struct commit_list *l;
283 if (commit->object.flags & UNINTERESTING)
284 return;
285 commit->object.flags |= UNINTERESTING;
288 * Normally we haven't parsed the parent
289 * yet, so we won't have a parent of a parent
290 * here. However, it may turn out that we've
291 * reached this commit some other way (where it
292 * wasn't uninteresting), in which case we need
293 * to mark its parents recursively too..
295 for (l = commit->parents; l; l = l->next)
296 commit_stack_push(pending, l->item);
299 void mark_parents_uninteresting(struct commit *commit)
301 struct commit_stack pending = COMMIT_STACK_INIT;
302 struct commit_list *l;
304 for (l = commit->parents; l; l = l->next)
305 mark_one_parent_uninteresting(l->item, &pending);
307 while (pending.nr > 0)
308 mark_one_parent_uninteresting(commit_stack_pop(&pending),
309 &pending);
311 commit_stack_clear(&pending);
314 static void add_pending_object_with_path(struct rev_info *revs,
315 struct object *obj,
316 const char *name, unsigned mode,
317 const char *path)
319 struct interpret_branch_name_options options = { 0 };
320 if (!obj)
321 return;
322 if (revs->no_walk && (obj->flags & UNINTERESTING))
323 revs->no_walk = 0;
324 if (revs->reflog_info && obj->type == OBJ_COMMIT) {
325 struct strbuf buf = STRBUF_INIT;
326 int len = interpret_branch_name(name, 0, &buf, &options);
328 if (0 < len && name[len] && buf.len)
329 strbuf_addstr(&buf, name + len);
330 add_reflog_for_walk(revs->reflog_info,
331 (struct commit *)obj,
332 buf.buf[0] ? buf.buf: name);
333 strbuf_release(&buf);
334 return; /* do not add the commit itself */
336 add_object_array_with_path(obj, name, &revs->pending, mode, path);
339 static void add_pending_object_with_mode(struct rev_info *revs,
340 struct object *obj,
341 const char *name, unsigned mode)
343 add_pending_object_with_path(revs, obj, name, mode, NULL);
346 void add_pending_object(struct rev_info *revs,
347 struct object *obj, const char *name)
349 add_pending_object_with_mode(revs, obj, name, S_IFINVALID);
352 void add_head_to_pending(struct rev_info *revs)
354 struct object_id oid;
355 struct object *obj;
356 if (get_oid("HEAD", &oid))
357 return;
358 obj = parse_object(revs->repo, &oid);
359 if (!obj)
360 return;
361 add_pending_object(revs, obj, "HEAD");
364 static struct object *get_reference(struct rev_info *revs, const char *name,
365 const struct object_id *oid,
366 unsigned int flags)
368 struct object *object;
371 * If the repository has commit graphs, repo_parse_commit() avoids
372 * reading the object buffer, so use it whenever possible.
374 if (oid_object_info(revs->repo, oid, NULL) == OBJ_COMMIT) {
375 struct commit *c = lookup_commit(revs->repo, oid);
376 if (!repo_parse_commit(revs->repo, c))
377 object = (struct object *) c;
378 else
379 object = NULL;
380 } else {
381 object = parse_object(revs->repo, oid);
384 if (!object) {
385 if (revs->ignore_missing)
386 return object;
387 if (revs->exclude_promisor_objects && is_promisor_object(oid))
388 return NULL;
389 die("bad object %s", name);
391 object->flags |= flags;
392 return object;
395 void add_pending_oid(struct rev_info *revs, const char *name,
396 const struct object_id *oid, unsigned int flags)
398 struct object *object = get_reference(revs, name, oid, flags);
399 add_pending_object(revs, object, name);
402 static struct commit *handle_commit(struct rev_info *revs,
403 struct object_array_entry *entry)
405 struct object *object = entry->item;
406 const char *name = entry->name;
407 const char *path = entry->path;
408 unsigned int mode = entry->mode;
409 unsigned long flags = object->flags;
412 * Tag object? Look what it points to..
414 while (object->type == OBJ_TAG) {
415 struct tag *tag = (struct tag *) object;
416 if (revs->tag_objects && !(flags & UNINTERESTING))
417 add_pending_object(revs, object, tag->tag);
418 object = parse_object(revs->repo, get_tagged_oid(tag));
419 if (!object) {
420 if (revs->ignore_missing_links || (flags & UNINTERESTING))
421 return NULL;
422 if (revs->exclude_promisor_objects &&
423 is_promisor_object(&tag->tagged->oid))
424 return NULL;
425 die("bad object %s", oid_to_hex(&tag->tagged->oid));
427 object->flags |= flags;
429 * We'll handle the tagged object by looping or dropping
430 * through to the non-tag handlers below. Do not
431 * propagate path data from the tag's pending entry.
433 path = NULL;
434 mode = 0;
438 * Commit object? Just return it, we'll do all the complex
439 * reachability crud.
441 if (object->type == OBJ_COMMIT) {
442 struct commit *commit = (struct commit *)object;
444 if (repo_parse_commit(revs->repo, commit) < 0)
445 die("unable to parse commit %s", name);
446 if (flags & UNINTERESTING) {
447 mark_parents_uninteresting(commit);
449 if (!revs->topo_order || !generation_numbers_enabled(the_repository))
450 revs->limited = 1;
452 if (revs->sources) {
453 char **slot = revision_sources_at(revs->sources, commit);
455 if (!*slot)
456 *slot = xstrdup(name);
458 return commit;
462 * Tree object? Either mark it uninteresting, or add it
463 * to the list of objects to look at later..
465 if (object->type == OBJ_TREE) {
466 struct tree *tree = (struct tree *)object;
467 if (!revs->tree_objects)
468 return NULL;
469 if (flags & UNINTERESTING) {
470 mark_tree_contents_uninteresting(revs->repo, tree);
471 return NULL;
473 add_pending_object_with_path(revs, object, name, mode, path);
474 return NULL;
478 * Blob object? You know the drill by now..
480 if (object->type == OBJ_BLOB) {
481 if (!revs->blob_objects)
482 return NULL;
483 if (flags & UNINTERESTING)
484 return NULL;
485 add_pending_object_with_path(revs, object, name, mode, path);
486 return NULL;
488 die("%s is unknown object", name);
491 static int everybody_uninteresting(struct commit_list *orig,
492 struct commit **interesting_cache)
494 struct commit_list *list = orig;
496 if (*interesting_cache) {
497 struct commit *commit = *interesting_cache;
498 if (!(commit->object.flags & UNINTERESTING))
499 return 0;
502 while (list) {
503 struct commit *commit = list->item;
504 list = list->next;
505 if (commit->object.flags & UNINTERESTING)
506 continue;
508 *interesting_cache = commit;
509 return 0;
511 return 1;
515 * A definition of "relevant" commit that we can use to simplify limited graphs
516 * by eliminating side branches.
518 * A "relevant" commit is one that is !UNINTERESTING (ie we are including it
519 * in our list), or that is a specified BOTTOM commit. Then after computing
520 * a limited list, during processing we can generally ignore boundary merges
521 * coming from outside the graph, (ie from irrelevant parents), and treat
522 * those merges as if they were single-parent. TREESAME is defined to consider
523 * only relevant parents, if any. If we are TREESAME to our on-graph parents,
524 * we don't care if we were !TREESAME to non-graph parents.
526 * Treating bottom commits as relevant ensures that a limited graph's
527 * connection to the actual bottom commit is not viewed as a side branch, but
528 * treated as part of the graph. For example:
530 * ....Z...A---X---o---o---B
531 * . /
532 * W---Y
534 * When computing "A..B", the A-X connection is at least as important as
535 * Y-X, despite A being flagged UNINTERESTING.
537 * And when computing --ancestry-path "A..B", the A-X connection is more
538 * important than Y-X, despite both A and Y being flagged UNINTERESTING.
540 static inline int relevant_commit(struct commit *commit)
542 return (commit->object.flags & (UNINTERESTING | BOTTOM)) != UNINTERESTING;
546 * Return a single relevant commit from a parent list. If we are a TREESAME
547 * commit, and this selects one of our parents, then we can safely simplify to
548 * that parent.
550 static struct commit *one_relevant_parent(const struct rev_info *revs,
551 struct commit_list *orig)
553 struct commit_list *list = orig;
554 struct commit *relevant = NULL;
556 if (!orig)
557 return NULL;
560 * For 1-parent commits, or if first-parent-only, then return that
561 * first parent (even if not "relevant" by the above definition).
562 * TREESAME will have been set purely on that parent.
564 if (revs->first_parent_only || !orig->next)
565 return orig->item;
568 * For multi-parent commits, identify a sole relevant parent, if any.
569 * If we have only one relevant parent, then TREESAME will be set purely
570 * with regard to that parent, and we can simplify accordingly.
572 * If we have more than one relevant parent, or no relevant parents
573 * (and multiple irrelevant ones), then we can't select a parent here
574 * and return NULL.
576 while (list) {
577 struct commit *commit = list->item;
578 list = list->next;
579 if (relevant_commit(commit)) {
580 if (relevant)
581 return NULL;
582 relevant = commit;
585 return relevant;
589 * The goal is to get REV_TREE_NEW as the result only if the
590 * diff consists of all '+' (and no other changes), REV_TREE_OLD
591 * if the whole diff is removal of old data, and otherwise
592 * REV_TREE_DIFFERENT (of course if the trees are the same we
593 * want REV_TREE_SAME).
595 * The only time we care about the distinction is when
596 * remove_empty_trees is in effect, in which case we care only about
597 * whether the whole change is REV_TREE_NEW, or if there's another type
598 * of change. Which means we can stop the diff early in either of these
599 * cases:
601 * 1. We're not using remove_empty_trees at all.
603 * 2. We saw anything except REV_TREE_NEW.
605 static int tree_difference = REV_TREE_SAME;
607 static void file_add_remove(struct diff_options *options,
608 int addremove, unsigned mode,
609 const struct object_id *oid,
610 int oid_valid,
611 const char *fullpath, unsigned dirty_submodule)
613 int diff = addremove == '+' ? REV_TREE_NEW : REV_TREE_OLD;
614 struct rev_info *revs = options->change_fn_data;
616 tree_difference |= diff;
617 if (!revs->remove_empty_trees || tree_difference != REV_TREE_NEW)
618 options->flags.has_changes = 1;
621 static void file_change(struct diff_options *options,
622 unsigned old_mode, unsigned new_mode,
623 const struct object_id *old_oid,
624 const struct object_id *new_oid,
625 int old_oid_valid, int new_oid_valid,
626 const char *fullpath,
627 unsigned old_dirty_submodule, unsigned new_dirty_submodule)
629 tree_difference = REV_TREE_DIFFERENT;
630 options->flags.has_changes = 1;
633 static int bloom_filter_atexit_registered;
634 static unsigned int count_bloom_filter_maybe;
635 static unsigned int count_bloom_filter_definitely_not;
636 static unsigned int count_bloom_filter_false_positive;
637 static unsigned int count_bloom_filter_not_present;
639 static void trace2_bloom_filter_statistics_atexit(void)
641 struct json_writer jw = JSON_WRITER_INIT;
643 jw_object_begin(&jw, 0);
644 jw_object_intmax(&jw, "filter_not_present", count_bloom_filter_not_present);
645 jw_object_intmax(&jw, "maybe", count_bloom_filter_maybe);
646 jw_object_intmax(&jw, "definitely_not", count_bloom_filter_definitely_not);
647 jw_object_intmax(&jw, "false_positive", count_bloom_filter_false_positive);
648 jw_end(&jw);
650 trace2_data_json("bloom", the_repository, "statistics", &jw);
652 jw_release(&jw);
655 static int forbid_bloom_filters(struct pathspec *spec)
657 if (spec->has_wildcard)
658 return 1;
659 if (spec->nr > 1)
660 return 1;
661 if (spec->magic & ~PATHSPEC_LITERAL)
662 return 1;
663 if (spec->nr && (spec->items[0].magic & ~PATHSPEC_LITERAL))
664 return 1;
666 return 0;
669 static void prepare_to_use_bloom_filter(struct rev_info *revs)
671 struct pathspec_item *pi;
672 char *path_alloc = NULL;
673 const char *path, *p;
674 size_t len;
675 int path_component_nr = 1;
677 if (!revs->commits)
678 return;
680 if (forbid_bloom_filters(&revs->prune_data))
681 return;
683 repo_parse_commit(revs->repo, revs->commits->item);
685 revs->bloom_filter_settings = get_bloom_filter_settings(revs->repo);
686 if (!revs->bloom_filter_settings)
687 return;
689 if (!revs->pruning.pathspec.nr)
690 return;
692 pi = &revs->pruning.pathspec.items[0];
694 /* remove single trailing slash from path, if needed */
695 if (pi->len > 0 && pi->match[pi->len - 1] == '/') {
696 path_alloc = xmemdupz(pi->match, pi->len - 1);
697 path = path_alloc;
698 } else
699 path = pi->match;
701 len = strlen(path);
702 if (!len) {
703 revs->bloom_filter_settings = NULL;
704 free(path_alloc);
705 return;
708 p = path;
709 while (*p) {
711 * At this point, the path is normalized to use Unix-style
712 * path separators. This is required due to how the
713 * changed-path Bloom filters store the paths.
715 if (*p == '/')
716 path_component_nr++;
717 p++;
720 revs->bloom_keys_nr = path_component_nr;
721 ALLOC_ARRAY(revs->bloom_keys, revs->bloom_keys_nr);
723 fill_bloom_key(path, len, &revs->bloom_keys[0],
724 revs->bloom_filter_settings);
725 path_component_nr = 1;
727 p = path + len - 1;
728 while (p > path) {
729 if (*p == '/')
730 fill_bloom_key(path, p - path,
731 &revs->bloom_keys[path_component_nr++],
732 revs->bloom_filter_settings);
733 p--;
736 if (trace2_is_enabled() && !bloom_filter_atexit_registered) {
737 atexit(trace2_bloom_filter_statistics_atexit);
738 bloom_filter_atexit_registered = 1;
741 free(path_alloc);
744 static int check_maybe_different_in_bloom_filter(struct rev_info *revs,
745 struct commit *commit)
747 struct bloom_filter *filter;
748 int result = 1, j;
750 if (!revs->repo->objects->commit_graph)
751 return -1;
753 if (commit_graph_generation(commit) == GENERATION_NUMBER_INFINITY)
754 return -1;
756 filter = get_bloom_filter(revs->repo, commit);
758 if (!filter) {
759 count_bloom_filter_not_present++;
760 return -1;
763 for (j = 0; result && j < revs->bloom_keys_nr; j++) {
764 result = bloom_filter_contains(filter,
765 &revs->bloom_keys[j],
766 revs->bloom_filter_settings);
769 if (result)
770 count_bloom_filter_maybe++;
771 else
772 count_bloom_filter_definitely_not++;
774 return result;
777 static int rev_compare_tree(struct rev_info *revs,
778 struct commit *parent, struct commit *commit, int nth_parent)
780 struct tree *t1 = get_commit_tree(parent);
781 struct tree *t2 = get_commit_tree(commit);
782 int bloom_ret = 1;
784 if (!t1)
785 return REV_TREE_NEW;
786 if (!t2)
787 return REV_TREE_OLD;
789 if (revs->simplify_by_decoration) {
791 * If we are simplifying by decoration, then the commit
792 * is worth showing if it has a tag pointing at it.
794 if (get_name_decoration(&commit->object))
795 return REV_TREE_DIFFERENT;
797 * A commit that is not pointed by a tag is uninteresting
798 * if we are not limited by path. This means that you will
799 * see the usual "commits that touch the paths" plus any
800 * tagged commit by specifying both --simplify-by-decoration
801 * and pathspec.
803 if (!revs->prune_data.nr)
804 return REV_TREE_SAME;
807 if (revs->bloom_keys_nr && !nth_parent) {
808 bloom_ret = check_maybe_different_in_bloom_filter(revs, commit);
810 if (bloom_ret == 0)
811 return REV_TREE_SAME;
814 tree_difference = REV_TREE_SAME;
815 revs->pruning.flags.has_changes = 0;
816 diff_tree_oid(&t1->object.oid, &t2->object.oid, "", &revs->pruning);
818 if (!nth_parent)
819 if (bloom_ret == 1 && tree_difference == REV_TREE_SAME)
820 count_bloom_filter_false_positive++;
822 return tree_difference;
825 static int rev_same_tree_as_empty(struct rev_info *revs, struct commit *commit)
827 struct tree *t1 = get_commit_tree(commit);
829 if (!t1)
830 return 0;
832 tree_difference = REV_TREE_SAME;
833 revs->pruning.flags.has_changes = 0;
834 diff_tree_oid(NULL, &t1->object.oid, "", &revs->pruning);
836 return tree_difference == REV_TREE_SAME;
839 struct treesame_state {
840 unsigned int nparents;
841 unsigned char treesame[FLEX_ARRAY];
844 static struct treesame_state *initialise_treesame(struct rev_info *revs, struct commit *commit)
846 unsigned n = commit_list_count(commit->parents);
847 struct treesame_state *st = xcalloc(1, st_add(sizeof(*st), n));
848 st->nparents = n;
849 add_decoration(&revs->treesame, &commit->object, st);
850 return st;
854 * Must be called immediately after removing the nth_parent from a commit's
855 * parent list, if we are maintaining the per-parent treesame[] decoration.
856 * This does not recalculate the master TREESAME flag - update_treesame()
857 * should be called to update it after a sequence of treesame[] modifications
858 * that may have affected it.
860 static int compact_treesame(struct rev_info *revs, struct commit *commit, unsigned nth_parent)
862 struct treesame_state *st;
863 int old_same;
865 if (!commit->parents) {
867 * Have just removed the only parent from a non-merge.
868 * Different handling, as we lack decoration.
870 if (nth_parent != 0)
871 die("compact_treesame %u", nth_parent);
872 old_same = !!(commit->object.flags & TREESAME);
873 if (rev_same_tree_as_empty(revs, commit))
874 commit->object.flags |= TREESAME;
875 else
876 commit->object.flags &= ~TREESAME;
877 return old_same;
880 st = lookup_decoration(&revs->treesame, &commit->object);
881 if (!st || nth_parent >= st->nparents)
882 die("compact_treesame %u", nth_parent);
884 old_same = st->treesame[nth_parent];
885 memmove(st->treesame + nth_parent,
886 st->treesame + nth_parent + 1,
887 st->nparents - nth_parent - 1);
890 * If we've just become a non-merge commit, update TREESAME
891 * immediately, and remove the no-longer-needed decoration.
892 * If still a merge, defer update until update_treesame().
894 if (--st->nparents == 1) {
895 if (commit->parents->next)
896 die("compact_treesame parents mismatch");
897 if (st->treesame[0] && revs->dense)
898 commit->object.flags |= TREESAME;
899 else
900 commit->object.flags &= ~TREESAME;
901 free(add_decoration(&revs->treesame, &commit->object, NULL));
904 return old_same;
907 static unsigned update_treesame(struct rev_info *revs, struct commit *commit)
909 if (commit->parents && commit->parents->next) {
910 unsigned n;
911 struct treesame_state *st;
912 struct commit_list *p;
913 unsigned relevant_parents;
914 unsigned relevant_change, irrelevant_change;
916 st = lookup_decoration(&revs->treesame, &commit->object);
917 if (!st)
918 die("update_treesame %s", oid_to_hex(&commit->object.oid));
919 relevant_parents = 0;
920 relevant_change = irrelevant_change = 0;
921 for (p = commit->parents, n = 0; p; n++, p = p->next) {
922 if (relevant_commit(p->item)) {
923 relevant_change |= !st->treesame[n];
924 relevant_parents++;
925 } else
926 irrelevant_change |= !st->treesame[n];
928 if (relevant_parents ? relevant_change : irrelevant_change)
929 commit->object.flags &= ~TREESAME;
930 else
931 commit->object.flags |= TREESAME;
934 return commit->object.flags & TREESAME;
937 static inline int limiting_can_increase_treesame(const struct rev_info *revs)
940 * TREESAME is irrelevant unless prune && dense;
941 * if simplify_history is set, we can't have a mixture of TREESAME and
942 * !TREESAME INTERESTING parents (and we don't have treesame[]
943 * decoration anyway);
944 * if first_parent_only is set, then the TREESAME flag is locked
945 * against the first parent (and again we lack treesame[] decoration).
947 return revs->prune && revs->dense &&
948 !revs->simplify_history &&
949 !revs->first_parent_only;
952 static void try_to_simplify_commit(struct rev_info *revs, struct commit *commit)
954 struct commit_list **pp, *parent;
955 struct treesame_state *ts = NULL;
956 int relevant_change = 0, irrelevant_change = 0;
957 int relevant_parents, nth_parent;
960 * If we don't do pruning, everything is interesting
962 if (!revs->prune)
963 return;
965 if (!get_commit_tree(commit))
966 return;
968 if (!commit->parents) {
969 if (rev_same_tree_as_empty(revs, commit))
970 commit->object.flags |= TREESAME;
971 return;
975 * Normal non-merge commit? If we don't want to make the
976 * history dense, we consider it always to be a change..
978 if (!revs->dense && !commit->parents->next)
979 return;
981 for (pp = &commit->parents, nth_parent = 0, relevant_parents = 0;
982 (parent = *pp) != NULL;
983 pp = &parent->next, nth_parent++) {
984 struct commit *p = parent->item;
985 if (relevant_commit(p))
986 relevant_parents++;
988 if (nth_parent == 1) {
990 * This our second loop iteration - so we now know
991 * we're dealing with a merge.
993 * Do not compare with later parents when we care only about
994 * the first parent chain, in order to avoid derailing the
995 * traversal to follow a side branch that brought everything
996 * in the path we are limited to by the pathspec.
998 if (revs->first_parent_only)
999 break;
1001 * If this will remain a potentially-simplifiable
1002 * merge, remember per-parent treesame if needed.
1003 * Initialise the array with the comparison from our
1004 * first iteration.
1006 if (revs->treesame.name &&
1007 !revs->simplify_history &&
1008 !(commit->object.flags & UNINTERESTING)) {
1009 ts = initialise_treesame(revs, commit);
1010 if (!(irrelevant_change || relevant_change))
1011 ts->treesame[0] = 1;
1014 if (repo_parse_commit(revs->repo, p) < 0)
1015 die("cannot simplify commit %s (because of %s)",
1016 oid_to_hex(&commit->object.oid),
1017 oid_to_hex(&p->object.oid));
1018 switch (rev_compare_tree(revs, p, commit, nth_parent)) {
1019 case REV_TREE_SAME:
1020 if (!revs->simplify_history || !relevant_commit(p)) {
1021 /* Even if a merge with an uninteresting
1022 * side branch brought the entire change
1023 * we are interested in, we do not want
1024 * to lose the other branches of this
1025 * merge, so we just keep going.
1027 if (ts)
1028 ts->treesame[nth_parent] = 1;
1029 continue;
1031 parent->next = NULL;
1032 commit->parents = parent;
1035 * A merge commit is a "diversion" if it is not
1036 * TREESAME to its first parent but is TREESAME
1037 * to a later parent. In the simplified history,
1038 * we "divert" the history walk to the later
1039 * parent. These commits are shown when "show_pulls"
1040 * is enabled, so do not mark the object as
1041 * TREESAME here.
1043 if (!revs->show_pulls || !nth_parent)
1044 commit->object.flags |= TREESAME;
1046 return;
1048 case REV_TREE_NEW:
1049 if (revs->remove_empty_trees &&
1050 rev_same_tree_as_empty(revs, p)) {
1051 /* We are adding all the specified
1052 * paths from this parent, so the
1053 * history beyond this parent is not
1054 * interesting. Remove its parents
1055 * (they are grandparents for us).
1056 * IOW, we pretend this parent is a
1057 * "root" commit.
1059 if (repo_parse_commit(revs->repo, p) < 0)
1060 die("cannot simplify commit %s (invalid %s)",
1061 oid_to_hex(&commit->object.oid),
1062 oid_to_hex(&p->object.oid));
1063 p->parents = NULL;
1065 /* fallthrough */
1066 case REV_TREE_OLD:
1067 case REV_TREE_DIFFERENT:
1068 if (relevant_commit(p))
1069 relevant_change = 1;
1070 else
1071 irrelevant_change = 1;
1073 if (!nth_parent)
1074 commit->object.flags |= PULL_MERGE;
1076 continue;
1078 die("bad tree compare for commit %s", oid_to_hex(&commit->object.oid));
1082 * TREESAME is straightforward for single-parent commits. For merge
1083 * commits, it is most useful to define it so that "irrelevant"
1084 * parents cannot make us !TREESAME - if we have any relevant
1085 * parents, then we only consider TREESAMEness with respect to them,
1086 * allowing irrelevant merges from uninteresting branches to be
1087 * simplified away. Only if we have only irrelevant parents do we
1088 * base TREESAME on them. Note that this logic is replicated in
1089 * update_treesame, which should be kept in sync.
1091 if (relevant_parents ? !relevant_change : !irrelevant_change)
1092 commit->object.flags |= TREESAME;
1095 static int process_parents(struct rev_info *revs, struct commit *commit,
1096 struct commit_list **list, struct prio_queue *queue)
1098 struct commit_list *parent = commit->parents;
1099 unsigned left_flag;
1101 if (commit->object.flags & ADDED)
1102 return 0;
1103 commit->object.flags |= ADDED;
1105 if (revs->include_check &&
1106 !revs->include_check(commit, revs->include_check_data))
1107 return 0;
1110 * If the commit is uninteresting, don't try to
1111 * prune parents - we want the maximal uninteresting
1112 * set.
1114 * Normally we haven't parsed the parent
1115 * yet, so we won't have a parent of a parent
1116 * here. However, it may turn out that we've
1117 * reached this commit some other way (where it
1118 * wasn't uninteresting), in which case we need
1119 * to mark its parents recursively too..
1121 if (commit->object.flags & UNINTERESTING) {
1122 while (parent) {
1123 struct commit *p = parent->item;
1124 parent = parent->next;
1125 if (p)
1126 p->object.flags |= UNINTERESTING;
1127 if (repo_parse_commit_gently(revs->repo, p, 1) < 0)
1128 continue;
1129 if (p->parents)
1130 mark_parents_uninteresting(p);
1131 if (p->object.flags & SEEN)
1132 continue;
1133 p->object.flags |= SEEN;
1134 if (list)
1135 commit_list_insert_by_date(p, list);
1136 if (queue)
1137 prio_queue_put(queue, p);
1139 return 0;
1143 * Ok, the commit wasn't uninteresting. Try to
1144 * simplify the commit history and find the parent
1145 * that has no differences in the path set if one exists.
1147 try_to_simplify_commit(revs, commit);
1149 if (revs->no_walk)
1150 return 0;
1152 left_flag = (commit->object.flags & SYMMETRIC_LEFT);
1154 for (parent = commit->parents; parent; parent = parent->next) {
1155 struct commit *p = parent->item;
1156 int gently = revs->ignore_missing_links ||
1157 revs->exclude_promisor_objects;
1158 if (repo_parse_commit_gently(revs->repo, p, gently) < 0) {
1159 if (revs->exclude_promisor_objects &&
1160 is_promisor_object(&p->object.oid)) {
1161 if (revs->first_parent_only)
1162 break;
1163 continue;
1165 return -1;
1167 if (revs->sources) {
1168 char **slot = revision_sources_at(revs->sources, p);
1170 if (!*slot)
1171 *slot = *revision_sources_at(revs->sources, commit);
1173 p->object.flags |= left_flag;
1174 if (!(p->object.flags & SEEN)) {
1175 p->object.flags |= SEEN;
1176 if (list)
1177 commit_list_insert_by_date(p, list);
1178 if (queue)
1179 prio_queue_put(queue, p);
1181 if (revs->first_parent_only)
1182 break;
1184 return 0;
1187 static void cherry_pick_list(struct commit_list *list, struct rev_info *revs)
1189 struct commit_list *p;
1190 int left_count = 0, right_count = 0;
1191 int left_first;
1192 struct patch_ids ids;
1193 unsigned cherry_flag;
1195 /* First count the commits on the left and on the right */
1196 for (p = list; p; p = p->next) {
1197 struct commit *commit = p->item;
1198 unsigned flags = commit->object.flags;
1199 if (flags & BOUNDARY)
1201 else if (flags & SYMMETRIC_LEFT)
1202 left_count++;
1203 else
1204 right_count++;
1207 if (!left_count || !right_count)
1208 return;
1210 left_first = left_count < right_count;
1211 init_patch_ids(revs->repo, &ids);
1212 ids.diffopts.pathspec = revs->diffopt.pathspec;
1214 /* Compute patch-ids for one side */
1215 for (p = list; p; p = p->next) {
1216 struct commit *commit = p->item;
1217 unsigned flags = commit->object.flags;
1219 if (flags & BOUNDARY)
1220 continue;
1222 * If we have fewer left, left_first is set and we omit
1223 * commits on the right branch in this loop. If we have
1224 * fewer right, we skip the left ones.
1226 if (left_first != !!(flags & SYMMETRIC_LEFT))
1227 continue;
1228 add_commit_patch_id(commit, &ids);
1231 /* either cherry_mark or cherry_pick are true */
1232 cherry_flag = revs->cherry_mark ? PATCHSAME : SHOWN;
1234 /* Check the other side */
1235 for (p = list; p; p = p->next) {
1236 struct commit *commit = p->item;
1237 struct patch_id *id;
1238 unsigned flags = commit->object.flags;
1240 if (flags & BOUNDARY)
1241 continue;
1243 * If we have fewer left, left_first is set and we omit
1244 * commits on the left branch in this loop.
1246 if (left_first == !!(flags & SYMMETRIC_LEFT))
1247 continue;
1250 * Have we seen the same patch id?
1252 id = has_commit_patch_id(commit, &ids);
1253 if (!id)
1254 continue;
1256 commit->object.flags |= cherry_flag;
1257 id->commit->object.flags |= cherry_flag;
1260 free_patch_ids(&ids);
1263 /* How many extra uninteresting commits we want to see.. */
1264 #define SLOP 5
1266 static int still_interesting(struct commit_list *src, timestamp_t date, int slop,
1267 struct commit **interesting_cache)
1270 * No source list at all? We're definitely done..
1272 if (!src)
1273 return 0;
1276 * Does the destination list contain entries with a date
1277 * before the source list? Definitely _not_ done.
1279 if (date <= src->item->date)
1280 return SLOP;
1283 * Does the source list still have interesting commits in
1284 * it? Definitely not done..
1286 if (!everybody_uninteresting(src, interesting_cache))
1287 return SLOP;
1289 /* Ok, we're closing in.. */
1290 return slop-1;
1294 * "rev-list --ancestry-path A..B" computes commits that are ancestors
1295 * of B but not ancestors of A but further limits the result to those
1296 * that are descendants of A. This takes the list of bottom commits and
1297 * the result of "A..B" without --ancestry-path, and limits the latter
1298 * further to the ones that can reach one of the commits in "bottom".
1300 static void limit_to_ancestry(struct commit_list *bottom, struct commit_list *list)
1302 struct commit_list *p;
1303 struct commit_list *rlist = NULL;
1304 int made_progress;
1307 * Reverse the list so that it will be likely that we would
1308 * process parents before children.
1310 for (p = list; p; p = p->next)
1311 commit_list_insert(p->item, &rlist);
1313 for (p = bottom; p; p = p->next)
1314 p->item->object.flags |= TMP_MARK;
1317 * Mark the ones that can reach bottom commits in "list",
1318 * in a bottom-up fashion.
1320 do {
1321 made_progress = 0;
1322 for (p = rlist; p; p = p->next) {
1323 struct commit *c = p->item;
1324 struct commit_list *parents;
1325 if (c->object.flags & (TMP_MARK | UNINTERESTING))
1326 continue;
1327 for (parents = c->parents;
1328 parents;
1329 parents = parents->next) {
1330 if (!(parents->item->object.flags & TMP_MARK))
1331 continue;
1332 c->object.flags |= TMP_MARK;
1333 made_progress = 1;
1334 break;
1337 } while (made_progress);
1340 * NEEDSWORK: decide if we want to remove parents that are
1341 * not marked with TMP_MARK from commit->parents for commits
1342 * in the resulting list. We may not want to do that, though.
1346 * The ones that are not marked with TMP_MARK are uninteresting
1348 for (p = list; p; p = p->next) {
1349 struct commit *c = p->item;
1350 if (c->object.flags & TMP_MARK)
1351 continue;
1352 c->object.flags |= UNINTERESTING;
1355 /* We are done with the TMP_MARK */
1356 for (p = list; p; p = p->next)
1357 p->item->object.flags &= ~TMP_MARK;
1358 for (p = bottom; p; p = p->next)
1359 p->item->object.flags &= ~TMP_MARK;
1360 free_commit_list(rlist);
1364 * Before walking the history, keep the set of "negative" refs the
1365 * caller has asked to exclude.
1367 * This is used to compute "rev-list --ancestry-path A..B", as we need
1368 * to filter the result of "A..B" further to the ones that can actually
1369 * reach A.
1371 static struct commit_list *collect_bottom_commits(struct commit_list *list)
1373 struct commit_list *elem, *bottom = NULL;
1374 for (elem = list; elem; elem = elem->next)
1375 if (elem->item->object.flags & BOTTOM)
1376 commit_list_insert(elem->item, &bottom);
1377 return bottom;
1380 /* Assumes either left_only or right_only is set */
1381 static void limit_left_right(struct commit_list *list, struct rev_info *revs)
1383 struct commit_list *p;
1385 for (p = list; p; p = p->next) {
1386 struct commit *commit = p->item;
1388 if (revs->right_only) {
1389 if (commit->object.flags & SYMMETRIC_LEFT)
1390 commit->object.flags |= SHOWN;
1391 } else /* revs->left_only is set */
1392 if (!(commit->object.flags & SYMMETRIC_LEFT))
1393 commit->object.flags |= SHOWN;
1397 static int limit_list(struct rev_info *revs)
1399 int slop = SLOP;
1400 timestamp_t date = TIME_MAX;
1401 struct commit_list *list = revs->commits;
1402 struct commit_list *newlist = NULL;
1403 struct commit_list **p = &newlist;
1404 struct commit_list *bottom = NULL;
1405 struct commit *interesting_cache = NULL;
1407 if (revs->ancestry_path) {
1408 bottom = collect_bottom_commits(list);
1409 if (!bottom)
1410 die("--ancestry-path given but there are no bottom commits");
1413 while (list) {
1414 struct commit *commit = pop_commit(&list);
1415 struct object *obj = &commit->object;
1416 show_early_output_fn_t show;
1418 if (commit == interesting_cache)
1419 interesting_cache = NULL;
1421 if (revs->max_age != -1 && (commit->date < revs->max_age))
1422 obj->flags |= UNINTERESTING;
1423 if (process_parents(revs, commit, &list, NULL) < 0)
1424 return -1;
1425 if (obj->flags & UNINTERESTING) {
1426 mark_parents_uninteresting(commit);
1427 slop = still_interesting(list, date, slop, &interesting_cache);
1428 if (slop)
1429 continue;
1430 break;
1432 if (revs->min_age != -1 && (commit->date > revs->min_age) &&
1433 !revs->line_level_traverse)
1434 continue;
1435 date = commit->date;
1436 p = &commit_list_insert(commit, p)->next;
1438 show = show_early_output;
1439 if (!show)
1440 continue;
1442 show(revs, newlist);
1443 show_early_output = NULL;
1445 if (revs->cherry_pick || revs->cherry_mark)
1446 cherry_pick_list(newlist, revs);
1448 if (revs->left_only || revs->right_only)
1449 limit_left_right(newlist, revs);
1451 if (bottom) {
1452 limit_to_ancestry(bottom, newlist);
1453 free_commit_list(bottom);
1457 * Check if any commits have become TREESAME by some of their parents
1458 * becoming UNINTERESTING.
1460 if (limiting_can_increase_treesame(revs))
1461 for (list = newlist; list; list = list->next) {
1462 struct commit *c = list->item;
1463 if (c->object.flags & (UNINTERESTING | TREESAME))
1464 continue;
1465 update_treesame(revs, c);
1468 revs->commits = newlist;
1469 return 0;
1473 * Add an entry to refs->cmdline with the specified information.
1474 * *name is copied.
1476 static void add_rev_cmdline(struct rev_info *revs,
1477 struct object *item,
1478 const char *name,
1479 int whence,
1480 unsigned flags)
1482 struct rev_cmdline_info *info = &revs->cmdline;
1483 unsigned int nr = info->nr;
1485 ALLOC_GROW(info->rev, nr + 1, info->alloc);
1486 info->rev[nr].item = item;
1487 info->rev[nr].name = xstrdup(name);
1488 info->rev[nr].whence = whence;
1489 info->rev[nr].flags = flags;
1490 info->nr++;
1493 static void add_rev_cmdline_list(struct rev_info *revs,
1494 struct commit_list *commit_list,
1495 int whence,
1496 unsigned flags)
1498 while (commit_list) {
1499 struct object *object = &commit_list->item->object;
1500 add_rev_cmdline(revs, object, oid_to_hex(&object->oid),
1501 whence, flags);
1502 commit_list = commit_list->next;
1506 struct all_refs_cb {
1507 int all_flags;
1508 int warned_bad_reflog;
1509 struct rev_info *all_revs;
1510 const char *name_for_errormsg;
1511 struct worktree *wt;
1514 int ref_excluded(struct string_list *ref_excludes, const char *path)
1516 struct string_list_item *item;
1518 if (!ref_excludes)
1519 return 0;
1520 for_each_string_list_item(item, ref_excludes) {
1521 if (!wildmatch(item->string, path, 0))
1522 return 1;
1524 return 0;
1527 static int handle_one_ref(const char *path, const struct object_id *oid,
1528 int flag, void *cb_data)
1530 struct all_refs_cb *cb = cb_data;
1531 struct object *object;
1533 if (ref_excluded(cb->all_revs->ref_excludes, path))
1534 return 0;
1536 object = get_reference(cb->all_revs, path, oid, cb->all_flags);
1537 add_rev_cmdline(cb->all_revs, object, path, REV_CMD_REF, cb->all_flags);
1538 add_pending_oid(cb->all_revs, path, oid, cb->all_flags);
1539 return 0;
1542 static void init_all_refs_cb(struct all_refs_cb *cb, struct rev_info *revs,
1543 unsigned flags)
1545 cb->all_revs = revs;
1546 cb->all_flags = flags;
1547 revs->rev_input_given = 1;
1548 cb->wt = NULL;
1551 void clear_ref_exclusion(struct string_list **ref_excludes_p)
1553 if (*ref_excludes_p) {
1554 string_list_clear(*ref_excludes_p, 0);
1555 free(*ref_excludes_p);
1557 *ref_excludes_p = NULL;
1560 void add_ref_exclusion(struct string_list **ref_excludes_p, const char *exclude)
1562 if (!*ref_excludes_p) {
1563 *ref_excludes_p = xcalloc(1, sizeof(**ref_excludes_p));
1564 (*ref_excludes_p)->strdup_strings = 1;
1566 string_list_append(*ref_excludes_p, exclude);
1569 static void handle_refs(struct ref_store *refs,
1570 struct rev_info *revs, unsigned flags,
1571 int (*for_each)(struct ref_store *, each_ref_fn, void *))
1573 struct all_refs_cb cb;
1575 if (!refs) {
1576 /* this could happen with uninitialized submodules */
1577 return;
1580 init_all_refs_cb(&cb, revs, flags);
1581 for_each(refs, handle_one_ref, &cb);
1584 static void handle_one_reflog_commit(struct object_id *oid, void *cb_data)
1586 struct all_refs_cb *cb = cb_data;
1587 if (!is_null_oid(oid)) {
1588 struct object *o = parse_object(cb->all_revs->repo, oid);
1589 if (o) {
1590 o->flags |= cb->all_flags;
1591 /* ??? CMDLINEFLAGS ??? */
1592 add_pending_object(cb->all_revs, o, "");
1594 else if (!cb->warned_bad_reflog) {
1595 warning("reflog of '%s' references pruned commits",
1596 cb->name_for_errormsg);
1597 cb->warned_bad_reflog = 1;
1602 static int handle_one_reflog_ent(struct object_id *ooid, struct object_id *noid,
1603 const char *email, timestamp_t timestamp, int tz,
1604 const char *message, void *cb_data)
1606 handle_one_reflog_commit(ooid, cb_data);
1607 handle_one_reflog_commit(noid, cb_data);
1608 return 0;
1611 static int handle_one_reflog(const char *refname_in_wt,
1612 const struct object_id *oid,
1613 int flag, void *cb_data)
1615 struct all_refs_cb *cb = cb_data;
1616 struct strbuf refname = STRBUF_INIT;
1618 cb->warned_bad_reflog = 0;
1619 strbuf_worktree_ref(cb->wt, &refname, refname_in_wt);
1620 cb->name_for_errormsg = refname.buf;
1621 refs_for_each_reflog_ent(get_main_ref_store(the_repository),
1622 refname.buf,
1623 handle_one_reflog_ent, cb_data);
1624 strbuf_release(&refname);
1625 return 0;
1628 static void add_other_reflogs_to_pending(struct all_refs_cb *cb)
1630 struct worktree **worktrees, **p;
1632 worktrees = get_worktrees();
1633 for (p = worktrees; *p; p++) {
1634 struct worktree *wt = *p;
1636 if (wt->is_current)
1637 continue;
1639 cb->wt = wt;
1640 refs_for_each_reflog(get_worktree_ref_store(wt),
1641 handle_one_reflog,
1642 cb);
1644 free_worktrees(worktrees);
1647 void add_reflogs_to_pending(struct rev_info *revs, unsigned flags)
1649 struct all_refs_cb cb;
1651 cb.all_revs = revs;
1652 cb.all_flags = flags;
1653 cb.wt = NULL;
1654 for_each_reflog(handle_one_reflog, &cb);
1656 if (!revs->single_worktree)
1657 add_other_reflogs_to_pending(&cb);
1660 static void add_cache_tree(struct cache_tree *it, struct rev_info *revs,
1661 struct strbuf *path, unsigned int flags)
1663 size_t baselen = path->len;
1664 int i;
1666 if (it->entry_count >= 0) {
1667 struct tree *tree = lookup_tree(revs->repo, &it->oid);
1668 tree->object.flags |= flags;
1669 add_pending_object_with_path(revs, &tree->object, "",
1670 040000, path->buf);
1673 for (i = 0; i < it->subtree_nr; i++) {
1674 struct cache_tree_sub *sub = it->down[i];
1675 strbuf_addf(path, "%s%s", baselen ? "/" : "", sub->name);
1676 add_cache_tree(sub->cache_tree, revs, path, flags);
1677 strbuf_setlen(path, baselen);
1682 static void do_add_index_objects_to_pending(struct rev_info *revs,
1683 struct index_state *istate,
1684 unsigned int flags)
1686 int i;
1688 for (i = 0; i < istate->cache_nr; i++) {
1689 struct cache_entry *ce = istate->cache[i];
1690 struct blob *blob;
1692 if (S_ISGITLINK(ce->ce_mode))
1693 continue;
1695 blob = lookup_blob(revs->repo, &ce->oid);
1696 if (!blob)
1697 die("unable to add index blob to traversal");
1698 blob->object.flags |= flags;
1699 add_pending_object_with_path(revs, &blob->object, "",
1700 ce->ce_mode, ce->name);
1703 if (istate->cache_tree) {
1704 struct strbuf path = STRBUF_INIT;
1705 add_cache_tree(istate->cache_tree, revs, &path, flags);
1706 strbuf_release(&path);
1710 void add_index_objects_to_pending(struct rev_info *revs, unsigned int flags)
1712 struct worktree **worktrees, **p;
1714 repo_read_index(revs->repo);
1715 do_add_index_objects_to_pending(revs, revs->repo->index, flags);
1717 if (revs->single_worktree)
1718 return;
1720 worktrees = get_worktrees();
1721 for (p = worktrees; *p; p++) {
1722 struct worktree *wt = *p;
1723 struct index_state istate = { NULL };
1725 if (wt->is_current)
1726 continue; /* current index already taken care of */
1728 if (read_index_from(&istate,
1729 worktree_git_path(wt, "index"),
1730 get_worktree_git_dir(wt)) > 0)
1731 do_add_index_objects_to_pending(revs, &istate, flags);
1732 discard_index(&istate);
1734 free_worktrees(worktrees);
1737 struct add_alternate_refs_data {
1738 struct rev_info *revs;
1739 unsigned int flags;
1742 static void add_one_alternate_ref(const struct object_id *oid,
1743 void *vdata)
1745 const char *name = ".alternate";
1746 struct add_alternate_refs_data *data = vdata;
1747 struct object *obj;
1749 obj = get_reference(data->revs, name, oid, data->flags);
1750 add_rev_cmdline(data->revs, obj, name, REV_CMD_REV, data->flags);
1751 add_pending_object(data->revs, obj, name);
1754 static void add_alternate_refs_to_pending(struct rev_info *revs,
1755 unsigned int flags)
1757 struct add_alternate_refs_data data;
1758 data.revs = revs;
1759 data.flags = flags;
1760 for_each_alternate_ref(add_one_alternate_ref, &data);
1763 static int add_parents_only(struct rev_info *revs, const char *arg_, int flags,
1764 int exclude_parent)
1766 struct object_id oid;
1767 struct object *it;
1768 struct commit *commit;
1769 struct commit_list *parents;
1770 int parent_number;
1771 const char *arg = arg_;
1773 if (*arg == '^') {
1774 flags ^= UNINTERESTING | BOTTOM;
1775 arg++;
1777 if (get_oid_committish(arg, &oid))
1778 return 0;
1779 while (1) {
1780 it = get_reference(revs, arg, &oid, 0);
1781 if (!it && revs->ignore_missing)
1782 return 0;
1783 if (it->type != OBJ_TAG)
1784 break;
1785 if (!((struct tag*)it)->tagged)
1786 return 0;
1787 oidcpy(&oid, &((struct tag*)it)->tagged->oid);
1789 if (it->type != OBJ_COMMIT)
1790 return 0;
1791 commit = (struct commit *)it;
1792 if (exclude_parent &&
1793 exclude_parent > commit_list_count(commit->parents))
1794 return 0;
1795 for (parents = commit->parents, parent_number = 1;
1796 parents;
1797 parents = parents->next, parent_number++) {
1798 if (exclude_parent && parent_number != exclude_parent)
1799 continue;
1801 it = &parents->item->object;
1802 it->flags |= flags;
1803 add_rev_cmdline(revs, it, arg_, REV_CMD_PARENTS_ONLY, flags);
1804 add_pending_object(revs, it, arg);
1806 return 1;
1809 void repo_init_revisions(struct repository *r,
1810 struct rev_info *revs,
1811 const char *prefix)
1813 memset(revs, 0, sizeof(*revs));
1815 revs->repo = r;
1816 revs->abbrev = DEFAULT_ABBREV;
1817 diff_merges_init_revs(revs);
1818 revs->simplify_history = 1;
1819 revs->pruning.repo = r;
1820 revs->pruning.flags.recursive = 1;
1821 revs->pruning.flags.quick = 1;
1822 revs->pruning.add_remove = file_add_remove;
1823 revs->pruning.change = file_change;
1824 revs->pruning.change_fn_data = revs;
1825 revs->sort_order = REV_SORT_IN_GRAPH_ORDER;
1826 revs->dense = 1;
1827 revs->prefix = prefix;
1828 revs->max_age = -1;
1829 revs->min_age = -1;
1830 revs->skip_count = -1;
1831 revs->max_count = -1;
1832 revs->max_parents = -1;
1833 revs->expand_tabs_in_log = -1;
1835 revs->commit_format = CMIT_FMT_DEFAULT;
1836 revs->expand_tabs_in_log_default = 8;
1838 init_grep_defaults(revs->repo);
1839 grep_init(&revs->grep_filter, revs->repo, prefix);
1840 revs->grep_filter.status_only = 1;
1842 repo_diff_setup(revs->repo, &revs->diffopt);
1843 if (prefix && !revs->diffopt.prefix) {
1844 revs->diffopt.prefix = prefix;
1845 revs->diffopt.prefix_length = strlen(prefix);
1848 init_display_notes(&revs->notes_opt);
1851 static void add_pending_commit_list(struct rev_info *revs,
1852 struct commit_list *commit_list,
1853 unsigned int flags)
1855 while (commit_list) {
1856 struct object *object = &commit_list->item->object;
1857 object->flags |= flags;
1858 add_pending_object(revs, object, oid_to_hex(&object->oid));
1859 commit_list = commit_list->next;
1863 static void prepare_show_merge(struct rev_info *revs)
1865 struct commit_list *bases;
1866 struct commit *head, *other;
1867 struct object_id oid;
1868 const char **prune = NULL;
1869 int i, prune_num = 1; /* counting terminating NULL */
1870 struct index_state *istate = revs->repo->index;
1872 if (get_oid("HEAD", &oid))
1873 die("--merge without HEAD?");
1874 head = lookup_commit_or_die(&oid, "HEAD");
1875 if (get_oid("MERGE_HEAD", &oid))
1876 die("--merge without MERGE_HEAD?");
1877 other = lookup_commit_or_die(&oid, "MERGE_HEAD");
1878 add_pending_object(revs, &head->object, "HEAD");
1879 add_pending_object(revs, &other->object, "MERGE_HEAD");
1880 bases = get_merge_bases(head, other);
1881 add_rev_cmdline_list(revs, bases, REV_CMD_MERGE_BASE, UNINTERESTING | BOTTOM);
1882 add_pending_commit_list(revs, bases, UNINTERESTING | BOTTOM);
1883 free_commit_list(bases);
1884 head->object.flags |= SYMMETRIC_LEFT;
1886 if (!istate->cache_nr)
1887 repo_read_index(revs->repo);
1888 for (i = 0; i < istate->cache_nr; i++) {
1889 const struct cache_entry *ce = istate->cache[i];
1890 if (!ce_stage(ce))
1891 continue;
1892 if (ce_path_match(istate, ce, &revs->prune_data, NULL)) {
1893 prune_num++;
1894 REALLOC_ARRAY(prune, prune_num);
1895 prune[prune_num-2] = ce->name;
1896 prune[prune_num-1] = NULL;
1898 while ((i+1 < istate->cache_nr) &&
1899 ce_same_name(ce, istate->cache[i+1]))
1900 i++;
1902 clear_pathspec(&revs->prune_data);
1903 parse_pathspec(&revs->prune_data, PATHSPEC_ALL_MAGIC & ~PATHSPEC_LITERAL,
1904 PATHSPEC_PREFER_FULL | PATHSPEC_LITERAL_PATH, "", prune);
1905 revs->limited = 1;
1908 static int dotdot_missing(const char *arg, char *dotdot,
1909 struct rev_info *revs, int symmetric)
1911 if (revs->ignore_missing)
1912 return 0;
1913 /* de-munge so we report the full argument */
1914 *dotdot = '.';
1915 die(symmetric
1916 ? "Invalid symmetric difference expression %s"
1917 : "Invalid revision range %s", arg);
1920 static int handle_dotdot_1(const char *arg, char *dotdot,
1921 struct rev_info *revs, int flags,
1922 int cant_be_filename,
1923 struct object_context *a_oc,
1924 struct object_context *b_oc)
1926 const char *a_name, *b_name;
1927 struct object_id a_oid, b_oid;
1928 struct object *a_obj, *b_obj;
1929 unsigned int a_flags, b_flags;
1930 int symmetric = 0;
1931 unsigned int flags_exclude = flags ^ (UNINTERESTING | BOTTOM);
1932 unsigned int oc_flags = GET_OID_COMMITTISH | GET_OID_RECORD_PATH;
1934 a_name = arg;
1935 if (!*a_name)
1936 a_name = "HEAD";
1938 b_name = dotdot + 2;
1939 if (*b_name == '.') {
1940 symmetric = 1;
1941 b_name++;
1943 if (!*b_name)
1944 b_name = "HEAD";
1946 if (get_oid_with_context(revs->repo, a_name, oc_flags, &a_oid, a_oc) ||
1947 get_oid_with_context(revs->repo, b_name, oc_flags, &b_oid, b_oc))
1948 return -1;
1950 if (!cant_be_filename) {
1951 *dotdot = '.';
1952 verify_non_filename(revs->prefix, arg);
1953 *dotdot = '\0';
1956 a_obj = parse_object(revs->repo, &a_oid);
1957 b_obj = parse_object(revs->repo, &b_oid);
1958 if (!a_obj || !b_obj)
1959 return dotdot_missing(arg, dotdot, revs, symmetric);
1961 if (!symmetric) {
1962 /* just A..B */
1963 b_flags = flags;
1964 a_flags = flags_exclude;
1965 } else {
1966 /* A...B -- find merge bases between the two */
1967 struct commit *a, *b;
1968 struct commit_list *exclude;
1970 a = lookup_commit_reference(revs->repo, &a_obj->oid);
1971 b = lookup_commit_reference(revs->repo, &b_obj->oid);
1972 if (!a || !b)
1973 return dotdot_missing(arg, dotdot, revs, symmetric);
1975 exclude = get_merge_bases(a, b);
1976 add_rev_cmdline_list(revs, exclude, REV_CMD_MERGE_BASE,
1977 flags_exclude);
1978 add_pending_commit_list(revs, exclude, flags_exclude);
1979 free_commit_list(exclude);
1981 b_flags = flags;
1982 a_flags = flags | SYMMETRIC_LEFT;
1985 a_obj->flags |= a_flags;
1986 b_obj->flags |= b_flags;
1987 add_rev_cmdline(revs, a_obj, a_name, REV_CMD_LEFT, a_flags);
1988 add_rev_cmdline(revs, b_obj, b_name, REV_CMD_RIGHT, b_flags);
1989 add_pending_object_with_path(revs, a_obj, a_name, a_oc->mode, a_oc->path);
1990 add_pending_object_with_path(revs, b_obj, b_name, b_oc->mode, b_oc->path);
1991 return 0;
1994 static int handle_dotdot(const char *arg,
1995 struct rev_info *revs, int flags,
1996 int cant_be_filename)
1998 struct object_context a_oc, b_oc;
1999 char *dotdot = strstr(arg, "..");
2000 int ret;
2002 if (!dotdot)
2003 return -1;
2005 memset(&a_oc, 0, sizeof(a_oc));
2006 memset(&b_oc, 0, sizeof(b_oc));
2008 *dotdot = '\0';
2009 ret = handle_dotdot_1(arg, dotdot, revs, flags, cant_be_filename,
2010 &a_oc, &b_oc);
2011 *dotdot = '.';
2013 free(a_oc.path);
2014 free(b_oc.path);
2016 return ret;
2019 static int handle_revision_arg_1(const char *arg_, struct rev_info *revs, int flags, unsigned revarg_opt)
2021 struct object_context oc;
2022 char *mark;
2023 struct object *object;
2024 struct object_id oid;
2025 int local_flags;
2026 const char *arg = arg_;
2027 int cant_be_filename = revarg_opt & REVARG_CANNOT_BE_FILENAME;
2028 unsigned get_sha1_flags = GET_OID_RECORD_PATH;
2030 flags = flags & UNINTERESTING ? flags | BOTTOM : flags & ~BOTTOM;
2032 if (!cant_be_filename && !strcmp(arg, "..")) {
2034 * Just ".."? That is not a range but the
2035 * pathspec for the parent directory.
2037 return -1;
2040 if (!handle_dotdot(arg, revs, flags, revarg_opt))
2041 return 0;
2043 mark = strstr(arg, "^@");
2044 if (mark && !mark[2]) {
2045 *mark = 0;
2046 if (add_parents_only(revs, arg, flags, 0))
2047 return 0;
2048 *mark = '^';
2050 mark = strstr(arg, "^!");
2051 if (mark && !mark[2]) {
2052 *mark = 0;
2053 if (!add_parents_only(revs, arg, flags ^ (UNINTERESTING | BOTTOM), 0))
2054 *mark = '^';
2056 mark = strstr(arg, "^-");
2057 if (mark) {
2058 int exclude_parent = 1;
2060 if (mark[2]) {
2061 char *end;
2062 exclude_parent = strtoul(mark + 2, &end, 10);
2063 if (*end != '\0' || !exclude_parent)
2064 return -1;
2067 *mark = 0;
2068 if (!add_parents_only(revs, arg, flags ^ (UNINTERESTING | BOTTOM), exclude_parent))
2069 *mark = '^';
2072 local_flags = 0;
2073 if (*arg == '^') {
2074 local_flags = UNINTERESTING | BOTTOM;
2075 arg++;
2078 if (revarg_opt & REVARG_COMMITTISH)
2079 get_sha1_flags |= GET_OID_COMMITTISH;
2081 if (get_oid_with_context(revs->repo, arg, get_sha1_flags, &oid, &oc))
2082 return revs->ignore_missing ? 0 : -1;
2083 if (!cant_be_filename)
2084 verify_non_filename(revs->prefix, arg);
2085 object = get_reference(revs, arg, &oid, flags ^ local_flags);
2086 if (!object)
2087 return revs->ignore_missing ? 0 : -1;
2088 add_rev_cmdline(revs, object, arg_, REV_CMD_REV, flags ^ local_flags);
2089 add_pending_object_with_path(revs, object, arg, oc.mode, oc.path);
2090 free(oc.path);
2091 return 0;
2094 int handle_revision_arg(const char *arg, struct rev_info *revs, int flags, unsigned revarg_opt)
2096 int ret = handle_revision_arg_1(arg, revs, flags, revarg_opt);
2097 if (!ret)
2098 revs->rev_input_given = 1;
2099 return ret;
2102 static void read_pathspec_from_stdin(struct strbuf *sb,
2103 struct strvec *prune)
2105 while (strbuf_getline(sb, stdin) != EOF)
2106 strvec_push(prune, sb->buf);
2109 static void read_revisions_from_stdin(struct rev_info *revs,
2110 struct strvec *prune)
2112 struct strbuf sb;
2113 int seen_dashdash = 0;
2114 int save_warning;
2116 save_warning = warn_on_object_refname_ambiguity;
2117 warn_on_object_refname_ambiguity = 0;
2119 strbuf_init(&sb, 1000);
2120 while (strbuf_getline(&sb, stdin) != EOF) {
2121 int len = sb.len;
2122 if (!len)
2123 break;
2124 if (sb.buf[0] == '-') {
2125 if (len == 2 && sb.buf[1] == '-') {
2126 seen_dashdash = 1;
2127 break;
2129 die("options not supported in --stdin mode");
2131 if (handle_revision_arg(sb.buf, revs, 0,
2132 REVARG_CANNOT_BE_FILENAME))
2133 die("bad revision '%s'", sb.buf);
2135 if (seen_dashdash)
2136 read_pathspec_from_stdin(&sb, prune);
2138 strbuf_release(&sb);
2139 warn_on_object_refname_ambiguity = save_warning;
2142 static void add_grep(struct rev_info *revs, const char *ptn, enum grep_pat_token what)
2144 append_grep_pattern(&revs->grep_filter, ptn, "command line", 0, what);
2147 static void add_header_grep(struct rev_info *revs, enum grep_header_field field, const char *pattern)
2149 append_header_grep_pattern(&revs->grep_filter, field, pattern);
2152 static void add_message_grep(struct rev_info *revs, const char *pattern)
2154 add_grep(revs, pattern, GREP_PATTERN_BODY);
2157 static int handle_revision_opt(struct rev_info *revs, int argc, const char **argv,
2158 int *unkc, const char **unkv,
2159 const struct setup_revision_opt* opt)
2161 const char *arg = argv[0];
2162 const char *optarg;
2163 int argcount;
2164 const unsigned hexsz = the_hash_algo->hexsz;
2166 /* pseudo revision arguments */
2167 if (!strcmp(arg, "--all") || !strcmp(arg, "--branches") ||
2168 !strcmp(arg, "--tags") || !strcmp(arg, "--remotes") ||
2169 !strcmp(arg, "--reflog") || !strcmp(arg, "--not") ||
2170 !strcmp(arg, "--no-walk") || !strcmp(arg, "--do-walk") ||
2171 !strcmp(arg, "--bisect") || starts_with(arg, "--glob=") ||
2172 !strcmp(arg, "--indexed-objects") ||
2173 !strcmp(arg, "--alternate-refs") ||
2174 starts_with(arg, "--exclude=") ||
2175 starts_with(arg, "--branches=") || starts_with(arg, "--tags=") ||
2176 starts_with(arg, "--remotes=") || starts_with(arg, "--no-walk="))
2178 unkv[(*unkc)++] = arg;
2179 return 1;
2182 if ((argcount = parse_long_opt("max-count", argv, &optarg))) {
2183 revs->max_count = atoi(optarg);
2184 revs->no_walk = 0;
2185 return argcount;
2186 } else if ((argcount = parse_long_opt("skip", argv, &optarg))) {
2187 revs->skip_count = atoi(optarg);
2188 return argcount;
2189 } else if ((*arg == '-') && isdigit(arg[1])) {
2190 /* accept -<digit>, like traditional "head" */
2191 if (strtol_i(arg + 1, 10, &revs->max_count) < 0 ||
2192 revs->max_count < 0)
2193 die("'%s': not a non-negative integer", arg + 1);
2194 revs->no_walk = 0;
2195 } else if (!strcmp(arg, "-n")) {
2196 if (argc <= 1)
2197 return error("-n requires an argument");
2198 revs->max_count = atoi(argv[1]);
2199 revs->no_walk = 0;
2200 return 2;
2201 } else if (skip_prefix(arg, "-n", &optarg)) {
2202 revs->max_count = atoi(optarg);
2203 revs->no_walk = 0;
2204 } else if ((argcount = parse_long_opt("max-age", argv, &optarg))) {
2205 revs->max_age = atoi(optarg);
2206 return argcount;
2207 } else if ((argcount = parse_long_opt("since", argv, &optarg))) {
2208 revs->max_age = approxidate(optarg);
2209 return argcount;
2210 } else if ((argcount = parse_long_opt("after", argv, &optarg))) {
2211 revs->max_age = approxidate(optarg);
2212 return argcount;
2213 } else if ((argcount = parse_long_opt("min-age", argv, &optarg))) {
2214 revs->min_age = atoi(optarg);
2215 return argcount;
2216 } else if ((argcount = parse_long_opt("before", argv, &optarg))) {
2217 revs->min_age = approxidate(optarg);
2218 return argcount;
2219 } else if ((argcount = parse_long_opt("until", argv, &optarg))) {
2220 revs->min_age = approxidate(optarg);
2221 return argcount;
2222 } else if (!strcmp(arg, "--first-parent")) {
2223 revs->first_parent_only = 1;
2224 } else if (!strcmp(arg, "--ancestry-path")) {
2225 revs->ancestry_path = 1;
2226 revs->simplify_history = 0;
2227 revs->limited = 1;
2228 } else if (!strcmp(arg, "-g") || !strcmp(arg, "--walk-reflogs")) {
2229 init_reflog_walk(&revs->reflog_info);
2230 } else if (!strcmp(arg, "--default")) {
2231 if (argc <= 1)
2232 return error("bad --default argument");
2233 revs->def = argv[1];
2234 return 2;
2235 } else if (!strcmp(arg, "--merge")) {
2236 revs->show_merge = 1;
2237 } else if (!strcmp(arg, "--topo-order")) {
2238 revs->sort_order = REV_SORT_IN_GRAPH_ORDER;
2239 revs->topo_order = 1;
2240 } else if (!strcmp(arg, "--simplify-merges")) {
2241 revs->simplify_merges = 1;
2242 revs->topo_order = 1;
2243 revs->rewrite_parents = 1;
2244 revs->simplify_history = 0;
2245 revs->limited = 1;
2246 } else if (!strcmp(arg, "--simplify-by-decoration")) {
2247 revs->simplify_merges = 1;
2248 revs->topo_order = 1;
2249 revs->rewrite_parents = 1;
2250 revs->simplify_history = 0;
2251 revs->simplify_by_decoration = 1;
2252 revs->limited = 1;
2253 revs->prune = 1;
2254 } else if (!strcmp(arg, "--date-order")) {
2255 revs->sort_order = REV_SORT_BY_COMMIT_DATE;
2256 revs->topo_order = 1;
2257 } else if (!strcmp(arg, "--author-date-order")) {
2258 revs->sort_order = REV_SORT_BY_AUTHOR_DATE;
2259 revs->topo_order = 1;
2260 } else if (!strcmp(arg, "--early-output")) {
2261 revs->early_output = 100;
2262 revs->topo_order = 1;
2263 } else if (skip_prefix(arg, "--early-output=", &optarg)) {
2264 if (strtoul_ui(optarg, 10, &revs->early_output) < 0)
2265 die("'%s': not a non-negative integer", optarg);
2266 revs->topo_order = 1;
2267 } else if (!strcmp(arg, "--parents")) {
2268 revs->rewrite_parents = 1;
2269 revs->print_parents = 1;
2270 } else if (!strcmp(arg, "--dense")) {
2271 revs->dense = 1;
2272 } else if (!strcmp(arg, "--sparse")) {
2273 revs->dense = 0;
2274 } else if (!strcmp(arg, "--in-commit-order")) {
2275 revs->tree_blobs_in_commit_order = 1;
2276 } else if (!strcmp(arg, "--remove-empty")) {
2277 revs->remove_empty_trees = 1;
2278 } else if (!strcmp(arg, "--merges")) {
2279 revs->min_parents = 2;
2280 } else if (!strcmp(arg, "--no-merges")) {
2281 revs->max_parents = 1;
2282 } else if (skip_prefix(arg, "--min-parents=", &optarg)) {
2283 revs->min_parents = atoi(optarg);
2284 } else if (!strcmp(arg, "--no-min-parents")) {
2285 revs->min_parents = 0;
2286 } else if (skip_prefix(arg, "--max-parents=", &optarg)) {
2287 revs->max_parents = atoi(optarg);
2288 } else if (!strcmp(arg, "--no-max-parents")) {
2289 revs->max_parents = -1;
2290 } else if (!strcmp(arg, "--boundary")) {
2291 revs->boundary = 1;
2292 } else if (!strcmp(arg, "--left-right")) {
2293 revs->left_right = 1;
2294 } else if (!strcmp(arg, "--left-only")) {
2295 if (revs->right_only)
2296 die("--left-only is incompatible with --right-only"
2297 " or --cherry");
2298 revs->left_only = 1;
2299 } else if (!strcmp(arg, "--right-only")) {
2300 if (revs->left_only)
2301 die("--right-only is incompatible with --left-only");
2302 revs->right_only = 1;
2303 } else if (!strcmp(arg, "--cherry")) {
2304 if (revs->left_only)
2305 die("--cherry is incompatible with --left-only");
2306 revs->cherry_mark = 1;
2307 revs->right_only = 1;
2308 revs->max_parents = 1;
2309 revs->limited = 1;
2310 } else if (!strcmp(arg, "--count")) {
2311 revs->count = 1;
2312 } else if (!strcmp(arg, "--cherry-mark")) {
2313 if (revs->cherry_pick)
2314 die("--cherry-mark is incompatible with --cherry-pick");
2315 revs->cherry_mark = 1;
2316 revs->limited = 1; /* needs limit_list() */
2317 } else if (!strcmp(arg, "--cherry-pick")) {
2318 if (revs->cherry_mark)
2319 die("--cherry-pick is incompatible with --cherry-mark");
2320 revs->cherry_pick = 1;
2321 revs->limited = 1;
2322 } else if (!strcmp(arg, "--objects")) {
2323 revs->tag_objects = 1;
2324 revs->tree_objects = 1;
2325 revs->blob_objects = 1;
2326 } else if (!strcmp(arg, "--objects-edge")) {
2327 revs->tag_objects = 1;
2328 revs->tree_objects = 1;
2329 revs->blob_objects = 1;
2330 revs->edge_hint = 1;
2331 } else if (!strcmp(arg, "--objects-edge-aggressive")) {
2332 revs->tag_objects = 1;
2333 revs->tree_objects = 1;
2334 revs->blob_objects = 1;
2335 revs->edge_hint = 1;
2336 revs->edge_hint_aggressive = 1;
2337 } else if (!strcmp(arg, "--verify-objects")) {
2338 revs->tag_objects = 1;
2339 revs->tree_objects = 1;
2340 revs->blob_objects = 1;
2341 revs->verify_objects = 1;
2342 } else if (!strcmp(arg, "--unpacked")) {
2343 revs->unpacked = 1;
2344 } else if (starts_with(arg, "--unpacked=")) {
2345 die(_("--unpacked=<packfile> no longer supported"));
2346 } else if (!strcmp(arg, "-r")) {
2347 revs->diff = 1;
2348 revs->diffopt.flags.recursive = 1;
2349 } else if (!strcmp(arg, "-t")) {
2350 revs->diff = 1;
2351 revs->diffopt.flags.recursive = 1;
2352 revs->diffopt.flags.tree_in_recursive = 1;
2353 } else if ((argcount = diff_merges_parse_opts(revs, argv))) {
2354 return argcount;
2355 } else if (!strcmp(arg, "-v")) {
2356 revs->verbose_header = 1;
2357 } else if (!strcmp(arg, "--pretty")) {
2358 revs->verbose_header = 1;
2359 revs->pretty_given = 1;
2360 get_commit_format(NULL, revs);
2361 } else if (skip_prefix(arg, "--pretty=", &optarg) ||
2362 skip_prefix(arg, "--format=", &optarg)) {
2364 * Detached form ("--pretty X" as opposed to "--pretty=X")
2365 * not allowed, since the argument is optional.
2367 revs->verbose_header = 1;
2368 revs->pretty_given = 1;
2369 get_commit_format(optarg, revs);
2370 } else if (!strcmp(arg, "--expand-tabs")) {
2371 revs->expand_tabs_in_log = 8;
2372 } else if (!strcmp(arg, "--no-expand-tabs")) {
2373 revs->expand_tabs_in_log = 0;
2374 } else if (skip_prefix(arg, "--expand-tabs=", &arg)) {
2375 int val;
2376 if (strtol_i(arg, 10, &val) < 0 || val < 0)
2377 die("'%s': not a non-negative integer", arg);
2378 revs->expand_tabs_in_log = val;
2379 } else if (!strcmp(arg, "--show-notes") || !strcmp(arg, "--notes")) {
2380 enable_default_display_notes(&revs->notes_opt, &revs->show_notes);
2381 revs->show_notes_given = 1;
2382 } else if (!strcmp(arg, "--show-signature")) {
2383 revs->show_signature = 1;
2384 } else if (!strcmp(arg, "--no-show-signature")) {
2385 revs->show_signature = 0;
2386 } else if (!strcmp(arg, "--show-linear-break")) {
2387 revs->break_bar = " ..........";
2388 revs->track_linear = 1;
2389 revs->track_first_time = 1;
2390 } else if (skip_prefix(arg, "--show-linear-break=", &optarg)) {
2391 revs->break_bar = xstrdup(optarg);
2392 revs->track_linear = 1;
2393 revs->track_first_time = 1;
2394 } else if (skip_prefix(arg, "--show-notes=", &optarg) ||
2395 skip_prefix(arg, "--notes=", &optarg)) {
2396 if (starts_with(arg, "--show-notes=") &&
2397 revs->notes_opt.use_default_notes < 0)
2398 revs->notes_opt.use_default_notes = 1;
2399 enable_ref_display_notes(&revs->notes_opt, &revs->show_notes, optarg);
2400 revs->show_notes_given = 1;
2401 } else if (!strcmp(arg, "--no-notes")) {
2402 disable_display_notes(&revs->notes_opt, &revs->show_notes);
2403 revs->show_notes_given = 1;
2404 } else if (!strcmp(arg, "--standard-notes")) {
2405 revs->show_notes_given = 1;
2406 revs->notes_opt.use_default_notes = 1;
2407 } else if (!strcmp(arg, "--no-standard-notes")) {
2408 revs->notes_opt.use_default_notes = 0;
2409 } else if (!strcmp(arg, "--oneline")) {
2410 revs->verbose_header = 1;
2411 get_commit_format("oneline", revs);
2412 revs->pretty_given = 1;
2413 revs->abbrev_commit = 1;
2414 } else if (!strcmp(arg, "--graph")) {
2415 revs->topo_order = 1;
2416 revs->rewrite_parents = 1;
2417 revs->graph = graph_init(revs);
2418 } else if (!strcmp(arg, "--encode-email-headers")) {
2419 revs->encode_email_headers = 1;
2420 } else if (!strcmp(arg, "--no-encode-email-headers")) {
2421 revs->encode_email_headers = 0;
2422 } else if (!strcmp(arg, "--root")) {
2423 revs->show_root_diff = 1;
2424 } else if (!strcmp(arg, "--no-commit-id")) {
2425 revs->no_commit_id = 1;
2426 } else if (!strcmp(arg, "--always")) {
2427 revs->always_show_header = 1;
2428 } else if (!strcmp(arg, "--no-abbrev")) {
2429 revs->abbrev = 0;
2430 } else if (!strcmp(arg, "--abbrev")) {
2431 revs->abbrev = DEFAULT_ABBREV;
2432 } else if (skip_prefix(arg, "--abbrev=", &optarg)) {
2433 revs->abbrev = strtoul(optarg, NULL, 10);
2434 if (revs->abbrev < MINIMUM_ABBREV)
2435 revs->abbrev = MINIMUM_ABBREV;
2436 else if (revs->abbrev > hexsz)
2437 revs->abbrev = hexsz;
2438 } else if (!strcmp(arg, "--abbrev-commit")) {
2439 revs->abbrev_commit = 1;
2440 revs->abbrev_commit_given = 1;
2441 } else if (!strcmp(arg, "--no-abbrev-commit")) {
2442 revs->abbrev_commit = 0;
2443 } else if (!strcmp(arg, "--full-diff")) {
2444 revs->diff = 1;
2445 revs->full_diff = 1;
2446 } else if (!strcmp(arg, "--show-pulls")) {
2447 revs->show_pulls = 1;
2448 } else if (!strcmp(arg, "--full-history")) {
2449 revs->simplify_history = 0;
2450 } else if (!strcmp(arg, "--relative-date")) {
2451 revs->date_mode.type = DATE_RELATIVE;
2452 revs->date_mode_explicit = 1;
2453 } else if ((argcount = parse_long_opt("date", argv, &optarg))) {
2454 parse_date_format(optarg, &revs->date_mode);
2455 revs->date_mode_explicit = 1;
2456 return argcount;
2457 } else if (!strcmp(arg, "--log-size")) {
2458 revs->show_log_size = 1;
2461 * Grepping the commit log
2463 else if ((argcount = parse_long_opt("author", argv, &optarg))) {
2464 add_header_grep(revs, GREP_HEADER_AUTHOR, optarg);
2465 return argcount;
2466 } else if ((argcount = parse_long_opt("committer", argv, &optarg))) {
2467 add_header_grep(revs, GREP_HEADER_COMMITTER, optarg);
2468 return argcount;
2469 } else if ((argcount = parse_long_opt("grep-reflog", argv, &optarg))) {
2470 add_header_grep(revs, GREP_HEADER_REFLOG, optarg);
2471 return argcount;
2472 } else if ((argcount = parse_long_opt("grep", argv, &optarg))) {
2473 add_message_grep(revs, optarg);
2474 return argcount;
2475 } else if (!strcmp(arg, "--grep-debug")) {
2476 revs->grep_filter.debug = 1;
2477 } else if (!strcmp(arg, "--basic-regexp")) {
2478 revs->grep_filter.pattern_type_option = GREP_PATTERN_TYPE_BRE;
2479 } else if (!strcmp(arg, "--extended-regexp") || !strcmp(arg, "-E")) {
2480 revs->grep_filter.pattern_type_option = GREP_PATTERN_TYPE_ERE;
2481 } else if (!strcmp(arg, "--regexp-ignore-case") || !strcmp(arg, "-i")) {
2482 revs->grep_filter.ignore_case = 1;
2483 revs->diffopt.pickaxe_opts |= DIFF_PICKAXE_IGNORE_CASE;
2484 } else if (!strcmp(arg, "--fixed-strings") || !strcmp(arg, "-F")) {
2485 revs->grep_filter.pattern_type_option = GREP_PATTERN_TYPE_FIXED;
2486 } else if (!strcmp(arg, "--perl-regexp") || !strcmp(arg, "-P")) {
2487 revs->grep_filter.pattern_type_option = GREP_PATTERN_TYPE_PCRE;
2488 } else if (!strcmp(arg, "--all-match")) {
2489 revs->grep_filter.all_match = 1;
2490 } else if (!strcmp(arg, "--invert-grep")) {
2491 revs->invert_grep = 1;
2492 } else if ((argcount = parse_long_opt("encoding", argv, &optarg))) {
2493 if (strcmp(optarg, "none"))
2494 git_log_output_encoding = xstrdup(optarg);
2495 else
2496 git_log_output_encoding = "";
2497 return argcount;
2498 } else if (!strcmp(arg, "--reverse")) {
2499 revs->reverse ^= 1;
2500 } else if (!strcmp(arg, "--children")) {
2501 revs->children.name = "children";
2502 revs->limited = 1;
2503 } else if (!strcmp(arg, "--ignore-missing")) {
2504 revs->ignore_missing = 1;
2505 } else if (opt && opt->allow_exclude_promisor_objects &&
2506 !strcmp(arg, "--exclude-promisor-objects")) {
2507 if (fetch_if_missing)
2508 BUG("exclude_promisor_objects can only be used when fetch_if_missing is 0");
2509 revs->exclude_promisor_objects = 1;
2510 } else {
2511 int opts = diff_opt_parse(&revs->diffopt, argv, argc, revs->prefix);
2512 if (!opts)
2513 unkv[(*unkc)++] = arg;
2514 return opts;
2516 if (revs->graph && revs->track_linear)
2517 die("--show-linear-break and --graph are incompatible");
2519 return 1;
2522 void parse_revision_opt(struct rev_info *revs, struct parse_opt_ctx_t *ctx,
2523 const struct option *options,
2524 const char * const usagestr[])
2526 int n = handle_revision_opt(revs, ctx->argc, ctx->argv,
2527 &ctx->cpidx, ctx->out, NULL);
2528 if (n <= 0) {
2529 error("unknown option `%s'", ctx->argv[0]);
2530 usage_with_options(usagestr, options);
2532 ctx->argv += n;
2533 ctx->argc -= n;
2536 static int for_each_bisect_ref(struct ref_store *refs, each_ref_fn fn,
2537 void *cb_data, const char *term)
2539 struct strbuf bisect_refs = STRBUF_INIT;
2540 int status;
2541 strbuf_addf(&bisect_refs, "refs/bisect/%s", term);
2542 status = refs_for_each_fullref_in(refs, bisect_refs.buf, fn, cb_data, 0);
2543 strbuf_release(&bisect_refs);
2544 return status;
2547 static int for_each_bad_bisect_ref(struct ref_store *refs, each_ref_fn fn, void *cb_data)
2549 return for_each_bisect_ref(refs, fn, cb_data, term_bad);
2552 static int for_each_good_bisect_ref(struct ref_store *refs, each_ref_fn fn, void *cb_data)
2554 return for_each_bisect_ref(refs, fn, cb_data, term_good);
2557 static int handle_revision_pseudo_opt(const char *submodule,
2558 struct rev_info *revs,
2559 const char **argv, int *flags)
2561 const char *arg = argv[0];
2562 const char *optarg;
2563 struct ref_store *refs;
2564 int argcount;
2566 if (submodule) {
2568 * We need some something like get_submodule_worktrees()
2569 * before we can go through all worktrees of a submodule,
2570 * .e.g with adding all HEADs from --all, which is not
2571 * supported right now, so stick to single worktree.
2573 if (!revs->single_worktree)
2574 BUG("--single-worktree cannot be used together with submodule");
2575 refs = get_submodule_ref_store(submodule);
2576 } else
2577 refs = get_main_ref_store(revs->repo);
2580 * NOTE!
2582 * Commands like "git shortlog" will not accept the options below
2583 * unless parse_revision_opt queues them (as opposed to erroring
2584 * out).
2586 * When implementing your new pseudo-option, remember to
2587 * register it in the list at the top of handle_revision_opt.
2589 if (!strcmp(arg, "--all")) {
2590 handle_refs(refs, revs, *flags, refs_for_each_ref);
2591 handle_refs(refs, revs, *flags, refs_head_ref);
2592 if (!revs->single_worktree) {
2593 struct all_refs_cb cb;
2595 init_all_refs_cb(&cb, revs, *flags);
2596 other_head_refs(handle_one_ref, &cb);
2598 clear_ref_exclusion(&revs->ref_excludes);
2599 } else if (!strcmp(arg, "--branches")) {
2600 handle_refs(refs, revs, *flags, refs_for_each_branch_ref);
2601 clear_ref_exclusion(&revs->ref_excludes);
2602 } else if (!strcmp(arg, "--bisect")) {
2603 read_bisect_terms(&term_bad, &term_good);
2604 handle_refs(refs, revs, *flags, for_each_bad_bisect_ref);
2605 handle_refs(refs, revs, *flags ^ (UNINTERESTING | BOTTOM),
2606 for_each_good_bisect_ref);
2607 revs->bisect = 1;
2608 } else if (!strcmp(arg, "--tags")) {
2609 handle_refs(refs, revs, *flags, refs_for_each_tag_ref);
2610 clear_ref_exclusion(&revs->ref_excludes);
2611 } else if (!strcmp(arg, "--remotes")) {
2612 handle_refs(refs, revs, *flags, refs_for_each_remote_ref);
2613 clear_ref_exclusion(&revs->ref_excludes);
2614 } else if ((argcount = parse_long_opt("glob", argv, &optarg))) {
2615 struct all_refs_cb cb;
2616 init_all_refs_cb(&cb, revs, *flags);
2617 for_each_glob_ref(handle_one_ref, optarg, &cb);
2618 clear_ref_exclusion(&revs->ref_excludes);
2619 return argcount;
2620 } else if ((argcount = parse_long_opt("exclude", argv, &optarg))) {
2621 add_ref_exclusion(&revs->ref_excludes, optarg);
2622 return argcount;
2623 } else if (skip_prefix(arg, "--branches=", &optarg)) {
2624 struct all_refs_cb cb;
2625 init_all_refs_cb(&cb, revs, *flags);
2626 for_each_glob_ref_in(handle_one_ref, optarg, "refs/heads/", &cb);
2627 clear_ref_exclusion(&revs->ref_excludes);
2628 } else if (skip_prefix(arg, "--tags=", &optarg)) {
2629 struct all_refs_cb cb;
2630 init_all_refs_cb(&cb, revs, *flags);
2631 for_each_glob_ref_in(handle_one_ref, optarg, "refs/tags/", &cb);
2632 clear_ref_exclusion(&revs->ref_excludes);
2633 } else if (skip_prefix(arg, "--remotes=", &optarg)) {
2634 struct all_refs_cb cb;
2635 init_all_refs_cb(&cb, revs, *flags);
2636 for_each_glob_ref_in(handle_one_ref, optarg, "refs/remotes/", &cb);
2637 clear_ref_exclusion(&revs->ref_excludes);
2638 } else if (!strcmp(arg, "--reflog")) {
2639 add_reflogs_to_pending(revs, *flags);
2640 } else if (!strcmp(arg, "--indexed-objects")) {
2641 add_index_objects_to_pending(revs, *flags);
2642 } else if (!strcmp(arg, "--alternate-refs")) {
2643 add_alternate_refs_to_pending(revs, *flags);
2644 } else if (!strcmp(arg, "--not")) {
2645 *flags ^= UNINTERESTING | BOTTOM;
2646 } else if (!strcmp(arg, "--no-walk")) {
2647 revs->no_walk = REVISION_WALK_NO_WALK_SORTED;
2648 } else if (skip_prefix(arg, "--no-walk=", &optarg)) {
2650 * Detached form ("--no-walk X" as opposed to "--no-walk=X")
2651 * not allowed, since the argument is optional.
2653 if (!strcmp(optarg, "sorted"))
2654 revs->no_walk = REVISION_WALK_NO_WALK_SORTED;
2655 else if (!strcmp(optarg, "unsorted"))
2656 revs->no_walk = REVISION_WALK_NO_WALK_UNSORTED;
2657 else
2658 return error("invalid argument to --no-walk");
2659 } else if (!strcmp(arg, "--do-walk")) {
2660 revs->no_walk = 0;
2661 } else if (!strcmp(arg, "--single-worktree")) {
2662 revs->single_worktree = 1;
2663 } else {
2664 return 0;
2667 return 1;
2670 static void NORETURN diagnose_missing_default(const char *def)
2672 int flags;
2673 const char *refname;
2675 refname = resolve_ref_unsafe(def, 0, NULL, &flags);
2676 if (!refname || !(flags & REF_ISSYMREF) || (flags & REF_ISBROKEN))
2677 die(_("your current branch appears to be broken"));
2679 skip_prefix(refname, "refs/heads/", &refname);
2680 die(_("your current branch '%s' does not have any commits yet"),
2681 refname);
2685 * Parse revision information, filling in the "rev_info" structure,
2686 * and removing the used arguments from the argument list.
2688 * Returns the number of arguments left that weren't recognized
2689 * (which are also moved to the head of the argument list)
2691 int setup_revisions(int argc, const char **argv, struct rev_info *revs, struct setup_revision_opt *opt)
2693 int i, flags, left, seen_dashdash, revarg_opt;
2694 struct strvec prune_data = STRVEC_INIT;
2695 const char *submodule = NULL;
2696 int seen_end_of_options = 0;
2698 if (opt)
2699 submodule = opt->submodule;
2701 /* First, search for "--" */
2702 if (opt && opt->assume_dashdash) {
2703 seen_dashdash = 1;
2704 } else {
2705 seen_dashdash = 0;
2706 for (i = 1; i < argc; i++) {
2707 const char *arg = argv[i];
2708 if (strcmp(arg, "--"))
2709 continue;
2710 argv[i] = NULL;
2711 argc = i;
2712 if (argv[i + 1])
2713 strvec_pushv(&prune_data, argv + i + 1);
2714 seen_dashdash = 1;
2715 break;
2719 /* Second, deal with arguments and options */
2720 flags = 0;
2721 revarg_opt = opt ? opt->revarg_opt : 0;
2722 if (seen_dashdash)
2723 revarg_opt |= REVARG_CANNOT_BE_FILENAME;
2724 for (left = i = 1; i < argc; i++) {
2725 const char *arg = argv[i];
2726 if (!seen_end_of_options && *arg == '-') {
2727 int opts;
2729 opts = handle_revision_pseudo_opt(submodule,
2730 revs, argv + i,
2731 &flags);
2732 if (opts > 0) {
2733 i += opts - 1;
2734 continue;
2737 if (!strcmp(arg, "--stdin")) {
2738 if (revs->disable_stdin) {
2739 argv[left++] = arg;
2740 continue;
2742 if (revs->read_from_stdin++)
2743 die("--stdin given twice?");
2744 read_revisions_from_stdin(revs, &prune_data);
2745 continue;
2748 if (!strcmp(arg, "--end-of-options")) {
2749 seen_end_of_options = 1;
2750 continue;
2753 opts = handle_revision_opt(revs, argc - i, argv + i,
2754 &left, argv, opt);
2755 if (opts > 0) {
2756 i += opts - 1;
2757 continue;
2759 if (opts < 0)
2760 exit(128);
2761 continue;
2765 if (handle_revision_arg(arg, revs, flags, revarg_opt)) {
2766 int j;
2767 if (seen_dashdash || *arg == '^')
2768 die("bad revision '%s'", arg);
2770 /* If we didn't have a "--":
2771 * (1) all filenames must exist;
2772 * (2) all rev-args must not be interpretable
2773 * as a valid filename.
2774 * but the latter we have checked in the main loop.
2776 for (j = i; j < argc; j++)
2777 verify_filename(revs->prefix, argv[j], j == i);
2779 strvec_pushv(&prune_data, argv + i);
2780 break;
2784 if (prune_data.nr) {
2786 * If we need to introduce the magic "a lone ':' means no
2787 * pathspec whatsoever", here is the place to do so.
2789 * if (prune_data.nr == 1 && !strcmp(prune_data[0], ":")) {
2790 * prune_data.nr = 0;
2791 * prune_data.alloc = 0;
2792 * free(prune_data.path);
2793 * prune_data.path = NULL;
2794 * } else {
2795 * terminate prune_data.alloc with NULL and
2796 * call init_pathspec() to set revs->prune_data here.
2799 parse_pathspec(&revs->prune_data, 0, 0,
2800 revs->prefix, prune_data.v);
2802 strvec_clear(&prune_data);
2804 if (revs->def == NULL)
2805 revs->def = opt ? opt->def : NULL;
2806 if (opt && opt->tweak)
2807 opt->tweak(revs, opt);
2808 if (revs->show_merge)
2809 prepare_show_merge(revs);
2810 if (revs->def && !revs->pending.nr && !revs->rev_input_given) {
2811 struct object_id oid;
2812 struct object *object;
2813 struct object_context oc;
2814 if (get_oid_with_context(revs->repo, revs->def, 0, &oid, &oc))
2815 diagnose_missing_default(revs->def);
2816 object = get_reference(revs, revs->def, &oid, 0);
2817 add_pending_object_with_mode(revs, object, revs->def, oc.mode);
2820 /* Did the user ask for any diff output? Run the diff! */
2821 if (revs->diffopt.output_format & ~DIFF_FORMAT_NO_OUTPUT)
2822 revs->diff = 1;
2824 /* Pickaxe, diff-filter and rename following need diffs */
2825 if ((revs->diffopt.pickaxe_opts & DIFF_PICKAXE_KINDS_MASK) ||
2826 revs->diffopt.filter ||
2827 revs->diffopt.flags.follow_renames)
2828 revs->diff = 1;
2830 if (revs->diffopt.objfind)
2831 revs->simplify_history = 0;
2833 if (revs->line_level_traverse) {
2834 if (want_ancestry(revs))
2835 revs->limited = 1;
2836 revs->topo_order = 1;
2839 if (revs->topo_order && !generation_numbers_enabled(the_repository))
2840 revs->limited = 1;
2842 if (revs->prune_data.nr) {
2843 copy_pathspec(&revs->pruning.pathspec, &revs->prune_data);
2844 /* Can't prune commits with rename following: the paths change.. */
2845 if (!revs->diffopt.flags.follow_renames)
2846 revs->prune = 1;
2847 if (!revs->full_diff)
2848 copy_pathspec(&revs->diffopt.pathspec,
2849 &revs->prune_data);
2852 diff_merges_setup_revs(revs);
2854 revs->diffopt.abbrev = revs->abbrev;
2856 diff_setup_done(&revs->diffopt);
2858 grep_commit_pattern_type(GREP_PATTERN_TYPE_UNSPECIFIED,
2859 &revs->grep_filter);
2860 if (!is_encoding_utf8(get_log_output_encoding()))
2861 revs->grep_filter.ignore_locale = 1;
2862 compile_grep_patterns(&revs->grep_filter);
2864 if (revs->reverse && revs->reflog_info)
2865 die("cannot combine --reverse with --walk-reflogs");
2866 if (revs->reflog_info && revs->limited)
2867 die("cannot combine --walk-reflogs with history-limiting options");
2868 if (revs->rewrite_parents && revs->children.name)
2869 die("cannot combine --parents and --children");
2872 * Limitations on the graph functionality
2874 if (revs->reverse && revs->graph)
2875 die("cannot combine --reverse with --graph");
2877 if (revs->reflog_info && revs->graph)
2878 die("cannot combine --walk-reflogs with --graph");
2879 if (revs->no_walk && revs->graph)
2880 die("cannot combine --no-walk with --graph");
2881 if (!revs->reflog_info && revs->grep_filter.use_reflog_filter)
2882 die("cannot use --grep-reflog without --walk-reflogs");
2884 if (revs->line_level_traverse &&
2885 (revs->diffopt.output_format & ~(DIFF_FORMAT_PATCH | DIFF_FORMAT_NO_OUTPUT)))
2886 die(_("-L does not yet support diff formats besides -p and -s"));
2888 if (revs->expand_tabs_in_log < 0)
2889 revs->expand_tabs_in_log = revs->expand_tabs_in_log_default;
2891 return left;
2894 static void add_child(struct rev_info *revs, struct commit *parent, struct commit *child)
2896 struct commit_list *l = xcalloc(1, sizeof(*l));
2898 l->item = child;
2899 l->next = add_decoration(&revs->children, &parent->object, l);
2902 static int remove_duplicate_parents(struct rev_info *revs, struct commit *commit)
2904 struct treesame_state *ts = lookup_decoration(&revs->treesame, &commit->object);
2905 struct commit_list **pp, *p;
2906 int surviving_parents;
2908 /* Examine existing parents while marking ones we have seen... */
2909 pp = &commit->parents;
2910 surviving_parents = 0;
2911 while ((p = *pp) != NULL) {
2912 struct commit *parent = p->item;
2913 if (parent->object.flags & TMP_MARK) {
2914 *pp = p->next;
2915 if (ts)
2916 compact_treesame(revs, commit, surviving_parents);
2917 continue;
2919 parent->object.flags |= TMP_MARK;
2920 surviving_parents++;
2921 pp = &p->next;
2923 /* clear the temporary mark */
2924 for (p = commit->parents; p; p = p->next) {
2925 p->item->object.flags &= ~TMP_MARK;
2927 /* no update_treesame() - removing duplicates can't affect TREESAME */
2928 return surviving_parents;
2931 struct merge_simplify_state {
2932 struct commit *simplified;
2935 static struct merge_simplify_state *locate_simplify_state(struct rev_info *revs, struct commit *commit)
2937 struct merge_simplify_state *st;
2939 st = lookup_decoration(&revs->merge_simplification, &commit->object);
2940 if (!st) {
2941 st = xcalloc(1, sizeof(*st));
2942 add_decoration(&revs->merge_simplification, &commit->object, st);
2944 return st;
2947 static int mark_redundant_parents(struct commit *commit)
2949 struct commit_list *h = reduce_heads(commit->parents);
2950 int i = 0, marked = 0;
2951 struct commit_list *po, *pn;
2953 /* Want these for sanity-checking only */
2954 int orig_cnt = commit_list_count(commit->parents);
2955 int cnt = commit_list_count(h);
2958 * Not ready to remove items yet, just mark them for now, based
2959 * on the output of reduce_heads(). reduce_heads outputs the reduced
2960 * set in its original order, so this isn't too hard.
2962 po = commit->parents;
2963 pn = h;
2964 while (po) {
2965 if (pn && po->item == pn->item) {
2966 pn = pn->next;
2967 i++;
2968 } else {
2969 po->item->object.flags |= TMP_MARK;
2970 marked++;
2972 po=po->next;
2975 if (i != cnt || cnt+marked != orig_cnt)
2976 die("mark_redundant_parents %d %d %d %d", orig_cnt, cnt, i, marked);
2978 free_commit_list(h);
2980 return marked;
2983 static int mark_treesame_root_parents(struct commit *commit)
2985 struct commit_list *p;
2986 int marked = 0;
2988 for (p = commit->parents; p; p = p->next) {
2989 struct commit *parent = p->item;
2990 if (!parent->parents && (parent->object.flags & TREESAME)) {
2991 parent->object.flags |= TMP_MARK;
2992 marked++;
2996 return marked;
3000 * Awkward naming - this means one parent we are TREESAME to.
3001 * cf mark_treesame_root_parents: root parents that are TREESAME (to an
3002 * empty tree). Better name suggestions?
3004 static int leave_one_treesame_to_parent(struct rev_info *revs, struct commit *commit)
3006 struct treesame_state *ts = lookup_decoration(&revs->treesame, &commit->object);
3007 struct commit *unmarked = NULL, *marked = NULL;
3008 struct commit_list *p;
3009 unsigned n;
3011 for (p = commit->parents, n = 0; p; p = p->next, n++) {
3012 if (ts->treesame[n]) {
3013 if (p->item->object.flags & TMP_MARK) {
3014 if (!marked)
3015 marked = p->item;
3016 } else {
3017 if (!unmarked) {
3018 unmarked = p->item;
3019 break;
3026 * If we are TREESAME to a marked-for-deletion parent, but not to any
3027 * unmarked parents, unmark the first TREESAME parent. This is the
3028 * parent that the default simplify_history==1 scan would have followed,
3029 * and it doesn't make sense to omit that path when asking for a
3030 * simplified full history. Retaining it improves the chances of
3031 * understanding odd missed merges that took an old version of a file.
3033 * Example:
3035 * I--------*X A modified the file, but mainline merge X used
3036 * \ / "-s ours", so took the version from I. X is
3037 * `-*A--' TREESAME to I and !TREESAME to A.
3039 * Default log from X would produce "I". Without this check,
3040 * --full-history --simplify-merges would produce "I-A-X", showing
3041 * the merge commit X and that it changed A, but not making clear that
3042 * it had just taken the I version. With this check, the topology above
3043 * is retained.
3045 * Note that it is possible that the simplification chooses a different
3046 * TREESAME parent from the default, in which case this test doesn't
3047 * activate, and we _do_ drop the default parent. Example:
3049 * I------X A modified the file, but it was reverted in B,
3050 * \ / meaning mainline merge X is TREESAME to both
3051 * *A-*B parents.
3053 * Default log would produce "I" by following the first parent;
3054 * --full-history --simplify-merges will produce "I-A-B". But this is a
3055 * reasonable result - it presents a logical full history leading from
3056 * I to X, and X is not an important merge.
3058 if (!unmarked && marked) {
3059 marked->object.flags &= ~TMP_MARK;
3060 return 1;
3063 return 0;
3066 static int remove_marked_parents(struct rev_info *revs, struct commit *commit)
3068 struct commit_list **pp, *p;
3069 int nth_parent, removed = 0;
3071 pp = &commit->parents;
3072 nth_parent = 0;
3073 while ((p = *pp) != NULL) {
3074 struct commit *parent = p->item;
3075 if (parent->object.flags & TMP_MARK) {
3076 parent->object.flags &= ~TMP_MARK;
3077 *pp = p->next;
3078 free(p);
3079 removed++;
3080 compact_treesame(revs, commit, nth_parent);
3081 continue;
3083 pp = &p->next;
3084 nth_parent++;
3087 /* Removing parents can only increase TREESAMEness */
3088 if (removed && !(commit->object.flags & TREESAME))
3089 update_treesame(revs, commit);
3091 return nth_parent;
3094 static struct commit_list **simplify_one(struct rev_info *revs, struct commit *commit, struct commit_list **tail)
3096 struct commit_list *p;
3097 struct commit *parent;
3098 struct merge_simplify_state *st, *pst;
3099 int cnt;
3101 st = locate_simplify_state(revs, commit);
3104 * Have we handled this one?
3106 if (st->simplified)
3107 return tail;
3110 * An UNINTERESTING commit simplifies to itself, so does a
3111 * root commit. We do not rewrite parents of such commit
3112 * anyway.
3114 if ((commit->object.flags & UNINTERESTING) || !commit->parents) {
3115 st->simplified = commit;
3116 return tail;
3120 * Do we know what commit all of our parents that matter
3121 * should be rewritten to? Otherwise we are not ready to
3122 * rewrite this one yet.
3124 for (cnt = 0, p = commit->parents; p; p = p->next) {
3125 pst = locate_simplify_state(revs, p->item);
3126 if (!pst->simplified) {
3127 tail = &commit_list_insert(p->item, tail)->next;
3128 cnt++;
3130 if (revs->first_parent_only)
3131 break;
3133 if (cnt) {
3134 tail = &commit_list_insert(commit, tail)->next;
3135 return tail;
3139 * Rewrite our list of parents. Note that this cannot
3140 * affect our TREESAME flags in any way - a commit is
3141 * always TREESAME to its simplification.
3143 for (p = commit->parents; p; p = p->next) {
3144 pst = locate_simplify_state(revs, p->item);
3145 p->item = pst->simplified;
3146 if (revs->first_parent_only)
3147 break;
3150 if (revs->first_parent_only)
3151 cnt = 1;
3152 else
3153 cnt = remove_duplicate_parents(revs, commit);
3156 * It is possible that we are a merge and one side branch
3157 * does not have any commit that touches the given paths;
3158 * in such a case, the immediate parent from that branch
3159 * will be rewritten to be the merge base.
3161 * o----X X: the commit we are looking at;
3162 * / / o: a commit that touches the paths;
3163 * ---o----'
3165 * Further, a merge of an independent branch that doesn't
3166 * touch the path will reduce to a treesame root parent:
3168 * ----o----X X: the commit we are looking at;
3169 * / o: a commit that touches the paths;
3170 * r r: a root commit not touching the paths
3172 * Detect and simplify both cases.
3174 if (1 < cnt) {
3175 int marked = mark_redundant_parents(commit);
3176 marked += mark_treesame_root_parents(commit);
3177 if (marked)
3178 marked -= leave_one_treesame_to_parent(revs, commit);
3179 if (marked)
3180 cnt = remove_marked_parents(revs, commit);
3184 * A commit simplifies to itself if it is a root, if it is
3185 * UNINTERESTING, if it touches the given paths, or if it is a
3186 * merge and its parents don't simplify to one relevant commit
3187 * (the first two cases are already handled at the beginning of
3188 * this function).
3190 * Otherwise, it simplifies to what its sole relevant parent
3191 * simplifies to.
3193 if (!cnt ||
3194 (commit->object.flags & UNINTERESTING) ||
3195 !(commit->object.flags & TREESAME) ||
3196 (parent = one_relevant_parent(revs, commit->parents)) == NULL ||
3197 (revs->show_pulls && (commit->object.flags & PULL_MERGE)))
3198 st->simplified = commit;
3199 else {
3200 pst = locate_simplify_state(revs, parent);
3201 st->simplified = pst->simplified;
3203 return tail;
3206 static void simplify_merges(struct rev_info *revs)
3208 struct commit_list *list, *next;
3209 struct commit_list *yet_to_do, **tail;
3210 struct commit *commit;
3212 if (!revs->prune)
3213 return;
3215 /* feed the list reversed */
3216 yet_to_do = NULL;
3217 for (list = revs->commits; list; list = next) {
3218 commit = list->item;
3219 next = list->next;
3221 * Do not free(list) here yet; the original list
3222 * is used later in this function.
3224 commit_list_insert(commit, &yet_to_do);
3226 while (yet_to_do) {
3227 list = yet_to_do;
3228 yet_to_do = NULL;
3229 tail = &yet_to_do;
3230 while (list) {
3231 commit = pop_commit(&list);
3232 tail = simplify_one(revs, commit, tail);
3236 /* clean up the result, removing the simplified ones */
3237 list = revs->commits;
3238 revs->commits = NULL;
3239 tail = &revs->commits;
3240 while (list) {
3241 struct merge_simplify_state *st;
3243 commit = pop_commit(&list);
3244 st = locate_simplify_state(revs, commit);
3245 if (st->simplified == commit)
3246 tail = &commit_list_insert(commit, tail)->next;
3250 static void set_children(struct rev_info *revs)
3252 struct commit_list *l;
3253 for (l = revs->commits; l; l = l->next) {
3254 struct commit *commit = l->item;
3255 struct commit_list *p;
3257 for (p = commit->parents; p; p = p->next)
3258 add_child(revs, p->item, commit);
3262 void reset_revision_walk(void)
3264 clear_object_flags(SEEN | ADDED | SHOWN | TOPO_WALK_EXPLORED | TOPO_WALK_INDEGREE);
3267 static int mark_uninteresting(const struct object_id *oid,
3268 struct packed_git *pack,
3269 uint32_t pos,
3270 void *cb)
3272 struct rev_info *revs = cb;
3273 struct object *o = parse_object(revs->repo, oid);
3274 o->flags |= UNINTERESTING | SEEN;
3275 return 0;
3278 define_commit_slab(indegree_slab, int);
3279 define_commit_slab(author_date_slab, timestamp_t);
3281 struct topo_walk_info {
3282 uint32_t min_generation;
3283 struct prio_queue explore_queue;
3284 struct prio_queue indegree_queue;
3285 struct prio_queue topo_queue;
3286 struct indegree_slab indegree;
3287 struct author_date_slab author_date;
3290 static inline void test_flag_and_insert(struct prio_queue *q, struct commit *c, int flag)
3292 if (c->object.flags & flag)
3293 return;
3295 c->object.flags |= flag;
3296 prio_queue_put(q, c);
3299 static void explore_walk_step(struct rev_info *revs)
3301 struct topo_walk_info *info = revs->topo_walk_info;
3302 struct commit_list *p;
3303 struct commit *c = prio_queue_get(&info->explore_queue);
3305 if (!c)
3306 return;
3308 if (repo_parse_commit_gently(revs->repo, c, 1) < 0)
3309 return;
3311 if (revs->sort_order == REV_SORT_BY_AUTHOR_DATE)
3312 record_author_date(&info->author_date, c);
3314 if (revs->max_age != -1 && (c->date < revs->max_age))
3315 c->object.flags |= UNINTERESTING;
3317 if (process_parents(revs, c, NULL, NULL) < 0)
3318 return;
3320 if (c->object.flags & UNINTERESTING)
3321 mark_parents_uninteresting(c);
3323 for (p = c->parents; p; p = p->next)
3324 test_flag_and_insert(&info->explore_queue, p->item, TOPO_WALK_EXPLORED);
3327 static void explore_to_depth(struct rev_info *revs,
3328 uint32_t gen_cutoff)
3330 struct topo_walk_info *info = revs->topo_walk_info;
3331 struct commit *c;
3332 while ((c = prio_queue_peek(&info->explore_queue)) &&
3333 commit_graph_generation(c) >= gen_cutoff)
3334 explore_walk_step(revs);
3337 static void indegree_walk_step(struct rev_info *revs)
3339 struct commit_list *p;
3340 struct topo_walk_info *info = revs->topo_walk_info;
3341 struct commit *c = prio_queue_get(&info->indegree_queue);
3343 if (!c)
3344 return;
3346 if (repo_parse_commit_gently(revs->repo, c, 1) < 0)
3347 return;
3349 explore_to_depth(revs, commit_graph_generation(c));
3351 for (p = c->parents; p; p = p->next) {
3352 struct commit *parent = p->item;
3353 int *pi = indegree_slab_at(&info->indegree, parent);
3355 if (*pi)
3356 (*pi)++;
3357 else
3358 *pi = 2;
3360 test_flag_and_insert(&info->indegree_queue, parent, TOPO_WALK_INDEGREE);
3362 if (revs->first_parent_only)
3363 return;
3367 static void compute_indegrees_to_depth(struct rev_info *revs,
3368 uint32_t gen_cutoff)
3370 struct topo_walk_info *info = revs->topo_walk_info;
3371 struct commit *c;
3372 while ((c = prio_queue_peek(&info->indegree_queue)) &&
3373 commit_graph_generation(c) >= gen_cutoff)
3374 indegree_walk_step(revs);
3377 static void reset_topo_walk(struct rev_info *revs)
3379 struct topo_walk_info *info = revs->topo_walk_info;
3381 clear_prio_queue(&info->explore_queue);
3382 clear_prio_queue(&info->indegree_queue);
3383 clear_prio_queue(&info->topo_queue);
3384 clear_indegree_slab(&info->indegree);
3385 clear_author_date_slab(&info->author_date);
3387 FREE_AND_NULL(revs->topo_walk_info);
3390 static void init_topo_walk(struct rev_info *revs)
3392 struct topo_walk_info *info;
3393 struct commit_list *list;
3394 if (revs->topo_walk_info)
3395 reset_topo_walk(revs);
3397 revs->topo_walk_info = xmalloc(sizeof(struct topo_walk_info));
3398 info = revs->topo_walk_info;
3399 memset(info, 0, sizeof(struct topo_walk_info));
3401 init_indegree_slab(&info->indegree);
3402 memset(&info->explore_queue, 0, sizeof(info->explore_queue));
3403 memset(&info->indegree_queue, 0, sizeof(info->indegree_queue));
3404 memset(&info->topo_queue, 0, sizeof(info->topo_queue));
3406 switch (revs->sort_order) {
3407 default: /* REV_SORT_IN_GRAPH_ORDER */
3408 info->topo_queue.compare = NULL;
3409 break;
3410 case REV_SORT_BY_COMMIT_DATE:
3411 info->topo_queue.compare = compare_commits_by_commit_date;
3412 break;
3413 case REV_SORT_BY_AUTHOR_DATE:
3414 init_author_date_slab(&info->author_date);
3415 info->topo_queue.compare = compare_commits_by_author_date;
3416 info->topo_queue.cb_data = &info->author_date;
3417 break;
3420 info->explore_queue.compare = compare_commits_by_gen_then_commit_date;
3421 info->indegree_queue.compare = compare_commits_by_gen_then_commit_date;
3423 info->min_generation = GENERATION_NUMBER_INFINITY;
3424 for (list = revs->commits; list; list = list->next) {
3425 struct commit *c = list->item;
3426 uint32_t generation;
3428 if (repo_parse_commit_gently(revs->repo, c, 1))
3429 continue;
3431 test_flag_and_insert(&info->explore_queue, c, TOPO_WALK_EXPLORED);
3432 test_flag_and_insert(&info->indegree_queue, c, TOPO_WALK_INDEGREE);
3434 generation = commit_graph_generation(c);
3435 if (generation < info->min_generation)
3436 info->min_generation = generation;
3438 *(indegree_slab_at(&info->indegree, c)) = 1;
3440 if (revs->sort_order == REV_SORT_BY_AUTHOR_DATE)
3441 record_author_date(&info->author_date, c);
3443 compute_indegrees_to_depth(revs, info->min_generation);
3445 for (list = revs->commits; list; list = list->next) {
3446 struct commit *c = list->item;
3448 if (*(indegree_slab_at(&info->indegree, c)) == 1)
3449 prio_queue_put(&info->topo_queue, c);
3453 * This is unfortunate; the initial tips need to be shown
3454 * in the order given from the revision traversal machinery.
3456 if (revs->sort_order == REV_SORT_IN_GRAPH_ORDER)
3457 prio_queue_reverse(&info->topo_queue);
3460 static struct commit *next_topo_commit(struct rev_info *revs)
3462 struct commit *c;
3463 struct topo_walk_info *info = revs->topo_walk_info;
3465 /* pop next off of topo_queue */
3466 c = prio_queue_get(&info->topo_queue);
3468 if (c)
3469 *(indegree_slab_at(&info->indegree, c)) = 0;
3471 return c;
3474 static void expand_topo_walk(struct rev_info *revs, struct commit *commit)
3476 struct commit_list *p;
3477 struct topo_walk_info *info = revs->topo_walk_info;
3478 if (process_parents(revs, commit, NULL, NULL) < 0) {
3479 if (!revs->ignore_missing_links)
3480 die("Failed to traverse parents of commit %s",
3481 oid_to_hex(&commit->object.oid));
3484 for (p = commit->parents; p; p = p->next) {
3485 struct commit *parent = p->item;
3486 int *pi;
3487 uint32_t generation;
3489 if (parent->object.flags & UNINTERESTING)
3490 continue;
3492 if (repo_parse_commit_gently(revs->repo, parent, 1) < 0)
3493 continue;
3495 generation = commit_graph_generation(parent);
3496 if (generation < info->min_generation) {
3497 info->min_generation = generation;
3498 compute_indegrees_to_depth(revs, info->min_generation);
3501 pi = indegree_slab_at(&info->indegree, parent);
3503 (*pi)--;
3504 if (*pi == 1)
3505 prio_queue_put(&info->topo_queue, parent);
3507 if (revs->first_parent_only)
3508 return;
3512 int prepare_revision_walk(struct rev_info *revs)
3514 int i;
3515 struct object_array old_pending;
3516 struct commit_list **next = &revs->commits;
3518 memcpy(&old_pending, &revs->pending, sizeof(old_pending));
3519 revs->pending.nr = 0;
3520 revs->pending.alloc = 0;
3521 revs->pending.objects = NULL;
3522 for (i = 0; i < old_pending.nr; i++) {
3523 struct object_array_entry *e = old_pending.objects + i;
3524 struct commit *commit = handle_commit(revs, e);
3525 if (commit) {
3526 if (!(commit->object.flags & SEEN)) {
3527 commit->object.flags |= SEEN;
3528 next = commit_list_append(commit, next);
3532 object_array_clear(&old_pending);
3534 /* Signal whether we need per-parent treesame decoration */
3535 if (revs->simplify_merges ||
3536 (revs->limited && limiting_can_increase_treesame(revs)))
3537 revs->treesame.name = "treesame";
3539 if (revs->exclude_promisor_objects) {
3540 for_each_packed_object(mark_uninteresting, revs,
3541 FOR_EACH_OBJECT_PROMISOR_ONLY);
3544 if (!revs->reflog_info)
3545 prepare_to_use_bloom_filter(revs);
3546 if (revs->no_walk != REVISION_WALK_NO_WALK_UNSORTED)
3547 commit_list_sort_by_date(&revs->commits);
3548 if (revs->no_walk)
3549 return 0;
3550 if (revs->limited) {
3551 if (limit_list(revs) < 0)
3552 return -1;
3553 if (revs->topo_order)
3554 sort_in_topological_order(&revs->commits, revs->sort_order);
3555 } else if (revs->topo_order)
3556 init_topo_walk(revs);
3557 if (revs->line_level_traverse && want_ancestry(revs))
3559 * At the moment we can only do line-level log with parent
3560 * rewriting by performing this expensive pre-filtering step.
3561 * If parent rewriting is not requested, then we rather
3562 * perform the line-level log filtering during the regular
3563 * history traversal.
3565 line_log_filter(revs);
3566 if (revs->simplify_merges)
3567 simplify_merges(revs);
3568 if (revs->children.name)
3569 set_children(revs);
3571 return 0;
3574 static enum rewrite_result rewrite_one_1(struct rev_info *revs,
3575 struct commit **pp,
3576 struct prio_queue *queue)
3578 for (;;) {
3579 struct commit *p = *pp;
3580 if (!revs->limited)
3581 if (process_parents(revs, p, NULL, queue) < 0)
3582 return rewrite_one_error;
3583 if (p->object.flags & UNINTERESTING)
3584 return rewrite_one_ok;
3585 if (!(p->object.flags & TREESAME))
3586 return rewrite_one_ok;
3587 if (!p->parents)
3588 return rewrite_one_noparents;
3589 if ((p = one_relevant_parent(revs, p->parents)) == NULL)
3590 return rewrite_one_ok;
3591 *pp = p;
3595 static void merge_queue_into_list(struct prio_queue *q, struct commit_list **list)
3597 while (q->nr) {
3598 struct commit *item = prio_queue_peek(q);
3599 struct commit_list *p = *list;
3601 if (p && p->item->date >= item->date)
3602 list = &p->next;
3603 else {
3604 p = commit_list_insert(item, list);
3605 list = &p->next; /* skip newly added item */
3606 prio_queue_get(q); /* pop item */
3611 static enum rewrite_result rewrite_one(struct rev_info *revs, struct commit **pp)
3613 struct prio_queue queue = { compare_commits_by_commit_date };
3614 enum rewrite_result ret = rewrite_one_1(revs, pp, &queue);
3615 merge_queue_into_list(&queue, &revs->commits);
3616 clear_prio_queue(&queue);
3617 return ret;
3620 int rewrite_parents(struct rev_info *revs, struct commit *commit,
3621 rewrite_parent_fn_t rewrite_parent)
3623 struct commit_list **pp = &commit->parents;
3624 while (*pp) {
3625 struct commit_list *parent = *pp;
3626 switch (rewrite_parent(revs, &parent->item)) {
3627 case rewrite_one_ok:
3628 break;
3629 case rewrite_one_noparents:
3630 *pp = parent->next;
3631 continue;
3632 case rewrite_one_error:
3633 return -1;
3635 pp = &parent->next;
3637 remove_duplicate_parents(revs, commit);
3638 return 0;
3641 static int commit_rewrite_person(struct strbuf *buf, const char *what, struct string_list *mailmap)
3643 char *person, *endp;
3644 size_t len, namelen, maillen;
3645 const char *name;
3646 const char *mail;
3647 struct ident_split ident;
3649 person = strstr(buf->buf, what);
3650 if (!person)
3651 return 0;
3653 person += strlen(what);
3654 endp = strchr(person, '\n');
3655 if (!endp)
3656 return 0;
3658 len = endp - person;
3660 if (split_ident_line(&ident, person, len))
3661 return 0;
3663 mail = ident.mail_begin;
3664 maillen = ident.mail_end - ident.mail_begin;
3665 name = ident.name_begin;
3666 namelen = ident.name_end - ident.name_begin;
3668 if (map_user(mailmap, &mail, &maillen, &name, &namelen)) {
3669 struct strbuf namemail = STRBUF_INIT;
3671 strbuf_addf(&namemail, "%.*s <%.*s>",
3672 (int)namelen, name, (int)maillen, mail);
3674 strbuf_splice(buf, ident.name_begin - buf->buf,
3675 ident.mail_end - ident.name_begin + 1,
3676 namemail.buf, namemail.len);
3678 strbuf_release(&namemail);
3680 return 1;
3683 return 0;
3686 static int commit_match(struct commit *commit, struct rev_info *opt)
3688 int retval;
3689 const char *encoding;
3690 const char *message;
3691 struct strbuf buf = STRBUF_INIT;
3693 if (!opt->grep_filter.pattern_list && !opt->grep_filter.header_list)
3694 return 1;
3696 /* Prepend "fake" headers as needed */
3697 if (opt->grep_filter.use_reflog_filter) {
3698 strbuf_addstr(&buf, "reflog ");
3699 get_reflog_message(&buf, opt->reflog_info);
3700 strbuf_addch(&buf, '\n');
3704 * We grep in the user's output encoding, under the assumption that it
3705 * is the encoding they are most likely to write their grep pattern
3706 * for. In addition, it means we will match the "notes" encoding below,
3707 * so we will not end up with a buffer that has two different encodings
3708 * in it.
3710 encoding = get_log_output_encoding();
3711 message = logmsg_reencode(commit, NULL, encoding);
3713 /* Copy the commit to temporary if we are using "fake" headers */
3714 if (buf.len)
3715 strbuf_addstr(&buf, message);
3717 if (opt->grep_filter.header_list && opt->mailmap) {
3718 if (!buf.len)
3719 strbuf_addstr(&buf, message);
3721 commit_rewrite_person(&buf, "\nauthor ", opt->mailmap);
3722 commit_rewrite_person(&buf, "\ncommitter ", opt->mailmap);
3725 /* Append "fake" message parts as needed */
3726 if (opt->show_notes) {
3727 if (!buf.len)
3728 strbuf_addstr(&buf, message);
3729 format_display_notes(&commit->object.oid, &buf, encoding, 1);
3733 * Find either in the original commit message, or in the temporary.
3734 * Note that we cast away the constness of "message" here. It is
3735 * const because it may come from the cached commit buffer. That's OK,
3736 * because we know that it is modifiable heap memory, and that while
3737 * grep_buffer may modify it for speed, it will restore any
3738 * changes before returning.
3740 if (buf.len)
3741 retval = grep_buffer(&opt->grep_filter, buf.buf, buf.len);
3742 else
3743 retval = grep_buffer(&opt->grep_filter,
3744 (char *)message, strlen(message));
3745 strbuf_release(&buf);
3746 unuse_commit_buffer(commit, message);
3747 return opt->invert_grep ? !retval : retval;
3750 static inline int want_ancestry(const struct rev_info *revs)
3752 return (revs->rewrite_parents || revs->children.name);
3756 * Return a timestamp to be used for --since/--until comparisons for this
3757 * commit, based on the revision options.
3759 static timestamp_t comparison_date(const struct rev_info *revs,
3760 struct commit *commit)
3762 return revs->reflog_info ?
3763 get_reflog_timestamp(revs->reflog_info) :
3764 commit->date;
3767 enum commit_action get_commit_action(struct rev_info *revs, struct commit *commit)
3769 if (commit->object.flags & SHOWN)
3770 return commit_ignore;
3771 if (revs->unpacked && has_object_pack(&commit->object.oid))
3772 return commit_ignore;
3773 if (commit->object.flags & UNINTERESTING)
3774 return commit_ignore;
3775 if (revs->line_level_traverse && !want_ancestry(revs)) {
3777 * In case of line-level log with parent rewriting
3778 * prepare_revision_walk() already took care of all line-level
3779 * log filtering, and there is nothing left to do here.
3781 * If parent rewriting was not requested, then this is the
3782 * place to perform the line-level log filtering. Notably,
3783 * this check, though expensive, must come before the other,
3784 * cheaper filtering conditions, because the tracked line
3785 * ranges must be adjusted even when the commit will end up
3786 * being ignored based on other conditions.
3788 if (!line_log_process_ranges_arbitrary_commit(revs, commit))
3789 return commit_ignore;
3791 if (revs->min_age != -1 &&
3792 comparison_date(revs, commit) > revs->min_age)
3793 return commit_ignore;
3794 if (revs->min_parents || (revs->max_parents >= 0)) {
3795 int n = commit_list_count(commit->parents);
3796 if ((n < revs->min_parents) ||
3797 ((revs->max_parents >= 0) && (n > revs->max_parents)))
3798 return commit_ignore;
3800 if (!commit_match(commit, revs))
3801 return commit_ignore;
3802 if (revs->prune && revs->dense) {
3803 /* Commit without changes? */
3804 if (commit->object.flags & TREESAME) {
3805 int n;
3806 struct commit_list *p;
3807 /* drop merges unless we want parenthood */
3808 if (!want_ancestry(revs))
3809 return commit_ignore;
3811 if (revs->show_pulls && (commit->object.flags & PULL_MERGE))
3812 return commit_show;
3815 * If we want ancestry, then need to keep any merges
3816 * between relevant commits to tie together topology.
3817 * For consistency with TREESAME and simplification
3818 * use "relevant" here rather than just INTERESTING,
3819 * to treat bottom commit(s) as part of the topology.
3821 for (n = 0, p = commit->parents; p; p = p->next)
3822 if (relevant_commit(p->item))
3823 if (++n >= 2)
3824 return commit_show;
3825 return commit_ignore;
3828 return commit_show;
3831 define_commit_slab(saved_parents, struct commit_list *);
3833 #define EMPTY_PARENT_LIST ((struct commit_list *)-1)
3836 * You may only call save_parents() once per commit (this is checked
3837 * for non-root commits).
3839 static void save_parents(struct rev_info *revs, struct commit *commit)
3841 struct commit_list **pp;
3843 if (!revs->saved_parents_slab) {
3844 revs->saved_parents_slab = xmalloc(sizeof(struct saved_parents));
3845 init_saved_parents(revs->saved_parents_slab);
3848 pp = saved_parents_at(revs->saved_parents_slab, commit);
3851 * When walking with reflogs, we may visit the same commit
3852 * several times: once for each appearance in the reflog.
3854 * In this case, save_parents() will be called multiple times.
3855 * We want to keep only the first set of parents. We need to
3856 * store a sentinel value for an empty (i.e., NULL) parent
3857 * list to distinguish it from a not-yet-saved list, however.
3859 if (*pp)
3860 return;
3861 if (commit->parents)
3862 *pp = copy_commit_list(commit->parents);
3863 else
3864 *pp = EMPTY_PARENT_LIST;
3867 static void free_saved_parents(struct rev_info *revs)
3869 if (revs->saved_parents_slab)
3870 clear_saved_parents(revs->saved_parents_slab);
3873 struct commit_list *get_saved_parents(struct rev_info *revs, const struct commit *commit)
3875 struct commit_list *parents;
3877 if (!revs->saved_parents_slab)
3878 return commit->parents;
3880 parents = *saved_parents_at(revs->saved_parents_slab, commit);
3881 if (parents == EMPTY_PARENT_LIST)
3882 return NULL;
3883 return parents;
3886 enum commit_action simplify_commit(struct rev_info *revs, struct commit *commit)
3888 enum commit_action action = get_commit_action(revs, commit);
3890 if (action == commit_show &&
3891 revs->prune && revs->dense && want_ancestry(revs)) {
3893 * --full-diff on simplified parents is no good: it
3894 * will show spurious changes from the commits that
3895 * were elided. So we save the parents on the side
3896 * when --full-diff is in effect.
3898 if (revs->full_diff)
3899 save_parents(revs, commit);
3900 if (rewrite_parents(revs, commit, rewrite_one) < 0)
3901 return commit_error;
3903 return action;
3906 static void track_linear(struct rev_info *revs, struct commit *commit)
3908 if (revs->track_first_time) {
3909 revs->linear = 1;
3910 revs->track_first_time = 0;
3911 } else {
3912 struct commit_list *p;
3913 for (p = revs->previous_parents; p; p = p->next)
3914 if (p->item == NULL || /* first commit */
3915 oideq(&p->item->object.oid, &commit->object.oid))
3916 break;
3917 revs->linear = p != NULL;
3919 if (revs->reverse) {
3920 if (revs->linear)
3921 commit->object.flags |= TRACK_LINEAR;
3923 free_commit_list(revs->previous_parents);
3924 revs->previous_parents = copy_commit_list(commit->parents);
3927 static struct commit *get_revision_1(struct rev_info *revs)
3929 while (1) {
3930 struct commit *commit;
3932 if (revs->reflog_info)
3933 commit = next_reflog_entry(revs->reflog_info);
3934 else if (revs->topo_walk_info)
3935 commit = next_topo_commit(revs);
3936 else
3937 commit = pop_commit(&revs->commits);
3939 if (!commit)
3940 return NULL;
3942 if (revs->reflog_info)
3943 commit->object.flags &= ~(ADDED | SEEN | SHOWN);
3946 * If we haven't done the list limiting, we need to look at
3947 * the parents here. We also need to do the date-based limiting
3948 * that we'd otherwise have done in limit_list().
3950 if (!revs->limited) {
3951 if (revs->max_age != -1 &&
3952 comparison_date(revs, commit) < revs->max_age)
3953 continue;
3955 if (revs->reflog_info)
3956 try_to_simplify_commit(revs, commit);
3957 else if (revs->topo_walk_info)
3958 expand_topo_walk(revs, commit);
3959 else if (process_parents(revs, commit, &revs->commits, NULL) < 0) {
3960 if (!revs->ignore_missing_links)
3961 die("Failed to traverse parents of commit %s",
3962 oid_to_hex(&commit->object.oid));
3966 switch (simplify_commit(revs, commit)) {
3967 case commit_ignore:
3968 continue;
3969 case commit_error:
3970 die("Failed to simplify parents of commit %s",
3971 oid_to_hex(&commit->object.oid));
3972 default:
3973 if (revs->track_linear)
3974 track_linear(revs, commit);
3975 return commit;
3981 * Return true for entries that have not yet been shown. (This is an
3982 * object_array_each_func_t.)
3984 static int entry_unshown(struct object_array_entry *entry, void *cb_data_unused)
3986 return !(entry->item->flags & SHOWN);
3990 * If array is on the verge of a realloc, garbage-collect any entries
3991 * that have already been shown to try to free up some space.
3993 static void gc_boundary(struct object_array *array)
3995 if (array->nr == array->alloc)
3996 object_array_filter(array, entry_unshown, NULL);
3999 static void create_boundary_commit_list(struct rev_info *revs)
4001 unsigned i;
4002 struct commit *c;
4003 struct object_array *array = &revs->boundary_commits;
4004 struct object_array_entry *objects = array->objects;
4007 * If revs->commits is non-NULL at this point, an error occurred in
4008 * get_revision_1(). Ignore the error and continue printing the
4009 * boundary commits anyway. (This is what the code has always
4010 * done.)
4012 if (revs->commits) {
4013 free_commit_list(revs->commits);
4014 revs->commits = NULL;
4018 * Put all of the actual boundary commits from revs->boundary_commits
4019 * into revs->commits
4021 for (i = 0; i < array->nr; i++) {
4022 c = (struct commit *)(objects[i].item);
4023 if (!c)
4024 continue;
4025 if (!(c->object.flags & CHILD_SHOWN))
4026 continue;
4027 if (c->object.flags & (SHOWN | BOUNDARY))
4028 continue;
4029 c->object.flags |= BOUNDARY;
4030 commit_list_insert(c, &revs->commits);
4034 * If revs->topo_order is set, sort the boundary commits
4035 * in topological order
4037 sort_in_topological_order(&revs->commits, revs->sort_order);
4040 static struct commit *get_revision_internal(struct rev_info *revs)
4042 struct commit *c = NULL;
4043 struct commit_list *l;
4045 if (revs->boundary == 2) {
4047 * All of the normal commits have already been returned,
4048 * and we are now returning boundary commits.
4049 * create_boundary_commit_list() has populated
4050 * revs->commits with the remaining commits to return.
4052 c = pop_commit(&revs->commits);
4053 if (c)
4054 c->object.flags |= SHOWN;
4055 return c;
4059 * If our max_count counter has reached zero, then we are done. We
4060 * don't simply return NULL because we still might need to show
4061 * boundary commits. But we want to avoid calling get_revision_1, which
4062 * might do a considerable amount of work finding the next commit only
4063 * for us to throw it away.
4065 * If it is non-zero, then either we don't have a max_count at all
4066 * (-1), or it is still counting, in which case we decrement.
4068 if (revs->max_count) {
4069 c = get_revision_1(revs);
4070 if (c) {
4071 while (revs->skip_count > 0) {
4072 revs->skip_count--;
4073 c = get_revision_1(revs);
4074 if (!c)
4075 break;
4079 if (revs->max_count > 0)
4080 revs->max_count--;
4083 if (c)
4084 c->object.flags |= SHOWN;
4086 if (!revs->boundary)
4087 return c;
4089 if (!c) {
4091 * get_revision_1() runs out the commits, and
4092 * we are done computing the boundaries.
4093 * switch to boundary commits output mode.
4095 revs->boundary = 2;
4098 * Update revs->commits to contain the list of
4099 * boundary commits.
4101 create_boundary_commit_list(revs);
4103 return get_revision_internal(revs);
4107 * boundary commits are the commits that are parents of the
4108 * ones we got from get_revision_1() but they themselves are
4109 * not returned from get_revision_1(). Before returning
4110 * 'c', we need to mark its parents that they could be boundaries.
4113 for (l = c->parents; l; l = l->next) {
4114 struct object *p;
4115 p = &(l->item->object);
4116 if (p->flags & (CHILD_SHOWN | SHOWN))
4117 continue;
4118 p->flags |= CHILD_SHOWN;
4119 gc_boundary(&revs->boundary_commits);
4120 add_object_array(p, NULL, &revs->boundary_commits);
4123 return c;
4126 struct commit *get_revision(struct rev_info *revs)
4128 struct commit *c;
4129 struct commit_list *reversed;
4131 if (revs->reverse) {
4132 reversed = NULL;
4133 while ((c = get_revision_internal(revs)))
4134 commit_list_insert(c, &reversed);
4135 revs->commits = reversed;
4136 revs->reverse = 0;
4137 revs->reverse_output_stage = 1;
4140 if (revs->reverse_output_stage) {
4141 c = pop_commit(&revs->commits);
4142 if (revs->track_linear)
4143 revs->linear = !!(c && c->object.flags & TRACK_LINEAR);
4144 return c;
4147 c = get_revision_internal(revs);
4148 if (c && revs->graph)
4149 graph_update(revs->graph, c);
4150 if (!c) {
4151 free_saved_parents(revs);
4152 if (revs->previous_parents) {
4153 free_commit_list(revs->previous_parents);
4154 revs->previous_parents = NULL;
4157 return c;
4160 const char *get_revision_mark(const struct rev_info *revs, const struct commit *commit)
4162 if (commit->object.flags & BOUNDARY)
4163 return "-";
4164 else if (commit->object.flags & UNINTERESTING)
4165 return "^";
4166 else if (commit->object.flags & PATCHSAME)
4167 return "=";
4168 else if (!revs || revs->left_right) {
4169 if (commit->object.flags & SYMMETRIC_LEFT)
4170 return "<";
4171 else
4172 return ">";
4173 } else if (revs->graph)
4174 return "*";
4175 else if (revs->cherry_mark)
4176 return "+";
4177 return "";
4180 void put_revision_mark(const struct rev_info *revs, const struct commit *commit)
4182 const char *mark = get_revision_mark(revs, commit);
4183 if (!strlen(mark))
4184 return;
4185 fputs(mark, stdout);
4186 putchar(' ');