1 #include "git-compat-util.h"
4 #include "environment.h"
7 #include "object-store.h"
13 #include "diff-merges.h"
16 #include "repository.h"
19 #include "reflog-walk.h"
20 #include "patch-ids.h"
23 #include "string-list.h"
26 #include "commit-slab.h"
28 #include "cache-tree.h"
34 #include "commit-reach.h"
35 #include "commit-graph.h"
36 #include "prio-queue.h"
40 #include "json-writer.h"
41 #include "list-objects-filter-options.h"
42 #include "resolve-undo.h"
44 volatile show_early_output_fn_t show_early_output
;
46 static const char *term_bad
;
47 static const char *term_good
;
49 implement_shared_commit_slab(revision_sources
, char *);
51 static inline int want_ancestry(const struct rev_info
*revs
);
53 void show_object_with_name(FILE *out
, struct object
*obj
, const char *name
)
55 fprintf(out
, "%s ", oid_to_hex(&obj
->oid
));
56 for (const char *p
= name
; *p
&& *p
!= '\n'; p
++)
61 static void mark_blob_uninteresting(struct blob
*blob
)
65 if (blob
->object
.flags
& UNINTERESTING
)
67 blob
->object
.flags
|= UNINTERESTING
;
70 static void mark_tree_contents_uninteresting(struct repository
*r
,
73 struct tree_desc desc
;
74 struct name_entry entry
;
76 if (parse_tree_gently(tree
, 1) < 0)
79 init_tree_desc(&desc
, tree
->buffer
, tree
->size
);
80 while (tree_entry(&desc
, &entry
)) {
81 switch (object_type(entry
.mode
)) {
83 mark_tree_uninteresting(r
, lookup_tree(r
, &entry
.oid
));
86 mark_blob_uninteresting(lookup_blob(r
, &entry
.oid
));
89 /* Subproject commit - not in this repository */
95 * We don't care about the tree any more
96 * after it has been marked uninteresting.
98 free_tree_buffer(tree
);
101 void mark_tree_uninteresting(struct repository
*r
, struct tree
*tree
)
109 if (obj
->flags
& UNINTERESTING
)
111 obj
->flags
|= UNINTERESTING
;
112 mark_tree_contents_uninteresting(r
, tree
);
115 struct path_and_oids_entry
{
116 struct hashmap_entry ent
;
121 static int path_and_oids_cmp(const void *hashmap_cmp_fn_data UNUSED
,
122 const struct hashmap_entry
*eptr
,
123 const struct hashmap_entry
*entry_or_key
,
124 const void *keydata UNUSED
)
126 const struct path_and_oids_entry
*e1
, *e2
;
128 e1
= container_of(eptr
, const struct path_and_oids_entry
, ent
);
129 e2
= container_of(entry_or_key
, const struct path_and_oids_entry
, ent
);
131 return strcmp(e1
->path
, e2
->path
);
134 static void paths_and_oids_clear(struct hashmap
*map
)
136 struct hashmap_iter iter
;
137 struct path_and_oids_entry
*entry
;
139 hashmap_for_each_entry(map
, &iter
, entry
, ent
/* member name */) {
140 oidset_clear(&entry
->trees
);
144 hashmap_clear_and_free(map
, struct path_and_oids_entry
, ent
);
147 static void paths_and_oids_insert(struct hashmap
*map
,
149 const struct object_id
*oid
)
151 int hash
= strhash(path
);
152 struct path_and_oids_entry key
;
153 struct path_and_oids_entry
*entry
;
155 hashmap_entry_init(&key
.ent
, hash
);
157 /* use a shallow copy for the lookup */
158 key
.path
= (char *)path
;
159 oidset_init(&key
.trees
, 0);
161 entry
= hashmap_get_entry(map
, &key
, ent
, NULL
);
163 CALLOC_ARRAY(entry
, 1);
164 hashmap_entry_init(&entry
->ent
, hash
);
165 entry
->path
= xstrdup(key
.path
);
166 oidset_init(&entry
->trees
, 16);
167 hashmap_put(map
, &entry
->ent
);
170 oidset_insert(&entry
->trees
, oid
);
173 static void add_children_by_path(struct repository
*r
,
177 struct tree_desc desc
;
178 struct name_entry entry
;
183 if (parse_tree_gently(tree
, 1) < 0)
186 init_tree_desc(&desc
, tree
->buffer
, tree
->size
);
187 while (tree_entry(&desc
, &entry
)) {
188 switch (object_type(entry
.mode
)) {
190 paths_and_oids_insert(map
, entry
.path
, &entry
.oid
);
192 if (tree
->object
.flags
& UNINTERESTING
) {
193 struct tree
*child
= lookup_tree(r
, &entry
.oid
);
195 child
->object
.flags
|= UNINTERESTING
;
199 if (tree
->object
.flags
& UNINTERESTING
) {
200 struct blob
*child
= lookup_blob(r
, &entry
.oid
);
202 child
->object
.flags
|= UNINTERESTING
;
206 /* Subproject commit - not in this repository */
211 free_tree_buffer(tree
);
214 void mark_trees_uninteresting_sparse(struct repository
*r
,
215 struct oidset
*trees
)
217 unsigned has_interesting
= 0, has_uninteresting
= 0;
218 struct hashmap map
= HASHMAP_INIT(path_and_oids_cmp
, NULL
);
219 struct hashmap_iter map_iter
;
220 struct path_and_oids_entry
*entry
;
221 struct object_id
*oid
;
222 struct oidset_iter iter
;
224 oidset_iter_init(trees
, &iter
);
225 while ((!has_interesting
|| !has_uninteresting
) &&
226 (oid
= oidset_iter_next(&iter
))) {
227 struct tree
*tree
= lookup_tree(r
, oid
);
232 if (tree
->object
.flags
& UNINTERESTING
)
233 has_uninteresting
= 1;
238 /* Do not walk unless we have both types of trees. */
239 if (!has_uninteresting
|| !has_interesting
)
242 oidset_iter_init(trees
, &iter
);
243 while ((oid
= oidset_iter_next(&iter
))) {
244 struct tree
*tree
= lookup_tree(r
, oid
);
245 add_children_by_path(r
, tree
, &map
);
248 hashmap_for_each_entry(&map
, &map_iter
, entry
, ent
/* member name */)
249 mark_trees_uninteresting_sparse(r
, &entry
->trees
);
251 paths_and_oids_clear(&map
);
254 struct commit_stack
{
255 struct commit
**items
;
258 #define COMMIT_STACK_INIT { 0 }
260 static void commit_stack_push(struct commit_stack
*stack
, struct commit
*commit
)
262 ALLOC_GROW(stack
->items
, stack
->nr
+ 1, stack
->alloc
);
263 stack
->items
[stack
->nr
++] = commit
;
266 static struct commit
*commit_stack_pop(struct commit_stack
*stack
)
268 return stack
->nr
? stack
->items
[--stack
->nr
] : NULL
;
271 static void commit_stack_clear(struct commit_stack
*stack
)
273 FREE_AND_NULL(stack
->items
);
274 stack
->nr
= stack
->alloc
= 0;
277 static void mark_one_parent_uninteresting(struct rev_info
*revs
, struct commit
*commit
,
278 struct commit_stack
*pending
)
280 struct commit_list
*l
;
282 if (commit
->object
.flags
& UNINTERESTING
)
284 commit
->object
.flags
|= UNINTERESTING
;
287 * Normally we haven't parsed the parent
288 * yet, so we won't have a parent of a parent
289 * here. However, it may turn out that we've
290 * reached this commit some other way (where it
291 * wasn't uninteresting), in which case we need
292 * to mark its parents recursively too..
294 for (l
= commit
->parents
; l
; l
= l
->next
) {
295 commit_stack_push(pending
, l
->item
);
296 if (revs
&& revs
->exclude_first_parent_only
)
301 void mark_parents_uninteresting(struct rev_info
*revs
, struct commit
*commit
)
303 struct commit_stack pending
= COMMIT_STACK_INIT
;
304 struct commit_list
*l
;
306 for (l
= commit
->parents
; l
; l
= l
->next
) {
307 mark_one_parent_uninteresting(revs
, l
->item
, &pending
);
308 if (revs
&& revs
->exclude_first_parent_only
)
312 while (pending
.nr
> 0)
313 mark_one_parent_uninteresting(revs
, commit_stack_pop(&pending
),
316 commit_stack_clear(&pending
);
319 static void add_pending_object_with_path(struct rev_info
*revs
,
321 const char *name
, unsigned mode
,
324 struct interpret_branch_name_options options
= { 0 };
327 if (revs
->no_walk
&& (obj
->flags
& UNINTERESTING
))
329 if (revs
->reflog_info
&& obj
->type
== OBJ_COMMIT
) {
330 struct strbuf buf
= STRBUF_INIT
;
331 size_t namelen
= strlen(name
);
332 int len
= interpret_branch_name(name
, namelen
, &buf
, &options
);
334 if (0 < len
&& len
< namelen
&& buf
.len
)
335 strbuf_addstr(&buf
, name
+ len
);
336 add_reflog_for_walk(revs
->reflog_info
,
337 (struct commit
*)obj
,
338 buf
.buf
[0] ? buf
.buf
: name
);
339 strbuf_release(&buf
);
340 return; /* do not add the commit itself */
342 add_object_array_with_path(obj
, name
, &revs
->pending
, mode
, path
);
345 static void add_pending_object_with_mode(struct rev_info
*revs
,
347 const char *name
, unsigned mode
)
349 add_pending_object_with_path(revs
, obj
, name
, mode
, NULL
);
352 void add_pending_object(struct rev_info
*revs
,
353 struct object
*obj
, const char *name
)
355 add_pending_object_with_mode(revs
, obj
, name
, S_IFINVALID
);
358 void add_head_to_pending(struct rev_info
*revs
)
360 struct object_id oid
;
362 if (get_oid("HEAD", &oid
))
364 obj
= parse_object(revs
->repo
, &oid
);
367 add_pending_object(revs
, obj
, "HEAD");
370 static struct object
*get_reference(struct rev_info
*revs
, const char *name
,
371 const struct object_id
*oid
,
374 struct object
*object
;
376 object
= parse_object_with_flags(revs
->repo
, oid
,
377 revs
->verify_objects
? 0 :
378 PARSE_OBJECT_SKIP_HASH_CHECK
);
381 if (revs
->ignore_missing
)
383 if (revs
->exclude_promisor_objects
&& is_promisor_object(oid
))
385 die("bad object %s", name
);
387 object
->flags
|= flags
;
391 void add_pending_oid(struct rev_info
*revs
, const char *name
,
392 const struct object_id
*oid
, unsigned int flags
)
394 struct object
*object
= get_reference(revs
, name
, oid
, flags
);
395 add_pending_object(revs
, object
, name
);
398 static struct commit
*handle_commit(struct rev_info
*revs
,
399 struct object_array_entry
*entry
)
401 struct object
*object
= entry
->item
;
402 const char *name
= entry
->name
;
403 const char *path
= entry
->path
;
404 unsigned int mode
= entry
->mode
;
405 unsigned long flags
= object
->flags
;
408 * Tag object? Look what it points to..
410 while (object
->type
== OBJ_TAG
) {
411 struct tag
*tag
= (struct tag
*) object
;
412 if (revs
->tag_objects
&& !(flags
& UNINTERESTING
))
413 add_pending_object(revs
, object
, tag
->tag
);
414 object
= parse_object(revs
->repo
, get_tagged_oid(tag
));
416 if (revs
->ignore_missing_links
|| (flags
& UNINTERESTING
))
418 if (revs
->exclude_promisor_objects
&&
419 is_promisor_object(&tag
->tagged
->oid
))
421 die("bad object %s", oid_to_hex(&tag
->tagged
->oid
));
423 object
->flags
|= flags
;
425 * We'll handle the tagged object by looping or dropping
426 * through to the non-tag handlers below. Do not
427 * propagate path data from the tag's pending entry.
434 * Commit object? Just return it, we'll do all the complex
437 if (object
->type
== OBJ_COMMIT
) {
438 struct commit
*commit
= (struct commit
*)object
;
440 if (repo_parse_commit(revs
->repo
, commit
) < 0)
441 die("unable to parse commit %s", name
);
442 if (flags
& UNINTERESTING
) {
443 mark_parents_uninteresting(revs
, commit
);
445 if (!revs
->topo_order
|| !generation_numbers_enabled(the_repository
))
449 char **slot
= revision_sources_at(revs
->sources
, commit
);
452 *slot
= xstrdup(name
);
458 * Tree object? Either mark it uninteresting, or add it
459 * to the list of objects to look at later..
461 if (object
->type
== OBJ_TREE
) {
462 struct tree
*tree
= (struct tree
*)object
;
463 if (!revs
->tree_objects
)
465 if (flags
& UNINTERESTING
) {
466 mark_tree_contents_uninteresting(revs
->repo
, tree
);
469 add_pending_object_with_path(revs
, object
, name
, mode
, path
);
474 * Blob object? You know the drill by now..
476 if (object
->type
== OBJ_BLOB
) {
477 if (!revs
->blob_objects
)
479 if (flags
& UNINTERESTING
)
481 add_pending_object_with_path(revs
, object
, name
, mode
, path
);
484 die("%s is unknown object", name
);
487 static int everybody_uninteresting(struct commit_list
*orig
,
488 struct commit
**interesting_cache
)
490 struct commit_list
*list
= orig
;
492 if (*interesting_cache
) {
493 struct commit
*commit
= *interesting_cache
;
494 if (!(commit
->object
.flags
& UNINTERESTING
))
499 struct commit
*commit
= list
->item
;
501 if (commit
->object
.flags
& UNINTERESTING
)
504 *interesting_cache
= commit
;
511 * A definition of "relevant" commit that we can use to simplify limited graphs
512 * by eliminating side branches.
514 * A "relevant" commit is one that is !UNINTERESTING (ie we are including it
515 * in our list), or that is a specified BOTTOM commit. Then after computing
516 * a limited list, during processing we can generally ignore boundary merges
517 * coming from outside the graph, (ie from irrelevant parents), and treat
518 * those merges as if they were single-parent. TREESAME is defined to consider
519 * only relevant parents, if any. If we are TREESAME to our on-graph parents,
520 * we don't care if we were !TREESAME to non-graph parents.
522 * Treating bottom commits as relevant ensures that a limited graph's
523 * connection to the actual bottom commit is not viewed as a side branch, but
524 * treated as part of the graph. For example:
526 * ....Z...A---X---o---o---B
530 * When computing "A..B", the A-X connection is at least as important as
531 * Y-X, despite A being flagged UNINTERESTING.
533 * And when computing --ancestry-path "A..B", the A-X connection is more
534 * important than Y-X, despite both A and Y being flagged UNINTERESTING.
536 static inline int relevant_commit(struct commit
*commit
)
538 return (commit
->object
.flags
& (UNINTERESTING
| BOTTOM
)) != UNINTERESTING
;
542 * Return a single relevant commit from a parent list. If we are a TREESAME
543 * commit, and this selects one of our parents, then we can safely simplify to
546 static struct commit
*one_relevant_parent(const struct rev_info
*revs
,
547 struct commit_list
*orig
)
549 struct commit_list
*list
= orig
;
550 struct commit
*relevant
= NULL
;
556 * For 1-parent commits, or if first-parent-only, then return that
557 * first parent (even if not "relevant" by the above definition).
558 * TREESAME will have been set purely on that parent.
560 if (revs
->first_parent_only
|| !orig
->next
)
564 * For multi-parent commits, identify a sole relevant parent, if any.
565 * If we have only one relevant parent, then TREESAME will be set purely
566 * with regard to that parent, and we can simplify accordingly.
568 * If we have more than one relevant parent, or no relevant parents
569 * (and multiple irrelevant ones), then we can't select a parent here
573 struct commit
*commit
= list
->item
;
575 if (relevant_commit(commit
)) {
585 * The goal is to get REV_TREE_NEW as the result only if the
586 * diff consists of all '+' (and no other changes), REV_TREE_OLD
587 * if the whole diff is removal of old data, and otherwise
588 * REV_TREE_DIFFERENT (of course if the trees are the same we
589 * want REV_TREE_SAME).
591 * The only time we care about the distinction is when
592 * remove_empty_trees is in effect, in which case we care only about
593 * whether the whole change is REV_TREE_NEW, or if there's another type
594 * of change. Which means we can stop the diff early in either of these
597 * 1. We're not using remove_empty_trees at all.
599 * 2. We saw anything except REV_TREE_NEW.
601 #define REV_TREE_SAME 0
602 #define REV_TREE_NEW 1 /* Only new files */
603 #define REV_TREE_OLD 2 /* Only files removed */
604 #define REV_TREE_DIFFERENT 3 /* Mixed changes */
605 static int tree_difference
= REV_TREE_SAME
;
607 static void file_add_remove(struct diff_options
*options
,
609 unsigned mode UNUSED
,
610 const struct object_id
*oid UNUSED
,
611 int oid_valid UNUSED
,
612 const char *fullpath UNUSED
,
613 unsigned dirty_submodule UNUSED
)
615 int diff
= addremove
== '+' ? REV_TREE_NEW
: REV_TREE_OLD
;
616 struct rev_info
*revs
= options
->change_fn_data
;
618 tree_difference
|= diff
;
619 if (!revs
->remove_empty_trees
|| tree_difference
!= REV_TREE_NEW
)
620 options
->flags
.has_changes
= 1;
623 static void file_change(struct diff_options
*options
,
624 unsigned old_mode UNUSED
,
625 unsigned new_mode UNUSED
,
626 const struct object_id
*old_oid UNUSED
,
627 const struct object_id
*new_oid UNUSED
,
628 int old_oid_valid UNUSED
,
629 int new_oid_valid UNUSED
,
630 const char *fullpath UNUSED
,
631 unsigned old_dirty_submodule UNUSED
,
632 unsigned new_dirty_submodule UNUSED
)
634 tree_difference
= REV_TREE_DIFFERENT
;
635 options
->flags
.has_changes
= 1;
638 static int bloom_filter_atexit_registered
;
639 static unsigned int count_bloom_filter_maybe
;
640 static unsigned int count_bloom_filter_definitely_not
;
641 static unsigned int count_bloom_filter_false_positive
;
642 static unsigned int count_bloom_filter_not_present
;
644 static void trace2_bloom_filter_statistics_atexit(void)
646 struct json_writer jw
= JSON_WRITER_INIT
;
648 jw_object_begin(&jw
, 0);
649 jw_object_intmax(&jw
, "filter_not_present", count_bloom_filter_not_present
);
650 jw_object_intmax(&jw
, "maybe", count_bloom_filter_maybe
);
651 jw_object_intmax(&jw
, "definitely_not", count_bloom_filter_definitely_not
);
652 jw_object_intmax(&jw
, "false_positive", count_bloom_filter_false_positive
);
655 trace2_data_json("bloom", the_repository
, "statistics", &jw
);
660 static int forbid_bloom_filters(struct pathspec
*spec
)
662 if (spec
->has_wildcard
)
666 if (spec
->magic
& ~PATHSPEC_LITERAL
)
668 if (spec
->nr
&& (spec
->items
[0].magic
& ~PATHSPEC_LITERAL
))
674 static void prepare_to_use_bloom_filter(struct rev_info
*revs
)
676 struct pathspec_item
*pi
;
677 char *path_alloc
= NULL
;
678 const char *path
, *p
;
680 int path_component_nr
= 1;
685 if (forbid_bloom_filters(&revs
->prune_data
))
688 repo_parse_commit(revs
->repo
, revs
->commits
->item
);
690 revs
->bloom_filter_settings
= get_bloom_filter_settings(revs
->repo
);
691 if (!revs
->bloom_filter_settings
)
694 if (!revs
->pruning
.pathspec
.nr
)
697 pi
= &revs
->pruning
.pathspec
.items
[0];
699 /* remove single trailing slash from path, if needed */
700 if (pi
->len
> 0 && pi
->match
[pi
->len
- 1] == '/') {
701 path_alloc
= xmemdupz(pi
->match
, pi
->len
- 1);
708 revs
->bloom_filter_settings
= NULL
;
716 * At this point, the path is normalized to use Unix-style
717 * path separators. This is required due to how the
718 * changed-path Bloom filters store the paths.
725 revs
->bloom_keys_nr
= path_component_nr
;
726 ALLOC_ARRAY(revs
->bloom_keys
, revs
->bloom_keys_nr
);
728 fill_bloom_key(path
, len
, &revs
->bloom_keys
[0],
729 revs
->bloom_filter_settings
);
730 path_component_nr
= 1;
735 fill_bloom_key(path
, p
- path
,
736 &revs
->bloom_keys
[path_component_nr
++],
737 revs
->bloom_filter_settings
);
741 if (trace2_is_enabled() && !bloom_filter_atexit_registered
) {
742 atexit(trace2_bloom_filter_statistics_atexit
);
743 bloom_filter_atexit_registered
= 1;
749 static int check_maybe_different_in_bloom_filter(struct rev_info
*revs
,
750 struct commit
*commit
)
752 struct bloom_filter
*filter
;
755 if (!revs
->repo
->objects
->commit_graph
)
758 if (commit_graph_generation(commit
) == GENERATION_NUMBER_INFINITY
)
761 filter
= get_bloom_filter(revs
->repo
, commit
);
764 count_bloom_filter_not_present
++;
768 for (j
= 0; result
&& j
< revs
->bloom_keys_nr
; j
++) {
769 result
= bloom_filter_contains(filter
,
770 &revs
->bloom_keys
[j
],
771 revs
->bloom_filter_settings
);
775 count_bloom_filter_maybe
++;
777 count_bloom_filter_definitely_not
++;
782 static int rev_compare_tree(struct rev_info
*revs
,
783 struct commit
*parent
, struct commit
*commit
, int nth_parent
)
785 struct tree
*t1
= get_commit_tree(parent
);
786 struct tree
*t2
= get_commit_tree(commit
);
794 if (revs
->simplify_by_decoration
) {
796 * If we are simplifying by decoration, then the commit
797 * is worth showing if it has a tag pointing at it.
799 if (get_name_decoration(&commit
->object
))
800 return REV_TREE_DIFFERENT
;
802 * A commit that is not pointed by a tag is uninteresting
803 * if we are not limited by path. This means that you will
804 * see the usual "commits that touch the paths" plus any
805 * tagged commit by specifying both --simplify-by-decoration
808 if (!revs
->prune_data
.nr
)
809 return REV_TREE_SAME
;
812 if (revs
->bloom_keys_nr
&& !nth_parent
) {
813 bloom_ret
= check_maybe_different_in_bloom_filter(revs
, commit
);
816 return REV_TREE_SAME
;
819 tree_difference
= REV_TREE_SAME
;
820 revs
->pruning
.flags
.has_changes
= 0;
821 diff_tree_oid(&t1
->object
.oid
, &t2
->object
.oid
, "", &revs
->pruning
);
824 if (bloom_ret
== 1 && tree_difference
== REV_TREE_SAME
)
825 count_bloom_filter_false_positive
++;
827 return tree_difference
;
830 static int rev_same_tree_as_empty(struct rev_info
*revs
, struct commit
*commit
)
832 struct tree
*t1
= get_commit_tree(commit
);
837 tree_difference
= REV_TREE_SAME
;
838 revs
->pruning
.flags
.has_changes
= 0;
839 diff_tree_oid(NULL
, &t1
->object
.oid
, "", &revs
->pruning
);
841 return tree_difference
== REV_TREE_SAME
;
844 struct treesame_state
{
845 unsigned int nparents
;
846 unsigned char treesame
[FLEX_ARRAY
];
849 static struct treesame_state
*initialise_treesame(struct rev_info
*revs
, struct commit
*commit
)
851 unsigned n
= commit_list_count(commit
->parents
);
852 struct treesame_state
*st
= xcalloc(1, st_add(sizeof(*st
), n
));
854 add_decoration(&revs
->treesame
, &commit
->object
, st
);
859 * Must be called immediately after removing the nth_parent from a commit's
860 * parent list, if we are maintaining the per-parent treesame[] decoration.
861 * This does not recalculate the master TREESAME flag - update_treesame()
862 * should be called to update it after a sequence of treesame[] modifications
863 * that may have affected it.
865 static int compact_treesame(struct rev_info
*revs
, struct commit
*commit
, unsigned nth_parent
)
867 struct treesame_state
*st
;
870 if (!commit
->parents
) {
872 * Have just removed the only parent from a non-merge.
873 * Different handling, as we lack decoration.
876 die("compact_treesame %u", nth_parent
);
877 old_same
= !!(commit
->object
.flags
& TREESAME
);
878 if (rev_same_tree_as_empty(revs
, commit
))
879 commit
->object
.flags
|= TREESAME
;
881 commit
->object
.flags
&= ~TREESAME
;
885 st
= lookup_decoration(&revs
->treesame
, &commit
->object
);
886 if (!st
|| nth_parent
>= st
->nparents
)
887 die("compact_treesame %u", nth_parent
);
889 old_same
= st
->treesame
[nth_parent
];
890 memmove(st
->treesame
+ nth_parent
,
891 st
->treesame
+ nth_parent
+ 1,
892 st
->nparents
- nth_parent
- 1);
895 * If we've just become a non-merge commit, update TREESAME
896 * immediately, and remove the no-longer-needed decoration.
897 * If still a merge, defer update until update_treesame().
899 if (--st
->nparents
== 1) {
900 if (commit
->parents
->next
)
901 die("compact_treesame parents mismatch");
902 if (st
->treesame
[0] && revs
->dense
)
903 commit
->object
.flags
|= TREESAME
;
905 commit
->object
.flags
&= ~TREESAME
;
906 free(add_decoration(&revs
->treesame
, &commit
->object
, NULL
));
912 static unsigned update_treesame(struct rev_info
*revs
, struct commit
*commit
)
914 if (commit
->parents
&& commit
->parents
->next
) {
916 struct treesame_state
*st
;
917 struct commit_list
*p
;
918 unsigned relevant_parents
;
919 unsigned relevant_change
, irrelevant_change
;
921 st
= lookup_decoration(&revs
->treesame
, &commit
->object
);
923 die("update_treesame %s", oid_to_hex(&commit
->object
.oid
));
924 relevant_parents
= 0;
925 relevant_change
= irrelevant_change
= 0;
926 for (p
= commit
->parents
, n
= 0; p
; n
++, p
= p
->next
) {
927 if (relevant_commit(p
->item
)) {
928 relevant_change
|= !st
->treesame
[n
];
931 irrelevant_change
|= !st
->treesame
[n
];
933 if (relevant_parents
? relevant_change
: irrelevant_change
)
934 commit
->object
.flags
&= ~TREESAME
;
936 commit
->object
.flags
|= TREESAME
;
939 return commit
->object
.flags
& TREESAME
;
942 static inline int limiting_can_increase_treesame(const struct rev_info
*revs
)
945 * TREESAME is irrelevant unless prune && dense;
946 * if simplify_history is set, we can't have a mixture of TREESAME and
947 * !TREESAME INTERESTING parents (and we don't have treesame[]
948 * decoration anyway);
949 * if first_parent_only is set, then the TREESAME flag is locked
950 * against the first parent (and again we lack treesame[] decoration).
952 return revs
->prune
&& revs
->dense
&&
953 !revs
->simplify_history
&&
954 !revs
->first_parent_only
;
957 static void try_to_simplify_commit(struct rev_info
*revs
, struct commit
*commit
)
959 struct commit_list
**pp
, *parent
;
960 struct treesame_state
*ts
= NULL
;
961 int relevant_change
= 0, irrelevant_change
= 0;
962 int relevant_parents
, nth_parent
;
965 * If we don't do pruning, everything is interesting
970 if (!get_commit_tree(commit
))
973 if (!commit
->parents
) {
974 if (rev_same_tree_as_empty(revs
, commit
))
975 commit
->object
.flags
|= TREESAME
;
980 * Normal non-merge commit? If we don't want to make the
981 * history dense, we consider it always to be a change..
983 if (!revs
->dense
&& !commit
->parents
->next
)
986 for (pp
= &commit
->parents
, nth_parent
= 0, relevant_parents
= 0;
987 (parent
= *pp
) != NULL
;
988 pp
= &parent
->next
, nth_parent
++) {
989 struct commit
*p
= parent
->item
;
990 if (relevant_commit(p
))
993 if (nth_parent
== 1) {
995 * This our second loop iteration - so we now know
996 * we're dealing with a merge.
998 * Do not compare with later parents when we care only about
999 * the first parent chain, in order to avoid derailing the
1000 * traversal to follow a side branch that brought everything
1001 * in the path we are limited to by the pathspec.
1003 if (revs
->first_parent_only
)
1006 * If this will remain a potentially-simplifiable
1007 * merge, remember per-parent treesame if needed.
1008 * Initialise the array with the comparison from our
1011 if (revs
->treesame
.name
&&
1012 !revs
->simplify_history
&&
1013 !(commit
->object
.flags
& UNINTERESTING
)) {
1014 ts
= initialise_treesame(revs
, commit
);
1015 if (!(irrelevant_change
|| relevant_change
))
1016 ts
->treesame
[0] = 1;
1019 if (repo_parse_commit(revs
->repo
, p
) < 0)
1020 die("cannot simplify commit %s (because of %s)",
1021 oid_to_hex(&commit
->object
.oid
),
1022 oid_to_hex(&p
->object
.oid
));
1023 switch (rev_compare_tree(revs
, p
, commit
, nth_parent
)) {
1025 if (!revs
->simplify_history
|| !relevant_commit(p
)) {
1026 /* Even if a merge with an uninteresting
1027 * side branch brought the entire change
1028 * we are interested in, we do not want
1029 * to lose the other branches of this
1030 * merge, so we just keep going.
1033 ts
->treesame
[nth_parent
] = 1;
1036 parent
->next
= NULL
;
1037 commit
->parents
= parent
;
1040 * A merge commit is a "diversion" if it is not
1041 * TREESAME to its first parent but is TREESAME
1042 * to a later parent. In the simplified history,
1043 * we "divert" the history walk to the later
1044 * parent. These commits are shown when "show_pulls"
1045 * is enabled, so do not mark the object as
1048 if (!revs
->show_pulls
|| !nth_parent
)
1049 commit
->object
.flags
|= TREESAME
;
1054 if (revs
->remove_empty_trees
&&
1055 rev_same_tree_as_empty(revs
, p
)) {
1056 /* We are adding all the specified
1057 * paths from this parent, so the
1058 * history beyond this parent is not
1059 * interesting. Remove its parents
1060 * (they are grandparents for us).
1061 * IOW, we pretend this parent is a
1064 if (repo_parse_commit(revs
->repo
, p
) < 0)
1065 die("cannot simplify commit %s (invalid %s)",
1066 oid_to_hex(&commit
->object
.oid
),
1067 oid_to_hex(&p
->object
.oid
));
1072 case REV_TREE_DIFFERENT
:
1073 if (relevant_commit(p
))
1074 relevant_change
= 1;
1076 irrelevant_change
= 1;
1079 commit
->object
.flags
|= PULL_MERGE
;
1083 die("bad tree compare for commit %s", oid_to_hex(&commit
->object
.oid
));
1087 * TREESAME is straightforward for single-parent commits. For merge
1088 * commits, it is most useful to define it so that "irrelevant"
1089 * parents cannot make us !TREESAME - if we have any relevant
1090 * parents, then we only consider TREESAMEness with respect to them,
1091 * allowing irrelevant merges from uninteresting branches to be
1092 * simplified away. Only if we have only irrelevant parents do we
1093 * base TREESAME on them. Note that this logic is replicated in
1094 * update_treesame, which should be kept in sync.
1096 if (relevant_parents
? !relevant_change
: !irrelevant_change
)
1097 commit
->object
.flags
|= TREESAME
;
1100 static int process_parents(struct rev_info
*revs
, struct commit
*commit
,
1101 struct commit_list
**list
, struct prio_queue
*queue
)
1103 struct commit_list
*parent
= commit
->parents
;
1104 unsigned pass_flags
;
1106 if (commit
->object
.flags
& ADDED
)
1108 commit
->object
.flags
|= ADDED
;
1110 if (revs
->include_check
&&
1111 !revs
->include_check(commit
, revs
->include_check_data
))
1115 * If the commit is uninteresting, don't try to
1116 * prune parents - we want the maximal uninteresting
1119 * Normally we haven't parsed the parent
1120 * yet, so we won't have a parent of a parent
1121 * here. However, it may turn out that we've
1122 * reached this commit some other way (where it
1123 * wasn't uninteresting), in which case we need
1124 * to mark its parents recursively too..
1126 if (commit
->object
.flags
& UNINTERESTING
) {
1128 struct commit
*p
= parent
->item
;
1129 parent
= parent
->next
;
1131 p
->object
.flags
|= UNINTERESTING
;
1132 if (repo_parse_commit_gently(revs
->repo
, p
, 1) < 0)
1135 mark_parents_uninteresting(revs
, p
);
1136 if (p
->object
.flags
& SEEN
)
1138 p
->object
.flags
|= (SEEN
| NOT_USER_GIVEN
);
1140 commit_list_insert_by_date(p
, list
);
1142 prio_queue_put(queue
, p
);
1143 if (revs
->exclude_first_parent_only
)
1150 * Ok, the commit wasn't uninteresting. Try to
1151 * simplify the commit history and find the parent
1152 * that has no differences in the path set if one exists.
1154 try_to_simplify_commit(revs
, commit
);
1159 pass_flags
= (commit
->object
.flags
& (SYMMETRIC_LEFT
| ANCESTRY_PATH
));
1161 for (parent
= commit
->parents
; parent
; parent
= parent
->next
) {
1162 struct commit
*p
= parent
->item
;
1163 int gently
= revs
->ignore_missing_links
||
1164 revs
->exclude_promisor_objects
;
1165 if (repo_parse_commit_gently(revs
->repo
, p
, gently
) < 0) {
1166 if (revs
->exclude_promisor_objects
&&
1167 is_promisor_object(&p
->object
.oid
)) {
1168 if (revs
->first_parent_only
)
1174 if (revs
->sources
) {
1175 char **slot
= revision_sources_at(revs
->sources
, p
);
1178 *slot
= *revision_sources_at(revs
->sources
, commit
);
1180 p
->object
.flags
|= pass_flags
;
1181 if (!(p
->object
.flags
& SEEN
)) {
1182 p
->object
.flags
|= (SEEN
| NOT_USER_GIVEN
);
1184 commit_list_insert_by_date(p
, list
);
1186 prio_queue_put(queue
, p
);
1188 if (revs
->first_parent_only
)
1194 static void cherry_pick_list(struct commit_list
*list
, struct rev_info
*revs
)
1196 struct commit_list
*p
;
1197 int left_count
= 0, right_count
= 0;
1199 struct patch_ids ids
;
1200 unsigned cherry_flag
;
1202 /* First count the commits on the left and on the right */
1203 for (p
= list
; p
; p
= p
->next
) {
1204 struct commit
*commit
= p
->item
;
1205 unsigned flags
= commit
->object
.flags
;
1206 if (flags
& BOUNDARY
)
1208 else if (flags
& SYMMETRIC_LEFT
)
1214 if (!left_count
|| !right_count
)
1217 left_first
= left_count
< right_count
;
1218 init_patch_ids(revs
->repo
, &ids
);
1219 ids
.diffopts
.pathspec
= revs
->diffopt
.pathspec
;
1221 /* Compute patch-ids for one side */
1222 for (p
= list
; p
; p
= p
->next
) {
1223 struct commit
*commit
= p
->item
;
1224 unsigned flags
= commit
->object
.flags
;
1226 if (flags
& BOUNDARY
)
1229 * If we have fewer left, left_first is set and we omit
1230 * commits on the right branch in this loop. If we have
1231 * fewer right, we skip the left ones.
1233 if (left_first
!= !!(flags
& SYMMETRIC_LEFT
))
1235 add_commit_patch_id(commit
, &ids
);
1238 /* either cherry_mark or cherry_pick are true */
1239 cherry_flag
= revs
->cherry_mark
? PATCHSAME
: SHOWN
;
1241 /* Check the other side */
1242 for (p
= list
; p
; p
= p
->next
) {
1243 struct commit
*commit
= p
->item
;
1244 struct patch_id
*id
;
1245 unsigned flags
= commit
->object
.flags
;
1247 if (flags
& BOUNDARY
)
1250 * If we have fewer left, left_first is set and we omit
1251 * commits on the left branch in this loop.
1253 if (left_first
== !!(flags
& SYMMETRIC_LEFT
))
1257 * Have we seen the same patch id?
1259 id
= patch_id_iter_first(commit
, &ids
);
1263 commit
->object
.flags
|= cherry_flag
;
1265 id
->commit
->object
.flags
|= cherry_flag
;
1266 } while ((id
= patch_id_iter_next(id
, &ids
)));
1269 free_patch_ids(&ids
);
1272 /* How many extra uninteresting commits we want to see.. */
1275 static int still_interesting(struct commit_list
*src
, timestamp_t date
, int slop
,
1276 struct commit
**interesting_cache
)
1279 * No source list at all? We're definitely done..
1285 * Does the destination list contain entries with a date
1286 * before the source list? Definitely _not_ done.
1288 if (date
<= src
->item
->date
)
1292 * Does the source list still have interesting commits in
1293 * it? Definitely not done..
1295 if (!everybody_uninteresting(src
, interesting_cache
))
1298 /* Ok, we're closing in.. */
1303 * "rev-list --ancestry-path=C_0 [--ancestry-path=C_1 ...] A..B"
1304 * computes commits that are ancestors of B but not ancestors of A but
1305 * further limits the result to those that have any of C in their
1306 * ancestry path (i.e. are either ancestors of any of C, descendants
1307 * of any of C, or are any of C). If --ancestry-path is specified with
1308 * no commit, we use all bottom commits for C.
1310 * Before this function is called, ancestors of C will have already
1311 * been marked with ANCESTRY_PATH previously.
1313 * This takes the list of bottom commits and the result of "A..B"
1314 * without --ancestry-path, and limits the latter further to the ones
1315 * that have any of C in their ancestry path. Since the ancestors of C
1316 * have already been marked (a prerequisite of this function), we just
1317 * need to mark the descendants, then exclude any commit that does not
1318 * have any of these marks.
1320 static void limit_to_ancestry(struct commit_list
*bottoms
, struct commit_list
*list
)
1322 struct commit_list
*p
;
1323 struct commit_list
*rlist
= NULL
;
1327 * Reverse the list so that it will be likely that we would
1328 * process parents before children.
1330 for (p
= list
; p
; p
= p
->next
)
1331 commit_list_insert(p
->item
, &rlist
);
1333 for (p
= bottoms
; p
; p
= p
->next
)
1334 p
->item
->object
.flags
|= TMP_MARK
;
1337 * Mark the ones that can reach bottom commits in "list",
1338 * in a bottom-up fashion.
1342 for (p
= rlist
; p
; p
= p
->next
) {
1343 struct commit
*c
= p
->item
;
1344 struct commit_list
*parents
;
1345 if (c
->object
.flags
& (TMP_MARK
| UNINTERESTING
))
1347 for (parents
= c
->parents
;
1349 parents
= parents
->next
) {
1350 if (!(parents
->item
->object
.flags
& TMP_MARK
))
1352 c
->object
.flags
|= TMP_MARK
;
1357 } while (made_progress
);
1360 * NEEDSWORK: decide if we want to remove parents that are
1361 * not marked with TMP_MARK from commit->parents for commits
1362 * in the resulting list. We may not want to do that, though.
1366 * The ones that are not marked with either TMP_MARK or
1367 * ANCESTRY_PATH are uninteresting
1369 for (p
= list
; p
; p
= p
->next
) {
1370 struct commit
*c
= p
->item
;
1371 if (c
->object
.flags
& (TMP_MARK
| ANCESTRY_PATH
))
1373 c
->object
.flags
|= UNINTERESTING
;
1376 /* We are done with TMP_MARK and ANCESTRY_PATH */
1377 for (p
= list
; p
; p
= p
->next
)
1378 p
->item
->object
.flags
&= ~(TMP_MARK
| ANCESTRY_PATH
);
1379 for (p
= bottoms
; p
; p
= p
->next
)
1380 p
->item
->object
.flags
&= ~(TMP_MARK
| ANCESTRY_PATH
);
1381 free_commit_list(rlist
);
1385 * Before walking the history, add the set of "negative" refs the
1386 * caller has asked to exclude to the bottom list.
1388 * This is used to compute "rev-list --ancestry-path A..B", as we need
1389 * to filter the result of "A..B" further to the ones that can actually
1392 static void collect_bottom_commits(struct commit_list
*list
,
1393 struct commit_list
**bottom
)
1395 struct commit_list
*elem
;
1396 for (elem
= list
; elem
; elem
= elem
->next
)
1397 if (elem
->item
->object
.flags
& BOTTOM
)
1398 commit_list_insert(elem
->item
, bottom
);
1401 /* Assumes either left_only or right_only is set */
1402 static void limit_left_right(struct commit_list
*list
, struct rev_info
*revs
)
1404 struct commit_list
*p
;
1406 for (p
= list
; p
; p
= p
->next
) {
1407 struct commit
*commit
= p
->item
;
1409 if (revs
->right_only
) {
1410 if (commit
->object
.flags
& SYMMETRIC_LEFT
)
1411 commit
->object
.flags
|= SHOWN
;
1412 } else /* revs->left_only is set */
1413 if (!(commit
->object
.flags
& SYMMETRIC_LEFT
))
1414 commit
->object
.flags
|= SHOWN
;
1418 static int limit_list(struct rev_info
*revs
)
1421 timestamp_t date
= TIME_MAX
;
1422 struct commit_list
*original_list
= revs
->commits
;
1423 struct commit_list
*newlist
= NULL
;
1424 struct commit_list
**p
= &newlist
;
1425 struct commit
*interesting_cache
= NULL
;
1427 if (revs
->ancestry_path_implicit_bottoms
) {
1428 collect_bottom_commits(original_list
,
1429 &revs
->ancestry_path_bottoms
);
1430 if (!revs
->ancestry_path_bottoms
)
1431 die("--ancestry-path given but there are no bottom commits");
1434 while (original_list
) {
1435 struct commit
*commit
= pop_commit(&original_list
);
1436 struct object
*obj
= &commit
->object
;
1437 show_early_output_fn_t show
;
1439 if (commit
== interesting_cache
)
1440 interesting_cache
= NULL
;
1442 if (revs
->max_age
!= -1 && (commit
->date
< revs
->max_age
))
1443 obj
->flags
|= UNINTERESTING
;
1444 if (process_parents(revs
, commit
, &original_list
, NULL
) < 0)
1446 if (obj
->flags
& UNINTERESTING
) {
1447 mark_parents_uninteresting(revs
, commit
);
1448 slop
= still_interesting(original_list
, date
, slop
, &interesting_cache
);
1453 if (revs
->min_age
!= -1 && (commit
->date
> revs
->min_age
) &&
1454 !revs
->line_level_traverse
)
1456 if (revs
->max_age_as_filter
!= -1 &&
1457 (commit
->date
< revs
->max_age_as_filter
) && !revs
->line_level_traverse
)
1459 date
= commit
->date
;
1460 p
= &commit_list_insert(commit
, p
)->next
;
1462 show
= show_early_output
;
1466 show(revs
, newlist
);
1467 show_early_output
= NULL
;
1469 if (revs
->cherry_pick
|| revs
->cherry_mark
)
1470 cherry_pick_list(newlist
, revs
);
1472 if (revs
->left_only
|| revs
->right_only
)
1473 limit_left_right(newlist
, revs
);
1475 if (revs
->ancestry_path
)
1476 limit_to_ancestry(revs
->ancestry_path_bottoms
, newlist
);
1479 * Check if any commits have become TREESAME by some of their parents
1480 * becoming UNINTERESTING.
1482 if (limiting_can_increase_treesame(revs
)) {
1483 struct commit_list
*list
= NULL
;
1484 for (list
= newlist
; list
; list
= list
->next
) {
1485 struct commit
*c
= list
->item
;
1486 if (c
->object
.flags
& (UNINTERESTING
| TREESAME
))
1488 update_treesame(revs
, c
);
1492 free_commit_list(original_list
);
1493 revs
->commits
= newlist
;
1498 * Add an entry to refs->cmdline with the specified information.
1501 static void add_rev_cmdline(struct rev_info
*revs
,
1502 struct object
*item
,
1507 struct rev_cmdline_info
*info
= &revs
->cmdline
;
1508 unsigned int nr
= info
->nr
;
1510 ALLOC_GROW(info
->rev
, nr
+ 1, info
->alloc
);
1511 info
->rev
[nr
].item
= item
;
1512 info
->rev
[nr
].name
= xstrdup(name
);
1513 info
->rev
[nr
].whence
= whence
;
1514 info
->rev
[nr
].flags
= flags
;
1518 static void add_rev_cmdline_list(struct rev_info
*revs
,
1519 struct commit_list
*commit_list
,
1523 while (commit_list
) {
1524 struct object
*object
= &commit_list
->item
->object
;
1525 add_rev_cmdline(revs
, object
, oid_to_hex(&object
->oid
),
1527 commit_list
= commit_list
->next
;
1531 int ref_excluded(const struct ref_exclusions
*exclusions
, const char *path
)
1533 const char *stripped_path
= strip_namespace(path
);
1534 struct string_list_item
*item
;
1536 for_each_string_list_item(item
, &exclusions
->excluded_refs
) {
1537 if (!wildmatch(item
->string
, path
, 0))
1541 if (ref_is_hidden(stripped_path
, path
, &exclusions
->hidden_refs
))
1547 void init_ref_exclusions(struct ref_exclusions
*exclusions
)
1549 struct ref_exclusions blank
= REF_EXCLUSIONS_INIT
;
1550 memcpy(exclusions
, &blank
, sizeof(*exclusions
));
1553 void clear_ref_exclusions(struct ref_exclusions
*exclusions
)
1555 string_list_clear(&exclusions
->excluded_refs
, 0);
1556 string_list_clear(&exclusions
->hidden_refs
, 0);
1557 exclusions
->hidden_refs_configured
= 0;
1560 void add_ref_exclusion(struct ref_exclusions
*exclusions
, const char *exclude
)
1562 string_list_append(&exclusions
->excluded_refs
, exclude
);
1565 struct exclude_hidden_refs_cb
{
1566 struct ref_exclusions
*exclusions
;
1567 const char *section
;
1570 static int hide_refs_config(const char *var
, const char *value
, void *cb_data
)
1572 struct exclude_hidden_refs_cb
*cb
= cb_data
;
1573 cb
->exclusions
->hidden_refs_configured
= 1;
1574 return parse_hide_refs_config(var
, value
, cb
->section
,
1575 &cb
->exclusions
->hidden_refs
);
1578 void exclude_hidden_refs(struct ref_exclusions
*exclusions
, const char *section
)
1580 struct exclude_hidden_refs_cb cb
;
1582 if (strcmp(section
, "fetch") && strcmp(section
, "receive") &&
1583 strcmp(section
, "uploadpack"))
1584 die(_("unsupported section for hidden refs: %s"), section
);
1586 if (exclusions
->hidden_refs_configured
)
1587 die(_("--exclude-hidden= passed more than once"));
1589 cb
.exclusions
= exclusions
;
1590 cb
.section
= section
;
1592 git_config(hide_refs_config
, &cb
);
1595 struct all_refs_cb
{
1597 int warned_bad_reflog
;
1598 struct rev_info
*all_revs
;
1599 const char *name_for_errormsg
;
1600 struct worktree
*wt
;
1603 static int handle_one_ref(const char *path
, const struct object_id
*oid
,
1607 struct all_refs_cb
*cb
= cb_data
;
1608 struct object
*object
;
1610 if (ref_excluded(&cb
->all_revs
->ref_excludes
, path
))
1613 object
= get_reference(cb
->all_revs
, path
, oid
, cb
->all_flags
);
1614 add_rev_cmdline(cb
->all_revs
, object
, path
, REV_CMD_REF
, cb
->all_flags
);
1615 add_pending_object(cb
->all_revs
, object
, path
);
1619 static void init_all_refs_cb(struct all_refs_cb
*cb
, struct rev_info
*revs
,
1622 cb
->all_revs
= revs
;
1623 cb
->all_flags
= flags
;
1624 revs
->rev_input_given
= 1;
1628 static void handle_refs(struct ref_store
*refs
,
1629 struct rev_info
*revs
, unsigned flags
,
1630 int (*for_each
)(struct ref_store
*, each_ref_fn
, void *))
1632 struct all_refs_cb cb
;
1635 /* this could happen with uninitialized submodules */
1639 init_all_refs_cb(&cb
, revs
, flags
);
1640 for_each(refs
, handle_one_ref
, &cb
);
1643 static void handle_one_reflog_commit(struct object_id
*oid
, void *cb_data
)
1645 struct all_refs_cb
*cb
= cb_data
;
1646 if (!is_null_oid(oid
)) {
1647 struct object
*o
= parse_object(cb
->all_revs
->repo
, oid
);
1649 o
->flags
|= cb
->all_flags
;
1650 /* ??? CMDLINEFLAGS ??? */
1651 add_pending_object(cb
->all_revs
, o
, "");
1653 else if (!cb
->warned_bad_reflog
) {
1654 warning("reflog of '%s' references pruned commits",
1655 cb
->name_for_errormsg
);
1656 cb
->warned_bad_reflog
= 1;
1661 static int handle_one_reflog_ent(struct object_id
*ooid
, struct object_id
*noid
,
1662 const char *email UNUSED
,
1663 timestamp_t timestamp UNUSED
,
1665 const char *message UNUSED
,
1668 handle_one_reflog_commit(ooid
, cb_data
);
1669 handle_one_reflog_commit(noid
, cb_data
);
1673 static int handle_one_reflog(const char *refname_in_wt
,
1674 const struct object_id
*oid UNUSED
,
1675 int flag UNUSED
, void *cb_data
)
1677 struct all_refs_cb
*cb
= cb_data
;
1678 struct strbuf refname
= STRBUF_INIT
;
1680 cb
->warned_bad_reflog
= 0;
1681 strbuf_worktree_ref(cb
->wt
, &refname
, refname_in_wt
);
1682 cb
->name_for_errormsg
= refname
.buf
;
1683 refs_for_each_reflog_ent(get_main_ref_store(the_repository
),
1685 handle_one_reflog_ent
, cb_data
);
1686 strbuf_release(&refname
);
1690 static void add_other_reflogs_to_pending(struct all_refs_cb
*cb
)
1692 struct worktree
**worktrees
, **p
;
1694 worktrees
= get_worktrees();
1695 for (p
= worktrees
; *p
; p
++) {
1696 struct worktree
*wt
= *p
;
1702 refs_for_each_reflog(get_worktree_ref_store(wt
),
1706 free_worktrees(worktrees
);
1709 void add_reflogs_to_pending(struct rev_info
*revs
, unsigned flags
)
1711 struct all_refs_cb cb
;
1714 cb
.all_flags
= flags
;
1716 for_each_reflog(handle_one_reflog
, &cb
);
1718 if (!revs
->single_worktree
)
1719 add_other_reflogs_to_pending(&cb
);
1722 static void add_cache_tree(struct cache_tree
*it
, struct rev_info
*revs
,
1723 struct strbuf
*path
, unsigned int flags
)
1725 size_t baselen
= path
->len
;
1728 if (it
->entry_count
>= 0) {
1729 struct tree
*tree
= lookup_tree(revs
->repo
, &it
->oid
);
1730 tree
->object
.flags
|= flags
;
1731 add_pending_object_with_path(revs
, &tree
->object
, "",
1735 for (i
= 0; i
< it
->subtree_nr
; i
++) {
1736 struct cache_tree_sub
*sub
= it
->down
[i
];
1737 strbuf_addf(path
, "%s%s", baselen
? "/" : "", sub
->name
);
1738 add_cache_tree(sub
->cache_tree
, revs
, path
, flags
);
1739 strbuf_setlen(path
, baselen
);
1744 static void add_resolve_undo_to_pending(struct index_state
*istate
, struct rev_info
*revs
)
1746 struct string_list_item
*item
;
1747 struct string_list
*resolve_undo
= istate
->resolve_undo
;
1752 for_each_string_list_item(item
, resolve_undo
) {
1753 const char *path
= item
->string
;
1754 struct resolve_undo_info
*ru
= item
->util
;
1759 for (i
= 0; i
< 3; i
++) {
1762 if (!ru
->mode
[i
] || !S_ISREG(ru
->mode
[i
]))
1765 blob
= lookup_blob(revs
->repo
, &ru
->oid
[i
]);
1767 warning(_("resolve-undo records `%s` which is missing"),
1768 oid_to_hex(&ru
->oid
[i
]));
1771 add_pending_object_with_path(revs
, &blob
->object
, "",
1777 static void do_add_index_objects_to_pending(struct rev_info
*revs
,
1778 struct index_state
*istate
,
1783 /* TODO: audit for interaction with sparse-index. */
1784 ensure_full_index(istate
);
1785 for (i
= 0; i
< istate
->cache_nr
; i
++) {
1786 struct cache_entry
*ce
= istate
->cache
[i
];
1789 if (S_ISGITLINK(ce
->ce_mode
))
1792 blob
= lookup_blob(revs
->repo
, &ce
->oid
);
1794 die("unable to add index blob to traversal");
1795 blob
->object
.flags
|= flags
;
1796 add_pending_object_with_path(revs
, &blob
->object
, "",
1797 ce
->ce_mode
, ce
->name
);
1800 if (istate
->cache_tree
) {
1801 struct strbuf path
= STRBUF_INIT
;
1802 add_cache_tree(istate
->cache_tree
, revs
, &path
, flags
);
1803 strbuf_release(&path
);
1806 add_resolve_undo_to_pending(istate
, revs
);
1809 void add_index_objects_to_pending(struct rev_info
*revs
, unsigned int flags
)
1811 struct worktree
**worktrees
, **p
;
1813 repo_read_index(revs
->repo
);
1814 do_add_index_objects_to_pending(revs
, revs
->repo
->index
, flags
);
1816 if (revs
->single_worktree
)
1819 worktrees
= get_worktrees();
1820 for (p
= worktrees
; *p
; p
++) {
1821 struct worktree
*wt
= *p
;
1822 struct index_state istate
= INDEX_STATE_INIT(revs
->repo
);
1825 continue; /* current index already taken care of */
1827 if (read_index_from(&istate
,
1828 worktree_git_path(wt
, "index"),
1829 get_worktree_git_dir(wt
)) > 0)
1830 do_add_index_objects_to_pending(revs
, &istate
, flags
);
1831 discard_index(&istate
);
1833 free_worktrees(worktrees
);
1836 struct add_alternate_refs_data
{
1837 struct rev_info
*revs
;
1841 static void add_one_alternate_ref(const struct object_id
*oid
,
1844 const char *name
= ".alternate";
1845 struct add_alternate_refs_data
*data
= vdata
;
1848 obj
= get_reference(data
->revs
, name
, oid
, data
->flags
);
1849 add_rev_cmdline(data
->revs
, obj
, name
, REV_CMD_REV
, data
->flags
);
1850 add_pending_object(data
->revs
, obj
, name
);
1853 static void add_alternate_refs_to_pending(struct rev_info
*revs
,
1856 struct add_alternate_refs_data data
;
1859 for_each_alternate_ref(add_one_alternate_ref
, &data
);
1862 static int add_parents_only(struct rev_info
*revs
, const char *arg_
, int flags
,
1865 struct object_id oid
;
1867 struct commit
*commit
;
1868 struct commit_list
*parents
;
1870 const char *arg
= arg_
;
1873 flags
^= UNINTERESTING
| BOTTOM
;
1876 if (get_oid_committish(arg
, &oid
))
1879 it
= get_reference(revs
, arg
, &oid
, 0);
1880 if (!it
&& revs
->ignore_missing
)
1882 if (it
->type
!= OBJ_TAG
)
1884 if (!((struct tag
*)it
)->tagged
)
1886 oidcpy(&oid
, &((struct tag
*)it
)->tagged
->oid
);
1888 if (it
->type
!= OBJ_COMMIT
)
1890 commit
= (struct commit
*)it
;
1891 if (exclude_parent
&&
1892 exclude_parent
> commit_list_count(commit
->parents
))
1894 for (parents
= commit
->parents
, parent_number
= 1;
1896 parents
= parents
->next
, parent_number
++) {
1897 if (exclude_parent
&& parent_number
!= exclude_parent
)
1900 it
= &parents
->item
->object
;
1902 add_rev_cmdline(revs
, it
, arg_
, REV_CMD_PARENTS_ONLY
, flags
);
1903 add_pending_object(revs
, it
, arg
);
1908 void repo_init_revisions(struct repository
*r
,
1909 struct rev_info
*revs
,
1912 struct rev_info blank
= REV_INFO_INIT
;
1913 memcpy(revs
, &blank
, sizeof(*revs
));
1916 revs
->pruning
.repo
= r
;
1917 revs
->pruning
.add_remove
= file_add_remove
;
1918 revs
->pruning
.change
= file_change
;
1919 revs
->pruning
.change_fn_data
= revs
;
1920 revs
->prefix
= prefix
;
1922 grep_init(&revs
->grep_filter
, revs
->repo
);
1923 revs
->grep_filter
.status_only
= 1;
1925 repo_diff_setup(revs
->repo
, &revs
->diffopt
);
1926 if (prefix
&& !revs
->diffopt
.prefix
) {
1927 revs
->diffopt
.prefix
= prefix
;
1928 revs
->diffopt
.prefix_length
= strlen(prefix
);
1931 init_display_notes(&revs
->notes_opt
);
1932 list_objects_filter_init(&revs
->filter
);
1933 init_ref_exclusions(&revs
->ref_excludes
);
1936 static void add_pending_commit_list(struct rev_info
*revs
,
1937 struct commit_list
*commit_list
,
1940 while (commit_list
) {
1941 struct object
*object
= &commit_list
->item
->object
;
1942 object
->flags
|= flags
;
1943 add_pending_object(revs
, object
, oid_to_hex(&object
->oid
));
1944 commit_list
= commit_list
->next
;
1948 static void prepare_show_merge(struct rev_info
*revs
)
1950 struct commit_list
*bases
;
1951 struct commit
*head
, *other
;
1952 struct object_id oid
;
1953 const char **prune
= NULL
;
1954 int i
, prune_num
= 1; /* counting terminating NULL */
1955 struct index_state
*istate
= revs
->repo
->index
;
1957 if (get_oid("HEAD", &oid
))
1958 die("--merge without HEAD?");
1959 head
= lookup_commit_or_die(&oid
, "HEAD");
1960 if (get_oid("MERGE_HEAD", &oid
))
1961 die("--merge without MERGE_HEAD?");
1962 other
= lookup_commit_or_die(&oid
, "MERGE_HEAD");
1963 add_pending_object(revs
, &head
->object
, "HEAD");
1964 add_pending_object(revs
, &other
->object
, "MERGE_HEAD");
1965 bases
= get_merge_bases(head
, other
);
1966 add_rev_cmdline_list(revs
, bases
, REV_CMD_MERGE_BASE
, UNINTERESTING
| BOTTOM
);
1967 add_pending_commit_list(revs
, bases
, UNINTERESTING
| BOTTOM
);
1968 free_commit_list(bases
);
1969 head
->object
.flags
|= SYMMETRIC_LEFT
;
1971 if (!istate
->cache_nr
)
1972 repo_read_index(revs
->repo
);
1973 for (i
= 0; i
< istate
->cache_nr
; i
++) {
1974 const struct cache_entry
*ce
= istate
->cache
[i
];
1977 if (ce_path_match(istate
, ce
, &revs
->prune_data
, NULL
)) {
1979 REALLOC_ARRAY(prune
, prune_num
);
1980 prune
[prune_num
-2] = ce
->name
;
1981 prune
[prune_num
-1] = NULL
;
1983 while ((i
+1 < istate
->cache_nr
) &&
1984 ce_same_name(ce
, istate
->cache
[i
+1]))
1987 clear_pathspec(&revs
->prune_data
);
1988 parse_pathspec(&revs
->prune_data
, PATHSPEC_ALL_MAGIC
& ~PATHSPEC_LITERAL
,
1989 PATHSPEC_PREFER_FULL
| PATHSPEC_LITERAL_PATH
, "", prune
);
1993 static int dotdot_missing(const char *arg
, char *dotdot
,
1994 struct rev_info
*revs
, int symmetric
)
1996 if (revs
->ignore_missing
)
1998 /* de-munge so we report the full argument */
2001 ? "Invalid symmetric difference expression %s"
2002 : "Invalid revision range %s", arg
);
2005 static int handle_dotdot_1(const char *arg
, char *dotdot
,
2006 struct rev_info
*revs
, int flags
,
2007 int cant_be_filename
,
2008 struct object_context
*a_oc
,
2009 struct object_context
*b_oc
)
2011 const char *a_name
, *b_name
;
2012 struct object_id a_oid
, b_oid
;
2013 struct object
*a_obj
, *b_obj
;
2014 unsigned int a_flags
, b_flags
;
2016 unsigned int flags_exclude
= flags
^ (UNINTERESTING
| BOTTOM
);
2017 unsigned int oc_flags
= GET_OID_COMMITTISH
| GET_OID_RECORD_PATH
;
2023 b_name
= dotdot
+ 2;
2024 if (*b_name
== '.') {
2031 if (get_oid_with_context(revs
->repo
, a_name
, oc_flags
, &a_oid
, a_oc
) ||
2032 get_oid_with_context(revs
->repo
, b_name
, oc_flags
, &b_oid
, b_oc
))
2035 if (!cant_be_filename
) {
2037 verify_non_filename(revs
->prefix
, arg
);
2041 a_obj
= parse_object(revs
->repo
, &a_oid
);
2042 b_obj
= parse_object(revs
->repo
, &b_oid
);
2043 if (!a_obj
|| !b_obj
)
2044 return dotdot_missing(arg
, dotdot
, revs
, symmetric
);
2049 a_flags
= flags_exclude
;
2051 /* A...B -- find merge bases between the two */
2052 struct commit
*a
, *b
;
2053 struct commit_list
*exclude
;
2055 a
= lookup_commit_reference(revs
->repo
, &a_obj
->oid
);
2056 b
= lookup_commit_reference(revs
->repo
, &b_obj
->oid
);
2058 return dotdot_missing(arg
, dotdot
, revs
, symmetric
);
2060 exclude
= get_merge_bases(a
, b
);
2061 add_rev_cmdline_list(revs
, exclude
, REV_CMD_MERGE_BASE
,
2063 add_pending_commit_list(revs
, exclude
, flags_exclude
);
2064 free_commit_list(exclude
);
2067 a_flags
= flags
| SYMMETRIC_LEFT
;
2070 a_obj
->flags
|= a_flags
;
2071 b_obj
->flags
|= b_flags
;
2072 add_rev_cmdline(revs
, a_obj
, a_name
, REV_CMD_LEFT
, a_flags
);
2073 add_rev_cmdline(revs
, b_obj
, b_name
, REV_CMD_RIGHT
, b_flags
);
2074 add_pending_object_with_path(revs
, a_obj
, a_name
, a_oc
->mode
, a_oc
->path
);
2075 add_pending_object_with_path(revs
, b_obj
, b_name
, b_oc
->mode
, b_oc
->path
);
2079 static int handle_dotdot(const char *arg
,
2080 struct rev_info
*revs
, int flags
,
2081 int cant_be_filename
)
2083 struct object_context a_oc
, b_oc
;
2084 char *dotdot
= strstr(arg
, "..");
2090 memset(&a_oc
, 0, sizeof(a_oc
));
2091 memset(&b_oc
, 0, sizeof(b_oc
));
2094 ret
= handle_dotdot_1(arg
, dotdot
, revs
, flags
, cant_be_filename
,
2104 static int handle_revision_arg_1(const char *arg_
, struct rev_info
*revs
, int flags
, unsigned revarg_opt
)
2106 struct object_context oc
;
2108 struct object
*object
;
2109 struct object_id oid
;
2111 const char *arg
= arg_
;
2112 int cant_be_filename
= revarg_opt
& REVARG_CANNOT_BE_FILENAME
;
2113 unsigned get_sha1_flags
= GET_OID_RECORD_PATH
;
2115 flags
= flags
& UNINTERESTING
? flags
| BOTTOM
: flags
& ~BOTTOM
;
2117 if (!cant_be_filename
&& !strcmp(arg
, "..")) {
2119 * Just ".."? That is not a range but the
2120 * pathspec for the parent directory.
2125 if (!handle_dotdot(arg
, revs
, flags
, revarg_opt
))
2128 mark
= strstr(arg
, "^@");
2129 if (mark
&& !mark
[2]) {
2131 if (add_parents_only(revs
, arg
, flags
, 0))
2135 mark
= strstr(arg
, "^!");
2136 if (mark
&& !mark
[2]) {
2138 if (!add_parents_only(revs
, arg
, flags
^ (UNINTERESTING
| BOTTOM
), 0))
2141 mark
= strstr(arg
, "^-");
2143 int exclude_parent
= 1;
2146 if (strtol_i(mark
+ 2, 10, &exclude_parent
) ||
2152 if (!add_parents_only(revs
, arg
, flags
^ (UNINTERESTING
| BOTTOM
), exclude_parent
))
2158 local_flags
= UNINTERESTING
| BOTTOM
;
2162 if (revarg_opt
& REVARG_COMMITTISH
)
2163 get_sha1_flags
|= GET_OID_COMMITTISH
;
2165 if (get_oid_with_context(revs
->repo
, arg
, get_sha1_flags
, &oid
, &oc
))
2166 return revs
->ignore_missing
? 0 : -1;
2167 if (!cant_be_filename
)
2168 verify_non_filename(revs
->prefix
, arg
);
2169 object
= get_reference(revs
, arg
, &oid
, flags
^ local_flags
);
2171 return revs
->ignore_missing
? 0 : -1;
2172 add_rev_cmdline(revs
, object
, arg_
, REV_CMD_REV
, flags
^ local_flags
);
2173 add_pending_object_with_path(revs
, object
, arg
, oc
.mode
, oc
.path
);
2178 int handle_revision_arg(const char *arg
, struct rev_info
*revs
, int flags
, unsigned revarg_opt
)
2180 int ret
= handle_revision_arg_1(arg
, revs
, flags
, revarg_opt
);
2182 revs
->rev_input_given
= 1;
2186 static void read_pathspec_from_stdin(struct strbuf
*sb
,
2187 struct strvec
*prune
)
2189 while (strbuf_getline(sb
, stdin
) != EOF
)
2190 strvec_push(prune
, sb
->buf
);
2193 static void read_revisions_from_stdin(struct rev_info
*revs
,
2194 struct strvec
*prune
)
2197 int seen_dashdash
= 0;
2200 save_warning
= warn_on_object_refname_ambiguity
;
2201 warn_on_object_refname_ambiguity
= 0;
2203 strbuf_init(&sb
, 1000);
2204 while (strbuf_getline(&sb
, stdin
) != EOF
) {
2208 if (sb
.buf
[0] == '-') {
2209 if (len
== 2 && sb
.buf
[1] == '-') {
2213 die("options not supported in --stdin mode");
2215 if (handle_revision_arg(sb
.buf
, revs
, 0,
2216 REVARG_CANNOT_BE_FILENAME
))
2217 die("bad revision '%s'", sb
.buf
);
2220 read_pathspec_from_stdin(&sb
, prune
);
2222 strbuf_release(&sb
);
2223 warn_on_object_refname_ambiguity
= save_warning
;
2226 static void add_grep(struct rev_info
*revs
, const char *ptn
, enum grep_pat_token what
)
2228 append_grep_pattern(&revs
->grep_filter
, ptn
, "command line", 0, what
);
2231 static void add_header_grep(struct rev_info
*revs
, enum grep_header_field field
, const char *pattern
)
2233 append_header_grep_pattern(&revs
->grep_filter
, field
, pattern
);
2236 static void add_message_grep(struct rev_info
*revs
, const char *pattern
)
2238 add_grep(revs
, pattern
, GREP_PATTERN_BODY
);
2241 static int handle_revision_opt(struct rev_info
*revs
, int argc
, const char **argv
,
2242 int *unkc
, const char **unkv
,
2243 const struct setup_revision_opt
* opt
)
2245 const char *arg
= argv
[0];
2246 const char *optarg
= NULL
;
2248 const unsigned hexsz
= the_hash_algo
->hexsz
;
2250 /* pseudo revision arguments */
2251 if (!strcmp(arg
, "--all") || !strcmp(arg
, "--branches") ||
2252 !strcmp(arg
, "--tags") || !strcmp(arg
, "--remotes") ||
2253 !strcmp(arg
, "--reflog") || !strcmp(arg
, "--not") ||
2254 !strcmp(arg
, "--no-walk") || !strcmp(arg
, "--do-walk") ||
2255 !strcmp(arg
, "--bisect") || starts_with(arg
, "--glob=") ||
2256 !strcmp(arg
, "--indexed-objects") ||
2257 !strcmp(arg
, "--alternate-refs") ||
2258 starts_with(arg
, "--exclude=") || starts_with(arg
, "--exclude-hidden=") ||
2259 starts_with(arg
, "--branches=") || starts_with(arg
, "--tags=") ||
2260 starts_with(arg
, "--remotes=") || starts_with(arg
, "--no-walk="))
2262 unkv
[(*unkc
)++] = arg
;
2266 if ((argcount
= parse_long_opt("max-count", argv
, &optarg
))) {
2267 revs
->max_count
= atoi(optarg
);
2270 } else if ((argcount
= parse_long_opt("skip", argv
, &optarg
))) {
2271 revs
->skip_count
= atoi(optarg
);
2273 } else if ((*arg
== '-') && isdigit(arg
[1])) {
2274 /* accept -<digit>, like traditional "head" */
2275 if (strtol_i(arg
+ 1, 10, &revs
->max_count
) < 0 ||
2276 revs
->max_count
< 0)
2277 die("'%s': not a non-negative integer", arg
+ 1);
2279 } else if (!strcmp(arg
, "-n")) {
2281 return error("-n requires an argument");
2282 revs
->max_count
= atoi(argv
[1]);
2285 } else if (skip_prefix(arg
, "-n", &optarg
)) {
2286 revs
->max_count
= atoi(optarg
);
2288 } else if ((argcount
= parse_long_opt("max-age", argv
, &optarg
))) {
2289 revs
->max_age
= atoi(optarg
);
2291 } else if ((argcount
= parse_long_opt("since", argv
, &optarg
))) {
2292 revs
->max_age
= approxidate(optarg
);
2294 } else if ((argcount
= parse_long_opt("since-as-filter", argv
, &optarg
))) {
2295 revs
->max_age_as_filter
= approxidate(optarg
);
2297 } else if ((argcount
= parse_long_opt("after", argv
, &optarg
))) {
2298 revs
->max_age
= approxidate(optarg
);
2300 } else if ((argcount
= parse_long_opt("min-age", argv
, &optarg
))) {
2301 revs
->min_age
= atoi(optarg
);
2303 } else if ((argcount
= parse_long_opt("before", argv
, &optarg
))) {
2304 revs
->min_age
= approxidate(optarg
);
2306 } else if ((argcount
= parse_long_opt("until", argv
, &optarg
))) {
2307 revs
->min_age
= approxidate(optarg
);
2309 } else if (!strcmp(arg
, "--first-parent")) {
2310 revs
->first_parent_only
= 1;
2311 } else if (!strcmp(arg
, "--exclude-first-parent-only")) {
2312 revs
->exclude_first_parent_only
= 1;
2313 } else if (!strcmp(arg
, "--ancestry-path")) {
2314 revs
->ancestry_path
= 1;
2315 revs
->simplify_history
= 0;
2317 revs
->ancestry_path_implicit_bottoms
= 1;
2318 } else if (skip_prefix(arg
, "--ancestry-path=", &optarg
)) {
2320 struct object_id oid
;
2321 const char *msg
= _("could not get commit for ancestry-path argument %s");
2323 revs
->ancestry_path
= 1;
2324 revs
->simplify_history
= 0;
2327 if (repo_get_oid_committish(revs
->repo
, optarg
, &oid
))
2328 return error(msg
, optarg
);
2329 get_reference(revs
, optarg
, &oid
, ANCESTRY_PATH
);
2330 c
= lookup_commit_reference(revs
->repo
, &oid
);
2332 return error(msg
, optarg
);
2333 commit_list_insert(c
, &revs
->ancestry_path_bottoms
);
2334 } else if (!strcmp(arg
, "-g") || !strcmp(arg
, "--walk-reflogs")) {
2335 init_reflog_walk(&revs
->reflog_info
);
2336 } else if (!strcmp(arg
, "--default")) {
2338 return error("bad --default argument");
2339 revs
->def
= argv
[1];
2341 } else if (!strcmp(arg
, "--merge")) {
2342 revs
->show_merge
= 1;
2343 } else if (!strcmp(arg
, "--topo-order")) {
2344 revs
->sort_order
= REV_SORT_IN_GRAPH_ORDER
;
2345 revs
->topo_order
= 1;
2346 } else if (!strcmp(arg
, "--simplify-merges")) {
2347 revs
->simplify_merges
= 1;
2348 revs
->topo_order
= 1;
2349 revs
->rewrite_parents
= 1;
2350 revs
->simplify_history
= 0;
2352 } else if (!strcmp(arg
, "--simplify-by-decoration")) {
2353 revs
->simplify_merges
= 1;
2354 revs
->topo_order
= 1;
2355 revs
->rewrite_parents
= 1;
2356 revs
->simplify_history
= 0;
2357 revs
->simplify_by_decoration
= 1;
2360 } else if (!strcmp(arg
, "--date-order")) {
2361 revs
->sort_order
= REV_SORT_BY_COMMIT_DATE
;
2362 revs
->topo_order
= 1;
2363 } else if (!strcmp(arg
, "--author-date-order")) {
2364 revs
->sort_order
= REV_SORT_BY_AUTHOR_DATE
;
2365 revs
->topo_order
= 1;
2366 } else if (!strcmp(arg
, "--early-output")) {
2367 revs
->early_output
= 100;
2368 revs
->topo_order
= 1;
2369 } else if (skip_prefix(arg
, "--early-output=", &optarg
)) {
2370 if (strtoul_ui(optarg
, 10, &revs
->early_output
) < 0)
2371 die("'%s': not a non-negative integer", optarg
);
2372 revs
->topo_order
= 1;
2373 } else if (!strcmp(arg
, "--parents")) {
2374 revs
->rewrite_parents
= 1;
2375 revs
->print_parents
= 1;
2376 } else if (!strcmp(arg
, "--dense")) {
2378 } else if (!strcmp(arg
, "--sparse")) {
2380 } else if (!strcmp(arg
, "--in-commit-order")) {
2381 revs
->tree_blobs_in_commit_order
= 1;
2382 } else if (!strcmp(arg
, "--remove-empty")) {
2383 revs
->remove_empty_trees
= 1;
2384 } else if (!strcmp(arg
, "--merges")) {
2385 revs
->min_parents
= 2;
2386 } else if (!strcmp(arg
, "--no-merges")) {
2387 revs
->max_parents
= 1;
2388 } else if (skip_prefix(arg
, "--min-parents=", &optarg
)) {
2389 revs
->min_parents
= atoi(optarg
);
2390 } else if (!strcmp(arg
, "--no-min-parents")) {
2391 revs
->min_parents
= 0;
2392 } else if (skip_prefix(arg
, "--max-parents=", &optarg
)) {
2393 revs
->max_parents
= atoi(optarg
);
2394 } else if (!strcmp(arg
, "--no-max-parents")) {
2395 revs
->max_parents
= -1;
2396 } else if (!strcmp(arg
, "--boundary")) {
2398 } else if (!strcmp(arg
, "--left-right")) {
2399 revs
->left_right
= 1;
2400 } else if (!strcmp(arg
, "--left-only")) {
2401 if (revs
->right_only
)
2402 die("--left-only is incompatible with --right-only"
2404 revs
->left_only
= 1;
2405 } else if (!strcmp(arg
, "--right-only")) {
2406 if (revs
->left_only
)
2407 die(_("options '%s' and '%s' cannot be used together"), "--right-only", "--left-only");
2408 revs
->right_only
= 1;
2409 } else if (!strcmp(arg
, "--cherry")) {
2410 if (revs
->left_only
)
2411 die(_("options '%s' and '%s' cannot be used together"), "--cherry", "--left-only");
2412 revs
->cherry_mark
= 1;
2413 revs
->right_only
= 1;
2414 revs
->max_parents
= 1;
2416 } else if (!strcmp(arg
, "--count")) {
2418 } else if (!strcmp(arg
, "--cherry-mark")) {
2419 if (revs
->cherry_pick
)
2420 die(_("options '%s' and '%s' cannot be used together"), "--cherry-mark", "--cherry-pick");
2421 revs
->cherry_mark
= 1;
2422 revs
->limited
= 1; /* needs limit_list() */
2423 } else if (!strcmp(arg
, "--cherry-pick")) {
2424 if (revs
->cherry_mark
)
2425 die(_("options '%s' and '%s' cannot be used together"), "--cherry-pick", "--cherry-mark");
2426 revs
->cherry_pick
= 1;
2428 } else if (!strcmp(arg
, "--objects")) {
2429 revs
->tag_objects
= 1;
2430 revs
->tree_objects
= 1;
2431 revs
->blob_objects
= 1;
2432 } else if (!strcmp(arg
, "--objects-edge")) {
2433 revs
->tag_objects
= 1;
2434 revs
->tree_objects
= 1;
2435 revs
->blob_objects
= 1;
2436 revs
->edge_hint
= 1;
2437 } else if (!strcmp(arg
, "--objects-edge-aggressive")) {
2438 revs
->tag_objects
= 1;
2439 revs
->tree_objects
= 1;
2440 revs
->blob_objects
= 1;
2441 revs
->edge_hint
= 1;
2442 revs
->edge_hint_aggressive
= 1;
2443 } else if (!strcmp(arg
, "--verify-objects")) {
2444 revs
->tag_objects
= 1;
2445 revs
->tree_objects
= 1;
2446 revs
->blob_objects
= 1;
2447 revs
->verify_objects
= 1;
2448 disable_commit_graph(revs
->repo
);
2449 } else if (!strcmp(arg
, "--unpacked")) {
2451 } else if (starts_with(arg
, "--unpacked=")) {
2452 die(_("--unpacked=<packfile> no longer supported"));
2453 } else if (!strcmp(arg
, "--no-kept-objects")) {
2454 revs
->no_kept_objects
= 1;
2455 revs
->keep_pack_cache_flags
|= IN_CORE_KEEP_PACKS
;
2456 revs
->keep_pack_cache_flags
|= ON_DISK_KEEP_PACKS
;
2457 } else if (skip_prefix(arg
, "--no-kept-objects=", &optarg
)) {
2458 revs
->no_kept_objects
= 1;
2459 if (!strcmp(optarg
, "in-core"))
2460 revs
->keep_pack_cache_flags
|= IN_CORE_KEEP_PACKS
;
2461 if (!strcmp(optarg
, "on-disk"))
2462 revs
->keep_pack_cache_flags
|= ON_DISK_KEEP_PACKS
;
2463 } else if (!strcmp(arg
, "-r")) {
2465 revs
->diffopt
.flags
.recursive
= 1;
2466 } else if (!strcmp(arg
, "-t")) {
2468 revs
->diffopt
.flags
.recursive
= 1;
2469 revs
->diffopt
.flags
.tree_in_recursive
= 1;
2470 } else if ((argcount
= diff_merges_parse_opts(revs
, argv
))) {
2472 } else if (!strcmp(arg
, "-v")) {
2473 revs
->verbose_header
= 1;
2474 } else if (!strcmp(arg
, "--pretty")) {
2475 revs
->verbose_header
= 1;
2476 revs
->pretty_given
= 1;
2477 get_commit_format(NULL
, revs
);
2478 } else if (skip_prefix(arg
, "--pretty=", &optarg
) ||
2479 skip_prefix(arg
, "--format=", &optarg
)) {
2481 * Detached form ("--pretty X" as opposed to "--pretty=X")
2482 * not allowed, since the argument is optional.
2484 revs
->verbose_header
= 1;
2485 revs
->pretty_given
= 1;
2486 get_commit_format(optarg
, revs
);
2487 } else if (!strcmp(arg
, "--expand-tabs")) {
2488 revs
->expand_tabs_in_log
= 8;
2489 } else if (!strcmp(arg
, "--no-expand-tabs")) {
2490 revs
->expand_tabs_in_log
= 0;
2491 } else if (skip_prefix(arg
, "--expand-tabs=", &arg
)) {
2493 if (strtol_i(arg
, 10, &val
) < 0 || val
< 0)
2494 die("'%s': not a non-negative integer", arg
);
2495 revs
->expand_tabs_in_log
= val
;
2496 } else if (!strcmp(arg
, "--show-notes") || !strcmp(arg
, "--notes")) {
2497 enable_default_display_notes(&revs
->notes_opt
, &revs
->show_notes
);
2498 revs
->show_notes_given
= 1;
2499 } else if (!strcmp(arg
, "--show-signature")) {
2500 revs
->show_signature
= 1;
2501 } else if (!strcmp(arg
, "--no-show-signature")) {
2502 revs
->show_signature
= 0;
2503 } else if (!strcmp(arg
, "--show-linear-break")) {
2504 revs
->break_bar
= " ..........";
2505 revs
->track_linear
= 1;
2506 revs
->track_first_time
= 1;
2507 } else if (skip_prefix(arg
, "--show-linear-break=", &optarg
)) {
2508 revs
->break_bar
= xstrdup(optarg
);
2509 revs
->track_linear
= 1;
2510 revs
->track_first_time
= 1;
2511 } else if (skip_prefix(arg
, "--show-notes=", &optarg
) ||
2512 skip_prefix(arg
, "--notes=", &optarg
)) {
2513 if (starts_with(arg
, "--show-notes=") &&
2514 revs
->notes_opt
.use_default_notes
< 0)
2515 revs
->notes_opt
.use_default_notes
= 1;
2516 enable_ref_display_notes(&revs
->notes_opt
, &revs
->show_notes
, optarg
);
2517 revs
->show_notes_given
= 1;
2518 } else if (!strcmp(arg
, "--no-notes")) {
2519 disable_display_notes(&revs
->notes_opt
, &revs
->show_notes
);
2520 revs
->show_notes_given
= 1;
2521 } else if (!strcmp(arg
, "--standard-notes")) {
2522 revs
->show_notes_given
= 1;
2523 revs
->notes_opt
.use_default_notes
= 1;
2524 } else if (!strcmp(arg
, "--no-standard-notes")) {
2525 revs
->notes_opt
.use_default_notes
= 0;
2526 } else if (!strcmp(arg
, "--oneline")) {
2527 revs
->verbose_header
= 1;
2528 get_commit_format("oneline", revs
);
2529 revs
->pretty_given
= 1;
2530 revs
->abbrev_commit
= 1;
2531 } else if (!strcmp(arg
, "--graph")) {
2532 graph_clear(revs
->graph
);
2533 revs
->graph
= graph_init(revs
);
2534 } else if (!strcmp(arg
, "--no-graph")) {
2535 graph_clear(revs
->graph
);
2537 } else if (!strcmp(arg
, "--encode-email-headers")) {
2538 revs
->encode_email_headers
= 1;
2539 } else if (!strcmp(arg
, "--no-encode-email-headers")) {
2540 revs
->encode_email_headers
= 0;
2541 } else if (!strcmp(arg
, "--root")) {
2542 revs
->show_root_diff
= 1;
2543 } else if (!strcmp(arg
, "--no-commit-id")) {
2544 revs
->no_commit_id
= 1;
2545 } else if (!strcmp(arg
, "--always")) {
2546 revs
->always_show_header
= 1;
2547 } else if (!strcmp(arg
, "--no-abbrev")) {
2549 } else if (!strcmp(arg
, "--abbrev")) {
2550 revs
->abbrev
= DEFAULT_ABBREV
;
2551 } else if (skip_prefix(arg
, "--abbrev=", &optarg
)) {
2552 revs
->abbrev
= strtoul(optarg
, NULL
, 10);
2553 if (revs
->abbrev
< MINIMUM_ABBREV
)
2554 revs
->abbrev
= MINIMUM_ABBREV
;
2555 else if (revs
->abbrev
> hexsz
)
2556 revs
->abbrev
= hexsz
;
2557 } else if (!strcmp(arg
, "--abbrev-commit")) {
2558 revs
->abbrev_commit
= 1;
2559 revs
->abbrev_commit_given
= 1;
2560 } else if (!strcmp(arg
, "--no-abbrev-commit")) {
2561 revs
->abbrev_commit
= 0;
2562 } else if (!strcmp(arg
, "--full-diff")) {
2564 revs
->full_diff
= 1;
2565 } else if (!strcmp(arg
, "--show-pulls")) {
2566 revs
->show_pulls
= 1;
2567 } else if (!strcmp(arg
, "--full-history")) {
2568 revs
->simplify_history
= 0;
2569 } else if (!strcmp(arg
, "--relative-date")) {
2570 revs
->date_mode
.type
= DATE_RELATIVE
;
2571 revs
->date_mode_explicit
= 1;
2572 } else if ((argcount
= parse_long_opt("date", argv
, &optarg
))) {
2573 parse_date_format(optarg
, &revs
->date_mode
);
2574 revs
->date_mode_explicit
= 1;
2576 } else if (!strcmp(arg
, "--log-size")) {
2577 revs
->show_log_size
= 1;
2580 * Grepping the commit log
2582 else if ((argcount
= parse_long_opt("author", argv
, &optarg
))) {
2583 add_header_grep(revs
, GREP_HEADER_AUTHOR
, optarg
);
2585 } else if ((argcount
= parse_long_opt("committer", argv
, &optarg
))) {
2586 add_header_grep(revs
, GREP_HEADER_COMMITTER
, optarg
);
2588 } else if ((argcount
= parse_long_opt("grep-reflog", argv
, &optarg
))) {
2589 add_header_grep(revs
, GREP_HEADER_REFLOG
, optarg
);
2591 } else if ((argcount
= parse_long_opt("grep", argv
, &optarg
))) {
2592 add_message_grep(revs
, optarg
);
2594 } else if (!strcmp(arg
, "--basic-regexp")) {
2595 revs
->grep_filter
.pattern_type_option
= GREP_PATTERN_TYPE_BRE
;
2596 } else if (!strcmp(arg
, "--extended-regexp") || !strcmp(arg
, "-E")) {
2597 revs
->grep_filter
.pattern_type_option
= GREP_PATTERN_TYPE_ERE
;
2598 } else if (!strcmp(arg
, "--regexp-ignore-case") || !strcmp(arg
, "-i")) {
2599 revs
->grep_filter
.ignore_case
= 1;
2600 revs
->diffopt
.pickaxe_opts
|= DIFF_PICKAXE_IGNORE_CASE
;
2601 } else if (!strcmp(arg
, "--fixed-strings") || !strcmp(arg
, "-F")) {
2602 revs
->grep_filter
.pattern_type_option
= GREP_PATTERN_TYPE_FIXED
;
2603 } else if (!strcmp(arg
, "--perl-regexp") || !strcmp(arg
, "-P")) {
2604 revs
->grep_filter
.pattern_type_option
= GREP_PATTERN_TYPE_PCRE
;
2605 } else if (!strcmp(arg
, "--all-match")) {
2606 revs
->grep_filter
.all_match
= 1;
2607 } else if (!strcmp(arg
, "--invert-grep")) {
2608 revs
->grep_filter
.no_body_match
= 1;
2609 } else if ((argcount
= parse_long_opt("encoding", argv
, &optarg
))) {
2610 if (strcmp(optarg
, "none"))
2611 git_log_output_encoding
= xstrdup(optarg
);
2613 git_log_output_encoding
= "";
2615 } else if (!strcmp(arg
, "--reverse")) {
2617 } else if (!strcmp(arg
, "--children")) {
2618 revs
->children
.name
= "children";
2620 } else if (!strcmp(arg
, "--ignore-missing")) {
2621 revs
->ignore_missing
= 1;
2622 } else if (opt
&& opt
->allow_exclude_promisor_objects
&&
2623 !strcmp(arg
, "--exclude-promisor-objects")) {
2624 if (fetch_if_missing
)
2625 BUG("exclude_promisor_objects can only be used when fetch_if_missing is 0");
2626 revs
->exclude_promisor_objects
= 1;
2628 int opts
= diff_opt_parse(&revs
->diffopt
, argv
, argc
, revs
->prefix
);
2630 unkv
[(*unkc
)++] = arg
;
2637 void parse_revision_opt(struct rev_info
*revs
, struct parse_opt_ctx_t
*ctx
,
2638 const struct option
*options
,
2639 const char * const usagestr
[])
2641 int n
= handle_revision_opt(revs
, ctx
->argc
, ctx
->argv
,
2642 &ctx
->cpidx
, ctx
->out
, NULL
);
2644 error("unknown option `%s'", ctx
->argv
[0]);
2645 usage_with_options(usagestr
, options
);
2651 void revision_opts_finish(struct rev_info
*revs
)
2653 if (revs
->graph
&& revs
->track_linear
)
2654 die(_("options '%s' and '%s' cannot be used together"), "--show-linear-break", "--graph");
2657 revs
->topo_order
= 1;
2658 revs
->rewrite_parents
= 1;
2662 static int for_each_bisect_ref(struct ref_store
*refs
, each_ref_fn fn
,
2663 void *cb_data
, const char *term
)
2665 struct strbuf bisect_refs
= STRBUF_INIT
;
2667 strbuf_addf(&bisect_refs
, "refs/bisect/%s", term
);
2668 status
= refs_for_each_fullref_in(refs
, bisect_refs
.buf
, fn
, cb_data
);
2669 strbuf_release(&bisect_refs
);
2673 static int for_each_bad_bisect_ref(struct ref_store
*refs
, each_ref_fn fn
, void *cb_data
)
2675 return for_each_bisect_ref(refs
, fn
, cb_data
, term_bad
);
2678 static int for_each_good_bisect_ref(struct ref_store
*refs
, each_ref_fn fn
, void *cb_data
)
2680 return for_each_bisect_ref(refs
, fn
, cb_data
, term_good
);
2683 static int handle_revision_pseudo_opt(struct rev_info
*revs
,
2684 const char **argv
, int *flags
)
2686 const char *arg
= argv
[0];
2688 struct ref_store
*refs
;
2691 if (revs
->repo
!= the_repository
) {
2693 * We need some something like get_submodule_worktrees()
2694 * before we can go through all worktrees of a submodule,
2695 * .e.g with adding all HEADs from --all, which is not
2696 * supported right now, so stick to single worktree.
2698 if (!revs
->single_worktree
)
2699 BUG("--single-worktree cannot be used together with submodule");
2701 refs
= get_main_ref_store(revs
->repo
);
2706 * Commands like "git shortlog" will not accept the options below
2707 * unless parse_revision_opt queues them (as opposed to erroring
2710 * When implementing your new pseudo-option, remember to
2711 * register it in the list at the top of handle_revision_opt.
2713 if (!strcmp(arg
, "--all")) {
2714 handle_refs(refs
, revs
, *flags
, refs_for_each_ref
);
2715 handle_refs(refs
, revs
, *flags
, refs_head_ref
);
2716 if (!revs
->single_worktree
) {
2717 struct all_refs_cb cb
;
2719 init_all_refs_cb(&cb
, revs
, *flags
);
2720 other_head_refs(handle_one_ref
, &cb
);
2722 clear_ref_exclusions(&revs
->ref_excludes
);
2723 } else if (!strcmp(arg
, "--branches")) {
2724 if (revs
->ref_excludes
.hidden_refs_configured
)
2725 return error(_("--exclude-hidden cannot be used together with --branches"));
2726 handle_refs(refs
, revs
, *flags
, refs_for_each_branch_ref
);
2727 clear_ref_exclusions(&revs
->ref_excludes
);
2728 } else if (!strcmp(arg
, "--bisect")) {
2729 read_bisect_terms(&term_bad
, &term_good
);
2730 handle_refs(refs
, revs
, *flags
, for_each_bad_bisect_ref
);
2731 handle_refs(refs
, revs
, *flags
^ (UNINTERESTING
| BOTTOM
),
2732 for_each_good_bisect_ref
);
2734 } else if (!strcmp(arg
, "--tags")) {
2735 if (revs
->ref_excludes
.hidden_refs_configured
)
2736 return error(_("--exclude-hidden cannot be used together with --tags"));
2737 handle_refs(refs
, revs
, *flags
, refs_for_each_tag_ref
);
2738 clear_ref_exclusions(&revs
->ref_excludes
);
2739 } else if (!strcmp(arg
, "--remotes")) {
2740 if (revs
->ref_excludes
.hidden_refs_configured
)
2741 return error(_("--exclude-hidden cannot be used together with --remotes"));
2742 handle_refs(refs
, revs
, *flags
, refs_for_each_remote_ref
);
2743 clear_ref_exclusions(&revs
->ref_excludes
);
2744 } else if ((argcount
= parse_long_opt("glob", argv
, &optarg
))) {
2745 struct all_refs_cb cb
;
2746 init_all_refs_cb(&cb
, revs
, *flags
);
2747 for_each_glob_ref(handle_one_ref
, optarg
, &cb
);
2748 clear_ref_exclusions(&revs
->ref_excludes
);
2750 } else if ((argcount
= parse_long_opt("exclude", argv
, &optarg
))) {
2751 add_ref_exclusion(&revs
->ref_excludes
, optarg
);
2753 } else if ((argcount
= parse_long_opt("exclude-hidden", argv
, &optarg
))) {
2754 exclude_hidden_refs(&revs
->ref_excludes
, optarg
);
2756 } else if (skip_prefix(arg
, "--branches=", &optarg
)) {
2757 struct all_refs_cb cb
;
2758 if (revs
->ref_excludes
.hidden_refs_configured
)
2759 return error(_("--exclude-hidden cannot be used together with --branches"));
2760 init_all_refs_cb(&cb
, revs
, *flags
);
2761 for_each_glob_ref_in(handle_one_ref
, optarg
, "refs/heads/", &cb
);
2762 clear_ref_exclusions(&revs
->ref_excludes
);
2763 } else if (skip_prefix(arg
, "--tags=", &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 --tags"));
2767 init_all_refs_cb(&cb
, revs
, *flags
);
2768 for_each_glob_ref_in(handle_one_ref
, optarg
, "refs/tags/", &cb
);
2769 clear_ref_exclusions(&revs
->ref_excludes
);
2770 } else if (skip_prefix(arg
, "--remotes=", &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 --remotes"));
2774 init_all_refs_cb(&cb
, revs
, *flags
);
2775 for_each_glob_ref_in(handle_one_ref
, optarg
, "refs/remotes/", &cb
);
2776 clear_ref_exclusions(&revs
->ref_excludes
);
2777 } else if (!strcmp(arg
, "--reflog")) {
2778 add_reflogs_to_pending(revs
, *flags
);
2779 } else if (!strcmp(arg
, "--indexed-objects")) {
2780 add_index_objects_to_pending(revs
, *flags
);
2781 } else if (!strcmp(arg
, "--alternate-refs")) {
2782 add_alternate_refs_to_pending(revs
, *flags
);
2783 } else if (!strcmp(arg
, "--not")) {
2784 *flags
^= UNINTERESTING
| BOTTOM
;
2785 } else if (!strcmp(arg
, "--no-walk")) {
2787 } else if (skip_prefix(arg
, "--no-walk=", &optarg
)) {
2789 * Detached form ("--no-walk X" as opposed to "--no-walk=X")
2790 * not allowed, since the argument is optional.
2793 if (!strcmp(optarg
, "sorted"))
2794 revs
->unsorted_input
= 0;
2795 else if (!strcmp(optarg
, "unsorted"))
2796 revs
->unsorted_input
= 1;
2798 return error("invalid argument to --no-walk");
2799 } else if (!strcmp(arg
, "--do-walk")) {
2801 } else if (!strcmp(arg
, "--single-worktree")) {
2802 revs
->single_worktree
= 1;
2803 } else if (skip_prefix(arg
, ("--filter="), &arg
)) {
2804 parse_list_objects_filter(&revs
->filter
, arg
);
2805 } else if (!strcmp(arg
, ("--no-filter"))) {
2806 list_objects_filter_set_no_filter(&revs
->filter
);
2814 static void NORETURN
diagnose_missing_default(const char *def
)
2817 const char *refname
;
2819 refname
= resolve_ref_unsafe(def
, 0, NULL
, &flags
);
2820 if (!refname
|| !(flags
& REF_ISSYMREF
) || (flags
& REF_ISBROKEN
))
2821 die(_("your current branch appears to be broken"));
2823 skip_prefix(refname
, "refs/heads/", &refname
);
2824 die(_("your current branch '%s' does not have any commits yet"),
2829 * Parse revision information, filling in the "rev_info" structure,
2830 * and removing the used arguments from the argument list.
2832 * Returns the number of arguments left that weren't recognized
2833 * (which are also moved to the head of the argument list)
2835 int setup_revisions(int argc
, const char **argv
, struct rev_info
*revs
, struct setup_revision_opt
*opt
)
2837 int i
, flags
, left
, seen_dashdash
, revarg_opt
;
2838 struct strvec prune_data
= STRVEC_INIT
;
2839 int seen_end_of_options
= 0;
2841 /* First, search for "--" */
2842 if (opt
&& opt
->assume_dashdash
) {
2846 for (i
= 1; i
< argc
; i
++) {
2847 const char *arg
= argv
[i
];
2848 if (strcmp(arg
, "--"))
2850 if (opt
&& opt
->free_removed_argv_elements
)
2851 free((char *)argv
[i
]);
2855 strvec_pushv(&prune_data
, argv
+ i
+ 1);
2861 /* Second, deal with arguments and options */
2863 revarg_opt
= opt
? opt
->revarg_opt
: 0;
2865 revarg_opt
|= REVARG_CANNOT_BE_FILENAME
;
2866 for (left
= i
= 1; i
< argc
; i
++) {
2867 const char *arg
= argv
[i
];
2868 if (!seen_end_of_options
&& *arg
== '-') {
2871 opts
= handle_revision_pseudo_opt(
2879 if (!strcmp(arg
, "--stdin")) {
2880 if (revs
->disable_stdin
) {
2884 if (revs
->read_from_stdin
++)
2885 die("--stdin given twice?");
2886 read_revisions_from_stdin(revs
, &prune_data
);
2890 if (!strcmp(arg
, "--end-of-options")) {
2891 seen_end_of_options
= 1;
2895 opts
= handle_revision_opt(revs
, argc
- i
, argv
+ i
,
2907 if (handle_revision_arg(arg
, revs
, flags
, revarg_opt
)) {
2909 if (seen_dashdash
|| *arg
== '^')
2910 die("bad revision '%s'", arg
);
2912 /* If we didn't have a "--":
2913 * (1) all filenames must exist;
2914 * (2) all rev-args must not be interpretable
2915 * as a valid filename.
2916 * but the latter we have checked in the main loop.
2918 for (j
= i
; j
< argc
; j
++)
2919 verify_filename(revs
->prefix
, argv
[j
], j
== i
);
2921 strvec_pushv(&prune_data
, argv
+ i
);
2925 revision_opts_finish(revs
);
2927 if (prune_data
.nr
) {
2929 * If we need to introduce the magic "a lone ':' means no
2930 * pathspec whatsoever", here is the place to do so.
2932 * if (prune_data.nr == 1 && !strcmp(prune_data[0], ":")) {
2933 * prune_data.nr = 0;
2934 * prune_data.alloc = 0;
2935 * free(prune_data.path);
2936 * prune_data.path = NULL;
2938 * terminate prune_data.alloc with NULL and
2939 * call init_pathspec() to set revs->prune_data here.
2942 parse_pathspec(&revs
->prune_data
, 0, 0,
2943 revs
->prefix
, prune_data
.v
);
2945 strvec_clear(&prune_data
);
2948 revs
->def
= opt
? opt
->def
: NULL
;
2949 if (opt
&& opt
->tweak
)
2950 opt
->tweak(revs
, opt
);
2951 if (revs
->show_merge
)
2952 prepare_show_merge(revs
);
2953 if (revs
->def
&& !revs
->pending
.nr
&& !revs
->rev_input_given
) {
2954 struct object_id oid
;
2955 struct object
*object
;
2956 struct object_context oc
;
2957 if (get_oid_with_context(revs
->repo
, revs
->def
, 0, &oid
, &oc
))
2958 diagnose_missing_default(revs
->def
);
2959 object
= get_reference(revs
, revs
->def
, &oid
, 0);
2960 add_pending_object_with_mode(revs
, object
, revs
->def
, oc
.mode
);
2963 /* Did the user ask for any diff output? Run the diff! */
2964 if (revs
->diffopt
.output_format
& ~DIFF_FORMAT_NO_OUTPUT
)
2967 /* Pickaxe, diff-filter and rename following need diffs */
2968 if ((revs
->diffopt
.pickaxe_opts
& DIFF_PICKAXE_KINDS_MASK
) ||
2969 revs
->diffopt
.filter
||
2970 revs
->diffopt
.flags
.follow_renames
)
2973 if (revs
->diffopt
.objfind
)
2974 revs
->simplify_history
= 0;
2976 if (revs
->line_level_traverse
) {
2977 if (want_ancestry(revs
))
2979 revs
->topo_order
= 1;
2982 if (revs
->topo_order
&& !generation_numbers_enabled(the_repository
))
2985 if (revs
->prune_data
.nr
) {
2986 copy_pathspec(&revs
->pruning
.pathspec
, &revs
->prune_data
);
2987 /* Can't prune commits with rename following: the paths change.. */
2988 if (!revs
->diffopt
.flags
.follow_renames
)
2990 if (!revs
->full_diff
)
2991 copy_pathspec(&revs
->diffopt
.pathspec
,
2995 diff_merges_setup_revs(revs
);
2997 revs
->diffopt
.abbrev
= revs
->abbrev
;
2999 diff_setup_done(&revs
->diffopt
);
3001 if (!is_encoding_utf8(get_log_output_encoding()))
3002 revs
->grep_filter
.ignore_locale
= 1;
3003 compile_grep_patterns(&revs
->grep_filter
);
3005 if (revs
->reverse
&& revs
->reflog_info
)
3006 die(_("options '%s' and '%s' cannot be used together"), "--reverse", "--walk-reflogs");
3007 if (revs
->reflog_info
&& revs
->limited
)
3008 die("cannot combine --walk-reflogs with history-limiting options");
3009 if (revs
->rewrite_parents
&& revs
->children
.name
)
3010 die(_("options '%s' and '%s' cannot be used together"), "--parents", "--children");
3011 if (revs
->filter
.choice
&& !revs
->blob_objects
)
3012 die(_("object filtering requires --objects"));
3015 * Limitations on the graph functionality
3017 if (revs
->reverse
&& revs
->graph
)
3018 die(_("options '%s' and '%s' cannot be used together"), "--reverse", "--graph");
3020 if (revs
->reflog_info
&& revs
->graph
)
3021 die(_("options '%s' and '%s' cannot be used together"), "--walk-reflogs", "--graph");
3022 if (revs
->no_walk
&& revs
->graph
)
3023 die(_("options '%s' and '%s' cannot be used together"), "--no-walk", "--graph");
3024 if (!revs
->reflog_info
&& revs
->grep_filter
.use_reflog_filter
)
3025 die(_("the option '%s' requires '%s'"), "--grep-reflog", "--walk-reflogs");
3027 if (revs
->line_level_traverse
&&
3028 (revs
->diffopt
.output_format
& ~(DIFF_FORMAT_PATCH
| DIFF_FORMAT_NO_OUTPUT
)))
3029 die(_("-L does not yet support diff formats besides -p and -s"));
3031 if (revs
->expand_tabs_in_log
< 0)
3032 revs
->expand_tabs_in_log
= revs
->expand_tabs_in_log_default
;
3037 static void release_revisions_cmdline(struct rev_cmdline_info
*cmdline
)
3041 for (i
= 0; i
< cmdline
->nr
; i
++)
3042 free((char *)cmdline
->rev
[i
].name
);
3046 static void release_revisions_mailmap(struct string_list
*mailmap
)
3050 clear_mailmap(mailmap
);
3054 static void release_revisions_topo_walk_info(struct topo_walk_info
*info
);
3056 void release_revisions(struct rev_info
*revs
)
3058 free_commit_list(revs
->commits
);
3059 free_commit_list(revs
->ancestry_path_bottoms
);
3060 object_array_clear(&revs
->pending
);
3061 object_array_clear(&revs
->boundary_commits
);
3062 release_revisions_cmdline(&revs
->cmdline
);
3063 list_objects_filter_release(&revs
->filter
);
3064 clear_pathspec(&revs
->prune_data
);
3065 date_mode_release(&revs
->date_mode
);
3066 release_revisions_mailmap(revs
->mailmap
);
3067 free_grep_patterns(&revs
->grep_filter
);
3068 graph_clear(revs
->graph
);
3069 /* TODO (need to handle "no_free"): diff_free(&revs->diffopt) */
3070 diff_free(&revs
->pruning
);
3071 reflog_walk_info_release(revs
->reflog_info
);
3072 release_revisions_topo_walk_info(revs
->topo_walk_info
);
3075 static void add_child(struct rev_info
*revs
, struct commit
*parent
, struct commit
*child
)
3077 struct commit_list
*l
= xcalloc(1, sizeof(*l
));
3080 l
->next
= add_decoration(&revs
->children
, &parent
->object
, l
);
3083 static int remove_duplicate_parents(struct rev_info
*revs
, struct commit
*commit
)
3085 struct treesame_state
*ts
= lookup_decoration(&revs
->treesame
, &commit
->object
);
3086 struct commit_list
**pp
, *p
;
3087 int surviving_parents
;
3089 /* Examine existing parents while marking ones we have seen... */
3090 pp
= &commit
->parents
;
3091 surviving_parents
= 0;
3092 while ((p
= *pp
) != NULL
) {
3093 struct commit
*parent
= p
->item
;
3094 if (parent
->object
.flags
& TMP_MARK
) {
3097 compact_treesame(revs
, commit
, surviving_parents
);
3100 parent
->object
.flags
|= TMP_MARK
;
3101 surviving_parents
++;
3104 /* clear the temporary mark */
3105 for (p
= commit
->parents
; p
; p
= p
->next
) {
3106 p
->item
->object
.flags
&= ~TMP_MARK
;
3108 /* no update_treesame() - removing duplicates can't affect TREESAME */
3109 return surviving_parents
;
3112 struct merge_simplify_state
{
3113 struct commit
*simplified
;
3116 static struct merge_simplify_state
*locate_simplify_state(struct rev_info
*revs
, struct commit
*commit
)
3118 struct merge_simplify_state
*st
;
3120 st
= lookup_decoration(&revs
->merge_simplification
, &commit
->object
);
3122 CALLOC_ARRAY(st
, 1);
3123 add_decoration(&revs
->merge_simplification
, &commit
->object
, st
);
3128 static int mark_redundant_parents(struct commit
*commit
)
3130 struct commit_list
*h
= reduce_heads(commit
->parents
);
3131 int i
= 0, marked
= 0;
3132 struct commit_list
*po
, *pn
;
3134 /* Want these for sanity-checking only */
3135 int orig_cnt
= commit_list_count(commit
->parents
);
3136 int cnt
= commit_list_count(h
);
3139 * Not ready to remove items yet, just mark them for now, based
3140 * on the output of reduce_heads(). reduce_heads outputs the reduced
3141 * set in its original order, so this isn't too hard.
3143 po
= commit
->parents
;
3146 if (pn
&& po
->item
== pn
->item
) {
3150 po
->item
->object
.flags
|= TMP_MARK
;
3156 if (i
!= cnt
|| cnt
+marked
!= orig_cnt
)
3157 die("mark_redundant_parents %d %d %d %d", orig_cnt
, cnt
, i
, marked
);
3159 free_commit_list(h
);
3164 static int mark_treesame_root_parents(struct commit
*commit
)
3166 struct commit_list
*p
;
3169 for (p
= commit
->parents
; p
; p
= p
->next
) {
3170 struct commit
*parent
= p
->item
;
3171 if (!parent
->parents
&& (parent
->object
.flags
& TREESAME
)) {
3172 parent
->object
.flags
|= TMP_MARK
;
3181 * Awkward naming - this means one parent we are TREESAME to.
3182 * cf mark_treesame_root_parents: root parents that are TREESAME (to an
3183 * empty tree). Better name suggestions?
3185 static int leave_one_treesame_to_parent(struct rev_info
*revs
, struct commit
*commit
)
3187 struct treesame_state
*ts
= lookup_decoration(&revs
->treesame
, &commit
->object
);
3188 struct commit
*unmarked
= NULL
, *marked
= NULL
;
3189 struct commit_list
*p
;
3192 for (p
= commit
->parents
, n
= 0; p
; p
= p
->next
, n
++) {
3193 if (ts
->treesame
[n
]) {
3194 if (p
->item
->object
.flags
& TMP_MARK
) {
3207 * If we are TREESAME to a marked-for-deletion parent, but not to any
3208 * unmarked parents, unmark the first TREESAME parent. This is the
3209 * parent that the default simplify_history==1 scan would have followed,
3210 * and it doesn't make sense to omit that path when asking for a
3211 * simplified full history. Retaining it improves the chances of
3212 * understanding odd missed merges that took an old version of a file.
3216 * I--------*X A modified the file, but mainline merge X used
3217 * \ / "-s ours", so took the version from I. X is
3218 * `-*A--' TREESAME to I and !TREESAME to A.
3220 * Default log from X would produce "I". Without this check,
3221 * --full-history --simplify-merges would produce "I-A-X", showing
3222 * the merge commit X and that it changed A, but not making clear that
3223 * it had just taken the I version. With this check, the topology above
3226 * Note that it is possible that the simplification chooses a different
3227 * TREESAME parent from the default, in which case this test doesn't
3228 * activate, and we _do_ drop the default parent. Example:
3230 * I------X A modified the file, but it was reverted in B,
3231 * \ / meaning mainline merge X is TREESAME to both
3234 * Default log would produce "I" by following the first parent;
3235 * --full-history --simplify-merges will produce "I-A-B". But this is a
3236 * reasonable result - it presents a logical full history leading from
3237 * I to X, and X is not an important merge.
3239 if (!unmarked
&& marked
) {
3240 marked
->object
.flags
&= ~TMP_MARK
;
3247 static int remove_marked_parents(struct rev_info
*revs
, struct commit
*commit
)
3249 struct commit_list
**pp
, *p
;
3250 int nth_parent
, removed
= 0;
3252 pp
= &commit
->parents
;
3254 while ((p
= *pp
) != NULL
) {
3255 struct commit
*parent
= p
->item
;
3256 if (parent
->object
.flags
& TMP_MARK
) {
3257 parent
->object
.flags
&= ~TMP_MARK
;
3261 compact_treesame(revs
, commit
, nth_parent
);
3268 /* Removing parents can only increase TREESAMEness */
3269 if (removed
&& !(commit
->object
.flags
& TREESAME
))
3270 update_treesame(revs
, commit
);
3275 static struct commit_list
**simplify_one(struct rev_info
*revs
, struct commit
*commit
, struct commit_list
**tail
)
3277 struct commit_list
*p
;
3278 struct commit
*parent
;
3279 struct merge_simplify_state
*st
, *pst
;
3282 st
= locate_simplify_state(revs
, commit
);
3285 * Have we handled this one?
3291 * An UNINTERESTING commit simplifies to itself, so does a
3292 * root commit. We do not rewrite parents of such commit
3295 if ((commit
->object
.flags
& UNINTERESTING
) || !commit
->parents
) {
3296 st
->simplified
= commit
;
3301 * Do we know what commit all of our parents that matter
3302 * should be rewritten to? Otherwise we are not ready to
3303 * rewrite this one yet.
3305 for (cnt
= 0, p
= commit
->parents
; p
; p
= p
->next
) {
3306 pst
= locate_simplify_state(revs
, p
->item
);
3307 if (!pst
->simplified
) {
3308 tail
= &commit_list_insert(p
->item
, tail
)->next
;
3311 if (revs
->first_parent_only
)
3315 tail
= &commit_list_insert(commit
, tail
)->next
;
3320 * Rewrite our list of parents. Note that this cannot
3321 * affect our TREESAME flags in any way - a commit is
3322 * always TREESAME to its simplification.
3324 for (p
= commit
->parents
; p
; p
= p
->next
) {
3325 pst
= locate_simplify_state(revs
, p
->item
);
3326 p
->item
= pst
->simplified
;
3327 if (revs
->first_parent_only
)
3331 if (revs
->first_parent_only
)
3334 cnt
= remove_duplicate_parents(revs
, commit
);
3337 * It is possible that we are a merge and one side branch
3338 * does not have any commit that touches the given paths;
3339 * in such a case, the immediate parent from that branch
3340 * will be rewritten to be the merge base.
3342 * o----X X: the commit we are looking at;
3343 * / / o: a commit that touches the paths;
3346 * Further, a merge of an independent branch that doesn't
3347 * touch the path will reduce to a treesame root parent:
3349 * ----o----X X: the commit we are looking at;
3350 * / o: a commit that touches the paths;
3351 * r r: a root commit not touching the paths
3353 * Detect and simplify both cases.
3356 int marked
= mark_redundant_parents(commit
);
3357 marked
+= mark_treesame_root_parents(commit
);
3359 marked
-= leave_one_treesame_to_parent(revs
, commit
);
3361 cnt
= remove_marked_parents(revs
, commit
);
3365 * A commit simplifies to itself if it is a root, if it is
3366 * UNINTERESTING, if it touches the given paths, or if it is a
3367 * merge and its parents don't simplify to one relevant commit
3368 * (the first two cases are already handled at the beginning of
3371 * Otherwise, it simplifies to what its sole relevant parent
3375 (commit
->object
.flags
& UNINTERESTING
) ||
3376 !(commit
->object
.flags
& TREESAME
) ||
3377 (parent
= one_relevant_parent(revs
, commit
->parents
)) == NULL
||
3378 (revs
->show_pulls
&& (commit
->object
.flags
& PULL_MERGE
)))
3379 st
->simplified
= commit
;
3381 pst
= locate_simplify_state(revs
, parent
);
3382 st
->simplified
= pst
->simplified
;
3387 static void simplify_merges(struct rev_info
*revs
)
3389 struct commit_list
*list
, *next
;
3390 struct commit_list
*yet_to_do
, **tail
;
3391 struct commit
*commit
;
3396 /* feed the list reversed */
3398 for (list
= revs
->commits
; list
; list
= next
) {
3399 commit
= list
->item
;
3402 * Do not free(list) here yet; the original list
3403 * is used later in this function.
3405 commit_list_insert(commit
, &yet_to_do
);
3412 commit
= pop_commit(&list
);
3413 tail
= simplify_one(revs
, commit
, tail
);
3417 /* clean up the result, removing the simplified ones */
3418 list
= revs
->commits
;
3419 revs
->commits
= NULL
;
3420 tail
= &revs
->commits
;
3422 struct merge_simplify_state
*st
;
3424 commit
= pop_commit(&list
);
3425 st
= locate_simplify_state(revs
, commit
);
3426 if (st
->simplified
== commit
)
3427 tail
= &commit_list_insert(commit
, tail
)->next
;
3431 static void set_children(struct rev_info
*revs
)
3433 struct commit_list
*l
;
3434 for (l
= revs
->commits
; l
; l
= l
->next
) {
3435 struct commit
*commit
= l
->item
;
3436 struct commit_list
*p
;
3438 for (p
= commit
->parents
; p
; p
= p
->next
)
3439 add_child(revs
, p
->item
, commit
);
3443 void reset_revision_walk(void)
3445 clear_object_flags(SEEN
| ADDED
| SHOWN
| TOPO_WALK_EXPLORED
| TOPO_WALK_INDEGREE
);
3448 static int mark_uninteresting(const struct object_id
*oid
,
3449 struct packed_git
*pack UNUSED
,
3450 uint32_t pos UNUSED
,
3453 struct rev_info
*revs
= cb
;
3454 struct object
*o
= lookup_unknown_object(revs
->repo
, oid
);
3455 o
->flags
|= UNINTERESTING
| SEEN
;
3459 define_commit_slab(indegree_slab
, int);
3460 define_commit_slab(author_date_slab
, timestamp_t
);
3462 struct topo_walk_info
{
3463 timestamp_t min_generation
;
3464 struct prio_queue explore_queue
;
3465 struct prio_queue indegree_queue
;
3466 struct prio_queue topo_queue
;
3467 struct indegree_slab indegree
;
3468 struct author_date_slab author_date
;
3471 static int topo_walk_atexit_registered
;
3472 static unsigned int count_explore_walked
;
3473 static unsigned int count_indegree_walked
;
3474 static unsigned int count_topo_walked
;
3476 static void trace2_topo_walk_statistics_atexit(void)
3478 struct json_writer jw
= JSON_WRITER_INIT
;
3480 jw_object_begin(&jw
, 0);
3481 jw_object_intmax(&jw
, "count_explore_walked", count_explore_walked
);
3482 jw_object_intmax(&jw
, "count_indegree_walked", count_indegree_walked
);
3483 jw_object_intmax(&jw
, "count_topo_walked", count_topo_walked
);
3486 trace2_data_json("topo_walk", the_repository
, "statistics", &jw
);
3491 static inline void test_flag_and_insert(struct prio_queue
*q
, struct commit
*c
, int flag
)
3493 if (c
->object
.flags
& flag
)
3496 c
->object
.flags
|= flag
;
3497 prio_queue_put(q
, c
);
3500 static void explore_walk_step(struct rev_info
*revs
)
3502 struct topo_walk_info
*info
= revs
->topo_walk_info
;
3503 struct commit_list
*p
;
3504 struct commit
*c
= prio_queue_get(&info
->explore_queue
);
3509 if (repo_parse_commit_gently(revs
->repo
, c
, 1) < 0)
3512 count_explore_walked
++;
3514 if (revs
->sort_order
== REV_SORT_BY_AUTHOR_DATE
)
3515 record_author_date(&info
->author_date
, c
);
3517 if (revs
->max_age
!= -1 && (c
->date
< revs
->max_age
))
3518 c
->object
.flags
|= UNINTERESTING
;
3520 if (process_parents(revs
, c
, NULL
, NULL
) < 0)
3523 if (c
->object
.flags
& UNINTERESTING
)
3524 mark_parents_uninteresting(revs
, c
);
3526 for (p
= c
->parents
; p
; p
= p
->next
)
3527 test_flag_and_insert(&info
->explore_queue
, p
->item
, TOPO_WALK_EXPLORED
);
3530 static void explore_to_depth(struct rev_info
*revs
,
3531 timestamp_t gen_cutoff
)
3533 struct topo_walk_info
*info
= revs
->topo_walk_info
;
3535 while ((c
= prio_queue_peek(&info
->explore_queue
)) &&
3536 commit_graph_generation(c
) >= gen_cutoff
)
3537 explore_walk_step(revs
);
3540 static void indegree_walk_step(struct rev_info
*revs
)
3542 struct commit_list
*p
;
3543 struct topo_walk_info
*info
= revs
->topo_walk_info
;
3544 struct commit
*c
= prio_queue_get(&info
->indegree_queue
);
3549 if (repo_parse_commit_gently(revs
->repo
, c
, 1) < 0)
3552 count_indegree_walked
++;
3554 explore_to_depth(revs
, commit_graph_generation(c
));
3556 for (p
= c
->parents
; p
; p
= p
->next
) {
3557 struct commit
*parent
= p
->item
;
3558 int *pi
= indegree_slab_at(&info
->indegree
, parent
);
3560 if (repo_parse_commit_gently(revs
->repo
, parent
, 1) < 0)
3568 test_flag_and_insert(&info
->indegree_queue
, parent
, TOPO_WALK_INDEGREE
);
3570 if (revs
->first_parent_only
)
3575 static void compute_indegrees_to_depth(struct rev_info
*revs
,
3576 timestamp_t gen_cutoff
)
3578 struct topo_walk_info
*info
= revs
->topo_walk_info
;
3580 while ((c
= prio_queue_peek(&info
->indegree_queue
)) &&
3581 commit_graph_generation(c
) >= gen_cutoff
)
3582 indegree_walk_step(revs
);
3585 static void release_revisions_topo_walk_info(struct topo_walk_info
*info
)
3589 clear_prio_queue(&info
->explore_queue
);
3590 clear_prio_queue(&info
->indegree_queue
);
3591 clear_prio_queue(&info
->topo_queue
);
3592 clear_indegree_slab(&info
->indegree
);
3593 clear_author_date_slab(&info
->author_date
);
3597 static void reset_topo_walk(struct rev_info
*revs
)
3599 release_revisions_topo_walk_info(revs
->topo_walk_info
);
3600 revs
->topo_walk_info
= NULL
;
3603 static void init_topo_walk(struct rev_info
*revs
)
3605 struct topo_walk_info
*info
;
3606 struct commit_list
*list
;
3607 if (revs
->topo_walk_info
)
3608 reset_topo_walk(revs
);
3610 revs
->topo_walk_info
= xmalloc(sizeof(struct topo_walk_info
));
3611 info
= revs
->topo_walk_info
;
3612 memset(info
, 0, sizeof(struct topo_walk_info
));
3614 init_indegree_slab(&info
->indegree
);
3615 memset(&info
->explore_queue
, 0, sizeof(info
->explore_queue
));
3616 memset(&info
->indegree_queue
, 0, sizeof(info
->indegree_queue
));
3617 memset(&info
->topo_queue
, 0, sizeof(info
->topo_queue
));
3619 switch (revs
->sort_order
) {
3620 default: /* REV_SORT_IN_GRAPH_ORDER */
3621 info
->topo_queue
.compare
= NULL
;
3623 case REV_SORT_BY_COMMIT_DATE
:
3624 info
->topo_queue
.compare
= compare_commits_by_commit_date
;
3626 case REV_SORT_BY_AUTHOR_DATE
:
3627 init_author_date_slab(&info
->author_date
);
3628 info
->topo_queue
.compare
= compare_commits_by_author_date
;
3629 info
->topo_queue
.cb_data
= &info
->author_date
;
3633 info
->explore_queue
.compare
= compare_commits_by_gen_then_commit_date
;
3634 info
->indegree_queue
.compare
= compare_commits_by_gen_then_commit_date
;
3636 info
->min_generation
= GENERATION_NUMBER_INFINITY
;
3637 for (list
= revs
->commits
; list
; list
= list
->next
) {
3638 struct commit
*c
= list
->item
;
3639 timestamp_t generation
;
3641 if (repo_parse_commit_gently(revs
->repo
, c
, 1))
3644 test_flag_and_insert(&info
->explore_queue
, c
, TOPO_WALK_EXPLORED
);
3645 test_flag_and_insert(&info
->indegree_queue
, c
, TOPO_WALK_INDEGREE
);
3647 generation
= commit_graph_generation(c
);
3648 if (generation
< info
->min_generation
)
3649 info
->min_generation
= generation
;
3651 *(indegree_slab_at(&info
->indegree
, c
)) = 1;
3653 if (revs
->sort_order
== REV_SORT_BY_AUTHOR_DATE
)
3654 record_author_date(&info
->author_date
, c
);
3656 compute_indegrees_to_depth(revs
, info
->min_generation
);
3658 for (list
= revs
->commits
; list
; list
= list
->next
) {
3659 struct commit
*c
= list
->item
;
3661 if (*(indegree_slab_at(&info
->indegree
, c
)) == 1)
3662 prio_queue_put(&info
->topo_queue
, c
);
3666 * This is unfortunate; the initial tips need to be shown
3667 * in the order given from the revision traversal machinery.
3669 if (revs
->sort_order
== REV_SORT_IN_GRAPH_ORDER
)
3670 prio_queue_reverse(&info
->topo_queue
);
3672 if (trace2_is_enabled() && !topo_walk_atexit_registered
) {
3673 atexit(trace2_topo_walk_statistics_atexit
);
3674 topo_walk_atexit_registered
= 1;
3678 static struct commit
*next_topo_commit(struct rev_info
*revs
)
3681 struct topo_walk_info
*info
= revs
->topo_walk_info
;
3683 /* pop next off of topo_queue */
3684 c
= prio_queue_get(&info
->topo_queue
);
3687 *(indegree_slab_at(&info
->indegree
, c
)) = 0;
3692 static void expand_topo_walk(struct rev_info
*revs
, struct commit
*commit
)
3694 struct commit_list
*p
;
3695 struct topo_walk_info
*info
= revs
->topo_walk_info
;
3696 if (process_parents(revs
, commit
, NULL
, NULL
) < 0) {
3697 if (!revs
->ignore_missing_links
)
3698 die("Failed to traverse parents of commit %s",
3699 oid_to_hex(&commit
->object
.oid
));
3702 count_topo_walked
++;
3704 for (p
= commit
->parents
; p
; p
= p
->next
) {
3705 struct commit
*parent
= p
->item
;
3707 timestamp_t generation
;
3709 if (parent
->object
.flags
& UNINTERESTING
)
3712 if (repo_parse_commit_gently(revs
->repo
, parent
, 1) < 0)
3715 generation
= commit_graph_generation(parent
);
3716 if (generation
< info
->min_generation
) {
3717 info
->min_generation
= generation
;
3718 compute_indegrees_to_depth(revs
, info
->min_generation
);
3721 pi
= indegree_slab_at(&info
->indegree
, parent
);
3725 prio_queue_put(&info
->topo_queue
, parent
);
3727 if (revs
->first_parent_only
)
3732 int prepare_revision_walk(struct rev_info
*revs
)
3735 struct object_array old_pending
;
3736 struct commit_list
**next
= &revs
->commits
;
3738 memcpy(&old_pending
, &revs
->pending
, sizeof(old_pending
));
3739 revs
->pending
.nr
= 0;
3740 revs
->pending
.alloc
= 0;
3741 revs
->pending
.objects
= NULL
;
3742 for (i
= 0; i
< old_pending
.nr
; i
++) {
3743 struct object_array_entry
*e
= old_pending
.objects
+ i
;
3744 struct commit
*commit
= handle_commit(revs
, e
);
3746 if (!(commit
->object
.flags
& SEEN
)) {
3747 commit
->object
.flags
|= SEEN
;
3748 next
= commit_list_append(commit
, next
);
3752 object_array_clear(&old_pending
);
3754 /* Signal whether we need per-parent treesame decoration */
3755 if (revs
->simplify_merges
||
3756 (revs
->limited
&& limiting_can_increase_treesame(revs
)))
3757 revs
->treesame
.name
= "treesame";
3759 if (revs
->exclude_promisor_objects
) {
3760 for_each_packed_object(mark_uninteresting
, revs
,
3761 FOR_EACH_OBJECT_PROMISOR_ONLY
);
3764 if (!revs
->reflog_info
)
3765 prepare_to_use_bloom_filter(revs
);
3766 if (!revs
->unsorted_input
)
3767 commit_list_sort_by_date(&revs
->commits
);
3770 if (revs
->limited
) {
3771 if (limit_list(revs
) < 0)
3773 if (revs
->topo_order
)
3774 sort_in_topological_order(&revs
->commits
, revs
->sort_order
);
3775 } else if (revs
->topo_order
)
3776 init_topo_walk(revs
);
3777 if (revs
->line_level_traverse
&& want_ancestry(revs
))
3779 * At the moment we can only do line-level log with parent
3780 * rewriting by performing this expensive pre-filtering step.
3781 * If parent rewriting is not requested, then we rather
3782 * perform the line-level log filtering during the regular
3783 * history traversal.
3785 line_log_filter(revs
);
3786 if (revs
->simplify_merges
)
3787 simplify_merges(revs
);
3788 if (revs
->children
.name
)
3794 static enum rewrite_result
rewrite_one_1(struct rev_info
*revs
,
3796 struct prio_queue
*queue
)
3799 struct commit
*p
= *pp
;
3801 if (process_parents(revs
, p
, NULL
, queue
) < 0)
3802 return rewrite_one_error
;
3803 if (p
->object
.flags
& UNINTERESTING
)
3804 return rewrite_one_ok
;
3805 if (!(p
->object
.flags
& TREESAME
))
3806 return rewrite_one_ok
;
3808 return rewrite_one_noparents
;
3809 if (!(p
= one_relevant_parent(revs
, p
->parents
)))
3810 return rewrite_one_ok
;
3815 static void merge_queue_into_list(struct prio_queue
*q
, struct commit_list
**list
)
3818 struct commit
*item
= prio_queue_peek(q
);
3819 struct commit_list
*p
= *list
;
3821 if (p
&& p
->item
->date
>= item
->date
)
3824 p
= commit_list_insert(item
, list
);
3825 list
= &p
->next
; /* skip newly added item */
3826 prio_queue_get(q
); /* pop item */
3831 static enum rewrite_result
rewrite_one(struct rev_info
*revs
, struct commit
**pp
)
3833 struct prio_queue queue
= { compare_commits_by_commit_date
};
3834 enum rewrite_result ret
= rewrite_one_1(revs
, pp
, &queue
);
3835 merge_queue_into_list(&queue
, &revs
->commits
);
3836 clear_prio_queue(&queue
);
3840 int rewrite_parents(struct rev_info
*revs
, struct commit
*commit
,
3841 rewrite_parent_fn_t rewrite_parent
)
3843 struct commit_list
**pp
= &commit
->parents
;
3845 struct commit_list
*parent
= *pp
;
3846 switch (rewrite_parent(revs
, &parent
->item
)) {
3847 case rewrite_one_ok
:
3849 case rewrite_one_noparents
:
3852 case rewrite_one_error
:
3857 remove_duplicate_parents(revs
, commit
);
3861 static int commit_match(struct commit
*commit
, struct rev_info
*opt
)
3864 const char *encoding
;
3865 const char *message
;
3866 struct strbuf buf
= STRBUF_INIT
;
3868 if (!opt
->grep_filter
.pattern_list
&& !opt
->grep_filter
.header_list
)
3871 /* Prepend "fake" headers as needed */
3872 if (opt
->grep_filter
.use_reflog_filter
) {
3873 strbuf_addstr(&buf
, "reflog ");
3874 get_reflog_message(&buf
, opt
->reflog_info
);
3875 strbuf_addch(&buf
, '\n');
3879 * We grep in the user's output encoding, under the assumption that it
3880 * is the encoding they are most likely to write their grep pattern
3881 * for. In addition, it means we will match the "notes" encoding below,
3882 * so we will not end up with a buffer that has two different encodings
3885 encoding
= get_log_output_encoding();
3886 message
= logmsg_reencode(commit
, NULL
, encoding
);
3888 /* Copy the commit to temporary if we are using "fake" headers */
3890 strbuf_addstr(&buf
, message
);
3892 if (opt
->grep_filter
.header_list
&& opt
->mailmap
) {
3893 const char *commit_headers
[] = { "author ", "committer ", NULL
};
3896 strbuf_addstr(&buf
, message
);
3898 apply_mailmap_to_header(&buf
, commit_headers
, opt
->mailmap
);
3901 /* Append "fake" message parts as needed */
3902 if (opt
->show_notes
) {
3904 strbuf_addstr(&buf
, message
);
3905 format_display_notes(&commit
->object
.oid
, &buf
, encoding
, 1);
3909 * Find either in the original commit message, or in the temporary.
3910 * Note that we cast away the constness of "message" here. It is
3911 * const because it may come from the cached commit buffer. That's OK,
3912 * because we know that it is modifiable heap memory, and that while
3913 * grep_buffer may modify it for speed, it will restore any
3914 * changes before returning.
3917 retval
= grep_buffer(&opt
->grep_filter
, buf
.buf
, buf
.len
);
3919 retval
= grep_buffer(&opt
->grep_filter
,
3920 (char *)message
, strlen(message
));
3921 strbuf_release(&buf
);
3922 unuse_commit_buffer(commit
, message
);
3926 static inline int want_ancestry(const struct rev_info
*revs
)
3928 return (revs
->rewrite_parents
|| revs
->children
.name
);
3932 * Return a timestamp to be used for --since/--until comparisons for this
3933 * commit, based on the revision options.
3935 static timestamp_t
comparison_date(const struct rev_info
*revs
,
3936 struct commit
*commit
)
3938 return revs
->reflog_info
?
3939 get_reflog_timestamp(revs
->reflog_info
) :
3943 enum commit_action
get_commit_action(struct rev_info
*revs
, struct commit
*commit
)
3945 if (commit
->object
.flags
& SHOWN
)
3946 return commit_ignore
;
3947 if (revs
->unpacked
&& has_object_pack(&commit
->object
.oid
))
3948 return commit_ignore
;
3949 if (revs
->no_kept_objects
) {
3950 if (has_object_kept_pack(&commit
->object
.oid
,
3951 revs
->keep_pack_cache_flags
))
3952 return commit_ignore
;
3954 if (commit
->object
.flags
& UNINTERESTING
)
3955 return commit_ignore
;
3956 if (revs
->line_level_traverse
&& !want_ancestry(revs
)) {
3958 * In case of line-level log with parent rewriting
3959 * prepare_revision_walk() already took care of all line-level
3960 * log filtering, and there is nothing left to do here.
3962 * If parent rewriting was not requested, then this is the
3963 * place to perform the line-level log filtering. Notably,
3964 * this check, though expensive, must come before the other,
3965 * cheaper filtering conditions, because the tracked line
3966 * ranges must be adjusted even when the commit will end up
3967 * being ignored based on other conditions.
3969 if (!line_log_process_ranges_arbitrary_commit(revs
, commit
))
3970 return commit_ignore
;
3972 if (revs
->min_age
!= -1 &&
3973 comparison_date(revs
, commit
) > revs
->min_age
)
3974 return commit_ignore
;
3975 if (revs
->max_age_as_filter
!= -1 &&
3976 comparison_date(revs
, commit
) < revs
->max_age_as_filter
)
3977 return commit_ignore
;
3978 if (revs
->min_parents
|| (revs
->max_parents
>= 0)) {
3979 int n
= commit_list_count(commit
->parents
);
3980 if ((n
< revs
->min_parents
) ||
3981 ((revs
->max_parents
>= 0) && (n
> revs
->max_parents
)))
3982 return commit_ignore
;
3984 if (!commit_match(commit
, revs
))
3985 return commit_ignore
;
3986 if (revs
->prune
&& revs
->dense
) {
3987 /* Commit without changes? */
3988 if (commit
->object
.flags
& TREESAME
) {
3990 struct commit_list
*p
;
3991 /* drop merges unless we want parenthood */
3992 if (!want_ancestry(revs
))
3993 return commit_ignore
;
3995 if (revs
->show_pulls
&& (commit
->object
.flags
& PULL_MERGE
))
3999 * If we want ancestry, then need to keep any merges
4000 * between relevant commits to tie together topology.
4001 * For consistency with TREESAME and simplification
4002 * use "relevant" here rather than just INTERESTING,
4003 * to treat bottom commit(s) as part of the topology.
4005 for (n
= 0, p
= commit
->parents
; p
; p
= p
->next
)
4006 if (relevant_commit(p
->item
))
4009 return commit_ignore
;
4015 define_commit_slab(saved_parents
, struct commit_list
*);
4017 #define EMPTY_PARENT_LIST ((struct commit_list *)-1)
4020 * You may only call save_parents() once per commit (this is checked
4021 * for non-root commits).
4023 static void save_parents(struct rev_info
*revs
, struct commit
*commit
)
4025 struct commit_list
**pp
;
4027 if (!revs
->saved_parents_slab
) {
4028 revs
->saved_parents_slab
= xmalloc(sizeof(struct saved_parents
));
4029 init_saved_parents(revs
->saved_parents_slab
);
4032 pp
= saved_parents_at(revs
->saved_parents_slab
, commit
);
4035 * When walking with reflogs, we may visit the same commit
4036 * several times: once for each appearance in the reflog.
4038 * In this case, save_parents() will be called multiple times.
4039 * We want to keep only the first set of parents. We need to
4040 * store a sentinel value for an empty (i.e., NULL) parent
4041 * list to distinguish it from a not-yet-saved list, however.
4045 if (commit
->parents
)
4046 *pp
= copy_commit_list(commit
->parents
);
4048 *pp
= EMPTY_PARENT_LIST
;
4051 static void free_saved_parents(struct rev_info
*revs
)
4053 if (revs
->saved_parents_slab
)
4054 clear_saved_parents(revs
->saved_parents_slab
);
4057 struct commit_list
*get_saved_parents(struct rev_info
*revs
, const struct commit
*commit
)
4059 struct commit_list
*parents
;
4061 if (!revs
->saved_parents_slab
)
4062 return commit
->parents
;
4064 parents
= *saved_parents_at(revs
->saved_parents_slab
, commit
);
4065 if (parents
== EMPTY_PARENT_LIST
)
4070 enum commit_action
simplify_commit(struct rev_info
*revs
, struct commit
*commit
)
4072 enum commit_action action
= get_commit_action(revs
, commit
);
4074 if (action
== commit_show
&&
4075 revs
->prune
&& revs
->dense
&& want_ancestry(revs
)) {
4077 * --full-diff on simplified parents is no good: it
4078 * will show spurious changes from the commits that
4079 * were elided. So we save the parents on the side
4080 * when --full-diff is in effect.
4082 if (revs
->full_diff
)
4083 save_parents(revs
, commit
);
4084 if (rewrite_parents(revs
, commit
, rewrite_one
) < 0)
4085 return commit_error
;
4090 static void track_linear(struct rev_info
*revs
, struct commit
*commit
)
4092 if (revs
->track_first_time
) {
4094 revs
->track_first_time
= 0;
4096 struct commit_list
*p
;
4097 for (p
= revs
->previous_parents
; p
; p
= p
->next
)
4098 if (p
->item
== NULL
|| /* first commit */
4099 oideq(&p
->item
->object
.oid
, &commit
->object
.oid
))
4101 revs
->linear
= p
!= NULL
;
4103 if (revs
->reverse
) {
4105 commit
->object
.flags
|= TRACK_LINEAR
;
4107 free_commit_list(revs
->previous_parents
);
4108 revs
->previous_parents
= copy_commit_list(commit
->parents
);
4111 static struct commit
*get_revision_1(struct rev_info
*revs
)
4114 struct commit
*commit
;
4116 if (revs
->reflog_info
)
4117 commit
= next_reflog_entry(revs
->reflog_info
);
4118 else if (revs
->topo_walk_info
)
4119 commit
= next_topo_commit(revs
);
4121 commit
= pop_commit(&revs
->commits
);
4126 if (revs
->reflog_info
)
4127 commit
->object
.flags
&= ~(ADDED
| SEEN
| SHOWN
);
4130 * If we haven't done the list limiting, we need to look at
4131 * the parents here. We also need to do the date-based limiting
4132 * that we'd otherwise have done in limit_list().
4134 if (!revs
->limited
) {
4135 if (revs
->max_age
!= -1 &&
4136 comparison_date(revs
, commit
) < revs
->max_age
)
4139 if (revs
->reflog_info
)
4140 try_to_simplify_commit(revs
, commit
);
4141 else if (revs
->topo_walk_info
)
4142 expand_topo_walk(revs
, commit
);
4143 else if (process_parents(revs
, commit
, &revs
->commits
, NULL
) < 0) {
4144 if (!revs
->ignore_missing_links
)
4145 die("Failed to traverse parents of commit %s",
4146 oid_to_hex(&commit
->object
.oid
));
4150 switch (simplify_commit(revs
, commit
)) {
4154 die("Failed to simplify parents of commit %s",
4155 oid_to_hex(&commit
->object
.oid
));
4157 if (revs
->track_linear
)
4158 track_linear(revs
, commit
);
4165 * Return true for entries that have not yet been shown. (This is an
4166 * object_array_each_func_t.)
4168 static int entry_unshown(struct object_array_entry
*entry
, void *cb_data UNUSED
)
4170 return !(entry
->item
->flags
& SHOWN
);
4174 * If array is on the verge of a realloc, garbage-collect any entries
4175 * that have already been shown to try to free up some space.
4177 static void gc_boundary(struct object_array
*array
)
4179 if (array
->nr
== array
->alloc
)
4180 object_array_filter(array
, entry_unshown
, NULL
);
4183 static void create_boundary_commit_list(struct rev_info
*revs
)
4187 struct object_array
*array
= &revs
->boundary_commits
;
4188 struct object_array_entry
*objects
= array
->objects
;
4191 * If revs->commits is non-NULL at this point, an error occurred in
4192 * get_revision_1(). Ignore the error and continue printing the
4193 * boundary commits anyway. (This is what the code has always
4196 free_commit_list(revs
->commits
);
4197 revs
->commits
= NULL
;
4200 * Put all of the actual boundary commits from revs->boundary_commits
4201 * into revs->commits
4203 for (i
= 0; i
< array
->nr
; i
++) {
4204 c
= (struct commit
*)(objects
[i
].item
);
4207 if (!(c
->object
.flags
& CHILD_SHOWN
))
4209 if (c
->object
.flags
& (SHOWN
| BOUNDARY
))
4211 c
->object
.flags
|= BOUNDARY
;
4212 commit_list_insert(c
, &revs
->commits
);
4216 * If revs->topo_order is set, sort the boundary commits
4217 * in topological order
4219 sort_in_topological_order(&revs
->commits
, revs
->sort_order
);
4222 static struct commit
*get_revision_internal(struct rev_info
*revs
)
4224 struct commit
*c
= NULL
;
4225 struct commit_list
*l
;
4227 if (revs
->boundary
== 2) {
4229 * All of the normal commits have already been returned,
4230 * and we are now returning boundary commits.
4231 * create_boundary_commit_list() has populated
4232 * revs->commits with the remaining commits to return.
4234 c
= pop_commit(&revs
->commits
);
4236 c
->object
.flags
|= SHOWN
;
4241 * If our max_count counter has reached zero, then we are done. We
4242 * don't simply return NULL because we still might need to show
4243 * boundary commits. But we want to avoid calling get_revision_1, which
4244 * might do a considerable amount of work finding the next commit only
4245 * for us to throw it away.
4247 * If it is non-zero, then either we don't have a max_count at all
4248 * (-1), or it is still counting, in which case we decrement.
4250 if (revs
->max_count
) {
4251 c
= get_revision_1(revs
);
4253 while (revs
->skip_count
> 0) {
4255 c
= get_revision_1(revs
);
4261 if (revs
->max_count
> 0)
4266 c
->object
.flags
|= SHOWN
;
4268 if (!revs
->boundary
)
4273 * get_revision_1() runs out the commits, and
4274 * we are done computing the boundaries.
4275 * switch to boundary commits output mode.
4280 * Update revs->commits to contain the list of
4283 create_boundary_commit_list(revs
);
4285 return get_revision_internal(revs
);
4289 * boundary commits are the commits that are parents of the
4290 * ones we got from get_revision_1() but they themselves are
4291 * not returned from get_revision_1(). Before returning
4292 * 'c', we need to mark its parents that they could be boundaries.
4295 for (l
= c
->parents
; l
; l
= l
->next
) {
4297 p
= &(l
->item
->object
);
4298 if (p
->flags
& (CHILD_SHOWN
| SHOWN
))
4300 p
->flags
|= CHILD_SHOWN
;
4301 gc_boundary(&revs
->boundary_commits
);
4302 add_object_array(p
, NULL
, &revs
->boundary_commits
);
4308 struct commit
*get_revision(struct rev_info
*revs
)
4311 struct commit_list
*reversed
;
4313 if (revs
->reverse
) {
4315 while ((c
= get_revision_internal(revs
)))
4316 commit_list_insert(c
, &reversed
);
4317 revs
->commits
= reversed
;
4319 revs
->reverse_output_stage
= 1;
4322 if (revs
->reverse_output_stage
) {
4323 c
= pop_commit(&revs
->commits
);
4324 if (revs
->track_linear
)
4325 revs
->linear
= !!(c
&& c
->object
.flags
& TRACK_LINEAR
);
4329 c
= get_revision_internal(revs
);
4330 if (c
&& revs
->graph
)
4331 graph_update(revs
->graph
, c
);
4333 free_saved_parents(revs
);
4334 free_commit_list(revs
->previous_parents
);
4335 revs
->previous_parents
= NULL
;
4340 const char *get_revision_mark(const struct rev_info
*revs
, const struct commit
*commit
)
4342 if (commit
->object
.flags
& BOUNDARY
)
4344 else if (commit
->object
.flags
& UNINTERESTING
)
4346 else if (commit
->object
.flags
& PATCHSAME
)
4348 else if (!revs
|| revs
->left_right
) {
4349 if (commit
->object
.flags
& SYMMETRIC_LEFT
)
4353 } else if (revs
->graph
)
4355 else if (revs
->cherry_mark
)
4360 void put_revision_mark(const struct rev_info
*revs
, const struct commit
*commit
)
4362 const char *mark
= get_revision_mark(revs
, commit
);
4365 fputs(mark
, stdout
);