2 #include "object-store.h"
10 #include "repository.h"
13 #include "reflog-walk.h"
14 #include "patch-ids.h"
17 #include "string-list.h"
20 #include "commit-slab.h"
22 #include "cache-tree.h"
26 #include "argv-array.h"
27 #include "commit-reach.h"
28 #include "commit-graph.h"
29 #include "prio-queue.h"
31 volatile show_early_output_fn_t show_early_output
;
33 static const char *term_bad
;
34 static const char *term_good
;
36 implement_shared_commit_slab(revision_sources
, char *);
38 void show_object_with_name(FILE *out
, struct object
*obj
, const char *name
)
42 fprintf(out
, "%s ", oid_to_hex(&obj
->oid
));
43 for (p
= name
; *p
&& *p
!= '\n'; p
++)
48 static void mark_blob_uninteresting(struct blob
*blob
)
52 if (blob
->object
.flags
& UNINTERESTING
)
54 blob
->object
.flags
|= UNINTERESTING
;
57 static void mark_tree_contents_uninteresting(struct repository
*r
,
60 struct tree_desc desc
;
61 struct name_entry entry
;
63 if (parse_tree_gently(tree
, 1) < 0)
66 init_tree_desc(&desc
, tree
->buffer
, tree
->size
);
67 while (tree_entry(&desc
, &entry
)) {
68 switch (object_type(entry
.mode
)) {
70 mark_tree_uninteresting(r
, lookup_tree(r
, entry
.oid
));
73 mark_blob_uninteresting(lookup_blob(r
, entry
.oid
));
76 /* Subproject commit - not in this repository */
82 * We don't care about the tree any more
83 * after it has been marked uninteresting.
85 free_tree_buffer(tree
);
88 void mark_tree_uninteresting(struct repository
*r
, struct tree
*tree
)
96 if (obj
->flags
& UNINTERESTING
)
98 obj
->flags
|= UNINTERESTING
;
99 mark_tree_contents_uninteresting(r
, tree
);
102 struct commit_stack
{
103 struct commit
**items
;
106 #define COMMIT_STACK_INIT { NULL, 0, 0 }
108 static void commit_stack_push(struct commit_stack
*stack
, struct commit
*commit
)
110 ALLOC_GROW(stack
->items
, stack
->nr
+ 1, stack
->alloc
);
111 stack
->items
[stack
->nr
++] = commit
;
114 static struct commit
*commit_stack_pop(struct commit_stack
*stack
)
116 return stack
->nr
? stack
->items
[--stack
->nr
] : NULL
;
119 static void commit_stack_clear(struct commit_stack
*stack
)
121 FREE_AND_NULL(stack
->items
);
122 stack
->nr
= stack
->alloc
= 0;
125 static void mark_one_parent_uninteresting(struct commit
*commit
,
126 struct commit_stack
*pending
)
128 struct commit_list
*l
;
130 if (commit
->object
.flags
& UNINTERESTING
)
132 commit
->object
.flags
|= UNINTERESTING
;
135 * Normally we haven't parsed the parent
136 * yet, so we won't have a parent of a parent
137 * here. However, it may turn out that we've
138 * reached this commit some other way (where it
139 * wasn't uninteresting), in which case we need
140 * to mark its parents recursively too..
142 for (l
= commit
->parents
; l
; l
= l
->next
)
143 commit_stack_push(pending
, l
->item
);
146 void mark_parents_uninteresting(struct commit
*commit
)
148 struct commit_stack pending
= COMMIT_STACK_INIT
;
149 struct commit_list
*l
;
151 for (l
= commit
->parents
; l
; l
= l
->next
)
152 mark_one_parent_uninteresting(l
->item
, &pending
);
154 while (pending
.nr
> 0)
155 mark_one_parent_uninteresting(commit_stack_pop(&pending
),
158 commit_stack_clear(&pending
);
161 static void add_pending_object_with_path(struct rev_info
*revs
,
163 const char *name
, unsigned mode
,
168 if (revs
->no_walk
&& (obj
->flags
& UNINTERESTING
))
170 if (revs
->reflog_info
&& obj
->type
== OBJ_COMMIT
) {
171 struct strbuf buf
= STRBUF_INIT
;
172 int len
= interpret_branch_name(name
, 0, &buf
, 0);
174 if (0 < len
&& name
[len
] && buf
.len
)
175 strbuf_addstr(&buf
, name
+ len
);
176 add_reflog_for_walk(revs
->reflog_info
,
177 (struct commit
*)obj
,
178 buf
.buf
[0] ? buf
.buf
: name
);
179 strbuf_release(&buf
);
180 return; /* do not add the commit itself */
182 add_object_array_with_path(obj
, name
, &revs
->pending
, mode
, path
);
185 static void add_pending_object_with_mode(struct rev_info
*revs
,
187 const char *name
, unsigned mode
)
189 add_pending_object_with_path(revs
, obj
, name
, mode
, NULL
);
192 void add_pending_object(struct rev_info
*revs
,
193 struct object
*obj
, const char *name
)
195 add_pending_object_with_mode(revs
, obj
, name
, S_IFINVALID
);
198 void add_head_to_pending(struct rev_info
*revs
)
200 struct object_id oid
;
202 if (get_oid("HEAD", &oid
))
204 obj
= parse_object(revs
->repo
, &oid
);
207 add_pending_object(revs
, obj
, "HEAD");
210 static struct object
*get_reference(struct rev_info
*revs
, const char *name
,
211 const struct object_id
*oid
,
214 struct object
*object
;
216 object
= parse_object(revs
->repo
, oid
);
218 if (revs
->ignore_missing
)
220 if (revs
->exclude_promisor_objects
&& is_promisor_object(oid
))
222 die("bad object %s", name
);
224 object
->flags
|= flags
;
228 void add_pending_oid(struct rev_info
*revs
, const char *name
,
229 const struct object_id
*oid
, unsigned int flags
)
231 struct object
*object
= get_reference(revs
, name
, oid
, flags
);
232 add_pending_object(revs
, object
, name
);
235 static struct commit
*handle_commit(struct rev_info
*revs
,
236 struct object_array_entry
*entry
)
238 struct object
*object
= entry
->item
;
239 const char *name
= entry
->name
;
240 const char *path
= entry
->path
;
241 unsigned int mode
= entry
->mode
;
242 unsigned long flags
= object
->flags
;
245 * Tag object? Look what it points to..
247 while (object
->type
== OBJ_TAG
) {
248 struct tag
*tag
= (struct tag
*) object
;
249 if (revs
->tag_objects
&& !(flags
& UNINTERESTING
))
250 add_pending_object(revs
, object
, tag
->tag
);
253 object
= parse_object(revs
->repo
, &tag
->tagged
->oid
);
255 if (revs
->ignore_missing_links
|| (flags
& UNINTERESTING
))
257 if (revs
->exclude_promisor_objects
&&
258 is_promisor_object(&tag
->tagged
->oid
))
260 die("bad object %s", oid_to_hex(&tag
->tagged
->oid
));
262 object
->flags
|= flags
;
264 * We'll handle the tagged object by looping or dropping
265 * through to the non-tag handlers below. Do not
266 * propagate path data from the tag's pending entry.
273 * Commit object? Just return it, we'll do all the complex
276 if (object
->type
== OBJ_COMMIT
) {
277 struct commit
*commit
= (struct commit
*)object
;
279 if (parse_commit(commit
) < 0)
280 die("unable to parse commit %s", name
);
281 if (flags
& UNINTERESTING
) {
282 mark_parents_uninteresting(commit
);
286 char **slot
= revision_sources_at(revs
->sources
, commit
);
289 *slot
= xstrdup(name
);
295 * Tree object? Either mark it uninteresting, or add it
296 * to the list of objects to look at later..
298 if (object
->type
== OBJ_TREE
) {
299 struct tree
*tree
= (struct tree
*)object
;
300 if (!revs
->tree_objects
)
302 if (flags
& UNINTERESTING
) {
303 mark_tree_contents_uninteresting(revs
->repo
, tree
);
306 add_pending_object_with_path(revs
, object
, name
, mode
, path
);
311 * Blob object? You know the drill by now..
313 if (object
->type
== OBJ_BLOB
) {
314 if (!revs
->blob_objects
)
316 if (flags
& UNINTERESTING
)
318 add_pending_object_with_path(revs
, object
, name
, mode
, path
);
321 die("%s is unknown object", name
);
324 static int everybody_uninteresting(struct commit_list
*orig
,
325 struct commit
**interesting_cache
)
327 struct commit_list
*list
= orig
;
329 if (*interesting_cache
) {
330 struct commit
*commit
= *interesting_cache
;
331 if (!(commit
->object
.flags
& UNINTERESTING
))
336 struct commit
*commit
= list
->item
;
338 if (commit
->object
.flags
& UNINTERESTING
)
341 *interesting_cache
= commit
;
348 * A definition of "relevant" commit that we can use to simplify limited graphs
349 * by eliminating side branches.
351 * A "relevant" commit is one that is !UNINTERESTING (ie we are including it
352 * in our list), or that is a specified BOTTOM commit. Then after computing
353 * a limited list, during processing we can generally ignore boundary merges
354 * coming from outside the graph, (ie from irrelevant parents), and treat
355 * those merges as if they were single-parent. TREESAME is defined to consider
356 * only relevant parents, if any. If we are TREESAME to our on-graph parents,
357 * we don't care if we were !TREESAME to non-graph parents.
359 * Treating bottom commits as relevant ensures that a limited graph's
360 * connection to the actual bottom commit is not viewed as a side branch, but
361 * treated as part of the graph. For example:
363 * ....Z...A---X---o---o---B
367 * When computing "A..B", the A-X connection is at least as important as
368 * Y-X, despite A being flagged UNINTERESTING.
370 * And when computing --ancestry-path "A..B", the A-X connection is more
371 * important than Y-X, despite both A and Y being flagged UNINTERESTING.
373 static inline int relevant_commit(struct commit
*commit
)
375 return (commit
->object
.flags
& (UNINTERESTING
| BOTTOM
)) != UNINTERESTING
;
379 * Return a single relevant commit from a parent list. If we are a TREESAME
380 * commit, and this selects one of our parents, then we can safely simplify to
383 static struct commit
*one_relevant_parent(const struct rev_info
*revs
,
384 struct commit_list
*orig
)
386 struct commit_list
*list
= orig
;
387 struct commit
*relevant
= NULL
;
393 * For 1-parent commits, or if first-parent-only, then return that
394 * first parent (even if not "relevant" by the above definition).
395 * TREESAME will have been set purely on that parent.
397 if (revs
->first_parent_only
|| !orig
->next
)
401 * For multi-parent commits, identify a sole relevant parent, if any.
402 * If we have only one relevant parent, then TREESAME will be set purely
403 * with regard to that parent, and we can simplify accordingly.
405 * If we have more than one relevant parent, or no relevant parents
406 * (and multiple irrelevant ones), then we can't select a parent here
410 struct commit
*commit
= list
->item
;
412 if (relevant_commit(commit
)) {
422 * The goal is to get REV_TREE_NEW as the result only if the
423 * diff consists of all '+' (and no other changes), REV_TREE_OLD
424 * if the whole diff is removal of old data, and otherwise
425 * REV_TREE_DIFFERENT (of course if the trees are the same we
426 * want REV_TREE_SAME).
428 * The only time we care about the distinction is when
429 * remove_empty_trees is in effect, in which case we care only about
430 * whether the whole change is REV_TREE_NEW, or if there's another type
431 * of change. Which means we can stop the diff early in either of these
434 * 1. We're not using remove_empty_trees at all.
436 * 2. We saw anything except REV_TREE_NEW.
438 static int tree_difference
= REV_TREE_SAME
;
440 static void file_add_remove(struct diff_options
*options
,
441 int addremove
, unsigned mode
,
442 const struct object_id
*oid
,
444 const char *fullpath
, unsigned dirty_submodule
)
446 int diff
= addremove
== '+' ? REV_TREE_NEW
: REV_TREE_OLD
;
447 struct rev_info
*revs
= options
->change_fn_data
;
449 tree_difference
|= diff
;
450 if (!revs
->remove_empty_trees
|| tree_difference
!= REV_TREE_NEW
)
451 options
->flags
.has_changes
= 1;
454 static void file_change(struct diff_options
*options
,
455 unsigned old_mode
, unsigned new_mode
,
456 const struct object_id
*old_oid
,
457 const struct object_id
*new_oid
,
458 int old_oid_valid
, int new_oid_valid
,
459 const char *fullpath
,
460 unsigned old_dirty_submodule
, unsigned new_dirty_submodule
)
462 tree_difference
= REV_TREE_DIFFERENT
;
463 options
->flags
.has_changes
= 1;
466 static int rev_compare_tree(struct rev_info
*revs
,
467 struct commit
*parent
, struct commit
*commit
)
469 struct tree
*t1
= get_commit_tree(parent
);
470 struct tree
*t2
= get_commit_tree(commit
);
477 if (revs
->simplify_by_decoration
) {
479 * If we are simplifying by decoration, then the commit
480 * is worth showing if it has a tag pointing at it.
482 if (get_name_decoration(&commit
->object
))
483 return REV_TREE_DIFFERENT
;
485 * A commit that is not pointed by a tag is uninteresting
486 * if we are not limited by path. This means that you will
487 * see the usual "commits that touch the paths" plus any
488 * tagged commit by specifying both --simplify-by-decoration
491 if (!revs
->prune_data
.nr
)
492 return REV_TREE_SAME
;
495 tree_difference
= REV_TREE_SAME
;
496 revs
->pruning
.flags
.has_changes
= 0;
497 if (diff_tree_oid(&t1
->object
.oid
, &t2
->object
.oid
, "",
499 return REV_TREE_DIFFERENT
;
500 return tree_difference
;
503 static int rev_same_tree_as_empty(struct rev_info
*revs
, struct commit
*commit
)
506 struct tree
*t1
= get_commit_tree(commit
);
511 tree_difference
= REV_TREE_SAME
;
512 revs
->pruning
.flags
.has_changes
= 0;
513 retval
= diff_tree_oid(NULL
, &t1
->object
.oid
, "", &revs
->pruning
);
515 return retval
>= 0 && (tree_difference
== REV_TREE_SAME
);
518 struct treesame_state
{
519 unsigned int nparents
;
520 unsigned char treesame
[FLEX_ARRAY
];
523 static struct treesame_state
*initialise_treesame(struct rev_info
*revs
, struct commit
*commit
)
525 unsigned n
= commit_list_count(commit
->parents
);
526 struct treesame_state
*st
= xcalloc(1, st_add(sizeof(*st
), n
));
528 add_decoration(&revs
->treesame
, &commit
->object
, st
);
533 * Must be called immediately after removing the nth_parent from a commit's
534 * parent list, if we are maintaining the per-parent treesame[] decoration.
535 * This does not recalculate the master TREESAME flag - update_treesame()
536 * should be called to update it after a sequence of treesame[] modifications
537 * that may have affected it.
539 static int compact_treesame(struct rev_info
*revs
, struct commit
*commit
, unsigned nth_parent
)
541 struct treesame_state
*st
;
544 if (!commit
->parents
) {
546 * Have just removed the only parent from a non-merge.
547 * Different handling, as we lack decoration.
550 die("compact_treesame %u", nth_parent
);
551 old_same
= !!(commit
->object
.flags
& TREESAME
);
552 if (rev_same_tree_as_empty(revs
, commit
))
553 commit
->object
.flags
|= TREESAME
;
555 commit
->object
.flags
&= ~TREESAME
;
559 st
= lookup_decoration(&revs
->treesame
, &commit
->object
);
560 if (!st
|| nth_parent
>= st
->nparents
)
561 die("compact_treesame %u", nth_parent
);
563 old_same
= st
->treesame
[nth_parent
];
564 memmove(st
->treesame
+ nth_parent
,
565 st
->treesame
+ nth_parent
+ 1,
566 st
->nparents
- nth_parent
- 1);
569 * If we've just become a non-merge commit, update TREESAME
570 * immediately, and remove the no-longer-needed decoration.
571 * If still a merge, defer update until update_treesame().
573 if (--st
->nparents
== 1) {
574 if (commit
->parents
->next
)
575 die("compact_treesame parents mismatch");
576 if (st
->treesame
[0] && revs
->dense
)
577 commit
->object
.flags
|= TREESAME
;
579 commit
->object
.flags
&= ~TREESAME
;
580 free(add_decoration(&revs
->treesame
, &commit
->object
, NULL
));
586 static unsigned update_treesame(struct rev_info
*revs
, struct commit
*commit
)
588 if (commit
->parents
&& commit
->parents
->next
) {
590 struct treesame_state
*st
;
591 struct commit_list
*p
;
592 unsigned relevant_parents
;
593 unsigned relevant_change
, irrelevant_change
;
595 st
= lookup_decoration(&revs
->treesame
, &commit
->object
);
597 die("update_treesame %s", oid_to_hex(&commit
->object
.oid
));
598 relevant_parents
= 0;
599 relevant_change
= irrelevant_change
= 0;
600 for (p
= commit
->parents
, n
= 0; p
; n
++, p
= p
->next
) {
601 if (relevant_commit(p
->item
)) {
602 relevant_change
|= !st
->treesame
[n
];
605 irrelevant_change
|= !st
->treesame
[n
];
607 if (relevant_parents
? relevant_change
: irrelevant_change
)
608 commit
->object
.flags
&= ~TREESAME
;
610 commit
->object
.flags
|= TREESAME
;
613 return commit
->object
.flags
& TREESAME
;
616 static inline int limiting_can_increase_treesame(const struct rev_info
*revs
)
619 * TREESAME is irrelevant unless prune && dense;
620 * if simplify_history is set, we can't have a mixture of TREESAME and
621 * !TREESAME INTERESTING parents (and we don't have treesame[]
622 * decoration anyway);
623 * if first_parent_only is set, then the TREESAME flag is locked
624 * against the first parent (and again we lack treesame[] decoration).
626 return revs
->prune
&& revs
->dense
&&
627 !revs
->simplify_history
&&
628 !revs
->first_parent_only
;
631 static void try_to_simplify_commit(struct rev_info
*revs
, struct commit
*commit
)
633 struct commit_list
**pp
, *parent
;
634 struct treesame_state
*ts
= NULL
;
635 int relevant_change
= 0, irrelevant_change
= 0;
636 int relevant_parents
, nth_parent
;
639 * If we don't do pruning, everything is interesting
644 if (!get_commit_tree(commit
))
647 if (!commit
->parents
) {
648 if (rev_same_tree_as_empty(revs
, commit
))
649 commit
->object
.flags
|= TREESAME
;
654 * Normal non-merge commit? If we don't want to make the
655 * history dense, we consider it always to be a change..
657 if (!revs
->dense
&& !commit
->parents
->next
)
660 for (pp
= &commit
->parents
, nth_parent
= 0, relevant_parents
= 0;
661 (parent
= *pp
) != NULL
;
662 pp
= &parent
->next
, nth_parent
++) {
663 struct commit
*p
= parent
->item
;
664 if (relevant_commit(p
))
667 if (nth_parent
== 1) {
669 * This our second loop iteration - so we now know
670 * we're dealing with a merge.
672 * Do not compare with later parents when we care only about
673 * the first parent chain, in order to avoid derailing the
674 * traversal to follow a side branch that brought everything
675 * in the path we are limited to by the pathspec.
677 if (revs
->first_parent_only
)
680 * If this will remain a potentially-simplifiable
681 * merge, remember per-parent treesame if needed.
682 * Initialise the array with the comparison from our
685 if (revs
->treesame
.name
&&
686 !revs
->simplify_history
&&
687 !(commit
->object
.flags
& UNINTERESTING
)) {
688 ts
= initialise_treesame(revs
, commit
);
689 if (!(irrelevant_change
|| relevant_change
))
693 if (parse_commit(p
) < 0)
694 die("cannot simplify commit %s (because of %s)",
695 oid_to_hex(&commit
->object
.oid
),
696 oid_to_hex(&p
->object
.oid
));
697 switch (rev_compare_tree(revs
, p
, commit
)) {
699 if (!revs
->simplify_history
|| !relevant_commit(p
)) {
700 /* Even if a merge with an uninteresting
701 * side branch brought the entire change
702 * we are interested in, we do not want
703 * to lose the other branches of this
704 * merge, so we just keep going.
707 ts
->treesame
[nth_parent
] = 1;
711 commit
->parents
= parent
;
712 commit
->object
.flags
|= TREESAME
;
716 if (revs
->remove_empty_trees
&&
717 rev_same_tree_as_empty(revs
, p
)) {
718 /* We are adding all the specified
719 * paths from this parent, so the
720 * history beyond this parent is not
721 * interesting. Remove its parents
722 * (they are grandparents for us).
723 * IOW, we pretend this parent is a
726 if (parse_commit(p
) < 0)
727 die("cannot simplify commit %s (invalid %s)",
728 oid_to_hex(&commit
->object
.oid
),
729 oid_to_hex(&p
->object
.oid
));
734 case REV_TREE_DIFFERENT
:
735 if (relevant_commit(p
))
738 irrelevant_change
= 1;
741 die("bad tree compare for commit %s", oid_to_hex(&commit
->object
.oid
));
745 * TREESAME is straightforward for single-parent commits. For merge
746 * commits, it is most useful to define it so that "irrelevant"
747 * parents cannot make us !TREESAME - if we have any relevant
748 * parents, then we only consider TREESAMEness with respect to them,
749 * allowing irrelevant merges from uninteresting branches to be
750 * simplified away. Only if we have only irrelevant parents do we
751 * base TREESAME on them. Note that this logic is replicated in
752 * update_treesame, which should be kept in sync.
754 if (relevant_parents
? !relevant_change
: !irrelevant_change
)
755 commit
->object
.flags
|= TREESAME
;
758 static void commit_list_insert_by_date_cached(struct commit
*p
, struct commit_list
**head
,
759 struct commit_list
*cached_base
, struct commit_list
**cache
)
761 struct commit_list
*new_entry
;
763 if (cached_base
&& p
->date
< cached_base
->item
->date
)
764 new_entry
= commit_list_insert_by_date(p
, &cached_base
->next
);
766 new_entry
= commit_list_insert_by_date(p
, head
);
768 if (cache
&& (!*cache
|| p
->date
< (*cache
)->item
->date
))
772 static int process_parents(struct rev_info
*revs
, struct commit
*commit
,
773 struct commit_list
**list
, struct commit_list
**cache_ptr
)
775 struct commit_list
*parent
= commit
->parents
;
777 struct commit_list
*cached_base
= cache_ptr
? *cache_ptr
: NULL
;
779 if (commit
->object
.flags
& ADDED
)
781 commit
->object
.flags
|= ADDED
;
783 if (revs
->include_check
&&
784 !revs
->include_check(commit
, revs
->include_check_data
))
788 * If the commit is uninteresting, don't try to
789 * prune parents - we want the maximal uninteresting
792 * Normally we haven't parsed the parent
793 * yet, so we won't have a parent of a parent
794 * here. However, it may turn out that we've
795 * reached this commit some other way (where it
796 * wasn't uninteresting), in which case we need
797 * to mark its parents recursively too..
799 if (commit
->object
.flags
& UNINTERESTING
) {
801 struct commit
*p
= parent
->item
;
802 parent
= parent
->next
;
804 p
->object
.flags
|= UNINTERESTING
;
805 if (parse_commit_gently(p
, 1) < 0)
808 mark_parents_uninteresting(p
);
809 if (p
->object
.flags
& SEEN
)
811 p
->object
.flags
|= SEEN
;
813 commit_list_insert_by_date_cached(p
, list
, cached_base
, cache_ptr
);
819 * Ok, the commit wasn't uninteresting. Try to
820 * simplify the commit history and find the parent
821 * that has no differences in the path set if one exists.
823 try_to_simplify_commit(revs
, commit
);
828 left_flag
= (commit
->object
.flags
& SYMMETRIC_LEFT
);
830 for (parent
= commit
->parents
; parent
; parent
= parent
->next
) {
831 struct commit
*p
= parent
->item
;
832 int gently
= revs
->ignore_missing_links
||
833 revs
->exclude_promisor_objects
;
834 if (parse_commit_gently(p
, gently
) < 0) {
835 if (revs
->exclude_promisor_objects
&&
836 is_promisor_object(&p
->object
.oid
)) {
837 if (revs
->first_parent_only
)
844 char **slot
= revision_sources_at(revs
->sources
, p
);
847 *slot
= *revision_sources_at(revs
->sources
, commit
);
849 p
->object
.flags
|= left_flag
;
850 if (!(p
->object
.flags
& SEEN
)) {
851 p
->object
.flags
|= SEEN
;
853 commit_list_insert_by_date_cached(p
, list
, cached_base
, cache_ptr
);
855 if (revs
->first_parent_only
)
861 static void cherry_pick_list(struct commit_list
*list
, struct rev_info
*revs
)
863 struct commit_list
*p
;
864 int left_count
= 0, right_count
= 0;
866 struct patch_ids ids
;
867 unsigned cherry_flag
;
869 /* First count the commits on the left and on the right */
870 for (p
= list
; p
; p
= p
->next
) {
871 struct commit
*commit
= p
->item
;
872 unsigned flags
= commit
->object
.flags
;
873 if (flags
& BOUNDARY
)
875 else if (flags
& SYMMETRIC_LEFT
)
881 if (!left_count
|| !right_count
)
884 left_first
= left_count
< right_count
;
885 init_patch_ids(revs
->repo
, &ids
);
886 ids
.diffopts
.pathspec
= revs
->diffopt
.pathspec
;
888 /* Compute patch-ids for one side */
889 for (p
= list
; p
; p
= p
->next
) {
890 struct commit
*commit
= p
->item
;
891 unsigned flags
= commit
->object
.flags
;
893 if (flags
& BOUNDARY
)
896 * If we have fewer left, left_first is set and we omit
897 * commits on the right branch in this loop. If we have
898 * fewer right, we skip the left ones.
900 if (left_first
!= !!(flags
& SYMMETRIC_LEFT
))
902 add_commit_patch_id(commit
, &ids
);
905 /* either cherry_mark or cherry_pick are true */
906 cherry_flag
= revs
->cherry_mark
? PATCHSAME
: SHOWN
;
908 /* Check the other side */
909 for (p
= list
; p
; p
= p
->next
) {
910 struct commit
*commit
= p
->item
;
912 unsigned flags
= commit
->object
.flags
;
914 if (flags
& BOUNDARY
)
917 * If we have fewer left, left_first is set and we omit
918 * commits on the left branch in this loop.
920 if (left_first
== !!(flags
& SYMMETRIC_LEFT
))
924 * Have we seen the same patch id?
926 id
= has_commit_patch_id(commit
, &ids
);
930 commit
->object
.flags
|= cherry_flag
;
931 id
->commit
->object
.flags
|= cherry_flag
;
934 free_patch_ids(&ids
);
937 /* How many extra uninteresting commits we want to see.. */
940 static int still_interesting(struct commit_list
*src
, timestamp_t date
, int slop
,
941 struct commit
**interesting_cache
)
944 * No source list at all? We're definitely done..
950 * Does the destination list contain entries with a date
951 * before the source list? Definitely _not_ done.
953 if (date
<= src
->item
->date
)
957 * Does the source list still have interesting commits in
958 * it? Definitely not done..
960 if (!everybody_uninteresting(src
, interesting_cache
))
963 /* Ok, we're closing in.. */
968 * "rev-list --ancestry-path A..B" computes commits that are ancestors
969 * of B but not ancestors of A but further limits the result to those
970 * that are descendants of A. This takes the list of bottom commits and
971 * the result of "A..B" without --ancestry-path, and limits the latter
972 * further to the ones that can reach one of the commits in "bottom".
974 static void limit_to_ancestry(struct commit_list
*bottom
, struct commit_list
*list
)
976 struct commit_list
*p
;
977 struct commit_list
*rlist
= NULL
;
981 * Reverse the list so that it will be likely that we would
982 * process parents before children.
984 for (p
= list
; p
; p
= p
->next
)
985 commit_list_insert(p
->item
, &rlist
);
987 for (p
= bottom
; p
; p
= p
->next
)
988 p
->item
->object
.flags
|= TMP_MARK
;
991 * Mark the ones that can reach bottom commits in "list",
992 * in a bottom-up fashion.
996 for (p
= rlist
; p
; p
= p
->next
) {
997 struct commit
*c
= p
->item
;
998 struct commit_list
*parents
;
999 if (c
->object
.flags
& (TMP_MARK
| UNINTERESTING
))
1001 for (parents
= c
->parents
;
1003 parents
= parents
->next
) {
1004 if (!(parents
->item
->object
.flags
& TMP_MARK
))
1006 c
->object
.flags
|= TMP_MARK
;
1011 } while (made_progress
);
1014 * NEEDSWORK: decide if we want to remove parents that are
1015 * not marked with TMP_MARK from commit->parents for commits
1016 * in the resulting list. We may not want to do that, though.
1020 * The ones that are not marked with TMP_MARK are uninteresting
1022 for (p
= list
; p
; p
= p
->next
) {
1023 struct commit
*c
= p
->item
;
1024 if (c
->object
.flags
& TMP_MARK
)
1026 c
->object
.flags
|= UNINTERESTING
;
1029 /* We are done with the TMP_MARK */
1030 for (p
= list
; p
; p
= p
->next
)
1031 p
->item
->object
.flags
&= ~TMP_MARK
;
1032 for (p
= bottom
; p
; p
= p
->next
)
1033 p
->item
->object
.flags
&= ~TMP_MARK
;
1034 free_commit_list(rlist
);
1038 * Before walking the history, keep the set of "negative" refs the
1039 * caller has asked to exclude.
1041 * This is used to compute "rev-list --ancestry-path A..B", as we need
1042 * to filter the result of "A..B" further to the ones that can actually
1045 static struct commit_list
*collect_bottom_commits(struct commit_list
*list
)
1047 struct commit_list
*elem
, *bottom
= NULL
;
1048 for (elem
= list
; elem
; elem
= elem
->next
)
1049 if (elem
->item
->object
.flags
& BOTTOM
)
1050 commit_list_insert(elem
->item
, &bottom
);
1054 /* Assumes either left_only or right_only is set */
1055 static void limit_left_right(struct commit_list
*list
, struct rev_info
*revs
)
1057 struct commit_list
*p
;
1059 for (p
= list
; p
; p
= p
->next
) {
1060 struct commit
*commit
= p
->item
;
1062 if (revs
->right_only
) {
1063 if (commit
->object
.flags
& SYMMETRIC_LEFT
)
1064 commit
->object
.flags
|= SHOWN
;
1065 } else /* revs->left_only is set */
1066 if (!(commit
->object
.flags
& SYMMETRIC_LEFT
))
1067 commit
->object
.flags
|= SHOWN
;
1071 static int limit_list(struct rev_info
*revs
)
1074 timestamp_t date
= TIME_MAX
;
1075 struct commit_list
*list
= revs
->commits
;
1076 struct commit_list
*newlist
= NULL
;
1077 struct commit_list
**p
= &newlist
;
1078 struct commit_list
*bottom
= NULL
;
1079 struct commit
*interesting_cache
= NULL
;
1081 if (revs
->ancestry_path
) {
1082 bottom
= collect_bottom_commits(list
);
1084 die("--ancestry-path given but there are no bottom commits");
1088 struct commit
*commit
= pop_commit(&list
);
1089 struct object
*obj
= &commit
->object
;
1090 show_early_output_fn_t show
;
1092 if (commit
== interesting_cache
)
1093 interesting_cache
= NULL
;
1095 if (revs
->max_age
!= -1 && (commit
->date
< revs
->max_age
))
1096 obj
->flags
|= UNINTERESTING
;
1097 if (process_parents(revs
, commit
, &list
, NULL
) < 0)
1099 if (obj
->flags
& UNINTERESTING
) {
1100 mark_parents_uninteresting(commit
);
1101 slop
= still_interesting(list
, date
, slop
, &interesting_cache
);
1106 if (revs
->min_age
!= -1 && (commit
->date
> revs
->min_age
))
1108 date
= commit
->date
;
1109 p
= &commit_list_insert(commit
, p
)->next
;
1111 show
= show_early_output
;
1115 show(revs
, newlist
);
1116 show_early_output
= NULL
;
1118 if (revs
->cherry_pick
|| revs
->cherry_mark
)
1119 cherry_pick_list(newlist
, revs
);
1121 if (revs
->left_only
|| revs
->right_only
)
1122 limit_left_right(newlist
, revs
);
1125 limit_to_ancestry(bottom
, newlist
);
1126 free_commit_list(bottom
);
1130 * Check if any commits have become TREESAME by some of their parents
1131 * becoming UNINTERESTING.
1133 if (limiting_can_increase_treesame(revs
))
1134 for (list
= newlist
; list
; list
= list
->next
) {
1135 struct commit
*c
= list
->item
;
1136 if (c
->object
.flags
& (UNINTERESTING
| TREESAME
))
1138 update_treesame(revs
, c
);
1141 revs
->commits
= newlist
;
1146 * Add an entry to refs->cmdline with the specified information.
1149 static void add_rev_cmdline(struct rev_info
*revs
,
1150 struct object
*item
,
1155 struct rev_cmdline_info
*info
= &revs
->cmdline
;
1156 unsigned int nr
= info
->nr
;
1158 ALLOC_GROW(info
->rev
, nr
+ 1, info
->alloc
);
1159 info
->rev
[nr
].item
= item
;
1160 info
->rev
[nr
].name
= xstrdup(name
);
1161 info
->rev
[nr
].whence
= whence
;
1162 info
->rev
[nr
].flags
= flags
;
1166 static void add_rev_cmdline_list(struct rev_info
*revs
,
1167 struct commit_list
*commit_list
,
1171 while (commit_list
) {
1172 struct object
*object
= &commit_list
->item
->object
;
1173 add_rev_cmdline(revs
, object
, oid_to_hex(&object
->oid
),
1175 commit_list
= commit_list
->next
;
1179 struct all_refs_cb
{
1181 int warned_bad_reflog
;
1182 struct rev_info
*all_revs
;
1183 const char *name_for_errormsg
;
1184 struct worktree
*wt
;
1187 int ref_excluded(struct string_list
*ref_excludes
, const char *path
)
1189 struct string_list_item
*item
;
1193 for_each_string_list_item(item
, ref_excludes
) {
1194 if (!wildmatch(item
->string
, path
, 0))
1200 static int handle_one_ref(const char *path
, const struct object_id
*oid
,
1201 int flag
, void *cb_data
)
1203 struct all_refs_cb
*cb
= cb_data
;
1204 struct object
*object
;
1206 if (ref_excluded(cb
->all_revs
->ref_excludes
, path
))
1209 object
= get_reference(cb
->all_revs
, path
, oid
, cb
->all_flags
);
1210 add_rev_cmdline(cb
->all_revs
, object
, path
, REV_CMD_REF
, cb
->all_flags
);
1211 add_pending_oid(cb
->all_revs
, path
, oid
, cb
->all_flags
);
1215 static void init_all_refs_cb(struct all_refs_cb
*cb
, struct rev_info
*revs
,
1218 cb
->all_revs
= revs
;
1219 cb
->all_flags
= flags
;
1220 revs
->rev_input_given
= 1;
1224 void clear_ref_exclusion(struct string_list
**ref_excludes_p
)
1226 if (*ref_excludes_p
) {
1227 string_list_clear(*ref_excludes_p
, 0);
1228 free(*ref_excludes_p
);
1230 *ref_excludes_p
= NULL
;
1233 void add_ref_exclusion(struct string_list
**ref_excludes_p
, const char *exclude
)
1235 if (!*ref_excludes_p
) {
1236 *ref_excludes_p
= xcalloc(1, sizeof(**ref_excludes_p
));
1237 (*ref_excludes_p
)->strdup_strings
= 1;
1239 string_list_append(*ref_excludes_p
, exclude
);
1242 static void handle_refs(struct ref_store
*refs
,
1243 struct rev_info
*revs
, unsigned flags
,
1244 int (*for_each
)(struct ref_store
*, each_ref_fn
, void *))
1246 struct all_refs_cb cb
;
1249 /* this could happen with uninitialized submodules */
1253 init_all_refs_cb(&cb
, revs
, flags
);
1254 for_each(refs
, handle_one_ref
, &cb
);
1257 static void handle_one_reflog_commit(struct object_id
*oid
, void *cb_data
)
1259 struct all_refs_cb
*cb
= cb_data
;
1260 if (!is_null_oid(oid
)) {
1261 struct object
*o
= parse_object(cb
->all_revs
->repo
, oid
);
1263 o
->flags
|= cb
->all_flags
;
1264 /* ??? CMDLINEFLAGS ??? */
1265 add_pending_object(cb
->all_revs
, o
, "");
1267 else if (!cb
->warned_bad_reflog
) {
1268 warning("reflog of '%s' references pruned commits",
1269 cb
->name_for_errormsg
);
1270 cb
->warned_bad_reflog
= 1;
1275 static int handle_one_reflog_ent(struct object_id
*ooid
, struct object_id
*noid
,
1276 const char *email
, timestamp_t timestamp
, int tz
,
1277 const char *message
, void *cb_data
)
1279 handle_one_reflog_commit(ooid
, cb_data
);
1280 handle_one_reflog_commit(noid
, cb_data
);
1284 static int handle_one_reflog(const char *refname_in_wt
,
1285 const struct object_id
*oid
,
1286 int flag
, void *cb_data
)
1288 struct all_refs_cb
*cb
= cb_data
;
1289 struct strbuf refname
= STRBUF_INIT
;
1291 cb
->warned_bad_reflog
= 0;
1292 strbuf_worktree_ref(cb
->wt
, &refname
, refname_in_wt
);
1293 cb
->name_for_errormsg
= refname
.buf
;
1294 refs_for_each_reflog_ent(get_main_ref_store(the_repository
),
1296 handle_one_reflog_ent
, cb_data
);
1297 strbuf_release(&refname
);
1301 static void add_other_reflogs_to_pending(struct all_refs_cb
*cb
)
1303 struct worktree
**worktrees
, **p
;
1305 worktrees
= get_worktrees(0);
1306 for (p
= worktrees
; *p
; p
++) {
1307 struct worktree
*wt
= *p
;
1313 refs_for_each_reflog(get_worktree_ref_store(wt
),
1317 free_worktrees(worktrees
);
1320 void add_reflogs_to_pending(struct rev_info
*revs
, unsigned flags
)
1322 struct all_refs_cb cb
;
1325 cb
.all_flags
= flags
;
1327 for_each_reflog(handle_one_reflog
, &cb
);
1329 if (!revs
->single_worktree
)
1330 add_other_reflogs_to_pending(&cb
);
1333 static void add_cache_tree(struct cache_tree
*it
, struct rev_info
*revs
,
1334 struct strbuf
*path
, unsigned int flags
)
1336 size_t baselen
= path
->len
;
1339 if (it
->entry_count
>= 0) {
1340 struct tree
*tree
= lookup_tree(revs
->repo
, &it
->oid
);
1341 tree
->object
.flags
|= flags
;
1342 add_pending_object_with_path(revs
, &tree
->object
, "",
1346 for (i
= 0; i
< it
->subtree_nr
; i
++) {
1347 struct cache_tree_sub
*sub
= it
->down
[i
];
1348 strbuf_addf(path
, "%s%s", baselen
? "/" : "", sub
->name
);
1349 add_cache_tree(sub
->cache_tree
, revs
, path
, flags
);
1350 strbuf_setlen(path
, baselen
);
1355 static void do_add_index_objects_to_pending(struct rev_info
*revs
,
1356 struct index_state
*istate
,
1361 for (i
= 0; i
< istate
->cache_nr
; i
++) {
1362 struct cache_entry
*ce
= istate
->cache
[i
];
1365 if (S_ISGITLINK(ce
->ce_mode
))
1368 blob
= lookup_blob(revs
->repo
, &ce
->oid
);
1370 die("unable to add index blob to traversal");
1371 blob
->object
.flags
|= flags
;
1372 add_pending_object_with_path(revs
, &blob
->object
, "",
1373 ce
->ce_mode
, ce
->name
);
1376 if (istate
->cache_tree
) {
1377 struct strbuf path
= STRBUF_INIT
;
1378 add_cache_tree(istate
->cache_tree
, revs
, &path
, flags
);
1379 strbuf_release(&path
);
1383 void add_index_objects_to_pending(struct rev_info
*revs
, unsigned int flags
)
1385 struct worktree
**worktrees
, **p
;
1387 read_index(revs
->repo
->index
);
1388 do_add_index_objects_to_pending(revs
, revs
->repo
->index
, flags
);
1390 if (revs
->single_worktree
)
1393 worktrees
= get_worktrees(0);
1394 for (p
= worktrees
; *p
; p
++) {
1395 struct worktree
*wt
= *p
;
1396 struct index_state istate
= { NULL
};
1399 continue; /* current index already taken care of */
1401 if (read_index_from(&istate
,
1402 worktree_git_path(wt
, "index"),
1403 get_worktree_git_dir(wt
)) > 0)
1404 do_add_index_objects_to_pending(revs
, &istate
, flags
);
1405 discard_index(&istate
);
1407 free_worktrees(worktrees
);
1410 static int add_parents_only(struct rev_info
*revs
, const char *arg_
, int flags
,
1413 struct object_id oid
;
1415 struct commit
*commit
;
1416 struct commit_list
*parents
;
1418 const char *arg
= arg_
;
1421 flags
^= UNINTERESTING
| BOTTOM
;
1424 if (get_oid_committish(arg
, &oid
))
1427 it
= get_reference(revs
, arg
, &oid
, 0);
1428 if (!it
&& revs
->ignore_missing
)
1430 if (it
->type
!= OBJ_TAG
)
1432 if (!((struct tag
*)it
)->tagged
)
1434 oidcpy(&oid
, &((struct tag
*)it
)->tagged
->oid
);
1436 if (it
->type
!= OBJ_COMMIT
)
1438 commit
= (struct commit
*)it
;
1439 if (exclude_parent
&&
1440 exclude_parent
> commit_list_count(commit
->parents
))
1442 for (parents
= commit
->parents
, parent_number
= 1;
1444 parents
= parents
->next
, parent_number
++) {
1445 if (exclude_parent
&& parent_number
!= exclude_parent
)
1448 it
= &parents
->item
->object
;
1450 add_rev_cmdline(revs
, it
, arg_
, REV_CMD_PARENTS_ONLY
, flags
);
1451 add_pending_object(revs
, it
, arg
);
1456 void repo_init_revisions(struct repository
*r
,
1457 struct rev_info
*revs
,
1460 memset(revs
, 0, sizeof(*revs
));
1463 revs
->abbrev
= DEFAULT_ABBREV
;
1464 revs
->ignore_merges
= 1;
1465 revs
->simplify_history
= 1;
1466 revs
->pruning
.repo
= r
;
1467 revs
->pruning
.flags
.recursive
= 1;
1468 revs
->pruning
.flags
.quick
= 1;
1469 revs
->pruning
.add_remove
= file_add_remove
;
1470 revs
->pruning
.change
= file_change
;
1471 revs
->pruning
.change_fn_data
= revs
;
1472 revs
->sort_order
= REV_SORT_IN_GRAPH_ORDER
;
1474 revs
->prefix
= prefix
;
1477 revs
->skip_count
= -1;
1478 revs
->max_count
= -1;
1479 revs
->max_parents
= -1;
1480 revs
->expand_tabs_in_log
= -1;
1482 revs
->commit_format
= CMIT_FMT_DEFAULT
;
1483 revs
->expand_tabs_in_log_default
= 8;
1485 init_grep_defaults(revs
->repo
);
1486 grep_init(&revs
->grep_filter
, revs
->repo
, prefix
);
1487 revs
->grep_filter
.status_only
= 1;
1489 repo_diff_setup(revs
->repo
, &revs
->diffopt
);
1490 if (prefix
&& !revs
->diffopt
.prefix
) {
1491 revs
->diffopt
.prefix
= prefix
;
1492 revs
->diffopt
.prefix_length
= strlen(prefix
);
1495 revs
->notes_opt
.use_default_notes
= -1;
1498 static void add_pending_commit_list(struct rev_info
*revs
,
1499 struct commit_list
*commit_list
,
1502 while (commit_list
) {
1503 struct object
*object
= &commit_list
->item
->object
;
1504 object
->flags
|= flags
;
1505 add_pending_object(revs
, object
, oid_to_hex(&object
->oid
));
1506 commit_list
= commit_list
->next
;
1510 static void prepare_show_merge(struct rev_info
*revs
)
1512 struct commit_list
*bases
;
1513 struct commit
*head
, *other
;
1514 struct object_id oid
;
1515 const char **prune
= NULL
;
1516 int i
, prune_num
= 1; /* counting terminating NULL */
1517 struct index_state
*istate
= revs
->repo
->index
;
1519 if (get_oid("HEAD", &oid
))
1520 die("--merge without HEAD?");
1521 head
= lookup_commit_or_die(&oid
, "HEAD");
1522 if (get_oid("MERGE_HEAD", &oid
))
1523 die("--merge without MERGE_HEAD?");
1524 other
= lookup_commit_or_die(&oid
, "MERGE_HEAD");
1525 add_pending_object(revs
, &head
->object
, "HEAD");
1526 add_pending_object(revs
, &other
->object
, "MERGE_HEAD");
1527 bases
= get_merge_bases(head
, other
);
1528 add_rev_cmdline_list(revs
, bases
, REV_CMD_MERGE_BASE
, UNINTERESTING
| BOTTOM
);
1529 add_pending_commit_list(revs
, bases
, UNINTERESTING
| BOTTOM
);
1530 free_commit_list(bases
);
1531 head
->object
.flags
|= SYMMETRIC_LEFT
;
1533 if (!istate
->cache_nr
)
1535 for (i
= 0; i
< istate
->cache_nr
; i
++) {
1536 const struct cache_entry
*ce
= istate
->cache
[i
];
1539 if (ce_path_match(istate
, ce
, &revs
->prune_data
, NULL
)) {
1541 REALLOC_ARRAY(prune
, prune_num
);
1542 prune
[prune_num
-2] = ce
->name
;
1543 prune
[prune_num
-1] = NULL
;
1545 while ((i
+1 < istate
->cache_nr
) &&
1546 ce_same_name(ce
, istate
->cache
[i
+1]))
1549 clear_pathspec(&revs
->prune_data
);
1550 parse_pathspec(&revs
->prune_data
, PATHSPEC_ALL_MAGIC
& ~PATHSPEC_LITERAL
,
1551 PATHSPEC_PREFER_FULL
| PATHSPEC_LITERAL_PATH
, "", prune
);
1555 static int dotdot_missing(const char *arg
, char *dotdot
,
1556 struct rev_info
*revs
, int symmetric
)
1558 if (revs
->ignore_missing
)
1560 /* de-munge so we report the full argument */
1563 ? "Invalid symmetric difference expression %s"
1564 : "Invalid revision range %s", arg
);
1567 static int handle_dotdot_1(const char *arg
, char *dotdot
,
1568 struct rev_info
*revs
, int flags
,
1569 int cant_be_filename
,
1570 struct object_context
*a_oc
,
1571 struct object_context
*b_oc
)
1573 const char *a_name
, *b_name
;
1574 struct object_id a_oid
, b_oid
;
1575 struct object
*a_obj
, *b_obj
;
1576 unsigned int a_flags
, b_flags
;
1578 unsigned int flags_exclude
= flags
^ (UNINTERESTING
| BOTTOM
);
1579 unsigned int oc_flags
= GET_OID_COMMITTISH
| GET_OID_RECORD_PATH
;
1585 b_name
= dotdot
+ 2;
1586 if (*b_name
== '.') {
1593 if (get_oid_with_context(a_name
, oc_flags
, &a_oid
, a_oc
) ||
1594 get_oid_with_context(b_name
, oc_flags
, &b_oid
, b_oc
))
1597 if (!cant_be_filename
) {
1599 verify_non_filename(revs
->prefix
, arg
);
1603 a_obj
= parse_object(revs
->repo
, &a_oid
);
1604 b_obj
= parse_object(revs
->repo
, &b_oid
);
1605 if (!a_obj
|| !b_obj
)
1606 return dotdot_missing(arg
, dotdot
, revs
, symmetric
);
1611 a_flags
= flags_exclude
;
1613 /* A...B -- find merge bases between the two */
1614 struct commit
*a
, *b
;
1615 struct commit_list
*exclude
;
1617 a
= lookup_commit_reference(revs
->repo
, &a_obj
->oid
);
1618 b
= lookup_commit_reference(revs
->repo
, &b_obj
->oid
);
1620 return dotdot_missing(arg
, dotdot
, revs
, symmetric
);
1622 exclude
= get_merge_bases(a
, b
);
1623 add_rev_cmdline_list(revs
, exclude
, REV_CMD_MERGE_BASE
,
1625 add_pending_commit_list(revs
, exclude
, flags_exclude
);
1626 free_commit_list(exclude
);
1629 a_flags
= flags
| SYMMETRIC_LEFT
;
1632 a_obj
->flags
|= a_flags
;
1633 b_obj
->flags
|= b_flags
;
1634 add_rev_cmdline(revs
, a_obj
, a_name
, REV_CMD_LEFT
, a_flags
);
1635 add_rev_cmdline(revs
, b_obj
, b_name
, REV_CMD_RIGHT
, b_flags
);
1636 add_pending_object_with_path(revs
, a_obj
, a_name
, a_oc
->mode
, a_oc
->path
);
1637 add_pending_object_with_path(revs
, b_obj
, b_name
, b_oc
->mode
, b_oc
->path
);
1641 static int handle_dotdot(const char *arg
,
1642 struct rev_info
*revs
, int flags
,
1643 int cant_be_filename
)
1645 struct object_context a_oc
, b_oc
;
1646 char *dotdot
= strstr(arg
, "..");
1652 memset(&a_oc
, 0, sizeof(a_oc
));
1653 memset(&b_oc
, 0, sizeof(b_oc
));
1656 ret
= handle_dotdot_1(arg
, dotdot
, revs
, flags
, cant_be_filename
,
1666 int handle_revision_arg(const char *arg_
, struct rev_info
*revs
, int flags
, unsigned revarg_opt
)
1668 struct object_context oc
;
1670 struct object
*object
;
1671 struct object_id oid
;
1673 const char *arg
= arg_
;
1674 int cant_be_filename
= revarg_opt
& REVARG_CANNOT_BE_FILENAME
;
1675 unsigned get_sha1_flags
= GET_OID_RECORD_PATH
;
1677 flags
= flags
& UNINTERESTING
? flags
| BOTTOM
: flags
& ~BOTTOM
;
1679 if (!cant_be_filename
&& !strcmp(arg
, "..")) {
1681 * Just ".."? That is not a range but the
1682 * pathspec for the parent directory.
1687 if (!handle_dotdot(arg
, revs
, flags
, revarg_opt
))
1690 mark
= strstr(arg
, "^@");
1691 if (mark
&& !mark
[2]) {
1693 if (add_parents_only(revs
, arg
, flags
, 0))
1697 mark
= strstr(arg
, "^!");
1698 if (mark
&& !mark
[2]) {
1700 if (!add_parents_only(revs
, arg
, flags
^ (UNINTERESTING
| BOTTOM
), 0))
1703 mark
= strstr(arg
, "^-");
1705 int exclude_parent
= 1;
1709 exclude_parent
= strtoul(mark
+ 2, &end
, 10);
1710 if (*end
!= '\0' || !exclude_parent
)
1715 if (!add_parents_only(revs
, arg
, flags
^ (UNINTERESTING
| BOTTOM
), exclude_parent
))
1721 local_flags
= UNINTERESTING
| BOTTOM
;
1725 if (revarg_opt
& REVARG_COMMITTISH
)
1726 get_sha1_flags
|= GET_OID_COMMITTISH
;
1728 if (get_oid_with_context(arg
, get_sha1_flags
, &oid
, &oc
))
1729 return revs
->ignore_missing
? 0 : -1;
1730 if (!cant_be_filename
)
1731 verify_non_filename(revs
->prefix
, arg
);
1732 object
= get_reference(revs
, arg
, &oid
, flags
^ local_flags
);
1733 add_rev_cmdline(revs
, object
, arg_
, REV_CMD_REV
, flags
^ local_flags
);
1734 add_pending_object_with_path(revs
, object
, arg
, oc
.mode
, oc
.path
);
1739 static void read_pathspec_from_stdin(struct rev_info
*revs
, struct strbuf
*sb
,
1740 struct argv_array
*prune
)
1742 while (strbuf_getline(sb
, stdin
) != EOF
)
1743 argv_array_push(prune
, sb
->buf
);
1746 static void read_revisions_from_stdin(struct rev_info
*revs
,
1747 struct argv_array
*prune
)
1750 int seen_dashdash
= 0;
1753 save_warning
= warn_on_object_refname_ambiguity
;
1754 warn_on_object_refname_ambiguity
= 0;
1756 strbuf_init(&sb
, 1000);
1757 while (strbuf_getline(&sb
, stdin
) != EOF
) {
1761 if (sb
.buf
[0] == '-') {
1762 if (len
== 2 && sb
.buf
[1] == '-') {
1766 die("options not supported in --stdin mode");
1768 if (handle_revision_arg(sb
.buf
, revs
, 0,
1769 REVARG_CANNOT_BE_FILENAME
))
1770 die("bad revision '%s'", sb
.buf
);
1773 read_pathspec_from_stdin(revs
, &sb
, prune
);
1775 strbuf_release(&sb
);
1776 warn_on_object_refname_ambiguity
= save_warning
;
1779 static void add_grep(struct rev_info
*revs
, const char *ptn
, enum grep_pat_token what
)
1781 append_grep_pattern(&revs
->grep_filter
, ptn
, "command line", 0, what
);
1784 static void add_header_grep(struct rev_info
*revs
, enum grep_header_field field
, const char *pattern
)
1786 append_header_grep_pattern(&revs
->grep_filter
, field
, pattern
);
1789 static void add_message_grep(struct rev_info
*revs
, const char *pattern
)
1791 add_grep(revs
, pattern
, GREP_PATTERN_BODY
);
1794 static int handle_revision_opt(struct rev_info
*revs
, int argc
, const char **argv
,
1795 int *unkc
, const char **unkv
)
1797 const char *arg
= argv
[0];
1800 const unsigned hexsz
= the_hash_algo
->hexsz
;
1802 /* pseudo revision arguments */
1803 if (!strcmp(arg
, "--all") || !strcmp(arg
, "--branches") ||
1804 !strcmp(arg
, "--tags") || !strcmp(arg
, "--remotes") ||
1805 !strcmp(arg
, "--reflog") || !strcmp(arg
, "--not") ||
1806 !strcmp(arg
, "--no-walk") || !strcmp(arg
, "--do-walk") ||
1807 !strcmp(arg
, "--bisect") || starts_with(arg
, "--glob=") ||
1808 !strcmp(arg
, "--indexed-objects") ||
1809 starts_with(arg
, "--exclude=") ||
1810 starts_with(arg
, "--branches=") || starts_with(arg
, "--tags=") ||
1811 starts_with(arg
, "--remotes=") || starts_with(arg
, "--no-walk="))
1813 unkv
[(*unkc
)++] = arg
;
1817 if ((argcount
= parse_long_opt("max-count", argv
, &optarg
))) {
1818 revs
->max_count
= atoi(optarg
);
1821 } else if ((argcount
= parse_long_opt("skip", argv
, &optarg
))) {
1822 revs
->skip_count
= atoi(optarg
);
1824 } else if ((*arg
== '-') && isdigit(arg
[1])) {
1825 /* accept -<digit>, like traditional "head" */
1826 if (strtol_i(arg
+ 1, 10, &revs
->max_count
) < 0 ||
1827 revs
->max_count
< 0)
1828 die("'%s': not a non-negative integer", arg
+ 1);
1830 } else if (!strcmp(arg
, "-n")) {
1832 return error("-n requires an argument");
1833 revs
->max_count
= atoi(argv
[1]);
1836 } else if (skip_prefix(arg
, "-n", &optarg
)) {
1837 revs
->max_count
= atoi(optarg
);
1839 } else if ((argcount
= parse_long_opt("max-age", argv
, &optarg
))) {
1840 revs
->max_age
= atoi(optarg
);
1842 } else if ((argcount
= parse_long_opt("since", argv
, &optarg
))) {
1843 revs
->max_age
= approxidate(optarg
);
1845 } else if ((argcount
= parse_long_opt("after", argv
, &optarg
))) {
1846 revs
->max_age
= approxidate(optarg
);
1848 } else if ((argcount
= parse_long_opt("min-age", argv
, &optarg
))) {
1849 revs
->min_age
= atoi(optarg
);
1851 } else if ((argcount
= parse_long_opt("before", argv
, &optarg
))) {
1852 revs
->min_age
= approxidate(optarg
);
1854 } else if ((argcount
= parse_long_opt("until", argv
, &optarg
))) {
1855 revs
->min_age
= approxidate(optarg
);
1857 } else if (!strcmp(arg
, "--first-parent")) {
1858 revs
->first_parent_only
= 1;
1859 } else if (!strcmp(arg
, "--ancestry-path")) {
1860 revs
->ancestry_path
= 1;
1861 revs
->simplify_history
= 0;
1863 } else if (!strcmp(arg
, "-g") || !strcmp(arg
, "--walk-reflogs")) {
1864 init_reflog_walk(&revs
->reflog_info
);
1865 } else if (!strcmp(arg
, "--default")) {
1867 return error("bad --default argument");
1868 revs
->def
= argv
[1];
1870 } else if (!strcmp(arg
, "--merge")) {
1871 revs
->show_merge
= 1;
1872 } else if (!strcmp(arg
, "--topo-order")) {
1873 revs
->sort_order
= REV_SORT_IN_GRAPH_ORDER
;
1874 revs
->topo_order
= 1;
1875 } else if (!strcmp(arg
, "--simplify-merges")) {
1876 revs
->simplify_merges
= 1;
1877 revs
->topo_order
= 1;
1878 revs
->rewrite_parents
= 1;
1879 revs
->simplify_history
= 0;
1881 } else if (!strcmp(arg
, "--simplify-by-decoration")) {
1882 revs
->simplify_merges
= 1;
1883 revs
->topo_order
= 1;
1884 revs
->rewrite_parents
= 1;
1885 revs
->simplify_history
= 0;
1886 revs
->simplify_by_decoration
= 1;
1889 load_ref_decorations(NULL
, DECORATE_SHORT_REFS
);
1890 } else if (!strcmp(arg
, "--date-order")) {
1891 revs
->sort_order
= REV_SORT_BY_COMMIT_DATE
;
1892 revs
->topo_order
= 1;
1893 } else if (!strcmp(arg
, "--author-date-order")) {
1894 revs
->sort_order
= REV_SORT_BY_AUTHOR_DATE
;
1895 revs
->topo_order
= 1;
1896 } else if (!strcmp(arg
, "--early-output")) {
1897 revs
->early_output
= 100;
1898 revs
->topo_order
= 1;
1899 } else if (skip_prefix(arg
, "--early-output=", &optarg
)) {
1900 if (strtoul_ui(optarg
, 10, &revs
->early_output
) < 0)
1901 die("'%s': not a non-negative integer", optarg
);
1902 revs
->topo_order
= 1;
1903 } else if (!strcmp(arg
, "--parents")) {
1904 revs
->rewrite_parents
= 1;
1905 revs
->print_parents
= 1;
1906 } else if (!strcmp(arg
, "--dense")) {
1908 } else if (!strcmp(arg
, "--sparse")) {
1910 } else if (!strcmp(arg
, "--in-commit-order")) {
1911 revs
->tree_blobs_in_commit_order
= 1;
1912 } else if (!strcmp(arg
, "--remove-empty")) {
1913 revs
->remove_empty_trees
= 1;
1914 } else if (!strcmp(arg
, "--merges")) {
1915 revs
->min_parents
= 2;
1916 } else if (!strcmp(arg
, "--no-merges")) {
1917 revs
->max_parents
= 1;
1918 } else if (skip_prefix(arg
, "--min-parents=", &optarg
)) {
1919 revs
->min_parents
= atoi(optarg
);
1920 } else if (!strcmp(arg
, "--no-min-parents")) {
1921 revs
->min_parents
= 0;
1922 } else if (skip_prefix(arg
, "--max-parents=", &optarg
)) {
1923 revs
->max_parents
= atoi(optarg
);
1924 } else if (!strcmp(arg
, "--no-max-parents")) {
1925 revs
->max_parents
= -1;
1926 } else if (!strcmp(arg
, "--boundary")) {
1928 } else if (!strcmp(arg
, "--left-right")) {
1929 revs
->left_right
= 1;
1930 } else if (!strcmp(arg
, "--left-only")) {
1931 if (revs
->right_only
)
1932 die("--left-only is incompatible with --right-only"
1934 revs
->left_only
= 1;
1935 } else if (!strcmp(arg
, "--right-only")) {
1936 if (revs
->left_only
)
1937 die("--right-only is incompatible with --left-only");
1938 revs
->right_only
= 1;
1939 } else if (!strcmp(arg
, "--cherry")) {
1940 if (revs
->left_only
)
1941 die("--cherry is incompatible with --left-only");
1942 revs
->cherry_mark
= 1;
1943 revs
->right_only
= 1;
1944 revs
->max_parents
= 1;
1946 } else if (!strcmp(arg
, "--count")) {
1948 } else if (!strcmp(arg
, "--cherry-mark")) {
1949 if (revs
->cherry_pick
)
1950 die("--cherry-mark is incompatible with --cherry-pick");
1951 revs
->cherry_mark
= 1;
1952 revs
->limited
= 1; /* needs limit_list() */
1953 } else if (!strcmp(arg
, "--cherry-pick")) {
1954 if (revs
->cherry_mark
)
1955 die("--cherry-pick is incompatible with --cherry-mark");
1956 revs
->cherry_pick
= 1;
1958 } else if (!strcmp(arg
, "--objects")) {
1959 revs
->tag_objects
= 1;
1960 revs
->tree_objects
= 1;
1961 revs
->blob_objects
= 1;
1962 } else if (!strcmp(arg
, "--objects-edge")) {
1963 revs
->tag_objects
= 1;
1964 revs
->tree_objects
= 1;
1965 revs
->blob_objects
= 1;
1966 revs
->edge_hint
= 1;
1967 } else if (!strcmp(arg
, "--objects-edge-aggressive")) {
1968 revs
->tag_objects
= 1;
1969 revs
->tree_objects
= 1;
1970 revs
->blob_objects
= 1;
1971 revs
->edge_hint
= 1;
1972 revs
->edge_hint_aggressive
= 1;
1973 } else if (!strcmp(arg
, "--verify-objects")) {
1974 revs
->tag_objects
= 1;
1975 revs
->tree_objects
= 1;
1976 revs
->blob_objects
= 1;
1977 revs
->verify_objects
= 1;
1978 } else if (!strcmp(arg
, "--unpacked")) {
1980 } else if (starts_with(arg
, "--unpacked=")) {
1981 die("--unpacked=<packfile> no longer supported.");
1982 } else if (!strcmp(arg
, "-r")) {
1984 revs
->diffopt
.flags
.recursive
= 1;
1985 } else if (!strcmp(arg
, "-t")) {
1987 revs
->diffopt
.flags
.recursive
= 1;
1988 revs
->diffopt
.flags
.tree_in_recursive
= 1;
1989 } else if (!strcmp(arg
, "-m")) {
1990 revs
->ignore_merges
= 0;
1991 } else if (!strcmp(arg
, "-c")) {
1993 revs
->dense_combined_merges
= 0;
1994 revs
->combine_merges
= 1;
1995 } else if (!strcmp(arg
, "--cc")) {
1997 revs
->dense_combined_merges
= 1;
1998 revs
->combine_merges
= 1;
1999 } else if (!strcmp(arg
, "-v")) {
2000 revs
->verbose_header
= 1;
2001 } else if (!strcmp(arg
, "--pretty")) {
2002 revs
->verbose_header
= 1;
2003 revs
->pretty_given
= 1;
2004 get_commit_format(NULL
, revs
);
2005 } else if (skip_prefix(arg
, "--pretty=", &optarg
) ||
2006 skip_prefix(arg
, "--format=", &optarg
)) {
2008 * Detached form ("--pretty X" as opposed to "--pretty=X")
2009 * not allowed, since the argument is optional.
2011 revs
->verbose_header
= 1;
2012 revs
->pretty_given
= 1;
2013 get_commit_format(optarg
, revs
);
2014 } else if (!strcmp(arg
, "--expand-tabs")) {
2015 revs
->expand_tabs_in_log
= 8;
2016 } else if (!strcmp(arg
, "--no-expand-tabs")) {
2017 revs
->expand_tabs_in_log
= 0;
2018 } else if (skip_prefix(arg
, "--expand-tabs=", &arg
)) {
2020 if (strtol_i(arg
, 10, &val
) < 0 || val
< 0)
2021 die("'%s': not a non-negative integer", arg
);
2022 revs
->expand_tabs_in_log
= val
;
2023 } else if (!strcmp(arg
, "--show-notes") || !strcmp(arg
, "--notes")) {
2024 revs
->show_notes
= 1;
2025 revs
->show_notes_given
= 1;
2026 revs
->notes_opt
.use_default_notes
= 1;
2027 } else if (!strcmp(arg
, "--show-signature")) {
2028 revs
->show_signature
= 1;
2029 } else if (!strcmp(arg
, "--no-show-signature")) {
2030 revs
->show_signature
= 0;
2031 } else if (!strcmp(arg
, "--show-linear-break")) {
2032 revs
->break_bar
= " ..........";
2033 revs
->track_linear
= 1;
2034 revs
->track_first_time
= 1;
2035 } else if (skip_prefix(arg
, "--show-linear-break=", &optarg
)) {
2036 revs
->break_bar
= xstrdup(optarg
);
2037 revs
->track_linear
= 1;
2038 revs
->track_first_time
= 1;
2039 } else if (skip_prefix(arg
, "--show-notes=", &optarg
) ||
2040 skip_prefix(arg
, "--notes=", &optarg
)) {
2041 struct strbuf buf
= STRBUF_INIT
;
2042 revs
->show_notes
= 1;
2043 revs
->show_notes_given
= 1;
2044 if (starts_with(arg
, "--show-notes=") &&
2045 revs
->notes_opt
.use_default_notes
< 0)
2046 revs
->notes_opt
.use_default_notes
= 1;
2047 strbuf_addstr(&buf
, optarg
);
2048 expand_notes_ref(&buf
);
2049 string_list_append(&revs
->notes_opt
.extra_notes_refs
,
2050 strbuf_detach(&buf
, NULL
));
2051 } else if (!strcmp(arg
, "--no-notes")) {
2052 revs
->show_notes
= 0;
2053 revs
->show_notes_given
= 1;
2054 revs
->notes_opt
.use_default_notes
= -1;
2055 /* we have been strdup'ing ourselves, so trick
2056 * string_list into free()ing strings */
2057 revs
->notes_opt
.extra_notes_refs
.strdup_strings
= 1;
2058 string_list_clear(&revs
->notes_opt
.extra_notes_refs
, 0);
2059 revs
->notes_opt
.extra_notes_refs
.strdup_strings
= 0;
2060 } else if (!strcmp(arg
, "--standard-notes")) {
2061 revs
->show_notes_given
= 1;
2062 revs
->notes_opt
.use_default_notes
= 1;
2063 } else if (!strcmp(arg
, "--no-standard-notes")) {
2064 revs
->notes_opt
.use_default_notes
= 0;
2065 } else if (!strcmp(arg
, "--oneline")) {
2066 revs
->verbose_header
= 1;
2067 get_commit_format("oneline", revs
);
2068 revs
->pretty_given
= 1;
2069 revs
->abbrev_commit
= 1;
2070 } else if (!strcmp(arg
, "--graph")) {
2071 revs
->topo_order
= 1;
2072 revs
->rewrite_parents
= 1;
2073 revs
->graph
= graph_init(revs
);
2074 } else if (!strcmp(arg
, "--root")) {
2075 revs
->show_root_diff
= 1;
2076 } else if (!strcmp(arg
, "--no-commit-id")) {
2077 revs
->no_commit_id
= 1;
2078 } else if (!strcmp(arg
, "--always")) {
2079 revs
->always_show_header
= 1;
2080 } else if (!strcmp(arg
, "--no-abbrev")) {
2082 } else if (!strcmp(arg
, "--abbrev")) {
2083 revs
->abbrev
= DEFAULT_ABBREV
;
2084 } else if (skip_prefix(arg
, "--abbrev=", &optarg
)) {
2085 revs
->abbrev
= strtoul(optarg
, NULL
, 10);
2086 if (revs
->abbrev
< MINIMUM_ABBREV
)
2087 revs
->abbrev
= MINIMUM_ABBREV
;
2088 else if (revs
->abbrev
> hexsz
)
2089 revs
->abbrev
= hexsz
;
2090 } else if (!strcmp(arg
, "--abbrev-commit")) {
2091 revs
->abbrev_commit
= 1;
2092 revs
->abbrev_commit_given
= 1;
2093 } else if (!strcmp(arg
, "--no-abbrev-commit")) {
2094 revs
->abbrev_commit
= 0;
2095 } else if (!strcmp(arg
, "--full-diff")) {
2097 revs
->full_diff
= 1;
2098 } else if (!strcmp(arg
, "--full-history")) {
2099 revs
->simplify_history
= 0;
2100 } else if (!strcmp(arg
, "--relative-date")) {
2101 revs
->date_mode
.type
= DATE_RELATIVE
;
2102 revs
->date_mode_explicit
= 1;
2103 } else if ((argcount
= parse_long_opt("date", argv
, &optarg
))) {
2104 parse_date_format(optarg
, &revs
->date_mode
);
2105 revs
->date_mode_explicit
= 1;
2107 } else if (!strcmp(arg
, "--log-size")) {
2108 revs
->show_log_size
= 1;
2111 * Grepping the commit log
2113 else if ((argcount
= parse_long_opt("author", argv
, &optarg
))) {
2114 add_header_grep(revs
, GREP_HEADER_AUTHOR
, optarg
);
2116 } else if ((argcount
= parse_long_opt("committer", argv
, &optarg
))) {
2117 add_header_grep(revs
, GREP_HEADER_COMMITTER
, optarg
);
2119 } else if ((argcount
= parse_long_opt("grep-reflog", argv
, &optarg
))) {
2120 add_header_grep(revs
, GREP_HEADER_REFLOG
, optarg
);
2122 } else if ((argcount
= parse_long_opt("grep", argv
, &optarg
))) {
2123 add_message_grep(revs
, optarg
);
2125 } else if (!strcmp(arg
, "--grep-debug")) {
2126 revs
->grep_filter
.debug
= 1;
2127 } else if (!strcmp(arg
, "--basic-regexp")) {
2128 revs
->grep_filter
.pattern_type_option
= GREP_PATTERN_TYPE_BRE
;
2129 } else if (!strcmp(arg
, "--extended-regexp") || !strcmp(arg
, "-E")) {
2130 revs
->grep_filter
.pattern_type_option
= GREP_PATTERN_TYPE_ERE
;
2131 } else if (!strcmp(arg
, "--regexp-ignore-case") || !strcmp(arg
, "-i")) {
2132 revs
->grep_filter
.ignore_case
= 1;
2133 revs
->diffopt
.pickaxe_opts
|= DIFF_PICKAXE_IGNORE_CASE
;
2134 } else if (!strcmp(arg
, "--fixed-strings") || !strcmp(arg
, "-F")) {
2135 revs
->grep_filter
.pattern_type_option
= GREP_PATTERN_TYPE_FIXED
;
2136 } else if (!strcmp(arg
, "--perl-regexp") || !strcmp(arg
, "-P")) {
2137 revs
->grep_filter
.pattern_type_option
= GREP_PATTERN_TYPE_PCRE
;
2138 } else if (!strcmp(arg
, "--all-match")) {
2139 revs
->grep_filter
.all_match
= 1;
2140 } else if (!strcmp(arg
, "--invert-grep")) {
2141 revs
->invert_grep
= 1;
2142 } else if ((argcount
= parse_long_opt("encoding", argv
, &optarg
))) {
2143 if (strcmp(optarg
, "none"))
2144 git_log_output_encoding
= xstrdup(optarg
);
2146 git_log_output_encoding
= "";
2148 } else if (!strcmp(arg
, "--reverse")) {
2150 } else if (!strcmp(arg
, "--children")) {
2151 revs
->children
.name
= "children";
2153 } else if (!strcmp(arg
, "--ignore-missing")) {
2154 revs
->ignore_missing
= 1;
2155 } else if (revs
->allow_exclude_promisor_objects_opt
&&
2156 !strcmp(arg
, "--exclude-promisor-objects")) {
2157 if (fetch_if_missing
)
2158 BUG("exclude_promisor_objects can only be used when fetch_if_missing is 0");
2159 revs
->exclude_promisor_objects
= 1;
2161 int opts
= diff_opt_parse(&revs
->diffopt
, argv
, argc
, revs
->prefix
);
2163 unkv
[(*unkc
)++] = arg
;
2166 if (revs
->graph
&& revs
->track_linear
)
2167 die("--show-linear-break and --graph are incompatible");
2172 void parse_revision_opt(struct rev_info
*revs
, struct parse_opt_ctx_t
*ctx
,
2173 const struct option
*options
,
2174 const char * const usagestr
[])
2176 int n
= handle_revision_opt(revs
, ctx
->argc
, ctx
->argv
,
2177 &ctx
->cpidx
, ctx
->out
);
2179 error("unknown option `%s'", ctx
->argv
[0]);
2180 usage_with_options(usagestr
, options
);
2186 static int for_each_bisect_ref(struct ref_store
*refs
, each_ref_fn fn
,
2187 void *cb_data
, const char *term
)
2189 struct strbuf bisect_refs
= STRBUF_INIT
;
2191 strbuf_addf(&bisect_refs
, "refs/bisect/%s", term
);
2192 status
= refs_for_each_fullref_in(refs
, bisect_refs
.buf
, fn
, cb_data
, 0);
2193 strbuf_release(&bisect_refs
);
2197 static int for_each_bad_bisect_ref(struct ref_store
*refs
, each_ref_fn fn
, void *cb_data
)
2199 return for_each_bisect_ref(refs
, fn
, cb_data
, term_bad
);
2202 static int for_each_good_bisect_ref(struct ref_store
*refs
, each_ref_fn fn
, void *cb_data
)
2204 return for_each_bisect_ref(refs
, fn
, cb_data
, term_good
);
2207 static int handle_revision_pseudo_opt(const char *submodule
,
2208 struct rev_info
*revs
,
2209 int argc
, const char **argv
, int *flags
)
2211 const char *arg
= argv
[0];
2213 struct ref_store
*refs
;
2218 * We need some something like get_submodule_worktrees()
2219 * before we can go through all worktrees of a submodule,
2220 * .e.g with adding all HEADs from --all, which is not
2221 * supported right now, so stick to single worktree.
2223 if (!revs
->single_worktree
)
2224 BUG("--single-worktree cannot be used together with submodule");
2225 refs
= get_submodule_ref_store(submodule
);
2227 refs
= get_main_ref_store(revs
->repo
);
2232 * Commands like "git shortlog" will not accept the options below
2233 * unless parse_revision_opt queues them (as opposed to erroring
2236 * When implementing your new pseudo-option, remember to
2237 * register it in the list at the top of handle_revision_opt.
2239 if (!strcmp(arg
, "--all")) {
2240 handle_refs(refs
, revs
, *flags
, refs_for_each_ref
);
2241 handle_refs(refs
, revs
, *flags
, refs_head_ref
);
2242 if (!revs
->single_worktree
) {
2243 struct all_refs_cb cb
;
2245 init_all_refs_cb(&cb
, revs
, *flags
);
2246 other_head_refs(handle_one_ref
, &cb
);
2248 clear_ref_exclusion(&revs
->ref_excludes
);
2249 } else if (!strcmp(arg
, "--branches")) {
2250 handle_refs(refs
, revs
, *flags
, refs_for_each_branch_ref
);
2251 clear_ref_exclusion(&revs
->ref_excludes
);
2252 } else if (!strcmp(arg
, "--bisect")) {
2253 read_bisect_terms(&term_bad
, &term_good
);
2254 handle_refs(refs
, revs
, *flags
, for_each_bad_bisect_ref
);
2255 handle_refs(refs
, revs
, *flags
^ (UNINTERESTING
| BOTTOM
),
2256 for_each_good_bisect_ref
);
2258 } else if (!strcmp(arg
, "--tags")) {
2259 handle_refs(refs
, revs
, *flags
, refs_for_each_tag_ref
);
2260 clear_ref_exclusion(&revs
->ref_excludes
);
2261 } else if (!strcmp(arg
, "--remotes")) {
2262 handle_refs(refs
, revs
, *flags
, refs_for_each_remote_ref
);
2263 clear_ref_exclusion(&revs
->ref_excludes
);
2264 } else if ((argcount
= parse_long_opt("glob", argv
, &optarg
))) {
2265 struct all_refs_cb cb
;
2266 init_all_refs_cb(&cb
, revs
, *flags
);
2267 for_each_glob_ref(handle_one_ref
, optarg
, &cb
);
2268 clear_ref_exclusion(&revs
->ref_excludes
);
2270 } else if ((argcount
= parse_long_opt("exclude", argv
, &optarg
))) {
2271 add_ref_exclusion(&revs
->ref_excludes
, optarg
);
2273 } else if (skip_prefix(arg
, "--branches=", &optarg
)) {
2274 struct all_refs_cb cb
;
2275 init_all_refs_cb(&cb
, revs
, *flags
);
2276 for_each_glob_ref_in(handle_one_ref
, optarg
, "refs/heads/", &cb
);
2277 clear_ref_exclusion(&revs
->ref_excludes
);
2278 } else if (skip_prefix(arg
, "--tags=", &optarg
)) {
2279 struct all_refs_cb cb
;
2280 init_all_refs_cb(&cb
, revs
, *flags
);
2281 for_each_glob_ref_in(handle_one_ref
, optarg
, "refs/tags/", &cb
);
2282 clear_ref_exclusion(&revs
->ref_excludes
);
2283 } else if (skip_prefix(arg
, "--remotes=", &optarg
)) {
2284 struct all_refs_cb cb
;
2285 init_all_refs_cb(&cb
, revs
, *flags
);
2286 for_each_glob_ref_in(handle_one_ref
, optarg
, "refs/remotes/", &cb
);
2287 clear_ref_exclusion(&revs
->ref_excludes
);
2288 } else if (!strcmp(arg
, "--reflog")) {
2289 add_reflogs_to_pending(revs
, *flags
);
2290 } else if (!strcmp(arg
, "--indexed-objects")) {
2291 add_index_objects_to_pending(revs
, *flags
);
2292 } else if (!strcmp(arg
, "--not")) {
2293 *flags
^= UNINTERESTING
| BOTTOM
;
2294 } else if (!strcmp(arg
, "--no-walk")) {
2295 revs
->no_walk
= REVISION_WALK_NO_WALK_SORTED
;
2296 } else if (skip_prefix(arg
, "--no-walk=", &optarg
)) {
2298 * Detached form ("--no-walk X" as opposed to "--no-walk=X")
2299 * not allowed, since the argument is optional.
2301 if (!strcmp(optarg
, "sorted"))
2302 revs
->no_walk
= REVISION_WALK_NO_WALK_SORTED
;
2303 else if (!strcmp(optarg
, "unsorted"))
2304 revs
->no_walk
= REVISION_WALK_NO_WALK_UNSORTED
;
2306 return error("invalid argument to --no-walk");
2307 } else if (!strcmp(arg
, "--do-walk")) {
2309 } else if (!strcmp(arg
, "--single-worktree")) {
2310 revs
->single_worktree
= 1;
2318 static void NORETURN
diagnose_missing_default(const char *def
)
2321 const char *refname
;
2323 refname
= resolve_ref_unsafe(def
, 0, NULL
, &flags
);
2324 if (!refname
|| !(flags
& REF_ISSYMREF
) || (flags
& REF_ISBROKEN
))
2325 die(_("your current branch appears to be broken"));
2327 skip_prefix(refname
, "refs/heads/", &refname
);
2328 die(_("your current branch '%s' does not have any commits yet"),
2333 * Parse revision information, filling in the "rev_info" structure,
2334 * and removing the used arguments from the argument list.
2336 * Returns the number of arguments left that weren't recognized
2337 * (which are also moved to the head of the argument list)
2339 int setup_revisions(int argc
, const char **argv
, struct rev_info
*revs
, struct setup_revision_opt
*opt
)
2341 int i
, flags
, left
, seen_dashdash
, got_rev_arg
= 0, revarg_opt
;
2342 struct argv_array prune_data
= ARGV_ARRAY_INIT
;
2343 const char *submodule
= NULL
;
2346 submodule
= opt
->submodule
;
2348 /* First, search for "--" */
2349 if (opt
&& opt
->assume_dashdash
) {
2353 for (i
= 1; i
< argc
; i
++) {
2354 const char *arg
= argv
[i
];
2355 if (strcmp(arg
, "--"))
2360 argv_array_pushv(&prune_data
, argv
+ i
+ 1);
2366 /* Second, deal with arguments and options */
2368 revarg_opt
= opt
? opt
->revarg_opt
: 0;
2370 revarg_opt
|= REVARG_CANNOT_BE_FILENAME
;
2371 for (left
= i
= 1; i
< argc
; i
++) {
2372 const char *arg
= argv
[i
];
2376 opts
= handle_revision_pseudo_opt(submodule
,
2377 revs
, argc
- i
, argv
+ i
,
2384 if (!strcmp(arg
, "--stdin")) {
2385 if (revs
->disable_stdin
) {
2389 if (revs
->read_from_stdin
++)
2390 die("--stdin given twice?");
2391 read_revisions_from_stdin(revs
, &prune_data
);
2395 opts
= handle_revision_opt(revs
, argc
- i
, argv
+ i
, &left
, argv
);
2406 if (handle_revision_arg(arg
, revs
, flags
, revarg_opt
)) {
2408 if (seen_dashdash
|| *arg
== '^')
2409 die("bad revision '%s'", arg
);
2411 /* If we didn't have a "--":
2412 * (1) all filenames must exist;
2413 * (2) all rev-args must not be interpretable
2414 * as a valid filename.
2415 * but the latter we have checked in the main loop.
2417 for (j
= i
; j
< argc
; j
++)
2418 verify_filename(revs
->prefix
, argv
[j
], j
== i
);
2420 argv_array_pushv(&prune_data
, argv
+ i
);
2427 if (prune_data
.argc
) {
2429 * If we need to introduce the magic "a lone ':' means no
2430 * pathspec whatsoever", here is the place to do so.
2432 * if (prune_data.nr == 1 && !strcmp(prune_data[0], ":")) {
2433 * prune_data.nr = 0;
2434 * prune_data.alloc = 0;
2435 * free(prune_data.path);
2436 * prune_data.path = NULL;
2438 * terminate prune_data.alloc with NULL and
2439 * call init_pathspec() to set revs->prune_data here.
2442 parse_pathspec(&revs
->prune_data
, 0, 0,
2443 revs
->prefix
, prune_data
.argv
);
2445 argv_array_clear(&prune_data
);
2447 if (revs
->def
== NULL
)
2448 revs
->def
= opt
? opt
->def
: NULL
;
2449 if (opt
&& opt
->tweak
)
2450 opt
->tweak(revs
, opt
);
2451 if (revs
->show_merge
)
2452 prepare_show_merge(revs
);
2453 if (revs
->def
&& !revs
->pending
.nr
&& !revs
->rev_input_given
&& !got_rev_arg
) {
2454 struct object_id oid
;
2455 struct object
*object
;
2456 struct object_context oc
;
2457 if (get_oid_with_context(revs
->def
, 0, &oid
, &oc
))
2458 diagnose_missing_default(revs
->def
);
2459 object
= get_reference(revs
, revs
->def
, &oid
, 0);
2460 add_pending_object_with_mode(revs
, object
, revs
->def
, oc
.mode
);
2463 /* Did the user ask for any diff output? Run the diff! */
2464 if (revs
->diffopt
.output_format
& ~DIFF_FORMAT_NO_OUTPUT
)
2467 /* Pickaxe, diff-filter and rename following need diffs */
2468 if ((revs
->diffopt
.pickaxe_opts
& DIFF_PICKAXE_KINDS_MASK
) ||
2469 revs
->diffopt
.filter
||
2470 revs
->diffopt
.flags
.follow_renames
)
2473 if (revs
->diffopt
.objfind
)
2474 revs
->simplify_history
= 0;
2476 if (revs
->topo_order
&& !generation_numbers_enabled(the_repository
))
2479 if (revs
->prune_data
.nr
) {
2480 copy_pathspec(&revs
->pruning
.pathspec
, &revs
->prune_data
);
2481 /* Can't prune commits with rename following: the paths change.. */
2482 if (!revs
->diffopt
.flags
.follow_renames
)
2484 if (!revs
->full_diff
)
2485 copy_pathspec(&revs
->diffopt
.pathspec
,
2488 if (revs
->combine_merges
)
2489 revs
->ignore_merges
= 0;
2490 revs
->diffopt
.abbrev
= revs
->abbrev
;
2492 if (revs
->line_level_traverse
) {
2494 revs
->topo_order
= 1;
2497 diff_setup_done(&revs
->diffopt
);
2499 grep_commit_pattern_type(GREP_PATTERN_TYPE_UNSPECIFIED
,
2500 &revs
->grep_filter
);
2501 compile_grep_patterns(&revs
->grep_filter
);
2503 if (revs
->reverse
&& revs
->reflog_info
)
2504 die("cannot combine --reverse with --walk-reflogs");
2505 if (revs
->reflog_info
&& revs
->limited
)
2506 die("cannot combine --walk-reflogs with history-limiting options");
2507 if (revs
->rewrite_parents
&& revs
->children
.name
)
2508 die("cannot combine --parents and --children");
2511 * Limitations on the graph functionality
2513 if (revs
->reverse
&& revs
->graph
)
2514 die("cannot combine --reverse with --graph");
2516 if (revs
->reflog_info
&& revs
->graph
)
2517 die("cannot combine --walk-reflogs with --graph");
2518 if (revs
->no_walk
&& revs
->graph
)
2519 die("cannot combine --no-walk with --graph");
2520 if (!revs
->reflog_info
&& revs
->grep_filter
.use_reflog_filter
)
2521 die("cannot use --grep-reflog without --walk-reflogs");
2523 if (revs
->first_parent_only
&& revs
->bisect
)
2524 die(_("--first-parent is incompatible with --bisect"));
2526 if (revs
->expand_tabs_in_log
< 0)
2527 revs
->expand_tabs_in_log
= revs
->expand_tabs_in_log_default
;
2532 static void add_child(struct rev_info
*revs
, struct commit
*parent
, struct commit
*child
)
2534 struct commit_list
*l
= xcalloc(1, sizeof(*l
));
2537 l
->next
= add_decoration(&revs
->children
, &parent
->object
, l
);
2540 static int remove_duplicate_parents(struct rev_info
*revs
, struct commit
*commit
)
2542 struct treesame_state
*ts
= lookup_decoration(&revs
->treesame
, &commit
->object
);
2543 struct commit_list
**pp
, *p
;
2544 int surviving_parents
;
2546 /* Examine existing parents while marking ones we have seen... */
2547 pp
= &commit
->parents
;
2548 surviving_parents
= 0;
2549 while ((p
= *pp
) != NULL
) {
2550 struct commit
*parent
= p
->item
;
2551 if (parent
->object
.flags
& TMP_MARK
) {
2554 compact_treesame(revs
, commit
, surviving_parents
);
2557 parent
->object
.flags
|= TMP_MARK
;
2558 surviving_parents
++;
2561 /* clear the temporary mark */
2562 for (p
= commit
->parents
; p
; p
= p
->next
) {
2563 p
->item
->object
.flags
&= ~TMP_MARK
;
2565 /* no update_treesame() - removing duplicates can't affect TREESAME */
2566 return surviving_parents
;
2569 struct merge_simplify_state
{
2570 struct commit
*simplified
;
2573 static struct merge_simplify_state
*locate_simplify_state(struct rev_info
*revs
, struct commit
*commit
)
2575 struct merge_simplify_state
*st
;
2577 st
= lookup_decoration(&revs
->merge_simplification
, &commit
->object
);
2579 st
= xcalloc(1, sizeof(*st
));
2580 add_decoration(&revs
->merge_simplification
, &commit
->object
, st
);
2585 static int mark_redundant_parents(struct rev_info
*revs
, struct commit
*commit
)
2587 struct commit_list
*h
= reduce_heads(commit
->parents
);
2588 int i
= 0, marked
= 0;
2589 struct commit_list
*po
, *pn
;
2591 /* Want these for sanity-checking only */
2592 int orig_cnt
= commit_list_count(commit
->parents
);
2593 int cnt
= commit_list_count(h
);
2596 * Not ready to remove items yet, just mark them for now, based
2597 * on the output of reduce_heads(). reduce_heads outputs the reduced
2598 * set in its original order, so this isn't too hard.
2600 po
= commit
->parents
;
2603 if (pn
&& po
->item
== pn
->item
) {
2607 po
->item
->object
.flags
|= TMP_MARK
;
2613 if (i
!= cnt
|| cnt
+marked
!= orig_cnt
)
2614 die("mark_redundant_parents %d %d %d %d", orig_cnt
, cnt
, i
, marked
);
2616 free_commit_list(h
);
2621 static int mark_treesame_root_parents(struct rev_info
*revs
, struct commit
*commit
)
2623 struct commit_list
*p
;
2626 for (p
= commit
->parents
; p
; p
= p
->next
) {
2627 struct commit
*parent
= p
->item
;
2628 if (!parent
->parents
&& (parent
->object
.flags
& TREESAME
)) {
2629 parent
->object
.flags
|= TMP_MARK
;
2638 * Awkward naming - this means one parent we are TREESAME to.
2639 * cf mark_treesame_root_parents: root parents that are TREESAME (to an
2640 * empty tree). Better name suggestions?
2642 static int leave_one_treesame_to_parent(struct rev_info
*revs
, struct commit
*commit
)
2644 struct treesame_state
*ts
= lookup_decoration(&revs
->treesame
, &commit
->object
);
2645 struct commit
*unmarked
= NULL
, *marked
= NULL
;
2646 struct commit_list
*p
;
2649 for (p
= commit
->parents
, n
= 0; p
; p
= p
->next
, n
++) {
2650 if (ts
->treesame
[n
]) {
2651 if (p
->item
->object
.flags
& TMP_MARK
) {
2664 * If we are TREESAME to a marked-for-deletion parent, but not to any
2665 * unmarked parents, unmark the first TREESAME parent. This is the
2666 * parent that the default simplify_history==1 scan would have followed,
2667 * and it doesn't make sense to omit that path when asking for a
2668 * simplified full history. Retaining it improves the chances of
2669 * understanding odd missed merges that took an old version of a file.
2673 * I--------*X A modified the file, but mainline merge X used
2674 * \ / "-s ours", so took the version from I. X is
2675 * `-*A--' TREESAME to I and !TREESAME to A.
2677 * Default log from X would produce "I". Without this check,
2678 * --full-history --simplify-merges would produce "I-A-X", showing
2679 * the merge commit X and that it changed A, but not making clear that
2680 * it had just taken the I version. With this check, the topology above
2683 * Note that it is possible that the simplification chooses a different
2684 * TREESAME parent from the default, in which case this test doesn't
2685 * activate, and we _do_ drop the default parent. Example:
2687 * I------X A modified the file, but it was reverted in B,
2688 * \ / meaning mainline merge X is TREESAME to both
2691 * Default log would produce "I" by following the first parent;
2692 * --full-history --simplify-merges will produce "I-A-B". But this is a
2693 * reasonable result - it presents a logical full history leading from
2694 * I to X, and X is not an important merge.
2696 if (!unmarked
&& marked
) {
2697 marked
->object
.flags
&= ~TMP_MARK
;
2704 static int remove_marked_parents(struct rev_info
*revs
, struct commit
*commit
)
2706 struct commit_list
**pp
, *p
;
2707 int nth_parent
, removed
= 0;
2709 pp
= &commit
->parents
;
2711 while ((p
= *pp
) != NULL
) {
2712 struct commit
*parent
= p
->item
;
2713 if (parent
->object
.flags
& TMP_MARK
) {
2714 parent
->object
.flags
&= ~TMP_MARK
;
2718 compact_treesame(revs
, commit
, nth_parent
);
2725 /* Removing parents can only increase TREESAMEness */
2726 if (removed
&& !(commit
->object
.flags
& TREESAME
))
2727 update_treesame(revs
, commit
);
2732 static struct commit_list
**simplify_one(struct rev_info
*revs
, struct commit
*commit
, struct commit_list
**tail
)
2734 struct commit_list
*p
;
2735 struct commit
*parent
;
2736 struct merge_simplify_state
*st
, *pst
;
2739 st
= locate_simplify_state(revs
, commit
);
2742 * Have we handled this one?
2748 * An UNINTERESTING commit simplifies to itself, so does a
2749 * root commit. We do not rewrite parents of such commit
2752 if ((commit
->object
.flags
& UNINTERESTING
) || !commit
->parents
) {
2753 st
->simplified
= commit
;
2758 * Do we know what commit all of our parents that matter
2759 * should be rewritten to? Otherwise we are not ready to
2760 * rewrite this one yet.
2762 for (cnt
= 0, p
= commit
->parents
; p
; p
= p
->next
) {
2763 pst
= locate_simplify_state(revs
, p
->item
);
2764 if (!pst
->simplified
) {
2765 tail
= &commit_list_insert(p
->item
, tail
)->next
;
2768 if (revs
->first_parent_only
)
2772 tail
= &commit_list_insert(commit
, tail
)->next
;
2777 * Rewrite our list of parents. Note that this cannot
2778 * affect our TREESAME flags in any way - a commit is
2779 * always TREESAME to its simplification.
2781 for (p
= commit
->parents
; p
; p
= p
->next
) {
2782 pst
= locate_simplify_state(revs
, p
->item
);
2783 p
->item
= pst
->simplified
;
2784 if (revs
->first_parent_only
)
2788 if (revs
->first_parent_only
)
2791 cnt
= remove_duplicate_parents(revs
, commit
);
2794 * It is possible that we are a merge and one side branch
2795 * does not have any commit that touches the given paths;
2796 * in such a case, the immediate parent from that branch
2797 * will be rewritten to be the merge base.
2799 * o----X X: the commit we are looking at;
2800 * / / o: a commit that touches the paths;
2803 * Further, a merge of an independent branch that doesn't
2804 * touch the path will reduce to a treesame root parent:
2806 * ----o----X X: the commit we are looking at;
2807 * / o: a commit that touches the paths;
2808 * r r: a root commit not touching the paths
2810 * Detect and simplify both cases.
2813 int marked
= mark_redundant_parents(revs
, commit
);
2814 marked
+= mark_treesame_root_parents(revs
, commit
);
2816 marked
-= leave_one_treesame_to_parent(revs
, commit
);
2818 cnt
= remove_marked_parents(revs
, commit
);
2822 * A commit simplifies to itself if it is a root, if it is
2823 * UNINTERESTING, if it touches the given paths, or if it is a
2824 * merge and its parents don't simplify to one relevant commit
2825 * (the first two cases are already handled at the beginning of
2828 * Otherwise, it simplifies to what its sole relevant parent
2832 (commit
->object
.flags
& UNINTERESTING
) ||
2833 !(commit
->object
.flags
& TREESAME
) ||
2834 (parent
= one_relevant_parent(revs
, commit
->parents
)) == NULL
)
2835 st
->simplified
= commit
;
2837 pst
= locate_simplify_state(revs
, parent
);
2838 st
->simplified
= pst
->simplified
;
2843 static void simplify_merges(struct rev_info
*revs
)
2845 struct commit_list
*list
, *next
;
2846 struct commit_list
*yet_to_do
, **tail
;
2847 struct commit
*commit
;
2852 /* feed the list reversed */
2854 for (list
= revs
->commits
; list
; list
= next
) {
2855 commit
= list
->item
;
2858 * Do not free(list) here yet; the original list
2859 * is used later in this function.
2861 commit_list_insert(commit
, &yet_to_do
);
2868 commit
= pop_commit(&list
);
2869 tail
= simplify_one(revs
, commit
, tail
);
2873 /* clean up the result, removing the simplified ones */
2874 list
= revs
->commits
;
2875 revs
->commits
= NULL
;
2876 tail
= &revs
->commits
;
2878 struct merge_simplify_state
*st
;
2880 commit
= pop_commit(&list
);
2881 st
= locate_simplify_state(revs
, commit
);
2882 if (st
->simplified
== commit
)
2883 tail
= &commit_list_insert(commit
, tail
)->next
;
2887 static void set_children(struct rev_info
*revs
)
2889 struct commit_list
*l
;
2890 for (l
= revs
->commits
; l
; l
= l
->next
) {
2891 struct commit
*commit
= l
->item
;
2892 struct commit_list
*p
;
2894 for (p
= commit
->parents
; p
; p
= p
->next
)
2895 add_child(revs
, p
->item
, commit
);
2899 void reset_revision_walk(void)
2901 clear_object_flags(SEEN
| ADDED
| SHOWN
);
2904 static int mark_uninteresting(const struct object_id
*oid
,
2905 struct packed_git
*pack
,
2909 struct rev_info
*revs
= cb
;
2910 struct object
*o
= parse_object(revs
->repo
, oid
);
2911 o
->flags
|= UNINTERESTING
| SEEN
;
2915 define_commit_slab(indegree_slab
, int);
2916 define_commit_slab(author_date_slab
, timestamp_t
);
2918 struct topo_walk_info
{
2919 uint32_t min_generation
;
2920 struct prio_queue explore_queue
;
2921 struct prio_queue indegree_queue
;
2922 struct prio_queue topo_queue
;
2923 struct indegree_slab indegree
;
2924 struct author_date_slab author_date
;
2927 static inline void test_flag_and_insert(struct prio_queue
*q
, struct commit
*c
, int flag
)
2929 if (c
->object
.flags
& flag
)
2932 c
->object
.flags
|= flag
;
2933 prio_queue_put(q
, c
);
2936 static void explore_walk_step(struct rev_info
*revs
)
2938 struct topo_walk_info
*info
= revs
->topo_walk_info
;
2939 struct commit_list
*p
;
2940 struct commit
*c
= prio_queue_get(&info
->explore_queue
);
2945 if (parse_commit_gently(c
, 1) < 0)
2948 if (revs
->sort_order
== REV_SORT_BY_AUTHOR_DATE
)
2949 record_author_date(&info
->author_date
, c
);
2951 if (revs
->max_age
!= -1 && (c
->date
< revs
->max_age
))
2952 c
->object
.flags
|= UNINTERESTING
;
2954 if (process_parents(revs
, c
, NULL
, NULL
) < 0)
2957 if (c
->object
.flags
& UNINTERESTING
)
2958 mark_parents_uninteresting(c
);
2960 for (p
= c
->parents
; p
; p
= p
->next
)
2961 test_flag_and_insert(&info
->explore_queue
, p
->item
, TOPO_WALK_EXPLORED
);
2964 static void explore_to_depth(struct rev_info
*revs
,
2965 uint32_t gen_cutoff
)
2967 struct topo_walk_info
*info
= revs
->topo_walk_info
;
2969 while ((c
= prio_queue_peek(&info
->explore_queue
)) &&
2970 c
->generation
>= gen_cutoff
)
2971 explore_walk_step(revs
);
2974 static void indegree_walk_step(struct rev_info
*revs
)
2976 struct commit_list
*p
;
2977 struct topo_walk_info
*info
= revs
->topo_walk_info
;
2978 struct commit
*c
= prio_queue_get(&info
->indegree_queue
);
2983 if (parse_commit_gently(c
, 1) < 0)
2986 explore_to_depth(revs
, c
->generation
);
2988 for (p
= c
->parents
; p
; p
= p
->next
) {
2989 struct commit
*parent
= p
->item
;
2990 int *pi
= indegree_slab_at(&info
->indegree
, parent
);
2997 test_flag_and_insert(&info
->indegree_queue
, parent
, TOPO_WALK_INDEGREE
);
2999 if (revs
->first_parent_only
)
3004 static void compute_indegrees_to_depth(struct rev_info
*revs
,
3005 uint32_t gen_cutoff
)
3007 struct topo_walk_info
*info
= revs
->topo_walk_info
;
3009 while ((c
= prio_queue_peek(&info
->indegree_queue
)) &&
3010 c
->generation
>= gen_cutoff
)
3011 indegree_walk_step(revs
);
3014 static void init_topo_walk(struct rev_info
*revs
)
3016 struct topo_walk_info
*info
;
3017 struct commit_list
*list
;
3018 revs
->topo_walk_info
= xmalloc(sizeof(struct topo_walk_info
));
3019 info
= revs
->topo_walk_info
;
3020 memset(info
, 0, sizeof(struct topo_walk_info
));
3022 init_indegree_slab(&info
->indegree
);
3023 memset(&info
->explore_queue
, 0, sizeof(info
->explore_queue
));
3024 memset(&info
->indegree_queue
, 0, sizeof(info
->indegree_queue
));
3025 memset(&info
->topo_queue
, 0, sizeof(info
->topo_queue
));
3027 switch (revs
->sort_order
) {
3028 default: /* REV_SORT_IN_GRAPH_ORDER */
3029 info
->topo_queue
.compare
= NULL
;
3031 case REV_SORT_BY_COMMIT_DATE
:
3032 info
->topo_queue
.compare
= compare_commits_by_commit_date
;
3034 case REV_SORT_BY_AUTHOR_DATE
:
3035 init_author_date_slab(&info
->author_date
);
3036 info
->topo_queue
.compare
= compare_commits_by_author_date
;
3037 info
->topo_queue
.cb_data
= &info
->author_date
;
3041 info
->explore_queue
.compare
= compare_commits_by_gen_then_commit_date
;
3042 info
->indegree_queue
.compare
= compare_commits_by_gen_then_commit_date
;
3044 info
->min_generation
= GENERATION_NUMBER_INFINITY
;
3045 for (list
= revs
->commits
; list
; list
= list
->next
) {
3046 struct commit
*c
= list
->item
;
3048 if (parse_commit_gently(c
, 1))
3051 test_flag_and_insert(&info
->explore_queue
, c
, TOPO_WALK_EXPLORED
);
3052 test_flag_and_insert(&info
->indegree_queue
, c
, TOPO_WALK_INDEGREE
);
3054 if (c
->generation
< info
->min_generation
)
3055 info
->min_generation
= c
->generation
;
3057 *(indegree_slab_at(&info
->indegree
, c
)) = 1;
3059 if (revs
->sort_order
== REV_SORT_BY_AUTHOR_DATE
)
3060 record_author_date(&info
->author_date
, c
);
3062 compute_indegrees_to_depth(revs
, info
->min_generation
);
3064 for (list
= revs
->commits
; list
; list
= list
->next
) {
3065 struct commit
*c
= list
->item
;
3067 if (*(indegree_slab_at(&info
->indegree
, c
)) == 1)
3068 prio_queue_put(&info
->topo_queue
, c
);
3072 * This is unfortunate; the initial tips need to be shown
3073 * in the order given from the revision traversal machinery.
3075 if (revs
->sort_order
== REV_SORT_IN_GRAPH_ORDER
)
3076 prio_queue_reverse(&info
->topo_queue
);
3079 static struct commit
*next_topo_commit(struct rev_info
*revs
)
3082 struct topo_walk_info
*info
= revs
->topo_walk_info
;
3084 /* pop next off of topo_queue */
3085 c
= prio_queue_get(&info
->topo_queue
);
3088 *(indegree_slab_at(&info
->indegree
, c
)) = 0;
3093 static void expand_topo_walk(struct rev_info
*revs
, struct commit
*commit
)
3095 struct commit_list
*p
;
3096 struct topo_walk_info
*info
= revs
->topo_walk_info
;
3097 if (process_parents(revs
, commit
, NULL
, NULL
) < 0) {
3098 if (!revs
->ignore_missing_links
)
3099 die("Failed to traverse parents of commit %s",
3100 oid_to_hex(&commit
->object
.oid
));
3103 for (p
= commit
->parents
; p
; p
= p
->next
) {
3104 struct commit
*parent
= p
->item
;
3107 if (parse_commit_gently(parent
, 1) < 0)
3110 if (parent
->generation
< info
->min_generation
) {
3111 info
->min_generation
= parent
->generation
;
3112 compute_indegrees_to_depth(revs
, info
->min_generation
);
3115 pi
= indegree_slab_at(&info
->indegree
, parent
);
3119 prio_queue_put(&info
->topo_queue
, parent
);
3121 if (revs
->first_parent_only
)
3126 int prepare_revision_walk(struct rev_info
*revs
)
3129 struct object_array old_pending
;
3130 struct commit_list
**next
= &revs
->commits
;
3132 memcpy(&old_pending
, &revs
->pending
, sizeof(old_pending
));
3133 revs
->pending
.nr
= 0;
3134 revs
->pending
.alloc
= 0;
3135 revs
->pending
.objects
= NULL
;
3136 for (i
= 0; i
< old_pending
.nr
; i
++) {
3137 struct object_array_entry
*e
= old_pending
.objects
+ i
;
3138 struct commit
*commit
= handle_commit(revs
, e
);
3140 if (!(commit
->object
.flags
& SEEN
)) {
3141 commit
->object
.flags
|= SEEN
;
3142 next
= commit_list_append(commit
, next
);
3146 object_array_clear(&old_pending
);
3148 /* Signal whether we need per-parent treesame decoration */
3149 if (revs
->simplify_merges
||
3150 (revs
->limited
&& limiting_can_increase_treesame(revs
)))
3151 revs
->treesame
.name
= "treesame";
3153 if (revs
->exclude_promisor_objects
) {
3154 for_each_packed_object(mark_uninteresting
, revs
,
3155 FOR_EACH_OBJECT_PROMISOR_ONLY
);
3158 if (revs
->no_walk
!= REVISION_WALK_NO_WALK_UNSORTED
)
3159 commit_list_sort_by_date(&revs
->commits
);
3162 if (revs
->limited
) {
3163 if (limit_list(revs
) < 0)
3165 if (revs
->topo_order
)
3166 sort_in_topological_order(&revs
->commits
, revs
->sort_order
);
3167 } else if (revs
->topo_order
)
3168 init_topo_walk(revs
);
3169 if (revs
->line_level_traverse
)
3170 line_log_filter(revs
);
3171 if (revs
->simplify_merges
)
3172 simplify_merges(revs
);
3173 if (revs
->children
.name
)
3178 static enum rewrite_result
rewrite_one(struct rev_info
*revs
, struct commit
**pp
)
3180 struct commit_list
*cache
= NULL
;
3183 struct commit
*p
= *pp
;
3185 if (process_parents(revs
, p
, &revs
->commits
, &cache
) < 0)
3186 return rewrite_one_error
;
3187 if (p
->object
.flags
& UNINTERESTING
)
3188 return rewrite_one_ok
;
3189 if (!(p
->object
.flags
& TREESAME
))
3190 return rewrite_one_ok
;
3192 return rewrite_one_noparents
;
3193 if ((p
= one_relevant_parent(revs
, p
->parents
)) == NULL
)
3194 return rewrite_one_ok
;
3199 int rewrite_parents(struct rev_info
*revs
, struct commit
*commit
,
3200 rewrite_parent_fn_t rewrite_parent
)
3202 struct commit_list
**pp
= &commit
->parents
;
3204 struct commit_list
*parent
= *pp
;
3205 switch (rewrite_parent(revs
, &parent
->item
)) {
3206 case rewrite_one_ok
:
3208 case rewrite_one_noparents
:
3211 case rewrite_one_error
:
3216 remove_duplicate_parents(revs
, commit
);
3220 static int commit_rewrite_person(struct strbuf
*buf
, const char *what
, struct string_list
*mailmap
)
3222 char *person
, *endp
;
3223 size_t len
, namelen
, maillen
;
3226 struct ident_split ident
;
3228 person
= strstr(buf
->buf
, what
);
3232 person
+= strlen(what
);
3233 endp
= strchr(person
, '\n');
3237 len
= endp
- person
;
3239 if (split_ident_line(&ident
, person
, len
))
3242 mail
= ident
.mail_begin
;
3243 maillen
= ident
.mail_end
- ident
.mail_begin
;
3244 name
= ident
.name_begin
;
3245 namelen
= ident
.name_end
- ident
.name_begin
;
3247 if (map_user(mailmap
, &mail
, &maillen
, &name
, &namelen
)) {
3248 struct strbuf namemail
= STRBUF_INIT
;
3250 strbuf_addf(&namemail
, "%.*s <%.*s>",
3251 (int)namelen
, name
, (int)maillen
, mail
);
3253 strbuf_splice(buf
, ident
.name_begin
- buf
->buf
,
3254 ident
.mail_end
- ident
.name_begin
+ 1,
3255 namemail
.buf
, namemail
.len
);
3257 strbuf_release(&namemail
);
3265 static int commit_match(struct commit
*commit
, struct rev_info
*opt
)
3268 const char *encoding
;
3269 const char *message
;
3270 struct strbuf buf
= STRBUF_INIT
;
3272 if (!opt
->grep_filter
.pattern_list
&& !opt
->grep_filter
.header_list
)
3275 /* Prepend "fake" headers as needed */
3276 if (opt
->grep_filter
.use_reflog_filter
) {
3277 strbuf_addstr(&buf
, "reflog ");
3278 get_reflog_message(&buf
, opt
->reflog_info
);
3279 strbuf_addch(&buf
, '\n');
3283 * We grep in the user's output encoding, under the assumption that it
3284 * is the encoding they are most likely to write their grep pattern
3285 * for. In addition, it means we will match the "notes" encoding below,
3286 * so we will not end up with a buffer that has two different encodings
3289 encoding
= get_log_output_encoding();
3290 message
= logmsg_reencode(commit
, NULL
, encoding
);
3292 /* Copy the commit to temporary if we are using "fake" headers */
3294 strbuf_addstr(&buf
, message
);
3296 if (opt
->grep_filter
.header_list
&& opt
->mailmap
) {
3298 strbuf_addstr(&buf
, message
);
3300 commit_rewrite_person(&buf
, "\nauthor ", opt
->mailmap
);
3301 commit_rewrite_person(&buf
, "\ncommitter ", opt
->mailmap
);
3304 /* Append "fake" message parts as needed */
3305 if (opt
->show_notes
) {
3307 strbuf_addstr(&buf
, message
);
3308 format_display_notes(&commit
->object
.oid
, &buf
, encoding
, 1);
3312 * Find either in the original commit message, or in the temporary.
3313 * Note that we cast away the constness of "message" here. It is
3314 * const because it may come from the cached commit buffer. That's OK,
3315 * because we know that it is modifiable heap memory, and that while
3316 * grep_buffer may modify it for speed, it will restore any
3317 * changes before returning.
3320 retval
= grep_buffer(&opt
->grep_filter
, buf
.buf
, buf
.len
);
3322 retval
= grep_buffer(&opt
->grep_filter
,
3323 (char *)message
, strlen(message
));
3324 strbuf_release(&buf
);
3325 unuse_commit_buffer(commit
, message
);
3326 return opt
->invert_grep
? !retval
: retval
;
3329 static inline int want_ancestry(const struct rev_info
*revs
)
3331 return (revs
->rewrite_parents
|| revs
->children
.name
);
3335 * Return a timestamp to be used for --since/--until comparisons for this
3336 * commit, based on the revision options.
3338 static timestamp_t
comparison_date(const struct rev_info
*revs
,
3339 struct commit
*commit
)
3341 return revs
->reflog_info
?
3342 get_reflog_timestamp(revs
->reflog_info
) :
3346 enum commit_action
get_commit_action(struct rev_info
*revs
, struct commit
*commit
)
3348 if (commit
->object
.flags
& SHOWN
)
3349 return commit_ignore
;
3350 if (revs
->unpacked
&& has_object_pack(&commit
->object
.oid
))
3351 return commit_ignore
;
3352 if (commit
->object
.flags
& UNINTERESTING
)
3353 return commit_ignore
;
3354 if (revs
->min_age
!= -1 &&
3355 comparison_date(revs
, commit
) > revs
->min_age
)
3356 return commit_ignore
;
3357 if (revs
->min_parents
|| (revs
->max_parents
>= 0)) {
3358 int n
= commit_list_count(commit
->parents
);
3359 if ((n
< revs
->min_parents
) ||
3360 ((revs
->max_parents
>= 0) && (n
> revs
->max_parents
)))
3361 return commit_ignore
;
3363 if (!commit_match(commit
, revs
))
3364 return commit_ignore
;
3365 if (revs
->prune
&& revs
->dense
) {
3366 /* Commit without changes? */
3367 if (commit
->object
.flags
& TREESAME
) {
3369 struct commit_list
*p
;
3370 /* drop merges unless we want parenthood */
3371 if (!want_ancestry(revs
))
3372 return commit_ignore
;
3374 * If we want ancestry, then need to keep any merges
3375 * between relevant commits to tie together topology.
3376 * For consistency with TREESAME and simplification
3377 * use "relevant" here rather than just INTERESTING,
3378 * to treat bottom commit(s) as part of the topology.
3380 for (n
= 0, p
= commit
->parents
; p
; p
= p
->next
)
3381 if (relevant_commit(p
->item
))
3384 return commit_ignore
;
3390 define_commit_slab(saved_parents
, struct commit_list
*);
3392 #define EMPTY_PARENT_LIST ((struct commit_list *)-1)
3395 * You may only call save_parents() once per commit (this is checked
3396 * for non-root commits).
3398 static void save_parents(struct rev_info
*revs
, struct commit
*commit
)
3400 struct commit_list
**pp
;
3402 if (!revs
->saved_parents_slab
) {
3403 revs
->saved_parents_slab
= xmalloc(sizeof(struct saved_parents
));
3404 init_saved_parents(revs
->saved_parents_slab
);
3407 pp
= saved_parents_at(revs
->saved_parents_slab
, commit
);
3410 * When walking with reflogs, we may visit the same commit
3411 * several times: once for each appearance in the reflog.
3413 * In this case, save_parents() will be called multiple times.
3414 * We want to keep only the first set of parents. We need to
3415 * store a sentinel value for an empty (i.e., NULL) parent
3416 * list to distinguish it from a not-yet-saved list, however.
3420 if (commit
->parents
)
3421 *pp
= copy_commit_list(commit
->parents
);
3423 *pp
= EMPTY_PARENT_LIST
;
3426 static void free_saved_parents(struct rev_info
*revs
)
3428 if (revs
->saved_parents_slab
)
3429 clear_saved_parents(revs
->saved_parents_slab
);
3432 struct commit_list
*get_saved_parents(struct rev_info
*revs
, const struct commit
*commit
)
3434 struct commit_list
*parents
;
3436 if (!revs
->saved_parents_slab
)
3437 return commit
->parents
;
3439 parents
= *saved_parents_at(revs
->saved_parents_slab
, commit
);
3440 if (parents
== EMPTY_PARENT_LIST
)
3445 enum commit_action
simplify_commit(struct rev_info
*revs
, struct commit
*commit
)
3447 enum commit_action action
= get_commit_action(revs
, commit
);
3449 if (action
== commit_show
&&
3450 revs
->prune
&& revs
->dense
&& want_ancestry(revs
)) {
3452 * --full-diff on simplified parents is no good: it
3453 * will show spurious changes from the commits that
3454 * were elided. So we save the parents on the side
3455 * when --full-diff is in effect.
3457 if (revs
->full_diff
)
3458 save_parents(revs
, commit
);
3459 if (rewrite_parents(revs
, commit
, rewrite_one
) < 0)
3460 return commit_error
;
3465 static void track_linear(struct rev_info
*revs
, struct commit
*commit
)
3467 if (revs
->track_first_time
) {
3469 revs
->track_first_time
= 0;
3471 struct commit_list
*p
;
3472 for (p
= revs
->previous_parents
; p
; p
= p
->next
)
3473 if (p
->item
== NULL
|| /* first commit */
3474 oideq(&p
->item
->object
.oid
, &commit
->object
.oid
))
3476 revs
->linear
= p
!= NULL
;
3478 if (revs
->reverse
) {
3480 commit
->object
.flags
|= TRACK_LINEAR
;
3482 free_commit_list(revs
->previous_parents
);
3483 revs
->previous_parents
= copy_commit_list(commit
->parents
);
3486 static struct commit
*get_revision_1(struct rev_info
*revs
)
3489 struct commit
*commit
;
3491 if (revs
->reflog_info
)
3492 commit
= next_reflog_entry(revs
->reflog_info
);
3493 else if (revs
->topo_walk_info
)
3494 commit
= next_topo_commit(revs
);
3496 commit
= pop_commit(&revs
->commits
);
3501 if (revs
->reflog_info
)
3502 commit
->object
.flags
&= ~(ADDED
| SEEN
| SHOWN
);
3505 * If we haven't done the list limiting, we need to look at
3506 * the parents here. We also need to do the date-based limiting
3507 * that we'd otherwise have done in limit_list().
3509 if (!revs
->limited
) {
3510 if (revs
->max_age
!= -1 &&
3511 comparison_date(revs
, commit
) < revs
->max_age
)
3514 if (revs
->reflog_info
)
3515 try_to_simplify_commit(revs
, commit
);
3516 else if (revs
->topo_walk_info
)
3517 expand_topo_walk(revs
, commit
);
3518 else if (process_parents(revs
, commit
, &revs
->commits
, NULL
) < 0) {
3519 if (!revs
->ignore_missing_links
)
3520 die("Failed to traverse parents of commit %s",
3521 oid_to_hex(&commit
->object
.oid
));
3525 switch (simplify_commit(revs
, commit
)) {
3529 die("Failed to simplify parents of commit %s",
3530 oid_to_hex(&commit
->object
.oid
));
3532 if (revs
->track_linear
)
3533 track_linear(revs
, commit
);
3540 * Return true for entries that have not yet been shown. (This is an
3541 * object_array_each_func_t.)
3543 static int entry_unshown(struct object_array_entry
*entry
, void *cb_data_unused
)
3545 return !(entry
->item
->flags
& SHOWN
);
3549 * If array is on the verge of a realloc, garbage-collect any entries
3550 * that have already been shown to try to free up some space.
3552 static void gc_boundary(struct object_array
*array
)
3554 if (array
->nr
== array
->alloc
)
3555 object_array_filter(array
, entry_unshown
, NULL
);
3558 static void create_boundary_commit_list(struct rev_info
*revs
)
3562 struct object_array
*array
= &revs
->boundary_commits
;
3563 struct object_array_entry
*objects
= array
->objects
;
3566 * If revs->commits is non-NULL at this point, an error occurred in
3567 * get_revision_1(). Ignore the error and continue printing the
3568 * boundary commits anyway. (This is what the code has always
3571 if (revs
->commits
) {
3572 free_commit_list(revs
->commits
);
3573 revs
->commits
= NULL
;
3577 * Put all of the actual boundary commits from revs->boundary_commits
3578 * into revs->commits
3580 for (i
= 0; i
< array
->nr
; i
++) {
3581 c
= (struct commit
*)(objects
[i
].item
);
3584 if (!(c
->object
.flags
& CHILD_SHOWN
))
3586 if (c
->object
.flags
& (SHOWN
| BOUNDARY
))
3588 c
->object
.flags
|= BOUNDARY
;
3589 commit_list_insert(c
, &revs
->commits
);
3593 * If revs->topo_order is set, sort the boundary commits
3594 * in topological order
3596 sort_in_topological_order(&revs
->commits
, revs
->sort_order
);
3599 static struct commit
*get_revision_internal(struct rev_info
*revs
)
3601 struct commit
*c
= NULL
;
3602 struct commit_list
*l
;
3604 if (revs
->boundary
== 2) {
3606 * All of the normal commits have already been returned,
3607 * and we are now returning boundary commits.
3608 * create_boundary_commit_list() has populated
3609 * revs->commits with the remaining commits to return.
3611 c
= pop_commit(&revs
->commits
);
3613 c
->object
.flags
|= SHOWN
;
3618 * If our max_count counter has reached zero, then we are done. We
3619 * don't simply return NULL because we still might need to show
3620 * boundary commits. But we want to avoid calling get_revision_1, which
3621 * might do a considerable amount of work finding the next commit only
3622 * for us to throw it away.
3624 * If it is non-zero, then either we don't have a max_count at all
3625 * (-1), or it is still counting, in which case we decrement.
3627 if (revs
->max_count
) {
3628 c
= get_revision_1(revs
);
3630 while (revs
->skip_count
> 0) {
3632 c
= get_revision_1(revs
);
3638 if (revs
->max_count
> 0)
3643 c
->object
.flags
|= SHOWN
;
3645 if (!revs
->boundary
)
3650 * get_revision_1() runs out the commits, and
3651 * we are done computing the boundaries.
3652 * switch to boundary commits output mode.
3657 * Update revs->commits to contain the list of
3660 create_boundary_commit_list(revs
);
3662 return get_revision_internal(revs
);
3666 * boundary commits are the commits that are parents of the
3667 * ones we got from get_revision_1() but they themselves are
3668 * not returned from get_revision_1(). Before returning
3669 * 'c', we need to mark its parents that they could be boundaries.
3672 for (l
= c
->parents
; l
; l
= l
->next
) {
3674 p
= &(l
->item
->object
);
3675 if (p
->flags
& (CHILD_SHOWN
| SHOWN
))
3677 p
->flags
|= CHILD_SHOWN
;
3678 gc_boundary(&revs
->boundary_commits
);
3679 add_object_array(p
, NULL
, &revs
->boundary_commits
);
3685 struct commit
*get_revision(struct rev_info
*revs
)
3688 struct commit_list
*reversed
;
3690 if (revs
->reverse
) {
3692 while ((c
= get_revision_internal(revs
)))
3693 commit_list_insert(c
, &reversed
);
3694 revs
->commits
= reversed
;
3696 revs
->reverse_output_stage
= 1;
3699 if (revs
->reverse_output_stage
) {
3700 c
= pop_commit(&revs
->commits
);
3701 if (revs
->track_linear
)
3702 revs
->linear
= !!(c
&& c
->object
.flags
& TRACK_LINEAR
);
3706 c
= get_revision_internal(revs
);
3707 if (c
&& revs
->graph
)
3708 graph_update(revs
->graph
, c
);
3710 free_saved_parents(revs
);
3711 if (revs
->previous_parents
) {
3712 free_commit_list(revs
->previous_parents
);
3713 revs
->previous_parents
= NULL
;
3719 char *get_revision_mark(const struct rev_info
*revs
, const struct commit
*commit
)
3721 if (commit
->object
.flags
& BOUNDARY
)
3723 else if (commit
->object
.flags
& UNINTERESTING
)
3725 else if (commit
->object
.flags
& PATCHSAME
)
3727 else if (!revs
|| revs
->left_right
) {
3728 if (commit
->object
.flags
& SYMMETRIC_LEFT
)
3732 } else if (revs
->graph
)
3734 else if (revs
->cherry_mark
)
3739 void put_revision_mark(const struct rev_info
*revs
, const struct commit
*commit
)
3741 char *mark
= get_revision_mark(revs
, commit
);
3744 fputs(mark
, stdout
);