2 #include "object-store.h"
10 #include "repository.h"
13 #include "reflog-walk.h"
14 #include "patch-ids.h"
17 #include "string-list.h"
20 #include "commit-slab.h"
22 #include "cache-tree.h"
26 #include "argv-array.h"
27 #include "commit-reach.h"
28 #include "commit-graph.h"
29 #include "prio-queue.h"
32 volatile show_early_output_fn_t show_early_output
;
34 static const char *term_bad
;
35 static const char *term_good
;
37 implement_shared_commit_slab(revision_sources
, char *);
39 void show_object_with_name(FILE *out
, struct object
*obj
, const char *name
)
43 fprintf(out
, "%s ", oid_to_hex(&obj
->oid
));
44 for (p
= name
; *p
&& *p
!= '\n'; p
++)
49 static void mark_blob_uninteresting(struct blob
*blob
)
53 if (blob
->object
.flags
& UNINTERESTING
)
55 blob
->object
.flags
|= UNINTERESTING
;
58 static void mark_tree_contents_uninteresting(struct repository
*r
,
61 struct tree_desc desc
;
62 struct name_entry entry
;
64 if (parse_tree_gently(tree
, 1) < 0)
67 init_tree_desc(&desc
, tree
->buffer
, tree
->size
);
68 while (tree_entry(&desc
, &entry
)) {
69 switch (object_type(entry
.mode
)) {
71 mark_tree_uninteresting(r
, lookup_tree(r
, &entry
.oid
));
74 mark_blob_uninteresting(lookup_blob(r
, &entry
.oid
));
77 /* Subproject commit - not in this repository */
83 * We don't care about the tree any more
84 * after it has been marked uninteresting.
86 free_tree_buffer(tree
);
89 void mark_tree_uninteresting(struct repository
*r
, struct tree
*tree
)
97 if (obj
->flags
& UNINTERESTING
)
99 obj
->flags
|= UNINTERESTING
;
100 mark_tree_contents_uninteresting(r
, tree
);
103 struct path_and_oids_entry
{
104 struct hashmap_entry ent
;
109 static int path_and_oids_cmp(const void *hashmap_cmp_fn_data
,
110 const struct path_and_oids_entry
*e1
,
111 const struct path_and_oids_entry
*e2
,
114 return strcmp(e1
->path
, e2
->path
);
117 static void paths_and_oids_init(struct hashmap
*map
)
119 hashmap_init(map
, (hashmap_cmp_fn
) path_and_oids_cmp
, NULL
, 0);
122 static void paths_and_oids_clear(struct hashmap
*map
)
124 struct hashmap_iter iter
;
125 struct path_and_oids_entry
*entry
;
126 hashmap_iter_init(map
, &iter
);
128 while ((entry
= (struct path_and_oids_entry
*)hashmap_iter_next(&iter
))) {
129 oidset_clear(&entry
->trees
);
133 hashmap_free(map
, 1);
136 static void paths_and_oids_insert(struct hashmap
*map
,
138 const struct object_id
*oid
)
140 int hash
= strhash(path
);
141 struct path_and_oids_entry key
;
142 struct path_and_oids_entry
*entry
;
144 hashmap_entry_init(&key
, hash
);
146 /* use a shallow copy for the lookup */
147 key
.path
= (char *)path
;
148 oidset_init(&key
.trees
, 0);
150 if (!(entry
= (struct path_and_oids_entry
*)hashmap_get(map
, &key
, NULL
))) {
151 entry
= xcalloc(1, sizeof(struct path_and_oids_entry
));
152 hashmap_entry_init(entry
, hash
);
153 entry
->path
= xstrdup(key
.path
);
154 oidset_init(&entry
->trees
, 16);
155 hashmap_put(map
, entry
);
158 oidset_insert(&entry
->trees
, oid
);
161 static void add_children_by_path(struct repository
*r
,
165 struct tree_desc desc
;
166 struct name_entry entry
;
171 if (parse_tree_gently(tree
, 1) < 0)
174 init_tree_desc(&desc
, tree
->buffer
, tree
->size
);
175 while (tree_entry(&desc
, &entry
)) {
176 switch (object_type(entry
.mode
)) {
178 paths_and_oids_insert(map
, entry
.path
, &entry
.oid
);
180 if (tree
->object
.flags
& UNINTERESTING
) {
181 struct tree
*child
= lookup_tree(r
, &entry
.oid
);
183 child
->object
.flags
|= UNINTERESTING
;
187 if (tree
->object
.flags
& UNINTERESTING
) {
188 struct blob
*child
= lookup_blob(r
, &entry
.oid
);
190 child
->object
.flags
|= UNINTERESTING
;
194 /* Subproject commit - not in this repository */
199 free_tree_buffer(tree
);
202 void mark_trees_uninteresting_sparse(struct repository
*r
,
203 struct oidset
*trees
)
205 unsigned has_interesting
= 0, has_uninteresting
= 0;
207 struct hashmap_iter map_iter
;
208 struct path_and_oids_entry
*entry
;
209 struct object_id
*oid
;
210 struct oidset_iter iter
;
212 oidset_iter_init(trees
, &iter
);
213 while ((!has_interesting
|| !has_uninteresting
) &&
214 (oid
= oidset_iter_next(&iter
))) {
215 struct tree
*tree
= lookup_tree(r
, oid
);
220 if (tree
->object
.flags
& UNINTERESTING
)
221 has_uninteresting
= 1;
226 /* Do not walk unless we have both types of trees. */
227 if (!has_uninteresting
|| !has_interesting
)
230 paths_and_oids_init(&map
);
232 oidset_iter_init(trees
, &iter
);
233 while ((oid
= oidset_iter_next(&iter
))) {
234 struct tree
*tree
= lookup_tree(r
, oid
);
235 add_children_by_path(r
, tree
, &map
);
238 hashmap_iter_init(&map
, &map_iter
);
239 while ((entry
= hashmap_iter_next(&map_iter
)))
240 mark_trees_uninteresting_sparse(r
, &entry
->trees
);
242 paths_and_oids_clear(&map
);
245 struct commit_stack
{
246 struct commit
**items
;
249 #define COMMIT_STACK_INIT { NULL, 0, 0 }
251 static void commit_stack_push(struct commit_stack
*stack
, struct commit
*commit
)
253 ALLOC_GROW(stack
->items
, stack
->nr
+ 1, stack
->alloc
);
254 stack
->items
[stack
->nr
++] = commit
;
257 static struct commit
*commit_stack_pop(struct commit_stack
*stack
)
259 return stack
->nr
? stack
->items
[--stack
->nr
] : NULL
;
262 static void commit_stack_clear(struct commit_stack
*stack
)
264 FREE_AND_NULL(stack
->items
);
265 stack
->nr
= stack
->alloc
= 0;
268 static void mark_one_parent_uninteresting(struct commit
*commit
,
269 struct commit_stack
*pending
)
271 struct commit_list
*l
;
273 if (commit
->object
.flags
& UNINTERESTING
)
275 commit
->object
.flags
|= UNINTERESTING
;
278 * Normally we haven't parsed the parent
279 * yet, so we won't have a parent of a parent
280 * here. However, it may turn out that we've
281 * reached this commit some other way (where it
282 * wasn't uninteresting), in which case we need
283 * to mark its parents recursively too..
285 for (l
= commit
->parents
; l
; l
= l
->next
)
286 commit_stack_push(pending
, l
->item
);
289 void mark_parents_uninteresting(struct commit
*commit
)
291 struct commit_stack pending
= COMMIT_STACK_INIT
;
292 struct commit_list
*l
;
294 for (l
= commit
->parents
; l
; l
= l
->next
)
295 mark_one_parent_uninteresting(l
->item
, &pending
);
297 while (pending
.nr
> 0)
298 mark_one_parent_uninteresting(commit_stack_pop(&pending
),
301 commit_stack_clear(&pending
);
304 static void add_pending_object_with_path(struct rev_info
*revs
,
306 const char *name
, unsigned mode
,
311 if (revs
->no_walk
&& (obj
->flags
& UNINTERESTING
))
313 if (revs
->reflog_info
&& obj
->type
== OBJ_COMMIT
) {
314 struct strbuf buf
= STRBUF_INIT
;
315 int len
= interpret_branch_name(name
, 0, &buf
, 0);
317 if (0 < len
&& name
[len
] && buf
.len
)
318 strbuf_addstr(&buf
, name
+ len
);
319 add_reflog_for_walk(revs
->reflog_info
,
320 (struct commit
*)obj
,
321 buf
.buf
[0] ? buf
.buf
: name
);
322 strbuf_release(&buf
);
323 return; /* do not add the commit itself */
325 add_object_array_with_path(obj
, name
, &revs
->pending
, mode
, path
);
328 static void add_pending_object_with_mode(struct rev_info
*revs
,
330 const char *name
, unsigned mode
)
332 add_pending_object_with_path(revs
, obj
, name
, mode
, NULL
);
335 void add_pending_object(struct rev_info
*revs
,
336 struct object
*obj
, const char *name
)
338 add_pending_object_with_mode(revs
, obj
, name
, S_IFINVALID
);
341 void add_head_to_pending(struct rev_info
*revs
)
343 struct object_id oid
;
345 if (get_oid("HEAD", &oid
))
347 obj
= parse_object(revs
->repo
, &oid
);
350 add_pending_object(revs
, obj
, "HEAD");
353 static struct object
*get_reference(struct rev_info
*revs
, const char *name
,
354 const struct object_id
*oid
,
357 struct object
*object
;
360 * If the repository has commit graphs, repo_parse_commit() avoids
361 * reading the object buffer, so use it whenever possible.
363 if (oid_object_info(revs
->repo
, oid
, NULL
) == OBJ_COMMIT
) {
364 struct commit
*c
= lookup_commit(revs
->repo
, oid
);
365 if (!repo_parse_commit(revs
->repo
, c
))
366 object
= (struct object
*) c
;
370 object
= parse_object(revs
->repo
, oid
);
374 if (revs
->ignore_missing
)
376 if (revs
->exclude_promisor_objects
&& is_promisor_object(oid
))
378 die("bad object %s", name
);
380 object
->flags
|= flags
;
384 void add_pending_oid(struct rev_info
*revs
, const char *name
,
385 const struct object_id
*oid
, unsigned int flags
)
387 struct object
*object
= get_reference(revs
, name
, oid
, flags
);
388 add_pending_object(revs
, object
, name
);
391 static struct commit
*handle_commit(struct rev_info
*revs
,
392 struct object_array_entry
*entry
)
394 struct object
*object
= entry
->item
;
395 const char *name
= entry
->name
;
396 const char *path
= entry
->path
;
397 unsigned int mode
= entry
->mode
;
398 unsigned long flags
= object
->flags
;
401 * Tag object? Look what it points to..
403 while (object
->type
== OBJ_TAG
) {
404 struct tag
*tag
= (struct tag
*) object
;
405 if (revs
->tag_objects
&& !(flags
& UNINTERESTING
))
406 add_pending_object(revs
, object
, tag
->tag
);
407 object
= parse_object(revs
->repo
, get_tagged_oid(tag
));
409 if (revs
->ignore_missing_links
|| (flags
& UNINTERESTING
))
411 if (revs
->exclude_promisor_objects
&&
412 is_promisor_object(&tag
->tagged
->oid
))
414 die("bad object %s", oid_to_hex(&tag
->tagged
->oid
));
416 object
->flags
|= flags
;
418 * We'll handle the tagged object by looping or dropping
419 * through to the non-tag handlers below. Do not
420 * propagate path data from the tag's pending entry.
427 * Commit object? Just return it, we'll do all the complex
430 if (object
->type
== OBJ_COMMIT
) {
431 struct commit
*commit
= (struct commit
*)object
;
433 if (parse_commit(commit
) < 0)
434 die("unable to parse commit %s", name
);
435 if (flags
& UNINTERESTING
) {
436 mark_parents_uninteresting(commit
);
438 if (!revs
->topo_order
|| !generation_numbers_enabled(the_repository
))
442 char **slot
= revision_sources_at(revs
->sources
, commit
);
445 *slot
= xstrdup(name
);
451 * Tree object? Either mark it uninteresting, or add it
452 * to the list of objects to look at later..
454 if (object
->type
== OBJ_TREE
) {
455 struct tree
*tree
= (struct tree
*)object
;
456 if (!revs
->tree_objects
)
458 if (flags
& UNINTERESTING
) {
459 mark_tree_contents_uninteresting(revs
->repo
, tree
);
462 add_pending_object_with_path(revs
, object
, name
, mode
, path
);
467 * Blob object? You know the drill by now..
469 if (object
->type
== OBJ_BLOB
) {
470 if (!revs
->blob_objects
)
472 if (flags
& UNINTERESTING
)
474 add_pending_object_with_path(revs
, object
, name
, mode
, path
);
477 die("%s is unknown object", name
);
480 static int everybody_uninteresting(struct commit_list
*orig
,
481 struct commit
**interesting_cache
)
483 struct commit_list
*list
= orig
;
485 if (*interesting_cache
) {
486 struct commit
*commit
= *interesting_cache
;
487 if (!(commit
->object
.flags
& UNINTERESTING
))
492 struct commit
*commit
= list
->item
;
494 if (commit
->object
.flags
& UNINTERESTING
)
497 *interesting_cache
= commit
;
504 * A definition of "relevant" commit that we can use to simplify limited graphs
505 * by eliminating side branches.
507 * A "relevant" commit is one that is !UNINTERESTING (ie we are including it
508 * in our list), or that is a specified BOTTOM commit. Then after computing
509 * a limited list, during processing we can generally ignore boundary merges
510 * coming from outside the graph, (ie from irrelevant parents), and treat
511 * those merges as if they were single-parent. TREESAME is defined to consider
512 * only relevant parents, if any. If we are TREESAME to our on-graph parents,
513 * we don't care if we were !TREESAME to non-graph parents.
515 * Treating bottom commits as relevant ensures that a limited graph's
516 * connection to the actual bottom commit is not viewed as a side branch, but
517 * treated as part of the graph. For example:
519 * ....Z...A---X---o---o---B
523 * When computing "A..B", the A-X connection is at least as important as
524 * Y-X, despite A being flagged UNINTERESTING.
526 * And when computing --ancestry-path "A..B", the A-X connection is more
527 * important than Y-X, despite both A and Y being flagged UNINTERESTING.
529 static inline int relevant_commit(struct commit
*commit
)
531 return (commit
->object
.flags
& (UNINTERESTING
| BOTTOM
)) != UNINTERESTING
;
535 * Return a single relevant commit from a parent list. If we are a TREESAME
536 * commit, and this selects one of our parents, then we can safely simplify to
539 static struct commit
*one_relevant_parent(const struct rev_info
*revs
,
540 struct commit_list
*orig
)
542 struct commit_list
*list
= orig
;
543 struct commit
*relevant
= NULL
;
549 * For 1-parent commits, or if first-parent-only, then return that
550 * first parent (even if not "relevant" by the above definition).
551 * TREESAME will have been set purely on that parent.
553 if (revs
->first_parent_only
|| !orig
->next
)
557 * For multi-parent commits, identify a sole relevant parent, if any.
558 * If we have only one relevant parent, then TREESAME will be set purely
559 * with regard to that parent, and we can simplify accordingly.
561 * If we have more than one relevant parent, or no relevant parents
562 * (and multiple irrelevant ones), then we can't select a parent here
566 struct commit
*commit
= list
->item
;
568 if (relevant_commit(commit
)) {
578 * The goal is to get REV_TREE_NEW as the result only if the
579 * diff consists of all '+' (and no other changes), REV_TREE_OLD
580 * if the whole diff is removal of old data, and otherwise
581 * REV_TREE_DIFFERENT (of course if the trees are the same we
582 * want REV_TREE_SAME).
584 * The only time we care about the distinction is when
585 * remove_empty_trees is in effect, in which case we care only about
586 * whether the whole change is REV_TREE_NEW, or if there's another type
587 * of change. Which means we can stop the diff early in either of these
590 * 1. We're not using remove_empty_trees at all.
592 * 2. We saw anything except REV_TREE_NEW.
594 static int tree_difference
= REV_TREE_SAME
;
596 static void file_add_remove(struct diff_options
*options
,
597 int addremove
, unsigned mode
,
598 const struct object_id
*oid
,
600 const char *fullpath
, unsigned dirty_submodule
)
602 int diff
= addremove
== '+' ? REV_TREE_NEW
: REV_TREE_OLD
;
603 struct rev_info
*revs
= options
->change_fn_data
;
605 tree_difference
|= diff
;
606 if (!revs
->remove_empty_trees
|| tree_difference
!= REV_TREE_NEW
)
607 options
->flags
.has_changes
= 1;
610 static void file_change(struct diff_options
*options
,
611 unsigned old_mode
, unsigned new_mode
,
612 const struct object_id
*old_oid
,
613 const struct object_id
*new_oid
,
614 int old_oid_valid
, int new_oid_valid
,
615 const char *fullpath
,
616 unsigned old_dirty_submodule
, unsigned new_dirty_submodule
)
618 tree_difference
= REV_TREE_DIFFERENT
;
619 options
->flags
.has_changes
= 1;
622 static int rev_compare_tree(struct rev_info
*revs
,
623 struct commit
*parent
, struct commit
*commit
)
625 struct tree
*t1
= get_commit_tree(parent
);
626 struct tree
*t2
= get_commit_tree(commit
);
633 if (revs
->simplify_by_decoration
) {
635 * If we are simplifying by decoration, then the commit
636 * is worth showing if it has a tag pointing at it.
638 if (get_name_decoration(&commit
->object
))
639 return REV_TREE_DIFFERENT
;
641 * A commit that is not pointed by a tag is uninteresting
642 * if we are not limited by path. This means that you will
643 * see the usual "commits that touch the paths" plus any
644 * tagged commit by specifying both --simplify-by-decoration
647 if (!revs
->prune_data
.nr
)
648 return REV_TREE_SAME
;
651 tree_difference
= REV_TREE_SAME
;
652 revs
->pruning
.flags
.has_changes
= 0;
653 if (diff_tree_oid(&t1
->object
.oid
, &t2
->object
.oid
, "",
655 return REV_TREE_DIFFERENT
;
656 return tree_difference
;
659 static int rev_same_tree_as_empty(struct rev_info
*revs
, struct commit
*commit
)
662 struct tree
*t1
= get_commit_tree(commit
);
667 tree_difference
= REV_TREE_SAME
;
668 revs
->pruning
.flags
.has_changes
= 0;
669 retval
= diff_tree_oid(NULL
, &t1
->object
.oid
, "", &revs
->pruning
);
671 return retval
>= 0 && (tree_difference
== REV_TREE_SAME
);
674 struct treesame_state
{
675 unsigned int nparents
;
676 unsigned char treesame
[FLEX_ARRAY
];
679 static struct treesame_state
*initialise_treesame(struct rev_info
*revs
, struct commit
*commit
)
681 unsigned n
= commit_list_count(commit
->parents
);
682 struct treesame_state
*st
= xcalloc(1, st_add(sizeof(*st
), n
));
684 add_decoration(&revs
->treesame
, &commit
->object
, st
);
689 * Must be called immediately after removing the nth_parent from a commit's
690 * parent list, if we are maintaining the per-parent treesame[] decoration.
691 * This does not recalculate the master TREESAME flag - update_treesame()
692 * should be called to update it after a sequence of treesame[] modifications
693 * that may have affected it.
695 static int compact_treesame(struct rev_info
*revs
, struct commit
*commit
, unsigned nth_parent
)
697 struct treesame_state
*st
;
700 if (!commit
->parents
) {
702 * Have just removed the only parent from a non-merge.
703 * Different handling, as we lack decoration.
706 die("compact_treesame %u", nth_parent
);
707 old_same
= !!(commit
->object
.flags
& TREESAME
);
708 if (rev_same_tree_as_empty(revs
, commit
))
709 commit
->object
.flags
|= TREESAME
;
711 commit
->object
.flags
&= ~TREESAME
;
715 st
= lookup_decoration(&revs
->treesame
, &commit
->object
);
716 if (!st
|| nth_parent
>= st
->nparents
)
717 die("compact_treesame %u", nth_parent
);
719 old_same
= st
->treesame
[nth_parent
];
720 memmove(st
->treesame
+ nth_parent
,
721 st
->treesame
+ nth_parent
+ 1,
722 st
->nparents
- nth_parent
- 1);
725 * If we've just become a non-merge commit, update TREESAME
726 * immediately, and remove the no-longer-needed decoration.
727 * If still a merge, defer update until update_treesame().
729 if (--st
->nparents
== 1) {
730 if (commit
->parents
->next
)
731 die("compact_treesame parents mismatch");
732 if (st
->treesame
[0] && revs
->dense
)
733 commit
->object
.flags
|= TREESAME
;
735 commit
->object
.flags
&= ~TREESAME
;
736 free(add_decoration(&revs
->treesame
, &commit
->object
, NULL
));
742 static unsigned update_treesame(struct rev_info
*revs
, struct commit
*commit
)
744 if (commit
->parents
&& commit
->parents
->next
) {
746 struct treesame_state
*st
;
747 struct commit_list
*p
;
748 unsigned relevant_parents
;
749 unsigned relevant_change
, irrelevant_change
;
751 st
= lookup_decoration(&revs
->treesame
, &commit
->object
);
753 die("update_treesame %s", oid_to_hex(&commit
->object
.oid
));
754 relevant_parents
= 0;
755 relevant_change
= irrelevant_change
= 0;
756 for (p
= commit
->parents
, n
= 0; p
; n
++, p
= p
->next
) {
757 if (relevant_commit(p
->item
)) {
758 relevant_change
|= !st
->treesame
[n
];
761 irrelevant_change
|= !st
->treesame
[n
];
763 if (relevant_parents
? relevant_change
: irrelevant_change
)
764 commit
->object
.flags
&= ~TREESAME
;
766 commit
->object
.flags
|= TREESAME
;
769 return commit
->object
.flags
& TREESAME
;
772 static inline int limiting_can_increase_treesame(const struct rev_info
*revs
)
775 * TREESAME is irrelevant unless prune && dense;
776 * if simplify_history is set, we can't have a mixture of TREESAME and
777 * !TREESAME INTERESTING parents (and we don't have treesame[]
778 * decoration anyway);
779 * if first_parent_only is set, then the TREESAME flag is locked
780 * against the first parent (and again we lack treesame[] decoration).
782 return revs
->prune
&& revs
->dense
&&
783 !revs
->simplify_history
&&
784 !revs
->first_parent_only
;
787 static void try_to_simplify_commit(struct rev_info
*revs
, struct commit
*commit
)
789 struct commit_list
**pp
, *parent
;
790 struct treesame_state
*ts
= NULL
;
791 int relevant_change
= 0, irrelevant_change
= 0;
792 int relevant_parents
, nth_parent
;
795 * If we don't do pruning, everything is interesting
800 if (!get_commit_tree(commit
))
803 if (!commit
->parents
) {
804 if (rev_same_tree_as_empty(revs
, commit
))
805 commit
->object
.flags
|= TREESAME
;
810 * Normal non-merge commit? If we don't want to make the
811 * history dense, we consider it always to be a change..
813 if (!revs
->dense
&& !commit
->parents
->next
)
816 for (pp
= &commit
->parents
, nth_parent
= 0, relevant_parents
= 0;
817 (parent
= *pp
) != NULL
;
818 pp
= &parent
->next
, nth_parent
++) {
819 struct commit
*p
= parent
->item
;
820 if (relevant_commit(p
))
823 if (nth_parent
== 1) {
825 * This our second loop iteration - so we now know
826 * we're dealing with a merge.
828 * Do not compare with later parents when we care only about
829 * the first parent chain, in order to avoid derailing the
830 * traversal to follow a side branch that brought everything
831 * in the path we are limited to by the pathspec.
833 if (revs
->first_parent_only
)
836 * If this will remain a potentially-simplifiable
837 * merge, remember per-parent treesame if needed.
838 * Initialise the array with the comparison from our
841 if (revs
->treesame
.name
&&
842 !revs
->simplify_history
&&
843 !(commit
->object
.flags
& UNINTERESTING
)) {
844 ts
= initialise_treesame(revs
, commit
);
845 if (!(irrelevant_change
|| relevant_change
))
849 if (parse_commit(p
) < 0)
850 die("cannot simplify commit %s (because of %s)",
851 oid_to_hex(&commit
->object
.oid
),
852 oid_to_hex(&p
->object
.oid
));
853 switch (rev_compare_tree(revs
, p
, commit
)) {
855 if (!revs
->simplify_history
|| !relevant_commit(p
)) {
856 /* Even if a merge with an uninteresting
857 * side branch brought the entire change
858 * we are interested in, we do not want
859 * to lose the other branches of this
860 * merge, so we just keep going.
863 ts
->treesame
[nth_parent
] = 1;
867 commit
->parents
= parent
;
868 commit
->object
.flags
|= TREESAME
;
872 if (revs
->remove_empty_trees
&&
873 rev_same_tree_as_empty(revs
, p
)) {
874 /* We are adding all the specified
875 * paths from this parent, so the
876 * history beyond this parent is not
877 * interesting. Remove its parents
878 * (they are grandparents for us).
879 * IOW, we pretend this parent is a
882 if (parse_commit(p
) < 0)
883 die("cannot simplify commit %s (invalid %s)",
884 oid_to_hex(&commit
->object
.oid
),
885 oid_to_hex(&p
->object
.oid
));
890 case REV_TREE_DIFFERENT
:
891 if (relevant_commit(p
))
894 irrelevant_change
= 1;
897 die("bad tree compare for commit %s", oid_to_hex(&commit
->object
.oid
));
901 * TREESAME is straightforward for single-parent commits. For merge
902 * commits, it is most useful to define it so that "irrelevant"
903 * parents cannot make us !TREESAME - if we have any relevant
904 * parents, then we only consider TREESAMEness with respect to them,
905 * allowing irrelevant merges from uninteresting branches to be
906 * simplified away. Only if we have only irrelevant parents do we
907 * base TREESAME on them. Note that this logic is replicated in
908 * update_treesame, which should be kept in sync.
910 if (relevant_parents
? !relevant_change
: !irrelevant_change
)
911 commit
->object
.flags
|= TREESAME
;
914 static int process_parents(struct rev_info
*revs
, struct commit
*commit
,
915 struct commit_list
**list
, struct prio_queue
*queue
)
917 struct commit_list
*parent
= commit
->parents
;
920 if (commit
->object
.flags
& ADDED
)
922 commit
->object
.flags
|= ADDED
;
924 if (revs
->include_check
&&
925 !revs
->include_check(commit
, revs
->include_check_data
))
929 * If the commit is uninteresting, don't try to
930 * prune parents - we want the maximal uninteresting
933 * Normally we haven't parsed the parent
934 * yet, so we won't have a parent of a parent
935 * here. However, it may turn out that we've
936 * reached this commit some other way (where it
937 * wasn't uninteresting), in which case we need
938 * to mark its parents recursively too..
940 if (commit
->object
.flags
& UNINTERESTING
) {
942 struct commit
*p
= parent
->item
;
943 parent
= parent
->next
;
945 p
->object
.flags
|= UNINTERESTING
;
946 if (parse_commit_gently(p
, 1) < 0)
949 mark_parents_uninteresting(p
);
950 if (p
->object
.flags
& SEEN
)
952 p
->object
.flags
|= SEEN
;
954 commit_list_insert_by_date(p
, list
);
956 prio_queue_put(queue
, p
);
962 * Ok, the commit wasn't uninteresting. Try to
963 * simplify the commit history and find the parent
964 * that has no differences in the path set if one exists.
966 try_to_simplify_commit(revs
, commit
);
971 left_flag
= (commit
->object
.flags
& SYMMETRIC_LEFT
);
973 for (parent
= commit
->parents
; parent
; parent
= parent
->next
) {
974 struct commit
*p
= parent
->item
;
975 int gently
= revs
->ignore_missing_links
||
976 revs
->exclude_promisor_objects
;
977 if (parse_commit_gently(p
, gently
) < 0) {
978 if (revs
->exclude_promisor_objects
&&
979 is_promisor_object(&p
->object
.oid
)) {
980 if (revs
->first_parent_only
)
987 char **slot
= revision_sources_at(revs
->sources
, p
);
990 *slot
= *revision_sources_at(revs
->sources
, commit
);
992 p
->object
.flags
|= left_flag
;
993 if (!(p
->object
.flags
& SEEN
)) {
994 p
->object
.flags
|= SEEN
;
996 commit_list_insert_by_date(p
, list
);
998 prio_queue_put(queue
, p
);
1000 if (revs
->first_parent_only
)
1006 static void cherry_pick_list(struct commit_list
*list
, struct rev_info
*revs
)
1008 struct commit_list
*p
;
1009 int left_count
= 0, right_count
= 0;
1011 struct patch_ids ids
;
1012 unsigned cherry_flag
;
1014 /* First count the commits on the left and on the right */
1015 for (p
= list
; p
; p
= p
->next
) {
1016 struct commit
*commit
= p
->item
;
1017 unsigned flags
= commit
->object
.flags
;
1018 if (flags
& BOUNDARY
)
1020 else if (flags
& SYMMETRIC_LEFT
)
1026 if (!left_count
|| !right_count
)
1029 left_first
= left_count
< right_count
;
1030 init_patch_ids(revs
->repo
, &ids
);
1031 ids
.diffopts
.pathspec
= revs
->diffopt
.pathspec
;
1033 /* Compute patch-ids for one side */
1034 for (p
= list
; p
; p
= p
->next
) {
1035 struct commit
*commit
= p
->item
;
1036 unsigned flags
= commit
->object
.flags
;
1038 if (flags
& BOUNDARY
)
1041 * If we have fewer left, left_first is set and we omit
1042 * commits on the right branch in this loop. If we have
1043 * fewer right, we skip the left ones.
1045 if (left_first
!= !!(flags
& SYMMETRIC_LEFT
))
1047 add_commit_patch_id(commit
, &ids
);
1050 /* either cherry_mark or cherry_pick are true */
1051 cherry_flag
= revs
->cherry_mark
? PATCHSAME
: SHOWN
;
1053 /* Check the other side */
1054 for (p
= list
; p
; p
= p
->next
) {
1055 struct commit
*commit
= p
->item
;
1056 struct patch_id
*id
;
1057 unsigned flags
= commit
->object
.flags
;
1059 if (flags
& BOUNDARY
)
1062 * If we have fewer left, left_first is set and we omit
1063 * commits on the left branch in this loop.
1065 if (left_first
== !!(flags
& SYMMETRIC_LEFT
))
1069 * Have we seen the same patch id?
1071 id
= has_commit_patch_id(commit
, &ids
);
1075 commit
->object
.flags
|= cherry_flag
;
1076 id
->commit
->object
.flags
|= cherry_flag
;
1079 free_patch_ids(&ids
);
1082 /* How many extra uninteresting commits we want to see.. */
1085 static int still_interesting(struct commit_list
*src
, timestamp_t date
, int slop
,
1086 struct commit
**interesting_cache
)
1089 * No source list at all? We're definitely done..
1095 * Does the destination list contain entries with a date
1096 * before the source list? Definitely _not_ done.
1098 if (date
<= src
->item
->date
)
1102 * Does the source list still have interesting commits in
1103 * it? Definitely not done..
1105 if (!everybody_uninteresting(src
, interesting_cache
))
1108 /* Ok, we're closing in.. */
1113 * "rev-list --ancestry-path A..B" computes commits that are ancestors
1114 * of B but not ancestors of A but further limits the result to those
1115 * that are descendants of A. This takes the list of bottom commits and
1116 * the result of "A..B" without --ancestry-path, and limits the latter
1117 * further to the ones that can reach one of the commits in "bottom".
1119 static void limit_to_ancestry(struct commit_list
*bottom
, struct commit_list
*list
)
1121 struct commit_list
*p
;
1122 struct commit_list
*rlist
= NULL
;
1126 * Reverse the list so that it will be likely that we would
1127 * process parents before children.
1129 for (p
= list
; p
; p
= p
->next
)
1130 commit_list_insert(p
->item
, &rlist
);
1132 for (p
= bottom
; p
; p
= p
->next
)
1133 p
->item
->object
.flags
|= TMP_MARK
;
1136 * Mark the ones that can reach bottom commits in "list",
1137 * in a bottom-up fashion.
1141 for (p
= rlist
; p
; p
= p
->next
) {
1142 struct commit
*c
= p
->item
;
1143 struct commit_list
*parents
;
1144 if (c
->object
.flags
& (TMP_MARK
| UNINTERESTING
))
1146 for (parents
= c
->parents
;
1148 parents
= parents
->next
) {
1149 if (!(parents
->item
->object
.flags
& TMP_MARK
))
1151 c
->object
.flags
|= TMP_MARK
;
1156 } while (made_progress
);
1159 * NEEDSWORK: decide if we want to remove parents that are
1160 * not marked with TMP_MARK from commit->parents for commits
1161 * in the resulting list. We may not want to do that, though.
1165 * The ones that are not marked with TMP_MARK are uninteresting
1167 for (p
= list
; p
; p
= p
->next
) {
1168 struct commit
*c
= p
->item
;
1169 if (c
->object
.flags
& TMP_MARK
)
1171 c
->object
.flags
|= UNINTERESTING
;
1174 /* We are done with the TMP_MARK */
1175 for (p
= list
; p
; p
= p
->next
)
1176 p
->item
->object
.flags
&= ~TMP_MARK
;
1177 for (p
= bottom
; p
; p
= p
->next
)
1178 p
->item
->object
.flags
&= ~TMP_MARK
;
1179 free_commit_list(rlist
);
1183 * Before walking the history, keep the set of "negative" refs the
1184 * caller has asked to exclude.
1186 * This is used to compute "rev-list --ancestry-path A..B", as we need
1187 * to filter the result of "A..B" further to the ones that can actually
1190 static struct commit_list
*collect_bottom_commits(struct commit_list
*list
)
1192 struct commit_list
*elem
, *bottom
= NULL
;
1193 for (elem
= list
; elem
; elem
= elem
->next
)
1194 if (elem
->item
->object
.flags
& BOTTOM
)
1195 commit_list_insert(elem
->item
, &bottom
);
1199 /* Assumes either left_only or right_only is set */
1200 static void limit_left_right(struct commit_list
*list
, struct rev_info
*revs
)
1202 struct commit_list
*p
;
1204 for (p
= list
; p
; p
= p
->next
) {
1205 struct commit
*commit
= p
->item
;
1207 if (revs
->right_only
) {
1208 if (commit
->object
.flags
& SYMMETRIC_LEFT
)
1209 commit
->object
.flags
|= SHOWN
;
1210 } else /* revs->left_only is set */
1211 if (!(commit
->object
.flags
& SYMMETRIC_LEFT
))
1212 commit
->object
.flags
|= SHOWN
;
1216 static int limit_list(struct rev_info
*revs
)
1219 timestamp_t date
= TIME_MAX
;
1220 struct commit_list
*list
= revs
->commits
;
1221 struct commit_list
*newlist
= NULL
;
1222 struct commit_list
**p
= &newlist
;
1223 struct commit_list
*bottom
= NULL
;
1224 struct commit
*interesting_cache
= NULL
;
1226 if (revs
->ancestry_path
) {
1227 bottom
= collect_bottom_commits(list
);
1229 die("--ancestry-path given but there are no bottom commits");
1233 struct commit
*commit
= pop_commit(&list
);
1234 struct object
*obj
= &commit
->object
;
1235 show_early_output_fn_t show
;
1237 if (commit
== interesting_cache
)
1238 interesting_cache
= NULL
;
1240 if (revs
->max_age
!= -1 && (commit
->date
< revs
->max_age
))
1241 obj
->flags
|= UNINTERESTING
;
1242 if (process_parents(revs
, commit
, &list
, NULL
) < 0)
1244 if (obj
->flags
& UNINTERESTING
) {
1245 mark_parents_uninteresting(commit
);
1246 slop
= still_interesting(list
, date
, slop
, &interesting_cache
);
1251 if (revs
->min_age
!= -1 && (commit
->date
> revs
->min_age
))
1253 date
= commit
->date
;
1254 p
= &commit_list_insert(commit
, p
)->next
;
1256 show
= show_early_output
;
1260 show(revs
, newlist
);
1261 show_early_output
= NULL
;
1263 if (revs
->cherry_pick
|| revs
->cherry_mark
)
1264 cherry_pick_list(newlist
, revs
);
1266 if (revs
->left_only
|| revs
->right_only
)
1267 limit_left_right(newlist
, revs
);
1270 limit_to_ancestry(bottom
, newlist
);
1271 free_commit_list(bottom
);
1275 * Check if any commits have become TREESAME by some of their parents
1276 * becoming UNINTERESTING.
1278 if (limiting_can_increase_treesame(revs
))
1279 for (list
= newlist
; list
; list
= list
->next
) {
1280 struct commit
*c
= list
->item
;
1281 if (c
->object
.flags
& (UNINTERESTING
| TREESAME
))
1283 update_treesame(revs
, c
);
1286 revs
->commits
= newlist
;
1291 * Add an entry to refs->cmdline with the specified information.
1294 static void add_rev_cmdline(struct rev_info
*revs
,
1295 struct object
*item
,
1300 struct rev_cmdline_info
*info
= &revs
->cmdline
;
1301 unsigned int nr
= info
->nr
;
1303 ALLOC_GROW(info
->rev
, nr
+ 1, info
->alloc
);
1304 info
->rev
[nr
].item
= item
;
1305 info
->rev
[nr
].name
= xstrdup(name
);
1306 info
->rev
[nr
].whence
= whence
;
1307 info
->rev
[nr
].flags
= flags
;
1311 static void add_rev_cmdline_list(struct rev_info
*revs
,
1312 struct commit_list
*commit_list
,
1316 while (commit_list
) {
1317 struct object
*object
= &commit_list
->item
->object
;
1318 add_rev_cmdline(revs
, object
, oid_to_hex(&object
->oid
),
1320 commit_list
= commit_list
->next
;
1324 struct all_refs_cb
{
1326 int warned_bad_reflog
;
1327 struct rev_info
*all_revs
;
1328 const char *name_for_errormsg
;
1329 struct worktree
*wt
;
1332 int ref_excluded(struct string_list
*ref_excludes
, const char *path
)
1334 struct string_list_item
*item
;
1338 for_each_string_list_item(item
, ref_excludes
) {
1339 if (!wildmatch(item
->string
, path
, 0))
1345 static int handle_one_ref(const char *path
, const struct object_id
*oid
,
1346 int flag
, void *cb_data
)
1348 struct all_refs_cb
*cb
= cb_data
;
1349 struct object
*object
;
1351 if (ref_excluded(cb
->all_revs
->ref_excludes
, path
))
1354 object
= get_reference(cb
->all_revs
, path
, oid
, cb
->all_flags
);
1355 add_rev_cmdline(cb
->all_revs
, object
, path
, REV_CMD_REF
, cb
->all_flags
);
1356 add_pending_oid(cb
->all_revs
, path
, oid
, cb
->all_flags
);
1360 static void init_all_refs_cb(struct all_refs_cb
*cb
, struct rev_info
*revs
,
1363 cb
->all_revs
= revs
;
1364 cb
->all_flags
= flags
;
1365 revs
->rev_input_given
= 1;
1369 void clear_ref_exclusion(struct string_list
**ref_excludes_p
)
1371 if (*ref_excludes_p
) {
1372 string_list_clear(*ref_excludes_p
, 0);
1373 free(*ref_excludes_p
);
1375 *ref_excludes_p
= NULL
;
1378 void add_ref_exclusion(struct string_list
**ref_excludes_p
, const char *exclude
)
1380 if (!*ref_excludes_p
) {
1381 *ref_excludes_p
= xcalloc(1, sizeof(**ref_excludes_p
));
1382 (*ref_excludes_p
)->strdup_strings
= 1;
1384 string_list_append(*ref_excludes_p
, exclude
);
1387 static void handle_refs(struct ref_store
*refs
,
1388 struct rev_info
*revs
, unsigned flags
,
1389 int (*for_each
)(struct ref_store
*, each_ref_fn
, void *))
1391 struct all_refs_cb cb
;
1394 /* this could happen with uninitialized submodules */
1398 init_all_refs_cb(&cb
, revs
, flags
);
1399 for_each(refs
, handle_one_ref
, &cb
);
1402 static void handle_one_reflog_commit(struct object_id
*oid
, void *cb_data
)
1404 struct all_refs_cb
*cb
= cb_data
;
1405 if (!is_null_oid(oid
)) {
1406 struct object
*o
= parse_object(cb
->all_revs
->repo
, oid
);
1408 o
->flags
|= cb
->all_flags
;
1409 /* ??? CMDLINEFLAGS ??? */
1410 add_pending_object(cb
->all_revs
, o
, "");
1412 else if (!cb
->warned_bad_reflog
) {
1413 warning("reflog of '%s' references pruned commits",
1414 cb
->name_for_errormsg
);
1415 cb
->warned_bad_reflog
= 1;
1420 static int handle_one_reflog_ent(struct object_id
*ooid
, struct object_id
*noid
,
1421 const char *email
, timestamp_t timestamp
, int tz
,
1422 const char *message
, void *cb_data
)
1424 handle_one_reflog_commit(ooid
, cb_data
);
1425 handle_one_reflog_commit(noid
, cb_data
);
1429 static int handle_one_reflog(const char *refname_in_wt
,
1430 const struct object_id
*oid
,
1431 int flag
, void *cb_data
)
1433 struct all_refs_cb
*cb
= cb_data
;
1434 struct strbuf refname
= STRBUF_INIT
;
1436 cb
->warned_bad_reflog
= 0;
1437 strbuf_worktree_ref(cb
->wt
, &refname
, refname_in_wt
);
1438 cb
->name_for_errormsg
= refname
.buf
;
1439 refs_for_each_reflog_ent(get_main_ref_store(the_repository
),
1441 handle_one_reflog_ent
, cb_data
);
1442 strbuf_release(&refname
);
1446 static void add_other_reflogs_to_pending(struct all_refs_cb
*cb
)
1448 struct worktree
**worktrees
, **p
;
1450 worktrees
= get_worktrees(0);
1451 for (p
= worktrees
; *p
; p
++) {
1452 struct worktree
*wt
= *p
;
1458 refs_for_each_reflog(get_worktree_ref_store(wt
),
1462 free_worktrees(worktrees
);
1465 void add_reflogs_to_pending(struct rev_info
*revs
, unsigned flags
)
1467 struct all_refs_cb cb
;
1470 cb
.all_flags
= flags
;
1472 for_each_reflog(handle_one_reflog
, &cb
);
1474 if (!revs
->single_worktree
)
1475 add_other_reflogs_to_pending(&cb
);
1478 static void add_cache_tree(struct cache_tree
*it
, struct rev_info
*revs
,
1479 struct strbuf
*path
, unsigned int flags
)
1481 size_t baselen
= path
->len
;
1484 if (it
->entry_count
>= 0) {
1485 struct tree
*tree
= lookup_tree(revs
->repo
, &it
->oid
);
1486 tree
->object
.flags
|= flags
;
1487 add_pending_object_with_path(revs
, &tree
->object
, "",
1491 for (i
= 0; i
< it
->subtree_nr
; i
++) {
1492 struct cache_tree_sub
*sub
= it
->down
[i
];
1493 strbuf_addf(path
, "%s%s", baselen
? "/" : "", sub
->name
);
1494 add_cache_tree(sub
->cache_tree
, revs
, path
, flags
);
1495 strbuf_setlen(path
, baselen
);
1500 static void do_add_index_objects_to_pending(struct rev_info
*revs
,
1501 struct index_state
*istate
,
1506 for (i
= 0; i
< istate
->cache_nr
; i
++) {
1507 struct cache_entry
*ce
= istate
->cache
[i
];
1510 if (S_ISGITLINK(ce
->ce_mode
))
1513 blob
= lookup_blob(revs
->repo
, &ce
->oid
);
1515 die("unable to add index blob to traversal");
1516 blob
->object
.flags
|= flags
;
1517 add_pending_object_with_path(revs
, &blob
->object
, "",
1518 ce
->ce_mode
, ce
->name
);
1521 if (istate
->cache_tree
) {
1522 struct strbuf path
= STRBUF_INIT
;
1523 add_cache_tree(istate
->cache_tree
, revs
, &path
, flags
);
1524 strbuf_release(&path
);
1528 void add_index_objects_to_pending(struct rev_info
*revs
, unsigned int flags
)
1530 struct worktree
**worktrees
, **p
;
1532 repo_read_index(revs
->repo
);
1533 do_add_index_objects_to_pending(revs
, revs
->repo
->index
, flags
);
1535 if (revs
->single_worktree
)
1538 worktrees
= get_worktrees(0);
1539 for (p
= worktrees
; *p
; p
++) {
1540 struct worktree
*wt
= *p
;
1541 struct index_state istate
= { NULL
};
1544 continue; /* current index already taken care of */
1546 if (read_index_from(&istate
,
1547 worktree_git_path(wt
, "index"),
1548 get_worktree_git_dir(wt
)) > 0)
1549 do_add_index_objects_to_pending(revs
, &istate
, flags
);
1550 discard_index(&istate
);
1552 free_worktrees(worktrees
);
1555 struct add_alternate_refs_data
{
1556 struct rev_info
*revs
;
1560 static void add_one_alternate_ref(const struct object_id
*oid
,
1563 const char *name
= ".alternate";
1564 struct add_alternate_refs_data
*data
= vdata
;
1567 obj
= get_reference(data
->revs
, name
, oid
, data
->flags
);
1568 add_rev_cmdline(data
->revs
, obj
, name
, REV_CMD_REV
, data
->flags
);
1569 add_pending_object(data
->revs
, obj
, name
);
1572 static void add_alternate_refs_to_pending(struct rev_info
*revs
,
1575 struct add_alternate_refs_data data
;
1578 for_each_alternate_ref(add_one_alternate_ref
, &data
);
1581 static int add_parents_only(struct rev_info
*revs
, const char *arg_
, int flags
,
1584 struct object_id oid
;
1586 struct commit
*commit
;
1587 struct commit_list
*parents
;
1589 const char *arg
= arg_
;
1592 flags
^= UNINTERESTING
| BOTTOM
;
1595 if (get_oid_committish(arg
, &oid
))
1598 it
= get_reference(revs
, arg
, &oid
, 0);
1599 if (!it
&& revs
->ignore_missing
)
1601 if (it
->type
!= OBJ_TAG
)
1603 if (!((struct tag
*)it
)->tagged
)
1605 oidcpy(&oid
, &((struct tag
*)it
)->tagged
->oid
);
1607 if (it
->type
!= OBJ_COMMIT
)
1609 commit
= (struct commit
*)it
;
1610 if (exclude_parent
&&
1611 exclude_parent
> commit_list_count(commit
->parents
))
1613 for (parents
= commit
->parents
, parent_number
= 1;
1615 parents
= parents
->next
, parent_number
++) {
1616 if (exclude_parent
&& parent_number
!= exclude_parent
)
1619 it
= &parents
->item
->object
;
1621 add_rev_cmdline(revs
, it
, arg_
, REV_CMD_PARENTS_ONLY
, flags
);
1622 add_pending_object(revs
, it
, arg
);
1627 void repo_init_revisions(struct repository
*r
,
1628 struct rev_info
*revs
,
1631 memset(revs
, 0, sizeof(*revs
));
1634 revs
->abbrev
= DEFAULT_ABBREV
;
1635 revs
->ignore_merges
= 1;
1636 revs
->simplify_history
= 1;
1637 revs
->pruning
.repo
= r
;
1638 revs
->pruning
.flags
.recursive
= 1;
1639 revs
->pruning
.flags
.quick
= 1;
1640 revs
->pruning
.add_remove
= file_add_remove
;
1641 revs
->pruning
.change
= file_change
;
1642 revs
->pruning
.change_fn_data
= revs
;
1643 revs
->sort_order
= REV_SORT_IN_GRAPH_ORDER
;
1645 revs
->prefix
= prefix
;
1648 revs
->skip_count
= -1;
1649 revs
->max_count
= -1;
1650 revs
->max_parents
= -1;
1651 revs
->expand_tabs_in_log
= -1;
1653 revs
->commit_format
= CMIT_FMT_DEFAULT
;
1654 revs
->expand_tabs_in_log_default
= 8;
1656 init_grep_defaults(revs
->repo
);
1657 grep_init(&revs
->grep_filter
, revs
->repo
, prefix
);
1658 revs
->grep_filter
.status_only
= 1;
1660 repo_diff_setup(revs
->repo
, &revs
->diffopt
);
1661 if (prefix
&& !revs
->diffopt
.prefix
) {
1662 revs
->diffopt
.prefix
= prefix
;
1663 revs
->diffopt
.prefix_length
= strlen(prefix
);
1666 revs
->notes_opt
.use_default_notes
= -1;
1669 static void add_pending_commit_list(struct rev_info
*revs
,
1670 struct commit_list
*commit_list
,
1673 while (commit_list
) {
1674 struct object
*object
= &commit_list
->item
->object
;
1675 object
->flags
|= flags
;
1676 add_pending_object(revs
, object
, oid_to_hex(&object
->oid
));
1677 commit_list
= commit_list
->next
;
1681 static void prepare_show_merge(struct rev_info
*revs
)
1683 struct commit_list
*bases
;
1684 struct commit
*head
, *other
;
1685 struct object_id oid
;
1686 const char **prune
= NULL
;
1687 int i
, prune_num
= 1; /* counting terminating NULL */
1688 struct index_state
*istate
= revs
->repo
->index
;
1690 if (get_oid("HEAD", &oid
))
1691 die("--merge without HEAD?");
1692 head
= lookup_commit_or_die(&oid
, "HEAD");
1693 if (get_oid("MERGE_HEAD", &oid
))
1694 die("--merge without MERGE_HEAD?");
1695 other
= lookup_commit_or_die(&oid
, "MERGE_HEAD");
1696 add_pending_object(revs
, &head
->object
, "HEAD");
1697 add_pending_object(revs
, &other
->object
, "MERGE_HEAD");
1698 bases
= get_merge_bases(head
, other
);
1699 add_rev_cmdline_list(revs
, bases
, REV_CMD_MERGE_BASE
, UNINTERESTING
| BOTTOM
);
1700 add_pending_commit_list(revs
, bases
, UNINTERESTING
| BOTTOM
);
1701 free_commit_list(bases
);
1702 head
->object
.flags
|= SYMMETRIC_LEFT
;
1704 if (!istate
->cache_nr
)
1705 repo_read_index(revs
->repo
);
1706 for (i
= 0; i
< istate
->cache_nr
; i
++) {
1707 const struct cache_entry
*ce
= istate
->cache
[i
];
1710 if (ce_path_match(istate
, ce
, &revs
->prune_data
, NULL
)) {
1712 REALLOC_ARRAY(prune
, prune_num
);
1713 prune
[prune_num
-2] = ce
->name
;
1714 prune
[prune_num
-1] = NULL
;
1716 while ((i
+1 < istate
->cache_nr
) &&
1717 ce_same_name(ce
, istate
->cache
[i
+1]))
1720 clear_pathspec(&revs
->prune_data
);
1721 parse_pathspec(&revs
->prune_data
, PATHSPEC_ALL_MAGIC
& ~PATHSPEC_LITERAL
,
1722 PATHSPEC_PREFER_FULL
| PATHSPEC_LITERAL_PATH
, "", prune
);
1726 static int dotdot_missing(const char *arg
, char *dotdot
,
1727 struct rev_info
*revs
, int symmetric
)
1729 if (revs
->ignore_missing
)
1731 /* de-munge so we report the full argument */
1734 ? "Invalid symmetric difference expression %s"
1735 : "Invalid revision range %s", arg
);
1738 static int handle_dotdot_1(const char *arg
, char *dotdot
,
1739 struct rev_info
*revs
, int flags
,
1740 int cant_be_filename
,
1741 struct object_context
*a_oc
,
1742 struct object_context
*b_oc
)
1744 const char *a_name
, *b_name
;
1745 struct object_id a_oid
, b_oid
;
1746 struct object
*a_obj
, *b_obj
;
1747 unsigned int a_flags
, b_flags
;
1749 unsigned int flags_exclude
= flags
^ (UNINTERESTING
| BOTTOM
);
1750 unsigned int oc_flags
= GET_OID_COMMITTISH
| GET_OID_RECORD_PATH
;
1756 b_name
= dotdot
+ 2;
1757 if (*b_name
== '.') {
1764 if (get_oid_with_context(revs
->repo
, a_name
, oc_flags
, &a_oid
, a_oc
) ||
1765 get_oid_with_context(revs
->repo
, b_name
, oc_flags
, &b_oid
, b_oc
))
1768 if (!cant_be_filename
) {
1770 verify_non_filename(revs
->prefix
, arg
);
1774 a_obj
= parse_object(revs
->repo
, &a_oid
);
1775 b_obj
= parse_object(revs
->repo
, &b_oid
);
1776 if (!a_obj
|| !b_obj
)
1777 return dotdot_missing(arg
, dotdot
, revs
, symmetric
);
1782 a_flags
= flags_exclude
;
1784 /* A...B -- find merge bases between the two */
1785 struct commit
*a
, *b
;
1786 struct commit_list
*exclude
;
1788 a
= lookup_commit_reference(revs
->repo
, &a_obj
->oid
);
1789 b
= lookup_commit_reference(revs
->repo
, &b_obj
->oid
);
1791 return dotdot_missing(arg
, dotdot
, revs
, symmetric
);
1793 exclude
= get_merge_bases(a
, b
);
1794 add_rev_cmdline_list(revs
, exclude
, REV_CMD_MERGE_BASE
,
1796 add_pending_commit_list(revs
, exclude
, flags_exclude
);
1797 free_commit_list(exclude
);
1800 a_flags
= flags
| SYMMETRIC_LEFT
;
1803 a_obj
->flags
|= a_flags
;
1804 b_obj
->flags
|= b_flags
;
1805 add_rev_cmdline(revs
, a_obj
, a_name
, REV_CMD_LEFT
, a_flags
);
1806 add_rev_cmdline(revs
, b_obj
, b_name
, REV_CMD_RIGHT
, b_flags
);
1807 add_pending_object_with_path(revs
, a_obj
, a_name
, a_oc
->mode
, a_oc
->path
);
1808 add_pending_object_with_path(revs
, b_obj
, b_name
, b_oc
->mode
, b_oc
->path
);
1812 static int handle_dotdot(const char *arg
,
1813 struct rev_info
*revs
, int flags
,
1814 int cant_be_filename
)
1816 struct object_context a_oc
, b_oc
;
1817 char *dotdot
= strstr(arg
, "..");
1823 memset(&a_oc
, 0, sizeof(a_oc
));
1824 memset(&b_oc
, 0, sizeof(b_oc
));
1827 ret
= handle_dotdot_1(arg
, dotdot
, revs
, flags
, cant_be_filename
,
1837 int handle_revision_arg(const char *arg_
, struct rev_info
*revs
, int flags
, unsigned revarg_opt
)
1839 struct object_context oc
;
1841 struct object
*object
;
1842 struct object_id oid
;
1844 const char *arg
= arg_
;
1845 int cant_be_filename
= revarg_opt
& REVARG_CANNOT_BE_FILENAME
;
1846 unsigned get_sha1_flags
= GET_OID_RECORD_PATH
;
1848 flags
= flags
& UNINTERESTING
? flags
| BOTTOM
: flags
& ~BOTTOM
;
1850 if (!cant_be_filename
&& !strcmp(arg
, "..")) {
1852 * Just ".."? That is not a range but the
1853 * pathspec for the parent directory.
1858 if (!handle_dotdot(arg
, revs
, flags
, revarg_opt
))
1861 mark
= strstr(arg
, "^@");
1862 if (mark
&& !mark
[2]) {
1864 if (add_parents_only(revs
, arg
, flags
, 0))
1868 mark
= strstr(arg
, "^!");
1869 if (mark
&& !mark
[2]) {
1871 if (!add_parents_only(revs
, arg
, flags
^ (UNINTERESTING
| BOTTOM
), 0))
1874 mark
= strstr(arg
, "^-");
1876 int exclude_parent
= 1;
1880 exclude_parent
= strtoul(mark
+ 2, &end
, 10);
1881 if (*end
!= '\0' || !exclude_parent
)
1886 if (!add_parents_only(revs
, arg
, flags
^ (UNINTERESTING
| BOTTOM
), exclude_parent
))
1892 local_flags
= UNINTERESTING
| BOTTOM
;
1896 if (revarg_opt
& REVARG_COMMITTISH
)
1897 get_sha1_flags
|= GET_OID_COMMITTISH
;
1899 if (get_oid_with_context(revs
->repo
, arg
, get_sha1_flags
, &oid
, &oc
))
1900 return revs
->ignore_missing
? 0 : -1;
1901 if (!cant_be_filename
)
1902 verify_non_filename(revs
->prefix
, arg
);
1903 object
= get_reference(revs
, arg
, &oid
, flags
^ local_flags
);
1905 return revs
->ignore_missing
? 0 : -1;
1906 add_rev_cmdline(revs
, object
, arg_
, REV_CMD_REV
, flags
^ local_flags
);
1907 add_pending_object_with_path(revs
, object
, arg
, oc
.mode
, oc
.path
);
1912 static void read_pathspec_from_stdin(struct strbuf
*sb
,
1913 struct argv_array
*prune
)
1915 while (strbuf_getline(sb
, stdin
) != EOF
)
1916 argv_array_push(prune
, sb
->buf
);
1919 static void read_revisions_from_stdin(struct rev_info
*revs
,
1920 struct argv_array
*prune
)
1923 int seen_dashdash
= 0;
1926 save_warning
= warn_on_object_refname_ambiguity
;
1927 warn_on_object_refname_ambiguity
= 0;
1929 strbuf_init(&sb
, 1000);
1930 while (strbuf_getline(&sb
, stdin
) != EOF
) {
1934 if (sb
.buf
[0] == '-') {
1935 if (len
== 2 && sb
.buf
[1] == '-') {
1939 die("options not supported in --stdin mode");
1941 if (handle_revision_arg(sb
.buf
, revs
, 0,
1942 REVARG_CANNOT_BE_FILENAME
))
1943 die("bad revision '%s'", sb
.buf
);
1946 read_pathspec_from_stdin(&sb
, prune
);
1948 strbuf_release(&sb
);
1949 warn_on_object_refname_ambiguity
= save_warning
;
1952 static void add_grep(struct rev_info
*revs
, const char *ptn
, enum grep_pat_token what
)
1954 append_grep_pattern(&revs
->grep_filter
, ptn
, "command line", 0, what
);
1957 static void add_header_grep(struct rev_info
*revs
, enum grep_header_field field
, const char *pattern
)
1959 append_header_grep_pattern(&revs
->grep_filter
, field
, pattern
);
1962 static void add_message_grep(struct rev_info
*revs
, const char *pattern
)
1964 add_grep(revs
, pattern
, GREP_PATTERN_BODY
);
1967 static int handle_revision_opt(struct rev_info
*revs
, int argc
, const char **argv
,
1968 int *unkc
, const char **unkv
,
1969 const struct setup_revision_opt
* opt
)
1971 const char *arg
= argv
[0];
1974 const unsigned hexsz
= the_hash_algo
->hexsz
;
1976 /* pseudo revision arguments */
1977 if (!strcmp(arg
, "--all") || !strcmp(arg
, "--branches") ||
1978 !strcmp(arg
, "--tags") || !strcmp(arg
, "--remotes") ||
1979 !strcmp(arg
, "--reflog") || !strcmp(arg
, "--not") ||
1980 !strcmp(arg
, "--no-walk") || !strcmp(arg
, "--do-walk") ||
1981 !strcmp(arg
, "--bisect") || starts_with(arg
, "--glob=") ||
1982 !strcmp(arg
, "--indexed-objects") ||
1983 !strcmp(arg
, "--alternate-refs") ||
1984 starts_with(arg
, "--exclude=") ||
1985 starts_with(arg
, "--branches=") || starts_with(arg
, "--tags=") ||
1986 starts_with(arg
, "--remotes=") || starts_with(arg
, "--no-walk="))
1988 unkv
[(*unkc
)++] = arg
;
1992 if ((argcount
= parse_long_opt("max-count", argv
, &optarg
))) {
1993 revs
->max_count
= atoi(optarg
);
1996 } else if ((argcount
= parse_long_opt("skip", argv
, &optarg
))) {
1997 revs
->skip_count
= atoi(optarg
);
1999 } else if ((*arg
== '-') && isdigit(arg
[1])) {
2000 /* accept -<digit>, like traditional "head" */
2001 if (strtol_i(arg
+ 1, 10, &revs
->max_count
) < 0 ||
2002 revs
->max_count
< 0)
2003 die("'%s': not a non-negative integer", arg
+ 1);
2005 } else if (!strcmp(arg
, "-n")) {
2007 return error("-n requires an argument");
2008 revs
->max_count
= atoi(argv
[1]);
2011 } else if (skip_prefix(arg
, "-n", &optarg
)) {
2012 revs
->max_count
= atoi(optarg
);
2014 } else if ((argcount
= parse_long_opt("max-age", argv
, &optarg
))) {
2015 revs
->max_age
= atoi(optarg
);
2017 } else if ((argcount
= parse_long_opt("since", argv
, &optarg
))) {
2018 revs
->max_age
= approxidate(optarg
);
2020 } else if ((argcount
= parse_long_opt("after", argv
, &optarg
))) {
2021 revs
->max_age
= approxidate(optarg
);
2023 } else if ((argcount
= parse_long_opt("min-age", argv
, &optarg
))) {
2024 revs
->min_age
= atoi(optarg
);
2026 } else if ((argcount
= parse_long_opt("before", argv
, &optarg
))) {
2027 revs
->min_age
= approxidate(optarg
);
2029 } else if ((argcount
= parse_long_opt("until", argv
, &optarg
))) {
2030 revs
->min_age
= approxidate(optarg
);
2032 } else if (!strcmp(arg
, "--first-parent")) {
2033 revs
->first_parent_only
= 1;
2034 } else if (!strcmp(arg
, "--ancestry-path")) {
2035 revs
->ancestry_path
= 1;
2036 revs
->simplify_history
= 0;
2038 } else if (!strcmp(arg
, "-g") || !strcmp(arg
, "--walk-reflogs")) {
2039 init_reflog_walk(&revs
->reflog_info
);
2040 } else if (!strcmp(arg
, "--default")) {
2042 return error("bad --default argument");
2043 revs
->def
= argv
[1];
2045 } else if (!strcmp(arg
, "--merge")) {
2046 revs
->show_merge
= 1;
2047 } else if (!strcmp(arg
, "--topo-order")) {
2048 revs
->sort_order
= REV_SORT_IN_GRAPH_ORDER
;
2049 revs
->topo_order
= 1;
2050 } else if (!strcmp(arg
, "--simplify-merges")) {
2051 revs
->simplify_merges
= 1;
2052 revs
->topo_order
= 1;
2053 revs
->rewrite_parents
= 1;
2054 revs
->simplify_history
= 0;
2056 } else if (!strcmp(arg
, "--simplify-by-decoration")) {
2057 revs
->simplify_merges
= 1;
2058 revs
->topo_order
= 1;
2059 revs
->rewrite_parents
= 1;
2060 revs
->simplify_history
= 0;
2061 revs
->simplify_by_decoration
= 1;
2064 } else if (!strcmp(arg
, "--date-order")) {
2065 revs
->sort_order
= REV_SORT_BY_COMMIT_DATE
;
2066 revs
->topo_order
= 1;
2067 } else if (!strcmp(arg
, "--author-date-order")) {
2068 revs
->sort_order
= REV_SORT_BY_AUTHOR_DATE
;
2069 revs
->topo_order
= 1;
2070 } else if (!strcmp(arg
, "--early-output")) {
2071 revs
->early_output
= 100;
2072 revs
->topo_order
= 1;
2073 } else if (skip_prefix(arg
, "--early-output=", &optarg
)) {
2074 if (strtoul_ui(optarg
, 10, &revs
->early_output
) < 0)
2075 die("'%s': not a non-negative integer", optarg
);
2076 revs
->topo_order
= 1;
2077 } else if (!strcmp(arg
, "--parents")) {
2078 revs
->rewrite_parents
= 1;
2079 revs
->print_parents
= 1;
2080 } else if (!strcmp(arg
, "--dense")) {
2082 } else if (!strcmp(arg
, "--sparse")) {
2084 } else if (!strcmp(arg
, "--in-commit-order")) {
2085 revs
->tree_blobs_in_commit_order
= 1;
2086 } else if (!strcmp(arg
, "--remove-empty")) {
2087 revs
->remove_empty_trees
= 1;
2088 } else if (!strcmp(arg
, "--merges")) {
2089 revs
->min_parents
= 2;
2090 } else if (!strcmp(arg
, "--no-merges")) {
2091 revs
->max_parents
= 1;
2092 } else if (skip_prefix(arg
, "--min-parents=", &optarg
)) {
2093 revs
->min_parents
= atoi(optarg
);
2094 } else if (!strcmp(arg
, "--no-min-parents")) {
2095 revs
->min_parents
= 0;
2096 } else if (skip_prefix(arg
, "--max-parents=", &optarg
)) {
2097 revs
->max_parents
= atoi(optarg
);
2098 } else if (!strcmp(arg
, "--no-max-parents")) {
2099 revs
->max_parents
= -1;
2100 } else if (!strcmp(arg
, "--boundary")) {
2102 } else if (!strcmp(arg
, "--left-right")) {
2103 revs
->left_right
= 1;
2104 } else if (!strcmp(arg
, "--left-only")) {
2105 if (revs
->right_only
)
2106 die("--left-only is incompatible with --right-only"
2108 revs
->left_only
= 1;
2109 } else if (!strcmp(arg
, "--right-only")) {
2110 if (revs
->left_only
)
2111 die("--right-only is incompatible with --left-only");
2112 revs
->right_only
= 1;
2113 } else if (!strcmp(arg
, "--cherry")) {
2114 if (revs
->left_only
)
2115 die("--cherry is incompatible with --left-only");
2116 revs
->cherry_mark
= 1;
2117 revs
->right_only
= 1;
2118 revs
->max_parents
= 1;
2120 } else if (!strcmp(arg
, "--count")) {
2122 } else if (!strcmp(arg
, "--cherry-mark")) {
2123 if (revs
->cherry_pick
)
2124 die("--cherry-mark is incompatible with --cherry-pick");
2125 revs
->cherry_mark
= 1;
2126 revs
->limited
= 1; /* needs limit_list() */
2127 } else if (!strcmp(arg
, "--cherry-pick")) {
2128 if (revs
->cherry_mark
)
2129 die("--cherry-pick is incompatible with --cherry-mark");
2130 revs
->cherry_pick
= 1;
2132 } else if (!strcmp(arg
, "--objects")) {
2133 revs
->tag_objects
= 1;
2134 revs
->tree_objects
= 1;
2135 revs
->blob_objects
= 1;
2136 } else if (!strcmp(arg
, "--objects-edge")) {
2137 revs
->tag_objects
= 1;
2138 revs
->tree_objects
= 1;
2139 revs
->blob_objects
= 1;
2140 revs
->edge_hint
= 1;
2141 } else if (!strcmp(arg
, "--objects-edge-aggressive")) {
2142 revs
->tag_objects
= 1;
2143 revs
->tree_objects
= 1;
2144 revs
->blob_objects
= 1;
2145 revs
->edge_hint
= 1;
2146 revs
->edge_hint_aggressive
= 1;
2147 } else if (!strcmp(arg
, "--verify-objects")) {
2148 revs
->tag_objects
= 1;
2149 revs
->tree_objects
= 1;
2150 revs
->blob_objects
= 1;
2151 revs
->verify_objects
= 1;
2152 } else if (!strcmp(arg
, "--unpacked")) {
2154 } else if (starts_with(arg
, "--unpacked=")) {
2155 die("--unpacked=<packfile> no longer supported.");
2156 } else if (!strcmp(arg
, "-r")) {
2158 revs
->diffopt
.flags
.recursive
= 1;
2159 } else if (!strcmp(arg
, "-t")) {
2161 revs
->diffopt
.flags
.recursive
= 1;
2162 revs
->diffopt
.flags
.tree_in_recursive
= 1;
2163 } else if (!strcmp(arg
, "-m")) {
2164 revs
->ignore_merges
= 0;
2165 } else if (!strcmp(arg
, "-c")) {
2167 revs
->dense_combined_merges
= 0;
2168 revs
->combine_merges
= 1;
2169 } else if (!strcmp(arg
, "--combined-all-paths")) {
2171 revs
->combined_all_paths
= 1;
2172 } else if (!strcmp(arg
, "--cc")) {
2174 revs
->dense_combined_merges
= 1;
2175 revs
->combine_merges
= 1;
2176 } else if (!strcmp(arg
, "-v")) {
2177 revs
->verbose_header
= 1;
2178 } else if (!strcmp(arg
, "--pretty")) {
2179 revs
->verbose_header
= 1;
2180 revs
->pretty_given
= 1;
2181 get_commit_format(NULL
, revs
);
2182 } else if (skip_prefix(arg
, "--pretty=", &optarg
) ||
2183 skip_prefix(arg
, "--format=", &optarg
)) {
2185 * Detached form ("--pretty X" as opposed to "--pretty=X")
2186 * not allowed, since the argument is optional.
2188 revs
->verbose_header
= 1;
2189 revs
->pretty_given
= 1;
2190 get_commit_format(optarg
, revs
);
2191 } else if (!strcmp(arg
, "--expand-tabs")) {
2192 revs
->expand_tabs_in_log
= 8;
2193 } else if (!strcmp(arg
, "--no-expand-tabs")) {
2194 revs
->expand_tabs_in_log
= 0;
2195 } else if (skip_prefix(arg
, "--expand-tabs=", &arg
)) {
2197 if (strtol_i(arg
, 10, &val
) < 0 || val
< 0)
2198 die("'%s': not a non-negative integer", arg
);
2199 revs
->expand_tabs_in_log
= val
;
2200 } else if (!strcmp(arg
, "--show-notes") || !strcmp(arg
, "--notes")) {
2201 revs
->show_notes
= 1;
2202 revs
->show_notes_given
= 1;
2203 revs
->notes_opt
.use_default_notes
= 1;
2204 } else if (!strcmp(arg
, "--show-signature")) {
2205 revs
->show_signature
= 1;
2206 } else if (!strcmp(arg
, "--no-show-signature")) {
2207 revs
->show_signature
= 0;
2208 } else if (!strcmp(arg
, "--show-linear-break")) {
2209 revs
->break_bar
= " ..........";
2210 revs
->track_linear
= 1;
2211 revs
->track_first_time
= 1;
2212 } else if (skip_prefix(arg
, "--show-linear-break=", &optarg
)) {
2213 revs
->break_bar
= xstrdup(optarg
);
2214 revs
->track_linear
= 1;
2215 revs
->track_first_time
= 1;
2216 } else if (skip_prefix(arg
, "--show-notes=", &optarg
) ||
2217 skip_prefix(arg
, "--notes=", &optarg
)) {
2218 struct strbuf buf
= STRBUF_INIT
;
2219 revs
->show_notes
= 1;
2220 revs
->show_notes_given
= 1;
2221 if (starts_with(arg
, "--show-notes=") &&
2222 revs
->notes_opt
.use_default_notes
< 0)
2223 revs
->notes_opt
.use_default_notes
= 1;
2224 strbuf_addstr(&buf
, optarg
);
2225 expand_notes_ref(&buf
);
2226 string_list_append(&revs
->notes_opt
.extra_notes_refs
,
2227 strbuf_detach(&buf
, NULL
));
2228 } else if (!strcmp(arg
, "--no-notes")) {
2229 revs
->show_notes
= 0;
2230 revs
->show_notes_given
= 1;
2231 revs
->notes_opt
.use_default_notes
= -1;
2232 /* we have been strdup'ing ourselves, so trick
2233 * string_list into free()ing strings */
2234 revs
->notes_opt
.extra_notes_refs
.strdup_strings
= 1;
2235 string_list_clear(&revs
->notes_opt
.extra_notes_refs
, 0);
2236 revs
->notes_opt
.extra_notes_refs
.strdup_strings
= 0;
2237 } else if (!strcmp(arg
, "--standard-notes")) {
2238 revs
->show_notes_given
= 1;
2239 revs
->notes_opt
.use_default_notes
= 1;
2240 } else if (!strcmp(arg
, "--no-standard-notes")) {
2241 revs
->notes_opt
.use_default_notes
= 0;
2242 } else if (!strcmp(arg
, "--oneline")) {
2243 revs
->verbose_header
= 1;
2244 get_commit_format("oneline", revs
);
2245 revs
->pretty_given
= 1;
2246 revs
->abbrev_commit
= 1;
2247 } else if (!strcmp(arg
, "--graph")) {
2248 revs
->topo_order
= 1;
2249 revs
->rewrite_parents
= 1;
2250 revs
->graph
= graph_init(revs
);
2251 } else if (!strcmp(arg
, "--root")) {
2252 revs
->show_root_diff
= 1;
2253 } else if (!strcmp(arg
, "--no-commit-id")) {
2254 revs
->no_commit_id
= 1;
2255 } else if (!strcmp(arg
, "--always")) {
2256 revs
->always_show_header
= 1;
2257 } else if (!strcmp(arg
, "--no-abbrev")) {
2259 } else if (!strcmp(arg
, "--abbrev")) {
2260 revs
->abbrev
= DEFAULT_ABBREV
;
2261 } else if (skip_prefix(arg
, "--abbrev=", &optarg
)) {
2262 revs
->abbrev
= strtoul(optarg
, NULL
, 10);
2263 if (revs
->abbrev
< MINIMUM_ABBREV
)
2264 revs
->abbrev
= MINIMUM_ABBREV
;
2265 else if (revs
->abbrev
> hexsz
)
2266 revs
->abbrev
= hexsz
;
2267 } else if (!strcmp(arg
, "--abbrev-commit")) {
2268 revs
->abbrev_commit
= 1;
2269 revs
->abbrev_commit_given
= 1;
2270 } else if (!strcmp(arg
, "--no-abbrev-commit")) {
2271 revs
->abbrev_commit
= 0;
2272 } else if (!strcmp(arg
, "--full-diff")) {
2274 revs
->full_diff
= 1;
2275 } else if (!strcmp(arg
, "--full-history")) {
2276 revs
->simplify_history
= 0;
2277 } else if (!strcmp(arg
, "--relative-date")) {
2278 revs
->date_mode
.type
= DATE_RELATIVE
;
2279 revs
->date_mode_explicit
= 1;
2280 } else if ((argcount
= parse_long_opt("date", argv
, &optarg
))) {
2281 parse_date_format(optarg
, &revs
->date_mode
);
2282 revs
->date_mode_explicit
= 1;
2284 } else if (!strcmp(arg
, "--log-size")) {
2285 revs
->show_log_size
= 1;
2288 * Grepping the commit log
2290 else if ((argcount
= parse_long_opt("author", argv
, &optarg
))) {
2291 add_header_grep(revs
, GREP_HEADER_AUTHOR
, optarg
);
2293 } else if ((argcount
= parse_long_opt("committer", argv
, &optarg
))) {
2294 add_header_grep(revs
, GREP_HEADER_COMMITTER
, optarg
);
2296 } else if ((argcount
= parse_long_opt("grep-reflog", argv
, &optarg
))) {
2297 add_header_grep(revs
, GREP_HEADER_REFLOG
, optarg
);
2299 } else if ((argcount
= parse_long_opt("grep", argv
, &optarg
))) {
2300 add_message_grep(revs
, optarg
);
2302 } else if (!strcmp(arg
, "--grep-debug")) {
2303 revs
->grep_filter
.debug
= 1;
2304 } else if (!strcmp(arg
, "--basic-regexp")) {
2305 revs
->grep_filter
.pattern_type_option
= GREP_PATTERN_TYPE_BRE
;
2306 } else if (!strcmp(arg
, "--extended-regexp") || !strcmp(arg
, "-E")) {
2307 revs
->grep_filter
.pattern_type_option
= GREP_PATTERN_TYPE_ERE
;
2308 } else if (!strcmp(arg
, "--regexp-ignore-case") || !strcmp(arg
, "-i")) {
2309 revs
->grep_filter
.ignore_case
= 1;
2310 revs
->diffopt
.pickaxe_opts
|= DIFF_PICKAXE_IGNORE_CASE
;
2311 } else if (!strcmp(arg
, "--fixed-strings") || !strcmp(arg
, "-F")) {
2312 revs
->grep_filter
.pattern_type_option
= GREP_PATTERN_TYPE_FIXED
;
2313 } else if (!strcmp(arg
, "--perl-regexp") || !strcmp(arg
, "-P")) {
2314 revs
->grep_filter
.pattern_type_option
= GREP_PATTERN_TYPE_PCRE
;
2315 } else if (!strcmp(arg
, "--all-match")) {
2316 revs
->grep_filter
.all_match
= 1;
2317 } else if (!strcmp(arg
, "--invert-grep")) {
2318 revs
->invert_grep
= 1;
2319 } else if ((argcount
= parse_long_opt("encoding", argv
, &optarg
))) {
2320 if (strcmp(optarg
, "none"))
2321 git_log_output_encoding
= xstrdup(optarg
);
2323 git_log_output_encoding
= "";
2325 } else if (!strcmp(arg
, "--reverse")) {
2327 } else if (!strcmp(arg
, "--children")) {
2328 revs
->children
.name
= "children";
2330 } else if (!strcmp(arg
, "--ignore-missing")) {
2331 revs
->ignore_missing
= 1;
2332 } else if (opt
&& opt
->allow_exclude_promisor_objects
&&
2333 !strcmp(arg
, "--exclude-promisor-objects")) {
2334 if (fetch_if_missing
)
2335 BUG("exclude_promisor_objects can only be used when fetch_if_missing is 0");
2336 revs
->exclude_promisor_objects
= 1;
2338 int opts
= diff_opt_parse(&revs
->diffopt
, argv
, argc
, revs
->prefix
);
2340 unkv
[(*unkc
)++] = arg
;
2343 if (revs
->graph
&& revs
->track_linear
)
2344 die("--show-linear-break and --graph are incompatible");
2349 void parse_revision_opt(struct rev_info
*revs
, struct parse_opt_ctx_t
*ctx
,
2350 const struct option
*options
,
2351 const char * const usagestr
[])
2353 int n
= handle_revision_opt(revs
, ctx
->argc
, ctx
->argv
,
2354 &ctx
->cpidx
, ctx
->out
, NULL
);
2356 error("unknown option `%s'", ctx
->argv
[0]);
2357 usage_with_options(usagestr
, options
);
2363 static int for_each_bisect_ref(struct ref_store
*refs
, each_ref_fn fn
,
2364 void *cb_data
, const char *term
)
2366 struct strbuf bisect_refs
= STRBUF_INIT
;
2368 strbuf_addf(&bisect_refs
, "refs/bisect/%s", term
);
2369 status
= refs_for_each_fullref_in(refs
, bisect_refs
.buf
, fn
, cb_data
, 0);
2370 strbuf_release(&bisect_refs
);
2374 static int for_each_bad_bisect_ref(struct ref_store
*refs
, each_ref_fn fn
, void *cb_data
)
2376 return for_each_bisect_ref(refs
, fn
, cb_data
, term_bad
);
2379 static int for_each_good_bisect_ref(struct ref_store
*refs
, each_ref_fn fn
, void *cb_data
)
2381 return for_each_bisect_ref(refs
, fn
, cb_data
, term_good
);
2384 static int handle_revision_pseudo_opt(const char *submodule
,
2385 struct rev_info
*revs
,
2386 int argc
, const char **argv
, int *flags
)
2388 const char *arg
= argv
[0];
2390 struct ref_store
*refs
;
2395 * We need some something like get_submodule_worktrees()
2396 * before we can go through all worktrees of a submodule,
2397 * .e.g with adding all HEADs from --all, which is not
2398 * supported right now, so stick to single worktree.
2400 if (!revs
->single_worktree
)
2401 BUG("--single-worktree cannot be used together with submodule");
2402 refs
= get_submodule_ref_store(submodule
);
2404 refs
= get_main_ref_store(revs
->repo
);
2409 * Commands like "git shortlog" will not accept the options below
2410 * unless parse_revision_opt queues them (as opposed to erroring
2413 * When implementing your new pseudo-option, remember to
2414 * register it in the list at the top of handle_revision_opt.
2416 if (!strcmp(arg
, "--all")) {
2417 handle_refs(refs
, revs
, *flags
, refs_for_each_ref
);
2418 handle_refs(refs
, revs
, *flags
, refs_head_ref
);
2419 if (!revs
->single_worktree
) {
2420 struct all_refs_cb cb
;
2422 init_all_refs_cb(&cb
, revs
, *flags
);
2423 other_head_refs(handle_one_ref
, &cb
);
2425 clear_ref_exclusion(&revs
->ref_excludes
);
2426 } else if (!strcmp(arg
, "--branches")) {
2427 handle_refs(refs
, revs
, *flags
, refs_for_each_branch_ref
);
2428 clear_ref_exclusion(&revs
->ref_excludes
);
2429 } else if (!strcmp(arg
, "--bisect")) {
2430 read_bisect_terms(&term_bad
, &term_good
);
2431 handle_refs(refs
, revs
, *flags
, for_each_bad_bisect_ref
);
2432 handle_refs(refs
, revs
, *flags
^ (UNINTERESTING
| BOTTOM
),
2433 for_each_good_bisect_ref
);
2435 } else if (!strcmp(arg
, "--tags")) {
2436 handle_refs(refs
, revs
, *flags
, refs_for_each_tag_ref
);
2437 clear_ref_exclusion(&revs
->ref_excludes
);
2438 } else if (!strcmp(arg
, "--remotes")) {
2439 handle_refs(refs
, revs
, *flags
, refs_for_each_remote_ref
);
2440 clear_ref_exclusion(&revs
->ref_excludes
);
2441 } else if ((argcount
= parse_long_opt("glob", argv
, &optarg
))) {
2442 struct all_refs_cb cb
;
2443 init_all_refs_cb(&cb
, revs
, *flags
);
2444 for_each_glob_ref(handle_one_ref
, optarg
, &cb
);
2445 clear_ref_exclusion(&revs
->ref_excludes
);
2447 } else if ((argcount
= parse_long_opt("exclude", argv
, &optarg
))) {
2448 add_ref_exclusion(&revs
->ref_excludes
, optarg
);
2450 } else if (skip_prefix(arg
, "--branches=", &optarg
)) {
2451 struct all_refs_cb cb
;
2452 init_all_refs_cb(&cb
, revs
, *flags
);
2453 for_each_glob_ref_in(handle_one_ref
, optarg
, "refs/heads/", &cb
);
2454 clear_ref_exclusion(&revs
->ref_excludes
);
2455 } else if (skip_prefix(arg
, "--tags=", &optarg
)) {
2456 struct all_refs_cb cb
;
2457 init_all_refs_cb(&cb
, revs
, *flags
);
2458 for_each_glob_ref_in(handle_one_ref
, optarg
, "refs/tags/", &cb
);
2459 clear_ref_exclusion(&revs
->ref_excludes
);
2460 } else if (skip_prefix(arg
, "--remotes=", &optarg
)) {
2461 struct all_refs_cb cb
;
2462 init_all_refs_cb(&cb
, revs
, *flags
);
2463 for_each_glob_ref_in(handle_one_ref
, optarg
, "refs/remotes/", &cb
);
2464 clear_ref_exclusion(&revs
->ref_excludes
);
2465 } else if (!strcmp(arg
, "--reflog")) {
2466 add_reflogs_to_pending(revs
, *flags
);
2467 } else if (!strcmp(arg
, "--indexed-objects")) {
2468 add_index_objects_to_pending(revs
, *flags
);
2469 } else if (!strcmp(arg
, "--alternate-refs")) {
2470 add_alternate_refs_to_pending(revs
, *flags
);
2471 } else if (!strcmp(arg
, "--not")) {
2472 *flags
^= UNINTERESTING
| BOTTOM
;
2473 } else if (!strcmp(arg
, "--no-walk")) {
2474 revs
->no_walk
= REVISION_WALK_NO_WALK_SORTED
;
2475 } else if (skip_prefix(arg
, "--no-walk=", &optarg
)) {
2477 * Detached form ("--no-walk X" as opposed to "--no-walk=X")
2478 * not allowed, since the argument is optional.
2480 if (!strcmp(optarg
, "sorted"))
2481 revs
->no_walk
= REVISION_WALK_NO_WALK_SORTED
;
2482 else if (!strcmp(optarg
, "unsorted"))
2483 revs
->no_walk
= REVISION_WALK_NO_WALK_UNSORTED
;
2485 return error("invalid argument to --no-walk");
2486 } else if (!strcmp(arg
, "--do-walk")) {
2488 } else if (!strcmp(arg
, "--single-worktree")) {
2489 revs
->single_worktree
= 1;
2497 static void NORETURN
diagnose_missing_default(const char *def
)
2500 const char *refname
;
2502 refname
= resolve_ref_unsafe(def
, 0, NULL
, &flags
);
2503 if (!refname
|| !(flags
& REF_ISSYMREF
) || (flags
& REF_ISBROKEN
))
2504 die(_("your current branch appears to be broken"));
2506 skip_prefix(refname
, "refs/heads/", &refname
);
2507 die(_("your current branch '%s' does not have any commits yet"),
2512 * Parse revision information, filling in the "rev_info" structure,
2513 * and removing the used arguments from the argument list.
2515 * Returns the number of arguments left that weren't recognized
2516 * (which are also moved to the head of the argument list)
2518 int setup_revisions(int argc
, const char **argv
, struct rev_info
*revs
, struct setup_revision_opt
*opt
)
2520 int i
, flags
, left
, seen_dashdash
, got_rev_arg
= 0, revarg_opt
;
2521 struct argv_array prune_data
= ARGV_ARRAY_INIT
;
2522 const char *submodule
= NULL
;
2523 int seen_end_of_options
= 0;
2526 submodule
= opt
->submodule
;
2528 /* First, search for "--" */
2529 if (opt
&& opt
->assume_dashdash
) {
2533 for (i
= 1; i
< argc
; i
++) {
2534 const char *arg
= argv
[i
];
2535 if (strcmp(arg
, "--"))
2540 argv_array_pushv(&prune_data
, argv
+ i
+ 1);
2546 /* Second, deal with arguments and options */
2548 revarg_opt
= opt
? opt
->revarg_opt
: 0;
2550 revarg_opt
|= REVARG_CANNOT_BE_FILENAME
;
2551 for (left
= i
= 1; i
< argc
; i
++) {
2552 const char *arg
= argv
[i
];
2553 if (!seen_end_of_options
&& *arg
== '-') {
2556 opts
= handle_revision_pseudo_opt(submodule
,
2557 revs
, argc
- i
, argv
+ i
,
2564 if (!strcmp(arg
, "--stdin")) {
2565 if (revs
->disable_stdin
) {
2569 if (revs
->read_from_stdin
++)
2570 die("--stdin given twice?");
2571 read_revisions_from_stdin(revs
, &prune_data
);
2575 if (!strcmp(arg
, "--end-of-options")) {
2576 seen_end_of_options
= 1;
2580 opts
= handle_revision_opt(revs
, argc
- i
, argv
+ i
,
2592 if (handle_revision_arg(arg
, revs
, flags
, revarg_opt
)) {
2594 if (seen_dashdash
|| *arg
== '^')
2595 die("bad revision '%s'", arg
);
2597 /* If we didn't have a "--":
2598 * (1) all filenames must exist;
2599 * (2) all rev-args must not be interpretable
2600 * as a valid filename.
2601 * but the latter we have checked in the main loop.
2603 for (j
= i
; j
< argc
; j
++)
2604 verify_filename(revs
->prefix
, argv
[j
], j
== i
);
2606 argv_array_pushv(&prune_data
, argv
+ i
);
2613 if (prune_data
.argc
) {
2615 * If we need to introduce the magic "a lone ':' means no
2616 * pathspec whatsoever", here is the place to do so.
2618 * if (prune_data.nr == 1 && !strcmp(prune_data[0], ":")) {
2619 * prune_data.nr = 0;
2620 * prune_data.alloc = 0;
2621 * free(prune_data.path);
2622 * prune_data.path = NULL;
2624 * terminate prune_data.alloc with NULL and
2625 * call init_pathspec() to set revs->prune_data here.
2628 parse_pathspec(&revs
->prune_data
, 0, 0,
2629 revs
->prefix
, prune_data
.argv
);
2631 argv_array_clear(&prune_data
);
2633 if (revs
->def
== NULL
)
2634 revs
->def
= opt
? opt
->def
: NULL
;
2635 if (opt
&& opt
->tweak
)
2636 opt
->tweak(revs
, opt
);
2637 if (revs
->show_merge
)
2638 prepare_show_merge(revs
);
2639 if (revs
->def
&& !revs
->pending
.nr
&& !revs
->rev_input_given
&& !got_rev_arg
) {
2640 struct object_id oid
;
2641 struct object
*object
;
2642 struct object_context oc
;
2643 if (get_oid_with_context(revs
->repo
, revs
->def
, 0, &oid
, &oc
))
2644 diagnose_missing_default(revs
->def
);
2645 object
= get_reference(revs
, revs
->def
, &oid
, 0);
2646 add_pending_object_with_mode(revs
, object
, revs
->def
, oc
.mode
);
2649 /* Did the user ask for any diff output? Run the diff! */
2650 if (revs
->diffopt
.output_format
& ~DIFF_FORMAT_NO_OUTPUT
)
2653 /* Pickaxe, diff-filter and rename following need diffs */
2654 if ((revs
->diffopt
.pickaxe_opts
& DIFF_PICKAXE_KINDS_MASK
) ||
2655 revs
->diffopt
.filter
||
2656 revs
->diffopt
.flags
.follow_renames
)
2659 if (revs
->diffopt
.objfind
)
2660 revs
->simplify_history
= 0;
2662 if (revs
->topo_order
&& !generation_numbers_enabled(the_repository
))
2665 if (revs
->prune_data
.nr
) {
2666 copy_pathspec(&revs
->pruning
.pathspec
, &revs
->prune_data
);
2667 /* Can't prune commits with rename following: the paths change.. */
2668 if (!revs
->diffopt
.flags
.follow_renames
)
2670 if (!revs
->full_diff
)
2671 copy_pathspec(&revs
->diffopt
.pathspec
,
2674 if (revs
->combine_merges
)
2675 revs
->ignore_merges
= 0;
2676 if (revs
->combined_all_paths
&& !revs
->combine_merges
)
2677 die("--combined-all-paths makes no sense without -c or --cc");
2679 revs
->diffopt
.abbrev
= revs
->abbrev
;
2681 if (revs
->line_level_traverse
) {
2683 revs
->topo_order
= 1;
2686 diff_setup_done(&revs
->diffopt
);
2688 grep_commit_pattern_type(GREP_PATTERN_TYPE_UNSPECIFIED
,
2689 &revs
->grep_filter
);
2690 compile_grep_patterns(&revs
->grep_filter
);
2692 if (revs
->reverse
&& revs
->reflog_info
)
2693 die("cannot combine --reverse with --walk-reflogs");
2694 if (revs
->reflog_info
&& revs
->limited
)
2695 die("cannot combine --walk-reflogs with history-limiting options");
2696 if (revs
->rewrite_parents
&& revs
->children
.name
)
2697 die("cannot combine --parents and --children");
2700 * Limitations on the graph functionality
2702 if (revs
->reverse
&& revs
->graph
)
2703 die("cannot combine --reverse with --graph");
2705 if (revs
->reflog_info
&& revs
->graph
)
2706 die("cannot combine --walk-reflogs with --graph");
2707 if (revs
->no_walk
&& revs
->graph
)
2708 die("cannot combine --no-walk with --graph");
2709 if (!revs
->reflog_info
&& revs
->grep_filter
.use_reflog_filter
)
2710 die("cannot use --grep-reflog without --walk-reflogs");
2712 if (revs
->first_parent_only
&& revs
->bisect
)
2713 die(_("--first-parent is incompatible with --bisect"));
2715 if (revs
->line_level_traverse
&&
2716 (revs
->diffopt
.output_format
& ~(DIFF_FORMAT_PATCH
| DIFF_FORMAT_NO_OUTPUT
)))
2717 die(_("-L does not yet support diff formats besides -p and -s"));
2719 if (revs
->expand_tabs_in_log
< 0)
2720 revs
->expand_tabs_in_log
= revs
->expand_tabs_in_log_default
;
2725 static void add_child(struct rev_info
*revs
, struct commit
*parent
, struct commit
*child
)
2727 struct commit_list
*l
= xcalloc(1, sizeof(*l
));
2730 l
->next
= add_decoration(&revs
->children
, &parent
->object
, l
);
2733 static int remove_duplicate_parents(struct rev_info
*revs
, struct commit
*commit
)
2735 struct treesame_state
*ts
= lookup_decoration(&revs
->treesame
, &commit
->object
);
2736 struct commit_list
**pp
, *p
;
2737 int surviving_parents
;
2739 /* Examine existing parents while marking ones we have seen... */
2740 pp
= &commit
->parents
;
2741 surviving_parents
= 0;
2742 while ((p
= *pp
) != NULL
) {
2743 struct commit
*parent
= p
->item
;
2744 if (parent
->object
.flags
& TMP_MARK
) {
2747 compact_treesame(revs
, commit
, surviving_parents
);
2750 parent
->object
.flags
|= TMP_MARK
;
2751 surviving_parents
++;
2754 /* clear the temporary mark */
2755 for (p
= commit
->parents
; p
; p
= p
->next
) {
2756 p
->item
->object
.flags
&= ~TMP_MARK
;
2758 /* no update_treesame() - removing duplicates can't affect TREESAME */
2759 return surviving_parents
;
2762 struct merge_simplify_state
{
2763 struct commit
*simplified
;
2766 static struct merge_simplify_state
*locate_simplify_state(struct rev_info
*revs
, struct commit
*commit
)
2768 struct merge_simplify_state
*st
;
2770 st
= lookup_decoration(&revs
->merge_simplification
, &commit
->object
);
2772 st
= xcalloc(1, sizeof(*st
));
2773 add_decoration(&revs
->merge_simplification
, &commit
->object
, st
);
2778 static int mark_redundant_parents(struct commit
*commit
)
2780 struct commit_list
*h
= reduce_heads(commit
->parents
);
2781 int i
= 0, marked
= 0;
2782 struct commit_list
*po
, *pn
;
2784 /* Want these for sanity-checking only */
2785 int orig_cnt
= commit_list_count(commit
->parents
);
2786 int cnt
= commit_list_count(h
);
2789 * Not ready to remove items yet, just mark them for now, based
2790 * on the output of reduce_heads(). reduce_heads outputs the reduced
2791 * set in its original order, so this isn't too hard.
2793 po
= commit
->parents
;
2796 if (pn
&& po
->item
== pn
->item
) {
2800 po
->item
->object
.flags
|= TMP_MARK
;
2806 if (i
!= cnt
|| cnt
+marked
!= orig_cnt
)
2807 die("mark_redundant_parents %d %d %d %d", orig_cnt
, cnt
, i
, marked
);
2809 free_commit_list(h
);
2814 static int mark_treesame_root_parents(struct commit
*commit
)
2816 struct commit_list
*p
;
2819 for (p
= commit
->parents
; p
; p
= p
->next
) {
2820 struct commit
*parent
= p
->item
;
2821 if (!parent
->parents
&& (parent
->object
.flags
& TREESAME
)) {
2822 parent
->object
.flags
|= TMP_MARK
;
2831 * Awkward naming - this means one parent we are TREESAME to.
2832 * cf mark_treesame_root_parents: root parents that are TREESAME (to an
2833 * empty tree). Better name suggestions?
2835 static int leave_one_treesame_to_parent(struct rev_info
*revs
, struct commit
*commit
)
2837 struct treesame_state
*ts
= lookup_decoration(&revs
->treesame
, &commit
->object
);
2838 struct commit
*unmarked
= NULL
, *marked
= NULL
;
2839 struct commit_list
*p
;
2842 for (p
= commit
->parents
, n
= 0; p
; p
= p
->next
, n
++) {
2843 if (ts
->treesame
[n
]) {
2844 if (p
->item
->object
.flags
& TMP_MARK
) {
2857 * If we are TREESAME to a marked-for-deletion parent, but not to any
2858 * unmarked parents, unmark the first TREESAME parent. This is the
2859 * parent that the default simplify_history==1 scan would have followed,
2860 * and it doesn't make sense to omit that path when asking for a
2861 * simplified full history. Retaining it improves the chances of
2862 * understanding odd missed merges that took an old version of a file.
2866 * I--------*X A modified the file, but mainline merge X used
2867 * \ / "-s ours", so took the version from I. X is
2868 * `-*A--' TREESAME to I and !TREESAME to A.
2870 * Default log from X would produce "I". Without this check,
2871 * --full-history --simplify-merges would produce "I-A-X", showing
2872 * the merge commit X and that it changed A, but not making clear that
2873 * it had just taken the I version. With this check, the topology above
2876 * Note that it is possible that the simplification chooses a different
2877 * TREESAME parent from the default, in which case this test doesn't
2878 * activate, and we _do_ drop the default parent. Example:
2880 * I------X A modified the file, but it was reverted in B,
2881 * \ / meaning mainline merge X is TREESAME to both
2884 * Default log would produce "I" by following the first parent;
2885 * --full-history --simplify-merges will produce "I-A-B". But this is a
2886 * reasonable result - it presents a logical full history leading from
2887 * I to X, and X is not an important merge.
2889 if (!unmarked
&& marked
) {
2890 marked
->object
.flags
&= ~TMP_MARK
;
2897 static int remove_marked_parents(struct rev_info
*revs
, struct commit
*commit
)
2899 struct commit_list
**pp
, *p
;
2900 int nth_parent
, removed
= 0;
2902 pp
= &commit
->parents
;
2904 while ((p
= *pp
) != NULL
) {
2905 struct commit
*parent
= p
->item
;
2906 if (parent
->object
.flags
& TMP_MARK
) {
2907 parent
->object
.flags
&= ~TMP_MARK
;
2911 compact_treesame(revs
, commit
, nth_parent
);
2918 /* Removing parents can only increase TREESAMEness */
2919 if (removed
&& !(commit
->object
.flags
& TREESAME
))
2920 update_treesame(revs
, commit
);
2925 static struct commit_list
**simplify_one(struct rev_info
*revs
, struct commit
*commit
, struct commit_list
**tail
)
2927 struct commit_list
*p
;
2928 struct commit
*parent
;
2929 struct merge_simplify_state
*st
, *pst
;
2932 st
= locate_simplify_state(revs
, commit
);
2935 * Have we handled this one?
2941 * An UNINTERESTING commit simplifies to itself, so does a
2942 * root commit. We do not rewrite parents of such commit
2945 if ((commit
->object
.flags
& UNINTERESTING
) || !commit
->parents
) {
2946 st
->simplified
= commit
;
2951 * Do we know what commit all of our parents that matter
2952 * should be rewritten to? Otherwise we are not ready to
2953 * rewrite this one yet.
2955 for (cnt
= 0, p
= commit
->parents
; p
; p
= p
->next
) {
2956 pst
= locate_simplify_state(revs
, p
->item
);
2957 if (!pst
->simplified
) {
2958 tail
= &commit_list_insert(p
->item
, tail
)->next
;
2961 if (revs
->first_parent_only
)
2965 tail
= &commit_list_insert(commit
, tail
)->next
;
2970 * Rewrite our list of parents. Note that this cannot
2971 * affect our TREESAME flags in any way - a commit is
2972 * always TREESAME to its simplification.
2974 for (p
= commit
->parents
; p
; p
= p
->next
) {
2975 pst
= locate_simplify_state(revs
, p
->item
);
2976 p
->item
= pst
->simplified
;
2977 if (revs
->first_parent_only
)
2981 if (revs
->first_parent_only
)
2984 cnt
= remove_duplicate_parents(revs
, commit
);
2987 * It is possible that we are a merge and one side branch
2988 * does not have any commit that touches the given paths;
2989 * in such a case, the immediate parent from that branch
2990 * will be rewritten to be the merge base.
2992 * o----X X: the commit we are looking at;
2993 * / / o: a commit that touches the paths;
2996 * Further, a merge of an independent branch that doesn't
2997 * touch the path will reduce to a treesame root parent:
2999 * ----o----X X: the commit we are looking at;
3000 * / o: a commit that touches the paths;
3001 * r r: a root commit not touching the paths
3003 * Detect and simplify both cases.
3006 int marked
= mark_redundant_parents(commit
);
3007 marked
+= mark_treesame_root_parents(commit
);
3009 marked
-= leave_one_treesame_to_parent(revs
, commit
);
3011 cnt
= remove_marked_parents(revs
, commit
);
3015 * A commit simplifies to itself if it is a root, if it is
3016 * UNINTERESTING, if it touches the given paths, or if it is a
3017 * merge and its parents don't simplify to one relevant commit
3018 * (the first two cases are already handled at the beginning of
3021 * Otherwise, it simplifies to what its sole relevant parent
3025 (commit
->object
.flags
& UNINTERESTING
) ||
3026 !(commit
->object
.flags
& TREESAME
) ||
3027 (parent
= one_relevant_parent(revs
, commit
->parents
)) == NULL
)
3028 st
->simplified
= commit
;
3030 pst
= locate_simplify_state(revs
, parent
);
3031 st
->simplified
= pst
->simplified
;
3036 static void simplify_merges(struct rev_info
*revs
)
3038 struct commit_list
*list
, *next
;
3039 struct commit_list
*yet_to_do
, **tail
;
3040 struct commit
*commit
;
3045 /* feed the list reversed */
3047 for (list
= revs
->commits
; list
; list
= next
) {
3048 commit
= list
->item
;
3051 * Do not free(list) here yet; the original list
3052 * is used later in this function.
3054 commit_list_insert(commit
, &yet_to_do
);
3061 commit
= pop_commit(&list
);
3062 tail
= simplify_one(revs
, commit
, tail
);
3066 /* clean up the result, removing the simplified ones */
3067 list
= revs
->commits
;
3068 revs
->commits
= NULL
;
3069 tail
= &revs
->commits
;
3071 struct merge_simplify_state
*st
;
3073 commit
= pop_commit(&list
);
3074 st
= locate_simplify_state(revs
, commit
);
3075 if (st
->simplified
== commit
)
3076 tail
= &commit_list_insert(commit
, tail
)->next
;
3080 static void set_children(struct rev_info
*revs
)
3082 struct commit_list
*l
;
3083 for (l
= revs
->commits
; l
; l
= l
->next
) {
3084 struct commit
*commit
= l
->item
;
3085 struct commit_list
*p
;
3087 for (p
= commit
->parents
; p
; p
= p
->next
)
3088 add_child(revs
, p
->item
, commit
);
3092 void reset_revision_walk(void)
3094 clear_object_flags(SEEN
| ADDED
| SHOWN
);
3097 static int mark_uninteresting(const struct object_id
*oid
,
3098 struct packed_git
*pack
,
3102 struct rev_info
*revs
= cb
;
3103 struct object
*o
= parse_object(revs
->repo
, oid
);
3104 o
->flags
|= UNINTERESTING
| SEEN
;
3108 define_commit_slab(indegree_slab
, int);
3109 define_commit_slab(author_date_slab
, timestamp_t
);
3111 struct topo_walk_info
{
3112 uint32_t min_generation
;
3113 struct prio_queue explore_queue
;
3114 struct prio_queue indegree_queue
;
3115 struct prio_queue topo_queue
;
3116 struct indegree_slab indegree
;
3117 struct author_date_slab author_date
;
3120 static inline void test_flag_and_insert(struct prio_queue
*q
, struct commit
*c
, int flag
)
3122 if (c
->object
.flags
& flag
)
3125 c
->object
.flags
|= flag
;
3126 prio_queue_put(q
, c
);
3129 static void explore_walk_step(struct rev_info
*revs
)
3131 struct topo_walk_info
*info
= revs
->topo_walk_info
;
3132 struct commit_list
*p
;
3133 struct commit
*c
= prio_queue_get(&info
->explore_queue
);
3138 if (parse_commit_gently(c
, 1) < 0)
3141 if (revs
->sort_order
== REV_SORT_BY_AUTHOR_DATE
)
3142 record_author_date(&info
->author_date
, c
);
3144 if (revs
->max_age
!= -1 && (c
->date
< revs
->max_age
))
3145 c
->object
.flags
|= UNINTERESTING
;
3147 if (process_parents(revs
, c
, NULL
, NULL
) < 0)
3150 if (c
->object
.flags
& UNINTERESTING
)
3151 mark_parents_uninteresting(c
);
3153 for (p
= c
->parents
; p
; p
= p
->next
)
3154 test_flag_and_insert(&info
->explore_queue
, p
->item
, TOPO_WALK_EXPLORED
);
3157 static void explore_to_depth(struct rev_info
*revs
,
3158 uint32_t gen_cutoff
)
3160 struct topo_walk_info
*info
= revs
->topo_walk_info
;
3162 while ((c
= prio_queue_peek(&info
->explore_queue
)) &&
3163 c
->generation
>= gen_cutoff
)
3164 explore_walk_step(revs
);
3167 static void indegree_walk_step(struct rev_info
*revs
)
3169 struct commit_list
*p
;
3170 struct topo_walk_info
*info
= revs
->topo_walk_info
;
3171 struct commit
*c
= prio_queue_get(&info
->indegree_queue
);
3176 if (parse_commit_gently(c
, 1) < 0)
3179 explore_to_depth(revs
, c
->generation
);
3181 for (p
= c
->parents
; p
; p
= p
->next
) {
3182 struct commit
*parent
= p
->item
;
3183 int *pi
= indegree_slab_at(&info
->indegree
, parent
);
3190 test_flag_and_insert(&info
->indegree_queue
, parent
, TOPO_WALK_INDEGREE
);
3192 if (revs
->first_parent_only
)
3197 static void compute_indegrees_to_depth(struct rev_info
*revs
,
3198 uint32_t gen_cutoff
)
3200 struct topo_walk_info
*info
= revs
->topo_walk_info
;
3202 while ((c
= prio_queue_peek(&info
->indegree_queue
)) &&
3203 c
->generation
>= gen_cutoff
)
3204 indegree_walk_step(revs
);
3207 static void init_topo_walk(struct rev_info
*revs
)
3209 struct topo_walk_info
*info
;
3210 struct commit_list
*list
;
3211 revs
->topo_walk_info
= xmalloc(sizeof(struct topo_walk_info
));
3212 info
= revs
->topo_walk_info
;
3213 memset(info
, 0, sizeof(struct topo_walk_info
));
3215 init_indegree_slab(&info
->indegree
);
3216 memset(&info
->explore_queue
, 0, sizeof(info
->explore_queue
));
3217 memset(&info
->indegree_queue
, 0, sizeof(info
->indegree_queue
));
3218 memset(&info
->topo_queue
, 0, sizeof(info
->topo_queue
));
3220 switch (revs
->sort_order
) {
3221 default: /* REV_SORT_IN_GRAPH_ORDER */
3222 info
->topo_queue
.compare
= NULL
;
3224 case REV_SORT_BY_COMMIT_DATE
:
3225 info
->topo_queue
.compare
= compare_commits_by_commit_date
;
3227 case REV_SORT_BY_AUTHOR_DATE
:
3228 init_author_date_slab(&info
->author_date
);
3229 info
->topo_queue
.compare
= compare_commits_by_author_date
;
3230 info
->topo_queue
.cb_data
= &info
->author_date
;
3234 info
->explore_queue
.compare
= compare_commits_by_gen_then_commit_date
;
3235 info
->indegree_queue
.compare
= compare_commits_by_gen_then_commit_date
;
3237 info
->min_generation
= GENERATION_NUMBER_INFINITY
;
3238 for (list
= revs
->commits
; list
; list
= list
->next
) {
3239 struct commit
*c
= list
->item
;
3241 if (parse_commit_gently(c
, 1))
3244 test_flag_and_insert(&info
->explore_queue
, c
, TOPO_WALK_EXPLORED
);
3245 test_flag_and_insert(&info
->indegree_queue
, c
, TOPO_WALK_INDEGREE
);
3247 if (c
->generation
< info
->min_generation
)
3248 info
->min_generation
= c
->generation
;
3250 *(indegree_slab_at(&info
->indegree
, c
)) = 1;
3252 if (revs
->sort_order
== REV_SORT_BY_AUTHOR_DATE
)
3253 record_author_date(&info
->author_date
, c
);
3255 compute_indegrees_to_depth(revs
, info
->min_generation
);
3257 for (list
= revs
->commits
; list
; list
= list
->next
) {
3258 struct commit
*c
= list
->item
;
3260 if (*(indegree_slab_at(&info
->indegree
, c
)) == 1)
3261 prio_queue_put(&info
->topo_queue
, c
);
3265 * This is unfortunate; the initial tips need to be shown
3266 * in the order given from the revision traversal machinery.
3268 if (revs
->sort_order
== REV_SORT_IN_GRAPH_ORDER
)
3269 prio_queue_reverse(&info
->topo_queue
);
3272 static struct commit
*next_topo_commit(struct rev_info
*revs
)
3275 struct topo_walk_info
*info
= revs
->topo_walk_info
;
3277 /* pop next off of topo_queue */
3278 c
= prio_queue_get(&info
->topo_queue
);
3281 *(indegree_slab_at(&info
->indegree
, c
)) = 0;
3286 static void expand_topo_walk(struct rev_info
*revs
, struct commit
*commit
)
3288 struct commit_list
*p
;
3289 struct topo_walk_info
*info
= revs
->topo_walk_info
;
3290 if (process_parents(revs
, commit
, NULL
, NULL
) < 0) {
3291 if (!revs
->ignore_missing_links
)
3292 die("Failed to traverse parents of commit %s",
3293 oid_to_hex(&commit
->object
.oid
));
3296 for (p
= commit
->parents
; p
; p
= p
->next
) {
3297 struct commit
*parent
= p
->item
;
3300 if (parent
->object
.flags
& UNINTERESTING
)
3303 if (parse_commit_gently(parent
, 1) < 0)
3306 if (parent
->generation
< info
->min_generation
) {
3307 info
->min_generation
= parent
->generation
;
3308 compute_indegrees_to_depth(revs
, info
->min_generation
);
3311 pi
= indegree_slab_at(&info
->indegree
, parent
);
3315 prio_queue_put(&info
->topo_queue
, parent
);
3317 if (revs
->first_parent_only
)
3322 int prepare_revision_walk(struct rev_info
*revs
)
3325 struct object_array old_pending
;
3326 struct commit_list
**next
= &revs
->commits
;
3328 memcpy(&old_pending
, &revs
->pending
, sizeof(old_pending
));
3329 revs
->pending
.nr
= 0;
3330 revs
->pending
.alloc
= 0;
3331 revs
->pending
.objects
= NULL
;
3332 for (i
= 0; i
< old_pending
.nr
; i
++) {
3333 struct object_array_entry
*e
= old_pending
.objects
+ i
;
3334 struct commit
*commit
= handle_commit(revs
, e
);
3336 if (!(commit
->object
.flags
& SEEN
)) {
3337 commit
->object
.flags
|= SEEN
;
3338 next
= commit_list_append(commit
, next
);
3342 object_array_clear(&old_pending
);
3344 /* Signal whether we need per-parent treesame decoration */
3345 if (revs
->simplify_merges
||
3346 (revs
->limited
&& limiting_can_increase_treesame(revs
)))
3347 revs
->treesame
.name
= "treesame";
3349 if (revs
->exclude_promisor_objects
) {
3350 for_each_packed_object(mark_uninteresting
, revs
,
3351 FOR_EACH_OBJECT_PROMISOR_ONLY
);
3354 if (revs
->no_walk
!= REVISION_WALK_NO_WALK_UNSORTED
)
3355 commit_list_sort_by_date(&revs
->commits
);
3358 if (revs
->limited
) {
3359 if (limit_list(revs
) < 0)
3361 if (revs
->topo_order
)
3362 sort_in_topological_order(&revs
->commits
, revs
->sort_order
);
3363 } else if (revs
->topo_order
)
3364 init_topo_walk(revs
);
3365 if (revs
->line_level_traverse
)
3366 line_log_filter(revs
);
3367 if (revs
->simplify_merges
)
3368 simplify_merges(revs
);
3369 if (revs
->children
.name
)
3374 static enum rewrite_result
rewrite_one_1(struct rev_info
*revs
,
3376 struct prio_queue
*queue
)
3379 struct commit
*p
= *pp
;
3381 if (process_parents(revs
, p
, NULL
, queue
) < 0)
3382 return rewrite_one_error
;
3383 if (p
->object
.flags
& UNINTERESTING
)
3384 return rewrite_one_ok
;
3385 if (!(p
->object
.flags
& TREESAME
))
3386 return rewrite_one_ok
;
3388 return rewrite_one_noparents
;
3389 if ((p
= one_relevant_parent(revs
, p
->parents
)) == NULL
)
3390 return rewrite_one_ok
;
3395 static void merge_queue_into_list(struct prio_queue
*q
, struct commit_list
**list
)
3398 struct commit
*item
= prio_queue_peek(q
);
3399 struct commit_list
*p
= *list
;
3401 if (p
&& p
->item
->date
>= item
->date
)
3404 p
= commit_list_insert(item
, list
);
3405 list
= &p
->next
; /* skip newly added item */
3406 prio_queue_get(q
); /* pop item */
3411 static enum rewrite_result
rewrite_one(struct rev_info
*revs
, struct commit
**pp
)
3413 struct prio_queue queue
= { compare_commits_by_commit_date
};
3414 enum rewrite_result ret
= rewrite_one_1(revs
, pp
, &queue
);
3415 merge_queue_into_list(&queue
, &revs
->commits
);
3416 clear_prio_queue(&queue
);
3420 int rewrite_parents(struct rev_info
*revs
, struct commit
*commit
,
3421 rewrite_parent_fn_t rewrite_parent
)
3423 struct commit_list
**pp
= &commit
->parents
;
3425 struct commit_list
*parent
= *pp
;
3426 switch (rewrite_parent(revs
, &parent
->item
)) {
3427 case rewrite_one_ok
:
3429 case rewrite_one_noparents
:
3432 case rewrite_one_error
:
3437 remove_duplicate_parents(revs
, commit
);
3441 static int commit_rewrite_person(struct strbuf
*buf
, const char *what
, struct string_list
*mailmap
)
3443 char *person
, *endp
;
3444 size_t len
, namelen
, maillen
;
3447 struct ident_split ident
;
3449 person
= strstr(buf
->buf
, what
);
3453 person
+= strlen(what
);
3454 endp
= strchr(person
, '\n');
3458 len
= endp
- person
;
3460 if (split_ident_line(&ident
, person
, len
))
3463 mail
= ident
.mail_begin
;
3464 maillen
= ident
.mail_end
- ident
.mail_begin
;
3465 name
= ident
.name_begin
;
3466 namelen
= ident
.name_end
- ident
.name_begin
;
3468 if (map_user(mailmap
, &mail
, &maillen
, &name
, &namelen
)) {
3469 struct strbuf namemail
= STRBUF_INIT
;
3471 strbuf_addf(&namemail
, "%.*s <%.*s>",
3472 (int)namelen
, name
, (int)maillen
, mail
);
3474 strbuf_splice(buf
, ident
.name_begin
- buf
->buf
,
3475 ident
.mail_end
- ident
.name_begin
+ 1,
3476 namemail
.buf
, namemail
.len
);
3478 strbuf_release(&namemail
);
3486 static int commit_match(struct commit
*commit
, struct rev_info
*opt
)
3489 const char *encoding
;
3490 const char *message
;
3491 struct strbuf buf
= STRBUF_INIT
;
3493 if (!opt
->grep_filter
.pattern_list
&& !opt
->grep_filter
.header_list
)
3496 /* Prepend "fake" headers as needed */
3497 if (opt
->grep_filter
.use_reflog_filter
) {
3498 strbuf_addstr(&buf
, "reflog ");
3499 get_reflog_message(&buf
, opt
->reflog_info
);
3500 strbuf_addch(&buf
, '\n');
3504 * We grep in the user's output encoding, under the assumption that it
3505 * is the encoding they are most likely to write their grep pattern
3506 * for. In addition, it means we will match the "notes" encoding below,
3507 * so we will not end up with a buffer that has two different encodings
3510 encoding
= get_log_output_encoding();
3511 message
= logmsg_reencode(commit
, NULL
, encoding
);
3513 /* Copy the commit to temporary if we are using "fake" headers */
3515 strbuf_addstr(&buf
, message
);
3517 if (opt
->grep_filter
.header_list
&& opt
->mailmap
) {
3519 strbuf_addstr(&buf
, message
);
3521 commit_rewrite_person(&buf
, "\nauthor ", opt
->mailmap
);
3522 commit_rewrite_person(&buf
, "\ncommitter ", opt
->mailmap
);
3525 /* Append "fake" message parts as needed */
3526 if (opt
->show_notes
) {
3528 strbuf_addstr(&buf
, message
);
3529 format_display_notes(&commit
->object
.oid
, &buf
, encoding
, 1);
3533 * Find either in the original commit message, or in the temporary.
3534 * Note that we cast away the constness of "message" here. It is
3535 * const because it may come from the cached commit buffer. That's OK,
3536 * because we know that it is modifiable heap memory, and that while
3537 * grep_buffer may modify it for speed, it will restore any
3538 * changes before returning.
3541 retval
= grep_buffer(&opt
->grep_filter
, buf
.buf
, buf
.len
);
3543 retval
= grep_buffer(&opt
->grep_filter
,
3544 (char *)message
, strlen(message
));
3545 strbuf_release(&buf
);
3546 unuse_commit_buffer(commit
, message
);
3547 return opt
->invert_grep
? !retval
: retval
;
3550 static inline int want_ancestry(const struct rev_info
*revs
)
3552 return (revs
->rewrite_parents
|| revs
->children
.name
);
3556 * Return a timestamp to be used for --since/--until comparisons for this
3557 * commit, based on the revision options.
3559 static timestamp_t
comparison_date(const struct rev_info
*revs
,
3560 struct commit
*commit
)
3562 return revs
->reflog_info
?
3563 get_reflog_timestamp(revs
->reflog_info
) :
3567 enum commit_action
get_commit_action(struct rev_info
*revs
, struct commit
*commit
)
3569 if (commit
->object
.flags
& SHOWN
)
3570 return commit_ignore
;
3571 if (revs
->unpacked
&& has_object_pack(&commit
->object
.oid
))
3572 return commit_ignore
;
3573 if (commit
->object
.flags
& UNINTERESTING
)
3574 return commit_ignore
;
3575 if (revs
->min_age
!= -1 &&
3576 comparison_date(revs
, commit
) > revs
->min_age
)
3577 return commit_ignore
;
3578 if (revs
->min_parents
|| (revs
->max_parents
>= 0)) {
3579 int n
= commit_list_count(commit
->parents
);
3580 if ((n
< revs
->min_parents
) ||
3581 ((revs
->max_parents
>= 0) && (n
> revs
->max_parents
)))
3582 return commit_ignore
;
3584 if (!commit_match(commit
, revs
))
3585 return commit_ignore
;
3586 if (revs
->prune
&& revs
->dense
) {
3587 /* Commit without changes? */
3588 if (commit
->object
.flags
& TREESAME
) {
3590 struct commit_list
*p
;
3591 /* drop merges unless we want parenthood */
3592 if (!want_ancestry(revs
))
3593 return commit_ignore
;
3595 * If we want ancestry, then need to keep any merges
3596 * between relevant commits to tie together topology.
3597 * For consistency with TREESAME and simplification
3598 * use "relevant" here rather than just INTERESTING,
3599 * to treat bottom commit(s) as part of the topology.
3601 for (n
= 0, p
= commit
->parents
; p
; p
= p
->next
)
3602 if (relevant_commit(p
->item
))
3605 return commit_ignore
;
3611 define_commit_slab(saved_parents
, struct commit_list
*);
3613 #define EMPTY_PARENT_LIST ((struct commit_list *)-1)
3616 * You may only call save_parents() once per commit (this is checked
3617 * for non-root commits).
3619 static void save_parents(struct rev_info
*revs
, struct commit
*commit
)
3621 struct commit_list
**pp
;
3623 if (!revs
->saved_parents_slab
) {
3624 revs
->saved_parents_slab
= xmalloc(sizeof(struct saved_parents
));
3625 init_saved_parents(revs
->saved_parents_slab
);
3628 pp
= saved_parents_at(revs
->saved_parents_slab
, commit
);
3631 * When walking with reflogs, we may visit the same commit
3632 * several times: once for each appearance in the reflog.
3634 * In this case, save_parents() will be called multiple times.
3635 * We want to keep only the first set of parents. We need to
3636 * store a sentinel value for an empty (i.e., NULL) parent
3637 * list to distinguish it from a not-yet-saved list, however.
3641 if (commit
->parents
)
3642 *pp
= copy_commit_list(commit
->parents
);
3644 *pp
= EMPTY_PARENT_LIST
;
3647 static void free_saved_parents(struct rev_info
*revs
)
3649 if (revs
->saved_parents_slab
)
3650 clear_saved_parents(revs
->saved_parents_slab
);
3653 struct commit_list
*get_saved_parents(struct rev_info
*revs
, const struct commit
*commit
)
3655 struct commit_list
*parents
;
3657 if (!revs
->saved_parents_slab
)
3658 return commit
->parents
;
3660 parents
= *saved_parents_at(revs
->saved_parents_slab
, commit
);
3661 if (parents
== EMPTY_PARENT_LIST
)
3666 enum commit_action
simplify_commit(struct rev_info
*revs
, struct commit
*commit
)
3668 enum commit_action action
= get_commit_action(revs
, commit
);
3670 if (action
== commit_show
&&
3671 revs
->prune
&& revs
->dense
&& want_ancestry(revs
)) {
3673 * --full-diff on simplified parents is no good: it
3674 * will show spurious changes from the commits that
3675 * were elided. So we save the parents on the side
3676 * when --full-diff is in effect.
3678 if (revs
->full_diff
)
3679 save_parents(revs
, commit
);
3680 if (rewrite_parents(revs
, commit
, rewrite_one
) < 0)
3681 return commit_error
;
3686 static void track_linear(struct rev_info
*revs
, struct commit
*commit
)
3688 if (revs
->track_first_time
) {
3690 revs
->track_first_time
= 0;
3692 struct commit_list
*p
;
3693 for (p
= revs
->previous_parents
; p
; p
= p
->next
)
3694 if (p
->item
== NULL
|| /* first commit */
3695 oideq(&p
->item
->object
.oid
, &commit
->object
.oid
))
3697 revs
->linear
= p
!= NULL
;
3699 if (revs
->reverse
) {
3701 commit
->object
.flags
|= TRACK_LINEAR
;
3703 free_commit_list(revs
->previous_parents
);
3704 revs
->previous_parents
= copy_commit_list(commit
->parents
);
3707 static struct commit
*get_revision_1(struct rev_info
*revs
)
3710 struct commit
*commit
;
3712 if (revs
->reflog_info
)
3713 commit
= next_reflog_entry(revs
->reflog_info
);
3714 else if (revs
->topo_walk_info
)
3715 commit
= next_topo_commit(revs
);
3717 commit
= pop_commit(&revs
->commits
);
3722 if (revs
->reflog_info
)
3723 commit
->object
.flags
&= ~(ADDED
| SEEN
| SHOWN
);
3726 * If we haven't done the list limiting, we need to look at
3727 * the parents here. We also need to do the date-based limiting
3728 * that we'd otherwise have done in limit_list().
3730 if (!revs
->limited
) {
3731 if (revs
->max_age
!= -1 &&
3732 comparison_date(revs
, commit
) < revs
->max_age
)
3735 if (revs
->reflog_info
)
3736 try_to_simplify_commit(revs
, commit
);
3737 else if (revs
->topo_walk_info
)
3738 expand_topo_walk(revs
, commit
);
3739 else if (process_parents(revs
, commit
, &revs
->commits
, NULL
) < 0) {
3740 if (!revs
->ignore_missing_links
)
3741 die("Failed to traverse parents of commit %s",
3742 oid_to_hex(&commit
->object
.oid
));
3746 switch (simplify_commit(revs
, commit
)) {
3750 die("Failed to simplify parents of commit %s",
3751 oid_to_hex(&commit
->object
.oid
));
3753 if (revs
->track_linear
)
3754 track_linear(revs
, commit
);
3761 * Return true for entries that have not yet been shown. (This is an
3762 * object_array_each_func_t.)
3764 static int entry_unshown(struct object_array_entry
*entry
, void *cb_data_unused
)
3766 return !(entry
->item
->flags
& SHOWN
);
3770 * If array is on the verge of a realloc, garbage-collect any entries
3771 * that have already been shown to try to free up some space.
3773 static void gc_boundary(struct object_array
*array
)
3775 if (array
->nr
== array
->alloc
)
3776 object_array_filter(array
, entry_unshown
, NULL
);
3779 static void create_boundary_commit_list(struct rev_info
*revs
)
3783 struct object_array
*array
= &revs
->boundary_commits
;
3784 struct object_array_entry
*objects
= array
->objects
;
3787 * If revs->commits is non-NULL at this point, an error occurred in
3788 * get_revision_1(). Ignore the error and continue printing the
3789 * boundary commits anyway. (This is what the code has always
3792 if (revs
->commits
) {
3793 free_commit_list(revs
->commits
);
3794 revs
->commits
= NULL
;
3798 * Put all of the actual boundary commits from revs->boundary_commits
3799 * into revs->commits
3801 for (i
= 0; i
< array
->nr
; i
++) {
3802 c
= (struct commit
*)(objects
[i
].item
);
3805 if (!(c
->object
.flags
& CHILD_SHOWN
))
3807 if (c
->object
.flags
& (SHOWN
| BOUNDARY
))
3809 c
->object
.flags
|= BOUNDARY
;
3810 commit_list_insert(c
, &revs
->commits
);
3814 * If revs->topo_order is set, sort the boundary commits
3815 * in topological order
3817 sort_in_topological_order(&revs
->commits
, revs
->sort_order
);
3820 static struct commit
*get_revision_internal(struct rev_info
*revs
)
3822 struct commit
*c
= NULL
;
3823 struct commit_list
*l
;
3825 if (revs
->boundary
== 2) {
3827 * All of the normal commits have already been returned,
3828 * and we are now returning boundary commits.
3829 * create_boundary_commit_list() has populated
3830 * revs->commits with the remaining commits to return.
3832 c
= pop_commit(&revs
->commits
);
3834 c
->object
.flags
|= SHOWN
;
3839 * If our max_count counter has reached zero, then we are done. We
3840 * don't simply return NULL because we still might need to show
3841 * boundary commits. But we want to avoid calling get_revision_1, which
3842 * might do a considerable amount of work finding the next commit only
3843 * for us to throw it away.
3845 * If it is non-zero, then either we don't have a max_count at all
3846 * (-1), or it is still counting, in which case we decrement.
3848 if (revs
->max_count
) {
3849 c
= get_revision_1(revs
);
3851 while (revs
->skip_count
> 0) {
3853 c
= get_revision_1(revs
);
3859 if (revs
->max_count
> 0)
3864 c
->object
.flags
|= SHOWN
;
3866 if (!revs
->boundary
)
3871 * get_revision_1() runs out the commits, and
3872 * we are done computing the boundaries.
3873 * switch to boundary commits output mode.
3878 * Update revs->commits to contain the list of
3881 create_boundary_commit_list(revs
);
3883 return get_revision_internal(revs
);
3887 * boundary commits are the commits that are parents of the
3888 * ones we got from get_revision_1() but they themselves are
3889 * not returned from get_revision_1(). Before returning
3890 * 'c', we need to mark its parents that they could be boundaries.
3893 for (l
= c
->parents
; l
; l
= l
->next
) {
3895 p
= &(l
->item
->object
);
3896 if (p
->flags
& (CHILD_SHOWN
| SHOWN
))
3898 p
->flags
|= CHILD_SHOWN
;
3899 gc_boundary(&revs
->boundary_commits
);
3900 add_object_array(p
, NULL
, &revs
->boundary_commits
);
3906 struct commit
*get_revision(struct rev_info
*revs
)
3909 struct commit_list
*reversed
;
3911 if (revs
->reverse
) {
3913 while ((c
= get_revision_internal(revs
)))
3914 commit_list_insert(c
, &reversed
);
3915 revs
->commits
= reversed
;
3917 revs
->reverse_output_stage
= 1;
3920 if (revs
->reverse_output_stage
) {
3921 c
= pop_commit(&revs
->commits
);
3922 if (revs
->track_linear
)
3923 revs
->linear
= !!(c
&& c
->object
.flags
& TRACK_LINEAR
);
3927 c
= get_revision_internal(revs
);
3928 if (c
&& revs
->graph
)
3929 graph_update(revs
->graph
, c
);
3931 free_saved_parents(revs
);
3932 if (revs
->previous_parents
) {
3933 free_commit_list(revs
->previous_parents
);
3934 revs
->previous_parents
= NULL
;
3940 char *get_revision_mark(const struct rev_info
*revs
, const struct commit
*commit
)
3942 if (commit
->object
.flags
& BOUNDARY
)
3944 else if (commit
->object
.flags
& UNINTERESTING
)
3946 else if (commit
->object
.flags
& PATCHSAME
)
3948 else if (!revs
|| revs
->left_right
) {
3949 if (commit
->object
.flags
& SYMMETRIC_LEFT
)
3953 } else if (revs
->graph
)
3955 else if (revs
->cherry_mark
)
3960 void put_revision_mark(const struct rev_info
*revs
, const struct commit
*commit
)
3962 char *mark
= get_revision_mark(revs
, commit
);
3965 fputs(mark
, stdout
);