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
;
54 struct object
*obj
= &tree
->object
;
56 if (!has_object_file(&obj
->oid
))
58 if (parse_tree(tree
) < 0)
59 die("bad tree %s", oid_to_hex(&obj
->oid
));
61 init_tree_desc(&desc
, tree
->buffer
, tree
->size
);
62 while (tree_entry(&desc
, &entry
)) {
63 switch (object_type(entry
.mode
)) {
65 mark_tree_uninteresting(lookup_tree(entry
.oid
));
68 mark_blob_uninteresting(lookup_blob(entry
.oid
));
71 /* Subproject commit - not in this repository */
77 * We don't care about the tree any more
78 * after it has been marked uninteresting.
80 free_tree_buffer(tree
);
83 void mark_tree_uninteresting(struct tree
*tree
)
91 if (obj
->flags
& UNINTERESTING
)
93 obj
->flags
|= UNINTERESTING
;
94 mark_tree_contents_uninteresting(tree
);
97 void mark_parents_uninteresting(struct commit
*commit
)
99 struct commit_list
*parents
= NULL
, *l
;
101 for (l
= commit
->parents
; l
; l
= l
->next
)
102 commit_list_insert(l
->item
, &parents
);
105 struct commit
*commit
= pop_commit(&parents
);
109 * A missing commit is ok iff its parent is marked
112 * We just mark such a thing parsed, so that when
113 * it is popped next time around, we won't be trying
114 * to parse it and get an error.
116 if (!has_object_file(&commit
->object
.oid
))
117 commit
->object
.parsed
= 1;
119 if (commit
->object
.flags
& UNINTERESTING
)
122 commit
->object
.flags
|= UNINTERESTING
;
125 * Normally we haven't parsed the parent
126 * yet, so we won't have a parent of a parent
127 * here. However, it may turn out that we've
128 * reached this commit some other way (where it
129 * wasn't uninteresting), in which case we need
130 * to mark its parents recursively too..
132 if (!commit
->parents
)
135 for (l
= commit
->parents
->next
; l
; l
= l
->next
)
136 commit_list_insert(l
->item
, &parents
);
137 commit
= commit
->parents
->item
;
142 static void add_pending_object_with_path(struct rev_info
*revs
,
144 const char *name
, unsigned mode
,
149 if (revs
->no_walk
&& (obj
->flags
& UNINTERESTING
))
151 if (revs
->reflog_info
&& obj
->type
== OBJ_COMMIT
) {
152 struct strbuf buf
= STRBUF_INIT
;
153 int len
= interpret_branch_name(name
, 0, &buf
, 0);
155 if (0 < len
&& name
[len
] && buf
.len
)
156 strbuf_addstr(&buf
, name
+ len
);
157 add_reflog_for_walk(revs
->reflog_info
,
158 (struct commit
*)obj
,
159 buf
.buf
[0] ? buf
.buf
: name
);
160 strbuf_release(&buf
);
161 return; /* do not add the commit itself */
163 add_object_array_with_path(obj
, name
, &revs
->pending
, mode
, path
);
166 static void add_pending_object_with_mode(struct rev_info
*revs
,
168 const char *name
, unsigned mode
)
170 add_pending_object_with_path(revs
, obj
, name
, mode
, NULL
);
173 void add_pending_object(struct rev_info
*revs
,
174 struct object
*obj
, const char *name
)
176 add_pending_object_with_mode(revs
, obj
, name
, S_IFINVALID
);
179 void add_head_to_pending(struct rev_info
*revs
)
181 struct object_id oid
;
183 if (get_oid("HEAD", &oid
))
185 obj
= parse_object(&oid
);
188 add_pending_object(revs
, obj
, "HEAD");
191 static struct object
*get_reference(struct rev_info
*revs
, const char *name
,
192 const struct object_id
*oid
,
195 struct object
*object
;
197 object
= parse_object(oid
);
199 if (revs
->ignore_missing
)
201 if (revs
->exclude_promisor_objects
&& is_promisor_object(oid
))
203 die("bad object %s", name
);
205 object
->flags
|= flags
;
209 void add_pending_oid(struct rev_info
*revs
, const char *name
,
210 const struct object_id
*oid
, unsigned int flags
)
212 struct object
*object
= get_reference(revs
, name
, oid
, flags
);
213 add_pending_object(revs
, object
, name
);
216 static struct commit
*handle_commit(struct rev_info
*revs
,
217 struct object_array_entry
*entry
)
219 struct object
*object
= entry
->item
;
220 const char *name
= entry
->name
;
221 const char *path
= entry
->path
;
222 unsigned int mode
= entry
->mode
;
223 unsigned long flags
= object
->flags
;
226 * Tag object? Look what it points to..
228 while (object
->type
== OBJ_TAG
) {
229 struct tag
*tag
= (struct tag
*) object
;
230 if (revs
->tag_objects
&& !(flags
& UNINTERESTING
))
231 add_pending_object(revs
, object
, tag
->tag
);
234 object
= parse_object(&tag
->tagged
->oid
);
236 if (revs
->ignore_missing_links
|| (flags
& UNINTERESTING
))
238 die("bad object %s", oid_to_hex(&tag
->tagged
->oid
));
240 object
->flags
|= flags
;
242 * We'll handle the tagged object by looping or dropping
243 * through to the non-tag handlers below. Do not
244 * propagate path data from the tag's pending entry.
251 * Commit object? Just return it, we'll do all the complex
254 if (object
->type
== OBJ_COMMIT
) {
255 struct commit
*commit
= (struct commit
*)object
;
256 if (parse_commit(commit
) < 0)
257 die("unable to parse commit %s", name
);
258 if (flags
& UNINTERESTING
) {
259 mark_parents_uninteresting(commit
);
262 if (revs
->show_source
&& !commit
->util
)
263 commit
->util
= xstrdup(name
);
268 * Tree object? Either mark it uninteresting, or add it
269 * to the list of objects to look at later..
271 if (object
->type
== OBJ_TREE
) {
272 struct tree
*tree
= (struct tree
*)object
;
273 if (!revs
->tree_objects
)
275 if (flags
& UNINTERESTING
) {
276 mark_tree_contents_uninteresting(tree
);
279 add_pending_object_with_path(revs
, object
, name
, mode
, path
);
284 * Blob object? You know the drill by now..
286 if (object
->type
== OBJ_BLOB
) {
287 if (!revs
->blob_objects
)
289 if (flags
& UNINTERESTING
)
291 add_pending_object_with_path(revs
, object
, name
, mode
, path
);
294 die("%s is unknown object", name
);
297 static int everybody_uninteresting(struct commit_list
*orig
,
298 struct commit
**interesting_cache
)
300 struct commit_list
*list
= orig
;
302 if (*interesting_cache
) {
303 struct commit
*commit
= *interesting_cache
;
304 if (!(commit
->object
.flags
& UNINTERESTING
))
309 struct commit
*commit
= list
->item
;
311 if (commit
->object
.flags
& UNINTERESTING
)
314 *interesting_cache
= commit
;
321 * A definition of "relevant" commit that we can use to simplify limited graphs
322 * by eliminating side branches.
324 * A "relevant" commit is one that is !UNINTERESTING (ie we are including it
325 * in our list), or that is a specified BOTTOM commit. Then after computing
326 * a limited list, during processing we can generally ignore boundary merges
327 * coming from outside the graph, (ie from irrelevant parents), and treat
328 * those merges as if they were single-parent. TREESAME is defined to consider
329 * only relevant parents, if any. If we are TREESAME to our on-graph parents,
330 * we don't care if we were !TREESAME to non-graph parents.
332 * Treating bottom commits as relevant ensures that a limited graph's
333 * connection to the actual bottom commit is not viewed as a side branch, but
334 * treated as part of the graph. For example:
336 * ....Z...A---X---o---o---B
340 * When computing "A..B", the A-X connection is at least as important as
341 * Y-X, despite A being flagged UNINTERESTING.
343 * And when computing --ancestry-path "A..B", the A-X connection is more
344 * important than Y-X, despite both A and Y being flagged UNINTERESTING.
346 static inline int relevant_commit(struct commit
*commit
)
348 return (commit
->object
.flags
& (UNINTERESTING
| BOTTOM
)) != UNINTERESTING
;
352 * Return a single relevant commit from a parent list. If we are a TREESAME
353 * commit, and this selects one of our parents, then we can safely simplify to
356 static struct commit
*one_relevant_parent(const struct rev_info
*revs
,
357 struct commit_list
*orig
)
359 struct commit_list
*list
= orig
;
360 struct commit
*relevant
= NULL
;
366 * For 1-parent commits, or if first-parent-only, then return that
367 * first parent (even if not "relevant" by the above definition).
368 * TREESAME will have been set purely on that parent.
370 if (revs
->first_parent_only
|| !orig
->next
)
374 * For multi-parent commits, identify a sole relevant parent, if any.
375 * If we have only one relevant parent, then TREESAME will be set purely
376 * with regard to that parent, and we can simplify accordingly.
378 * If we have more than one relevant parent, or no relevant parents
379 * (and multiple irrelevant ones), then we can't select a parent here
383 struct commit
*commit
= list
->item
;
385 if (relevant_commit(commit
)) {
395 * The goal is to get REV_TREE_NEW as the result only if the
396 * diff consists of all '+' (and no other changes), REV_TREE_OLD
397 * if the whole diff is removal of old data, and otherwise
398 * REV_TREE_DIFFERENT (of course if the trees are the same we
399 * want REV_TREE_SAME).
401 * The only time we care about the distinction is when
402 * remove_empty_trees is in effect, in which case we care only about
403 * whether the whole change is REV_TREE_NEW, or if there's another type
404 * of change. Which means we can stop the diff early in either of these
407 * 1. We're not using remove_empty_trees at all.
409 * 2. We saw anything except REV_TREE_NEW.
411 static int tree_difference
= REV_TREE_SAME
;
413 static void file_add_remove(struct diff_options
*options
,
414 int addremove
, unsigned mode
,
415 const struct object_id
*oid
,
417 const char *fullpath
, unsigned dirty_submodule
)
419 int diff
= addremove
== '+' ? REV_TREE_NEW
: REV_TREE_OLD
;
420 struct rev_info
*revs
= options
->change_fn_data
;
422 tree_difference
|= diff
;
423 if (!revs
->remove_empty_trees
|| tree_difference
!= REV_TREE_NEW
)
424 options
->flags
.has_changes
= 1;
427 static void file_change(struct diff_options
*options
,
428 unsigned old_mode
, unsigned new_mode
,
429 const struct object_id
*old_oid
,
430 const struct object_id
*new_oid
,
431 int old_oid_valid
, int new_oid_valid
,
432 const char *fullpath
,
433 unsigned old_dirty_submodule
, unsigned new_dirty_submodule
)
435 tree_difference
= REV_TREE_DIFFERENT
;
436 options
->flags
.has_changes
= 1;
439 static int rev_compare_tree(struct rev_info
*revs
,
440 struct commit
*parent
, struct commit
*commit
)
442 struct tree
*t1
= parent
->tree
;
443 struct tree
*t2
= commit
->tree
;
450 if (revs
->simplify_by_decoration
) {
452 * If we are simplifying by decoration, then the commit
453 * is worth showing if it has a tag pointing at it.
455 if (get_name_decoration(&commit
->object
))
456 return REV_TREE_DIFFERENT
;
458 * A commit that is not pointed by a tag is uninteresting
459 * if we are not limited by path. This means that you will
460 * see the usual "commits that touch the paths" plus any
461 * tagged commit by specifying both --simplify-by-decoration
464 if (!revs
->prune_data
.nr
)
465 return REV_TREE_SAME
;
468 tree_difference
= REV_TREE_SAME
;
469 revs
->pruning
.flags
.has_changes
= 0;
470 if (diff_tree_oid(&t1
->object
.oid
, &t2
->object
.oid
, "",
472 return REV_TREE_DIFFERENT
;
473 return tree_difference
;
476 static int rev_same_tree_as_empty(struct rev_info
*revs
, struct commit
*commit
)
479 struct tree
*t1
= commit
->tree
;
484 tree_difference
= REV_TREE_SAME
;
485 revs
->pruning
.flags
.has_changes
= 0;
486 retval
= diff_tree_oid(NULL
, &t1
->object
.oid
, "", &revs
->pruning
);
488 return retval
>= 0 && (tree_difference
== REV_TREE_SAME
);
491 struct treesame_state
{
492 unsigned int nparents
;
493 unsigned char treesame
[FLEX_ARRAY
];
496 static struct treesame_state
*initialise_treesame(struct rev_info
*revs
, struct commit
*commit
)
498 unsigned n
= commit_list_count(commit
->parents
);
499 struct treesame_state
*st
= xcalloc(1, st_add(sizeof(*st
), n
));
501 add_decoration(&revs
->treesame
, &commit
->object
, st
);
506 * Must be called immediately after removing the nth_parent from a commit's
507 * parent list, if we are maintaining the per-parent treesame[] decoration.
508 * This does not recalculate the master TREESAME flag - update_treesame()
509 * should be called to update it after a sequence of treesame[] modifications
510 * that may have affected it.
512 static int compact_treesame(struct rev_info
*revs
, struct commit
*commit
, unsigned nth_parent
)
514 struct treesame_state
*st
;
517 if (!commit
->parents
) {
519 * Have just removed the only parent from a non-merge.
520 * Different handling, as we lack decoration.
523 die("compact_treesame %u", nth_parent
);
524 old_same
= !!(commit
->object
.flags
& TREESAME
);
525 if (rev_same_tree_as_empty(revs
, commit
))
526 commit
->object
.flags
|= TREESAME
;
528 commit
->object
.flags
&= ~TREESAME
;
532 st
= lookup_decoration(&revs
->treesame
, &commit
->object
);
533 if (!st
|| nth_parent
>= st
->nparents
)
534 die("compact_treesame %u", nth_parent
);
536 old_same
= st
->treesame
[nth_parent
];
537 memmove(st
->treesame
+ nth_parent
,
538 st
->treesame
+ nth_parent
+ 1,
539 st
->nparents
- nth_parent
- 1);
542 * If we've just become a non-merge commit, update TREESAME
543 * immediately, and remove the no-longer-needed decoration.
544 * If still a merge, defer update until update_treesame().
546 if (--st
->nparents
== 1) {
547 if (commit
->parents
->next
)
548 die("compact_treesame parents mismatch");
549 if (st
->treesame
[0] && revs
->dense
)
550 commit
->object
.flags
|= TREESAME
;
552 commit
->object
.flags
&= ~TREESAME
;
553 free(add_decoration(&revs
->treesame
, &commit
->object
, NULL
));
559 static unsigned update_treesame(struct rev_info
*revs
, struct commit
*commit
)
561 if (commit
->parents
&& commit
->parents
->next
) {
563 struct treesame_state
*st
;
564 struct commit_list
*p
;
565 unsigned relevant_parents
;
566 unsigned relevant_change
, irrelevant_change
;
568 st
= lookup_decoration(&revs
->treesame
, &commit
->object
);
570 die("update_treesame %s", oid_to_hex(&commit
->object
.oid
));
571 relevant_parents
= 0;
572 relevant_change
= irrelevant_change
= 0;
573 for (p
= commit
->parents
, n
= 0; p
; n
++, p
= p
->next
) {
574 if (relevant_commit(p
->item
)) {
575 relevant_change
|= !st
->treesame
[n
];
578 irrelevant_change
|= !st
->treesame
[n
];
580 if (relevant_parents
? relevant_change
: irrelevant_change
)
581 commit
->object
.flags
&= ~TREESAME
;
583 commit
->object
.flags
|= TREESAME
;
586 return commit
->object
.flags
& TREESAME
;
589 static inline int limiting_can_increase_treesame(const struct rev_info
*revs
)
592 * TREESAME is irrelevant unless prune && dense;
593 * if simplify_history is set, we can't have a mixture of TREESAME and
594 * !TREESAME INTERESTING parents (and we don't have treesame[]
595 * decoration anyway);
596 * if first_parent_only is set, then the TREESAME flag is locked
597 * against the first parent (and again we lack treesame[] decoration).
599 return revs
->prune
&& revs
->dense
&&
600 !revs
->simplify_history
&&
601 !revs
->first_parent_only
;
604 static void try_to_simplify_commit(struct rev_info
*revs
, struct commit
*commit
)
606 struct commit_list
**pp
, *parent
;
607 struct treesame_state
*ts
= NULL
;
608 int relevant_change
= 0, irrelevant_change
= 0;
609 int relevant_parents
, nth_parent
;
612 * If we don't do pruning, everything is interesting
620 if (!commit
->parents
) {
621 if (rev_same_tree_as_empty(revs
, commit
))
622 commit
->object
.flags
|= TREESAME
;
627 * Normal non-merge commit? If we don't want to make the
628 * history dense, we consider it always to be a change..
630 if (!revs
->dense
&& !commit
->parents
->next
)
633 for (pp
= &commit
->parents
, nth_parent
= 0, relevant_parents
= 0;
634 (parent
= *pp
) != NULL
;
635 pp
= &parent
->next
, nth_parent
++) {
636 struct commit
*p
= parent
->item
;
637 if (relevant_commit(p
))
640 if (nth_parent
== 1) {
642 * This our second loop iteration - so we now know
643 * we're dealing with a merge.
645 * Do not compare with later parents when we care only about
646 * the first parent chain, in order to avoid derailing the
647 * traversal to follow a side branch that brought everything
648 * in the path we are limited to by the pathspec.
650 if (revs
->first_parent_only
)
653 * If this will remain a potentially-simplifiable
654 * merge, remember per-parent treesame if needed.
655 * Initialise the array with the comparison from our
658 if (revs
->treesame
.name
&&
659 !revs
->simplify_history
&&
660 !(commit
->object
.flags
& UNINTERESTING
)) {
661 ts
= initialise_treesame(revs
, commit
);
662 if (!(irrelevant_change
|| relevant_change
))
666 if (parse_commit(p
) < 0)
667 die("cannot simplify commit %s (because of %s)",
668 oid_to_hex(&commit
->object
.oid
),
669 oid_to_hex(&p
->object
.oid
));
670 switch (rev_compare_tree(revs
, p
, commit
)) {
672 if (!revs
->simplify_history
|| !relevant_commit(p
)) {
673 /* Even if a merge with an uninteresting
674 * side branch brought the entire change
675 * we are interested in, we do not want
676 * to lose the other branches of this
677 * merge, so we just keep going.
680 ts
->treesame
[nth_parent
] = 1;
684 commit
->parents
= parent
;
685 commit
->object
.flags
|= TREESAME
;
689 if (revs
->remove_empty_trees
&&
690 rev_same_tree_as_empty(revs
, p
)) {
691 /* We are adding all the specified
692 * paths from this parent, so the
693 * history beyond this parent is not
694 * interesting. Remove its parents
695 * (they are grandparents for us).
696 * IOW, we pretend this parent is a
699 if (parse_commit(p
) < 0)
700 die("cannot simplify commit %s (invalid %s)",
701 oid_to_hex(&commit
->object
.oid
),
702 oid_to_hex(&p
->object
.oid
));
707 case REV_TREE_DIFFERENT
:
708 if (relevant_commit(p
))
711 irrelevant_change
= 1;
714 die("bad tree compare for commit %s", oid_to_hex(&commit
->object
.oid
));
718 * TREESAME is straightforward for single-parent commits. For merge
719 * commits, it is most useful to define it so that "irrelevant"
720 * parents cannot make us !TREESAME - if we have any relevant
721 * parents, then we only consider TREESAMEness with respect to them,
722 * allowing irrelevant merges from uninteresting branches to be
723 * simplified away. Only if we have only irrelevant parents do we
724 * base TREESAME on them. Note that this logic is replicated in
725 * update_treesame, which should be kept in sync.
727 if (relevant_parents
? !relevant_change
: !irrelevant_change
)
728 commit
->object
.flags
|= TREESAME
;
731 static void commit_list_insert_by_date_cached(struct commit
*p
, struct commit_list
**head
,
732 struct commit_list
*cached_base
, struct commit_list
**cache
)
734 struct commit_list
*new_entry
;
736 if (cached_base
&& p
->date
< cached_base
->item
->date
)
737 new_entry
= commit_list_insert_by_date(p
, &cached_base
->next
);
739 new_entry
= commit_list_insert_by_date(p
, head
);
741 if (cache
&& (!*cache
|| p
->date
< (*cache
)->item
->date
))
745 static int add_parents_to_list(struct rev_info
*revs
, struct commit
*commit
,
746 struct commit_list
**list
, struct commit_list
**cache_ptr
)
748 struct commit_list
*parent
= commit
->parents
;
750 struct commit_list
*cached_base
= cache_ptr
? *cache_ptr
: NULL
;
752 if (commit
->object
.flags
& ADDED
)
754 commit
->object
.flags
|= ADDED
;
756 if (revs
->include_check
&&
757 !revs
->include_check(commit
, revs
->include_check_data
))
761 * If the commit is uninteresting, don't try to
762 * prune parents - we want the maximal uninteresting
765 * Normally we haven't parsed the parent
766 * yet, so we won't have a parent of a parent
767 * here. However, it may turn out that we've
768 * reached this commit some other way (where it
769 * wasn't uninteresting), in which case we need
770 * to mark its parents recursively too..
772 if (commit
->object
.flags
& UNINTERESTING
) {
774 struct commit
*p
= parent
->item
;
775 parent
= parent
->next
;
777 p
->object
.flags
|= UNINTERESTING
;
778 if (parse_commit_gently(p
, 1) < 0)
781 mark_parents_uninteresting(p
);
782 if (p
->object
.flags
& SEEN
)
784 p
->object
.flags
|= SEEN
;
785 commit_list_insert_by_date_cached(p
, list
, cached_base
, cache_ptr
);
791 * Ok, the commit wasn't uninteresting. Try to
792 * simplify the commit history and find the parent
793 * that has no differences in the path set if one exists.
795 try_to_simplify_commit(revs
, commit
);
800 left_flag
= (commit
->object
.flags
& SYMMETRIC_LEFT
);
802 for (parent
= commit
->parents
; parent
; parent
= parent
->next
) {
803 struct commit
*p
= parent
->item
;
804 int gently
= revs
->ignore_missing_links
||
805 revs
->exclude_promisor_objects
;
806 if (parse_commit_gently(p
, gently
) < 0) {
807 if (revs
->exclude_promisor_objects
&&
808 is_promisor_object(&p
->object
.oid
)) {
809 if (revs
->first_parent_only
)
815 if (revs
->show_source
&& !p
->util
)
816 p
->util
= commit
->util
;
817 p
->object
.flags
|= left_flag
;
818 if (!(p
->object
.flags
& SEEN
)) {
819 p
->object
.flags
|= SEEN
;
820 commit_list_insert_by_date_cached(p
, list
, cached_base
, cache_ptr
);
822 if (revs
->first_parent_only
)
828 static void cherry_pick_list(struct commit_list
*list
, struct rev_info
*revs
)
830 struct commit_list
*p
;
831 int left_count
= 0, right_count
= 0;
833 struct patch_ids ids
;
834 unsigned cherry_flag
;
836 /* First count the commits on the left and on the right */
837 for (p
= list
; p
; p
= p
->next
) {
838 struct commit
*commit
= p
->item
;
839 unsigned flags
= commit
->object
.flags
;
840 if (flags
& BOUNDARY
)
842 else if (flags
& SYMMETRIC_LEFT
)
848 if (!left_count
|| !right_count
)
851 left_first
= left_count
< right_count
;
852 init_patch_ids(&ids
);
853 ids
.diffopts
.pathspec
= revs
->diffopt
.pathspec
;
855 /* Compute patch-ids for one side */
856 for (p
= list
; p
; p
= p
->next
) {
857 struct commit
*commit
= p
->item
;
858 unsigned flags
= commit
->object
.flags
;
860 if (flags
& BOUNDARY
)
863 * If we have fewer left, left_first is set and we omit
864 * commits on the right branch in this loop. If we have
865 * fewer right, we skip the left ones.
867 if (left_first
!= !!(flags
& SYMMETRIC_LEFT
))
869 add_commit_patch_id(commit
, &ids
);
872 /* either cherry_mark or cherry_pick are true */
873 cherry_flag
= revs
->cherry_mark
? PATCHSAME
: SHOWN
;
875 /* Check the other side */
876 for (p
= list
; p
; p
= p
->next
) {
877 struct commit
*commit
= p
->item
;
879 unsigned flags
= commit
->object
.flags
;
881 if (flags
& BOUNDARY
)
884 * If we have fewer left, left_first is set and we omit
885 * commits on the left branch in this loop.
887 if (left_first
== !!(flags
& SYMMETRIC_LEFT
))
891 * Have we seen the same patch id?
893 id
= has_commit_patch_id(commit
, &ids
);
897 commit
->object
.flags
|= cherry_flag
;
898 id
->commit
->object
.flags
|= cherry_flag
;
901 free_patch_ids(&ids
);
904 /* How many extra uninteresting commits we want to see.. */
907 static int still_interesting(struct commit_list
*src
, timestamp_t date
, int slop
,
908 struct commit
**interesting_cache
)
911 * No source list at all? We're definitely done..
917 * Does the destination list contain entries with a date
918 * before the source list? Definitely _not_ done.
920 if (date
<= src
->item
->date
)
924 * Does the source list still have interesting commits in
925 * it? Definitely not done..
927 if (!everybody_uninteresting(src
, interesting_cache
))
930 /* Ok, we're closing in.. */
935 * "rev-list --ancestry-path A..B" computes commits that are ancestors
936 * of B but not ancestors of A but further limits the result to those
937 * that are descendants of A. This takes the list of bottom commits and
938 * the result of "A..B" without --ancestry-path, and limits the latter
939 * further to the ones that can reach one of the commits in "bottom".
941 static void limit_to_ancestry(struct commit_list
*bottom
, struct commit_list
*list
)
943 struct commit_list
*p
;
944 struct commit_list
*rlist
= NULL
;
948 * Reverse the list so that it will be likely that we would
949 * process parents before children.
951 for (p
= list
; p
; p
= p
->next
)
952 commit_list_insert(p
->item
, &rlist
);
954 for (p
= bottom
; p
; p
= p
->next
)
955 p
->item
->object
.flags
|= TMP_MARK
;
958 * Mark the ones that can reach bottom commits in "list",
959 * in a bottom-up fashion.
963 for (p
= rlist
; p
; p
= p
->next
) {
964 struct commit
*c
= p
->item
;
965 struct commit_list
*parents
;
966 if (c
->object
.flags
& (TMP_MARK
| UNINTERESTING
))
968 for (parents
= c
->parents
;
970 parents
= parents
->next
) {
971 if (!(parents
->item
->object
.flags
& TMP_MARK
))
973 c
->object
.flags
|= TMP_MARK
;
978 } while (made_progress
);
981 * NEEDSWORK: decide if we want to remove parents that are
982 * not marked with TMP_MARK from commit->parents for commits
983 * in the resulting list. We may not want to do that, though.
987 * The ones that are not marked with TMP_MARK are uninteresting
989 for (p
= list
; p
; p
= p
->next
) {
990 struct commit
*c
= p
->item
;
991 if (c
->object
.flags
& TMP_MARK
)
993 c
->object
.flags
|= UNINTERESTING
;
996 /* We are done with the TMP_MARK */
997 for (p
= list
; p
; p
= p
->next
)
998 p
->item
->object
.flags
&= ~TMP_MARK
;
999 for (p
= bottom
; p
; p
= p
->next
)
1000 p
->item
->object
.flags
&= ~TMP_MARK
;
1001 free_commit_list(rlist
);
1005 * Before walking the history, keep the set of "negative" refs the
1006 * caller has asked to exclude.
1008 * This is used to compute "rev-list --ancestry-path A..B", as we need
1009 * to filter the result of "A..B" further to the ones that can actually
1012 static struct commit_list
*collect_bottom_commits(struct commit_list
*list
)
1014 struct commit_list
*elem
, *bottom
= NULL
;
1015 for (elem
= list
; elem
; elem
= elem
->next
)
1016 if (elem
->item
->object
.flags
& BOTTOM
)
1017 commit_list_insert(elem
->item
, &bottom
);
1021 /* Assumes either left_only or right_only is set */
1022 static void limit_left_right(struct commit_list
*list
, struct rev_info
*revs
)
1024 struct commit_list
*p
;
1026 for (p
= list
; p
; p
= p
->next
) {
1027 struct commit
*commit
= p
->item
;
1029 if (revs
->right_only
) {
1030 if (commit
->object
.flags
& SYMMETRIC_LEFT
)
1031 commit
->object
.flags
|= SHOWN
;
1032 } else /* revs->left_only is set */
1033 if (!(commit
->object
.flags
& SYMMETRIC_LEFT
))
1034 commit
->object
.flags
|= SHOWN
;
1038 static int limit_list(struct rev_info
*revs
)
1041 timestamp_t date
= TIME_MAX
;
1042 struct commit_list
*list
= revs
->commits
;
1043 struct commit_list
*newlist
= NULL
;
1044 struct commit_list
**p
= &newlist
;
1045 struct commit_list
*bottom
= NULL
;
1046 struct commit
*interesting_cache
= NULL
;
1048 if (revs
->ancestry_path
) {
1049 bottom
= collect_bottom_commits(list
);
1051 die("--ancestry-path given but there are no bottom commits");
1055 struct commit
*commit
= pop_commit(&list
);
1056 struct object
*obj
= &commit
->object
;
1057 show_early_output_fn_t show
;
1059 if (commit
== interesting_cache
)
1060 interesting_cache
= NULL
;
1062 if (revs
->max_age
!= -1 && (commit
->date
< revs
->max_age
))
1063 obj
->flags
|= UNINTERESTING
;
1064 if (add_parents_to_list(revs
, commit
, &list
, NULL
) < 0)
1066 if (obj
->flags
& UNINTERESTING
) {
1067 mark_parents_uninteresting(commit
);
1069 p
= &commit_list_insert(commit
, p
)->next
;
1070 slop
= still_interesting(list
, date
, slop
, &interesting_cache
);
1073 /* If showing all, add the whole pending list to the end */
1078 if (revs
->min_age
!= -1 && (commit
->date
> revs
->min_age
))
1080 date
= commit
->date
;
1081 p
= &commit_list_insert(commit
, p
)->next
;
1083 show
= show_early_output
;
1087 show(revs
, newlist
);
1088 show_early_output
= NULL
;
1090 if (revs
->cherry_pick
|| revs
->cherry_mark
)
1091 cherry_pick_list(newlist
, revs
);
1093 if (revs
->left_only
|| revs
->right_only
)
1094 limit_left_right(newlist
, revs
);
1097 limit_to_ancestry(bottom
, newlist
);
1098 free_commit_list(bottom
);
1102 * Check if any commits have become TREESAME by some of their parents
1103 * becoming UNINTERESTING.
1105 if (limiting_can_increase_treesame(revs
))
1106 for (list
= newlist
; list
; list
= list
->next
) {
1107 struct commit
*c
= list
->item
;
1108 if (c
->object
.flags
& (UNINTERESTING
| TREESAME
))
1110 update_treesame(revs
, c
);
1113 revs
->commits
= newlist
;
1118 * Add an entry to refs->cmdline with the specified information.
1121 static void add_rev_cmdline(struct rev_info
*revs
,
1122 struct object
*item
,
1127 struct rev_cmdline_info
*info
= &revs
->cmdline
;
1128 unsigned int nr
= info
->nr
;
1130 ALLOC_GROW(info
->rev
, nr
+ 1, info
->alloc
);
1131 info
->rev
[nr
].item
= item
;
1132 info
->rev
[nr
].name
= xstrdup(name
);
1133 info
->rev
[nr
].whence
= whence
;
1134 info
->rev
[nr
].flags
= flags
;
1138 static void add_rev_cmdline_list(struct rev_info
*revs
,
1139 struct commit_list
*commit_list
,
1143 while (commit_list
) {
1144 struct object
*object
= &commit_list
->item
->object
;
1145 add_rev_cmdline(revs
, object
, oid_to_hex(&object
->oid
),
1147 commit_list
= commit_list
->next
;
1151 struct all_refs_cb
{
1153 int warned_bad_reflog
;
1154 struct rev_info
*all_revs
;
1155 const char *name_for_errormsg
;
1156 struct ref_store
*refs
;
1159 int ref_excluded(struct string_list
*ref_excludes
, const char *path
)
1161 struct string_list_item
*item
;
1165 for_each_string_list_item(item
, ref_excludes
) {
1166 if (!wildmatch(item
->string
, path
, 0))
1172 static int handle_one_ref(const char *path
, const struct object_id
*oid
,
1173 int flag
, void *cb_data
)
1175 struct all_refs_cb
*cb
= cb_data
;
1176 struct object
*object
;
1178 if (ref_excluded(cb
->all_revs
->ref_excludes
, path
))
1181 object
= get_reference(cb
->all_revs
, path
, oid
, cb
->all_flags
);
1182 add_rev_cmdline(cb
->all_revs
, object
, path
, REV_CMD_REF
, cb
->all_flags
);
1183 add_pending_oid(cb
->all_revs
, path
, oid
, cb
->all_flags
);
1187 static void init_all_refs_cb(struct all_refs_cb
*cb
, struct rev_info
*revs
,
1190 cb
->all_revs
= revs
;
1191 cb
->all_flags
= flags
;
1192 revs
->rev_input_given
= 1;
1196 void clear_ref_exclusion(struct string_list
**ref_excludes_p
)
1198 if (*ref_excludes_p
) {
1199 string_list_clear(*ref_excludes_p
, 0);
1200 free(*ref_excludes_p
);
1202 *ref_excludes_p
= NULL
;
1205 void add_ref_exclusion(struct string_list
**ref_excludes_p
, const char *exclude
)
1207 if (!*ref_excludes_p
) {
1208 *ref_excludes_p
= xcalloc(1, sizeof(**ref_excludes_p
));
1209 (*ref_excludes_p
)->strdup_strings
= 1;
1211 string_list_append(*ref_excludes_p
, exclude
);
1214 static void handle_refs(struct ref_store
*refs
,
1215 struct rev_info
*revs
, unsigned flags
,
1216 int (*for_each
)(struct ref_store
*, each_ref_fn
, void *))
1218 struct all_refs_cb cb
;
1221 /* this could happen with uninitialized submodules */
1225 init_all_refs_cb(&cb
, revs
, flags
);
1226 for_each(refs
, handle_one_ref
, &cb
);
1229 static void handle_one_reflog_commit(struct object_id
*oid
, void *cb_data
)
1231 struct all_refs_cb
*cb
= cb_data
;
1232 if (!is_null_oid(oid
)) {
1233 struct object
*o
= parse_object(oid
);
1235 o
->flags
|= cb
->all_flags
;
1236 /* ??? CMDLINEFLAGS ??? */
1237 add_pending_object(cb
->all_revs
, o
, "");
1239 else if (!cb
->warned_bad_reflog
) {
1240 warning("reflog of '%s' references pruned commits",
1241 cb
->name_for_errormsg
);
1242 cb
->warned_bad_reflog
= 1;
1247 static int handle_one_reflog_ent(struct object_id
*ooid
, struct object_id
*noid
,
1248 const char *email
, timestamp_t timestamp
, int tz
,
1249 const char *message
, void *cb_data
)
1251 handle_one_reflog_commit(ooid
, cb_data
);
1252 handle_one_reflog_commit(noid
, cb_data
);
1256 static int handle_one_reflog(const char *path
, const struct object_id
*oid
,
1257 int flag
, void *cb_data
)
1259 struct all_refs_cb
*cb
= cb_data
;
1260 cb
->warned_bad_reflog
= 0;
1261 cb
->name_for_errormsg
= path
;
1262 refs_for_each_reflog_ent(cb
->refs
, path
,
1263 handle_one_reflog_ent
, cb_data
);
1267 static void add_other_reflogs_to_pending(struct all_refs_cb
*cb
)
1269 struct worktree
**worktrees
, **p
;
1271 worktrees
= get_worktrees(0);
1272 for (p
= worktrees
; *p
; p
++) {
1273 struct worktree
*wt
= *p
;
1278 cb
->refs
= get_worktree_ref_store(wt
);
1279 refs_for_each_reflog(cb
->refs
,
1283 free_worktrees(worktrees
);
1286 void add_reflogs_to_pending(struct rev_info
*revs
, unsigned flags
)
1288 struct all_refs_cb cb
;
1291 cb
.all_flags
= flags
;
1292 cb
.refs
= get_main_ref_store();
1293 for_each_reflog(handle_one_reflog
, &cb
);
1295 if (!revs
->single_worktree
)
1296 add_other_reflogs_to_pending(&cb
);
1299 static void add_cache_tree(struct cache_tree
*it
, struct rev_info
*revs
,
1300 struct strbuf
*path
)
1302 size_t baselen
= path
->len
;
1305 if (it
->entry_count
>= 0) {
1306 struct tree
*tree
= lookup_tree(&it
->oid
);
1307 add_pending_object_with_path(revs
, &tree
->object
, "",
1311 for (i
= 0; i
< it
->subtree_nr
; i
++) {
1312 struct cache_tree_sub
*sub
= it
->down
[i
];
1313 strbuf_addf(path
, "%s%s", baselen
? "/" : "", sub
->name
);
1314 add_cache_tree(sub
->cache_tree
, revs
, path
);
1315 strbuf_setlen(path
, baselen
);
1320 static void do_add_index_objects_to_pending(struct rev_info
*revs
,
1321 struct index_state
*istate
)
1325 for (i
= 0; i
< istate
->cache_nr
; i
++) {
1326 struct cache_entry
*ce
= istate
->cache
[i
];
1329 if (S_ISGITLINK(ce
->ce_mode
))
1332 blob
= lookup_blob(&ce
->oid
);
1334 die("unable to add index blob to traversal");
1335 add_pending_object_with_path(revs
, &blob
->object
, "",
1336 ce
->ce_mode
, ce
->name
);
1339 if (istate
->cache_tree
) {
1340 struct strbuf path
= STRBUF_INIT
;
1341 add_cache_tree(istate
->cache_tree
, revs
, &path
);
1342 strbuf_release(&path
);
1346 void add_index_objects_to_pending(struct rev_info
*revs
, unsigned int flags
)
1348 struct worktree
**worktrees
, **p
;
1351 do_add_index_objects_to_pending(revs
, &the_index
);
1353 if (revs
->single_worktree
)
1356 worktrees
= get_worktrees(0);
1357 for (p
= worktrees
; *p
; p
++) {
1358 struct worktree
*wt
= *p
;
1359 struct index_state istate
= { NULL
};
1362 continue; /* current index already taken care of */
1364 if (read_index_from(&istate
,
1365 worktree_git_path(wt
, "index")) > 0)
1366 do_add_index_objects_to_pending(revs
, &istate
);
1367 discard_index(&istate
);
1369 free_worktrees(worktrees
);
1372 static int add_parents_only(struct rev_info
*revs
, const char *arg_
, int flags
,
1375 struct object_id oid
;
1377 struct commit
*commit
;
1378 struct commit_list
*parents
;
1380 const char *arg
= arg_
;
1383 flags
^= UNINTERESTING
| BOTTOM
;
1386 if (get_oid_committish(arg
, &oid
))
1389 it
= get_reference(revs
, arg
, &oid
, 0);
1390 if (!it
&& revs
->ignore_missing
)
1392 if (it
->type
!= OBJ_TAG
)
1394 if (!((struct tag
*)it
)->tagged
)
1396 oidcpy(&oid
, &((struct tag
*)it
)->tagged
->oid
);
1398 if (it
->type
!= OBJ_COMMIT
)
1400 commit
= (struct commit
*)it
;
1401 if (exclude_parent
&&
1402 exclude_parent
> commit_list_count(commit
->parents
))
1404 for (parents
= commit
->parents
, parent_number
= 1;
1406 parents
= parents
->next
, parent_number
++) {
1407 if (exclude_parent
&& parent_number
!= exclude_parent
)
1410 it
= &parents
->item
->object
;
1412 add_rev_cmdline(revs
, it
, arg_
, REV_CMD_PARENTS_ONLY
, flags
);
1413 add_pending_object(revs
, it
, arg
);
1418 void init_revisions(struct rev_info
*revs
, const char *prefix
)
1420 memset(revs
, 0, sizeof(*revs
));
1422 revs
->abbrev
= DEFAULT_ABBREV
;
1423 revs
->ignore_merges
= 1;
1424 revs
->simplify_history
= 1;
1425 revs
->pruning
.flags
.recursive
= 1;
1426 revs
->pruning
.flags
.quick
= 1;
1427 revs
->pruning
.add_remove
= file_add_remove
;
1428 revs
->pruning
.change
= file_change
;
1429 revs
->pruning
.change_fn_data
= revs
;
1430 revs
->sort_order
= REV_SORT_IN_GRAPH_ORDER
;
1432 revs
->prefix
= prefix
;
1435 revs
->skip_count
= -1;
1436 revs
->max_count
= -1;
1437 revs
->max_parents
= -1;
1438 revs
->expand_tabs_in_log
= -1;
1440 revs
->commit_format
= CMIT_FMT_DEFAULT
;
1441 revs
->expand_tabs_in_log_default
= 8;
1443 init_grep_defaults();
1444 grep_init(&revs
->grep_filter
, prefix
);
1445 revs
->grep_filter
.status_only
= 1;
1447 diff_setup(&revs
->diffopt
);
1448 if (prefix
&& !revs
->diffopt
.prefix
) {
1449 revs
->diffopt
.prefix
= prefix
;
1450 revs
->diffopt
.prefix_length
= strlen(prefix
);
1453 revs
->notes_opt
.use_default_notes
= -1;
1456 static void add_pending_commit_list(struct rev_info
*revs
,
1457 struct commit_list
*commit_list
,
1460 while (commit_list
) {
1461 struct object
*object
= &commit_list
->item
->object
;
1462 object
->flags
|= flags
;
1463 add_pending_object(revs
, object
, oid_to_hex(&object
->oid
));
1464 commit_list
= commit_list
->next
;
1468 static void prepare_show_merge(struct rev_info
*revs
)
1470 struct commit_list
*bases
;
1471 struct commit
*head
, *other
;
1472 struct object_id oid
;
1473 const char **prune
= NULL
;
1474 int i
, prune_num
= 1; /* counting terminating NULL */
1476 if (get_oid("HEAD", &oid
))
1477 die("--merge without HEAD?");
1478 head
= lookup_commit_or_die(&oid
, "HEAD");
1479 if (get_oid("MERGE_HEAD", &oid
))
1480 die("--merge without MERGE_HEAD?");
1481 other
= lookup_commit_or_die(&oid
, "MERGE_HEAD");
1482 add_pending_object(revs
, &head
->object
, "HEAD");
1483 add_pending_object(revs
, &other
->object
, "MERGE_HEAD");
1484 bases
= get_merge_bases(head
, other
);
1485 add_rev_cmdline_list(revs
, bases
, REV_CMD_MERGE_BASE
, UNINTERESTING
| BOTTOM
);
1486 add_pending_commit_list(revs
, bases
, UNINTERESTING
| BOTTOM
);
1487 free_commit_list(bases
);
1488 head
->object
.flags
|= SYMMETRIC_LEFT
;
1492 for (i
= 0; i
< active_nr
; i
++) {
1493 const struct cache_entry
*ce
= active_cache
[i
];
1496 if (ce_path_match(ce
, &revs
->prune_data
, NULL
)) {
1498 REALLOC_ARRAY(prune
, prune_num
);
1499 prune
[prune_num
-2] = ce
->name
;
1500 prune
[prune_num
-1] = NULL
;
1502 while ((i
+1 < active_nr
) &&
1503 ce_same_name(ce
, active_cache
[i
+1]))
1506 clear_pathspec(&revs
->prune_data
);
1507 parse_pathspec(&revs
->prune_data
, PATHSPEC_ALL_MAGIC
& ~PATHSPEC_LITERAL
,
1508 PATHSPEC_PREFER_FULL
| PATHSPEC_LITERAL_PATH
, "", prune
);
1512 static int dotdot_missing(const char *arg
, char *dotdot
,
1513 struct rev_info
*revs
, int symmetric
)
1515 if (revs
->ignore_missing
)
1517 /* de-munge so we report the full argument */
1520 ? "Invalid symmetric difference expression %s"
1521 : "Invalid revision range %s", arg
);
1524 static int handle_dotdot_1(const char *arg
, char *dotdot
,
1525 struct rev_info
*revs
, int flags
,
1526 int cant_be_filename
,
1527 struct object_context
*a_oc
,
1528 struct object_context
*b_oc
)
1530 const char *a_name
, *b_name
;
1531 struct object_id a_oid
, b_oid
;
1532 struct object
*a_obj
, *b_obj
;
1533 unsigned int a_flags
, b_flags
;
1535 unsigned int flags_exclude
= flags
^ (UNINTERESTING
| BOTTOM
);
1536 unsigned int oc_flags
= GET_OID_COMMITTISH
| GET_OID_RECORD_PATH
;
1542 b_name
= dotdot
+ 2;
1543 if (*b_name
== '.') {
1550 if (get_oid_with_context(a_name
, oc_flags
, &a_oid
, a_oc
) ||
1551 get_oid_with_context(b_name
, oc_flags
, &b_oid
, b_oc
))
1554 if (!cant_be_filename
) {
1556 verify_non_filename(revs
->prefix
, arg
);
1560 a_obj
= parse_object(&a_oid
);
1561 b_obj
= parse_object(&b_oid
);
1562 if (!a_obj
|| !b_obj
)
1563 return dotdot_missing(arg
, dotdot
, revs
, symmetric
);
1568 a_flags
= flags_exclude
;
1570 /* A...B -- find merge bases between the two */
1571 struct commit
*a
, *b
;
1572 struct commit_list
*exclude
;
1574 a
= lookup_commit_reference(&a_obj
->oid
);
1575 b
= lookup_commit_reference(&b_obj
->oid
);
1577 return dotdot_missing(arg
, dotdot
, revs
, symmetric
);
1579 exclude
= get_merge_bases(a
, b
);
1580 add_rev_cmdline_list(revs
, exclude
, REV_CMD_MERGE_BASE
,
1582 add_pending_commit_list(revs
, exclude
, flags_exclude
);
1583 free_commit_list(exclude
);
1586 a_flags
= flags
| SYMMETRIC_LEFT
;
1589 a_obj
->flags
|= a_flags
;
1590 b_obj
->flags
|= b_flags
;
1591 add_rev_cmdline(revs
, a_obj
, a_name
, REV_CMD_LEFT
, a_flags
);
1592 add_rev_cmdline(revs
, b_obj
, b_name
, REV_CMD_RIGHT
, b_flags
);
1593 add_pending_object_with_path(revs
, a_obj
, a_name
, a_oc
->mode
, a_oc
->path
);
1594 add_pending_object_with_path(revs
, b_obj
, b_name
, b_oc
->mode
, b_oc
->path
);
1598 static int handle_dotdot(const char *arg
,
1599 struct rev_info
*revs
, int flags
,
1600 int cant_be_filename
)
1602 struct object_context a_oc
, b_oc
;
1603 char *dotdot
= strstr(arg
, "..");
1609 memset(&a_oc
, 0, sizeof(a_oc
));
1610 memset(&b_oc
, 0, sizeof(b_oc
));
1613 ret
= handle_dotdot_1(arg
, dotdot
, revs
, flags
, cant_be_filename
,
1623 int handle_revision_arg(const char *arg_
, struct rev_info
*revs
, int flags
, unsigned revarg_opt
)
1625 struct object_context oc
;
1627 struct object
*object
;
1628 struct object_id oid
;
1630 const char *arg
= arg_
;
1631 int cant_be_filename
= revarg_opt
& REVARG_CANNOT_BE_FILENAME
;
1632 unsigned get_sha1_flags
= GET_OID_RECORD_PATH
;
1634 flags
= flags
& UNINTERESTING
? flags
| BOTTOM
: flags
& ~BOTTOM
;
1636 if (!cant_be_filename
&& !strcmp(arg
, "..")) {
1638 * Just ".."? That is not a range but the
1639 * pathspec for the parent directory.
1644 if (!handle_dotdot(arg
, revs
, flags
, revarg_opt
))
1647 mark
= strstr(arg
, "^@");
1648 if (mark
&& !mark
[2]) {
1650 if (add_parents_only(revs
, arg
, flags
, 0))
1654 mark
= strstr(arg
, "^!");
1655 if (mark
&& !mark
[2]) {
1657 if (!add_parents_only(revs
, arg
, flags
^ (UNINTERESTING
| BOTTOM
), 0))
1660 mark
= strstr(arg
, "^-");
1662 int exclude_parent
= 1;
1666 exclude_parent
= strtoul(mark
+ 2, &end
, 10);
1667 if (*end
!= '\0' || !exclude_parent
)
1672 if (!add_parents_only(revs
, arg
, flags
^ (UNINTERESTING
| BOTTOM
), exclude_parent
))
1678 local_flags
= UNINTERESTING
| BOTTOM
;
1682 if (revarg_opt
& REVARG_COMMITTISH
)
1683 get_sha1_flags
|= GET_OID_COMMITTISH
;
1685 if (get_oid_with_context(arg
, get_sha1_flags
, &oid
, &oc
))
1686 return revs
->ignore_missing
? 0 : -1;
1687 if (!cant_be_filename
)
1688 verify_non_filename(revs
->prefix
, arg
);
1689 object
= get_reference(revs
, arg
, &oid
, flags
^ local_flags
);
1690 add_rev_cmdline(revs
, object
, arg_
, REV_CMD_REV
, flags
^ local_flags
);
1691 add_pending_object_with_path(revs
, object
, arg
, oc
.mode
, oc
.path
);
1696 static void read_pathspec_from_stdin(struct rev_info
*revs
, struct strbuf
*sb
,
1697 struct argv_array
*prune
)
1699 while (strbuf_getline(sb
, stdin
) != EOF
)
1700 argv_array_push(prune
, sb
->buf
);
1703 static void read_revisions_from_stdin(struct rev_info
*revs
,
1704 struct argv_array
*prune
)
1707 int seen_dashdash
= 0;
1710 save_warning
= warn_on_object_refname_ambiguity
;
1711 warn_on_object_refname_ambiguity
= 0;
1713 strbuf_init(&sb
, 1000);
1714 while (strbuf_getline(&sb
, stdin
) != EOF
) {
1718 if (sb
.buf
[0] == '-') {
1719 if (len
== 2 && sb
.buf
[1] == '-') {
1723 die("options not supported in --stdin mode");
1725 if (handle_revision_arg(sb
.buf
, revs
, 0,
1726 REVARG_CANNOT_BE_FILENAME
))
1727 die("bad revision '%s'", sb
.buf
);
1730 read_pathspec_from_stdin(revs
, &sb
, prune
);
1732 strbuf_release(&sb
);
1733 warn_on_object_refname_ambiguity
= save_warning
;
1736 static void add_grep(struct rev_info
*revs
, const char *ptn
, enum grep_pat_token what
)
1738 append_grep_pattern(&revs
->grep_filter
, ptn
, "command line", 0, what
);
1741 static void add_header_grep(struct rev_info
*revs
, enum grep_header_field field
, const char *pattern
)
1743 append_header_grep_pattern(&revs
->grep_filter
, field
, pattern
);
1746 static void add_message_grep(struct rev_info
*revs
, const char *pattern
)
1748 add_grep(revs
, pattern
, GREP_PATTERN_BODY
);
1751 static int handle_revision_opt(struct rev_info
*revs
, int argc
, const char **argv
,
1752 int *unkc
, const char **unkv
)
1754 const char *arg
= argv
[0];
1758 /* pseudo revision arguments */
1759 if (!strcmp(arg
, "--all") || !strcmp(arg
, "--branches") ||
1760 !strcmp(arg
, "--tags") || !strcmp(arg
, "--remotes") ||
1761 !strcmp(arg
, "--reflog") || !strcmp(arg
, "--not") ||
1762 !strcmp(arg
, "--no-walk") || !strcmp(arg
, "--do-walk") ||
1763 !strcmp(arg
, "--bisect") || starts_with(arg
, "--glob=") ||
1764 !strcmp(arg
, "--indexed-objects") ||
1765 starts_with(arg
, "--exclude=") ||
1766 starts_with(arg
, "--branches=") || starts_with(arg
, "--tags=") ||
1767 starts_with(arg
, "--remotes=") || starts_with(arg
, "--no-walk="))
1769 unkv
[(*unkc
)++] = arg
;
1773 if ((argcount
= parse_long_opt("max-count", argv
, &optarg
))) {
1774 revs
->max_count
= atoi(optarg
);
1777 } else if ((argcount
= parse_long_opt("skip", argv
, &optarg
))) {
1778 revs
->skip_count
= atoi(optarg
);
1780 } else if ((*arg
== '-') && isdigit(arg
[1])) {
1781 /* accept -<digit>, like traditional "head" */
1782 if (strtol_i(arg
+ 1, 10, &revs
->max_count
) < 0 ||
1783 revs
->max_count
< 0)
1784 die("'%s': not a non-negative integer", arg
+ 1);
1786 } else if (!strcmp(arg
, "-n")) {
1788 return error("-n requires an argument");
1789 revs
->max_count
= atoi(argv
[1]);
1792 } else if (skip_prefix(arg
, "-n", &optarg
)) {
1793 revs
->max_count
= atoi(optarg
);
1795 } else if ((argcount
= parse_long_opt("max-age", argv
, &optarg
))) {
1796 revs
->max_age
= atoi(optarg
);
1798 } else if ((argcount
= parse_long_opt("since", argv
, &optarg
))) {
1799 revs
->max_age
= approxidate(optarg
);
1801 } else if ((argcount
= parse_long_opt("after", argv
, &optarg
))) {
1802 revs
->max_age
= approxidate(optarg
);
1804 } else if ((argcount
= parse_long_opt("min-age", argv
, &optarg
))) {
1805 revs
->min_age
= atoi(optarg
);
1807 } else if ((argcount
= parse_long_opt("before", argv
, &optarg
))) {
1808 revs
->min_age
= approxidate(optarg
);
1810 } else if ((argcount
= parse_long_opt("until", argv
, &optarg
))) {
1811 revs
->min_age
= approxidate(optarg
);
1813 } else if (!strcmp(arg
, "--first-parent")) {
1814 revs
->first_parent_only
= 1;
1815 } else if (!strcmp(arg
, "--ancestry-path")) {
1816 revs
->ancestry_path
= 1;
1817 revs
->simplify_history
= 0;
1819 } else if (!strcmp(arg
, "-g") || !strcmp(arg
, "--walk-reflogs")) {
1820 init_reflog_walk(&revs
->reflog_info
);
1821 } else if (!strcmp(arg
, "--default")) {
1823 return error("bad --default argument");
1824 revs
->def
= argv
[1];
1826 } else if (!strcmp(arg
, "--merge")) {
1827 revs
->show_merge
= 1;
1828 } else if (!strcmp(arg
, "--topo-order")) {
1829 revs
->sort_order
= REV_SORT_IN_GRAPH_ORDER
;
1830 revs
->topo_order
= 1;
1831 } else if (!strcmp(arg
, "--simplify-merges")) {
1832 revs
->simplify_merges
= 1;
1833 revs
->topo_order
= 1;
1834 revs
->rewrite_parents
= 1;
1835 revs
->simplify_history
= 0;
1837 } else if (!strcmp(arg
, "--simplify-by-decoration")) {
1838 revs
->simplify_merges
= 1;
1839 revs
->topo_order
= 1;
1840 revs
->rewrite_parents
= 1;
1841 revs
->simplify_history
= 0;
1842 revs
->simplify_by_decoration
= 1;
1845 load_ref_decorations(NULL
, DECORATE_SHORT_REFS
);
1846 } else if (!strcmp(arg
, "--date-order")) {
1847 revs
->sort_order
= REV_SORT_BY_COMMIT_DATE
;
1848 revs
->topo_order
= 1;
1849 } else if (!strcmp(arg
, "--author-date-order")) {
1850 revs
->sort_order
= REV_SORT_BY_AUTHOR_DATE
;
1851 revs
->topo_order
= 1;
1852 } else if (!strcmp(arg
, "--early-output")) {
1853 revs
->early_output
= 100;
1854 revs
->topo_order
= 1;
1855 } else if (skip_prefix(arg
, "--early-output=", &optarg
)) {
1856 if (strtoul_ui(optarg
, 10, &revs
->early_output
) < 0)
1857 die("'%s': not a non-negative integer", optarg
);
1858 revs
->topo_order
= 1;
1859 } else if (!strcmp(arg
, "--parents")) {
1860 revs
->rewrite_parents
= 1;
1861 revs
->print_parents
= 1;
1862 } else if (!strcmp(arg
, "--dense")) {
1864 } else if (!strcmp(arg
, "--sparse")) {
1866 } else if (!strcmp(arg
, "--show-all")) {
1868 } else if (!strcmp(arg
, "--in-commit-order")) {
1869 revs
->tree_blobs_in_commit_order
= 1;
1870 } else if (!strcmp(arg
, "--remove-empty")) {
1871 revs
->remove_empty_trees
= 1;
1872 } else if (!strcmp(arg
, "--merges")) {
1873 revs
->min_parents
= 2;
1874 } else if (!strcmp(arg
, "--no-merges")) {
1875 revs
->max_parents
= 1;
1876 } else if (skip_prefix(arg
, "--min-parents=", &optarg
)) {
1877 revs
->min_parents
= atoi(optarg
);
1878 } else if (!strcmp(arg
, "--no-min-parents")) {
1879 revs
->min_parents
= 0;
1880 } else if (skip_prefix(arg
, "--max-parents=", &optarg
)) {
1881 revs
->max_parents
= atoi(optarg
);
1882 } else if (!strcmp(arg
, "--no-max-parents")) {
1883 revs
->max_parents
= -1;
1884 } else if (!strcmp(arg
, "--boundary")) {
1886 } else if (!strcmp(arg
, "--left-right")) {
1887 revs
->left_right
= 1;
1888 } else if (!strcmp(arg
, "--left-only")) {
1889 if (revs
->right_only
)
1890 die("--left-only is incompatible with --right-only"
1892 revs
->left_only
= 1;
1893 } else if (!strcmp(arg
, "--right-only")) {
1894 if (revs
->left_only
)
1895 die("--right-only is incompatible with --left-only");
1896 revs
->right_only
= 1;
1897 } else if (!strcmp(arg
, "--cherry")) {
1898 if (revs
->left_only
)
1899 die("--cherry is incompatible with --left-only");
1900 revs
->cherry_mark
= 1;
1901 revs
->right_only
= 1;
1902 revs
->max_parents
= 1;
1904 } else if (!strcmp(arg
, "--count")) {
1906 } else if (!strcmp(arg
, "--cherry-mark")) {
1907 if (revs
->cherry_pick
)
1908 die("--cherry-mark is incompatible with --cherry-pick");
1909 revs
->cherry_mark
= 1;
1910 revs
->limited
= 1; /* needs limit_list() */
1911 } else if (!strcmp(arg
, "--cherry-pick")) {
1912 if (revs
->cherry_mark
)
1913 die("--cherry-pick is incompatible with --cherry-mark");
1914 revs
->cherry_pick
= 1;
1916 } else if (!strcmp(arg
, "--objects")) {
1917 revs
->tag_objects
= 1;
1918 revs
->tree_objects
= 1;
1919 revs
->blob_objects
= 1;
1920 } else if (!strcmp(arg
, "--objects-edge")) {
1921 revs
->tag_objects
= 1;
1922 revs
->tree_objects
= 1;
1923 revs
->blob_objects
= 1;
1924 revs
->edge_hint
= 1;
1925 } else if (!strcmp(arg
, "--objects-edge-aggressive")) {
1926 revs
->tag_objects
= 1;
1927 revs
->tree_objects
= 1;
1928 revs
->blob_objects
= 1;
1929 revs
->edge_hint
= 1;
1930 revs
->edge_hint_aggressive
= 1;
1931 } else if (!strcmp(arg
, "--verify-objects")) {
1932 revs
->tag_objects
= 1;
1933 revs
->tree_objects
= 1;
1934 revs
->blob_objects
= 1;
1935 revs
->verify_objects
= 1;
1936 } else if (!strcmp(arg
, "--unpacked")) {
1938 } else if (starts_with(arg
, "--unpacked=")) {
1939 die("--unpacked=<packfile> no longer supported.");
1940 } else if (!strcmp(arg
, "-r")) {
1942 revs
->diffopt
.flags
.recursive
= 1;
1943 } else if (!strcmp(arg
, "-t")) {
1945 revs
->diffopt
.flags
.recursive
= 1;
1946 revs
->diffopt
.flags
.tree_in_recursive
= 1;
1947 } else if (!strcmp(arg
, "-m")) {
1948 revs
->ignore_merges
= 0;
1949 } else if (!strcmp(arg
, "-c")) {
1951 revs
->dense_combined_merges
= 0;
1952 revs
->combine_merges
= 1;
1953 } else if (!strcmp(arg
, "--cc")) {
1955 revs
->dense_combined_merges
= 1;
1956 revs
->combine_merges
= 1;
1957 } else if (!strcmp(arg
, "-v")) {
1958 revs
->verbose_header
= 1;
1959 } else if (!strcmp(arg
, "--pretty")) {
1960 revs
->verbose_header
= 1;
1961 revs
->pretty_given
= 1;
1962 get_commit_format(NULL
, revs
);
1963 } else if (skip_prefix(arg
, "--pretty=", &optarg
) ||
1964 skip_prefix(arg
, "--format=", &optarg
)) {
1966 * Detached form ("--pretty X" as opposed to "--pretty=X")
1967 * not allowed, since the argument is optional.
1969 revs
->verbose_header
= 1;
1970 revs
->pretty_given
= 1;
1971 get_commit_format(optarg
, revs
);
1972 } else if (!strcmp(arg
, "--expand-tabs")) {
1973 revs
->expand_tabs_in_log
= 8;
1974 } else if (!strcmp(arg
, "--no-expand-tabs")) {
1975 revs
->expand_tabs_in_log
= 0;
1976 } else if (skip_prefix(arg
, "--expand-tabs=", &arg
)) {
1978 if (strtol_i(arg
, 10, &val
) < 0 || val
< 0)
1979 die("'%s': not a non-negative integer", arg
);
1980 revs
->expand_tabs_in_log
= val
;
1981 } else if (!strcmp(arg
, "--show-notes") || !strcmp(arg
, "--notes")) {
1982 revs
->show_notes
= 1;
1983 revs
->show_notes_given
= 1;
1984 revs
->notes_opt
.use_default_notes
= 1;
1985 } else if (!strcmp(arg
, "--show-signature")) {
1986 revs
->show_signature
= 1;
1987 } else if (!strcmp(arg
, "--no-show-signature")) {
1988 revs
->show_signature
= 0;
1989 } else if (!strcmp(arg
, "--show-linear-break")) {
1990 revs
->break_bar
= " ..........";
1991 revs
->track_linear
= 1;
1992 revs
->track_first_time
= 1;
1993 } else if (skip_prefix(arg
, "--show-linear-break=", &optarg
)) {
1994 revs
->break_bar
= xstrdup(optarg
);
1995 revs
->track_linear
= 1;
1996 revs
->track_first_time
= 1;
1997 } else if (skip_prefix(arg
, "--show-notes=", &optarg
) ||
1998 skip_prefix(arg
, "--notes=", &optarg
)) {
1999 struct strbuf buf
= STRBUF_INIT
;
2000 revs
->show_notes
= 1;
2001 revs
->show_notes_given
= 1;
2002 if (starts_with(arg
, "--show-notes=") &&
2003 revs
->notes_opt
.use_default_notes
< 0)
2004 revs
->notes_opt
.use_default_notes
= 1;
2005 strbuf_addstr(&buf
, optarg
);
2006 expand_notes_ref(&buf
);
2007 string_list_append(&revs
->notes_opt
.extra_notes_refs
,
2008 strbuf_detach(&buf
, NULL
));
2009 } else if (!strcmp(arg
, "--no-notes")) {
2010 revs
->show_notes
= 0;
2011 revs
->show_notes_given
= 1;
2012 revs
->notes_opt
.use_default_notes
= -1;
2013 /* we have been strdup'ing ourselves, so trick
2014 * string_list into free()ing strings */
2015 revs
->notes_opt
.extra_notes_refs
.strdup_strings
= 1;
2016 string_list_clear(&revs
->notes_opt
.extra_notes_refs
, 0);
2017 revs
->notes_opt
.extra_notes_refs
.strdup_strings
= 0;
2018 } else if (!strcmp(arg
, "--standard-notes")) {
2019 revs
->show_notes_given
= 1;
2020 revs
->notes_opt
.use_default_notes
= 1;
2021 } else if (!strcmp(arg
, "--no-standard-notes")) {
2022 revs
->notes_opt
.use_default_notes
= 0;
2023 } else if (!strcmp(arg
, "--oneline")) {
2024 revs
->verbose_header
= 1;
2025 get_commit_format("oneline", revs
);
2026 revs
->pretty_given
= 1;
2027 revs
->abbrev_commit
= 1;
2028 } else if (!strcmp(arg
, "--graph")) {
2029 revs
->topo_order
= 1;
2030 revs
->rewrite_parents
= 1;
2031 revs
->graph
= graph_init(revs
);
2032 } else if (!strcmp(arg
, "--root")) {
2033 revs
->show_root_diff
= 1;
2034 } else if (!strcmp(arg
, "--no-commit-id")) {
2035 revs
->no_commit_id
= 1;
2036 } else if (!strcmp(arg
, "--always")) {
2037 revs
->always_show_header
= 1;
2038 } else if (!strcmp(arg
, "--no-abbrev")) {
2040 } else if (!strcmp(arg
, "--abbrev")) {
2041 revs
->abbrev
= DEFAULT_ABBREV
;
2042 } else if (skip_prefix(arg
, "--abbrev=", &optarg
)) {
2043 revs
->abbrev
= strtoul(optarg
, NULL
, 10);
2044 if (revs
->abbrev
< MINIMUM_ABBREV
)
2045 revs
->abbrev
= MINIMUM_ABBREV
;
2046 else if (revs
->abbrev
> 40)
2048 } else if (!strcmp(arg
, "--abbrev-commit")) {
2049 revs
->abbrev_commit
= 1;
2050 revs
->abbrev_commit_given
= 1;
2051 } else if (!strcmp(arg
, "--no-abbrev-commit")) {
2052 revs
->abbrev_commit
= 0;
2053 } else if (!strcmp(arg
, "--full-diff")) {
2055 revs
->full_diff
= 1;
2056 } else if (!strcmp(arg
, "--full-history")) {
2057 revs
->simplify_history
= 0;
2058 } else if (!strcmp(arg
, "--relative-date")) {
2059 revs
->date_mode
.type
= DATE_RELATIVE
;
2060 revs
->date_mode_explicit
= 1;
2061 } else if ((argcount
= parse_long_opt("date", argv
, &optarg
))) {
2062 parse_date_format(optarg
, &revs
->date_mode
);
2063 revs
->date_mode_explicit
= 1;
2065 } else if (!strcmp(arg
, "--log-size")) {
2066 revs
->show_log_size
= 1;
2069 * Grepping the commit log
2071 else if ((argcount
= parse_long_opt("author", argv
, &optarg
))) {
2072 add_header_grep(revs
, GREP_HEADER_AUTHOR
, optarg
);
2074 } else if ((argcount
= parse_long_opt("committer", argv
, &optarg
))) {
2075 add_header_grep(revs
, GREP_HEADER_COMMITTER
, optarg
);
2077 } else if ((argcount
= parse_long_opt("grep-reflog", argv
, &optarg
))) {
2078 add_header_grep(revs
, GREP_HEADER_REFLOG
, optarg
);
2080 } else if ((argcount
= parse_long_opt("grep", argv
, &optarg
))) {
2081 add_message_grep(revs
, optarg
);
2083 } else if (!strcmp(arg
, "--grep-debug")) {
2084 revs
->grep_filter
.debug
= 1;
2085 } else if (!strcmp(arg
, "--basic-regexp")) {
2086 revs
->grep_filter
.pattern_type_option
= GREP_PATTERN_TYPE_BRE
;
2087 } else if (!strcmp(arg
, "--extended-regexp") || !strcmp(arg
, "-E")) {
2088 revs
->grep_filter
.pattern_type_option
= GREP_PATTERN_TYPE_ERE
;
2089 } else if (!strcmp(arg
, "--regexp-ignore-case") || !strcmp(arg
, "-i")) {
2090 revs
->grep_filter
.ignore_case
= 1;
2091 revs
->diffopt
.pickaxe_opts
|= DIFF_PICKAXE_IGNORE_CASE
;
2092 } else if (!strcmp(arg
, "--fixed-strings") || !strcmp(arg
, "-F")) {
2093 revs
->grep_filter
.pattern_type_option
= GREP_PATTERN_TYPE_FIXED
;
2094 } else if (!strcmp(arg
, "--perl-regexp") || !strcmp(arg
, "-P")) {
2095 revs
->grep_filter
.pattern_type_option
= GREP_PATTERN_TYPE_PCRE
;
2096 } else if (!strcmp(arg
, "--all-match")) {
2097 revs
->grep_filter
.all_match
= 1;
2098 } else if (!strcmp(arg
, "--invert-grep")) {
2099 revs
->invert_grep
= 1;
2100 } else if ((argcount
= parse_long_opt("encoding", argv
, &optarg
))) {
2101 if (strcmp(optarg
, "none"))
2102 git_log_output_encoding
= xstrdup(optarg
);
2104 git_log_output_encoding
= "";
2106 } else if (!strcmp(arg
, "--reverse")) {
2108 } else if (!strcmp(arg
, "--children")) {
2109 revs
->children
.name
= "children";
2111 } else if (!strcmp(arg
, "--ignore-missing")) {
2112 revs
->ignore_missing
= 1;
2113 } else if (!strcmp(arg
, "--exclude-promisor-objects")) {
2114 if (fetch_if_missing
)
2115 die("BUG: exclude_promisor_objects can only be used when fetch_if_missing is 0");
2116 revs
->exclude_promisor_objects
= 1;
2118 int opts
= diff_opt_parse(&revs
->diffopt
, argv
, argc
, revs
->prefix
);
2120 unkv
[(*unkc
)++] = arg
;
2123 if (revs
->graph
&& revs
->track_linear
)
2124 die("--show-linear-break and --graph are incompatible");
2129 void parse_revision_opt(struct rev_info
*revs
, struct parse_opt_ctx_t
*ctx
,
2130 const struct option
*options
,
2131 const char * const usagestr
[])
2133 int n
= handle_revision_opt(revs
, ctx
->argc
, ctx
->argv
,
2134 &ctx
->cpidx
, ctx
->out
);
2136 error("unknown option `%s'", ctx
->argv
[0]);
2137 usage_with_options(usagestr
, options
);
2143 static int for_each_bisect_ref(struct ref_store
*refs
, each_ref_fn fn
,
2144 void *cb_data
, const char *term
)
2146 struct strbuf bisect_refs
= STRBUF_INIT
;
2148 strbuf_addf(&bisect_refs
, "refs/bisect/%s", term
);
2149 status
= refs_for_each_fullref_in(refs
, bisect_refs
.buf
, fn
, cb_data
, 0);
2150 strbuf_release(&bisect_refs
);
2154 static int for_each_bad_bisect_ref(struct ref_store
*refs
, each_ref_fn fn
, void *cb_data
)
2156 return for_each_bisect_ref(refs
, fn
, cb_data
, term_bad
);
2159 static int for_each_good_bisect_ref(struct ref_store
*refs
, each_ref_fn fn
, void *cb_data
)
2161 return for_each_bisect_ref(refs
, fn
, cb_data
, term_good
);
2164 static int handle_revision_pseudo_opt(const char *submodule
,
2165 struct rev_info
*revs
,
2166 int argc
, const char **argv
, int *flags
)
2168 const char *arg
= argv
[0];
2170 struct ref_store
*refs
;
2175 * We need some something like get_submodule_worktrees()
2176 * before we can go through all worktrees of a submodule,
2177 * .e.g with adding all HEADs from --all, which is not
2178 * supported right now, so stick to single worktree.
2180 if (!revs
->single_worktree
)
2181 die("BUG: --single-worktree cannot be used together with submodule");
2182 refs
= get_submodule_ref_store(submodule
);
2184 refs
= get_main_ref_store();
2189 * Commands like "git shortlog" will not accept the options below
2190 * unless parse_revision_opt queues them (as opposed to erroring
2193 * When implementing your new pseudo-option, remember to
2194 * register it in the list at the top of handle_revision_opt.
2196 if (!strcmp(arg
, "--all")) {
2197 handle_refs(refs
, revs
, *flags
, refs_for_each_ref
);
2198 handle_refs(refs
, revs
, *flags
, refs_head_ref
);
2199 if (!revs
->single_worktree
) {
2200 struct all_refs_cb cb
;
2202 init_all_refs_cb(&cb
, revs
, *flags
);
2203 other_head_refs(handle_one_ref
, &cb
);
2205 clear_ref_exclusion(&revs
->ref_excludes
);
2206 } else if (!strcmp(arg
, "--branches")) {
2207 handle_refs(refs
, revs
, *flags
, refs_for_each_branch_ref
);
2208 clear_ref_exclusion(&revs
->ref_excludes
);
2209 } else if (!strcmp(arg
, "--bisect")) {
2210 read_bisect_terms(&term_bad
, &term_good
);
2211 handle_refs(refs
, revs
, *flags
, for_each_bad_bisect_ref
);
2212 handle_refs(refs
, revs
, *flags
^ (UNINTERESTING
| BOTTOM
),
2213 for_each_good_bisect_ref
);
2215 } else if (!strcmp(arg
, "--tags")) {
2216 handle_refs(refs
, revs
, *flags
, refs_for_each_tag_ref
);
2217 clear_ref_exclusion(&revs
->ref_excludes
);
2218 } else if (!strcmp(arg
, "--remotes")) {
2219 handle_refs(refs
, revs
, *flags
, refs_for_each_remote_ref
);
2220 clear_ref_exclusion(&revs
->ref_excludes
);
2221 } else if ((argcount
= parse_long_opt("glob", argv
, &optarg
))) {
2222 struct all_refs_cb cb
;
2223 init_all_refs_cb(&cb
, revs
, *flags
);
2224 for_each_glob_ref(handle_one_ref
, optarg
, &cb
);
2225 clear_ref_exclusion(&revs
->ref_excludes
);
2227 } else if ((argcount
= parse_long_opt("exclude", argv
, &optarg
))) {
2228 add_ref_exclusion(&revs
->ref_excludes
, optarg
);
2230 } else if (skip_prefix(arg
, "--branches=", &optarg
)) {
2231 struct all_refs_cb cb
;
2232 init_all_refs_cb(&cb
, revs
, *flags
);
2233 for_each_glob_ref_in(handle_one_ref
, optarg
, "refs/heads/", &cb
);
2234 clear_ref_exclusion(&revs
->ref_excludes
);
2235 } else if (skip_prefix(arg
, "--tags=", &optarg
)) {
2236 struct all_refs_cb cb
;
2237 init_all_refs_cb(&cb
, revs
, *flags
);
2238 for_each_glob_ref_in(handle_one_ref
, optarg
, "refs/tags/", &cb
);
2239 clear_ref_exclusion(&revs
->ref_excludes
);
2240 } else if (skip_prefix(arg
, "--remotes=", &optarg
)) {
2241 struct all_refs_cb cb
;
2242 init_all_refs_cb(&cb
, revs
, *flags
);
2243 for_each_glob_ref_in(handle_one_ref
, optarg
, "refs/remotes/", &cb
);
2244 clear_ref_exclusion(&revs
->ref_excludes
);
2245 } else if (!strcmp(arg
, "--reflog")) {
2246 add_reflogs_to_pending(revs
, *flags
);
2247 } else if (!strcmp(arg
, "--indexed-objects")) {
2248 add_index_objects_to_pending(revs
, *flags
);
2249 } else if (!strcmp(arg
, "--not")) {
2250 *flags
^= UNINTERESTING
| BOTTOM
;
2251 } else if (!strcmp(arg
, "--no-walk")) {
2252 revs
->no_walk
= REVISION_WALK_NO_WALK_SORTED
;
2253 } else if (skip_prefix(arg
, "--no-walk=", &optarg
)) {
2255 * Detached form ("--no-walk X" as opposed to "--no-walk=X")
2256 * not allowed, since the argument is optional.
2258 if (!strcmp(optarg
, "sorted"))
2259 revs
->no_walk
= REVISION_WALK_NO_WALK_SORTED
;
2260 else if (!strcmp(optarg
, "unsorted"))
2261 revs
->no_walk
= REVISION_WALK_NO_WALK_UNSORTED
;
2263 return error("invalid argument to --no-walk");
2264 } else if (!strcmp(arg
, "--do-walk")) {
2266 } else if (!strcmp(arg
, "--single-worktree")) {
2267 revs
->single_worktree
= 1;
2275 static void NORETURN
diagnose_missing_default(const char *def
)
2278 const char *refname
;
2280 refname
= resolve_ref_unsafe(def
, 0, NULL
, &flags
);
2281 if (!refname
|| !(flags
& REF_ISSYMREF
) || (flags
& REF_ISBROKEN
))
2282 die(_("your current branch appears to be broken"));
2284 skip_prefix(refname
, "refs/heads/", &refname
);
2285 die(_("your current branch '%s' does not have any commits yet"),
2290 * Parse revision information, filling in the "rev_info" structure,
2291 * and removing the used arguments from the argument list.
2293 * Returns the number of arguments left that weren't recognized
2294 * (which are also moved to the head of the argument list)
2296 int setup_revisions(int argc
, const char **argv
, struct rev_info
*revs
, struct setup_revision_opt
*opt
)
2298 int i
, flags
, left
, seen_dashdash
, read_from_stdin
, got_rev_arg
= 0, revarg_opt
;
2299 struct argv_array prune_data
= ARGV_ARRAY_INIT
;
2300 const char *submodule
= NULL
;
2303 submodule
= opt
->submodule
;
2305 /* First, search for "--" */
2306 if (opt
&& opt
->assume_dashdash
) {
2310 for (i
= 1; i
< argc
; i
++) {
2311 const char *arg
= argv
[i
];
2312 if (strcmp(arg
, "--"))
2317 argv_array_pushv(&prune_data
, argv
+ i
+ 1);
2323 /* Second, deal with arguments and options */
2325 revarg_opt
= opt
? opt
->revarg_opt
: 0;
2327 revarg_opt
|= REVARG_CANNOT_BE_FILENAME
;
2328 read_from_stdin
= 0;
2329 for (left
= i
= 1; i
< argc
; i
++) {
2330 const char *arg
= argv
[i
];
2334 opts
= handle_revision_pseudo_opt(submodule
,
2335 revs
, argc
- i
, argv
+ i
,
2342 if (!strcmp(arg
, "--stdin")) {
2343 if (revs
->disable_stdin
) {
2347 if (read_from_stdin
++)
2348 die("--stdin given twice?");
2349 read_revisions_from_stdin(revs
, &prune_data
);
2353 opts
= handle_revision_opt(revs
, argc
- i
, argv
+ i
, &left
, argv
);
2364 if (handle_revision_arg(arg
, revs
, flags
, revarg_opt
)) {
2366 if (seen_dashdash
|| *arg
== '^')
2367 die("bad revision '%s'", arg
);
2369 /* If we didn't have a "--":
2370 * (1) all filenames must exist;
2371 * (2) all rev-args must not be interpretable
2372 * as a valid filename.
2373 * but the latter we have checked in the main loop.
2375 for (j
= i
; j
< argc
; j
++)
2376 verify_filename(revs
->prefix
, argv
[j
], j
== i
);
2378 argv_array_pushv(&prune_data
, argv
+ i
);
2385 if (prune_data
.argc
) {
2387 * If we need to introduce the magic "a lone ':' means no
2388 * pathspec whatsoever", here is the place to do so.
2390 * if (prune_data.nr == 1 && !strcmp(prune_data[0], ":")) {
2391 * prune_data.nr = 0;
2392 * prune_data.alloc = 0;
2393 * free(prune_data.path);
2394 * prune_data.path = NULL;
2396 * terminate prune_data.alloc with NULL and
2397 * call init_pathspec() to set revs->prune_data here.
2400 parse_pathspec(&revs
->prune_data
, 0, 0,
2401 revs
->prefix
, prune_data
.argv
);
2403 argv_array_clear(&prune_data
);
2405 if (revs
->def
== NULL
)
2406 revs
->def
= opt
? opt
->def
: NULL
;
2407 if (opt
&& opt
->tweak
)
2408 opt
->tweak(revs
, opt
);
2409 if (revs
->show_merge
)
2410 prepare_show_merge(revs
);
2411 if (revs
->def
&& !revs
->pending
.nr
&& !revs
->rev_input_given
&& !got_rev_arg
) {
2412 struct object_id oid
;
2413 struct object
*object
;
2414 struct object_context oc
;
2415 if (get_oid_with_context(revs
->def
, 0, &oid
, &oc
))
2416 diagnose_missing_default(revs
->def
);
2417 object
= get_reference(revs
, revs
->def
, &oid
, 0);
2418 add_pending_object_with_mode(revs
, object
, revs
->def
, oc
.mode
);
2421 /* Did the user ask for any diff output? Run the diff! */
2422 if (revs
->diffopt
.output_format
& ~DIFF_FORMAT_NO_OUTPUT
)
2425 /* Pickaxe, diff-filter and rename following need diffs */
2426 if ((revs
->diffopt
.pickaxe_opts
& DIFF_PICKAXE_KINDS_MASK
) ||
2427 revs
->diffopt
.filter
||
2428 revs
->diffopt
.flags
.follow_renames
)
2431 if (revs
->diffopt
.objfind
)
2432 revs
->simplify_history
= 0;
2434 if (revs
->topo_order
)
2437 if (revs
->prune_data
.nr
) {
2438 copy_pathspec(&revs
->pruning
.pathspec
, &revs
->prune_data
);
2439 /* Can't prune commits with rename following: the paths change.. */
2440 if (!revs
->diffopt
.flags
.follow_renames
)
2442 if (!revs
->full_diff
)
2443 copy_pathspec(&revs
->diffopt
.pathspec
,
2446 if (revs
->combine_merges
)
2447 revs
->ignore_merges
= 0;
2448 revs
->diffopt
.abbrev
= revs
->abbrev
;
2450 if (revs
->line_level_traverse
) {
2452 revs
->topo_order
= 1;
2455 diff_setup_done(&revs
->diffopt
);
2457 grep_commit_pattern_type(GREP_PATTERN_TYPE_UNSPECIFIED
,
2458 &revs
->grep_filter
);
2459 compile_grep_patterns(&revs
->grep_filter
);
2461 if (revs
->reverse
&& revs
->reflog_info
)
2462 die("cannot combine --reverse with --walk-reflogs");
2463 if (revs
->reflog_info
&& revs
->limited
)
2464 die("cannot combine --walk-reflogs with history-limiting options");
2465 if (revs
->rewrite_parents
&& revs
->children
.name
)
2466 die("cannot combine --parents and --children");
2469 * Limitations on the graph functionality
2471 if (revs
->reverse
&& revs
->graph
)
2472 die("cannot combine --reverse with --graph");
2474 if (revs
->reflog_info
&& revs
->graph
)
2475 die("cannot combine --walk-reflogs with --graph");
2476 if (revs
->no_walk
&& revs
->graph
)
2477 die("cannot combine --no-walk with --graph");
2478 if (!revs
->reflog_info
&& revs
->grep_filter
.use_reflog_filter
)
2479 die("cannot use --grep-reflog without --walk-reflogs");
2481 if (revs
->first_parent_only
&& revs
->bisect
)
2482 die(_("--first-parent is incompatible with --bisect"));
2484 if (revs
->expand_tabs_in_log
< 0)
2485 revs
->expand_tabs_in_log
= revs
->expand_tabs_in_log_default
;
2490 static void add_child(struct rev_info
*revs
, struct commit
*parent
, struct commit
*child
)
2492 struct commit_list
*l
= xcalloc(1, sizeof(*l
));
2495 l
->next
= add_decoration(&revs
->children
, &parent
->object
, l
);
2498 static int remove_duplicate_parents(struct rev_info
*revs
, struct commit
*commit
)
2500 struct treesame_state
*ts
= lookup_decoration(&revs
->treesame
, &commit
->object
);
2501 struct commit_list
**pp
, *p
;
2502 int surviving_parents
;
2504 /* Examine existing parents while marking ones we have seen... */
2505 pp
= &commit
->parents
;
2506 surviving_parents
= 0;
2507 while ((p
= *pp
) != NULL
) {
2508 struct commit
*parent
= p
->item
;
2509 if (parent
->object
.flags
& TMP_MARK
) {
2512 compact_treesame(revs
, commit
, surviving_parents
);
2515 parent
->object
.flags
|= TMP_MARK
;
2516 surviving_parents
++;
2519 /* clear the temporary mark */
2520 for (p
= commit
->parents
; p
; p
= p
->next
) {
2521 p
->item
->object
.flags
&= ~TMP_MARK
;
2523 /* no update_treesame() - removing duplicates can't affect TREESAME */
2524 return surviving_parents
;
2527 struct merge_simplify_state
{
2528 struct commit
*simplified
;
2531 static struct merge_simplify_state
*locate_simplify_state(struct rev_info
*revs
, struct commit
*commit
)
2533 struct merge_simplify_state
*st
;
2535 st
= lookup_decoration(&revs
->merge_simplification
, &commit
->object
);
2537 st
= xcalloc(1, sizeof(*st
));
2538 add_decoration(&revs
->merge_simplification
, &commit
->object
, st
);
2543 static int mark_redundant_parents(struct rev_info
*revs
, struct commit
*commit
)
2545 struct commit_list
*h
= reduce_heads(commit
->parents
);
2546 int i
= 0, marked
= 0;
2547 struct commit_list
*po
, *pn
;
2549 /* Want these for sanity-checking only */
2550 int orig_cnt
= commit_list_count(commit
->parents
);
2551 int cnt
= commit_list_count(h
);
2554 * Not ready to remove items yet, just mark them for now, based
2555 * on the output of reduce_heads(). reduce_heads outputs the reduced
2556 * set in its original order, so this isn't too hard.
2558 po
= commit
->parents
;
2561 if (pn
&& po
->item
== pn
->item
) {
2565 po
->item
->object
.flags
|= TMP_MARK
;
2571 if (i
!= cnt
|| cnt
+marked
!= orig_cnt
)
2572 die("mark_redundant_parents %d %d %d %d", orig_cnt
, cnt
, i
, marked
);
2574 free_commit_list(h
);
2579 static int mark_treesame_root_parents(struct rev_info
*revs
, struct commit
*commit
)
2581 struct commit_list
*p
;
2584 for (p
= commit
->parents
; p
; p
= p
->next
) {
2585 struct commit
*parent
= p
->item
;
2586 if (!parent
->parents
&& (parent
->object
.flags
& TREESAME
)) {
2587 parent
->object
.flags
|= TMP_MARK
;
2596 * Awkward naming - this means one parent we are TREESAME to.
2597 * cf mark_treesame_root_parents: root parents that are TREESAME (to an
2598 * empty tree). Better name suggestions?
2600 static int leave_one_treesame_to_parent(struct rev_info
*revs
, struct commit
*commit
)
2602 struct treesame_state
*ts
= lookup_decoration(&revs
->treesame
, &commit
->object
);
2603 struct commit
*unmarked
= NULL
, *marked
= NULL
;
2604 struct commit_list
*p
;
2607 for (p
= commit
->parents
, n
= 0; p
; p
= p
->next
, n
++) {
2608 if (ts
->treesame
[n
]) {
2609 if (p
->item
->object
.flags
& TMP_MARK
) {
2622 * If we are TREESAME to a marked-for-deletion parent, but not to any
2623 * unmarked parents, unmark the first TREESAME parent. This is the
2624 * parent that the default simplify_history==1 scan would have followed,
2625 * and it doesn't make sense to omit that path when asking for a
2626 * simplified full history. Retaining it improves the chances of
2627 * understanding odd missed merges that took an old version of a file.
2631 * I--------*X A modified the file, but mainline merge X used
2632 * \ / "-s ours", so took the version from I. X is
2633 * `-*A--' TREESAME to I and !TREESAME to A.
2635 * Default log from X would produce "I". Without this check,
2636 * --full-history --simplify-merges would produce "I-A-X", showing
2637 * the merge commit X and that it changed A, but not making clear that
2638 * it had just taken the I version. With this check, the topology above
2641 * Note that it is possible that the simplification chooses a different
2642 * TREESAME parent from the default, in which case this test doesn't
2643 * activate, and we _do_ drop the default parent. Example:
2645 * I------X A modified the file, but it was reverted in B,
2646 * \ / meaning mainline merge X is TREESAME to both
2649 * Default log would produce "I" by following the first parent;
2650 * --full-history --simplify-merges will produce "I-A-B". But this is a
2651 * reasonable result - it presents a logical full history leading from
2652 * I to X, and X is not an important merge.
2654 if (!unmarked
&& marked
) {
2655 marked
->object
.flags
&= ~TMP_MARK
;
2662 static int remove_marked_parents(struct rev_info
*revs
, struct commit
*commit
)
2664 struct commit_list
**pp
, *p
;
2665 int nth_parent
, removed
= 0;
2667 pp
= &commit
->parents
;
2669 while ((p
= *pp
) != NULL
) {
2670 struct commit
*parent
= p
->item
;
2671 if (parent
->object
.flags
& TMP_MARK
) {
2672 parent
->object
.flags
&= ~TMP_MARK
;
2676 compact_treesame(revs
, commit
, nth_parent
);
2683 /* Removing parents can only increase TREESAMEness */
2684 if (removed
&& !(commit
->object
.flags
& TREESAME
))
2685 update_treesame(revs
, commit
);
2690 static struct commit_list
**simplify_one(struct rev_info
*revs
, struct commit
*commit
, struct commit_list
**tail
)
2692 struct commit_list
*p
;
2693 struct commit
*parent
;
2694 struct merge_simplify_state
*st
, *pst
;
2697 st
= locate_simplify_state(revs
, commit
);
2700 * Have we handled this one?
2706 * An UNINTERESTING commit simplifies to itself, so does a
2707 * root commit. We do not rewrite parents of such commit
2710 if ((commit
->object
.flags
& UNINTERESTING
) || !commit
->parents
) {
2711 st
->simplified
= commit
;
2716 * Do we know what commit all of our parents that matter
2717 * should be rewritten to? Otherwise we are not ready to
2718 * rewrite this one yet.
2720 for (cnt
= 0, p
= commit
->parents
; p
; p
= p
->next
) {
2721 pst
= locate_simplify_state(revs
, p
->item
);
2722 if (!pst
->simplified
) {
2723 tail
= &commit_list_insert(p
->item
, tail
)->next
;
2726 if (revs
->first_parent_only
)
2730 tail
= &commit_list_insert(commit
, tail
)->next
;
2735 * Rewrite our list of parents. Note that this cannot
2736 * affect our TREESAME flags in any way - a commit is
2737 * always TREESAME to its simplification.
2739 for (p
= commit
->parents
; p
; p
= p
->next
) {
2740 pst
= locate_simplify_state(revs
, p
->item
);
2741 p
->item
= pst
->simplified
;
2742 if (revs
->first_parent_only
)
2746 if (revs
->first_parent_only
)
2749 cnt
= remove_duplicate_parents(revs
, commit
);
2752 * It is possible that we are a merge and one side branch
2753 * does not have any commit that touches the given paths;
2754 * in such a case, the immediate parent from that branch
2755 * will be rewritten to be the merge base.
2757 * o----X X: the commit we are looking at;
2758 * / / o: a commit that touches the paths;
2761 * Further, a merge of an independent branch that doesn't
2762 * touch the path will reduce to a treesame root parent:
2764 * ----o----X X: the commit we are looking at;
2765 * / o: a commit that touches the paths;
2766 * r r: a root commit not touching the paths
2768 * Detect and simplify both cases.
2771 int marked
= mark_redundant_parents(revs
, commit
);
2772 marked
+= mark_treesame_root_parents(revs
, commit
);
2774 marked
-= leave_one_treesame_to_parent(revs
, commit
);
2776 cnt
= remove_marked_parents(revs
, commit
);
2780 * A commit simplifies to itself if it is a root, if it is
2781 * UNINTERESTING, if it touches the given paths, or if it is a
2782 * merge and its parents don't simplify to one relevant commit
2783 * (the first two cases are already handled at the beginning of
2786 * Otherwise, it simplifies to what its sole relevant parent
2790 (commit
->object
.flags
& UNINTERESTING
) ||
2791 !(commit
->object
.flags
& TREESAME
) ||
2792 (parent
= one_relevant_parent(revs
, commit
->parents
)) == NULL
)
2793 st
->simplified
= commit
;
2795 pst
= locate_simplify_state(revs
, parent
);
2796 st
->simplified
= pst
->simplified
;
2801 static void simplify_merges(struct rev_info
*revs
)
2803 struct commit_list
*list
, *next
;
2804 struct commit_list
*yet_to_do
, **tail
;
2805 struct commit
*commit
;
2810 /* feed the list reversed */
2812 for (list
= revs
->commits
; list
; list
= next
) {
2813 commit
= list
->item
;
2816 * Do not free(list) here yet; the original list
2817 * is used later in this function.
2819 commit_list_insert(commit
, &yet_to_do
);
2826 commit
= pop_commit(&list
);
2827 tail
= simplify_one(revs
, commit
, tail
);
2831 /* clean up the result, removing the simplified ones */
2832 list
= revs
->commits
;
2833 revs
->commits
= NULL
;
2834 tail
= &revs
->commits
;
2836 struct merge_simplify_state
*st
;
2838 commit
= pop_commit(&list
);
2839 st
= locate_simplify_state(revs
, commit
);
2840 if (st
->simplified
== commit
)
2841 tail
= &commit_list_insert(commit
, tail
)->next
;
2845 static void set_children(struct rev_info
*revs
)
2847 struct commit_list
*l
;
2848 for (l
= revs
->commits
; l
; l
= l
->next
) {
2849 struct commit
*commit
= l
->item
;
2850 struct commit_list
*p
;
2852 for (p
= commit
->parents
; p
; p
= p
->next
)
2853 add_child(revs
, p
->item
, commit
);
2857 void reset_revision_walk(void)
2859 clear_object_flags(SEEN
| ADDED
| SHOWN
);
2862 static int mark_uninteresting(const struct object_id
*oid
,
2863 struct packed_git
*pack
,
2867 struct object
*o
= parse_object(oid
);
2868 o
->flags
|= UNINTERESTING
| SEEN
;
2872 int prepare_revision_walk(struct rev_info
*revs
)
2875 struct object_array old_pending
;
2876 struct commit_list
**next
= &revs
->commits
;
2878 memcpy(&old_pending
, &revs
->pending
, sizeof(old_pending
));
2879 revs
->pending
.nr
= 0;
2880 revs
->pending
.alloc
= 0;
2881 revs
->pending
.objects
= NULL
;
2882 for (i
= 0; i
< old_pending
.nr
; i
++) {
2883 struct object_array_entry
*e
= old_pending
.objects
+ i
;
2884 struct commit
*commit
= handle_commit(revs
, e
);
2886 if (!(commit
->object
.flags
& SEEN
)) {
2887 commit
->object
.flags
|= SEEN
;
2888 next
= commit_list_append(commit
, next
);
2892 object_array_clear(&old_pending
);
2894 /* Signal whether we need per-parent treesame decoration */
2895 if (revs
->simplify_merges
||
2896 (revs
->limited
&& limiting_can_increase_treesame(revs
)))
2897 revs
->treesame
.name
= "treesame";
2899 if (revs
->exclude_promisor_objects
) {
2900 for_each_packed_object(mark_uninteresting
, NULL
,
2901 FOR_EACH_OBJECT_PROMISOR_ONLY
);
2904 if (revs
->no_walk
!= REVISION_WALK_NO_WALK_UNSORTED
)
2905 commit_list_sort_by_date(&revs
->commits
);
2909 if (limit_list(revs
) < 0)
2911 if (revs
->topo_order
)
2912 sort_in_topological_order(&revs
->commits
, revs
->sort_order
);
2913 if (revs
->line_level_traverse
)
2914 line_log_filter(revs
);
2915 if (revs
->simplify_merges
)
2916 simplify_merges(revs
);
2917 if (revs
->children
.name
)
2922 static enum rewrite_result
rewrite_one(struct rev_info
*revs
, struct commit
**pp
)
2924 struct commit_list
*cache
= NULL
;
2927 struct commit
*p
= *pp
;
2929 if (add_parents_to_list(revs
, p
, &revs
->commits
, &cache
) < 0)
2930 return rewrite_one_error
;
2931 if (p
->object
.flags
& UNINTERESTING
)
2932 return rewrite_one_ok
;
2933 if (!(p
->object
.flags
& TREESAME
))
2934 return rewrite_one_ok
;
2936 return rewrite_one_noparents
;
2937 if ((p
= one_relevant_parent(revs
, p
->parents
)) == NULL
)
2938 return rewrite_one_ok
;
2943 int rewrite_parents(struct rev_info
*revs
, struct commit
*commit
,
2944 rewrite_parent_fn_t rewrite_parent
)
2946 struct commit_list
**pp
= &commit
->parents
;
2948 struct commit_list
*parent
= *pp
;
2949 switch (rewrite_parent(revs
, &parent
->item
)) {
2950 case rewrite_one_ok
:
2952 case rewrite_one_noparents
:
2955 case rewrite_one_error
:
2960 remove_duplicate_parents(revs
, commit
);
2964 static int commit_rewrite_person(struct strbuf
*buf
, const char *what
, struct string_list
*mailmap
)
2966 char *person
, *endp
;
2967 size_t len
, namelen
, maillen
;
2970 struct ident_split ident
;
2972 person
= strstr(buf
->buf
, what
);
2976 person
+= strlen(what
);
2977 endp
= strchr(person
, '\n');
2981 len
= endp
- person
;
2983 if (split_ident_line(&ident
, person
, len
))
2986 mail
= ident
.mail_begin
;
2987 maillen
= ident
.mail_end
- ident
.mail_begin
;
2988 name
= ident
.name_begin
;
2989 namelen
= ident
.name_end
- ident
.name_begin
;
2991 if (map_user(mailmap
, &mail
, &maillen
, &name
, &namelen
)) {
2992 struct strbuf namemail
= STRBUF_INIT
;
2994 strbuf_addf(&namemail
, "%.*s <%.*s>",
2995 (int)namelen
, name
, (int)maillen
, mail
);
2997 strbuf_splice(buf
, ident
.name_begin
- buf
->buf
,
2998 ident
.mail_end
- ident
.name_begin
+ 1,
2999 namemail
.buf
, namemail
.len
);
3001 strbuf_release(&namemail
);
3009 static int commit_match(struct commit
*commit
, struct rev_info
*opt
)
3012 const char *encoding
;
3013 const char *message
;
3014 struct strbuf buf
= STRBUF_INIT
;
3016 if (!opt
->grep_filter
.pattern_list
&& !opt
->grep_filter
.header_list
)
3019 /* Prepend "fake" headers as needed */
3020 if (opt
->grep_filter
.use_reflog_filter
) {
3021 strbuf_addstr(&buf
, "reflog ");
3022 get_reflog_message(&buf
, opt
->reflog_info
);
3023 strbuf_addch(&buf
, '\n');
3027 * We grep in the user's output encoding, under the assumption that it
3028 * is the encoding they are most likely to write their grep pattern
3029 * for. In addition, it means we will match the "notes" encoding below,
3030 * so we will not end up with a buffer that has two different encodings
3033 encoding
= get_log_output_encoding();
3034 message
= logmsg_reencode(commit
, NULL
, encoding
);
3036 /* Copy the commit to temporary if we are using "fake" headers */
3038 strbuf_addstr(&buf
, message
);
3040 if (opt
->grep_filter
.header_list
&& opt
->mailmap
) {
3042 strbuf_addstr(&buf
, message
);
3044 commit_rewrite_person(&buf
, "\nauthor ", opt
->mailmap
);
3045 commit_rewrite_person(&buf
, "\ncommitter ", opt
->mailmap
);
3048 /* Append "fake" message parts as needed */
3049 if (opt
->show_notes
) {
3051 strbuf_addstr(&buf
, message
);
3052 format_display_notes(&commit
->object
.oid
, &buf
, encoding
, 1);
3056 * Find either in the original commit message, or in the temporary.
3057 * Note that we cast away the constness of "message" here. It is
3058 * const because it may come from the cached commit buffer. That's OK,
3059 * because we know that it is modifiable heap memory, and that while
3060 * grep_buffer may modify it for speed, it will restore any
3061 * changes before returning.
3064 retval
= grep_buffer(&opt
->grep_filter
, buf
.buf
, buf
.len
);
3066 retval
= grep_buffer(&opt
->grep_filter
,
3067 (char *)message
, strlen(message
));
3068 strbuf_release(&buf
);
3069 unuse_commit_buffer(commit
, message
);
3070 return opt
->invert_grep
? !retval
: retval
;
3073 static inline int want_ancestry(const struct rev_info
*revs
)
3075 return (revs
->rewrite_parents
|| revs
->children
.name
);
3079 * Return a timestamp to be used for --since/--until comparisons for this
3080 * commit, based on the revision options.
3082 static timestamp_t
comparison_date(const struct rev_info
*revs
,
3083 struct commit
*commit
)
3085 return revs
->reflog_info
?
3086 get_reflog_timestamp(revs
->reflog_info
) :
3090 enum commit_action
get_commit_action(struct rev_info
*revs
, struct commit
*commit
)
3092 if (commit
->object
.flags
& SHOWN
)
3093 return commit_ignore
;
3094 if (revs
->unpacked
&& has_sha1_pack(commit
->object
.oid
.hash
))
3095 return commit_ignore
;
3098 if (commit
->object
.flags
& UNINTERESTING
)
3099 return commit_ignore
;
3100 if (revs
->min_age
!= -1 &&
3101 comparison_date(revs
, commit
) > revs
->min_age
)
3102 return commit_ignore
;
3103 if (revs
->min_parents
|| (revs
->max_parents
>= 0)) {
3104 int n
= commit_list_count(commit
->parents
);
3105 if ((n
< revs
->min_parents
) ||
3106 ((revs
->max_parents
>= 0) && (n
> revs
->max_parents
)))
3107 return commit_ignore
;
3109 if (!commit_match(commit
, revs
))
3110 return commit_ignore
;
3111 if (revs
->prune
&& revs
->dense
) {
3112 /* Commit without changes? */
3113 if (commit
->object
.flags
& TREESAME
) {
3115 struct commit_list
*p
;
3116 /* drop merges unless we want parenthood */
3117 if (!want_ancestry(revs
))
3118 return commit_ignore
;
3120 * If we want ancestry, then need to keep any merges
3121 * between relevant commits to tie together topology.
3122 * For consistency with TREESAME and simplification
3123 * use "relevant" here rather than just INTERESTING,
3124 * to treat bottom commit(s) as part of the topology.
3126 for (n
= 0, p
= commit
->parents
; p
; p
= p
->next
)
3127 if (relevant_commit(p
->item
))
3130 return commit_ignore
;
3136 define_commit_slab(saved_parents
, struct commit_list
*);
3138 #define EMPTY_PARENT_LIST ((struct commit_list *)-1)
3141 * You may only call save_parents() once per commit (this is checked
3142 * for non-root commits).
3144 static void save_parents(struct rev_info
*revs
, struct commit
*commit
)
3146 struct commit_list
**pp
;
3148 if (!revs
->saved_parents_slab
) {
3149 revs
->saved_parents_slab
= xmalloc(sizeof(struct saved_parents
));
3150 init_saved_parents(revs
->saved_parents_slab
);
3153 pp
= saved_parents_at(revs
->saved_parents_slab
, commit
);
3156 * When walking with reflogs, we may visit the same commit
3157 * several times: once for each appearance in the reflog.
3159 * In this case, save_parents() will be called multiple times.
3160 * We want to keep only the first set of parents. We need to
3161 * store a sentinel value for an empty (i.e., NULL) parent
3162 * list to distinguish it from a not-yet-saved list, however.
3166 if (commit
->parents
)
3167 *pp
= copy_commit_list(commit
->parents
);
3169 *pp
= EMPTY_PARENT_LIST
;
3172 static void free_saved_parents(struct rev_info
*revs
)
3174 if (revs
->saved_parents_slab
)
3175 clear_saved_parents(revs
->saved_parents_slab
);
3178 struct commit_list
*get_saved_parents(struct rev_info
*revs
, const struct commit
*commit
)
3180 struct commit_list
*parents
;
3182 if (!revs
->saved_parents_slab
)
3183 return commit
->parents
;
3185 parents
= *saved_parents_at(revs
->saved_parents_slab
, commit
);
3186 if (parents
== EMPTY_PARENT_LIST
)
3191 enum commit_action
simplify_commit(struct rev_info
*revs
, struct commit
*commit
)
3193 enum commit_action action
= get_commit_action(revs
, commit
);
3195 if (action
== commit_show
&&
3197 revs
->prune
&& revs
->dense
&& want_ancestry(revs
)) {
3199 * --full-diff on simplified parents is no good: it
3200 * will show spurious changes from the commits that
3201 * were elided. So we save the parents on the side
3202 * when --full-diff is in effect.
3204 if (revs
->full_diff
)
3205 save_parents(revs
, commit
);
3206 if (rewrite_parents(revs
, commit
, rewrite_one
) < 0)
3207 return commit_error
;
3212 static void track_linear(struct rev_info
*revs
, struct commit
*commit
)
3214 if (revs
->track_first_time
) {
3216 revs
->track_first_time
= 0;
3218 struct commit_list
*p
;
3219 for (p
= revs
->previous_parents
; p
; p
= p
->next
)
3220 if (p
->item
== NULL
|| /* first commit */
3221 !oidcmp(&p
->item
->object
.oid
, &commit
->object
.oid
))
3223 revs
->linear
= p
!= NULL
;
3225 if (revs
->reverse
) {
3227 commit
->object
.flags
|= TRACK_LINEAR
;
3229 free_commit_list(revs
->previous_parents
);
3230 revs
->previous_parents
= copy_commit_list(commit
->parents
);
3233 static struct commit
*get_revision_1(struct rev_info
*revs
)
3236 struct commit
*commit
;
3238 if (revs
->reflog_info
)
3239 commit
= next_reflog_entry(revs
->reflog_info
);
3241 commit
= pop_commit(&revs
->commits
);
3246 if (revs
->reflog_info
)
3247 commit
->object
.flags
&= ~(ADDED
| SEEN
| SHOWN
);
3250 * If we haven't done the list limiting, we need to look at
3251 * the parents here. We also need to do the date-based limiting
3252 * that we'd otherwise have done in limit_list().
3254 if (!revs
->limited
) {
3255 if (revs
->max_age
!= -1 &&
3256 comparison_date(revs
, commit
) < revs
->max_age
)
3259 if (revs
->reflog_info
)
3260 try_to_simplify_commit(revs
, commit
);
3261 else if (add_parents_to_list(revs
, commit
, &revs
->commits
, NULL
) < 0) {
3262 if (!revs
->ignore_missing_links
)
3263 die("Failed to traverse parents of commit %s",
3264 oid_to_hex(&commit
->object
.oid
));
3268 switch (simplify_commit(revs
, commit
)) {
3272 die("Failed to simplify parents of commit %s",
3273 oid_to_hex(&commit
->object
.oid
));
3275 if (revs
->track_linear
)
3276 track_linear(revs
, commit
);
3283 * Return true for entries that have not yet been shown. (This is an
3284 * object_array_each_func_t.)
3286 static int entry_unshown(struct object_array_entry
*entry
, void *cb_data_unused
)
3288 return !(entry
->item
->flags
& SHOWN
);
3292 * If array is on the verge of a realloc, garbage-collect any entries
3293 * that have already been shown to try to free up some space.
3295 static void gc_boundary(struct object_array
*array
)
3297 if (array
->nr
== array
->alloc
)
3298 object_array_filter(array
, entry_unshown
, NULL
);
3301 static void create_boundary_commit_list(struct rev_info
*revs
)
3305 struct object_array
*array
= &revs
->boundary_commits
;
3306 struct object_array_entry
*objects
= array
->objects
;
3309 * If revs->commits is non-NULL at this point, an error occurred in
3310 * get_revision_1(). Ignore the error and continue printing the
3311 * boundary commits anyway. (This is what the code has always
3314 if (revs
->commits
) {
3315 free_commit_list(revs
->commits
);
3316 revs
->commits
= NULL
;
3320 * Put all of the actual boundary commits from revs->boundary_commits
3321 * into revs->commits
3323 for (i
= 0; i
< array
->nr
; i
++) {
3324 c
= (struct commit
*)(objects
[i
].item
);
3327 if (!(c
->object
.flags
& CHILD_SHOWN
))
3329 if (c
->object
.flags
& (SHOWN
| BOUNDARY
))
3331 c
->object
.flags
|= BOUNDARY
;
3332 commit_list_insert(c
, &revs
->commits
);
3336 * If revs->topo_order is set, sort the boundary commits
3337 * in topological order
3339 sort_in_topological_order(&revs
->commits
, revs
->sort_order
);
3342 static struct commit
*get_revision_internal(struct rev_info
*revs
)
3344 struct commit
*c
= NULL
;
3345 struct commit_list
*l
;
3347 if (revs
->boundary
== 2) {
3349 * All of the normal commits have already been returned,
3350 * and we are now returning boundary commits.
3351 * create_boundary_commit_list() has populated
3352 * revs->commits with the remaining commits to return.
3354 c
= pop_commit(&revs
->commits
);
3356 c
->object
.flags
|= SHOWN
;
3361 * If our max_count counter has reached zero, then we are done. We
3362 * don't simply return NULL because we still might need to show
3363 * boundary commits. But we want to avoid calling get_revision_1, which
3364 * might do a considerable amount of work finding the next commit only
3365 * for us to throw it away.
3367 * If it is non-zero, then either we don't have a max_count at all
3368 * (-1), or it is still counting, in which case we decrement.
3370 if (revs
->max_count
) {
3371 c
= get_revision_1(revs
);
3373 while (revs
->skip_count
> 0) {
3375 c
= get_revision_1(revs
);
3381 if (revs
->max_count
> 0)
3386 c
->object
.flags
|= SHOWN
;
3388 if (!revs
->boundary
)
3393 * get_revision_1() runs out the commits, and
3394 * we are done computing the boundaries.
3395 * switch to boundary commits output mode.
3400 * Update revs->commits to contain the list of
3403 create_boundary_commit_list(revs
);
3405 return get_revision_internal(revs
);
3409 * boundary commits are the commits that are parents of the
3410 * ones we got from get_revision_1() but they themselves are
3411 * not returned from get_revision_1(). Before returning
3412 * 'c', we need to mark its parents that they could be boundaries.
3415 for (l
= c
->parents
; l
; l
= l
->next
) {
3417 p
= &(l
->item
->object
);
3418 if (p
->flags
& (CHILD_SHOWN
| SHOWN
))
3420 p
->flags
|= CHILD_SHOWN
;
3421 gc_boundary(&revs
->boundary_commits
);
3422 add_object_array(p
, NULL
, &revs
->boundary_commits
);
3428 struct commit
*get_revision(struct rev_info
*revs
)
3431 struct commit_list
*reversed
;
3433 if (revs
->reverse
) {
3435 while ((c
= get_revision_internal(revs
)))
3436 commit_list_insert(c
, &reversed
);
3437 revs
->commits
= reversed
;
3439 revs
->reverse_output_stage
= 1;
3442 if (revs
->reverse_output_stage
) {
3443 c
= pop_commit(&revs
->commits
);
3444 if (revs
->track_linear
)
3445 revs
->linear
= !!(c
&& c
->object
.flags
& TRACK_LINEAR
);
3449 c
= get_revision_internal(revs
);
3450 if (c
&& revs
->graph
)
3451 graph_update(revs
->graph
, c
);
3453 free_saved_parents(revs
);
3454 if (revs
->previous_parents
) {
3455 free_commit_list(revs
->previous_parents
);
3456 revs
->previous_parents
= NULL
;
3462 char *get_revision_mark(const struct rev_info
*revs
, const struct commit
*commit
)
3464 if (commit
->object
.flags
& BOUNDARY
)
3466 else if (commit
->object
.flags
& UNINTERESTING
)
3468 else if (commit
->object
.flags
& PATCHSAME
)
3470 else if (!revs
|| revs
->left_right
) {
3471 if (commit
->object
.flags
& SYMMETRIC_LEFT
)
3475 } else if (revs
->graph
)
3477 else if (revs
->cherry_mark
)
3482 void put_revision_mark(const struct rev_info
*revs
, const struct commit
*commit
)
3484 char *mark
= get_revision_mark(revs
, commit
);
3487 fputs(mark
, stdout
);