2 #include "object-store.h"
10 #include "repository.h"
13 #include "reflog-walk.h"
14 #include "patch-ids.h"
17 #include "string-list.h"
20 #include "commit-slab.h"
22 #include "cache-tree.h"
26 #include "argv-array.h"
27 #include "commit-reach.h"
28 #include "commit-graph.h"
29 #include "prio-queue.h"
32 volatile show_early_output_fn_t show_early_output
;
34 static const char *term_bad
;
35 static const char *term_good
;
37 implement_shared_commit_slab(revision_sources
, char *);
39 void show_object_with_name(FILE *out
, struct object
*obj
, const char *name
)
43 fprintf(out
, "%s ", oid_to_hex(&obj
->oid
));
44 for (p
= name
; *p
&& *p
!= '\n'; p
++)
49 static void mark_blob_uninteresting(struct blob
*blob
)
53 if (blob
->object
.flags
& UNINTERESTING
)
55 blob
->object
.flags
|= UNINTERESTING
;
58 static void mark_tree_contents_uninteresting(struct repository
*r
,
61 struct tree_desc desc
;
62 struct name_entry entry
;
64 if (parse_tree_gently(tree
, 1) < 0)
67 init_tree_desc(&desc
, tree
->buffer
, tree
->size
);
68 while (tree_entry(&desc
, &entry
)) {
69 switch (object_type(entry
.mode
)) {
71 mark_tree_uninteresting(r
, lookup_tree(r
, &entry
.oid
));
74 mark_blob_uninteresting(lookup_blob(r
, &entry
.oid
));
77 /* Subproject commit - not in this repository */
83 * We don't care about the tree any more
84 * after it has been marked uninteresting.
86 free_tree_buffer(tree
);
89 void mark_tree_uninteresting(struct repository
*r
, struct tree
*tree
)
97 if (obj
->flags
& UNINTERESTING
)
99 obj
->flags
|= UNINTERESTING
;
100 mark_tree_contents_uninteresting(r
, tree
);
103 struct path_and_oids_entry
{
104 struct hashmap_entry ent
;
109 static int path_and_oids_cmp(const void *hashmap_cmp_fn_data
,
110 const struct path_and_oids_entry
*e1
,
111 const struct path_and_oids_entry
*e2
,
114 return strcmp(e1
->path
, e2
->path
);
117 static void paths_and_oids_init(struct hashmap
*map
)
119 hashmap_init(map
, (hashmap_cmp_fn
) path_and_oids_cmp
, NULL
, 0);
122 static void paths_and_oids_clear(struct hashmap
*map
)
124 struct hashmap_iter iter
;
125 struct path_and_oids_entry
*entry
;
126 hashmap_iter_init(map
, &iter
);
128 while ((entry
= (struct path_and_oids_entry
*)hashmap_iter_next(&iter
))) {
129 oidset_clear(&entry
->trees
);
133 hashmap_free(map
, 1);
136 static void paths_and_oids_insert(struct hashmap
*map
,
138 const struct object_id
*oid
)
140 int hash
= strhash(path
);
141 struct path_and_oids_entry key
;
142 struct path_and_oids_entry
*entry
;
144 hashmap_entry_init(&key
, hash
);
146 /* use a shallow copy for the lookup */
147 key
.path
= (char *)path
;
148 oidset_init(&key
.trees
, 0);
150 if (!(entry
= (struct path_and_oids_entry
*)hashmap_get(map
, &key
, NULL
))) {
151 entry
= xcalloc(1, sizeof(struct path_and_oids_entry
));
152 hashmap_entry_init(entry
, hash
);
153 entry
->path
= xstrdup(key
.path
);
154 oidset_init(&entry
->trees
, 16);
155 hashmap_put(map
, entry
);
158 oidset_insert(&entry
->trees
, oid
);
161 static void add_children_by_path(struct repository
*r
,
165 struct tree_desc desc
;
166 struct name_entry entry
;
171 if (parse_tree_gently(tree
, 1) < 0)
174 init_tree_desc(&desc
, tree
->buffer
, tree
->size
);
175 while (tree_entry(&desc
, &entry
)) {
176 switch (object_type(entry
.mode
)) {
178 paths_and_oids_insert(map
, entry
.path
, &entry
.oid
);
180 if (tree
->object
.flags
& UNINTERESTING
) {
181 struct tree
*child
= lookup_tree(r
, &entry
.oid
);
183 child
->object
.flags
|= UNINTERESTING
;
187 if (tree
->object
.flags
& UNINTERESTING
) {
188 struct blob
*child
= lookup_blob(r
, &entry
.oid
);
190 child
->object
.flags
|= UNINTERESTING
;
194 /* Subproject commit - not in this repository */
199 free_tree_buffer(tree
);
202 void mark_trees_uninteresting_sparse(struct repository
*r
,
203 struct oidset
*trees
)
205 unsigned has_interesting
= 0, has_uninteresting
= 0;
207 struct hashmap_iter map_iter
;
208 struct path_and_oids_entry
*entry
;
209 struct object_id
*oid
;
210 struct oidset_iter iter
;
212 oidset_iter_init(trees
, &iter
);
213 while ((!has_interesting
|| !has_uninteresting
) &&
214 (oid
= oidset_iter_next(&iter
))) {
215 struct tree
*tree
= lookup_tree(r
, oid
);
220 if (tree
->object
.flags
& UNINTERESTING
)
221 has_uninteresting
= 1;
226 /* Do not walk unless we have both types of trees. */
227 if (!has_uninteresting
|| !has_interesting
)
230 paths_and_oids_init(&map
);
232 oidset_iter_init(trees
, &iter
);
233 while ((oid
= oidset_iter_next(&iter
))) {
234 struct tree
*tree
= lookup_tree(r
, oid
);
235 add_children_by_path(r
, tree
, &map
);
238 hashmap_iter_init(&map
, &map_iter
);
239 while ((entry
= hashmap_iter_next(&map_iter
)))
240 mark_trees_uninteresting_sparse(r
, &entry
->trees
);
242 paths_and_oids_clear(&map
);
245 struct commit_stack
{
246 struct commit
**items
;
249 #define COMMIT_STACK_INIT { NULL, 0, 0 }
251 static void commit_stack_push(struct commit_stack
*stack
, struct commit
*commit
)
253 ALLOC_GROW(stack
->items
, stack
->nr
+ 1, stack
->alloc
);
254 stack
->items
[stack
->nr
++] = commit
;
257 static struct commit
*commit_stack_pop(struct commit_stack
*stack
)
259 return stack
->nr
? stack
->items
[--stack
->nr
] : NULL
;
262 static void commit_stack_clear(struct commit_stack
*stack
)
264 FREE_AND_NULL(stack
->items
);
265 stack
->nr
= stack
->alloc
= 0;
268 static void mark_one_parent_uninteresting(struct commit
*commit
,
269 struct commit_stack
*pending
)
271 struct commit_list
*l
;
273 if (commit
->object
.flags
& UNINTERESTING
)
275 commit
->object
.flags
|= UNINTERESTING
;
278 * Normally we haven't parsed the parent
279 * yet, so we won't have a parent of a parent
280 * here. However, it may turn out that we've
281 * reached this commit some other way (where it
282 * wasn't uninteresting), in which case we need
283 * to mark its parents recursively too..
285 for (l
= commit
->parents
; l
; l
= l
->next
)
286 commit_stack_push(pending
, l
->item
);
289 void mark_parents_uninteresting(struct commit
*commit
)
291 struct commit_stack pending
= COMMIT_STACK_INIT
;
292 struct commit_list
*l
;
294 for (l
= commit
->parents
; l
; l
= l
->next
)
295 mark_one_parent_uninteresting(l
->item
, &pending
);
297 while (pending
.nr
> 0)
298 mark_one_parent_uninteresting(commit_stack_pop(&pending
),
301 commit_stack_clear(&pending
);
304 static void add_pending_object_with_path(struct rev_info
*revs
,
306 const char *name
, unsigned mode
,
311 if (revs
->no_walk
&& (obj
->flags
& UNINTERESTING
))
313 if (revs
->reflog_info
&& obj
->type
== OBJ_COMMIT
) {
314 struct strbuf buf
= STRBUF_INIT
;
315 int len
= interpret_branch_name(name
, 0, &buf
, 0);
317 if (0 < len
&& name
[len
] && buf
.len
)
318 strbuf_addstr(&buf
, name
+ len
);
319 add_reflog_for_walk(revs
->reflog_info
,
320 (struct commit
*)obj
,
321 buf
.buf
[0] ? buf
.buf
: name
);
322 strbuf_release(&buf
);
323 return; /* do not add the commit itself */
325 add_object_array_with_path(obj
, name
, &revs
->pending
, mode
, path
);
328 static void add_pending_object_with_mode(struct rev_info
*revs
,
330 const char *name
, unsigned mode
)
332 add_pending_object_with_path(revs
, obj
, name
, mode
, NULL
);
335 void add_pending_object(struct rev_info
*revs
,
336 struct object
*obj
, const char *name
)
338 add_pending_object_with_mode(revs
, obj
, name
, S_IFINVALID
);
341 void add_head_to_pending(struct rev_info
*revs
)
343 struct object_id oid
;
345 if (get_oid("HEAD", &oid
))
347 obj
= parse_object(revs
->repo
, &oid
);
350 add_pending_object(revs
, obj
, "HEAD");
353 static struct object
*get_reference(struct rev_info
*revs
, const char *name
,
354 const struct object_id
*oid
,
357 struct object
*object
;
360 * If the repository has commit graphs, repo_parse_commit() avoids
361 * reading the object buffer, so use it whenever possible.
363 if (oid_object_info(revs
->repo
, oid
, NULL
) == OBJ_COMMIT
) {
364 struct commit
*c
= lookup_commit(revs
->repo
, oid
);
365 if (!repo_parse_commit(revs
->repo
, c
))
366 object
= (struct object
*) c
;
370 object
= parse_object(revs
->repo
, oid
);
374 if (revs
->ignore_missing
)
376 if (revs
->exclude_promisor_objects
&& is_promisor_object(oid
))
378 die("bad object %s", name
);
380 object
->flags
|= flags
;
384 void add_pending_oid(struct rev_info
*revs
, const char *name
,
385 const struct object_id
*oid
, unsigned int flags
)
387 struct object
*object
= get_reference(revs
, name
, oid
, flags
);
388 add_pending_object(revs
, object
, name
);
391 static struct commit
*handle_commit(struct rev_info
*revs
,
392 struct object_array_entry
*entry
)
394 struct object
*object
= entry
->item
;
395 const char *name
= entry
->name
;
396 const char *path
= entry
->path
;
397 unsigned int mode
= entry
->mode
;
398 unsigned long flags
= object
->flags
;
401 * Tag object? Look what it points to..
403 while (object
->type
== OBJ_TAG
) {
404 struct tag
*tag
= (struct tag
*) object
;
405 if (revs
->tag_objects
&& !(flags
& UNINTERESTING
))
406 add_pending_object(revs
, object
, tag
->tag
);
409 object
= parse_object(revs
->repo
, &tag
->tagged
->oid
);
411 if (revs
->ignore_missing_links
|| (flags
& UNINTERESTING
))
413 if (revs
->exclude_promisor_objects
&&
414 is_promisor_object(&tag
->tagged
->oid
))
416 die("bad object %s", oid_to_hex(&tag
->tagged
->oid
));
418 object
->flags
|= flags
;
420 * We'll handle the tagged object by looping or dropping
421 * through to the non-tag handlers below. Do not
422 * propagate path data from the tag's pending entry.
429 * Commit object? Just return it, we'll do all the complex
432 if (object
->type
== OBJ_COMMIT
) {
433 struct commit
*commit
= (struct commit
*)object
;
435 if (parse_commit(commit
) < 0)
436 die("unable to parse commit %s", name
);
437 if (flags
& UNINTERESTING
) {
438 mark_parents_uninteresting(commit
);
442 char **slot
= revision_sources_at(revs
->sources
, commit
);
445 *slot
= xstrdup(name
);
451 * Tree object? Either mark it uninteresting, or add it
452 * to the list of objects to look at later..
454 if (object
->type
== OBJ_TREE
) {
455 struct tree
*tree
= (struct tree
*)object
;
456 if (!revs
->tree_objects
)
458 if (flags
& UNINTERESTING
) {
459 mark_tree_contents_uninteresting(revs
->repo
, tree
);
462 add_pending_object_with_path(revs
, object
, name
, mode
, path
);
467 * Blob object? You know the drill by now..
469 if (object
->type
== OBJ_BLOB
) {
470 if (!revs
->blob_objects
)
472 if (flags
& UNINTERESTING
)
474 add_pending_object_with_path(revs
, object
, name
, mode
, path
);
477 die("%s is unknown object", name
);
480 static int everybody_uninteresting(struct commit_list
*orig
,
481 struct commit
**interesting_cache
)
483 struct commit_list
*list
= orig
;
485 if (*interesting_cache
) {
486 struct commit
*commit
= *interesting_cache
;
487 if (!(commit
->object
.flags
& UNINTERESTING
))
492 struct commit
*commit
= list
->item
;
494 if (commit
->object
.flags
& UNINTERESTING
)
497 *interesting_cache
= commit
;
504 * A definition of "relevant" commit that we can use to simplify limited graphs
505 * by eliminating side branches.
507 * A "relevant" commit is one that is !UNINTERESTING (ie we are including it
508 * in our list), or that is a specified BOTTOM commit. Then after computing
509 * a limited list, during processing we can generally ignore boundary merges
510 * coming from outside the graph, (ie from irrelevant parents), and treat
511 * those merges as if they were single-parent. TREESAME is defined to consider
512 * only relevant parents, if any. If we are TREESAME to our on-graph parents,
513 * we don't care if we were !TREESAME to non-graph parents.
515 * Treating bottom commits as relevant ensures that a limited graph's
516 * connection to the actual bottom commit is not viewed as a side branch, but
517 * treated as part of the graph. For example:
519 * ....Z...A---X---o---o---B
523 * When computing "A..B", the A-X connection is at least as important as
524 * Y-X, despite A being flagged UNINTERESTING.
526 * And when computing --ancestry-path "A..B", the A-X connection is more
527 * important than Y-X, despite both A and Y being flagged UNINTERESTING.
529 static inline int relevant_commit(struct commit
*commit
)
531 return (commit
->object
.flags
& (UNINTERESTING
| BOTTOM
)) != UNINTERESTING
;
535 * Return a single relevant commit from a parent list. If we are a TREESAME
536 * commit, and this selects one of our parents, then we can safely simplify to
539 static struct commit
*one_relevant_parent(const struct rev_info
*revs
,
540 struct commit_list
*orig
)
542 struct commit_list
*list
= orig
;
543 struct commit
*relevant
= NULL
;
549 * For 1-parent commits, or if first-parent-only, then return that
550 * first parent (even if not "relevant" by the above definition).
551 * TREESAME will have been set purely on that parent.
553 if (revs
->first_parent_only
|| !orig
->next
)
557 * For multi-parent commits, identify a sole relevant parent, if any.
558 * If we have only one relevant parent, then TREESAME will be set purely
559 * with regard to that parent, and we can simplify accordingly.
561 * If we have more than one relevant parent, or no relevant parents
562 * (and multiple irrelevant ones), then we can't select a parent here
566 struct commit
*commit
= list
->item
;
568 if (relevant_commit(commit
)) {
578 * The goal is to get REV_TREE_NEW as the result only if the
579 * diff consists of all '+' (and no other changes), REV_TREE_OLD
580 * if the whole diff is removal of old data, and otherwise
581 * REV_TREE_DIFFERENT (of course if the trees are the same we
582 * want REV_TREE_SAME).
584 * The only time we care about the distinction is when
585 * remove_empty_trees is in effect, in which case we care only about
586 * whether the whole change is REV_TREE_NEW, or if there's another type
587 * of change. Which means we can stop the diff early in either of these
590 * 1. We're not using remove_empty_trees at all.
592 * 2. We saw anything except REV_TREE_NEW.
594 static int tree_difference
= REV_TREE_SAME
;
596 static void file_add_remove(struct diff_options
*options
,
597 int addremove
, unsigned mode
,
598 const struct object_id
*oid
,
600 const char *fullpath
, unsigned dirty_submodule
)
602 int diff
= addremove
== '+' ? REV_TREE_NEW
: REV_TREE_OLD
;
603 struct rev_info
*revs
= options
->change_fn_data
;
605 tree_difference
|= diff
;
606 if (!revs
->remove_empty_trees
|| tree_difference
!= REV_TREE_NEW
)
607 options
->flags
.has_changes
= 1;
610 static void file_change(struct diff_options
*options
,
611 unsigned old_mode
, unsigned new_mode
,
612 const struct object_id
*old_oid
,
613 const struct object_id
*new_oid
,
614 int old_oid_valid
, int new_oid_valid
,
615 const char *fullpath
,
616 unsigned old_dirty_submodule
, unsigned new_dirty_submodule
)
618 tree_difference
= REV_TREE_DIFFERENT
;
619 options
->flags
.has_changes
= 1;
622 static int rev_compare_tree(struct rev_info
*revs
,
623 struct commit
*parent
, struct commit
*commit
)
625 struct tree
*t1
= get_commit_tree(parent
);
626 struct tree
*t2
= get_commit_tree(commit
);
633 if (revs
->simplify_by_decoration
) {
635 * If we are simplifying by decoration, then the commit
636 * is worth showing if it has a tag pointing at it.
638 if (get_name_decoration(&commit
->object
))
639 return REV_TREE_DIFFERENT
;
641 * A commit that is not pointed by a tag is uninteresting
642 * if we are not limited by path. This means that you will
643 * see the usual "commits that touch the paths" plus any
644 * tagged commit by specifying both --simplify-by-decoration
647 if (!revs
->prune_data
.nr
)
648 return REV_TREE_SAME
;
651 tree_difference
= REV_TREE_SAME
;
652 revs
->pruning
.flags
.has_changes
= 0;
653 if (diff_tree_oid(&t1
->object
.oid
, &t2
->object
.oid
, "",
655 return REV_TREE_DIFFERENT
;
656 return tree_difference
;
659 static int rev_same_tree_as_empty(struct rev_info
*revs
, struct commit
*commit
)
662 struct tree
*t1
= get_commit_tree(commit
);
667 tree_difference
= REV_TREE_SAME
;
668 revs
->pruning
.flags
.has_changes
= 0;
669 retval
= diff_tree_oid(NULL
, &t1
->object
.oid
, "", &revs
->pruning
);
671 return retval
>= 0 && (tree_difference
== REV_TREE_SAME
);
674 struct treesame_state
{
675 unsigned int nparents
;
676 unsigned char treesame
[FLEX_ARRAY
];
679 static struct treesame_state
*initialise_treesame(struct rev_info
*revs
, struct commit
*commit
)
681 unsigned n
= commit_list_count(commit
->parents
);
682 struct treesame_state
*st
= xcalloc(1, st_add(sizeof(*st
), n
));
684 add_decoration(&revs
->treesame
, &commit
->object
, st
);
689 * Must be called immediately after removing the nth_parent from a commit's
690 * parent list, if we are maintaining the per-parent treesame[] decoration.
691 * This does not recalculate the master TREESAME flag - update_treesame()
692 * should be called to update it after a sequence of treesame[] modifications
693 * that may have affected it.
695 static int compact_treesame(struct rev_info
*revs
, struct commit
*commit
, unsigned nth_parent
)
697 struct treesame_state
*st
;
700 if (!commit
->parents
) {
702 * Have just removed the only parent from a non-merge.
703 * Different handling, as we lack decoration.
706 die("compact_treesame %u", nth_parent
);
707 old_same
= !!(commit
->object
.flags
& TREESAME
);
708 if (rev_same_tree_as_empty(revs
, commit
))
709 commit
->object
.flags
|= TREESAME
;
711 commit
->object
.flags
&= ~TREESAME
;
715 st
= lookup_decoration(&revs
->treesame
, &commit
->object
);
716 if (!st
|| nth_parent
>= st
->nparents
)
717 die("compact_treesame %u", nth_parent
);
719 old_same
= st
->treesame
[nth_parent
];
720 memmove(st
->treesame
+ nth_parent
,
721 st
->treesame
+ nth_parent
+ 1,
722 st
->nparents
- nth_parent
- 1);
725 * If we've just become a non-merge commit, update TREESAME
726 * immediately, and remove the no-longer-needed decoration.
727 * If still a merge, defer update until update_treesame().
729 if (--st
->nparents
== 1) {
730 if (commit
->parents
->next
)
731 die("compact_treesame parents mismatch");
732 if (st
->treesame
[0] && revs
->dense
)
733 commit
->object
.flags
|= TREESAME
;
735 commit
->object
.flags
&= ~TREESAME
;
736 free(add_decoration(&revs
->treesame
, &commit
->object
, NULL
));
742 static unsigned update_treesame(struct rev_info
*revs
, struct commit
*commit
)
744 if (commit
->parents
&& commit
->parents
->next
) {
746 struct treesame_state
*st
;
747 struct commit_list
*p
;
748 unsigned relevant_parents
;
749 unsigned relevant_change
, irrelevant_change
;
751 st
= lookup_decoration(&revs
->treesame
, &commit
->object
);
753 die("update_treesame %s", oid_to_hex(&commit
->object
.oid
));
754 relevant_parents
= 0;
755 relevant_change
= irrelevant_change
= 0;
756 for (p
= commit
->parents
, n
= 0; p
; n
++, p
= p
->next
) {
757 if (relevant_commit(p
->item
)) {
758 relevant_change
|= !st
->treesame
[n
];
761 irrelevant_change
|= !st
->treesame
[n
];
763 if (relevant_parents
? relevant_change
: irrelevant_change
)
764 commit
->object
.flags
&= ~TREESAME
;
766 commit
->object
.flags
|= TREESAME
;
769 return commit
->object
.flags
& TREESAME
;
772 static inline int limiting_can_increase_treesame(const struct rev_info
*revs
)
775 * TREESAME is irrelevant unless prune && dense;
776 * if simplify_history is set, we can't have a mixture of TREESAME and
777 * !TREESAME INTERESTING parents (and we don't have treesame[]
778 * decoration anyway);
779 * if first_parent_only is set, then the TREESAME flag is locked
780 * against the first parent (and again we lack treesame[] decoration).
782 return revs
->prune
&& revs
->dense
&&
783 !revs
->simplify_history
&&
784 !revs
->first_parent_only
;
787 static void try_to_simplify_commit(struct rev_info
*revs
, struct commit
*commit
)
789 struct commit_list
**pp
, *parent
;
790 struct treesame_state
*ts
= NULL
;
791 int relevant_change
= 0, irrelevant_change
= 0;
792 int relevant_parents
, nth_parent
;
795 * If we don't do pruning, everything is interesting
800 if (!get_commit_tree(commit
))
803 if (!commit
->parents
) {
804 if (rev_same_tree_as_empty(revs
, commit
))
805 commit
->object
.flags
|= TREESAME
;
810 * Normal non-merge commit? If we don't want to make the
811 * history dense, we consider it always to be a change..
813 if (!revs
->dense
&& !commit
->parents
->next
)
816 for (pp
= &commit
->parents
, nth_parent
= 0, relevant_parents
= 0;
817 (parent
= *pp
) != NULL
;
818 pp
= &parent
->next
, nth_parent
++) {
819 struct commit
*p
= parent
->item
;
820 if (relevant_commit(p
))
823 if (nth_parent
== 1) {
825 * This our second loop iteration - so we now know
826 * we're dealing with a merge.
828 * Do not compare with later parents when we care only about
829 * the first parent chain, in order to avoid derailing the
830 * traversal to follow a side branch that brought everything
831 * in the path we are limited to by the pathspec.
833 if (revs
->first_parent_only
)
836 * If this will remain a potentially-simplifiable
837 * merge, remember per-parent treesame if needed.
838 * Initialise the array with the comparison from our
841 if (revs
->treesame
.name
&&
842 !revs
->simplify_history
&&
843 !(commit
->object
.flags
& UNINTERESTING
)) {
844 ts
= initialise_treesame(revs
, commit
);
845 if (!(irrelevant_change
|| relevant_change
))
849 if (parse_commit(p
) < 0)
850 die("cannot simplify commit %s (because of %s)",
851 oid_to_hex(&commit
->object
.oid
),
852 oid_to_hex(&p
->object
.oid
));
853 switch (rev_compare_tree(revs
, p
, commit
)) {
855 if (!revs
->simplify_history
|| !relevant_commit(p
)) {
856 /* Even if a merge with an uninteresting
857 * side branch brought the entire change
858 * we are interested in, we do not want
859 * to lose the other branches of this
860 * merge, so we just keep going.
863 ts
->treesame
[nth_parent
] = 1;
867 commit
->parents
= parent
;
868 commit
->object
.flags
|= TREESAME
;
872 if (revs
->remove_empty_trees
&&
873 rev_same_tree_as_empty(revs
, p
)) {
874 /* We are adding all the specified
875 * paths from this parent, so the
876 * history beyond this parent is not
877 * interesting. Remove its parents
878 * (they are grandparents for us).
879 * IOW, we pretend this parent is a
882 if (parse_commit(p
) < 0)
883 die("cannot simplify commit %s (invalid %s)",
884 oid_to_hex(&commit
->object
.oid
),
885 oid_to_hex(&p
->object
.oid
));
890 case REV_TREE_DIFFERENT
:
891 if (relevant_commit(p
))
894 irrelevant_change
= 1;
897 die("bad tree compare for commit %s", oid_to_hex(&commit
->object
.oid
));
901 * TREESAME is straightforward for single-parent commits. For merge
902 * commits, it is most useful to define it so that "irrelevant"
903 * parents cannot make us !TREESAME - if we have any relevant
904 * parents, then we only consider TREESAMEness with respect to them,
905 * allowing irrelevant merges from uninteresting branches to be
906 * simplified away. Only if we have only irrelevant parents do we
907 * base TREESAME on them. Note that this logic is replicated in
908 * update_treesame, which should be kept in sync.
910 if (relevant_parents
? !relevant_change
: !irrelevant_change
)
911 commit
->object
.flags
|= TREESAME
;
914 static void commit_list_insert_by_date_cached(struct commit
*p
, struct commit_list
**head
,
915 struct commit_list
*cached_base
, struct commit_list
**cache
)
917 struct commit_list
*new_entry
;
919 if (cached_base
&& p
->date
< cached_base
->item
->date
)
920 new_entry
= commit_list_insert_by_date(p
, &cached_base
->next
);
922 new_entry
= commit_list_insert_by_date(p
, head
);
924 if (cache
&& (!*cache
|| p
->date
< (*cache
)->item
->date
))
928 static int process_parents(struct rev_info
*revs
, struct commit
*commit
,
929 struct commit_list
**list
, struct commit_list
**cache_ptr
)
931 struct commit_list
*parent
= commit
->parents
;
933 struct commit_list
*cached_base
= cache_ptr
? *cache_ptr
: NULL
;
935 if (commit
->object
.flags
& ADDED
)
937 commit
->object
.flags
|= ADDED
;
939 if (revs
->include_check
&&
940 !revs
->include_check(commit
, revs
->include_check_data
))
944 * If the commit is uninteresting, don't try to
945 * prune parents - we want the maximal uninteresting
948 * Normally we haven't parsed the parent
949 * yet, so we won't have a parent of a parent
950 * here. However, it may turn out that we've
951 * reached this commit some other way (where it
952 * wasn't uninteresting), in which case we need
953 * to mark its parents recursively too..
955 if (commit
->object
.flags
& UNINTERESTING
) {
957 struct commit
*p
= parent
->item
;
958 parent
= parent
->next
;
960 p
->object
.flags
|= UNINTERESTING
;
961 if (parse_commit_gently(p
, 1) < 0)
964 mark_parents_uninteresting(p
);
965 if (p
->object
.flags
& SEEN
)
967 p
->object
.flags
|= SEEN
;
969 commit_list_insert_by_date_cached(p
, list
, cached_base
, cache_ptr
);
975 * Ok, the commit wasn't uninteresting. Try to
976 * simplify the commit history and find the parent
977 * that has no differences in the path set if one exists.
979 try_to_simplify_commit(revs
, commit
);
984 left_flag
= (commit
->object
.flags
& SYMMETRIC_LEFT
);
986 for (parent
= commit
->parents
; parent
; parent
= parent
->next
) {
987 struct commit
*p
= parent
->item
;
988 int gently
= revs
->ignore_missing_links
||
989 revs
->exclude_promisor_objects
;
990 if (parse_commit_gently(p
, gently
) < 0) {
991 if (revs
->exclude_promisor_objects
&&
992 is_promisor_object(&p
->object
.oid
)) {
993 if (revs
->first_parent_only
)
1000 char **slot
= revision_sources_at(revs
->sources
, p
);
1003 *slot
= *revision_sources_at(revs
->sources
, commit
);
1005 p
->object
.flags
|= left_flag
;
1006 if (!(p
->object
.flags
& SEEN
)) {
1007 p
->object
.flags
|= SEEN
;
1009 commit_list_insert_by_date_cached(p
, list
, cached_base
, cache_ptr
);
1011 if (revs
->first_parent_only
)
1017 static void cherry_pick_list(struct commit_list
*list
, struct rev_info
*revs
)
1019 struct commit_list
*p
;
1020 int left_count
= 0, right_count
= 0;
1022 struct patch_ids ids
;
1023 unsigned cherry_flag
;
1025 /* First count the commits on the left and on the right */
1026 for (p
= list
; p
; p
= p
->next
) {
1027 struct commit
*commit
= p
->item
;
1028 unsigned flags
= commit
->object
.flags
;
1029 if (flags
& BOUNDARY
)
1031 else if (flags
& SYMMETRIC_LEFT
)
1037 if (!left_count
|| !right_count
)
1040 left_first
= left_count
< right_count
;
1041 init_patch_ids(revs
->repo
, &ids
);
1042 ids
.diffopts
.pathspec
= revs
->diffopt
.pathspec
;
1044 /* Compute patch-ids for one side */
1045 for (p
= list
; p
; p
= p
->next
) {
1046 struct commit
*commit
= p
->item
;
1047 unsigned flags
= commit
->object
.flags
;
1049 if (flags
& BOUNDARY
)
1052 * If we have fewer left, left_first is set and we omit
1053 * commits on the right branch in this loop. If we have
1054 * fewer right, we skip the left ones.
1056 if (left_first
!= !!(flags
& SYMMETRIC_LEFT
))
1058 add_commit_patch_id(commit
, &ids
);
1061 /* either cherry_mark or cherry_pick are true */
1062 cherry_flag
= revs
->cherry_mark
? PATCHSAME
: SHOWN
;
1064 /* Check the other side */
1065 for (p
= list
; p
; p
= p
->next
) {
1066 struct commit
*commit
= p
->item
;
1067 struct patch_id
*id
;
1068 unsigned flags
= commit
->object
.flags
;
1070 if (flags
& BOUNDARY
)
1073 * If we have fewer left, left_first is set and we omit
1074 * commits on the left branch in this loop.
1076 if (left_first
== !!(flags
& SYMMETRIC_LEFT
))
1080 * Have we seen the same patch id?
1082 id
= has_commit_patch_id(commit
, &ids
);
1086 commit
->object
.flags
|= cherry_flag
;
1087 id
->commit
->object
.flags
|= cherry_flag
;
1090 free_patch_ids(&ids
);
1093 /* How many extra uninteresting commits we want to see.. */
1096 static int still_interesting(struct commit_list
*src
, timestamp_t date
, int slop
,
1097 struct commit
**interesting_cache
)
1100 * No source list at all? We're definitely done..
1106 * Does the destination list contain entries with a date
1107 * before the source list? Definitely _not_ done.
1109 if (date
<= src
->item
->date
)
1113 * Does the source list still have interesting commits in
1114 * it? Definitely not done..
1116 if (!everybody_uninteresting(src
, interesting_cache
))
1119 /* Ok, we're closing in.. */
1124 * "rev-list --ancestry-path A..B" computes commits that are ancestors
1125 * of B but not ancestors of A but further limits the result to those
1126 * that are descendants of A. This takes the list of bottom commits and
1127 * the result of "A..B" without --ancestry-path, and limits the latter
1128 * further to the ones that can reach one of the commits in "bottom".
1130 static void limit_to_ancestry(struct commit_list
*bottom
, struct commit_list
*list
)
1132 struct commit_list
*p
;
1133 struct commit_list
*rlist
= NULL
;
1137 * Reverse the list so that it will be likely that we would
1138 * process parents before children.
1140 for (p
= list
; p
; p
= p
->next
)
1141 commit_list_insert(p
->item
, &rlist
);
1143 for (p
= bottom
; p
; p
= p
->next
)
1144 p
->item
->object
.flags
|= TMP_MARK
;
1147 * Mark the ones that can reach bottom commits in "list",
1148 * in a bottom-up fashion.
1152 for (p
= rlist
; p
; p
= p
->next
) {
1153 struct commit
*c
= p
->item
;
1154 struct commit_list
*parents
;
1155 if (c
->object
.flags
& (TMP_MARK
| UNINTERESTING
))
1157 for (parents
= c
->parents
;
1159 parents
= parents
->next
) {
1160 if (!(parents
->item
->object
.flags
& TMP_MARK
))
1162 c
->object
.flags
|= TMP_MARK
;
1167 } while (made_progress
);
1170 * NEEDSWORK: decide if we want to remove parents that are
1171 * not marked with TMP_MARK from commit->parents for commits
1172 * in the resulting list. We may not want to do that, though.
1176 * The ones that are not marked with TMP_MARK are uninteresting
1178 for (p
= list
; p
; p
= p
->next
) {
1179 struct commit
*c
= p
->item
;
1180 if (c
->object
.flags
& TMP_MARK
)
1182 c
->object
.flags
|= UNINTERESTING
;
1185 /* We are done with the TMP_MARK */
1186 for (p
= list
; p
; p
= p
->next
)
1187 p
->item
->object
.flags
&= ~TMP_MARK
;
1188 for (p
= bottom
; p
; p
= p
->next
)
1189 p
->item
->object
.flags
&= ~TMP_MARK
;
1190 free_commit_list(rlist
);
1194 * Before walking the history, keep the set of "negative" refs the
1195 * caller has asked to exclude.
1197 * This is used to compute "rev-list --ancestry-path A..B", as we need
1198 * to filter the result of "A..B" further to the ones that can actually
1201 static struct commit_list
*collect_bottom_commits(struct commit_list
*list
)
1203 struct commit_list
*elem
, *bottom
= NULL
;
1204 for (elem
= list
; elem
; elem
= elem
->next
)
1205 if (elem
->item
->object
.flags
& BOTTOM
)
1206 commit_list_insert(elem
->item
, &bottom
);
1210 /* Assumes either left_only or right_only is set */
1211 static void limit_left_right(struct commit_list
*list
, struct rev_info
*revs
)
1213 struct commit_list
*p
;
1215 for (p
= list
; p
; p
= p
->next
) {
1216 struct commit
*commit
= p
->item
;
1218 if (revs
->right_only
) {
1219 if (commit
->object
.flags
& SYMMETRIC_LEFT
)
1220 commit
->object
.flags
|= SHOWN
;
1221 } else /* revs->left_only is set */
1222 if (!(commit
->object
.flags
& SYMMETRIC_LEFT
))
1223 commit
->object
.flags
|= SHOWN
;
1227 static int limit_list(struct rev_info
*revs
)
1230 timestamp_t date
= TIME_MAX
;
1231 struct commit_list
*list
= revs
->commits
;
1232 struct commit_list
*newlist
= NULL
;
1233 struct commit_list
**p
= &newlist
;
1234 struct commit_list
*bottom
= NULL
;
1235 struct commit
*interesting_cache
= NULL
;
1237 if (revs
->ancestry_path
) {
1238 bottom
= collect_bottom_commits(list
);
1240 die("--ancestry-path given but there are no bottom commits");
1244 struct commit
*commit
= pop_commit(&list
);
1245 struct object
*obj
= &commit
->object
;
1246 show_early_output_fn_t show
;
1248 if (commit
== interesting_cache
)
1249 interesting_cache
= NULL
;
1251 if (revs
->max_age
!= -1 && (commit
->date
< revs
->max_age
))
1252 obj
->flags
|= UNINTERESTING
;
1253 if (process_parents(revs
, commit
, &list
, NULL
) < 0)
1255 if (obj
->flags
& UNINTERESTING
) {
1256 mark_parents_uninteresting(commit
);
1257 slop
= still_interesting(list
, date
, slop
, &interesting_cache
);
1262 if (revs
->min_age
!= -1 && (commit
->date
> revs
->min_age
))
1264 date
= commit
->date
;
1265 p
= &commit_list_insert(commit
, p
)->next
;
1267 show
= show_early_output
;
1271 show(revs
, newlist
);
1272 show_early_output
= NULL
;
1274 if (revs
->cherry_pick
|| revs
->cherry_mark
)
1275 cherry_pick_list(newlist
, revs
);
1277 if (revs
->left_only
|| revs
->right_only
)
1278 limit_left_right(newlist
, revs
);
1281 limit_to_ancestry(bottom
, newlist
);
1282 free_commit_list(bottom
);
1286 * Check if any commits have become TREESAME by some of their parents
1287 * becoming UNINTERESTING.
1289 if (limiting_can_increase_treesame(revs
))
1290 for (list
= newlist
; list
; list
= list
->next
) {
1291 struct commit
*c
= list
->item
;
1292 if (c
->object
.flags
& (UNINTERESTING
| TREESAME
))
1294 update_treesame(revs
, c
);
1297 revs
->commits
= newlist
;
1302 * Add an entry to refs->cmdline with the specified information.
1305 static void add_rev_cmdline(struct rev_info
*revs
,
1306 struct object
*item
,
1311 struct rev_cmdline_info
*info
= &revs
->cmdline
;
1312 unsigned int nr
= info
->nr
;
1314 ALLOC_GROW(info
->rev
, nr
+ 1, info
->alloc
);
1315 info
->rev
[nr
].item
= item
;
1316 info
->rev
[nr
].name
= xstrdup(name
);
1317 info
->rev
[nr
].whence
= whence
;
1318 info
->rev
[nr
].flags
= flags
;
1322 static void add_rev_cmdline_list(struct rev_info
*revs
,
1323 struct commit_list
*commit_list
,
1327 while (commit_list
) {
1328 struct object
*object
= &commit_list
->item
->object
;
1329 add_rev_cmdline(revs
, object
, oid_to_hex(&object
->oid
),
1331 commit_list
= commit_list
->next
;
1335 struct all_refs_cb
{
1337 int warned_bad_reflog
;
1338 struct rev_info
*all_revs
;
1339 const char *name_for_errormsg
;
1340 struct worktree
*wt
;
1343 int ref_excluded(struct string_list
*ref_excludes
, const char *path
)
1345 struct string_list_item
*item
;
1349 for_each_string_list_item(item
, ref_excludes
) {
1350 if (!wildmatch(item
->string
, path
, 0))
1356 static int handle_one_ref(const char *path
, const struct object_id
*oid
,
1357 int flag
, void *cb_data
)
1359 struct all_refs_cb
*cb
= cb_data
;
1360 struct object
*object
;
1362 if (ref_excluded(cb
->all_revs
->ref_excludes
, path
))
1365 object
= get_reference(cb
->all_revs
, path
, oid
, cb
->all_flags
);
1366 add_rev_cmdline(cb
->all_revs
, object
, path
, REV_CMD_REF
, cb
->all_flags
);
1367 add_pending_oid(cb
->all_revs
, path
, oid
, cb
->all_flags
);
1371 static void init_all_refs_cb(struct all_refs_cb
*cb
, struct rev_info
*revs
,
1374 cb
->all_revs
= revs
;
1375 cb
->all_flags
= flags
;
1376 revs
->rev_input_given
= 1;
1380 void clear_ref_exclusion(struct string_list
**ref_excludes_p
)
1382 if (*ref_excludes_p
) {
1383 string_list_clear(*ref_excludes_p
, 0);
1384 free(*ref_excludes_p
);
1386 *ref_excludes_p
= NULL
;
1389 void add_ref_exclusion(struct string_list
**ref_excludes_p
, const char *exclude
)
1391 if (!*ref_excludes_p
) {
1392 *ref_excludes_p
= xcalloc(1, sizeof(**ref_excludes_p
));
1393 (*ref_excludes_p
)->strdup_strings
= 1;
1395 string_list_append(*ref_excludes_p
, exclude
);
1398 static void handle_refs(struct ref_store
*refs
,
1399 struct rev_info
*revs
, unsigned flags
,
1400 int (*for_each
)(struct ref_store
*, each_ref_fn
, void *))
1402 struct all_refs_cb cb
;
1405 /* this could happen with uninitialized submodules */
1409 init_all_refs_cb(&cb
, revs
, flags
);
1410 for_each(refs
, handle_one_ref
, &cb
);
1413 static void handle_one_reflog_commit(struct object_id
*oid
, void *cb_data
)
1415 struct all_refs_cb
*cb
= cb_data
;
1416 if (!is_null_oid(oid
)) {
1417 struct object
*o
= parse_object(cb
->all_revs
->repo
, oid
);
1419 o
->flags
|= cb
->all_flags
;
1420 /* ??? CMDLINEFLAGS ??? */
1421 add_pending_object(cb
->all_revs
, o
, "");
1423 else if (!cb
->warned_bad_reflog
) {
1424 warning("reflog of '%s' references pruned commits",
1425 cb
->name_for_errormsg
);
1426 cb
->warned_bad_reflog
= 1;
1431 static int handle_one_reflog_ent(struct object_id
*ooid
, struct object_id
*noid
,
1432 const char *email
, timestamp_t timestamp
, int tz
,
1433 const char *message
, void *cb_data
)
1435 handle_one_reflog_commit(ooid
, cb_data
);
1436 handle_one_reflog_commit(noid
, cb_data
);
1440 static int handle_one_reflog(const char *refname_in_wt
,
1441 const struct object_id
*oid
,
1442 int flag
, void *cb_data
)
1444 struct all_refs_cb
*cb
= cb_data
;
1445 struct strbuf refname
= STRBUF_INIT
;
1447 cb
->warned_bad_reflog
= 0;
1448 strbuf_worktree_ref(cb
->wt
, &refname
, refname_in_wt
);
1449 cb
->name_for_errormsg
= refname
.buf
;
1450 refs_for_each_reflog_ent(get_main_ref_store(the_repository
),
1452 handle_one_reflog_ent
, cb_data
);
1453 strbuf_release(&refname
);
1457 static void add_other_reflogs_to_pending(struct all_refs_cb
*cb
)
1459 struct worktree
**worktrees
, **p
;
1461 worktrees
= get_worktrees(0);
1462 for (p
= worktrees
; *p
; p
++) {
1463 struct worktree
*wt
= *p
;
1469 refs_for_each_reflog(get_worktree_ref_store(wt
),
1473 free_worktrees(worktrees
);
1476 void add_reflogs_to_pending(struct rev_info
*revs
, unsigned flags
)
1478 struct all_refs_cb cb
;
1481 cb
.all_flags
= flags
;
1483 for_each_reflog(handle_one_reflog
, &cb
);
1485 if (!revs
->single_worktree
)
1486 add_other_reflogs_to_pending(&cb
);
1489 static void add_cache_tree(struct cache_tree
*it
, struct rev_info
*revs
,
1490 struct strbuf
*path
, unsigned int flags
)
1492 size_t baselen
= path
->len
;
1495 if (it
->entry_count
>= 0) {
1496 struct tree
*tree
= lookup_tree(revs
->repo
, &it
->oid
);
1497 tree
->object
.flags
|= flags
;
1498 add_pending_object_with_path(revs
, &tree
->object
, "",
1502 for (i
= 0; i
< it
->subtree_nr
; i
++) {
1503 struct cache_tree_sub
*sub
= it
->down
[i
];
1504 strbuf_addf(path
, "%s%s", baselen
? "/" : "", sub
->name
);
1505 add_cache_tree(sub
->cache_tree
, revs
, path
, flags
);
1506 strbuf_setlen(path
, baselen
);
1511 static void do_add_index_objects_to_pending(struct rev_info
*revs
,
1512 struct index_state
*istate
,
1517 for (i
= 0; i
< istate
->cache_nr
; i
++) {
1518 struct cache_entry
*ce
= istate
->cache
[i
];
1521 if (S_ISGITLINK(ce
->ce_mode
))
1524 blob
= lookup_blob(revs
->repo
, &ce
->oid
);
1526 die("unable to add index blob to traversal");
1527 blob
->object
.flags
|= flags
;
1528 add_pending_object_with_path(revs
, &blob
->object
, "",
1529 ce
->ce_mode
, ce
->name
);
1532 if (istate
->cache_tree
) {
1533 struct strbuf path
= STRBUF_INIT
;
1534 add_cache_tree(istate
->cache_tree
, revs
, &path
, flags
);
1535 strbuf_release(&path
);
1539 void add_index_objects_to_pending(struct rev_info
*revs
, unsigned int flags
)
1541 struct worktree
**worktrees
, **p
;
1543 repo_read_index(revs
->repo
);
1544 do_add_index_objects_to_pending(revs
, revs
->repo
->index
, flags
);
1546 if (revs
->single_worktree
)
1549 worktrees
= get_worktrees(0);
1550 for (p
= worktrees
; *p
; p
++) {
1551 struct worktree
*wt
= *p
;
1552 struct index_state istate
= { NULL
};
1555 continue; /* current index already taken care of */
1557 if (read_index_from(&istate
,
1558 worktree_git_path(wt
, "index"),
1559 get_worktree_git_dir(wt
)) > 0)
1560 do_add_index_objects_to_pending(revs
, &istate
, flags
);
1561 discard_index(&istate
);
1563 free_worktrees(worktrees
);
1566 static int add_parents_only(struct rev_info
*revs
, const char *arg_
, int flags
,
1569 struct object_id oid
;
1571 struct commit
*commit
;
1572 struct commit_list
*parents
;
1574 const char *arg
= arg_
;
1577 flags
^= UNINTERESTING
| BOTTOM
;
1580 if (get_oid_committish(arg
, &oid
))
1583 it
= get_reference(revs
, arg
, &oid
, 0);
1584 if (!it
&& revs
->ignore_missing
)
1586 if (it
->type
!= OBJ_TAG
)
1588 if (!((struct tag
*)it
)->tagged
)
1590 oidcpy(&oid
, &((struct tag
*)it
)->tagged
->oid
);
1592 if (it
->type
!= OBJ_COMMIT
)
1594 commit
= (struct commit
*)it
;
1595 if (exclude_parent
&&
1596 exclude_parent
> commit_list_count(commit
->parents
))
1598 for (parents
= commit
->parents
, parent_number
= 1;
1600 parents
= parents
->next
, parent_number
++) {
1601 if (exclude_parent
&& parent_number
!= exclude_parent
)
1604 it
= &parents
->item
->object
;
1606 add_rev_cmdline(revs
, it
, arg_
, REV_CMD_PARENTS_ONLY
, flags
);
1607 add_pending_object(revs
, it
, arg
);
1612 void repo_init_revisions(struct repository
*r
,
1613 struct rev_info
*revs
,
1616 memset(revs
, 0, sizeof(*revs
));
1619 revs
->abbrev
= DEFAULT_ABBREV
;
1620 revs
->ignore_merges
= 1;
1621 revs
->simplify_history
= 1;
1622 revs
->pruning
.repo
= r
;
1623 revs
->pruning
.flags
.recursive
= 1;
1624 revs
->pruning
.flags
.quick
= 1;
1625 revs
->pruning
.add_remove
= file_add_remove
;
1626 revs
->pruning
.change
= file_change
;
1627 revs
->pruning
.change_fn_data
= revs
;
1628 revs
->sort_order
= REV_SORT_IN_GRAPH_ORDER
;
1630 revs
->prefix
= prefix
;
1633 revs
->skip_count
= -1;
1634 revs
->max_count
= -1;
1635 revs
->max_parents
= -1;
1636 revs
->expand_tabs_in_log
= -1;
1638 revs
->commit_format
= CMIT_FMT_DEFAULT
;
1639 revs
->expand_tabs_in_log_default
= 8;
1641 init_grep_defaults(revs
->repo
);
1642 grep_init(&revs
->grep_filter
, revs
->repo
, prefix
);
1643 revs
->grep_filter
.status_only
= 1;
1645 repo_diff_setup(revs
->repo
, &revs
->diffopt
);
1646 if (prefix
&& !revs
->diffopt
.prefix
) {
1647 revs
->diffopt
.prefix
= prefix
;
1648 revs
->diffopt
.prefix_length
= strlen(prefix
);
1651 revs
->notes_opt
.use_default_notes
= -1;
1654 static void add_pending_commit_list(struct rev_info
*revs
,
1655 struct commit_list
*commit_list
,
1658 while (commit_list
) {
1659 struct object
*object
= &commit_list
->item
->object
;
1660 object
->flags
|= flags
;
1661 add_pending_object(revs
, object
, oid_to_hex(&object
->oid
));
1662 commit_list
= commit_list
->next
;
1666 static void prepare_show_merge(struct rev_info
*revs
)
1668 struct commit_list
*bases
;
1669 struct commit
*head
, *other
;
1670 struct object_id oid
;
1671 const char **prune
= NULL
;
1672 int i
, prune_num
= 1; /* counting terminating NULL */
1673 struct index_state
*istate
= revs
->repo
->index
;
1675 if (get_oid("HEAD", &oid
))
1676 die("--merge without HEAD?");
1677 head
= lookup_commit_or_die(&oid
, "HEAD");
1678 if (get_oid("MERGE_HEAD", &oid
))
1679 die("--merge without MERGE_HEAD?");
1680 other
= lookup_commit_or_die(&oid
, "MERGE_HEAD");
1681 add_pending_object(revs
, &head
->object
, "HEAD");
1682 add_pending_object(revs
, &other
->object
, "MERGE_HEAD");
1683 bases
= get_merge_bases(head
, other
);
1684 add_rev_cmdline_list(revs
, bases
, REV_CMD_MERGE_BASE
, UNINTERESTING
| BOTTOM
);
1685 add_pending_commit_list(revs
, bases
, UNINTERESTING
| BOTTOM
);
1686 free_commit_list(bases
);
1687 head
->object
.flags
|= SYMMETRIC_LEFT
;
1689 if (!istate
->cache_nr
)
1690 repo_read_index(revs
->repo
);
1691 for (i
= 0; i
< istate
->cache_nr
; i
++) {
1692 const struct cache_entry
*ce
= istate
->cache
[i
];
1695 if (ce_path_match(istate
, ce
, &revs
->prune_data
, NULL
)) {
1697 REALLOC_ARRAY(prune
, prune_num
);
1698 prune
[prune_num
-2] = ce
->name
;
1699 prune
[prune_num
-1] = NULL
;
1701 while ((i
+1 < istate
->cache_nr
) &&
1702 ce_same_name(ce
, istate
->cache
[i
+1]))
1705 clear_pathspec(&revs
->prune_data
);
1706 parse_pathspec(&revs
->prune_data
, PATHSPEC_ALL_MAGIC
& ~PATHSPEC_LITERAL
,
1707 PATHSPEC_PREFER_FULL
| PATHSPEC_LITERAL_PATH
, "", prune
);
1711 static int dotdot_missing(const char *arg
, char *dotdot
,
1712 struct rev_info
*revs
, int symmetric
)
1714 if (revs
->ignore_missing
)
1716 /* de-munge so we report the full argument */
1719 ? "Invalid symmetric difference expression %s"
1720 : "Invalid revision range %s", arg
);
1723 static int handle_dotdot_1(const char *arg
, char *dotdot
,
1724 struct rev_info
*revs
, int flags
,
1725 int cant_be_filename
,
1726 struct object_context
*a_oc
,
1727 struct object_context
*b_oc
)
1729 const char *a_name
, *b_name
;
1730 struct object_id a_oid
, b_oid
;
1731 struct object
*a_obj
, *b_obj
;
1732 unsigned int a_flags
, b_flags
;
1734 unsigned int flags_exclude
= flags
^ (UNINTERESTING
| BOTTOM
);
1735 unsigned int oc_flags
= GET_OID_COMMITTISH
| GET_OID_RECORD_PATH
;
1741 b_name
= dotdot
+ 2;
1742 if (*b_name
== '.') {
1749 if (get_oid_with_context(revs
->repo
, a_name
, oc_flags
, &a_oid
, a_oc
) ||
1750 get_oid_with_context(revs
->repo
, b_name
, oc_flags
, &b_oid
, b_oc
))
1753 if (!cant_be_filename
) {
1755 verify_non_filename(revs
->prefix
, arg
);
1759 a_obj
= parse_object(revs
->repo
, &a_oid
);
1760 b_obj
= parse_object(revs
->repo
, &b_oid
);
1761 if (!a_obj
|| !b_obj
)
1762 return dotdot_missing(arg
, dotdot
, revs
, symmetric
);
1767 a_flags
= flags_exclude
;
1769 /* A...B -- find merge bases between the two */
1770 struct commit
*a
, *b
;
1771 struct commit_list
*exclude
;
1773 a
= lookup_commit_reference(revs
->repo
, &a_obj
->oid
);
1774 b
= lookup_commit_reference(revs
->repo
, &b_obj
->oid
);
1776 return dotdot_missing(arg
, dotdot
, revs
, symmetric
);
1778 exclude
= get_merge_bases(a
, b
);
1779 add_rev_cmdline_list(revs
, exclude
, REV_CMD_MERGE_BASE
,
1781 add_pending_commit_list(revs
, exclude
, flags_exclude
);
1782 free_commit_list(exclude
);
1785 a_flags
= flags
| SYMMETRIC_LEFT
;
1788 a_obj
->flags
|= a_flags
;
1789 b_obj
->flags
|= b_flags
;
1790 add_rev_cmdline(revs
, a_obj
, a_name
, REV_CMD_LEFT
, a_flags
);
1791 add_rev_cmdline(revs
, b_obj
, b_name
, REV_CMD_RIGHT
, b_flags
);
1792 add_pending_object_with_path(revs
, a_obj
, a_name
, a_oc
->mode
, a_oc
->path
);
1793 add_pending_object_with_path(revs
, b_obj
, b_name
, b_oc
->mode
, b_oc
->path
);
1797 static int handle_dotdot(const char *arg
,
1798 struct rev_info
*revs
, int flags
,
1799 int cant_be_filename
)
1801 struct object_context a_oc
, b_oc
;
1802 char *dotdot
= strstr(arg
, "..");
1808 memset(&a_oc
, 0, sizeof(a_oc
));
1809 memset(&b_oc
, 0, sizeof(b_oc
));
1812 ret
= handle_dotdot_1(arg
, dotdot
, revs
, flags
, cant_be_filename
,
1822 int handle_revision_arg(const char *arg_
, struct rev_info
*revs
, int flags
, unsigned revarg_opt
)
1824 struct object_context oc
;
1826 struct object
*object
;
1827 struct object_id oid
;
1829 const char *arg
= arg_
;
1830 int cant_be_filename
= revarg_opt
& REVARG_CANNOT_BE_FILENAME
;
1831 unsigned get_sha1_flags
= GET_OID_RECORD_PATH
;
1833 flags
= flags
& UNINTERESTING
? flags
| BOTTOM
: flags
& ~BOTTOM
;
1835 if (!cant_be_filename
&& !strcmp(arg
, "..")) {
1837 * Just ".."? That is not a range but the
1838 * pathspec for the parent directory.
1843 if (!handle_dotdot(arg
, revs
, flags
, revarg_opt
))
1846 mark
= strstr(arg
, "^@");
1847 if (mark
&& !mark
[2]) {
1849 if (add_parents_only(revs
, arg
, flags
, 0))
1853 mark
= strstr(arg
, "^!");
1854 if (mark
&& !mark
[2]) {
1856 if (!add_parents_only(revs
, arg
, flags
^ (UNINTERESTING
| BOTTOM
), 0))
1859 mark
= strstr(arg
, "^-");
1861 int exclude_parent
= 1;
1865 exclude_parent
= strtoul(mark
+ 2, &end
, 10);
1866 if (*end
!= '\0' || !exclude_parent
)
1871 if (!add_parents_only(revs
, arg
, flags
^ (UNINTERESTING
| BOTTOM
), exclude_parent
))
1877 local_flags
= UNINTERESTING
| BOTTOM
;
1881 if (revarg_opt
& REVARG_COMMITTISH
)
1882 get_sha1_flags
|= GET_OID_COMMITTISH
;
1884 if (get_oid_with_context(revs
->repo
, arg
, get_sha1_flags
, &oid
, &oc
))
1885 return revs
->ignore_missing
? 0 : -1;
1886 if (!cant_be_filename
)
1887 verify_non_filename(revs
->prefix
, arg
);
1888 object
= get_reference(revs
, arg
, &oid
, flags
^ local_flags
);
1890 return revs
->ignore_missing
? 0 : -1;
1891 add_rev_cmdline(revs
, object
, arg_
, REV_CMD_REV
, flags
^ local_flags
);
1892 add_pending_object_with_path(revs
, object
, arg
, oc
.mode
, oc
.path
);
1897 static void read_pathspec_from_stdin(struct rev_info
*revs
, struct strbuf
*sb
,
1898 struct argv_array
*prune
)
1900 while (strbuf_getline(sb
, stdin
) != EOF
)
1901 argv_array_push(prune
, sb
->buf
);
1904 static void read_revisions_from_stdin(struct rev_info
*revs
,
1905 struct argv_array
*prune
)
1908 int seen_dashdash
= 0;
1911 save_warning
= warn_on_object_refname_ambiguity
;
1912 warn_on_object_refname_ambiguity
= 0;
1914 strbuf_init(&sb
, 1000);
1915 while (strbuf_getline(&sb
, stdin
) != EOF
) {
1919 if (sb
.buf
[0] == '-') {
1920 if (len
== 2 && sb
.buf
[1] == '-') {
1924 die("options not supported in --stdin mode");
1926 if (handle_revision_arg(sb
.buf
, revs
, 0,
1927 REVARG_CANNOT_BE_FILENAME
))
1928 die("bad revision '%s'", sb
.buf
);
1931 read_pathspec_from_stdin(revs
, &sb
, prune
);
1933 strbuf_release(&sb
);
1934 warn_on_object_refname_ambiguity
= save_warning
;
1937 static void add_grep(struct rev_info
*revs
, const char *ptn
, enum grep_pat_token what
)
1939 append_grep_pattern(&revs
->grep_filter
, ptn
, "command line", 0, what
);
1942 static void add_header_grep(struct rev_info
*revs
, enum grep_header_field field
, const char *pattern
)
1944 append_header_grep_pattern(&revs
->grep_filter
, field
, pattern
);
1947 static void add_message_grep(struct rev_info
*revs
, const char *pattern
)
1949 add_grep(revs
, pattern
, GREP_PATTERN_BODY
);
1952 static int handle_revision_opt(struct rev_info
*revs
, int argc
, const char **argv
,
1953 int *unkc
, const char **unkv
,
1954 const struct setup_revision_opt
* opt
)
1956 const char *arg
= argv
[0];
1959 const unsigned hexsz
= the_hash_algo
->hexsz
;
1961 /* pseudo revision arguments */
1962 if (!strcmp(arg
, "--all") || !strcmp(arg
, "--branches") ||
1963 !strcmp(arg
, "--tags") || !strcmp(arg
, "--remotes") ||
1964 !strcmp(arg
, "--reflog") || !strcmp(arg
, "--not") ||
1965 !strcmp(arg
, "--no-walk") || !strcmp(arg
, "--do-walk") ||
1966 !strcmp(arg
, "--bisect") || starts_with(arg
, "--glob=") ||
1967 !strcmp(arg
, "--indexed-objects") ||
1968 starts_with(arg
, "--exclude=") ||
1969 starts_with(arg
, "--branches=") || starts_with(arg
, "--tags=") ||
1970 starts_with(arg
, "--remotes=") || starts_with(arg
, "--no-walk="))
1972 unkv
[(*unkc
)++] = arg
;
1976 if ((argcount
= parse_long_opt("max-count", argv
, &optarg
))) {
1977 revs
->max_count
= atoi(optarg
);
1980 } else if ((argcount
= parse_long_opt("skip", argv
, &optarg
))) {
1981 revs
->skip_count
= atoi(optarg
);
1983 } else if ((*arg
== '-') && isdigit(arg
[1])) {
1984 /* accept -<digit>, like traditional "head" */
1985 if (strtol_i(arg
+ 1, 10, &revs
->max_count
) < 0 ||
1986 revs
->max_count
< 0)
1987 die("'%s': not a non-negative integer", arg
+ 1);
1989 } else if (!strcmp(arg
, "-n")) {
1991 return error("-n requires an argument");
1992 revs
->max_count
= atoi(argv
[1]);
1995 } else if (skip_prefix(arg
, "-n", &optarg
)) {
1996 revs
->max_count
= atoi(optarg
);
1998 } else if ((argcount
= parse_long_opt("max-age", argv
, &optarg
))) {
1999 revs
->max_age
= atoi(optarg
);
2001 } else if ((argcount
= parse_long_opt("since", argv
, &optarg
))) {
2002 revs
->max_age
= approxidate(optarg
);
2004 } else if ((argcount
= parse_long_opt("after", argv
, &optarg
))) {
2005 revs
->max_age
= approxidate(optarg
);
2007 } else if ((argcount
= parse_long_opt("min-age", argv
, &optarg
))) {
2008 revs
->min_age
= atoi(optarg
);
2010 } else if ((argcount
= parse_long_opt("before", argv
, &optarg
))) {
2011 revs
->min_age
= approxidate(optarg
);
2013 } else if ((argcount
= parse_long_opt("until", argv
, &optarg
))) {
2014 revs
->min_age
= approxidate(optarg
);
2016 } else if (!strcmp(arg
, "--first-parent")) {
2017 revs
->first_parent_only
= 1;
2018 } else if (!strcmp(arg
, "--ancestry-path")) {
2019 revs
->ancestry_path
= 1;
2020 revs
->simplify_history
= 0;
2022 } else if (!strcmp(arg
, "-g") || !strcmp(arg
, "--walk-reflogs")) {
2023 init_reflog_walk(&revs
->reflog_info
);
2024 } else if (!strcmp(arg
, "--default")) {
2026 return error("bad --default argument");
2027 revs
->def
= argv
[1];
2029 } else if (!strcmp(arg
, "--merge")) {
2030 revs
->show_merge
= 1;
2031 } else if (!strcmp(arg
, "--topo-order")) {
2032 revs
->sort_order
= REV_SORT_IN_GRAPH_ORDER
;
2033 revs
->topo_order
= 1;
2034 } else if (!strcmp(arg
, "--simplify-merges")) {
2035 revs
->simplify_merges
= 1;
2036 revs
->topo_order
= 1;
2037 revs
->rewrite_parents
= 1;
2038 revs
->simplify_history
= 0;
2040 } else if (!strcmp(arg
, "--simplify-by-decoration")) {
2041 revs
->simplify_merges
= 1;
2042 revs
->topo_order
= 1;
2043 revs
->rewrite_parents
= 1;
2044 revs
->simplify_history
= 0;
2045 revs
->simplify_by_decoration
= 1;
2048 load_ref_decorations(NULL
, DECORATE_SHORT_REFS
);
2049 } else if (!strcmp(arg
, "--date-order")) {
2050 revs
->sort_order
= REV_SORT_BY_COMMIT_DATE
;
2051 revs
->topo_order
= 1;
2052 } else if (!strcmp(arg
, "--author-date-order")) {
2053 revs
->sort_order
= REV_SORT_BY_AUTHOR_DATE
;
2054 revs
->topo_order
= 1;
2055 } else if (!strcmp(arg
, "--early-output")) {
2056 revs
->early_output
= 100;
2057 revs
->topo_order
= 1;
2058 } else if (skip_prefix(arg
, "--early-output=", &optarg
)) {
2059 if (strtoul_ui(optarg
, 10, &revs
->early_output
) < 0)
2060 die("'%s': not a non-negative integer", optarg
);
2061 revs
->topo_order
= 1;
2062 } else if (!strcmp(arg
, "--parents")) {
2063 revs
->rewrite_parents
= 1;
2064 revs
->print_parents
= 1;
2065 } else if (!strcmp(arg
, "--dense")) {
2067 } else if (!strcmp(arg
, "--sparse")) {
2069 } else if (!strcmp(arg
, "--in-commit-order")) {
2070 revs
->tree_blobs_in_commit_order
= 1;
2071 } else if (!strcmp(arg
, "--remove-empty")) {
2072 revs
->remove_empty_trees
= 1;
2073 } else if (!strcmp(arg
, "--merges")) {
2074 revs
->min_parents
= 2;
2075 } else if (!strcmp(arg
, "--no-merges")) {
2076 revs
->max_parents
= 1;
2077 } else if (skip_prefix(arg
, "--min-parents=", &optarg
)) {
2078 revs
->min_parents
= atoi(optarg
);
2079 } else if (!strcmp(arg
, "--no-min-parents")) {
2080 revs
->min_parents
= 0;
2081 } else if (skip_prefix(arg
, "--max-parents=", &optarg
)) {
2082 revs
->max_parents
= atoi(optarg
);
2083 } else if (!strcmp(arg
, "--no-max-parents")) {
2084 revs
->max_parents
= -1;
2085 } else if (!strcmp(arg
, "--boundary")) {
2087 } else if (!strcmp(arg
, "--left-right")) {
2088 revs
->left_right
= 1;
2089 } else if (!strcmp(arg
, "--left-only")) {
2090 if (revs
->right_only
)
2091 die("--left-only is incompatible with --right-only"
2093 revs
->left_only
= 1;
2094 } else if (!strcmp(arg
, "--right-only")) {
2095 if (revs
->left_only
)
2096 die("--right-only is incompatible with --left-only");
2097 revs
->right_only
= 1;
2098 } else if (!strcmp(arg
, "--cherry")) {
2099 if (revs
->left_only
)
2100 die("--cherry is incompatible with --left-only");
2101 revs
->cherry_mark
= 1;
2102 revs
->right_only
= 1;
2103 revs
->max_parents
= 1;
2105 } else if (!strcmp(arg
, "--count")) {
2107 } else if (!strcmp(arg
, "--cherry-mark")) {
2108 if (revs
->cherry_pick
)
2109 die("--cherry-mark is incompatible with --cherry-pick");
2110 revs
->cherry_mark
= 1;
2111 revs
->limited
= 1; /* needs limit_list() */
2112 } else if (!strcmp(arg
, "--cherry-pick")) {
2113 if (revs
->cherry_mark
)
2114 die("--cherry-pick is incompatible with --cherry-mark");
2115 revs
->cherry_pick
= 1;
2117 } else if (!strcmp(arg
, "--objects")) {
2118 revs
->tag_objects
= 1;
2119 revs
->tree_objects
= 1;
2120 revs
->blob_objects
= 1;
2121 } else if (!strcmp(arg
, "--objects-edge")) {
2122 revs
->tag_objects
= 1;
2123 revs
->tree_objects
= 1;
2124 revs
->blob_objects
= 1;
2125 revs
->edge_hint
= 1;
2126 } else if (!strcmp(arg
, "--objects-edge-aggressive")) {
2127 revs
->tag_objects
= 1;
2128 revs
->tree_objects
= 1;
2129 revs
->blob_objects
= 1;
2130 revs
->edge_hint
= 1;
2131 revs
->edge_hint_aggressive
= 1;
2132 } else if (!strcmp(arg
, "--verify-objects")) {
2133 revs
->tag_objects
= 1;
2134 revs
->tree_objects
= 1;
2135 revs
->blob_objects
= 1;
2136 revs
->verify_objects
= 1;
2137 } else if (!strcmp(arg
, "--unpacked")) {
2139 } else if (starts_with(arg
, "--unpacked=")) {
2140 die("--unpacked=<packfile> no longer supported.");
2141 } else if (!strcmp(arg
, "-r")) {
2143 revs
->diffopt
.flags
.recursive
= 1;
2144 } else if (!strcmp(arg
, "-t")) {
2146 revs
->diffopt
.flags
.recursive
= 1;
2147 revs
->diffopt
.flags
.tree_in_recursive
= 1;
2148 } else if (!strcmp(arg
, "-m")) {
2149 revs
->ignore_merges
= 0;
2150 } else if (!strcmp(arg
, "-c")) {
2152 revs
->dense_combined_merges
= 0;
2153 revs
->combine_merges
= 1;
2154 } else if (!strcmp(arg
, "--cc")) {
2156 revs
->dense_combined_merges
= 1;
2157 revs
->combine_merges
= 1;
2158 } else if (!strcmp(arg
, "-v")) {
2159 revs
->verbose_header
= 1;
2160 } else if (!strcmp(arg
, "--pretty")) {
2161 revs
->verbose_header
= 1;
2162 revs
->pretty_given
= 1;
2163 get_commit_format(NULL
, revs
);
2164 } else if (skip_prefix(arg
, "--pretty=", &optarg
) ||
2165 skip_prefix(arg
, "--format=", &optarg
)) {
2167 * Detached form ("--pretty X" as opposed to "--pretty=X")
2168 * not allowed, since the argument is optional.
2170 revs
->verbose_header
= 1;
2171 revs
->pretty_given
= 1;
2172 get_commit_format(optarg
, revs
);
2173 } else if (!strcmp(arg
, "--expand-tabs")) {
2174 revs
->expand_tabs_in_log
= 8;
2175 } else if (!strcmp(arg
, "--no-expand-tabs")) {
2176 revs
->expand_tabs_in_log
= 0;
2177 } else if (skip_prefix(arg
, "--expand-tabs=", &arg
)) {
2179 if (strtol_i(arg
, 10, &val
) < 0 || val
< 0)
2180 die("'%s': not a non-negative integer", arg
);
2181 revs
->expand_tabs_in_log
= val
;
2182 } else if (!strcmp(arg
, "--show-notes") || !strcmp(arg
, "--notes")) {
2183 revs
->show_notes
= 1;
2184 revs
->show_notes_given
= 1;
2185 revs
->notes_opt
.use_default_notes
= 1;
2186 } else if (!strcmp(arg
, "--show-signature")) {
2187 revs
->show_signature
= 1;
2188 } else if (!strcmp(arg
, "--no-show-signature")) {
2189 revs
->show_signature
= 0;
2190 } else if (!strcmp(arg
, "--show-linear-break")) {
2191 revs
->break_bar
= " ..........";
2192 revs
->track_linear
= 1;
2193 revs
->track_first_time
= 1;
2194 } else if (skip_prefix(arg
, "--show-linear-break=", &optarg
)) {
2195 revs
->break_bar
= xstrdup(optarg
);
2196 revs
->track_linear
= 1;
2197 revs
->track_first_time
= 1;
2198 } else if (skip_prefix(arg
, "--show-notes=", &optarg
) ||
2199 skip_prefix(arg
, "--notes=", &optarg
)) {
2200 struct strbuf buf
= STRBUF_INIT
;
2201 revs
->show_notes
= 1;
2202 revs
->show_notes_given
= 1;
2203 if (starts_with(arg
, "--show-notes=") &&
2204 revs
->notes_opt
.use_default_notes
< 0)
2205 revs
->notes_opt
.use_default_notes
= 1;
2206 strbuf_addstr(&buf
, optarg
);
2207 expand_notes_ref(&buf
);
2208 string_list_append(&revs
->notes_opt
.extra_notes_refs
,
2209 strbuf_detach(&buf
, NULL
));
2210 } else if (!strcmp(arg
, "--no-notes")) {
2211 revs
->show_notes
= 0;
2212 revs
->show_notes_given
= 1;
2213 revs
->notes_opt
.use_default_notes
= -1;
2214 /* we have been strdup'ing ourselves, so trick
2215 * string_list into free()ing strings */
2216 revs
->notes_opt
.extra_notes_refs
.strdup_strings
= 1;
2217 string_list_clear(&revs
->notes_opt
.extra_notes_refs
, 0);
2218 revs
->notes_opt
.extra_notes_refs
.strdup_strings
= 0;
2219 } else if (!strcmp(arg
, "--standard-notes")) {
2220 revs
->show_notes_given
= 1;
2221 revs
->notes_opt
.use_default_notes
= 1;
2222 } else if (!strcmp(arg
, "--no-standard-notes")) {
2223 revs
->notes_opt
.use_default_notes
= 0;
2224 } else if (!strcmp(arg
, "--oneline")) {
2225 revs
->verbose_header
= 1;
2226 get_commit_format("oneline", revs
);
2227 revs
->pretty_given
= 1;
2228 revs
->abbrev_commit
= 1;
2229 } else if (!strcmp(arg
, "--graph")) {
2230 revs
->topo_order
= 1;
2231 revs
->rewrite_parents
= 1;
2232 revs
->graph
= graph_init(revs
);
2233 } else if (!strcmp(arg
, "--root")) {
2234 revs
->show_root_diff
= 1;
2235 } else if (!strcmp(arg
, "--no-commit-id")) {
2236 revs
->no_commit_id
= 1;
2237 } else if (!strcmp(arg
, "--always")) {
2238 revs
->always_show_header
= 1;
2239 } else if (!strcmp(arg
, "--no-abbrev")) {
2241 } else if (!strcmp(arg
, "--abbrev")) {
2242 revs
->abbrev
= DEFAULT_ABBREV
;
2243 } else if (skip_prefix(arg
, "--abbrev=", &optarg
)) {
2244 revs
->abbrev
= strtoul(optarg
, NULL
, 10);
2245 if (revs
->abbrev
< MINIMUM_ABBREV
)
2246 revs
->abbrev
= MINIMUM_ABBREV
;
2247 else if (revs
->abbrev
> hexsz
)
2248 revs
->abbrev
= hexsz
;
2249 } else if (!strcmp(arg
, "--abbrev-commit")) {
2250 revs
->abbrev_commit
= 1;
2251 revs
->abbrev_commit_given
= 1;
2252 } else if (!strcmp(arg
, "--no-abbrev-commit")) {
2253 revs
->abbrev_commit
= 0;
2254 } else if (!strcmp(arg
, "--full-diff")) {
2256 revs
->full_diff
= 1;
2257 } else if (!strcmp(arg
, "--full-history")) {
2258 revs
->simplify_history
= 0;
2259 } else if (!strcmp(arg
, "--relative-date")) {
2260 revs
->date_mode
.type
= DATE_RELATIVE
;
2261 revs
->date_mode_explicit
= 1;
2262 } else if ((argcount
= parse_long_opt("date", argv
, &optarg
))) {
2263 parse_date_format(optarg
, &revs
->date_mode
);
2264 revs
->date_mode_explicit
= 1;
2266 } else if (!strcmp(arg
, "--log-size")) {
2267 revs
->show_log_size
= 1;
2270 * Grepping the commit log
2272 else if ((argcount
= parse_long_opt("author", argv
, &optarg
))) {
2273 add_header_grep(revs
, GREP_HEADER_AUTHOR
, optarg
);
2275 } else if ((argcount
= parse_long_opt("committer", argv
, &optarg
))) {
2276 add_header_grep(revs
, GREP_HEADER_COMMITTER
, optarg
);
2278 } else if ((argcount
= parse_long_opt("grep-reflog", argv
, &optarg
))) {
2279 add_header_grep(revs
, GREP_HEADER_REFLOG
, optarg
);
2281 } else if ((argcount
= parse_long_opt("grep", argv
, &optarg
))) {
2282 add_message_grep(revs
, optarg
);
2284 } else if (!strcmp(arg
, "--grep-debug")) {
2285 revs
->grep_filter
.debug
= 1;
2286 } else if (!strcmp(arg
, "--basic-regexp")) {
2287 revs
->grep_filter
.pattern_type_option
= GREP_PATTERN_TYPE_BRE
;
2288 } else if (!strcmp(arg
, "--extended-regexp") || !strcmp(arg
, "-E")) {
2289 revs
->grep_filter
.pattern_type_option
= GREP_PATTERN_TYPE_ERE
;
2290 } else if (!strcmp(arg
, "--regexp-ignore-case") || !strcmp(arg
, "-i")) {
2291 revs
->grep_filter
.ignore_case
= 1;
2292 revs
->diffopt
.pickaxe_opts
|= DIFF_PICKAXE_IGNORE_CASE
;
2293 } else if (!strcmp(arg
, "--fixed-strings") || !strcmp(arg
, "-F")) {
2294 revs
->grep_filter
.pattern_type_option
= GREP_PATTERN_TYPE_FIXED
;
2295 } else if (!strcmp(arg
, "--perl-regexp") || !strcmp(arg
, "-P")) {
2296 revs
->grep_filter
.pattern_type_option
= GREP_PATTERN_TYPE_PCRE
;
2297 } else if (!strcmp(arg
, "--all-match")) {
2298 revs
->grep_filter
.all_match
= 1;
2299 } else if (!strcmp(arg
, "--invert-grep")) {
2300 revs
->invert_grep
= 1;
2301 } else if ((argcount
= parse_long_opt("encoding", argv
, &optarg
))) {
2302 if (strcmp(optarg
, "none"))
2303 git_log_output_encoding
= xstrdup(optarg
);
2305 git_log_output_encoding
= "";
2307 } else if (!strcmp(arg
, "--reverse")) {
2309 } else if (!strcmp(arg
, "--children")) {
2310 revs
->children
.name
= "children";
2312 } else if (!strcmp(arg
, "--ignore-missing")) {
2313 revs
->ignore_missing
= 1;
2314 } else if (opt
&& opt
->allow_exclude_promisor_objects
&&
2315 !strcmp(arg
, "--exclude-promisor-objects")) {
2316 if (fetch_if_missing
)
2317 BUG("exclude_promisor_objects can only be used when fetch_if_missing is 0");
2318 revs
->exclude_promisor_objects
= 1;
2320 int opts
= diff_opt_parse(&revs
->diffopt
, argv
, argc
, revs
->prefix
);
2322 unkv
[(*unkc
)++] = arg
;
2325 if (revs
->graph
&& revs
->track_linear
)
2326 die("--show-linear-break and --graph are incompatible");
2331 void parse_revision_opt(struct rev_info
*revs
, struct parse_opt_ctx_t
*ctx
,
2332 const struct option
*options
,
2333 const char * const usagestr
[])
2335 int n
= handle_revision_opt(revs
, ctx
->argc
, ctx
->argv
,
2336 &ctx
->cpidx
, ctx
->out
, NULL
);
2338 error("unknown option `%s'", ctx
->argv
[0]);
2339 usage_with_options(usagestr
, options
);
2345 static int for_each_bisect_ref(struct ref_store
*refs
, each_ref_fn fn
,
2346 void *cb_data
, const char *term
)
2348 struct strbuf bisect_refs
= STRBUF_INIT
;
2350 strbuf_addf(&bisect_refs
, "refs/bisect/%s", term
);
2351 status
= refs_for_each_fullref_in(refs
, bisect_refs
.buf
, fn
, cb_data
, 0);
2352 strbuf_release(&bisect_refs
);
2356 static int for_each_bad_bisect_ref(struct ref_store
*refs
, each_ref_fn fn
, void *cb_data
)
2358 return for_each_bisect_ref(refs
, fn
, cb_data
, term_bad
);
2361 static int for_each_good_bisect_ref(struct ref_store
*refs
, each_ref_fn fn
, void *cb_data
)
2363 return for_each_bisect_ref(refs
, fn
, cb_data
, term_good
);
2366 static int handle_revision_pseudo_opt(const char *submodule
,
2367 struct rev_info
*revs
,
2368 int argc
, const char **argv
, int *flags
)
2370 const char *arg
= argv
[0];
2372 struct ref_store
*refs
;
2377 * We need some something like get_submodule_worktrees()
2378 * before we can go through all worktrees of a submodule,
2379 * .e.g with adding all HEADs from --all, which is not
2380 * supported right now, so stick to single worktree.
2382 if (!revs
->single_worktree
)
2383 BUG("--single-worktree cannot be used together with submodule");
2384 refs
= get_submodule_ref_store(submodule
);
2386 refs
= get_main_ref_store(revs
->repo
);
2391 * Commands like "git shortlog" will not accept the options below
2392 * unless parse_revision_opt queues them (as opposed to erroring
2395 * When implementing your new pseudo-option, remember to
2396 * register it in the list at the top of handle_revision_opt.
2398 if (!strcmp(arg
, "--all")) {
2399 handle_refs(refs
, revs
, *flags
, refs_for_each_ref
);
2400 handle_refs(refs
, revs
, *flags
, refs_head_ref
);
2401 if (!revs
->single_worktree
) {
2402 struct all_refs_cb cb
;
2404 init_all_refs_cb(&cb
, revs
, *flags
);
2405 other_head_refs(handle_one_ref
, &cb
);
2407 clear_ref_exclusion(&revs
->ref_excludes
);
2408 } else if (!strcmp(arg
, "--branches")) {
2409 handle_refs(refs
, revs
, *flags
, refs_for_each_branch_ref
);
2410 clear_ref_exclusion(&revs
->ref_excludes
);
2411 } else if (!strcmp(arg
, "--bisect")) {
2412 read_bisect_terms(&term_bad
, &term_good
);
2413 handle_refs(refs
, revs
, *flags
, for_each_bad_bisect_ref
);
2414 handle_refs(refs
, revs
, *flags
^ (UNINTERESTING
| BOTTOM
),
2415 for_each_good_bisect_ref
);
2417 } else if (!strcmp(arg
, "--tags")) {
2418 handle_refs(refs
, revs
, *flags
, refs_for_each_tag_ref
);
2419 clear_ref_exclusion(&revs
->ref_excludes
);
2420 } else if (!strcmp(arg
, "--remotes")) {
2421 handle_refs(refs
, revs
, *flags
, refs_for_each_remote_ref
);
2422 clear_ref_exclusion(&revs
->ref_excludes
);
2423 } else if ((argcount
= parse_long_opt("glob", argv
, &optarg
))) {
2424 struct all_refs_cb cb
;
2425 init_all_refs_cb(&cb
, revs
, *flags
);
2426 for_each_glob_ref(handle_one_ref
, optarg
, &cb
);
2427 clear_ref_exclusion(&revs
->ref_excludes
);
2429 } else if ((argcount
= parse_long_opt("exclude", argv
, &optarg
))) {
2430 add_ref_exclusion(&revs
->ref_excludes
, optarg
);
2432 } else if (skip_prefix(arg
, "--branches=", &optarg
)) {
2433 struct all_refs_cb cb
;
2434 init_all_refs_cb(&cb
, revs
, *flags
);
2435 for_each_glob_ref_in(handle_one_ref
, optarg
, "refs/heads/", &cb
);
2436 clear_ref_exclusion(&revs
->ref_excludes
);
2437 } else if (skip_prefix(arg
, "--tags=", &optarg
)) {
2438 struct all_refs_cb cb
;
2439 init_all_refs_cb(&cb
, revs
, *flags
);
2440 for_each_glob_ref_in(handle_one_ref
, optarg
, "refs/tags/", &cb
);
2441 clear_ref_exclusion(&revs
->ref_excludes
);
2442 } else if (skip_prefix(arg
, "--remotes=", &optarg
)) {
2443 struct all_refs_cb cb
;
2444 init_all_refs_cb(&cb
, revs
, *flags
);
2445 for_each_glob_ref_in(handle_one_ref
, optarg
, "refs/remotes/", &cb
);
2446 clear_ref_exclusion(&revs
->ref_excludes
);
2447 } else if (!strcmp(arg
, "--reflog")) {
2448 add_reflogs_to_pending(revs
, *flags
);
2449 } else if (!strcmp(arg
, "--indexed-objects")) {
2450 add_index_objects_to_pending(revs
, *flags
);
2451 } else if (!strcmp(arg
, "--not")) {
2452 *flags
^= UNINTERESTING
| BOTTOM
;
2453 } else if (!strcmp(arg
, "--no-walk")) {
2454 revs
->no_walk
= REVISION_WALK_NO_WALK_SORTED
;
2455 } else if (skip_prefix(arg
, "--no-walk=", &optarg
)) {
2457 * Detached form ("--no-walk X" as opposed to "--no-walk=X")
2458 * not allowed, since the argument is optional.
2460 if (!strcmp(optarg
, "sorted"))
2461 revs
->no_walk
= REVISION_WALK_NO_WALK_SORTED
;
2462 else if (!strcmp(optarg
, "unsorted"))
2463 revs
->no_walk
= REVISION_WALK_NO_WALK_UNSORTED
;
2465 return error("invalid argument to --no-walk");
2466 } else if (!strcmp(arg
, "--do-walk")) {
2468 } else if (!strcmp(arg
, "--single-worktree")) {
2469 revs
->single_worktree
= 1;
2477 static void NORETURN
diagnose_missing_default(const char *def
)
2480 const char *refname
;
2482 refname
= resolve_ref_unsafe(def
, 0, NULL
, &flags
);
2483 if (!refname
|| !(flags
& REF_ISSYMREF
) || (flags
& REF_ISBROKEN
))
2484 die(_("your current branch appears to be broken"));
2486 skip_prefix(refname
, "refs/heads/", &refname
);
2487 die(_("your current branch '%s' does not have any commits yet"),
2492 * Parse revision information, filling in the "rev_info" structure,
2493 * and removing the used arguments from the argument list.
2495 * Returns the number of arguments left that weren't recognized
2496 * (which are also moved to the head of the argument list)
2498 int setup_revisions(int argc
, const char **argv
, struct rev_info
*revs
, struct setup_revision_opt
*opt
)
2500 int i
, flags
, left
, seen_dashdash
, got_rev_arg
= 0, revarg_opt
;
2501 struct argv_array prune_data
= ARGV_ARRAY_INIT
;
2502 const char *submodule
= NULL
;
2505 submodule
= opt
->submodule
;
2507 /* First, search for "--" */
2508 if (opt
&& opt
->assume_dashdash
) {
2512 for (i
= 1; i
< argc
; i
++) {
2513 const char *arg
= argv
[i
];
2514 if (strcmp(arg
, "--"))
2519 argv_array_pushv(&prune_data
, argv
+ i
+ 1);
2525 /* Second, deal with arguments and options */
2527 revarg_opt
= opt
? opt
->revarg_opt
: 0;
2529 revarg_opt
|= REVARG_CANNOT_BE_FILENAME
;
2530 for (left
= i
= 1; i
< argc
; i
++) {
2531 const char *arg
= argv
[i
];
2535 opts
= handle_revision_pseudo_opt(submodule
,
2536 revs
, argc
- i
, argv
+ i
,
2543 if (!strcmp(arg
, "--stdin")) {
2544 if (revs
->disable_stdin
) {
2548 if (revs
->read_from_stdin
++)
2549 die("--stdin given twice?");
2550 read_revisions_from_stdin(revs
, &prune_data
);
2554 opts
= handle_revision_opt(revs
, argc
- i
, argv
+ i
,
2566 if (handle_revision_arg(arg
, revs
, flags
, revarg_opt
)) {
2568 if (seen_dashdash
|| *arg
== '^')
2569 die("bad revision '%s'", arg
);
2571 /* If we didn't have a "--":
2572 * (1) all filenames must exist;
2573 * (2) all rev-args must not be interpretable
2574 * as a valid filename.
2575 * but the latter we have checked in the main loop.
2577 for (j
= i
; j
< argc
; j
++)
2578 verify_filename(revs
->prefix
, argv
[j
], j
== i
);
2580 argv_array_pushv(&prune_data
, argv
+ i
);
2587 if (prune_data
.argc
) {
2589 * If we need to introduce the magic "a lone ':' means no
2590 * pathspec whatsoever", here is the place to do so.
2592 * if (prune_data.nr == 1 && !strcmp(prune_data[0], ":")) {
2593 * prune_data.nr = 0;
2594 * prune_data.alloc = 0;
2595 * free(prune_data.path);
2596 * prune_data.path = NULL;
2598 * terminate prune_data.alloc with NULL and
2599 * call init_pathspec() to set revs->prune_data here.
2602 parse_pathspec(&revs
->prune_data
, 0, 0,
2603 revs
->prefix
, prune_data
.argv
);
2605 argv_array_clear(&prune_data
);
2607 if (revs
->def
== NULL
)
2608 revs
->def
= opt
? opt
->def
: NULL
;
2609 if (opt
&& opt
->tweak
)
2610 opt
->tweak(revs
, opt
);
2611 if (revs
->show_merge
)
2612 prepare_show_merge(revs
);
2613 if (revs
->def
&& !revs
->pending
.nr
&& !revs
->rev_input_given
&& !got_rev_arg
) {
2614 struct object_id oid
;
2615 struct object
*object
;
2616 struct object_context oc
;
2617 if (get_oid_with_context(revs
->repo
, revs
->def
, 0, &oid
, &oc
))
2618 diagnose_missing_default(revs
->def
);
2619 object
= get_reference(revs
, revs
->def
, &oid
, 0);
2620 add_pending_object_with_mode(revs
, object
, revs
->def
, oc
.mode
);
2623 /* Did the user ask for any diff output? Run the diff! */
2624 if (revs
->diffopt
.output_format
& ~DIFF_FORMAT_NO_OUTPUT
)
2627 /* Pickaxe, diff-filter and rename following need diffs */
2628 if ((revs
->diffopt
.pickaxe_opts
& DIFF_PICKAXE_KINDS_MASK
) ||
2629 revs
->diffopt
.filter
||
2630 revs
->diffopt
.flags
.follow_renames
)
2633 if (revs
->diffopt
.objfind
)
2634 revs
->simplify_history
= 0;
2636 if (revs
->topo_order
&& !generation_numbers_enabled(the_repository
))
2639 if (revs
->prune_data
.nr
) {
2640 copy_pathspec(&revs
->pruning
.pathspec
, &revs
->prune_data
);
2641 /* Can't prune commits with rename following: the paths change.. */
2642 if (!revs
->diffopt
.flags
.follow_renames
)
2644 if (!revs
->full_diff
)
2645 copy_pathspec(&revs
->diffopt
.pathspec
,
2648 if (revs
->combine_merges
)
2649 revs
->ignore_merges
= 0;
2650 revs
->diffopt
.abbrev
= revs
->abbrev
;
2652 if (revs
->line_level_traverse
) {
2654 revs
->topo_order
= 1;
2657 diff_setup_done(&revs
->diffopt
);
2659 grep_commit_pattern_type(GREP_PATTERN_TYPE_UNSPECIFIED
,
2660 &revs
->grep_filter
);
2661 compile_grep_patterns(&revs
->grep_filter
);
2663 if (revs
->reverse
&& revs
->reflog_info
)
2664 die("cannot combine --reverse with --walk-reflogs");
2665 if (revs
->reflog_info
&& revs
->limited
)
2666 die("cannot combine --walk-reflogs with history-limiting options");
2667 if (revs
->rewrite_parents
&& revs
->children
.name
)
2668 die("cannot combine --parents and --children");
2671 * Limitations on the graph functionality
2673 if (revs
->reverse
&& revs
->graph
)
2674 die("cannot combine --reverse with --graph");
2676 if (revs
->reflog_info
&& revs
->graph
)
2677 die("cannot combine --walk-reflogs with --graph");
2678 if (revs
->no_walk
&& revs
->graph
)
2679 die("cannot combine --no-walk with --graph");
2680 if (!revs
->reflog_info
&& revs
->grep_filter
.use_reflog_filter
)
2681 die("cannot use --grep-reflog without --walk-reflogs");
2683 if (revs
->first_parent_only
&& revs
->bisect
)
2684 die(_("--first-parent is incompatible with --bisect"));
2686 if (revs
->expand_tabs_in_log
< 0)
2687 revs
->expand_tabs_in_log
= revs
->expand_tabs_in_log_default
;
2692 static void add_child(struct rev_info
*revs
, struct commit
*parent
, struct commit
*child
)
2694 struct commit_list
*l
= xcalloc(1, sizeof(*l
));
2697 l
->next
= add_decoration(&revs
->children
, &parent
->object
, l
);
2700 static int remove_duplicate_parents(struct rev_info
*revs
, struct commit
*commit
)
2702 struct treesame_state
*ts
= lookup_decoration(&revs
->treesame
, &commit
->object
);
2703 struct commit_list
**pp
, *p
;
2704 int surviving_parents
;
2706 /* Examine existing parents while marking ones we have seen... */
2707 pp
= &commit
->parents
;
2708 surviving_parents
= 0;
2709 while ((p
= *pp
) != NULL
) {
2710 struct commit
*parent
= p
->item
;
2711 if (parent
->object
.flags
& TMP_MARK
) {
2714 compact_treesame(revs
, commit
, surviving_parents
);
2717 parent
->object
.flags
|= TMP_MARK
;
2718 surviving_parents
++;
2721 /* clear the temporary mark */
2722 for (p
= commit
->parents
; p
; p
= p
->next
) {
2723 p
->item
->object
.flags
&= ~TMP_MARK
;
2725 /* no update_treesame() - removing duplicates can't affect TREESAME */
2726 return surviving_parents
;
2729 struct merge_simplify_state
{
2730 struct commit
*simplified
;
2733 static struct merge_simplify_state
*locate_simplify_state(struct rev_info
*revs
, struct commit
*commit
)
2735 struct merge_simplify_state
*st
;
2737 st
= lookup_decoration(&revs
->merge_simplification
, &commit
->object
);
2739 st
= xcalloc(1, sizeof(*st
));
2740 add_decoration(&revs
->merge_simplification
, &commit
->object
, st
);
2745 static int mark_redundant_parents(struct rev_info
*revs
, struct commit
*commit
)
2747 struct commit_list
*h
= reduce_heads(commit
->parents
);
2748 int i
= 0, marked
= 0;
2749 struct commit_list
*po
, *pn
;
2751 /* Want these for sanity-checking only */
2752 int orig_cnt
= commit_list_count(commit
->parents
);
2753 int cnt
= commit_list_count(h
);
2756 * Not ready to remove items yet, just mark them for now, based
2757 * on the output of reduce_heads(). reduce_heads outputs the reduced
2758 * set in its original order, so this isn't too hard.
2760 po
= commit
->parents
;
2763 if (pn
&& po
->item
== pn
->item
) {
2767 po
->item
->object
.flags
|= TMP_MARK
;
2773 if (i
!= cnt
|| cnt
+marked
!= orig_cnt
)
2774 die("mark_redundant_parents %d %d %d %d", orig_cnt
, cnt
, i
, marked
);
2776 free_commit_list(h
);
2781 static int mark_treesame_root_parents(struct rev_info
*revs
, struct commit
*commit
)
2783 struct commit_list
*p
;
2786 for (p
= commit
->parents
; p
; p
= p
->next
) {
2787 struct commit
*parent
= p
->item
;
2788 if (!parent
->parents
&& (parent
->object
.flags
& TREESAME
)) {
2789 parent
->object
.flags
|= TMP_MARK
;
2798 * Awkward naming - this means one parent we are TREESAME to.
2799 * cf mark_treesame_root_parents: root parents that are TREESAME (to an
2800 * empty tree). Better name suggestions?
2802 static int leave_one_treesame_to_parent(struct rev_info
*revs
, struct commit
*commit
)
2804 struct treesame_state
*ts
= lookup_decoration(&revs
->treesame
, &commit
->object
);
2805 struct commit
*unmarked
= NULL
, *marked
= NULL
;
2806 struct commit_list
*p
;
2809 for (p
= commit
->parents
, n
= 0; p
; p
= p
->next
, n
++) {
2810 if (ts
->treesame
[n
]) {
2811 if (p
->item
->object
.flags
& TMP_MARK
) {
2824 * If we are TREESAME to a marked-for-deletion parent, but not to any
2825 * unmarked parents, unmark the first TREESAME parent. This is the
2826 * parent that the default simplify_history==1 scan would have followed,
2827 * and it doesn't make sense to omit that path when asking for a
2828 * simplified full history. Retaining it improves the chances of
2829 * understanding odd missed merges that took an old version of a file.
2833 * I--------*X A modified the file, but mainline merge X used
2834 * \ / "-s ours", so took the version from I. X is
2835 * `-*A--' TREESAME to I and !TREESAME to A.
2837 * Default log from X would produce "I". Without this check,
2838 * --full-history --simplify-merges would produce "I-A-X", showing
2839 * the merge commit X and that it changed A, but not making clear that
2840 * it had just taken the I version. With this check, the topology above
2843 * Note that it is possible that the simplification chooses a different
2844 * TREESAME parent from the default, in which case this test doesn't
2845 * activate, and we _do_ drop the default parent. Example:
2847 * I------X A modified the file, but it was reverted in B,
2848 * \ / meaning mainline merge X is TREESAME to both
2851 * Default log would produce "I" by following the first parent;
2852 * --full-history --simplify-merges will produce "I-A-B". But this is a
2853 * reasonable result - it presents a logical full history leading from
2854 * I to X, and X is not an important merge.
2856 if (!unmarked
&& marked
) {
2857 marked
->object
.flags
&= ~TMP_MARK
;
2864 static int remove_marked_parents(struct rev_info
*revs
, struct commit
*commit
)
2866 struct commit_list
**pp
, *p
;
2867 int nth_parent
, removed
= 0;
2869 pp
= &commit
->parents
;
2871 while ((p
= *pp
) != NULL
) {
2872 struct commit
*parent
= p
->item
;
2873 if (parent
->object
.flags
& TMP_MARK
) {
2874 parent
->object
.flags
&= ~TMP_MARK
;
2878 compact_treesame(revs
, commit
, nth_parent
);
2885 /* Removing parents can only increase TREESAMEness */
2886 if (removed
&& !(commit
->object
.flags
& TREESAME
))
2887 update_treesame(revs
, commit
);
2892 static struct commit_list
**simplify_one(struct rev_info
*revs
, struct commit
*commit
, struct commit_list
**tail
)
2894 struct commit_list
*p
;
2895 struct commit
*parent
;
2896 struct merge_simplify_state
*st
, *pst
;
2899 st
= locate_simplify_state(revs
, commit
);
2902 * Have we handled this one?
2908 * An UNINTERESTING commit simplifies to itself, so does a
2909 * root commit. We do not rewrite parents of such commit
2912 if ((commit
->object
.flags
& UNINTERESTING
) || !commit
->parents
) {
2913 st
->simplified
= commit
;
2918 * Do we know what commit all of our parents that matter
2919 * should be rewritten to? Otherwise we are not ready to
2920 * rewrite this one yet.
2922 for (cnt
= 0, p
= commit
->parents
; p
; p
= p
->next
) {
2923 pst
= locate_simplify_state(revs
, p
->item
);
2924 if (!pst
->simplified
) {
2925 tail
= &commit_list_insert(p
->item
, tail
)->next
;
2928 if (revs
->first_parent_only
)
2932 tail
= &commit_list_insert(commit
, tail
)->next
;
2937 * Rewrite our list of parents. Note that this cannot
2938 * affect our TREESAME flags in any way - a commit is
2939 * always TREESAME to its simplification.
2941 for (p
= commit
->parents
; p
; p
= p
->next
) {
2942 pst
= locate_simplify_state(revs
, p
->item
);
2943 p
->item
= pst
->simplified
;
2944 if (revs
->first_parent_only
)
2948 if (revs
->first_parent_only
)
2951 cnt
= remove_duplicate_parents(revs
, commit
);
2954 * It is possible that we are a merge and one side branch
2955 * does not have any commit that touches the given paths;
2956 * in such a case, the immediate parent from that branch
2957 * will be rewritten to be the merge base.
2959 * o----X X: the commit we are looking at;
2960 * / / o: a commit that touches the paths;
2963 * Further, a merge of an independent branch that doesn't
2964 * touch the path will reduce to a treesame root parent:
2966 * ----o----X X: the commit we are looking at;
2967 * / o: a commit that touches the paths;
2968 * r r: a root commit not touching the paths
2970 * Detect and simplify both cases.
2973 int marked
= mark_redundant_parents(revs
, commit
);
2974 marked
+= mark_treesame_root_parents(revs
, commit
);
2976 marked
-= leave_one_treesame_to_parent(revs
, commit
);
2978 cnt
= remove_marked_parents(revs
, commit
);
2982 * A commit simplifies to itself if it is a root, if it is
2983 * UNINTERESTING, if it touches the given paths, or if it is a
2984 * merge and its parents don't simplify to one relevant commit
2985 * (the first two cases are already handled at the beginning of
2988 * Otherwise, it simplifies to what its sole relevant parent
2992 (commit
->object
.flags
& UNINTERESTING
) ||
2993 !(commit
->object
.flags
& TREESAME
) ||
2994 (parent
= one_relevant_parent(revs
, commit
->parents
)) == NULL
)
2995 st
->simplified
= commit
;
2997 pst
= locate_simplify_state(revs
, parent
);
2998 st
->simplified
= pst
->simplified
;
3003 static void simplify_merges(struct rev_info
*revs
)
3005 struct commit_list
*list
, *next
;
3006 struct commit_list
*yet_to_do
, **tail
;
3007 struct commit
*commit
;
3012 /* feed the list reversed */
3014 for (list
= revs
->commits
; list
; list
= next
) {
3015 commit
= list
->item
;
3018 * Do not free(list) here yet; the original list
3019 * is used later in this function.
3021 commit_list_insert(commit
, &yet_to_do
);
3028 commit
= pop_commit(&list
);
3029 tail
= simplify_one(revs
, commit
, tail
);
3033 /* clean up the result, removing the simplified ones */
3034 list
= revs
->commits
;
3035 revs
->commits
= NULL
;
3036 tail
= &revs
->commits
;
3038 struct merge_simplify_state
*st
;
3040 commit
= pop_commit(&list
);
3041 st
= locate_simplify_state(revs
, commit
);
3042 if (st
->simplified
== commit
)
3043 tail
= &commit_list_insert(commit
, tail
)->next
;
3047 static void set_children(struct rev_info
*revs
)
3049 struct commit_list
*l
;
3050 for (l
= revs
->commits
; l
; l
= l
->next
) {
3051 struct commit
*commit
= l
->item
;
3052 struct commit_list
*p
;
3054 for (p
= commit
->parents
; p
; p
= p
->next
)
3055 add_child(revs
, p
->item
, commit
);
3059 void reset_revision_walk(void)
3061 clear_object_flags(SEEN
| ADDED
| SHOWN
);
3064 static int mark_uninteresting(const struct object_id
*oid
,
3065 struct packed_git
*pack
,
3069 struct rev_info
*revs
= cb
;
3070 struct object
*o
= parse_object(revs
->repo
, oid
);
3071 o
->flags
|= UNINTERESTING
| SEEN
;
3075 define_commit_slab(indegree_slab
, int);
3076 define_commit_slab(author_date_slab
, timestamp_t
);
3078 struct topo_walk_info
{
3079 uint32_t min_generation
;
3080 struct prio_queue explore_queue
;
3081 struct prio_queue indegree_queue
;
3082 struct prio_queue topo_queue
;
3083 struct indegree_slab indegree
;
3084 struct author_date_slab author_date
;
3087 static inline void test_flag_and_insert(struct prio_queue
*q
, struct commit
*c
, int flag
)
3089 if (c
->object
.flags
& flag
)
3092 c
->object
.flags
|= flag
;
3093 prio_queue_put(q
, c
);
3096 static void explore_walk_step(struct rev_info
*revs
)
3098 struct topo_walk_info
*info
= revs
->topo_walk_info
;
3099 struct commit_list
*p
;
3100 struct commit
*c
= prio_queue_get(&info
->explore_queue
);
3105 if (parse_commit_gently(c
, 1) < 0)
3108 if (revs
->sort_order
== REV_SORT_BY_AUTHOR_DATE
)
3109 record_author_date(&info
->author_date
, c
);
3111 if (revs
->max_age
!= -1 && (c
->date
< revs
->max_age
))
3112 c
->object
.flags
|= UNINTERESTING
;
3114 if (process_parents(revs
, c
, NULL
, NULL
) < 0)
3117 if (c
->object
.flags
& UNINTERESTING
)
3118 mark_parents_uninteresting(c
);
3120 for (p
= c
->parents
; p
; p
= p
->next
)
3121 test_flag_and_insert(&info
->explore_queue
, p
->item
, TOPO_WALK_EXPLORED
);
3124 static void explore_to_depth(struct rev_info
*revs
,
3125 uint32_t gen_cutoff
)
3127 struct topo_walk_info
*info
= revs
->topo_walk_info
;
3129 while ((c
= prio_queue_peek(&info
->explore_queue
)) &&
3130 c
->generation
>= gen_cutoff
)
3131 explore_walk_step(revs
);
3134 static void indegree_walk_step(struct rev_info
*revs
)
3136 struct commit_list
*p
;
3137 struct topo_walk_info
*info
= revs
->topo_walk_info
;
3138 struct commit
*c
= prio_queue_get(&info
->indegree_queue
);
3143 if (parse_commit_gently(c
, 1) < 0)
3146 explore_to_depth(revs
, c
->generation
);
3148 for (p
= c
->parents
; p
; p
= p
->next
) {
3149 struct commit
*parent
= p
->item
;
3150 int *pi
= indegree_slab_at(&info
->indegree
, parent
);
3157 test_flag_and_insert(&info
->indegree_queue
, parent
, TOPO_WALK_INDEGREE
);
3159 if (revs
->first_parent_only
)
3164 static void compute_indegrees_to_depth(struct rev_info
*revs
,
3165 uint32_t gen_cutoff
)
3167 struct topo_walk_info
*info
= revs
->topo_walk_info
;
3169 while ((c
= prio_queue_peek(&info
->indegree_queue
)) &&
3170 c
->generation
>= gen_cutoff
)
3171 indegree_walk_step(revs
);
3174 static void init_topo_walk(struct rev_info
*revs
)
3176 struct topo_walk_info
*info
;
3177 struct commit_list
*list
;
3178 revs
->topo_walk_info
= xmalloc(sizeof(struct topo_walk_info
));
3179 info
= revs
->topo_walk_info
;
3180 memset(info
, 0, sizeof(struct topo_walk_info
));
3182 init_indegree_slab(&info
->indegree
);
3183 memset(&info
->explore_queue
, 0, sizeof(info
->explore_queue
));
3184 memset(&info
->indegree_queue
, 0, sizeof(info
->indegree_queue
));
3185 memset(&info
->topo_queue
, 0, sizeof(info
->topo_queue
));
3187 switch (revs
->sort_order
) {
3188 default: /* REV_SORT_IN_GRAPH_ORDER */
3189 info
->topo_queue
.compare
= NULL
;
3191 case REV_SORT_BY_COMMIT_DATE
:
3192 info
->topo_queue
.compare
= compare_commits_by_commit_date
;
3194 case REV_SORT_BY_AUTHOR_DATE
:
3195 init_author_date_slab(&info
->author_date
);
3196 info
->topo_queue
.compare
= compare_commits_by_author_date
;
3197 info
->topo_queue
.cb_data
= &info
->author_date
;
3201 info
->explore_queue
.compare
= compare_commits_by_gen_then_commit_date
;
3202 info
->indegree_queue
.compare
= compare_commits_by_gen_then_commit_date
;
3204 info
->min_generation
= GENERATION_NUMBER_INFINITY
;
3205 for (list
= revs
->commits
; list
; list
= list
->next
) {
3206 struct commit
*c
= list
->item
;
3208 if (parse_commit_gently(c
, 1))
3211 test_flag_and_insert(&info
->explore_queue
, c
, TOPO_WALK_EXPLORED
);
3212 test_flag_and_insert(&info
->indegree_queue
, c
, TOPO_WALK_INDEGREE
);
3214 if (c
->generation
< info
->min_generation
)
3215 info
->min_generation
= c
->generation
;
3217 *(indegree_slab_at(&info
->indegree
, c
)) = 1;
3219 if (revs
->sort_order
== REV_SORT_BY_AUTHOR_DATE
)
3220 record_author_date(&info
->author_date
, c
);
3222 compute_indegrees_to_depth(revs
, info
->min_generation
);
3224 for (list
= revs
->commits
; list
; list
= list
->next
) {
3225 struct commit
*c
= list
->item
;
3227 if (*(indegree_slab_at(&info
->indegree
, c
)) == 1)
3228 prio_queue_put(&info
->topo_queue
, c
);
3232 * This is unfortunate; the initial tips need to be shown
3233 * in the order given from the revision traversal machinery.
3235 if (revs
->sort_order
== REV_SORT_IN_GRAPH_ORDER
)
3236 prio_queue_reverse(&info
->topo_queue
);
3239 static struct commit
*next_topo_commit(struct rev_info
*revs
)
3242 struct topo_walk_info
*info
= revs
->topo_walk_info
;
3244 /* pop next off of topo_queue */
3245 c
= prio_queue_get(&info
->topo_queue
);
3248 *(indegree_slab_at(&info
->indegree
, c
)) = 0;
3253 static void expand_topo_walk(struct rev_info
*revs
, struct commit
*commit
)
3255 struct commit_list
*p
;
3256 struct topo_walk_info
*info
= revs
->topo_walk_info
;
3257 if (process_parents(revs
, commit
, NULL
, NULL
) < 0) {
3258 if (!revs
->ignore_missing_links
)
3259 die("Failed to traverse parents of commit %s",
3260 oid_to_hex(&commit
->object
.oid
));
3263 for (p
= commit
->parents
; p
; p
= p
->next
) {
3264 struct commit
*parent
= p
->item
;
3267 if (parse_commit_gently(parent
, 1) < 0)
3270 if (parent
->generation
< info
->min_generation
) {
3271 info
->min_generation
= parent
->generation
;
3272 compute_indegrees_to_depth(revs
, info
->min_generation
);
3275 pi
= indegree_slab_at(&info
->indegree
, parent
);
3279 prio_queue_put(&info
->topo_queue
, parent
);
3281 if (revs
->first_parent_only
)
3286 int prepare_revision_walk(struct rev_info
*revs
)
3289 struct object_array old_pending
;
3290 struct commit_list
**next
= &revs
->commits
;
3292 memcpy(&old_pending
, &revs
->pending
, sizeof(old_pending
));
3293 revs
->pending
.nr
= 0;
3294 revs
->pending
.alloc
= 0;
3295 revs
->pending
.objects
= NULL
;
3296 for (i
= 0; i
< old_pending
.nr
; i
++) {
3297 struct object_array_entry
*e
= old_pending
.objects
+ i
;
3298 struct commit
*commit
= handle_commit(revs
, e
);
3300 if (!(commit
->object
.flags
& SEEN
)) {
3301 commit
->object
.flags
|= SEEN
;
3302 next
= commit_list_append(commit
, next
);
3306 object_array_clear(&old_pending
);
3308 /* Signal whether we need per-parent treesame decoration */
3309 if (revs
->simplify_merges
||
3310 (revs
->limited
&& limiting_can_increase_treesame(revs
)))
3311 revs
->treesame
.name
= "treesame";
3313 if (revs
->exclude_promisor_objects
) {
3314 for_each_packed_object(mark_uninteresting
, revs
,
3315 FOR_EACH_OBJECT_PROMISOR_ONLY
);
3318 if (revs
->no_walk
!= REVISION_WALK_NO_WALK_UNSORTED
)
3319 commit_list_sort_by_date(&revs
->commits
);
3322 if (revs
->limited
) {
3323 if (limit_list(revs
) < 0)
3325 if (revs
->topo_order
)
3326 sort_in_topological_order(&revs
->commits
, revs
->sort_order
);
3327 } else if (revs
->topo_order
)
3328 init_topo_walk(revs
);
3329 if (revs
->line_level_traverse
)
3330 line_log_filter(revs
);
3331 if (revs
->simplify_merges
)
3332 simplify_merges(revs
);
3333 if (revs
->children
.name
)
3338 static enum rewrite_result
rewrite_one(struct rev_info
*revs
, struct commit
**pp
)
3340 struct commit_list
*cache
= NULL
;
3343 struct commit
*p
= *pp
;
3345 if (process_parents(revs
, p
, &revs
->commits
, &cache
) < 0)
3346 return rewrite_one_error
;
3347 if (p
->object
.flags
& UNINTERESTING
)
3348 return rewrite_one_ok
;
3349 if (!(p
->object
.flags
& TREESAME
))
3350 return rewrite_one_ok
;
3352 return rewrite_one_noparents
;
3353 if ((p
= one_relevant_parent(revs
, p
->parents
)) == NULL
)
3354 return rewrite_one_ok
;
3359 int rewrite_parents(struct rev_info
*revs
, struct commit
*commit
,
3360 rewrite_parent_fn_t rewrite_parent
)
3362 struct commit_list
**pp
= &commit
->parents
;
3364 struct commit_list
*parent
= *pp
;
3365 switch (rewrite_parent(revs
, &parent
->item
)) {
3366 case rewrite_one_ok
:
3368 case rewrite_one_noparents
:
3371 case rewrite_one_error
:
3376 remove_duplicate_parents(revs
, commit
);
3380 static int commit_rewrite_person(struct strbuf
*buf
, const char *what
, struct string_list
*mailmap
)
3382 char *person
, *endp
;
3383 size_t len
, namelen
, maillen
;
3386 struct ident_split ident
;
3388 person
= strstr(buf
->buf
, what
);
3392 person
+= strlen(what
);
3393 endp
= strchr(person
, '\n');
3397 len
= endp
- person
;
3399 if (split_ident_line(&ident
, person
, len
))
3402 mail
= ident
.mail_begin
;
3403 maillen
= ident
.mail_end
- ident
.mail_begin
;
3404 name
= ident
.name_begin
;
3405 namelen
= ident
.name_end
- ident
.name_begin
;
3407 if (map_user(mailmap
, &mail
, &maillen
, &name
, &namelen
)) {
3408 struct strbuf namemail
= STRBUF_INIT
;
3410 strbuf_addf(&namemail
, "%.*s <%.*s>",
3411 (int)namelen
, name
, (int)maillen
, mail
);
3413 strbuf_splice(buf
, ident
.name_begin
- buf
->buf
,
3414 ident
.mail_end
- ident
.name_begin
+ 1,
3415 namemail
.buf
, namemail
.len
);
3417 strbuf_release(&namemail
);
3425 static int commit_match(struct commit
*commit
, struct rev_info
*opt
)
3428 const char *encoding
;
3429 const char *message
;
3430 struct strbuf buf
= STRBUF_INIT
;
3432 if (!opt
->grep_filter
.pattern_list
&& !opt
->grep_filter
.header_list
)
3435 /* Prepend "fake" headers as needed */
3436 if (opt
->grep_filter
.use_reflog_filter
) {
3437 strbuf_addstr(&buf
, "reflog ");
3438 get_reflog_message(&buf
, opt
->reflog_info
);
3439 strbuf_addch(&buf
, '\n');
3443 * We grep in the user's output encoding, under the assumption that it
3444 * is the encoding they are most likely to write their grep pattern
3445 * for. In addition, it means we will match the "notes" encoding below,
3446 * so we will not end up with a buffer that has two different encodings
3449 encoding
= get_log_output_encoding();
3450 message
= logmsg_reencode(commit
, NULL
, encoding
);
3452 /* Copy the commit to temporary if we are using "fake" headers */
3454 strbuf_addstr(&buf
, message
);
3456 if (opt
->grep_filter
.header_list
&& opt
->mailmap
) {
3458 strbuf_addstr(&buf
, message
);
3460 commit_rewrite_person(&buf
, "\nauthor ", opt
->mailmap
);
3461 commit_rewrite_person(&buf
, "\ncommitter ", opt
->mailmap
);
3464 /* Append "fake" message parts as needed */
3465 if (opt
->show_notes
) {
3467 strbuf_addstr(&buf
, message
);
3468 format_display_notes(&commit
->object
.oid
, &buf
, encoding
, 1);
3472 * Find either in the original commit message, or in the temporary.
3473 * Note that we cast away the constness of "message" here. It is
3474 * const because it may come from the cached commit buffer. That's OK,
3475 * because we know that it is modifiable heap memory, and that while
3476 * grep_buffer may modify it for speed, it will restore any
3477 * changes before returning.
3480 retval
= grep_buffer(&opt
->grep_filter
, buf
.buf
, buf
.len
);
3482 retval
= grep_buffer(&opt
->grep_filter
,
3483 (char *)message
, strlen(message
));
3484 strbuf_release(&buf
);
3485 unuse_commit_buffer(commit
, message
);
3486 return opt
->invert_grep
? !retval
: retval
;
3489 static inline int want_ancestry(const struct rev_info
*revs
)
3491 return (revs
->rewrite_parents
|| revs
->children
.name
);
3495 * Return a timestamp to be used for --since/--until comparisons for this
3496 * commit, based on the revision options.
3498 static timestamp_t
comparison_date(const struct rev_info
*revs
,
3499 struct commit
*commit
)
3501 return revs
->reflog_info
?
3502 get_reflog_timestamp(revs
->reflog_info
) :
3506 enum commit_action
get_commit_action(struct rev_info
*revs
, struct commit
*commit
)
3508 if (commit
->object
.flags
& SHOWN
)
3509 return commit_ignore
;
3510 if (revs
->unpacked
&& has_object_pack(&commit
->object
.oid
))
3511 return commit_ignore
;
3512 if (commit
->object
.flags
& UNINTERESTING
)
3513 return commit_ignore
;
3514 if (revs
->min_age
!= -1 &&
3515 comparison_date(revs
, commit
) > revs
->min_age
)
3516 return commit_ignore
;
3517 if (revs
->min_parents
|| (revs
->max_parents
>= 0)) {
3518 int n
= commit_list_count(commit
->parents
);
3519 if ((n
< revs
->min_parents
) ||
3520 ((revs
->max_parents
>= 0) && (n
> revs
->max_parents
)))
3521 return commit_ignore
;
3523 if (!commit_match(commit
, revs
))
3524 return commit_ignore
;
3525 if (revs
->prune
&& revs
->dense
) {
3526 /* Commit without changes? */
3527 if (commit
->object
.flags
& TREESAME
) {
3529 struct commit_list
*p
;
3530 /* drop merges unless we want parenthood */
3531 if (!want_ancestry(revs
))
3532 return commit_ignore
;
3534 * If we want ancestry, then need to keep any merges
3535 * between relevant commits to tie together topology.
3536 * For consistency with TREESAME and simplification
3537 * use "relevant" here rather than just INTERESTING,
3538 * to treat bottom commit(s) as part of the topology.
3540 for (n
= 0, p
= commit
->parents
; p
; p
= p
->next
)
3541 if (relevant_commit(p
->item
))
3544 return commit_ignore
;
3550 define_commit_slab(saved_parents
, struct commit_list
*);
3552 #define EMPTY_PARENT_LIST ((struct commit_list *)-1)
3555 * You may only call save_parents() once per commit (this is checked
3556 * for non-root commits).
3558 static void save_parents(struct rev_info
*revs
, struct commit
*commit
)
3560 struct commit_list
**pp
;
3562 if (!revs
->saved_parents_slab
) {
3563 revs
->saved_parents_slab
= xmalloc(sizeof(struct saved_parents
));
3564 init_saved_parents(revs
->saved_parents_slab
);
3567 pp
= saved_parents_at(revs
->saved_parents_slab
, commit
);
3570 * When walking with reflogs, we may visit the same commit
3571 * several times: once for each appearance in the reflog.
3573 * In this case, save_parents() will be called multiple times.
3574 * We want to keep only the first set of parents. We need to
3575 * store a sentinel value for an empty (i.e., NULL) parent
3576 * list to distinguish it from a not-yet-saved list, however.
3580 if (commit
->parents
)
3581 *pp
= copy_commit_list(commit
->parents
);
3583 *pp
= EMPTY_PARENT_LIST
;
3586 static void free_saved_parents(struct rev_info
*revs
)
3588 if (revs
->saved_parents_slab
)
3589 clear_saved_parents(revs
->saved_parents_slab
);
3592 struct commit_list
*get_saved_parents(struct rev_info
*revs
, const struct commit
*commit
)
3594 struct commit_list
*parents
;
3596 if (!revs
->saved_parents_slab
)
3597 return commit
->parents
;
3599 parents
= *saved_parents_at(revs
->saved_parents_slab
, commit
);
3600 if (parents
== EMPTY_PARENT_LIST
)
3605 enum commit_action
simplify_commit(struct rev_info
*revs
, struct commit
*commit
)
3607 enum commit_action action
= get_commit_action(revs
, commit
);
3609 if (action
== commit_show
&&
3610 revs
->prune
&& revs
->dense
&& want_ancestry(revs
)) {
3612 * --full-diff on simplified parents is no good: it
3613 * will show spurious changes from the commits that
3614 * were elided. So we save the parents on the side
3615 * when --full-diff is in effect.
3617 if (revs
->full_diff
)
3618 save_parents(revs
, commit
);
3619 if (rewrite_parents(revs
, commit
, rewrite_one
) < 0)
3620 return commit_error
;
3625 static void track_linear(struct rev_info
*revs
, struct commit
*commit
)
3627 if (revs
->track_first_time
) {
3629 revs
->track_first_time
= 0;
3631 struct commit_list
*p
;
3632 for (p
= revs
->previous_parents
; p
; p
= p
->next
)
3633 if (p
->item
== NULL
|| /* first commit */
3634 oideq(&p
->item
->object
.oid
, &commit
->object
.oid
))
3636 revs
->linear
= p
!= NULL
;
3638 if (revs
->reverse
) {
3640 commit
->object
.flags
|= TRACK_LINEAR
;
3642 free_commit_list(revs
->previous_parents
);
3643 revs
->previous_parents
= copy_commit_list(commit
->parents
);
3646 static struct commit
*get_revision_1(struct rev_info
*revs
)
3649 struct commit
*commit
;
3651 if (revs
->reflog_info
)
3652 commit
= next_reflog_entry(revs
->reflog_info
);
3653 else if (revs
->topo_walk_info
)
3654 commit
= next_topo_commit(revs
);
3656 commit
= pop_commit(&revs
->commits
);
3661 if (revs
->reflog_info
)
3662 commit
->object
.flags
&= ~(ADDED
| SEEN
| SHOWN
);
3665 * If we haven't done the list limiting, we need to look at
3666 * the parents here. We also need to do the date-based limiting
3667 * that we'd otherwise have done in limit_list().
3669 if (!revs
->limited
) {
3670 if (revs
->max_age
!= -1 &&
3671 comparison_date(revs
, commit
) < revs
->max_age
)
3674 if (revs
->reflog_info
)
3675 try_to_simplify_commit(revs
, commit
);
3676 else if (revs
->topo_walk_info
)
3677 expand_topo_walk(revs
, commit
);
3678 else if (process_parents(revs
, commit
, &revs
->commits
, NULL
) < 0) {
3679 if (!revs
->ignore_missing_links
)
3680 die("Failed to traverse parents of commit %s",
3681 oid_to_hex(&commit
->object
.oid
));
3685 switch (simplify_commit(revs
, commit
)) {
3689 die("Failed to simplify parents of commit %s",
3690 oid_to_hex(&commit
->object
.oid
));
3692 if (revs
->track_linear
)
3693 track_linear(revs
, commit
);
3700 * Return true for entries that have not yet been shown. (This is an
3701 * object_array_each_func_t.)
3703 static int entry_unshown(struct object_array_entry
*entry
, void *cb_data_unused
)
3705 return !(entry
->item
->flags
& SHOWN
);
3709 * If array is on the verge of a realloc, garbage-collect any entries
3710 * that have already been shown to try to free up some space.
3712 static void gc_boundary(struct object_array
*array
)
3714 if (array
->nr
== array
->alloc
)
3715 object_array_filter(array
, entry_unshown
, NULL
);
3718 static void create_boundary_commit_list(struct rev_info
*revs
)
3722 struct object_array
*array
= &revs
->boundary_commits
;
3723 struct object_array_entry
*objects
= array
->objects
;
3726 * If revs->commits is non-NULL at this point, an error occurred in
3727 * get_revision_1(). Ignore the error and continue printing the
3728 * boundary commits anyway. (This is what the code has always
3731 if (revs
->commits
) {
3732 free_commit_list(revs
->commits
);
3733 revs
->commits
= NULL
;
3737 * Put all of the actual boundary commits from revs->boundary_commits
3738 * into revs->commits
3740 for (i
= 0; i
< array
->nr
; i
++) {
3741 c
= (struct commit
*)(objects
[i
].item
);
3744 if (!(c
->object
.flags
& CHILD_SHOWN
))
3746 if (c
->object
.flags
& (SHOWN
| BOUNDARY
))
3748 c
->object
.flags
|= BOUNDARY
;
3749 commit_list_insert(c
, &revs
->commits
);
3753 * If revs->topo_order is set, sort the boundary commits
3754 * in topological order
3756 sort_in_topological_order(&revs
->commits
, revs
->sort_order
);
3759 static struct commit
*get_revision_internal(struct rev_info
*revs
)
3761 struct commit
*c
= NULL
;
3762 struct commit_list
*l
;
3764 if (revs
->boundary
== 2) {
3766 * All of the normal commits have already been returned,
3767 * and we are now returning boundary commits.
3768 * create_boundary_commit_list() has populated
3769 * revs->commits with the remaining commits to return.
3771 c
= pop_commit(&revs
->commits
);
3773 c
->object
.flags
|= SHOWN
;
3778 * If our max_count counter has reached zero, then we are done. We
3779 * don't simply return NULL because we still might need to show
3780 * boundary commits. But we want to avoid calling get_revision_1, which
3781 * might do a considerable amount of work finding the next commit only
3782 * for us to throw it away.
3784 * If it is non-zero, then either we don't have a max_count at all
3785 * (-1), or it is still counting, in which case we decrement.
3787 if (revs
->max_count
) {
3788 c
= get_revision_1(revs
);
3790 while (revs
->skip_count
> 0) {
3792 c
= get_revision_1(revs
);
3798 if (revs
->max_count
> 0)
3803 c
->object
.flags
|= SHOWN
;
3805 if (!revs
->boundary
)
3810 * get_revision_1() runs out the commits, and
3811 * we are done computing the boundaries.
3812 * switch to boundary commits output mode.
3817 * Update revs->commits to contain the list of
3820 create_boundary_commit_list(revs
);
3822 return get_revision_internal(revs
);
3826 * boundary commits are the commits that are parents of the
3827 * ones we got from get_revision_1() but they themselves are
3828 * not returned from get_revision_1(). Before returning
3829 * 'c', we need to mark its parents that they could be boundaries.
3832 for (l
= c
->parents
; l
; l
= l
->next
) {
3834 p
= &(l
->item
->object
);
3835 if (p
->flags
& (CHILD_SHOWN
| SHOWN
))
3837 p
->flags
|= CHILD_SHOWN
;
3838 gc_boundary(&revs
->boundary_commits
);
3839 add_object_array(p
, NULL
, &revs
->boundary_commits
);
3845 struct commit
*get_revision(struct rev_info
*revs
)
3848 struct commit_list
*reversed
;
3850 if (revs
->reverse
) {
3852 while ((c
= get_revision_internal(revs
)))
3853 commit_list_insert(c
, &reversed
);
3854 revs
->commits
= reversed
;
3856 revs
->reverse_output_stage
= 1;
3859 if (revs
->reverse_output_stage
) {
3860 c
= pop_commit(&revs
->commits
);
3861 if (revs
->track_linear
)
3862 revs
->linear
= !!(c
&& c
->object
.flags
& TRACK_LINEAR
);
3866 c
= get_revision_internal(revs
);
3867 if (c
&& revs
->graph
)
3868 graph_update(revs
->graph
, c
);
3870 free_saved_parents(revs
);
3871 if (revs
->previous_parents
) {
3872 free_commit_list(revs
->previous_parents
);
3873 revs
->previous_parents
= NULL
;
3879 char *get_revision_mark(const struct rev_info
*revs
, const struct commit
*commit
)
3881 if (commit
->object
.flags
& BOUNDARY
)
3883 else if (commit
->object
.flags
& UNINTERESTING
)
3885 else if (commit
->object
.flags
& PATCHSAME
)
3887 else if (!revs
|| revs
->left_right
) {
3888 if (commit
->object
.flags
& SYMMETRIC_LEFT
)
3892 } else if (revs
->graph
)
3894 else if (revs
->cherry_mark
)
3899 void put_revision_mark(const struct rev_info
*revs
, const struct commit
*commit
)
3901 char *mark
= get_revision_mark(revs
, commit
);
3904 fputs(mark
, stdout
);