11 #include "reflog-walk.h"
12 #include "patch-ids.h"
15 #include "string-list.h"
18 volatile show_early_output_fn_t show_early_output
;
20 char *path_name(const struct name_path
*path
, const char *name
)
22 const struct name_path
*p
;
24 int nlen
= strlen(name
);
27 for (p
= path
; p
; p
= p
->up
) {
29 len
+= p
->elem_len
+ 1;
32 m
= n
+ len
- (nlen
+ 1);
34 for (p
= path
; p
; p
= p
->up
) {
37 memcpy(m
, p
->elem
, p
->elem_len
);
44 static int show_path_component_truncated(FILE *out
, const char *name
, int len
)
47 for (cnt
= 0; cnt
< len
; cnt
++) {
49 if (!ch
|| ch
== '\n')
56 static int show_path_truncated(FILE *out
, const struct name_path
*path
)
62 emitted
= show_path_truncated(out
, path
->up
);
67 ours
= show_path_component_truncated(out
, path
->elem
, path
->elem_len
);
70 return ours
|| emitted
;
73 void show_object_with_name(FILE *out
, struct object
*obj
, const struct name_path
*path
, const char *component
)
75 struct name_path leaf
;
76 leaf
.up
= (struct name_path
*)path
;
77 leaf
.elem
= component
;
78 leaf
.elem_len
= strlen(component
);
80 fprintf(out
, "%s ", sha1_to_hex(obj
->sha1
));
81 show_path_truncated(out
, &leaf
);
85 void add_object(struct object
*obj
,
86 struct object_array
*p
,
87 struct name_path
*path
,
90 add_object_array(obj
, path_name(path
, name
), p
);
93 static void mark_blob_uninteresting(struct blob
*blob
)
97 if (blob
->object
.flags
& UNINTERESTING
)
99 blob
->object
.flags
|= UNINTERESTING
;
102 void mark_tree_uninteresting(struct tree
*tree
)
104 struct tree_desc desc
;
105 struct name_entry entry
;
106 struct object
*obj
= &tree
->object
;
110 if (obj
->flags
& UNINTERESTING
)
112 obj
->flags
|= UNINTERESTING
;
113 if (!has_sha1_file(obj
->sha1
))
115 if (parse_tree(tree
) < 0)
116 die("bad tree %s", sha1_to_hex(obj
->sha1
));
118 init_tree_desc(&desc
, tree
->buffer
, tree
->size
);
119 while (tree_entry(&desc
, &entry
)) {
120 switch (object_type(entry
.mode
)) {
122 mark_tree_uninteresting(lookup_tree(entry
.sha1
));
125 mark_blob_uninteresting(lookup_blob(entry
.sha1
));
128 /* Subproject commit - not in this repository */
134 * We don't care about the tree any more
135 * after it has been marked uninteresting.
141 void mark_parents_uninteresting(struct commit
*commit
)
143 struct commit_list
*parents
= NULL
, *l
;
145 for (l
= commit
->parents
; l
; l
= l
->next
)
146 commit_list_insert(l
->item
, &parents
);
149 struct commit
*commit
= parents
->item
;
151 parents
= parents
->next
;
156 * A missing commit is ok iff its parent is marked
159 * We just mark such a thing parsed, so that when
160 * it is popped next time around, we won't be trying
161 * to parse it and get an error.
163 if (!has_sha1_file(commit
->object
.sha1
))
164 commit
->object
.parsed
= 1;
166 if (commit
->object
.flags
& UNINTERESTING
)
169 commit
->object
.flags
|= UNINTERESTING
;
172 * Normally we haven't parsed the parent
173 * yet, so we won't have a parent of a parent
174 * here. However, it may turn out that we've
175 * reached this commit some other way (where it
176 * wasn't uninteresting), in which case we need
177 * to mark its parents recursively too..
179 if (!commit
->parents
)
182 for (l
= commit
->parents
->next
; l
; l
= l
->next
)
183 commit_list_insert(l
->item
, &parents
);
184 commit
= commit
->parents
->item
;
189 static void add_pending_object_with_mode(struct rev_info
*revs
, struct object
*obj
, const char *name
, unsigned mode
)
193 if (revs
->no_walk
&& (obj
->flags
& UNINTERESTING
))
195 if (revs
->reflog_info
&& obj
->type
== OBJ_COMMIT
) {
196 struct strbuf buf
= STRBUF_INIT
;
197 int len
= interpret_branch_name(name
, &buf
);
200 if (0 < len
&& name
[len
] && buf
.len
)
201 strbuf_addstr(&buf
, name
+ len
);
202 st
= add_reflog_for_walk(revs
->reflog_info
,
203 (struct commit
*)obj
,
204 buf
.buf
[0] ? buf
.buf
: name
);
205 strbuf_release(&buf
);
209 add_object_array_with_mode(obj
, name
, &revs
->pending
, mode
);
212 void add_pending_object(struct rev_info
*revs
, struct object
*obj
, const char *name
)
214 add_pending_object_with_mode(revs
, obj
, name
, S_IFINVALID
);
217 void add_head_to_pending(struct rev_info
*revs
)
219 unsigned char sha1
[20];
221 if (get_sha1("HEAD", sha1
))
223 obj
= parse_object(sha1
);
226 add_pending_object(revs
, obj
, "HEAD");
229 static struct object
*get_reference(struct rev_info
*revs
, const char *name
, const unsigned char *sha1
, unsigned int flags
)
231 struct object
*object
;
233 object
= parse_object(sha1
);
235 if (revs
->ignore_missing
)
237 die("bad object %s", name
);
239 object
->flags
|= flags
;
243 void add_pending_sha1(struct rev_info
*revs
, const char *name
,
244 const unsigned char *sha1
, unsigned int flags
)
246 struct object
*object
= get_reference(revs
, name
, sha1
, flags
);
247 add_pending_object(revs
, object
, name
);
250 static struct commit
*handle_commit(struct rev_info
*revs
, struct object
*object
, const char *name
)
252 unsigned long flags
= object
->flags
;
255 * Tag object? Look what it points to..
257 while (object
->type
== OBJ_TAG
) {
258 struct tag
*tag
= (struct tag
*) object
;
259 if (revs
->tag_objects
&& !(flags
& UNINTERESTING
))
260 add_pending_object(revs
, object
, tag
->tag
);
263 object
= parse_object(tag
->tagged
->sha1
);
265 if (flags
& UNINTERESTING
)
267 die("bad object %s", sha1_to_hex(tag
->tagged
->sha1
));
272 * Commit object? Just return it, we'll do all the complex
275 if (object
->type
== OBJ_COMMIT
) {
276 struct commit
*commit
= (struct commit
*)object
;
277 if (parse_commit(commit
) < 0)
278 die("unable to parse commit %s", name
);
279 if (flags
& UNINTERESTING
) {
280 commit
->object
.flags
|= UNINTERESTING
;
281 mark_parents_uninteresting(commit
);
284 if (revs
->show_source
&& !commit
->util
)
285 commit
->util
= (void *) name
;
290 * Tree object? Either mark it uninteresting, or add it
291 * to the list of objects to look at later..
293 if (object
->type
== OBJ_TREE
) {
294 struct tree
*tree
= (struct tree
*)object
;
295 if (!revs
->tree_objects
)
297 if (flags
& UNINTERESTING
) {
298 mark_tree_uninteresting(tree
);
301 add_pending_object(revs
, object
, "");
306 * Blob object? You know the drill by now..
308 if (object
->type
== OBJ_BLOB
) {
309 struct blob
*blob
= (struct blob
*)object
;
310 if (!revs
->blob_objects
)
312 if (flags
& UNINTERESTING
) {
313 mark_blob_uninteresting(blob
);
316 add_pending_object(revs
, object
, "");
319 die("%s is unknown object", name
);
322 static int everybody_uninteresting(struct commit_list
*orig
)
324 struct commit_list
*list
= orig
;
326 struct commit
*commit
= list
->item
;
328 if (commit
->object
.flags
& UNINTERESTING
)
336 * A definition of "relevant" commit that we can use to simplify limited graphs
337 * by eliminating side branches.
339 * A "relevant" commit is one that is !UNINTERESTING (ie we are including it
340 * in our list), or that is a specified BOTTOM commit. Then after computing
341 * a limited list, during processing we can generally ignore boundary merges
342 * coming from outside the graph, (ie from irrelevant parents), and treat
343 * those merges as if they were single-parent. TREESAME is defined to consider
344 * only relevant parents, if any. If we are TREESAME to our on-graph parents,
345 * we don't care if we were !TREESAME to non-graph parents.
347 * Treating bottom commits as relevant ensures that a limited graph's
348 * connection to the actual bottom commit is not viewed as a side branch, but
349 * treated as part of the graph. For example:
351 * ....Z...A---X---o---o---B
355 * When computing "A..B", the A-X connection is at least as important as
356 * Y-X, despite A being flagged UNINTERESTING.
358 * And when computing --ancestry-path "A..B", the A-X connection is more
359 * important than Y-X, despite both A and Y being flagged UNINTERESTING.
361 static inline int relevant_commit(struct commit
*commit
)
363 return (commit
->object
.flags
& (UNINTERESTING
| BOTTOM
)) != UNINTERESTING
;
367 * Return a single relevant commit from a parent list. If we are a TREESAME
368 * commit, and this selects one of our parents, then we can safely simplify to
371 static struct commit
*one_relevant_parent(const struct rev_info
*revs
,
372 struct commit_list
*orig
)
374 struct commit_list
*list
= orig
;
375 struct commit
*relevant
= NULL
;
381 * For 1-parent commits, or if first-parent-only, then return that
382 * first parent (even if not "relevant" by the above definition).
383 * TREESAME will have been set purely on that parent.
385 if (revs
->first_parent_only
|| !orig
->next
)
389 * For multi-parent commits, identify a sole relevant parent, if any.
390 * If we have only one relevant parent, then TREESAME will be set purely
391 * with regard to that parent, and we can simplify accordingly.
393 * If we have more than one relevant parent, or no relevant parents
394 * (and multiple irrelevant ones), then we can't select a parent here
398 struct commit
*commit
= list
->item
;
400 if (relevant_commit(commit
)) {
410 * The goal is to get REV_TREE_NEW as the result only if the
411 * diff consists of all '+' (and no other changes), REV_TREE_OLD
412 * if the whole diff is removal of old data, and otherwise
413 * REV_TREE_DIFFERENT (of course if the trees are the same we
414 * want REV_TREE_SAME).
415 * That means that once we get to REV_TREE_DIFFERENT, we do not
416 * have to look any further.
418 static int tree_difference
= REV_TREE_SAME
;
420 static void file_add_remove(struct diff_options
*options
,
421 int addremove
, unsigned mode
,
422 const unsigned char *sha1
,
424 const char *fullpath
, unsigned dirty_submodule
)
426 int diff
= addremove
== '+' ? REV_TREE_NEW
: REV_TREE_OLD
;
428 tree_difference
|= diff
;
429 if (tree_difference
== REV_TREE_DIFFERENT
)
430 DIFF_OPT_SET(options
, HAS_CHANGES
);
433 static void file_change(struct diff_options
*options
,
434 unsigned old_mode
, unsigned new_mode
,
435 const unsigned char *old_sha1
,
436 const unsigned char *new_sha1
,
437 int old_sha1_valid
, int new_sha1_valid
,
438 const char *fullpath
,
439 unsigned old_dirty_submodule
, unsigned new_dirty_submodule
)
441 tree_difference
= REV_TREE_DIFFERENT
;
442 DIFF_OPT_SET(options
, HAS_CHANGES
);
445 static int rev_compare_tree(struct rev_info
*revs
, struct commit
*parent
, struct commit
*commit
)
447 struct tree
*t1
= parent
->tree
;
448 struct tree
*t2
= commit
->tree
;
455 if (revs
->simplify_by_decoration
) {
457 * If we are simplifying by decoration, then the commit
458 * is worth showing if it has a tag pointing at it.
460 if (lookup_decoration(&name_decoration
, &commit
->object
))
461 return REV_TREE_DIFFERENT
;
463 * A commit that is not pointed by a tag is uninteresting
464 * if we are not limited by path. This means that you will
465 * see the usual "commits that touch the paths" plus any
466 * tagged commit by specifying both --simplify-by-decoration
469 if (!revs
->prune_data
.nr
)
470 return REV_TREE_SAME
;
473 tree_difference
= REV_TREE_SAME
;
474 DIFF_OPT_CLR(&revs
->pruning
, HAS_CHANGES
);
475 if (diff_tree_sha1(t1
->object
.sha1
, t2
->object
.sha1
, "",
477 return REV_TREE_DIFFERENT
;
478 return tree_difference
;
481 static int rev_same_tree_as_empty(struct rev_info
*revs
, struct commit
*commit
)
486 struct tree_desc empty
, real
;
487 struct tree
*t1
= commit
->tree
;
492 tree
= read_object_with_reference(t1
->object
.sha1
, tree_type
, &size
, NULL
);
495 init_tree_desc(&real
, tree
, size
);
496 init_tree_desc(&empty
, "", 0);
498 tree_difference
= REV_TREE_SAME
;
499 DIFF_OPT_CLR(&revs
->pruning
, HAS_CHANGES
);
500 retval
= diff_tree(&empty
, &real
, "", &revs
->pruning
);
503 return retval
>= 0 && (tree_difference
== REV_TREE_SAME
);
506 struct treesame_state
{
507 unsigned int nparents
;
508 unsigned char treesame
[FLEX_ARRAY
];
511 static struct treesame_state
*initialise_treesame(struct rev_info
*revs
, struct commit
*commit
)
513 unsigned n
= commit_list_count(commit
->parents
);
514 struct treesame_state
*st
= xcalloc(1, sizeof(*st
) + n
);
516 add_decoration(&revs
->treesame
, &commit
->object
, st
);
521 * Must be called immediately after removing the nth_parent from a commit's
522 * parent list, if we are maintaining the per-parent treesame[] decoration.
523 * This does not recalculate the master TREESAME flag - update_treesame()
524 * should be called to update it after a sequence of treesame[] modifications
525 * that may have affected it.
527 static int compact_treesame(struct rev_info
*revs
, struct commit
*commit
, unsigned nth_parent
)
529 struct treesame_state
*st
;
532 if (!commit
->parents
) {
534 * Have just removed the only parent from a non-merge.
535 * Different handling, as we lack decoration.
538 die("compact_treesame %u", nth_parent
);
539 old_same
= !!(commit
->object
.flags
& TREESAME
);
540 if (rev_same_tree_as_empty(revs
, commit
))
541 commit
->object
.flags
|= TREESAME
;
543 commit
->object
.flags
&= ~TREESAME
;
547 st
= lookup_decoration(&revs
->treesame
, &commit
->object
);
548 if (!st
|| nth_parent
>= st
->nparents
)
549 die("compact_treesame %u", nth_parent
);
551 old_same
= st
->treesame
[nth_parent
];
552 memmove(st
->treesame
+ nth_parent
,
553 st
->treesame
+ nth_parent
+ 1,
554 st
->nparents
- nth_parent
- 1);
557 * If we've just become a non-merge commit, update TREESAME
558 * immediately, and remove the no-longer-needed decoration.
559 * If still a merge, defer update until update_treesame().
561 if (--st
->nparents
== 1) {
562 if (commit
->parents
->next
)
563 die("compact_treesame parents mismatch");
564 if (st
->treesame
[0] && revs
->dense
)
565 commit
->object
.flags
|= TREESAME
;
567 commit
->object
.flags
&= ~TREESAME
;
568 free(add_decoration(&revs
->treesame
, &commit
->object
, NULL
));
574 static unsigned update_treesame(struct rev_info
*revs
, struct commit
*commit
)
576 if (commit
->parents
&& commit
->parents
->next
) {
578 struct treesame_state
*st
;
579 struct commit_list
*p
;
580 unsigned relevant_parents
;
581 unsigned relevant_change
, irrelevant_change
;
583 st
= lookup_decoration(&revs
->treesame
, &commit
->object
);
585 die("update_treesame %s", sha1_to_hex(commit
->object
.sha1
));
586 relevant_parents
= 0;
587 relevant_change
= irrelevant_change
= 0;
588 for (p
= commit
->parents
, n
= 0; p
; n
++, p
= p
->next
) {
589 if (relevant_commit(p
->item
)) {
590 relevant_change
|= !st
->treesame
[n
];
593 irrelevant_change
|= !st
->treesame
[n
];
595 if (relevant_parents
? relevant_change
: irrelevant_change
)
596 commit
->object
.flags
&= ~TREESAME
;
598 commit
->object
.flags
|= TREESAME
;
601 return commit
->object
.flags
& TREESAME
;
604 static inline int limiting_can_increase_treesame(const struct rev_info
*revs
)
607 * TREESAME is irrelevant unless prune && dense;
608 * if simplify_history is set, we can't have a mixture of TREESAME and
609 * !TREESAME INTERESTING parents (and we don't have treesame[]
610 * decoration anyway);
611 * if first_parent_only is set, then the TREESAME flag is locked
612 * against the first parent (and again we lack treesame[] decoration).
614 return revs
->prune
&& revs
->dense
&&
615 !revs
->simplify_history
&&
616 !revs
->first_parent_only
;
619 static void try_to_simplify_commit(struct rev_info
*revs
, struct commit
*commit
)
621 struct commit_list
**pp
, *parent
;
622 struct treesame_state
*ts
= NULL
;
623 int relevant_change
= 0, irrelevant_change
= 0;
624 int relevant_parents
, nth_parent
;
627 * If we don't do pruning, everything is interesting
635 if (!commit
->parents
) {
636 if (rev_same_tree_as_empty(revs
, commit
))
637 commit
->object
.flags
|= TREESAME
;
642 * Normal non-merge commit? If we don't want to make the
643 * history dense, we consider it always to be a change..
645 if (!revs
->dense
&& !commit
->parents
->next
)
648 for (pp
= &commit
->parents
, nth_parent
= 0, relevant_parents
= 0;
649 (parent
= *pp
) != NULL
;
650 pp
= &parent
->next
, nth_parent
++) {
651 struct commit
*p
= parent
->item
;
652 if (relevant_commit(p
))
655 if (nth_parent
== 1) {
657 * This our second loop iteration - so we now know
658 * we're dealing with a merge.
660 * Do not compare with later parents when we care only about
661 * the first parent chain, in order to avoid derailing the
662 * traversal to follow a side branch that brought everything
663 * in the path we are limited to by the pathspec.
665 if (revs
->first_parent_only
)
668 * If this will remain a potentially-simplifiable
669 * merge, remember per-parent treesame if needed.
670 * Initialise the array with the comparison from our
673 if (revs
->treesame
.name
&&
674 !revs
->simplify_history
&&
675 !(commit
->object
.flags
& UNINTERESTING
)) {
676 ts
= initialise_treesame(revs
, commit
);
677 if (!(irrelevant_change
|| relevant_change
))
681 if (parse_commit(p
) < 0)
682 die("cannot simplify commit %s (because of %s)",
683 sha1_to_hex(commit
->object
.sha1
),
684 sha1_to_hex(p
->object
.sha1
));
685 switch (rev_compare_tree(revs
, p
, commit
)) {
687 if (!revs
->simplify_history
|| (p
->object
.flags
& UNINTERESTING
)) {
688 /* Even if a merge with an uninteresting
689 * side branch brought the entire change
690 * we are interested in, we do not want
691 * to lose the other branches of this
692 * merge, so we just keep going.
695 ts
->treesame
[nth_parent
] = 1;
699 commit
->parents
= parent
;
700 commit
->object
.flags
|= TREESAME
;
704 if (revs
->remove_empty_trees
&&
705 rev_same_tree_as_empty(revs
, p
)) {
706 /* We are adding all the specified
707 * paths from this parent, so the
708 * history beyond this parent is not
709 * interesting. Remove its parents
710 * (they are grandparents for us).
711 * IOW, we pretend this parent is a
714 if (parse_commit(p
) < 0)
715 die("cannot simplify commit %s (invalid %s)",
716 sha1_to_hex(commit
->object
.sha1
),
717 sha1_to_hex(p
->object
.sha1
));
722 case REV_TREE_DIFFERENT
:
723 if (relevant_commit(p
))
726 irrelevant_change
= 1;
729 die("bad tree compare for commit %s", sha1_to_hex(commit
->object
.sha1
));
733 * TREESAME is straightforward for single-parent commits. For merge
734 * commits, it is most useful to define it so that "irrelevant"
735 * parents cannot make us !TREESAME - if we have any relevant
736 * parents, then we only consider TREESAMEness with respect to them,
737 * allowing irrelevant merges from uninteresting branches to be
738 * simplified away. Only if we have only irrelevant parents do we
739 * base TREESAME on them. Note that this logic is replicated in
740 * update_treesame, which should be kept in sync.
742 if (relevant_parents
? !relevant_change
: !irrelevant_change
)
743 commit
->object
.flags
|= TREESAME
;
746 static void commit_list_insert_by_date_cached(struct commit
*p
, struct commit_list
**head
,
747 struct commit_list
*cached_base
, struct commit_list
**cache
)
749 struct commit_list
*new_entry
;
751 if (cached_base
&& p
->date
< cached_base
->item
->date
)
752 new_entry
= commit_list_insert_by_date(p
, &cached_base
->next
);
754 new_entry
= commit_list_insert_by_date(p
, head
);
756 if (cache
&& (!*cache
|| p
->date
< (*cache
)->item
->date
))
760 static int add_parents_to_list(struct rev_info
*revs
, struct commit
*commit
,
761 struct commit_list
**list
, struct commit_list
**cache_ptr
)
763 struct commit_list
*parent
= commit
->parents
;
765 struct commit_list
*cached_base
= cache_ptr
? *cache_ptr
: NULL
;
767 if (commit
->object
.flags
& ADDED
)
769 commit
->object
.flags
|= ADDED
;
772 * If the commit is uninteresting, don't try to
773 * prune parents - we want the maximal uninteresting
776 * Normally we haven't parsed the parent
777 * yet, so we won't have a parent of a parent
778 * here. However, it may turn out that we've
779 * reached this commit some other way (where it
780 * wasn't uninteresting), in which case we need
781 * to mark its parents recursively too..
783 if (commit
->object
.flags
& UNINTERESTING
) {
785 struct commit
*p
= parent
->item
;
786 parent
= parent
->next
;
788 p
->object
.flags
|= UNINTERESTING
;
789 if (parse_commit(p
) < 0)
792 mark_parents_uninteresting(p
);
793 if (p
->object
.flags
& SEEN
)
795 p
->object
.flags
|= SEEN
;
796 commit_list_insert_by_date_cached(p
, list
, cached_base
, cache_ptr
);
802 * Ok, the commit wasn't uninteresting. Try to
803 * simplify the commit history and find the parent
804 * that has no differences in the path set if one exists.
806 try_to_simplify_commit(revs
, commit
);
811 left_flag
= (commit
->object
.flags
& SYMMETRIC_LEFT
);
813 for (parent
= commit
->parents
; parent
; parent
= parent
->next
) {
814 struct commit
*p
= parent
->item
;
816 if (parse_commit(p
) < 0)
818 if (revs
->show_source
&& !p
->util
)
819 p
->util
= commit
->util
;
820 p
->object
.flags
|= left_flag
;
821 if (!(p
->object
.flags
& SEEN
)) {
822 p
->object
.flags
|= SEEN
;
823 commit_list_insert_by_date_cached(p
, list
, cached_base
, cache_ptr
);
825 if (revs
->first_parent_only
)
831 static void cherry_pick_list(struct commit_list
*list
, struct rev_info
*revs
)
833 struct commit_list
*p
;
834 int left_count
= 0, right_count
= 0;
836 struct patch_ids ids
;
837 unsigned cherry_flag
;
839 /* First count the commits on the left and on the right */
840 for (p
= list
; p
; p
= p
->next
) {
841 struct commit
*commit
= p
->item
;
842 unsigned flags
= commit
->object
.flags
;
843 if (flags
& BOUNDARY
)
845 else if (flags
& SYMMETRIC_LEFT
)
851 if (!left_count
|| !right_count
)
854 left_first
= left_count
< right_count
;
855 init_patch_ids(&ids
);
856 ids
.diffopts
.pathspec
= revs
->diffopt
.pathspec
;
858 /* Compute patch-ids for one side */
859 for (p
= list
; p
; p
= p
->next
) {
860 struct commit
*commit
= p
->item
;
861 unsigned flags
= commit
->object
.flags
;
863 if (flags
& BOUNDARY
)
866 * If we have fewer left, left_first is set and we omit
867 * commits on the right branch in this loop. If we have
868 * fewer right, we skip the left ones.
870 if (left_first
!= !!(flags
& SYMMETRIC_LEFT
))
872 commit
->util
= add_commit_patch_id(commit
, &ids
);
875 /* either cherry_mark or cherry_pick are true */
876 cherry_flag
= revs
->cherry_mark
? PATCHSAME
: SHOWN
;
878 /* Check the other side */
879 for (p
= list
; p
; p
= p
->next
) {
880 struct commit
*commit
= p
->item
;
882 unsigned flags
= commit
->object
.flags
;
884 if (flags
& BOUNDARY
)
887 * If we have fewer left, left_first is set and we omit
888 * commits on the left branch in this loop.
890 if (left_first
== !!(flags
& SYMMETRIC_LEFT
))
894 * Have we seen the same patch id?
896 id
= has_commit_patch_id(commit
, &ids
);
900 commit
->object
.flags
|= cherry_flag
;
903 /* Now check the original side for seen ones */
904 for (p
= list
; p
; p
= p
->next
) {
905 struct commit
*commit
= p
->item
;
906 struct patch_id
*ent
;
912 commit
->object
.flags
|= cherry_flag
;
916 free_patch_ids(&ids
);
919 /* How many extra uninteresting commits we want to see.. */
922 static int still_interesting(struct commit_list
*src
, unsigned long date
, int slop
)
925 * No source list at all? We're definitely done..
931 * Does the destination list contain entries with a date
932 * before the source list? Definitely _not_ done.
934 if (date
<= src
->item
->date
)
938 * Does the source list still have interesting commits in
939 * it? Definitely not done..
941 if (!everybody_uninteresting(src
))
944 /* Ok, we're closing in.. */
949 * "rev-list --ancestry-path A..B" computes commits that are ancestors
950 * of B but not ancestors of A but further limits the result to those
951 * that are descendants of A. This takes the list of bottom commits and
952 * the result of "A..B" without --ancestry-path, and limits the latter
953 * further to the ones that can reach one of the commits in "bottom".
955 static void limit_to_ancestry(struct commit_list
*bottom
, struct commit_list
*list
)
957 struct commit_list
*p
;
958 struct commit_list
*rlist
= NULL
;
962 * Reverse the list so that it will be likely that we would
963 * process parents before children.
965 for (p
= list
; p
; p
= p
->next
)
966 commit_list_insert(p
->item
, &rlist
);
968 for (p
= bottom
; p
; p
= p
->next
)
969 p
->item
->object
.flags
|= TMP_MARK
;
972 * Mark the ones that can reach bottom commits in "list",
973 * in a bottom-up fashion.
977 for (p
= rlist
; p
; p
= p
->next
) {
978 struct commit
*c
= p
->item
;
979 struct commit_list
*parents
;
980 if (c
->object
.flags
& (TMP_MARK
| UNINTERESTING
))
982 for (parents
= c
->parents
;
984 parents
= parents
->next
) {
985 if (!(parents
->item
->object
.flags
& TMP_MARK
))
987 c
->object
.flags
|= TMP_MARK
;
992 } while (made_progress
);
995 * NEEDSWORK: decide if we want to remove parents that are
996 * not marked with TMP_MARK from commit->parents for commits
997 * in the resulting list. We may not want to do that, though.
1001 * The ones that are not marked with TMP_MARK are uninteresting
1003 for (p
= list
; p
; p
= p
->next
) {
1004 struct commit
*c
= p
->item
;
1005 if (c
->object
.flags
& TMP_MARK
)
1007 c
->object
.flags
|= UNINTERESTING
;
1010 /* We are done with the TMP_MARK */
1011 for (p
= list
; p
; p
= p
->next
)
1012 p
->item
->object
.flags
&= ~TMP_MARK
;
1013 for (p
= bottom
; p
; p
= p
->next
)
1014 p
->item
->object
.flags
&= ~TMP_MARK
;
1015 free_commit_list(rlist
);
1019 * Before walking the history, keep the set of "negative" refs the
1020 * caller has asked to exclude.
1022 * This is used to compute "rev-list --ancestry-path A..B", as we need
1023 * to filter the result of "A..B" further to the ones that can actually
1026 static struct commit_list
*collect_bottom_commits(struct commit_list
*list
)
1028 struct commit_list
*elem
, *bottom
= NULL
;
1029 for (elem
= list
; elem
; elem
= elem
->next
)
1030 if (elem
->item
->object
.flags
& BOTTOM
)
1031 commit_list_insert(elem
->item
, &bottom
);
1035 /* Assumes either left_only or right_only is set */
1036 static void limit_left_right(struct commit_list
*list
, struct rev_info
*revs
)
1038 struct commit_list
*p
;
1040 for (p
= list
; p
; p
= p
->next
) {
1041 struct commit
*commit
= p
->item
;
1043 if (revs
->right_only
) {
1044 if (commit
->object
.flags
& SYMMETRIC_LEFT
)
1045 commit
->object
.flags
|= SHOWN
;
1046 } else /* revs->left_only is set */
1047 if (!(commit
->object
.flags
& SYMMETRIC_LEFT
))
1048 commit
->object
.flags
|= SHOWN
;
1052 static int limit_list(struct rev_info
*revs
)
1055 unsigned long date
= ~0ul;
1056 struct commit_list
*list
= revs
->commits
;
1057 struct commit_list
*newlist
= NULL
;
1058 struct commit_list
**p
= &newlist
;
1059 struct commit_list
*bottom
= NULL
;
1061 if (revs
->ancestry_path
) {
1062 bottom
= collect_bottom_commits(list
);
1064 die("--ancestry-path given but there are no bottom commits");
1068 struct commit_list
*entry
= list
;
1069 struct commit
*commit
= list
->item
;
1070 struct object
*obj
= &commit
->object
;
1071 show_early_output_fn_t show
;
1076 if (revs
->max_age
!= -1 && (commit
->date
< revs
->max_age
))
1077 obj
->flags
|= UNINTERESTING
;
1078 if (add_parents_to_list(revs
, commit
, &list
, NULL
) < 0)
1080 if (obj
->flags
& UNINTERESTING
) {
1081 mark_parents_uninteresting(commit
);
1083 p
= &commit_list_insert(commit
, p
)->next
;
1084 slop
= still_interesting(list
, date
, slop
);
1087 /* If showing all, add the whole pending list to the end */
1092 if (revs
->min_age
!= -1 && (commit
->date
> revs
->min_age
))
1094 date
= commit
->date
;
1095 p
= &commit_list_insert(commit
, p
)->next
;
1097 show
= show_early_output
;
1101 show(revs
, newlist
);
1102 show_early_output
= NULL
;
1104 if (revs
->cherry_pick
|| revs
->cherry_mark
)
1105 cherry_pick_list(newlist
, revs
);
1107 if (revs
->left_only
|| revs
->right_only
)
1108 limit_left_right(newlist
, revs
);
1111 limit_to_ancestry(bottom
, newlist
);
1112 free_commit_list(bottom
);
1116 * Check if any commits have become TREESAME by some of their parents
1117 * becoming UNINTERESTING.
1119 if (limiting_can_increase_treesame(revs
))
1120 for (list
= newlist
; list
; list
= list
->next
) {
1121 struct commit
*c
= list
->item
;
1122 if (c
->object
.flags
& (UNINTERESTING
| TREESAME
))
1124 update_treesame(revs
, c
);
1127 revs
->commits
= newlist
;
1131 static void add_rev_cmdline(struct rev_info
*revs
,
1132 struct object
*item
,
1137 struct rev_cmdline_info
*info
= &revs
->cmdline
;
1140 ALLOC_GROW(info
->rev
, nr
+ 1, info
->alloc
);
1141 info
->rev
[nr
].item
= item
;
1142 info
->rev
[nr
].name
= name
;
1143 info
->rev
[nr
].whence
= whence
;
1144 info
->rev
[nr
].flags
= flags
;
1148 static void add_rev_cmdline_list(struct rev_info
*revs
,
1149 struct commit_list
*commit_list
,
1153 while (commit_list
) {
1154 struct object
*object
= &commit_list
->item
->object
;
1155 add_rev_cmdline(revs
, object
, sha1_to_hex(object
->sha1
),
1157 commit_list
= commit_list
->next
;
1161 struct all_refs_cb
{
1163 int warned_bad_reflog
;
1164 struct rev_info
*all_revs
;
1165 const char *name_for_errormsg
;
1168 static int handle_one_ref(const char *path
, const unsigned char *sha1
, int flag
, void *cb_data
)
1170 struct all_refs_cb
*cb
= cb_data
;
1171 struct object
*object
= get_reference(cb
->all_revs
, path
, sha1
,
1173 add_rev_cmdline(cb
->all_revs
, object
, path
, REV_CMD_REF
, cb
->all_flags
);
1174 add_pending_sha1(cb
->all_revs
, path
, sha1
, cb
->all_flags
);
1178 static void init_all_refs_cb(struct all_refs_cb
*cb
, struct rev_info
*revs
,
1181 cb
->all_revs
= revs
;
1182 cb
->all_flags
= flags
;
1185 static void handle_refs(const char *submodule
, struct rev_info
*revs
, unsigned flags
,
1186 int (*for_each
)(const char *, each_ref_fn
, void *))
1188 struct all_refs_cb cb
;
1189 init_all_refs_cb(&cb
, revs
, flags
);
1190 for_each(submodule
, handle_one_ref
, &cb
);
1193 static void handle_one_reflog_commit(unsigned char *sha1
, void *cb_data
)
1195 struct all_refs_cb
*cb
= cb_data
;
1196 if (!is_null_sha1(sha1
)) {
1197 struct object
*o
= parse_object(sha1
);
1199 o
->flags
|= cb
->all_flags
;
1200 /* ??? CMDLINEFLAGS ??? */
1201 add_pending_object(cb
->all_revs
, o
, "");
1203 else if (!cb
->warned_bad_reflog
) {
1204 warning("reflog of '%s' references pruned commits",
1205 cb
->name_for_errormsg
);
1206 cb
->warned_bad_reflog
= 1;
1211 static int handle_one_reflog_ent(unsigned char *osha1
, unsigned char *nsha1
,
1212 const char *email
, unsigned long timestamp
, int tz
,
1213 const char *message
, void *cb_data
)
1215 handle_one_reflog_commit(osha1
, cb_data
);
1216 handle_one_reflog_commit(nsha1
, cb_data
);
1220 static int handle_one_reflog(const char *path
, const unsigned char *sha1
, int flag
, void *cb_data
)
1222 struct all_refs_cb
*cb
= cb_data
;
1223 cb
->warned_bad_reflog
= 0;
1224 cb
->name_for_errormsg
= path
;
1225 for_each_reflog_ent(path
, handle_one_reflog_ent
, cb_data
);
1229 static void handle_reflog(struct rev_info
*revs
, unsigned flags
)
1231 struct all_refs_cb cb
;
1233 cb
.all_flags
= flags
;
1234 for_each_reflog(handle_one_reflog
, &cb
);
1237 static int add_parents_only(struct rev_info
*revs
, const char *arg_
, int flags
)
1239 unsigned char sha1
[20];
1241 struct commit
*commit
;
1242 struct commit_list
*parents
;
1243 const char *arg
= arg_
;
1246 flags
^= UNINTERESTING
| BOTTOM
;
1249 if (get_sha1_committish(arg
, sha1
))
1252 it
= get_reference(revs
, arg
, sha1
, 0);
1253 if (!it
&& revs
->ignore_missing
)
1255 if (it
->type
!= OBJ_TAG
)
1257 if (!((struct tag
*)it
)->tagged
)
1259 hashcpy(sha1
, ((struct tag
*)it
)->tagged
->sha1
);
1261 if (it
->type
!= OBJ_COMMIT
)
1263 commit
= (struct commit
*)it
;
1264 for (parents
= commit
->parents
; parents
; parents
= parents
->next
) {
1265 it
= &parents
->item
->object
;
1267 add_rev_cmdline(revs
, it
, arg_
, REV_CMD_PARENTS_ONLY
, flags
);
1268 add_pending_object(revs
, it
, arg
);
1273 void init_revisions(struct rev_info
*revs
, const char *prefix
)
1275 memset(revs
, 0, sizeof(*revs
));
1277 revs
->abbrev
= DEFAULT_ABBREV
;
1278 revs
->ignore_merges
= 1;
1279 revs
->simplify_history
= 1;
1280 DIFF_OPT_SET(&revs
->pruning
, RECURSIVE
);
1281 DIFF_OPT_SET(&revs
->pruning
, QUICK
);
1282 revs
->pruning
.add_remove
= file_add_remove
;
1283 revs
->pruning
.change
= file_change
;
1286 revs
->prefix
= prefix
;
1289 revs
->skip_count
= -1;
1290 revs
->max_count
= -1;
1291 revs
->max_parents
= -1;
1293 revs
->commit_format
= CMIT_FMT_DEFAULT
;
1295 init_grep_defaults();
1296 grep_init(&revs
->grep_filter
, prefix
);
1297 revs
->grep_filter
.status_only
= 1;
1298 revs
->grep_filter
.regflags
= REG_NEWLINE
;
1300 diff_setup(&revs
->diffopt
);
1301 if (prefix
&& !revs
->diffopt
.prefix
) {
1302 revs
->diffopt
.prefix
= prefix
;
1303 revs
->diffopt
.prefix_length
= strlen(prefix
);
1306 revs
->notes_opt
.use_default_notes
= -1;
1309 static void add_pending_commit_list(struct rev_info
*revs
,
1310 struct commit_list
*commit_list
,
1313 while (commit_list
) {
1314 struct object
*object
= &commit_list
->item
->object
;
1315 object
->flags
|= flags
;
1316 add_pending_object(revs
, object
, sha1_to_hex(object
->sha1
));
1317 commit_list
= commit_list
->next
;
1321 static void prepare_show_merge(struct rev_info
*revs
)
1323 struct commit_list
*bases
;
1324 struct commit
*head
, *other
;
1325 unsigned char sha1
[20];
1326 const char **prune
= NULL
;
1327 int i
, prune_num
= 1; /* counting terminating NULL */
1329 if (get_sha1("HEAD", sha1
))
1330 die("--merge without HEAD?");
1331 head
= lookup_commit_or_die(sha1
, "HEAD");
1332 if (get_sha1("MERGE_HEAD", sha1
))
1333 die("--merge without MERGE_HEAD?");
1334 other
= lookup_commit_or_die(sha1
, "MERGE_HEAD");
1335 add_pending_object(revs
, &head
->object
, "HEAD");
1336 add_pending_object(revs
, &other
->object
, "MERGE_HEAD");
1337 bases
= get_merge_bases(head
, other
, 1);
1338 add_rev_cmdline_list(revs
, bases
, REV_CMD_MERGE_BASE
, UNINTERESTING
| BOTTOM
);
1339 add_pending_commit_list(revs
, bases
, UNINTERESTING
| BOTTOM
);
1340 free_commit_list(bases
);
1341 head
->object
.flags
|= SYMMETRIC_LEFT
;
1345 for (i
= 0; i
< active_nr
; i
++) {
1346 struct cache_entry
*ce
= active_cache
[i
];
1349 if (ce_path_match(ce
, &revs
->prune_data
)) {
1351 prune
= xrealloc(prune
, sizeof(*prune
) * prune_num
);
1352 prune
[prune_num
-2] = ce
->name
;
1353 prune
[prune_num
-1] = NULL
;
1355 while ((i
+1 < active_nr
) &&
1356 ce_same_name(ce
, active_cache
[i
+1]))
1359 free_pathspec(&revs
->prune_data
);
1360 init_pathspec(&revs
->prune_data
, prune
);
1364 int handle_revision_arg(const char *arg_
, struct rev_info
*revs
, int flags
, unsigned revarg_opt
)
1366 struct object_context oc
;
1368 struct object
*object
;
1369 unsigned char sha1
[20];
1371 const char *arg
= arg_
;
1372 int cant_be_filename
= revarg_opt
& REVARG_CANNOT_BE_FILENAME
;
1373 unsigned get_sha1_flags
= 0;
1375 flags
= flags
& UNINTERESTING
? flags
| BOTTOM
: flags
& ~BOTTOM
;
1377 dotdot
= strstr(arg
, "..");
1379 unsigned char from_sha1
[20];
1380 const char *next
= dotdot
+ 2;
1381 const char *this = arg
;
1382 int symmetric
= *next
== '.';
1383 unsigned int flags_exclude
= flags
^ (UNINTERESTING
| BOTTOM
);
1384 static const char head_by_default
[] = "HEAD";
1385 unsigned int a_flags
;
1391 next
= head_by_default
;
1393 this = head_by_default
;
1394 if (this == head_by_default
&& next
== head_by_default
&&
1397 * Just ".."? That is not a range but the
1398 * pathspec for the parent directory.
1400 if (!cant_be_filename
) {
1405 if (!get_sha1_committish(this, from_sha1
) &&
1406 !get_sha1_committish(next
, sha1
)) {
1407 struct commit
*a
, *b
;
1408 struct commit_list
*exclude
;
1410 a
= lookup_commit_reference(from_sha1
);
1411 b
= lookup_commit_reference(sha1
);
1413 if (revs
->ignore_missing
)
1416 "Invalid symmetric difference expression %s...%s" :
1417 "Invalid revision range %s..%s",
1421 if (!cant_be_filename
) {
1423 verify_non_filename(revs
->prefix
, arg
);
1427 exclude
= get_merge_bases(a
, b
, 1);
1428 add_rev_cmdline_list(revs
, exclude
,
1431 add_pending_commit_list(revs
, exclude
,
1433 free_commit_list(exclude
);
1434 a_flags
= flags
| SYMMETRIC_LEFT
;
1436 a_flags
= flags_exclude
;
1437 a
->object
.flags
|= a_flags
;
1438 b
->object
.flags
|= flags
;
1439 add_rev_cmdline(revs
, &a
->object
, this,
1440 REV_CMD_LEFT
, a_flags
);
1441 add_rev_cmdline(revs
, &b
->object
, next
,
1442 REV_CMD_RIGHT
, flags
);
1443 add_pending_object(revs
, &a
->object
, this);
1444 add_pending_object(revs
, &b
->object
, next
);
1449 dotdot
= strstr(arg
, "^@");
1450 if (dotdot
&& !dotdot
[2]) {
1452 if (add_parents_only(revs
, arg
, flags
))
1456 dotdot
= strstr(arg
, "^!");
1457 if (dotdot
&& !dotdot
[2]) {
1459 if (!add_parents_only(revs
, arg
, flags
^ (UNINTERESTING
| BOTTOM
)))
1465 local_flags
= UNINTERESTING
| BOTTOM
;
1469 if (revarg_opt
& REVARG_COMMITTISH
)
1470 get_sha1_flags
= GET_SHA1_COMMITTISH
;
1472 if (get_sha1_with_context(arg
, get_sha1_flags
, sha1
, &oc
))
1473 return revs
->ignore_missing
? 0 : -1;
1474 if (!cant_be_filename
)
1475 verify_non_filename(revs
->prefix
, arg
);
1476 object
= get_reference(revs
, arg
, sha1
, flags
^ local_flags
);
1477 add_rev_cmdline(revs
, object
, arg_
, REV_CMD_REV
, flags
^ local_flags
);
1478 add_pending_object_with_mode(revs
, object
, arg
, oc
.mode
);
1482 struct cmdline_pathspec
{
1488 static void append_prune_data(struct cmdline_pathspec
*prune
, const char **av
)
1491 ALLOC_GROW(prune
->path
, prune
->nr
+1, prune
->alloc
);
1492 prune
->path
[prune
->nr
++] = *(av
++);
1496 static void read_pathspec_from_stdin(struct rev_info
*revs
, struct strbuf
*sb
,
1497 struct cmdline_pathspec
*prune
)
1499 while (strbuf_getwholeline(sb
, stdin
, '\n') != EOF
) {
1501 if (len
&& sb
->buf
[len
- 1] == '\n')
1502 sb
->buf
[--len
] = '\0';
1503 ALLOC_GROW(prune
->path
, prune
->nr
+1, prune
->alloc
);
1504 prune
->path
[prune
->nr
++] = xstrdup(sb
->buf
);
1508 static void read_revisions_from_stdin(struct rev_info
*revs
,
1509 struct cmdline_pathspec
*prune
)
1512 int seen_dashdash
= 0;
1514 strbuf_init(&sb
, 1000);
1515 while (strbuf_getwholeline(&sb
, stdin
, '\n') != EOF
) {
1517 if (len
&& sb
.buf
[len
- 1] == '\n')
1518 sb
.buf
[--len
] = '\0';
1521 if (sb
.buf
[0] == '-') {
1522 if (len
== 2 && sb
.buf
[1] == '-') {
1526 die("options not supported in --stdin mode");
1528 if (handle_revision_arg(xstrdup(sb
.buf
), revs
, 0,
1529 REVARG_CANNOT_BE_FILENAME
))
1530 die("bad revision '%s'", sb
.buf
);
1533 read_pathspec_from_stdin(revs
, &sb
, prune
);
1534 strbuf_release(&sb
);
1537 static void add_grep(struct rev_info
*revs
, const char *ptn
, enum grep_pat_token what
)
1539 append_grep_pattern(&revs
->grep_filter
, ptn
, "command line", 0, what
);
1542 static void add_header_grep(struct rev_info
*revs
, enum grep_header_field field
, const char *pattern
)
1544 append_header_grep_pattern(&revs
->grep_filter
, field
, pattern
);
1547 static void add_message_grep(struct rev_info
*revs
, const char *pattern
)
1549 add_grep(revs
, pattern
, GREP_PATTERN_BODY
);
1552 static int handle_revision_opt(struct rev_info
*revs
, int argc
, const char **argv
,
1553 int *unkc
, const char **unkv
)
1555 const char *arg
= argv
[0];
1559 /* pseudo revision arguments */
1560 if (!strcmp(arg
, "--all") || !strcmp(arg
, "--branches") ||
1561 !strcmp(arg
, "--tags") || !strcmp(arg
, "--remotes") ||
1562 !strcmp(arg
, "--reflog") || !strcmp(arg
, "--not") ||
1563 !strcmp(arg
, "--no-walk") || !strcmp(arg
, "--do-walk") ||
1564 !strcmp(arg
, "--bisect") || !prefixcmp(arg
, "--glob=") ||
1565 !prefixcmp(arg
, "--branches=") || !prefixcmp(arg
, "--tags=") ||
1566 !prefixcmp(arg
, "--remotes=") || !prefixcmp(arg
, "--no-walk="))
1568 unkv
[(*unkc
)++] = arg
;
1572 if ((argcount
= parse_long_opt("max-count", argv
, &optarg
))) {
1573 revs
->max_count
= atoi(optarg
);
1576 } else if ((argcount
= parse_long_opt("skip", argv
, &optarg
))) {
1577 revs
->skip_count
= atoi(optarg
);
1579 } else if ((*arg
== '-') && isdigit(arg
[1])) {
1580 /* accept -<digit>, like traditional "head" */
1581 revs
->max_count
= atoi(arg
+ 1);
1583 } else if (!strcmp(arg
, "-n")) {
1585 return error("-n requires an argument");
1586 revs
->max_count
= atoi(argv
[1]);
1589 } else if (!prefixcmp(arg
, "-n")) {
1590 revs
->max_count
= atoi(arg
+ 2);
1592 } else if ((argcount
= parse_long_opt("max-age", argv
, &optarg
))) {
1593 revs
->max_age
= atoi(optarg
);
1595 } else if ((argcount
= parse_long_opt("since", argv
, &optarg
))) {
1596 revs
->max_age
= approxidate(optarg
);
1598 } else if ((argcount
= parse_long_opt("after", argv
, &optarg
))) {
1599 revs
->max_age
= approxidate(optarg
);
1601 } else if ((argcount
= parse_long_opt("min-age", argv
, &optarg
))) {
1602 revs
->min_age
= atoi(optarg
);
1604 } else if ((argcount
= parse_long_opt("before", argv
, &optarg
))) {
1605 revs
->min_age
= approxidate(optarg
);
1607 } else if ((argcount
= parse_long_opt("until", argv
, &optarg
))) {
1608 revs
->min_age
= approxidate(optarg
);
1610 } else if (!strcmp(arg
, "--first-parent")) {
1611 revs
->first_parent_only
= 1;
1612 } else if (!strcmp(arg
, "--ancestry-path")) {
1613 revs
->ancestry_path
= 1;
1614 revs
->simplify_history
= 0;
1616 } else if (!strcmp(arg
, "-g") || !strcmp(arg
, "--walk-reflogs")) {
1617 init_reflog_walk(&revs
->reflog_info
);
1618 } else if (!strcmp(arg
, "--default")) {
1620 return error("bad --default argument");
1621 revs
->def
= argv
[1];
1623 } else if (!strcmp(arg
, "--merge")) {
1624 revs
->show_merge
= 1;
1625 } else if (!strcmp(arg
, "--topo-order")) {
1627 revs
->topo_order
= 1;
1628 } else if (!strcmp(arg
, "--simplify-merges")) {
1629 revs
->simplify_merges
= 1;
1630 revs
->topo_order
= 1;
1631 revs
->rewrite_parents
= 1;
1632 revs
->simplify_history
= 0;
1634 } else if (!strcmp(arg
, "--simplify-by-decoration")) {
1635 revs
->simplify_merges
= 1;
1636 revs
->topo_order
= 1;
1637 revs
->rewrite_parents
= 1;
1638 revs
->simplify_history
= 0;
1639 revs
->simplify_by_decoration
= 1;
1642 load_ref_decorations(DECORATE_SHORT_REFS
);
1643 } else if (!strcmp(arg
, "--date-order")) {
1645 revs
->topo_order
= 1;
1646 } else if (!prefixcmp(arg
, "--early-output")) {
1650 count
= atoi(arg
+15);
1653 revs
->topo_order
= 1;
1654 revs
->early_output
= count
;
1656 } else if (!strcmp(arg
, "--parents")) {
1657 revs
->rewrite_parents
= 1;
1658 revs
->print_parents
= 1;
1659 } else if (!strcmp(arg
, "--dense")) {
1661 } else if (!strcmp(arg
, "--sparse")) {
1663 } else if (!strcmp(arg
, "--show-all")) {
1665 } else if (!strcmp(arg
, "--remove-empty")) {
1666 revs
->remove_empty_trees
= 1;
1667 } else if (!strcmp(arg
, "--merges")) {
1668 revs
->min_parents
= 2;
1669 } else if (!strcmp(arg
, "--no-merges")) {
1670 revs
->max_parents
= 1;
1671 } else if (!prefixcmp(arg
, "--min-parents=")) {
1672 revs
->min_parents
= atoi(arg
+14);
1673 } else if (!prefixcmp(arg
, "--no-min-parents")) {
1674 revs
->min_parents
= 0;
1675 } else if (!prefixcmp(arg
, "--max-parents=")) {
1676 revs
->max_parents
= atoi(arg
+14);
1677 } else if (!prefixcmp(arg
, "--no-max-parents")) {
1678 revs
->max_parents
= -1;
1679 } else if (!strcmp(arg
, "--boundary")) {
1681 } else if (!strcmp(arg
, "--left-right")) {
1682 revs
->left_right
= 1;
1683 } else if (!strcmp(arg
, "--left-only")) {
1684 if (revs
->right_only
)
1685 die("--left-only is incompatible with --right-only"
1687 revs
->left_only
= 1;
1688 } else if (!strcmp(arg
, "--right-only")) {
1689 if (revs
->left_only
)
1690 die("--right-only is incompatible with --left-only");
1691 revs
->right_only
= 1;
1692 } else if (!strcmp(arg
, "--cherry")) {
1693 if (revs
->left_only
)
1694 die("--cherry is incompatible with --left-only");
1695 revs
->cherry_mark
= 1;
1696 revs
->right_only
= 1;
1697 revs
->max_parents
= 1;
1699 } else if (!strcmp(arg
, "--count")) {
1701 } else if (!strcmp(arg
, "--cherry-mark")) {
1702 if (revs
->cherry_pick
)
1703 die("--cherry-mark is incompatible with --cherry-pick");
1704 revs
->cherry_mark
= 1;
1705 revs
->limited
= 1; /* needs limit_list() */
1706 } else if (!strcmp(arg
, "--cherry-pick")) {
1707 if (revs
->cherry_mark
)
1708 die("--cherry-pick is incompatible with --cherry-mark");
1709 revs
->cherry_pick
= 1;
1711 } else if (!strcmp(arg
, "--objects")) {
1712 revs
->tag_objects
= 1;
1713 revs
->tree_objects
= 1;
1714 revs
->blob_objects
= 1;
1715 } else if (!strcmp(arg
, "--objects-edge")) {
1716 revs
->tag_objects
= 1;
1717 revs
->tree_objects
= 1;
1718 revs
->blob_objects
= 1;
1719 revs
->edge_hint
= 1;
1720 } else if (!strcmp(arg
, "--verify-objects")) {
1721 revs
->tag_objects
= 1;
1722 revs
->tree_objects
= 1;
1723 revs
->blob_objects
= 1;
1724 revs
->verify_objects
= 1;
1725 } else if (!strcmp(arg
, "--unpacked")) {
1727 } else if (!prefixcmp(arg
, "--unpacked=")) {
1728 die("--unpacked=<packfile> no longer supported.");
1729 } else if (!strcmp(arg
, "-r")) {
1731 DIFF_OPT_SET(&revs
->diffopt
, RECURSIVE
);
1732 } else if (!strcmp(arg
, "-t")) {
1734 DIFF_OPT_SET(&revs
->diffopt
, RECURSIVE
);
1735 DIFF_OPT_SET(&revs
->diffopt
, TREE_IN_RECURSIVE
);
1736 } else if (!strcmp(arg
, "-m")) {
1737 revs
->ignore_merges
= 0;
1738 } else if (!strcmp(arg
, "-c")) {
1740 revs
->dense_combined_merges
= 0;
1741 revs
->combine_merges
= 1;
1742 } else if (!strcmp(arg
, "--cc")) {
1744 revs
->dense_combined_merges
= 1;
1745 revs
->combine_merges
= 1;
1746 } else if (!strcmp(arg
, "-v")) {
1747 revs
->verbose_header
= 1;
1748 } else if (!strcmp(arg
, "--pretty")) {
1749 revs
->verbose_header
= 1;
1750 revs
->pretty_given
= 1;
1751 get_commit_format(arg
+8, revs
);
1752 } else if (!prefixcmp(arg
, "--pretty=") || !prefixcmp(arg
, "--format=")) {
1754 * Detached form ("--pretty X" as opposed to "--pretty=X")
1755 * not allowed, since the argument is optional.
1757 revs
->verbose_header
= 1;
1758 revs
->pretty_given
= 1;
1759 get_commit_format(arg
+9, revs
);
1760 } else if (!strcmp(arg
, "--show-notes") || !strcmp(arg
, "--notes")) {
1761 revs
->show_notes
= 1;
1762 revs
->show_notes_given
= 1;
1763 revs
->notes_opt
.use_default_notes
= 1;
1764 } else if (!strcmp(arg
, "--show-signature")) {
1765 revs
->show_signature
= 1;
1766 } else if (!prefixcmp(arg
, "--show-notes=") ||
1767 !prefixcmp(arg
, "--notes=")) {
1768 struct strbuf buf
= STRBUF_INIT
;
1769 revs
->show_notes
= 1;
1770 revs
->show_notes_given
= 1;
1771 if (!prefixcmp(arg
, "--show-notes")) {
1772 if (revs
->notes_opt
.use_default_notes
< 0)
1773 revs
->notes_opt
.use_default_notes
= 1;
1774 strbuf_addstr(&buf
, arg
+13);
1777 strbuf_addstr(&buf
, arg
+8);
1778 expand_notes_ref(&buf
);
1779 string_list_append(&revs
->notes_opt
.extra_notes_refs
,
1780 strbuf_detach(&buf
, NULL
));
1781 } else if (!strcmp(arg
, "--no-notes")) {
1782 revs
->show_notes
= 0;
1783 revs
->show_notes_given
= 1;
1784 revs
->notes_opt
.use_default_notes
= -1;
1785 /* we have been strdup'ing ourselves, so trick
1786 * string_list into free()ing strings */
1787 revs
->notes_opt
.extra_notes_refs
.strdup_strings
= 1;
1788 string_list_clear(&revs
->notes_opt
.extra_notes_refs
, 0);
1789 revs
->notes_opt
.extra_notes_refs
.strdup_strings
= 0;
1790 } else if (!strcmp(arg
, "--standard-notes")) {
1791 revs
->show_notes_given
= 1;
1792 revs
->notes_opt
.use_default_notes
= 1;
1793 } else if (!strcmp(arg
, "--no-standard-notes")) {
1794 revs
->notes_opt
.use_default_notes
= 0;
1795 } else if (!strcmp(arg
, "--oneline")) {
1796 revs
->verbose_header
= 1;
1797 get_commit_format("oneline", revs
);
1798 revs
->pretty_given
= 1;
1799 revs
->abbrev_commit
= 1;
1800 } else if (!strcmp(arg
, "--graph")) {
1801 revs
->topo_order
= 1;
1802 revs
->rewrite_parents
= 1;
1803 revs
->graph
= graph_init(revs
);
1804 } else if (!strcmp(arg
, "--root")) {
1805 revs
->show_root_diff
= 1;
1806 } else if (!strcmp(arg
, "--no-commit-id")) {
1807 revs
->no_commit_id
= 1;
1808 } else if (!strcmp(arg
, "--always")) {
1809 revs
->always_show_header
= 1;
1810 } else if (!strcmp(arg
, "--no-abbrev")) {
1812 } else if (!strcmp(arg
, "--abbrev")) {
1813 revs
->abbrev
= DEFAULT_ABBREV
;
1814 } else if (!prefixcmp(arg
, "--abbrev=")) {
1815 revs
->abbrev
= strtoul(arg
+ 9, NULL
, 10);
1816 if (revs
->abbrev
< MINIMUM_ABBREV
)
1817 revs
->abbrev
= MINIMUM_ABBREV
;
1818 else if (revs
->abbrev
> 40)
1820 } else if (!strcmp(arg
, "--abbrev-commit")) {
1821 revs
->abbrev_commit
= 1;
1822 revs
->abbrev_commit_given
= 1;
1823 } else if (!strcmp(arg
, "--no-abbrev-commit")) {
1824 revs
->abbrev_commit
= 0;
1825 } else if (!strcmp(arg
, "--full-diff")) {
1827 revs
->full_diff
= 1;
1828 } else if (!strcmp(arg
, "--full-history")) {
1829 revs
->simplify_history
= 0;
1830 } else if (!strcmp(arg
, "--relative-date")) {
1831 revs
->date_mode
= DATE_RELATIVE
;
1832 revs
->date_mode_explicit
= 1;
1833 } else if ((argcount
= parse_long_opt("date", argv
, &optarg
))) {
1834 revs
->date_mode
= parse_date_format(optarg
);
1835 revs
->date_mode_explicit
= 1;
1837 } else if (!strcmp(arg
, "--log-size")) {
1838 revs
->show_log_size
= 1;
1841 * Grepping the commit log
1843 else if ((argcount
= parse_long_opt("author", argv
, &optarg
))) {
1844 add_header_grep(revs
, GREP_HEADER_AUTHOR
, optarg
);
1846 } else if ((argcount
= parse_long_opt("committer", argv
, &optarg
))) {
1847 add_header_grep(revs
, GREP_HEADER_COMMITTER
, optarg
);
1849 } else if ((argcount
= parse_long_opt("grep-reflog", argv
, &optarg
))) {
1850 add_header_grep(revs
, GREP_HEADER_REFLOG
, optarg
);
1852 } else if ((argcount
= parse_long_opt("grep", argv
, &optarg
))) {
1853 add_message_grep(revs
, optarg
);
1855 } else if (!strcmp(arg
, "--grep-debug")) {
1856 revs
->grep_filter
.debug
= 1;
1857 } else if (!strcmp(arg
, "--basic-regexp")) {
1858 grep_set_pattern_type_option(GREP_PATTERN_TYPE_BRE
, &revs
->grep_filter
);
1859 } else if (!strcmp(arg
, "--extended-regexp") || !strcmp(arg
, "-E")) {
1860 grep_set_pattern_type_option(GREP_PATTERN_TYPE_ERE
, &revs
->grep_filter
);
1861 } else if (!strcmp(arg
, "--regexp-ignore-case") || !strcmp(arg
, "-i")) {
1862 revs
->grep_filter
.regflags
|= REG_ICASE
;
1863 DIFF_OPT_SET(&revs
->diffopt
, PICKAXE_IGNORE_CASE
);
1864 } else if (!strcmp(arg
, "--fixed-strings") || !strcmp(arg
, "-F")) {
1865 grep_set_pattern_type_option(GREP_PATTERN_TYPE_FIXED
, &revs
->grep_filter
);
1866 } else if (!strcmp(arg
, "--perl-regexp")) {
1867 grep_set_pattern_type_option(GREP_PATTERN_TYPE_PCRE
, &revs
->grep_filter
);
1868 } else if (!strcmp(arg
, "--all-match")) {
1869 revs
->grep_filter
.all_match
= 1;
1870 } else if ((argcount
= parse_long_opt("encoding", argv
, &optarg
))) {
1871 if (strcmp(optarg
, "none"))
1872 git_log_output_encoding
= xstrdup(optarg
);
1874 git_log_output_encoding
= "";
1876 } else if (!strcmp(arg
, "--reverse")) {
1878 } else if (!strcmp(arg
, "--children")) {
1879 revs
->children
.name
= "children";
1881 } else if (!strcmp(arg
, "--ignore-missing")) {
1882 revs
->ignore_missing
= 1;
1884 int opts
= diff_opt_parse(&revs
->diffopt
, argv
, argc
);
1886 unkv
[(*unkc
)++] = arg
;
1893 void parse_revision_opt(struct rev_info
*revs
, struct parse_opt_ctx_t
*ctx
,
1894 const struct option
*options
,
1895 const char * const usagestr
[])
1897 int n
= handle_revision_opt(revs
, ctx
->argc
, ctx
->argv
,
1898 &ctx
->cpidx
, ctx
->out
);
1900 error("unknown option `%s'", ctx
->argv
[0]);
1901 usage_with_options(usagestr
, options
);
1907 static int for_each_bad_bisect_ref(const char *submodule
, each_ref_fn fn
, void *cb_data
)
1909 return for_each_ref_in_submodule(submodule
, "refs/bisect/bad", fn
, cb_data
);
1912 static int for_each_good_bisect_ref(const char *submodule
, each_ref_fn fn
, void *cb_data
)
1914 return for_each_ref_in_submodule(submodule
, "refs/bisect/good", fn
, cb_data
);
1917 static int handle_revision_pseudo_opt(const char *submodule
,
1918 struct rev_info
*revs
,
1919 int argc
, const char **argv
, int *flags
)
1921 const char *arg
= argv
[0];
1928 * Commands like "git shortlog" will not accept the options below
1929 * unless parse_revision_opt queues them (as opposed to erroring
1932 * When implementing your new pseudo-option, remember to
1933 * register it in the list at the top of handle_revision_opt.
1935 if (!strcmp(arg
, "--all")) {
1936 handle_refs(submodule
, revs
, *flags
, for_each_ref_submodule
);
1937 handle_refs(submodule
, revs
, *flags
, head_ref_submodule
);
1938 } else if (!strcmp(arg
, "--branches")) {
1939 handle_refs(submodule
, revs
, *flags
, for_each_branch_ref_submodule
);
1940 } else if (!strcmp(arg
, "--bisect")) {
1941 handle_refs(submodule
, revs
, *flags
, for_each_bad_bisect_ref
);
1942 handle_refs(submodule
, revs
, *flags
^ (UNINTERESTING
| BOTTOM
), for_each_good_bisect_ref
);
1944 } else if (!strcmp(arg
, "--tags")) {
1945 handle_refs(submodule
, revs
, *flags
, for_each_tag_ref_submodule
);
1946 } else if (!strcmp(arg
, "--remotes")) {
1947 handle_refs(submodule
, revs
, *flags
, for_each_remote_ref_submodule
);
1948 } else if ((argcount
= parse_long_opt("glob", argv
, &optarg
))) {
1949 struct all_refs_cb cb
;
1950 init_all_refs_cb(&cb
, revs
, *flags
);
1951 for_each_glob_ref(handle_one_ref
, optarg
, &cb
);
1953 } else if (!prefixcmp(arg
, "--branches=")) {
1954 struct all_refs_cb cb
;
1955 init_all_refs_cb(&cb
, revs
, *flags
);
1956 for_each_glob_ref_in(handle_one_ref
, arg
+ 11, "refs/heads/", &cb
);
1957 } else if (!prefixcmp(arg
, "--tags=")) {
1958 struct all_refs_cb cb
;
1959 init_all_refs_cb(&cb
, revs
, *flags
);
1960 for_each_glob_ref_in(handle_one_ref
, arg
+ 7, "refs/tags/", &cb
);
1961 } else if (!prefixcmp(arg
, "--remotes=")) {
1962 struct all_refs_cb cb
;
1963 init_all_refs_cb(&cb
, revs
, *flags
);
1964 for_each_glob_ref_in(handle_one_ref
, arg
+ 10, "refs/remotes/", &cb
);
1965 } else if (!strcmp(arg
, "--reflog")) {
1966 handle_reflog(revs
, *flags
);
1967 } else if (!strcmp(arg
, "--not")) {
1968 *flags
^= UNINTERESTING
| BOTTOM
;
1969 } else if (!strcmp(arg
, "--no-walk")) {
1970 revs
->no_walk
= REVISION_WALK_NO_WALK_SORTED
;
1971 } else if (!prefixcmp(arg
, "--no-walk=")) {
1973 * Detached form ("--no-walk X" as opposed to "--no-walk=X")
1974 * not allowed, since the argument is optional.
1976 if (!strcmp(arg
+ 10, "sorted"))
1977 revs
->no_walk
= REVISION_WALK_NO_WALK_SORTED
;
1978 else if (!strcmp(arg
+ 10, "unsorted"))
1979 revs
->no_walk
= REVISION_WALK_NO_WALK_UNSORTED
;
1981 return error("invalid argument to --no-walk");
1982 } else if (!strcmp(arg
, "--do-walk")) {
1992 * Parse revision information, filling in the "rev_info" structure,
1993 * and removing the used arguments from the argument list.
1995 * Returns the number of arguments left that weren't recognized
1996 * (which are also moved to the head of the argument list)
1998 int setup_revisions(int argc
, const char **argv
, struct rev_info
*revs
, struct setup_revision_opt
*opt
)
2000 int i
, flags
, left
, seen_dashdash
, read_from_stdin
, got_rev_arg
= 0, revarg_opt
;
2001 struct cmdline_pathspec prune_data
;
2002 const char *submodule
= NULL
;
2004 memset(&prune_data
, 0, sizeof(prune_data
));
2006 submodule
= opt
->submodule
;
2008 /* First, search for "--" */
2009 if (opt
&& opt
->assume_dashdash
) {
2013 for (i
= 1; i
< argc
; i
++) {
2014 const char *arg
= argv
[i
];
2015 if (strcmp(arg
, "--"))
2020 append_prune_data(&prune_data
, argv
+ i
+ 1);
2026 /* Second, deal with arguments and options */
2028 revarg_opt
= opt
? opt
->revarg_opt
: 0;
2030 revarg_opt
|= REVARG_CANNOT_BE_FILENAME
;
2031 read_from_stdin
= 0;
2032 for (left
= i
= 1; i
< argc
; i
++) {
2033 const char *arg
= argv
[i
];
2037 opts
= handle_revision_pseudo_opt(submodule
,
2038 revs
, argc
- i
, argv
+ i
,
2045 if (!strcmp(arg
, "--stdin")) {
2046 if (revs
->disable_stdin
) {
2050 if (read_from_stdin
++)
2051 die("--stdin given twice?");
2052 read_revisions_from_stdin(revs
, &prune_data
);
2056 opts
= handle_revision_opt(revs
, argc
- i
, argv
+ i
, &left
, argv
);
2067 if (handle_revision_arg(arg
, revs
, flags
, revarg_opt
)) {
2069 if (seen_dashdash
|| *arg
== '^')
2070 die("bad revision '%s'", arg
);
2072 /* If we didn't have a "--":
2073 * (1) all filenames must exist;
2074 * (2) all rev-args must not be interpretable
2075 * as a valid filename.
2076 * but the latter we have checked in the main loop.
2078 for (j
= i
; j
< argc
; j
++)
2079 verify_filename(revs
->prefix
, argv
[j
], j
== i
);
2081 append_prune_data(&prune_data
, argv
+ i
);
2088 if (prune_data
.nr
) {
2090 * If we need to introduce the magic "a lone ':' means no
2091 * pathspec whatsoever", here is the place to do so.
2093 * if (prune_data.nr == 1 && !strcmp(prune_data[0], ":")) {
2094 * prune_data.nr = 0;
2095 * prune_data.alloc = 0;
2096 * free(prune_data.path);
2097 * prune_data.path = NULL;
2099 * terminate prune_data.alloc with NULL and
2100 * call init_pathspec() to set revs->prune_data here.
2103 ALLOC_GROW(prune_data
.path
, prune_data
.nr
+1, prune_data
.alloc
);
2104 prune_data
.path
[prune_data
.nr
++] = NULL
;
2105 init_pathspec(&revs
->prune_data
,
2106 get_pathspec(revs
->prefix
, prune_data
.path
));
2109 if (revs
->def
== NULL
)
2110 revs
->def
= opt
? opt
->def
: NULL
;
2111 if (opt
&& opt
->tweak
)
2112 opt
->tweak(revs
, opt
);
2113 if (revs
->show_merge
)
2114 prepare_show_merge(revs
);
2115 if (revs
->def
&& !revs
->pending
.nr
&& !got_rev_arg
) {
2116 unsigned char sha1
[20];
2117 struct object
*object
;
2118 struct object_context oc
;
2119 if (get_sha1_with_context(revs
->def
, 0, sha1
, &oc
))
2120 die("bad default revision '%s'", revs
->def
);
2121 object
= get_reference(revs
, revs
->def
, sha1
, 0);
2122 add_pending_object_with_mode(revs
, object
, revs
->def
, oc
.mode
);
2125 /* Did the user ask for any diff output? Run the diff! */
2126 if (revs
->diffopt
.output_format
& ~DIFF_FORMAT_NO_OUTPUT
)
2129 /* Pickaxe, diff-filter and rename following need diffs */
2130 if (revs
->diffopt
.pickaxe
||
2131 revs
->diffopt
.filter
||
2132 DIFF_OPT_TST(&revs
->diffopt
, FOLLOW_RENAMES
))
2135 if (revs
->topo_order
)
2138 if (revs
->prune_data
.nr
) {
2139 diff_tree_setup_paths(revs
->prune_data
.raw
, &revs
->pruning
);
2140 /* Can't prune commits with rename following: the paths change.. */
2141 if (!DIFF_OPT_TST(&revs
->diffopt
, FOLLOW_RENAMES
))
2143 if (!revs
->full_diff
)
2144 diff_tree_setup_paths(revs
->prune_data
.raw
, &revs
->diffopt
);
2146 if (revs
->combine_merges
)
2147 revs
->ignore_merges
= 0;
2148 revs
->diffopt
.abbrev
= revs
->abbrev
;
2149 diff_setup_done(&revs
->diffopt
);
2151 grep_commit_pattern_type(GREP_PATTERN_TYPE_UNSPECIFIED
,
2152 &revs
->grep_filter
);
2153 compile_grep_patterns(&revs
->grep_filter
);
2155 if (revs
->reverse
&& revs
->reflog_info
)
2156 die("cannot combine --reverse with --walk-reflogs");
2157 if (revs
->rewrite_parents
&& revs
->children
.name
)
2158 die("cannot combine --parents and --children");
2161 * Limitations on the graph functionality
2163 if (revs
->reverse
&& revs
->graph
)
2164 die("cannot combine --reverse with --graph");
2166 if (revs
->reflog_info
&& revs
->graph
)
2167 die("cannot combine --walk-reflogs with --graph");
2168 if (!revs
->reflog_info
&& revs
->grep_filter
.use_reflog_filter
)
2169 die("cannot use --grep-reflog without --walk-reflogs");
2174 static void add_child(struct rev_info
*revs
, struct commit
*parent
, struct commit
*child
)
2176 struct commit_list
*l
= xcalloc(1, sizeof(*l
));
2179 l
->next
= add_decoration(&revs
->children
, &parent
->object
, l
);
2182 static int remove_duplicate_parents(struct rev_info
*revs
, struct commit
*commit
)
2184 struct treesame_state
*ts
= lookup_decoration(&revs
->treesame
, &commit
->object
);
2185 struct commit_list
**pp
, *p
;
2186 int surviving_parents
;
2188 /* Examine existing parents while marking ones we have seen... */
2189 pp
= &commit
->parents
;
2190 surviving_parents
= 0;
2191 while ((p
= *pp
) != NULL
) {
2192 struct commit
*parent
= p
->item
;
2193 if (parent
->object
.flags
& TMP_MARK
) {
2196 compact_treesame(revs
, commit
, surviving_parents
);
2199 parent
->object
.flags
|= TMP_MARK
;
2200 surviving_parents
++;
2203 /* clear the temporary mark */
2204 for (p
= commit
->parents
; p
; p
= p
->next
) {
2205 p
->item
->object
.flags
&= ~TMP_MARK
;
2207 /* no update_treesame() - removing duplicates can't affect TREESAME */
2208 return surviving_parents
;
2211 struct merge_simplify_state
{
2212 struct commit
*simplified
;
2215 static struct merge_simplify_state
*locate_simplify_state(struct rev_info
*revs
, struct commit
*commit
)
2217 struct merge_simplify_state
*st
;
2219 st
= lookup_decoration(&revs
->merge_simplification
, &commit
->object
);
2221 st
= xcalloc(1, sizeof(*st
));
2222 add_decoration(&revs
->merge_simplification
, &commit
->object
, st
);
2227 static int mark_redundant_parents(struct rev_info
*revs
, struct commit
*commit
)
2229 struct commit_list
*h
= reduce_heads(commit
->parents
);
2230 int i
= 0, marked
= 0;
2231 struct commit_list
*po
, *pn
;
2233 /* Want these for sanity-checking only */
2234 int orig_cnt
= commit_list_count(commit
->parents
);
2235 int cnt
= commit_list_count(h
);
2238 * Not ready to remove items yet, just mark them for now, based
2239 * on the output of reduce_heads(). reduce_heads outputs the reduced
2240 * set in its original order, so this isn't too hard.
2242 po
= commit
->parents
;
2245 if (pn
&& po
->item
== pn
->item
) {
2249 po
->item
->object
.flags
|= TMP_MARK
;
2255 if (i
!= cnt
|| cnt
+marked
!= orig_cnt
)
2256 die("mark_redundant_parents %d %d %d %d", orig_cnt
, cnt
, i
, marked
);
2258 free_commit_list(h
);
2263 static int mark_treesame_root_parents(struct rev_info
*revs
, struct commit
*commit
)
2265 struct commit_list
*p
;
2268 for (p
= commit
->parents
; p
; p
= p
->next
) {
2269 struct commit
*parent
= p
->item
;
2270 if (!parent
->parents
&& (parent
->object
.flags
& TREESAME
)) {
2271 parent
->object
.flags
|= TMP_MARK
;
2280 * Awkward naming - this means one parent we are TREESAME to.
2281 * cf mark_treesame_root_parents: root parents that are TREESAME (to an
2282 * empty tree). Better name suggestions?
2284 static int leave_one_treesame_to_parent(struct rev_info
*revs
, struct commit
*commit
)
2286 struct treesame_state
*ts
= lookup_decoration(&revs
->treesame
, &commit
->object
);
2287 struct commit
*unmarked
= NULL
, *marked
= NULL
;
2288 struct commit_list
*p
;
2291 for (p
= commit
->parents
, n
= 0; p
; p
= p
->next
, n
++) {
2292 if (ts
->treesame
[n
]) {
2293 if (p
->item
->object
.flags
& TMP_MARK
) {
2306 * If we are TREESAME to a marked-for-deletion parent, but not to any
2307 * unmarked parents, unmark the first TREESAME parent. This is the
2308 * parent that the default simplify_history==1 scan would have followed,
2309 * and it doesn't make sense to omit that path when asking for a
2310 * simplified full history. Retaining it improves the chances of
2311 * understanding odd missed merges that took an old version of a file.
2315 * I--------*X A modified the file, but mainline merge X used
2316 * \ / "-s ours", so took the version from I. X is
2317 * `-*A--' TREESAME to I and !TREESAME to A.
2319 * Default log from X would produce "I". Without this check,
2320 * --full-history --simplify-merges would produce "I-A-X", showing
2321 * the merge commit X and that it changed A, but not making clear that
2322 * it had just taken the I version. With this check, the topology above
2325 * Note that it is possible that the simplification chooses a different
2326 * TREESAME parent from the default, in which case this test doesn't
2327 * activate, and we _do_ drop the default parent. Example:
2329 * I------X A modified the file, but it was reverted in B,
2330 * \ / meaning mainline merge X is TREESAME to both
2333 * Default log would produce "I" by following the first parent;
2334 * --full-history --simplify-merges will produce "I-A-B". But this is a
2335 * reasonable result - it presents a logical full history leading from
2336 * I to X, and X is not an important merge.
2338 if (!unmarked
&& marked
) {
2339 marked
->object
.flags
&= ~TMP_MARK
;
2346 static int remove_marked_parents(struct rev_info
*revs
, struct commit
*commit
)
2348 struct commit_list
**pp
, *p
;
2349 int nth_parent
, removed
= 0;
2351 pp
= &commit
->parents
;
2353 while ((p
= *pp
) != NULL
) {
2354 struct commit
*parent
= p
->item
;
2355 if (parent
->object
.flags
& TMP_MARK
) {
2356 parent
->object
.flags
&= ~TMP_MARK
;
2360 compact_treesame(revs
, commit
, nth_parent
);
2367 /* Removing parents can only increase TREESAMEness */
2368 if (removed
&& !(commit
->object
.flags
& TREESAME
))
2369 update_treesame(revs
, commit
);
2374 static struct commit_list
**simplify_one(struct rev_info
*revs
, struct commit
*commit
, struct commit_list
**tail
)
2376 struct commit_list
*p
;
2377 struct commit
*parent
;
2378 struct merge_simplify_state
*st
, *pst
;
2381 st
= locate_simplify_state(revs
, commit
);
2384 * Have we handled this one?
2390 * An UNINTERESTING commit simplifies to itself, so does a
2391 * root commit. We do not rewrite parents of such commit
2394 if ((commit
->object
.flags
& UNINTERESTING
) || !commit
->parents
) {
2395 st
->simplified
= commit
;
2400 * Do we know what commit all of our parents that matter
2401 * should be rewritten to? Otherwise we are not ready to
2402 * rewrite this one yet.
2404 for (cnt
= 0, p
= commit
->parents
; p
; p
= p
->next
) {
2405 pst
= locate_simplify_state(revs
, p
->item
);
2406 if (!pst
->simplified
) {
2407 tail
= &commit_list_insert(p
->item
, tail
)->next
;
2410 if (revs
->first_parent_only
)
2414 tail
= &commit_list_insert(commit
, tail
)->next
;
2419 * Rewrite our list of parents. Note that this cannot
2420 * affect our TREESAME flags in any way - a commit is
2421 * always TREESAME to its simplification.
2423 for (p
= commit
->parents
; p
; p
= p
->next
) {
2424 pst
= locate_simplify_state(revs
, p
->item
);
2425 p
->item
= pst
->simplified
;
2426 if (revs
->first_parent_only
)
2430 if (revs
->first_parent_only
)
2433 cnt
= remove_duplicate_parents(revs
, commit
);
2436 * It is possible that we are a merge and one side branch
2437 * does not have any commit that touches the given paths;
2438 * in such a case, the immediate parent from that branch
2439 * will be rewritten to be the merge base.
2441 * o----X X: the commit we are looking at;
2442 * / / o: a commit that touches the paths;
2445 * Further, a merge of an independent branch that doesn't
2446 * touch the path will reduce to a treesame root parent:
2448 * ----o----X X: the commit we are looking at;
2449 * / o: a commit that touches the paths;
2450 * r r: a root commit not touching the paths
2452 * Detect and simplify both cases.
2455 int marked
= mark_redundant_parents(revs
, commit
);
2456 marked
+= mark_treesame_root_parents(revs
, commit
);
2458 marked
-= leave_one_treesame_to_parent(revs
, commit
);
2460 cnt
= remove_marked_parents(revs
, commit
);
2464 * A commit simplifies to itself if it is a root, if it is
2465 * UNINTERESTING, if it touches the given paths, or if it is a
2466 * merge and its parents don't simplify to one relevant commit
2467 * (the first two cases are already handled at the beginning of
2470 * Otherwise, it simplifies to what its sole relevant parent
2474 (commit
->object
.flags
& UNINTERESTING
) ||
2475 !(commit
->object
.flags
& TREESAME
) ||
2476 (parent
= one_relevant_parent(revs
, commit
->parents
)) == NULL
)
2477 st
->simplified
= commit
;
2479 pst
= locate_simplify_state(revs
, parent
);
2480 st
->simplified
= pst
->simplified
;
2485 static void simplify_merges(struct rev_info
*revs
)
2487 struct commit_list
*list
, *next
;
2488 struct commit_list
*yet_to_do
, **tail
;
2489 struct commit
*commit
;
2494 /* feed the list reversed */
2496 for (list
= revs
->commits
; list
; list
= next
) {
2497 commit
= list
->item
;
2500 * Do not free(list) here yet; the original list
2501 * is used later in this function.
2503 commit_list_insert(commit
, &yet_to_do
);
2510 commit
= list
->item
;
2514 tail
= simplify_one(revs
, commit
, tail
);
2518 /* clean up the result, removing the simplified ones */
2519 list
= revs
->commits
;
2520 revs
->commits
= NULL
;
2521 tail
= &revs
->commits
;
2523 struct merge_simplify_state
*st
;
2525 commit
= list
->item
;
2529 st
= locate_simplify_state(revs
, commit
);
2530 if (st
->simplified
== commit
)
2531 tail
= &commit_list_insert(commit
, tail
)->next
;
2535 static void set_children(struct rev_info
*revs
)
2537 struct commit_list
*l
;
2538 for (l
= revs
->commits
; l
; l
= l
->next
) {
2539 struct commit
*commit
= l
->item
;
2540 struct commit_list
*p
;
2542 for (p
= commit
->parents
; p
; p
= p
->next
)
2543 add_child(revs
, p
->item
, commit
);
2547 void reset_revision_walk(void)
2549 clear_object_flags(SEEN
| ADDED
| SHOWN
);
2552 int prepare_revision_walk(struct rev_info
*revs
)
2554 int nr
= revs
->pending
.nr
;
2555 struct object_array_entry
*e
, *list
;
2556 struct commit_list
**next
= &revs
->commits
;
2558 e
= list
= revs
->pending
.objects
;
2559 revs
->pending
.nr
= 0;
2560 revs
->pending
.alloc
= 0;
2561 revs
->pending
.objects
= NULL
;
2563 struct commit
*commit
= handle_commit(revs
, e
->item
, e
->name
);
2565 if (!(commit
->object
.flags
& SEEN
)) {
2566 commit
->object
.flags
|= SEEN
;
2567 next
= commit_list_append(commit
, next
);
2572 if (!revs
->leak_pending
)
2575 /* Signal whether we need per-parent treesame decoration */
2576 if (revs
->simplify_merges
||
2577 (revs
->limited
&& limiting_can_increase_treesame(revs
)))
2578 revs
->treesame
.name
= "treesame";
2580 if (revs
->no_walk
!= REVISION_WALK_NO_WALK_UNSORTED
)
2581 commit_list_sort_by_date(&revs
->commits
);
2585 if (limit_list(revs
) < 0)
2587 if (revs
->topo_order
)
2588 sort_in_topological_order(&revs
->commits
, revs
->lifo
);
2589 if (revs
->simplify_merges
)
2590 simplify_merges(revs
);
2591 if (revs
->children
.name
)
2596 enum rewrite_result
{
2598 rewrite_one_noparents
,
2602 static enum rewrite_result
rewrite_one(struct rev_info
*revs
, struct commit
**pp
)
2604 struct commit_list
*cache
= NULL
;
2607 struct commit
*p
= *pp
;
2609 if (add_parents_to_list(revs
, p
, &revs
->commits
, &cache
) < 0)
2610 return rewrite_one_error
;
2611 if (p
->object
.flags
& UNINTERESTING
)
2612 return rewrite_one_ok
;
2613 if (!(p
->object
.flags
& TREESAME
))
2614 return rewrite_one_ok
;
2616 return rewrite_one_noparents
;
2617 if ((p
= one_relevant_parent(revs
, p
->parents
)) == NULL
)
2618 return rewrite_one_ok
;
2623 static int rewrite_parents(struct rev_info
*revs
, struct commit
*commit
)
2625 struct commit_list
**pp
= &commit
->parents
;
2627 struct commit_list
*parent
= *pp
;
2628 switch (rewrite_one(revs
, &parent
->item
)) {
2629 case rewrite_one_ok
:
2631 case rewrite_one_noparents
:
2634 case rewrite_one_error
:
2639 remove_duplicate_parents(revs
, commit
);
2643 static int commit_rewrite_person(struct strbuf
*buf
, const char *what
, struct string_list
*mailmap
)
2645 char *person
, *endp
;
2646 size_t len
, namelen
, maillen
;
2649 struct ident_split ident
;
2651 person
= strstr(buf
->buf
, what
);
2655 person
+= strlen(what
);
2656 endp
= strchr(person
, '\n');
2660 len
= endp
- person
;
2662 if (split_ident_line(&ident
, person
, len
))
2665 mail
= ident
.mail_begin
;
2666 maillen
= ident
.mail_end
- ident
.mail_begin
;
2667 name
= ident
.name_begin
;
2668 namelen
= ident
.name_end
- ident
.name_begin
;
2670 if (map_user(mailmap
, &mail
, &maillen
, &name
, &namelen
)) {
2671 struct strbuf namemail
= STRBUF_INIT
;
2673 strbuf_addf(&namemail
, "%.*s <%.*s>",
2674 (int)namelen
, name
, (int)maillen
, mail
);
2676 strbuf_splice(buf
, ident
.name_begin
- buf
->buf
,
2677 ident
.mail_end
- ident
.name_begin
+ 1,
2678 namemail
.buf
, namemail
.len
);
2680 strbuf_release(&namemail
);
2688 static int commit_match(struct commit
*commit
, struct rev_info
*opt
)
2691 const char *encoding
;
2693 struct strbuf buf
= STRBUF_INIT
;
2695 if (!opt
->grep_filter
.pattern_list
&& !opt
->grep_filter
.header_list
)
2698 /* Prepend "fake" headers as needed */
2699 if (opt
->grep_filter
.use_reflog_filter
) {
2700 strbuf_addstr(&buf
, "reflog ");
2701 get_reflog_message(&buf
, opt
->reflog_info
);
2702 strbuf_addch(&buf
, '\n');
2706 * We grep in the user's output encoding, under the assumption that it
2707 * is the encoding they are most likely to write their grep pattern
2708 * for. In addition, it means we will match the "notes" encoding below,
2709 * so we will not end up with a buffer that has two different encodings
2712 encoding
= get_log_output_encoding();
2713 message
= logmsg_reencode(commit
, NULL
, encoding
);
2715 /* Copy the commit to temporary if we are using "fake" headers */
2717 strbuf_addstr(&buf
, message
);
2719 if (opt
->grep_filter
.header_list
&& opt
->mailmap
) {
2721 strbuf_addstr(&buf
, message
);
2723 commit_rewrite_person(&buf
, "\nauthor ", opt
->mailmap
);
2724 commit_rewrite_person(&buf
, "\ncommitter ", opt
->mailmap
);
2727 /* Append "fake" message parts as needed */
2728 if (opt
->show_notes
) {
2730 strbuf_addstr(&buf
, message
);
2731 format_display_notes(commit
->object
.sha1
, &buf
, encoding
, 1);
2734 /* Find either in the original commit message, or in the temporary */
2736 retval
= grep_buffer(&opt
->grep_filter
, buf
.buf
, buf
.len
);
2738 retval
= grep_buffer(&opt
->grep_filter
,
2739 message
, strlen(message
));
2740 strbuf_release(&buf
);
2741 logmsg_free(message
, commit
);
2745 static inline int want_ancestry(struct rev_info
*revs
)
2747 return (revs
->rewrite_parents
|| revs
->children
.name
);
2750 enum commit_action
get_commit_action(struct rev_info
*revs
, struct commit
*commit
)
2752 if (commit
->object
.flags
& SHOWN
)
2753 return commit_ignore
;
2754 if (revs
->unpacked
&& has_sha1_pack(commit
->object
.sha1
))
2755 return commit_ignore
;
2758 if (commit
->object
.flags
& UNINTERESTING
)
2759 return commit_ignore
;
2760 if (revs
->min_age
!= -1 && (commit
->date
> revs
->min_age
))
2761 return commit_ignore
;
2762 if (revs
->min_parents
|| (revs
->max_parents
>= 0)) {
2763 int n
= commit_list_count(commit
->parents
);
2764 if ((n
< revs
->min_parents
) ||
2765 ((revs
->max_parents
>= 0) && (n
> revs
->max_parents
)))
2766 return commit_ignore
;
2768 if (!commit_match(commit
, revs
))
2769 return commit_ignore
;
2770 if (revs
->prune
&& revs
->dense
) {
2771 /* Commit without changes? */
2772 if (commit
->object
.flags
& TREESAME
) {
2774 struct commit_list
*p
;
2775 /* drop merges unless we want parenthood */
2776 if (!want_ancestry(revs
))
2777 return commit_ignore
;
2779 * If we want ancestry, then need to keep any merges
2780 * between relevant commits to tie together topology.
2781 * For consistency with TREESAME and simplification
2782 * use "relevant" here rather than just INTERESTING,
2783 * to treat bottom commit(s) as part of the topology.
2785 for (n
= 0, p
= commit
->parents
; p
; p
= p
->next
)
2786 if (relevant_commit(p
->item
))
2789 return commit_ignore
;
2795 enum commit_action
simplify_commit(struct rev_info
*revs
, struct commit
*commit
)
2797 enum commit_action action
= get_commit_action(revs
, commit
);
2799 if (action
== commit_show
&&
2801 revs
->prune
&& revs
->dense
&& want_ancestry(revs
)) {
2802 if (rewrite_parents(revs
, commit
) < 0)
2803 return commit_error
;
2808 static struct commit
*get_revision_1(struct rev_info
*revs
)
2814 struct commit_list
*entry
= revs
->commits
;
2815 struct commit
*commit
= entry
->item
;
2817 revs
->commits
= entry
->next
;
2820 if (revs
->reflog_info
) {
2821 fake_reflog_parent(revs
->reflog_info
, commit
);
2822 commit
->object
.flags
&= ~(ADDED
| SEEN
| SHOWN
);
2826 * If we haven't done the list limiting, we need to look at
2827 * the parents here. We also need to do the date-based limiting
2828 * that we'd otherwise have done in limit_list().
2830 if (!revs
->limited
) {
2831 if (revs
->max_age
!= -1 &&
2832 (commit
->date
< revs
->max_age
))
2834 if (add_parents_to_list(revs
, commit
, &revs
->commits
, NULL
) < 0)
2835 die("Failed to traverse parents of commit %s",
2836 sha1_to_hex(commit
->object
.sha1
));
2839 switch (simplify_commit(revs
, commit
)) {
2843 die("Failed to simplify parents of commit %s",
2844 sha1_to_hex(commit
->object
.sha1
));
2848 } while (revs
->commits
);
2852 static void gc_boundary(struct object_array
*array
)
2854 unsigned nr
= array
->nr
;
2855 unsigned alloc
= array
->alloc
;
2856 struct object_array_entry
*objects
= array
->objects
;
2860 for (i
= j
= 0; i
< nr
; i
++) {
2861 if (objects
[i
].item
->flags
& SHOWN
)
2864 objects
[j
] = objects
[i
];
2867 for (i
= j
; i
< nr
; i
++)
2868 objects
[i
].item
= NULL
;
2873 static void create_boundary_commit_list(struct rev_info
*revs
)
2877 struct object_array
*array
= &revs
->boundary_commits
;
2878 struct object_array_entry
*objects
= array
->objects
;
2881 * If revs->commits is non-NULL at this point, an error occurred in
2882 * get_revision_1(). Ignore the error and continue printing the
2883 * boundary commits anyway. (This is what the code has always
2886 if (revs
->commits
) {
2887 free_commit_list(revs
->commits
);
2888 revs
->commits
= NULL
;
2892 * Put all of the actual boundary commits from revs->boundary_commits
2893 * into revs->commits
2895 for (i
= 0; i
< array
->nr
; i
++) {
2896 c
= (struct commit
*)(objects
[i
].item
);
2899 if (!(c
->object
.flags
& CHILD_SHOWN
))
2901 if (c
->object
.flags
& (SHOWN
| BOUNDARY
))
2903 c
->object
.flags
|= BOUNDARY
;
2904 commit_list_insert(c
, &revs
->commits
);
2908 * If revs->topo_order is set, sort the boundary commits
2909 * in topological order
2911 sort_in_topological_order(&revs
->commits
, revs
->lifo
);
2914 static struct commit
*get_revision_internal(struct rev_info
*revs
)
2916 struct commit
*c
= NULL
;
2917 struct commit_list
*l
;
2919 if (revs
->boundary
== 2) {
2921 * All of the normal commits have already been returned,
2922 * and we are now returning boundary commits.
2923 * create_boundary_commit_list() has populated
2924 * revs->commits with the remaining commits to return.
2926 c
= pop_commit(&revs
->commits
);
2928 c
->object
.flags
|= SHOWN
;
2933 * If our max_count counter has reached zero, then we are done. We
2934 * don't simply return NULL because we still might need to show
2935 * boundary commits. But we want to avoid calling get_revision_1, which
2936 * might do a considerable amount of work finding the next commit only
2937 * for us to throw it away.
2939 * If it is non-zero, then either we don't have a max_count at all
2940 * (-1), or it is still counting, in which case we decrement.
2942 if (revs
->max_count
) {
2943 c
= get_revision_1(revs
);
2945 while (0 < revs
->skip_count
) {
2947 c
= get_revision_1(revs
);
2953 if (revs
->max_count
> 0)
2958 c
->object
.flags
|= SHOWN
;
2960 if (!revs
->boundary
) {
2966 * get_revision_1() runs out the commits, and
2967 * we are done computing the boundaries.
2968 * switch to boundary commits output mode.
2973 * Update revs->commits to contain the list of
2976 create_boundary_commit_list(revs
);
2978 return get_revision_internal(revs
);
2982 * boundary commits are the commits that are parents of the
2983 * ones we got from get_revision_1() but they themselves are
2984 * not returned from get_revision_1(). Before returning
2985 * 'c', we need to mark its parents that they could be boundaries.
2988 for (l
= c
->parents
; l
; l
= l
->next
) {
2990 p
= &(l
->item
->object
);
2991 if (p
->flags
& (CHILD_SHOWN
| SHOWN
))
2993 p
->flags
|= CHILD_SHOWN
;
2994 gc_boundary(&revs
->boundary_commits
);
2995 add_object_array(p
, NULL
, &revs
->boundary_commits
);
3001 struct commit
*get_revision(struct rev_info
*revs
)
3004 struct commit_list
*reversed
;
3006 if (revs
->reverse
) {
3008 while ((c
= get_revision_internal(revs
))) {
3009 commit_list_insert(c
, &reversed
);
3011 revs
->commits
= reversed
;
3013 revs
->reverse_output_stage
= 1;
3016 if (revs
->reverse_output_stage
)
3017 return pop_commit(&revs
->commits
);
3019 c
= get_revision_internal(revs
);
3020 if (c
&& revs
->graph
)
3021 graph_update(revs
->graph
, c
);
3025 char *get_revision_mark(const struct rev_info
*revs
, const struct commit
*commit
)
3027 if (commit
->object
.flags
& BOUNDARY
)
3029 else if (commit
->object
.flags
& UNINTERESTING
)
3031 else if (commit
->object
.flags
& PATCHSAME
)
3033 else if (!revs
|| revs
->left_right
) {
3034 if (commit
->object
.flags
& SYMMETRIC_LEFT
)
3038 } else if (revs
->graph
)
3040 else if (revs
->cherry_mark
)
3045 void put_revision_mark(const struct rev_info
*revs
, const struct commit
*commit
)
3047 char *mark
= get_revision_mark(revs
, commit
);
3050 fputs(mark
, stdout
);