11 #include "reflog-walk.h"
12 #include "patch-ids.h"
15 #include "string-list.h"
18 volatile show_early_output_fn_t show_early_output
;
20 char *path_name(const struct name_path
*path
, const char *name
)
22 const struct name_path
*p
;
24 int nlen
= strlen(name
);
27 for (p
= path
; p
; p
= p
->up
) {
29 len
+= p
->elem_len
+ 1;
32 m
= n
+ len
- (nlen
+ 1);
34 for (p
= path
; p
; p
= p
->up
) {
37 memcpy(m
, p
->elem
, p
->elem_len
);
44 void add_object(struct object
*obj
,
45 struct object_array
*p
,
46 struct name_path
*path
,
49 add_object_array(obj
, path_name(path
, name
), p
);
52 static void mark_blob_uninteresting(struct blob
*blob
)
56 if (blob
->object
.flags
& UNINTERESTING
)
58 blob
->object
.flags
|= UNINTERESTING
;
61 void mark_tree_uninteresting(struct tree
*tree
)
63 struct tree_desc desc
;
64 struct name_entry entry
;
65 struct object
*obj
= &tree
->object
;
69 if (obj
->flags
& UNINTERESTING
)
71 obj
->flags
|= UNINTERESTING
;
72 if (!has_sha1_file(obj
->sha1
))
74 if (parse_tree(tree
) < 0)
75 die("bad tree %s", sha1_to_hex(obj
->sha1
));
77 init_tree_desc(&desc
, tree
->buffer
, tree
->size
);
78 while (tree_entry(&desc
, &entry
)) {
79 switch (object_type(entry
.mode
)) {
81 mark_tree_uninteresting(lookup_tree(entry
.sha1
));
84 mark_blob_uninteresting(lookup_blob(entry
.sha1
));
87 /* Subproject commit - not in this repository */
93 * We don't care about the tree any more
94 * after it has been marked uninteresting.
100 void mark_parents_uninteresting(struct commit
*commit
)
102 struct commit_list
*parents
= commit
->parents
;
105 struct commit
*commit
= parents
->item
;
106 if (!(commit
->object
.flags
& UNINTERESTING
)) {
107 commit
->object
.flags
|= UNINTERESTING
;
110 * Normally we haven't parsed the parent
111 * yet, so we won't have a parent of a parent
112 * here. However, it may turn out that we've
113 * reached this commit some other way (where it
114 * wasn't uninteresting), in which case we need
115 * to mark its parents recursively too..
118 mark_parents_uninteresting(commit
);
122 * A missing commit is ok iff its parent is marked
125 * We just mark such a thing parsed, so that when
126 * it is popped next time around, we won't be trying
127 * to parse it and get an error.
129 if (!has_sha1_file(commit
->object
.sha1
))
130 commit
->object
.parsed
= 1;
131 parents
= parents
->next
;
135 static void add_pending_object_with_mode(struct rev_info
*revs
, struct object
*obj
, const char *name
, unsigned mode
)
137 if (revs
->no_walk
&& (obj
->flags
& UNINTERESTING
))
139 if (revs
->reflog_info
&& obj
->type
== OBJ_COMMIT
) {
140 struct strbuf buf
= STRBUF_INIT
;
141 int len
= interpret_branch_name(name
, &buf
);
144 if (0 < len
&& name
[len
] && buf
.len
)
145 strbuf_addstr(&buf
, name
+ len
);
146 st
= add_reflog_for_walk(revs
->reflog_info
,
147 (struct commit
*)obj
,
148 buf
.buf
[0] ? buf
.buf
: name
);
149 strbuf_release(&buf
);
153 add_object_array_with_mode(obj
, name
, &revs
->pending
, mode
);
156 void add_pending_object(struct rev_info
*revs
, struct object
*obj
, const char *name
)
158 add_pending_object_with_mode(revs
, obj
, name
, S_IFINVALID
);
161 void add_head_to_pending(struct rev_info
*revs
)
163 unsigned char sha1
[20];
165 if (get_sha1("HEAD", sha1
))
167 obj
= parse_object(sha1
);
170 add_pending_object(revs
, obj
, "HEAD");
173 static struct object
*get_reference(struct rev_info
*revs
, const char *name
, const unsigned char *sha1
, unsigned int flags
)
175 struct object
*object
;
177 object
= parse_object(sha1
);
179 die("bad object %s", name
);
180 object
->flags
|= flags
;
184 static struct commit
*handle_commit(struct rev_info
*revs
, struct object
*object
, const char *name
)
186 unsigned long flags
= object
->flags
;
189 * Tag object? Look what it points to..
191 while (object
->type
== OBJ_TAG
) {
192 struct tag
*tag
= (struct tag
*) object
;
193 if (revs
->tag_objects
&& !(flags
& UNINTERESTING
))
194 add_pending_object(revs
, object
, tag
->tag
);
197 object
= parse_object(tag
->tagged
->sha1
);
199 if (flags
& UNINTERESTING
)
201 die("bad object %s", sha1_to_hex(tag
->tagged
->sha1
));
206 * Commit object? Just return it, we'll do all the complex
209 if (object
->type
== OBJ_COMMIT
) {
210 struct commit
*commit
= (struct commit
*)object
;
211 if (parse_commit(commit
) < 0)
212 die("unable to parse commit %s", name
);
213 if (flags
& UNINTERESTING
) {
214 commit
->object
.flags
|= UNINTERESTING
;
215 mark_parents_uninteresting(commit
);
218 if (revs
->show_source
&& !commit
->util
)
219 commit
->util
= (void *) name
;
224 * Tree object? Either mark it uninteresting, or add it
225 * to the list of objects to look at later..
227 if (object
->type
== OBJ_TREE
) {
228 struct tree
*tree
= (struct tree
*)object
;
229 if (!revs
->tree_objects
)
231 if (flags
& UNINTERESTING
) {
232 mark_tree_uninteresting(tree
);
235 add_pending_object(revs
, object
, "");
240 * Blob object? You know the drill by now..
242 if (object
->type
== OBJ_BLOB
) {
243 struct blob
*blob
= (struct blob
*)object
;
244 if (!revs
->blob_objects
)
246 if (flags
& UNINTERESTING
) {
247 mark_blob_uninteresting(blob
);
250 add_pending_object(revs
, object
, "");
253 die("%s is unknown object", name
);
256 static int everybody_uninteresting(struct commit_list
*orig
)
258 struct commit_list
*list
= orig
;
260 struct commit
*commit
= list
->item
;
262 if (commit
->object
.flags
& UNINTERESTING
)
270 * The goal is to get REV_TREE_NEW as the result only if the
271 * diff consists of all '+' (and no other changes), REV_TREE_OLD
272 * if the whole diff is removal of old data, and otherwise
273 * REV_TREE_DIFFERENT (of course if the trees are the same we
274 * want REV_TREE_SAME).
275 * That means that once we get to REV_TREE_DIFFERENT, we do not
276 * have to look any further.
278 static int tree_difference
= REV_TREE_SAME
;
280 static void file_add_remove(struct diff_options
*options
,
281 int addremove
, unsigned mode
,
282 const unsigned char *sha1
,
283 const char *fullpath
, unsigned dirty_submodule
)
285 int diff
= addremove
== '+' ? REV_TREE_NEW
: REV_TREE_OLD
;
287 tree_difference
|= diff
;
288 if (tree_difference
== REV_TREE_DIFFERENT
)
289 DIFF_OPT_SET(options
, HAS_CHANGES
);
292 static void file_change(struct diff_options
*options
,
293 unsigned old_mode
, unsigned new_mode
,
294 const unsigned char *old_sha1
,
295 const unsigned char *new_sha1
,
296 const char *fullpath
,
297 unsigned old_dirty_submodule
, unsigned new_dirty_submodule
)
299 tree_difference
= REV_TREE_DIFFERENT
;
300 DIFF_OPT_SET(options
, HAS_CHANGES
);
303 static int rev_compare_tree(struct rev_info
*revs
, struct commit
*parent
, struct commit
*commit
)
305 struct tree
*t1
= parent
->tree
;
306 struct tree
*t2
= commit
->tree
;
313 if (revs
->simplify_by_decoration
) {
315 * If we are simplifying by decoration, then the commit
316 * is worth showing if it has a tag pointing at it.
318 if (lookup_decoration(&name_decoration
, &commit
->object
))
319 return REV_TREE_DIFFERENT
;
321 * A commit that is not pointed by a tag is uninteresting
322 * if we are not limited by path. This means that you will
323 * see the usual "commits that touch the paths" plus any
324 * tagged commit by specifying both --simplify-by-decoration
327 if (!revs
->prune_data
)
328 return REV_TREE_SAME
;
331 tree_difference
= REV_TREE_SAME
;
332 DIFF_OPT_CLR(&revs
->pruning
, HAS_CHANGES
);
333 if (diff_tree_sha1(t1
->object
.sha1
, t2
->object
.sha1
, "",
335 return REV_TREE_DIFFERENT
;
336 return tree_difference
;
339 static int rev_same_tree_as_empty(struct rev_info
*revs
, struct commit
*commit
)
344 struct tree_desc empty
, real
;
345 struct tree
*t1
= commit
->tree
;
350 tree
= read_object_with_reference(t1
->object
.sha1
, tree_type
, &size
, NULL
);
353 init_tree_desc(&real
, tree
, size
);
354 init_tree_desc(&empty
, "", 0);
356 tree_difference
= REV_TREE_SAME
;
357 DIFF_OPT_CLR(&revs
->pruning
, HAS_CHANGES
);
358 retval
= diff_tree(&empty
, &real
, "", &revs
->pruning
);
361 return retval
>= 0 && (tree_difference
== REV_TREE_SAME
);
364 static void try_to_simplify_commit(struct rev_info
*revs
, struct commit
*commit
)
366 struct commit_list
**pp
, *parent
;
367 int tree_changed
= 0, tree_same
= 0;
370 * If we don't do pruning, everything is interesting
378 if (!commit
->parents
) {
379 if (rev_same_tree_as_empty(revs
, commit
))
380 commit
->object
.flags
|= TREESAME
;
385 * Normal non-merge commit? If we don't want to make the
386 * history dense, we consider it always to be a change..
388 if (!revs
->dense
&& !commit
->parents
->next
)
391 pp
= &commit
->parents
;
392 while ((parent
= *pp
) != NULL
) {
393 struct commit
*p
= parent
->item
;
395 if (parse_commit(p
) < 0)
396 die("cannot simplify commit %s (because of %s)",
397 sha1_to_hex(commit
->object
.sha1
),
398 sha1_to_hex(p
->object
.sha1
));
399 switch (rev_compare_tree(revs
, p
, commit
)) {
402 if (!revs
->simplify_history
|| (p
->object
.flags
& UNINTERESTING
)) {
403 /* Even if a merge with an uninteresting
404 * side branch brought the entire change
405 * we are interested in, we do not want
406 * to lose the other branches of this
407 * merge, so we just keep going.
413 commit
->parents
= parent
;
414 commit
->object
.flags
|= TREESAME
;
418 if (revs
->remove_empty_trees
&&
419 rev_same_tree_as_empty(revs
, p
)) {
420 /* We are adding all the specified
421 * paths from this parent, so the
422 * history beyond this parent is not
423 * interesting. Remove its parents
424 * (they are grandparents for us).
425 * IOW, we pretend this parent is a
428 if (parse_commit(p
) < 0)
429 die("cannot simplify commit %s (invalid %s)",
430 sha1_to_hex(commit
->object
.sha1
),
431 sha1_to_hex(p
->object
.sha1
));
436 case REV_TREE_DIFFERENT
:
441 die("bad tree compare for commit %s", sha1_to_hex(commit
->object
.sha1
));
443 if (tree_changed
&& !tree_same
)
445 commit
->object
.flags
|= TREESAME
;
448 static void insert_by_date_cached(struct commit
*p
, struct commit_list
**head
,
449 struct commit_list
*cached_base
, struct commit_list
**cache
)
451 struct commit_list
*new_entry
;
453 if (cached_base
&& p
->date
< cached_base
->item
->date
)
454 new_entry
= insert_by_date(p
, &cached_base
->next
);
456 new_entry
= insert_by_date(p
, head
);
458 if (cache
&& (!*cache
|| p
->date
< (*cache
)->item
->date
))
462 static int add_parents_to_list(struct rev_info
*revs
, struct commit
*commit
,
463 struct commit_list
**list
, struct commit_list
**cache_ptr
)
465 struct commit_list
*parent
= commit
->parents
;
467 struct commit_list
*cached_base
= cache_ptr
? *cache_ptr
: NULL
;
469 if (commit
->object
.flags
& ADDED
)
471 commit
->object
.flags
|= ADDED
;
474 * If the commit is uninteresting, don't try to
475 * prune parents - we want the maximal uninteresting
478 * Normally we haven't parsed the parent
479 * yet, so we won't have a parent of a parent
480 * here. However, it may turn out that we've
481 * reached this commit some other way (where it
482 * wasn't uninteresting), in which case we need
483 * to mark its parents recursively too..
485 if (commit
->object
.flags
& UNINTERESTING
) {
487 struct commit
*p
= parent
->item
;
488 parent
= parent
->next
;
490 p
->object
.flags
|= UNINTERESTING
;
491 if (parse_commit(p
) < 0)
494 mark_parents_uninteresting(p
);
495 if (p
->object
.flags
& SEEN
)
497 p
->object
.flags
|= SEEN
;
498 insert_by_date_cached(p
, list
, cached_base
, cache_ptr
);
504 * Ok, the commit wasn't uninteresting. Try to
505 * simplify the commit history and find the parent
506 * that has no differences in the path set if one exists.
508 try_to_simplify_commit(revs
, commit
);
513 left_flag
= (commit
->object
.flags
& SYMMETRIC_LEFT
);
515 for (parent
= commit
->parents
; parent
; parent
= parent
->next
) {
516 struct commit
*p
= parent
->item
;
518 if (parse_commit(p
) < 0)
520 if (revs
->show_source
&& !p
->util
)
521 p
->util
= commit
->util
;
522 p
->object
.flags
|= left_flag
;
523 if (!(p
->object
.flags
& SEEN
)) {
524 p
->object
.flags
|= SEEN
;
525 insert_by_date_cached(p
, list
, cached_base
, cache_ptr
);
527 if (revs
->first_parent_only
)
533 static void cherry_pick_list(struct commit_list
*list
, struct rev_info
*revs
)
535 struct commit_list
*p
;
536 int left_count
= 0, right_count
= 0;
538 struct patch_ids ids
;
540 /* First count the commits on the left and on the right */
541 for (p
= list
; p
; p
= p
->next
) {
542 struct commit
*commit
= p
->item
;
543 unsigned flags
= commit
->object
.flags
;
544 if (flags
& BOUNDARY
)
546 else if (flags
& SYMMETRIC_LEFT
)
552 if (!left_count
|| !right_count
)
555 left_first
= left_count
< right_count
;
556 init_patch_ids(&ids
);
557 if (revs
->diffopt
.nr_paths
) {
558 ids
.diffopts
.nr_paths
= revs
->diffopt
.nr_paths
;
559 ids
.diffopts
.paths
= revs
->diffopt
.paths
;
560 ids
.diffopts
.pathlens
= revs
->diffopt
.pathlens
;
563 /* Compute patch-ids for one side */
564 for (p
= list
; p
; p
= p
->next
) {
565 struct commit
*commit
= p
->item
;
566 unsigned flags
= commit
->object
.flags
;
568 if (flags
& BOUNDARY
)
571 * If we have fewer left, left_first is set and we omit
572 * commits on the right branch in this loop. If we have
573 * fewer right, we skip the left ones.
575 if (left_first
!= !!(flags
& SYMMETRIC_LEFT
))
577 commit
->util
= add_commit_patch_id(commit
, &ids
);
580 /* Check the other side */
581 for (p
= list
; p
; p
= p
->next
) {
582 struct commit
*commit
= p
->item
;
584 unsigned flags
= commit
->object
.flags
;
586 if (flags
& BOUNDARY
)
589 * If we have fewer left, left_first is set and we omit
590 * commits on the left branch in this loop.
592 if (left_first
== !!(flags
& SYMMETRIC_LEFT
))
596 * Have we seen the same patch id?
598 id
= has_commit_patch_id(commit
, &ids
);
602 commit
->object
.flags
|= SHOWN
;
605 /* Now check the original side for seen ones */
606 for (p
= list
; p
; p
= p
->next
) {
607 struct commit
*commit
= p
->item
;
608 struct patch_id
*ent
;
614 commit
->object
.flags
|= SHOWN
;
618 free_patch_ids(&ids
);
621 /* How many extra uninteresting commits we want to see.. */
624 static int still_interesting(struct commit_list
*src
, unsigned long date
, int slop
)
627 * No source list at all? We're definitely done..
633 * Does the destination list contain entries with a date
634 * before the source list? Definitely _not_ done.
636 if (date
< src
->item
->date
)
640 * Does the source list still have interesting commits in
641 * it? Definitely not done..
643 if (!everybody_uninteresting(src
))
646 /* Ok, we're closing in.. */
651 * "rev-list --ancestry-path A..B" computes commits that are ancestors
652 * of B but not ancestors of A but further limits the result to those
653 * that are descendants of A. This takes the list of bottom commits and
654 * the result of "A..B" without --ancestry-path, and limits the latter
655 * further to the ones that can reach one of the commits in "bottom".
657 static void limit_to_ancestry(struct commit_list
*bottom
, struct commit_list
*list
)
659 struct commit_list
*p
;
660 struct commit_list
*rlist
= NULL
;
664 * Reverse the list so that it will be likely that we would
665 * process parents before children.
667 for (p
= list
; p
; p
= p
->next
)
668 commit_list_insert(p
->item
, &rlist
);
670 for (p
= bottom
; p
; p
= p
->next
)
671 p
->item
->object
.flags
|= TMP_MARK
;
674 * Mark the ones that can reach bottom commits in "list",
675 * in a bottom-up fashion.
679 for (p
= rlist
; p
; p
= p
->next
) {
680 struct commit
*c
= p
->item
;
681 struct commit_list
*parents
;
682 if (c
->object
.flags
& (TMP_MARK
| UNINTERESTING
))
684 for (parents
= c
->parents
;
686 parents
= parents
->next
) {
687 if (!(parents
->item
->object
.flags
& TMP_MARK
))
689 c
->object
.flags
|= TMP_MARK
;
694 } while (made_progress
);
697 * NEEDSWORK: decide if we want to remove parents that are
698 * not marked with TMP_MARK from commit->parents for commits
699 * in the resulting list. We may not want to do that, though.
703 * The ones that are not marked with TMP_MARK are uninteresting
705 for (p
= list
; p
; p
= p
->next
) {
706 struct commit
*c
= p
->item
;
707 if (c
->object
.flags
& TMP_MARK
)
709 c
->object
.flags
|= UNINTERESTING
;
712 /* We are done with the TMP_MARK */
713 for (p
= list
; p
; p
= p
->next
)
714 p
->item
->object
.flags
&= ~TMP_MARK
;
715 for (p
= bottom
; p
; p
= p
->next
)
716 p
->item
->object
.flags
&= ~TMP_MARK
;
717 free_commit_list(rlist
);
721 * Before walking the history, keep the set of "negative" refs the
722 * caller has asked to exclude.
724 * This is used to compute "rev-list --ancestry-path A..B", as we need
725 * to filter the result of "A..B" further to the ones that can actually
728 static struct commit_list
*collect_bottom_commits(struct commit_list
*list
)
730 struct commit_list
*elem
, *bottom
= NULL
;
731 for (elem
= list
; elem
; elem
= elem
->next
)
732 if (elem
->item
->object
.flags
& UNINTERESTING
)
733 commit_list_insert(elem
->item
, &bottom
);
737 static int limit_list(struct rev_info
*revs
)
740 unsigned long date
= ~0ul;
741 struct commit_list
*list
= revs
->commits
;
742 struct commit_list
*newlist
= NULL
;
743 struct commit_list
**p
= &newlist
;
744 struct commit_list
*bottom
= NULL
;
746 if (revs
->ancestry_path
) {
747 bottom
= collect_bottom_commits(list
);
749 die("--ancestry-path given but there are no bottom commits");
753 struct commit_list
*entry
= list
;
754 struct commit
*commit
= list
->item
;
755 struct object
*obj
= &commit
->object
;
756 show_early_output_fn_t show
;
761 if (revs
->max_age
!= -1 && (commit
->date
< revs
->max_age
))
762 obj
->flags
|= UNINTERESTING
;
763 if (add_parents_to_list(revs
, commit
, &list
, NULL
) < 0)
765 if (obj
->flags
& UNINTERESTING
) {
766 mark_parents_uninteresting(commit
);
768 p
= &commit_list_insert(commit
, p
)->next
;
769 slop
= still_interesting(list
, date
, slop
);
772 /* If showing all, add the whole pending list to the end */
777 if (revs
->min_age
!= -1 && (commit
->date
> revs
->min_age
))
780 p
= &commit_list_insert(commit
, p
)->next
;
782 show
= show_early_output
;
787 show_early_output
= NULL
;
789 if (revs
->cherry_pick
)
790 cherry_pick_list(newlist
, revs
);
793 limit_to_ancestry(bottom
, newlist
);
794 free_commit_list(bottom
);
797 revs
->commits
= newlist
;
803 int warned_bad_reflog
;
804 struct rev_info
*all_revs
;
805 const char *name_for_errormsg
;
808 static int handle_one_ref(const char *path
, const unsigned char *sha1
, int flag
, void *cb_data
)
810 struct all_refs_cb
*cb
= cb_data
;
811 struct object
*object
= get_reference(cb
->all_revs
, path
, sha1
,
813 add_pending_object(cb
->all_revs
, object
, path
);
817 static void init_all_refs_cb(struct all_refs_cb
*cb
, struct rev_info
*revs
,
821 cb
->all_flags
= flags
;
824 static void handle_refs(const char *submodule
, struct rev_info
*revs
, unsigned flags
,
825 int (*for_each
)(const char *, each_ref_fn
, void *))
827 struct all_refs_cb cb
;
828 init_all_refs_cb(&cb
, revs
, flags
);
829 for_each(submodule
, handle_one_ref
, &cb
);
832 static void handle_one_reflog_commit(unsigned char *sha1
, void *cb_data
)
834 struct all_refs_cb
*cb
= cb_data
;
835 if (!is_null_sha1(sha1
)) {
836 struct object
*o
= parse_object(sha1
);
838 o
->flags
|= cb
->all_flags
;
839 add_pending_object(cb
->all_revs
, o
, "");
841 else if (!cb
->warned_bad_reflog
) {
842 warning("reflog of '%s' references pruned commits",
843 cb
->name_for_errormsg
);
844 cb
->warned_bad_reflog
= 1;
849 static int handle_one_reflog_ent(unsigned char *osha1
, unsigned char *nsha1
,
850 const char *email
, unsigned long timestamp
, int tz
,
851 const char *message
, void *cb_data
)
853 handle_one_reflog_commit(osha1
, cb_data
);
854 handle_one_reflog_commit(nsha1
, cb_data
);
858 static int handle_one_reflog(const char *path
, const unsigned char *sha1
, int flag
, void *cb_data
)
860 struct all_refs_cb
*cb
= cb_data
;
861 cb
->warned_bad_reflog
= 0;
862 cb
->name_for_errormsg
= path
;
863 for_each_reflog_ent(path
, handle_one_reflog_ent
, cb_data
);
867 static void handle_reflog(struct rev_info
*revs
, unsigned flags
)
869 struct all_refs_cb cb
;
871 cb
.all_flags
= flags
;
872 for_each_reflog(handle_one_reflog
, &cb
);
875 static int add_parents_only(struct rev_info
*revs
, const char *arg
, int flags
)
877 unsigned char sha1
[20];
879 struct commit
*commit
;
880 struct commit_list
*parents
;
883 flags
^= UNINTERESTING
;
886 if (get_sha1(arg
, sha1
))
889 it
= get_reference(revs
, arg
, sha1
, 0);
890 if (it
->type
!= OBJ_TAG
)
892 if (!((struct tag
*)it
)->tagged
)
894 hashcpy(sha1
, ((struct tag
*)it
)->tagged
->sha1
);
896 if (it
->type
!= OBJ_COMMIT
)
898 commit
= (struct commit
*)it
;
899 for (parents
= commit
->parents
; parents
; parents
= parents
->next
) {
900 it
= &parents
->item
->object
;
902 add_pending_object(revs
, it
, arg
);
907 void init_revisions(struct rev_info
*revs
, const char *prefix
)
909 memset(revs
, 0, sizeof(*revs
));
911 revs
->abbrev
= DEFAULT_ABBREV
;
912 revs
->ignore_merges
= 1;
913 revs
->simplify_history
= 1;
914 DIFF_OPT_SET(&revs
->pruning
, RECURSIVE
);
915 DIFF_OPT_SET(&revs
->pruning
, QUICK
);
916 revs
->pruning
.add_remove
= file_add_remove
;
917 revs
->pruning
.change
= file_change
;
920 revs
->prefix
= prefix
;
923 revs
->skip_count
= -1;
924 revs
->max_count
= -1;
926 revs
->commit_format
= CMIT_FMT_DEFAULT
;
928 revs
->grep_filter
.status_only
= 1;
929 revs
->grep_filter
.pattern_tail
= &(revs
->grep_filter
.pattern_list
);
930 revs
->grep_filter
.header_tail
= &(revs
->grep_filter
.header_list
);
931 revs
->grep_filter
.regflags
= REG_NEWLINE
;
933 diff_setup(&revs
->diffopt
);
934 if (prefix
&& !revs
->diffopt
.prefix
) {
935 revs
->diffopt
.prefix
= prefix
;
936 revs
->diffopt
.prefix_length
= strlen(prefix
);
940 static void add_pending_commit_list(struct rev_info
*revs
,
941 struct commit_list
*commit_list
,
944 while (commit_list
) {
945 struct object
*object
= &commit_list
->item
->object
;
946 object
->flags
|= flags
;
947 add_pending_object(revs
, object
, sha1_to_hex(object
->sha1
));
948 commit_list
= commit_list
->next
;
952 static void prepare_show_merge(struct rev_info
*revs
)
954 struct commit_list
*bases
;
955 struct commit
*head
, *other
;
956 unsigned char sha1
[20];
957 const char **prune
= NULL
;
958 int i
, prune_num
= 1; /* counting terminating NULL */
960 if (get_sha1("HEAD", sha1
) || !(head
= lookup_commit(sha1
)))
961 die("--merge without HEAD?");
962 if (get_sha1("MERGE_HEAD", sha1
) || !(other
= lookup_commit(sha1
)))
963 die("--merge without MERGE_HEAD?");
964 add_pending_object(revs
, &head
->object
, "HEAD");
965 add_pending_object(revs
, &other
->object
, "MERGE_HEAD");
966 bases
= get_merge_bases(head
, other
, 1);
967 add_pending_commit_list(revs
, bases
, UNINTERESTING
);
968 free_commit_list(bases
);
969 head
->object
.flags
|= SYMMETRIC_LEFT
;
973 for (i
= 0; i
< active_nr
; i
++) {
974 struct cache_entry
*ce
= active_cache
[i
];
977 if (ce_path_match(ce
, revs
->prune_data
)) {
979 prune
= xrealloc(prune
, sizeof(*prune
) * prune_num
);
980 prune
[prune_num
-2] = ce
->name
;
981 prune
[prune_num
-1] = NULL
;
983 while ((i
+1 < active_nr
) &&
984 ce_same_name(ce
, active_cache
[i
+1]))
987 revs
->prune_data
= prune
;
991 int handle_revision_arg(const char *arg
, struct rev_info
*revs
,
993 int cant_be_filename
)
997 struct object
*object
;
998 unsigned char sha1
[20];
1001 dotdot
= strstr(arg
, "..");
1003 unsigned char from_sha1
[20];
1004 const char *next
= dotdot
+ 2;
1005 const char *this = arg
;
1006 int symmetric
= *next
== '.';
1007 unsigned int flags_exclude
= flags
^ UNINTERESTING
;
1016 if (!get_sha1(this, from_sha1
) &&
1017 !get_sha1(next
, sha1
)) {
1018 struct commit
*a
, *b
;
1019 struct commit_list
*exclude
;
1021 a
= lookup_commit_reference(from_sha1
);
1022 b
= lookup_commit_reference(sha1
);
1025 "Invalid symmetric difference expression %s...%s" :
1026 "Invalid revision range %s..%s",
1030 if (!cant_be_filename
) {
1032 verify_non_filename(revs
->prefix
, arg
);
1036 exclude
= get_merge_bases(a
, b
, 1);
1037 add_pending_commit_list(revs
, exclude
,
1039 free_commit_list(exclude
);
1040 a
->object
.flags
|= flags
| SYMMETRIC_LEFT
;
1042 a
->object
.flags
|= flags_exclude
;
1043 b
->object
.flags
|= flags
;
1044 add_pending_object(revs
, &a
->object
, this);
1045 add_pending_object(revs
, &b
->object
, next
);
1050 dotdot
= strstr(arg
, "^@");
1051 if (dotdot
&& !dotdot
[2]) {
1053 if (add_parents_only(revs
, arg
, flags
))
1057 dotdot
= strstr(arg
, "^!");
1058 if (dotdot
&& !dotdot
[2]) {
1060 if (!add_parents_only(revs
, arg
, flags
^ UNINTERESTING
))
1066 local_flags
= UNINTERESTING
;
1069 if (get_sha1_with_mode(arg
, sha1
, &mode
))
1071 if (!cant_be_filename
)
1072 verify_non_filename(revs
->prefix
, arg
);
1073 object
= get_reference(revs
, arg
, sha1
, flags
^ local_flags
);
1074 add_pending_object_with_mode(revs
, object
, arg
, mode
);
1078 static void read_pathspec_from_stdin(struct rev_info
*revs
, struct strbuf
*sb
, const char ***prune_data
)
1080 const char **prune
= *prune_data
;
1084 /* count existing ones */
1088 for (prune_nr
= 0; prune
[prune_nr
]; prune_nr
++)
1090 prune_alloc
= prune_nr
; /* not really, but we do not know */
1092 while (strbuf_getwholeline(sb
, stdin
, '\n') != EOF
) {
1094 if (len
&& sb
->buf
[len
- 1] == '\n')
1095 sb
->buf
[--len
] = '\0';
1096 ALLOC_GROW(prune
, prune_nr
+1, prune_alloc
);
1097 prune
[prune_nr
++] = xstrdup(sb
->buf
);
1100 ALLOC_GROW(prune
, prune_nr
+1, prune_alloc
);
1101 prune
[prune_nr
] = NULL
;
1103 *prune_data
= prune
;
1106 static void read_revisions_from_stdin(struct rev_info
*revs
, const char ***prune
)
1109 int seen_dashdash
= 0;
1111 strbuf_init(&sb
, 1000);
1112 while (strbuf_getwholeline(&sb
, stdin
, '\n') != EOF
) {
1114 if (len
&& sb
.buf
[len
- 1] == '\n')
1115 sb
.buf
[--len
] = '\0';
1118 if (sb
.buf
[0] == '-') {
1119 if (len
== 2 && sb
.buf
[1] == '-') {
1123 die("options not supported in --stdin mode");
1125 if (handle_revision_arg(sb
.buf
, revs
, 0, 1))
1126 die("bad revision '%s'", sb
.buf
);
1129 read_pathspec_from_stdin(revs
, &sb
, prune
);
1130 strbuf_release(&sb
);
1133 static void add_grep(struct rev_info
*revs
, const char *ptn
, enum grep_pat_token what
)
1135 append_grep_pattern(&revs
->grep_filter
, ptn
, "command line", 0, what
);
1138 static void add_header_grep(struct rev_info
*revs
, enum grep_header_field field
, const char *pattern
)
1140 append_header_grep_pattern(&revs
->grep_filter
, field
, pattern
);
1143 static void add_message_grep(struct rev_info
*revs
, const char *pattern
)
1145 add_grep(revs
, pattern
, GREP_PATTERN_BODY
);
1148 static int handle_revision_opt(struct rev_info
*revs
, int argc
, const char **argv
,
1149 int *unkc
, const char **unkv
)
1151 const char *arg
= argv
[0];
1155 /* pseudo revision arguments */
1156 if (!strcmp(arg
, "--all") || !strcmp(arg
, "--branches") ||
1157 !strcmp(arg
, "--tags") || !strcmp(arg
, "--remotes") ||
1158 !strcmp(arg
, "--reflog") || !strcmp(arg
, "--not") ||
1159 !strcmp(arg
, "--no-walk") || !strcmp(arg
, "--do-walk") ||
1160 !strcmp(arg
, "--bisect"))
1162 unkv
[(*unkc
)++] = arg
;
1166 if ((argcount
= parse_long_opt("max-count", argv
, &optarg
))) {
1167 revs
->max_count
= atoi(optarg
);
1170 } else if ((argcount
= parse_long_opt("skip", argv
, &optarg
))) {
1171 revs
->skip_count
= atoi(optarg
);
1173 } else if ((*arg
== '-') && isdigit(arg
[1])) {
1174 /* accept -<digit>, like traditional "head" */
1175 revs
->max_count
= atoi(arg
+ 1);
1177 } else if (!strcmp(arg
, "-n")) {
1179 return error("-n requires an argument");
1180 revs
->max_count
= atoi(argv
[1]);
1183 } else if (!prefixcmp(arg
, "-n")) {
1184 revs
->max_count
= atoi(arg
+ 2);
1186 } else if ((argcount
= parse_long_opt("max-age", argv
, &optarg
))) {
1187 revs
->max_age
= atoi(optarg
);
1189 } else if ((argcount
= parse_long_opt("since", argv
, &optarg
))) {
1190 revs
->max_age
= approxidate(optarg
);
1192 } else if ((argcount
= parse_long_opt("after", argv
, &optarg
))) {
1193 revs
->max_age
= approxidate(optarg
);
1195 } else if ((argcount
= parse_long_opt("min-age", argv
, &optarg
))) {
1196 revs
->min_age
= atoi(optarg
);
1198 } else if ((argcount
= parse_long_opt("before", argv
, &optarg
))) {
1199 revs
->min_age
= approxidate(optarg
);
1201 } else if ((argcount
= parse_long_opt("until", argv
, &optarg
))) {
1202 revs
->min_age
= approxidate(optarg
);
1204 } else if (!strcmp(arg
, "--first-parent")) {
1205 revs
->first_parent_only
= 1;
1206 } else if (!strcmp(arg
, "--ancestry-path")) {
1207 revs
->ancestry_path
= 1;
1208 revs
->simplify_history
= 0;
1210 } else if (!strcmp(arg
, "-g") || !strcmp(arg
, "--walk-reflogs")) {
1211 init_reflog_walk(&revs
->reflog_info
);
1212 } else if (!strcmp(arg
, "--default")) {
1214 return error("bad --default argument");
1215 revs
->def
= argv
[1];
1217 } else if (!strcmp(arg
, "--merge")) {
1218 revs
->show_merge
= 1;
1219 } else if (!strcmp(arg
, "--topo-order")) {
1221 revs
->topo_order
= 1;
1222 } else if (!strcmp(arg
, "--simplify-merges")) {
1223 revs
->simplify_merges
= 1;
1224 revs
->rewrite_parents
= 1;
1225 revs
->simplify_history
= 0;
1227 } else if (!strcmp(arg
, "--simplify-by-decoration")) {
1228 revs
->simplify_merges
= 1;
1229 revs
->rewrite_parents
= 1;
1230 revs
->simplify_history
= 0;
1231 revs
->simplify_by_decoration
= 1;
1234 load_ref_decorations(DECORATE_SHORT_REFS
);
1235 } else if (!strcmp(arg
, "--date-order")) {
1237 revs
->topo_order
= 1;
1238 } else if (!prefixcmp(arg
, "--early-output")) {
1242 count
= atoi(arg
+15);
1245 revs
->topo_order
= 1;
1246 revs
->early_output
= count
;
1248 } else if (!strcmp(arg
, "--parents")) {
1249 revs
->rewrite_parents
= 1;
1250 revs
->print_parents
= 1;
1251 } else if (!strcmp(arg
, "--dense")) {
1253 } else if (!strcmp(arg
, "--sparse")) {
1255 } else if (!strcmp(arg
, "--show-all")) {
1257 } else if (!strcmp(arg
, "--remove-empty")) {
1258 revs
->remove_empty_trees
= 1;
1259 } else if (!strcmp(arg
, "--merges")) {
1260 revs
->merges_only
= 1;
1261 } else if (!strcmp(arg
, "--no-merges")) {
1262 revs
->no_merges
= 1;
1263 } else if (!strcmp(arg
, "--boundary")) {
1265 } else if (!strcmp(arg
, "--left-right")) {
1266 revs
->left_right
= 1;
1267 } else if (!strcmp(arg
, "--count")) {
1269 } else if (!strcmp(arg
, "--cherry-pick")) {
1270 revs
->cherry_pick
= 1;
1272 } else if (!strcmp(arg
, "--objects")) {
1273 revs
->tag_objects
= 1;
1274 revs
->tree_objects
= 1;
1275 revs
->blob_objects
= 1;
1276 } else if (!strcmp(arg
, "--objects-edge")) {
1277 revs
->tag_objects
= 1;
1278 revs
->tree_objects
= 1;
1279 revs
->blob_objects
= 1;
1280 revs
->edge_hint
= 1;
1281 } else if (!strcmp(arg
, "--unpacked")) {
1283 } else if (!prefixcmp(arg
, "--unpacked=")) {
1284 die("--unpacked=<packfile> no longer supported.");
1285 } else if (!strcmp(arg
, "-r")) {
1287 DIFF_OPT_SET(&revs
->diffopt
, RECURSIVE
);
1288 } else if (!strcmp(arg
, "-t")) {
1290 DIFF_OPT_SET(&revs
->diffopt
, RECURSIVE
);
1291 DIFF_OPT_SET(&revs
->diffopt
, TREE_IN_RECURSIVE
);
1292 } else if (!strcmp(arg
, "-m")) {
1293 revs
->ignore_merges
= 0;
1294 } else if (!strcmp(arg
, "-c")) {
1296 revs
->dense_combined_merges
= 0;
1297 revs
->combine_merges
= 1;
1298 } else if (!strcmp(arg
, "--cc")) {
1300 revs
->dense_combined_merges
= 1;
1301 revs
->combine_merges
= 1;
1302 } else if (!strcmp(arg
, "-v")) {
1303 revs
->verbose_header
= 1;
1304 } else if (!strcmp(arg
, "--pretty")) {
1305 revs
->verbose_header
= 1;
1306 revs
->pretty_given
= 1;
1307 get_commit_format(arg
+8, revs
);
1308 } else if (!prefixcmp(arg
, "--pretty=") || !prefixcmp(arg
, "--format=")) {
1310 * Detached form ("--pretty X" as opposed to "--pretty=X")
1311 * not allowed, since the argument is optional.
1313 revs
->verbose_header
= 1;
1314 revs
->pretty_given
= 1;
1315 get_commit_format(arg
+9, revs
);
1316 } else if (!strcmp(arg
, "--show-notes")) {
1317 revs
->show_notes
= 1;
1318 revs
->show_notes_given
= 1;
1319 } else if (!prefixcmp(arg
, "--show-notes=")) {
1320 struct strbuf buf
= STRBUF_INIT
;
1321 revs
->show_notes
= 1;
1322 revs
->show_notes_given
= 1;
1323 if (!revs
->notes_opt
.extra_notes_refs
)
1324 revs
->notes_opt
.extra_notes_refs
= xcalloc(1, sizeof(struct string_list
));
1325 if (!prefixcmp(arg
+13, "refs/"))
1327 else if (!prefixcmp(arg
+13, "notes/"))
1328 strbuf_addstr(&buf
, "refs/");
1330 strbuf_addstr(&buf
, "refs/notes/");
1331 strbuf_addstr(&buf
, arg
+13);
1332 string_list_append(revs
->notes_opt
.extra_notes_refs
,
1333 strbuf_detach(&buf
, NULL
));
1334 } else if (!strcmp(arg
, "--no-notes")) {
1335 revs
->show_notes
= 0;
1336 revs
->show_notes_given
= 1;
1337 } else if (!strcmp(arg
, "--standard-notes")) {
1338 revs
->show_notes_given
= 1;
1339 revs
->notes_opt
.suppress_default_notes
= 0;
1340 } else if (!strcmp(arg
, "--no-standard-notes")) {
1341 revs
->notes_opt
.suppress_default_notes
= 1;
1342 } else if (!strcmp(arg
, "--oneline")) {
1343 revs
->verbose_header
= 1;
1344 get_commit_format("oneline", revs
);
1345 revs
->pretty_given
= 1;
1346 revs
->abbrev_commit
= 1;
1347 } else if (!strcmp(arg
, "--graph")) {
1348 revs
->topo_order
= 1;
1349 revs
->rewrite_parents
= 1;
1350 revs
->graph
= graph_init(revs
);
1351 } else if (!strcmp(arg
, "--root")) {
1352 revs
->show_root_diff
= 1;
1353 } else if (!strcmp(arg
, "--no-commit-id")) {
1354 revs
->no_commit_id
= 1;
1355 } else if (!strcmp(arg
, "--always")) {
1356 revs
->always_show_header
= 1;
1357 } else if (!strcmp(arg
, "--no-abbrev")) {
1359 } else if (!strcmp(arg
, "--abbrev")) {
1360 revs
->abbrev
= DEFAULT_ABBREV
;
1361 } else if (!prefixcmp(arg
, "--abbrev=")) {
1362 revs
->abbrev
= strtoul(arg
+ 9, NULL
, 10);
1363 if (revs
->abbrev
< MINIMUM_ABBREV
)
1364 revs
->abbrev
= MINIMUM_ABBREV
;
1365 else if (revs
->abbrev
> 40)
1367 } else if (!strcmp(arg
, "--abbrev-commit")) {
1368 revs
->abbrev_commit
= 1;
1369 } else if (!strcmp(arg
, "--full-diff")) {
1371 revs
->full_diff
= 1;
1372 } else if (!strcmp(arg
, "--full-history")) {
1373 revs
->simplify_history
= 0;
1374 } else if (!strcmp(arg
, "--relative-date")) {
1375 revs
->date_mode
= DATE_RELATIVE
;
1376 revs
->date_mode_explicit
= 1;
1377 } else if ((argcount
= parse_long_opt("date", argv
, &optarg
))) {
1378 revs
->date_mode
= parse_date_format(optarg
);
1379 revs
->date_mode_explicit
= 1;
1381 } else if (!strcmp(arg
, "--log-size")) {
1382 revs
->show_log_size
= 1;
1385 * Grepping the commit log
1387 else if ((argcount
= parse_long_opt("author", argv
, &optarg
))) {
1388 add_header_grep(revs
, GREP_HEADER_AUTHOR
, optarg
);
1390 } else if ((argcount
= parse_long_opt("committer", argv
, &optarg
))) {
1391 add_header_grep(revs
, GREP_HEADER_COMMITTER
, optarg
);
1393 } else if ((argcount
= parse_long_opt("grep", argv
, &optarg
))) {
1394 add_message_grep(revs
, optarg
);
1396 } else if (!strcmp(arg
, "--extended-regexp") || !strcmp(arg
, "-E")) {
1397 revs
->grep_filter
.regflags
|= REG_EXTENDED
;
1398 } else if (!strcmp(arg
, "--regexp-ignore-case") || !strcmp(arg
, "-i")) {
1399 revs
->grep_filter
.regflags
|= REG_ICASE
;
1400 } else if (!strcmp(arg
, "--fixed-strings") || !strcmp(arg
, "-F")) {
1401 revs
->grep_filter
.fixed
= 1;
1402 } else if (!strcmp(arg
, "--all-match")) {
1403 revs
->grep_filter
.all_match
= 1;
1404 } else if ((argcount
= parse_long_opt("encoding", argv
, &optarg
))) {
1405 if (strcmp(optarg
, "none"))
1406 git_log_output_encoding
= xstrdup(optarg
);
1408 git_log_output_encoding
= "";
1410 } else if (!strcmp(arg
, "--reverse")) {
1412 } else if (!strcmp(arg
, "--children")) {
1413 revs
->children
.name
= "children";
1416 int opts
= diff_opt_parse(&revs
->diffopt
, argv
, argc
);
1418 unkv
[(*unkc
)++] = arg
;
1425 void parse_revision_opt(struct rev_info
*revs
, struct parse_opt_ctx_t
*ctx
,
1426 const struct option
*options
,
1427 const char * const usagestr
[])
1429 int n
= handle_revision_opt(revs
, ctx
->argc
, ctx
->argv
,
1430 &ctx
->cpidx
, ctx
->out
);
1432 error("unknown option `%s'", ctx
->argv
[0]);
1433 usage_with_options(usagestr
, options
);
1439 static int for_each_bad_bisect_ref(const char *submodule
, each_ref_fn fn
, void *cb_data
)
1441 return for_each_ref_in_submodule(submodule
, "refs/bisect/bad", fn
, cb_data
);
1444 static int for_each_good_bisect_ref(const char *submodule
, each_ref_fn fn
, void *cb_data
)
1446 return for_each_ref_in_submodule(submodule
, "refs/bisect/good", fn
, cb_data
);
1449 static void append_prune_data(const char ***prune_data
, const char **av
)
1451 const char **prune
= *prune_data
;
1460 /* count existing ones */
1461 for (prune_nr
= 0; prune
[prune_nr
]; prune_nr
++)
1463 prune_alloc
= prune_nr
; /* not really, but we do not know */
1466 ALLOC_GROW(prune
, prune_nr
+1, prune_alloc
);
1467 prune
[prune_nr
++] = *av
;
1471 ALLOC_GROW(prune
, prune_nr
+1, prune_alloc
);
1472 prune
[prune_nr
] = NULL
;
1474 *prune_data
= prune
;
1478 * Parse revision information, filling in the "rev_info" structure,
1479 * and removing the used arguments from the argument list.
1481 * Returns the number of arguments left that weren't recognized
1482 * (which are also moved to the head of the argument list)
1484 int setup_revisions(int argc
, const char **argv
, struct rev_info
*revs
, struct setup_revision_opt
*opt
)
1486 int i
, flags
, left
, seen_dashdash
, read_from_stdin
, got_rev_arg
= 0;
1487 const char **prune_data
= NULL
;
1488 const char *submodule
= NULL
;
1493 submodule
= opt
->submodule
;
1495 /* First, search for "--" */
1497 for (i
= 1; i
< argc
; i
++) {
1498 const char *arg
= argv
[i
];
1499 if (strcmp(arg
, "--"))
1504 prune_data
= argv
+ i
+ 1;
1509 /* Second, deal with arguments and options */
1511 read_from_stdin
= 0;
1512 for (left
= i
= 1; i
< argc
; i
++) {
1513 const char *arg
= argv
[i
];
1517 if (!strcmp(arg
, "--all")) {
1518 handle_refs(submodule
, revs
, flags
, for_each_ref_submodule
);
1519 handle_refs(submodule
, revs
, flags
, head_ref_submodule
);
1522 if (!strcmp(arg
, "--branches")) {
1523 handle_refs(submodule
, revs
, flags
, for_each_branch_ref_submodule
);
1526 if (!strcmp(arg
, "--bisect")) {
1527 handle_refs(submodule
, revs
, flags
, for_each_bad_bisect_ref
);
1528 handle_refs(submodule
, revs
, flags
^ UNINTERESTING
, for_each_good_bisect_ref
);
1532 if (!strcmp(arg
, "--tags")) {
1533 handle_refs(submodule
, revs
, flags
, for_each_tag_ref_submodule
);
1536 if (!strcmp(arg
, "--remotes")) {
1537 handle_refs(submodule
, revs
, flags
, for_each_remote_ref_submodule
);
1540 if ((argcount
= parse_long_opt("glob", argv
+ i
, &optarg
))) {
1541 struct all_refs_cb cb
;
1543 init_all_refs_cb(&cb
, revs
, flags
);
1544 for_each_glob_ref(handle_one_ref
, optarg
, &cb
);
1547 if (!prefixcmp(arg
, "--branches=")) {
1548 struct all_refs_cb cb
;
1549 init_all_refs_cb(&cb
, revs
, flags
);
1550 for_each_glob_ref_in(handle_one_ref
, arg
+ 11, "refs/heads/", &cb
);
1553 if (!prefixcmp(arg
, "--tags=")) {
1554 struct all_refs_cb cb
;
1555 init_all_refs_cb(&cb
, revs
, flags
);
1556 for_each_glob_ref_in(handle_one_ref
, arg
+ 7, "refs/tags/", &cb
);
1559 if (!prefixcmp(arg
, "--remotes=")) {
1560 struct all_refs_cb cb
;
1561 init_all_refs_cb(&cb
, revs
, flags
);
1562 for_each_glob_ref_in(handle_one_ref
, arg
+ 10, "refs/remotes/", &cb
);
1565 if (!strcmp(arg
, "--reflog")) {
1566 handle_reflog(revs
, flags
);
1569 if (!strcmp(arg
, "--not")) {
1570 flags
^= UNINTERESTING
;
1573 if (!strcmp(arg
, "--no-walk")) {
1577 if (!strcmp(arg
, "--do-walk")) {
1581 if (!strcmp(arg
, "--stdin")) {
1582 if (revs
->disable_stdin
) {
1586 if (read_from_stdin
++)
1587 die("--stdin given twice?");
1588 read_revisions_from_stdin(revs
, &prune_data
);
1592 opts
= handle_revision_opt(revs
, argc
- i
, argv
+ i
, &left
, argv
);
1602 if (handle_revision_arg(arg
, revs
, flags
, seen_dashdash
)) {
1604 if (seen_dashdash
|| *arg
== '^')
1605 die("bad revision '%s'", arg
);
1607 /* If we didn't have a "--":
1608 * (1) all filenames must exist;
1609 * (2) all rev-args must not be interpretable
1610 * as a valid filename.
1611 * but the latter we have checked in the main loop.
1613 for (j
= i
; j
< argc
; j
++)
1614 verify_filename(revs
->prefix
, argv
[j
]);
1616 append_prune_data(&prune_data
, argv
+ i
);
1624 revs
->prune_data
= get_pathspec(revs
->prefix
, prune_data
);
1626 if (revs
->def
== NULL
)
1627 revs
->def
= opt
? opt
->def
: NULL
;
1628 if (opt
&& opt
->tweak
)
1629 opt
->tweak(revs
, opt
);
1630 if (revs
->show_merge
)
1631 prepare_show_merge(revs
);
1632 if (revs
->def
&& !revs
->pending
.nr
&& !got_rev_arg
) {
1633 unsigned char sha1
[20];
1634 struct object
*object
;
1636 if (get_sha1_with_mode(revs
->def
, sha1
, &mode
))
1637 die("bad default revision '%s'", revs
->def
);
1638 object
= get_reference(revs
, revs
->def
, sha1
, 0);
1639 add_pending_object_with_mode(revs
, object
, revs
->def
, mode
);
1642 /* Did the user ask for any diff output? Run the diff! */
1643 if (revs
->diffopt
.output_format
& ~DIFF_FORMAT_NO_OUTPUT
)
1646 /* Pickaxe, diff-filter and rename following need diffs */
1647 if (revs
->diffopt
.pickaxe
||
1648 revs
->diffopt
.filter
||
1649 DIFF_OPT_TST(&revs
->diffopt
, FOLLOW_RENAMES
))
1652 if (revs
->topo_order
)
1655 if (revs
->prune_data
) {
1656 diff_tree_setup_paths(revs
->prune_data
, &revs
->pruning
);
1657 /* Can't prune commits with rename following: the paths change.. */
1658 if (!DIFF_OPT_TST(&revs
->diffopt
, FOLLOW_RENAMES
))
1660 if (!revs
->full_diff
)
1661 diff_tree_setup_paths(revs
->prune_data
, &revs
->diffopt
);
1663 if (revs
->combine_merges
)
1664 revs
->ignore_merges
= 0;
1665 revs
->diffopt
.abbrev
= revs
->abbrev
;
1667 if (revs
->line_level_traverse
) {
1669 revs
->topo_order
= 1;
1672 if (diff_setup_done(&revs
->diffopt
) < 0)
1673 die("diff_setup_done failed");
1675 compile_grep_patterns(&revs
->grep_filter
);
1677 if (revs
->reverse
&& revs
->reflog_info
)
1678 die("cannot combine --reverse with --walk-reflogs");
1679 if (revs
->rewrite_parents
&& revs
->children
.name
)
1680 die("cannot combine --parents and --children");
1683 * Limitations on the graph functionality
1685 if (revs
->reverse
&& revs
->graph
)
1686 die("cannot combine --reverse with --graph");
1688 if (revs
->reflog_info
&& revs
->graph
)
1689 die("cannot combine --walk-reflogs with --graph");
1694 static void add_child(struct rev_info
*revs
, struct commit
*parent
, struct commit
*child
)
1696 struct commit_list
*l
= xcalloc(1, sizeof(*l
));
1699 l
->next
= add_decoration(&revs
->children
, &parent
->object
, l
);
1702 static int remove_duplicate_parents(struct commit
*commit
)
1704 struct commit_list
**pp
, *p
;
1705 int surviving_parents
;
1707 /* Examine existing parents while marking ones we have seen... */
1708 pp
= &commit
->parents
;
1709 while ((p
= *pp
) != NULL
) {
1710 struct commit
*parent
= p
->item
;
1711 if (parent
->object
.flags
& TMP_MARK
) {
1715 parent
->object
.flags
|= TMP_MARK
;
1718 /* count them while clearing the temporary mark */
1719 surviving_parents
= 0;
1720 for (p
= commit
->parents
; p
; p
= p
->next
) {
1721 p
->item
->object
.flags
&= ~TMP_MARK
;
1722 surviving_parents
++;
1724 return surviving_parents
;
1727 struct merge_simplify_state
{
1728 struct commit
*simplified
;
1731 static struct merge_simplify_state
*locate_simplify_state(struct rev_info
*revs
, struct commit
*commit
)
1733 struct merge_simplify_state
*st
;
1735 st
= lookup_decoration(&revs
->merge_simplification
, &commit
->object
);
1737 st
= xcalloc(1, sizeof(*st
));
1738 add_decoration(&revs
->merge_simplification
, &commit
->object
, st
);
1743 static struct commit_list
**simplify_one(struct rev_info
*revs
, struct commit
*commit
, struct commit_list
**tail
)
1745 struct commit_list
*p
;
1746 struct merge_simplify_state
*st
, *pst
;
1749 st
= locate_simplify_state(revs
, commit
);
1752 * Have we handled this one?
1758 * An UNINTERESTING commit simplifies to itself, so does a
1759 * root commit. We do not rewrite parents of such commit
1762 if ((commit
->object
.flags
& UNINTERESTING
) || !commit
->parents
) {
1763 st
->simplified
= commit
;
1768 * Do we know what commit all of our parents should be rewritten to?
1769 * Otherwise we are not ready to rewrite this one yet.
1771 for (cnt
= 0, p
= commit
->parents
; p
; p
= p
->next
) {
1772 pst
= locate_simplify_state(revs
, p
->item
);
1773 if (!pst
->simplified
) {
1774 tail
= &commit_list_insert(p
->item
, tail
)->next
;
1779 tail
= &commit_list_insert(commit
, tail
)->next
;
1784 * Rewrite our list of parents.
1786 for (p
= commit
->parents
; p
; p
= p
->next
) {
1787 pst
= locate_simplify_state(revs
, p
->item
);
1788 p
->item
= pst
->simplified
;
1790 cnt
= remove_duplicate_parents(commit
);
1793 * It is possible that we are a merge and one side branch
1794 * does not have any commit that touches the given paths;
1795 * in such a case, the immediate parents will be rewritten
1796 * to different commits.
1798 * o----X X: the commit we are looking at;
1799 * / / o: a commit that touches the paths;
1802 * Further reduce the parents by removing redundant parents.
1805 struct commit_list
*h
= reduce_heads(commit
->parents
);
1806 cnt
= commit_list_count(h
);
1807 free_commit_list(commit
->parents
);
1808 commit
->parents
= h
;
1812 * A commit simplifies to itself if it is a root, if it is
1813 * UNINTERESTING, if it touches the given paths, or if it is a
1814 * merge and its parents simplifies to more than one commits
1815 * (the first two cases are already handled at the beginning of
1818 * Otherwise, it simplifies to what its sole parent simplifies to.
1821 (commit
->object
.flags
& UNINTERESTING
) ||
1822 !(commit
->object
.flags
& TREESAME
) ||
1824 st
->simplified
= commit
;
1826 pst
= locate_simplify_state(revs
, commit
->parents
->item
);
1827 st
->simplified
= pst
->simplified
;
1832 static void simplify_merges(struct rev_info
*revs
)
1834 struct commit_list
*list
;
1835 struct commit_list
*yet_to_do
, **tail
;
1837 if (!revs
->topo_order
)
1838 sort_in_topological_order(&revs
->commits
, revs
->lifo
);
1842 /* feed the list reversed */
1844 for (list
= revs
->commits
; list
; list
= list
->next
)
1845 commit_list_insert(list
->item
, &yet_to_do
);
1851 struct commit
*commit
= list
->item
;
1852 struct commit_list
*next
= list
->next
;
1855 tail
= simplify_one(revs
, commit
, tail
);
1859 /* clean up the result, removing the simplified ones */
1860 list
= revs
->commits
;
1861 revs
->commits
= NULL
;
1862 tail
= &revs
->commits
;
1864 struct commit
*commit
= list
->item
;
1865 struct commit_list
*next
= list
->next
;
1866 struct merge_simplify_state
*st
;
1869 st
= locate_simplify_state(revs
, commit
);
1870 if (st
->simplified
== commit
)
1871 tail
= &commit_list_insert(commit
, tail
)->next
;
1875 static void set_children(struct rev_info
*revs
)
1877 struct commit_list
*l
;
1878 for (l
= revs
->commits
; l
; l
= l
->next
) {
1879 struct commit
*commit
= l
->item
;
1880 struct commit_list
*p
;
1882 for (p
= commit
->parents
; p
; p
= p
->next
)
1883 add_child(revs
, p
->item
, commit
);
1887 int prepare_revision_walk(struct rev_info
*revs
)
1889 int nr
= revs
->pending
.nr
;
1890 struct object_array_entry
*e
, *list
;
1892 e
= list
= revs
->pending
.objects
;
1893 revs
->pending
.nr
= 0;
1894 revs
->pending
.alloc
= 0;
1895 revs
->pending
.objects
= NULL
;
1897 struct commit
*commit
= handle_commit(revs
, e
->item
, e
->name
);
1899 if (!(commit
->object
.flags
& SEEN
)) {
1900 commit
->object
.flags
|= SEEN
;
1901 insert_by_date(commit
, &revs
->commits
);
1911 if (limit_list(revs
) < 0)
1913 if (revs
->topo_order
)
1914 sort_in_topological_order(&revs
->commits
, revs
->lifo
);
1915 if (revs
->full_line_diff
)
1917 if (revs
->rewrite_parents
&& revs
->line_level_traverse
1918 && !revs
->full_line_diff
)
1919 limit_list_line(revs
);
1920 if (revs
->simplify_merges
)
1921 simplify_merges(revs
);
1922 if (revs
->children
.name
)
1927 static enum rewrite_result
rewrite_one(struct rev_info
*revs
, struct commit
**pp
)
1929 struct commit_list
*cache
= NULL
;
1932 struct commit
*p
= *pp
;
1934 if (add_parents_to_list(revs
, p
, &revs
->commits
, &cache
) < 0)
1935 return rewrite_one_error
;
1936 if (p
->parents
&& p
->parents
->next
)
1937 return rewrite_one_ok
;
1938 if (p
->object
.flags
& UNINTERESTING
)
1939 return rewrite_one_ok
;
1940 if (!(p
->object
.flags
& TREESAME
))
1941 return rewrite_one_ok
;
1943 return rewrite_one_noparents
;
1944 *pp
= p
->parents
->item
;
1948 int rewrite_parents(struct rev_info
*revs
, struct commit
*commit
,
1949 rewrite_parent_fn_t rewrite_parent
)
1951 struct commit_list
**pp
= &commit
->parents
;
1953 struct commit_list
*parent
= *pp
;
1954 switch (rewrite_parent(revs
, &parent
->item
)) {
1955 case rewrite_one_ok
:
1957 case rewrite_one_noparents
:
1960 case rewrite_one_error
:
1965 remove_duplicate_parents(commit
);
1969 static int commit_match(struct commit
*commit
, struct rev_info
*opt
)
1971 if (!opt
->grep_filter
.pattern_list
&& !opt
->grep_filter
.header_list
)
1973 return grep_buffer(&opt
->grep_filter
,
1974 NULL
, /* we say nothing, not even filename */
1975 commit
->buffer
, strlen(commit
->buffer
));
1978 static inline int want_ancestry(struct rev_info
*revs
)
1980 return (revs
->rewrite_parents
|| revs
->children
.name
);
1983 enum commit_action
get_commit_action(struct rev_info
*revs
, struct commit
*commit
)
1985 if (commit
->object
.flags
& SHOWN
)
1986 return commit_ignore
;
1987 if (revs
->unpacked
&& has_sha1_pack(commit
->object
.sha1
))
1988 return commit_ignore
;
1991 if (commit
->object
.flags
& UNINTERESTING
)
1992 return commit_ignore
;
1993 if (revs
->min_age
!= -1 && (commit
->date
> revs
->min_age
))
1994 return commit_ignore
;
1995 if (revs
->no_merges
&& commit
->parents
&& commit
->parents
->next
)
1996 return commit_ignore
;
1997 if (revs
->merges_only
&& !(commit
->parents
&& commit
->parents
->next
))
1998 return commit_ignore
;
1999 if (!commit_match(commit
, revs
))
2000 return commit_ignore
;
2001 if (revs
->prune
&& revs
->dense
) {
2002 /* Commit without changes? */
2003 if (commit
->object
.flags
& TREESAME
) {
2004 /* drop merges unless we want parenthood */
2005 if (!want_ancestry(revs
))
2006 return commit_ignore
;
2007 /* non-merge - always ignore it */
2008 if (!commit
->parents
|| !commit
->parents
->next
)
2009 return commit_ignore
;
2015 enum commit_action
simplify_commit(struct rev_info
*revs
, struct commit
*commit
)
2017 enum commit_action action
= get_commit_action(revs
, commit
);
2019 if (action
== commit_show
&&
2021 revs
->prune
&& revs
->dense
&& want_ancestry(revs
)) {
2022 if (rewrite_parents(revs
, commit
, rewrite_one
) < 0)
2023 return commit_error
;
2028 static struct commit
*get_revision_1(struct rev_info
*revs
)
2034 struct commit_list
*entry
= revs
->commits
;
2035 struct commit
*commit
= entry
->item
;
2037 revs
->commits
= entry
->next
;
2040 if (revs
->reflog_info
)
2041 fake_reflog_parent(revs
->reflog_info
, commit
);
2044 * If we haven't done the list limiting, we need to look at
2045 * the parents here. We also need to do the date-based limiting
2046 * that we'd otherwise have done in limit_list().
2048 if (!revs
->limited
) {
2049 if (revs
->max_age
!= -1 &&
2050 (commit
->date
< revs
->max_age
))
2052 if (add_parents_to_list(revs
, commit
, &revs
->commits
, NULL
) < 0)
2053 die("Failed to traverse parents of commit %s",
2054 sha1_to_hex(commit
->object
.sha1
));
2057 switch (simplify_commit(revs
, commit
)) {
2061 die("Failed to simplify parents of commit %s",
2062 sha1_to_hex(commit
->object
.sha1
));
2066 } while (revs
->commits
);
2070 static void gc_boundary(struct object_array
*array
)
2072 unsigned nr
= array
->nr
;
2073 unsigned alloc
= array
->alloc
;
2074 struct object_array_entry
*objects
= array
->objects
;
2078 for (i
= j
= 0; i
< nr
; i
++) {
2079 if (objects
[i
].item
->flags
& SHOWN
)
2082 objects
[j
] = objects
[i
];
2085 for (i
= j
; i
< nr
; i
++)
2086 objects
[i
].item
= NULL
;
2091 static void create_boundary_commit_list(struct rev_info
*revs
)
2095 struct object_array
*array
= &revs
->boundary_commits
;
2096 struct object_array_entry
*objects
= array
->objects
;
2099 * If revs->commits is non-NULL at this point, an error occurred in
2100 * get_revision_1(). Ignore the error and continue printing the
2101 * boundary commits anyway. (This is what the code has always
2104 if (revs
->commits
) {
2105 free_commit_list(revs
->commits
);
2106 revs
->commits
= NULL
;
2110 * Put all of the actual boundary commits from revs->boundary_commits
2111 * into revs->commits
2113 for (i
= 0; i
< array
->nr
; i
++) {
2114 c
= (struct commit
*)(objects
[i
].item
);
2117 if (!(c
->object
.flags
& CHILD_SHOWN
))
2119 if (c
->object
.flags
& (SHOWN
| BOUNDARY
))
2121 c
->object
.flags
|= BOUNDARY
;
2122 commit_list_insert(c
, &revs
->commits
);
2126 * If revs->topo_order is set, sort the boundary commits
2127 * in topological order
2129 sort_in_topological_order(&revs
->commits
, revs
->lifo
);
2132 static struct commit
*get_revision_internal(struct rev_info
*revs
)
2134 struct commit
*c
= NULL
;
2135 struct commit_list
*l
;
2137 if (revs
->boundary
== 2) {
2139 * All of the normal commits have already been returned,
2140 * and we are now returning boundary commits.
2141 * create_boundary_commit_list() has populated
2142 * revs->commits with the remaining commits to return.
2144 c
= pop_commit(&revs
->commits
);
2146 c
->object
.flags
|= SHOWN
;
2151 * Now pick up what they want to give us
2153 c
= get_revision_1(revs
);
2155 while (0 < revs
->skip_count
) {
2157 c
= get_revision_1(revs
);
2164 * Check the max_count.
2166 switch (revs
->max_count
) {
2177 c
->object
.flags
|= SHOWN
;
2179 if (!revs
->boundary
) {
2185 * get_revision_1() runs out the commits, and
2186 * we are done computing the boundaries.
2187 * switch to boundary commits output mode.
2192 * Update revs->commits to contain the list of
2195 create_boundary_commit_list(revs
);
2197 return get_revision_internal(revs
);
2201 * boundary commits are the commits that are parents of the
2202 * ones we got from get_revision_1() but they themselves are
2203 * not returned from get_revision_1(). Before returning
2204 * 'c', we need to mark its parents that they could be boundaries.
2207 for (l
= c
->parents
; l
; l
= l
->next
) {
2209 p
= &(l
->item
->object
);
2210 if (p
->flags
& (CHILD_SHOWN
| SHOWN
))
2212 p
->flags
|= CHILD_SHOWN
;
2213 gc_boundary(&revs
->boundary_commits
);
2214 add_object_array(p
, NULL
, &revs
->boundary_commits
);
2220 struct commit
*get_revision(struct rev_info
*revs
)
2223 struct commit_list
*reversed
;
2225 if (revs
->reverse
) {
2227 while ((c
= get_revision_internal(revs
))) {
2228 commit_list_insert(c
, &reversed
);
2230 revs
->commits
= reversed
;
2232 revs
->reverse_output_stage
= 1;
2235 if (revs
->reverse_output_stage
)
2236 return pop_commit(&revs
->commits
);
2238 c
= get_revision_internal(revs
);
2239 if (c
&& revs
->graph
)
2240 graph_update(revs
->graph
, c
);