11 #include "reflog-walk.h"
12 #include "patch-ids.h"
15 #include "string-list.h"
17 volatile show_early_output_fn_t show_early_output
;
19 char *path_name(const struct name_path
*path
, const char *name
)
21 const struct name_path
*p
;
23 int nlen
= strlen(name
);
26 for (p
= path
; p
; p
= p
->up
) {
28 len
+= p
->elem_len
+ 1;
31 m
= n
+ len
- (nlen
+ 1);
33 for (p
= path
; p
; p
= p
->up
) {
36 memcpy(m
, p
->elem
, p
->elem_len
);
43 void add_object(struct object
*obj
,
44 struct object_array
*p
,
45 struct name_path
*path
,
48 add_object_array(obj
, path_name(path
, name
), p
);
51 static void mark_blob_uninteresting(struct blob
*blob
)
55 if (blob
->object
.flags
& UNINTERESTING
)
57 blob
->object
.flags
|= UNINTERESTING
;
60 void mark_tree_uninteresting(struct tree
*tree
)
62 struct tree_desc desc
;
63 struct name_entry entry
;
64 struct object
*obj
= &tree
->object
;
68 if (obj
->flags
& UNINTERESTING
)
70 obj
->flags
|= UNINTERESTING
;
71 if (!has_sha1_file(obj
->sha1
))
73 if (parse_tree(tree
) < 0)
74 die("bad tree %s", sha1_to_hex(obj
->sha1
));
76 init_tree_desc(&desc
, tree
->buffer
, tree
->size
);
77 while (tree_entry(&desc
, &entry
)) {
78 switch (object_type(entry
.mode
)) {
80 mark_tree_uninteresting(lookup_tree(entry
.sha1
));
83 mark_blob_uninteresting(lookup_blob(entry
.sha1
));
86 /* Subproject commit - not in this repository */
92 * We don't care about the tree any more
93 * after it has been marked uninteresting.
99 void mark_parents_uninteresting(struct commit
*commit
)
101 struct commit_list
*parents
= commit
->parents
;
104 struct commit
*commit
= parents
->item
;
105 if (!(commit
->object
.flags
& UNINTERESTING
)) {
106 commit
->object
.flags
|= UNINTERESTING
;
109 * Normally we haven't parsed the parent
110 * yet, so we won't have a parent of a parent
111 * here. However, it may turn out that we've
112 * reached this commit some other way (where it
113 * wasn't uninteresting), in which case we need
114 * to mark its parents recursively too..
117 mark_parents_uninteresting(commit
);
121 * A missing commit is ok iff its parent is marked
124 * We just mark such a thing parsed, so that when
125 * it is popped next time around, we won't be trying
126 * to parse it and get an error.
128 if (!has_sha1_file(commit
->object
.sha1
))
129 commit
->object
.parsed
= 1;
130 parents
= parents
->next
;
134 static void add_pending_object_with_mode(struct rev_info
*revs
, struct object
*obj
, const char *name
, unsigned mode
)
138 if (revs
->no_walk
&& (obj
->flags
& UNINTERESTING
))
140 if (revs
->reflog_info
&& obj
->type
== OBJ_COMMIT
) {
141 struct strbuf buf
= STRBUF_INIT
;
142 int len
= interpret_branch_name(name
, &buf
);
145 if (0 < len
&& name
[len
] && buf
.len
)
146 strbuf_addstr(&buf
, name
+ len
);
147 st
= add_reflog_for_walk(revs
->reflog_info
,
148 (struct commit
*)obj
,
149 buf
.buf
[0] ? buf
.buf
: name
);
150 strbuf_release(&buf
);
154 add_object_array_with_mode(obj
, name
, &revs
->pending
, mode
);
157 void add_pending_object(struct rev_info
*revs
, struct object
*obj
, const char *name
)
159 add_pending_object_with_mode(revs
, obj
, name
, S_IFINVALID
);
162 void add_head_to_pending(struct rev_info
*revs
)
164 unsigned char sha1
[20];
166 if (get_sha1("HEAD", sha1
))
168 obj
= parse_object(sha1
);
171 add_pending_object(revs
, obj
, "HEAD");
174 static struct object
*get_reference(struct rev_info
*revs
, const char *name
, const unsigned char *sha1
, unsigned int flags
)
176 struct object
*object
;
178 object
= parse_object(sha1
);
180 if (revs
->ignore_missing
)
182 die("bad object %s", name
);
184 object
->flags
|= flags
;
188 static struct commit
*handle_commit(struct rev_info
*revs
, struct object
*object
, const char *name
)
190 unsigned long flags
= object
->flags
;
193 * Tag object? Look what it points to..
195 while (object
->type
== OBJ_TAG
) {
196 struct tag
*tag
= (struct tag
*) object
;
197 if (revs
->tag_objects
&& !(flags
& UNINTERESTING
))
198 add_pending_object(revs
, object
, tag
->tag
);
201 object
= parse_object(tag
->tagged
->sha1
);
203 if (flags
& UNINTERESTING
)
205 die("bad object %s", sha1_to_hex(tag
->tagged
->sha1
));
210 * Commit object? Just return it, we'll do all the complex
213 if (object
->type
== OBJ_COMMIT
) {
214 struct commit
*commit
= (struct commit
*)object
;
215 if (parse_commit(commit
) < 0)
216 die("unable to parse commit %s", name
);
217 if (flags
& UNINTERESTING
) {
218 commit
->object
.flags
|= UNINTERESTING
;
219 mark_parents_uninteresting(commit
);
222 if (revs
->show_source
&& !commit
->util
)
223 commit
->util
= (void *) name
;
228 * Tree object? Either mark it uninteresting, or add it
229 * to the list of objects to look at later..
231 if (object
->type
== OBJ_TREE
) {
232 struct tree
*tree
= (struct tree
*)object
;
233 if (!revs
->tree_objects
)
235 if (flags
& UNINTERESTING
) {
236 mark_tree_uninteresting(tree
);
239 add_pending_object(revs
, object
, "");
244 * Blob object? You know the drill by now..
246 if (object
->type
== OBJ_BLOB
) {
247 struct blob
*blob
= (struct blob
*)object
;
248 if (!revs
->blob_objects
)
250 if (flags
& UNINTERESTING
) {
251 mark_blob_uninteresting(blob
);
254 add_pending_object(revs
, object
, "");
257 die("%s is unknown object", name
);
260 static int everybody_uninteresting(struct commit_list
*orig
)
262 struct commit_list
*list
= orig
;
264 struct commit
*commit
= list
->item
;
266 if (commit
->object
.flags
& UNINTERESTING
)
274 * The goal is to get REV_TREE_NEW as the result only if the
275 * diff consists of all '+' (and no other changes), REV_TREE_OLD
276 * if the whole diff is removal of old data, and otherwise
277 * REV_TREE_DIFFERENT (of course if the trees are the same we
278 * want REV_TREE_SAME).
279 * That means that once we get to REV_TREE_DIFFERENT, we do not
280 * have to look any further.
282 static int tree_difference
= REV_TREE_SAME
;
284 static void file_add_remove(struct diff_options
*options
,
285 int addremove
, unsigned mode
,
286 const unsigned char *sha1
,
287 const char *fullpath
, unsigned dirty_submodule
)
289 int diff
= addremove
== '+' ? REV_TREE_NEW
: REV_TREE_OLD
;
291 tree_difference
|= diff
;
292 if (tree_difference
== REV_TREE_DIFFERENT
)
293 DIFF_OPT_SET(options
, HAS_CHANGES
);
296 static void file_change(struct diff_options
*options
,
297 unsigned old_mode
, unsigned new_mode
,
298 const unsigned char *old_sha1
,
299 const unsigned char *new_sha1
,
300 const char *fullpath
,
301 unsigned old_dirty_submodule
, unsigned new_dirty_submodule
)
303 tree_difference
= REV_TREE_DIFFERENT
;
304 DIFF_OPT_SET(options
, HAS_CHANGES
);
307 static int rev_compare_tree(struct rev_info
*revs
, struct commit
*parent
, struct commit
*commit
)
309 struct tree
*t1
= parent
->tree
;
310 struct tree
*t2
= commit
->tree
;
317 if (revs
->simplify_by_decoration
) {
319 * If we are simplifying by decoration, then the commit
320 * is worth showing if it has a tag pointing at it.
322 if (lookup_decoration(&name_decoration
, &commit
->object
))
323 return REV_TREE_DIFFERENT
;
325 * A commit that is not pointed by a tag is uninteresting
326 * if we are not limited by path. This means that you will
327 * see the usual "commits that touch the paths" plus any
328 * tagged commit by specifying both --simplify-by-decoration
331 if (!revs
->prune_data
.nr
)
332 return REV_TREE_SAME
;
335 tree_difference
= REV_TREE_SAME
;
336 DIFF_OPT_CLR(&revs
->pruning
, HAS_CHANGES
);
337 if (diff_tree_sha1(t1
->object
.sha1
, t2
->object
.sha1
, "",
339 return REV_TREE_DIFFERENT
;
340 return tree_difference
;
343 static int rev_same_tree_as_empty(struct rev_info
*revs
, struct commit
*commit
)
348 struct tree_desc empty
, real
;
349 struct tree
*t1
= commit
->tree
;
354 tree
= read_object_with_reference(t1
->object
.sha1
, tree_type
, &size
, NULL
);
357 init_tree_desc(&real
, tree
, size
);
358 init_tree_desc(&empty
, "", 0);
360 tree_difference
= REV_TREE_SAME
;
361 DIFF_OPT_CLR(&revs
->pruning
, HAS_CHANGES
);
362 retval
= diff_tree(&empty
, &real
, "", &revs
->pruning
);
365 return retval
>= 0 && (tree_difference
== REV_TREE_SAME
);
368 static void try_to_simplify_commit(struct rev_info
*revs
, struct commit
*commit
)
370 struct commit_list
**pp
, *parent
;
371 int tree_changed
= 0, tree_same
= 0, nth_parent
= 0;
374 * If we don't do pruning, everything is interesting
382 if (!commit
->parents
) {
383 if (rev_same_tree_as_empty(revs
, commit
))
384 commit
->object
.flags
|= TREESAME
;
389 * Normal non-merge commit? If we don't want to make the
390 * history dense, we consider it always to be a change..
392 if (!revs
->dense
&& !commit
->parents
->next
)
395 pp
= &commit
->parents
;
396 while ((parent
= *pp
) != NULL
) {
397 struct commit
*p
= parent
->item
;
400 * Do not compare with later parents when we care only about
401 * the first parent chain, in order to avoid derailing the
402 * traversal to follow a side branch that brought everything
403 * in the path we are limited to by the pathspec.
405 if (revs
->first_parent_only
&& nth_parent
++)
407 if (parse_commit(p
) < 0)
408 die("cannot simplify commit %s (because of %s)",
409 sha1_to_hex(commit
->object
.sha1
),
410 sha1_to_hex(p
->object
.sha1
));
411 switch (rev_compare_tree(revs
, p
, commit
)) {
414 if (!revs
->simplify_history
|| (p
->object
.flags
& UNINTERESTING
)) {
415 /* Even if a merge with an uninteresting
416 * side branch brought the entire change
417 * we are interested in, we do not want
418 * to lose the other branches of this
419 * merge, so we just keep going.
425 commit
->parents
= parent
;
426 commit
->object
.flags
|= TREESAME
;
430 if (revs
->remove_empty_trees
&&
431 rev_same_tree_as_empty(revs
, p
)) {
432 /* We are adding all the specified
433 * paths from this parent, so the
434 * history beyond this parent is not
435 * interesting. Remove its parents
436 * (they are grandparents for us).
437 * IOW, we pretend this parent is a
440 if (parse_commit(p
) < 0)
441 die("cannot simplify commit %s (invalid %s)",
442 sha1_to_hex(commit
->object
.sha1
),
443 sha1_to_hex(p
->object
.sha1
));
448 case REV_TREE_DIFFERENT
:
453 die("bad tree compare for commit %s", sha1_to_hex(commit
->object
.sha1
));
455 if (tree_changed
&& !tree_same
)
457 commit
->object
.flags
|= TREESAME
;
460 static void commit_list_insert_by_date_cached(struct commit
*p
, struct commit_list
**head
,
461 struct commit_list
*cached_base
, struct commit_list
**cache
)
463 struct commit_list
*new_entry
;
465 if (cached_base
&& p
->date
< cached_base
->item
->date
)
466 new_entry
= commit_list_insert_by_date(p
, &cached_base
->next
);
468 new_entry
= commit_list_insert_by_date(p
, head
);
470 if (cache
&& (!*cache
|| p
->date
< (*cache
)->item
->date
))
474 static int add_parents_to_list(struct rev_info
*revs
, struct commit
*commit
,
475 struct commit_list
**list
, struct commit_list
**cache_ptr
)
477 struct commit_list
*parent
= commit
->parents
;
479 struct commit_list
*cached_base
= cache_ptr
? *cache_ptr
: NULL
;
481 if (commit
->object
.flags
& ADDED
)
483 commit
->object
.flags
|= ADDED
;
486 * If the commit is uninteresting, don't try to
487 * prune parents - we want the maximal uninteresting
490 * Normally we haven't parsed the parent
491 * yet, so we won't have a parent of a parent
492 * here. However, it may turn out that we've
493 * reached this commit some other way (where it
494 * wasn't uninteresting), in which case we need
495 * to mark its parents recursively too..
497 if (commit
->object
.flags
& UNINTERESTING
) {
499 struct commit
*p
= parent
->item
;
500 parent
= parent
->next
;
502 p
->object
.flags
|= UNINTERESTING
;
503 if (parse_commit(p
) < 0)
506 mark_parents_uninteresting(p
);
507 if (p
->object
.flags
& SEEN
)
509 p
->object
.flags
|= SEEN
;
510 commit_list_insert_by_date_cached(p
, list
, cached_base
, cache_ptr
);
516 * Ok, the commit wasn't uninteresting. Try to
517 * simplify the commit history and find the parent
518 * that has no differences in the path set if one exists.
520 try_to_simplify_commit(revs
, commit
);
525 left_flag
= (commit
->object
.flags
& SYMMETRIC_LEFT
);
527 for (parent
= commit
->parents
; parent
; parent
= parent
->next
) {
528 struct commit
*p
= parent
->item
;
530 if (parse_commit(p
) < 0)
532 if (revs
->show_source
&& !p
->util
)
533 p
->util
= commit
->util
;
534 p
->object
.flags
|= left_flag
;
535 if (!(p
->object
.flags
& SEEN
)) {
536 p
->object
.flags
|= SEEN
;
537 commit_list_insert_by_date_cached(p
, list
, cached_base
, cache_ptr
);
539 if (revs
->first_parent_only
)
545 static void cherry_pick_list(struct commit_list
*list
, struct rev_info
*revs
)
547 struct commit_list
*p
;
548 int left_count
= 0, right_count
= 0;
550 struct patch_ids ids
;
551 unsigned cherry_flag
;
553 /* First count the commits on the left and on the right */
554 for (p
= list
; p
; p
= p
->next
) {
555 struct commit
*commit
= p
->item
;
556 unsigned flags
= commit
->object
.flags
;
557 if (flags
& BOUNDARY
)
559 else if (flags
& SYMMETRIC_LEFT
)
565 if (!left_count
|| !right_count
)
568 left_first
= left_count
< right_count
;
569 init_patch_ids(&ids
);
570 ids
.diffopts
.pathspec
= revs
->diffopt
.pathspec
;
572 /* Compute patch-ids for one side */
573 for (p
= list
; p
; p
= p
->next
) {
574 struct commit
*commit
= p
->item
;
575 unsigned flags
= commit
->object
.flags
;
577 if (flags
& BOUNDARY
)
580 * If we have fewer left, left_first is set and we omit
581 * commits on the right branch in this loop. If we have
582 * fewer right, we skip the left ones.
584 if (left_first
!= !!(flags
& SYMMETRIC_LEFT
))
586 commit
->util
= add_commit_patch_id(commit
, &ids
);
589 /* either cherry_mark or cherry_pick are true */
590 cherry_flag
= revs
->cherry_mark
? PATCHSAME
: SHOWN
;
592 /* Check the other side */
593 for (p
= list
; p
; p
= p
->next
) {
594 struct commit
*commit
= p
->item
;
596 unsigned flags
= commit
->object
.flags
;
598 if (flags
& BOUNDARY
)
601 * If we have fewer left, left_first is set and we omit
602 * commits on the left branch in this loop.
604 if (left_first
== !!(flags
& SYMMETRIC_LEFT
))
608 * Have we seen the same patch id?
610 id
= has_commit_patch_id(commit
, &ids
);
614 commit
->object
.flags
|= cherry_flag
;
617 /* Now check the original side for seen ones */
618 for (p
= list
; p
; p
= p
->next
) {
619 struct commit
*commit
= p
->item
;
620 struct patch_id
*ent
;
626 commit
->object
.flags
|= cherry_flag
;
630 free_patch_ids(&ids
);
633 /* How many extra uninteresting commits we want to see.. */
636 static int still_interesting(struct commit_list
*src
, unsigned long date
, int slop
)
639 * No source list at all? We're definitely done..
645 * Does the destination list contain entries with a date
646 * before the source list? Definitely _not_ done.
648 if (date
< src
->item
->date
)
652 * Does the source list still have interesting commits in
653 * it? Definitely not done..
655 if (!everybody_uninteresting(src
))
658 /* Ok, we're closing in.. */
663 * "rev-list --ancestry-path A..B" computes commits that are ancestors
664 * of B but not ancestors of A but further limits the result to those
665 * that are descendants of A. This takes the list of bottom commits and
666 * the result of "A..B" without --ancestry-path, and limits the latter
667 * further to the ones that can reach one of the commits in "bottom".
669 static void limit_to_ancestry(struct commit_list
*bottom
, struct commit_list
*list
)
671 struct commit_list
*p
;
672 struct commit_list
*rlist
= NULL
;
676 * Reverse the list so that it will be likely that we would
677 * process parents before children.
679 for (p
= list
; p
; p
= p
->next
)
680 commit_list_insert(p
->item
, &rlist
);
682 for (p
= bottom
; p
; p
= p
->next
)
683 p
->item
->object
.flags
|= TMP_MARK
;
686 * Mark the ones that can reach bottom commits in "list",
687 * in a bottom-up fashion.
691 for (p
= rlist
; p
; p
= p
->next
) {
692 struct commit
*c
= p
->item
;
693 struct commit_list
*parents
;
694 if (c
->object
.flags
& (TMP_MARK
| UNINTERESTING
))
696 for (parents
= c
->parents
;
698 parents
= parents
->next
) {
699 if (!(parents
->item
->object
.flags
& TMP_MARK
))
701 c
->object
.flags
|= TMP_MARK
;
706 } while (made_progress
);
709 * NEEDSWORK: decide if we want to remove parents that are
710 * not marked with TMP_MARK from commit->parents for commits
711 * in the resulting list. We may not want to do that, though.
715 * The ones that are not marked with TMP_MARK are uninteresting
717 for (p
= list
; p
; p
= p
->next
) {
718 struct commit
*c
= p
->item
;
719 if (c
->object
.flags
& TMP_MARK
)
721 c
->object
.flags
|= UNINTERESTING
;
724 /* We are done with the TMP_MARK */
725 for (p
= list
; p
; p
= p
->next
)
726 p
->item
->object
.flags
&= ~TMP_MARK
;
727 for (p
= bottom
; p
; p
= p
->next
)
728 p
->item
->object
.flags
&= ~TMP_MARK
;
729 free_commit_list(rlist
);
733 * Before walking the history, keep the set of "negative" refs the
734 * caller has asked to exclude.
736 * This is used to compute "rev-list --ancestry-path A..B", as we need
737 * to filter the result of "A..B" further to the ones that can actually
740 static struct commit_list
*collect_bottom_commits(struct commit_list
*list
)
742 struct commit_list
*elem
, *bottom
= NULL
;
743 for (elem
= list
; elem
; elem
= elem
->next
)
744 if (elem
->item
->object
.flags
& UNINTERESTING
)
745 commit_list_insert(elem
->item
, &bottom
);
749 /* Assumes either left_only or right_only is set */
750 static void limit_left_right(struct commit_list
*list
, struct rev_info
*revs
)
752 struct commit_list
*p
;
754 for (p
= list
; p
; p
= p
->next
) {
755 struct commit
*commit
= p
->item
;
757 if (revs
->right_only
) {
758 if (commit
->object
.flags
& SYMMETRIC_LEFT
)
759 commit
->object
.flags
|= SHOWN
;
760 } else /* revs->left_only is set */
761 if (!(commit
->object
.flags
& SYMMETRIC_LEFT
))
762 commit
->object
.flags
|= SHOWN
;
766 static int limit_list(struct rev_info
*revs
)
769 unsigned long date
= ~0ul;
770 struct commit_list
*list
= revs
->commits
;
771 struct commit_list
*newlist
= NULL
;
772 struct commit_list
**p
= &newlist
;
773 struct commit_list
*bottom
= NULL
;
775 if (revs
->ancestry_path
) {
776 bottom
= collect_bottom_commits(list
);
778 die("--ancestry-path given but there are no bottom commits");
782 struct commit_list
*entry
= list
;
783 struct commit
*commit
= list
->item
;
784 struct object
*obj
= &commit
->object
;
785 show_early_output_fn_t show
;
790 if (revs
->max_age
!= -1 && (commit
->date
< revs
->max_age
))
791 obj
->flags
|= UNINTERESTING
;
792 if (add_parents_to_list(revs
, commit
, &list
, NULL
) < 0)
794 if (obj
->flags
& UNINTERESTING
) {
795 mark_parents_uninteresting(commit
);
797 p
= &commit_list_insert(commit
, p
)->next
;
798 slop
= still_interesting(list
, date
, slop
);
801 /* If showing all, add the whole pending list to the end */
806 if (revs
->min_age
!= -1 && (commit
->date
> revs
->min_age
))
809 p
= &commit_list_insert(commit
, p
)->next
;
811 show
= show_early_output
;
816 show_early_output
= NULL
;
818 if (revs
->cherry_pick
|| revs
->cherry_mark
)
819 cherry_pick_list(newlist
, revs
);
821 if (revs
->left_only
|| revs
->right_only
)
822 limit_left_right(newlist
, revs
);
825 limit_to_ancestry(bottom
, newlist
);
826 free_commit_list(bottom
);
829 revs
->commits
= newlist
;
835 int warned_bad_reflog
;
836 struct rev_info
*all_revs
;
837 const char *name_for_errormsg
;
840 static int handle_one_ref(const char *path
, const unsigned char *sha1
, int flag
, void *cb_data
)
842 struct all_refs_cb
*cb
= cb_data
;
843 struct object
*object
= get_reference(cb
->all_revs
, path
, sha1
,
845 add_pending_object(cb
->all_revs
, object
, path
);
849 static void init_all_refs_cb(struct all_refs_cb
*cb
, struct rev_info
*revs
,
853 cb
->all_flags
= flags
;
856 static void handle_refs(const char *submodule
, struct rev_info
*revs
, unsigned flags
,
857 int (*for_each
)(const char *, each_ref_fn
, void *))
859 struct all_refs_cb cb
;
860 init_all_refs_cb(&cb
, revs
, flags
);
861 for_each(submodule
, handle_one_ref
, &cb
);
864 static void handle_one_reflog_commit(unsigned char *sha1
, void *cb_data
)
866 struct all_refs_cb
*cb
= cb_data
;
867 if (!is_null_sha1(sha1
)) {
868 struct object
*o
= parse_object(sha1
);
870 o
->flags
|= cb
->all_flags
;
871 add_pending_object(cb
->all_revs
, o
, "");
873 else if (!cb
->warned_bad_reflog
) {
874 warning("reflog of '%s' references pruned commits",
875 cb
->name_for_errormsg
);
876 cb
->warned_bad_reflog
= 1;
881 static int handle_one_reflog_ent(unsigned char *osha1
, unsigned char *nsha1
,
882 const char *email
, unsigned long timestamp
, int tz
,
883 const char *message
, void *cb_data
)
885 handle_one_reflog_commit(osha1
, cb_data
);
886 handle_one_reflog_commit(nsha1
, cb_data
);
890 static int handle_one_reflog(const char *path
, const unsigned char *sha1
, int flag
, void *cb_data
)
892 struct all_refs_cb
*cb
= cb_data
;
893 cb
->warned_bad_reflog
= 0;
894 cb
->name_for_errormsg
= path
;
895 for_each_reflog_ent(path
, handle_one_reflog_ent
, cb_data
);
899 static void handle_reflog(struct rev_info
*revs
, unsigned flags
)
901 struct all_refs_cb cb
;
903 cb
.all_flags
= flags
;
904 for_each_reflog(handle_one_reflog
, &cb
);
907 static int add_parents_only(struct rev_info
*revs
, const char *arg
, int flags
)
909 unsigned char sha1
[20];
911 struct commit
*commit
;
912 struct commit_list
*parents
;
915 flags
^= UNINTERESTING
;
918 if (get_sha1(arg
, sha1
))
921 it
= get_reference(revs
, arg
, sha1
, 0);
922 if (!it
&& revs
->ignore_missing
)
924 if (it
->type
!= OBJ_TAG
)
926 if (!((struct tag
*)it
)->tagged
)
928 hashcpy(sha1
, ((struct tag
*)it
)->tagged
->sha1
);
930 if (it
->type
!= OBJ_COMMIT
)
932 commit
= (struct commit
*)it
;
933 for (parents
= commit
->parents
; parents
; parents
= parents
->next
) {
934 it
= &parents
->item
->object
;
936 add_pending_object(revs
, it
, arg
);
941 void init_revisions(struct rev_info
*revs
, const char *prefix
)
943 memset(revs
, 0, sizeof(*revs
));
945 revs
->abbrev
= DEFAULT_ABBREV
;
946 revs
->ignore_merges
= 1;
947 revs
->simplify_history
= 1;
948 DIFF_OPT_SET(&revs
->pruning
, RECURSIVE
);
949 DIFF_OPT_SET(&revs
->pruning
, QUICK
);
950 revs
->pruning
.add_remove
= file_add_remove
;
951 revs
->pruning
.change
= file_change
;
954 revs
->prefix
= prefix
;
957 revs
->skip_count
= -1;
958 revs
->max_count
= -1;
959 revs
->max_parents
= -1;
961 revs
->commit_format
= CMIT_FMT_DEFAULT
;
963 revs
->grep_filter
.status_only
= 1;
964 revs
->grep_filter
.pattern_tail
= &(revs
->grep_filter
.pattern_list
);
965 revs
->grep_filter
.header_tail
= &(revs
->grep_filter
.header_list
);
966 revs
->grep_filter
.regflags
= REG_NEWLINE
;
968 diff_setup(&revs
->diffopt
);
969 if (prefix
&& !revs
->diffopt
.prefix
) {
970 revs
->diffopt
.prefix
= prefix
;
971 revs
->diffopt
.prefix_length
= strlen(prefix
);
974 revs
->notes_opt
.use_default_notes
= -1;
977 static void add_pending_commit_list(struct rev_info
*revs
,
978 struct commit_list
*commit_list
,
981 while (commit_list
) {
982 struct object
*object
= &commit_list
->item
->object
;
983 object
->flags
|= flags
;
984 add_pending_object(revs
, object
, sha1_to_hex(object
->sha1
));
985 commit_list
= commit_list
->next
;
989 static void prepare_show_merge(struct rev_info
*revs
)
991 struct commit_list
*bases
;
992 struct commit
*head
, *other
;
993 unsigned char sha1
[20];
994 const char **prune
= NULL
;
995 int i
, prune_num
= 1; /* counting terminating NULL */
997 if (get_sha1("HEAD", sha1
) || !(head
= lookup_commit(sha1
)))
998 die("--merge without HEAD?");
999 if (get_sha1("MERGE_HEAD", sha1
) || !(other
= lookup_commit(sha1
)))
1000 die("--merge without MERGE_HEAD?");
1001 add_pending_object(revs
, &head
->object
, "HEAD");
1002 add_pending_object(revs
, &other
->object
, "MERGE_HEAD");
1003 bases
= get_merge_bases(head
, other
, 1);
1004 add_pending_commit_list(revs
, bases
, UNINTERESTING
);
1005 free_commit_list(bases
);
1006 head
->object
.flags
|= SYMMETRIC_LEFT
;
1010 for (i
= 0; i
< active_nr
; i
++) {
1011 struct cache_entry
*ce
= active_cache
[i
];
1014 if (ce_path_match(ce
, &revs
->prune_data
)) {
1016 prune
= xrealloc(prune
, sizeof(*prune
) * prune_num
);
1017 prune
[prune_num
-2] = ce
->name
;
1018 prune
[prune_num
-1] = NULL
;
1020 while ((i
+1 < active_nr
) &&
1021 ce_same_name(ce
, active_cache
[i
+1]))
1024 free_pathspec(&revs
->prune_data
);
1025 init_pathspec(&revs
->prune_data
, prune
);
1029 int handle_revision_arg(const char *arg
, struct rev_info
*revs
,
1031 int cant_be_filename
)
1035 struct object
*object
;
1036 unsigned char sha1
[20];
1039 dotdot
= strstr(arg
, "..");
1041 unsigned char from_sha1
[20];
1042 const char *next
= dotdot
+ 2;
1043 const char *this = arg
;
1044 int symmetric
= *next
== '.';
1045 unsigned int flags_exclude
= flags
^ UNINTERESTING
;
1054 if (!get_sha1(this, from_sha1
) &&
1055 !get_sha1(next
, sha1
)) {
1056 struct commit
*a
, *b
;
1057 struct commit_list
*exclude
;
1059 a
= lookup_commit_reference(from_sha1
);
1060 b
= lookup_commit_reference(sha1
);
1062 if (revs
->ignore_missing
)
1065 "Invalid symmetric difference expression %s...%s" :
1066 "Invalid revision range %s..%s",
1070 if (!cant_be_filename
) {
1072 verify_non_filename(revs
->prefix
, arg
);
1076 exclude
= get_merge_bases(a
, b
, 1);
1077 add_pending_commit_list(revs
, exclude
,
1079 free_commit_list(exclude
);
1080 a
->object
.flags
|= flags
| SYMMETRIC_LEFT
;
1082 a
->object
.flags
|= flags_exclude
;
1083 b
->object
.flags
|= flags
;
1084 add_pending_object(revs
, &a
->object
, this);
1085 add_pending_object(revs
, &b
->object
, next
);
1090 dotdot
= strstr(arg
, "^@");
1091 if (dotdot
&& !dotdot
[2]) {
1093 if (add_parents_only(revs
, arg
, flags
))
1097 dotdot
= strstr(arg
, "^!");
1098 if (dotdot
&& !dotdot
[2]) {
1100 if (!add_parents_only(revs
, arg
, flags
^ UNINTERESTING
))
1106 local_flags
= UNINTERESTING
;
1109 if (get_sha1_with_mode(arg
, sha1
, &mode
))
1110 return revs
->ignore_missing
? 0 : -1;
1111 if (!cant_be_filename
)
1112 verify_non_filename(revs
->prefix
, arg
);
1113 object
= get_reference(revs
, arg
, sha1
, flags
^ local_flags
);
1114 add_pending_object_with_mode(revs
, object
, arg
, mode
);
1118 struct cmdline_pathspec
{
1124 static void append_prune_data(struct cmdline_pathspec
*prune
, const char **av
)
1127 ALLOC_GROW(prune
->path
, prune
->nr
+1, prune
->alloc
);
1128 prune
->path
[prune
->nr
++] = *(av
++);
1132 static void read_pathspec_from_stdin(struct rev_info
*revs
, struct strbuf
*sb
,
1133 struct cmdline_pathspec
*prune
)
1135 while (strbuf_getwholeline(sb
, stdin
, '\n') != EOF
) {
1137 if (len
&& sb
->buf
[len
- 1] == '\n')
1138 sb
->buf
[--len
] = '\0';
1139 ALLOC_GROW(prune
->path
, prune
->nr
+1, prune
->alloc
);
1140 prune
->path
[prune
->nr
++] = xstrdup(sb
->buf
);
1144 static void read_revisions_from_stdin(struct rev_info
*revs
,
1145 struct cmdline_pathspec
*prune
)
1148 int seen_dashdash
= 0;
1150 strbuf_init(&sb
, 1000);
1151 while (strbuf_getwholeline(&sb
, stdin
, '\n') != EOF
) {
1153 if (len
&& sb
.buf
[len
- 1] == '\n')
1154 sb
.buf
[--len
] = '\0';
1157 if (sb
.buf
[0] == '-') {
1158 if (len
== 2 && sb
.buf
[1] == '-') {
1162 die("options not supported in --stdin mode");
1164 if (handle_revision_arg(sb
.buf
, revs
, 0, 1))
1165 die("bad revision '%s'", sb
.buf
);
1168 read_pathspec_from_stdin(revs
, &sb
, prune
);
1169 strbuf_release(&sb
);
1172 static void add_grep(struct rev_info
*revs
, const char *ptn
, enum grep_pat_token what
)
1174 append_grep_pattern(&revs
->grep_filter
, ptn
, "command line", 0, what
);
1177 static void add_header_grep(struct rev_info
*revs
, enum grep_header_field field
, const char *pattern
)
1179 append_header_grep_pattern(&revs
->grep_filter
, field
, pattern
);
1182 static void add_message_grep(struct rev_info
*revs
, const char *pattern
)
1184 add_grep(revs
, pattern
, GREP_PATTERN_BODY
);
1187 static int handle_revision_opt(struct rev_info
*revs
, int argc
, const char **argv
,
1188 int *unkc
, const char **unkv
)
1190 const char *arg
= argv
[0];
1194 /* pseudo revision arguments */
1195 if (!strcmp(arg
, "--all") || !strcmp(arg
, "--branches") ||
1196 !strcmp(arg
, "--tags") || !strcmp(arg
, "--remotes") ||
1197 !strcmp(arg
, "--reflog") || !strcmp(arg
, "--not") ||
1198 !strcmp(arg
, "--no-walk") || !strcmp(arg
, "--do-walk") ||
1199 !strcmp(arg
, "--bisect") || !prefixcmp(arg
, "--glob=") ||
1200 !prefixcmp(arg
, "--branches=") || !prefixcmp(arg
, "--tags=") ||
1201 !prefixcmp(arg
, "--remotes="))
1203 unkv
[(*unkc
)++] = arg
;
1207 if ((argcount
= parse_long_opt("max-count", argv
, &optarg
))) {
1208 revs
->max_count
= atoi(optarg
);
1211 } else if ((argcount
= parse_long_opt("skip", argv
, &optarg
))) {
1212 revs
->skip_count
= atoi(optarg
);
1214 } else if ((*arg
== '-') && isdigit(arg
[1])) {
1215 /* accept -<digit>, like traditional "head" */
1216 revs
->max_count
= atoi(arg
+ 1);
1218 } else if (!strcmp(arg
, "-n")) {
1220 return error("-n requires an argument");
1221 revs
->max_count
= atoi(argv
[1]);
1224 } else if (!prefixcmp(arg
, "-n")) {
1225 revs
->max_count
= atoi(arg
+ 2);
1227 } else if ((argcount
= parse_long_opt("max-age", argv
, &optarg
))) {
1228 revs
->max_age
= atoi(optarg
);
1230 } else if ((argcount
= parse_long_opt("since", argv
, &optarg
))) {
1231 revs
->max_age
= approxidate(optarg
);
1233 } else if ((argcount
= parse_long_opt("after", argv
, &optarg
))) {
1234 revs
->max_age
= approxidate(optarg
);
1236 } else if ((argcount
= parse_long_opt("min-age", argv
, &optarg
))) {
1237 revs
->min_age
= atoi(optarg
);
1239 } else if ((argcount
= parse_long_opt("before", argv
, &optarg
))) {
1240 revs
->min_age
= approxidate(optarg
);
1242 } else if ((argcount
= parse_long_opt("until", argv
, &optarg
))) {
1243 revs
->min_age
= approxidate(optarg
);
1245 } else if (!strcmp(arg
, "--first-parent")) {
1246 revs
->first_parent_only
= 1;
1247 } else if (!strcmp(arg
, "--ancestry-path")) {
1248 revs
->ancestry_path
= 1;
1249 revs
->simplify_history
= 0;
1251 } else if (!strcmp(arg
, "-g") || !strcmp(arg
, "--walk-reflogs")) {
1252 init_reflog_walk(&revs
->reflog_info
);
1253 } else if (!strcmp(arg
, "--default")) {
1255 return error("bad --default argument");
1256 revs
->def
= argv
[1];
1258 } else if (!strcmp(arg
, "--merge")) {
1259 revs
->show_merge
= 1;
1260 } else if (!strcmp(arg
, "--topo-order")) {
1262 revs
->topo_order
= 1;
1263 } else if (!strcmp(arg
, "--simplify-merges")) {
1264 revs
->simplify_merges
= 1;
1265 revs
->rewrite_parents
= 1;
1266 revs
->simplify_history
= 0;
1268 } else if (!strcmp(arg
, "--simplify-by-decoration")) {
1269 revs
->simplify_merges
= 1;
1270 revs
->rewrite_parents
= 1;
1271 revs
->simplify_history
= 0;
1272 revs
->simplify_by_decoration
= 1;
1275 load_ref_decorations(DECORATE_SHORT_REFS
);
1276 } else if (!strcmp(arg
, "--date-order")) {
1278 revs
->topo_order
= 1;
1279 } else if (!prefixcmp(arg
, "--early-output")) {
1283 count
= atoi(arg
+15);
1286 revs
->topo_order
= 1;
1287 revs
->early_output
= count
;
1289 } else if (!strcmp(arg
, "--parents")) {
1290 revs
->rewrite_parents
= 1;
1291 revs
->print_parents
= 1;
1292 } else if (!strcmp(arg
, "--dense")) {
1294 } else if (!strcmp(arg
, "--sparse")) {
1296 } else if (!strcmp(arg
, "--show-all")) {
1298 } else if (!strcmp(arg
, "--remove-empty")) {
1299 revs
->remove_empty_trees
= 1;
1300 } else if (!strcmp(arg
, "--merges")) {
1301 revs
->min_parents
= 2;
1302 } else if (!strcmp(arg
, "--no-merges")) {
1303 revs
->max_parents
= 1;
1304 } else if (!prefixcmp(arg
, "--min-parents=")) {
1305 revs
->min_parents
= atoi(arg
+14);
1306 } else if (!prefixcmp(arg
, "--no-min-parents")) {
1307 revs
->min_parents
= 0;
1308 } else if (!prefixcmp(arg
, "--max-parents=")) {
1309 revs
->max_parents
= atoi(arg
+14);
1310 } else if (!prefixcmp(arg
, "--no-max-parents")) {
1311 revs
->max_parents
= -1;
1312 } else if (!strcmp(arg
, "--boundary")) {
1314 } else if (!strcmp(arg
, "--left-right")) {
1315 revs
->left_right
= 1;
1316 } else if (!strcmp(arg
, "--left-only")) {
1317 if (revs
->right_only
)
1318 die("--left-only is incompatible with --right-only"
1320 revs
->left_only
= 1;
1321 } else if (!strcmp(arg
, "--right-only")) {
1322 if (revs
->left_only
)
1323 die("--right-only is incompatible with --left-only");
1324 revs
->right_only
= 1;
1325 } else if (!strcmp(arg
, "--cherry")) {
1326 if (revs
->left_only
)
1327 die("--cherry is incompatible with --left-only");
1328 revs
->cherry_mark
= 1;
1329 revs
->right_only
= 1;
1330 revs
->max_parents
= 1;
1332 } else if (!strcmp(arg
, "--count")) {
1334 } else if (!strcmp(arg
, "--cherry-mark")) {
1335 if (revs
->cherry_pick
)
1336 die("--cherry-mark is incompatible with --cherry-pick");
1337 revs
->cherry_mark
= 1;
1338 revs
->limited
= 1; /* needs limit_list() */
1339 } else if (!strcmp(arg
, "--cherry-pick")) {
1340 if (revs
->cherry_mark
)
1341 die("--cherry-pick is incompatible with --cherry-mark");
1342 revs
->cherry_pick
= 1;
1344 } else if (!strcmp(arg
, "--objects")) {
1345 revs
->tag_objects
= 1;
1346 revs
->tree_objects
= 1;
1347 revs
->blob_objects
= 1;
1348 } else if (!strcmp(arg
, "--objects-edge")) {
1349 revs
->tag_objects
= 1;
1350 revs
->tree_objects
= 1;
1351 revs
->blob_objects
= 1;
1352 revs
->edge_hint
= 1;
1353 } else if (!strcmp(arg
, "--unpacked")) {
1355 } else if (!prefixcmp(arg
, "--unpacked=")) {
1356 die("--unpacked=<packfile> no longer supported.");
1357 } else if (!strcmp(arg
, "-r")) {
1359 DIFF_OPT_SET(&revs
->diffopt
, RECURSIVE
);
1360 } else if (!strcmp(arg
, "-t")) {
1362 DIFF_OPT_SET(&revs
->diffopt
, RECURSIVE
);
1363 DIFF_OPT_SET(&revs
->diffopt
, TREE_IN_RECURSIVE
);
1364 } else if (!strcmp(arg
, "-m")) {
1365 revs
->ignore_merges
= 0;
1366 } else if (!strcmp(arg
, "-c")) {
1368 revs
->dense_combined_merges
= 0;
1369 revs
->combine_merges
= 1;
1370 } else if (!strcmp(arg
, "--cc")) {
1372 revs
->dense_combined_merges
= 1;
1373 revs
->combine_merges
= 1;
1374 } else if (!strcmp(arg
, "-v")) {
1375 revs
->verbose_header
= 1;
1376 } else if (!strcmp(arg
, "--pretty")) {
1377 revs
->verbose_header
= 1;
1378 revs
->pretty_given
= 1;
1379 get_commit_format(arg
+8, revs
);
1380 } else if (!prefixcmp(arg
, "--pretty=") || !prefixcmp(arg
, "--format=")) {
1382 * Detached form ("--pretty X" as opposed to "--pretty=X")
1383 * not allowed, since the argument is optional.
1385 revs
->verbose_header
= 1;
1386 revs
->pretty_given
= 1;
1387 get_commit_format(arg
+9, revs
);
1388 } else if (!strcmp(arg
, "--show-notes") || !strcmp(arg
, "--notes")) {
1389 revs
->show_notes
= 1;
1390 revs
->show_notes_given
= 1;
1391 revs
->notes_opt
.use_default_notes
= 1;
1392 } else if (!prefixcmp(arg
, "--show-notes=") ||
1393 !prefixcmp(arg
, "--notes=")) {
1394 struct strbuf buf
= STRBUF_INIT
;
1395 revs
->show_notes
= 1;
1396 revs
->show_notes_given
= 1;
1397 if (!prefixcmp(arg
, "--show-notes")) {
1398 if (revs
->notes_opt
.use_default_notes
< 0)
1399 revs
->notes_opt
.use_default_notes
= 1;
1400 strbuf_addstr(&buf
, arg
+13);
1403 strbuf_addstr(&buf
, arg
+8);
1404 expand_notes_ref(&buf
);
1405 string_list_append(&revs
->notes_opt
.extra_notes_refs
,
1406 strbuf_detach(&buf
, NULL
));
1407 } else if (!strcmp(arg
, "--no-notes")) {
1408 revs
->show_notes
= 0;
1409 revs
->show_notes_given
= 1;
1410 revs
->notes_opt
.use_default_notes
= -1;
1411 /* we have been strdup'ing ourselves, so trick
1412 * string_list into free()ing strings */
1413 revs
->notes_opt
.extra_notes_refs
.strdup_strings
= 1;
1414 string_list_clear(&revs
->notes_opt
.extra_notes_refs
, 0);
1415 revs
->notes_opt
.extra_notes_refs
.strdup_strings
= 0;
1416 } else if (!strcmp(arg
, "--standard-notes")) {
1417 revs
->show_notes_given
= 1;
1418 revs
->notes_opt
.use_default_notes
= 1;
1419 } else if (!strcmp(arg
, "--no-standard-notes")) {
1420 revs
->notes_opt
.use_default_notes
= 0;
1421 } else if (!strcmp(arg
, "--oneline")) {
1422 revs
->verbose_header
= 1;
1423 get_commit_format("oneline", revs
);
1424 revs
->pretty_given
= 1;
1425 revs
->abbrev_commit
= 1;
1426 } else if (!strcmp(arg
, "--graph")) {
1427 revs
->topo_order
= 1;
1428 revs
->rewrite_parents
= 1;
1429 revs
->graph
= graph_init(revs
);
1430 } else if (!strcmp(arg
, "--root")) {
1431 revs
->show_root_diff
= 1;
1432 } else if (!strcmp(arg
, "--no-commit-id")) {
1433 revs
->no_commit_id
= 1;
1434 } else if (!strcmp(arg
, "--always")) {
1435 revs
->always_show_header
= 1;
1436 } else if (!strcmp(arg
, "--no-abbrev")) {
1438 } else if (!strcmp(arg
, "--abbrev")) {
1439 revs
->abbrev
= DEFAULT_ABBREV
;
1440 } else if (!prefixcmp(arg
, "--abbrev=")) {
1441 revs
->abbrev
= strtoul(arg
+ 9, NULL
, 10);
1442 if (revs
->abbrev
< MINIMUM_ABBREV
)
1443 revs
->abbrev
= MINIMUM_ABBREV
;
1444 else if (revs
->abbrev
> 40)
1446 } else if (!strcmp(arg
, "--abbrev-commit")) {
1447 revs
->abbrev_commit
= 1;
1448 revs
->abbrev_commit_given
= 1;
1449 } else if (!strcmp(arg
, "--no-abbrev-commit")) {
1450 revs
->abbrev_commit
= 0;
1451 } else if (!strcmp(arg
, "--full-diff")) {
1453 revs
->full_diff
= 1;
1454 } else if (!strcmp(arg
, "--full-history")) {
1455 revs
->simplify_history
= 0;
1456 } else if (!strcmp(arg
, "--relative-date")) {
1457 revs
->date_mode
= DATE_RELATIVE
;
1458 revs
->date_mode_explicit
= 1;
1459 } else if ((argcount
= parse_long_opt("date", argv
, &optarg
))) {
1460 revs
->date_mode
= parse_date_format(optarg
);
1461 revs
->date_mode_explicit
= 1;
1463 } else if (!strcmp(arg
, "--log-size")) {
1464 revs
->show_log_size
= 1;
1467 * Grepping the commit log
1469 else if ((argcount
= parse_long_opt("author", argv
, &optarg
))) {
1470 add_header_grep(revs
, GREP_HEADER_AUTHOR
, optarg
);
1472 } else if ((argcount
= parse_long_opt("committer", argv
, &optarg
))) {
1473 add_header_grep(revs
, GREP_HEADER_COMMITTER
, optarg
);
1475 } else if ((argcount
= parse_long_opt("grep", argv
, &optarg
))) {
1476 add_message_grep(revs
, optarg
);
1478 } else if (!strcmp(arg
, "--extended-regexp") || !strcmp(arg
, "-E")) {
1479 revs
->grep_filter
.regflags
|= REG_EXTENDED
;
1480 } else if (!strcmp(arg
, "--regexp-ignore-case") || !strcmp(arg
, "-i")) {
1481 revs
->grep_filter
.regflags
|= REG_ICASE
;
1482 } else if (!strcmp(arg
, "--fixed-strings") || !strcmp(arg
, "-F")) {
1483 revs
->grep_filter
.fixed
= 1;
1484 } else if (!strcmp(arg
, "--all-match")) {
1485 revs
->grep_filter
.all_match
= 1;
1486 } else if ((argcount
= parse_long_opt("encoding", argv
, &optarg
))) {
1487 if (strcmp(optarg
, "none"))
1488 git_log_output_encoding
= xstrdup(optarg
);
1490 git_log_output_encoding
= "";
1492 } else if (!strcmp(arg
, "--reverse")) {
1494 } else if (!strcmp(arg
, "--children")) {
1495 revs
->children
.name
= "children";
1497 } else if (!strcmp(arg
, "--ignore-missing")) {
1498 revs
->ignore_missing
= 1;
1500 int opts
= diff_opt_parse(&revs
->diffopt
, argv
, argc
);
1502 unkv
[(*unkc
)++] = arg
;
1509 void parse_revision_opt(struct rev_info
*revs
, struct parse_opt_ctx_t
*ctx
,
1510 const struct option
*options
,
1511 const char * const usagestr
[])
1513 int n
= handle_revision_opt(revs
, ctx
->argc
, ctx
->argv
,
1514 &ctx
->cpidx
, ctx
->out
);
1516 error("unknown option `%s'", ctx
->argv
[0]);
1517 usage_with_options(usagestr
, options
);
1523 static int for_each_bad_bisect_ref(const char *submodule
, each_ref_fn fn
, void *cb_data
)
1525 return for_each_ref_in_submodule(submodule
, "refs/bisect/bad", fn
, cb_data
);
1528 static int for_each_good_bisect_ref(const char *submodule
, each_ref_fn fn
, void *cb_data
)
1530 return for_each_ref_in_submodule(submodule
, "refs/bisect/good", fn
, cb_data
);
1533 static int handle_revision_pseudo_opt(const char *submodule
,
1534 struct rev_info
*revs
,
1535 int argc
, const char **argv
, int *flags
)
1537 const char *arg
= argv
[0];
1544 * Commands like "git shortlog" will not accept the options below
1545 * unless parse_revision_opt queues them (as opposed to erroring
1548 * When implementing your new pseudo-option, remember to
1549 * register it in the list at the top of handle_revision_opt.
1551 if (!strcmp(arg
, "--all")) {
1552 handle_refs(submodule
, revs
, *flags
, for_each_ref_submodule
);
1553 handle_refs(submodule
, revs
, *flags
, head_ref_submodule
);
1554 } else if (!strcmp(arg
, "--branches")) {
1555 handle_refs(submodule
, revs
, *flags
, for_each_branch_ref_submodule
);
1556 } else if (!strcmp(arg
, "--bisect")) {
1557 handle_refs(submodule
, revs
, *flags
, for_each_bad_bisect_ref
);
1558 handle_refs(submodule
, revs
, *flags
^ UNINTERESTING
, for_each_good_bisect_ref
);
1560 } else if (!strcmp(arg
, "--tags")) {
1561 handle_refs(submodule
, revs
, *flags
, for_each_tag_ref_submodule
);
1562 } else if (!strcmp(arg
, "--remotes")) {
1563 handle_refs(submodule
, revs
, *flags
, for_each_remote_ref_submodule
);
1564 } else if ((argcount
= parse_long_opt("glob", argv
, &optarg
))) {
1565 struct all_refs_cb cb
;
1566 init_all_refs_cb(&cb
, revs
, *flags
);
1567 for_each_glob_ref(handle_one_ref
, optarg
, &cb
);
1569 } else if (!prefixcmp(arg
, "--branches=")) {
1570 struct all_refs_cb cb
;
1571 init_all_refs_cb(&cb
, revs
, *flags
);
1572 for_each_glob_ref_in(handle_one_ref
, arg
+ 11, "refs/heads/", &cb
);
1573 } else if (!prefixcmp(arg
, "--tags=")) {
1574 struct all_refs_cb cb
;
1575 init_all_refs_cb(&cb
, revs
, *flags
);
1576 for_each_glob_ref_in(handle_one_ref
, arg
+ 7, "refs/tags/", &cb
);
1577 } else if (!prefixcmp(arg
, "--remotes=")) {
1578 struct all_refs_cb cb
;
1579 init_all_refs_cb(&cb
, revs
, *flags
);
1580 for_each_glob_ref_in(handle_one_ref
, arg
+ 10, "refs/remotes/", &cb
);
1581 } else if (!strcmp(arg
, "--reflog")) {
1582 handle_reflog(revs
, *flags
);
1583 } else if (!strcmp(arg
, "--not")) {
1584 *flags
^= UNINTERESTING
;
1585 } else if (!strcmp(arg
, "--no-walk")) {
1587 } else if (!strcmp(arg
, "--do-walk")) {
1597 * Parse revision information, filling in the "rev_info" structure,
1598 * and removing the used arguments from the argument list.
1600 * Returns the number of arguments left that weren't recognized
1601 * (which are also moved to the head of the argument list)
1603 int setup_revisions(int argc
, const char **argv
, struct rev_info
*revs
, struct setup_revision_opt
*opt
)
1605 int i
, flags
, left
, seen_dashdash
, read_from_stdin
, got_rev_arg
= 0;
1606 struct cmdline_pathspec prune_data
;
1607 const char *submodule
= NULL
;
1609 memset(&prune_data
, 0, sizeof(prune_data
));
1611 submodule
= opt
->submodule
;
1613 /* First, search for "--" */
1615 for (i
= 1; i
< argc
; i
++) {
1616 const char *arg
= argv
[i
];
1617 if (strcmp(arg
, "--"))
1622 append_prune_data(&prune_data
, argv
+ i
+ 1);
1627 /* Second, deal with arguments and options */
1629 read_from_stdin
= 0;
1630 for (left
= i
= 1; i
< argc
; i
++) {
1631 const char *arg
= argv
[i
];
1635 opts
= handle_revision_pseudo_opt(submodule
,
1636 revs
, argc
- i
, argv
+ i
,
1643 if (!strcmp(arg
, "--stdin")) {
1644 if (revs
->disable_stdin
) {
1648 if (read_from_stdin
++)
1649 die("--stdin given twice?");
1650 read_revisions_from_stdin(revs
, &prune_data
);
1654 opts
= handle_revision_opt(revs
, argc
- i
, argv
+ i
, &left
, argv
);
1664 if (handle_revision_arg(arg
, revs
, flags
, seen_dashdash
)) {
1666 if (seen_dashdash
|| *arg
== '^')
1667 die("bad revision '%s'", arg
);
1669 /* If we didn't have a "--":
1670 * (1) all filenames must exist;
1671 * (2) all rev-args must not be interpretable
1672 * as a valid filename.
1673 * but the latter we have checked in the main loop.
1675 for (j
= i
; j
< argc
; j
++)
1676 verify_filename(revs
->prefix
, argv
[j
]);
1678 append_prune_data(&prune_data
, argv
+ i
);
1685 if (prune_data
.nr
) {
1687 * If we need to introduce the magic "a lone ':' means no
1688 * pathspec whatsoever", here is the place to do so.
1690 * if (prune_data.nr == 1 && !strcmp(prune_data[0], ":")) {
1691 * prune_data.nr = 0;
1692 * prune_data.alloc = 0;
1693 * free(prune_data.path);
1694 * prune_data.path = NULL;
1696 * terminate prune_data.alloc with NULL and
1697 * call init_pathspec() to set revs->prune_data here.
1700 ALLOC_GROW(prune_data
.path
, prune_data
.nr
+1, prune_data
.alloc
);
1701 prune_data
.path
[prune_data
.nr
++] = NULL
;
1702 init_pathspec(&revs
->prune_data
,
1703 get_pathspec(revs
->prefix
, prune_data
.path
));
1706 if (revs
->def
== NULL
)
1707 revs
->def
= opt
? opt
->def
: NULL
;
1708 if (opt
&& opt
->tweak
)
1709 opt
->tweak(revs
, opt
);
1710 if (revs
->show_merge
)
1711 prepare_show_merge(revs
);
1712 if (revs
->def
&& !revs
->pending
.nr
&& !got_rev_arg
) {
1713 unsigned char sha1
[20];
1714 struct object
*object
;
1716 if (get_sha1_with_mode(revs
->def
, sha1
, &mode
))
1717 die("bad default revision '%s'", revs
->def
);
1718 object
= get_reference(revs
, revs
->def
, sha1
, 0);
1719 add_pending_object_with_mode(revs
, object
, revs
->def
, mode
);
1722 /* Did the user ask for any diff output? Run the diff! */
1723 if (revs
->diffopt
.output_format
& ~DIFF_FORMAT_NO_OUTPUT
)
1726 /* Pickaxe, diff-filter and rename following need diffs */
1727 if (revs
->diffopt
.pickaxe
||
1728 revs
->diffopt
.filter
||
1729 DIFF_OPT_TST(&revs
->diffopt
, FOLLOW_RENAMES
))
1732 if (revs
->topo_order
)
1735 if (revs
->prune_data
.nr
) {
1736 diff_tree_setup_paths(revs
->prune_data
.raw
, &revs
->pruning
);
1737 /* Can't prune commits with rename following: the paths change.. */
1738 if (!DIFF_OPT_TST(&revs
->diffopt
, FOLLOW_RENAMES
))
1740 if (!revs
->full_diff
)
1741 diff_tree_setup_paths(revs
->prune_data
.raw
, &revs
->diffopt
);
1743 if (revs
->combine_merges
)
1744 revs
->ignore_merges
= 0;
1745 revs
->diffopt
.abbrev
= revs
->abbrev
;
1746 if (diff_setup_done(&revs
->diffopt
) < 0)
1747 die("diff_setup_done failed");
1749 compile_grep_patterns(&revs
->grep_filter
);
1751 if (revs
->reverse
&& revs
->reflog_info
)
1752 die("cannot combine --reverse with --walk-reflogs");
1753 if (revs
->rewrite_parents
&& revs
->children
.name
)
1754 die("cannot combine --parents and --children");
1757 * Limitations on the graph functionality
1759 if (revs
->reverse
&& revs
->graph
)
1760 die("cannot combine --reverse with --graph");
1762 if (revs
->reflog_info
&& revs
->graph
)
1763 die("cannot combine --walk-reflogs with --graph");
1768 static void add_child(struct rev_info
*revs
, struct commit
*parent
, struct commit
*child
)
1770 struct commit_list
*l
= xcalloc(1, sizeof(*l
));
1773 l
->next
= add_decoration(&revs
->children
, &parent
->object
, l
);
1776 static int remove_duplicate_parents(struct commit
*commit
)
1778 struct commit_list
**pp
, *p
;
1779 int surviving_parents
;
1781 /* Examine existing parents while marking ones we have seen... */
1782 pp
= &commit
->parents
;
1783 while ((p
= *pp
) != NULL
) {
1784 struct commit
*parent
= p
->item
;
1785 if (parent
->object
.flags
& TMP_MARK
) {
1789 parent
->object
.flags
|= TMP_MARK
;
1792 /* count them while clearing the temporary mark */
1793 surviving_parents
= 0;
1794 for (p
= commit
->parents
; p
; p
= p
->next
) {
1795 p
->item
->object
.flags
&= ~TMP_MARK
;
1796 surviving_parents
++;
1798 return surviving_parents
;
1801 struct merge_simplify_state
{
1802 struct commit
*simplified
;
1805 static struct merge_simplify_state
*locate_simplify_state(struct rev_info
*revs
, struct commit
*commit
)
1807 struct merge_simplify_state
*st
;
1809 st
= lookup_decoration(&revs
->merge_simplification
, &commit
->object
);
1811 st
= xcalloc(1, sizeof(*st
));
1812 add_decoration(&revs
->merge_simplification
, &commit
->object
, st
);
1817 static struct commit_list
**simplify_one(struct rev_info
*revs
, struct commit
*commit
, struct commit_list
**tail
)
1819 struct commit_list
*p
;
1820 struct merge_simplify_state
*st
, *pst
;
1823 st
= locate_simplify_state(revs
, commit
);
1826 * Have we handled this one?
1832 * An UNINTERESTING commit simplifies to itself, so does a
1833 * root commit. We do not rewrite parents of such commit
1836 if ((commit
->object
.flags
& UNINTERESTING
) || !commit
->parents
) {
1837 st
->simplified
= commit
;
1842 * Do we know what commit all of our parents should be rewritten to?
1843 * Otherwise we are not ready to rewrite this one yet.
1845 for (cnt
= 0, p
= commit
->parents
; p
; p
= p
->next
) {
1846 pst
= locate_simplify_state(revs
, p
->item
);
1847 if (!pst
->simplified
) {
1848 tail
= &commit_list_insert(p
->item
, tail
)->next
;
1853 tail
= &commit_list_insert(commit
, tail
)->next
;
1858 * Rewrite our list of parents.
1860 for (p
= commit
->parents
; p
; p
= p
->next
) {
1861 pst
= locate_simplify_state(revs
, p
->item
);
1862 p
->item
= pst
->simplified
;
1864 cnt
= remove_duplicate_parents(commit
);
1867 * It is possible that we are a merge and one side branch
1868 * does not have any commit that touches the given paths;
1869 * in such a case, the immediate parents will be rewritten
1870 * to different commits.
1872 * o----X X: the commit we are looking at;
1873 * / / o: a commit that touches the paths;
1876 * Further reduce the parents by removing redundant parents.
1879 struct commit_list
*h
= reduce_heads(commit
->parents
);
1880 cnt
= commit_list_count(h
);
1881 free_commit_list(commit
->parents
);
1882 commit
->parents
= h
;
1886 * A commit simplifies to itself if it is a root, if it is
1887 * UNINTERESTING, if it touches the given paths, or if it is a
1888 * merge and its parents simplifies to more than one commits
1889 * (the first two cases are already handled at the beginning of
1892 * Otherwise, it simplifies to what its sole parent simplifies to.
1895 (commit
->object
.flags
& UNINTERESTING
) ||
1896 !(commit
->object
.flags
& TREESAME
) ||
1898 st
->simplified
= commit
;
1900 pst
= locate_simplify_state(revs
, commit
->parents
->item
);
1901 st
->simplified
= pst
->simplified
;
1906 static void simplify_merges(struct rev_info
*revs
)
1908 struct commit_list
*list
;
1909 struct commit_list
*yet_to_do
, **tail
;
1911 if (!revs
->topo_order
)
1912 sort_in_topological_order(&revs
->commits
, revs
->lifo
);
1916 /* feed the list reversed */
1918 for (list
= revs
->commits
; list
; list
= list
->next
)
1919 commit_list_insert(list
->item
, &yet_to_do
);
1925 struct commit
*commit
= list
->item
;
1926 struct commit_list
*next
= list
->next
;
1929 tail
= simplify_one(revs
, commit
, tail
);
1933 /* clean up the result, removing the simplified ones */
1934 list
= revs
->commits
;
1935 revs
->commits
= NULL
;
1936 tail
= &revs
->commits
;
1938 struct commit
*commit
= list
->item
;
1939 struct commit_list
*next
= list
->next
;
1940 struct merge_simplify_state
*st
;
1943 st
= locate_simplify_state(revs
, commit
);
1944 if (st
->simplified
== commit
)
1945 tail
= &commit_list_insert(commit
, tail
)->next
;
1949 static void set_children(struct rev_info
*revs
)
1951 struct commit_list
*l
;
1952 for (l
= revs
->commits
; l
; l
= l
->next
) {
1953 struct commit
*commit
= l
->item
;
1954 struct commit_list
*p
;
1956 for (p
= commit
->parents
; p
; p
= p
->next
)
1957 add_child(revs
, p
->item
, commit
);
1961 int prepare_revision_walk(struct rev_info
*revs
)
1963 int nr
= revs
->pending
.nr
;
1964 struct object_array_entry
*e
, *list
;
1966 e
= list
= revs
->pending
.objects
;
1967 revs
->pending
.nr
= 0;
1968 revs
->pending
.alloc
= 0;
1969 revs
->pending
.objects
= NULL
;
1971 struct commit
*commit
= handle_commit(revs
, e
->item
, e
->name
);
1973 if (!(commit
->object
.flags
& SEEN
)) {
1974 commit
->object
.flags
|= SEEN
;
1975 commit_list_insert_by_date(commit
, &revs
->commits
);
1985 if (limit_list(revs
) < 0)
1987 if (revs
->topo_order
)
1988 sort_in_topological_order(&revs
->commits
, revs
->lifo
);
1989 if (revs
->simplify_merges
)
1990 simplify_merges(revs
);
1991 if (revs
->children
.name
)
1996 enum rewrite_result
{
1998 rewrite_one_noparents
,
2002 static enum rewrite_result
rewrite_one(struct rev_info
*revs
, struct commit
**pp
)
2004 struct commit_list
*cache
= NULL
;
2007 struct commit
*p
= *pp
;
2009 if (add_parents_to_list(revs
, p
, &revs
->commits
, &cache
) < 0)
2010 return rewrite_one_error
;
2011 if (p
->parents
&& p
->parents
->next
)
2012 return rewrite_one_ok
;
2013 if (p
->object
.flags
& UNINTERESTING
)
2014 return rewrite_one_ok
;
2015 if (!(p
->object
.flags
& TREESAME
))
2016 return rewrite_one_ok
;
2018 return rewrite_one_noparents
;
2019 *pp
= p
->parents
->item
;
2023 static int rewrite_parents(struct rev_info
*revs
, struct commit
*commit
)
2025 struct commit_list
**pp
= &commit
->parents
;
2027 struct commit_list
*parent
= *pp
;
2028 switch (rewrite_one(revs
, &parent
->item
)) {
2029 case rewrite_one_ok
:
2031 case rewrite_one_noparents
:
2034 case rewrite_one_error
:
2039 remove_duplicate_parents(commit
);
2043 static int commit_match(struct commit
*commit
, struct rev_info
*opt
)
2045 if (!opt
->grep_filter
.pattern_list
&& !opt
->grep_filter
.header_list
)
2047 return grep_buffer(&opt
->grep_filter
,
2048 NULL
, /* we say nothing, not even filename */
2049 commit
->buffer
, strlen(commit
->buffer
));
2052 static inline int want_ancestry(struct rev_info
*revs
)
2054 return (revs
->rewrite_parents
|| revs
->children
.name
);
2057 enum commit_action
get_commit_action(struct rev_info
*revs
, struct commit
*commit
)
2059 if (commit
->object
.flags
& SHOWN
)
2060 return commit_ignore
;
2061 if (revs
->unpacked
&& has_sha1_pack(commit
->object
.sha1
))
2062 return commit_ignore
;
2065 if (commit
->object
.flags
& UNINTERESTING
)
2066 return commit_ignore
;
2067 if (revs
->min_age
!= -1 && (commit
->date
> revs
->min_age
))
2068 return commit_ignore
;
2069 if (revs
->min_parents
|| (revs
->max_parents
>= 0)) {
2071 struct commit_list
*p
;
2072 for (p
= commit
->parents
; p
; p
= p
->next
)
2074 if ((n
< revs
->min_parents
) ||
2075 ((revs
->max_parents
>= 0) && (n
> revs
->max_parents
)))
2076 return commit_ignore
;
2078 if (!commit_match(commit
, revs
))
2079 return commit_ignore
;
2080 if (revs
->prune
&& revs
->dense
) {
2081 /* Commit without changes? */
2082 if (commit
->object
.flags
& TREESAME
) {
2083 /* drop merges unless we want parenthood */
2084 if (!want_ancestry(revs
))
2085 return commit_ignore
;
2086 /* non-merge - always ignore it */
2087 if (!commit
->parents
|| !commit
->parents
->next
)
2088 return commit_ignore
;
2094 enum commit_action
simplify_commit(struct rev_info
*revs
, struct commit
*commit
)
2096 enum commit_action action
= get_commit_action(revs
, commit
);
2098 if (action
== commit_show
&&
2100 revs
->prune
&& revs
->dense
&& want_ancestry(revs
)) {
2101 if (rewrite_parents(revs
, commit
) < 0)
2102 return commit_error
;
2107 static struct commit
*get_revision_1(struct rev_info
*revs
)
2113 struct commit_list
*entry
= revs
->commits
;
2114 struct commit
*commit
= entry
->item
;
2116 revs
->commits
= entry
->next
;
2119 if (revs
->reflog_info
) {
2120 fake_reflog_parent(revs
->reflog_info
, commit
);
2121 commit
->object
.flags
&= ~(ADDED
| SEEN
| SHOWN
);
2125 * If we haven't done the list limiting, we need to look at
2126 * the parents here. We also need to do the date-based limiting
2127 * that we'd otherwise have done in limit_list().
2129 if (!revs
->limited
) {
2130 if (revs
->max_age
!= -1 &&
2131 (commit
->date
< revs
->max_age
))
2133 if (add_parents_to_list(revs
, commit
, &revs
->commits
, NULL
) < 0)
2134 die("Failed to traverse parents of commit %s",
2135 sha1_to_hex(commit
->object
.sha1
));
2138 switch (simplify_commit(revs
, commit
)) {
2142 die("Failed to simplify parents of commit %s",
2143 sha1_to_hex(commit
->object
.sha1
));
2147 } while (revs
->commits
);
2151 static void gc_boundary(struct object_array
*array
)
2153 unsigned nr
= array
->nr
;
2154 unsigned alloc
= array
->alloc
;
2155 struct object_array_entry
*objects
= array
->objects
;
2159 for (i
= j
= 0; i
< nr
; i
++) {
2160 if (objects
[i
].item
->flags
& SHOWN
)
2163 objects
[j
] = objects
[i
];
2166 for (i
= j
; i
< nr
; i
++)
2167 objects
[i
].item
= NULL
;
2172 static void create_boundary_commit_list(struct rev_info
*revs
)
2176 struct object_array
*array
= &revs
->boundary_commits
;
2177 struct object_array_entry
*objects
= array
->objects
;
2180 * If revs->commits is non-NULL at this point, an error occurred in
2181 * get_revision_1(). Ignore the error and continue printing the
2182 * boundary commits anyway. (This is what the code has always
2185 if (revs
->commits
) {
2186 free_commit_list(revs
->commits
);
2187 revs
->commits
= NULL
;
2191 * Put all of the actual boundary commits from revs->boundary_commits
2192 * into revs->commits
2194 for (i
= 0; i
< array
->nr
; i
++) {
2195 c
= (struct commit
*)(objects
[i
].item
);
2198 if (!(c
->object
.flags
& CHILD_SHOWN
))
2200 if (c
->object
.flags
& (SHOWN
| BOUNDARY
))
2202 c
->object
.flags
|= BOUNDARY
;
2203 commit_list_insert(c
, &revs
->commits
);
2207 * If revs->topo_order is set, sort the boundary commits
2208 * in topological order
2210 sort_in_topological_order(&revs
->commits
, revs
->lifo
);
2213 static struct commit
*get_revision_internal(struct rev_info
*revs
)
2215 struct commit
*c
= NULL
;
2216 struct commit_list
*l
;
2218 if (revs
->boundary
== 2) {
2220 * All of the normal commits have already been returned,
2221 * and we are now returning boundary commits.
2222 * create_boundary_commit_list() has populated
2223 * revs->commits with the remaining commits to return.
2225 c
= pop_commit(&revs
->commits
);
2227 c
->object
.flags
|= SHOWN
;
2232 * Now pick up what they want to give us
2234 c
= get_revision_1(revs
);
2236 while (0 < revs
->skip_count
) {
2238 c
= get_revision_1(revs
);
2245 * Check the max_count.
2247 switch (revs
->max_count
) {
2258 c
->object
.flags
|= SHOWN
;
2260 if (!revs
->boundary
) {
2266 * get_revision_1() runs out the commits, and
2267 * we are done computing the boundaries.
2268 * switch to boundary commits output mode.
2273 * Update revs->commits to contain the list of
2276 create_boundary_commit_list(revs
);
2278 return get_revision_internal(revs
);
2282 * boundary commits are the commits that are parents of the
2283 * ones we got from get_revision_1() but they themselves are
2284 * not returned from get_revision_1(). Before returning
2285 * 'c', we need to mark its parents that they could be boundaries.
2288 for (l
= c
->parents
; l
; l
= l
->next
) {
2290 p
= &(l
->item
->object
);
2291 if (p
->flags
& (CHILD_SHOWN
| SHOWN
))
2293 p
->flags
|= CHILD_SHOWN
;
2294 gc_boundary(&revs
->boundary_commits
);
2295 add_object_array(p
, NULL
, &revs
->boundary_commits
);
2301 struct commit
*get_revision(struct rev_info
*revs
)
2304 struct commit_list
*reversed
;
2306 if (revs
->reverse
) {
2308 while ((c
= get_revision_internal(revs
))) {
2309 commit_list_insert(c
, &reversed
);
2311 revs
->commits
= reversed
;
2313 revs
->reverse_output_stage
= 1;
2316 if (revs
->reverse_output_stage
)
2317 return pop_commit(&revs
->commits
);
2319 c
= get_revision_internal(revs
);
2320 if (c
&& revs
->graph
)
2321 graph_update(revs
->graph
, c
);
2325 char *get_revision_mark(const struct rev_info
*revs
, const struct commit
*commit
)
2327 if (commit
->object
.flags
& BOUNDARY
)
2329 else if (commit
->object
.flags
& UNINTERESTING
)
2331 else if (commit
->object
.flags
& PATCHSAME
)
2333 else if (!revs
|| revs
->left_right
) {
2334 if (commit
->object
.flags
& SYMMETRIC_LEFT
)
2338 } else if (revs
->graph
)
2340 else if (revs
->cherry_mark
)
2345 void put_revision_mark(const struct rev_info
*revs
, const struct commit
*commit
)
2347 char *mark
= get_revision_mark(revs
, commit
);
2350 fputs(mark
, stdout
);