9 #include "repository.h"
12 #include "reflog-walk.h"
13 #include "patch-ids.h"
16 #include "string-list.h"
19 #include "commit-slab.h"
21 #include "cache-tree.h"
25 #include "argv-array.h"
27 volatile show_early_output_fn_t show_early_output
;
29 static const char *term_bad
;
30 static const char *term_good
;
32 void show_object_with_name(FILE *out
, struct object
*obj
, const char *name
)
36 fprintf(out
, "%s ", oid_to_hex(&obj
->oid
));
37 for (p
= name
; *p
&& *p
!= '\n'; p
++)
42 static void mark_blob_uninteresting(struct blob
*blob
)
46 if (blob
->object
.flags
& UNINTERESTING
)
48 blob
->object
.flags
|= UNINTERESTING
;
51 static void mark_tree_contents_uninteresting(struct tree
*tree
)
53 struct tree_desc desc
;
54 struct name_entry entry
;
56 if (parse_tree_gently(tree
, 1) < 0)
59 init_tree_desc(&desc
, tree
->buffer
, tree
->size
);
60 while (tree_entry(&desc
, &entry
)) {
61 switch (object_type(entry
.mode
)) {
63 mark_tree_uninteresting(lookup_tree(entry
.oid
));
66 mark_blob_uninteresting(lookup_blob(entry
.oid
));
69 /* Subproject commit - not in this repository */
75 * We don't care about the tree any more
76 * after it has been marked uninteresting.
78 free_tree_buffer(tree
);
81 void mark_tree_uninteresting(struct tree
*tree
)
89 if (obj
->flags
& UNINTERESTING
)
91 obj
->flags
|= UNINTERESTING
;
92 mark_tree_contents_uninteresting(tree
);
96 struct commit
**items
;
99 #define COMMIT_STACK_INIT { NULL, 0, 0 }
101 static void commit_stack_push(struct commit_stack
*stack
, struct commit
*commit
)
103 ALLOC_GROW(stack
->items
, stack
->nr
+ 1, stack
->alloc
);
104 stack
->items
[stack
->nr
++] = commit
;
107 static struct commit
*commit_stack_pop(struct commit_stack
*stack
)
109 return stack
->nr
? stack
->items
[--stack
->nr
] : NULL
;
112 static void commit_stack_clear(struct commit_stack
*stack
)
114 FREE_AND_NULL(stack
->items
);
115 stack
->nr
= stack
->alloc
= 0;
118 static void mark_one_parent_uninteresting(struct commit
*commit
,
119 struct commit_stack
*pending
)
121 struct commit_list
*l
;
123 if (commit
->object
.flags
& UNINTERESTING
)
125 commit
->object
.flags
|= UNINTERESTING
;
128 * Normally we haven't parsed the parent
129 * yet, so we won't have a parent of a parent
130 * here. However, it may turn out that we've
131 * reached this commit some other way (where it
132 * wasn't uninteresting), in which case we need
133 * to mark its parents recursively too..
135 for (l
= commit
->parents
; l
; l
= l
->next
)
136 commit_stack_push(pending
, l
->item
);
139 void mark_parents_uninteresting(struct commit
*commit
)
141 struct commit_stack pending
= COMMIT_STACK_INIT
;
142 struct commit_list
*l
;
144 for (l
= commit
->parents
; l
; l
= l
->next
)
145 mark_one_parent_uninteresting(l
->item
, &pending
);
147 while (pending
.nr
> 0)
148 mark_one_parent_uninteresting(commit_stack_pop(&pending
),
151 commit_stack_clear(&pending
);
154 static void add_pending_object_with_path(struct rev_info
*revs
,
156 const char *name
, unsigned mode
,
161 if (revs
->no_walk
&& (obj
->flags
& UNINTERESTING
))
163 if (revs
->reflog_info
&& obj
->type
== OBJ_COMMIT
) {
164 struct strbuf buf
= STRBUF_INIT
;
165 int len
= interpret_branch_name(name
, 0, &buf
, 0);
167 if (0 < len
&& name
[len
] && buf
.len
)
168 strbuf_addstr(&buf
, name
+ len
);
169 add_reflog_for_walk(revs
->reflog_info
,
170 (struct commit
*)obj
,
171 buf
.buf
[0] ? buf
.buf
: name
);
172 strbuf_release(&buf
);
173 return; /* do not add the commit itself */
175 obj
->flags
|= USER_GIVEN
;
176 add_object_array_with_path(obj
, name
, &revs
->pending
, mode
, path
);
179 static void add_pending_object_with_mode(struct rev_info
*revs
,
181 const char *name
, unsigned mode
)
183 add_pending_object_with_path(revs
, obj
, name
, mode
, NULL
);
186 void add_pending_object(struct rev_info
*revs
,
187 struct object
*obj
, const char *name
)
189 add_pending_object_with_mode(revs
, obj
, name
, S_IFINVALID
);
192 void add_head_to_pending(struct rev_info
*revs
)
194 struct object_id oid
;
196 if (get_oid("HEAD", &oid
))
198 obj
= parse_object(&oid
);
201 add_pending_object(revs
, obj
, "HEAD");
204 static struct object
*get_reference(struct rev_info
*revs
, const char *name
,
205 const struct object_id
*oid
,
208 struct object
*object
;
210 object
= parse_object(oid
);
212 if (revs
->ignore_missing
)
214 if (revs
->exclude_promisor_objects
&& is_promisor_object(oid
))
216 die("bad object %s", name
);
218 object
->flags
|= flags
;
222 void add_pending_oid(struct rev_info
*revs
, const char *name
,
223 const struct object_id
*oid
, unsigned int flags
)
225 struct object
*object
= get_reference(revs
, name
, oid
, flags
);
226 add_pending_object(revs
, object
, name
);
229 static struct commit
*handle_commit(struct rev_info
*revs
,
230 struct object_array_entry
*entry
)
232 struct object
*object
= entry
->item
;
233 const char *name
= entry
->name
;
234 const char *path
= entry
->path
;
235 unsigned int mode
= entry
->mode
;
236 unsigned long flags
= object
->flags
;
239 * Tag object? Look what it points to..
241 while (object
->type
== OBJ_TAG
) {
242 struct tag
*tag
= (struct tag
*) object
;
243 if (revs
->tag_objects
&& !(flags
& UNINTERESTING
))
244 add_pending_object(revs
, object
, tag
->tag
);
247 object
= parse_object(&tag
->tagged
->oid
);
249 if (revs
->ignore_missing_links
|| (flags
& UNINTERESTING
))
251 if (revs
->exclude_promisor_objects
&&
252 is_promisor_object(&tag
->tagged
->oid
))
254 die("bad object %s", oid_to_hex(&tag
->tagged
->oid
));
256 object
->flags
|= flags
;
258 * We'll handle the tagged object by looping or dropping
259 * through to the non-tag handlers below. Do not
260 * propagate path data from the tag's pending entry.
267 * Commit object? Just return it, we'll do all the complex
270 if (object
->type
== OBJ_COMMIT
) {
271 struct commit
*commit
= (struct commit
*)object
;
272 if (parse_commit(commit
) < 0)
273 die("unable to parse commit %s", name
);
274 if (flags
& UNINTERESTING
) {
275 mark_parents_uninteresting(commit
);
278 if (revs
->show_source
&& !commit
->util
)
279 commit
->util
= xstrdup(name
);
284 * Tree object? Either mark it uninteresting, or add it
285 * to the list of objects to look at later..
287 if (object
->type
== OBJ_TREE
) {
288 struct tree
*tree
= (struct tree
*)object
;
289 if (!revs
->tree_objects
)
291 if (flags
& UNINTERESTING
) {
292 mark_tree_contents_uninteresting(tree
);
295 add_pending_object_with_path(revs
, object
, name
, mode
, path
);
300 * Blob object? You know the drill by now..
302 if (object
->type
== OBJ_BLOB
) {
303 if (!revs
->blob_objects
)
305 if (flags
& UNINTERESTING
)
307 add_pending_object_with_path(revs
, object
, name
, mode
, path
);
310 die("%s is unknown object", name
);
313 static int everybody_uninteresting(struct commit_list
*orig
,
314 struct commit
**interesting_cache
)
316 struct commit_list
*list
= orig
;
318 if (*interesting_cache
) {
319 struct commit
*commit
= *interesting_cache
;
320 if (!(commit
->object
.flags
& UNINTERESTING
))
325 struct commit
*commit
= list
->item
;
327 if (commit
->object
.flags
& UNINTERESTING
)
330 *interesting_cache
= commit
;
337 * A definition of "relevant" commit that we can use to simplify limited graphs
338 * by eliminating side branches.
340 * A "relevant" commit is one that is !UNINTERESTING (ie we are including it
341 * in our list), or that is a specified BOTTOM commit. Then after computing
342 * a limited list, during processing we can generally ignore boundary merges
343 * coming from outside the graph, (ie from irrelevant parents), and treat
344 * those merges as if they were single-parent. TREESAME is defined to consider
345 * only relevant parents, if any. If we are TREESAME to our on-graph parents,
346 * we don't care if we were !TREESAME to non-graph parents.
348 * Treating bottom commits as relevant ensures that a limited graph's
349 * connection to the actual bottom commit is not viewed as a side branch, but
350 * treated as part of the graph. For example:
352 * ....Z...A---X---o---o---B
356 * When computing "A..B", the A-X connection is at least as important as
357 * Y-X, despite A being flagged UNINTERESTING.
359 * And when computing --ancestry-path "A..B", the A-X connection is more
360 * important than Y-X, despite both A and Y being flagged UNINTERESTING.
362 static inline int relevant_commit(struct commit
*commit
)
364 return (commit
->object
.flags
& (UNINTERESTING
| BOTTOM
)) != UNINTERESTING
;
368 * Return a single relevant commit from a parent list. If we are a TREESAME
369 * commit, and this selects one of our parents, then we can safely simplify to
372 static struct commit
*one_relevant_parent(const struct rev_info
*revs
,
373 struct commit_list
*orig
)
375 struct commit_list
*list
= orig
;
376 struct commit
*relevant
= NULL
;
382 * For 1-parent commits, or if first-parent-only, then return that
383 * first parent (even if not "relevant" by the above definition).
384 * TREESAME will have been set purely on that parent.
386 if (revs
->first_parent_only
|| !orig
->next
)
390 * For multi-parent commits, identify a sole relevant parent, if any.
391 * If we have only one relevant parent, then TREESAME will be set purely
392 * with regard to that parent, and we can simplify accordingly.
394 * If we have more than one relevant parent, or no relevant parents
395 * (and multiple irrelevant ones), then we can't select a parent here
399 struct commit
*commit
= list
->item
;
401 if (relevant_commit(commit
)) {
411 * The goal is to get REV_TREE_NEW as the result only if the
412 * diff consists of all '+' (and no other changes), REV_TREE_OLD
413 * if the whole diff is removal of old data, and otherwise
414 * REV_TREE_DIFFERENT (of course if the trees are the same we
415 * want REV_TREE_SAME).
417 * The only time we care about the distinction is when
418 * remove_empty_trees is in effect, in which case we care only about
419 * whether the whole change is REV_TREE_NEW, or if there's another type
420 * of change. Which means we can stop the diff early in either of these
423 * 1. We're not using remove_empty_trees at all.
425 * 2. We saw anything except REV_TREE_NEW.
427 static int tree_difference
= REV_TREE_SAME
;
429 static void file_add_remove(struct diff_options
*options
,
430 int addremove
, unsigned mode
,
431 const struct object_id
*oid
,
433 const char *fullpath
, unsigned dirty_submodule
)
435 int diff
= addremove
== '+' ? REV_TREE_NEW
: REV_TREE_OLD
;
436 struct rev_info
*revs
= options
->change_fn_data
;
438 tree_difference
|= diff
;
439 if (!revs
->remove_empty_trees
|| tree_difference
!= REV_TREE_NEW
)
440 options
->flags
.has_changes
= 1;
443 static void file_change(struct diff_options
*options
,
444 unsigned old_mode
, unsigned new_mode
,
445 const struct object_id
*old_oid
,
446 const struct object_id
*new_oid
,
447 int old_oid_valid
, int new_oid_valid
,
448 const char *fullpath
,
449 unsigned old_dirty_submodule
, unsigned new_dirty_submodule
)
451 tree_difference
= REV_TREE_DIFFERENT
;
452 options
->flags
.has_changes
= 1;
455 static int rev_compare_tree(struct rev_info
*revs
,
456 struct commit
*parent
, struct commit
*commit
)
458 struct tree
*t1
= get_commit_tree(parent
);
459 struct tree
*t2
= get_commit_tree(commit
);
466 if (revs
->simplify_by_decoration
) {
468 * If we are simplifying by decoration, then the commit
469 * is worth showing if it has a tag pointing at it.
471 if (get_name_decoration(&commit
->object
))
472 return REV_TREE_DIFFERENT
;
474 * A commit that is not pointed by a tag is uninteresting
475 * if we are not limited by path. This means that you will
476 * see the usual "commits that touch the paths" plus any
477 * tagged commit by specifying both --simplify-by-decoration
480 if (!revs
->prune_data
.nr
)
481 return REV_TREE_SAME
;
484 tree_difference
= REV_TREE_SAME
;
485 revs
->pruning
.flags
.has_changes
= 0;
486 if (diff_tree_oid(&t1
->object
.oid
, &t2
->object
.oid
, "",
488 return REV_TREE_DIFFERENT
;
489 return tree_difference
;
492 static int rev_same_tree_as_empty(struct rev_info
*revs
, struct commit
*commit
)
495 struct tree
*t1
= get_commit_tree(commit
);
500 tree_difference
= REV_TREE_SAME
;
501 revs
->pruning
.flags
.has_changes
= 0;
502 retval
= diff_tree_oid(NULL
, &t1
->object
.oid
, "", &revs
->pruning
);
504 return retval
>= 0 && (tree_difference
== REV_TREE_SAME
);
507 struct treesame_state
{
508 unsigned int nparents
;
509 unsigned char treesame
[FLEX_ARRAY
];
512 static struct treesame_state
*initialise_treesame(struct rev_info
*revs
, struct commit
*commit
)
514 unsigned n
= commit_list_count(commit
->parents
);
515 struct treesame_state
*st
= xcalloc(1, st_add(sizeof(*st
), n
));
517 add_decoration(&revs
->treesame
, &commit
->object
, st
);
522 * Must be called immediately after removing the nth_parent from a commit's
523 * parent list, if we are maintaining the per-parent treesame[] decoration.
524 * This does not recalculate the master TREESAME flag - update_treesame()
525 * should be called to update it after a sequence of treesame[] modifications
526 * that may have affected it.
528 static int compact_treesame(struct rev_info
*revs
, struct commit
*commit
, unsigned nth_parent
)
530 struct treesame_state
*st
;
533 if (!commit
->parents
) {
535 * Have just removed the only parent from a non-merge.
536 * Different handling, as we lack decoration.
539 die("compact_treesame %u", nth_parent
);
540 old_same
= !!(commit
->object
.flags
& TREESAME
);
541 if (rev_same_tree_as_empty(revs
, commit
))
542 commit
->object
.flags
|= TREESAME
;
544 commit
->object
.flags
&= ~TREESAME
;
548 st
= lookup_decoration(&revs
->treesame
, &commit
->object
);
549 if (!st
|| nth_parent
>= st
->nparents
)
550 die("compact_treesame %u", nth_parent
);
552 old_same
= st
->treesame
[nth_parent
];
553 memmove(st
->treesame
+ nth_parent
,
554 st
->treesame
+ nth_parent
+ 1,
555 st
->nparents
- nth_parent
- 1);
558 * If we've just become a non-merge commit, update TREESAME
559 * immediately, and remove the no-longer-needed decoration.
560 * If still a merge, defer update until update_treesame().
562 if (--st
->nparents
== 1) {
563 if (commit
->parents
->next
)
564 die("compact_treesame parents mismatch");
565 if (st
->treesame
[0] && revs
->dense
)
566 commit
->object
.flags
|= TREESAME
;
568 commit
->object
.flags
&= ~TREESAME
;
569 free(add_decoration(&revs
->treesame
, &commit
->object
, NULL
));
575 static unsigned update_treesame(struct rev_info
*revs
, struct commit
*commit
)
577 if (commit
->parents
&& commit
->parents
->next
) {
579 struct treesame_state
*st
;
580 struct commit_list
*p
;
581 unsigned relevant_parents
;
582 unsigned relevant_change
, irrelevant_change
;
584 st
= lookup_decoration(&revs
->treesame
, &commit
->object
);
586 die("update_treesame %s", oid_to_hex(&commit
->object
.oid
));
587 relevant_parents
= 0;
588 relevant_change
= irrelevant_change
= 0;
589 for (p
= commit
->parents
, n
= 0; p
; n
++, p
= p
->next
) {
590 if (relevant_commit(p
->item
)) {
591 relevant_change
|= !st
->treesame
[n
];
594 irrelevant_change
|= !st
->treesame
[n
];
596 if (relevant_parents
? relevant_change
: irrelevant_change
)
597 commit
->object
.flags
&= ~TREESAME
;
599 commit
->object
.flags
|= TREESAME
;
602 return commit
->object
.flags
& TREESAME
;
605 static inline int limiting_can_increase_treesame(const struct rev_info
*revs
)
608 * TREESAME is irrelevant unless prune && dense;
609 * if simplify_history is set, we can't have a mixture of TREESAME and
610 * !TREESAME INTERESTING parents (and we don't have treesame[]
611 * decoration anyway);
612 * if first_parent_only is set, then the TREESAME flag is locked
613 * against the first parent (and again we lack treesame[] decoration).
615 return revs
->prune
&& revs
->dense
&&
616 !revs
->simplify_history
&&
617 !revs
->first_parent_only
;
620 static void try_to_simplify_commit(struct rev_info
*revs
, struct commit
*commit
)
622 struct commit_list
**pp
, *parent
;
623 struct treesame_state
*ts
= NULL
;
624 int relevant_change
= 0, irrelevant_change
= 0;
625 int relevant_parents
, nth_parent
;
628 * If we don't do pruning, everything is interesting
633 if (!get_commit_tree(commit
))
636 if (!commit
->parents
) {
637 if (rev_same_tree_as_empty(revs
, commit
))
638 commit
->object
.flags
|= TREESAME
;
643 * Normal non-merge commit? If we don't want to make the
644 * history dense, we consider it always to be a change..
646 if (!revs
->dense
&& !commit
->parents
->next
)
649 for (pp
= &commit
->parents
, nth_parent
= 0, relevant_parents
= 0;
650 (parent
= *pp
) != NULL
;
651 pp
= &parent
->next
, nth_parent
++) {
652 struct commit
*p
= parent
->item
;
653 if (relevant_commit(p
))
656 if (nth_parent
== 1) {
658 * This our second loop iteration - so we now know
659 * we're dealing with a merge.
661 * Do not compare with later parents when we care only about
662 * the first parent chain, in order to avoid derailing the
663 * traversal to follow a side branch that brought everything
664 * in the path we are limited to by the pathspec.
666 if (revs
->first_parent_only
)
669 * If this will remain a potentially-simplifiable
670 * merge, remember per-parent treesame if needed.
671 * Initialise the array with the comparison from our
674 if (revs
->treesame
.name
&&
675 !revs
->simplify_history
&&
676 !(commit
->object
.flags
& UNINTERESTING
)) {
677 ts
= initialise_treesame(revs
, commit
);
678 if (!(irrelevant_change
|| relevant_change
))
682 if (parse_commit(p
) < 0)
683 die("cannot simplify commit %s (because of %s)",
684 oid_to_hex(&commit
->object
.oid
),
685 oid_to_hex(&p
->object
.oid
));
686 switch (rev_compare_tree(revs
, p
, commit
)) {
688 if (!revs
->simplify_history
|| !relevant_commit(p
)) {
689 /* Even if a merge with an uninteresting
690 * side branch brought the entire change
691 * we are interested in, we do not want
692 * to lose the other branches of this
693 * merge, so we just keep going.
696 ts
->treesame
[nth_parent
] = 1;
700 commit
->parents
= parent
;
701 commit
->object
.flags
|= TREESAME
;
705 if (revs
->remove_empty_trees
&&
706 rev_same_tree_as_empty(revs
, p
)) {
707 /* We are adding all the specified
708 * paths from this parent, so the
709 * history beyond this parent is not
710 * interesting. Remove its parents
711 * (they are grandparents for us).
712 * IOW, we pretend this parent is a
715 if (parse_commit(p
) < 0)
716 die("cannot simplify commit %s (invalid %s)",
717 oid_to_hex(&commit
->object
.oid
),
718 oid_to_hex(&p
->object
.oid
));
723 case REV_TREE_DIFFERENT
:
724 if (relevant_commit(p
))
727 irrelevant_change
= 1;
730 die("bad tree compare for commit %s", oid_to_hex(&commit
->object
.oid
));
734 * TREESAME is straightforward for single-parent commits. For merge
735 * commits, it is most useful to define it so that "irrelevant"
736 * parents cannot make us !TREESAME - if we have any relevant
737 * parents, then we only consider TREESAMEness with respect to them,
738 * allowing irrelevant merges from uninteresting branches to be
739 * simplified away. Only if we have only irrelevant parents do we
740 * base TREESAME on them. Note that this logic is replicated in
741 * update_treesame, which should be kept in sync.
743 if (relevant_parents
? !relevant_change
: !irrelevant_change
)
744 commit
->object
.flags
|= TREESAME
;
747 static void commit_list_insert_by_date_cached(struct commit
*p
, struct commit_list
**head
,
748 struct commit_list
*cached_base
, struct commit_list
**cache
)
750 struct commit_list
*new_entry
;
752 if (cached_base
&& p
->date
< cached_base
->item
->date
)
753 new_entry
= commit_list_insert_by_date(p
, &cached_base
->next
);
755 new_entry
= commit_list_insert_by_date(p
, head
);
757 if (cache
&& (!*cache
|| p
->date
< (*cache
)->item
->date
))
761 static int add_parents_to_list(struct rev_info
*revs
, struct commit
*commit
,
762 struct commit_list
**list
, struct commit_list
**cache_ptr
)
764 struct commit_list
*parent
= commit
->parents
;
766 struct commit_list
*cached_base
= cache_ptr
? *cache_ptr
: NULL
;
768 if (commit
->object
.flags
& ADDED
)
770 commit
->object
.flags
|= ADDED
;
772 if (revs
->include_check
&&
773 !revs
->include_check(commit
, revs
->include_check_data
))
777 * If the commit is uninteresting, don't try to
778 * prune parents - we want the maximal uninteresting
781 * Normally we haven't parsed the parent
782 * yet, so we won't have a parent of a parent
783 * here. However, it may turn out that we've
784 * reached this commit some other way (where it
785 * wasn't uninteresting), in which case we need
786 * to mark its parents recursively too..
788 if (commit
->object
.flags
& UNINTERESTING
) {
790 struct commit
*p
= parent
->item
;
791 parent
= parent
->next
;
793 p
->object
.flags
|= UNINTERESTING
;
794 if (parse_commit_gently(p
, 1) < 0)
797 mark_parents_uninteresting(p
);
798 if (p
->object
.flags
& SEEN
)
800 p
->object
.flags
|= SEEN
;
801 commit_list_insert_by_date_cached(p
, list
, cached_base
, cache_ptr
);
807 * Ok, the commit wasn't uninteresting. Try to
808 * simplify the commit history and find the parent
809 * that has no differences in the path set if one exists.
811 try_to_simplify_commit(revs
, commit
);
816 left_flag
= (commit
->object
.flags
& SYMMETRIC_LEFT
);
818 for (parent
= commit
->parents
; parent
; parent
= parent
->next
) {
819 struct commit
*p
= parent
->item
;
820 int gently
= revs
->ignore_missing_links
||
821 revs
->exclude_promisor_objects
;
822 if (parse_commit_gently(p
, gently
) < 0) {
823 if (revs
->exclude_promisor_objects
&&
824 is_promisor_object(&p
->object
.oid
)) {
825 if (revs
->first_parent_only
)
831 if (revs
->show_source
&& !p
->util
)
832 p
->util
= commit
->util
;
833 p
->object
.flags
|= left_flag
;
834 if (!(p
->object
.flags
& SEEN
)) {
835 p
->object
.flags
|= SEEN
;
836 commit_list_insert_by_date_cached(p
, list
, cached_base
, cache_ptr
);
838 if (revs
->first_parent_only
)
844 static void cherry_pick_list(struct commit_list
*list
, struct rev_info
*revs
)
846 struct commit_list
*p
;
847 int left_count
= 0, right_count
= 0;
849 struct patch_ids ids
;
850 unsigned cherry_flag
;
852 /* First count the commits on the left and on the right */
853 for (p
= list
; p
; p
= p
->next
) {
854 struct commit
*commit
= p
->item
;
855 unsigned flags
= commit
->object
.flags
;
856 if (flags
& BOUNDARY
)
858 else if (flags
& SYMMETRIC_LEFT
)
864 if (!left_count
|| !right_count
)
867 left_first
= left_count
< right_count
;
868 init_patch_ids(&ids
);
869 ids
.diffopts
.pathspec
= revs
->diffopt
.pathspec
;
871 /* Compute patch-ids for one side */
872 for (p
= list
; p
; p
= p
->next
) {
873 struct commit
*commit
= p
->item
;
874 unsigned flags
= commit
->object
.flags
;
876 if (flags
& BOUNDARY
)
879 * If we have fewer left, left_first is set and we omit
880 * commits on the right branch in this loop. If we have
881 * fewer right, we skip the left ones.
883 if (left_first
!= !!(flags
& SYMMETRIC_LEFT
))
885 add_commit_patch_id(commit
, &ids
);
888 /* either cherry_mark or cherry_pick are true */
889 cherry_flag
= revs
->cherry_mark
? PATCHSAME
: SHOWN
;
891 /* Check the other side */
892 for (p
= list
; p
; p
= p
->next
) {
893 struct commit
*commit
= p
->item
;
895 unsigned flags
= commit
->object
.flags
;
897 if (flags
& BOUNDARY
)
900 * If we have fewer left, left_first is set and we omit
901 * commits on the left branch in this loop.
903 if (left_first
== !!(flags
& SYMMETRIC_LEFT
))
907 * Have we seen the same patch id?
909 id
= has_commit_patch_id(commit
, &ids
);
913 commit
->object
.flags
|= cherry_flag
;
914 id
->commit
->object
.flags
|= cherry_flag
;
917 free_patch_ids(&ids
);
920 /* How many extra uninteresting commits we want to see.. */
923 static int still_interesting(struct commit_list
*src
, timestamp_t date
, int slop
,
924 struct commit
**interesting_cache
)
927 * No source list at all? We're definitely done..
933 * Does the destination list contain entries with a date
934 * before the source list? Definitely _not_ done.
936 if (date
<= src
->item
->date
)
940 * Does the source list still have interesting commits in
941 * it? Definitely not done..
943 if (!everybody_uninteresting(src
, interesting_cache
))
946 /* Ok, we're closing in.. */
951 * "rev-list --ancestry-path A..B" computes commits that are ancestors
952 * of B but not ancestors of A but further limits the result to those
953 * that are descendants of A. This takes the list of bottom commits and
954 * the result of "A..B" without --ancestry-path, and limits the latter
955 * further to the ones that can reach one of the commits in "bottom".
957 static void limit_to_ancestry(struct commit_list
*bottom
, struct commit_list
*list
)
959 struct commit_list
*p
;
960 struct commit_list
*rlist
= NULL
;
964 * Reverse the list so that it will be likely that we would
965 * process parents before children.
967 for (p
= list
; p
; p
= p
->next
)
968 commit_list_insert(p
->item
, &rlist
);
970 for (p
= bottom
; p
; p
= p
->next
)
971 p
->item
->object
.flags
|= TMP_MARK
;
974 * Mark the ones that can reach bottom commits in "list",
975 * in a bottom-up fashion.
979 for (p
= rlist
; p
; p
= p
->next
) {
980 struct commit
*c
= p
->item
;
981 struct commit_list
*parents
;
982 if (c
->object
.flags
& (TMP_MARK
| UNINTERESTING
))
984 for (parents
= c
->parents
;
986 parents
= parents
->next
) {
987 if (!(parents
->item
->object
.flags
& TMP_MARK
))
989 c
->object
.flags
|= TMP_MARK
;
994 } while (made_progress
);
997 * NEEDSWORK: decide if we want to remove parents that are
998 * not marked with TMP_MARK from commit->parents for commits
999 * in the resulting list. We may not want to do that, though.
1003 * The ones that are not marked with TMP_MARK are uninteresting
1005 for (p
= list
; p
; p
= p
->next
) {
1006 struct commit
*c
= p
->item
;
1007 if (c
->object
.flags
& TMP_MARK
)
1009 c
->object
.flags
|= UNINTERESTING
;
1012 /* We are done with the TMP_MARK */
1013 for (p
= list
; p
; p
= p
->next
)
1014 p
->item
->object
.flags
&= ~TMP_MARK
;
1015 for (p
= bottom
; p
; p
= p
->next
)
1016 p
->item
->object
.flags
&= ~TMP_MARK
;
1017 free_commit_list(rlist
);
1021 * Before walking the history, keep the set of "negative" refs the
1022 * caller has asked to exclude.
1024 * This is used to compute "rev-list --ancestry-path A..B", as we need
1025 * to filter the result of "A..B" further to the ones that can actually
1028 static struct commit_list
*collect_bottom_commits(struct commit_list
*list
)
1030 struct commit_list
*elem
, *bottom
= NULL
;
1031 for (elem
= list
; elem
; elem
= elem
->next
)
1032 if (elem
->item
->object
.flags
& BOTTOM
)
1033 commit_list_insert(elem
->item
, &bottom
);
1037 /* Assumes either left_only or right_only is set */
1038 static void limit_left_right(struct commit_list
*list
, struct rev_info
*revs
)
1040 struct commit_list
*p
;
1042 for (p
= list
; p
; p
= p
->next
) {
1043 struct commit
*commit
= p
->item
;
1045 if (revs
->right_only
) {
1046 if (commit
->object
.flags
& SYMMETRIC_LEFT
)
1047 commit
->object
.flags
|= SHOWN
;
1048 } else /* revs->left_only is set */
1049 if (!(commit
->object
.flags
& SYMMETRIC_LEFT
))
1050 commit
->object
.flags
|= SHOWN
;
1054 static int limit_list(struct rev_info
*revs
)
1057 timestamp_t date
= TIME_MAX
;
1058 struct commit_list
*list
= revs
->commits
;
1059 struct commit_list
*newlist
= NULL
;
1060 struct commit_list
**p
= &newlist
;
1061 struct commit_list
*bottom
= NULL
;
1062 struct commit
*interesting_cache
= NULL
;
1064 if (revs
->ancestry_path
) {
1065 bottom
= collect_bottom_commits(list
);
1067 die("--ancestry-path given but there are no bottom commits");
1071 struct commit
*commit
= pop_commit(&list
);
1072 struct object
*obj
= &commit
->object
;
1073 show_early_output_fn_t show
;
1075 if (commit
== interesting_cache
)
1076 interesting_cache
= NULL
;
1078 if (revs
->max_age
!= -1 && (commit
->date
< revs
->max_age
))
1079 obj
->flags
|= UNINTERESTING
;
1080 if (add_parents_to_list(revs
, commit
, &list
, NULL
) < 0)
1082 if (obj
->flags
& UNINTERESTING
) {
1083 mark_parents_uninteresting(commit
);
1084 slop
= still_interesting(list
, date
, slop
, &interesting_cache
);
1089 if (revs
->min_age
!= -1 && (commit
->date
> revs
->min_age
))
1091 date
= commit
->date
;
1092 p
= &commit_list_insert(commit
, p
)->next
;
1094 show
= show_early_output
;
1098 show(revs
, newlist
);
1099 show_early_output
= NULL
;
1101 if (revs
->cherry_pick
|| revs
->cherry_mark
)
1102 cherry_pick_list(newlist
, revs
);
1104 if (revs
->left_only
|| revs
->right_only
)
1105 limit_left_right(newlist
, revs
);
1108 limit_to_ancestry(bottom
, newlist
);
1109 free_commit_list(bottom
);
1113 * Check if any commits have become TREESAME by some of their parents
1114 * becoming UNINTERESTING.
1116 if (limiting_can_increase_treesame(revs
))
1117 for (list
= newlist
; list
; list
= list
->next
) {
1118 struct commit
*c
= list
->item
;
1119 if (c
->object
.flags
& (UNINTERESTING
| TREESAME
))
1121 update_treesame(revs
, c
);
1124 revs
->commits
= newlist
;
1129 * Add an entry to refs->cmdline with the specified information.
1132 static void add_rev_cmdline(struct rev_info
*revs
,
1133 struct object
*item
,
1138 struct rev_cmdline_info
*info
= &revs
->cmdline
;
1139 unsigned int nr
= info
->nr
;
1141 ALLOC_GROW(info
->rev
, nr
+ 1, info
->alloc
);
1142 info
->rev
[nr
].item
= item
;
1143 info
->rev
[nr
].name
= xstrdup(name
);
1144 info
->rev
[nr
].whence
= whence
;
1145 info
->rev
[nr
].flags
= flags
;
1149 static void add_rev_cmdline_list(struct rev_info
*revs
,
1150 struct commit_list
*commit_list
,
1154 while (commit_list
) {
1155 struct object
*object
= &commit_list
->item
->object
;
1156 add_rev_cmdline(revs
, object
, oid_to_hex(&object
->oid
),
1158 commit_list
= commit_list
->next
;
1162 struct all_refs_cb
{
1164 int warned_bad_reflog
;
1165 struct rev_info
*all_revs
;
1166 const char *name_for_errormsg
;
1167 struct ref_store
*refs
;
1170 int ref_excluded(struct string_list
*ref_excludes
, const char *path
)
1172 struct string_list_item
*item
;
1176 for_each_string_list_item(item
, ref_excludes
) {
1177 if (!wildmatch(item
->string
, path
, 0))
1183 static int handle_one_ref(const char *path
, const struct object_id
*oid
,
1184 int flag
, void *cb_data
)
1186 struct all_refs_cb
*cb
= cb_data
;
1187 struct object
*object
;
1189 if (ref_excluded(cb
->all_revs
->ref_excludes
, path
))
1192 object
= get_reference(cb
->all_revs
, path
, oid
, cb
->all_flags
);
1193 add_rev_cmdline(cb
->all_revs
, object
, path
, REV_CMD_REF
, cb
->all_flags
);
1194 add_pending_oid(cb
->all_revs
, path
, oid
, cb
->all_flags
);
1198 static void init_all_refs_cb(struct all_refs_cb
*cb
, struct rev_info
*revs
,
1201 cb
->all_revs
= revs
;
1202 cb
->all_flags
= flags
;
1203 revs
->rev_input_given
= 1;
1207 void clear_ref_exclusion(struct string_list
**ref_excludes_p
)
1209 if (*ref_excludes_p
) {
1210 string_list_clear(*ref_excludes_p
, 0);
1211 free(*ref_excludes_p
);
1213 *ref_excludes_p
= NULL
;
1216 void add_ref_exclusion(struct string_list
**ref_excludes_p
, const char *exclude
)
1218 if (!*ref_excludes_p
) {
1219 *ref_excludes_p
= xcalloc(1, sizeof(**ref_excludes_p
));
1220 (*ref_excludes_p
)->strdup_strings
= 1;
1222 string_list_append(*ref_excludes_p
, exclude
);
1225 static void handle_refs(struct ref_store
*refs
,
1226 struct rev_info
*revs
, unsigned flags
,
1227 int (*for_each
)(struct ref_store
*, each_ref_fn
, void *))
1229 struct all_refs_cb cb
;
1232 /* this could happen with uninitialized submodules */
1236 init_all_refs_cb(&cb
, revs
, flags
);
1237 for_each(refs
, handle_one_ref
, &cb
);
1240 static void handle_one_reflog_commit(struct object_id
*oid
, void *cb_data
)
1242 struct all_refs_cb
*cb
= cb_data
;
1243 if (!is_null_oid(oid
)) {
1244 struct object
*o
= parse_object(oid
);
1246 o
->flags
|= cb
->all_flags
;
1247 /* ??? CMDLINEFLAGS ??? */
1248 add_pending_object(cb
->all_revs
, o
, "");
1250 else if (!cb
->warned_bad_reflog
) {
1251 warning("reflog of '%s' references pruned commits",
1252 cb
->name_for_errormsg
);
1253 cb
->warned_bad_reflog
= 1;
1258 static int handle_one_reflog_ent(struct object_id
*ooid
, struct object_id
*noid
,
1259 const char *email
, timestamp_t timestamp
, int tz
,
1260 const char *message
, void *cb_data
)
1262 handle_one_reflog_commit(ooid
, cb_data
);
1263 handle_one_reflog_commit(noid
, cb_data
);
1267 static int handle_one_reflog(const char *path
, const struct object_id
*oid
,
1268 int flag
, void *cb_data
)
1270 struct all_refs_cb
*cb
= cb_data
;
1271 cb
->warned_bad_reflog
= 0;
1272 cb
->name_for_errormsg
= path
;
1273 refs_for_each_reflog_ent(cb
->refs
, path
,
1274 handle_one_reflog_ent
, cb_data
);
1278 static void add_other_reflogs_to_pending(struct all_refs_cb
*cb
)
1280 struct worktree
**worktrees
, **p
;
1282 worktrees
= get_worktrees(0);
1283 for (p
= worktrees
; *p
; p
++) {
1284 struct worktree
*wt
= *p
;
1289 cb
->refs
= get_worktree_ref_store(wt
);
1290 refs_for_each_reflog(cb
->refs
,
1294 free_worktrees(worktrees
);
1297 void add_reflogs_to_pending(struct rev_info
*revs
, unsigned flags
)
1299 struct all_refs_cb cb
;
1302 cb
.all_flags
= flags
;
1303 cb
.refs
= get_main_ref_store(the_repository
);
1304 for_each_reflog(handle_one_reflog
, &cb
);
1306 if (!revs
->single_worktree
)
1307 add_other_reflogs_to_pending(&cb
);
1310 static void add_cache_tree(struct cache_tree
*it
, struct rev_info
*revs
,
1311 struct strbuf
*path
)
1313 size_t baselen
= path
->len
;
1316 if (it
->entry_count
>= 0) {
1317 struct tree
*tree
= lookup_tree(&it
->oid
);
1318 add_pending_object_with_path(revs
, &tree
->object
, "",
1322 for (i
= 0; i
< it
->subtree_nr
; i
++) {
1323 struct cache_tree_sub
*sub
= it
->down
[i
];
1324 strbuf_addf(path
, "%s%s", baselen
? "/" : "", sub
->name
);
1325 add_cache_tree(sub
->cache_tree
, revs
, path
);
1326 strbuf_setlen(path
, baselen
);
1331 static void do_add_index_objects_to_pending(struct rev_info
*revs
,
1332 struct index_state
*istate
)
1336 for (i
= 0; i
< istate
->cache_nr
; i
++) {
1337 struct cache_entry
*ce
= istate
->cache
[i
];
1340 if (S_ISGITLINK(ce
->ce_mode
))
1343 blob
= lookup_blob(&ce
->oid
);
1345 die("unable to add index blob to traversal");
1346 add_pending_object_with_path(revs
, &blob
->object
, "",
1347 ce
->ce_mode
, ce
->name
);
1350 if (istate
->cache_tree
) {
1351 struct strbuf path
= STRBUF_INIT
;
1352 add_cache_tree(istate
->cache_tree
, revs
, &path
);
1353 strbuf_release(&path
);
1357 void add_index_objects_to_pending(struct rev_info
*revs
, unsigned int flags
)
1359 struct worktree
**worktrees
, **p
;
1362 do_add_index_objects_to_pending(revs
, &the_index
);
1364 if (revs
->single_worktree
)
1367 worktrees
= get_worktrees(0);
1368 for (p
= worktrees
; *p
; p
++) {
1369 struct worktree
*wt
= *p
;
1370 struct index_state istate
= { NULL
};
1373 continue; /* current index already taken care of */
1375 if (read_index_from(&istate
,
1376 worktree_git_path(wt
, "index"),
1377 get_worktree_git_dir(wt
)) > 0)
1378 do_add_index_objects_to_pending(revs
, &istate
);
1379 discard_index(&istate
);
1381 free_worktrees(worktrees
);
1384 static int add_parents_only(struct rev_info
*revs
, const char *arg_
, int flags
,
1387 struct object_id oid
;
1389 struct commit
*commit
;
1390 struct commit_list
*parents
;
1392 const char *arg
= arg_
;
1395 flags
^= UNINTERESTING
| BOTTOM
;
1398 if (get_oid_committish(arg
, &oid
))
1401 it
= get_reference(revs
, arg
, &oid
, 0);
1402 if (!it
&& revs
->ignore_missing
)
1404 if (it
->type
!= OBJ_TAG
)
1406 if (!((struct tag
*)it
)->tagged
)
1408 oidcpy(&oid
, &((struct tag
*)it
)->tagged
->oid
);
1410 if (it
->type
!= OBJ_COMMIT
)
1412 commit
= (struct commit
*)it
;
1413 if (exclude_parent
&&
1414 exclude_parent
> commit_list_count(commit
->parents
))
1416 for (parents
= commit
->parents
, parent_number
= 1;
1418 parents
= parents
->next
, parent_number
++) {
1419 if (exclude_parent
&& parent_number
!= exclude_parent
)
1422 it
= &parents
->item
->object
;
1424 add_rev_cmdline(revs
, it
, arg_
, REV_CMD_PARENTS_ONLY
, flags
);
1425 add_pending_object(revs
, it
, arg
);
1430 void init_revisions(struct rev_info
*revs
, const char *prefix
)
1432 memset(revs
, 0, sizeof(*revs
));
1434 revs
->abbrev
= DEFAULT_ABBREV
;
1435 revs
->ignore_merges
= 1;
1436 revs
->simplify_history
= 1;
1437 revs
->pruning
.flags
.recursive
= 1;
1438 revs
->pruning
.flags
.quick
= 1;
1439 revs
->pruning
.add_remove
= file_add_remove
;
1440 revs
->pruning
.change
= file_change
;
1441 revs
->pruning
.change_fn_data
= revs
;
1442 revs
->sort_order
= REV_SORT_IN_GRAPH_ORDER
;
1444 revs
->prefix
= prefix
;
1447 revs
->skip_count
= -1;
1448 revs
->max_count
= -1;
1449 revs
->max_parents
= -1;
1450 revs
->expand_tabs_in_log
= -1;
1452 revs
->commit_format
= CMIT_FMT_DEFAULT
;
1453 revs
->expand_tabs_in_log_default
= 8;
1455 init_grep_defaults();
1456 grep_init(&revs
->grep_filter
, prefix
);
1457 revs
->grep_filter
.status_only
= 1;
1459 diff_setup(&revs
->diffopt
);
1460 if (prefix
&& !revs
->diffopt
.prefix
) {
1461 revs
->diffopt
.prefix
= prefix
;
1462 revs
->diffopt
.prefix_length
= strlen(prefix
);
1465 revs
->notes_opt
.use_default_notes
= -1;
1468 static void add_pending_commit_list(struct rev_info
*revs
,
1469 struct commit_list
*commit_list
,
1472 while (commit_list
) {
1473 struct object
*object
= &commit_list
->item
->object
;
1474 object
->flags
|= flags
;
1475 add_pending_object(revs
, object
, oid_to_hex(&object
->oid
));
1476 commit_list
= commit_list
->next
;
1480 static void prepare_show_merge(struct rev_info
*revs
)
1482 struct commit_list
*bases
;
1483 struct commit
*head
, *other
;
1484 struct object_id oid
;
1485 const char **prune
= NULL
;
1486 int i
, prune_num
= 1; /* counting terminating NULL */
1488 if (get_oid("HEAD", &oid
))
1489 die("--merge without HEAD?");
1490 head
= lookup_commit_or_die(&oid
, "HEAD");
1491 if (get_oid("MERGE_HEAD", &oid
))
1492 die("--merge without MERGE_HEAD?");
1493 other
= lookup_commit_or_die(&oid
, "MERGE_HEAD");
1494 add_pending_object(revs
, &head
->object
, "HEAD");
1495 add_pending_object(revs
, &other
->object
, "MERGE_HEAD");
1496 bases
= get_merge_bases(head
, other
);
1497 add_rev_cmdline_list(revs
, bases
, REV_CMD_MERGE_BASE
, UNINTERESTING
| BOTTOM
);
1498 add_pending_commit_list(revs
, bases
, UNINTERESTING
| BOTTOM
);
1499 free_commit_list(bases
);
1500 head
->object
.flags
|= SYMMETRIC_LEFT
;
1504 for (i
= 0; i
< active_nr
; i
++) {
1505 const struct cache_entry
*ce
= active_cache
[i
];
1508 if (ce_path_match(ce
, &revs
->prune_data
, NULL
)) {
1510 REALLOC_ARRAY(prune
, prune_num
);
1511 prune
[prune_num
-2] = ce
->name
;
1512 prune
[prune_num
-1] = NULL
;
1514 while ((i
+1 < active_nr
) &&
1515 ce_same_name(ce
, active_cache
[i
+1]))
1518 clear_pathspec(&revs
->prune_data
);
1519 parse_pathspec(&revs
->prune_data
, PATHSPEC_ALL_MAGIC
& ~PATHSPEC_LITERAL
,
1520 PATHSPEC_PREFER_FULL
| PATHSPEC_LITERAL_PATH
, "", prune
);
1524 static int dotdot_missing(const char *arg
, char *dotdot
,
1525 struct rev_info
*revs
, int symmetric
)
1527 if (revs
->ignore_missing
)
1529 /* de-munge so we report the full argument */
1532 ? "Invalid symmetric difference expression %s"
1533 : "Invalid revision range %s", arg
);
1536 static int handle_dotdot_1(const char *arg
, char *dotdot
,
1537 struct rev_info
*revs
, int flags
,
1538 int cant_be_filename
,
1539 struct object_context
*a_oc
,
1540 struct object_context
*b_oc
)
1542 const char *a_name
, *b_name
;
1543 struct object_id a_oid
, b_oid
;
1544 struct object
*a_obj
, *b_obj
;
1545 unsigned int a_flags
, b_flags
;
1547 unsigned int flags_exclude
= flags
^ (UNINTERESTING
| BOTTOM
);
1548 unsigned int oc_flags
= GET_OID_COMMITTISH
| GET_OID_RECORD_PATH
;
1554 b_name
= dotdot
+ 2;
1555 if (*b_name
== '.') {
1562 if (get_oid_with_context(a_name
, oc_flags
, &a_oid
, a_oc
) ||
1563 get_oid_with_context(b_name
, oc_flags
, &b_oid
, b_oc
))
1566 if (!cant_be_filename
) {
1568 verify_non_filename(revs
->prefix
, arg
);
1572 a_obj
= parse_object(&a_oid
);
1573 b_obj
= parse_object(&b_oid
);
1574 if (!a_obj
|| !b_obj
)
1575 return dotdot_missing(arg
, dotdot
, revs
, symmetric
);
1580 a_flags
= flags_exclude
;
1582 /* A...B -- find merge bases between the two */
1583 struct commit
*a
, *b
;
1584 struct commit_list
*exclude
;
1586 a
= lookup_commit_reference(&a_obj
->oid
);
1587 b
= lookup_commit_reference(&b_obj
->oid
);
1589 return dotdot_missing(arg
, dotdot
, revs
, symmetric
);
1591 exclude
= get_merge_bases(a
, b
);
1592 add_rev_cmdline_list(revs
, exclude
, REV_CMD_MERGE_BASE
,
1594 add_pending_commit_list(revs
, exclude
, flags_exclude
);
1595 free_commit_list(exclude
);
1598 a_flags
= flags
| SYMMETRIC_LEFT
;
1601 a_obj
->flags
|= a_flags
;
1602 b_obj
->flags
|= b_flags
;
1603 add_rev_cmdline(revs
, a_obj
, a_name
, REV_CMD_LEFT
, a_flags
);
1604 add_rev_cmdline(revs
, b_obj
, b_name
, REV_CMD_RIGHT
, b_flags
);
1605 add_pending_object_with_path(revs
, a_obj
, a_name
, a_oc
->mode
, a_oc
->path
);
1606 add_pending_object_with_path(revs
, b_obj
, b_name
, b_oc
->mode
, b_oc
->path
);
1610 static int handle_dotdot(const char *arg
,
1611 struct rev_info
*revs
, int flags
,
1612 int cant_be_filename
)
1614 struct object_context a_oc
, b_oc
;
1615 char *dotdot
= strstr(arg
, "..");
1621 memset(&a_oc
, 0, sizeof(a_oc
));
1622 memset(&b_oc
, 0, sizeof(b_oc
));
1625 ret
= handle_dotdot_1(arg
, dotdot
, revs
, flags
, cant_be_filename
,
1635 int handle_revision_arg(const char *arg_
, struct rev_info
*revs
, int flags
, unsigned revarg_opt
)
1637 struct object_context oc
;
1639 struct object
*object
;
1640 struct object_id oid
;
1642 const char *arg
= arg_
;
1643 int cant_be_filename
= revarg_opt
& REVARG_CANNOT_BE_FILENAME
;
1644 unsigned get_sha1_flags
= GET_OID_RECORD_PATH
;
1646 flags
= flags
& UNINTERESTING
? flags
| BOTTOM
: flags
& ~BOTTOM
;
1648 if (!cant_be_filename
&& !strcmp(arg
, "..")) {
1650 * Just ".."? That is not a range but the
1651 * pathspec for the parent directory.
1656 if (!handle_dotdot(arg
, revs
, flags
, revarg_opt
))
1659 mark
= strstr(arg
, "^@");
1660 if (mark
&& !mark
[2]) {
1662 if (add_parents_only(revs
, arg
, flags
, 0))
1666 mark
= strstr(arg
, "^!");
1667 if (mark
&& !mark
[2]) {
1669 if (!add_parents_only(revs
, arg
, flags
^ (UNINTERESTING
| BOTTOM
), 0))
1672 mark
= strstr(arg
, "^-");
1674 int exclude_parent
= 1;
1678 exclude_parent
= strtoul(mark
+ 2, &end
, 10);
1679 if (*end
!= '\0' || !exclude_parent
)
1684 if (!add_parents_only(revs
, arg
, flags
^ (UNINTERESTING
| BOTTOM
), exclude_parent
))
1690 local_flags
= UNINTERESTING
| BOTTOM
;
1694 if (revarg_opt
& REVARG_COMMITTISH
)
1695 get_sha1_flags
|= GET_OID_COMMITTISH
;
1697 if (get_oid_with_context(arg
, get_sha1_flags
, &oid
, &oc
))
1698 return revs
->ignore_missing
? 0 : -1;
1699 if (!cant_be_filename
)
1700 verify_non_filename(revs
->prefix
, arg
);
1701 object
= get_reference(revs
, arg
, &oid
, flags
^ local_flags
);
1702 add_rev_cmdline(revs
, object
, arg_
, REV_CMD_REV
, flags
^ local_flags
);
1703 add_pending_object_with_path(revs
, object
, arg
, oc
.mode
, oc
.path
);
1708 static void read_pathspec_from_stdin(struct rev_info
*revs
, struct strbuf
*sb
,
1709 struct argv_array
*prune
)
1711 while (strbuf_getline(sb
, stdin
) != EOF
)
1712 argv_array_push(prune
, sb
->buf
);
1715 static void read_revisions_from_stdin(struct rev_info
*revs
,
1716 struct argv_array
*prune
)
1719 int seen_dashdash
= 0;
1722 save_warning
= warn_on_object_refname_ambiguity
;
1723 warn_on_object_refname_ambiguity
= 0;
1725 strbuf_init(&sb
, 1000);
1726 while (strbuf_getline(&sb
, stdin
) != EOF
) {
1730 if (sb
.buf
[0] == '-') {
1731 if (len
== 2 && sb
.buf
[1] == '-') {
1735 die("options not supported in --stdin mode");
1737 if (handle_revision_arg(sb
.buf
, revs
, 0,
1738 REVARG_CANNOT_BE_FILENAME
))
1739 die("bad revision '%s'", sb
.buf
);
1742 read_pathspec_from_stdin(revs
, &sb
, prune
);
1744 strbuf_release(&sb
);
1745 warn_on_object_refname_ambiguity
= save_warning
;
1748 static void add_grep(struct rev_info
*revs
, const char *ptn
, enum grep_pat_token what
)
1750 append_grep_pattern(&revs
->grep_filter
, ptn
, "command line", 0, what
);
1753 static void add_header_grep(struct rev_info
*revs
, enum grep_header_field field
, const char *pattern
)
1755 append_header_grep_pattern(&revs
->grep_filter
, field
, pattern
);
1758 static void add_message_grep(struct rev_info
*revs
, const char *pattern
)
1760 add_grep(revs
, pattern
, GREP_PATTERN_BODY
);
1763 static int handle_revision_opt(struct rev_info
*revs
, int argc
, const char **argv
,
1764 int *unkc
, const char **unkv
)
1766 const char *arg
= argv
[0];
1769 const unsigned hexsz
= the_hash_algo
->hexsz
;
1771 /* pseudo revision arguments */
1772 if (!strcmp(arg
, "--all") || !strcmp(arg
, "--branches") ||
1773 !strcmp(arg
, "--tags") || !strcmp(arg
, "--remotes") ||
1774 !strcmp(arg
, "--reflog") || !strcmp(arg
, "--not") ||
1775 !strcmp(arg
, "--no-walk") || !strcmp(arg
, "--do-walk") ||
1776 !strcmp(arg
, "--bisect") || starts_with(arg
, "--glob=") ||
1777 !strcmp(arg
, "--indexed-objects") ||
1778 starts_with(arg
, "--exclude=") ||
1779 starts_with(arg
, "--branches=") || starts_with(arg
, "--tags=") ||
1780 starts_with(arg
, "--remotes=") || starts_with(arg
, "--no-walk="))
1782 unkv
[(*unkc
)++] = arg
;
1786 if ((argcount
= parse_long_opt("max-count", argv
, &optarg
))) {
1787 revs
->max_count
= atoi(optarg
);
1790 } else if ((argcount
= parse_long_opt("skip", argv
, &optarg
))) {
1791 revs
->skip_count
= atoi(optarg
);
1793 } else if ((*arg
== '-') && isdigit(arg
[1])) {
1794 /* accept -<digit>, like traditional "head" */
1795 if (strtol_i(arg
+ 1, 10, &revs
->max_count
) < 0 ||
1796 revs
->max_count
< 0)
1797 die("'%s': not a non-negative integer", arg
+ 1);
1799 } else if (!strcmp(arg
, "-n")) {
1801 return error("-n requires an argument");
1802 revs
->max_count
= atoi(argv
[1]);
1805 } else if (skip_prefix(arg
, "-n", &optarg
)) {
1806 revs
->max_count
= atoi(optarg
);
1808 } else if ((argcount
= parse_long_opt("max-age", argv
, &optarg
))) {
1809 revs
->max_age
= atoi(optarg
);
1811 } else if ((argcount
= parse_long_opt("since", argv
, &optarg
))) {
1812 revs
->max_age
= approxidate(optarg
);
1814 } else if ((argcount
= parse_long_opt("after", argv
, &optarg
))) {
1815 revs
->max_age
= approxidate(optarg
);
1817 } else if ((argcount
= parse_long_opt("min-age", argv
, &optarg
))) {
1818 revs
->min_age
= atoi(optarg
);
1820 } else if ((argcount
= parse_long_opt("before", argv
, &optarg
))) {
1821 revs
->min_age
= approxidate(optarg
);
1823 } else if ((argcount
= parse_long_opt("until", argv
, &optarg
))) {
1824 revs
->min_age
= approxidate(optarg
);
1826 } else if (!strcmp(arg
, "--first-parent")) {
1827 revs
->first_parent_only
= 1;
1828 } else if (!strcmp(arg
, "--ancestry-path")) {
1829 revs
->ancestry_path
= 1;
1830 revs
->simplify_history
= 0;
1832 } else if (!strcmp(arg
, "-g") || !strcmp(arg
, "--walk-reflogs")) {
1833 init_reflog_walk(&revs
->reflog_info
);
1834 } else if (!strcmp(arg
, "--default")) {
1836 return error("bad --default argument");
1837 revs
->def
= argv
[1];
1839 } else if (!strcmp(arg
, "--merge")) {
1840 revs
->show_merge
= 1;
1841 } else if (!strcmp(arg
, "--topo-order")) {
1842 revs
->sort_order
= REV_SORT_IN_GRAPH_ORDER
;
1843 revs
->topo_order
= 1;
1844 } else if (!strcmp(arg
, "--simplify-merges")) {
1845 revs
->simplify_merges
= 1;
1846 revs
->topo_order
= 1;
1847 revs
->rewrite_parents
= 1;
1848 revs
->simplify_history
= 0;
1850 } else if (!strcmp(arg
, "--simplify-by-decoration")) {
1851 revs
->simplify_merges
= 1;
1852 revs
->topo_order
= 1;
1853 revs
->rewrite_parents
= 1;
1854 revs
->simplify_history
= 0;
1855 revs
->simplify_by_decoration
= 1;
1858 load_ref_decorations(NULL
, DECORATE_SHORT_REFS
);
1859 } else if (!strcmp(arg
, "--date-order")) {
1860 revs
->sort_order
= REV_SORT_BY_COMMIT_DATE
;
1861 revs
->topo_order
= 1;
1862 } else if (!strcmp(arg
, "--author-date-order")) {
1863 revs
->sort_order
= REV_SORT_BY_AUTHOR_DATE
;
1864 revs
->topo_order
= 1;
1865 } else if (!strcmp(arg
, "--early-output")) {
1866 revs
->early_output
= 100;
1867 revs
->topo_order
= 1;
1868 } else if (skip_prefix(arg
, "--early-output=", &optarg
)) {
1869 if (strtoul_ui(optarg
, 10, &revs
->early_output
) < 0)
1870 die("'%s': not a non-negative integer", optarg
);
1871 revs
->topo_order
= 1;
1872 } else if (!strcmp(arg
, "--parents")) {
1873 revs
->rewrite_parents
= 1;
1874 revs
->print_parents
= 1;
1875 } else if (!strcmp(arg
, "--dense")) {
1877 } else if (!strcmp(arg
, "--sparse")) {
1879 } else if (!strcmp(arg
, "--in-commit-order")) {
1880 revs
->tree_blobs_in_commit_order
= 1;
1881 } else if (!strcmp(arg
, "--remove-empty")) {
1882 revs
->remove_empty_trees
= 1;
1883 } else if (!strcmp(arg
, "--merges")) {
1884 revs
->min_parents
= 2;
1885 } else if (!strcmp(arg
, "--no-merges")) {
1886 revs
->max_parents
= 1;
1887 } else if (skip_prefix(arg
, "--min-parents=", &optarg
)) {
1888 revs
->min_parents
= atoi(optarg
);
1889 } else if (!strcmp(arg
, "--no-min-parents")) {
1890 revs
->min_parents
= 0;
1891 } else if (skip_prefix(arg
, "--max-parents=", &optarg
)) {
1892 revs
->max_parents
= atoi(optarg
);
1893 } else if (!strcmp(arg
, "--no-max-parents")) {
1894 revs
->max_parents
= -1;
1895 } else if (!strcmp(arg
, "--boundary")) {
1897 } else if (!strcmp(arg
, "--left-right")) {
1898 revs
->left_right
= 1;
1899 } else if (!strcmp(arg
, "--left-only")) {
1900 if (revs
->right_only
)
1901 die("--left-only is incompatible with --right-only"
1903 revs
->left_only
= 1;
1904 } else if (!strcmp(arg
, "--right-only")) {
1905 if (revs
->left_only
)
1906 die("--right-only is incompatible with --left-only");
1907 revs
->right_only
= 1;
1908 } else if (!strcmp(arg
, "--cherry")) {
1909 if (revs
->left_only
)
1910 die("--cherry is incompatible with --left-only");
1911 revs
->cherry_mark
= 1;
1912 revs
->right_only
= 1;
1913 revs
->max_parents
= 1;
1915 } else if (!strcmp(arg
, "--count")) {
1917 } else if (!strcmp(arg
, "--cherry-mark")) {
1918 if (revs
->cherry_pick
)
1919 die("--cherry-mark is incompatible with --cherry-pick");
1920 revs
->cherry_mark
= 1;
1921 revs
->limited
= 1; /* needs limit_list() */
1922 } else if (!strcmp(arg
, "--cherry-pick")) {
1923 if (revs
->cherry_mark
)
1924 die("--cherry-pick is incompatible with --cherry-mark");
1925 revs
->cherry_pick
= 1;
1927 } else if (!strcmp(arg
, "--objects")) {
1928 revs
->tag_objects
= 1;
1929 revs
->tree_objects
= 1;
1930 revs
->blob_objects
= 1;
1931 } else if (!strcmp(arg
, "--objects-edge")) {
1932 revs
->tag_objects
= 1;
1933 revs
->tree_objects
= 1;
1934 revs
->blob_objects
= 1;
1935 revs
->edge_hint
= 1;
1936 } else if (!strcmp(arg
, "--objects-edge-aggressive")) {
1937 revs
->tag_objects
= 1;
1938 revs
->tree_objects
= 1;
1939 revs
->blob_objects
= 1;
1940 revs
->edge_hint
= 1;
1941 revs
->edge_hint_aggressive
= 1;
1942 } else if (!strcmp(arg
, "--verify-objects")) {
1943 revs
->tag_objects
= 1;
1944 revs
->tree_objects
= 1;
1945 revs
->blob_objects
= 1;
1946 revs
->verify_objects
= 1;
1947 } else if (!strcmp(arg
, "--unpacked")) {
1949 } else if (starts_with(arg
, "--unpacked=")) {
1950 die("--unpacked=<packfile> no longer supported.");
1951 } else if (!strcmp(arg
, "-r")) {
1953 revs
->diffopt
.flags
.recursive
= 1;
1954 } else if (!strcmp(arg
, "-t")) {
1956 revs
->diffopt
.flags
.recursive
= 1;
1957 revs
->diffopt
.flags
.tree_in_recursive
= 1;
1958 } else if (!strcmp(arg
, "-m")) {
1959 revs
->ignore_merges
= 0;
1960 } else if (!strcmp(arg
, "-c")) {
1962 revs
->dense_combined_merges
= 0;
1963 revs
->combine_merges
= 1;
1964 } else if (!strcmp(arg
, "--cc")) {
1966 revs
->dense_combined_merges
= 1;
1967 revs
->combine_merges
= 1;
1968 } else if (!strcmp(arg
, "-v")) {
1969 revs
->verbose_header
= 1;
1970 } else if (!strcmp(arg
, "--pretty")) {
1971 revs
->verbose_header
= 1;
1972 revs
->pretty_given
= 1;
1973 get_commit_format(NULL
, revs
);
1974 } else if (skip_prefix(arg
, "--pretty=", &optarg
) ||
1975 skip_prefix(arg
, "--format=", &optarg
)) {
1977 * Detached form ("--pretty X" as opposed to "--pretty=X")
1978 * not allowed, since the argument is optional.
1980 revs
->verbose_header
= 1;
1981 revs
->pretty_given
= 1;
1982 get_commit_format(optarg
, revs
);
1983 } else if (!strcmp(arg
, "--expand-tabs")) {
1984 revs
->expand_tabs_in_log
= 8;
1985 } else if (!strcmp(arg
, "--no-expand-tabs")) {
1986 revs
->expand_tabs_in_log
= 0;
1987 } else if (skip_prefix(arg
, "--expand-tabs=", &arg
)) {
1989 if (strtol_i(arg
, 10, &val
) < 0 || val
< 0)
1990 die("'%s': not a non-negative integer", arg
);
1991 revs
->expand_tabs_in_log
= val
;
1992 } else if (!strcmp(arg
, "--show-notes") || !strcmp(arg
, "--notes")) {
1993 revs
->show_notes
= 1;
1994 revs
->show_notes_given
= 1;
1995 revs
->notes_opt
.use_default_notes
= 1;
1996 } else if (!strcmp(arg
, "--show-signature")) {
1997 revs
->show_signature
= 1;
1998 } else if (!strcmp(arg
, "--no-show-signature")) {
1999 revs
->show_signature
= 0;
2000 } else if (!strcmp(arg
, "--show-linear-break")) {
2001 revs
->break_bar
= " ..........";
2002 revs
->track_linear
= 1;
2003 revs
->track_first_time
= 1;
2004 } else if (skip_prefix(arg
, "--show-linear-break=", &optarg
)) {
2005 revs
->break_bar
= xstrdup(optarg
);
2006 revs
->track_linear
= 1;
2007 revs
->track_first_time
= 1;
2008 } else if (skip_prefix(arg
, "--show-notes=", &optarg
) ||
2009 skip_prefix(arg
, "--notes=", &optarg
)) {
2010 struct strbuf buf
= STRBUF_INIT
;
2011 revs
->show_notes
= 1;
2012 revs
->show_notes_given
= 1;
2013 if (starts_with(arg
, "--show-notes=") &&
2014 revs
->notes_opt
.use_default_notes
< 0)
2015 revs
->notes_opt
.use_default_notes
= 1;
2016 strbuf_addstr(&buf
, optarg
);
2017 expand_notes_ref(&buf
);
2018 string_list_append(&revs
->notes_opt
.extra_notes_refs
,
2019 strbuf_detach(&buf
, NULL
));
2020 } else if (!strcmp(arg
, "--no-notes")) {
2021 revs
->show_notes
= 0;
2022 revs
->show_notes_given
= 1;
2023 revs
->notes_opt
.use_default_notes
= -1;
2024 /* we have been strdup'ing ourselves, so trick
2025 * string_list into free()ing strings */
2026 revs
->notes_opt
.extra_notes_refs
.strdup_strings
= 1;
2027 string_list_clear(&revs
->notes_opt
.extra_notes_refs
, 0);
2028 revs
->notes_opt
.extra_notes_refs
.strdup_strings
= 0;
2029 } else if (!strcmp(arg
, "--standard-notes")) {
2030 revs
->show_notes_given
= 1;
2031 revs
->notes_opt
.use_default_notes
= 1;
2032 } else if (!strcmp(arg
, "--no-standard-notes")) {
2033 revs
->notes_opt
.use_default_notes
= 0;
2034 } else if (!strcmp(arg
, "--oneline")) {
2035 revs
->verbose_header
= 1;
2036 get_commit_format("oneline", revs
);
2037 revs
->pretty_given
= 1;
2038 revs
->abbrev_commit
= 1;
2039 } else if (!strcmp(arg
, "--graph")) {
2040 revs
->topo_order
= 1;
2041 revs
->rewrite_parents
= 1;
2042 revs
->graph
= graph_init(revs
);
2043 } else if (!strcmp(arg
, "--root")) {
2044 revs
->show_root_diff
= 1;
2045 } else if (!strcmp(arg
, "--no-commit-id")) {
2046 revs
->no_commit_id
= 1;
2047 } else if (!strcmp(arg
, "--always")) {
2048 revs
->always_show_header
= 1;
2049 } else if (!strcmp(arg
, "--no-abbrev")) {
2051 } else if (!strcmp(arg
, "--abbrev")) {
2052 revs
->abbrev
= DEFAULT_ABBREV
;
2053 } else if (skip_prefix(arg
, "--abbrev=", &optarg
)) {
2054 revs
->abbrev
= strtoul(optarg
, NULL
, 10);
2055 if (revs
->abbrev
< MINIMUM_ABBREV
)
2056 revs
->abbrev
= MINIMUM_ABBREV
;
2057 else if (revs
->abbrev
> hexsz
)
2058 revs
->abbrev
= hexsz
;
2059 } else if (!strcmp(arg
, "--abbrev-commit")) {
2060 revs
->abbrev_commit
= 1;
2061 revs
->abbrev_commit_given
= 1;
2062 } else if (!strcmp(arg
, "--no-abbrev-commit")) {
2063 revs
->abbrev_commit
= 0;
2064 } else if (!strcmp(arg
, "--full-diff")) {
2066 revs
->full_diff
= 1;
2067 } else if (!strcmp(arg
, "--full-history")) {
2068 revs
->simplify_history
= 0;
2069 } else if (!strcmp(arg
, "--relative-date")) {
2070 revs
->date_mode
.type
= DATE_RELATIVE
;
2071 revs
->date_mode_explicit
= 1;
2072 } else if ((argcount
= parse_long_opt("date", argv
, &optarg
))) {
2073 parse_date_format(optarg
, &revs
->date_mode
);
2074 revs
->date_mode_explicit
= 1;
2076 } else if (!strcmp(arg
, "--log-size")) {
2077 revs
->show_log_size
= 1;
2080 * Grepping the commit log
2082 else if ((argcount
= parse_long_opt("author", argv
, &optarg
))) {
2083 add_header_grep(revs
, GREP_HEADER_AUTHOR
, optarg
);
2085 } else if ((argcount
= parse_long_opt("committer", argv
, &optarg
))) {
2086 add_header_grep(revs
, GREP_HEADER_COMMITTER
, optarg
);
2088 } else if ((argcount
= parse_long_opt("grep-reflog", argv
, &optarg
))) {
2089 add_header_grep(revs
, GREP_HEADER_REFLOG
, optarg
);
2091 } else if ((argcount
= parse_long_opt("grep", argv
, &optarg
))) {
2092 add_message_grep(revs
, optarg
);
2094 } else if (!strcmp(arg
, "--grep-debug")) {
2095 revs
->grep_filter
.debug
= 1;
2096 } else if (!strcmp(arg
, "--basic-regexp")) {
2097 revs
->grep_filter
.pattern_type_option
= GREP_PATTERN_TYPE_BRE
;
2098 } else if (!strcmp(arg
, "--extended-regexp") || !strcmp(arg
, "-E")) {
2099 revs
->grep_filter
.pattern_type_option
= GREP_PATTERN_TYPE_ERE
;
2100 } else if (!strcmp(arg
, "--regexp-ignore-case") || !strcmp(arg
, "-i")) {
2101 revs
->grep_filter
.ignore_case
= 1;
2102 revs
->diffopt
.pickaxe_opts
|= DIFF_PICKAXE_IGNORE_CASE
;
2103 } else if (!strcmp(arg
, "--fixed-strings") || !strcmp(arg
, "-F")) {
2104 revs
->grep_filter
.pattern_type_option
= GREP_PATTERN_TYPE_FIXED
;
2105 } else if (!strcmp(arg
, "--perl-regexp") || !strcmp(arg
, "-P")) {
2106 revs
->grep_filter
.pattern_type_option
= GREP_PATTERN_TYPE_PCRE
;
2107 } else if (!strcmp(arg
, "--all-match")) {
2108 revs
->grep_filter
.all_match
= 1;
2109 } else if (!strcmp(arg
, "--invert-grep")) {
2110 revs
->invert_grep
= 1;
2111 } else if ((argcount
= parse_long_opt("encoding", argv
, &optarg
))) {
2112 if (strcmp(optarg
, "none"))
2113 git_log_output_encoding
= xstrdup(optarg
);
2115 git_log_output_encoding
= "";
2117 } else if (!strcmp(arg
, "--reverse")) {
2119 } else if (!strcmp(arg
, "--children")) {
2120 revs
->children
.name
= "children";
2122 } else if (!strcmp(arg
, "--ignore-missing")) {
2123 revs
->ignore_missing
= 1;
2124 } else if (!strcmp(arg
, "--exclude-promisor-objects")) {
2125 if (fetch_if_missing
)
2126 BUG("exclude_promisor_objects can only be used when fetch_if_missing is 0");
2127 revs
->exclude_promisor_objects
= 1;
2129 int opts
= diff_opt_parse(&revs
->diffopt
, argv
, argc
, revs
->prefix
);
2131 unkv
[(*unkc
)++] = arg
;
2134 if (revs
->graph
&& revs
->track_linear
)
2135 die("--show-linear-break and --graph are incompatible");
2140 void parse_revision_opt(struct rev_info
*revs
, struct parse_opt_ctx_t
*ctx
,
2141 const struct option
*options
,
2142 const char * const usagestr
[])
2144 int n
= handle_revision_opt(revs
, ctx
->argc
, ctx
->argv
,
2145 &ctx
->cpidx
, ctx
->out
);
2147 error("unknown option `%s'", ctx
->argv
[0]);
2148 usage_with_options(usagestr
, options
);
2154 static int for_each_bisect_ref(struct ref_store
*refs
, each_ref_fn fn
,
2155 void *cb_data
, const char *term
)
2157 struct strbuf bisect_refs
= STRBUF_INIT
;
2159 strbuf_addf(&bisect_refs
, "refs/bisect/%s", term
);
2160 status
= refs_for_each_fullref_in(refs
, bisect_refs
.buf
, fn
, cb_data
, 0);
2161 strbuf_release(&bisect_refs
);
2165 static int for_each_bad_bisect_ref(struct ref_store
*refs
, each_ref_fn fn
, void *cb_data
)
2167 return for_each_bisect_ref(refs
, fn
, cb_data
, term_bad
);
2170 static int for_each_good_bisect_ref(struct ref_store
*refs
, each_ref_fn fn
, void *cb_data
)
2172 return for_each_bisect_ref(refs
, fn
, cb_data
, term_good
);
2175 static int handle_revision_pseudo_opt(const char *submodule
,
2176 struct rev_info
*revs
,
2177 int argc
, const char **argv
, int *flags
)
2179 const char *arg
= argv
[0];
2181 struct ref_store
*refs
;
2186 * We need some something like get_submodule_worktrees()
2187 * before we can go through all worktrees of a submodule,
2188 * .e.g with adding all HEADs from --all, which is not
2189 * supported right now, so stick to single worktree.
2191 if (!revs
->single_worktree
)
2192 BUG("--single-worktree cannot be used together with submodule");
2193 refs
= get_submodule_ref_store(submodule
);
2195 refs
= get_main_ref_store(the_repository
);
2200 * Commands like "git shortlog" will not accept the options below
2201 * unless parse_revision_opt queues them (as opposed to erroring
2204 * When implementing your new pseudo-option, remember to
2205 * register it in the list at the top of handle_revision_opt.
2207 if (!strcmp(arg
, "--all")) {
2208 handle_refs(refs
, revs
, *flags
, refs_for_each_ref
);
2209 handle_refs(refs
, revs
, *flags
, refs_head_ref
);
2210 if (!revs
->single_worktree
) {
2211 struct all_refs_cb cb
;
2213 init_all_refs_cb(&cb
, revs
, *flags
);
2214 other_head_refs(handle_one_ref
, &cb
);
2216 clear_ref_exclusion(&revs
->ref_excludes
);
2217 } else if (!strcmp(arg
, "--branches")) {
2218 handle_refs(refs
, revs
, *flags
, refs_for_each_branch_ref
);
2219 clear_ref_exclusion(&revs
->ref_excludes
);
2220 } else if (!strcmp(arg
, "--bisect")) {
2221 read_bisect_terms(&term_bad
, &term_good
);
2222 handle_refs(refs
, revs
, *flags
, for_each_bad_bisect_ref
);
2223 handle_refs(refs
, revs
, *flags
^ (UNINTERESTING
| BOTTOM
),
2224 for_each_good_bisect_ref
);
2226 } else if (!strcmp(arg
, "--tags")) {
2227 handle_refs(refs
, revs
, *flags
, refs_for_each_tag_ref
);
2228 clear_ref_exclusion(&revs
->ref_excludes
);
2229 } else if (!strcmp(arg
, "--remotes")) {
2230 handle_refs(refs
, revs
, *flags
, refs_for_each_remote_ref
);
2231 clear_ref_exclusion(&revs
->ref_excludes
);
2232 } else if ((argcount
= parse_long_opt("glob", argv
, &optarg
))) {
2233 struct all_refs_cb cb
;
2234 init_all_refs_cb(&cb
, revs
, *flags
);
2235 for_each_glob_ref(handle_one_ref
, optarg
, &cb
);
2236 clear_ref_exclusion(&revs
->ref_excludes
);
2238 } else if ((argcount
= parse_long_opt("exclude", argv
, &optarg
))) {
2239 add_ref_exclusion(&revs
->ref_excludes
, optarg
);
2241 } else if (skip_prefix(arg
, "--branches=", &optarg
)) {
2242 struct all_refs_cb cb
;
2243 init_all_refs_cb(&cb
, revs
, *flags
);
2244 for_each_glob_ref_in(handle_one_ref
, optarg
, "refs/heads/", &cb
);
2245 clear_ref_exclusion(&revs
->ref_excludes
);
2246 } else if (skip_prefix(arg
, "--tags=", &optarg
)) {
2247 struct all_refs_cb cb
;
2248 init_all_refs_cb(&cb
, revs
, *flags
);
2249 for_each_glob_ref_in(handle_one_ref
, optarg
, "refs/tags/", &cb
);
2250 clear_ref_exclusion(&revs
->ref_excludes
);
2251 } else if (skip_prefix(arg
, "--remotes=", &optarg
)) {
2252 struct all_refs_cb cb
;
2253 init_all_refs_cb(&cb
, revs
, *flags
);
2254 for_each_glob_ref_in(handle_one_ref
, optarg
, "refs/remotes/", &cb
);
2255 clear_ref_exclusion(&revs
->ref_excludes
);
2256 } else if (!strcmp(arg
, "--reflog")) {
2257 add_reflogs_to_pending(revs
, *flags
);
2258 } else if (!strcmp(arg
, "--indexed-objects")) {
2259 add_index_objects_to_pending(revs
, *flags
);
2260 } else if (!strcmp(arg
, "--not")) {
2261 *flags
^= UNINTERESTING
| BOTTOM
;
2262 } else if (!strcmp(arg
, "--no-walk")) {
2263 revs
->no_walk
= REVISION_WALK_NO_WALK_SORTED
;
2264 } else if (skip_prefix(arg
, "--no-walk=", &optarg
)) {
2266 * Detached form ("--no-walk X" as opposed to "--no-walk=X")
2267 * not allowed, since the argument is optional.
2269 if (!strcmp(optarg
, "sorted"))
2270 revs
->no_walk
= REVISION_WALK_NO_WALK_SORTED
;
2271 else if (!strcmp(optarg
, "unsorted"))
2272 revs
->no_walk
= REVISION_WALK_NO_WALK_UNSORTED
;
2274 return error("invalid argument to --no-walk");
2275 } else if (!strcmp(arg
, "--do-walk")) {
2277 } else if (!strcmp(arg
, "--single-worktree")) {
2278 revs
->single_worktree
= 1;
2286 static void NORETURN
diagnose_missing_default(const char *def
)
2289 const char *refname
;
2291 refname
= resolve_ref_unsafe(def
, 0, NULL
, &flags
);
2292 if (!refname
|| !(flags
& REF_ISSYMREF
) || (flags
& REF_ISBROKEN
))
2293 die(_("your current branch appears to be broken"));
2295 skip_prefix(refname
, "refs/heads/", &refname
);
2296 die(_("your current branch '%s' does not have any commits yet"),
2301 * Parse revision information, filling in the "rev_info" structure,
2302 * and removing the used arguments from the argument list.
2304 * Returns the number of arguments left that weren't recognized
2305 * (which are also moved to the head of the argument list)
2307 int setup_revisions(int argc
, const char **argv
, struct rev_info
*revs
, struct setup_revision_opt
*opt
)
2309 int i
, flags
, left
, seen_dashdash
, read_from_stdin
, got_rev_arg
= 0, revarg_opt
;
2310 struct argv_array prune_data
= ARGV_ARRAY_INIT
;
2311 const char *submodule
= NULL
;
2314 submodule
= opt
->submodule
;
2316 /* First, search for "--" */
2317 if (opt
&& opt
->assume_dashdash
) {
2321 for (i
= 1; i
< argc
; i
++) {
2322 const char *arg
= argv
[i
];
2323 if (strcmp(arg
, "--"))
2328 argv_array_pushv(&prune_data
, argv
+ i
+ 1);
2334 /* Second, deal with arguments and options */
2336 revarg_opt
= opt
? opt
->revarg_opt
: 0;
2338 revarg_opt
|= REVARG_CANNOT_BE_FILENAME
;
2339 read_from_stdin
= 0;
2340 for (left
= i
= 1; i
< argc
; i
++) {
2341 const char *arg
= argv
[i
];
2345 opts
= handle_revision_pseudo_opt(submodule
,
2346 revs
, argc
- i
, argv
+ i
,
2353 if (!strcmp(arg
, "--stdin")) {
2354 if (revs
->disable_stdin
) {
2358 if (read_from_stdin
++)
2359 die("--stdin given twice?");
2360 read_revisions_from_stdin(revs
, &prune_data
);
2364 opts
= handle_revision_opt(revs
, argc
- i
, argv
+ i
, &left
, argv
);
2375 if (handle_revision_arg(arg
, revs
, flags
, revarg_opt
)) {
2377 if (seen_dashdash
|| *arg
== '^')
2378 die("bad revision '%s'", arg
);
2380 /* If we didn't have a "--":
2381 * (1) all filenames must exist;
2382 * (2) all rev-args must not be interpretable
2383 * as a valid filename.
2384 * but the latter we have checked in the main loop.
2386 for (j
= i
; j
< argc
; j
++)
2387 verify_filename(revs
->prefix
, argv
[j
], j
== i
);
2389 argv_array_pushv(&prune_data
, argv
+ i
);
2396 if (prune_data
.argc
) {
2398 * If we need to introduce the magic "a lone ':' means no
2399 * pathspec whatsoever", here is the place to do so.
2401 * if (prune_data.nr == 1 && !strcmp(prune_data[0], ":")) {
2402 * prune_data.nr = 0;
2403 * prune_data.alloc = 0;
2404 * free(prune_data.path);
2405 * prune_data.path = NULL;
2407 * terminate prune_data.alloc with NULL and
2408 * call init_pathspec() to set revs->prune_data here.
2411 parse_pathspec(&revs
->prune_data
, 0, 0,
2412 revs
->prefix
, prune_data
.argv
);
2414 argv_array_clear(&prune_data
);
2416 if (revs
->def
== NULL
)
2417 revs
->def
= opt
? opt
->def
: NULL
;
2418 if (opt
&& opt
->tweak
)
2419 opt
->tweak(revs
, opt
);
2420 if (revs
->show_merge
)
2421 prepare_show_merge(revs
);
2422 if (revs
->def
&& !revs
->pending
.nr
&& !revs
->rev_input_given
&& !got_rev_arg
) {
2423 struct object_id oid
;
2424 struct object
*object
;
2425 struct object_context oc
;
2426 if (get_oid_with_context(revs
->def
, 0, &oid
, &oc
))
2427 diagnose_missing_default(revs
->def
);
2428 object
= get_reference(revs
, revs
->def
, &oid
, 0);
2429 add_pending_object_with_mode(revs
, object
, revs
->def
, oc
.mode
);
2432 /* Did the user ask for any diff output? Run the diff! */
2433 if (revs
->diffopt
.output_format
& ~DIFF_FORMAT_NO_OUTPUT
)
2436 /* Pickaxe, diff-filter and rename following need diffs */
2437 if ((revs
->diffopt
.pickaxe_opts
& DIFF_PICKAXE_KINDS_MASK
) ||
2438 revs
->diffopt
.filter
||
2439 revs
->diffopt
.flags
.follow_renames
)
2442 if (revs
->diffopt
.objfind
)
2443 revs
->simplify_history
= 0;
2445 if (revs
->topo_order
)
2448 if (revs
->prune_data
.nr
) {
2449 copy_pathspec(&revs
->pruning
.pathspec
, &revs
->prune_data
);
2450 /* Can't prune commits with rename following: the paths change.. */
2451 if (!revs
->diffopt
.flags
.follow_renames
)
2453 if (!revs
->full_diff
)
2454 copy_pathspec(&revs
->diffopt
.pathspec
,
2457 if (revs
->combine_merges
)
2458 revs
->ignore_merges
= 0;
2459 revs
->diffopt
.abbrev
= revs
->abbrev
;
2461 if (revs
->line_level_traverse
) {
2463 revs
->topo_order
= 1;
2466 diff_setup_done(&revs
->diffopt
);
2468 grep_commit_pattern_type(GREP_PATTERN_TYPE_UNSPECIFIED
,
2469 &revs
->grep_filter
);
2470 compile_grep_patterns(&revs
->grep_filter
);
2472 if (revs
->reverse
&& revs
->reflog_info
)
2473 die("cannot combine --reverse with --walk-reflogs");
2474 if (revs
->reflog_info
&& revs
->limited
)
2475 die("cannot combine --walk-reflogs with history-limiting options");
2476 if (revs
->rewrite_parents
&& revs
->children
.name
)
2477 die("cannot combine --parents and --children");
2480 * Limitations on the graph functionality
2482 if (revs
->reverse
&& revs
->graph
)
2483 die("cannot combine --reverse with --graph");
2485 if (revs
->reflog_info
&& revs
->graph
)
2486 die("cannot combine --walk-reflogs with --graph");
2487 if (revs
->no_walk
&& revs
->graph
)
2488 die("cannot combine --no-walk with --graph");
2489 if (!revs
->reflog_info
&& revs
->grep_filter
.use_reflog_filter
)
2490 die("cannot use --grep-reflog without --walk-reflogs");
2492 if (revs
->first_parent_only
&& revs
->bisect
)
2493 die(_("--first-parent is incompatible with --bisect"));
2495 if (revs
->expand_tabs_in_log
< 0)
2496 revs
->expand_tabs_in_log
= revs
->expand_tabs_in_log_default
;
2501 static void add_child(struct rev_info
*revs
, struct commit
*parent
, struct commit
*child
)
2503 struct commit_list
*l
= xcalloc(1, sizeof(*l
));
2506 l
->next
= add_decoration(&revs
->children
, &parent
->object
, l
);
2509 static int remove_duplicate_parents(struct rev_info
*revs
, struct commit
*commit
)
2511 struct treesame_state
*ts
= lookup_decoration(&revs
->treesame
, &commit
->object
);
2512 struct commit_list
**pp
, *p
;
2513 int surviving_parents
;
2515 /* Examine existing parents while marking ones we have seen... */
2516 pp
= &commit
->parents
;
2517 surviving_parents
= 0;
2518 while ((p
= *pp
) != NULL
) {
2519 struct commit
*parent
= p
->item
;
2520 if (parent
->object
.flags
& TMP_MARK
) {
2523 compact_treesame(revs
, commit
, surviving_parents
);
2526 parent
->object
.flags
|= TMP_MARK
;
2527 surviving_parents
++;
2530 /* clear the temporary mark */
2531 for (p
= commit
->parents
; p
; p
= p
->next
) {
2532 p
->item
->object
.flags
&= ~TMP_MARK
;
2534 /* no update_treesame() - removing duplicates can't affect TREESAME */
2535 return surviving_parents
;
2538 struct merge_simplify_state
{
2539 struct commit
*simplified
;
2542 static struct merge_simplify_state
*locate_simplify_state(struct rev_info
*revs
, struct commit
*commit
)
2544 struct merge_simplify_state
*st
;
2546 st
= lookup_decoration(&revs
->merge_simplification
, &commit
->object
);
2548 st
= xcalloc(1, sizeof(*st
));
2549 add_decoration(&revs
->merge_simplification
, &commit
->object
, st
);
2554 static int mark_redundant_parents(struct rev_info
*revs
, struct commit
*commit
)
2556 struct commit_list
*h
= reduce_heads(commit
->parents
);
2557 int i
= 0, marked
= 0;
2558 struct commit_list
*po
, *pn
;
2560 /* Want these for sanity-checking only */
2561 int orig_cnt
= commit_list_count(commit
->parents
);
2562 int cnt
= commit_list_count(h
);
2565 * Not ready to remove items yet, just mark them for now, based
2566 * on the output of reduce_heads(). reduce_heads outputs the reduced
2567 * set in its original order, so this isn't too hard.
2569 po
= commit
->parents
;
2572 if (pn
&& po
->item
== pn
->item
) {
2576 po
->item
->object
.flags
|= TMP_MARK
;
2582 if (i
!= cnt
|| cnt
+marked
!= orig_cnt
)
2583 die("mark_redundant_parents %d %d %d %d", orig_cnt
, cnt
, i
, marked
);
2585 free_commit_list(h
);
2590 static int mark_treesame_root_parents(struct rev_info
*revs
, struct commit
*commit
)
2592 struct commit_list
*p
;
2595 for (p
= commit
->parents
; p
; p
= p
->next
) {
2596 struct commit
*parent
= p
->item
;
2597 if (!parent
->parents
&& (parent
->object
.flags
& TREESAME
)) {
2598 parent
->object
.flags
|= TMP_MARK
;
2607 * Awkward naming - this means one parent we are TREESAME to.
2608 * cf mark_treesame_root_parents: root parents that are TREESAME (to an
2609 * empty tree). Better name suggestions?
2611 static int leave_one_treesame_to_parent(struct rev_info
*revs
, struct commit
*commit
)
2613 struct treesame_state
*ts
= lookup_decoration(&revs
->treesame
, &commit
->object
);
2614 struct commit
*unmarked
= NULL
, *marked
= NULL
;
2615 struct commit_list
*p
;
2618 for (p
= commit
->parents
, n
= 0; p
; p
= p
->next
, n
++) {
2619 if (ts
->treesame
[n
]) {
2620 if (p
->item
->object
.flags
& TMP_MARK
) {
2633 * If we are TREESAME to a marked-for-deletion parent, but not to any
2634 * unmarked parents, unmark the first TREESAME parent. This is the
2635 * parent that the default simplify_history==1 scan would have followed,
2636 * and it doesn't make sense to omit that path when asking for a
2637 * simplified full history. Retaining it improves the chances of
2638 * understanding odd missed merges that took an old version of a file.
2642 * I--------*X A modified the file, but mainline merge X used
2643 * \ / "-s ours", so took the version from I. X is
2644 * `-*A--' TREESAME to I and !TREESAME to A.
2646 * Default log from X would produce "I". Without this check,
2647 * --full-history --simplify-merges would produce "I-A-X", showing
2648 * the merge commit X and that it changed A, but not making clear that
2649 * it had just taken the I version. With this check, the topology above
2652 * Note that it is possible that the simplification chooses a different
2653 * TREESAME parent from the default, in which case this test doesn't
2654 * activate, and we _do_ drop the default parent. Example:
2656 * I------X A modified the file, but it was reverted in B,
2657 * \ / meaning mainline merge X is TREESAME to both
2660 * Default log would produce "I" by following the first parent;
2661 * --full-history --simplify-merges will produce "I-A-B". But this is a
2662 * reasonable result - it presents a logical full history leading from
2663 * I to X, and X is not an important merge.
2665 if (!unmarked
&& marked
) {
2666 marked
->object
.flags
&= ~TMP_MARK
;
2673 static int remove_marked_parents(struct rev_info
*revs
, struct commit
*commit
)
2675 struct commit_list
**pp
, *p
;
2676 int nth_parent
, removed
= 0;
2678 pp
= &commit
->parents
;
2680 while ((p
= *pp
) != NULL
) {
2681 struct commit
*parent
= p
->item
;
2682 if (parent
->object
.flags
& TMP_MARK
) {
2683 parent
->object
.flags
&= ~TMP_MARK
;
2687 compact_treesame(revs
, commit
, nth_parent
);
2694 /* Removing parents can only increase TREESAMEness */
2695 if (removed
&& !(commit
->object
.flags
& TREESAME
))
2696 update_treesame(revs
, commit
);
2701 static struct commit_list
**simplify_one(struct rev_info
*revs
, struct commit
*commit
, struct commit_list
**tail
)
2703 struct commit_list
*p
;
2704 struct commit
*parent
;
2705 struct merge_simplify_state
*st
, *pst
;
2708 st
= locate_simplify_state(revs
, commit
);
2711 * Have we handled this one?
2717 * An UNINTERESTING commit simplifies to itself, so does a
2718 * root commit. We do not rewrite parents of such commit
2721 if ((commit
->object
.flags
& UNINTERESTING
) || !commit
->parents
) {
2722 st
->simplified
= commit
;
2727 * Do we know what commit all of our parents that matter
2728 * should be rewritten to? Otherwise we are not ready to
2729 * rewrite this one yet.
2731 for (cnt
= 0, p
= commit
->parents
; p
; p
= p
->next
) {
2732 pst
= locate_simplify_state(revs
, p
->item
);
2733 if (!pst
->simplified
) {
2734 tail
= &commit_list_insert(p
->item
, tail
)->next
;
2737 if (revs
->first_parent_only
)
2741 tail
= &commit_list_insert(commit
, tail
)->next
;
2746 * Rewrite our list of parents. Note that this cannot
2747 * affect our TREESAME flags in any way - a commit is
2748 * always TREESAME to its simplification.
2750 for (p
= commit
->parents
; p
; p
= p
->next
) {
2751 pst
= locate_simplify_state(revs
, p
->item
);
2752 p
->item
= pst
->simplified
;
2753 if (revs
->first_parent_only
)
2757 if (revs
->first_parent_only
)
2760 cnt
= remove_duplicate_parents(revs
, commit
);
2763 * It is possible that we are a merge and one side branch
2764 * does not have any commit that touches the given paths;
2765 * in such a case, the immediate parent from that branch
2766 * will be rewritten to be the merge base.
2768 * o----X X: the commit we are looking at;
2769 * / / o: a commit that touches the paths;
2772 * Further, a merge of an independent branch that doesn't
2773 * touch the path will reduce to a treesame root parent:
2775 * ----o----X X: the commit we are looking at;
2776 * / o: a commit that touches the paths;
2777 * r r: a root commit not touching the paths
2779 * Detect and simplify both cases.
2782 int marked
= mark_redundant_parents(revs
, commit
);
2783 marked
+= mark_treesame_root_parents(revs
, commit
);
2785 marked
-= leave_one_treesame_to_parent(revs
, commit
);
2787 cnt
= remove_marked_parents(revs
, commit
);
2791 * A commit simplifies to itself if it is a root, if it is
2792 * UNINTERESTING, if it touches the given paths, or if it is a
2793 * merge and its parents don't simplify to one relevant commit
2794 * (the first two cases are already handled at the beginning of
2797 * Otherwise, it simplifies to what its sole relevant parent
2801 (commit
->object
.flags
& UNINTERESTING
) ||
2802 !(commit
->object
.flags
& TREESAME
) ||
2803 (parent
= one_relevant_parent(revs
, commit
->parents
)) == NULL
)
2804 st
->simplified
= commit
;
2806 pst
= locate_simplify_state(revs
, parent
);
2807 st
->simplified
= pst
->simplified
;
2812 static void simplify_merges(struct rev_info
*revs
)
2814 struct commit_list
*list
, *next
;
2815 struct commit_list
*yet_to_do
, **tail
;
2816 struct commit
*commit
;
2821 /* feed the list reversed */
2823 for (list
= revs
->commits
; list
; list
= next
) {
2824 commit
= list
->item
;
2827 * Do not free(list) here yet; the original list
2828 * is used later in this function.
2830 commit_list_insert(commit
, &yet_to_do
);
2837 commit
= pop_commit(&list
);
2838 tail
= simplify_one(revs
, commit
, tail
);
2842 /* clean up the result, removing the simplified ones */
2843 list
= revs
->commits
;
2844 revs
->commits
= NULL
;
2845 tail
= &revs
->commits
;
2847 struct merge_simplify_state
*st
;
2849 commit
= pop_commit(&list
);
2850 st
= locate_simplify_state(revs
, commit
);
2851 if (st
->simplified
== commit
)
2852 tail
= &commit_list_insert(commit
, tail
)->next
;
2856 static void set_children(struct rev_info
*revs
)
2858 struct commit_list
*l
;
2859 for (l
= revs
->commits
; l
; l
= l
->next
) {
2860 struct commit
*commit
= l
->item
;
2861 struct commit_list
*p
;
2863 for (p
= commit
->parents
; p
; p
= p
->next
)
2864 add_child(revs
, p
->item
, commit
);
2868 void reset_revision_walk(void)
2870 clear_object_flags(SEEN
| ADDED
| SHOWN
);
2873 static int mark_uninteresting(const struct object_id
*oid
,
2874 struct packed_git
*pack
,
2878 struct object
*o
= parse_object(oid
);
2879 o
->flags
|= UNINTERESTING
| SEEN
;
2883 int prepare_revision_walk(struct rev_info
*revs
)
2886 struct object_array old_pending
;
2887 struct commit_list
**next
= &revs
->commits
;
2889 memcpy(&old_pending
, &revs
->pending
, sizeof(old_pending
));
2890 revs
->pending
.nr
= 0;
2891 revs
->pending
.alloc
= 0;
2892 revs
->pending
.objects
= NULL
;
2893 for (i
= 0; i
< old_pending
.nr
; i
++) {
2894 struct object_array_entry
*e
= old_pending
.objects
+ i
;
2895 struct commit
*commit
= handle_commit(revs
, e
);
2897 if (!(commit
->object
.flags
& SEEN
)) {
2898 commit
->object
.flags
|= SEEN
;
2899 next
= commit_list_append(commit
, next
);
2903 object_array_clear(&old_pending
);
2905 /* Signal whether we need per-parent treesame decoration */
2906 if (revs
->simplify_merges
||
2907 (revs
->limited
&& limiting_can_increase_treesame(revs
)))
2908 revs
->treesame
.name
= "treesame";
2910 if (revs
->exclude_promisor_objects
) {
2911 for_each_packed_object(mark_uninteresting
, NULL
,
2912 FOR_EACH_OBJECT_PROMISOR_ONLY
);
2915 if (revs
->no_walk
!= REVISION_WALK_NO_WALK_UNSORTED
)
2916 commit_list_sort_by_date(&revs
->commits
);
2920 if (limit_list(revs
) < 0)
2922 if (revs
->topo_order
)
2923 sort_in_topological_order(&revs
->commits
, revs
->sort_order
);
2924 if (revs
->line_level_traverse
)
2925 line_log_filter(revs
);
2926 if (revs
->simplify_merges
)
2927 simplify_merges(revs
);
2928 if (revs
->children
.name
)
2933 static enum rewrite_result
rewrite_one(struct rev_info
*revs
, struct commit
**pp
)
2935 struct commit_list
*cache
= NULL
;
2938 struct commit
*p
= *pp
;
2940 if (add_parents_to_list(revs
, p
, &revs
->commits
, &cache
) < 0)
2941 return rewrite_one_error
;
2942 if (p
->object
.flags
& UNINTERESTING
)
2943 return rewrite_one_ok
;
2944 if (!(p
->object
.flags
& TREESAME
))
2945 return rewrite_one_ok
;
2947 return rewrite_one_noparents
;
2948 if ((p
= one_relevant_parent(revs
, p
->parents
)) == NULL
)
2949 return rewrite_one_ok
;
2954 int rewrite_parents(struct rev_info
*revs
, struct commit
*commit
,
2955 rewrite_parent_fn_t rewrite_parent
)
2957 struct commit_list
**pp
= &commit
->parents
;
2959 struct commit_list
*parent
= *pp
;
2960 switch (rewrite_parent(revs
, &parent
->item
)) {
2961 case rewrite_one_ok
:
2963 case rewrite_one_noparents
:
2966 case rewrite_one_error
:
2971 remove_duplicate_parents(revs
, commit
);
2975 static int commit_rewrite_person(struct strbuf
*buf
, const char *what
, struct string_list
*mailmap
)
2977 char *person
, *endp
;
2978 size_t len
, namelen
, maillen
;
2981 struct ident_split ident
;
2983 person
= strstr(buf
->buf
, what
);
2987 person
+= strlen(what
);
2988 endp
= strchr(person
, '\n');
2992 len
= endp
- person
;
2994 if (split_ident_line(&ident
, person
, len
))
2997 mail
= ident
.mail_begin
;
2998 maillen
= ident
.mail_end
- ident
.mail_begin
;
2999 name
= ident
.name_begin
;
3000 namelen
= ident
.name_end
- ident
.name_begin
;
3002 if (map_user(mailmap
, &mail
, &maillen
, &name
, &namelen
)) {
3003 struct strbuf namemail
= STRBUF_INIT
;
3005 strbuf_addf(&namemail
, "%.*s <%.*s>",
3006 (int)namelen
, name
, (int)maillen
, mail
);
3008 strbuf_splice(buf
, ident
.name_begin
- buf
->buf
,
3009 ident
.mail_end
- ident
.name_begin
+ 1,
3010 namemail
.buf
, namemail
.len
);
3012 strbuf_release(&namemail
);
3020 static int commit_match(struct commit
*commit
, struct rev_info
*opt
)
3023 const char *encoding
;
3024 const char *message
;
3025 struct strbuf buf
= STRBUF_INIT
;
3027 if (!opt
->grep_filter
.pattern_list
&& !opt
->grep_filter
.header_list
)
3030 /* Prepend "fake" headers as needed */
3031 if (opt
->grep_filter
.use_reflog_filter
) {
3032 strbuf_addstr(&buf
, "reflog ");
3033 get_reflog_message(&buf
, opt
->reflog_info
);
3034 strbuf_addch(&buf
, '\n');
3038 * We grep in the user's output encoding, under the assumption that it
3039 * is the encoding they are most likely to write their grep pattern
3040 * for. In addition, it means we will match the "notes" encoding below,
3041 * so we will not end up with a buffer that has two different encodings
3044 encoding
= get_log_output_encoding();
3045 message
= logmsg_reencode(commit
, NULL
, encoding
);
3047 /* Copy the commit to temporary if we are using "fake" headers */
3049 strbuf_addstr(&buf
, message
);
3051 if (opt
->grep_filter
.header_list
&& opt
->mailmap
) {
3053 strbuf_addstr(&buf
, message
);
3055 commit_rewrite_person(&buf
, "\nauthor ", opt
->mailmap
);
3056 commit_rewrite_person(&buf
, "\ncommitter ", opt
->mailmap
);
3059 /* Append "fake" message parts as needed */
3060 if (opt
->show_notes
) {
3062 strbuf_addstr(&buf
, message
);
3063 format_display_notes(&commit
->object
.oid
, &buf
, encoding
, 1);
3067 * Find either in the original commit message, or in the temporary.
3068 * Note that we cast away the constness of "message" here. It is
3069 * const because it may come from the cached commit buffer. That's OK,
3070 * because we know that it is modifiable heap memory, and that while
3071 * grep_buffer may modify it for speed, it will restore any
3072 * changes before returning.
3075 retval
= grep_buffer(&opt
->grep_filter
, buf
.buf
, buf
.len
);
3077 retval
= grep_buffer(&opt
->grep_filter
,
3078 (char *)message
, strlen(message
));
3079 strbuf_release(&buf
);
3080 unuse_commit_buffer(commit
, message
);
3081 return opt
->invert_grep
? !retval
: retval
;
3084 static inline int want_ancestry(const struct rev_info
*revs
)
3086 return (revs
->rewrite_parents
|| revs
->children
.name
);
3090 * Return a timestamp to be used for --since/--until comparisons for this
3091 * commit, based on the revision options.
3093 static timestamp_t
comparison_date(const struct rev_info
*revs
,
3094 struct commit
*commit
)
3096 return revs
->reflog_info
?
3097 get_reflog_timestamp(revs
->reflog_info
) :
3101 enum commit_action
get_commit_action(struct rev_info
*revs
, struct commit
*commit
)
3103 if (commit
->object
.flags
& SHOWN
)
3104 return commit_ignore
;
3105 if (revs
->unpacked
&& has_object_pack(&commit
->object
.oid
))
3106 return commit_ignore
;
3107 if (commit
->object
.flags
& UNINTERESTING
)
3108 return commit_ignore
;
3109 if (revs
->min_age
!= -1 &&
3110 comparison_date(revs
, commit
) > revs
->min_age
)
3111 return commit_ignore
;
3112 if (revs
->min_parents
|| (revs
->max_parents
>= 0)) {
3113 int n
= commit_list_count(commit
->parents
);
3114 if ((n
< revs
->min_parents
) ||
3115 ((revs
->max_parents
>= 0) && (n
> revs
->max_parents
)))
3116 return commit_ignore
;
3118 if (!commit_match(commit
, revs
))
3119 return commit_ignore
;
3120 if (revs
->prune
&& revs
->dense
) {
3121 /* Commit without changes? */
3122 if (commit
->object
.flags
& TREESAME
) {
3124 struct commit_list
*p
;
3125 /* drop merges unless we want parenthood */
3126 if (!want_ancestry(revs
))
3127 return commit_ignore
;
3129 * If we want ancestry, then need to keep any merges
3130 * between relevant commits to tie together topology.
3131 * For consistency with TREESAME and simplification
3132 * use "relevant" here rather than just INTERESTING,
3133 * to treat bottom commit(s) as part of the topology.
3135 for (n
= 0, p
= commit
->parents
; p
; p
= p
->next
)
3136 if (relevant_commit(p
->item
))
3139 return commit_ignore
;
3145 define_commit_slab(saved_parents
, struct commit_list
*);
3147 #define EMPTY_PARENT_LIST ((struct commit_list *)-1)
3150 * You may only call save_parents() once per commit (this is checked
3151 * for non-root commits).
3153 static void save_parents(struct rev_info
*revs
, struct commit
*commit
)
3155 struct commit_list
**pp
;
3157 if (!revs
->saved_parents_slab
) {
3158 revs
->saved_parents_slab
= xmalloc(sizeof(struct saved_parents
));
3159 init_saved_parents(revs
->saved_parents_slab
);
3162 pp
= saved_parents_at(revs
->saved_parents_slab
, commit
);
3165 * When walking with reflogs, we may visit the same commit
3166 * several times: once for each appearance in the reflog.
3168 * In this case, save_parents() will be called multiple times.
3169 * We want to keep only the first set of parents. We need to
3170 * store a sentinel value for an empty (i.e., NULL) parent
3171 * list to distinguish it from a not-yet-saved list, however.
3175 if (commit
->parents
)
3176 *pp
= copy_commit_list(commit
->parents
);
3178 *pp
= EMPTY_PARENT_LIST
;
3181 static void free_saved_parents(struct rev_info
*revs
)
3183 if (revs
->saved_parents_slab
)
3184 clear_saved_parents(revs
->saved_parents_slab
);
3187 struct commit_list
*get_saved_parents(struct rev_info
*revs
, const struct commit
*commit
)
3189 struct commit_list
*parents
;
3191 if (!revs
->saved_parents_slab
)
3192 return commit
->parents
;
3194 parents
= *saved_parents_at(revs
->saved_parents_slab
, commit
);
3195 if (parents
== EMPTY_PARENT_LIST
)
3200 enum commit_action
simplify_commit(struct rev_info
*revs
, struct commit
*commit
)
3202 enum commit_action action
= get_commit_action(revs
, commit
);
3204 if (action
== commit_show
&&
3205 revs
->prune
&& revs
->dense
&& want_ancestry(revs
)) {
3207 * --full-diff on simplified parents is no good: it
3208 * will show spurious changes from the commits that
3209 * were elided. So we save the parents on the side
3210 * when --full-diff is in effect.
3212 if (revs
->full_diff
)
3213 save_parents(revs
, commit
);
3214 if (rewrite_parents(revs
, commit
, rewrite_one
) < 0)
3215 return commit_error
;
3220 static void track_linear(struct rev_info
*revs
, struct commit
*commit
)
3222 if (revs
->track_first_time
) {
3224 revs
->track_first_time
= 0;
3226 struct commit_list
*p
;
3227 for (p
= revs
->previous_parents
; p
; p
= p
->next
)
3228 if (p
->item
== NULL
|| /* first commit */
3229 !oidcmp(&p
->item
->object
.oid
, &commit
->object
.oid
))
3231 revs
->linear
= p
!= NULL
;
3233 if (revs
->reverse
) {
3235 commit
->object
.flags
|= TRACK_LINEAR
;
3237 free_commit_list(revs
->previous_parents
);
3238 revs
->previous_parents
= copy_commit_list(commit
->parents
);
3241 static struct commit
*get_revision_1(struct rev_info
*revs
)
3244 struct commit
*commit
;
3246 if (revs
->reflog_info
)
3247 commit
= next_reflog_entry(revs
->reflog_info
);
3249 commit
= pop_commit(&revs
->commits
);
3254 if (revs
->reflog_info
)
3255 commit
->object
.flags
&= ~(ADDED
| SEEN
| SHOWN
);
3258 * If we haven't done the list limiting, we need to look at
3259 * the parents here. We also need to do the date-based limiting
3260 * that we'd otherwise have done in limit_list().
3262 if (!revs
->limited
) {
3263 if (revs
->max_age
!= -1 &&
3264 comparison_date(revs
, commit
) < revs
->max_age
)
3267 if (revs
->reflog_info
)
3268 try_to_simplify_commit(revs
, commit
);
3269 else if (add_parents_to_list(revs
, commit
, &revs
->commits
, NULL
) < 0) {
3270 if (!revs
->ignore_missing_links
)
3271 die("Failed to traverse parents of commit %s",
3272 oid_to_hex(&commit
->object
.oid
));
3276 switch (simplify_commit(revs
, commit
)) {
3280 die("Failed to simplify parents of commit %s",
3281 oid_to_hex(&commit
->object
.oid
));
3283 if (revs
->track_linear
)
3284 track_linear(revs
, commit
);
3291 * Return true for entries that have not yet been shown. (This is an
3292 * object_array_each_func_t.)
3294 static int entry_unshown(struct object_array_entry
*entry
, void *cb_data_unused
)
3296 return !(entry
->item
->flags
& SHOWN
);
3300 * If array is on the verge of a realloc, garbage-collect any entries
3301 * that have already been shown to try to free up some space.
3303 static void gc_boundary(struct object_array
*array
)
3305 if (array
->nr
== array
->alloc
)
3306 object_array_filter(array
, entry_unshown
, NULL
);
3309 static void create_boundary_commit_list(struct rev_info
*revs
)
3313 struct object_array
*array
= &revs
->boundary_commits
;
3314 struct object_array_entry
*objects
= array
->objects
;
3317 * If revs->commits is non-NULL at this point, an error occurred in
3318 * get_revision_1(). Ignore the error and continue printing the
3319 * boundary commits anyway. (This is what the code has always
3322 if (revs
->commits
) {
3323 free_commit_list(revs
->commits
);
3324 revs
->commits
= NULL
;
3328 * Put all of the actual boundary commits from revs->boundary_commits
3329 * into revs->commits
3331 for (i
= 0; i
< array
->nr
; i
++) {
3332 c
= (struct commit
*)(objects
[i
].item
);
3335 if (!(c
->object
.flags
& CHILD_SHOWN
))
3337 if (c
->object
.flags
& (SHOWN
| BOUNDARY
))
3339 c
->object
.flags
|= BOUNDARY
;
3340 commit_list_insert(c
, &revs
->commits
);
3344 * If revs->topo_order is set, sort the boundary commits
3345 * in topological order
3347 sort_in_topological_order(&revs
->commits
, revs
->sort_order
);
3350 static struct commit
*get_revision_internal(struct rev_info
*revs
)
3352 struct commit
*c
= NULL
;
3353 struct commit_list
*l
;
3355 if (revs
->boundary
== 2) {
3357 * All of the normal commits have already been returned,
3358 * and we are now returning boundary commits.
3359 * create_boundary_commit_list() has populated
3360 * revs->commits with the remaining commits to return.
3362 c
= pop_commit(&revs
->commits
);
3364 c
->object
.flags
|= SHOWN
;
3369 * If our max_count counter has reached zero, then we are done. We
3370 * don't simply return NULL because we still might need to show
3371 * boundary commits. But we want to avoid calling get_revision_1, which
3372 * might do a considerable amount of work finding the next commit only
3373 * for us to throw it away.
3375 * If it is non-zero, then either we don't have a max_count at all
3376 * (-1), or it is still counting, in which case we decrement.
3378 if (revs
->max_count
) {
3379 c
= get_revision_1(revs
);
3381 while (revs
->skip_count
> 0) {
3383 c
= get_revision_1(revs
);
3389 if (revs
->max_count
> 0)
3394 c
->object
.flags
|= SHOWN
;
3396 if (!revs
->boundary
)
3401 * get_revision_1() runs out the commits, and
3402 * we are done computing the boundaries.
3403 * switch to boundary commits output mode.
3408 * Update revs->commits to contain the list of
3411 create_boundary_commit_list(revs
);
3413 return get_revision_internal(revs
);
3417 * boundary commits are the commits that are parents of the
3418 * ones we got from get_revision_1() but they themselves are
3419 * not returned from get_revision_1(). Before returning
3420 * 'c', we need to mark its parents that they could be boundaries.
3423 for (l
= c
->parents
; l
; l
= l
->next
) {
3425 p
= &(l
->item
->object
);
3426 if (p
->flags
& (CHILD_SHOWN
| SHOWN
))
3428 p
->flags
|= CHILD_SHOWN
;
3429 gc_boundary(&revs
->boundary_commits
);
3430 add_object_array(p
, NULL
, &revs
->boundary_commits
);
3436 struct commit
*get_revision(struct rev_info
*revs
)
3439 struct commit_list
*reversed
;
3441 if (revs
->reverse
) {
3443 while ((c
= get_revision_internal(revs
)))
3444 commit_list_insert(c
, &reversed
);
3445 revs
->commits
= reversed
;
3447 revs
->reverse_output_stage
= 1;
3450 if (revs
->reverse_output_stage
) {
3451 c
= pop_commit(&revs
->commits
);
3452 if (revs
->track_linear
)
3453 revs
->linear
= !!(c
&& c
->object
.flags
& TRACK_LINEAR
);
3457 c
= get_revision_internal(revs
);
3458 if (c
&& revs
->graph
)
3459 graph_update(revs
->graph
, c
);
3461 free_saved_parents(revs
);
3462 if (revs
->previous_parents
) {
3463 free_commit_list(revs
->previous_parents
);
3464 revs
->previous_parents
= NULL
;
3470 char *get_revision_mark(const struct rev_info
*revs
, const struct commit
*commit
)
3472 if (commit
->object
.flags
& BOUNDARY
)
3474 else if (commit
->object
.flags
& UNINTERESTING
)
3476 else if (commit
->object
.flags
& PATCHSAME
)
3478 else if (!revs
|| revs
->left_right
) {
3479 if (commit
->object
.flags
& SYMMETRIC_LEFT
)
3483 } else if (revs
->graph
)
3485 else if (revs
->cherry_mark
)
3490 void put_revision_mark(const struct rev_info
*revs
, const struct commit
*commit
)
3492 char *mark
= get_revision_mark(revs
, commit
);
3495 fputs(mark
, stdout
);