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"
23 volatile show_early_output_fn_t show_early_output
;
25 static const char *term_bad
;
26 static const char *term_good
;
28 char *path_name(struct strbuf
*path
, const char *name
)
30 struct strbuf ret
= STRBUF_INIT
;
32 strbuf_addbuf(&ret
, path
);
33 strbuf_addstr(&ret
, name
);
34 return strbuf_detach(&ret
, NULL
);
37 void show_object_with_name(FILE *out
, struct object
*obj
,
38 struct strbuf
*path
, const char *component
)
40 char *name
= path_name(path
, component
);
43 fprintf(out
, "%s ", oid_to_hex(&obj
->oid
));
44 for (p
= name
; *p
&& *p
!= '\n'; p
++)
51 static void mark_blob_uninteresting(struct blob
*blob
)
55 if (blob
->object
.flags
& UNINTERESTING
)
57 blob
->object
.flags
|= UNINTERESTING
;
60 static void mark_tree_contents_uninteresting(struct tree
*tree
)
62 struct tree_desc desc
;
63 struct name_entry entry
;
64 struct object
*obj
= &tree
->object
;
66 if (!has_object_file(&obj
->oid
))
68 if (parse_tree(tree
) < 0)
69 die("bad tree %s", oid_to_hex(&obj
->oid
));
71 init_tree_desc(&desc
, tree
->buffer
, tree
->size
);
72 while (tree_entry(&desc
, &entry
)) {
73 switch (object_type(entry
.mode
)) {
75 mark_tree_uninteresting(lookup_tree(entry
.sha1
));
78 mark_blob_uninteresting(lookup_blob(entry
.sha1
));
81 /* Subproject commit - not in this repository */
87 * We don't care about the tree any more
88 * after it has been marked uninteresting.
90 free_tree_buffer(tree
);
93 void mark_tree_uninteresting(struct tree
*tree
)
101 if (obj
->flags
& UNINTERESTING
)
103 obj
->flags
|= UNINTERESTING
;
104 mark_tree_contents_uninteresting(tree
);
107 void mark_parents_uninteresting(struct commit
*commit
)
109 struct commit_list
*parents
= NULL
, *l
;
111 for (l
= commit
->parents
; l
; l
= l
->next
)
112 commit_list_insert(l
->item
, &parents
);
115 struct commit
*commit
= pop_commit(&parents
);
119 * A missing commit is ok iff its parent is marked
122 * We just mark such a thing parsed, so that when
123 * it is popped next time around, we won't be trying
124 * to parse it and get an error.
126 if (!has_object_file(&commit
->object
.oid
))
127 commit
->object
.parsed
= 1;
129 if (commit
->object
.flags
& UNINTERESTING
)
132 commit
->object
.flags
|= UNINTERESTING
;
135 * Normally we haven't parsed the parent
136 * yet, so we won't have a parent of a parent
137 * here. However, it may turn out that we've
138 * reached this commit some other way (where it
139 * wasn't uninteresting), in which case we need
140 * to mark its parents recursively too..
142 if (!commit
->parents
)
145 for (l
= commit
->parents
->next
; l
; l
= l
->next
)
146 commit_list_insert(l
->item
, &parents
);
147 commit
= commit
->parents
->item
;
152 static void add_pending_object_with_path(struct rev_info
*revs
,
154 const char *name
, unsigned mode
,
159 if (revs
->no_walk
&& (obj
->flags
& UNINTERESTING
))
161 if (revs
->reflog_info
&& obj
->type
== OBJ_COMMIT
) {
162 struct strbuf buf
= STRBUF_INIT
;
163 int len
= interpret_branch_name(name
, 0, &buf
);
166 if (0 < len
&& name
[len
] && buf
.len
)
167 strbuf_addstr(&buf
, name
+ len
);
168 st
= add_reflog_for_walk(revs
->reflog_info
,
169 (struct commit
*)obj
,
170 buf
.buf
[0] ? buf
.buf
: name
);
171 strbuf_release(&buf
);
175 add_object_array_with_path(obj
, name
, &revs
->pending
, mode
, path
);
178 static void add_pending_object_with_mode(struct rev_info
*revs
,
180 const char *name
, unsigned mode
)
182 add_pending_object_with_path(revs
, obj
, name
, mode
, NULL
);
185 void add_pending_object(struct rev_info
*revs
,
186 struct object
*obj
, const char *name
)
188 add_pending_object_with_mode(revs
, obj
, name
, S_IFINVALID
);
191 void add_head_to_pending(struct rev_info
*revs
)
193 unsigned char sha1
[20];
195 if (get_sha1("HEAD", sha1
))
197 obj
= parse_object(sha1
);
200 add_pending_object(revs
, obj
, "HEAD");
203 static struct object
*get_reference(struct rev_info
*revs
, const char *name
,
204 const unsigned char *sha1
,
207 struct object
*object
;
209 object
= parse_object(sha1
);
211 if (revs
->ignore_missing
)
213 die("bad object %s", name
);
215 object
->flags
|= flags
;
219 void add_pending_sha1(struct rev_info
*revs
, const char *name
,
220 const unsigned char *sha1
, unsigned int flags
)
222 struct object
*object
= get_reference(revs
, name
, sha1
, flags
);
223 add_pending_object(revs
, object
, name
);
226 static struct commit
*handle_commit(struct rev_info
*revs
,
227 struct object_array_entry
*entry
)
229 struct object
*object
= entry
->item
;
230 const char *name
= entry
->name
;
231 const char *path
= entry
->path
;
232 unsigned int mode
= entry
->mode
;
233 unsigned long flags
= object
->flags
;
236 * Tag object? Look what it points to..
238 while (object
->type
== OBJ_TAG
) {
239 struct tag
*tag
= (struct tag
*) object
;
240 if (revs
->tag_objects
&& !(flags
& UNINTERESTING
))
241 add_pending_object(revs
, object
, tag
->tag
);
244 object
= parse_object(tag
->tagged
->oid
.hash
);
246 if (flags
& UNINTERESTING
)
248 die("bad object %s", oid_to_hex(&tag
->tagged
->oid
));
250 object
->flags
|= flags
;
252 * We'll handle the tagged object by looping or dropping
253 * through to the non-tag handlers below. Do not
254 * propagate path data from the tag's pending entry.
261 * Commit object? Just return it, we'll do all the complex
264 if (object
->type
== OBJ_COMMIT
) {
265 struct commit
*commit
= (struct commit
*)object
;
266 if (parse_commit(commit
) < 0)
267 die("unable to parse commit %s", name
);
268 if (flags
& UNINTERESTING
) {
269 mark_parents_uninteresting(commit
);
272 if (revs
->show_source
&& !commit
->util
)
273 commit
->util
= xstrdup(name
);
278 * Tree object? Either mark it uninteresting, or add it
279 * to the list of objects to look at later..
281 if (object
->type
== OBJ_TREE
) {
282 struct tree
*tree
= (struct tree
*)object
;
283 if (!revs
->tree_objects
)
285 if (flags
& UNINTERESTING
) {
286 mark_tree_contents_uninteresting(tree
);
289 add_pending_object_with_path(revs
, object
, name
, mode
, path
);
294 * Blob object? You know the drill by now..
296 if (object
->type
== OBJ_BLOB
) {
297 if (!revs
->blob_objects
)
299 if (flags
& UNINTERESTING
)
301 add_pending_object_with_path(revs
, object
, name
, mode
, path
);
304 die("%s is unknown object", name
);
307 static int everybody_uninteresting(struct commit_list
*orig
,
308 struct commit
**interesting_cache
)
310 struct commit_list
*list
= orig
;
312 if (*interesting_cache
) {
313 struct commit
*commit
= *interesting_cache
;
314 if (!(commit
->object
.flags
& UNINTERESTING
))
319 struct commit
*commit
= list
->item
;
321 if (commit
->object
.flags
& UNINTERESTING
)
324 *interesting_cache
= commit
;
331 * A definition of "relevant" commit that we can use to simplify limited graphs
332 * by eliminating side branches.
334 * A "relevant" commit is one that is !UNINTERESTING (ie we are including it
335 * in our list), or that is a specified BOTTOM commit. Then after computing
336 * a limited list, during processing we can generally ignore boundary merges
337 * coming from outside the graph, (ie from irrelevant parents), and treat
338 * those merges as if they were single-parent. TREESAME is defined to consider
339 * only relevant parents, if any. If we are TREESAME to our on-graph parents,
340 * we don't care if we were !TREESAME to non-graph parents.
342 * Treating bottom commits as relevant ensures that a limited graph's
343 * connection to the actual bottom commit is not viewed as a side branch, but
344 * treated as part of the graph. For example:
346 * ....Z...A---X---o---o---B
350 * When computing "A..B", the A-X connection is at least as important as
351 * Y-X, despite A being flagged UNINTERESTING.
353 * And when computing --ancestry-path "A..B", the A-X connection is more
354 * important than Y-X, despite both A and Y being flagged UNINTERESTING.
356 static inline int relevant_commit(struct commit
*commit
)
358 return (commit
->object
.flags
& (UNINTERESTING
| BOTTOM
)) != UNINTERESTING
;
362 * Return a single relevant commit from a parent list. If we are a TREESAME
363 * commit, and this selects one of our parents, then we can safely simplify to
366 static struct commit
*one_relevant_parent(const struct rev_info
*revs
,
367 struct commit_list
*orig
)
369 struct commit_list
*list
= orig
;
370 struct commit
*relevant
= NULL
;
376 * For 1-parent commits, or if first-parent-only, then return that
377 * first parent (even if not "relevant" by the above definition).
378 * TREESAME will have been set purely on that parent.
380 if (revs
->first_parent_only
|| !orig
->next
)
384 * For multi-parent commits, identify a sole relevant parent, if any.
385 * If we have only one relevant parent, then TREESAME will be set purely
386 * with regard to that parent, and we can simplify accordingly.
388 * If we have more than one relevant parent, or no relevant parents
389 * (and multiple irrelevant ones), then we can't select a parent here
393 struct commit
*commit
= list
->item
;
395 if (relevant_commit(commit
)) {
405 * The goal is to get REV_TREE_NEW as the result only if the
406 * diff consists of all '+' (and no other changes), REV_TREE_OLD
407 * if the whole diff is removal of old data, and otherwise
408 * REV_TREE_DIFFERENT (of course if the trees are the same we
409 * want REV_TREE_SAME).
410 * That means that once we get to REV_TREE_DIFFERENT, we do not
411 * have to look any further.
413 static int tree_difference
= REV_TREE_SAME
;
415 static void file_add_remove(struct diff_options
*options
,
416 int addremove
, unsigned mode
,
417 const unsigned char *sha1
,
419 const char *fullpath
, unsigned dirty_submodule
)
421 int diff
= addremove
== '+' ? REV_TREE_NEW
: REV_TREE_OLD
;
423 tree_difference
|= diff
;
424 if (tree_difference
== REV_TREE_DIFFERENT
)
425 DIFF_OPT_SET(options
, HAS_CHANGES
);
428 static void file_change(struct diff_options
*options
,
429 unsigned old_mode
, unsigned new_mode
,
430 const unsigned char *old_sha1
,
431 const unsigned char *new_sha1
,
432 int old_sha1_valid
, int new_sha1_valid
,
433 const char *fullpath
,
434 unsigned old_dirty_submodule
, unsigned new_dirty_submodule
)
436 tree_difference
= REV_TREE_DIFFERENT
;
437 DIFF_OPT_SET(options
, HAS_CHANGES
);
440 static int rev_compare_tree(struct rev_info
*revs
,
441 struct commit
*parent
, struct commit
*commit
)
443 struct tree
*t1
= parent
->tree
;
444 struct tree
*t2
= commit
->tree
;
451 if (revs
->simplify_by_decoration
) {
453 * If we are simplifying by decoration, then the commit
454 * is worth showing if it has a tag pointing at it.
456 if (get_name_decoration(&commit
->object
))
457 return REV_TREE_DIFFERENT
;
459 * A commit that is not pointed by a tag is uninteresting
460 * if we are not limited by path. This means that you will
461 * see the usual "commits that touch the paths" plus any
462 * tagged commit by specifying both --simplify-by-decoration
465 if (!revs
->prune_data
.nr
)
466 return REV_TREE_SAME
;
469 tree_difference
= REV_TREE_SAME
;
470 DIFF_OPT_CLR(&revs
->pruning
, HAS_CHANGES
);
471 if (diff_tree_sha1(t1
->object
.oid
.hash
, t2
->object
.oid
.hash
, "",
473 return REV_TREE_DIFFERENT
;
474 return tree_difference
;
477 static int rev_same_tree_as_empty(struct rev_info
*revs
, struct commit
*commit
)
480 struct tree
*t1
= commit
->tree
;
485 tree_difference
= REV_TREE_SAME
;
486 DIFF_OPT_CLR(&revs
->pruning
, HAS_CHANGES
);
487 retval
= diff_tree_sha1(NULL
, t1
->object
.oid
.hash
, "", &revs
->pruning
);
489 return retval
>= 0 && (tree_difference
== REV_TREE_SAME
);
492 struct treesame_state
{
493 unsigned int nparents
;
494 unsigned char treesame
[FLEX_ARRAY
];
497 static struct treesame_state
*initialise_treesame(struct rev_info
*revs
, struct commit
*commit
)
499 unsigned n
= commit_list_count(commit
->parents
);
500 struct treesame_state
*st
= xcalloc(1, sizeof(*st
) + n
);
502 add_decoration(&revs
->treesame
, &commit
->object
, st
);
507 * Must be called immediately after removing the nth_parent from a commit's
508 * parent list, if we are maintaining the per-parent treesame[] decoration.
509 * This does not recalculate the master TREESAME flag - update_treesame()
510 * should be called to update it after a sequence of treesame[] modifications
511 * that may have affected it.
513 static int compact_treesame(struct rev_info
*revs
, struct commit
*commit
, unsigned nth_parent
)
515 struct treesame_state
*st
;
518 if (!commit
->parents
) {
520 * Have just removed the only parent from a non-merge.
521 * Different handling, as we lack decoration.
524 die("compact_treesame %u", nth_parent
);
525 old_same
= !!(commit
->object
.flags
& TREESAME
);
526 if (rev_same_tree_as_empty(revs
, commit
))
527 commit
->object
.flags
|= TREESAME
;
529 commit
->object
.flags
&= ~TREESAME
;
533 st
= lookup_decoration(&revs
->treesame
, &commit
->object
);
534 if (!st
|| nth_parent
>= st
->nparents
)
535 die("compact_treesame %u", nth_parent
);
537 old_same
= st
->treesame
[nth_parent
];
538 memmove(st
->treesame
+ nth_parent
,
539 st
->treesame
+ nth_parent
+ 1,
540 st
->nparents
- nth_parent
- 1);
543 * If we've just become a non-merge commit, update TREESAME
544 * immediately, and remove the no-longer-needed decoration.
545 * If still a merge, defer update until update_treesame().
547 if (--st
->nparents
== 1) {
548 if (commit
->parents
->next
)
549 die("compact_treesame parents mismatch");
550 if (st
->treesame
[0] && revs
->dense
)
551 commit
->object
.flags
|= TREESAME
;
553 commit
->object
.flags
&= ~TREESAME
;
554 free(add_decoration(&revs
->treesame
, &commit
->object
, NULL
));
560 static unsigned update_treesame(struct rev_info
*revs
, struct commit
*commit
)
562 if (commit
->parents
&& commit
->parents
->next
) {
564 struct treesame_state
*st
;
565 struct commit_list
*p
;
566 unsigned relevant_parents
;
567 unsigned relevant_change
, irrelevant_change
;
569 st
= lookup_decoration(&revs
->treesame
, &commit
->object
);
571 die("update_treesame %s", oid_to_hex(&commit
->object
.oid
));
572 relevant_parents
= 0;
573 relevant_change
= irrelevant_change
= 0;
574 for (p
= commit
->parents
, n
= 0; p
; n
++, p
= p
->next
) {
575 if (relevant_commit(p
->item
)) {
576 relevant_change
|= !st
->treesame
[n
];
579 irrelevant_change
|= !st
->treesame
[n
];
581 if (relevant_parents
? relevant_change
: irrelevant_change
)
582 commit
->object
.flags
&= ~TREESAME
;
584 commit
->object
.flags
|= TREESAME
;
587 return commit
->object
.flags
& TREESAME
;
590 static inline int limiting_can_increase_treesame(const struct rev_info
*revs
)
593 * TREESAME is irrelevant unless prune && dense;
594 * if simplify_history is set, we can't have a mixture of TREESAME and
595 * !TREESAME INTERESTING parents (and we don't have treesame[]
596 * decoration anyway);
597 * if first_parent_only is set, then the TREESAME flag is locked
598 * against the first parent (and again we lack treesame[] decoration).
600 return revs
->prune
&& revs
->dense
&&
601 !revs
->simplify_history
&&
602 !revs
->first_parent_only
;
605 static void try_to_simplify_commit(struct rev_info
*revs
, struct commit
*commit
)
607 struct commit_list
**pp
, *parent
;
608 struct treesame_state
*ts
= NULL
;
609 int relevant_change
= 0, irrelevant_change
= 0;
610 int relevant_parents
, nth_parent
;
613 * If we don't do pruning, everything is interesting
621 if (!commit
->parents
) {
622 if (rev_same_tree_as_empty(revs
, commit
))
623 commit
->object
.flags
|= TREESAME
;
628 * Normal non-merge commit? If we don't want to make the
629 * history dense, we consider it always to be a change..
631 if (!revs
->dense
&& !commit
->parents
->next
)
634 for (pp
= &commit
->parents
, nth_parent
= 0, relevant_parents
= 0;
635 (parent
= *pp
) != NULL
;
636 pp
= &parent
->next
, nth_parent
++) {
637 struct commit
*p
= parent
->item
;
638 if (relevant_commit(p
))
641 if (nth_parent
== 1) {
643 * This our second loop iteration - so we now know
644 * we're dealing with a merge.
646 * Do not compare with later parents when we care only about
647 * the first parent chain, in order to avoid derailing the
648 * traversal to follow a side branch that brought everything
649 * in the path we are limited to by the pathspec.
651 if (revs
->first_parent_only
)
654 * If this will remain a potentially-simplifiable
655 * merge, remember per-parent treesame if needed.
656 * Initialise the array with the comparison from our
659 if (revs
->treesame
.name
&&
660 !revs
->simplify_history
&&
661 !(commit
->object
.flags
& UNINTERESTING
)) {
662 ts
= initialise_treesame(revs
, commit
);
663 if (!(irrelevant_change
|| relevant_change
))
667 if (parse_commit(p
) < 0)
668 die("cannot simplify commit %s (because of %s)",
669 oid_to_hex(&commit
->object
.oid
),
670 oid_to_hex(&p
->object
.oid
));
671 switch (rev_compare_tree(revs
, p
, commit
)) {
673 if (!revs
->simplify_history
|| !relevant_commit(p
)) {
674 /* Even if a merge with an uninteresting
675 * side branch brought the entire change
676 * we are interested in, we do not want
677 * to lose the other branches of this
678 * merge, so we just keep going.
681 ts
->treesame
[nth_parent
] = 1;
685 commit
->parents
= parent
;
686 commit
->object
.flags
|= TREESAME
;
690 if (revs
->remove_empty_trees
&&
691 rev_same_tree_as_empty(revs
, p
)) {
692 /* We are adding all the specified
693 * paths from this parent, so the
694 * history beyond this parent is not
695 * interesting. Remove its parents
696 * (they are grandparents for us).
697 * IOW, we pretend this parent is a
700 if (parse_commit(p
) < 0)
701 die("cannot simplify commit %s (invalid %s)",
702 oid_to_hex(&commit
->object
.oid
),
703 oid_to_hex(&p
->object
.oid
));
708 case REV_TREE_DIFFERENT
:
709 if (relevant_commit(p
))
712 irrelevant_change
= 1;
715 die("bad tree compare for commit %s", oid_to_hex(&commit
->object
.oid
));
719 * TREESAME is straightforward for single-parent commits. For merge
720 * commits, it is most useful to define it so that "irrelevant"
721 * parents cannot make us !TREESAME - if we have any relevant
722 * parents, then we only consider TREESAMEness with respect to them,
723 * allowing irrelevant merges from uninteresting branches to be
724 * simplified away. Only if we have only irrelevant parents do we
725 * base TREESAME on them. Note that this logic is replicated in
726 * update_treesame, which should be kept in sync.
728 if (relevant_parents
? !relevant_change
: !irrelevant_change
)
729 commit
->object
.flags
|= TREESAME
;
732 static void commit_list_insert_by_date_cached(struct commit
*p
, struct commit_list
**head
,
733 struct commit_list
*cached_base
, struct commit_list
**cache
)
735 struct commit_list
*new_entry
;
737 if (cached_base
&& p
->date
< cached_base
->item
->date
)
738 new_entry
= commit_list_insert_by_date(p
, &cached_base
->next
);
740 new_entry
= commit_list_insert_by_date(p
, head
);
742 if (cache
&& (!*cache
|| p
->date
< (*cache
)->item
->date
))
746 static int add_parents_to_list(struct rev_info
*revs
, struct commit
*commit
,
747 struct commit_list
**list
, struct commit_list
**cache_ptr
)
749 struct commit_list
*parent
= commit
->parents
;
751 struct commit_list
*cached_base
= cache_ptr
? *cache_ptr
: NULL
;
753 if (commit
->object
.flags
& ADDED
)
755 commit
->object
.flags
|= ADDED
;
757 if (revs
->include_check
&&
758 !revs
->include_check(commit
, revs
->include_check_data
))
762 * If the commit is uninteresting, don't try to
763 * prune parents - we want the maximal uninteresting
766 * Normally we haven't parsed the parent
767 * yet, so we won't have a parent of a parent
768 * here. However, it may turn out that we've
769 * reached this commit some other way (where it
770 * wasn't uninteresting), in which case we need
771 * to mark its parents recursively too..
773 if (commit
->object
.flags
& UNINTERESTING
) {
775 struct commit
*p
= parent
->item
;
776 parent
= parent
->next
;
778 p
->object
.flags
|= UNINTERESTING
;
779 if (parse_commit_gently(p
, 1) < 0)
782 mark_parents_uninteresting(p
);
783 if (p
->object
.flags
& SEEN
)
785 p
->object
.flags
|= SEEN
;
786 commit_list_insert_by_date_cached(p
, list
, cached_base
, cache_ptr
);
792 * Ok, the commit wasn't uninteresting. Try to
793 * simplify the commit history and find the parent
794 * that has no differences in the path set if one exists.
796 try_to_simplify_commit(revs
, commit
);
801 left_flag
= (commit
->object
.flags
& SYMMETRIC_LEFT
);
803 for (parent
= commit
->parents
; parent
; parent
= parent
->next
) {
804 struct commit
*p
= parent
->item
;
806 if (parse_commit_gently(p
, revs
->ignore_missing_links
) < 0)
808 if (revs
->show_source
&& !p
->util
)
809 p
->util
= commit
->util
;
810 p
->object
.flags
|= left_flag
;
811 if (!(p
->object
.flags
& SEEN
)) {
812 p
->object
.flags
|= SEEN
;
813 commit_list_insert_by_date_cached(p
, list
, cached_base
, cache_ptr
);
815 if (revs
->first_parent_only
)
821 static void cherry_pick_list(struct commit_list
*list
, struct rev_info
*revs
)
823 struct commit_list
*p
;
824 int left_count
= 0, right_count
= 0;
826 struct patch_ids ids
;
827 unsigned cherry_flag
;
829 /* First count the commits on the left and on the right */
830 for (p
= list
; p
; p
= p
->next
) {
831 struct commit
*commit
= p
->item
;
832 unsigned flags
= commit
->object
.flags
;
833 if (flags
& BOUNDARY
)
835 else if (flags
& SYMMETRIC_LEFT
)
841 if (!left_count
|| !right_count
)
844 left_first
= left_count
< right_count
;
845 init_patch_ids(&ids
);
846 ids
.diffopts
.pathspec
= revs
->diffopt
.pathspec
;
848 /* Compute patch-ids for one side */
849 for (p
= list
; p
; p
= p
->next
) {
850 struct commit
*commit
= p
->item
;
851 unsigned flags
= commit
->object
.flags
;
853 if (flags
& BOUNDARY
)
856 * If we have fewer left, left_first is set and we omit
857 * commits on the right branch in this loop. If we have
858 * fewer right, we skip the left ones.
860 if (left_first
!= !!(flags
& SYMMETRIC_LEFT
))
862 commit
->util
= add_commit_patch_id(commit
, &ids
);
865 /* either cherry_mark or cherry_pick are true */
866 cherry_flag
= revs
->cherry_mark
? PATCHSAME
: SHOWN
;
868 /* Check the other side */
869 for (p
= list
; p
; p
= p
->next
) {
870 struct commit
*commit
= p
->item
;
872 unsigned flags
= commit
->object
.flags
;
874 if (flags
& BOUNDARY
)
877 * If we have fewer left, left_first is set and we omit
878 * commits on the left branch in this loop.
880 if (left_first
== !!(flags
& SYMMETRIC_LEFT
))
884 * Have we seen the same patch id?
886 id
= has_commit_patch_id(commit
, &ids
);
890 commit
->object
.flags
|= cherry_flag
;
893 /* Now check the original side for seen ones */
894 for (p
= list
; p
; p
= p
->next
) {
895 struct commit
*commit
= p
->item
;
896 struct patch_id
*ent
;
902 commit
->object
.flags
|= cherry_flag
;
906 free_patch_ids(&ids
);
909 /* How many extra uninteresting commits we want to see.. */
912 static int still_interesting(struct commit_list
*src
, unsigned long date
, int slop
,
913 struct commit
**interesting_cache
)
916 * No source list at all? We're definitely done..
922 * Does the destination list contain entries with a date
923 * before the source list? Definitely _not_ done.
925 if (date
<= src
->item
->date
)
929 * Does the source list still have interesting commits in
930 * it? Definitely not done..
932 if (!everybody_uninteresting(src
, interesting_cache
))
935 /* Ok, we're closing in.. */
940 * "rev-list --ancestry-path A..B" computes commits that are ancestors
941 * of B but not ancestors of A but further limits the result to those
942 * that are descendants of A. This takes the list of bottom commits and
943 * the result of "A..B" without --ancestry-path, and limits the latter
944 * further to the ones that can reach one of the commits in "bottom".
946 static void limit_to_ancestry(struct commit_list
*bottom
, struct commit_list
*list
)
948 struct commit_list
*p
;
949 struct commit_list
*rlist
= NULL
;
953 * Reverse the list so that it will be likely that we would
954 * process parents before children.
956 for (p
= list
; p
; p
= p
->next
)
957 commit_list_insert(p
->item
, &rlist
);
959 for (p
= bottom
; p
; p
= p
->next
)
960 p
->item
->object
.flags
|= TMP_MARK
;
963 * Mark the ones that can reach bottom commits in "list",
964 * in a bottom-up fashion.
968 for (p
= rlist
; p
; p
= p
->next
) {
969 struct commit
*c
= p
->item
;
970 struct commit_list
*parents
;
971 if (c
->object
.flags
& (TMP_MARK
| UNINTERESTING
))
973 for (parents
= c
->parents
;
975 parents
= parents
->next
) {
976 if (!(parents
->item
->object
.flags
& TMP_MARK
))
978 c
->object
.flags
|= TMP_MARK
;
983 } while (made_progress
);
986 * NEEDSWORK: decide if we want to remove parents that are
987 * not marked with TMP_MARK from commit->parents for commits
988 * in the resulting list. We may not want to do that, though.
992 * The ones that are not marked with TMP_MARK are uninteresting
994 for (p
= list
; p
; p
= p
->next
) {
995 struct commit
*c
= p
->item
;
996 if (c
->object
.flags
& TMP_MARK
)
998 c
->object
.flags
|= UNINTERESTING
;
1001 /* We are done with the TMP_MARK */
1002 for (p
= list
; p
; p
= p
->next
)
1003 p
->item
->object
.flags
&= ~TMP_MARK
;
1004 for (p
= bottom
; p
; p
= p
->next
)
1005 p
->item
->object
.flags
&= ~TMP_MARK
;
1006 free_commit_list(rlist
);
1010 * Before walking the history, keep the set of "negative" refs the
1011 * caller has asked to exclude.
1013 * This is used to compute "rev-list --ancestry-path A..B", as we need
1014 * to filter the result of "A..B" further to the ones that can actually
1017 static struct commit_list
*collect_bottom_commits(struct commit_list
*list
)
1019 struct commit_list
*elem
, *bottom
= NULL
;
1020 for (elem
= list
; elem
; elem
= elem
->next
)
1021 if (elem
->item
->object
.flags
& BOTTOM
)
1022 commit_list_insert(elem
->item
, &bottom
);
1026 /* Assumes either left_only or right_only is set */
1027 static void limit_left_right(struct commit_list
*list
, struct rev_info
*revs
)
1029 struct commit_list
*p
;
1031 for (p
= list
; p
; p
= p
->next
) {
1032 struct commit
*commit
= p
->item
;
1034 if (revs
->right_only
) {
1035 if (commit
->object
.flags
& SYMMETRIC_LEFT
)
1036 commit
->object
.flags
|= SHOWN
;
1037 } else /* revs->left_only is set */
1038 if (!(commit
->object
.flags
& SYMMETRIC_LEFT
))
1039 commit
->object
.flags
|= SHOWN
;
1043 static int limit_list(struct rev_info
*revs
)
1046 unsigned long date
= ~0ul;
1047 struct commit_list
*list
= revs
->commits
;
1048 struct commit_list
*newlist
= NULL
;
1049 struct commit_list
**p
= &newlist
;
1050 struct commit_list
*bottom
= NULL
;
1051 struct commit
*interesting_cache
= NULL
;
1053 if (revs
->ancestry_path
) {
1054 bottom
= collect_bottom_commits(list
);
1056 die("--ancestry-path given but there are no bottom commits");
1060 struct commit
*commit
= pop_commit(&list
);
1061 struct object
*obj
= &commit
->object
;
1062 show_early_output_fn_t show
;
1064 if (commit
== interesting_cache
)
1065 interesting_cache
= NULL
;
1067 if (revs
->max_age
!= -1 && (commit
->date
< revs
->max_age
))
1068 obj
->flags
|= UNINTERESTING
;
1069 if (add_parents_to_list(revs
, commit
, &list
, NULL
) < 0)
1071 if (obj
->flags
& UNINTERESTING
) {
1072 mark_parents_uninteresting(commit
);
1074 p
= &commit_list_insert(commit
, p
)->next
;
1075 slop
= still_interesting(list
, date
, slop
, &interesting_cache
);
1078 /* If showing all, add the whole pending list to the end */
1083 if (revs
->min_age
!= -1 && (commit
->date
> revs
->min_age
))
1085 date
= commit
->date
;
1086 p
= &commit_list_insert(commit
, p
)->next
;
1088 show
= show_early_output
;
1092 show(revs
, newlist
);
1093 show_early_output
= NULL
;
1095 if (revs
->cherry_pick
|| revs
->cherry_mark
)
1096 cherry_pick_list(newlist
, revs
);
1098 if (revs
->left_only
|| revs
->right_only
)
1099 limit_left_right(newlist
, revs
);
1102 limit_to_ancestry(bottom
, newlist
);
1103 free_commit_list(bottom
);
1107 * Check if any commits have become TREESAME by some of their parents
1108 * becoming UNINTERESTING.
1110 if (limiting_can_increase_treesame(revs
))
1111 for (list
= newlist
; list
; list
= list
->next
) {
1112 struct commit
*c
= list
->item
;
1113 if (c
->object
.flags
& (UNINTERESTING
| TREESAME
))
1115 update_treesame(revs
, c
);
1118 revs
->commits
= newlist
;
1123 * Add an entry to refs->cmdline with the specified information.
1126 static void add_rev_cmdline(struct rev_info
*revs
,
1127 struct object
*item
,
1132 struct rev_cmdline_info
*info
= &revs
->cmdline
;
1135 ALLOC_GROW(info
->rev
, nr
+ 1, info
->alloc
);
1136 info
->rev
[nr
].item
= item
;
1137 info
->rev
[nr
].name
= xstrdup(name
);
1138 info
->rev
[nr
].whence
= whence
;
1139 info
->rev
[nr
].flags
= flags
;
1143 static void add_rev_cmdline_list(struct rev_info
*revs
,
1144 struct commit_list
*commit_list
,
1148 while (commit_list
) {
1149 struct object
*object
= &commit_list
->item
->object
;
1150 add_rev_cmdline(revs
, object
, oid_to_hex(&object
->oid
),
1152 commit_list
= commit_list
->next
;
1156 struct all_refs_cb
{
1158 int warned_bad_reflog
;
1159 struct rev_info
*all_revs
;
1160 const char *name_for_errormsg
;
1163 int ref_excluded(struct string_list
*ref_excludes
, const char *path
)
1165 struct string_list_item
*item
;
1169 for_each_string_list_item(item
, ref_excludes
) {
1170 if (!wildmatch(item
->string
, path
, 0, NULL
))
1176 static int handle_one_ref(const char *path
, const struct object_id
*oid
,
1177 int flag
, void *cb_data
)
1179 struct all_refs_cb
*cb
= cb_data
;
1180 struct object
*object
;
1182 if (ref_excluded(cb
->all_revs
->ref_excludes
, path
))
1185 object
= get_reference(cb
->all_revs
, path
, oid
->hash
, cb
->all_flags
);
1186 add_rev_cmdline(cb
->all_revs
, object
, path
, REV_CMD_REF
, cb
->all_flags
);
1187 add_pending_sha1(cb
->all_revs
, path
, oid
->hash
, cb
->all_flags
);
1191 static void init_all_refs_cb(struct all_refs_cb
*cb
, struct rev_info
*revs
,
1194 cb
->all_revs
= revs
;
1195 cb
->all_flags
= flags
;
1198 void clear_ref_exclusion(struct string_list
**ref_excludes_p
)
1200 if (*ref_excludes_p
) {
1201 string_list_clear(*ref_excludes_p
, 0);
1202 free(*ref_excludes_p
);
1204 *ref_excludes_p
= NULL
;
1207 void add_ref_exclusion(struct string_list
**ref_excludes_p
, const char *exclude
)
1209 if (!*ref_excludes_p
) {
1210 *ref_excludes_p
= xcalloc(1, sizeof(**ref_excludes_p
));
1211 (*ref_excludes_p
)->strdup_strings
= 1;
1213 string_list_append(*ref_excludes_p
, exclude
);
1216 static void handle_refs(const char *submodule
, struct rev_info
*revs
, unsigned flags
,
1217 int (*for_each
)(const char *, each_ref_fn
, void *))
1219 struct all_refs_cb cb
;
1220 init_all_refs_cb(&cb
, revs
, flags
);
1221 for_each(submodule
, handle_one_ref
, &cb
);
1224 static void handle_one_reflog_commit(unsigned char *sha1
, void *cb_data
)
1226 struct all_refs_cb
*cb
= cb_data
;
1227 if (!is_null_sha1(sha1
)) {
1228 struct object
*o
= parse_object(sha1
);
1230 o
->flags
|= cb
->all_flags
;
1231 /* ??? CMDLINEFLAGS ??? */
1232 add_pending_object(cb
->all_revs
, o
, "");
1234 else if (!cb
->warned_bad_reflog
) {
1235 warning("reflog of '%s' references pruned commits",
1236 cb
->name_for_errormsg
);
1237 cb
->warned_bad_reflog
= 1;
1242 static int handle_one_reflog_ent(unsigned char *osha1
, unsigned char *nsha1
,
1243 const char *email
, unsigned long timestamp
, int tz
,
1244 const char *message
, void *cb_data
)
1246 handle_one_reflog_commit(osha1
, cb_data
);
1247 handle_one_reflog_commit(nsha1
, cb_data
);
1251 static int handle_one_reflog(const char *path
, const struct object_id
*oid
,
1252 int flag
, void *cb_data
)
1254 struct all_refs_cb
*cb
= cb_data
;
1255 cb
->warned_bad_reflog
= 0;
1256 cb
->name_for_errormsg
= path
;
1257 for_each_reflog_ent(path
, handle_one_reflog_ent
, cb_data
);
1261 void add_reflogs_to_pending(struct rev_info
*revs
, unsigned flags
)
1263 struct all_refs_cb cb
;
1266 cb
.all_flags
= flags
;
1267 for_each_reflog(handle_one_reflog
, &cb
);
1270 static void add_cache_tree(struct cache_tree
*it
, struct rev_info
*revs
,
1271 struct strbuf
*path
)
1273 size_t baselen
= path
->len
;
1276 if (it
->entry_count
>= 0) {
1277 struct tree
*tree
= lookup_tree(it
->sha1
);
1278 add_pending_object_with_path(revs
, &tree
->object
, "",
1282 for (i
= 0; i
< it
->subtree_nr
; i
++) {
1283 struct cache_tree_sub
*sub
= it
->down
[i
];
1284 strbuf_addf(path
, "%s%s", baselen
? "/" : "", sub
->name
);
1285 add_cache_tree(sub
->cache_tree
, revs
, path
);
1286 strbuf_setlen(path
, baselen
);
1291 void add_index_objects_to_pending(struct rev_info
*revs
, unsigned flags
)
1296 for (i
= 0; i
< active_nr
; i
++) {
1297 struct cache_entry
*ce
= active_cache
[i
];
1300 if (S_ISGITLINK(ce
->ce_mode
))
1303 blob
= lookup_blob(ce
->sha1
);
1305 die("unable to add index blob to traversal");
1306 add_pending_object_with_path(revs
, &blob
->object
, "",
1307 ce
->ce_mode
, ce
->name
);
1310 if (active_cache_tree
) {
1311 struct strbuf path
= STRBUF_INIT
;
1312 add_cache_tree(active_cache_tree
, revs
, &path
);
1313 strbuf_release(&path
);
1317 static int add_parents_only(struct rev_info
*revs
, const char *arg_
, int flags
)
1319 unsigned char sha1
[20];
1321 struct commit
*commit
;
1322 struct commit_list
*parents
;
1323 const char *arg
= arg_
;
1326 flags
^= UNINTERESTING
| BOTTOM
;
1329 if (get_sha1_committish(arg
, sha1
))
1332 it
= get_reference(revs
, arg
, sha1
, 0);
1333 if (!it
&& revs
->ignore_missing
)
1335 if (it
->type
!= OBJ_TAG
)
1337 if (!((struct tag
*)it
)->tagged
)
1339 hashcpy(sha1
, ((struct tag
*)it
)->tagged
->oid
.hash
);
1341 if (it
->type
!= OBJ_COMMIT
)
1343 commit
= (struct commit
*)it
;
1344 for (parents
= commit
->parents
; parents
; parents
= parents
->next
) {
1345 it
= &parents
->item
->object
;
1347 add_rev_cmdline(revs
, it
, arg_
, REV_CMD_PARENTS_ONLY
, flags
);
1348 add_pending_object(revs
, it
, arg
);
1353 void init_revisions(struct rev_info
*revs
, const char *prefix
)
1355 memset(revs
, 0, sizeof(*revs
));
1357 revs
->abbrev
= DEFAULT_ABBREV
;
1358 revs
->ignore_merges
= 1;
1359 revs
->simplify_history
= 1;
1360 DIFF_OPT_SET(&revs
->pruning
, RECURSIVE
);
1361 DIFF_OPT_SET(&revs
->pruning
, QUICK
);
1362 revs
->pruning
.add_remove
= file_add_remove
;
1363 revs
->pruning
.change
= file_change
;
1364 revs
->sort_order
= REV_SORT_IN_GRAPH_ORDER
;
1366 revs
->prefix
= prefix
;
1369 revs
->skip_count
= -1;
1370 revs
->max_count
= -1;
1371 revs
->max_parents
= -1;
1373 revs
->commit_format
= CMIT_FMT_DEFAULT
;
1375 init_grep_defaults();
1376 grep_init(&revs
->grep_filter
, prefix
);
1377 revs
->grep_filter
.status_only
= 1;
1378 revs
->grep_filter
.regflags
= REG_NEWLINE
;
1380 diff_setup(&revs
->diffopt
);
1381 if (prefix
&& !revs
->diffopt
.prefix
) {
1382 revs
->diffopt
.prefix
= prefix
;
1383 revs
->diffopt
.prefix_length
= strlen(prefix
);
1386 revs
->notes_opt
.use_default_notes
= -1;
1389 static void add_pending_commit_list(struct rev_info
*revs
,
1390 struct commit_list
*commit_list
,
1393 while (commit_list
) {
1394 struct object
*object
= &commit_list
->item
->object
;
1395 object
->flags
|= flags
;
1396 add_pending_object(revs
, object
, oid_to_hex(&object
->oid
));
1397 commit_list
= commit_list
->next
;
1401 static void prepare_show_merge(struct rev_info
*revs
)
1403 struct commit_list
*bases
;
1404 struct commit
*head
, *other
;
1405 unsigned char sha1
[20];
1406 const char **prune
= NULL
;
1407 int i
, prune_num
= 1; /* counting terminating NULL */
1409 if (get_sha1("HEAD", sha1
))
1410 die("--merge without HEAD?");
1411 head
= lookup_commit_or_die(sha1
, "HEAD");
1412 if (get_sha1("MERGE_HEAD", sha1
))
1413 die("--merge without MERGE_HEAD?");
1414 other
= lookup_commit_or_die(sha1
, "MERGE_HEAD");
1415 add_pending_object(revs
, &head
->object
, "HEAD");
1416 add_pending_object(revs
, &other
->object
, "MERGE_HEAD");
1417 bases
= get_merge_bases(head
, other
);
1418 add_rev_cmdline_list(revs
, bases
, REV_CMD_MERGE_BASE
, UNINTERESTING
| BOTTOM
);
1419 add_pending_commit_list(revs
, bases
, UNINTERESTING
| BOTTOM
);
1420 free_commit_list(bases
);
1421 head
->object
.flags
|= SYMMETRIC_LEFT
;
1425 for (i
= 0; i
< active_nr
; i
++) {
1426 const struct cache_entry
*ce
= active_cache
[i
];
1429 if (ce_path_match(ce
, &revs
->prune_data
, NULL
)) {
1431 REALLOC_ARRAY(prune
, prune_num
);
1432 prune
[prune_num
-2] = ce
->name
;
1433 prune
[prune_num
-1] = NULL
;
1435 while ((i
+1 < active_nr
) &&
1436 ce_same_name(ce
, active_cache
[i
+1]))
1439 free_pathspec(&revs
->prune_data
);
1440 parse_pathspec(&revs
->prune_data
, PATHSPEC_ALL_MAGIC
& ~PATHSPEC_LITERAL
,
1441 PATHSPEC_PREFER_FULL
| PATHSPEC_LITERAL_PATH
, "", prune
);
1445 int handle_revision_arg(const char *arg_
, struct rev_info
*revs
, int flags
, unsigned revarg_opt
)
1447 struct object_context oc
;
1449 struct object
*object
;
1450 unsigned char sha1
[20];
1452 const char *arg
= arg_
;
1453 int cant_be_filename
= revarg_opt
& REVARG_CANNOT_BE_FILENAME
;
1454 unsigned get_sha1_flags
= 0;
1456 flags
= flags
& UNINTERESTING
? flags
| BOTTOM
: flags
& ~BOTTOM
;
1458 dotdot
= strstr(arg
, "..");
1460 unsigned char from_sha1
[20];
1461 const char *next
= dotdot
+ 2;
1462 const char *this = arg
;
1463 int symmetric
= *next
== '.';
1464 unsigned int flags_exclude
= flags
^ (UNINTERESTING
| BOTTOM
);
1465 static const char head_by_default
[] = "HEAD";
1466 unsigned int a_flags
;
1472 next
= head_by_default
;
1474 this = head_by_default
;
1475 if (this == head_by_default
&& next
== head_by_default
&&
1478 * Just ".."? That is not a range but the
1479 * pathspec for the parent directory.
1481 if (!cant_be_filename
) {
1486 if (!get_sha1_committish(this, from_sha1
) &&
1487 !get_sha1_committish(next
, sha1
)) {
1488 struct object
*a_obj
, *b_obj
;
1490 if (!cant_be_filename
) {
1492 verify_non_filename(revs
->prefix
, arg
);
1495 a_obj
= parse_object(from_sha1
);
1496 b_obj
= parse_object(sha1
);
1497 if (!a_obj
|| !b_obj
) {
1499 if (revs
->ignore_missing
)
1502 ? "Invalid symmetric difference expression %s"
1503 : "Invalid revision range %s", arg
);
1508 a_flags
= flags_exclude
;
1510 /* A...B -- find merge bases between the two */
1511 struct commit
*a
, *b
;
1512 struct commit_list
*exclude
;
1514 a
= (a_obj
->type
== OBJ_COMMIT
1515 ? (struct commit
*)a_obj
1516 : lookup_commit_reference(a_obj
->oid
.hash
));
1517 b
= (b_obj
->type
== OBJ_COMMIT
1518 ? (struct commit
*)b_obj
1519 : lookup_commit_reference(b_obj
->oid
.hash
));
1522 exclude
= get_merge_bases(a
, b
);
1523 add_rev_cmdline_list(revs
, exclude
,
1526 add_pending_commit_list(revs
, exclude
,
1528 free_commit_list(exclude
);
1530 a_flags
= flags
| SYMMETRIC_LEFT
;
1533 a_obj
->flags
|= a_flags
;
1534 b_obj
->flags
|= flags
;
1535 add_rev_cmdline(revs
, a_obj
, this,
1536 REV_CMD_LEFT
, a_flags
);
1537 add_rev_cmdline(revs
, b_obj
, next
,
1538 REV_CMD_RIGHT
, flags
);
1539 add_pending_object(revs
, a_obj
, this);
1540 add_pending_object(revs
, b_obj
, next
);
1545 dotdot
= strstr(arg
, "^@");
1546 if (dotdot
&& !dotdot
[2]) {
1548 if (add_parents_only(revs
, arg
, flags
))
1552 dotdot
= strstr(arg
, "^!");
1553 if (dotdot
&& !dotdot
[2]) {
1555 if (!add_parents_only(revs
, arg
, flags
^ (UNINTERESTING
| BOTTOM
)))
1561 local_flags
= UNINTERESTING
| BOTTOM
;
1565 if (revarg_opt
& REVARG_COMMITTISH
)
1566 get_sha1_flags
= GET_SHA1_COMMITTISH
;
1568 if (get_sha1_with_context(arg
, get_sha1_flags
, sha1
, &oc
))
1569 return revs
->ignore_missing
? 0 : -1;
1570 if (!cant_be_filename
)
1571 verify_non_filename(revs
->prefix
, arg
);
1572 object
= get_reference(revs
, arg
, sha1
, flags
^ local_flags
);
1573 add_rev_cmdline(revs
, object
, arg_
, REV_CMD_REV
, flags
^ local_flags
);
1574 add_pending_object_with_mode(revs
, object
, arg
, oc
.mode
);
1578 struct cmdline_pathspec
{
1584 static void append_prune_data(struct cmdline_pathspec
*prune
, const char **av
)
1587 ALLOC_GROW(prune
->path
, prune
->nr
+ 1, prune
->alloc
);
1588 prune
->path
[prune
->nr
++] = *(av
++);
1592 static void read_pathspec_from_stdin(struct rev_info
*revs
, struct strbuf
*sb
,
1593 struct cmdline_pathspec
*prune
)
1595 while (strbuf_getline(sb
, stdin
) != EOF
) {
1596 ALLOC_GROW(prune
->path
, prune
->nr
+ 1, prune
->alloc
);
1597 prune
->path
[prune
->nr
++] = xstrdup(sb
->buf
);
1601 static void read_revisions_from_stdin(struct rev_info
*revs
,
1602 struct cmdline_pathspec
*prune
)
1605 int seen_dashdash
= 0;
1608 save_warning
= warn_on_object_refname_ambiguity
;
1609 warn_on_object_refname_ambiguity
= 0;
1611 strbuf_init(&sb
, 1000);
1612 while (strbuf_getline(&sb
, stdin
) != EOF
) {
1616 if (sb
.buf
[0] == '-') {
1617 if (len
== 2 && sb
.buf
[1] == '-') {
1621 die("options not supported in --stdin mode");
1623 if (handle_revision_arg(sb
.buf
, revs
, 0,
1624 REVARG_CANNOT_BE_FILENAME
))
1625 die("bad revision '%s'", sb
.buf
);
1628 read_pathspec_from_stdin(revs
, &sb
, prune
);
1630 strbuf_release(&sb
);
1631 warn_on_object_refname_ambiguity
= save_warning
;
1634 static void add_grep(struct rev_info
*revs
, const char *ptn
, enum grep_pat_token what
)
1636 append_grep_pattern(&revs
->grep_filter
, ptn
, "command line", 0, what
);
1639 static void add_header_grep(struct rev_info
*revs
, enum grep_header_field field
, const char *pattern
)
1641 append_header_grep_pattern(&revs
->grep_filter
, field
, pattern
);
1644 static void add_message_grep(struct rev_info
*revs
, const char *pattern
)
1646 add_grep(revs
, pattern
, GREP_PATTERN_BODY
);
1649 static int handle_revision_opt(struct rev_info
*revs
, int argc
, const char **argv
,
1650 int *unkc
, const char **unkv
)
1652 const char *arg
= argv
[0];
1656 /* pseudo revision arguments */
1657 if (!strcmp(arg
, "--all") || !strcmp(arg
, "--branches") ||
1658 !strcmp(arg
, "--tags") || !strcmp(arg
, "--remotes") ||
1659 !strcmp(arg
, "--reflog") || !strcmp(arg
, "--not") ||
1660 !strcmp(arg
, "--no-walk") || !strcmp(arg
, "--do-walk") ||
1661 !strcmp(arg
, "--bisect") || starts_with(arg
, "--glob=") ||
1662 !strcmp(arg
, "--indexed-objects") ||
1663 starts_with(arg
, "--exclude=") ||
1664 starts_with(arg
, "--branches=") || starts_with(arg
, "--tags=") ||
1665 starts_with(arg
, "--remotes=") || starts_with(arg
, "--no-walk="))
1667 unkv
[(*unkc
)++] = arg
;
1671 if ((argcount
= parse_long_opt("max-count", argv
, &optarg
))) {
1672 revs
->max_count
= atoi(optarg
);
1675 } else if ((argcount
= parse_long_opt("skip", argv
, &optarg
))) {
1676 revs
->skip_count
= atoi(optarg
);
1678 } else if ((*arg
== '-') && isdigit(arg
[1])) {
1679 /* accept -<digit>, like traditional "head" */
1680 if (strtol_i(arg
+ 1, 10, &revs
->max_count
) < 0 ||
1681 revs
->max_count
< 0)
1682 die("'%s': not a non-negative integer", arg
+ 1);
1684 } else if (!strcmp(arg
, "-n")) {
1686 return error("-n requires an argument");
1687 revs
->max_count
= atoi(argv
[1]);
1690 } else if (starts_with(arg
, "-n")) {
1691 revs
->max_count
= atoi(arg
+ 2);
1693 } else if ((argcount
= parse_long_opt("max-age", argv
, &optarg
))) {
1694 revs
->max_age
= atoi(optarg
);
1696 } else if ((argcount
= parse_long_opt("since", argv
, &optarg
))) {
1697 revs
->max_age
= approxidate(optarg
);
1699 } else if ((argcount
= parse_long_opt("after", argv
, &optarg
))) {
1700 revs
->max_age
= approxidate(optarg
);
1702 } else if ((argcount
= parse_long_opt("min-age", argv
, &optarg
))) {
1703 revs
->min_age
= atoi(optarg
);
1705 } else if ((argcount
= parse_long_opt("before", argv
, &optarg
))) {
1706 revs
->min_age
= approxidate(optarg
);
1708 } else if ((argcount
= parse_long_opt("until", argv
, &optarg
))) {
1709 revs
->min_age
= approxidate(optarg
);
1711 } else if (!strcmp(arg
, "--first-parent")) {
1712 revs
->first_parent_only
= 1;
1713 } else if (!strcmp(arg
, "--ancestry-path")) {
1714 revs
->ancestry_path
= 1;
1715 revs
->simplify_history
= 0;
1717 } else if (!strcmp(arg
, "-g") || !strcmp(arg
, "--walk-reflogs")) {
1718 init_reflog_walk(&revs
->reflog_info
);
1719 } else if (!strcmp(arg
, "--default")) {
1721 return error("bad --default argument");
1722 revs
->def
= argv
[1];
1724 } else if (!strcmp(arg
, "--merge")) {
1725 revs
->show_merge
= 1;
1726 } else if (!strcmp(arg
, "--topo-order")) {
1727 revs
->sort_order
= REV_SORT_IN_GRAPH_ORDER
;
1728 revs
->topo_order
= 1;
1729 } else if (!strcmp(arg
, "--simplify-merges")) {
1730 revs
->simplify_merges
= 1;
1731 revs
->topo_order
= 1;
1732 revs
->rewrite_parents
= 1;
1733 revs
->simplify_history
= 0;
1735 } else if (!strcmp(arg
, "--simplify-by-decoration")) {
1736 revs
->simplify_merges
= 1;
1737 revs
->topo_order
= 1;
1738 revs
->rewrite_parents
= 1;
1739 revs
->simplify_history
= 0;
1740 revs
->simplify_by_decoration
= 1;
1743 load_ref_decorations(DECORATE_SHORT_REFS
);
1744 } else if (!strcmp(arg
, "--date-order")) {
1745 revs
->sort_order
= REV_SORT_BY_COMMIT_DATE
;
1746 revs
->topo_order
= 1;
1747 } else if (!strcmp(arg
, "--author-date-order")) {
1748 revs
->sort_order
= REV_SORT_BY_AUTHOR_DATE
;
1749 revs
->topo_order
= 1;
1750 } else if (starts_with(arg
, "--early-output")) {
1754 count
= atoi(arg
+15);
1757 revs
->topo_order
= 1;
1758 revs
->early_output
= count
;
1760 } else if (!strcmp(arg
, "--parents")) {
1761 revs
->rewrite_parents
= 1;
1762 revs
->print_parents
= 1;
1763 } else if (!strcmp(arg
, "--dense")) {
1765 } else if (!strcmp(arg
, "--sparse")) {
1767 } else if (!strcmp(arg
, "--show-all")) {
1769 } else if (!strcmp(arg
, "--remove-empty")) {
1770 revs
->remove_empty_trees
= 1;
1771 } else if (!strcmp(arg
, "--merges")) {
1772 revs
->min_parents
= 2;
1773 } else if (!strcmp(arg
, "--no-merges")) {
1774 revs
->max_parents
= 1;
1775 } else if (starts_with(arg
, "--min-parents=")) {
1776 revs
->min_parents
= atoi(arg
+14);
1777 } else if (starts_with(arg
, "--no-min-parents")) {
1778 revs
->min_parents
= 0;
1779 } else if (starts_with(arg
, "--max-parents=")) {
1780 revs
->max_parents
= atoi(arg
+14);
1781 } else if (starts_with(arg
, "--no-max-parents")) {
1782 revs
->max_parents
= -1;
1783 } else if (!strcmp(arg
, "--boundary")) {
1785 } else if (!strcmp(arg
, "--left-right")) {
1786 revs
->left_right
= 1;
1787 } else if (!strcmp(arg
, "--left-only")) {
1788 if (revs
->right_only
)
1789 die("--left-only is incompatible with --right-only"
1791 revs
->left_only
= 1;
1792 } else if (!strcmp(arg
, "--right-only")) {
1793 if (revs
->left_only
)
1794 die("--right-only is incompatible with --left-only");
1795 revs
->right_only
= 1;
1796 } else if (!strcmp(arg
, "--cherry")) {
1797 if (revs
->left_only
)
1798 die("--cherry is incompatible with --left-only");
1799 revs
->cherry_mark
= 1;
1800 revs
->right_only
= 1;
1801 revs
->max_parents
= 1;
1803 } else if (!strcmp(arg
, "--count")) {
1805 } else if (!strcmp(arg
, "--cherry-mark")) {
1806 if (revs
->cherry_pick
)
1807 die("--cherry-mark is incompatible with --cherry-pick");
1808 revs
->cherry_mark
= 1;
1809 revs
->limited
= 1; /* needs limit_list() */
1810 } else if (!strcmp(arg
, "--cherry-pick")) {
1811 if (revs
->cherry_mark
)
1812 die("--cherry-pick is incompatible with --cherry-mark");
1813 revs
->cherry_pick
= 1;
1815 } else if (!strcmp(arg
, "--objects")) {
1816 revs
->tag_objects
= 1;
1817 revs
->tree_objects
= 1;
1818 revs
->blob_objects
= 1;
1819 } else if (!strcmp(arg
, "--objects-edge")) {
1820 revs
->tag_objects
= 1;
1821 revs
->tree_objects
= 1;
1822 revs
->blob_objects
= 1;
1823 revs
->edge_hint
= 1;
1824 } else if (!strcmp(arg
, "--objects-edge-aggressive")) {
1825 revs
->tag_objects
= 1;
1826 revs
->tree_objects
= 1;
1827 revs
->blob_objects
= 1;
1828 revs
->edge_hint
= 1;
1829 revs
->edge_hint_aggressive
= 1;
1830 } else if (!strcmp(arg
, "--verify-objects")) {
1831 revs
->tag_objects
= 1;
1832 revs
->tree_objects
= 1;
1833 revs
->blob_objects
= 1;
1834 revs
->verify_objects
= 1;
1835 } else if (!strcmp(arg
, "--unpacked")) {
1837 } else if (starts_with(arg
, "--unpacked=")) {
1838 die("--unpacked=<packfile> no longer supported.");
1839 } else if (!strcmp(arg
, "-r")) {
1841 DIFF_OPT_SET(&revs
->diffopt
, RECURSIVE
);
1842 } else if (!strcmp(arg
, "-t")) {
1844 DIFF_OPT_SET(&revs
->diffopt
, RECURSIVE
);
1845 DIFF_OPT_SET(&revs
->diffopt
, TREE_IN_RECURSIVE
);
1846 } else if (!strcmp(arg
, "-m")) {
1847 revs
->ignore_merges
= 0;
1848 } else if (!strcmp(arg
, "-c")) {
1850 revs
->dense_combined_merges
= 0;
1851 revs
->combine_merges
= 1;
1852 } else if (!strcmp(arg
, "--cc")) {
1854 revs
->dense_combined_merges
= 1;
1855 revs
->combine_merges
= 1;
1856 } else if (!strcmp(arg
, "-v")) {
1857 revs
->verbose_header
= 1;
1858 } else if (!strcmp(arg
, "--pretty")) {
1859 revs
->verbose_header
= 1;
1860 revs
->pretty_given
= 1;
1861 get_commit_format(NULL
, revs
);
1862 } else if (starts_with(arg
, "--pretty=") || starts_with(arg
, "--format=")) {
1864 * Detached form ("--pretty X" as opposed to "--pretty=X")
1865 * not allowed, since the argument is optional.
1867 revs
->verbose_header
= 1;
1868 revs
->pretty_given
= 1;
1869 get_commit_format(arg
+9, revs
);
1870 } else if (!strcmp(arg
, "--show-notes") || !strcmp(arg
, "--notes")) {
1871 revs
->show_notes
= 1;
1872 revs
->show_notes_given
= 1;
1873 revs
->notes_opt
.use_default_notes
= 1;
1874 } else if (!strcmp(arg
, "--show-signature")) {
1875 revs
->show_signature
= 1;
1876 } else if (!strcmp(arg
, "--show-linear-break") ||
1877 starts_with(arg
, "--show-linear-break=")) {
1878 if (starts_with(arg
, "--show-linear-break="))
1879 revs
->break_bar
= xstrdup(arg
+ 20);
1881 revs
->break_bar
= " ..........";
1882 revs
->track_linear
= 1;
1883 revs
->track_first_time
= 1;
1884 } else if (starts_with(arg
, "--show-notes=") ||
1885 starts_with(arg
, "--notes=")) {
1886 struct strbuf buf
= STRBUF_INIT
;
1887 revs
->show_notes
= 1;
1888 revs
->show_notes_given
= 1;
1889 if (starts_with(arg
, "--show-notes")) {
1890 if (revs
->notes_opt
.use_default_notes
< 0)
1891 revs
->notes_opt
.use_default_notes
= 1;
1892 strbuf_addstr(&buf
, arg
+13);
1895 strbuf_addstr(&buf
, arg
+8);
1896 expand_notes_ref(&buf
);
1897 string_list_append(&revs
->notes_opt
.extra_notes_refs
,
1898 strbuf_detach(&buf
, NULL
));
1899 } else if (!strcmp(arg
, "--no-notes")) {
1900 revs
->show_notes
= 0;
1901 revs
->show_notes_given
= 1;
1902 revs
->notes_opt
.use_default_notes
= -1;
1903 /* we have been strdup'ing ourselves, so trick
1904 * string_list into free()ing strings */
1905 revs
->notes_opt
.extra_notes_refs
.strdup_strings
= 1;
1906 string_list_clear(&revs
->notes_opt
.extra_notes_refs
, 0);
1907 revs
->notes_opt
.extra_notes_refs
.strdup_strings
= 0;
1908 } else if (!strcmp(arg
, "--standard-notes")) {
1909 revs
->show_notes_given
= 1;
1910 revs
->notes_opt
.use_default_notes
= 1;
1911 } else if (!strcmp(arg
, "--no-standard-notes")) {
1912 revs
->notes_opt
.use_default_notes
= 0;
1913 } else if (!strcmp(arg
, "--oneline")) {
1914 revs
->verbose_header
= 1;
1915 get_commit_format("oneline", revs
);
1916 revs
->pretty_given
= 1;
1917 revs
->abbrev_commit
= 1;
1918 } else if (!strcmp(arg
, "--graph")) {
1919 revs
->topo_order
= 1;
1920 revs
->rewrite_parents
= 1;
1921 revs
->graph
= graph_init(revs
);
1922 } else if (!strcmp(arg
, "--root")) {
1923 revs
->show_root_diff
= 1;
1924 } else if (!strcmp(arg
, "--no-commit-id")) {
1925 revs
->no_commit_id
= 1;
1926 } else if (!strcmp(arg
, "--always")) {
1927 revs
->always_show_header
= 1;
1928 } else if (!strcmp(arg
, "--no-abbrev")) {
1930 } else if (!strcmp(arg
, "--abbrev")) {
1931 revs
->abbrev
= DEFAULT_ABBREV
;
1932 } else if (starts_with(arg
, "--abbrev=")) {
1933 revs
->abbrev
= strtoul(arg
+ 9, NULL
, 10);
1934 if (revs
->abbrev
< MINIMUM_ABBREV
)
1935 revs
->abbrev
= MINIMUM_ABBREV
;
1936 else if (revs
->abbrev
> 40)
1938 } else if (!strcmp(arg
, "--abbrev-commit")) {
1939 revs
->abbrev_commit
= 1;
1940 revs
->abbrev_commit_given
= 1;
1941 } else if (!strcmp(arg
, "--no-abbrev-commit")) {
1942 revs
->abbrev_commit
= 0;
1943 } else if (!strcmp(arg
, "--full-diff")) {
1945 revs
->full_diff
= 1;
1946 } else if (!strcmp(arg
, "--full-history")) {
1947 revs
->simplify_history
= 0;
1948 } else if (!strcmp(arg
, "--relative-date")) {
1949 revs
->date_mode
.type
= DATE_RELATIVE
;
1950 revs
->date_mode_explicit
= 1;
1951 } else if ((argcount
= parse_long_opt("date", argv
, &optarg
))) {
1952 parse_date_format(optarg
, &revs
->date_mode
);
1953 revs
->date_mode_explicit
= 1;
1955 } else if (!strcmp(arg
, "--log-size")) {
1956 revs
->show_log_size
= 1;
1959 * Grepping the commit log
1961 else if ((argcount
= parse_long_opt("author", argv
, &optarg
))) {
1962 add_header_grep(revs
, GREP_HEADER_AUTHOR
, optarg
);
1964 } else if ((argcount
= parse_long_opt("committer", argv
, &optarg
))) {
1965 add_header_grep(revs
, GREP_HEADER_COMMITTER
, optarg
);
1967 } else if ((argcount
= parse_long_opt("grep-reflog", argv
, &optarg
))) {
1968 add_header_grep(revs
, GREP_HEADER_REFLOG
, optarg
);
1970 } else if ((argcount
= parse_long_opt("grep", argv
, &optarg
))) {
1971 add_message_grep(revs
, optarg
);
1973 } else if (!strcmp(arg
, "--grep-debug")) {
1974 revs
->grep_filter
.debug
= 1;
1975 } else if (!strcmp(arg
, "--basic-regexp")) {
1976 grep_set_pattern_type_option(GREP_PATTERN_TYPE_BRE
, &revs
->grep_filter
);
1977 } else if (!strcmp(arg
, "--extended-regexp") || !strcmp(arg
, "-E")) {
1978 grep_set_pattern_type_option(GREP_PATTERN_TYPE_ERE
, &revs
->grep_filter
);
1979 } else if (!strcmp(arg
, "--regexp-ignore-case") || !strcmp(arg
, "-i")) {
1980 revs
->grep_filter
.regflags
|= REG_ICASE
;
1981 DIFF_OPT_SET(&revs
->diffopt
, PICKAXE_IGNORE_CASE
);
1982 } else if (!strcmp(arg
, "--fixed-strings") || !strcmp(arg
, "-F")) {
1983 grep_set_pattern_type_option(GREP_PATTERN_TYPE_FIXED
, &revs
->grep_filter
);
1984 } else if (!strcmp(arg
, "--perl-regexp")) {
1985 grep_set_pattern_type_option(GREP_PATTERN_TYPE_PCRE
, &revs
->grep_filter
);
1986 } else if (!strcmp(arg
, "--all-match")) {
1987 revs
->grep_filter
.all_match
= 1;
1988 } else if (!strcmp(arg
, "--invert-grep")) {
1989 revs
->invert_grep
= 1;
1990 } else if ((argcount
= parse_long_opt("encoding", argv
, &optarg
))) {
1991 if (strcmp(optarg
, "none"))
1992 git_log_output_encoding
= xstrdup(optarg
);
1994 git_log_output_encoding
= "";
1996 } else if (!strcmp(arg
, "--reverse")) {
1998 } else if (!strcmp(arg
, "--children")) {
1999 revs
->children
.name
= "children";
2001 } else if (!strcmp(arg
, "--ignore-missing")) {
2002 revs
->ignore_missing
= 1;
2004 int opts
= diff_opt_parse(&revs
->diffopt
, argv
, argc
, revs
->prefix
);
2006 unkv
[(*unkc
)++] = arg
;
2009 if (revs
->graph
&& revs
->track_linear
)
2010 die("--show-linear-break and --graph are incompatible");
2015 void parse_revision_opt(struct rev_info
*revs
, struct parse_opt_ctx_t
*ctx
,
2016 const struct option
*options
,
2017 const char * const usagestr
[])
2019 int n
= handle_revision_opt(revs
, ctx
->argc
, ctx
->argv
,
2020 &ctx
->cpidx
, ctx
->out
);
2022 error("unknown option `%s'", ctx
->argv
[0]);
2023 usage_with_options(usagestr
, options
);
2029 static int for_each_bisect_ref(const char *submodule
, each_ref_fn fn
, void *cb_data
, const char *term
) {
2030 struct strbuf bisect_refs
= STRBUF_INIT
;
2032 strbuf_addf(&bisect_refs
, "refs/bisect/%s", term
);
2033 status
= for_each_ref_in_submodule(submodule
, bisect_refs
.buf
, fn
, cb_data
);
2034 strbuf_release(&bisect_refs
);
2038 static int for_each_bad_bisect_ref(const char *submodule
, each_ref_fn fn
, void *cb_data
)
2040 return for_each_bisect_ref(submodule
, fn
, cb_data
, term_bad
);
2043 static int for_each_good_bisect_ref(const char *submodule
, each_ref_fn fn
, void *cb_data
)
2045 return for_each_bisect_ref(submodule
, fn
, cb_data
, term_good
);
2048 static int handle_revision_pseudo_opt(const char *submodule
,
2049 struct rev_info
*revs
,
2050 int argc
, const char **argv
, int *flags
)
2052 const char *arg
= argv
[0];
2059 * Commands like "git shortlog" will not accept the options below
2060 * unless parse_revision_opt queues them (as opposed to erroring
2063 * When implementing your new pseudo-option, remember to
2064 * register it in the list at the top of handle_revision_opt.
2066 if (!strcmp(arg
, "--all")) {
2067 handle_refs(submodule
, revs
, *flags
, for_each_ref_submodule
);
2068 handle_refs(submodule
, revs
, *flags
, head_ref_submodule
);
2069 clear_ref_exclusion(&revs
->ref_excludes
);
2070 } else if (!strcmp(arg
, "--branches")) {
2071 handle_refs(submodule
, revs
, *flags
, for_each_branch_ref_submodule
);
2072 clear_ref_exclusion(&revs
->ref_excludes
);
2073 } else if (!strcmp(arg
, "--bisect")) {
2074 read_bisect_terms(&term_bad
, &term_good
);
2075 handle_refs(submodule
, revs
, *flags
, for_each_bad_bisect_ref
);
2076 handle_refs(submodule
, revs
, *flags
^ (UNINTERESTING
| BOTTOM
), for_each_good_bisect_ref
);
2078 } else if (!strcmp(arg
, "--tags")) {
2079 handle_refs(submodule
, revs
, *flags
, for_each_tag_ref_submodule
);
2080 clear_ref_exclusion(&revs
->ref_excludes
);
2081 } else if (!strcmp(arg
, "--remotes")) {
2082 handle_refs(submodule
, revs
, *flags
, for_each_remote_ref_submodule
);
2083 clear_ref_exclusion(&revs
->ref_excludes
);
2084 } else if ((argcount
= parse_long_opt("glob", argv
, &optarg
))) {
2085 struct all_refs_cb cb
;
2086 init_all_refs_cb(&cb
, revs
, *flags
);
2087 for_each_glob_ref(handle_one_ref
, optarg
, &cb
);
2088 clear_ref_exclusion(&revs
->ref_excludes
);
2090 } else if ((argcount
= parse_long_opt("exclude", argv
, &optarg
))) {
2091 add_ref_exclusion(&revs
->ref_excludes
, optarg
);
2093 } else if (starts_with(arg
, "--branches=")) {
2094 struct all_refs_cb cb
;
2095 init_all_refs_cb(&cb
, revs
, *flags
);
2096 for_each_glob_ref_in(handle_one_ref
, arg
+ 11, "refs/heads/", &cb
);
2097 clear_ref_exclusion(&revs
->ref_excludes
);
2098 } else if (starts_with(arg
, "--tags=")) {
2099 struct all_refs_cb cb
;
2100 init_all_refs_cb(&cb
, revs
, *flags
);
2101 for_each_glob_ref_in(handle_one_ref
, arg
+ 7, "refs/tags/", &cb
);
2102 clear_ref_exclusion(&revs
->ref_excludes
);
2103 } else if (starts_with(arg
, "--remotes=")) {
2104 struct all_refs_cb cb
;
2105 init_all_refs_cb(&cb
, revs
, *flags
);
2106 for_each_glob_ref_in(handle_one_ref
, arg
+ 10, "refs/remotes/", &cb
);
2107 clear_ref_exclusion(&revs
->ref_excludes
);
2108 } else if (!strcmp(arg
, "--reflog")) {
2109 add_reflogs_to_pending(revs
, *flags
);
2110 } else if (!strcmp(arg
, "--indexed-objects")) {
2111 add_index_objects_to_pending(revs
, *flags
);
2112 } else if (!strcmp(arg
, "--not")) {
2113 *flags
^= UNINTERESTING
| BOTTOM
;
2114 } else if (!strcmp(arg
, "--no-walk")) {
2115 revs
->no_walk
= REVISION_WALK_NO_WALK_SORTED
;
2116 } else if (starts_with(arg
, "--no-walk=")) {
2118 * Detached form ("--no-walk X" as opposed to "--no-walk=X")
2119 * not allowed, since the argument is optional.
2121 if (!strcmp(arg
+ 10, "sorted"))
2122 revs
->no_walk
= REVISION_WALK_NO_WALK_SORTED
;
2123 else if (!strcmp(arg
+ 10, "unsorted"))
2124 revs
->no_walk
= REVISION_WALK_NO_WALK_UNSORTED
;
2126 return error("invalid argument to --no-walk");
2127 } else if (!strcmp(arg
, "--do-walk")) {
2136 static void NORETURN
diagnose_missing_default(const char *def
)
2138 unsigned char sha1
[20];
2140 const char *refname
;
2142 refname
= resolve_ref_unsafe(def
, 0, sha1
, &flags
);
2143 if (!refname
|| !(flags
& REF_ISSYMREF
) || (flags
& REF_ISBROKEN
))
2144 die(_("your current branch appears to be broken"));
2146 skip_prefix(refname
, "refs/heads/", &refname
);
2147 die(_("your current branch '%s' does not have any commits yet"),
2152 * Parse revision information, filling in the "rev_info" structure,
2153 * and removing the used arguments from the argument list.
2155 * Returns the number of arguments left that weren't recognized
2156 * (which are also moved to the head of the argument list)
2158 int setup_revisions(int argc
, const char **argv
, struct rev_info
*revs
, struct setup_revision_opt
*opt
)
2160 int i
, flags
, left
, seen_dashdash
, read_from_stdin
, got_rev_arg
= 0, revarg_opt
;
2161 struct cmdline_pathspec prune_data
;
2162 const char *submodule
= NULL
;
2164 memset(&prune_data
, 0, sizeof(prune_data
));
2166 submodule
= opt
->submodule
;
2168 /* First, search for "--" */
2169 if (opt
&& opt
->assume_dashdash
) {
2173 for (i
= 1; i
< argc
; i
++) {
2174 const char *arg
= argv
[i
];
2175 if (strcmp(arg
, "--"))
2180 append_prune_data(&prune_data
, argv
+ i
+ 1);
2186 /* Second, deal with arguments and options */
2188 revarg_opt
= opt
? opt
->revarg_opt
: 0;
2190 revarg_opt
|= REVARG_CANNOT_BE_FILENAME
;
2191 read_from_stdin
= 0;
2192 for (left
= i
= 1; i
< argc
; i
++) {
2193 const char *arg
= argv
[i
];
2197 opts
= handle_revision_pseudo_opt(submodule
,
2198 revs
, argc
- i
, argv
+ i
,
2205 if (!strcmp(arg
, "--stdin")) {
2206 if (revs
->disable_stdin
) {
2210 if (read_from_stdin
++)
2211 die("--stdin given twice?");
2212 read_revisions_from_stdin(revs
, &prune_data
);
2216 opts
= handle_revision_opt(revs
, argc
- i
, argv
+ i
, &left
, argv
);
2227 if (handle_revision_arg(arg
, revs
, flags
, revarg_opt
)) {
2229 if (seen_dashdash
|| *arg
== '^')
2230 die("bad revision '%s'", arg
);
2232 /* If we didn't have a "--":
2233 * (1) all filenames must exist;
2234 * (2) all rev-args must not be interpretable
2235 * as a valid filename.
2236 * but the latter we have checked in the main loop.
2238 for (j
= i
; j
< argc
; j
++)
2239 verify_filename(revs
->prefix
, argv
[j
], j
== i
);
2241 append_prune_data(&prune_data
, argv
+ i
);
2248 if (prune_data
.nr
) {
2250 * If we need to introduce the magic "a lone ':' means no
2251 * pathspec whatsoever", here is the place to do so.
2253 * if (prune_data.nr == 1 && !strcmp(prune_data[0], ":")) {
2254 * prune_data.nr = 0;
2255 * prune_data.alloc = 0;
2256 * free(prune_data.path);
2257 * prune_data.path = NULL;
2259 * terminate prune_data.alloc with NULL and
2260 * call init_pathspec() to set revs->prune_data here.
2263 ALLOC_GROW(prune_data
.path
, prune_data
.nr
+ 1, prune_data
.alloc
);
2264 prune_data
.path
[prune_data
.nr
++] = NULL
;
2265 parse_pathspec(&revs
->prune_data
, 0, 0,
2266 revs
->prefix
, prune_data
.path
);
2269 if (revs
->def
== NULL
)
2270 revs
->def
= opt
? opt
->def
: NULL
;
2271 if (opt
&& opt
->tweak
)
2272 opt
->tweak(revs
, opt
);
2273 if (revs
->show_merge
)
2274 prepare_show_merge(revs
);
2275 if (revs
->def
&& !revs
->pending
.nr
&& !got_rev_arg
) {
2276 unsigned char sha1
[20];
2277 struct object
*object
;
2278 struct object_context oc
;
2279 if (get_sha1_with_context(revs
->def
, 0, sha1
, &oc
))
2280 diagnose_missing_default(revs
->def
);
2281 object
= get_reference(revs
, revs
->def
, sha1
, 0);
2282 add_pending_object_with_mode(revs
, object
, revs
->def
, oc
.mode
);
2285 /* Did the user ask for any diff output? Run the diff! */
2286 if (revs
->diffopt
.output_format
& ~DIFF_FORMAT_NO_OUTPUT
)
2289 /* Pickaxe, diff-filter and rename following need diffs */
2290 if (revs
->diffopt
.pickaxe
||
2291 revs
->diffopt
.filter
||
2292 DIFF_OPT_TST(&revs
->diffopt
, FOLLOW_RENAMES
))
2295 if (revs
->topo_order
)
2298 if (revs
->prune_data
.nr
) {
2299 copy_pathspec(&revs
->pruning
.pathspec
, &revs
->prune_data
);
2300 /* Can't prune commits with rename following: the paths change.. */
2301 if (!DIFF_OPT_TST(&revs
->diffopt
, FOLLOW_RENAMES
))
2303 if (!revs
->full_diff
)
2304 copy_pathspec(&revs
->diffopt
.pathspec
,
2307 if (revs
->combine_merges
)
2308 revs
->ignore_merges
= 0;
2309 revs
->diffopt
.abbrev
= revs
->abbrev
;
2311 if (revs
->line_level_traverse
) {
2313 revs
->topo_order
= 1;
2316 diff_setup_done(&revs
->diffopt
);
2318 grep_commit_pattern_type(GREP_PATTERN_TYPE_UNSPECIFIED
,
2319 &revs
->grep_filter
);
2320 compile_grep_patterns(&revs
->grep_filter
);
2322 if (revs
->reverse
&& revs
->reflog_info
)
2323 die("cannot combine --reverse with --walk-reflogs");
2324 if (revs
->rewrite_parents
&& revs
->children
.name
)
2325 die("cannot combine --parents and --children");
2328 * Limitations on the graph functionality
2330 if (revs
->reverse
&& revs
->graph
)
2331 die("cannot combine --reverse with --graph");
2333 if (revs
->reflog_info
&& revs
->graph
)
2334 die("cannot combine --walk-reflogs with --graph");
2335 if (revs
->no_walk
&& revs
->graph
)
2336 die("cannot combine --no-walk with --graph");
2337 if (!revs
->reflog_info
&& revs
->grep_filter
.use_reflog_filter
)
2338 die("cannot use --grep-reflog without --walk-reflogs");
2340 if (revs
->first_parent_only
&& revs
->bisect
)
2341 die(_("--first-parent is incompatible with --bisect"));
2346 static void add_child(struct rev_info
*revs
, struct commit
*parent
, struct commit
*child
)
2348 struct commit_list
*l
= xcalloc(1, sizeof(*l
));
2351 l
->next
= add_decoration(&revs
->children
, &parent
->object
, l
);
2354 static int remove_duplicate_parents(struct rev_info
*revs
, struct commit
*commit
)
2356 struct treesame_state
*ts
= lookup_decoration(&revs
->treesame
, &commit
->object
);
2357 struct commit_list
**pp
, *p
;
2358 int surviving_parents
;
2360 /* Examine existing parents while marking ones we have seen... */
2361 pp
= &commit
->parents
;
2362 surviving_parents
= 0;
2363 while ((p
= *pp
) != NULL
) {
2364 struct commit
*parent
= p
->item
;
2365 if (parent
->object
.flags
& TMP_MARK
) {
2368 compact_treesame(revs
, commit
, surviving_parents
);
2371 parent
->object
.flags
|= TMP_MARK
;
2372 surviving_parents
++;
2375 /* clear the temporary mark */
2376 for (p
= commit
->parents
; p
; p
= p
->next
) {
2377 p
->item
->object
.flags
&= ~TMP_MARK
;
2379 /* no update_treesame() - removing duplicates can't affect TREESAME */
2380 return surviving_parents
;
2383 struct merge_simplify_state
{
2384 struct commit
*simplified
;
2387 static struct merge_simplify_state
*locate_simplify_state(struct rev_info
*revs
, struct commit
*commit
)
2389 struct merge_simplify_state
*st
;
2391 st
= lookup_decoration(&revs
->merge_simplification
, &commit
->object
);
2393 st
= xcalloc(1, sizeof(*st
));
2394 add_decoration(&revs
->merge_simplification
, &commit
->object
, st
);
2399 static int mark_redundant_parents(struct rev_info
*revs
, struct commit
*commit
)
2401 struct commit_list
*h
= reduce_heads(commit
->parents
);
2402 int i
= 0, marked
= 0;
2403 struct commit_list
*po
, *pn
;
2405 /* Want these for sanity-checking only */
2406 int orig_cnt
= commit_list_count(commit
->parents
);
2407 int cnt
= commit_list_count(h
);
2410 * Not ready to remove items yet, just mark them for now, based
2411 * on the output of reduce_heads(). reduce_heads outputs the reduced
2412 * set in its original order, so this isn't too hard.
2414 po
= commit
->parents
;
2417 if (pn
&& po
->item
== pn
->item
) {
2421 po
->item
->object
.flags
|= TMP_MARK
;
2427 if (i
!= cnt
|| cnt
+marked
!= orig_cnt
)
2428 die("mark_redundant_parents %d %d %d %d", orig_cnt
, cnt
, i
, marked
);
2430 free_commit_list(h
);
2435 static int mark_treesame_root_parents(struct rev_info
*revs
, struct commit
*commit
)
2437 struct commit_list
*p
;
2440 for (p
= commit
->parents
; p
; p
= p
->next
) {
2441 struct commit
*parent
= p
->item
;
2442 if (!parent
->parents
&& (parent
->object
.flags
& TREESAME
)) {
2443 parent
->object
.flags
|= TMP_MARK
;
2452 * Awkward naming - this means one parent we are TREESAME to.
2453 * cf mark_treesame_root_parents: root parents that are TREESAME (to an
2454 * empty tree). Better name suggestions?
2456 static int leave_one_treesame_to_parent(struct rev_info
*revs
, struct commit
*commit
)
2458 struct treesame_state
*ts
= lookup_decoration(&revs
->treesame
, &commit
->object
);
2459 struct commit
*unmarked
= NULL
, *marked
= NULL
;
2460 struct commit_list
*p
;
2463 for (p
= commit
->parents
, n
= 0; p
; p
= p
->next
, n
++) {
2464 if (ts
->treesame
[n
]) {
2465 if (p
->item
->object
.flags
& TMP_MARK
) {
2478 * If we are TREESAME to a marked-for-deletion parent, but not to any
2479 * unmarked parents, unmark the first TREESAME parent. This is the
2480 * parent that the default simplify_history==1 scan would have followed,
2481 * and it doesn't make sense to omit that path when asking for a
2482 * simplified full history. Retaining it improves the chances of
2483 * understanding odd missed merges that took an old version of a file.
2487 * I--------*X A modified the file, but mainline merge X used
2488 * \ / "-s ours", so took the version from I. X is
2489 * `-*A--' TREESAME to I and !TREESAME to A.
2491 * Default log from X would produce "I". Without this check,
2492 * --full-history --simplify-merges would produce "I-A-X", showing
2493 * the merge commit X and that it changed A, but not making clear that
2494 * it had just taken the I version. With this check, the topology above
2497 * Note that it is possible that the simplification chooses a different
2498 * TREESAME parent from the default, in which case this test doesn't
2499 * activate, and we _do_ drop the default parent. Example:
2501 * I------X A modified the file, but it was reverted in B,
2502 * \ / meaning mainline merge X is TREESAME to both
2505 * Default log would produce "I" by following the first parent;
2506 * --full-history --simplify-merges will produce "I-A-B". But this is a
2507 * reasonable result - it presents a logical full history leading from
2508 * I to X, and X is not an important merge.
2510 if (!unmarked
&& marked
) {
2511 marked
->object
.flags
&= ~TMP_MARK
;
2518 static int remove_marked_parents(struct rev_info
*revs
, struct commit
*commit
)
2520 struct commit_list
**pp
, *p
;
2521 int nth_parent
, removed
= 0;
2523 pp
= &commit
->parents
;
2525 while ((p
= *pp
) != NULL
) {
2526 struct commit
*parent
= p
->item
;
2527 if (parent
->object
.flags
& TMP_MARK
) {
2528 parent
->object
.flags
&= ~TMP_MARK
;
2532 compact_treesame(revs
, commit
, nth_parent
);
2539 /* Removing parents can only increase TREESAMEness */
2540 if (removed
&& !(commit
->object
.flags
& TREESAME
))
2541 update_treesame(revs
, commit
);
2546 static struct commit_list
**simplify_one(struct rev_info
*revs
, struct commit
*commit
, struct commit_list
**tail
)
2548 struct commit_list
*p
;
2549 struct commit
*parent
;
2550 struct merge_simplify_state
*st
, *pst
;
2553 st
= locate_simplify_state(revs
, commit
);
2556 * Have we handled this one?
2562 * An UNINTERESTING commit simplifies to itself, so does a
2563 * root commit. We do not rewrite parents of such commit
2566 if ((commit
->object
.flags
& UNINTERESTING
) || !commit
->parents
) {
2567 st
->simplified
= commit
;
2572 * Do we know what commit all of our parents that matter
2573 * should be rewritten to? Otherwise we are not ready to
2574 * rewrite this one yet.
2576 for (cnt
= 0, p
= commit
->parents
; p
; p
= p
->next
) {
2577 pst
= locate_simplify_state(revs
, p
->item
);
2578 if (!pst
->simplified
) {
2579 tail
= &commit_list_insert(p
->item
, tail
)->next
;
2582 if (revs
->first_parent_only
)
2586 tail
= &commit_list_insert(commit
, tail
)->next
;
2591 * Rewrite our list of parents. Note that this cannot
2592 * affect our TREESAME flags in any way - a commit is
2593 * always TREESAME to its simplification.
2595 for (p
= commit
->parents
; p
; p
= p
->next
) {
2596 pst
= locate_simplify_state(revs
, p
->item
);
2597 p
->item
= pst
->simplified
;
2598 if (revs
->first_parent_only
)
2602 if (revs
->first_parent_only
)
2605 cnt
= remove_duplicate_parents(revs
, commit
);
2608 * It is possible that we are a merge and one side branch
2609 * does not have any commit that touches the given paths;
2610 * in such a case, the immediate parent from that branch
2611 * will be rewritten to be the merge base.
2613 * o----X X: the commit we are looking at;
2614 * / / o: a commit that touches the paths;
2617 * Further, a merge of an independent branch that doesn't
2618 * touch the path will reduce to a treesame root parent:
2620 * ----o----X X: the commit we are looking at;
2621 * / o: a commit that touches the paths;
2622 * r r: a root commit not touching the paths
2624 * Detect and simplify both cases.
2627 int marked
= mark_redundant_parents(revs
, commit
);
2628 marked
+= mark_treesame_root_parents(revs
, commit
);
2630 marked
-= leave_one_treesame_to_parent(revs
, commit
);
2632 cnt
= remove_marked_parents(revs
, commit
);
2636 * A commit simplifies to itself if it is a root, if it is
2637 * UNINTERESTING, if it touches the given paths, or if it is a
2638 * merge and its parents don't simplify to one relevant commit
2639 * (the first two cases are already handled at the beginning of
2642 * Otherwise, it simplifies to what its sole relevant parent
2646 (commit
->object
.flags
& UNINTERESTING
) ||
2647 !(commit
->object
.flags
& TREESAME
) ||
2648 (parent
= one_relevant_parent(revs
, commit
->parents
)) == NULL
)
2649 st
->simplified
= commit
;
2651 pst
= locate_simplify_state(revs
, parent
);
2652 st
->simplified
= pst
->simplified
;
2657 static void simplify_merges(struct rev_info
*revs
)
2659 struct commit_list
*list
, *next
;
2660 struct commit_list
*yet_to_do
, **tail
;
2661 struct commit
*commit
;
2666 /* feed the list reversed */
2668 for (list
= revs
->commits
; list
; list
= next
) {
2669 commit
= list
->item
;
2672 * Do not free(list) here yet; the original list
2673 * is used later in this function.
2675 commit_list_insert(commit
, &yet_to_do
);
2682 commit
= pop_commit(&list
);
2683 tail
= simplify_one(revs
, commit
, tail
);
2687 /* clean up the result, removing the simplified ones */
2688 list
= revs
->commits
;
2689 revs
->commits
= NULL
;
2690 tail
= &revs
->commits
;
2692 struct merge_simplify_state
*st
;
2694 commit
= pop_commit(&list
);
2695 st
= locate_simplify_state(revs
, commit
);
2696 if (st
->simplified
== commit
)
2697 tail
= &commit_list_insert(commit
, tail
)->next
;
2701 static void set_children(struct rev_info
*revs
)
2703 struct commit_list
*l
;
2704 for (l
= revs
->commits
; l
; l
= l
->next
) {
2705 struct commit
*commit
= l
->item
;
2706 struct commit_list
*p
;
2708 for (p
= commit
->parents
; p
; p
= p
->next
)
2709 add_child(revs
, p
->item
, commit
);
2713 void reset_revision_walk(void)
2715 clear_object_flags(SEEN
| ADDED
| SHOWN
);
2718 int prepare_revision_walk(struct rev_info
*revs
)
2721 struct object_array old_pending
;
2722 struct commit_list
**next
= &revs
->commits
;
2724 memcpy(&old_pending
, &revs
->pending
, sizeof(old_pending
));
2725 revs
->pending
.nr
= 0;
2726 revs
->pending
.alloc
= 0;
2727 revs
->pending
.objects
= NULL
;
2728 for (i
= 0; i
< old_pending
.nr
; i
++) {
2729 struct object_array_entry
*e
= old_pending
.objects
+ i
;
2730 struct commit
*commit
= handle_commit(revs
, e
);
2732 if (!(commit
->object
.flags
& SEEN
)) {
2733 commit
->object
.flags
|= SEEN
;
2734 next
= commit_list_append(commit
, next
);
2738 if (!revs
->leak_pending
)
2739 object_array_clear(&old_pending
);
2741 /* Signal whether we need per-parent treesame decoration */
2742 if (revs
->simplify_merges
||
2743 (revs
->limited
&& limiting_can_increase_treesame(revs
)))
2744 revs
->treesame
.name
= "treesame";
2746 if (revs
->no_walk
!= REVISION_WALK_NO_WALK_UNSORTED
)
2747 commit_list_sort_by_date(&revs
->commits
);
2751 if (limit_list(revs
) < 0)
2753 if (revs
->topo_order
)
2754 sort_in_topological_order(&revs
->commits
, revs
->sort_order
);
2755 if (revs
->line_level_traverse
)
2756 line_log_filter(revs
);
2757 if (revs
->simplify_merges
)
2758 simplify_merges(revs
);
2759 if (revs
->children
.name
)
2764 static enum rewrite_result
rewrite_one(struct rev_info
*revs
, struct commit
**pp
)
2766 struct commit_list
*cache
= NULL
;
2769 struct commit
*p
= *pp
;
2771 if (add_parents_to_list(revs
, p
, &revs
->commits
, &cache
) < 0)
2772 return rewrite_one_error
;
2773 if (p
->object
.flags
& UNINTERESTING
)
2774 return rewrite_one_ok
;
2775 if (!(p
->object
.flags
& TREESAME
))
2776 return rewrite_one_ok
;
2778 return rewrite_one_noparents
;
2779 if ((p
= one_relevant_parent(revs
, p
->parents
)) == NULL
)
2780 return rewrite_one_ok
;
2785 int rewrite_parents(struct rev_info
*revs
, struct commit
*commit
,
2786 rewrite_parent_fn_t rewrite_parent
)
2788 struct commit_list
**pp
= &commit
->parents
;
2790 struct commit_list
*parent
= *pp
;
2791 switch (rewrite_parent(revs
, &parent
->item
)) {
2792 case rewrite_one_ok
:
2794 case rewrite_one_noparents
:
2797 case rewrite_one_error
:
2802 remove_duplicate_parents(revs
, commit
);
2806 static int commit_rewrite_person(struct strbuf
*buf
, const char *what
, struct string_list
*mailmap
)
2808 char *person
, *endp
;
2809 size_t len
, namelen
, maillen
;
2812 struct ident_split ident
;
2814 person
= strstr(buf
->buf
, what
);
2818 person
+= strlen(what
);
2819 endp
= strchr(person
, '\n');
2823 len
= endp
- person
;
2825 if (split_ident_line(&ident
, person
, len
))
2828 mail
= ident
.mail_begin
;
2829 maillen
= ident
.mail_end
- ident
.mail_begin
;
2830 name
= ident
.name_begin
;
2831 namelen
= ident
.name_end
- ident
.name_begin
;
2833 if (map_user(mailmap
, &mail
, &maillen
, &name
, &namelen
)) {
2834 struct strbuf namemail
= STRBUF_INIT
;
2836 strbuf_addf(&namemail
, "%.*s <%.*s>",
2837 (int)namelen
, name
, (int)maillen
, mail
);
2839 strbuf_splice(buf
, ident
.name_begin
- buf
->buf
,
2840 ident
.mail_end
- ident
.name_begin
+ 1,
2841 namemail
.buf
, namemail
.len
);
2843 strbuf_release(&namemail
);
2851 static int commit_match(struct commit
*commit
, struct rev_info
*opt
)
2854 const char *encoding
;
2855 const char *message
;
2856 struct strbuf buf
= STRBUF_INIT
;
2858 if (!opt
->grep_filter
.pattern_list
&& !opt
->grep_filter
.header_list
)
2861 /* Prepend "fake" headers as needed */
2862 if (opt
->grep_filter
.use_reflog_filter
) {
2863 strbuf_addstr(&buf
, "reflog ");
2864 get_reflog_message(&buf
, opt
->reflog_info
);
2865 strbuf_addch(&buf
, '\n');
2869 * We grep in the user's output encoding, under the assumption that it
2870 * is the encoding they are most likely to write their grep pattern
2871 * for. In addition, it means we will match the "notes" encoding below,
2872 * so we will not end up with a buffer that has two different encodings
2875 encoding
= get_log_output_encoding();
2876 message
= logmsg_reencode(commit
, NULL
, encoding
);
2878 /* Copy the commit to temporary if we are using "fake" headers */
2880 strbuf_addstr(&buf
, message
);
2882 if (opt
->grep_filter
.header_list
&& opt
->mailmap
) {
2884 strbuf_addstr(&buf
, message
);
2886 commit_rewrite_person(&buf
, "\nauthor ", opt
->mailmap
);
2887 commit_rewrite_person(&buf
, "\ncommitter ", opt
->mailmap
);
2890 /* Append "fake" message parts as needed */
2891 if (opt
->show_notes
) {
2893 strbuf_addstr(&buf
, message
);
2894 format_display_notes(commit
->object
.oid
.hash
, &buf
, encoding
, 1);
2898 * Find either in the original commit message, or in the temporary.
2899 * Note that we cast away the constness of "message" here. It is
2900 * const because it may come from the cached commit buffer. That's OK,
2901 * because we know that it is modifiable heap memory, and that while
2902 * grep_buffer may modify it for speed, it will restore any
2903 * changes before returning.
2906 retval
= grep_buffer(&opt
->grep_filter
, buf
.buf
, buf
.len
);
2908 retval
= grep_buffer(&opt
->grep_filter
,
2909 (char *)message
, strlen(message
));
2910 strbuf_release(&buf
);
2911 unuse_commit_buffer(commit
, message
);
2912 return opt
->invert_grep
? !retval
: retval
;
2915 static inline int want_ancestry(const struct rev_info
*revs
)
2917 return (revs
->rewrite_parents
|| revs
->children
.name
);
2920 enum commit_action
get_commit_action(struct rev_info
*revs
, struct commit
*commit
)
2922 if (commit
->object
.flags
& SHOWN
)
2923 return commit_ignore
;
2924 if (revs
->unpacked
&& has_sha1_pack(commit
->object
.oid
.hash
))
2925 return commit_ignore
;
2928 if (commit
->object
.flags
& UNINTERESTING
)
2929 return commit_ignore
;
2930 if (revs
->min_age
!= -1 && (commit
->date
> revs
->min_age
))
2931 return commit_ignore
;
2932 if (revs
->min_parents
|| (revs
->max_parents
>= 0)) {
2933 int n
= commit_list_count(commit
->parents
);
2934 if ((n
< revs
->min_parents
) ||
2935 ((revs
->max_parents
>= 0) && (n
> revs
->max_parents
)))
2936 return commit_ignore
;
2938 if (!commit_match(commit
, revs
))
2939 return commit_ignore
;
2940 if (revs
->prune
&& revs
->dense
) {
2941 /* Commit without changes? */
2942 if (commit
->object
.flags
& TREESAME
) {
2944 struct commit_list
*p
;
2945 /* drop merges unless we want parenthood */
2946 if (!want_ancestry(revs
))
2947 return commit_ignore
;
2949 * If we want ancestry, then need to keep any merges
2950 * between relevant commits to tie together topology.
2951 * For consistency with TREESAME and simplification
2952 * use "relevant" here rather than just INTERESTING,
2953 * to treat bottom commit(s) as part of the topology.
2955 for (n
= 0, p
= commit
->parents
; p
; p
= p
->next
)
2956 if (relevant_commit(p
->item
))
2959 return commit_ignore
;
2965 define_commit_slab(saved_parents
, struct commit_list
*);
2967 #define EMPTY_PARENT_LIST ((struct commit_list *)-1)
2970 * You may only call save_parents() once per commit (this is checked
2971 * for non-root commits).
2973 static void save_parents(struct rev_info
*revs
, struct commit
*commit
)
2975 struct commit_list
**pp
;
2977 if (!revs
->saved_parents_slab
) {
2978 revs
->saved_parents_slab
= xmalloc(sizeof(struct saved_parents
));
2979 init_saved_parents(revs
->saved_parents_slab
);
2982 pp
= saved_parents_at(revs
->saved_parents_slab
, commit
);
2985 * When walking with reflogs, we may visit the same commit
2986 * several times: once for each appearance in the reflog.
2988 * In this case, save_parents() will be called multiple times.
2989 * We want to keep only the first set of parents. We need to
2990 * store a sentinel value for an empty (i.e., NULL) parent
2991 * list to distinguish it from a not-yet-saved list, however.
2995 if (commit
->parents
)
2996 *pp
= copy_commit_list(commit
->parents
);
2998 *pp
= EMPTY_PARENT_LIST
;
3001 static void free_saved_parents(struct rev_info
*revs
)
3003 if (revs
->saved_parents_slab
)
3004 clear_saved_parents(revs
->saved_parents_slab
);
3007 struct commit_list
*get_saved_parents(struct rev_info
*revs
, const struct commit
*commit
)
3009 struct commit_list
*parents
;
3011 if (!revs
->saved_parents_slab
)
3012 return commit
->parents
;
3014 parents
= *saved_parents_at(revs
->saved_parents_slab
, commit
);
3015 if (parents
== EMPTY_PARENT_LIST
)
3020 enum commit_action
simplify_commit(struct rev_info
*revs
, struct commit
*commit
)
3022 enum commit_action action
= get_commit_action(revs
, commit
);
3024 if (action
== commit_show
&&
3026 revs
->prune
&& revs
->dense
&& want_ancestry(revs
)) {
3028 * --full-diff on simplified parents is no good: it
3029 * will show spurious changes from the commits that
3030 * were elided. So we save the parents on the side
3031 * when --full-diff is in effect.
3033 if (revs
->full_diff
)
3034 save_parents(revs
, commit
);
3035 if (rewrite_parents(revs
, commit
, rewrite_one
) < 0)
3036 return commit_error
;
3041 static void track_linear(struct rev_info
*revs
, struct commit
*commit
)
3043 if (revs
->track_first_time
) {
3045 revs
->track_first_time
= 0;
3047 struct commit_list
*p
;
3048 for (p
= revs
->previous_parents
; p
; p
= p
->next
)
3049 if (p
->item
== NULL
|| /* first commit */
3050 !oidcmp(&p
->item
->object
.oid
, &commit
->object
.oid
))
3052 revs
->linear
= p
!= NULL
;
3054 if (revs
->reverse
) {
3056 commit
->object
.flags
|= TRACK_LINEAR
;
3058 free_commit_list(revs
->previous_parents
);
3059 revs
->previous_parents
= copy_commit_list(commit
->parents
);
3062 static struct commit
*get_revision_1(struct rev_info
*revs
)
3068 struct commit
*commit
= pop_commit(&revs
->commits
);
3070 if (revs
->reflog_info
) {
3071 save_parents(revs
, commit
);
3072 fake_reflog_parent(revs
->reflog_info
, commit
);
3073 commit
->object
.flags
&= ~(ADDED
| SEEN
| SHOWN
);
3077 * If we haven't done the list limiting, we need to look at
3078 * the parents here. We also need to do the date-based limiting
3079 * that we'd otherwise have done in limit_list().
3081 if (!revs
->limited
) {
3082 if (revs
->max_age
!= -1 &&
3083 (commit
->date
< revs
->max_age
))
3085 if (add_parents_to_list(revs
, commit
, &revs
->commits
, NULL
) < 0) {
3086 if (!revs
->ignore_missing_links
)
3087 die("Failed to traverse parents of commit %s",
3088 oid_to_hex(&commit
->object
.oid
));
3092 switch (simplify_commit(revs
, commit
)) {
3096 die("Failed to simplify parents of commit %s",
3097 oid_to_hex(&commit
->object
.oid
));
3099 if (revs
->track_linear
)
3100 track_linear(revs
, commit
);
3103 } while (revs
->commits
);
3108 * Return true for entries that have not yet been shown. (This is an
3109 * object_array_each_func_t.)
3111 static int entry_unshown(struct object_array_entry
*entry
, void *cb_data_unused
)
3113 return !(entry
->item
->flags
& SHOWN
);
3117 * If array is on the verge of a realloc, garbage-collect any entries
3118 * that have already been shown to try to free up some space.
3120 static void gc_boundary(struct object_array
*array
)
3122 if (array
->nr
== array
->alloc
)
3123 object_array_filter(array
, entry_unshown
, NULL
);
3126 static void create_boundary_commit_list(struct rev_info
*revs
)
3130 struct object_array
*array
= &revs
->boundary_commits
;
3131 struct object_array_entry
*objects
= array
->objects
;
3134 * If revs->commits is non-NULL at this point, an error occurred in
3135 * get_revision_1(). Ignore the error and continue printing the
3136 * boundary commits anyway. (This is what the code has always
3139 if (revs
->commits
) {
3140 free_commit_list(revs
->commits
);
3141 revs
->commits
= NULL
;
3145 * Put all of the actual boundary commits from revs->boundary_commits
3146 * into revs->commits
3148 for (i
= 0; i
< array
->nr
; i
++) {
3149 c
= (struct commit
*)(objects
[i
].item
);
3152 if (!(c
->object
.flags
& CHILD_SHOWN
))
3154 if (c
->object
.flags
& (SHOWN
| BOUNDARY
))
3156 c
->object
.flags
|= BOUNDARY
;
3157 commit_list_insert(c
, &revs
->commits
);
3161 * If revs->topo_order is set, sort the boundary commits
3162 * in topological order
3164 sort_in_topological_order(&revs
->commits
, revs
->sort_order
);
3167 static struct commit
*get_revision_internal(struct rev_info
*revs
)
3169 struct commit
*c
= NULL
;
3170 struct commit_list
*l
;
3172 if (revs
->boundary
== 2) {
3174 * All of the normal commits have already been returned,
3175 * and we are now returning boundary commits.
3176 * create_boundary_commit_list() has populated
3177 * revs->commits with the remaining commits to return.
3179 c
= pop_commit(&revs
->commits
);
3181 c
->object
.flags
|= SHOWN
;
3186 * If our max_count counter has reached zero, then we are done. We
3187 * don't simply return NULL because we still might need to show
3188 * boundary commits. But we want to avoid calling get_revision_1, which
3189 * might do a considerable amount of work finding the next commit only
3190 * for us to throw it away.
3192 * If it is non-zero, then either we don't have a max_count at all
3193 * (-1), or it is still counting, in which case we decrement.
3195 if (revs
->max_count
) {
3196 c
= get_revision_1(revs
);
3198 while (revs
->skip_count
> 0) {
3200 c
= get_revision_1(revs
);
3206 if (revs
->max_count
> 0)
3211 c
->object
.flags
|= SHOWN
;
3213 if (!revs
->boundary
)
3218 * get_revision_1() runs out the commits, and
3219 * we are done computing the boundaries.
3220 * switch to boundary commits output mode.
3225 * Update revs->commits to contain the list of
3228 create_boundary_commit_list(revs
);
3230 return get_revision_internal(revs
);
3234 * boundary commits are the commits that are parents of the
3235 * ones we got from get_revision_1() but they themselves are
3236 * not returned from get_revision_1(). Before returning
3237 * 'c', we need to mark its parents that they could be boundaries.
3240 for (l
= c
->parents
; l
; l
= l
->next
) {
3242 p
= &(l
->item
->object
);
3243 if (p
->flags
& (CHILD_SHOWN
| SHOWN
))
3245 p
->flags
|= CHILD_SHOWN
;
3246 gc_boundary(&revs
->boundary_commits
);
3247 add_object_array(p
, NULL
, &revs
->boundary_commits
);
3253 struct commit
*get_revision(struct rev_info
*revs
)
3256 struct commit_list
*reversed
;
3258 if (revs
->reverse
) {
3260 while ((c
= get_revision_internal(revs
)))
3261 commit_list_insert(c
, &reversed
);
3262 revs
->commits
= reversed
;
3264 revs
->reverse_output_stage
= 1;
3267 if (revs
->reverse_output_stage
) {
3268 c
= pop_commit(&revs
->commits
);
3269 if (revs
->track_linear
)
3270 revs
->linear
= !!(c
&& c
->object
.flags
& TRACK_LINEAR
);
3274 c
= get_revision_internal(revs
);
3275 if (c
&& revs
->graph
)
3276 graph_update(revs
->graph
, c
);
3278 free_saved_parents(revs
);
3279 if (revs
->previous_parents
) {
3280 free_commit_list(revs
->previous_parents
);
3281 revs
->previous_parents
= NULL
;
3287 char *get_revision_mark(const struct rev_info
*revs
, const struct commit
*commit
)
3289 if (commit
->object
.flags
& BOUNDARY
)
3291 else if (commit
->object
.flags
& UNINTERESTING
)
3293 else if (commit
->object
.flags
& PATCHSAME
)
3295 else if (!revs
|| revs
->left_right
) {
3296 if (commit
->object
.flags
& SYMMETRIC_LEFT
)
3300 } else if (revs
->graph
)
3302 else if (revs
->cherry_mark
)
3307 void put_revision_mark(const struct rev_info
*revs
, const struct commit
*commit
)
3309 char *mark
= get_revision_mark(revs
, commit
);
3312 fputs(mark
, stdout
);