1 #include "git-compat-util.h"
4 #include "environment.h"
7 #include "object-name.h"
8 #include "object-file.h"
9 #include "object-store.h"
15 #include "diff-merges.h"
18 #include "repository.h"
21 #include "reflog-walk.h"
22 #include "patch-ids.h"
25 #include "string-list.h"
28 #include "commit-slab.h"
30 #include "cache-tree.h"
34 #include "read-cache.h"
36 #include "sparse-index.h"
39 #include "commit-reach.h"
40 #include "commit-graph.h"
41 #include "prio-queue.h"
45 #include "json-writer.h"
46 #include "list-objects-filter-options.h"
47 #include "resolve-undo.h"
48 #include "parse-options.h"
50 volatile show_early_output_fn_t show_early_output
;
52 static const char *term_bad
;
53 static const char *term_good
;
55 implement_shared_commit_slab(revision_sources
, char *);
57 static inline int want_ancestry(const struct rev_info
*revs
);
59 void show_object_with_name(FILE *out
, struct object
*obj
, const char *name
)
61 fprintf(out
, "%s ", oid_to_hex(&obj
->oid
));
62 for (const char *p
= name
; *p
&& *p
!= '\n'; p
++)
67 static void mark_blob_uninteresting(struct blob
*blob
)
71 if (blob
->object
.flags
& UNINTERESTING
)
73 blob
->object
.flags
|= UNINTERESTING
;
76 static void mark_tree_contents_uninteresting(struct repository
*r
,
79 struct tree_desc desc
;
80 struct name_entry entry
;
82 if (parse_tree_gently(tree
, 1) < 0)
85 init_tree_desc(&desc
, tree
->buffer
, tree
->size
);
86 while (tree_entry(&desc
, &entry
)) {
87 switch (object_type(entry
.mode
)) {
89 mark_tree_uninteresting(r
, lookup_tree(r
, &entry
.oid
));
92 mark_blob_uninteresting(lookup_blob(r
, &entry
.oid
));
95 /* Subproject commit - not in this repository */
101 * We don't care about the tree any more
102 * after it has been marked uninteresting.
104 free_tree_buffer(tree
);
107 void mark_tree_uninteresting(struct repository
*r
, struct tree
*tree
)
115 if (obj
->flags
& UNINTERESTING
)
117 obj
->flags
|= UNINTERESTING
;
118 mark_tree_contents_uninteresting(r
, tree
);
121 struct path_and_oids_entry
{
122 struct hashmap_entry ent
;
127 static int path_and_oids_cmp(const void *hashmap_cmp_fn_data UNUSED
,
128 const struct hashmap_entry
*eptr
,
129 const struct hashmap_entry
*entry_or_key
,
130 const void *keydata UNUSED
)
132 const struct path_and_oids_entry
*e1
, *e2
;
134 e1
= container_of(eptr
, const struct path_and_oids_entry
, ent
);
135 e2
= container_of(entry_or_key
, const struct path_and_oids_entry
, ent
);
137 return strcmp(e1
->path
, e2
->path
);
140 static void paths_and_oids_clear(struct hashmap
*map
)
142 struct hashmap_iter iter
;
143 struct path_and_oids_entry
*entry
;
145 hashmap_for_each_entry(map
, &iter
, entry
, ent
/* member name */) {
146 oidset_clear(&entry
->trees
);
150 hashmap_clear_and_free(map
, struct path_and_oids_entry
, ent
);
153 static void paths_and_oids_insert(struct hashmap
*map
,
155 const struct object_id
*oid
)
157 int hash
= strhash(path
);
158 struct path_and_oids_entry key
;
159 struct path_and_oids_entry
*entry
;
161 hashmap_entry_init(&key
.ent
, hash
);
163 /* use a shallow copy for the lookup */
164 key
.path
= (char *)path
;
165 oidset_init(&key
.trees
, 0);
167 entry
= hashmap_get_entry(map
, &key
, ent
, NULL
);
169 CALLOC_ARRAY(entry
, 1);
170 hashmap_entry_init(&entry
->ent
, hash
);
171 entry
->path
= xstrdup(key
.path
);
172 oidset_init(&entry
->trees
, 16);
173 hashmap_put(map
, &entry
->ent
);
176 oidset_insert(&entry
->trees
, oid
);
179 static void add_children_by_path(struct repository
*r
,
183 struct tree_desc desc
;
184 struct name_entry entry
;
189 if (parse_tree_gently(tree
, 1) < 0)
192 init_tree_desc(&desc
, tree
->buffer
, tree
->size
);
193 while (tree_entry(&desc
, &entry
)) {
194 switch (object_type(entry
.mode
)) {
196 paths_and_oids_insert(map
, entry
.path
, &entry
.oid
);
198 if (tree
->object
.flags
& UNINTERESTING
) {
199 struct tree
*child
= lookup_tree(r
, &entry
.oid
);
201 child
->object
.flags
|= UNINTERESTING
;
205 if (tree
->object
.flags
& UNINTERESTING
) {
206 struct blob
*child
= lookup_blob(r
, &entry
.oid
);
208 child
->object
.flags
|= UNINTERESTING
;
212 /* Subproject commit - not in this repository */
217 free_tree_buffer(tree
);
220 void mark_trees_uninteresting_sparse(struct repository
*r
,
221 struct oidset
*trees
)
223 unsigned has_interesting
= 0, has_uninteresting
= 0;
224 struct hashmap map
= HASHMAP_INIT(path_and_oids_cmp
, NULL
);
225 struct hashmap_iter map_iter
;
226 struct path_and_oids_entry
*entry
;
227 struct object_id
*oid
;
228 struct oidset_iter iter
;
230 oidset_iter_init(trees
, &iter
);
231 while ((!has_interesting
|| !has_uninteresting
) &&
232 (oid
= oidset_iter_next(&iter
))) {
233 struct tree
*tree
= lookup_tree(r
, oid
);
238 if (tree
->object
.flags
& UNINTERESTING
)
239 has_uninteresting
= 1;
244 /* Do not walk unless we have both types of trees. */
245 if (!has_uninteresting
|| !has_interesting
)
248 oidset_iter_init(trees
, &iter
);
249 while ((oid
= oidset_iter_next(&iter
))) {
250 struct tree
*tree
= lookup_tree(r
, oid
);
251 add_children_by_path(r
, tree
, &map
);
254 hashmap_for_each_entry(&map
, &map_iter
, entry
, ent
/* member name */)
255 mark_trees_uninteresting_sparse(r
, &entry
->trees
);
257 paths_and_oids_clear(&map
);
260 struct commit_stack
{
261 struct commit
**items
;
264 #define COMMIT_STACK_INIT { 0 }
266 static void commit_stack_push(struct commit_stack
*stack
, struct commit
*commit
)
268 ALLOC_GROW(stack
->items
, stack
->nr
+ 1, stack
->alloc
);
269 stack
->items
[stack
->nr
++] = commit
;
272 static struct commit
*commit_stack_pop(struct commit_stack
*stack
)
274 return stack
->nr
? stack
->items
[--stack
->nr
] : NULL
;
277 static void commit_stack_clear(struct commit_stack
*stack
)
279 FREE_AND_NULL(stack
->items
);
280 stack
->nr
= stack
->alloc
= 0;
283 static void mark_one_parent_uninteresting(struct rev_info
*revs
, struct commit
*commit
,
284 struct commit_stack
*pending
)
286 struct commit_list
*l
;
288 if (commit
->object
.flags
& UNINTERESTING
)
290 commit
->object
.flags
|= UNINTERESTING
;
293 * Normally we haven't parsed the parent
294 * yet, so we won't have a parent of a parent
295 * here. However, it may turn out that we've
296 * reached this commit some other way (where it
297 * wasn't uninteresting), in which case we need
298 * to mark its parents recursively too..
300 for (l
= commit
->parents
; l
; l
= l
->next
) {
301 commit_stack_push(pending
, l
->item
);
302 if (revs
&& revs
->exclude_first_parent_only
)
307 void mark_parents_uninteresting(struct rev_info
*revs
, struct commit
*commit
)
309 struct commit_stack pending
= COMMIT_STACK_INIT
;
310 struct commit_list
*l
;
312 for (l
= commit
->parents
; l
; l
= l
->next
) {
313 mark_one_parent_uninteresting(revs
, l
->item
, &pending
);
314 if (revs
&& revs
->exclude_first_parent_only
)
318 while (pending
.nr
> 0)
319 mark_one_parent_uninteresting(revs
, commit_stack_pop(&pending
),
322 commit_stack_clear(&pending
);
325 static void add_pending_object_with_path(struct rev_info
*revs
,
327 const char *name
, unsigned mode
,
330 struct interpret_branch_name_options options
= { 0 };
333 if (revs
->no_walk
&& (obj
->flags
& UNINTERESTING
))
335 if (revs
->reflog_info
&& obj
->type
== OBJ_COMMIT
) {
336 struct strbuf buf
= STRBUF_INIT
;
337 size_t namelen
= strlen(name
);
338 int len
= repo_interpret_branch_name(the_repository
, name
,
339 namelen
, &buf
, &options
);
341 if (0 < len
&& len
< namelen
&& buf
.len
)
342 strbuf_addstr(&buf
, name
+ len
);
343 add_reflog_for_walk(revs
->reflog_info
,
344 (struct commit
*)obj
,
345 buf
.buf
[0] ? buf
.buf
: name
);
346 strbuf_release(&buf
);
347 return; /* do not add the commit itself */
349 add_object_array_with_path(obj
, name
, &revs
->pending
, mode
, path
);
352 static void add_pending_object_with_mode(struct rev_info
*revs
,
354 const char *name
, unsigned mode
)
356 add_pending_object_with_path(revs
, obj
, name
, mode
, NULL
);
359 void add_pending_object(struct rev_info
*revs
,
360 struct object
*obj
, const char *name
)
362 add_pending_object_with_mode(revs
, obj
, name
, S_IFINVALID
);
365 void add_head_to_pending(struct rev_info
*revs
)
367 struct object_id oid
;
369 if (repo_get_oid(the_repository
, "HEAD", &oid
))
371 obj
= parse_object(revs
->repo
, &oid
);
374 add_pending_object(revs
, obj
, "HEAD");
377 static struct object
*get_reference(struct rev_info
*revs
, const char *name
,
378 const struct object_id
*oid
,
381 struct object
*object
;
383 object
= parse_object_with_flags(revs
->repo
, oid
,
384 revs
->verify_objects
? 0 :
385 PARSE_OBJECT_SKIP_HASH_CHECK
);
388 if (revs
->ignore_missing
)
390 if (revs
->exclude_promisor_objects
&& is_promisor_object(oid
))
392 die("bad object %s", name
);
394 object
->flags
|= flags
;
398 void add_pending_oid(struct rev_info
*revs
, const char *name
,
399 const struct object_id
*oid
, unsigned int flags
)
401 struct object
*object
= get_reference(revs
, name
, oid
, flags
);
402 add_pending_object(revs
, object
, name
);
405 static struct commit
*handle_commit(struct rev_info
*revs
,
406 struct object_array_entry
*entry
)
408 struct object
*object
= entry
->item
;
409 const char *name
= entry
->name
;
410 const char *path
= entry
->path
;
411 unsigned int mode
= entry
->mode
;
412 unsigned long flags
= object
->flags
;
415 * Tag object? Look what it points to..
417 while (object
->type
== OBJ_TAG
) {
418 struct tag
*tag
= (struct tag
*) object
;
419 if (revs
->tag_objects
&& !(flags
& UNINTERESTING
))
420 add_pending_object(revs
, object
, tag
->tag
);
421 object
= parse_object(revs
->repo
, get_tagged_oid(tag
));
423 if (revs
->ignore_missing_links
|| (flags
& UNINTERESTING
))
425 if (revs
->exclude_promisor_objects
&&
426 is_promisor_object(&tag
->tagged
->oid
))
428 die("bad object %s", oid_to_hex(&tag
->tagged
->oid
));
430 object
->flags
|= flags
;
432 * We'll handle the tagged object by looping or dropping
433 * through to the non-tag handlers below. Do not
434 * propagate path data from the tag's pending entry.
441 * Commit object? Just return it, we'll do all the complex
444 if (object
->type
== OBJ_COMMIT
) {
445 struct commit
*commit
= (struct commit
*)object
;
447 if (repo_parse_commit(revs
->repo
, commit
) < 0)
448 die("unable to parse commit %s", name
);
449 if (flags
& UNINTERESTING
) {
450 mark_parents_uninteresting(revs
, commit
);
452 if (!revs
->topo_order
|| !generation_numbers_enabled(the_repository
))
456 char **slot
= revision_sources_at(revs
->sources
, commit
);
459 *slot
= xstrdup(name
);
465 * Tree object? Either mark it uninteresting, or add it
466 * to the list of objects to look at later..
468 if (object
->type
== OBJ_TREE
) {
469 struct tree
*tree
= (struct tree
*)object
;
470 if (!revs
->tree_objects
)
472 if (flags
& UNINTERESTING
) {
473 mark_tree_contents_uninteresting(revs
->repo
, tree
);
476 add_pending_object_with_path(revs
, object
, name
, mode
, path
);
481 * Blob object? You know the drill by now..
483 if (object
->type
== OBJ_BLOB
) {
484 if (!revs
->blob_objects
)
486 if (flags
& UNINTERESTING
)
488 add_pending_object_with_path(revs
, object
, name
, mode
, path
);
491 die("%s is unknown object", name
);
494 static int everybody_uninteresting(struct commit_list
*orig
,
495 struct commit
**interesting_cache
)
497 struct commit_list
*list
= orig
;
499 if (*interesting_cache
) {
500 struct commit
*commit
= *interesting_cache
;
501 if (!(commit
->object
.flags
& UNINTERESTING
))
506 struct commit
*commit
= list
->item
;
508 if (commit
->object
.flags
& UNINTERESTING
)
511 *interesting_cache
= commit
;
518 * A definition of "relevant" commit that we can use to simplify limited graphs
519 * by eliminating side branches.
521 * A "relevant" commit is one that is !UNINTERESTING (ie we are including it
522 * in our list), or that is a specified BOTTOM commit. Then after computing
523 * a limited list, during processing we can generally ignore boundary merges
524 * coming from outside the graph, (ie from irrelevant parents), and treat
525 * those merges as if they were single-parent. TREESAME is defined to consider
526 * only relevant parents, if any. If we are TREESAME to our on-graph parents,
527 * we don't care if we were !TREESAME to non-graph parents.
529 * Treating bottom commits as relevant ensures that a limited graph's
530 * connection to the actual bottom commit is not viewed as a side branch, but
531 * treated as part of the graph. For example:
533 * ....Z...A---X---o---o---B
537 * When computing "A..B", the A-X connection is at least as important as
538 * Y-X, despite A being flagged UNINTERESTING.
540 * And when computing --ancestry-path "A..B", the A-X connection is more
541 * important than Y-X, despite both A and Y being flagged UNINTERESTING.
543 static inline int relevant_commit(struct commit
*commit
)
545 return (commit
->object
.flags
& (UNINTERESTING
| BOTTOM
)) != UNINTERESTING
;
549 * Return a single relevant commit from a parent list. If we are a TREESAME
550 * commit, and this selects one of our parents, then we can safely simplify to
553 static struct commit
*one_relevant_parent(const struct rev_info
*revs
,
554 struct commit_list
*orig
)
556 struct commit_list
*list
= orig
;
557 struct commit
*relevant
= NULL
;
563 * For 1-parent commits, or if first-parent-only, then return that
564 * first parent (even if not "relevant" by the above definition).
565 * TREESAME will have been set purely on that parent.
567 if (revs
->first_parent_only
|| !orig
->next
)
571 * For multi-parent commits, identify a sole relevant parent, if any.
572 * If we have only one relevant parent, then TREESAME will be set purely
573 * with regard to that parent, and we can simplify accordingly.
575 * If we have more than one relevant parent, or no relevant parents
576 * (and multiple irrelevant ones), then we can't select a parent here
580 struct commit
*commit
= list
->item
;
582 if (relevant_commit(commit
)) {
592 * The goal is to get REV_TREE_NEW as the result only if the
593 * diff consists of all '+' (and no other changes), REV_TREE_OLD
594 * if the whole diff is removal of old data, and otherwise
595 * REV_TREE_DIFFERENT (of course if the trees are the same we
596 * want REV_TREE_SAME).
598 * The only time we care about the distinction is when
599 * remove_empty_trees is in effect, in which case we care only about
600 * whether the whole change is REV_TREE_NEW, or if there's another type
601 * of change. Which means we can stop the diff early in either of these
604 * 1. We're not using remove_empty_trees at all.
606 * 2. We saw anything except REV_TREE_NEW.
608 #define REV_TREE_SAME 0
609 #define REV_TREE_NEW 1 /* Only new files */
610 #define REV_TREE_OLD 2 /* Only files removed */
611 #define REV_TREE_DIFFERENT 3 /* Mixed changes */
612 static int tree_difference
= REV_TREE_SAME
;
614 static void file_add_remove(struct diff_options
*options
,
616 unsigned mode UNUSED
,
617 const struct object_id
*oid UNUSED
,
618 int oid_valid UNUSED
,
619 const char *fullpath UNUSED
,
620 unsigned dirty_submodule UNUSED
)
622 int diff
= addremove
== '+' ? REV_TREE_NEW
: REV_TREE_OLD
;
623 struct rev_info
*revs
= options
->change_fn_data
;
625 tree_difference
|= diff
;
626 if (!revs
->remove_empty_trees
|| tree_difference
!= REV_TREE_NEW
)
627 options
->flags
.has_changes
= 1;
630 static void file_change(struct diff_options
*options
,
631 unsigned old_mode UNUSED
,
632 unsigned new_mode UNUSED
,
633 const struct object_id
*old_oid UNUSED
,
634 const struct object_id
*new_oid UNUSED
,
635 int old_oid_valid UNUSED
,
636 int new_oid_valid UNUSED
,
637 const char *fullpath UNUSED
,
638 unsigned old_dirty_submodule UNUSED
,
639 unsigned new_dirty_submodule UNUSED
)
641 tree_difference
= REV_TREE_DIFFERENT
;
642 options
->flags
.has_changes
= 1;
645 static int bloom_filter_atexit_registered
;
646 static unsigned int count_bloom_filter_maybe
;
647 static unsigned int count_bloom_filter_definitely_not
;
648 static unsigned int count_bloom_filter_false_positive
;
649 static unsigned int count_bloom_filter_not_present
;
651 static void trace2_bloom_filter_statistics_atexit(void)
653 struct json_writer jw
= JSON_WRITER_INIT
;
655 jw_object_begin(&jw
, 0);
656 jw_object_intmax(&jw
, "filter_not_present", count_bloom_filter_not_present
);
657 jw_object_intmax(&jw
, "maybe", count_bloom_filter_maybe
);
658 jw_object_intmax(&jw
, "definitely_not", count_bloom_filter_definitely_not
);
659 jw_object_intmax(&jw
, "false_positive", count_bloom_filter_false_positive
);
662 trace2_data_json("bloom", the_repository
, "statistics", &jw
);
667 static int forbid_bloom_filters(struct pathspec
*spec
)
669 if (spec
->has_wildcard
)
673 if (spec
->magic
& ~PATHSPEC_LITERAL
)
675 if (spec
->nr
&& (spec
->items
[0].magic
& ~PATHSPEC_LITERAL
))
681 static void prepare_to_use_bloom_filter(struct rev_info
*revs
)
683 struct pathspec_item
*pi
;
684 char *path_alloc
= NULL
;
685 const char *path
, *p
;
687 int path_component_nr
= 1;
692 if (forbid_bloom_filters(&revs
->prune_data
))
695 repo_parse_commit(revs
->repo
, revs
->commits
->item
);
697 revs
->bloom_filter_settings
= get_bloom_filter_settings(revs
->repo
);
698 if (!revs
->bloom_filter_settings
)
701 if (!revs
->pruning
.pathspec
.nr
)
704 pi
= &revs
->pruning
.pathspec
.items
[0];
706 /* remove single trailing slash from path, if needed */
707 if (pi
->len
> 0 && pi
->match
[pi
->len
- 1] == '/') {
708 path_alloc
= xmemdupz(pi
->match
, pi
->len
- 1);
715 revs
->bloom_filter_settings
= NULL
;
723 * At this point, the path is normalized to use Unix-style
724 * path separators. This is required due to how the
725 * changed-path Bloom filters store the paths.
732 revs
->bloom_keys_nr
= path_component_nr
;
733 ALLOC_ARRAY(revs
->bloom_keys
, revs
->bloom_keys_nr
);
735 fill_bloom_key(path
, len
, &revs
->bloom_keys
[0],
736 revs
->bloom_filter_settings
);
737 path_component_nr
= 1;
742 fill_bloom_key(path
, p
- path
,
743 &revs
->bloom_keys
[path_component_nr
++],
744 revs
->bloom_filter_settings
);
748 if (trace2_is_enabled() && !bloom_filter_atexit_registered
) {
749 atexit(trace2_bloom_filter_statistics_atexit
);
750 bloom_filter_atexit_registered
= 1;
756 static int check_maybe_different_in_bloom_filter(struct rev_info
*revs
,
757 struct commit
*commit
)
759 struct bloom_filter
*filter
;
762 if (!revs
->repo
->objects
->commit_graph
)
765 if (commit_graph_generation(commit
) == GENERATION_NUMBER_INFINITY
)
768 filter
= get_bloom_filter(revs
->repo
, commit
);
771 count_bloom_filter_not_present
++;
775 for (j
= 0; result
&& j
< revs
->bloom_keys_nr
; j
++) {
776 result
= bloom_filter_contains(filter
,
777 &revs
->bloom_keys
[j
],
778 revs
->bloom_filter_settings
);
782 count_bloom_filter_maybe
++;
784 count_bloom_filter_definitely_not
++;
789 static int rev_compare_tree(struct rev_info
*revs
,
790 struct commit
*parent
, struct commit
*commit
, int nth_parent
)
792 struct tree
*t1
= repo_get_commit_tree(the_repository
, parent
);
793 struct tree
*t2
= repo_get_commit_tree(the_repository
, commit
);
801 if (revs
->simplify_by_decoration
) {
803 * If we are simplifying by decoration, then the commit
804 * is worth showing if it has a tag pointing at it.
806 if (get_name_decoration(&commit
->object
))
807 return REV_TREE_DIFFERENT
;
809 * A commit that is not pointed by a tag is uninteresting
810 * if we are not limited by path. This means that you will
811 * see the usual "commits that touch the paths" plus any
812 * tagged commit by specifying both --simplify-by-decoration
815 if (!revs
->prune_data
.nr
)
816 return REV_TREE_SAME
;
819 if (revs
->bloom_keys_nr
&& !nth_parent
) {
820 bloom_ret
= check_maybe_different_in_bloom_filter(revs
, commit
);
823 return REV_TREE_SAME
;
826 tree_difference
= REV_TREE_SAME
;
827 revs
->pruning
.flags
.has_changes
= 0;
828 diff_tree_oid(&t1
->object
.oid
, &t2
->object
.oid
, "", &revs
->pruning
);
831 if (bloom_ret
== 1 && tree_difference
== REV_TREE_SAME
)
832 count_bloom_filter_false_positive
++;
834 return tree_difference
;
837 static int rev_same_tree_as_empty(struct rev_info
*revs
, struct commit
*commit
)
839 struct tree
*t1
= repo_get_commit_tree(the_repository
, commit
);
844 tree_difference
= REV_TREE_SAME
;
845 revs
->pruning
.flags
.has_changes
= 0;
846 diff_tree_oid(NULL
, &t1
->object
.oid
, "", &revs
->pruning
);
848 return tree_difference
== REV_TREE_SAME
;
851 struct treesame_state
{
852 unsigned int nparents
;
853 unsigned char treesame
[FLEX_ARRAY
];
856 static struct treesame_state
*initialise_treesame(struct rev_info
*revs
, struct commit
*commit
)
858 unsigned n
= commit_list_count(commit
->parents
);
859 struct treesame_state
*st
= xcalloc(1, st_add(sizeof(*st
), n
));
861 add_decoration(&revs
->treesame
, &commit
->object
, st
);
866 * Must be called immediately after removing the nth_parent from a commit's
867 * parent list, if we are maintaining the per-parent treesame[] decoration.
868 * This does not recalculate the master TREESAME flag - update_treesame()
869 * should be called to update it after a sequence of treesame[] modifications
870 * that may have affected it.
872 static int compact_treesame(struct rev_info
*revs
, struct commit
*commit
, unsigned nth_parent
)
874 struct treesame_state
*st
;
877 if (!commit
->parents
) {
879 * Have just removed the only parent from a non-merge.
880 * Different handling, as we lack decoration.
883 die("compact_treesame %u", nth_parent
);
884 old_same
= !!(commit
->object
.flags
& TREESAME
);
885 if (rev_same_tree_as_empty(revs
, commit
))
886 commit
->object
.flags
|= TREESAME
;
888 commit
->object
.flags
&= ~TREESAME
;
892 st
= lookup_decoration(&revs
->treesame
, &commit
->object
);
893 if (!st
|| nth_parent
>= st
->nparents
)
894 die("compact_treesame %u", nth_parent
);
896 old_same
= st
->treesame
[nth_parent
];
897 memmove(st
->treesame
+ nth_parent
,
898 st
->treesame
+ nth_parent
+ 1,
899 st
->nparents
- nth_parent
- 1);
902 * If we've just become a non-merge commit, update TREESAME
903 * immediately, and remove the no-longer-needed decoration.
904 * If still a merge, defer update until update_treesame().
906 if (--st
->nparents
== 1) {
907 if (commit
->parents
->next
)
908 die("compact_treesame parents mismatch");
909 if (st
->treesame
[0] && revs
->dense
)
910 commit
->object
.flags
|= TREESAME
;
912 commit
->object
.flags
&= ~TREESAME
;
913 free(add_decoration(&revs
->treesame
, &commit
->object
, NULL
));
919 static unsigned update_treesame(struct rev_info
*revs
, struct commit
*commit
)
921 if (commit
->parents
&& commit
->parents
->next
) {
923 struct treesame_state
*st
;
924 struct commit_list
*p
;
925 unsigned relevant_parents
;
926 unsigned relevant_change
, irrelevant_change
;
928 st
= lookup_decoration(&revs
->treesame
, &commit
->object
);
930 die("update_treesame %s", oid_to_hex(&commit
->object
.oid
));
931 relevant_parents
= 0;
932 relevant_change
= irrelevant_change
= 0;
933 for (p
= commit
->parents
, n
= 0; p
; n
++, p
= p
->next
) {
934 if (relevant_commit(p
->item
)) {
935 relevant_change
|= !st
->treesame
[n
];
938 irrelevant_change
|= !st
->treesame
[n
];
940 if (relevant_parents
? relevant_change
: irrelevant_change
)
941 commit
->object
.flags
&= ~TREESAME
;
943 commit
->object
.flags
|= TREESAME
;
946 return commit
->object
.flags
& TREESAME
;
949 static inline int limiting_can_increase_treesame(const struct rev_info
*revs
)
952 * TREESAME is irrelevant unless prune && dense;
953 * if simplify_history is set, we can't have a mixture of TREESAME and
954 * !TREESAME INTERESTING parents (and we don't have treesame[]
955 * decoration anyway);
956 * if first_parent_only is set, then the TREESAME flag is locked
957 * against the first parent (and again we lack treesame[] decoration).
959 return revs
->prune
&& revs
->dense
&&
960 !revs
->simplify_history
&&
961 !revs
->first_parent_only
;
964 static void try_to_simplify_commit(struct rev_info
*revs
, struct commit
*commit
)
966 struct commit_list
**pp
, *parent
;
967 struct treesame_state
*ts
= NULL
;
968 int relevant_change
= 0, irrelevant_change
= 0;
969 int relevant_parents
, nth_parent
;
972 * If we don't do pruning, everything is interesting
977 if (!repo_get_commit_tree(the_repository
, commit
))
980 if (!commit
->parents
) {
981 if (rev_same_tree_as_empty(revs
, commit
))
982 commit
->object
.flags
|= TREESAME
;
987 * Normal non-merge commit? If we don't want to make the
988 * history dense, we consider it always to be a change..
990 if (!revs
->dense
&& !commit
->parents
->next
)
993 for (pp
= &commit
->parents
, nth_parent
= 0, relevant_parents
= 0;
994 (parent
= *pp
) != NULL
;
995 pp
= &parent
->next
, nth_parent
++) {
996 struct commit
*p
= parent
->item
;
997 if (relevant_commit(p
))
1000 if (nth_parent
== 1) {
1002 * This our second loop iteration - so we now know
1003 * we're dealing with a merge.
1005 * Do not compare with later parents when we care only about
1006 * the first parent chain, in order to avoid derailing the
1007 * traversal to follow a side branch that brought everything
1008 * in the path we are limited to by the pathspec.
1010 if (revs
->first_parent_only
)
1013 * If this will remain a potentially-simplifiable
1014 * merge, remember per-parent treesame if needed.
1015 * Initialise the array with the comparison from our
1018 if (revs
->treesame
.name
&&
1019 !revs
->simplify_history
&&
1020 !(commit
->object
.flags
& UNINTERESTING
)) {
1021 ts
= initialise_treesame(revs
, commit
);
1022 if (!(irrelevant_change
|| relevant_change
))
1023 ts
->treesame
[0] = 1;
1026 if (repo_parse_commit(revs
->repo
, p
) < 0)
1027 die("cannot simplify commit %s (because of %s)",
1028 oid_to_hex(&commit
->object
.oid
),
1029 oid_to_hex(&p
->object
.oid
));
1030 switch (rev_compare_tree(revs
, p
, commit
, nth_parent
)) {
1032 if (!revs
->simplify_history
|| !relevant_commit(p
)) {
1033 /* Even if a merge with an uninteresting
1034 * side branch brought the entire change
1035 * we are interested in, we do not want
1036 * to lose the other branches of this
1037 * merge, so we just keep going.
1040 ts
->treesame
[nth_parent
] = 1;
1043 parent
->next
= NULL
;
1044 commit
->parents
= parent
;
1047 * A merge commit is a "diversion" if it is not
1048 * TREESAME to its first parent but is TREESAME
1049 * to a later parent. In the simplified history,
1050 * we "divert" the history walk to the later
1051 * parent. These commits are shown when "show_pulls"
1052 * is enabled, so do not mark the object as
1055 if (!revs
->show_pulls
|| !nth_parent
)
1056 commit
->object
.flags
|= TREESAME
;
1061 if (revs
->remove_empty_trees
&&
1062 rev_same_tree_as_empty(revs
, p
)) {
1063 /* We are adding all the specified
1064 * paths from this parent, so the
1065 * history beyond this parent is not
1066 * interesting. Remove its parents
1067 * (they are grandparents for us).
1068 * IOW, we pretend this parent is a
1071 if (repo_parse_commit(revs
->repo
, p
) < 0)
1072 die("cannot simplify commit %s (invalid %s)",
1073 oid_to_hex(&commit
->object
.oid
),
1074 oid_to_hex(&p
->object
.oid
));
1079 case REV_TREE_DIFFERENT
:
1080 if (relevant_commit(p
))
1081 relevant_change
= 1;
1083 irrelevant_change
= 1;
1086 commit
->object
.flags
|= PULL_MERGE
;
1090 die("bad tree compare for commit %s", oid_to_hex(&commit
->object
.oid
));
1094 * TREESAME is straightforward for single-parent commits. For merge
1095 * commits, it is most useful to define it so that "irrelevant"
1096 * parents cannot make us !TREESAME - if we have any relevant
1097 * parents, then we only consider TREESAMEness with respect to them,
1098 * allowing irrelevant merges from uninteresting branches to be
1099 * simplified away. Only if we have only irrelevant parents do we
1100 * base TREESAME on them. Note that this logic is replicated in
1101 * update_treesame, which should be kept in sync.
1103 if (relevant_parents
? !relevant_change
: !irrelevant_change
)
1104 commit
->object
.flags
|= TREESAME
;
1107 static int process_parents(struct rev_info
*revs
, struct commit
*commit
,
1108 struct commit_list
**list
, struct prio_queue
*queue
)
1110 struct commit_list
*parent
= commit
->parents
;
1111 unsigned pass_flags
;
1113 if (commit
->object
.flags
& ADDED
)
1115 commit
->object
.flags
|= ADDED
;
1117 if (revs
->include_check
&&
1118 !revs
->include_check(commit
, revs
->include_check_data
))
1122 * If the commit is uninteresting, don't try to
1123 * prune parents - we want the maximal uninteresting
1126 * Normally we haven't parsed the parent
1127 * yet, so we won't have a parent of a parent
1128 * here. However, it may turn out that we've
1129 * reached this commit some other way (where it
1130 * wasn't uninteresting), in which case we need
1131 * to mark its parents recursively too..
1133 if (commit
->object
.flags
& UNINTERESTING
) {
1135 struct commit
*p
= parent
->item
;
1136 parent
= parent
->next
;
1138 p
->object
.flags
|= UNINTERESTING
;
1139 if (repo_parse_commit_gently(revs
->repo
, p
, 1) < 0)
1142 mark_parents_uninteresting(revs
, p
);
1143 if (p
->object
.flags
& SEEN
)
1145 p
->object
.flags
|= (SEEN
| NOT_USER_GIVEN
);
1147 commit_list_insert_by_date(p
, list
);
1149 prio_queue_put(queue
, p
);
1150 if (revs
->exclude_first_parent_only
)
1157 * Ok, the commit wasn't uninteresting. Try to
1158 * simplify the commit history and find the parent
1159 * that has no differences in the path set if one exists.
1161 try_to_simplify_commit(revs
, commit
);
1166 pass_flags
= (commit
->object
.flags
& (SYMMETRIC_LEFT
| ANCESTRY_PATH
));
1168 for (parent
= commit
->parents
; parent
; parent
= parent
->next
) {
1169 struct commit
*p
= parent
->item
;
1170 int gently
= revs
->ignore_missing_links
||
1171 revs
->exclude_promisor_objects
;
1172 if (repo_parse_commit_gently(revs
->repo
, p
, gently
) < 0) {
1173 if (revs
->exclude_promisor_objects
&&
1174 is_promisor_object(&p
->object
.oid
)) {
1175 if (revs
->first_parent_only
)
1181 if (revs
->sources
) {
1182 char **slot
= revision_sources_at(revs
->sources
, p
);
1185 *slot
= *revision_sources_at(revs
->sources
, commit
);
1187 p
->object
.flags
|= pass_flags
;
1188 if (!(p
->object
.flags
& SEEN
)) {
1189 p
->object
.flags
|= (SEEN
| NOT_USER_GIVEN
);
1191 commit_list_insert_by_date(p
, list
);
1193 prio_queue_put(queue
, p
);
1195 if (revs
->first_parent_only
)
1201 static void cherry_pick_list(struct commit_list
*list
, struct rev_info
*revs
)
1203 struct commit_list
*p
;
1204 int left_count
= 0, right_count
= 0;
1206 struct patch_ids ids
;
1207 unsigned cherry_flag
;
1209 /* First count the commits on the left and on the right */
1210 for (p
= list
; p
; p
= p
->next
) {
1211 struct commit
*commit
= p
->item
;
1212 unsigned flags
= commit
->object
.flags
;
1213 if (flags
& BOUNDARY
)
1215 else if (flags
& SYMMETRIC_LEFT
)
1221 if (!left_count
|| !right_count
)
1224 left_first
= left_count
< right_count
;
1225 init_patch_ids(revs
->repo
, &ids
);
1226 ids
.diffopts
.pathspec
= revs
->diffopt
.pathspec
;
1228 /* Compute patch-ids for one side */
1229 for (p
= list
; p
; p
= p
->next
) {
1230 struct commit
*commit
= p
->item
;
1231 unsigned flags
= commit
->object
.flags
;
1233 if (flags
& BOUNDARY
)
1236 * If we have fewer left, left_first is set and we omit
1237 * commits on the right branch in this loop. If we have
1238 * fewer right, we skip the left ones.
1240 if (left_first
!= !!(flags
& SYMMETRIC_LEFT
))
1242 add_commit_patch_id(commit
, &ids
);
1245 /* either cherry_mark or cherry_pick are true */
1246 cherry_flag
= revs
->cherry_mark
? PATCHSAME
: SHOWN
;
1248 /* Check the other side */
1249 for (p
= list
; p
; p
= p
->next
) {
1250 struct commit
*commit
= p
->item
;
1251 struct patch_id
*id
;
1252 unsigned flags
= commit
->object
.flags
;
1254 if (flags
& BOUNDARY
)
1257 * If we have fewer left, left_first is set and we omit
1258 * commits on the left branch in this loop.
1260 if (left_first
== !!(flags
& SYMMETRIC_LEFT
))
1264 * Have we seen the same patch id?
1266 id
= patch_id_iter_first(commit
, &ids
);
1270 commit
->object
.flags
|= cherry_flag
;
1272 id
->commit
->object
.flags
|= cherry_flag
;
1273 } while ((id
= patch_id_iter_next(id
, &ids
)));
1276 free_patch_ids(&ids
);
1279 /* How many extra uninteresting commits we want to see.. */
1282 static int still_interesting(struct commit_list
*src
, timestamp_t date
, int slop
,
1283 struct commit
**interesting_cache
)
1286 * No source list at all? We're definitely done..
1292 * Does the destination list contain entries with a date
1293 * before the source list? Definitely _not_ done.
1295 if (date
<= src
->item
->date
)
1299 * Does the source list still have interesting commits in
1300 * it? Definitely not done..
1302 if (!everybody_uninteresting(src
, interesting_cache
))
1305 /* Ok, we're closing in.. */
1310 * "rev-list --ancestry-path=C_0 [--ancestry-path=C_1 ...] A..B"
1311 * computes commits that are ancestors of B but not ancestors of A but
1312 * further limits the result to those that have any of C in their
1313 * ancestry path (i.e. are either ancestors of any of C, descendants
1314 * of any of C, or are any of C). If --ancestry-path is specified with
1315 * no commit, we use all bottom commits for C.
1317 * Before this function is called, ancestors of C will have already
1318 * been marked with ANCESTRY_PATH previously.
1320 * This takes the list of bottom commits and the result of "A..B"
1321 * without --ancestry-path, and limits the latter further to the ones
1322 * that have any of C in their ancestry path. Since the ancestors of C
1323 * have already been marked (a prerequisite of this function), we just
1324 * need to mark the descendants, then exclude any commit that does not
1325 * have any of these marks.
1327 static void limit_to_ancestry(struct commit_list
*bottoms
, struct commit_list
*list
)
1329 struct commit_list
*p
;
1330 struct commit_list
*rlist
= NULL
;
1334 * Reverse the list so that it will be likely that we would
1335 * process parents before children.
1337 for (p
= list
; p
; p
= p
->next
)
1338 commit_list_insert(p
->item
, &rlist
);
1340 for (p
= bottoms
; p
; p
= p
->next
)
1341 p
->item
->object
.flags
|= TMP_MARK
;
1344 * Mark the ones that can reach bottom commits in "list",
1345 * in a bottom-up fashion.
1349 for (p
= rlist
; p
; p
= p
->next
) {
1350 struct commit
*c
= p
->item
;
1351 struct commit_list
*parents
;
1352 if (c
->object
.flags
& (TMP_MARK
| UNINTERESTING
))
1354 for (parents
= c
->parents
;
1356 parents
= parents
->next
) {
1357 if (!(parents
->item
->object
.flags
& TMP_MARK
))
1359 c
->object
.flags
|= TMP_MARK
;
1364 } while (made_progress
);
1367 * NEEDSWORK: decide if we want to remove parents that are
1368 * not marked with TMP_MARK from commit->parents for commits
1369 * in the resulting list. We may not want to do that, though.
1373 * The ones that are not marked with either TMP_MARK or
1374 * ANCESTRY_PATH are uninteresting
1376 for (p
= list
; p
; p
= p
->next
) {
1377 struct commit
*c
= p
->item
;
1378 if (c
->object
.flags
& (TMP_MARK
| ANCESTRY_PATH
))
1380 c
->object
.flags
|= UNINTERESTING
;
1383 /* We are done with TMP_MARK and ANCESTRY_PATH */
1384 for (p
= list
; p
; p
= p
->next
)
1385 p
->item
->object
.flags
&= ~(TMP_MARK
| ANCESTRY_PATH
);
1386 for (p
= bottoms
; p
; p
= p
->next
)
1387 p
->item
->object
.flags
&= ~(TMP_MARK
| ANCESTRY_PATH
);
1388 free_commit_list(rlist
);
1392 * Before walking the history, add the set of "negative" refs the
1393 * caller has asked to exclude to the bottom list.
1395 * This is used to compute "rev-list --ancestry-path A..B", as we need
1396 * to filter the result of "A..B" further to the ones that can actually
1399 static void collect_bottom_commits(struct commit_list
*list
,
1400 struct commit_list
**bottom
)
1402 struct commit_list
*elem
;
1403 for (elem
= list
; elem
; elem
= elem
->next
)
1404 if (elem
->item
->object
.flags
& BOTTOM
)
1405 commit_list_insert(elem
->item
, bottom
);
1408 /* Assumes either left_only or right_only is set */
1409 static void limit_left_right(struct commit_list
*list
, struct rev_info
*revs
)
1411 struct commit_list
*p
;
1413 for (p
= list
; p
; p
= p
->next
) {
1414 struct commit
*commit
= p
->item
;
1416 if (revs
->right_only
) {
1417 if (commit
->object
.flags
& SYMMETRIC_LEFT
)
1418 commit
->object
.flags
|= SHOWN
;
1419 } else /* revs->left_only is set */
1420 if (!(commit
->object
.flags
& SYMMETRIC_LEFT
))
1421 commit
->object
.flags
|= SHOWN
;
1425 static int limit_list(struct rev_info
*revs
)
1428 timestamp_t date
= TIME_MAX
;
1429 struct commit_list
*original_list
= revs
->commits
;
1430 struct commit_list
*newlist
= NULL
;
1431 struct commit_list
**p
= &newlist
;
1432 struct commit
*interesting_cache
= NULL
;
1434 if (revs
->ancestry_path_implicit_bottoms
) {
1435 collect_bottom_commits(original_list
,
1436 &revs
->ancestry_path_bottoms
);
1437 if (!revs
->ancestry_path_bottoms
)
1438 die("--ancestry-path given but there are no bottom commits");
1441 while (original_list
) {
1442 struct commit
*commit
= pop_commit(&original_list
);
1443 struct object
*obj
= &commit
->object
;
1444 show_early_output_fn_t show
;
1446 if (commit
== interesting_cache
)
1447 interesting_cache
= NULL
;
1449 if (revs
->max_age
!= -1 && (commit
->date
< revs
->max_age
))
1450 obj
->flags
|= UNINTERESTING
;
1451 if (process_parents(revs
, commit
, &original_list
, NULL
) < 0)
1453 if (obj
->flags
& UNINTERESTING
) {
1454 mark_parents_uninteresting(revs
, commit
);
1455 slop
= still_interesting(original_list
, date
, slop
, &interesting_cache
);
1460 if (revs
->min_age
!= -1 && (commit
->date
> revs
->min_age
) &&
1461 !revs
->line_level_traverse
)
1463 if (revs
->max_age_as_filter
!= -1 &&
1464 (commit
->date
< revs
->max_age_as_filter
) && !revs
->line_level_traverse
)
1466 date
= commit
->date
;
1467 p
= &commit_list_insert(commit
, p
)->next
;
1469 show
= show_early_output
;
1473 show(revs
, newlist
);
1474 show_early_output
= NULL
;
1476 if (revs
->cherry_pick
|| revs
->cherry_mark
)
1477 cherry_pick_list(newlist
, revs
);
1479 if (revs
->left_only
|| revs
->right_only
)
1480 limit_left_right(newlist
, revs
);
1482 if (revs
->ancestry_path
)
1483 limit_to_ancestry(revs
->ancestry_path_bottoms
, newlist
);
1486 * Check if any commits have become TREESAME by some of their parents
1487 * becoming UNINTERESTING.
1489 if (limiting_can_increase_treesame(revs
)) {
1490 struct commit_list
*list
= NULL
;
1491 for (list
= newlist
; list
; list
= list
->next
) {
1492 struct commit
*c
= list
->item
;
1493 if (c
->object
.flags
& (UNINTERESTING
| TREESAME
))
1495 update_treesame(revs
, c
);
1499 free_commit_list(original_list
);
1500 revs
->commits
= newlist
;
1505 * Add an entry to refs->cmdline with the specified information.
1508 static void add_rev_cmdline(struct rev_info
*revs
,
1509 struct object
*item
,
1514 struct rev_cmdline_info
*info
= &revs
->cmdline
;
1515 unsigned int nr
= info
->nr
;
1517 ALLOC_GROW(info
->rev
, nr
+ 1, info
->alloc
);
1518 info
->rev
[nr
].item
= item
;
1519 info
->rev
[nr
].name
= xstrdup(name
);
1520 info
->rev
[nr
].whence
= whence
;
1521 info
->rev
[nr
].flags
= flags
;
1525 static void add_rev_cmdline_list(struct rev_info
*revs
,
1526 struct commit_list
*commit_list
,
1530 while (commit_list
) {
1531 struct object
*object
= &commit_list
->item
->object
;
1532 add_rev_cmdline(revs
, object
, oid_to_hex(&object
->oid
),
1534 commit_list
= commit_list
->next
;
1538 int ref_excluded(const struct ref_exclusions
*exclusions
, const char *path
)
1540 const char *stripped_path
= strip_namespace(path
);
1541 struct string_list_item
*item
;
1543 for_each_string_list_item(item
, &exclusions
->excluded_refs
) {
1544 if (!wildmatch(item
->string
, path
, 0))
1548 if (ref_is_hidden(stripped_path
, path
, &exclusions
->hidden_refs
))
1554 void init_ref_exclusions(struct ref_exclusions
*exclusions
)
1556 struct ref_exclusions blank
= REF_EXCLUSIONS_INIT
;
1557 memcpy(exclusions
, &blank
, sizeof(*exclusions
));
1560 void clear_ref_exclusions(struct ref_exclusions
*exclusions
)
1562 string_list_clear(&exclusions
->excluded_refs
, 0);
1563 string_list_clear(&exclusions
->hidden_refs
, 0);
1564 exclusions
->hidden_refs_configured
= 0;
1567 void add_ref_exclusion(struct ref_exclusions
*exclusions
, const char *exclude
)
1569 string_list_append(&exclusions
->excluded_refs
, exclude
);
1572 struct exclude_hidden_refs_cb
{
1573 struct ref_exclusions
*exclusions
;
1574 const char *section
;
1577 static int hide_refs_config(const char *var
, const char *value
, void *cb_data
)
1579 struct exclude_hidden_refs_cb
*cb
= cb_data
;
1580 cb
->exclusions
->hidden_refs_configured
= 1;
1581 return parse_hide_refs_config(var
, value
, cb
->section
,
1582 &cb
->exclusions
->hidden_refs
);
1585 void exclude_hidden_refs(struct ref_exclusions
*exclusions
, const char *section
)
1587 struct exclude_hidden_refs_cb cb
;
1589 if (strcmp(section
, "fetch") && strcmp(section
, "receive") &&
1590 strcmp(section
, "uploadpack"))
1591 die(_("unsupported section for hidden refs: %s"), section
);
1593 if (exclusions
->hidden_refs_configured
)
1594 die(_("--exclude-hidden= passed more than once"));
1596 cb
.exclusions
= exclusions
;
1597 cb
.section
= section
;
1599 git_config(hide_refs_config
, &cb
);
1602 struct all_refs_cb
{
1604 int warned_bad_reflog
;
1605 struct rev_info
*all_revs
;
1606 const char *name_for_errormsg
;
1607 struct worktree
*wt
;
1610 static int handle_one_ref(const char *path
, const struct object_id
*oid
,
1614 struct all_refs_cb
*cb
= cb_data
;
1615 struct object
*object
;
1617 if (ref_excluded(&cb
->all_revs
->ref_excludes
, path
))
1620 object
= get_reference(cb
->all_revs
, path
, oid
, cb
->all_flags
);
1621 add_rev_cmdline(cb
->all_revs
, object
, path
, REV_CMD_REF
, cb
->all_flags
);
1622 add_pending_object(cb
->all_revs
, object
, path
);
1626 static void init_all_refs_cb(struct all_refs_cb
*cb
, struct rev_info
*revs
,
1629 cb
->all_revs
= revs
;
1630 cb
->all_flags
= flags
;
1631 revs
->rev_input_given
= 1;
1635 static void handle_refs(struct ref_store
*refs
,
1636 struct rev_info
*revs
, unsigned flags
,
1637 int (*for_each
)(struct ref_store
*, each_ref_fn
, void *))
1639 struct all_refs_cb cb
;
1642 /* this could happen with uninitialized submodules */
1646 init_all_refs_cb(&cb
, revs
, flags
);
1647 for_each(refs
, handle_one_ref
, &cb
);
1650 static void handle_one_reflog_commit(struct object_id
*oid
, void *cb_data
)
1652 struct all_refs_cb
*cb
= cb_data
;
1653 if (!is_null_oid(oid
)) {
1654 struct object
*o
= parse_object(cb
->all_revs
->repo
, oid
);
1656 o
->flags
|= cb
->all_flags
;
1657 /* ??? CMDLINEFLAGS ??? */
1658 add_pending_object(cb
->all_revs
, o
, "");
1660 else if (!cb
->warned_bad_reflog
) {
1661 warning("reflog of '%s' references pruned commits",
1662 cb
->name_for_errormsg
);
1663 cb
->warned_bad_reflog
= 1;
1668 static int handle_one_reflog_ent(struct object_id
*ooid
, struct object_id
*noid
,
1669 const char *email UNUSED
,
1670 timestamp_t timestamp UNUSED
,
1672 const char *message UNUSED
,
1675 handle_one_reflog_commit(ooid
, cb_data
);
1676 handle_one_reflog_commit(noid
, cb_data
);
1680 static int handle_one_reflog(const char *refname_in_wt
,
1681 const struct object_id
*oid UNUSED
,
1682 int flag UNUSED
, void *cb_data
)
1684 struct all_refs_cb
*cb
= cb_data
;
1685 struct strbuf refname
= STRBUF_INIT
;
1687 cb
->warned_bad_reflog
= 0;
1688 strbuf_worktree_ref(cb
->wt
, &refname
, refname_in_wt
);
1689 cb
->name_for_errormsg
= refname
.buf
;
1690 refs_for_each_reflog_ent(get_main_ref_store(the_repository
),
1692 handle_one_reflog_ent
, cb_data
);
1693 strbuf_release(&refname
);
1697 static void add_other_reflogs_to_pending(struct all_refs_cb
*cb
)
1699 struct worktree
**worktrees
, **p
;
1701 worktrees
= get_worktrees();
1702 for (p
= worktrees
; *p
; p
++) {
1703 struct worktree
*wt
= *p
;
1709 refs_for_each_reflog(get_worktree_ref_store(wt
),
1713 free_worktrees(worktrees
);
1716 void add_reflogs_to_pending(struct rev_info
*revs
, unsigned flags
)
1718 struct all_refs_cb cb
;
1721 cb
.all_flags
= flags
;
1723 for_each_reflog(handle_one_reflog
, &cb
);
1725 if (!revs
->single_worktree
)
1726 add_other_reflogs_to_pending(&cb
);
1729 static void add_cache_tree(struct cache_tree
*it
, struct rev_info
*revs
,
1730 struct strbuf
*path
, unsigned int flags
)
1732 size_t baselen
= path
->len
;
1735 if (it
->entry_count
>= 0) {
1736 struct tree
*tree
= lookup_tree(revs
->repo
, &it
->oid
);
1737 tree
->object
.flags
|= flags
;
1738 add_pending_object_with_path(revs
, &tree
->object
, "",
1742 for (i
= 0; i
< it
->subtree_nr
; i
++) {
1743 struct cache_tree_sub
*sub
= it
->down
[i
];
1744 strbuf_addf(path
, "%s%s", baselen
? "/" : "", sub
->name
);
1745 add_cache_tree(sub
->cache_tree
, revs
, path
, flags
);
1746 strbuf_setlen(path
, baselen
);
1751 static void add_resolve_undo_to_pending(struct index_state
*istate
, struct rev_info
*revs
)
1753 struct string_list_item
*item
;
1754 struct string_list
*resolve_undo
= istate
->resolve_undo
;
1759 for_each_string_list_item(item
, resolve_undo
) {
1760 const char *path
= item
->string
;
1761 struct resolve_undo_info
*ru
= item
->util
;
1766 for (i
= 0; i
< 3; i
++) {
1769 if (!ru
->mode
[i
] || !S_ISREG(ru
->mode
[i
]))
1772 blob
= lookup_blob(revs
->repo
, &ru
->oid
[i
]);
1774 warning(_("resolve-undo records `%s` which is missing"),
1775 oid_to_hex(&ru
->oid
[i
]));
1778 add_pending_object_with_path(revs
, &blob
->object
, "",
1784 static void do_add_index_objects_to_pending(struct rev_info
*revs
,
1785 struct index_state
*istate
,
1790 /* TODO: audit for interaction with sparse-index. */
1791 ensure_full_index(istate
);
1792 for (i
= 0; i
< istate
->cache_nr
; i
++) {
1793 struct cache_entry
*ce
= istate
->cache
[i
];
1796 if (S_ISGITLINK(ce
->ce_mode
))
1799 blob
= lookup_blob(revs
->repo
, &ce
->oid
);
1801 die("unable to add index blob to traversal");
1802 blob
->object
.flags
|= flags
;
1803 add_pending_object_with_path(revs
, &blob
->object
, "",
1804 ce
->ce_mode
, ce
->name
);
1807 if (istate
->cache_tree
) {
1808 struct strbuf path
= STRBUF_INIT
;
1809 add_cache_tree(istate
->cache_tree
, revs
, &path
, flags
);
1810 strbuf_release(&path
);
1813 add_resolve_undo_to_pending(istate
, revs
);
1816 void add_index_objects_to_pending(struct rev_info
*revs
, unsigned int flags
)
1818 struct worktree
**worktrees
, **p
;
1820 repo_read_index(revs
->repo
);
1821 do_add_index_objects_to_pending(revs
, revs
->repo
->index
, flags
);
1823 if (revs
->single_worktree
)
1826 worktrees
= get_worktrees();
1827 for (p
= worktrees
; *p
; p
++) {
1828 struct worktree
*wt
= *p
;
1829 struct index_state istate
= INDEX_STATE_INIT(revs
->repo
);
1832 continue; /* current index already taken care of */
1834 if (read_index_from(&istate
,
1835 worktree_git_path(wt
, "index"),
1836 get_worktree_git_dir(wt
)) > 0)
1837 do_add_index_objects_to_pending(revs
, &istate
, flags
);
1838 discard_index(&istate
);
1840 free_worktrees(worktrees
);
1843 struct add_alternate_refs_data
{
1844 struct rev_info
*revs
;
1848 static void add_one_alternate_ref(const struct object_id
*oid
,
1851 const char *name
= ".alternate";
1852 struct add_alternate_refs_data
*data
= vdata
;
1855 obj
= get_reference(data
->revs
, name
, oid
, data
->flags
);
1856 add_rev_cmdline(data
->revs
, obj
, name
, REV_CMD_REV
, data
->flags
);
1857 add_pending_object(data
->revs
, obj
, name
);
1860 static void add_alternate_refs_to_pending(struct rev_info
*revs
,
1863 struct add_alternate_refs_data data
;
1866 for_each_alternate_ref(add_one_alternate_ref
, &data
);
1869 static int add_parents_only(struct rev_info
*revs
, const char *arg_
, int flags
,
1872 struct object_id oid
;
1874 struct commit
*commit
;
1875 struct commit_list
*parents
;
1877 const char *arg
= arg_
;
1880 flags
^= UNINTERESTING
| BOTTOM
;
1883 if (repo_get_oid_committish(the_repository
, arg
, &oid
))
1886 it
= get_reference(revs
, arg
, &oid
, 0);
1887 if (!it
&& revs
->ignore_missing
)
1889 if (it
->type
!= OBJ_TAG
)
1891 if (!((struct tag
*)it
)->tagged
)
1893 oidcpy(&oid
, &((struct tag
*)it
)->tagged
->oid
);
1895 if (it
->type
!= OBJ_COMMIT
)
1897 commit
= (struct commit
*)it
;
1898 if (exclude_parent
&&
1899 exclude_parent
> commit_list_count(commit
->parents
))
1901 for (parents
= commit
->parents
, parent_number
= 1;
1903 parents
= parents
->next
, parent_number
++) {
1904 if (exclude_parent
&& parent_number
!= exclude_parent
)
1907 it
= &parents
->item
->object
;
1909 add_rev_cmdline(revs
, it
, arg_
, REV_CMD_PARENTS_ONLY
, flags
);
1910 add_pending_object(revs
, it
, arg
);
1915 void repo_init_revisions(struct repository
*r
,
1916 struct rev_info
*revs
,
1919 struct rev_info blank
= REV_INFO_INIT
;
1920 memcpy(revs
, &blank
, sizeof(*revs
));
1923 revs
->pruning
.repo
= r
;
1924 revs
->pruning
.add_remove
= file_add_remove
;
1925 revs
->pruning
.change
= file_change
;
1926 revs
->pruning
.change_fn_data
= revs
;
1927 revs
->prefix
= prefix
;
1929 grep_init(&revs
->grep_filter
, revs
->repo
);
1930 revs
->grep_filter
.status_only
= 1;
1932 repo_diff_setup(revs
->repo
, &revs
->diffopt
);
1933 if (prefix
&& !revs
->diffopt
.prefix
) {
1934 revs
->diffopt
.prefix
= prefix
;
1935 revs
->diffopt
.prefix_length
= strlen(prefix
);
1938 init_display_notes(&revs
->notes_opt
);
1939 list_objects_filter_init(&revs
->filter
);
1940 init_ref_exclusions(&revs
->ref_excludes
);
1943 static void add_pending_commit_list(struct rev_info
*revs
,
1944 struct commit_list
*commit_list
,
1947 while (commit_list
) {
1948 struct object
*object
= &commit_list
->item
->object
;
1949 object
->flags
|= flags
;
1950 add_pending_object(revs
, object
, oid_to_hex(&object
->oid
));
1951 commit_list
= commit_list
->next
;
1955 static void prepare_show_merge(struct rev_info
*revs
)
1957 struct commit_list
*bases
;
1958 struct commit
*head
, *other
;
1959 struct object_id oid
;
1960 const char **prune
= NULL
;
1961 int i
, prune_num
= 1; /* counting terminating NULL */
1962 struct index_state
*istate
= revs
->repo
->index
;
1964 if (repo_get_oid(the_repository
, "HEAD", &oid
))
1965 die("--merge without HEAD?");
1966 head
= lookup_commit_or_die(&oid
, "HEAD");
1967 if (repo_get_oid(the_repository
, "MERGE_HEAD", &oid
))
1968 die("--merge without MERGE_HEAD?");
1969 other
= lookup_commit_or_die(&oid
, "MERGE_HEAD");
1970 add_pending_object(revs
, &head
->object
, "HEAD");
1971 add_pending_object(revs
, &other
->object
, "MERGE_HEAD");
1972 bases
= repo_get_merge_bases(the_repository
, head
, other
);
1973 add_rev_cmdline_list(revs
, bases
, REV_CMD_MERGE_BASE
, UNINTERESTING
| BOTTOM
);
1974 add_pending_commit_list(revs
, bases
, UNINTERESTING
| BOTTOM
);
1975 free_commit_list(bases
);
1976 head
->object
.flags
|= SYMMETRIC_LEFT
;
1978 if (!istate
->cache_nr
)
1979 repo_read_index(revs
->repo
);
1980 for (i
= 0; i
< istate
->cache_nr
; i
++) {
1981 const struct cache_entry
*ce
= istate
->cache
[i
];
1984 if (ce_path_match(istate
, ce
, &revs
->prune_data
, NULL
)) {
1986 REALLOC_ARRAY(prune
, prune_num
);
1987 prune
[prune_num
-2] = ce
->name
;
1988 prune
[prune_num
-1] = NULL
;
1990 while ((i
+1 < istate
->cache_nr
) &&
1991 ce_same_name(ce
, istate
->cache
[i
+1]))
1994 clear_pathspec(&revs
->prune_data
);
1995 parse_pathspec(&revs
->prune_data
, PATHSPEC_ALL_MAGIC
& ~PATHSPEC_LITERAL
,
1996 PATHSPEC_PREFER_FULL
| PATHSPEC_LITERAL_PATH
, "", prune
);
2000 static int dotdot_missing(const char *arg
, char *dotdot
,
2001 struct rev_info
*revs
, int symmetric
)
2003 if (revs
->ignore_missing
)
2005 /* de-munge so we report the full argument */
2008 ? "Invalid symmetric difference expression %s"
2009 : "Invalid revision range %s", arg
);
2012 static int handle_dotdot_1(const char *arg
, char *dotdot
,
2013 struct rev_info
*revs
, int flags
,
2014 int cant_be_filename
,
2015 struct object_context
*a_oc
,
2016 struct object_context
*b_oc
)
2018 const char *a_name
, *b_name
;
2019 struct object_id a_oid
, b_oid
;
2020 struct object
*a_obj
, *b_obj
;
2021 unsigned int a_flags
, b_flags
;
2023 unsigned int flags_exclude
= flags
^ (UNINTERESTING
| BOTTOM
);
2024 unsigned int oc_flags
= GET_OID_COMMITTISH
| GET_OID_RECORD_PATH
;
2030 b_name
= dotdot
+ 2;
2031 if (*b_name
== '.') {
2038 if (get_oid_with_context(revs
->repo
, a_name
, oc_flags
, &a_oid
, a_oc
) ||
2039 get_oid_with_context(revs
->repo
, b_name
, oc_flags
, &b_oid
, b_oc
))
2042 if (!cant_be_filename
) {
2044 verify_non_filename(revs
->prefix
, arg
);
2048 a_obj
= parse_object(revs
->repo
, &a_oid
);
2049 b_obj
= parse_object(revs
->repo
, &b_oid
);
2050 if (!a_obj
|| !b_obj
)
2051 return dotdot_missing(arg
, dotdot
, revs
, symmetric
);
2056 a_flags
= flags_exclude
;
2058 /* A...B -- find merge bases between the two */
2059 struct commit
*a
, *b
;
2060 struct commit_list
*exclude
;
2062 a
= lookup_commit_reference(revs
->repo
, &a_obj
->oid
);
2063 b
= lookup_commit_reference(revs
->repo
, &b_obj
->oid
);
2065 return dotdot_missing(arg
, dotdot
, revs
, symmetric
);
2067 exclude
= repo_get_merge_bases(the_repository
, a
, b
);
2068 add_rev_cmdline_list(revs
, exclude
, REV_CMD_MERGE_BASE
,
2070 add_pending_commit_list(revs
, exclude
, flags_exclude
);
2071 free_commit_list(exclude
);
2074 a_flags
= flags
| SYMMETRIC_LEFT
;
2077 a_obj
->flags
|= a_flags
;
2078 b_obj
->flags
|= b_flags
;
2079 add_rev_cmdline(revs
, a_obj
, a_name
, REV_CMD_LEFT
, a_flags
);
2080 add_rev_cmdline(revs
, b_obj
, b_name
, REV_CMD_RIGHT
, b_flags
);
2081 add_pending_object_with_path(revs
, a_obj
, a_name
, a_oc
->mode
, a_oc
->path
);
2082 add_pending_object_with_path(revs
, b_obj
, b_name
, b_oc
->mode
, b_oc
->path
);
2086 static int handle_dotdot(const char *arg
,
2087 struct rev_info
*revs
, int flags
,
2088 int cant_be_filename
)
2090 struct object_context a_oc
, b_oc
;
2091 char *dotdot
= strstr(arg
, "..");
2097 memset(&a_oc
, 0, sizeof(a_oc
));
2098 memset(&b_oc
, 0, sizeof(b_oc
));
2101 ret
= handle_dotdot_1(arg
, dotdot
, revs
, flags
, cant_be_filename
,
2111 static int handle_revision_arg_1(const char *arg_
, struct rev_info
*revs
, int flags
, unsigned revarg_opt
)
2113 struct object_context oc
;
2115 struct object
*object
;
2116 struct object_id oid
;
2118 const char *arg
= arg_
;
2119 int cant_be_filename
= revarg_opt
& REVARG_CANNOT_BE_FILENAME
;
2120 unsigned get_sha1_flags
= GET_OID_RECORD_PATH
;
2122 flags
= flags
& UNINTERESTING
? flags
| BOTTOM
: flags
& ~BOTTOM
;
2124 if (!cant_be_filename
&& !strcmp(arg
, "..")) {
2126 * Just ".."? That is not a range but the
2127 * pathspec for the parent directory.
2132 if (!handle_dotdot(arg
, revs
, flags
, revarg_opt
))
2135 mark
= strstr(arg
, "^@");
2136 if (mark
&& !mark
[2]) {
2138 if (add_parents_only(revs
, arg
, flags
, 0))
2142 mark
= strstr(arg
, "^!");
2143 if (mark
&& !mark
[2]) {
2145 if (!add_parents_only(revs
, arg
, flags
^ (UNINTERESTING
| BOTTOM
), 0))
2148 mark
= strstr(arg
, "^-");
2150 int exclude_parent
= 1;
2153 if (strtol_i(mark
+ 2, 10, &exclude_parent
) ||
2159 if (!add_parents_only(revs
, arg
, flags
^ (UNINTERESTING
| BOTTOM
), exclude_parent
))
2165 local_flags
= UNINTERESTING
| BOTTOM
;
2169 if (revarg_opt
& REVARG_COMMITTISH
)
2170 get_sha1_flags
|= GET_OID_COMMITTISH
;
2172 if (get_oid_with_context(revs
->repo
, arg
, get_sha1_flags
, &oid
, &oc
))
2173 return revs
->ignore_missing
? 0 : -1;
2174 if (!cant_be_filename
)
2175 verify_non_filename(revs
->prefix
, arg
);
2176 object
= get_reference(revs
, arg
, &oid
, flags
^ local_flags
);
2178 return revs
->ignore_missing
? 0 : -1;
2179 add_rev_cmdline(revs
, object
, arg_
, REV_CMD_REV
, flags
^ local_flags
);
2180 add_pending_object_with_path(revs
, object
, arg
, oc
.mode
, oc
.path
);
2185 int handle_revision_arg(const char *arg
, struct rev_info
*revs
, int flags
, unsigned revarg_opt
)
2187 int ret
= handle_revision_arg_1(arg
, revs
, flags
, revarg_opt
);
2189 revs
->rev_input_given
= 1;
2193 static void read_pathspec_from_stdin(struct strbuf
*sb
,
2194 struct strvec
*prune
)
2196 while (strbuf_getline(sb
, stdin
) != EOF
)
2197 strvec_push(prune
, sb
->buf
);
2200 static void read_revisions_from_stdin(struct rev_info
*revs
,
2201 struct strvec
*prune
)
2204 int seen_dashdash
= 0;
2207 save_warning
= warn_on_object_refname_ambiguity
;
2208 warn_on_object_refname_ambiguity
= 0;
2210 strbuf_init(&sb
, 1000);
2211 while (strbuf_getline(&sb
, stdin
) != EOF
) {
2215 if (sb
.buf
[0] == '-') {
2216 if (len
== 2 && sb
.buf
[1] == '-') {
2220 die("options not supported in --stdin mode");
2222 if (handle_revision_arg(sb
.buf
, revs
, 0,
2223 REVARG_CANNOT_BE_FILENAME
))
2224 die("bad revision '%s'", sb
.buf
);
2227 read_pathspec_from_stdin(&sb
, prune
);
2229 strbuf_release(&sb
);
2230 warn_on_object_refname_ambiguity
= save_warning
;
2233 static void add_grep(struct rev_info
*revs
, const char *ptn
, enum grep_pat_token what
)
2235 append_grep_pattern(&revs
->grep_filter
, ptn
, "command line", 0, what
);
2238 static void add_header_grep(struct rev_info
*revs
, enum grep_header_field field
, const char *pattern
)
2240 append_header_grep_pattern(&revs
->grep_filter
, field
, pattern
);
2243 static void add_message_grep(struct rev_info
*revs
, const char *pattern
)
2245 add_grep(revs
, pattern
, GREP_PATTERN_BODY
);
2248 static int handle_revision_opt(struct rev_info
*revs
, int argc
, const char **argv
,
2249 int *unkc
, const char **unkv
,
2250 const struct setup_revision_opt
* opt
)
2252 const char *arg
= argv
[0];
2253 const char *optarg
= NULL
;
2255 const unsigned hexsz
= the_hash_algo
->hexsz
;
2257 /* pseudo revision arguments */
2258 if (!strcmp(arg
, "--all") || !strcmp(arg
, "--branches") ||
2259 !strcmp(arg
, "--tags") || !strcmp(arg
, "--remotes") ||
2260 !strcmp(arg
, "--reflog") || !strcmp(arg
, "--not") ||
2261 !strcmp(arg
, "--no-walk") || !strcmp(arg
, "--do-walk") ||
2262 !strcmp(arg
, "--bisect") || starts_with(arg
, "--glob=") ||
2263 !strcmp(arg
, "--indexed-objects") ||
2264 !strcmp(arg
, "--alternate-refs") ||
2265 starts_with(arg
, "--exclude=") || starts_with(arg
, "--exclude-hidden=") ||
2266 starts_with(arg
, "--branches=") || starts_with(arg
, "--tags=") ||
2267 starts_with(arg
, "--remotes=") || starts_with(arg
, "--no-walk="))
2269 unkv
[(*unkc
)++] = arg
;
2273 if ((argcount
= parse_long_opt("max-count", argv
, &optarg
))) {
2274 revs
->max_count
= atoi(optarg
);
2277 } else if ((argcount
= parse_long_opt("skip", argv
, &optarg
))) {
2278 revs
->skip_count
= atoi(optarg
);
2280 } else if ((*arg
== '-') && isdigit(arg
[1])) {
2281 /* accept -<digit>, like traditional "head" */
2282 if (strtol_i(arg
+ 1, 10, &revs
->max_count
) < 0 ||
2283 revs
->max_count
< 0)
2284 die("'%s': not a non-negative integer", arg
+ 1);
2286 } else if (!strcmp(arg
, "-n")) {
2288 return error("-n requires an argument");
2289 revs
->max_count
= atoi(argv
[1]);
2292 } else if (skip_prefix(arg
, "-n", &optarg
)) {
2293 revs
->max_count
= atoi(optarg
);
2295 } else if ((argcount
= parse_long_opt("max-age", argv
, &optarg
))) {
2296 revs
->max_age
= atoi(optarg
);
2298 } else if ((argcount
= parse_long_opt("since", argv
, &optarg
))) {
2299 revs
->max_age
= approxidate(optarg
);
2301 } else if ((argcount
= parse_long_opt("since-as-filter", argv
, &optarg
))) {
2302 revs
->max_age_as_filter
= approxidate(optarg
);
2304 } else if ((argcount
= parse_long_opt("after", argv
, &optarg
))) {
2305 revs
->max_age
= approxidate(optarg
);
2307 } else if ((argcount
= parse_long_opt("min-age", argv
, &optarg
))) {
2308 revs
->min_age
= atoi(optarg
);
2310 } else if ((argcount
= parse_long_opt("before", argv
, &optarg
))) {
2311 revs
->min_age
= approxidate(optarg
);
2313 } else if ((argcount
= parse_long_opt("until", argv
, &optarg
))) {
2314 revs
->min_age
= approxidate(optarg
);
2316 } else if (!strcmp(arg
, "--first-parent")) {
2317 revs
->first_parent_only
= 1;
2318 } else if (!strcmp(arg
, "--exclude-first-parent-only")) {
2319 revs
->exclude_first_parent_only
= 1;
2320 } else if (!strcmp(arg
, "--ancestry-path")) {
2321 revs
->ancestry_path
= 1;
2322 revs
->simplify_history
= 0;
2324 revs
->ancestry_path_implicit_bottoms
= 1;
2325 } else if (skip_prefix(arg
, "--ancestry-path=", &optarg
)) {
2327 struct object_id oid
;
2328 const char *msg
= _("could not get commit for ancestry-path argument %s");
2330 revs
->ancestry_path
= 1;
2331 revs
->simplify_history
= 0;
2334 if (repo_get_oid_committish(revs
->repo
, optarg
, &oid
))
2335 return error(msg
, optarg
);
2336 get_reference(revs
, optarg
, &oid
, ANCESTRY_PATH
);
2337 c
= lookup_commit_reference(revs
->repo
, &oid
);
2339 return error(msg
, optarg
);
2340 commit_list_insert(c
, &revs
->ancestry_path_bottoms
);
2341 } else if (!strcmp(arg
, "-g") || !strcmp(arg
, "--walk-reflogs")) {
2342 init_reflog_walk(&revs
->reflog_info
);
2343 } else if (!strcmp(arg
, "--default")) {
2345 return error("bad --default argument");
2346 revs
->def
= argv
[1];
2348 } else if (!strcmp(arg
, "--merge")) {
2349 revs
->show_merge
= 1;
2350 } else if (!strcmp(arg
, "--topo-order")) {
2351 revs
->sort_order
= REV_SORT_IN_GRAPH_ORDER
;
2352 revs
->topo_order
= 1;
2353 } else if (!strcmp(arg
, "--simplify-merges")) {
2354 revs
->simplify_merges
= 1;
2355 revs
->topo_order
= 1;
2356 revs
->rewrite_parents
= 1;
2357 revs
->simplify_history
= 0;
2359 } else if (!strcmp(arg
, "--simplify-by-decoration")) {
2360 revs
->simplify_merges
= 1;
2361 revs
->topo_order
= 1;
2362 revs
->rewrite_parents
= 1;
2363 revs
->simplify_history
= 0;
2364 revs
->simplify_by_decoration
= 1;
2367 } else if (!strcmp(arg
, "--date-order")) {
2368 revs
->sort_order
= REV_SORT_BY_COMMIT_DATE
;
2369 revs
->topo_order
= 1;
2370 } else if (!strcmp(arg
, "--author-date-order")) {
2371 revs
->sort_order
= REV_SORT_BY_AUTHOR_DATE
;
2372 revs
->topo_order
= 1;
2373 } else if (!strcmp(arg
, "--early-output")) {
2374 revs
->early_output
= 100;
2375 revs
->topo_order
= 1;
2376 } else if (skip_prefix(arg
, "--early-output=", &optarg
)) {
2377 if (strtoul_ui(optarg
, 10, &revs
->early_output
) < 0)
2378 die("'%s': not a non-negative integer", optarg
);
2379 revs
->topo_order
= 1;
2380 } else if (!strcmp(arg
, "--parents")) {
2381 revs
->rewrite_parents
= 1;
2382 revs
->print_parents
= 1;
2383 } else if (!strcmp(arg
, "--dense")) {
2385 } else if (!strcmp(arg
, "--sparse")) {
2387 } else if (!strcmp(arg
, "--in-commit-order")) {
2388 revs
->tree_blobs_in_commit_order
= 1;
2389 } else if (!strcmp(arg
, "--remove-empty")) {
2390 revs
->remove_empty_trees
= 1;
2391 } else if (!strcmp(arg
, "--merges")) {
2392 revs
->min_parents
= 2;
2393 } else if (!strcmp(arg
, "--no-merges")) {
2394 revs
->max_parents
= 1;
2395 } else if (skip_prefix(arg
, "--min-parents=", &optarg
)) {
2396 revs
->min_parents
= atoi(optarg
);
2397 } else if (!strcmp(arg
, "--no-min-parents")) {
2398 revs
->min_parents
= 0;
2399 } else if (skip_prefix(arg
, "--max-parents=", &optarg
)) {
2400 revs
->max_parents
= atoi(optarg
);
2401 } else if (!strcmp(arg
, "--no-max-parents")) {
2402 revs
->max_parents
= -1;
2403 } else if (!strcmp(arg
, "--boundary")) {
2405 } else if (!strcmp(arg
, "--left-right")) {
2406 revs
->left_right
= 1;
2407 } else if (!strcmp(arg
, "--left-only")) {
2408 if (revs
->right_only
)
2409 die("--left-only is incompatible with --right-only"
2411 revs
->left_only
= 1;
2412 } else if (!strcmp(arg
, "--right-only")) {
2413 if (revs
->left_only
)
2414 die(_("options '%s' and '%s' cannot be used together"), "--right-only", "--left-only");
2415 revs
->right_only
= 1;
2416 } else if (!strcmp(arg
, "--cherry")) {
2417 if (revs
->left_only
)
2418 die(_("options '%s' and '%s' cannot be used together"), "--cherry", "--left-only");
2419 revs
->cherry_mark
= 1;
2420 revs
->right_only
= 1;
2421 revs
->max_parents
= 1;
2423 } else if (!strcmp(arg
, "--count")) {
2425 } else if (!strcmp(arg
, "--cherry-mark")) {
2426 if (revs
->cherry_pick
)
2427 die(_("options '%s' and '%s' cannot be used together"), "--cherry-mark", "--cherry-pick");
2428 revs
->cherry_mark
= 1;
2429 revs
->limited
= 1; /* needs limit_list() */
2430 } else if (!strcmp(arg
, "--cherry-pick")) {
2431 if (revs
->cherry_mark
)
2432 die(_("options '%s' and '%s' cannot be used together"), "--cherry-pick", "--cherry-mark");
2433 revs
->cherry_pick
= 1;
2435 } else if (!strcmp(arg
, "--objects")) {
2436 revs
->tag_objects
= 1;
2437 revs
->tree_objects
= 1;
2438 revs
->blob_objects
= 1;
2439 } else if (!strcmp(arg
, "--objects-edge")) {
2440 revs
->tag_objects
= 1;
2441 revs
->tree_objects
= 1;
2442 revs
->blob_objects
= 1;
2443 revs
->edge_hint
= 1;
2444 } else if (!strcmp(arg
, "--objects-edge-aggressive")) {
2445 revs
->tag_objects
= 1;
2446 revs
->tree_objects
= 1;
2447 revs
->blob_objects
= 1;
2448 revs
->edge_hint
= 1;
2449 revs
->edge_hint_aggressive
= 1;
2450 } else if (!strcmp(arg
, "--verify-objects")) {
2451 revs
->tag_objects
= 1;
2452 revs
->tree_objects
= 1;
2453 revs
->blob_objects
= 1;
2454 revs
->verify_objects
= 1;
2455 disable_commit_graph(revs
->repo
);
2456 } else if (!strcmp(arg
, "--unpacked")) {
2458 } else if (starts_with(arg
, "--unpacked=")) {
2459 die(_("--unpacked=<packfile> no longer supported"));
2460 } else if (!strcmp(arg
, "--no-kept-objects")) {
2461 revs
->no_kept_objects
= 1;
2462 revs
->keep_pack_cache_flags
|= IN_CORE_KEEP_PACKS
;
2463 revs
->keep_pack_cache_flags
|= ON_DISK_KEEP_PACKS
;
2464 } else if (skip_prefix(arg
, "--no-kept-objects=", &optarg
)) {
2465 revs
->no_kept_objects
= 1;
2466 if (!strcmp(optarg
, "in-core"))
2467 revs
->keep_pack_cache_flags
|= IN_CORE_KEEP_PACKS
;
2468 if (!strcmp(optarg
, "on-disk"))
2469 revs
->keep_pack_cache_flags
|= ON_DISK_KEEP_PACKS
;
2470 } else if (!strcmp(arg
, "-r")) {
2472 revs
->diffopt
.flags
.recursive
= 1;
2473 } else if (!strcmp(arg
, "-t")) {
2475 revs
->diffopt
.flags
.recursive
= 1;
2476 revs
->diffopt
.flags
.tree_in_recursive
= 1;
2477 } else if ((argcount
= diff_merges_parse_opts(revs
, argv
))) {
2479 } else if (!strcmp(arg
, "-v")) {
2480 revs
->verbose_header
= 1;
2481 } else if (!strcmp(arg
, "--pretty")) {
2482 revs
->verbose_header
= 1;
2483 revs
->pretty_given
= 1;
2484 get_commit_format(NULL
, revs
);
2485 } else if (skip_prefix(arg
, "--pretty=", &optarg
) ||
2486 skip_prefix(arg
, "--format=", &optarg
)) {
2488 * Detached form ("--pretty X" as opposed to "--pretty=X")
2489 * not allowed, since the argument is optional.
2491 revs
->verbose_header
= 1;
2492 revs
->pretty_given
= 1;
2493 get_commit_format(optarg
, revs
);
2494 } else if (!strcmp(arg
, "--expand-tabs")) {
2495 revs
->expand_tabs_in_log
= 8;
2496 } else if (!strcmp(arg
, "--no-expand-tabs")) {
2497 revs
->expand_tabs_in_log
= 0;
2498 } else if (skip_prefix(arg
, "--expand-tabs=", &arg
)) {
2500 if (strtol_i(arg
, 10, &val
) < 0 || val
< 0)
2501 die("'%s': not a non-negative integer", arg
);
2502 revs
->expand_tabs_in_log
= val
;
2503 } else if (!strcmp(arg
, "--show-notes") || !strcmp(arg
, "--notes")) {
2504 enable_default_display_notes(&revs
->notes_opt
, &revs
->show_notes
);
2505 revs
->show_notes_given
= 1;
2506 } else if (!strcmp(arg
, "--show-signature")) {
2507 revs
->show_signature
= 1;
2508 } else if (!strcmp(arg
, "--no-show-signature")) {
2509 revs
->show_signature
= 0;
2510 } else if (!strcmp(arg
, "--show-linear-break")) {
2511 revs
->break_bar
= " ..........";
2512 revs
->track_linear
= 1;
2513 revs
->track_first_time
= 1;
2514 } else if (skip_prefix(arg
, "--show-linear-break=", &optarg
)) {
2515 revs
->break_bar
= xstrdup(optarg
);
2516 revs
->track_linear
= 1;
2517 revs
->track_first_time
= 1;
2518 } else if (skip_prefix(arg
, "--show-notes=", &optarg
) ||
2519 skip_prefix(arg
, "--notes=", &optarg
)) {
2520 if (starts_with(arg
, "--show-notes=") &&
2521 revs
->notes_opt
.use_default_notes
< 0)
2522 revs
->notes_opt
.use_default_notes
= 1;
2523 enable_ref_display_notes(&revs
->notes_opt
, &revs
->show_notes
, optarg
);
2524 revs
->show_notes_given
= 1;
2525 } else if (!strcmp(arg
, "--no-notes")) {
2526 disable_display_notes(&revs
->notes_opt
, &revs
->show_notes
);
2527 revs
->show_notes_given
= 1;
2528 } else if (!strcmp(arg
, "--standard-notes")) {
2529 revs
->show_notes_given
= 1;
2530 revs
->notes_opt
.use_default_notes
= 1;
2531 } else if (!strcmp(arg
, "--no-standard-notes")) {
2532 revs
->notes_opt
.use_default_notes
= 0;
2533 } else if (!strcmp(arg
, "--oneline")) {
2534 revs
->verbose_header
= 1;
2535 get_commit_format("oneline", revs
);
2536 revs
->pretty_given
= 1;
2537 revs
->abbrev_commit
= 1;
2538 } else if (!strcmp(arg
, "--graph")) {
2539 graph_clear(revs
->graph
);
2540 revs
->graph
= graph_init(revs
);
2541 } else if (!strcmp(arg
, "--no-graph")) {
2542 graph_clear(revs
->graph
);
2544 } else if (!strcmp(arg
, "--encode-email-headers")) {
2545 revs
->encode_email_headers
= 1;
2546 } else if (!strcmp(arg
, "--no-encode-email-headers")) {
2547 revs
->encode_email_headers
= 0;
2548 } else if (!strcmp(arg
, "--root")) {
2549 revs
->show_root_diff
= 1;
2550 } else if (!strcmp(arg
, "--no-commit-id")) {
2551 revs
->no_commit_id
= 1;
2552 } else if (!strcmp(arg
, "--always")) {
2553 revs
->always_show_header
= 1;
2554 } else if (!strcmp(arg
, "--no-abbrev")) {
2556 } else if (!strcmp(arg
, "--abbrev")) {
2557 revs
->abbrev
= DEFAULT_ABBREV
;
2558 } else if (skip_prefix(arg
, "--abbrev=", &optarg
)) {
2559 revs
->abbrev
= strtoul(optarg
, NULL
, 10);
2560 if (revs
->abbrev
< MINIMUM_ABBREV
)
2561 revs
->abbrev
= MINIMUM_ABBREV
;
2562 else if (revs
->abbrev
> hexsz
)
2563 revs
->abbrev
= hexsz
;
2564 } else if (!strcmp(arg
, "--abbrev-commit")) {
2565 revs
->abbrev_commit
= 1;
2566 revs
->abbrev_commit_given
= 1;
2567 } else if (!strcmp(arg
, "--no-abbrev-commit")) {
2568 revs
->abbrev_commit
= 0;
2569 } else if (!strcmp(arg
, "--full-diff")) {
2571 revs
->full_diff
= 1;
2572 } else if (!strcmp(arg
, "--show-pulls")) {
2573 revs
->show_pulls
= 1;
2574 } else if (!strcmp(arg
, "--full-history")) {
2575 revs
->simplify_history
= 0;
2576 } else if (!strcmp(arg
, "--relative-date")) {
2577 revs
->date_mode
.type
= DATE_RELATIVE
;
2578 revs
->date_mode_explicit
= 1;
2579 } else if ((argcount
= parse_long_opt("date", argv
, &optarg
))) {
2580 parse_date_format(optarg
, &revs
->date_mode
);
2581 revs
->date_mode_explicit
= 1;
2583 } else if (!strcmp(arg
, "--log-size")) {
2584 revs
->show_log_size
= 1;
2587 * Grepping the commit log
2589 else if ((argcount
= parse_long_opt("author", argv
, &optarg
))) {
2590 add_header_grep(revs
, GREP_HEADER_AUTHOR
, optarg
);
2592 } else if ((argcount
= parse_long_opt("committer", argv
, &optarg
))) {
2593 add_header_grep(revs
, GREP_HEADER_COMMITTER
, optarg
);
2595 } else if ((argcount
= parse_long_opt("grep-reflog", argv
, &optarg
))) {
2596 add_header_grep(revs
, GREP_HEADER_REFLOG
, optarg
);
2598 } else if ((argcount
= parse_long_opt("grep", argv
, &optarg
))) {
2599 add_message_grep(revs
, optarg
);
2601 } else if (!strcmp(arg
, "--basic-regexp")) {
2602 revs
->grep_filter
.pattern_type_option
= GREP_PATTERN_TYPE_BRE
;
2603 } else if (!strcmp(arg
, "--extended-regexp") || !strcmp(arg
, "-E")) {
2604 revs
->grep_filter
.pattern_type_option
= GREP_PATTERN_TYPE_ERE
;
2605 } else if (!strcmp(arg
, "--regexp-ignore-case") || !strcmp(arg
, "-i")) {
2606 revs
->grep_filter
.ignore_case
= 1;
2607 revs
->diffopt
.pickaxe_opts
|= DIFF_PICKAXE_IGNORE_CASE
;
2608 } else if (!strcmp(arg
, "--fixed-strings") || !strcmp(arg
, "-F")) {
2609 revs
->grep_filter
.pattern_type_option
= GREP_PATTERN_TYPE_FIXED
;
2610 } else if (!strcmp(arg
, "--perl-regexp") || !strcmp(arg
, "-P")) {
2611 revs
->grep_filter
.pattern_type_option
= GREP_PATTERN_TYPE_PCRE
;
2612 } else if (!strcmp(arg
, "--all-match")) {
2613 revs
->grep_filter
.all_match
= 1;
2614 } else if (!strcmp(arg
, "--invert-grep")) {
2615 revs
->grep_filter
.no_body_match
= 1;
2616 } else if ((argcount
= parse_long_opt("encoding", argv
, &optarg
))) {
2617 if (strcmp(optarg
, "none"))
2618 git_log_output_encoding
= xstrdup(optarg
);
2620 git_log_output_encoding
= "";
2622 } else if (!strcmp(arg
, "--reverse")) {
2624 } else if (!strcmp(arg
, "--children")) {
2625 revs
->children
.name
= "children";
2627 } else if (!strcmp(arg
, "--ignore-missing")) {
2628 revs
->ignore_missing
= 1;
2629 } else if (opt
&& opt
->allow_exclude_promisor_objects
&&
2630 !strcmp(arg
, "--exclude-promisor-objects")) {
2631 if (fetch_if_missing
)
2632 BUG("exclude_promisor_objects can only be used when fetch_if_missing is 0");
2633 revs
->exclude_promisor_objects
= 1;
2635 int opts
= diff_opt_parse(&revs
->diffopt
, argv
, argc
, revs
->prefix
);
2637 unkv
[(*unkc
)++] = arg
;
2644 void parse_revision_opt(struct rev_info
*revs
, struct parse_opt_ctx_t
*ctx
,
2645 const struct option
*options
,
2646 const char * const usagestr
[])
2648 int n
= handle_revision_opt(revs
, ctx
->argc
, ctx
->argv
,
2649 &ctx
->cpidx
, ctx
->out
, NULL
);
2651 error("unknown option `%s'", ctx
->argv
[0]);
2652 usage_with_options(usagestr
, options
);
2658 void revision_opts_finish(struct rev_info
*revs
)
2660 if (revs
->graph
&& revs
->track_linear
)
2661 die(_("options '%s' and '%s' cannot be used together"), "--show-linear-break", "--graph");
2664 revs
->topo_order
= 1;
2665 revs
->rewrite_parents
= 1;
2669 static int for_each_bisect_ref(struct ref_store
*refs
, each_ref_fn fn
,
2670 void *cb_data
, const char *term
)
2672 struct strbuf bisect_refs
= STRBUF_INIT
;
2674 strbuf_addf(&bisect_refs
, "refs/bisect/%s", term
);
2675 status
= refs_for_each_fullref_in(refs
, bisect_refs
.buf
, fn
, cb_data
);
2676 strbuf_release(&bisect_refs
);
2680 static int for_each_bad_bisect_ref(struct ref_store
*refs
, each_ref_fn fn
, void *cb_data
)
2682 return for_each_bisect_ref(refs
, fn
, cb_data
, term_bad
);
2685 static int for_each_good_bisect_ref(struct ref_store
*refs
, each_ref_fn fn
, void *cb_data
)
2687 return for_each_bisect_ref(refs
, fn
, cb_data
, term_good
);
2690 static int handle_revision_pseudo_opt(struct rev_info
*revs
,
2691 const char **argv
, int *flags
)
2693 const char *arg
= argv
[0];
2695 struct ref_store
*refs
;
2698 if (revs
->repo
!= the_repository
) {
2700 * We need some something like get_submodule_worktrees()
2701 * before we can go through all worktrees of a submodule,
2702 * .e.g with adding all HEADs from --all, which is not
2703 * supported right now, so stick to single worktree.
2705 if (!revs
->single_worktree
)
2706 BUG("--single-worktree cannot be used together with submodule");
2708 refs
= get_main_ref_store(revs
->repo
);
2713 * Commands like "git shortlog" will not accept the options below
2714 * unless parse_revision_opt queues them (as opposed to erroring
2717 * When implementing your new pseudo-option, remember to
2718 * register it in the list at the top of handle_revision_opt.
2720 if (!strcmp(arg
, "--all")) {
2721 handle_refs(refs
, revs
, *flags
, refs_for_each_ref
);
2722 handle_refs(refs
, revs
, *flags
, refs_head_ref
);
2723 if (!revs
->single_worktree
) {
2724 struct all_refs_cb cb
;
2726 init_all_refs_cb(&cb
, revs
, *flags
);
2727 other_head_refs(handle_one_ref
, &cb
);
2729 clear_ref_exclusions(&revs
->ref_excludes
);
2730 } else if (!strcmp(arg
, "--branches")) {
2731 if (revs
->ref_excludes
.hidden_refs_configured
)
2732 return error(_("--exclude-hidden cannot be used together with --branches"));
2733 handle_refs(refs
, revs
, *flags
, refs_for_each_branch_ref
);
2734 clear_ref_exclusions(&revs
->ref_excludes
);
2735 } else if (!strcmp(arg
, "--bisect")) {
2736 read_bisect_terms(&term_bad
, &term_good
);
2737 handle_refs(refs
, revs
, *flags
, for_each_bad_bisect_ref
);
2738 handle_refs(refs
, revs
, *flags
^ (UNINTERESTING
| BOTTOM
),
2739 for_each_good_bisect_ref
);
2741 } else if (!strcmp(arg
, "--tags")) {
2742 if (revs
->ref_excludes
.hidden_refs_configured
)
2743 return error(_("--exclude-hidden cannot be used together with --tags"));
2744 handle_refs(refs
, revs
, *flags
, refs_for_each_tag_ref
);
2745 clear_ref_exclusions(&revs
->ref_excludes
);
2746 } else if (!strcmp(arg
, "--remotes")) {
2747 if (revs
->ref_excludes
.hidden_refs_configured
)
2748 return error(_("--exclude-hidden cannot be used together with --remotes"));
2749 handle_refs(refs
, revs
, *flags
, refs_for_each_remote_ref
);
2750 clear_ref_exclusions(&revs
->ref_excludes
);
2751 } else if ((argcount
= parse_long_opt("glob", argv
, &optarg
))) {
2752 struct all_refs_cb cb
;
2753 init_all_refs_cb(&cb
, revs
, *flags
);
2754 for_each_glob_ref(handle_one_ref
, optarg
, &cb
);
2755 clear_ref_exclusions(&revs
->ref_excludes
);
2757 } else if ((argcount
= parse_long_opt("exclude", argv
, &optarg
))) {
2758 add_ref_exclusion(&revs
->ref_excludes
, optarg
);
2760 } else if ((argcount
= parse_long_opt("exclude-hidden", argv
, &optarg
))) {
2761 exclude_hidden_refs(&revs
->ref_excludes
, optarg
);
2763 } else if (skip_prefix(arg
, "--branches=", &optarg
)) {
2764 struct all_refs_cb cb
;
2765 if (revs
->ref_excludes
.hidden_refs_configured
)
2766 return error(_("--exclude-hidden cannot be used together with --branches"));
2767 init_all_refs_cb(&cb
, revs
, *flags
);
2768 for_each_glob_ref_in(handle_one_ref
, optarg
, "refs/heads/", &cb
);
2769 clear_ref_exclusions(&revs
->ref_excludes
);
2770 } else if (skip_prefix(arg
, "--tags=", &optarg
)) {
2771 struct all_refs_cb cb
;
2772 if (revs
->ref_excludes
.hidden_refs_configured
)
2773 return error(_("--exclude-hidden cannot be used together with --tags"));
2774 init_all_refs_cb(&cb
, revs
, *flags
);
2775 for_each_glob_ref_in(handle_one_ref
, optarg
, "refs/tags/", &cb
);
2776 clear_ref_exclusions(&revs
->ref_excludes
);
2777 } else if (skip_prefix(arg
, "--remotes=", &optarg
)) {
2778 struct all_refs_cb cb
;
2779 if (revs
->ref_excludes
.hidden_refs_configured
)
2780 return error(_("--exclude-hidden cannot be used together with --remotes"));
2781 init_all_refs_cb(&cb
, revs
, *flags
);
2782 for_each_glob_ref_in(handle_one_ref
, optarg
, "refs/remotes/", &cb
);
2783 clear_ref_exclusions(&revs
->ref_excludes
);
2784 } else if (!strcmp(arg
, "--reflog")) {
2785 add_reflogs_to_pending(revs
, *flags
);
2786 } else if (!strcmp(arg
, "--indexed-objects")) {
2787 add_index_objects_to_pending(revs
, *flags
);
2788 } else if (!strcmp(arg
, "--alternate-refs")) {
2789 add_alternate_refs_to_pending(revs
, *flags
);
2790 } else if (!strcmp(arg
, "--not")) {
2791 *flags
^= UNINTERESTING
| BOTTOM
;
2792 } else if (!strcmp(arg
, "--no-walk")) {
2794 } else if (skip_prefix(arg
, "--no-walk=", &optarg
)) {
2796 * Detached form ("--no-walk X" as opposed to "--no-walk=X")
2797 * not allowed, since the argument is optional.
2800 if (!strcmp(optarg
, "sorted"))
2801 revs
->unsorted_input
= 0;
2802 else if (!strcmp(optarg
, "unsorted"))
2803 revs
->unsorted_input
= 1;
2805 return error("invalid argument to --no-walk");
2806 } else if (!strcmp(arg
, "--do-walk")) {
2808 } else if (!strcmp(arg
, "--single-worktree")) {
2809 revs
->single_worktree
= 1;
2810 } else if (skip_prefix(arg
, ("--filter="), &arg
)) {
2811 parse_list_objects_filter(&revs
->filter
, arg
);
2812 } else if (!strcmp(arg
, ("--no-filter"))) {
2813 list_objects_filter_set_no_filter(&revs
->filter
);
2821 static void NORETURN
diagnose_missing_default(const char *def
)
2824 const char *refname
;
2826 refname
= resolve_ref_unsafe(def
, 0, NULL
, &flags
);
2827 if (!refname
|| !(flags
& REF_ISSYMREF
) || (flags
& REF_ISBROKEN
))
2828 die(_("your current branch appears to be broken"));
2830 skip_prefix(refname
, "refs/heads/", &refname
);
2831 die(_("your current branch '%s' does not have any commits yet"),
2836 * Parse revision information, filling in the "rev_info" structure,
2837 * and removing the used arguments from the argument list.
2839 * Returns the number of arguments left that weren't recognized
2840 * (which are also moved to the head of the argument list)
2842 int setup_revisions(int argc
, const char **argv
, struct rev_info
*revs
, struct setup_revision_opt
*opt
)
2844 int i
, flags
, left
, seen_dashdash
, revarg_opt
;
2845 struct strvec prune_data
= STRVEC_INIT
;
2846 int seen_end_of_options
= 0;
2848 /* First, search for "--" */
2849 if (opt
&& opt
->assume_dashdash
) {
2853 for (i
= 1; i
< argc
; i
++) {
2854 const char *arg
= argv
[i
];
2855 if (strcmp(arg
, "--"))
2857 if (opt
&& opt
->free_removed_argv_elements
)
2858 free((char *)argv
[i
]);
2862 strvec_pushv(&prune_data
, argv
+ i
+ 1);
2868 /* Second, deal with arguments and options */
2870 revarg_opt
= opt
? opt
->revarg_opt
: 0;
2872 revarg_opt
|= REVARG_CANNOT_BE_FILENAME
;
2873 for (left
= i
= 1; i
< argc
; i
++) {
2874 const char *arg
= argv
[i
];
2875 if (!seen_end_of_options
&& *arg
== '-') {
2878 opts
= handle_revision_pseudo_opt(
2886 if (!strcmp(arg
, "--stdin")) {
2887 if (revs
->disable_stdin
) {
2891 if (revs
->read_from_stdin
++)
2892 die("--stdin given twice?");
2893 read_revisions_from_stdin(revs
, &prune_data
);
2897 if (!strcmp(arg
, "--end-of-options")) {
2898 seen_end_of_options
= 1;
2902 opts
= handle_revision_opt(revs
, argc
- i
, argv
+ i
,
2914 if (handle_revision_arg(arg
, revs
, flags
, revarg_opt
)) {
2916 if (seen_dashdash
|| *arg
== '^')
2917 die("bad revision '%s'", arg
);
2919 /* If we didn't have a "--":
2920 * (1) all filenames must exist;
2921 * (2) all rev-args must not be interpretable
2922 * as a valid filename.
2923 * but the latter we have checked in the main loop.
2925 for (j
= i
; j
< argc
; j
++)
2926 verify_filename(revs
->prefix
, argv
[j
], j
== i
);
2928 strvec_pushv(&prune_data
, argv
+ i
);
2932 revision_opts_finish(revs
);
2934 if (prune_data
.nr
) {
2936 * If we need to introduce the magic "a lone ':' means no
2937 * pathspec whatsoever", here is the place to do so.
2939 * if (prune_data.nr == 1 && !strcmp(prune_data[0], ":")) {
2940 * prune_data.nr = 0;
2941 * prune_data.alloc = 0;
2942 * free(prune_data.path);
2943 * prune_data.path = NULL;
2945 * terminate prune_data.alloc with NULL and
2946 * call init_pathspec() to set revs->prune_data here.
2949 parse_pathspec(&revs
->prune_data
, 0, 0,
2950 revs
->prefix
, prune_data
.v
);
2952 strvec_clear(&prune_data
);
2955 revs
->def
= opt
? opt
->def
: NULL
;
2956 if (opt
&& opt
->tweak
)
2957 opt
->tweak(revs
, opt
);
2958 if (revs
->show_merge
)
2959 prepare_show_merge(revs
);
2960 if (revs
->def
&& !revs
->pending
.nr
&& !revs
->rev_input_given
) {
2961 struct object_id oid
;
2962 struct object
*object
;
2963 struct object_context oc
;
2964 if (get_oid_with_context(revs
->repo
, revs
->def
, 0, &oid
, &oc
))
2965 diagnose_missing_default(revs
->def
);
2966 object
= get_reference(revs
, revs
->def
, &oid
, 0);
2967 add_pending_object_with_mode(revs
, object
, revs
->def
, oc
.mode
);
2970 /* Did the user ask for any diff output? Run the diff! */
2971 if (revs
->diffopt
.output_format
& ~DIFF_FORMAT_NO_OUTPUT
)
2974 /* Pickaxe, diff-filter and rename following need diffs */
2975 if ((revs
->diffopt
.pickaxe_opts
& DIFF_PICKAXE_KINDS_MASK
) ||
2976 revs
->diffopt
.filter
||
2977 revs
->diffopt
.flags
.follow_renames
)
2980 if (revs
->diffopt
.objfind
)
2981 revs
->simplify_history
= 0;
2983 if (revs
->line_level_traverse
) {
2984 if (want_ancestry(revs
))
2986 revs
->topo_order
= 1;
2989 if (revs
->topo_order
&& !generation_numbers_enabled(the_repository
))
2992 if (revs
->prune_data
.nr
) {
2993 copy_pathspec(&revs
->pruning
.pathspec
, &revs
->prune_data
);
2994 /* Can't prune commits with rename following: the paths change.. */
2995 if (!revs
->diffopt
.flags
.follow_renames
)
2997 if (!revs
->full_diff
)
2998 copy_pathspec(&revs
->diffopt
.pathspec
,
3002 diff_merges_setup_revs(revs
);
3004 revs
->diffopt
.abbrev
= revs
->abbrev
;
3006 diff_setup_done(&revs
->diffopt
);
3008 if (!is_encoding_utf8(get_log_output_encoding()))
3009 revs
->grep_filter
.ignore_locale
= 1;
3010 compile_grep_patterns(&revs
->grep_filter
);
3012 if (revs
->reverse
&& revs
->reflog_info
)
3013 die(_("options '%s' and '%s' cannot be used together"), "--reverse", "--walk-reflogs");
3014 if (revs
->reflog_info
&& revs
->limited
)
3015 die("cannot combine --walk-reflogs with history-limiting options");
3016 if (revs
->rewrite_parents
&& revs
->children
.name
)
3017 die(_("options '%s' and '%s' cannot be used together"), "--parents", "--children");
3018 if (revs
->filter
.choice
&& !revs
->blob_objects
)
3019 die(_("object filtering requires --objects"));
3022 * Limitations on the graph functionality
3024 if (revs
->reverse
&& revs
->graph
)
3025 die(_("options '%s' and '%s' cannot be used together"), "--reverse", "--graph");
3027 if (revs
->reflog_info
&& revs
->graph
)
3028 die(_("options '%s' and '%s' cannot be used together"), "--walk-reflogs", "--graph");
3029 if (revs
->no_walk
&& revs
->graph
)
3030 die(_("options '%s' and '%s' cannot be used together"), "--no-walk", "--graph");
3031 if (!revs
->reflog_info
&& revs
->grep_filter
.use_reflog_filter
)
3032 die(_("the option '%s' requires '%s'"), "--grep-reflog", "--walk-reflogs");
3034 if (revs
->line_level_traverse
&&
3035 (revs
->diffopt
.output_format
& ~(DIFF_FORMAT_PATCH
| DIFF_FORMAT_NO_OUTPUT
)))
3036 die(_("-L does not yet support diff formats besides -p and -s"));
3038 if (revs
->expand_tabs_in_log
< 0)
3039 revs
->expand_tabs_in_log
= revs
->expand_tabs_in_log_default
;
3044 static void release_revisions_cmdline(struct rev_cmdline_info
*cmdline
)
3048 for (i
= 0; i
< cmdline
->nr
; i
++)
3049 free((char *)cmdline
->rev
[i
].name
);
3053 static void release_revisions_mailmap(struct string_list
*mailmap
)
3057 clear_mailmap(mailmap
);
3061 static void release_revisions_topo_walk_info(struct topo_walk_info
*info
);
3063 void release_revisions(struct rev_info
*revs
)
3065 free_commit_list(revs
->commits
);
3066 free_commit_list(revs
->ancestry_path_bottoms
);
3067 object_array_clear(&revs
->pending
);
3068 object_array_clear(&revs
->boundary_commits
);
3069 release_revisions_cmdline(&revs
->cmdline
);
3070 list_objects_filter_release(&revs
->filter
);
3071 clear_pathspec(&revs
->prune_data
);
3072 date_mode_release(&revs
->date_mode
);
3073 release_revisions_mailmap(revs
->mailmap
);
3074 free_grep_patterns(&revs
->grep_filter
);
3075 graph_clear(revs
->graph
);
3076 /* TODO (need to handle "no_free"): diff_free(&revs->diffopt) */
3077 diff_free(&revs
->pruning
);
3078 reflog_walk_info_release(revs
->reflog_info
);
3079 release_revisions_topo_walk_info(revs
->topo_walk_info
);
3082 static void add_child(struct rev_info
*revs
, struct commit
*parent
, struct commit
*child
)
3084 struct commit_list
*l
= xcalloc(1, sizeof(*l
));
3087 l
->next
= add_decoration(&revs
->children
, &parent
->object
, l
);
3090 static int remove_duplicate_parents(struct rev_info
*revs
, struct commit
*commit
)
3092 struct treesame_state
*ts
= lookup_decoration(&revs
->treesame
, &commit
->object
);
3093 struct commit_list
**pp
, *p
;
3094 int surviving_parents
;
3096 /* Examine existing parents while marking ones we have seen... */
3097 pp
= &commit
->parents
;
3098 surviving_parents
= 0;
3099 while ((p
= *pp
) != NULL
) {
3100 struct commit
*parent
= p
->item
;
3101 if (parent
->object
.flags
& TMP_MARK
) {
3104 compact_treesame(revs
, commit
, surviving_parents
);
3107 parent
->object
.flags
|= TMP_MARK
;
3108 surviving_parents
++;
3111 /* clear the temporary mark */
3112 for (p
= commit
->parents
; p
; p
= p
->next
) {
3113 p
->item
->object
.flags
&= ~TMP_MARK
;
3115 /* no update_treesame() - removing duplicates can't affect TREESAME */
3116 return surviving_parents
;
3119 struct merge_simplify_state
{
3120 struct commit
*simplified
;
3123 static struct merge_simplify_state
*locate_simplify_state(struct rev_info
*revs
, struct commit
*commit
)
3125 struct merge_simplify_state
*st
;
3127 st
= lookup_decoration(&revs
->merge_simplification
, &commit
->object
);
3129 CALLOC_ARRAY(st
, 1);
3130 add_decoration(&revs
->merge_simplification
, &commit
->object
, st
);
3135 static int mark_redundant_parents(struct commit
*commit
)
3137 struct commit_list
*h
= reduce_heads(commit
->parents
);
3138 int i
= 0, marked
= 0;
3139 struct commit_list
*po
, *pn
;
3141 /* Want these for sanity-checking only */
3142 int orig_cnt
= commit_list_count(commit
->parents
);
3143 int cnt
= commit_list_count(h
);
3146 * Not ready to remove items yet, just mark them for now, based
3147 * on the output of reduce_heads(). reduce_heads outputs the reduced
3148 * set in its original order, so this isn't too hard.
3150 po
= commit
->parents
;
3153 if (pn
&& po
->item
== pn
->item
) {
3157 po
->item
->object
.flags
|= TMP_MARK
;
3163 if (i
!= cnt
|| cnt
+marked
!= orig_cnt
)
3164 die("mark_redundant_parents %d %d %d %d", orig_cnt
, cnt
, i
, marked
);
3166 free_commit_list(h
);
3171 static int mark_treesame_root_parents(struct commit
*commit
)
3173 struct commit_list
*p
;
3176 for (p
= commit
->parents
; p
; p
= p
->next
) {
3177 struct commit
*parent
= p
->item
;
3178 if (!parent
->parents
&& (parent
->object
.flags
& TREESAME
)) {
3179 parent
->object
.flags
|= TMP_MARK
;
3188 * Awkward naming - this means one parent we are TREESAME to.
3189 * cf mark_treesame_root_parents: root parents that are TREESAME (to an
3190 * empty tree). Better name suggestions?
3192 static int leave_one_treesame_to_parent(struct rev_info
*revs
, struct commit
*commit
)
3194 struct treesame_state
*ts
= lookup_decoration(&revs
->treesame
, &commit
->object
);
3195 struct commit
*unmarked
= NULL
, *marked
= NULL
;
3196 struct commit_list
*p
;
3199 for (p
= commit
->parents
, n
= 0; p
; p
= p
->next
, n
++) {
3200 if (ts
->treesame
[n
]) {
3201 if (p
->item
->object
.flags
& TMP_MARK
) {
3214 * If we are TREESAME to a marked-for-deletion parent, but not to any
3215 * unmarked parents, unmark the first TREESAME parent. This is the
3216 * parent that the default simplify_history==1 scan would have followed,
3217 * and it doesn't make sense to omit that path when asking for a
3218 * simplified full history. Retaining it improves the chances of
3219 * understanding odd missed merges that took an old version of a file.
3223 * I--------*X A modified the file, but mainline merge X used
3224 * \ / "-s ours", so took the version from I. X is
3225 * `-*A--' TREESAME to I and !TREESAME to A.
3227 * Default log from X would produce "I". Without this check,
3228 * --full-history --simplify-merges would produce "I-A-X", showing
3229 * the merge commit X and that it changed A, but not making clear that
3230 * it had just taken the I version. With this check, the topology above
3233 * Note that it is possible that the simplification chooses a different
3234 * TREESAME parent from the default, in which case this test doesn't
3235 * activate, and we _do_ drop the default parent. Example:
3237 * I------X A modified the file, but it was reverted in B,
3238 * \ / meaning mainline merge X is TREESAME to both
3241 * Default log would produce "I" by following the first parent;
3242 * --full-history --simplify-merges will produce "I-A-B". But this is a
3243 * reasonable result - it presents a logical full history leading from
3244 * I to X, and X is not an important merge.
3246 if (!unmarked
&& marked
) {
3247 marked
->object
.flags
&= ~TMP_MARK
;
3254 static int remove_marked_parents(struct rev_info
*revs
, struct commit
*commit
)
3256 struct commit_list
**pp
, *p
;
3257 int nth_parent
, removed
= 0;
3259 pp
= &commit
->parents
;
3261 while ((p
= *pp
) != NULL
) {
3262 struct commit
*parent
= p
->item
;
3263 if (parent
->object
.flags
& TMP_MARK
) {
3264 parent
->object
.flags
&= ~TMP_MARK
;
3268 compact_treesame(revs
, commit
, nth_parent
);
3275 /* Removing parents can only increase TREESAMEness */
3276 if (removed
&& !(commit
->object
.flags
& TREESAME
))
3277 update_treesame(revs
, commit
);
3282 static struct commit_list
**simplify_one(struct rev_info
*revs
, struct commit
*commit
, struct commit_list
**tail
)
3284 struct commit_list
*p
;
3285 struct commit
*parent
;
3286 struct merge_simplify_state
*st
, *pst
;
3289 st
= locate_simplify_state(revs
, commit
);
3292 * Have we handled this one?
3298 * An UNINTERESTING commit simplifies to itself, so does a
3299 * root commit. We do not rewrite parents of such commit
3302 if ((commit
->object
.flags
& UNINTERESTING
) || !commit
->parents
) {
3303 st
->simplified
= commit
;
3308 * Do we know what commit all of our parents that matter
3309 * should be rewritten to? Otherwise we are not ready to
3310 * rewrite this one yet.
3312 for (cnt
= 0, p
= commit
->parents
; p
; p
= p
->next
) {
3313 pst
= locate_simplify_state(revs
, p
->item
);
3314 if (!pst
->simplified
) {
3315 tail
= &commit_list_insert(p
->item
, tail
)->next
;
3318 if (revs
->first_parent_only
)
3322 tail
= &commit_list_insert(commit
, tail
)->next
;
3327 * Rewrite our list of parents. Note that this cannot
3328 * affect our TREESAME flags in any way - a commit is
3329 * always TREESAME to its simplification.
3331 for (p
= commit
->parents
; p
; p
= p
->next
) {
3332 pst
= locate_simplify_state(revs
, p
->item
);
3333 p
->item
= pst
->simplified
;
3334 if (revs
->first_parent_only
)
3338 if (revs
->first_parent_only
)
3341 cnt
= remove_duplicate_parents(revs
, commit
);
3344 * It is possible that we are a merge and one side branch
3345 * does not have any commit that touches the given paths;
3346 * in such a case, the immediate parent from that branch
3347 * will be rewritten to be the merge base.
3349 * o----X X: the commit we are looking at;
3350 * / / o: a commit that touches the paths;
3353 * Further, a merge of an independent branch that doesn't
3354 * touch the path will reduce to a treesame root parent:
3356 * ----o----X X: the commit we are looking at;
3357 * / o: a commit that touches the paths;
3358 * r r: a root commit not touching the paths
3360 * Detect and simplify both cases.
3363 int marked
= mark_redundant_parents(commit
);
3364 marked
+= mark_treesame_root_parents(commit
);
3366 marked
-= leave_one_treesame_to_parent(revs
, commit
);
3368 cnt
= remove_marked_parents(revs
, commit
);
3372 * A commit simplifies to itself if it is a root, if it is
3373 * UNINTERESTING, if it touches the given paths, or if it is a
3374 * merge and its parents don't simplify to one relevant commit
3375 * (the first two cases are already handled at the beginning of
3378 * Otherwise, it simplifies to what its sole relevant parent
3382 (commit
->object
.flags
& UNINTERESTING
) ||
3383 !(commit
->object
.flags
& TREESAME
) ||
3384 (parent
= one_relevant_parent(revs
, commit
->parents
)) == NULL
||
3385 (revs
->show_pulls
&& (commit
->object
.flags
& PULL_MERGE
)))
3386 st
->simplified
= commit
;
3388 pst
= locate_simplify_state(revs
, parent
);
3389 st
->simplified
= pst
->simplified
;
3394 static void simplify_merges(struct rev_info
*revs
)
3396 struct commit_list
*list
, *next
;
3397 struct commit_list
*yet_to_do
, **tail
;
3398 struct commit
*commit
;
3403 /* feed the list reversed */
3405 for (list
= revs
->commits
; list
; list
= next
) {
3406 commit
= list
->item
;
3409 * Do not free(list) here yet; the original list
3410 * is used later in this function.
3412 commit_list_insert(commit
, &yet_to_do
);
3419 commit
= pop_commit(&list
);
3420 tail
= simplify_one(revs
, commit
, tail
);
3424 /* clean up the result, removing the simplified ones */
3425 list
= revs
->commits
;
3426 revs
->commits
= NULL
;
3427 tail
= &revs
->commits
;
3429 struct merge_simplify_state
*st
;
3431 commit
= pop_commit(&list
);
3432 st
= locate_simplify_state(revs
, commit
);
3433 if (st
->simplified
== commit
)
3434 tail
= &commit_list_insert(commit
, tail
)->next
;
3438 static void set_children(struct rev_info
*revs
)
3440 struct commit_list
*l
;
3441 for (l
= revs
->commits
; l
; l
= l
->next
) {
3442 struct commit
*commit
= l
->item
;
3443 struct commit_list
*p
;
3445 for (p
= commit
->parents
; p
; p
= p
->next
)
3446 add_child(revs
, p
->item
, commit
);
3450 void reset_revision_walk(void)
3452 clear_object_flags(SEEN
| ADDED
| SHOWN
| TOPO_WALK_EXPLORED
| TOPO_WALK_INDEGREE
);
3455 static int mark_uninteresting(const struct object_id
*oid
,
3456 struct packed_git
*pack UNUSED
,
3457 uint32_t pos UNUSED
,
3460 struct rev_info
*revs
= cb
;
3461 struct object
*o
= lookup_unknown_object(revs
->repo
, oid
);
3462 o
->flags
|= UNINTERESTING
| SEEN
;
3466 define_commit_slab(indegree_slab
, int);
3467 define_commit_slab(author_date_slab
, timestamp_t
);
3469 struct topo_walk_info
{
3470 timestamp_t min_generation
;
3471 struct prio_queue explore_queue
;
3472 struct prio_queue indegree_queue
;
3473 struct prio_queue topo_queue
;
3474 struct indegree_slab indegree
;
3475 struct author_date_slab author_date
;
3478 static int topo_walk_atexit_registered
;
3479 static unsigned int count_explore_walked
;
3480 static unsigned int count_indegree_walked
;
3481 static unsigned int count_topo_walked
;
3483 static void trace2_topo_walk_statistics_atexit(void)
3485 struct json_writer jw
= JSON_WRITER_INIT
;
3487 jw_object_begin(&jw
, 0);
3488 jw_object_intmax(&jw
, "count_explore_walked", count_explore_walked
);
3489 jw_object_intmax(&jw
, "count_indegree_walked", count_indegree_walked
);
3490 jw_object_intmax(&jw
, "count_topo_walked", count_topo_walked
);
3493 trace2_data_json("topo_walk", the_repository
, "statistics", &jw
);
3498 static inline void test_flag_and_insert(struct prio_queue
*q
, struct commit
*c
, int flag
)
3500 if (c
->object
.flags
& flag
)
3503 c
->object
.flags
|= flag
;
3504 prio_queue_put(q
, c
);
3507 static void explore_walk_step(struct rev_info
*revs
)
3509 struct topo_walk_info
*info
= revs
->topo_walk_info
;
3510 struct commit_list
*p
;
3511 struct commit
*c
= prio_queue_get(&info
->explore_queue
);
3516 if (repo_parse_commit_gently(revs
->repo
, c
, 1) < 0)
3519 count_explore_walked
++;
3521 if (revs
->sort_order
== REV_SORT_BY_AUTHOR_DATE
)
3522 record_author_date(&info
->author_date
, c
);
3524 if (revs
->max_age
!= -1 && (c
->date
< revs
->max_age
))
3525 c
->object
.flags
|= UNINTERESTING
;
3527 if (process_parents(revs
, c
, NULL
, NULL
) < 0)
3530 if (c
->object
.flags
& UNINTERESTING
)
3531 mark_parents_uninteresting(revs
, c
);
3533 for (p
= c
->parents
; p
; p
= p
->next
)
3534 test_flag_and_insert(&info
->explore_queue
, p
->item
, TOPO_WALK_EXPLORED
);
3537 static void explore_to_depth(struct rev_info
*revs
,
3538 timestamp_t gen_cutoff
)
3540 struct topo_walk_info
*info
= revs
->topo_walk_info
;
3542 while ((c
= prio_queue_peek(&info
->explore_queue
)) &&
3543 commit_graph_generation(c
) >= gen_cutoff
)
3544 explore_walk_step(revs
);
3547 static void indegree_walk_step(struct rev_info
*revs
)
3549 struct commit_list
*p
;
3550 struct topo_walk_info
*info
= revs
->topo_walk_info
;
3551 struct commit
*c
= prio_queue_get(&info
->indegree_queue
);
3556 if (repo_parse_commit_gently(revs
->repo
, c
, 1) < 0)
3559 count_indegree_walked
++;
3561 explore_to_depth(revs
, commit_graph_generation(c
));
3563 for (p
= c
->parents
; p
; p
= p
->next
) {
3564 struct commit
*parent
= p
->item
;
3565 int *pi
= indegree_slab_at(&info
->indegree
, parent
);
3567 if (repo_parse_commit_gently(revs
->repo
, parent
, 1) < 0)
3575 test_flag_and_insert(&info
->indegree_queue
, parent
, TOPO_WALK_INDEGREE
);
3577 if (revs
->first_parent_only
)
3582 static void compute_indegrees_to_depth(struct rev_info
*revs
,
3583 timestamp_t gen_cutoff
)
3585 struct topo_walk_info
*info
= revs
->topo_walk_info
;
3587 while ((c
= prio_queue_peek(&info
->indegree_queue
)) &&
3588 commit_graph_generation(c
) >= gen_cutoff
)
3589 indegree_walk_step(revs
);
3592 static void release_revisions_topo_walk_info(struct topo_walk_info
*info
)
3596 clear_prio_queue(&info
->explore_queue
);
3597 clear_prio_queue(&info
->indegree_queue
);
3598 clear_prio_queue(&info
->topo_queue
);
3599 clear_indegree_slab(&info
->indegree
);
3600 clear_author_date_slab(&info
->author_date
);
3604 static void reset_topo_walk(struct rev_info
*revs
)
3606 release_revisions_topo_walk_info(revs
->topo_walk_info
);
3607 revs
->topo_walk_info
= NULL
;
3610 static void init_topo_walk(struct rev_info
*revs
)
3612 struct topo_walk_info
*info
;
3613 struct commit_list
*list
;
3614 if (revs
->topo_walk_info
)
3615 reset_topo_walk(revs
);
3617 revs
->topo_walk_info
= xmalloc(sizeof(struct topo_walk_info
));
3618 info
= revs
->topo_walk_info
;
3619 memset(info
, 0, sizeof(struct topo_walk_info
));
3621 init_indegree_slab(&info
->indegree
);
3622 memset(&info
->explore_queue
, 0, sizeof(info
->explore_queue
));
3623 memset(&info
->indegree_queue
, 0, sizeof(info
->indegree_queue
));
3624 memset(&info
->topo_queue
, 0, sizeof(info
->topo_queue
));
3626 switch (revs
->sort_order
) {
3627 default: /* REV_SORT_IN_GRAPH_ORDER */
3628 info
->topo_queue
.compare
= NULL
;
3630 case REV_SORT_BY_COMMIT_DATE
:
3631 info
->topo_queue
.compare
= compare_commits_by_commit_date
;
3633 case REV_SORT_BY_AUTHOR_DATE
:
3634 init_author_date_slab(&info
->author_date
);
3635 info
->topo_queue
.compare
= compare_commits_by_author_date
;
3636 info
->topo_queue
.cb_data
= &info
->author_date
;
3640 info
->explore_queue
.compare
= compare_commits_by_gen_then_commit_date
;
3641 info
->indegree_queue
.compare
= compare_commits_by_gen_then_commit_date
;
3643 info
->min_generation
= GENERATION_NUMBER_INFINITY
;
3644 for (list
= revs
->commits
; list
; list
= list
->next
) {
3645 struct commit
*c
= list
->item
;
3646 timestamp_t generation
;
3648 if (repo_parse_commit_gently(revs
->repo
, c
, 1))
3651 test_flag_and_insert(&info
->explore_queue
, c
, TOPO_WALK_EXPLORED
);
3652 test_flag_and_insert(&info
->indegree_queue
, c
, TOPO_WALK_INDEGREE
);
3654 generation
= commit_graph_generation(c
);
3655 if (generation
< info
->min_generation
)
3656 info
->min_generation
= generation
;
3658 *(indegree_slab_at(&info
->indegree
, c
)) = 1;
3660 if (revs
->sort_order
== REV_SORT_BY_AUTHOR_DATE
)
3661 record_author_date(&info
->author_date
, c
);
3663 compute_indegrees_to_depth(revs
, info
->min_generation
);
3665 for (list
= revs
->commits
; list
; list
= list
->next
) {
3666 struct commit
*c
= list
->item
;
3668 if (*(indegree_slab_at(&info
->indegree
, c
)) == 1)
3669 prio_queue_put(&info
->topo_queue
, c
);
3673 * This is unfortunate; the initial tips need to be shown
3674 * in the order given from the revision traversal machinery.
3676 if (revs
->sort_order
== REV_SORT_IN_GRAPH_ORDER
)
3677 prio_queue_reverse(&info
->topo_queue
);
3679 if (trace2_is_enabled() && !topo_walk_atexit_registered
) {
3680 atexit(trace2_topo_walk_statistics_atexit
);
3681 topo_walk_atexit_registered
= 1;
3685 static struct commit
*next_topo_commit(struct rev_info
*revs
)
3688 struct topo_walk_info
*info
= revs
->topo_walk_info
;
3690 /* pop next off of topo_queue */
3691 c
= prio_queue_get(&info
->topo_queue
);
3694 *(indegree_slab_at(&info
->indegree
, c
)) = 0;
3699 static void expand_topo_walk(struct rev_info
*revs
, struct commit
*commit
)
3701 struct commit_list
*p
;
3702 struct topo_walk_info
*info
= revs
->topo_walk_info
;
3703 if (process_parents(revs
, commit
, NULL
, NULL
) < 0) {
3704 if (!revs
->ignore_missing_links
)
3705 die("Failed to traverse parents of commit %s",
3706 oid_to_hex(&commit
->object
.oid
));
3709 count_topo_walked
++;
3711 for (p
= commit
->parents
; p
; p
= p
->next
) {
3712 struct commit
*parent
= p
->item
;
3714 timestamp_t generation
;
3716 if (parent
->object
.flags
& UNINTERESTING
)
3719 if (repo_parse_commit_gently(revs
->repo
, parent
, 1) < 0)
3722 generation
= commit_graph_generation(parent
);
3723 if (generation
< info
->min_generation
) {
3724 info
->min_generation
= generation
;
3725 compute_indegrees_to_depth(revs
, info
->min_generation
);
3728 pi
= indegree_slab_at(&info
->indegree
, parent
);
3732 prio_queue_put(&info
->topo_queue
, parent
);
3734 if (revs
->first_parent_only
)
3739 int prepare_revision_walk(struct rev_info
*revs
)
3742 struct object_array old_pending
;
3743 struct commit_list
**next
= &revs
->commits
;
3745 memcpy(&old_pending
, &revs
->pending
, sizeof(old_pending
));
3746 revs
->pending
.nr
= 0;
3747 revs
->pending
.alloc
= 0;
3748 revs
->pending
.objects
= NULL
;
3749 for (i
= 0; i
< old_pending
.nr
; i
++) {
3750 struct object_array_entry
*e
= old_pending
.objects
+ i
;
3751 struct commit
*commit
= handle_commit(revs
, e
);
3753 if (!(commit
->object
.flags
& SEEN
)) {
3754 commit
->object
.flags
|= SEEN
;
3755 next
= commit_list_append(commit
, next
);
3759 object_array_clear(&old_pending
);
3761 /* Signal whether we need per-parent treesame decoration */
3762 if (revs
->simplify_merges
||
3763 (revs
->limited
&& limiting_can_increase_treesame(revs
)))
3764 revs
->treesame
.name
= "treesame";
3766 if (revs
->exclude_promisor_objects
) {
3767 for_each_packed_object(mark_uninteresting
, revs
,
3768 FOR_EACH_OBJECT_PROMISOR_ONLY
);
3771 if (!revs
->reflog_info
)
3772 prepare_to_use_bloom_filter(revs
);
3773 if (!revs
->unsorted_input
)
3774 commit_list_sort_by_date(&revs
->commits
);
3777 if (revs
->limited
) {
3778 if (limit_list(revs
) < 0)
3780 if (revs
->topo_order
)
3781 sort_in_topological_order(&revs
->commits
, revs
->sort_order
);
3782 } else if (revs
->topo_order
)
3783 init_topo_walk(revs
);
3784 if (revs
->line_level_traverse
&& want_ancestry(revs
))
3786 * At the moment we can only do line-level log with parent
3787 * rewriting by performing this expensive pre-filtering step.
3788 * If parent rewriting is not requested, then we rather
3789 * perform the line-level log filtering during the regular
3790 * history traversal.
3792 line_log_filter(revs
);
3793 if (revs
->simplify_merges
)
3794 simplify_merges(revs
);
3795 if (revs
->children
.name
)
3801 static enum rewrite_result
rewrite_one_1(struct rev_info
*revs
,
3803 struct prio_queue
*queue
)
3806 struct commit
*p
= *pp
;
3808 if (process_parents(revs
, p
, NULL
, queue
) < 0)
3809 return rewrite_one_error
;
3810 if (p
->object
.flags
& UNINTERESTING
)
3811 return rewrite_one_ok
;
3812 if (!(p
->object
.flags
& TREESAME
))
3813 return rewrite_one_ok
;
3815 return rewrite_one_noparents
;
3816 if (!(p
= one_relevant_parent(revs
, p
->parents
)))
3817 return rewrite_one_ok
;
3822 static void merge_queue_into_list(struct prio_queue
*q
, struct commit_list
**list
)
3825 struct commit
*item
= prio_queue_peek(q
);
3826 struct commit_list
*p
= *list
;
3828 if (p
&& p
->item
->date
>= item
->date
)
3831 p
= commit_list_insert(item
, list
);
3832 list
= &p
->next
; /* skip newly added item */
3833 prio_queue_get(q
); /* pop item */
3838 static enum rewrite_result
rewrite_one(struct rev_info
*revs
, struct commit
**pp
)
3840 struct prio_queue queue
= { compare_commits_by_commit_date
};
3841 enum rewrite_result ret
= rewrite_one_1(revs
, pp
, &queue
);
3842 merge_queue_into_list(&queue
, &revs
->commits
);
3843 clear_prio_queue(&queue
);
3847 int rewrite_parents(struct rev_info
*revs
, struct commit
*commit
,
3848 rewrite_parent_fn_t rewrite_parent
)
3850 struct commit_list
**pp
= &commit
->parents
;
3852 struct commit_list
*parent
= *pp
;
3853 switch (rewrite_parent(revs
, &parent
->item
)) {
3854 case rewrite_one_ok
:
3856 case rewrite_one_noparents
:
3859 case rewrite_one_error
:
3864 remove_duplicate_parents(revs
, commit
);
3868 static int commit_match(struct commit
*commit
, struct rev_info
*opt
)
3871 const char *encoding
;
3872 const char *message
;
3873 struct strbuf buf
= STRBUF_INIT
;
3875 if (!opt
->grep_filter
.pattern_list
&& !opt
->grep_filter
.header_list
)
3878 /* Prepend "fake" headers as needed */
3879 if (opt
->grep_filter
.use_reflog_filter
) {
3880 strbuf_addstr(&buf
, "reflog ");
3881 get_reflog_message(&buf
, opt
->reflog_info
);
3882 strbuf_addch(&buf
, '\n');
3886 * We grep in the user's output encoding, under the assumption that it
3887 * is the encoding they are most likely to write their grep pattern
3888 * for. In addition, it means we will match the "notes" encoding below,
3889 * so we will not end up with a buffer that has two different encodings
3892 encoding
= get_log_output_encoding();
3893 message
= repo_logmsg_reencode(the_repository
, commit
, NULL
, encoding
);
3895 /* Copy the commit to temporary if we are using "fake" headers */
3897 strbuf_addstr(&buf
, message
);
3899 if (opt
->grep_filter
.header_list
&& opt
->mailmap
) {
3900 const char *commit_headers
[] = { "author ", "committer ", NULL
};
3903 strbuf_addstr(&buf
, message
);
3905 apply_mailmap_to_header(&buf
, commit_headers
, opt
->mailmap
);
3908 /* Append "fake" message parts as needed */
3909 if (opt
->show_notes
) {
3911 strbuf_addstr(&buf
, message
);
3912 format_display_notes(&commit
->object
.oid
, &buf
, encoding
, 1);
3916 * Find either in the original commit message, or in the temporary.
3917 * Note that we cast away the constness of "message" here. It is
3918 * const because it may come from the cached commit buffer. That's OK,
3919 * because we know that it is modifiable heap memory, and that while
3920 * grep_buffer may modify it for speed, it will restore any
3921 * changes before returning.
3924 retval
= grep_buffer(&opt
->grep_filter
, buf
.buf
, buf
.len
);
3926 retval
= grep_buffer(&opt
->grep_filter
,
3927 (char *)message
, strlen(message
));
3928 strbuf_release(&buf
);
3929 repo_unuse_commit_buffer(the_repository
, commit
, message
);
3933 static inline int want_ancestry(const struct rev_info
*revs
)
3935 return (revs
->rewrite_parents
|| revs
->children
.name
);
3939 * Return a timestamp to be used for --since/--until comparisons for this
3940 * commit, based on the revision options.
3942 static timestamp_t
comparison_date(const struct rev_info
*revs
,
3943 struct commit
*commit
)
3945 return revs
->reflog_info
?
3946 get_reflog_timestamp(revs
->reflog_info
) :
3950 enum commit_action
get_commit_action(struct rev_info
*revs
, struct commit
*commit
)
3952 if (commit
->object
.flags
& SHOWN
)
3953 return commit_ignore
;
3954 if (revs
->unpacked
&& has_object_pack(&commit
->object
.oid
))
3955 return commit_ignore
;
3956 if (revs
->no_kept_objects
) {
3957 if (has_object_kept_pack(&commit
->object
.oid
,
3958 revs
->keep_pack_cache_flags
))
3959 return commit_ignore
;
3961 if (commit
->object
.flags
& UNINTERESTING
)
3962 return commit_ignore
;
3963 if (revs
->line_level_traverse
&& !want_ancestry(revs
)) {
3965 * In case of line-level log with parent rewriting
3966 * prepare_revision_walk() already took care of all line-level
3967 * log filtering, and there is nothing left to do here.
3969 * If parent rewriting was not requested, then this is the
3970 * place to perform the line-level log filtering. Notably,
3971 * this check, though expensive, must come before the other,
3972 * cheaper filtering conditions, because the tracked line
3973 * ranges must be adjusted even when the commit will end up
3974 * being ignored based on other conditions.
3976 if (!line_log_process_ranges_arbitrary_commit(revs
, commit
))
3977 return commit_ignore
;
3979 if (revs
->min_age
!= -1 &&
3980 comparison_date(revs
, commit
) > revs
->min_age
)
3981 return commit_ignore
;
3982 if (revs
->max_age_as_filter
!= -1 &&
3983 comparison_date(revs
, commit
) < revs
->max_age_as_filter
)
3984 return commit_ignore
;
3985 if (revs
->min_parents
|| (revs
->max_parents
>= 0)) {
3986 int n
= commit_list_count(commit
->parents
);
3987 if ((n
< revs
->min_parents
) ||
3988 ((revs
->max_parents
>= 0) && (n
> revs
->max_parents
)))
3989 return commit_ignore
;
3991 if (!commit_match(commit
, revs
))
3992 return commit_ignore
;
3993 if (revs
->prune
&& revs
->dense
) {
3994 /* Commit without changes? */
3995 if (commit
->object
.flags
& TREESAME
) {
3997 struct commit_list
*p
;
3998 /* drop merges unless we want parenthood */
3999 if (!want_ancestry(revs
))
4000 return commit_ignore
;
4002 if (revs
->show_pulls
&& (commit
->object
.flags
& PULL_MERGE
))
4006 * If we want ancestry, then need to keep any merges
4007 * between relevant commits to tie together topology.
4008 * For consistency with TREESAME and simplification
4009 * use "relevant" here rather than just INTERESTING,
4010 * to treat bottom commit(s) as part of the topology.
4012 for (n
= 0, p
= commit
->parents
; p
; p
= p
->next
)
4013 if (relevant_commit(p
->item
))
4016 return commit_ignore
;
4022 define_commit_slab(saved_parents
, struct commit_list
*);
4024 #define EMPTY_PARENT_LIST ((struct commit_list *)-1)
4027 * You may only call save_parents() once per commit (this is checked
4028 * for non-root commits).
4030 static void save_parents(struct rev_info
*revs
, struct commit
*commit
)
4032 struct commit_list
**pp
;
4034 if (!revs
->saved_parents_slab
) {
4035 revs
->saved_parents_slab
= xmalloc(sizeof(struct saved_parents
));
4036 init_saved_parents(revs
->saved_parents_slab
);
4039 pp
= saved_parents_at(revs
->saved_parents_slab
, commit
);
4042 * When walking with reflogs, we may visit the same commit
4043 * several times: once for each appearance in the reflog.
4045 * In this case, save_parents() will be called multiple times.
4046 * We want to keep only the first set of parents. We need to
4047 * store a sentinel value for an empty (i.e., NULL) parent
4048 * list to distinguish it from a not-yet-saved list, however.
4052 if (commit
->parents
)
4053 *pp
= copy_commit_list(commit
->parents
);
4055 *pp
= EMPTY_PARENT_LIST
;
4058 static void free_saved_parents(struct rev_info
*revs
)
4060 if (revs
->saved_parents_slab
)
4061 clear_saved_parents(revs
->saved_parents_slab
);
4064 struct commit_list
*get_saved_parents(struct rev_info
*revs
, const struct commit
*commit
)
4066 struct commit_list
*parents
;
4068 if (!revs
->saved_parents_slab
)
4069 return commit
->parents
;
4071 parents
= *saved_parents_at(revs
->saved_parents_slab
, commit
);
4072 if (parents
== EMPTY_PARENT_LIST
)
4077 enum commit_action
simplify_commit(struct rev_info
*revs
, struct commit
*commit
)
4079 enum commit_action action
= get_commit_action(revs
, commit
);
4081 if (action
== commit_show
&&
4082 revs
->prune
&& revs
->dense
&& want_ancestry(revs
)) {
4084 * --full-diff on simplified parents is no good: it
4085 * will show spurious changes from the commits that
4086 * were elided. So we save the parents on the side
4087 * when --full-diff is in effect.
4089 if (revs
->full_diff
)
4090 save_parents(revs
, commit
);
4091 if (rewrite_parents(revs
, commit
, rewrite_one
) < 0)
4092 return commit_error
;
4097 static void track_linear(struct rev_info
*revs
, struct commit
*commit
)
4099 if (revs
->track_first_time
) {
4101 revs
->track_first_time
= 0;
4103 struct commit_list
*p
;
4104 for (p
= revs
->previous_parents
; p
; p
= p
->next
)
4105 if (p
->item
== NULL
|| /* first commit */
4106 oideq(&p
->item
->object
.oid
, &commit
->object
.oid
))
4108 revs
->linear
= p
!= NULL
;
4110 if (revs
->reverse
) {
4112 commit
->object
.flags
|= TRACK_LINEAR
;
4114 free_commit_list(revs
->previous_parents
);
4115 revs
->previous_parents
= copy_commit_list(commit
->parents
);
4118 static struct commit
*get_revision_1(struct rev_info
*revs
)
4121 struct commit
*commit
;
4123 if (revs
->reflog_info
)
4124 commit
= next_reflog_entry(revs
->reflog_info
);
4125 else if (revs
->topo_walk_info
)
4126 commit
= next_topo_commit(revs
);
4128 commit
= pop_commit(&revs
->commits
);
4133 if (revs
->reflog_info
)
4134 commit
->object
.flags
&= ~(ADDED
| SEEN
| SHOWN
);
4137 * If we haven't done the list limiting, we need to look at
4138 * the parents here. We also need to do the date-based limiting
4139 * that we'd otherwise have done in limit_list().
4141 if (!revs
->limited
) {
4142 if (revs
->max_age
!= -1 &&
4143 comparison_date(revs
, commit
) < revs
->max_age
)
4146 if (revs
->reflog_info
)
4147 try_to_simplify_commit(revs
, commit
);
4148 else if (revs
->topo_walk_info
)
4149 expand_topo_walk(revs
, commit
);
4150 else if (process_parents(revs
, commit
, &revs
->commits
, NULL
) < 0) {
4151 if (!revs
->ignore_missing_links
)
4152 die("Failed to traverse parents of commit %s",
4153 oid_to_hex(&commit
->object
.oid
));
4157 switch (simplify_commit(revs
, commit
)) {
4161 die("Failed to simplify parents of commit %s",
4162 oid_to_hex(&commit
->object
.oid
));
4164 if (revs
->track_linear
)
4165 track_linear(revs
, commit
);
4172 * Return true for entries that have not yet been shown. (This is an
4173 * object_array_each_func_t.)
4175 static int entry_unshown(struct object_array_entry
*entry
, void *cb_data UNUSED
)
4177 return !(entry
->item
->flags
& SHOWN
);
4181 * If array is on the verge of a realloc, garbage-collect any entries
4182 * that have already been shown to try to free up some space.
4184 static void gc_boundary(struct object_array
*array
)
4186 if (array
->nr
== array
->alloc
)
4187 object_array_filter(array
, entry_unshown
, NULL
);
4190 static void create_boundary_commit_list(struct rev_info
*revs
)
4194 struct object_array
*array
= &revs
->boundary_commits
;
4195 struct object_array_entry
*objects
= array
->objects
;
4198 * If revs->commits is non-NULL at this point, an error occurred in
4199 * get_revision_1(). Ignore the error and continue printing the
4200 * boundary commits anyway. (This is what the code has always
4203 free_commit_list(revs
->commits
);
4204 revs
->commits
= NULL
;
4207 * Put all of the actual boundary commits from revs->boundary_commits
4208 * into revs->commits
4210 for (i
= 0; i
< array
->nr
; i
++) {
4211 c
= (struct commit
*)(objects
[i
].item
);
4214 if (!(c
->object
.flags
& CHILD_SHOWN
))
4216 if (c
->object
.flags
& (SHOWN
| BOUNDARY
))
4218 c
->object
.flags
|= BOUNDARY
;
4219 commit_list_insert(c
, &revs
->commits
);
4223 * If revs->topo_order is set, sort the boundary commits
4224 * in topological order
4226 sort_in_topological_order(&revs
->commits
, revs
->sort_order
);
4229 static struct commit
*get_revision_internal(struct rev_info
*revs
)
4231 struct commit
*c
= NULL
;
4232 struct commit_list
*l
;
4234 if (revs
->boundary
== 2) {
4236 * All of the normal commits have already been returned,
4237 * and we are now returning boundary commits.
4238 * create_boundary_commit_list() has populated
4239 * revs->commits with the remaining commits to return.
4241 c
= pop_commit(&revs
->commits
);
4243 c
->object
.flags
|= SHOWN
;
4248 * If our max_count counter has reached zero, then we are done. We
4249 * don't simply return NULL because we still might need to show
4250 * boundary commits. But we want to avoid calling get_revision_1, which
4251 * might do a considerable amount of work finding the next commit only
4252 * for us to throw it away.
4254 * If it is non-zero, then either we don't have a max_count at all
4255 * (-1), or it is still counting, in which case we decrement.
4257 if (revs
->max_count
) {
4258 c
= get_revision_1(revs
);
4260 while (revs
->skip_count
> 0) {
4262 c
= get_revision_1(revs
);
4268 if (revs
->max_count
> 0)
4273 c
->object
.flags
|= SHOWN
;
4275 if (!revs
->boundary
)
4280 * get_revision_1() runs out the commits, and
4281 * we are done computing the boundaries.
4282 * switch to boundary commits output mode.
4287 * Update revs->commits to contain the list of
4290 create_boundary_commit_list(revs
);
4292 return get_revision_internal(revs
);
4296 * boundary commits are the commits that are parents of the
4297 * ones we got from get_revision_1() but they themselves are
4298 * not returned from get_revision_1(). Before returning
4299 * 'c', we need to mark its parents that they could be boundaries.
4302 for (l
= c
->parents
; l
; l
= l
->next
) {
4304 p
= &(l
->item
->object
);
4305 if (p
->flags
& (CHILD_SHOWN
| SHOWN
))
4307 p
->flags
|= CHILD_SHOWN
;
4308 gc_boundary(&revs
->boundary_commits
);
4309 add_object_array(p
, NULL
, &revs
->boundary_commits
);
4315 struct commit
*get_revision(struct rev_info
*revs
)
4318 struct commit_list
*reversed
;
4320 if (revs
->reverse
) {
4322 while ((c
= get_revision_internal(revs
)))
4323 commit_list_insert(c
, &reversed
);
4324 revs
->commits
= reversed
;
4326 revs
->reverse_output_stage
= 1;
4329 if (revs
->reverse_output_stage
) {
4330 c
= pop_commit(&revs
->commits
);
4331 if (revs
->track_linear
)
4332 revs
->linear
= !!(c
&& c
->object
.flags
& TRACK_LINEAR
);
4336 c
= get_revision_internal(revs
);
4337 if (c
&& revs
->graph
)
4338 graph_update(revs
->graph
, c
);
4340 free_saved_parents(revs
);
4341 free_commit_list(revs
->previous_parents
);
4342 revs
->previous_parents
= NULL
;
4347 const char *get_revision_mark(const struct rev_info
*revs
, const struct commit
*commit
)
4349 if (commit
->object
.flags
& BOUNDARY
)
4351 else if (commit
->object
.flags
& UNINTERESTING
)
4353 else if (commit
->object
.flags
& PATCHSAME
)
4355 else if (!revs
|| revs
->left_right
) {
4356 if (commit
->object
.flags
& SYMMETRIC_LEFT
)
4360 } else if (revs
->graph
)
4362 else if (revs
->cherry_mark
)
4367 void put_revision_mark(const struct rev_info
*revs
, const struct commit
*commit
)
4369 const char *mark
= get_revision_mark(revs
, commit
);
4372 fputs(mark
, stdout
);