11 #include "reflog-walk.h"
12 #include "patch-ids.h"
15 #include "string-list.h"
18 #include "commit-slab.h"
21 volatile show_early_output_fn_t show_early_output
;
23 char *path_name(const struct name_path
*path
, const char *name
)
25 const struct name_path
*p
;
27 int nlen
= strlen(name
);
30 for (p
= path
; p
; p
= p
->up
) {
32 len
+= p
->elem_len
+ 1;
35 m
= n
+ len
- (nlen
+ 1);
37 for (p
= path
; p
; p
= p
->up
) {
40 memcpy(m
, p
->elem
, p
->elem_len
);
47 static int show_path_component_truncated(FILE *out
, const char *name
, int len
)
50 for (cnt
= 0; cnt
< len
; cnt
++) {
52 if (!ch
|| ch
== '\n')
59 static int show_path_truncated(FILE *out
, const struct name_path
*path
)
65 emitted
= show_path_truncated(out
, path
->up
);
70 ours
= show_path_component_truncated(out
, path
->elem
, path
->elem_len
);
73 return ours
|| emitted
;
76 void show_object_with_name(FILE *out
, struct object
*obj
,
77 const struct name_path
*path
, const char *component
)
79 struct name_path leaf
;
80 leaf
.up
= (struct name_path
*)path
;
81 leaf
.elem
= component
;
82 leaf
.elem_len
= strlen(component
);
84 fprintf(out
, "%s ", sha1_to_hex(obj
->sha1
));
85 show_path_truncated(out
, &leaf
);
89 void add_object(struct object
*obj
,
90 struct object_array
*p
,
91 struct name_path
*path
,
94 char *pn
= path_name(path
, name
);
95 add_object_array(obj
, pn
, p
);
99 static void mark_blob_uninteresting(struct blob
*blob
)
103 if (blob
->object
.flags
& UNINTERESTING
)
105 blob
->object
.flags
|= UNINTERESTING
;
108 static void mark_tree_contents_uninteresting(struct tree
*tree
)
110 struct tree_desc desc
;
111 struct name_entry entry
;
112 struct object
*obj
= &tree
->object
;
114 if (!has_sha1_file(obj
->sha1
))
116 if (parse_tree(tree
) < 0)
117 die("bad tree %s", sha1_to_hex(obj
->sha1
));
119 init_tree_desc(&desc
, tree
->buffer
, tree
->size
);
120 while (tree_entry(&desc
, &entry
)) {
121 switch (object_type(entry
.mode
)) {
123 mark_tree_uninteresting(lookup_tree(entry
.sha1
));
126 mark_blob_uninteresting(lookup_blob(entry
.sha1
));
129 /* Subproject commit - not in this repository */
135 * We don't care about the tree any more
136 * after it has been marked uninteresting.
138 free_tree_buffer(tree
);
141 void mark_tree_uninteresting(struct tree
*tree
)
143 struct object
*obj
= &tree
->object
;
147 if (obj
->flags
& UNINTERESTING
)
149 obj
->flags
|= UNINTERESTING
;
150 mark_tree_contents_uninteresting(tree
);
153 void mark_parents_uninteresting(struct commit
*commit
)
155 struct commit_list
*parents
= NULL
, *l
;
157 for (l
= commit
->parents
; l
; l
= l
->next
)
158 commit_list_insert(l
->item
, &parents
);
161 struct commit
*commit
= parents
->item
;
163 parents
= parents
->next
;
168 * A missing commit is ok iff its parent is marked
171 * We just mark such a thing parsed, so that when
172 * it is popped next time around, we won't be trying
173 * to parse it and get an error.
175 if (!has_sha1_file(commit
->object
.sha1
))
176 commit
->object
.parsed
= 1;
178 if (commit
->object
.flags
& UNINTERESTING
)
181 commit
->object
.flags
|= UNINTERESTING
;
184 * Normally we haven't parsed the parent
185 * yet, so we won't have a parent of a parent
186 * here. However, it may turn out that we've
187 * reached this commit some other way (where it
188 * wasn't uninteresting), in which case we need
189 * to mark its parents recursively too..
191 if (!commit
->parents
)
194 for (l
= commit
->parents
->next
; l
; l
= l
->next
)
195 commit_list_insert(l
->item
, &parents
);
196 commit
= commit
->parents
->item
;
201 static void add_pending_object_with_mode(struct rev_info
*revs
,
203 const char *name
, unsigned mode
)
207 if (revs
->no_walk
&& (obj
->flags
& UNINTERESTING
))
209 if (revs
->reflog_info
&& obj
->type
== OBJ_COMMIT
) {
210 struct strbuf buf
= STRBUF_INIT
;
211 int len
= interpret_branch_name(name
, 0, &buf
);
214 if (0 < len
&& name
[len
] && buf
.len
)
215 strbuf_addstr(&buf
, name
+ len
);
216 st
= add_reflog_for_walk(revs
->reflog_info
,
217 (struct commit
*)obj
,
218 buf
.buf
[0] ? buf
.buf
: name
);
219 strbuf_release(&buf
);
223 add_object_array_with_mode(obj
, name
, &revs
->pending
, mode
);
226 void add_pending_object(struct rev_info
*revs
,
227 struct object
*obj
, const char *name
)
229 add_pending_object_with_mode(revs
, obj
, name
, S_IFINVALID
);
232 void add_head_to_pending(struct rev_info
*revs
)
234 unsigned char sha1
[20];
236 if (get_sha1("HEAD", sha1
))
238 obj
= parse_object(sha1
);
241 add_pending_object(revs
, obj
, "HEAD");
244 static struct object
*get_reference(struct rev_info
*revs
, const char *name
,
245 const unsigned char *sha1
,
248 struct object
*object
;
250 object
= parse_object(sha1
);
252 if (revs
->ignore_missing
)
254 die("bad object %s", name
);
256 object
->flags
|= flags
;
260 void add_pending_sha1(struct rev_info
*revs
, const char *name
,
261 const unsigned char *sha1
, unsigned int flags
)
263 struct object
*object
= get_reference(revs
, name
, sha1
, flags
);
264 add_pending_object(revs
, object
, name
);
267 static struct commit
*handle_commit(struct rev_info
*revs
,
268 struct object
*object
, const char *name
)
270 unsigned long flags
= object
->flags
;
273 * Tag object? Look what it points to..
275 while (object
->type
== OBJ_TAG
) {
276 struct tag
*tag
= (struct tag
*) object
;
277 if (revs
->tag_objects
&& !(flags
& UNINTERESTING
))
278 add_pending_object(revs
, object
, tag
->tag
);
281 object
= parse_object(tag
->tagged
->sha1
);
283 if (flags
& UNINTERESTING
)
285 die("bad object %s", sha1_to_hex(tag
->tagged
->sha1
));
287 object
->flags
|= flags
;
291 * Commit object? Just return it, we'll do all the complex
294 if (object
->type
== OBJ_COMMIT
) {
295 struct commit
*commit
= (struct commit
*)object
;
296 if (parse_commit(commit
) < 0)
297 die("unable to parse commit %s", name
);
298 if (flags
& UNINTERESTING
) {
299 mark_parents_uninteresting(commit
);
302 if (revs
->show_source
&& !commit
->util
)
303 commit
->util
= (void *) name
;
308 * Tree object? Either mark it uninteresting, or add it
309 * to the list of objects to look at later..
311 if (object
->type
== OBJ_TREE
) {
312 struct tree
*tree
= (struct tree
*)object
;
313 if (!revs
->tree_objects
)
315 if (flags
& UNINTERESTING
) {
316 mark_tree_contents_uninteresting(tree
);
319 add_pending_object(revs
, object
, "");
324 * Blob object? You know the drill by now..
326 if (object
->type
== OBJ_BLOB
) {
327 if (!revs
->blob_objects
)
329 if (flags
& UNINTERESTING
)
331 add_pending_object(revs
, object
, "");
334 die("%s is unknown object", name
);
337 static int everybody_uninteresting(struct commit_list
*orig
)
339 struct commit_list
*list
= orig
;
341 struct commit
*commit
= list
->item
;
343 if (commit
->object
.flags
& UNINTERESTING
)
351 * A definition of "relevant" commit that we can use to simplify limited graphs
352 * by eliminating side branches.
354 * A "relevant" commit is one that is !UNINTERESTING (ie we are including it
355 * in our list), or that is a specified BOTTOM commit. Then after computing
356 * a limited list, during processing we can generally ignore boundary merges
357 * coming from outside the graph, (ie from irrelevant parents), and treat
358 * those merges as if they were single-parent. TREESAME is defined to consider
359 * only relevant parents, if any. If we are TREESAME to our on-graph parents,
360 * we don't care if we were !TREESAME to non-graph parents.
362 * Treating bottom commits as relevant ensures that a limited graph's
363 * connection to the actual bottom commit is not viewed as a side branch, but
364 * treated as part of the graph. For example:
366 * ....Z...A---X---o---o---B
370 * When computing "A..B", the A-X connection is at least as important as
371 * Y-X, despite A being flagged UNINTERESTING.
373 * And when computing --ancestry-path "A..B", the A-X connection is more
374 * important than Y-X, despite both A and Y being flagged UNINTERESTING.
376 static inline int relevant_commit(struct commit
*commit
)
378 return (commit
->object
.flags
& (UNINTERESTING
| BOTTOM
)) != UNINTERESTING
;
382 * Return a single relevant commit from a parent list. If we are a TREESAME
383 * commit, and this selects one of our parents, then we can safely simplify to
386 static struct commit
*one_relevant_parent(const struct rev_info
*revs
,
387 struct commit_list
*orig
)
389 struct commit_list
*list
= orig
;
390 struct commit
*relevant
= NULL
;
396 * For 1-parent commits, or if first-parent-only, then return that
397 * first parent (even if not "relevant" by the above definition).
398 * TREESAME will have been set purely on that parent.
400 if (revs
->first_parent_only
|| !orig
->next
)
404 * For multi-parent commits, identify a sole relevant parent, if any.
405 * If we have only one relevant parent, then TREESAME will be set purely
406 * with regard to that parent, and we can simplify accordingly.
408 * If we have more than one relevant parent, or no relevant parents
409 * (and multiple irrelevant ones), then we can't select a parent here
413 struct commit
*commit
= list
->item
;
415 if (relevant_commit(commit
)) {
425 * The goal is to get REV_TREE_NEW as the result only if the
426 * diff consists of all '+' (and no other changes), REV_TREE_OLD
427 * if the whole diff is removal of old data, and otherwise
428 * REV_TREE_DIFFERENT (of course if the trees are the same we
429 * want REV_TREE_SAME).
430 * That means that once we get to REV_TREE_DIFFERENT, we do not
431 * have to look any further.
433 static int tree_difference
= REV_TREE_SAME
;
435 static void file_add_remove(struct diff_options
*options
,
436 int addremove
, unsigned mode
,
437 const unsigned char *sha1
,
439 const char *fullpath
, unsigned dirty_submodule
)
441 int diff
= addremove
== '+' ? REV_TREE_NEW
: REV_TREE_OLD
;
443 tree_difference
|= diff
;
444 if (tree_difference
== REV_TREE_DIFFERENT
)
445 DIFF_OPT_SET(options
, HAS_CHANGES
);
448 static void file_change(struct diff_options
*options
,
449 unsigned old_mode
, unsigned new_mode
,
450 const unsigned char *old_sha1
,
451 const unsigned char *new_sha1
,
452 int old_sha1_valid
, int new_sha1_valid
,
453 const char *fullpath
,
454 unsigned old_dirty_submodule
, unsigned new_dirty_submodule
)
456 tree_difference
= REV_TREE_DIFFERENT
;
457 DIFF_OPT_SET(options
, HAS_CHANGES
);
460 static int rev_compare_tree(struct rev_info
*revs
,
461 struct commit
*parent
, struct commit
*commit
)
463 struct tree
*t1
= parent
->tree
;
464 struct tree
*t2
= commit
->tree
;
471 if (revs
->simplify_by_decoration
) {
473 * If we are simplifying by decoration, then the commit
474 * is worth showing if it has a tag pointing at it.
476 if (lookup_decoration(&name_decoration
, &commit
->object
))
477 return REV_TREE_DIFFERENT
;
479 * A commit that is not pointed by a tag is uninteresting
480 * if we are not limited by path. This means that you will
481 * see the usual "commits that touch the paths" plus any
482 * tagged commit by specifying both --simplify-by-decoration
485 if (!revs
->prune_data
.nr
)
486 return REV_TREE_SAME
;
489 tree_difference
= REV_TREE_SAME
;
490 DIFF_OPT_CLR(&revs
->pruning
, HAS_CHANGES
);
491 if (diff_tree_sha1(t1
->object
.sha1
, t2
->object
.sha1
, "",
493 return REV_TREE_DIFFERENT
;
494 return tree_difference
;
497 static int rev_same_tree_as_empty(struct rev_info
*revs
, struct commit
*commit
)
500 struct tree
*t1
= commit
->tree
;
505 tree_difference
= REV_TREE_SAME
;
506 DIFF_OPT_CLR(&revs
->pruning
, HAS_CHANGES
);
507 retval
= diff_tree_sha1(NULL
, t1
->object
.sha1
, "", &revs
->pruning
);
509 return retval
>= 0 && (tree_difference
== REV_TREE_SAME
);
512 struct treesame_state
{
513 unsigned int nparents
;
514 unsigned char treesame
[FLEX_ARRAY
];
517 static struct treesame_state
*initialise_treesame(struct rev_info
*revs
, struct commit
*commit
)
519 unsigned n
= commit_list_count(commit
->parents
);
520 struct treesame_state
*st
= xcalloc(1, sizeof(*st
) + n
);
522 add_decoration(&revs
->treesame
, &commit
->object
, st
);
527 * Must be called immediately after removing the nth_parent from a commit's
528 * parent list, if we are maintaining the per-parent treesame[] decoration.
529 * This does not recalculate the master TREESAME flag - update_treesame()
530 * should be called to update it after a sequence of treesame[] modifications
531 * that may have affected it.
533 static int compact_treesame(struct rev_info
*revs
, struct commit
*commit
, unsigned nth_parent
)
535 struct treesame_state
*st
;
538 if (!commit
->parents
) {
540 * Have just removed the only parent from a non-merge.
541 * Different handling, as we lack decoration.
544 die("compact_treesame %u", nth_parent
);
545 old_same
= !!(commit
->object
.flags
& TREESAME
);
546 if (rev_same_tree_as_empty(revs
, commit
))
547 commit
->object
.flags
|= TREESAME
;
549 commit
->object
.flags
&= ~TREESAME
;
553 st
= lookup_decoration(&revs
->treesame
, &commit
->object
);
554 if (!st
|| nth_parent
>= st
->nparents
)
555 die("compact_treesame %u", nth_parent
);
557 old_same
= st
->treesame
[nth_parent
];
558 memmove(st
->treesame
+ nth_parent
,
559 st
->treesame
+ nth_parent
+ 1,
560 st
->nparents
- nth_parent
- 1);
563 * If we've just become a non-merge commit, update TREESAME
564 * immediately, and remove the no-longer-needed decoration.
565 * If still a merge, defer update until update_treesame().
567 if (--st
->nparents
== 1) {
568 if (commit
->parents
->next
)
569 die("compact_treesame parents mismatch");
570 if (st
->treesame
[0] && revs
->dense
)
571 commit
->object
.flags
|= TREESAME
;
573 commit
->object
.flags
&= ~TREESAME
;
574 free(add_decoration(&revs
->treesame
, &commit
->object
, NULL
));
580 static unsigned update_treesame(struct rev_info
*revs
, struct commit
*commit
)
582 if (commit
->parents
&& commit
->parents
->next
) {
584 struct treesame_state
*st
;
585 struct commit_list
*p
;
586 unsigned relevant_parents
;
587 unsigned relevant_change
, irrelevant_change
;
589 st
= lookup_decoration(&revs
->treesame
, &commit
->object
);
591 die("update_treesame %s", sha1_to_hex(commit
->object
.sha1
));
592 relevant_parents
= 0;
593 relevant_change
= irrelevant_change
= 0;
594 for (p
= commit
->parents
, n
= 0; p
; n
++, p
= p
->next
) {
595 if (relevant_commit(p
->item
)) {
596 relevant_change
|= !st
->treesame
[n
];
599 irrelevant_change
|= !st
->treesame
[n
];
601 if (relevant_parents
? relevant_change
: irrelevant_change
)
602 commit
->object
.flags
&= ~TREESAME
;
604 commit
->object
.flags
|= TREESAME
;
607 return commit
->object
.flags
& TREESAME
;
610 static inline int limiting_can_increase_treesame(const struct rev_info
*revs
)
613 * TREESAME is irrelevant unless prune && dense;
614 * if simplify_history is set, we can't have a mixture of TREESAME and
615 * !TREESAME INTERESTING parents (and we don't have treesame[]
616 * decoration anyway);
617 * if first_parent_only is set, then the TREESAME flag is locked
618 * against the first parent (and again we lack treesame[] decoration).
620 return revs
->prune
&& revs
->dense
&&
621 !revs
->simplify_history
&&
622 !revs
->first_parent_only
;
625 static void try_to_simplify_commit(struct rev_info
*revs
, struct commit
*commit
)
627 struct commit_list
**pp
, *parent
;
628 struct treesame_state
*ts
= NULL
;
629 int relevant_change
= 0, irrelevant_change
= 0;
630 int relevant_parents
, nth_parent
;
633 * If we don't do pruning, everything is interesting
641 if (!commit
->parents
) {
642 if (rev_same_tree_as_empty(revs
, commit
))
643 commit
->object
.flags
|= TREESAME
;
648 * Normal non-merge commit? If we don't want to make the
649 * history dense, we consider it always to be a change..
651 if (!revs
->dense
&& !commit
->parents
->next
)
654 for (pp
= &commit
->parents
, nth_parent
= 0, relevant_parents
= 0;
655 (parent
= *pp
) != NULL
;
656 pp
= &parent
->next
, nth_parent
++) {
657 struct commit
*p
= parent
->item
;
658 if (relevant_commit(p
))
661 if (nth_parent
== 1) {
663 * This our second loop iteration - so we now know
664 * we're dealing with a merge.
666 * Do not compare with later parents when we care only about
667 * the first parent chain, in order to avoid derailing the
668 * traversal to follow a side branch that brought everything
669 * in the path we are limited to by the pathspec.
671 if (revs
->first_parent_only
)
674 * If this will remain a potentially-simplifiable
675 * merge, remember per-parent treesame if needed.
676 * Initialise the array with the comparison from our
679 if (revs
->treesame
.name
&&
680 !revs
->simplify_history
&&
681 !(commit
->object
.flags
& UNINTERESTING
)) {
682 ts
= initialise_treesame(revs
, commit
);
683 if (!(irrelevant_change
|| relevant_change
))
687 if (parse_commit(p
) < 0)
688 die("cannot simplify commit %s (because of %s)",
689 sha1_to_hex(commit
->object
.sha1
),
690 sha1_to_hex(p
->object
.sha1
));
691 switch (rev_compare_tree(revs
, p
, commit
)) {
693 if (!revs
->simplify_history
|| !relevant_commit(p
)) {
694 /* Even if a merge with an uninteresting
695 * side branch brought the entire change
696 * we are interested in, we do not want
697 * to lose the other branches of this
698 * merge, so we just keep going.
701 ts
->treesame
[nth_parent
] = 1;
705 commit
->parents
= parent
;
706 commit
->object
.flags
|= TREESAME
;
710 if (revs
->remove_empty_trees
&&
711 rev_same_tree_as_empty(revs
, p
)) {
712 /* We are adding all the specified
713 * paths from this parent, so the
714 * history beyond this parent is not
715 * interesting. Remove its parents
716 * (they are grandparents for us).
717 * IOW, we pretend this parent is a
720 if (parse_commit(p
) < 0)
721 die("cannot simplify commit %s (invalid %s)",
722 sha1_to_hex(commit
->object
.sha1
),
723 sha1_to_hex(p
->object
.sha1
));
728 case REV_TREE_DIFFERENT
:
729 if (relevant_commit(p
))
732 irrelevant_change
= 1;
735 die("bad tree compare for commit %s", sha1_to_hex(commit
->object
.sha1
));
739 * TREESAME is straightforward for single-parent commits. For merge
740 * commits, it is most useful to define it so that "irrelevant"
741 * parents cannot make us !TREESAME - if we have any relevant
742 * parents, then we only consider TREESAMEness with respect to them,
743 * allowing irrelevant merges from uninteresting branches to be
744 * simplified away. Only if we have only irrelevant parents do we
745 * base TREESAME on them. Note that this logic is replicated in
746 * update_treesame, which should be kept in sync.
748 if (relevant_parents
? !relevant_change
: !irrelevant_change
)
749 commit
->object
.flags
|= TREESAME
;
752 static void commit_list_insert_by_date_cached(struct commit
*p
, struct commit_list
**head
,
753 struct commit_list
*cached_base
, struct commit_list
**cache
)
755 struct commit_list
*new_entry
;
757 if (cached_base
&& p
->date
< cached_base
->item
->date
)
758 new_entry
= commit_list_insert_by_date(p
, &cached_base
->next
);
760 new_entry
= commit_list_insert_by_date(p
, head
);
762 if (cache
&& (!*cache
|| p
->date
< (*cache
)->item
->date
))
766 static int add_parents_to_list(struct rev_info
*revs
, struct commit
*commit
,
767 struct commit_list
**list
, struct commit_list
**cache_ptr
)
769 struct commit_list
*parent
= commit
->parents
;
771 struct commit_list
*cached_base
= cache_ptr
? *cache_ptr
: NULL
;
773 if (commit
->object
.flags
& ADDED
)
775 commit
->object
.flags
|= ADDED
;
777 if (revs
->include_check
&&
778 !revs
->include_check(commit
, revs
->include_check_data
))
782 * If the commit is uninteresting, don't try to
783 * prune parents - we want the maximal uninteresting
786 * Normally we haven't parsed the parent
787 * yet, so we won't have a parent of a parent
788 * here. However, it may turn out that we've
789 * reached this commit some other way (where it
790 * wasn't uninteresting), in which case we need
791 * to mark its parents recursively too..
793 if (commit
->object
.flags
& UNINTERESTING
) {
795 struct commit
*p
= parent
->item
;
796 parent
= parent
->next
;
798 p
->object
.flags
|= UNINTERESTING
;
799 if (parse_commit(p
) < 0)
802 mark_parents_uninteresting(p
);
803 if (p
->object
.flags
& SEEN
)
805 p
->object
.flags
|= SEEN
;
806 commit_list_insert_by_date_cached(p
, list
, cached_base
, cache_ptr
);
812 * Ok, the commit wasn't uninteresting. Try to
813 * simplify the commit history and find the parent
814 * that has no differences in the path set if one exists.
816 try_to_simplify_commit(revs
, commit
);
821 left_flag
= (commit
->object
.flags
& SYMMETRIC_LEFT
);
823 for (parent
= commit
->parents
; parent
; parent
= parent
->next
) {
824 struct commit
*p
= parent
->item
;
826 if (parse_commit(p
) < 0)
828 if (revs
->show_source
&& !p
->util
)
829 p
->util
= commit
->util
;
830 p
->object
.flags
|= left_flag
;
831 if (!(p
->object
.flags
& SEEN
)) {
832 p
->object
.flags
|= SEEN
;
833 commit_list_insert_by_date_cached(p
, list
, cached_base
, cache_ptr
);
835 if (revs
->first_parent_only
)
841 static void cherry_pick_list(struct commit_list
*list
, struct rev_info
*revs
)
843 struct commit_list
*p
;
844 int left_count
= 0, right_count
= 0;
846 struct patch_ids ids
;
847 unsigned cherry_flag
;
849 /* First count the commits on the left and on the right */
850 for (p
= list
; p
; p
= p
->next
) {
851 struct commit
*commit
= p
->item
;
852 unsigned flags
= commit
->object
.flags
;
853 if (flags
& BOUNDARY
)
855 else if (flags
& SYMMETRIC_LEFT
)
861 if (!left_count
|| !right_count
)
864 left_first
= left_count
< right_count
;
865 init_patch_ids(&ids
);
866 ids
.diffopts
.pathspec
= revs
->diffopt
.pathspec
;
868 /* Compute patch-ids for one side */
869 for (p
= list
; p
; p
= p
->next
) {
870 struct commit
*commit
= p
->item
;
871 unsigned flags
= commit
->object
.flags
;
873 if (flags
& BOUNDARY
)
876 * If we have fewer left, left_first is set and we omit
877 * commits on the right branch in this loop. If we have
878 * fewer right, we skip the left ones.
880 if (left_first
!= !!(flags
& SYMMETRIC_LEFT
))
882 commit
->util
= add_commit_patch_id(commit
, &ids
);
885 /* either cherry_mark or cherry_pick are true */
886 cherry_flag
= revs
->cherry_mark
? PATCHSAME
: SHOWN
;
888 /* Check the other side */
889 for (p
= list
; p
; p
= p
->next
) {
890 struct commit
*commit
= p
->item
;
892 unsigned flags
= commit
->object
.flags
;
894 if (flags
& BOUNDARY
)
897 * If we have fewer left, left_first is set and we omit
898 * commits on the left branch in this loop.
900 if (left_first
== !!(flags
& SYMMETRIC_LEFT
))
904 * Have we seen the same patch id?
906 id
= has_commit_patch_id(commit
, &ids
);
910 commit
->object
.flags
|= cherry_flag
;
913 /* Now check the original side for seen ones */
914 for (p
= list
; p
; p
= p
->next
) {
915 struct commit
*commit
= p
->item
;
916 struct patch_id
*ent
;
922 commit
->object
.flags
|= cherry_flag
;
926 free_patch_ids(&ids
);
929 /* How many extra uninteresting commits we want to see.. */
932 static int still_interesting(struct commit_list
*src
, unsigned long date
, int slop
)
935 * No source list at all? We're definitely done..
941 * Does the destination list contain entries with a date
942 * before the source list? Definitely _not_ done.
944 if (date
<= src
->item
->date
)
948 * Does the source list still have interesting commits in
949 * it? Definitely not done..
951 if (!everybody_uninteresting(src
))
954 /* Ok, we're closing in.. */
959 * "rev-list --ancestry-path A..B" computes commits that are ancestors
960 * of B but not ancestors of A but further limits the result to those
961 * that are descendants of A. This takes the list of bottom commits and
962 * the result of "A..B" without --ancestry-path, and limits the latter
963 * further to the ones that can reach one of the commits in "bottom".
965 static void limit_to_ancestry(struct commit_list
*bottom
, struct commit_list
*list
)
967 struct commit_list
*p
;
968 struct commit_list
*rlist
= NULL
;
972 * Reverse the list so that it will be likely that we would
973 * process parents before children.
975 for (p
= list
; p
; p
= p
->next
)
976 commit_list_insert(p
->item
, &rlist
);
978 for (p
= bottom
; p
; p
= p
->next
)
979 p
->item
->object
.flags
|= TMP_MARK
;
982 * Mark the ones that can reach bottom commits in "list",
983 * in a bottom-up fashion.
987 for (p
= rlist
; p
; p
= p
->next
) {
988 struct commit
*c
= p
->item
;
989 struct commit_list
*parents
;
990 if (c
->object
.flags
& (TMP_MARK
| UNINTERESTING
))
992 for (parents
= c
->parents
;
994 parents
= parents
->next
) {
995 if (!(parents
->item
->object
.flags
& TMP_MARK
))
997 c
->object
.flags
|= TMP_MARK
;
1002 } while (made_progress
);
1005 * NEEDSWORK: decide if we want to remove parents that are
1006 * not marked with TMP_MARK from commit->parents for commits
1007 * in the resulting list. We may not want to do that, though.
1011 * The ones that are not marked with TMP_MARK are uninteresting
1013 for (p
= list
; p
; p
= p
->next
) {
1014 struct commit
*c
= p
->item
;
1015 if (c
->object
.flags
& TMP_MARK
)
1017 c
->object
.flags
|= UNINTERESTING
;
1020 /* We are done with the TMP_MARK */
1021 for (p
= list
; p
; p
= p
->next
)
1022 p
->item
->object
.flags
&= ~TMP_MARK
;
1023 for (p
= bottom
; p
; p
= p
->next
)
1024 p
->item
->object
.flags
&= ~TMP_MARK
;
1025 free_commit_list(rlist
);
1029 * Before walking the history, keep the set of "negative" refs the
1030 * caller has asked to exclude.
1032 * This is used to compute "rev-list --ancestry-path A..B", as we need
1033 * to filter the result of "A..B" further to the ones that can actually
1036 static struct commit_list
*collect_bottom_commits(struct commit_list
*list
)
1038 struct commit_list
*elem
, *bottom
= NULL
;
1039 for (elem
= list
; elem
; elem
= elem
->next
)
1040 if (elem
->item
->object
.flags
& BOTTOM
)
1041 commit_list_insert(elem
->item
, &bottom
);
1045 /* Assumes either left_only or right_only is set */
1046 static void limit_left_right(struct commit_list
*list
, struct rev_info
*revs
)
1048 struct commit_list
*p
;
1050 for (p
= list
; p
; p
= p
->next
) {
1051 struct commit
*commit
= p
->item
;
1053 if (revs
->right_only
) {
1054 if (commit
->object
.flags
& SYMMETRIC_LEFT
)
1055 commit
->object
.flags
|= SHOWN
;
1056 } else /* revs->left_only is set */
1057 if (!(commit
->object
.flags
& SYMMETRIC_LEFT
))
1058 commit
->object
.flags
|= SHOWN
;
1062 static int limit_list(struct rev_info
*revs
)
1065 unsigned long date
= ~0ul;
1066 struct commit_list
*list
= revs
->commits
;
1067 struct commit_list
*newlist
= NULL
;
1068 struct commit_list
**p
= &newlist
;
1069 struct commit_list
*bottom
= NULL
;
1071 if (revs
->ancestry_path
) {
1072 bottom
= collect_bottom_commits(list
);
1074 die("--ancestry-path given but there are no bottom commits");
1078 struct commit_list
*entry
= list
;
1079 struct commit
*commit
= list
->item
;
1080 struct object
*obj
= &commit
->object
;
1081 show_early_output_fn_t show
;
1086 if (revs
->max_age
!= -1 && (commit
->date
< revs
->max_age
))
1087 obj
->flags
|= UNINTERESTING
;
1088 if (add_parents_to_list(revs
, commit
, &list
, NULL
) < 0)
1090 if (obj
->flags
& UNINTERESTING
) {
1091 mark_parents_uninteresting(commit
);
1093 p
= &commit_list_insert(commit
, p
)->next
;
1094 slop
= still_interesting(list
, date
, slop
);
1097 /* If showing all, add the whole pending list to the end */
1102 if (revs
->min_age
!= -1 && (commit
->date
> revs
->min_age
))
1104 date
= commit
->date
;
1105 p
= &commit_list_insert(commit
, p
)->next
;
1107 show
= show_early_output
;
1111 show(revs
, newlist
);
1112 show_early_output
= NULL
;
1114 if (revs
->cherry_pick
|| revs
->cherry_mark
)
1115 cherry_pick_list(newlist
, revs
);
1117 if (revs
->left_only
|| revs
->right_only
)
1118 limit_left_right(newlist
, revs
);
1121 limit_to_ancestry(bottom
, newlist
);
1122 free_commit_list(bottom
);
1126 * Check if any commits have become TREESAME by some of their parents
1127 * becoming UNINTERESTING.
1129 if (limiting_can_increase_treesame(revs
))
1130 for (list
= newlist
; list
; list
= list
->next
) {
1131 struct commit
*c
= list
->item
;
1132 if (c
->object
.flags
& (UNINTERESTING
| TREESAME
))
1134 update_treesame(revs
, c
);
1137 revs
->commits
= newlist
;
1142 * Add an entry to refs->cmdline with the specified information.
1145 static void add_rev_cmdline(struct rev_info
*revs
,
1146 struct object
*item
,
1151 struct rev_cmdline_info
*info
= &revs
->cmdline
;
1154 ALLOC_GROW(info
->rev
, nr
+ 1, info
->alloc
);
1155 info
->rev
[nr
].item
= item
;
1156 info
->rev
[nr
].name
= xstrdup(name
);
1157 info
->rev
[nr
].whence
= whence
;
1158 info
->rev
[nr
].flags
= flags
;
1162 static void add_rev_cmdline_list(struct rev_info
*revs
,
1163 struct commit_list
*commit_list
,
1167 while (commit_list
) {
1168 struct object
*object
= &commit_list
->item
->object
;
1169 add_rev_cmdline(revs
, object
, sha1_to_hex(object
->sha1
),
1171 commit_list
= commit_list
->next
;
1175 struct all_refs_cb
{
1177 int warned_bad_reflog
;
1178 struct rev_info
*all_revs
;
1179 const char *name_for_errormsg
;
1182 int ref_excluded(struct string_list
*ref_excludes
, const char *path
)
1184 struct string_list_item
*item
;
1188 for_each_string_list_item(item
, ref_excludes
) {
1189 if (!wildmatch(item
->string
, path
, 0, NULL
))
1195 static int handle_one_ref(const char *path
, const unsigned char *sha1
, int flag
, void *cb_data
)
1197 struct all_refs_cb
*cb
= cb_data
;
1198 struct object
*object
;
1200 if (ref_excluded(cb
->all_revs
->ref_excludes
, path
))
1203 object
= get_reference(cb
->all_revs
, path
, sha1
, cb
->all_flags
);
1204 add_rev_cmdline(cb
->all_revs
, object
, path
, REV_CMD_REF
, cb
->all_flags
);
1205 add_pending_sha1(cb
->all_revs
, path
, sha1
, cb
->all_flags
);
1209 static void init_all_refs_cb(struct all_refs_cb
*cb
, struct rev_info
*revs
,
1212 cb
->all_revs
= revs
;
1213 cb
->all_flags
= flags
;
1216 void clear_ref_exclusion(struct string_list
**ref_excludes_p
)
1218 if (*ref_excludes_p
) {
1219 string_list_clear(*ref_excludes_p
, 0);
1220 free(*ref_excludes_p
);
1222 *ref_excludes_p
= NULL
;
1225 void add_ref_exclusion(struct string_list
**ref_excludes_p
, const char *exclude
)
1227 if (!*ref_excludes_p
) {
1228 *ref_excludes_p
= xcalloc(1, sizeof(**ref_excludes_p
));
1229 (*ref_excludes_p
)->strdup_strings
= 1;
1231 string_list_append(*ref_excludes_p
, exclude
);
1234 static void handle_refs(const char *submodule
, struct rev_info
*revs
, unsigned flags
,
1235 int (*for_each
)(const char *, each_ref_fn
, void *))
1237 struct all_refs_cb cb
;
1238 init_all_refs_cb(&cb
, revs
, flags
);
1239 for_each(submodule
, handle_one_ref
, &cb
);
1242 static void handle_one_reflog_commit(unsigned char *sha1
, void *cb_data
)
1244 struct all_refs_cb
*cb
= cb_data
;
1245 if (!is_null_sha1(sha1
)) {
1246 struct object
*o
= parse_object(sha1
);
1248 o
->flags
|= cb
->all_flags
;
1249 /* ??? CMDLINEFLAGS ??? */
1250 add_pending_object(cb
->all_revs
, o
, "");
1252 else if (!cb
->warned_bad_reflog
) {
1253 warning("reflog of '%s' references pruned commits",
1254 cb
->name_for_errormsg
);
1255 cb
->warned_bad_reflog
= 1;
1260 static int handle_one_reflog_ent(unsigned char *osha1
, unsigned char *nsha1
,
1261 const char *email
, unsigned long timestamp
, int tz
,
1262 const char *message
, void *cb_data
)
1264 handle_one_reflog_commit(osha1
, cb_data
);
1265 handle_one_reflog_commit(nsha1
, cb_data
);
1269 static int handle_one_reflog(const char *path
, const unsigned char *sha1
, int flag
, void *cb_data
)
1271 struct all_refs_cb
*cb
= cb_data
;
1272 cb
->warned_bad_reflog
= 0;
1273 cb
->name_for_errormsg
= path
;
1274 for_each_reflog_ent(path
, handle_one_reflog_ent
, cb_data
);
1278 static void handle_reflog(struct rev_info
*revs
, unsigned flags
)
1280 struct all_refs_cb cb
;
1282 cb
.all_flags
= flags
;
1283 for_each_reflog(handle_one_reflog
, &cb
);
1286 static int add_parents_only(struct rev_info
*revs
, const char *arg_
, int flags
)
1288 unsigned char sha1
[20];
1290 struct commit
*commit
;
1291 struct commit_list
*parents
;
1292 const char *arg
= arg_
;
1295 flags
^= UNINTERESTING
| BOTTOM
;
1298 if (get_sha1_committish(arg
, sha1
))
1301 it
= get_reference(revs
, arg
, sha1
, 0);
1302 if (!it
&& revs
->ignore_missing
)
1304 if (it
->type
!= OBJ_TAG
)
1306 if (!((struct tag
*)it
)->tagged
)
1308 hashcpy(sha1
, ((struct tag
*)it
)->tagged
->sha1
);
1310 if (it
->type
!= OBJ_COMMIT
)
1312 commit
= (struct commit
*)it
;
1313 for (parents
= commit
->parents
; parents
; parents
= parents
->next
) {
1314 it
= &parents
->item
->object
;
1316 add_rev_cmdline(revs
, it
, arg_
, REV_CMD_PARENTS_ONLY
, flags
);
1317 add_pending_object(revs
, it
, arg
);
1322 void init_revisions(struct rev_info
*revs
, const char *prefix
)
1324 memset(revs
, 0, sizeof(*revs
));
1326 revs
->abbrev
= DEFAULT_ABBREV
;
1327 revs
->ignore_merges
= 1;
1328 revs
->simplify_history
= 1;
1329 DIFF_OPT_SET(&revs
->pruning
, RECURSIVE
);
1330 DIFF_OPT_SET(&revs
->pruning
, QUICK
);
1331 revs
->pruning
.add_remove
= file_add_remove
;
1332 revs
->pruning
.change
= file_change
;
1333 revs
->sort_order
= REV_SORT_IN_GRAPH_ORDER
;
1335 revs
->prefix
= prefix
;
1338 revs
->skip_count
= -1;
1339 revs
->max_count
= -1;
1340 revs
->max_parents
= -1;
1342 revs
->commit_format
= CMIT_FMT_DEFAULT
;
1344 init_grep_defaults();
1345 grep_init(&revs
->grep_filter
, prefix
);
1346 revs
->grep_filter
.status_only
= 1;
1347 revs
->grep_filter
.regflags
= REG_NEWLINE
;
1349 diff_setup(&revs
->diffopt
);
1350 if (prefix
&& !revs
->diffopt
.prefix
) {
1351 revs
->diffopt
.prefix
= prefix
;
1352 revs
->diffopt
.prefix_length
= strlen(prefix
);
1355 revs
->notes_opt
.use_default_notes
= -1;
1358 static void add_pending_commit_list(struct rev_info
*revs
,
1359 struct commit_list
*commit_list
,
1362 while (commit_list
) {
1363 struct object
*object
= &commit_list
->item
->object
;
1364 object
->flags
|= flags
;
1365 add_pending_object(revs
, object
, sha1_to_hex(object
->sha1
));
1366 commit_list
= commit_list
->next
;
1370 static void prepare_show_merge(struct rev_info
*revs
)
1372 struct commit_list
*bases
;
1373 struct commit
*head
, *other
;
1374 unsigned char sha1
[20];
1375 const char **prune
= NULL
;
1376 int i
, prune_num
= 1; /* counting terminating NULL */
1378 if (get_sha1("HEAD", sha1
))
1379 die("--merge without HEAD?");
1380 head
= lookup_commit_or_die(sha1
, "HEAD");
1381 if (get_sha1("MERGE_HEAD", sha1
))
1382 die("--merge without MERGE_HEAD?");
1383 other
= lookup_commit_or_die(sha1
, "MERGE_HEAD");
1384 add_pending_object(revs
, &head
->object
, "HEAD");
1385 add_pending_object(revs
, &other
->object
, "MERGE_HEAD");
1386 bases
= get_merge_bases(head
, other
, 1);
1387 add_rev_cmdline_list(revs
, bases
, REV_CMD_MERGE_BASE
, UNINTERESTING
| BOTTOM
);
1388 add_pending_commit_list(revs
, bases
, UNINTERESTING
| BOTTOM
);
1389 free_commit_list(bases
);
1390 head
->object
.flags
|= SYMMETRIC_LEFT
;
1394 for (i
= 0; i
< active_nr
; i
++) {
1395 const struct cache_entry
*ce
= active_cache
[i
];
1398 if (ce_path_match(ce
, &revs
->prune_data
, NULL
)) {
1400 prune
= xrealloc(prune
, sizeof(*prune
) * prune_num
);
1401 prune
[prune_num
-2] = ce
->name
;
1402 prune
[prune_num
-1] = NULL
;
1404 while ((i
+1 < active_nr
) &&
1405 ce_same_name(ce
, active_cache
[i
+1]))
1408 free_pathspec(&revs
->prune_data
);
1409 parse_pathspec(&revs
->prune_data
, PATHSPEC_ALL_MAGIC
& ~PATHSPEC_LITERAL
,
1410 PATHSPEC_PREFER_FULL
| PATHSPEC_LITERAL_PATH
, "", prune
);
1414 int handle_revision_arg(const char *arg_
, struct rev_info
*revs
, int flags
, unsigned revarg_opt
)
1416 struct object_context oc
;
1418 struct object
*object
;
1419 unsigned char sha1
[20];
1421 const char *arg
= arg_
;
1422 int cant_be_filename
= revarg_opt
& REVARG_CANNOT_BE_FILENAME
;
1423 unsigned get_sha1_flags
= 0;
1425 flags
= flags
& UNINTERESTING
? flags
| BOTTOM
: flags
& ~BOTTOM
;
1427 dotdot
= strstr(arg
, "..");
1429 unsigned char from_sha1
[20];
1430 const char *next
= dotdot
+ 2;
1431 const char *this = arg
;
1432 int symmetric
= *next
== '.';
1433 unsigned int flags_exclude
= flags
^ (UNINTERESTING
| BOTTOM
);
1434 static const char head_by_default
[] = "HEAD";
1435 unsigned int a_flags
;
1441 next
= head_by_default
;
1443 this = head_by_default
;
1444 if (this == head_by_default
&& next
== head_by_default
&&
1447 * Just ".."? That is not a range but the
1448 * pathspec for the parent directory.
1450 if (!cant_be_filename
) {
1455 if (!get_sha1_committish(this, from_sha1
) &&
1456 !get_sha1_committish(next
, sha1
)) {
1457 struct object
*a_obj
, *b_obj
;
1459 if (!cant_be_filename
) {
1461 verify_non_filename(revs
->prefix
, arg
);
1464 a_obj
= parse_object(from_sha1
);
1465 b_obj
= parse_object(sha1
);
1466 if (!a_obj
|| !b_obj
) {
1468 if (revs
->ignore_missing
)
1471 ? "Invalid symmetric difference expression %s"
1472 : "Invalid revision range %s", arg
);
1477 a_flags
= flags_exclude
;
1479 /* A...B -- find merge bases between the two */
1480 struct commit
*a
, *b
;
1481 struct commit_list
*exclude
;
1483 a
= (a_obj
->type
== OBJ_COMMIT
1484 ? (struct commit
*)a_obj
1485 : lookup_commit_reference(a_obj
->sha1
));
1486 b
= (b_obj
->type
== OBJ_COMMIT
1487 ? (struct commit
*)b_obj
1488 : lookup_commit_reference(b_obj
->sha1
));
1491 exclude
= get_merge_bases(a
, b
, 1);
1492 add_rev_cmdline_list(revs
, exclude
,
1495 add_pending_commit_list(revs
, exclude
,
1497 free_commit_list(exclude
);
1499 a_flags
= flags
| SYMMETRIC_LEFT
;
1502 a_obj
->flags
|= a_flags
;
1503 b_obj
->flags
|= flags
;
1504 add_rev_cmdline(revs
, a_obj
, this,
1505 REV_CMD_LEFT
, a_flags
);
1506 add_rev_cmdline(revs
, b_obj
, next
,
1507 REV_CMD_RIGHT
, flags
);
1508 add_pending_object(revs
, a_obj
, this);
1509 add_pending_object(revs
, b_obj
, next
);
1514 dotdot
= strstr(arg
, "^@");
1515 if (dotdot
&& !dotdot
[2]) {
1517 if (add_parents_only(revs
, arg
, flags
))
1521 dotdot
= strstr(arg
, "^!");
1522 if (dotdot
&& !dotdot
[2]) {
1524 if (!add_parents_only(revs
, arg
, flags
^ (UNINTERESTING
| BOTTOM
)))
1530 local_flags
= UNINTERESTING
| BOTTOM
;
1534 if (revarg_opt
& REVARG_COMMITTISH
)
1535 get_sha1_flags
= GET_SHA1_COMMITTISH
;
1537 if (get_sha1_with_context(arg
, get_sha1_flags
, sha1
, &oc
))
1538 return revs
->ignore_missing
? 0 : -1;
1539 if (!cant_be_filename
)
1540 verify_non_filename(revs
->prefix
, arg
);
1541 object
= get_reference(revs
, arg
, sha1
, flags
^ local_flags
);
1542 add_rev_cmdline(revs
, object
, arg_
, REV_CMD_REV
, flags
^ local_flags
);
1543 add_pending_object_with_mode(revs
, object
, arg
, oc
.mode
);
1547 struct cmdline_pathspec
{
1553 static void append_prune_data(struct cmdline_pathspec
*prune
, const char **av
)
1556 ALLOC_GROW(prune
->path
, prune
->nr
+ 1, prune
->alloc
);
1557 prune
->path
[prune
->nr
++] = *(av
++);
1561 static void read_pathspec_from_stdin(struct rev_info
*revs
, struct strbuf
*sb
,
1562 struct cmdline_pathspec
*prune
)
1564 while (strbuf_getwholeline(sb
, stdin
, '\n') != EOF
) {
1566 if (len
&& sb
->buf
[len
- 1] == '\n')
1567 sb
->buf
[--len
] = '\0';
1568 ALLOC_GROW(prune
->path
, prune
->nr
+ 1, prune
->alloc
);
1569 prune
->path
[prune
->nr
++] = xstrdup(sb
->buf
);
1573 static void read_revisions_from_stdin(struct rev_info
*revs
,
1574 struct cmdline_pathspec
*prune
)
1577 int seen_dashdash
= 0;
1580 save_warning
= warn_on_object_refname_ambiguity
;
1581 warn_on_object_refname_ambiguity
= 0;
1583 strbuf_init(&sb
, 1000);
1584 while (strbuf_getwholeline(&sb
, stdin
, '\n') != EOF
) {
1586 if (len
&& sb
.buf
[len
- 1] == '\n')
1587 sb
.buf
[--len
] = '\0';
1590 if (sb
.buf
[0] == '-') {
1591 if (len
== 2 && sb
.buf
[1] == '-') {
1595 die("options not supported in --stdin mode");
1597 if (handle_revision_arg(sb
.buf
, revs
, 0,
1598 REVARG_CANNOT_BE_FILENAME
))
1599 die("bad revision '%s'", sb
.buf
);
1602 read_pathspec_from_stdin(revs
, &sb
, prune
);
1604 strbuf_release(&sb
);
1605 warn_on_object_refname_ambiguity
= save_warning
;
1608 static void add_grep(struct rev_info
*revs
, const char *ptn
, enum grep_pat_token what
)
1610 append_grep_pattern(&revs
->grep_filter
, ptn
, "command line", 0, what
);
1613 static void add_header_grep(struct rev_info
*revs
, enum grep_header_field field
, const char *pattern
)
1615 append_header_grep_pattern(&revs
->grep_filter
, field
, pattern
);
1618 static void add_message_grep(struct rev_info
*revs
, const char *pattern
)
1620 add_grep(revs
, pattern
, GREP_PATTERN_BODY
);
1623 static int handle_revision_opt(struct rev_info
*revs
, int argc
, const char **argv
,
1624 int *unkc
, const char **unkv
)
1626 const char *arg
= argv
[0];
1630 /* pseudo revision arguments */
1631 if (!strcmp(arg
, "--all") || !strcmp(arg
, "--branches") ||
1632 !strcmp(arg
, "--tags") || !strcmp(arg
, "--remotes") ||
1633 !strcmp(arg
, "--reflog") || !strcmp(arg
, "--not") ||
1634 !strcmp(arg
, "--no-walk") || !strcmp(arg
, "--do-walk") ||
1635 !strcmp(arg
, "--bisect") || starts_with(arg
, "--glob=") ||
1636 starts_with(arg
, "--branches=") || starts_with(arg
, "--tags=") ||
1637 starts_with(arg
, "--remotes=") || starts_with(arg
, "--no-walk="))
1639 unkv
[(*unkc
)++] = arg
;
1643 if ((argcount
= parse_long_opt("max-count", argv
, &optarg
))) {
1644 revs
->max_count
= atoi(optarg
);
1647 } else if ((argcount
= parse_long_opt("skip", argv
, &optarg
))) {
1648 revs
->skip_count
= atoi(optarg
);
1650 } else if ((*arg
== '-') && isdigit(arg
[1])) {
1651 /* accept -<digit>, like traditional "head" */
1652 revs
->max_count
= atoi(arg
+ 1);
1654 } else if (!strcmp(arg
, "-n")) {
1656 return error("-n requires an argument");
1657 revs
->max_count
= atoi(argv
[1]);
1660 } else if (starts_with(arg
, "-n")) {
1661 revs
->max_count
= atoi(arg
+ 2);
1663 } else if ((argcount
= parse_long_opt("max-age", argv
, &optarg
))) {
1664 revs
->max_age
= atoi(optarg
);
1666 } else if ((argcount
= parse_long_opt("since", argv
, &optarg
))) {
1667 revs
->max_age
= approxidate(optarg
);
1669 } else if ((argcount
= parse_long_opt("after", argv
, &optarg
))) {
1670 revs
->max_age
= approxidate(optarg
);
1672 } else if ((argcount
= parse_long_opt("min-age", argv
, &optarg
))) {
1673 revs
->min_age
= atoi(optarg
);
1675 } else if ((argcount
= parse_long_opt("before", argv
, &optarg
))) {
1676 revs
->min_age
= approxidate(optarg
);
1678 } else if ((argcount
= parse_long_opt("until", argv
, &optarg
))) {
1679 revs
->min_age
= approxidate(optarg
);
1681 } else if (!strcmp(arg
, "--first-parent")) {
1682 revs
->first_parent_only
= 1;
1683 } else if (!strcmp(arg
, "--ancestry-path")) {
1684 revs
->ancestry_path
= 1;
1685 revs
->simplify_history
= 0;
1687 } else if (!strcmp(arg
, "-g") || !strcmp(arg
, "--walk-reflogs")) {
1688 init_reflog_walk(&revs
->reflog_info
);
1689 } else if (!strcmp(arg
, "--default")) {
1691 return error("bad --default argument");
1692 revs
->def
= argv
[1];
1694 } else if (!strcmp(arg
, "--merge")) {
1695 revs
->show_merge
= 1;
1696 } else if (!strcmp(arg
, "--topo-order")) {
1697 revs
->sort_order
= REV_SORT_IN_GRAPH_ORDER
;
1698 revs
->topo_order
= 1;
1699 } else if (!strcmp(arg
, "--simplify-merges")) {
1700 revs
->simplify_merges
= 1;
1701 revs
->topo_order
= 1;
1702 revs
->rewrite_parents
= 1;
1703 revs
->simplify_history
= 0;
1705 } else if (!strcmp(arg
, "--simplify-by-decoration")) {
1706 revs
->simplify_merges
= 1;
1707 revs
->topo_order
= 1;
1708 revs
->rewrite_parents
= 1;
1709 revs
->simplify_history
= 0;
1710 revs
->simplify_by_decoration
= 1;
1713 load_ref_decorations(DECORATE_SHORT_REFS
);
1714 } else if (!strcmp(arg
, "--date-order")) {
1715 revs
->sort_order
= REV_SORT_BY_COMMIT_DATE
;
1716 revs
->topo_order
= 1;
1717 } else if (!strcmp(arg
, "--author-date-order")) {
1718 revs
->sort_order
= REV_SORT_BY_AUTHOR_DATE
;
1719 revs
->topo_order
= 1;
1720 } else if (starts_with(arg
, "--early-output")) {
1724 count
= atoi(arg
+15);
1727 revs
->topo_order
= 1;
1728 revs
->early_output
= count
;
1730 } else if (!strcmp(arg
, "--parents")) {
1731 revs
->rewrite_parents
= 1;
1732 revs
->print_parents
= 1;
1733 } else if (!strcmp(arg
, "--dense")) {
1735 } else if (!strcmp(arg
, "--sparse")) {
1737 } else if (!strcmp(arg
, "--show-all")) {
1739 } else if (!strcmp(arg
, "--remove-empty")) {
1740 revs
->remove_empty_trees
= 1;
1741 } else if (!strcmp(arg
, "--merges")) {
1742 revs
->min_parents
= 2;
1743 } else if (!strcmp(arg
, "--no-merges")) {
1744 revs
->max_parents
= 1;
1745 } else if (starts_with(arg
, "--min-parents=")) {
1746 revs
->min_parents
= atoi(arg
+14);
1747 } else if (starts_with(arg
, "--no-min-parents")) {
1748 revs
->min_parents
= 0;
1749 } else if (starts_with(arg
, "--max-parents=")) {
1750 revs
->max_parents
= atoi(arg
+14);
1751 } else if (starts_with(arg
, "--no-max-parents")) {
1752 revs
->max_parents
= -1;
1753 } else if (!strcmp(arg
, "--boundary")) {
1755 } else if (!strcmp(arg
, "--left-right")) {
1756 revs
->left_right
= 1;
1757 } else if (!strcmp(arg
, "--left-only")) {
1758 if (revs
->right_only
)
1759 die("--left-only is incompatible with --right-only"
1761 revs
->left_only
= 1;
1762 } else if (!strcmp(arg
, "--right-only")) {
1763 if (revs
->left_only
)
1764 die("--right-only is incompatible with --left-only");
1765 revs
->right_only
= 1;
1766 } else if (!strcmp(arg
, "--cherry")) {
1767 if (revs
->left_only
)
1768 die("--cherry is incompatible with --left-only");
1769 revs
->cherry_mark
= 1;
1770 revs
->right_only
= 1;
1771 revs
->max_parents
= 1;
1773 } else if (!strcmp(arg
, "--count")) {
1775 } else if (!strcmp(arg
, "--cherry-mark")) {
1776 if (revs
->cherry_pick
)
1777 die("--cherry-mark is incompatible with --cherry-pick");
1778 revs
->cherry_mark
= 1;
1779 revs
->limited
= 1; /* needs limit_list() */
1780 } else if (!strcmp(arg
, "--cherry-pick")) {
1781 if (revs
->cherry_mark
)
1782 die("--cherry-pick is incompatible with --cherry-mark");
1783 revs
->cherry_pick
= 1;
1785 } else if (!strcmp(arg
, "--objects")) {
1786 revs
->tag_objects
= 1;
1787 revs
->tree_objects
= 1;
1788 revs
->blob_objects
= 1;
1789 } else if (!strcmp(arg
, "--objects-edge")) {
1790 revs
->tag_objects
= 1;
1791 revs
->tree_objects
= 1;
1792 revs
->blob_objects
= 1;
1793 revs
->edge_hint
= 1;
1794 } else if (!strcmp(arg
, "--verify-objects")) {
1795 revs
->tag_objects
= 1;
1796 revs
->tree_objects
= 1;
1797 revs
->blob_objects
= 1;
1798 revs
->verify_objects
= 1;
1799 } else if (!strcmp(arg
, "--unpacked")) {
1801 } else if (starts_with(arg
, "--unpacked=")) {
1802 die("--unpacked=<packfile> no longer supported.");
1803 } else if (!strcmp(arg
, "-r")) {
1805 DIFF_OPT_SET(&revs
->diffopt
, RECURSIVE
);
1806 } else if (!strcmp(arg
, "-t")) {
1808 DIFF_OPT_SET(&revs
->diffopt
, RECURSIVE
);
1809 DIFF_OPT_SET(&revs
->diffopt
, TREE_IN_RECURSIVE
);
1810 } else if (!strcmp(arg
, "-m")) {
1811 revs
->ignore_merges
= 0;
1812 } else if (!strcmp(arg
, "-c")) {
1814 revs
->dense_combined_merges
= 0;
1815 revs
->combine_merges
= 1;
1816 } else if (!strcmp(arg
, "--cc")) {
1818 revs
->dense_combined_merges
= 1;
1819 revs
->combine_merges
= 1;
1820 } else if (!strcmp(arg
, "-v")) {
1821 revs
->verbose_header
= 1;
1822 } else if (!strcmp(arg
, "--pretty")) {
1823 revs
->verbose_header
= 1;
1824 revs
->pretty_given
= 1;
1825 get_commit_format(arg
+8, revs
);
1826 } else if (starts_with(arg
, "--pretty=") || starts_with(arg
, "--format=")) {
1828 * Detached form ("--pretty X" as opposed to "--pretty=X")
1829 * not allowed, since the argument is optional.
1831 revs
->verbose_header
= 1;
1832 revs
->pretty_given
= 1;
1833 get_commit_format(arg
+9, revs
);
1834 } else if (!strcmp(arg
, "--show-notes") || !strcmp(arg
, "--notes")) {
1835 revs
->show_notes
= 1;
1836 revs
->show_notes_given
= 1;
1837 revs
->notes_opt
.use_default_notes
= 1;
1838 } else if (!strcmp(arg
, "--show-signature")) {
1839 revs
->show_signature
= 1;
1840 } else if (starts_with(arg
, "--show-notes=") ||
1841 starts_with(arg
, "--notes=")) {
1842 struct strbuf buf
= STRBUF_INIT
;
1843 revs
->show_notes
= 1;
1844 revs
->show_notes_given
= 1;
1845 if (starts_with(arg
, "--show-notes")) {
1846 if (revs
->notes_opt
.use_default_notes
< 0)
1847 revs
->notes_opt
.use_default_notes
= 1;
1848 strbuf_addstr(&buf
, arg
+13);
1851 strbuf_addstr(&buf
, arg
+8);
1852 expand_notes_ref(&buf
);
1853 string_list_append(&revs
->notes_opt
.extra_notes_refs
,
1854 strbuf_detach(&buf
, NULL
));
1855 } else if (!strcmp(arg
, "--no-notes")) {
1856 revs
->show_notes
= 0;
1857 revs
->show_notes_given
= 1;
1858 revs
->notes_opt
.use_default_notes
= -1;
1859 /* we have been strdup'ing ourselves, so trick
1860 * string_list into free()ing strings */
1861 revs
->notes_opt
.extra_notes_refs
.strdup_strings
= 1;
1862 string_list_clear(&revs
->notes_opt
.extra_notes_refs
, 0);
1863 revs
->notes_opt
.extra_notes_refs
.strdup_strings
= 0;
1864 } else if (!strcmp(arg
, "--standard-notes")) {
1865 revs
->show_notes_given
= 1;
1866 revs
->notes_opt
.use_default_notes
= 1;
1867 } else if (!strcmp(arg
, "--no-standard-notes")) {
1868 revs
->notes_opt
.use_default_notes
= 0;
1869 } else if (!strcmp(arg
, "--oneline")) {
1870 revs
->verbose_header
= 1;
1871 get_commit_format("oneline", revs
);
1872 revs
->pretty_given
= 1;
1873 revs
->abbrev_commit
= 1;
1874 } else if (!strcmp(arg
, "--graph")) {
1875 revs
->topo_order
= 1;
1876 revs
->rewrite_parents
= 1;
1877 revs
->graph
= graph_init(revs
);
1878 } else if (!strcmp(arg
, "--root")) {
1879 revs
->show_root_diff
= 1;
1880 } else if (!strcmp(arg
, "--no-commit-id")) {
1881 revs
->no_commit_id
= 1;
1882 } else if (!strcmp(arg
, "--always")) {
1883 revs
->always_show_header
= 1;
1884 } else if (!strcmp(arg
, "--no-abbrev")) {
1886 } else if (!strcmp(arg
, "--abbrev")) {
1887 revs
->abbrev
= DEFAULT_ABBREV
;
1888 } else if (starts_with(arg
, "--abbrev=")) {
1889 revs
->abbrev
= strtoul(arg
+ 9, NULL
, 10);
1890 if (revs
->abbrev
< MINIMUM_ABBREV
)
1891 revs
->abbrev
= MINIMUM_ABBREV
;
1892 else if (revs
->abbrev
> 40)
1894 } else if (!strcmp(arg
, "--abbrev-commit")) {
1895 revs
->abbrev_commit
= 1;
1896 revs
->abbrev_commit_given
= 1;
1897 } else if (!strcmp(arg
, "--no-abbrev-commit")) {
1898 revs
->abbrev_commit
= 0;
1899 } else if (!strcmp(arg
, "--full-diff")) {
1901 revs
->full_diff
= 1;
1902 } else if (!strcmp(arg
, "--full-history")) {
1903 revs
->simplify_history
= 0;
1904 } else if (!strcmp(arg
, "--relative-date")) {
1905 revs
->date_mode
= DATE_RELATIVE
;
1906 revs
->date_mode_explicit
= 1;
1907 } else if ((argcount
= parse_long_opt("date", argv
, &optarg
))) {
1908 revs
->date_mode
= parse_date_format(optarg
);
1909 revs
->date_mode_explicit
= 1;
1911 } else if (!strcmp(arg
, "--log-size")) {
1912 revs
->show_log_size
= 1;
1915 * Grepping the commit log
1917 else if ((argcount
= parse_long_opt("author", argv
, &optarg
))) {
1918 add_header_grep(revs
, GREP_HEADER_AUTHOR
, optarg
);
1920 } else if ((argcount
= parse_long_opt("committer", argv
, &optarg
))) {
1921 add_header_grep(revs
, GREP_HEADER_COMMITTER
, optarg
);
1923 } else if ((argcount
= parse_long_opt("grep-reflog", argv
, &optarg
))) {
1924 add_header_grep(revs
, GREP_HEADER_REFLOG
, optarg
);
1926 } else if ((argcount
= parse_long_opt("grep", argv
, &optarg
))) {
1927 add_message_grep(revs
, optarg
);
1929 } else if (!strcmp(arg
, "--grep-debug")) {
1930 revs
->grep_filter
.debug
= 1;
1931 } else if (!strcmp(arg
, "--basic-regexp")) {
1932 grep_set_pattern_type_option(GREP_PATTERN_TYPE_BRE
, &revs
->grep_filter
);
1933 } else if (!strcmp(arg
, "--extended-regexp") || !strcmp(arg
, "-E")) {
1934 grep_set_pattern_type_option(GREP_PATTERN_TYPE_ERE
, &revs
->grep_filter
);
1935 } else if (!strcmp(arg
, "--regexp-ignore-case") || !strcmp(arg
, "-i")) {
1936 revs
->grep_filter
.regflags
|= REG_ICASE
;
1937 DIFF_OPT_SET(&revs
->diffopt
, PICKAXE_IGNORE_CASE
);
1938 } else if (!strcmp(arg
, "--fixed-strings") || !strcmp(arg
, "-F")) {
1939 grep_set_pattern_type_option(GREP_PATTERN_TYPE_FIXED
, &revs
->grep_filter
);
1940 } else if (!strcmp(arg
, "--perl-regexp")) {
1941 grep_set_pattern_type_option(GREP_PATTERN_TYPE_PCRE
, &revs
->grep_filter
);
1942 } else if (!strcmp(arg
, "--all-match")) {
1943 revs
->grep_filter
.all_match
= 1;
1944 } else if ((argcount
= parse_long_opt("encoding", argv
, &optarg
))) {
1945 if (strcmp(optarg
, "none"))
1946 git_log_output_encoding
= xstrdup(optarg
);
1948 git_log_output_encoding
= "";
1950 } else if (!strcmp(arg
, "--reverse")) {
1952 } else if (!strcmp(arg
, "--children")) {
1953 revs
->children
.name
= "children";
1955 } else if (!strcmp(arg
, "--ignore-missing")) {
1956 revs
->ignore_missing
= 1;
1958 int opts
= diff_opt_parse(&revs
->diffopt
, argv
, argc
);
1960 unkv
[(*unkc
)++] = arg
;
1967 void parse_revision_opt(struct rev_info
*revs
, struct parse_opt_ctx_t
*ctx
,
1968 const struct option
*options
,
1969 const char * const usagestr
[])
1971 int n
= handle_revision_opt(revs
, ctx
->argc
, ctx
->argv
,
1972 &ctx
->cpidx
, ctx
->out
);
1974 error("unknown option `%s'", ctx
->argv
[0]);
1975 usage_with_options(usagestr
, options
);
1981 static int for_each_bad_bisect_ref(const char *submodule
, each_ref_fn fn
, void *cb_data
)
1983 return for_each_ref_in_submodule(submodule
, "refs/bisect/bad", fn
, cb_data
);
1986 static int for_each_good_bisect_ref(const char *submodule
, each_ref_fn fn
, void *cb_data
)
1988 return for_each_ref_in_submodule(submodule
, "refs/bisect/good", fn
, cb_data
);
1991 static int handle_revision_pseudo_opt(const char *submodule
,
1992 struct rev_info
*revs
,
1993 int argc
, const char **argv
, int *flags
)
1995 const char *arg
= argv
[0];
2002 * Commands like "git shortlog" will not accept the options below
2003 * unless parse_revision_opt queues them (as opposed to erroring
2006 * When implementing your new pseudo-option, remember to
2007 * register it in the list at the top of handle_revision_opt.
2009 if (!strcmp(arg
, "--all")) {
2010 handle_refs(submodule
, revs
, *flags
, for_each_ref_submodule
);
2011 handle_refs(submodule
, revs
, *flags
, head_ref_submodule
);
2012 clear_ref_exclusion(&revs
->ref_excludes
);
2013 } else if (!strcmp(arg
, "--branches")) {
2014 handle_refs(submodule
, revs
, *flags
, for_each_branch_ref_submodule
);
2015 clear_ref_exclusion(&revs
->ref_excludes
);
2016 } else if (!strcmp(arg
, "--bisect")) {
2017 handle_refs(submodule
, revs
, *flags
, for_each_bad_bisect_ref
);
2018 handle_refs(submodule
, revs
, *flags
^ (UNINTERESTING
| BOTTOM
), for_each_good_bisect_ref
);
2020 } else if (!strcmp(arg
, "--tags")) {
2021 handle_refs(submodule
, revs
, *flags
, for_each_tag_ref_submodule
);
2022 clear_ref_exclusion(&revs
->ref_excludes
);
2023 } else if (!strcmp(arg
, "--remotes")) {
2024 handle_refs(submodule
, revs
, *flags
, for_each_remote_ref_submodule
);
2025 clear_ref_exclusion(&revs
->ref_excludes
);
2026 } else if ((argcount
= parse_long_opt("glob", argv
, &optarg
))) {
2027 struct all_refs_cb cb
;
2028 init_all_refs_cb(&cb
, revs
, *flags
);
2029 for_each_glob_ref(handle_one_ref
, optarg
, &cb
);
2030 clear_ref_exclusion(&revs
->ref_excludes
);
2032 } else if ((argcount
= parse_long_opt("exclude", argv
, &optarg
))) {
2033 add_ref_exclusion(&revs
->ref_excludes
, optarg
);
2035 } else if (starts_with(arg
, "--branches=")) {
2036 struct all_refs_cb cb
;
2037 init_all_refs_cb(&cb
, revs
, *flags
);
2038 for_each_glob_ref_in(handle_one_ref
, arg
+ 11, "refs/heads/", &cb
);
2039 clear_ref_exclusion(&revs
->ref_excludes
);
2040 } else if (starts_with(arg
, "--tags=")) {
2041 struct all_refs_cb cb
;
2042 init_all_refs_cb(&cb
, revs
, *flags
);
2043 for_each_glob_ref_in(handle_one_ref
, arg
+ 7, "refs/tags/", &cb
);
2044 clear_ref_exclusion(&revs
->ref_excludes
);
2045 } else if (starts_with(arg
, "--remotes=")) {
2046 struct all_refs_cb cb
;
2047 init_all_refs_cb(&cb
, revs
, *flags
);
2048 for_each_glob_ref_in(handle_one_ref
, arg
+ 10, "refs/remotes/", &cb
);
2049 clear_ref_exclusion(&revs
->ref_excludes
);
2050 } else if (!strcmp(arg
, "--reflog")) {
2051 handle_reflog(revs
, *flags
);
2052 } else if (!strcmp(arg
, "--not")) {
2053 *flags
^= UNINTERESTING
| BOTTOM
;
2054 } else if (!strcmp(arg
, "--no-walk")) {
2055 revs
->no_walk
= REVISION_WALK_NO_WALK_SORTED
;
2056 } else if (starts_with(arg
, "--no-walk=")) {
2058 * Detached form ("--no-walk X" as opposed to "--no-walk=X")
2059 * not allowed, since the argument is optional.
2061 if (!strcmp(arg
+ 10, "sorted"))
2062 revs
->no_walk
= REVISION_WALK_NO_WALK_SORTED
;
2063 else if (!strcmp(arg
+ 10, "unsorted"))
2064 revs
->no_walk
= REVISION_WALK_NO_WALK_UNSORTED
;
2066 return error("invalid argument to --no-walk");
2067 } else if (!strcmp(arg
, "--do-walk")) {
2077 * Parse revision information, filling in the "rev_info" structure,
2078 * and removing the used arguments from the argument list.
2080 * Returns the number of arguments left that weren't recognized
2081 * (which are also moved to the head of the argument list)
2083 int setup_revisions(int argc
, const char **argv
, struct rev_info
*revs
, struct setup_revision_opt
*opt
)
2085 int i
, flags
, left
, seen_dashdash
, read_from_stdin
, got_rev_arg
= 0, revarg_opt
;
2086 struct cmdline_pathspec prune_data
;
2087 const char *submodule
= NULL
;
2089 memset(&prune_data
, 0, sizeof(prune_data
));
2091 submodule
= opt
->submodule
;
2093 /* First, search for "--" */
2094 if (opt
&& opt
->assume_dashdash
) {
2098 for (i
= 1; i
< argc
; i
++) {
2099 const char *arg
= argv
[i
];
2100 if (strcmp(arg
, "--"))
2105 append_prune_data(&prune_data
, argv
+ i
+ 1);
2111 /* Second, deal with arguments and options */
2113 revarg_opt
= opt
? opt
->revarg_opt
: 0;
2115 revarg_opt
|= REVARG_CANNOT_BE_FILENAME
;
2116 read_from_stdin
= 0;
2117 for (left
= i
= 1; i
< argc
; i
++) {
2118 const char *arg
= argv
[i
];
2122 opts
= handle_revision_pseudo_opt(submodule
,
2123 revs
, argc
- i
, argv
+ i
,
2130 if (!strcmp(arg
, "--stdin")) {
2131 if (revs
->disable_stdin
) {
2135 if (read_from_stdin
++)
2136 die("--stdin given twice?");
2137 read_revisions_from_stdin(revs
, &prune_data
);
2141 opts
= handle_revision_opt(revs
, argc
- i
, argv
+ i
, &left
, argv
);
2152 if (handle_revision_arg(arg
, revs
, flags
, revarg_opt
)) {
2154 if (seen_dashdash
|| *arg
== '^')
2155 die("bad revision '%s'", arg
);
2157 /* If we didn't have a "--":
2158 * (1) all filenames must exist;
2159 * (2) all rev-args must not be interpretable
2160 * as a valid filename.
2161 * but the latter we have checked in the main loop.
2163 for (j
= i
; j
< argc
; j
++)
2164 verify_filename(revs
->prefix
, argv
[j
], j
== i
);
2166 append_prune_data(&prune_data
, argv
+ i
);
2173 if (prune_data
.nr
) {
2175 * If we need to introduce the magic "a lone ':' means no
2176 * pathspec whatsoever", here is the place to do so.
2178 * if (prune_data.nr == 1 && !strcmp(prune_data[0], ":")) {
2179 * prune_data.nr = 0;
2180 * prune_data.alloc = 0;
2181 * free(prune_data.path);
2182 * prune_data.path = NULL;
2184 * terminate prune_data.alloc with NULL and
2185 * call init_pathspec() to set revs->prune_data here.
2188 ALLOC_GROW(prune_data
.path
, prune_data
.nr
+ 1, prune_data
.alloc
);
2189 prune_data
.path
[prune_data
.nr
++] = NULL
;
2190 parse_pathspec(&revs
->prune_data
, 0, 0,
2191 revs
->prefix
, prune_data
.path
);
2194 if (revs
->def
== NULL
)
2195 revs
->def
= opt
? opt
->def
: NULL
;
2196 if (opt
&& opt
->tweak
)
2197 opt
->tweak(revs
, opt
);
2198 if (revs
->show_merge
)
2199 prepare_show_merge(revs
);
2200 if (revs
->def
&& !revs
->pending
.nr
&& !got_rev_arg
) {
2201 unsigned char sha1
[20];
2202 struct object
*object
;
2203 struct object_context oc
;
2204 if (get_sha1_with_context(revs
->def
, 0, sha1
, &oc
))
2205 die("bad default revision '%s'", revs
->def
);
2206 object
= get_reference(revs
, revs
->def
, sha1
, 0);
2207 add_pending_object_with_mode(revs
, object
, revs
->def
, oc
.mode
);
2210 /* Did the user ask for any diff output? Run the diff! */
2211 if (revs
->diffopt
.output_format
& ~DIFF_FORMAT_NO_OUTPUT
)
2214 /* Pickaxe, diff-filter and rename following need diffs */
2215 if (revs
->diffopt
.pickaxe
||
2216 revs
->diffopt
.filter
||
2217 DIFF_OPT_TST(&revs
->diffopt
, FOLLOW_RENAMES
))
2220 if (revs
->topo_order
)
2223 if (revs
->prune_data
.nr
) {
2224 copy_pathspec(&revs
->pruning
.pathspec
, &revs
->prune_data
);
2225 /* Can't prune commits with rename following: the paths change.. */
2226 if (!DIFF_OPT_TST(&revs
->diffopt
, FOLLOW_RENAMES
))
2228 if (!revs
->full_diff
)
2229 copy_pathspec(&revs
->diffopt
.pathspec
,
2232 if (revs
->combine_merges
)
2233 revs
->ignore_merges
= 0;
2234 revs
->diffopt
.abbrev
= revs
->abbrev
;
2236 if (revs
->line_level_traverse
) {
2238 revs
->topo_order
= 1;
2241 diff_setup_done(&revs
->diffopt
);
2243 grep_commit_pattern_type(GREP_PATTERN_TYPE_UNSPECIFIED
,
2244 &revs
->grep_filter
);
2245 compile_grep_patterns(&revs
->grep_filter
);
2247 if (revs
->reverse
&& revs
->reflog_info
)
2248 die("cannot combine --reverse with --walk-reflogs");
2249 if (revs
->rewrite_parents
&& revs
->children
.name
)
2250 die("cannot combine --parents and --children");
2253 * Limitations on the graph functionality
2255 if (revs
->reverse
&& revs
->graph
)
2256 die("cannot combine --reverse with --graph");
2258 if (revs
->reflog_info
&& revs
->graph
)
2259 die("cannot combine --walk-reflogs with --graph");
2260 if (!revs
->reflog_info
&& revs
->grep_filter
.use_reflog_filter
)
2261 die("cannot use --grep-reflog without --walk-reflogs");
2266 static void add_child(struct rev_info
*revs
, struct commit
*parent
, struct commit
*child
)
2268 struct commit_list
*l
= xcalloc(1, sizeof(*l
));
2271 l
->next
= add_decoration(&revs
->children
, &parent
->object
, l
);
2274 static int remove_duplicate_parents(struct rev_info
*revs
, struct commit
*commit
)
2276 struct treesame_state
*ts
= lookup_decoration(&revs
->treesame
, &commit
->object
);
2277 struct commit_list
**pp
, *p
;
2278 int surviving_parents
;
2280 /* Examine existing parents while marking ones we have seen... */
2281 pp
= &commit
->parents
;
2282 surviving_parents
= 0;
2283 while ((p
= *pp
) != NULL
) {
2284 struct commit
*parent
= p
->item
;
2285 if (parent
->object
.flags
& TMP_MARK
) {
2288 compact_treesame(revs
, commit
, surviving_parents
);
2291 parent
->object
.flags
|= TMP_MARK
;
2292 surviving_parents
++;
2295 /* clear the temporary mark */
2296 for (p
= commit
->parents
; p
; p
= p
->next
) {
2297 p
->item
->object
.flags
&= ~TMP_MARK
;
2299 /* no update_treesame() - removing duplicates can't affect TREESAME */
2300 return surviving_parents
;
2303 struct merge_simplify_state
{
2304 struct commit
*simplified
;
2307 static struct merge_simplify_state
*locate_simplify_state(struct rev_info
*revs
, struct commit
*commit
)
2309 struct merge_simplify_state
*st
;
2311 st
= lookup_decoration(&revs
->merge_simplification
, &commit
->object
);
2313 st
= xcalloc(1, sizeof(*st
));
2314 add_decoration(&revs
->merge_simplification
, &commit
->object
, st
);
2319 static int mark_redundant_parents(struct rev_info
*revs
, struct commit
*commit
)
2321 struct commit_list
*h
= reduce_heads(commit
->parents
);
2322 int i
= 0, marked
= 0;
2323 struct commit_list
*po
, *pn
;
2325 /* Want these for sanity-checking only */
2326 int orig_cnt
= commit_list_count(commit
->parents
);
2327 int cnt
= commit_list_count(h
);
2330 * Not ready to remove items yet, just mark them for now, based
2331 * on the output of reduce_heads(). reduce_heads outputs the reduced
2332 * set in its original order, so this isn't too hard.
2334 po
= commit
->parents
;
2337 if (pn
&& po
->item
== pn
->item
) {
2341 po
->item
->object
.flags
|= TMP_MARK
;
2347 if (i
!= cnt
|| cnt
+marked
!= orig_cnt
)
2348 die("mark_redundant_parents %d %d %d %d", orig_cnt
, cnt
, i
, marked
);
2350 free_commit_list(h
);
2355 static int mark_treesame_root_parents(struct rev_info
*revs
, struct commit
*commit
)
2357 struct commit_list
*p
;
2360 for (p
= commit
->parents
; p
; p
= p
->next
) {
2361 struct commit
*parent
= p
->item
;
2362 if (!parent
->parents
&& (parent
->object
.flags
& TREESAME
)) {
2363 parent
->object
.flags
|= TMP_MARK
;
2372 * Awkward naming - this means one parent we are TREESAME to.
2373 * cf mark_treesame_root_parents: root parents that are TREESAME (to an
2374 * empty tree). Better name suggestions?
2376 static int leave_one_treesame_to_parent(struct rev_info
*revs
, struct commit
*commit
)
2378 struct treesame_state
*ts
= lookup_decoration(&revs
->treesame
, &commit
->object
);
2379 struct commit
*unmarked
= NULL
, *marked
= NULL
;
2380 struct commit_list
*p
;
2383 for (p
= commit
->parents
, n
= 0; p
; p
= p
->next
, n
++) {
2384 if (ts
->treesame
[n
]) {
2385 if (p
->item
->object
.flags
& TMP_MARK
) {
2398 * If we are TREESAME to a marked-for-deletion parent, but not to any
2399 * unmarked parents, unmark the first TREESAME parent. This is the
2400 * parent that the default simplify_history==1 scan would have followed,
2401 * and it doesn't make sense to omit that path when asking for a
2402 * simplified full history. Retaining it improves the chances of
2403 * understanding odd missed merges that took an old version of a file.
2407 * I--------*X A modified the file, but mainline merge X used
2408 * \ / "-s ours", so took the version from I. X is
2409 * `-*A--' TREESAME to I and !TREESAME to A.
2411 * Default log from X would produce "I". Without this check,
2412 * --full-history --simplify-merges would produce "I-A-X", showing
2413 * the merge commit X and that it changed A, but not making clear that
2414 * it had just taken the I version. With this check, the topology above
2417 * Note that it is possible that the simplification chooses a different
2418 * TREESAME parent from the default, in which case this test doesn't
2419 * activate, and we _do_ drop the default parent. Example:
2421 * I------X A modified the file, but it was reverted in B,
2422 * \ / meaning mainline merge X is TREESAME to both
2425 * Default log would produce "I" by following the first parent;
2426 * --full-history --simplify-merges will produce "I-A-B". But this is a
2427 * reasonable result - it presents a logical full history leading from
2428 * I to X, and X is not an important merge.
2430 if (!unmarked
&& marked
) {
2431 marked
->object
.flags
&= ~TMP_MARK
;
2438 static int remove_marked_parents(struct rev_info
*revs
, struct commit
*commit
)
2440 struct commit_list
**pp
, *p
;
2441 int nth_parent
, removed
= 0;
2443 pp
= &commit
->parents
;
2445 while ((p
= *pp
) != NULL
) {
2446 struct commit
*parent
= p
->item
;
2447 if (parent
->object
.flags
& TMP_MARK
) {
2448 parent
->object
.flags
&= ~TMP_MARK
;
2452 compact_treesame(revs
, commit
, nth_parent
);
2459 /* Removing parents can only increase TREESAMEness */
2460 if (removed
&& !(commit
->object
.flags
& TREESAME
))
2461 update_treesame(revs
, commit
);
2466 static struct commit_list
**simplify_one(struct rev_info
*revs
, struct commit
*commit
, struct commit_list
**tail
)
2468 struct commit_list
*p
;
2469 struct commit
*parent
;
2470 struct merge_simplify_state
*st
, *pst
;
2473 st
= locate_simplify_state(revs
, commit
);
2476 * Have we handled this one?
2482 * An UNINTERESTING commit simplifies to itself, so does a
2483 * root commit. We do not rewrite parents of such commit
2486 if ((commit
->object
.flags
& UNINTERESTING
) || !commit
->parents
) {
2487 st
->simplified
= commit
;
2492 * Do we know what commit all of our parents that matter
2493 * should be rewritten to? Otherwise we are not ready to
2494 * rewrite this one yet.
2496 for (cnt
= 0, p
= commit
->parents
; p
; p
= p
->next
) {
2497 pst
= locate_simplify_state(revs
, p
->item
);
2498 if (!pst
->simplified
) {
2499 tail
= &commit_list_insert(p
->item
, tail
)->next
;
2502 if (revs
->first_parent_only
)
2506 tail
= &commit_list_insert(commit
, tail
)->next
;
2511 * Rewrite our list of parents. Note that this cannot
2512 * affect our TREESAME flags in any way - a commit is
2513 * always TREESAME to its simplification.
2515 for (p
= commit
->parents
; p
; p
= p
->next
) {
2516 pst
= locate_simplify_state(revs
, p
->item
);
2517 p
->item
= pst
->simplified
;
2518 if (revs
->first_parent_only
)
2522 if (revs
->first_parent_only
)
2525 cnt
= remove_duplicate_parents(revs
, commit
);
2528 * It is possible that we are a merge and one side branch
2529 * does not have any commit that touches the given paths;
2530 * in such a case, the immediate parent from that branch
2531 * will be rewritten to be the merge base.
2533 * o----X X: the commit we are looking at;
2534 * / / o: a commit that touches the paths;
2537 * Further, a merge of an independent branch that doesn't
2538 * touch the path will reduce to a treesame root parent:
2540 * ----o----X X: the commit we are looking at;
2541 * / o: a commit that touches the paths;
2542 * r r: a root commit not touching the paths
2544 * Detect and simplify both cases.
2547 int marked
= mark_redundant_parents(revs
, commit
);
2548 marked
+= mark_treesame_root_parents(revs
, commit
);
2550 marked
-= leave_one_treesame_to_parent(revs
, commit
);
2552 cnt
= remove_marked_parents(revs
, commit
);
2556 * A commit simplifies to itself if it is a root, if it is
2557 * UNINTERESTING, if it touches the given paths, or if it is a
2558 * merge and its parents don't simplify to one relevant commit
2559 * (the first two cases are already handled at the beginning of
2562 * Otherwise, it simplifies to what its sole relevant parent
2566 (commit
->object
.flags
& UNINTERESTING
) ||
2567 !(commit
->object
.flags
& TREESAME
) ||
2568 (parent
= one_relevant_parent(revs
, commit
->parents
)) == NULL
)
2569 st
->simplified
= commit
;
2571 pst
= locate_simplify_state(revs
, parent
);
2572 st
->simplified
= pst
->simplified
;
2577 static void simplify_merges(struct rev_info
*revs
)
2579 struct commit_list
*list
, *next
;
2580 struct commit_list
*yet_to_do
, **tail
;
2581 struct commit
*commit
;
2586 /* feed the list reversed */
2588 for (list
= revs
->commits
; list
; list
= next
) {
2589 commit
= list
->item
;
2592 * Do not free(list) here yet; the original list
2593 * is used later in this function.
2595 commit_list_insert(commit
, &yet_to_do
);
2602 commit
= list
->item
;
2606 tail
= simplify_one(revs
, commit
, tail
);
2610 /* clean up the result, removing the simplified ones */
2611 list
= revs
->commits
;
2612 revs
->commits
= NULL
;
2613 tail
= &revs
->commits
;
2615 struct merge_simplify_state
*st
;
2617 commit
= list
->item
;
2621 st
= locate_simplify_state(revs
, commit
);
2622 if (st
->simplified
== commit
)
2623 tail
= &commit_list_insert(commit
, tail
)->next
;
2627 static void set_children(struct rev_info
*revs
)
2629 struct commit_list
*l
;
2630 for (l
= revs
->commits
; l
; l
= l
->next
) {
2631 struct commit
*commit
= l
->item
;
2632 struct commit_list
*p
;
2634 for (p
= commit
->parents
; p
; p
= p
->next
)
2635 add_child(revs
, p
->item
, commit
);
2639 void reset_revision_walk(void)
2641 clear_object_flags(SEEN
| ADDED
| SHOWN
);
2644 int prepare_revision_walk(struct rev_info
*revs
)
2646 int nr
= revs
->pending
.nr
;
2647 struct object_array_entry
*e
, *list
;
2648 struct commit_list
**next
= &revs
->commits
;
2650 e
= list
= revs
->pending
.objects
;
2651 revs
->pending
.nr
= 0;
2652 revs
->pending
.alloc
= 0;
2653 revs
->pending
.objects
= NULL
;
2655 struct commit
*commit
= handle_commit(revs
, e
->item
, e
->name
);
2657 if (!(commit
->object
.flags
& SEEN
)) {
2658 commit
->object
.flags
|= SEEN
;
2659 next
= commit_list_append(commit
, next
);
2664 if (!revs
->leak_pending
)
2667 /* Signal whether we need per-parent treesame decoration */
2668 if (revs
->simplify_merges
||
2669 (revs
->limited
&& limiting_can_increase_treesame(revs
)))
2670 revs
->treesame
.name
= "treesame";
2672 if (revs
->no_walk
!= REVISION_WALK_NO_WALK_UNSORTED
)
2673 commit_list_sort_by_date(&revs
->commits
);
2677 if (limit_list(revs
) < 0)
2679 if (revs
->topo_order
)
2680 sort_in_topological_order(&revs
->commits
, revs
->sort_order
);
2681 if (revs
->line_level_traverse
)
2682 line_log_filter(revs
);
2683 if (revs
->simplify_merges
)
2684 simplify_merges(revs
);
2685 if (revs
->children
.name
)
2690 static enum rewrite_result
rewrite_one(struct rev_info
*revs
, struct commit
**pp
)
2692 struct commit_list
*cache
= NULL
;
2695 struct commit
*p
= *pp
;
2697 if (add_parents_to_list(revs
, p
, &revs
->commits
, &cache
) < 0)
2698 return rewrite_one_error
;
2699 if (p
->object
.flags
& UNINTERESTING
)
2700 return rewrite_one_ok
;
2701 if (!(p
->object
.flags
& TREESAME
))
2702 return rewrite_one_ok
;
2704 return rewrite_one_noparents
;
2705 if ((p
= one_relevant_parent(revs
, p
->parents
)) == NULL
)
2706 return rewrite_one_ok
;
2711 int rewrite_parents(struct rev_info
*revs
, struct commit
*commit
,
2712 rewrite_parent_fn_t rewrite_parent
)
2714 struct commit_list
**pp
= &commit
->parents
;
2716 struct commit_list
*parent
= *pp
;
2717 switch (rewrite_parent(revs
, &parent
->item
)) {
2718 case rewrite_one_ok
:
2720 case rewrite_one_noparents
:
2723 case rewrite_one_error
:
2728 remove_duplicate_parents(revs
, commit
);
2732 static int commit_rewrite_person(struct strbuf
*buf
, const char *what
, struct string_list
*mailmap
)
2734 char *person
, *endp
;
2735 size_t len
, namelen
, maillen
;
2738 struct ident_split ident
;
2740 person
= strstr(buf
->buf
, what
);
2744 person
+= strlen(what
);
2745 endp
= strchr(person
, '\n');
2749 len
= endp
- person
;
2751 if (split_ident_line(&ident
, person
, len
))
2754 mail
= ident
.mail_begin
;
2755 maillen
= ident
.mail_end
- ident
.mail_begin
;
2756 name
= ident
.name_begin
;
2757 namelen
= ident
.name_end
- ident
.name_begin
;
2759 if (map_user(mailmap
, &mail
, &maillen
, &name
, &namelen
)) {
2760 struct strbuf namemail
= STRBUF_INIT
;
2762 strbuf_addf(&namemail
, "%.*s <%.*s>",
2763 (int)namelen
, name
, (int)maillen
, mail
);
2765 strbuf_splice(buf
, ident
.name_begin
- buf
->buf
,
2766 ident
.mail_end
- ident
.name_begin
+ 1,
2767 namemail
.buf
, namemail
.len
);
2769 strbuf_release(&namemail
);
2777 static int commit_match(struct commit
*commit
, struct rev_info
*opt
)
2780 const char *encoding
;
2782 struct strbuf buf
= STRBUF_INIT
;
2784 if (!opt
->grep_filter
.pattern_list
&& !opt
->grep_filter
.header_list
)
2787 /* Prepend "fake" headers as needed */
2788 if (opt
->grep_filter
.use_reflog_filter
) {
2789 strbuf_addstr(&buf
, "reflog ");
2790 get_reflog_message(&buf
, opt
->reflog_info
);
2791 strbuf_addch(&buf
, '\n');
2795 * We grep in the user's output encoding, under the assumption that it
2796 * is the encoding they are most likely to write their grep pattern
2797 * for. In addition, it means we will match the "notes" encoding below,
2798 * so we will not end up with a buffer that has two different encodings
2801 encoding
= get_log_output_encoding();
2802 message
= logmsg_reencode(commit
, NULL
, encoding
);
2804 /* Copy the commit to temporary if we are using "fake" headers */
2806 strbuf_addstr(&buf
, message
);
2808 if (opt
->grep_filter
.header_list
&& opt
->mailmap
) {
2810 strbuf_addstr(&buf
, message
);
2812 commit_rewrite_person(&buf
, "\nauthor ", opt
->mailmap
);
2813 commit_rewrite_person(&buf
, "\ncommitter ", opt
->mailmap
);
2816 /* Append "fake" message parts as needed */
2817 if (opt
->show_notes
) {
2819 strbuf_addstr(&buf
, message
);
2820 format_display_notes(commit
->object
.sha1
, &buf
, encoding
, 1);
2823 /* Find either in the original commit message, or in the temporary */
2825 retval
= grep_buffer(&opt
->grep_filter
, buf
.buf
, buf
.len
);
2827 retval
= grep_buffer(&opt
->grep_filter
,
2828 message
, strlen(message
));
2829 strbuf_release(&buf
);
2830 logmsg_free(message
, commit
);
2834 static inline int want_ancestry(const struct rev_info
*revs
)
2836 return (revs
->rewrite_parents
|| revs
->children
.name
);
2839 enum commit_action
get_commit_action(struct rev_info
*revs
, struct commit
*commit
)
2841 if (commit
->object
.flags
& SHOWN
)
2842 return commit_ignore
;
2843 if (revs
->unpacked
&& has_sha1_pack(commit
->object
.sha1
))
2844 return commit_ignore
;
2847 if (commit
->object
.flags
& UNINTERESTING
)
2848 return commit_ignore
;
2849 if (revs
->min_age
!= -1 && (commit
->date
> revs
->min_age
))
2850 return commit_ignore
;
2851 if (revs
->min_parents
|| (revs
->max_parents
>= 0)) {
2852 int n
= commit_list_count(commit
->parents
);
2853 if ((n
< revs
->min_parents
) ||
2854 ((revs
->max_parents
>= 0) && (n
> revs
->max_parents
)))
2855 return commit_ignore
;
2857 if (!commit_match(commit
, revs
))
2858 return commit_ignore
;
2859 if (revs
->prune
&& revs
->dense
) {
2860 /* Commit without changes? */
2861 if (commit
->object
.flags
& TREESAME
) {
2863 struct commit_list
*p
;
2864 /* drop merges unless we want parenthood */
2865 if (!want_ancestry(revs
))
2866 return commit_ignore
;
2868 * If we want ancestry, then need to keep any merges
2869 * between relevant commits to tie together topology.
2870 * For consistency with TREESAME and simplification
2871 * use "relevant" here rather than just INTERESTING,
2872 * to treat bottom commit(s) as part of the topology.
2874 for (n
= 0, p
= commit
->parents
; p
; p
= p
->next
)
2875 if (relevant_commit(p
->item
))
2878 return commit_ignore
;
2884 enum commit_action
simplify_commit(struct rev_info
*revs
, struct commit
*commit
)
2886 enum commit_action action
= get_commit_action(revs
, commit
);
2888 if (action
== commit_show
&&
2890 revs
->prune
&& revs
->dense
&& want_ancestry(revs
)) {
2892 * --full-diff on simplified parents is no good: it
2893 * will show spurious changes from the commits that
2894 * were elided. So we save the parents on the side
2895 * when --full-diff is in effect.
2897 if (revs
->full_diff
)
2898 save_parents(revs
, commit
);
2899 if (rewrite_parents(revs
, commit
, rewrite_one
) < 0)
2900 return commit_error
;
2905 static struct commit
*get_revision_1(struct rev_info
*revs
)
2911 struct commit_list
*entry
= revs
->commits
;
2912 struct commit
*commit
= entry
->item
;
2914 revs
->commits
= entry
->next
;
2917 if (revs
->reflog_info
) {
2918 save_parents(revs
, commit
);
2919 fake_reflog_parent(revs
->reflog_info
, commit
);
2920 commit
->object
.flags
&= ~(ADDED
| SEEN
| SHOWN
);
2924 * If we haven't done the list limiting, we need to look at
2925 * the parents here. We also need to do the date-based limiting
2926 * that we'd otherwise have done in limit_list().
2928 if (!revs
->limited
) {
2929 if (revs
->max_age
!= -1 &&
2930 (commit
->date
< revs
->max_age
))
2932 if (add_parents_to_list(revs
, commit
, &revs
->commits
, NULL
) < 0)
2933 die("Failed to traverse parents of commit %s",
2934 sha1_to_hex(commit
->object
.sha1
));
2937 switch (simplify_commit(revs
, commit
)) {
2941 die("Failed to simplify parents of commit %s",
2942 sha1_to_hex(commit
->object
.sha1
));
2946 } while (revs
->commits
);
2951 * Return true for entries that have not yet been shown. (This is an
2952 * object_array_each_func_t.)
2954 static int entry_unshown(struct object_array_entry
*entry
, void *cb_data_unused
)
2956 return !(entry
->item
->flags
& SHOWN
);
2960 * If array is on the verge of a realloc, garbage-collect any entries
2961 * that have already been shown to try to free up some space.
2963 static void gc_boundary(struct object_array
*array
)
2965 if (array
->nr
== array
->alloc
)
2966 object_array_filter(array
, entry_unshown
, NULL
);
2969 static void create_boundary_commit_list(struct rev_info
*revs
)
2973 struct object_array
*array
= &revs
->boundary_commits
;
2974 struct object_array_entry
*objects
= array
->objects
;
2977 * If revs->commits is non-NULL at this point, an error occurred in
2978 * get_revision_1(). Ignore the error and continue printing the
2979 * boundary commits anyway. (This is what the code has always
2982 if (revs
->commits
) {
2983 free_commit_list(revs
->commits
);
2984 revs
->commits
= NULL
;
2988 * Put all of the actual boundary commits from revs->boundary_commits
2989 * into revs->commits
2991 for (i
= 0; i
< array
->nr
; i
++) {
2992 c
= (struct commit
*)(objects
[i
].item
);
2995 if (!(c
->object
.flags
& CHILD_SHOWN
))
2997 if (c
->object
.flags
& (SHOWN
| BOUNDARY
))
2999 c
->object
.flags
|= BOUNDARY
;
3000 commit_list_insert(c
, &revs
->commits
);
3004 * If revs->topo_order is set, sort the boundary commits
3005 * in topological order
3007 sort_in_topological_order(&revs
->commits
, revs
->sort_order
);
3010 static struct commit
*get_revision_internal(struct rev_info
*revs
)
3012 struct commit
*c
= NULL
;
3013 struct commit_list
*l
;
3015 if (revs
->boundary
== 2) {
3017 * All of the normal commits have already been returned,
3018 * and we are now returning boundary commits.
3019 * create_boundary_commit_list() has populated
3020 * revs->commits with the remaining commits to return.
3022 c
= pop_commit(&revs
->commits
);
3024 c
->object
.flags
|= SHOWN
;
3029 * If our max_count counter has reached zero, then we are done. We
3030 * don't simply return NULL because we still might need to show
3031 * boundary commits. But we want to avoid calling get_revision_1, which
3032 * might do a considerable amount of work finding the next commit only
3033 * for us to throw it away.
3035 * If it is non-zero, then either we don't have a max_count at all
3036 * (-1), or it is still counting, in which case we decrement.
3038 if (revs
->max_count
) {
3039 c
= get_revision_1(revs
);
3041 while (revs
->skip_count
> 0) {
3043 c
= get_revision_1(revs
);
3049 if (revs
->max_count
> 0)
3054 c
->object
.flags
|= SHOWN
;
3056 if (!revs
->boundary
)
3061 * get_revision_1() runs out the commits, and
3062 * we are done computing the boundaries.
3063 * switch to boundary commits output mode.
3068 * Update revs->commits to contain the list of
3071 create_boundary_commit_list(revs
);
3073 return get_revision_internal(revs
);
3077 * boundary commits are the commits that are parents of the
3078 * ones we got from get_revision_1() but they themselves are
3079 * not returned from get_revision_1(). Before returning
3080 * 'c', we need to mark its parents that they could be boundaries.
3083 for (l
= c
->parents
; l
; l
= l
->next
) {
3085 p
= &(l
->item
->object
);
3086 if (p
->flags
& (CHILD_SHOWN
| SHOWN
))
3088 p
->flags
|= CHILD_SHOWN
;
3089 gc_boundary(&revs
->boundary_commits
);
3090 add_object_array(p
, NULL
, &revs
->boundary_commits
);
3096 struct commit
*get_revision(struct rev_info
*revs
)
3099 struct commit_list
*reversed
;
3101 if (revs
->reverse
) {
3103 while ((c
= get_revision_internal(revs
)))
3104 commit_list_insert(c
, &reversed
);
3105 revs
->commits
= reversed
;
3107 revs
->reverse_output_stage
= 1;
3110 if (revs
->reverse_output_stage
)
3111 return pop_commit(&revs
->commits
);
3113 c
= get_revision_internal(revs
);
3114 if (c
&& revs
->graph
)
3115 graph_update(revs
->graph
, c
);
3117 free_saved_parents(revs
);
3121 char *get_revision_mark(const struct rev_info
*revs
, const struct commit
*commit
)
3123 if (commit
->object
.flags
& BOUNDARY
)
3125 else if (commit
->object
.flags
& UNINTERESTING
)
3127 else if (commit
->object
.flags
& PATCHSAME
)
3129 else if (!revs
|| revs
->left_right
) {
3130 if (commit
->object
.flags
& SYMMETRIC_LEFT
)
3134 } else if (revs
->graph
)
3136 else if (revs
->cherry_mark
)
3141 void put_revision_mark(const struct rev_info
*revs
, const struct commit
*commit
)
3143 char *mark
= get_revision_mark(revs
, commit
);
3146 fputs(mark
, stdout
);
3150 define_commit_slab(saved_parents
, struct commit_list
*);
3152 #define EMPTY_PARENT_LIST ((struct commit_list *)-1)
3154 void save_parents(struct rev_info
*revs
, struct commit
*commit
)
3156 struct commit_list
**pp
;
3158 if (!revs
->saved_parents_slab
) {
3159 revs
->saved_parents_slab
= xmalloc(sizeof(struct saved_parents
));
3160 init_saved_parents(revs
->saved_parents_slab
);
3163 pp
= saved_parents_at(revs
->saved_parents_slab
, commit
);
3166 * When walking with reflogs, we may visit the same commit
3167 * several times: once for each appearance in the reflog.
3169 * In this case, save_parents() will be called multiple times.
3170 * We want to keep only the first set of parents. We need to
3171 * store a sentinel value for an empty (i.e., NULL) parent
3172 * list to distinguish it from a not-yet-saved list, however.
3176 if (commit
->parents
)
3177 *pp
= copy_commit_list(commit
->parents
);
3179 *pp
= EMPTY_PARENT_LIST
;
3182 struct commit_list
*get_saved_parents(struct rev_info
*revs
, const struct commit
*commit
)
3184 struct commit_list
*parents
;
3186 if (!revs
->saved_parents_slab
)
3187 return commit
->parents
;
3189 parents
= *saved_parents_at(revs
->saved_parents_slab
, commit
);
3190 if (parents
== EMPTY_PARENT_LIST
)
3195 void free_saved_parents(struct rev_info
*revs
)
3197 if (revs
->saved_parents_slab
)
3198 clear_saved_parents(revs
->saved_parents_slab
);