11 #include "reflog-walk.h"
12 #include "patch-ids.h"
15 #include "rev-cache.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
)
136 if (revs
->no_walk
&& (obj
->flags
& UNINTERESTING
))
138 if (revs
->reflog_info
&& obj
->type
== OBJ_COMMIT
&&
139 add_reflog_for_walk(revs
->reflog_info
,
140 (struct commit
*)obj
, name
))
142 add_object_array_with_mode(obj
, name
, &revs
->pending
, mode
);
145 void add_pending_object(struct rev_info
*revs
, struct object
*obj
, const char *name
)
147 add_pending_object_with_mode(revs
, obj
, name
, S_IFINVALID
);
150 void add_head_to_pending(struct rev_info
*revs
)
152 unsigned char sha1
[20];
154 if (get_sha1("HEAD", sha1
))
156 obj
= parse_object(sha1
);
159 add_pending_object(revs
, obj
, "HEAD");
162 static struct object
*get_reference(struct rev_info
*revs
, const char *name
, const unsigned char *sha1
, unsigned int flags
)
164 struct object
*object
;
166 object
= parse_object(sha1
);
168 die("bad object %s", name
);
169 object
->flags
|= flags
;
173 static struct commit
*handle_commit(struct rev_info
*revs
, struct object
*object
, const char *name
)
175 unsigned long flags
= object
->flags
;
178 * Tag object? Look what it points to..
180 while (object
->type
== OBJ_TAG
) {
181 struct tag
*tag
= (struct tag
*) object
;
182 if (revs
->tag_objects
&& !(flags
& UNINTERESTING
))
183 add_pending_object(revs
, object
, tag
->tag
);
186 object
= parse_object(tag
->tagged
->sha1
);
188 if (flags
& UNINTERESTING
)
190 die("bad object %s", sha1_to_hex(tag
->tagged
->sha1
));
195 * Commit object? Just return it, we'll do all the complex
198 if (object
->type
== OBJ_COMMIT
) {
199 struct commit
*commit
= (struct commit
*)object
;
200 if (parse_commit(commit
) < 0)
201 die("unable to parse commit %s", name
);
202 if (flags
& UNINTERESTING
) {
203 commit
->object
.flags
|= UNINTERESTING
;
204 mark_parents_uninteresting(commit
);
207 if (revs
->show_source
&& !commit
->util
)
208 commit
->util
= (void *) name
;
213 * Tree object? Either mark it uninteresting, or add it
214 * to the list of objects to look at later..
216 if (object
->type
== OBJ_TREE
) {
217 struct tree
*tree
= (struct tree
*)object
;
218 if (!revs
->tree_objects
)
220 if (flags
& UNINTERESTING
) {
221 mark_tree_uninteresting(tree
);
224 add_pending_object(revs
, object
, "");
229 * Blob object? You know the drill by now..
231 if (object
->type
== OBJ_BLOB
) {
232 struct blob
*blob
= (struct blob
*)object
;
233 if (!revs
->blob_objects
)
235 if (flags
& UNINTERESTING
) {
236 mark_blob_uninteresting(blob
);
239 add_pending_object(revs
, object
, "");
242 die("%s is unknown object", name
);
245 static int everybody_uninteresting(struct commit_list
*orig
)
247 struct commit_list
*list
= orig
;
249 struct commit
*commit
= list
->item
;
251 if (commit
->object
.flags
& UNINTERESTING
)
259 * The goal is to get REV_TREE_NEW as the result only if the
260 * diff consists of all '+' (and no other changes), REV_TREE_OLD
261 * if the whole diff is removal of old data, and otherwise
262 * REV_TREE_DIFFERENT (of course if the trees are the same we
263 * want REV_TREE_SAME).
264 * That means that once we get to REV_TREE_DIFFERENT, we do not
265 * have to look any further.
267 static int tree_difference
= REV_TREE_SAME
;
269 static void file_add_remove(struct diff_options
*options
,
270 int addremove
, unsigned mode
,
271 const unsigned char *sha1
,
272 const char *fullpath
)
274 int diff
= addremove
== '+' ? REV_TREE_NEW
: REV_TREE_OLD
;
276 tree_difference
|= diff
;
277 if (tree_difference
== REV_TREE_DIFFERENT
)
278 DIFF_OPT_SET(options
, HAS_CHANGES
);
281 static void file_change(struct diff_options
*options
,
282 unsigned old_mode
, unsigned new_mode
,
283 const unsigned char *old_sha1
,
284 const unsigned char *new_sha1
,
285 const char *fullpath
)
287 tree_difference
= REV_TREE_DIFFERENT
;
288 DIFF_OPT_SET(options
, HAS_CHANGES
);
291 static int rev_compare_tree(struct rev_info
*revs
, struct commit
*parent
, struct commit
*commit
)
293 struct tree
*t1
= parent
->tree
;
294 struct tree
*t2
= commit
->tree
;
301 if (revs
->simplify_by_decoration
) {
303 * If we are simplifying by decoration, then the commit
304 * is worth showing if it has a tag pointing at it.
306 if (lookup_decoration(&name_decoration
, &commit
->object
))
307 return REV_TREE_DIFFERENT
;
309 * A commit that is not pointed by a tag is uninteresting
310 * if we are not limited by path. This means that you will
311 * see the usual "commits that touch the paths" plus any
312 * tagged commit by specifying both --simplify-by-decoration
315 if (!revs
->prune_data
)
316 return REV_TREE_SAME
;
319 tree_difference
= REV_TREE_SAME
;
320 DIFF_OPT_CLR(&revs
->pruning
, HAS_CHANGES
);
321 if (diff_tree_sha1(t1
->object
.sha1
, t2
->object
.sha1
, "",
323 return REV_TREE_DIFFERENT
;
324 return tree_difference
;
327 static int rev_same_tree_as_empty(struct rev_info
*revs
, struct commit
*commit
)
332 struct tree_desc empty
, real
;
333 struct tree
*t1
= commit
->tree
;
338 tree
= read_object_with_reference(t1
->object
.sha1
, tree_type
, &size
, NULL
);
341 init_tree_desc(&real
, tree
, size
);
342 init_tree_desc(&empty
, "", 0);
344 tree_difference
= REV_TREE_SAME
;
345 DIFF_OPT_CLR(&revs
->pruning
, HAS_CHANGES
);
346 retval
= diff_tree(&empty
, &real
, "", &revs
->pruning
);
349 return retval
>= 0 && (tree_difference
== REV_TREE_SAME
);
352 static void try_to_simplify_commit(struct rev_info
*revs
, struct commit
*commit
)
354 struct commit_list
**pp
, *parent
;
355 int tree_changed
= 0, tree_same
= 0;
358 * If we don't do pruning, everything is interesting
366 if (!commit
->parents
) {
367 if (rev_same_tree_as_empty(revs
, commit
))
368 commit
->object
.flags
|= TREESAME
;
373 * Normal non-merge commit? If we don't want to make the
374 * history dense, we consider it always to be a change..
376 if (!revs
->dense
&& !commit
->parents
->next
)
379 pp
= &commit
->parents
;
380 while ((parent
= *pp
) != NULL
) {
381 struct commit
*p
= parent
->item
;
383 if (parse_commit(p
) < 0)
384 die("cannot simplify commit %s (because of %s)",
385 sha1_to_hex(commit
->object
.sha1
),
386 sha1_to_hex(p
->object
.sha1
));
387 switch (rev_compare_tree(revs
, p
, commit
)) {
390 if (!revs
->simplify_history
|| (p
->object
.flags
& UNINTERESTING
)) {
391 /* Even if a merge with an uninteresting
392 * side branch brought the entire change
393 * we are interested in, we do not want
394 * to lose the other branches of this
395 * merge, so we just keep going.
401 commit
->parents
= parent
;
402 commit
->object
.flags
|= TREESAME
;
406 if (revs
->remove_empty_trees
&&
407 rev_same_tree_as_empty(revs
, p
)) {
408 /* We are adding all the specified
409 * paths from this parent, so the
410 * history beyond this parent is not
411 * interesting. Remove its parents
412 * (they are grandparents for us).
413 * IOW, we pretend this parent is a
416 if (parse_commit(p
) < 0)
417 die("cannot simplify commit %s (invalid %s)",
418 sha1_to_hex(commit
->object
.sha1
),
419 sha1_to_hex(p
->object
.sha1
));
424 case REV_TREE_DIFFERENT
:
429 die("bad tree compare for commit %s", sha1_to_hex(commit
->object
.sha1
));
431 if (tree_changed
&& !tree_same
)
433 commit
->object
.flags
|= TREESAME
;
436 void insert_by_date_cached(struct commit
*p
, struct commit_list
**head
,
437 struct commit_list
*cached_base
, struct commit_list
**cache
)
439 struct commit_list
*new_entry
;
441 if (cached_base
&& p
->date
< cached_base
->item
->date
)
442 new_entry
= insert_by_date(p
, &cached_base
->next
);
444 new_entry
= insert_by_date(p
, head
);
446 if (cache
&& (!*cache
|| p
->date
< (*cache
)->item
->date
))
450 static int add_parents_to_list(struct rev_info
*revs
, struct commit
*commit
,
451 struct commit_list
**list
, struct commit_list
**cache_ptr
)
453 struct commit_list
*parent
= commit
->parents
;
455 struct commit_list
*cached_base
= cache_ptr
? *cache_ptr
: NULL
;
457 if (commit
->object
.flags
& ADDED
)
459 commit
->object
.flags
|= ADDED
;
462 * If the commit is uninteresting, don't try to
463 * prune parents - we want the maximal uninteresting
466 * Normally we haven't parsed the parent
467 * yet, so we won't have a parent of a parent
468 * here. However, it may turn out that we've
469 * reached this commit some other way (where it
470 * wasn't uninteresting), in which case we need
471 * to mark its parents recursively too..
473 if (commit
->object
.flags
& UNINTERESTING
) {
475 struct commit
*p
= parent
->item
;
476 parent
= parent
->next
;
478 p
->object
.flags
|= UNINTERESTING
;
479 if (parse_commit(p
) < 0)
482 mark_parents_uninteresting(p
);
483 if (p
->object
.flags
& SEEN
)
485 p
->object
.flags
|= SEEN
;
486 insert_by_date_cached(p
, list
, cached_base
, cache_ptr
);
492 * Ok, the commit wasn't uninteresting. Try to
493 * simplify the commit history and find the parent
494 * that has no differences in the path set if one exists.
496 try_to_simplify_commit(revs
, commit
);
501 left_flag
= (commit
->object
.flags
& SYMMETRIC_LEFT
);
503 for (parent
= commit
->parents
; parent
; parent
= parent
->next
) {
504 struct commit
*p
= parent
->item
;
506 if (parse_commit(p
) < 0)
508 if (revs
->show_source
&& !p
->util
)
509 p
->util
= commit
->util
;
510 p
->object
.flags
|= left_flag
;
511 if (!(p
->object
.flags
& SEEN
)) {
512 p
->object
.flags
|= SEEN
;
513 insert_by_date_cached(p
, list
, cached_base
, cache_ptr
);
515 if (revs
->first_parent_only
)
521 static void cherry_pick_list(struct commit_list
*list
, struct rev_info
*revs
)
523 struct commit_list
*p
;
524 int left_count
= 0, right_count
= 0;
526 struct patch_ids ids
;
528 /* First count the commits on the left and on the right */
529 for (p
= list
; p
; p
= p
->next
) {
530 struct commit
*commit
= p
->item
;
531 unsigned flags
= commit
->object
.flags
;
532 if (flags
& BOUNDARY
)
534 else if (flags
& SYMMETRIC_LEFT
)
540 left_first
= left_count
< right_count
;
541 init_patch_ids(&ids
);
542 if (revs
->diffopt
.nr_paths
) {
543 ids
.diffopts
.nr_paths
= revs
->diffopt
.nr_paths
;
544 ids
.diffopts
.paths
= revs
->diffopt
.paths
;
545 ids
.diffopts
.pathlens
= revs
->diffopt
.pathlens
;
548 /* Compute patch-ids for one side */
549 for (p
= list
; p
; p
= p
->next
) {
550 struct commit
*commit
= p
->item
;
551 unsigned flags
= commit
->object
.flags
;
553 if (flags
& BOUNDARY
)
556 * If we have fewer left, left_first is set and we omit
557 * commits on the right branch in this loop. If we have
558 * fewer right, we skip the left ones.
560 if (left_first
!= !!(flags
& SYMMETRIC_LEFT
))
562 commit
->util
= add_commit_patch_id(commit
, &ids
);
565 /* Check the other side */
566 for (p
= list
; p
; p
= p
->next
) {
567 struct commit
*commit
= p
->item
;
569 unsigned flags
= commit
->object
.flags
;
571 if (flags
& BOUNDARY
)
574 * If we have fewer left, left_first is set and we omit
575 * commits on the left branch in this loop.
577 if (left_first
== !!(flags
& SYMMETRIC_LEFT
))
581 * Have we seen the same patch id?
583 id
= has_commit_patch_id(commit
, &ids
);
587 commit
->object
.flags
|= SHOWN
;
590 /* Now check the original side for seen ones */
591 for (p
= list
; p
; p
= p
->next
) {
592 struct commit
*commit
= p
->item
;
593 struct patch_id
*ent
;
599 commit
->object
.flags
|= SHOWN
;
603 free_patch_ids(&ids
);
606 /* How many extra uninteresting commits we want to see.. */
609 static int still_interesting(struct commit_list
*src
, unsigned long date
, int slop
)
612 * No source list at all? We're definitely done..
618 * Does the destination list contain entries with a date
619 * before the source list? Definitely _not_ done.
621 if (date
< src
->item
->date
)
625 * Does the source list still have interesting commits in
626 * it? Definitely not done..
628 if (!everybody_uninteresting(src
))
631 /* Ok, we're closing in.. */
635 static int limit_list(struct rev_info
*revs
)
638 unsigned long date
= ~0ul;
639 struct commit_list
*list
= revs
->commits
;
640 struct commit_list
*newlist
= NULL
;
641 struct commit_list
**p
= &newlist
;
642 unsigned char *cache_sha1
;
646 struct commit_list
*entry
= list
;
647 struct commit
*commit
= list
->item
;
648 struct object
*obj
= &commit
->object
;
649 show_early_output_fn_t show
;
654 if (revs
->max_age
!= -1 && (commit
->date
< revs
->max_age
))
655 obj
->flags
|= UNINTERESTING
;
657 /* rev-cache to the rescue!!! */
659 if (!revs
->dont_cache_me
&& !(obj
->flags
& ADDED
)) {
660 cache_sha1
= get_cache_slice(commit
);
662 if (traverse_cache_slice(revs
, cache_sha1
, commit
, &date
, &slop
, &p
, &list
) < 0)
670 if (add_parents_to_list(revs
, commit
, &list
, NULL
) < 0)
672 if (obj
->flags
& UNINTERESTING
) {
673 mark_parents_uninteresting(commit
); /* ME: why? */
675 p
= &commit_list_insert(commit
, p
)->next
;
676 slop
= still_interesting(list
, date
, slop
);
679 /* If showing all, add the whole pending list to the end */
684 if (revs
->min_age
!= -1 && (commit
->date
> revs
->min_age
))
687 p
= &commit_list_insert(commit
, p
)->next
;
690 show
= show_early_output
;
695 show_early_output
= NULL
;
697 if (revs
->cherry_pick
)
698 cherry_pick_list(newlist
, revs
);
700 revs
->commits
= newlist
;
706 int warned_bad_reflog
;
707 struct rev_info
*all_revs
;
708 const char *name_for_errormsg
;
711 static int handle_one_ref(const char *path
, const unsigned char *sha1
, int flag
, void *cb_data
)
713 struct all_refs_cb
*cb
= cb_data
;
714 struct object
*object
= get_reference(cb
->all_revs
, path
, sha1
,
716 add_pending_object(cb
->all_revs
, object
, path
);
720 static void handle_refs(struct rev_info
*revs
, unsigned flags
,
721 int (*for_each
)(each_ref_fn
, void *))
723 struct all_refs_cb cb
;
725 cb
.all_flags
= flags
;
726 for_each(handle_one_ref
, &cb
);
729 static void handle_one_reflog_commit(unsigned char *sha1
, void *cb_data
)
731 struct all_refs_cb
*cb
= cb_data
;
732 if (!is_null_sha1(sha1
)) {
733 struct object
*o
= parse_object(sha1
);
735 o
->flags
|= cb
->all_flags
;
736 add_pending_object(cb
->all_revs
, o
, "");
738 else if (!cb
->warned_bad_reflog
) {
739 warning("reflog of '%s' references pruned commits",
740 cb
->name_for_errormsg
);
741 cb
->warned_bad_reflog
= 1;
746 static int handle_one_reflog_ent(unsigned char *osha1
, unsigned char *nsha1
,
747 const char *email
, unsigned long timestamp
, int tz
,
748 const char *message
, void *cb_data
)
750 handle_one_reflog_commit(osha1
, cb_data
);
751 handle_one_reflog_commit(nsha1
, cb_data
);
755 static int handle_one_reflog(const char *path
, const unsigned char *sha1
, int flag
, void *cb_data
)
757 struct all_refs_cb
*cb
= cb_data
;
758 cb
->warned_bad_reflog
= 0;
759 cb
->name_for_errormsg
= path
;
760 for_each_reflog_ent(path
, handle_one_reflog_ent
, cb_data
);
764 static void handle_reflog(struct rev_info
*revs
, unsigned flags
)
766 struct all_refs_cb cb
;
768 cb
.all_flags
= flags
;
769 for_each_reflog(handle_one_reflog
, &cb
);
772 static int add_parents_only(struct rev_info
*revs
, const char *arg
, int flags
)
774 unsigned char sha1
[20];
776 struct commit
*commit
;
777 struct commit_list
*parents
;
780 flags
^= UNINTERESTING
;
783 if (get_sha1(arg
, sha1
))
786 it
= get_reference(revs
, arg
, sha1
, 0);
787 if (it
->type
!= OBJ_TAG
)
789 if (!((struct tag
*)it
)->tagged
)
791 hashcpy(sha1
, ((struct tag
*)it
)->tagged
->sha1
);
793 if (it
->type
!= OBJ_COMMIT
)
795 commit
= (struct commit
*)it
;
796 for (parents
= commit
->parents
; parents
; parents
= parents
->next
) {
797 it
= &parents
->item
->object
;
799 add_pending_object(revs
, it
, arg
);
804 void init_revisions(struct rev_info
*revs
, const char *prefix
)
806 memset(revs
, 0, sizeof(*revs
));
808 revs
->abbrev
= DEFAULT_ABBREV
;
809 revs
->ignore_merges
= 1;
810 revs
->simplify_history
= 1;
811 DIFF_OPT_SET(&revs
->pruning
, RECURSIVE
);
812 DIFF_OPT_SET(&revs
->pruning
, QUIET
);
813 revs
->pruning
.add_remove
= file_add_remove
;
814 revs
->pruning
.change
= file_change
;
817 revs
->prefix
= prefix
;
820 revs
->skip_count
= -1;
821 revs
->max_count
= -1;
823 revs
->commit_format
= CMIT_FMT_DEFAULT
;
825 revs
->grep_filter
.status_only
= 1;
826 revs
->grep_filter
.pattern_tail
= &(revs
->grep_filter
.pattern_list
);
827 revs
->grep_filter
.regflags
= REG_NEWLINE
;
829 diff_setup(&revs
->diffopt
);
830 if (prefix
&& !revs
->diffopt
.prefix
) {
831 revs
->diffopt
.prefix
= prefix
;
832 revs
->diffopt
.prefix_length
= strlen(prefix
);
835 init_rev_cache_info(&revs
->rev_cache_info
);
838 static void add_pending_commit_list(struct rev_info
*revs
,
839 struct commit_list
*commit_list
,
842 while (commit_list
) {
843 struct object
*object
= &commit_list
->item
->object
;
844 object
->flags
|= flags
;
845 add_pending_object(revs
, object
, sha1_to_hex(object
->sha1
));
846 commit_list
= commit_list
->next
;
850 static void prepare_show_merge(struct rev_info
*revs
)
852 struct commit_list
*bases
;
853 struct commit
*head
, *other
;
854 unsigned char sha1
[20];
855 const char **prune
= NULL
;
856 int i
, prune_num
= 1; /* counting terminating NULL */
858 if (get_sha1("HEAD", sha1
) || !(head
= lookup_commit(sha1
)))
859 die("--merge without HEAD?");
860 if (get_sha1("MERGE_HEAD", sha1
) || !(other
= lookup_commit(sha1
)))
861 die("--merge without MERGE_HEAD?");
862 add_pending_object(revs
, &head
->object
, "HEAD");
863 add_pending_object(revs
, &other
->object
, "MERGE_HEAD");
864 bases
= get_merge_bases(head
, other
, 1);
865 add_pending_commit_list(revs
, bases
, UNINTERESTING
);
866 free_commit_list(bases
);
867 head
->object
.flags
|= SYMMETRIC_LEFT
;
871 for (i
= 0; i
< active_nr
; i
++) {
872 struct cache_entry
*ce
= active_cache
[i
];
875 if (ce_path_match(ce
, revs
->prune_data
)) {
877 prune
= xrealloc(prune
, sizeof(*prune
) * prune_num
);
878 prune
[prune_num
-2] = ce
->name
;
879 prune
[prune_num
-1] = NULL
;
881 while ((i
+1 < active_nr
) &&
882 ce_same_name(ce
, active_cache
[i
+1]))
885 revs
->prune_data
= prune
;
889 int handle_revision_arg(const char *arg
, struct rev_info
*revs
,
891 int cant_be_filename
)
895 struct object
*object
;
896 unsigned char sha1
[20];
899 dotdot
= strstr(arg
, "..");
901 unsigned char from_sha1
[20];
902 const char *next
= dotdot
+ 2;
903 const char *this = arg
;
904 int symmetric
= *next
== '.';
905 unsigned int flags_exclude
= flags
^ UNINTERESTING
;
914 if (!get_sha1(this, from_sha1
) &&
915 !get_sha1(next
, sha1
)) {
916 struct commit
*a
, *b
;
917 struct commit_list
*exclude
;
919 a
= lookup_commit_reference(from_sha1
);
920 b
= lookup_commit_reference(sha1
);
923 "Invalid symmetric difference expression %s...%s" :
924 "Invalid revision range %s..%s",
928 if (!cant_be_filename
) {
930 verify_non_filename(revs
->prefix
, arg
);
934 exclude
= get_merge_bases(a
, b
, 1);
935 add_pending_commit_list(revs
, exclude
,
937 free_commit_list(exclude
);
938 a
->object
.flags
|= flags
| SYMMETRIC_LEFT
;
940 a
->object
.flags
|= flags_exclude
;
941 b
->object
.flags
|= flags
;
942 add_pending_object(revs
, &a
->object
, this);
943 add_pending_object(revs
, &b
->object
, next
);
948 dotdot
= strstr(arg
, "^@");
949 if (dotdot
&& !dotdot
[2]) {
951 if (add_parents_only(revs
, arg
, flags
))
955 dotdot
= strstr(arg
, "^!");
956 if (dotdot
&& !dotdot
[2]) {
958 if (!add_parents_only(revs
, arg
, flags
^ UNINTERESTING
))
964 local_flags
= UNINTERESTING
;
967 if (get_sha1_with_mode(arg
, sha1
, &mode
))
969 if (!cant_be_filename
)
970 verify_non_filename(revs
->prefix
, arg
);
971 object
= get_reference(revs
, arg
, sha1
, flags
^ local_flags
);
972 add_pending_object_with_mode(revs
, object
, arg
, mode
);
976 void read_revisions_from_stdin(struct rev_info
*revs
)
980 while (fgets(line
, sizeof(line
), stdin
) != NULL
) {
981 int len
= strlen(line
);
982 if (len
&& line
[len
- 1] == '\n')
987 die("options not supported in --stdin mode");
988 if (handle_revision_arg(line
, revs
, 0, 1))
989 die("bad revision '%s'", line
);
993 static void add_grep(struct rev_info
*revs
, const char *ptn
, enum grep_pat_token what
)
995 append_grep_pattern(&revs
->grep_filter
, ptn
, "command line", 0, what
);
998 static void add_header_grep(struct rev_info
*revs
, enum grep_header_field field
, const char *pattern
)
1000 append_header_grep_pattern(&revs
->grep_filter
, field
, pattern
);
1003 static void add_message_grep(struct rev_info
*revs
, const char *pattern
)
1005 add_grep(revs
, pattern
, GREP_PATTERN_BODY
);
1008 static int handle_revision_opt(struct rev_info
*revs
, int argc
, const char **argv
,
1009 int *unkc
, const char **unkv
)
1011 const char *arg
= argv
[0];
1013 /* pseudo revision arguments */
1014 if (!strcmp(arg
, "--all") || !strcmp(arg
, "--branches") ||
1015 !strcmp(arg
, "--tags") || !strcmp(arg
, "--remotes") ||
1016 !strcmp(arg
, "--reflog") || !strcmp(arg
, "--not") ||
1017 !strcmp(arg
, "--no-walk") || !strcmp(arg
, "--do-walk"))
1019 unkv
[(*unkc
)++] = arg
;
1023 if (!prefixcmp(arg
, "--max-count=")) {
1024 revs
->max_count
= atoi(arg
+ 12);
1025 } else if (!prefixcmp(arg
, "--skip=")) {
1026 revs
->skip_count
= atoi(arg
+ 7);
1027 } else if ((*arg
== '-') && isdigit(arg
[1])) {
1028 /* accept -<digit>, like traditional "head" */
1029 revs
->max_count
= atoi(arg
+ 1);
1030 } else if (!strcmp(arg
, "-n")) {
1032 return error("-n requires an argument");
1033 revs
->max_count
= atoi(argv
[1]);
1035 } else if (!prefixcmp(arg
, "-n")) {
1036 revs
->max_count
= atoi(arg
+ 2);
1037 } else if (!prefixcmp(arg
, "--max-age=")) {
1038 revs
->max_age
= atoi(arg
+ 10);
1039 } else if (!prefixcmp(arg
, "--since=")) {
1040 revs
->max_age
= approxidate(arg
+ 8);
1041 } else if (!prefixcmp(arg
, "--after=")) {
1042 revs
->max_age
= approxidate(arg
+ 8);
1043 } else if (!prefixcmp(arg
, "--min-age=")) {
1044 revs
->min_age
= atoi(arg
+ 10);
1045 } else if (!prefixcmp(arg
, "--before=")) {
1046 revs
->min_age
= approxidate(arg
+ 9);
1047 } else if (!prefixcmp(arg
, "--until=")) {
1048 revs
->min_age
= approxidate(arg
+ 8);
1049 } else if (!strcmp(arg
, "--first-parent")) {
1050 revs
->first_parent_only
= 1;
1051 } else if (!strcmp(arg
, "-g") || !strcmp(arg
, "--walk-reflogs")) {
1052 init_reflog_walk(&revs
->reflog_info
);
1053 } else if (!strcmp(arg
, "--default")) {
1055 return error("bad --default argument");
1056 revs
->def
= argv
[1];
1058 } else if (!strcmp(arg
, "--merge")) {
1059 revs
->show_merge
= 1;
1060 } else if (!strcmp(arg
, "--topo-order")) {
1062 revs
->topo_order
= 1;
1063 } else if (!strcmp(arg
, "--simplify-merges")) {
1064 revs
->simplify_merges
= 1;
1065 revs
->rewrite_parents
= 1;
1066 revs
->simplify_history
= 0;
1068 } else if (!strcmp(arg
, "--simplify-by-decoration")) {
1069 revs
->simplify_merges
= 1;
1070 revs
->rewrite_parents
= 1;
1071 revs
->simplify_history
= 0;
1072 revs
->simplify_by_decoration
= 1;
1075 load_ref_decorations(DECORATE_SHORT_REFS
);
1076 } else if (!strcmp(arg
, "--date-order")) {
1078 revs
->topo_order
= 1;
1079 } else if (!prefixcmp(arg
, "--early-output")) {
1083 count
= atoi(arg
+15);
1086 revs
->topo_order
= 1;
1087 revs
->early_output
= count
;
1089 } else if (!strcmp(arg
, "--parents")) {
1090 revs
->rewrite_parents
= 1;
1091 revs
->print_parents
= 1;
1092 } else if (!strcmp(arg
, "--dense")) {
1094 } else if (!strcmp(arg
, "--sparse")) {
1096 } else if (!strcmp(arg
, "--show-all")) {
1098 } else if (!strcmp(arg
, "--remove-empty")) {
1099 revs
->remove_empty_trees
= 1;
1100 } else if (!strcmp(arg
, "--merges")) {
1101 revs
->merges_only
= 1;
1102 } else if (!strcmp(arg
, "--no-merges")) {
1103 revs
->no_merges
= 1;
1104 } else if (!strcmp(arg
, "--boundary")) {
1106 } else if (!strcmp(arg
, "--left-right")) {
1107 revs
->left_right
= 1;
1108 } else if (!strcmp(arg
, "--cherry-pick")) {
1109 revs
->cherry_pick
= 1;
1111 } else if (!strcmp(arg
, "--objects")) {
1112 revs
->tag_objects
= 1;
1113 revs
->tree_objects
= 1;
1114 revs
->blob_objects
= 1;
1115 } else if (!strcmp(arg
, "--objects-edge")) {
1116 revs
->tag_objects
= 1;
1117 revs
->tree_objects
= 1;
1118 revs
->blob_objects
= 1;
1119 revs
->edge_hint
= 1;
1120 } else if (!strcmp(arg
, "--unpacked")) {
1122 } else if (!prefixcmp(arg
, "--unpacked=")) {
1123 die("--unpacked=<packfile> no longer supported.");
1124 } else if (!strcmp(arg
, "-r")) {
1126 DIFF_OPT_SET(&revs
->diffopt
, RECURSIVE
);
1127 } else if (!strcmp(arg
, "-t")) {
1129 DIFF_OPT_SET(&revs
->diffopt
, RECURSIVE
);
1130 DIFF_OPT_SET(&revs
->diffopt
, TREE_IN_RECURSIVE
);
1131 } else if (!strcmp(arg
, "-m")) {
1132 revs
->ignore_merges
= 0;
1133 } else if (!strcmp(arg
, "-c")) {
1135 revs
->dense_combined_merges
= 0;
1136 revs
->combine_merges
= 1;
1137 } else if (!strcmp(arg
, "--cc")) {
1139 revs
->dense_combined_merges
= 1;
1140 revs
->combine_merges
= 1;
1141 } else if (!strcmp(arg
, "-v")) {
1142 revs
->verbose_header
= 1;
1143 } else if (!strcmp(arg
, "--pretty")) {
1144 revs
->verbose_header
= 1;
1145 get_commit_format(arg
+8, revs
);
1146 } else if (!prefixcmp(arg
, "--pretty=") || !prefixcmp(arg
, "--format=")) {
1147 revs
->verbose_header
= 1;
1148 get_commit_format(arg
+9, revs
);
1149 } else if (!strcmp(arg
, "--oneline")) {
1150 revs
->verbose_header
= 1;
1151 get_commit_format("oneline", revs
);
1152 revs
->abbrev_commit
= 1;
1153 } else if (!strcmp(arg
, "--graph")) {
1154 revs
->topo_order
= 1;
1155 revs
->rewrite_parents
= 1;
1156 revs
->graph
= graph_init(revs
);
1157 } else if (!strcmp(arg
, "--root")) {
1158 revs
->show_root_diff
= 1;
1159 } else if (!strcmp(arg
, "--no-commit-id")) {
1160 revs
->no_commit_id
= 1;
1161 } else if (!strcmp(arg
, "--always")) {
1162 revs
->always_show_header
= 1;
1163 } else if (!strcmp(arg
, "--no-abbrev")) {
1165 } else if (!strcmp(arg
, "--abbrev")) {
1166 revs
->abbrev
= DEFAULT_ABBREV
;
1167 } else if (!prefixcmp(arg
, "--abbrev=")) {
1168 revs
->abbrev
= strtoul(arg
+ 9, NULL
, 10);
1169 if (revs
->abbrev
< MINIMUM_ABBREV
)
1170 revs
->abbrev
= MINIMUM_ABBREV
;
1171 else if (revs
->abbrev
> 40)
1173 } else if (!strcmp(arg
, "--abbrev-commit")) {
1174 revs
->abbrev_commit
= 1;
1175 } else if (!strcmp(arg
, "--full-diff")) {
1177 revs
->full_diff
= 1;
1178 } else if (!strcmp(arg
, "--full-history")) {
1179 revs
->simplify_history
= 0;
1180 } else if (!strcmp(arg
, "--relative-date")) {
1181 revs
->date_mode
= DATE_RELATIVE
;
1182 } else if (!strncmp(arg
, "--date=", 7)) {
1183 revs
->date_mode
= parse_date_format(arg
+ 7);
1184 } else if (!strcmp(arg
, "--log-size")) {
1185 revs
->show_log_size
= 1;
1188 * Grepping the commit log
1190 else if (!prefixcmp(arg
, "--author=")) {
1191 add_header_grep(revs
, GREP_HEADER_AUTHOR
, arg
+9);
1192 } else if (!prefixcmp(arg
, "--committer=")) {
1193 add_header_grep(revs
, GREP_HEADER_COMMITTER
, arg
+12);
1194 } else if (!prefixcmp(arg
, "--grep=")) {
1195 add_message_grep(revs
, arg
+7);
1196 } else if (!strcmp(arg
, "--extended-regexp") || !strcmp(arg
, "-E")) {
1197 revs
->grep_filter
.regflags
|= REG_EXTENDED
;
1198 } else if (!strcmp(arg
, "--regexp-ignore-case") || !strcmp(arg
, "-i")) {
1199 revs
->grep_filter
.regflags
|= REG_ICASE
;
1200 } else if (!strcmp(arg
, "--fixed-strings") || !strcmp(arg
, "-F")) {
1201 revs
->grep_filter
.fixed
= 1;
1202 } else if (!strcmp(arg
, "--all-match")) {
1203 revs
->grep_filter
.all_match
= 1;
1204 } else if (!prefixcmp(arg
, "--encoding=")) {
1206 if (strcmp(arg
, "none"))
1207 git_log_output_encoding
= xstrdup(arg
);
1209 git_log_output_encoding
= "";
1210 } else if (!strcmp(arg
, "--reverse")) {
1212 } else if (!strcmp(arg
, "--children")) {
1213 revs
->children
.name
= "children";
1216 int opts
= diff_opt_parse(&revs
->diffopt
, argv
, argc
);
1218 unkv
[(*unkc
)++] = arg
;
1225 void parse_revision_opt(struct rev_info
*revs
, struct parse_opt_ctx_t
*ctx
,
1226 const struct option
*options
,
1227 const char * const usagestr
[])
1229 int n
= handle_revision_opt(revs
, ctx
->argc
, ctx
->argv
,
1230 &ctx
->cpidx
, ctx
->out
);
1232 error("unknown option `%s'", ctx
->argv
[0]);
1233 usage_with_options(usagestr
, options
);
1240 * Parse revision information, filling in the "rev_info" structure,
1241 * and removing the used arguments from the argument list.
1243 * Returns the number of arguments left that weren't recognized
1244 * (which are also moved to the head of the argument list)
1246 int setup_revisions(int argc
, const char **argv
, struct rev_info
*revs
, const char *def
)
1248 int i
, flags
, left
, seen_dashdash
;
1250 /* First, search for "--" */
1252 for (i
= 1; i
< argc
; i
++) {
1253 const char *arg
= argv
[i
];
1254 if (strcmp(arg
, "--"))
1259 revs
->prune_data
= get_pathspec(revs
->prefix
, argv
+ i
+ 1);
1264 /* Second, deal with arguments and options */
1266 for (left
= i
= 1; i
< argc
; i
++) {
1267 const char *arg
= argv
[i
];
1271 if (!strcmp(arg
, "--all")) {
1272 handle_refs(revs
, flags
, for_each_ref
);
1273 handle_refs(revs
, flags
, head_ref
);
1276 if (!strcmp(arg
, "--branches")) {
1277 handle_refs(revs
, flags
, for_each_branch_ref
);
1280 if (!strcmp(arg
, "--tags")) {
1281 handle_refs(revs
, flags
, for_each_tag_ref
);
1284 if (!strcmp(arg
, "--remotes")) {
1285 handle_refs(revs
, flags
, for_each_remote_ref
);
1288 if (!strcmp(arg
, "--reflog")) {
1289 handle_reflog(revs
, flags
);
1292 if (!strcmp(arg
, "--not")) {
1293 flags
^= UNINTERESTING
;
1296 if (!strcmp(arg
, "--no-walk")) {
1300 if (!strcmp(arg
, "--do-walk")) {
1305 opts
= handle_revision_opt(revs
, argc
- i
, argv
+ i
, &left
, argv
);
1315 if (handle_revision_arg(arg
, revs
, flags
, seen_dashdash
)) {
1317 if (seen_dashdash
|| *arg
== '^')
1318 die("bad revision '%s'", arg
);
1320 /* If we didn't have a "--":
1321 * (1) all filenames must exist;
1322 * (2) all rev-args must not be interpretable
1323 * as a valid filename.
1324 * but the latter we have checked in the main loop.
1326 for (j
= i
; j
< argc
; j
++)
1327 verify_filename(revs
->prefix
, argv
[j
]);
1329 revs
->prune_data
= get_pathspec(revs
->prefix
,
1335 if (revs
->def
== NULL
)
1337 if (revs
->show_merge
)
1338 prepare_show_merge(revs
);
1339 if (revs
->def
&& !revs
->pending
.nr
) {
1340 unsigned char sha1
[20];
1341 struct object
*object
;
1343 if (get_sha1_with_mode(revs
->def
, sha1
, &mode
))
1344 die("bad default revision '%s'", revs
->def
);
1345 object
= get_reference(revs
, revs
->def
, sha1
, 0);
1346 add_pending_object_with_mode(revs
, object
, revs
->def
, mode
);
1349 /* Did the user ask for any diff output? Run the diff! */
1350 if (revs
->diffopt
.output_format
& ~DIFF_FORMAT_NO_OUTPUT
)
1353 /* Pickaxe, diff-filter and rename following need diffs */
1354 if (revs
->diffopt
.pickaxe
||
1355 revs
->diffopt
.filter
||
1356 DIFF_OPT_TST(&revs
->diffopt
, FOLLOW_RENAMES
))
1359 if (revs
->topo_order
)
1362 if (revs
->prune_data
) {
1363 diff_tree_setup_paths(revs
->prune_data
, &revs
->pruning
);
1364 /* Can't prune commits with rename following: the paths change.. */
1365 if (!DIFF_OPT_TST(&revs
->diffopt
, FOLLOW_RENAMES
))
1367 if (!revs
->full_diff
)
1368 diff_tree_setup_paths(revs
->prune_data
, &revs
->diffopt
);
1370 if (revs
->combine_merges
) {
1371 revs
->ignore_merges
= 0;
1372 if (revs
->dense_combined_merges
&& !revs
->diffopt
.output_format
)
1373 revs
->diffopt
.output_format
= DIFF_FORMAT_PATCH
;
1375 revs
->diffopt
.abbrev
= revs
->abbrev
;
1376 if (diff_setup_done(&revs
->diffopt
) < 0)
1377 die("diff_setup_done failed");
1379 compile_grep_patterns(&revs
->grep_filter
);
1381 if (revs
->reverse
&& revs
->reflog_info
)
1382 die("cannot combine --reverse with --walk-reflogs");
1383 if (revs
->rewrite_parents
&& revs
->children
.name
)
1384 die("cannot combine --parents and --children");
1387 * Limitations on the graph functionality
1389 if (revs
->reverse
&& revs
->graph
)
1390 die("cannot combine --reverse with --graph");
1392 if (revs
->reflog_info
&& revs
->graph
)
1393 die("cannot combine --walk-reflogs with --graph");
1395 /* limits on caching
1396 * todo: implement this functionality */
1397 if (revs
->prune
|| revs
->diff
)
1398 revs
->dont_cache_me
= 1;
1403 static void add_child(struct rev_info
*revs
, struct commit
*parent
, struct commit
*child
)
1405 struct commit_list
*l
= xcalloc(1, sizeof(*l
));
1408 l
->next
= add_decoration(&revs
->children
, &parent
->object
, l
);
1411 static int remove_duplicate_parents(struct commit
*commit
)
1413 struct commit_list
**pp
, *p
;
1414 int surviving_parents
;
1416 /* Examine existing parents while marking ones we have seen... */
1417 pp
= &commit
->parents
;
1418 while ((p
= *pp
) != NULL
) {
1419 struct commit
*parent
= p
->item
;
1420 if (parent
->object
.flags
& TMP_MARK
) {
1424 parent
->object
.flags
|= TMP_MARK
;
1427 /* count them while clearing the temporary mark */
1428 surviving_parents
= 0;
1429 for (p
= commit
->parents
; p
; p
= p
->next
) {
1430 p
->item
->object
.flags
&= ~TMP_MARK
;
1431 surviving_parents
++;
1433 return surviving_parents
;
1436 struct merge_simplify_state
{
1437 struct commit
*simplified
;
1440 static struct merge_simplify_state
*locate_simplify_state(struct rev_info
*revs
, struct commit
*commit
)
1442 struct merge_simplify_state
*st
;
1444 st
= lookup_decoration(&revs
->merge_simplification
, &commit
->object
);
1446 st
= xcalloc(1, sizeof(*st
));
1447 add_decoration(&revs
->merge_simplification
, &commit
->object
, st
);
1452 static struct commit_list
**simplify_one(struct rev_info
*revs
, struct commit
*commit
, struct commit_list
**tail
)
1454 struct commit_list
*p
;
1455 struct merge_simplify_state
*st
, *pst
;
1458 st
= locate_simplify_state(revs
, commit
);
1461 * Have we handled this one?
1467 * An UNINTERESTING commit simplifies to itself, so does a
1468 * root commit. We do not rewrite parents of such commit
1471 if ((commit
->object
.flags
& UNINTERESTING
) || !commit
->parents
) {
1472 st
->simplified
= commit
;
1477 * Do we know what commit all of our parents should be rewritten to?
1478 * Otherwise we are not ready to rewrite this one yet.
1480 for (cnt
= 0, p
= commit
->parents
; p
; p
= p
->next
) {
1481 pst
= locate_simplify_state(revs
, p
->item
);
1482 if (!pst
->simplified
) {
1483 tail
= &commit_list_insert(p
->item
, tail
)->next
;
1488 tail
= &commit_list_insert(commit
, tail
)->next
;
1493 * Rewrite our list of parents.
1495 for (p
= commit
->parents
; p
; p
= p
->next
) {
1496 pst
= locate_simplify_state(revs
, p
->item
);
1497 p
->item
= pst
->simplified
;
1499 cnt
= remove_duplicate_parents(commit
);
1502 * It is possible that we are a merge and one side branch
1503 * does not have any commit that touches the given paths;
1504 * in such a case, the immediate parents will be rewritten
1505 * to different commits.
1507 * o----X X: the commit we are looking at;
1508 * / / o: a commit that touches the paths;
1511 * Further reduce the parents by removing redundant parents.
1514 struct commit_list
*h
= reduce_heads(commit
->parents
);
1515 cnt
= commit_list_count(h
);
1516 free_commit_list(commit
->parents
);
1517 commit
->parents
= h
;
1521 * A commit simplifies to itself if it is a root, if it is
1522 * UNINTERESTING, if it touches the given paths, or if it is a
1523 * merge and its parents simplifies to more than one commits
1524 * (the first two cases are already handled at the beginning of
1527 * Otherwise, it simplifies to what its sole parent simplifies to.
1530 (commit
->object
.flags
& UNINTERESTING
) ||
1531 !(commit
->object
.flags
& TREESAME
) ||
1533 st
->simplified
= commit
;
1535 pst
= locate_simplify_state(revs
, commit
->parents
->item
);
1536 st
->simplified
= pst
->simplified
;
1541 static void simplify_merges(struct rev_info
*revs
)
1543 struct commit_list
*list
;
1544 struct commit_list
*yet_to_do
, **tail
;
1546 if (!revs
->topo_order
)
1547 sort_in_topological_order(&revs
->commits
, revs
->lifo
);
1551 /* feed the list reversed */
1553 for (list
= revs
->commits
; list
; list
= list
->next
)
1554 commit_list_insert(list
->item
, &yet_to_do
);
1560 struct commit
*commit
= list
->item
;
1561 struct commit_list
*next
= list
->next
;
1564 tail
= simplify_one(revs
, commit
, tail
);
1568 /* clean up the result, removing the simplified ones */
1569 list
= revs
->commits
;
1570 revs
->commits
= NULL
;
1571 tail
= &revs
->commits
;
1573 struct commit
*commit
= list
->item
;
1574 struct commit_list
*next
= list
->next
;
1575 struct merge_simplify_state
*st
;
1578 st
= locate_simplify_state(revs
, commit
);
1579 if (st
->simplified
== commit
)
1580 tail
= &commit_list_insert(commit
, tail
)->next
;
1584 static void set_children(struct rev_info
*revs
)
1586 struct commit_list
*l
;
1587 for (l
= revs
->commits
; l
; l
= l
->next
) {
1588 struct commit
*commit
= l
->item
;
1589 struct commit_list
*p
;
1591 for (p
= commit
->parents
; p
; p
= p
->next
)
1592 add_child(revs
, p
->item
, commit
);
1596 int prepare_revision_walk(struct rev_info
*revs
)
1598 int nr
= revs
->pending
.nr
;
1599 struct object_array_entry
*e
, *list
;
1601 e
= list
= revs
->pending
.objects
;
1602 revs
->pending
.nr
= 0;
1603 revs
->pending
.alloc
= 0;
1604 revs
->pending
.objects
= NULL
;
1606 struct commit
*commit
= handle_commit(revs
, e
->item
, e
->name
);
1608 if (!(commit
->object
.flags
& SEEN
)) {
1609 commit
->object
.flags
|= SEEN
;
1610 insert_by_date(commit
, &revs
->commits
);
1620 if (limit_list(revs
) < 0)
1622 if (revs
->topo_order
)
1623 sort_in_topological_order(&revs
->commits
, revs
->lifo
);
1624 if (revs
->simplify_merges
)
1625 simplify_merges(revs
);
1626 if (revs
->children
.name
)
1631 enum rewrite_result
{
1633 rewrite_one_noparents
,
1637 static enum rewrite_result
rewrite_one(struct rev_info
*revs
, struct commit
**pp
)
1639 struct commit_list
*cache
= NULL
;
1642 struct commit
*p
= *pp
;
1644 if (add_parents_to_list(revs
, p
, &revs
->commits
, &cache
) < 0)
1645 return rewrite_one_error
;
1646 if (p
->parents
&& p
->parents
->next
)
1647 return rewrite_one_ok
;
1648 if (p
->object
.flags
& UNINTERESTING
)
1649 return rewrite_one_ok
;
1650 if (!(p
->object
.flags
& TREESAME
))
1651 return rewrite_one_ok
;
1653 return rewrite_one_noparents
;
1654 *pp
= p
->parents
->item
;
1658 static int rewrite_parents(struct rev_info
*revs
, struct commit
*commit
)
1660 struct commit_list
**pp
= &commit
->parents
;
1662 struct commit_list
*parent
= *pp
;
1663 switch (rewrite_one(revs
, &parent
->item
)) {
1664 case rewrite_one_ok
:
1666 case rewrite_one_noparents
:
1669 case rewrite_one_error
:
1674 remove_duplicate_parents(commit
);
1678 static int commit_match(struct commit
*commit
, struct rev_info
*opt
)
1680 if (!opt
->grep_filter
.pattern_list
)
1682 if (!commit
->object
.parsed
)
1683 parse_commit(commit
);
1684 return grep_buffer(&opt
->grep_filter
,
1685 NULL
, /* we say nothing, not even filename */
1686 commit
->buffer
, strlen(commit
->buffer
));
1689 static inline int want_ancestry(struct rev_info
*revs
)
1691 return (revs
->rewrite_parents
|| revs
->children
.name
);
1694 enum commit_action
get_commit_action(struct rev_info
*revs
, struct commit
*commit
)
1696 if (commit
->object
.flags
& SHOWN
)
1697 return commit_ignore
;
1698 if (revs
->unpacked
&& has_sha1_pack(commit
->object
.sha1
))
1699 return commit_ignore
;
1702 if (commit
->object
.flags
& UNINTERESTING
)
1703 return commit_ignore
;
1704 if (revs
->min_age
!= -1 && (commit
->date
> revs
->min_age
))
1705 return commit_ignore
;
1706 if (revs
->no_merges
&& commit
->parents
&& commit
->parents
->next
)
1707 return commit_ignore
;
1708 if (revs
->merges_only
&& !(commit
->parents
&& commit
->parents
->next
))
1709 return commit_ignore
;
1710 if (!commit_match(commit
, revs
))
1711 return commit_ignore
;
1712 if (revs
->prune
&& revs
->dense
) {
1713 /* Commit without changes? */
1714 if (commit
->object
.flags
& TREESAME
) {
1715 /* drop merges unless we want parenthood */
1716 if (!want_ancestry(revs
))
1717 return commit_ignore
;
1718 /* non-merge - always ignore it */
1719 if (!commit
->parents
|| !commit
->parents
->next
)
1720 return commit_ignore
;
1726 enum commit_action
simplify_commit(struct rev_info
*revs
, struct commit
*commit
)
1728 enum commit_action action
= get_commit_action(revs
, commit
);
1730 if (action
== commit_show
&&
1732 revs
->prune
&& revs
->dense
&& want_ancestry(revs
)) {
1733 if (rewrite_parents(revs
, commit
) < 0)
1734 return commit_error
;
1739 static struct commit
*get_revision_1(struct rev_info
*revs
)
1745 struct commit_list
*entry
= revs
->commits
;
1746 struct commit
*commit
= entry
->item
;
1747 struct object
*obj
= &commit
->object
;
1749 revs
->commits
= entry
->next
;
1752 if (revs
->reflog_info
)
1753 fake_reflog_parent(revs
->reflog_info
, commit
);
1756 * If we haven't done the list limiting, we need to look at
1757 * the parents here. We also need to do the date-based limiting
1758 * that we'd otherwise have done in limit_list().
1760 if (!revs
->limited
) {
1761 if (revs
->max_age
!= -1 &&
1762 (commit
->date
< revs
->max_age
))
1765 if (!revs
->dont_cache_me
) {
1766 struct commit_list
*queue
= 0, **queuep
= &queue
;
1767 unsigned char *cache_sha1
;
1769 if (obj
->flags
& ADDED
)
1770 goto skip_parenting
;
1772 cache_sha1
= get_cache_slice(commit
);
1774 if (!traverse_cache_slice(revs
, cache_sha1
, commit
, 0, 0, &queuep
, &revs
->commits
)) {
1775 struct commit_list
*work
= revs
->commits
;
1777 /* attach queue to end of ->commits */
1778 while (work
&& work
->next
)
1784 revs
->commits
= queue
;
1786 goto skip_parenting
;
1791 if (add_parents_to_list(revs
, commit
, &revs
->commits
, NULL
) < 0)
1792 die("Failed to traverse parents of commit %s",
1793 sha1_to_hex(commit
->object
.sha1
));
1797 switch (simplify_commit(revs
, commit
)) {
1801 die("Failed to simplify parents of commit %s",
1802 sha1_to_hex(commit
->object
.sha1
));
1806 } while (revs
->commits
);
1810 static void gc_boundary(struct object_array
*array
)
1812 unsigned nr
= array
->nr
;
1813 unsigned alloc
= array
->alloc
;
1814 struct object_array_entry
*objects
= array
->objects
;
1818 for (i
= j
= 0; i
< nr
; i
++) {
1819 if (objects
[i
].item
->flags
& SHOWN
)
1822 objects
[j
] = objects
[i
];
1825 for (i
= j
; i
< nr
; i
++)
1826 objects
[i
].item
= NULL
;
1831 static void create_boundary_commit_list(struct rev_info
*revs
)
1835 struct object_array
*array
= &revs
->boundary_commits
;
1836 struct object_array_entry
*objects
= array
->objects
;
1839 * If revs->commits is non-NULL at this point, an error occurred in
1840 * get_revision_1(). Ignore the error and continue printing the
1841 * boundary commits anyway. (This is what the code has always
1844 if (revs
->commits
) {
1845 free_commit_list(revs
->commits
);
1846 revs
->commits
= NULL
;
1850 * Put all of the actual boundary commits from revs->boundary_commits
1851 * into revs->commits
1853 for (i
= 0; i
< array
->nr
; i
++) {
1854 c
= (struct commit
*)(objects
[i
].item
);
1857 if (!(c
->object
.flags
& CHILD_SHOWN
))
1859 if (c
->object
.flags
& (SHOWN
| BOUNDARY
))
1861 c
->object
.flags
|= BOUNDARY
;
1862 commit_list_insert(c
, &revs
->commits
);
1866 * If revs->topo_order is set, sort the boundary commits
1867 * in topological order
1869 sort_in_topological_order(&revs
->commits
, revs
->lifo
);
1872 static struct commit
*get_revision_internal(struct rev_info
*revs
)
1874 struct commit
*c
= NULL
;
1875 struct commit_list
*l
;
1877 if (revs
->boundary
== 2) {
1879 * All of the normal commits have already been returned,
1880 * and we are now returning boundary commits.
1881 * create_boundary_commit_list() has populated
1882 * revs->commits with the remaining commits to return.
1884 c
= pop_commit(&revs
->commits
);
1886 c
->object
.flags
|= SHOWN
;
1891 * Now pick up what they want to give us
1893 c
= get_revision_1(revs
);
1895 while (0 < revs
->skip_count
) {
1897 c
= get_revision_1(revs
);
1904 * Check the max_count.
1906 switch (revs
->max_count
) {
1917 c
->object
.flags
|= SHOWN
;
1919 if (!revs
->boundary
) {
1925 * get_revision_1() runs out the commits, and
1926 * we are done computing the boundaries.
1927 * switch to boundary commits output mode.
1932 * Update revs->commits to contain the list of
1935 create_boundary_commit_list(revs
);
1937 return get_revision_internal(revs
);
1941 * boundary commits are the commits that are parents of the
1942 * ones we got from get_revision_1() but they themselves are
1943 * not returned from get_revision_1(). Before returning
1944 * 'c', we need to mark its parents that they could be boundaries.
1947 for (l
= c
->parents
; l
; l
= l
->next
) {
1949 p
= &(l
->item
->object
);
1950 if (p
->flags
& (CHILD_SHOWN
| SHOWN
))
1952 p
->flags
|= CHILD_SHOWN
;
1953 gc_boundary(&revs
->boundary_commits
);
1954 add_object_array(p
, NULL
, &revs
->boundary_commits
);
1960 struct commit
*get_revision(struct rev_info
*revs
)
1963 struct commit_list
*reversed
;
1965 if (revs
->reverse
) {
1967 while ((c
= get_revision_internal(revs
))) {
1968 commit_list_insert(c
, &reversed
);
1970 revs
->commits
= reversed
;
1972 revs
->reverse_output_stage
= 1;
1975 if (revs
->reverse_output_stage
)
1976 return pop_commit(&revs
->commits
);
1978 c
= get_revision_internal(revs
);
1979 if (c
&& revs
->graph
)
1980 graph_update(revs
->graph
, c
);