11 #include "reflog-walk.h"
12 #include "patch-ids.h"
15 #include "string-list.h"
18 #include "commit-slab.h"
20 #include "cache-tree.h"
24 #include "argv-array.h"
26 volatile show_early_output_fn_t show_early_output
;
28 static const char *term_bad
;
29 static const char *term_good
;
31 void show_object_with_name(FILE *out
, struct object
*obj
, const char *name
)
35 fprintf(out
, "%s ", oid_to_hex(&obj
->oid
));
36 for (p
= name
; *p
&& *p
!= '\n'; p
++)
41 static void mark_blob_uninteresting(struct blob
*blob
)
45 if (blob
->object
.flags
& UNINTERESTING
)
47 blob
->object
.flags
|= UNINTERESTING
;
50 static void mark_tree_contents_uninteresting(struct tree
*tree
)
52 struct tree_desc desc
;
53 struct name_entry entry
;
55 if (parse_tree_gently(tree
, 1) < 0)
58 init_tree_desc(&desc
, tree
->buffer
, tree
->size
);
59 while (tree_entry(&desc
, &entry
)) {
60 switch (object_type(entry
.mode
)) {
62 mark_tree_uninteresting(lookup_tree(entry
.oid
));
65 mark_blob_uninteresting(lookup_blob(entry
.oid
));
68 /* Subproject commit - not in this repository */
74 * We don't care about the tree any more
75 * after it has been marked uninteresting.
77 free_tree_buffer(tree
);
80 void mark_tree_uninteresting(struct tree
*tree
)
88 if (obj
->flags
& UNINTERESTING
)
90 obj
->flags
|= UNINTERESTING
;
91 mark_tree_contents_uninteresting(tree
);
94 void mark_parents_uninteresting(struct commit
*commit
)
96 struct commit_list
*parents
= NULL
, *l
;
98 for (l
= commit
->parents
; l
; l
= l
->next
)
99 commit_list_insert(l
->item
, &parents
);
102 struct commit
*commit
= pop_commit(&parents
);
105 if (commit
->object
.flags
& UNINTERESTING
)
108 commit
->object
.flags
|= UNINTERESTING
;
111 * Normally we haven't parsed the parent
112 * yet, so we won't have a parent of a parent
113 * here. However, it may turn out that we've
114 * reached this commit some other way (where it
115 * wasn't uninteresting), in which case we need
116 * to mark its parents recursively too..
118 if (!commit
->parents
)
121 for (l
= commit
->parents
->next
; l
; l
= l
->next
)
122 commit_list_insert(l
->item
, &parents
);
123 commit
= commit
->parents
->item
;
128 static void add_pending_object_with_path(struct rev_info
*revs
,
130 const char *name
, unsigned mode
,
135 if (revs
->no_walk
&& (obj
->flags
& UNINTERESTING
))
137 if (revs
->reflog_info
&& obj
->type
== OBJ_COMMIT
) {
138 struct strbuf buf
= STRBUF_INIT
;
139 int len
= interpret_branch_name(name
, 0, &buf
, 0);
141 if (0 < len
&& name
[len
] && buf
.len
)
142 strbuf_addstr(&buf
, name
+ len
);
143 add_reflog_for_walk(revs
->reflog_info
,
144 (struct commit
*)obj
,
145 buf
.buf
[0] ? buf
.buf
: name
);
146 strbuf_release(&buf
);
147 return; /* do not add the commit itself */
149 add_object_array_with_path(obj
, name
, &revs
->pending
, mode
, path
);
152 static void add_pending_object_with_mode(struct rev_info
*revs
,
154 const char *name
, unsigned mode
)
156 add_pending_object_with_path(revs
, obj
, name
, mode
, NULL
);
159 void add_pending_object(struct rev_info
*revs
,
160 struct object
*obj
, const char *name
)
162 add_pending_object_with_mode(revs
, obj
, name
, S_IFINVALID
);
165 void add_head_to_pending(struct rev_info
*revs
)
167 struct object_id oid
;
169 if (get_oid("HEAD", &oid
))
171 obj
= parse_object(&oid
);
174 add_pending_object(revs
, obj
, "HEAD");
177 static struct object
*get_reference(struct rev_info
*revs
, const char *name
,
178 const struct object_id
*oid
,
181 struct object
*object
;
183 object
= parse_object(oid
);
185 if (revs
->ignore_missing
)
187 if (revs
->exclude_promisor_objects
&& is_promisor_object(oid
))
189 die("bad object %s", name
);
191 object
->flags
|= flags
;
195 void add_pending_oid(struct rev_info
*revs
, const char *name
,
196 const struct object_id
*oid
, unsigned int flags
)
198 struct object
*object
= get_reference(revs
, name
, oid
, flags
);
199 add_pending_object(revs
, object
, name
);
202 static struct commit
*handle_commit(struct rev_info
*revs
,
203 struct object_array_entry
*entry
)
205 struct object
*object
= entry
->item
;
206 const char *name
= entry
->name
;
207 const char *path
= entry
->path
;
208 unsigned int mode
= entry
->mode
;
209 unsigned long flags
= object
->flags
;
212 * Tag object? Look what it points to..
214 while (object
->type
== OBJ_TAG
) {
215 struct tag
*tag
= (struct tag
*) object
;
216 if (revs
->tag_objects
&& !(flags
& UNINTERESTING
))
217 add_pending_object(revs
, object
, tag
->tag
);
220 object
= parse_object(&tag
->tagged
->oid
);
222 if (revs
->ignore_missing_links
|| (flags
& UNINTERESTING
))
224 die("bad object %s", oid_to_hex(&tag
->tagged
->oid
));
226 object
->flags
|= flags
;
228 * We'll handle the tagged object by looping or dropping
229 * through to the non-tag handlers below. Do not
230 * propagate path data from the tag's pending entry.
237 * Commit object? Just return it, we'll do all the complex
240 if (object
->type
== OBJ_COMMIT
) {
241 struct commit
*commit
= (struct commit
*)object
;
242 if (parse_commit(commit
) < 0)
243 die("unable to parse commit %s", name
);
244 if (flags
& UNINTERESTING
) {
245 mark_parents_uninteresting(commit
);
248 if (revs
->show_source
&& !commit
->util
)
249 commit
->util
= xstrdup(name
);
254 * Tree object? Either mark it uninteresting, or add it
255 * to the list of objects to look at later..
257 if (object
->type
== OBJ_TREE
) {
258 struct tree
*tree
= (struct tree
*)object
;
259 if (!revs
->tree_objects
)
261 if (flags
& UNINTERESTING
) {
262 mark_tree_contents_uninteresting(tree
);
265 add_pending_object_with_path(revs
, object
, name
, mode
, path
);
270 * Blob object? You know the drill by now..
272 if (object
->type
== OBJ_BLOB
) {
273 if (!revs
->blob_objects
)
275 if (flags
& UNINTERESTING
)
277 add_pending_object_with_path(revs
, object
, name
, mode
, path
);
280 die("%s is unknown object", name
);
283 static int everybody_uninteresting(struct commit_list
*orig
,
284 struct commit
**interesting_cache
)
286 struct commit_list
*list
= orig
;
288 if (*interesting_cache
) {
289 struct commit
*commit
= *interesting_cache
;
290 if (!(commit
->object
.flags
& UNINTERESTING
))
295 struct commit
*commit
= list
->item
;
297 if (commit
->object
.flags
& UNINTERESTING
)
300 *interesting_cache
= commit
;
307 * A definition of "relevant" commit that we can use to simplify limited graphs
308 * by eliminating side branches.
310 * A "relevant" commit is one that is !UNINTERESTING (ie we are including it
311 * in our list), or that is a specified BOTTOM commit. Then after computing
312 * a limited list, during processing we can generally ignore boundary merges
313 * coming from outside the graph, (ie from irrelevant parents), and treat
314 * those merges as if they were single-parent. TREESAME is defined to consider
315 * only relevant parents, if any. If we are TREESAME to our on-graph parents,
316 * we don't care if we were !TREESAME to non-graph parents.
318 * Treating bottom commits as relevant ensures that a limited graph's
319 * connection to the actual bottom commit is not viewed as a side branch, but
320 * treated as part of the graph. For example:
322 * ....Z...A---X---o---o---B
326 * When computing "A..B", the A-X connection is at least as important as
327 * Y-X, despite A being flagged UNINTERESTING.
329 * And when computing --ancestry-path "A..B", the A-X connection is more
330 * important than Y-X, despite both A and Y being flagged UNINTERESTING.
332 static inline int relevant_commit(struct commit
*commit
)
334 return (commit
->object
.flags
& (UNINTERESTING
| BOTTOM
)) != UNINTERESTING
;
338 * Return a single relevant commit from a parent list. If we are a TREESAME
339 * commit, and this selects one of our parents, then we can safely simplify to
342 static struct commit
*one_relevant_parent(const struct rev_info
*revs
,
343 struct commit_list
*orig
)
345 struct commit_list
*list
= orig
;
346 struct commit
*relevant
= NULL
;
352 * For 1-parent commits, or if first-parent-only, then return that
353 * first parent (even if not "relevant" by the above definition).
354 * TREESAME will have been set purely on that parent.
356 if (revs
->first_parent_only
|| !orig
->next
)
360 * For multi-parent commits, identify a sole relevant parent, if any.
361 * If we have only one relevant parent, then TREESAME will be set purely
362 * with regard to that parent, and we can simplify accordingly.
364 * If we have more than one relevant parent, or no relevant parents
365 * (and multiple irrelevant ones), then we can't select a parent here
369 struct commit
*commit
= list
->item
;
371 if (relevant_commit(commit
)) {
381 * The goal is to get REV_TREE_NEW as the result only if the
382 * diff consists of all '+' (and no other changes), REV_TREE_OLD
383 * if the whole diff is removal of old data, and otherwise
384 * REV_TREE_DIFFERENT (of course if the trees are the same we
385 * want REV_TREE_SAME).
387 * The only time we care about the distinction is when
388 * remove_empty_trees is in effect, in which case we care only about
389 * whether the whole change is REV_TREE_NEW, or if there's another type
390 * of change. Which means we can stop the diff early in either of these
393 * 1. We're not using remove_empty_trees at all.
395 * 2. We saw anything except REV_TREE_NEW.
397 static int tree_difference
= REV_TREE_SAME
;
399 static void file_add_remove(struct diff_options
*options
,
400 int addremove
, unsigned mode
,
401 const struct object_id
*oid
,
403 const char *fullpath
, unsigned dirty_submodule
)
405 int diff
= addremove
== '+' ? REV_TREE_NEW
: REV_TREE_OLD
;
406 struct rev_info
*revs
= options
->change_fn_data
;
408 tree_difference
|= diff
;
409 if (!revs
->remove_empty_trees
|| tree_difference
!= REV_TREE_NEW
)
410 options
->flags
.has_changes
= 1;
413 static void file_change(struct diff_options
*options
,
414 unsigned old_mode
, unsigned new_mode
,
415 const struct object_id
*old_oid
,
416 const struct object_id
*new_oid
,
417 int old_oid_valid
, int new_oid_valid
,
418 const char *fullpath
,
419 unsigned old_dirty_submodule
, unsigned new_dirty_submodule
)
421 tree_difference
= REV_TREE_DIFFERENT
;
422 options
->flags
.has_changes
= 1;
425 static int rev_compare_tree(struct rev_info
*revs
,
426 struct commit
*parent
, struct commit
*commit
)
428 struct tree
*t1
= parent
->tree
;
429 struct tree
*t2
= commit
->tree
;
436 if (revs
->simplify_by_decoration
) {
438 * If we are simplifying by decoration, then the commit
439 * is worth showing if it has a tag pointing at it.
441 if (get_name_decoration(&commit
->object
))
442 return REV_TREE_DIFFERENT
;
444 * A commit that is not pointed by a tag is uninteresting
445 * if we are not limited by path. This means that you will
446 * see the usual "commits that touch the paths" plus any
447 * tagged commit by specifying both --simplify-by-decoration
450 if (!revs
->prune_data
.nr
)
451 return REV_TREE_SAME
;
454 tree_difference
= REV_TREE_SAME
;
455 revs
->pruning
.flags
.has_changes
= 0;
456 if (diff_tree_oid(&t1
->object
.oid
, &t2
->object
.oid
, "",
458 return REV_TREE_DIFFERENT
;
459 return tree_difference
;
462 static int rev_same_tree_as_empty(struct rev_info
*revs
, struct commit
*commit
)
465 struct tree
*t1
= commit
->tree
;
470 tree_difference
= REV_TREE_SAME
;
471 revs
->pruning
.flags
.has_changes
= 0;
472 retval
= diff_tree_oid(NULL
, &t1
->object
.oid
, "", &revs
->pruning
);
474 return retval
>= 0 && (tree_difference
== REV_TREE_SAME
);
477 struct treesame_state
{
478 unsigned int nparents
;
479 unsigned char treesame
[FLEX_ARRAY
];
482 static struct treesame_state
*initialise_treesame(struct rev_info
*revs
, struct commit
*commit
)
484 unsigned n
= commit_list_count(commit
->parents
);
485 struct treesame_state
*st
= xcalloc(1, st_add(sizeof(*st
), n
));
487 add_decoration(&revs
->treesame
, &commit
->object
, st
);
492 * Must be called immediately after removing the nth_parent from a commit's
493 * parent list, if we are maintaining the per-parent treesame[] decoration.
494 * This does not recalculate the master TREESAME flag - update_treesame()
495 * should be called to update it after a sequence of treesame[] modifications
496 * that may have affected it.
498 static int compact_treesame(struct rev_info
*revs
, struct commit
*commit
, unsigned nth_parent
)
500 struct treesame_state
*st
;
503 if (!commit
->parents
) {
505 * Have just removed the only parent from a non-merge.
506 * Different handling, as we lack decoration.
509 die("compact_treesame %u", nth_parent
);
510 old_same
= !!(commit
->object
.flags
& TREESAME
);
511 if (rev_same_tree_as_empty(revs
, commit
))
512 commit
->object
.flags
|= TREESAME
;
514 commit
->object
.flags
&= ~TREESAME
;
518 st
= lookup_decoration(&revs
->treesame
, &commit
->object
);
519 if (!st
|| nth_parent
>= st
->nparents
)
520 die("compact_treesame %u", nth_parent
);
522 old_same
= st
->treesame
[nth_parent
];
523 memmove(st
->treesame
+ nth_parent
,
524 st
->treesame
+ nth_parent
+ 1,
525 st
->nparents
- nth_parent
- 1);
528 * If we've just become a non-merge commit, update TREESAME
529 * immediately, and remove the no-longer-needed decoration.
530 * If still a merge, defer update until update_treesame().
532 if (--st
->nparents
== 1) {
533 if (commit
->parents
->next
)
534 die("compact_treesame parents mismatch");
535 if (st
->treesame
[0] && revs
->dense
)
536 commit
->object
.flags
|= TREESAME
;
538 commit
->object
.flags
&= ~TREESAME
;
539 free(add_decoration(&revs
->treesame
, &commit
->object
, NULL
));
545 static unsigned update_treesame(struct rev_info
*revs
, struct commit
*commit
)
547 if (commit
->parents
&& commit
->parents
->next
) {
549 struct treesame_state
*st
;
550 struct commit_list
*p
;
551 unsigned relevant_parents
;
552 unsigned relevant_change
, irrelevant_change
;
554 st
= lookup_decoration(&revs
->treesame
, &commit
->object
);
556 die("update_treesame %s", oid_to_hex(&commit
->object
.oid
));
557 relevant_parents
= 0;
558 relevant_change
= irrelevant_change
= 0;
559 for (p
= commit
->parents
, n
= 0; p
; n
++, p
= p
->next
) {
560 if (relevant_commit(p
->item
)) {
561 relevant_change
|= !st
->treesame
[n
];
564 irrelevant_change
|= !st
->treesame
[n
];
566 if (relevant_parents
? relevant_change
: irrelevant_change
)
567 commit
->object
.flags
&= ~TREESAME
;
569 commit
->object
.flags
|= TREESAME
;
572 return commit
->object
.flags
& TREESAME
;
575 static inline int limiting_can_increase_treesame(const struct rev_info
*revs
)
578 * TREESAME is irrelevant unless prune && dense;
579 * if simplify_history is set, we can't have a mixture of TREESAME and
580 * !TREESAME INTERESTING parents (and we don't have treesame[]
581 * decoration anyway);
582 * if first_parent_only is set, then the TREESAME flag is locked
583 * against the first parent (and again we lack treesame[] decoration).
585 return revs
->prune
&& revs
->dense
&&
586 !revs
->simplify_history
&&
587 !revs
->first_parent_only
;
590 static void try_to_simplify_commit(struct rev_info
*revs
, struct commit
*commit
)
592 struct commit_list
**pp
, *parent
;
593 struct treesame_state
*ts
= NULL
;
594 int relevant_change
= 0, irrelevant_change
= 0;
595 int relevant_parents
, nth_parent
;
598 * If we don't do pruning, everything is interesting
606 if (!commit
->parents
) {
607 if (rev_same_tree_as_empty(revs
, commit
))
608 commit
->object
.flags
|= TREESAME
;
613 * Normal non-merge commit? If we don't want to make the
614 * history dense, we consider it always to be a change..
616 if (!revs
->dense
&& !commit
->parents
->next
)
619 for (pp
= &commit
->parents
, nth_parent
= 0, relevant_parents
= 0;
620 (parent
= *pp
) != NULL
;
621 pp
= &parent
->next
, nth_parent
++) {
622 struct commit
*p
= parent
->item
;
623 if (relevant_commit(p
))
626 if (nth_parent
== 1) {
628 * This our second loop iteration - so we now know
629 * we're dealing with a merge.
631 * Do not compare with later parents when we care only about
632 * the first parent chain, in order to avoid derailing the
633 * traversal to follow a side branch that brought everything
634 * in the path we are limited to by the pathspec.
636 if (revs
->first_parent_only
)
639 * If this will remain a potentially-simplifiable
640 * merge, remember per-parent treesame if needed.
641 * Initialise the array with the comparison from our
644 if (revs
->treesame
.name
&&
645 !revs
->simplify_history
&&
646 !(commit
->object
.flags
& UNINTERESTING
)) {
647 ts
= initialise_treesame(revs
, commit
);
648 if (!(irrelevant_change
|| relevant_change
))
652 if (parse_commit(p
) < 0)
653 die("cannot simplify commit %s (because of %s)",
654 oid_to_hex(&commit
->object
.oid
),
655 oid_to_hex(&p
->object
.oid
));
656 switch (rev_compare_tree(revs
, p
, commit
)) {
658 if (!revs
->simplify_history
|| !relevant_commit(p
)) {
659 /* Even if a merge with an uninteresting
660 * side branch brought the entire change
661 * we are interested in, we do not want
662 * to lose the other branches of this
663 * merge, so we just keep going.
666 ts
->treesame
[nth_parent
] = 1;
670 commit
->parents
= parent
;
671 commit
->object
.flags
|= TREESAME
;
675 if (revs
->remove_empty_trees
&&
676 rev_same_tree_as_empty(revs
, p
)) {
677 /* We are adding all the specified
678 * paths from this parent, so the
679 * history beyond this parent is not
680 * interesting. Remove its parents
681 * (they are grandparents for us).
682 * IOW, we pretend this parent is a
685 if (parse_commit(p
) < 0)
686 die("cannot simplify commit %s (invalid %s)",
687 oid_to_hex(&commit
->object
.oid
),
688 oid_to_hex(&p
->object
.oid
));
693 case REV_TREE_DIFFERENT
:
694 if (relevant_commit(p
))
697 irrelevant_change
= 1;
700 die("bad tree compare for commit %s", oid_to_hex(&commit
->object
.oid
));
704 * TREESAME is straightforward for single-parent commits. For merge
705 * commits, it is most useful to define it so that "irrelevant"
706 * parents cannot make us !TREESAME - if we have any relevant
707 * parents, then we only consider TREESAMEness with respect to them,
708 * allowing irrelevant merges from uninteresting branches to be
709 * simplified away. Only if we have only irrelevant parents do we
710 * base TREESAME on them. Note that this logic is replicated in
711 * update_treesame, which should be kept in sync.
713 if (relevant_parents
? !relevant_change
: !irrelevant_change
)
714 commit
->object
.flags
|= TREESAME
;
717 static void commit_list_insert_by_date_cached(struct commit
*p
, struct commit_list
**head
,
718 struct commit_list
*cached_base
, struct commit_list
**cache
)
720 struct commit_list
*new_entry
;
722 if (cached_base
&& p
->date
< cached_base
->item
->date
)
723 new_entry
= commit_list_insert_by_date(p
, &cached_base
->next
);
725 new_entry
= commit_list_insert_by_date(p
, head
);
727 if (cache
&& (!*cache
|| p
->date
< (*cache
)->item
->date
))
731 static int add_parents_to_list(struct rev_info
*revs
, struct commit
*commit
,
732 struct commit_list
**list
, struct commit_list
**cache_ptr
)
734 struct commit_list
*parent
= commit
->parents
;
736 struct commit_list
*cached_base
= cache_ptr
? *cache_ptr
: NULL
;
738 if (commit
->object
.flags
& ADDED
)
740 commit
->object
.flags
|= ADDED
;
742 if (revs
->include_check
&&
743 !revs
->include_check(commit
, revs
->include_check_data
))
747 * If the commit is uninteresting, don't try to
748 * prune parents - we want the maximal uninteresting
751 * Normally we haven't parsed the parent
752 * yet, so we won't have a parent of a parent
753 * here. However, it may turn out that we've
754 * reached this commit some other way (where it
755 * wasn't uninteresting), in which case we need
756 * to mark its parents recursively too..
758 if (commit
->object
.flags
& UNINTERESTING
) {
760 struct commit
*p
= parent
->item
;
761 parent
= parent
->next
;
763 p
->object
.flags
|= UNINTERESTING
;
764 if (parse_commit_gently(p
, 1) < 0)
767 mark_parents_uninteresting(p
);
768 if (p
->object
.flags
& SEEN
)
770 p
->object
.flags
|= SEEN
;
771 commit_list_insert_by_date_cached(p
, list
, cached_base
, cache_ptr
);
777 * Ok, the commit wasn't uninteresting. Try to
778 * simplify the commit history and find the parent
779 * that has no differences in the path set if one exists.
781 try_to_simplify_commit(revs
, commit
);
786 left_flag
= (commit
->object
.flags
& SYMMETRIC_LEFT
);
788 for (parent
= commit
->parents
; parent
; parent
= parent
->next
) {
789 struct commit
*p
= parent
->item
;
790 int gently
= revs
->ignore_missing_links
||
791 revs
->exclude_promisor_objects
;
792 if (parse_commit_gently(p
, gently
) < 0) {
793 if (revs
->exclude_promisor_objects
&&
794 is_promisor_object(&p
->object
.oid
)) {
795 if (revs
->first_parent_only
)
801 if (revs
->show_source
&& !p
->util
)
802 p
->util
= commit
->util
;
803 p
->object
.flags
|= left_flag
;
804 if (!(p
->object
.flags
& SEEN
)) {
805 p
->object
.flags
|= SEEN
;
806 commit_list_insert_by_date_cached(p
, list
, cached_base
, cache_ptr
);
808 if (revs
->first_parent_only
)
814 static void cherry_pick_list(struct commit_list
*list
, struct rev_info
*revs
)
816 struct commit_list
*p
;
817 int left_count
= 0, right_count
= 0;
819 struct patch_ids ids
;
820 unsigned cherry_flag
;
822 /* First count the commits on the left and on the right */
823 for (p
= list
; p
; p
= p
->next
) {
824 struct commit
*commit
= p
->item
;
825 unsigned flags
= commit
->object
.flags
;
826 if (flags
& BOUNDARY
)
828 else if (flags
& SYMMETRIC_LEFT
)
834 if (!left_count
|| !right_count
)
837 left_first
= left_count
< right_count
;
838 init_patch_ids(&ids
);
839 ids
.diffopts
.pathspec
= revs
->diffopt
.pathspec
;
841 /* Compute patch-ids for one side */
842 for (p
= list
; p
; p
= p
->next
) {
843 struct commit
*commit
= p
->item
;
844 unsigned flags
= commit
->object
.flags
;
846 if (flags
& BOUNDARY
)
849 * If we have fewer left, left_first is set and we omit
850 * commits on the right branch in this loop. If we have
851 * fewer right, we skip the left ones.
853 if (left_first
!= !!(flags
& SYMMETRIC_LEFT
))
855 add_commit_patch_id(commit
, &ids
);
858 /* either cherry_mark or cherry_pick are true */
859 cherry_flag
= revs
->cherry_mark
? PATCHSAME
: SHOWN
;
861 /* Check the other side */
862 for (p
= list
; p
; p
= p
->next
) {
863 struct commit
*commit
= p
->item
;
865 unsigned flags
= commit
->object
.flags
;
867 if (flags
& BOUNDARY
)
870 * If we have fewer left, left_first is set and we omit
871 * commits on the left branch in this loop.
873 if (left_first
== !!(flags
& SYMMETRIC_LEFT
))
877 * Have we seen the same patch id?
879 id
= has_commit_patch_id(commit
, &ids
);
883 commit
->object
.flags
|= cherry_flag
;
884 id
->commit
->object
.flags
|= cherry_flag
;
887 free_patch_ids(&ids
);
890 /* How many extra uninteresting commits we want to see.. */
893 static int still_interesting(struct commit_list
*src
, timestamp_t date
, int slop
,
894 struct commit
**interesting_cache
)
897 * No source list at all? We're definitely done..
903 * Does the destination list contain entries with a date
904 * before the source list? Definitely _not_ done.
906 if (date
<= src
->item
->date
)
910 * Does the source list still have interesting commits in
911 * it? Definitely not done..
913 if (!everybody_uninteresting(src
, interesting_cache
))
916 /* Ok, we're closing in.. */
921 * "rev-list --ancestry-path A..B" computes commits that are ancestors
922 * of B but not ancestors of A but further limits the result to those
923 * that are descendants of A. This takes the list of bottom commits and
924 * the result of "A..B" without --ancestry-path, and limits the latter
925 * further to the ones that can reach one of the commits in "bottom".
927 static void limit_to_ancestry(struct commit_list
*bottom
, struct commit_list
*list
)
929 struct commit_list
*p
;
930 struct commit_list
*rlist
= NULL
;
934 * Reverse the list so that it will be likely that we would
935 * process parents before children.
937 for (p
= list
; p
; p
= p
->next
)
938 commit_list_insert(p
->item
, &rlist
);
940 for (p
= bottom
; p
; p
= p
->next
)
941 p
->item
->object
.flags
|= TMP_MARK
;
944 * Mark the ones that can reach bottom commits in "list",
945 * in a bottom-up fashion.
949 for (p
= rlist
; p
; p
= p
->next
) {
950 struct commit
*c
= p
->item
;
951 struct commit_list
*parents
;
952 if (c
->object
.flags
& (TMP_MARK
| UNINTERESTING
))
954 for (parents
= c
->parents
;
956 parents
= parents
->next
) {
957 if (!(parents
->item
->object
.flags
& TMP_MARK
))
959 c
->object
.flags
|= TMP_MARK
;
964 } while (made_progress
);
967 * NEEDSWORK: decide if we want to remove parents that are
968 * not marked with TMP_MARK from commit->parents for commits
969 * in the resulting list. We may not want to do that, though.
973 * The ones that are not marked with TMP_MARK are uninteresting
975 for (p
= list
; p
; p
= p
->next
) {
976 struct commit
*c
= p
->item
;
977 if (c
->object
.flags
& TMP_MARK
)
979 c
->object
.flags
|= UNINTERESTING
;
982 /* We are done with the TMP_MARK */
983 for (p
= list
; p
; p
= p
->next
)
984 p
->item
->object
.flags
&= ~TMP_MARK
;
985 for (p
= bottom
; p
; p
= p
->next
)
986 p
->item
->object
.flags
&= ~TMP_MARK
;
987 free_commit_list(rlist
);
991 * Before walking the history, keep the set of "negative" refs the
992 * caller has asked to exclude.
994 * This is used to compute "rev-list --ancestry-path A..B", as we need
995 * to filter the result of "A..B" further to the ones that can actually
998 static struct commit_list
*collect_bottom_commits(struct commit_list
*list
)
1000 struct commit_list
*elem
, *bottom
= NULL
;
1001 for (elem
= list
; elem
; elem
= elem
->next
)
1002 if (elem
->item
->object
.flags
& BOTTOM
)
1003 commit_list_insert(elem
->item
, &bottom
);
1007 /* Assumes either left_only or right_only is set */
1008 static void limit_left_right(struct commit_list
*list
, struct rev_info
*revs
)
1010 struct commit_list
*p
;
1012 for (p
= list
; p
; p
= p
->next
) {
1013 struct commit
*commit
= p
->item
;
1015 if (revs
->right_only
) {
1016 if (commit
->object
.flags
& SYMMETRIC_LEFT
)
1017 commit
->object
.flags
|= SHOWN
;
1018 } else /* revs->left_only is set */
1019 if (!(commit
->object
.flags
& SYMMETRIC_LEFT
))
1020 commit
->object
.flags
|= SHOWN
;
1024 static int limit_list(struct rev_info
*revs
)
1027 timestamp_t date
= TIME_MAX
;
1028 struct commit_list
*list
= revs
->commits
;
1029 struct commit_list
*newlist
= NULL
;
1030 struct commit_list
**p
= &newlist
;
1031 struct commit_list
*bottom
= NULL
;
1032 struct commit
*interesting_cache
= NULL
;
1034 if (revs
->ancestry_path
) {
1035 bottom
= collect_bottom_commits(list
);
1037 die("--ancestry-path given but there are no bottom commits");
1041 struct commit
*commit
= pop_commit(&list
);
1042 struct object
*obj
= &commit
->object
;
1043 show_early_output_fn_t show
;
1045 if (commit
== interesting_cache
)
1046 interesting_cache
= NULL
;
1048 if (revs
->max_age
!= -1 && (commit
->date
< revs
->max_age
))
1049 obj
->flags
|= UNINTERESTING
;
1050 if (add_parents_to_list(revs
, commit
, &list
, NULL
) < 0)
1052 if (obj
->flags
& UNINTERESTING
) {
1053 mark_parents_uninteresting(commit
);
1054 slop
= still_interesting(list
, date
, slop
, &interesting_cache
);
1059 if (revs
->min_age
!= -1 && (commit
->date
> revs
->min_age
))
1061 date
= commit
->date
;
1062 p
= &commit_list_insert(commit
, p
)->next
;
1064 show
= show_early_output
;
1068 show(revs
, newlist
);
1069 show_early_output
= NULL
;
1071 if (revs
->cherry_pick
|| revs
->cherry_mark
)
1072 cherry_pick_list(newlist
, revs
);
1074 if (revs
->left_only
|| revs
->right_only
)
1075 limit_left_right(newlist
, revs
);
1078 limit_to_ancestry(bottom
, newlist
);
1079 free_commit_list(bottom
);
1083 * Check if any commits have become TREESAME by some of their parents
1084 * becoming UNINTERESTING.
1086 if (limiting_can_increase_treesame(revs
))
1087 for (list
= newlist
; list
; list
= list
->next
) {
1088 struct commit
*c
= list
->item
;
1089 if (c
->object
.flags
& (UNINTERESTING
| TREESAME
))
1091 update_treesame(revs
, c
);
1094 revs
->commits
= newlist
;
1099 * Add an entry to refs->cmdline with the specified information.
1102 static void add_rev_cmdline(struct rev_info
*revs
,
1103 struct object
*item
,
1108 struct rev_cmdline_info
*info
= &revs
->cmdline
;
1109 unsigned int nr
= info
->nr
;
1111 ALLOC_GROW(info
->rev
, nr
+ 1, info
->alloc
);
1112 info
->rev
[nr
].item
= item
;
1113 info
->rev
[nr
].name
= xstrdup(name
);
1114 info
->rev
[nr
].whence
= whence
;
1115 info
->rev
[nr
].flags
= flags
;
1119 static void add_rev_cmdline_list(struct rev_info
*revs
,
1120 struct commit_list
*commit_list
,
1124 while (commit_list
) {
1125 struct object
*object
= &commit_list
->item
->object
;
1126 add_rev_cmdline(revs
, object
, oid_to_hex(&object
->oid
),
1128 commit_list
= commit_list
->next
;
1132 struct all_refs_cb
{
1134 int warned_bad_reflog
;
1135 struct rev_info
*all_revs
;
1136 const char *name_for_errormsg
;
1137 struct ref_store
*refs
;
1140 int ref_excluded(struct string_list
*ref_excludes
, const char *path
)
1142 struct string_list_item
*item
;
1146 for_each_string_list_item(item
, ref_excludes
) {
1147 if (!wildmatch(item
->string
, path
, 0))
1153 static int handle_one_ref(const char *path
, const struct object_id
*oid
,
1154 int flag
, void *cb_data
)
1156 struct all_refs_cb
*cb
= cb_data
;
1157 struct object
*object
;
1159 if (ref_excluded(cb
->all_revs
->ref_excludes
, path
))
1162 object
= get_reference(cb
->all_revs
, path
, oid
, cb
->all_flags
);
1163 add_rev_cmdline(cb
->all_revs
, object
, path
, REV_CMD_REF
, cb
->all_flags
);
1164 add_pending_oid(cb
->all_revs
, path
, oid
, cb
->all_flags
);
1168 static void init_all_refs_cb(struct all_refs_cb
*cb
, struct rev_info
*revs
,
1171 cb
->all_revs
= revs
;
1172 cb
->all_flags
= flags
;
1173 revs
->rev_input_given
= 1;
1177 void clear_ref_exclusion(struct string_list
**ref_excludes_p
)
1179 if (*ref_excludes_p
) {
1180 string_list_clear(*ref_excludes_p
, 0);
1181 free(*ref_excludes_p
);
1183 *ref_excludes_p
= NULL
;
1186 void add_ref_exclusion(struct string_list
**ref_excludes_p
, const char *exclude
)
1188 if (!*ref_excludes_p
) {
1189 *ref_excludes_p
= xcalloc(1, sizeof(**ref_excludes_p
));
1190 (*ref_excludes_p
)->strdup_strings
= 1;
1192 string_list_append(*ref_excludes_p
, exclude
);
1195 static void handle_refs(struct ref_store
*refs
,
1196 struct rev_info
*revs
, unsigned flags
,
1197 int (*for_each
)(struct ref_store
*, each_ref_fn
, void *))
1199 struct all_refs_cb cb
;
1202 /* this could happen with uninitialized submodules */
1206 init_all_refs_cb(&cb
, revs
, flags
);
1207 for_each(refs
, handle_one_ref
, &cb
);
1210 static void handle_one_reflog_commit(struct object_id
*oid
, void *cb_data
)
1212 struct all_refs_cb
*cb
= cb_data
;
1213 if (!is_null_oid(oid
)) {
1214 struct object
*o
= parse_object(oid
);
1216 o
->flags
|= cb
->all_flags
;
1217 /* ??? CMDLINEFLAGS ??? */
1218 add_pending_object(cb
->all_revs
, o
, "");
1220 else if (!cb
->warned_bad_reflog
) {
1221 warning("reflog of '%s' references pruned commits",
1222 cb
->name_for_errormsg
);
1223 cb
->warned_bad_reflog
= 1;
1228 static int handle_one_reflog_ent(struct object_id
*ooid
, struct object_id
*noid
,
1229 const char *email
, timestamp_t timestamp
, int tz
,
1230 const char *message
, void *cb_data
)
1232 handle_one_reflog_commit(ooid
, cb_data
);
1233 handle_one_reflog_commit(noid
, cb_data
);
1237 static int handle_one_reflog(const char *path
, const struct object_id
*oid
,
1238 int flag
, void *cb_data
)
1240 struct all_refs_cb
*cb
= cb_data
;
1241 cb
->warned_bad_reflog
= 0;
1242 cb
->name_for_errormsg
= path
;
1243 refs_for_each_reflog_ent(cb
->refs
, path
,
1244 handle_one_reflog_ent
, cb_data
);
1248 static void add_other_reflogs_to_pending(struct all_refs_cb
*cb
)
1250 struct worktree
**worktrees
, **p
;
1252 worktrees
= get_worktrees(0);
1253 for (p
= worktrees
; *p
; p
++) {
1254 struct worktree
*wt
= *p
;
1259 cb
->refs
= get_worktree_ref_store(wt
);
1260 refs_for_each_reflog(cb
->refs
,
1264 free_worktrees(worktrees
);
1267 void add_reflogs_to_pending(struct rev_info
*revs
, unsigned flags
)
1269 struct all_refs_cb cb
;
1272 cb
.all_flags
= flags
;
1273 cb
.refs
= get_main_ref_store();
1274 for_each_reflog(handle_one_reflog
, &cb
);
1276 if (!revs
->single_worktree
)
1277 add_other_reflogs_to_pending(&cb
);
1280 static void add_cache_tree(struct cache_tree
*it
, struct rev_info
*revs
,
1281 struct strbuf
*path
)
1283 size_t baselen
= path
->len
;
1286 if (it
->entry_count
>= 0) {
1287 struct tree
*tree
= lookup_tree(&it
->oid
);
1288 add_pending_object_with_path(revs
, &tree
->object
, "",
1292 for (i
= 0; i
< it
->subtree_nr
; i
++) {
1293 struct cache_tree_sub
*sub
= it
->down
[i
];
1294 strbuf_addf(path
, "%s%s", baselen
? "/" : "", sub
->name
);
1295 add_cache_tree(sub
->cache_tree
, revs
, path
);
1296 strbuf_setlen(path
, baselen
);
1301 static void do_add_index_objects_to_pending(struct rev_info
*revs
,
1302 struct index_state
*istate
)
1306 for (i
= 0; i
< istate
->cache_nr
; i
++) {
1307 struct cache_entry
*ce
= istate
->cache
[i
];
1310 if (S_ISGITLINK(ce
->ce_mode
))
1313 blob
= lookup_blob(&ce
->oid
);
1315 die("unable to add index blob to traversal");
1316 add_pending_object_with_path(revs
, &blob
->object
, "",
1317 ce
->ce_mode
, ce
->name
);
1320 if (istate
->cache_tree
) {
1321 struct strbuf path
= STRBUF_INIT
;
1322 add_cache_tree(istate
->cache_tree
, revs
, &path
);
1323 strbuf_release(&path
);
1327 void add_index_objects_to_pending(struct rev_info
*revs
, unsigned int flags
)
1329 struct worktree
**worktrees
, **p
;
1332 do_add_index_objects_to_pending(revs
, &the_index
);
1334 if (revs
->single_worktree
)
1337 worktrees
= get_worktrees(0);
1338 for (p
= worktrees
; *p
; p
++) {
1339 struct worktree
*wt
= *p
;
1340 struct index_state istate
= { NULL
};
1343 continue; /* current index already taken care of */
1345 if (read_index_from(&istate
,
1346 worktree_git_path(wt
, "index"),
1347 get_worktree_git_dir(wt
)) > 0)
1348 do_add_index_objects_to_pending(revs
, &istate
);
1349 discard_index(&istate
);
1351 free_worktrees(worktrees
);
1354 static int add_parents_only(struct rev_info
*revs
, const char *arg_
, int flags
,
1357 struct object_id oid
;
1359 struct commit
*commit
;
1360 struct commit_list
*parents
;
1362 const char *arg
= arg_
;
1365 flags
^= UNINTERESTING
| BOTTOM
;
1368 if (get_oid_committish(arg
, &oid
))
1371 it
= get_reference(revs
, arg
, &oid
, 0);
1372 if (!it
&& revs
->ignore_missing
)
1374 if (it
->type
!= OBJ_TAG
)
1376 if (!((struct tag
*)it
)->tagged
)
1378 oidcpy(&oid
, &((struct tag
*)it
)->tagged
->oid
);
1380 if (it
->type
!= OBJ_COMMIT
)
1382 commit
= (struct commit
*)it
;
1383 if (exclude_parent
&&
1384 exclude_parent
> commit_list_count(commit
->parents
))
1386 for (parents
= commit
->parents
, parent_number
= 1;
1388 parents
= parents
->next
, parent_number
++) {
1389 if (exclude_parent
&& parent_number
!= exclude_parent
)
1392 it
= &parents
->item
->object
;
1394 add_rev_cmdline(revs
, it
, arg_
, REV_CMD_PARENTS_ONLY
, flags
);
1395 add_pending_object(revs
, it
, arg
);
1400 void init_revisions(struct rev_info
*revs
, const char *prefix
)
1402 memset(revs
, 0, sizeof(*revs
));
1404 revs
->abbrev
= DEFAULT_ABBREV
;
1405 revs
->ignore_merges
= 1;
1406 revs
->simplify_history
= 1;
1407 revs
->pruning
.flags
.recursive
= 1;
1408 revs
->pruning
.flags
.quick
= 1;
1409 revs
->pruning
.add_remove
= file_add_remove
;
1410 revs
->pruning
.change
= file_change
;
1411 revs
->pruning
.change_fn_data
= revs
;
1412 revs
->sort_order
= REV_SORT_IN_GRAPH_ORDER
;
1414 revs
->prefix
= prefix
;
1417 revs
->skip_count
= -1;
1418 revs
->max_count
= -1;
1419 revs
->max_parents
= -1;
1420 revs
->expand_tabs_in_log
= -1;
1422 revs
->commit_format
= CMIT_FMT_DEFAULT
;
1423 revs
->expand_tabs_in_log_default
= 8;
1425 init_grep_defaults();
1426 grep_init(&revs
->grep_filter
, prefix
);
1427 revs
->grep_filter
.status_only
= 1;
1429 diff_setup(&revs
->diffopt
);
1430 if (prefix
&& !revs
->diffopt
.prefix
) {
1431 revs
->diffopt
.prefix
= prefix
;
1432 revs
->diffopt
.prefix_length
= strlen(prefix
);
1435 revs
->notes_opt
.use_default_notes
= -1;
1438 static void add_pending_commit_list(struct rev_info
*revs
,
1439 struct commit_list
*commit_list
,
1442 while (commit_list
) {
1443 struct object
*object
= &commit_list
->item
->object
;
1444 object
->flags
|= flags
;
1445 add_pending_object(revs
, object
, oid_to_hex(&object
->oid
));
1446 commit_list
= commit_list
->next
;
1450 static void prepare_show_merge(struct rev_info
*revs
)
1452 struct commit_list
*bases
;
1453 struct commit
*head
, *other
;
1454 struct object_id oid
;
1455 const char **prune
= NULL
;
1456 int i
, prune_num
= 1; /* counting terminating NULL */
1458 if (get_oid("HEAD", &oid
))
1459 die("--merge without HEAD?");
1460 head
= lookup_commit_or_die(&oid
, "HEAD");
1461 if (get_oid("MERGE_HEAD", &oid
))
1462 die("--merge without MERGE_HEAD?");
1463 other
= lookup_commit_or_die(&oid
, "MERGE_HEAD");
1464 add_pending_object(revs
, &head
->object
, "HEAD");
1465 add_pending_object(revs
, &other
->object
, "MERGE_HEAD");
1466 bases
= get_merge_bases(head
, other
);
1467 add_rev_cmdline_list(revs
, bases
, REV_CMD_MERGE_BASE
, UNINTERESTING
| BOTTOM
);
1468 add_pending_commit_list(revs
, bases
, UNINTERESTING
| BOTTOM
);
1469 free_commit_list(bases
);
1470 head
->object
.flags
|= SYMMETRIC_LEFT
;
1474 for (i
= 0; i
< active_nr
; i
++) {
1475 const struct cache_entry
*ce
= active_cache
[i
];
1478 if (ce_path_match(ce
, &revs
->prune_data
, NULL
)) {
1480 REALLOC_ARRAY(prune
, prune_num
);
1481 prune
[prune_num
-2] = ce
->name
;
1482 prune
[prune_num
-1] = NULL
;
1484 while ((i
+1 < active_nr
) &&
1485 ce_same_name(ce
, active_cache
[i
+1]))
1488 clear_pathspec(&revs
->prune_data
);
1489 parse_pathspec(&revs
->prune_data
, PATHSPEC_ALL_MAGIC
& ~PATHSPEC_LITERAL
,
1490 PATHSPEC_PREFER_FULL
| PATHSPEC_LITERAL_PATH
, "", prune
);
1494 static int dotdot_missing(const char *arg
, char *dotdot
,
1495 struct rev_info
*revs
, int symmetric
)
1497 if (revs
->ignore_missing
)
1499 /* de-munge so we report the full argument */
1502 ? "Invalid symmetric difference expression %s"
1503 : "Invalid revision range %s", arg
);
1506 static int handle_dotdot_1(const char *arg
, char *dotdot
,
1507 struct rev_info
*revs
, int flags
,
1508 int cant_be_filename
,
1509 struct object_context
*a_oc
,
1510 struct object_context
*b_oc
)
1512 const char *a_name
, *b_name
;
1513 struct object_id a_oid
, b_oid
;
1514 struct object
*a_obj
, *b_obj
;
1515 unsigned int a_flags
, b_flags
;
1517 unsigned int flags_exclude
= flags
^ (UNINTERESTING
| BOTTOM
);
1518 unsigned int oc_flags
= GET_OID_COMMITTISH
| GET_OID_RECORD_PATH
;
1524 b_name
= dotdot
+ 2;
1525 if (*b_name
== '.') {
1532 if (get_oid_with_context(a_name
, oc_flags
, &a_oid
, a_oc
) ||
1533 get_oid_with_context(b_name
, oc_flags
, &b_oid
, b_oc
))
1536 if (!cant_be_filename
) {
1538 verify_non_filename(revs
->prefix
, arg
);
1542 a_obj
= parse_object(&a_oid
);
1543 b_obj
= parse_object(&b_oid
);
1544 if (!a_obj
|| !b_obj
)
1545 return dotdot_missing(arg
, dotdot
, revs
, symmetric
);
1550 a_flags
= flags_exclude
;
1552 /* A...B -- find merge bases between the two */
1553 struct commit
*a
, *b
;
1554 struct commit_list
*exclude
;
1556 a
= lookup_commit_reference(&a_obj
->oid
);
1557 b
= lookup_commit_reference(&b_obj
->oid
);
1559 return dotdot_missing(arg
, dotdot
, revs
, symmetric
);
1561 exclude
= get_merge_bases(a
, b
);
1562 add_rev_cmdline_list(revs
, exclude
, REV_CMD_MERGE_BASE
,
1564 add_pending_commit_list(revs
, exclude
, flags_exclude
);
1565 free_commit_list(exclude
);
1568 a_flags
= flags
| SYMMETRIC_LEFT
;
1571 a_obj
->flags
|= a_flags
;
1572 b_obj
->flags
|= b_flags
;
1573 add_rev_cmdline(revs
, a_obj
, a_name
, REV_CMD_LEFT
, a_flags
);
1574 add_rev_cmdline(revs
, b_obj
, b_name
, REV_CMD_RIGHT
, b_flags
);
1575 add_pending_object_with_path(revs
, a_obj
, a_name
, a_oc
->mode
, a_oc
->path
);
1576 add_pending_object_with_path(revs
, b_obj
, b_name
, b_oc
->mode
, b_oc
->path
);
1580 static int handle_dotdot(const char *arg
,
1581 struct rev_info
*revs
, int flags
,
1582 int cant_be_filename
)
1584 struct object_context a_oc
, b_oc
;
1585 char *dotdot
= strstr(arg
, "..");
1591 memset(&a_oc
, 0, sizeof(a_oc
));
1592 memset(&b_oc
, 0, sizeof(b_oc
));
1595 ret
= handle_dotdot_1(arg
, dotdot
, revs
, flags
, cant_be_filename
,
1605 int handle_revision_arg(const char *arg_
, struct rev_info
*revs
, int flags
, unsigned revarg_opt
)
1607 struct object_context oc
;
1609 struct object
*object
;
1610 struct object_id oid
;
1612 const char *arg
= arg_
;
1613 int cant_be_filename
= revarg_opt
& REVARG_CANNOT_BE_FILENAME
;
1614 unsigned get_sha1_flags
= GET_OID_RECORD_PATH
;
1616 flags
= flags
& UNINTERESTING
? flags
| BOTTOM
: flags
& ~BOTTOM
;
1618 if (!cant_be_filename
&& !strcmp(arg
, "..")) {
1620 * Just ".."? That is not a range but the
1621 * pathspec for the parent directory.
1626 if (!handle_dotdot(arg
, revs
, flags
, revarg_opt
))
1629 mark
= strstr(arg
, "^@");
1630 if (mark
&& !mark
[2]) {
1632 if (add_parents_only(revs
, arg
, flags
, 0))
1636 mark
= strstr(arg
, "^!");
1637 if (mark
&& !mark
[2]) {
1639 if (!add_parents_only(revs
, arg
, flags
^ (UNINTERESTING
| BOTTOM
), 0))
1642 mark
= strstr(arg
, "^-");
1644 int exclude_parent
= 1;
1648 exclude_parent
= strtoul(mark
+ 2, &end
, 10);
1649 if (*end
!= '\0' || !exclude_parent
)
1654 if (!add_parents_only(revs
, arg
, flags
^ (UNINTERESTING
| BOTTOM
), exclude_parent
))
1660 local_flags
= UNINTERESTING
| BOTTOM
;
1664 if (revarg_opt
& REVARG_COMMITTISH
)
1665 get_sha1_flags
|= GET_OID_COMMITTISH
;
1667 if (get_oid_with_context(arg
, get_sha1_flags
, &oid
, &oc
))
1668 return revs
->ignore_missing
? 0 : -1;
1669 if (!cant_be_filename
)
1670 verify_non_filename(revs
->prefix
, arg
);
1671 object
= get_reference(revs
, arg
, &oid
, flags
^ local_flags
);
1672 add_rev_cmdline(revs
, object
, arg_
, REV_CMD_REV
, flags
^ local_flags
);
1673 add_pending_object_with_path(revs
, object
, arg
, oc
.mode
, oc
.path
);
1678 static void read_pathspec_from_stdin(struct rev_info
*revs
, struct strbuf
*sb
,
1679 struct argv_array
*prune
)
1681 while (strbuf_getline(sb
, stdin
) != EOF
)
1682 argv_array_push(prune
, sb
->buf
);
1685 static void read_revisions_from_stdin(struct rev_info
*revs
,
1686 struct argv_array
*prune
)
1689 int seen_dashdash
= 0;
1692 save_warning
= warn_on_object_refname_ambiguity
;
1693 warn_on_object_refname_ambiguity
= 0;
1695 strbuf_init(&sb
, 1000);
1696 while (strbuf_getline(&sb
, stdin
) != EOF
) {
1700 if (sb
.buf
[0] == '-') {
1701 if (len
== 2 && sb
.buf
[1] == '-') {
1705 die("options not supported in --stdin mode");
1707 if (handle_revision_arg(sb
.buf
, revs
, 0,
1708 REVARG_CANNOT_BE_FILENAME
))
1709 die("bad revision '%s'", sb
.buf
);
1712 read_pathspec_from_stdin(revs
, &sb
, prune
);
1714 strbuf_release(&sb
);
1715 warn_on_object_refname_ambiguity
= save_warning
;
1718 static void add_grep(struct rev_info
*revs
, const char *ptn
, enum grep_pat_token what
)
1720 append_grep_pattern(&revs
->grep_filter
, ptn
, "command line", 0, what
);
1723 static void add_header_grep(struct rev_info
*revs
, enum grep_header_field field
, const char *pattern
)
1725 append_header_grep_pattern(&revs
->grep_filter
, field
, pattern
);
1728 static void add_message_grep(struct rev_info
*revs
, const char *pattern
)
1730 add_grep(revs
, pattern
, GREP_PATTERN_BODY
);
1733 static int handle_revision_opt(struct rev_info
*revs
, int argc
, const char **argv
,
1734 int *unkc
, const char **unkv
)
1736 const char *arg
= argv
[0];
1740 /* pseudo revision arguments */
1741 if (!strcmp(arg
, "--all") || !strcmp(arg
, "--branches") ||
1742 !strcmp(arg
, "--tags") || !strcmp(arg
, "--remotes") ||
1743 !strcmp(arg
, "--reflog") || !strcmp(arg
, "--not") ||
1744 !strcmp(arg
, "--no-walk") || !strcmp(arg
, "--do-walk") ||
1745 !strcmp(arg
, "--bisect") || starts_with(arg
, "--glob=") ||
1746 !strcmp(arg
, "--indexed-objects") ||
1747 starts_with(arg
, "--exclude=") ||
1748 starts_with(arg
, "--branches=") || starts_with(arg
, "--tags=") ||
1749 starts_with(arg
, "--remotes=") || starts_with(arg
, "--no-walk="))
1751 unkv
[(*unkc
)++] = arg
;
1755 if ((argcount
= parse_long_opt("max-count", argv
, &optarg
))) {
1756 revs
->max_count
= atoi(optarg
);
1759 } else if ((argcount
= parse_long_opt("skip", argv
, &optarg
))) {
1760 revs
->skip_count
= atoi(optarg
);
1762 } else if ((*arg
== '-') && isdigit(arg
[1])) {
1763 /* accept -<digit>, like traditional "head" */
1764 if (strtol_i(arg
+ 1, 10, &revs
->max_count
) < 0 ||
1765 revs
->max_count
< 0)
1766 die("'%s': not a non-negative integer", arg
+ 1);
1768 } else if (!strcmp(arg
, "-n")) {
1770 return error("-n requires an argument");
1771 revs
->max_count
= atoi(argv
[1]);
1774 } else if (skip_prefix(arg
, "-n", &optarg
)) {
1775 revs
->max_count
= atoi(optarg
);
1777 } else if ((argcount
= parse_long_opt("max-age", argv
, &optarg
))) {
1778 revs
->max_age
= atoi(optarg
);
1780 } else if ((argcount
= parse_long_opt("since", argv
, &optarg
))) {
1781 revs
->max_age
= approxidate(optarg
);
1783 } else if ((argcount
= parse_long_opt("after", argv
, &optarg
))) {
1784 revs
->max_age
= approxidate(optarg
);
1786 } else if ((argcount
= parse_long_opt("min-age", argv
, &optarg
))) {
1787 revs
->min_age
= atoi(optarg
);
1789 } else if ((argcount
= parse_long_opt("before", argv
, &optarg
))) {
1790 revs
->min_age
= approxidate(optarg
);
1792 } else if ((argcount
= parse_long_opt("until", argv
, &optarg
))) {
1793 revs
->min_age
= approxidate(optarg
);
1795 } else if (!strcmp(arg
, "--first-parent")) {
1796 revs
->first_parent_only
= 1;
1797 } else if (!strcmp(arg
, "--ancestry-path")) {
1798 revs
->ancestry_path
= 1;
1799 revs
->simplify_history
= 0;
1801 } else if (!strcmp(arg
, "-g") || !strcmp(arg
, "--walk-reflogs")) {
1802 init_reflog_walk(&revs
->reflog_info
);
1803 } else if (!strcmp(arg
, "--default")) {
1805 return error("bad --default argument");
1806 revs
->def
= argv
[1];
1808 } else if (!strcmp(arg
, "--merge")) {
1809 revs
->show_merge
= 1;
1810 } else if (!strcmp(arg
, "--topo-order")) {
1811 revs
->sort_order
= REV_SORT_IN_GRAPH_ORDER
;
1812 revs
->topo_order
= 1;
1813 } else if (!strcmp(arg
, "--simplify-merges")) {
1814 revs
->simplify_merges
= 1;
1815 revs
->topo_order
= 1;
1816 revs
->rewrite_parents
= 1;
1817 revs
->simplify_history
= 0;
1819 } else if (!strcmp(arg
, "--simplify-by-decoration")) {
1820 revs
->simplify_merges
= 1;
1821 revs
->topo_order
= 1;
1822 revs
->rewrite_parents
= 1;
1823 revs
->simplify_history
= 0;
1824 revs
->simplify_by_decoration
= 1;
1827 load_ref_decorations(NULL
, DECORATE_SHORT_REFS
);
1828 } else if (!strcmp(arg
, "--date-order")) {
1829 revs
->sort_order
= REV_SORT_BY_COMMIT_DATE
;
1830 revs
->topo_order
= 1;
1831 } else if (!strcmp(arg
, "--author-date-order")) {
1832 revs
->sort_order
= REV_SORT_BY_AUTHOR_DATE
;
1833 revs
->topo_order
= 1;
1834 } else if (!strcmp(arg
, "--early-output")) {
1835 revs
->early_output
= 100;
1836 revs
->topo_order
= 1;
1837 } else if (skip_prefix(arg
, "--early-output=", &optarg
)) {
1838 if (strtoul_ui(optarg
, 10, &revs
->early_output
) < 0)
1839 die("'%s': not a non-negative integer", optarg
);
1840 revs
->topo_order
= 1;
1841 } else if (!strcmp(arg
, "--parents")) {
1842 revs
->rewrite_parents
= 1;
1843 revs
->print_parents
= 1;
1844 } else if (!strcmp(arg
, "--dense")) {
1846 } else if (!strcmp(arg
, "--sparse")) {
1848 } else if (!strcmp(arg
, "--in-commit-order")) {
1849 revs
->tree_blobs_in_commit_order
= 1;
1850 } else if (!strcmp(arg
, "--remove-empty")) {
1851 revs
->remove_empty_trees
= 1;
1852 } else if (!strcmp(arg
, "--merges")) {
1853 revs
->min_parents
= 2;
1854 } else if (!strcmp(arg
, "--no-merges")) {
1855 revs
->max_parents
= 1;
1856 } else if (skip_prefix(arg
, "--min-parents=", &optarg
)) {
1857 revs
->min_parents
= atoi(optarg
);
1858 } else if (!strcmp(arg
, "--no-min-parents")) {
1859 revs
->min_parents
= 0;
1860 } else if (skip_prefix(arg
, "--max-parents=", &optarg
)) {
1861 revs
->max_parents
= atoi(optarg
);
1862 } else if (!strcmp(arg
, "--no-max-parents")) {
1863 revs
->max_parents
= -1;
1864 } else if (!strcmp(arg
, "--boundary")) {
1866 } else if (!strcmp(arg
, "--left-right")) {
1867 revs
->left_right
= 1;
1868 } else if (!strcmp(arg
, "--left-only")) {
1869 if (revs
->right_only
)
1870 die("--left-only is incompatible with --right-only"
1872 revs
->left_only
= 1;
1873 } else if (!strcmp(arg
, "--right-only")) {
1874 if (revs
->left_only
)
1875 die("--right-only is incompatible with --left-only");
1876 revs
->right_only
= 1;
1877 } else if (!strcmp(arg
, "--cherry")) {
1878 if (revs
->left_only
)
1879 die("--cherry is incompatible with --left-only");
1880 revs
->cherry_mark
= 1;
1881 revs
->right_only
= 1;
1882 revs
->max_parents
= 1;
1884 } else if (!strcmp(arg
, "--count")) {
1886 } else if (!strcmp(arg
, "--cherry-mark")) {
1887 if (revs
->cherry_pick
)
1888 die("--cherry-mark is incompatible with --cherry-pick");
1889 revs
->cherry_mark
= 1;
1890 revs
->limited
= 1; /* needs limit_list() */
1891 } else if (!strcmp(arg
, "--cherry-pick")) {
1892 if (revs
->cherry_mark
)
1893 die("--cherry-pick is incompatible with --cherry-mark");
1894 revs
->cherry_pick
= 1;
1896 } else if (!strcmp(arg
, "--objects")) {
1897 revs
->tag_objects
= 1;
1898 revs
->tree_objects
= 1;
1899 revs
->blob_objects
= 1;
1900 } else if (!strcmp(arg
, "--objects-edge")) {
1901 revs
->tag_objects
= 1;
1902 revs
->tree_objects
= 1;
1903 revs
->blob_objects
= 1;
1904 revs
->edge_hint
= 1;
1905 } else if (!strcmp(arg
, "--objects-edge-aggressive")) {
1906 revs
->tag_objects
= 1;
1907 revs
->tree_objects
= 1;
1908 revs
->blob_objects
= 1;
1909 revs
->edge_hint
= 1;
1910 revs
->edge_hint_aggressive
= 1;
1911 } else if (!strcmp(arg
, "--verify-objects")) {
1912 revs
->tag_objects
= 1;
1913 revs
->tree_objects
= 1;
1914 revs
->blob_objects
= 1;
1915 revs
->verify_objects
= 1;
1916 } else if (!strcmp(arg
, "--unpacked")) {
1918 } else if (starts_with(arg
, "--unpacked=")) {
1919 die("--unpacked=<packfile> no longer supported.");
1920 } else if (!strcmp(arg
, "-r")) {
1922 revs
->diffopt
.flags
.recursive
= 1;
1923 } else if (!strcmp(arg
, "-t")) {
1925 revs
->diffopt
.flags
.recursive
= 1;
1926 revs
->diffopt
.flags
.tree_in_recursive
= 1;
1927 } else if (!strcmp(arg
, "-m")) {
1928 revs
->ignore_merges
= 0;
1929 } else if (!strcmp(arg
, "-c")) {
1931 revs
->dense_combined_merges
= 0;
1932 revs
->combine_merges
= 1;
1933 } else if (!strcmp(arg
, "--cc")) {
1935 revs
->dense_combined_merges
= 1;
1936 revs
->combine_merges
= 1;
1937 } else if (!strcmp(arg
, "-v")) {
1938 revs
->verbose_header
= 1;
1939 } else if (!strcmp(arg
, "--pretty")) {
1940 revs
->verbose_header
= 1;
1941 revs
->pretty_given
= 1;
1942 get_commit_format(NULL
, revs
);
1943 } else if (skip_prefix(arg
, "--pretty=", &optarg
) ||
1944 skip_prefix(arg
, "--format=", &optarg
)) {
1946 * Detached form ("--pretty X" as opposed to "--pretty=X")
1947 * not allowed, since the argument is optional.
1949 revs
->verbose_header
= 1;
1950 revs
->pretty_given
= 1;
1951 get_commit_format(optarg
, revs
);
1952 } else if (!strcmp(arg
, "--expand-tabs")) {
1953 revs
->expand_tabs_in_log
= 8;
1954 } else if (!strcmp(arg
, "--no-expand-tabs")) {
1955 revs
->expand_tabs_in_log
= 0;
1956 } else if (skip_prefix(arg
, "--expand-tabs=", &arg
)) {
1958 if (strtol_i(arg
, 10, &val
) < 0 || val
< 0)
1959 die("'%s': not a non-negative integer", arg
);
1960 revs
->expand_tabs_in_log
= val
;
1961 } else if (!strcmp(arg
, "--show-notes") || !strcmp(arg
, "--notes")) {
1962 revs
->show_notes
= 1;
1963 revs
->show_notes_given
= 1;
1964 revs
->notes_opt
.use_default_notes
= 1;
1965 } else if (!strcmp(arg
, "--show-signature")) {
1966 revs
->show_signature
= 1;
1967 } else if (!strcmp(arg
, "--no-show-signature")) {
1968 revs
->show_signature
= 0;
1969 } else if (!strcmp(arg
, "--show-linear-break")) {
1970 revs
->break_bar
= " ..........";
1971 revs
->track_linear
= 1;
1972 revs
->track_first_time
= 1;
1973 } else if (skip_prefix(arg
, "--show-linear-break=", &optarg
)) {
1974 revs
->break_bar
= xstrdup(optarg
);
1975 revs
->track_linear
= 1;
1976 revs
->track_first_time
= 1;
1977 } else if (skip_prefix(arg
, "--show-notes=", &optarg
) ||
1978 skip_prefix(arg
, "--notes=", &optarg
)) {
1979 struct strbuf buf
= STRBUF_INIT
;
1980 revs
->show_notes
= 1;
1981 revs
->show_notes_given
= 1;
1982 if (starts_with(arg
, "--show-notes=") &&
1983 revs
->notes_opt
.use_default_notes
< 0)
1984 revs
->notes_opt
.use_default_notes
= 1;
1985 strbuf_addstr(&buf
, optarg
);
1986 expand_notes_ref(&buf
);
1987 string_list_append(&revs
->notes_opt
.extra_notes_refs
,
1988 strbuf_detach(&buf
, NULL
));
1989 } else if (!strcmp(arg
, "--no-notes")) {
1990 revs
->show_notes
= 0;
1991 revs
->show_notes_given
= 1;
1992 revs
->notes_opt
.use_default_notes
= -1;
1993 /* we have been strdup'ing ourselves, so trick
1994 * string_list into free()ing strings */
1995 revs
->notes_opt
.extra_notes_refs
.strdup_strings
= 1;
1996 string_list_clear(&revs
->notes_opt
.extra_notes_refs
, 0);
1997 revs
->notes_opt
.extra_notes_refs
.strdup_strings
= 0;
1998 } else if (!strcmp(arg
, "--standard-notes")) {
1999 revs
->show_notes_given
= 1;
2000 revs
->notes_opt
.use_default_notes
= 1;
2001 } else if (!strcmp(arg
, "--no-standard-notes")) {
2002 revs
->notes_opt
.use_default_notes
= 0;
2003 } else if (!strcmp(arg
, "--oneline")) {
2004 revs
->verbose_header
= 1;
2005 get_commit_format("oneline", revs
);
2006 revs
->pretty_given
= 1;
2007 revs
->abbrev_commit
= 1;
2008 } else if (!strcmp(arg
, "--graph")) {
2009 revs
->topo_order
= 1;
2010 revs
->rewrite_parents
= 1;
2011 revs
->graph
= graph_init(revs
);
2012 } else if (!strcmp(arg
, "--root")) {
2013 revs
->show_root_diff
= 1;
2014 } else if (!strcmp(arg
, "--no-commit-id")) {
2015 revs
->no_commit_id
= 1;
2016 } else if (!strcmp(arg
, "--always")) {
2017 revs
->always_show_header
= 1;
2018 } else if (!strcmp(arg
, "--no-abbrev")) {
2020 } else if (!strcmp(arg
, "--abbrev")) {
2021 revs
->abbrev
= DEFAULT_ABBREV
;
2022 } else if (skip_prefix(arg
, "--abbrev=", &optarg
)) {
2023 revs
->abbrev
= strtoul(optarg
, NULL
, 10);
2024 if (revs
->abbrev
< MINIMUM_ABBREV
)
2025 revs
->abbrev
= MINIMUM_ABBREV
;
2026 else if (revs
->abbrev
> 40)
2028 } else if (!strcmp(arg
, "--abbrev-commit")) {
2029 revs
->abbrev_commit
= 1;
2030 revs
->abbrev_commit_given
= 1;
2031 } else if (!strcmp(arg
, "--no-abbrev-commit")) {
2032 revs
->abbrev_commit
= 0;
2033 } else if (!strcmp(arg
, "--full-diff")) {
2035 revs
->full_diff
= 1;
2036 } else if (!strcmp(arg
, "--full-history")) {
2037 revs
->simplify_history
= 0;
2038 } else if (!strcmp(arg
, "--relative-date")) {
2039 revs
->date_mode
.type
= DATE_RELATIVE
;
2040 revs
->date_mode_explicit
= 1;
2041 } else if ((argcount
= parse_long_opt("date", argv
, &optarg
))) {
2042 parse_date_format(optarg
, &revs
->date_mode
);
2043 revs
->date_mode_explicit
= 1;
2045 } else if (!strcmp(arg
, "--log-size")) {
2046 revs
->show_log_size
= 1;
2049 * Grepping the commit log
2051 else if ((argcount
= parse_long_opt("author", argv
, &optarg
))) {
2052 add_header_grep(revs
, GREP_HEADER_AUTHOR
, optarg
);
2054 } else if ((argcount
= parse_long_opt("committer", argv
, &optarg
))) {
2055 add_header_grep(revs
, GREP_HEADER_COMMITTER
, optarg
);
2057 } else if ((argcount
= parse_long_opt("grep-reflog", argv
, &optarg
))) {
2058 add_header_grep(revs
, GREP_HEADER_REFLOG
, optarg
);
2060 } else if ((argcount
= parse_long_opt("grep", argv
, &optarg
))) {
2061 add_message_grep(revs
, optarg
);
2063 } else if (!strcmp(arg
, "--grep-debug")) {
2064 revs
->grep_filter
.debug
= 1;
2065 } else if (!strcmp(arg
, "--basic-regexp")) {
2066 revs
->grep_filter
.pattern_type_option
= GREP_PATTERN_TYPE_BRE
;
2067 } else if (!strcmp(arg
, "--extended-regexp") || !strcmp(arg
, "-E")) {
2068 revs
->grep_filter
.pattern_type_option
= GREP_PATTERN_TYPE_ERE
;
2069 } else if (!strcmp(arg
, "--regexp-ignore-case") || !strcmp(arg
, "-i")) {
2070 revs
->grep_filter
.ignore_case
= 1;
2071 revs
->diffopt
.pickaxe_opts
|= DIFF_PICKAXE_IGNORE_CASE
;
2072 } else if (!strcmp(arg
, "--fixed-strings") || !strcmp(arg
, "-F")) {
2073 revs
->grep_filter
.pattern_type_option
= GREP_PATTERN_TYPE_FIXED
;
2074 } else if (!strcmp(arg
, "--perl-regexp") || !strcmp(arg
, "-P")) {
2075 revs
->grep_filter
.pattern_type_option
= GREP_PATTERN_TYPE_PCRE
;
2076 } else if (!strcmp(arg
, "--all-match")) {
2077 revs
->grep_filter
.all_match
= 1;
2078 } else if (!strcmp(arg
, "--invert-grep")) {
2079 revs
->invert_grep
= 1;
2080 } else if ((argcount
= parse_long_opt("encoding", argv
, &optarg
))) {
2081 if (strcmp(optarg
, "none"))
2082 git_log_output_encoding
= xstrdup(optarg
);
2084 git_log_output_encoding
= "";
2086 } else if (!strcmp(arg
, "--reverse")) {
2088 } else if (!strcmp(arg
, "--children")) {
2089 revs
->children
.name
= "children";
2091 } else if (!strcmp(arg
, "--ignore-missing")) {
2092 revs
->ignore_missing
= 1;
2093 } else if (!strcmp(arg
, "--exclude-promisor-objects")) {
2094 if (fetch_if_missing
)
2095 die("BUG: exclude_promisor_objects can only be used when fetch_if_missing is 0");
2096 revs
->exclude_promisor_objects
= 1;
2098 int opts
= diff_opt_parse(&revs
->diffopt
, argv
, argc
, revs
->prefix
);
2100 unkv
[(*unkc
)++] = arg
;
2103 if (revs
->graph
&& revs
->track_linear
)
2104 die("--show-linear-break and --graph are incompatible");
2109 void parse_revision_opt(struct rev_info
*revs
, struct parse_opt_ctx_t
*ctx
,
2110 const struct option
*options
,
2111 const char * const usagestr
[])
2113 int n
= handle_revision_opt(revs
, ctx
->argc
, ctx
->argv
,
2114 &ctx
->cpidx
, ctx
->out
);
2116 error("unknown option `%s'", ctx
->argv
[0]);
2117 usage_with_options(usagestr
, options
);
2123 static int for_each_bisect_ref(struct ref_store
*refs
, each_ref_fn fn
,
2124 void *cb_data
, const char *term
)
2126 struct strbuf bisect_refs
= STRBUF_INIT
;
2128 strbuf_addf(&bisect_refs
, "refs/bisect/%s", term
);
2129 status
= refs_for_each_fullref_in(refs
, bisect_refs
.buf
, fn
, cb_data
, 0);
2130 strbuf_release(&bisect_refs
);
2134 static int for_each_bad_bisect_ref(struct ref_store
*refs
, each_ref_fn fn
, void *cb_data
)
2136 return for_each_bisect_ref(refs
, fn
, cb_data
, term_bad
);
2139 static int for_each_good_bisect_ref(struct ref_store
*refs
, each_ref_fn fn
, void *cb_data
)
2141 return for_each_bisect_ref(refs
, fn
, cb_data
, term_good
);
2144 static int handle_revision_pseudo_opt(const char *submodule
,
2145 struct rev_info
*revs
,
2146 int argc
, const char **argv
, int *flags
)
2148 const char *arg
= argv
[0];
2150 struct ref_store
*refs
;
2155 * We need some something like get_submodule_worktrees()
2156 * before we can go through all worktrees of a submodule,
2157 * .e.g with adding all HEADs from --all, which is not
2158 * supported right now, so stick to single worktree.
2160 if (!revs
->single_worktree
)
2161 die("BUG: --single-worktree cannot be used together with submodule");
2162 refs
= get_submodule_ref_store(submodule
);
2164 refs
= get_main_ref_store();
2169 * Commands like "git shortlog" will not accept the options below
2170 * unless parse_revision_opt queues them (as opposed to erroring
2173 * When implementing your new pseudo-option, remember to
2174 * register it in the list at the top of handle_revision_opt.
2176 if (!strcmp(arg
, "--all")) {
2177 handle_refs(refs
, revs
, *flags
, refs_for_each_ref
);
2178 handle_refs(refs
, revs
, *flags
, refs_head_ref
);
2179 if (!revs
->single_worktree
) {
2180 struct all_refs_cb cb
;
2182 init_all_refs_cb(&cb
, revs
, *flags
);
2183 other_head_refs(handle_one_ref
, &cb
);
2185 clear_ref_exclusion(&revs
->ref_excludes
);
2186 } else if (!strcmp(arg
, "--branches")) {
2187 handle_refs(refs
, revs
, *flags
, refs_for_each_branch_ref
);
2188 clear_ref_exclusion(&revs
->ref_excludes
);
2189 } else if (!strcmp(arg
, "--bisect")) {
2190 read_bisect_terms(&term_bad
, &term_good
);
2191 handle_refs(refs
, revs
, *flags
, for_each_bad_bisect_ref
);
2192 handle_refs(refs
, revs
, *flags
^ (UNINTERESTING
| BOTTOM
),
2193 for_each_good_bisect_ref
);
2195 } else if (!strcmp(arg
, "--tags")) {
2196 handle_refs(refs
, revs
, *flags
, refs_for_each_tag_ref
);
2197 clear_ref_exclusion(&revs
->ref_excludes
);
2198 } else if (!strcmp(arg
, "--remotes")) {
2199 handle_refs(refs
, revs
, *flags
, refs_for_each_remote_ref
);
2200 clear_ref_exclusion(&revs
->ref_excludes
);
2201 } else if ((argcount
= parse_long_opt("glob", argv
, &optarg
))) {
2202 struct all_refs_cb cb
;
2203 init_all_refs_cb(&cb
, revs
, *flags
);
2204 for_each_glob_ref(handle_one_ref
, optarg
, &cb
);
2205 clear_ref_exclusion(&revs
->ref_excludes
);
2207 } else if ((argcount
= parse_long_opt("exclude", argv
, &optarg
))) {
2208 add_ref_exclusion(&revs
->ref_excludes
, optarg
);
2210 } else if (skip_prefix(arg
, "--branches=", &optarg
)) {
2211 struct all_refs_cb cb
;
2212 init_all_refs_cb(&cb
, revs
, *flags
);
2213 for_each_glob_ref_in(handle_one_ref
, optarg
, "refs/heads/", &cb
);
2214 clear_ref_exclusion(&revs
->ref_excludes
);
2215 } else if (skip_prefix(arg
, "--tags=", &optarg
)) {
2216 struct all_refs_cb cb
;
2217 init_all_refs_cb(&cb
, revs
, *flags
);
2218 for_each_glob_ref_in(handle_one_ref
, optarg
, "refs/tags/", &cb
);
2219 clear_ref_exclusion(&revs
->ref_excludes
);
2220 } else if (skip_prefix(arg
, "--remotes=", &optarg
)) {
2221 struct all_refs_cb cb
;
2222 init_all_refs_cb(&cb
, revs
, *flags
);
2223 for_each_glob_ref_in(handle_one_ref
, optarg
, "refs/remotes/", &cb
);
2224 clear_ref_exclusion(&revs
->ref_excludes
);
2225 } else if (!strcmp(arg
, "--reflog")) {
2226 add_reflogs_to_pending(revs
, *flags
);
2227 } else if (!strcmp(arg
, "--indexed-objects")) {
2228 add_index_objects_to_pending(revs
, *flags
);
2229 } else if (!strcmp(arg
, "--not")) {
2230 *flags
^= UNINTERESTING
| BOTTOM
;
2231 } else if (!strcmp(arg
, "--no-walk")) {
2232 revs
->no_walk
= REVISION_WALK_NO_WALK_SORTED
;
2233 } else if (skip_prefix(arg
, "--no-walk=", &optarg
)) {
2235 * Detached form ("--no-walk X" as opposed to "--no-walk=X")
2236 * not allowed, since the argument is optional.
2238 if (!strcmp(optarg
, "sorted"))
2239 revs
->no_walk
= REVISION_WALK_NO_WALK_SORTED
;
2240 else if (!strcmp(optarg
, "unsorted"))
2241 revs
->no_walk
= REVISION_WALK_NO_WALK_UNSORTED
;
2243 return error("invalid argument to --no-walk");
2244 } else if (!strcmp(arg
, "--do-walk")) {
2246 } else if (!strcmp(arg
, "--single-worktree")) {
2247 revs
->single_worktree
= 1;
2255 static void NORETURN
diagnose_missing_default(const char *def
)
2258 const char *refname
;
2260 refname
= resolve_ref_unsafe(def
, 0, NULL
, &flags
);
2261 if (!refname
|| !(flags
& REF_ISSYMREF
) || (flags
& REF_ISBROKEN
))
2262 die(_("your current branch appears to be broken"));
2264 skip_prefix(refname
, "refs/heads/", &refname
);
2265 die(_("your current branch '%s' does not have any commits yet"),
2270 * Parse revision information, filling in the "rev_info" structure,
2271 * and removing the used arguments from the argument list.
2273 * Returns the number of arguments left that weren't recognized
2274 * (which are also moved to the head of the argument list)
2276 int setup_revisions(int argc
, const char **argv
, struct rev_info
*revs
, struct setup_revision_opt
*opt
)
2278 int i
, flags
, left
, seen_dashdash
, read_from_stdin
, got_rev_arg
= 0, revarg_opt
;
2279 struct argv_array prune_data
= ARGV_ARRAY_INIT
;
2280 const char *submodule
= NULL
;
2283 submodule
= opt
->submodule
;
2285 /* First, search for "--" */
2286 if (opt
&& opt
->assume_dashdash
) {
2290 for (i
= 1; i
< argc
; i
++) {
2291 const char *arg
= argv
[i
];
2292 if (strcmp(arg
, "--"))
2297 argv_array_pushv(&prune_data
, argv
+ i
+ 1);
2303 /* Second, deal with arguments and options */
2305 revarg_opt
= opt
? opt
->revarg_opt
: 0;
2307 revarg_opt
|= REVARG_CANNOT_BE_FILENAME
;
2308 read_from_stdin
= 0;
2309 for (left
= i
= 1; i
< argc
; i
++) {
2310 const char *arg
= argv
[i
];
2314 opts
= handle_revision_pseudo_opt(submodule
,
2315 revs
, argc
- i
, argv
+ i
,
2322 if (!strcmp(arg
, "--stdin")) {
2323 if (revs
->disable_stdin
) {
2327 if (read_from_stdin
++)
2328 die("--stdin given twice?");
2329 read_revisions_from_stdin(revs
, &prune_data
);
2333 opts
= handle_revision_opt(revs
, argc
- i
, argv
+ i
, &left
, argv
);
2344 if (handle_revision_arg(arg
, revs
, flags
, revarg_opt
)) {
2346 if (seen_dashdash
|| *arg
== '^')
2347 die("bad revision '%s'", arg
);
2349 /* If we didn't have a "--":
2350 * (1) all filenames must exist;
2351 * (2) all rev-args must not be interpretable
2352 * as a valid filename.
2353 * but the latter we have checked in the main loop.
2355 for (j
= i
; j
< argc
; j
++)
2356 verify_filename(revs
->prefix
, argv
[j
], j
== i
);
2358 argv_array_pushv(&prune_data
, argv
+ i
);
2365 if (prune_data
.argc
) {
2367 * If we need to introduce the magic "a lone ':' means no
2368 * pathspec whatsoever", here is the place to do so.
2370 * if (prune_data.nr == 1 && !strcmp(prune_data[0], ":")) {
2371 * prune_data.nr = 0;
2372 * prune_data.alloc = 0;
2373 * free(prune_data.path);
2374 * prune_data.path = NULL;
2376 * terminate prune_data.alloc with NULL and
2377 * call init_pathspec() to set revs->prune_data here.
2380 parse_pathspec(&revs
->prune_data
, 0, 0,
2381 revs
->prefix
, prune_data
.argv
);
2383 argv_array_clear(&prune_data
);
2385 if (revs
->def
== NULL
)
2386 revs
->def
= opt
? opt
->def
: NULL
;
2387 if (opt
&& opt
->tweak
)
2388 opt
->tweak(revs
, opt
);
2389 if (revs
->show_merge
)
2390 prepare_show_merge(revs
);
2391 if (revs
->def
&& !revs
->pending
.nr
&& !revs
->rev_input_given
&& !got_rev_arg
) {
2392 struct object_id oid
;
2393 struct object
*object
;
2394 struct object_context oc
;
2395 if (get_oid_with_context(revs
->def
, 0, &oid
, &oc
))
2396 diagnose_missing_default(revs
->def
);
2397 object
= get_reference(revs
, revs
->def
, &oid
, 0);
2398 add_pending_object_with_mode(revs
, object
, revs
->def
, oc
.mode
);
2401 /* Did the user ask for any diff output? Run the diff! */
2402 if (revs
->diffopt
.output_format
& ~DIFF_FORMAT_NO_OUTPUT
)
2405 /* Pickaxe, diff-filter and rename following need diffs */
2406 if ((revs
->diffopt
.pickaxe_opts
& DIFF_PICKAXE_KINDS_MASK
) ||
2407 revs
->diffopt
.filter
||
2408 revs
->diffopt
.flags
.follow_renames
)
2411 if (revs
->diffopt
.objfind
)
2412 revs
->simplify_history
= 0;
2414 if (revs
->topo_order
)
2417 if (revs
->prune_data
.nr
) {
2418 copy_pathspec(&revs
->pruning
.pathspec
, &revs
->prune_data
);
2419 /* Can't prune commits with rename following: the paths change.. */
2420 if (!revs
->diffopt
.flags
.follow_renames
)
2422 if (!revs
->full_diff
)
2423 copy_pathspec(&revs
->diffopt
.pathspec
,
2426 if (revs
->combine_merges
)
2427 revs
->ignore_merges
= 0;
2428 revs
->diffopt
.abbrev
= revs
->abbrev
;
2430 if (revs
->line_level_traverse
) {
2432 revs
->topo_order
= 1;
2435 diff_setup_done(&revs
->diffopt
);
2437 grep_commit_pattern_type(GREP_PATTERN_TYPE_UNSPECIFIED
,
2438 &revs
->grep_filter
);
2439 compile_grep_patterns(&revs
->grep_filter
);
2441 if (revs
->reverse
&& revs
->reflog_info
)
2442 die("cannot combine --reverse with --walk-reflogs");
2443 if (revs
->reflog_info
&& revs
->limited
)
2444 die("cannot combine --walk-reflogs with history-limiting options");
2445 if (revs
->rewrite_parents
&& revs
->children
.name
)
2446 die("cannot combine --parents and --children");
2449 * Limitations on the graph functionality
2451 if (revs
->reverse
&& revs
->graph
)
2452 die("cannot combine --reverse with --graph");
2454 if (revs
->reflog_info
&& revs
->graph
)
2455 die("cannot combine --walk-reflogs with --graph");
2456 if (revs
->no_walk
&& revs
->graph
)
2457 die("cannot combine --no-walk with --graph");
2458 if (!revs
->reflog_info
&& revs
->grep_filter
.use_reflog_filter
)
2459 die("cannot use --grep-reflog without --walk-reflogs");
2461 if (revs
->first_parent_only
&& revs
->bisect
)
2462 die(_("--first-parent is incompatible with --bisect"));
2464 if (revs
->expand_tabs_in_log
< 0)
2465 revs
->expand_tabs_in_log
= revs
->expand_tabs_in_log_default
;
2470 static void add_child(struct rev_info
*revs
, struct commit
*parent
, struct commit
*child
)
2472 struct commit_list
*l
= xcalloc(1, sizeof(*l
));
2475 l
->next
= add_decoration(&revs
->children
, &parent
->object
, l
);
2478 static int remove_duplicate_parents(struct rev_info
*revs
, struct commit
*commit
)
2480 struct treesame_state
*ts
= lookup_decoration(&revs
->treesame
, &commit
->object
);
2481 struct commit_list
**pp
, *p
;
2482 int surviving_parents
;
2484 /* Examine existing parents while marking ones we have seen... */
2485 pp
= &commit
->parents
;
2486 surviving_parents
= 0;
2487 while ((p
= *pp
) != NULL
) {
2488 struct commit
*parent
= p
->item
;
2489 if (parent
->object
.flags
& TMP_MARK
) {
2492 compact_treesame(revs
, commit
, surviving_parents
);
2495 parent
->object
.flags
|= TMP_MARK
;
2496 surviving_parents
++;
2499 /* clear the temporary mark */
2500 for (p
= commit
->parents
; p
; p
= p
->next
) {
2501 p
->item
->object
.flags
&= ~TMP_MARK
;
2503 /* no update_treesame() - removing duplicates can't affect TREESAME */
2504 return surviving_parents
;
2507 struct merge_simplify_state
{
2508 struct commit
*simplified
;
2511 static struct merge_simplify_state
*locate_simplify_state(struct rev_info
*revs
, struct commit
*commit
)
2513 struct merge_simplify_state
*st
;
2515 st
= lookup_decoration(&revs
->merge_simplification
, &commit
->object
);
2517 st
= xcalloc(1, sizeof(*st
));
2518 add_decoration(&revs
->merge_simplification
, &commit
->object
, st
);
2523 static int mark_redundant_parents(struct rev_info
*revs
, struct commit
*commit
)
2525 struct commit_list
*h
= reduce_heads(commit
->parents
);
2526 int i
= 0, marked
= 0;
2527 struct commit_list
*po
, *pn
;
2529 /* Want these for sanity-checking only */
2530 int orig_cnt
= commit_list_count(commit
->parents
);
2531 int cnt
= commit_list_count(h
);
2534 * Not ready to remove items yet, just mark them for now, based
2535 * on the output of reduce_heads(). reduce_heads outputs the reduced
2536 * set in its original order, so this isn't too hard.
2538 po
= commit
->parents
;
2541 if (pn
&& po
->item
== pn
->item
) {
2545 po
->item
->object
.flags
|= TMP_MARK
;
2551 if (i
!= cnt
|| cnt
+marked
!= orig_cnt
)
2552 die("mark_redundant_parents %d %d %d %d", orig_cnt
, cnt
, i
, marked
);
2554 free_commit_list(h
);
2559 static int mark_treesame_root_parents(struct rev_info
*revs
, struct commit
*commit
)
2561 struct commit_list
*p
;
2564 for (p
= commit
->parents
; p
; p
= p
->next
) {
2565 struct commit
*parent
= p
->item
;
2566 if (!parent
->parents
&& (parent
->object
.flags
& TREESAME
)) {
2567 parent
->object
.flags
|= TMP_MARK
;
2576 * Awkward naming - this means one parent we are TREESAME to.
2577 * cf mark_treesame_root_parents: root parents that are TREESAME (to an
2578 * empty tree). Better name suggestions?
2580 static int leave_one_treesame_to_parent(struct rev_info
*revs
, struct commit
*commit
)
2582 struct treesame_state
*ts
= lookup_decoration(&revs
->treesame
, &commit
->object
);
2583 struct commit
*unmarked
= NULL
, *marked
= NULL
;
2584 struct commit_list
*p
;
2587 for (p
= commit
->parents
, n
= 0; p
; p
= p
->next
, n
++) {
2588 if (ts
->treesame
[n
]) {
2589 if (p
->item
->object
.flags
& TMP_MARK
) {
2602 * If we are TREESAME to a marked-for-deletion parent, but not to any
2603 * unmarked parents, unmark the first TREESAME parent. This is the
2604 * parent that the default simplify_history==1 scan would have followed,
2605 * and it doesn't make sense to omit that path when asking for a
2606 * simplified full history. Retaining it improves the chances of
2607 * understanding odd missed merges that took an old version of a file.
2611 * I--------*X A modified the file, but mainline merge X used
2612 * \ / "-s ours", so took the version from I. X is
2613 * `-*A--' TREESAME to I and !TREESAME to A.
2615 * Default log from X would produce "I". Without this check,
2616 * --full-history --simplify-merges would produce "I-A-X", showing
2617 * the merge commit X and that it changed A, but not making clear that
2618 * it had just taken the I version. With this check, the topology above
2621 * Note that it is possible that the simplification chooses a different
2622 * TREESAME parent from the default, in which case this test doesn't
2623 * activate, and we _do_ drop the default parent. Example:
2625 * I------X A modified the file, but it was reverted in B,
2626 * \ / meaning mainline merge X is TREESAME to both
2629 * Default log would produce "I" by following the first parent;
2630 * --full-history --simplify-merges will produce "I-A-B". But this is a
2631 * reasonable result - it presents a logical full history leading from
2632 * I to X, and X is not an important merge.
2634 if (!unmarked
&& marked
) {
2635 marked
->object
.flags
&= ~TMP_MARK
;
2642 static int remove_marked_parents(struct rev_info
*revs
, struct commit
*commit
)
2644 struct commit_list
**pp
, *p
;
2645 int nth_parent
, removed
= 0;
2647 pp
= &commit
->parents
;
2649 while ((p
= *pp
) != NULL
) {
2650 struct commit
*parent
= p
->item
;
2651 if (parent
->object
.flags
& TMP_MARK
) {
2652 parent
->object
.flags
&= ~TMP_MARK
;
2656 compact_treesame(revs
, commit
, nth_parent
);
2663 /* Removing parents can only increase TREESAMEness */
2664 if (removed
&& !(commit
->object
.flags
& TREESAME
))
2665 update_treesame(revs
, commit
);
2670 static struct commit_list
**simplify_one(struct rev_info
*revs
, struct commit
*commit
, struct commit_list
**tail
)
2672 struct commit_list
*p
;
2673 struct commit
*parent
;
2674 struct merge_simplify_state
*st
, *pst
;
2677 st
= locate_simplify_state(revs
, commit
);
2680 * Have we handled this one?
2686 * An UNINTERESTING commit simplifies to itself, so does a
2687 * root commit. We do not rewrite parents of such commit
2690 if ((commit
->object
.flags
& UNINTERESTING
) || !commit
->parents
) {
2691 st
->simplified
= commit
;
2696 * Do we know what commit all of our parents that matter
2697 * should be rewritten to? Otherwise we are not ready to
2698 * rewrite this one yet.
2700 for (cnt
= 0, p
= commit
->parents
; p
; p
= p
->next
) {
2701 pst
= locate_simplify_state(revs
, p
->item
);
2702 if (!pst
->simplified
) {
2703 tail
= &commit_list_insert(p
->item
, tail
)->next
;
2706 if (revs
->first_parent_only
)
2710 tail
= &commit_list_insert(commit
, tail
)->next
;
2715 * Rewrite our list of parents. Note that this cannot
2716 * affect our TREESAME flags in any way - a commit is
2717 * always TREESAME to its simplification.
2719 for (p
= commit
->parents
; p
; p
= p
->next
) {
2720 pst
= locate_simplify_state(revs
, p
->item
);
2721 p
->item
= pst
->simplified
;
2722 if (revs
->first_parent_only
)
2726 if (revs
->first_parent_only
)
2729 cnt
= remove_duplicate_parents(revs
, commit
);
2732 * It is possible that we are a merge and one side branch
2733 * does not have any commit that touches the given paths;
2734 * in such a case, the immediate parent from that branch
2735 * will be rewritten to be the merge base.
2737 * o----X X: the commit we are looking at;
2738 * / / o: a commit that touches the paths;
2741 * Further, a merge of an independent branch that doesn't
2742 * touch the path will reduce to a treesame root parent:
2744 * ----o----X X: the commit we are looking at;
2745 * / o: a commit that touches the paths;
2746 * r r: a root commit not touching the paths
2748 * Detect and simplify both cases.
2751 int marked
= mark_redundant_parents(revs
, commit
);
2752 marked
+= mark_treesame_root_parents(revs
, commit
);
2754 marked
-= leave_one_treesame_to_parent(revs
, commit
);
2756 cnt
= remove_marked_parents(revs
, commit
);
2760 * A commit simplifies to itself if it is a root, if it is
2761 * UNINTERESTING, if it touches the given paths, or if it is a
2762 * merge and its parents don't simplify to one relevant commit
2763 * (the first two cases are already handled at the beginning of
2766 * Otherwise, it simplifies to what its sole relevant parent
2770 (commit
->object
.flags
& UNINTERESTING
) ||
2771 !(commit
->object
.flags
& TREESAME
) ||
2772 (parent
= one_relevant_parent(revs
, commit
->parents
)) == NULL
)
2773 st
->simplified
= commit
;
2775 pst
= locate_simplify_state(revs
, parent
);
2776 st
->simplified
= pst
->simplified
;
2781 static void simplify_merges(struct rev_info
*revs
)
2783 struct commit_list
*list
, *next
;
2784 struct commit_list
*yet_to_do
, **tail
;
2785 struct commit
*commit
;
2790 /* feed the list reversed */
2792 for (list
= revs
->commits
; list
; list
= next
) {
2793 commit
= list
->item
;
2796 * Do not free(list) here yet; the original list
2797 * is used later in this function.
2799 commit_list_insert(commit
, &yet_to_do
);
2806 commit
= pop_commit(&list
);
2807 tail
= simplify_one(revs
, commit
, tail
);
2811 /* clean up the result, removing the simplified ones */
2812 list
= revs
->commits
;
2813 revs
->commits
= NULL
;
2814 tail
= &revs
->commits
;
2816 struct merge_simplify_state
*st
;
2818 commit
= pop_commit(&list
);
2819 st
= locate_simplify_state(revs
, commit
);
2820 if (st
->simplified
== commit
)
2821 tail
= &commit_list_insert(commit
, tail
)->next
;
2825 static void set_children(struct rev_info
*revs
)
2827 struct commit_list
*l
;
2828 for (l
= revs
->commits
; l
; l
= l
->next
) {
2829 struct commit
*commit
= l
->item
;
2830 struct commit_list
*p
;
2832 for (p
= commit
->parents
; p
; p
= p
->next
)
2833 add_child(revs
, p
->item
, commit
);
2837 void reset_revision_walk(void)
2839 clear_object_flags(SEEN
| ADDED
| SHOWN
);
2842 static int mark_uninteresting(const struct object_id
*oid
,
2843 struct packed_git
*pack
,
2847 struct object
*o
= parse_object(oid
);
2848 o
->flags
|= UNINTERESTING
| SEEN
;
2852 int prepare_revision_walk(struct rev_info
*revs
)
2855 struct object_array old_pending
;
2856 struct commit_list
**next
= &revs
->commits
;
2858 memcpy(&old_pending
, &revs
->pending
, sizeof(old_pending
));
2859 revs
->pending
.nr
= 0;
2860 revs
->pending
.alloc
= 0;
2861 revs
->pending
.objects
= NULL
;
2862 for (i
= 0; i
< old_pending
.nr
; i
++) {
2863 struct object_array_entry
*e
= old_pending
.objects
+ i
;
2864 struct commit
*commit
= handle_commit(revs
, e
);
2866 if (!(commit
->object
.flags
& SEEN
)) {
2867 commit
->object
.flags
|= SEEN
;
2868 next
= commit_list_append(commit
, next
);
2872 object_array_clear(&old_pending
);
2874 /* Signal whether we need per-parent treesame decoration */
2875 if (revs
->simplify_merges
||
2876 (revs
->limited
&& limiting_can_increase_treesame(revs
)))
2877 revs
->treesame
.name
= "treesame";
2879 if (revs
->exclude_promisor_objects
) {
2880 for_each_packed_object(mark_uninteresting
, NULL
,
2881 FOR_EACH_OBJECT_PROMISOR_ONLY
);
2884 if (revs
->no_walk
!= REVISION_WALK_NO_WALK_UNSORTED
)
2885 commit_list_sort_by_date(&revs
->commits
);
2889 if (limit_list(revs
) < 0)
2891 if (revs
->topo_order
)
2892 sort_in_topological_order(&revs
->commits
, revs
->sort_order
);
2893 if (revs
->line_level_traverse
)
2894 line_log_filter(revs
);
2895 if (revs
->simplify_merges
)
2896 simplify_merges(revs
);
2897 if (revs
->children
.name
)
2902 static enum rewrite_result
rewrite_one(struct rev_info
*revs
, struct commit
**pp
)
2904 struct commit_list
*cache
= NULL
;
2907 struct commit
*p
= *pp
;
2909 if (add_parents_to_list(revs
, p
, &revs
->commits
, &cache
) < 0)
2910 return rewrite_one_error
;
2911 if (p
->object
.flags
& UNINTERESTING
)
2912 return rewrite_one_ok
;
2913 if (!(p
->object
.flags
& TREESAME
))
2914 return rewrite_one_ok
;
2916 return rewrite_one_noparents
;
2917 if ((p
= one_relevant_parent(revs
, p
->parents
)) == NULL
)
2918 return rewrite_one_ok
;
2923 int rewrite_parents(struct rev_info
*revs
, struct commit
*commit
,
2924 rewrite_parent_fn_t rewrite_parent
)
2926 struct commit_list
**pp
= &commit
->parents
;
2928 struct commit_list
*parent
= *pp
;
2929 switch (rewrite_parent(revs
, &parent
->item
)) {
2930 case rewrite_one_ok
:
2932 case rewrite_one_noparents
:
2935 case rewrite_one_error
:
2940 remove_duplicate_parents(revs
, commit
);
2944 static int commit_rewrite_person(struct strbuf
*buf
, const char *what
, struct string_list
*mailmap
)
2946 char *person
, *endp
;
2947 size_t len
, namelen
, maillen
;
2950 struct ident_split ident
;
2952 person
= strstr(buf
->buf
, what
);
2956 person
+= strlen(what
);
2957 endp
= strchr(person
, '\n');
2961 len
= endp
- person
;
2963 if (split_ident_line(&ident
, person
, len
))
2966 mail
= ident
.mail_begin
;
2967 maillen
= ident
.mail_end
- ident
.mail_begin
;
2968 name
= ident
.name_begin
;
2969 namelen
= ident
.name_end
- ident
.name_begin
;
2971 if (map_user(mailmap
, &mail
, &maillen
, &name
, &namelen
)) {
2972 struct strbuf namemail
= STRBUF_INIT
;
2974 strbuf_addf(&namemail
, "%.*s <%.*s>",
2975 (int)namelen
, name
, (int)maillen
, mail
);
2977 strbuf_splice(buf
, ident
.name_begin
- buf
->buf
,
2978 ident
.mail_end
- ident
.name_begin
+ 1,
2979 namemail
.buf
, namemail
.len
);
2981 strbuf_release(&namemail
);
2989 static int commit_match(struct commit
*commit
, struct rev_info
*opt
)
2992 const char *encoding
;
2993 const char *message
;
2994 struct strbuf buf
= STRBUF_INIT
;
2996 if (!opt
->grep_filter
.pattern_list
&& !opt
->grep_filter
.header_list
)
2999 /* Prepend "fake" headers as needed */
3000 if (opt
->grep_filter
.use_reflog_filter
) {
3001 strbuf_addstr(&buf
, "reflog ");
3002 get_reflog_message(&buf
, opt
->reflog_info
);
3003 strbuf_addch(&buf
, '\n');
3007 * We grep in the user's output encoding, under the assumption that it
3008 * is the encoding they are most likely to write their grep pattern
3009 * for. In addition, it means we will match the "notes" encoding below,
3010 * so we will not end up with a buffer that has two different encodings
3013 encoding
= get_log_output_encoding();
3014 message
= logmsg_reencode(commit
, NULL
, encoding
);
3016 /* Copy the commit to temporary if we are using "fake" headers */
3018 strbuf_addstr(&buf
, message
);
3020 if (opt
->grep_filter
.header_list
&& opt
->mailmap
) {
3022 strbuf_addstr(&buf
, message
);
3024 commit_rewrite_person(&buf
, "\nauthor ", opt
->mailmap
);
3025 commit_rewrite_person(&buf
, "\ncommitter ", opt
->mailmap
);
3028 /* Append "fake" message parts as needed */
3029 if (opt
->show_notes
) {
3031 strbuf_addstr(&buf
, message
);
3032 format_display_notes(&commit
->object
.oid
, &buf
, encoding
, 1);
3036 * Find either in the original commit message, or in the temporary.
3037 * Note that we cast away the constness of "message" here. It is
3038 * const because it may come from the cached commit buffer. That's OK,
3039 * because we know that it is modifiable heap memory, and that while
3040 * grep_buffer may modify it for speed, it will restore any
3041 * changes before returning.
3044 retval
= grep_buffer(&opt
->grep_filter
, buf
.buf
, buf
.len
);
3046 retval
= grep_buffer(&opt
->grep_filter
,
3047 (char *)message
, strlen(message
));
3048 strbuf_release(&buf
);
3049 unuse_commit_buffer(commit
, message
);
3050 return opt
->invert_grep
? !retval
: retval
;
3053 static inline int want_ancestry(const struct rev_info
*revs
)
3055 return (revs
->rewrite_parents
|| revs
->children
.name
);
3059 * Return a timestamp to be used for --since/--until comparisons for this
3060 * commit, based on the revision options.
3062 static timestamp_t
comparison_date(const struct rev_info
*revs
,
3063 struct commit
*commit
)
3065 return revs
->reflog_info
?
3066 get_reflog_timestamp(revs
->reflog_info
) :
3070 enum commit_action
get_commit_action(struct rev_info
*revs
, struct commit
*commit
)
3072 if (commit
->object
.flags
& SHOWN
)
3073 return commit_ignore
;
3074 if (revs
->unpacked
&& has_sha1_pack(commit
->object
.oid
.hash
))
3075 return commit_ignore
;
3076 if (commit
->object
.flags
& UNINTERESTING
)
3077 return commit_ignore
;
3078 if (revs
->min_age
!= -1 &&
3079 comparison_date(revs
, commit
) > revs
->min_age
)
3080 return commit_ignore
;
3081 if (revs
->min_parents
|| (revs
->max_parents
>= 0)) {
3082 int n
= commit_list_count(commit
->parents
);
3083 if ((n
< revs
->min_parents
) ||
3084 ((revs
->max_parents
>= 0) && (n
> revs
->max_parents
)))
3085 return commit_ignore
;
3087 if (!commit_match(commit
, revs
))
3088 return commit_ignore
;
3089 if (revs
->prune
&& revs
->dense
) {
3090 /* Commit without changes? */
3091 if (commit
->object
.flags
& TREESAME
) {
3093 struct commit_list
*p
;
3094 /* drop merges unless we want parenthood */
3095 if (!want_ancestry(revs
))
3096 return commit_ignore
;
3098 * If we want ancestry, then need to keep any merges
3099 * between relevant commits to tie together topology.
3100 * For consistency with TREESAME and simplification
3101 * use "relevant" here rather than just INTERESTING,
3102 * to treat bottom commit(s) as part of the topology.
3104 for (n
= 0, p
= commit
->parents
; p
; p
= p
->next
)
3105 if (relevant_commit(p
->item
))
3108 return commit_ignore
;
3114 define_commit_slab(saved_parents
, struct commit_list
*);
3116 #define EMPTY_PARENT_LIST ((struct commit_list *)-1)
3119 * You may only call save_parents() once per commit (this is checked
3120 * for non-root commits).
3122 static void save_parents(struct rev_info
*revs
, struct commit
*commit
)
3124 struct commit_list
**pp
;
3126 if (!revs
->saved_parents_slab
) {
3127 revs
->saved_parents_slab
= xmalloc(sizeof(struct saved_parents
));
3128 init_saved_parents(revs
->saved_parents_slab
);
3131 pp
= saved_parents_at(revs
->saved_parents_slab
, commit
);
3134 * When walking with reflogs, we may visit the same commit
3135 * several times: once for each appearance in the reflog.
3137 * In this case, save_parents() will be called multiple times.
3138 * We want to keep only the first set of parents. We need to
3139 * store a sentinel value for an empty (i.e., NULL) parent
3140 * list to distinguish it from a not-yet-saved list, however.
3144 if (commit
->parents
)
3145 *pp
= copy_commit_list(commit
->parents
);
3147 *pp
= EMPTY_PARENT_LIST
;
3150 static void free_saved_parents(struct rev_info
*revs
)
3152 if (revs
->saved_parents_slab
)
3153 clear_saved_parents(revs
->saved_parents_slab
);
3156 struct commit_list
*get_saved_parents(struct rev_info
*revs
, const struct commit
*commit
)
3158 struct commit_list
*parents
;
3160 if (!revs
->saved_parents_slab
)
3161 return commit
->parents
;
3163 parents
= *saved_parents_at(revs
->saved_parents_slab
, commit
);
3164 if (parents
== EMPTY_PARENT_LIST
)
3169 enum commit_action
simplify_commit(struct rev_info
*revs
, struct commit
*commit
)
3171 enum commit_action action
= get_commit_action(revs
, commit
);
3173 if (action
== commit_show
&&
3174 revs
->prune
&& revs
->dense
&& want_ancestry(revs
)) {
3176 * --full-diff on simplified parents is no good: it
3177 * will show spurious changes from the commits that
3178 * were elided. So we save the parents on the side
3179 * when --full-diff is in effect.
3181 if (revs
->full_diff
)
3182 save_parents(revs
, commit
);
3183 if (rewrite_parents(revs
, commit
, rewrite_one
) < 0)
3184 return commit_error
;
3189 static void track_linear(struct rev_info
*revs
, struct commit
*commit
)
3191 if (revs
->track_first_time
) {
3193 revs
->track_first_time
= 0;
3195 struct commit_list
*p
;
3196 for (p
= revs
->previous_parents
; p
; p
= p
->next
)
3197 if (p
->item
== NULL
|| /* first commit */
3198 !oidcmp(&p
->item
->object
.oid
, &commit
->object
.oid
))
3200 revs
->linear
= p
!= NULL
;
3202 if (revs
->reverse
) {
3204 commit
->object
.flags
|= TRACK_LINEAR
;
3206 free_commit_list(revs
->previous_parents
);
3207 revs
->previous_parents
= copy_commit_list(commit
->parents
);
3210 static struct commit
*get_revision_1(struct rev_info
*revs
)
3213 struct commit
*commit
;
3215 if (revs
->reflog_info
)
3216 commit
= next_reflog_entry(revs
->reflog_info
);
3218 commit
= pop_commit(&revs
->commits
);
3223 if (revs
->reflog_info
)
3224 commit
->object
.flags
&= ~(ADDED
| SEEN
| SHOWN
);
3227 * If we haven't done the list limiting, we need to look at
3228 * the parents here. We also need to do the date-based limiting
3229 * that we'd otherwise have done in limit_list().
3231 if (!revs
->limited
) {
3232 if (revs
->max_age
!= -1 &&
3233 comparison_date(revs
, commit
) < revs
->max_age
)
3236 if (revs
->reflog_info
)
3237 try_to_simplify_commit(revs
, commit
);
3238 else if (add_parents_to_list(revs
, commit
, &revs
->commits
, NULL
) < 0) {
3239 if (!revs
->ignore_missing_links
)
3240 die("Failed to traverse parents of commit %s",
3241 oid_to_hex(&commit
->object
.oid
));
3245 switch (simplify_commit(revs
, commit
)) {
3249 die("Failed to simplify parents of commit %s",
3250 oid_to_hex(&commit
->object
.oid
));
3252 if (revs
->track_linear
)
3253 track_linear(revs
, commit
);
3260 * Return true for entries that have not yet been shown. (This is an
3261 * object_array_each_func_t.)
3263 static int entry_unshown(struct object_array_entry
*entry
, void *cb_data_unused
)
3265 return !(entry
->item
->flags
& SHOWN
);
3269 * If array is on the verge of a realloc, garbage-collect any entries
3270 * that have already been shown to try to free up some space.
3272 static void gc_boundary(struct object_array
*array
)
3274 if (array
->nr
== array
->alloc
)
3275 object_array_filter(array
, entry_unshown
, NULL
);
3278 static void create_boundary_commit_list(struct rev_info
*revs
)
3282 struct object_array
*array
= &revs
->boundary_commits
;
3283 struct object_array_entry
*objects
= array
->objects
;
3286 * If revs->commits is non-NULL at this point, an error occurred in
3287 * get_revision_1(). Ignore the error and continue printing the
3288 * boundary commits anyway. (This is what the code has always
3291 if (revs
->commits
) {
3292 free_commit_list(revs
->commits
);
3293 revs
->commits
= NULL
;
3297 * Put all of the actual boundary commits from revs->boundary_commits
3298 * into revs->commits
3300 for (i
= 0; i
< array
->nr
; i
++) {
3301 c
= (struct commit
*)(objects
[i
].item
);
3304 if (!(c
->object
.flags
& CHILD_SHOWN
))
3306 if (c
->object
.flags
& (SHOWN
| BOUNDARY
))
3308 c
->object
.flags
|= BOUNDARY
;
3309 commit_list_insert(c
, &revs
->commits
);
3313 * If revs->topo_order is set, sort the boundary commits
3314 * in topological order
3316 sort_in_topological_order(&revs
->commits
, revs
->sort_order
);
3319 static struct commit
*get_revision_internal(struct rev_info
*revs
)
3321 struct commit
*c
= NULL
;
3322 struct commit_list
*l
;
3324 if (revs
->boundary
== 2) {
3326 * All of the normal commits have already been returned,
3327 * and we are now returning boundary commits.
3328 * create_boundary_commit_list() has populated
3329 * revs->commits with the remaining commits to return.
3331 c
= pop_commit(&revs
->commits
);
3333 c
->object
.flags
|= SHOWN
;
3338 * If our max_count counter has reached zero, then we are done. We
3339 * don't simply return NULL because we still might need to show
3340 * boundary commits. But we want to avoid calling get_revision_1, which
3341 * might do a considerable amount of work finding the next commit only
3342 * for us to throw it away.
3344 * If it is non-zero, then either we don't have a max_count at all
3345 * (-1), or it is still counting, in which case we decrement.
3347 if (revs
->max_count
) {
3348 c
= get_revision_1(revs
);
3350 while (revs
->skip_count
> 0) {
3352 c
= get_revision_1(revs
);
3358 if (revs
->max_count
> 0)
3363 c
->object
.flags
|= SHOWN
;
3365 if (!revs
->boundary
)
3370 * get_revision_1() runs out the commits, and
3371 * we are done computing the boundaries.
3372 * switch to boundary commits output mode.
3377 * Update revs->commits to contain the list of
3380 create_boundary_commit_list(revs
);
3382 return get_revision_internal(revs
);
3386 * boundary commits are the commits that are parents of the
3387 * ones we got from get_revision_1() but they themselves are
3388 * not returned from get_revision_1(). Before returning
3389 * 'c', we need to mark its parents that they could be boundaries.
3392 for (l
= c
->parents
; l
; l
= l
->next
) {
3394 p
= &(l
->item
->object
);
3395 if (p
->flags
& (CHILD_SHOWN
| SHOWN
))
3397 p
->flags
|= CHILD_SHOWN
;
3398 gc_boundary(&revs
->boundary_commits
);
3399 add_object_array(p
, NULL
, &revs
->boundary_commits
);
3405 struct commit
*get_revision(struct rev_info
*revs
)
3408 struct commit_list
*reversed
;
3410 if (revs
->reverse
) {
3412 while ((c
= get_revision_internal(revs
)))
3413 commit_list_insert(c
, &reversed
);
3414 revs
->commits
= reversed
;
3416 revs
->reverse_output_stage
= 1;
3419 if (revs
->reverse_output_stage
) {
3420 c
= pop_commit(&revs
->commits
);
3421 if (revs
->track_linear
)
3422 revs
->linear
= !!(c
&& c
->object
.flags
& TRACK_LINEAR
);
3426 c
= get_revision_internal(revs
);
3427 if (c
&& revs
->graph
)
3428 graph_update(revs
->graph
, c
);
3430 free_saved_parents(revs
);
3431 if (revs
->previous_parents
) {
3432 free_commit_list(revs
->previous_parents
);
3433 revs
->previous_parents
= NULL
;
3439 char *get_revision_mark(const struct rev_info
*revs
, const struct commit
*commit
)
3441 if (commit
->object
.flags
& BOUNDARY
)
3443 else if (commit
->object
.flags
& UNINTERESTING
)
3445 else if (commit
->object
.flags
& PATCHSAME
)
3447 else if (!revs
|| revs
->left_right
) {
3448 if (commit
->object
.flags
& SYMMETRIC_LEFT
)
3452 } else if (revs
->graph
)
3454 else if (revs
->cherry_mark
)
3459 void put_revision_mark(const struct rev_info
*revs
, const struct commit
*commit
)
3461 char *mark
= get_revision_mark(revs
, commit
);
3464 fputs(mark
, stdout
);