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 hashmap_entry
*eptr
,
111 const struct hashmap_entry
*entry_or_key
,
114 const struct path_and_oids_entry
*e1
, *e2
;
116 e1
= container_of(eptr
, const struct path_and_oids_entry
, ent
);
117 e2
= container_of(entry_or_key
, const struct path_and_oids_entry
, ent
);
119 return strcmp(e1
->path
, e2
->path
);
122 static void paths_and_oids_init(struct hashmap
*map
)
124 hashmap_init(map
, path_and_oids_cmp
, NULL
, 0);
127 static void paths_and_oids_clear(struct hashmap
*map
)
129 struct hashmap_iter iter
;
130 struct path_and_oids_entry
*entry
;
132 hashmap_for_each_entry(map
, &iter
, entry
, ent
/* member name */) {
133 oidset_clear(&entry
->trees
);
137 hashmap_free_entries(map
, struct path_and_oids_entry
, ent
);
140 static void paths_and_oids_insert(struct hashmap
*map
,
142 const struct object_id
*oid
)
144 int hash
= strhash(path
);
145 struct path_and_oids_entry key
;
146 struct path_and_oids_entry
*entry
;
148 hashmap_entry_init(&key
.ent
, hash
);
150 /* use a shallow copy for the lookup */
151 key
.path
= (char *)path
;
152 oidset_init(&key
.trees
, 0);
154 entry
= hashmap_get_entry(map
, &key
, NULL
,
155 struct path_and_oids_entry
, ent
);
157 entry
= xcalloc(1, sizeof(struct path_and_oids_entry
));
158 hashmap_entry_init(&entry
->ent
, hash
);
159 entry
->path
= xstrdup(key
.path
);
160 oidset_init(&entry
->trees
, 16);
161 hashmap_put(map
, &entry
->ent
);
164 oidset_insert(&entry
->trees
, oid
);
167 static void add_children_by_path(struct repository
*r
,
171 struct tree_desc desc
;
172 struct name_entry entry
;
177 if (parse_tree_gently(tree
, 1) < 0)
180 init_tree_desc(&desc
, tree
->buffer
, tree
->size
);
181 while (tree_entry(&desc
, &entry
)) {
182 switch (object_type(entry
.mode
)) {
184 paths_and_oids_insert(map
, entry
.path
, &entry
.oid
);
186 if (tree
->object
.flags
& UNINTERESTING
) {
187 struct tree
*child
= lookup_tree(r
, &entry
.oid
);
189 child
->object
.flags
|= UNINTERESTING
;
193 if (tree
->object
.flags
& UNINTERESTING
) {
194 struct blob
*child
= lookup_blob(r
, &entry
.oid
);
196 child
->object
.flags
|= UNINTERESTING
;
200 /* Subproject commit - not in this repository */
205 free_tree_buffer(tree
);
208 void mark_trees_uninteresting_sparse(struct repository
*r
,
209 struct oidset
*trees
)
211 unsigned has_interesting
= 0, has_uninteresting
= 0;
213 struct hashmap_iter map_iter
;
214 struct path_and_oids_entry
*entry
;
215 struct object_id
*oid
;
216 struct oidset_iter iter
;
218 oidset_iter_init(trees
, &iter
);
219 while ((!has_interesting
|| !has_uninteresting
) &&
220 (oid
= oidset_iter_next(&iter
))) {
221 struct tree
*tree
= lookup_tree(r
, oid
);
226 if (tree
->object
.flags
& UNINTERESTING
)
227 has_uninteresting
= 1;
232 /* Do not walk unless we have both types of trees. */
233 if (!has_uninteresting
|| !has_interesting
)
236 paths_and_oids_init(&map
);
238 oidset_iter_init(trees
, &iter
);
239 while ((oid
= oidset_iter_next(&iter
))) {
240 struct tree
*tree
= lookup_tree(r
, oid
);
241 add_children_by_path(r
, tree
, &map
);
244 hashmap_for_each_entry(&map
, &map_iter
, entry
, ent
/* member name */)
245 mark_trees_uninteresting_sparse(r
, &entry
->trees
);
247 paths_and_oids_clear(&map
);
250 struct commit_stack
{
251 struct commit
**items
;
254 #define COMMIT_STACK_INIT { NULL, 0, 0 }
256 static void commit_stack_push(struct commit_stack
*stack
, struct commit
*commit
)
258 ALLOC_GROW(stack
->items
, stack
->nr
+ 1, stack
->alloc
);
259 stack
->items
[stack
->nr
++] = commit
;
262 static struct commit
*commit_stack_pop(struct commit_stack
*stack
)
264 return stack
->nr
? stack
->items
[--stack
->nr
] : NULL
;
267 static void commit_stack_clear(struct commit_stack
*stack
)
269 FREE_AND_NULL(stack
->items
);
270 stack
->nr
= stack
->alloc
= 0;
273 static void mark_one_parent_uninteresting(struct commit
*commit
,
274 struct commit_stack
*pending
)
276 struct commit_list
*l
;
278 if (commit
->object
.flags
& UNINTERESTING
)
280 commit
->object
.flags
|= UNINTERESTING
;
283 * Normally we haven't parsed the parent
284 * yet, so we won't have a parent of a parent
285 * here. However, it may turn out that we've
286 * reached this commit some other way (where it
287 * wasn't uninteresting), in which case we need
288 * to mark its parents recursively too..
290 for (l
= commit
->parents
; l
; l
= l
->next
)
291 commit_stack_push(pending
, l
->item
);
294 void mark_parents_uninteresting(struct commit
*commit
)
296 struct commit_stack pending
= COMMIT_STACK_INIT
;
297 struct commit_list
*l
;
299 for (l
= commit
->parents
; l
; l
= l
->next
)
300 mark_one_parent_uninteresting(l
->item
, &pending
);
302 while (pending
.nr
> 0)
303 mark_one_parent_uninteresting(commit_stack_pop(&pending
),
306 commit_stack_clear(&pending
);
309 static void add_pending_object_with_path(struct rev_info
*revs
,
311 const char *name
, unsigned mode
,
316 if (revs
->no_walk
&& (obj
->flags
& UNINTERESTING
))
318 if (revs
->reflog_info
&& obj
->type
== OBJ_COMMIT
) {
319 struct strbuf buf
= STRBUF_INIT
;
320 int len
= interpret_branch_name(name
, 0, &buf
, 0);
322 if (0 < len
&& name
[len
] && buf
.len
)
323 strbuf_addstr(&buf
, name
+ len
);
324 add_reflog_for_walk(revs
->reflog_info
,
325 (struct commit
*)obj
,
326 buf
.buf
[0] ? buf
.buf
: name
);
327 strbuf_release(&buf
);
328 return; /* do not add the commit itself */
330 add_object_array_with_path(obj
, name
, &revs
->pending
, mode
, path
);
333 static void add_pending_object_with_mode(struct rev_info
*revs
,
335 const char *name
, unsigned mode
)
337 add_pending_object_with_path(revs
, obj
, name
, mode
, NULL
);
340 void add_pending_object(struct rev_info
*revs
,
341 struct object
*obj
, const char *name
)
343 add_pending_object_with_mode(revs
, obj
, name
, S_IFINVALID
);
346 void add_head_to_pending(struct rev_info
*revs
)
348 struct object_id oid
;
350 if (get_oid("HEAD", &oid
))
352 obj
= parse_object(revs
->repo
, &oid
);
355 add_pending_object(revs
, obj
, "HEAD");
358 static struct object
*get_reference(struct rev_info
*revs
, const char *name
,
359 const struct object_id
*oid
,
362 struct object
*object
;
365 * If the repository has commit graphs, repo_parse_commit() avoids
366 * reading the object buffer, so use it whenever possible.
368 if (oid_object_info(revs
->repo
, oid
, NULL
) == OBJ_COMMIT
) {
369 struct commit
*c
= lookup_commit(revs
->repo
, oid
);
370 if (!repo_parse_commit(revs
->repo
, c
))
371 object
= (struct object
*) c
;
375 object
= parse_object(revs
->repo
, oid
);
379 if (revs
->ignore_missing
)
381 if (revs
->exclude_promisor_objects
&& is_promisor_object(oid
))
383 die("bad object %s", name
);
385 object
->flags
|= flags
;
389 void add_pending_oid(struct rev_info
*revs
, const char *name
,
390 const struct object_id
*oid
, unsigned int flags
)
392 struct object
*object
= get_reference(revs
, name
, oid
, flags
);
393 add_pending_object(revs
, object
, name
);
396 static struct commit
*handle_commit(struct rev_info
*revs
,
397 struct object_array_entry
*entry
)
399 struct object
*object
= entry
->item
;
400 const char *name
= entry
->name
;
401 const char *path
= entry
->path
;
402 unsigned int mode
= entry
->mode
;
403 unsigned long flags
= object
->flags
;
406 * Tag object? Look what it points to..
408 while (object
->type
== OBJ_TAG
) {
409 struct tag
*tag
= (struct tag
*) object
;
410 if (revs
->tag_objects
&& !(flags
& UNINTERESTING
))
411 add_pending_object(revs
, object
, tag
->tag
);
414 object
= parse_object(revs
->repo
, &tag
->tagged
->oid
);
416 if (revs
->ignore_missing_links
|| (flags
& UNINTERESTING
))
418 if (revs
->exclude_promisor_objects
&&
419 is_promisor_object(&tag
->tagged
->oid
))
421 die("bad object %s", oid_to_hex(&tag
->tagged
->oid
));
423 object
->flags
|= flags
;
425 * We'll handle the tagged object by looping or dropping
426 * through to the non-tag handlers below. Do not
427 * propagate path data from the tag's pending entry.
434 * Commit object? Just return it, we'll do all the complex
437 if (object
->type
== OBJ_COMMIT
) {
438 struct commit
*commit
= (struct commit
*)object
;
440 if (parse_commit(commit
) < 0)
441 die("unable to parse commit %s", name
);
442 if (flags
& UNINTERESTING
) {
443 mark_parents_uninteresting(commit
);
445 if (!revs
->topo_order
|| !generation_numbers_enabled(the_repository
))
449 char **slot
= revision_sources_at(revs
->sources
, commit
);
452 *slot
= xstrdup(name
);
458 * Tree object? Either mark it uninteresting, or add it
459 * to the list of objects to look at later..
461 if (object
->type
== OBJ_TREE
) {
462 struct tree
*tree
= (struct tree
*)object
;
463 if (!revs
->tree_objects
)
465 if (flags
& UNINTERESTING
) {
466 mark_tree_contents_uninteresting(revs
->repo
, tree
);
469 add_pending_object_with_path(revs
, object
, name
, mode
, path
);
474 * Blob object? You know the drill by now..
476 if (object
->type
== OBJ_BLOB
) {
477 if (!revs
->blob_objects
)
479 if (flags
& UNINTERESTING
)
481 add_pending_object_with_path(revs
, object
, name
, mode
, path
);
484 die("%s is unknown object", name
);
487 static int everybody_uninteresting(struct commit_list
*orig
,
488 struct commit
**interesting_cache
)
490 struct commit_list
*list
= orig
;
492 if (*interesting_cache
) {
493 struct commit
*commit
= *interesting_cache
;
494 if (!(commit
->object
.flags
& UNINTERESTING
))
499 struct commit
*commit
= list
->item
;
501 if (commit
->object
.flags
& UNINTERESTING
)
504 *interesting_cache
= commit
;
511 * A definition of "relevant" commit that we can use to simplify limited graphs
512 * by eliminating side branches.
514 * A "relevant" commit is one that is !UNINTERESTING (ie we are including it
515 * in our list), or that is a specified BOTTOM commit. Then after computing
516 * a limited list, during processing we can generally ignore boundary merges
517 * coming from outside the graph, (ie from irrelevant parents), and treat
518 * those merges as if they were single-parent. TREESAME is defined to consider
519 * only relevant parents, if any. If we are TREESAME to our on-graph parents,
520 * we don't care if we were !TREESAME to non-graph parents.
522 * Treating bottom commits as relevant ensures that a limited graph's
523 * connection to the actual bottom commit is not viewed as a side branch, but
524 * treated as part of the graph. For example:
526 * ....Z...A---X---o---o---B
530 * When computing "A..B", the A-X connection is at least as important as
531 * Y-X, despite A being flagged UNINTERESTING.
533 * And when computing --ancestry-path "A..B", the A-X connection is more
534 * important than Y-X, despite both A and Y being flagged UNINTERESTING.
536 static inline int relevant_commit(struct commit
*commit
)
538 return (commit
->object
.flags
& (UNINTERESTING
| BOTTOM
)) != UNINTERESTING
;
542 * Return a single relevant commit from a parent list. If we are a TREESAME
543 * commit, and this selects one of our parents, then we can safely simplify to
546 static struct commit
*one_relevant_parent(const struct rev_info
*revs
,
547 struct commit_list
*orig
)
549 struct commit_list
*list
= orig
;
550 struct commit
*relevant
= NULL
;
556 * For 1-parent commits, or if first-parent-only, then return that
557 * first parent (even if not "relevant" by the above definition).
558 * TREESAME will have been set purely on that parent.
560 if (revs
->first_parent_only
|| !orig
->next
)
564 * For multi-parent commits, identify a sole relevant parent, if any.
565 * If we have only one relevant parent, then TREESAME will be set purely
566 * with regard to that parent, and we can simplify accordingly.
568 * If we have more than one relevant parent, or no relevant parents
569 * (and multiple irrelevant ones), then we can't select a parent here
573 struct commit
*commit
= list
->item
;
575 if (relevant_commit(commit
)) {
585 * The goal is to get REV_TREE_NEW as the result only if the
586 * diff consists of all '+' (and no other changes), REV_TREE_OLD
587 * if the whole diff is removal of old data, and otherwise
588 * REV_TREE_DIFFERENT (of course if the trees are the same we
589 * want REV_TREE_SAME).
591 * The only time we care about the distinction is when
592 * remove_empty_trees is in effect, in which case we care only about
593 * whether the whole change is REV_TREE_NEW, or if there's another type
594 * of change. Which means we can stop the diff early in either of these
597 * 1. We're not using remove_empty_trees at all.
599 * 2. We saw anything except REV_TREE_NEW.
601 static int tree_difference
= REV_TREE_SAME
;
603 static void file_add_remove(struct diff_options
*options
,
604 int addremove
, unsigned mode
,
605 const struct object_id
*oid
,
607 const char *fullpath
, unsigned dirty_submodule
)
609 int diff
= addremove
== '+' ? REV_TREE_NEW
: REV_TREE_OLD
;
610 struct rev_info
*revs
= options
->change_fn_data
;
612 tree_difference
|= diff
;
613 if (!revs
->remove_empty_trees
|| tree_difference
!= REV_TREE_NEW
)
614 options
->flags
.has_changes
= 1;
617 static void file_change(struct diff_options
*options
,
618 unsigned old_mode
, unsigned new_mode
,
619 const struct object_id
*old_oid
,
620 const struct object_id
*new_oid
,
621 int old_oid_valid
, int new_oid_valid
,
622 const char *fullpath
,
623 unsigned old_dirty_submodule
, unsigned new_dirty_submodule
)
625 tree_difference
= REV_TREE_DIFFERENT
;
626 options
->flags
.has_changes
= 1;
629 static int rev_compare_tree(struct rev_info
*revs
,
630 struct commit
*parent
, struct commit
*commit
)
632 struct tree
*t1
= get_commit_tree(parent
);
633 struct tree
*t2
= get_commit_tree(commit
);
640 if (revs
->simplify_by_decoration
) {
642 * If we are simplifying by decoration, then the commit
643 * is worth showing if it has a tag pointing at it.
645 if (get_name_decoration(&commit
->object
))
646 return REV_TREE_DIFFERENT
;
648 * A commit that is not pointed by a tag is uninteresting
649 * if we are not limited by path. This means that you will
650 * see the usual "commits that touch the paths" plus any
651 * tagged commit by specifying both --simplify-by-decoration
654 if (!revs
->prune_data
.nr
)
655 return REV_TREE_SAME
;
658 tree_difference
= REV_TREE_SAME
;
659 revs
->pruning
.flags
.has_changes
= 0;
660 if (diff_tree_oid(&t1
->object
.oid
, &t2
->object
.oid
, "",
662 return REV_TREE_DIFFERENT
;
663 return tree_difference
;
666 static int rev_same_tree_as_empty(struct rev_info
*revs
, struct commit
*commit
)
669 struct tree
*t1
= get_commit_tree(commit
);
674 tree_difference
= REV_TREE_SAME
;
675 revs
->pruning
.flags
.has_changes
= 0;
676 retval
= diff_tree_oid(NULL
, &t1
->object
.oid
, "", &revs
->pruning
);
678 return retval
>= 0 && (tree_difference
== REV_TREE_SAME
);
681 struct treesame_state
{
682 unsigned int nparents
;
683 unsigned char treesame
[FLEX_ARRAY
];
686 static struct treesame_state
*initialise_treesame(struct rev_info
*revs
, struct commit
*commit
)
688 unsigned n
= commit_list_count(commit
->parents
);
689 struct treesame_state
*st
= xcalloc(1, st_add(sizeof(*st
), n
));
691 add_decoration(&revs
->treesame
, &commit
->object
, st
);
696 * Must be called immediately after removing the nth_parent from a commit's
697 * parent list, if we are maintaining the per-parent treesame[] decoration.
698 * This does not recalculate the master TREESAME flag - update_treesame()
699 * should be called to update it after a sequence of treesame[] modifications
700 * that may have affected it.
702 static int compact_treesame(struct rev_info
*revs
, struct commit
*commit
, unsigned nth_parent
)
704 struct treesame_state
*st
;
707 if (!commit
->parents
) {
709 * Have just removed the only parent from a non-merge.
710 * Different handling, as we lack decoration.
713 die("compact_treesame %u", nth_parent
);
714 old_same
= !!(commit
->object
.flags
& TREESAME
);
715 if (rev_same_tree_as_empty(revs
, commit
))
716 commit
->object
.flags
|= TREESAME
;
718 commit
->object
.flags
&= ~TREESAME
;
722 st
= lookup_decoration(&revs
->treesame
, &commit
->object
);
723 if (!st
|| nth_parent
>= st
->nparents
)
724 die("compact_treesame %u", nth_parent
);
726 old_same
= st
->treesame
[nth_parent
];
727 memmove(st
->treesame
+ nth_parent
,
728 st
->treesame
+ nth_parent
+ 1,
729 st
->nparents
- nth_parent
- 1);
732 * If we've just become a non-merge commit, update TREESAME
733 * immediately, and remove the no-longer-needed decoration.
734 * If still a merge, defer update until update_treesame().
736 if (--st
->nparents
== 1) {
737 if (commit
->parents
->next
)
738 die("compact_treesame parents mismatch");
739 if (st
->treesame
[0] && revs
->dense
)
740 commit
->object
.flags
|= TREESAME
;
742 commit
->object
.flags
&= ~TREESAME
;
743 free(add_decoration(&revs
->treesame
, &commit
->object
, NULL
));
749 static unsigned update_treesame(struct rev_info
*revs
, struct commit
*commit
)
751 if (commit
->parents
&& commit
->parents
->next
) {
753 struct treesame_state
*st
;
754 struct commit_list
*p
;
755 unsigned relevant_parents
;
756 unsigned relevant_change
, irrelevant_change
;
758 st
= lookup_decoration(&revs
->treesame
, &commit
->object
);
760 die("update_treesame %s", oid_to_hex(&commit
->object
.oid
));
761 relevant_parents
= 0;
762 relevant_change
= irrelevant_change
= 0;
763 for (p
= commit
->parents
, n
= 0; p
; n
++, p
= p
->next
) {
764 if (relevant_commit(p
->item
)) {
765 relevant_change
|= !st
->treesame
[n
];
768 irrelevant_change
|= !st
->treesame
[n
];
770 if (relevant_parents
? relevant_change
: irrelevant_change
)
771 commit
->object
.flags
&= ~TREESAME
;
773 commit
->object
.flags
|= TREESAME
;
776 return commit
->object
.flags
& TREESAME
;
779 static inline int limiting_can_increase_treesame(const struct rev_info
*revs
)
782 * TREESAME is irrelevant unless prune && dense;
783 * if simplify_history is set, we can't have a mixture of TREESAME and
784 * !TREESAME INTERESTING parents (and we don't have treesame[]
785 * decoration anyway);
786 * if first_parent_only is set, then the TREESAME flag is locked
787 * against the first parent (and again we lack treesame[] decoration).
789 return revs
->prune
&& revs
->dense
&&
790 !revs
->simplify_history
&&
791 !revs
->first_parent_only
;
794 static void try_to_simplify_commit(struct rev_info
*revs
, struct commit
*commit
)
796 struct commit_list
**pp
, *parent
;
797 struct treesame_state
*ts
= NULL
;
798 int relevant_change
= 0, irrelevant_change
= 0;
799 int relevant_parents
, nth_parent
;
802 * If we don't do pruning, everything is interesting
807 if (!get_commit_tree(commit
))
810 if (!commit
->parents
) {
811 if (rev_same_tree_as_empty(revs
, commit
))
812 commit
->object
.flags
|= TREESAME
;
817 * Normal non-merge commit? If we don't want to make the
818 * history dense, we consider it always to be a change..
820 if (!revs
->dense
&& !commit
->parents
->next
)
823 for (pp
= &commit
->parents
, nth_parent
= 0, relevant_parents
= 0;
824 (parent
= *pp
) != NULL
;
825 pp
= &parent
->next
, nth_parent
++) {
826 struct commit
*p
= parent
->item
;
827 if (relevant_commit(p
))
830 if (nth_parent
== 1) {
832 * This our second loop iteration - so we now know
833 * we're dealing with a merge.
835 * Do not compare with later parents when we care only about
836 * the first parent chain, in order to avoid derailing the
837 * traversal to follow a side branch that brought everything
838 * in the path we are limited to by the pathspec.
840 if (revs
->first_parent_only
)
843 * If this will remain a potentially-simplifiable
844 * merge, remember per-parent treesame if needed.
845 * Initialise the array with the comparison from our
848 if (revs
->treesame
.name
&&
849 !revs
->simplify_history
&&
850 !(commit
->object
.flags
& UNINTERESTING
)) {
851 ts
= initialise_treesame(revs
, commit
);
852 if (!(irrelevant_change
|| relevant_change
))
856 if (parse_commit(p
) < 0)
857 die("cannot simplify commit %s (because of %s)",
858 oid_to_hex(&commit
->object
.oid
),
859 oid_to_hex(&p
->object
.oid
));
860 switch (rev_compare_tree(revs
, p
, commit
)) {
862 if (!revs
->simplify_history
|| !relevant_commit(p
)) {
863 /* Even if a merge with an uninteresting
864 * side branch brought the entire change
865 * we are interested in, we do not want
866 * to lose the other branches of this
867 * merge, so we just keep going.
870 ts
->treesame
[nth_parent
] = 1;
874 commit
->parents
= parent
;
875 commit
->object
.flags
|= TREESAME
;
879 if (revs
->remove_empty_trees
&&
880 rev_same_tree_as_empty(revs
, p
)) {
881 /* We are adding all the specified
882 * paths from this parent, so the
883 * history beyond this parent is not
884 * interesting. Remove its parents
885 * (they are grandparents for us).
886 * IOW, we pretend this parent is a
889 if (parse_commit(p
) < 0)
890 die("cannot simplify commit %s (invalid %s)",
891 oid_to_hex(&commit
->object
.oid
),
892 oid_to_hex(&p
->object
.oid
));
897 case REV_TREE_DIFFERENT
:
898 if (relevant_commit(p
))
901 irrelevant_change
= 1;
904 die("bad tree compare for commit %s", oid_to_hex(&commit
->object
.oid
));
908 * TREESAME is straightforward for single-parent commits. For merge
909 * commits, it is most useful to define it so that "irrelevant"
910 * parents cannot make us !TREESAME - if we have any relevant
911 * parents, then we only consider TREESAMEness with respect to them,
912 * allowing irrelevant merges from uninteresting branches to be
913 * simplified away. Only if we have only irrelevant parents do we
914 * base TREESAME on them. Note that this logic is replicated in
915 * update_treesame, which should be kept in sync.
917 if (relevant_parents
? !relevant_change
: !irrelevant_change
)
918 commit
->object
.flags
|= TREESAME
;
921 static int process_parents(struct rev_info
*revs
, struct commit
*commit
,
922 struct commit_list
**list
, struct prio_queue
*queue
)
924 struct commit_list
*parent
= commit
->parents
;
927 if (commit
->object
.flags
& ADDED
)
929 commit
->object
.flags
|= ADDED
;
931 if (revs
->include_check
&&
932 !revs
->include_check(commit
, revs
->include_check_data
))
936 * If the commit is uninteresting, don't try to
937 * prune parents - we want the maximal uninteresting
940 * Normally we haven't parsed the parent
941 * yet, so we won't have a parent of a parent
942 * here. However, it may turn out that we've
943 * reached this commit some other way (where it
944 * wasn't uninteresting), in which case we need
945 * to mark its parents recursively too..
947 if (commit
->object
.flags
& UNINTERESTING
) {
949 struct commit
*p
= parent
->item
;
950 parent
= parent
->next
;
952 p
->object
.flags
|= UNINTERESTING
;
953 if (parse_commit_gently(p
, 1) < 0)
956 mark_parents_uninteresting(p
);
957 if (p
->object
.flags
& SEEN
)
959 p
->object
.flags
|= SEEN
;
961 commit_list_insert_by_date(p
, list
);
963 prio_queue_put(queue
, p
);
969 * Ok, the commit wasn't uninteresting. Try to
970 * simplify the commit history and find the parent
971 * that has no differences in the path set if one exists.
973 try_to_simplify_commit(revs
, commit
);
978 left_flag
= (commit
->object
.flags
& SYMMETRIC_LEFT
);
980 for (parent
= commit
->parents
; parent
; parent
= parent
->next
) {
981 struct commit
*p
= parent
->item
;
982 int gently
= revs
->ignore_missing_links
||
983 revs
->exclude_promisor_objects
;
984 if (parse_commit_gently(p
, gently
) < 0) {
985 if (revs
->exclude_promisor_objects
&&
986 is_promisor_object(&p
->object
.oid
)) {
987 if (revs
->first_parent_only
)
994 char **slot
= revision_sources_at(revs
->sources
, p
);
997 *slot
= *revision_sources_at(revs
->sources
, commit
);
999 p
->object
.flags
|= left_flag
;
1000 if (!(p
->object
.flags
& SEEN
)) {
1001 p
->object
.flags
|= SEEN
;
1003 commit_list_insert_by_date(p
, list
);
1005 prio_queue_put(queue
, p
);
1007 if (revs
->first_parent_only
)
1013 static void cherry_pick_list(struct commit_list
*list
, struct rev_info
*revs
)
1015 struct commit_list
*p
;
1016 int left_count
= 0, right_count
= 0;
1018 struct patch_ids ids
;
1019 unsigned cherry_flag
;
1021 /* First count the commits on the left and on the right */
1022 for (p
= list
; p
; p
= p
->next
) {
1023 struct commit
*commit
= p
->item
;
1024 unsigned flags
= commit
->object
.flags
;
1025 if (flags
& BOUNDARY
)
1027 else if (flags
& SYMMETRIC_LEFT
)
1033 if (!left_count
|| !right_count
)
1036 left_first
= left_count
< right_count
;
1037 init_patch_ids(revs
->repo
, &ids
);
1038 ids
.diffopts
.pathspec
= revs
->diffopt
.pathspec
;
1040 /* Compute patch-ids for one side */
1041 for (p
= list
; p
; p
= p
->next
) {
1042 struct commit
*commit
= p
->item
;
1043 unsigned flags
= commit
->object
.flags
;
1045 if (flags
& BOUNDARY
)
1048 * If we have fewer left, left_first is set and we omit
1049 * commits on the right branch in this loop. If we have
1050 * fewer right, we skip the left ones.
1052 if (left_first
!= !!(flags
& SYMMETRIC_LEFT
))
1054 add_commit_patch_id(commit
, &ids
);
1057 /* either cherry_mark or cherry_pick are true */
1058 cherry_flag
= revs
->cherry_mark
? PATCHSAME
: SHOWN
;
1060 /* Check the other side */
1061 for (p
= list
; p
; p
= p
->next
) {
1062 struct commit
*commit
= p
->item
;
1063 struct patch_id
*id
;
1064 unsigned flags
= commit
->object
.flags
;
1066 if (flags
& BOUNDARY
)
1069 * If we have fewer left, left_first is set and we omit
1070 * commits on the left branch in this loop.
1072 if (left_first
== !!(flags
& SYMMETRIC_LEFT
))
1076 * Have we seen the same patch id?
1078 id
= has_commit_patch_id(commit
, &ids
);
1082 commit
->object
.flags
|= cherry_flag
;
1083 id
->commit
->object
.flags
|= cherry_flag
;
1086 free_patch_ids(&ids
);
1089 /* How many extra uninteresting commits we want to see.. */
1092 static int still_interesting(struct commit_list
*src
, timestamp_t date
, int slop
,
1093 struct commit
**interesting_cache
)
1096 * No source list at all? We're definitely done..
1102 * Does the destination list contain entries with a date
1103 * before the source list? Definitely _not_ done.
1105 if (date
<= src
->item
->date
)
1109 * Does the source list still have interesting commits in
1110 * it? Definitely not done..
1112 if (!everybody_uninteresting(src
, interesting_cache
))
1115 /* Ok, we're closing in.. */
1120 * "rev-list --ancestry-path A..B" computes commits that are ancestors
1121 * of B but not ancestors of A but further limits the result to those
1122 * that are descendants of A. This takes the list of bottom commits and
1123 * the result of "A..B" without --ancestry-path, and limits the latter
1124 * further to the ones that can reach one of the commits in "bottom".
1126 static void limit_to_ancestry(struct commit_list
*bottom
, struct commit_list
*list
)
1128 struct commit_list
*p
;
1129 struct commit_list
*rlist
= NULL
;
1133 * Reverse the list so that it will be likely that we would
1134 * process parents before children.
1136 for (p
= list
; p
; p
= p
->next
)
1137 commit_list_insert(p
->item
, &rlist
);
1139 for (p
= bottom
; p
; p
= p
->next
)
1140 p
->item
->object
.flags
|= TMP_MARK
;
1143 * Mark the ones that can reach bottom commits in "list",
1144 * in a bottom-up fashion.
1148 for (p
= rlist
; p
; p
= p
->next
) {
1149 struct commit
*c
= p
->item
;
1150 struct commit_list
*parents
;
1151 if (c
->object
.flags
& (TMP_MARK
| UNINTERESTING
))
1153 for (parents
= c
->parents
;
1155 parents
= parents
->next
) {
1156 if (!(parents
->item
->object
.flags
& TMP_MARK
))
1158 c
->object
.flags
|= TMP_MARK
;
1163 } while (made_progress
);
1166 * NEEDSWORK: decide if we want to remove parents that are
1167 * not marked with TMP_MARK from commit->parents for commits
1168 * in the resulting list. We may not want to do that, though.
1172 * The ones that are not marked with TMP_MARK are uninteresting
1174 for (p
= list
; p
; p
= p
->next
) {
1175 struct commit
*c
= p
->item
;
1176 if (c
->object
.flags
& TMP_MARK
)
1178 c
->object
.flags
|= UNINTERESTING
;
1181 /* We are done with the TMP_MARK */
1182 for (p
= list
; p
; p
= p
->next
)
1183 p
->item
->object
.flags
&= ~TMP_MARK
;
1184 for (p
= bottom
; p
; p
= p
->next
)
1185 p
->item
->object
.flags
&= ~TMP_MARK
;
1186 free_commit_list(rlist
);
1190 * Before walking the history, keep the set of "negative" refs the
1191 * caller has asked to exclude.
1193 * This is used to compute "rev-list --ancestry-path A..B", as we need
1194 * to filter the result of "A..B" further to the ones that can actually
1197 static struct commit_list
*collect_bottom_commits(struct commit_list
*list
)
1199 struct commit_list
*elem
, *bottom
= NULL
;
1200 for (elem
= list
; elem
; elem
= elem
->next
)
1201 if (elem
->item
->object
.flags
& BOTTOM
)
1202 commit_list_insert(elem
->item
, &bottom
);
1206 /* Assumes either left_only or right_only is set */
1207 static void limit_left_right(struct commit_list
*list
, struct rev_info
*revs
)
1209 struct commit_list
*p
;
1211 for (p
= list
; p
; p
= p
->next
) {
1212 struct commit
*commit
= p
->item
;
1214 if (revs
->right_only
) {
1215 if (commit
->object
.flags
& SYMMETRIC_LEFT
)
1216 commit
->object
.flags
|= SHOWN
;
1217 } else /* revs->left_only is set */
1218 if (!(commit
->object
.flags
& SYMMETRIC_LEFT
))
1219 commit
->object
.flags
|= SHOWN
;
1223 static int limit_list(struct rev_info
*revs
)
1226 timestamp_t date
= TIME_MAX
;
1227 struct commit_list
*list
= revs
->commits
;
1228 struct commit_list
*newlist
= NULL
;
1229 struct commit_list
**p
= &newlist
;
1230 struct commit_list
*bottom
= NULL
;
1231 struct commit
*interesting_cache
= NULL
;
1233 if (revs
->ancestry_path
) {
1234 bottom
= collect_bottom_commits(list
);
1236 die("--ancestry-path given but there are no bottom commits");
1240 struct commit
*commit
= pop_commit(&list
);
1241 struct object
*obj
= &commit
->object
;
1242 show_early_output_fn_t show
;
1244 if (commit
== interesting_cache
)
1245 interesting_cache
= NULL
;
1247 if (revs
->max_age
!= -1 && (commit
->date
< revs
->max_age
))
1248 obj
->flags
|= UNINTERESTING
;
1249 if (process_parents(revs
, commit
, &list
, NULL
) < 0)
1251 if (obj
->flags
& UNINTERESTING
) {
1252 mark_parents_uninteresting(commit
);
1253 slop
= still_interesting(list
, date
, slop
, &interesting_cache
);
1258 if (revs
->min_age
!= -1 && (commit
->date
> revs
->min_age
))
1260 date
= commit
->date
;
1261 p
= &commit_list_insert(commit
, p
)->next
;
1263 show
= show_early_output
;
1267 show(revs
, newlist
);
1268 show_early_output
= NULL
;
1270 if (revs
->cherry_pick
|| revs
->cherry_mark
)
1271 cherry_pick_list(newlist
, revs
);
1273 if (revs
->left_only
|| revs
->right_only
)
1274 limit_left_right(newlist
, revs
);
1277 limit_to_ancestry(bottom
, newlist
);
1278 free_commit_list(bottom
);
1282 * Check if any commits have become TREESAME by some of their parents
1283 * becoming UNINTERESTING.
1285 if (limiting_can_increase_treesame(revs
))
1286 for (list
= newlist
; list
; list
= list
->next
) {
1287 struct commit
*c
= list
->item
;
1288 if (c
->object
.flags
& (UNINTERESTING
| TREESAME
))
1290 update_treesame(revs
, c
);
1293 revs
->commits
= newlist
;
1298 * Add an entry to refs->cmdline with the specified information.
1301 static void add_rev_cmdline(struct rev_info
*revs
,
1302 struct object
*item
,
1307 struct rev_cmdline_info
*info
= &revs
->cmdline
;
1308 unsigned int nr
= info
->nr
;
1310 ALLOC_GROW(info
->rev
, nr
+ 1, info
->alloc
);
1311 info
->rev
[nr
].item
= item
;
1312 info
->rev
[nr
].name
= xstrdup(name
);
1313 info
->rev
[nr
].whence
= whence
;
1314 info
->rev
[nr
].flags
= flags
;
1318 static void add_rev_cmdline_list(struct rev_info
*revs
,
1319 struct commit_list
*commit_list
,
1323 while (commit_list
) {
1324 struct object
*object
= &commit_list
->item
->object
;
1325 add_rev_cmdline(revs
, object
, oid_to_hex(&object
->oid
),
1327 commit_list
= commit_list
->next
;
1331 struct all_refs_cb
{
1333 int warned_bad_reflog
;
1334 struct rev_info
*all_revs
;
1335 const char *name_for_errormsg
;
1336 struct worktree
*wt
;
1339 int ref_excluded(struct string_list
*ref_excludes
, const char *path
)
1341 struct string_list_item
*item
;
1345 for_each_string_list_item(item
, ref_excludes
) {
1346 if (!wildmatch(item
->string
, path
, 0))
1352 static int handle_one_ref(const char *path
, const struct object_id
*oid
,
1353 int flag
, void *cb_data
)
1355 struct all_refs_cb
*cb
= cb_data
;
1356 struct object
*object
;
1358 if (ref_excluded(cb
->all_revs
->ref_excludes
, path
))
1361 object
= get_reference(cb
->all_revs
, path
, oid
, cb
->all_flags
);
1362 add_rev_cmdline(cb
->all_revs
, object
, path
, REV_CMD_REF
, cb
->all_flags
);
1363 add_pending_oid(cb
->all_revs
, path
, oid
, cb
->all_flags
);
1367 static void init_all_refs_cb(struct all_refs_cb
*cb
, struct rev_info
*revs
,
1370 cb
->all_revs
= revs
;
1371 cb
->all_flags
= flags
;
1372 revs
->rev_input_given
= 1;
1376 void clear_ref_exclusion(struct string_list
**ref_excludes_p
)
1378 if (*ref_excludes_p
) {
1379 string_list_clear(*ref_excludes_p
, 0);
1380 free(*ref_excludes_p
);
1382 *ref_excludes_p
= NULL
;
1385 void add_ref_exclusion(struct string_list
**ref_excludes_p
, const char *exclude
)
1387 if (!*ref_excludes_p
) {
1388 *ref_excludes_p
= xcalloc(1, sizeof(**ref_excludes_p
));
1389 (*ref_excludes_p
)->strdup_strings
= 1;
1391 string_list_append(*ref_excludes_p
, exclude
);
1394 static void handle_refs(struct ref_store
*refs
,
1395 struct rev_info
*revs
, unsigned flags
,
1396 int (*for_each
)(struct ref_store
*, each_ref_fn
, void *))
1398 struct all_refs_cb cb
;
1401 /* this could happen with uninitialized submodules */
1405 init_all_refs_cb(&cb
, revs
, flags
);
1406 for_each(refs
, handle_one_ref
, &cb
);
1409 static void handle_one_reflog_commit(struct object_id
*oid
, void *cb_data
)
1411 struct all_refs_cb
*cb
= cb_data
;
1412 if (!is_null_oid(oid
)) {
1413 struct object
*o
= parse_object(cb
->all_revs
->repo
, oid
);
1415 o
->flags
|= cb
->all_flags
;
1416 /* ??? CMDLINEFLAGS ??? */
1417 add_pending_object(cb
->all_revs
, o
, "");
1419 else if (!cb
->warned_bad_reflog
) {
1420 warning("reflog of '%s' references pruned commits",
1421 cb
->name_for_errormsg
);
1422 cb
->warned_bad_reflog
= 1;
1427 static int handle_one_reflog_ent(struct object_id
*ooid
, struct object_id
*noid
,
1428 const char *email
, timestamp_t timestamp
, int tz
,
1429 const char *message
, void *cb_data
)
1431 handle_one_reflog_commit(ooid
, cb_data
);
1432 handle_one_reflog_commit(noid
, cb_data
);
1436 static int handle_one_reflog(const char *refname_in_wt
,
1437 const struct object_id
*oid
,
1438 int flag
, void *cb_data
)
1440 struct all_refs_cb
*cb
= cb_data
;
1441 struct strbuf refname
= STRBUF_INIT
;
1443 cb
->warned_bad_reflog
= 0;
1444 strbuf_worktree_ref(cb
->wt
, &refname
, refname_in_wt
);
1445 cb
->name_for_errormsg
= refname
.buf
;
1446 refs_for_each_reflog_ent(get_main_ref_store(the_repository
),
1448 handle_one_reflog_ent
, cb_data
);
1449 strbuf_release(&refname
);
1453 static void add_other_reflogs_to_pending(struct all_refs_cb
*cb
)
1455 struct worktree
**worktrees
, **p
;
1457 worktrees
= get_worktrees(0);
1458 for (p
= worktrees
; *p
; p
++) {
1459 struct worktree
*wt
= *p
;
1465 refs_for_each_reflog(get_worktree_ref_store(wt
),
1469 free_worktrees(worktrees
);
1472 void add_reflogs_to_pending(struct rev_info
*revs
, unsigned flags
)
1474 struct all_refs_cb cb
;
1477 cb
.all_flags
= flags
;
1479 for_each_reflog(handle_one_reflog
, &cb
);
1481 if (!revs
->single_worktree
)
1482 add_other_reflogs_to_pending(&cb
);
1485 static void add_cache_tree(struct cache_tree
*it
, struct rev_info
*revs
,
1486 struct strbuf
*path
, unsigned int flags
)
1488 size_t baselen
= path
->len
;
1491 if (it
->entry_count
>= 0) {
1492 struct tree
*tree
= lookup_tree(revs
->repo
, &it
->oid
);
1493 tree
->object
.flags
|= flags
;
1494 add_pending_object_with_path(revs
, &tree
->object
, "",
1498 for (i
= 0; i
< it
->subtree_nr
; i
++) {
1499 struct cache_tree_sub
*sub
= it
->down
[i
];
1500 strbuf_addf(path
, "%s%s", baselen
? "/" : "", sub
->name
);
1501 add_cache_tree(sub
->cache_tree
, revs
, path
, flags
);
1502 strbuf_setlen(path
, baselen
);
1507 static void do_add_index_objects_to_pending(struct rev_info
*revs
,
1508 struct index_state
*istate
,
1513 for (i
= 0; i
< istate
->cache_nr
; i
++) {
1514 struct cache_entry
*ce
= istate
->cache
[i
];
1517 if (S_ISGITLINK(ce
->ce_mode
))
1520 blob
= lookup_blob(revs
->repo
, &ce
->oid
);
1522 die("unable to add index blob to traversal");
1523 blob
->object
.flags
|= flags
;
1524 add_pending_object_with_path(revs
, &blob
->object
, "",
1525 ce
->ce_mode
, ce
->name
);
1528 if (istate
->cache_tree
) {
1529 struct strbuf path
= STRBUF_INIT
;
1530 add_cache_tree(istate
->cache_tree
, revs
, &path
, flags
);
1531 strbuf_release(&path
);
1535 void add_index_objects_to_pending(struct rev_info
*revs
, unsigned int flags
)
1537 struct worktree
**worktrees
, **p
;
1539 repo_read_index(revs
->repo
);
1540 do_add_index_objects_to_pending(revs
, revs
->repo
->index
, flags
);
1542 if (revs
->single_worktree
)
1545 worktrees
= get_worktrees(0);
1546 for (p
= worktrees
; *p
; p
++) {
1547 struct worktree
*wt
= *p
;
1548 struct index_state istate
= { NULL
};
1551 continue; /* current index already taken care of */
1553 if (read_index_from(&istate
,
1554 worktree_git_path(wt
, "index"),
1555 get_worktree_git_dir(wt
)) > 0)
1556 do_add_index_objects_to_pending(revs
, &istate
, flags
);
1557 discard_index(&istate
);
1559 free_worktrees(worktrees
);
1562 struct add_alternate_refs_data
{
1563 struct rev_info
*revs
;
1567 static void add_one_alternate_ref(const struct object_id
*oid
,
1570 const char *name
= ".alternate";
1571 struct add_alternate_refs_data
*data
= vdata
;
1574 obj
= get_reference(data
->revs
, name
, oid
, data
->flags
);
1575 add_rev_cmdline(data
->revs
, obj
, name
, REV_CMD_REV
, data
->flags
);
1576 add_pending_object(data
->revs
, obj
, name
);
1579 static void add_alternate_refs_to_pending(struct rev_info
*revs
,
1582 struct add_alternate_refs_data data
;
1585 for_each_alternate_ref(add_one_alternate_ref
, &data
);
1588 static int add_parents_only(struct rev_info
*revs
, const char *arg_
, int flags
,
1591 struct object_id oid
;
1593 struct commit
*commit
;
1594 struct commit_list
*parents
;
1596 const char *arg
= arg_
;
1599 flags
^= UNINTERESTING
| BOTTOM
;
1602 if (get_oid_committish(arg
, &oid
))
1605 it
= get_reference(revs
, arg
, &oid
, 0);
1606 if (!it
&& revs
->ignore_missing
)
1608 if (it
->type
!= OBJ_TAG
)
1610 if (!((struct tag
*)it
)->tagged
)
1612 oidcpy(&oid
, &((struct tag
*)it
)->tagged
->oid
);
1614 if (it
->type
!= OBJ_COMMIT
)
1616 commit
= (struct commit
*)it
;
1617 if (exclude_parent
&&
1618 exclude_parent
> commit_list_count(commit
->parents
))
1620 for (parents
= commit
->parents
, parent_number
= 1;
1622 parents
= parents
->next
, parent_number
++) {
1623 if (exclude_parent
&& parent_number
!= exclude_parent
)
1626 it
= &parents
->item
->object
;
1628 add_rev_cmdline(revs
, it
, arg_
, REV_CMD_PARENTS_ONLY
, flags
);
1629 add_pending_object(revs
, it
, arg
);
1634 void repo_init_revisions(struct repository
*r
,
1635 struct rev_info
*revs
,
1638 memset(revs
, 0, sizeof(*revs
));
1641 revs
->abbrev
= DEFAULT_ABBREV
;
1642 revs
->ignore_merges
= 1;
1643 revs
->simplify_history
= 1;
1644 revs
->pruning
.repo
= r
;
1645 revs
->pruning
.flags
.recursive
= 1;
1646 revs
->pruning
.flags
.quick
= 1;
1647 revs
->pruning
.add_remove
= file_add_remove
;
1648 revs
->pruning
.change
= file_change
;
1649 revs
->pruning
.change_fn_data
= revs
;
1650 revs
->sort_order
= REV_SORT_IN_GRAPH_ORDER
;
1652 revs
->prefix
= prefix
;
1655 revs
->skip_count
= -1;
1656 revs
->max_count
= -1;
1657 revs
->max_parents
= -1;
1658 revs
->expand_tabs_in_log
= -1;
1660 revs
->commit_format
= CMIT_FMT_DEFAULT
;
1661 revs
->expand_tabs_in_log_default
= 8;
1663 init_grep_defaults(revs
->repo
);
1664 grep_init(&revs
->grep_filter
, revs
->repo
, prefix
);
1665 revs
->grep_filter
.status_only
= 1;
1667 repo_diff_setup(revs
->repo
, &revs
->diffopt
);
1668 if (prefix
&& !revs
->diffopt
.prefix
) {
1669 revs
->diffopt
.prefix
= prefix
;
1670 revs
->diffopt
.prefix_length
= strlen(prefix
);
1673 revs
->notes_opt
.use_default_notes
= -1;
1676 static void add_pending_commit_list(struct rev_info
*revs
,
1677 struct commit_list
*commit_list
,
1680 while (commit_list
) {
1681 struct object
*object
= &commit_list
->item
->object
;
1682 object
->flags
|= flags
;
1683 add_pending_object(revs
, object
, oid_to_hex(&object
->oid
));
1684 commit_list
= commit_list
->next
;
1688 static void prepare_show_merge(struct rev_info
*revs
)
1690 struct commit_list
*bases
;
1691 struct commit
*head
, *other
;
1692 struct object_id oid
;
1693 const char **prune
= NULL
;
1694 int i
, prune_num
= 1; /* counting terminating NULL */
1695 struct index_state
*istate
= revs
->repo
->index
;
1697 if (get_oid("HEAD", &oid
))
1698 die("--merge without HEAD?");
1699 head
= lookup_commit_or_die(&oid
, "HEAD");
1700 if (get_oid("MERGE_HEAD", &oid
))
1701 die("--merge without MERGE_HEAD?");
1702 other
= lookup_commit_or_die(&oid
, "MERGE_HEAD");
1703 add_pending_object(revs
, &head
->object
, "HEAD");
1704 add_pending_object(revs
, &other
->object
, "MERGE_HEAD");
1705 bases
= get_merge_bases(head
, other
);
1706 add_rev_cmdline_list(revs
, bases
, REV_CMD_MERGE_BASE
, UNINTERESTING
| BOTTOM
);
1707 add_pending_commit_list(revs
, bases
, UNINTERESTING
| BOTTOM
);
1708 free_commit_list(bases
);
1709 head
->object
.flags
|= SYMMETRIC_LEFT
;
1711 if (!istate
->cache_nr
)
1712 repo_read_index(revs
->repo
);
1713 for (i
= 0; i
< istate
->cache_nr
; i
++) {
1714 const struct cache_entry
*ce
= istate
->cache
[i
];
1717 if (ce_path_match(istate
, ce
, &revs
->prune_data
, NULL
)) {
1719 REALLOC_ARRAY(prune
, prune_num
);
1720 prune
[prune_num
-2] = ce
->name
;
1721 prune
[prune_num
-1] = NULL
;
1723 while ((i
+1 < istate
->cache_nr
) &&
1724 ce_same_name(ce
, istate
->cache
[i
+1]))
1727 clear_pathspec(&revs
->prune_data
);
1728 parse_pathspec(&revs
->prune_data
, PATHSPEC_ALL_MAGIC
& ~PATHSPEC_LITERAL
,
1729 PATHSPEC_PREFER_FULL
| PATHSPEC_LITERAL_PATH
, "", prune
);
1733 static int dotdot_missing(const char *arg
, char *dotdot
,
1734 struct rev_info
*revs
, int symmetric
)
1736 if (revs
->ignore_missing
)
1738 /* de-munge so we report the full argument */
1741 ? "Invalid symmetric difference expression %s"
1742 : "Invalid revision range %s", arg
);
1745 static int handle_dotdot_1(const char *arg
, char *dotdot
,
1746 struct rev_info
*revs
, int flags
,
1747 int cant_be_filename
,
1748 struct object_context
*a_oc
,
1749 struct object_context
*b_oc
)
1751 const char *a_name
, *b_name
;
1752 struct object_id a_oid
, b_oid
;
1753 struct object
*a_obj
, *b_obj
;
1754 unsigned int a_flags
, b_flags
;
1756 unsigned int flags_exclude
= flags
^ (UNINTERESTING
| BOTTOM
);
1757 unsigned int oc_flags
= GET_OID_COMMITTISH
| GET_OID_RECORD_PATH
;
1763 b_name
= dotdot
+ 2;
1764 if (*b_name
== '.') {
1771 if (get_oid_with_context(revs
->repo
, a_name
, oc_flags
, &a_oid
, a_oc
) ||
1772 get_oid_with_context(revs
->repo
, b_name
, oc_flags
, &b_oid
, b_oc
))
1775 if (!cant_be_filename
) {
1777 verify_non_filename(revs
->prefix
, arg
);
1781 a_obj
= parse_object(revs
->repo
, &a_oid
);
1782 b_obj
= parse_object(revs
->repo
, &b_oid
);
1783 if (!a_obj
|| !b_obj
)
1784 return dotdot_missing(arg
, dotdot
, revs
, symmetric
);
1789 a_flags
= flags_exclude
;
1791 /* A...B -- find merge bases between the two */
1792 struct commit
*a
, *b
;
1793 struct commit_list
*exclude
;
1795 a
= lookup_commit_reference(revs
->repo
, &a_obj
->oid
);
1796 b
= lookup_commit_reference(revs
->repo
, &b_obj
->oid
);
1798 return dotdot_missing(arg
, dotdot
, revs
, symmetric
);
1800 exclude
= get_merge_bases(a
, b
);
1801 add_rev_cmdline_list(revs
, exclude
, REV_CMD_MERGE_BASE
,
1803 add_pending_commit_list(revs
, exclude
, flags_exclude
);
1804 free_commit_list(exclude
);
1807 a_flags
= flags
| SYMMETRIC_LEFT
;
1810 a_obj
->flags
|= a_flags
;
1811 b_obj
->flags
|= b_flags
;
1812 add_rev_cmdline(revs
, a_obj
, a_name
, REV_CMD_LEFT
, a_flags
);
1813 add_rev_cmdline(revs
, b_obj
, b_name
, REV_CMD_RIGHT
, b_flags
);
1814 add_pending_object_with_path(revs
, a_obj
, a_name
, a_oc
->mode
, a_oc
->path
);
1815 add_pending_object_with_path(revs
, b_obj
, b_name
, b_oc
->mode
, b_oc
->path
);
1819 static int handle_dotdot(const char *arg
,
1820 struct rev_info
*revs
, int flags
,
1821 int cant_be_filename
)
1823 struct object_context a_oc
, b_oc
;
1824 char *dotdot
= strstr(arg
, "..");
1830 memset(&a_oc
, 0, sizeof(a_oc
));
1831 memset(&b_oc
, 0, sizeof(b_oc
));
1834 ret
= handle_dotdot_1(arg
, dotdot
, revs
, flags
, cant_be_filename
,
1844 int handle_revision_arg(const char *arg_
, struct rev_info
*revs
, int flags
, unsigned revarg_opt
)
1846 struct object_context oc
;
1848 struct object
*object
;
1849 struct object_id oid
;
1851 const char *arg
= arg_
;
1852 int cant_be_filename
= revarg_opt
& REVARG_CANNOT_BE_FILENAME
;
1853 unsigned get_sha1_flags
= GET_OID_RECORD_PATH
;
1855 flags
= flags
& UNINTERESTING
? flags
| BOTTOM
: flags
& ~BOTTOM
;
1857 if (!cant_be_filename
&& !strcmp(arg
, "..")) {
1859 * Just ".."? That is not a range but the
1860 * pathspec for the parent directory.
1865 if (!handle_dotdot(arg
, revs
, flags
, revarg_opt
))
1868 mark
= strstr(arg
, "^@");
1869 if (mark
&& !mark
[2]) {
1871 if (add_parents_only(revs
, arg
, flags
, 0))
1875 mark
= strstr(arg
, "^!");
1876 if (mark
&& !mark
[2]) {
1878 if (!add_parents_only(revs
, arg
, flags
^ (UNINTERESTING
| BOTTOM
), 0))
1881 mark
= strstr(arg
, "^-");
1883 int exclude_parent
= 1;
1887 exclude_parent
= strtoul(mark
+ 2, &end
, 10);
1888 if (*end
!= '\0' || !exclude_parent
)
1893 if (!add_parents_only(revs
, arg
, flags
^ (UNINTERESTING
| BOTTOM
), exclude_parent
))
1899 local_flags
= UNINTERESTING
| BOTTOM
;
1903 if (revarg_opt
& REVARG_COMMITTISH
)
1904 get_sha1_flags
|= GET_OID_COMMITTISH
;
1906 if (get_oid_with_context(revs
->repo
, arg
, get_sha1_flags
, &oid
, &oc
))
1907 return revs
->ignore_missing
? 0 : -1;
1908 if (!cant_be_filename
)
1909 verify_non_filename(revs
->prefix
, arg
);
1910 object
= get_reference(revs
, arg
, &oid
, flags
^ local_flags
);
1912 return revs
->ignore_missing
? 0 : -1;
1913 add_rev_cmdline(revs
, object
, arg_
, REV_CMD_REV
, flags
^ local_flags
);
1914 add_pending_object_with_path(revs
, object
, arg
, oc
.mode
, oc
.path
);
1919 static void read_pathspec_from_stdin(struct strbuf
*sb
,
1920 struct argv_array
*prune
)
1922 while (strbuf_getline(sb
, stdin
) != EOF
)
1923 argv_array_push(prune
, sb
->buf
);
1926 static void read_revisions_from_stdin(struct rev_info
*revs
,
1927 struct argv_array
*prune
)
1930 int seen_dashdash
= 0;
1933 save_warning
= warn_on_object_refname_ambiguity
;
1934 warn_on_object_refname_ambiguity
= 0;
1936 strbuf_init(&sb
, 1000);
1937 while (strbuf_getline(&sb
, stdin
) != EOF
) {
1941 if (sb
.buf
[0] == '-') {
1942 if (len
== 2 && sb
.buf
[1] == '-') {
1946 die("options not supported in --stdin mode");
1948 if (handle_revision_arg(sb
.buf
, revs
, 0,
1949 REVARG_CANNOT_BE_FILENAME
))
1950 die("bad revision '%s'", sb
.buf
);
1953 read_pathspec_from_stdin(&sb
, prune
);
1955 strbuf_release(&sb
);
1956 warn_on_object_refname_ambiguity
= save_warning
;
1959 static void add_grep(struct rev_info
*revs
, const char *ptn
, enum grep_pat_token what
)
1961 append_grep_pattern(&revs
->grep_filter
, ptn
, "command line", 0, what
);
1964 static void add_header_grep(struct rev_info
*revs
, enum grep_header_field field
, const char *pattern
)
1966 append_header_grep_pattern(&revs
->grep_filter
, field
, pattern
);
1969 static void add_message_grep(struct rev_info
*revs
, const char *pattern
)
1971 add_grep(revs
, pattern
, GREP_PATTERN_BODY
);
1974 static int handle_revision_opt(struct rev_info
*revs
, int argc
, const char **argv
,
1975 int *unkc
, const char **unkv
,
1976 const struct setup_revision_opt
* opt
)
1978 const char *arg
= argv
[0];
1981 const unsigned hexsz
= the_hash_algo
->hexsz
;
1983 /* pseudo revision arguments */
1984 if (!strcmp(arg
, "--all") || !strcmp(arg
, "--branches") ||
1985 !strcmp(arg
, "--tags") || !strcmp(arg
, "--remotes") ||
1986 !strcmp(arg
, "--reflog") || !strcmp(arg
, "--not") ||
1987 !strcmp(arg
, "--no-walk") || !strcmp(arg
, "--do-walk") ||
1988 !strcmp(arg
, "--bisect") || starts_with(arg
, "--glob=") ||
1989 !strcmp(arg
, "--indexed-objects") ||
1990 !strcmp(arg
, "--alternate-refs") ||
1991 starts_with(arg
, "--exclude=") ||
1992 starts_with(arg
, "--branches=") || starts_with(arg
, "--tags=") ||
1993 starts_with(arg
, "--remotes=") || starts_with(arg
, "--no-walk="))
1995 unkv
[(*unkc
)++] = arg
;
1999 if ((argcount
= parse_long_opt("max-count", argv
, &optarg
))) {
2000 revs
->max_count
= atoi(optarg
);
2003 } else if ((argcount
= parse_long_opt("skip", argv
, &optarg
))) {
2004 revs
->skip_count
= atoi(optarg
);
2006 } else if ((*arg
== '-') && isdigit(arg
[1])) {
2007 /* accept -<digit>, like traditional "head" */
2008 if (strtol_i(arg
+ 1, 10, &revs
->max_count
) < 0 ||
2009 revs
->max_count
< 0)
2010 die("'%s': not a non-negative integer", arg
+ 1);
2012 } else if (!strcmp(arg
, "-n")) {
2014 return error("-n requires an argument");
2015 revs
->max_count
= atoi(argv
[1]);
2018 } else if (skip_prefix(arg
, "-n", &optarg
)) {
2019 revs
->max_count
= atoi(optarg
);
2021 } else if ((argcount
= parse_long_opt("max-age", argv
, &optarg
))) {
2022 revs
->max_age
= atoi(optarg
);
2024 } else if ((argcount
= parse_long_opt("since", argv
, &optarg
))) {
2025 revs
->max_age
= approxidate(optarg
);
2027 } else if ((argcount
= parse_long_opt("after", argv
, &optarg
))) {
2028 revs
->max_age
= approxidate(optarg
);
2030 } else if ((argcount
= parse_long_opt("min-age", argv
, &optarg
))) {
2031 revs
->min_age
= atoi(optarg
);
2033 } else if ((argcount
= parse_long_opt("before", argv
, &optarg
))) {
2034 revs
->min_age
= approxidate(optarg
);
2036 } else if ((argcount
= parse_long_opt("until", argv
, &optarg
))) {
2037 revs
->min_age
= approxidate(optarg
);
2039 } else if (!strcmp(arg
, "--first-parent")) {
2040 revs
->first_parent_only
= 1;
2041 } else if (!strcmp(arg
, "--ancestry-path")) {
2042 revs
->ancestry_path
= 1;
2043 revs
->simplify_history
= 0;
2045 } else if (!strcmp(arg
, "-g") || !strcmp(arg
, "--walk-reflogs")) {
2046 init_reflog_walk(&revs
->reflog_info
);
2047 } else if (!strcmp(arg
, "--default")) {
2049 return error("bad --default argument");
2050 revs
->def
= argv
[1];
2052 } else if (!strcmp(arg
, "--merge")) {
2053 revs
->show_merge
= 1;
2054 } else if (!strcmp(arg
, "--topo-order")) {
2055 revs
->sort_order
= REV_SORT_IN_GRAPH_ORDER
;
2056 revs
->topo_order
= 1;
2057 } else if (!strcmp(arg
, "--simplify-merges")) {
2058 revs
->simplify_merges
= 1;
2059 revs
->topo_order
= 1;
2060 revs
->rewrite_parents
= 1;
2061 revs
->simplify_history
= 0;
2063 } else if (!strcmp(arg
, "--simplify-by-decoration")) {
2064 revs
->simplify_merges
= 1;
2065 revs
->topo_order
= 1;
2066 revs
->rewrite_parents
= 1;
2067 revs
->simplify_history
= 0;
2068 revs
->simplify_by_decoration
= 1;
2071 load_ref_decorations(NULL
, DECORATE_SHORT_REFS
);
2072 } else if (!strcmp(arg
, "--date-order")) {
2073 revs
->sort_order
= REV_SORT_BY_COMMIT_DATE
;
2074 revs
->topo_order
= 1;
2075 } else if (!strcmp(arg
, "--author-date-order")) {
2076 revs
->sort_order
= REV_SORT_BY_AUTHOR_DATE
;
2077 revs
->topo_order
= 1;
2078 } else if (!strcmp(arg
, "--early-output")) {
2079 revs
->early_output
= 100;
2080 revs
->topo_order
= 1;
2081 } else if (skip_prefix(arg
, "--early-output=", &optarg
)) {
2082 if (strtoul_ui(optarg
, 10, &revs
->early_output
) < 0)
2083 die("'%s': not a non-negative integer", optarg
);
2084 revs
->topo_order
= 1;
2085 } else if (!strcmp(arg
, "--parents")) {
2086 revs
->rewrite_parents
= 1;
2087 revs
->print_parents
= 1;
2088 } else if (!strcmp(arg
, "--dense")) {
2090 } else if (!strcmp(arg
, "--sparse")) {
2092 } else if (!strcmp(arg
, "--in-commit-order")) {
2093 revs
->tree_blobs_in_commit_order
= 1;
2094 } else if (!strcmp(arg
, "--remove-empty")) {
2095 revs
->remove_empty_trees
= 1;
2096 } else if (!strcmp(arg
, "--merges")) {
2097 revs
->min_parents
= 2;
2098 } else if (!strcmp(arg
, "--no-merges")) {
2099 revs
->max_parents
= 1;
2100 } else if (skip_prefix(arg
, "--min-parents=", &optarg
)) {
2101 revs
->min_parents
= atoi(optarg
);
2102 } else if (!strcmp(arg
, "--no-min-parents")) {
2103 revs
->min_parents
= 0;
2104 } else if (skip_prefix(arg
, "--max-parents=", &optarg
)) {
2105 revs
->max_parents
= atoi(optarg
);
2106 } else if (!strcmp(arg
, "--no-max-parents")) {
2107 revs
->max_parents
= -1;
2108 } else if (!strcmp(arg
, "--boundary")) {
2110 } else if (!strcmp(arg
, "--left-right")) {
2111 revs
->left_right
= 1;
2112 } else if (!strcmp(arg
, "--left-only")) {
2113 if (revs
->right_only
)
2114 die("--left-only is incompatible with --right-only"
2116 revs
->left_only
= 1;
2117 } else if (!strcmp(arg
, "--right-only")) {
2118 if (revs
->left_only
)
2119 die("--right-only is incompatible with --left-only");
2120 revs
->right_only
= 1;
2121 } else if (!strcmp(arg
, "--cherry")) {
2122 if (revs
->left_only
)
2123 die("--cherry is incompatible with --left-only");
2124 revs
->cherry_mark
= 1;
2125 revs
->right_only
= 1;
2126 revs
->max_parents
= 1;
2128 } else if (!strcmp(arg
, "--count")) {
2130 } else if (!strcmp(arg
, "--cherry-mark")) {
2131 if (revs
->cherry_pick
)
2132 die("--cherry-mark is incompatible with --cherry-pick");
2133 revs
->cherry_mark
= 1;
2134 revs
->limited
= 1; /* needs limit_list() */
2135 } else if (!strcmp(arg
, "--cherry-pick")) {
2136 if (revs
->cherry_mark
)
2137 die("--cherry-pick is incompatible with --cherry-mark");
2138 revs
->cherry_pick
= 1;
2140 } else if (!strcmp(arg
, "--objects")) {
2141 revs
->tag_objects
= 1;
2142 revs
->tree_objects
= 1;
2143 revs
->blob_objects
= 1;
2144 } else if (!strcmp(arg
, "--objects-edge")) {
2145 revs
->tag_objects
= 1;
2146 revs
->tree_objects
= 1;
2147 revs
->blob_objects
= 1;
2148 revs
->edge_hint
= 1;
2149 } else if (!strcmp(arg
, "--objects-edge-aggressive")) {
2150 revs
->tag_objects
= 1;
2151 revs
->tree_objects
= 1;
2152 revs
->blob_objects
= 1;
2153 revs
->edge_hint
= 1;
2154 revs
->edge_hint_aggressive
= 1;
2155 } else if (!strcmp(arg
, "--verify-objects")) {
2156 revs
->tag_objects
= 1;
2157 revs
->tree_objects
= 1;
2158 revs
->blob_objects
= 1;
2159 revs
->verify_objects
= 1;
2160 } else if (!strcmp(arg
, "--unpacked")) {
2162 } else if (starts_with(arg
, "--unpacked=")) {
2163 die("--unpacked=<packfile> no longer supported.");
2164 } else if (!strcmp(arg
, "-r")) {
2166 revs
->diffopt
.flags
.recursive
= 1;
2167 } else if (!strcmp(arg
, "-t")) {
2169 revs
->diffopt
.flags
.recursive
= 1;
2170 revs
->diffopt
.flags
.tree_in_recursive
= 1;
2171 } else if (!strcmp(arg
, "-m")) {
2172 revs
->ignore_merges
= 0;
2173 } else if (!strcmp(arg
, "-c")) {
2175 revs
->dense_combined_merges
= 0;
2176 revs
->combine_merges
= 1;
2177 } else if (!strcmp(arg
, "--combined-all-paths")) {
2179 revs
->combined_all_paths
= 1;
2180 } else if (!strcmp(arg
, "--cc")) {
2182 revs
->dense_combined_merges
= 1;
2183 revs
->combine_merges
= 1;
2184 } else if (!strcmp(arg
, "-v")) {
2185 revs
->verbose_header
= 1;
2186 } else if (!strcmp(arg
, "--pretty")) {
2187 revs
->verbose_header
= 1;
2188 revs
->pretty_given
= 1;
2189 get_commit_format(NULL
, revs
);
2190 } else if (skip_prefix(arg
, "--pretty=", &optarg
) ||
2191 skip_prefix(arg
, "--format=", &optarg
)) {
2193 * Detached form ("--pretty X" as opposed to "--pretty=X")
2194 * not allowed, since the argument is optional.
2196 revs
->verbose_header
= 1;
2197 revs
->pretty_given
= 1;
2198 get_commit_format(optarg
, revs
);
2199 } else if (!strcmp(arg
, "--expand-tabs")) {
2200 revs
->expand_tabs_in_log
= 8;
2201 } else if (!strcmp(arg
, "--no-expand-tabs")) {
2202 revs
->expand_tabs_in_log
= 0;
2203 } else if (skip_prefix(arg
, "--expand-tabs=", &arg
)) {
2205 if (strtol_i(arg
, 10, &val
) < 0 || val
< 0)
2206 die("'%s': not a non-negative integer", arg
);
2207 revs
->expand_tabs_in_log
= val
;
2208 } else if (!strcmp(arg
, "--show-notes") || !strcmp(arg
, "--notes")) {
2209 revs
->show_notes
= 1;
2210 revs
->show_notes_given
= 1;
2211 revs
->notes_opt
.use_default_notes
= 1;
2212 } else if (!strcmp(arg
, "--show-signature")) {
2213 revs
->show_signature
= 1;
2214 } else if (!strcmp(arg
, "--no-show-signature")) {
2215 revs
->show_signature
= 0;
2216 } else if (!strcmp(arg
, "--show-linear-break")) {
2217 revs
->break_bar
= " ..........";
2218 revs
->track_linear
= 1;
2219 revs
->track_first_time
= 1;
2220 } else if (skip_prefix(arg
, "--show-linear-break=", &optarg
)) {
2221 revs
->break_bar
= xstrdup(optarg
);
2222 revs
->track_linear
= 1;
2223 revs
->track_first_time
= 1;
2224 } else if (skip_prefix(arg
, "--show-notes=", &optarg
) ||
2225 skip_prefix(arg
, "--notes=", &optarg
)) {
2226 struct strbuf buf
= STRBUF_INIT
;
2227 revs
->show_notes
= 1;
2228 revs
->show_notes_given
= 1;
2229 if (starts_with(arg
, "--show-notes=") &&
2230 revs
->notes_opt
.use_default_notes
< 0)
2231 revs
->notes_opt
.use_default_notes
= 1;
2232 strbuf_addstr(&buf
, optarg
);
2233 expand_notes_ref(&buf
);
2234 string_list_append(&revs
->notes_opt
.extra_notes_refs
,
2235 strbuf_detach(&buf
, NULL
));
2236 } else if (!strcmp(arg
, "--no-notes")) {
2237 revs
->show_notes
= 0;
2238 revs
->show_notes_given
= 1;
2239 revs
->notes_opt
.use_default_notes
= -1;
2240 /* we have been strdup'ing ourselves, so trick
2241 * string_list into free()ing strings */
2242 revs
->notes_opt
.extra_notes_refs
.strdup_strings
= 1;
2243 string_list_clear(&revs
->notes_opt
.extra_notes_refs
, 0);
2244 revs
->notes_opt
.extra_notes_refs
.strdup_strings
= 0;
2245 } else if (!strcmp(arg
, "--standard-notes")) {
2246 revs
->show_notes_given
= 1;
2247 revs
->notes_opt
.use_default_notes
= 1;
2248 } else if (!strcmp(arg
, "--no-standard-notes")) {
2249 revs
->notes_opt
.use_default_notes
= 0;
2250 } else if (!strcmp(arg
, "--oneline")) {
2251 revs
->verbose_header
= 1;
2252 get_commit_format("oneline", revs
);
2253 revs
->pretty_given
= 1;
2254 revs
->abbrev_commit
= 1;
2255 } else if (!strcmp(arg
, "--graph")) {
2256 revs
->topo_order
= 1;
2257 revs
->rewrite_parents
= 1;
2258 revs
->graph
= graph_init(revs
);
2259 } else if (!strcmp(arg
, "--root")) {
2260 revs
->show_root_diff
= 1;
2261 } else if (!strcmp(arg
, "--no-commit-id")) {
2262 revs
->no_commit_id
= 1;
2263 } else if (!strcmp(arg
, "--always")) {
2264 revs
->always_show_header
= 1;
2265 } else if (!strcmp(arg
, "--no-abbrev")) {
2267 } else if (!strcmp(arg
, "--abbrev")) {
2268 revs
->abbrev
= DEFAULT_ABBREV
;
2269 } else if (skip_prefix(arg
, "--abbrev=", &optarg
)) {
2270 revs
->abbrev
= strtoul(optarg
, NULL
, 10);
2271 if (revs
->abbrev
< MINIMUM_ABBREV
)
2272 revs
->abbrev
= MINIMUM_ABBREV
;
2273 else if (revs
->abbrev
> hexsz
)
2274 revs
->abbrev
= hexsz
;
2275 } else if (!strcmp(arg
, "--abbrev-commit")) {
2276 revs
->abbrev_commit
= 1;
2277 revs
->abbrev_commit_given
= 1;
2278 } else if (!strcmp(arg
, "--no-abbrev-commit")) {
2279 revs
->abbrev_commit
= 0;
2280 } else if (!strcmp(arg
, "--full-diff")) {
2282 revs
->full_diff
= 1;
2283 } else if (!strcmp(arg
, "--full-history")) {
2284 revs
->simplify_history
= 0;
2285 } else if (!strcmp(arg
, "--relative-date")) {
2286 revs
->date_mode
.type
= DATE_RELATIVE
;
2287 revs
->date_mode_explicit
= 1;
2288 } else if ((argcount
= parse_long_opt("date", argv
, &optarg
))) {
2289 parse_date_format(optarg
, &revs
->date_mode
);
2290 revs
->date_mode_explicit
= 1;
2292 } else if (!strcmp(arg
, "--log-size")) {
2293 revs
->show_log_size
= 1;
2296 * Grepping the commit log
2298 else if ((argcount
= parse_long_opt("author", argv
, &optarg
))) {
2299 add_header_grep(revs
, GREP_HEADER_AUTHOR
, optarg
);
2301 } else if ((argcount
= parse_long_opt("committer", argv
, &optarg
))) {
2302 add_header_grep(revs
, GREP_HEADER_COMMITTER
, optarg
);
2304 } else if ((argcount
= parse_long_opt("grep-reflog", argv
, &optarg
))) {
2305 add_header_grep(revs
, GREP_HEADER_REFLOG
, optarg
);
2307 } else if ((argcount
= parse_long_opt("grep", argv
, &optarg
))) {
2308 add_message_grep(revs
, optarg
);
2310 } else if (!strcmp(arg
, "--grep-debug")) {
2311 revs
->grep_filter
.debug
= 1;
2312 } else if (!strcmp(arg
, "--basic-regexp")) {
2313 revs
->grep_filter
.pattern_type_option
= GREP_PATTERN_TYPE_BRE
;
2314 } else if (!strcmp(arg
, "--extended-regexp") || !strcmp(arg
, "-E")) {
2315 revs
->grep_filter
.pattern_type_option
= GREP_PATTERN_TYPE_ERE
;
2316 } else if (!strcmp(arg
, "--regexp-ignore-case") || !strcmp(arg
, "-i")) {
2317 revs
->grep_filter
.ignore_case
= 1;
2318 revs
->diffopt
.pickaxe_opts
|= DIFF_PICKAXE_IGNORE_CASE
;
2319 } else if (!strcmp(arg
, "--fixed-strings") || !strcmp(arg
, "-F")) {
2320 revs
->grep_filter
.pattern_type_option
= GREP_PATTERN_TYPE_FIXED
;
2321 } else if (!strcmp(arg
, "--perl-regexp") || !strcmp(arg
, "-P")) {
2322 revs
->grep_filter
.pattern_type_option
= GREP_PATTERN_TYPE_PCRE
;
2323 } else if (!strcmp(arg
, "--all-match")) {
2324 revs
->grep_filter
.all_match
= 1;
2325 } else if (!strcmp(arg
, "--invert-grep")) {
2326 revs
->invert_grep
= 1;
2327 } else if ((argcount
= parse_long_opt("encoding", argv
, &optarg
))) {
2328 if (strcmp(optarg
, "none"))
2329 git_log_output_encoding
= xstrdup(optarg
);
2331 git_log_output_encoding
= "";
2333 } else if (!strcmp(arg
, "--reverse")) {
2335 } else if (!strcmp(arg
, "--children")) {
2336 revs
->children
.name
= "children";
2338 } else if (!strcmp(arg
, "--ignore-missing")) {
2339 revs
->ignore_missing
= 1;
2340 } else if (opt
&& opt
->allow_exclude_promisor_objects
&&
2341 !strcmp(arg
, "--exclude-promisor-objects")) {
2342 if (fetch_if_missing
)
2343 BUG("exclude_promisor_objects can only be used when fetch_if_missing is 0");
2344 revs
->exclude_promisor_objects
= 1;
2346 int opts
= diff_opt_parse(&revs
->diffopt
, argv
, argc
, revs
->prefix
);
2348 unkv
[(*unkc
)++] = arg
;
2351 if (revs
->graph
&& revs
->track_linear
)
2352 die("--show-linear-break and --graph are incompatible");
2357 void parse_revision_opt(struct rev_info
*revs
, struct parse_opt_ctx_t
*ctx
,
2358 const struct option
*options
,
2359 const char * const usagestr
[])
2361 int n
= handle_revision_opt(revs
, ctx
->argc
, ctx
->argv
,
2362 &ctx
->cpidx
, ctx
->out
, NULL
);
2364 error("unknown option `%s'", ctx
->argv
[0]);
2365 usage_with_options(usagestr
, options
);
2371 static int for_each_bisect_ref(struct ref_store
*refs
, each_ref_fn fn
,
2372 void *cb_data
, const char *term
)
2374 struct strbuf bisect_refs
= STRBUF_INIT
;
2376 strbuf_addf(&bisect_refs
, "refs/bisect/%s", term
);
2377 status
= refs_for_each_fullref_in(refs
, bisect_refs
.buf
, fn
, cb_data
, 0);
2378 strbuf_release(&bisect_refs
);
2382 static int for_each_bad_bisect_ref(struct ref_store
*refs
, each_ref_fn fn
, void *cb_data
)
2384 return for_each_bisect_ref(refs
, fn
, cb_data
, term_bad
);
2387 static int for_each_good_bisect_ref(struct ref_store
*refs
, each_ref_fn fn
, void *cb_data
)
2389 return for_each_bisect_ref(refs
, fn
, cb_data
, term_good
);
2392 static int handle_revision_pseudo_opt(const char *submodule
,
2393 struct rev_info
*revs
,
2394 int argc
, const char **argv
, int *flags
)
2396 const char *arg
= argv
[0];
2398 struct ref_store
*refs
;
2403 * We need some something like get_submodule_worktrees()
2404 * before we can go through all worktrees of a submodule,
2405 * .e.g with adding all HEADs from --all, which is not
2406 * supported right now, so stick to single worktree.
2408 if (!revs
->single_worktree
)
2409 BUG("--single-worktree cannot be used together with submodule");
2410 refs
= get_submodule_ref_store(submodule
);
2412 refs
= get_main_ref_store(revs
->repo
);
2417 * Commands like "git shortlog" will not accept the options below
2418 * unless parse_revision_opt queues them (as opposed to erroring
2421 * When implementing your new pseudo-option, remember to
2422 * register it in the list at the top of handle_revision_opt.
2424 if (!strcmp(arg
, "--all")) {
2425 handle_refs(refs
, revs
, *flags
, refs_for_each_ref
);
2426 handle_refs(refs
, revs
, *flags
, refs_head_ref
);
2427 if (!revs
->single_worktree
) {
2428 struct all_refs_cb cb
;
2430 init_all_refs_cb(&cb
, revs
, *flags
);
2431 other_head_refs(handle_one_ref
, &cb
);
2433 clear_ref_exclusion(&revs
->ref_excludes
);
2434 } else if (!strcmp(arg
, "--branches")) {
2435 handle_refs(refs
, revs
, *flags
, refs_for_each_branch_ref
);
2436 clear_ref_exclusion(&revs
->ref_excludes
);
2437 } else if (!strcmp(arg
, "--bisect")) {
2438 read_bisect_terms(&term_bad
, &term_good
);
2439 handle_refs(refs
, revs
, *flags
, for_each_bad_bisect_ref
);
2440 handle_refs(refs
, revs
, *flags
^ (UNINTERESTING
| BOTTOM
),
2441 for_each_good_bisect_ref
);
2443 } else if (!strcmp(arg
, "--tags")) {
2444 handle_refs(refs
, revs
, *flags
, refs_for_each_tag_ref
);
2445 clear_ref_exclusion(&revs
->ref_excludes
);
2446 } else if (!strcmp(arg
, "--remotes")) {
2447 handle_refs(refs
, revs
, *flags
, refs_for_each_remote_ref
);
2448 clear_ref_exclusion(&revs
->ref_excludes
);
2449 } else if ((argcount
= parse_long_opt("glob", argv
, &optarg
))) {
2450 struct all_refs_cb cb
;
2451 init_all_refs_cb(&cb
, revs
, *flags
);
2452 for_each_glob_ref(handle_one_ref
, optarg
, &cb
);
2453 clear_ref_exclusion(&revs
->ref_excludes
);
2455 } else if ((argcount
= parse_long_opt("exclude", argv
, &optarg
))) {
2456 add_ref_exclusion(&revs
->ref_excludes
, optarg
);
2458 } else if (skip_prefix(arg
, "--branches=", &optarg
)) {
2459 struct all_refs_cb cb
;
2460 init_all_refs_cb(&cb
, revs
, *flags
);
2461 for_each_glob_ref_in(handle_one_ref
, optarg
, "refs/heads/", &cb
);
2462 clear_ref_exclusion(&revs
->ref_excludes
);
2463 } else if (skip_prefix(arg
, "--tags=", &optarg
)) {
2464 struct all_refs_cb cb
;
2465 init_all_refs_cb(&cb
, revs
, *flags
);
2466 for_each_glob_ref_in(handle_one_ref
, optarg
, "refs/tags/", &cb
);
2467 clear_ref_exclusion(&revs
->ref_excludes
);
2468 } else if (skip_prefix(arg
, "--remotes=", &optarg
)) {
2469 struct all_refs_cb cb
;
2470 init_all_refs_cb(&cb
, revs
, *flags
);
2471 for_each_glob_ref_in(handle_one_ref
, optarg
, "refs/remotes/", &cb
);
2472 clear_ref_exclusion(&revs
->ref_excludes
);
2473 } else if (!strcmp(arg
, "--reflog")) {
2474 add_reflogs_to_pending(revs
, *flags
);
2475 } else if (!strcmp(arg
, "--indexed-objects")) {
2476 add_index_objects_to_pending(revs
, *flags
);
2477 } else if (!strcmp(arg
, "--alternate-refs")) {
2478 add_alternate_refs_to_pending(revs
, *flags
);
2479 } else if (!strcmp(arg
, "--not")) {
2480 *flags
^= UNINTERESTING
| BOTTOM
;
2481 } else if (!strcmp(arg
, "--no-walk")) {
2482 revs
->no_walk
= REVISION_WALK_NO_WALK_SORTED
;
2483 } else if (skip_prefix(arg
, "--no-walk=", &optarg
)) {
2485 * Detached form ("--no-walk X" as opposed to "--no-walk=X")
2486 * not allowed, since the argument is optional.
2488 if (!strcmp(optarg
, "sorted"))
2489 revs
->no_walk
= REVISION_WALK_NO_WALK_SORTED
;
2490 else if (!strcmp(optarg
, "unsorted"))
2491 revs
->no_walk
= REVISION_WALK_NO_WALK_UNSORTED
;
2493 return error("invalid argument to --no-walk");
2494 } else if (!strcmp(arg
, "--do-walk")) {
2496 } else if (!strcmp(arg
, "--single-worktree")) {
2497 revs
->single_worktree
= 1;
2505 static void NORETURN
diagnose_missing_default(const char *def
)
2508 const char *refname
;
2510 refname
= resolve_ref_unsafe(def
, 0, NULL
, &flags
);
2511 if (!refname
|| !(flags
& REF_ISSYMREF
) || (flags
& REF_ISBROKEN
))
2512 die(_("your current branch appears to be broken"));
2514 skip_prefix(refname
, "refs/heads/", &refname
);
2515 die(_("your current branch '%s' does not have any commits yet"),
2520 * Parse revision information, filling in the "rev_info" structure,
2521 * and removing the used arguments from the argument list.
2523 * Returns the number of arguments left that weren't recognized
2524 * (which are also moved to the head of the argument list)
2526 int setup_revisions(int argc
, const char **argv
, struct rev_info
*revs
, struct setup_revision_opt
*opt
)
2528 int i
, flags
, left
, seen_dashdash
, got_rev_arg
= 0, revarg_opt
;
2529 struct argv_array prune_data
= ARGV_ARRAY_INIT
;
2530 const char *submodule
= NULL
;
2533 submodule
= opt
->submodule
;
2535 /* First, search for "--" */
2536 if (opt
&& opt
->assume_dashdash
) {
2540 for (i
= 1; i
< argc
; i
++) {
2541 const char *arg
= argv
[i
];
2542 if (strcmp(arg
, "--"))
2547 argv_array_pushv(&prune_data
, argv
+ i
+ 1);
2553 /* Second, deal with arguments and options */
2555 revarg_opt
= opt
? opt
->revarg_opt
: 0;
2557 revarg_opt
|= REVARG_CANNOT_BE_FILENAME
;
2558 for (left
= i
= 1; i
< argc
; i
++) {
2559 const char *arg
= argv
[i
];
2563 opts
= handle_revision_pseudo_opt(submodule
,
2564 revs
, argc
- i
, argv
+ i
,
2571 if (!strcmp(arg
, "--stdin")) {
2572 if (revs
->disable_stdin
) {
2576 if (revs
->read_from_stdin
++)
2577 die("--stdin given twice?");
2578 read_revisions_from_stdin(revs
, &prune_data
);
2582 opts
= handle_revision_opt(revs
, argc
- i
, argv
+ i
,
2594 if (handle_revision_arg(arg
, revs
, flags
, revarg_opt
)) {
2596 if (seen_dashdash
|| *arg
== '^')
2597 die("bad revision '%s'", arg
);
2599 /* If we didn't have a "--":
2600 * (1) all filenames must exist;
2601 * (2) all rev-args must not be interpretable
2602 * as a valid filename.
2603 * but the latter we have checked in the main loop.
2605 for (j
= i
; j
< argc
; j
++)
2606 verify_filename(revs
->prefix
, argv
[j
], j
== i
);
2608 argv_array_pushv(&prune_data
, argv
+ i
);
2615 if (prune_data
.argc
) {
2617 * If we need to introduce the magic "a lone ':' means no
2618 * pathspec whatsoever", here is the place to do so.
2620 * if (prune_data.nr == 1 && !strcmp(prune_data[0], ":")) {
2621 * prune_data.nr = 0;
2622 * prune_data.alloc = 0;
2623 * free(prune_data.path);
2624 * prune_data.path = NULL;
2626 * terminate prune_data.alloc with NULL and
2627 * call init_pathspec() to set revs->prune_data here.
2630 parse_pathspec(&revs
->prune_data
, 0, 0,
2631 revs
->prefix
, prune_data
.argv
);
2633 argv_array_clear(&prune_data
);
2635 if (revs
->def
== NULL
)
2636 revs
->def
= opt
? opt
->def
: NULL
;
2637 if (opt
&& opt
->tweak
)
2638 opt
->tweak(revs
, opt
);
2639 if (revs
->show_merge
)
2640 prepare_show_merge(revs
);
2641 if (revs
->def
&& !revs
->pending
.nr
&& !revs
->rev_input_given
&& !got_rev_arg
) {
2642 struct object_id oid
;
2643 struct object
*object
;
2644 struct object_context oc
;
2645 if (get_oid_with_context(revs
->repo
, revs
->def
, 0, &oid
, &oc
))
2646 diagnose_missing_default(revs
->def
);
2647 object
= get_reference(revs
, revs
->def
, &oid
, 0);
2648 add_pending_object_with_mode(revs
, object
, revs
->def
, oc
.mode
);
2651 /* Did the user ask for any diff output? Run the diff! */
2652 if (revs
->diffopt
.output_format
& ~DIFF_FORMAT_NO_OUTPUT
)
2655 /* Pickaxe, diff-filter and rename following need diffs */
2656 if ((revs
->diffopt
.pickaxe_opts
& DIFF_PICKAXE_KINDS_MASK
) ||
2657 revs
->diffopt
.filter
||
2658 revs
->diffopt
.flags
.follow_renames
)
2661 if (revs
->diffopt
.objfind
)
2662 revs
->simplify_history
= 0;
2664 if (revs
->topo_order
&& !generation_numbers_enabled(the_repository
))
2667 if (revs
->prune_data
.nr
) {
2668 copy_pathspec(&revs
->pruning
.pathspec
, &revs
->prune_data
);
2669 /* Can't prune commits with rename following: the paths change.. */
2670 if (!revs
->diffopt
.flags
.follow_renames
)
2672 if (!revs
->full_diff
)
2673 copy_pathspec(&revs
->diffopt
.pathspec
,
2676 if (revs
->combine_merges
)
2677 revs
->ignore_merges
= 0;
2678 if (revs
->combined_all_paths
&& !revs
->combine_merges
)
2679 die("--combined-all-paths makes no sense without -c or --cc");
2681 revs
->diffopt
.abbrev
= revs
->abbrev
;
2683 if (revs
->line_level_traverse
) {
2685 revs
->topo_order
= 1;
2688 diff_setup_done(&revs
->diffopt
);
2690 grep_commit_pattern_type(GREP_PATTERN_TYPE_UNSPECIFIED
,
2691 &revs
->grep_filter
);
2692 compile_grep_patterns(&revs
->grep_filter
);
2694 if (revs
->reverse
&& revs
->reflog_info
)
2695 die("cannot combine --reverse with --walk-reflogs");
2696 if (revs
->reflog_info
&& revs
->limited
)
2697 die("cannot combine --walk-reflogs with history-limiting options");
2698 if (revs
->rewrite_parents
&& revs
->children
.name
)
2699 die("cannot combine --parents and --children");
2702 * Limitations on the graph functionality
2704 if (revs
->reverse
&& revs
->graph
)
2705 die("cannot combine --reverse with --graph");
2707 if (revs
->reflog_info
&& revs
->graph
)
2708 die("cannot combine --walk-reflogs with --graph");
2709 if (revs
->no_walk
&& revs
->graph
)
2710 die("cannot combine --no-walk with --graph");
2711 if (!revs
->reflog_info
&& revs
->grep_filter
.use_reflog_filter
)
2712 die("cannot use --grep-reflog without --walk-reflogs");
2714 if (revs
->first_parent_only
&& revs
->bisect
)
2715 die(_("--first-parent is incompatible with --bisect"));
2717 if (revs
->line_level_traverse
&&
2718 (revs
->diffopt
.output_format
& ~(DIFF_FORMAT_PATCH
| DIFF_FORMAT_NO_OUTPUT
)))
2719 die(_("-L does not yet support diff formats besides -p and -s"));
2721 if (revs
->expand_tabs_in_log
< 0)
2722 revs
->expand_tabs_in_log
= revs
->expand_tabs_in_log_default
;
2727 static void add_child(struct rev_info
*revs
, struct commit
*parent
, struct commit
*child
)
2729 struct commit_list
*l
= xcalloc(1, sizeof(*l
));
2732 l
->next
= add_decoration(&revs
->children
, &parent
->object
, l
);
2735 static int remove_duplicate_parents(struct rev_info
*revs
, struct commit
*commit
)
2737 struct treesame_state
*ts
= lookup_decoration(&revs
->treesame
, &commit
->object
);
2738 struct commit_list
**pp
, *p
;
2739 int surviving_parents
;
2741 /* Examine existing parents while marking ones we have seen... */
2742 pp
= &commit
->parents
;
2743 surviving_parents
= 0;
2744 while ((p
= *pp
) != NULL
) {
2745 struct commit
*parent
= p
->item
;
2746 if (parent
->object
.flags
& TMP_MARK
) {
2749 compact_treesame(revs
, commit
, surviving_parents
);
2752 parent
->object
.flags
|= TMP_MARK
;
2753 surviving_parents
++;
2756 /* clear the temporary mark */
2757 for (p
= commit
->parents
; p
; p
= p
->next
) {
2758 p
->item
->object
.flags
&= ~TMP_MARK
;
2760 /* no update_treesame() - removing duplicates can't affect TREESAME */
2761 return surviving_parents
;
2764 struct merge_simplify_state
{
2765 struct commit
*simplified
;
2768 static struct merge_simplify_state
*locate_simplify_state(struct rev_info
*revs
, struct commit
*commit
)
2770 struct merge_simplify_state
*st
;
2772 st
= lookup_decoration(&revs
->merge_simplification
, &commit
->object
);
2774 st
= xcalloc(1, sizeof(*st
));
2775 add_decoration(&revs
->merge_simplification
, &commit
->object
, st
);
2780 static int mark_redundant_parents(struct commit
*commit
)
2782 struct commit_list
*h
= reduce_heads(commit
->parents
);
2783 int i
= 0, marked
= 0;
2784 struct commit_list
*po
, *pn
;
2786 /* Want these for sanity-checking only */
2787 int orig_cnt
= commit_list_count(commit
->parents
);
2788 int cnt
= commit_list_count(h
);
2791 * Not ready to remove items yet, just mark them for now, based
2792 * on the output of reduce_heads(). reduce_heads outputs the reduced
2793 * set in its original order, so this isn't too hard.
2795 po
= commit
->parents
;
2798 if (pn
&& po
->item
== pn
->item
) {
2802 po
->item
->object
.flags
|= TMP_MARK
;
2808 if (i
!= cnt
|| cnt
+marked
!= orig_cnt
)
2809 die("mark_redundant_parents %d %d %d %d", orig_cnt
, cnt
, i
, marked
);
2811 free_commit_list(h
);
2816 static int mark_treesame_root_parents(struct commit
*commit
)
2818 struct commit_list
*p
;
2821 for (p
= commit
->parents
; p
; p
= p
->next
) {
2822 struct commit
*parent
= p
->item
;
2823 if (!parent
->parents
&& (parent
->object
.flags
& TREESAME
)) {
2824 parent
->object
.flags
|= TMP_MARK
;
2833 * Awkward naming - this means one parent we are TREESAME to.
2834 * cf mark_treesame_root_parents: root parents that are TREESAME (to an
2835 * empty tree). Better name suggestions?
2837 static int leave_one_treesame_to_parent(struct rev_info
*revs
, struct commit
*commit
)
2839 struct treesame_state
*ts
= lookup_decoration(&revs
->treesame
, &commit
->object
);
2840 struct commit
*unmarked
= NULL
, *marked
= NULL
;
2841 struct commit_list
*p
;
2844 for (p
= commit
->parents
, n
= 0; p
; p
= p
->next
, n
++) {
2845 if (ts
->treesame
[n
]) {
2846 if (p
->item
->object
.flags
& TMP_MARK
) {
2859 * If we are TREESAME to a marked-for-deletion parent, but not to any
2860 * unmarked parents, unmark the first TREESAME parent. This is the
2861 * parent that the default simplify_history==1 scan would have followed,
2862 * and it doesn't make sense to omit that path when asking for a
2863 * simplified full history. Retaining it improves the chances of
2864 * understanding odd missed merges that took an old version of a file.
2868 * I--------*X A modified the file, but mainline merge X used
2869 * \ / "-s ours", so took the version from I. X is
2870 * `-*A--' TREESAME to I and !TREESAME to A.
2872 * Default log from X would produce "I". Without this check,
2873 * --full-history --simplify-merges would produce "I-A-X", showing
2874 * the merge commit X and that it changed A, but not making clear that
2875 * it had just taken the I version. With this check, the topology above
2878 * Note that it is possible that the simplification chooses a different
2879 * TREESAME parent from the default, in which case this test doesn't
2880 * activate, and we _do_ drop the default parent. Example:
2882 * I------X A modified the file, but it was reverted in B,
2883 * \ / meaning mainline merge X is TREESAME to both
2886 * Default log would produce "I" by following the first parent;
2887 * --full-history --simplify-merges will produce "I-A-B". But this is a
2888 * reasonable result - it presents a logical full history leading from
2889 * I to X, and X is not an important merge.
2891 if (!unmarked
&& marked
) {
2892 marked
->object
.flags
&= ~TMP_MARK
;
2899 static int remove_marked_parents(struct rev_info
*revs
, struct commit
*commit
)
2901 struct commit_list
**pp
, *p
;
2902 int nth_parent
, removed
= 0;
2904 pp
= &commit
->parents
;
2906 while ((p
= *pp
) != NULL
) {
2907 struct commit
*parent
= p
->item
;
2908 if (parent
->object
.flags
& TMP_MARK
) {
2909 parent
->object
.flags
&= ~TMP_MARK
;
2913 compact_treesame(revs
, commit
, nth_parent
);
2920 /* Removing parents can only increase TREESAMEness */
2921 if (removed
&& !(commit
->object
.flags
& TREESAME
))
2922 update_treesame(revs
, commit
);
2927 static struct commit_list
**simplify_one(struct rev_info
*revs
, struct commit
*commit
, struct commit_list
**tail
)
2929 struct commit_list
*p
;
2930 struct commit
*parent
;
2931 struct merge_simplify_state
*st
, *pst
;
2934 st
= locate_simplify_state(revs
, commit
);
2937 * Have we handled this one?
2943 * An UNINTERESTING commit simplifies to itself, so does a
2944 * root commit. We do not rewrite parents of such commit
2947 if ((commit
->object
.flags
& UNINTERESTING
) || !commit
->parents
) {
2948 st
->simplified
= commit
;
2953 * Do we know what commit all of our parents that matter
2954 * should be rewritten to? Otherwise we are not ready to
2955 * rewrite this one yet.
2957 for (cnt
= 0, p
= commit
->parents
; p
; p
= p
->next
) {
2958 pst
= locate_simplify_state(revs
, p
->item
);
2959 if (!pst
->simplified
) {
2960 tail
= &commit_list_insert(p
->item
, tail
)->next
;
2963 if (revs
->first_parent_only
)
2967 tail
= &commit_list_insert(commit
, tail
)->next
;
2972 * Rewrite our list of parents. Note that this cannot
2973 * affect our TREESAME flags in any way - a commit is
2974 * always TREESAME to its simplification.
2976 for (p
= commit
->parents
; p
; p
= p
->next
) {
2977 pst
= locate_simplify_state(revs
, p
->item
);
2978 p
->item
= pst
->simplified
;
2979 if (revs
->first_parent_only
)
2983 if (revs
->first_parent_only
)
2986 cnt
= remove_duplicate_parents(revs
, commit
);
2989 * It is possible that we are a merge and one side branch
2990 * does not have any commit that touches the given paths;
2991 * in such a case, the immediate parent from that branch
2992 * will be rewritten to be the merge base.
2994 * o----X X: the commit we are looking at;
2995 * / / o: a commit that touches the paths;
2998 * Further, a merge of an independent branch that doesn't
2999 * touch the path will reduce to a treesame root parent:
3001 * ----o----X X: the commit we are looking at;
3002 * / o: a commit that touches the paths;
3003 * r r: a root commit not touching the paths
3005 * Detect and simplify both cases.
3008 int marked
= mark_redundant_parents(commit
);
3009 marked
+= mark_treesame_root_parents(commit
);
3011 marked
-= leave_one_treesame_to_parent(revs
, commit
);
3013 cnt
= remove_marked_parents(revs
, commit
);
3017 * A commit simplifies to itself if it is a root, if it is
3018 * UNINTERESTING, if it touches the given paths, or if it is a
3019 * merge and its parents don't simplify to one relevant commit
3020 * (the first two cases are already handled at the beginning of
3023 * Otherwise, it simplifies to what its sole relevant parent
3027 (commit
->object
.flags
& UNINTERESTING
) ||
3028 !(commit
->object
.flags
& TREESAME
) ||
3029 (parent
= one_relevant_parent(revs
, commit
->parents
)) == NULL
)
3030 st
->simplified
= commit
;
3032 pst
= locate_simplify_state(revs
, parent
);
3033 st
->simplified
= pst
->simplified
;
3038 static void simplify_merges(struct rev_info
*revs
)
3040 struct commit_list
*list
, *next
;
3041 struct commit_list
*yet_to_do
, **tail
;
3042 struct commit
*commit
;
3047 /* feed the list reversed */
3049 for (list
= revs
->commits
; list
; list
= next
) {
3050 commit
= list
->item
;
3053 * Do not free(list) here yet; the original list
3054 * is used later in this function.
3056 commit_list_insert(commit
, &yet_to_do
);
3063 commit
= pop_commit(&list
);
3064 tail
= simplify_one(revs
, commit
, tail
);
3068 /* clean up the result, removing the simplified ones */
3069 list
= revs
->commits
;
3070 revs
->commits
= NULL
;
3071 tail
= &revs
->commits
;
3073 struct merge_simplify_state
*st
;
3075 commit
= pop_commit(&list
);
3076 st
= locate_simplify_state(revs
, commit
);
3077 if (st
->simplified
== commit
)
3078 tail
= &commit_list_insert(commit
, tail
)->next
;
3082 static void set_children(struct rev_info
*revs
)
3084 struct commit_list
*l
;
3085 for (l
= revs
->commits
; l
; l
= l
->next
) {
3086 struct commit
*commit
= l
->item
;
3087 struct commit_list
*p
;
3089 for (p
= commit
->parents
; p
; p
= p
->next
)
3090 add_child(revs
, p
->item
, commit
);
3094 void reset_revision_walk(void)
3096 clear_object_flags(SEEN
| ADDED
| SHOWN
);
3099 static int mark_uninteresting(const struct object_id
*oid
,
3100 struct packed_git
*pack
,
3104 struct rev_info
*revs
= cb
;
3105 struct object
*o
= parse_object(revs
->repo
, oid
);
3106 o
->flags
|= UNINTERESTING
| SEEN
;
3110 define_commit_slab(indegree_slab
, int);
3111 define_commit_slab(author_date_slab
, timestamp_t
);
3113 struct topo_walk_info
{
3114 uint32_t min_generation
;
3115 struct prio_queue explore_queue
;
3116 struct prio_queue indegree_queue
;
3117 struct prio_queue topo_queue
;
3118 struct indegree_slab indegree
;
3119 struct author_date_slab author_date
;
3122 static inline void test_flag_and_insert(struct prio_queue
*q
, struct commit
*c
, int flag
)
3124 if (c
->object
.flags
& flag
)
3127 c
->object
.flags
|= flag
;
3128 prio_queue_put(q
, c
);
3131 static void explore_walk_step(struct rev_info
*revs
)
3133 struct topo_walk_info
*info
= revs
->topo_walk_info
;
3134 struct commit_list
*p
;
3135 struct commit
*c
= prio_queue_get(&info
->explore_queue
);
3140 if (parse_commit_gently(c
, 1) < 0)
3143 if (revs
->sort_order
== REV_SORT_BY_AUTHOR_DATE
)
3144 record_author_date(&info
->author_date
, c
);
3146 if (revs
->max_age
!= -1 && (c
->date
< revs
->max_age
))
3147 c
->object
.flags
|= UNINTERESTING
;
3149 if (process_parents(revs
, c
, NULL
, NULL
) < 0)
3152 if (c
->object
.flags
& UNINTERESTING
)
3153 mark_parents_uninteresting(c
);
3155 for (p
= c
->parents
; p
; p
= p
->next
)
3156 test_flag_and_insert(&info
->explore_queue
, p
->item
, TOPO_WALK_EXPLORED
);
3159 static void explore_to_depth(struct rev_info
*revs
,
3160 uint32_t gen_cutoff
)
3162 struct topo_walk_info
*info
= revs
->topo_walk_info
;
3164 while ((c
= prio_queue_peek(&info
->explore_queue
)) &&
3165 c
->generation
>= gen_cutoff
)
3166 explore_walk_step(revs
);
3169 static void indegree_walk_step(struct rev_info
*revs
)
3171 struct commit_list
*p
;
3172 struct topo_walk_info
*info
= revs
->topo_walk_info
;
3173 struct commit
*c
= prio_queue_get(&info
->indegree_queue
);
3178 if (parse_commit_gently(c
, 1) < 0)
3181 explore_to_depth(revs
, c
->generation
);
3183 for (p
= c
->parents
; p
; p
= p
->next
) {
3184 struct commit
*parent
= p
->item
;
3185 int *pi
= indegree_slab_at(&info
->indegree
, parent
);
3192 test_flag_and_insert(&info
->indegree_queue
, parent
, TOPO_WALK_INDEGREE
);
3194 if (revs
->first_parent_only
)
3199 static void compute_indegrees_to_depth(struct rev_info
*revs
,
3200 uint32_t gen_cutoff
)
3202 struct topo_walk_info
*info
= revs
->topo_walk_info
;
3204 while ((c
= prio_queue_peek(&info
->indegree_queue
)) &&
3205 c
->generation
>= gen_cutoff
)
3206 indegree_walk_step(revs
);
3209 static void init_topo_walk(struct rev_info
*revs
)
3211 struct topo_walk_info
*info
;
3212 struct commit_list
*list
;
3213 revs
->topo_walk_info
= xmalloc(sizeof(struct topo_walk_info
));
3214 info
= revs
->topo_walk_info
;
3215 memset(info
, 0, sizeof(struct topo_walk_info
));
3217 init_indegree_slab(&info
->indegree
);
3218 memset(&info
->explore_queue
, 0, sizeof(info
->explore_queue
));
3219 memset(&info
->indegree_queue
, 0, sizeof(info
->indegree_queue
));
3220 memset(&info
->topo_queue
, 0, sizeof(info
->topo_queue
));
3222 switch (revs
->sort_order
) {
3223 default: /* REV_SORT_IN_GRAPH_ORDER */
3224 info
->topo_queue
.compare
= NULL
;
3226 case REV_SORT_BY_COMMIT_DATE
:
3227 info
->topo_queue
.compare
= compare_commits_by_commit_date
;
3229 case REV_SORT_BY_AUTHOR_DATE
:
3230 init_author_date_slab(&info
->author_date
);
3231 info
->topo_queue
.compare
= compare_commits_by_author_date
;
3232 info
->topo_queue
.cb_data
= &info
->author_date
;
3236 info
->explore_queue
.compare
= compare_commits_by_gen_then_commit_date
;
3237 info
->indegree_queue
.compare
= compare_commits_by_gen_then_commit_date
;
3239 info
->min_generation
= GENERATION_NUMBER_INFINITY
;
3240 for (list
= revs
->commits
; list
; list
= list
->next
) {
3241 struct commit
*c
= list
->item
;
3243 if (parse_commit_gently(c
, 1))
3246 test_flag_and_insert(&info
->explore_queue
, c
, TOPO_WALK_EXPLORED
);
3247 test_flag_and_insert(&info
->indegree_queue
, c
, TOPO_WALK_INDEGREE
);
3249 if (c
->generation
< info
->min_generation
)
3250 info
->min_generation
= c
->generation
;
3252 *(indegree_slab_at(&info
->indegree
, c
)) = 1;
3254 if (revs
->sort_order
== REV_SORT_BY_AUTHOR_DATE
)
3255 record_author_date(&info
->author_date
, c
);
3257 compute_indegrees_to_depth(revs
, info
->min_generation
);
3259 for (list
= revs
->commits
; list
; list
= list
->next
) {
3260 struct commit
*c
= list
->item
;
3262 if (*(indegree_slab_at(&info
->indegree
, c
)) == 1)
3263 prio_queue_put(&info
->topo_queue
, c
);
3267 * This is unfortunate; the initial tips need to be shown
3268 * in the order given from the revision traversal machinery.
3270 if (revs
->sort_order
== REV_SORT_IN_GRAPH_ORDER
)
3271 prio_queue_reverse(&info
->topo_queue
);
3274 static struct commit
*next_topo_commit(struct rev_info
*revs
)
3277 struct topo_walk_info
*info
= revs
->topo_walk_info
;
3279 /* pop next off of topo_queue */
3280 c
= prio_queue_get(&info
->topo_queue
);
3283 *(indegree_slab_at(&info
->indegree
, c
)) = 0;
3288 static void expand_topo_walk(struct rev_info
*revs
, struct commit
*commit
)
3290 struct commit_list
*p
;
3291 struct topo_walk_info
*info
= revs
->topo_walk_info
;
3292 if (process_parents(revs
, commit
, NULL
, NULL
) < 0) {
3293 if (!revs
->ignore_missing_links
)
3294 die("Failed to traverse parents of commit %s",
3295 oid_to_hex(&commit
->object
.oid
));
3298 for (p
= commit
->parents
; p
; p
= p
->next
) {
3299 struct commit
*parent
= p
->item
;
3302 if (parent
->object
.flags
& UNINTERESTING
)
3305 if (parse_commit_gently(parent
, 1) < 0)
3308 if (parent
->generation
< info
->min_generation
) {
3309 info
->min_generation
= parent
->generation
;
3310 compute_indegrees_to_depth(revs
, info
->min_generation
);
3313 pi
= indegree_slab_at(&info
->indegree
, parent
);
3317 prio_queue_put(&info
->topo_queue
, parent
);
3319 if (revs
->first_parent_only
)
3324 int prepare_revision_walk(struct rev_info
*revs
)
3327 struct object_array old_pending
;
3328 struct commit_list
**next
= &revs
->commits
;
3330 memcpy(&old_pending
, &revs
->pending
, sizeof(old_pending
));
3331 revs
->pending
.nr
= 0;
3332 revs
->pending
.alloc
= 0;
3333 revs
->pending
.objects
= NULL
;
3334 for (i
= 0; i
< old_pending
.nr
; i
++) {
3335 struct object_array_entry
*e
= old_pending
.objects
+ i
;
3336 struct commit
*commit
= handle_commit(revs
, e
);
3338 if (!(commit
->object
.flags
& SEEN
)) {
3339 commit
->object
.flags
|= SEEN
;
3340 next
= commit_list_append(commit
, next
);
3344 object_array_clear(&old_pending
);
3346 /* Signal whether we need per-parent treesame decoration */
3347 if (revs
->simplify_merges
||
3348 (revs
->limited
&& limiting_can_increase_treesame(revs
)))
3349 revs
->treesame
.name
= "treesame";
3351 if (revs
->exclude_promisor_objects
) {
3352 for_each_packed_object(mark_uninteresting
, revs
,
3353 FOR_EACH_OBJECT_PROMISOR_ONLY
);
3356 if (revs
->no_walk
!= REVISION_WALK_NO_WALK_UNSORTED
)
3357 commit_list_sort_by_date(&revs
->commits
);
3360 if (revs
->limited
) {
3361 if (limit_list(revs
) < 0)
3363 if (revs
->topo_order
)
3364 sort_in_topological_order(&revs
->commits
, revs
->sort_order
);
3365 } else if (revs
->topo_order
)
3366 init_topo_walk(revs
);
3367 if (revs
->line_level_traverse
)
3368 line_log_filter(revs
);
3369 if (revs
->simplify_merges
)
3370 simplify_merges(revs
);
3371 if (revs
->children
.name
)
3376 static enum rewrite_result
rewrite_one_1(struct rev_info
*revs
,
3378 struct prio_queue
*queue
)
3381 struct commit
*p
= *pp
;
3383 if (process_parents(revs
, p
, NULL
, queue
) < 0)
3384 return rewrite_one_error
;
3385 if (p
->object
.flags
& UNINTERESTING
)
3386 return rewrite_one_ok
;
3387 if (!(p
->object
.flags
& TREESAME
))
3388 return rewrite_one_ok
;
3390 return rewrite_one_noparents
;
3391 if ((p
= one_relevant_parent(revs
, p
->parents
)) == NULL
)
3392 return rewrite_one_ok
;
3397 static void merge_queue_into_list(struct prio_queue
*q
, struct commit_list
**list
)
3400 struct commit
*item
= prio_queue_peek(q
);
3401 struct commit_list
*p
= *list
;
3403 if (p
&& p
->item
->date
>= item
->date
)
3406 p
= commit_list_insert(item
, list
);
3407 list
= &p
->next
; /* skip newly added item */
3408 prio_queue_get(q
); /* pop item */
3413 static enum rewrite_result
rewrite_one(struct rev_info
*revs
, struct commit
**pp
)
3415 struct prio_queue queue
= { compare_commits_by_commit_date
};
3416 enum rewrite_result ret
= rewrite_one_1(revs
, pp
, &queue
);
3417 merge_queue_into_list(&queue
, &revs
->commits
);
3418 clear_prio_queue(&queue
);
3422 int rewrite_parents(struct rev_info
*revs
, struct commit
*commit
,
3423 rewrite_parent_fn_t rewrite_parent
)
3425 struct commit_list
**pp
= &commit
->parents
;
3427 struct commit_list
*parent
= *pp
;
3428 switch (rewrite_parent(revs
, &parent
->item
)) {
3429 case rewrite_one_ok
:
3431 case rewrite_one_noparents
:
3434 case rewrite_one_error
:
3439 remove_duplicate_parents(revs
, commit
);
3443 static int commit_rewrite_person(struct strbuf
*buf
, const char *what
, struct string_list
*mailmap
)
3445 char *person
, *endp
;
3446 size_t len
, namelen
, maillen
;
3449 struct ident_split ident
;
3451 person
= strstr(buf
->buf
, what
);
3455 person
+= strlen(what
);
3456 endp
= strchr(person
, '\n');
3460 len
= endp
- person
;
3462 if (split_ident_line(&ident
, person
, len
))
3465 mail
= ident
.mail_begin
;
3466 maillen
= ident
.mail_end
- ident
.mail_begin
;
3467 name
= ident
.name_begin
;
3468 namelen
= ident
.name_end
- ident
.name_begin
;
3470 if (map_user(mailmap
, &mail
, &maillen
, &name
, &namelen
)) {
3471 struct strbuf namemail
= STRBUF_INIT
;
3473 strbuf_addf(&namemail
, "%.*s <%.*s>",
3474 (int)namelen
, name
, (int)maillen
, mail
);
3476 strbuf_splice(buf
, ident
.name_begin
- buf
->buf
,
3477 ident
.mail_end
- ident
.name_begin
+ 1,
3478 namemail
.buf
, namemail
.len
);
3480 strbuf_release(&namemail
);
3488 static int commit_match(struct commit
*commit
, struct rev_info
*opt
)
3491 const char *encoding
;
3492 const char *message
;
3493 struct strbuf buf
= STRBUF_INIT
;
3495 if (!opt
->grep_filter
.pattern_list
&& !opt
->grep_filter
.header_list
)
3498 /* Prepend "fake" headers as needed */
3499 if (opt
->grep_filter
.use_reflog_filter
) {
3500 strbuf_addstr(&buf
, "reflog ");
3501 get_reflog_message(&buf
, opt
->reflog_info
);
3502 strbuf_addch(&buf
, '\n');
3506 * We grep in the user's output encoding, under the assumption that it
3507 * is the encoding they are most likely to write their grep pattern
3508 * for. In addition, it means we will match the "notes" encoding below,
3509 * so we will not end up with a buffer that has two different encodings
3512 encoding
= get_log_output_encoding();
3513 message
= logmsg_reencode(commit
, NULL
, encoding
);
3515 /* Copy the commit to temporary if we are using "fake" headers */
3517 strbuf_addstr(&buf
, message
);
3519 if (opt
->grep_filter
.header_list
&& opt
->mailmap
) {
3521 strbuf_addstr(&buf
, message
);
3523 commit_rewrite_person(&buf
, "\nauthor ", opt
->mailmap
);
3524 commit_rewrite_person(&buf
, "\ncommitter ", opt
->mailmap
);
3527 /* Append "fake" message parts as needed */
3528 if (opt
->show_notes
) {
3530 strbuf_addstr(&buf
, message
);
3531 format_display_notes(&commit
->object
.oid
, &buf
, encoding
, 1);
3535 * Find either in the original commit message, or in the temporary.
3536 * Note that we cast away the constness of "message" here. It is
3537 * const because it may come from the cached commit buffer. That's OK,
3538 * because we know that it is modifiable heap memory, and that while
3539 * grep_buffer may modify it for speed, it will restore any
3540 * changes before returning.
3543 retval
= grep_buffer(&opt
->grep_filter
, buf
.buf
, buf
.len
);
3545 retval
= grep_buffer(&opt
->grep_filter
,
3546 (char *)message
, strlen(message
));
3547 strbuf_release(&buf
);
3548 unuse_commit_buffer(commit
, message
);
3549 return opt
->invert_grep
? !retval
: retval
;
3552 static inline int want_ancestry(const struct rev_info
*revs
)
3554 return (revs
->rewrite_parents
|| revs
->children
.name
);
3558 * Return a timestamp to be used for --since/--until comparisons for this
3559 * commit, based on the revision options.
3561 static timestamp_t
comparison_date(const struct rev_info
*revs
,
3562 struct commit
*commit
)
3564 return revs
->reflog_info
?
3565 get_reflog_timestamp(revs
->reflog_info
) :
3569 enum commit_action
get_commit_action(struct rev_info
*revs
, struct commit
*commit
)
3571 if (commit
->object
.flags
& SHOWN
)
3572 return commit_ignore
;
3573 if (revs
->unpacked
&& has_object_pack(&commit
->object
.oid
))
3574 return commit_ignore
;
3575 if (commit
->object
.flags
& UNINTERESTING
)
3576 return commit_ignore
;
3577 if (revs
->min_age
!= -1 &&
3578 comparison_date(revs
, commit
) > revs
->min_age
)
3579 return commit_ignore
;
3580 if (revs
->min_parents
|| (revs
->max_parents
>= 0)) {
3581 int n
= commit_list_count(commit
->parents
);
3582 if ((n
< revs
->min_parents
) ||
3583 ((revs
->max_parents
>= 0) && (n
> revs
->max_parents
)))
3584 return commit_ignore
;
3586 if (!commit_match(commit
, revs
))
3587 return commit_ignore
;
3588 if (revs
->prune
&& revs
->dense
) {
3589 /* Commit without changes? */
3590 if (commit
->object
.flags
& TREESAME
) {
3592 struct commit_list
*p
;
3593 /* drop merges unless we want parenthood */
3594 if (!want_ancestry(revs
))
3595 return commit_ignore
;
3597 * If we want ancestry, then need to keep any merges
3598 * between relevant commits to tie together topology.
3599 * For consistency with TREESAME and simplification
3600 * use "relevant" here rather than just INTERESTING,
3601 * to treat bottom commit(s) as part of the topology.
3603 for (n
= 0, p
= commit
->parents
; p
; p
= p
->next
)
3604 if (relevant_commit(p
->item
))
3607 return commit_ignore
;
3613 define_commit_slab(saved_parents
, struct commit_list
*);
3615 #define EMPTY_PARENT_LIST ((struct commit_list *)-1)
3618 * You may only call save_parents() once per commit (this is checked
3619 * for non-root commits).
3621 static void save_parents(struct rev_info
*revs
, struct commit
*commit
)
3623 struct commit_list
**pp
;
3625 if (!revs
->saved_parents_slab
) {
3626 revs
->saved_parents_slab
= xmalloc(sizeof(struct saved_parents
));
3627 init_saved_parents(revs
->saved_parents_slab
);
3630 pp
= saved_parents_at(revs
->saved_parents_slab
, commit
);
3633 * When walking with reflogs, we may visit the same commit
3634 * several times: once for each appearance in the reflog.
3636 * In this case, save_parents() will be called multiple times.
3637 * We want to keep only the first set of parents. We need to
3638 * store a sentinel value for an empty (i.e., NULL) parent
3639 * list to distinguish it from a not-yet-saved list, however.
3643 if (commit
->parents
)
3644 *pp
= copy_commit_list(commit
->parents
);
3646 *pp
= EMPTY_PARENT_LIST
;
3649 static void free_saved_parents(struct rev_info
*revs
)
3651 if (revs
->saved_parents_slab
)
3652 clear_saved_parents(revs
->saved_parents_slab
);
3655 struct commit_list
*get_saved_parents(struct rev_info
*revs
, const struct commit
*commit
)
3657 struct commit_list
*parents
;
3659 if (!revs
->saved_parents_slab
)
3660 return commit
->parents
;
3662 parents
= *saved_parents_at(revs
->saved_parents_slab
, commit
);
3663 if (parents
== EMPTY_PARENT_LIST
)
3668 enum commit_action
simplify_commit(struct rev_info
*revs
, struct commit
*commit
)
3670 enum commit_action action
= get_commit_action(revs
, commit
);
3672 if (action
== commit_show
&&
3673 revs
->prune
&& revs
->dense
&& want_ancestry(revs
)) {
3675 * --full-diff on simplified parents is no good: it
3676 * will show spurious changes from the commits that
3677 * were elided. So we save the parents on the side
3678 * when --full-diff is in effect.
3680 if (revs
->full_diff
)
3681 save_parents(revs
, commit
);
3682 if (rewrite_parents(revs
, commit
, rewrite_one
) < 0)
3683 return commit_error
;
3688 static void track_linear(struct rev_info
*revs
, struct commit
*commit
)
3690 if (revs
->track_first_time
) {
3692 revs
->track_first_time
= 0;
3694 struct commit_list
*p
;
3695 for (p
= revs
->previous_parents
; p
; p
= p
->next
)
3696 if (p
->item
== NULL
|| /* first commit */
3697 oideq(&p
->item
->object
.oid
, &commit
->object
.oid
))
3699 revs
->linear
= p
!= NULL
;
3701 if (revs
->reverse
) {
3703 commit
->object
.flags
|= TRACK_LINEAR
;
3705 free_commit_list(revs
->previous_parents
);
3706 revs
->previous_parents
= copy_commit_list(commit
->parents
);
3709 static struct commit
*get_revision_1(struct rev_info
*revs
)
3712 struct commit
*commit
;
3714 if (revs
->reflog_info
)
3715 commit
= next_reflog_entry(revs
->reflog_info
);
3716 else if (revs
->topo_walk_info
)
3717 commit
= next_topo_commit(revs
);
3719 commit
= pop_commit(&revs
->commits
);
3724 if (revs
->reflog_info
)
3725 commit
->object
.flags
&= ~(ADDED
| SEEN
| SHOWN
);
3728 * If we haven't done the list limiting, we need to look at
3729 * the parents here. We also need to do the date-based limiting
3730 * that we'd otherwise have done in limit_list().
3732 if (!revs
->limited
) {
3733 if (revs
->max_age
!= -1 &&
3734 comparison_date(revs
, commit
) < revs
->max_age
)
3737 if (revs
->reflog_info
)
3738 try_to_simplify_commit(revs
, commit
);
3739 else if (revs
->topo_walk_info
)
3740 expand_topo_walk(revs
, commit
);
3741 else if (process_parents(revs
, commit
, &revs
->commits
, NULL
) < 0) {
3742 if (!revs
->ignore_missing_links
)
3743 die("Failed to traverse parents of commit %s",
3744 oid_to_hex(&commit
->object
.oid
));
3748 switch (simplify_commit(revs
, commit
)) {
3752 die("Failed to simplify parents of commit %s",
3753 oid_to_hex(&commit
->object
.oid
));
3755 if (revs
->track_linear
)
3756 track_linear(revs
, commit
);
3763 * Return true for entries that have not yet been shown. (This is an
3764 * object_array_each_func_t.)
3766 static int entry_unshown(struct object_array_entry
*entry
, void *cb_data_unused
)
3768 return !(entry
->item
->flags
& SHOWN
);
3772 * If array is on the verge of a realloc, garbage-collect any entries
3773 * that have already been shown to try to free up some space.
3775 static void gc_boundary(struct object_array
*array
)
3777 if (array
->nr
== array
->alloc
)
3778 object_array_filter(array
, entry_unshown
, NULL
);
3781 static void create_boundary_commit_list(struct rev_info
*revs
)
3785 struct object_array
*array
= &revs
->boundary_commits
;
3786 struct object_array_entry
*objects
= array
->objects
;
3789 * If revs->commits is non-NULL at this point, an error occurred in
3790 * get_revision_1(). Ignore the error and continue printing the
3791 * boundary commits anyway. (This is what the code has always
3794 if (revs
->commits
) {
3795 free_commit_list(revs
->commits
);
3796 revs
->commits
= NULL
;
3800 * Put all of the actual boundary commits from revs->boundary_commits
3801 * into revs->commits
3803 for (i
= 0; i
< array
->nr
; i
++) {
3804 c
= (struct commit
*)(objects
[i
].item
);
3807 if (!(c
->object
.flags
& CHILD_SHOWN
))
3809 if (c
->object
.flags
& (SHOWN
| BOUNDARY
))
3811 c
->object
.flags
|= BOUNDARY
;
3812 commit_list_insert(c
, &revs
->commits
);
3816 * If revs->topo_order is set, sort the boundary commits
3817 * in topological order
3819 sort_in_topological_order(&revs
->commits
, revs
->sort_order
);
3822 static struct commit
*get_revision_internal(struct rev_info
*revs
)
3824 struct commit
*c
= NULL
;
3825 struct commit_list
*l
;
3827 if (revs
->boundary
== 2) {
3829 * All of the normal commits have already been returned,
3830 * and we are now returning boundary commits.
3831 * create_boundary_commit_list() has populated
3832 * revs->commits with the remaining commits to return.
3834 c
= pop_commit(&revs
->commits
);
3836 c
->object
.flags
|= SHOWN
;
3841 * If our max_count counter has reached zero, then we are done. We
3842 * don't simply return NULL because we still might need to show
3843 * boundary commits. But we want to avoid calling get_revision_1, which
3844 * might do a considerable amount of work finding the next commit only
3845 * for us to throw it away.
3847 * If it is non-zero, then either we don't have a max_count at all
3848 * (-1), or it is still counting, in which case we decrement.
3850 if (revs
->max_count
) {
3851 c
= get_revision_1(revs
);
3853 while (revs
->skip_count
> 0) {
3855 c
= get_revision_1(revs
);
3861 if (revs
->max_count
> 0)
3866 c
->object
.flags
|= SHOWN
;
3868 if (!revs
->boundary
)
3873 * get_revision_1() runs out the commits, and
3874 * we are done computing the boundaries.
3875 * switch to boundary commits output mode.
3880 * Update revs->commits to contain the list of
3883 create_boundary_commit_list(revs
);
3885 return get_revision_internal(revs
);
3889 * boundary commits are the commits that are parents of the
3890 * ones we got from get_revision_1() but they themselves are
3891 * not returned from get_revision_1(). Before returning
3892 * 'c', we need to mark its parents that they could be boundaries.
3895 for (l
= c
->parents
; l
; l
= l
->next
) {
3897 p
= &(l
->item
->object
);
3898 if (p
->flags
& (CHILD_SHOWN
| SHOWN
))
3900 p
->flags
|= CHILD_SHOWN
;
3901 gc_boundary(&revs
->boundary_commits
);
3902 add_object_array(p
, NULL
, &revs
->boundary_commits
);
3908 struct commit
*get_revision(struct rev_info
*revs
)
3911 struct commit_list
*reversed
;
3913 if (revs
->reverse
) {
3915 while ((c
= get_revision_internal(revs
)))
3916 commit_list_insert(c
, &reversed
);
3917 revs
->commits
= reversed
;
3919 revs
->reverse_output_stage
= 1;
3922 if (revs
->reverse_output_stage
) {
3923 c
= pop_commit(&revs
->commits
);
3924 if (revs
->track_linear
)
3925 revs
->linear
= !!(c
&& c
->object
.flags
& TRACK_LINEAR
);
3929 c
= get_revision_internal(revs
);
3930 if (c
&& revs
->graph
)
3931 graph_update(revs
->graph
, c
);
3933 free_saved_parents(revs
);
3934 if (revs
->previous_parents
) {
3935 free_commit_list(revs
->previous_parents
);
3936 revs
->previous_parents
= NULL
;
3942 char *get_revision_mark(const struct rev_info
*revs
, const struct commit
*commit
)
3944 if (commit
->object
.flags
& BOUNDARY
)
3946 else if (commit
->object
.flags
& UNINTERESTING
)
3948 else if (commit
->object
.flags
& PATCHSAME
)
3950 else if (!revs
|| revs
->left_right
) {
3951 if (commit
->object
.flags
& SYMMETRIC_LEFT
)
3955 } else if (revs
->graph
)
3957 else if (revs
->cherry_mark
)
3962 void put_revision_mark(const struct rev_info
*revs
, const struct commit
*commit
)
3964 char *mark
= get_revision_mark(revs
, commit
);
3967 fputs(mark
, stdout
);