1 #include "git-compat-util.h"
4 #include "environment.h"
7 #include "object-store.h"
13 #include "diff-merges.h"
16 #include "repository.h"
19 #include "reflog-walk.h"
20 #include "patch-ids.h"
23 #include "string-list.h"
26 #include "commit-slab.h"
28 #include "cache-tree.h"
33 #include "commit-reach.h"
34 #include "commit-graph.h"
35 #include "prio-queue.h"
39 #include "json-writer.h"
40 #include "list-objects-filter-options.h"
41 #include "resolve-undo.h"
43 volatile show_early_output_fn_t show_early_output
;
45 static const char *term_bad
;
46 static const char *term_good
;
48 implement_shared_commit_slab(revision_sources
, char *);
50 static inline int want_ancestry(const struct rev_info
*revs
);
52 void show_object_with_name(FILE *out
, struct object
*obj
, const char *name
)
54 fprintf(out
, "%s ", oid_to_hex(&obj
->oid
));
55 for (const char *p
= name
; *p
&& *p
!= '\n'; p
++)
60 static void mark_blob_uninteresting(struct blob
*blob
)
64 if (blob
->object
.flags
& UNINTERESTING
)
66 blob
->object
.flags
|= UNINTERESTING
;
69 static void mark_tree_contents_uninteresting(struct repository
*r
,
72 struct tree_desc desc
;
73 struct name_entry entry
;
75 if (parse_tree_gently(tree
, 1) < 0)
78 init_tree_desc(&desc
, tree
->buffer
, tree
->size
);
79 while (tree_entry(&desc
, &entry
)) {
80 switch (object_type(entry
.mode
)) {
82 mark_tree_uninteresting(r
, lookup_tree(r
, &entry
.oid
));
85 mark_blob_uninteresting(lookup_blob(r
, &entry
.oid
));
88 /* Subproject commit - not in this repository */
94 * We don't care about the tree any more
95 * after it has been marked uninteresting.
97 free_tree_buffer(tree
);
100 void mark_tree_uninteresting(struct repository
*r
, struct tree
*tree
)
108 if (obj
->flags
& UNINTERESTING
)
110 obj
->flags
|= UNINTERESTING
;
111 mark_tree_contents_uninteresting(r
, tree
);
114 struct path_and_oids_entry
{
115 struct hashmap_entry ent
;
120 static int path_and_oids_cmp(const void *hashmap_cmp_fn_data UNUSED
,
121 const struct hashmap_entry
*eptr
,
122 const struct hashmap_entry
*entry_or_key
,
123 const void *keydata UNUSED
)
125 const struct path_and_oids_entry
*e1
, *e2
;
127 e1
= container_of(eptr
, const struct path_and_oids_entry
, ent
);
128 e2
= container_of(entry_or_key
, const struct path_and_oids_entry
, ent
);
130 return strcmp(e1
->path
, e2
->path
);
133 static void paths_and_oids_clear(struct hashmap
*map
)
135 struct hashmap_iter iter
;
136 struct path_and_oids_entry
*entry
;
138 hashmap_for_each_entry(map
, &iter
, entry
, ent
/* member name */) {
139 oidset_clear(&entry
->trees
);
143 hashmap_clear_and_free(map
, struct path_and_oids_entry
, ent
);
146 static void paths_and_oids_insert(struct hashmap
*map
,
148 const struct object_id
*oid
)
150 int hash
= strhash(path
);
151 struct path_and_oids_entry key
;
152 struct path_and_oids_entry
*entry
;
154 hashmap_entry_init(&key
.ent
, hash
);
156 /* use a shallow copy for the lookup */
157 key
.path
= (char *)path
;
158 oidset_init(&key
.trees
, 0);
160 entry
= hashmap_get_entry(map
, &key
, ent
, NULL
);
162 CALLOC_ARRAY(entry
, 1);
163 hashmap_entry_init(&entry
->ent
, hash
);
164 entry
->path
= xstrdup(key
.path
);
165 oidset_init(&entry
->trees
, 16);
166 hashmap_put(map
, &entry
->ent
);
169 oidset_insert(&entry
->trees
, oid
);
172 static void add_children_by_path(struct repository
*r
,
176 struct tree_desc desc
;
177 struct name_entry entry
;
182 if (parse_tree_gently(tree
, 1) < 0)
185 init_tree_desc(&desc
, tree
->buffer
, tree
->size
);
186 while (tree_entry(&desc
, &entry
)) {
187 switch (object_type(entry
.mode
)) {
189 paths_and_oids_insert(map
, entry
.path
, &entry
.oid
);
191 if (tree
->object
.flags
& UNINTERESTING
) {
192 struct tree
*child
= lookup_tree(r
, &entry
.oid
);
194 child
->object
.flags
|= UNINTERESTING
;
198 if (tree
->object
.flags
& UNINTERESTING
) {
199 struct blob
*child
= lookup_blob(r
, &entry
.oid
);
201 child
->object
.flags
|= UNINTERESTING
;
205 /* Subproject commit - not in this repository */
210 free_tree_buffer(tree
);
213 void mark_trees_uninteresting_sparse(struct repository
*r
,
214 struct oidset
*trees
)
216 unsigned has_interesting
= 0, has_uninteresting
= 0;
217 struct hashmap map
= HASHMAP_INIT(path_and_oids_cmp
, NULL
);
218 struct hashmap_iter map_iter
;
219 struct path_and_oids_entry
*entry
;
220 struct object_id
*oid
;
221 struct oidset_iter iter
;
223 oidset_iter_init(trees
, &iter
);
224 while ((!has_interesting
|| !has_uninteresting
) &&
225 (oid
= oidset_iter_next(&iter
))) {
226 struct tree
*tree
= lookup_tree(r
, oid
);
231 if (tree
->object
.flags
& UNINTERESTING
)
232 has_uninteresting
= 1;
237 /* Do not walk unless we have both types of trees. */
238 if (!has_uninteresting
|| !has_interesting
)
241 oidset_iter_init(trees
, &iter
);
242 while ((oid
= oidset_iter_next(&iter
))) {
243 struct tree
*tree
= lookup_tree(r
, oid
);
244 add_children_by_path(r
, tree
, &map
);
247 hashmap_for_each_entry(&map
, &map_iter
, entry
, ent
/* member name */)
248 mark_trees_uninteresting_sparse(r
, &entry
->trees
);
250 paths_and_oids_clear(&map
);
253 struct commit_stack
{
254 struct commit
**items
;
257 #define COMMIT_STACK_INIT { 0 }
259 static void commit_stack_push(struct commit_stack
*stack
, struct commit
*commit
)
261 ALLOC_GROW(stack
->items
, stack
->nr
+ 1, stack
->alloc
);
262 stack
->items
[stack
->nr
++] = commit
;
265 static struct commit
*commit_stack_pop(struct commit_stack
*stack
)
267 return stack
->nr
? stack
->items
[--stack
->nr
] : NULL
;
270 static void commit_stack_clear(struct commit_stack
*stack
)
272 FREE_AND_NULL(stack
->items
);
273 stack
->nr
= stack
->alloc
= 0;
276 static void mark_one_parent_uninteresting(struct rev_info
*revs
, struct commit
*commit
,
277 struct commit_stack
*pending
)
279 struct commit_list
*l
;
281 if (commit
->object
.flags
& UNINTERESTING
)
283 commit
->object
.flags
|= UNINTERESTING
;
286 * Normally we haven't parsed the parent
287 * yet, so we won't have a parent of a parent
288 * here. However, it may turn out that we've
289 * reached this commit some other way (where it
290 * wasn't uninteresting), in which case we need
291 * to mark its parents recursively too..
293 for (l
= commit
->parents
; l
; l
= l
->next
) {
294 commit_stack_push(pending
, l
->item
);
295 if (revs
&& revs
->exclude_first_parent_only
)
300 void mark_parents_uninteresting(struct rev_info
*revs
, struct commit
*commit
)
302 struct commit_stack pending
= COMMIT_STACK_INIT
;
303 struct commit_list
*l
;
305 for (l
= commit
->parents
; l
; l
= l
->next
) {
306 mark_one_parent_uninteresting(revs
, l
->item
, &pending
);
307 if (revs
&& revs
->exclude_first_parent_only
)
311 while (pending
.nr
> 0)
312 mark_one_parent_uninteresting(revs
, commit_stack_pop(&pending
),
315 commit_stack_clear(&pending
);
318 static void add_pending_object_with_path(struct rev_info
*revs
,
320 const char *name
, unsigned mode
,
323 struct interpret_branch_name_options options
= { 0 };
326 if (revs
->no_walk
&& (obj
->flags
& UNINTERESTING
))
328 if (revs
->reflog_info
&& obj
->type
== OBJ_COMMIT
) {
329 struct strbuf buf
= STRBUF_INIT
;
330 size_t namelen
= strlen(name
);
331 int len
= interpret_branch_name(name
, namelen
, &buf
, &options
);
333 if (0 < len
&& len
< namelen
&& buf
.len
)
334 strbuf_addstr(&buf
, name
+ len
);
335 add_reflog_for_walk(revs
->reflog_info
,
336 (struct commit
*)obj
,
337 buf
.buf
[0] ? buf
.buf
: name
);
338 strbuf_release(&buf
);
339 return; /* do not add the commit itself */
341 add_object_array_with_path(obj
, name
, &revs
->pending
, mode
, path
);
344 static void add_pending_object_with_mode(struct rev_info
*revs
,
346 const char *name
, unsigned mode
)
348 add_pending_object_with_path(revs
, obj
, name
, mode
, NULL
);
351 void add_pending_object(struct rev_info
*revs
,
352 struct object
*obj
, const char *name
)
354 add_pending_object_with_mode(revs
, obj
, name
, S_IFINVALID
);
357 void add_head_to_pending(struct rev_info
*revs
)
359 struct object_id oid
;
361 if (get_oid("HEAD", &oid
))
363 obj
= parse_object(revs
->repo
, &oid
);
366 add_pending_object(revs
, obj
, "HEAD");
369 static struct object
*get_reference(struct rev_info
*revs
, const char *name
,
370 const struct object_id
*oid
,
373 struct object
*object
;
375 object
= parse_object_with_flags(revs
->repo
, oid
,
376 revs
->verify_objects
? 0 :
377 PARSE_OBJECT_SKIP_HASH_CHECK
);
380 if (revs
->ignore_missing
)
382 if (revs
->exclude_promisor_objects
&& is_promisor_object(oid
))
384 die("bad object %s", name
);
386 object
->flags
|= flags
;
390 void add_pending_oid(struct rev_info
*revs
, const char *name
,
391 const struct object_id
*oid
, unsigned int flags
)
393 struct object
*object
= get_reference(revs
, name
, oid
, flags
);
394 add_pending_object(revs
, object
, name
);
397 static struct commit
*handle_commit(struct rev_info
*revs
,
398 struct object_array_entry
*entry
)
400 struct object
*object
= entry
->item
;
401 const char *name
= entry
->name
;
402 const char *path
= entry
->path
;
403 unsigned int mode
= entry
->mode
;
404 unsigned long flags
= object
->flags
;
407 * Tag object? Look what it points to..
409 while (object
->type
== OBJ_TAG
) {
410 struct tag
*tag
= (struct tag
*) object
;
411 if (revs
->tag_objects
&& !(flags
& UNINTERESTING
))
412 add_pending_object(revs
, object
, tag
->tag
);
413 object
= parse_object(revs
->repo
, get_tagged_oid(tag
));
415 if (revs
->ignore_missing_links
|| (flags
& UNINTERESTING
))
417 if (revs
->exclude_promisor_objects
&&
418 is_promisor_object(&tag
->tagged
->oid
))
420 die("bad object %s", oid_to_hex(&tag
->tagged
->oid
));
422 object
->flags
|= flags
;
424 * We'll handle the tagged object by looping or dropping
425 * through to the non-tag handlers below. Do not
426 * propagate path data from the tag's pending entry.
433 * Commit object? Just return it, we'll do all the complex
436 if (object
->type
== OBJ_COMMIT
) {
437 struct commit
*commit
= (struct commit
*)object
;
439 if (repo_parse_commit(revs
->repo
, commit
) < 0)
440 die("unable to parse commit %s", name
);
441 if (flags
& UNINTERESTING
) {
442 mark_parents_uninteresting(revs
, commit
);
444 if (!revs
->topo_order
|| !generation_numbers_enabled(the_repository
))
448 char **slot
= revision_sources_at(revs
->sources
, commit
);
451 *slot
= xstrdup(name
);
457 * Tree object? Either mark it uninteresting, or add it
458 * to the list of objects to look at later..
460 if (object
->type
== OBJ_TREE
) {
461 struct tree
*tree
= (struct tree
*)object
;
462 if (!revs
->tree_objects
)
464 if (flags
& UNINTERESTING
) {
465 mark_tree_contents_uninteresting(revs
->repo
, tree
);
468 add_pending_object_with_path(revs
, object
, name
, mode
, path
);
473 * Blob object? You know the drill by now..
475 if (object
->type
== OBJ_BLOB
) {
476 if (!revs
->blob_objects
)
478 if (flags
& UNINTERESTING
)
480 add_pending_object_with_path(revs
, object
, name
, mode
, path
);
483 die("%s is unknown object", name
);
486 static int everybody_uninteresting(struct commit_list
*orig
,
487 struct commit
**interesting_cache
)
489 struct commit_list
*list
= orig
;
491 if (*interesting_cache
) {
492 struct commit
*commit
= *interesting_cache
;
493 if (!(commit
->object
.flags
& UNINTERESTING
))
498 struct commit
*commit
= list
->item
;
500 if (commit
->object
.flags
& UNINTERESTING
)
503 *interesting_cache
= commit
;
510 * A definition of "relevant" commit that we can use to simplify limited graphs
511 * by eliminating side branches.
513 * A "relevant" commit is one that is !UNINTERESTING (ie we are including it
514 * in our list), or that is a specified BOTTOM commit. Then after computing
515 * a limited list, during processing we can generally ignore boundary merges
516 * coming from outside the graph, (ie from irrelevant parents), and treat
517 * those merges as if they were single-parent. TREESAME is defined to consider
518 * only relevant parents, if any. If we are TREESAME to our on-graph parents,
519 * we don't care if we were !TREESAME to non-graph parents.
521 * Treating bottom commits as relevant ensures that a limited graph's
522 * connection to the actual bottom commit is not viewed as a side branch, but
523 * treated as part of the graph. For example:
525 * ....Z...A---X---o---o---B
529 * When computing "A..B", the A-X connection is at least as important as
530 * Y-X, despite A being flagged UNINTERESTING.
532 * And when computing --ancestry-path "A..B", the A-X connection is more
533 * important than Y-X, despite both A and Y being flagged UNINTERESTING.
535 static inline int relevant_commit(struct commit
*commit
)
537 return (commit
->object
.flags
& (UNINTERESTING
| BOTTOM
)) != UNINTERESTING
;
541 * Return a single relevant commit from a parent list. If we are a TREESAME
542 * commit, and this selects one of our parents, then we can safely simplify to
545 static struct commit
*one_relevant_parent(const struct rev_info
*revs
,
546 struct commit_list
*orig
)
548 struct commit_list
*list
= orig
;
549 struct commit
*relevant
= NULL
;
555 * For 1-parent commits, or if first-parent-only, then return that
556 * first parent (even if not "relevant" by the above definition).
557 * TREESAME will have been set purely on that parent.
559 if (revs
->first_parent_only
|| !orig
->next
)
563 * For multi-parent commits, identify a sole relevant parent, if any.
564 * If we have only one relevant parent, then TREESAME will be set purely
565 * with regard to that parent, and we can simplify accordingly.
567 * If we have more than one relevant parent, or no relevant parents
568 * (and multiple irrelevant ones), then we can't select a parent here
572 struct commit
*commit
= list
->item
;
574 if (relevant_commit(commit
)) {
584 * The goal is to get REV_TREE_NEW as the result only if the
585 * diff consists of all '+' (and no other changes), REV_TREE_OLD
586 * if the whole diff is removal of old data, and otherwise
587 * REV_TREE_DIFFERENT (of course if the trees are the same we
588 * want REV_TREE_SAME).
590 * The only time we care about the distinction is when
591 * remove_empty_trees is in effect, in which case we care only about
592 * whether the whole change is REV_TREE_NEW, or if there's another type
593 * of change. Which means we can stop the diff early in either of these
596 * 1. We're not using remove_empty_trees at all.
598 * 2. We saw anything except REV_TREE_NEW.
600 #define REV_TREE_SAME 0
601 #define REV_TREE_NEW 1 /* Only new files */
602 #define REV_TREE_OLD 2 /* Only files removed */
603 #define REV_TREE_DIFFERENT 3 /* Mixed changes */
604 static int tree_difference
= REV_TREE_SAME
;
606 static void file_add_remove(struct diff_options
*options
,
608 unsigned mode UNUSED
,
609 const struct object_id
*oid UNUSED
,
610 int oid_valid UNUSED
,
611 const char *fullpath UNUSED
,
612 unsigned dirty_submodule UNUSED
)
614 int diff
= addremove
== '+' ? REV_TREE_NEW
: REV_TREE_OLD
;
615 struct rev_info
*revs
= options
->change_fn_data
;
617 tree_difference
|= diff
;
618 if (!revs
->remove_empty_trees
|| tree_difference
!= REV_TREE_NEW
)
619 options
->flags
.has_changes
= 1;
622 static void file_change(struct diff_options
*options
,
623 unsigned old_mode UNUSED
,
624 unsigned new_mode UNUSED
,
625 const struct object_id
*old_oid UNUSED
,
626 const struct object_id
*new_oid UNUSED
,
627 int old_oid_valid UNUSED
,
628 int new_oid_valid UNUSED
,
629 const char *fullpath UNUSED
,
630 unsigned old_dirty_submodule UNUSED
,
631 unsigned new_dirty_submodule UNUSED
)
633 tree_difference
= REV_TREE_DIFFERENT
;
634 options
->flags
.has_changes
= 1;
637 static int bloom_filter_atexit_registered
;
638 static unsigned int count_bloom_filter_maybe
;
639 static unsigned int count_bloom_filter_definitely_not
;
640 static unsigned int count_bloom_filter_false_positive
;
641 static unsigned int count_bloom_filter_not_present
;
643 static void trace2_bloom_filter_statistics_atexit(void)
645 struct json_writer jw
= JSON_WRITER_INIT
;
647 jw_object_begin(&jw
, 0);
648 jw_object_intmax(&jw
, "filter_not_present", count_bloom_filter_not_present
);
649 jw_object_intmax(&jw
, "maybe", count_bloom_filter_maybe
);
650 jw_object_intmax(&jw
, "definitely_not", count_bloom_filter_definitely_not
);
651 jw_object_intmax(&jw
, "false_positive", count_bloom_filter_false_positive
);
654 trace2_data_json("bloom", the_repository
, "statistics", &jw
);
659 static int forbid_bloom_filters(struct pathspec
*spec
)
661 if (spec
->has_wildcard
)
665 if (spec
->magic
& ~PATHSPEC_LITERAL
)
667 if (spec
->nr
&& (spec
->items
[0].magic
& ~PATHSPEC_LITERAL
))
673 static void prepare_to_use_bloom_filter(struct rev_info
*revs
)
675 struct pathspec_item
*pi
;
676 char *path_alloc
= NULL
;
677 const char *path
, *p
;
679 int path_component_nr
= 1;
684 if (forbid_bloom_filters(&revs
->prune_data
))
687 repo_parse_commit(revs
->repo
, revs
->commits
->item
);
689 revs
->bloom_filter_settings
= get_bloom_filter_settings(revs
->repo
);
690 if (!revs
->bloom_filter_settings
)
693 if (!revs
->pruning
.pathspec
.nr
)
696 pi
= &revs
->pruning
.pathspec
.items
[0];
698 /* remove single trailing slash from path, if needed */
699 if (pi
->len
> 0 && pi
->match
[pi
->len
- 1] == '/') {
700 path_alloc
= xmemdupz(pi
->match
, pi
->len
- 1);
707 revs
->bloom_filter_settings
= NULL
;
715 * At this point, the path is normalized to use Unix-style
716 * path separators. This is required due to how the
717 * changed-path Bloom filters store the paths.
724 revs
->bloom_keys_nr
= path_component_nr
;
725 ALLOC_ARRAY(revs
->bloom_keys
, revs
->bloom_keys_nr
);
727 fill_bloom_key(path
, len
, &revs
->bloom_keys
[0],
728 revs
->bloom_filter_settings
);
729 path_component_nr
= 1;
734 fill_bloom_key(path
, p
- path
,
735 &revs
->bloom_keys
[path_component_nr
++],
736 revs
->bloom_filter_settings
);
740 if (trace2_is_enabled() && !bloom_filter_atexit_registered
) {
741 atexit(trace2_bloom_filter_statistics_atexit
);
742 bloom_filter_atexit_registered
= 1;
748 static int check_maybe_different_in_bloom_filter(struct rev_info
*revs
,
749 struct commit
*commit
)
751 struct bloom_filter
*filter
;
754 if (!revs
->repo
->objects
->commit_graph
)
757 if (commit_graph_generation(commit
) == GENERATION_NUMBER_INFINITY
)
760 filter
= get_bloom_filter(revs
->repo
, commit
);
763 count_bloom_filter_not_present
++;
767 for (j
= 0; result
&& j
< revs
->bloom_keys_nr
; j
++) {
768 result
= bloom_filter_contains(filter
,
769 &revs
->bloom_keys
[j
],
770 revs
->bloom_filter_settings
);
774 count_bloom_filter_maybe
++;
776 count_bloom_filter_definitely_not
++;
781 static int rev_compare_tree(struct rev_info
*revs
,
782 struct commit
*parent
, struct commit
*commit
, int nth_parent
)
784 struct tree
*t1
= get_commit_tree(parent
);
785 struct tree
*t2
= get_commit_tree(commit
);
793 if (revs
->simplify_by_decoration
) {
795 * If we are simplifying by decoration, then the commit
796 * is worth showing if it has a tag pointing at it.
798 if (get_name_decoration(&commit
->object
))
799 return REV_TREE_DIFFERENT
;
801 * A commit that is not pointed by a tag is uninteresting
802 * if we are not limited by path. This means that you will
803 * see the usual "commits that touch the paths" plus any
804 * tagged commit by specifying both --simplify-by-decoration
807 if (!revs
->prune_data
.nr
)
808 return REV_TREE_SAME
;
811 if (revs
->bloom_keys_nr
&& !nth_parent
) {
812 bloom_ret
= check_maybe_different_in_bloom_filter(revs
, commit
);
815 return REV_TREE_SAME
;
818 tree_difference
= REV_TREE_SAME
;
819 revs
->pruning
.flags
.has_changes
= 0;
820 diff_tree_oid(&t1
->object
.oid
, &t2
->object
.oid
, "", &revs
->pruning
);
823 if (bloom_ret
== 1 && tree_difference
== REV_TREE_SAME
)
824 count_bloom_filter_false_positive
++;
826 return tree_difference
;
829 static int rev_same_tree_as_empty(struct rev_info
*revs
, struct commit
*commit
)
831 struct tree
*t1
= get_commit_tree(commit
);
836 tree_difference
= REV_TREE_SAME
;
837 revs
->pruning
.flags
.has_changes
= 0;
838 diff_tree_oid(NULL
, &t1
->object
.oid
, "", &revs
->pruning
);
840 return tree_difference
== REV_TREE_SAME
;
843 struct treesame_state
{
844 unsigned int nparents
;
845 unsigned char treesame
[FLEX_ARRAY
];
848 static struct treesame_state
*initialise_treesame(struct rev_info
*revs
, struct commit
*commit
)
850 unsigned n
= commit_list_count(commit
->parents
);
851 struct treesame_state
*st
= xcalloc(1, st_add(sizeof(*st
), n
));
853 add_decoration(&revs
->treesame
, &commit
->object
, st
);
858 * Must be called immediately after removing the nth_parent from a commit's
859 * parent list, if we are maintaining the per-parent treesame[] decoration.
860 * This does not recalculate the master TREESAME flag - update_treesame()
861 * should be called to update it after a sequence of treesame[] modifications
862 * that may have affected it.
864 static int compact_treesame(struct rev_info
*revs
, struct commit
*commit
, unsigned nth_parent
)
866 struct treesame_state
*st
;
869 if (!commit
->parents
) {
871 * Have just removed the only parent from a non-merge.
872 * Different handling, as we lack decoration.
875 die("compact_treesame %u", nth_parent
);
876 old_same
= !!(commit
->object
.flags
& TREESAME
);
877 if (rev_same_tree_as_empty(revs
, commit
))
878 commit
->object
.flags
|= TREESAME
;
880 commit
->object
.flags
&= ~TREESAME
;
884 st
= lookup_decoration(&revs
->treesame
, &commit
->object
);
885 if (!st
|| nth_parent
>= st
->nparents
)
886 die("compact_treesame %u", nth_parent
);
888 old_same
= st
->treesame
[nth_parent
];
889 memmove(st
->treesame
+ nth_parent
,
890 st
->treesame
+ nth_parent
+ 1,
891 st
->nparents
- nth_parent
- 1);
894 * If we've just become a non-merge commit, update TREESAME
895 * immediately, and remove the no-longer-needed decoration.
896 * If still a merge, defer update until update_treesame().
898 if (--st
->nparents
== 1) {
899 if (commit
->parents
->next
)
900 die("compact_treesame parents mismatch");
901 if (st
->treesame
[0] && revs
->dense
)
902 commit
->object
.flags
|= TREESAME
;
904 commit
->object
.flags
&= ~TREESAME
;
905 free(add_decoration(&revs
->treesame
, &commit
->object
, NULL
));
911 static unsigned update_treesame(struct rev_info
*revs
, struct commit
*commit
)
913 if (commit
->parents
&& commit
->parents
->next
) {
915 struct treesame_state
*st
;
916 struct commit_list
*p
;
917 unsigned relevant_parents
;
918 unsigned relevant_change
, irrelevant_change
;
920 st
= lookup_decoration(&revs
->treesame
, &commit
->object
);
922 die("update_treesame %s", oid_to_hex(&commit
->object
.oid
));
923 relevant_parents
= 0;
924 relevant_change
= irrelevant_change
= 0;
925 for (p
= commit
->parents
, n
= 0; p
; n
++, p
= p
->next
) {
926 if (relevant_commit(p
->item
)) {
927 relevant_change
|= !st
->treesame
[n
];
930 irrelevant_change
|= !st
->treesame
[n
];
932 if (relevant_parents
? relevant_change
: irrelevant_change
)
933 commit
->object
.flags
&= ~TREESAME
;
935 commit
->object
.flags
|= TREESAME
;
938 return commit
->object
.flags
& TREESAME
;
941 static inline int limiting_can_increase_treesame(const struct rev_info
*revs
)
944 * TREESAME is irrelevant unless prune && dense;
945 * if simplify_history is set, we can't have a mixture of TREESAME and
946 * !TREESAME INTERESTING parents (and we don't have treesame[]
947 * decoration anyway);
948 * if first_parent_only is set, then the TREESAME flag is locked
949 * against the first parent (and again we lack treesame[] decoration).
951 return revs
->prune
&& revs
->dense
&&
952 !revs
->simplify_history
&&
953 !revs
->first_parent_only
;
956 static void try_to_simplify_commit(struct rev_info
*revs
, struct commit
*commit
)
958 struct commit_list
**pp
, *parent
;
959 struct treesame_state
*ts
= NULL
;
960 int relevant_change
= 0, irrelevant_change
= 0;
961 int relevant_parents
, nth_parent
;
964 * If we don't do pruning, everything is interesting
969 if (!get_commit_tree(commit
))
972 if (!commit
->parents
) {
973 if (rev_same_tree_as_empty(revs
, commit
))
974 commit
->object
.flags
|= TREESAME
;
979 * Normal non-merge commit? If we don't want to make the
980 * history dense, we consider it always to be a change..
982 if (!revs
->dense
&& !commit
->parents
->next
)
985 for (pp
= &commit
->parents
, nth_parent
= 0, relevant_parents
= 0;
986 (parent
= *pp
) != NULL
;
987 pp
= &parent
->next
, nth_parent
++) {
988 struct commit
*p
= parent
->item
;
989 if (relevant_commit(p
))
992 if (nth_parent
== 1) {
994 * This our second loop iteration - so we now know
995 * we're dealing with a merge.
997 * Do not compare with later parents when we care only about
998 * the first parent chain, in order to avoid derailing the
999 * traversal to follow a side branch that brought everything
1000 * in the path we are limited to by the pathspec.
1002 if (revs
->first_parent_only
)
1005 * If this will remain a potentially-simplifiable
1006 * merge, remember per-parent treesame if needed.
1007 * Initialise the array with the comparison from our
1010 if (revs
->treesame
.name
&&
1011 !revs
->simplify_history
&&
1012 !(commit
->object
.flags
& UNINTERESTING
)) {
1013 ts
= initialise_treesame(revs
, commit
);
1014 if (!(irrelevant_change
|| relevant_change
))
1015 ts
->treesame
[0] = 1;
1018 if (repo_parse_commit(revs
->repo
, p
) < 0)
1019 die("cannot simplify commit %s (because of %s)",
1020 oid_to_hex(&commit
->object
.oid
),
1021 oid_to_hex(&p
->object
.oid
));
1022 switch (rev_compare_tree(revs
, p
, commit
, nth_parent
)) {
1024 if (!revs
->simplify_history
|| !relevant_commit(p
)) {
1025 /* Even if a merge with an uninteresting
1026 * side branch brought the entire change
1027 * we are interested in, we do not want
1028 * to lose the other branches of this
1029 * merge, so we just keep going.
1032 ts
->treesame
[nth_parent
] = 1;
1035 parent
->next
= NULL
;
1036 commit
->parents
= parent
;
1039 * A merge commit is a "diversion" if it is not
1040 * TREESAME to its first parent but is TREESAME
1041 * to a later parent. In the simplified history,
1042 * we "divert" the history walk to the later
1043 * parent. These commits are shown when "show_pulls"
1044 * is enabled, so do not mark the object as
1047 if (!revs
->show_pulls
|| !nth_parent
)
1048 commit
->object
.flags
|= TREESAME
;
1053 if (revs
->remove_empty_trees
&&
1054 rev_same_tree_as_empty(revs
, p
)) {
1055 /* We are adding all the specified
1056 * paths from this parent, so the
1057 * history beyond this parent is not
1058 * interesting. Remove its parents
1059 * (they are grandparents for us).
1060 * IOW, we pretend this parent is a
1063 if (repo_parse_commit(revs
->repo
, p
) < 0)
1064 die("cannot simplify commit %s (invalid %s)",
1065 oid_to_hex(&commit
->object
.oid
),
1066 oid_to_hex(&p
->object
.oid
));
1071 case REV_TREE_DIFFERENT
:
1072 if (relevant_commit(p
))
1073 relevant_change
= 1;
1075 irrelevant_change
= 1;
1078 commit
->object
.flags
|= PULL_MERGE
;
1082 die("bad tree compare for commit %s", oid_to_hex(&commit
->object
.oid
));
1086 * TREESAME is straightforward for single-parent commits. For merge
1087 * commits, it is most useful to define it so that "irrelevant"
1088 * parents cannot make us !TREESAME - if we have any relevant
1089 * parents, then we only consider TREESAMEness with respect to them,
1090 * allowing irrelevant merges from uninteresting branches to be
1091 * simplified away. Only if we have only irrelevant parents do we
1092 * base TREESAME on them. Note that this logic is replicated in
1093 * update_treesame, which should be kept in sync.
1095 if (relevant_parents
? !relevant_change
: !irrelevant_change
)
1096 commit
->object
.flags
|= TREESAME
;
1099 static int process_parents(struct rev_info
*revs
, struct commit
*commit
,
1100 struct commit_list
**list
, struct prio_queue
*queue
)
1102 struct commit_list
*parent
= commit
->parents
;
1103 unsigned pass_flags
;
1105 if (commit
->object
.flags
& ADDED
)
1107 commit
->object
.flags
|= ADDED
;
1109 if (revs
->include_check
&&
1110 !revs
->include_check(commit
, revs
->include_check_data
))
1114 * If the commit is uninteresting, don't try to
1115 * prune parents - we want the maximal uninteresting
1118 * Normally we haven't parsed the parent
1119 * yet, so we won't have a parent of a parent
1120 * here. However, it may turn out that we've
1121 * reached this commit some other way (where it
1122 * wasn't uninteresting), in which case we need
1123 * to mark its parents recursively too..
1125 if (commit
->object
.flags
& UNINTERESTING
) {
1127 struct commit
*p
= parent
->item
;
1128 parent
= parent
->next
;
1130 p
->object
.flags
|= UNINTERESTING
;
1131 if (repo_parse_commit_gently(revs
->repo
, p
, 1) < 0)
1134 mark_parents_uninteresting(revs
, p
);
1135 if (p
->object
.flags
& SEEN
)
1137 p
->object
.flags
|= (SEEN
| NOT_USER_GIVEN
);
1139 commit_list_insert_by_date(p
, list
);
1141 prio_queue_put(queue
, p
);
1142 if (revs
->exclude_first_parent_only
)
1149 * Ok, the commit wasn't uninteresting. Try to
1150 * simplify the commit history and find the parent
1151 * that has no differences in the path set if one exists.
1153 try_to_simplify_commit(revs
, commit
);
1158 pass_flags
= (commit
->object
.flags
& (SYMMETRIC_LEFT
| ANCESTRY_PATH
));
1160 for (parent
= commit
->parents
; parent
; parent
= parent
->next
) {
1161 struct commit
*p
= parent
->item
;
1162 int gently
= revs
->ignore_missing_links
||
1163 revs
->exclude_promisor_objects
;
1164 if (repo_parse_commit_gently(revs
->repo
, p
, gently
) < 0) {
1165 if (revs
->exclude_promisor_objects
&&
1166 is_promisor_object(&p
->object
.oid
)) {
1167 if (revs
->first_parent_only
)
1173 if (revs
->sources
) {
1174 char **slot
= revision_sources_at(revs
->sources
, p
);
1177 *slot
= *revision_sources_at(revs
->sources
, commit
);
1179 p
->object
.flags
|= pass_flags
;
1180 if (!(p
->object
.flags
& SEEN
)) {
1181 p
->object
.flags
|= (SEEN
| NOT_USER_GIVEN
);
1183 commit_list_insert_by_date(p
, list
);
1185 prio_queue_put(queue
, p
);
1187 if (revs
->first_parent_only
)
1193 static void cherry_pick_list(struct commit_list
*list
, struct rev_info
*revs
)
1195 struct commit_list
*p
;
1196 int left_count
= 0, right_count
= 0;
1198 struct patch_ids ids
;
1199 unsigned cherry_flag
;
1201 /* First count the commits on the left and on the right */
1202 for (p
= list
; p
; p
= p
->next
) {
1203 struct commit
*commit
= p
->item
;
1204 unsigned flags
= commit
->object
.flags
;
1205 if (flags
& BOUNDARY
)
1207 else if (flags
& SYMMETRIC_LEFT
)
1213 if (!left_count
|| !right_count
)
1216 left_first
= left_count
< right_count
;
1217 init_patch_ids(revs
->repo
, &ids
);
1218 ids
.diffopts
.pathspec
= revs
->diffopt
.pathspec
;
1220 /* Compute patch-ids for one side */
1221 for (p
= list
; p
; p
= p
->next
) {
1222 struct commit
*commit
= p
->item
;
1223 unsigned flags
= commit
->object
.flags
;
1225 if (flags
& BOUNDARY
)
1228 * If we have fewer left, left_first is set and we omit
1229 * commits on the right branch in this loop. If we have
1230 * fewer right, we skip the left ones.
1232 if (left_first
!= !!(flags
& SYMMETRIC_LEFT
))
1234 add_commit_patch_id(commit
, &ids
);
1237 /* either cherry_mark or cherry_pick are true */
1238 cherry_flag
= revs
->cherry_mark
? PATCHSAME
: SHOWN
;
1240 /* Check the other side */
1241 for (p
= list
; p
; p
= p
->next
) {
1242 struct commit
*commit
= p
->item
;
1243 struct patch_id
*id
;
1244 unsigned flags
= commit
->object
.flags
;
1246 if (flags
& BOUNDARY
)
1249 * If we have fewer left, left_first is set and we omit
1250 * commits on the left branch in this loop.
1252 if (left_first
== !!(flags
& SYMMETRIC_LEFT
))
1256 * Have we seen the same patch id?
1258 id
= patch_id_iter_first(commit
, &ids
);
1262 commit
->object
.flags
|= cherry_flag
;
1264 id
->commit
->object
.flags
|= cherry_flag
;
1265 } while ((id
= patch_id_iter_next(id
, &ids
)));
1268 free_patch_ids(&ids
);
1271 /* How many extra uninteresting commits we want to see.. */
1274 static int still_interesting(struct commit_list
*src
, timestamp_t date
, int slop
,
1275 struct commit
**interesting_cache
)
1278 * No source list at all? We're definitely done..
1284 * Does the destination list contain entries with a date
1285 * before the source list? Definitely _not_ done.
1287 if (date
<= src
->item
->date
)
1291 * Does the source list still have interesting commits in
1292 * it? Definitely not done..
1294 if (!everybody_uninteresting(src
, interesting_cache
))
1297 /* Ok, we're closing in.. */
1302 * "rev-list --ancestry-path=C_0 [--ancestry-path=C_1 ...] A..B"
1303 * computes commits that are ancestors of B but not ancestors of A but
1304 * further limits the result to those that have any of C in their
1305 * ancestry path (i.e. are either ancestors of any of C, descendants
1306 * of any of C, or are any of C). If --ancestry-path is specified with
1307 * no commit, we use all bottom commits for C.
1309 * Before this function is called, ancestors of C will have already
1310 * been marked with ANCESTRY_PATH previously.
1312 * This takes the list of bottom commits and the result of "A..B"
1313 * without --ancestry-path, and limits the latter further to the ones
1314 * that have any of C in their ancestry path. Since the ancestors of C
1315 * have already been marked (a prerequisite of this function), we just
1316 * need to mark the descendants, then exclude any commit that does not
1317 * have any of these marks.
1319 static void limit_to_ancestry(struct commit_list
*bottoms
, struct commit_list
*list
)
1321 struct commit_list
*p
;
1322 struct commit_list
*rlist
= NULL
;
1326 * Reverse the list so that it will be likely that we would
1327 * process parents before children.
1329 for (p
= list
; p
; p
= p
->next
)
1330 commit_list_insert(p
->item
, &rlist
);
1332 for (p
= bottoms
; p
; p
= p
->next
)
1333 p
->item
->object
.flags
|= TMP_MARK
;
1336 * Mark the ones that can reach bottom commits in "list",
1337 * in a bottom-up fashion.
1341 for (p
= rlist
; p
; p
= p
->next
) {
1342 struct commit
*c
= p
->item
;
1343 struct commit_list
*parents
;
1344 if (c
->object
.flags
& (TMP_MARK
| UNINTERESTING
))
1346 for (parents
= c
->parents
;
1348 parents
= parents
->next
) {
1349 if (!(parents
->item
->object
.flags
& TMP_MARK
))
1351 c
->object
.flags
|= TMP_MARK
;
1356 } while (made_progress
);
1359 * NEEDSWORK: decide if we want to remove parents that are
1360 * not marked with TMP_MARK from commit->parents for commits
1361 * in the resulting list. We may not want to do that, though.
1365 * The ones that are not marked with either TMP_MARK or
1366 * ANCESTRY_PATH are uninteresting
1368 for (p
= list
; p
; p
= p
->next
) {
1369 struct commit
*c
= p
->item
;
1370 if (c
->object
.flags
& (TMP_MARK
| ANCESTRY_PATH
))
1372 c
->object
.flags
|= UNINTERESTING
;
1375 /* We are done with TMP_MARK and ANCESTRY_PATH */
1376 for (p
= list
; p
; p
= p
->next
)
1377 p
->item
->object
.flags
&= ~(TMP_MARK
| ANCESTRY_PATH
);
1378 for (p
= bottoms
; p
; p
= p
->next
)
1379 p
->item
->object
.flags
&= ~(TMP_MARK
| ANCESTRY_PATH
);
1380 free_commit_list(rlist
);
1384 * Before walking the history, add the set of "negative" refs the
1385 * caller has asked to exclude to the bottom list.
1387 * This is used to compute "rev-list --ancestry-path A..B", as we need
1388 * to filter the result of "A..B" further to the ones that can actually
1391 static void collect_bottom_commits(struct commit_list
*list
,
1392 struct commit_list
**bottom
)
1394 struct commit_list
*elem
;
1395 for (elem
= list
; elem
; elem
= elem
->next
)
1396 if (elem
->item
->object
.flags
& BOTTOM
)
1397 commit_list_insert(elem
->item
, bottom
);
1400 /* Assumes either left_only or right_only is set */
1401 static void limit_left_right(struct commit_list
*list
, struct rev_info
*revs
)
1403 struct commit_list
*p
;
1405 for (p
= list
; p
; p
= p
->next
) {
1406 struct commit
*commit
= p
->item
;
1408 if (revs
->right_only
) {
1409 if (commit
->object
.flags
& SYMMETRIC_LEFT
)
1410 commit
->object
.flags
|= SHOWN
;
1411 } else /* revs->left_only is set */
1412 if (!(commit
->object
.flags
& SYMMETRIC_LEFT
))
1413 commit
->object
.flags
|= SHOWN
;
1417 static int limit_list(struct rev_info
*revs
)
1420 timestamp_t date
= TIME_MAX
;
1421 struct commit_list
*original_list
= revs
->commits
;
1422 struct commit_list
*newlist
= NULL
;
1423 struct commit_list
**p
= &newlist
;
1424 struct commit
*interesting_cache
= NULL
;
1426 if (revs
->ancestry_path_implicit_bottoms
) {
1427 collect_bottom_commits(original_list
,
1428 &revs
->ancestry_path_bottoms
);
1429 if (!revs
->ancestry_path_bottoms
)
1430 die("--ancestry-path given but there are no bottom commits");
1433 while (original_list
) {
1434 struct commit
*commit
= pop_commit(&original_list
);
1435 struct object
*obj
= &commit
->object
;
1436 show_early_output_fn_t show
;
1438 if (commit
== interesting_cache
)
1439 interesting_cache
= NULL
;
1441 if (revs
->max_age
!= -1 && (commit
->date
< revs
->max_age
))
1442 obj
->flags
|= UNINTERESTING
;
1443 if (process_parents(revs
, commit
, &original_list
, NULL
) < 0)
1445 if (obj
->flags
& UNINTERESTING
) {
1446 mark_parents_uninteresting(revs
, commit
);
1447 slop
= still_interesting(original_list
, date
, slop
, &interesting_cache
);
1452 if (revs
->min_age
!= -1 && (commit
->date
> revs
->min_age
) &&
1453 !revs
->line_level_traverse
)
1455 if (revs
->max_age_as_filter
!= -1 &&
1456 (commit
->date
< revs
->max_age_as_filter
) && !revs
->line_level_traverse
)
1458 date
= commit
->date
;
1459 p
= &commit_list_insert(commit
, p
)->next
;
1461 show
= show_early_output
;
1465 show(revs
, newlist
);
1466 show_early_output
= NULL
;
1468 if (revs
->cherry_pick
|| revs
->cherry_mark
)
1469 cherry_pick_list(newlist
, revs
);
1471 if (revs
->left_only
|| revs
->right_only
)
1472 limit_left_right(newlist
, revs
);
1474 if (revs
->ancestry_path
)
1475 limit_to_ancestry(revs
->ancestry_path_bottoms
, newlist
);
1478 * Check if any commits have become TREESAME by some of their parents
1479 * becoming UNINTERESTING.
1481 if (limiting_can_increase_treesame(revs
)) {
1482 struct commit_list
*list
= NULL
;
1483 for (list
= newlist
; list
; list
= list
->next
) {
1484 struct commit
*c
= list
->item
;
1485 if (c
->object
.flags
& (UNINTERESTING
| TREESAME
))
1487 update_treesame(revs
, c
);
1491 free_commit_list(original_list
);
1492 revs
->commits
= newlist
;
1497 * Add an entry to refs->cmdline with the specified information.
1500 static void add_rev_cmdline(struct rev_info
*revs
,
1501 struct object
*item
,
1506 struct rev_cmdline_info
*info
= &revs
->cmdline
;
1507 unsigned int nr
= info
->nr
;
1509 ALLOC_GROW(info
->rev
, nr
+ 1, info
->alloc
);
1510 info
->rev
[nr
].item
= item
;
1511 info
->rev
[nr
].name
= xstrdup(name
);
1512 info
->rev
[nr
].whence
= whence
;
1513 info
->rev
[nr
].flags
= flags
;
1517 static void add_rev_cmdline_list(struct rev_info
*revs
,
1518 struct commit_list
*commit_list
,
1522 while (commit_list
) {
1523 struct object
*object
= &commit_list
->item
->object
;
1524 add_rev_cmdline(revs
, object
, oid_to_hex(&object
->oid
),
1526 commit_list
= commit_list
->next
;
1530 int ref_excluded(const struct ref_exclusions
*exclusions
, const char *path
)
1532 const char *stripped_path
= strip_namespace(path
);
1533 struct string_list_item
*item
;
1535 for_each_string_list_item(item
, &exclusions
->excluded_refs
) {
1536 if (!wildmatch(item
->string
, path
, 0))
1540 if (ref_is_hidden(stripped_path
, path
, &exclusions
->hidden_refs
))
1546 void init_ref_exclusions(struct ref_exclusions
*exclusions
)
1548 struct ref_exclusions blank
= REF_EXCLUSIONS_INIT
;
1549 memcpy(exclusions
, &blank
, sizeof(*exclusions
));
1552 void clear_ref_exclusions(struct ref_exclusions
*exclusions
)
1554 string_list_clear(&exclusions
->excluded_refs
, 0);
1555 string_list_clear(&exclusions
->hidden_refs
, 0);
1556 exclusions
->hidden_refs_configured
= 0;
1559 void add_ref_exclusion(struct ref_exclusions
*exclusions
, const char *exclude
)
1561 string_list_append(&exclusions
->excluded_refs
, exclude
);
1564 struct exclude_hidden_refs_cb
{
1565 struct ref_exclusions
*exclusions
;
1566 const char *section
;
1569 static int hide_refs_config(const char *var
, const char *value
, void *cb_data
)
1571 struct exclude_hidden_refs_cb
*cb
= cb_data
;
1572 cb
->exclusions
->hidden_refs_configured
= 1;
1573 return parse_hide_refs_config(var
, value
, cb
->section
,
1574 &cb
->exclusions
->hidden_refs
);
1577 void exclude_hidden_refs(struct ref_exclusions
*exclusions
, const char *section
)
1579 struct exclude_hidden_refs_cb cb
;
1581 if (strcmp(section
, "fetch") && strcmp(section
, "receive") &&
1582 strcmp(section
, "uploadpack"))
1583 die(_("unsupported section for hidden refs: %s"), section
);
1585 if (exclusions
->hidden_refs_configured
)
1586 die(_("--exclude-hidden= passed more than once"));
1588 cb
.exclusions
= exclusions
;
1589 cb
.section
= section
;
1591 git_config(hide_refs_config
, &cb
);
1594 struct all_refs_cb
{
1596 int warned_bad_reflog
;
1597 struct rev_info
*all_revs
;
1598 const char *name_for_errormsg
;
1599 struct worktree
*wt
;
1602 static int handle_one_ref(const char *path
, const struct object_id
*oid
,
1606 struct all_refs_cb
*cb
= cb_data
;
1607 struct object
*object
;
1609 if (ref_excluded(&cb
->all_revs
->ref_excludes
, path
))
1612 object
= get_reference(cb
->all_revs
, path
, oid
, cb
->all_flags
);
1613 add_rev_cmdline(cb
->all_revs
, object
, path
, REV_CMD_REF
, cb
->all_flags
);
1614 add_pending_object(cb
->all_revs
, object
, path
);
1618 static void init_all_refs_cb(struct all_refs_cb
*cb
, struct rev_info
*revs
,
1621 cb
->all_revs
= revs
;
1622 cb
->all_flags
= flags
;
1623 revs
->rev_input_given
= 1;
1627 static void handle_refs(struct ref_store
*refs
,
1628 struct rev_info
*revs
, unsigned flags
,
1629 int (*for_each
)(struct ref_store
*, each_ref_fn
, void *))
1631 struct all_refs_cb cb
;
1634 /* this could happen with uninitialized submodules */
1638 init_all_refs_cb(&cb
, revs
, flags
);
1639 for_each(refs
, handle_one_ref
, &cb
);
1642 static void handle_one_reflog_commit(struct object_id
*oid
, void *cb_data
)
1644 struct all_refs_cb
*cb
= cb_data
;
1645 if (!is_null_oid(oid
)) {
1646 struct object
*o
= parse_object(cb
->all_revs
->repo
, oid
);
1648 o
->flags
|= cb
->all_flags
;
1649 /* ??? CMDLINEFLAGS ??? */
1650 add_pending_object(cb
->all_revs
, o
, "");
1652 else if (!cb
->warned_bad_reflog
) {
1653 warning("reflog of '%s' references pruned commits",
1654 cb
->name_for_errormsg
);
1655 cb
->warned_bad_reflog
= 1;
1660 static int handle_one_reflog_ent(struct object_id
*ooid
, struct object_id
*noid
,
1661 const char *email UNUSED
,
1662 timestamp_t timestamp UNUSED
,
1664 const char *message UNUSED
,
1667 handle_one_reflog_commit(ooid
, cb_data
);
1668 handle_one_reflog_commit(noid
, cb_data
);
1672 static int handle_one_reflog(const char *refname_in_wt
,
1673 const struct object_id
*oid UNUSED
,
1674 int flag UNUSED
, void *cb_data
)
1676 struct all_refs_cb
*cb
= cb_data
;
1677 struct strbuf refname
= STRBUF_INIT
;
1679 cb
->warned_bad_reflog
= 0;
1680 strbuf_worktree_ref(cb
->wt
, &refname
, refname_in_wt
);
1681 cb
->name_for_errormsg
= refname
.buf
;
1682 refs_for_each_reflog_ent(get_main_ref_store(the_repository
),
1684 handle_one_reflog_ent
, cb_data
);
1685 strbuf_release(&refname
);
1689 static void add_other_reflogs_to_pending(struct all_refs_cb
*cb
)
1691 struct worktree
**worktrees
, **p
;
1693 worktrees
= get_worktrees();
1694 for (p
= worktrees
; *p
; p
++) {
1695 struct worktree
*wt
= *p
;
1701 refs_for_each_reflog(get_worktree_ref_store(wt
),
1705 free_worktrees(worktrees
);
1708 void add_reflogs_to_pending(struct rev_info
*revs
, unsigned flags
)
1710 struct all_refs_cb cb
;
1713 cb
.all_flags
= flags
;
1715 for_each_reflog(handle_one_reflog
, &cb
);
1717 if (!revs
->single_worktree
)
1718 add_other_reflogs_to_pending(&cb
);
1721 static void add_cache_tree(struct cache_tree
*it
, struct rev_info
*revs
,
1722 struct strbuf
*path
, unsigned int flags
)
1724 size_t baselen
= path
->len
;
1727 if (it
->entry_count
>= 0) {
1728 struct tree
*tree
= lookup_tree(revs
->repo
, &it
->oid
);
1729 tree
->object
.flags
|= flags
;
1730 add_pending_object_with_path(revs
, &tree
->object
, "",
1734 for (i
= 0; i
< it
->subtree_nr
; i
++) {
1735 struct cache_tree_sub
*sub
= it
->down
[i
];
1736 strbuf_addf(path
, "%s%s", baselen
? "/" : "", sub
->name
);
1737 add_cache_tree(sub
->cache_tree
, revs
, path
, flags
);
1738 strbuf_setlen(path
, baselen
);
1743 static void add_resolve_undo_to_pending(struct index_state
*istate
, struct rev_info
*revs
)
1745 struct string_list_item
*item
;
1746 struct string_list
*resolve_undo
= istate
->resolve_undo
;
1751 for_each_string_list_item(item
, resolve_undo
) {
1752 const char *path
= item
->string
;
1753 struct resolve_undo_info
*ru
= item
->util
;
1758 for (i
= 0; i
< 3; i
++) {
1761 if (!ru
->mode
[i
] || !S_ISREG(ru
->mode
[i
]))
1764 blob
= lookup_blob(revs
->repo
, &ru
->oid
[i
]);
1766 warning(_("resolve-undo records `%s` which is missing"),
1767 oid_to_hex(&ru
->oid
[i
]));
1770 add_pending_object_with_path(revs
, &blob
->object
, "",
1776 static void do_add_index_objects_to_pending(struct rev_info
*revs
,
1777 struct index_state
*istate
,
1782 /* TODO: audit for interaction with sparse-index. */
1783 ensure_full_index(istate
);
1784 for (i
= 0; i
< istate
->cache_nr
; i
++) {
1785 struct cache_entry
*ce
= istate
->cache
[i
];
1788 if (S_ISGITLINK(ce
->ce_mode
))
1791 blob
= lookup_blob(revs
->repo
, &ce
->oid
);
1793 die("unable to add index blob to traversal");
1794 blob
->object
.flags
|= flags
;
1795 add_pending_object_with_path(revs
, &blob
->object
, "",
1796 ce
->ce_mode
, ce
->name
);
1799 if (istate
->cache_tree
) {
1800 struct strbuf path
= STRBUF_INIT
;
1801 add_cache_tree(istate
->cache_tree
, revs
, &path
, flags
);
1802 strbuf_release(&path
);
1805 add_resolve_undo_to_pending(istate
, revs
);
1808 void add_index_objects_to_pending(struct rev_info
*revs
, unsigned int flags
)
1810 struct worktree
**worktrees
, **p
;
1812 repo_read_index(revs
->repo
);
1813 do_add_index_objects_to_pending(revs
, revs
->repo
->index
, flags
);
1815 if (revs
->single_worktree
)
1818 worktrees
= get_worktrees();
1819 for (p
= worktrees
; *p
; p
++) {
1820 struct worktree
*wt
= *p
;
1821 struct index_state istate
= INDEX_STATE_INIT(revs
->repo
);
1824 continue; /* current index already taken care of */
1826 if (read_index_from(&istate
,
1827 worktree_git_path(wt
, "index"),
1828 get_worktree_git_dir(wt
)) > 0)
1829 do_add_index_objects_to_pending(revs
, &istate
, flags
);
1830 discard_index(&istate
);
1832 free_worktrees(worktrees
);
1835 struct add_alternate_refs_data
{
1836 struct rev_info
*revs
;
1840 static void add_one_alternate_ref(const struct object_id
*oid
,
1843 const char *name
= ".alternate";
1844 struct add_alternate_refs_data
*data
= vdata
;
1847 obj
= get_reference(data
->revs
, name
, oid
, data
->flags
);
1848 add_rev_cmdline(data
->revs
, obj
, name
, REV_CMD_REV
, data
->flags
);
1849 add_pending_object(data
->revs
, obj
, name
);
1852 static void add_alternate_refs_to_pending(struct rev_info
*revs
,
1855 struct add_alternate_refs_data data
;
1858 for_each_alternate_ref(add_one_alternate_ref
, &data
);
1861 static int add_parents_only(struct rev_info
*revs
, const char *arg_
, int flags
,
1864 struct object_id oid
;
1866 struct commit
*commit
;
1867 struct commit_list
*parents
;
1869 const char *arg
= arg_
;
1872 flags
^= UNINTERESTING
| BOTTOM
;
1875 if (get_oid_committish(arg
, &oid
))
1878 it
= get_reference(revs
, arg
, &oid
, 0);
1879 if (!it
&& revs
->ignore_missing
)
1881 if (it
->type
!= OBJ_TAG
)
1883 if (!((struct tag
*)it
)->tagged
)
1885 oidcpy(&oid
, &((struct tag
*)it
)->tagged
->oid
);
1887 if (it
->type
!= OBJ_COMMIT
)
1889 commit
= (struct commit
*)it
;
1890 if (exclude_parent
&&
1891 exclude_parent
> commit_list_count(commit
->parents
))
1893 for (parents
= commit
->parents
, parent_number
= 1;
1895 parents
= parents
->next
, parent_number
++) {
1896 if (exclude_parent
&& parent_number
!= exclude_parent
)
1899 it
= &parents
->item
->object
;
1901 add_rev_cmdline(revs
, it
, arg_
, REV_CMD_PARENTS_ONLY
, flags
);
1902 add_pending_object(revs
, it
, arg
);
1907 void repo_init_revisions(struct repository
*r
,
1908 struct rev_info
*revs
,
1911 struct rev_info blank
= REV_INFO_INIT
;
1912 memcpy(revs
, &blank
, sizeof(*revs
));
1915 revs
->pruning
.repo
= r
;
1916 revs
->pruning
.add_remove
= file_add_remove
;
1917 revs
->pruning
.change
= file_change
;
1918 revs
->pruning
.change_fn_data
= revs
;
1919 revs
->prefix
= prefix
;
1921 grep_init(&revs
->grep_filter
, revs
->repo
);
1922 revs
->grep_filter
.status_only
= 1;
1924 repo_diff_setup(revs
->repo
, &revs
->diffopt
);
1925 if (prefix
&& !revs
->diffopt
.prefix
) {
1926 revs
->diffopt
.prefix
= prefix
;
1927 revs
->diffopt
.prefix_length
= strlen(prefix
);
1930 init_display_notes(&revs
->notes_opt
);
1931 list_objects_filter_init(&revs
->filter
);
1932 init_ref_exclusions(&revs
->ref_excludes
);
1935 static void add_pending_commit_list(struct rev_info
*revs
,
1936 struct commit_list
*commit_list
,
1939 while (commit_list
) {
1940 struct object
*object
= &commit_list
->item
->object
;
1941 object
->flags
|= flags
;
1942 add_pending_object(revs
, object
, oid_to_hex(&object
->oid
));
1943 commit_list
= commit_list
->next
;
1947 static void prepare_show_merge(struct rev_info
*revs
)
1949 struct commit_list
*bases
;
1950 struct commit
*head
, *other
;
1951 struct object_id oid
;
1952 const char **prune
= NULL
;
1953 int i
, prune_num
= 1; /* counting terminating NULL */
1954 struct index_state
*istate
= revs
->repo
->index
;
1956 if (get_oid("HEAD", &oid
))
1957 die("--merge without HEAD?");
1958 head
= lookup_commit_or_die(&oid
, "HEAD");
1959 if (get_oid("MERGE_HEAD", &oid
))
1960 die("--merge without MERGE_HEAD?");
1961 other
= lookup_commit_or_die(&oid
, "MERGE_HEAD");
1962 add_pending_object(revs
, &head
->object
, "HEAD");
1963 add_pending_object(revs
, &other
->object
, "MERGE_HEAD");
1964 bases
= get_merge_bases(head
, other
);
1965 add_rev_cmdline_list(revs
, bases
, REV_CMD_MERGE_BASE
, UNINTERESTING
| BOTTOM
);
1966 add_pending_commit_list(revs
, bases
, UNINTERESTING
| BOTTOM
);
1967 free_commit_list(bases
);
1968 head
->object
.flags
|= SYMMETRIC_LEFT
;
1970 if (!istate
->cache_nr
)
1971 repo_read_index(revs
->repo
);
1972 for (i
= 0; i
< istate
->cache_nr
; i
++) {
1973 const struct cache_entry
*ce
= istate
->cache
[i
];
1976 if (ce_path_match(istate
, ce
, &revs
->prune_data
, NULL
)) {
1978 REALLOC_ARRAY(prune
, prune_num
);
1979 prune
[prune_num
-2] = ce
->name
;
1980 prune
[prune_num
-1] = NULL
;
1982 while ((i
+1 < istate
->cache_nr
) &&
1983 ce_same_name(ce
, istate
->cache
[i
+1]))
1986 clear_pathspec(&revs
->prune_data
);
1987 parse_pathspec(&revs
->prune_data
, PATHSPEC_ALL_MAGIC
& ~PATHSPEC_LITERAL
,
1988 PATHSPEC_PREFER_FULL
| PATHSPEC_LITERAL_PATH
, "", prune
);
1992 static int dotdot_missing(const char *arg
, char *dotdot
,
1993 struct rev_info
*revs
, int symmetric
)
1995 if (revs
->ignore_missing
)
1997 /* de-munge so we report the full argument */
2000 ? "Invalid symmetric difference expression %s"
2001 : "Invalid revision range %s", arg
);
2004 static int handle_dotdot_1(const char *arg
, char *dotdot
,
2005 struct rev_info
*revs
, int flags
,
2006 int cant_be_filename
,
2007 struct object_context
*a_oc
,
2008 struct object_context
*b_oc
)
2010 const char *a_name
, *b_name
;
2011 struct object_id a_oid
, b_oid
;
2012 struct object
*a_obj
, *b_obj
;
2013 unsigned int a_flags
, b_flags
;
2015 unsigned int flags_exclude
= flags
^ (UNINTERESTING
| BOTTOM
);
2016 unsigned int oc_flags
= GET_OID_COMMITTISH
| GET_OID_RECORD_PATH
;
2022 b_name
= dotdot
+ 2;
2023 if (*b_name
== '.') {
2030 if (get_oid_with_context(revs
->repo
, a_name
, oc_flags
, &a_oid
, a_oc
) ||
2031 get_oid_with_context(revs
->repo
, b_name
, oc_flags
, &b_oid
, b_oc
))
2034 if (!cant_be_filename
) {
2036 verify_non_filename(revs
->prefix
, arg
);
2040 a_obj
= parse_object(revs
->repo
, &a_oid
);
2041 b_obj
= parse_object(revs
->repo
, &b_oid
);
2042 if (!a_obj
|| !b_obj
)
2043 return dotdot_missing(arg
, dotdot
, revs
, symmetric
);
2048 a_flags
= flags_exclude
;
2050 /* A...B -- find merge bases between the two */
2051 struct commit
*a
, *b
;
2052 struct commit_list
*exclude
;
2054 a
= lookup_commit_reference(revs
->repo
, &a_obj
->oid
);
2055 b
= lookup_commit_reference(revs
->repo
, &b_obj
->oid
);
2057 return dotdot_missing(arg
, dotdot
, revs
, symmetric
);
2059 exclude
= get_merge_bases(a
, b
);
2060 add_rev_cmdline_list(revs
, exclude
, REV_CMD_MERGE_BASE
,
2062 add_pending_commit_list(revs
, exclude
, flags_exclude
);
2063 free_commit_list(exclude
);
2066 a_flags
= flags
| SYMMETRIC_LEFT
;
2069 a_obj
->flags
|= a_flags
;
2070 b_obj
->flags
|= b_flags
;
2071 add_rev_cmdline(revs
, a_obj
, a_name
, REV_CMD_LEFT
, a_flags
);
2072 add_rev_cmdline(revs
, b_obj
, b_name
, REV_CMD_RIGHT
, b_flags
);
2073 add_pending_object_with_path(revs
, a_obj
, a_name
, a_oc
->mode
, a_oc
->path
);
2074 add_pending_object_with_path(revs
, b_obj
, b_name
, b_oc
->mode
, b_oc
->path
);
2078 static int handle_dotdot(const char *arg
,
2079 struct rev_info
*revs
, int flags
,
2080 int cant_be_filename
)
2082 struct object_context a_oc
, b_oc
;
2083 char *dotdot
= strstr(arg
, "..");
2089 memset(&a_oc
, 0, sizeof(a_oc
));
2090 memset(&b_oc
, 0, sizeof(b_oc
));
2093 ret
= handle_dotdot_1(arg
, dotdot
, revs
, flags
, cant_be_filename
,
2103 static int handle_revision_arg_1(const char *arg_
, struct rev_info
*revs
, int flags
, unsigned revarg_opt
)
2105 struct object_context oc
;
2107 struct object
*object
;
2108 struct object_id oid
;
2110 const char *arg
= arg_
;
2111 int cant_be_filename
= revarg_opt
& REVARG_CANNOT_BE_FILENAME
;
2112 unsigned get_sha1_flags
= GET_OID_RECORD_PATH
;
2114 flags
= flags
& UNINTERESTING
? flags
| BOTTOM
: flags
& ~BOTTOM
;
2116 if (!cant_be_filename
&& !strcmp(arg
, "..")) {
2118 * Just ".."? That is not a range but the
2119 * pathspec for the parent directory.
2124 if (!handle_dotdot(arg
, revs
, flags
, revarg_opt
))
2127 mark
= strstr(arg
, "^@");
2128 if (mark
&& !mark
[2]) {
2130 if (add_parents_only(revs
, arg
, flags
, 0))
2134 mark
= strstr(arg
, "^!");
2135 if (mark
&& !mark
[2]) {
2137 if (!add_parents_only(revs
, arg
, flags
^ (UNINTERESTING
| BOTTOM
), 0))
2140 mark
= strstr(arg
, "^-");
2142 int exclude_parent
= 1;
2145 if (strtol_i(mark
+ 2, 10, &exclude_parent
) ||
2151 if (!add_parents_only(revs
, arg
, flags
^ (UNINTERESTING
| BOTTOM
), exclude_parent
))
2157 local_flags
= UNINTERESTING
| BOTTOM
;
2161 if (revarg_opt
& REVARG_COMMITTISH
)
2162 get_sha1_flags
|= GET_OID_COMMITTISH
;
2164 if (get_oid_with_context(revs
->repo
, arg
, get_sha1_flags
, &oid
, &oc
))
2165 return revs
->ignore_missing
? 0 : -1;
2166 if (!cant_be_filename
)
2167 verify_non_filename(revs
->prefix
, arg
);
2168 object
= get_reference(revs
, arg
, &oid
, flags
^ local_flags
);
2170 return revs
->ignore_missing
? 0 : -1;
2171 add_rev_cmdline(revs
, object
, arg_
, REV_CMD_REV
, flags
^ local_flags
);
2172 add_pending_object_with_path(revs
, object
, arg
, oc
.mode
, oc
.path
);
2177 int handle_revision_arg(const char *arg
, struct rev_info
*revs
, int flags
, unsigned revarg_opt
)
2179 int ret
= handle_revision_arg_1(arg
, revs
, flags
, revarg_opt
);
2181 revs
->rev_input_given
= 1;
2185 static void read_pathspec_from_stdin(struct strbuf
*sb
,
2186 struct strvec
*prune
)
2188 while (strbuf_getline(sb
, stdin
) != EOF
)
2189 strvec_push(prune
, sb
->buf
);
2192 static void read_revisions_from_stdin(struct rev_info
*revs
,
2193 struct strvec
*prune
)
2196 int seen_dashdash
= 0;
2199 save_warning
= warn_on_object_refname_ambiguity
;
2200 warn_on_object_refname_ambiguity
= 0;
2202 strbuf_init(&sb
, 1000);
2203 while (strbuf_getline(&sb
, stdin
) != EOF
) {
2207 if (sb
.buf
[0] == '-') {
2208 if (len
== 2 && sb
.buf
[1] == '-') {
2212 die("options not supported in --stdin mode");
2214 if (handle_revision_arg(sb
.buf
, revs
, 0,
2215 REVARG_CANNOT_BE_FILENAME
))
2216 die("bad revision '%s'", sb
.buf
);
2219 read_pathspec_from_stdin(&sb
, prune
);
2221 strbuf_release(&sb
);
2222 warn_on_object_refname_ambiguity
= save_warning
;
2225 static void add_grep(struct rev_info
*revs
, const char *ptn
, enum grep_pat_token what
)
2227 append_grep_pattern(&revs
->grep_filter
, ptn
, "command line", 0, what
);
2230 static void add_header_grep(struct rev_info
*revs
, enum grep_header_field field
, const char *pattern
)
2232 append_header_grep_pattern(&revs
->grep_filter
, field
, pattern
);
2235 static void add_message_grep(struct rev_info
*revs
, const char *pattern
)
2237 add_grep(revs
, pattern
, GREP_PATTERN_BODY
);
2240 static int handle_revision_opt(struct rev_info
*revs
, int argc
, const char **argv
,
2241 int *unkc
, const char **unkv
,
2242 const struct setup_revision_opt
* opt
)
2244 const char *arg
= argv
[0];
2245 const char *optarg
= NULL
;
2247 const unsigned hexsz
= the_hash_algo
->hexsz
;
2249 /* pseudo revision arguments */
2250 if (!strcmp(arg
, "--all") || !strcmp(arg
, "--branches") ||
2251 !strcmp(arg
, "--tags") || !strcmp(arg
, "--remotes") ||
2252 !strcmp(arg
, "--reflog") || !strcmp(arg
, "--not") ||
2253 !strcmp(arg
, "--no-walk") || !strcmp(arg
, "--do-walk") ||
2254 !strcmp(arg
, "--bisect") || starts_with(arg
, "--glob=") ||
2255 !strcmp(arg
, "--indexed-objects") ||
2256 !strcmp(arg
, "--alternate-refs") ||
2257 starts_with(arg
, "--exclude=") || starts_with(arg
, "--exclude-hidden=") ||
2258 starts_with(arg
, "--branches=") || starts_with(arg
, "--tags=") ||
2259 starts_with(arg
, "--remotes=") || starts_with(arg
, "--no-walk="))
2261 unkv
[(*unkc
)++] = arg
;
2265 if ((argcount
= parse_long_opt("max-count", argv
, &optarg
))) {
2266 revs
->max_count
= atoi(optarg
);
2269 } else if ((argcount
= parse_long_opt("skip", argv
, &optarg
))) {
2270 revs
->skip_count
= atoi(optarg
);
2272 } else if ((*arg
== '-') && isdigit(arg
[1])) {
2273 /* accept -<digit>, like traditional "head" */
2274 if (strtol_i(arg
+ 1, 10, &revs
->max_count
) < 0 ||
2275 revs
->max_count
< 0)
2276 die("'%s': not a non-negative integer", arg
+ 1);
2278 } else if (!strcmp(arg
, "-n")) {
2280 return error("-n requires an argument");
2281 revs
->max_count
= atoi(argv
[1]);
2284 } else if (skip_prefix(arg
, "-n", &optarg
)) {
2285 revs
->max_count
= atoi(optarg
);
2287 } else if ((argcount
= parse_long_opt("max-age", argv
, &optarg
))) {
2288 revs
->max_age
= atoi(optarg
);
2290 } else if ((argcount
= parse_long_opt("since", argv
, &optarg
))) {
2291 revs
->max_age
= approxidate(optarg
);
2293 } else if ((argcount
= parse_long_opt("since-as-filter", argv
, &optarg
))) {
2294 revs
->max_age_as_filter
= approxidate(optarg
);
2296 } else if ((argcount
= parse_long_opt("after", argv
, &optarg
))) {
2297 revs
->max_age
= approxidate(optarg
);
2299 } else if ((argcount
= parse_long_opt("min-age", argv
, &optarg
))) {
2300 revs
->min_age
= atoi(optarg
);
2302 } else if ((argcount
= parse_long_opt("before", argv
, &optarg
))) {
2303 revs
->min_age
= approxidate(optarg
);
2305 } else if ((argcount
= parse_long_opt("until", argv
, &optarg
))) {
2306 revs
->min_age
= approxidate(optarg
);
2308 } else if (!strcmp(arg
, "--first-parent")) {
2309 revs
->first_parent_only
= 1;
2310 } else if (!strcmp(arg
, "--exclude-first-parent-only")) {
2311 revs
->exclude_first_parent_only
= 1;
2312 } else if (!strcmp(arg
, "--ancestry-path")) {
2313 revs
->ancestry_path
= 1;
2314 revs
->simplify_history
= 0;
2316 revs
->ancestry_path_implicit_bottoms
= 1;
2317 } else if (skip_prefix(arg
, "--ancestry-path=", &optarg
)) {
2319 struct object_id oid
;
2320 const char *msg
= _("could not get commit for ancestry-path argument %s");
2322 revs
->ancestry_path
= 1;
2323 revs
->simplify_history
= 0;
2326 if (repo_get_oid_committish(revs
->repo
, optarg
, &oid
))
2327 return error(msg
, optarg
);
2328 get_reference(revs
, optarg
, &oid
, ANCESTRY_PATH
);
2329 c
= lookup_commit_reference(revs
->repo
, &oid
);
2331 return error(msg
, optarg
);
2332 commit_list_insert(c
, &revs
->ancestry_path_bottoms
);
2333 } else if (!strcmp(arg
, "-g") || !strcmp(arg
, "--walk-reflogs")) {
2334 init_reflog_walk(&revs
->reflog_info
);
2335 } else if (!strcmp(arg
, "--default")) {
2337 return error("bad --default argument");
2338 revs
->def
= argv
[1];
2340 } else if (!strcmp(arg
, "--merge")) {
2341 revs
->show_merge
= 1;
2342 } else if (!strcmp(arg
, "--topo-order")) {
2343 revs
->sort_order
= REV_SORT_IN_GRAPH_ORDER
;
2344 revs
->topo_order
= 1;
2345 } else if (!strcmp(arg
, "--simplify-merges")) {
2346 revs
->simplify_merges
= 1;
2347 revs
->topo_order
= 1;
2348 revs
->rewrite_parents
= 1;
2349 revs
->simplify_history
= 0;
2351 } else if (!strcmp(arg
, "--simplify-by-decoration")) {
2352 revs
->simplify_merges
= 1;
2353 revs
->topo_order
= 1;
2354 revs
->rewrite_parents
= 1;
2355 revs
->simplify_history
= 0;
2356 revs
->simplify_by_decoration
= 1;
2359 } else if (!strcmp(arg
, "--date-order")) {
2360 revs
->sort_order
= REV_SORT_BY_COMMIT_DATE
;
2361 revs
->topo_order
= 1;
2362 } else if (!strcmp(arg
, "--author-date-order")) {
2363 revs
->sort_order
= REV_SORT_BY_AUTHOR_DATE
;
2364 revs
->topo_order
= 1;
2365 } else if (!strcmp(arg
, "--early-output")) {
2366 revs
->early_output
= 100;
2367 revs
->topo_order
= 1;
2368 } else if (skip_prefix(arg
, "--early-output=", &optarg
)) {
2369 if (strtoul_ui(optarg
, 10, &revs
->early_output
) < 0)
2370 die("'%s': not a non-negative integer", optarg
);
2371 revs
->topo_order
= 1;
2372 } else if (!strcmp(arg
, "--parents")) {
2373 revs
->rewrite_parents
= 1;
2374 revs
->print_parents
= 1;
2375 } else if (!strcmp(arg
, "--dense")) {
2377 } else if (!strcmp(arg
, "--sparse")) {
2379 } else if (!strcmp(arg
, "--in-commit-order")) {
2380 revs
->tree_blobs_in_commit_order
= 1;
2381 } else if (!strcmp(arg
, "--remove-empty")) {
2382 revs
->remove_empty_trees
= 1;
2383 } else if (!strcmp(arg
, "--merges")) {
2384 revs
->min_parents
= 2;
2385 } else if (!strcmp(arg
, "--no-merges")) {
2386 revs
->max_parents
= 1;
2387 } else if (skip_prefix(arg
, "--min-parents=", &optarg
)) {
2388 revs
->min_parents
= atoi(optarg
);
2389 } else if (!strcmp(arg
, "--no-min-parents")) {
2390 revs
->min_parents
= 0;
2391 } else if (skip_prefix(arg
, "--max-parents=", &optarg
)) {
2392 revs
->max_parents
= atoi(optarg
);
2393 } else if (!strcmp(arg
, "--no-max-parents")) {
2394 revs
->max_parents
= -1;
2395 } else if (!strcmp(arg
, "--boundary")) {
2397 } else if (!strcmp(arg
, "--left-right")) {
2398 revs
->left_right
= 1;
2399 } else if (!strcmp(arg
, "--left-only")) {
2400 if (revs
->right_only
)
2401 die("--left-only is incompatible with --right-only"
2403 revs
->left_only
= 1;
2404 } else if (!strcmp(arg
, "--right-only")) {
2405 if (revs
->left_only
)
2406 die(_("options '%s' and '%s' cannot be used together"), "--right-only", "--left-only");
2407 revs
->right_only
= 1;
2408 } else if (!strcmp(arg
, "--cherry")) {
2409 if (revs
->left_only
)
2410 die(_("options '%s' and '%s' cannot be used together"), "--cherry", "--left-only");
2411 revs
->cherry_mark
= 1;
2412 revs
->right_only
= 1;
2413 revs
->max_parents
= 1;
2415 } else if (!strcmp(arg
, "--count")) {
2417 } else if (!strcmp(arg
, "--cherry-mark")) {
2418 if (revs
->cherry_pick
)
2419 die(_("options '%s' and '%s' cannot be used together"), "--cherry-mark", "--cherry-pick");
2420 revs
->cherry_mark
= 1;
2421 revs
->limited
= 1; /* needs limit_list() */
2422 } else if (!strcmp(arg
, "--cherry-pick")) {
2423 if (revs
->cherry_mark
)
2424 die(_("options '%s' and '%s' cannot be used together"), "--cherry-pick", "--cherry-mark");
2425 revs
->cherry_pick
= 1;
2427 } else if (!strcmp(arg
, "--objects")) {
2428 revs
->tag_objects
= 1;
2429 revs
->tree_objects
= 1;
2430 revs
->blob_objects
= 1;
2431 } else if (!strcmp(arg
, "--objects-edge")) {
2432 revs
->tag_objects
= 1;
2433 revs
->tree_objects
= 1;
2434 revs
->blob_objects
= 1;
2435 revs
->edge_hint
= 1;
2436 } else if (!strcmp(arg
, "--objects-edge-aggressive")) {
2437 revs
->tag_objects
= 1;
2438 revs
->tree_objects
= 1;
2439 revs
->blob_objects
= 1;
2440 revs
->edge_hint
= 1;
2441 revs
->edge_hint_aggressive
= 1;
2442 } else if (!strcmp(arg
, "--verify-objects")) {
2443 revs
->tag_objects
= 1;
2444 revs
->tree_objects
= 1;
2445 revs
->blob_objects
= 1;
2446 revs
->verify_objects
= 1;
2447 disable_commit_graph(revs
->repo
);
2448 } else if (!strcmp(arg
, "--unpacked")) {
2450 } else if (starts_with(arg
, "--unpacked=")) {
2451 die(_("--unpacked=<packfile> no longer supported"));
2452 } else if (!strcmp(arg
, "--no-kept-objects")) {
2453 revs
->no_kept_objects
= 1;
2454 revs
->keep_pack_cache_flags
|= IN_CORE_KEEP_PACKS
;
2455 revs
->keep_pack_cache_flags
|= ON_DISK_KEEP_PACKS
;
2456 } else if (skip_prefix(arg
, "--no-kept-objects=", &optarg
)) {
2457 revs
->no_kept_objects
= 1;
2458 if (!strcmp(optarg
, "in-core"))
2459 revs
->keep_pack_cache_flags
|= IN_CORE_KEEP_PACKS
;
2460 if (!strcmp(optarg
, "on-disk"))
2461 revs
->keep_pack_cache_flags
|= ON_DISK_KEEP_PACKS
;
2462 } else if (!strcmp(arg
, "-r")) {
2464 revs
->diffopt
.flags
.recursive
= 1;
2465 } else if (!strcmp(arg
, "-t")) {
2467 revs
->diffopt
.flags
.recursive
= 1;
2468 revs
->diffopt
.flags
.tree_in_recursive
= 1;
2469 } else if ((argcount
= diff_merges_parse_opts(revs
, argv
))) {
2471 } else if (!strcmp(arg
, "-v")) {
2472 revs
->verbose_header
= 1;
2473 } else if (!strcmp(arg
, "--pretty")) {
2474 revs
->verbose_header
= 1;
2475 revs
->pretty_given
= 1;
2476 get_commit_format(NULL
, revs
);
2477 } else if (skip_prefix(arg
, "--pretty=", &optarg
) ||
2478 skip_prefix(arg
, "--format=", &optarg
)) {
2480 * Detached form ("--pretty X" as opposed to "--pretty=X")
2481 * not allowed, since the argument is optional.
2483 revs
->verbose_header
= 1;
2484 revs
->pretty_given
= 1;
2485 get_commit_format(optarg
, revs
);
2486 } else if (!strcmp(arg
, "--expand-tabs")) {
2487 revs
->expand_tabs_in_log
= 8;
2488 } else if (!strcmp(arg
, "--no-expand-tabs")) {
2489 revs
->expand_tabs_in_log
= 0;
2490 } else if (skip_prefix(arg
, "--expand-tabs=", &arg
)) {
2492 if (strtol_i(arg
, 10, &val
) < 0 || val
< 0)
2493 die("'%s': not a non-negative integer", arg
);
2494 revs
->expand_tabs_in_log
= val
;
2495 } else if (!strcmp(arg
, "--show-notes") || !strcmp(arg
, "--notes")) {
2496 enable_default_display_notes(&revs
->notes_opt
, &revs
->show_notes
);
2497 revs
->show_notes_given
= 1;
2498 } else if (!strcmp(arg
, "--show-signature")) {
2499 revs
->show_signature
= 1;
2500 } else if (!strcmp(arg
, "--no-show-signature")) {
2501 revs
->show_signature
= 0;
2502 } else if (!strcmp(arg
, "--show-linear-break")) {
2503 revs
->break_bar
= " ..........";
2504 revs
->track_linear
= 1;
2505 revs
->track_first_time
= 1;
2506 } else if (skip_prefix(arg
, "--show-linear-break=", &optarg
)) {
2507 revs
->break_bar
= xstrdup(optarg
);
2508 revs
->track_linear
= 1;
2509 revs
->track_first_time
= 1;
2510 } else if (skip_prefix(arg
, "--show-notes=", &optarg
) ||
2511 skip_prefix(arg
, "--notes=", &optarg
)) {
2512 if (starts_with(arg
, "--show-notes=") &&
2513 revs
->notes_opt
.use_default_notes
< 0)
2514 revs
->notes_opt
.use_default_notes
= 1;
2515 enable_ref_display_notes(&revs
->notes_opt
, &revs
->show_notes
, optarg
);
2516 revs
->show_notes_given
= 1;
2517 } else if (!strcmp(arg
, "--no-notes")) {
2518 disable_display_notes(&revs
->notes_opt
, &revs
->show_notes
);
2519 revs
->show_notes_given
= 1;
2520 } else if (!strcmp(arg
, "--standard-notes")) {
2521 revs
->show_notes_given
= 1;
2522 revs
->notes_opt
.use_default_notes
= 1;
2523 } else if (!strcmp(arg
, "--no-standard-notes")) {
2524 revs
->notes_opt
.use_default_notes
= 0;
2525 } else if (!strcmp(arg
, "--oneline")) {
2526 revs
->verbose_header
= 1;
2527 get_commit_format("oneline", revs
);
2528 revs
->pretty_given
= 1;
2529 revs
->abbrev_commit
= 1;
2530 } else if (!strcmp(arg
, "--graph")) {
2531 graph_clear(revs
->graph
);
2532 revs
->graph
= graph_init(revs
);
2533 } else if (!strcmp(arg
, "--no-graph")) {
2534 graph_clear(revs
->graph
);
2536 } else if (!strcmp(arg
, "--encode-email-headers")) {
2537 revs
->encode_email_headers
= 1;
2538 } else if (!strcmp(arg
, "--no-encode-email-headers")) {
2539 revs
->encode_email_headers
= 0;
2540 } else if (!strcmp(arg
, "--root")) {
2541 revs
->show_root_diff
= 1;
2542 } else if (!strcmp(arg
, "--no-commit-id")) {
2543 revs
->no_commit_id
= 1;
2544 } else if (!strcmp(arg
, "--always")) {
2545 revs
->always_show_header
= 1;
2546 } else if (!strcmp(arg
, "--no-abbrev")) {
2548 } else if (!strcmp(arg
, "--abbrev")) {
2549 revs
->abbrev
= DEFAULT_ABBREV
;
2550 } else if (skip_prefix(arg
, "--abbrev=", &optarg
)) {
2551 revs
->abbrev
= strtoul(optarg
, NULL
, 10);
2552 if (revs
->abbrev
< MINIMUM_ABBREV
)
2553 revs
->abbrev
= MINIMUM_ABBREV
;
2554 else if (revs
->abbrev
> hexsz
)
2555 revs
->abbrev
= hexsz
;
2556 } else if (!strcmp(arg
, "--abbrev-commit")) {
2557 revs
->abbrev_commit
= 1;
2558 revs
->abbrev_commit_given
= 1;
2559 } else if (!strcmp(arg
, "--no-abbrev-commit")) {
2560 revs
->abbrev_commit
= 0;
2561 } else if (!strcmp(arg
, "--full-diff")) {
2563 revs
->full_diff
= 1;
2564 } else if (!strcmp(arg
, "--show-pulls")) {
2565 revs
->show_pulls
= 1;
2566 } else if (!strcmp(arg
, "--full-history")) {
2567 revs
->simplify_history
= 0;
2568 } else if (!strcmp(arg
, "--relative-date")) {
2569 revs
->date_mode
.type
= DATE_RELATIVE
;
2570 revs
->date_mode_explicit
= 1;
2571 } else if ((argcount
= parse_long_opt("date", argv
, &optarg
))) {
2572 parse_date_format(optarg
, &revs
->date_mode
);
2573 revs
->date_mode_explicit
= 1;
2575 } else if (!strcmp(arg
, "--log-size")) {
2576 revs
->show_log_size
= 1;
2579 * Grepping the commit log
2581 else if ((argcount
= parse_long_opt("author", argv
, &optarg
))) {
2582 add_header_grep(revs
, GREP_HEADER_AUTHOR
, optarg
);
2584 } else if ((argcount
= parse_long_opt("committer", argv
, &optarg
))) {
2585 add_header_grep(revs
, GREP_HEADER_COMMITTER
, optarg
);
2587 } else if ((argcount
= parse_long_opt("grep-reflog", argv
, &optarg
))) {
2588 add_header_grep(revs
, GREP_HEADER_REFLOG
, optarg
);
2590 } else if ((argcount
= parse_long_opt("grep", argv
, &optarg
))) {
2591 add_message_grep(revs
, optarg
);
2593 } else if (!strcmp(arg
, "--basic-regexp")) {
2594 revs
->grep_filter
.pattern_type_option
= GREP_PATTERN_TYPE_BRE
;
2595 } else if (!strcmp(arg
, "--extended-regexp") || !strcmp(arg
, "-E")) {
2596 revs
->grep_filter
.pattern_type_option
= GREP_PATTERN_TYPE_ERE
;
2597 } else if (!strcmp(arg
, "--regexp-ignore-case") || !strcmp(arg
, "-i")) {
2598 revs
->grep_filter
.ignore_case
= 1;
2599 revs
->diffopt
.pickaxe_opts
|= DIFF_PICKAXE_IGNORE_CASE
;
2600 } else if (!strcmp(arg
, "--fixed-strings") || !strcmp(arg
, "-F")) {
2601 revs
->grep_filter
.pattern_type_option
= GREP_PATTERN_TYPE_FIXED
;
2602 } else if (!strcmp(arg
, "--perl-regexp") || !strcmp(arg
, "-P")) {
2603 revs
->grep_filter
.pattern_type_option
= GREP_PATTERN_TYPE_PCRE
;
2604 } else if (!strcmp(arg
, "--all-match")) {
2605 revs
->grep_filter
.all_match
= 1;
2606 } else if (!strcmp(arg
, "--invert-grep")) {
2607 revs
->grep_filter
.no_body_match
= 1;
2608 } else if ((argcount
= parse_long_opt("encoding", argv
, &optarg
))) {
2609 if (strcmp(optarg
, "none"))
2610 git_log_output_encoding
= xstrdup(optarg
);
2612 git_log_output_encoding
= "";
2614 } else if (!strcmp(arg
, "--reverse")) {
2616 } else if (!strcmp(arg
, "--children")) {
2617 revs
->children
.name
= "children";
2619 } else if (!strcmp(arg
, "--ignore-missing")) {
2620 revs
->ignore_missing
= 1;
2621 } else if (opt
&& opt
->allow_exclude_promisor_objects
&&
2622 !strcmp(arg
, "--exclude-promisor-objects")) {
2623 if (fetch_if_missing
)
2624 BUG("exclude_promisor_objects can only be used when fetch_if_missing is 0");
2625 revs
->exclude_promisor_objects
= 1;
2627 int opts
= diff_opt_parse(&revs
->diffopt
, argv
, argc
, revs
->prefix
);
2629 unkv
[(*unkc
)++] = arg
;
2636 void parse_revision_opt(struct rev_info
*revs
, struct parse_opt_ctx_t
*ctx
,
2637 const struct option
*options
,
2638 const char * const usagestr
[])
2640 int n
= handle_revision_opt(revs
, ctx
->argc
, ctx
->argv
,
2641 &ctx
->cpidx
, ctx
->out
, NULL
);
2643 error("unknown option `%s'", ctx
->argv
[0]);
2644 usage_with_options(usagestr
, options
);
2650 void revision_opts_finish(struct rev_info
*revs
)
2652 if (revs
->graph
&& revs
->track_linear
)
2653 die(_("options '%s' and '%s' cannot be used together"), "--show-linear-break", "--graph");
2656 revs
->topo_order
= 1;
2657 revs
->rewrite_parents
= 1;
2661 static int for_each_bisect_ref(struct ref_store
*refs
, each_ref_fn fn
,
2662 void *cb_data
, const char *term
)
2664 struct strbuf bisect_refs
= STRBUF_INIT
;
2666 strbuf_addf(&bisect_refs
, "refs/bisect/%s", term
);
2667 status
= refs_for_each_fullref_in(refs
, bisect_refs
.buf
, fn
, cb_data
);
2668 strbuf_release(&bisect_refs
);
2672 static int for_each_bad_bisect_ref(struct ref_store
*refs
, each_ref_fn fn
, void *cb_data
)
2674 return for_each_bisect_ref(refs
, fn
, cb_data
, term_bad
);
2677 static int for_each_good_bisect_ref(struct ref_store
*refs
, each_ref_fn fn
, void *cb_data
)
2679 return for_each_bisect_ref(refs
, fn
, cb_data
, term_good
);
2682 static int handle_revision_pseudo_opt(struct rev_info
*revs
,
2683 const char **argv
, int *flags
)
2685 const char *arg
= argv
[0];
2687 struct ref_store
*refs
;
2690 if (revs
->repo
!= the_repository
) {
2692 * We need some something like get_submodule_worktrees()
2693 * before we can go through all worktrees of a submodule,
2694 * .e.g with adding all HEADs from --all, which is not
2695 * supported right now, so stick to single worktree.
2697 if (!revs
->single_worktree
)
2698 BUG("--single-worktree cannot be used together with submodule");
2700 refs
= get_main_ref_store(revs
->repo
);
2705 * Commands like "git shortlog" will not accept the options below
2706 * unless parse_revision_opt queues them (as opposed to erroring
2709 * When implementing your new pseudo-option, remember to
2710 * register it in the list at the top of handle_revision_opt.
2712 if (!strcmp(arg
, "--all")) {
2713 handle_refs(refs
, revs
, *flags
, refs_for_each_ref
);
2714 handle_refs(refs
, revs
, *flags
, refs_head_ref
);
2715 if (!revs
->single_worktree
) {
2716 struct all_refs_cb cb
;
2718 init_all_refs_cb(&cb
, revs
, *flags
);
2719 other_head_refs(handle_one_ref
, &cb
);
2721 clear_ref_exclusions(&revs
->ref_excludes
);
2722 } else if (!strcmp(arg
, "--branches")) {
2723 if (revs
->ref_excludes
.hidden_refs_configured
)
2724 return error(_("--exclude-hidden cannot be used together with --branches"));
2725 handle_refs(refs
, revs
, *flags
, refs_for_each_branch_ref
);
2726 clear_ref_exclusions(&revs
->ref_excludes
);
2727 } else if (!strcmp(arg
, "--bisect")) {
2728 read_bisect_terms(&term_bad
, &term_good
);
2729 handle_refs(refs
, revs
, *flags
, for_each_bad_bisect_ref
);
2730 handle_refs(refs
, revs
, *flags
^ (UNINTERESTING
| BOTTOM
),
2731 for_each_good_bisect_ref
);
2733 } else if (!strcmp(arg
, "--tags")) {
2734 if (revs
->ref_excludes
.hidden_refs_configured
)
2735 return error(_("--exclude-hidden cannot be used together with --tags"));
2736 handle_refs(refs
, revs
, *flags
, refs_for_each_tag_ref
);
2737 clear_ref_exclusions(&revs
->ref_excludes
);
2738 } else if (!strcmp(arg
, "--remotes")) {
2739 if (revs
->ref_excludes
.hidden_refs_configured
)
2740 return error(_("--exclude-hidden cannot be used together with --remotes"));
2741 handle_refs(refs
, revs
, *flags
, refs_for_each_remote_ref
);
2742 clear_ref_exclusions(&revs
->ref_excludes
);
2743 } else if ((argcount
= parse_long_opt("glob", argv
, &optarg
))) {
2744 struct all_refs_cb cb
;
2745 init_all_refs_cb(&cb
, revs
, *flags
);
2746 for_each_glob_ref(handle_one_ref
, optarg
, &cb
);
2747 clear_ref_exclusions(&revs
->ref_excludes
);
2749 } else if ((argcount
= parse_long_opt("exclude", argv
, &optarg
))) {
2750 add_ref_exclusion(&revs
->ref_excludes
, optarg
);
2752 } else if ((argcount
= parse_long_opt("exclude-hidden", argv
, &optarg
))) {
2753 exclude_hidden_refs(&revs
->ref_excludes
, optarg
);
2755 } else if (skip_prefix(arg
, "--branches=", &optarg
)) {
2756 struct all_refs_cb cb
;
2757 if (revs
->ref_excludes
.hidden_refs_configured
)
2758 return error(_("--exclude-hidden cannot be used together with --branches"));
2759 init_all_refs_cb(&cb
, revs
, *flags
);
2760 for_each_glob_ref_in(handle_one_ref
, optarg
, "refs/heads/", &cb
);
2761 clear_ref_exclusions(&revs
->ref_excludes
);
2762 } else if (skip_prefix(arg
, "--tags=", &optarg
)) {
2763 struct all_refs_cb cb
;
2764 if (revs
->ref_excludes
.hidden_refs_configured
)
2765 return error(_("--exclude-hidden cannot be used together with --tags"));
2766 init_all_refs_cb(&cb
, revs
, *flags
);
2767 for_each_glob_ref_in(handle_one_ref
, optarg
, "refs/tags/", &cb
);
2768 clear_ref_exclusions(&revs
->ref_excludes
);
2769 } else if (skip_prefix(arg
, "--remotes=", &optarg
)) {
2770 struct all_refs_cb cb
;
2771 if (revs
->ref_excludes
.hidden_refs_configured
)
2772 return error(_("--exclude-hidden cannot be used together with --remotes"));
2773 init_all_refs_cb(&cb
, revs
, *flags
);
2774 for_each_glob_ref_in(handle_one_ref
, optarg
, "refs/remotes/", &cb
);
2775 clear_ref_exclusions(&revs
->ref_excludes
);
2776 } else if (!strcmp(arg
, "--reflog")) {
2777 add_reflogs_to_pending(revs
, *flags
);
2778 } else if (!strcmp(arg
, "--indexed-objects")) {
2779 add_index_objects_to_pending(revs
, *flags
);
2780 } else if (!strcmp(arg
, "--alternate-refs")) {
2781 add_alternate_refs_to_pending(revs
, *flags
);
2782 } else if (!strcmp(arg
, "--not")) {
2783 *flags
^= UNINTERESTING
| BOTTOM
;
2784 } else if (!strcmp(arg
, "--no-walk")) {
2786 } else if (skip_prefix(arg
, "--no-walk=", &optarg
)) {
2788 * Detached form ("--no-walk X" as opposed to "--no-walk=X")
2789 * not allowed, since the argument is optional.
2792 if (!strcmp(optarg
, "sorted"))
2793 revs
->unsorted_input
= 0;
2794 else if (!strcmp(optarg
, "unsorted"))
2795 revs
->unsorted_input
= 1;
2797 return error("invalid argument to --no-walk");
2798 } else if (!strcmp(arg
, "--do-walk")) {
2800 } else if (!strcmp(arg
, "--single-worktree")) {
2801 revs
->single_worktree
= 1;
2802 } else if (skip_prefix(arg
, ("--filter="), &arg
)) {
2803 parse_list_objects_filter(&revs
->filter
, arg
);
2804 } else if (!strcmp(arg
, ("--no-filter"))) {
2805 list_objects_filter_set_no_filter(&revs
->filter
);
2813 static void NORETURN
diagnose_missing_default(const char *def
)
2816 const char *refname
;
2818 refname
= resolve_ref_unsafe(def
, 0, NULL
, &flags
);
2819 if (!refname
|| !(flags
& REF_ISSYMREF
) || (flags
& REF_ISBROKEN
))
2820 die(_("your current branch appears to be broken"));
2822 skip_prefix(refname
, "refs/heads/", &refname
);
2823 die(_("your current branch '%s' does not have any commits yet"),
2828 * Parse revision information, filling in the "rev_info" structure,
2829 * and removing the used arguments from the argument list.
2831 * Returns the number of arguments left that weren't recognized
2832 * (which are also moved to the head of the argument list)
2834 int setup_revisions(int argc
, const char **argv
, struct rev_info
*revs
, struct setup_revision_opt
*opt
)
2836 int i
, flags
, left
, seen_dashdash
, revarg_opt
;
2837 struct strvec prune_data
= STRVEC_INIT
;
2838 int seen_end_of_options
= 0;
2840 /* First, search for "--" */
2841 if (opt
&& opt
->assume_dashdash
) {
2845 for (i
= 1; i
< argc
; i
++) {
2846 const char *arg
= argv
[i
];
2847 if (strcmp(arg
, "--"))
2849 if (opt
&& opt
->free_removed_argv_elements
)
2850 free((char *)argv
[i
]);
2854 strvec_pushv(&prune_data
, argv
+ i
+ 1);
2860 /* Second, deal with arguments and options */
2862 revarg_opt
= opt
? opt
->revarg_opt
: 0;
2864 revarg_opt
|= REVARG_CANNOT_BE_FILENAME
;
2865 for (left
= i
= 1; i
< argc
; i
++) {
2866 const char *arg
= argv
[i
];
2867 if (!seen_end_of_options
&& *arg
== '-') {
2870 opts
= handle_revision_pseudo_opt(
2878 if (!strcmp(arg
, "--stdin")) {
2879 if (revs
->disable_stdin
) {
2883 if (revs
->read_from_stdin
++)
2884 die("--stdin given twice?");
2885 read_revisions_from_stdin(revs
, &prune_data
);
2889 if (!strcmp(arg
, "--end-of-options")) {
2890 seen_end_of_options
= 1;
2894 opts
= handle_revision_opt(revs
, argc
- i
, argv
+ i
,
2906 if (handle_revision_arg(arg
, revs
, flags
, revarg_opt
)) {
2908 if (seen_dashdash
|| *arg
== '^')
2909 die("bad revision '%s'", arg
);
2911 /* If we didn't have a "--":
2912 * (1) all filenames must exist;
2913 * (2) all rev-args must not be interpretable
2914 * as a valid filename.
2915 * but the latter we have checked in the main loop.
2917 for (j
= i
; j
< argc
; j
++)
2918 verify_filename(revs
->prefix
, argv
[j
], j
== i
);
2920 strvec_pushv(&prune_data
, argv
+ i
);
2924 revision_opts_finish(revs
);
2926 if (prune_data
.nr
) {
2928 * If we need to introduce the magic "a lone ':' means no
2929 * pathspec whatsoever", here is the place to do so.
2931 * if (prune_data.nr == 1 && !strcmp(prune_data[0], ":")) {
2932 * prune_data.nr = 0;
2933 * prune_data.alloc = 0;
2934 * free(prune_data.path);
2935 * prune_data.path = NULL;
2937 * terminate prune_data.alloc with NULL and
2938 * call init_pathspec() to set revs->prune_data here.
2941 parse_pathspec(&revs
->prune_data
, 0, 0,
2942 revs
->prefix
, prune_data
.v
);
2944 strvec_clear(&prune_data
);
2947 revs
->def
= opt
? opt
->def
: NULL
;
2948 if (opt
&& opt
->tweak
)
2949 opt
->tweak(revs
, opt
);
2950 if (revs
->show_merge
)
2951 prepare_show_merge(revs
);
2952 if (revs
->def
&& !revs
->pending
.nr
&& !revs
->rev_input_given
) {
2953 struct object_id oid
;
2954 struct object
*object
;
2955 struct object_context oc
;
2956 if (get_oid_with_context(revs
->repo
, revs
->def
, 0, &oid
, &oc
))
2957 diagnose_missing_default(revs
->def
);
2958 object
= get_reference(revs
, revs
->def
, &oid
, 0);
2959 add_pending_object_with_mode(revs
, object
, revs
->def
, oc
.mode
);
2962 /* Did the user ask for any diff output? Run the diff! */
2963 if (revs
->diffopt
.output_format
& ~DIFF_FORMAT_NO_OUTPUT
)
2966 /* Pickaxe, diff-filter and rename following need diffs */
2967 if ((revs
->diffopt
.pickaxe_opts
& DIFF_PICKAXE_KINDS_MASK
) ||
2968 revs
->diffopt
.filter
||
2969 revs
->diffopt
.flags
.follow_renames
)
2972 if (revs
->diffopt
.objfind
)
2973 revs
->simplify_history
= 0;
2975 if (revs
->line_level_traverse
) {
2976 if (want_ancestry(revs
))
2978 revs
->topo_order
= 1;
2981 if (revs
->topo_order
&& !generation_numbers_enabled(the_repository
))
2984 if (revs
->prune_data
.nr
) {
2985 copy_pathspec(&revs
->pruning
.pathspec
, &revs
->prune_data
);
2986 /* Can't prune commits with rename following: the paths change.. */
2987 if (!revs
->diffopt
.flags
.follow_renames
)
2989 if (!revs
->full_diff
)
2990 copy_pathspec(&revs
->diffopt
.pathspec
,
2994 diff_merges_setup_revs(revs
);
2996 revs
->diffopt
.abbrev
= revs
->abbrev
;
2998 diff_setup_done(&revs
->diffopt
);
3000 if (!is_encoding_utf8(get_log_output_encoding()))
3001 revs
->grep_filter
.ignore_locale
= 1;
3002 compile_grep_patterns(&revs
->grep_filter
);
3004 if (revs
->reverse
&& revs
->reflog_info
)
3005 die(_("options '%s' and '%s' cannot be used together"), "--reverse", "--walk-reflogs");
3006 if (revs
->reflog_info
&& revs
->limited
)
3007 die("cannot combine --walk-reflogs with history-limiting options");
3008 if (revs
->rewrite_parents
&& revs
->children
.name
)
3009 die(_("options '%s' and '%s' cannot be used together"), "--parents", "--children");
3010 if (revs
->filter
.choice
&& !revs
->blob_objects
)
3011 die(_("object filtering requires --objects"));
3014 * Limitations on the graph functionality
3016 if (revs
->reverse
&& revs
->graph
)
3017 die(_("options '%s' and '%s' cannot be used together"), "--reverse", "--graph");
3019 if (revs
->reflog_info
&& revs
->graph
)
3020 die(_("options '%s' and '%s' cannot be used together"), "--walk-reflogs", "--graph");
3021 if (revs
->no_walk
&& revs
->graph
)
3022 die(_("options '%s' and '%s' cannot be used together"), "--no-walk", "--graph");
3023 if (!revs
->reflog_info
&& revs
->grep_filter
.use_reflog_filter
)
3024 die(_("the option '%s' requires '%s'"), "--grep-reflog", "--walk-reflogs");
3026 if (revs
->line_level_traverse
&&
3027 (revs
->diffopt
.output_format
& ~(DIFF_FORMAT_PATCH
| DIFF_FORMAT_NO_OUTPUT
)))
3028 die(_("-L does not yet support diff formats besides -p and -s"));
3030 if (revs
->expand_tabs_in_log
< 0)
3031 revs
->expand_tabs_in_log
= revs
->expand_tabs_in_log_default
;
3036 static void release_revisions_cmdline(struct rev_cmdline_info
*cmdline
)
3040 for (i
= 0; i
< cmdline
->nr
; i
++)
3041 free((char *)cmdline
->rev
[i
].name
);
3045 static void release_revisions_mailmap(struct string_list
*mailmap
)
3049 clear_mailmap(mailmap
);
3053 static void release_revisions_topo_walk_info(struct topo_walk_info
*info
);
3055 void release_revisions(struct rev_info
*revs
)
3057 free_commit_list(revs
->commits
);
3058 free_commit_list(revs
->ancestry_path_bottoms
);
3059 object_array_clear(&revs
->pending
);
3060 object_array_clear(&revs
->boundary_commits
);
3061 release_revisions_cmdline(&revs
->cmdline
);
3062 list_objects_filter_release(&revs
->filter
);
3063 clear_pathspec(&revs
->prune_data
);
3064 date_mode_release(&revs
->date_mode
);
3065 release_revisions_mailmap(revs
->mailmap
);
3066 free_grep_patterns(&revs
->grep_filter
);
3067 graph_clear(revs
->graph
);
3068 /* TODO (need to handle "no_free"): diff_free(&revs->diffopt) */
3069 diff_free(&revs
->pruning
);
3070 reflog_walk_info_release(revs
->reflog_info
);
3071 release_revisions_topo_walk_info(revs
->topo_walk_info
);
3074 static void add_child(struct rev_info
*revs
, struct commit
*parent
, struct commit
*child
)
3076 struct commit_list
*l
= xcalloc(1, sizeof(*l
));
3079 l
->next
= add_decoration(&revs
->children
, &parent
->object
, l
);
3082 static int remove_duplicate_parents(struct rev_info
*revs
, struct commit
*commit
)
3084 struct treesame_state
*ts
= lookup_decoration(&revs
->treesame
, &commit
->object
);
3085 struct commit_list
**pp
, *p
;
3086 int surviving_parents
;
3088 /* Examine existing parents while marking ones we have seen... */
3089 pp
= &commit
->parents
;
3090 surviving_parents
= 0;
3091 while ((p
= *pp
) != NULL
) {
3092 struct commit
*parent
= p
->item
;
3093 if (parent
->object
.flags
& TMP_MARK
) {
3096 compact_treesame(revs
, commit
, surviving_parents
);
3099 parent
->object
.flags
|= TMP_MARK
;
3100 surviving_parents
++;
3103 /* clear the temporary mark */
3104 for (p
= commit
->parents
; p
; p
= p
->next
) {
3105 p
->item
->object
.flags
&= ~TMP_MARK
;
3107 /* no update_treesame() - removing duplicates can't affect TREESAME */
3108 return surviving_parents
;
3111 struct merge_simplify_state
{
3112 struct commit
*simplified
;
3115 static struct merge_simplify_state
*locate_simplify_state(struct rev_info
*revs
, struct commit
*commit
)
3117 struct merge_simplify_state
*st
;
3119 st
= lookup_decoration(&revs
->merge_simplification
, &commit
->object
);
3121 CALLOC_ARRAY(st
, 1);
3122 add_decoration(&revs
->merge_simplification
, &commit
->object
, st
);
3127 static int mark_redundant_parents(struct commit
*commit
)
3129 struct commit_list
*h
= reduce_heads(commit
->parents
);
3130 int i
= 0, marked
= 0;
3131 struct commit_list
*po
, *pn
;
3133 /* Want these for sanity-checking only */
3134 int orig_cnt
= commit_list_count(commit
->parents
);
3135 int cnt
= commit_list_count(h
);
3138 * Not ready to remove items yet, just mark them for now, based
3139 * on the output of reduce_heads(). reduce_heads outputs the reduced
3140 * set in its original order, so this isn't too hard.
3142 po
= commit
->parents
;
3145 if (pn
&& po
->item
== pn
->item
) {
3149 po
->item
->object
.flags
|= TMP_MARK
;
3155 if (i
!= cnt
|| cnt
+marked
!= orig_cnt
)
3156 die("mark_redundant_parents %d %d %d %d", orig_cnt
, cnt
, i
, marked
);
3158 free_commit_list(h
);
3163 static int mark_treesame_root_parents(struct commit
*commit
)
3165 struct commit_list
*p
;
3168 for (p
= commit
->parents
; p
; p
= p
->next
) {
3169 struct commit
*parent
= p
->item
;
3170 if (!parent
->parents
&& (parent
->object
.flags
& TREESAME
)) {
3171 parent
->object
.flags
|= TMP_MARK
;
3180 * Awkward naming - this means one parent we are TREESAME to.
3181 * cf mark_treesame_root_parents: root parents that are TREESAME (to an
3182 * empty tree). Better name suggestions?
3184 static int leave_one_treesame_to_parent(struct rev_info
*revs
, struct commit
*commit
)
3186 struct treesame_state
*ts
= lookup_decoration(&revs
->treesame
, &commit
->object
);
3187 struct commit
*unmarked
= NULL
, *marked
= NULL
;
3188 struct commit_list
*p
;
3191 for (p
= commit
->parents
, n
= 0; p
; p
= p
->next
, n
++) {
3192 if (ts
->treesame
[n
]) {
3193 if (p
->item
->object
.flags
& TMP_MARK
) {
3206 * If we are TREESAME to a marked-for-deletion parent, but not to any
3207 * unmarked parents, unmark the first TREESAME parent. This is the
3208 * parent that the default simplify_history==1 scan would have followed,
3209 * and it doesn't make sense to omit that path when asking for a
3210 * simplified full history. Retaining it improves the chances of
3211 * understanding odd missed merges that took an old version of a file.
3215 * I--------*X A modified the file, but mainline merge X used
3216 * \ / "-s ours", so took the version from I. X is
3217 * `-*A--' TREESAME to I and !TREESAME to A.
3219 * Default log from X would produce "I". Without this check,
3220 * --full-history --simplify-merges would produce "I-A-X", showing
3221 * the merge commit X and that it changed A, but not making clear that
3222 * it had just taken the I version. With this check, the topology above
3225 * Note that it is possible that the simplification chooses a different
3226 * TREESAME parent from the default, in which case this test doesn't
3227 * activate, and we _do_ drop the default parent. Example:
3229 * I------X A modified the file, but it was reverted in B,
3230 * \ / meaning mainline merge X is TREESAME to both
3233 * Default log would produce "I" by following the first parent;
3234 * --full-history --simplify-merges will produce "I-A-B". But this is a
3235 * reasonable result - it presents a logical full history leading from
3236 * I to X, and X is not an important merge.
3238 if (!unmarked
&& marked
) {
3239 marked
->object
.flags
&= ~TMP_MARK
;
3246 static int remove_marked_parents(struct rev_info
*revs
, struct commit
*commit
)
3248 struct commit_list
**pp
, *p
;
3249 int nth_parent
, removed
= 0;
3251 pp
= &commit
->parents
;
3253 while ((p
= *pp
) != NULL
) {
3254 struct commit
*parent
= p
->item
;
3255 if (parent
->object
.flags
& TMP_MARK
) {
3256 parent
->object
.flags
&= ~TMP_MARK
;
3260 compact_treesame(revs
, commit
, nth_parent
);
3267 /* Removing parents can only increase TREESAMEness */
3268 if (removed
&& !(commit
->object
.flags
& TREESAME
))
3269 update_treesame(revs
, commit
);
3274 static struct commit_list
**simplify_one(struct rev_info
*revs
, struct commit
*commit
, struct commit_list
**tail
)
3276 struct commit_list
*p
;
3277 struct commit
*parent
;
3278 struct merge_simplify_state
*st
, *pst
;
3281 st
= locate_simplify_state(revs
, commit
);
3284 * Have we handled this one?
3290 * An UNINTERESTING commit simplifies to itself, so does a
3291 * root commit. We do not rewrite parents of such commit
3294 if ((commit
->object
.flags
& UNINTERESTING
) || !commit
->parents
) {
3295 st
->simplified
= commit
;
3300 * Do we know what commit all of our parents that matter
3301 * should be rewritten to? Otherwise we are not ready to
3302 * rewrite this one yet.
3304 for (cnt
= 0, p
= commit
->parents
; p
; p
= p
->next
) {
3305 pst
= locate_simplify_state(revs
, p
->item
);
3306 if (!pst
->simplified
) {
3307 tail
= &commit_list_insert(p
->item
, tail
)->next
;
3310 if (revs
->first_parent_only
)
3314 tail
= &commit_list_insert(commit
, tail
)->next
;
3319 * Rewrite our list of parents. Note that this cannot
3320 * affect our TREESAME flags in any way - a commit is
3321 * always TREESAME to its simplification.
3323 for (p
= commit
->parents
; p
; p
= p
->next
) {
3324 pst
= locate_simplify_state(revs
, p
->item
);
3325 p
->item
= pst
->simplified
;
3326 if (revs
->first_parent_only
)
3330 if (revs
->first_parent_only
)
3333 cnt
= remove_duplicate_parents(revs
, commit
);
3336 * It is possible that we are a merge and one side branch
3337 * does not have any commit that touches the given paths;
3338 * in such a case, the immediate parent from that branch
3339 * will be rewritten to be the merge base.
3341 * o----X X: the commit we are looking at;
3342 * / / o: a commit that touches the paths;
3345 * Further, a merge of an independent branch that doesn't
3346 * touch the path will reduce to a treesame root parent:
3348 * ----o----X X: the commit we are looking at;
3349 * / o: a commit that touches the paths;
3350 * r r: a root commit not touching the paths
3352 * Detect and simplify both cases.
3355 int marked
= mark_redundant_parents(commit
);
3356 marked
+= mark_treesame_root_parents(commit
);
3358 marked
-= leave_one_treesame_to_parent(revs
, commit
);
3360 cnt
= remove_marked_parents(revs
, commit
);
3364 * A commit simplifies to itself if it is a root, if it is
3365 * UNINTERESTING, if it touches the given paths, or if it is a
3366 * merge and its parents don't simplify to one relevant commit
3367 * (the first two cases are already handled at the beginning of
3370 * Otherwise, it simplifies to what its sole relevant parent
3374 (commit
->object
.flags
& UNINTERESTING
) ||
3375 !(commit
->object
.flags
& TREESAME
) ||
3376 (parent
= one_relevant_parent(revs
, commit
->parents
)) == NULL
||
3377 (revs
->show_pulls
&& (commit
->object
.flags
& PULL_MERGE
)))
3378 st
->simplified
= commit
;
3380 pst
= locate_simplify_state(revs
, parent
);
3381 st
->simplified
= pst
->simplified
;
3386 static void simplify_merges(struct rev_info
*revs
)
3388 struct commit_list
*list
, *next
;
3389 struct commit_list
*yet_to_do
, **tail
;
3390 struct commit
*commit
;
3395 /* feed the list reversed */
3397 for (list
= revs
->commits
; list
; list
= next
) {
3398 commit
= list
->item
;
3401 * Do not free(list) here yet; the original list
3402 * is used later in this function.
3404 commit_list_insert(commit
, &yet_to_do
);
3411 commit
= pop_commit(&list
);
3412 tail
= simplify_one(revs
, commit
, tail
);
3416 /* clean up the result, removing the simplified ones */
3417 list
= revs
->commits
;
3418 revs
->commits
= NULL
;
3419 tail
= &revs
->commits
;
3421 struct merge_simplify_state
*st
;
3423 commit
= pop_commit(&list
);
3424 st
= locate_simplify_state(revs
, commit
);
3425 if (st
->simplified
== commit
)
3426 tail
= &commit_list_insert(commit
, tail
)->next
;
3430 static void set_children(struct rev_info
*revs
)
3432 struct commit_list
*l
;
3433 for (l
= revs
->commits
; l
; l
= l
->next
) {
3434 struct commit
*commit
= l
->item
;
3435 struct commit_list
*p
;
3437 for (p
= commit
->parents
; p
; p
= p
->next
)
3438 add_child(revs
, p
->item
, commit
);
3442 void reset_revision_walk(void)
3444 clear_object_flags(SEEN
| ADDED
| SHOWN
| TOPO_WALK_EXPLORED
| TOPO_WALK_INDEGREE
);
3447 static int mark_uninteresting(const struct object_id
*oid
,
3448 struct packed_git
*pack UNUSED
,
3449 uint32_t pos UNUSED
,
3452 struct rev_info
*revs
= cb
;
3453 struct object
*o
= lookup_unknown_object(revs
->repo
, oid
);
3454 o
->flags
|= UNINTERESTING
| SEEN
;
3458 define_commit_slab(indegree_slab
, int);
3459 define_commit_slab(author_date_slab
, timestamp_t
);
3461 struct topo_walk_info
{
3462 timestamp_t min_generation
;
3463 struct prio_queue explore_queue
;
3464 struct prio_queue indegree_queue
;
3465 struct prio_queue topo_queue
;
3466 struct indegree_slab indegree
;
3467 struct author_date_slab author_date
;
3470 static int topo_walk_atexit_registered
;
3471 static unsigned int count_explore_walked
;
3472 static unsigned int count_indegree_walked
;
3473 static unsigned int count_topo_walked
;
3475 static void trace2_topo_walk_statistics_atexit(void)
3477 struct json_writer jw
= JSON_WRITER_INIT
;
3479 jw_object_begin(&jw
, 0);
3480 jw_object_intmax(&jw
, "count_explore_walked", count_explore_walked
);
3481 jw_object_intmax(&jw
, "count_indegree_walked", count_indegree_walked
);
3482 jw_object_intmax(&jw
, "count_topo_walked", count_topo_walked
);
3485 trace2_data_json("topo_walk", the_repository
, "statistics", &jw
);
3490 static inline void test_flag_and_insert(struct prio_queue
*q
, struct commit
*c
, int flag
)
3492 if (c
->object
.flags
& flag
)
3495 c
->object
.flags
|= flag
;
3496 prio_queue_put(q
, c
);
3499 static void explore_walk_step(struct rev_info
*revs
)
3501 struct topo_walk_info
*info
= revs
->topo_walk_info
;
3502 struct commit_list
*p
;
3503 struct commit
*c
= prio_queue_get(&info
->explore_queue
);
3508 if (repo_parse_commit_gently(revs
->repo
, c
, 1) < 0)
3511 count_explore_walked
++;
3513 if (revs
->sort_order
== REV_SORT_BY_AUTHOR_DATE
)
3514 record_author_date(&info
->author_date
, c
);
3516 if (revs
->max_age
!= -1 && (c
->date
< revs
->max_age
))
3517 c
->object
.flags
|= UNINTERESTING
;
3519 if (process_parents(revs
, c
, NULL
, NULL
) < 0)
3522 if (c
->object
.flags
& UNINTERESTING
)
3523 mark_parents_uninteresting(revs
, c
);
3525 for (p
= c
->parents
; p
; p
= p
->next
)
3526 test_flag_and_insert(&info
->explore_queue
, p
->item
, TOPO_WALK_EXPLORED
);
3529 static void explore_to_depth(struct rev_info
*revs
,
3530 timestamp_t gen_cutoff
)
3532 struct topo_walk_info
*info
= revs
->topo_walk_info
;
3534 while ((c
= prio_queue_peek(&info
->explore_queue
)) &&
3535 commit_graph_generation(c
) >= gen_cutoff
)
3536 explore_walk_step(revs
);
3539 static void indegree_walk_step(struct rev_info
*revs
)
3541 struct commit_list
*p
;
3542 struct topo_walk_info
*info
= revs
->topo_walk_info
;
3543 struct commit
*c
= prio_queue_get(&info
->indegree_queue
);
3548 if (repo_parse_commit_gently(revs
->repo
, c
, 1) < 0)
3551 count_indegree_walked
++;
3553 explore_to_depth(revs
, commit_graph_generation(c
));
3555 for (p
= c
->parents
; p
; p
= p
->next
) {
3556 struct commit
*parent
= p
->item
;
3557 int *pi
= indegree_slab_at(&info
->indegree
, parent
);
3559 if (repo_parse_commit_gently(revs
->repo
, parent
, 1) < 0)
3567 test_flag_and_insert(&info
->indegree_queue
, parent
, TOPO_WALK_INDEGREE
);
3569 if (revs
->first_parent_only
)
3574 static void compute_indegrees_to_depth(struct rev_info
*revs
,
3575 timestamp_t gen_cutoff
)
3577 struct topo_walk_info
*info
= revs
->topo_walk_info
;
3579 while ((c
= prio_queue_peek(&info
->indegree_queue
)) &&
3580 commit_graph_generation(c
) >= gen_cutoff
)
3581 indegree_walk_step(revs
);
3584 static void release_revisions_topo_walk_info(struct topo_walk_info
*info
)
3588 clear_prio_queue(&info
->explore_queue
);
3589 clear_prio_queue(&info
->indegree_queue
);
3590 clear_prio_queue(&info
->topo_queue
);
3591 clear_indegree_slab(&info
->indegree
);
3592 clear_author_date_slab(&info
->author_date
);
3596 static void reset_topo_walk(struct rev_info
*revs
)
3598 release_revisions_topo_walk_info(revs
->topo_walk_info
);
3599 revs
->topo_walk_info
= NULL
;
3602 static void init_topo_walk(struct rev_info
*revs
)
3604 struct topo_walk_info
*info
;
3605 struct commit_list
*list
;
3606 if (revs
->topo_walk_info
)
3607 reset_topo_walk(revs
);
3609 revs
->topo_walk_info
= xmalloc(sizeof(struct topo_walk_info
));
3610 info
= revs
->topo_walk_info
;
3611 memset(info
, 0, sizeof(struct topo_walk_info
));
3613 init_indegree_slab(&info
->indegree
);
3614 memset(&info
->explore_queue
, 0, sizeof(info
->explore_queue
));
3615 memset(&info
->indegree_queue
, 0, sizeof(info
->indegree_queue
));
3616 memset(&info
->topo_queue
, 0, sizeof(info
->topo_queue
));
3618 switch (revs
->sort_order
) {
3619 default: /* REV_SORT_IN_GRAPH_ORDER */
3620 info
->topo_queue
.compare
= NULL
;
3622 case REV_SORT_BY_COMMIT_DATE
:
3623 info
->topo_queue
.compare
= compare_commits_by_commit_date
;
3625 case REV_SORT_BY_AUTHOR_DATE
:
3626 init_author_date_slab(&info
->author_date
);
3627 info
->topo_queue
.compare
= compare_commits_by_author_date
;
3628 info
->topo_queue
.cb_data
= &info
->author_date
;
3632 info
->explore_queue
.compare
= compare_commits_by_gen_then_commit_date
;
3633 info
->indegree_queue
.compare
= compare_commits_by_gen_then_commit_date
;
3635 info
->min_generation
= GENERATION_NUMBER_INFINITY
;
3636 for (list
= revs
->commits
; list
; list
= list
->next
) {
3637 struct commit
*c
= list
->item
;
3638 timestamp_t generation
;
3640 if (repo_parse_commit_gently(revs
->repo
, c
, 1))
3643 test_flag_and_insert(&info
->explore_queue
, c
, TOPO_WALK_EXPLORED
);
3644 test_flag_and_insert(&info
->indegree_queue
, c
, TOPO_WALK_INDEGREE
);
3646 generation
= commit_graph_generation(c
);
3647 if (generation
< info
->min_generation
)
3648 info
->min_generation
= generation
;
3650 *(indegree_slab_at(&info
->indegree
, c
)) = 1;
3652 if (revs
->sort_order
== REV_SORT_BY_AUTHOR_DATE
)
3653 record_author_date(&info
->author_date
, c
);
3655 compute_indegrees_to_depth(revs
, info
->min_generation
);
3657 for (list
= revs
->commits
; list
; list
= list
->next
) {
3658 struct commit
*c
= list
->item
;
3660 if (*(indegree_slab_at(&info
->indegree
, c
)) == 1)
3661 prio_queue_put(&info
->topo_queue
, c
);
3665 * This is unfortunate; the initial tips need to be shown
3666 * in the order given from the revision traversal machinery.
3668 if (revs
->sort_order
== REV_SORT_IN_GRAPH_ORDER
)
3669 prio_queue_reverse(&info
->topo_queue
);
3671 if (trace2_is_enabled() && !topo_walk_atexit_registered
) {
3672 atexit(trace2_topo_walk_statistics_atexit
);
3673 topo_walk_atexit_registered
= 1;
3677 static struct commit
*next_topo_commit(struct rev_info
*revs
)
3680 struct topo_walk_info
*info
= revs
->topo_walk_info
;
3682 /* pop next off of topo_queue */
3683 c
= prio_queue_get(&info
->topo_queue
);
3686 *(indegree_slab_at(&info
->indegree
, c
)) = 0;
3691 static void expand_topo_walk(struct rev_info
*revs
, struct commit
*commit
)
3693 struct commit_list
*p
;
3694 struct topo_walk_info
*info
= revs
->topo_walk_info
;
3695 if (process_parents(revs
, commit
, NULL
, NULL
) < 0) {
3696 if (!revs
->ignore_missing_links
)
3697 die("Failed to traverse parents of commit %s",
3698 oid_to_hex(&commit
->object
.oid
));
3701 count_topo_walked
++;
3703 for (p
= commit
->parents
; p
; p
= p
->next
) {
3704 struct commit
*parent
= p
->item
;
3706 timestamp_t generation
;
3708 if (parent
->object
.flags
& UNINTERESTING
)
3711 if (repo_parse_commit_gently(revs
->repo
, parent
, 1) < 0)
3714 generation
= commit_graph_generation(parent
);
3715 if (generation
< info
->min_generation
) {
3716 info
->min_generation
= generation
;
3717 compute_indegrees_to_depth(revs
, info
->min_generation
);
3720 pi
= indegree_slab_at(&info
->indegree
, parent
);
3724 prio_queue_put(&info
->topo_queue
, parent
);
3726 if (revs
->first_parent_only
)
3731 int prepare_revision_walk(struct rev_info
*revs
)
3734 struct object_array old_pending
;
3735 struct commit_list
**next
= &revs
->commits
;
3737 memcpy(&old_pending
, &revs
->pending
, sizeof(old_pending
));
3738 revs
->pending
.nr
= 0;
3739 revs
->pending
.alloc
= 0;
3740 revs
->pending
.objects
= NULL
;
3741 for (i
= 0; i
< old_pending
.nr
; i
++) {
3742 struct object_array_entry
*e
= old_pending
.objects
+ i
;
3743 struct commit
*commit
= handle_commit(revs
, e
);
3745 if (!(commit
->object
.flags
& SEEN
)) {
3746 commit
->object
.flags
|= SEEN
;
3747 next
= commit_list_append(commit
, next
);
3751 object_array_clear(&old_pending
);
3753 /* Signal whether we need per-parent treesame decoration */
3754 if (revs
->simplify_merges
||
3755 (revs
->limited
&& limiting_can_increase_treesame(revs
)))
3756 revs
->treesame
.name
= "treesame";
3758 if (revs
->exclude_promisor_objects
) {
3759 for_each_packed_object(mark_uninteresting
, revs
,
3760 FOR_EACH_OBJECT_PROMISOR_ONLY
);
3763 if (!revs
->reflog_info
)
3764 prepare_to_use_bloom_filter(revs
);
3765 if (!revs
->unsorted_input
)
3766 commit_list_sort_by_date(&revs
->commits
);
3769 if (revs
->limited
) {
3770 if (limit_list(revs
) < 0)
3772 if (revs
->topo_order
)
3773 sort_in_topological_order(&revs
->commits
, revs
->sort_order
);
3774 } else if (revs
->topo_order
)
3775 init_topo_walk(revs
);
3776 if (revs
->line_level_traverse
&& want_ancestry(revs
))
3778 * At the moment we can only do line-level log with parent
3779 * rewriting by performing this expensive pre-filtering step.
3780 * If parent rewriting is not requested, then we rather
3781 * perform the line-level log filtering during the regular
3782 * history traversal.
3784 line_log_filter(revs
);
3785 if (revs
->simplify_merges
)
3786 simplify_merges(revs
);
3787 if (revs
->children
.name
)
3793 static enum rewrite_result
rewrite_one_1(struct rev_info
*revs
,
3795 struct prio_queue
*queue
)
3798 struct commit
*p
= *pp
;
3800 if (process_parents(revs
, p
, NULL
, queue
) < 0)
3801 return rewrite_one_error
;
3802 if (p
->object
.flags
& UNINTERESTING
)
3803 return rewrite_one_ok
;
3804 if (!(p
->object
.flags
& TREESAME
))
3805 return rewrite_one_ok
;
3807 return rewrite_one_noparents
;
3808 if (!(p
= one_relevant_parent(revs
, p
->parents
)))
3809 return rewrite_one_ok
;
3814 static void merge_queue_into_list(struct prio_queue
*q
, struct commit_list
**list
)
3817 struct commit
*item
= prio_queue_peek(q
);
3818 struct commit_list
*p
= *list
;
3820 if (p
&& p
->item
->date
>= item
->date
)
3823 p
= commit_list_insert(item
, list
);
3824 list
= &p
->next
; /* skip newly added item */
3825 prio_queue_get(q
); /* pop item */
3830 static enum rewrite_result
rewrite_one(struct rev_info
*revs
, struct commit
**pp
)
3832 struct prio_queue queue
= { compare_commits_by_commit_date
};
3833 enum rewrite_result ret
= rewrite_one_1(revs
, pp
, &queue
);
3834 merge_queue_into_list(&queue
, &revs
->commits
);
3835 clear_prio_queue(&queue
);
3839 int rewrite_parents(struct rev_info
*revs
, struct commit
*commit
,
3840 rewrite_parent_fn_t rewrite_parent
)
3842 struct commit_list
**pp
= &commit
->parents
;
3844 struct commit_list
*parent
= *pp
;
3845 switch (rewrite_parent(revs
, &parent
->item
)) {
3846 case rewrite_one_ok
:
3848 case rewrite_one_noparents
:
3851 case rewrite_one_error
:
3856 remove_duplicate_parents(revs
, commit
);
3860 static int commit_match(struct commit
*commit
, struct rev_info
*opt
)
3863 const char *encoding
;
3864 const char *message
;
3865 struct strbuf buf
= STRBUF_INIT
;
3867 if (!opt
->grep_filter
.pattern_list
&& !opt
->grep_filter
.header_list
)
3870 /* Prepend "fake" headers as needed */
3871 if (opt
->grep_filter
.use_reflog_filter
) {
3872 strbuf_addstr(&buf
, "reflog ");
3873 get_reflog_message(&buf
, opt
->reflog_info
);
3874 strbuf_addch(&buf
, '\n');
3878 * We grep in the user's output encoding, under the assumption that it
3879 * is the encoding they are most likely to write their grep pattern
3880 * for. In addition, it means we will match the "notes" encoding below,
3881 * so we will not end up with a buffer that has two different encodings
3884 encoding
= get_log_output_encoding();
3885 message
= logmsg_reencode(commit
, NULL
, encoding
);
3887 /* Copy the commit to temporary if we are using "fake" headers */
3889 strbuf_addstr(&buf
, message
);
3891 if (opt
->grep_filter
.header_list
&& opt
->mailmap
) {
3892 const char *commit_headers
[] = { "author ", "committer ", NULL
};
3895 strbuf_addstr(&buf
, message
);
3897 apply_mailmap_to_header(&buf
, commit_headers
, opt
->mailmap
);
3900 /* Append "fake" message parts as needed */
3901 if (opt
->show_notes
) {
3903 strbuf_addstr(&buf
, message
);
3904 format_display_notes(&commit
->object
.oid
, &buf
, encoding
, 1);
3908 * Find either in the original commit message, or in the temporary.
3909 * Note that we cast away the constness of "message" here. It is
3910 * const because it may come from the cached commit buffer. That's OK,
3911 * because we know that it is modifiable heap memory, and that while
3912 * grep_buffer may modify it for speed, it will restore any
3913 * changes before returning.
3916 retval
= grep_buffer(&opt
->grep_filter
, buf
.buf
, buf
.len
);
3918 retval
= grep_buffer(&opt
->grep_filter
,
3919 (char *)message
, strlen(message
));
3920 strbuf_release(&buf
);
3921 unuse_commit_buffer(commit
, message
);
3925 static inline int want_ancestry(const struct rev_info
*revs
)
3927 return (revs
->rewrite_parents
|| revs
->children
.name
);
3931 * Return a timestamp to be used for --since/--until comparisons for this
3932 * commit, based on the revision options.
3934 static timestamp_t
comparison_date(const struct rev_info
*revs
,
3935 struct commit
*commit
)
3937 return revs
->reflog_info
?
3938 get_reflog_timestamp(revs
->reflog_info
) :
3942 enum commit_action
get_commit_action(struct rev_info
*revs
, struct commit
*commit
)
3944 if (commit
->object
.flags
& SHOWN
)
3945 return commit_ignore
;
3946 if (revs
->unpacked
&& has_object_pack(&commit
->object
.oid
))
3947 return commit_ignore
;
3948 if (revs
->no_kept_objects
) {
3949 if (has_object_kept_pack(&commit
->object
.oid
,
3950 revs
->keep_pack_cache_flags
))
3951 return commit_ignore
;
3953 if (commit
->object
.flags
& UNINTERESTING
)
3954 return commit_ignore
;
3955 if (revs
->line_level_traverse
&& !want_ancestry(revs
)) {
3957 * In case of line-level log with parent rewriting
3958 * prepare_revision_walk() already took care of all line-level
3959 * log filtering, and there is nothing left to do here.
3961 * If parent rewriting was not requested, then this is the
3962 * place to perform the line-level log filtering. Notably,
3963 * this check, though expensive, must come before the other,
3964 * cheaper filtering conditions, because the tracked line
3965 * ranges must be adjusted even when the commit will end up
3966 * being ignored based on other conditions.
3968 if (!line_log_process_ranges_arbitrary_commit(revs
, commit
))
3969 return commit_ignore
;
3971 if (revs
->min_age
!= -1 &&
3972 comparison_date(revs
, commit
) > revs
->min_age
)
3973 return commit_ignore
;
3974 if (revs
->max_age_as_filter
!= -1 &&
3975 comparison_date(revs
, commit
) < revs
->max_age_as_filter
)
3976 return commit_ignore
;
3977 if (revs
->min_parents
|| (revs
->max_parents
>= 0)) {
3978 int n
= commit_list_count(commit
->parents
);
3979 if ((n
< revs
->min_parents
) ||
3980 ((revs
->max_parents
>= 0) && (n
> revs
->max_parents
)))
3981 return commit_ignore
;
3983 if (!commit_match(commit
, revs
))
3984 return commit_ignore
;
3985 if (revs
->prune
&& revs
->dense
) {
3986 /* Commit without changes? */
3987 if (commit
->object
.flags
& TREESAME
) {
3989 struct commit_list
*p
;
3990 /* drop merges unless we want parenthood */
3991 if (!want_ancestry(revs
))
3992 return commit_ignore
;
3994 if (revs
->show_pulls
&& (commit
->object
.flags
& PULL_MERGE
))
3998 * If we want ancestry, then need to keep any merges
3999 * between relevant commits to tie together topology.
4000 * For consistency with TREESAME and simplification
4001 * use "relevant" here rather than just INTERESTING,
4002 * to treat bottom commit(s) as part of the topology.
4004 for (n
= 0, p
= commit
->parents
; p
; p
= p
->next
)
4005 if (relevant_commit(p
->item
))
4008 return commit_ignore
;
4014 define_commit_slab(saved_parents
, struct commit_list
*);
4016 #define EMPTY_PARENT_LIST ((struct commit_list *)-1)
4019 * You may only call save_parents() once per commit (this is checked
4020 * for non-root commits).
4022 static void save_parents(struct rev_info
*revs
, struct commit
*commit
)
4024 struct commit_list
**pp
;
4026 if (!revs
->saved_parents_slab
) {
4027 revs
->saved_parents_slab
= xmalloc(sizeof(struct saved_parents
));
4028 init_saved_parents(revs
->saved_parents_slab
);
4031 pp
= saved_parents_at(revs
->saved_parents_slab
, commit
);
4034 * When walking with reflogs, we may visit the same commit
4035 * several times: once for each appearance in the reflog.
4037 * In this case, save_parents() will be called multiple times.
4038 * We want to keep only the first set of parents. We need to
4039 * store a sentinel value for an empty (i.e., NULL) parent
4040 * list to distinguish it from a not-yet-saved list, however.
4044 if (commit
->parents
)
4045 *pp
= copy_commit_list(commit
->parents
);
4047 *pp
= EMPTY_PARENT_LIST
;
4050 static void free_saved_parents(struct rev_info
*revs
)
4052 if (revs
->saved_parents_slab
)
4053 clear_saved_parents(revs
->saved_parents_slab
);
4056 struct commit_list
*get_saved_parents(struct rev_info
*revs
, const struct commit
*commit
)
4058 struct commit_list
*parents
;
4060 if (!revs
->saved_parents_slab
)
4061 return commit
->parents
;
4063 parents
= *saved_parents_at(revs
->saved_parents_slab
, commit
);
4064 if (parents
== EMPTY_PARENT_LIST
)
4069 enum commit_action
simplify_commit(struct rev_info
*revs
, struct commit
*commit
)
4071 enum commit_action action
= get_commit_action(revs
, commit
);
4073 if (action
== commit_show
&&
4074 revs
->prune
&& revs
->dense
&& want_ancestry(revs
)) {
4076 * --full-diff on simplified parents is no good: it
4077 * will show spurious changes from the commits that
4078 * were elided. So we save the parents on the side
4079 * when --full-diff is in effect.
4081 if (revs
->full_diff
)
4082 save_parents(revs
, commit
);
4083 if (rewrite_parents(revs
, commit
, rewrite_one
) < 0)
4084 return commit_error
;
4089 static void track_linear(struct rev_info
*revs
, struct commit
*commit
)
4091 if (revs
->track_first_time
) {
4093 revs
->track_first_time
= 0;
4095 struct commit_list
*p
;
4096 for (p
= revs
->previous_parents
; p
; p
= p
->next
)
4097 if (p
->item
== NULL
|| /* first commit */
4098 oideq(&p
->item
->object
.oid
, &commit
->object
.oid
))
4100 revs
->linear
= p
!= NULL
;
4102 if (revs
->reverse
) {
4104 commit
->object
.flags
|= TRACK_LINEAR
;
4106 free_commit_list(revs
->previous_parents
);
4107 revs
->previous_parents
= copy_commit_list(commit
->parents
);
4110 static struct commit
*get_revision_1(struct rev_info
*revs
)
4113 struct commit
*commit
;
4115 if (revs
->reflog_info
)
4116 commit
= next_reflog_entry(revs
->reflog_info
);
4117 else if (revs
->topo_walk_info
)
4118 commit
= next_topo_commit(revs
);
4120 commit
= pop_commit(&revs
->commits
);
4125 if (revs
->reflog_info
)
4126 commit
->object
.flags
&= ~(ADDED
| SEEN
| SHOWN
);
4129 * If we haven't done the list limiting, we need to look at
4130 * the parents here. We also need to do the date-based limiting
4131 * that we'd otherwise have done in limit_list().
4133 if (!revs
->limited
) {
4134 if (revs
->max_age
!= -1 &&
4135 comparison_date(revs
, commit
) < revs
->max_age
)
4138 if (revs
->reflog_info
)
4139 try_to_simplify_commit(revs
, commit
);
4140 else if (revs
->topo_walk_info
)
4141 expand_topo_walk(revs
, commit
);
4142 else if (process_parents(revs
, commit
, &revs
->commits
, NULL
) < 0) {
4143 if (!revs
->ignore_missing_links
)
4144 die("Failed to traverse parents of commit %s",
4145 oid_to_hex(&commit
->object
.oid
));
4149 switch (simplify_commit(revs
, commit
)) {
4153 die("Failed to simplify parents of commit %s",
4154 oid_to_hex(&commit
->object
.oid
));
4156 if (revs
->track_linear
)
4157 track_linear(revs
, commit
);
4164 * Return true for entries that have not yet been shown. (This is an
4165 * object_array_each_func_t.)
4167 static int entry_unshown(struct object_array_entry
*entry
, void *cb_data UNUSED
)
4169 return !(entry
->item
->flags
& SHOWN
);
4173 * If array is on the verge of a realloc, garbage-collect any entries
4174 * that have already been shown to try to free up some space.
4176 static void gc_boundary(struct object_array
*array
)
4178 if (array
->nr
== array
->alloc
)
4179 object_array_filter(array
, entry_unshown
, NULL
);
4182 static void create_boundary_commit_list(struct rev_info
*revs
)
4186 struct object_array
*array
= &revs
->boundary_commits
;
4187 struct object_array_entry
*objects
= array
->objects
;
4190 * If revs->commits is non-NULL at this point, an error occurred in
4191 * get_revision_1(). Ignore the error and continue printing the
4192 * boundary commits anyway. (This is what the code has always
4195 free_commit_list(revs
->commits
);
4196 revs
->commits
= NULL
;
4199 * Put all of the actual boundary commits from revs->boundary_commits
4200 * into revs->commits
4202 for (i
= 0; i
< array
->nr
; i
++) {
4203 c
= (struct commit
*)(objects
[i
].item
);
4206 if (!(c
->object
.flags
& CHILD_SHOWN
))
4208 if (c
->object
.flags
& (SHOWN
| BOUNDARY
))
4210 c
->object
.flags
|= BOUNDARY
;
4211 commit_list_insert(c
, &revs
->commits
);
4215 * If revs->topo_order is set, sort the boundary commits
4216 * in topological order
4218 sort_in_topological_order(&revs
->commits
, revs
->sort_order
);
4221 static struct commit
*get_revision_internal(struct rev_info
*revs
)
4223 struct commit
*c
= NULL
;
4224 struct commit_list
*l
;
4226 if (revs
->boundary
== 2) {
4228 * All of the normal commits have already been returned,
4229 * and we are now returning boundary commits.
4230 * create_boundary_commit_list() has populated
4231 * revs->commits with the remaining commits to return.
4233 c
= pop_commit(&revs
->commits
);
4235 c
->object
.flags
|= SHOWN
;
4240 * If our max_count counter has reached zero, then we are done. We
4241 * don't simply return NULL because we still might need to show
4242 * boundary commits. But we want to avoid calling get_revision_1, which
4243 * might do a considerable amount of work finding the next commit only
4244 * for us to throw it away.
4246 * If it is non-zero, then either we don't have a max_count at all
4247 * (-1), or it is still counting, in which case we decrement.
4249 if (revs
->max_count
) {
4250 c
= get_revision_1(revs
);
4252 while (revs
->skip_count
> 0) {
4254 c
= get_revision_1(revs
);
4260 if (revs
->max_count
> 0)
4265 c
->object
.flags
|= SHOWN
;
4267 if (!revs
->boundary
)
4272 * get_revision_1() runs out the commits, and
4273 * we are done computing the boundaries.
4274 * switch to boundary commits output mode.
4279 * Update revs->commits to contain the list of
4282 create_boundary_commit_list(revs
);
4284 return get_revision_internal(revs
);
4288 * boundary commits are the commits that are parents of the
4289 * ones we got from get_revision_1() but they themselves are
4290 * not returned from get_revision_1(). Before returning
4291 * 'c', we need to mark its parents that they could be boundaries.
4294 for (l
= c
->parents
; l
; l
= l
->next
) {
4296 p
= &(l
->item
->object
);
4297 if (p
->flags
& (CHILD_SHOWN
| SHOWN
))
4299 p
->flags
|= CHILD_SHOWN
;
4300 gc_boundary(&revs
->boundary_commits
);
4301 add_object_array(p
, NULL
, &revs
->boundary_commits
);
4307 struct commit
*get_revision(struct rev_info
*revs
)
4310 struct commit_list
*reversed
;
4312 if (revs
->reverse
) {
4314 while ((c
= get_revision_internal(revs
)))
4315 commit_list_insert(c
, &reversed
);
4316 revs
->commits
= reversed
;
4318 revs
->reverse_output_stage
= 1;
4321 if (revs
->reverse_output_stage
) {
4322 c
= pop_commit(&revs
->commits
);
4323 if (revs
->track_linear
)
4324 revs
->linear
= !!(c
&& c
->object
.flags
& TRACK_LINEAR
);
4328 c
= get_revision_internal(revs
);
4329 if (c
&& revs
->graph
)
4330 graph_update(revs
->graph
, c
);
4332 free_saved_parents(revs
);
4333 free_commit_list(revs
->previous_parents
);
4334 revs
->previous_parents
= NULL
;
4339 const char *get_revision_mark(const struct rev_info
*revs
, const struct commit
*commit
)
4341 if (commit
->object
.flags
& BOUNDARY
)
4343 else if (commit
->object
.flags
& UNINTERESTING
)
4345 else if (commit
->object
.flags
& PATCHSAME
)
4347 else if (!revs
|| revs
->left_right
) {
4348 if (commit
->object
.flags
& SYMMETRIC_LEFT
)
4352 } else if (revs
->graph
)
4354 else if (revs
->cherry_mark
)
4359 void put_revision_mark(const struct rev_info
*revs
, const struct commit
*commit
)
4361 const char *mark
= get_revision_mark(revs
, commit
);
4364 fputs(mark
, stdout
);