commit-graph: unify the signatures of all write_graph_chunk_*() functions
[git/debian.git] / revision.c
blob7339750af12a8f49d505d968292dc039dab3cb0f
1 #include "cache.h"
2 #include "object-store.h"
3 #include "tag.h"
4 #include "blob.h"
5 #include "tree.h"
6 #include "commit.h"
7 #include "diff.h"
8 #include "refs.h"
9 #include "revision.h"
10 #include "repository.h"
11 #include "graph.h"
12 #include "grep.h"
13 #include "reflog-walk.h"
14 #include "patch-ids.h"
15 #include "decorate.h"
16 #include "log-tree.h"
17 #include "string-list.h"
18 #include "line-log.h"
19 #include "mailmap.h"
20 #include "commit-slab.h"
21 #include "dir.h"
22 #include "cache-tree.h"
23 #include "bisect.h"
24 #include "packfile.h"
25 #include "worktree.h"
26 #include "argv-array.h"
27 #include "commit-reach.h"
28 #include "commit-graph.h"
29 #include "prio-queue.h"
30 #include "hashmap.h"
31 #include "utf8.h"
32 #include "bloom.h"
33 #include "json-writer.h"
35 volatile show_early_output_fn_t show_early_output;
37 static const char *term_bad;
38 static const char *term_good;
40 implement_shared_commit_slab(revision_sources, char *);
42 static inline int want_ancestry(const struct rev_info *revs);
44 void show_object_with_name(FILE *out, struct object *obj, const char *name)
46 const char *p;
48 fprintf(out, "%s ", oid_to_hex(&obj->oid));
49 for (p = name; *p && *p != '\n'; p++)
50 fputc(*p, out);
51 fputc('\n', out);
54 static void mark_blob_uninteresting(struct blob *blob)
56 if (!blob)
57 return;
58 if (blob->object.flags & UNINTERESTING)
59 return;
60 blob->object.flags |= UNINTERESTING;
63 static void mark_tree_contents_uninteresting(struct repository *r,
64 struct tree *tree)
66 struct tree_desc desc;
67 struct name_entry entry;
69 if (parse_tree_gently(tree, 1) < 0)
70 return;
72 init_tree_desc(&desc, tree->buffer, tree->size);
73 while (tree_entry(&desc, &entry)) {
74 switch (object_type(entry.mode)) {
75 case OBJ_TREE:
76 mark_tree_uninteresting(r, lookup_tree(r, &entry.oid));
77 break;
78 case OBJ_BLOB:
79 mark_blob_uninteresting(lookup_blob(r, &entry.oid));
80 break;
81 default:
82 /* Subproject commit - not in this repository */
83 break;
88 * We don't care about the tree any more
89 * after it has been marked uninteresting.
91 free_tree_buffer(tree);
94 void mark_tree_uninteresting(struct repository *r, struct tree *tree)
96 struct object *obj;
98 if (!tree)
99 return;
101 obj = &tree->object;
102 if (obj->flags & UNINTERESTING)
103 return;
104 obj->flags |= UNINTERESTING;
105 mark_tree_contents_uninteresting(r, tree);
108 struct path_and_oids_entry {
109 struct hashmap_entry ent;
110 char *path;
111 struct oidset trees;
114 static int path_and_oids_cmp(const void *hashmap_cmp_fn_data,
115 const struct hashmap_entry *eptr,
116 const struct hashmap_entry *entry_or_key,
117 const void *keydata)
119 const struct path_and_oids_entry *e1, *e2;
121 e1 = container_of(eptr, const struct path_and_oids_entry, ent);
122 e2 = container_of(entry_or_key, const struct path_and_oids_entry, ent);
124 return strcmp(e1->path, e2->path);
127 static void paths_and_oids_init(struct hashmap *map)
129 hashmap_init(map, path_and_oids_cmp, NULL, 0);
132 static void paths_and_oids_clear(struct hashmap *map)
134 struct hashmap_iter iter;
135 struct path_and_oids_entry *entry;
137 hashmap_for_each_entry(map, &iter, entry, ent /* member name */) {
138 oidset_clear(&entry->trees);
139 free(entry->path);
142 hashmap_free_entries(map, struct path_and_oids_entry, ent);
145 static void paths_and_oids_insert(struct hashmap *map,
146 const char *path,
147 const struct object_id *oid)
149 int hash = strhash(path);
150 struct path_and_oids_entry key;
151 struct path_and_oids_entry *entry;
153 hashmap_entry_init(&key.ent, hash);
155 /* use a shallow copy for the lookup */
156 key.path = (char *)path;
157 oidset_init(&key.trees, 0);
159 entry = hashmap_get_entry(map, &key, ent, NULL);
160 if (!entry) {
161 entry = xcalloc(1, sizeof(struct path_and_oids_entry));
162 hashmap_entry_init(&entry->ent, hash);
163 entry->path = xstrdup(key.path);
164 oidset_init(&entry->trees, 16);
165 hashmap_put(map, &entry->ent);
168 oidset_insert(&entry->trees, oid);
171 static void add_children_by_path(struct repository *r,
172 struct tree *tree,
173 struct hashmap *map)
175 struct tree_desc desc;
176 struct name_entry entry;
178 if (!tree)
179 return;
181 if (parse_tree_gently(tree, 1) < 0)
182 return;
184 init_tree_desc(&desc, tree->buffer, tree->size);
185 while (tree_entry(&desc, &entry)) {
186 switch (object_type(entry.mode)) {
187 case OBJ_TREE:
188 paths_and_oids_insert(map, entry.path, &entry.oid);
190 if (tree->object.flags & UNINTERESTING) {
191 struct tree *child = lookup_tree(r, &entry.oid);
192 if (child)
193 child->object.flags |= UNINTERESTING;
195 break;
196 case OBJ_BLOB:
197 if (tree->object.flags & UNINTERESTING) {
198 struct blob *child = lookup_blob(r, &entry.oid);
199 if (child)
200 child->object.flags |= UNINTERESTING;
202 break;
203 default:
204 /* Subproject commit - not in this repository */
205 break;
209 free_tree_buffer(tree);
212 void mark_trees_uninteresting_sparse(struct repository *r,
213 struct oidset *trees)
215 unsigned has_interesting = 0, has_uninteresting = 0;
216 struct hashmap map;
217 struct hashmap_iter map_iter;
218 struct path_and_oids_entry *entry;
219 struct object_id *oid;
220 struct oidset_iter iter;
222 oidset_iter_init(trees, &iter);
223 while ((!has_interesting || !has_uninteresting) &&
224 (oid = oidset_iter_next(&iter))) {
225 struct tree *tree = lookup_tree(r, oid);
227 if (!tree)
228 continue;
230 if (tree->object.flags & UNINTERESTING)
231 has_uninteresting = 1;
232 else
233 has_interesting = 1;
236 /* Do not walk unless we have both types of trees. */
237 if (!has_uninteresting || !has_interesting)
238 return;
240 paths_and_oids_init(&map);
242 oidset_iter_init(trees, &iter);
243 while ((oid = oidset_iter_next(&iter))) {
244 struct tree *tree = lookup_tree(r, oid);
245 add_children_by_path(r, tree, &map);
248 hashmap_for_each_entry(&map, &map_iter, entry, ent /* member name */)
249 mark_trees_uninteresting_sparse(r, &entry->trees);
251 paths_and_oids_clear(&map);
254 struct commit_stack {
255 struct commit **items;
256 size_t nr, alloc;
258 #define COMMIT_STACK_INIT { NULL, 0, 0 }
260 static void commit_stack_push(struct commit_stack *stack, struct commit *commit)
262 ALLOC_GROW(stack->items, stack->nr + 1, stack->alloc);
263 stack->items[stack->nr++] = commit;
266 static struct commit *commit_stack_pop(struct commit_stack *stack)
268 return stack->nr ? stack->items[--stack->nr] : NULL;
271 static void commit_stack_clear(struct commit_stack *stack)
273 FREE_AND_NULL(stack->items);
274 stack->nr = stack->alloc = 0;
277 static void mark_one_parent_uninteresting(struct commit *commit,
278 struct commit_stack *pending)
280 struct commit_list *l;
282 if (commit->object.flags & UNINTERESTING)
283 return;
284 commit->object.flags |= UNINTERESTING;
287 * Normally we haven't parsed the parent
288 * yet, so we won't have a parent of a parent
289 * here. However, it may turn out that we've
290 * reached this commit some other way (where it
291 * wasn't uninteresting), in which case we need
292 * to mark its parents recursively too..
294 for (l = commit->parents; l; l = l->next)
295 commit_stack_push(pending, l->item);
298 void mark_parents_uninteresting(struct commit *commit)
300 struct commit_stack pending = COMMIT_STACK_INIT;
301 struct commit_list *l;
303 for (l = commit->parents; l; l = l->next)
304 mark_one_parent_uninteresting(l->item, &pending);
306 while (pending.nr > 0)
307 mark_one_parent_uninteresting(commit_stack_pop(&pending),
308 &pending);
310 commit_stack_clear(&pending);
313 static void add_pending_object_with_path(struct rev_info *revs,
314 struct object *obj,
315 const char *name, unsigned mode,
316 const char *path)
318 if (!obj)
319 return;
320 if (revs->no_walk && (obj->flags & UNINTERESTING))
321 revs->no_walk = 0;
322 if (revs->reflog_info && obj->type == OBJ_COMMIT) {
323 struct strbuf buf = STRBUF_INIT;
324 int len = interpret_branch_name(name, 0, &buf, 0);
326 if (0 < len && name[len] && buf.len)
327 strbuf_addstr(&buf, name + len);
328 add_reflog_for_walk(revs->reflog_info,
329 (struct commit *)obj,
330 buf.buf[0] ? buf.buf: name);
331 strbuf_release(&buf);
332 return; /* do not add the commit itself */
334 add_object_array_with_path(obj, name, &revs->pending, mode, path);
337 static void add_pending_object_with_mode(struct rev_info *revs,
338 struct object *obj,
339 const char *name, unsigned mode)
341 add_pending_object_with_path(revs, obj, name, mode, NULL);
344 void add_pending_object(struct rev_info *revs,
345 struct object *obj, const char *name)
347 add_pending_object_with_mode(revs, obj, name, S_IFINVALID);
350 void add_head_to_pending(struct rev_info *revs)
352 struct object_id oid;
353 struct object *obj;
354 if (get_oid("HEAD", &oid))
355 return;
356 obj = parse_object(revs->repo, &oid);
357 if (!obj)
358 return;
359 add_pending_object(revs, obj, "HEAD");
362 static struct object *get_reference(struct rev_info *revs, const char *name,
363 const struct object_id *oid,
364 unsigned int flags)
366 struct object *object;
369 * If the repository has commit graphs, repo_parse_commit() avoids
370 * reading the object buffer, so use it whenever possible.
372 if (oid_object_info(revs->repo, oid, NULL) == OBJ_COMMIT) {
373 struct commit *c = lookup_commit(revs->repo, oid);
374 if (!repo_parse_commit(revs->repo, c))
375 object = (struct object *) c;
376 else
377 object = NULL;
378 } else {
379 object = parse_object(revs->repo, oid);
382 if (!object) {
383 if (revs->ignore_missing)
384 return object;
385 if (revs->exclude_promisor_objects && is_promisor_object(oid))
386 return NULL;
387 die("bad object %s", name);
389 object->flags |= flags;
390 return object;
393 void add_pending_oid(struct rev_info *revs, const char *name,
394 const struct object_id *oid, unsigned int flags)
396 struct object *object = get_reference(revs, name, oid, flags);
397 add_pending_object(revs, object, name);
400 static struct commit *handle_commit(struct rev_info *revs,
401 struct object_array_entry *entry)
403 struct object *object = entry->item;
404 const char *name = entry->name;
405 const char *path = entry->path;
406 unsigned int mode = entry->mode;
407 unsigned long flags = object->flags;
410 * Tag object? Look what it points to..
412 while (object->type == OBJ_TAG) {
413 struct tag *tag = (struct tag *) object;
414 if (revs->tag_objects && !(flags & UNINTERESTING))
415 add_pending_object(revs, object, tag->tag);
416 object = parse_object(revs->repo, get_tagged_oid(tag));
417 if (!object) {
418 if (revs->ignore_missing_links || (flags & UNINTERESTING))
419 return NULL;
420 if (revs->exclude_promisor_objects &&
421 is_promisor_object(&tag->tagged->oid))
422 return NULL;
423 die("bad object %s", oid_to_hex(&tag->tagged->oid));
425 object->flags |= flags;
427 * We'll handle the tagged object by looping or dropping
428 * through to the non-tag handlers below. Do not
429 * propagate path data from the tag's pending entry.
431 path = NULL;
432 mode = 0;
436 * Commit object? Just return it, we'll do all the complex
437 * reachability crud.
439 if (object->type == OBJ_COMMIT) {
440 struct commit *commit = (struct commit *)object;
442 if (parse_commit(commit) < 0)
443 die("unable to parse commit %s", name);
444 if (flags & UNINTERESTING) {
445 mark_parents_uninteresting(commit);
447 if (!revs->topo_order || !generation_numbers_enabled(the_repository))
448 revs->limited = 1;
450 if (revs->sources) {
451 char **slot = revision_sources_at(revs->sources, commit);
453 if (!*slot)
454 *slot = xstrdup(name);
456 return commit;
460 * Tree object? Either mark it uninteresting, or add it
461 * to the list of objects to look at later..
463 if (object->type == OBJ_TREE) {
464 struct tree *tree = (struct tree *)object;
465 if (!revs->tree_objects)
466 return NULL;
467 if (flags & UNINTERESTING) {
468 mark_tree_contents_uninteresting(revs->repo, tree);
469 return NULL;
471 add_pending_object_with_path(revs, object, name, mode, path);
472 return NULL;
476 * Blob object? You know the drill by now..
478 if (object->type == OBJ_BLOB) {
479 if (!revs->blob_objects)
480 return NULL;
481 if (flags & UNINTERESTING)
482 return NULL;
483 add_pending_object_with_path(revs, object, name, mode, path);
484 return NULL;
486 die("%s is unknown object", name);
489 static int everybody_uninteresting(struct commit_list *orig,
490 struct commit **interesting_cache)
492 struct commit_list *list = orig;
494 if (*interesting_cache) {
495 struct commit *commit = *interesting_cache;
496 if (!(commit->object.flags & UNINTERESTING))
497 return 0;
500 while (list) {
501 struct commit *commit = list->item;
502 list = list->next;
503 if (commit->object.flags & UNINTERESTING)
504 continue;
506 *interesting_cache = commit;
507 return 0;
509 return 1;
513 * A definition of "relevant" commit that we can use to simplify limited graphs
514 * by eliminating side branches.
516 * A "relevant" commit is one that is !UNINTERESTING (ie we are including it
517 * in our list), or that is a specified BOTTOM commit. Then after computing
518 * a limited list, during processing we can generally ignore boundary merges
519 * coming from outside the graph, (ie from irrelevant parents), and treat
520 * those merges as if they were single-parent. TREESAME is defined to consider
521 * only relevant parents, if any. If we are TREESAME to our on-graph parents,
522 * we don't care if we were !TREESAME to non-graph parents.
524 * Treating bottom commits as relevant ensures that a limited graph's
525 * connection to the actual bottom commit is not viewed as a side branch, but
526 * treated as part of the graph. For example:
528 * ....Z...A---X---o---o---B
529 * . /
530 * W---Y
532 * When computing "A..B", the A-X connection is at least as important as
533 * Y-X, despite A being flagged UNINTERESTING.
535 * And when computing --ancestry-path "A..B", the A-X connection is more
536 * important than Y-X, despite both A and Y being flagged UNINTERESTING.
538 static inline int relevant_commit(struct commit *commit)
540 return (commit->object.flags & (UNINTERESTING | BOTTOM)) != UNINTERESTING;
544 * Return a single relevant commit from a parent list. If we are a TREESAME
545 * commit, and this selects one of our parents, then we can safely simplify to
546 * that parent.
548 static struct commit *one_relevant_parent(const struct rev_info *revs,
549 struct commit_list *orig)
551 struct commit_list *list = orig;
552 struct commit *relevant = NULL;
554 if (!orig)
555 return NULL;
558 * For 1-parent commits, or if first-parent-only, then return that
559 * first parent (even if not "relevant" by the above definition).
560 * TREESAME will have been set purely on that parent.
562 if (revs->first_parent_only || !orig->next)
563 return orig->item;
566 * For multi-parent commits, identify a sole relevant parent, if any.
567 * If we have only one relevant parent, then TREESAME will be set purely
568 * with regard to that parent, and we can simplify accordingly.
570 * If we have more than one relevant parent, or no relevant parents
571 * (and multiple irrelevant ones), then we can't select a parent here
572 * and return NULL.
574 while (list) {
575 struct commit *commit = list->item;
576 list = list->next;
577 if (relevant_commit(commit)) {
578 if (relevant)
579 return NULL;
580 relevant = commit;
583 return relevant;
587 * The goal is to get REV_TREE_NEW as the result only if the
588 * diff consists of all '+' (and no other changes), REV_TREE_OLD
589 * if the whole diff is removal of old data, and otherwise
590 * REV_TREE_DIFFERENT (of course if the trees are the same we
591 * want REV_TREE_SAME).
593 * The only time we care about the distinction is when
594 * remove_empty_trees is in effect, in which case we care only about
595 * whether the whole change is REV_TREE_NEW, or if there's another type
596 * of change. Which means we can stop the diff early in either of these
597 * cases:
599 * 1. We're not using remove_empty_trees at all.
601 * 2. We saw anything except REV_TREE_NEW.
603 static int tree_difference = REV_TREE_SAME;
605 static void file_add_remove(struct diff_options *options,
606 int addremove, unsigned mode,
607 const struct object_id *oid,
608 int oid_valid,
609 const char *fullpath, unsigned dirty_submodule)
611 int diff = addremove == '+' ? REV_TREE_NEW : REV_TREE_OLD;
612 struct rev_info *revs = options->change_fn_data;
614 tree_difference |= diff;
615 if (!revs->remove_empty_trees || tree_difference != REV_TREE_NEW)
616 options->flags.has_changes = 1;
619 static void file_change(struct diff_options *options,
620 unsigned old_mode, unsigned new_mode,
621 const struct object_id *old_oid,
622 const struct object_id *new_oid,
623 int old_oid_valid, int new_oid_valid,
624 const char *fullpath,
625 unsigned old_dirty_submodule, unsigned new_dirty_submodule)
627 tree_difference = REV_TREE_DIFFERENT;
628 options->flags.has_changes = 1;
631 static int bloom_filter_atexit_registered;
632 static unsigned int count_bloom_filter_maybe;
633 static unsigned int count_bloom_filter_definitely_not;
634 static unsigned int count_bloom_filter_false_positive;
635 static unsigned int count_bloom_filter_not_present;
637 static void trace2_bloom_filter_statistics_atexit(void)
639 struct json_writer jw = JSON_WRITER_INIT;
641 jw_object_begin(&jw, 0);
642 jw_object_intmax(&jw, "filter_not_present", count_bloom_filter_not_present);
643 jw_object_intmax(&jw, "maybe", count_bloom_filter_maybe);
644 jw_object_intmax(&jw, "definitely_not", count_bloom_filter_definitely_not);
645 jw_object_intmax(&jw, "false_positive", count_bloom_filter_false_positive);
646 jw_end(&jw);
648 trace2_data_json("bloom", the_repository, "statistics", &jw);
650 jw_release(&jw);
653 static int forbid_bloom_filters(struct pathspec *spec)
655 if (spec->has_wildcard)
656 return 1;
657 if (spec->nr > 1)
658 return 1;
659 if (spec->magic & ~PATHSPEC_LITERAL)
660 return 1;
661 if (spec->nr && (spec->items[0].magic & ~PATHSPEC_LITERAL))
662 return 1;
664 return 0;
667 static void prepare_to_use_bloom_filter(struct rev_info *revs)
669 struct pathspec_item *pi;
670 char *path_alloc = NULL;
671 const char *path;
672 int last_index;
673 int len;
675 if (!revs->commits)
676 return;
678 if (forbid_bloom_filters(&revs->prune_data))
679 return;
681 repo_parse_commit(revs->repo, revs->commits->item);
683 if (!revs->repo->objects->commit_graph)
684 return;
686 revs->bloom_filter_settings = revs->repo->objects->commit_graph->bloom_filter_settings;
687 if (!revs->bloom_filter_settings)
688 return;
690 if (!revs->pruning.pathspec.nr)
691 return;
693 pi = &revs->pruning.pathspec.items[0];
694 last_index = pi->len - 1;
696 /* remove single trailing slash from path, if needed */
697 if (pi->match[last_index] == '/') {
698 path_alloc = xstrdup(pi->match);
699 path_alloc[last_index] = '\0';
700 path = path_alloc;
701 } else
702 path = pi->match;
704 len = strlen(path);
706 revs->bloom_key = xmalloc(sizeof(struct bloom_key));
707 fill_bloom_key(path, len, revs->bloom_key, revs->bloom_filter_settings);
709 if (trace2_is_enabled() && !bloom_filter_atexit_registered) {
710 atexit(trace2_bloom_filter_statistics_atexit);
711 bloom_filter_atexit_registered = 1;
714 free(path_alloc);
717 static int check_maybe_different_in_bloom_filter(struct rev_info *revs,
718 struct commit *commit)
720 struct bloom_filter *filter;
721 int result;
723 if (!revs->repo->objects->commit_graph)
724 return -1;
726 if (commit->generation == GENERATION_NUMBER_INFINITY)
727 return -1;
729 filter = get_bloom_filter(revs->repo, commit, 0);
731 if (!filter) {
732 count_bloom_filter_not_present++;
733 return -1;
736 result = bloom_filter_contains(filter,
737 revs->bloom_key,
738 revs->bloom_filter_settings);
740 if (result)
741 count_bloom_filter_maybe++;
742 else
743 count_bloom_filter_definitely_not++;
745 return result;
748 static int rev_compare_tree(struct rev_info *revs,
749 struct commit *parent, struct commit *commit, int nth_parent)
751 struct tree *t1 = get_commit_tree(parent);
752 struct tree *t2 = get_commit_tree(commit);
753 int bloom_ret = 1;
755 if (!t1)
756 return REV_TREE_NEW;
757 if (!t2)
758 return REV_TREE_OLD;
760 if (revs->simplify_by_decoration) {
762 * If we are simplifying by decoration, then the commit
763 * is worth showing if it has a tag pointing at it.
765 if (get_name_decoration(&commit->object))
766 return REV_TREE_DIFFERENT;
768 * A commit that is not pointed by a tag is uninteresting
769 * if we are not limited by path. This means that you will
770 * see the usual "commits that touch the paths" plus any
771 * tagged commit by specifying both --simplify-by-decoration
772 * and pathspec.
774 if (!revs->prune_data.nr)
775 return REV_TREE_SAME;
778 if (revs->bloom_key && !nth_parent) {
779 bloom_ret = check_maybe_different_in_bloom_filter(revs, commit);
781 if (bloom_ret == 0)
782 return REV_TREE_SAME;
785 tree_difference = REV_TREE_SAME;
786 revs->pruning.flags.has_changes = 0;
787 diff_tree_oid(&t1->object.oid, &t2->object.oid, "", &revs->pruning);
789 if (!nth_parent)
790 if (bloom_ret == 1 && tree_difference == REV_TREE_SAME)
791 count_bloom_filter_false_positive++;
793 return tree_difference;
796 static int rev_same_tree_as_empty(struct rev_info *revs, struct commit *commit)
798 struct tree *t1 = get_commit_tree(commit);
800 if (!t1)
801 return 0;
803 tree_difference = REV_TREE_SAME;
804 revs->pruning.flags.has_changes = 0;
805 diff_tree_oid(NULL, &t1->object.oid, "", &revs->pruning);
807 return tree_difference == REV_TREE_SAME;
810 struct treesame_state {
811 unsigned int nparents;
812 unsigned char treesame[FLEX_ARRAY];
815 static struct treesame_state *initialise_treesame(struct rev_info *revs, struct commit *commit)
817 unsigned n = commit_list_count(commit->parents);
818 struct treesame_state *st = xcalloc(1, st_add(sizeof(*st), n));
819 st->nparents = n;
820 add_decoration(&revs->treesame, &commit->object, st);
821 return st;
825 * Must be called immediately after removing the nth_parent from a commit's
826 * parent list, if we are maintaining the per-parent treesame[] decoration.
827 * This does not recalculate the master TREESAME flag - update_treesame()
828 * should be called to update it after a sequence of treesame[] modifications
829 * that may have affected it.
831 static int compact_treesame(struct rev_info *revs, struct commit *commit, unsigned nth_parent)
833 struct treesame_state *st;
834 int old_same;
836 if (!commit->parents) {
838 * Have just removed the only parent from a non-merge.
839 * Different handling, as we lack decoration.
841 if (nth_parent != 0)
842 die("compact_treesame %u", nth_parent);
843 old_same = !!(commit->object.flags & TREESAME);
844 if (rev_same_tree_as_empty(revs, commit))
845 commit->object.flags |= TREESAME;
846 else
847 commit->object.flags &= ~TREESAME;
848 return old_same;
851 st = lookup_decoration(&revs->treesame, &commit->object);
852 if (!st || nth_parent >= st->nparents)
853 die("compact_treesame %u", nth_parent);
855 old_same = st->treesame[nth_parent];
856 memmove(st->treesame + nth_parent,
857 st->treesame + nth_parent + 1,
858 st->nparents - nth_parent - 1);
861 * If we've just become a non-merge commit, update TREESAME
862 * immediately, and remove the no-longer-needed decoration.
863 * If still a merge, defer update until update_treesame().
865 if (--st->nparents == 1) {
866 if (commit->parents->next)
867 die("compact_treesame parents mismatch");
868 if (st->treesame[0] && revs->dense)
869 commit->object.flags |= TREESAME;
870 else
871 commit->object.flags &= ~TREESAME;
872 free(add_decoration(&revs->treesame, &commit->object, NULL));
875 return old_same;
878 static unsigned update_treesame(struct rev_info *revs, struct commit *commit)
880 if (commit->parents && commit->parents->next) {
881 unsigned n;
882 struct treesame_state *st;
883 struct commit_list *p;
884 unsigned relevant_parents;
885 unsigned relevant_change, irrelevant_change;
887 st = lookup_decoration(&revs->treesame, &commit->object);
888 if (!st)
889 die("update_treesame %s", oid_to_hex(&commit->object.oid));
890 relevant_parents = 0;
891 relevant_change = irrelevant_change = 0;
892 for (p = commit->parents, n = 0; p; n++, p = p->next) {
893 if (relevant_commit(p->item)) {
894 relevant_change |= !st->treesame[n];
895 relevant_parents++;
896 } else
897 irrelevant_change |= !st->treesame[n];
899 if (relevant_parents ? relevant_change : irrelevant_change)
900 commit->object.flags &= ~TREESAME;
901 else
902 commit->object.flags |= TREESAME;
905 return commit->object.flags & TREESAME;
908 static inline int limiting_can_increase_treesame(const struct rev_info *revs)
911 * TREESAME is irrelevant unless prune && dense;
912 * if simplify_history is set, we can't have a mixture of TREESAME and
913 * !TREESAME INTERESTING parents (and we don't have treesame[]
914 * decoration anyway);
915 * if first_parent_only is set, then the TREESAME flag is locked
916 * against the first parent (and again we lack treesame[] decoration).
918 return revs->prune && revs->dense &&
919 !revs->simplify_history &&
920 !revs->first_parent_only;
923 static void try_to_simplify_commit(struct rev_info *revs, struct commit *commit)
925 struct commit_list **pp, *parent;
926 struct treesame_state *ts = NULL;
927 int relevant_change = 0, irrelevant_change = 0;
928 int relevant_parents, nth_parent;
931 * If we don't do pruning, everything is interesting
933 if (!revs->prune)
934 return;
936 if (!get_commit_tree(commit))
937 return;
939 if (!commit->parents) {
940 if (rev_same_tree_as_empty(revs, commit))
941 commit->object.flags |= TREESAME;
942 return;
946 * Normal non-merge commit? If we don't want to make the
947 * history dense, we consider it always to be a change..
949 if (!revs->dense && !commit->parents->next)
950 return;
952 for (pp = &commit->parents, nth_parent = 0, relevant_parents = 0;
953 (parent = *pp) != NULL;
954 pp = &parent->next, nth_parent++) {
955 struct commit *p = parent->item;
956 if (relevant_commit(p))
957 relevant_parents++;
959 if (nth_parent == 1) {
961 * This our second loop iteration - so we now know
962 * we're dealing with a merge.
964 * Do not compare with later parents when we care only about
965 * the first parent chain, in order to avoid derailing the
966 * traversal to follow a side branch that brought everything
967 * in the path we are limited to by the pathspec.
969 if (revs->first_parent_only)
970 break;
972 * If this will remain a potentially-simplifiable
973 * merge, remember per-parent treesame if needed.
974 * Initialise the array with the comparison from our
975 * first iteration.
977 if (revs->treesame.name &&
978 !revs->simplify_history &&
979 !(commit->object.flags & UNINTERESTING)) {
980 ts = initialise_treesame(revs, commit);
981 if (!(irrelevant_change || relevant_change))
982 ts->treesame[0] = 1;
985 if (parse_commit(p) < 0)
986 die("cannot simplify commit %s (because of %s)",
987 oid_to_hex(&commit->object.oid),
988 oid_to_hex(&p->object.oid));
989 switch (rev_compare_tree(revs, p, commit, nth_parent)) {
990 case REV_TREE_SAME:
991 if (!revs->simplify_history || !relevant_commit(p)) {
992 /* Even if a merge with an uninteresting
993 * side branch brought the entire change
994 * we are interested in, we do not want
995 * to lose the other branches of this
996 * merge, so we just keep going.
998 if (ts)
999 ts->treesame[nth_parent] = 1;
1000 continue;
1002 parent->next = NULL;
1003 commit->parents = parent;
1004 commit->object.flags |= TREESAME;
1005 return;
1007 case REV_TREE_NEW:
1008 if (revs->remove_empty_trees &&
1009 rev_same_tree_as_empty(revs, p)) {
1010 /* We are adding all the specified
1011 * paths from this parent, so the
1012 * history beyond this parent is not
1013 * interesting. Remove its parents
1014 * (they are grandparents for us).
1015 * IOW, we pretend this parent is a
1016 * "root" commit.
1018 if (parse_commit(p) < 0)
1019 die("cannot simplify commit %s (invalid %s)",
1020 oid_to_hex(&commit->object.oid),
1021 oid_to_hex(&p->object.oid));
1022 p->parents = NULL;
1024 /* fallthrough */
1025 case REV_TREE_OLD:
1026 case REV_TREE_DIFFERENT:
1027 if (relevant_commit(p))
1028 relevant_change = 1;
1029 else
1030 irrelevant_change = 1;
1031 continue;
1033 die("bad tree compare for commit %s", oid_to_hex(&commit->object.oid));
1037 * TREESAME is straightforward for single-parent commits. For merge
1038 * commits, it is most useful to define it so that "irrelevant"
1039 * parents cannot make us !TREESAME - if we have any relevant
1040 * parents, then we only consider TREESAMEness with respect to them,
1041 * allowing irrelevant merges from uninteresting branches to be
1042 * simplified away. Only if we have only irrelevant parents do we
1043 * base TREESAME on them. Note that this logic is replicated in
1044 * update_treesame, which should be kept in sync.
1046 if (relevant_parents ? !relevant_change : !irrelevant_change)
1047 commit->object.flags |= TREESAME;
1050 static int process_parents(struct rev_info *revs, struct commit *commit,
1051 struct commit_list **list, struct prio_queue *queue)
1053 struct commit_list *parent = commit->parents;
1054 unsigned left_flag;
1056 if (commit->object.flags & ADDED)
1057 return 0;
1058 commit->object.flags |= ADDED;
1060 if (revs->include_check &&
1061 !revs->include_check(commit, revs->include_check_data))
1062 return 0;
1065 * If the commit is uninteresting, don't try to
1066 * prune parents - we want the maximal uninteresting
1067 * set.
1069 * Normally we haven't parsed the parent
1070 * yet, so we won't have a parent of a parent
1071 * here. However, it may turn out that we've
1072 * reached this commit some other way (where it
1073 * wasn't uninteresting), in which case we need
1074 * to mark its parents recursively too..
1076 if (commit->object.flags & UNINTERESTING) {
1077 while (parent) {
1078 struct commit *p = parent->item;
1079 parent = parent->next;
1080 if (p)
1081 p->object.flags |= UNINTERESTING;
1082 if (parse_commit_gently(p, 1) < 0)
1083 continue;
1084 if (p->parents)
1085 mark_parents_uninteresting(p);
1086 if (p->object.flags & SEEN)
1087 continue;
1088 p->object.flags |= SEEN;
1089 if (list)
1090 commit_list_insert_by_date(p, list);
1091 if (queue)
1092 prio_queue_put(queue, p);
1094 return 0;
1098 * Ok, the commit wasn't uninteresting. Try to
1099 * simplify the commit history and find the parent
1100 * that has no differences in the path set if one exists.
1102 try_to_simplify_commit(revs, commit);
1104 if (revs->no_walk)
1105 return 0;
1107 left_flag = (commit->object.flags & SYMMETRIC_LEFT);
1109 for (parent = commit->parents; parent; parent = parent->next) {
1110 struct commit *p = parent->item;
1111 int gently = revs->ignore_missing_links ||
1112 revs->exclude_promisor_objects;
1113 if (parse_commit_gently(p, gently) < 0) {
1114 if (revs->exclude_promisor_objects &&
1115 is_promisor_object(&p->object.oid)) {
1116 if (revs->first_parent_only)
1117 break;
1118 continue;
1120 return -1;
1122 if (revs->sources) {
1123 char **slot = revision_sources_at(revs->sources, p);
1125 if (!*slot)
1126 *slot = *revision_sources_at(revs->sources, commit);
1128 p->object.flags |= left_flag;
1129 if (!(p->object.flags & SEEN)) {
1130 p->object.flags |= SEEN;
1131 if (list)
1132 commit_list_insert_by_date(p, list);
1133 if (queue)
1134 prio_queue_put(queue, p);
1136 if (revs->first_parent_only)
1137 break;
1139 return 0;
1142 static void cherry_pick_list(struct commit_list *list, struct rev_info *revs)
1144 struct commit_list *p;
1145 int left_count = 0, right_count = 0;
1146 int left_first;
1147 struct patch_ids ids;
1148 unsigned cherry_flag;
1150 /* First count the commits on the left and on the right */
1151 for (p = list; p; p = p->next) {
1152 struct commit *commit = p->item;
1153 unsigned flags = commit->object.flags;
1154 if (flags & BOUNDARY)
1156 else if (flags & SYMMETRIC_LEFT)
1157 left_count++;
1158 else
1159 right_count++;
1162 if (!left_count || !right_count)
1163 return;
1165 left_first = left_count < right_count;
1166 init_patch_ids(revs->repo, &ids);
1167 ids.diffopts.pathspec = revs->diffopt.pathspec;
1169 /* Compute patch-ids for one side */
1170 for (p = list; p; p = p->next) {
1171 struct commit *commit = p->item;
1172 unsigned flags = commit->object.flags;
1174 if (flags & BOUNDARY)
1175 continue;
1177 * If we have fewer left, left_first is set and we omit
1178 * commits on the right branch in this loop. If we have
1179 * fewer right, we skip the left ones.
1181 if (left_first != !!(flags & SYMMETRIC_LEFT))
1182 continue;
1183 add_commit_patch_id(commit, &ids);
1186 /* either cherry_mark or cherry_pick are true */
1187 cherry_flag = revs->cherry_mark ? PATCHSAME : SHOWN;
1189 /* Check the other side */
1190 for (p = list; p; p = p->next) {
1191 struct commit *commit = p->item;
1192 struct patch_id *id;
1193 unsigned flags = commit->object.flags;
1195 if (flags & BOUNDARY)
1196 continue;
1198 * If we have fewer left, left_first is set and we omit
1199 * commits on the left branch in this loop.
1201 if (left_first == !!(flags & SYMMETRIC_LEFT))
1202 continue;
1205 * Have we seen the same patch id?
1207 id = has_commit_patch_id(commit, &ids);
1208 if (!id)
1209 continue;
1211 commit->object.flags |= cherry_flag;
1212 id->commit->object.flags |= cherry_flag;
1215 free_patch_ids(&ids);
1218 /* How many extra uninteresting commits we want to see.. */
1219 #define SLOP 5
1221 static int still_interesting(struct commit_list *src, timestamp_t date, int slop,
1222 struct commit **interesting_cache)
1225 * No source list at all? We're definitely done..
1227 if (!src)
1228 return 0;
1231 * Does the destination list contain entries with a date
1232 * before the source list? Definitely _not_ done.
1234 if (date <= src->item->date)
1235 return SLOP;
1238 * Does the source list still have interesting commits in
1239 * it? Definitely not done..
1241 if (!everybody_uninteresting(src, interesting_cache))
1242 return SLOP;
1244 /* Ok, we're closing in.. */
1245 return slop-1;
1249 * "rev-list --ancestry-path A..B" computes commits that are ancestors
1250 * of B but not ancestors of A but further limits the result to those
1251 * that are descendants of A. This takes the list of bottom commits and
1252 * the result of "A..B" without --ancestry-path, and limits the latter
1253 * further to the ones that can reach one of the commits in "bottom".
1255 static void limit_to_ancestry(struct commit_list *bottom, struct commit_list *list)
1257 struct commit_list *p;
1258 struct commit_list *rlist = NULL;
1259 int made_progress;
1262 * Reverse the list so that it will be likely that we would
1263 * process parents before children.
1265 for (p = list; p; p = p->next)
1266 commit_list_insert(p->item, &rlist);
1268 for (p = bottom; p; p = p->next)
1269 p->item->object.flags |= TMP_MARK;
1272 * Mark the ones that can reach bottom commits in "list",
1273 * in a bottom-up fashion.
1275 do {
1276 made_progress = 0;
1277 for (p = rlist; p; p = p->next) {
1278 struct commit *c = p->item;
1279 struct commit_list *parents;
1280 if (c->object.flags & (TMP_MARK | UNINTERESTING))
1281 continue;
1282 for (parents = c->parents;
1283 parents;
1284 parents = parents->next) {
1285 if (!(parents->item->object.flags & TMP_MARK))
1286 continue;
1287 c->object.flags |= TMP_MARK;
1288 made_progress = 1;
1289 break;
1292 } while (made_progress);
1295 * NEEDSWORK: decide if we want to remove parents that are
1296 * not marked with TMP_MARK from commit->parents for commits
1297 * in the resulting list. We may not want to do that, though.
1301 * The ones that are not marked with TMP_MARK are uninteresting
1303 for (p = list; p; p = p->next) {
1304 struct commit *c = p->item;
1305 if (c->object.flags & TMP_MARK)
1306 continue;
1307 c->object.flags |= UNINTERESTING;
1310 /* We are done with the TMP_MARK */
1311 for (p = list; p; p = p->next)
1312 p->item->object.flags &= ~TMP_MARK;
1313 for (p = bottom; p; p = p->next)
1314 p->item->object.flags &= ~TMP_MARK;
1315 free_commit_list(rlist);
1319 * Before walking the history, keep the set of "negative" refs the
1320 * caller has asked to exclude.
1322 * This is used to compute "rev-list --ancestry-path A..B", as we need
1323 * to filter the result of "A..B" further to the ones that can actually
1324 * reach A.
1326 static struct commit_list *collect_bottom_commits(struct commit_list *list)
1328 struct commit_list *elem, *bottom = NULL;
1329 for (elem = list; elem; elem = elem->next)
1330 if (elem->item->object.flags & BOTTOM)
1331 commit_list_insert(elem->item, &bottom);
1332 return bottom;
1335 /* Assumes either left_only or right_only is set */
1336 static void limit_left_right(struct commit_list *list, struct rev_info *revs)
1338 struct commit_list *p;
1340 for (p = list; p; p = p->next) {
1341 struct commit *commit = p->item;
1343 if (revs->right_only) {
1344 if (commit->object.flags & SYMMETRIC_LEFT)
1345 commit->object.flags |= SHOWN;
1346 } else /* revs->left_only is set */
1347 if (!(commit->object.flags & SYMMETRIC_LEFT))
1348 commit->object.flags |= SHOWN;
1352 static int limit_list(struct rev_info *revs)
1354 int slop = SLOP;
1355 timestamp_t date = TIME_MAX;
1356 struct commit_list *list = revs->commits;
1357 struct commit_list *newlist = NULL;
1358 struct commit_list **p = &newlist;
1359 struct commit_list *bottom = NULL;
1360 struct commit *interesting_cache = NULL;
1362 if (revs->ancestry_path) {
1363 bottom = collect_bottom_commits(list);
1364 if (!bottom)
1365 die("--ancestry-path given but there are no bottom commits");
1368 while (list) {
1369 struct commit *commit = pop_commit(&list);
1370 struct object *obj = &commit->object;
1371 show_early_output_fn_t show;
1373 if (commit == interesting_cache)
1374 interesting_cache = NULL;
1376 if (revs->max_age != -1 && (commit->date < revs->max_age))
1377 obj->flags |= UNINTERESTING;
1378 if (process_parents(revs, commit, &list, NULL) < 0)
1379 return -1;
1380 if (obj->flags & UNINTERESTING) {
1381 mark_parents_uninteresting(commit);
1382 slop = still_interesting(list, date, slop, &interesting_cache);
1383 if (slop)
1384 continue;
1385 break;
1387 if (revs->min_age != -1 && (commit->date > revs->min_age))
1388 continue;
1389 date = commit->date;
1390 p = &commit_list_insert(commit, p)->next;
1392 show = show_early_output;
1393 if (!show)
1394 continue;
1396 show(revs, newlist);
1397 show_early_output = NULL;
1399 if (revs->cherry_pick || revs->cherry_mark)
1400 cherry_pick_list(newlist, revs);
1402 if (revs->left_only || revs->right_only)
1403 limit_left_right(newlist, revs);
1405 if (bottom) {
1406 limit_to_ancestry(bottom, newlist);
1407 free_commit_list(bottom);
1411 * Check if any commits have become TREESAME by some of their parents
1412 * becoming UNINTERESTING.
1414 if (limiting_can_increase_treesame(revs))
1415 for (list = newlist; list; list = list->next) {
1416 struct commit *c = list->item;
1417 if (c->object.flags & (UNINTERESTING | TREESAME))
1418 continue;
1419 update_treesame(revs, c);
1422 revs->commits = newlist;
1423 return 0;
1427 * Add an entry to refs->cmdline with the specified information.
1428 * *name is copied.
1430 static void add_rev_cmdline(struct rev_info *revs,
1431 struct object *item,
1432 const char *name,
1433 int whence,
1434 unsigned flags)
1436 struct rev_cmdline_info *info = &revs->cmdline;
1437 unsigned int nr = info->nr;
1439 ALLOC_GROW(info->rev, nr + 1, info->alloc);
1440 info->rev[nr].item = item;
1441 info->rev[nr].name = xstrdup(name);
1442 info->rev[nr].whence = whence;
1443 info->rev[nr].flags = flags;
1444 info->nr++;
1447 static void add_rev_cmdline_list(struct rev_info *revs,
1448 struct commit_list *commit_list,
1449 int whence,
1450 unsigned flags)
1452 while (commit_list) {
1453 struct object *object = &commit_list->item->object;
1454 add_rev_cmdline(revs, object, oid_to_hex(&object->oid),
1455 whence, flags);
1456 commit_list = commit_list->next;
1460 struct all_refs_cb {
1461 int all_flags;
1462 int warned_bad_reflog;
1463 struct rev_info *all_revs;
1464 const char *name_for_errormsg;
1465 struct worktree *wt;
1468 int ref_excluded(struct string_list *ref_excludes, const char *path)
1470 struct string_list_item *item;
1472 if (!ref_excludes)
1473 return 0;
1474 for_each_string_list_item(item, ref_excludes) {
1475 if (!wildmatch(item->string, path, 0))
1476 return 1;
1478 return 0;
1481 static int handle_one_ref(const char *path, const struct object_id *oid,
1482 int flag, void *cb_data)
1484 struct all_refs_cb *cb = cb_data;
1485 struct object *object;
1487 if (ref_excluded(cb->all_revs->ref_excludes, path))
1488 return 0;
1490 object = get_reference(cb->all_revs, path, oid, cb->all_flags);
1491 add_rev_cmdline(cb->all_revs, object, path, REV_CMD_REF, cb->all_flags);
1492 add_pending_oid(cb->all_revs, path, oid, cb->all_flags);
1493 return 0;
1496 static void init_all_refs_cb(struct all_refs_cb *cb, struct rev_info *revs,
1497 unsigned flags)
1499 cb->all_revs = revs;
1500 cb->all_flags = flags;
1501 revs->rev_input_given = 1;
1502 cb->wt = NULL;
1505 void clear_ref_exclusion(struct string_list **ref_excludes_p)
1507 if (*ref_excludes_p) {
1508 string_list_clear(*ref_excludes_p, 0);
1509 free(*ref_excludes_p);
1511 *ref_excludes_p = NULL;
1514 void add_ref_exclusion(struct string_list **ref_excludes_p, const char *exclude)
1516 if (!*ref_excludes_p) {
1517 *ref_excludes_p = xcalloc(1, sizeof(**ref_excludes_p));
1518 (*ref_excludes_p)->strdup_strings = 1;
1520 string_list_append(*ref_excludes_p, exclude);
1523 static void handle_refs(struct ref_store *refs,
1524 struct rev_info *revs, unsigned flags,
1525 int (*for_each)(struct ref_store *, each_ref_fn, void *))
1527 struct all_refs_cb cb;
1529 if (!refs) {
1530 /* this could happen with uninitialized submodules */
1531 return;
1534 init_all_refs_cb(&cb, revs, flags);
1535 for_each(refs, handle_one_ref, &cb);
1538 static void handle_one_reflog_commit(struct object_id *oid, void *cb_data)
1540 struct all_refs_cb *cb = cb_data;
1541 if (!is_null_oid(oid)) {
1542 struct object *o = parse_object(cb->all_revs->repo, oid);
1543 if (o) {
1544 o->flags |= cb->all_flags;
1545 /* ??? CMDLINEFLAGS ??? */
1546 add_pending_object(cb->all_revs, o, "");
1548 else if (!cb->warned_bad_reflog) {
1549 warning("reflog of '%s' references pruned commits",
1550 cb->name_for_errormsg);
1551 cb->warned_bad_reflog = 1;
1556 static int handle_one_reflog_ent(struct object_id *ooid, struct object_id *noid,
1557 const char *email, timestamp_t timestamp, int tz,
1558 const char *message, void *cb_data)
1560 handle_one_reflog_commit(ooid, cb_data);
1561 handle_one_reflog_commit(noid, cb_data);
1562 return 0;
1565 static int handle_one_reflog(const char *refname_in_wt,
1566 const struct object_id *oid,
1567 int flag, void *cb_data)
1569 struct all_refs_cb *cb = cb_data;
1570 struct strbuf refname = STRBUF_INIT;
1572 cb->warned_bad_reflog = 0;
1573 strbuf_worktree_ref(cb->wt, &refname, refname_in_wt);
1574 cb->name_for_errormsg = refname.buf;
1575 refs_for_each_reflog_ent(get_main_ref_store(the_repository),
1576 refname.buf,
1577 handle_one_reflog_ent, cb_data);
1578 strbuf_release(&refname);
1579 return 0;
1582 static void add_other_reflogs_to_pending(struct all_refs_cb *cb)
1584 struct worktree **worktrees, **p;
1586 worktrees = get_worktrees(0);
1587 for (p = worktrees; *p; p++) {
1588 struct worktree *wt = *p;
1590 if (wt->is_current)
1591 continue;
1593 cb->wt = wt;
1594 refs_for_each_reflog(get_worktree_ref_store(wt),
1595 handle_one_reflog,
1596 cb);
1598 free_worktrees(worktrees);
1601 void add_reflogs_to_pending(struct rev_info *revs, unsigned flags)
1603 struct all_refs_cb cb;
1605 cb.all_revs = revs;
1606 cb.all_flags = flags;
1607 cb.wt = NULL;
1608 for_each_reflog(handle_one_reflog, &cb);
1610 if (!revs->single_worktree)
1611 add_other_reflogs_to_pending(&cb);
1614 static void add_cache_tree(struct cache_tree *it, struct rev_info *revs,
1615 struct strbuf *path, unsigned int flags)
1617 size_t baselen = path->len;
1618 int i;
1620 if (it->entry_count >= 0) {
1621 struct tree *tree = lookup_tree(revs->repo, &it->oid);
1622 tree->object.flags |= flags;
1623 add_pending_object_with_path(revs, &tree->object, "",
1624 040000, path->buf);
1627 for (i = 0; i < it->subtree_nr; i++) {
1628 struct cache_tree_sub *sub = it->down[i];
1629 strbuf_addf(path, "%s%s", baselen ? "/" : "", sub->name);
1630 add_cache_tree(sub->cache_tree, revs, path, flags);
1631 strbuf_setlen(path, baselen);
1636 static void do_add_index_objects_to_pending(struct rev_info *revs,
1637 struct index_state *istate,
1638 unsigned int flags)
1640 int i;
1642 for (i = 0; i < istate->cache_nr; i++) {
1643 struct cache_entry *ce = istate->cache[i];
1644 struct blob *blob;
1646 if (S_ISGITLINK(ce->ce_mode))
1647 continue;
1649 blob = lookup_blob(revs->repo, &ce->oid);
1650 if (!blob)
1651 die("unable to add index blob to traversal");
1652 blob->object.flags |= flags;
1653 add_pending_object_with_path(revs, &blob->object, "",
1654 ce->ce_mode, ce->name);
1657 if (istate->cache_tree) {
1658 struct strbuf path = STRBUF_INIT;
1659 add_cache_tree(istate->cache_tree, revs, &path, flags);
1660 strbuf_release(&path);
1664 void add_index_objects_to_pending(struct rev_info *revs, unsigned int flags)
1666 struct worktree **worktrees, **p;
1668 repo_read_index(revs->repo);
1669 do_add_index_objects_to_pending(revs, revs->repo->index, flags);
1671 if (revs->single_worktree)
1672 return;
1674 worktrees = get_worktrees(0);
1675 for (p = worktrees; *p; p++) {
1676 struct worktree *wt = *p;
1677 struct index_state istate = { NULL };
1679 if (wt->is_current)
1680 continue; /* current index already taken care of */
1682 if (read_index_from(&istate,
1683 worktree_git_path(wt, "index"),
1684 get_worktree_git_dir(wt)) > 0)
1685 do_add_index_objects_to_pending(revs, &istate, flags);
1686 discard_index(&istate);
1688 free_worktrees(worktrees);
1691 struct add_alternate_refs_data {
1692 struct rev_info *revs;
1693 unsigned int flags;
1696 static void add_one_alternate_ref(const struct object_id *oid,
1697 void *vdata)
1699 const char *name = ".alternate";
1700 struct add_alternate_refs_data *data = vdata;
1701 struct object *obj;
1703 obj = get_reference(data->revs, name, oid, data->flags);
1704 add_rev_cmdline(data->revs, obj, name, REV_CMD_REV, data->flags);
1705 add_pending_object(data->revs, obj, name);
1708 static void add_alternate_refs_to_pending(struct rev_info *revs,
1709 unsigned int flags)
1711 struct add_alternate_refs_data data;
1712 data.revs = revs;
1713 data.flags = flags;
1714 for_each_alternate_ref(add_one_alternate_ref, &data);
1717 static int add_parents_only(struct rev_info *revs, const char *arg_, int flags,
1718 int exclude_parent)
1720 struct object_id oid;
1721 struct object *it;
1722 struct commit *commit;
1723 struct commit_list *parents;
1724 int parent_number;
1725 const char *arg = arg_;
1727 if (*arg == '^') {
1728 flags ^= UNINTERESTING | BOTTOM;
1729 arg++;
1731 if (get_oid_committish(arg, &oid))
1732 return 0;
1733 while (1) {
1734 it = get_reference(revs, arg, &oid, 0);
1735 if (!it && revs->ignore_missing)
1736 return 0;
1737 if (it->type != OBJ_TAG)
1738 break;
1739 if (!((struct tag*)it)->tagged)
1740 return 0;
1741 oidcpy(&oid, &((struct tag*)it)->tagged->oid);
1743 if (it->type != OBJ_COMMIT)
1744 return 0;
1745 commit = (struct commit *)it;
1746 if (exclude_parent &&
1747 exclude_parent > commit_list_count(commit->parents))
1748 return 0;
1749 for (parents = commit->parents, parent_number = 1;
1750 parents;
1751 parents = parents->next, parent_number++) {
1752 if (exclude_parent && parent_number != exclude_parent)
1753 continue;
1755 it = &parents->item->object;
1756 it->flags |= flags;
1757 add_rev_cmdline(revs, it, arg_, REV_CMD_PARENTS_ONLY, flags);
1758 add_pending_object(revs, it, arg);
1760 return 1;
1763 void repo_init_revisions(struct repository *r,
1764 struct rev_info *revs,
1765 const char *prefix)
1767 memset(revs, 0, sizeof(*revs));
1769 revs->repo = r;
1770 revs->abbrev = DEFAULT_ABBREV;
1771 revs->ignore_merges = 1;
1772 revs->simplify_history = 1;
1773 revs->pruning.repo = r;
1774 revs->pruning.flags.recursive = 1;
1775 revs->pruning.flags.quick = 1;
1776 revs->pruning.add_remove = file_add_remove;
1777 revs->pruning.change = file_change;
1778 revs->pruning.change_fn_data = revs;
1779 revs->sort_order = REV_SORT_IN_GRAPH_ORDER;
1780 revs->dense = 1;
1781 revs->prefix = prefix;
1782 revs->max_age = -1;
1783 revs->min_age = -1;
1784 revs->skip_count = -1;
1785 revs->max_count = -1;
1786 revs->max_parents = -1;
1787 revs->expand_tabs_in_log = -1;
1789 revs->commit_format = CMIT_FMT_DEFAULT;
1790 revs->expand_tabs_in_log_default = 8;
1792 init_grep_defaults(revs->repo);
1793 grep_init(&revs->grep_filter, revs->repo, prefix);
1794 revs->grep_filter.status_only = 1;
1796 repo_diff_setup(revs->repo, &revs->diffopt);
1797 if (prefix && !revs->diffopt.prefix) {
1798 revs->diffopt.prefix = prefix;
1799 revs->diffopt.prefix_length = strlen(prefix);
1802 init_display_notes(&revs->notes_opt);
1805 static void add_pending_commit_list(struct rev_info *revs,
1806 struct commit_list *commit_list,
1807 unsigned int flags)
1809 while (commit_list) {
1810 struct object *object = &commit_list->item->object;
1811 object->flags |= flags;
1812 add_pending_object(revs, object, oid_to_hex(&object->oid));
1813 commit_list = commit_list->next;
1817 static void prepare_show_merge(struct rev_info *revs)
1819 struct commit_list *bases;
1820 struct commit *head, *other;
1821 struct object_id oid;
1822 const char **prune = NULL;
1823 int i, prune_num = 1; /* counting terminating NULL */
1824 struct index_state *istate = revs->repo->index;
1826 if (get_oid("HEAD", &oid))
1827 die("--merge without HEAD?");
1828 head = lookup_commit_or_die(&oid, "HEAD");
1829 if (get_oid("MERGE_HEAD", &oid))
1830 die("--merge without MERGE_HEAD?");
1831 other = lookup_commit_or_die(&oid, "MERGE_HEAD");
1832 add_pending_object(revs, &head->object, "HEAD");
1833 add_pending_object(revs, &other->object, "MERGE_HEAD");
1834 bases = get_merge_bases(head, other);
1835 add_rev_cmdline_list(revs, bases, REV_CMD_MERGE_BASE, UNINTERESTING | BOTTOM);
1836 add_pending_commit_list(revs, bases, UNINTERESTING | BOTTOM);
1837 free_commit_list(bases);
1838 head->object.flags |= SYMMETRIC_LEFT;
1840 if (!istate->cache_nr)
1841 repo_read_index(revs->repo);
1842 for (i = 0; i < istate->cache_nr; i++) {
1843 const struct cache_entry *ce = istate->cache[i];
1844 if (!ce_stage(ce))
1845 continue;
1846 if (ce_path_match(istate, ce, &revs->prune_data, NULL)) {
1847 prune_num++;
1848 REALLOC_ARRAY(prune, prune_num);
1849 prune[prune_num-2] = ce->name;
1850 prune[prune_num-1] = NULL;
1852 while ((i+1 < istate->cache_nr) &&
1853 ce_same_name(ce, istate->cache[i+1]))
1854 i++;
1856 clear_pathspec(&revs->prune_data);
1857 parse_pathspec(&revs->prune_data, PATHSPEC_ALL_MAGIC & ~PATHSPEC_LITERAL,
1858 PATHSPEC_PREFER_FULL | PATHSPEC_LITERAL_PATH, "", prune);
1859 revs->limited = 1;
1862 static int dotdot_missing(const char *arg, char *dotdot,
1863 struct rev_info *revs, int symmetric)
1865 if (revs->ignore_missing)
1866 return 0;
1867 /* de-munge so we report the full argument */
1868 *dotdot = '.';
1869 die(symmetric
1870 ? "Invalid symmetric difference expression %s"
1871 : "Invalid revision range %s", arg);
1874 static int handle_dotdot_1(const char *arg, char *dotdot,
1875 struct rev_info *revs, int flags,
1876 int cant_be_filename,
1877 struct object_context *a_oc,
1878 struct object_context *b_oc)
1880 const char *a_name, *b_name;
1881 struct object_id a_oid, b_oid;
1882 struct object *a_obj, *b_obj;
1883 unsigned int a_flags, b_flags;
1884 int symmetric = 0;
1885 unsigned int flags_exclude = flags ^ (UNINTERESTING | BOTTOM);
1886 unsigned int oc_flags = GET_OID_COMMITTISH | GET_OID_RECORD_PATH;
1888 a_name = arg;
1889 if (!*a_name)
1890 a_name = "HEAD";
1892 b_name = dotdot + 2;
1893 if (*b_name == '.') {
1894 symmetric = 1;
1895 b_name++;
1897 if (!*b_name)
1898 b_name = "HEAD";
1900 if (get_oid_with_context(revs->repo, a_name, oc_flags, &a_oid, a_oc) ||
1901 get_oid_with_context(revs->repo, b_name, oc_flags, &b_oid, b_oc))
1902 return -1;
1904 if (!cant_be_filename) {
1905 *dotdot = '.';
1906 verify_non_filename(revs->prefix, arg);
1907 *dotdot = '\0';
1910 a_obj = parse_object(revs->repo, &a_oid);
1911 b_obj = parse_object(revs->repo, &b_oid);
1912 if (!a_obj || !b_obj)
1913 return dotdot_missing(arg, dotdot, revs, symmetric);
1915 if (!symmetric) {
1916 /* just A..B */
1917 b_flags = flags;
1918 a_flags = flags_exclude;
1919 } else {
1920 /* A...B -- find merge bases between the two */
1921 struct commit *a, *b;
1922 struct commit_list *exclude;
1924 a = lookup_commit_reference(revs->repo, &a_obj->oid);
1925 b = lookup_commit_reference(revs->repo, &b_obj->oid);
1926 if (!a || !b)
1927 return dotdot_missing(arg, dotdot, revs, symmetric);
1929 exclude = get_merge_bases(a, b);
1930 add_rev_cmdline_list(revs, exclude, REV_CMD_MERGE_BASE,
1931 flags_exclude);
1932 add_pending_commit_list(revs, exclude, flags_exclude);
1933 free_commit_list(exclude);
1935 b_flags = flags;
1936 a_flags = flags | SYMMETRIC_LEFT;
1939 a_obj->flags |= a_flags;
1940 b_obj->flags |= b_flags;
1941 add_rev_cmdline(revs, a_obj, a_name, REV_CMD_LEFT, a_flags);
1942 add_rev_cmdline(revs, b_obj, b_name, REV_CMD_RIGHT, b_flags);
1943 add_pending_object_with_path(revs, a_obj, a_name, a_oc->mode, a_oc->path);
1944 add_pending_object_with_path(revs, b_obj, b_name, b_oc->mode, b_oc->path);
1945 return 0;
1948 static int handle_dotdot(const char *arg,
1949 struct rev_info *revs, int flags,
1950 int cant_be_filename)
1952 struct object_context a_oc, b_oc;
1953 char *dotdot = strstr(arg, "..");
1954 int ret;
1956 if (!dotdot)
1957 return -1;
1959 memset(&a_oc, 0, sizeof(a_oc));
1960 memset(&b_oc, 0, sizeof(b_oc));
1962 *dotdot = '\0';
1963 ret = handle_dotdot_1(arg, dotdot, revs, flags, cant_be_filename,
1964 &a_oc, &b_oc);
1965 *dotdot = '.';
1967 free(a_oc.path);
1968 free(b_oc.path);
1970 return ret;
1973 int handle_revision_arg(const char *arg_, struct rev_info *revs, int flags, unsigned revarg_opt)
1975 struct object_context oc;
1976 char *mark;
1977 struct object *object;
1978 struct object_id oid;
1979 int local_flags;
1980 const char *arg = arg_;
1981 int cant_be_filename = revarg_opt & REVARG_CANNOT_BE_FILENAME;
1982 unsigned get_sha1_flags = GET_OID_RECORD_PATH;
1984 flags = flags & UNINTERESTING ? flags | BOTTOM : flags & ~BOTTOM;
1986 if (!cant_be_filename && !strcmp(arg, "..")) {
1988 * Just ".."? That is not a range but the
1989 * pathspec for the parent directory.
1991 return -1;
1994 if (!handle_dotdot(arg, revs, flags, revarg_opt))
1995 return 0;
1997 mark = strstr(arg, "^@");
1998 if (mark && !mark[2]) {
1999 *mark = 0;
2000 if (add_parents_only(revs, arg, flags, 0))
2001 return 0;
2002 *mark = '^';
2004 mark = strstr(arg, "^!");
2005 if (mark && !mark[2]) {
2006 *mark = 0;
2007 if (!add_parents_only(revs, arg, flags ^ (UNINTERESTING | BOTTOM), 0))
2008 *mark = '^';
2010 mark = strstr(arg, "^-");
2011 if (mark) {
2012 int exclude_parent = 1;
2014 if (mark[2]) {
2015 char *end;
2016 exclude_parent = strtoul(mark + 2, &end, 10);
2017 if (*end != '\0' || !exclude_parent)
2018 return -1;
2021 *mark = 0;
2022 if (!add_parents_only(revs, arg, flags ^ (UNINTERESTING | BOTTOM), exclude_parent))
2023 *mark = '^';
2026 local_flags = 0;
2027 if (*arg == '^') {
2028 local_flags = UNINTERESTING | BOTTOM;
2029 arg++;
2032 if (revarg_opt & REVARG_COMMITTISH)
2033 get_sha1_flags |= GET_OID_COMMITTISH;
2035 if (get_oid_with_context(revs->repo, arg, get_sha1_flags, &oid, &oc))
2036 return revs->ignore_missing ? 0 : -1;
2037 if (!cant_be_filename)
2038 verify_non_filename(revs->prefix, arg);
2039 object = get_reference(revs, arg, &oid, flags ^ local_flags);
2040 if (!object)
2041 return revs->ignore_missing ? 0 : -1;
2042 add_rev_cmdline(revs, object, arg_, REV_CMD_REV, flags ^ local_flags);
2043 add_pending_object_with_path(revs, object, arg, oc.mode, oc.path);
2044 free(oc.path);
2045 return 0;
2048 static void read_pathspec_from_stdin(struct strbuf *sb,
2049 struct argv_array *prune)
2051 while (strbuf_getline(sb, stdin) != EOF)
2052 argv_array_push(prune, sb->buf);
2055 static void read_revisions_from_stdin(struct rev_info *revs,
2056 struct argv_array *prune)
2058 struct strbuf sb;
2059 int seen_dashdash = 0;
2060 int save_warning;
2062 save_warning = warn_on_object_refname_ambiguity;
2063 warn_on_object_refname_ambiguity = 0;
2065 strbuf_init(&sb, 1000);
2066 while (strbuf_getline(&sb, stdin) != EOF) {
2067 int len = sb.len;
2068 if (!len)
2069 break;
2070 if (sb.buf[0] == '-') {
2071 if (len == 2 && sb.buf[1] == '-') {
2072 seen_dashdash = 1;
2073 break;
2075 die("options not supported in --stdin mode");
2077 if (handle_revision_arg(sb.buf, revs, 0,
2078 REVARG_CANNOT_BE_FILENAME))
2079 die("bad revision '%s'", sb.buf);
2081 if (seen_dashdash)
2082 read_pathspec_from_stdin(&sb, prune);
2084 strbuf_release(&sb);
2085 warn_on_object_refname_ambiguity = save_warning;
2088 static void add_grep(struct rev_info *revs, const char *ptn, enum grep_pat_token what)
2090 append_grep_pattern(&revs->grep_filter, ptn, "command line", 0, what);
2093 static void add_header_grep(struct rev_info *revs, enum grep_header_field field, const char *pattern)
2095 append_header_grep_pattern(&revs->grep_filter, field, pattern);
2098 static void add_message_grep(struct rev_info *revs, const char *pattern)
2100 add_grep(revs, pattern, GREP_PATTERN_BODY);
2103 static int handle_revision_opt(struct rev_info *revs, int argc, const char **argv,
2104 int *unkc, const char **unkv,
2105 const struct setup_revision_opt* opt)
2107 const char *arg = argv[0];
2108 const char *optarg;
2109 int argcount;
2110 const unsigned hexsz = the_hash_algo->hexsz;
2112 /* pseudo revision arguments */
2113 if (!strcmp(arg, "--all") || !strcmp(arg, "--branches") ||
2114 !strcmp(arg, "--tags") || !strcmp(arg, "--remotes") ||
2115 !strcmp(arg, "--reflog") || !strcmp(arg, "--not") ||
2116 !strcmp(arg, "--no-walk") || !strcmp(arg, "--do-walk") ||
2117 !strcmp(arg, "--bisect") || starts_with(arg, "--glob=") ||
2118 !strcmp(arg, "--indexed-objects") ||
2119 !strcmp(arg, "--alternate-refs") ||
2120 starts_with(arg, "--exclude=") ||
2121 starts_with(arg, "--branches=") || starts_with(arg, "--tags=") ||
2122 starts_with(arg, "--remotes=") || starts_with(arg, "--no-walk="))
2124 unkv[(*unkc)++] = arg;
2125 return 1;
2128 if ((argcount = parse_long_opt("max-count", argv, &optarg))) {
2129 revs->max_count = atoi(optarg);
2130 revs->no_walk = 0;
2131 return argcount;
2132 } else if ((argcount = parse_long_opt("skip", argv, &optarg))) {
2133 revs->skip_count = atoi(optarg);
2134 return argcount;
2135 } else if ((*arg == '-') && isdigit(arg[1])) {
2136 /* accept -<digit>, like traditional "head" */
2137 if (strtol_i(arg + 1, 10, &revs->max_count) < 0 ||
2138 revs->max_count < 0)
2139 die("'%s': not a non-negative integer", arg + 1);
2140 revs->no_walk = 0;
2141 } else if (!strcmp(arg, "-n")) {
2142 if (argc <= 1)
2143 return error("-n requires an argument");
2144 revs->max_count = atoi(argv[1]);
2145 revs->no_walk = 0;
2146 return 2;
2147 } else if (skip_prefix(arg, "-n", &optarg)) {
2148 revs->max_count = atoi(optarg);
2149 revs->no_walk = 0;
2150 } else if ((argcount = parse_long_opt("max-age", argv, &optarg))) {
2151 revs->max_age = atoi(optarg);
2152 return argcount;
2153 } else if ((argcount = parse_long_opt("since", argv, &optarg))) {
2154 revs->max_age = approxidate(optarg);
2155 return argcount;
2156 } else if ((argcount = parse_long_opt("after", argv, &optarg))) {
2157 revs->max_age = approxidate(optarg);
2158 return argcount;
2159 } else if ((argcount = parse_long_opt("min-age", argv, &optarg))) {
2160 revs->min_age = atoi(optarg);
2161 return argcount;
2162 } else if ((argcount = parse_long_opt("before", argv, &optarg))) {
2163 revs->min_age = approxidate(optarg);
2164 return argcount;
2165 } else if ((argcount = parse_long_opt("until", argv, &optarg))) {
2166 revs->min_age = approxidate(optarg);
2167 return argcount;
2168 } else if (!strcmp(arg, "--first-parent")) {
2169 revs->first_parent_only = 1;
2170 } else if (!strcmp(arg, "--ancestry-path")) {
2171 revs->ancestry_path = 1;
2172 revs->simplify_history = 0;
2173 revs->limited = 1;
2174 } else if (!strcmp(arg, "-g") || !strcmp(arg, "--walk-reflogs")) {
2175 init_reflog_walk(&revs->reflog_info);
2176 } else if (!strcmp(arg, "--default")) {
2177 if (argc <= 1)
2178 return error("bad --default argument");
2179 revs->def = argv[1];
2180 return 2;
2181 } else if (!strcmp(arg, "--merge")) {
2182 revs->show_merge = 1;
2183 } else if (!strcmp(arg, "--topo-order")) {
2184 revs->sort_order = REV_SORT_IN_GRAPH_ORDER;
2185 revs->topo_order = 1;
2186 } else if (!strcmp(arg, "--simplify-merges")) {
2187 revs->simplify_merges = 1;
2188 revs->topo_order = 1;
2189 revs->rewrite_parents = 1;
2190 revs->simplify_history = 0;
2191 revs->limited = 1;
2192 } else if (!strcmp(arg, "--simplify-by-decoration")) {
2193 revs->simplify_merges = 1;
2194 revs->topo_order = 1;
2195 revs->rewrite_parents = 1;
2196 revs->simplify_history = 0;
2197 revs->simplify_by_decoration = 1;
2198 revs->limited = 1;
2199 revs->prune = 1;
2200 } else if (!strcmp(arg, "--date-order")) {
2201 revs->sort_order = REV_SORT_BY_COMMIT_DATE;
2202 revs->topo_order = 1;
2203 } else if (!strcmp(arg, "--author-date-order")) {
2204 revs->sort_order = REV_SORT_BY_AUTHOR_DATE;
2205 revs->topo_order = 1;
2206 } else if (!strcmp(arg, "--early-output")) {
2207 revs->early_output = 100;
2208 revs->topo_order = 1;
2209 } else if (skip_prefix(arg, "--early-output=", &optarg)) {
2210 if (strtoul_ui(optarg, 10, &revs->early_output) < 0)
2211 die("'%s': not a non-negative integer", optarg);
2212 revs->topo_order = 1;
2213 } else if (!strcmp(arg, "--parents")) {
2214 revs->rewrite_parents = 1;
2215 revs->print_parents = 1;
2216 } else if (!strcmp(arg, "--dense")) {
2217 revs->dense = 1;
2218 } else if (!strcmp(arg, "--sparse")) {
2219 revs->dense = 0;
2220 } else if (!strcmp(arg, "--in-commit-order")) {
2221 revs->tree_blobs_in_commit_order = 1;
2222 } else if (!strcmp(arg, "--remove-empty")) {
2223 revs->remove_empty_trees = 1;
2224 } else if (!strcmp(arg, "--merges")) {
2225 revs->min_parents = 2;
2226 } else if (!strcmp(arg, "--no-merges")) {
2227 revs->max_parents = 1;
2228 } else if (skip_prefix(arg, "--min-parents=", &optarg)) {
2229 revs->min_parents = atoi(optarg);
2230 } else if (!strcmp(arg, "--no-min-parents")) {
2231 revs->min_parents = 0;
2232 } else if (skip_prefix(arg, "--max-parents=", &optarg)) {
2233 revs->max_parents = atoi(optarg);
2234 } else if (!strcmp(arg, "--no-max-parents")) {
2235 revs->max_parents = -1;
2236 } else if (!strcmp(arg, "--boundary")) {
2237 revs->boundary = 1;
2238 } else if (!strcmp(arg, "--left-right")) {
2239 revs->left_right = 1;
2240 } else if (!strcmp(arg, "--left-only")) {
2241 if (revs->right_only)
2242 die("--left-only is incompatible with --right-only"
2243 " or --cherry");
2244 revs->left_only = 1;
2245 } else if (!strcmp(arg, "--right-only")) {
2246 if (revs->left_only)
2247 die("--right-only is incompatible with --left-only");
2248 revs->right_only = 1;
2249 } else if (!strcmp(arg, "--cherry")) {
2250 if (revs->left_only)
2251 die("--cherry is incompatible with --left-only");
2252 revs->cherry_mark = 1;
2253 revs->right_only = 1;
2254 revs->max_parents = 1;
2255 revs->limited = 1;
2256 } else if (!strcmp(arg, "--count")) {
2257 revs->count = 1;
2258 } else if (!strcmp(arg, "--cherry-mark")) {
2259 if (revs->cherry_pick)
2260 die("--cherry-mark is incompatible with --cherry-pick");
2261 revs->cherry_mark = 1;
2262 revs->limited = 1; /* needs limit_list() */
2263 } else if (!strcmp(arg, "--cherry-pick")) {
2264 if (revs->cherry_mark)
2265 die("--cherry-pick is incompatible with --cherry-mark");
2266 revs->cherry_pick = 1;
2267 revs->limited = 1;
2268 } else if (!strcmp(arg, "--objects")) {
2269 revs->tag_objects = 1;
2270 revs->tree_objects = 1;
2271 revs->blob_objects = 1;
2272 } else if (!strcmp(arg, "--objects-edge")) {
2273 revs->tag_objects = 1;
2274 revs->tree_objects = 1;
2275 revs->blob_objects = 1;
2276 revs->edge_hint = 1;
2277 } else if (!strcmp(arg, "--objects-edge-aggressive")) {
2278 revs->tag_objects = 1;
2279 revs->tree_objects = 1;
2280 revs->blob_objects = 1;
2281 revs->edge_hint = 1;
2282 revs->edge_hint_aggressive = 1;
2283 } else if (!strcmp(arg, "--verify-objects")) {
2284 revs->tag_objects = 1;
2285 revs->tree_objects = 1;
2286 revs->blob_objects = 1;
2287 revs->verify_objects = 1;
2288 } else if (!strcmp(arg, "--unpacked")) {
2289 revs->unpacked = 1;
2290 } else if (starts_with(arg, "--unpacked=")) {
2291 die("--unpacked=<packfile> no longer supported.");
2292 } else if (!strcmp(arg, "-r")) {
2293 revs->diff = 1;
2294 revs->diffopt.flags.recursive = 1;
2295 } else if (!strcmp(arg, "-t")) {
2296 revs->diff = 1;
2297 revs->diffopt.flags.recursive = 1;
2298 revs->diffopt.flags.tree_in_recursive = 1;
2299 } else if (!strcmp(arg, "-m")) {
2300 revs->ignore_merges = 0;
2301 } else if (!strcmp(arg, "-c")) {
2302 revs->diff = 1;
2303 revs->dense_combined_merges = 0;
2304 revs->combine_merges = 1;
2305 } else if (!strcmp(arg, "--combined-all-paths")) {
2306 revs->diff = 1;
2307 revs->combined_all_paths = 1;
2308 } else if (!strcmp(arg, "--cc")) {
2309 revs->diff = 1;
2310 revs->dense_combined_merges = 1;
2311 revs->combine_merges = 1;
2312 } else if (!strcmp(arg, "-v")) {
2313 revs->verbose_header = 1;
2314 } else if (!strcmp(arg, "--pretty")) {
2315 revs->verbose_header = 1;
2316 revs->pretty_given = 1;
2317 get_commit_format(NULL, revs);
2318 } else if (skip_prefix(arg, "--pretty=", &optarg) ||
2319 skip_prefix(arg, "--format=", &optarg)) {
2321 * Detached form ("--pretty X" as opposed to "--pretty=X")
2322 * not allowed, since the argument is optional.
2324 revs->verbose_header = 1;
2325 revs->pretty_given = 1;
2326 get_commit_format(optarg, revs);
2327 } else if (!strcmp(arg, "--expand-tabs")) {
2328 revs->expand_tabs_in_log = 8;
2329 } else if (!strcmp(arg, "--no-expand-tabs")) {
2330 revs->expand_tabs_in_log = 0;
2331 } else if (skip_prefix(arg, "--expand-tabs=", &arg)) {
2332 int val;
2333 if (strtol_i(arg, 10, &val) < 0 || val < 0)
2334 die("'%s': not a non-negative integer", arg);
2335 revs->expand_tabs_in_log = val;
2336 } else if (!strcmp(arg, "--show-notes") || !strcmp(arg, "--notes")) {
2337 enable_default_display_notes(&revs->notes_opt, &revs->show_notes);
2338 revs->show_notes_given = 1;
2339 } else if (!strcmp(arg, "--show-signature")) {
2340 revs->show_signature = 1;
2341 } else if (!strcmp(arg, "--no-show-signature")) {
2342 revs->show_signature = 0;
2343 } else if (!strcmp(arg, "--show-linear-break")) {
2344 revs->break_bar = " ..........";
2345 revs->track_linear = 1;
2346 revs->track_first_time = 1;
2347 } else if (skip_prefix(arg, "--show-linear-break=", &optarg)) {
2348 revs->break_bar = xstrdup(optarg);
2349 revs->track_linear = 1;
2350 revs->track_first_time = 1;
2351 } else if (skip_prefix(arg, "--show-notes=", &optarg) ||
2352 skip_prefix(arg, "--notes=", &optarg)) {
2353 if (starts_with(arg, "--show-notes=") &&
2354 revs->notes_opt.use_default_notes < 0)
2355 revs->notes_opt.use_default_notes = 1;
2356 enable_ref_display_notes(&revs->notes_opt, &revs->show_notes, optarg);
2357 revs->show_notes_given = 1;
2358 } else if (!strcmp(arg, "--no-notes")) {
2359 disable_display_notes(&revs->notes_opt, &revs->show_notes);
2360 revs->show_notes_given = 1;
2361 } else if (!strcmp(arg, "--standard-notes")) {
2362 revs->show_notes_given = 1;
2363 revs->notes_opt.use_default_notes = 1;
2364 } else if (!strcmp(arg, "--no-standard-notes")) {
2365 revs->notes_opt.use_default_notes = 0;
2366 } else if (!strcmp(arg, "--oneline")) {
2367 revs->verbose_header = 1;
2368 get_commit_format("oneline", revs);
2369 revs->pretty_given = 1;
2370 revs->abbrev_commit = 1;
2371 } else if (!strcmp(arg, "--graph")) {
2372 revs->topo_order = 1;
2373 revs->rewrite_parents = 1;
2374 revs->graph = graph_init(revs);
2375 } else if (!strcmp(arg, "--root")) {
2376 revs->show_root_diff = 1;
2377 } else if (!strcmp(arg, "--no-commit-id")) {
2378 revs->no_commit_id = 1;
2379 } else if (!strcmp(arg, "--always")) {
2380 revs->always_show_header = 1;
2381 } else if (!strcmp(arg, "--no-abbrev")) {
2382 revs->abbrev = 0;
2383 } else if (!strcmp(arg, "--abbrev")) {
2384 revs->abbrev = DEFAULT_ABBREV;
2385 } else if (skip_prefix(arg, "--abbrev=", &optarg)) {
2386 revs->abbrev = strtoul(optarg, NULL, 10);
2387 if (revs->abbrev < MINIMUM_ABBREV)
2388 revs->abbrev = MINIMUM_ABBREV;
2389 else if (revs->abbrev > hexsz)
2390 revs->abbrev = hexsz;
2391 } else if (!strcmp(arg, "--abbrev-commit")) {
2392 revs->abbrev_commit = 1;
2393 revs->abbrev_commit_given = 1;
2394 } else if (!strcmp(arg, "--no-abbrev-commit")) {
2395 revs->abbrev_commit = 0;
2396 } else if (!strcmp(arg, "--full-diff")) {
2397 revs->diff = 1;
2398 revs->full_diff = 1;
2399 } else if (!strcmp(arg, "--full-history")) {
2400 revs->simplify_history = 0;
2401 } else if (!strcmp(arg, "--relative-date")) {
2402 revs->date_mode.type = DATE_RELATIVE;
2403 revs->date_mode_explicit = 1;
2404 } else if ((argcount = parse_long_opt("date", argv, &optarg))) {
2405 parse_date_format(optarg, &revs->date_mode);
2406 revs->date_mode_explicit = 1;
2407 return argcount;
2408 } else if (!strcmp(arg, "--log-size")) {
2409 revs->show_log_size = 1;
2412 * Grepping the commit log
2414 else if ((argcount = parse_long_opt("author", argv, &optarg))) {
2415 add_header_grep(revs, GREP_HEADER_AUTHOR, optarg);
2416 return argcount;
2417 } else if ((argcount = parse_long_opt("committer", argv, &optarg))) {
2418 add_header_grep(revs, GREP_HEADER_COMMITTER, optarg);
2419 return argcount;
2420 } else if ((argcount = parse_long_opt("grep-reflog", argv, &optarg))) {
2421 add_header_grep(revs, GREP_HEADER_REFLOG, optarg);
2422 return argcount;
2423 } else if ((argcount = parse_long_opt("grep", argv, &optarg))) {
2424 add_message_grep(revs, optarg);
2425 return argcount;
2426 } else if (!strcmp(arg, "--grep-debug")) {
2427 revs->grep_filter.debug = 1;
2428 } else if (!strcmp(arg, "--basic-regexp")) {
2429 revs->grep_filter.pattern_type_option = GREP_PATTERN_TYPE_BRE;
2430 } else if (!strcmp(arg, "--extended-regexp") || !strcmp(arg, "-E")) {
2431 revs->grep_filter.pattern_type_option = GREP_PATTERN_TYPE_ERE;
2432 } else if (!strcmp(arg, "--regexp-ignore-case") || !strcmp(arg, "-i")) {
2433 revs->grep_filter.ignore_case = 1;
2434 revs->diffopt.pickaxe_opts |= DIFF_PICKAXE_IGNORE_CASE;
2435 } else if (!strcmp(arg, "--fixed-strings") || !strcmp(arg, "-F")) {
2436 revs->grep_filter.pattern_type_option = GREP_PATTERN_TYPE_FIXED;
2437 } else if (!strcmp(arg, "--perl-regexp") || !strcmp(arg, "-P")) {
2438 revs->grep_filter.pattern_type_option = GREP_PATTERN_TYPE_PCRE;
2439 } else if (!strcmp(arg, "--all-match")) {
2440 revs->grep_filter.all_match = 1;
2441 } else if (!strcmp(arg, "--invert-grep")) {
2442 revs->invert_grep = 1;
2443 } else if ((argcount = parse_long_opt("encoding", argv, &optarg))) {
2444 if (strcmp(optarg, "none"))
2445 git_log_output_encoding = xstrdup(optarg);
2446 else
2447 git_log_output_encoding = "";
2448 return argcount;
2449 } else if (!strcmp(arg, "--reverse")) {
2450 revs->reverse ^= 1;
2451 } else if (!strcmp(arg, "--children")) {
2452 revs->children.name = "children";
2453 revs->limited = 1;
2454 } else if (!strcmp(arg, "--ignore-missing")) {
2455 revs->ignore_missing = 1;
2456 } else if (opt && opt->allow_exclude_promisor_objects &&
2457 !strcmp(arg, "--exclude-promisor-objects")) {
2458 if (fetch_if_missing)
2459 BUG("exclude_promisor_objects can only be used when fetch_if_missing is 0");
2460 revs->exclude_promisor_objects = 1;
2461 } else {
2462 int opts = diff_opt_parse(&revs->diffopt, argv, argc, revs->prefix);
2463 if (!opts)
2464 unkv[(*unkc)++] = arg;
2465 return opts;
2467 if (revs->graph && revs->track_linear)
2468 die("--show-linear-break and --graph are incompatible");
2470 return 1;
2473 void parse_revision_opt(struct rev_info *revs, struct parse_opt_ctx_t *ctx,
2474 const struct option *options,
2475 const char * const usagestr[])
2477 int n = handle_revision_opt(revs, ctx->argc, ctx->argv,
2478 &ctx->cpidx, ctx->out, NULL);
2479 if (n <= 0) {
2480 error("unknown option `%s'", ctx->argv[0]);
2481 usage_with_options(usagestr, options);
2483 ctx->argv += n;
2484 ctx->argc -= n;
2487 static int for_each_bisect_ref(struct ref_store *refs, each_ref_fn fn,
2488 void *cb_data, const char *term)
2490 struct strbuf bisect_refs = STRBUF_INIT;
2491 int status;
2492 strbuf_addf(&bisect_refs, "refs/bisect/%s", term);
2493 status = refs_for_each_fullref_in(refs, bisect_refs.buf, fn, cb_data, 0);
2494 strbuf_release(&bisect_refs);
2495 return status;
2498 static int for_each_bad_bisect_ref(struct ref_store *refs, each_ref_fn fn, void *cb_data)
2500 return for_each_bisect_ref(refs, fn, cb_data, term_bad);
2503 static int for_each_good_bisect_ref(struct ref_store *refs, each_ref_fn fn, void *cb_data)
2505 return for_each_bisect_ref(refs, fn, cb_data, term_good);
2508 static int handle_revision_pseudo_opt(const char *submodule,
2509 struct rev_info *revs,
2510 int argc, const char **argv, int *flags)
2512 const char *arg = argv[0];
2513 const char *optarg;
2514 struct ref_store *refs;
2515 int argcount;
2517 if (submodule) {
2519 * We need some something like get_submodule_worktrees()
2520 * before we can go through all worktrees of a submodule,
2521 * .e.g with adding all HEADs from --all, which is not
2522 * supported right now, so stick to single worktree.
2524 if (!revs->single_worktree)
2525 BUG("--single-worktree cannot be used together with submodule");
2526 refs = get_submodule_ref_store(submodule);
2527 } else
2528 refs = get_main_ref_store(revs->repo);
2531 * NOTE!
2533 * Commands like "git shortlog" will not accept the options below
2534 * unless parse_revision_opt queues them (as opposed to erroring
2535 * out).
2537 * When implementing your new pseudo-option, remember to
2538 * register it in the list at the top of handle_revision_opt.
2540 if (!strcmp(arg, "--all")) {
2541 handle_refs(refs, revs, *flags, refs_for_each_ref);
2542 handle_refs(refs, revs, *flags, refs_head_ref);
2543 if (!revs->single_worktree) {
2544 struct all_refs_cb cb;
2546 init_all_refs_cb(&cb, revs, *flags);
2547 other_head_refs(handle_one_ref, &cb);
2549 clear_ref_exclusion(&revs->ref_excludes);
2550 } else if (!strcmp(arg, "--branches")) {
2551 handle_refs(refs, revs, *flags, refs_for_each_branch_ref);
2552 clear_ref_exclusion(&revs->ref_excludes);
2553 } else if (!strcmp(arg, "--bisect")) {
2554 read_bisect_terms(&term_bad, &term_good);
2555 handle_refs(refs, revs, *flags, for_each_bad_bisect_ref);
2556 handle_refs(refs, revs, *flags ^ (UNINTERESTING | BOTTOM),
2557 for_each_good_bisect_ref);
2558 revs->bisect = 1;
2559 } else if (!strcmp(arg, "--tags")) {
2560 handle_refs(refs, revs, *flags, refs_for_each_tag_ref);
2561 clear_ref_exclusion(&revs->ref_excludes);
2562 } else if (!strcmp(arg, "--remotes")) {
2563 handle_refs(refs, revs, *flags, refs_for_each_remote_ref);
2564 clear_ref_exclusion(&revs->ref_excludes);
2565 } else if ((argcount = parse_long_opt("glob", argv, &optarg))) {
2566 struct all_refs_cb cb;
2567 init_all_refs_cb(&cb, revs, *flags);
2568 for_each_glob_ref(handle_one_ref, optarg, &cb);
2569 clear_ref_exclusion(&revs->ref_excludes);
2570 return argcount;
2571 } else if ((argcount = parse_long_opt("exclude", argv, &optarg))) {
2572 add_ref_exclusion(&revs->ref_excludes, optarg);
2573 return argcount;
2574 } else if (skip_prefix(arg, "--branches=", &optarg)) {
2575 struct all_refs_cb cb;
2576 init_all_refs_cb(&cb, revs, *flags);
2577 for_each_glob_ref_in(handle_one_ref, optarg, "refs/heads/", &cb);
2578 clear_ref_exclusion(&revs->ref_excludes);
2579 } else if (skip_prefix(arg, "--tags=", &optarg)) {
2580 struct all_refs_cb cb;
2581 init_all_refs_cb(&cb, revs, *flags);
2582 for_each_glob_ref_in(handle_one_ref, optarg, "refs/tags/", &cb);
2583 clear_ref_exclusion(&revs->ref_excludes);
2584 } else if (skip_prefix(arg, "--remotes=", &optarg)) {
2585 struct all_refs_cb cb;
2586 init_all_refs_cb(&cb, revs, *flags);
2587 for_each_glob_ref_in(handle_one_ref, optarg, "refs/remotes/", &cb);
2588 clear_ref_exclusion(&revs->ref_excludes);
2589 } else if (!strcmp(arg, "--reflog")) {
2590 add_reflogs_to_pending(revs, *flags);
2591 } else if (!strcmp(arg, "--indexed-objects")) {
2592 add_index_objects_to_pending(revs, *flags);
2593 } else if (!strcmp(arg, "--alternate-refs")) {
2594 add_alternate_refs_to_pending(revs, *flags);
2595 } else if (!strcmp(arg, "--not")) {
2596 *flags ^= UNINTERESTING | BOTTOM;
2597 } else if (!strcmp(arg, "--no-walk")) {
2598 revs->no_walk = REVISION_WALK_NO_WALK_SORTED;
2599 } else if (skip_prefix(arg, "--no-walk=", &optarg)) {
2601 * Detached form ("--no-walk X" as opposed to "--no-walk=X")
2602 * not allowed, since the argument is optional.
2604 if (!strcmp(optarg, "sorted"))
2605 revs->no_walk = REVISION_WALK_NO_WALK_SORTED;
2606 else if (!strcmp(optarg, "unsorted"))
2607 revs->no_walk = REVISION_WALK_NO_WALK_UNSORTED;
2608 else
2609 return error("invalid argument to --no-walk");
2610 } else if (!strcmp(arg, "--do-walk")) {
2611 revs->no_walk = 0;
2612 } else if (!strcmp(arg, "--single-worktree")) {
2613 revs->single_worktree = 1;
2614 } else {
2615 return 0;
2618 return 1;
2621 static void NORETURN diagnose_missing_default(const char *def)
2623 int flags;
2624 const char *refname;
2626 refname = resolve_ref_unsafe(def, 0, NULL, &flags);
2627 if (!refname || !(flags & REF_ISSYMREF) || (flags & REF_ISBROKEN))
2628 die(_("your current branch appears to be broken"));
2630 skip_prefix(refname, "refs/heads/", &refname);
2631 die(_("your current branch '%s' does not have any commits yet"),
2632 refname);
2636 * Parse revision information, filling in the "rev_info" structure,
2637 * and removing the used arguments from the argument list.
2639 * Returns the number of arguments left that weren't recognized
2640 * (which are also moved to the head of the argument list)
2642 int setup_revisions(int argc, const char **argv, struct rev_info *revs, struct setup_revision_opt *opt)
2644 int i, flags, left, seen_dashdash, got_rev_arg = 0, revarg_opt;
2645 struct argv_array prune_data = ARGV_ARRAY_INIT;
2646 const char *submodule = NULL;
2647 int seen_end_of_options = 0;
2649 if (opt)
2650 submodule = opt->submodule;
2652 /* First, search for "--" */
2653 if (opt && opt->assume_dashdash) {
2654 seen_dashdash = 1;
2655 } else {
2656 seen_dashdash = 0;
2657 for (i = 1; i < argc; i++) {
2658 const char *arg = argv[i];
2659 if (strcmp(arg, "--"))
2660 continue;
2661 argv[i] = NULL;
2662 argc = i;
2663 if (argv[i + 1])
2664 argv_array_pushv(&prune_data, argv + i + 1);
2665 seen_dashdash = 1;
2666 break;
2670 /* Second, deal with arguments and options */
2671 flags = 0;
2672 revarg_opt = opt ? opt->revarg_opt : 0;
2673 if (seen_dashdash)
2674 revarg_opt |= REVARG_CANNOT_BE_FILENAME;
2675 for (left = i = 1; i < argc; i++) {
2676 const char *arg = argv[i];
2677 if (!seen_end_of_options && *arg == '-') {
2678 int opts;
2680 opts = handle_revision_pseudo_opt(submodule,
2681 revs, argc - i, argv + i,
2682 &flags);
2683 if (opts > 0) {
2684 i += opts - 1;
2685 continue;
2688 if (!strcmp(arg, "--stdin")) {
2689 if (revs->disable_stdin) {
2690 argv[left++] = arg;
2691 continue;
2693 if (revs->read_from_stdin++)
2694 die("--stdin given twice?");
2695 read_revisions_from_stdin(revs, &prune_data);
2696 continue;
2699 if (!strcmp(arg, "--end-of-options")) {
2700 seen_end_of_options = 1;
2701 continue;
2704 opts = handle_revision_opt(revs, argc - i, argv + i,
2705 &left, argv, opt);
2706 if (opts > 0) {
2707 i += opts - 1;
2708 continue;
2710 if (opts < 0)
2711 exit(128);
2712 continue;
2716 if (handle_revision_arg(arg, revs, flags, revarg_opt)) {
2717 int j;
2718 if (seen_dashdash || *arg == '^')
2719 die("bad revision '%s'", arg);
2721 /* If we didn't have a "--":
2722 * (1) all filenames must exist;
2723 * (2) all rev-args must not be interpretable
2724 * as a valid filename.
2725 * but the latter we have checked in the main loop.
2727 for (j = i; j < argc; j++)
2728 verify_filename(revs->prefix, argv[j], j == i);
2730 argv_array_pushv(&prune_data, argv + i);
2731 break;
2733 else
2734 got_rev_arg = 1;
2737 if (prune_data.argc) {
2739 * If we need to introduce the magic "a lone ':' means no
2740 * pathspec whatsoever", here is the place to do so.
2742 * if (prune_data.nr == 1 && !strcmp(prune_data[0], ":")) {
2743 * prune_data.nr = 0;
2744 * prune_data.alloc = 0;
2745 * free(prune_data.path);
2746 * prune_data.path = NULL;
2747 * } else {
2748 * terminate prune_data.alloc with NULL and
2749 * call init_pathspec() to set revs->prune_data here.
2752 parse_pathspec(&revs->prune_data, 0, 0,
2753 revs->prefix, prune_data.argv);
2755 argv_array_clear(&prune_data);
2757 if (revs->def == NULL)
2758 revs->def = opt ? opt->def : NULL;
2759 if (opt && opt->tweak)
2760 opt->tweak(revs, opt);
2761 if (revs->show_merge)
2762 prepare_show_merge(revs);
2763 if (revs->def && !revs->pending.nr && !revs->rev_input_given && !got_rev_arg) {
2764 struct object_id oid;
2765 struct object *object;
2766 struct object_context oc;
2767 if (get_oid_with_context(revs->repo, revs->def, 0, &oid, &oc))
2768 diagnose_missing_default(revs->def);
2769 object = get_reference(revs, revs->def, &oid, 0);
2770 add_pending_object_with_mode(revs, object, revs->def, oc.mode);
2773 /* Did the user ask for any diff output? Run the diff! */
2774 if (revs->diffopt.output_format & ~DIFF_FORMAT_NO_OUTPUT)
2775 revs->diff = 1;
2777 /* Pickaxe, diff-filter and rename following need diffs */
2778 if ((revs->diffopt.pickaxe_opts & DIFF_PICKAXE_KINDS_MASK) ||
2779 revs->diffopt.filter ||
2780 revs->diffopt.flags.follow_renames)
2781 revs->diff = 1;
2783 if (revs->diffopt.objfind)
2784 revs->simplify_history = 0;
2786 if (revs->line_level_traverse) {
2787 if (want_ancestry(revs))
2788 revs->limited = 1;
2789 revs->topo_order = 1;
2792 if (revs->topo_order && !generation_numbers_enabled(the_repository))
2793 revs->limited = 1;
2795 if (revs->prune_data.nr) {
2796 copy_pathspec(&revs->pruning.pathspec, &revs->prune_data);
2797 /* Can't prune commits with rename following: the paths change.. */
2798 if (!revs->diffopt.flags.follow_renames)
2799 revs->prune = 1;
2800 if (!revs->full_diff)
2801 copy_pathspec(&revs->diffopt.pathspec,
2802 &revs->prune_data);
2804 if (revs->combine_merges)
2805 revs->ignore_merges = 0;
2806 if (revs->combined_all_paths && !revs->combine_merges)
2807 die("--combined-all-paths makes no sense without -c or --cc");
2809 revs->diffopt.abbrev = revs->abbrev;
2811 diff_setup_done(&revs->diffopt);
2813 grep_commit_pattern_type(GREP_PATTERN_TYPE_UNSPECIFIED,
2814 &revs->grep_filter);
2815 if (!is_encoding_utf8(get_log_output_encoding()))
2816 revs->grep_filter.ignore_locale = 1;
2817 compile_grep_patterns(&revs->grep_filter);
2819 if (revs->reverse && revs->reflog_info)
2820 die("cannot combine --reverse with --walk-reflogs");
2821 if (revs->reflog_info && revs->limited)
2822 die("cannot combine --walk-reflogs with history-limiting options");
2823 if (revs->rewrite_parents && revs->children.name)
2824 die("cannot combine --parents and --children");
2827 * Limitations on the graph functionality
2829 if (revs->reverse && revs->graph)
2830 die("cannot combine --reverse with --graph");
2832 if (revs->reflog_info && revs->graph)
2833 die("cannot combine --walk-reflogs with --graph");
2834 if (revs->no_walk && revs->graph)
2835 die("cannot combine --no-walk with --graph");
2836 if (!revs->reflog_info && revs->grep_filter.use_reflog_filter)
2837 die("cannot use --grep-reflog without --walk-reflogs");
2839 if (revs->first_parent_only && revs->bisect)
2840 die(_("--first-parent is incompatible with --bisect"));
2842 if (revs->line_level_traverse &&
2843 (revs->diffopt.output_format & ~(DIFF_FORMAT_PATCH | DIFF_FORMAT_NO_OUTPUT)))
2844 die(_("-L does not yet support diff formats besides -p and -s"));
2846 if (revs->expand_tabs_in_log < 0)
2847 revs->expand_tabs_in_log = revs->expand_tabs_in_log_default;
2849 return left;
2852 static void add_child(struct rev_info *revs, struct commit *parent, struct commit *child)
2854 struct commit_list *l = xcalloc(1, sizeof(*l));
2856 l->item = child;
2857 l->next = add_decoration(&revs->children, &parent->object, l);
2860 static int remove_duplicate_parents(struct rev_info *revs, struct commit *commit)
2862 struct treesame_state *ts = lookup_decoration(&revs->treesame, &commit->object);
2863 struct commit_list **pp, *p;
2864 int surviving_parents;
2866 /* Examine existing parents while marking ones we have seen... */
2867 pp = &commit->parents;
2868 surviving_parents = 0;
2869 while ((p = *pp) != NULL) {
2870 struct commit *parent = p->item;
2871 if (parent->object.flags & TMP_MARK) {
2872 *pp = p->next;
2873 if (ts)
2874 compact_treesame(revs, commit, surviving_parents);
2875 continue;
2877 parent->object.flags |= TMP_MARK;
2878 surviving_parents++;
2879 pp = &p->next;
2881 /* clear the temporary mark */
2882 for (p = commit->parents; p; p = p->next) {
2883 p->item->object.flags &= ~TMP_MARK;
2885 /* no update_treesame() - removing duplicates can't affect TREESAME */
2886 return surviving_parents;
2889 struct merge_simplify_state {
2890 struct commit *simplified;
2893 static struct merge_simplify_state *locate_simplify_state(struct rev_info *revs, struct commit *commit)
2895 struct merge_simplify_state *st;
2897 st = lookup_decoration(&revs->merge_simplification, &commit->object);
2898 if (!st) {
2899 st = xcalloc(1, sizeof(*st));
2900 add_decoration(&revs->merge_simplification, &commit->object, st);
2902 return st;
2905 static int mark_redundant_parents(struct commit *commit)
2907 struct commit_list *h = reduce_heads(commit->parents);
2908 int i = 0, marked = 0;
2909 struct commit_list *po, *pn;
2911 /* Want these for sanity-checking only */
2912 int orig_cnt = commit_list_count(commit->parents);
2913 int cnt = commit_list_count(h);
2916 * Not ready to remove items yet, just mark them for now, based
2917 * on the output of reduce_heads(). reduce_heads outputs the reduced
2918 * set in its original order, so this isn't too hard.
2920 po = commit->parents;
2921 pn = h;
2922 while (po) {
2923 if (pn && po->item == pn->item) {
2924 pn = pn->next;
2925 i++;
2926 } else {
2927 po->item->object.flags |= TMP_MARK;
2928 marked++;
2930 po=po->next;
2933 if (i != cnt || cnt+marked != orig_cnt)
2934 die("mark_redundant_parents %d %d %d %d", orig_cnt, cnt, i, marked);
2936 free_commit_list(h);
2938 return marked;
2941 static int mark_treesame_root_parents(struct commit *commit)
2943 struct commit_list *p;
2944 int marked = 0;
2946 for (p = commit->parents; p; p = p->next) {
2947 struct commit *parent = p->item;
2948 if (!parent->parents && (parent->object.flags & TREESAME)) {
2949 parent->object.flags |= TMP_MARK;
2950 marked++;
2954 return marked;
2958 * Awkward naming - this means one parent we are TREESAME to.
2959 * cf mark_treesame_root_parents: root parents that are TREESAME (to an
2960 * empty tree). Better name suggestions?
2962 static int leave_one_treesame_to_parent(struct rev_info *revs, struct commit *commit)
2964 struct treesame_state *ts = lookup_decoration(&revs->treesame, &commit->object);
2965 struct commit *unmarked = NULL, *marked = NULL;
2966 struct commit_list *p;
2967 unsigned n;
2969 for (p = commit->parents, n = 0; p; p = p->next, n++) {
2970 if (ts->treesame[n]) {
2971 if (p->item->object.flags & TMP_MARK) {
2972 if (!marked)
2973 marked = p->item;
2974 } else {
2975 if (!unmarked) {
2976 unmarked = p->item;
2977 break;
2984 * If we are TREESAME to a marked-for-deletion parent, but not to any
2985 * unmarked parents, unmark the first TREESAME parent. This is the
2986 * parent that the default simplify_history==1 scan would have followed,
2987 * and it doesn't make sense to omit that path when asking for a
2988 * simplified full history. Retaining it improves the chances of
2989 * understanding odd missed merges that took an old version of a file.
2991 * Example:
2993 * I--------*X A modified the file, but mainline merge X used
2994 * \ / "-s ours", so took the version from I. X is
2995 * `-*A--' TREESAME to I and !TREESAME to A.
2997 * Default log from X would produce "I". Without this check,
2998 * --full-history --simplify-merges would produce "I-A-X", showing
2999 * the merge commit X and that it changed A, but not making clear that
3000 * it had just taken the I version. With this check, the topology above
3001 * is retained.
3003 * Note that it is possible that the simplification chooses a different
3004 * TREESAME parent from the default, in which case this test doesn't
3005 * activate, and we _do_ drop the default parent. Example:
3007 * I------X A modified the file, but it was reverted in B,
3008 * \ / meaning mainline merge X is TREESAME to both
3009 * *A-*B parents.
3011 * Default log would produce "I" by following the first parent;
3012 * --full-history --simplify-merges will produce "I-A-B". But this is a
3013 * reasonable result - it presents a logical full history leading from
3014 * I to X, and X is not an important merge.
3016 if (!unmarked && marked) {
3017 marked->object.flags &= ~TMP_MARK;
3018 return 1;
3021 return 0;
3024 static int remove_marked_parents(struct rev_info *revs, struct commit *commit)
3026 struct commit_list **pp, *p;
3027 int nth_parent, removed = 0;
3029 pp = &commit->parents;
3030 nth_parent = 0;
3031 while ((p = *pp) != NULL) {
3032 struct commit *parent = p->item;
3033 if (parent->object.flags & TMP_MARK) {
3034 parent->object.flags &= ~TMP_MARK;
3035 *pp = p->next;
3036 free(p);
3037 removed++;
3038 compact_treesame(revs, commit, nth_parent);
3039 continue;
3041 pp = &p->next;
3042 nth_parent++;
3045 /* Removing parents can only increase TREESAMEness */
3046 if (removed && !(commit->object.flags & TREESAME))
3047 update_treesame(revs, commit);
3049 return nth_parent;
3052 static struct commit_list **simplify_one(struct rev_info *revs, struct commit *commit, struct commit_list **tail)
3054 struct commit_list *p;
3055 struct commit *parent;
3056 struct merge_simplify_state *st, *pst;
3057 int cnt;
3059 st = locate_simplify_state(revs, commit);
3062 * Have we handled this one?
3064 if (st->simplified)
3065 return tail;
3068 * An UNINTERESTING commit simplifies to itself, so does a
3069 * root commit. We do not rewrite parents of such commit
3070 * anyway.
3072 if ((commit->object.flags & UNINTERESTING) || !commit->parents) {
3073 st->simplified = commit;
3074 return tail;
3078 * Do we know what commit all of our parents that matter
3079 * should be rewritten to? Otherwise we are not ready to
3080 * rewrite this one yet.
3082 for (cnt = 0, p = commit->parents; p; p = p->next) {
3083 pst = locate_simplify_state(revs, p->item);
3084 if (!pst->simplified) {
3085 tail = &commit_list_insert(p->item, tail)->next;
3086 cnt++;
3088 if (revs->first_parent_only)
3089 break;
3091 if (cnt) {
3092 tail = &commit_list_insert(commit, tail)->next;
3093 return tail;
3097 * Rewrite our list of parents. Note that this cannot
3098 * affect our TREESAME flags in any way - a commit is
3099 * always TREESAME to its simplification.
3101 for (p = commit->parents; p; p = p->next) {
3102 pst = locate_simplify_state(revs, p->item);
3103 p->item = pst->simplified;
3104 if (revs->first_parent_only)
3105 break;
3108 if (revs->first_parent_only)
3109 cnt = 1;
3110 else
3111 cnt = remove_duplicate_parents(revs, commit);
3114 * It is possible that we are a merge and one side branch
3115 * does not have any commit that touches the given paths;
3116 * in such a case, the immediate parent from that branch
3117 * will be rewritten to be the merge base.
3119 * o----X X: the commit we are looking at;
3120 * / / o: a commit that touches the paths;
3121 * ---o----'
3123 * Further, a merge of an independent branch that doesn't
3124 * touch the path will reduce to a treesame root parent:
3126 * ----o----X X: the commit we are looking at;
3127 * / o: a commit that touches the paths;
3128 * r r: a root commit not touching the paths
3130 * Detect and simplify both cases.
3132 if (1 < cnt) {
3133 int marked = mark_redundant_parents(commit);
3134 marked += mark_treesame_root_parents(commit);
3135 if (marked)
3136 marked -= leave_one_treesame_to_parent(revs, commit);
3137 if (marked)
3138 cnt = remove_marked_parents(revs, commit);
3142 * A commit simplifies to itself if it is a root, if it is
3143 * UNINTERESTING, if it touches the given paths, or if it is a
3144 * merge and its parents don't simplify to one relevant commit
3145 * (the first two cases are already handled at the beginning of
3146 * this function).
3148 * Otherwise, it simplifies to what its sole relevant parent
3149 * simplifies to.
3151 if (!cnt ||
3152 (commit->object.flags & UNINTERESTING) ||
3153 !(commit->object.flags & TREESAME) ||
3154 (parent = one_relevant_parent(revs, commit->parents)) == NULL)
3155 st->simplified = commit;
3156 else {
3157 pst = locate_simplify_state(revs, parent);
3158 st->simplified = pst->simplified;
3160 return tail;
3163 static void simplify_merges(struct rev_info *revs)
3165 struct commit_list *list, *next;
3166 struct commit_list *yet_to_do, **tail;
3167 struct commit *commit;
3169 if (!revs->prune)
3170 return;
3172 /* feed the list reversed */
3173 yet_to_do = NULL;
3174 for (list = revs->commits; list; list = next) {
3175 commit = list->item;
3176 next = list->next;
3178 * Do not free(list) here yet; the original list
3179 * is used later in this function.
3181 commit_list_insert(commit, &yet_to_do);
3183 while (yet_to_do) {
3184 list = yet_to_do;
3185 yet_to_do = NULL;
3186 tail = &yet_to_do;
3187 while (list) {
3188 commit = pop_commit(&list);
3189 tail = simplify_one(revs, commit, tail);
3193 /* clean up the result, removing the simplified ones */
3194 list = revs->commits;
3195 revs->commits = NULL;
3196 tail = &revs->commits;
3197 while (list) {
3198 struct merge_simplify_state *st;
3200 commit = pop_commit(&list);
3201 st = locate_simplify_state(revs, commit);
3202 if (st->simplified == commit)
3203 tail = &commit_list_insert(commit, tail)->next;
3207 static void set_children(struct rev_info *revs)
3209 struct commit_list *l;
3210 for (l = revs->commits; l; l = l->next) {
3211 struct commit *commit = l->item;
3212 struct commit_list *p;
3214 for (p = commit->parents; p; p = p->next)
3215 add_child(revs, p->item, commit);
3219 void reset_revision_walk(void)
3221 clear_object_flags(SEEN | ADDED | SHOWN | TOPO_WALK_EXPLORED | TOPO_WALK_INDEGREE);
3224 static int mark_uninteresting(const struct object_id *oid,
3225 struct packed_git *pack,
3226 uint32_t pos,
3227 void *cb)
3229 struct rev_info *revs = cb;
3230 struct object *o = parse_object(revs->repo, oid);
3231 o->flags |= UNINTERESTING | SEEN;
3232 return 0;
3235 define_commit_slab(indegree_slab, int);
3236 define_commit_slab(author_date_slab, timestamp_t);
3238 struct topo_walk_info {
3239 uint32_t min_generation;
3240 struct prio_queue explore_queue;
3241 struct prio_queue indegree_queue;
3242 struct prio_queue topo_queue;
3243 struct indegree_slab indegree;
3244 struct author_date_slab author_date;
3247 static inline void test_flag_and_insert(struct prio_queue *q, struct commit *c, int flag)
3249 if (c->object.flags & flag)
3250 return;
3252 c->object.flags |= flag;
3253 prio_queue_put(q, c);
3256 static void explore_walk_step(struct rev_info *revs)
3258 struct topo_walk_info *info = revs->topo_walk_info;
3259 struct commit_list *p;
3260 struct commit *c = prio_queue_get(&info->explore_queue);
3262 if (!c)
3263 return;
3265 if (parse_commit_gently(c, 1) < 0)
3266 return;
3268 if (revs->sort_order == REV_SORT_BY_AUTHOR_DATE)
3269 record_author_date(&info->author_date, c);
3271 if (revs->max_age != -1 && (c->date < revs->max_age))
3272 c->object.flags |= UNINTERESTING;
3274 if (process_parents(revs, c, NULL, NULL) < 0)
3275 return;
3277 if (c->object.flags & UNINTERESTING)
3278 mark_parents_uninteresting(c);
3280 for (p = c->parents; p; p = p->next)
3281 test_flag_and_insert(&info->explore_queue, p->item, TOPO_WALK_EXPLORED);
3284 static void explore_to_depth(struct rev_info *revs,
3285 uint32_t gen_cutoff)
3287 struct topo_walk_info *info = revs->topo_walk_info;
3288 struct commit *c;
3289 while ((c = prio_queue_peek(&info->explore_queue)) &&
3290 c->generation >= gen_cutoff)
3291 explore_walk_step(revs);
3294 static void indegree_walk_step(struct rev_info *revs)
3296 struct commit_list *p;
3297 struct topo_walk_info *info = revs->topo_walk_info;
3298 struct commit *c = prio_queue_get(&info->indegree_queue);
3300 if (!c)
3301 return;
3303 if (parse_commit_gently(c, 1) < 0)
3304 return;
3306 explore_to_depth(revs, c->generation);
3308 for (p = c->parents; p; p = p->next) {
3309 struct commit *parent = p->item;
3310 int *pi = indegree_slab_at(&info->indegree, parent);
3312 if (*pi)
3313 (*pi)++;
3314 else
3315 *pi = 2;
3317 test_flag_and_insert(&info->indegree_queue, parent, TOPO_WALK_INDEGREE);
3319 if (revs->first_parent_only)
3320 return;
3324 static void compute_indegrees_to_depth(struct rev_info *revs,
3325 uint32_t gen_cutoff)
3327 struct topo_walk_info *info = revs->topo_walk_info;
3328 struct commit *c;
3329 while ((c = prio_queue_peek(&info->indegree_queue)) &&
3330 c->generation >= gen_cutoff)
3331 indegree_walk_step(revs);
3334 static void reset_topo_walk(struct rev_info *revs)
3336 struct topo_walk_info *info = revs->topo_walk_info;
3338 clear_prio_queue(&info->explore_queue);
3339 clear_prio_queue(&info->indegree_queue);
3340 clear_prio_queue(&info->topo_queue);
3341 clear_indegree_slab(&info->indegree);
3342 clear_author_date_slab(&info->author_date);
3344 FREE_AND_NULL(revs->topo_walk_info);
3347 static void init_topo_walk(struct rev_info *revs)
3349 struct topo_walk_info *info;
3350 struct commit_list *list;
3351 if (revs->topo_walk_info)
3352 reset_topo_walk(revs);
3354 revs->topo_walk_info = xmalloc(sizeof(struct topo_walk_info));
3355 info = revs->topo_walk_info;
3356 memset(info, 0, sizeof(struct topo_walk_info));
3358 init_indegree_slab(&info->indegree);
3359 memset(&info->explore_queue, 0, sizeof(info->explore_queue));
3360 memset(&info->indegree_queue, 0, sizeof(info->indegree_queue));
3361 memset(&info->topo_queue, 0, sizeof(info->topo_queue));
3363 switch (revs->sort_order) {
3364 default: /* REV_SORT_IN_GRAPH_ORDER */
3365 info->topo_queue.compare = NULL;
3366 break;
3367 case REV_SORT_BY_COMMIT_DATE:
3368 info->topo_queue.compare = compare_commits_by_commit_date;
3369 break;
3370 case REV_SORT_BY_AUTHOR_DATE:
3371 init_author_date_slab(&info->author_date);
3372 info->topo_queue.compare = compare_commits_by_author_date;
3373 info->topo_queue.cb_data = &info->author_date;
3374 break;
3377 info->explore_queue.compare = compare_commits_by_gen_then_commit_date;
3378 info->indegree_queue.compare = compare_commits_by_gen_then_commit_date;
3380 info->min_generation = GENERATION_NUMBER_INFINITY;
3381 for (list = revs->commits; list; list = list->next) {
3382 struct commit *c = list->item;
3384 if (parse_commit_gently(c, 1))
3385 continue;
3387 test_flag_and_insert(&info->explore_queue, c, TOPO_WALK_EXPLORED);
3388 test_flag_and_insert(&info->indegree_queue, c, TOPO_WALK_INDEGREE);
3390 if (c->generation < info->min_generation)
3391 info->min_generation = c->generation;
3393 *(indegree_slab_at(&info->indegree, c)) = 1;
3395 if (revs->sort_order == REV_SORT_BY_AUTHOR_DATE)
3396 record_author_date(&info->author_date, c);
3398 compute_indegrees_to_depth(revs, info->min_generation);
3400 for (list = revs->commits; list; list = list->next) {
3401 struct commit *c = list->item;
3403 if (*(indegree_slab_at(&info->indegree, c)) == 1)
3404 prio_queue_put(&info->topo_queue, c);
3408 * This is unfortunate; the initial tips need to be shown
3409 * in the order given from the revision traversal machinery.
3411 if (revs->sort_order == REV_SORT_IN_GRAPH_ORDER)
3412 prio_queue_reverse(&info->topo_queue);
3415 static struct commit *next_topo_commit(struct rev_info *revs)
3417 struct commit *c;
3418 struct topo_walk_info *info = revs->topo_walk_info;
3420 /* pop next off of topo_queue */
3421 c = prio_queue_get(&info->topo_queue);
3423 if (c)
3424 *(indegree_slab_at(&info->indegree, c)) = 0;
3426 return c;
3429 static void expand_topo_walk(struct rev_info *revs, struct commit *commit)
3431 struct commit_list *p;
3432 struct topo_walk_info *info = revs->topo_walk_info;
3433 if (process_parents(revs, commit, NULL, NULL) < 0) {
3434 if (!revs->ignore_missing_links)
3435 die("Failed to traverse parents of commit %s",
3436 oid_to_hex(&commit->object.oid));
3439 for (p = commit->parents; p; p = p->next) {
3440 struct commit *parent = p->item;
3441 int *pi;
3443 if (parent->object.flags & UNINTERESTING)
3444 continue;
3446 if (parse_commit_gently(parent, 1) < 0)
3447 continue;
3449 if (parent->generation < info->min_generation) {
3450 info->min_generation = parent->generation;
3451 compute_indegrees_to_depth(revs, info->min_generation);
3454 pi = indegree_slab_at(&info->indegree, parent);
3456 (*pi)--;
3457 if (*pi == 1)
3458 prio_queue_put(&info->topo_queue, parent);
3460 if (revs->first_parent_only)
3461 return;
3465 int prepare_revision_walk(struct rev_info *revs)
3467 int i;
3468 struct object_array old_pending;
3469 struct commit_list **next = &revs->commits;
3471 memcpy(&old_pending, &revs->pending, sizeof(old_pending));
3472 revs->pending.nr = 0;
3473 revs->pending.alloc = 0;
3474 revs->pending.objects = NULL;
3475 for (i = 0; i < old_pending.nr; i++) {
3476 struct object_array_entry *e = old_pending.objects + i;
3477 struct commit *commit = handle_commit(revs, e);
3478 if (commit) {
3479 if (!(commit->object.flags & SEEN)) {
3480 commit->object.flags |= SEEN;
3481 next = commit_list_append(commit, next);
3485 object_array_clear(&old_pending);
3487 /* Signal whether we need per-parent treesame decoration */
3488 if (revs->simplify_merges ||
3489 (revs->limited && limiting_can_increase_treesame(revs)))
3490 revs->treesame.name = "treesame";
3492 if (revs->exclude_promisor_objects) {
3493 for_each_packed_object(mark_uninteresting, revs,
3494 FOR_EACH_OBJECT_PROMISOR_ONLY);
3497 if (!revs->reflog_info)
3498 prepare_to_use_bloom_filter(revs);
3499 if (revs->no_walk != REVISION_WALK_NO_WALK_UNSORTED)
3500 commit_list_sort_by_date(&revs->commits);
3501 if (revs->no_walk)
3502 return 0;
3503 if (revs->limited) {
3504 if (limit_list(revs) < 0)
3505 return -1;
3506 if (revs->topo_order)
3507 sort_in_topological_order(&revs->commits, revs->sort_order);
3508 } else if (revs->topo_order)
3509 init_topo_walk(revs);
3510 if (revs->line_level_traverse && want_ancestry(revs))
3512 * At the moment we can only do line-level log with parent
3513 * rewriting by performing this expensive pre-filtering step.
3514 * If parent rewriting is not requested, then we rather
3515 * perform the line-level log filtering during the regular
3516 * history traversal.
3518 line_log_filter(revs);
3519 if (revs->simplify_merges)
3520 simplify_merges(revs);
3521 if (revs->children.name)
3522 set_children(revs);
3524 return 0;
3527 static enum rewrite_result rewrite_one_1(struct rev_info *revs,
3528 struct commit **pp,
3529 struct prio_queue *queue)
3531 for (;;) {
3532 struct commit *p = *pp;
3533 if (!revs->limited)
3534 if (process_parents(revs, p, NULL, queue) < 0)
3535 return rewrite_one_error;
3536 if (p->object.flags & UNINTERESTING)
3537 return rewrite_one_ok;
3538 if (!(p->object.flags & TREESAME))
3539 return rewrite_one_ok;
3540 if (!p->parents)
3541 return rewrite_one_noparents;
3542 if ((p = one_relevant_parent(revs, p->parents)) == NULL)
3543 return rewrite_one_ok;
3544 *pp = p;
3548 static void merge_queue_into_list(struct prio_queue *q, struct commit_list **list)
3550 while (q->nr) {
3551 struct commit *item = prio_queue_peek(q);
3552 struct commit_list *p = *list;
3554 if (p && p->item->date >= item->date)
3555 list = &p->next;
3556 else {
3557 p = commit_list_insert(item, list);
3558 list = &p->next; /* skip newly added item */
3559 prio_queue_get(q); /* pop item */
3564 static enum rewrite_result rewrite_one(struct rev_info *revs, struct commit **pp)
3566 struct prio_queue queue = { compare_commits_by_commit_date };
3567 enum rewrite_result ret = rewrite_one_1(revs, pp, &queue);
3568 merge_queue_into_list(&queue, &revs->commits);
3569 clear_prio_queue(&queue);
3570 return ret;
3573 int rewrite_parents(struct rev_info *revs, struct commit *commit,
3574 rewrite_parent_fn_t rewrite_parent)
3576 struct commit_list **pp = &commit->parents;
3577 while (*pp) {
3578 struct commit_list *parent = *pp;
3579 switch (rewrite_parent(revs, &parent->item)) {
3580 case rewrite_one_ok:
3581 break;
3582 case rewrite_one_noparents:
3583 *pp = parent->next;
3584 continue;
3585 case rewrite_one_error:
3586 return -1;
3588 pp = &parent->next;
3590 remove_duplicate_parents(revs, commit);
3591 return 0;
3594 static int commit_rewrite_person(struct strbuf *buf, const char *what, struct string_list *mailmap)
3596 char *person, *endp;
3597 size_t len, namelen, maillen;
3598 const char *name;
3599 const char *mail;
3600 struct ident_split ident;
3602 person = strstr(buf->buf, what);
3603 if (!person)
3604 return 0;
3606 person += strlen(what);
3607 endp = strchr(person, '\n');
3608 if (!endp)
3609 return 0;
3611 len = endp - person;
3613 if (split_ident_line(&ident, person, len))
3614 return 0;
3616 mail = ident.mail_begin;
3617 maillen = ident.mail_end - ident.mail_begin;
3618 name = ident.name_begin;
3619 namelen = ident.name_end - ident.name_begin;
3621 if (map_user(mailmap, &mail, &maillen, &name, &namelen)) {
3622 struct strbuf namemail = STRBUF_INIT;
3624 strbuf_addf(&namemail, "%.*s <%.*s>",
3625 (int)namelen, name, (int)maillen, mail);
3627 strbuf_splice(buf, ident.name_begin - buf->buf,
3628 ident.mail_end - ident.name_begin + 1,
3629 namemail.buf, namemail.len);
3631 strbuf_release(&namemail);
3633 return 1;
3636 return 0;
3639 static int commit_match(struct commit *commit, struct rev_info *opt)
3641 int retval;
3642 const char *encoding;
3643 const char *message;
3644 struct strbuf buf = STRBUF_INIT;
3646 if (!opt->grep_filter.pattern_list && !opt->grep_filter.header_list)
3647 return 1;
3649 /* Prepend "fake" headers as needed */
3650 if (opt->grep_filter.use_reflog_filter) {
3651 strbuf_addstr(&buf, "reflog ");
3652 get_reflog_message(&buf, opt->reflog_info);
3653 strbuf_addch(&buf, '\n');
3657 * We grep in the user's output encoding, under the assumption that it
3658 * is the encoding they are most likely to write their grep pattern
3659 * for. In addition, it means we will match the "notes" encoding below,
3660 * so we will not end up with a buffer that has two different encodings
3661 * in it.
3663 encoding = get_log_output_encoding();
3664 message = logmsg_reencode(commit, NULL, encoding);
3666 /* Copy the commit to temporary if we are using "fake" headers */
3667 if (buf.len)
3668 strbuf_addstr(&buf, message);
3670 if (opt->grep_filter.header_list && opt->mailmap) {
3671 if (!buf.len)
3672 strbuf_addstr(&buf, message);
3674 commit_rewrite_person(&buf, "\nauthor ", opt->mailmap);
3675 commit_rewrite_person(&buf, "\ncommitter ", opt->mailmap);
3678 /* Append "fake" message parts as needed */
3679 if (opt->show_notes) {
3680 if (!buf.len)
3681 strbuf_addstr(&buf, message);
3682 format_display_notes(&commit->object.oid, &buf, encoding, 1);
3686 * Find either in the original commit message, or in the temporary.
3687 * Note that we cast away the constness of "message" here. It is
3688 * const because it may come from the cached commit buffer. That's OK,
3689 * because we know that it is modifiable heap memory, and that while
3690 * grep_buffer may modify it for speed, it will restore any
3691 * changes before returning.
3693 if (buf.len)
3694 retval = grep_buffer(&opt->grep_filter, buf.buf, buf.len);
3695 else
3696 retval = grep_buffer(&opt->grep_filter,
3697 (char *)message, strlen(message));
3698 strbuf_release(&buf);
3699 unuse_commit_buffer(commit, message);
3700 return opt->invert_grep ? !retval : retval;
3703 static inline int want_ancestry(const struct rev_info *revs)
3705 return (revs->rewrite_parents || revs->children.name);
3709 * Return a timestamp to be used for --since/--until comparisons for this
3710 * commit, based on the revision options.
3712 static timestamp_t comparison_date(const struct rev_info *revs,
3713 struct commit *commit)
3715 return revs->reflog_info ?
3716 get_reflog_timestamp(revs->reflog_info) :
3717 commit->date;
3720 enum commit_action get_commit_action(struct rev_info *revs, struct commit *commit)
3722 if (commit->object.flags & SHOWN)
3723 return commit_ignore;
3724 if (revs->unpacked && has_object_pack(&commit->object.oid))
3725 return commit_ignore;
3726 if (commit->object.flags & UNINTERESTING)
3727 return commit_ignore;
3728 if (revs->line_level_traverse && !want_ancestry(revs)) {
3730 * In case of line-level log with parent rewriting
3731 * prepare_revision_walk() already took care of all line-level
3732 * log filtering, and there is nothing left to do here.
3734 * If parent rewriting was not requested, then this is the
3735 * place to perform the line-level log filtering. Notably,
3736 * this check, though expensive, must come before the other,
3737 * cheaper filtering conditions, because the tracked line
3738 * ranges must be adjusted even when the commit will end up
3739 * being ignored based on other conditions.
3741 if (!line_log_process_ranges_arbitrary_commit(revs, commit))
3742 return commit_ignore;
3744 if (revs->min_age != -1 &&
3745 comparison_date(revs, commit) > revs->min_age)
3746 return commit_ignore;
3747 if (revs->min_parents || (revs->max_parents >= 0)) {
3748 int n = commit_list_count(commit->parents);
3749 if ((n < revs->min_parents) ||
3750 ((revs->max_parents >= 0) && (n > revs->max_parents)))
3751 return commit_ignore;
3753 if (!commit_match(commit, revs))
3754 return commit_ignore;
3755 if (revs->prune && revs->dense) {
3756 /* Commit without changes? */
3757 if (commit->object.flags & TREESAME) {
3758 int n;
3759 struct commit_list *p;
3760 /* drop merges unless we want parenthood */
3761 if (!want_ancestry(revs))
3762 return commit_ignore;
3764 * If we want ancestry, then need to keep any merges
3765 * between relevant commits to tie together topology.
3766 * For consistency with TREESAME and simplification
3767 * use "relevant" here rather than just INTERESTING,
3768 * to treat bottom commit(s) as part of the topology.
3770 for (n = 0, p = commit->parents; p; p = p->next)
3771 if (relevant_commit(p->item))
3772 if (++n >= 2)
3773 return commit_show;
3774 return commit_ignore;
3777 return commit_show;
3780 define_commit_slab(saved_parents, struct commit_list *);
3782 #define EMPTY_PARENT_LIST ((struct commit_list *)-1)
3785 * You may only call save_parents() once per commit (this is checked
3786 * for non-root commits).
3788 static void save_parents(struct rev_info *revs, struct commit *commit)
3790 struct commit_list **pp;
3792 if (!revs->saved_parents_slab) {
3793 revs->saved_parents_slab = xmalloc(sizeof(struct saved_parents));
3794 init_saved_parents(revs->saved_parents_slab);
3797 pp = saved_parents_at(revs->saved_parents_slab, commit);
3800 * When walking with reflogs, we may visit the same commit
3801 * several times: once for each appearance in the reflog.
3803 * In this case, save_parents() will be called multiple times.
3804 * We want to keep only the first set of parents. We need to
3805 * store a sentinel value for an empty (i.e., NULL) parent
3806 * list to distinguish it from a not-yet-saved list, however.
3808 if (*pp)
3809 return;
3810 if (commit->parents)
3811 *pp = copy_commit_list(commit->parents);
3812 else
3813 *pp = EMPTY_PARENT_LIST;
3816 static void free_saved_parents(struct rev_info *revs)
3818 if (revs->saved_parents_slab)
3819 clear_saved_parents(revs->saved_parents_slab);
3822 struct commit_list *get_saved_parents(struct rev_info *revs, const struct commit *commit)
3824 struct commit_list *parents;
3826 if (!revs->saved_parents_slab)
3827 return commit->parents;
3829 parents = *saved_parents_at(revs->saved_parents_slab, commit);
3830 if (parents == EMPTY_PARENT_LIST)
3831 return NULL;
3832 return parents;
3835 enum commit_action simplify_commit(struct rev_info *revs, struct commit *commit)
3837 enum commit_action action = get_commit_action(revs, commit);
3839 if (action == commit_show &&
3840 revs->prune && revs->dense && want_ancestry(revs)) {
3842 * --full-diff on simplified parents is no good: it
3843 * will show spurious changes from the commits that
3844 * were elided. So we save the parents on the side
3845 * when --full-diff is in effect.
3847 if (revs->full_diff)
3848 save_parents(revs, commit);
3849 if (rewrite_parents(revs, commit, rewrite_one) < 0)
3850 return commit_error;
3852 return action;
3855 static void track_linear(struct rev_info *revs, struct commit *commit)
3857 if (revs->track_first_time) {
3858 revs->linear = 1;
3859 revs->track_first_time = 0;
3860 } else {
3861 struct commit_list *p;
3862 for (p = revs->previous_parents; p; p = p->next)
3863 if (p->item == NULL || /* first commit */
3864 oideq(&p->item->object.oid, &commit->object.oid))
3865 break;
3866 revs->linear = p != NULL;
3868 if (revs->reverse) {
3869 if (revs->linear)
3870 commit->object.flags |= TRACK_LINEAR;
3872 free_commit_list(revs->previous_parents);
3873 revs->previous_parents = copy_commit_list(commit->parents);
3876 static struct commit *get_revision_1(struct rev_info *revs)
3878 while (1) {
3879 struct commit *commit;
3881 if (revs->reflog_info)
3882 commit = next_reflog_entry(revs->reflog_info);
3883 else if (revs->topo_walk_info)
3884 commit = next_topo_commit(revs);
3885 else
3886 commit = pop_commit(&revs->commits);
3888 if (!commit)
3889 return NULL;
3891 if (revs->reflog_info)
3892 commit->object.flags &= ~(ADDED | SEEN | SHOWN);
3895 * If we haven't done the list limiting, we need to look at
3896 * the parents here. We also need to do the date-based limiting
3897 * that we'd otherwise have done in limit_list().
3899 if (!revs->limited) {
3900 if (revs->max_age != -1 &&
3901 comparison_date(revs, commit) < revs->max_age)
3902 continue;
3904 if (revs->reflog_info)
3905 try_to_simplify_commit(revs, commit);
3906 else if (revs->topo_walk_info)
3907 expand_topo_walk(revs, commit);
3908 else if (process_parents(revs, commit, &revs->commits, NULL) < 0) {
3909 if (!revs->ignore_missing_links)
3910 die("Failed to traverse parents of commit %s",
3911 oid_to_hex(&commit->object.oid));
3915 switch (simplify_commit(revs, commit)) {
3916 case commit_ignore:
3917 continue;
3918 case commit_error:
3919 die("Failed to simplify parents of commit %s",
3920 oid_to_hex(&commit->object.oid));
3921 default:
3922 if (revs->track_linear)
3923 track_linear(revs, commit);
3924 return commit;
3930 * Return true for entries that have not yet been shown. (This is an
3931 * object_array_each_func_t.)
3933 static int entry_unshown(struct object_array_entry *entry, void *cb_data_unused)
3935 return !(entry->item->flags & SHOWN);
3939 * If array is on the verge of a realloc, garbage-collect any entries
3940 * that have already been shown to try to free up some space.
3942 static void gc_boundary(struct object_array *array)
3944 if (array->nr == array->alloc)
3945 object_array_filter(array, entry_unshown, NULL);
3948 static void create_boundary_commit_list(struct rev_info *revs)
3950 unsigned i;
3951 struct commit *c;
3952 struct object_array *array = &revs->boundary_commits;
3953 struct object_array_entry *objects = array->objects;
3956 * If revs->commits is non-NULL at this point, an error occurred in
3957 * get_revision_1(). Ignore the error and continue printing the
3958 * boundary commits anyway. (This is what the code has always
3959 * done.)
3961 if (revs->commits) {
3962 free_commit_list(revs->commits);
3963 revs->commits = NULL;
3967 * Put all of the actual boundary commits from revs->boundary_commits
3968 * into revs->commits
3970 for (i = 0; i < array->nr; i++) {
3971 c = (struct commit *)(objects[i].item);
3972 if (!c)
3973 continue;
3974 if (!(c->object.flags & CHILD_SHOWN))
3975 continue;
3976 if (c->object.flags & (SHOWN | BOUNDARY))
3977 continue;
3978 c->object.flags |= BOUNDARY;
3979 commit_list_insert(c, &revs->commits);
3983 * If revs->topo_order is set, sort the boundary commits
3984 * in topological order
3986 sort_in_topological_order(&revs->commits, revs->sort_order);
3989 static struct commit *get_revision_internal(struct rev_info *revs)
3991 struct commit *c = NULL;
3992 struct commit_list *l;
3994 if (revs->boundary == 2) {
3996 * All of the normal commits have already been returned,
3997 * and we are now returning boundary commits.
3998 * create_boundary_commit_list() has populated
3999 * revs->commits with the remaining commits to return.
4001 c = pop_commit(&revs->commits);
4002 if (c)
4003 c->object.flags |= SHOWN;
4004 return c;
4008 * If our max_count counter has reached zero, then we are done. We
4009 * don't simply return NULL because we still might need to show
4010 * boundary commits. But we want to avoid calling get_revision_1, which
4011 * might do a considerable amount of work finding the next commit only
4012 * for us to throw it away.
4014 * If it is non-zero, then either we don't have a max_count at all
4015 * (-1), or it is still counting, in which case we decrement.
4017 if (revs->max_count) {
4018 c = get_revision_1(revs);
4019 if (c) {
4020 while (revs->skip_count > 0) {
4021 revs->skip_count--;
4022 c = get_revision_1(revs);
4023 if (!c)
4024 break;
4028 if (revs->max_count > 0)
4029 revs->max_count--;
4032 if (c)
4033 c->object.flags |= SHOWN;
4035 if (!revs->boundary)
4036 return c;
4038 if (!c) {
4040 * get_revision_1() runs out the commits, and
4041 * we are done computing the boundaries.
4042 * switch to boundary commits output mode.
4044 revs->boundary = 2;
4047 * Update revs->commits to contain the list of
4048 * boundary commits.
4050 create_boundary_commit_list(revs);
4052 return get_revision_internal(revs);
4056 * boundary commits are the commits that are parents of the
4057 * ones we got from get_revision_1() but they themselves are
4058 * not returned from get_revision_1(). Before returning
4059 * 'c', we need to mark its parents that they could be boundaries.
4062 for (l = c->parents; l; l = l->next) {
4063 struct object *p;
4064 p = &(l->item->object);
4065 if (p->flags & (CHILD_SHOWN | SHOWN))
4066 continue;
4067 p->flags |= CHILD_SHOWN;
4068 gc_boundary(&revs->boundary_commits);
4069 add_object_array(p, NULL, &revs->boundary_commits);
4072 return c;
4075 struct commit *get_revision(struct rev_info *revs)
4077 struct commit *c;
4078 struct commit_list *reversed;
4080 if (revs->reverse) {
4081 reversed = NULL;
4082 while ((c = get_revision_internal(revs)))
4083 commit_list_insert(c, &reversed);
4084 revs->commits = reversed;
4085 revs->reverse = 0;
4086 revs->reverse_output_stage = 1;
4089 if (revs->reverse_output_stage) {
4090 c = pop_commit(&revs->commits);
4091 if (revs->track_linear)
4092 revs->linear = !!(c && c->object.flags & TRACK_LINEAR);
4093 return c;
4096 c = get_revision_internal(revs);
4097 if (c && revs->graph)
4098 graph_update(revs->graph, c);
4099 if (!c) {
4100 free_saved_parents(revs);
4101 if (revs->previous_parents) {
4102 free_commit_list(revs->previous_parents);
4103 revs->previous_parents = NULL;
4106 return c;
4109 const char *get_revision_mark(const struct rev_info *revs, const struct commit *commit)
4111 if (commit->object.flags & BOUNDARY)
4112 return "-";
4113 else if (commit->object.flags & UNINTERESTING)
4114 return "^";
4115 else if (commit->object.flags & PATCHSAME)
4116 return "=";
4117 else if (!revs || revs->left_right) {
4118 if (commit->object.flags & SYMMETRIC_LEFT)
4119 return "<";
4120 else
4121 return ">";
4122 } else if (revs->graph)
4123 return "*";
4124 else if (revs->cherry_mark)
4125 return "+";
4126 return "";
4129 void put_revision_mark(const struct rev_info *revs, const struct commit *commit)
4131 const char *mark = get_revision_mark(revs, commit);
4132 if (!strlen(mark))
4133 return;
4134 fputs(mark, stdout);
4135 putchar(' ');