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"
29 volatile show_early_output_fn_t show_early_output
;
31 static const char *term_bad
;
32 static const char *term_good
;
34 implement_shared_commit_slab(revision_sources
, char *);
36 void show_object_with_name(FILE *out
, struct object
*obj
, const char *name
)
40 fprintf(out
, "%s ", oid_to_hex(&obj
->oid
));
41 for (p
= name
; *p
&& *p
!= '\n'; p
++)
46 static void mark_blob_uninteresting(struct blob
*blob
)
50 if (blob
->object
.flags
& UNINTERESTING
)
52 blob
->object
.flags
|= UNINTERESTING
;
55 static void mark_tree_contents_uninteresting(struct repository
*r
,
58 struct tree_desc desc
;
59 struct name_entry entry
;
61 if (parse_tree_gently(tree
, 1) < 0)
64 init_tree_desc(&desc
, tree
->buffer
, tree
->size
);
65 while (tree_entry(&desc
, &entry
)) {
66 switch (object_type(entry
.mode
)) {
68 mark_tree_uninteresting(r
, lookup_tree(r
, entry
.oid
));
71 mark_blob_uninteresting(lookup_blob(r
, entry
.oid
));
74 /* Subproject commit - not in this repository */
80 * We don't care about the tree any more
81 * after it has been marked uninteresting.
83 free_tree_buffer(tree
);
86 void mark_tree_uninteresting(struct repository
*r
, struct tree
*tree
)
94 if (obj
->flags
& UNINTERESTING
)
96 obj
->flags
|= UNINTERESTING
;
97 mark_tree_contents_uninteresting(r
, tree
);
100 struct commit_stack
{
101 struct commit
**items
;
104 #define COMMIT_STACK_INIT { NULL, 0, 0 }
106 static void commit_stack_push(struct commit_stack
*stack
, struct commit
*commit
)
108 ALLOC_GROW(stack
->items
, stack
->nr
+ 1, stack
->alloc
);
109 stack
->items
[stack
->nr
++] = commit
;
112 static struct commit
*commit_stack_pop(struct commit_stack
*stack
)
114 return stack
->nr
? stack
->items
[--stack
->nr
] : NULL
;
117 static void commit_stack_clear(struct commit_stack
*stack
)
119 FREE_AND_NULL(stack
->items
);
120 stack
->nr
= stack
->alloc
= 0;
123 static void mark_one_parent_uninteresting(struct commit
*commit
,
124 struct commit_stack
*pending
)
126 struct commit_list
*l
;
128 if (commit
->object
.flags
& UNINTERESTING
)
130 commit
->object
.flags
|= UNINTERESTING
;
133 * Normally we haven't parsed the parent
134 * yet, so we won't have a parent of a parent
135 * here. However, it may turn out that we've
136 * reached this commit some other way (where it
137 * wasn't uninteresting), in which case we need
138 * to mark its parents recursively too..
140 for (l
= commit
->parents
; l
; l
= l
->next
)
141 commit_stack_push(pending
, l
->item
);
144 void mark_parents_uninteresting(struct commit
*commit
)
146 struct commit_stack pending
= COMMIT_STACK_INIT
;
147 struct commit_list
*l
;
149 for (l
= commit
->parents
; l
; l
= l
->next
)
150 mark_one_parent_uninteresting(l
->item
, &pending
);
152 while (pending
.nr
> 0)
153 mark_one_parent_uninteresting(commit_stack_pop(&pending
),
156 commit_stack_clear(&pending
);
159 static void add_pending_object_with_path(struct rev_info
*revs
,
161 const char *name
, unsigned mode
,
166 if (revs
->no_walk
&& (obj
->flags
& UNINTERESTING
))
168 if (revs
->reflog_info
&& obj
->type
== OBJ_COMMIT
) {
169 struct strbuf buf
= STRBUF_INIT
;
170 int len
= interpret_branch_name(name
, 0, &buf
, 0);
172 if (0 < len
&& name
[len
] && buf
.len
)
173 strbuf_addstr(&buf
, name
+ len
);
174 add_reflog_for_walk(revs
->reflog_info
,
175 (struct commit
*)obj
,
176 buf
.buf
[0] ? buf
.buf
: name
);
177 strbuf_release(&buf
);
178 return; /* do not add the commit itself */
180 add_object_array_with_path(obj
, name
, &revs
->pending
, mode
, path
);
183 static void add_pending_object_with_mode(struct rev_info
*revs
,
185 const char *name
, unsigned mode
)
187 add_pending_object_with_path(revs
, obj
, name
, mode
, NULL
);
190 void add_pending_object(struct rev_info
*revs
,
191 struct object
*obj
, const char *name
)
193 add_pending_object_with_mode(revs
, obj
, name
, S_IFINVALID
);
196 void add_head_to_pending(struct rev_info
*revs
)
198 struct object_id oid
;
200 if (get_oid("HEAD", &oid
))
202 obj
= parse_object(revs
->repo
, &oid
);
205 add_pending_object(revs
, obj
, "HEAD");
208 static struct object
*get_reference(struct rev_info
*revs
, const char *name
,
209 const struct object_id
*oid
,
212 struct object
*object
;
214 object
= parse_object(revs
->repo
, oid
);
216 if (revs
->ignore_missing
)
218 if (revs
->exclude_promisor_objects
&& is_promisor_object(oid
))
220 die("bad object %s", name
);
222 object
->flags
|= flags
;
226 void add_pending_oid(struct rev_info
*revs
, const char *name
,
227 const struct object_id
*oid
, unsigned int flags
)
229 struct object
*object
= get_reference(revs
, name
, oid
, flags
);
230 add_pending_object(revs
, object
, name
);
233 static struct commit
*handle_commit(struct rev_info
*revs
,
234 struct object_array_entry
*entry
)
236 struct object
*object
= entry
->item
;
237 const char *name
= entry
->name
;
238 const char *path
= entry
->path
;
239 unsigned int mode
= entry
->mode
;
240 unsigned long flags
= object
->flags
;
243 * Tag object? Look what it points to..
245 while (object
->type
== OBJ_TAG
) {
246 struct tag
*tag
= (struct tag
*) object
;
247 if (revs
->tag_objects
&& !(flags
& UNINTERESTING
))
248 add_pending_object(revs
, object
, tag
->tag
);
251 object
= parse_object(revs
->repo
, &tag
->tagged
->oid
);
253 if (revs
->ignore_missing_links
|| (flags
& UNINTERESTING
))
255 if (revs
->exclude_promisor_objects
&&
256 is_promisor_object(&tag
->tagged
->oid
))
258 die("bad object %s", oid_to_hex(&tag
->tagged
->oid
));
260 object
->flags
|= flags
;
262 * We'll handle the tagged object by looping or dropping
263 * through to the non-tag handlers below. Do not
264 * propagate path data from the tag's pending entry.
271 * Commit object? Just return it, we'll do all the complex
274 if (object
->type
== OBJ_COMMIT
) {
275 struct commit
*commit
= (struct commit
*)object
;
277 if (parse_commit(commit
) < 0)
278 die("unable to parse commit %s", name
);
279 if (flags
& UNINTERESTING
) {
280 mark_parents_uninteresting(commit
);
284 char **slot
= revision_sources_at(revs
->sources
, commit
);
287 *slot
= xstrdup(name
);
293 * Tree object? Either mark it uninteresting, or add it
294 * to the list of objects to look at later..
296 if (object
->type
== OBJ_TREE
) {
297 struct tree
*tree
= (struct tree
*)object
;
298 if (!revs
->tree_objects
)
300 if (flags
& UNINTERESTING
) {
301 mark_tree_contents_uninteresting(revs
->repo
, tree
);
304 add_pending_object_with_path(revs
, object
, name
, mode
, path
);
309 * Blob object? You know the drill by now..
311 if (object
->type
== OBJ_BLOB
) {
312 if (!revs
->blob_objects
)
314 if (flags
& UNINTERESTING
)
316 add_pending_object_with_path(revs
, object
, name
, mode
, path
);
319 die("%s is unknown object", name
);
322 static int everybody_uninteresting(struct commit_list
*orig
,
323 struct commit
**interesting_cache
)
325 struct commit_list
*list
= orig
;
327 if (*interesting_cache
) {
328 struct commit
*commit
= *interesting_cache
;
329 if (!(commit
->object
.flags
& UNINTERESTING
))
334 struct commit
*commit
= list
->item
;
336 if (commit
->object
.flags
& UNINTERESTING
)
339 *interesting_cache
= commit
;
346 * A definition of "relevant" commit that we can use to simplify limited graphs
347 * by eliminating side branches.
349 * A "relevant" commit is one that is !UNINTERESTING (ie we are including it
350 * in our list), or that is a specified BOTTOM commit. Then after computing
351 * a limited list, during processing we can generally ignore boundary merges
352 * coming from outside the graph, (ie from irrelevant parents), and treat
353 * those merges as if they were single-parent. TREESAME is defined to consider
354 * only relevant parents, if any. If we are TREESAME to our on-graph parents,
355 * we don't care if we were !TREESAME to non-graph parents.
357 * Treating bottom commits as relevant ensures that a limited graph's
358 * connection to the actual bottom commit is not viewed as a side branch, but
359 * treated as part of the graph. For example:
361 * ....Z...A---X---o---o---B
365 * When computing "A..B", the A-X connection is at least as important as
366 * Y-X, despite A being flagged UNINTERESTING.
368 * And when computing --ancestry-path "A..B", the A-X connection is more
369 * important than Y-X, despite both A and Y being flagged UNINTERESTING.
371 static inline int relevant_commit(struct commit
*commit
)
373 return (commit
->object
.flags
& (UNINTERESTING
| BOTTOM
)) != UNINTERESTING
;
377 * Return a single relevant commit from a parent list. If we are a TREESAME
378 * commit, and this selects one of our parents, then we can safely simplify to
381 static struct commit
*one_relevant_parent(const struct rev_info
*revs
,
382 struct commit_list
*orig
)
384 struct commit_list
*list
= orig
;
385 struct commit
*relevant
= NULL
;
391 * For 1-parent commits, or if first-parent-only, then return that
392 * first parent (even if not "relevant" by the above definition).
393 * TREESAME will have been set purely on that parent.
395 if (revs
->first_parent_only
|| !orig
->next
)
399 * For multi-parent commits, identify a sole relevant parent, if any.
400 * If we have only one relevant parent, then TREESAME will be set purely
401 * with regard to that parent, and we can simplify accordingly.
403 * If we have more than one relevant parent, or no relevant parents
404 * (and multiple irrelevant ones), then we can't select a parent here
408 struct commit
*commit
= list
->item
;
410 if (relevant_commit(commit
)) {
420 * The goal is to get REV_TREE_NEW as the result only if the
421 * diff consists of all '+' (and no other changes), REV_TREE_OLD
422 * if the whole diff is removal of old data, and otherwise
423 * REV_TREE_DIFFERENT (of course if the trees are the same we
424 * want REV_TREE_SAME).
426 * The only time we care about the distinction is when
427 * remove_empty_trees is in effect, in which case we care only about
428 * whether the whole change is REV_TREE_NEW, or if there's another type
429 * of change. Which means we can stop the diff early in either of these
432 * 1. We're not using remove_empty_trees at all.
434 * 2. We saw anything except REV_TREE_NEW.
436 static int tree_difference
= REV_TREE_SAME
;
438 static void file_add_remove(struct diff_options
*options
,
439 int addremove
, unsigned mode
,
440 const struct object_id
*oid
,
442 const char *fullpath
, unsigned dirty_submodule
)
444 int diff
= addremove
== '+' ? REV_TREE_NEW
: REV_TREE_OLD
;
445 struct rev_info
*revs
= options
->change_fn_data
;
447 tree_difference
|= diff
;
448 if (!revs
->remove_empty_trees
|| tree_difference
!= REV_TREE_NEW
)
449 options
->flags
.has_changes
= 1;
452 static void file_change(struct diff_options
*options
,
453 unsigned old_mode
, unsigned new_mode
,
454 const struct object_id
*old_oid
,
455 const struct object_id
*new_oid
,
456 int old_oid_valid
, int new_oid_valid
,
457 const char *fullpath
,
458 unsigned old_dirty_submodule
, unsigned new_dirty_submodule
)
460 tree_difference
= REV_TREE_DIFFERENT
;
461 options
->flags
.has_changes
= 1;
464 static int rev_compare_tree(struct rev_info
*revs
,
465 struct commit
*parent
, struct commit
*commit
)
467 struct tree
*t1
= get_commit_tree(parent
);
468 struct tree
*t2
= get_commit_tree(commit
);
475 if (revs
->simplify_by_decoration
) {
477 * If we are simplifying by decoration, then the commit
478 * is worth showing if it has a tag pointing at it.
480 if (get_name_decoration(&commit
->object
))
481 return REV_TREE_DIFFERENT
;
483 * A commit that is not pointed by a tag is uninteresting
484 * if we are not limited by path. This means that you will
485 * see the usual "commits that touch the paths" plus any
486 * tagged commit by specifying both --simplify-by-decoration
489 if (!revs
->prune_data
.nr
)
490 return REV_TREE_SAME
;
493 tree_difference
= REV_TREE_SAME
;
494 revs
->pruning
.flags
.has_changes
= 0;
495 if (diff_tree_oid(&t1
->object
.oid
, &t2
->object
.oid
, "",
497 return REV_TREE_DIFFERENT
;
498 return tree_difference
;
501 static int rev_same_tree_as_empty(struct rev_info
*revs
, struct commit
*commit
)
504 struct tree
*t1
= get_commit_tree(commit
);
509 tree_difference
= REV_TREE_SAME
;
510 revs
->pruning
.flags
.has_changes
= 0;
511 retval
= diff_tree_oid(NULL
, &t1
->object
.oid
, "", &revs
->pruning
);
513 return retval
>= 0 && (tree_difference
== REV_TREE_SAME
);
516 struct treesame_state
{
517 unsigned int nparents
;
518 unsigned char treesame
[FLEX_ARRAY
];
521 static struct treesame_state
*initialise_treesame(struct rev_info
*revs
, struct commit
*commit
)
523 unsigned n
= commit_list_count(commit
->parents
);
524 struct treesame_state
*st
= xcalloc(1, st_add(sizeof(*st
), n
));
526 add_decoration(&revs
->treesame
, &commit
->object
, st
);
531 * Must be called immediately after removing the nth_parent from a commit's
532 * parent list, if we are maintaining the per-parent treesame[] decoration.
533 * This does not recalculate the master TREESAME flag - update_treesame()
534 * should be called to update it after a sequence of treesame[] modifications
535 * that may have affected it.
537 static int compact_treesame(struct rev_info
*revs
, struct commit
*commit
, unsigned nth_parent
)
539 struct treesame_state
*st
;
542 if (!commit
->parents
) {
544 * Have just removed the only parent from a non-merge.
545 * Different handling, as we lack decoration.
548 die("compact_treesame %u", nth_parent
);
549 old_same
= !!(commit
->object
.flags
& TREESAME
);
550 if (rev_same_tree_as_empty(revs
, commit
))
551 commit
->object
.flags
|= TREESAME
;
553 commit
->object
.flags
&= ~TREESAME
;
557 st
= lookup_decoration(&revs
->treesame
, &commit
->object
);
558 if (!st
|| nth_parent
>= st
->nparents
)
559 die("compact_treesame %u", nth_parent
);
561 old_same
= st
->treesame
[nth_parent
];
562 memmove(st
->treesame
+ nth_parent
,
563 st
->treesame
+ nth_parent
+ 1,
564 st
->nparents
- nth_parent
- 1);
567 * If we've just become a non-merge commit, update TREESAME
568 * immediately, and remove the no-longer-needed decoration.
569 * If still a merge, defer update until update_treesame().
571 if (--st
->nparents
== 1) {
572 if (commit
->parents
->next
)
573 die("compact_treesame parents mismatch");
574 if (st
->treesame
[0] && revs
->dense
)
575 commit
->object
.flags
|= TREESAME
;
577 commit
->object
.flags
&= ~TREESAME
;
578 free(add_decoration(&revs
->treesame
, &commit
->object
, NULL
));
584 static unsigned update_treesame(struct rev_info
*revs
, struct commit
*commit
)
586 if (commit
->parents
&& commit
->parents
->next
) {
588 struct treesame_state
*st
;
589 struct commit_list
*p
;
590 unsigned relevant_parents
;
591 unsigned relevant_change
, irrelevant_change
;
593 st
= lookup_decoration(&revs
->treesame
, &commit
->object
);
595 die("update_treesame %s", oid_to_hex(&commit
->object
.oid
));
596 relevant_parents
= 0;
597 relevant_change
= irrelevant_change
= 0;
598 for (p
= commit
->parents
, n
= 0; p
; n
++, p
= p
->next
) {
599 if (relevant_commit(p
->item
)) {
600 relevant_change
|= !st
->treesame
[n
];
603 irrelevant_change
|= !st
->treesame
[n
];
605 if (relevant_parents
? relevant_change
: irrelevant_change
)
606 commit
->object
.flags
&= ~TREESAME
;
608 commit
->object
.flags
|= TREESAME
;
611 return commit
->object
.flags
& TREESAME
;
614 static inline int limiting_can_increase_treesame(const struct rev_info
*revs
)
617 * TREESAME is irrelevant unless prune && dense;
618 * if simplify_history is set, we can't have a mixture of TREESAME and
619 * !TREESAME INTERESTING parents (and we don't have treesame[]
620 * decoration anyway);
621 * if first_parent_only is set, then the TREESAME flag is locked
622 * against the first parent (and again we lack treesame[] decoration).
624 return revs
->prune
&& revs
->dense
&&
625 !revs
->simplify_history
&&
626 !revs
->first_parent_only
;
629 static void try_to_simplify_commit(struct rev_info
*revs
, struct commit
*commit
)
631 struct commit_list
**pp
, *parent
;
632 struct treesame_state
*ts
= NULL
;
633 int relevant_change
= 0, irrelevant_change
= 0;
634 int relevant_parents
, nth_parent
;
637 * If we don't do pruning, everything is interesting
642 if (!get_commit_tree(commit
))
645 if (!commit
->parents
) {
646 if (rev_same_tree_as_empty(revs
, commit
))
647 commit
->object
.flags
|= TREESAME
;
652 * Normal non-merge commit? If we don't want to make the
653 * history dense, we consider it always to be a change..
655 if (!revs
->dense
&& !commit
->parents
->next
)
658 for (pp
= &commit
->parents
, nth_parent
= 0, relevant_parents
= 0;
659 (parent
= *pp
) != NULL
;
660 pp
= &parent
->next
, nth_parent
++) {
661 struct commit
*p
= parent
->item
;
662 if (relevant_commit(p
))
665 if (nth_parent
== 1) {
667 * This our second loop iteration - so we now know
668 * we're dealing with a merge.
670 * Do not compare with later parents when we care only about
671 * the first parent chain, in order to avoid derailing the
672 * traversal to follow a side branch that brought everything
673 * in the path we are limited to by the pathspec.
675 if (revs
->first_parent_only
)
678 * If this will remain a potentially-simplifiable
679 * merge, remember per-parent treesame if needed.
680 * Initialise the array with the comparison from our
683 if (revs
->treesame
.name
&&
684 !revs
->simplify_history
&&
685 !(commit
->object
.flags
& UNINTERESTING
)) {
686 ts
= initialise_treesame(revs
, commit
);
687 if (!(irrelevant_change
|| relevant_change
))
691 if (parse_commit(p
) < 0)
692 die("cannot simplify commit %s (because of %s)",
693 oid_to_hex(&commit
->object
.oid
),
694 oid_to_hex(&p
->object
.oid
));
695 switch (rev_compare_tree(revs
, p
, commit
)) {
697 if (!revs
->simplify_history
|| !relevant_commit(p
)) {
698 /* Even if a merge with an uninteresting
699 * side branch brought the entire change
700 * we are interested in, we do not want
701 * to lose the other branches of this
702 * merge, so we just keep going.
705 ts
->treesame
[nth_parent
] = 1;
709 commit
->parents
= parent
;
710 commit
->object
.flags
|= TREESAME
;
714 if (revs
->remove_empty_trees
&&
715 rev_same_tree_as_empty(revs
, p
)) {
716 /* We are adding all the specified
717 * paths from this parent, so the
718 * history beyond this parent is not
719 * interesting. Remove its parents
720 * (they are grandparents for us).
721 * IOW, we pretend this parent is a
724 if (parse_commit(p
) < 0)
725 die("cannot simplify commit %s (invalid %s)",
726 oid_to_hex(&commit
->object
.oid
),
727 oid_to_hex(&p
->object
.oid
));
732 case REV_TREE_DIFFERENT
:
733 if (relevant_commit(p
))
736 irrelevant_change
= 1;
739 die("bad tree compare for commit %s", oid_to_hex(&commit
->object
.oid
));
743 * TREESAME is straightforward for single-parent commits. For merge
744 * commits, it is most useful to define it so that "irrelevant"
745 * parents cannot make us !TREESAME - if we have any relevant
746 * parents, then we only consider TREESAMEness with respect to them,
747 * allowing irrelevant merges from uninteresting branches to be
748 * simplified away. Only if we have only irrelevant parents do we
749 * base TREESAME on them. Note that this logic is replicated in
750 * update_treesame, which should be kept in sync.
752 if (relevant_parents
? !relevant_change
: !irrelevant_change
)
753 commit
->object
.flags
|= TREESAME
;
756 static void commit_list_insert_by_date_cached(struct commit
*p
, struct commit_list
**head
,
757 struct commit_list
*cached_base
, struct commit_list
**cache
)
759 struct commit_list
*new_entry
;
761 if (cached_base
&& p
->date
< cached_base
->item
->date
)
762 new_entry
= commit_list_insert_by_date(p
, &cached_base
->next
);
764 new_entry
= commit_list_insert_by_date(p
, head
);
766 if (cache
&& (!*cache
|| p
->date
< (*cache
)->item
->date
))
770 static int add_parents_to_list(struct rev_info
*revs
, struct commit
*commit
,
771 struct commit_list
**list
, struct commit_list
**cache_ptr
)
773 struct commit_list
*parent
= commit
->parents
;
775 struct commit_list
*cached_base
= cache_ptr
? *cache_ptr
: NULL
;
777 if (commit
->object
.flags
& ADDED
)
779 commit
->object
.flags
|= ADDED
;
781 if (revs
->include_check
&&
782 !revs
->include_check(commit
, revs
->include_check_data
))
786 * If the commit is uninteresting, don't try to
787 * prune parents - we want the maximal uninteresting
790 * Normally we haven't parsed the parent
791 * yet, so we won't have a parent of a parent
792 * here. However, it may turn out that we've
793 * reached this commit some other way (where it
794 * wasn't uninteresting), in which case we need
795 * to mark its parents recursively too..
797 if (commit
->object
.flags
& UNINTERESTING
) {
799 struct commit
*p
= parent
->item
;
800 parent
= parent
->next
;
802 p
->object
.flags
|= UNINTERESTING
;
803 if (parse_commit_gently(p
, 1) < 0)
806 mark_parents_uninteresting(p
);
807 if (p
->object
.flags
& SEEN
)
809 p
->object
.flags
|= SEEN
;
810 commit_list_insert_by_date_cached(p
, list
, cached_base
, cache_ptr
);
816 * Ok, the commit wasn't uninteresting. Try to
817 * simplify the commit history and find the parent
818 * that has no differences in the path set if one exists.
820 try_to_simplify_commit(revs
, commit
);
825 left_flag
= (commit
->object
.flags
& SYMMETRIC_LEFT
);
827 for (parent
= commit
->parents
; parent
; parent
= parent
->next
) {
828 struct commit
*p
= parent
->item
;
829 int gently
= revs
->ignore_missing_links
||
830 revs
->exclude_promisor_objects
;
831 if (parse_commit_gently(p
, gently
) < 0) {
832 if (revs
->exclude_promisor_objects
&&
833 is_promisor_object(&p
->object
.oid
)) {
834 if (revs
->first_parent_only
)
841 char **slot
= revision_sources_at(revs
->sources
, p
);
844 *slot
= *revision_sources_at(revs
->sources
, commit
);
846 p
->object
.flags
|= left_flag
;
847 if (!(p
->object
.flags
& SEEN
)) {
848 p
->object
.flags
|= SEEN
;
849 commit_list_insert_by_date_cached(p
, list
, cached_base
, cache_ptr
);
851 if (revs
->first_parent_only
)
857 static void cherry_pick_list(struct commit_list
*list
, struct rev_info
*revs
)
859 struct commit_list
*p
;
860 int left_count
= 0, right_count
= 0;
862 struct patch_ids ids
;
863 unsigned cherry_flag
;
865 /* First count the commits on the left and on the right */
866 for (p
= list
; p
; p
= p
->next
) {
867 struct commit
*commit
= p
->item
;
868 unsigned flags
= commit
->object
.flags
;
869 if (flags
& BOUNDARY
)
871 else if (flags
& SYMMETRIC_LEFT
)
877 if (!left_count
|| !right_count
)
880 left_first
= left_count
< right_count
;
881 init_patch_ids(revs
->repo
, &ids
);
882 ids
.diffopts
.pathspec
= revs
->diffopt
.pathspec
;
884 /* Compute patch-ids for one side */
885 for (p
= list
; p
; p
= p
->next
) {
886 struct commit
*commit
= p
->item
;
887 unsigned flags
= commit
->object
.flags
;
889 if (flags
& BOUNDARY
)
892 * If we have fewer left, left_first is set and we omit
893 * commits on the right branch in this loop. If we have
894 * fewer right, we skip the left ones.
896 if (left_first
!= !!(flags
& SYMMETRIC_LEFT
))
898 add_commit_patch_id(commit
, &ids
);
901 /* either cherry_mark or cherry_pick are true */
902 cherry_flag
= revs
->cherry_mark
? PATCHSAME
: SHOWN
;
904 /* Check the other side */
905 for (p
= list
; p
; p
= p
->next
) {
906 struct commit
*commit
= p
->item
;
908 unsigned flags
= commit
->object
.flags
;
910 if (flags
& BOUNDARY
)
913 * If we have fewer left, left_first is set and we omit
914 * commits on the left branch in this loop.
916 if (left_first
== !!(flags
& SYMMETRIC_LEFT
))
920 * Have we seen the same patch id?
922 id
= has_commit_patch_id(commit
, &ids
);
926 commit
->object
.flags
|= cherry_flag
;
927 id
->commit
->object
.flags
|= cherry_flag
;
930 free_patch_ids(&ids
);
933 /* How many extra uninteresting commits we want to see.. */
936 static int still_interesting(struct commit_list
*src
, timestamp_t date
, int slop
,
937 struct commit
**interesting_cache
)
940 * No source list at all? We're definitely done..
946 * Does the destination list contain entries with a date
947 * before the source list? Definitely _not_ done.
949 if (date
<= src
->item
->date
)
953 * Does the source list still have interesting commits in
954 * it? Definitely not done..
956 if (!everybody_uninteresting(src
, interesting_cache
))
959 /* Ok, we're closing in.. */
964 * "rev-list --ancestry-path A..B" computes commits that are ancestors
965 * of B but not ancestors of A but further limits the result to those
966 * that are descendants of A. This takes the list of bottom commits and
967 * the result of "A..B" without --ancestry-path, and limits the latter
968 * further to the ones that can reach one of the commits in "bottom".
970 static void limit_to_ancestry(struct commit_list
*bottom
, struct commit_list
*list
)
972 struct commit_list
*p
;
973 struct commit_list
*rlist
= NULL
;
977 * Reverse the list so that it will be likely that we would
978 * process parents before children.
980 for (p
= list
; p
; p
= p
->next
)
981 commit_list_insert(p
->item
, &rlist
);
983 for (p
= bottom
; p
; p
= p
->next
)
984 p
->item
->object
.flags
|= TMP_MARK
;
987 * Mark the ones that can reach bottom commits in "list",
988 * in a bottom-up fashion.
992 for (p
= rlist
; p
; p
= p
->next
) {
993 struct commit
*c
= p
->item
;
994 struct commit_list
*parents
;
995 if (c
->object
.flags
& (TMP_MARK
| UNINTERESTING
))
997 for (parents
= c
->parents
;
999 parents
= parents
->next
) {
1000 if (!(parents
->item
->object
.flags
& TMP_MARK
))
1002 c
->object
.flags
|= TMP_MARK
;
1007 } while (made_progress
);
1010 * NEEDSWORK: decide if we want to remove parents that are
1011 * not marked with TMP_MARK from commit->parents for commits
1012 * in the resulting list. We may not want to do that, though.
1016 * The ones that are not marked with TMP_MARK are uninteresting
1018 for (p
= list
; p
; p
= p
->next
) {
1019 struct commit
*c
= p
->item
;
1020 if (c
->object
.flags
& TMP_MARK
)
1022 c
->object
.flags
|= UNINTERESTING
;
1025 /* We are done with the TMP_MARK */
1026 for (p
= list
; p
; p
= p
->next
)
1027 p
->item
->object
.flags
&= ~TMP_MARK
;
1028 for (p
= bottom
; p
; p
= p
->next
)
1029 p
->item
->object
.flags
&= ~TMP_MARK
;
1030 free_commit_list(rlist
);
1034 * Before walking the history, keep the set of "negative" refs the
1035 * caller has asked to exclude.
1037 * This is used to compute "rev-list --ancestry-path A..B", as we need
1038 * to filter the result of "A..B" further to the ones that can actually
1041 static struct commit_list
*collect_bottom_commits(struct commit_list
*list
)
1043 struct commit_list
*elem
, *bottom
= NULL
;
1044 for (elem
= list
; elem
; elem
= elem
->next
)
1045 if (elem
->item
->object
.flags
& BOTTOM
)
1046 commit_list_insert(elem
->item
, &bottom
);
1050 /* Assumes either left_only or right_only is set */
1051 static void limit_left_right(struct commit_list
*list
, struct rev_info
*revs
)
1053 struct commit_list
*p
;
1055 for (p
= list
; p
; p
= p
->next
) {
1056 struct commit
*commit
= p
->item
;
1058 if (revs
->right_only
) {
1059 if (commit
->object
.flags
& SYMMETRIC_LEFT
)
1060 commit
->object
.flags
|= SHOWN
;
1061 } else /* revs->left_only is set */
1062 if (!(commit
->object
.flags
& SYMMETRIC_LEFT
))
1063 commit
->object
.flags
|= SHOWN
;
1067 static int limit_list(struct rev_info
*revs
)
1070 timestamp_t date
= TIME_MAX
;
1071 struct commit_list
*list
= revs
->commits
;
1072 struct commit_list
*newlist
= NULL
;
1073 struct commit_list
**p
= &newlist
;
1074 struct commit_list
*bottom
= NULL
;
1075 struct commit
*interesting_cache
= NULL
;
1077 if (revs
->ancestry_path
) {
1078 bottom
= collect_bottom_commits(list
);
1080 die("--ancestry-path given but there are no bottom commits");
1084 struct commit
*commit
= pop_commit(&list
);
1085 struct object
*obj
= &commit
->object
;
1086 show_early_output_fn_t show
;
1088 if (commit
== interesting_cache
)
1089 interesting_cache
= NULL
;
1091 if (revs
->max_age
!= -1 && (commit
->date
< revs
->max_age
))
1092 obj
->flags
|= UNINTERESTING
;
1093 if (add_parents_to_list(revs
, commit
, &list
, NULL
) < 0)
1095 if (obj
->flags
& UNINTERESTING
) {
1096 mark_parents_uninteresting(commit
);
1097 slop
= still_interesting(list
, date
, slop
, &interesting_cache
);
1102 if (revs
->min_age
!= -1 && (commit
->date
> revs
->min_age
))
1104 date
= commit
->date
;
1105 p
= &commit_list_insert(commit
, p
)->next
;
1107 show
= show_early_output
;
1111 show(revs
, newlist
);
1112 show_early_output
= NULL
;
1114 if (revs
->cherry_pick
|| revs
->cherry_mark
)
1115 cherry_pick_list(newlist
, revs
);
1117 if (revs
->left_only
|| revs
->right_only
)
1118 limit_left_right(newlist
, revs
);
1121 limit_to_ancestry(bottom
, newlist
);
1122 free_commit_list(bottom
);
1126 * Check if any commits have become TREESAME by some of their parents
1127 * becoming UNINTERESTING.
1129 if (limiting_can_increase_treesame(revs
))
1130 for (list
= newlist
; list
; list
= list
->next
) {
1131 struct commit
*c
= list
->item
;
1132 if (c
->object
.flags
& (UNINTERESTING
| TREESAME
))
1134 update_treesame(revs
, c
);
1137 revs
->commits
= newlist
;
1142 * Add an entry to refs->cmdline with the specified information.
1145 static void add_rev_cmdline(struct rev_info
*revs
,
1146 struct object
*item
,
1151 struct rev_cmdline_info
*info
= &revs
->cmdline
;
1152 unsigned int nr
= info
->nr
;
1154 ALLOC_GROW(info
->rev
, nr
+ 1, info
->alloc
);
1155 info
->rev
[nr
].item
= item
;
1156 info
->rev
[nr
].name
= xstrdup(name
);
1157 info
->rev
[nr
].whence
= whence
;
1158 info
->rev
[nr
].flags
= flags
;
1162 static void add_rev_cmdline_list(struct rev_info
*revs
,
1163 struct commit_list
*commit_list
,
1167 while (commit_list
) {
1168 struct object
*object
= &commit_list
->item
->object
;
1169 add_rev_cmdline(revs
, object
, oid_to_hex(&object
->oid
),
1171 commit_list
= commit_list
->next
;
1175 struct all_refs_cb
{
1177 int warned_bad_reflog
;
1178 struct rev_info
*all_revs
;
1179 const char *name_for_errormsg
;
1180 struct worktree
*wt
;
1183 int ref_excluded(struct string_list
*ref_excludes
, const char *path
)
1185 struct string_list_item
*item
;
1189 for_each_string_list_item(item
, ref_excludes
) {
1190 if (!wildmatch(item
->string
, path
, 0))
1196 static int handle_one_ref(const char *path
, const struct object_id
*oid
,
1197 int flag
, void *cb_data
)
1199 struct all_refs_cb
*cb
= cb_data
;
1200 struct object
*object
;
1202 if (ref_excluded(cb
->all_revs
->ref_excludes
, path
))
1205 object
= get_reference(cb
->all_revs
, path
, oid
, cb
->all_flags
);
1206 add_rev_cmdline(cb
->all_revs
, object
, path
, REV_CMD_REF
, cb
->all_flags
);
1207 add_pending_oid(cb
->all_revs
, path
, oid
, cb
->all_flags
);
1211 static void init_all_refs_cb(struct all_refs_cb
*cb
, struct rev_info
*revs
,
1214 cb
->all_revs
= revs
;
1215 cb
->all_flags
= flags
;
1216 revs
->rev_input_given
= 1;
1220 void clear_ref_exclusion(struct string_list
**ref_excludes_p
)
1222 if (*ref_excludes_p
) {
1223 string_list_clear(*ref_excludes_p
, 0);
1224 free(*ref_excludes_p
);
1226 *ref_excludes_p
= NULL
;
1229 void add_ref_exclusion(struct string_list
**ref_excludes_p
, const char *exclude
)
1231 if (!*ref_excludes_p
) {
1232 *ref_excludes_p
= xcalloc(1, sizeof(**ref_excludes_p
));
1233 (*ref_excludes_p
)->strdup_strings
= 1;
1235 string_list_append(*ref_excludes_p
, exclude
);
1238 static void handle_refs(struct ref_store
*refs
,
1239 struct rev_info
*revs
, unsigned flags
,
1240 int (*for_each
)(struct ref_store
*, each_ref_fn
, void *))
1242 struct all_refs_cb cb
;
1245 /* this could happen with uninitialized submodules */
1249 init_all_refs_cb(&cb
, revs
, flags
);
1250 for_each(refs
, handle_one_ref
, &cb
);
1253 static void handle_one_reflog_commit(struct object_id
*oid
, void *cb_data
)
1255 struct all_refs_cb
*cb
= cb_data
;
1256 if (!is_null_oid(oid
)) {
1257 struct object
*o
= parse_object(cb
->all_revs
->repo
, oid
);
1259 o
->flags
|= cb
->all_flags
;
1260 /* ??? CMDLINEFLAGS ??? */
1261 add_pending_object(cb
->all_revs
, o
, "");
1263 else if (!cb
->warned_bad_reflog
) {
1264 warning("reflog of '%s' references pruned commits",
1265 cb
->name_for_errormsg
);
1266 cb
->warned_bad_reflog
= 1;
1271 static int handle_one_reflog_ent(struct object_id
*ooid
, struct object_id
*noid
,
1272 const char *email
, timestamp_t timestamp
, int tz
,
1273 const char *message
, void *cb_data
)
1275 handle_one_reflog_commit(ooid
, cb_data
);
1276 handle_one_reflog_commit(noid
, cb_data
);
1280 static int handle_one_reflog(const char *refname_in_wt
,
1281 const struct object_id
*oid
,
1282 int flag
, void *cb_data
)
1284 struct all_refs_cb
*cb
= cb_data
;
1285 struct strbuf refname
= STRBUF_INIT
;
1287 cb
->warned_bad_reflog
= 0;
1288 strbuf_worktree_ref(cb
->wt
, &refname
, refname_in_wt
);
1289 cb
->name_for_errormsg
= refname
.buf
;
1290 refs_for_each_reflog_ent(get_main_ref_store(the_repository
),
1292 handle_one_reflog_ent
, cb_data
);
1293 strbuf_release(&refname
);
1297 static void add_other_reflogs_to_pending(struct all_refs_cb
*cb
)
1299 struct worktree
**worktrees
, **p
;
1301 worktrees
= get_worktrees(0);
1302 for (p
= worktrees
; *p
; p
++) {
1303 struct worktree
*wt
= *p
;
1309 refs_for_each_reflog(get_worktree_ref_store(wt
),
1313 free_worktrees(worktrees
);
1316 void add_reflogs_to_pending(struct rev_info
*revs
, unsigned flags
)
1318 struct all_refs_cb cb
;
1321 cb
.all_flags
= flags
;
1323 for_each_reflog(handle_one_reflog
, &cb
);
1325 if (!revs
->single_worktree
)
1326 add_other_reflogs_to_pending(&cb
);
1329 static void add_cache_tree(struct cache_tree
*it
, struct rev_info
*revs
,
1330 struct strbuf
*path
, unsigned int flags
)
1332 size_t baselen
= path
->len
;
1335 if (it
->entry_count
>= 0) {
1336 struct tree
*tree
= lookup_tree(revs
->repo
, &it
->oid
);
1337 tree
->object
.flags
|= flags
;
1338 add_pending_object_with_path(revs
, &tree
->object
, "",
1342 for (i
= 0; i
< it
->subtree_nr
; i
++) {
1343 struct cache_tree_sub
*sub
= it
->down
[i
];
1344 strbuf_addf(path
, "%s%s", baselen
? "/" : "", sub
->name
);
1345 add_cache_tree(sub
->cache_tree
, revs
, path
, flags
);
1346 strbuf_setlen(path
, baselen
);
1351 static void do_add_index_objects_to_pending(struct rev_info
*revs
,
1352 struct index_state
*istate
,
1357 for (i
= 0; i
< istate
->cache_nr
; i
++) {
1358 struct cache_entry
*ce
= istate
->cache
[i
];
1361 if (S_ISGITLINK(ce
->ce_mode
))
1364 blob
= lookup_blob(revs
->repo
, &ce
->oid
);
1366 die("unable to add index blob to traversal");
1367 blob
->object
.flags
|= flags
;
1368 add_pending_object_with_path(revs
, &blob
->object
, "",
1369 ce
->ce_mode
, ce
->name
);
1372 if (istate
->cache_tree
) {
1373 struct strbuf path
= STRBUF_INIT
;
1374 add_cache_tree(istate
->cache_tree
, revs
, &path
, flags
);
1375 strbuf_release(&path
);
1379 void add_index_objects_to_pending(struct rev_info
*revs
, unsigned int flags
)
1381 struct worktree
**worktrees
, **p
;
1383 read_index(revs
->repo
->index
);
1384 do_add_index_objects_to_pending(revs
, revs
->repo
->index
, flags
);
1386 if (revs
->single_worktree
)
1389 worktrees
= get_worktrees(0);
1390 for (p
= worktrees
; *p
; p
++) {
1391 struct worktree
*wt
= *p
;
1392 struct index_state istate
= { NULL
};
1395 continue; /* current index already taken care of */
1397 if (read_index_from(&istate
,
1398 worktree_git_path(wt
, "index"),
1399 get_worktree_git_dir(wt
)) > 0)
1400 do_add_index_objects_to_pending(revs
, &istate
, flags
);
1401 discard_index(&istate
);
1403 free_worktrees(worktrees
);
1406 static int add_parents_only(struct rev_info
*revs
, const char *arg_
, int flags
,
1409 struct object_id oid
;
1411 struct commit
*commit
;
1412 struct commit_list
*parents
;
1414 const char *arg
= arg_
;
1417 flags
^= UNINTERESTING
| BOTTOM
;
1420 if (get_oid_committish(arg
, &oid
))
1423 it
= get_reference(revs
, arg
, &oid
, 0);
1424 if (!it
&& revs
->ignore_missing
)
1426 if (it
->type
!= OBJ_TAG
)
1428 if (!((struct tag
*)it
)->tagged
)
1430 oidcpy(&oid
, &((struct tag
*)it
)->tagged
->oid
);
1432 if (it
->type
!= OBJ_COMMIT
)
1434 commit
= (struct commit
*)it
;
1435 if (exclude_parent
&&
1436 exclude_parent
> commit_list_count(commit
->parents
))
1438 for (parents
= commit
->parents
, parent_number
= 1;
1440 parents
= parents
->next
, parent_number
++) {
1441 if (exclude_parent
&& parent_number
!= exclude_parent
)
1444 it
= &parents
->item
->object
;
1446 add_rev_cmdline(revs
, it
, arg_
, REV_CMD_PARENTS_ONLY
, flags
);
1447 add_pending_object(revs
, it
, arg
);
1452 void repo_init_revisions(struct repository
*r
,
1453 struct rev_info
*revs
,
1456 memset(revs
, 0, sizeof(*revs
));
1459 revs
->abbrev
= DEFAULT_ABBREV
;
1460 revs
->ignore_merges
= 1;
1461 revs
->simplify_history
= 1;
1462 revs
->pruning
.flags
.recursive
= 1;
1463 revs
->pruning
.flags
.quick
= 1;
1464 revs
->pruning
.add_remove
= file_add_remove
;
1465 revs
->pruning
.change
= file_change
;
1466 revs
->pruning
.change_fn_data
= revs
;
1467 revs
->sort_order
= REV_SORT_IN_GRAPH_ORDER
;
1469 revs
->prefix
= prefix
;
1472 revs
->skip_count
= -1;
1473 revs
->max_count
= -1;
1474 revs
->max_parents
= -1;
1475 revs
->expand_tabs_in_log
= -1;
1477 revs
->commit_format
= CMIT_FMT_DEFAULT
;
1478 revs
->expand_tabs_in_log_default
= 8;
1480 init_grep_defaults(revs
->repo
);
1481 grep_init(&revs
->grep_filter
, revs
->repo
, prefix
);
1482 revs
->grep_filter
.status_only
= 1;
1484 repo_diff_setup(revs
->repo
, &revs
->diffopt
);
1485 if (prefix
&& !revs
->diffopt
.prefix
) {
1486 revs
->diffopt
.prefix
= prefix
;
1487 revs
->diffopt
.prefix_length
= strlen(prefix
);
1490 revs
->notes_opt
.use_default_notes
= -1;
1493 static void add_pending_commit_list(struct rev_info
*revs
,
1494 struct commit_list
*commit_list
,
1497 while (commit_list
) {
1498 struct object
*object
= &commit_list
->item
->object
;
1499 object
->flags
|= flags
;
1500 add_pending_object(revs
, object
, oid_to_hex(&object
->oid
));
1501 commit_list
= commit_list
->next
;
1505 static void prepare_show_merge(struct rev_info
*revs
)
1507 struct commit_list
*bases
;
1508 struct commit
*head
, *other
;
1509 struct object_id oid
;
1510 const char **prune
= NULL
;
1511 int i
, prune_num
= 1; /* counting terminating NULL */
1512 struct index_state
*istate
= revs
->repo
->index
;
1514 if (get_oid("HEAD", &oid
))
1515 die("--merge without HEAD?");
1516 head
= lookup_commit_or_die(&oid
, "HEAD");
1517 if (get_oid("MERGE_HEAD", &oid
))
1518 die("--merge without MERGE_HEAD?");
1519 other
= lookup_commit_or_die(&oid
, "MERGE_HEAD");
1520 add_pending_object(revs
, &head
->object
, "HEAD");
1521 add_pending_object(revs
, &other
->object
, "MERGE_HEAD");
1522 bases
= get_merge_bases(head
, other
);
1523 add_rev_cmdline_list(revs
, bases
, REV_CMD_MERGE_BASE
, UNINTERESTING
| BOTTOM
);
1524 add_pending_commit_list(revs
, bases
, UNINTERESTING
| BOTTOM
);
1525 free_commit_list(bases
);
1526 head
->object
.flags
|= SYMMETRIC_LEFT
;
1528 if (!istate
->cache_nr
)
1530 for (i
= 0; i
< istate
->cache_nr
; i
++) {
1531 const struct cache_entry
*ce
= istate
->cache
[i
];
1534 if (ce_path_match(istate
, ce
, &revs
->prune_data
, NULL
)) {
1536 REALLOC_ARRAY(prune
, prune_num
);
1537 prune
[prune_num
-2] = ce
->name
;
1538 prune
[prune_num
-1] = NULL
;
1540 while ((i
+1 < istate
->cache_nr
) &&
1541 ce_same_name(ce
, istate
->cache
[i
+1]))
1544 clear_pathspec(&revs
->prune_data
);
1545 parse_pathspec(&revs
->prune_data
, PATHSPEC_ALL_MAGIC
& ~PATHSPEC_LITERAL
,
1546 PATHSPEC_PREFER_FULL
| PATHSPEC_LITERAL_PATH
, "", prune
);
1550 static int dotdot_missing(const char *arg
, char *dotdot
,
1551 struct rev_info
*revs
, int symmetric
)
1553 if (revs
->ignore_missing
)
1555 /* de-munge so we report the full argument */
1558 ? "Invalid symmetric difference expression %s"
1559 : "Invalid revision range %s", arg
);
1562 static int handle_dotdot_1(const char *arg
, char *dotdot
,
1563 struct rev_info
*revs
, int flags
,
1564 int cant_be_filename
,
1565 struct object_context
*a_oc
,
1566 struct object_context
*b_oc
)
1568 const char *a_name
, *b_name
;
1569 struct object_id a_oid
, b_oid
;
1570 struct object
*a_obj
, *b_obj
;
1571 unsigned int a_flags
, b_flags
;
1573 unsigned int flags_exclude
= flags
^ (UNINTERESTING
| BOTTOM
);
1574 unsigned int oc_flags
= GET_OID_COMMITTISH
| GET_OID_RECORD_PATH
;
1580 b_name
= dotdot
+ 2;
1581 if (*b_name
== '.') {
1588 if (get_oid_with_context(a_name
, oc_flags
, &a_oid
, a_oc
) ||
1589 get_oid_with_context(b_name
, oc_flags
, &b_oid
, b_oc
))
1592 if (!cant_be_filename
) {
1594 verify_non_filename(revs
->prefix
, arg
);
1598 a_obj
= parse_object(revs
->repo
, &a_oid
);
1599 b_obj
= parse_object(revs
->repo
, &b_oid
);
1600 if (!a_obj
|| !b_obj
)
1601 return dotdot_missing(arg
, dotdot
, revs
, symmetric
);
1606 a_flags
= flags_exclude
;
1608 /* A...B -- find merge bases between the two */
1609 struct commit
*a
, *b
;
1610 struct commit_list
*exclude
;
1612 a
= lookup_commit_reference(revs
->repo
, &a_obj
->oid
);
1613 b
= lookup_commit_reference(revs
->repo
, &b_obj
->oid
);
1615 return dotdot_missing(arg
, dotdot
, revs
, symmetric
);
1617 exclude
= get_merge_bases(a
, b
);
1618 add_rev_cmdline_list(revs
, exclude
, REV_CMD_MERGE_BASE
,
1620 add_pending_commit_list(revs
, exclude
, flags_exclude
);
1621 free_commit_list(exclude
);
1624 a_flags
= flags
| SYMMETRIC_LEFT
;
1627 a_obj
->flags
|= a_flags
;
1628 b_obj
->flags
|= b_flags
;
1629 add_rev_cmdline(revs
, a_obj
, a_name
, REV_CMD_LEFT
, a_flags
);
1630 add_rev_cmdline(revs
, b_obj
, b_name
, REV_CMD_RIGHT
, b_flags
);
1631 add_pending_object_with_path(revs
, a_obj
, a_name
, a_oc
->mode
, a_oc
->path
);
1632 add_pending_object_with_path(revs
, b_obj
, b_name
, b_oc
->mode
, b_oc
->path
);
1636 static int handle_dotdot(const char *arg
,
1637 struct rev_info
*revs
, int flags
,
1638 int cant_be_filename
)
1640 struct object_context a_oc
, b_oc
;
1641 char *dotdot
= strstr(arg
, "..");
1647 memset(&a_oc
, 0, sizeof(a_oc
));
1648 memset(&b_oc
, 0, sizeof(b_oc
));
1651 ret
= handle_dotdot_1(arg
, dotdot
, revs
, flags
, cant_be_filename
,
1661 int handle_revision_arg(const char *arg_
, struct rev_info
*revs
, int flags
, unsigned revarg_opt
)
1663 struct object_context oc
;
1665 struct object
*object
;
1666 struct object_id oid
;
1668 const char *arg
= arg_
;
1669 int cant_be_filename
= revarg_opt
& REVARG_CANNOT_BE_FILENAME
;
1670 unsigned get_sha1_flags
= GET_OID_RECORD_PATH
;
1672 flags
= flags
& UNINTERESTING
? flags
| BOTTOM
: flags
& ~BOTTOM
;
1674 if (!cant_be_filename
&& !strcmp(arg
, "..")) {
1676 * Just ".."? That is not a range but the
1677 * pathspec for the parent directory.
1682 if (!handle_dotdot(arg
, revs
, flags
, revarg_opt
))
1685 mark
= strstr(arg
, "^@");
1686 if (mark
&& !mark
[2]) {
1688 if (add_parents_only(revs
, arg
, flags
, 0))
1692 mark
= strstr(arg
, "^!");
1693 if (mark
&& !mark
[2]) {
1695 if (!add_parents_only(revs
, arg
, flags
^ (UNINTERESTING
| BOTTOM
), 0))
1698 mark
= strstr(arg
, "^-");
1700 int exclude_parent
= 1;
1704 exclude_parent
= strtoul(mark
+ 2, &end
, 10);
1705 if (*end
!= '\0' || !exclude_parent
)
1710 if (!add_parents_only(revs
, arg
, flags
^ (UNINTERESTING
| BOTTOM
), exclude_parent
))
1716 local_flags
= UNINTERESTING
| BOTTOM
;
1720 if (revarg_opt
& REVARG_COMMITTISH
)
1721 get_sha1_flags
|= GET_OID_COMMITTISH
;
1723 if (get_oid_with_context(arg
, get_sha1_flags
, &oid
, &oc
))
1724 return revs
->ignore_missing
? 0 : -1;
1725 if (!cant_be_filename
)
1726 verify_non_filename(revs
->prefix
, arg
);
1727 object
= get_reference(revs
, arg
, &oid
, flags
^ local_flags
);
1728 add_rev_cmdline(revs
, object
, arg_
, REV_CMD_REV
, flags
^ local_flags
);
1729 add_pending_object_with_path(revs
, object
, arg
, oc
.mode
, oc
.path
);
1734 static void read_pathspec_from_stdin(struct rev_info
*revs
, struct strbuf
*sb
,
1735 struct argv_array
*prune
)
1737 while (strbuf_getline(sb
, stdin
) != EOF
)
1738 argv_array_push(prune
, sb
->buf
);
1741 static void read_revisions_from_stdin(struct rev_info
*revs
,
1742 struct argv_array
*prune
)
1745 int seen_dashdash
= 0;
1748 save_warning
= warn_on_object_refname_ambiguity
;
1749 warn_on_object_refname_ambiguity
= 0;
1751 strbuf_init(&sb
, 1000);
1752 while (strbuf_getline(&sb
, stdin
) != EOF
) {
1756 if (sb
.buf
[0] == '-') {
1757 if (len
== 2 && sb
.buf
[1] == '-') {
1761 die("options not supported in --stdin mode");
1763 if (handle_revision_arg(sb
.buf
, revs
, 0,
1764 REVARG_CANNOT_BE_FILENAME
))
1765 die("bad revision '%s'", sb
.buf
);
1768 read_pathspec_from_stdin(revs
, &sb
, prune
);
1770 strbuf_release(&sb
);
1771 warn_on_object_refname_ambiguity
= save_warning
;
1774 static void add_grep(struct rev_info
*revs
, const char *ptn
, enum grep_pat_token what
)
1776 append_grep_pattern(&revs
->grep_filter
, ptn
, "command line", 0, what
);
1779 static void add_header_grep(struct rev_info
*revs
, enum grep_header_field field
, const char *pattern
)
1781 append_header_grep_pattern(&revs
->grep_filter
, field
, pattern
);
1784 static void add_message_grep(struct rev_info
*revs
, const char *pattern
)
1786 add_grep(revs
, pattern
, GREP_PATTERN_BODY
);
1789 static int handle_revision_opt(struct rev_info
*revs
, int argc
, const char **argv
,
1790 int *unkc
, const char **unkv
)
1792 const char *arg
= argv
[0];
1795 const unsigned hexsz
= the_hash_algo
->hexsz
;
1797 /* pseudo revision arguments */
1798 if (!strcmp(arg
, "--all") || !strcmp(arg
, "--branches") ||
1799 !strcmp(arg
, "--tags") || !strcmp(arg
, "--remotes") ||
1800 !strcmp(arg
, "--reflog") || !strcmp(arg
, "--not") ||
1801 !strcmp(arg
, "--no-walk") || !strcmp(arg
, "--do-walk") ||
1802 !strcmp(arg
, "--bisect") || starts_with(arg
, "--glob=") ||
1803 !strcmp(arg
, "--indexed-objects") ||
1804 starts_with(arg
, "--exclude=") ||
1805 starts_with(arg
, "--branches=") || starts_with(arg
, "--tags=") ||
1806 starts_with(arg
, "--remotes=") || starts_with(arg
, "--no-walk="))
1808 unkv
[(*unkc
)++] = arg
;
1812 if ((argcount
= parse_long_opt("max-count", argv
, &optarg
))) {
1813 revs
->max_count
= atoi(optarg
);
1816 } else if ((argcount
= parse_long_opt("skip", argv
, &optarg
))) {
1817 revs
->skip_count
= atoi(optarg
);
1819 } else if ((*arg
== '-') && isdigit(arg
[1])) {
1820 /* accept -<digit>, like traditional "head" */
1821 if (strtol_i(arg
+ 1, 10, &revs
->max_count
) < 0 ||
1822 revs
->max_count
< 0)
1823 die("'%s': not a non-negative integer", arg
+ 1);
1825 } else if (!strcmp(arg
, "-n")) {
1827 return error("-n requires an argument");
1828 revs
->max_count
= atoi(argv
[1]);
1831 } else if (skip_prefix(arg
, "-n", &optarg
)) {
1832 revs
->max_count
= atoi(optarg
);
1834 } else if ((argcount
= parse_long_opt("max-age", argv
, &optarg
))) {
1835 revs
->max_age
= atoi(optarg
);
1837 } else if ((argcount
= parse_long_opt("since", argv
, &optarg
))) {
1838 revs
->max_age
= approxidate(optarg
);
1840 } else if ((argcount
= parse_long_opt("after", argv
, &optarg
))) {
1841 revs
->max_age
= approxidate(optarg
);
1843 } else if ((argcount
= parse_long_opt("min-age", argv
, &optarg
))) {
1844 revs
->min_age
= atoi(optarg
);
1846 } else if ((argcount
= parse_long_opt("before", argv
, &optarg
))) {
1847 revs
->min_age
= approxidate(optarg
);
1849 } else if ((argcount
= parse_long_opt("until", argv
, &optarg
))) {
1850 revs
->min_age
= approxidate(optarg
);
1852 } else if (!strcmp(arg
, "--first-parent")) {
1853 revs
->first_parent_only
= 1;
1854 } else if (!strcmp(arg
, "--ancestry-path")) {
1855 revs
->ancestry_path
= 1;
1856 revs
->simplify_history
= 0;
1858 } else if (!strcmp(arg
, "-g") || !strcmp(arg
, "--walk-reflogs")) {
1859 init_reflog_walk(&revs
->reflog_info
);
1860 } else if (!strcmp(arg
, "--default")) {
1862 return error("bad --default argument");
1863 revs
->def
= argv
[1];
1865 } else if (!strcmp(arg
, "--merge")) {
1866 revs
->show_merge
= 1;
1867 } else if (!strcmp(arg
, "--topo-order")) {
1868 revs
->sort_order
= REV_SORT_IN_GRAPH_ORDER
;
1869 revs
->topo_order
= 1;
1870 } else if (!strcmp(arg
, "--simplify-merges")) {
1871 revs
->simplify_merges
= 1;
1872 revs
->topo_order
= 1;
1873 revs
->rewrite_parents
= 1;
1874 revs
->simplify_history
= 0;
1876 } else if (!strcmp(arg
, "--simplify-by-decoration")) {
1877 revs
->simplify_merges
= 1;
1878 revs
->topo_order
= 1;
1879 revs
->rewrite_parents
= 1;
1880 revs
->simplify_history
= 0;
1881 revs
->simplify_by_decoration
= 1;
1884 load_ref_decorations(NULL
, DECORATE_SHORT_REFS
);
1885 } else if (!strcmp(arg
, "--date-order")) {
1886 revs
->sort_order
= REV_SORT_BY_COMMIT_DATE
;
1887 revs
->topo_order
= 1;
1888 } else if (!strcmp(arg
, "--author-date-order")) {
1889 revs
->sort_order
= REV_SORT_BY_AUTHOR_DATE
;
1890 revs
->topo_order
= 1;
1891 } else if (!strcmp(arg
, "--early-output")) {
1892 revs
->early_output
= 100;
1893 revs
->topo_order
= 1;
1894 } else if (skip_prefix(arg
, "--early-output=", &optarg
)) {
1895 if (strtoul_ui(optarg
, 10, &revs
->early_output
) < 0)
1896 die("'%s': not a non-negative integer", optarg
);
1897 revs
->topo_order
= 1;
1898 } else if (!strcmp(arg
, "--parents")) {
1899 revs
->rewrite_parents
= 1;
1900 revs
->print_parents
= 1;
1901 } else if (!strcmp(arg
, "--dense")) {
1903 } else if (!strcmp(arg
, "--sparse")) {
1905 } else if (!strcmp(arg
, "--in-commit-order")) {
1906 revs
->tree_blobs_in_commit_order
= 1;
1907 } else if (!strcmp(arg
, "--remove-empty")) {
1908 revs
->remove_empty_trees
= 1;
1909 } else if (!strcmp(arg
, "--merges")) {
1910 revs
->min_parents
= 2;
1911 } else if (!strcmp(arg
, "--no-merges")) {
1912 revs
->max_parents
= 1;
1913 } else if (skip_prefix(arg
, "--min-parents=", &optarg
)) {
1914 revs
->min_parents
= atoi(optarg
);
1915 } else if (!strcmp(arg
, "--no-min-parents")) {
1916 revs
->min_parents
= 0;
1917 } else if (skip_prefix(arg
, "--max-parents=", &optarg
)) {
1918 revs
->max_parents
= atoi(optarg
);
1919 } else if (!strcmp(arg
, "--no-max-parents")) {
1920 revs
->max_parents
= -1;
1921 } else if (!strcmp(arg
, "--boundary")) {
1923 } else if (!strcmp(arg
, "--left-right")) {
1924 revs
->left_right
= 1;
1925 } else if (!strcmp(arg
, "--left-only")) {
1926 if (revs
->right_only
)
1927 die("--left-only is incompatible with --right-only"
1929 revs
->left_only
= 1;
1930 } else if (!strcmp(arg
, "--right-only")) {
1931 if (revs
->left_only
)
1932 die("--right-only is incompatible with --left-only");
1933 revs
->right_only
= 1;
1934 } else if (!strcmp(arg
, "--cherry")) {
1935 if (revs
->left_only
)
1936 die("--cherry is incompatible with --left-only");
1937 revs
->cherry_mark
= 1;
1938 revs
->right_only
= 1;
1939 revs
->max_parents
= 1;
1941 } else if (!strcmp(arg
, "--count")) {
1943 } else if (!strcmp(arg
, "--cherry-mark")) {
1944 if (revs
->cherry_pick
)
1945 die("--cherry-mark is incompatible with --cherry-pick");
1946 revs
->cherry_mark
= 1;
1947 revs
->limited
= 1; /* needs limit_list() */
1948 } else if (!strcmp(arg
, "--cherry-pick")) {
1949 if (revs
->cherry_mark
)
1950 die("--cherry-pick is incompatible with --cherry-mark");
1951 revs
->cherry_pick
= 1;
1953 } else if (!strcmp(arg
, "--objects")) {
1954 revs
->tag_objects
= 1;
1955 revs
->tree_objects
= 1;
1956 revs
->blob_objects
= 1;
1957 } else if (!strcmp(arg
, "--objects-edge")) {
1958 revs
->tag_objects
= 1;
1959 revs
->tree_objects
= 1;
1960 revs
->blob_objects
= 1;
1961 revs
->edge_hint
= 1;
1962 } else if (!strcmp(arg
, "--objects-edge-aggressive")) {
1963 revs
->tag_objects
= 1;
1964 revs
->tree_objects
= 1;
1965 revs
->blob_objects
= 1;
1966 revs
->edge_hint
= 1;
1967 revs
->edge_hint_aggressive
= 1;
1968 } else if (!strcmp(arg
, "--verify-objects")) {
1969 revs
->tag_objects
= 1;
1970 revs
->tree_objects
= 1;
1971 revs
->blob_objects
= 1;
1972 revs
->verify_objects
= 1;
1973 } else if (!strcmp(arg
, "--unpacked")) {
1975 } else if (starts_with(arg
, "--unpacked=")) {
1976 die("--unpacked=<packfile> no longer supported.");
1977 } else if (!strcmp(arg
, "-r")) {
1979 revs
->diffopt
.flags
.recursive
= 1;
1980 } else if (!strcmp(arg
, "-t")) {
1982 revs
->diffopt
.flags
.recursive
= 1;
1983 revs
->diffopt
.flags
.tree_in_recursive
= 1;
1984 } else if (!strcmp(arg
, "-m")) {
1985 revs
->ignore_merges
= 0;
1986 } else if (!strcmp(arg
, "-c")) {
1988 revs
->dense_combined_merges
= 0;
1989 revs
->combine_merges
= 1;
1990 } else if (!strcmp(arg
, "--cc")) {
1992 revs
->dense_combined_merges
= 1;
1993 revs
->combine_merges
= 1;
1994 } else if (!strcmp(arg
, "-v")) {
1995 revs
->verbose_header
= 1;
1996 } else if (!strcmp(arg
, "--pretty")) {
1997 revs
->verbose_header
= 1;
1998 revs
->pretty_given
= 1;
1999 get_commit_format(NULL
, revs
);
2000 } else if (skip_prefix(arg
, "--pretty=", &optarg
) ||
2001 skip_prefix(arg
, "--format=", &optarg
)) {
2003 * Detached form ("--pretty X" as opposed to "--pretty=X")
2004 * not allowed, since the argument is optional.
2006 revs
->verbose_header
= 1;
2007 revs
->pretty_given
= 1;
2008 get_commit_format(optarg
, revs
);
2009 } else if (!strcmp(arg
, "--expand-tabs")) {
2010 revs
->expand_tabs_in_log
= 8;
2011 } else if (!strcmp(arg
, "--no-expand-tabs")) {
2012 revs
->expand_tabs_in_log
= 0;
2013 } else if (skip_prefix(arg
, "--expand-tabs=", &arg
)) {
2015 if (strtol_i(arg
, 10, &val
) < 0 || val
< 0)
2016 die("'%s': not a non-negative integer", arg
);
2017 revs
->expand_tabs_in_log
= val
;
2018 } else if (!strcmp(arg
, "--show-notes") || !strcmp(arg
, "--notes")) {
2019 revs
->show_notes
= 1;
2020 revs
->show_notes_given
= 1;
2021 revs
->notes_opt
.use_default_notes
= 1;
2022 } else if (!strcmp(arg
, "--show-signature")) {
2023 revs
->show_signature
= 1;
2024 } else if (!strcmp(arg
, "--no-show-signature")) {
2025 revs
->show_signature
= 0;
2026 } else if (!strcmp(arg
, "--show-linear-break")) {
2027 revs
->break_bar
= " ..........";
2028 revs
->track_linear
= 1;
2029 revs
->track_first_time
= 1;
2030 } else if (skip_prefix(arg
, "--show-linear-break=", &optarg
)) {
2031 revs
->break_bar
= xstrdup(optarg
);
2032 revs
->track_linear
= 1;
2033 revs
->track_first_time
= 1;
2034 } else if (skip_prefix(arg
, "--show-notes=", &optarg
) ||
2035 skip_prefix(arg
, "--notes=", &optarg
)) {
2036 struct strbuf buf
= STRBUF_INIT
;
2037 revs
->show_notes
= 1;
2038 revs
->show_notes_given
= 1;
2039 if (starts_with(arg
, "--show-notes=") &&
2040 revs
->notes_opt
.use_default_notes
< 0)
2041 revs
->notes_opt
.use_default_notes
= 1;
2042 strbuf_addstr(&buf
, optarg
);
2043 expand_notes_ref(&buf
);
2044 string_list_append(&revs
->notes_opt
.extra_notes_refs
,
2045 strbuf_detach(&buf
, NULL
));
2046 } else if (!strcmp(arg
, "--no-notes")) {
2047 revs
->show_notes
= 0;
2048 revs
->show_notes_given
= 1;
2049 revs
->notes_opt
.use_default_notes
= -1;
2050 /* we have been strdup'ing ourselves, so trick
2051 * string_list into free()ing strings */
2052 revs
->notes_opt
.extra_notes_refs
.strdup_strings
= 1;
2053 string_list_clear(&revs
->notes_opt
.extra_notes_refs
, 0);
2054 revs
->notes_opt
.extra_notes_refs
.strdup_strings
= 0;
2055 } else if (!strcmp(arg
, "--standard-notes")) {
2056 revs
->show_notes_given
= 1;
2057 revs
->notes_opt
.use_default_notes
= 1;
2058 } else if (!strcmp(arg
, "--no-standard-notes")) {
2059 revs
->notes_opt
.use_default_notes
= 0;
2060 } else if (!strcmp(arg
, "--oneline")) {
2061 revs
->verbose_header
= 1;
2062 get_commit_format("oneline", revs
);
2063 revs
->pretty_given
= 1;
2064 revs
->abbrev_commit
= 1;
2065 } else if (!strcmp(arg
, "--graph")) {
2066 revs
->topo_order
= 1;
2067 revs
->rewrite_parents
= 1;
2068 revs
->graph
= graph_init(revs
);
2069 } else if (!strcmp(arg
, "--root")) {
2070 revs
->show_root_diff
= 1;
2071 } else if (!strcmp(arg
, "--no-commit-id")) {
2072 revs
->no_commit_id
= 1;
2073 } else if (!strcmp(arg
, "--always")) {
2074 revs
->always_show_header
= 1;
2075 } else if (!strcmp(arg
, "--no-abbrev")) {
2077 } else if (!strcmp(arg
, "--abbrev")) {
2078 revs
->abbrev
= DEFAULT_ABBREV
;
2079 } else if (skip_prefix(arg
, "--abbrev=", &optarg
)) {
2080 revs
->abbrev
= strtoul(optarg
, NULL
, 10);
2081 if (revs
->abbrev
< MINIMUM_ABBREV
)
2082 revs
->abbrev
= MINIMUM_ABBREV
;
2083 else if (revs
->abbrev
> hexsz
)
2084 revs
->abbrev
= hexsz
;
2085 } else if (!strcmp(arg
, "--abbrev-commit")) {
2086 revs
->abbrev_commit
= 1;
2087 revs
->abbrev_commit_given
= 1;
2088 } else if (!strcmp(arg
, "--no-abbrev-commit")) {
2089 revs
->abbrev_commit
= 0;
2090 } else if (!strcmp(arg
, "--full-diff")) {
2092 revs
->full_diff
= 1;
2093 } else if (!strcmp(arg
, "--full-history")) {
2094 revs
->simplify_history
= 0;
2095 } else if (!strcmp(arg
, "--relative-date")) {
2096 revs
->date_mode
.type
= DATE_RELATIVE
;
2097 revs
->date_mode_explicit
= 1;
2098 } else if ((argcount
= parse_long_opt("date", argv
, &optarg
))) {
2099 parse_date_format(optarg
, &revs
->date_mode
);
2100 revs
->date_mode_explicit
= 1;
2102 } else if (!strcmp(arg
, "--log-size")) {
2103 revs
->show_log_size
= 1;
2106 * Grepping the commit log
2108 else if ((argcount
= parse_long_opt("author", argv
, &optarg
))) {
2109 add_header_grep(revs
, GREP_HEADER_AUTHOR
, optarg
);
2111 } else if ((argcount
= parse_long_opt("committer", argv
, &optarg
))) {
2112 add_header_grep(revs
, GREP_HEADER_COMMITTER
, optarg
);
2114 } else if ((argcount
= parse_long_opt("grep-reflog", argv
, &optarg
))) {
2115 add_header_grep(revs
, GREP_HEADER_REFLOG
, optarg
);
2117 } else if ((argcount
= parse_long_opt("grep", argv
, &optarg
))) {
2118 add_message_grep(revs
, optarg
);
2120 } else if (!strcmp(arg
, "--grep-debug")) {
2121 revs
->grep_filter
.debug
= 1;
2122 } else if (!strcmp(arg
, "--basic-regexp")) {
2123 revs
->grep_filter
.pattern_type_option
= GREP_PATTERN_TYPE_BRE
;
2124 } else if (!strcmp(arg
, "--extended-regexp") || !strcmp(arg
, "-E")) {
2125 revs
->grep_filter
.pattern_type_option
= GREP_PATTERN_TYPE_ERE
;
2126 } else if (!strcmp(arg
, "--regexp-ignore-case") || !strcmp(arg
, "-i")) {
2127 revs
->grep_filter
.ignore_case
= 1;
2128 revs
->diffopt
.pickaxe_opts
|= DIFF_PICKAXE_IGNORE_CASE
;
2129 } else if (!strcmp(arg
, "--fixed-strings") || !strcmp(arg
, "-F")) {
2130 revs
->grep_filter
.pattern_type_option
= GREP_PATTERN_TYPE_FIXED
;
2131 } else if (!strcmp(arg
, "--perl-regexp") || !strcmp(arg
, "-P")) {
2132 revs
->grep_filter
.pattern_type_option
= GREP_PATTERN_TYPE_PCRE
;
2133 } else if (!strcmp(arg
, "--all-match")) {
2134 revs
->grep_filter
.all_match
= 1;
2135 } else if (!strcmp(arg
, "--invert-grep")) {
2136 revs
->invert_grep
= 1;
2137 } else if ((argcount
= parse_long_opt("encoding", argv
, &optarg
))) {
2138 if (strcmp(optarg
, "none"))
2139 git_log_output_encoding
= xstrdup(optarg
);
2141 git_log_output_encoding
= "";
2143 } else if (!strcmp(arg
, "--reverse")) {
2145 } else if (!strcmp(arg
, "--children")) {
2146 revs
->children
.name
= "children";
2148 } else if (!strcmp(arg
, "--ignore-missing")) {
2149 revs
->ignore_missing
= 1;
2150 } else if (revs
->allow_exclude_promisor_objects_opt
&&
2151 !strcmp(arg
, "--exclude-promisor-objects")) {
2152 if (fetch_if_missing
)
2153 BUG("exclude_promisor_objects can only be used when fetch_if_missing is 0");
2154 revs
->exclude_promisor_objects
= 1;
2156 int opts
= diff_opt_parse(&revs
->diffopt
, argv
, argc
, revs
->prefix
);
2158 unkv
[(*unkc
)++] = arg
;
2161 if (revs
->graph
&& revs
->track_linear
)
2162 die("--show-linear-break and --graph are incompatible");
2167 void parse_revision_opt(struct rev_info
*revs
, struct parse_opt_ctx_t
*ctx
,
2168 const struct option
*options
,
2169 const char * const usagestr
[])
2171 int n
= handle_revision_opt(revs
, ctx
->argc
, ctx
->argv
,
2172 &ctx
->cpidx
, ctx
->out
);
2174 error("unknown option `%s'", ctx
->argv
[0]);
2175 usage_with_options(usagestr
, options
);
2181 static int for_each_bisect_ref(struct ref_store
*refs
, each_ref_fn fn
,
2182 void *cb_data
, const char *term
)
2184 struct strbuf bisect_refs
= STRBUF_INIT
;
2186 strbuf_addf(&bisect_refs
, "refs/bisect/%s", term
);
2187 status
= refs_for_each_fullref_in(refs
, bisect_refs
.buf
, fn
, cb_data
, 0);
2188 strbuf_release(&bisect_refs
);
2192 static int for_each_bad_bisect_ref(struct ref_store
*refs
, each_ref_fn fn
, void *cb_data
)
2194 return for_each_bisect_ref(refs
, fn
, cb_data
, term_bad
);
2197 static int for_each_good_bisect_ref(struct ref_store
*refs
, each_ref_fn fn
, void *cb_data
)
2199 return for_each_bisect_ref(refs
, fn
, cb_data
, term_good
);
2202 static int handle_revision_pseudo_opt(const char *submodule
,
2203 struct rev_info
*revs
,
2204 int argc
, const char **argv
, int *flags
)
2206 const char *arg
= argv
[0];
2208 struct ref_store
*refs
;
2213 * We need some something like get_submodule_worktrees()
2214 * before we can go through all worktrees of a submodule,
2215 * .e.g with adding all HEADs from --all, which is not
2216 * supported right now, so stick to single worktree.
2218 if (!revs
->single_worktree
)
2219 BUG("--single-worktree cannot be used together with submodule");
2220 refs
= get_submodule_ref_store(submodule
);
2222 refs
= get_main_ref_store(revs
->repo
);
2227 * Commands like "git shortlog" will not accept the options below
2228 * unless parse_revision_opt queues them (as opposed to erroring
2231 * When implementing your new pseudo-option, remember to
2232 * register it in the list at the top of handle_revision_opt.
2234 if (!strcmp(arg
, "--all")) {
2235 handle_refs(refs
, revs
, *flags
, refs_for_each_ref
);
2236 handle_refs(refs
, revs
, *flags
, refs_head_ref
);
2237 if (!revs
->single_worktree
) {
2238 struct all_refs_cb cb
;
2240 init_all_refs_cb(&cb
, revs
, *flags
);
2241 other_head_refs(handle_one_ref
, &cb
);
2243 clear_ref_exclusion(&revs
->ref_excludes
);
2244 } else if (!strcmp(arg
, "--branches")) {
2245 handle_refs(refs
, revs
, *flags
, refs_for_each_branch_ref
);
2246 clear_ref_exclusion(&revs
->ref_excludes
);
2247 } else if (!strcmp(arg
, "--bisect")) {
2248 read_bisect_terms(&term_bad
, &term_good
);
2249 handle_refs(refs
, revs
, *flags
, for_each_bad_bisect_ref
);
2250 handle_refs(refs
, revs
, *flags
^ (UNINTERESTING
| BOTTOM
),
2251 for_each_good_bisect_ref
);
2253 } else if (!strcmp(arg
, "--tags")) {
2254 handle_refs(refs
, revs
, *flags
, refs_for_each_tag_ref
);
2255 clear_ref_exclusion(&revs
->ref_excludes
);
2256 } else if (!strcmp(arg
, "--remotes")) {
2257 handle_refs(refs
, revs
, *flags
, refs_for_each_remote_ref
);
2258 clear_ref_exclusion(&revs
->ref_excludes
);
2259 } else if ((argcount
= parse_long_opt("glob", argv
, &optarg
))) {
2260 struct all_refs_cb cb
;
2261 init_all_refs_cb(&cb
, revs
, *flags
);
2262 for_each_glob_ref(handle_one_ref
, optarg
, &cb
);
2263 clear_ref_exclusion(&revs
->ref_excludes
);
2265 } else if ((argcount
= parse_long_opt("exclude", argv
, &optarg
))) {
2266 add_ref_exclusion(&revs
->ref_excludes
, optarg
);
2268 } else if (skip_prefix(arg
, "--branches=", &optarg
)) {
2269 struct all_refs_cb cb
;
2270 init_all_refs_cb(&cb
, revs
, *flags
);
2271 for_each_glob_ref_in(handle_one_ref
, optarg
, "refs/heads/", &cb
);
2272 clear_ref_exclusion(&revs
->ref_excludes
);
2273 } else if (skip_prefix(arg
, "--tags=", &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/tags/", &cb
);
2277 clear_ref_exclusion(&revs
->ref_excludes
);
2278 } else if (skip_prefix(arg
, "--remotes=", &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/remotes/", &cb
);
2282 clear_ref_exclusion(&revs
->ref_excludes
);
2283 } else if (!strcmp(arg
, "--reflog")) {
2284 add_reflogs_to_pending(revs
, *flags
);
2285 } else if (!strcmp(arg
, "--indexed-objects")) {
2286 add_index_objects_to_pending(revs
, *flags
);
2287 } else if (!strcmp(arg
, "--not")) {
2288 *flags
^= UNINTERESTING
| BOTTOM
;
2289 } else if (!strcmp(arg
, "--no-walk")) {
2290 revs
->no_walk
= REVISION_WALK_NO_WALK_SORTED
;
2291 } else if (skip_prefix(arg
, "--no-walk=", &optarg
)) {
2293 * Detached form ("--no-walk X" as opposed to "--no-walk=X")
2294 * not allowed, since the argument is optional.
2296 if (!strcmp(optarg
, "sorted"))
2297 revs
->no_walk
= REVISION_WALK_NO_WALK_SORTED
;
2298 else if (!strcmp(optarg
, "unsorted"))
2299 revs
->no_walk
= REVISION_WALK_NO_WALK_UNSORTED
;
2301 return error("invalid argument to --no-walk");
2302 } else if (!strcmp(arg
, "--do-walk")) {
2304 } else if (!strcmp(arg
, "--single-worktree")) {
2305 revs
->single_worktree
= 1;
2313 static void NORETURN
diagnose_missing_default(const char *def
)
2316 const char *refname
;
2318 refname
= resolve_ref_unsafe(def
, 0, NULL
, &flags
);
2319 if (!refname
|| !(flags
& REF_ISSYMREF
) || (flags
& REF_ISBROKEN
))
2320 die(_("your current branch appears to be broken"));
2322 skip_prefix(refname
, "refs/heads/", &refname
);
2323 die(_("your current branch '%s' does not have any commits yet"),
2328 * Parse revision information, filling in the "rev_info" structure,
2329 * and removing the used arguments from the argument list.
2331 * Returns the number of arguments left that weren't recognized
2332 * (which are also moved to the head of the argument list)
2334 int setup_revisions(int argc
, const char **argv
, struct rev_info
*revs
, struct setup_revision_opt
*opt
)
2336 int i
, flags
, left
, seen_dashdash
, got_rev_arg
= 0, revarg_opt
;
2337 struct argv_array prune_data
= ARGV_ARRAY_INIT
;
2338 const char *submodule
= NULL
;
2341 submodule
= opt
->submodule
;
2343 /* First, search for "--" */
2344 if (opt
&& opt
->assume_dashdash
) {
2348 for (i
= 1; i
< argc
; i
++) {
2349 const char *arg
= argv
[i
];
2350 if (strcmp(arg
, "--"))
2355 argv_array_pushv(&prune_data
, argv
+ i
+ 1);
2361 /* Second, deal with arguments and options */
2363 revarg_opt
= opt
? opt
->revarg_opt
: 0;
2365 revarg_opt
|= REVARG_CANNOT_BE_FILENAME
;
2366 for (left
= i
= 1; i
< argc
; i
++) {
2367 const char *arg
= argv
[i
];
2371 opts
= handle_revision_pseudo_opt(submodule
,
2372 revs
, argc
- i
, argv
+ i
,
2379 if (!strcmp(arg
, "--stdin")) {
2380 if (revs
->disable_stdin
) {
2384 if (revs
->read_from_stdin
++)
2385 die("--stdin given twice?");
2386 read_revisions_from_stdin(revs
, &prune_data
);
2390 opts
= handle_revision_opt(revs
, argc
- i
, argv
+ i
, &left
, argv
);
2401 if (handle_revision_arg(arg
, revs
, flags
, revarg_opt
)) {
2403 if (seen_dashdash
|| *arg
== '^')
2404 die("bad revision '%s'", arg
);
2406 /* If we didn't have a "--":
2407 * (1) all filenames must exist;
2408 * (2) all rev-args must not be interpretable
2409 * as a valid filename.
2410 * but the latter we have checked in the main loop.
2412 for (j
= i
; j
< argc
; j
++)
2413 verify_filename(revs
->prefix
, argv
[j
], j
== i
);
2415 argv_array_pushv(&prune_data
, argv
+ i
);
2422 if (prune_data
.argc
) {
2424 * If we need to introduce the magic "a lone ':' means no
2425 * pathspec whatsoever", here is the place to do so.
2427 * if (prune_data.nr == 1 && !strcmp(prune_data[0], ":")) {
2428 * prune_data.nr = 0;
2429 * prune_data.alloc = 0;
2430 * free(prune_data.path);
2431 * prune_data.path = NULL;
2433 * terminate prune_data.alloc with NULL and
2434 * call init_pathspec() to set revs->prune_data here.
2437 parse_pathspec(&revs
->prune_data
, 0, 0,
2438 revs
->prefix
, prune_data
.argv
);
2440 argv_array_clear(&prune_data
);
2442 if (revs
->def
== NULL
)
2443 revs
->def
= opt
? opt
->def
: NULL
;
2444 if (opt
&& opt
->tweak
)
2445 opt
->tweak(revs
, opt
);
2446 if (revs
->show_merge
)
2447 prepare_show_merge(revs
);
2448 if (revs
->def
&& !revs
->pending
.nr
&& !revs
->rev_input_given
&& !got_rev_arg
) {
2449 struct object_id oid
;
2450 struct object
*object
;
2451 struct object_context oc
;
2452 if (get_oid_with_context(revs
->def
, 0, &oid
, &oc
))
2453 diagnose_missing_default(revs
->def
);
2454 object
= get_reference(revs
, revs
->def
, &oid
, 0);
2455 add_pending_object_with_mode(revs
, object
, revs
->def
, oc
.mode
);
2458 /* Did the user ask for any diff output? Run the diff! */
2459 if (revs
->diffopt
.output_format
& ~DIFF_FORMAT_NO_OUTPUT
)
2462 /* Pickaxe, diff-filter and rename following need diffs */
2463 if ((revs
->diffopt
.pickaxe_opts
& DIFF_PICKAXE_KINDS_MASK
) ||
2464 revs
->diffopt
.filter
||
2465 revs
->diffopt
.flags
.follow_renames
)
2468 if (revs
->diffopt
.objfind
)
2469 revs
->simplify_history
= 0;
2471 if (revs
->topo_order
)
2474 if (revs
->prune_data
.nr
) {
2475 copy_pathspec(&revs
->pruning
.pathspec
, &revs
->prune_data
);
2476 /* Can't prune commits with rename following: the paths change.. */
2477 if (!revs
->diffopt
.flags
.follow_renames
)
2479 if (!revs
->full_diff
)
2480 copy_pathspec(&revs
->diffopt
.pathspec
,
2483 if (revs
->combine_merges
)
2484 revs
->ignore_merges
= 0;
2485 revs
->diffopt
.abbrev
= revs
->abbrev
;
2487 if (revs
->line_level_traverse
) {
2489 revs
->topo_order
= 1;
2492 diff_setup_done(&revs
->diffopt
);
2494 grep_commit_pattern_type(GREP_PATTERN_TYPE_UNSPECIFIED
,
2495 &revs
->grep_filter
);
2496 compile_grep_patterns(&revs
->grep_filter
);
2498 if (revs
->reverse
&& revs
->reflog_info
)
2499 die("cannot combine --reverse with --walk-reflogs");
2500 if (revs
->reflog_info
&& revs
->limited
)
2501 die("cannot combine --walk-reflogs with history-limiting options");
2502 if (revs
->rewrite_parents
&& revs
->children
.name
)
2503 die("cannot combine --parents and --children");
2506 * Limitations on the graph functionality
2508 if (revs
->reverse
&& revs
->graph
)
2509 die("cannot combine --reverse with --graph");
2511 if (revs
->reflog_info
&& revs
->graph
)
2512 die("cannot combine --walk-reflogs with --graph");
2513 if (revs
->no_walk
&& revs
->graph
)
2514 die("cannot combine --no-walk with --graph");
2515 if (!revs
->reflog_info
&& revs
->grep_filter
.use_reflog_filter
)
2516 die("cannot use --grep-reflog without --walk-reflogs");
2518 if (revs
->first_parent_only
&& revs
->bisect
)
2519 die(_("--first-parent is incompatible with --bisect"));
2521 if (revs
->expand_tabs_in_log
< 0)
2522 revs
->expand_tabs_in_log
= revs
->expand_tabs_in_log_default
;
2527 static void add_child(struct rev_info
*revs
, struct commit
*parent
, struct commit
*child
)
2529 struct commit_list
*l
= xcalloc(1, sizeof(*l
));
2532 l
->next
= add_decoration(&revs
->children
, &parent
->object
, l
);
2535 static int remove_duplicate_parents(struct rev_info
*revs
, struct commit
*commit
)
2537 struct treesame_state
*ts
= lookup_decoration(&revs
->treesame
, &commit
->object
);
2538 struct commit_list
**pp
, *p
;
2539 int surviving_parents
;
2541 /* Examine existing parents while marking ones we have seen... */
2542 pp
= &commit
->parents
;
2543 surviving_parents
= 0;
2544 while ((p
= *pp
) != NULL
) {
2545 struct commit
*parent
= p
->item
;
2546 if (parent
->object
.flags
& TMP_MARK
) {
2549 compact_treesame(revs
, commit
, surviving_parents
);
2552 parent
->object
.flags
|= TMP_MARK
;
2553 surviving_parents
++;
2556 /* clear the temporary mark */
2557 for (p
= commit
->parents
; p
; p
= p
->next
) {
2558 p
->item
->object
.flags
&= ~TMP_MARK
;
2560 /* no update_treesame() - removing duplicates can't affect TREESAME */
2561 return surviving_parents
;
2564 struct merge_simplify_state
{
2565 struct commit
*simplified
;
2568 static struct merge_simplify_state
*locate_simplify_state(struct rev_info
*revs
, struct commit
*commit
)
2570 struct merge_simplify_state
*st
;
2572 st
= lookup_decoration(&revs
->merge_simplification
, &commit
->object
);
2574 st
= xcalloc(1, sizeof(*st
));
2575 add_decoration(&revs
->merge_simplification
, &commit
->object
, st
);
2580 static int mark_redundant_parents(struct rev_info
*revs
, struct commit
*commit
)
2582 struct commit_list
*h
= reduce_heads(commit
->parents
);
2583 int i
= 0, marked
= 0;
2584 struct commit_list
*po
, *pn
;
2586 /* Want these for sanity-checking only */
2587 int orig_cnt
= commit_list_count(commit
->parents
);
2588 int cnt
= commit_list_count(h
);
2591 * Not ready to remove items yet, just mark them for now, based
2592 * on the output of reduce_heads(). reduce_heads outputs the reduced
2593 * set in its original order, so this isn't too hard.
2595 po
= commit
->parents
;
2598 if (pn
&& po
->item
== pn
->item
) {
2602 po
->item
->object
.flags
|= TMP_MARK
;
2608 if (i
!= cnt
|| cnt
+marked
!= orig_cnt
)
2609 die("mark_redundant_parents %d %d %d %d", orig_cnt
, cnt
, i
, marked
);
2611 free_commit_list(h
);
2616 static int mark_treesame_root_parents(struct rev_info
*revs
, struct commit
*commit
)
2618 struct commit_list
*p
;
2621 for (p
= commit
->parents
; p
; p
= p
->next
) {
2622 struct commit
*parent
= p
->item
;
2623 if (!parent
->parents
&& (parent
->object
.flags
& TREESAME
)) {
2624 parent
->object
.flags
|= TMP_MARK
;
2633 * Awkward naming - this means one parent we are TREESAME to.
2634 * cf mark_treesame_root_parents: root parents that are TREESAME (to an
2635 * empty tree). Better name suggestions?
2637 static int leave_one_treesame_to_parent(struct rev_info
*revs
, struct commit
*commit
)
2639 struct treesame_state
*ts
= lookup_decoration(&revs
->treesame
, &commit
->object
);
2640 struct commit
*unmarked
= NULL
, *marked
= NULL
;
2641 struct commit_list
*p
;
2644 for (p
= commit
->parents
, n
= 0; p
; p
= p
->next
, n
++) {
2645 if (ts
->treesame
[n
]) {
2646 if (p
->item
->object
.flags
& TMP_MARK
) {
2659 * If we are TREESAME to a marked-for-deletion parent, but not to any
2660 * unmarked parents, unmark the first TREESAME parent. This is the
2661 * parent that the default simplify_history==1 scan would have followed,
2662 * and it doesn't make sense to omit that path when asking for a
2663 * simplified full history. Retaining it improves the chances of
2664 * understanding odd missed merges that took an old version of a file.
2668 * I--------*X A modified the file, but mainline merge X used
2669 * \ / "-s ours", so took the version from I. X is
2670 * `-*A--' TREESAME to I and !TREESAME to A.
2672 * Default log from X would produce "I". Without this check,
2673 * --full-history --simplify-merges would produce "I-A-X", showing
2674 * the merge commit X and that it changed A, but not making clear that
2675 * it had just taken the I version. With this check, the topology above
2678 * Note that it is possible that the simplification chooses a different
2679 * TREESAME parent from the default, in which case this test doesn't
2680 * activate, and we _do_ drop the default parent. Example:
2682 * I------X A modified the file, but it was reverted in B,
2683 * \ / meaning mainline merge X is TREESAME to both
2686 * Default log would produce "I" by following the first parent;
2687 * --full-history --simplify-merges will produce "I-A-B". But this is a
2688 * reasonable result - it presents a logical full history leading from
2689 * I to X, and X is not an important merge.
2691 if (!unmarked
&& marked
) {
2692 marked
->object
.flags
&= ~TMP_MARK
;
2699 static int remove_marked_parents(struct rev_info
*revs
, struct commit
*commit
)
2701 struct commit_list
**pp
, *p
;
2702 int nth_parent
, removed
= 0;
2704 pp
= &commit
->parents
;
2706 while ((p
= *pp
) != NULL
) {
2707 struct commit
*parent
= p
->item
;
2708 if (parent
->object
.flags
& TMP_MARK
) {
2709 parent
->object
.flags
&= ~TMP_MARK
;
2713 compact_treesame(revs
, commit
, nth_parent
);
2720 /* Removing parents can only increase TREESAMEness */
2721 if (removed
&& !(commit
->object
.flags
& TREESAME
))
2722 update_treesame(revs
, commit
);
2727 static struct commit_list
**simplify_one(struct rev_info
*revs
, struct commit
*commit
, struct commit_list
**tail
)
2729 struct commit_list
*p
;
2730 struct commit
*parent
;
2731 struct merge_simplify_state
*st
, *pst
;
2734 st
= locate_simplify_state(revs
, commit
);
2737 * Have we handled this one?
2743 * An UNINTERESTING commit simplifies to itself, so does a
2744 * root commit. We do not rewrite parents of such commit
2747 if ((commit
->object
.flags
& UNINTERESTING
) || !commit
->parents
) {
2748 st
->simplified
= commit
;
2753 * Do we know what commit all of our parents that matter
2754 * should be rewritten to? Otherwise we are not ready to
2755 * rewrite this one yet.
2757 for (cnt
= 0, p
= commit
->parents
; p
; p
= p
->next
) {
2758 pst
= locate_simplify_state(revs
, p
->item
);
2759 if (!pst
->simplified
) {
2760 tail
= &commit_list_insert(p
->item
, tail
)->next
;
2763 if (revs
->first_parent_only
)
2767 tail
= &commit_list_insert(commit
, tail
)->next
;
2772 * Rewrite our list of parents. Note that this cannot
2773 * affect our TREESAME flags in any way - a commit is
2774 * always TREESAME to its simplification.
2776 for (p
= commit
->parents
; p
; p
= p
->next
) {
2777 pst
= locate_simplify_state(revs
, p
->item
);
2778 p
->item
= pst
->simplified
;
2779 if (revs
->first_parent_only
)
2783 if (revs
->first_parent_only
)
2786 cnt
= remove_duplicate_parents(revs
, commit
);
2789 * It is possible that we are a merge and one side branch
2790 * does not have any commit that touches the given paths;
2791 * in such a case, the immediate parent from that branch
2792 * will be rewritten to be the merge base.
2794 * o----X X: the commit we are looking at;
2795 * / / o: a commit that touches the paths;
2798 * Further, a merge of an independent branch that doesn't
2799 * touch the path will reduce to a treesame root parent:
2801 * ----o----X X: the commit we are looking at;
2802 * / o: a commit that touches the paths;
2803 * r r: a root commit not touching the paths
2805 * Detect and simplify both cases.
2808 int marked
= mark_redundant_parents(revs
, commit
);
2809 marked
+= mark_treesame_root_parents(revs
, commit
);
2811 marked
-= leave_one_treesame_to_parent(revs
, commit
);
2813 cnt
= remove_marked_parents(revs
, commit
);
2817 * A commit simplifies to itself if it is a root, if it is
2818 * UNINTERESTING, if it touches the given paths, or if it is a
2819 * merge and its parents don't simplify to one relevant commit
2820 * (the first two cases are already handled at the beginning of
2823 * Otherwise, it simplifies to what its sole relevant parent
2827 (commit
->object
.flags
& UNINTERESTING
) ||
2828 !(commit
->object
.flags
& TREESAME
) ||
2829 (parent
= one_relevant_parent(revs
, commit
->parents
)) == NULL
)
2830 st
->simplified
= commit
;
2832 pst
= locate_simplify_state(revs
, parent
);
2833 st
->simplified
= pst
->simplified
;
2838 static void simplify_merges(struct rev_info
*revs
)
2840 struct commit_list
*list
, *next
;
2841 struct commit_list
*yet_to_do
, **tail
;
2842 struct commit
*commit
;
2847 /* feed the list reversed */
2849 for (list
= revs
->commits
; list
; list
= next
) {
2850 commit
= list
->item
;
2853 * Do not free(list) here yet; the original list
2854 * is used later in this function.
2856 commit_list_insert(commit
, &yet_to_do
);
2863 commit
= pop_commit(&list
);
2864 tail
= simplify_one(revs
, commit
, tail
);
2868 /* clean up the result, removing the simplified ones */
2869 list
= revs
->commits
;
2870 revs
->commits
= NULL
;
2871 tail
= &revs
->commits
;
2873 struct merge_simplify_state
*st
;
2875 commit
= pop_commit(&list
);
2876 st
= locate_simplify_state(revs
, commit
);
2877 if (st
->simplified
== commit
)
2878 tail
= &commit_list_insert(commit
, tail
)->next
;
2882 static void set_children(struct rev_info
*revs
)
2884 struct commit_list
*l
;
2885 for (l
= revs
->commits
; l
; l
= l
->next
) {
2886 struct commit
*commit
= l
->item
;
2887 struct commit_list
*p
;
2889 for (p
= commit
->parents
; p
; p
= p
->next
)
2890 add_child(revs
, p
->item
, commit
);
2894 void reset_revision_walk(void)
2896 clear_object_flags(SEEN
| ADDED
| SHOWN
);
2899 static int mark_uninteresting(const struct object_id
*oid
,
2900 struct packed_git
*pack
,
2904 struct rev_info
*revs
= cb
;
2905 struct object
*o
= parse_object(revs
->repo
, oid
);
2906 o
->flags
|= UNINTERESTING
| SEEN
;
2910 int prepare_revision_walk(struct rev_info
*revs
)
2913 struct object_array old_pending
;
2914 struct commit_list
**next
= &revs
->commits
;
2916 memcpy(&old_pending
, &revs
->pending
, sizeof(old_pending
));
2917 revs
->pending
.nr
= 0;
2918 revs
->pending
.alloc
= 0;
2919 revs
->pending
.objects
= NULL
;
2920 for (i
= 0; i
< old_pending
.nr
; i
++) {
2921 struct object_array_entry
*e
= old_pending
.objects
+ i
;
2922 struct commit
*commit
= handle_commit(revs
, e
);
2924 if (!(commit
->object
.flags
& SEEN
)) {
2925 commit
->object
.flags
|= SEEN
;
2926 next
= commit_list_append(commit
, next
);
2930 object_array_clear(&old_pending
);
2932 /* Signal whether we need per-parent treesame decoration */
2933 if (revs
->simplify_merges
||
2934 (revs
->limited
&& limiting_can_increase_treesame(revs
)))
2935 revs
->treesame
.name
= "treesame";
2937 if (revs
->exclude_promisor_objects
) {
2938 for_each_packed_object(mark_uninteresting
, revs
,
2939 FOR_EACH_OBJECT_PROMISOR_ONLY
);
2942 if (revs
->no_walk
!= REVISION_WALK_NO_WALK_UNSORTED
)
2943 commit_list_sort_by_date(&revs
->commits
);
2947 if (limit_list(revs
) < 0)
2949 if (revs
->topo_order
)
2950 sort_in_topological_order(&revs
->commits
, revs
->sort_order
);
2951 if (revs
->line_level_traverse
)
2952 line_log_filter(revs
);
2953 if (revs
->simplify_merges
)
2954 simplify_merges(revs
);
2955 if (revs
->children
.name
)
2960 static enum rewrite_result
rewrite_one(struct rev_info
*revs
, struct commit
**pp
)
2962 struct commit_list
*cache
= NULL
;
2965 struct commit
*p
= *pp
;
2967 if (add_parents_to_list(revs
, p
, &revs
->commits
, &cache
) < 0)
2968 return rewrite_one_error
;
2969 if (p
->object
.flags
& UNINTERESTING
)
2970 return rewrite_one_ok
;
2971 if (!(p
->object
.flags
& TREESAME
))
2972 return rewrite_one_ok
;
2974 return rewrite_one_noparents
;
2975 if ((p
= one_relevant_parent(revs
, p
->parents
)) == NULL
)
2976 return rewrite_one_ok
;
2981 int rewrite_parents(struct rev_info
*revs
, struct commit
*commit
,
2982 rewrite_parent_fn_t rewrite_parent
)
2984 struct commit_list
**pp
= &commit
->parents
;
2986 struct commit_list
*parent
= *pp
;
2987 switch (rewrite_parent(revs
, &parent
->item
)) {
2988 case rewrite_one_ok
:
2990 case rewrite_one_noparents
:
2993 case rewrite_one_error
:
2998 remove_duplicate_parents(revs
, commit
);
3002 static int commit_rewrite_person(struct strbuf
*buf
, const char *what
, struct string_list
*mailmap
)
3004 char *person
, *endp
;
3005 size_t len
, namelen
, maillen
;
3008 struct ident_split ident
;
3010 person
= strstr(buf
->buf
, what
);
3014 person
+= strlen(what
);
3015 endp
= strchr(person
, '\n');
3019 len
= endp
- person
;
3021 if (split_ident_line(&ident
, person
, len
))
3024 mail
= ident
.mail_begin
;
3025 maillen
= ident
.mail_end
- ident
.mail_begin
;
3026 name
= ident
.name_begin
;
3027 namelen
= ident
.name_end
- ident
.name_begin
;
3029 if (map_user(mailmap
, &mail
, &maillen
, &name
, &namelen
)) {
3030 struct strbuf namemail
= STRBUF_INIT
;
3032 strbuf_addf(&namemail
, "%.*s <%.*s>",
3033 (int)namelen
, name
, (int)maillen
, mail
);
3035 strbuf_splice(buf
, ident
.name_begin
- buf
->buf
,
3036 ident
.mail_end
- ident
.name_begin
+ 1,
3037 namemail
.buf
, namemail
.len
);
3039 strbuf_release(&namemail
);
3047 static int commit_match(struct commit
*commit
, struct rev_info
*opt
)
3050 const char *encoding
;
3051 const char *message
;
3052 struct strbuf buf
= STRBUF_INIT
;
3054 if (!opt
->grep_filter
.pattern_list
&& !opt
->grep_filter
.header_list
)
3057 /* Prepend "fake" headers as needed */
3058 if (opt
->grep_filter
.use_reflog_filter
) {
3059 strbuf_addstr(&buf
, "reflog ");
3060 get_reflog_message(&buf
, opt
->reflog_info
);
3061 strbuf_addch(&buf
, '\n');
3065 * We grep in the user's output encoding, under the assumption that it
3066 * is the encoding they are most likely to write their grep pattern
3067 * for. In addition, it means we will match the "notes" encoding below,
3068 * so we will not end up with a buffer that has two different encodings
3071 encoding
= get_log_output_encoding();
3072 message
= logmsg_reencode(commit
, NULL
, encoding
);
3074 /* Copy the commit to temporary if we are using "fake" headers */
3076 strbuf_addstr(&buf
, message
);
3078 if (opt
->grep_filter
.header_list
&& opt
->mailmap
) {
3080 strbuf_addstr(&buf
, message
);
3082 commit_rewrite_person(&buf
, "\nauthor ", opt
->mailmap
);
3083 commit_rewrite_person(&buf
, "\ncommitter ", opt
->mailmap
);
3086 /* Append "fake" message parts as needed */
3087 if (opt
->show_notes
) {
3089 strbuf_addstr(&buf
, message
);
3090 format_display_notes(&commit
->object
.oid
, &buf
, encoding
, 1);
3094 * Find either in the original commit message, or in the temporary.
3095 * Note that we cast away the constness of "message" here. It is
3096 * const because it may come from the cached commit buffer. That's OK,
3097 * because we know that it is modifiable heap memory, and that while
3098 * grep_buffer may modify it for speed, it will restore any
3099 * changes before returning.
3102 retval
= grep_buffer(&opt
->grep_filter
, buf
.buf
, buf
.len
);
3104 retval
= grep_buffer(&opt
->grep_filter
,
3105 (char *)message
, strlen(message
));
3106 strbuf_release(&buf
);
3107 unuse_commit_buffer(commit
, message
);
3108 return opt
->invert_grep
? !retval
: retval
;
3111 static inline int want_ancestry(const struct rev_info
*revs
)
3113 return (revs
->rewrite_parents
|| revs
->children
.name
);
3117 * Return a timestamp to be used for --since/--until comparisons for this
3118 * commit, based on the revision options.
3120 static timestamp_t
comparison_date(const struct rev_info
*revs
,
3121 struct commit
*commit
)
3123 return revs
->reflog_info
?
3124 get_reflog_timestamp(revs
->reflog_info
) :
3128 enum commit_action
get_commit_action(struct rev_info
*revs
, struct commit
*commit
)
3130 if (commit
->object
.flags
& SHOWN
)
3131 return commit_ignore
;
3132 if (revs
->unpacked
&& has_object_pack(&commit
->object
.oid
))
3133 return commit_ignore
;
3134 if (commit
->object
.flags
& UNINTERESTING
)
3135 return commit_ignore
;
3136 if (revs
->min_age
!= -1 &&
3137 comparison_date(revs
, commit
) > revs
->min_age
)
3138 return commit_ignore
;
3139 if (revs
->min_parents
|| (revs
->max_parents
>= 0)) {
3140 int n
= commit_list_count(commit
->parents
);
3141 if ((n
< revs
->min_parents
) ||
3142 ((revs
->max_parents
>= 0) && (n
> revs
->max_parents
)))
3143 return commit_ignore
;
3145 if (!commit_match(commit
, revs
))
3146 return commit_ignore
;
3147 if (revs
->prune
&& revs
->dense
) {
3148 /* Commit without changes? */
3149 if (commit
->object
.flags
& TREESAME
) {
3151 struct commit_list
*p
;
3152 /* drop merges unless we want parenthood */
3153 if (!want_ancestry(revs
))
3154 return commit_ignore
;
3156 * If we want ancestry, then need to keep any merges
3157 * between relevant commits to tie together topology.
3158 * For consistency with TREESAME and simplification
3159 * use "relevant" here rather than just INTERESTING,
3160 * to treat bottom commit(s) as part of the topology.
3162 for (n
= 0, p
= commit
->parents
; p
; p
= p
->next
)
3163 if (relevant_commit(p
->item
))
3166 return commit_ignore
;
3172 define_commit_slab(saved_parents
, struct commit_list
*);
3174 #define EMPTY_PARENT_LIST ((struct commit_list *)-1)
3177 * You may only call save_parents() once per commit (this is checked
3178 * for non-root commits).
3180 static void save_parents(struct rev_info
*revs
, struct commit
*commit
)
3182 struct commit_list
**pp
;
3184 if (!revs
->saved_parents_slab
) {
3185 revs
->saved_parents_slab
= xmalloc(sizeof(struct saved_parents
));
3186 init_saved_parents(revs
->saved_parents_slab
);
3189 pp
= saved_parents_at(revs
->saved_parents_slab
, commit
);
3192 * When walking with reflogs, we may visit the same commit
3193 * several times: once for each appearance in the reflog.
3195 * In this case, save_parents() will be called multiple times.
3196 * We want to keep only the first set of parents. We need to
3197 * store a sentinel value for an empty (i.e., NULL) parent
3198 * list to distinguish it from a not-yet-saved list, however.
3202 if (commit
->parents
)
3203 *pp
= copy_commit_list(commit
->parents
);
3205 *pp
= EMPTY_PARENT_LIST
;
3208 static void free_saved_parents(struct rev_info
*revs
)
3210 if (revs
->saved_parents_slab
)
3211 clear_saved_parents(revs
->saved_parents_slab
);
3214 struct commit_list
*get_saved_parents(struct rev_info
*revs
, const struct commit
*commit
)
3216 struct commit_list
*parents
;
3218 if (!revs
->saved_parents_slab
)
3219 return commit
->parents
;
3221 parents
= *saved_parents_at(revs
->saved_parents_slab
, commit
);
3222 if (parents
== EMPTY_PARENT_LIST
)
3227 enum commit_action
simplify_commit(struct rev_info
*revs
, struct commit
*commit
)
3229 enum commit_action action
= get_commit_action(revs
, commit
);
3231 if (action
== commit_show
&&
3232 revs
->prune
&& revs
->dense
&& want_ancestry(revs
)) {
3234 * --full-diff on simplified parents is no good: it
3235 * will show spurious changes from the commits that
3236 * were elided. So we save the parents on the side
3237 * when --full-diff is in effect.
3239 if (revs
->full_diff
)
3240 save_parents(revs
, commit
);
3241 if (rewrite_parents(revs
, commit
, rewrite_one
) < 0)
3242 return commit_error
;
3247 static void track_linear(struct rev_info
*revs
, struct commit
*commit
)
3249 if (revs
->track_first_time
) {
3251 revs
->track_first_time
= 0;
3253 struct commit_list
*p
;
3254 for (p
= revs
->previous_parents
; p
; p
= p
->next
)
3255 if (p
->item
== NULL
|| /* first commit */
3256 oideq(&p
->item
->object
.oid
, &commit
->object
.oid
))
3258 revs
->linear
= p
!= NULL
;
3260 if (revs
->reverse
) {
3262 commit
->object
.flags
|= TRACK_LINEAR
;
3264 free_commit_list(revs
->previous_parents
);
3265 revs
->previous_parents
= copy_commit_list(commit
->parents
);
3268 static struct commit
*get_revision_1(struct rev_info
*revs
)
3271 struct commit
*commit
;
3273 if (revs
->reflog_info
)
3274 commit
= next_reflog_entry(revs
->reflog_info
);
3276 commit
= pop_commit(&revs
->commits
);
3281 if (revs
->reflog_info
)
3282 commit
->object
.flags
&= ~(ADDED
| SEEN
| SHOWN
);
3285 * If we haven't done the list limiting, we need to look at
3286 * the parents here. We also need to do the date-based limiting
3287 * that we'd otherwise have done in limit_list().
3289 if (!revs
->limited
) {
3290 if (revs
->max_age
!= -1 &&
3291 comparison_date(revs
, commit
) < revs
->max_age
)
3294 if (revs
->reflog_info
)
3295 try_to_simplify_commit(revs
, commit
);
3296 else if (add_parents_to_list(revs
, commit
, &revs
->commits
, NULL
) < 0) {
3297 if (!revs
->ignore_missing_links
)
3298 die("Failed to traverse parents of commit %s",
3299 oid_to_hex(&commit
->object
.oid
));
3303 switch (simplify_commit(revs
, commit
)) {
3307 die("Failed to simplify parents of commit %s",
3308 oid_to_hex(&commit
->object
.oid
));
3310 if (revs
->track_linear
)
3311 track_linear(revs
, commit
);
3318 * Return true for entries that have not yet been shown. (This is an
3319 * object_array_each_func_t.)
3321 static int entry_unshown(struct object_array_entry
*entry
, void *cb_data_unused
)
3323 return !(entry
->item
->flags
& SHOWN
);
3327 * If array is on the verge of a realloc, garbage-collect any entries
3328 * that have already been shown to try to free up some space.
3330 static void gc_boundary(struct object_array
*array
)
3332 if (array
->nr
== array
->alloc
)
3333 object_array_filter(array
, entry_unshown
, NULL
);
3336 static void create_boundary_commit_list(struct rev_info
*revs
)
3340 struct object_array
*array
= &revs
->boundary_commits
;
3341 struct object_array_entry
*objects
= array
->objects
;
3344 * If revs->commits is non-NULL at this point, an error occurred in
3345 * get_revision_1(). Ignore the error and continue printing the
3346 * boundary commits anyway. (This is what the code has always
3349 if (revs
->commits
) {
3350 free_commit_list(revs
->commits
);
3351 revs
->commits
= NULL
;
3355 * Put all of the actual boundary commits from revs->boundary_commits
3356 * into revs->commits
3358 for (i
= 0; i
< array
->nr
; i
++) {
3359 c
= (struct commit
*)(objects
[i
].item
);
3362 if (!(c
->object
.flags
& CHILD_SHOWN
))
3364 if (c
->object
.flags
& (SHOWN
| BOUNDARY
))
3366 c
->object
.flags
|= BOUNDARY
;
3367 commit_list_insert(c
, &revs
->commits
);
3371 * If revs->topo_order is set, sort the boundary commits
3372 * in topological order
3374 sort_in_topological_order(&revs
->commits
, revs
->sort_order
);
3377 static struct commit
*get_revision_internal(struct rev_info
*revs
)
3379 struct commit
*c
= NULL
;
3380 struct commit_list
*l
;
3382 if (revs
->boundary
== 2) {
3384 * All of the normal commits have already been returned,
3385 * and we are now returning boundary commits.
3386 * create_boundary_commit_list() has populated
3387 * revs->commits with the remaining commits to return.
3389 c
= pop_commit(&revs
->commits
);
3391 c
->object
.flags
|= SHOWN
;
3396 * If our max_count counter has reached zero, then we are done. We
3397 * don't simply return NULL because we still might need to show
3398 * boundary commits. But we want to avoid calling get_revision_1, which
3399 * might do a considerable amount of work finding the next commit only
3400 * for us to throw it away.
3402 * If it is non-zero, then either we don't have a max_count at all
3403 * (-1), or it is still counting, in which case we decrement.
3405 if (revs
->max_count
) {
3406 c
= get_revision_1(revs
);
3408 while (revs
->skip_count
> 0) {
3410 c
= get_revision_1(revs
);
3416 if (revs
->max_count
> 0)
3421 c
->object
.flags
|= SHOWN
;
3423 if (!revs
->boundary
)
3428 * get_revision_1() runs out the commits, and
3429 * we are done computing the boundaries.
3430 * switch to boundary commits output mode.
3435 * Update revs->commits to contain the list of
3438 create_boundary_commit_list(revs
);
3440 return get_revision_internal(revs
);
3444 * boundary commits are the commits that are parents of the
3445 * ones we got from get_revision_1() but they themselves are
3446 * not returned from get_revision_1(). Before returning
3447 * 'c', we need to mark its parents that they could be boundaries.
3450 for (l
= c
->parents
; l
; l
= l
->next
) {
3452 p
= &(l
->item
->object
);
3453 if (p
->flags
& (CHILD_SHOWN
| SHOWN
))
3455 p
->flags
|= CHILD_SHOWN
;
3456 gc_boundary(&revs
->boundary_commits
);
3457 add_object_array(p
, NULL
, &revs
->boundary_commits
);
3463 struct commit
*get_revision(struct rev_info
*revs
)
3466 struct commit_list
*reversed
;
3468 if (revs
->reverse
) {
3470 while ((c
= get_revision_internal(revs
)))
3471 commit_list_insert(c
, &reversed
);
3472 revs
->commits
= reversed
;
3474 revs
->reverse_output_stage
= 1;
3477 if (revs
->reverse_output_stage
) {
3478 c
= pop_commit(&revs
->commits
);
3479 if (revs
->track_linear
)
3480 revs
->linear
= !!(c
&& c
->object
.flags
& TRACK_LINEAR
);
3484 c
= get_revision_internal(revs
);
3485 if (c
&& revs
->graph
)
3486 graph_update(revs
->graph
, c
);
3488 free_saved_parents(revs
);
3489 if (revs
->previous_parents
) {
3490 free_commit_list(revs
->previous_parents
);
3491 revs
->previous_parents
= NULL
;
3497 char *get_revision_mark(const struct rev_info
*revs
, const struct commit
*commit
)
3499 if (commit
->object
.flags
& BOUNDARY
)
3501 else if (commit
->object
.flags
& UNINTERESTING
)
3503 else if (commit
->object
.flags
& PATCHSAME
)
3505 else if (!revs
|| revs
->left_right
) {
3506 if (commit
->object
.flags
& SYMMETRIC_LEFT
)
3510 } else if (revs
->graph
)
3512 else if (revs
->cherry_mark
)
3517 void put_revision_mark(const struct rev_info
*revs
, const struct commit
*commit
)
3519 char *mark
= get_revision_mark(revs
, commit
);
3522 fputs(mark
, stdout
);