1 #include "git-compat-util.h"
5 #include "object-store.h"
11 #include "diff-merges.h"
14 #include "repository.h"
17 #include "reflog-walk.h"
18 #include "patch-ids.h"
21 #include "string-list.h"
24 #include "commit-slab.h"
26 #include "cache-tree.h"
31 #include "commit-reach.h"
32 #include "commit-graph.h"
33 #include "prio-queue.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
++)
58 static void mark_blob_uninteresting(struct blob
*blob
)
62 if (blob
->object
.flags
& UNINTERESTING
)
64 blob
->object
.flags
|= UNINTERESTING
;
67 static void mark_tree_contents_uninteresting(struct repository
*r
,
70 struct tree_desc desc
;
71 struct name_entry entry
;
73 if (parse_tree_gently(tree
, 1) < 0)
76 init_tree_desc(&desc
, tree
->buffer
, tree
->size
);
77 while (tree_entry(&desc
, &entry
)) {
78 switch (object_type(entry
.mode
)) {
80 mark_tree_uninteresting(r
, lookup_tree(r
, &entry
.oid
));
83 mark_blob_uninteresting(lookup_blob(r
, &entry
.oid
));
86 /* Subproject commit - not in this repository */
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
)
106 if (obj
->flags
& UNINTERESTING
)
108 obj
->flags
|= UNINTERESTING
;
109 mark_tree_contents_uninteresting(r
, tree
);
112 struct path_and_oids_entry
{
113 struct hashmap_entry ent
;
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
);
141 hashmap_clear_and_free(map
, struct path_and_oids_entry
, ent
);
144 static void paths_and_oids_insert(struct hashmap
*map
,
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
);
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
,
174 struct tree_desc desc
;
175 struct name_entry entry
;
180 if (parse_tree_gently(tree
, 1) < 0)
183 init_tree_desc(&desc
, tree
->buffer
, tree
->size
);
184 while (tree_entry(&desc
, &entry
)) {
185 switch (object_type(entry
.mode
)) {
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
);
192 child
->object
.flags
|= UNINTERESTING
;
196 if (tree
->object
.flags
& UNINTERESTING
) {
197 struct blob
*child
= lookup_blob(r
, &entry
.oid
);
199 child
->object
.flags
|= UNINTERESTING
;
203 /* Subproject commit - not in this repository */
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
);
229 if (tree
->object
.flags
& UNINTERESTING
)
230 has_uninteresting
= 1;
235 /* Do not walk unless we have both types of trees. */
236 if (!has_uninteresting
|| !has_interesting
)
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
;
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
)
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
)
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
)
309 while (pending
.nr
> 0)
310 mark_one_parent_uninteresting(revs
, commit_stack_pop(&pending
),
313 commit_stack_clear(&pending
);
316 static void add_pending_object_with_path(struct rev_info
*revs
,
318 const char *name
, unsigned mode
,
321 struct interpret_branch_name_options options
= { 0 };
324 if (revs
->no_walk
&& (obj
->flags
& UNINTERESTING
))
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
,
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
;
359 if (get_oid("HEAD", &oid
))
361 obj
= parse_object(revs
->repo
, &oid
);
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
,
371 struct object
*object
;
373 object
= parse_object_with_flags(revs
->repo
, oid
,
374 revs
->verify_objects
? 0 :
375 PARSE_OBJECT_SKIP_HASH_CHECK
);
378 if (revs
->ignore_missing
)
380 if (revs
->exclude_promisor_objects
&& is_promisor_object(oid
))
382 die("bad object %s", name
);
384 object
->flags
|= flags
;
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
));
413 if (revs
->ignore_missing_links
|| (flags
& UNINTERESTING
))
415 if (revs
->exclude_promisor_objects
&&
416 is_promisor_object(&tag
->tagged
->oid
))
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.
431 * Commit object? Just return it, we'll do all the complex
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
))
446 char **slot
= revision_sources_at(revs
->sources
, commit
);
449 *slot
= xstrdup(name
);
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
)
462 if (flags
& UNINTERESTING
) {
463 mark_tree_contents_uninteresting(revs
->repo
, tree
);
466 add_pending_object_with_path(revs
, object
, name
, mode
, path
);
471 * Blob object? You know the drill by now..
473 if (object
->type
== OBJ_BLOB
) {
474 if (!revs
->blob_objects
)
476 if (flags
& UNINTERESTING
)
478 add_pending_object_with_path(revs
, object
, name
, mode
, path
);
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
))
496 struct commit
*commit
= list
->item
;
498 if (commit
->object
.flags
& UNINTERESTING
)
501 *interesting_cache
= commit
;
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
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
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
;
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
)
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
570 struct commit
*commit
= list
->item
;
572 if (relevant_commit(commit
)) {
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
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
,
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
);
652 trace2_data_json("bloom", the_repository
, "statistics", &jw
);
657 static int forbid_bloom_filters(struct pathspec
*spec
)
659 if (spec
->has_wildcard
)
663 if (spec
->magic
& ~PATHSPEC_LITERAL
)
665 if (spec
->nr
&& (spec
->items
[0].magic
& ~PATHSPEC_LITERAL
))
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
;
677 int path_component_nr
= 1;
682 if (forbid_bloom_filters(&revs
->prune_data
))
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
)
691 if (!revs
->pruning
.pathspec
.nr
)
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);
705 revs
->bloom_filter_settings
= NULL
;
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.
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;
732 fill_bloom_key(path
, p
- path
,
733 &revs
->bloom_keys
[path_component_nr
++],
734 revs
->bloom_filter_settings
);
738 if (trace2_is_enabled() && !bloom_filter_atexit_registered
) {
739 atexit(trace2_bloom_filter_statistics_atexit
);
740 bloom_filter_atexit_registered
= 1;
746 static int check_maybe_different_in_bloom_filter(struct rev_info
*revs
,
747 struct commit
*commit
)
749 struct bloom_filter
*filter
;
752 if (!revs
->repo
->objects
->commit_graph
)
755 if (commit_graph_generation(commit
) == GENERATION_NUMBER_INFINITY
)
758 filter
= get_bloom_filter(revs
->repo
, commit
);
761 count_bloom_filter_not_present
++;
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
);
772 count_bloom_filter_maybe
++;
774 count_bloom_filter_definitely_not
++;
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
);
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
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
);
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
);
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
);
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
));
851 add_decoration(&revs
->treesame
, &commit
->object
, 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
;
867 if (!commit
->parents
) {
869 * Have just removed the only parent from a non-merge.
870 * Different handling, as we lack decoration.
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
;
878 commit
->object
.flags
&= ~TREESAME
;
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
;
902 commit
->object
.flags
&= ~TREESAME
;
903 free(add_decoration(&revs
->treesame
, &commit
->object
, NULL
));
909 static unsigned update_treesame(struct rev_info
*revs
, struct commit
*commit
)
911 if (commit
->parents
&& commit
->parents
->next
) {
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
);
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
];
928 irrelevant_change
|= !st
->treesame
[n
];
930 if (relevant_parents
? relevant_change
: irrelevant_change
)
931 commit
->object
.flags
&= ~TREESAME
;
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
967 if (!get_commit_tree(commit
))
970 if (!commit
->parents
) {
971 if (rev_same_tree_as_empty(revs
, commit
))
972 commit
->object
.flags
|= TREESAME
;
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
)
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
))
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
)
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
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
)) {
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.
1030 ts
->treesame
[nth_parent
] = 1;
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
1045 if (!revs
->show_pulls
|| !nth_parent
)
1046 commit
->object
.flags
|= TREESAME
;
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
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
));
1069 case REV_TREE_DIFFERENT
:
1070 if (relevant_commit(p
))
1071 relevant_change
= 1;
1073 irrelevant_change
= 1;
1076 commit
->object
.flags
|= PULL_MERGE
;
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
)
1105 commit
->object
.flags
|= ADDED
;
1107 if (revs
->include_check
&&
1108 !revs
->include_check(commit
, revs
->include_check_data
))
1112 * If the commit is uninteresting, don't try to
1113 * prune parents - we want the maximal uninteresting
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
) {
1125 struct commit
*p
= parent
->item
;
1126 parent
= parent
->next
;
1128 p
->object
.flags
|= UNINTERESTING
;
1129 if (repo_parse_commit_gently(revs
->repo
, p
, 1) < 0)
1132 mark_parents_uninteresting(revs
, p
);
1133 if (p
->object
.flags
& SEEN
)
1135 p
->object
.flags
|= (SEEN
| NOT_USER_GIVEN
);
1137 commit_list_insert_by_date(p
, list
);
1139 prio_queue_put(queue
, p
);
1140 if (revs
->exclude_first_parent_only
)
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
);
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
)
1171 if (revs
->sources
) {
1172 char **slot
= revision_sources_at(revs
->sources
, p
);
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
);
1181 commit_list_insert_by_date(p
, list
);
1183 prio_queue_put(queue
, p
);
1185 if (revs
->first_parent_only
)
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;
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
)
1211 if (!left_count
|| !right_count
)
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
)
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
))
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
)
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
))
1254 * Have we seen the same patch id?
1256 id
= patch_id_iter_first(commit
, &ids
);
1260 commit
->object
.flags
|= cherry_flag
;
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.. */
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..
1282 * Does the destination list contain entries with a date
1283 * before the source list? Definitely _not_ done.
1285 if (date
<= src
->item
->date
)
1289 * Does the source list still have interesting commits in
1290 * it? Definitely not done..
1292 if (!everybody_uninteresting(src
, interesting_cache
))
1295 /* Ok, we're closing in.. */
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
;
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.
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
))
1344 for (parents
= c
->parents
;
1346 parents
= parents
->next
) {
1347 if (!(parents
->item
->object
.flags
& TMP_MARK
))
1349 c
->object
.flags
|= TMP_MARK
;
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
))
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
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
)
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)
1443 if (obj
->flags
& UNINTERESTING
) {
1444 mark_parents_uninteresting(revs
, commit
);
1445 slop
= still_interesting(original_list
, date
, slop
, &interesting_cache
);
1450 if (revs
->min_age
!= -1 && (commit
->date
> revs
->min_age
) &&
1451 !revs
->line_level_traverse
)
1453 if (revs
->max_age_as_filter
!= -1 &&
1454 (commit
->date
< revs
->max_age_as_filter
) && !revs
->line_level_traverse
)
1456 date
= commit
->date
;
1457 p
= &commit_list_insert(commit
, p
)->next
;
1459 show
= show_early_output
;
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
))
1485 update_treesame(revs
, c
);
1489 free_commit_list(original_list
);
1490 revs
->commits
= newlist
;
1495 * Add an entry to refs->cmdline with the specified information.
1498 static void add_rev_cmdline(struct rev_info
*revs
,
1499 struct object
*item
,
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
;
1515 static void add_rev_cmdline_list(struct rev_info
*revs
,
1516 struct commit_list
*commit_list
,
1520 while (commit_list
) {
1521 struct object
*object
= &commit_list
->item
->object
;
1522 add_rev_cmdline(revs
, object
, oid_to_hex(&object
->oid
),
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))
1538 if (ref_is_hidden(stripped_path
, path
, &exclusions
->hidden_refs
))
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
, "fetch") && strcmp(section
, "receive") &&
1580 strcmp(section
, "uploadpack"))
1581 die(_("unsupported section for hidden refs: %s"), section
);
1583 if (exclusions
->hidden_refs_configured
)
1584 die(_("--exclude-hidden= passed more than once"));
1586 cb
.exclusions
= exclusions
;
1587 cb
.section
= section
;
1589 git_config(hide_refs_config
, &cb
);
1592 struct all_refs_cb
{
1594 int warned_bad_reflog
;
1595 struct rev_info
*all_revs
;
1596 const char *name_for_errormsg
;
1597 struct worktree
*wt
;
1600 static int handle_one_ref(const char *path
, const struct object_id
*oid
,
1604 struct all_refs_cb
*cb
= cb_data
;
1605 struct object
*object
;
1607 if (ref_excluded(&cb
->all_revs
->ref_excludes
, path
))
1610 object
= get_reference(cb
->all_revs
, path
, oid
, cb
->all_flags
);
1611 add_rev_cmdline(cb
->all_revs
, object
, path
, REV_CMD_REF
, cb
->all_flags
);
1612 add_pending_object(cb
->all_revs
, object
, path
);
1616 static void init_all_refs_cb(struct all_refs_cb
*cb
, struct rev_info
*revs
,
1619 cb
->all_revs
= revs
;
1620 cb
->all_flags
= flags
;
1621 revs
->rev_input_given
= 1;
1625 static void handle_refs(struct ref_store
*refs
,
1626 struct rev_info
*revs
, unsigned flags
,
1627 int (*for_each
)(struct ref_store
*, each_ref_fn
, void *))
1629 struct all_refs_cb cb
;
1632 /* this could happen with uninitialized submodules */
1636 init_all_refs_cb(&cb
, revs
, flags
);
1637 for_each(refs
, handle_one_ref
, &cb
);
1640 static void handle_one_reflog_commit(struct object_id
*oid
, void *cb_data
)
1642 struct all_refs_cb
*cb
= cb_data
;
1643 if (!is_null_oid(oid
)) {
1644 struct object
*o
= parse_object(cb
->all_revs
->repo
, oid
);
1646 o
->flags
|= cb
->all_flags
;
1647 /* ??? CMDLINEFLAGS ??? */
1648 add_pending_object(cb
->all_revs
, o
, "");
1650 else if (!cb
->warned_bad_reflog
) {
1651 warning("reflog of '%s' references pruned commits",
1652 cb
->name_for_errormsg
);
1653 cb
->warned_bad_reflog
= 1;
1658 static int handle_one_reflog_ent(struct object_id
*ooid
, struct object_id
*noid
,
1659 const char *email UNUSED
,
1660 timestamp_t timestamp UNUSED
,
1662 const char *message UNUSED
,
1665 handle_one_reflog_commit(ooid
, cb_data
);
1666 handle_one_reflog_commit(noid
, cb_data
);
1670 static int handle_one_reflog(const char *refname_in_wt
,
1671 const struct object_id
*oid UNUSED
,
1672 int flag UNUSED
, void *cb_data
)
1674 struct all_refs_cb
*cb
= cb_data
;
1675 struct strbuf refname
= STRBUF_INIT
;
1677 cb
->warned_bad_reflog
= 0;
1678 strbuf_worktree_ref(cb
->wt
, &refname
, refname_in_wt
);
1679 cb
->name_for_errormsg
= refname
.buf
;
1680 refs_for_each_reflog_ent(get_main_ref_store(the_repository
),
1682 handle_one_reflog_ent
, cb_data
);
1683 strbuf_release(&refname
);
1687 static void add_other_reflogs_to_pending(struct all_refs_cb
*cb
)
1689 struct worktree
**worktrees
, **p
;
1691 worktrees
= get_worktrees();
1692 for (p
= worktrees
; *p
; p
++) {
1693 struct worktree
*wt
= *p
;
1699 refs_for_each_reflog(get_worktree_ref_store(wt
),
1703 free_worktrees(worktrees
);
1706 void add_reflogs_to_pending(struct rev_info
*revs
, unsigned flags
)
1708 struct all_refs_cb cb
;
1711 cb
.all_flags
= flags
;
1713 for_each_reflog(handle_one_reflog
, &cb
);
1715 if (!revs
->single_worktree
)
1716 add_other_reflogs_to_pending(&cb
);
1719 static void add_cache_tree(struct cache_tree
*it
, struct rev_info
*revs
,
1720 struct strbuf
*path
, unsigned int flags
)
1722 size_t baselen
= path
->len
;
1725 if (it
->entry_count
>= 0) {
1726 struct tree
*tree
= lookup_tree(revs
->repo
, &it
->oid
);
1727 tree
->object
.flags
|= flags
;
1728 add_pending_object_with_path(revs
, &tree
->object
, "",
1732 for (i
= 0; i
< it
->subtree_nr
; i
++) {
1733 struct cache_tree_sub
*sub
= it
->down
[i
];
1734 strbuf_addf(path
, "%s%s", baselen
? "/" : "", sub
->name
);
1735 add_cache_tree(sub
->cache_tree
, revs
, path
, flags
);
1736 strbuf_setlen(path
, baselen
);
1741 static void add_resolve_undo_to_pending(struct index_state
*istate
, struct rev_info
*revs
)
1743 struct string_list_item
*item
;
1744 struct string_list
*resolve_undo
= istate
->resolve_undo
;
1749 for_each_string_list_item(item
, resolve_undo
) {
1750 const char *path
= item
->string
;
1751 struct resolve_undo_info
*ru
= item
->util
;
1756 for (i
= 0; i
< 3; i
++) {
1759 if (!ru
->mode
[i
] || !S_ISREG(ru
->mode
[i
]))
1762 blob
= lookup_blob(revs
->repo
, &ru
->oid
[i
]);
1764 warning(_("resolve-undo records `%s` which is missing"),
1765 oid_to_hex(&ru
->oid
[i
]));
1768 add_pending_object_with_path(revs
, &blob
->object
, "",
1774 static void do_add_index_objects_to_pending(struct rev_info
*revs
,
1775 struct index_state
*istate
,
1780 /* TODO: audit for interaction with sparse-index. */
1781 ensure_full_index(istate
);
1782 for (i
= 0; i
< istate
->cache_nr
; i
++) {
1783 struct cache_entry
*ce
= istate
->cache
[i
];
1786 if (S_ISGITLINK(ce
->ce_mode
))
1789 blob
= lookup_blob(revs
->repo
, &ce
->oid
);
1791 die("unable to add index blob to traversal");
1792 blob
->object
.flags
|= flags
;
1793 add_pending_object_with_path(revs
, &blob
->object
, "",
1794 ce
->ce_mode
, ce
->name
);
1797 if (istate
->cache_tree
) {
1798 struct strbuf path
= STRBUF_INIT
;
1799 add_cache_tree(istate
->cache_tree
, revs
, &path
, flags
);
1800 strbuf_release(&path
);
1803 add_resolve_undo_to_pending(istate
, revs
);
1806 void add_index_objects_to_pending(struct rev_info
*revs
, unsigned int flags
)
1808 struct worktree
**worktrees
, **p
;
1810 repo_read_index(revs
->repo
);
1811 do_add_index_objects_to_pending(revs
, revs
->repo
->index
, flags
);
1813 if (revs
->single_worktree
)
1816 worktrees
= get_worktrees();
1817 for (p
= worktrees
; *p
; p
++) {
1818 struct worktree
*wt
= *p
;
1819 struct index_state istate
= INDEX_STATE_INIT(revs
->repo
);
1822 continue; /* current index already taken care of */
1824 if (read_index_from(&istate
,
1825 worktree_git_path(wt
, "index"),
1826 get_worktree_git_dir(wt
)) > 0)
1827 do_add_index_objects_to_pending(revs
, &istate
, flags
);
1828 discard_index(&istate
);
1830 free_worktrees(worktrees
);
1833 struct add_alternate_refs_data
{
1834 struct rev_info
*revs
;
1838 static void add_one_alternate_ref(const struct object_id
*oid
,
1841 const char *name
= ".alternate";
1842 struct add_alternate_refs_data
*data
= vdata
;
1845 obj
= get_reference(data
->revs
, name
, oid
, data
->flags
);
1846 add_rev_cmdline(data
->revs
, obj
, name
, REV_CMD_REV
, data
->flags
);
1847 add_pending_object(data
->revs
, obj
, name
);
1850 static void add_alternate_refs_to_pending(struct rev_info
*revs
,
1853 struct add_alternate_refs_data data
;
1856 for_each_alternate_ref(add_one_alternate_ref
, &data
);
1859 static int add_parents_only(struct rev_info
*revs
, const char *arg_
, int flags
,
1862 struct object_id oid
;
1864 struct commit
*commit
;
1865 struct commit_list
*parents
;
1867 const char *arg
= arg_
;
1870 flags
^= UNINTERESTING
| BOTTOM
;
1873 if (get_oid_committish(arg
, &oid
))
1876 it
= get_reference(revs
, arg
, &oid
, 0);
1877 if (!it
&& revs
->ignore_missing
)
1879 if (it
->type
!= OBJ_TAG
)
1881 if (!((struct tag
*)it
)->tagged
)
1883 oidcpy(&oid
, &((struct tag
*)it
)->tagged
->oid
);
1885 if (it
->type
!= OBJ_COMMIT
)
1887 commit
= (struct commit
*)it
;
1888 if (exclude_parent
&&
1889 exclude_parent
> commit_list_count(commit
->parents
))
1891 for (parents
= commit
->parents
, parent_number
= 1;
1893 parents
= parents
->next
, parent_number
++) {
1894 if (exclude_parent
&& parent_number
!= exclude_parent
)
1897 it
= &parents
->item
->object
;
1899 add_rev_cmdline(revs
, it
, arg_
, REV_CMD_PARENTS_ONLY
, flags
);
1900 add_pending_object(revs
, it
, arg
);
1905 void repo_init_revisions(struct repository
*r
,
1906 struct rev_info
*revs
,
1909 struct rev_info blank
= REV_INFO_INIT
;
1910 memcpy(revs
, &blank
, sizeof(*revs
));
1913 revs
->pruning
.repo
= r
;
1914 revs
->pruning
.add_remove
= file_add_remove
;
1915 revs
->pruning
.change
= file_change
;
1916 revs
->pruning
.change_fn_data
= revs
;
1917 revs
->prefix
= prefix
;
1919 grep_init(&revs
->grep_filter
, revs
->repo
);
1920 revs
->grep_filter
.status_only
= 1;
1922 repo_diff_setup(revs
->repo
, &revs
->diffopt
);
1923 if (prefix
&& !revs
->diffopt
.prefix
) {
1924 revs
->diffopt
.prefix
= prefix
;
1925 revs
->diffopt
.prefix_length
= strlen(prefix
);
1928 init_display_notes(&revs
->notes_opt
);
1929 list_objects_filter_init(&revs
->filter
);
1930 init_ref_exclusions(&revs
->ref_excludes
);
1933 static void add_pending_commit_list(struct rev_info
*revs
,
1934 struct commit_list
*commit_list
,
1937 while (commit_list
) {
1938 struct object
*object
= &commit_list
->item
->object
;
1939 object
->flags
|= flags
;
1940 add_pending_object(revs
, object
, oid_to_hex(&object
->oid
));
1941 commit_list
= commit_list
->next
;
1945 static void prepare_show_merge(struct rev_info
*revs
)
1947 struct commit_list
*bases
;
1948 struct commit
*head
, *other
;
1949 struct object_id oid
;
1950 const char **prune
= NULL
;
1951 int i
, prune_num
= 1; /* counting terminating NULL */
1952 struct index_state
*istate
= revs
->repo
->index
;
1954 if (get_oid("HEAD", &oid
))
1955 die("--merge without HEAD?");
1956 head
= lookup_commit_or_die(&oid
, "HEAD");
1957 if (get_oid("MERGE_HEAD", &oid
))
1958 die("--merge without MERGE_HEAD?");
1959 other
= lookup_commit_or_die(&oid
, "MERGE_HEAD");
1960 add_pending_object(revs
, &head
->object
, "HEAD");
1961 add_pending_object(revs
, &other
->object
, "MERGE_HEAD");
1962 bases
= get_merge_bases(head
, other
);
1963 add_rev_cmdline_list(revs
, bases
, REV_CMD_MERGE_BASE
, UNINTERESTING
| BOTTOM
);
1964 add_pending_commit_list(revs
, bases
, UNINTERESTING
| BOTTOM
);
1965 free_commit_list(bases
);
1966 head
->object
.flags
|= SYMMETRIC_LEFT
;
1968 if (!istate
->cache_nr
)
1969 repo_read_index(revs
->repo
);
1970 for (i
= 0; i
< istate
->cache_nr
; i
++) {
1971 const struct cache_entry
*ce
= istate
->cache
[i
];
1974 if (ce_path_match(istate
, ce
, &revs
->prune_data
, NULL
)) {
1976 REALLOC_ARRAY(prune
, prune_num
);
1977 prune
[prune_num
-2] = ce
->name
;
1978 prune
[prune_num
-1] = NULL
;
1980 while ((i
+1 < istate
->cache_nr
) &&
1981 ce_same_name(ce
, istate
->cache
[i
+1]))
1984 clear_pathspec(&revs
->prune_data
);
1985 parse_pathspec(&revs
->prune_data
, PATHSPEC_ALL_MAGIC
& ~PATHSPEC_LITERAL
,
1986 PATHSPEC_PREFER_FULL
| PATHSPEC_LITERAL_PATH
, "", prune
);
1990 static int dotdot_missing(const char *arg
, char *dotdot
,
1991 struct rev_info
*revs
, int symmetric
)
1993 if (revs
->ignore_missing
)
1995 /* de-munge so we report the full argument */
1998 ? "Invalid symmetric difference expression %s"
1999 : "Invalid revision range %s", arg
);
2002 static int handle_dotdot_1(const char *arg
, char *dotdot
,
2003 struct rev_info
*revs
, int flags
,
2004 int cant_be_filename
,
2005 struct object_context
*a_oc
,
2006 struct object_context
*b_oc
)
2008 const char *a_name
, *b_name
;
2009 struct object_id a_oid
, b_oid
;
2010 struct object
*a_obj
, *b_obj
;
2011 unsigned int a_flags
, b_flags
;
2013 unsigned int flags_exclude
= flags
^ (UNINTERESTING
| BOTTOM
);
2014 unsigned int oc_flags
= GET_OID_COMMITTISH
| GET_OID_RECORD_PATH
;
2020 b_name
= dotdot
+ 2;
2021 if (*b_name
== '.') {
2028 if (get_oid_with_context(revs
->repo
, a_name
, oc_flags
, &a_oid
, a_oc
) ||
2029 get_oid_with_context(revs
->repo
, b_name
, oc_flags
, &b_oid
, b_oc
))
2032 if (!cant_be_filename
) {
2034 verify_non_filename(revs
->prefix
, arg
);
2038 a_obj
= parse_object(revs
->repo
, &a_oid
);
2039 b_obj
= parse_object(revs
->repo
, &b_oid
);
2040 if (!a_obj
|| !b_obj
)
2041 return dotdot_missing(arg
, dotdot
, revs
, symmetric
);
2046 a_flags
= flags_exclude
;
2048 /* A...B -- find merge bases between the two */
2049 struct commit
*a
, *b
;
2050 struct commit_list
*exclude
;
2052 a
= lookup_commit_reference(revs
->repo
, &a_obj
->oid
);
2053 b
= lookup_commit_reference(revs
->repo
, &b_obj
->oid
);
2055 return dotdot_missing(arg
, dotdot
, revs
, symmetric
);
2057 exclude
= get_merge_bases(a
, b
);
2058 add_rev_cmdline_list(revs
, exclude
, REV_CMD_MERGE_BASE
,
2060 add_pending_commit_list(revs
, exclude
, flags_exclude
);
2061 free_commit_list(exclude
);
2064 a_flags
= flags
| SYMMETRIC_LEFT
;
2067 a_obj
->flags
|= a_flags
;
2068 b_obj
->flags
|= b_flags
;
2069 add_rev_cmdline(revs
, a_obj
, a_name
, REV_CMD_LEFT
, a_flags
);
2070 add_rev_cmdline(revs
, b_obj
, b_name
, REV_CMD_RIGHT
, b_flags
);
2071 add_pending_object_with_path(revs
, a_obj
, a_name
, a_oc
->mode
, a_oc
->path
);
2072 add_pending_object_with_path(revs
, b_obj
, b_name
, b_oc
->mode
, b_oc
->path
);
2076 static int handle_dotdot(const char *arg
,
2077 struct rev_info
*revs
, int flags
,
2078 int cant_be_filename
)
2080 struct object_context a_oc
, b_oc
;
2081 char *dotdot
= strstr(arg
, "..");
2087 memset(&a_oc
, 0, sizeof(a_oc
));
2088 memset(&b_oc
, 0, sizeof(b_oc
));
2091 ret
= handle_dotdot_1(arg
, dotdot
, revs
, flags
, cant_be_filename
,
2101 static int handle_revision_arg_1(const char *arg_
, struct rev_info
*revs
, int flags
, unsigned revarg_opt
)
2103 struct object_context oc
;
2105 struct object
*object
;
2106 struct object_id oid
;
2108 const char *arg
= arg_
;
2109 int cant_be_filename
= revarg_opt
& REVARG_CANNOT_BE_FILENAME
;
2110 unsigned get_sha1_flags
= GET_OID_RECORD_PATH
;
2112 flags
= flags
& UNINTERESTING
? flags
| BOTTOM
: flags
& ~BOTTOM
;
2114 if (!cant_be_filename
&& !strcmp(arg
, "..")) {
2116 * Just ".."? That is not a range but the
2117 * pathspec for the parent directory.
2122 if (!handle_dotdot(arg
, revs
, flags
, revarg_opt
))
2125 mark
= strstr(arg
, "^@");
2126 if (mark
&& !mark
[2]) {
2128 if (add_parents_only(revs
, arg
, flags
, 0))
2132 mark
= strstr(arg
, "^!");
2133 if (mark
&& !mark
[2]) {
2135 if (!add_parents_only(revs
, arg
, flags
^ (UNINTERESTING
| BOTTOM
), 0))
2138 mark
= strstr(arg
, "^-");
2140 int exclude_parent
= 1;
2143 if (strtol_i(mark
+ 2, 10, &exclude_parent
) ||
2149 if (!add_parents_only(revs
, arg
, flags
^ (UNINTERESTING
| BOTTOM
), exclude_parent
))
2155 local_flags
= UNINTERESTING
| BOTTOM
;
2159 if (revarg_opt
& REVARG_COMMITTISH
)
2160 get_sha1_flags
|= GET_OID_COMMITTISH
;
2162 if (get_oid_with_context(revs
->repo
, arg
, get_sha1_flags
, &oid
, &oc
))
2163 return revs
->ignore_missing
? 0 : -1;
2164 if (!cant_be_filename
)
2165 verify_non_filename(revs
->prefix
, arg
);
2166 object
= get_reference(revs
, arg
, &oid
, flags
^ local_flags
);
2168 return revs
->ignore_missing
? 0 : -1;
2169 add_rev_cmdline(revs
, object
, arg_
, REV_CMD_REV
, flags
^ local_flags
);
2170 add_pending_object_with_path(revs
, object
, arg
, oc
.mode
, oc
.path
);
2175 int handle_revision_arg(const char *arg
, struct rev_info
*revs
, int flags
, unsigned revarg_opt
)
2177 int ret
= handle_revision_arg_1(arg
, revs
, flags
, revarg_opt
);
2179 revs
->rev_input_given
= 1;
2183 static void read_pathspec_from_stdin(struct strbuf
*sb
,
2184 struct strvec
*prune
)
2186 while (strbuf_getline(sb
, stdin
) != EOF
)
2187 strvec_push(prune
, sb
->buf
);
2190 static void read_revisions_from_stdin(struct rev_info
*revs
,
2191 struct strvec
*prune
)
2194 int seen_dashdash
= 0;
2197 save_warning
= warn_on_object_refname_ambiguity
;
2198 warn_on_object_refname_ambiguity
= 0;
2200 strbuf_init(&sb
, 1000);
2201 while (strbuf_getline(&sb
, stdin
) != EOF
) {
2205 if (sb
.buf
[0] == '-') {
2206 if (len
== 2 && sb
.buf
[1] == '-') {
2210 die("options not supported in --stdin mode");
2212 if (handle_revision_arg(sb
.buf
, revs
, 0,
2213 REVARG_CANNOT_BE_FILENAME
))
2214 die("bad revision '%s'", sb
.buf
);
2217 read_pathspec_from_stdin(&sb
, prune
);
2219 strbuf_release(&sb
);
2220 warn_on_object_refname_ambiguity
= save_warning
;
2223 static void add_grep(struct rev_info
*revs
, const char *ptn
, enum grep_pat_token what
)
2225 append_grep_pattern(&revs
->grep_filter
, ptn
, "command line", 0, what
);
2228 static void add_header_grep(struct rev_info
*revs
, enum grep_header_field field
, const char *pattern
)
2230 append_header_grep_pattern(&revs
->grep_filter
, field
, pattern
);
2233 static void add_message_grep(struct rev_info
*revs
, const char *pattern
)
2235 add_grep(revs
, pattern
, GREP_PATTERN_BODY
);
2238 static int handle_revision_opt(struct rev_info
*revs
, int argc
, const char **argv
,
2239 int *unkc
, const char **unkv
,
2240 const struct setup_revision_opt
* opt
)
2242 const char *arg
= argv
[0];
2243 const char *optarg
= NULL
;
2245 const unsigned hexsz
= the_hash_algo
->hexsz
;
2247 /* pseudo revision arguments */
2248 if (!strcmp(arg
, "--all") || !strcmp(arg
, "--branches") ||
2249 !strcmp(arg
, "--tags") || !strcmp(arg
, "--remotes") ||
2250 !strcmp(arg
, "--reflog") || !strcmp(arg
, "--not") ||
2251 !strcmp(arg
, "--no-walk") || !strcmp(arg
, "--do-walk") ||
2252 !strcmp(arg
, "--bisect") || starts_with(arg
, "--glob=") ||
2253 !strcmp(arg
, "--indexed-objects") ||
2254 !strcmp(arg
, "--alternate-refs") ||
2255 starts_with(arg
, "--exclude=") || starts_with(arg
, "--exclude-hidden=") ||
2256 starts_with(arg
, "--branches=") || starts_with(arg
, "--tags=") ||
2257 starts_with(arg
, "--remotes=") || starts_with(arg
, "--no-walk="))
2259 unkv
[(*unkc
)++] = arg
;
2263 if ((argcount
= parse_long_opt("max-count", argv
, &optarg
))) {
2264 revs
->max_count
= atoi(optarg
);
2267 } else if ((argcount
= parse_long_opt("skip", argv
, &optarg
))) {
2268 revs
->skip_count
= atoi(optarg
);
2270 } else if ((*arg
== '-') && isdigit(arg
[1])) {
2271 /* accept -<digit>, like traditional "head" */
2272 if (strtol_i(arg
+ 1, 10, &revs
->max_count
) < 0 ||
2273 revs
->max_count
< 0)
2274 die("'%s': not a non-negative integer", arg
+ 1);
2276 } else if (!strcmp(arg
, "-n")) {
2278 return error("-n requires an argument");
2279 revs
->max_count
= atoi(argv
[1]);
2282 } else if (skip_prefix(arg
, "-n", &optarg
)) {
2283 revs
->max_count
= atoi(optarg
);
2285 } else if ((argcount
= parse_long_opt("max-age", argv
, &optarg
))) {
2286 revs
->max_age
= atoi(optarg
);
2288 } else if ((argcount
= parse_long_opt("since", argv
, &optarg
))) {
2289 revs
->max_age
= approxidate(optarg
);
2291 } else if ((argcount
= parse_long_opt("since-as-filter", argv
, &optarg
))) {
2292 revs
->max_age_as_filter
= approxidate(optarg
);
2294 } else if ((argcount
= parse_long_opt("after", argv
, &optarg
))) {
2295 revs
->max_age
= approxidate(optarg
);
2297 } else if ((argcount
= parse_long_opt("min-age", argv
, &optarg
))) {
2298 revs
->min_age
= atoi(optarg
);
2300 } else if ((argcount
= parse_long_opt("before", argv
, &optarg
))) {
2301 revs
->min_age
= approxidate(optarg
);
2303 } else if ((argcount
= parse_long_opt("until", argv
, &optarg
))) {
2304 revs
->min_age
= approxidate(optarg
);
2306 } else if (!strcmp(arg
, "--first-parent")) {
2307 revs
->first_parent_only
= 1;
2308 } else if (!strcmp(arg
, "--exclude-first-parent-only")) {
2309 revs
->exclude_first_parent_only
= 1;
2310 } else if (!strcmp(arg
, "--ancestry-path")) {
2311 revs
->ancestry_path
= 1;
2312 revs
->simplify_history
= 0;
2314 revs
->ancestry_path_implicit_bottoms
= 1;
2315 } else if (skip_prefix(arg
, "--ancestry-path=", &optarg
)) {
2317 struct object_id oid
;
2318 const char *msg
= _("could not get commit for ancestry-path argument %s");
2320 revs
->ancestry_path
= 1;
2321 revs
->simplify_history
= 0;
2324 if (repo_get_oid_committish(revs
->repo
, optarg
, &oid
))
2325 return error(msg
, optarg
);
2326 get_reference(revs
, optarg
, &oid
, ANCESTRY_PATH
);
2327 c
= lookup_commit_reference(revs
->repo
, &oid
);
2329 return error(msg
, optarg
);
2330 commit_list_insert(c
, &revs
->ancestry_path_bottoms
);
2331 } else if (!strcmp(arg
, "-g") || !strcmp(arg
, "--walk-reflogs")) {
2332 init_reflog_walk(&revs
->reflog_info
);
2333 } else if (!strcmp(arg
, "--default")) {
2335 return error("bad --default argument");
2336 revs
->def
= argv
[1];
2338 } else if (!strcmp(arg
, "--merge")) {
2339 revs
->show_merge
= 1;
2340 } else if (!strcmp(arg
, "--topo-order")) {
2341 revs
->sort_order
= REV_SORT_IN_GRAPH_ORDER
;
2342 revs
->topo_order
= 1;
2343 } else if (!strcmp(arg
, "--simplify-merges")) {
2344 revs
->simplify_merges
= 1;
2345 revs
->topo_order
= 1;
2346 revs
->rewrite_parents
= 1;
2347 revs
->simplify_history
= 0;
2349 } else if (!strcmp(arg
, "--simplify-by-decoration")) {
2350 revs
->simplify_merges
= 1;
2351 revs
->topo_order
= 1;
2352 revs
->rewrite_parents
= 1;
2353 revs
->simplify_history
= 0;
2354 revs
->simplify_by_decoration
= 1;
2357 } else if (!strcmp(arg
, "--date-order")) {
2358 revs
->sort_order
= REV_SORT_BY_COMMIT_DATE
;
2359 revs
->topo_order
= 1;
2360 } else if (!strcmp(arg
, "--author-date-order")) {
2361 revs
->sort_order
= REV_SORT_BY_AUTHOR_DATE
;
2362 revs
->topo_order
= 1;
2363 } else if (!strcmp(arg
, "--early-output")) {
2364 revs
->early_output
= 100;
2365 revs
->topo_order
= 1;
2366 } else if (skip_prefix(arg
, "--early-output=", &optarg
)) {
2367 if (strtoul_ui(optarg
, 10, &revs
->early_output
) < 0)
2368 die("'%s': not a non-negative integer", optarg
);
2369 revs
->topo_order
= 1;
2370 } else if (!strcmp(arg
, "--parents")) {
2371 revs
->rewrite_parents
= 1;
2372 revs
->print_parents
= 1;
2373 } else if (!strcmp(arg
, "--dense")) {
2375 } else if (!strcmp(arg
, "--sparse")) {
2377 } else if (!strcmp(arg
, "--in-commit-order")) {
2378 revs
->tree_blobs_in_commit_order
= 1;
2379 } else if (!strcmp(arg
, "--remove-empty")) {
2380 revs
->remove_empty_trees
= 1;
2381 } else if (!strcmp(arg
, "--merges")) {
2382 revs
->min_parents
= 2;
2383 } else if (!strcmp(arg
, "--no-merges")) {
2384 revs
->max_parents
= 1;
2385 } else if (skip_prefix(arg
, "--min-parents=", &optarg
)) {
2386 revs
->min_parents
= atoi(optarg
);
2387 } else if (!strcmp(arg
, "--no-min-parents")) {
2388 revs
->min_parents
= 0;
2389 } else if (skip_prefix(arg
, "--max-parents=", &optarg
)) {
2390 revs
->max_parents
= atoi(optarg
);
2391 } else if (!strcmp(arg
, "--no-max-parents")) {
2392 revs
->max_parents
= -1;
2393 } else if (!strcmp(arg
, "--boundary")) {
2395 } else if (!strcmp(arg
, "--left-right")) {
2396 revs
->left_right
= 1;
2397 } else if (!strcmp(arg
, "--left-only")) {
2398 if (revs
->right_only
)
2399 die("--left-only is incompatible with --right-only"
2401 revs
->left_only
= 1;
2402 } else if (!strcmp(arg
, "--right-only")) {
2403 if (revs
->left_only
)
2404 die(_("options '%s' and '%s' cannot be used together"), "--right-only", "--left-only");
2405 revs
->right_only
= 1;
2406 } else if (!strcmp(arg
, "--cherry")) {
2407 if (revs
->left_only
)
2408 die(_("options '%s' and '%s' cannot be used together"), "--cherry", "--left-only");
2409 revs
->cherry_mark
= 1;
2410 revs
->right_only
= 1;
2411 revs
->max_parents
= 1;
2413 } else if (!strcmp(arg
, "--count")) {
2415 } else if (!strcmp(arg
, "--cherry-mark")) {
2416 if (revs
->cherry_pick
)
2417 die(_("options '%s' and '%s' cannot be used together"), "--cherry-mark", "--cherry-pick");
2418 revs
->cherry_mark
= 1;
2419 revs
->limited
= 1; /* needs limit_list() */
2420 } else if (!strcmp(arg
, "--cherry-pick")) {
2421 if (revs
->cherry_mark
)
2422 die(_("options '%s' and '%s' cannot be used together"), "--cherry-pick", "--cherry-mark");
2423 revs
->cherry_pick
= 1;
2425 } else if (!strcmp(arg
, "--objects")) {
2426 revs
->tag_objects
= 1;
2427 revs
->tree_objects
= 1;
2428 revs
->blob_objects
= 1;
2429 } else if (!strcmp(arg
, "--objects-edge")) {
2430 revs
->tag_objects
= 1;
2431 revs
->tree_objects
= 1;
2432 revs
->blob_objects
= 1;
2433 revs
->edge_hint
= 1;
2434 } else if (!strcmp(arg
, "--objects-edge-aggressive")) {
2435 revs
->tag_objects
= 1;
2436 revs
->tree_objects
= 1;
2437 revs
->blob_objects
= 1;
2438 revs
->edge_hint
= 1;
2439 revs
->edge_hint_aggressive
= 1;
2440 } else if (!strcmp(arg
, "--verify-objects")) {
2441 revs
->tag_objects
= 1;
2442 revs
->tree_objects
= 1;
2443 revs
->blob_objects
= 1;
2444 revs
->verify_objects
= 1;
2445 disable_commit_graph(revs
->repo
);
2446 } else if (!strcmp(arg
, "--unpacked")) {
2448 } else if (starts_with(arg
, "--unpacked=")) {
2449 die(_("--unpacked=<packfile> no longer supported"));
2450 } else if (!strcmp(arg
, "--no-kept-objects")) {
2451 revs
->no_kept_objects
= 1;
2452 revs
->keep_pack_cache_flags
|= IN_CORE_KEEP_PACKS
;
2453 revs
->keep_pack_cache_flags
|= ON_DISK_KEEP_PACKS
;
2454 } else if (skip_prefix(arg
, "--no-kept-objects=", &optarg
)) {
2455 revs
->no_kept_objects
= 1;
2456 if (!strcmp(optarg
, "in-core"))
2457 revs
->keep_pack_cache_flags
|= IN_CORE_KEEP_PACKS
;
2458 if (!strcmp(optarg
, "on-disk"))
2459 revs
->keep_pack_cache_flags
|= ON_DISK_KEEP_PACKS
;
2460 } else if (!strcmp(arg
, "-r")) {
2462 revs
->diffopt
.flags
.recursive
= 1;
2463 } else if (!strcmp(arg
, "-t")) {
2465 revs
->diffopt
.flags
.recursive
= 1;
2466 revs
->diffopt
.flags
.tree_in_recursive
= 1;
2467 } else if ((argcount
= diff_merges_parse_opts(revs
, argv
))) {
2469 } else if (!strcmp(arg
, "-v")) {
2470 revs
->verbose_header
= 1;
2471 } else if (!strcmp(arg
, "--pretty")) {
2472 revs
->verbose_header
= 1;
2473 revs
->pretty_given
= 1;
2474 get_commit_format(NULL
, revs
);
2475 } else if (skip_prefix(arg
, "--pretty=", &optarg
) ||
2476 skip_prefix(arg
, "--format=", &optarg
)) {
2478 * Detached form ("--pretty X" as opposed to "--pretty=X")
2479 * not allowed, since the argument is optional.
2481 revs
->verbose_header
= 1;
2482 revs
->pretty_given
= 1;
2483 get_commit_format(optarg
, revs
);
2484 } else if (!strcmp(arg
, "--expand-tabs")) {
2485 revs
->expand_tabs_in_log
= 8;
2486 } else if (!strcmp(arg
, "--no-expand-tabs")) {
2487 revs
->expand_tabs_in_log
= 0;
2488 } else if (skip_prefix(arg
, "--expand-tabs=", &arg
)) {
2490 if (strtol_i(arg
, 10, &val
) < 0 || val
< 0)
2491 die("'%s': not a non-negative integer", arg
);
2492 revs
->expand_tabs_in_log
= val
;
2493 } else if (!strcmp(arg
, "--show-notes") || !strcmp(arg
, "--notes")) {
2494 enable_default_display_notes(&revs
->notes_opt
, &revs
->show_notes
);
2495 revs
->show_notes_given
= 1;
2496 } else if (!strcmp(arg
, "--show-signature")) {
2497 revs
->show_signature
= 1;
2498 } else if (!strcmp(arg
, "--no-show-signature")) {
2499 revs
->show_signature
= 0;
2500 } else if (!strcmp(arg
, "--show-linear-break")) {
2501 revs
->break_bar
= " ..........";
2502 revs
->track_linear
= 1;
2503 revs
->track_first_time
= 1;
2504 } else if (skip_prefix(arg
, "--show-linear-break=", &optarg
)) {
2505 revs
->break_bar
= xstrdup(optarg
);
2506 revs
->track_linear
= 1;
2507 revs
->track_first_time
= 1;
2508 } else if (skip_prefix(arg
, "--show-notes=", &optarg
) ||
2509 skip_prefix(arg
, "--notes=", &optarg
)) {
2510 if (starts_with(arg
, "--show-notes=") &&
2511 revs
->notes_opt
.use_default_notes
< 0)
2512 revs
->notes_opt
.use_default_notes
= 1;
2513 enable_ref_display_notes(&revs
->notes_opt
, &revs
->show_notes
, optarg
);
2514 revs
->show_notes_given
= 1;
2515 } else if (!strcmp(arg
, "--no-notes")) {
2516 disable_display_notes(&revs
->notes_opt
, &revs
->show_notes
);
2517 revs
->show_notes_given
= 1;
2518 } else if (!strcmp(arg
, "--standard-notes")) {
2519 revs
->show_notes_given
= 1;
2520 revs
->notes_opt
.use_default_notes
= 1;
2521 } else if (!strcmp(arg
, "--no-standard-notes")) {
2522 revs
->notes_opt
.use_default_notes
= 0;
2523 } else if (!strcmp(arg
, "--oneline")) {
2524 revs
->verbose_header
= 1;
2525 get_commit_format("oneline", revs
);
2526 revs
->pretty_given
= 1;
2527 revs
->abbrev_commit
= 1;
2528 } else if (!strcmp(arg
, "--graph")) {
2529 graph_clear(revs
->graph
);
2530 revs
->graph
= graph_init(revs
);
2531 } else if (!strcmp(arg
, "--no-graph")) {
2532 graph_clear(revs
->graph
);
2534 } else if (!strcmp(arg
, "--encode-email-headers")) {
2535 revs
->encode_email_headers
= 1;
2536 } else if (!strcmp(arg
, "--no-encode-email-headers")) {
2537 revs
->encode_email_headers
= 0;
2538 } else if (!strcmp(arg
, "--root")) {
2539 revs
->show_root_diff
= 1;
2540 } else if (!strcmp(arg
, "--no-commit-id")) {
2541 revs
->no_commit_id
= 1;
2542 } else if (!strcmp(arg
, "--always")) {
2543 revs
->always_show_header
= 1;
2544 } else if (!strcmp(arg
, "--no-abbrev")) {
2546 } else if (!strcmp(arg
, "--abbrev")) {
2547 revs
->abbrev
= DEFAULT_ABBREV
;
2548 } else if (skip_prefix(arg
, "--abbrev=", &optarg
)) {
2549 revs
->abbrev
= strtoul(optarg
, NULL
, 10);
2550 if (revs
->abbrev
< MINIMUM_ABBREV
)
2551 revs
->abbrev
= MINIMUM_ABBREV
;
2552 else if (revs
->abbrev
> hexsz
)
2553 revs
->abbrev
= hexsz
;
2554 } else if (!strcmp(arg
, "--abbrev-commit")) {
2555 revs
->abbrev_commit
= 1;
2556 revs
->abbrev_commit_given
= 1;
2557 } else if (!strcmp(arg
, "--no-abbrev-commit")) {
2558 revs
->abbrev_commit
= 0;
2559 } else if (!strcmp(arg
, "--full-diff")) {
2561 revs
->full_diff
= 1;
2562 } else if (!strcmp(arg
, "--show-pulls")) {
2563 revs
->show_pulls
= 1;
2564 } else if (!strcmp(arg
, "--full-history")) {
2565 revs
->simplify_history
= 0;
2566 } else if (!strcmp(arg
, "--relative-date")) {
2567 revs
->date_mode
.type
= DATE_RELATIVE
;
2568 revs
->date_mode_explicit
= 1;
2569 } else if ((argcount
= parse_long_opt("date", argv
, &optarg
))) {
2570 parse_date_format(optarg
, &revs
->date_mode
);
2571 revs
->date_mode_explicit
= 1;
2573 } else if (!strcmp(arg
, "--log-size")) {
2574 revs
->show_log_size
= 1;
2577 * Grepping the commit log
2579 else if ((argcount
= parse_long_opt("author", argv
, &optarg
))) {
2580 add_header_grep(revs
, GREP_HEADER_AUTHOR
, optarg
);
2582 } else if ((argcount
= parse_long_opt("committer", argv
, &optarg
))) {
2583 add_header_grep(revs
, GREP_HEADER_COMMITTER
, optarg
);
2585 } else if ((argcount
= parse_long_opt("grep-reflog", argv
, &optarg
))) {
2586 add_header_grep(revs
, GREP_HEADER_REFLOG
, optarg
);
2588 } else if ((argcount
= parse_long_opt("grep", argv
, &optarg
))) {
2589 add_message_grep(revs
, optarg
);
2591 } else if (!strcmp(arg
, "--basic-regexp")) {
2592 revs
->grep_filter
.pattern_type_option
= GREP_PATTERN_TYPE_BRE
;
2593 } else if (!strcmp(arg
, "--extended-regexp") || !strcmp(arg
, "-E")) {
2594 revs
->grep_filter
.pattern_type_option
= GREP_PATTERN_TYPE_ERE
;
2595 } else if (!strcmp(arg
, "--regexp-ignore-case") || !strcmp(arg
, "-i")) {
2596 revs
->grep_filter
.ignore_case
= 1;
2597 revs
->diffopt
.pickaxe_opts
|= DIFF_PICKAXE_IGNORE_CASE
;
2598 } else if (!strcmp(arg
, "--fixed-strings") || !strcmp(arg
, "-F")) {
2599 revs
->grep_filter
.pattern_type_option
= GREP_PATTERN_TYPE_FIXED
;
2600 } else if (!strcmp(arg
, "--perl-regexp") || !strcmp(arg
, "-P")) {
2601 revs
->grep_filter
.pattern_type_option
= GREP_PATTERN_TYPE_PCRE
;
2602 } else if (!strcmp(arg
, "--all-match")) {
2603 revs
->grep_filter
.all_match
= 1;
2604 } else if (!strcmp(arg
, "--invert-grep")) {
2605 revs
->grep_filter
.no_body_match
= 1;
2606 } else if ((argcount
= parse_long_opt("encoding", argv
, &optarg
))) {
2607 if (strcmp(optarg
, "none"))
2608 git_log_output_encoding
= xstrdup(optarg
);
2610 git_log_output_encoding
= "";
2612 } else if (!strcmp(arg
, "--reverse")) {
2614 } else if (!strcmp(arg
, "--children")) {
2615 revs
->children
.name
= "children";
2617 } else if (!strcmp(arg
, "--ignore-missing")) {
2618 revs
->ignore_missing
= 1;
2619 } else if (opt
&& opt
->allow_exclude_promisor_objects
&&
2620 !strcmp(arg
, "--exclude-promisor-objects")) {
2621 if (fetch_if_missing
)
2622 BUG("exclude_promisor_objects can only be used when fetch_if_missing is 0");
2623 revs
->exclude_promisor_objects
= 1;
2625 int opts
= diff_opt_parse(&revs
->diffopt
, argv
, argc
, revs
->prefix
);
2627 unkv
[(*unkc
)++] = arg
;
2634 void parse_revision_opt(struct rev_info
*revs
, struct parse_opt_ctx_t
*ctx
,
2635 const struct option
*options
,
2636 const char * const usagestr
[])
2638 int n
= handle_revision_opt(revs
, ctx
->argc
, ctx
->argv
,
2639 &ctx
->cpidx
, ctx
->out
, NULL
);
2641 error("unknown option `%s'", ctx
->argv
[0]);
2642 usage_with_options(usagestr
, options
);
2648 void revision_opts_finish(struct rev_info
*revs
)
2650 if (revs
->graph
&& revs
->track_linear
)
2651 die(_("options '%s' and '%s' cannot be used together"), "--show-linear-break", "--graph");
2654 revs
->topo_order
= 1;
2655 revs
->rewrite_parents
= 1;
2659 static int for_each_bisect_ref(struct ref_store
*refs
, each_ref_fn fn
,
2660 void *cb_data
, const char *term
)
2662 struct strbuf bisect_refs
= STRBUF_INIT
;
2664 strbuf_addf(&bisect_refs
, "refs/bisect/%s", term
);
2665 status
= refs_for_each_fullref_in(refs
, bisect_refs
.buf
, fn
, cb_data
);
2666 strbuf_release(&bisect_refs
);
2670 static int for_each_bad_bisect_ref(struct ref_store
*refs
, each_ref_fn fn
, void *cb_data
)
2672 return for_each_bisect_ref(refs
, fn
, cb_data
, term_bad
);
2675 static int for_each_good_bisect_ref(struct ref_store
*refs
, each_ref_fn fn
, void *cb_data
)
2677 return for_each_bisect_ref(refs
, fn
, cb_data
, term_good
);
2680 static int handle_revision_pseudo_opt(struct rev_info
*revs
,
2681 const char **argv
, int *flags
)
2683 const char *arg
= argv
[0];
2685 struct ref_store
*refs
;
2688 if (revs
->repo
!= the_repository
) {
2690 * We need some something like get_submodule_worktrees()
2691 * before we can go through all worktrees of a submodule,
2692 * .e.g with adding all HEADs from --all, which is not
2693 * supported right now, so stick to single worktree.
2695 if (!revs
->single_worktree
)
2696 BUG("--single-worktree cannot be used together with submodule");
2698 refs
= get_main_ref_store(revs
->repo
);
2703 * Commands like "git shortlog" will not accept the options below
2704 * unless parse_revision_opt queues them (as opposed to erroring
2707 * When implementing your new pseudo-option, remember to
2708 * register it in the list at the top of handle_revision_opt.
2710 if (!strcmp(arg
, "--all")) {
2711 handle_refs(refs
, revs
, *flags
, refs_for_each_ref
);
2712 handle_refs(refs
, revs
, *flags
, refs_head_ref
);
2713 if (!revs
->single_worktree
) {
2714 struct all_refs_cb cb
;
2716 init_all_refs_cb(&cb
, revs
, *flags
);
2717 other_head_refs(handle_one_ref
, &cb
);
2719 clear_ref_exclusions(&revs
->ref_excludes
);
2720 } else if (!strcmp(arg
, "--branches")) {
2721 if (revs
->ref_excludes
.hidden_refs_configured
)
2722 return error(_("--exclude-hidden cannot be used together with --branches"));
2723 handle_refs(refs
, revs
, *flags
, refs_for_each_branch_ref
);
2724 clear_ref_exclusions(&revs
->ref_excludes
);
2725 } else if (!strcmp(arg
, "--bisect")) {
2726 read_bisect_terms(&term_bad
, &term_good
);
2727 handle_refs(refs
, revs
, *flags
, for_each_bad_bisect_ref
);
2728 handle_refs(refs
, revs
, *flags
^ (UNINTERESTING
| BOTTOM
),
2729 for_each_good_bisect_ref
);
2731 } else if (!strcmp(arg
, "--tags")) {
2732 if (revs
->ref_excludes
.hidden_refs_configured
)
2733 return error(_("--exclude-hidden cannot be used together with --tags"));
2734 handle_refs(refs
, revs
, *flags
, refs_for_each_tag_ref
);
2735 clear_ref_exclusions(&revs
->ref_excludes
);
2736 } else if (!strcmp(arg
, "--remotes")) {
2737 if (revs
->ref_excludes
.hidden_refs_configured
)
2738 return error(_("--exclude-hidden cannot be used together with --remotes"));
2739 handle_refs(refs
, revs
, *flags
, refs_for_each_remote_ref
);
2740 clear_ref_exclusions(&revs
->ref_excludes
);
2741 } else if ((argcount
= parse_long_opt("glob", argv
, &optarg
))) {
2742 struct all_refs_cb cb
;
2743 init_all_refs_cb(&cb
, revs
, *flags
);
2744 for_each_glob_ref(handle_one_ref
, optarg
, &cb
);
2745 clear_ref_exclusions(&revs
->ref_excludes
);
2747 } else if ((argcount
= parse_long_opt("exclude", argv
, &optarg
))) {
2748 add_ref_exclusion(&revs
->ref_excludes
, optarg
);
2750 } else if ((argcount
= parse_long_opt("exclude-hidden", argv
, &optarg
))) {
2751 exclude_hidden_refs(&revs
->ref_excludes
, optarg
);
2753 } else if (skip_prefix(arg
, "--branches=", &optarg
)) {
2754 struct all_refs_cb cb
;
2755 if (revs
->ref_excludes
.hidden_refs_configured
)
2756 return error(_("--exclude-hidden cannot be used together with --branches"));
2757 init_all_refs_cb(&cb
, revs
, *flags
);
2758 for_each_glob_ref_in(handle_one_ref
, optarg
, "refs/heads/", &cb
);
2759 clear_ref_exclusions(&revs
->ref_excludes
);
2760 } else if (skip_prefix(arg
, "--tags=", &optarg
)) {
2761 struct all_refs_cb cb
;
2762 if (revs
->ref_excludes
.hidden_refs_configured
)
2763 return error(_("--exclude-hidden cannot be used together with --tags"));
2764 init_all_refs_cb(&cb
, revs
, *flags
);
2765 for_each_glob_ref_in(handle_one_ref
, optarg
, "refs/tags/", &cb
);
2766 clear_ref_exclusions(&revs
->ref_excludes
);
2767 } else if (skip_prefix(arg
, "--remotes=", &optarg
)) {
2768 struct all_refs_cb cb
;
2769 if (revs
->ref_excludes
.hidden_refs_configured
)
2770 return error(_("--exclude-hidden cannot be used together with --remotes"));
2771 init_all_refs_cb(&cb
, revs
, *flags
);
2772 for_each_glob_ref_in(handle_one_ref
, optarg
, "refs/remotes/", &cb
);
2773 clear_ref_exclusions(&revs
->ref_excludes
);
2774 } else if (!strcmp(arg
, "--reflog")) {
2775 add_reflogs_to_pending(revs
, *flags
);
2776 } else if (!strcmp(arg
, "--indexed-objects")) {
2777 add_index_objects_to_pending(revs
, *flags
);
2778 } else if (!strcmp(arg
, "--alternate-refs")) {
2779 add_alternate_refs_to_pending(revs
, *flags
);
2780 } else if (!strcmp(arg
, "--not")) {
2781 *flags
^= UNINTERESTING
| BOTTOM
;
2782 } else if (!strcmp(arg
, "--no-walk")) {
2784 } else if (skip_prefix(arg
, "--no-walk=", &optarg
)) {
2786 * Detached form ("--no-walk X" as opposed to "--no-walk=X")
2787 * not allowed, since the argument is optional.
2790 if (!strcmp(optarg
, "sorted"))
2791 revs
->unsorted_input
= 0;
2792 else if (!strcmp(optarg
, "unsorted"))
2793 revs
->unsorted_input
= 1;
2795 return error("invalid argument to --no-walk");
2796 } else if (!strcmp(arg
, "--do-walk")) {
2798 } else if (!strcmp(arg
, "--single-worktree")) {
2799 revs
->single_worktree
= 1;
2800 } else if (skip_prefix(arg
, ("--filter="), &arg
)) {
2801 parse_list_objects_filter(&revs
->filter
, arg
);
2802 } else if (!strcmp(arg
, ("--no-filter"))) {
2803 list_objects_filter_set_no_filter(&revs
->filter
);
2811 static void NORETURN
diagnose_missing_default(const char *def
)
2814 const char *refname
;
2816 refname
= resolve_ref_unsafe(def
, 0, NULL
, &flags
);
2817 if (!refname
|| !(flags
& REF_ISSYMREF
) || (flags
& REF_ISBROKEN
))
2818 die(_("your current branch appears to be broken"));
2820 skip_prefix(refname
, "refs/heads/", &refname
);
2821 die(_("your current branch '%s' does not have any commits yet"),
2826 * Parse revision information, filling in the "rev_info" structure,
2827 * and removing the used arguments from the argument list.
2829 * Returns the number of arguments left that weren't recognized
2830 * (which are also moved to the head of the argument list)
2832 int setup_revisions(int argc
, const char **argv
, struct rev_info
*revs
, struct setup_revision_opt
*opt
)
2834 int i
, flags
, left
, seen_dashdash
, revarg_opt
;
2835 struct strvec prune_data
= STRVEC_INIT
;
2836 int seen_end_of_options
= 0;
2838 /* First, search for "--" */
2839 if (opt
&& opt
->assume_dashdash
) {
2843 for (i
= 1; i
< argc
; i
++) {
2844 const char *arg
= argv
[i
];
2845 if (strcmp(arg
, "--"))
2847 if (opt
&& opt
->free_removed_argv_elements
)
2848 free((char *)argv
[i
]);
2852 strvec_pushv(&prune_data
, argv
+ i
+ 1);
2858 /* Second, deal with arguments and options */
2860 revarg_opt
= opt
? opt
->revarg_opt
: 0;
2862 revarg_opt
|= REVARG_CANNOT_BE_FILENAME
;
2863 for (left
= i
= 1; i
< argc
; i
++) {
2864 const char *arg
= argv
[i
];
2865 if (!seen_end_of_options
&& *arg
== '-') {
2868 opts
= handle_revision_pseudo_opt(
2876 if (!strcmp(arg
, "--stdin")) {
2877 if (revs
->disable_stdin
) {
2881 if (revs
->read_from_stdin
++)
2882 die("--stdin given twice?");
2883 read_revisions_from_stdin(revs
, &prune_data
);
2887 if (!strcmp(arg
, "--end-of-options")) {
2888 seen_end_of_options
= 1;
2892 opts
= handle_revision_opt(revs
, argc
- i
, argv
+ i
,
2904 if (handle_revision_arg(arg
, revs
, flags
, revarg_opt
)) {
2906 if (seen_dashdash
|| *arg
== '^')
2907 die("bad revision '%s'", arg
);
2909 /* If we didn't have a "--":
2910 * (1) all filenames must exist;
2911 * (2) all rev-args must not be interpretable
2912 * as a valid filename.
2913 * but the latter we have checked in the main loop.
2915 for (j
= i
; j
< argc
; j
++)
2916 verify_filename(revs
->prefix
, argv
[j
], j
== i
);
2918 strvec_pushv(&prune_data
, argv
+ i
);
2922 revision_opts_finish(revs
);
2924 if (prune_data
.nr
) {
2926 * If we need to introduce the magic "a lone ':' means no
2927 * pathspec whatsoever", here is the place to do so.
2929 * if (prune_data.nr == 1 && !strcmp(prune_data[0], ":")) {
2930 * prune_data.nr = 0;
2931 * prune_data.alloc = 0;
2932 * free(prune_data.path);
2933 * prune_data.path = NULL;
2935 * terminate prune_data.alloc with NULL and
2936 * call init_pathspec() to set revs->prune_data here.
2939 parse_pathspec(&revs
->prune_data
, 0, 0,
2940 revs
->prefix
, prune_data
.v
);
2942 strvec_clear(&prune_data
);
2945 revs
->def
= opt
? opt
->def
: NULL
;
2946 if (opt
&& opt
->tweak
)
2947 opt
->tweak(revs
, opt
);
2948 if (revs
->show_merge
)
2949 prepare_show_merge(revs
);
2950 if (revs
->def
&& !revs
->pending
.nr
&& !revs
->rev_input_given
) {
2951 struct object_id oid
;
2952 struct object
*object
;
2953 struct object_context oc
;
2954 if (get_oid_with_context(revs
->repo
, revs
->def
, 0, &oid
, &oc
))
2955 diagnose_missing_default(revs
->def
);
2956 object
= get_reference(revs
, revs
->def
, &oid
, 0);
2957 add_pending_object_with_mode(revs
, object
, revs
->def
, oc
.mode
);
2960 /* Did the user ask for any diff output? Run the diff! */
2961 if (revs
->diffopt
.output_format
& ~DIFF_FORMAT_NO_OUTPUT
)
2964 /* Pickaxe, diff-filter and rename following need diffs */
2965 if ((revs
->diffopt
.pickaxe_opts
& DIFF_PICKAXE_KINDS_MASK
) ||
2966 revs
->diffopt
.filter
||
2967 revs
->diffopt
.flags
.follow_renames
)
2970 if (revs
->diffopt
.objfind
)
2971 revs
->simplify_history
= 0;
2973 if (revs
->line_level_traverse
) {
2974 if (want_ancestry(revs
))
2976 revs
->topo_order
= 1;
2979 if (revs
->topo_order
&& !generation_numbers_enabled(the_repository
))
2982 if (revs
->prune_data
.nr
) {
2983 copy_pathspec(&revs
->pruning
.pathspec
, &revs
->prune_data
);
2984 /* Can't prune commits with rename following: the paths change.. */
2985 if (!revs
->diffopt
.flags
.follow_renames
)
2987 if (!revs
->full_diff
)
2988 copy_pathspec(&revs
->diffopt
.pathspec
,
2992 diff_merges_setup_revs(revs
);
2994 revs
->diffopt
.abbrev
= revs
->abbrev
;
2996 diff_setup_done(&revs
->diffopt
);
2998 if (!is_encoding_utf8(get_log_output_encoding()))
2999 revs
->grep_filter
.ignore_locale
= 1;
3000 compile_grep_patterns(&revs
->grep_filter
);
3002 if (revs
->reverse
&& revs
->reflog_info
)
3003 die(_("options '%s' and '%s' cannot be used together"), "--reverse", "--walk-reflogs");
3004 if (revs
->reflog_info
&& revs
->limited
)
3005 die("cannot combine --walk-reflogs with history-limiting options");
3006 if (revs
->rewrite_parents
&& revs
->children
.name
)
3007 die(_("options '%s' and '%s' cannot be used together"), "--parents", "--children");
3008 if (revs
->filter
.choice
&& !revs
->blob_objects
)
3009 die(_("object filtering requires --objects"));
3012 * Limitations on the graph functionality
3014 if (revs
->reverse
&& revs
->graph
)
3015 die(_("options '%s' and '%s' cannot be used together"), "--reverse", "--graph");
3017 if (revs
->reflog_info
&& revs
->graph
)
3018 die(_("options '%s' and '%s' cannot be used together"), "--walk-reflogs", "--graph");
3019 if (revs
->no_walk
&& revs
->graph
)
3020 die(_("options '%s' and '%s' cannot be used together"), "--no-walk", "--graph");
3021 if (!revs
->reflog_info
&& revs
->grep_filter
.use_reflog_filter
)
3022 die(_("the option '%s' requires '%s'"), "--grep-reflog", "--walk-reflogs");
3024 if (revs
->line_level_traverse
&&
3025 (revs
->diffopt
.output_format
& ~(DIFF_FORMAT_PATCH
| DIFF_FORMAT_NO_OUTPUT
)))
3026 die(_("-L does not yet support diff formats besides -p and -s"));
3028 if (revs
->expand_tabs_in_log
< 0)
3029 revs
->expand_tabs_in_log
= revs
->expand_tabs_in_log_default
;
3034 static void release_revisions_cmdline(struct rev_cmdline_info
*cmdline
)
3038 for (i
= 0; i
< cmdline
->nr
; i
++)
3039 free((char *)cmdline
->rev
[i
].name
);
3043 static void release_revisions_mailmap(struct string_list
*mailmap
)
3047 clear_mailmap(mailmap
);
3051 static void release_revisions_topo_walk_info(struct topo_walk_info
*info
);
3053 void release_revisions(struct rev_info
*revs
)
3055 free_commit_list(revs
->commits
);
3056 free_commit_list(revs
->ancestry_path_bottoms
);
3057 object_array_clear(&revs
->pending
);
3058 object_array_clear(&revs
->boundary_commits
);
3059 release_revisions_cmdline(&revs
->cmdline
);
3060 list_objects_filter_release(&revs
->filter
);
3061 clear_pathspec(&revs
->prune_data
);
3062 date_mode_release(&revs
->date_mode
);
3063 release_revisions_mailmap(revs
->mailmap
);
3064 free_grep_patterns(&revs
->grep_filter
);
3065 graph_clear(revs
->graph
);
3066 /* TODO (need to handle "no_free"): diff_free(&revs->diffopt) */
3067 diff_free(&revs
->pruning
);
3068 reflog_walk_info_release(revs
->reflog_info
);
3069 release_revisions_topo_walk_info(revs
->topo_walk_info
);
3072 static void add_child(struct rev_info
*revs
, struct commit
*parent
, struct commit
*child
)
3074 struct commit_list
*l
= xcalloc(1, sizeof(*l
));
3077 l
->next
= add_decoration(&revs
->children
, &parent
->object
, l
);
3080 static int remove_duplicate_parents(struct rev_info
*revs
, struct commit
*commit
)
3082 struct treesame_state
*ts
= lookup_decoration(&revs
->treesame
, &commit
->object
);
3083 struct commit_list
**pp
, *p
;
3084 int surviving_parents
;
3086 /* Examine existing parents while marking ones we have seen... */
3087 pp
= &commit
->parents
;
3088 surviving_parents
= 0;
3089 while ((p
= *pp
) != NULL
) {
3090 struct commit
*parent
= p
->item
;
3091 if (parent
->object
.flags
& TMP_MARK
) {
3094 compact_treesame(revs
, commit
, surviving_parents
);
3097 parent
->object
.flags
|= TMP_MARK
;
3098 surviving_parents
++;
3101 /* clear the temporary mark */
3102 for (p
= commit
->parents
; p
; p
= p
->next
) {
3103 p
->item
->object
.flags
&= ~TMP_MARK
;
3105 /* no update_treesame() - removing duplicates can't affect TREESAME */
3106 return surviving_parents
;
3109 struct merge_simplify_state
{
3110 struct commit
*simplified
;
3113 static struct merge_simplify_state
*locate_simplify_state(struct rev_info
*revs
, struct commit
*commit
)
3115 struct merge_simplify_state
*st
;
3117 st
= lookup_decoration(&revs
->merge_simplification
, &commit
->object
);
3119 CALLOC_ARRAY(st
, 1);
3120 add_decoration(&revs
->merge_simplification
, &commit
->object
, st
);
3125 static int mark_redundant_parents(struct commit
*commit
)
3127 struct commit_list
*h
= reduce_heads(commit
->parents
);
3128 int i
= 0, marked
= 0;
3129 struct commit_list
*po
, *pn
;
3131 /* Want these for sanity-checking only */
3132 int orig_cnt
= commit_list_count(commit
->parents
);
3133 int cnt
= commit_list_count(h
);
3136 * Not ready to remove items yet, just mark them for now, based
3137 * on the output of reduce_heads(). reduce_heads outputs the reduced
3138 * set in its original order, so this isn't too hard.
3140 po
= commit
->parents
;
3143 if (pn
&& po
->item
== pn
->item
) {
3147 po
->item
->object
.flags
|= TMP_MARK
;
3153 if (i
!= cnt
|| cnt
+marked
!= orig_cnt
)
3154 die("mark_redundant_parents %d %d %d %d", orig_cnt
, cnt
, i
, marked
);
3156 free_commit_list(h
);
3161 static int mark_treesame_root_parents(struct commit
*commit
)
3163 struct commit_list
*p
;
3166 for (p
= commit
->parents
; p
; p
= p
->next
) {
3167 struct commit
*parent
= p
->item
;
3168 if (!parent
->parents
&& (parent
->object
.flags
& TREESAME
)) {
3169 parent
->object
.flags
|= TMP_MARK
;
3178 * Awkward naming - this means one parent we are TREESAME to.
3179 * cf mark_treesame_root_parents: root parents that are TREESAME (to an
3180 * empty tree). Better name suggestions?
3182 static int leave_one_treesame_to_parent(struct rev_info
*revs
, struct commit
*commit
)
3184 struct treesame_state
*ts
= lookup_decoration(&revs
->treesame
, &commit
->object
);
3185 struct commit
*unmarked
= NULL
, *marked
= NULL
;
3186 struct commit_list
*p
;
3189 for (p
= commit
->parents
, n
= 0; p
; p
= p
->next
, n
++) {
3190 if (ts
->treesame
[n
]) {
3191 if (p
->item
->object
.flags
& TMP_MARK
) {
3204 * If we are TREESAME to a marked-for-deletion parent, but not to any
3205 * unmarked parents, unmark the first TREESAME parent. This is the
3206 * parent that the default simplify_history==1 scan would have followed,
3207 * and it doesn't make sense to omit that path when asking for a
3208 * simplified full history. Retaining it improves the chances of
3209 * understanding odd missed merges that took an old version of a file.
3213 * I--------*X A modified the file, but mainline merge X used
3214 * \ / "-s ours", so took the version from I. X is
3215 * `-*A--' TREESAME to I and !TREESAME to A.
3217 * Default log from X would produce "I". Without this check,
3218 * --full-history --simplify-merges would produce "I-A-X", showing
3219 * the merge commit X and that it changed A, but not making clear that
3220 * it had just taken the I version. With this check, the topology above
3223 * Note that it is possible that the simplification chooses a different
3224 * TREESAME parent from the default, in which case this test doesn't
3225 * activate, and we _do_ drop the default parent. Example:
3227 * I------X A modified the file, but it was reverted in B,
3228 * \ / meaning mainline merge X is TREESAME to both
3231 * Default log would produce "I" by following the first parent;
3232 * --full-history --simplify-merges will produce "I-A-B". But this is a
3233 * reasonable result - it presents a logical full history leading from
3234 * I to X, and X is not an important merge.
3236 if (!unmarked
&& marked
) {
3237 marked
->object
.flags
&= ~TMP_MARK
;
3244 static int remove_marked_parents(struct rev_info
*revs
, struct commit
*commit
)
3246 struct commit_list
**pp
, *p
;
3247 int nth_parent
, removed
= 0;
3249 pp
= &commit
->parents
;
3251 while ((p
= *pp
) != NULL
) {
3252 struct commit
*parent
= p
->item
;
3253 if (parent
->object
.flags
& TMP_MARK
) {
3254 parent
->object
.flags
&= ~TMP_MARK
;
3258 compact_treesame(revs
, commit
, nth_parent
);
3265 /* Removing parents can only increase TREESAMEness */
3266 if (removed
&& !(commit
->object
.flags
& TREESAME
))
3267 update_treesame(revs
, commit
);
3272 static struct commit_list
**simplify_one(struct rev_info
*revs
, struct commit
*commit
, struct commit_list
**tail
)
3274 struct commit_list
*p
;
3275 struct commit
*parent
;
3276 struct merge_simplify_state
*st
, *pst
;
3279 st
= locate_simplify_state(revs
, commit
);
3282 * Have we handled this one?
3288 * An UNINTERESTING commit simplifies to itself, so does a
3289 * root commit. We do not rewrite parents of such commit
3292 if ((commit
->object
.flags
& UNINTERESTING
) || !commit
->parents
) {
3293 st
->simplified
= commit
;
3298 * Do we know what commit all of our parents that matter
3299 * should be rewritten to? Otherwise we are not ready to
3300 * rewrite this one yet.
3302 for (cnt
= 0, p
= commit
->parents
; p
; p
= p
->next
) {
3303 pst
= locate_simplify_state(revs
, p
->item
);
3304 if (!pst
->simplified
) {
3305 tail
= &commit_list_insert(p
->item
, tail
)->next
;
3308 if (revs
->first_parent_only
)
3312 tail
= &commit_list_insert(commit
, tail
)->next
;
3317 * Rewrite our list of parents. Note that this cannot
3318 * affect our TREESAME flags in any way - a commit is
3319 * always TREESAME to its simplification.
3321 for (p
= commit
->parents
; p
; p
= p
->next
) {
3322 pst
= locate_simplify_state(revs
, p
->item
);
3323 p
->item
= pst
->simplified
;
3324 if (revs
->first_parent_only
)
3328 if (revs
->first_parent_only
)
3331 cnt
= remove_duplicate_parents(revs
, commit
);
3334 * It is possible that we are a merge and one side branch
3335 * does not have any commit that touches the given paths;
3336 * in such a case, the immediate parent from that branch
3337 * will be rewritten to be the merge base.
3339 * o----X X: the commit we are looking at;
3340 * / / o: a commit that touches the paths;
3343 * Further, a merge of an independent branch that doesn't
3344 * touch the path will reduce to a treesame root parent:
3346 * ----o----X X: the commit we are looking at;
3347 * / o: a commit that touches the paths;
3348 * r r: a root commit not touching the paths
3350 * Detect and simplify both cases.
3353 int marked
= mark_redundant_parents(commit
);
3354 marked
+= mark_treesame_root_parents(commit
);
3356 marked
-= leave_one_treesame_to_parent(revs
, commit
);
3358 cnt
= remove_marked_parents(revs
, commit
);
3362 * A commit simplifies to itself if it is a root, if it is
3363 * UNINTERESTING, if it touches the given paths, or if it is a
3364 * merge and its parents don't simplify to one relevant commit
3365 * (the first two cases are already handled at the beginning of
3368 * Otherwise, it simplifies to what its sole relevant parent
3372 (commit
->object
.flags
& UNINTERESTING
) ||
3373 !(commit
->object
.flags
& TREESAME
) ||
3374 (parent
= one_relevant_parent(revs
, commit
->parents
)) == NULL
||
3375 (revs
->show_pulls
&& (commit
->object
.flags
& PULL_MERGE
)))
3376 st
->simplified
= commit
;
3378 pst
= locate_simplify_state(revs
, parent
);
3379 st
->simplified
= pst
->simplified
;
3384 static void simplify_merges(struct rev_info
*revs
)
3386 struct commit_list
*list
, *next
;
3387 struct commit_list
*yet_to_do
, **tail
;
3388 struct commit
*commit
;
3393 /* feed the list reversed */
3395 for (list
= revs
->commits
; list
; list
= next
) {
3396 commit
= list
->item
;
3399 * Do not free(list) here yet; the original list
3400 * is used later in this function.
3402 commit_list_insert(commit
, &yet_to_do
);
3409 commit
= pop_commit(&list
);
3410 tail
= simplify_one(revs
, commit
, tail
);
3414 /* clean up the result, removing the simplified ones */
3415 list
= revs
->commits
;
3416 revs
->commits
= NULL
;
3417 tail
= &revs
->commits
;
3419 struct merge_simplify_state
*st
;
3421 commit
= pop_commit(&list
);
3422 st
= locate_simplify_state(revs
, commit
);
3423 if (st
->simplified
== commit
)
3424 tail
= &commit_list_insert(commit
, tail
)->next
;
3428 static void set_children(struct rev_info
*revs
)
3430 struct commit_list
*l
;
3431 for (l
= revs
->commits
; l
; l
= l
->next
) {
3432 struct commit
*commit
= l
->item
;
3433 struct commit_list
*p
;
3435 for (p
= commit
->parents
; p
; p
= p
->next
)
3436 add_child(revs
, p
->item
, commit
);
3440 void reset_revision_walk(void)
3442 clear_object_flags(SEEN
| ADDED
| SHOWN
| TOPO_WALK_EXPLORED
| TOPO_WALK_INDEGREE
);
3445 static int mark_uninteresting(const struct object_id
*oid
,
3446 struct packed_git
*pack UNUSED
,
3447 uint32_t pos UNUSED
,
3450 struct rev_info
*revs
= cb
;
3451 struct object
*o
= lookup_unknown_object(revs
->repo
, oid
);
3452 o
->flags
|= UNINTERESTING
| SEEN
;
3456 define_commit_slab(indegree_slab
, int);
3457 define_commit_slab(author_date_slab
, timestamp_t
);
3459 struct topo_walk_info
{
3460 timestamp_t min_generation
;
3461 struct prio_queue explore_queue
;
3462 struct prio_queue indegree_queue
;
3463 struct prio_queue topo_queue
;
3464 struct indegree_slab indegree
;
3465 struct author_date_slab author_date
;
3468 static int topo_walk_atexit_registered
;
3469 static unsigned int count_explore_walked
;
3470 static unsigned int count_indegree_walked
;
3471 static unsigned int count_topo_walked
;
3473 static void trace2_topo_walk_statistics_atexit(void)
3475 struct json_writer jw
= JSON_WRITER_INIT
;
3477 jw_object_begin(&jw
, 0);
3478 jw_object_intmax(&jw
, "count_explore_walked", count_explore_walked
);
3479 jw_object_intmax(&jw
, "count_indegree_walked", count_indegree_walked
);
3480 jw_object_intmax(&jw
, "count_topo_walked", count_topo_walked
);
3483 trace2_data_json("topo_walk", the_repository
, "statistics", &jw
);
3488 static inline void test_flag_and_insert(struct prio_queue
*q
, struct commit
*c
, int flag
)
3490 if (c
->object
.flags
& flag
)
3493 c
->object
.flags
|= flag
;
3494 prio_queue_put(q
, c
);
3497 static void explore_walk_step(struct rev_info
*revs
)
3499 struct topo_walk_info
*info
= revs
->topo_walk_info
;
3500 struct commit_list
*p
;
3501 struct commit
*c
= prio_queue_get(&info
->explore_queue
);
3506 if (repo_parse_commit_gently(revs
->repo
, c
, 1) < 0)
3509 count_explore_walked
++;
3511 if (revs
->sort_order
== REV_SORT_BY_AUTHOR_DATE
)
3512 record_author_date(&info
->author_date
, c
);
3514 if (revs
->max_age
!= -1 && (c
->date
< revs
->max_age
))
3515 c
->object
.flags
|= UNINTERESTING
;
3517 if (process_parents(revs
, c
, NULL
, NULL
) < 0)
3520 if (c
->object
.flags
& UNINTERESTING
)
3521 mark_parents_uninteresting(revs
, c
);
3523 for (p
= c
->parents
; p
; p
= p
->next
)
3524 test_flag_and_insert(&info
->explore_queue
, p
->item
, TOPO_WALK_EXPLORED
);
3527 static void explore_to_depth(struct rev_info
*revs
,
3528 timestamp_t gen_cutoff
)
3530 struct topo_walk_info
*info
= revs
->topo_walk_info
;
3532 while ((c
= prio_queue_peek(&info
->explore_queue
)) &&
3533 commit_graph_generation(c
) >= gen_cutoff
)
3534 explore_walk_step(revs
);
3537 static void indegree_walk_step(struct rev_info
*revs
)
3539 struct commit_list
*p
;
3540 struct topo_walk_info
*info
= revs
->topo_walk_info
;
3541 struct commit
*c
= prio_queue_get(&info
->indegree_queue
);
3546 if (repo_parse_commit_gently(revs
->repo
, c
, 1) < 0)
3549 count_indegree_walked
++;
3551 explore_to_depth(revs
, commit_graph_generation(c
));
3553 for (p
= c
->parents
; p
; p
= p
->next
) {
3554 struct commit
*parent
= p
->item
;
3555 int *pi
= indegree_slab_at(&info
->indegree
, parent
);
3557 if (repo_parse_commit_gently(revs
->repo
, parent
, 1) < 0)
3565 test_flag_and_insert(&info
->indegree_queue
, parent
, TOPO_WALK_INDEGREE
);
3567 if (revs
->first_parent_only
)
3572 static void compute_indegrees_to_depth(struct rev_info
*revs
,
3573 timestamp_t gen_cutoff
)
3575 struct topo_walk_info
*info
= revs
->topo_walk_info
;
3577 while ((c
= prio_queue_peek(&info
->indegree_queue
)) &&
3578 commit_graph_generation(c
) >= gen_cutoff
)
3579 indegree_walk_step(revs
);
3582 static void release_revisions_topo_walk_info(struct topo_walk_info
*info
)
3586 clear_prio_queue(&info
->explore_queue
);
3587 clear_prio_queue(&info
->indegree_queue
);
3588 clear_prio_queue(&info
->topo_queue
);
3589 clear_indegree_slab(&info
->indegree
);
3590 clear_author_date_slab(&info
->author_date
);
3594 static void reset_topo_walk(struct rev_info
*revs
)
3596 release_revisions_topo_walk_info(revs
->topo_walk_info
);
3597 revs
->topo_walk_info
= NULL
;
3600 static void init_topo_walk(struct rev_info
*revs
)
3602 struct topo_walk_info
*info
;
3603 struct commit_list
*list
;
3604 if (revs
->topo_walk_info
)
3605 reset_topo_walk(revs
);
3607 revs
->topo_walk_info
= xmalloc(sizeof(struct topo_walk_info
));
3608 info
= revs
->topo_walk_info
;
3609 memset(info
, 0, sizeof(struct topo_walk_info
));
3611 init_indegree_slab(&info
->indegree
);
3612 memset(&info
->explore_queue
, 0, sizeof(info
->explore_queue
));
3613 memset(&info
->indegree_queue
, 0, sizeof(info
->indegree_queue
));
3614 memset(&info
->topo_queue
, 0, sizeof(info
->topo_queue
));
3616 switch (revs
->sort_order
) {
3617 default: /* REV_SORT_IN_GRAPH_ORDER */
3618 info
->topo_queue
.compare
= NULL
;
3620 case REV_SORT_BY_COMMIT_DATE
:
3621 info
->topo_queue
.compare
= compare_commits_by_commit_date
;
3623 case REV_SORT_BY_AUTHOR_DATE
:
3624 init_author_date_slab(&info
->author_date
);
3625 info
->topo_queue
.compare
= compare_commits_by_author_date
;
3626 info
->topo_queue
.cb_data
= &info
->author_date
;
3630 info
->explore_queue
.compare
= compare_commits_by_gen_then_commit_date
;
3631 info
->indegree_queue
.compare
= compare_commits_by_gen_then_commit_date
;
3633 info
->min_generation
= GENERATION_NUMBER_INFINITY
;
3634 for (list
= revs
->commits
; list
; list
= list
->next
) {
3635 struct commit
*c
= list
->item
;
3636 timestamp_t generation
;
3638 if (repo_parse_commit_gently(revs
->repo
, c
, 1))
3641 test_flag_and_insert(&info
->explore_queue
, c
, TOPO_WALK_EXPLORED
);
3642 test_flag_and_insert(&info
->indegree_queue
, c
, TOPO_WALK_INDEGREE
);
3644 generation
= commit_graph_generation(c
);
3645 if (generation
< info
->min_generation
)
3646 info
->min_generation
= generation
;
3648 *(indegree_slab_at(&info
->indegree
, c
)) = 1;
3650 if (revs
->sort_order
== REV_SORT_BY_AUTHOR_DATE
)
3651 record_author_date(&info
->author_date
, c
);
3653 compute_indegrees_to_depth(revs
, info
->min_generation
);
3655 for (list
= revs
->commits
; list
; list
= list
->next
) {
3656 struct commit
*c
= list
->item
;
3658 if (*(indegree_slab_at(&info
->indegree
, c
)) == 1)
3659 prio_queue_put(&info
->topo_queue
, c
);
3663 * This is unfortunate; the initial tips need to be shown
3664 * in the order given from the revision traversal machinery.
3666 if (revs
->sort_order
== REV_SORT_IN_GRAPH_ORDER
)
3667 prio_queue_reverse(&info
->topo_queue
);
3669 if (trace2_is_enabled() && !topo_walk_atexit_registered
) {
3670 atexit(trace2_topo_walk_statistics_atexit
);
3671 topo_walk_atexit_registered
= 1;
3675 static struct commit
*next_topo_commit(struct rev_info
*revs
)
3678 struct topo_walk_info
*info
= revs
->topo_walk_info
;
3680 /* pop next off of topo_queue */
3681 c
= prio_queue_get(&info
->topo_queue
);
3684 *(indegree_slab_at(&info
->indegree
, c
)) = 0;
3689 static void expand_topo_walk(struct rev_info
*revs
, struct commit
*commit
)
3691 struct commit_list
*p
;
3692 struct topo_walk_info
*info
= revs
->topo_walk_info
;
3693 if (process_parents(revs
, commit
, NULL
, NULL
) < 0) {
3694 if (!revs
->ignore_missing_links
)
3695 die("Failed to traverse parents of commit %s",
3696 oid_to_hex(&commit
->object
.oid
));
3699 count_topo_walked
++;
3701 for (p
= commit
->parents
; p
; p
= p
->next
) {
3702 struct commit
*parent
= p
->item
;
3704 timestamp_t generation
;
3706 if (parent
->object
.flags
& UNINTERESTING
)
3709 if (repo_parse_commit_gently(revs
->repo
, parent
, 1) < 0)
3712 generation
= commit_graph_generation(parent
);
3713 if (generation
< info
->min_generation
) {
3714 info
->min_generation
= generation
;
3715 compute_indegrees_to_depth(revs
, info
->min_generation
);
3718 pi
= indegree_slab_at(&info
->indegree
, parent
);
3722 prio_queue_put(&info
->topo_queue
, parent
);
3724 if (revs
->first_parent_only
)
3729 int prepare_revision_walk(struct rev_info
*revs
)
3732 struct object_array old_pending
;
3733 struct commit_list
**next
= &revs
->commits
;
3735 memcpy(&old_pending
, &revs
->pending
, sizeof(old_pending
));
3736 revs
->pending
.nr
= 0;
3737 revs
->pending
.alloc
= 0;
3738 revs
->pending
.objects
= NULL
;
3739 for (i
= 0; i
< old_pending
.nr
; i
++) {
3740 struct object_array_entry
*e
= old_pending
.objects
+ i
;
3741 struct commit
*commit
= handle_commit(revs
, e
);
3743 if (!(commit
->object
.flags
& SEEN
)) {
3744 commit
->object
.flags
|= SEEN
;
3745 next
= commit_list_append(commit
, next
);
3749 object_array_clear(&old_pending
);
3751 /* Signal whether we need per-parent treesame decoration */
3752 if (revs
->simplify_merges
||
3753 (revs
->limited
&& limiting_can_increase_treesame(revs
)))
3754 revs
->treesame
.name
= "treesame";
3756 if (revs
->exclude_promisor_objects
) {
3757 for_each_packed_object(mark_uninteresting
, revs
,
3758 FOR_EACH_OBJECT_PROMISOR_ONLY
);
3761 if (!revs
->reflog_info
)
3762 prepare_to_use_bloom_filter(revs
);
3763 if (!revs
->unsorted_input
)
3764 commit_list_sort_by_date(&revs
->commits
);
3767 if (revs
->limited
) {
3768 if (limit_list(revs
) < 0)
3770 if (revs
->topo_order
)
3771 sort_in_topological_order(&revs
->commits
, revs
->sort_order
);
3772 } else if (revs
->topo_order
)
3773 init_topo_walk(revs
);
3774 if (revs
->line_level_traverse
&& want_ancestry(revs
))
3776 * At the moment we can only do line-level log with parent
3777 * rewriting by performing this expensive pre-filtering step.
3778 * If parent rewriting is not requested, then we rather
3779 * perform the line-level log filtering during the regular
3780 * history traversal.
3782 line_log_filter(revs
);
3783 if (revs
->simplify_merges
)
3784 simplify_merges(revs
);
3785 if (revs
->children
.name
)
3791 static enum rewrite_result
rewrite_one_1(struct rev_info
*revs
,
3793 struct prio_queue
*queue
)
3796 struct commit
*p
= *pp
;
3798 if (process_parents(revs
, p
, NULL
, queue
) < 0)
3799 return rewrite_one_error
;
3800 if (p
->object
.flags
& UNINTERESTING
)
3801 return rewrite_one_ok
;
3802 if (!(p
->object
.flags
& TREESAME
))
3803 return rewrite_one_ok
;
3805 return rewrite_one_noparents
;
3806 if (!(p
= one_relevant_parent(revs
, p
->parents
)))
3807 return rewrite_one_ok
;
3812 static void merge_queue_into_list(struct prio_queue
*q
, struct commit_list
**list
)
3815 struct commit
*item
= prio_queue_peek(q
);
3816 struct commit_list
*p
= *list
;
3818 if (p
&& p
->item
->date
>= item
->date
)
3821 p
= commit_list_insert(item
, list
);
3822 list
= &p
->next
; /* skip newly added item */
3823 prio_queue_get(q
); /* pop item */
3828 static enum rewrite_result
rewrite_one(struct rev_info
*revs
, struct commit
**pp
)
3830 struct prio_queue queue
= { compare_commits_by_commit_date
};
3831 enum rewrite_result ret
= rewrite_one_1(revs
, pp
, &queue
);
3832 merge_queue_into_list(&queue
, &revs
->commits
);
3833 clear_prio_queue(&queue
);
3837 int rewrite_parents(struct rev_info
*revs
, struct commit
*commit
,
3838 rewrite_parent_fn_t rewrite_parent
)
3840 struct commit_list
**pp
= &commit
->parents
;
3842 struct commit_list
*parent
= *pp
;
3843 switch (rewrite_parent(revs
, &parent
->item
)) {
3844 case rewrite_one_ok
:
3846 case rewrite_one_noparents
:
3849 case rewrite_one_error
:
3854 remove_duplicate_parents(revs
, commit
);
3858 static int commit_match(struct commit
*commit
, struct rev_info
*opt
)
3861 const char *encoding
;
3862 const char *message
;
3863 struct strbuf buf
= STRBUF_INIT
;
3865 if (!opt
->grep_filter
.pattern_list
&& !opt
->grep_filter
.header_list
)
3868 /* Prepend "fake" headers as needed */
3869 if (opt
->grep_filter
.use_reflog_filter
) {
3870 strbuf_addstr(&buf
, "reflog ");
3871 get_reflog_message(&buf
, opt
->reflog_info
);
3872 strbuf_addch(&buf
, '\n');
3876 * We grep in the user's output encoding, under the assumption that it
3877 * is the encoding they are most likely to write their grep pattern
3878 * for. In addition, it means we will match the "notes" encoding below,
3879 * so we will not end up with a buffer that has two different encodings
3882 encoding
= get_log_output_encoding();
3883 message
= logmsg_reencode(commit
, NULL
, encoding
);
3885 /* Copy the commit to temporary if we are using "fake" headers */
3887 strbuf_addstr(&buf
, message
);
3889 if (opt
->grep_filter
.header_list
&& opt
->mailmap
) {
3890 const char *commit_headers
[] = { "author ", "committer ", NULL
};
3893 strbuf_addstr(&buf
, message
);
3895 apply_mailmap_to_header(&buf
, commit_headers
, opt
->mailmap
);
3898 /* Append "fake" message parts as needed */
3899 if (opt
->show_notes
) {
3901 strbuf_addstr(&buf
, message
);
3902 format_display_notes(&commit
->object
.oid
, &buf
, encoding
, 1);
3906 * Find either in the original commit message, or in the temporary.
3907 * Note that we cast away the constness of "message" here. It is
3908 * const because it may come from the cached commit buffer. That's OK,
3909 * because we know that it is modifiable heap memory, and that while
3910 * grep_buffer may modify it for speed, it will restore any
3911 * changes before returning.
3914 retval
= grep_buffer(&opt
->grep_filter
, buf
.buf
, buf
.len
);
3916 retval
= grep_buffer(&opt
->grep_filter
,
3917 (char *)message
, strlen(message
));
3918 strbuf_release(&buf
);
3919 unuse_commit_buffer(commit
, message
);
3923 static inline int want_ancestry(const struct rev_info
*revs
)
3925 return (revs
->rewrite_parents
|| revs
->children
.name
);
3929 * Return a timestamp to be used for --since/--until comparisons for this
3930 * commit, based on the revision options.
3932 static timestamp_t
comparison_date(const struct rev_info
*revs
,
3933 struct commit
*commit
)
3935 return revs
->reflog_info
?
3936 get_reflog_timestamp(revs
->reflog_info
) :
3940 enum commit_action
get_commit_action(struct rev_info
*revs
, struct commit
*commit
)
3942 if (commit
->object
.flags
& SHOWN
)
3943 return commit_ignore
;
3944 if (revs
->unpacked
&& has_object_pack(&commit
->object
.oid
))
3945 return commit_ignore
;
3946 if (revs
->no_kept_objects
) {
3947 if (has_object_kept_pack(&commit
->object
.oid
,
3948 revs
->keep_pack_cache_flags
))
3949 return commit_ignore
;
3951 if (commit
->object
.flags
& UNINTERESTING
)
3952 return commit_ignore
;
3953 if (revs
->line_level_traverse
&& !want_ancestry(revs
)) {
3955 * In case of line-level log with parent rewriting
3956 * prepare_revision_walk() already took care of all line-level
3957 * log filtering, and there is nothing left to do here.
3959 * If parent rewriting was not requested, then this is the
3960 * place to perform the line-level log filtering. Notably,
3961 * this check, though expensive, must come before the other,
3962 * cheaper filtering conditions, because the tracked line
3963 * ranges must be adjusted even when the commit will end up
3964 * being ignored based on other conditions.
3966 if (!line_log_process_ranges_arbitrary_commit(revs
, commit
))
3967 return commit_ignore
;
3969 if (revs
->min_age
!= -1 &&
3970 comparison_date(revs
, commit
) > revs
->min_age
)
3971 return commit_ignore
;
3972 if (revs
->max_age_as_filter
!= -1 &&
3973 comparison_date(revs
, commit
) < revs
->max_age_as_filter
)
3974 return commit_ignore
;
3975 if (revs
->min_parents
|| (revs
->max_parents
>= 0)) {
3976 int n
= commit_list_count(commit
->parents
);
3977 if ((n
< revs
->min_parents
) ||
3978 ((revs
->max_parents
>= 0) && (n
> revs
->max_parents
)))
3979 return commit_ignore
;
3981 if (!commit_match(commit
, revs
))
3982 return commit_ignore
;
3983 if (revs
->prune
&& revs
->dense
) {
3984 /* Commit without changes? */
3985 if (commit
->object
.flags
& TREESAME
) {
3987 struct commit_list
*p
;
3988 /* drop merges unless we want parenthood */
3989 if (!want_ancestry(revs
))
3990 return commit_ignore
;
3992 if (revs
->show_pulls
&& (commit
->object
.flags
& PULL_MERGE
))
3996 * If we want ancestry, then need to keep any merges
3997 * between relevant commits to tie together topology.
3998 * For consistency with TREESAME and simplification
3999 * use "relevant" here rather than just INTERESTING,
4000 * to treat bottom commit(s) as part of the topology.
4002 for (n
= 0, p
= commit
->parents
; p
; p
= p
->next
)
4003 if (relevant_commit(p
->item
))
4006 return commit_ignore
;
4012 define_commit_slab(saved_parents
, struct commit_list
*);
4014 #define EMPTY_PARENT_LIST ((struct commit_list *)-1)
4017 * You may only call save_parents() once per commit (this is checked
4018 * for non-root commits).
4020 static void save_parents(struct rev_info
*revs
, struct commit
*commit
)
4022 struct commit_list
**pp
;
4024 if (!revs
->saved_parents_slab
) {
4025 revs
->saved_parents_slab
= xmalloc(sizeof(struct saved_parents
));
4026 init_saved_parents(revs
->saved_parents_slab
);
4029 pp
= saved_parents_at(revs
->saved_parents_slab
, commit
);
4032 * When walking with reflogs, we may visit the same commit
4033 * several times: once for each appearance in the reflog.
4035 * In this case, save_parents() will be called multiple times.
4036 * We want to keep only the first set of parents. We need to
4037 * store a sentinel value for an empty (i.e., NULL) parent
4038 * list to distinguish it from a not-yet-saved list, however.
4042 if (commit
->parents
)
4043 *pp
= copy_commit_list(commit
->parents
);
4045 *pp
= EMPTY_PARENT_LIST
;
4048 static void free_saved_parents(struct rev_info
*revs
)
4050 if (revs
->saved_parents_slab
)
4051 clear_saved_parents(revs
->saved_parents_slab
);
4054 struct commit_list
*get_saved_parents(struct rev_info
*revs
, const struct commit
*commit
)
4056 struct commit_list
*parents
;
4058 if (!revs
->saved_parents_slab
)
4059 return commit
->parents
;
4061 parents
= *saved_parents_at(revs
->saved_parents_slab
, commit
);
4062 if (parents
== EMPTY_PARENT_LIST
)
4067 enum commit_action
simplify_commit(struct rev_info
*revs
, struct commit
*commit
)
4069 enum commit_action action
= get_commit_action(revs
, commit
);
4071 if (action
== commit_show
&&
4072 revs
->prune
&& revs
->dense
&& want_ancestry(revs
)) {
4074 * --full-diff on simplified parents is no good: it
4075 * will show spurious changes from the commits that
4076 * were elided. So we save the parents on the side
4077 * when --full-diff is in effect.
4079 if (revs
->full_diff
)
4080 save_parents(revs
, commit
);
4081 if (rewrite_parents(revs
, commit
, rewrite_one
) < 0)
4082 return commit_error
;
4087 static void track_linear(struct rev_info
*revs
, struct commit
*commit
)
4089 if (revs
->track_first_time
) {
4091 revs
->track_first_time
= 0;
4093 struct commit_list
*p
;
4094 for (p
= revs
->previous_parents
; p
; p
= p
->next
)
4095 if (p
->item
== NULL
|| /* first commit */
4096 oideq(&p
->item
->object
.oid
, &commit
->object
.oid
))
4098 revs
->linear
= p
!= NULL
;
4100 if (revs
->reverse
) {
4102 commit
->object
.flags
|= TRACK_LINEAR
;
4104 free_commit_list(revs
->previous_parents
);
4105 revs
->previous_parents
= copy_commit_list(commit
->parents
);
4108 static struct commit
*get_revision_1(struct rev_info
*revs
)
4111 struct commit
*commit
;
4113 if (revs
->reflog_info
)
4114 commit
= next_reflog_entry(revs
->reflog_info
);
4115 else if (revs
->topo_walk_info
)
4116 commit
= next_topo_commit(revs
);
4118 commit
= pop_commit(&revs
->commits
);
4123 if (revs
->reflog_info
)
4124 commit
->object
.flags
&= ~(ADDED
| SEEN
| SHOWN
);
4127 * If we haven't done the list limiting, we need to look at
4128 * the parents here. We also need to do the date-based limiting
4129 * that we'd otherwise have done in limit_list().
4131 if (!revs
->limited
) {
4132 if (revs
->max_age
!= -1 &&
4133 comparison_date(revs
, commit
) < revs
->max_age
)
4136 if (revs
->reflog_info
)
4137 try_to_simplify_commit(revs
, commit
);
4138 else if (revs
->topo_walk_info
)
4139 expand_topo_walk(revs
, commit
);
4140 else if (process_parents(revs
, commit
, &revs
->commits
, NULL
) < 0) {
4141 if (!revs
->ignore_missing_links
)
4142 die("Failed to traverse parents of commit %s",
4143 oid_to_hex(&commit
->object
.oid
));
4147 switch (simplify_commit(revs
, commit
)) {
4151 die("Failed to simplify parents of commit %s",
4152 oid_to_hex(&commit
->object
.oid
));
4154 if (revs
->track_linear
)
4155 track_linear(revs
, commit
);
4162 * Return true for entries that have not yet been shown. (This is an
4163 * object_array_each_func_t.)
4165 static int entry_unshown(struct object_array_entry
*entry
, void *cb_data UNUSED
)
4167 return !(entry
->item
->flags
& SHOWN
);
4171 * If array is on the verge of a realloc, garbage-collect any entries
4172 * that have already been shown to try to free up some space.
4174 static void gc_boundary(struct object_array
*array
)
4176 if (array
->nr
== array
->alloc
)
4177 object_array_filter(array
, entry_unshown
, NULL
);
4180 static void create_boundary_commit_list(struct rev_info
*revs
)
4184 struct object_array
*array
= &revs
->boundary_commits
;
4185 struct object_array_entry
*objects
= array
->objects
;
4188 * If revs->commits is non-NULL at this point, an error occurred in
4189 * get_revision_1(). Ignore the error and continue printing the
4190 * boundary commits anyway. (This is what the code has always
4193 free_commit_list(revs
->commits
);
4194 revs
->commits
= NULL
;
4197 * Put all of the actual boundary commits from revs->boundary_commits
4198 * into revs->commits
4200 for (i
= 0; i
< array
->nr
; i
++) {
4201 c
= (struct commit
*)(objects
[i
].item
);
4204 if (!(c
->object
.flags
& CHILD_SHOWN
))
4206 if (c
->object
.flags
& (SHOWN
| BOUNDARY
))
4208 c
->object
.flags
|= BOUNDARY
;
4209 commit_list_insert(c
, &revs
->commits
);
4213 * If revs->topo_order is set, sort the boundary commits
4214 * in topological order
4216 sort_in_topological_order(&revs
->commits
, revs
->sort_order
);
4219 static struct commit
*get_revision_internal(struct rev_info
*revs
)
4221 struct commit
*c
= NULL
;
4222 struct commit_list
*l
;
4224 if (revs
->boundary
== 2) {
4226 * All of the normal commits have already been returned,
4227 * and we are now returning boundary commits.
4228 * create_boundary_commit_list() has populated
4229 * revs->commits with the remaining commits to return.
4231 c
= pop_commit(&revs
->commits
);
4233 c
->object
.flags
|= SHOWN
;
4238 * If our max_count counter has reached zero, then we are done. We
4239 * don't simply return NULL because we still might need to show
4240 * boundary commits. But we want to avoid calling get_revision_1, which
4241 * might do a considerable amount of work finding the next commit only
4242 * for us to throw it away.
4244 * If it is non-zero, then either we don't have a max_count at all
4245 * (-1), or it is still counting, in which case we decrement.
4247 if (revs
->max_count
) {
4248 c
= get_revision_1(revs
);
4250 while (revs
->skip_count
> 0) {
4252 c
= get_revision_1(revs
);
4258 if (revs
->max_count
> 0)
4263 c
->object
.flags
|= SHOWN
;
4265 if (!revs
->boundary
)
4270 * get_revision_1() runs out the commits, and
4271 * we are done computing the boundaries.
4272 * switch to boundary commits output mode.
4277 * Update revs->commits to contain the list of
4280 create_boundary_commit_list(revs
);
4282 return get_revision_internal(revs
);
4286 * boundary commits are the commits that are parents of the
4287 * ones we got from get_revision_1() but they themselves are
4288 * not returned from get_revision_1(). Before returning
4289 * 'c', we need to mark its parents that they could be boundaries.
4292 for (l
= c
->parents
; l
; l
= l
->next
) {
4294 p
= &(l
->item
->object
);
4295 if (p
->flags
& (CHILD_SHOWN
| SHOWN
))
4297 p
->flags
|= CHILD_SHOWN
;
4298 gc_boundary(&revs
->boundary_commits
);
4299 add_object_array(p
, NULL
, &revs
->boundary_commits
);
4305 struct commit
*get_revision(struct rev_info
*revs
)
4308 struct commit_list
*reversed
;
4310 if (revs
->reverse
) {
4312 while ((c
= get_revision_internal(revs
)))
4313 commit_list_insert(c
, &reversed
);
4314 revs
->commits
= reversed
;
4316 revs
->reverse_output_stage
= 1;
4319 if (revs
->reverse_output_stage
) {
4320 c
= pop_commit(&revs
->commits
);
4321 if (revs
->track_linear
)
4322 revs
->linear
= !!(c
&& c
->object
.flags
& TRACK_LINEAR
);
4326 c
= get_revision_internal(revs
);
4327 if (c
&& revs
->graph
)
4328 graph_update(revs
->graph
, c
);
4330 free_saved_parents(revs
);
4331 free_commit_list(revs
->previous_parents
);
4332 revs
->previous_parents
= NULL
;
4337 const char *get_revision_mark(const struct rev_info
*revs
, const struct commit
*commit
)
4339 if (commit
->object
.flags
& BOUNDARY
)
4341 else if (commit
->object
.flags
& UNINTERESTING
)
4343 else if (commit
->object
.flags
& PATCHSAME
)
4345 else if (!revs
|| revs
->left_right
) {
4346 if (commit
->object
.flags
& SYMMETRIC_LEFT
)
4350 } else if (revs
->graph
)
4352 else if (revs
->cherry_mark
)
4357 void put_revision_mark(const struct rev_info
*revs
, const struct commit
*commit
)
4359 const char *mark
= get_revision_mark(revs
, commit
);
4362 fputs(mark
, stdout
);