Merge branch 'jk/unused-post-2.39-part2'
[git/gitster.git] / revision.c
blob113a33d195e3893a111a0bf917bafcc066b43fba
1 #include "git-compat-util.h"
2 #include "alloc.h"
3 #include "config.h"
4 #include "hex.h"
5 #include "object-store.h"
6 #include "tag.h"
7 #include "blob.h"
8 #include "tree.h"
9 #include "commit.h"
10 #include "diff.h"
11 #include "diff-merges.h"
12 #include "refs.h"
13 #include "revision.h"
14 #include "repository.h"
15 #include "graph.h"
16 #include "grep.h"
17 #include "reflog-walk.h"
18 #include "patch-ids.h"
19 #include "decorate.h"
20 #include "log-tree.h"
21 #include "string-list.h"
22 #include "line-log.h"
23 #include "mailmap.h"
24 #include "commit-slab.h"
25 #include "dir.h"
26 #include "cache-tree.h"
27 #include "bisect.h"
28 #include "packfile.h"
29 #include "worktree.h"
30 #include "strvec.h"
31 #include "commit-reach.h"
32 #include "commit-graph.h"
33 #include "prio-queue.h"
34 #include "hashmap.h"
35 #include "utf8.h"
36 #include "bloom.h"
37 #include "json-writer.h"
38 #include "list-objects-filter-options.h"
39 #include "resolve-undo.h"
41 volatile show_early_output_fn_t show_early_output;
43 static const char *term_bad;
44 static const char *term_good;
46 implement_shared_commit_slab(revision_sources, char *);
48 static inline int want_ancestry(const struct rev_info *revs);
50 void show_object_with_name(FILE *out, struct object *obj, const char *name)
52 fprintf(out, "%s ", oid_to_hex(&obj->oid));
53 for (const char *p = name; *p && *p != '\n'; p++)
54 fputc(*p, out);
55 fputc('\n', out);
58 static void mark_blob_uninteresting(struct blob *blob)
60 if (!blob)
61 return;
62 if (blob->object.flags & UNINTERESTING)
63 return;
64 blob->object.flags |= UNINTERESTING;
67 static void mark_tree_contents_uninteresting(struct repository *r,
68 struct tree *tree)
70 struct tree_desc desc;
71 struct name_entry entry;
73 if (parse_tree_gently(tree, 1) < 0)
74 return;
76 init_tree_desc(&desc, tree->buffer, tree->size);
77 while (tree_entry(&desc, &entry)) {
78 switch (object_type(entry.mode)) {
79 case OBJ_TREE:
80 mark_tree_uninteresting(r, lookup_tree(r, &entry.oid));
81 break;
82 case OBJ_BLOB:
83 mark_blob_uninteresting(lookup_blob(r, &entry.oid));
84 break;
85 default:
86 /* Subproject commit - not in this repository */
87 break;
92 * We don't care about the tree any more
93 * after it has been marked uninteresting.
95 free_tree_buffer(tree);
98 void mark_tree_uninteresting(struct repository *r, struct tree *tree)
100 struct object *obj;
102 if (!tree)
103 return;
105 obj = &tree->object;
106 if (obj->flags & UNINTERESTING)
107 return;
108 obj->flags |= UNINTERESTING;
109 mark_tree_contents_uninteresting(r, tree);
112 struct path_and_oids_entry {
113 struct hashmap_entry ent;
114 char *path;
115 struct oidset trees;
118 static int path_and_oids_cmp(const void *hashmap_cmp_fn_data UNUSED,
119 const struct hashmap_entry *eptr,
120 const struct hashmap_entry *entry_or_key,
121 const void *keydata UNUSED)
123 const struct path_and_oids_entry *e1, *e2;
125 e1 = container_of(eptr, const struct path_and_oids_entry, ent);
126 e2 = container_of(entry_or_key, const struct path_and_oids_entry, ent);
128 return strcmp(e1->path, e2->path);
131 static void paths_and_oids_clear(struct hashmap *map)
133 struct hashmap_iter iter;
134 struct path_and_oids_entry *entry;
136 hashmap_for_each_entry(map, &iter, entry, ent /* member name */) {
137 oidset_clear(&entry->trees);
138 free(entry->path);
141 hashmap_clear_and_free(map, struct path_and_oids_entry, ent);
144 static void paths_and_oids_insert(struct hashmap *map,
145 const char *path,
146 const struct object_id *oid)
148 int hash = strhash(path);
149 struct path_and_oids_entry key;
150 struct path_and_oids_entry *entry;
152 hashmap_entry_init(&key.ent, hash);
154 /* use a shallow copy for the lookup */
155 key.path = (char *)path;
156 oidset_init(&key.trees, 0);
158 entry = hashmap_get_entry(map, &key, ent, NULL);
159 if (!entry) {
160 CALLOC_ARRAY(entry, 1);
161 hashmap_entry_init(&entry->ent, hash);
162 entry->path = xstrdup(key.path);
163 oidset_init(&entry->trees, 16);
164 hashmap_put(map, &entry->ent);
167 oidset_insert(&entry->trees, oid);
170 static void add_children_by_path(struct repository *r,
171 struct tree *tree,
172 struct hashmap *map)
174 struct tree_desc desc;
175 struct name_entry entry;
177 if (!tree)
178 return;
180 if (parse_tree_gently(tree, 1) < 0)
181 return;
183 init_tree_desc(&desc, tree->buffer, tree->size);
184 while (tree_entry(&desc, &entry)) {
185 switch (object_type(entry.mode)) {
186 case OBJ_TREE:
187 paths_and_oids_insert(map, entry.path, &entry.oid);
189 if (tree->object.flags & UNINTERESTING) {
190 struct tree *child = lookup_tree(r, &entry.oid);
191 if (child)
192 child->object.flags |= UNINTERESTING;
194 break;
195 case OBJ_BLOB:
196 if (tree->object.flags & UNINTERESTING) {
197 struct blob *child = lookup_blob(r, &entry.oid);
198 if (child)
199 child->object.flags |= UNINTERESTING;
201 break;
202 default:
203 /* Subproject commit - not in this repository */
204 break;
208 free_tree_buffer(tree);
211 void mark_trees_uninteresting_sparse(struct repository *r,
212 struct oidset *trees)
214 unsigned has_interesting = 0, has_uninteresting = 0;
215 struct hashmap map = HASHMAP_INIT(path_and_oids_cmp, NULL);
216 struct hashmap_iter map_iter;
217 struct path_and_oids_entry *entry;
218 struct object_id *oid;
219 struct oidset_iter iter;
221 oidset_iter_init(trees, &iter);
222 while ((!has_interesting || !has_uninteresting) &&
223 (oid = oidset_iter_next(&iter))) {
224 struct tree *tree = lookup_tree(r, oid);
226 if (!tree)
227 continue;
229 if (tree->object.flags & UNINTERESTING)
230 has_uninteresting = 1;
231 else
232 has_interesting = 1;
235 /* Do not walk unless we have both types of trees. */
236 if (!has_uninteresting || !has_interesting)
237 return;
239 oidset_iter_init(trees, &iter);
240 while ((oid = oidset_iter_next(&iter))) {
241 struct tree *tree = lookup_tree(r, oid);
242 add_children_by_path(r, tree, &map);
245 hashmap_for_each_entry(&map, &map_iter, entry, ent /* member name */)
246 mark_trees_uninteresting_sparse(r, &entry->trees);
248 paths_and_oids_clear(&map);
251 struct commit_stack {
252 struct commit **items;
253 size_t nr, alloc;
255 #define COMMIT_STACK_INIT { 0 }
257 static void commit_stack_push(struct commit_stack *stack, struct commit *commit)
259 ALLOC_GROW(stack->items, stack->nr + 1, stack->alloc);
260 stack->items[stack->nr++] = commit;
263 static struct commit *commit_stack_pop(struct commit_stack *stack)
265 return stack->nr ? stack->items[--stack->nr] : NULL;
268 static void commit_stack_clear(struct commit_stack *stack)
270 FREE_AND_NULL(stack->items);
271 stack->nr = stack->alloc = 0;
274 static void mark_one_parent_uninteresting(struct rev_info *revs, struct commit *commit,
275 struct commit_stack *pending)
277 struct commit_list *l;
279 if (commit->object.flags & UNINTERESTING)
280 return;
281 commit->object.flags |= UNINTERESTING;
284 * Normally we haven't parsed the parent
285 * yet, so we won't have a parent of a parent
286 * here. However, it may turn out that we've
287 * reached this commit some other way (where it
288 * wasn't uninteresting), in which case we need
289 * to mark its parents recursively too..
291 for (l = commit->parents; l; l = l->next) {
292 commit_stack_push(pending, l->item);
293 if (revs && revs->exclude_first_parent_only)
294 break;
298 void mark_parents_uninteresting(struct rev_info *revs, 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(revs, l->item, &pending);
305 if (revs && revs->exclude_first_parent_only)
306 break;
309 while (pending.nr > 0)
310 mark_one_parent_uninteresting(revs, commit_stack_pop(&pending),
311 &pending);
313 commit_stack_clear(&pending);
316 static void add_pending_object_with_path(struct rev_info *revs,
317 struct object *obj,
318 const char *name, unsigned mode,
319 const char *path)
321 struct interpret_branch_name_options options = { 0 };
322 if (!obj)
323 return;
324 if (revs->no_walk && (obj->flags & UNINTERESTING))
325 revs->no_walk = 0;
326 if (revs->reflog_info && obj->type == OBJ_COMMIT) {
327 struct strbuf buf = STRBUF_INIT;
328 size_t namelen = strlen(name);
329 int len = interpret_branch_name(name, namelen, &buf, &options);
331 if (0 < len && len < namelen && buf.len)
332 strbuf_addstr(&buf, name + len);
333 add_reflog_for_walk(revs->reflog_info,
334 (struct commit *)obj,
335 buf.buf[0] ? buf.buf: name);
336 strbuf_release(&buf);
337 return; /* do not add the commit itself */
339 add_object_array_with_path(obj, name, &revs->pending, mode, path);
342 static void add_pending_object_with_mode(struct rev_info *revs,
343 struct object *obj,
344 const char *name, unsigned mode)
346 add_pending_object_with_path(revs, obj, name, mode, NULL);
349 void add_pending_object(struct rev_info *revs,
350 struct object *obj, const char *name)
352 add_pending_object_with_mode(revs, obj, name, S_IFINVALID);
355 void add_head_to_pending(struct rev_info *revs)
357 struct object_id oid;
358 struct object *obj;
359 if (get_oid("HEAD", &oid))
360 return;
361 obj = parse_object(revs->repo, &oid);
362 if (!obj)
363 return;
364 add_pending_object(revs, obj, "HEAD");
367 static struct object *get_reference(struct rev_info *revs, const char *name,
368 const struct object_id *oid,
369 unsigned int flags)
371 struct object *object;
373 object = parse_object_with_flags(revs->repo, oid,
374 revs->verify_objects ? 0 :
375 PARSE_OBJECT_SKIP_HASH_CHECK);
377 if (!object) {
378 if (revs->ignore_missing)
379 return object;
380 if (revs->exclude_promisor_objects && is_promisor_object(oid))
381 return NULL;
382 die("bad object %s", name);
384 object->flags |= flags;
385 return object;
388 void add_pending_oid(struct rev_info *revs, const char *name,
389 const struct object_id *oid, unsigned int flags)
391 struct object *object = get_reference(revs, name, oid, flags);
392 add_pending_object(revs, object, name);
395 static struct commit *handle_commit(struct rev_info *revs,
396 struct object_array_entry *entry)
398 struct object *object = entry->item;
399 const char *name = entry->name;
400 const char *path = entry->path;
401 unsigned int mode = entry->mode;
402 unsigned long flags = object->flags;
405 * Tag object? Look what it points to..
407 while (object->type == OBJ_TAG) {
408 struct tag *tag = (struct tag *) object;
409 if (revs->tag_objects && !(flags & UNINTERESTING))
410 add_pending_object(revs, object, tag->tag);
411 object = parse_object(revs->repo, get_tagged_oid(tag));
412 if (!object) {
413 if (revs->ignore_missing_links || (flags & UNINTERESTING))
414 return NULL;
415 if (revs->exclude_promisor_objects &&
416 is_promisor_object(&tag->tagged->oid))
417 return NULL;
418 die("bad object %s", oid_to_hex(&tag->tagged->oid));
420 object->flags |= flags;
422 * We'll handle the tagged object by looping or dropping
423 * through to the non-tag handlers below. Do not
424 * propagate path data from the tag's pending entry.
426 path = NULL;
427 mode = 0;
431 * Commit object? Just return it, we'll do all the complex
432 * reachability crud.
434 if (object->type == OBJ_COMMIT) {
435 struct commit *commit = (struct commit *)object;
437 if (repo_parse_commit(revs->repo, commit) < 0)
438 die("unable to parse commit %s", name);
439 if (flags & UNINTERESTING) {
440 mark_parents_uninteresting(revs, commit);
442 if (!revs->topo_order || !generation_numbers_enabled(the_repository))
443 revs->limited = 1;
445 if (revs->sources) {
446 char **slot = revision_sources_at(revs->sources, commit);
448 if (!*slot)
449 *slot = xstrdup(name);
451 return commit;
455 * Tree object? Either mark it uninteresting, or add it
456 * to the list of objects to look at later..
458 if (object->type == OBJ_TREE) {
459 struct tree *tree = (struct tree *)object;
460 if (!revs->tree_objects)
461 return NULL;
462 if (flags & UNINTERESTING) {
463 mark_tree_contents_uninteresting(revs->repo, tree);
464 return NULL;
466 add_pending_object_with_path(revs, object, name, mode, path);
467 return NULL;
471 * Blob object? You know the drill by now..
473 if (object->type == OBJ_BLOB) {
474 if (!revs->blob_objects)
475 return NULL;
476 if (flags & UNINTERESTING)
477 return NULL;
478 add_pending_object_with_path(revs, object, name, mode, path);
479 return NULL;
481 die("%s is unknown object", name);
484 static int everybody_uninteresting(struct commit_list *orig,
485 struct commit **interesting_cache)
487 struct commit_list *list = orig;
489 if (*interesting_cache) {
490 struct commit *commit = *interesting_cache;
491 if (!(commit->object.flags & UNINTERESTING))
492 return 0;
495 while (list) {
496 struct commit *commit = list->item;
497 list = list->next;
498 if (commit->object.flags & UNINTERESTING)
499 continue;
501 *interesting_cache = commit;
502 return 0;
504 return 1;
508 * A definition of "relevant" commit that we can use to simplify limited graphs
509 * by eliminating side branches.
511 * A "relevant" commit is one that is !UNINTERESTING (ie we are including it
512 * in our list), or that is a specified BOTTOM commit. Then after computing
513 * a limited list, during processing we can generally ignore boundary merges
514 * coming from outside the graph, (ie from irrelevant parents), and treat
515 * those merges as if they were single-parent. TREESAME is defined to consider
516 * only relevant parents, if any. If we are TREESAME to our on-graph parents,
517 * we don't care if we were !TREESAME to non-graph parents.
519 * Treating bottom commits as relevant ensures that a limited graph's
520 * connection to the actual bottom commit is not viewed as a side branch, but
521 * treated as part of the graph. For example:
523 * ....Z...A---X---o---o---B
524 * . /
525 * W---Y
527 * When computing "A..B", the A-X connection is at least as important as
528 * Y-X, despite A being flagged UNINTERESTING.
530 * And when computing --ancestry-path "A..B", the A-X connection is more
531 * important than Y-X, despite both A and Y being flagged UNINTERESTING.
533 static inline int relevant_commit(struct commit *commit)
535 return (commit->object.flags & (UNINTERESTING | BOTTOM)) != UNINTERESTING;
539 * Return a single relevant commit from a parent list. If we are a TREESAME
540 * commit, and this selects one of our parents, then we can safely simplify to
541 * that parent.
543 static struct commit *one_relevant_parent(const struct rev_info *revs,
544 struct commit_list *orig)
546 struct commit_list *list = orig;
547 struct commit *relevant = NULL;
549 if (!orig)
550 return NULL;
553 * For 1-parent commits, or if first-parent-only, then return that
554 * first parent (even if not "relevant" by the above definition).
555 * TREESAME will have been set purely on that parent.
557 if (revs->first_parent_only || !orig->next)
558 return orig->item;
561 * For multi-parent commits, identify a sole relevant parent, if any.
562 * If we have only one relevant parent, then TREESAME will be set purely
563 * with regard to that parent, and we can simplify accordingly.
565 * If we have more than one relevant parent, or no relevant parents
566 * (and multiple irrelevant ones), then we can't select a parent here
567 * and return NULL.
569 while (list) {
570 struct commit *commit = list->item;
571 list = list->next;
572 if (relevant_commit(commit)) {
573 if (relevant)
574 return NULL;
575 relevant = commit;
578 return relevant;
582 * The goal is to get REV_TREE_NEW as the result only if the
583 * diff consists of all '+' (and no other changes), REV_TREE_OLD
584 * if the whole diff is removal of old data, and otherwise
585 * REV_TREE_DIFFERENT (of course if the trees are the same we
586 * want REV_TREE_SAME).
588 * The only time we care about the distinction is when
589 * remove_empty_trees is in effect, in which case we care only about
590 * whether the whole change is REV_TREE_NEW, or if there's another type
591 * of change. Which means we can stop the diff early in either of these
592 * cases:
594 * 1. We're not using remove_empty_trees at all.
596 * 2. We saw anything except REV_TREE_NEW.
598 #define REV_TREE_SAME 0
599 #define REV_TREE_NEW 1 /* Only new files */
600 #define REV_TREE_OLD 2 /* Only files removed */
601 #define REV_TREE_DIFFERENT 3 /* Mixed changes */
602 static int tree_difference = REV_TREE_SAME;
604 static void file_add_remove(struct diff_options *options,
605 int addremove,
606 unsigned mode UNUSED,
607 const struct object_id *oid UNUSED,
608 int oid_valid UNUSED,
609 const char *fullpath UNUSED,
610 unsigned dirty_submodule UNUSED)
612 int diff = addremove == '+' ? REV_TREE_NEW : REV_TREE_OLD;
613 struct rev_info *revs = options->change_fn_data;
615 tree_difference |= diff;
616 if (!revs->remove_empty_trees || tree_difference != REV_TREE_NEW)
617 options->flags.has_changes = 1;
620 static void file_change(struct diff_options *options,
621 unsigned old_mode UNUSED,
622 unsigned new_mode UNUSED,
623 const struct object_id *old_oid UNUSED,
624 const struct object_id *new_oid UNUSED,
625 int old_oid_valid UNUSED,
626 int new_oid_valid UNUSED,
627 const char *fullpath UNUSED,
628 unsigned old_dirty_submodule UNUSED,
629 unsigned new_dirty_submodule UNUSED)
631 tree_difference = REV_TREE_DIFFERENT;
632 options->flags.has_changes = 1;
635 static int bloom_filter_atexit_registered;
636 static unsigned int count_bloom_filter_maybe;
637 static unsigned int count_bloom_filter_definitely_not;
638 static unsigned int count_bloom_filter_false_positive;
639 static unsigned int count_bloom_filter_not_present;
641 static void trace2_bloom_filter_statistics_atexit(void)
643 struct json_writer jw = JSON_WRITER_INIT;
645 jw_object_begin(&jw, 0);
646 jw_object_intmax(&jw, "filter_not_present", count_bloom_filter_not_present);
647 jw_object_intmax(&jw, "maybe", count_bloom_filter_maybe);
648 jw_object_intmax(&jw, "definitely_not", count_bloom_filter_definitely_not);
649 jw_object_intmax(&jw, "false_positive", count_bloom_filter_false_positive);
650 jw_end(&jw);
652 trace2_data_json("bloom", the_repository, "statistics", &jw);
654 jw_release(&jw);
657 static int forbid_bloom_filters(struct pathspec *spec)
659 if (spec->has_wildcard)
660 return 1;
661 if (spec->nr > 1)
662 return 1;
663 if (spec->magic & ~PATHSPEC_LITERAL)
664 return 1;
665 if (spec->nr && (spec->items[0].magic & ~PATHSPEC_LITERAL))
666 return 1;
668 return 0;
671 static void prepare_to_use_bloom_filter(struct rev_info *revs)
673 struct pathspec_item *pi;
674 char *path_alloc = NULL;
675 const char *path, *p;
676 size_t len;
677 int path_component_nr = 1;
679 if (!revs->commits)
680 return;
682 if (forbid_bloom_filters(&revs->prune_data))
683 return;
685 repo_parse_commit(revs->repo, revs->commits->item);
687 revs->bloom_filter_settings = get_bloom_filter_settings(revs->repo);
688 if (!revs->bloom_filter_settings)
689 return;
691 if (!revs->pruning.pathspec.nr)
692 return;
694 pi = &revs->pruning.pathspec.items[0];
696 /* remove single trailing slash from path, if needed */
697 if (pi->len > 0 && pi->match[pi->len - 1] == '/') {
698 path_alloc = xmemdupz(pi->match, pi->len - 1);
699 path = path_alloc;
700 } else
701 path = pi->match;
703 len = strlen(path);
704 if (!len) {
705 revs->bloom_filter_settings = NULL;
706 free(path_alloc);
707 return;
710 p = path;
711 while (*p) {
713 * At this point, the path is normalized to use Unix-style
714 * path separators. This is required due to how the
715 * changed-path Bloom filters store the paths.
717 if (*p == '/')
718 path_component_nr++;
719 p++;
722 revs->bloom_keys_nr = path_component_nr;
723 ALLOC_ARRAY(revs->bloom_keys, revs->bloom_keys_nr);
725 fill_bloom_key(path, len, &revs->bloom_keys[0],
726 revs->bloom_filter_settings);
727 path_component_nr = 1;
729 p = path + len - 1;
730 while (p > path) {
731 if (*p == '/')
732 fill_bloom_key(path, p - path,
733 &revs->bloom_keys[path_component_nr++],
734 revs->bloom_filter_settings);
735 p--;
738 if (trace2_is_enabled() && !bloom_filter_atexit_registered) {
739 atexit(trace2_bloom_filter_statistics_atexit);
740 bloom_filter_atexit_registered = 1;
743 free(path_alloc);
746 static int check_maybe_different_in_bloom_filter(struct rev_info *revs,
747 struct commit *commit)
749 struct bloom_filter *filter;
750 int result = 1, j;
752 if (!revs->repo->objects->commit_graph)
753 return -1;
755 if (commit_graph_generation(commit) == GENERATION_NUMBER_INFINITY)
756 return -1;
758 filter = get_bloom_filter(revs->repo, commit);
760 if (!filter) {
761 count_bloom_filter_not_present++;
762 return -1;
765 for (j = 0; result && j < revs->bloom_keys_nr; j++) {
766 result = bloom_filter_contains(filter,
767 &revs->bloom_keys[j],
768 revs->bloom_filter_settings);
771 if (result)
772 count_bloom_filter_maybe++;
773 else
774 count_bloom_filter_definitely_not++;
776 return result;
779 static int rev_compare_tree(struct rev_info *revs,
780 struct commit *parent, struct commit *commit, int nth_parent)
782 struct tree *t1 = get_commit_tree(parent);
783 struct tree *t2 = get_commit_tree(commit);
784 int bloom_ret = 1;
786 if (!t1)
787 return REV_TREE_NEW;
788 if (!t2)
789 return REV_TREE_OLD;
791 if (revs->simplify_by_decoration) {
793 * If we are simplifying by decoration, then the commit
794 * is worth showing if it has a tag pointing at it.
796 if (get_name_decoration(&commit->object))
797 return REV_TREE_DIFFERENT;
799 * A commit that is not pointed by a tag is uninteresting
800 * if we are not limited by path. This means that you will
801 * see the usual "commits that touch the paths" plus any
802 * tagged commit by specifying both --simplify-by-decoration
803 * and pathspec.
805 if (!revs->prune_data.nr)
806 return REV_TREE_SAME;
809 if (revs->bloom_keys_nr && !nth_parent) {
810 bloom_ret = check_maybe_different_in_bloom_filter(revs, commit);
812 if (bloom_ret == 0)
813 return REV_TREE_SAME;
816 tree_difference = REV_TREE_SAME;
817 revs->pruning.flags.has_changes = 0;
818 diff_tree_oid(&t1->object.oid, &t2->object.oid, "", &revs->pruning);
820 if (!nth_parent)
821 if (bloom_ret == 1 && tree_difference == REV_TREE_SAME)
822 count_bloom_filter_false_positive++;
824 return tree_difference;
827 static int rev_same_tree_as_empty(struct rev_info *revs, struct commit *commit)
829 struct tree *t1 = get_commit_tree(commit);
831 if (!t1)
832 return 0;
834 tree_difference = REV_TREE_SAME;
835 revs->pruning.flags.has_changes = 0;
836 diff_tree_oid(NULL, &t1->object.oid, "", &revs->pruning);
838 return tree_difference == REV_TREE_SAME;
841 struct treesame_state {
842 unsigned int nparents;
843 unsigned char treesame[FLEX_ARRAY];
846 static struct treesame_state *initialise_treesame(struct rev_info *revs, struct commit *commit)
848 unsigned n = commit_list_count(commit->parents);
849 struct treesame_state *st = xcalloc(1, st_add(sizeof(*st), n));
850 st->nparents = n;
851 add_decoration(&revs->treesame, &commit->object, st);
852 return st;
856 * Must be called immediately after removing the nth_parent from a commit's
857 * parent list, if we are maintaining the per-parent treesame[] decoration.
858 * This does not recalculate the master TREESAME flag - update_treesame()
859 * should be called to update it after a sequence of treesame[] modifications
860 * that may have affected it.
862 static int compact_treesame(struct rev_info *revs, struct commit *commit, unsigned nth_parent)
864 struct treesame_state *st;
865 int old_same;
867 if (!commit->parents) {
869 * Have just removed the only parent from a non-merge.
870 * Different handling, as we lack decoration.
872 if (nth_parent != 0)
873 die("compact_treesame %u", nth_parent);
874 old_same = !!(commit->object.flags & TREESAME);
875 if (rev_same_tree_as_empty(revs, commit))
876 commit->object.flags |= TREESAME;
877 else
878 commit->object.flags &= ~TREESAME;
879 return old_same;
882 st = lookup_decoration(&revs->treesame, &commit->object);
883 if (!st || nth_parent >= st->nparents)
884 die("compact_treesame %u", nth_parent);
886 old_same = st->treesame[nth_parent];
887 memmove(st->treesame + nth_parent,
888 st->treesame + nth_parent + 1,
889 st->nparents - nth_parent - 1);
892 * If we've just become a non-merge commit, update TREESAME
893 * immediately, and remove the no-longer-needed decoration.
894 * If still a merge, defer update until update_treesame().
896 if (--st->nparents == 1) {
897 if (commit->parents->next)
898 die("compact_treesame parents mismatch");
899 if (st->treesame[0] && revs->dense)
900 commit->object.flags |= TREESAME;
901 else
902 commit->object.flags &= ~TREESAME;
903 free(add_decoration(&revs->treesame, &commit->object, NULL));
906 return old_same;
909 static unsigned update_treesame(struct rev_info *revs, struct commit *commit)
911 if (commit->parents && commit->parents->next) {
912 unsigned n;
913 struct treesame_state *st;
914 struct commit_list *p;
915 unsigned relevant_parents;
916 unsigned relevant_change, irrelevant_change;
918 st = lookup_decoration(&revs->treesame, &commit->object);
919 if (!st)
920 die("update_treesame %s", oid_to_hex(&commit->object.oid));
921 relevant_parents = 0;
922 relevant_change = irrelevant_change = 0;
923 for (p = commit->parents, n = 0; p; n++, p = p->next) {
924 if (relevant_commit(p->item)) {
925 relevant_change |= !st->treesame[n];
926 relevant_parents++;
927 } else
928 irrelevant_change |= !st->treesame[n];
930 if (relevant_parents ? relevant_change : irrelevant_change)
931 commit->object.flags &= ~TREESAME;
932 else
933 commit->object.flags |= TREESAME;
936 return commit->object.flags & TREESAME;
939 static inline int limiting_can_increase_treesame(const struct rev_info *revs)
942 * TREESAME is irrelevant unless prune && dense;
943 * if simplify_history is set, we can't have a mixture of TREESAME and
944 * !TREESAME INTERESTING parents (and we don't have treesame[]
945 * decoration anyway);
946 * if first_parent_only is set, then the TREESAME flag is locked
947 * against the first parent (and again we lack treesame[] decoration).
949 return revs->prune && revs->dense &&
950 !revs->simplify_history &&
951 !revs->first_parent_only;
954 static void try_to_simplify_commit(struct rev_info *revs, struct commit *commit)
956 struct commit_list **pp, *parent;
957 struct treesame_state *ts = NULL;
958 int relevant_change = 0, irrelevant_change = 0;
959 int relevant_parents, nth_parent;
962 * If we don't do pruning, everything is interesting
964 if (!revs->prune)
965 return;
967 if (!get_commit_tree(commit))
968 return;
970 if (!commit->parents) {
971 if (rev_same_tree_as_empty(revs, commit))
972 commit->object.flags |= TREESAME;
973 return;
977 * Normal non-merge commit? If we don't want to make the
978 * history dense, we consider it always to be a change..
980 if (!revs->dense && !commit->parents->next)
981 return;
983 for (pp = &commit->parents, nth_parent = 0, relevant_parents = 0;
984 (parent = *pp) != NULL;
985 pp = &parent->next, nth_parent++) {
986 struct commit *p = parent->item;
987 if (relevant_commit(p))
988 relevant_parents++;
990 if (nth_parent == 1) {
992 * This our second loop iteration - so we now know
993 * we're dealing with a merge.
995 * Do not compare with later parents when we care only about
996 * the first parent chain, in order to avoid derailing the
997 * traversal to follow a side branch that brought everything
998 * in the path we are limited to by the pathspec.
1000 if (revs->first_parent_only)
1001 break;
1003 * If this will remain a potentially-simplifiable
1004 * merge, remember per-parent treesame if needed.
1005 * Initialise the array with the comparison from our
1006 * first iteration.
1008 if (revs->treesame.name &&
1009 !revs->simplify_history &&
1010 !(commit->object.flags & UNINTERESTING)) {
1011 ts = initialise_treesame(revs, commit);
1012 if (!(irrelevant_change || relevant_change))
1013 ts->treesame[0] = 1;
1016 if (repo_parse_commit(revs->repo, p) < 0)
1017 die("cannot simplify commit %s (because of %s)",
1018 oid_to_hex(&commit->object.oid),
1019 oid_to_hex(&p->object.oid));
1020 switch (rev_compare_tree(revs, p, commit, nth_parent)) {
1021 case REV_TREE_SAME:
1022 if (!revs->simplify_history || !relevant_commit(p)) {
1023 /* Even if a merge with an uninteresting
1024 * side branch brought the entire change
1025 * we are interested in, we do not want
1026 * to lose the other branches of this
1027 * merge, so we just keep going.
1029 if (ts)
1030 ts->treesame[nth_parent] = 1;
1031 continue;
1033 parent->next = NULL;
1034 commit->parents = parent;
1037 * A merge commit is a "diversion" if it is not
1038 * TREESAME to its first parent but is TREESAME
1039 * to a later parent. In the simplified history,
1040 * we "divert" the history walk to the later
1041 * parent. These commits are shown when "show_pulls"
1042 * is enabled, so do not mark the object as
1043 * TREESAME here.
1045 if (!revs->show_pulls || !nth_parent)
1046 commit->object.flags |= TREESAME;
1048 return;
1050 case REV_TREE_NEW:
1051 if (revs->remove_empty_trees &&
1052 rev_same_tree_as_empty(revs, p)) {
1053 /* We are adding all the specified
1054 * paths from this parent, so the
1055 * history beyond this parent is not
1056 * interesting. Remove its parents
1057 * (they are grandparents for us).
1058 * IOW, we pretend this parent is a
1059 * "root" commit.
1061 if (repo_parse_commit(revs->repo, p) < 0)
1062 die("cannot simplify commit %s (invalid %s)",
1063 oid_to_hex(&commit->object.oid),
1064 oid_to_hex(&p->object.oid));
1065 p->parents = NULL;
1067 /* fallthrough */
1068 case REV_TREE_OLD:
1069 case REV_TREE_DIFFERENT:
1070 if (relevant_commit(p))
1071 relevant_change = 1;
1072 else
1073 irrelevant_change = 1;
1075 if (!nth_parent)
1076 commit->object.flags |= PULL_MERGE;
1078 continue;
1080 die("bad tree compare for commit %s", oid_to_hex(&commit->object.oid));
1084 * TREESAME is straightforward for single-parent commits. For merge
1085 * commits, it is most useful to define it so that "irrelevant"
1086 * parents cannot make us !TREESAME - if we have any relevant
1087 * parents, then we only consider TREESAMEness with respect to them,
1088 * allowing irrelevant merges from uninteresting branches to be
1089 * simplified away. Only if we have only irrelevant parents do we
1090 * base TREESAME on them. Note that this logic is replicated in
1091 * update_treesame, which should be kept in sync.
1093 if (relevant_parents ? !relevant_change : !irrelevant_change)
1094 commit->object.flags |= TREESAME;
1097 static int process_parents(struct rev_info *revs, struct commit *commit,
1098 struct commit_list **list, struct prio_queue *queue)
1100 struct commit_list *parent = commit->parents;
1101 unsigned pass_flags;
1103 if (commit->object.flags & ADDED)
1104 return 0;
1105 commit->object.flags |= ADDED;
1107 if (revs->include_check &&
1108 !revs->include_check(commit, revs->include_check_data))
1109 return 0;
1112 * If the commit is uninteresting, don't try to
1113 * prune parents - we want the maximal uninteresting
1114 * set.
1116 * Normally we haven't parsed the parent
1117 * yet, so we won't have a parent of a parent
1118 * here. However, it may turn out that we've
1119 * reached this commit some other way (where it
1120 * wasn't uninteresting), in which case we need
1121 * to mark its parents recursively too..
1123 if (commit->object.flags & UNINTERESTING) {
1124 while (parent) {
1125 struct commit *p = parent->item;
1126 parent = parent->next;
1127 if (p)
1128 p->object.flags |= UNINTERESTING;
1129 if (repo_parse_commit_gently(revs->repo, p, 1) < 0)
1130 continue;
1131 if (p->parents)
1132 mark_parents_uninteresting(revs, p);
1133 if (p->object.flags & SEEN)
1134 continue;
1135 p->object.flags |= (SEEN | NOT_USER_GIVEN);
1136 if (list)
1137 commit_list_insert_by_date(p, list);
1138 if (queue)
1139 prio_queue_put(queue, p);
1140 if (revs->exclude_first_parent_only)
1141 break;
1143 return 0;
1147 * Ok, the commit wasn't uninteresting. Try to
1148 * simplify the commit history and find the parent
1149 * that has no differences in the path set if one exists.
1151 try_to_simplify_commit(revs, commit);
1153 if (revs->no_walk)
1154 return 0;
1156 pass_flags = (commit->object.flags & (SYMMETRIC_LEFT | ANCESTRY_PATH));
1158 for (parent = commit->parents; parent; parent = parent->next) {
1159 struct commit *p = parent->item;
1160 int gently = revs->ignore_missing_links ||
1161 revs->exclude_promisor_objects;
1162 if (repo_parse_commit_gently(revs->repo, p, gently) < 0) {
1163 if (revs->exclude_promisor_objects &&
1164 is_promisor_object(&p->object.oid)) {
1165 if (revs->first_parent_only)
1166 break;
1167 continue;
1169 return -1;
1171 if (revs->sources) {
1172 char **slot = revision_sources_at(revs->sources, p);
1174 if (!*slot)
1175 *slot = *revision_sources_at(revs->sources, commit);
1177 p->object.flags |= pass_flags;
1178 if (!(p->object.flags & SEEN)) {
1179 p->object.flags |= (SEEN | NOT_USER_GIVEN);
1180 if (list)
1181 commit_list_insert_by_date(p, list);
1182 if (queue)
1183 prio_queue_put(queue, p);
1185 if (revs->first_parent_only)
1186 break;
1188 return 0;
1191 static void cherry_pick_list(struct commit_list *list, struct rev_info *revs)
1193 struct commit_list *p;
1194 int left_count = 0, right_count = 0;
1195 int left_first;
1196 struct patch_ids ids;
1197 unsigned cherry_flag;
1199 /* First count the commits on the left and on the right */
1200 for (p = list; p; p = p->next) {
1201 struct commit *commit = p->item;
1202 unsigned flags = commit->object.flags;
1203 if (flags & BOUNDARY)
1205 else if (flags & SYMMETRIC_LEFT)
1206 left_count++;
1207 else
1208 right_count++;
1211 if (!left_count || !right_count)
1212 return;
1214 left_first = left_count < right_count;
1215 init_patch_ids(revs->repo, &ids);
1216 ids.diffopts.pathspec = revs->diffopt.pathspec;
1218 /* Compute patch-ids for one side */
1219 for (p = list; p; p = p->next) {
1220 struct commit *commit = p->item;
1221 unsigned flags = commit->object.flags;
1223 if (flags & BOUNDARY)
1224 continue;
1226 * If we have fewer left, left_first is set and we omit
1227 * commits on the right branch in this loop. If we have
1228 * fewer right, we skip the left ones.
1230 if (left_first != !!(flags & SYMMETRIC_LEFT))
1231 continue;
1232 add_commit_patch_id(commit, &ids);
1235 /* either cherry_mark or cherry_pick are true */
1236 cherry_flag = revs->cherry_mark ? PATCHSAME : SHOWN;
1238 /* Check the other side */
1239 for (p = list; p; p = p->next) {
1240 struct commit *commit = p->item;
1241 struct patch_id *id;
1242 unsigned flags = commit->object.flags;
1244 if (flags & BOUNDARY)
1245 continue;
1247 * If we have fewer left, left_first is set and we omit
1248 * commits on the left branch in this loop.
1250 if (left_first == !!(flags & SYMMETRIC_LEFT))
1251 continue;
1254 * Have we seen the same patch id?
1256 id = patch_id_iter_first(commit, &ids);
1257 if (!id)
1258 continue;
1260 commit->object.flags |= cherry_flag;
1261 do {
1262 id->commit->object.flags |= cherry_flag;
1263 } while ((id = patch_id_iter_next(id, &ids)));
1266 free_patch_ids(&ids);
1269 /* How many extra uninteresting commits we want to see.. */
1270 #define SLOP 5
1272 static int still_interesting(struct commit_list *src, timestamp_t date, int slop,
1273 struct commit **interesting_cache)
1276 * No source list at all? We're definitely done..
1278 if (!src)
1279 return 0;
1282 * Does the destination list contain entries with a date
1283 * before the source list? Definitely _not_ done.
1285 if (date <= src->item->date)
1286 return SLOP;
1289 * Does the source list still have interesting commits in
1290 * it? Definitely not done..
1292 if (!everybody_uninteresting(src, interesting_cache))
1293 return SLOP;
1295 /* Ok, we're closing in.. */
1296 return slop-1;
1300 * "rev-list --ancestry-path=C_0 [--ancestry-path=C_1 ...] A..B"
1301 * computes commits that are ancestors of B but not ancestors of A but
1302 * further limits the result to those that have any of C in their
1303 * ancestry path (i.e. are either ancestors of any of C, descendants
1304 * of any of C, or are any of C). If --ancestry-path is specified with
1305 * no commit, we use all bottom commits for C.
1307 * Before this function is called, ancestors of C will have already
1308 * been marked with ANCESTRY_PATH previously.
1310 * This takes the list of bottom commits and the result of "A..B"
1311 * without --ancestry-path, and limits the latter further to the ones
1312 * that have any of C in their ancestry path. Since the ancestors of C
1313 * have already been marked (a prerequisite of this function), we just
1314 * need to mark the descendants, then exclude any commit that does not
1315 * have any of these marks.
1317 static void limit_to_ancestry(struct commit_list *bottoms, struct commit_list *list)
1319 struct commit_list *p;
1320 struct commit_list *rlist = NULL;
1321 int made_progress;
1324 * Reverse the list so that it will be likely that we would
1325 * process parents before children.
1327 for (p = list; p; p = p->next)
1328 commit_list_insert(p->item, &rlist);
1330 for (p = bottoms; p; p = p->next)
1331 p->item->object.flags |= TMP_MARK;
1334 * Mark the ones that can reach bottom commits in "list",
1335 * in a bottom-up fashion.
1337 do {
1338 made_progress = 0;
1339 for (p = rlist; p; p = p->next) {
1340 struct commit *c = p->item;
1341 struct commit_list *parents;
1342 if (c->object.flags & (TMP_MARK | UNINTERESTING))
1343 continue;
1344 for (parents = c->parents;
1345 parents;
1346 parents = parents->next) {
1347 if (!(parents->item->object.flags & TMP_MARK))
1348 continue;
1349 c->object.flags |= TMP_MARK;
1350 made_progress = 1;
1351 break;
1354 } while (made_progress);
1357 * NEEDSWORK: decide if we want to remove parents that are
1358 * not marked with TMP_MARK from commit->parents for commits
1359 * in the resulting list. We may not want to do that, though.
1363 * The ones that are not marked with either TMP_MARK or
1364 * ANCESTRY_PATH are uninteresting
1366 for (p = list; p; p = p->next) {
1367 struct commit *c = p->item;
1368 if (c->object.flags & (TMP_MARK | ANCESTRY_PATH))
1369 continue;
1370 c->object.flags |= UNINTERESTING;
1373 /* We are done with TMP_MARK and ANCESTRY_PATH */
1374 for (p = list; p; p = p->next)
1375 p->item->object.flags &= ~(TMP_MARK | ANCESTRY_PATH);
1376 for (p = bottoms; p; p = p->next)
1377 p->item->object.flags &= ~(TMP_MARK | ANCESTRY_PATH);
1378 free_commit_list(rlist);
1382 * Before walking the history, add the set of "negative" refs the
1383 * caller has asked to exclude to the bottom list.
1385 * This is used to compute "rev-list --ancestry-path A..B", as we need
1386 * to filter the result of "A..B" further to the ones that can actually
1387 * reach A.
1389 static void collect_bottom_commits(struct commit_list *list,
1390 struct commit_list **bottom)
1392 struct commit_list *elem;
1393 for (elem = list; elem; elem = elem->next)
1394 if (elem->item->object.flags & BOTTOM)
1395 commit_list_insert(elem->item, bottom);
1398 /* Assumes either left_only or right_only is set */
1399 static void limit_left_right(struct commit_list *list, struct rev_info *revs)
1401 struct commit_list *p;
1403 for (p = list; p; p = p->next) {
1404 struct commit *commit = p->item;
1406 if (revs->right_only) {
1407 if (commit->object.flags & SYMMETRIC_LEFT)
1408 commit->object.flags |= SHOWN;
1409 } else /* revs->left_only is set */
1410 if (!(commit->object.flags & SYMMETRIC_LEFT))
1411 commit->object.flags |= SHOWN;
1415 static int limit_list(struct rev_info *revs)
1417 int slop = SLOP;
1418 timestamp_t date = TIME_MAX;
1419 struct commit_list *original_list = revs->commits;
1420 struct commit_list *newlist = NULL;
1421 struct commit_list **p = &newlist;
1422 struct commit *interesting_cache = NULL;
1424 if (revs->ancestry_path_implicit_bottoms) {
1425 collect_bottom_commits(original_list,
1426 &revs->ancestry_path_bottoms);
1427 if (!revs->ancestry_path_bottoms)
1428 die("--ancestry-path given but there are no bottom commits");
1431 while (original_list) {
1432 struct commit *commit = pop_commit(&original_list);
1433 struct object *obj = &commit->object;
1434 show_early_output_fn_t show;
1436 if (commit == interesting_cache)
1437 interesting_cache = NULL;
1439 if (revs->max_age != -1 && (commit->date < revs->max_age))
1440 obj->flags |= UNINTERESTING;
1441 if (process_parents(revs, commit, &original_list, NULL) < 0)
1442 return -1;
1443 if (obj->flags & UNINTERESTING) {
1444 mark_parents_uninteresting(revs, commit);
1445 slop = still_interesting(original_list, date, slop, &interesting_cache);
1446 if (slop)
1447 continue;
1448 break;
1450 if (revs->min_age != -1 && (commit->date > revs->min_age) &&
1451 !revs->line_level_traverse)
1452 continue;
1453 if (revs->max_age_as_filter != -1 &&
1454 (commit->date < revs->max_age_as_filter) && !revs->line_level_traverse)
1455 continue;
1456 date = commit->date;
1457 p = &commit_list_insert(commit, p)->next;
1459 show = show_early_output;
1460 if (!show)
1461 continue;
1463 show(revs, newlist);
1464 show_early_output = NULL;
1466 if (revs->cherry_pick || revs->cherry_mark)
1467 cherry_pick_list(newlist, revs);
1469 if (revs->left_only || revs->right_only)
1470 limit_left_right(newlist, revs);
1472 if (revs->ancestry_path)
1473 limit_to_ancestry(revs->ancestry_path_bottoms, newlist);
1476 * Check if any commits have become TREESAME by some of their parents
1477 * becoming UNINTERESTING.
1479 if (limiting_can_increase_treesame(revs)) {
1480 struct commit_list *list = NULL;
1481 for (list = newlist; list; list = list->next) {
1482 struct commit *c = list->item;
1483 if (c->object.flags & (UNINTERESTING | TREESAME))
1484 continue;
1485 update_treesame(revs, c);
1489 free_commit_list(original_list);
1490 revs->commits = newlist;
1491 return 0;
1495 * Add an entry to refs->cmdline with the specified information.
1496 * *name is copied.
1498 static void add_rev_cmdline(struct rev_info *revs,
1499 struct object *item,
1500 const char *name,
1501 int whence,
1502 unsigned flags)
1504 struct rev_cmdline_info *info = &revs->cmdline;
1505 unsigned int nr = info->nr;
1507 ALLOC_GROW(info->rev, nr + 1, info->alloc);
1508 info->rev[nr].item = item;
1509 info->rev[nr].name = xstrdup(name);
1510 info->rev[nr].whence = whence;
1511 info->rev[nr].flags = flags;
1512 info->nr++;
1515 static void add_rev_cmdline_list(struct rev_info *revs,
1516 struct commit_list *commit_list,
1517 int whence,
1518 unsigned flags)
1520 while (commit_list) {
1521 struct object *object = &commit_list->item->object;
1522 add_rev_cmdline(revs, object, oid_to_hex(&object->oid),
1523 whence, flags);
1524 commit_list = commit_list->next;
1528 int ref_excluded(const struct ref_exclusions *exclusions, const char *path)
1530 const char *stripped_path = strip_namespace(path);
1531 struct string_list_item *item;
1533 for_each_string_list_item(item, &exclusions->excluded_refs) {
1534 if (!wildmatch(item->string, path, 0))
1535 return 1;
1538 if (ref_is_hidden(stripped_path, path, &exclusions->hidden_refs))
1539 return 1;
1541 return 0;
1544 void init_ref_exclusions(struct ref_exclusions *exclusions)
1546 struct ref_exclusions blank = REF_EXCLUSIONS_INIT;
1547 memcpy(exclusions, &blank, sizeof(*exclusions));
1550 void clear_ref_exclusions(struct ref_exclusions *exclusions)
1552 string_list_clear(&exclusions->excluded_refs, 0);
1553 string_list_clear(&exclusions->hidden_refs, 0);
1554 exclusions->hidden_refs_configured = 0;
1557 void add_ref_exclusion(struct ref_exclusions *exclusions, const char *exclude)
1559 string_list_append(&exclusions->excluded_refs, exclude);
1562 struct exclude_hidden_refs_cb {
1563 struct ref_exclusions *exclusions;
1564 const char *section;
1567 static int hide_refs_config(const char *var, const char *value, void *cb_data)
1569 struct exclude_hidden_refs_cb *cb = cb_data;
1570 cb->exclusions->hidden_refs_configured = 1;
1571 return parse_hide_refs_config(var, value, cb->section,
1572 &cb->exclusions->hidden_refs);
1575 void exclude_hidden_refs(struct ref_exclusions *exclusions, const char *section)
1577 struct exclude_hidden_refs_cb cb;
1579 if (strcmp(section, "receive") && strcmp(section, "uploadpack"))
1580 die(_("unsupported section for hidden refs: %s"), section);
1582 if (exclusions->hidden_refs_configured)
1583 die(_("--exclude-hidden= passed more than once"));
1585 cb.exclusions = exclusions;
1586 cb.section = section;
1588 git_config(hide_refs_config, &cb);
1591 struct all_refs_cb {
1592 int all_flags;
1593 int warned_bad_reflog;
1594 struct rev_info *all_revs;
1595 const char *name_for_errormsg;
1596 struct worktree *wt;
1599 static int handle_one_ref(const char *path, const struct object_id *oid,
1600 int flag UNUSED,
1601 void *cb_data)
1603 struct all_refs_cb *cb = cb_data;
1604 struct object *object;
1606 if (ref_excluded(&cb->all_revs->ref_excludes, path))
1607 return 0;
1609 object = get_reference(cb->all_revs, path, oid, cb->all_flags);
1610 add_rev_cmdline(cb->all_revs, object, path, REV_CMD_REF, cb->all_flags);
1611 add_pending_object(cb->all_revs, object, path);
1612 return 0;
1615 static void init_all_refs_cb(struct all_refs_cb *cb, struct rev_info *revs,
1616 unsigned flags)
1618 cb->all_revs = revs;
1619 cb->all_flags = flags;
1620 revs->rev_input_given = 1;
1621 cb->wt = NULL;
1624 static void handle_refs(struct ref_store *refs,
1625 struct rev_info *revs, unsigned flags,
1626 int (*for_each)(struct ref_store *, each_ref_fn, void *))
1628 struct all_refs_cb cb;
1630 if (!refs) {
1631 /* this could happen with uninitialized submodules */
1632 return;
1635 init_all_refs_cb(&cb, revs, flags);
1636 for_each(refs, handle_one_ref, &cb);
1639 static void handle_one_reflog_commit(struct object_id *oid, void *cb_data)
1641 struct all_refs_cb *cb = cb_data;
1642 if (!is_null_oid(oid)) {
1643 struct object *o = parse_object(cb->all_revs->repo, oid);
1644 if (o) {
1645 o->flags |= cb->all_flags;
1646 /* ??? CMDLINEFLAGS ??? */
1647 add_pending_object(cb->all_revs, o, "");
1649 else if (!cb->warned_bad_reflog) {
1650 warning("reflog of '%s' references pruned commits",
1651 cb->name_for_errormsg);
1652 cb->warned_bad_reflog = 1;
1657 static int handle_one_reflog_ent(struct object_id *ooid, struct object_id *noid,
1658 const char *email UNUSED,
1659 timestamp_t timestamp UNUSED,
1660 int tz UNUSED,
1661 const char *message UNUSED,
1662 void *cb_data)
1664 handle_one_reflog_commit(ooid, cb_data);
1665 handle_one_reflog_commit(noid, cb_data);
1666 return 0;
1669 static int handle_one_reflog(const char *refname_in_wt,
1670 const struct object_id *oid UNUSED,
1671 int flag UNUSED, void *cb_data)
1673 struct all_refs_cb *cb = cb_data;
1674 struct strbuf refname = STRBUF_INIT;
1676 cb->warned_bad_reflog = 0;
1677 strbuf_worktree_ref(cb->wt, &refname, refname_in_wt);
1678 cb->name_for_errormsg = refname.buf;
1679 refs_for_each_reflog_ent(get_main_ref_store(the_repository),
1680 refname.buf,
1681 handle_one_reflog_ent, cb_data);
1682 strbuf_release(&refname);
1683 return 0;
1686 static void add_other_reflogs_to_pending(struct all_refs_cb *cb)
1688 struct worktree **worktrees, **p;
1690 worktrees = get_worktrees();
1691 for (p = worktrees; *p; p++) {
1692 struct worktree *wt = *p;
1694 if (wt->is_current)
1695 continue;
1697 cb->wt = wt;
1698 refs_for_each_reflog(get_worktree_ref_store(wt),
1699 handle_one_reflog,
1700 cb);
1702 free_worktrees(worktrees);
1705 void add_reflogs_to_pending(struct rev_info *revs, unsigned flags)
1707 struct all_refs_cb cb;
1709 cb.all_revs = revs;
1710 cb.all_flags = flags;
1711 cb.wt = NULL;
1712 for_each_reflog(handle_one_reflog, &cb);
1714 if (!revs->single_worktree)
1715 add_other_reflogs_to_pending(&cb);
1718 static void add_cache_tree(struct cache_tree *it, struct rev_info *revs,
1719 struct strbuf *path, unsigned int flags)
1721 size_t baselen = path->len;
1722 int i;
1724 if (it->entry_count >= 0) {
1725 struct tree *tree = lookup_tree(revs->repo, &it->oid);
1726 tree->object.flags |= flags;
1727 add_pending_object_with_path(revs, &tree->object, "",
1728 040000, path->buf);
1731 for (i = 0; i < it->subtree_nr; i++) {
1732 struct cache_tree_sub *sub = it->down[i];
1733 strbuf_addf(path, "%s%s", baselen ? "/" : "", sub->name);
1734 add_cache_tree(sub->cache_tree, revs, path, flags);
1735 strbuf_setlen(path, baselen);
1740 static void add_resolve_undo_to_pending(struct index_state *istate, struct rev_info *revs)
1742 struct string_list_item *item;
1743 struct string_list *resolve_undo = istate->resolve_undo;
1745 if (!resolve_undo)
1746 return;
1748 for_each_string_list_item(item, resolve_undo) {
1749 const char *path = item->string;
1750 struct resolve_undo_info *ru = item->util;
1751 int i;
1753 if (!ru)
1754 continue;
1755 for (i = 0; i < 3; i++) {
1756 struct blob *blob;
1758 if (!ru->mode[i] || !S_ISREG(ru->mode[i]))
1759 continue;
1761 blob = lookup_blob(revs->repo, &ru->oid[i]);
1762 if (!blob) {
1763 warning(_("resolve-undo records `%s` which is missing"),
1764 oid_to_hex(&ru->oid[i]));
1765 continue;
1767 add_pending_object_with_path(revs, &blob->object, "",
1768 ru->mode[i], path);
1773 static void do_add_index_objects_to_pending(struct rev_info *revs,
1774 struct index_state *istate,
1775 unsigned int flags)
1777 int i;
1779 /* TODO: audit for interaction with sparse-index. */
1780 ensure_full_index(istate);
1781 for (i = 0; i < istate->cache_nr; i++) {
1782 struct cache_entry *ce = istate->cache[i];
1783 struct blob *blob;
1785 if (S_ISGITLINK(ce->ce_mode))
1786 continue;
1788 blob = lookup_blob(revs->repo, &ce->oid);
1789 if (!blob)
1790 die("unable to add index blob to traversal");
1791 blob->object.flags |= flags;
1792 add_pending_object_with_path(revs, &blob->object, "",
1793 ce->ce_mode, ce->name);
1796 if (istate->cache_tree) {
1797 struct strbuf path = STRBUF_INIT;
1798 add_cache_tree(istate->cache_tree, revs, &path, flags);
1799 strbuf_release(&path);
1802 add_resolve_undo_to_pending(istate, revs);
1805 void add_index_objects_to_pending(struct rev_info *revs, unsigned int flags)
1807 struct worktree **worktrees, **p;
1809 repo_read_index(revs->repo);
1810 do_add_index_objects_to_pending(revs, revs->repo->index, flags);
1812 if (revs->single_worktree)
1813 return;
1815 worktrees = get_worktrees();
1816 for (p = worktrees; *p; p++) {
1817 struct worktree *wt = *p;
1818 struct index_state istate = INDEX_STATE_INIT(revs->repo);
1820 if (wt->is_current)
1821 continue; /* current index already taken care of */
1823 if (read_index_from(&istate,
1824 worktree_git_path(wt, "index"),
1825 get_worktree_git_dir(wt)) > 0)
1826 do_add_index_objects_to_pending(revs, &istate, flags);
1827 discard_index(&istate);
1829 free_worktrees(worktrees);
1832 struct add_alternate_refs_data {
1833 struct rev_info *revs;
1834 unsigned int flags;
1837 static void add_one_alternate_ref(const struct object_id *oid,
1838 void *vdata)
1840 const char *name = ".alternate";
1841 struct add_alternate_refs_data *data = vdata;
1842 struct object *obj;
1844 obj = get_reference(data->revs, name, oid, data->flags);
1845 add_rev_cmdline(data->revs, obj, name, REV_CMD_REV, data->flags);
1846 add_pending_object(data->revs, obj, name);
1849 static void add_alternate_refs_to_pending(struct rev_info *revs,
1850 unsigned int flags)
1852 struct add_alternate_refs_data data;
1853 data.revs = revs;
1854 data.flags = flags;
1855 for_each_alternate_ref(add_one_alternate_ref, &data);
1858 static int add_parents_only(struct rev_info *revs, const char *arg_, int flags,
1859 int exclude_parent)
1861 struct object_id oid;
1862 struct object *it;
1863 struct commit *commit;
1864 struct commit_list *parents;
1865 int parent_number;
1866 const char *arg = arg_;
1868 if (*arg == '^') {
1869 flags ^= UNINTERESTING | BOTTOM;
1870 arg++;
1872 if (get_oid_committish(arg, &oid))
1873 return 0;
1874 while (1) {
1875 it = get_reference(revs, arg, &oid, 0);
1876 if (!it && revs->ignore_missing)
1877 return 0;
1878 if (it->type != OBJ_TAG)
1879 break;
1880 if (!((struct tag*)it)->tagged)
1881 return 0;
1882 oidcpy(&oid, &((struct tag*)it)->tagged->oid);
1884 if (it->type != OBJ_COMMIT)
1885 return 0;
1886 commit = (struct commit *)it;
1887 if (exclude_parent &&
1888 exclude_parent > commit_list_count(commit->parents))
1889 return 0;
1890 for (parents = commit->parents, parent_number = 1;
1891 parents;
1892 parents = parents->next, parent_number++) {
1893 if (exclude_parent && parent_number != exclude_parent)
1894 continue;
1896 it = &parents->item->object;
1897 it->flags |= flags;
1898 add_rev_cmdline(revs, it, arg_, REV_CMD_PARENTS_ONLY, flags);
1899 add_pending_object(revs, it, arg);
1901 return 1;
1904 void repo_init_revisions(struct repository *r,
1905 struct rev_info *revs,
1906 const char *prefix)
1908 struct rev_info blank = REV_INFO_INIT;
1909 memcpy(revs, &blank, sizeof(*revs));
1911 revs->repo = r;
1912 revs->pruning.repo = r;
1913 revs->pruning.add_remove = file_add_remove;
1914 revs->pruning.change = file_change;
1915 revs->pruning.change_fn_data = revs;
1916 revs->prefix = prefix;
1918 grep_init(&revs->grep_filter, revs->repo);
1919 revs->grep_filter.status_only = 1;
1921 repo_diff_setup(revs->repo, &revs->diffopt);
1922 if (prefix && !revs->diffopt.prefix) {
1923 revs->diffopt.prefix = prefix;
1924 revs->diffopt.prefix_length = strlen(prefix);
1927 init_display_notes(&revs->notes_opt);
1928 list_objects_filter_init(&revs->filter);
1929 init_ref_exclusions(&revs->ref_excludes);
1932 static void add_pending_commit_list(struct rev_info *revs,
1933 struct commit_list *commit_list,
1934 unsigned int flags)
1936 while (commit_list) {
1937 struct object *object = &commit_list->item->object;
1938 object->flags |= flags;
1939 add_pending_object(revs, object, oid_to_hex(&object->oid));
1940 commit_list = commit_list->next;
1944 static void prepare_show_merge(struct rev_info *revs)
1946 struct commit_list *bases;
1947 struct commit *head, *other;
1948 struct object_id oid;
1949 const char **prune = NULL;
1950 int i, prune_num = 1; /* counting terminating NULL */
1951 struct index_state *istate = revs->repo->index;
1953 if (get_oid("HEAD", &oid))
1954 die("--merge without HEAD?");
1955 head = lookup_commit_or_die(&oid, "HEAD");
1956 if (get_oid("MERGE_HEAD", &oid))
1957 die("--merge without MERGE_HEAD?");
1958 other = lookup_commit_or_die(&oid, "MERGE_HEAD");
1959 add_pending_object(revs, &head->object, "HEAD");
1960 add_pending_object(revs, &other->object, "MERGE_HEAD");
1961 bases = get_merge_bases(head, other);
1962 add_rev_cmdline_list(revs, bases, REV_CMD_MERGE_BASE, UNINTERESTING | BOTTOM);
1963 add_pending_commit_list(revs, bases, UNINTERESTING | BOTTOM);
1964 free_commit_list(bases);
1965 head->object.flags |= SYMMETRIC_LEFT;
1967 if (!istate->cache_nr)
1968 repo_read_index(revs->repo);
1969 for (i = 0; i < istate->cache_nr; i++) {
1970 const struct cache_entry *ce = istate->cache[i];
1971 if (!ce_stage(ce))
1972 continue;
1973 if (ce_path_match(istate, ce, &revs->prune_data, NULL)) {
1974 prune_num++;
1975 REALLOC_ARRAY(prune, prune_num);
1976 prune[prune_num-2] = ce->name;
1977 prune[prune_num-1] = NULL;
1979 while ((i+1 < istate->cache_nr) &&
1980 ce_same_name(ce, istate->cache[i+1]))
1981 i++;
1983 clear_pathspec(&revs->prune_data);
1984 parse_pathspec(&revs->prune_data, PATHSPEC_ALL_MAGIC & ~PATHSPEC_LITERAL,
1985 PATHSPEC_PREFER_FULL | PATHSPEC_LITERAL_PATH, "", prune);
1986 revs->limited = 1;
1989 static int dotdot_missing(const char *arg, char *dotdot,
1990 struct rev_info *revs, int symmetric)
1992 if (revs->ignore_missing)
1993 return 0;
1994 /* de-munge so we report the full argument */
1995 *dotdot = '.';
1996 die(symmetric
1997 ? "Invalid symmetric difference expression %s"
1998 : "Invalid revision range %s", arg);
2001 static int handle_dotdot_1(const char *arg, char *dotdot,
2002 struct rev_info *revs, int flags,
2003 int cant_be_filename,
2004 struct object_context *a_oc,
2005 struct object_context *b_oc)
2007 const char *a_name, *b_name;
2008 struct object_id a_oid, b_oid;
2009 struct object *a_obj, *b_obj;
2010 unsigned int a_flags, b_flags;
2011 int symmetric = 0;
2012 unsigned int flags_exclude = flags ^ (UNINTERESTING | BOTTOM);
2013 unsigned int oc_flags = GET_OID_COMMITTISH | GET_OID_RECORD_PATH;
2015 a_name = arg;
2016 if (!*a_name)
2017 a_name = "HEAD";
2019 b_name = dotdot + 2;
2020 if (*b_name == '.') {
2021 symmetric = 1;
2022 b_name++;
2024 if (!*b_name)
2025 b_name = "HEAD";
2027 if (get_oid_with_context(revs->repo, a_name, oc_flags, &a_oid, a_oc) ||
2028 get_oid_with_context(revs->repo, b_name, oc_flags, &b_oid, b_oc))
2029 return -1;
2031 if (!cant_be_filename) {
2032 *dotdot = '.';
2033 verify_non_filename(revs->prefix, arg);
2034 *dotdot = '\0';
2037 a_obj = parse_object(revs->repo, &a_oid);
2038 b_obj = parse_object(revs->repo, &b_oid);
2039 if (!a_obj || !b_obj)
2040 return dotdot_missing(arg, dotdot, revs, symmetric);
2042 if (!symmetric) {
2043 /* just A..B */
2044 b_flags = flags;
2045 a_flags = flags_exclude;
2046 } else {
2047 /* A...B -- find merge bases between the two */
2048 struct commit *a, *b;
2049 struct commit_list *exclude;
2051 a = lookup_commit_reference(revs->repo, &a_obj->oid);
2052 b = lookup_commit_reference(revs->repo, &b_obj->oid);
2053 if (!a || !b)
2054 return dotdot_missing(arg, dotdot, revs, symmetric);
2056 exclude = get_merge_bases(a, b);
2057 add_rev_cmdline_list(revs, exclude, REV_CMD_MERGE_BASE,
2058 flags_exclude);
2059 add_pending_commit_list(revs, exclude, flags_exclude);
2060 free_commit_list(exclude);
2062 b_flags = flags;
2063 a_flags = flags | SYMMETRIC_LEFT;
2066 a_obj->flags |= a_flags;
2067 b_obj->flags |= b_flags;
2068 add_rev_cmdline(revs, a_obj, a_name, REV_CMD_LEFT, a_flags);
2069 add_rev_cmdline(revs, b_obj, b_name, REV_CMD_RIGHT, b_flags);
2070 add_pending_object_with_path(revs, a_obj, a_name, a_oc->mode, a_oc->path);
2071 add_pending_object_with_path(revs, b_obj, b_name, b_oc->mode, b_oc->path);
2072 return 0;
2075 static int handle_dotdot(const char *arg,
2076 struct rev_info *revs, int flags,
2077 int cant_be_filename)
2079 struct object_context a_oc, b_oc;
2080 char *dotdot = strstr(arg, "..");
2081 int ret;
2083 if (!dotdot)
2084 return -1;
2086 memset(&a_oc, 0, sizeof(a_oc));
2087 memset(&b_oc, 0, sizeof(b_oc));
2089 *dotdot = '\0';
2090 ret = handle_dotdot_1(arg, dotdot, revs, flags, cant_be_filename,
2091 &a_oc, &b_oc);
2092 *dotdot = '.';
2094 free(a_oc.path);
2095 free(b_oc.path);
2097 return ret;
2100 static int handle_revision_arg_1(const char *arg_, struct rev_info *revs, int flags, unsigned revarg_opt)
2102 struct object_context oc;
2103 char *mark;
2104 struct object *object;
2105 struct object_id oid;
2106 int local_flags;
2107 const char *arg = arg_;
2108 int cant_be_filename = revarg_opt & REVARG_CANNOT_BE_FILENAME;
2109 unsigned get_sha1_flags = GET_OID_RECORD_PATH;
2111 flags = flags & UNINTERESTING ? flags | BOTTOM : flags & ~BOTTOM;
2113 if (!cant_be_filename && !strcmp(arg, "..")) {
2115 * Just ".."? That is not a range but the
2116 * pathspec for the parent directory.
2118 return -1;
2121 if (!handle_dotdot(arg, revs, flags, revarg_opt))
2122 return 0;
2124 mark = strstr(arg, "^@");
2125 if (mark && !mark[2]) {
2126 *mark = 0;
2127 if (add_parents_only(revs, arg, flags, 0))
2128 return 0;
2129 *mark = '^';
2131 mark = strstr(arg, "^!");
2132 if (mark && !mark[2]) {
2133 *mark = 0;
2134 if (!add_parents_only(revs, arg, flags ^ (UNINTERESTING | BOTTOM), 0))
2135 *mark = '^';
2137 mark = strstr(arg, "^-");
2138 if (mark) {
2139 int exclude_parent = 1;
2141 if (mark[2]) {
2142 if (strtol_i(mark + 2, 10, &exclude_parent) ||
2143 exclude_parent < 1)
2144 return -1;
2147 *mark = 0;
2148 if (!add_parents_only(revs, arg, flags ^ (UNINTERESTING | BOTTOM), exclude_parent))
2149 *mark = '^';
2152 local_flags = 0;
2153 if (*arg == '^') {
2154 local_flags = UNINTERESTING | BOTTOM;
2155 arg++;
2158 if (revarg_opt & REVARG_COMMITTISH)
2159 get_sha1_flags |= GET_OID_COMMITTISH;
2161 if (get_oid_with_context(revs->repo, arg, get_sha1_flags, &oid, &oc))
2162 return revs->ignore_missing ? 0 : -1;
2163 if (!cant_be_filename)
2164 verify_non_filename(revs->prefix, arg);
2165 object = get_reference(revs, arg, &oid, flags ^ local_flags);
2166 if (!object)
2167 return revs->ignore_missing ? 0 : -1;
2168 add_rev_cmdline(revs, object, arg_, REV_CMD_REV, flags ^ local_flags);
2169 add_pending_object_with_path(revs, object, arg, oc.mode, oc.path);
2170 free(oc.path);
2171 return 0;
2174 int handle_revision_arg(const char *arg, struct rev_info *revs, int flags, unsigned revarg_opt)
2176 int ret = handle_revision_arg_1(arg, revs, flags, revarg_opt);
2177 if (!ret)
2178 revs->rev_input_given = 1;
2179 return ret;
2182 static void read_pathspec_from_stdin(struct strbuf *sb,
2183 struct strvec *prune)
2185 while (strbuf_getline(sb, stdin) != EOF)
2186 strvec_push(prune, sb->buf);
2189 static void read_revisions_from_stdin(struct rev_info *revs,
2190 struct strvec *prune)
2192 struct strbuf sb;
2193 int seen_dashdash = 0;
2194 int save_warning;
2196 save_warning = warn_on_object_refname_ambiguity;
2197 warn_on_object_refname_ambiguity = 0;
2199 strbuf_init(&sb, 1000);
2200 while (strbuf_getline(&sb, stdin) != EOF) {
2201 int len = sb.len;
2202 if (!len)
2203 break;
2204 if (sb.buf[0] == '-') {
2205 if (len == 2 && sb.buf[1] == '-') {
2206 seen_dashdash = 1;
2207 break;
2209 die("options not supported in --stdin mode");
2211 if (handle_revision_arg(sb.buf, revs, 0,
2212 REVARG_CANNOT_BE_FILENAME))
2213 die("bad revision '%s'", sb.buf);
2215 if (seen_dashdash)
2216 read_pathspec_from_stdin(&sb, prune);
2218 strbuf_release(&sb);
2219 warn_on_object_refname_ambiguity = save_warning;
2222 static void add_grep(struct rev_info *revs, const char *ptn, enum grep_pat_token what)
2224 append_grep_pattern(&revs->grep_filter, ptn, "command line", 0, what);
2227 static void add_header_grep(struct rev_info *revs, enum grep_header_field field, const char *pattern)
2229 append_header_grep_pattern(&revs->grep_filter, field, pattern);
2232 static void add_message_grep(struct rev_info *revs, const char *pattern)
2234 add_grep(revs, pattern, GREP_PATTERN_BODY);
2237 static int handle_revision_opt(struct rev_info *revs, int argc, const char **argv,
2238 int *unkc, const char **unkv,
2239 const struct setup_revision_opt* opt)
2241 const char *arg = argv[0];
2242 const char *optarg = NULL;
2243 int argcount;
2244 const unsigned hexsz = the_hash_algo->hexsz;
2246 /* pseudo revision arguments */
2247 if (!strcmp(arg, "--all") || !strcmp(arg, "--branches") ||
2248 !strcmp(arg, "--tags") || !strcmp(arg, "--remotes") ||
2249 !strcmp(arg, "--reflog") || !strcmp(arg, "--not") ||
2250 !strcmp(arg, "--no-walk") || !strcmp(arg, "--do-walk") ||
2251 !strcmp(arg, "--bisect") || starts_with(arg, "--glob=") ||
2252 !strcmp(arg, "--indexed-objects") ||
2253 !strcmp(arg, "--alternate-refs") ||
2254 starts_with(arg, "--exclude=") || starts_with(arg, "--exclude-hidden=") ||
2255 starts_with(arg, "--branches=") || starts_with(arg, "--tags=") ||
2256 starts_with(arg, "--remotes=") || starts_with(arg, "--no-walk="))
2258 unkv[(*unkc)++] = arg;
2259 return 1;
2262 if ((argcount = parse_long_opt("max-count", argv, &optarg))) {
2263 revs->max_count = atoi(optarg);
2264 revs->no_walk = 0;
2265 return argcount;
2266 } else if ((argcount = parse_long_opt("skip", argv, &optarg))) {
2267 revs->skip_count = atoi(optarg);
2268 return argcount;
2269 } else if ((*arg == '-') && isdigit(arg[1])) {
2270 /* accept -<digit>, like traditional "head" */
2271 if (strtol_i(arg + 1, 10, &revs->max_count) < 0 ||
2272 revs->max_count < 0)
2273 die("'%s': not a non-negative integer", arg + 1);
2274 revs->no_walk = 0;
2275 } else if (!strcmp(arg, "-n")) {
2276 if (argc <= 1)
2277 return error("-n requires an argument");
2278 revs->max_count = atoi(argv[1]);
2279 revs->no_walk = 0;
2280 return 2;
2281 } else if (skip_prefix(arg, "-n", &optarg)) {
2282 revs->max_count = atoi(optarg);
2283 revs->no_walk = 0;
2284 } else if ((argcount = parse_long_opt("max-age", argv, &optarg))) {
2285 revs->max_age = atoi(optarg);
2286 return argcount;
2287 } else if ((argcount = parse_long_opt("since", argv, &optarg))) {
2288 revs->max_age = approxidate(optarg);
2289 return argcount;
2290 } else if ((argcount = parse_long_opt("since-as-filter", argv, &optarg))) {
2291 revs->max_age_as_filter = approxidate(optarg);
2292 return argcount;
2293 } else if ((argcount = parse_long_opt("after", argv, &optarg))) {
2294 revs->max_age = approxidate(optarg);
2295 return argcount;
2296 } else if ((argcount = parse_long_opt("min-age", argv, &optarg))) {
2297 revs->min_age = atoi(optarg);
2298 return argcount;
2299 } else if ((argcount = parse_long_opt("before", argv, &optarg))) {
2300 revs->min_age = approxidate(optarg);
2301 return argcount;
2302 } else if ((argcount = parse_long_opt("until", argv, &optarg))) {
2303 revs->min_age = approxidate(optarg);
2304 return argcount;
2305 } else if (!strcmp(arg, "--first-parent")) {
2306 revs->first_parent_only = 1;
2307 } else if (!strcmp(arg, "--exclude-first-parent-only")) {
2308 revs->exclude_first_parent_only = 1;
2309 } else if (!strcmp(arg, "--ancestry-path")) {
2310 revs->ancestry_path = 1;
2311 revs->simplify_history = 0;
2312 revs->limited = 1;
2313 revs->ancestry_path_implicit_bottoms = 1;
2314 } else if (skip_prefix(arg, "--ancestry-path=", &optarg)) {
2315 struct commit *c;
2316 struct object_id oid;
2317 const char *msg = _("could not get commit for ancestry-path argument %s");
2319 revs->ancestry_path = 1;
2320 revs->simplify_history = 0;
2321 revs->limited = 1;
2323 if (repo_get_oid_committish(revs->repo, optarg, &oid))
2324 return error(msg, optarg);
2325 get_reference(revs, optarg, &oid, ANCESTRY_PATH);
2326 c = lookup_commit_reference(revs->repo, &oid);
2327 if (!c)
2328 return error(msg, optarg);
2329 commit_list_insert(c, &revs->ancestry_path_bottoms);
2330 } else if (!strcmp(arg, "-g") || !strcmp(arg, "--walk-reflogs")) {
2331 init_reflog_walk(&revs->reflog_info);
2332 } else if (!strcmp(arg, "--default")) {
2333 if (argc <= 1)
2334 return error("bad --default argument");
2335 revs->def = argv[1];
2336 return 2;
2337 } else if (!strcmp(arg, "--merge")) {
2338 revs->show_merge = 1;
2339 } else if (!strcmp(arg, "--topo-order")) {
2340 revs->sort_order = REV_SORT_IN_GRAPH_ORDER;
2341 revs->topo_order = 1;
2342 } else if (!strcmp(arg, "--simplify-merges")) {
2343 revs->simplify_merges = 1;
2344 revs->topo_order = 1;
2345 revs->rewrite_parents = 1;
2346 revs->simplify_history = 0;
2347 revs->limited = 1;
2348 } else if (!strcmp(arg, "--simplify-by-decoration")) {
2349 revs->simplify_merges = 1;
2350 revs->topo_order = 1;
2351 revs->rewrite_parents = 1;
2352 revs->simplify_history = 0;
2353 revs->simplify_by_decoration = 1;
2354 revs->limited = 1;
2355 revs->prune = 1;
2356 } else if (!strcmp(arg, "--date-order")) {
2357 revs->sort_order = REV_SORT_BY_COMMIT_DATE;
2358 revs->topo_order = 1;
2359 } else if (!strcmp(arg, "--author-date-order")) {
2360 revs->sort_order = REV_SORT_BY_AUTHOR_DATE;
2361 revs->topo_order = 1;
2362 } else if (!strcmp(arg, "--early-output")) {
2363 revs->early_output = 100;
2364 revs->topo_order = 1;
2365 } else if (skip_prefix(arg, "--early-output=", &optarg)) {
2366 if (strtoul_ui(optarg, 10, &revs->early_output) < 0)
2367 die("'%s': not a non-negative integer", optarg);
2368 revs->topo_order = 1;
2369 } else if (!strcmp(arg, "--parents")) {
2370 revs->rewrite_parents = 1;
2371 revs->print_parents = 1;
2372 } else if (!strcmp(arg, "--dense")) {
2373 revs->dense = 1;
2374 } else if (!strcmp(arg, "--sparse")) {
2375 revs->dense = 0;
2376 } else if (!strcmp(arg, "--in-commit-order")) {
2377 revs->tree_blobs_in_commit_order = 1;
2378 } else if (!strcmp(arg, "--remove-empty")) {
2379 revs->remove_empty_trees = 1;
2380 } else if (!strcmp(arg, "--merges")) {
2381 revs->min_parents = 2;
2382 } else if (!strcmp(arg, "--no-merges")) {
2383 revs->max_parents = 1;
2384 } else if (skip_prefix(arg, "--min-parents=", &optarg)) {
2385 revs->min_parents = atoi(optarg);
2386 } else if (!strcmp(arg, "--no-min-parents")) {
2387 revs->min_parents = 0;
2388 } else if (skip_prefix(arg, "--max-parents=", &optarg)) {
2389 revs->max_parents = atoi(optarg);
2390 } else if (!strcmp(arg, "--no-max-parents")) {
2391 revs->max_parents = -1;
2392 } else if (!strcmp(arg, "--boundary")) {
2393 revs->boundary = 1;
2394 } else if (!strcmp(arg, "--left-right")) {
2395 revs->left_right = 1;
2396 } else if (!strcmp(arg, "--left-only")) {
2397 if (revs->right_only)
2398 die("--left-only is incompatible with --right-only"
2399 " or --cherry");
2400 revs->left_only = 1;
2401 } else if (!strcmp(arg, "--right-only")) {
2402 if (revs->left_only)
2403 die(_("options '%s' and '%s' cannot be used together"), "--right-only", "--left-only");
2404 revs->right_only = 1;
2405 } else if (!strcmp(arg, "--cherry")) {
2406 if (revs->left_only)
2407 die(_("options '%s' and '%s' cannot be used together"), "--cherry", "--left-only");
2408 revs->cherry_mark = 1;
2409 revs->right_only = 1;
2410 revs->max_parents = 1;
2411 revs->limited = 1;
2412 } else if (!strcmp(arg, "--count")) {
2413 revs->count = 1;
2414 } else if (!strcmp(arg, "--cherry-mark")) {
2415 if (revs->cherry_pick)
2416 die(_("options '%s' and '%s' cannot be used together"), "--cherry-mark", "--cherry-pick");
2417 revs->cherry_mark = 1;
2418 revs->limited = 1; /* needs limit_list() */
2419 } else if (!strcmp(arg, "--cherry-pick")) {
2420 if (revs->cherry_mark)
2421 die(_("options '%s' and '%s' cannot be used together"), "--cherry-pick", "--cherry-mark");
2422 revs->cherry_pick = 1;
2423 revs->limited = 1;
2424 } else if (!strcmp(arg, "--objects")) {
2425 revs->tag_objects = 1;
2426 revs->tree_objects = 1;
2427 revs->blob_objects = 1;
2428 } else if (!strcmp(arg, "--objects-edge")) {
2429 revs->tag_objects = 1;
2430 revs->tree_objects = 1;
2431 revs->blob_objects = 1;
2432 revs->edge_hint = 1;
2433 } else if (!strcmp(arg, "--objects-edge-aggressive")) {
2434 revs->tag_objects = 1;
2435 revs->tree_objects = 1;
2436 revs->blob_objects = 1;
2437 revs->edge_hint = 1;
2438 revs->edge_hint_aggressive = 1;
2439 } else if (!strcmp(arg, "--verify-objects")) {
2440 revs->tag_objects = 1;
2441 revs->tree_objects = 1;
2442 revs->blob_objects = 1;
2443 revs->verify_objects = 1;
2444 disable_commit_graph(revs->repo);
2445 } else if (!strcmp(arg, "--unpacked")) {
2446 revs->unpacked = 1;
2447 } else if (starts_with(arg, "--unpacked=")) {
2448 die(_("--unpacked=<packfile> no longer supported"));
2449 } else if (!strcmp(arg, "--no-kept-objects")) {
2450 revs->no_kept_objects = 1;
2451 revs->keep_pack_cache_flags |= IN_CORE_KEEP_PACKS;
2452 revs->keep_pack_cache_flags |= ON_DISK_KEEP_PACKS;
2453 } else if (skip_prefix(arg, "--no-kept-objects=", &optarg)) {
2454 revs->no_kept_objects = 1;
2455 if (!strcmp(optarg, "in-core"))
2456 revs->keep_pack_cache_flags |= IN_CORE_KEEP_PACKS;
2457 if (!strcmp(optarg, "on-disk"))
2458 revs->keep_pack_cache_flags |= ON_DISK_KEEP_PACKS;
2459 } else if (!strcmp(arg, "-r")) {
2460 revs->diff = 1;
2461 revs->diffopt.flags.recursive = 1;
2462 } else if (!strcmp(arg, "-t")) {
2463 revs->diff = 1;
2464 revs->diffopt.flags.recursive = 1;
2465 revs->diffopt.flags.tree_in_recursive = 1;
2466 } else if ((argcount = diff_merges_parse_opts(revs, argv))) {
2467 return argcount;
2468 } else if (!strcmp(arg, "-v")) {
2469 revs->verbose_header = 1;
2470 } else if (!strcmp(arg, "--pretty")) {
2471 revs->verbose_header = 1;
2472 revs->pretty_given = 1;
2473 get_commit_format(NULL, revs);
2474 } else if (skip_prefix(arg, "--pretty=", &optarg) ||
2475 skip_prefix(arg, "--format=", &optarg)) {
2477 * Detached form ("--pretty X" as opposed to "--pretty=X")
2478 * not allowed, since the argument is optional.
2480 revs->verbose_header = 1;
2481 revs->pretty_given = 1;
2482 get_commit_format(optarg, revs);
2483 } else if (!strcmp(arg, "--expand-tabs")) {
2484 revs->expand_tabs_in_log = 8;
2485 } else if (!strcmp(arg, "--no-expand-tabs")) {
2486 revs->expand_tabs_in_log = 0;
2487 } else if (skip_prefix(arg, "--expand-tabs=", &arg)) {
2488 int val;
2489 if (strtol_i(arg, 10, &val) < 0 || val < 0)
2490 die("'%s': not a non-negative integer", arg);
2491 revs->expand_tabs_in_log = val;
2492 } else if (!strcmp(arg, "--show-notes") || !strcmp(arg, "--notes")) {
2493 enable_default_display_notes(&revs->notes_opt, &revs->show_notes);
2494 revs->show_notes_given = 1;
2495 } else if (!strcmp(arg, "--show-signature")) {
2496 revs->show_signature = 1;
2497 } else if (!strcmp(arg, "--no-show-signature")) {
2498 revs->show_signature = 0;
2499 } else if (!strcmp(arg, "--show-linear-break")) {
2500 revs->break_bar = " ..........";
2501 revs->track_linear = 1;
2502 revs->track_first_time = 1;
2503 } else if (skip_prefix(arg, "--show-linear-break=", &optarg)) {
2504 revs->break_bar = xstrdup(optarg);
2505 revs->track_linear = 1;
2506 revs->track_first_time = 1;
2507 } else if (skip_prefix(arg, "--show-notes=", &optarg) ||
2508 skip_prefix(arg, "--notes=", &optarg)) {
2509 if (starts_with(arg, "--show-notes=") &&
2510 revs->notes_opt.use_default_notes < 0)
2511 revs->notes_opt.use_default_notes = 1;
2512 enable_ref_display_notes(&revs->notes_opt, &revs->show_notes, optarg);
2513 revs->show_notes_given = 1;
2514 } else if (!strcmp(arg, "--no-notes")) {
2515 disable_display_notes(&revs->notes_opt, &revs->show_notes);
2516 revs->show_notes_given = 1;
2517 } else if (!strcmp(arg, "--standard-notes")) {
2518 revs->show_notes_given = 1;
2519 revs->notes_opt.use_default_notes = 1;
2520 } else if (!strcmp(arg, "--no-standard-notes")) {
2521 revs->notes_opt.use_default_notes = 0;
2522 } else if (!strcmp(arg, "--oneline")) {
2523 revs->verbose_header = 1;
2524 get_commit_format("oneline", revs);
2525 revs->pretty_given = 1;
2526 revs->abbrev_commit = 1;
2527 } else if (!strcmp(arg, "--graph")) {
2528 graph_clear(revs->graph);
2529 revs->graph = graph_init(revs);
2530 } else if (!strcmp(arg, "--no-graph")) {
2531 graph_clear(revs->graph);
2532 revs->graph = NULL;
2533 } else if (!strcmp(arg, "--encode-email-headers")) {
2534 revs->encode_email_headers = 1;
2535 } else if (!strcmp(arg, "--no-encode-email-headers")) {
2536 revs->encode_email_headers = 0;
2537 } else if (!strcmp(arg, "--root")) {
2538 revs->show_root_diff = 1;
2539 } else if (!strcmp(arg, "--no-commit-id")) {
2540 revs->no_commit_id = 1;
2541 } else if (!strcmp(arg, "--always")) {
2542 revs->always_show_header = 1;
2543 } else if (!strcmp(arg, "--no-abbrev")) {
2544 revs->abbrev = 0;
2545 } else if (!strcmp(arg, "--abbrev")) {
2546 revs->abbrev = DEFAULT_ABBREV;
2547 } else if (skip_prefix(arg, "--abbrev=", &optarg)) {
2548 revs->abbrev = strtoul(optarg, NULL, 10);
2549 if (revs->abbrev < MINIMUM_ABBREV)
2550 revs->abbrev = MINIMUM_ABBREV;
2551 else if (revs->abbrev > hexsz)
2552 revs->abbrev = hexsz;
2553 } else if (!strcmp(arg, "--abbrev-commit")) {
2554 revs->abbrev_commit = 1;
2555 revs->abbrev_commit_given = 1;
2556 } else if (!strcmp(arg, "--no-abbrev-commit")) {
2557 revs->abbrev_commit = 0;
2558 } else if (!strcmp(arg, "--full-diff")) {
2559 revs->diff = 1;
2560 revs->full_diff = 1;
2561 } else if (!strcmp(arg, "--show-pulls")) {
2562 revs->show_pulls = 1;
2563 } else if (!strcmp(arg, "--full-history")) {
2564 revs->simplify_history = 0;
2565 } else if (!strcmp(arg, "--relative-date")) {
2566 revs->date_mode.type = DATE_RELATIVE;
2567 revs->date_mode_explicit = 1;
2568 } else if ((argcount = parse_long_opt("date", argv, &optarg))) {
2569 parse_date_format(optarg, &revs->date_mode);
2570 revs->date_mode_explicit = 1;
2571 return argcount;
2572 } else if (!strcmp(arg, "--log-size")) {
2573 revs->show_log_size = 1;
2576 * Grepping the commit log
2578 else if ((argcount = parse_long_opt("author", argv, &optarg))) {
2579 add_header_grep(revs, GREP_HEADER_AUTHOR, optarg);
2580 return argcount;
2581 } else if ((argcount = parse_long_opt("committer", argv, &optarg))) {
2582 add_header_grep(revs, GREP_HEADER_COMMITTER, optarg);
2583 return argcount;
2584 } else if ((argcount = parse_long_opt("grep-reflog", argv, &optarg))) {
2585 add_header_grep(revs, GREP_HEADER_REFLOG, optarg);
2586 return argcount;
2587 } else if ((argcount = parse_long_opt("grep", argv, &optarg))) {
2588 add_message_grep(revs, optarg);
2589 return argcount;
2590 } else if (!strcmp(arg, "--basic-regexp")) {
2591 revs->grep_filter.pattern_type_option = GREP_PATTERN_TYPE_BRE;
2592 } else if (!strcmp(arg, "--extended-regexp") || !strcmp(arg, "-E")) {
2593 revs->grep_filter.pattern_type_option = GREP_PATTERN_TYPE_ERE;
2594 } else if (!strcmp(arg, "--regexp-ignore-case") || !strcmp(arg, "-i")) {
2595 revs->grep_filter.ignore_case = 1;
2596 revs->diffopt.pickaxe_opts |= DIFF_PICKAXE_IGNORE_CASE;
2597 } else if (!strcmp(arg, "--fixed-strings") || !strcmp(arg, "-F")) {
2598 revs->grep_filter.pattern_type_option = GREP_PATTERN_TYPE_FIXED;
2599 } else if (!strcmp(arg, "--perl-regexp") || !strcmp(arg, "-P")) {
2600 revs->grep_filter.pattern_type_option = GREP_PATTERN_TYPE_PCRE;
2601 } else if (!strcmp(arg, "--all-match")) {
2602 revs->grep_filter.all_match = 1;
2603 } else if (!strcmp(arg, "--invert-grep")) {
2604 revs->grep_filter.no_body_match = 1;
2605 } else if ((argcount = parse_long_opt("encoding", argv, &optarg))) {
2606 if (strcmp(optarg, "none"))
2607 git_log_output_encoding = xstrdup(optarg);
2608 else
2609 git_log_output_encoding = "";
2610 return argcount;
2611 } else if (!strcmp(arg, "--reverse")) {
2612 revs->reverse ^= 1;
2613 } else if (!strcmp(arg, "--children")) {
2614 revs->children.name = "children";
2615 revs->limited = 1;
2616 } else if (!strcmp(arg, "--ignore-missing")) {
2617 revs->ignore_missing = 1;
2618 } else if (opt && opt->allow_exclude_promisor_objects &&
2619 !strcmp(arg, "--exclude-promisor-objects")) {
2620 if (fetch_if_missing)
2621 BUG("exclude_promisor_objects can only be used when fetch_if_missing is 0");
2622 revs->exclude_promisor_objects = 1;
2623 } else {
2624 int opts = diff_opt_parse(&revs->diffopt, argv, argc, revs->prefix);
2625 if (!opts)
2626 unkv[(*unkc)++] = arg;
2627 return opts;
2630 return 1;
2633 void parse_revision_opt(struct rev_info *revs, struct parse_opt_ctx_t *ctx,
2634 const struct option *options,
2635 const char * const usagestr[])
2637 int n = handle_revision_opt(revs, ctx->argc, ctx->argv,
2638 &ctx->cpidx, ctx->out, NULL);
2639 if (n <= 0) {
2640 error("unknown option `%s'", ctx->argv[0]);
2641 usage_with_options(usagestr, options);
2643 ctx->argv += n;
2644 ctx->argc -= n;
2647 void revision_opts_finish(struct rev_info *revs)
2649 if (revs->graph && revs->track_linear)
2650 die(_("options '%s' and '%s' cannot be used together"), "--show-linear-break", "--graph");
2652 if (revs->graph) {
2653 revs->topo_order = 1;
2654 revs->rewrite_parents = 1;
2658 static int for_each_bisect_ref(struct ref_store *refs, each_ref_fn fn,
2659 void *cb_data, const char *term)
2661 struct strbuf bisect_refs = STRBUF_INIT;
2662 int status;
2663 strbuf_addf(&bisect_refs, "refs/bisect/%s", term);
2664 status = refs_for_each_fullref_in(refs, bisect_refs.buf, fn, cb_data);
2665 strbuf_release(&bisect_refs);
2666 return status;
2669 static int for_each_bad_bisect_ref(struct ref_store *refs, each_ref_fn fn, void *cb_data)
2671 return for_each_bisect_ref(refs, fn, cb_data, term_bad);
2674 static int for_each_good_bisect_ref(struct ref_store *refs, each_ref_fn fn, void *cb_data)
2676 return for_each_bisect_ref(refs, fn, cb_data, term_good);
2679 static int handle_revision_pseudo_opt(struct rev_info *revs,
2680 const char **argv, int *flags)
2682 const char *arg = argv[0];
2683 const char *optarg;
2684 struct ref_store *refs;
2685 int argcount;
2687 if (revs->repo != the_repository) {
2689 * We need some something like get_submodule_worktrees()
2690 * before we can go through all worktrees of a submodule,
2691 * .e.g with adding all HEADs from --all, which is not
2692 * supported right now, so stick to single worktree.
2694 if (!revs->single_worktree)
2695 BUG("--single-worktree cannot be used together with submodule");
2697 refs = get_main_ref_store(revs->repo);
2700 * NOTE!
2702 * Commands like "git shortlog" will not accept the options below
2703 * unless parse_revision_opt queues them (as opposed to erroring
2704 * out).
2706 * When implementing your new pseudo-option, remember to
2707 * register it in the list at the top of handle_revision_opt.
2709 if (!strcmp(arg, "--all")) {
2710 handle_refs(refs, revs, *flags, refs_for_each_ref);
2711 handle_refs(refs, revs, *flags, refs_head_ref);
2712 if (!revs->single_worktree) {
2713 struct all_refs_cb cb;
2715 init_all_refs_cb(&cb, revs, *flags);
2716 other_head_refs(handle_one_ref, &cb);
2718 clear_ref_exclusions(&revs->ref_excludes);
2719 } else if (!strcmp(arg, "--branches")) {
2720 if (revs->ref_excludes.hidden_refs_configured)
2721 return error(_("--exclude-hidden cannot be used together with --branches"));
2722 handle_refs(refs, revs, *flags, refs_for_each_branch_ref);
2723 clear_ref_exclusions(&revs->ref_excludes);
2724 } else if (!strcmp(arg, "--bisect")) {
2725 read_bisect_terms(&term_bad, &term_good);
2726 handle_refs(refs, revs, *flags, for_each_bad_bisect_ref);
2727 handle_refs(refs, revs, *flags ^ (UNINTERESTING | BOTTOM),
2728 for_each_good_bisect_ref);
2729 revs->bisect = 1;
2730 } else if (!strcmp(arg, "--tags")) {
2731 if (revs->ref_excludes.hidden_refs_configured)
2732 return error(_("--exclude-hidden cannot be used together with --tags"));
2733 handle_refs(refs, revs, *flags, refs_for_each_tag_ref);
2734 clear_ref_exclusions(&revs->ref_excludes);
2735 } else if (!strcmp(arg, "--remotes")) {
2736 if (revs->ref_excludes.hidden_refs_configured)
2737 return error(_("--exclude-hidden cannot be used together with --remotes"));
2738 handle_refs(refs, revs, *flags, refs_for_each_remote_ref);
2739 clear_ref_exclusions(&revs->ref_excludes);
2740 } else if ((argcount = parse_long_opt("glob", argv, &optarg))) {
2741 struct all_refs_cb cb;
2742 init_all_refs_cb(&cb, revs, *flags);
2743 for_each_glob_ref(handle_one_ref, optarg, &cb);
2744 clear_ref_exclusions(&revs->ref_excludes);
2745 return argcount;
2746 } else if ((argcount = parse_long_opt("exclude", argv, &optarg))) {
2747 add_ref_exclusion(&revs->ref_excludes, optarg);
2748 return argcount;
2749 } else if ((argcount = parse_long_opt("exclude-hidden", argv, &optarg))) {
2750 exclude_hidden_refs(&revs->ref_excludes, optarg);
2751 return argcount;
2752 } else if (skip_prefix(arg, "--branches=", &optarg)) {
2753 struct all_refs_cb cb;
2754 if (revs->ref_excludes.hidden_refs_configured)
2755 return error(_("--exclude-hidden cannot be used together with --branches"));
2756 init_all_refs_cb(&cb, revs, *flags);
2757 for_each_glob_ref_in(handle_one_ref, optarg, "refs/heads/", &cb);
2758 clear_ref_exclusions(&revs->ref_excludes);
2759 } else if (skip_prefix(arg, "--tags=", &optarg)) {
2760 struct all_refs_cb cb;
2761 if (revs->ref_excludes.hidden_refs_configured)
2762 return error(_("--exclude-hidden cannot be used together with --tags"));
2763 init_all_refs_cb(&cb, revs, *flags);
2764 for_each_glob_ref_in(handle_one_ref, optarg, "refs/tags/", &cb);
2765 clear_ref_exclusions(&revs->ref_excludes);
2766 } else if (skip_prefix(arg, "--remotes=", &optarg)) {
2767 struct all_refs_cb cb;
2768 if (revs->ref_excludes.hidden_refs_configured)
2769 return error(_("--exclude-hidden cannot be used together with --remotes"));
2770 init_all_refs_cb(&cb, revs, *flags);
2771 for_each_glob_ref_in(handle_one_ref, optarg, "refs/remotes/", &cb);
2772 clear_ref_exclusions(&revs->ref_excludes);
2773 } else if (!strcmp(arg, "--reflog")) {
2774 add_reflogs_to_pending(revs, *flags);
2775 } else if (!strcmp(arg, "--indexed-objects")) {
2776 add_index_objects_to_pending(revs, *flags);
2777 } else if (!strcmp(arg, "--alternate-refs")) {
2778 add_alternate_refs_to_pending(revs, *flags);
2779 } else if (!strcmp(arg, "--not")) {
2780 *flags ^= UNINTERESTING | BOTTOM;
2781 } else if (!strcmp(arg, "--no-walk")) {
2782 revs->no_walk = 1;
2783 } else if (skip_prefix(arg, "--no-walk=", &optarg)) {
2785 * Detached form ("--no-walk X" as opposed to "--no-walk=X")
2786 * not allowed, since the argument is optional.
2788 revs->no_walk = 1;
2789 if (!strcmp(optarg, "sorted"))
2790 revs->unsorted_input = 0;
2791 else if (!strcmp(optarg, "unsorted"))
2792 revs->unsorted_input = 1;
2793 else
2794 return error("invalid argument to --no-walk");
2795 } else if (!strcmp(arg, "--do-walk")) {
2796 revs->no_walk = 0;
2797 } else if (!strcmp(arg, "--single-worktree")) {
2798 revs->single_worktree = 1;
2799 } else if (skip_prefix(arg, ("--filter="), &arg)) {
2800 parse_list_objects_filter(&revs->filter, arg);
2801 } else if (!strcmp(arg, ("--no-filter"))) {
2802 list_objects_filter_set_no_filter(&revs->filter);
2803 } else {
2804 return 0;
2807 return 1;
2810 static void NORETURN diagnose_missing_default(const char *def)
2812 int flags;
2813 const char *refname;
2815 refname = resolve_ref_unsafe(def, 0, NULL, &flags);
2816 if (!refname || !(flags & REF_ISSYMREF) || (flags & REF_ISBROKEN))
2817 die(_("your current branch appears to be broken"));
2819 skip_prefix(refname, "refs/heads/", &refname);
2820 die(_("your current branch '%s' does not have any commits yet"),
2821 refname);
2825 * Parse revision information, filling in the "rev_info" structure,
2826 * and removing the used arguments from the argument list.
2828 * Returns the number of arguments left that weren't recognized
2829 * (which are also moved to the head of the argument list)
2831 int setup_revisions(int argc, const char **argv, struct rev_info *revs, struct setup_revision_opt *opt)
2833 int i, flags, left, seen_dashdash, revarg_opt;
2834 struct strvec prune_data = STRVEC_INIT;
2835 int seen_end_of_options = 0;
2837 /* First, search for "--" */
2838 if (opt && opt->assume_dashdash) {
2839 seen_dashdash = 1;
2840 } else {
2841 seen_dashdash = 0;
2842 for (i = 1; i < argc; i++) {
2843 const char *arg = argv[i];
2844 if (strcmp(arg, "--"))
2845 continue;
2846 if (opt && opt->free_removed_argv_elements)
2847 free((char *)argv[i]);
2848 argv[i] = NULL;
2849 argc = i;
2850 if (argv[i + 1])
2851 strvec_pushv(&prune_data, argv + i + 1);
2852 seen_dashdash = 1;
2853 break;
2857 /* Second, deal with arguments and options */
2858 flags = 0;
2859 revarg_opt = opt ? opt->revarg_opt : 0;
2860 if (seen_dashdash)
2861 revarg_opt |= REVARG_CANNOT_BE_FILENAME;
2862 for (left = i = 1; i < argc; i++) {
2863 const char *arg = argv[i];
2864 if (!seen_end_of_options && *arg == '-') {
2865 int opts;
2867 opts = handle_revision_pseudo_opt(
2868 revs, argv + i,
2869 &flags);
2870 if (opts > 0) {
2871 i += opts - 1;
2872 continue;
2875 if (!strcmp(arg, "--stdin")) {
2876 if (revs->disable_stdin) {
2877 argv[left++] = arg;
2878 continue;
2880 if (revs->read_from_stdin++)
2881 die("--stdin given twice?");
2882 read_revisions_from_stdin(revs, &prune_data);
2883 continue;
2886 if (!strcmp(arg, "--end-of-options")) {
2887 seen_end_of_options = 1;
2888 continue;
2891 opts = handle_revision_opt(revs, argc - i, argv + i,
2892 &left, argv, opt);
2893 if (opts > 0) {
2894 i += opts - 1;
2895 continue;
2897 if (opts < 0)
2898 exit(128);
2899 continue;
2903 if (handle_revision_arg(arg, revs, flags, revarg_opt)) {
2904 int j;
2905 if (seen_dashdash || *arg == '^')
2906 die("bad revision '%s'", arg);
2908 /* If we didn't have a "--":
2909 * (1) all filenames must exist;
2910 * (2) all rev-args must not be interpretable
2911 * as a valid filename.
2912 * but the latter we have checked in the main loop.
2914 for (j = i; j < argc; j++)
2915 verify_filename(revs->prefix, argv[j], j == i);
2917 strvec_pushv(&prune_data, argv + i);
2918 break;
2921 revision_opts_finish(revs);
2923 if (prune_data.nr) {
2925 * If we need to introduce the magic "a lone ':' means no
2926 * pathspec whatsoever", here is the place to do so.
2928 * if (prune_data.nr == 1 && !strcmp(prune_data[0], ":")) {
2929 * prune_data.nr = 0;
2930 * prune_data.alloc = 0;
2931 * free(prune_data.path);
2932 * prune_data.path = NULL;
2933 * } else {
2934 * terminate prune_data.alloc with NULL and
2935 * call init_pathspec() to set revs->prune_data here.
2938 parse_pathspec(&revs->prune_data, 0, 0,
2939 revs->prefix, prune_data.v);
2941 strvec_clear(&prune_data);
2943 if (!revs->def)
2944 revs->def = opt ? opt->def : NULL;
2945 if (opt && opt->tweak)
2946 opt->tweak(revs, opt);
2947 if (revs->show_merge)
2948 prepare_show_merge(revs);
2949 if (revs->def && !revs->pending.nr && !revs->rev_input_given) {
2950 struct object_id oid;
2951 struct object *object;
2952 struct object_context oc;
2953 if (get_oid_with_context(revs->repo, revs->def, 0, &oid, &oc))
2954 diagnose_missing_default(revs->def);
2955 object = get_reference(revs, revs->def, &oid, 0);
2956 add_pending_object_with_mode(revs, object, revs->def, oc.mode);
2959 /* Did the user ask for any diff output? Run the diff! */
2960 if (revs->diffopt.output_format & ~DIFF_FORMAT_NO_OUTPUT)
2961 revs->diff = 1;
2963 /* Pickaxe, diff-filter and rename following need diffs */
2964 if ((revs->diffopt.pickaxe_opts & DIFF_PICKAXE_KINDS_MASK) ||
2965 revs->diffopt.filter ||
2966 revs->diffopt.flags.follow_renames)
2967 revs->diff = 1;
2969 if (revs->diffopt.objfind)
2970 revs->simplify_history = 0;
2972 if (revs->line_level_traverse) {
2973 if (want_ancestry(revs))
2974 revs->limited = 1;
2975 revs->topo_order = 1;
2978 if (revs->topo_order && !generation_numbers_enabled(the_repository))
2979 revs->limited = 1;
2981 if (revs->prune_data.nr) {
2982 copy_pathspec(&revs->pruning.pathspec, &revs->prune_data);
2983 /* Can't prune commits with rename following: the paths change.. */
2984 if (!revs->diffopt.flags.follow_renames)
2985 revs->prune = 1;
2986 if (!revs->full_diff)
2987 copy_pathspec(&revs->diffopt.pathspec,
2988 &revs->prune_data);
2991 diff_merges_setup_revs(revs);
2993 revs->diffopt.abbrev = revs->abbrev;
2995 diff_setup_done(&revs->diffopt);
2997 if (!is_encoding_utf8(get_log_output_encoding()))
2998 revs->grep_filter.ignore_locale = 1;
2999 compile_grep_patterns(&revs->grep_filter);
3001 if (revs->reverse && revs->reflog_info)
3002 die(_("options '%s' and '%s' cannot be used together"), "--reverse", "--walk-reflogs");
3003 if (revs->reflog_info && revs->limited)
3004 die("cannot combine --walk-reflogs with history-limiting options");
3005 if (revs->rewrite_parents && revs->children.name)
3006 die(_("options '%s' and '%s' cannot be used together"), "--parents", "--children");
3007 if (revs->filter.choice && !revs->blob_objects)
3008 die(_("object filtering requires --objects"));
3011 * Limitations on the graph functionality
3013 if (revs->reverse && revs->graph)
3014 die(_("options '%s' and '%s' cannot be used together"), "--reverse", "--graph");
3016 if (revs->reflog_info && revs->graph)
3017 die(_("options '%s' and '%s' cannot be used together"), "--walk-reflogs", "--graph");
3018 if (revs->no_walk && revs->graph)
3019 die(_("options '%s' and '%s' cannot be used together"), "--no-walk", "--graph");
3020 if (!revs->reflog_info && revs->grep_filter.use_reflog_filter)
3021 die(_("the option '%s' requires '%s'"), "--grep-reflog", "--walk-reflogs");
3023 if (revs->line_level_traverse &&
3024 (revs->diffopt.output_format & ~(DIFF_FORMAT_PATCH | DIFF_FORMAT_NO_OUTPUT)))
3025 die(_("-L does not yet support diff formats besides -p and -s"));
3027 if (revs->expand_tabs_in_log < 0)
3028 revs->expand_tabs_in_log = revs->expand_tabs_in_log_default;
3030 return left;
3033 static void release_revisions_cmdline(struct rev_cmdline_info *cmdline)
3035 unsigned int i;
3037 for (i = 0; i < cmdline->nr; i++)
3038 free((char *)cmdline->rev[i].name);
3039 free(cmdline->rev);
3042 static void release_revisions_mailmap(struct string_list *mailmap)
3044 if (!mailmap)
3045 return;
3046 clear_mailmap(mailmap);
3047 free(mailmap);
3050 static void release_revisions_topo_walk_info(struct topo_walk_info *info);
3052 void release_revisions(struct rev_info *revs)
3054 free_commit_list(revs->commits);
3055 free_commit_list(revs->ancestry_path_bottoms);
3056 object_array_clear(&revs->pending);
3057 object_array_clear(&revs->boundary_commits);
3058 release_revisions_cmdline(&revs->cmdline);
3059 list_objects_filter_release(&revs->filter);
3060 clear_pathspec(&revs->prune_data);
3061 date_mode_release(&revs->date_mode);
3062 release_revisions_mailmap(revs->mailmap);
3063 free_grep_patterns(&revs->grep_filter);
3064 graph_clear(revs->graph);
3065 /* TODO (need to handle "no_free"): diff_free(&revs->diffopt) */
3066 diff_free(&revs->pruning);
3067 reflog_walk_info_release(revs->reflog_info);
3068 release_revisions_topo_walk_info(revs->topo_walk_info);
3071 static void add_child(struct rev_info *revs, struct commit *parent, struct commit *child)
3073 struct commit_list *l = xcalloc(1, sizeof(*l));
3075 l->item = child;
3076 l->next = add_decoration(&revs->children, &parent->object, l);
3079 static int remove_duplicate_parents(struct rev_info *revs, struct commit *commit)
3081 struct treesame_state *ts = lookup_decoration(&revs->treesame, &commit->object);
3082 struct commit_list **pp, *p;
3083 int surviving_parents;
3085 /* Examine existing parents while marking ones we have seen... */
3086 pp = &commit->parents;
3087 surviving_parents = 0;
3088 while ((p = *pp) != NULL) {
3089 struct commit *parent = p->item;
3090 if (parent->object.flags & TMP_MARK) {
3091 *pp = p->next;
3092 if (ts)
3093 compact_treesame(revs, commit, surviving_parents);
3094 continue;
3096 parent->object.flags |= TMP_MARK;
3097 surviving_parents++;
3098 pp = &p->next;
3100 /* clear the temporary mark */
3101 for (p = commit->parents; p; p = p->next) {
3102 p->item->object.flags &= ~TMP_MARK;
3104 /* no update_treesame() - removing duplicates can't affect TREESAME */
3105 return surviving_parents;
3108 struct merge_simplify_state {
3109 struct commit *simplified;
3112 static struct merge_simplify_state *locate_simplify_state(struct rev_info *revs, struct commit *commit)
3114 struct merge_simplify_state *st;
3116 st = lookup_decoration(&revs->merge_simplification, &commit->object);
3117 if (!st) {
3118 CALLOC_ARRAY(st, 1);
3119 add_decoration(&revs->merge_simplification, &commit->object, st);
3121 return st;
3124 static int mark_redundant_parents(struct commit *commit)
3126 struct commit_list *h = reduce_heads(commit->parents);
3127 int i = 0, marked = 0;
3128 struct commit_list *po, *pn;
3130 /* Want these for sanity-checking only */
3131 int orig_cnt = commit_list_count(commit->parents);
3132 int cnt = commit_list_count(h);
3135 * Not ready to remove items yet, just mark them for now, based
3136 * on the output of reduce_heads(). reduce_heads outputs the reduced
3137 * set in its original order, so this isn't too hard.
3139 po = commit->parents;
3140 pn = h;
3141 while (po) {
3142 if (pn && po->item == pn->item) {
3143 pn = pn->next;
3144 i++;
3145 } else {
3146 po->item->object.flags |= TMP_MARK;
3147 marked++;
3149 po=po->next;
3152 if (i != cnt || cnt+marked != orig_cnt)
3153 die("mark_redundant_parents %d %d %d %d", orig_cnt, cnt, i, marked);
3155 free_commit_list(h);
3157 return marked;
3160 static int mark_treesame_root_parents(struct commit *commit)
3162 struct commit_list *p;
3163 int marked = 0;
3165 for (p = commit->parents; p; p = p->next) {
3166 struct commit *parent = p->item;
3167 if (!parent->parents && (parent->object.flags & TREESAME)) {
3168 parent->object.flags |= TMP_MARK;
3169 marked++;
3173 return marked;
3177 * Awkward naming - this means one parent we are TREESAME to.
3178 * cf mark_treesame_root_parents: root parents that are TREESAME (to an
3179 * empty tree). Better name suggestions?
3181 static int leave_one_treesame_to_parent(struct rev_info *revs, struct commit *commit)
3183 struct treesame_state *ts = lookup_decoration(&revs->treesame, &commit->object);
3184 struct commit *unmarked = NULL, *marked = NULL;
3185 struct commit_list *p;
3186 unsigned n;
3188 for (p = commit->parents, n = 0; p; p = p->next, n++) {
3189 if (ts->treesame[n]) {
3190 if (p->item->object.flags & TMP_MARK) {
3191 if (!marked)
3192 marked = p->item;
3193 } else {
3194 if (!unmarked) {
3195 unmarked = p->item;
3196 break;
3203 * If we are TREESAME to a marked-for-deletion parent, but not to any
3204 * unmarked parents, unmark the first TREESAME parent. This is the
3205 * parent that the default simplify_history==1 scan would have followed,
3206 * and it doesn't make sense to omit that path when asking for a
3207 * simplified full history. Retaining it improves the chances of
3208 * understanding odd missed merges that took an old version of a file.
3210 * Example:
3212 * I--------*X A modified the file, but mainline merge X used
3213 * \ / "-s ours", so took the version from I. X is
3214 * `-*A--' TREESAME to I and !TREESAME to A.
3216 * Default log from X would produce "I". Without this check,
3217 * --full-history --simplify-merges would produce "I-A-X", showing
3218 * the merge commit X and that it changed A, but not making clear that
3219 * it had just taken the I version. With this check, the topology above
3220 * is retained.
3222 * Note that it is possible that the simplification chooses a different
3223 * TREESAME parent from the default, in which case this test doesn't
3224 * activate, and we _do_ drop the default parent. Example:
3226 * I------X A modified the file, but it was reverted in B,
3227 * \ / meaning mainline merge X is TREESAME to both
3228 * *A-*B parents.
3230 * Default log would produce "I" by following the first parent;
3231 * --full-history --simplify-merges will produce "I-A-B". But this is a
3232 * reasonable result - it presents a logical full history leading from
3233 * I to X, and X is not an important merge.
3235 if (!unmarked && marked) {
3236 marked->object.flags &= ~TMP_MARK;
3237 return 1;
3240 return 0;
3243 static int remove_marked_parents(struct rev_info *revs, struct commit *commit)
3245 struct commit_list **pp, *p;
3246 int nth_parent, removed = 0;
3248 pp = &commit->parents;
3249 nth_parent = 0;
3250 while ((p = *pp) != NULL) {
3251 struct commit *parent = p->item;
3252 if (parent->object.flags & TMP_MARK) {
3253 parent->object.flags &= ~TMP_MARK;
3254 *pp = p->next;
3255 free(p);
3256 removed++;
3257 compact_treesame(revs, commit, nth_parent);
3258 continue;
3260 pp = &p->next;
3261 nth_parent++;
3264 /* Removing parents can only increase TREESAMEness */
3265 if (removed && !(commit->object.flags & TREESAME))
3266 update_treesame(revs, commit);
3268 return nth_parent;
3271 static struct commit_list **simplify_one(struct rev_info *revs, struct commit *commit, struct commit_list **tail)
3273 struct commit_list *p;
3274 struct commit *parent;
3275 struct merge_simplify_state *st, *pst;
3276 int cnt;
3278 st = locate_simplify_state(revs, commit);
3281 * Have we handled this one?
3283 if (st->simplified)
3284 return tail;
3287 * An UNINTERESTING commit simplifies to itself, so does a
3288 * root commit. We do not rewrite parents of such commit
3289 * anyway.
3291 if ((commit->object.flags & UNINTERESTING) || !commit->parents) {
3292 st->simplified = commit;
3293 return tail;
3297 * Do we know what commit all of our parents that matter
3298 * should be rewritten to? Otherwise we are not ready to
3299 * rewrite this one yet.
3301 for (cnt = 0, p = commit->parents; p; p = p->next) {
3302 pst = locate_simplify_state(revs, p->item);
3303 if (!pst->simplified) {
3304 tail = &commit_list_insert(p->item, tail)->next;
3305 cnt++;
3307 if (revs->first_parent_only)
3308 break;
3310 if (cnt) {
3311 tail = &commit_list_insert(commit, tail)->next;
3312 return tail;
3316 * Rewrite our list of parents. Note that this cannot
3317 * affect our TREESAME flags in any way - a commit is
3318 * always TREESAME to its simplification.
3320 for (p = commit->parents; p; p = p->next) {
3321 pst = locate_simplify_state(revs, p->item);
3322 p->item = pst->simplified;
3323 if (revs->first_parent_only)
3324 break;
3327 if (revs->first_parent_only)
3328 cnt = 1;
3329 else
3330 cnt = remove_duplicate_parents(revs, commit);
3333 * It is possible that we are a merge and one side branch
3334 * does not have any commit that touches the given paths;
3335 * in such a case, the immediate parent from that branch
3336 * will be rewritten to be the merge base.
3338 * o----X X: the commit we are looking at;
3339 * / / o: a commit that touches the paths;
3340 * ---o----'
3342 * Further, a merge of an independent branch that doesn't
3343 * touch the path will reduce to a treesame root parent:
3345 * ----o----X X: the commit we are looking at;
3346 * / o: a commit that touches the paths;
3347 * r r: a root commit not touching the paths
3349 * Detect and simplify both cases.
3351 if (1 < cnt) {
3352 int marked = mark_redundant_parents(commit);
3353 marked += mark_treesame_root_parents(commit);
3354 if (marked)
3355 marked -= leave_one_treesame_to_parent(revs, commit);
3356 if (marked)
3357 cnt = remove_marked_parents(revs, commit);
3361 * A commit simplifies to itself if it is a root, if it is
3362 * UNINTERESTING, if it touches the given paths, or if it is a
3363 * merge and its parents don't simplify to one relevant commit
3364 * (the first two cases are already handled at the beginning of
3365 * this function).
3367 * Otherwise, it simplifies to what its sole relevant parent
3368 * simplifies to.
3370 if (!cnt ||
3371 (commit->object.flags & UNINTERESTING) ||
3372 !(commit->object.flags & TREESAME) ||
3373 (parent = one_relevant_parent(revs, commit->parents)) == NULL ||
3374 (revs->show_pulls && (commit->object.flags & PULL_MERGE)))
3375 st->simplified = commit;
3376 else {
3377 pst = locate_simplify_state(revs, parent);
3378 st->simplified = pst->simplified;
3380 return tail;
3383 static void simplify_merges(struct rev_info *revs)
3385 struct commit_list *list, *next;
3386 struct commit_list *yet_to_do, **tail;
3387 struct commit *commit;
3389 if (!revs->prune)
3390 return;
3392 /* feed the list reversed */
3393 yet_to_do = NULL;
3394 for (list = revs->commits; list; list = next) {
3395 commit = list->item;
3396 next = list->next;
3398 * Do not free(list) here yet; the original list
3399 * is used later in this function.
3401 commit_list_insert(commit, &yet_to_do);
3403 while (yet_to_do) {
3404 list = yet_to_do;
3405 yet_to_do = NULL;
3406 tail = &yet_to_do;
3407 while (list) {
3408 commit = pop_commit(&list);
3409 tail = simplify_one(revs, commit, tail);
3413 /* clean up the result, removing the simplified ones */
3414 list = revs->commits;
3415 revs->commits = NULL;
3416 tail = &revs->commits;
3417 while (list) {
3418 struct merge_simplify_state *st;
3420 commit = pop_commit(&list);
3421 st = locate_simplify_state(revs, commit);
3422 if (st->simplified == commit)
3423 tail = &commit_list_insert(commit, tail)->next;
3427 static void set_children(struct rev_info *revs)
3429 struct commit_list *l;
3430 for (l = revs->commits; l; l = l->next) {
3431 struct commit *commit = l->item;
3432 struct commit_list *p;
3434 for (p = commit->parents; p; p = p->next)
3435 add_child(revs, p->item, commit);
3439 void reset_revision_walk(void)
3441 clear_object_flags(SEEN | ADDED | SHOWN | TOPO_WALK_EXPLORED | TOPO_WALK_INDEGREE);
3444 static int mark_uninteresting(const struct object_id *oid,
3445 struct packed_git *pack UNUSED,
3446 uint32_t pos UNUSED,
3447 void *cb)
3449 struct rev_info *revs = cb;
3450 struct object *o = lookup_unknown_object(revs->repo, oid);
3451 o->flags |= UNINTERESTING | SEEN;
3452 return 0;
3455 define_commit_slab(indegree_slab, int);
3456 define_commit_slab(author_date_slab, timestamp_t);
3458 struct topo_walk_info {
3459 timestamp_t min_generation;
3460 struct prio_queue explore_queue;
3461 struct prio_queue indegree_queue;
3462 struct prio_queue topo_queue;
3463 struct indegree_slab indegree;
3464 struct author_date_slab author_date;
3467 static int topo_walk_atexit_registered;
3468 static unsigned int count_explore_walked;
3469 static unsigned int count_indegree_walked;
3470 static unsigned int count_topo_walked;
3472 static void trace2_topo_walk_statistics_atexit(void)
3474 struct json_writer jw = JSON_WRITER_INIT;
3476 jw_object_begin(&jw, 0);
3477 jw_object_intmax(&jw, "count_explore_walked", count_explore_walked);
3478 jw_object_intmax(&jw, "count_indegree_walked", count_indegree_walked);
3479 jw_object_intmax(&jw, "count_topo_walked", count_topo_walked);
3480 jw_end(&jw);
3482 trace2_data_json("topo_walk", the_repository, "statistics", &jw);
3484 jw_release(&jw);
3487 static inline void test_flag_and_insert(struct prio_queue *q, struct commit *c, int flag)
3489 if (c->object.flags & flag)
3490 return;
3492 c->object.flags |= flag;
3493 prio_queue_put(q, c);
3496 static void explore_walk_step(struct rev_info *revs)
3498 struct topo_walk_info *info = revs->topo_walk_info;
3499 struct commit_list *p;
3500 struct commit *c = prio_queue_get(&info->explore_queue);
3502 if (!c)
3503 return;
3505 if (repo_parse_commit_gently(revs->repo, c, 1) < 0)
3506 return;
3508 count_explore_walked++;
3510 if (revs->sort_order == REV_SORT_BY_AUTHOR_DATE)
3511 record_author_date(&info->author_date, c);
3513 if (revs->max_age != -1 && (c->date < revs->max_age))
3514 c->object.flags |= UNINTERESTING;
3516 if (process_parents(revs, c, NULL, NULL) < 0)
3517 return;
3519 if (c->object.flags & UNINTERESTING)
3520 mark_parents_uninteresting(revs, c);
3522 for (p = c->parents; p; p = p->next)
3523 test_flag_and_insert(&info->explore_queue, p->item, TOPO_WALK_EXPLORED);
3526 static void explore_to_depth(struct rev_info *revs,
3527 timestamp_t gen_cutoff)
3529 struct topo_walk_info *info = revs->topo_walk_info;
3530 struct commit *c;
3531 while ((c = prio_queue_peek(&info->explore_queue)) &&
3532 commit_graph_generation(c) >= gen_cutoff)
3533 explore_walk_step(revs);
3536 static void indegree_walk_step(struct rev_info *revs)
3538 struct commit_list *p;
3539 struct topo_walk_info *info = revs->topo_walk_info;
3540 struct commit *c = prio_queue_get(&info->indegree_queue);
3542 if (!c)
3543 return;
3545 if (repo_parse_commit_gently(revs->repo, c, 1) < 0)
3546 return;
3548 count_indegree_walked++;
3550 explore_to_depth(revs, commit_graph_generation(c));
3552 for (p = c->parents; p; p = p->next) {
3553 struct commit *parent = p->item;
3554 int *pi = indegree_slab_at(&info->indegree, parent);
3556 if (repo_parse_commit_gently(revs->repo, parent, 1) < 0)
3557 return;
3559 if (*pi)
3560 (*pi)++;
3561 else
3562 *pi = 2;
3564 test_flag_and_insert(&info->indegree_queue, parent, TOPO_WALK_INDEGREE);
3566 if (revs->first_parent_only)
3567 return;
3571 static void compute_indegrees_to_depth(struct rev_info *revs,
3572 timestamp_t gen_cutoff)
3574 struct topo_walk_info *info = revs->topo_walk_info;
3575 struct commit *c;
3576 while ((c = prio_queue_peek(&info->indegree_queue)) &&
3577 commit_graph_generation(c) >= gen_cutoff)
3578 indegree_walk_step(revs);
3581 static void release_revisions_topo_walk_info(struct topo_walk_info *info)
3583 if (!info)
3584 return;
3585 clear_prio_queue(&info->explore_queue);
3586 clear_prio_queue(&info->indegree_queue);
3587 clear_prio_queue(&info->topo_queue);
3588 clear_indegree_slab(&info->indegree);
3589 clear_author_date_slab(&info->author_date);
3590 free(info);
3593 static void reset_topo_walk(struct rev_info *revs)
3595 release_revisions_topo_walk_info(revs->topo_walk_info);
3596 revs->topo_walk_info = NULL;
3599 static void init_topo_walk(struct rev_info *revs)
3601 struct topo_walk_info *info;
3602 struct commit_list *list;
3603 if (revs->topo_walk_info)
3604 reset_topo_walk(revs);
3606 revs->topo_walk_info = xmalloc(sizeof(struct topo_walk_info));
3607 info = revs->topo_walk_info;
3608 memset(info, 0, sizeof(struct topo_walk_info));
3610 init_indegree_slab(&info->indegree);
3611 memset(&info->explore_queue, 0, sizeof(info->explore_queue));
3612 memset(&info->indegree_queue, 0, sizeof(info->indegree_queue));
3613 memset(&info->topo_queue, 0, sizeof(info->topo_queue));
3615 switch (revs->sort_order) {
3616 default: /* REV_SORT_IN_GRAPH_ORDER */
3617 info->topo_queue.compare = NULL;
3618 break;
3619 case REV_SORT_BY_COMMIT_DATE:
3620 info->topo_queue.compare = compare_commits_by_commit_date;
3621 break;
3622 case REV_SORT_BY_AUTHOR_DATE:
3623 init_author_date_slab(&info->author_date);
3624 info->topo_queue.compare = compare_commits_by_author_date;
3625 info->topo_queue.cb_data = &info->author_date;
3626 break;
3629 info->explore_queue.compare = compare_commits_by_gen_then_commit_date;
3630 info->indegree_queue.compare = compare_commits_by_gen_then_commit_date;
3632 info->min_generation = GENERATION_NUMBER_INFINITY;
3633 for (list = revs->commits; list; list = list->next) {
3634 struct commit *c = list->item;
3635 timestamp_t generation;
3637 if (repo_parse_commit_gently(revs->repo, c, 1))
3638 continue;
3640 test_flag_and_insert(&info->explore_queue, c, TOPO_WALK_EXPLORED);
3641 test_flag_and_insert(&info->indegree_queue, c, TOPO_WALK_INDEGREE);
3643 generation = commit_graph_generation(c);
3644 if (generation < info->min_generation)
3645 info->min_generation = generation;
3647 *(indegree_slab_at(&info->indegree, c)) = 1;
3649 if (revs->sort_order == REV_SORT_BY_AUTHOR_DATE)
3650 record_author_date(&info->author_date, c);
3652 compute_indegrees_to_depth(revs, info->min_generation);
3654 for (list = revs->commits; list; list = list->next) {
3655 struct commit *c = list->item;
3657 if (*(indegree_slab_at(&info->indegree, c)) == 1)
3658 prio_queue_put(&info->topo_queue, c);
3662 * This is unfortunate; the initial tips need to be shown
3663 * in the order given from the revision traversal machinery.
3665 if (revs->sort_order == REV_SORT_IN_GRAPH_ORDER)
3666 prio_queue_reverse(&info->topo_queue);
3668 if (trace2_is_enabled() && !topo_walk_atexit_registered) {
3669 atexit(trace2_topo_walk_statistics_atexit);
3670 topo_walk_atexit_registered = 1;
3674 static struct commit *next_topo_commit(struct rev_info *revs)
3676 struct commit *c;
3677 struct topo_walk_info *info = revs->topo_walk_info;
3679 /* pop next off of topo_queue */
3680 c = prio_queue_get(&info->topo_queue);
3682 if (c)
3683 *(indegree_slab_at(&info->indegree, c)) = 0;
3685 return c;
3688 static void expand_topo_walk(struct rev_info *revs, struct commit *commit)
3690 struct commit_list *p;
3691 struct topo_walk_info *info = revs->topo_walk_info;
3692 if (process_parents(revs, commit, NULL, NULL) < 0) {
3693 if (!revs->ignore_missing_links)
3694 die("Failed to traverse parents of commit %s",
3695 oid_to_hex(&commit->object.oid));
3698 count_topo_walked++;
3700 for (p = commit->parents; p; p = p->next) {
3701 struct commit *parent = p->item;
3702 int *pi;
3703 timestamp_t generation;
3705 if (parent->object.flags & UNINTERESTING)
3706 continue;
3708 if (repo_parse_commit_gently(revs->repo, parent, 1) < 0)
3709 continue;
3711 generation = commit_graph_generation(parent);
3712 if (generation < info->min_generation) {
3713 info->min_generation = generation;
3714 compute_indegrees_to_depth(revs, info->min_generation);
3717 pi = indegree_slab_at(&info->indegree, parent);
3719 (*pi)--;
3720 if (*pi == 1)
3721 prio_queue_put(&info->topo_queue, parent);
3723 if (revs->first_parent_only)
3724 return;
3728 int prepare_revision_walk(struct rev_info *revs)
3730 int i;
3731 struct object_array old_pending;
3732 struct commit_list **next = &revs->commits;
3734 memcpy(&old_pending, &revs->pending, sizeof(old_pending));
3735 revs->pending.nr = 0;
3736 revs->pending.alloc = 0;
3737 revs->pending.objects = NULL;
3738 for (i = 0; i < old_pending.nr; i++) {
3739 struct object_array_entry *e = old_pending.objects + i;
3740 struct commit *commit = handle_commit(revs, e);
3741 if (commit) {
3742 if (!(commit->object.flags & SEEN)) {
3743 commit->object.flags |= SEEN;
3744 next = commit_list_append(commit, next);
3748 object_array_clear(&old_pending);
3750 /* Signal whether we need per-parent treesame decoration */
3751 if (revs->simplify_merges ||
3752 (revs->limited && limiting_can_increase_treesame(revs)))
3753 revs->treesame.name = "treesame";
3755 if (revs->exclude_promisor_objects) {
3756 for_each_packed_object(mark_uninteresting, revs,
3757 FOR_EACH_OBJECT_PROMISOR_ONLY);
3760 if (!revs->reflog_info)
3761 prepare_to_use_bloom_filter(revs);
3762 if (!revs->unsorted_input)
3763 commit_list_sort_by_date(&revs->commits);
3764 if (revs->no_walk)
3765 return 0;
3766 if (revs->limited) {
3767 if (limit_list(revs) < 0)
3768 return -1;
3769 if (revs->topo_order)
3770 sort_in_topological_order(&revs->commits, revs->sort_order);
3771 } else if (revs->topo_order)
3772 init_topo_walk(revs);
3773 if (revs->line_level_traverse && want_ancestry(revs))
3775 * At the moment we can only do line-level log with parent
3776 * rewriting by performing this expensive pre-filtering step.
3777 * If parent rewriting is not requested, then we rather
3778 * perform the line-level log filtering during the regular
3779 * history traversal.
3781 line_log_filter(revs);
3782 if (revs->simplify_merges)
3783 simplify_merges(revs);
3784 if (revs->children.name)
3785 set_children(revs);
3787 return 0;
3790 static enum rewrite_result rewrite_one_1(struct rev_info *revs,
3791 struct commit **pp,
3792 struct prio_queue *queue)
3794 for (;;) {
3795 struct commit *p = *pp;
3796 if (!revs->limited)
3797 if (process_parents(revs, p, NULL, queue) < 0)
3798 return rewrite_one_error;
3799 if (p->object.flags & UNINTERESTING)
3800 return rewrite_one_ok;
3801 if (!(p->object.flags & TREESAME))
3802 return rewrite_one_ok;
3803 if (!p->parents)
3804 return rewrite_one_noparents;
3805 if (!(p = one_relevant_parent(revs, p->parents)))
3806 return rewrite_one_ok;
3807 *pp = p;
3811 static void merge_queue_into_list(struct prio_queue *q, struct commit_list **list)
3813 while (q->nr) {
3814 struct commit *item = prio_queue_peek(q);
3815 struct commit_list *p = *list;
3817 if (p && p->item->date >= item->date)
3818 list = &p->next;
3819 else {
3820 p = commit_list_insert(item, list);
3821 list = &p->next; /* skip newly added item */
3822 prio_queue_get(q); /* pop item */
3827 static enum rewrite_result rewrite_one(struct rev_info *revs, struct commit **pp)
3829 struct prio_queue queue = { compare_commits_by_commit_date };
3830 enum rewrite_result ret = rewrite_one_1(revs, pp, &queue);
3831 merge_queue_into_list(&queue, &revs->commits);
3832 clear_prio_queue(&queue);
3833 return ret;
3836 int rewrite_parents(struct rev_info *revs, struct commit *commit,
3837 rewrite_parent_fn_t rewrite_parent)
3839 struct commit_list **pp = &commit->parents;
3840 while (*pp) {
3841 struct commit_list *parent = *pp;
3842 switch (rewrite_parent(revs, &parent->item)) {
3843 case rewrite_one_ok:
3844 break;
3845 case rewrite_one_noparents:
3846 *pp = parent->next;
3847 continue;
3848 case rewrite_one_error:
3849 return -1;
3851 pp = &parent->next;
3853 remove_duplicate_parents(revs, commit);
3854 return 0;
3857 static int commit_match(struct commit *commit, struct rev_info *opt)
3859 int retval;
3860 const char *encoding;
3861 const char *message;
3862 struct strbuf buf = STRBUF_INIT;
3864 if (!opt->grep_filter.pattern_list && !opt->grep_filter.header_list)
3865 return 1;
3867 /* Prepend "fake" headers as needed */
3868 if (opt->grep_filter.use_reflog_filter) {
3869 strbuf_addstr(&buf, "reflog ");
3870 get_reflog_message(&buf, opt->reflog_info);
3871 strbuf_addch(&buf, '\n');
3875 * We grep in the user's output encoding, under the assumption that it
3876 * is the encoding they are most likely to write their grep pattern
3877 * for. In addition, it means we will match the "notes" encoding below,
3878 * so we will not end up with a buffer that has two different encodings
3879 * in it.
3881 encoding = get_log_output_encoding();
3882 message = logmsg_reencode(commit, NULL, encoding);
3884 /* Copy the commit to temporary if we are using "fake" headers */
3885 if (buf.len)
3886 strbuf_addstr(&buf, message);
3888 if (opt->grep_filter.header_list && opt->mailmap) {
3889 const char *commit_headers[] = { "author ", "committer ", NULL };
3891 if (!buf.len)
3892 strbuf_addstr(&buf, message);
3894 apply_mailmap_to_header(&buf, commit_headers, opt->mailmap);
3897 /* Append "fake" message parts as needed */
3898 if (opt->show_notes) {
3899 if (!buf.len)
3900 strbuf_addstr(&buf, message);
3901 format_display_notes(&commit->object.oid, &buf, encoding, 1);
3905 * Find either in the original commit message, or in the temporary.
3906 * Note that we cast away the constness of "message" here. It is
3907 * const because it may come from the cached commit buffer. That's OK,
3908 * because we know that it is modifiable heap memory, and that while
3909 * grep_buffer may modify it for speed, it will restore any
3910 * changes before returning.
3912 if (buf.len)
3913 retval = grep_buffer(&opt->grep_filter, buf.buf, buf.len);
3914 else
3915 retval = grep_buffer(&opt->grep_filter,
3916 (char *)message, strlen(message));
3917 strbuf_release(&buf);
3918 unuse_commit_buffer(commit, message);
3919 return retval;
3922 static inline int want_ancestry(const struct rev_info *revs)
3924 return (revs->rewrite_parents || revs->children.name);
3928 * Return a timestamp to be used for --since/--until comparisons for this
3929 * commit, based on the revision options.
3931 static timestamp_t comparison_date(const struct rev_info *revs,
3932 struct commit *commit)
3934 return revs->reflog_info ?
3935 get_reflog_timestamp(revs->reflog_info) :
3936 commit->date;
3939 enum commit_action get_commit_action(struct rev_info *revs, struct commit *commit)
3941 if (commit->object.flags & SHOWN)
3942 return commit_ignore;
3943 if (revs->unpacked && has_object_pack(&commit->object.oid))
3944 return commit_ignore;
3945 if (revs->no_kept_objects) {
3946 if (has_object_kept_pack(&commit->object.oid,
3947 revs->keep_pack_cache_flags))
3948 return commit_ignore;
3950 if (commit->object.flags & UNINTERESTING)
3951 return commit_ignore;
3952 if (revs->line_level_traverse && !want_ancestry(revs)) {
3954 * In case of line-level log with parent rewriting
3955 * prepare_revision_walk() already took care of all line-level
3956 * log filtering, and there is nothing left to do here.
3958 * If parent rewriting was not requested, then this is the
3959 * place to perform the line-level log filtering. Notably,
3960 * this check, though expensive, must come before the other,
3961 * cheaper filtering conditions, because the tracked line
3962 * ranges must be adjusted even when the commit will end up
3963 * being ignored based on other conditions.
3965 if (!line_log_process_ranges_arbitrary_commit(revs, commit))
3966 return commit_ignore;
3968 if (revs->min_age != -1 &&
3969 comparison_date(revs, commit) > revs->min_age)
3970 return commit_ignore;
3971 if (revs->max_age_as_filter != -1 &&
3972 comparison_date(revs, commit) < revs->max_age_as_filter)
3973 return commit_ignore;
3974 if (revs->min_parents || (revs->max_parents >= 0)) {
3975 int n = commit_list_count(commit->parents);
3976 if ((n < revs->min_parents) ||
3977 ((revs->max_parents >= 0) && (n > revs->max_parents)))
3978 return commit_ignore;
3980 if (!commit_match(commit, revs))
3981 return commit_ignore;
3982 if (revs->prune && revs->dense) {
3983 /* Commit without changes? */
3984 if (commit->object.flags & TREESAME) {
3985 int n;
3986 struct commit_list *p;
3987 /* drop merges unless we want parenthood */
3988 if (!want_ancestry(revs))
3989 return commit_ignore;
3991 if (revs->show_pulls && (commit->object.flags & PULL_MERGE))
3992 return commit_show;
3995 * If we want ancestry, then need to keep any merges
3996 * between relevant commits to tie together topology.
3997 * For consistency with TREESAME and simplification
3998 * use "relevant" here rather than just INTERESTING,
3999 * to treat bottom commit(s) as part of the topology.
4001 for (n = 0, p = commit->parents; p; p = p->next)
4002 if (relevant_commit(p->item))
4003 if (++n >= 2)
4004 return commit_show;
4005 return commit_ignore;
4008 return commit_show;
4011 define_commit_slab(saved_parents, struct commit_list *);
4013 #define EMPTY_PARENT_LIST ((struct commit_list *)-1)
4016 * You may only call save_parents() once per commit (this is checked
4017 * for non-root commits).
4019 static void save_parents(struct rev_info *revs, struct commit *commit)
4021 struct commit_list **pp;
4023 if (!revs->saved_parents_slab) {
4024 revs->saved_parents_slab = xmalloc(sizeof(struct saved_parents));
4025 init_saved_parents(revs->saved_parents_slab);
4028 pp = saved_parents_at(revs->saved_parents_slab, commit);
4031 * When walking with reflogs, we may visit the same commit
4032 * several times: once for each appearance in the reflog.
4034 * In this case, save_parents() will be called multiple times.
4035 * We want to keep only the first set of parents. We need to
4036 * store a sentinel value for an empty (i.e., NULL) parent
4037 * list to distinguish it from a not-yet-saved list, however.
4039 if (*pp)
4040 return;
4041 if (commit->parents)
4042 *pp = copy_commit_list(commit->parents);
4043 else
4044 *pp = EMPTY_PARENT_LIST;
4047 static void free_saved_parents(struct rev_info *revs)
4049 if (revs->saved_parents_slab)
4050 clear_saved_parents(revs->saved_parents_slab);
4053 struct commit_list *get_saved_parents(struct rev_info *revs, const struct commit *commit)
4055 struct commit_list *parents;
4057 if (!revs->saved_parents_slab)
4058 return commit->parents;
4060 parents = *saved_parents_at(revs->saved_parents_slab, commit);
4061 if (parents == EMPTY_PARENT_LIST)
4062 return NULL;
4063 return parents;
4066 enum commit_action simplify_commit(struct rev_info *revs, struct commit *commit)
4068 enum commit_action action = get_commit_action(revs, commit);
4070 if (action == commit_show &&
4071 revs->prune && revs->dense && want_ancestry(revs)) {
4073 * --full-diff on simplified parents is no good: it
4074 * will show spurious changes from the commits that
4075 * were elided. So we save the parents on the side
4076 * when --full-diff is in effect.
4078 if (revs->full_diff)
4079 save_parents(revs, commit);
4080 if (rewrite_parents(revs, commit, rewrite_one) < 0)
4081 return commit_error;
4083 return action;
4086 static void track_linear(struct rev_info *revs, struct commit *commit)
4088 if (revs->track_first_time) {
4089 revs->linear = 1;
4090 revs->track_first_time = 0;
4091 } else {
4092 struct commit_list *p;
4093 for (p = revs->previous_parents; p; p = p->next)
4094 if (p->item == NULL || /* first commit */
4095 oideq(&p->item->object.oid, &commit->object.oid))
4096 break;
4097 revs->linear = p != NULL;
4099 if (revs->reverse) {
4100 if (revs->linear)
4101 commit->object.flags |= TRACK_LINEAR;
4103 free_commit_list(revs->previous_parents);
4104 revs->previous_parents = copy_commit_list(commit->parents);
4107 static struct commit *get_revision_1(struct rev_info *revs)
4109 while (1) {
4110 struct commit *commit;
4112 if (revs->reflog_info)
4113 commit = next_reflog_entry(revs->reflog_info);
4114 else if (revs->topo_walk_info)
4115 commit = next_topo_commit(revs);
4116 else
4117 commit = pop_commit(&revs->commits);
4119 if (!commit)
4120 return NULL;
4122 if (revs->reflog_info)
4123 commit->object.flags &= ~(ADDED | SEEN | SHOWN);
4126 * If we haven't done the list limiting, we need to look at
4127 * the parents here. We also need to do the date-based limiting
4128 * that we'd otherwise have done in limit_list().
4130 if (!revs->limited) {
4131 if (revs->max_age != -1 &&
4132 comparison_date(revs, commit) < revs->max_age)
4133 continue;
4135 if (revs->reflog_info)
4136 try_to_simplify_commit(revs, commit);
4137 else if (revs->topo_walk_info)
4138 expand_topo_walk(revs, commit);
4139 else if (process_parents(revs, commit, &revs->commits, NULL) < 0) {
4140 if (!revs->ignore_missing_links)
4141 die("Failed to traverse parents of commit %s",
4142 oid_to_hex(&commit->object.oid));
4146 switch (simplify_commit(revs, commit)) {
4147 case commit_ignore:
4148 continue;
4149 case commit_error:
4150 die("Failed to simplify parents of commit %s",
4151 oid_to_hex(&commit->object.oid));
4152 default:
4153 if (revs->track_linear)
4154 track_linear(revs, commit);
4155 return commit;
4161 * Return true for entries that have not yet been shown. (This is an
4162 * object_array_each_func_t.)
4164 static int entry_unshown(struct object_array_entry *entry, void *cb_data UNUSED)
4166 return !(entry->item->flags & SHOWN);
4170 * If array is on the verge of a realloc, garbage-collect any entries
4171 * that have already been shown to try to free up some space.
4173 static void gc_boundary(struct object_array *array)
4175 if (array->nr == array->alloc)
4176 object_array_filter(array, entry_unshown, NULL);
4179 static void create_boundary_commit_list(struct rev_info *revs)
4181 unsigned i;
4182 struct commit *c;
4183 struct object_array *array = &revs->boundary_commits;
4184 struct object_array_entry *objects = array->objects;
4187 * If revs->commits is non-NULL at this point, an error occurred in
4188 * get_revision_1(). Ignore the error and continue printing the
4189 * boundary commits anyway. (This is what the code has always
4190 * done.)
4192 free_commit_list(revs->commits);
4193 revs->commits = NULL;
4196 * Put all of the actual boundary commits from revs->boundary_commits
4197 * into revs->commits
4199 for (i = 0; i < array->nr; i++) {
4200 c = (struct commit *)(objects[i].item);
4201 if (!c)
4202 continue;
4203 if (!(c->object.flags & CHILD_SHOWN))
4204 continue;
4205 if (c->object.flags & (SHOWN | BOUNDARY))
4206 continue;
4207 c->object.flags |= BOUNDARY;
4208 commit_list_insert(c, &revs->commits);
4212 * If revs->topo_order is set, sort the boundary commits
4213 * in topological order
4215 sort_in_topological_order(&revs->commits, revs->sort_order);
4218 static struct commit *get_revision_internal(struct rev_info *revs)
4220 struct commit *c = NULL;
4221 struct commit_list *l;
4223 if (revs->boundary == 2) {
4225 * All of the normal commits have already been returned,
4226 * and we are now returning boundary commits.
4227 * create_boundary_commit_list() has populated
4228 * revs->commits with the remaining commits to return.
4230 c = pop_commit(&revs->commits);
4231 if (c)
4232 c->object.flags |= SHOWN;
4233 return c;
4237 * If our max_count counter has reached zero, then we are done. We
4238 * don't simply return NULL because we still might need to show
4239 * boundary commits. But we want to avoid calling get_revision_1, which
4240 * might do a considerable amount of work finding the next commit only
4241 * for us to throw it away.
4243 * If it is non-zero, then either we don't have a max_count at all
4244 * (-1), or it is still counting, in which case we decrement.
4246 if (revs->max_count) {
4247 c = get_revision_1(revs);
4248 if (c) {
4249 while (revs->skip_count > 0) {
4250 revs->skip_count--;
4251 c = get_revision_1(revs);
4252 if (!c)
4253 break;
4257 if (revs->max_count > 0)
4258 revs->max_count--;
4261 if (c)
4262 c->object.flags |= SHOWN;
4264 if (!revs->boundary)
4265 return c;
4267 if (!c) {
4269 * get_revision_1() runs out the commits, and
4270 * we are done computing the boundaries.
4271 * switch to boundary commits output mode.
4273 revs->boundary = 2;
4276 * Update revs->commits to contain the list of
4277 * boundary commits.
4279 create_boundary_commit_list(revs);
4281 return get_revision_internal(revs);
4285 * boundary commits are the commits that are parents of the
4286 * ones we got from get_revision_1() but they themselves are
4287 * not returned from get_revision_1(). Before returning
4288 * 'c', we need to mark its parents that they could be boundaries.
4291 for (l = c->parents; l; l = l->next) {
4292 struct object *p;
4293 p = &(l->item->object);
4294 if (p->flags & (CHILD_SHOWN | SHOWN))
4295 continue;
4296 p->flags |= CHILD_SHOWN;
4297 gc_boundary(&revs->boundary_commits);
4298 add_object_array(p, NULL, &revs->boundary_commits);
4301 return c;
4304 struct commit *get_revision(struct rev_info *revs)
4306 struct commit *c;
4307 struct commit_list *reversed;
4309 if (revs->reverse) {
4310 reversed = NULL;
4311 while ((c = get_revision_internal(revs)))
4312 commit_list_insert(c, &reversed);
4313 revs->commits = reversed;
4314 revs->reverse = 0;
4315 revs->reverse_output_stage = 1;
4318 if (revs->reverse_output_stage) {
4319 c = pop_commit(&revs->commits);
4320 if (revs->track_linear)
4321 revs->linear = !!(c && c->object.flags & TRACK_LINEAR);
4322 return c;
4325 c = get_revision_internal(revs);
4326 if (c && revs->graph)
4327 graph_update(revs->graph, c);
4328 if (!c) {
4329 free_saved_parents(revs);
4330 free_commit_list(revs->previous_parents);
4331 revs->previous_parents = NULL;
4333 return c;
4336 const char *get_revision_mark(const struct rev_info *revs, const struct commit *commit)
4338 if (commit->object.flags & BOUNDARY)
4339 return "-";
4340 else if (commit->object.flags & UNINTERESTING)
4341 return "^";
4342 else if (commit->object.flags & PATCHSAME)
4343 return "=";
4344 else if (!revs || revs->left_right) {
4345 if (commit->object.flags & SYMMETRIC_LEFT)
4346 return "<";
4347 else
4348 return ">";
4349 } else if (revs->graph)
4350 return "*";
4351 else if (revs->cherry_mark)
4352 return "+";
4353 return "";
4356 void put_revision_mark(const struct rev_info *revs, const struct commit *commit)
4358 const char *mark = get_revision_mark(revs, commit);
4359 if (!strlen(mark))
4360 return;
4361 fputs(mark, stdout);
4362 putchar(' ');