11 #include "reflog-walk.h"
12 #include "patch-ids.h"
15 #include "string-list.h"
18 #include "commit-slab.h"
20 #include "cache-tree.h"
24 #include "argv-array.h"
26 volatile show_early_output_fn_t show_early_output
;
28 static const char *term_bad
;
29 static const char *term_good
;
31 void show_object_with_name(FILE *out
, struct object
*obj
, const char *name
)
35 fprintf(out
, "%s ", oid_to_hex(&obj
->oid
));
36 for (p
= name
; *p
&& *p
!= '\n'; p
++)
41 static void mark_blob_uninteresting(struct blob
*blob
)
45 if (blob
->object
.flags
& UNINTERESTING
)
47 blob
->object
.flags
|= UNINTERESTING
;
50 static void mark_tree_contents_uninteresting(struct tree
*tree
)
52 struct tree_desc desc
;
53 struct name_entry entry
;
54 struct object
*obj
= &tree
->object
;
56 if (!has_object_file(&obj
->oid
))
58 if (parse_tree(tree
) < 0)
59 die("bad tree %s", oid_to_hex(&obj
->oid
));
61 init_tree_desc(&desc
, tree
->buffer
, tree
->size
);
62 while (tree_entry(&desc
, &entry
)) {
63 switch (object_type(entry
.mode
)) {
65 mark_tree_uninteresting(lookup_tree(entry
.oid
));
68 mark_blob_uninteresting(lookup_blob(entry
.oid
));
71 /* Subproject commit - not in this repository */
77 * We don't care about the tree any more
78 * after it has been marked uninteresting.
80 free_tree_buffer(tree
);
83 void mark_tree_uninteresting(struct tree
*tree
)
91 if (obj
->flags
& UNINTERESTING
)
93 obj
->flags
|= UNINTERESTING
;
94 mark_tree_contents_uninteresting(tree
);
97 void mark_parents_uninteresting(struct commit
*commit
)
99 struct commit_list
*parents
= NULL
, *l
;
101 for (l
= commit
->parents
; l
; l
= l
->next
)
102 commit_list_insert(l
->item
, &parents
);
105 struct commit
*commit
= pop_commit(&parents
);
109 * A missing commit is ok iff its parent is marked
112 * We just mark such a thing parsed, so that when
113 * it is popped next time around, we won't be trying
114 * to parse it and get an error.
116 if (!has_object_file(&commit
->object
.oid
))
117 commit
->object
.parsed
= 1;
119 if (commit
->object
.flags
& UNINTERESTING
)
122 commit
->object
.flags
|= UNINTERESTING
;
125 * Normally we haven't parsed the parent
126 * yet, so we won't have a parent of a parent
127 * here. However, it may turn out that we've
128 * reached this commit some other way (where it
129 * wasn't uninteresting), in which case we need
130 * to mark its parents recursively too..
132 if (!commit
->parents
)
135 for (l
= commit
->parents
->next
; l
; l
= l
->next
)
136 commit_list_insert(l
->item
, &parents
);
137 commit
= commit
->parents
->item
;
142 static void add_pending_object_with_path(struct rev_info
*revs
,
144 const char *name
, unsigned mode
,
149 if (revs
->no_walk
&& (obj
->flags
& UNINTERESTING
))
151 if (revs
->reflog_info
&& obj
->type
== OBJ_COMMIT
) {
152 struct strbuf buf
= STRBUF_INIT
;
153 int len
= interpret_branch_name(name
, 0, &buf
, 0);
155 if (0 < len
&& name
[len
] && buf
.len
)
156 strbuf_addstr(&buf
, name
+ len
);
157 add_reflog_for_walk(revs
->reflog_info
,
158 (struct commit
*)obj
,
159 buf
.buf
[0] ? buf
.buf
: name
);
160 strbuf_release(&buf
);
161 return; /* do not add the commit itself */
163 add_object_array_with_path(obj
, name
, &revs
->pending
, mode
, path
);
166 static void add_pending_object_with_mode(struct rev_info
*revs
,
168 const char *name
, unsigned mode
)
170 add_pending_object_with_path(revs
, obj
, name
, mode
, NULL
);
173 void add_pending_object(struct rev_info
*revs
,
174 struct object
*obj
, const char *name
)
176 add_pending_object_with_mode(revs
, obj
, name
, S_IFINVALID
);
179 void add_head_to_pending(struct rev_info
*revs
)
181 struct object_id oid
;
183 if (get_oid("HEAD", &oid
))
185 obj
= parse_object(&oid
);
188 add_pending_object(revs
, obj
, "HEAD");
191 static struct object
*get_reference(struct rev_info
*revs
, const char *name
,
192 const struct object_id
*oid
,
195 struct object
*object
;
197 object
= parse_object(oid
);
199 if (revs
->ignore_missing
)
201 die("bad object %s", name
);
203 object
->flags
|= flags
;
207 void add_pending_oid(struct rev_info
*revs
, const char *name
,
208 const struct object_id
*oid
, unsigned int flags
)
210 struct object
*object
= get_reference(revs
, name
, oid
, flags
);
211 add_pending_object(revs
, object
, name
);
214 static struct commit
*handle_commit(struct rev_info
*revs
,
215 struct object_array_entry
*entry
)
217 struct object
*object
= entry
->item
;
218 const char *name
= entry
->name
;
219 const char *path
= entry
->path
;
220 unsigned int mode
= entry
->mode
;
221 unsigned long flags
= object
->flags
;
224 * Tag object? Look what it points to..
226 while (object
->type
== OBJ_TAG
) {
227 struct tag
*tag
= (struct tag
*) object
;
228 if (revs
->tag_objects
&& !(flags
& UNINTERESTING
))
229 add_pending_object(revs
, object
, tag
->tag
);
232 object
= parse_object(&tag
->tagged
->oid
);
234 if (revs
->ignore_missing_links
|| (flags
& UNINTERESTING
))
236 die("bad object %s", oid_to_hex(&tag
->tagged
->oid
));
238 object
->flags
|= flags
;
240 * We'll handle the tagged object by looping or dropping
241 * through to the non-tag handlers below. Do not
242 * propagate path data from the tag's pending entry.
249 * Commit object? Just return it, we'll do all the complex
252 if (object
->type
== OBJ_COMMIT
) {
253 struct commit
*commit
= (struct commit
*)object
;
254 if (parse_commit(commit
) < 0)
255 die("unable to parse commit %s", name
);
256 if (flags
& UNINTERESTING
) {
257 mark_parents_uninteresting(commit
);
260 if (revs
->show_source
&& !commit
->util
)
261 commit
->util
= xstrdup(name
);
266 * Tree object? Either mark it uninteresting, or add it
267 * to the list of objects to look at later..
269 if (object
->type
== OBJ_TREE
) {
270 struct tree
*tree
= (struct tree
*)object
;
271 if (!revs
->tree_objects
)
273 if (flags
& UNINTERESTING
) {
274 mark_tree_contents_uninteresting(tree
);
277 add_pending_object_with_path(revs
, object
, name
, mode
, path
);
282 * Blob object? You know the drill by now..
284 if (object
->type
== OBJ_BLOB
) {
285 if (!revs
->blob_objects
)
287 if (flags
& UNINTERESTING
)
289 add_pending_object_with_path(revs
, object
, name
, mode
, path
);
292 die("%s is unknown object", name
);
295 static int everybody_uninteresting(struct commit_list
*orig
,
296 struct commit
**interesting_cache
)
298 struct commit_list
*list
= orig
;
300 if (*interesting_cache
) {
301 struct commit
*commit
= *interesting_cache
;
302 if (!(commit
->object
.flags
& UNINTERESTING
))
307 struct commit
*commit
= list
->item
;
309 if (commit
->object
.flags
& UNINTERESTING
)
312 *interesting_cache
= commit
;
319 * A definition of "relevant" commit that we can use to simplify limited graphs
320 * by eliminating side branches.
322 * A "relevant" commit is one that is !UNINTERESTING (ie we are including it
323 * in our list), or that is a specified BOTTOM commit. Then after computing
324 * a limited list, during processing we can generally ignore boundary merges
325 * coming from outside the graph, (ie from irrelevant parents), and treat
326 * those merges as if they were single-parent. TREESAME is defined to consider
327 * only relevant parents, if any. If we are TREESAME to our on-graph parents,
328 * we don't care if we were !TREESAME to non-graph parents.
330 * Treating bottom commits as relevant ensures that a limited graph's
331 * connection to the actual bottom commit is not viewed as a side branch, but
332 * treated as part of the graph. For example:
334 * ....Z...A---X---o---o---B
338 * When computing "A..B", the A-X connection is at least as important as
339 * Y-X, despite A being flagged UNINTERESTING.
341 * And when computing --ancestry-path "A..B", the A-X connection is more
342 * important than Y-X, despite both A and Y being flagged UNINTERESTING.
344 static inline int relevant_commit(struct commit
*commit
)
346 return (commit
->object
.flags
& (UNINTERESTING
| BOTTOM
)) != UNINTERESTING
;
350 * Return a single relevant commit from a parent list. If we are a TREESAME
351 * commit, and this selects one of our parents, then we can safely simplify to
354 static struct commit
*one_relevant_parent(const struct rev_info
*revs
,
355 struct commit_list
*orig
)
357 struct commit_list
*list
= orig
;
358 struct commit
*relevant
= NULL
;
364 * For 1-parent commits, or if first-parent-only, then return that
365 * first parent (even if not "relevant" by the above definition).
366 * TREESAME will have been set purely on that parent.
368 if (revs
->first_parent_only
|| !orig
->next
)
372 * For multi-parent commits, identify a sole relevant parent, if any.
373 * If we have only one relevant parent, then TREESAME will be set purely
374 * with regard to that parent, and we can simplify accordingly.
376 * If we have more than one relevant parent, or no relevant parents
377 * (and multiple irrelevant ones), then we can't select a parent here
381 struct commit
*commit
= list
->item
;
383 if (relevant_commit(commit
)) {
393 * The goal is to get REV_TREE_NEW as the result only if the
394 * diff consists of all '+' (and no other changes), REV_TREE_OLD
395 * if the whole diff is removal of old data, and otherwise
396 * REV_TREE_DIFFERENT (of course if the trees are the same we
397 * want REV_TREE_SAME).
399 * The only time we care about the distinction is when
400 * remove_empty_trees is in effect, in which case we care only about
401 * whether the whole change is REV_TREE_NEW, or if there's another type
402 * of change. Which means we can stop the diff early in either of these
405 * 1. We're not using remove_empty_trees at all.
407 * 2. We saw anything except REV_TREE_NEW.
409 static int tree_difference
= REV_TREE_SAME
;
411 static void file_add_remove(struct diff_options
*options
,
412 int addremove
, unsigned mode
,
413 const struct object_id
*oid
,
415 const char *fullpath
, unsigned dirty_submodule
)
417 int diff
= addremove
== '+' ? REV_TREE_NEW
: REV_TREE_OLD
;
418 struct rev_info
*revs
= options
->change_fn_data
;
420 tree_difference
|= diff
;
421 if (!revs
->remove_empty_trees
|| tree_difference
!= REV_TREE_NEW
)
422 options
->flags
.has_changes
= 1;
425 static void file_change(struct diff_options
*options
,
426 unsigned old_mode
, unsigned new_mode
,
427 const struct object_id
*old_oid
,
428 const struct object_id
*new_oid
,
429 int old_oid_valid
, int new_oid_valid
,
430 const char *fullpath
,
431 unsigned old_dirty_submodule
, unsigned new_dirty_submodule
)
433 tree_difference
= REV_TREE_DIFFERENT
;
434 options
->flags
.has_changes
= 1;
437 static int rev_compare_tree(struct rev_info
*revs
,
438 struct commit
*parent
, struct commit
*commit
)
440 struct tree
*t1
= parent
->tree
;
441 struct tree
*t2
= commit
->tree
;
448 if (revs
->simplify_by_decoration
) {
450 * If we are simplifying by decoration, then the commit
451 * is worth showing if it has a tag pointing at it.
453 if (get_name_decoration(&commit
->object
))
454 return REV_TREE_DIFFERENT
;
456 * A commit that is not pointed by a tag is uninteresting
457 * if we are not limited by path. This means that you will
458 * see the usual "commits that touch the paths" plus any
459 * tagged commit by specifying both --simplify-by-decoration
462 if (!revs
->prune_data
.nr
)
463 return REV_TREE_SAME
;
466 tree_difference
= REV_TREE_SAME
;
467 revs
->pruning
.flags
.has_changes
= 0;
468 if (diff_tree_oid(&t1
->object
.oid
, &t2
->object
.oid
, "",
470 return REV_TREE_DIFFERENT
;
471 return tree_difference
;
474 static int rev_same_tree_as_empty(struct rev_info
*revs
, struct commit
*commit
)
477 struct tree
*t1
= commit
->tree
;
482 tree_difference
= REV_TREE_SAME
;
483 revs
->pruning
.flags
.has_changes
= 0;
484 retval
= diff_tree_oid(NULL
, &t1
->object
.oid
, "", &revs
->pruning
);
486 return retval
>= 0 && (tree_difference
== REV_TREE_SAME
);
489 struct treesame_state
{
490 unsigned int nparents
;
491 unsigned char treesame
[FLEX_ARRAY
];
494 static struct treesame_state
*initialise_treesame(struct rev_info
*revs
, struct commit
*commit
)
496 unsigned n
= commit_list_count(commit
->parents
);
497 struct treesame_state
*st
= xcalloc(1, st_add(sizeof(*st
), n
));
499 add_decoration(&revs
->treesame
, &commit
->object
, st
);
504 * Must be called immediately after removing the nth_parent from a commit's
505 * parent list, if we are maintaining the per-parent treesame[] decoration.
506 * This does not recalculate the master TREESAME flag - update_treesame()
507 * should be called to update it after a sequence of treesame[] modifications
508 * that may have affected it.
510 static int compact_treesame(struct rev_info
*revs
, struct commit
*commit
, unsigned nth_parent
)
512 struct treesame_state
*st
;
515 if (!commit
->parents
) {
517 * Have just removed the only parent from a non-merge.
518 * Different handling, as we lack decoration.
521 die("compact_treesame %u", nth_parent
);
522 old_same
= !!(commit
->object
.flags
& TREESAME
);
523 if (rev_same_tree_as_empty(revs
, commit
))
524 commit
->object
.flags
|= TREESAME
;
526 commit
->object
.flags
&= ~TREESAME
;
530 st
= lookup_decoration(&revs
->treesame
, &commit
->object
);
531 if (!st
|| nth_parent
>= st
->nparents
)
532 die("compact_treesame %u", nth_parent
);
534 old_same
= st
->treesame
[nth_parent
];
535 memmove(st
->treesame
+ nth_parent
,
536 st
->treesame
+ nth_parent
+ 1,
537 st
->nparents
- nth_parent
- 1);
540 * If we've just become a non-merge commit, update TREESAME
541 * immediately, and remove the no-longer-needed decoration.
542 * If still a merge, defer update until update_treesame().
544 if (--st
->nparents
== 1) {
545 if (commit
->parents
->next
)
546 die("compact_treesame parents mismatch");
547 if (st
->treesame
[0] && revs
->dense
)
548 commit
->object
.flags
|= TREESAME
;
550 commit
->object
.flags
&= ~TREESAME
;
551 free(add_decoration(&revs
->treesame
, &commit
->object
, NULL
));
557 static unsigned update_treesame(struct rev_info
*revs
, struct commit
*commit
)
559 if (commit
->parents
&& commit
->parents
->next
) {
561 struct treesame_state
*st
;
562 struct commit_list
*p
;
563 unsigned relevant_parents
;
564 unsigned relevant_change
, irrelevant_change
;
566 st
= lookup_decoration(&revs
->treesame
, &commit
->object
);
568 die("update_treesame %s", oid_to_hex(&commit
->object
.oid
));
569 relevant_parents
= 0;
570 relevant_change
= irrelevant_change
= 0;
571 for (p
= commit
->parents
, n
= 0; p
; n
++, p
= p
->next
) {
572 if (relevant_commit(p
->item
)) {
573 relevant_change
|= !st
->treesame
[n
];
576 irrelevant_change
|= !st
->treesame
[n
];
578 if (relevant_parents
? relevant_change
: irrelevant_change
)
579 commit
->object
.flags
&= ~TREESAME
;
581 commit
->object
.flags
|= TREESAME
;
584 return commit
->object
.flags
& TREESAME
;
587 static inline int limiting_can_increase_treesame(const struct rev_info
*revs
)
590 * TREESAME is irrelevant unless prune && dense;
591 * if simplify_history is set, we can't have a mixture of TREESAME and
592 * !TREESAME INTERESTING parents (and we don't have treesame[]
593 * decoration anyway);
594 * if first_parent_only is set, then the TREESAME flag is locked
595 * against the first parent (and again we lack treesame[] decoration).
597 return revs
->prune
&& revs
->dense
&&
598 !revs
->simplify_history
&&
599 !revs
->first_parent_only
;
602 static void try_to_simplify_commit(struct rev_info
*revs
, struct commit
*commit
)
604 struct commit_list
**pp
, *parent
;
605 struct treesame_state
*ts
= NULL
;
606 int relevant_change
= 0, irrelevant_change
= 0;
607 int relevant_parents
, nth_parent
;
610 * If we don't do pruning, everything is interesting
618 if (!commit
->parents
) {
619 if (rev_same_tree_as_empty(revs
, commit
))
620 commit
->object
.flags
|= TREESAME
;
625 * Normal non-merge commit? If we don't want to make the
626 * history dense, we consider it always to be a change..
628 if (!revs
->dense
&& !commit
->parents
->next
)
631 for (pp
= &commit
->parents
, nth_parent
= 0, relevant_parents
= 0;
632 (parent
= *pp
) != NULL
;
633 pp
= &parent
->next
, nth_parent
++) {
634 struct commit
*p
= parent
->item
;
635 if (relevant_commit(p
))
638 if (nth_parent
== 1) {
640 * This our second loop iteration - so we now know
641 * we're dealing with a merge.
643 * Do not compare with later parents when we care only about
644 * the first parent chain, in order to avoid derailing the
645 * traversal to follow a side branch that brought everything
646 * in the path we are limited to by the pathspec.
648 if (revs
->first_parent_only
)
651 * If this will remain a potentially-simplifiable
652 * merge, remember per-parent treesame if needed.
653 * Initialise the array with the comparison from our
656 if (revs
->treesame
.name
&&
657 !revs
->simplify_history
&&
658 !(commit
->object
.flags
& UNINTERESTING
)) {
659 ts
= initialise_treesame(revs
, commit
);
660 if (!(irrelevant_change
|| relevant_change
))
664 if (parse_commit(p
) < 0)
665 die("cannot simplify commit %s (because of %s)",
666 oid_to_hex(&commit
->object
.oid
),
667 oid_to_hex(&p
->object
.oid
));
668 switch (rev_compare_tree(revs
, p
, commit
)) {
670 if (!revs
->simplify_history
|| !relevant_commit(p
)) {
671 /* Even if a merge with an uninteresting
672 * side branch brought the entire change
673 * we are interested in, we do not want
674 * to lose the other branches of this
675 * merge, so we just keep going.
678 ts
->treesame
[nth_parent
] = 1;
682 commit
->parents
= parent
;
683 commit
->object
.flags
|= TREESAME
;
687 if (revs
->remove_empty_trees
&&
688 rev_same_tree_as_empty(revs
, p
)) {
689 /* We are adding all the specified
690 * paths from this parent, so the
691 * history beyond this parent is not
692 * interesting. Remove its parents
693 * (they are grandparents for us).
694 * IOW, we pretend this parent is a
697 if (parse_commit(p
) < 0)
698 die("cannot simplify commit %s (invalid %s)",
699 oid_to_hex(&commit
->object
.oid
),
700 oid_to_hex(&p
->object
.oid
));
705 case REV_TREE_DIFFERENT
:
706 if (relevant_commit(p
))
709 irrelevant_change
= 1;
712 die("bad tree compare for commit %s", oid_to_hex(&commit
->object
.oid
));
716 * TREESAME is straightforward for single-parent commits. For merge
717 * commits, it is most useful to define it so that "irrelevant"
718 * parents cannot make us !TREESAME - if we have any relevant
719 * parents, then we only consider TREESAMEness with respect to them,
720 * allowing irrelevant merges from uninteresting branches to be
721 * simplified away. Only if we have only irrelevant parents do we
722 * base TREESAME on them. Note that this logic is replicated in
723 * update_treesame, which should be kept in sync.
725 if (relevant_parents
? !relevant_change
: !irrelevant_change
)
726 commit
->object
.flags
|= TREESAME
;
729 static void commit_list_insert_by_date_cached(struct commit
*p
, struct commit_list
**head
,
730 struct commit_list
*cached_base
, struct commit_list
**cache
)
732 struct commit_list
*new_entry
;
734 if (cached_base
&& p
->date
< cached_base
->item
->date
)
735 new_entry
= commit_list_insert_by_date(p
, &cached_base
->next
);
737 new_entry
= commit_list_insert_by_date(p
, head
);
739 if (cache
&& (!*cache
|| p
->date
< (*cache
)->item
->date
))
743 static int add_parents_to_list(struct rev_info
*revs
, struct commit
*commit
,
744 struct commit_list
**list
, struct commit_list
**cache_ptr
)
746 struct commit_list
*parent
= commit
->parents
;
748 struct commit_list
*cached_base
= cache_ptr
? *cache_ptr
: NULL
;
750 if (commit
->object
.flags
& ADDED
)
752 commit
->object
.flags
|= ADDED
;
754 if (revs
->include_check
&&
755 !revs
->include_check(commit
, revs
->include_check_data
))
759 * If the commit is uninteresting, don't try to
760 * prune parents - we want the maximal uninteresting
763 * Normally we haven't parsed the parent
764 * yet, so we won't have a parent of a parent
765 * here. However, it may turn out that we've
766 * reached this commit some other way (where it
767 * wasn't uninteresting), in which case we need
768 * to mark its parents recursively too..
770 if (commit
->object
.flags
& UNINTERESTING
) {
772 struct commit
*p
= parent
->item
;
773 parent
= parent
->next
;
775 p
->object
.flags
|= UNINTERESTING
;
776 if (parse_commit_gently(p
, 1) < 0)
779 mark_parents_uninteresting(p
);
780 if (p
->object
.flags
& SEEN
)
782 p
->object
.flags
|= SEEN
;
783 commit_list_insert_by_date_cached(p
, list
, cached_base
, cache_ptr
);
789 * Ok, the commit wasn't uninteresting. Try to
790 * simplify the commit history and find the parent
791 * that has no differences in the path set if one exists.
793 try_to_simplify_commit(revs
, commit
);
798 left_flag
= (commit
->object
.flags
& SYMMETRIC_LEFT
);
800 for (parent
= commit
->parents
; parent
; parent
= parent
->next
) {
801 struct commit
*p
= parent
->item
;
803 if (parse_commit_gently(p
, revs
->ignore_missing_links
) < 0)
805 if (revs
->show_source
&& !p
->util
)
806 p
->util
= commit
->util
;
807 p
->object
.flags
|= left_flag
;
808 if (!(p
->object
.flags
& SEEN
)) {
809 p
->object
.flags
|= SEEN
;
810 commit_list_insert_by_date_cached(p
, list
, cached_base
, cache_ptr
);
812 if (revs
->first_parent_only
)
818 static void cherry_pick_list(struct commit_list
*list
, struct rev_info
*revs
)
820 struct commit_list
*p
;
821 int left_count
= 0, right_count
= 0;
823 struct patch_ids ids
;
824 unsigned cherry_flag
;
826 /* First count the commits on the left and on the right */
827 for (p
= list
; p
; p
= p
->next
) {
828 struct commit
*commit
= p
->item
;
829 unsigned flags
= commit
->object
.flags
;
830 if (flags
& BOUNDARY
)
832 else if (flags
& SYMMETRIC_LEFT
)
838 if (!left_count
|| !right_count
)
841 left_first
= left_count
< right_count
;
842 init_patch_ids(&ids
);
843 ids
.diffopts
.pathspec
= revs
->diffopt
.pathspec
;
845 /* Compute patch-ids for one side */
846 for (p
= list
; p
; p
= p
->next
) {
847 struct commit
*commit
= p
->item
;
848 unsigned flags
= commit
->object
.flags
;
850 if (flags
& BOUNDARY
)
853 * If we have fewer left, left_first is set and we omit
854 * commits on the right branch in this loop. If we have
855 * fewer right, we skip the left ones.
857 if (left_first
!= !!(flags
& SYMMETRIC_LEFT
))
859 add_commit_patch_id(commit
, &ids
);
862 /* either cherry_mark or cherry_pick are true */
863 cherry_flag
= revs
->cherry_mark
? PATCHSAME
: SHOWN
;
865 /* Check the other side */
866 for (p
= list
; p
; p
= p
->next
) {
867 struct commit
*commit
= p
->item
;
869 unsigned flags
= commit
->object
.flags
;
871 if (flags
& BOUNDARY
)
874 * If we have fewer left, left_first is set and we omit
875 * commits on the left branch in this loop.
877 if (left_first
== !!(flags
& SYMMETRIC_LEFT
))
881 * Have we seen the same patch id?
883 id
= has_commit_patch_id(commit
, &ids
);
887 commit
->object
.flags
|= cherry_flag
;
888 id
->commit
->object
.flags
|= cherry_flag
;
891 free_patch_ids(&ids
);
894 /* How many extra uninteresting commits we want to see.. */
897 static int still_interesting(struct commit_list
*src
, timestamp_t date
, int slop
,
898 struct commit
**interesting_cache
)
901 * No source list at all? We're definitely done..
907 * Does the destination list contain entries with a date
908 * before the source list? Definitely _not_ done.
910 if (date
<= src
->item
->date
)
914 * Does the source list still have interesting commits in
915 * it? Definitely not done..
917 if (!everybody_uninteresting(src
, interesting_cache
))
920 /* Ok, we're closing in.. */
925 * "rev-list --ancestry-path A..B" computes commits that are ancestors
926 * of B but not ancestors of A but further limits the result to those
927 * that are descendants of A. This takes the list of bottom commits and
928 * the result of "A..B" without --ancestry-path, and limits the latter
929 * further to the ones that can reach one of the commits in "bottom".
931 static void limit_to_ancestry(struct commit_list
*bottom
, struct commit_list
*list
)
933 struct commit_list
*p
;
934 struct commit_list
*rlist
= NULL
;
938 * Reverse the list so that it will be likely that we would
939 * process parents before children.
941 for (p
= list
; p
; p
= p
->next
)
942 commit_list_insert(p
->item
, &rlist
);
944 for (p
= bottom
; p
; p
= p
->next
)
945 p
->item
->object
.flags
|= TMP_MARK
;
948 * Mark the ones that can reach bottom commits in "list",
949 * in a bottom-up fashion.
953 for (p
= rlist
; p
; p
= p
->next
) {
954 struct commit
*c
= p
->item
;
955 struct commit_list
*parents
;
956 if (c
->object
.flags
& (TMP_MARK
| UNINTERESTING
))
958 for (parents
= c
->parents
;
960 parents
= parents
->next
) {
961 if (!(parents
->item
->object
.flags
& TMP_MARK
))
963 c
->object
.flags
|= TMP_MARK
;
968 } while (made_progress
);
971 * NEEDSWORK: decide if we want to remove parents that are
972 * not marked with TMP_MARK from commit->parents for commits
973 * in the resulting list. We may not want to do that, though.
977 * The ones that are not marked with TMP_MARK are uninteresting
979 for (p
= list
; p
; p
= p
->next
) {
980 struct commit
*c
= p
->item
;
981 if (c
->object
.flags
& TMP_MARK
)
983 c
->object
.flags
|= UNINTERESTING
;
986 /* We are done with the TMP_MARK */
987 for (p
= list
; p
; p
= p
->next
)
988 p
->item
->object
.flags
&= ~TMP_MARK
;
989 for (p
= bottom
; p
; p
= p
->next
)
990 p
->item
->object
.flags
&= ~TMP_MARK
;
991 free_commit_list(rlist
);
995 * Before walking the history, keep the set of "negative" refs the
996 * caller has asked to exclude.
998 * This is used to compute "rev-list --ancestry-path A..B", as we need
999 * to filter the result of "A..B" further to the ones that can actually
1002 static struct commit_list
*collect_bottom_commits(struct commit_list
*list
)
1004 struct commit_list
*elem
, *bottom
= NULL
;
1005 for (elem
= list
; elem
; elem
= elem
->next
)
1006 if (elem
->item
->object
.flags
& BOTTOM
)
1007 commit_list_insert(elem
->item
, &bottom
);
1011 /* Assumes either left_only or right_only is set */
1012 static void limit_left_right(struct commit_list
*list
, struct rev_info
*revs
)
1014 struct commit_list
*p
;
1016 for (p
= list
; p
; p
= p
->next
) {
1017 struct commit
*commit
= p
->item
;
1019 if (revs
->right_only
) {
1020 if (commit
->object
.flags
& SYMMETRIC_LEFT
)
1021 commit
->object
.flags
|= SHOWN
;
1022 } else /* revs->left_only is set */
1023 if (!(commit
->object
.flags
& SYMMETRIC_LEFT
))
1024 commit
->object
.flags
|= SHOWN
;
1028 static int limit_list(struct rev_info
*revs
)
1031 timestamp_t date
= TIME_MAX
;
1032 struct commit_list
*list
= revs
->commits
;
1033 struct commit_list
*newlist
= NULL
;
1034 struct commit_list
**p
= &newlist
;
1035 struct commit_list
*bottom
= NULL
;
1036 struct commit
*interesting_cache
= NULL
;
1038 if (revs
->ancestry_path
) {
1039 bottom
= collect_bottom_commits(list
);
1041 die("--ancestry-path given but there are no bottom commits");
1045 struct commit
*commit
= pop_commit(&list
);
1046 struct object
*obj
= &commit
->object
;
1047 show_early_output_fn_t show
;
1049 if (commit
== interesting_cache
)
1050 interesting_cache
= NULL
;
1052 if (revs
->max_age
!= -1 && (commit
->date
< revs
->max_age
))
1053 obj
->flags
|= UNINTERESTING
;
1054 if (add_parents_to_list(revs
, commit
, &list
, NULL
) < 0)
1056 if (obj
->flags
& UNINTERESTING
) {
1057 mark_parents_uninteresting(commit
);
1058 slop
= still_interesting(list
, date
, slop
, &interesting_cache
);
1063 if (revs
->min_age
!= -1 && (commit
->date
> revs
->min_age
))
1065 date
= commit
->date
;
1066 p
= &commit_list_insert(commit
, p
)->next
;
1068 show
= show_early_output
;
1072 show(revs
, newlist
);
1073 show_early_output
= NULL
;
1075 if (revs
->cherry_pick
|| revs
->cherry_mark
)
1076 cherry_pick_list(newlist
, revs
);
1078 if (revs
->left_only
|| revs
->right_only
)
1079 limit_left_right(newlist
, revs
);
1082 limit_to_ancestry(bottom
, newlist
);
1083 free_commit_list(bottom
);
1087 * Check if any commits have become TREESAME by some of their parents
1088 * becoming UNINTERESTING.
1090 if (limiting_can_increase_treesame(revs
))
1091 for (list
= newlist
; list
; list
= list
->next
) {
1092 struct commit
*c
= list
->item
;
1093 if (c
->object
.flags
& (UNINTERESTING
| TREESAME
))
1095 update_treesame(revs
, c
);
1098 revs
->commits
= newlist
;
1103 * Add an entry to refs->cmdline with the specified information.
1106 static void add_rev_cmdline(struct rev_info
*revs
,
1107 struct object
*item
,
1112 struct rev_cmdline_info
*info
= &revs
->cmdline
;
1113 unsigned int nr
= info
->nr
;
1115 ALLOC_GROW(info
->rev
, nr
+ 1, info
->alloc
);
1116 info
->rev
[nr
].item
= item
;
1117 info
->rev
[nr
].name
= xstrdup(name
);
1118 info
->rev
[nr
].whence
= whence
;
1119 info
->rev
[nr
].flags
= flags
;
1123 static void add_rev_cmdline_list(struct rev_info
*revs
,
1124 struct commit_list
*commit_list
,
1128 while (commit_list
) {
1129 struct object
*object
= &commit_list
->item
->object
;
1130 add_rev_cmdline(revs
, object
, oid_to_hex(&object
->oid
),
1132 commit_list
= commit_list
->next
;
1136 struct all_refs_cb
{
1138 int warned_bad_reflog
;
1139 struct rev_info
*all_revs
;
1140 const char *name_for_errormsg
;
1141 struct ref_store
*refs
;
1144 int ref_excluded(struct string_list
*ref_excludes
, const char *path
)
1146 struct string_list_item
*item
;
1150 for_each_string_list_item(item
, ref_excludes
) {
1151 if (!wildmatch(item
->string
, path
, 0))
1157 static int handle_one_ref(const char *path
, const struct object_id
*oid
,
1158 int flag
, void *cb_data
)
1160 struct all_refs_cb
*cb
= cb_data
;
1161 struct object
*object
;
1163 if (ref_excluded(cb
->all_revs
->ref_excludes
, path
))
1166 object
= get_reference(cb
->all_revs
, path
, oid
, cb
->all_flags
);
1167 add_rev_cmdline(cb
->all_revs
, object
, path
, REV_CMD_REF
, cb
->all_flags
);
1168 add_pending_oid(cb
->all_revs
, path
, oid
, cb
->all_flags
);
1172 static void init_all_refs_cb(struct all_refs_cb
*cb
, struct rev_info
*revs
,
1175 cb
->all_revs
= revs
;
1176 cb
->all_flags
= flags
;
1177 revs
->rev_input_given
= 1;
1181 void clear_ref_exclusion(struct string_list
**ref_excludes_p
)
1183 if (*ref_excludes_p
) {
1184 string_list_clear(*ref_excludes_p
, 0);
1185 free(*ref_excludes_p
);
1187 *ref_excludes_p
= NULL
;
1190 void add_ref_exclusion(struct string_list
**ref_excludes_p
, const char *exclude
)
1192 if (!*ref_excludes_p
) {
1193 *ref_excludes_p
= xcalloc(1, sizeof(**ref_excludes_p
));
1194 (*ref_excludes_p
)->strdup_strings
= 1;
1196 string_list_append(*ref_excludes_p
, exclude
);
1199 static void handle_refs(struct ref_store
*refs
,
1200 struct rev_info
*revs
, unsigned flags
,
1201 int (*for_each
)(struct ref_store
*, each_ref_fn
, void *))
1203 struct all_refs_cb cb
;
1206 /* this could happen with uninitialized submodules */
1210 init_all_refs_cb(&cb
, revs
, flags
);
1211 for_each(refs
, handle_one_ref
, &cb
);
1214 static void handle_one_reflog_commit(struct object_id
*oid
, void *cb_data
)
1216 struct all_refs_cb
*cb
= cb_data
;
1217 if (!is_null_oid(oid
)) {
1218 struct object
*o
= parse_object(oid
);
1220 o
->flags
|= cb
->all_flags
;
1221 /* ??? CMDLINEFLAGS ??? */
1222 add_pending_object(cb
->all_revs
, o
, "");
1224 else if (!cb
->warned_bad_reflog
) {
1225 warning("reflog of '%s' references pruned commits",
1226 cb
->name_for_errormsg
);
1227 cb
->warned_bad_reflog
= 1;
1232 static int handle_one_reflog_ent(struct object_id
*ooid
, struct object_id
*noid
,
1233 const char *email
, timestamp_t timestamp
, int tz
,
1234 const char *message
, void *cb_data
)
1236 handle_one_reflog_commit(ooid
, cb_data
);
1237 handle_one_reflog_commit(noid
, cb_data
);
1241 static int handle_one_reflog(const char *path
, const struct object_id
*oid
,
1242 int flag
, void *cb_data
)
1244 struct all_refs_cb
*cb
= cb_data
;
1245 cb
->warned_bad_reflog
= 0;
1246 cb
->name_for_errormsg
= path
;
1247 refs_for_each_reflog_ent(cb
->refs
, path
,
1248 handle_one_reflog_ent
, cb_data
);
1252 static void add_other_reflogs_to_pending(struct all_refs_cb
*cb
)
1254 struct worktree
**worktrees
, **p
;
1256 worktrees
= get_worktrees(0);
1257 for (p
= worktrees
; *p
; p
++) {
1258 struct worktree
*wt
= *p
;
1263 cb
->refs
= get_worktree_ref_store(wt
);
1264 refs_for_each_reflog(cb
->refs
,
1268 free_worktrees(worktrees
);
1271 void add_reflogs_to_pending(struct rev_info
*revs
, unsigned flags
)
1273 struct all_refs_cb cb
;
1276 cb
.all_flags
= flags
;
1277 cb
.refs
= get_main_ref_store();
1278 for_each_reflog(handle_one_reflog
, &cb
);
1280 if (!revs
->single_worktree
)
1281 add_other_reflogs_to_pending(&cb
);
1284 static void add_cache_tree(struct cache_tree
*it
, struct rev_info
*revs
,
1285 struct strbuf
*path
)
1287 size_t baselen
= path
->len
;
1290 if (it
->entry_count
>= 0) {
1291 struct tree
*tree
= lookup_tree(&it
->oid
);
1292 add_pending_object_with_path(revs
, &tree
->object
, "",
1296 for (i
= 0; i
< it
->subtree_nr
; i
++) {
1297 struct cache_tree_sub
*sub
= it
->down
[i
];
1298 strbuf_addf(path
, "%s%s", baselen
? "/" : "", sub
->name
);
1299 add_cache_tree(sub
->cache_tree
, revs
, path
);
1300 strbuf_setlen(path
, baselen
);
1305 static void do_add_index_objects_to_pending(struct rev_info
*revs
,
1306 struct index_state
*istate
)
1310 for (i
= 0; i
< istate
->cache_nr
; i
++) {
1311 struct cache_entry
*ce
= istate
->cache
[i
];
1314 if (S_ISGITLINK(ce
->ce_mode
))
1317 blob
= lookup_blob(&ce
->oid
);
1319 die("unable to add index blob to traversal");
1320 add_pending_object_with_path(revs
, &blob
->object
, "",
1321 ce
->ce_mode
, ce
->name
);
1324 if (istate
->cache_tree
) {
1325 struct strbuf path
= STRBUF_INIT
;
1326 add_cache_tree(istate
->cache_tree
, revs
, &path
);
1327 strbuf_release(&path
);
1331 void add_index_objects_to_pending(struct rev_info
*revs
, unsigned int flags
)
1333 struct worktree
**worktrees
, **p
;
1336 do_add_index_objects_to_pending(revs
, &the_index
);
1338 if (revs
->single_worktree
)
1341 worktrees
= get_worktrees(0);
1342 for (p
= worktrees
; *p
; p
++) {
1343 struct worktree
*wt
= *p
;
1344 struct index_state istate
= { NULL
};
1347 continue; /* current index already taken care of */
1349 if (read_index_from(&istate
,
1350 worktree_git_path(wt
, "index"),
1351 get_worktree_git_dir(wt
)) > 0)
1352 do_add_index_objects_to_pending(revs
, &istate
);
1353 discard_index(&istate
);
1355 free_worktrees(worktrees
);
1358 static int add_parents_only(struct rev_info
*revs
, const char *arg_
, int flags
,
1361 struct object_id oid
;
1363 struct commit
*commit
;
1364 struct commit_list
*parents
;
1366 const char *arg
= arg_
;
1369 flags
^= UNINTERESTING
| BOTTOM
;
1372 if (get_oid_committish(arg
, &oid
))
1375 it
= get_reference(revs
, arg
, &oid
, 0);
1376 if (!it
&& revs
->ignore_missing
)
1378 if (it
->type
!= OBJ_TAG
)
1380 if (!((struct tag
*)it
)->tagged
)
1382 oidcpy(&oid
, &((struct tag
*)it
)->tagged
->oid
);
1384 if (it
->type
!= OBJ_COMMIT
)
1386 commit
= (struct commit
*)it
;
1387 if (exclude_parent
&&
1388 exclude_parent
> commit_list_count(commit
->parents
))
1390 for (parents
= commit
->parents
, parent_number
= 1;
1392 parents
= parents
->next
, parent_number
++) {
1393 if (exclude_parent
&& parent_number
!= exclude_parent
)
1396 it
= &parents
->item
->object
;
1398 add_rev_cmdline(revs
, it
, arg_
, REV_CMD_PARENTS_ONLY
, flags
);
1399 add_pending_object(revs
, it
, arg
);
1404 void init_revisions(struct rev_info
*revs
, const char *prefix
)
1406 memset(revs
, 0, sizeof(*revs
));
1408 revs
->abbrev
= DEFAULT_ABBREV
;
1409 revs
->ignore_merges
= 1;
1410 revs
->simplify_history
= 1;
1411 revs
->pruning
.flags
.recursive
= 1;
1412 revs
->pruning
.flags
.quick
= 1;
1413 revs
->pruning
.add_remove
= file_add_remove
;
1414 revs
->pruning
.change
= file_change
;
1415 revs
->pruning
.change_fn_data
= revs
;
1416 revs
->sort_order
= REV_SORT_IN_GRAPH_ORDER
;
1418 revs
->prefix
= prefix
;
1421 revs
->skip_count
= -1;
1422 revs
->max_count
= -1;
1423 revs
->max_parents
= -1;
1424 revs
->expand_tabs_in_log
= -1;
1426 revs
->commit_format
= CMIT_FMT_DEFAULT
;
1427 revs
->expand_tabs_in_log_default
= 8;
1429 init_grep_defaults();
1430 grep_init(&revs
->grep_filter
, prefix
);
1431 revs
->grep_filter
.status_only
= 1;
1433 diff_setup(&revs
->diffopt
);
1434 if (prefix
&& !revs
->diffopt
.prefix
) {
1435 revs
->diffopt
.prefix
= prefix
;
1436 revs
->diffopt
.prefix_length
= strlen(prefix
);
1439 revs
->notes_opt
.use_default_notes
= -1;
1442 static void add_pending_commit_list(struct rev_info
*revs
,
1443 struct commit_list
*commit_list
,
1446 while (commit_list
) {
1447 struct object
*object
= &commit_list
->item
->object
;
1448 object
->flags
|= flags
;
1449 add_pending_object(revs
, object
, oid_to_hex(&object
->oid
));
1450 commit_list
= commit_list
->next
;
1454 static void prepare_show_merge(struct rev_info
*revs
)
1456 struct commit_list
*bases
;
1457 struct commit
*head
, *other
;
1458 struct object_id oid
;
1459 const char **prune
= NULL
;
1460 int i
, prune_num
= 1; /* counting terminating NULL */
1462 if (get_oid("HEAD", &oid
))
1463 die("--merge without HEAD?");
1464 head
= lookup_commit_or_die(&oid
, "HEAD");
1465 if (get_oid("MERGE_HEAD", &oid
))
1466 die("--merge without MERGE_HEAD?");
1467 other
= lookup_commit_or_die(&oid
, "MERGE_HEAD");
1468 add_pending_object(revs
, &head
->object
, "HEAD");
1469 add_pending_object(revs
, &other
->object
, "MERGE_HEAD");
1470 bases
= get_merge_bases(head
, other
);
1471 add_rev_cmdline_list(revs
, bases
, REV_CMD_MERGE_BASE
, UNINTERESTING
| BOTTOM
);
1472 add_pending_commit_list(revs
, bases
, UNINTERESTING
| BOTTOM
);
1473 free_commit_list(bases
);
1474 head
->object
.flags
|= SYMMETRIC_LEFT
;
1478 for (i
= 0; i
< active_nr
; i
++) {
1479 const struct cache_entry
*ce
= active_cache
[i
];
1482 if (ce_path_match(ce
, &revs
->prune_data
, NULL
)) {
1484 REALLOC_ARRAY(prune
, prune_num
);
1485 prune
[prune_num
-2] = ce
->name
;
1486 prune
[prune_num
-1] = NULL
;
1488 while ((i
+1 < active_nr
) &&
1489 ce_same_name(ce
, active_cache
[i
+1]))
1492 clear_pathspec(&revs
->prune_data
);
1493 parse_pathspec(&revs
->prune_data
, PATHSPEC_ALL_MAGIC
& ~PATHSPEC_LITERAL
,
1494 PATHSPEC_PREFER_FULL
| PATHSPEC_LITERAL_PATH
, "", prune
);
1498 static int dotdot_missing(const char *arg
, char *dotdot
,
1499 struct rev_info
*revs
, int symmetric
)
1501 if (revs
->ignore_missing
)
1503 /* de-munge so we report the full argument */
1506 ? "Invalid symmetric difference expression %s"
1507 : "Invalid revision range %s", arg
);
1510 static int handle_dotdot_1(const char *arg
, char *dotdot
,
1511 struct rev_info
*revs
, int flags
,
1512 int cant_be_filename
,
1513 struct object_context
*a_oc
,
1514 struct object_context
*b_oc
)
1516 const char *a_name
, *b_name
;
1517 struct object_id a_oid
, b_oid
;
1518 struct object
*a_obj
, *b_obj
;
1519 unsigned int a_flags
, b_flags
;
1521 unsigned int flags_exclude
= flags
^ (UNINTERESTING
| BOTTOM
);
1522 unsigned int oc_flags
= GET_OID_COMMITTISH
| GET_OID_RECORD_PATH
;
1528 b_name
= dotdot
+ 2;
1529 if (*b_name
== '.') {
1536 if (get_oid_with_context(a_name
, oc_flags
, &a_oid
, a_oc
) ||
1537 get_oid_with_context(b_name
, oc_flags
, &b_oid
, b_oc
))
1540 if (!cant_be_filename
) {
1542 verify_non_filename(revs
->prefix
, arg
);
1546 a_obj
= parse_object(&a_oid
);
1547 b_obj
= parse_object(&b_oid
);
1548 if (!a_obj
|| !b_obj
)
1549 return dotdot_missing(arg
, dotdot
, revs
, symmetric
);
1554 a_flags
= flags_exclude
;
1556 /* A...B -- find merge bases between the two */
1557 struct commit
*a
, *b
;
1558 struct commit_list
*exclude
;
1560 a
= lookup_commit_reference(&a_obj
->oid
);
1561 b
= lookup_commit_reference(&b_obj
->oid
);
1563 return dotdot_missing(arg
, dotdot
, revs
, symmetric
);
1565 exclude
= get_merge_bases(a
, b
);
1566 add_rev_cmdline_list(revs
, exclude
, REV_CMD_MERGE_BASE
,
1568 add_pending_commit_list(revs
, exclude
, flags_exclude
);
1569 free_commit_list(exclude
);
1572 a_flags
= flags
| SYMMETRIC_LEFT
;
1575 a_obj
->flags
|= a_flags
;
1576 b_obj
->flags
|= b_flags
;
1577 add_rev_cmdline(revs
, a_obj
, a_name
, REV_CMD_LEFT
, a_flags
);
1578 add_rev_cmdline(revs
, b_obj
, b_name
, REV_CMD_RIGHT
, b_flags
);
1579 add_pending_object_with_path(revs
, a_obj
, a_name
, a_oc
->mode
, a_oc
->path
);
1580 add_pending_object_with_path(revs
, b_obj
, b_name
, b_oc
->mode
, b_oc
->path
);
1584 static int handle_dotdot(const char *arg
,
1585 struct rev_info
*revs
, int flags
,
1586 int cant_be_filename
)
1588 struct object_context a_oc
, b_oc
;
1589 char *dotdot
= strstr(arg
, "..");
1595 memset(&a_oc
, 0, sizeof(a_oc
));
1596 memset(&b_oc
, 0, sizeof(b_oc
));
1599 ret
= handle_dotdot_1(arg
, dotdot
, revs
, flags
, cant_be_filename
,
1609 int handle_revision_arg(const char *arg_
, struct rev_info
*revs
, int flags
, unsigned revarg_opt
)
1611 struct object_context oc
;
1613 struct object
*object
;
1614 struct object_id oid
;
1616 const char *arg
= arg_
;
1617 int cant_be_filename
= revarg_opt
& REVARG_CANNOT_BE_FILENAME
;
1618 unsigned get_sha1_flags
= GET_OID_RECORD_PATH
;
1620 flags
= flags
& UNINTERESTING
? flags
| BOTTOM
: flags
& ~BOTTOM
;
1622 if (!cant_be_filename
&& !strcmp(arg
, "..")) {
1624 * Just ".."? That is not a range but the
1625 * pathspec for the parent directory.
1630 if (!handle_dotdot(arg
, revs
, flags
, revarg_opt
))
1633 mark
= strstr(arg
, "^@");
1634 if (mark
&& !mark
[2]) {
1636 if (add_parents_only(revs
, arg
, flags
, 0))
1640 mark
= strstr(arg
, "^!");
1641 if (mark
&& !mark
[2]) {
1643 if (!add_parents_only(revs
, arg
, flags
^ (UNINTERESTING
| BOTTOM
), 0))
1646 mark
= strstr(arg
, "^-");
1648 int exclude_parent
= 1;
1652 exclude_parent
= strtoul(mark
+ 2, &end
, 10);
1653 if (*end
!= '\0' || !exclude_parent
)
1658 if (!add_parents_only(revs
, arg
, flags
^ (UNINTERESTING
| BOTTOM
), exclude_parent
))
1664 local_flags
= UNINTERESTING
| BOTTOM
;
1668 if (revarg_opt
& REVARG_COMMITTISH
)
1669 get_sha1_flags
|= GET_OID_COMMITTISH
;
1671 if (get_oid_with_context(arg
, get_sha1_flags
, &oid
, &oc
))
1672 return revs
->ignore_missing
? 0 : -1;
1673 if (!cant_be_filename
)
1674 verify_non_filename(revs
->prefix
, arg
);
1675 object
= get_reference(revs
, arg
, &oid
, flags
^ local_flags
);
1676 add_rev_cmdline(revs
, object
, arg_
, REV_CMD_REV
, flags
^ local_flags
);
1677 add_pending_object_with_path(revs
, object
, arg
, oc
.mode
, oc
.path
);
1682 static void read_pathspec_from_stdin(struct rev_info
*revs
, struct strbuf
*sb
,
1683 struct argv_array
*prune
)
1685 while (strbuf_getline(sb
, stdin
) != EOF
)
1686 argv_array_push(prune
, sb
->buf
);
1689 static void read_revisions_from_stdin(struct rev_info
*revs
,
1690 struct argv_array
*prune
)
1693 int seen_dashdash
= 0;
1696 save_warning
= warn_on_object_refname_ambiguity
;
1697 warn_on_object_refname_ambiguity
= 0;
1699 strbuf_init(&sb
, 1000);
1700 while (strbuf_getline(&sb
, stdin
) != EOF
) {
1704 if (sb
.buf
[0] == '-') {
1705 if (len
== 2 && sb
.buf
[1] == '-') {
1709 die("options not supported in --stdin mode");
1711 if (handle_revision_arg(sb
.buf
, revs
, 0,
1712 REVARG_CANNOT_BE_FILENAME
))
1713 die("bad revision '%s'", sb
.buf
);
1716 read_pathspec_from_stdin(revs
, &sb
, prune
);
1718 strbuf_release(&sb
);
1719 warn_on_object_refname_ambiguity
= save_warning
;
1722 static void add_grep(struct rev_info
*revs
, const char *ptn
, enum grep_pat_token what
)
1724 append_grep_pattern(&revs
->grep_filter
, ptn
, "command line", 0, what
);
1727 static void add_header_grep(struct rev_info
*revs
, enum grep_header_field field
, const char *pattern
)
1729 append_header_grep_pattern(&revs
->grep_filter
, field
, pattern
);
1732 static void add_message_grep(struct rev_info
*revs
, const char *pattern
)
1734 add_grep(revs
, pattern
, GREP_PATTERN_BODY
);
1737 static int handle_revision_opt(struct rev_info
*revs
, int argc
, const char **argv
,
1738 int *unkc
, const char **unkv
)
1740 const char *arg
= argv
[0];
1744 /* pseudo revision arguments */
1745 if (!strcmp(arg
, "--all") || !strcmp(arg
, "--branches") ||
1746 !strcmp(arg
, "--tags") || !strcmp(arg
, "--remotes") ||
1747 !strcmp(arg
, "--reflog") || !strcmp(arg
, "--not") ||
1748 !strcmp(arg
, "--no-walk") || !strcmp(arg
, "--do-walk") ||
1749 !strcmp(arg
, "--bisect") || starts_with(arg
, "--glob=") ||
1750 !strcmp(arg
, "--indexed-objects") ||
1751 starts_with(arg
, "--exclude=") ||
1752 starts_with(arg
, "--branches=") || starts_with(arg
, "--tags=") ||
1753 starts_with(arg
, "--remotes=") || starts_with(arg
, "--no-walk="))
1755 unkv
[(*unkc
)++] = arg
;
1759 if ((argcount
= parse_long_opt("max-count", argv
, &optarg
))) {
1760 revs
->max_count
= atoi(optarg
);
1763 } else if ((argcount
= parse_long_opt("skip", argv
, &optarg
))) {
1764 revs
->skip_count
= atoi(optarg
);
1766 } else if ((*arg
== '-') && isdigit(arg
[1])) {
1767 /* accept -<digit>, like traditional "head" */
1768 if (strtol_i(arg
+ 1, 10, &revs
->max_count
) < 0 ||
1769 revs
->max_count
< 0)
1770 die("'%s': not a non-negative integer", arg
+ 1);
1772 } else if (!strcmp(arg
, "-n")) {
1774 return error("-n requires an argument");
1775 revs
->max_count
= atoi(argv
[1]);
1778 } else if (skip_prefix(arg
, "-n", &optarg
)) {
1779 revs
->max_count
= atoi(optarg
);
1781 } else if ((argcount
= parse_long_opt("max-age", argv
, &optarg
))) {
1782 revs
->max_age
= atoi(optarg
);
1784 } else if ((argcount
= parse_long_opt("since", argv
, &optarg
))) {
1785 revs
->max_age
= approxidate(optarg
);
1787 } else if ((argcount
= parse_long_opt("after", argv
, &optarg
))) {
1788 revs
->max_age
= approxidate(optarg
);
1790 } else if ((argcount
= parse_long_opt("min-age", argv
, &optarg
))) {
1791 revs
->min_age
= atoi(optarg
);
1793 } else if ((argcount
= parse_long_opt("before", argv
, &optarg
))) {
1794 revs
->min_age
= approxidate(optarg
);
1796 } else if ((argcount
= parse_long_opt("until", argv
, &optarg
))) {
1797 revs
->min_age
= approxidate(optarg
);
1799 } else if (!strcmp(arg
, "--first-parent")) {
1800 revs
->first_parent_only
= 1;
1801 } else if (!strcmp(arg
, "--ancestry-path")) {
1802 revs
->ancestry_path
= 1;
1803 revs
->simplify_history
= 0;
1805 } else if (!strcmp(arg
, "-g") || !strcmp(arg
, "--walk-reflogs")) {
1806 init_reflog_walk(&revs
->reflog_info
);
1807 } else if (!strcmp(arg
, "--default")) {
1809 return error("bad --default argument");
1810 revs
->def
= argv
[1];
1812 } else if (!strcmp(arg
, "--merge")) {
1813 revs
->show_merge
= 1;
1814 } else if (!strcmp(arg
, "--topo-order")) {
1815 revs
->sort_order
= REV_SORT_IN_GRAPH_ORDER
;
1816 revs
->topo_order
= 1;
1817 } else if (!strcmp(arg
, "--simplify-merges")) {
1818 revs
->simplify_merges
= 1;
1819 revs
->topo_order
= 1;
1820 revs
->rewrite_parents
= 1;
1821 revs
->simplify_history
= 0;
1823 } else if (!strcmp(arg
, "--simplify-by-decoration")) {
1824 revs
->simplify_merges
= 1;
1825 revs
->topo_order
= 1;
1826 revs
->rewrite_parents
= 1;
1827 revs
->simplify_history
= 0;
1828 revs
->simplify_by_decoration
= 1;
1831 load_ref_decorations(NULL
, DECORATE_SHORT_REFS
);
1832 } else if (!strcmp(arg
, "--date-order")) {
1833 revs
->sort_order
= REV_SORT_BY_COMMIT_DATE
;
1834 revs
->topo_order
= 1;
1835 } else if (!strcmp(arg
, "--author-date-order")) {
1836 revs
->sort_order
= REV_SORT_BY_AUTHOR_DATE
;
1837 revs
->topo_order
= 1;
1838 } else if (!strcmp(arg
, "--early-output")) {
1839 revs
->early_output
= 100;
1840 revs
->topo_order
= 1;
1841 } else if (skip_prefix(arg
, "--early-output=", &optarg
)) {
1842 if (strtoul_ui(optarg
, 10, &revs
->early_output
) < 0)
1843 die("'%s': not a non-negative integer", optarg
);
1844 revs
->topo_order
= 1;
1845 } else if (!strcmp(arg
, "--parents")) {
1846 revs
->rewrite_parents
= 1;
1847 revs
->print_parents
= 1;
1848 } else if (!strcmp(arg
, "--dense")) {
1850 } else if (!strcmp(arg
, "--sparse")) {
1852 } else if (!strcmp(arg
, "--in-commit-order")) {
1853 revs
->tree_blobs_in_commit_order
= 1;
1854 } else if (!strcmp(arg
, "--remove-empty")) {
1855 revs
->remove_empty_trees
= 1;
1856 } else if (!strcmp(arg
, "--merges")) {
1857 revs
->min_parents
= 2;
1858 } else if (!strcmp(arg
, "--no-merges")) {
1859 revs
->max_parents
= 1;
1860 } else if (skip_prefix(arg
, "--min-parents=", &optarg
)) {
1861 revs
->min_parents
= atoi(optarg
);
1862 } else if (!strcmp(arg
, "--no-min-parents")) {
1863 revs
->min_parents
= 0;
1864 } else if (skip_prefix(arg
, "--max-parents=", &optarg
)) {
1865 revs
->max_parents
= atoi(optarg
);
1866 } else if (!strcmp(arg
, "--no-max-parents")) {
1867 revs
->max_parents
= -1;
1868 } else if (!strcmp(arg
, "--boundary")) {
1870 } else if (!strcmp(arg
, "--left-right")) {
1871 revs
->left_right
= 1;
1872 } else if (!strcmp(arg
, "--left-only")) {
1873 if (revs
->right_only
)
1874 die("--left-only is incompatible with --right-only"
1876 revs
->left_only
= 1;
1877 } else if (!strcmp(arg
, "--right-only")) {
1878 if (revs
->left_only
)
1879 die("--right-only is incompatible with --left-only");
1880 revs
->right_only
= 1;
1881 } else if (!strcmp(arg
, "--cherry")) {
1882 if (revs
->left_only
)
1883 die("--cherry is incompatible with --left-only");
1884 revs
->cherry_mark
= 1;
1885 revs
->right_only
= 1;
1886 revs
->max_parents
= 1;
1888 } else if (!strcmp(arg
, "--count")) {
1890 } else if (!strcmp(arg
, "--cherry-mark")) {
1891 if (revs
->cherry_pick
)
1892 die("--cherry-mark is incompatible with --cherry-pick");
1893 revs
->cherry_mark
= 1;
1894 revs
->limited
= 1; /* needs limit_list() */
1895 } else if (!strcmp(arg
, "--cherry-pick")) {
1896 if (revs
->cherry_mark
)
1897 die("--cherry-pick is incompatible with --cherry-mark");
1898 revs
->cherry_pick
= 1;
1900 } else if (!strcmp(arg
, "--objects")) {
1901 revs
->tag_objects
= 1;
1902 revs
->tree_objects
= 1;
1903 revs
->blob_objects
= 1;
1904 } else if (!strcmp(arg
, "--objects-edge")) {
1905 revs
->tag_objects
= 1;
1906 revs
->tree_objects
= 1;
1907 revs
->blob_objects
= 1;
1908 revs
->edge_hint
= 1;
1909 } else if (!strcmp(arg
, "--objects-edge-aggressive")) {
1910 revs
->tag_objects
= 1;
1911 revs
->tree_objects
= 1;
1912 revs
->blob_objects
= 1;
1913 revs
->edge_hint
= 1;
1914 revs
->edge_hint_aggressive
= 1;
1915 } else if (!strcmp(arg
, "--verify-objects")) {
1916 revs
->tag_objects
= 1;
1917 revs
->tree_objects
= 1;
1918 revs
->blob_objects
= 1;
1919 revs
->verify_objects
= 1;
1920 } else if (!strcmp(arg
, "--unpacked")) {
1922 } else if (starts_with(arg
, "--unpacked=")) {
1923 die("--unpacked=<packfile> no longer supported.");
1924 } else if (!strcmp(arg
, "-r")) {
1926 revs
->diffopt
.flags
.recursive
= 1;
1927 } else if (!strcmp(arg
, "-t")) {
1929 revs
->diffopt
.flags
.recursive
= 1;
1930 revs
->diffopt
.flags
.tree_in_recursive
= 1;
1931 } else if (!strcmp(arg
, "-m")) {
1932 revs
->ignore_merges
= 0;
1933 } else if (!strcmp(arg
, "-c")) {
1935 revs
->dense_combined_merges
= 0;
1936 revs
->combine_merges
= 1;
1937 } else if (!strcmp(arg
, "--cc")) {
1939 revs
->dense_combined_merges
= 1;
1940 revs
->combine_merges
= 1;
1941 } else if (!strcmp(arg
, "-v")) {
1942 revs
->verbose_header
= 1;
1943 } else if (!strcmp(arg
, "--pretty")) {
1944 revs
->verbose_header
= 1;
1945 revs
->pretty_given
= 1;
1946 get_commit_format(NULL
, revs
);
1947 } else if (skip_prefix(arg
, "--pretty=", &optarg
) ||
1948 skip_prefix(arg
, "--format=", &optarg
)) {
1950 * Detached form ("--pretty X" as opposed to "--pretty=X")
1951 * not allowed, since the argument is optional.
1953 revs
->verbose_header
= 1;
1954 revs
->pretty_given
= 1;
1955 get_commit_format(optarg
, revs
);
1956 } else if (!strcmp(arg
, "--expand-tabs")) {
1957 revs
->expand_tabs_in_log
= 8;
1958 } else if (!strcmp(arg
, "--no-expand-tabs")) {
1959 revs
->expand_tabs_in_log
= 0;
1960 } else if (skip_prefix(arg
, "--expand-tabs=", &arg
)) {
1962 if (strtol_i(arg
, 10, &val
) < 0 || val
< 0)
1963 die("'%s': not a non-negative integer", arg
);
1964 revs
->expand_tabs_in_log
= val
;
1965 } else if (!strcmp(arg
, "--show-notes") || !strcmp(arg
, "--notes")) {
1966 revs
->show_notes
= 1;
1967 revs
->show_notes_given
= 1;
1968 revs
->notes_opt
.use_default_notes
= 1;
1969 } else if (!strcmp(arg
, "--show-signature")) {
1970 revs
->show_signature
= 1;
1971 } else if (!strcmp(arg
, "--no-show-signature")) {
1972 revs
->show_signature
= 0;
1973 } else if (!strcmp(arg
, "--show-linear-break")) {
1974 revs
->break_bar
= " ..........";
1975 revs
->track_linear
= 1;
1976 revs
->track_first_time
= 1;
1977 } else if (skip_prefix(arg
, "--show-linear-break=", &optarg
)) {
1978 revs
->break_bar
= xstrdup(optarg
);
1979 revs
->track_linear
= 1;
1980 revs
->track_first_time
= 1;
1981 } else if (skip_prefix(arg
, "--show-notes=", &optarg
) ||
1982 skip_prefix(arg
, "--notes=", &optarg
)) {
1983 struct strbuf buf
= STRBUF_INIT
;
1984 revs
->show_notes
= 1;
1985 revs
->show_notes_given
= 1;
1986 if (starts_with(arg
, "--show-notes=") &&
1987 revs
->notes_opt
.use_default_notes
< 0)
1988 revs
->notes_opt
.use_default_notes
= 1;
1989 strbuf_addstr(&buf
, optarg
);
1990 expand_notes_ref(&buf
);
1991 string_list_append(&revs
->notes_opt
.extra_notes_refs
,
1992 strbuf_detach(&buf
, NULL
));
1993 } else if (!strcmp(arg
, "--no-notes")) {
1994 revs
->show_notes
= 0;
1995 revs
->show_notes_given
= 1;
1996 revs
->notes_opt
.use_default_notes
= -1;
1997 /* we have been strdup'ing ourselves, so trick
1998 * string_list into free()ing strings */
1999 revs
->notes_opt
.extra_notes_refs
.strdup_strings
= 1;
2000 string_list_clear(&revs
->notes_opt
.extra_notes_refs
, 0);
2001 revs
->notes_opt
.extra_notes_refs
.strdup_strings
= 0;
2002 } else if (!strcmp(arg
, "--standard-notes")) {
2003 revs
->show_notes_given
= 1;
2004 revs
->notes_opt
.use_default_notes
= 1;
2005 } else if (!strcmp(arg
, "--no-standard-notes")) {
2006 revs
->notes_opt
.use_default_notes
= 0;
2007 } else if (!strcmp(arg
, "--oneline")) {
2008 revs
->verbose_header
= 1;
2009 get_commit_format("oneline", revs
);
2010 revs
->pretty_given
= 1;
2011 revs
->abbrev_commit
= 1;
2012 } else if (!strcmp(arg
, "--graph")) {
2013 revs
->topo_order
= 1;
2014 revs
->rewrite_parents
= 1;
2015 revs
->graph
= graph_init(revs
);
2016 } else if (!strcmp(arg
, "--root")) {
2017 revs
->show_root_diff
= 1;
2018 } else if (!strcmp(arg
, "--no-commit-id")) {
2019 revs
->no_commit_id
= 1;
2020 } else if (!strcmp(arg
, "--always")) {
2021 revs
->always_show_header
= 1;
2022 } else if (!strcmp(arg
, "--no-abbrev")) {
2024 } else if (!strcmp(arg
, "--abbrev")) {
2025 revs
->abbrev
= DEFAULT_ABBREV
;
2026 } else if (skip_prefix(arg
, "--abbrev=", &optarg
)) {
2027 revs
->abbrev
= strtoul(optarg
, NULL
, 10);
2028 if (revs
->abbrev
< MINIMUM_ABBREV
)
2029 revs
->abbrev
= MINIMUM_ABBREV
;
2030 else if (revs
->abbrev
> 40)
2032 } else if (!strcmp(arg
, "--abbrev-commit")) {
2033 revs
->abbrev_commit
= 1;
2034 revs
->abbrev_commit_given
= 1;
2035 } else if (!strcmp(arg
, "--no-abbrev-commit")) {
2036 revs
->abbrev_commit
= 0;
2037 } else if (!strcmp(arg
, "--full-diff")) {
2039 revs
->full_diff
= 1;
2040 } else if (!strcmp(arg
, "--full-history")) {
2041 revs
->simplify_history
= 0;
2042 } else if (!strcmp(arg
, "--relative-date")) {
2043 revs
->date_mode
.type
= DATE_RELATIVE
;
2044 revs
->date_mode_explicit
= 1;
2045 } else if ((argcount
= parse_long_opt("date", argv
, &optarg
))) {
2046 parse_date_format(optarg
, &revs
->date_mode
);
2047 revs
->date_mode_explicit
= 1;
2049 } else if (!strcmp(arg
, "--log-size")) {
2050 revs
->show_log_size
= 1;
2053 * Grepping the commit log
2055 else if ((argcount
= parse_long_opt("author", argv
, &optarg
))) {
2056 add_header_grep(revs
, GREP_HEADER_AUTHOR
, optarg
);
2058 } else if ((argcount
= parse_long_opt("committer", argv
, &optarg
))) {
2059 add_header_grep(revs
, GREP_HEADER_COMMITTER
, optarg
);
2061 } else if ((argcount
= parse_long_opt("grep-reflog", argv
, &optarg
))) {
2062 add_header_grep(revs
, GREP_HEADER_REFLOG
, optarg
);
2064 } else if ((argcount
= parse_long_opt("grep", argv
, &optarg
))) {
2065 add_message_grep(revs
, optarg
);
2067 } else if (!strcmp(arg
, "--grep-debug")) {
2068 revs
->grep_filter
.debug
= 1;
2069 } else if (!strcmp(arg
, "--basic-regexp")) {
2070 revs
->grep_filter
.pattern_type_option
= GREP_PATTERN_TYPE_BRE
;
2071 } else if (!strcmp(arg
, "--extended-regexp") || !strcmp(arg
, "-E")) {
2072 revs
->grep_filter
.pattern_type_option
= GREP_PATTERN_TYPE_ERE
;
2073 } else if (!strcmp(arg
, "--regexp-ignore-case") || !strcmp(arg
, "-i")) {
2074 revs
->grep_filter
.ignore_case
= 1;
2075 revs
->diffopt
.flags
.pickaxe_ignore_case
= 1;
2076 } else if (!strcmp(arg
, "--fixed-strings") || !strcmp(arg
, "-F")) {
2077 revs
->grep_filter
.pattern_type_option
= GREP_PATTERN_TYPE_FIXED
;
2078 } else if (!strcmp(arg
, "--perl-regexp") || !strcmp(arg
, "-P")) {
2079 revs
->grep_filter
.pattern_type_option
= GREP_PATTERN_TYPE_PCRE
;
2080 } else if (!strcmp(arg
, "--all-match")) {
2081 revs
->grep_filter
.all_match
= 1;
2082 } else if (!strcmp(arg
, "--invert-grep")) {
2083 revs
->invert_grep
= 1;
2084 } else if ((argcount
= parse_long_opt("encoding", argv
, &optarg
))) {
2085 if (strcmp(optarg
, "none"))
2086 git_log_output_encoding
= xstrdup(optarg
);
2088 git_log_output_encoding
= "";
2090 } else if (!strcmp(arg
, "--reverse")) {
2092 } else if (!strcmp(arg
, "--children")) {
2093 revs
->children
.name
= "children";
2095 } else if (!strcmp(arg
, "--ignore-missing")) {
2096 revs
->ignore_missing
= 1;
2098 int opts
= diff_opt_parse(&revs
->diffopt
, argv
, argc
, revs
->prefix
);
2100 unkv
[(*unkc
)++] = arg
;
2103 if (revs
->graph
&& revs
->track_linear
)
2104 die("--show-linear-break and --graph are incompatible");
2109 void parse_revision_opt(struct rev_info
*revs
, struct parse_opt_ctx_t
*ctx
,
2110 const struct option
*options
,
2111 const char * const usagestr
[])
2113 int n
= handle_revision_opt(revs
, ctx
->argc
, ctx
->argv
,
2114 &ctx
->cpidx
, ctx
->out
);
2116 error("unknown option `%s'", ctx
->argv
[0]);
2117 usage_with_options(usagestr
, options
);
2123 static int for_each_bisect_ref(struct ref_store
*refs
, each_ref_fn fn
,
2124 void *cb_data
, const char *term
)
2126 struct strbuf bisect_refs
= STRBUF_INIT
;
2128 strbuf_addf(&bisect_refs
, "refs/bisect/%s", term
);
2129 status
= refs_for_each_fullref_in(refs
, bisect_refs
.buf
, fn
, cb_data
, 0);
2130 strbuf_release(&bisect_refs
);
2134 static int for_each_bad_bisect_ref(struct ref_store
*refs
, each_ref_fn fn
, void *cb_data
)
2136 return for_each_bisect_ref(refs
, fn
, cb_data
, term_bad
);
2139 static int for_each_good_bisect_ref(struct ref_store
*refs
, each_ref_fn fn
, void *cb_data
)
2141 return for_each_bisect_ref(refs
, fn
, cb_data
, term_good
);
2144 static int handle_revision_pseudo_opt(const char *submodule
,
2145 struct rev_info
*revs
,
2146 int argc
, const char **argv
, int *flags
)
2148 const char *arg
= argv
[0];
2150 struct ref_store
*refs
;
2155 * We need some something like get_submodule_worktrees()
2156 * before we can go through all worktrees of a submodule,
2157 * .e.g with adding all HEADs from --all, which is not
2158 * supported right now, so stick to single worktree.
2160 if (!revs
->single_worktree
)
2161 die("BUG: --single-worktree cannot be used together with submodule");
2162 refs
= get_submodule_ref_store(submodule
);
2164 refs
= get_main_ref_store();
2169 * Commands like "git shortlog" will not accept the options below
2170 * unless parse_revision_opt queues them (as opposed to erroring
2173 * When implementing your new pseudo-option, remember to
2174 * register it in the list at the top of handle_revision_opt.
2176 if (!strcmp(arg
, "--all")) {
2177 handle_refs(refs
, revs
, *flags
, refs_for_each_ref
);
2178 handle_refs(refs
, revs
, *flags
, refs_head_ref
);
2179 if (!revs
->single_worktree
) {
2180 struct all_refs_cb cb
;
2182 init_all_refs_cb(&cb
, revs
, *flags
);
2183 other_head_refs(handle_one_ref
, &cb
);
2185 clear_ref_exclusion(&revs
->ref_excludes
);
2186 } else if (!strcmp(arg
, "--branches")) {
2187 handle_refs(refs
, revs
, *flags
, refs_for_each_branch_ref
);
2188 clear_ref_exclusion(&revs
->ref_excludes
);
2189 } else if (!strcmp(arg
, "--bisect")) {
2190 read_bisect_terms(&term_bad
, &term_good
);
2191 handle_refs(refs
, revs
, *flags
, for_each_bad_bisect_ref
);
2192 handle_refs(refs
, revs
, *flags
^ (UNINTERESTING
| BOTTOM
),
2193 for_each_good_bisect_ref
);
2195 } else if (!strcmp(arg
, "--tags")) {
2196 handle_refs(refs
, revs
, *flags
, refs_for_each_tag_ref
);
2197 clear_ref_exclusion(&revs
->ref_excludes
);
2198 } else if (!strcmp(arg
, "--remotes")) {
2199 handle_refs(refs
, revs
, *flags
, refs_for_each_remote_ref
);
2200 clear_ref_exclusion(&revs
->ref_excludes
);
2201 } else if ((argcount
= parse_long_opt("glob", argv
, &optarg
))) {
2202 struct all_refs_cb cb
;
2203 init_all_refs_cb(&cb
, revs
, *flags
);
2204 for_each_glob_ref(handle_one_ref
, optarg
, &cb
);
2205 clear_ref_exclusion(&revs
->ref_excludes
);
2207 } else if ((argcount
= parse_long_opt("exclude", argv
, &optarg
))) {
2208 add_ref_exclusion(&revs
->ref_excludes
, optarg
);
2210 } else if (skip_prefix(arg
, "--branches=", &optarg
)) {
2211 struct all_refs_cb cb
;
2212 init_all_refs_cb(&cb
, revs
, *flags
);
2213 for_each_glob_ref_in(handle_one_ref
, optarg
, "refs/heads/", &cb
);
2214 clear_ref_exclusion(&revs
->ref_excludes
);
2215 } else if (skip_prefix(arg
, "--tags=", &optarg
)) {
2216 struct all_refs_cb cb
;
2217 init_all_refs_cb(&cb
, revs
, *flags
);
2218 for_each_glob_ref_in(handle_one_ref
, optarg
, "refs/tags/", &cb
);
2219 clear_ref_exclusion(&revs
->ref_excludes
);
2220 } else if (skip_prefix(arg
, "--remotes=", &optarg
)) {
2221 struct all_refs_cb cb
;
2222 init_all_refs_cb(&cb
, revs
, *flags
);
2223 for_each_glob_ref_in(handle_one_ref
, optarg
, "refs/remotes/", &cb
);
2224 clear_ref_exclusion(&revs
->ref_excludes
);
2225 } else if (!strcmp(arg
, "--reflog")) {
2226 add_reflogs_to_pending(revs
, *flags
);
2227 } else if (!strcmp(arg
, "--indexed-objects")) {
2228 add_index_objects_to_pending(revs
, *flags
);
2229 } else if (!strcmp(arg
, "--not")) {
2230 *flags
^= UNINTERESTING
| BOTTOM
;
2231 } else if (!strcmp(arg
, "--no-walk")) {
2232 revs
->no_walk
= REVISION_WALK_NO_WALK_SORTED
;
2233 } else if (skip_prefix(arg
, "--no-walk=", &optarg
)) {
2235 * Detached form ("--no-walk X" as opposed to "--no-walk=X")
2236 * not allowed, since the argument is optional.
2238 if (!strcmp(optarg
, "sorted"))
2239 revs
->no_walk
= REVISION_WALK_NO_WALK_SORTED
;
2240 else if (!strcmp(optarg
, "unsorted"))
2241 revs
->no_walk
= REVISION_WALK_NO_WALK_UNSORTED
;
2243 return error("invalid argument to --no-walk");
2244 } else if (!strcmp(arg
, "--do-walk")) {
2246 } else if (!strcmp(arg
, "--single-worktree")) {
2247 revs
->single_worktree
= 1;
2255 static void NORETURN
diagnose_missing_default(const char *def
)
2258 const char *refname
;
2260 refname
= resolve_ref_unsafe(def
, 0, NULL
, &flags
);
2261 if (!refname
|| !(flags
& REF_ISSYMREF
) || (flags
& REF_ISBROKEN
))
2262 die(_("your current branch appears to be broken"));
2264 skip_prefix(refname
, "refs/heads/", &refname
);
2265 die(_("your current branch '%s' does not have any commits yet"),
2270 * Parse revision information, filling in the "rev_info" structure,
2271 * and removing the used arguments from the argument list.
2273 * Returns the number of arguments left that weren't recognized
2274 * (which are also moved to the head of the argument list)
2276 int setup_revisions(int argc
, const char **argv
, struct rev_info
*revs
, struct setup_revision_opt
*opt
)
2278 int i
, flags
, left
, seen_dashdash
, read_from_stdin
, got_rev_arg
= 0, revarg_opt
;
2279 struct argv_array prune_data
= ARGV_ARRAY_INIT
;
2280 const char *submodule
= NULL
;
2283 submodule
= opt
->submodule
;
2285 /* First, search for "--" */
2286 if (opt
&& opt
->assume_dashdash
) {
2290 for (i
= 1; i
< argc
; i
++) {
2291 const char *arg
= argv
[i
];
2292 if (strcmp(arg
, "--"))
2297 argv_array_pushv(&prune_data
, argv
+ i
+ 1);
2303 /* Second, deal with arguments and options */
2305 revarg_opt
= opt
? opt
->revarg_opt
: 0;
2307 revarg_opt
|= REVARG_CANNOT_BE_FILENAME
;
2308 read_from_stdin
= 0;
2309 for (left
= i
= 1; i
< argc
; i
++) {
2310 const char *arg
= argv
[i
];
2314 opts
= handle_revision_pseudo_opt(submodule
,
2315 revs
, argc
- i
, argv
+ i
,
2322 if (!strcmp(arg
, "--stdin")) {
2323 if (revs
->disable_stdin
) {
2327 if (read_from_stdin
++)
2328 die("--stdin given twice?");
2329 read_revisions_from_stdin(revs
, &prune_data
);
2333 opts
= handle_revision_opt(revs
, argc
- i
, argv
+ i
, &left
, argv
);
2344 if (handle_revision_arg(arg
, revs
, flags
, revarg_opt
)) {
2346 if (seen_dashdash
|| *arg
== '^')
2347 die("bad revision '%s'", arg
);
2349 /* If we didn't have a "--":
2350 * (1) all filenames must exist;
2351 * (2) all rev-args must not be interpretable
2352 * as a valid filename.
2353 * but the latter we have checked in the main loop.
2355 for (j
= i
; j
< argc
; j
++)
2356 verify_filename(revs
->prefix
, argv
[j
], j
== i
);
2358 argv_array_pushv(&prune_data
, argv
+ i
);
2365 if (prune_data
.argc
) {
2367 * If we need to introduce the magic "a lone ':' means no
2368 * pathspec whatsoever", here is the place to do so.
2370 * if (prune_data.nr == 1 && !strcmp(prune_data[0], ":")) {
2371 * prune_data.nr = 0;
2372 * prune_data.alloc = 0;
2373 * free(prune_data.path);
2374 * prune_data.path = NULL;
2376 * terminate prune_data.alloc with NULL and
2377 * call init_pathspec() to set revs->prune_data here.
2380 parse_pathspec(&revs
->prune_data
, 0, 0,
2381 revs
->prefix
, prune_data
.argv
);
2383 argv_array_clear(&prune_data
);
2385 if (revs
->def
== NULL
)
2386 revs
->def
= opt
? opt
->def
: NULL
;
2387 if (opt
&& opt
->tweak
)
2388 opt
->tweak(revs
, opt
);
2389 if (revs
->show_merge
)
2390 prepare_show_merge(revs
);
2391 if (revs
->def
&& !revs
->pending
.nr
&& !revs
->rev_input_given
&& !got_rev_arg
) {
2392 struct object_id oid
;
2393 struct object
*object
;
2394 struct object_context oc
;
2395 if (get_oid_with_context(revs
->def
, 0, &oid
, &oc
))
2396 diagnose_missing_default(revs
->def
);
2397 object
= get_reference(revs
, revs
->def
, &oid
, 0);
2398 add_pending_object_with_mode(revs
, object
, revs
->def
, oc
.mode
);
2401 /* Did the user ask for any diff output? Run the diff! */
2402 if (revs
->diffopt
.output_format
& ~DIFF_FORMAT_NO_OUTPUT
)
2405 /* Pickaxe, diff-filter and rename following need diffs */
2406 if (revs
->diffopt
.pickaxe
||
2407 revs
->diffopt
.filter
||
2408 revs
->diffopt
.flags
.follow_renames
)
2411 if (revs
->topo_order
)
2414 if (revs
->prune_data
.nr
) {
2415 copy_pathspec(&revs
->pruning
.pathspec
, &revs
->prune_data
);
2416 /* Can't prune commits with rename following: the paths change.. */
2417 if (!revs
->diffopt
.flags
.follow_renames
)
2419 if (!revs
->full_diff
)
2420 copy_pathspec(&revs
->diffopt
.pathspec
,
2423 if (revs
->combine_merges
)
2424 revs
->ignore_merges
= 0;
2425 revs
->diffopt
.abbrev
= revs
->abbrev
;
2427 if (revs
->line_level_traverse
) {
2429 revs
->topo_order
= 1;
2432 diff_setup_done(&revs
->diffopt
);
2434 grep_commit_pattern_type(GREP_PATTERN_TYPE_UNSPECIFIED
,
2435 &revs
->grep_filter
);
2436 compile_grep_patterns(&revs
->grep_filter
);
2438 if (revs
->reverse
&& revs
->reflog_info
)
2439 die("cannot combine --reverse with --walk-reflogs");
2440 if (revs
->reflog_info
&& revs
->limited
)
2441 die("cannot combine --walk-reflogs with history-limiting options");
2442 if (revs
->rewrite_parents
&& revs
->children
.name
)
2443 die("cannot combine --parents and --children");
2446 * Limitations on the graph functionality
2448 if (revs
->reverse
&& revs
->graph
)
2449 die("cannot combine --reverse with --graph");
2451 if (revs
->reflog_info
&& revs
->graph
)
2452 die("cannot combine --walk-reflogs with --graph");
2453 if (revs
->no_walk
&& revs
->graph
)
2454 die("cannot combine --no-walk with --graph");
2455 if (!revs
->reflog_info
&& revs
->grep_filter
.use_reflog_filter
)
2456 die("cannot use --grep-reflog without --walk-reflogs");
2458 if (revs
->first_parent_only
&& revs
->bisect
)
2459 die(_("--first-parent is incompatible with --bisect"));
2461 if (revs
->expand_tabs_in_log
< 0)
2462 revs
->expand_tabs_in_log
= revs
->expand_tabs_in_log_default
;
2467 static void add_child(struct rev_info
*revs
, struct commit
*parent
, struct commit
*child
)
2469 struct commit_list
*l
= xcalloc(1, sizeof(*l
));
2472 l
->next
= add_decoration(&revs
->children
, &parent
->object
, l
);
2475 static int remove_duplicate_parents(struct rev_info
*revs
, struct commit
*commit
)
2477 struct treesame_state
*ts
= lookup_decoration(&revs
->treesame
, &commit
->object
);
2478 struct commit_list
**pp
, *p
;
2479 int surviving_parents
;
2481 /* Examine existing parents while marking ones we have seen... */
2482 pp
= &commit
->parents
;
2483 surviving_parents
= 0;
2484 while ((p
= *pp
) != NULL
) {
2485 struct commit
*parent
= p
->item
;
2486 if (parent
->object
.flags
& TMP_MARK
) {
2489 compact_treesame(revs
, commit
, surviving_parents
);
2492 parent
->object
.flags
|= TMP_MARK
;
2493 surviving_parents
++;
2496 /* clear the temporary mark */
2497 for (p
= commit
->parents
; p
; p
= p
->next
) {
2498 p
->item
->object
.flags
&= ~TMP_MARK
;
2500 /* no update_treesame() - removing duplicates can't affect TREESAME */
2501 return surviving_parents
;
2504 struct merge_simplify_state
{
2505 struct commit
*simplified
;
2508 static struct merge_simplify_state
*locate_simplify_state(struct rev_info
*revs
, struct commit
*commit
)
2510 struct merge_simplify_state
*st
;
2512 st
= lookup_decoration(&revs
->merge_simplification
, &commit
->object
);
2514 st
= xcalloc(1, sizeof(*st
));
2515 add_decoration(&revs
->merge_simplification
, &commit
->object
, st
);
2520 static int mark_redundant_parents(struct rev_info
*revs
, struct commit
*commit
)
2522 struct commit_list
*h
= reduce_heads(commit
->parents
);
2523 int i
= 0, marked
= 0;
2524 struct commit_list
*po
, *pn
;
2526 /* Want these for sanity-checking only */
2527 int orig_cnt
= commit_list_count(commit
->parents
);
2528 int cnt
= commit_list_count(h
);
2531 * Not ready to remove items yet, just mark them for now, based
2532 * on the output of reduce_heads(). reduce_heads outputs the reduced
2533 * set in its original order, so this isn't too hard.
2535 po
= commit
->parents
;
2538 if (pn
&& po
->item
== pn
->item
) {
2542 po
->item
->object
.flags
|= TMP_MARK
;
2548 if (i
!= cnt
|| cnt
+marked
!= orig_cnt
)
2549 die("mark_redundant_parents %d %d %d %d", orig_cnt
, cnt
, i
, marked
);
2551 free_commit_list(h
);
2556 static int mark_treesame_root_parents(struct rev_info
*revs
, struct commit
*commit
)
2558 struct commit_list
*p
;
2561 for (p
= commit
->parents
; p
; p
= p
->next
) {
2562 struct commit
*parent
= p
->item
;
2563 if (!parent
->parents
&& (parent
->object
.flags
& TREESAME
)) {
2564 parent
->object
.flags
|= TMP_MARK
;
2573 * Awkward naming - this means one parent we are TREESAME to.
2574 * cf mark_treesame_root_parents: root parents that are TREESAME (to an
2575 * empty tree). Better name suggestions?
2577 static int leave_one_treesame_to_parent(struct rev_info
*revs
, struct commit
*commit
)
2579 struct treesame_state
*ts
= lookup_decoration(&revs
->treesame
, &commit
->object
);
2580 struct commit
*unmarked
= NULL
, *marked
= NULL
;
2581 struct commit_list
*p
;
2584 for (p
= commit
->parents
, n
= 0; p
; p
= p
->next
, n
++) {
2585 if (ts
->treesame
[n
]) {
2586 if (p
->item
->object
.flags
& TMP_MARK
) {
2599 * If we are TREESAME to a marked-for-deletion parent, but not to any
2600 * unmarked parents, unmark the first TREESAME parent. This is the
2601 * parent that the default simplify_history==1 scan would have followed,
2602 * and it doesn't make sense to omit that path when asking for a
2603 * simplified full history. Retaining it improves the chances of
2604 * understanding odd missed merges that took an old version of a file.
2608 * I--------*X A modified the file, but mainline merge X used
2609 * \ / "-s ours", so took the version from I. X is
2610 * `-*A--' TREESAME to I and !TREESAME to A.
2612 * Default log from X would produce "I". Without this check,
2613 * --full-history --simplify-merges would produce "I-A-X", showing
2614 * the merge commit X and that it changed A, but not making clear that
2615 * it had just taken the I version. With this check, the topology above
2618 * Note that it is possible that the simplification chooses a different
2619 * TREESAME parent from the default, in which case this test doesn't
2620 * activate, and we _do_ drop the default parent. Example:
2622 * I------X A modified the file, but it was reverted in B,
2623 * \ / meaning mainline merge X is TREESAME to both
2626 * Default log would produce "I" by following the first parent;
2627 * --full-history --simplify-merges will produce "I-A-B". But this is a
2628 * reasonable result - it presents a logical full history leading from
2629 * I to X, and X is not an important merge.
2631 if (!unmarked
&& marked
) {
2632 marked
->object
.flags
&= ~TMP_MARK
;
2639 static int remove_marked_parents(struct rev_info
*revs
, struct commit
*commit
)
2641 struct commit_list
**pp
, *p
;
2642 int nth_parent
, removed
= 0;
2644 pp
= &commit
->parents
;
2646 while ((p
= *pp
) != NULL
) {
2647 struct commit
*parent
= p
->item
;
2648 if (parent
->object
.flags
& TMP_MARK
) {
2649 parent
->object
.flags
&= ~TMP_MARK
;
2653 compact_treesame(revs
, commit
, nth_parent
);
2660 /* Removing parents can only increase TREESAMEness */
2661 if (removed
&& !(commit
->object
.flags
& TREESAME
))
2662 update_treesame(revs
, commit
);
2667 static struct commit_list
**simplify_one(struct rev_info
*revs
, struct commit
*commit
, struct commit_list
**tail
)
2669 struct commit_list
*p
;
2670 struct commit
*parent
;
2671 struct merge_simplify_state
*st
, *pst
;
2674 st
= locate_simplify_state(revs
, commit
);
2677 * Have we handled this one?
2683 * An UNINTERESTING commit simplifies to itself, so does a
2684 * root commit. We do not rewrite parents of such commit
2687 if ((commit
->object
.flags
& UNINTERESTING
) || !commit
->parents
) {
2688 st
->simplified
= commit
;
2693 * Do we know what commit all of our parents that matter
2694 * should be rewritten to? Otherwise we are not ready to
2695 * rewrite this one yet.
2697 for (cnt
= 0, p
= commit
->parents
; p
; p
= p
->next
) {
2698 pst
= locate_simplify_state(revs
, p
->item
);
2699 if (!pst
->simplified
) {
2700 tail
= &commit_list_insert(p
->item
, tail
)->next
;
2703 if (revs
->first_parent_only
)
2707 tail
= &commit_list_insert(commit
, tail
)->next
;
2712 * Rewrite our list of parents. Note that this cannot
2713 * affect our TREESAME flags in any way - a commit is
2714 * always TREESAME to its simplification.
2716 for (p
= commit
->parents
; p
; p
= p
->next
) {
2717 pst
= locate_simplify_state(revs
, p
->item
);
2718 p
->item
= pst
->simplified
;
2719 if (revs
->first_parent_only
)
2723 if (revs
->first_parent_only
)
2726 cnt
= remove_duplicate_parents(revs
, commit
);
2729 * It is possible that we are a merge and one side branch
2730 * does not have any commit that touches the given paths;
2731 * in such a case, the immediate parent from that branch
2732 * will be rewritten to be the merge base.
2734 * o----X X: the commit we are looking at;
2735 * / / o: a commit that touches the paths;
2738 * Further, a merge of an independent branch that doesn't
2739 * touch the path will reduce to a treesame root parent:
2741 * ----o----X X: the commit we are looking at;
2742 * / o: a commit that touches the paths;
2743 * r r: a root commit not touching the paths
2745 * Detect and simplify both cases.
2748 int marked
= mark_redundant_parents(revs
, commit
);
2749 marked
+= mark_treesame_root_parents(revs
, commit
);
2751 marked
-= leave_one_treesame_to_parent(revs
, commit
);
2753 cnt
= remove_marked_parents(revs
, commit
);
2757 * A commit simplifies to itself if it is a root, if it is
2758 * UNINTERESTING, if it touches the given paths, or if it is a
2759 * merge and its parents don't simplify to one relevant commit
2760 * (the first two cases are already handled at the beginning of
2763 * Otherwise, it simplifies to what its sole relevant parent
2767 (commit
->object
.flags
& UNINTERESTING
) ||
2768 !(commit
->object
.flags
& TREESAME
) ||
2769 (parent
= one_relevant_parent(revs
, commit
->parents
)) == NULL
)
2770 st
->simplified
= commit
;
2772 pst
= locate_simplify_state(revs
, parent
);
2773 st
->simplified
= pst
->simplified
;
2778 static void simplify_merges(struct rev_info
*revs
)
2780 struct commit_list
*list
, *next
;
2781 struct commit_list
*yet_to_do
, **tail
;
2782 struct commit
*commit
;
2787 /* feed the list reversed */
2789 for (list
= revs
->commits
; list
; list
= next
) {
2790 commit
= list
->item
;
2793 * Do not free(list) here yet; the original list
2794 * is used later in this function.
2796 commit_list_insert(commit
, &yet_to_do
);
2803 commit
= pop_commit(&list
);
2804 tail
= simplify_one(revs
, commit
, tail
);
2808 /* clean up the result, removing the simplified ones */
2809 list
= revs
->commits
;
2810 revs
->commits
= NULL
;
2811 tail
= &revs
->commits
;
2813 struct merge_simplify_state
*st
;
2815 commit
= pop_commit(&list
);
2816 st
= locate_simplify_state(revs
, commit
);
2817 if (st
->simplified
== commit
)
2818 tail
= &commit_list_insert(commit
, tail
)->next
;
2822 static void set_children(struct rev_info
*revs
)
2824 struct commit_list
*l
;
2825 for (l
= revs
->commits
; l
; l
= l
->next
) {
2826 struct commit
*commit
= l
->item
;
2827 struct commit_list
*p
;
2829 for (p
= commit
->parents
; p
; p
= p
->next
)
2830 add_child(revs
, p
->item
, commit
);
2834 void reset_revision_walk(void)
2836 clear_object_flags(SEEN
| ADDED
| SHOWN
);
2839 int prepare_revision_walk(struct rev_info
*revs
)
2842 struct object_array old_pending
;
2843 struct commit_list
**next
= &revs
->commits
;
2845 memcpy(&old_pending
, &revs
->pending
, sizeof(old_pending
));
2846 revs
->pending
.nr
= 0;
2847 revs
->pending
.alloc
= 0;
2848 revs
->pending
.objects
= NULL
;
2849 for (i
= 0; i
< old_pending
.nr
; i
++) {
2850 struct object_array_entry
*e
= old_pending
.objects
+ i
;
2851 struct commit
*commit
= handle_commit(revs
, e
);
2853 if (!(commit
->object
.flags
& SEEN
)) {
2854 commit
->object
.flags
|= SEEN
;
2855 next
= commit_list_append(commit
, next
);
2859 object_array_clear(&old_pending
);
2861 /* Signal whether we need per-parent treesame decoration */
2862 if (revs
->simplify_merges
||
2863 (revs
->limited
&& limiting_can_increase_treesame(revs
)))
2864 revs
->treesame
.name
= "treesame";
2866 if (revs
->no_walk
!= REVISION_WALK_NO_WALK_UNSORTED
)
2867 commit_list_sort_by_date(&revs
->commits
);
2871 if (limit_list(revs
) < 0)
2873 if (revs
->topo_order
)
2874 sort_in_topological_order(&revs
->commits
, revs
->sort_order
);
2875 if (revs
->line_level_traverse
)
2876 line_log_filter(revs
);
2877 if (revs
->simplify_merges
)
2878 simplify_merges(revs
);
2879 if (revs
->children
.name
)
2884 static enum rewrite_result
rewrite_one(struct rev_info
*revs
, struct commit
**pp
)
2886 struct commit_list
*cache
= NULL
;
2889 struct commit
*p
= *pp
;
2891 if (add_parents_to_list(revs
, p
, &revs
->commits
, &cache
) < 0)
2892 return rewrite_one_error
;
2893 if (p
->object
.flags
& UNINTERESTING
)
2894 return rewrite_one_ok
;
2895 if (!(p
->object
.flags
& TREESAME
))
2896 return rewrite_one_ok
;
2898 return rewrite_one_noparents
;
2899 if ((p
= one_relevant_parent(revs
, p
->parents
)) == NULL
)
2900 return rewrite_one_ok
;
2905 int rewrite_parents(struct rev_info
*revs
, struct commit
*commit
,
2906 rewrite_parent_fn_t rewrite_parent
)
2908 struct commit_list
**pp
= &commit
->parents
;
2910 struct commit_list
*parent
= *pp
;
2911 switch (rewrite_parent(revs
, &parent
->item
)) {
2912 case rewrite_one_ok
:
2914 case rewrite_one_noparents
:
2917 case rewrite_one_error
:
2922 remove_duplicate_parents(revs
, commit
);
2926 static int commit_rewrite_person(struct strbuf
*buf
, const char *what
, struct string_list
*mailmap
)
2928 char *person
, *endp
;
2929 size_t len
, namelen
, maillen
;
2932 struct ident_split ident
;
2934 person
= strstr(buf
->buf
, what
);
2938 person
+= strlen(what
);
2939 endp
= strchr(person
, '\n');
2943 len
= endp
- person
;
2945 if (split_ident_line(&ident
, person
, len
))
2948 mail
= ident
.mail_begin
;
2949 maillen
= ident
.mail_end
- ident
.mail_begin
;
2950 name
= ident
.name_begin
;
2951 namelen
= ident
.name_end
- ident
.name_begin
;
2953 if (map_user(mailmap
, &mail
, &maillen
, &name
, &namelen
)) {
2954 struct strbuf namemail
= STRBUF_INIT
;
2956 strbuf_addf(&namemail
, "%.*s <%.*s>",
2957 (int)namelen
, name
, (int)maillen
, mail
);
2959 strbuf_splice(buf
, ident
.name_begin
- buf
->buf
,
2960 ident
.mail_end
- ident
.name_begin
+ 1,
2961 namemail
.buf
, namemail
.len
);
2963 strbuf_release(&namemail
);
2971 static int commit_match(struct commit
*commit
, struct rev_info
*opt
)
2974 const char *encoding
;
2975 const char *message
;
2976 struct strbuf buf
= STRBUF_INIT
;
2978 if (!opt
->grep_filter
.pattern_list
&& !opt
->grep_filter
.header_list
)
2981 /* Prepend "fake" headers as needed */
2982 if (opt
->grep_filter
.use_reflog_filter
) {
2983 strbuf_addstr(&buf
, "reflog ");
2984 get_reflog_message(&buf
, opt
->reflog_info
);
2985 strbuf_addch(&buf
, '\n');
2989 * We grep in the user's output encoding, under the assumption that it
2990 * is the encoding they are most likely to write their grep pattern
2991 * for. In addition, it means we will match the "notes" encoding below,
2992 * so we will not end up with a buffer that has two different encodings
2995 encoding
= get_log_output_encoding();
2996 message
= logmsg_reencode(commit
, NULL
, encoding
);
2998 /* Copy the commit to temporary if we are using "fake" headers */
3000 strbuf_addstr(&buf
, message
);
3002 if (opt
->grep_filter
.header_list
&& opt
->mailmap
) {
3004 strbuf_addstr(&buf
, message
);
3006 commit_rewrite_person(&buf
, "\nauthor ", opt
->mailmap
);
3007 commit_rewrite_person(&buf
, "\ncommitter ", opt
->mailmap
);
3010 /* Append "fake" message parts as needed */
3011 if (opt
->show_notes
) {
3013 strbuf_addstr(&buf
, message
);
3014 format_display_notes(&commit
->object
.oid
, &buf
, encoding
, 1);
3018 * Find either in the original commit message, or in the temporary.
3019 * Note that we cast away the constness of "message" here. It is
3020 * const because it may come from the cached commit buffer. That's OK,
3021 * because we know that it is modifiable heap memory, and that while
3022 * grep_buffer may modify it for speed, it will restore any
3023 * changes before returning.
3026 retval
= grep_buffer(&opt
->grep_filter
, buf
.buf
, buf
.len
);
3028 retval
= grep_buffer(&opt
->grep_filter
,
3029 (char *)message
, strlen(message
));
3030 strbuf_release(&buf
);
3031 unuse_commit_buffer(commit
, message
);
3032 return opt
->invert_grep
? !retval
: retval
;
3035 static inline int want_ancestry(const struct rev_info
*revs
)
3037 return (revs
->rewrite_parents
|| revs
->children
.name
);
3041 * Return a timestamp to be used for --since/--until comparisons for this
3042 * commit, based on the revision options.
3044 static timestamp_t
comparison_date(const struct rev_info
*revs
,
3045 struct commit
*commit
)
3047 return revs
->reflog_info
?
3048 get_reflog_timestamp(revs
->reflog_info
) :
3052 enum commit_action
get_commit_action(struct rev_info
*revs
, struct commit
*commit
)
3054 if (commit
->object
.flags
& SHOWN
)
3055 return commit_ignore
;
3056 if (revs
->unpacked
&& has_sha1_pack(commit
->object
.oid
.hash
))
3057 return commit_ignore
;
3058 if (commit
->object
.flags
& UNINTERESTING
)
3059 return commit_ignore
;
3060 if (revs
->min_age
!= -1 &&
3061 comparison_date(revs
, commit
) > revs
->min_age
)
3062 return commit_ignore
;
3063 if (revs
->min_parents
|| (revs
->max_parents
>= 0)) {
3064 int n
= commit_list_count(commit
->parents
);
3065 if ((n
< revs
->min_parents
) ||
3066 ((revs
->max_parents
>= 0) && (n
> revs
->max_parents
)))
3067 return commit_ignore
;
3069 if (!commit_match(commit
, revs
))
3070 return commit_ignore
;
3071 if (revs
->prune
&& revs
->dense
) {
3072 /* Commit without changes? */
3073 if (commit
->object
.flags
& TREESAME
) {
3075 struct commit_list
*p
;
3076 /* drop merges unless we want parenthood */
3077 if (!want_ancestry(revs
))
3078 return commit_ignore
;
3080 * If we want ancestry, then need to keep any merges
3081 * between relevant commits to tie together topology.
3082 * For consistency with TREESAME and simplification
3083 * use "relevant" here rather than just INTERESTING,
3084 * to treat bottom commit(s) as part of the topology.
3086 for (n
= 0, p
= commit
->parents
; p
; p
= p
->next
)
3087 if (relevant_commit(p
->item
))
3090 return commit_ignore
;
3096 define_commit_slab(saved_parents
, struct commit_list
*);
3098 #define EMPTY_PARENT_LIST ((struct commit_list *)-1)
3101 * You may only call save_parents() once per commit (this is checked
3102 * for non-root commits).
3104 static void save_parents(struct rev_info
*revs
, struct commit
*commit
)
3106 struct commit_list
**pp
;
3108 if (!revs
->saved_parents_slab
) {
3109 revs
->saved_parents_slab
= xmalloc(sizeof(struct saved_parents
));
3110 init_saved_parents(revs
->saved_parents_slab
);
3113 pp
= saved_parents_at(revs
->saved_parents_slab
, commit
);
3116 * When walking with reflogs, we may visit the same commit
3117 * several times: once for each appearance in the reflog.
3119 * In this case, save_parents() will be called multiple times.
3120 * We want to keep only the first set of parents. We need to
3121 * store a sentinel value for an empty (i.e., NULL) parent
3122 * list to distinguish it from a not-yet-saved list, however.
3126 if (commit
->parents
)
3127 *pp
= copy_commit_list(commit
->parents
);
3129 *pp
= EMPTY_PARENT_LIST
;
3132 static void free_saved_parents(struct rev_info
*revs
)
3134 if (revs
->saved_parents_slab
)
3135 clear_saved_parents(revs
->saved_parents_slab
);
3138 struct commit_list
*get_saved_parents(struct rev_info
*revs
, const struct commit
*commit
)
3140 struct commit_list
*parents
;
3142 if (!revs
->saved_parents_slab
)
3143 return commit
->parents
;
3145 parents
= *saved_parents_at(revs
->saved_parents_slab
, commit
);
3146 if (parents
== EMPTY_PARENT_LIST
)
3151 enum commit_action
simplify_commit(struct rev_info
*revs
, struct commit
*commit
)
3153 enum commit_action action
= get_commit_action(revs
, commit
);
3155 if (action
== commit_show
&&
3156 revs
->prune
&& revs
->dense
&& want_ancestry(revs
)) {
3158 * --full-diff on simplified parents is no good: it
3159 * will show spurious changes from the commits that
3160 * were elided. So we save the parents on the side
3161 * when --full-diff is in effect.
3163 if (revs
->full_diff
)
3164 save_parents(revs
, commit
);
3165 if (rewrite_parents(revs
, commit
, rewrite_one
) < 0)
3166 return commit_error
;
3171 static void track_linear(struct rev_info
*revs
, struct commit
*commit
)
3173 if (revs
->track_first_time
) {
3175 revs
->track_first_time
= 0;
3177 struct commit_list
*p
;
3178 for (p
= revs
->previous_parents
; p
; p
= p
->next
)
3179 if (p
->item
== NULL
|| /* first commit */
3180 !oidcmp(&p
->item
->object
.oid
, &commit
->object
.oid
))
3182 revs
->linear
= p
!= NULL
;
3184 if (revs
->reverse
) {
3186 commit
->object
.flags
|= TRACK_LINEAR
;
3188 free_commit_list(revs
->previous_parents
);
3189 revs
->previous_parents
= copy_commit_list(commit
->parents
);
3192 static struct commit
*get_revision_1(struct rev_info
*revs
)
3195 struct commit
*commit
;
3197 if (revs
->reflog_info
)
3198 commit
= next_reflog_entry(revs
->reflog_info
);
3200 commit
= pop_commit(&revs
->commits
);
3205 if (revs
->reflog_info
)
3206 commit
->object
.flags
&= ~(ADDED
| SEEN
| SHOWN
);
3209 * If we haven't done the list limiting, we need to look at
3210 * the parents here. We also need to do the date-based limiting
3211 * that we'd otherwise have done in limit_list().
3213 if (!revs
->limited
) {
3214 if (revs
->max_age
!= -1 &&
3215 comparison_date(revs
, commit
) < revs
->max_age
)
3218 if (revs
->reflog_info
)
3219 try_to_simplify_commit(revs
, commit
);
3220 else if (add_parents_to_list(revs
, commit
, &revs
->commits
, NULL
) < 0) {
3221 if (!revs
->ignore_missing_links
)
3222 die("Failed to traverse parents of commit %s",
3223 oid_to_hex(&commit
->object
.oid
));
3227 switch (simplify_commit(revs
, commit
)) {
3231 die("Failed to simplify parents of commit %s",
3232 oid_to_hex(&commit
->object
.oid
));
3234 if (revs
->track_linear
)
3235 track_linear(revs
, commit
);
3242 * Return true for entries that have not yet been shown. (This is an
3243 * object_array_each_func_t.)
3245 static int entry_unshown(struct object_array_entry
*entry
, void *cb_data_unused
)
3247 return !(entry
->item
->flags
& SHOWN
);
3251 * If array is on the verge of a realloc, garbage-collect any entries
3252 * that have already been shown to try to free up some space.
3254 static void gc_boundary(struct object_array
*array
)
3256 if (array
->nr
== array
->alloc
)
3257 object_array_filter(array
, entry_unshown
, NULL
);
3260 static void create_boundary_commit_list(struct rev_info
*revs
)
3264 struct object_array
*array
= &revs
->boundary_commits
;
3265 struct object_array_entry
*objects
= array
->objects
;
3268 * If revs->commits is non-NULL at this point, an error occurred in
3269 * get_revision_1(). Ignore the error and continue printing the
3270 * boundary commits anyway. (This is what the code has always
3273 if (revs
->commits
) {
3274 free_commit_list(revs
->commits
);
3275 revs
->commits
= NULL
;
3279 * Put all of the actual boundary commits from revs->boundary_commits
3280 * into revs->commits
3282 for (i
= 0; i
< array
->nr
; i
++) {
3283 c
= (struct commit
*)(objects
[i
].item
);
3286 if (!(c
->object
.flags
& CHILD_SHOWN
))
3288 if (c
->object
.flags
& (SHOWN
| BOUNDARY
))
3290 c
->object
.flags
|= BOUNDARY
;
3291 commit_list_insert(c
, &revs
->commits
);
3295 * If revs->topo_order is set, sort the boundary commits
3296 * in topological order
3298 sort_in_topological_order(&revs
->commits
, revs
->sort_order
);
3301 static struct commit
*get_revision_internal(struct rev_info
*revs
)
3303 struct commit
*c
= NULL
;
3304 struct commit_list
*l
;
3306 if (revs
->boundary
== 2) {
3308 * All of the normal commits have already been returned,
3309 * and we are now returning boundary commits.
3310 * create_boundary_commit_list() has populated
3311 * revs->commits with the remaining commits to return.
3313 c
= pop_commit(&revs
->commits
);
3315 c
->object
.flags
|= SHOWN
;
3320 * If our max_count counter has reached zero, then we are done. We
3321 * don't simply return NULL because we still might need to show
3322 * boundary commits. But we want to avoid calling get_revision_1, which
3323 * might do a considerable amount of work finding the next commit only
3324 * for us to throw it away.
3326 * If it is non-zero, then either we don't have a max_count at all
3327 * (-1), or it is still counting, in which case we decrement.
3329 if (revs
->max_count
) {
3330 c
= get_revision_1(revs
);
3332 while (revs
->skip_count
> 0) {
3334 c
= get_revision_1(revs
);
3340 if (revs
->max_count
> 0)
3345 c
->object
.flags
|= SHOWN
;
3347 if (!revs
->boundary
)
3352 * get_revision_1() runs out the commits, and
3353 * we are done computing the boundaries.
3354 * switch to boundary commits output mode.
3359 * Update revs->commits to contain the list of
3362 create_boundary_commit_list(revs
);
3364 return get_revision_internal(revs
);
3368 * boundary commits are the commits that are parents of the
3369 * ones we got from get_revision_1() but they themselves are
3370 * not returned from get_revision_1(). Before returning
3371 * 'c', we need to mark its parents that they could be boundaries.
3374 for (l
= c
->parents
; l
; l
= l
->next
) {
3376 p
= &(l
->item
->object
);
3377 if (p
->flags
& (CHILD_SHOWN
| SHOWN
))
3379 p
->flags
|= CHILD_SHOWN
;
3380 gc_boundary(&revs
->boundary_commits
);
3381 add_object_array(p
, NULL
, &revs
->boundary_commits
);
3387 struct commit
*get_revision(struct rev_info
*revs
)
3390 struct commit_list
*reversed
;
3392 if (revs
->reverse
) {
3394 while ((c
= get_revision_internal(revs
)))
3395 commit_list_insert(c
, &reversed
);
3396 revs
->commits
= reversed
;
3398 revs
->reverse_output_stage
= 1;
3401 if (revs
->reverse_output_stage
) {
3402 c
= pop_commit(&revs
->commits
);
3403 if (revs
->track_linear
)
3404 revs
->linear
= !!(c
&& c
->object
.flags
& TRACK_LINEAR
);
3408 c
= get_revision_internal(revs
);
3409 if (c
&& revs
->graph
)
3410 graph_update(revs
->graph
, c
);
3412 free_saved_parents(revs
);
3413 if (revs
->previous_parents
) {
3414 free_commit_list(revs
->previous_parents
);
3415 revs
->previous_parents
= NULL
;
3421 char *get_revision_mark(const struct rev_info
*revs
, const struct commit
*commit
)
3423 if (commit
->object
.flags
& BOUNDARY
)
3425 else if (commit
->object
.flags
& UNINTERESTING
)
3427 else if (commit
->object
.flags
& PATCHSAME
)
3429 else if (!revs
|| revs
->left_right
) {
3430 if (commit
->object
.flags
& SYMMETRIC_LEFT
)
3434 } else if (revs
->graph
)
3436 else if (revs
->cherry_mark
)
3441 void put_revision_mark(const struct rev_info
*revs
, const struct commit
*commit
)
3443 char *mark
= get_revision_mark(revs
, commit
);
3446 fputs(mark
, stdout
);