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;
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
;
399 if (parse_commit(p
) < 0)
400 die("cannot simplify commit %s (because of %s)",
401 sha1_to_hex(commit
->object
.sha1
),
402 sha1_to_hex(p
->object
.sha1
));
403 switch (rev_compare_tree(revs
, p
, commit
)) {
406 if (!revs
->simplify_history
|| (p
->object
.flags
& UNINTERESTING
)) {
407 /* Even if a merge with an uninteresting
408 * side branch brought the entire change
409 * we are interested in, we do not want
410 * to lose the other branches of this
411 * merge, so we just keep going.
417 commit
->parents
= parent
;
418 commit
->object
.flags
|= TREESAME
;
422 if (revs
->remove_empty_trees
&&
423 rev_same_tree_as_empty(revs
, p
)) {
424 /* We are adding all the specified
425 * paths from this parent, so the
426 * history beyond this parent is not
427 * interesting. Remove its parents
428 * (they are grandparents for us).
429 * IOW, we pretend this parent is a
432 if (parse_commit(p
) < 0)
433 die("cannot simplify commit %s (invalid %s)",
434 sha1_to_hex(commit
->object
.sha1
),
435 sha1_to_hex(p
->object
.sha1
));
440 case REV_TREE_DIFFERENT
:
445 die("bad tree compare for commit %s", sha1_to_hex(commit
->object
.sha1
));
447 if (tree_changed
&& !tree_same
)
449 commit
->object
.flags
|= TREESAME
;
452 static void commit_list_insert_by_date_cached(struct commit
*p
, struct commit_list
**head
,
453 struct commit_list
*cached_base
, struct commit_list
**cache
)
455 struct commit_list
*new_entry
;
457 if (cached_base
&& p
->date
< cached_base
->item
->date
)
458 new_entry
= commit_list_insert_by_date(p
, &cached_base
->next
);
460 new_entry
= commit_list_insert_by_date(p
, head
);
462 if (cache
&& (!*cache
|| p
->date
< (*cache
)->item
->date
))
466 static int add_parents_to_list(struct rev_info
*revs
, struct commit
*commit
,
467 struct commit_list
**list
, struct commit_list
**cache_ptr
)
469 struct commit_list
*parent
= commit
->parents
;
471 struct commit_list
*cached_base
= cache_ptr
? *cache_ptr
: NULL
;
473 if (commit
->object
.flags
& ADDED
)
475 commit
->object
.flags
|= ADDED
;
478 * If the commit is uninteresting, don't try to
479 * prune parents - we want the maximal uninteresting
482 * Normally we haven't parsed the parent
483 * yet, so we won't have a parent of a parent
484 * here. However, it may turn out that we've
485 * reached this commit some other way (where it
486 * wasn't uninteresting), in which case we need
487 * to mark its parents recursively too..
489 if (commit
->object
.flags
& UNINTERESTING
) {
491 struct commit
*p
= parent
->item
;
492 parent
= parent
->next
;
494 p
->object
.flags
|= UNINTERESTING
;
495 if (parse_commit(p
) < 0)
498 mark_parents_uninteresting(p
);
499 if (p
->object
.flags
& SEEN
)
501 p
->object
.flags
|= SEEN
;
502 commit_list_insert_by_date_cached(p
, list
, cached_base
, cache_ptr
);
508 * Ok, the commit wasn't uninteresting. Try to
509 * simplify the commit history and find the parent
510 * that has no differences in the path set if one exists.
512 try_to_simplify_commit(revs
, commit
);
517 left_flag
= (commit
->object
.flags
& SYMMETRIC_LEFT
);
519 for (parent
= commit
->parents
; parent
; parent
= parent
->next
) {
520 struct commit
*p
= parent
->item
;
522 if (parse_commit(p
) < 0)
524 if (revs
->show_source
&& !p
->util
)
525 p
->util
= commit
->util
;
526 p
->object
.flags
|= left_flag
;
527 if (!(p
->object
.flags
& SEEN
)) {
528 p
->object
.flags
|= SEEN
;
529 commit_list_insert_by_date_cached(p
, list
, cached_base
, cache_ptr
);
531 if (revs
->first_parent_only
)
537 static void cherry_pick_list(struct commit_list
*list
, struct rev_info
*revs
)
539 struct commit_list
*p
;
540 int left_count
= 0, right_count
= 0;
542 struct patch_ids ids
;
543 unsigned cherry_flag
;
545 /* First count the commits on the left and on the right */
546 for (p
= list
; p
; p
= p
->next
) {
547 struct commit
*commit
= p
->item
;
548 unsigned flags
= commit
->object
.flags
;
549 if (flags
& BOUNDARY
)
551 else if (flags
& SYMMETRIC_LEFT
)
557 if (!left_count
|| !right_count
)
560 left_first
= left_count
< right_count
;
561 init_patch_ids(&ids
);
562 ids
.diffopts
.pathspec
= revs
->diffopt
.pathspec
;
564 /* Compute patch-ids for one side */
565 for (p
= list
; p
; p
= p
->next
) {
566 struct commit
*commit
= p
->item
;
567 unsigned flags
= commit
->object
.flags
;
569 if (flags
& BOUNDARY
)
572 * If we have fewer left, left_first is set and we omit
573 * commits on the right branch in this loop. If we have
574 * fewer right, we skip the left ones.
576 if (left_first
!= !!(flags
& SYMMETRIC_LEFT
))
578 commit
->util
= add_commit_patch_id(commit
, &ids
);
581 /* either cherry_mark or cherry_pick are true */
582 cherry_flag
= revs
->cherry_mark
? PATCHSAME
: SHOWN
;
584 /* Check the other side */
585 for (p
= list
; p
; p
= p
->next
) {
586 struct commit
*commit
= p
->item
;
588 unsigned flags
= commit
->object
.flags
;
590 if (flags
& BOUNDARY
)
593 * If we have fewer left, left_first is set and we omit
594 * commits on the left branch in this loop.
596 if (left_first
== !!(flags
& SYMMETRIC_LEFT
))
600 * Have we seen the same patch id?
602 id
= has_commit_patch_id(commit
, &ids
);
606 commit
->object
.flags
|= cherry_flag
;
609 /* Now check the original side for seen ones */
610 for (p
= list
; p
; p
= p
->next
) {
611 struct commit
*commit
= p
->item
;
612 struct patch_id
*ent
;
618 commit
->object
.flags
|= cherry_flag
;
622 free_patch_ids(&ids
);
625 /* How many extra uninteresting commits we want to see.. */
628 static int still_interesting(struct commit_list
*src
, unsigned long date
, int slop
)
631 * No source list at all? We're definitely done..
637 * Does the destination list contain entries with a date
638 * before the source list? Definitely _not_ done.
640 if (date
< src
->item
->date
)
644 * Does the source list still have interesting commits in
645 * it? Definitely not done..
647 if (!everybody_uninteresting(src
))
650 /* Ok, we're closing in.. */
655 * "rev-list --ancestry-path A..B" computes commits that are ancestors
656 * of B but not ancestors of A but further limits the result to those
657 * that are descendants of A. This takes the list of bottom commits and
658 * the result of "A..B" without --ancestry-path, and limits the latter
659 * further to the ones that can reach one of the commits in "bottom".
661 static void limit_to_ancestry(struct commit_list
*bottom
, struct commit_list
*list
)
663 struct commit_list
*p
;
664 struct commit_list
*rlist
= NULL
;
668 * Reverse the list so that it will be likely that we would
669 * process parents before children.
671 for (p
= list
; p
; p
= p
->next
)
672 commit_list_insert(p
->item
, &rlist
);
674 for (p
= bottom
; p
; p
= p
->next
)
675 p
->item
->object
.flags
|= TMP_MARK
;
678 * Mark the ones that can reach bottom commits in "list",
679 * in a bottom-up fashion.
683 for (p
= rlist
; p
; p
= p
->next
) {
684 struct commit
*c
= p
->item
;
685 struct commit_list
*parents
;
686 if (c
->object
.flags
& (TMP_MARK
| UNINTERESTING
))
688 for (parents
= c
->parents
;
690 parents
= parents
->next
) {
691 if (!(parents
->item
->object
.flags
& TMP_MARK
))
693 c
->object
.flags
|= TMP_MARK
;
698 } while (made_progress
);
701 * NEEDSWORK: decide if we want to remove parents that are
702 * not marked with TMP_MARK from commit->parents for commits
703 * in the resulting list. We may not want to do that, though.
707 * The ones that are not marked with TMP_MARK are uninteresting
709 for (p
= list
; p
; p
= p
->next
) {
710 struct commit
*c
= p
->item
;
711 if (c
->object
.flags
& TMP_MARK
)
713 c
->object
.flags
|= UNINTERESTING
;
716 /* We are done with the TMP_MARK */
717 for (p
= list
; p
; p
= p
->next
)
718 p
->item
->object
.flags
&= ~TMP_MARK
;
719 for (p
= bottom
; p
; p
= p
->next
)
720 p
->item
->object
.flags
&= ~TMP_MARK
;
721 free_commit_list(rlist
);
725 * Before walking the history, keep the set of "negative" refs the
726 * caller has asked to exclude.
728 * This is used to compute "rev-list --ancestry-path A..B", as we need
729 * to filter the result of "A..B" further to the ones that can actually
732 static struct commit_list
*collect_bottom_commits(struct commit_list
*list
)
734 struct commit_list
*elem
, *bottom
= NULL
;
735 for (elem
= list
; elem
; elem
= elem
->next
)
736 if (elem
->item
->object
.flags
& UNINTERESTING
)
737 commit_list_insert(elem
->item
, &bottom
);
741 /* Assumes either left_only or right_only is set */
742 static void limit_left_right(struct commit_list
*list
, struct rev_info
*revs
)
744 struct commit_list
*p
;
746 for (p
= list
; p
; p
= p
->next
) {
747 struct commit
*commit
= p
->item
;
749 if (revs
->right_only
) {
750 if (commit
->object
.flags
& SYMMETRIC_LEFT
)
751 commit
->object
.flags
|= SHOWN
;
752 } else /* revs->left_only is set */
753 if (!(commit
->object
.flags
& SYMMETRIC_LEFT
))
754 commit
->object
.flags
|= SHOWN
;
758 static int limit_list(struct rev_info
*revs
)
761 unsigned long date
= ~0ul;
762 struct commit_list
*list
= revs
->commits
;
763 struct commit_list
*newlist
= NULL
;
764 struct commit_list
**p
= &newlist
;
765 struct commit_list
*bottom
= NULL
;
767 if (revs
->ancestry_path
) {
768 bottom
= collect_bottom_commits(list
);
770 die("--ancestry-path given but there are no bottom commits");
774 struct commit_list
*entry
= list
;
775 struct commit
*commit
= list
->item
;
776 struct object
*obj
= &commit
->object
;
777 show_early_output_fn_t show
;
782 if (revs
->max_age
!= -1 && (commit
->date
< revs
->max_age
))
783 obj
->flags
|= UNINTERESTING
;
784 if (add_parents_to_list(revs
, commit
, &list
, NULL
) < 0)
786 if (obj
->flags
& UNINTERESTING
) {
787 mark_parents_uninteresting(commit
);
789 p
= &commit_list_insert(commit
, p
)->next
;
790 slop
= still_interesting(list
, date
, slop
);
793 /* If showing all, add the whole pending list to the end */
798 if (revs
->min_age
!= -1 && (commit
->date
> revs
->min_age
))
801 p
= &commit_list_insert(commit
, p
)->next
;
803 show
= show_early_output
;
808 show_early_output
= NULL
;
810 if (revs
->cherry_pick
|| revs
->cherry_mark
)
811 cherry_pick_list(newlist
, revs
);
813 if (revs
->left_only
|| revs
->right_only
)
814 limit_left_right(newlist
, revs
);
817 limit_to_ancestry(bottom
, newlist
);
818 free_commit_list(bottom
);
821 revs
->commits
= newlist
;
827 int warned_bad_reflog
;
828 struct rev_info
*all_revs
;
829 const char *name_for_errormsg
;
832 static int handle_one_ref(const char *path
, const unsigned char *sha1
, int flag
, void *cb_data
)
834 struct all_refs_cb
*cb
= cb_data
;
835 struct object
*object
= get_reference(cb
->all_revs
, path
, sha1
,
837 add_pending_object(cb
->all_revs
, object
, path
);
841 static void init_all_refs_cb(struct all_refs_cb
*cb
, struct rev_info
*revs
,
845 cb
->all_flags
= flags
;
848 static void handle_refs(const char *submodule
, struct rev_info
*revs
, unsigned flags
,
849 int (*for_each
)(const char *, each_ref_fn
, void *))
851 struct all_refs_cb cb
;
852 init_all_refs_cb(&cb
, revs
, flags
);
853 for_each(submodule
, handle_one_ref
, &cb
);
856 static void handle_one_reflog_commit(unsigned char *sha1
, void *cb_data
)
858 struct all_refs_cb
*cb
= cb_data
;
859 if (!is_null_sha1(sha1
)) {
860 struct object
*o
= parse_object(sha1
);
862 o
->flags
|= cb
->all_flags
;
863 add_pending_object(cb
->all_revs
, o
, "");
865 else if (!cb
->warned_bad_reflog
) {
866 warning("reflog of '%s' references pruned commits",
867 cb
->name_for_errormsg
);
868 cb
->warned_bad_reflog
= 1;
873 static int handle_one_reflog_ent(unsigned char *osha1
, unsigned char *nsha1
,
874 const char *email
, unsigned long timestamp
, int tz
,
875 const char *message
, void *cb_data
)
877 handle_one_reflog_commit(osha1
, cb_data
);
878 handle_one_reflog_commit(nsha1
, cb_data
);
882 static int handle_one_reflog(const char *path
, const unsigned char *sha1
, int flag
, void *cb_data
)
884 struct all_refs_cb
*cb
= cb_data
;
885 cb
->warned_bad_reflog
= 0;
886 cb
->name_for_errormsg
= path
;
887 for_each_reflog_ent(path
, handle_one_reflog_ent
, cb_data
);
891 static void handle_reflog(struct rev_info
*revs
, unsigned flags
)
893 struct all_refs_cb cb
;
895 cb
.all_flags
= flags
;
896 for_each_reflog(handle_one_reflog
, &cb
);
899 static int add_parents_only(struct rev_info
*revs
, const char *arg
, int flags
)
901 unsigned char sha1
[20];
903 struct commit
*commit
;
904 struct commit_list
*parents
;
907 flags
^= UNINTERESTING
;
910 if (get_sha1(arg
, sha1
))
913 it
= get_reference(revs
, arg
, sha1
, 0);
914 if (!it
&& revs
->ignore_missing
)
916 if (it
->type
!= OBJ_TAG
)
918 if (!((struct tag
*)it
)->tagged
)
920 hashcpy(sha1
, ((struct tag
*)it
)->tagged
->sha1
);
922 if (it
->type
!= OBJ_COMMIT
)
924 commit
= (struct commit
*)it
;
925 for (parents
= commit
->parents
; parents
; parents
= parents
->next
) {
926 it
= &parents
->item
->object
;
928 add_pending_object(revs
, it
, arg
);
933 void init_revisions(struct rev_info
*revs
, const char *prefix
)
935 memset(revs
, 0, sizeof(*revs
));
937 revs
->abbrev
= DEFAULT_ABBREV
;
938 revs
->ignore_merges
= 1;
939 revs
->simplify_history
= 1;
940 DIFF_OPT_SET(&revs
->pruning
, RECURSIVE
);
941 DIFF_OPT_SET(&revs
->pruning
, QUICK
);
942 revs
->pruning
.add_remove
= file_add_remove
;
943 revs
->pruning
.change
= file_change
;
946 revs
->prefix
= prefix
;
949 revs
->skip_count
= -1;
950 revs
->max_count
= -1;
951 revs
->max_parents
= -1;
953 revs
->commit_format
= CMIT_FMT_DEFAULT
;
955 revs
->grep_filter
.status_only
= 1;
956 revs
->grep_filter
.pattern_tail
= &(revs
->grep_filter
.pattern_list
);
957 revs
->grep_filter
.header_tail
= &(revs
->grep_filter
.header_list
);
958 revs
->grep_filter
.regflags
= REG_NEWLINE
;
960 diff_setup(&revs
->diffopt
);
961 if (prefix
&& !revs
->diffopt
.prefix
) {
962 revs
->diffopt
.prefix
= prefix
;
963 revs
->diffopt
.prefix_length
= strlen(prefix
);
966 revs
->notes_opt
.use_default_notes
= -1;
969 static void add_pending_commit_list(struct rev_info
*revs
,
970 struct commit_list
*commit_list
,
973 while (commit_list
) {
974 struct object
*object
= &commit_list
->item
->object
;
975 object
->flags
|= flags
;
976 add_pending_object(revs
, object
, sha1_to_hex(object
->sha1
));
977 commit_list
= commit_list
->next
;
981 static void prepare_show_merge(struct rev_info
*revs
)
983 struct commit_list
*bases
;
984 struct commit
*head
, *other
;
985 unsigned char sha1
[20];
986 const char **prune
= NULL
;
987 int i
, prune_num
= 1; /* counting terminating NULL */
989 if (get_sha1("HEAD", sha1
) || !(head
= lookup_commit(sha1
)))
990 die("--merge without HEAD?");
991 if (get_sha1("MERGE_HEAD", sha1
) || !(other
= lookup_commit(sha1
)))
992 die("--merge without MERGE_HEAD?");
993 add_pending_object(revs
, &head
->object
, "HEAD");
994 add_pending_object(revs
, &other
->object
, "MERGE_HEAD");
995 bases
= get_merge_bases(head
, other
, 1);
996 add_pending_commit_list(revs
, bases
, UNINTERESTING
);
997 free_commit_list(bases
);
998 head
->object
.flags
|= SYMMETRIC_LEFT
;
1002 for (i
= 0; i
< active_nr
; i
++) {
1003 struct cache_entry
*ce
= active_cache
[i
];
1006 if (ce_path_match(ce
, &revs
->prune_data
)) {
1008 prune
= xrealloc(prune
, sizeof(*prune
) * prune_num
);
1009 prune
[prune_num
-2] = ce
->name
;
1010 prune
[prune_num
-1] = NULL
;
1012 while ((i
+1 < active_nr
) &&
1013 ce_same_name(ce
, active_cache
[i
+1]))
1016 free_pathspec(&revs
->prune_data
);
1017 init_pathspec(&revs
->prune_data
, prune
);
1021 int handle_revision_arg(const char *arg
, struct rev_info
*revs
,
1023 int cant_be_filename
)
1027 struct object
*object
;
1028 unsigned char sha1
[20];
1031 dotdot
= strstr(arg
, "..");
1033 unsigned char from_sha1
[20];
1034 const char *next
= dotdot
+ 2;
1035 const char *this = arg
;
1036 int symmetric
= *next
== '.';
1037 unsigned int flags_exclude
= flags
^ UNINTERESTING
;
1046 if (!get_sha1(this, from_sha1
) &&
1047 !get_sha1(next
, sha1
)) {
1048 struct commit
*a
, *b
;
1049 struct commit_list
*exclude
;
1051 a
= lookup_commit_reference(from_sha1
);
1052 b
= lookup_commit_reference(sha1
);
1054 if (revs
->ignore_missing
)
1057 "Invalid symmetric difference expression %s...%s" :
1058 "Invalid revision range %s..%s",
1062 if (!cant_be_filename
) {
1064 verify_non_filename(revs
->prefix
, arg
);
1068 exclude
= get_merge_bases(a
, b
, 1);
1069 add_pending_commit_list(revs
, exclude
,
1071 free_commit_list(exclude
);
1072 a
->object
.flags
|= flags
| SYMMETRIC_LEFT
;
1074 a
->object
.flags
|= flags_exclude
;
1075 b
->object
.flags
|= flags
;
1076 add_pending_object(revs
, &a
->object
, this);
1077 add_pending_object(revs
, &b
->object
, next
);
1082 dotdot
= strstr(arg
, "^@");
1083 if (dotdot
&& !dotdot
[2]) {
1085 if (add_parents_only(revs
, arg
, flags
))
1089 dotdot
= strstr(arg
, "^!");
1090 if (dotdot
&& !dotdot
[2]) {
1092 if (!add_parents_only(revs
, arg
, flags
^ UNINTERESTING
))
1098 local_flags
= UNINTERESTING
;
1101 if (get_sha1_with_mode(arg
, sha1
, &mode
))
1102 return revs
->ignore_missing
? 0 : -1;
1103 if (!cant_be_filename
)
1104 verify_non_filename(revs
->prefix
, arg
);
1105 object
= get_reference(revs
, arg
, sha1
, flags
^ local_flags
);
1106 add_pending_object_with_mode(revs
, object
, arg
, mode
);
1110 static void read_pathspec_from_stdin(struct rev_info
*revs
, struct strbuf
*sb
, const char ***prune_data
)
1112 const char **prune
= *prune_data
;
1116 /* count existing ones */
1120 for (prune_nr
= 0; prune
[prune_nr
]; prune_nr
++)
1122 prune_alloc
= prune_nr
; /* not really, but we do not know */
1124 while (strbuf_getwholeline(sb
, stdin
, '\n') != EOF
) {
1126 if (len
&& sb
->buf
[len
- 1] == '\n')
1127 sb
->buf
[--len
] = '\0';
1128 ALLOC_GROW(prune
, prune_nr
+1, prune_alloc
);
1129 prune
[prune_nr
++] = xstrdup(sb
->buf
);
1132 ALLOC_GROW(prune
, prune_nr
+1, prune_alloc
);
1133 prune
[prune_nr
] = NULL
;
1135 *prune_data
= prune
;
1138 static void read_revisions_from_stdin(struct rev_info
*revs
, const char ***prune
)
1141 int seen_dashdash
= 0;
1143 strbuf_init(&sb
, 1000);
1144 while (strbuf_getwholeline(&sb
, stdin
, '\n') != EOF
) {
1146 if (len
&& sb
.buf
[len
- 1] == '\n')
1147 sb
.buf
[--len
] = '\0';
1150 if (sb
.buf
[0] == '-') {
1151 if (len
== 2 && sb
.buf
[1] == '-') {
1155 die("options not supported in --stdin mode");
1157 if (handle_revision_arg(sb
.buf
, revs
, 0, 1))
1158 die("bad revision '%s'", sb
.buf
);
1161 read_pathspec_from_stdin(revs
, &sb
, prune
);
1162 strbuf_release(&sb
);
1165 static void add_grep(struct rev_info
*revs
, const char *ptn
, enum grep_pat_token what
)
1167 append_grep_pattern(&revs
->grep_filter
, ptn
, "command line", 0, what
);
1170 static void add_header_grep(struct rev_info
*revs
, enum grep_header_field field
, const char *pattern
)
1172 append_header_grep_pattern(&revs
->grep_filter
, field
, pattern
);
1175 static void add_message_grep(struct rev_info
*revs
, const char *pattern
)
1177 add_grep(revs
, pattern
, GREP_PATTERN_BODY
);
1180 static int handle_revision_opt(struct rev_info
*revs
, int argc
, const char **argv
,
1181 int *unkc
, const char **unkv
)
1183 const char *arg
= argv
[0];
1187 /* pseudo revision arguments */
1188 if (!strcmp(arg
, "--all") || !strcmp(arg
, "--branches") ||
1189 !strcmp(arg
, "--tags") || !strcmp(arg
, "--remotes") ||
1190 !strcmp(arg
, "--reflog") || !strcmp(arg
, "--not") ||
1191 !strcmp(arg
, "--no-walk") || !strcmp(arg
, "--do-walk") ||
1192 !strcmp(arg
, "--bisect") || !prefixcmp(arg
, "--glob=") ||
1193 !prefixcmp(arg
, "--branches=") || !prefixcmp(arg
, "--tags=") ||
1194 !prefixcmp(arg
, "--remotes="))
1196 unkv
[(*unkc
)++] = arg
;
1200 if ((argcount
= parse_long_opt("max-count", argv
, &optarg
))) {
1201 revs
->max_count
= atoi(optarg
);
1204 } else if ((argcount
= parse_long_opt("skip", argv
, &optarg
))) {
1205 revs
->skip_count
= atoi(optarg
);
1207 } else if ((*arg
== '-') && isdigit(arg
[1])) {
1208 /* accept -<digit>, like traditional "head" */
1209 revs
->max_count
= atoi(arg
+ 1);
1211 } else if (!strcmp(arg
, "-n")) {
1213 return error("-n requires an argument");
1214 revs
->max_count
= atoi(argv
[1]);
1217 } else if (!prefixcmp(arg
, "-n")) {
1218 revs
->max_count
= atoi(arg
+ 2);
1220 } else if ((argcount
= parse_long_opt("max-age", argv
, &optarg
))) {
1221 revs
->max_age
= atoi(optarg
);
1223 } else if ((argcount
= parse_long_opt("since", argv
, &optarg
))) {
1224 revs
->max_age
= approxidate(optarg
);
1226 } else if ((argcount
= parse_long_opt("after", argv
, &optarg
))) {
1227 revs
->max_age
= approxidate(optarg
);
1229 } else if ((argcount
= parse_long_opt("min-age", argv
, &optarg
))) {
1230 revs
->min_age
= atoi(optarg
);
1232 } else if ((argcount
= parse_long_opt("before", argv
, &optarg
))) {
1233 revs
->min_age
= approxidate(optarg
);
1235 } else if ((argcount
= parse_long_opt("until", argv
, &optarg
))) {
1236 revs
->min_age
= approxidate(optarg
);
1238 } else if (!strcmp(arg
, "--first-parent")) {
1239 revs
->first_parent_only
= 1;
1240 } else if (!strcmp(arg
, "--ancestry-path")) {
1241 revs
->ancestry_path
= 1;
1242 revs
->simplify_history
= 0;
1244 } else if (!strcmp(arg
, "-g") || !strcmp(arg
, "--walk-reflogs")) {
1245 init_reflog_walk(&revs
->reflog_info
);
1246 } else if (!strcmp(arg
, "--default")) {
1248 return error("bad --default argument");
1249 revs
->def
= argv
[1];
1251 } else if (!strcmp(arg
, "--merge")) {
1252 revs
->show_merge
= 1;
1253 } else if (!strcmp(arg
, "--topo-order")) {
1255 revs
->topo_order
= 1;
1256 } else if (!strcmp(arg
, "--simplify-merges")) {
1257 revs
->simplify_merges
= 1;
1258 revs
->rewrite_parents
= 1;
1259 revs
->simplify_history
= 0;
1261 } else if (!strcmp(arg
, "--simplify-by-decoration")) {
1262 revs
->simplify_merges
= 1;
1263 revs
->rewrite_parents
= 1;
1264 revs
->simplify_history
= 0;
1265 revs
->simplify_by_decoration
= 1;
1268 load_ref_decorations(DECORATE_SHORT_REFS
);
1269 } else if (!strcmp(arg
, "--date-order")) {
1271 revs
->topo_order
= 1;
1272 } else if (!prefixcmp(arg
, "--early-output")) {
1276 count
= atoi(arg
+15);
1279 revs
->topo_order
= 1;
1280 revs
->early_output
= count
;
1282 } else if (!strcmp(arg
, "--parents")) {
1283 revs
->rewrite_parents
= 1;
1284 revs
->print_parents
= 1;
1285 } else if (!strcmp(arg
, "--dense")) {
1287 } else if (!strcmp(arg
, "--sparse")) {
1289 } else if (!strcmp(arg
, "--show-all")) {
1291 } else if (!strcmp(arg
, "--remove-empty")) {
1292 revs
->remove_empty_trees
= 1;
1293 } else if (!strcmp(arg
, "--merges")) {
1294 revs
->min_parents
= 2;
1295 } else if (!strcmp(arg
, "--no-merges")) {
1296 revs
->max_parents
= 1;
1297 } else if (!prefixcmp(arg
, "--min-parents=")) {
1298 revs
->min_parents
= atoi(arg
+14);
1299 } else if (!prefixcmp(arg
, "--no-min-parents")) {
1300 revs
->min_parents
= 0;
1301 } else if (!prefixcmp(arg
, "--max-parents=")) {
1302 revs
->max_parents
= atoi(arg
+14);
1303 } else if (!prefixcmp(arg
, "--no-max-parents")) {
1304 revs
->max_parents
= -1;
1305 } else if (!strcmp(arg
, "--boundary")) {
1307 } else if (!strcmp(arg
, "--left-right")) {
1308 revs
->left_right
= 1;
1309 } else if (!strcmp(arg
, "--left-only")) {
1310 if (revs
->right_only
)
1311 die("--left-only is incompatible with --right-only"
1313 revs
->left_only
= 1;
1314 } else if (!strcmp(arg
, "--right-only")) {
1315 if (revs
->left_only
)
1316 die("--right-only is incompatible with --left-only");
1317 revs
->right_only
= 1;
1318 } else if (!strcmp(arg
, "--cherry")) {
1319 if (revs
->left_only
)
1320 die("--cherry is incompatible with --left-only");
1321 revs
->cherry_mark
= 1;
1322 revs
->right_only
= 1;
1323 revs
->max_parents
= 1;
1325 } else if (!strcmp(arg
, "--count")) {
1327 } else if (!strcmp(arg
, "--cherry-mark")) {
1328 if (revs
->cherry_pick
)
1329 die("--cherry-mark is incompatible with --cherry-pick");
1330 revs
->cherry_mark
= 1;
1331 revs
->limited
= 1; /* needs limit_list() */
1332 } else if (!strcmp(arg
, "--cherry-pick")) {
1333 if (revs
->cherry_mark
)
1334 die("--cherry-pick is incompatible with --cherry-mark");
1335 revs
->cherry_pick
= 1;
1337 } else if (!strcmp(arg
, "--objects")) {
1338 revs
->tag_objects
= 1;
1339 revs
->tree_objects
= 1;
1340 revs
->blob_objects
= 1;
1341 } else if (!strcmp(arg
, "--objects-edge")) {
1342 revs
->tag_objects
= 1;
1343 revs
->tree_objects
= 1;
1344 revs
->blob_objects
= 1;
1345 revs
->edge_hint
= 1;
1346 } else if (!strcmp(arg
, "--unpacked")) {
1348 } else if (!prefixcmp(arg
, "--unpacked=")) {
1349 die("--unpacked=<packfile> no longer supported.");
1350 } else if (!strcmp(arg
, "-r")) {
1352 DIFF_OPT_SET(&revs
->diffopt
, RECURSIVE
);
1353 } else if (!strcmp(arg
, "-t")) {
1355 DIFF_OPT_SET(&revs
->diffopt
, RECURSIVE
);
1356 DIFF_OPT_SET(&revs
->diffopt
, TREE_IN_RECURSIVE
);
1357 } else if (!strcmp(arg
, "-m")) {
1358 revs
->ignore_merges
= 0;
1359 } else if (!strcmp(arg
, "-c")) {
1361 revs
->dense_combined_merges
= 0;
1362 revs
->combine_merges
= 1;
1363 } else if (!strcmp(arg
, "--cc")) {
1365 revs
->dense_combined_merges
= 1;
1366 revs
->combine_merges
= 1;
1367 } else if (!strcmp(arg
, "-v")) {
1368 revs
->verbose_header
= 1;
1369 } else if (!strcmp(arg
, "--pretty")) {
1370 revs
->verbose_header
= 1;
1371 revs
->pretty_given
= 1;
1372 get_commit_format(arg
+8, revs
);
1373 } else if (!prefixcmp(arg
, "--pretty=") || !prefixcmp(arg
, "--format=")) {
1375 * Detached form ("--pretty X" as opposed to "--pretty=X")
1376 * not allowed, since the argument is optional.
1378 revs
->verbose_header
= 1;
1379 revs
->pretty_given
= 1;
1380 get_commit_format(arg
+9, revs
);
1381 } else if (!strcmp(arg
, "--show-notes") || !strcmp(arg
, "--notes")) {
1382 revs
->show_notes
= 1;
1383 revs
->show_notes_given
= 1;
1384 revs
->notes_opt
.use_default_notes
= 1;
1385 } else if (!prefixcmp(arg
, "--show-notes=") ||
1386 !prefixcmp(arg
, "--notes=")) {
1387 struct strbuf buf
= STRBUF_INIT
;
1388 revs
->show_notes
= 1;
1389 revs
->show_notes_given
= 1;
1390 if (!prefixcmp(arg
, "--show-notes")) {
1391 if (revs
->notes_opt
.use_default_notes
< 0)
1392 revs
->notes_opt
.use_default_notes
= 1;
1393 strbuf_addstr(&buf
, arg
+13);
1396 strbuf_addstr(&buf
, arg
+8);
1397 expand_notes_ref(&buf
);
1398 string_list_append(&revs
->notes_opt
.extra_notes_refs
,
1399 strbuf_detach(&buf
, NULL
));
1400 } else if (!strcmp(arg
, "--no-notes")) {
1401 revs
->show_notes
= 0;
1402 revs
->show_notes_given
= 1;
1403 revs
->notes_opt
.use_default_notes
= -1;
1404 /* we have been strdup'ing ourselves, so trick
1405 * string_list into free()ing strings */
1406 revs
->notes_opt
.extra_notes_refs
.strdup_strings
= 1;
1407 string_list_clear(&revs
->notes_opt
.extra_notes_refs
, 0);
1408 revs
->notes_opt
.extra_notes_refs
.strdup_strings
= 0;
1409 } else if (!strcmp(arg
, "--standard-notes")) {
1410 revs
->show_notes_given
= 1;
1411 revs
->notes_opt
.use_default_notes
= 1;
1412 } else if (!strcmp(arg
, "--no-standard-notes")) {
1413 revs
->notes_opt
.use_default_notes
= 0;
1414 } else if (!strcmp(arg
, "--oneline")) {
1415 revs
->verbose_header
= 1;
1416 get_commit_format("oneline", revs
);
1417 revs
->pretty_given
= 1;
1418 revs
->abbrev_commit
= 1;
1419 } else if (!strcmp(arg
, "--graph")) {
1420 revs
->topo_order
= 1;
1421 revs
->rewrite_parents
= 1;
1422 revs
->graph
= graph_init(revs
);
1423 } else if (!strcmp(arg
, "--root")) {
1424 revs
->show_root_diff
= 1;
1425 } else if (!strcmp(arg
, "--no-commit-id")) {
1426 revs
->no_commit_id
= 1;
1427 } else if (!strcmp(arg
, "--always")) {
1428 revs
->always_show_header
= 1;
1429 } else if (!strcmp(arg
, "--no-abbrev")) {
1431 } else if (!strcmp(arg
, "--abbrev")) {
1432 revs
->abbrev
= DEFAULT_ABBREV
;
1433 } else if (!prefixcmp(arg
, "--abbrev=")) {
1434 revs
->abbrev
= strtoul(arg
+ 9, NULL
, 10);
1435 if (revs
->abbrev
< MINIMUM_ABBREV
)
1436 revs
->abbrev
= MINIMUM_ABBREV
;
1437 else if (revs
->abbrev
> 40)
1439 } else if (!strcmp(arg
, "--abbrev-commit")) {
1440 revs
->abbrev_commit
= 1;
1441 } else if (!strcmp(arg
, "--full-diff")) {
1443 revs
->full_diff
= 1;
1444 } else if (!strcmp(arg
, "--full-history")) {
1445 revs
->simplify_history
= 0;
1446 } else if (!strcmp(arg
, "--relative-date")) {
1447 revs
->date_mode
= DATE_RELATIVE
;
1448 revs
->date_mode_explicit
= 1;
1449 } else if ((argcount
= parse_long_opt("date", argv
, &optarg
))) {
1450 revs
->date_mode
= parse_date_format(optarg
);
1451 revs
->date_mode_explicit
= 1;
1453 } else if (!strcmp(arg
, "--log-size")) {
1454 revs
->show_log_size
= 1;
1457 * Grepping the commit log
1459 else if ((argcount
= parse_long_opt("author", argv
, &optarg
))) {
1460 add_header_grep(revs
, GREP_HEADER_AUTHOR
, optarg
);
1462 } else if ((argcount
= parse_long_opt("committer", argv
, &optarg
))) {
1463 add_header_grep(revs
, GREP_HEADER_COMMITTER
, optarg
);
1465 } else if ((argcount
= parse_long_opt("grep", argv
, &optarg
))) {
1466 add_message_grep(revs
, optarg
);
1468 } else if (!strcmp(arg
, "--extended-regexp") || !strcmp(arg
, "-E")) {
1469 revs
->grep_filter
.regflags
|= REG_EXTENDED
;
1470 } else if (!strcmp(arg
, "--regexp-ignore-case") || !strcmp(arg
, "-i")) {
1471 revs
->grep_filter
.regflags
|= REG_ICASE
;
1472 } else if (!strcmp(arg
, "--fixed-strings") || !strcmp(arg
, "-F")) {
1473 revs
->grep_filter
.fixed
= 1;
1474 } else if (!strcmp(arg
, "--all-match")) {
1475 revs
->grep_filter
.all_match
= 1;
1476 } else if ((argcount
= parse_long_opt("encoding", argv
, &optarg
))) {
1477 if (strcmp(optarg
, "none"))
1478 git_log_output_encoding
= xstrdup(optarg
);
1480 git_log_output_encoding
= "";
1482 } else if (!strcmp(arg
, "--reverse")) {
1484 } else if (!strcmp(arg
, "--children")) {
1485 revs
->children
.name
= "children";
1487 } else if (!strcmp(arg
, "--ignore-missing")) {
1488 revs
->ignore_missing
= 1;
1490 int opts
= diff_opt_parse(&revs
->diffopt
, argv
, argc
);
1492 unkv
[(*unkc
)++] = arg
;
1499 void parse_revision_opt(struct rev_info
*revs
, struct parse_opt_ctx_t
*ctx
,
1500 const struct option
*options
,
1501 const char * const usagestr
[])
1503 int n
= handle_revision_opt(revs
, ctx
->argc
, ctx
->argv
,
1504 &ctx
->cpidx
, ctx
->out
);
1506 error("unknown option `%s'", ctx
->argv
[0]);
1507 usage_with_options(usagestr
, options
);
1513 static int for_each_bad_bisect_ref(const char *submodule
, each_ref_fn fn
, void *cb_data
)
1515 return for_each_ref_in_submodule(submodule
, "refs/bisect/bad", fn
, cb_data
);
1518 static int for_each_good_bisect_ref(const char *submodule
, each_ref_fn fn
, void *cb_data
)
1520 return for_each_ref_in_submodule(submodule
, "refs/bisect/good", fn
, cb_data
);
1523 static void append_prune_data(const char ***prune_data
, const char **av
)
1525 const char **prune
= *prune_data
;
1534 /* count existing ones */
1535 for (prune_nr
= 0; prune
[prune_nr
]; prune_nr
++)
1537 prune_alloc
= prune_nr
; /* not really, but we do not know */
1540 ALLOC_GROW(prune
, prune_nr
+1, prune_alloc
);
1541 prune
[prune_nr
++] = *av
;
1545 ALLOC_GROW(prune
, prune_nr
+1, prune_alloc
);
1546 prune
[prune_nr
] = NULL
;
1548 *prune_data
= prune
;
1551 static int handle_revision_pseudo_opt(const char *submodule
,
1552 struct rev_info
*revs
,
1553 int argc
, const char **argv
, int *flags
)
1555 const char *arg
= argv
[0];
1562 * Commands like "git shortlog" will not accept the options below
1563 * unless parse_revision_opt queues them (as opposed to erroring
1566 * When implementing your new pseudo-option, remember to
1567 * register it in the list at the top of handle_revision_opt.
1569 if (!strcmp(arg
, "--all")) {
1570 handle_refs(submodule
, revs
, *flags
, for_each_ref_submodule
);
1571 handle_refs(submodule
, revs
, *flags
, head_ref_submodule
);
1572 } else if (!strcmp(arg
, "--branches")) {
1573 handle_refs(submodule
, revs
, *flags
, for_each_branch_ref_submodule
);
1574 } else if (!strcmp(arg
, "--bisect")) {
1575 handle_refs(submodule
, revs
, *flags
, for_each_bad_bisect_ref
);
1576 handle_refs(submodule
, revs
, *flags
^ UNINTERESTING
, for_each_good_bisect_ref
);
1578 } else if (!strcmp(arg
, "--tags")) {
1579 handle_refs(submodule
, revs
, *flags
, for_each_tag_ref_submodule
);
1580 } else if (!strcmp(arg
, "--remotes")) {
1581 handle_refs(submodule
, revs
, *flags
, for_each_remote_ref_submodule
);
1582 } else if ((argcount
= parse_long_opt("glob", argv
, &optarg
))) {
1583 struct all_refs_cb cb
;
1584 init_all_refs_cb(&cb
, revs
, *flags
);
1585 for_each_glob_ref(handle_one_ref
, optarg
, &cb
);
1587 } else if (!prefixcmp(arg
, "--branches=")) {
1588 struct all_refs_cb cb
;
1589 init_all_refs_cb(&cb
, revs
, *flags
);
1590 for_each_glob_ref_in(handle_one_ref
, arg
+ 11, "refs/heads/", &cb
);
1591 } else if (!prefixcmp(arg
, "--tags=")) {
1592 struct all_refs_cb cb
;
1593 init_all_refs_cb(&cb
, revs
, *flags
);
1594 for_each_glob_ref_in(handle_one_ref
, arg
+ 7, "refs/tags/", &cb
);
1595 } else if (!prefixcmp(arg
, "--remotes=")) {
1596 struct all_refs_cb cb
;
1597 init_all_refs_cb(&cb
, revs
, *flags
);
1598 for_each_glob_ref_in(handle_one_ref
, arg
+ 10, "refs/remotes/", &cb
);
1599 } else if (!strcmp(arg
, "--reflog")) {
1600 handle_reflog(revs
, *flags
);
1601 } else if (!strcmp(arg
, "--not")) {
1602 *flags
^= UNINTERESTING
;
1603 } else if (!strcmp(arg
, "--no-walk")) {
1605 } else if (!strcmp(arg
, "--do-walk")) {
1615 * Parse revision information, filling in the "rev_info" structure,
1616 * and removing the used arguments from the argument list.
1618 * Returns the number of arguments left that weren't recognized
1619 * (which are also moved to the head of the argument list)
1621 int setup_revisions(int argc
, const char **argv
, struct rev_info
*revs
, struct setup_revision_opt
*opt
)
1623 int i
, flags
, left
, seen_dashdash
, read_from_stdin
, got_rev_arg
= 0;
1624 const char **prune_data
= NULL
;
1625 const char *submodule
= NULL
;
1628 submodule
= opt
->submodule
;
1630 /* First, search for "--" */
1632 for (i
= 1; i
< argc
; i
++) {
1633 const char *arg
= argv
[i
];
1634 if (strcmp(arg
, "--"))
1639 prune_data
= argv
+ i
+ 1;
1644 /* Second, deal with arguments and options */
1646 read_from_stdin
= 0;
1647 for (left
= i
= 1; i
< argc
; i
++) {
1648 const char *arg
= argv
[i
];
1652 opts
= handle_revision_pseudo_opt(submodule
,
1653 revs
, argc
- i
, argv
+ i
,
1660 if (!strcmp(arg
, "--stdin")) {
1661 if (revs
->disable_stdin
) {
1665 if (read_from_stdin
++)
1666 die("--stdin given twice?");
1667 read_revisions_from_stdin(revs
, &prune_data
);
1671 opts
= handle_revision_opt(revs
, argc
- i
, argv
+ i
, &left
, argv
);
1681 if (handle_revision_arg(arg
, revs
, flags
, seen_dashdash
)) {
1683 if (seen_dashdash
|| *arg
== '^')
1684 die("bad revision '%s'", arg
);
1686 /* If we didn't have a "--":
1687 * (1) all filenames must exist;
1688 * (2) all rev-args must not be interpretable
1689 * as a valid filename.
1690 * but the latter we have checked in the main loop.
1692 for (j
= i
; j
< argc
; j
++)
1693 verify_filename(revs
->prefix
, argv
[j
]);
1695 append_prune_data(&prune_data
, argv
+ i
);
1703 init_pathspec(&revs
->prune_data
, get_pathspec(revs
->prefix
, prune_data
));
1705 if (revs
->def
== NULL
)
1706 revs
->def
= opt
? opt
->def
: NULL
;
1707 if (opt
&& opt
->tweak
)
1708 opt
->tweak(revs
, opt
);
1709 if (revs
->show_merge
)
1710 prepare_show_merge(revs
);
1711 if (revs
->def
&& !revs
->pending
.nr
&& !got_rev_arg
) {
1712 unsigned char sha1
[20];
1713 struct object
*object
;
1715 if (get_sha1_with_mode(revs
->def
, sha1
, &mode
))
1716 die("bad default revision '%s'", revs
->def
);
1717 object
= get_reference(revs
, revs
->def
, sha1
, 0);
1718 add_pending_object_with_mode(revs
, object
, revs
->def
, mode
);
1721 /* Did the user ask for any diff output? Run the diff! */
1722 if (revs
->diffopt
.output_format
& ~DIFF_FORMAT_NO_OUTPUT
)
1725 /* Pickaxe, diff-filter and rename following need diffs */
1726 if (revs
->diffopt
.pickaxe
||
1727 revs
->diffopt
.filter
||
1728 DIFF_OPT_TST(&revs
->diffopt
, FOLLOW_RENAMES
))
1731 if (revs
->topo_order
)
1734 if (revs
->prune_data
.nr
) {
1735 diff_tree_setup_paths(revs
->prune_data
.raw
, &revs
->pruning
);
1736 /* Can't prune commits with rename following: the paths change.. */
1737 if (!DIFF_OPT_TST(&revs
->diffopt
, FOLLOW_RENAMES
))
1739 if (!revs
->full_diff
)
1740 diff_tree_setup_paths(revs
->prune_data
.raw
, &revs
->diffopt
);
1742 if (revs
->combine_merges
)
1743 revs
->ignore_merges
= 0;
1744 revs
->diffopt
.abbrev
= revs
->abbrev
;
1745 if (diff_setup_done(&revs
->diffopt
) < 0)
1746 die("diff_setup_done failed");
1748 compile_grep_patterns(&revs
->grep_filter
);
1750 if (revs
->reverse
&& revs
->reflog_info
)
1751 die("cannot combine --reverse with --walk-reflogs");
1752 if (revs
->rewrite_parents
&& revs
->children
.name
)
1753 die("cannot combine --parents and --children");
1756 * Limitations on the graph functionality
1758 if (revs
->reverse
&& revs
->graph
)
1759 die("cannot combine --reverse with --graph");
1761 if (revs
->reflog_info
&& revs
->graph
)
1762 die("cannot combine --walk-reflogs with --graph");
1767 static void add_child(struct rev_info
*revs
, struct commit
*parent
, struct commit
*child
)
1769 struct commit_list
*l
= xcalloc(1, sizeof(*l
));
1772 l
->next
= add_decoration(&revs
->children
, &parent
->object
, l
);
1775 static int remove_duplicate_parents(struct commit
*commit
)
1777 struct commit_list
**pp
, *p
;
1778 int surviving_parents
;
1780 /* Examine existing parents while marking ones we have seen... */
1781 pp
= &commit
->parents
;
1782 while ((p
= *pp
) != NULL
) {
1783 struct commit
*parent
= p
->item
;
1784 if (parent
->object
.flags
& TMP_MARK
) {
1788 parent
->object
.flags
|= TMP_MARK
;
1791 /* count them while clearing the temporary mark */
1792 surviving_parents
= 0;
1793 for (p
= commit
->parents
; p
; p
= p
->next
) {
1794 p
->item
->object
.flags
&= ~TMP_MARK
;
1795 surviving_parents
++;
1797 return surviving_parents
;
1800 struct merge_simplify_state
{
1801 struct commit
*simplified
;
1804 static struct merge_simplify_state
*locate_simplify_state(struct rev_info
*revs
, struct commit
*commit
)
1806 struct merge_simplify_state
*st
;
1808 st
= lookup_decoration(&revs
->merge_simplification
, &commit
->object
);
1810 st
= xcalloc(1, sizeof(*st
));
1811 add_decoration(&revs
->merge_simplification
, &commit
->object
, st
);
1816 static struct commit_list
**simplify_one(struct rev_info
*revs
, struct commit
*commit
, struct commit_list
**tail
)
1818 struct commit_list
*p
;
1819 struct merge_simplify_state
*st
, *pst
;
1822 st
= locate_simplify_state(revs
, commit
);
1825 * Have we handled this one?
1831 * An UNINTERESTING commit simplifies to itself, so does a
1832 * root commit. We do not rewrite parents of such commit
1835 if ((commit
->object
.flags
& UNINTERESTING
) || !commit
->parents
) {
1836 st
->simplified
= commit
;
1841 * Do we know what commit all of our parents should be rewritten to?
1842 * Otherwise we are not ready to rewrite this one yet.
1844 for (cnt
= 0, p
= commit
->parents
; p
; p
= p
->next
) {
1845 pst
= locate_simplify_state(revs
, p
->item
);
1846 if (!pst
->simplified
) {
1847 tail
= &commit_list_insert(p
->item
, tail
)->next
;
1852 tail
= &commit_list_insert(commit
, tail
)->next
;
1857 * Rewrite our list of parents.
1859 for (p
= commit
->parents
; p
; p
= p
->next
) {
1860 pst
= locate_simplify_state(revs
, p
->item
);
1861 p
->item
= pst
->simplified
;
1863 cnt
= remove_duplicate_parents(commit
);
1866 * It is possible that we are a merge and one side branch
1867 * does not have any commit that touches the given paths;
1868 * in such a case, the immediate parents will be rewritten
1869 * to different commits.
1871 * o----X X: the commit we are looking at;
1872 * / / o: a commit that touches the paths;
1875 * Further reduce the parents by removing redundant parents.
1878 struct commit_list
*h
= reduce_heads(commit
->parents
);
1879 cnt
= commit_list_count(h
);
1880 free_commit_list(commit
->parents
);
1881 commit
->parents
= h
;
1885 * A commit simplifies to itself if it is a root, if it is
1886 * UNINTERESTING, if it touches the given paths, or if it is a
1887 * merge and its parents simplifies to more than one commits
1888 * (the first two cases are already handled at the beginning of
1891 * Otherwise, it simplifies to what its sole parent simplifies to.
1894 (commit
->object
.flags
& UNINTERESTING
) ||
1895 !(commit
->object
.flags
& TREESAME
) ||
1897 st
->simplified
= commit
;
1899 pst
= locate_simplify_state(revs
, commit
->parents
->item
);
1900 st
->simplified
= pst
->simplified
;
1905 static void simplify_merges(struct rev_info
*revs
)
1907 struct commit_list
*list
;
1908 struct commit_list
*yet_to_do
, **tail
;
1910 if (!revs
->topo_order
)
1911 sort_in_topological_order(&revs
->commits
, revs
->lifo
);
1915 /* feed the list reversed */
1917 for (list
= revs
->commits
; list
; list
= list
->next
)
1918 commit_list_insert(list
->item
, &yet_to_do
);
1924 struct commit
*commit
= list
->item
;
1925 struct commit_list
*next
= list
->next
;
1928 tail
= simplify_one(revs
, commit
, tail
);
1932 /* clean up the result, removing the simplified ones */
1933 list
= revs
->commits
;
1934 revs
->commits
= NULL
;
1935 tail
= &revs
->commits
;
1937 struct commit
*commit
= list
->item
;
1938 struct commit_list
*next
= list
->next
;
1939 struct merge_simplify_state
*st
;
1942 st
= locate_simplify_state(revs
, commit
);
1943 if (st
->simplified
== commit
)
1944 tail
= &commit_list_insert(commit
, tail
)->next
;
1948 static void set_children(struct rev_info
*revs
)
1950 struct commit_list
*l
;
1951 for (l
= revs
->commits
; l
; l
= l
->next
) {
1952 struct commit
*commit
= l
->item
;
1953 struct commit_list
*p
;
1955 for (p
= commit
->parents
; p
; p
= p
->next
)
1956 add_child(revs
, p
->item
, commit
);
1960 int prepare_revision_walk(struct rev_info
*revs
)
1962 int nr
= revs
->pending
.nr
;
1963 struct object_array_entry
*e
, *list
;
1965 e
= list
= revs
->pending
.objects
;
1966 revs
->pending
.nr
= 0;
1967 revs
->pending
.alloc
= 0;
1968 revs
->pending
.objects
= NULL
;
1970 struct commit
*commit
= handle_commit(revs
, e
->item
, e
->name
);
1972 if (!(commit
->object
.flags
& SEEN
)) {
1973 commit
->object
.flags
|= SEEN
;
1974 commit_list_insert_by_date(commit
, &revs
->commits
);
1984 if (limit_list(revs
) < 0)
1986 if (revs
->topo_order
)
1987 sort_in_topological_order(&revs
->commits
, revs
->lifo
);
1988 if (revs
->simplify_merges
)
1989 simplify_merges(revs
);
1990 if (revs
->children
.name
)
1995 enum rewrite_result
{
1997 rewrite_one_noparents
,
2001 static enum rewrite_result
rewrite_one(struct rev_info
*revs
, struct commit
**pp
)
2003 struct commit_list
*cache
= NULL
;
2006 struct commit
*p
= *pp
;
2008 if (add_parents_to_list(revs
, p
, &revs
->commits
, &cache
) < 0)
2009 return rewrite_one_error
;
2010 if (p
->parents
&& p
->parents
->next
)
2011 return rewrite_one_ok
;
2012 if (p
->object
.flags
& UNINTERESTING
)
2013 return rewrite_one_ok
;
2014 if (!(p
->object
.flags
& TREESAME
))
2015 return rewrite_one_ok
;
2017 return rewrite_one_noparents
;
2018 *pp
= p
->parents
->item
;
2022 static int rewrite_parents(struct rev_info
*revs
, struct commit
*commit
)
2024 struct commit_list
**pp
= &commit
->parents
;
2026 struct commit_list
*parent
= *pp
;
2027 switch (rewrite_one(revs
, &parent
->item
)) {
2028 case rewrite_one_ok
:
2030 case rewrite_one_noparents
:
2033 case rewrite_one_error
:
2038 remove_duplicate_parents(commit
);
2042 static int commit_match(struct commit
*commit
, struct rev_info
*opt
)
2044 if (!opt
->grep_filter
.pattern_list
&& !opt
->grep_filter
.header_list
)
2046 return grep_buffer(&opt
->grep_filter
,
2047 NULL
, /* we say nothing, not even filename */
2048 commit
->buffer
, strlen(commit
->buffer
));
2051 static inline int want_ancestry(struct rev_info
*revs
)
2053 return (revs
->rewrite_parents
|| revs
->children
.name
);
2056 enum commit_action
get_commit_action(struct rev_info
*revs
, struct commit
*commit
)
2058 if (commit
->object
.flags
& SHOWN
)
2059 return commit_ignore
;
2060 if (revs
->unpacked
&& has_sha1_pack(commit
->object
.sha1
))
2061 return commit_ignore
;
2064 if (commit
->object
.flags
& UNINTERESTING
)
2065 return commit_ignore
;
2066 if (revs
->min_age
!= -1 && (commit
->date
> revs
->min_age
))
2067 return commit_ignore
;
2068 if (revs
->min_parents
|| (revs
->max_parents
>= 0)) {
2070 struct commit_list
*p
;
2071 for (p
= commit
->parents
; p
; p
= p
->next
)
2073 if ((n
< revs
->min_parents
) ||
2074 ((revs
->max_parents
>= 0) && (n
> revs
->max_parents
)))
2075 return commit_ignore
;
2077 if (!commit_match(commit
, revs
))
2078 return commit_ignore
;
2079 if (revs
->prune
&& revs
->dense
) {
2080 /* Commit without changes? */
2081 if (commit
->object
.flags
& TREESAME
) {
2082 /* drop merges unless we want parenthood */
2083 if (!want_ancestry(revs
))
2084 return commit_ignore
;
2085 /* non-merge - always ignore it */
2086 if (!commit
->parents
|| !commit
->parents
->next
)
2087 return commit_ignore
;
2093 enum commit_action
simplify_commit(struct rev_info
*revs
, struct commit
*commit
)
2095 enum commit_action action
= get_commit_action(revs
, commit
);
2097 if (action
== commit_show
&&
2099 revs
->prune
&& revs
->dense
&& want_ancestry(revs
)) {
2100 if (rewrite_parents(revs
, commit
) < 0)
2101 return commit_error
;
2106 static struct commit
*get_revision_1(struct rev_info
*revs
)
2112 struct commit_list
*entry
= revs
->commits
;
2113 struct commit
*commit
= entry
->item
;
2115 revs
->commits
= entry
->next
;
2118 if (revs
->reflog_info
) {
2119 fake_reflog_parent(revs
->reflog_info
, commit
);
2120 commit
->object
.flags
&= ~(ADDED
| SEEN
| SHOWN
);
2124 * If we haven't done the list limiting, we need to look at
2125 * the parents here. We also need to do the date-based limiting
2126 * that we'd otherwise have done in limit_list().
2128 if (!revs
->limited
) {
2129 if (revs
->max_age
!= -1 &&
2130 (commit
->date
< revs
->max_age
))
2132 if (add_parents_to_list(revs
, commit
, &revs
->commits
, NULL
) < 0)
2133 die("Failed to traverse parents of commit %s",
2134 sha1_to_hex(commit
->object
.sha1
));
2137 switch (simplify_commit(revs
, commit
)) {
2141 die("Failed to simplify parents of commit %s",
2142 sha1_to_hex(commit
->object
.sha1
));
2146 } while (revs
->commits
);
2150 static void gc_boundary(struct object_array
*array
)
2152 unsigned nr
= array
->nr
;
2153 unsigned alloc
= array
->alloc
;
2154 struct object_array_entry
*objects
= array
->objects
;
2158 for (i
= j
= 0; i
< nr
; i
++) {
2159 if (objects
[i
].item
->flags
& SHOWN
)
2162 objects
[j
] = objects
[i
];
2165 for (i
= j
; i
< nr
; i
++)
2166 objects
[i
].item
= NULL
;
2171 static void create_boundary_commit_list(struct rev_info
*revs
)
2175 struct object_array
*array
= &revs
->boundary_commits
;
2176 struct object_array_entry
*objects
= array
->objects
;
2179 * If revs->commits is non-NULL at this point, an error occurred in
2180 * get_revision_1(). Ignore the error and continue printing the
2181 * boundary commits anyway. (This is what the code has always
2184 if (revs
->commits
) {
2185 free_commit_list(revs
->commits
);
2186 revs
->commits
= NULL
;
2190 * Put all of the actual boundary commits from revs->boundary_commits
2191 * into revs->commits
2193 for (i
= 0; i
< array
->nr
; i
++) {
2194 c
= (struct commit
*)(objects
[i
].item
);
2197 if (!(c
->object
.flags
& CHILD_SHOWN
))
2199 if (c
->object
.flags
& (SHOWN
| BOUNDARY
))
2201 c
->object
.flags
|= BOUNDARY
;
2202 commit_list_insert(c
, &revs
->commits
);
2206 * If revs->topo_order is set, sort the boundary commits
2207 * in topological order
2209 sort_in_topological_order(&revs
->commits
, revs
->lifo
);
2212 static struct commit
*get_revision_internal(struct rev_info
*revs
)
2214 struct commit
*c
= NULL
;
2215 struct commit_list
*l
;
2217 if (revs
->boundary
== 2) {
2219 * All of the normal commits have already been returned,
2220 * and we are now returning boundary commits.
2221 * create_boundary_commit_list() has populated
2222 * revs->commits with the remaining commits to return.
2224 c
= pop_commit(&revs
->commits
);
2226 c
->object
.flags
|= SHOWN
;
2231 * Now pick up what they want to give us
2233 c
= get_revision_1(revs
);
2235 while (0 < revs
->skip_count
) {
2237 c
= get_revision_1(revs
);
2244 * Check the max_count.
2246 switch (revs
->max_count
) {
2257 c
->object
.flags
|= SHOWN
;
2259 if (!revs
->boundary
) {
2265 * get_revision_1() runs out the commits, and
2266 * we are done computing the boundaries.
2267 * switch to boundary commits output mode.
2272 * Update revs->commits to contain the list of
2275 create_boundary_commit_list(revs
);
2277 return get_revision_internal(revs
);
2281 * boundary commits are the commits that are parents of the
2282 * ones we got from get_revision_1() but they themselves are
2283 * not returned from get_revision_1(). Before returning
2284 * 'c', we need to mark its parents that they could be boundaries.
2287 for (l
= c
->parents
; l
; l
= l
->next
) {
2289 p
= &(l
->item
->object
);
2290 if (p
->flags
& (CHILD_SHOWN
| SHOWN
))
2292 p
->flags
|= CHILD_SHOWN
;
2293 gc_boundary(&revs
->boundary_commits
);
2294 add_object_array(p
, NULL
, &revs
->boundary_commits
);
2300 struct commit
*get_revision(struct rev_info
*revs
)
2303 struct commit_list
*reversed
;
2305 if (revs
->reverse
) {
2307 while ((c
= get_revision_internal(revs
))) {
2308 commit_list_insert(c
, &reversed
);
2310 revs
->commits
= reversed
;
2312 revs
->reverse_output_stage
= 1;
2315 if (revs
->reverse_output_stage
)
2316 return pop_commit(&revs
->commits
);
2318 c
= get_revision_internal(revs
);
2319 if (c
&& revs
->graph
)
2320 graph_update(revs
->graph
, c
);
2324 char *get_revision_mark(const struct rev_info
*revs
, const struct commit
*commit
)
2326 if (commit
->object
.flags
& BOUNDARY
)
2328 else if (commit
->object
.flags
& UNINTERESTING
)
2330 else if (commit
->object
.flags
& PATCHSAME
)
2332 else if (!revs
|| revs
->left_right
) {
2333 if (commit
->object
.flags
& SYMMETRIC_LEFT
)
2337 } else if (revs
->graph
)
2339 else if (revs
->cherry_mark
)
2344 void put_revision_mark(const struct rev_info
*revs
, const struct commit
*commit
)
2346 char *mark
= get_revision_mark(revs
, commit
);
2349 fputs(mark
, stdout
);