2 #include "object-store.h"
8 #include "diff-merges.h"
11 #include "repository.h"
14 #include "reflog-walk.h"
15 #include "patch-ids.h"
18 #include "string-list.h"
21 #include "commit-slab.h"
23 #include "cache-tree.h"
28 #include "commit-reach.h"
29 #include "commit-graph.h"
30 #include "prio-queue.h"
34 #include "json-writer.h"
35 #include "list-objects-filter-options.h"
36 #include "resolve-undo.h"
38 volatile show_early_output_fn_t show_early_output
;
40 static const char *term_bad
;
41 static const char *term_good
;
43 implement_shared_commit_slab(revision_sources
, char *);
45 static inline int want_ancestry(const struct rev_info
*revs
);
47 void show_object_with_name(FILE *out
, struct object
*obj
, const char *name
)
49 fprintf(out
, "%s ", oid_to_hex(&obj
->oid
));
51 * This "for (const char *p = ..." is made as a first step towards
52 * making use of such declarations elsewhere in our codebase. If
53 * it causes compilation problems on your platform, please report
54 * it to the Git mailing list at git@vger.kernel.org. In the meantime,
55 * adding -std=gnu99 to CFLAGS may help if you are with older GCC.
57 for (const char *p
= name
; *p
&& *p
!= '\n'; p
++)
62 static void mark_blob_uninteresting(struct blob
*blob
)
66 if (blob
->object
.flags
& UNINTERESTING
)
68 blob
->object
.flags
|= UNINTERESTING
;
71 static void mark_tree_contents_uninteresting(struct repository
*r
,
74 struct tree_desc desc
;
75 struct name_entry entry
;
77 if (parse_tree_gently(tree
, 1) < 0)
80 init_tree_desc(&desc
, tree
->buffer
, tree
->size
);
81 while (tree_entry(&desc
, &entry
)) {
82 switch (object_type(entry
.mode
)) {
84 mark_tree_uninteresting(r
, lookup_tree(r
, &entry
.oid
));
87 mark_blob_uninteresting(lookup_blob(r
, &entry
.oid
));
90 /* Subproject commit - not in this repository */
96 * We don't care about the tree any more
97 * after it has been marked uninteresting.
99 free_tree_buffer(tree
);
102 void mark_tree_uninteresting(struct repository
*r
, struct tree
*tree
)
110 if (obj
->flags
& UNINTERESTING
)
112 obj
->flags
|= UNINTERESTING
;
113 mark_tree_contents_uninteresting(r
, tree
);
116 struct path_and_oids_entry
{
117 struct hashmap_entry ent
;
122 static int path_and_oids_cmp(const void *hashmap_cmp_fn_data
,
123 const struct hashmap_entry
*eptr
,
124 const struct hashmap_entry
*entry_or_key
,
127 const struct path_and_oids_entry
*e1
, *e2
;
129 e1
= container_of(eptr
, const struct path_and_oids_entry
, ent
);
130 e2
= container_of(entry_or_key
, const struct path_and_oids_entry
, ent
);
132 return strcmp(e1
->path
, e2
->path
);
135 static void paths_and_oids_clear(struct hashmap
*map
)
137 struct hashmap_iter iter
;
138 struct path_and_oids_entry
*entry
;
140 hashmap_for_each_entry(map
, &iter
, entry
, ent
/* member name */) {
141 oidset_clear(&entry
->trees
);
145 hashmap_clear_and_free(map
, struct path_and_oids_entry
, ent
);
148 static void paths_and_oids_insert(struct hashmap
*map
,
150 const struct object_id
*oid
)
152 int hash
= strhash(path
);
153 struct path_and_oids_entry key
;
154 struct path_and_oids_entry
*entry
;
156 hashmap_entry_init(&key
.ent
, hash
);
158 /* use a shallow copy for the lookup */
159 key
.path
= (char *)path
;
160 oidset_init(&key
.trees
, 0);
162 entry
= hashmap_get_entry(map
, &key
, ent
, NULL
);
164 CALLOC_ARRAY(entry
, 1);
165 hashmap_entry_init(&entry
->ent
, hash
);
166 entry
->path
= xstrdup(key
.path
);
167 oidset_init(&entry
->trees
, 16);
168 hashmap_put(map
, &entry
->ent
);
171 oidset_insert(&entry
->trees
, oid
);
174 static void add_children_by_path(struct repository
*r
,
178 struct tree_desc desc
;
179 struct name_entry entry
;
184 if (parse_tree_gently(tree
, 1) < 0)
187 init_tree_desc(&desc
, tree
->buffer
, tree
->size
);
188 while (tree_entry(&desc
, &entry
)) {
189 switch (object_type(entry
.mode
)) {
191 paths_and_oids_insert(map
, entry
.path
, &entry
.oid
);
193 if (tree
->object
.flags
& UNINTERESTING
) {
194 struct tree
*child
= lookup_tree(r
, &entry
.oid
);
196 child
->object
.flags
|= UNINTERESTING
;
200 if (tree
->object
.flags
& UNINTERESTING
) {
201 struct blob
*child
= lookup_blob(r
, &entry
.oid
);
203 child
->object
.flags
|= UNINTERESTING
;
207 /* Subproject commit - not in this repository */
212 free_tree_buffer(tree
);
215 void mark_trees_uninteresting_sparse(struct repository
*r
,
216 struct oidset
*trees
)
218 unsigned has_interesting
= 0, has_uninteresting
= 0;
219 struct hashmap map
= HASHMAP_INIT(path_and_oids_cmp
, NULL
);
220 struct hashmap_iter map_iter
;
221 struct path_and_oids_entry
*entry
;
222 struct object_id
*oid
;
223 struct oidset_iter iter
;
225 oidset_iter_init(trees
, &iter
);
226 while ((!has_interesting
|| !has_uninteresting
) &&
227 (oid
= oidset_iter_next(&iter
))) {
228 struct tree
*tree
= lookup_tree(r
, oid
);
233 if (tree
->object
.flags
& UNINTERESTING
)
234 has_uninteresting
= 1;
239 /* Do not walk unless we have both types of trees. */
240 if (!has_uninteresting
|| !has_interesting
)
243 oidset_iter_init(trees
, &iter
);
244 while ((oid
= oidset_iter_next(&iter
))) {
245 struct tree
*tree
= lookup_tree(r
, oid
);
246 add_children_by_path(r
, tree
, &map
);
249 hashmap_for_each_entry(&map
, &map_iter
, entry
, ent
/* member name */)
250 mark_trees_uninteresting_sparse(r
, &entry
->trees
);
252 paths_and_oids_clear(&map
);
255 struct commit_stack
{
256 struct commit
**items
;
259 #define COMMIT_STACK_INIT { 0 }
261 static void commit_stack_push(struct commit_stack
*stack
, struct commit
*commit
)
263 ALLOC_GROW(stack
->items
, stack
->nr
+ 1, stack
->alloc
);
264 stack
->items
[stack
->nr
++] = commit
;
267 static struct commit
*commit_stack_pop(struct commit_stack
*stack
)
269 return stack
->nr
? stack
->items
[--stack
->nr
] : NULL
;
272 static void commit_stack_clear(struct commit_stack
*stack
)
274 FREE_AND_NULL(stack
->items
);
275 stack
->nr
= stack
->alloc
= 0;
278 static void mark_one_parent_uninteresting(struct rev_info
*revs
, struct commit
*commit
,
279 struct commit_stack
*pending
)
281 struct commit_list
*l
;
283 if (commit
->object
.flags
& UNINTERESTING
)
285 commit
->object
.flags
|= UNINTERESTING
;
288 * Normally we haven't parsed the parent
289 * yet, so we won't have a parent of a parent
290 * here. However, it may turn out that we've
291 * reached this commit some other way (where it
292 * wasn't uninteresting), in which case we need
293 * to mark its parents recursively too..
295 for (l
= commit
->parents
; l
; l
= l
->next
) {
296 commit_stack_push(pending
, l
->item
);
297 if (revs
&& revs
->exclude_first_parent_only
)
302 void mark_parents_uninteresting(struct rev_info
*revs
, struct commit
*commit
)
304 struct commit_stack pending
= COMMIT_STACK_INIT
;
305 struct commit_list
*l
;
307 for (l
= commit
->parents
; l
; l
= l
->next
) {
308 mark_one_parent_uninteresting(revs
, l
->item
, &pending
);
309 if (revs
&& revs
->exclude_first_parent_only
)
313 while (pending
.nr
> 0)
314 mark_one_parent_uninteresting(revs
, commit_stack_pop(&pending
),
317 commit_stack_clear(&pending
);
320 static void add_pending_object_with_path(struct rev_info
*revs
,
322 const char *name
, unsigned mode
,
325 struct interpret_branch_name_options options
= { 0 };
328 if (revs
->no_walk
&& (obj
->flags
& UNINTERESTING
))
330 if (revs
->reflog_info
&& obj
->type
== OBJ_COMMIT
) {
331 struct strbuf buf
= STRBUF_INIT
;
332 size_t namelen
= strlen(name
);
333 int len
= interpret_branch_name(name
, namelen
, &buf
, &options
);
335 if (0 < len
&& len
< namelen
&& buf
.len
)
336 strbuf_addstr(&buf
, name
+ len
);
337 add_reflog_for_walk(revs
->reflog_info
,
338 (struct commit
*)obj
,
339 buf
.buf
[0] ? buf
.buf
: name
);
340 strbuf_release(&buf
);
341 return; /* do not add the commit itself */
343 add_object_array_with_path(obj
, name
, &revs
->pending
, mode
, path
);
346 static void add_pending_object_with_mode(struct rev_info
*revs
,
348 const char *name
, unsigned mode
)
350 add_pending_object_with_path(revs
, obj
, name
, mode
, NULL
);
353 void add_pending_object(struct rev_info
*revs
,
354 struct object
*obj
, const char *name
)
356 add_pending_object_with_mode(revs
, obj
, name
, S_IFINVALID
);
359 void add_head_to_pending(struct rev_info
*revs
)
361 struct object_id oid
;
363 if (get_oid("HEAD", &oid
))
365 obj
= parse_object(revs
->repo
, &oid
);
368 add_pending_object(revs
, obj
, "HEAD");
371 static struct object
*get_reference(struct rev_info
*revs
, const char *name
,
372 const struct object_id
*oid
,
375 struct object
*object
;
377 object
= parse_object_with_flags(revs
->repo
, oid
,
378 revs
->verify_objects
? 0 :
379 PARSE_OBJECT_SKIP_HASH_CHECK
);
382 if (revs
->ignore_missing
)
384 if (revs
->exclude_promisor_objects
&& is_promisor_object(oid
))
386 die("bad object %s", name
);
388 object
->flags
|= flags
;
392 void add_pending_oid(struct rev_info
*revs
, const char *name
,
393 const struct object_id
*oid
, unsigned int flags
)
395 struct object
*object
= get_reference(revs
, name
, oid
, flags
);
396 add_pending_object(revs
, object
, name
);
399 static struct commit
*handle_commit(struct rev_info
*revs
,
400 struct object_array_entry
*entry
)
402 struct object
*object
= entry
->item
;
403 const char *name
= entry
->name
;
404 const char *path
= entry
->path
;
405 unsigned int mode
= entry
->mode
;
406 unsigned long flags
= object
->flags
;
409 * Tag object? Look what it points to..
411 while (object
->type
== OBJ_TAG
) {
412 struct tag
*tag
= (struct tag
*) object
;
413 if (revs
->tag_objects
&& !(flags
& UNINTERESTING
))
414 add_pending_object(revs
, object
, tag
->tag
);
415 object
= parse_object(revs
->repo
, get_tagged_oid(tag
));
417 if (revs
->ignore_missing_links
|| (flags
& UNINTERESTING
))
419 if (revs
->exclude_promisor_objects
&&
420 is_promisor_object(&tag
->tagged
->oid
))
422 die("bad object %s", oid_to_hex(&tag
->tagged
->oid
));
424 object
->flags
|= flags
;
426 * We'll handle the tagged object by looping or dropping
427 * through to the non-tag handlers below. Do not
428 * propagate path data from the tag's pending entry.
435 * Commit object? Just return it, we'll do all the complex
438 if (object
->type
== OBJ_COMMIT
) {
439 struct commit
*commit
= (struct commit
*)object
;
441 if (repo_parse_commit(revs
->repo
, commit
) < 0)
442 die("unable to parse commit %s", name
);
443 if (flags
& UNINTERESTING
) {
444 mark_parents_uninteresting(revs
, commit
);
446 if (!revs
->topo_order
|| !generation_numbers_enabled(the_repository
))
450 char **slot
= revision_sources_at(revs
->sources
, commit
);
453 *slot
= xstrdup(name
);
459 * Tree object? Either mark it uninteresting, or add it
460 * to the list of objects to look at later..
462 if (object
->type
== OBJ_TREE
) {
463 struct tree
*tree
= (struct tree
*)object
;
464 if (!revs
->tree_objects
)
466 if (flags
& UNINTERESTING
) {
467 mark_tree_contents_uninteresting(revs
->repo
, tree
);
470 add_pending_object_with_path(revs
, object
, name
, mode
, path
);
475 * Blob object? You know the drill by now..
477 if (object
->type
== OBJ_BLOB
) {
478 if (!revs
->blob_objects
)
480 if (flags
& UNINTERESTING
)
482 add_pending_object_with_path(revs
, object
, name
, mode
, path
);
485 die("%s is unknown object", name
);
488 static int everybody_uninteresting(struct commit_list
*orig
,
489 struct commit
**interesting_cache
)
491 struct commit_list
*list
= orig
;
493 if (*interesting_cache
) {
494 struct commit
*commit
= *interesting_cache
;
495 if (!(commit
->object
.flags
& UNINTERESTING
))
500 struct commit
*commit
= list
->item
;
502 if (commit
->object
.flags
& UNINTERESTING
)
505 *interesting_cache
= commit
;
512 * A definition of "relevant" commit that we can use to simplify limited graphs
513 * by eliminating side branches.
515 * A "relevant" commit is one that is !UNINTERESTING (ie we are including it
516 * in our list), or that is a specified BOTTOM commit. Then after computing
517 * a limited list, during processing we can generally ignore boundary merges
518 * coming from outside the graph, (ie from irrelevant parents), and treat
519 * those merges as if they were single-parent. TREESAME is defined to consider
520 * only relevant parents, if any. If we are TREESAME to our on-graph parents,
521 * we don't care if we were !TREESAME to non-graph parents.
523 * Treating bottom commits as relevant ensures that a limited graph's
524 * connection to the actual bottom commit is not viewed as a side branch, but
525 * treated as part of the graph. For example:
527 * ....Z...A---X---o---o---B
531 * When computing "A..B", the A-X connection is at least as important as
532 * Y-X, despite A being flagged UNINTERESTING.
534 * And when computing --ancestry-path "A..B", the A-X connection is more
535 * important than Y-X, despite both A and Y being flagged UNINTERESTING.
537 static inline int relevant_commit(struct commit
*commit
)
539 return (commit
->object
.flags
& (UNINTERESTING
| BOTTOM
)) != UNINTERESTING
;
543 * Return a single relevant commit from a parent list. If we are a TREESAME
544 * commit, and this selects one of our parents, then we can safely simplify to
547 static struct commit
*one_relevant_parent(const struct rev_info
*revs
,
548 struct commit_list
*orig
)
550 struct commit_list
*list
= orig
;
551 struct commit
*relevant
= NULL
;
557 * For 1-parent commits, or if first-parent-only, then return that
558 * first parent (even if not "relevant" by the above definition).
559 * TREESAME will have been set purely on that parent.
561 if (revs
->first_parent_only
|| !orig
->next
)
565 * For multi-parent commits, identify a sole relevant parent, if any.
566 * If we have only one relevant parent, then TREESAME will be set purely
567 * with regard to that parent, and we can simplify accordingly.
569 * If we have more than one relevant parent, or no relevant parents
570 * (and multiple irrelevant ones), then we can't select a parent here
574 struct commit
*commit
= list
->item
;
576 if (relevant_commit(commit
)) {
586 * The goal is to get REV_TREE_NEW as the result only if the
587 * diff consists of all '+' (and no other changes), REV_TREE_OLD
588 * if the whole diff is removal of old data, and otherwise
589 * REV_TREE_DIFFERENT (of course if the trees are the same we
590 * want REV_TREE_SAME).
592 * The only time we care about the distinction is when
593 * remove_empty_trees is in effect, in which case we care only about
594 * whether the whole change is REV_TREE_NEW, or if there's another type
595 * of change. Which means we can stop the diff early in either of these
598 * 1. We're not using remove_empty_trees at all.
600 * 2. We saw anything except REV_TREE_NEW.
602 #define REV_TREE_SAME 0
603 #define REV_TREE_NEW 1 /* Only new files */
604 #define REV_TREE_OLD 2 /* Only files removed */
605 #define REV_TREE_DIFFERENT 3 /* Mixed changes */
606 static int tree_difference
= REV_TREE_SAME
;
608 static void file_add_remove(struct diff_options
*options
,
609 int addremove
, unsigned mode
,
610 const struct object_id
*oid
,
612 const char *fullpath
, unsigned dirty_submodule
)
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
, unsigned new_mode
,
624 const struct object_id
*old_oid
,
625 const struct object_id
*new_oid
,
626 int old_oid_valid
, int new_oid_valid
,
627 const char *fullpath
,
628 unsigned old_dirty_submodule
, unsigned new_dirty_submodule
)
630 tree_difference
= REV_TREE_DIFFERENT
;
631 options
->flags
.has_changes
= 1;
634 static int bloom_filter_atexit_registered
;
635 static unsigned int count_bloom_filter_maybe
;
636 static unsigned int count_bloom_filter_definitely_not
;
637 static unsigned int count_bloom_filter_false_positive
;
638 static unsigned int count_bloom_filter_not_present
;
640 static void trace2_bloom_filter_statistics_atexit(void)
642 struct json_writer jw
= JSON_WRITER_INIT
;
644 jw_object_begin(&jw
, 0);
645 jw_object_intmax(&jw
, "filter_not_present", count_bloom_filter_not_present
);
646 jw_object_intmax(&jw
, "maybe", count_bloom_filter_maybe
);
647 jw_object_intmax(&jw
, "definitely_not", count_bloom_filter_definitely_not
);
648 jw_object_intmax(&jw
, "false_positive", count_bloom_filter_false_positive
);
651 trace2_data_json("bloom", the_repository
, "statistics", &jw
);
656 static int forbid_bloom_filters(struct pathspec
*spec
)
658 if (spec
->has_wildcard
)
662 if (spec
->magic
& ~PATHSPEC_LITERAL
)
664 if (spec
->nr
&& (spec
->items
[0].magic
& ~PATHSPEC_LITERAL
))
670 static void prepare_to_use_bloom_filter(struct rev_info
*revs
)
672 struct pathspec_item
*pi
;
673 char *path_alloc
= NULL
;
674 const char *path
, *p
;
676 int path_component_nr
= 1;
681 if (forbid_bloom_filters(&revs
->prune_data
))
684 repo_parse_commit(revs
->repo
, revs
->commits
->item
);
686 revs
->bloom_filter_settings
= get_bloom_filter_settings(revs
->repo
);
687 if (!revs
->bloom_filter_settings
)
690 if (!revs
->pruning
.pathspec
.nr
)
693 pi
= &revs
->pruning
.pathspec
.items
[0];
695 /* remove single trailing slash from path, if needed */
696 if (pi
->len
> 0 && pi
->match
[pi
->len
- 1] == '/') {
697 path_alloc
= xmemdupz(pi
->match
, pi
->len
- 1);
704 revs
->bloom_filter_settings
= NULL
;
712 * At this point, the path is normalized to use Unix-style
713 * path separators. This is required due to how the
714 * changed-path Bloom filters store the paths.
721 revs
->bloom_keys_nr
= path_component_nr
;
722 ALLOC_ARRAY(revs
->bloom_keys
, revs
->bloom_keys_nr
);
724 fill_bloom_key(path
, len
, &revs
->bloom_keys
[0],
725 revs
->bloom_filter_settings
);
726 path_component_nr
= 1;
731 fill_bloom_key(path
, p
- path
,
732 &revs
->bloom_keys
[path_component_nr
++],
733 revs
->bloom_filter_settings
);
737 if (trace2_is_enabled() && !bloom_filter_atexit_registered
) {
738 atexit(trace2_bloom_filter_statistics_atexit
);
739 bloom_filter_atexit_registered
= 1;
745 static int check_maybe_different_in_bloom_filter(struct rev_info
*revs
,
746 struct commit
*commit
)
748 struct bloom_filter
*filter
;
751 if (!revs
->repo
->objects
->commit_graph
)
754 if (commit_graph_generation(commit
) == GENERATION_NUMBER_INFINITY
)
757 filter
= get_bloom_filter(revs
->repo
, commit
);
760 count_bloom_filter_not_present
++;
764 for (j
= 0; result
&& j
< revs
->bloom_keys_nr
; j
++) {
765 result
= bloom_filter_contains(filter
,
766 &revs
->bloom_keys
[j
],
767 revs
->bloom_filter_settings
);
771 count_bloom_filter_maybe
++;
773 count_bloom_filter_definitely_not
++;
778 static int rev_compare_tree(struct rev_info
*revs
,
779 struct commit
*parent
, struct commit
*commit
, int nth_parent
)
781 struct tree
*t1
= get_commit_tree(parent
);
782 struct tree
*t2
= get_commit_tree(commit
);
790 if (revs
->simplify_by_decoration
) {
792 * If we are simplifying by decoration, then the commit
793 * is worth showing if it has a tag pointing at it.
795 if (get_name_decoration(&commit
->object
))
796 return REV_TREE_DIFFERENT
;
798 * A commit that is not pointed by a tag is uninteresting
799 * if we are not limited by path. This means that you will
800 * see the usual "commits that touch the paths" plus any
801 * tagged commit by specifying both --simplify-by-decoration
804 if (!revs
->prune_data
.nr
)
805 return REV_TREE_SAME
;
808 if (revs
->bloom_keys_nr
&& !nth_parent
) {
809 bloom_ret
= check_maybe_different_in_bloom_filter(revs
, commit
);
812 return REV_TREE_SAME
;
815 tree_difference
= REV_TREE_SAME
;
816 revs
->pruning
.flags
.has_changes
= 0;
817 diff_tree_oid(&t1
->object
.oid
, &t2
->object
.oid
, "", &revs
->pruning
);
820 if (bloom_ret
== 1 && tree_difference
== REV_TREE_SAME
)
821 count_bloom_filter_false_positive
++;
823 return tree_difference
;
826 static int rev_same_tree_as_empty(struct rev_info
*revs
, struct commit
*commit
)
828 struct tree
*t1
= get_commit_tree(commit
);
833 tree_difference
= REV_TREE_SAME
;
834 revs
->pruning
.flags
.has_changes
= 0;
835 diff_tree_oid(NULL
, &t1
->object
.oid
, "", &revs
->pruning
);
837 return tree_difference
== REV_TREE_SAME
;
840 struct treesame_state
{
841 unsigned int nparents
;
842 unsigned char treesame
[FLEX_ARRAY
];
845 static struct treesame_state
*initialise_treesame(struct rev_info
*revs
, struct commit
*commit
)
847 unsigned n
= commit_list_count(commit
->parents
);
848 struct treesame_state
*st
= xcalloc(1, st_add(sizeof(*st
), n
));
850 add_decoration(&revs
->treesame
, &commit
->object
, st
);
855 * Must be called immediately after removing the nth_parent from a commit's
856 * parent list, if we are maintaining the per-parent treesame[] decoration.
857 * This does not recalculate the master TREESAME flag - update_treesame()
858 * should be called to update it after a sequence of treesame[] modifications
859 * that may have affected it.
861 static int compact_treesame(struct rev_info
*revs
, struct commit
*commit
, unsigned nth_parent
)
863 struct treesame_state
*st
;
866 if (!commit
->parents
) {
868 * Have just removed the only parent from a non-merge.
869 * Different handling, as we lack decoration.
872 die("compact_treesame %u", nth_parent
);
873 old_same
= !!(commit
->object
.flags
& TREESAME
);
874 if (rev_same_tree_as_empty(revs
, commit
))
875 commit
->object
.flags
|= TREESAME
;
877 commit
->object
.flags
&= ~TREESAME
;
881 st
= lookup_decoration(&revs
->treesame
, &commit
->object
);
882 if (!st
|| nth_parent
>= st
->nparents
)
883 die("compact_treesame %u", nth_parent
);
885 old_same
= st
->treesame
[nth_parent
];
886 memmove(st
->treesame
+ nth_parent
,
887 st
->treesame
+ nth_parent
+ 1,
888 st
->nparents
- nth_parent
- 1);
891 * If we've just become a non-merge commit, update TREESAME
892 * immediately, and remove the no-longer-needed decoration.
893 * If still a merge, defer update until update_treesame().
895 if (--st
->nparents
== 1) {
896 if (commit
->parents
->next
)
897 die("compact_treesame parents mismatch");
898 if (st
->treesame
[0] && revs
->dense
)
899 commit
->object
.flags
|= TREESAME
;
901 commit
->object
.flags
&= ~TREESAME
;
902 free(add_decoration(&revs
->treesame
, &commit
->object
, NULL
));
908 static unsigned update_treesame(struct rev_info
*revs
, struct commit
*commit
)
910 if (commit
->parents
&& commit
->parents
->next
) {
912 struct treesame_state
*st
;
913 struct commit_list
*p
;
914 unsigned relevant_parents
;
915 unsigned relevant_change
, irrelevant_change
;
917 st
= lookup_decoration(&revs
->treesame
, &commit
->object
);
919 die("update_treesame %s", oid_to_hex(&commit
->object
.oid
));
920 relevant_parents
= 0;
921 relevant_change
= irrelevant_change
= 0;
922 for (p
= commit
->parents
, n
= 0; p
; n
++, p
= p
->next
) {
923 if (relevant_commit(p
->item
)) {
924 relevant_change
|= !st
->treesame
[n
];
927 irrelevant_change
|= !st
->treesame
[n
];
929 if (relevant_parents
? relevant_change
: irrelevant_change
)
930 commit
->object
.flags
&= ~TREESAME
;
932 commit
->object
.flags
|= TREESAME
;
935 return commit
->object
.flags
& TREESAME
;
938 static inline int limiting_can_increase_treesame(const struct rev_info
*revs
)
941 * TREESAME is irrelevant unless prune && dense;
942 * if simplify_history is set, we can't have a mixture of TREESAME and
943 * !TREESAME INTERESTING parents (and we don't have treesame[]
944 * decoration anyway);
945 * if first_parent_only is set, then the TREESAME flag is locked
946 * against the first parent (and again we lack treesame[] decoration).
948 return revs
->prune
&& revs
->dense
&&
949 !revs
->simplify_history
&&
950 !revs
->first_parent_only
;
953 static void try_to_simplify_commit(struct rev_info
*revs
, struct commit
*commit
)
955 struct commit_list
**pp
, *parent
;
956 struct treesame_state
*ts
= NULL
;
957 int relevant_change
= 0, irrelevant_change
= 0;
958 int relevant_parents
, nth_parent
;
961 * If we don't do pruning, everything is interesting
966 if (!get_commit_tree(commit
))
969 if (!commit
->parents
) {
970 if (rev_same_tree_as_empty(revs
, commit
))
971 commit
->object
.flags
|= TREESAME
;
976 * Normal non-merge commit? If we don't want to make the
977 * history dense, we consider it always to be a change..
979 if (!revs
->dense
&& !commit
->parents
->next
)
982 for (pp
= &commit
->parents
, nth_parent
= 0, relevant_parents
= 0;
983 (parent
= *pp
) != NULL
;
984 pp
= &parent
->next
, nth_parent
++) {
985 struct commit
*p
= parent
->item
;
986 if (relevant_commit(p
))
989 if (nth_parent
== 1) {
991 * This our second loop iteration - so we now know
992 * we're dealing with a merge.
994 * Do not compare with later parents when we care only about
995 * the first parent chain, in order to avoid derailing the
996 * traversal to follow a side branch that brought everything
997 * in the path we are limited to by the pathspec.
999 if (revs
->first_parent_only
)
1002 * If this will remain a potentially-simplifiable
1003 * merge, remember per-parent treesame if needed.
1004 * Initialise the array with the comparison from our
1007 if (revs
->treesame
.name
&&
1008 !revs
->simplify_history
&&
1009 !(commit
->object
.flags
& UNINTERESTING
)) {
1010 ts
= initialise_treesame(revs
, commit
);
1011 if (!(irrelevant_change
|| relevant_change
))
1012 ts
->treesame
[0] = 1;
1015 if (repo_parse_commit(revs
->repo
, p
) < 0)
1016 die("cannot simplify commit %s (because of %s)",
1017 oid_to_hex(&commit
->object
.oid
),
1018 oid_to_hex(&p
->object
.oid
));
1019 switch (rev_compare_tree(revs
, p
, commit
, nth_parent
)) {
1021 if (!revs
->simplify_history
|| !relevant_commit(p
)) {
1022 /* Even if a merge with an uninteresting
1023 * side branch brought the entire change
1024 * we are interested in, we do not want
1025 * to lose the other branches of this
1026 * merge, so we just keep going.
1029 ts
->treesame
[nth_parent
] = 1;
1032 parent
->next
= NULL
;
1033 commit
->parents
= parent
;
1036 * A merge commit is a "diversion" if it is not
1037 * TREESAME to its first parent but is TREESAME
1038 * to a later parent. In the simplified history,
1039 * we "divert" the history walk to the later
1040 * parent. These commits are shown when "show_pulls"
1041 * is enabled, so do not mark the object as
1044 if (!revs
->show_pulls
|| !nth_parent
)
1045 commit
->object
.flags
|= TREESAME
;
1050 if (revs
->remove_empty_trees
&&
1051 rev_same_tree_as_empty(revs
, p
)) {
1052 /* We are adding all the specified
1053 * paths from this parent, so the
1054 * history beyond this parent is not
1055 * interesting. Remove its parents
1056 * (they are grandparents for us).
1057 * IOW, we pretend this parent is a
1060 if (repo_parse_commit(revs
->repo
, p
) < 0)
1061 die("cannot simplify commit %s (invalid %s)",
1062 oid_to_hex(&commit
->object
.oid
),
1063 oid_to_hex(&p
->object
.oid
));
1068 case REV_TREE_DIFFERENT
:
1069 if (relevant_commit(p
))
1070 relevant_change
= 1;
1072 irrelevant_change
= 1;
1075 commit
->object
.flags
|= PULL_MERGE
;
1079 die("bad tree compare for commit %s", oid_to_hex(&commit
->object
.oid
));
1083 * TREESAME is straightforward for single-parent commits. For merge
1084 * commits, it is most useful to define it so that "irrelevant"
1085 * parents cannot make us !TREESAME - if we have any relevant
1086 * parents, then we only consider TREESAMEness with respect to them,
1087 * allowing irrelevant merges from uninteresting branches to be
1088 * simplified away. Only if we have only irrelevant parents do we
1089 * base TREESAME on them. Note that this logic is replicated in
1090 * update_treesame, which should be kept in sync.
1092 if (relevant_parents
? !relevant_change
: !irrelevant_change
)
1093 commit
->object
.flags
|= TREESAME
;
1096 static int process_parents(struct rev_info
*revs
, struct commit
*commit
,
1097 struct commit_list
**list
, struct prio_queue
*queue
)
1099 struct commit_list
*parent
= commit
->parents
;
1100 unsigned pass_flags
;
1102 if (commit
->object
.flags
& ADDED
)
1104 commit
->object
.flags
|= ADDED
;
1106 if (revs
->include_check
&&
1107 !revs
->include_check(commit
, revs
->include_check_data
))
1111 * If the commit is uninteresting, don't try to
1112 * prune parents - we want the maximal uninteresting
1115 * Normally we haven't parsed the parent
1116 * yet, so we won't have a parent of a parent
1117 * here. However, it may turn out that we've
1118 * reached this commit some other way (where it
1119 * wasn't uninteresting), in which case we need
1120 * to mark its parents recursively too..
1122 if (commit
->object
.flags
& UNINTERESTING
) {
1124 struct commit
*p
= parent
->item
;
1125 parent
= parent
->next
;
1127 p
->object
.flags
|= UNINTERESTING
;
1128 if (repo_parse_commit_gently(revs
->repo
, p
, 1) < 0)
1131 mark_parents_uninteresting(revs
, p
);
1132 if (p
->object
.flags
& SEEN
)
1134 p
->object
.flags
|= (SEEN
| NOT_USER_GIVEN
);
1136 commit_list_insert_by_date(p
, list
);
1138 prio_queue_put(queue
, p
);
1139 if (revs
->exclude_first_parent_only
)
1146 * Ok, the commit wasn't uninteresting. Try to
1147 * simplify the commit history and find the parent
1148 * that has no differences in the path set if one exists.
1150 try_to_simplify_commit(revs
, commit
);
1155 pass_flags
= (commit
->object
.flags
& (SYMMETRIC_LEFT
| ANCESTRY_PATH
));
1157 for (parent
= commit
->parents
; parent
; parent
= parent
->next
) {
1158 struct commit
*p
= parent
->item
;
1159 int gently
= revs
->ignore_missing_links
||
1160 revs
->exclude_promisor_objects
;
1161 if (repo_parse_commit_gently(revs
->repo
, p
, gently
) < 0) {
1162 if (revs
->exclude_promisor_objects
&&
1163 is_promisor_object(&p
->object
.oid
)) {
1164 if (revs
->first_parent_only
)
1170 if (revs
->sources
) {
1171 char **slot
= revision_sources_at(revs
->sources
, p
);
1174 *slot
= *revision_sources_at(revs
->sources
, commit
);
1176 p
->object
.flags
|= pass_flags
;
1177 if (!(p
->object
.flags
& SEEN
)) {
1178 p
->object
.flags
|= (SEEN
| NOT_USER_GIVEN
);
1180 commit_list_insert_by_date(p
, list
);
1182 prio_queue_put(queue
, p
);
1184 if (revs
->first_parent_only
)
1190 static void cherry_pick_list(struct commit_list
*list
, struct rev_info
*revs
)
1192 struct commit_list
*p
;
1193 int left_count
= 0, right_count
= 0;
1195 struct patch_ids ids
;
1196 unsigned cherry_flag
;
1198 /* First count the commits on the left and on the right */
1199 for (p
= list
; p
; p
= p
->next
) {
1200 struct commit
*commit
= p
->item
;
1201 unsigned flags
= commit
->object
.flags
;
1202 if (flags
& BOUNDARY
)
1204 else if (flags
& SYMMETRIC_LEFT
)
1210 if (!left_count
|| !right_count
)
1213 left_first
= left_count
< right_count
;
1214 init_patch_ids(revs
->repo
, &ids
);
1215 ids
.diffopts
.pathspec
= revs
->diffopt
.pathspec
;
1217 /* Compute patch-ids for one side */
1218 for (p
= list
; p
; p
= p
->next
) {
1219 struct commit
*commit
= p
->item
;
1220 unsigned flags
= commit
->object
.flags
;
1222 if (flags
& BOUNDARY
)
1225 * If we have fewer left, left_first is set and we omit
1226 * commits on the right branch in this loop. If we have
1227 * fewer right, we skip the left ones.
1229 if (left_first
!= !!(flags
& SYMMETRIC_LEFT
))
1231 add_commit_patch_id(commit
, &ids
);
1234 /* either cherry_mark or cherry_pick are true */
1235 cherry_flag
= revs
->cherry_mark
? PATCHSAME
: SHOWN
;
1237 /* Check the other side */
1238 for (p
= list
; p
; p
= p
->next
) {
1239 struct commit
*commit
= p
->item
;
1240 struct patch_id
*id
;
1241 unsigned flags
= commit
->object
.flags
;
1243 if (flags
& BOUNDARY
)
1246 * If we have fewer left, left_first is set and we omit
1247 * commits on the left branch in this loop.
1249 if (left_first
== !!(flags
& SYMMETRIC_LEFT
))
1253 * Have we seen the same patch id?
1255 id
= patch_id_iter_first(commit
, &ids
);
1259 commit
->object
.flags
|= cherry_flag
;
1261 id
->commit
->object
.flags
|= cherry_flag
;
1262 } while ((id
= patch_id_iter_next(id
, &ids
)));
1265 free_patch_ids(&ids
);
1268 /* How many extra uninteresting commits we want to see.. */
1271 static int still_interesting(struct commit_list
*src
, timestamp_t date
, int slop
,
1272 struct commit
**interesting_cache
)
1275 * No source list at all? We're definitely done..
1281 * Does the destination list contain entries with a date
1282 * before the source list? Definitely _not_ done.
1284 if (date
<= src
->item
->date
)
1288 * Does the source list still have interesting commits in
1289 * it? Definitely not done..
1291 if (!everybody_uninteresting(src
, interesting_cache
))
1294 /* Ok, we're closing in.. */
1299 * "rev-list --ancestry-path=C_0 [--ancestry-path=C_1 ...] A..B"
1300 * computes commits that are ancestors of B but not ancestors of A but
1301 * further limits the result to those that have any of C in their
1302 * ancestry path (i.e. are either ancestors of any of C, descendants
1303 * of any of C, or are any of C). If --ancestry-path is specified with
1304 * no commit, we use all bottom commits for C.
1306 * Before this function is called, ancestors of C will have already
1307 * been marked with ANCESTRY_PATH previously.
1309 * This takes the list of bottom commits and the result of "A..B"
1310 * without --ancestry-path, and limits the latter further to the ones
1311 * that have any of C in their ancestry path. Since the ancestors of C
1312 * have already been marked (a prerequisite of this function), we just
1313 * need to mark the descendants, then exclude any commit that does not
1314 * have any of these marks.
1316 static void limit_to_ancestry(struct commit_list
*bottoms
, struct commit_list
*list
)
1318 struct commit_list
*p
;
1319 struct commit_list
*rlist
= NULL
;
1323 * Reverse the list so that it will be likely that we would
1324 * process parents before children.
1326 for (p
= list
; p
; p
= p
->next
)
1327 commit_list_insert(p
->item
, &rlist
);
1329 for (p
= bottoms
; p
; p
= p
->next
)
1330 p
->item
->object
.flags
|= TMP_MARK
;
1333 * Mark the ones that can reach bottom commits in "list",
1334 * in a bottom-up fashion.
1338 for (p
= rlist
; p
; p
= p
->next
) {
1339 struct commit
*c
= p
->item
;
1340 struct commit_list
*parents
;
1341 if (c
->object
.flags
& (TMP_MARK
| UNINTERESTING
))
1343 for (parents
= c
->parents
;
1345 parents
= parents
->next
) {
1346 if (!(parents
->item
->object
.flags
& TMP_MARK
))
1348 c
->object
.flags
|= TMP_MARK
;
1353 } while (made_progress
);
1356 * NEEDSWORK: decide if we want to remove parents that are
1357 * not marked with TMP_MARK from commit->parents for commits
1358 * in the resulting list. We may not want to do that, though.
1362 * The ones that are not marked with either TMP_MARK or
1363 * ANCESTRY_PATH are uninteresting
1365 for (p
= list
; p
; p
= p
->next
) {
1366 struct commit
*c
= p
->item
;
1367 if (c
->object
.flags
& (TMP_MARK
| ANCESTRY_PATH
))
1369 c
->object
.flags
|= UNINTERESTING
;
1372 /* We are done with TMP_MARK and ANCESTRY_PATH */
1373 for (p
= list
; p
; p
= p
->next
)
1374 p
->item
->object
.flags
&= ~(TMP_MARK
| ANCESTRY_PATH
);
1375 for (p
= bottoms
; p
; p
= p
->next
)
1376 p
->item
->object
.flags
&= ~(TMP_MARK
| ANCESTRY_PATH
);
1377 free_commit_list(rlist
);
1381 * Before walking the history, add the set of "negative" refs the
1382 * caller has asked to exclude to the bottom list.
1384 * This is used to compute "rev-list --ancestry-path A..B", as we need
1385 * to filter the result of "A..B" further to the ones that can actually
1388 static void collect_bottom_commits(struct commit_list
*list
,
1389 struct commit_list
**bottom
)
1391 struct commit_list
*elem
;
1392 for (elem
= list
; elem
; elem
= elem
->next
)
1393 if (elem
->item
->object
.flags
& BOTTOM
)
1394 commit_list_insert(elem
->item
, bottom
);
1397 /* Assumes either left_only or right_only is set */
1398 static void limit_left_right(struct commit_list
*list
, struct rev_info
*revs
)
1400 struct commit_list
*p
;
1402 for (p
= list
; p
; p
= p
->next
) {
1403 struct commit
*commit
= p
->item
;
1405 if (revs
->right_only
) {
1406 if (commit
->object
.flags
& SYMMETRIC_LEFT
)
1407 commit
->object
.flags
|= SHOWN
;
1408 } else /* revs->left_only is set */
1409 if (!(commit
->object
.flags
& SYMMETRIC_LEFT
))
1410 commit
->object
.flags
|= SHOWN
;
1414 static int limit_list(struct rev_info
*revs
)
1417 timestamp_t date
= TIME_MAX
;
1418 struct commit_list
*original_list
= revs
->commits
;
1419 struct commit_list
*newlist
= NULL
;
1420 struct commit_list
**p
= &newlist
;
1421 struct commit
*interesting_cache
= NULL
;
1423 if (revs
->ancestry_path_implicit_bottoms
) {
1424 collect_bottom_commits(original_list
,
1425 &revs
->ancestry_path_bottoms
);
1426 if (!revs
->ancestry_path_bottoms
)
1427 die("--ancestry-path given but there are no bottom commits");
1430 while (original_list
) {
1431 struct commit
*commit
= pop_commit(&original_list
);
1432 struct object
*obj
= &commit
->object
;
1433 show_early_output_fn_t show
;
1435 if (commit
== interesting_cache
)
1436 interesting_cache
= NULL
;
1438 if (revs
->max_age
!= -1 && (commit
->date
< revs
->max_age
))
1439 obj
->flags
|= UNINTERESTING
;
1440 if (process_parents(revs
, commit
, &original_list
, NULL
) < 0)
1442 if (obj
->flags
& UNINTERESTING
) {
1443 mark_parents_uninteresting(revs
, commit
);
1444 slop
= still_interesting(original_list
, date
, slop
, &interesting_cache
);
1449 if (revs
->min_age
!= -1 && (commit
->date
> revs
->min_age
) &&
1450 !revs
->line_level_traverse
)
1452 if (revs
->max_age_as_filter
!= -1 &&
1453 (commit
->date
< revs
->max_age_as_filter
) && !revs
->line_level_traverse
)
1455 date
= commit
->date
;
1456 p
= &commit_list_insert(commit
, p
)->next
;
1458 show
= show_early_output
;
1462 show(revs
, newlist
);
1463 show_early_output
= NULL
;
1465 if (revs
->cherry_pick
|| revs
->cherry_mark
)
1466 cherry_pick_list(newlist
, revs
);
1468 if (revs
->left_only
|| revs
->right_only
)
1469 limit_left_right(newlist
, revs
);
1471 if (revs
->ancestry_path
)
1472 limit_to_ancestry(revs
->ancestry_path_bottoms
, newlist
);
1475 * Check if any commits have become TREESAME by some of their parents
1476 * becoming UNINTERESTING.
1478 if (limiting_can_increase_treesame(revs
)) {
1479 struct commit_list
*list
= NULL
;
1480 for (list
= newlist
; list
; list
= list
->next
) {
1481 struct commit
*c
= list
->item
;
1482 if (c
->object
.flags
& (UNINTERESTING
| TREESAME
))
1484 update_treesame(revs
, c
);
1488 free_commit_list(original_list
);
1489 revs
->commits
= newlist
;
1494 * Add an entry to refs->cmdline with the specified information.
1497 static void add_rev_cmdline(struct rev_info
*revs
,
1498 struct object
*item
,
1503 struct rev_cmdline_info
*info
= &revs
->cmdline
;
1504 unsigned int nr
= info
->nr
;
1506 ALLOC_GROW(info
->rev
, nr
+ 1, info
->alloc
);
1507 info
->rev
[nr
].item
= item
;
1508 info
->rev
[nr
].name
= xstrdup(name
);
1509 info
->rev
[nr
].whence
= whence
;
1510 info
->rev
[nr
].flags
= flags
;
1514 static void add_rev_cmdline_list(struct rev_info
*revs
,
1515 struct commit_list
*commit_list
,
1519 while (commit_list
) {
1520 struct object
*object
= &commit_list
->item
->object
;
1521 add_rev_cmdline(revs
, object
, oid_to_hex(&object
->oid
),
1523 commit_list
= commit_list
->next
;
1527 struct all_refs_cb
{
1529 int warned_bad_reflog
;
1530 struct rev_info
*all_revs
;
1531 const char *name_for_errormsg
;
1532 struct worktree
*wt
;
1535 int ref_excluded(struct string_list
*ref_excludes
, const char *path
)
1537 struct string_list_item
*item
;
1541 for_each_string_list_item(item
, ref_excludes
) {
1542 if (!wildmatch(item
->string
, path
, 0))
1548 static int handle_one_ref(const char *path
, const struct object_id
*oid
,
1549 int flag
, void *cb_data
)
1551 struct all_refs_cb
*cb
= cb_data
;
1552 struct object
*object
;
1554 if (ref_excluded(cb
->all_revs
->ref_excludes
, path
))
1557 object
= get_reference(cb
->all_revs
, path
, oid
, cb
->all_flags
);
1558 add_rev_cmdline(cb
->all_revs
, object
, path
, REV_CMD_REF
, cb
->all_flags
);
1559 add_pending_object(cb
->all_revs
, object
, path
);
1563 static void init_all_refs_cb(struct all_refs_cb
*cb
, struct rev_info
*revs
,
1566 cb
->all_revs
= revs
;
1567 cb
->all_flags
= flags
;
1568 revs
->rev_input_given
= 1;
1572 void clear_ref_exclusion(struct string_list
**ref_excludes_p
)
1574 if (*ref_excludes_p
) {
1575 string_list_clear(*ref_excludes_p
, 0);
1576 free(*ref_excludes_p
);
1578 *ref_excludes_p
= NULL
;
1581 void add_ref_exclusion(struct string_list
**ref_excludes_p
, const char *exclude
)
1583 if (!*ref_excludes_p
) {
1584 CALLOC_ARRAY(*ref_excludes_p
, 1);
1585 (*ref_excludes_p
)->strdup_strings
= 1;
1587 string_list_append(*ref_excludes_p
, exclude
);
1590 static void handle_refs(struct ref_store
*refs
,
1591 struct rev_info
*revs
, unsigned flags
,
1592 int (*for_each
)(struct ref_store
*, each_ref_fn
, void *))
1594 struct all_refs_cb cb
;
1597 /* this could happen with uninitialized submodules */
1601 init_all_refs_cb(&cb
, revs
, flags
);
1602 for_each(refs
, handle_one_ref
, &cb
);
1605 static void handle_one_reflog_commit(struct object_id
*oid
, void *cb_data
)
1607 struct all_refs_cb
*cb
= cb_data
;
1608 if (!is_null_oid(oid
)) {
1609 struct object
*o
= parse_object(cb
->all_revs
->repo
, oid
);
1611 o
->flags
|= cb
->all_flags
;
1612 /* ??? CMDLINEFLAGS ??? */
1613 add_pending_object(cb
->all_revs
, o
, "");
1615 else if (!cb
->warned_bad_reflog
) {
1616 warning("reflog of '%s' references pruned commits",
1617 cb
->name_for_errormsg
);
1618 cb
->warned_bad_reflog
= 1;
1623 static int handle_one_reflog_ent(struct object_id
*ooid
, struct object_id
*noid
,
1624 const char *email
, timestamp_t timestamp
, int tz
,
1625 const char *message
, void *cb_data
)
1627 handle_one_reflog_commit(ooid
, cb_data
);
1628 handle_one_reflog_commit(noid
, cb_data
);
1632 static int handle_one_reflog(const char *refname_in_wt
,
1633 const struct object_id
*oid
,
1634 int flag
, void *cb_data
)
1636 struct all_refs_cb
*cb
= cb_data
;
1637 struct strbuf refname
= STRBUF_INIT
;
1639 cb
->warned_bad_reflog
= 0;
1640 strbuf_worktree_ref(cb
->wt
, &refname
, refname_in_wt
);
1641 cb
->name_for_errormsg
= refname
.buf
;
1642 refs_for_each_reflog_ent(get_main_ref_store(the_repository
),
1644 handle_one_reflog_ent
, cb_data
);
1645 strbuf_release(&refname
);
1649 static void add_other_reflogs_to_pending(struct all_refs_cb
*cb
)
1651 struct worktree
**worktrees
, **p
;
1653 worktrees
= get_worktrees();
1654 for (p
= worktrees
; *p
; p
++) {
1655 struct worktree
*wt
= *p
;
1661 refs_for_each_reflog(get_worktree_ref_store(wt
),
1665 free_worktrees(worktrees
);
1668 void add_reflogs_to_pending(struct rev_info
*revs
, unsigned flags
)
1670 struct all_refs_cb cb
;
1673 cb
.all_flags
= flags
;
1675 for_each_reflog(handle_one_reflog
, &cb
);
1677 if (!revs
->single_worktree
)
1678 add_other_reflogs_to_pending(&cb
);
1681 static void add_cache_tree(struct cache_tree
*it
, struct rev_info
*revs
,
1682 struct strbuf
*path
, unsigned int flags
)
1684 size_t baselen
= path
->len
;
1687 if (it
->entry_count
>= 0) {
1688 struct tree
*tree
= lookup_tree(revs
->repo
, &it
->oid
);
1689 tree
->object
.flags
|= flags
;
1690 add_pending_object_with_path(revs
, &tree
->object
, "",
1694 for (i
= 0; i
< it
->subtree_nr
; i
++) {
1695 struct cache_tree_sub
*sub
= it
->down
[i
];
1696 strbuf_addf(path
, "%s%s", baselen
? "/" : "", sub
->name
);
1697 add_cache_tree(sub
->cache_tree
, revs
, path
, flags
);
1698 strbuf_setlen(path
, baselen
);
1703 static void add_resolve_undo_to_pending(struct index_state
*istate
, struct rev_info
*revs
)
1705 struct string_list_item
*item
;
1706 struct string_list
*resolve_undo
= istate
->resolve_undo
;
1711 for_each_string_list_item(item
, resolve_undo
) {
1712 const char *path
= item
->string
;
1713 struct resolve_undo_info
*ru
= item
->util
;
1718 for (i
= 0; i
< 3; i
++) {
1721 if (!ru
->mode
[i
] || !S_ISREG(ru
->mode
[i
]))
1724 blob
= lookup_blob(revs
->repo
, &ru
->oid
[i
]);
1726 warning(_("resolve-undo records `%s` which is missing"),
1727 oid_to_hex(&ru
->oid
[i
]));
1730 add_pending_object_with_path(revs
, &blob
->object
, "",
1736 static void do_add_index_objects_to_pending(struct rev_info
*revs
,
1737 struct index_state
*istate
,
1742 /* TODO: audit for interaction with sparse-index. */
1743 ensure_full_index(istate
);
1744 for (i
= 0; i
< istate
->cache_nr
; i
++) {
1745 struct cache_entry
*ce
= istate
->cache
[i
];
1748 if (S_ISGITLINK(ce
->ce_mode
))
1751 blob
= lookup_blob(revs
->repo
, &ce
->oid
);
1753 die("unable to add index blob to traversal");
1754 blob
->object
.flags
|= flags
;
1755 add_pending_object_with_path(revs
, &blob
->object
, "",
1756 ce
->ce_mode
, ce
->name
);
1759 if (istate
->cache_tree
) {
1760 struct strbuf path
= STRBUF_INIT
;
1761 add_cache_tree(istate
->cache_tree
, revs
, &path
, flags
);
1762 strbuf_release(&path
);
1765 add_resolve_undo_to_pending(istate
, revs
);
1768 void add_index_objects_to_pending(struct rev_info
*revs
, unsigned int flags
)
1770 struct worktree
**worktrees
, **p
;
1772 repo_read_index(revs
->repo
);
1773 do_add_index_objects_to_pending(revs
, revs
->repo
->index
, flags
);
1775 if (revs
->single_worktree
)
1778 worktrees
= get_worktrees();
1779 for (p
= worktrees
; *p
; p
++) {
1780 struct worktree
*wt
= *p
;
1781 struct index_state istate
= { NULL
};
1784 continue; /* current index already taken care of */
1786 if (read_index_from(&istate
,
1787 worktree_git_path(wt
, "index"),
1788 get_worktree_git_dir(wt
)) > 0)
1789 do_add_index_objects_to_pending(revs
, &istate
, flags
);
1790 discard_index(&istate
);
1792 free_worktrees(worktrees
);
1795 struct add_alternate_refs_data
{
1796 struct rev_info
*revs
;
1800 static void add_one_alternate_ref(const struct object_id
*oid
,
1803 const char *name
= ".alternate";
1804 struct add_alternate_refs_data
*data
= vdata
;
1807 obj
= get_reference(data
->revs
, name
, oid
, data
->flags
);
1808 add_rev_cmdline(data
->revs
, obj
, name
, REV_CMD_REV
, data
->flags
);
1809 add_pending_object(data
->revs
, obj
, name
);
1812 static void add_alternate_refs_to_pending(struct rev_info
*revs
,
1815 struct add_alternate_refs_data data
;
1818 for_each_alternate_ref(add_one_alternate_ref
, &data
);
1821 static int add_parents_only(struct rev_info
*revs
, const char *arg_
, int flags
,
1824 struct object_id oid
;
1826 struct commit
*commit
;
1827 struct commit_list
*parents
;
1829 const char *arg
= arg_
;
1832 flags
^= UNINTERESTING
| BOTTOM
;
1835 if (get_oid_committish(arg
, &oid
))
1838 it
= get_reference(revs
, arg
, &oid
, 0);
1839 if (!it
&& revs
->ignore_missing
)
1841 if (it
->type
!= OBJ_TAG
)
1843 if (!((struct tag
*)it
)->tagged
)
1845 oidcpy(&oid
, &((struct tag
*)it
)->tagged
->oid
);
1847 if (it
->type
!= OBJ_COMMIT
)
1849 commit
= (struct commit
*)it
;
1850 if (exclude_parent
&&
1851 exclude_parent
> commit_list_count(commit
->parents
))
1853 for (parents
= commit
->parents
, parent_number
= 1;
1855 parents
= parents
->next
, parent_number
++) {
1856 if (exclude_parent
&& parent_number
!= exclude_parent
)
1859 it
= &parents
->item
->object
;
1861 add_rev_cmdline(revs
, it
, arg_
, REV_CMD_PARENTS_ONLY
, flags
);
1862 add_pending_object(revs
, it
, arg
);
1867 void repo_init_revisions(struct repository
*r
,
1868 struct rev_info
*revs
,
1871 memset(revs
, 0, sizeof(*revs
));
1874 revs
->abbrev
= DEFAULT_ABBREV
;
1875 revs
->simplify_history
= 1;
1876 revs
->pruning
.repo
= r
;
1877 revs
->pruning
.flags
.recursive
= 1;
1878 revs
->pruning
.flags
.quick
= 1;
1879 revs
->pruning
.add_remove
= file_add_remove
;
1880 revs
->pruning
.change
= file_change
;
1881 revs
->pruning
.change_fn_data
= revs
;
1882 revs
->sort_order
= REV_SORT_IN_GRAPH_ORDER
;
1884 revs
->prefix
= prefix
;
1886 revs
->max_age_as_filter
= -1;
1888 revs
->skip_count
= -1;
1889 revs
->max_count
= -1;
1890 revs
->max_parents
= -1;
1891 revs
->expand_tabs_in_log
= -1;
1893 revs
->commit_format
= CMIT_FMT_DEFAULT
;
1894 revs
->expand_tabs_in_log_default
= 8;
1896 grep_init(&revs
->grep_filter
, revs
->repo
);
1897 revs
->grep_filter
.status_only
= 1;
1899 repo_diff_setup(revs
->repo
, &revs
->diffopt
);
1900 if (prefix
&& !revs
->diffopt
.prefix
) {
1901 revs
->diffopt
.prefix
= prefix
;
1902 revs
->diffopt
.prefix_length
= strlen(prefix
);
1905 init_display_notes(&revs
->notes_opt
);
1908 static void add_pending_commit_list(struct rev_info
*revs
,
1909 struct commit_list
*commit_list
,
1912 while (commit_list
) {
1913 struct object
*object
= &commit_list
->item
->object
;
1914 object
->flags
|= flags
;
1915 add_pending_object(revs
, object
, oid_to_hex(&object
->oid
));
1916 commit_list
= commit_list
->next
;
1920 static void prepare_show_merge(struct rev_info
*revs
)
1922 struct commit_list
*bases
;
1923 struct commit
*head
, *other
;
1924 struct object_id oid
;
1925 const char **prune
= NULL
;
1926 int i
, prune_num
= 1; /* counting terminating NULL */
1927 struct index_state
*istate
= revs
->repo
->index
;
1929 if (get_oid("HEAD", &oid
))
1930 die("--merge without HEAD?");
1931 head
= lookup_commit_or_die(&oid
, "HEAD");
1932 if (get_oid("MERGE_HEAD", &oid
))
1933 die("--merge without MERGE_HEAD?");
1934 other
= lookup_commit_or_die(&oid
, "MERGE_HEAD");
1935 add_pending_object(revs
, &head
->object
, "HEAD");
1936 add_pending_object(revs
, &other
->object
, "MERGE_HEAD");
1937 bases
= get_merge_bases(head
, other
);
1938 add_rev_cmdline_list(revs
, bases
, REV_CMD_MERGE_BASE
, UNINTERESTING
| BOTTOM
);
1939 add_pending_commit_list(revs
, bases
, UNINTERESTING
| BOTTOM
);
1940 free_commit_list(bases
);
1941 head
->object
.flags
|= SYMMETRIC_LEFT
;
1943 if (!istate
->cache_nr
)
1944 repo_read_index(revs
->repo
);
1945 for (i
= 0; i
< istate
->cache_nr
; i
++) {
1946 const struct cache_entry
*ce
= istate
->cache
[i
];
1949 if (ce_path_match(istate
, ce
, &revs
->prune_data
, NULL
)) {
1951 REALLOC_ARRAY(prune
, prune_num
);
1952 prune
[prune_num
-2] = ce
->name
;
1953 prune
[prune_num
-1] = NULL
;
1955 while ((i
+1 < istate
->cache_nr
) &&
1956 ce_same_name(ce
, istate
->cache
[i
+1]))
1959 clear_pathspec(&revs
->prune_data
);
1960 parse_pathspec(&revs
->prune_data
, PATHSPEC_ALL_MAGIC
& ~PATHSPEC_LITERAL
,
1961 PATHSPEC_PREFER_FULL
| PATHSPEC_LITERAL_PATH
, "", prune
);
1965 static int dotdot_missing(const char *arg
, char *dotdot
,
1966 struct rev_info
*revs
, int symmetric
)
1968 if (revs
->ignore_missing
)
1970 /* de-munge so we report the full argument */
1973 ? "Invalid symmetric difference expression %s"
1974 : "Invalid revision range %s", arg
);
1977 static int handle_dotdot_1(const char *arg
, char *dotdot
,
1978 struct rev_info
*revs
, int flags
,
1979 int cant_be_filename
,
1980 struct object_context
*a_oc
,
1981 struct object_context
*b_oc
)
1983 const char *a_name
, *b_name
;
1984 struct object_id a_oid
, b_oid
;
1985 struct object
*a_obj
, *b_obj
;
1986 unsigned int a_flags
, b_flags
;
1988 unsigned int flags_exclude
= flags
^ (UNINTERESTING
| BOTTOM
);
1989 unsigned int oc_flags
= GET_OID_COMMITTISH
| GET_OID_RECORD_PATH
;
1995 b_name
= dotdot
+ 2;
1996 if (*b_name
== '.') {
2003 if (get_oid_with_context(revs
->repo
, a_name
, oc_flags
, &a_oid
, a_oc
) ||
2004 get_oid_with_context(revs
->repo
, b_name
, oc_flags
, &b_oid
, b_oc
))
2007 if (!cant_be_filename
) {
2009 verify_non_filename(revs
->prefix
, arg
);
2013 a_obj
= parse_object(revs
->repo
, &a_oid
);
2014 b_obj
= parse_object(revs
->repo
, &b_oid
);
2015 if (!a_obj
|| !b_obj
)
2016 return dotdot_missing(arg
, dotdot
, revs
, symmetric
);
2021 a_flags
= flags_exclude
;
2023 /* A...B -- find merge bases between the two */
2024 struct commit
*a
, *b
;
2025 struct commit_list
*exclude
;
2027 a
= lookup_commit_reference(revs
->repo
, &a_obj
->oid
);
2028 b
= lookup_commit_reference(revs
->repo
, &b_obj
->oid
);
2030 return dotdot_missing(arg
, dotdot
, revs
, symmetric
);
2032 exclude
= get_merge_bases(a
, b
);
2033 add_rev_cmdline_list(revs
, exclude
, REV_CMD_MERGE_BASE
,
2035 add_pending_commit_list(revs
, exclude
, flags_exclude
);
2036 free_commit_list(exclude
);
2039 a_flags
= flags
| SYMMETRIC_LEFT
;
2042 a_obj
->flags
|= a_flags
;
2043 b_obj
->flags
|= b_flags
;
2044 add_rev_cmdline(revs
, a_obj
, a_name
, REV_CMD_LEFT
, a_flags
);
2045 add_rev_cmdline(revs
, b_obj
, b_name
, REV_CMD_RIGHT
, b_flags
);
2046 add_pending_object_with_path(revs
, a_obj
, a_name
, a_oc
->mode
, a_oc
->path
);
2047 add_pending_object_with_path(revs
, b_obj
, b_name
, b_oc
->mode
, b_oc
->path
);
2051 static int handle_dotdot(const char *arg
,
2052 struct rev_info
*revs
, int flags
,
2053 int cant_be_filename
)
2055 struct object_context a_oc
, b_oc
;
2056 char *dotdot
= strstr(arg
, "..");
2062 memset(&a_oc
, 0, sizeof(a_oc
));
2063 memset(&b_oc
, 0, sizeof(b_oc
));
2066 ret
= handle_dotdot_1(arg
, dotdot
, revs
, flags
, cant_be_filename
,
2076 static int handle_revision_arg_1(const char *arg_
, struct rev_info
*revs
, int flags
, unsigned revarg_opt
)
2078 struct object_context oc
;
2080 struct object
*object
;
2081 struct object_id oid
;
2083 const char *arg
= arg_
;
2084 int cant_be_filename
= revarg_opt
& REVARG_CANNOT_BE_FILENAME
;
2085 unsigned get_sha1_flags
= GET_OID_RECORD_PATH
;
2087 flags
= flags
& UNINTERESTING
? flags
| BOTTOM
: flags
& ~BOTTOM
;
2089 if (!cant_be_filename
&& !strcmp(arg
, "..")) {
2091 * Just ".."? That is not a range but the
2092 * pathspec for the parent directory.
2097 if (!handle_dotdot(arg
, revs
, flags
, revarg_opt
))
2100 mark
= strstr(arg
, "^@");
2101 if (mark
&& !mark
[2]) {
2103 if (add_parents_only(revs
, arg
, flags
, 0))
2107 mark
= strstr(arg
, "^!");
2108 if (mark
&& !mark
[2]) {
2110 if (!add_parents_only(revs
, arg
, flags
^ (UNINTERESTING
| BOTTOM
), 0))
2113 mark
= strstr(arg
, "^-");
2115 int exclude_parent
= 1;
2119 exclude_parent
= strtoul(mark
+ 2, &end
, 10);
2120 if (*end
!= '\0' || !exclude_parent
)
2125 if (!add_parents_only(revs
, arg
, flags
^ (UNINTERESTING
| BOTTOM
), exclude_parent
))
2131 local_flags
= UNINTERESTING
| BOTTOM
;
2135 if (revarg_opt
& REVARG_COMMITTISH
)
2136 get_sha1_flags
|= GET_OID_COMMITTISH
;
2138 if (get_oid_with_context(revs
->repo
, arg
, get_sha1_flags
, &oid
, &oc
))
2139 return revs
->ignore_missing
? 0 : -1;
2140 if (!cant_be_filename
)
2141 verify_non_filename(revs
->prefix
, arg
);
2142 object
= get_reference(revs
, arg
, &oid
, flags
^ local_flags
);
2144 return revs
->ignore_missing
? 0 : -1;
2145 add_rev_cmdline(revs
, object
, arg_
, REV_CMD_REV
, flags
^ local_flags
);
2146 add_pending_object_with_path(revs
, object
, arg
, oc
.mode
, oc
.path
);
2151 int handle_revision_arg(const char *arg
, struct rev_info
*revs
, int flags
, unsigned revarg_opt
)
2153 int ret
= handle_revision_arg_1(arg
, revs
, flags
, revarg_opt
);
2155 revs
->rev_input_given
= 1;
2159 static void read_pathspec_from_stdin(struct strbuf
*sb
,
2160 struct strvec
*prune
)
2162 while (strbuf_getline(sb
, stdin
) != EOF
)
2163 strvec_push(prune
, sb
->buf
);
2166 static void read_revisions_from_stdin(struct rev_info
*revs
,
2167 struct strvec
*prune
)
2170 int seen_dashdash
= 0;
2173 save_warning
= warn_on_object_refname_ambiguity
;
2174 warn_on_object_refname_ambiguity
= 0;
2176 strbuf_init(&sb
, 1000);
2177 while (strbuf_getline(&sb
, stdin
) != EOF
) {
2181 if (sb
.buf
[0] == '-') {
2182 if (len
== 2 && sb
.buf
[1] == '-') {
2186 die("options not supported in --stdin mode");
2188 if (handle_revision_arg(sb
.buf
, revs
, 0,
2189 REVARG_CANNOT_BE_FILENAME
))
2190 die("bad revision '%s'", sb
.buf
);
2193 read_pathspec_from_stdin(&sb
, prune
);
2195 strbuf_release(&sb
);
2196 warn_on_object_refname_ambiguity
= save_warning
;
2199 static void add_grep(struct rev_info
*revs
, const char *ptn
, enum grep_pat_token what
)
2201 append_grep_pattern(&revs
->grep_filter
, ptn
, "command line", 0, what
);
2204 static void add_header_grep(struct rev_info
*revs
, enum grep_header_field field
, const char *pattern
)
2206 append_header_grep_pattern(&revs
->grep_filter
, field
, pattern
);
2209 static void add_message_grep(struct rev_info
*revs
, const char *pattern
)
2211 add_grep(revs
, pattern
, GREP_PATTERN_BODY
);
2214 static int handle_revision_opt(struct rev_info
*revs
, int argc
, const char **argv
,
2215 int *unkc
, const char **unkv
,
2216 const struct setup_revision_opt
* opt
)
2218 const char *arg
= argv
[0];
2219 const char *optarg
= NULL
;
2221 const unsigned hexsz
= the_hash_algo
->hexsz
;
2223 /* pseudo revision arguments */
2224 if (!strcmp(arg
, "--all") || !strcmp(arg
, "--branches") ||
2225 !strcmp(arg
, "--tags") || !strcmp(arg
, "--remotes") ||
2226 !strcmp(arg
, "--reflog") || !strcmp(arg
, "--not") ||
2227 !strcmp(arg
, "--no-walk") || !strcmp(arg
, "--do-walk") ||
2228 !strcmp(arg
, "--bisect") || starts_with(arg
, "--glob=") ||
2229 !strcmp(arg
, "--indexed-objects") ||
2230 !strcmp(arg
, "--alternate-refs") ||
2231 starts_with(arg
, "--exclude=") ||
2232 starts_with(arg
, "--branches=") || starts_with(arg
, "--tags=") ||
2233 starts_with(arg
, "--remotes=") || starts_with(arg
, "--no-walk="))
2235 unkv
[(*unkc
)++] = arg
;
2239 if ((argcount
= parse_long_opt("max-count", argv
, &optarg
))) {
2240 revs
->max_count
= atoi(optarg
);
2243 } else if ((argcount
= parse_long_opt("skip", argv
, &optarg
))) {
2244 revs
->skip_count
= atoi(optarg
);
2246 } else if ((*arg
== '-') && isdigit(arg
[1])) {
2247 /* accept -<digit>, like traditional "head" */
2248 if (strtol_i(arg
+ 1, 10, &revs
->max_count
) < 0 ||
2249 revs
->max_count
< 0)
2250 die("'%s': not a non-negative integer", arg
+ 1);
2252 } else if (!strcmp(arg
, "-n")) {
2254 return error("-n requires an argument");
2255 revs
->max_count
= atoi(argv
[1]);
2258 } else if (skip_prefix(arg
, "-n", &optarg
)) {
2259 revs
->max_count
= atoi(optarg
);
2261 } else if ((argcount
= parse_long_opt("max-age", argv
, &optarg
))) {
2262 revs
->max_age
= atoi(optarg
);
2264 } else if ((argcount
= parse_long_opt("since", argv
, &optarg
))) {
2265 revs
->max_age
= approxidate(optarg
);
2267 } else if ((argcount
= parse_long_opt("since-as-filter", argv
, &optarg
))) {
2268 revs
->max_age_as_filter
= approxidate(optarg
);
2270 } else if ((argcount
= parse_long_opt("after", argv
, &optarg
))) {
2271 revs
->max_age
= approxidate(optarg
);
2273 } else if ((argcount
= parse_long_opt("min-age", argv
, &optarg
))) {
2274 revs
->min_age
= atoi(optarg
);
2276 } else if ((argcount
= parse_long_opt("before", argv
, &optarg
))) {
2277 revs
->min_age
= approxidate(optarg
);
2279 } else if ((argcount
= parse_long_opt("until", argv
, &optarg
))) {
2280 revs
->min_age
= approxidate(optarg
);
2282 } else if (!strcmp(arg
, "--first-parent")) {
2283 revs
->first_parent_only
= 1;
2284 } else if (!strcmp(arg
, "--exclude-first-parent-only")) {
2285 revs
->exclude_first_parent_only
= 1;
2286 } else if (!strcmp(arg
, "--ancestry-path")) {
2287 revs
->ancestry_path
= 1;
2288 revs
->simplify_history
= 0;
2290 revs
->ancestry_path_implicit_bottoms
= 1;
2291 } else if (skip_prefix(arg
, "--ancestry-path=", &optarg
)) {
2293 struct object_id oid
;
2294 const char *msg
= _("could not get commit for ancestry-path argument %s");
2296 revs
->ancestry_path
= 1;
2297 revs
->simplify_history
= 0;
2300 if (repo_get_oid_committish(revs
->repo
, optarg
, &oid
))
2301 return error(msg
, optarg
);
2302 get_reference(revs
, optarg
, &oid
, ANCESTRY_PATH
);
2303 c
= lookup_commit_reference(revs
->repo
, &oid
);
2305 return error(msg
, optarg
);
2306 commit_list_insert(c
, &revs
->ancestry_path_bottoms
);
2307 } else if (!strcmp(arg
, "-g") || !strcmp(arg
, "--walk-reflogs")) {
2308 init_reflog_walk(&revs
->reflog_info
);
2309 } else if (!strcmp(arg
, "--default")) {
2311 return error("bad --default argument");
2312 revs
->def
= argv
[1];
2314 } else if (!strcmp(arg
, "--merge")) {
2315 revs
->show_merge
= 1;
2316 } else if (!strcmp(arg
, "--topo-order")) {
2317 revs
->sort_order
= REV_SORT_IN_GRAPH_ORDER
;
2318 revs
->topo_order
= 1;
2319 } else if (!strcmp(arg
, "--simplify-merges")) {
2320 revs
->simplify_merges
= 1;
2321 revs
->topo_order
= 1;
2322 revs
->rewrite_parents
= 1;
2323 revs
->simplify_history
= 0;
2325 } else if (!strcmp(arg
, "--simplify-by-decoration")) {
2326 revs
->simplify_merges
= 1;
2327 revs
->topo_order
= 1;
2328 revs
->rewrite_parents
= 1;
2329 revs
->simplify_history
= 0;
2330 revs
->simplify_by_decoration
= 1;
2333 } else if (!strcmp(arg
, "--date-order")) {
2334 revs
->sort_order
= REV_SORT_BY_COMMIT_DATE
;
2335 revs
->topo_order
= 1;
2336 } else if (!strcmp(arg
, "--author-date-order")) {
2337 revs
->sort_order
= REV_SORT_BY_AUTHOR_DATE
;
2338 revs
->topo_order
= 1;
2339 } else if (!strcmp(arg
, "--early-output")) {
2340 revs
->early_output
= 100;
2341 revs
->topo_order
= 1;
2342 } else if (skip_prefix(arg
, "--early-output=", &optarg
)) {
2343 if (strtoul_ui(optarg
, 10, &revs
->early_output
) < 0)
2344 die("'%s': not a non-negative integer", optarg
);
2345 revs
->topo_order
= 1;
2346 } else if (!strcmp(arg
, "--parents")) {
2347 revs
->rewrite_parents
= 1;
2348 revs
->print_parents
= 1;
2349 } else if (!strcmp(arg
, "--dense")) {
2351 } else if (!strcmp(arg
, "--sparse")) {
2353 } else if (!strcmp(arg
, "--in-commit-order")) {
2354 revs
->tree_blobs_in_commit_order
= 1;
2355 } else if (!strcmp(arg
, "--remove-empty")) {
2356 revs
->remove_empty_trees
= 1;
2357 } else if (!strcmp(arg
, "--merges")) {
2358 revs
->min_parents
= 2;
2359 } else if (!strcmp(arg
, "--no-merges")) {
2360 revs
->max_parents
= 1;
2361 } else if (skip_prefix(arg
, "--min-parents=", &optarg
)) {
2362 revs
->min_parents
= atoi(optarg
);
2363 } else if (!strcmp(arg
, "--no-min-parents")) {
2364 revs
->min_parents
= 0;
2365 } else if (skip_prefix(arg
, "--max-parents=", &optarg
)) {
2366 revs
->max_parents
= atoi(optarg
);
2367 } else if (!strcmp(arg
, "--no-max-parents")) {
2368 revs
->max_parents
= -1;
2369 } else if (!strcmp(arg
, "--boundary")) {
2371 } else if (!strcmp(arg
, "--left-right")) {
2372 revs
->left_right
= 1;
2373 } else if (!strcmp(arg
, "--left-only")) {
2374 if (revs
->right_only
)
2375 die("--left-only is incompatible with --right-only"
2377 revs
->left_only
= 1;
2378 } else if (!strcmp(arg
, "--right-only")) {
2379 if (revs
->left_only
)
2380 die(_("options '%s' and '%s' cannot be used together"), "--right-only", "--left-only");
2381 revs
->right_only
= 1;
2382 } else if (!strcmp(arg
, "--cherry")) {
2383 if (revs
->left_only
)
2384 die(_("options '%s' and '%s' cannot be used together"), "--cherry", "--left-only");
2385 revs
->cherry_mark
= 1;
2386 revs
->right_only
= 1;
2387 revs
->max_parents
= 1;
2389 } else if (!strcmp(arg
, "--count")) {
2391 } else if (!strcmp(arg
, "--cherry-mark")) {
2392 if (revs
->cherry_pick
)
2393 die(_("options '%s' and '%s' cannot be used together"), "--cherry-mark", "--cherry-pick");
2394 revs
->cherry_mark
= 1;
2395 revs
->limited
= 1; /* needs limit_list() */
2396 } else if (!strcmp(arg
, "--cherry-pick")) {
2397 if (revs
->cherry_mark
)
2398 die(_("options '%s' and '%s' cannot be used together"), "--cherry-pick", "--cherry-mark");
2399 revs
->cherry_pick
= 1;
2401 } else if (!strcmp(arg
, "--objects")) {
2402 revs
->tag_objects
= 1;
2403 revs
->tree_objects
= 1;
2404 revs
->blob_objects
= 1;
2405 } else if (!strcmp(arg
, "--objects-edge")) {
2406 revs
->tag_objects
= 1;
2407 revs
->tree_objects
= 1;
2408 revs
->blob_objects
= 1;
2409 revs
->edge_hint
= 1;
2410 } else if (!strcmp(arg
, "--objects-edge-aggressive")) {
2411 revs
->tag_objects
= 1;
2412 revs
->tree_objects
= 1;
2413 revs
->blob_objects
= 1;
2414 revs
->edge_hint
= 1;
2415 revs
->edge_hint_aggressive
= 1;
2416 } else if (!strcmp(arg
, "--verify-objects")) {
2417 revs
->tag_objects
= 1;
2418 revs
->tree_objects
= 1;
2419 revs
->blob_objects
= 1;
2420 revs
->verify_objects
= 1;
2421 disable_commit_graph(revs
->repo
);
2422 } else if (!strcmp(arg
, "--unpacked")) {
2424 } else if (starts_with(arg
, "--unpacked=")) {
2425 die(_("--unpacked=<packfile> no longer supported"));
2426 } else if (!strcmp(arg
, "--no-kept-objects")) {
2427 revs
->no_kept_objects
= 1;
2428 revs
->keep_pack_cache_flags
|= IN_CORE_KEEP_PACKS
;
2429 revs
->keep_pack_cache_flags
|= ON_DISK_KEEP_PACKS
;
2430 } else if (skip_prefix(arg
, "--no-kept-objects=", &optarg
)) {
2431 revs
->no_kept_objects
= 1;
2432 if (!strcmp(optarg
, "in-core"))
2433 revs
->keep_pack_cache_flags
|= IN_CORE_KEEP_PACKS
;
2434 if (!strcmp(optarg
, "on-disk"))
2435 revs
->keep_pack_cache_flags
|= ON_DISK_KEEP_PACKS
;
2436 } else if (!strcmp(arg
, "-r")) {
2438 revs
->diffopt
.flags
.recursive
= 1;
2439 } else if (!strcmp(arg
, "-t")) {
2441 revs
->diffopt
.flags
.recursive
= 1;
2442 revs
->diffopt
.flags
.tree_in_recursive
= 1;
2443 } else if ((argcount
= diff_merges_parse_opts(revs
, argv
))) {
2445 } else if (!strcmp(arg
, "-v")) {
2446 revs
->verbose_header
= 1;
2447 } else if (!strcmp(arg
, "--pretty")) {
2448 revs
->verbose_header
= 1;
2449 revs
->pretty_given
= 1;
2450 get_commit_format(NULL
, revs
);
2451 } else if (skip_prefix(arg
, "--pretty=", &optarg
) ||
2452 skip_prefix(arg
, "--format=", &optarg
)) {
2454 * Detached form ("--pretty X" as opposed to "--pretty=X")
2455 * not allowed, since the argument is optional.
2457 revs
->verbose_header
= 1;
2458 revs
->pretty_given
= 1;
2459 get_commit_format(optarg
, revs
);
2460 } else if (!strcmp(arg
, "--expand-tabs")) {
2461 revs
->expand_tabs_in_log
= 8;
2462 } else if (!strcmp(arg
, "--no-expand-tabs")) {
2463 revs
->expand_tabs_in_log
= 0;
2464 } else if (skip_prefix(arg
, "--expand-tabs=", &arg
)) {
2466 if (strtol_i(arg
, 10, &val
) < 0 || val
< 0)
2467 die("'%s': not a non-negative integer", arg
);
2468 revs
->expand_tabs_in_log
= val
;
2469 } else if (!strcmp(arg
, "--show-notes") || !strcmp(arg
, "--notes")) {
2470 enable_default_display_notes(&revs
->notes_opt
, &revs
->show_notes
);
2471 revs
->show_notes_given
= 1;
2472 } else if (!strcmp(arg
, "--show-signature")) {
2473 revs
->show_signature
= 1;
2474 } else if (!strcmp(arg
, "--no-show-signature")) {
2475 revs
->show_signature
= 0;
2476 } else if (!strcmp(arg
, "--show-linear-break")) {
2477 revs
->break_bar
= " ..........";
2478 revs
->track_linear
= 1;
2479 revs
->track_first_time
= 1;
2480 } else if (skip_prefix(arg
, "--show-linear-break=", &optarg
)) {
2481 revs
->break_bar
= xstrdup(optarg
);
2482 revs
->track_linear
= 1;
2483 revs
->track_first_time
= 1;
2484 } else if (skip_prefix(arg
, "--show-notes=", &optarg
) ||
2485 skip_prefix(arg
, "--notes=", &optarg
)) {
2486 if (starts_with(arg
, "--show-notes=") &&
2487 revs
->notes_opt
.use_default_notes
< 0)
2488 revs
->notes_opt
.use_default_notes
= 1;
2489 enable_ref_display_notes(&revs
->notes_opt
, &revs
->show_notes
, optarg
);
2490 revs
->show_notes_given
= 1;
2491 } else if (!strcmp(arg
, "--no-notes")) {
2492 disable_display_notes(&revs
->notes_opt
, &revs
->show_notes
);
2493 revs
->show_notes_given
= 1;
2494 } else if (!strcmp(arg
, "--standard-notes")) {
2495 revs
->show_notes_given
= 1;
2496 revs
->notes_opt
.use_default_notes
= 1;
2497 } else if (!strcmp(arg
, "--no-standard-notes")) {
2498 revs
->notes_opt
.use_default_notes
= 0;
2499 } else if (!strcmp(arg
, "--oneline")) {
2500 revs
->verbose_header
= 1;
2501 get_commit_format("oneline", revs
);
2502 revs
->pretty_given
= 1;
2503 revs
->abbrev_commit
= 1;
2504 } else if (!strcmp(arg
, "--graph")) {
2505 graph_clear(revs
->graph
);
2506 revs
->graph
= graph_init(revs
);
2507 } else if (!strcmp(arg
, "--no-graph")) {
2508 graph_clear(revs
->graph
);
2510 } else if (!strcmp(arg
, "--encode-email-headers")) {
2511 revs
->encode_email_headers
= 1;
2512 } else if (!strcmp(arg
, "--no-encode-email-headers")) {
2513 revs
->encode_email_headers
= 0;
2514 } else if (!strcmp(arg
, "--root")) {
2515 revs
->show_root_diff
= 1;
2516 } else if (!strcmp(arg
, "--no-commit-id")) {
2517 revs
->no_commit_id
= 1;
2518 } else if (!strcmp(arg
, "--always")) {
2519 revs
->always_show_header
= 1;
2520 } else if (!strcmp(arg
, "--no-abbrev")) {
2522 } else if (!strcmp(arg
, "--abbrev")) {
2523 revs
->abbrev
= DEFAULT_ABBREV
;
2524 } else if (skip_prefix(arg
, "--abbrev=", &optarg
)) {
2525 revs
->abbrev
= strtoul(optarg
, NULL
, 10);
2526 if (revs
->abbrev
< MINIMUM_ABBREV
)
2527 revs
->abbrev
= MINIMUM_ABBREV
;
2528 else if (revs
->abbrev
> hexsz
)
2529 revs
->abbrev
= hexsz
;
2530 } else if (!strcmp(arg
, "--abbrev-commit")) {
2531 revs
->abbrev_commit
= 1;
2532 revs
->abbrev_commit_given
= 1;
2533 } else if (!strcmp(arg
, "--no-abbrev-commit")) {
2534 revs
->abbrev_commit
= 0;
2535 } else if (!strcmp(arg
, "--full-diff")) {
2537 revs
->full_diff
= 1;
2538 } else if (!strcmp(arg
, "--show-pulls")) {
2539 revs
->show_pulls
= 1;
2540 } else if (!strcmp(arg
, "--full-history")) {
2541 revs
->simplify_history
= 0;
2542 } else if (!strcmp(arg
, "--relative-date")) {
2543 revs
->date_mode
.type
= DATE_RELATIVE
;
2544 revs
->date_mode_explicit
= 1;
2545 } else if ((argcount
= parse_long_opt("date", argv
, &optarg
))) {
2546 parse_date_format(optarg
, &revs
->date_mode
);
2547 revs
->date_mode_explicit
= 1;
2549 } else if (!strcmp(arg
, "--log-size")) {
2550 revs
->show_log_size
= 1;
2553 * Grepping the commit log
2555 else if ((argcount
= parse_long_opt("author", argv
, &optarg
))) {
2556 add_header_grep(revs
, GREP_HEADER_AUTHOR
, optarg
);
2558 } else if ((argcount
= parse_long_opt("committer", argv
, &optarg
))) {
2559 add_header_grep(revs
, GREP_HEADER_COMMITTER
, optarg
);
2561 } else if ((argcount
= parse_long_opt("grep-reflog", argv
, &optarg
))) {
2562 add_header_grep(revs
, GREP_HEADER_REFLOG
, optarg
);
2564 } else if ((argcount
= parse_long_opt("grep", argv
, &optarg
))) {
2565 add_message_grep(revs
, optarg
);
2567 } else if (!strcmp(arg
, "--basic-regexp")) {
2568 revs
->grep_filter
.pattern_type_option
= GREP_PATTERN_TYPE_BRE
;
2569 } else if (!strcmp(arg
, "--extended-regexp") || !strcmp(arg
, "-E")) {
2570 revs
->grep_filter
.pattern_type_option
= GREP_PATTERN_TYPE_ERE
;
2571 } else if (!strcmp(arg
, "--regexp-ignore-case") || !strcmp(arg
, "-i")) {
2572 revs
->grep_filter
.ignore_case
= 1;
2573 revs
->diffopt
.pickaxe_opts
|= DIFF_PICKAXE_IGNORE_CASE
;
2574 } else if (!strcmp(arg
, "--fixed-strings") || !strcmp(arg
, "-F")) {
2575 revs
->grep_filter
.pattern_type_option
= GREP_PATTERN_TYPE_FIXED
;
2576 } else if (!strcmp(arg
, "--perl-regexp") || !strcmp(arg
, "-P")) {
2577 revs
->grep_filter
.pattern_type_option
= GREP_PATTERN_TYPE_PCRE
;
2578 } else if (!strcmp(arg
, "--all-match")) {
2579 revs
->grep_filter
.all_match
= 1;
2580 } else if (!strcmp(arg
, "--invert-grep")) {
2581 revs
->grep_filter
.no_body_match
= 1;
2582 } else if ((argcount
= parse_long_opt("encoding", argv
, &optarg
))) {
2583 if (strcmp(optarg
, "none"))
2584 git_log_output_encoding
= xstrdup(optarg
);
2586 git_log_output_encoding
= "";
2588 } else if (!strcmp(arg
, "--reverse")) {
2590 } else if (!strcmp(arg
, "--children")) {
2591 revs
->children
.name
= "children";
2593 } else if (!strcmp(arg
, "--ignore-missing")) {
2594 revs
->ignore_missing
= 1;
2595 } else if (opt
&& opt
->allow_exclude_promisor_objects
&&
2596 !strcmp(arg
, "--exclude-promisor-objects")) {
2597 if (fetch_if_missing
)
2598 BUG("exclude_promisor_objects can only be used when fetch_if_missing is 0");
2599 revs
->exclude_promisor_objects
= 1;
2601 int opts
= diff_opt_parse(&revs
->diffopt
, argv
, argc
, revs
->prefix
);
2603 unkv
[(*unkc
)++] = arg
;
2610 void parse_revision_opt(struct rev_info
*revs
, struct parse_opt_ctx_t
*ctx
,
2611 const struct option
*options
,
2612 const char * const usagestr
[])
2614 int n
= handle_revision_opt(revs
, ctx
->argc
, ctx
->argv
,
2615 &ctx
->cpidx
, ctx
->out
, NULL
);
2617 error("unknown option `%s'", ctx
->argv
[0]);
2618 usage_with_options(usagestr
, options
);
2624 void revision_opts_finish(struct rev_info
*revs
)
2626 if (revs
->graph
&& revs
->track_linear
)
2627 die(_("options '%s' and '%s' cannot be used together"), "--show-linear-break", "--graph");
2630 revs
->topo_order
= 1;
2631 revs
->rewrite_parents
= 1;
2635 static int for_each_bisect_ref(struct ref_store
*refs
, each_ref_fn fn
,
2636 void *cb_data
, const char *term
)
2638 struct strbuf bisect_refs
= STRBUF_INIT
;
2640 strbuf_addf(&bisect_refs
, "refs/bisect/%s", term
);
2641 status
= refs_for_each_fullref_in(refs
, bisect_refs
.buf
, fn
, cb_data
);
2642 strbuf_release(&bisect_refs
);
2646 static int for_each_bad_bisect_ref(struct ref_store
*refs
, each_ref_fn fn
, void *cb_data
)
2648 return for_each_bisect_ref(refs
, fn
, cb_data
, term_bad
);
2651 static int for_each_good_bisect_ref(struct ref_store
*refs
, each_ref_fn fn
, void *cb_data
)
2653 return for_each_bisect_ref(refs
, fn
, cb_data
, term_good
);
2656 static int handle_revision_pseudo_opt(struct rev_info
*revs
,
2657 const char **argv
, int *flags
)
2659 const char *arg
= argv
[0];
2661 struct ref_store
*refs
;
2664 if (revs
->repo
!= the_repository
) {
2666 * We need some something like get_submodule_worktrees()
2667 * before we can go through all worktrees of a submodule,
2668 * .e.g with adding all HEADs from --all, which is not
2669 * supported right now, so stick to single worktree.
2671 if (!revs
->single_worktree
)
2672 BUG("--single-worktree cannot be used together with submodule");
2674 refs
= get_main_ref_store(revs
->repo
);
2679 * Commands like "git shortlog" will not accept the options below
2680 * unless parse_revision_opt queues them (as opposed to erroring
2683 * When implementing your new pseudo-option, remember to
2684 * register it in the list at the top of handle_revision_opt.
2686 if (!strcmp(arg
, "--all")) {
2687 handle_refs(refs
, revs
, *flags
, refs_for_each_ref
);
2688 handle_refs(refs
, revs
, *flags
, refs_head_ref
);
2689 if (!revs
->single_worktree
) {
2690 struct all_refs_cb cb
;
2692 init_all_refs_cb(&cb
, revs
, *flags
);
2693 other_head_refs(handle_one_ref
, &cb
);
2695 clear_ref_exclusion(&revs
->ref_excludes
);
2696 } else if (!strcmp(arg
, "--branches")) {
2697 handle_refs(refs
, revs
, *flags
, refs_for_each_branch_ref
);
2698 clear_ref_exclusion(&revs
->ref_excludes
);
2699 } else if (!strcmp(arg
, "--bisect")) {
2700 read_bisect_terms(&term_bad
, &term_good
);
2701 handle_refs(refs
, revs
, *flags
, for_each_bad_bisect_ref
);
2702 handle_refs(refs
, revs
, *flags
^ (UNINTERESTING
| BOTTOM
),
2703 for_each_good_bisect_ref
);
2705 } else if (!strcmp(arg
, "--tags")) {
2706 handle_refs(refs
, revs
, *flags
, refs_for_each_tag_ref
);
2707 clear_ref_exclusion(&revs
->ref_excludes
);
2708 } else if (!strcmp(arg
, "--remotes")) {
2709 handle_refs(refs
, revs
, *flags
, refs_for_each_remote_ref
);
2710 clear_ref_exclusion(&revs
->ref_excludes
);
2711 } else if ((argcount
= parse_long_opt("glob", argv
, &optarg
))) {
2712 struct all_refs_cb cb
;
2713 init_all_refs_cb(&cb
, revs
, *flags
);
2714 for_each_glob_ref(handle_one_ref
, optarg
, &cb
);
2715 clear_ref_exclusion(&revs
->ref_excludes
);
2717 } else if ((argcount
= parse_long_opt("exclude", argv
, &optarg
))) {
2718 add_ref_exclusion(&revs
->ref_excludes
, optarg
);
2720 } else if (skip_prefix(arg
, "--branches=", &optarg
)) {
2721 struct all_refs_cb cb
;
2722 init_all_refs_cb(&cb
, revs
, *flags
);
2723 for_each_glob_ref_in(handle_one_ref
, optarg
, "refs/heads/", &cb
);
2724 clear_ref_exclusion(&revs
->ref_excludes
);
2725 } else if (skip_prefix(arg
, "--tags=", &optarg
)) {
2726 struct all_refs_cb cb
;
2727 init_all_refs_cb(&cb
, revs
, *flags
);
2728 for_each_glob_ref_in(handle_one_ref
, optarg
, "refs/tags/", &cb
);
2729 clear_ref_exclusion(&revs
->ref_excludes
);
2730 } else if (skip_prefix(arg
, "--remotes=", &optarg
)) {
2731 struct all_refs_cb cb
;
2732 init_all_refs_cb(&cb
, revs
, *flags
);
2733 for_each_glob_ref_in(handle_one_ref
, optarg
, "refs/remotes/", &cb
);
2734 clear_ref_exclusion(&revs
->ref_excludes
);
2735 } else if (!strcmp(arg
, "--reflog")) {
2736 add_reflogs_to_pending(revs
, *flags
);
2737 } else if (!strcmp(arg
, "--indexed-objects")) {
2738 add_index_objects_to_pending(revs
, *flags
);
2739 } else if (!strcmp(arg
, "--alternate-refs")) {
2740 add_alternate_refs_to_pending(revs
, *flags
);
2741 } else if (!strcmp(arg
, "--not")) {
2742 *flags
^= UNINTERESTING
| BOTTOM
;
2743 } else if (!strcmp(arg
, "--no-walk")) {
2745 } else if (skip_prefix(arg
, "--no-walk=", &optarg
)) {
2747 * Detached form ("--no-walk X" as opposed to "--no-walk=X")
2748 * not allowed, since the argument is optional.
2751 if (!strcmp(optarg
, "sorted"))
2752 revs
->unsorted_input
= 0;
2753 else if (!strcmp(optarg
, "unsorted"))
2754 revs
->unsorted_input
= 1;
2756 return error("invalid argument to --no-walk");
2757 } else if (!strcmp(arg
, "--do-walk")) {
2759 } else if (!strcmp(arg
, "--single-worktree")) {
2760 revs
->single_worktree
= 1;
2761 } else if (skip_prefix(arg
, ("--filter="), &arg
)) {
2762 parse_list_objects_filter(&revs
->filter
, arg
);
2763 } else if (!strcmp(arg
, ("--no-filter"))) {
2764 list_objects_filter_set_no_filter(&revs
->filter
);
2772 static void NORETURN
diagnose_missing_default(const char *def
)
2775 const char *refname
;
2777 refname
= resolve_ref_unsafe(def
, 0, NULL
, &flags
);
2778 if (!refname
|| !(flags
& REF_ISSYMREF
) || (flags
& REF_ISBROKEN
))
2779 die(_("your current branch appears to be broken"));
2781 skip_prefix(refname
, "refs/heads/", &refname
);
2782 die(_("your current branch '%s' does not have any commits yet"),
2787 * Parse revision information, filling in the "rev_info" structure,
2788 * and removing the used arguments from the argument list.
2790 * Returns the number of arguments left that weren't recognized
2791 * (which are also moved to the head of the argument list)
2793 int setup_revisions(int argc
, const char **argv
, struct rev_info
*revs
, struct setup_revision_opt
*opt
)
2795 int i
, flags
, left
, seen_dashdash
, revarg_opt
;
2796 struct strvec prune_data
= STRVEC_INIT
;
2797 int seen_end_of_options
= 0;
2799 /* First, search for "--" */
2800 if (opt
&& opt
->assume_dashdash
) {
2804 for (i
= 1; i
< argc
; i
++) {
2805 const char *arg
= argv
[i
];
2806 if (strcmp(arg
, "--"))
2808 if (opt
&& opt
->free_removed_argv_elements
)
2809 free((char *)argv
[i
]);
2813 strvec_pushv(&prune_data
, argv
+ i
+ 1);
2819 /* Second, deal with arguments and options */
2821 revarg_opt
= opt
? opt
->revarg_opt
: 0;
2823 revarg_opt
|= REVARG_CANNOT_BE_FILENAME
;
2824 for (left
= i
= 1; i
< argc
; i
++) {
2825 const char *arg
= argv
[i
];
2826 if (!seen_end_of_options
&& *arg
== '-') {
2829 opts
= handle_revision_pseudo_opt(
2837 if (!strcmp(arg
, "--stdin")) {
2838 if (revs
->disable_stdin
) {
2842 if (revs
->read_from_stdin
++)
2843 die("--stdin given twice?");
2844 read_revisions_from_stdin(revs
, &prune_data
);
2848 if (!strcmp(arg
, "--end-of-options")) {
2849 seen_end_of_options
= 1;
2853 opts
= handle_revision_opt(revs
, argc
- i
, argv
+ i
,
2865 if (handle_revision_arg(arg
, revs
, flags
, revarg_opt
)) {
2867 if (seen_dashdash
|| *arg
== '^')
2868 die("bad revision '%s'", arg
);
2870 /* If we didn't have a "--":
2871 * (1) all filenames must exist;
2872 * (2) all rev-args must not be interpretable
2873 * as a valid filename.
2874 * but the latter we have checked in the main loop.
2876 for (j
= i
; j
< argc
; j
++)
2877 verify_filename(revs
->prefix
, argv
[j
], j
== i
);
2879 strvec_pushv(&prune_data
, argv
+ i
);
2883 revision_opts_finish(revs
);
2885 if (prune_data
.nr
) {
2887 * If we need to introduce the magic "a lone ':' means no
2888 * pathspec whatsoever", here is the place to do so.
2890 * if (prune_data.nr == 1 && !strcmp(prune_data[0], ":")) {
2891 * prune_data.nr = 0;
2892 * prune_data.alloc = 0;
2893 * free(prune_data.path);
2894 * prune_data.path = NULL;
2896 * terminate prune_data.alloc with NULL and
2897 * call init_pathspec() to set revs->prune_data here.
2900 parse_pathspec(&revs
->prune_data
, 0, 0,
2901 revs
->prefix
, prune_data
.v
);
2903 strvec_clear(&prune_data
);
2906 revs
->def
= opt
? opt
->def
: NULL
;
2907 if (opt
&& opt
->tweak
)
2908 opt
->tweak(revs
, opt
);
2909 if (revs
->show_merge
)
2910 prepare_show_merge(revs
);
2911 if (revs
->def
&& !revs
->pending
.nr
&& !revs
->rev_input_given
) {
2912 struct object_id oid
;
2913 struct object
*object
;
2914 struct object_context oc
;
2915 if (get_oid_with_context(revs
->repo
, revs
->def
, 0, &oid
, &oc
))
2916 diagnose_missing_default(revs
->def
);
2917 object
= get_reference(revs
, revs
->def
, &oid
, 0);
2918 add_pending_object_with_mode(revs
, object
, revs
->def
, oc
.mode
);
2921 /* Did the user ask for any diff output? Run the diff! */
2922 if (revs
->diffopt
.output_format
& ~DIFF_FORMAT_NO_OUTPUT
)
2925 /* Pickaxe, diff-filter and rename following need diffs */
2926 if ((revs
->diffopt
.pickaxe_opts
& DIFF_PICKAXE_KINDS_MASK
) ||
2927 revs
->diffopt
.filter
||
2928 revs
->diffopt
.flags
.follow_renames
)
2931 if (revs
->diffopt
.objfind
)
2932 revs
->simplify_history
= 0;
2934 if (revs
->line_level_traverse
) {
2935 if (want_ancestry(revs
))
2937 revs
->topo_order
= 1;
2940 if (revs
->topo_order
&& !generation_numbers_enabled(the_repository
))
2943 if (revs
->prune_data
.nr
) {
2944 copy_pathspec(&revs
->pruning
.pathspec
, &revs
->prune_data
);
2945 /* Can't prune commits with rename following: the paths change.. */
2946 if (!revs
->diffopt
.flags
.follow_renames
)
2948 if (!revs
->full_diff
)
2949 copy_pathspec(&revs
->diffopt
.pathspec
,
2953 diff_merges_setup_revs(revs
);
2955 revs
->diffopt
.abbrev
= revs
->abbrev
;
2957 diff_setup_done(&revs
->diffopt
);
2959 if (!is_encoding_utf8(get_log_output_encoding()))
2960 revs
->grep_filter
.ignore_locale
= 1;
2961 compile_grep_patterns(&revs
->grep_filter
);
2963 if (revs
->reverse
&& revs
->reflog_info
)
2964 die(_("options '%s' and '%s' cannot be used together"), "--reverse", "--walk-reflogs");
2965 if (revs
->reflog_info
&& revs
->limited
)
2966 die("cannot combine --walk-reflogs with history-limiting options");
2967 if (revs
->rewrite_parents
&& revs
->children
.name
)
2968 die(_("options '%s' and '%s' cannot be used together"), "--parents", "--children");
2969 if (revs
->filter
.choice
&& !revs
->blob_objects
)
2970 die(_("object filtering requires --objects"));
2973 * Limitations on the graph functionality
2975 if (revs
->reverse
&& revs
->graph
)
2976 die(_("options '%s' and '%s' cannot be used together"), "--reverse", "--graph");
2978 if (revs
->reflog_info
&& revs
->graph
)
2979 die(_("options '%s' and '%s' cannot be used together"), "--walk-reflogs", "--graph");
2980 if (revs
->no_walk
&& revs
->graph
)
2981 die(_("options '%s' and '%s' cannot be used together"), "--no-walk", "--graph");
2982 if (!revs
->reflog_info
&& revs
->grep_filter
.use_reflog_filter
)
2983 die(_("the option '%s' requires '%s'"), "--grep-reflog", "--walk-reflogs");
2985 if (revs
->line_level_traverse
&&
2986 (revs
->diffopt
.output_format
& ~(DIFF_FORMAT_PATCH
| DIFF_FORMAT_NO_OUTPUT
)))
2987 die(_("-L does not yet support diff formats besides -p and -s"));
2989 if (revs
->expand_tabs_in_log
< 0)
2990 revs
->expand_tabs_in_log
= revs
->expand_tabs_in_log_default
;
2995 static void release_revisions_cmdline(struct rev_cmdline_info
*cmdline
)
2999 for (i
= 0; i
< cmdline
->nr
; i
++)
3000 free((char *)cmdline
->rev
[i
].name
);
3004 static void release_revisions_mailmap(struct string_list
*mailmap
)
3008 clear_mailmap(mailmap
);
3012 static void release_revisions_topo_walk_info(struct topo_walk_info
*info
);
3014 void release_revisions(struct rev_info
*revs
)
3016 free_commit_list(revs
->commits
);
3017 free_commit_list(revs
->ancestry_path_bottoms
);
3018 object_array_clear(&revs
->pending
);
3019 object_array_clear(&revs
->boundary_commits
);
3020 release_revisions_cmdline(&revs
->cmdline
);
3021 list_objects_filter_release(&revs
->filter
);
3022 clear_pathspec(&revs
->prune_data
);
3023 date_mode_release(&revs
->date_mode
);
3024 release_revisions_mailmap(revs
->mailmap
);
3025 free_grep_patterns(&revs
->grep_filter
);
3026 /* TODO (need to handle "no_free"): diff_free(&revs->diffopt) */
3027 diff_free(&revs
->pruning
);
3028 reflog_walk_info_release(revs
->reflog_info
);
3029 release_revisions_topo_walk_info(revs
->topo_walk_info
);
3032 static void add_child(struct rev_info
*revs
, struct commit
*parent
, struct commit
*child
)
3034 struct commit_list
*l
= xcalloc(1, sizeof(*l
));
3037 l
->next
= add_decoration(&revs
->children
, &parent
->object
, l
);
3040 static int remove_duplicate_parents(struct rev_info
*revs
, struct commit
*commit
)
3042 struct treesame_state
*ts
= lookup_decoration(&revs
->treesame
, &commit
->object
);
3043 struct commit_list
**pp
, *p
;
3044 int surviving_parents
;
3046 /* Examine existing parents while marking ones we have seen... */
3047 pp
= &commit
->parents
;
3048 surviving_parents
= 0;
3049 while ((p
= *pp
) != NULL
) {
3050 struct commit
*parent
= p
->item
;
3051 if (parent
->object
.flags
& TMP_MARK
) {
3054 compact_treesame(revs
, commit
, surviving_parents
);
3057 parent
->object
.flags
|= TMP_MARK
;
3058 surviving_parents
++;
3061 /* clear the temporary mark */
3062 for (p
= commit
->parents
; p
; p
= p
->next
) {
3063 p
->item
->object
.flags
&= ~TMP_MARK
;
3065 /* no update_treesame() - removing duplicates can't affect TREESAME */
3066 return surviving_parents
;
3069 struct merge_simplify_state
{
3070 struct commit
*simplified
;
3073 static struct merge_simplify_state
*locate_simplify_state(struct rev_info
*revs
, struct commit
*commit
)
3075 struct merge_simplify_state
*st
;
3077 st
= lookup_decoration(&revs
->merge_simplification
, &commit
->object
);
3079 CALLOC_ARRAY(st
, 1);
3080 add_decoration(&revs
->merge_simplification
, &commit
->object
, st
);
3085 static int mark_redundant_parents(struct commit
*commit
)
3087 struct commit_list
*h
= reduce_heads(commit
->parents
);
3088 int i
= 0, marked
= 0;
3089 struct commit_list
*po
, *pn
;
3091 /* Want these for sanity-checking only */
3092 int orig_cnt
= commit_list_count(commit
->parents
);
3093 int cnt
= commit_list_count(h
);
3096 * Not ready to remove items yet, just mark them for now, based
3097 * on the output of reduce_heads(). reduce_heads outputs the reduced
3098 * set in its original order, so this isn't too hard.
3100 po
= commit
->parents
;
3103 if (pn
&& po
->item
== pn
->item
) {
3107 po
->item
->object
.flags
|= TMP_MARK
;
3113 if (i
!= cnt
|| cnt
+marked
!= orig_cnt
)
3114 die("mark_redundant_parents %d %d %d %d", orig_cnt
, cnt
, i
, marked
);
3116 free_commit_list(h
);
3121 static int mark_treesame_root_parents(struct commit
*commit
)
3123 struct commit_list
*p
;
3126 for (p
= commit
->parents
; p
; p
= p
->next
) {
3127 struct commit
*parent
= p
->item
;
3128 if (!parent
->parents
&& (parent
->object
.flags
& TREESAME
)) {
3129 parent
->object
.flags
|= TMP_MARK
;
3138 * Awkward naming - this means one parent we are TREESAME to.
3139 * cf mark_treesame_root_parents: root parents that are TREESAME (to an
3140 * empty tree). Better name suggestions?
3142 static int leave_one_treesame_to_parent(struct rev_info
*revs
, struct commit
*commit
)
3144 struct treesame_state
*ts
= lookup_decoration(&revs
->treesame
, &commit
->object
);
3145 struct commit
*unmarked
= NULL
, *marked
= NULL
;
3146 struct commit_list
*p
;
3149 for (p
= commit
->parents
, n
= 0; p
; p
= p
->next
, n
++) {
3150 if (ts
->treesame
[n
]) {
3151 if (p
->item
->object
.flags
& TMP_MARK
) {
3164 * If we are TREESAME to a marked-for-deletion parent, but not to any
3165 * unmarked parents, unmark the first TREESAME parent. This is the
3166 * parent that the default simplify_history==1 scan would have followed,
3167 * and it doesn't make sense to omit that path when asking for a
3168 * simplified full history. Retaining it improves the chances of
3169 * understanding odd missed merges that took an old version of a file.
3173 * I--------*X A modified the file, but mainline merge X used
3174 * \ / "-s ours", so took the version from I. X is
3175 * `-*A--' TREESAME to I and !TREESAME to A.
3177 * Default log from X would produce "I". Without this check,
3178 * --full-history --simplify-merges would produce "I-A-X", showing
3179 * the merge commit X and that it changed A, but not making clear that
3180 * it had just taken the I version. With this check, the topology above
3183 * Note that it is possible that the simplification chooses a different
3184 * TREESAME parent from the default, in which case this test doesn't
3185 * activate, and we _do_ drop the default parent. Example:
3187 * I------X A modified the file, but it was reverted in B,
3188 * \ / meaning mainline merge X is TREESAME to both
3191 * Default log would produce "I" by following the first parent;
3192 * --full-history --simplify-merges will produce "I-A-B". But this is a
3193 * reasonable result - it presents a logical full history leading from
3194 * I to X, and X is not an important merge.
3196 if (!unmarked
&& marked
) {
3197 marked
->object
.flags
&= ~TMP_MARK
;
3204 static int remove_marked_parents(struct rev_info
*revs
, struct commit
*commit
)
3206 struct commit_list
**pp
, *p
;
3207 int nth_parent
, removed
= 0;
3209 pp
= &commit
->parents
;
3211 while ((p
= *pp
) != NULL
) {
3212 struct commit
*parent
= p
->item
;
3213 if (parent
->object
.flags
& TMP_MARK
) {
3214 parent
->object
.flags
&= ~TMP_MARK
;
3218 compact_treesame(revs
, commit
, nth_parent
);
3225 /* Removing parents can only increase TREESAMEness */
3226 if (removed
&& !(commit
->object
.flags
& TREESAME
))
3227 update_treesame(revs
, commit
);
3232 static struct commit_list
**simplify_one(struct rev_info
*revs
, struct commit
*commit
, struct commit_list
**tail
)
3234 struct commit_list
*p
;
3235 struct commit
*parent
;
3236 struct merge_simplify_state
*st
, *pst
;
3239 st
= locate_simplify_state(revs
, commit
);
3242 * Have we handled this one?
3248 * An UNINTERESTING commit simplifies to itself, so does a
3249 * root commit. We do not rewrite parents of such commit
3252 if ((commit
->object
.flags
& UNINTERESTING
) || !commit
->parents
) {
3253 st
->simplified
= commit
;
3258 * Do we know what commit all of our parents that matter
3259 * should be rewritten to? Otherwise we are not ready to
3260 * rewrite this one yet.
3262 for (cnt
= 0, p
= commit
->parents
; p
; p
= p
->next
) {
3263 pst
= locate_simplify_state(revs
, p
->item
);
3264 if (!pst
->simplified
) {
3265 tail
= &commit_list_insert(p
->item
, tail
)->next
;
3268 if (revs
->first_parent_only
)
3272 tail
= &commit_list_insert(commit
, tail
)->next
;
3277 * Rewrite our list of parents. Note that this cannot
3278 * affect our TREESAME flags in any way - a commit is
3279 * always TREESAME to its simplification.
3281 for (p
= commit
->parents
; p
; p
= p
->next
) {
3282 pst
= locate_simplify_state(revs
, p
->item
);
3283 p
->item
= pst
->simplified
;
3284 if (revs
->first_parent_only
)
3288 if (revs
->first_parent_only
)
3291 cnt
= remove_duplicate_parents(revs
, commit
);
3294 * It is possible that we are a merge and one side branch
3295 * does not have any commit that touches the given paths;
3296 * in such a case, the immediate parent from that branch
3297 * will be rewritten to be the merge base.
3299 * o----X X: the commit we are looking at;
3300 * / / o: a commit that touches the paths;
3303 * Further, a merge of an independent branch that doesn't
3304 * touch the path will reduce to a treesame root parent:
3306 * ----o----X X: the commit we are looking at;
3307 * / o: a commit that touches the paths;
3308 * r r: a root commit not touching the paths
3310 * Detect and simplify both cases.
3313 int marked
= mark_redundant_parents(commit
);
3314 marked
+= mark_treesame_root_parents(commit
);
3316 marked
-= leave_one_treesame_to_parent(revs
, commit
);
3318 cnt
= remove_marked_parents(revs
, commit
);
3322 * A commit simplifies to itself if it is a root, if it is
3323 * UNINTERESTING, if it touches the given paths, or if it is a
3324 * merge and its parents don't simplify to one relevant commit
3325 * (the first two cases are already handled at the beginning of
3328 * Otherwise, it simplifies to what its sole relevant parent
3332 (commit
->object
.flags
& UNINTERESTING
) ||
3333 !(commit
->object
.flags
& TREESAME
) ||
3334 (parent
= one_relevant_parent(revs
, commit
->parents
)) == NULL
||
3335 (revs
->show_pulls
&& (commit
->object
.flags
& PULL_MERGE
)))
3336 st
->simplified
= commit
;
3338 pst
= locate_simplify_state(revs
, parent
);
3339 st
->simplified
= pst
->simplified
;
3344 static void simplify_merges(struct rev_info
*revs
)
3346 struct commit_list
*list
, *next
;
3347 struct commit_list
*yet_to_do
, **tail
;
3348 struct commit
*commit
;
3353 /* feed the list reversed */
3355 for (list
= revs
->commits
; list
; list
= next
) {
3356 commit
= list
->item
;
3359 * Do not free(list) here yet; the original list
3360 * is used later in this function.
3362 commit_list_insert(commit
, &yet_to_do
);
3369 commit
= pop_commit(&list
);
3370 tail
= simplify_one(revs
, commit
, tail
);
3374 /* clean up the result, removing the simplified ones */
3375 list
= revs
->commits
;
3376 revs
->commits
= NULL
;
3377 tail
= &revs
->commits
;
3379 struct merge_simplify_state
*st
;
3381 commit
= pop_commit(&list
);
3382 st
= locate_simplify_state(revs
, commit
);
3383 if (st
->simplified
== commit
)
3384 tail
= &commit_list_insert(commit
, tail
)->next
;
3388 static void set_children(struct rev_info
*revs
)
3390 struct commit_list
*l
;
3391 for (l
= revs
->commits
; l
; l
= l
->next
) {
3392 struct commit
*commit
= l
->item
;
3393 struct commit_list
*p
;
3395 for (p
= commit
->parents
; p
; p
= p
->next
)
3396 add_child(revs
, p
->item
, commit
);
3400 void reset_revision_walk(void)
3402 clear_object_flags(SEEN
| ADDED
| SHOWN
| TOPO_WALK_EXPLORED
| TOPO_WALK_INDEGREE
);
3405 static int mark_uninteresting(const struct object_id
*oid
,
3406 struct packed_git
*pack
,
3410 struct rev_info
*revs
= cb
;
3411 struct object
*o
= lookup_unknown_object(revs
->repo
, oid
);
3412 o
->flags
|= UNINTERESTING
| SEEN
;
3416 define_commit_slab(indegree_slab
, int);
3417 define_commit_slab(author_date_slab
, timestamp_t
);
3419 struct topo_walk_info
{
3420 timestamp_t min_generation
;
3421 struct prio_queue explore_queue
;
3422 struct prio_queue indegree_queue
;
3423 struct prio_queue topo_queue
;
3424 struct indegree_slab indegree
;
3425 struct author_date_slab author_date
;
3428 static int topo_walk_atexit_registered
;
3429 static unsigned int count_explore_walked
;
3430 static unsigned int count_indegree_walked
;
3431 static unsigned int count_topo_walked
;
3433 static void trace2_topo_walk_statistics_atexit(void)
3435 struct json_writer jw
= JSON_WRITER_INIT
;
3437 jw_object_begin(&jw
, 0);
3438 jw_object_intmax(&jw
, "count_explore_walked", count_explore_walked
);
3439 jw_object_intmax(&jw
, "count_indegree_walked", count_indegree_walked
);
3440 jw_object_intmax(&jw
, "count_topo_walked", count_topo_walked
);
3443 trace2_data_json("topo_walk", the_repository
, "statistics", &jw
);
3448 static inline void test_flag_and_insert(struct prio_queue
*q
, struct commit
*c
, int flag
)
3450 if (c
->object
.flags
& flag
)
3453 c
->object
.flags
|= flag
;
3454 prio_queue_put(q
, c
);
3457 static void explore_walk_step(struct rev_info
*revs
)
3459 struct topo_walk_info
*info
= revs
->topo_walk_info
;
3460 struct commit_list
*p
;
3461 struct commit
*c
= prio_queue_get(&info
->explore_queue
);
3466 if (repo_parse_commit_gently(revs
->repo
, c
, 1) < 0)
3469 count_explore_walked
++;
3471 if (revs
->sort_order
== REV_SORT_BY_AUTHOR_DATE
)
3472 record_author_date(&info
->author_date
, c
);
3474 if (revs
->max_age
!= -1 && (c
->date
< revs
->max_age
))
3475 c
->object
.flags
|= UNINTERESTING
;
3477 if (process_parents(revs
, c
, NULL
, NULL
) < 0)
3480 if (c
->object
.flags
& UNINTERESTING
)
3481 mark_parents_uninteresting(revs
, c
);
3483 for (p
= c
->parents
; p
; p
= p
->next
)
3484 test_flag_and_insert(&info
->explore_queue
, p
->item
, TOPO_WALK_EXPLORED
);
3487 static void explore_to_depth(struct rev_info
*revs
,
3488 timestamp_t gen_cutoff
)
3490 struct topo_walk_info
*info
= revs
->topo_walk_info
;
3492 while ((c
= prio_queue_peek(&info
->explore_queue
)) &&
3493 commit_graph_generation(c
) >= gen_cutoff
)
3494 explore_walk_step(revs
);
3497 static void indegree_walk_step(struct rev_info
*revs
)
3499 struct commit_list
*p
;
3500 struct topo_walk_info
*info
= revs
->topo_walk_info
;
3501 struct commit
*c
= prio_queue_get(&info
->indegree_queue
);
3506 if (repo_parse_commit_gently(revs
->repo
, c
, 1) < 0)
3509 count_indegree_walked
++;
3511 explore_to_depth(revs
, commit_graph_generation(c
));
3513 for (p
= c
->parents
; p
; p
= p
->next
) {
3514 struct commit
*parent
= p
->item
;
3515 int *pi
= indegree_slab_at(&info
->indegree
, parent
);
3517 if (repo_parse_commit_gently(revs
->repo
, parent
, 1) < 0)
3525 test_flag_and_insert(&info
->indegree_queue
, parent
, TOPO_WALK_INDEGREE
);
3527 if (revs
->first_parent_only
)
3532 static void compute_indegrees_to_depth(struct rev_info
*revs
,
3533 timestamp_t gen_cutoff
)
3535 struct topo_walk_info
*info
= revs
->topo_walk_info
;
3537 while ((c
= prio_queue_peek(&info
->indegree_queue
)) &&
3538 commit_graph_generation(c
) >= gen_cutoff
)
3539 indegree_walk_step(revs
);
3542 static void release_revisions_topo_walk_info(struct topo_walk_info
*info
)
3546 clear_prio_queue(&info
->explore_queue
);
3547 clear_prio_queue(&info
->indegree_queue
);
3548 clear_prio_queue(&info
->topo_queue
);
3549 clear_indegree_slab(&info
->indegree
);
3550 clear_author_date_slab(&info
->author_date
);
3554 static void reset_topo_walk(struct rev_info
*revs
)
3556 release_revisions_topo_walk_info(revs
->topo_walk_info
);
3557 revs
->topo_walk_info
= NULL
;
3560 static void init_topo_walk(struct rev_info
*revs
)
3562 struct topo_walk_info
*info
;
3563 struct commit_list
*list
;
3564 if (revs
->topo_walk_info
)
3565 reset_topo_walk(revs
);
3567 revs
->topo_walk_info
= xmalloc(sizeof(struct topo_walk_info
));
3568 info
= revs
->topo_walk_info
;
3569 memset(info
, 0, sizeof(struct topo_walk_info
));
3571 init_indegree_slab(&info
->indegree
);
3572 memset(&info
->explore_queue
, 0, sizeof(info
->explore_queue
));
3573 memset(&info
->indegree_queue
, 0, sizeof(info
->indegree_queue
));
3574 memset(&info
->topo_queue
, 0, sizeof(info
->topo_queue
));
3576 switch (revs
->sort_order
) {
3577 default: /* REV_SORT_IN_GRAPH_ORDER */
3578 info
->topo_queue
.compare
= NULL
;
3580 case REV_SORT_BY_COMMIT_DATE
:
3581 info
->topo_queue
.compare
= compare_commits_by_commit_date
;
3583 case REV_SORT_BY_AUTHOR_DATE
:
3584 init_author_date_slab(&info
->author_date
);
3585 info
->topo_queue
.compare
= compare_commits_by_author_date
;
3586 info
->topo_queue
.cb_data
= &info
->author_date
;
3590 info
->explore_queue
.compare
= compare_commits_by_gen_then_commit_date
;
3591 info
->indegree_queue
.compare
= compare_commits_by_gen_then_commit_date
;
3593 info
->min_generation
= GENERATION_NUMBER_INFINITY
;
3594 for (list
= revs
->commits
; list
; list
= list
->next
) {
3595 struct commit
*c
= list
->item
;
3596 timestamp_t generation
;
3598 if (repo_parse_commit_gently(revs
->repo
, c
, 1))
3601 test_flag_and_insert(&info
->explore_queue
, c
, TOPO_WALK_EXPLORED
);
3602 test_flag_and_insert(&info
->indegree_queue
, c
, TOPO_WALK_INDEGREE
);
3604 generation
= commit_graph_generation(c
);
3605 if (generation
< info
->min_generation
)
3606 info
->min_generation
= generation
;
3608 *(indegree_slab_at(&info
->indegree
, c
)) = 1;
3610 if (revs
->sort_order
== REV_SORT_BY_AUTHOR_DATE
)
3611 record_author_date(&info
->author_date
, c
);
3613 compute_indegrees_to_depth(revs
, info
->min_generation
);
3615 for (list
= revs
->commits
; list
; list
= list
->next
) {
3616 struct commit
*c
= list
->item
;
3618 if (*(indegree_slab_at(&info
->indegree
, c
)) == 1)
3619 prio_queue_put(&info
->topo_queue
, c
);
3623 * This is unfortunate; the initial tips need to be shown
3624 * in the order given from the revision traversal machinery.
3626 if (revs
->sort_order
== REV_SORT_IN_GRAPH_ORDER
)
3627 prio_queue_reverse(&info
->topo_queue
);
3629 if (trace2_is_enabled() && !topo_walk_atexit_registered
) {
3630 atexit(trace2_topo_walk_statistics_atexit
);
3631 topo_walk_atexit_registered
= 1;
3635 static struct commit
*next_topo_commit(struct rev_info
*revs
)
3638 struct topo_walk_info
*info
= revs
->topo_walk_info
;
3640 /* pop next off of topo_queue */
3641 c
= prio_queue_get(&info
->topo_queue
);
3644 *(indegree_slab_at(&info
->indegree
, c
)) = 0;
3649 static void expand_topo_walk(struct rev_info
*revs
, struct commit
*commit
)
3651 struct commit_list
*p
;
3652 struct topo_walk_info
*info
= revs
->topo_walk_info
;
3653 if (process_parents(revs
, commit
, NULL
, NULL
) < 0) {
3654 if (!revs
->ignore_missing_links
)
3655 die("Failed to traverse parents of commit %s",
3656 oid_to_hex(&commit
->object
.oid
));
3659 count_topo_walked
++;
3661 for (p
= commit
->parents
; p
; p
= p
->next
) {
3662 struct commit
*parent
= p
->item
;
3664 timestamp_t generation
;
3666 if (parent
->object
.flags
& UNINTERESTING
)
3669 if (repo_parse_commit_gently(revs
->repo
, parent
, 1) < 0)
3672 generation
= commit_graph_generation(parent
);
3673 if (generation
< info
->min_generation
) {
3674 info
->min_generation
= generation
;
3675 compute_indegrees_to_depth(revs
, info
->min_generation
);
3678 pi
= indegree_slab_at(&info
->indegree
, parent
);
3682 prio_queue_put(&info
->topo_queue
, parent
);
3684 if (revs
->first_parent_only
)
3689 int prepare_revision_walk(struct rev_info
*revs
)
3692 struct object_array old_pending
;
3693 struct commit_list
**next
= &revs
->commits
;
3695 memcpy(&old_pending
, &revs
->pending
, sizeof(old_pending
));
3696 revs
->pending
.nr
= 0;
3697 revs
->pending
.alloc
= 0;
3698 revs
->pending
.objects
= NULL
;
3699 for (i
= 0; i
< old_pending
.nr
; i
++) {
3700 struct object_array_entry
*e
= old_pending
.objects
+ i
;
3701 struct commit
*commit
= handle_commit(revs
, e
);
3703 if (!(commit
->object
.flags
& SEEN
)) {
3704 commit
->object
.flags
|= SEEN
;
3705 next
= commit_list_append(commit
, next
);
3709 object_array_clear(&old_pending
);
3711 /* Signal whether we need per-parent treesame decoration */
3712 if (revs
->simplify_merges
||
3713 (revs
->limited
&& limiting_can_increase_treesame(revs
)))
3714 revs
->treesame
.name
= "treesame";
3716 if (revs
->exclude_promisor_objects
) {
3717 for_each_packed_object(mark_uninteresting
, revs
,
3718 FOR_EACH_OBJECT_PROMISOR_ONLY
);
3721 if (!revs
->reflog_info
)
3722 prepare_to_use_bloom_filter(revs
);
3723 if (!revs
->unsorted_input
)
3724 commit_list_sort_by_date(&revs
->commits
);
3727 if (revs
->limited
) {
3728 if (limit_list(revs
) < 0)
3730 if (revs
->topo_order
)
3731 sort_in_topological_order(&revs
->commits
, revs
->sort_order
);
3732 } else if (revs
->topo_order
)
3733 init_topo_walk(revs
);
3734 if (revs
->line_level_traverse
&& want_ancestry(revs
))
3736 * At the moment we can only do line-level log with parent
3737 * rewriting by performing this expensive pre-filtering step.
3738 * If parent rewriting is not requested, then we rather
3739 * perform the line-level log filtering during the regular
3740 * history traversal.
3742 line_log_filter(revs
);
3743 if (revs
->simplify_merges
)
3744 simplify_merges(revs
);
3745 if (revs
->children
.name
)
3751 static enum rewrite_result
rewrite_one_1(struct rev_info
*revs
,
3753 struct prio_queue
*queue
)
3756 struct commit
*p
= *pp
;
3758 if (process_parents(revs
, p
, NULL
, queue
) < 0)
3759 return rewrite_one_error
;
3760 if (p
->object
.flags
& UNINTERESTING
)
3761 return rewrite_one_ok
;
3762 if (!(p
->object
.flags
& TREESAME
))
3763 return rewrite_one_ok
;
3765 return rewrite_one_noparents
;
3766 if (!(p
= one_relevant_parent(revs
, p
->parents
)))
3767 return rewrite_one_ok
;
3772 static void merge_queue_into_list(struct prio_queue
*q
, struct commit_list
**list
)
3775 struct commit
*item
= prio_queue_peek(q
);
3776 struct commit_list
*p
= *list
;
3778 if (p
&& p
->item
->date
>= item
->date
)
3781 p
= commit_list_insert(item
, list
);
3782 list
= &p
->next
; /* skip newly added item */
3783 prio_queue_get(q
); /* pop item */
3788 static enum rewrite_result
rewrite_one(struct rev_info
*revs
, struct commit
**pp
)
3790 struct prio_queue queue
= { compare_commits_by_commit_date
};
3791 enum rewrite_result ret
= rewrite_one_1(revs
, pp
, &queue
);
3792 merge_queue_into_list(&queue
, &revs
->commits
);
3793 clear_prio_queue(&queue
);
3797 int rewrite_parents(struct rev_info
*revs
, struct commit
*commit
,
3798 rewrite_parent_fn_t rewrite_parent
)
3800 struct commit_list
**pp
= &commit
->parents
;
3802 struct commit_list
*parent
= *pp
;
3803 switch (rewrite_parent(revs
, &parent
->item
)) {
3804 case rewrite_one_ok
:
3806 case rewrite_one_noparents
:
3809 case rewrite_one_error
:
3814 remove_duplicate_parents(revs
, commit
);
3818 static int commit_match(struct commit
*commit
, struct rev_info
*opt
)
3821 const char *encoding
;
3822 const char *message
;
3823 struct strbuf buf
= STRBUF_INIT
;
3825 if (!opt
->grep_filter
.pattern_list
&& !opt
->grep_filter
.header_list
)
3828 /* Prepend "fake" headers as needed */
3829 if (opt
->grep_filter
.use_reflog_filter
) {
3830 strbuf_addstr(&buf
, "reflog ");
3831 get_reflog_message(&buf
, opt
->reflog_info
);
3832 strbuf_addch(&buf
, '\n');
3836 * We grep in the user's output encoding, under the assumption that it
3837 * is the encoding they are most likely to write their grep pattern
3838 * for. In addition, it means we will match the "notes" encoding below,
3839 * so we will not end up with a buffer that has two different encodings
3842 encoding
= get_log_output_encoding();
3843 message
= logmsg_reencode(commit
, NULL
, encoding
);
3845 /* Copy the commit to temporary if we are using "fake" headers */
3847 strbuf_addstr(&buf
, message
);
3849 if (opt
->grep_filter
.header_list
&& opt
->mailmap
) {
3850 const char *commit_headers
[] = { "author ", "committer ", NULL
};
3853 strbuf_addstr(&buf
, message
);
3855 apply_mailmap_to_header(&buf
, commit_headers
, opt
->mailmap
);
3858 /* Append "fake" message parts as needed */
3859 if (opt
->show_notes
) {
3861 strbuf_addstr(&buf
, message
);
3862 format_display_notes(&commit
->object
.oid
, &buf
, encoding
, 1);
3866 * Find either in the original commit message, or in the temporary.
3867 * Note that we cast away the constness of "message" here. It is
3868 * const because it may come from the cached commit buffer. That's OK,
3869 * because we know that it is modifiable heap memory, and that while
3870 * grep_buffer may modify it for speed, it will restore any
3871 * changes before returning.
3874 retval
= grep_buffer(&opt
->grep_filter
, buf
.buf
, buf
.len
);
3876 retval
= grep_buffer(&opt
->grep_filter
,
3877 (char *)message
, strlen(message
));
3878 strbuf_release(&buf
);
3879 unuse_commit_buffer(commit
, message
);
3883 static inline int want_ancestry(const struct rev_info
*revs
)
3885 return (revs
->rewrite_parents
|| revs
->children
.name
);
3889 * Return a timestamp to be used for --since/--until comparisons for this
3890 * commit, based on the revision options.
3892 static timestamp_t
comparison_date(const struct rev_info
*revs
,
3893 struct commit
*commit
)
3895 return revs
->reflog_info
?
3896 get_reflog_timestamp(revs
->reflog_info
) :
3900 enum commit_action
get_commit_action(struct rev_info
*revs
, struct commit
*commit
)
3902 if (commit
->object
.flags
& SHOWN
)
3903 return commit_ignore
;
3904 if (revs
->unpacked
&& has_object_pack(&commit
->object
.oid
))
3905 return commit_ignore
;
3906 if (revs
->no_kept_objects
) {
3907 if (has_object_kept_pack(&commit
->object
.oid
,
3908 revs
->keep_pack_cache_flags
))
3909 return commit_ignore
;
3911 if (commit
->object
.flags
& UNINTERESTING
)
3912 return commit_ignore
;
3913 if (revs
->line_level_traverse
&& !want_ancestry(revs
)) {
3915 * In case of line-level log with parent rewriting
3916 * prepare_revision_walk() already took care of all line-level
3917 * log filtering, and there is nothing left to do here.
3919 * If parent rewriting was not requested, then this is the
3920 * place to perform the line-level log filtering. Notably,
3921 * this check, though expensive, must come before the other,
3922 * cheaper filtering conditions, because the tracked line
3923 * ranges must be adjusted even when the commit will end up
3924 * being ignored based on other conditions.
3926 if (!line_log_process_ranges_arbitrary_commit(revs
, commit
))
3927 return commit_ignore
;
3929 if (revs
->min_age
!= -1 &&
3930 comparison_date(revs
, commit
) > revs
->min_age
)
3931 return commit_ignore
;
3932 if (revs
->max_age_as_filter
!= -1 &&
3933 comparison_date(revs
, commit
) < revs
->max_age_as_filter
)
3934 return commit_ignore
;
3935 if (revs
->min_parents
|| (revs
->max_parents
>= 0)) {
3936 int n
= commit_list_count(commit
->parents
);
3937 if ((n
< revs
->min_parents
) ||
3938 ((revs
->max_parents
>= 0) && (n
> revs
->max_parents
)))
3939 return commit_ignore
;
3941 if (!commit_match(commit
, revs
))
3942 return commit_ignore
;
3943 if (revs
->prune
&& revs
->dense
) {
3944 /* Commit without changes? */
3945 if (commit
->object
.flags
& TREESAME
) {
3947 struct commit_list
*p
;
3948 /* drop merges unless we want parenthood */
3949 if (!want_ancestry(revs
))
3950 return commit_ignore
;
3952 if (revs
->show_pulls
&& (commit
->object
.flags
& PULL_MERGE
))
3956 * If we want ancestry, then need to keep any merges
3957 * between relevant commits to tie together topology.
3958 * For consistency with TREESAME and simplification
3959 * use "relevant" here rather than just INTERESTING,
3960 * to treat bottom commit(s) as part of the topology.
3962 for (n
= 0, p
= commit
->parents
; p
; p
= p
->next
)
3963 if (relevant_commit(p
->item
))
3966 return commit_ignore
;
3972 define_commit_slab(saved_parents
, struct commit_list
*);
3974 #define EMPTY_PARENT_LIST ((struct commit_list *)-1)
3977 * You may only call save_parents() once per commit (this is checked
3978 * for non-root commits).
3980 static void save_parents(struct rev_info
*revs
, struct commit
*commit
)
3982 struct commit_list
**pp
;
3984 if (!revs
->saved_parents_slab
) {
3985 revs
->saved_parents_slab
= xmalloc(sizeof(struct saved_parents
));
3986 init_saved_parents(revs
->saved_parents_slab
);
3989 pp
= saved_parents_at(revs
->saved_parents_slab
, commit
);
3992 * When walking with reflogs, we may visit the same commit
3993 * several times: once for each appearance in the reflog.
3995 * In this case, save_parents() will be called multiple times.
3996 * We want to keep only the first set of parents. We need to
3997 * store a sentinel value for an empty (i.e., NULL) parent
3998 * list to distinguish it from a not-yet-saved list, however.
4002 if (commit
->parents
)
4003 *pp
= copy_commit_list(commit
->parents
);
4005 *pp
= EMPTY_PARENT_LIST
;
4008 static void free_saved_parents(struct rev_info
*revs
)
4010 if (revs
->saved_parents_slab
)
4011 clear_saved_parents(revs
->saved_parents_slab
);
4014 struct commit_list
*get_saved_parents(struct rev_info
*revs
, const struct commit
*commit
)
4016 struct commit_list
*parents
;
4018 if (!revs
->saved_parents_slab
)
4019 return commit
->parents
;
4021 parents
= *saved_parents_at(revs
->saved_parents_slab
, commit
);
4022 if (parents
== EMPTY_PARENT_LIST
)
4027 enum commit_action
simplify_commit(struct rev_info
*revs
, struct commit
*commit
)
4029 enum commit_action action
= get_commit_action(revs
, commit
);
4031 if (action
== commit_show
&&
4032 revs
->prune
&& revs
->dense
&& want_ancestry(revs
)) {
4034 * --full-diff on simplified parents is no good: it
4035 * will show spurious changes from the commits that
4036 * were elided. So we save the parents on the side
4037 * when --full-diff is in effect.
4039 if (revs
->full_diff
)
4040 save_parents(revs
, commit
);
4041 if (rewrite_parents(revs
, commit
, rewrite_one
) < 0)
4042 return commit_error
;
4047 static void track_linear(struct rev_info
*revs
, struct commit
*commit
)
4049 if (revs
->track_first_time
) {
4051 revs
->track_first_time
= 0;
4053 struct commit_list
*p
;
4054 for (p
= revs
->previous_parents
; p
; p
= p
->next
)
4055 if (p
->item
== NULL
|| /* first commit */
4056 oideq(&p
->item
->object
.oid
, &commit
->object
.oid
))
4058 revs
->linear
= p
!= NULL
;
4060 if (revs
->reverse
) {
4062 commit
->object
.flags
|= TRACK_LINEAR
;
4064 free_commit_list(revs
->previous_parents
);
4065 revs
->previous_parents
= copy_commit_list(commit
->parents
);
4068 static struct commit
*get_revision_1(struct rev_info
*revs
)
4071 struct commit
*commit
;
4073 if (revs
->reflog_info
)
4074 commit
= next_reflog_entry(revs
->reflog_info
);
4075 else if (revs
->topo_walk_info
)
4076 commit
= next_topo_commit(revs
);
4078 commit
= pop_commit(&revs
->commits
);
4083 if (revs
->reflog_info
)
4084 commit
->object
.flags
&= ~(ADDED
| SEEN
| SHOWN
);
4087 * If we haven't done the list limiting, we need to look at
4088 * the parents here. We also need to do the date-based limiting
4089 * that we'd otherwise have done in limit_list().
4091 if (!revs
->limited
) {
4092 if (revs
->max_age
!= -1 &&
4093 comparison_date(revs
, commit
) < revs
->max_age
)
4096 if (revs
->reflog_info
)
4097 try_to_simplify_commit(revs
, commit
);
4098 else if (revs
->topo_walk_info
)
4099 expand_topo_walk(revs
, commit
);
4100 else if (process_parents(revs
, commit
, &revs
->commits
, NULL
) < 0) {
4101 if (!revs
->ignore_missing_links
)
4102 die("Failed to traverse parents of commit %s",
4103 oid_to_hex(&commit
->object
.oid
));
4107 switch (simplify_commit(revs
, commit
)) {
4111 die("Failed to simplify parents of commit %s",
4112 oid_to_hex(&commit
->object
.oid
));
4114 if (revs
->track_linear
)
4115 track_linear(revs
, commit
);
4122 * Return true for entries that have not yet been shown. (This is an
4123 * object_array_each_func_t.)
4125 static int entry_unshown(struct object_array_entry
*entry
, void *cb_data_unused
)
4127 return !(entry
->item
->flags
& SHOWN
);
4131 * If array is on the verge of a realloc, garbage-collect any entries
4132 * that have already been shown to try to free up some space.
4134 static void gc_boundary(struct object_array
*array
)
4136 if (array
->nr
== array
->alloc
)
4137 object_array_filter(array
, entry_unshown
, NULL
);
4140 static void create_boundary_commit_list(struct rev_info
*revs
)
4144 struct object_array
*array
= &revs
->boundary_commits
;
4145 struct object_array_entry
*objects
= array
->objects
;
4148 * If revs->commits is non-NULL at this point, an error occurred in
4149 * get_revision_1(). Ignore the error and continue printing the
4150 * boundary commits anyway. (This is what the code has always
4153 free_commit_list(revs
->commits
);
4154 revs
->commits
= NULL
;
4157 * Put all of the actual boundary commits from revs->boundary_commits
4158 * into revs->commits
4160 for (i
= 0; i
< array
->nr
; i
++) {
4161 c
= (struct commit
*)(objects
[i
].item
);
4164 if (!(c
->object
.flags
& CHILD_SHOWN
))
4166 if (c
->object
.flags
& (SHOWN
| BOUNDARY
))
4168 c
->object
.flags
|= BOUNDARY
;
4169 commit_list_insert(c
, &revs
->commits
);
4173 * If revs->topo_order is set, sort the boundary commits
4174 * in topological order
4176 sort_in_topological_order(&revs
->commits
, revs
->sort_order
);
4179 static struct commit
*get_revision_internal(struct rev_info
*revs
)
4181 struct commit
*c
= NULL
;
4182 struct commit_list
*l
;
4184 if (revs
->boundary
== 2) {
4186 * All of the normal commits have already been returned,
4187 * and we are now returning boundary commits.
4188 * create_boundary_commit_list() has populated
4189 * revs->commits with the remaining commits to return.
4191 c
= pop_commit(&revs
->commits
);
4193 c
->object
.flags
|= SHOWN
;
4198 * If our max_count counter has reached zero, then we are done. We
4199 * don't simply return NULL because we still might need to show
4200 * boundary commits. But we want to avoid calling get_revision_1, which
4201 * might do a considerable amount of work finding the next commit only
4202 * for us to throw it away.
4204 * If it is non-zero, then either we don't have a max_count at all
4205 * (-1), or it is still counting, in which case we decrement.
4207 if (revs
->max_count
) {
4208 c
= get_revision_1(revs
);
4210 while (revs
->skip_count
> 0) {
4212 c
= get_revision_1(revs
);
4218 if (revs
->max_count
> 0)
4223 c
->object
.flags
|= SHOWN
;
4225 if (!revs
->boundary
)
4230 * get_revision_1() runs out the commits, and
4231 * we are done computing the boundaries.
4232 * switch to boundary commits output mode.
4237 * Update revs->commits to contain the list of
4240 create_boundary_commit_list(revs
);
4242 return get_revision_internal(revs
);
4246 * boundary commits are the commits that are parents of the
4247 * ones we got from get_revision_1() but they themselves are
4248 * not returned from get_revision_1(). Before returning
4249 * 'c', we need to mark its parents that they could be boundaries.
4252 for (l
= c
->parents
; l
; l
= l
->next
) {
4254 p
= &(l
->item
->object
);
4255 if (p
->flags
& (CHILD_SHOWN
| SHOWN
))
4257 p
->flags
|= CHILD_SHOWN
;
4258 gc_boundary(&revs
->boundary_commits
);
4259 add_object_array(p
, NULL
, &revs
->boundary_commits
);
4265 struct commit
*get_revision(struct rev_info
*revs
)
4268 struct commit_list
*reversed
;
4270 if (revs
->reverse
) {
4272 while ((c
= get_revision_internal(revs
)))
4273 commit_list_insert(c
, &reversed
);
4274 revs
->commits
= reversed
;
4276 revs
->reverse_output_stage
= 1;
4279 if (revs
->reverse_output_stage
) {
4280 c
= pop_commit(&revs
->commits
);
4281 if (revs
->track_linear
)
4282 revs
->linear
= !!(c
&& c
->object
.flags
& TRACK_LINEAR
);
4286 c
= get_revision_internal(revs
);
4287 if (c
&& revs
->graph
)
4288 graph_update(revs
->graph
, c
);
4290 free_saved_parents(revs
);
4291 free_commit_list(revs
->previous_parents
);
4292 revs
->previous_parents
= NULL
;
4297 const char *get_revision_mark(const struct rev_info
*revs
, const struct commit
*commit
)
4299 if (commit
->object
.flags
& BOUNDARY
)
4301 else if (commit
->object
.flags
& UNINTERESTING
)
4303 else if (commit
->object
.flags
& PATCHSAME
)
4305 else if (!revs
|| revs
->left_right
) {
4306 if (commit
->object
.flags
& SYMMETRIC_LEFT
)
4310 } else if (revs
->graph
)
4312 else if (revs
->cherry_mark
)
4317 void put_revision_mark(const struct rev_info
*revs
, const struct commit
*commit
)
4319 const char *mark
= get_revision_mark(revs
, commit
);
4322 fputs(mark
, stdout
);