t4015: refactor --color-moved whitespace test
[git/gitweb.git] / revision.c
blobd167223e694e54626786740954454d4973e28752
1 #include "cache.h"
2 #include "tag.h"
3 #include "blob.h"
4 #include "tree.h"
5 #include "commit.h"
6 #include "diff.h"
7 #include "refs.h"
8 #include "revision.h"
9 #include "graph.h"
10 #include "grep.h"
11 #include "reflog-walk.h"
12 #include "patch-ids.h"
13 #include "decorate.h"
14 #include "log-tree.h"
15 #include "string-list.h"
16 #include "line-log.h"
17 #include "mailmap.h"
18 #include "commit-slab.h"
19 #include "dir.h"
20 #include "cache-tree.h"
21 #include "bisect.h"
22 #include "packfile.h"
23 #include "worktree.h"
24 #include "argv-array.h"
26 volatile show_early_output_fn_t show_early_output;
28 static const char *term_bad;
29 static const char *term_good;
31 void show_object_with_name(FILE *out, struct object *obj, const char *name)
33 const char *p;
35 fprintf(out, "%s ", oid_to_hex(&obj->oid));
36 for (p = name; *p && *p != '\n'; p++)
37 fputc(*p, out);
38 fputc('\n', out);
41 static void mark_blob_uninteresting(struct blob *blob)
43 if (!blob)
44 return;
45 if (blob->object.flags & UNINTERESTING)
46 return;
47 blob->object.flags |= UNINTERESTING;
50 static void mark_tree_contents_uninteresting(struct tree *tree)
52 struct tree_desc desc;
53 struct name_entry entry;
54 struct object *obj = &tree->object;
56 if (!has_object_file(&obj->oid))
57 return;
58 if (parse_tree(tree) < 0)
59 die("bad tree %s", oid_to_hex(&obj->oid));
61 init_tree_desc(&desc, tree->buffer, tree->size);
62 while (tree_entry(&desc, &entry)) {
63 switch (object_type(entry.mode)) {
64 case OBJ_TREE:
65 mark_tree_uninteresting(lookup_tree(entry.oid));
66 break;
67 case OBJ_BLOB:
68 mark_blob_uninteresting(lookup_blob(entry.oid));
69 break;
70 default:
71 /* Subproject commit - not in this repository */
72 break;
77 * We don't care about the tree any more
78 * after it has been marked uninteresting.
80 free_tree_buffer(tree);
83 void mark_tree_uninteresting(struct tree *tree)
85 struct object *obj;
87 if (!tree)
88 return;
90 obj = &tree->object;
91 if (obj->flags & UNINTERESTING)
92 return;
93 obj->flags |= UNINTERESTING;
94 mark_tree_contents_uninteresting(tree);
97 void mark_parents_uninteresting(struct commit *commit)
99 struct commit_list *parents = NULL, *l;
101 for (l = commit->parents; l; l = l->next)
102 commit_list_insert(l->item, &parents);
104 while (parents) {
105 struct commit *commit = pop_commit(&parents);
107 while (commit) {
109 * A missing commit is ok iff its parent is marked
110 * uninteresting.
112 * We just mark such a thing parsed, so that when
113 * it is popped next time around, we won't be trying
114 * to parse it and get an error.
116 if (!has_object_file(&commit->object.oid))
117 commit->object.parsed = 1;
119 if (commit->object.flags & UNINTERESTING)
120 break;
122 commit->object.flags |= UNINTERESTING;
125 * Normally we haven't parsed the parent
126 * yet, so we won't have a parent of a parent
127 * here. However, it may turn out that we've
128 * reached this commit some other way (where it
129 * wasn't uninteresting), in which case we need
130 * to mark its parents recursively too..
132 if (!commit->parents)
133 break;
135 for (l = commit->parents->next; l; l = l->next)
136 commit_list_insert(l->item, &parents);
137 commit = commit->parents->item;
142 static void add_pending_object_with_path(struct rev_info *revs,
143 struct object *obj,
144 const char *name, unsigned mode,
145 const char *path)
147 if (!obj)
148 return;
149 if (revs->no_walk && (obj->flags & UNINTERESTING))
150 revs->no_walk = 0;
151 if (revs->reflog_info && obj->type == OBJ_COMMIT) {
152 struct strbuf buf = STRBUF_INIT;
153 int len = interpret_branch_name(name, 0, &buf, 0);
155 if (0 < len && name[len] && buf.len)
156 strbuf_addstr(&buf, name + len);
157 add_reflog_for_walk(revs->reflog_info,
158 (struct commit *)obj,
159 buf.buf[0] ? buf.buf: name);
160 strbuf_release(&buf);
161 return; /* do not add the commit itself */
163 add_object_array_with_path(obj, name, &revs->pending, mode, path);
166 static void add_pending_object_with_mode(struct rev_info *revs,
167 struct object *obj,
168 const char *name, unsigned mode)
170 add_pending_object_with_path(revs, obj, name, mode, NULL);
173 void add_pending_object(struct rev_info *revs,
174 struct object *obj, const char *name)
176 add_pending_object_with_mode(revs, obj, name, S_IFINVALID);
179 void add_head_to_pending(struct rev_info *revs)
181 struct object_id oid;
182 struct object *obj;
183 if (get_oid("HEAD", &oid))
184 return;
185 obj = parse_object(&oid);
186 if (!obj)
187 return;
188 add_pending_object(revs, obj, "HEAD");
191 static struct object *get_reference(struct rev_info *revs, const char *name,
192 const struct object_id *oid,
193 unsigned int flags)
195 struct object *object;
197 object = parse_object(oid);
198 if (!object) {
199 if (revs->ignore_missing)
200 return object;
201 die("bad object %s", name);
203 object->flags |= flags;
204 return object;
207 void add_pending_oid(struct rev_info *revs, const char *name,
208 const struct object_id *oid, unsigned int flags)
210 struct object *object = get_reference(revs, name, oid, flags);
211 add_pending_object(revs, object, name);
214 static struct commit *handle_commit(struct rev_info *revs,
215 struct object_array_entry *entry)
217 struct object *object = entry->item;
218 const char *name = entry->name;
219 const char *path = entry->path;
220 unsigned int mode = entry->mode;
221 unsigned long flags = object->flags;
224 * Tag object? Look what it points to..
226 while (object->type == OBJ_TAG) {
227 struct tag *tag = (struct tag *) object;
228 if (revs->tag_objects && !(flags & UNINTERESTING))
229 add_pending_object(revs, object, tag->tag);
230 if (!tag->tagged)
231 die("bad tag");
232 object = parse_object(&tag->tagged->oid);
233 if (!object) {
234 if (revs->ignore_missing_links || (flags & UNINTERESTING))
235 return NULL;
236 die("bad object %s", oid_to_hex(&tag->tagged->oid));
238 object->flags |= flags;
240 * We'll handle the tagged object by looping or dropping
241 * through to the non-tag handlers below. Do not
242 * propagate path data from the tag's pending entry.
244 path = NULL;
245 mode = 0;
249 * Commit object? Just return it, we'll do all the complex
250 * reachability crud.
252 if (object->type == OBJ_COMMIT) {
253 struct commit *commit = (struct commit *)object;
254 if (parse_commit(commit) < 0)
255 die("unable to parse commit %s", name);
256 if (flags & UNINTERESTING) {
257 mark_parents_uninteresting(commit);
258 revs->limited = 1;
260 if (revs->show_source && !commit->util)
261 commit->util = xstrdup(name);
262 return commit;
266 * Tree object? Either mark it uninteresting, or add it
267 * to the list of objects to look at later..
269 if (object->type == OBJ_TREE) {
270 struct tree *tree = (struct tree *)object;
271 if (!revs->tree_objects)
272 return NULL;
273 if (flags & UNINTERESTING) {
274 mark_tree_contents_uninteresting(tree);
275 return NULL;
277 add_pending_object_with_path(revs, object, name, mode, path);
278 return NULL;
282 * Blob object? You know the drill by now..
284 if (object->type == OBJ_BLOB) {
285 if (!revs->blob_objects)
286 return NULL;
287 if (flags & UNINTERESTING)
288 return NULL;
289 add_pending_object_with_path(revs, object, name, mode, path);
290 return NULL;
292 die("%s is unknown object", name);
295 static int everybody_uninteresting(struct commit_list *orig,
296 struct commit **interesting_cache)
298 struct commit_list *list = orig;
300 if (*interesting_cache) {
301 struct commit *commit = *interesting_cache;
302 if (!(commit->object.flags & UNINTERESTING))
303 return 0;
306 while (list) {
307 struct commit *commit = list->item;
308 list = list->next;
309 if (commit->object.flags & UNINTERESTING)
310 continue;
312 *interesting_cache = commit;
313 return 0;
315 return 1;
319 * A definition of "relevant" commit that we can use to simplify limited graphs
320 * by eliminating side branches.
322 * A "relevant" commit is one that is !UNINTERESTING (ie we are including it
323 * in our list), or that is a specified BOTTOM commit. Then after computing
324 * a limited list, during processing we can generally ignore boundary merges
325 * coming from outside the graph, (ie from irrelevant parents), and treat
326 * those merges as if they were single-parent. TREESAME is defined to consider
327 * only relevant parents, if any. If we are TREESAME to our on-graph parents,
328 * we don't care if we were !TREESAME to non-graph parents.
330 * Treating bottom commits as relevant ensures that a limited graph's
331 * connection to the actual bottom commit is not viewed as a side branch, but
332 * treated as part of the graph. For example:
334 * ....Z...A---X---o---o---B
335 * . /
336 * W---Y
338 * When computing "A..B", the A-X connection is at least as important as
339 * Y-X, despite A being flagged UNINTERESTING.
341 * And when computing --ancestry-path "A..B", the A-X connection is more
342 * important than Y-X, despite both A and Y being flagged UNINTERESTING.
344 static inline int relevant_commit(struct commit *commit)
346 return (commit->object.flags & (UNINTERESTING | BOTTOM)) != UNINTERESTING;
350 * Return a single relevant commit from a parent list. If we are a TREESAME
351 * commit, and this selects one of our parents, then we can safely simplify to
352 * that parent.
354 static struct commit *one_relevant_parent(const struct rev_info *revs,
355 struct commit_list *orig)
357 struct commit_list *list = orig;
358 struct commit *relevant = NULL;
360 if (!orig)
361 return NULL;
364 * For 1-parent commits, or if first-parent-only, then return that
365 * first parent (even if not "relevant" by the above definition).
366 * TREESAME will have been set purely on that parent.
368 if (revs->first_parent_only || !orig->next)
369 return orig->item;
372 * For multi-parent commits, identify a sole relevant parent, if any.
373 * If we have only one relevant parent, then TREESAME will be set purely
374 * with regard to that parent, and we can simplify accordingly.
376 * If we have more than one relevant parent, or no relevant parents
377 * (and multiple irrelevant ones), then we can't select a parent here
378 * and return NULL.
380 while (list) {
381 struct commit *commit = list->item;
382 list = list->next;
383 if (relevant_commit(commit)) {
384 if (relevant)
385 return NULL;
386 relevant = commit;
389 return relevant;
393 * The goal is to get REV_TREE_NEW as the result only if the
394 * diff consists of all '+' (and no other changes), REV_TREE_OLD
395 * if the whole diff is removal of old data, and otherwise
396 * REV_TREE_DIFFERENT (of course if the trees are the same we
397 * want REV_TREE_SAME).
398 * That means that once we get to REV_TREE_DIFFERENT, we do not
399 * have to look any further.
401 static int tree_difference = REV_TREE_SAME;
403 static void file_add_remove(struct diff_options *options,
404 int addremove, unsigned mode,
405 const struct object_id *oid,
406 int oid_valid,
407 const char *fullpath, unsigned dirty_submodule)
409 int diff = addremove == '+' ? REV_TREE_NEW : REV_TREE_OLD;
411 tree_difference |= diff;
412 if (tree_difference == REV_TREE_DIFFERENT)
413 DIFF_OPT_SET(options, HAS_CHANGES);
416 static void file_change(struct diff_options *options,
417 unsigned old_mode, unsigned new_mode,
418 const struct object_id *old_oid,
419 const struct object_id *new_oid,
420 int old_oid_valid, int new_oid_valid,
421 const char *fullpath,
422 unsigned old_dirty_submodule, unsigned new_dirty_submodule)
424 tree_difference = REV_TREE_DIFFERENT;
425 DIFF_OPT_SET(options, HAS_CHANGES);
428 static int rev_compare_tree(struct rev_info *revs,
429 struct commit *parent, struct commit *commit)
431 struct tree *t1 = parent->tree;
432 struct tree *t2 = commit->tree;
434 if (!t1)
435 return REV_TREE_NEW;
436 if (!t2)
437 return REV_TREE_OLD;
439 if (revs->simplify_by_decoration) {
441 * If we are simplifying by decoration, then the commit
442 * is worth showing if it has a tag pointing at it.
444 if (get_name_decoration(&commit->object))
445 return REV_TREE_DIFFERENT;
447 * A commit that is not pointed by a tag is uninteresting
448 * if we are not limited by path. This means that you will
449 * see the usual "commits that touch the paths" plus any
450 * tagged commit by specifying both --simplify-by-decoration
451 * and pathspec.
453 if (!revs->prune_data.nr)
454 return REV_TREE_SAME;
457 tree_difference = REV_TREE_SAME;
458 DIFF_OPT_CLR(&revs->pruning, HAS_CHANGES);
459 if (diff_tree_oid(&t1->object.oid, &t2->object.oid, "",
460 &revs->pruning) < 0)
461 return REV_TREE_DIFFERENT;
462 return tree_difference;
465 static int rev_same_tree_as_empty(struct rev_info *revs, struct commit *commit)
467 int retval;
468 struct tree *t1 = commit->tree;
470 if (!t1)
471 return 0;
473 tree_difference = REV_TREE_SAME;
474 DIFF_OPT_CLR(&revs->pruning, HAS_CHANGES);
475 retval = diff_tree_oid(NULL, &t1->object.oid, "", &revs->pruning);
477 return retval >= 0 && (tree_difference == REV_TREE_SAME);
480 struct treesame_state {
481 unsigned int nparents;
482 unsigned char treesame[FLEX_ARRAY];
485 static struct treesame_state *initialise_treesame(struct rev_info *revs, struct commit *commit)
487 unsigned n = commit_list_count(commit->parents);
488 struct treesame_state *st = xcalloc(1, st_add(sizeof(*st), n));
489 st->nparents = n;
490 add_decoration(&revs->treesame, &commit->object, st);
491 return st;
495 * Must be called immediately after removing the nth_parent from a commit's
496 * parent list, if we are maintaining the per-parent treesame[] decoration.
497 * This does not recalculate the master TREESAME flag - update_treesame()
498 * should be called to update it after a sequence of treesame[] modifications
499 * that may have affected it.
501 static int compact_treesame(struct rev_info *revs, struct commit *commit, unsigned nth_parent)
503 struct treesame_state *st;
504 int old_same;
506 if (!commit->parents) {
508 * Have just removed the only parent from a non-merge.
509 * Different handling, as we lack decoration.
511 if (nth_parent != 0)
512 die("compact_treesame %u", nth_parent);
513 old_same = !!(commit->object.flags & TREESAME);
514 if (rev_same_tree_as_empty(revs, commit))
515 commit->object.flags |= TREESAME;
516 else
517 commit->object.flags &= ~TREESAME;
518 return old_same;
521 st = lookup_decoration(&revs->treesame, &commit->object);
522 if (!st || nth_parent >= st->nparents)
523 die("compact_treesame %u", nth_parent);
525 old_same = st->treesame[nth_parent];
526 memmove(st->treesame + nth_parent,
527 st->treesame + nth_parent + 1,
528 st->nparents - nth_parent - 1);
531 * If we've just become a non-merge commit, update TREESAME
532 * immediately, and remove the no-longer-needed decoration.
533 * If still a merge, defer update until update_treesame().
535 if (--st->nparents == 1) {
536 if (commit->parents->next)
537 die("compact_treesame parents mismatch");
538 if (st->treesame[0] && revs->dense)
539 commit->object.flags |= TREESAME;
540 else
541 commit->object.flags &= ~TREESAME;
542 free(add_decoration(&revs->treesame, &commit->object, NULL));
545 return old_same;
548 static unsigned update_treesame(struct rev_info *revs, struct commit *commit)
550 if (commit->parents && commit->parents->next) {
551 unsigned n;
552 struct treesame_state *st;
553 struct commit_list *p;
554 unsigned relevant_parents;
555 unsigned relevant_change, irrelevant_change;
557 st = lookup_decoration(&revs->treesame, &commit->object);
558 if (!st)
559 die("update_treesame %s", oid_to_hex(&commit->object.oid));
560 relevant_parents = 0;
561 relevant_change = irrelevant_change = 0;
562 for (p = commit->parents, n = 0; p; n++, p = p->next) {
563 if (relevant_commit(p->item)) {
564 relevant_change |= !st->treesame[n];
565 relevant_parents++;
566 } else
567 irrelevant_change |= !st->treesame[n];
569 if (relevant_parents ? relevant_change : irrelevant_change)
570 commit->object.flags &= ~TREESAME;
571 else
572 commit->object.flags |= TREESAME;
575 return commit->object.flags & TREESAME;
578 static inline int limiting_can_increase_treesame(const struct rev_info *revs)
581 * TREESAME is irrelevant unless prune && dense;
582 * if simplify_history is set, we can't have a mixture of TREESAME and
583 * !TREESAME INTERESTING parents (and we don't have treesame[]
584 * decoration anyway);
585 * if first_parent_only is set, then the TREESAME flag is locked
586 * against the first parent (and again we lack treesame[] decoration).
588 return revs->prune && revs->dense &&
589 !revs->simplify_history &&
590 !revs->first_parent_only;
593 static void try_to_simplify_commit(struct rev_info *revs, struct commit *commit)
595 struct commit_list **pp, *parent;
596 struct treesame_state *ts = NULL;
597 int relevant_change = 0, irrelevant_change = 0;
598 int relevant_parents, nth_parent;
601 * If we don't do pruning, everything is interesting
603 if (!revs->prune)
604 return;
606 if (!commit->tree)
607 return;
609 if (!commit->parents) {
610 if (rev_same_tree_as_empty(revs, commit))
611 commit->object.flags |= TREESAME;
612 return;
616 * Normal non-merge commit? If we don't want to make the
617 * history dense, we consider it always to be a change..
619 if (!revs->dense && !commit->parents->next)
620 return;
622 for (pp = &commit->parents, nth_parent = 0, relevant_parents = 0;
623 (parent = *pp) != NULL;
624 pp = &parent->next, nth_parent++) {
625 struct commit *p = parent->item;
626 if (relevant_commit(p))
627 relevant_parents++;
629 if (nth_parent == 1) {
631 * This our second loop iteration - so we now know
632 * we're dealing with a merge.
634 * Do not compare with later parents when we care only about
635 * the first parent chain, in order to avoid derailing the
636 * traversal to follow a side branch that brought everything
637 * in the path we are limited to by the pathspec.
639 if (revs->first_parent_only)
640 break;
642 * If this will remain a potentially-simplifiable
643 * merge, remember per-parent treesame if needed.
644 * Initialise the array with the comparison from our
645 * first iteration.
647 if (revs->treesame.name &&
648 !revs->simplify_history &&
649 !(commit->object.flags & UNINTERESTING)) {
650 ts = initialise_treesame(revs, commit);
651 if (!(irrelevant_change || relevant_change))
652 ts->treesame[0] = 1;
655 if (parse_commit(p) < 0)
656 die("cannot simplify commit %s (because of %s)",
657 oid_to_hex(&commit->object.oid),
658 oid_to_hex(&p->object.oid));
659 switch (rev_compare_tree(revs, p, commit)) {
660 case REV_TREE_SAME:
661 if (!revs->simplify_history || !relevant_commit(p)) {
662 /* Even if a merge with an uninteresting
663 * side branch brought the entire change
664 * we are interested in, we do not want
665 * to lose the other branches of this
666 * merge, so we just keep going.
668 if (ts)
669 ts->treesame[nth_parent] = 1;
670 continue;
672 parent->next = NULL;
673 commit->parents = parent;
674 commit->object.flags |= TREESAME;
675 return;
677 case REV_TREE_NEW:
678 if (revs->remove_empty_trees &&
679 rev_same_tree_as_empty(revs, p)) {
680 /* We are adding all the specified
681 * paths from this parent, so the
682 * history beyond this parent is not
683 * interesting. Remove its parents
684 * (they are grandparents for us).
685 * IOW, we pretend this parent is a
686 * "root" commit.
688 if (parse_commit(p) < 0)
689 die("cannot simplify commit %s (invalid %s)",
690 oid_to_hex(&commit->object.oid),
691 oid_to_hex(&p->object.oid));
692 p->parents = NULL;
694 /* fallthrough */
695 case REV_TREE_OLD:
696 case REV_TREE_DIFFERENT:
697 if (relevant_commit(p))
698 relevant_change = 1;
699 else
700 irrelevant_change = 1;
701 continue;
703 die("bad tree compare for commit %s", oid_to_hex(&commit->object.oid));
707 * TREESAME is straightforward for single-parent commits. For merge
708 * commits, it is most useful to define it so that "irrelevant"
709 * parents cannot make us !TREESAME - if we have any relevant
710 * parents, then we only consider TREESAMEness with respect to them,
711 * allowing irrelevant merges from uninteresting branches to be
712 * simplified away. Only if we have only irrelevant parents do we
713 * base TREESAME on them. Note that this logic is replicated in
714 * update_treesame, which should be kept in sync.
716 if (relevant_parents ? !relevant_change : !irrelevant_change)
717 commit->object.flags |= TREESAME;
720 static void commit_list_insert_by_date_cached(struct commit *p, struct commit_list **head,
721 struct commit_list *cached_base, struct commit_list **cache)
723 struct commit_list *new_entry;
725 if (cached_base && p->date < cached_base->item->date)
726 new_entry = commit_list_insert_by_date(p, &cached_base->next);
727 else
728 new_entry = commit_list_insert_by_date(p, head);
730 if (cache && (!*cache || p->date < (*cache)->item->date))
731 *cache = new_entry;
734 static int add_parents_to_list(struct rev_info *revs, struct commit *commit,
735 struct commit_list **list, struct commit_list **cache_ptr)
737 struct commit_list *parent = commit->parents;
738 unsigned left_flag;
739 struct commit_list *cached_base = cache_ptr ? *cache_ptr : NULL;
741 if (commit->object.flags & ADDED)
742 return 0;
743 commit->object.flags |= ADDED;
745 if (revs->include_check &&
746 !revs->include_check(commit, revs->include_check_data))
747 return 0;
750 * If the commit is uninteresting, don't try to
751 * prune parents - we want the maximal uninteresting
752 * set.
754 * Normally we haven't parsed the parent
755 * yet, so we won't have a parent of a parent
756 * here. However, it may turn out that we've
757 * reached this commit some other way (where it
758 * wasn't uninteresting), in which case we need
759 * to mark its parents recursively too..
761 if (commit->object.flags & UNINTERESTING) {
762 while (parent) {
763 struct commit *p = parent->item;
764 parent = parent->next;
765 if (p)
766 p->object.flags |= UNINTERESTING;
767 if (parse_commit_gently(p, 1) < 0)
768 continue;
769 if (p->parents)
770 mark_parents_uninteresting(p);
771 if (p->object.flags & SEEN)
772 continue;
773 p->object.flags |= SEEN;
774 commit_list_insert_by_date_cached(p, list, cached_base, cache_ptr);
776 return 0;
780 * Ok, the commit wasn't uninteresting. Try to
781 * simplify the commit history and find the parent
782 * that has no differences in the path set if one exists.
784 try_to_simplify_commit(revs, commit);
786 if (revs->no_walk)
787 return 0;
789 left_flag = (commit->object.flags & SYMMETRIC_LEFT);
791 for (parent = commit->parents; parent; parent = parent->next) {
792 struct commit *p = parent->item;
794 if (parse_commit_gently(p, revs->ignore_missing_links) < 0)
795 return -1;
796 if (revs->show_source && !p->util)
797 p->util = commit->util;
798 p->object.flags |= left_flag;
799 if (!(p->object.flags & SEEN)) {
800 p->object.flags |= SEEN;
801 commit_list_insert_by_date_cached(p, list, cached_base, cache_ptr);
803 if (revs->first_parent_only)
804 break;
806 return 0;
809 static void cherry_pick_list(struct commit_list *list, struct rev_info *revs)
811 struct commit_list *p;
812 int left_count = 0, right_count = 0;
813 int left_first;
814 struct patch_ids ids;
815 unsigned cherry_flag;
817 /* First count the commits on the left and on the right */
818 for (p = list; p; p = p->next) {
819 struct commit *commit = p->item;
820 unsigned flags = commit->object.flags;
821 if (flags & BOUNDARY)
823 else if (flags & SYMMETRIC_LEFT)
824 left_count++;
825 else
826 right_count++;
829 if (!left_count || !right_count)
830 return;
832 left_first = left_count < right_count;
833 init_patch_ids(&ids);
834 ids.diffopts.pathspec = revs->diffopt.pathspec;
836 /* Compute patch-ids for one side */
837 for (p = list; p; p = p->next) {
838 struct commit *commit = p->item;
839 unsigned flags = commit->object.flags;
841 if (flags & BOUNDARY)
842 continue;
844 * If we have fewer left, left_first is set and we omit
845 * commits on the right branch in this loop. If we have
846 * fewer right, we skip the left ones.
848 if (left_first != !!(flags & SYMMETRIC_LEFT))
849 continue;
850 add_commit_patch_id(commit, &ids);
853 /* either cherry_mark or cherry_pick are true */
854 cherry_flag = revs->cherry_mark ? PATCHSAME : SHOWN;
856 /* Check the other side */
857 for (p = list; p; p = p->next) {
858 struct commit *commit = p->item;
859 struct patch_id *id;
860 unsigned flags = commit->object.flags;
862 if (flags & BOUNDARY)
863 continue;
865 * If we have fewer left, left_first is set and we omit
866 * commits on the left branch in this loop.
868 if (left_first == !!(flags & SYMMETRIC_LEFT))
869 continue;
872 * Have we seen the same patch id?
874 id = has_commit_patch_id(commit, &ids);
875 if (!id)
876 continue;
878 commit->object.flags |= cherry_flag;
879 id->commit->object.flags |= cherry_flag;
882 free_patch_ids(&ids);
885 /* How many extra uninteresting commits we want to see.. */
886 #define SLOP 5
888 static int still_interesting(struct commit_list *src, timestamp_t date, int slop,
889 struct commit **interesting_cache)
892 * No source list at all? We're definitely done..
894 if (!src)
895 return 0;
898 * Does the destination list contain entries with a date
899 * before the source list? Definitely _not_ done.
901 if (date <= src->item->date)
902 return SLOP;
905 * Does the source list still have interesting commits in
906 * it? Definitely not done..
908 if (!everybody_uninteresting(src, interesting_cache))
909 return SLOP;
911 /* Ok, we're closing in.. */
912 return slop-1;
916 * "rev-list --ancestry-path A..B" computes commits that are ancestors
917 * of B but not ancestors of A but further limits the result to those
918 * that are descendants of A. This takes the list of bottom commits and
919 * the result of "A..B" without --ancestry-path, and limits the latter
920 * further to the ones that can reach one of the commits in "bottom".
922 static void limit_to_ancestry(struct commit_list *bottom, struct commit_list *list)
924 struct commit_list *p;
925 struct commit_list *rlist = NULL;
926 int made_progress;
929 * Reverse the list so that it will be likely that we would
930 * process parents before children.
932 for (p = list; p; p = p->next)
933 commit_list_insert(p->item, &rlist);
935 for (p = bottom; p; p = p->next)
936 p->item->object.flags |= TMP_MARK;
939 * Mark the ones that can reach bottom commits in "list",
940 * in a bottom-up fashion.
942 do {
943 made_progress = 0;
944 for (p = rlist; p; p = p->next) {
945 struct commit *c = p->item;
946 struct commit_list *parents;
947 if (c->object.flags & (TMP_MARK | UNINTERESTING))
948 continue;
949 for (parents = c->parents;
950 parents;
951 parents = parents->next) {
952 if (!(parents->item->object.flags & TMP_MARK))
953 continue;
954 c->object.flags |= TMP_MARK;
955 made_progress = 1;
956 break;
959 } while (made_progress);
962 * NEEDSWORK: decide if we want to remove parents that are
963 * not marked with TMP_MARK from commit->parents for commits
964 * in the resulting list. We may not want to do that, though.
968 * The ones that are not marked with TMP_MARK are uninteresting
970 for (p = list; p; p = p->next) {
971 struct commit *c = p->item;
972 if (c->object.flags & TMP_MARK)
973 continue;
974 c->object.flags |= UNINTERESTING;
977 /* We are done with the TMP_MARK */
978 for (p = list; p; p = p->next)
979 p->item->object.flags &= ~TMP_MARK;
980 for (p = bottom; p; p = p->next)
981 p->item->object.flags &= ~TMP_MARK;
982 free_commit_list(rlist);
986 * Before walking the history, keep the set of "negative" refs the
987 * caller has asked to exclude.
989 * This is used to compute "rev-list --ancestry-path A..B", as we need
990 * to filter the result of "A..B" further to the ones that can actually
991 * reach A.
993 static struct commit_list *collect_bottom_commits(struct commit_list *list)
995 struct commit_list *elem, *bottom = NULL;
996 for (elem = list; elem; elem = elem->next)
997 if (elem->item->object.flags & BOTTOM)
998 commit_list_insert(elem->item, &bottom);
999 return bottom;
1002 /* Assumes either left_only or right_only is set */
1003 static void limit_left_right(struct commit_list *list, struct rev_info *revs)
1005 struct commit_list *p;
1007 for (p = list; p; p = p->next) {
1008 struct commit *commit = p->item;
1010 if (revs->right_only) {
1011 if (commit->object.flags & SYMMETRIC_LEFT)
1012 commit->object.flags |= SHOWN;
1013 } else /* revs->left_only is set */
1014 if (!(commit->object.flags & SYMMETRIC_LEFT))
1015 commit->object.flags |= SHOWN;
1019 static int limit_list(struct rev_info *revs)
1021 int slop = SLOP;
1022 timestamp_t date = TIME_MAX;
1023 struct commit_list *list = revs->commits;
1024 struct commit_list *newlist = NULL;
1025 struct commit_list **p = &newlist;
1026 struct commit_list *bottom = NULL;
1027 struct commit *interesting_cache = NULL;
1029 if (revs->ancestry_path) {
1030 bottom = collect_bottom_commits(list);
1031 if (!bottom)
1032 die("--ancestry-path given but there are no bottom commits");
1035 while (list) {
1036 struct commit *commit = pop_commit(&list);
1037 struct object *obj = &commit->object;
1038 show_early_output_fn_t show;
1040 if (commit == interesting_cache)
1041 interesting_cache = NULL;
1043 if (revs->max_age != -1 && (commit->date < revs->max_age))
1044 obj->flags |= UNINTERESTING;
1045 if (add_parents_to_list(revs, commit, &list, NULL) < 0)
1046 return -1;
1047 if (obj->flags & UNINTERESTING) {
1048 mark_parents_uninteresting(commit);
1049 if (revs->show_all)
1050 p = &commit_list_insert(commit, p)->next;
1051 slop = still_interesting(list, date, slop, &interesting_cache);
1052 if (slop)
1053 continue;
1054 /* If showing all, add the whole pending list to the end */
1055 if (revs->show_all)
1056 *p = list;
1057 break;
1059 if (revs->min_age != -1 && (commit->date > revs->min_age))
1060 continue;
1061 date = commit->date;
1062 p = &commit_list_insert(commit, p)->next;
1064 show = show_early_output;
1065 if (!show)
1066 continue;
1068 show(revs, newlist);
1069 show_early_output = NULL;
1071 if (revs->cherry_pick || revs->cherry_mark)
1072 cherry_pick_list(newlist, revs);
1074 if (revs->left_only || revs->right_only)
1075 limit_left_right(newlist, revs);
1077 if (bottom) {
1078 limit_to_ancestry(bottom, newlist);
1079 free_commit_list(bottom);
1083 * Check if any commits have become TREESAME by some of their parents
1084 * becoming UNINTERESTING.
1086 if (limiting_can_increase_treesame(revs))
1087 for (list = newlist; list; list = list->next) {
1088 struct commit *c = list->item;
1089 if (c->object.flags & (UNINTERESTING | TREESAME))
1090 continue;
1091 update_treesame(revs, c);
1094 revs->commits = newlist;
1095 return 0;
1099 * Add an entry to refs->cmdline with the specified information.
1100 * *name is copied.
1102 static void add_rev_cmdline(struct rev_info *revs,
1103 struct object *item,
1104 const char *name,
1105 int whence,
1106 unsigned flags)
1108 struct rev_cmdline_info *info = &revs->cmdline;
1109 unsigned int nr = info->nr;
1111 ALLOC_GROW(info->rev, nr + 1, info->alloc);
1112 info->rev[nr].item = item;
1113 info->rev[nr].name = xstrdup(name);
1114 info->rev[nr].whence = whence;
1115 info->rev[nr].flags = flags;
1116 info->nr++;
1119 static void add_rev_cmdline_list(struct rev_info *revs,
1120 struct commit_list *commit_list,
1121 int whence,
1122 unsigned flags)
1124 while (commit_list) {
1125 struct object *object = &commit_list->item->object;
1126 add_rev_cmdline(revs, object, oid_to_hex(&object->oid),
1127 whence, flags);
1128 commit_list = commit_list->next;
1132 struct all_refs_cb {
1133 int all_flags;
1134 int warned_bad_reflog;
1135 struct rev_info *all_revs;
1136 const char *name_for_errormsg;
1137 struct ref_store *refs;
1140 int ref_excluded(struct string_list *ref_excludes, const char *path)
1142 struct string_list_item *item;
1144 if (!ref_excludes)
1145 return 0;
1146 for_each_string_list_item(item, ref_excludes) {
1147 if (!wildmatch(item->string, path, 0))
1148 return 1;
1150 return 0;
1153 static int handle_one_ref(const char *path, const struct object_id *oid,
1154 int flag, void *cb_data)
1156 struct all_refs_cb *cb = cb_data;
1157 struct object *object;
1159 if (ref_excluded(cb->all_revs->ref_excludes, path))
1160 return 0;
1162 object = get_reference(cb->all_revs, path, oid, cb->all_flags);
1163 add_rev_cmdline(cb->all_revs, object, path, REV_CMD_REF, cb->all_flags);
1164 add_pending_oid(cb->all_revs, path, oid, cb->all_flags);
1165 return 0;
1168 static void init_all_refs_cb(struct all_refs_cb *cb, struct rev_info *revs,
1169 unsigned flags)
1171 cb->all_revs = revs;
1172 cb->all_flags = flags;
1173 revs->rev_input_given = 1;
1174 cb->refs = NULL;
1177 void clear_ref_exclusion(struct string_list **ref_excludes_p)
1179 if (*ref_excludes_p) {
1180 string_list_clear(*ref_excludes_p, 0);
1181 free(*ref_excludes_p);
1183 *ref_excludes_p = NULL;
1186 void add_ref_exclusion(struct string_list **ref_excludes_p, const char *exclude)
1188 if (!*ref_excludes_p) {
1189 *ref_excludes_p = xcalloc(1, sizeof(**ref_excludes_p));
1190 (*ref_excludes_p)->strdup_strings = 1;
1192 string_list_append(*ref_excludes_p, exclude);
1195 static void handle_refs(struct ref_store *refs,
1196 struct rev_info *revs, unsigned flags,
1197 int (*for_each)(struct ref_store *, each_ref_fn, void *))
1199 struct all_refs_cb cb;
1201 if (!refs) {
1202 /* this could happen with uninitialized submodules */
1203 return;
1206 init_all_refs_cb(&cb, revs, flags);
1207 for_each(refs, handle_one_ref, &cb);
1210 static void handle_one_reflog_commit(struct object_id *oid, void *cb_data)
1212 struct all_refs_cb *cb = cb_data;
1213 if (!is_null_oid(oid)) {
1214 struct object *o = parse_object(oid);
1215 if (o) {
1216 o->flags |= cb->all_flags;
1217 /* ??? CMDLINEFLAGS ??? */
1218 add_pending_object(cb->all_revs, o, "");
1220 else if (!cb->warned_bad_reflog) {
1221 warning("reflog of '%s' references pruned commits",
1222 cb->name_for_errormsg);
1223 cb->warned_bad_reflog = 1;
1228 static int handle_one_reflog_ent(struct object_id *ooid, struct object_id *noid,
1229 const char *email, timestamp_t timestamp, int tz,
1230 const char *message, void *cb_data)
1232 handle_one_reflog_commit(ooid, cb_data);
1233 handle_one_reflog_commit(noid, cb_data);
1234 return 0;
1237 static int handle_one_reflog(const char *path, const struct object_id *oid,
1238 int flag, void *cb_data)
1240 struct all_refs_cb *cb = cb_data;
1241 cb->warned_bad_reflog = 0;
1242 cb->name_for_errormsg = path;
1243 refs_for_each_reflog_ent(cb->refs, path,
1244 handle_one_reflog_ent, cb_data);
1245 return 0;
1248 static void add_other_reflogs_to_pending(struct all_refs_cb *cb)
1250 struct worktree **worktrees, **p;
1252 worktrees = get_worktrees(0);
1253 for (p = worktrees; *p; p++) {
1254 struct worktree *wt = *p;
1256 if (wt->is_current)
1257 continue;
1259 cb->refs = get_worktree_ref_store(wt);
1260 refs_for_each_reflog(cb->refs,
1261 handle_one_reflog,
1262 cb);
1264 free_worktrees(worktrees);
1267 void add_reflogs_to_pending(struct rev_info *revs, unsigned flags)
1269 struct all_refs_cb cb;
1271 cb.all_revs = revs;
1272 cb.all_flags = flags;
1273 cb.refs = get_main_ref_store();
1274 for_each_reflog(handle_one_reflog, &cb);
1276 if (!revs->single_worktree)
1277 add_other_reflogs_to_pending(&cb);
1280 static void add_cache_tree(struct cache_tree *it, struct rev_info *revs,
1281 struct strbuf *path)
1283 size_t baselen = path->len;
1284 int i;
1286 if (it->entry_count >= 0) {
1287 struct tree *tree = lookup_tree(&it->oid);
1288 add_pending_object_with_path(revs, &tree->object, "",
1289 040000, path->buf);
1292 for (i = 0; i < it->subtree_nr; i++) {
1293 struct cache_tree_sub *sub = it->down[i];
1294 strbuf_addf(path, "%s%s", baselen ? "/" : "", sub->name);
1295 add_cache_tree(sub->cache_tree, revs, path);
1296 strbuf_setlen(path, baselen);
1301 static void do_add_index_objects_to_pending(struct rev_info *revs,
1302 struct index_state *istate)
1304 int i;
1306 for (i = 0; i < istate->cache_nr; i++) {
1307 struct cache_entry *ce = istate->cache[i];
1308 struct blob *blob;
1310 if (S_ISGITLINK(ce->ce_mode))
1311 continue;
1313 blob = lookup_blob(&ce->oid);
1314 if (!blob)
1315 die("unable to add index blob to traversal");
1316 add_pending_object_with_path(revs, &blob->object, "",
1317 ce->ce_mode, ce->name);
1320 if (istate->cache_tree) {
1321 struct strbuf path = STRBUF_INIT;
1322 add_cache_tree(istate->cache_tree, revs, &path);
1323 strbuf_release(&path);
1327 void add_index_objects_to_pending(struct rev_info *revs, unsigned int flags)
1329 struct worktree **worktrees, **p;
1331 read_cache();
1332 do_add_index_objects_to_pending(revs, &the_index);
1334 if (revs->single_worktree)
1335 return;
1337 worktrees = get_worktrees(0);
1338 for (p = worktrees; *p; p++) {
1339 struct worktree *wt = *p;
1340 struct index_state istate = { NULL };
1342 if (wt->is_current)
1343 continue; /* current index already taken care of */
1345 if (read_index_from(&istate,
1346 worktree_git_path(wt, "index")) > 0)
1347 do_add_index_objects_to_pending(revs, &istate);
1348 discard_index(&istate);
1350 free_worktrees(worktrees);
1353 static int add_parents_only(struct rev_info *revs, const char *arg_, int flags,
1354 int exclude_parent)
1356 struct object_id oid;
1357 struct object *it;
1358 struct commit *commit;
1359 struct commit_list *parents;
1360 int parent_number;
1361 const char *arg = arg_;
1363 if (*arg == '^') {
1364 flags ^= UNINTERESTING | BOTTOM;
1365 arg++;
1367 if (get_oid_committish(arg, &oid))
1368 return 0;
1369 while (1) {
1370 it = get_reference(revs, arg, &oid, 0);
1371 if (!it && revs->ignore_missing)
1372 return 0;
1373 if (it->type != OBJ_TAG)
1374 break;
1375 if (!((struct tag*)it)->tagged)
1376 return 0;
1377 oidcpy(&oid, &((struct tag*)it)->tagged->oid);
1379 if (it->type != OBJ_COMMIT)
1380 return 0;
1381 commit = (struct commit *)it;
1382 if (exclude_parent &&
1383 exclude_parent > commit_list_count(commit->parents))
1384 return 0;
1385 for (parents = commit->parents, parent_number = 1;
1386 parents;
1387 parents = parents->next, parent_number++) {
1388 if (exclude_parent && parent_number != exclude_parent)
1389 continue;
1391 it = &parents->item->object;
1392 it->flags |= flags;
1393 add_rev_cmdline(revs, it, arg_, REV_CMD_PARENTS_ONLY, flags);
1394 add_pending_object(revs, it, arg);
1396 return 1;
1399 void init_revisions(struct rev_info *revs, const char *prefix)
1401 memset(revs, 0, sizeof(*revs));
1403 revs->abbrev = DEFAULT_ABBREV;
1404 revs->ignore_merges = 1;
1405 revs->simplify_history = 1;
1406 DIFF_OPT_SET(&revs->pruning, RECURSIVE);
1407 DIFF_OPT_SET(&revs->pruning, QUICK);
1408 revs->pruning.add_remove = file_add_remove;
1409 revs->pruning.change = file_change;
1410 revs->sort_order = REV_SORT_IN_GRAPH_ORDER;
1411 revs->dense = 1;
1412 revs->prefix = prefix;
1413 revs->max_age = -1;
1414 revs->min_age = -1;
1415 revs->skip_count = -1;
1416 revs->max_count = -1;
1417 revs->max_parents = -1;
1418 revs->expand_tabs_in_log = -1;
1420 revs->commit_format = CMIT_FMT_DEFAULT;
1421 revs->expand_tabs_in_log_default = 8;
1423 init_grep_defaults();
1424 grep_init(&revs->grep_filter, prefix);
1425 revs->grep_filter.status_only = 1;
1427 diff_setup(&revs->diffopt);
1428 if (prefix && !revs->diffopt.prefix) {
1429 revs->diffopt.prefix = prefix;
1430 revs->diffopt.prefix_length = strlen(prefix);
1433 revs->notes_opt.use_default_notes = -1;
1436 static void add_pending_commit_list(struct rev_info *revs,
1437 struct commit_list *commit_list,
1438 unsigned int flags)
1440 while (commit_list) {
1441 struct object *object = &commit_list->item->object;
1442 object->flags |= flags;
1443 add_pending_object(revs, object, oid_to_hex(&object->oid));
1444 commit_list = commit_list->next;
1448 static void prepare_show_merge(struct rev_info *revs)
1450 struct commit_list *bases;
1451 struct commit *head, *other;
1452 struct object_id oid;
1453 const char **prune = NULL;
1454 int i, prune_num = 1; /* counting terminating NULL */
1456 if (get_oid("HEAD", &oid))
1457 die("--merge without HEAD?");
1458 head = lookup_commit_or_die(&oid, "HEAD");
1459 if (get_oid("MERGE_HEAD", &oid))
1460 die("--merge without MERGE_HEAD?");
1461 other = lookup_commit_or_die(&oid, "MERGE_HEAD");
1462 add_pending_object(revs, &head->object, "HEAD");
1463 add_pending_object(revs, &other->object, "MERGE_HEAD");
1464 bases = get_merge_bases(head, other);
1465 add_rev_cmdline_list(revs, bases, REV_CMD_MERGE_BASE, UNINTERESTING | BOTTOM);
1466 add_pending_commit_list(revs, bases, UNINTERESTING | BOTTOM);
1467 free_commit_list(bases);
1468 head->object.flags |= SYMMETRIC_LEFT;
1470 if (!active_nr)
1471 read_cache();
1472 for (i = 0; i < active_nr; i++) {
1473 const struct cache_entry *ce = active_cache[i];
1474 if (!ce_stage(ce))
1475 continue;
1476 if (ce_path_match(ce, &revs->prune_data, NULL)) {
1477 prune_num++;
1478 REALLOC_ARRAY(prune, prune_num);
1479 prune[prune_num-2] = ce->name;
1480 prune[prune_num-1] = NULL;
1482 while ((i+1 < active_nr) &&
1483 ce_same_name(ce, active_cache[i+1]))
1484 i++;
1486 clear_pathspec(&revs->prune_data);
1487 parse_pathspec(&revs->prune_data, PATHSPEC_ALL_MAGIC & ~PATHSPEC_LITERAL,
1488 PATHSPEC_PREFER_FULL | PATHSPEC_LITERAL_PATH, "", prune);
1489 revs->limited = 1;
1492 static int dotdot_missing(const char *arg, char *dotdot,
1493 struct rev_info *revs, int symmetric)
1495 if (revs->ignore_missing)
1496 return 0;
1497 /* de-munge so we report the full argument */
1498 *dotdot = '.';
1499 die(symmetric
1500 ? "Invalid symmetric difference expression %s"
1501 : "Invalid revision range %s", arg);
1504 static int handle_dotdot_1(const char *arg, char *dotdot,
1505 struct rev_info *revs, int flags,
1506 int cant_be_filename,
1507 struct object_context *a_oc,
1508 struct object_context *b_oc)
1510 const char *a_name, *b_name;
1511 struct object_id a_oid, b_oid;
1512 struct object *a_obj, *b_obj;
1513 unsigned int a_flags, b_flags;
1514 int symmetric = 0;
1515 unsigned int flags_exclude = flags ^ (UNINTERESTING | BOTTOM);
1516 unsigned int oc_flags = GET_OID_COMMITTISH | GET_OID_RECORD_PATH;
1518 a_name = arg;
1519 if (!*a_name)
1520 a_name = "HEAD";
1522 b_name = dotdot + 2;
1523 if (*b_name == '.') {
1524 symmetric = 1;
1525 b_name++;
1527 if (!*b_name)
1528 b_name = "HEAD";
1530 if (get_oid_with_context(a_name, oc_flags, &a_oid, a_oc) ||
1531 get_oid_with_context(b_name, oc_flags, &b_oid, b_oc))
1532 return -1;
1534 if (!cant_be_filename) {
1535 *dotdot = '.';
1536 verify_non_filename(revs->prefix, arg);
1537 *dotdot = '\0';
1540 a_obj = parse_object(&a_oid);
1541 b_obj = parse_object(&b_oid);
1542 if (!a_obj || !b_obj)
1543 return dotdot_missing(arg, dotdot, revs, symmetric);
1545 if (!symmetric) {
1546 /* just A..B */
1547 b_flags = flags;
1548 a_flags = flags_exclude;
1549 } else {
1550 /* A...B -- find merge bases between the two */
1551 struct commit *a, *b;
1552 struct commit_list *exclude;
1554 a = lookup_commit_reference(&a_obj->oid);
1555 b = lookup_commit_reference(&b_obj->oid);
1556 if (!a || !b)
1557 return dotdot_missing(arg, dotdot, revs, symmetric);
1559 exclude = get_merge_bases(a, b);
1560 add_rev_cmdline_list(revs, exclude, REV_CMD_MERGE_BASE,
1561 flags_exclude);
1562 add_pending_commit_list(revs, exclude, flags_exclude);
1563 free_commit_list(exclude);
1565 b_flags = flags;
1566 a_flags = flags | SYMMETRIC_LEFT;
1569 a_obj->flags |= a_flags;
1570 b_obj->flags |= b_flags;
1571 add_rev_cmdline(revs, a_obj, a_name, REV_CMD_LEFT, a_flags);
1572 add_rev_cmdline(revs, b_obj, b_name, REV_CMD_RIGHT, b_flags);
1573 add_pending_object_with_path(revs, a_obj, a_name, a_oc->mode, a_oc->path);
1574 add_pending_object_with_path(revs, b_obj, b_name, b_oc->mode, b_oc->path);
1575 return 0;
1578 static int handle_dotdot(const char *arg,
1579 struct rev_info *revs, int flags,
1580 int cant_be_filename)
1582 struct object_context a_oc, b_oc;
1583 char *dotdot = strstr(arg, "..");
1584 int ret;
1586 if (!dotdot)
1587 return -1;
1589 memset(&a_oc, 0, sizeof(a_oc));
1590 memset(&b_oc, 0, sizeof(b_oc));
1592 *dotdot = '\0';
1593 ret = handle_dotdot_1(arg, dotdot, revs, flags, cant_be_filename,
1594 &a_oc, &b_oc);
1595 *dotdot = '.';
1597 free(a_oc.path);
1598 free(b_oc.path);
1600 return ret;
1603 int handle_revision_arg(const char *arg_, struct rev_info *revs, int flags, unsigned revarg_opt)
1605 struct object_context oc;
1606 char *mark;
1607 struct object *object;
1608 struct object_id oid;
1609 int local_flags;
1610 const char *arg = arg_;
1611 int cant_be_filename = revarg_opt & REVARG_CANNOT_BE_FILENAME;
1612 unsigned get_sha1_flags = GET_OID_RECORD_PATH;
1614 flags = flags & UNINTERESTING ? flags | BOTTOM : flags & ~BOTTOM;
1616 if (!cant_be_filename && !strcmp(arg, "..")) {
1618 * Just ".."? That is not a range but the
1619 * pathspec for the parent directory.
1621 return -1;
1624 if (!handle_dotdot(arg, revs, flags, revarg_opt))
1625 return 0;
1627 mark = strstr(arg, "^@");
1628 if (mark && !mark[2]) {
1629 *mark = 0;
1630 if (add_parents_only(revs, arg, flags, 0))
1631 return 0;
1632 *mark = '^';
1634 mark = strstr(arg, "^!");
1635 if (mark && !mark[2]) {
1636 *mark = 0;
1637 if (!add_parents_only(revs, arg, flags ^ (UNINTERESTING | BOTTOM), 0))
1638 *mark = '^';
1640 mark = strstr(arg, "^-");
1641 if (mark) {
1642 int exclude_parent = 1;
1644 if (mark[2]) {
1645 char *end;
1646 exclude_parent = strtoul(mark + 2, &end, 10);
1647 if (*end != '\0' || !exclude_parent)
1648 return -1;
1651 *mark = 0;
1652 if (!add_parents_only(revs, arg, flags ^ (UNINTERESTING | BOTTOM), exclude_parent))
1653 *mark = '^';
1656 local_flags = 0;
1657 if (*arg == '^') {
1658 local_flags = UNINTERESTING | BOTTOM;
1659 arg++;
1662 if (revarg_opt & REVARG_COMMITTISH)
1663 get_sha1_flags |= GET_OID_COMMITTISH;
1665 if (get_oid_with_context(arg, get_sha1_flags, &oid, &oc))
1666 return revs->ignore_missing ? 0 : -1;
1667 if (!cant_be_filename)
1668 verify_non_filename(revs->prefix, arg);
1669 object = get_reference(revs, arg, &oid, flags ^ local_flags);
1670 add_rev_cmdline(revs, object, arg_, REV_CMD_REV, flags ^ local_flags);
1671 add_pending_object_with_path(revs, object, arg, oc.mode, oc.path);
1672 free(oc.path);
1673 return 0;
1676 static void read_pathspec_from_stdin(struct rev_info *revs, struct strbuf *sb,
1677 struct argv_array *prune)
1679 while (strbuf_getline(sb, stdin) != EOF)
1680 argv_array_push(prune, sb->buf);
1683 static void read_revisions_from_stdin(struct rev_info *revs,
1684 struct argv_array *prune)
1686 struct strbuf sb;
1687 int seen_dashdash = 0;
1688 int save_warning;
1690 save_warning = warn_on_object_refname_ambiguity;
1691 warn_on_object_refname_ambiguity = 0;
1693 strbuf_init(&sb, 1000);
1694 while (strbuf_getline(&sb, stdin) != EOF) {
1695 int len = sb.len;
1696 if (!len)
1697 break;
1698 if (sb.buf[0] == '-') {
1699 if (len == 2 && sb.buf[1] == '-') {
1700 seen_dashdash = 1;
1701 break;
1703 die("options not supported in --stdin mode");
1705 if (handle_revision_arg(sb.buf, revs, 0,
1706 REVARG_CANNOT_BE_FILENAME))
1707 die("bad revision '%s'", sb.buf);
1709 if (seen_dashdash)
1710 read_pathspec_from_stdin(revs, &sb, prune);
1712 strbuf_release(&sb);
1713 warn_on_object_refname_ambiguity = save_warning;
1716 static void add_grep(struct rev_info *revs, const char *ptn, enum grep_pat_token what)
1718 append_grep_pattern(&revs->grep_filter, ptn, "command line", 0, what);
1721 static void add_header_grep(struct rev_info *revs, enum grep_header_field field, const char *pattern)
1723 append_header_grep_pattern(&revs->grep_filter, field, pattern);
1726 static void add_message_grep(struct rev_info *revs, const char *pattern)
1728 add_grep(revs, pattern, GREP_PATTERN_BODY);
1731 static int handle_revision_opt(struct rev_info *revs, int argc, const char **argv,
1732 int *unkc, const char **unkv)
1734 const char *arg = argv[0];
1735 const char *optarg;
1736 int argcount;
1738 /* pseudo revision arguments */
1739 if (!strcmp(arg, "--all") || !strcmp(arg, "--branches") ||
1740 !strcmp(arg, "--tags") || !strcmp(arg, "--remotes") ||
1741 !strcmp(arg, "--reflog") || !strcmp(arg, "--not") ||
1742 !strcmp(arg, "--no-walk") || !strcmp(arg, "--do-walk") ||
1743 !strcmp(arg, "--bisect") || starts_with(arg, "--glob=") ||
1744 !strcmp(arg, "--indexed-objects") ||
1745 starts_with(arg, "--exclude=") ||
1746 starts_with(arg, "--branches=") || starts_with(arg, "--tags=") ||
1747 starts_with(arg, "--remotes=") || starts_with(arg, "--no-walk="))
1749 unkv[(*unkc)++] = arg;
1750 return 1;
1753 if ((argcount = parse_long_opt("max-count", argv, &optarg))) {
1754 revs->max_count = atoi(optarg);
1755 revs->no_walk = 0;
1756 return argcount;
1757 } else if ((argcount = parse_long_opt("skip", argv, &optarg))) {
1758 revs->skip_count = atoi(optarg);
1759 return argcount;
1760 } else if ((*arg == '-') && isdigit(arg[1])) {
1761 /* accept -<digit>, like traditional "head" */
1762 if (strtol_i(arg + 1, 10, &revs->max_count) < 0 ||
1763 revs->max_count < 0)
1764 die("'%s': not a non-negative integer", arg + 1);
1765 revs->no_walk = 0;
1766 } else if (!strcmp(arg, "-n")) {
1767 if (argc <= 1)
1768 return error("-n requires an argument");
1769 revs->max_count = atoi(argv[1]);
1770 revs->no_walk = 0;
1771 return 2;
1772 } else if (skip_prefix(arg, "-n", &optarg)) {
1773 revs->max_count = atoi(optarg);
1774 revs->no_walk = 0;
1775 } else if ((argcount = parse_long_opt("max-age", argv, &optarg))) {
1776 revs->max_age = atoi(optarg);
1777 return argcount;
1778 } else if ((argcount = parse_long_opt("since", argv, &optarg))) {
1779 revs->max_age = approxidate(optarg);
1780 return argcount;
1781 } else if ((argcount = parse_long_opt("after", argv, &optarg))) {
1782 revs->max_age = approxidate(optarg);
1783 return argcount;
1784 } else if ((argcount = parse_long_opt("min-age", argv, &optarg))) {
1785 revs->min_age = atoi(optarg);
1786 return argcount;
1787 } else if ((argcount = parse_long_opt("before", argv, &optarg))) {
1788 revs->min_age = approxidate(optarg);
1789 return argcount;
1790 } else if ((argcount = parse_long_opt("until", argv, &optarg))) {
1791 revs->min_age = approxidate(optarg);
1792 return argcount;
1793 } else if (!strcmp(arg, "--first-parent")) {
1794 revs->first_parent_only = 1;
1795 } else if (!strcmp(arg, "--ancestry-path")) {
1796 revs->ancestry_path = 1;
1797 revs->simplify_history = 0;
1798 revs->limited = 1;
1799 } else if (!strcmp(arg, "-g") || !strcmp(arg, "--walk-reflogs")) {
1800 init_reflog_walk(&revs->reflog_info);
1801 } else if (!strcmp(arg, "--default")) {
1802 if (argc <= 1)
1803 return error("bad --default argument");
1804 revs->def = argv[1];
1805 return 2;
1806 } else if (!strcmp(arg, "--merge")) {
1807 revs->show_merge = 1;
1808 } else if (!strcmp(arg, "--topo-order")) {
1809 revs->sort_order = REV_SORT_IN_GRAPH_ORDER;
1810 revs->topo_order = 1;
1811 } else if (!strcmp(arg, "--simplify-merges")) {
1812 revs->simplify_merges = 1;
1813 revs->topo_order = 1;
1814 revs->rewrite_parents = 1;
1815 revs->simplify_history = 0;
1816 revs->limited = 1;
1817 } else if (!strcmp(arg, "--simplify-by-decoration")) {
1818 revs->simplify_merges = 1;
1819 revs->topo_order = 1;
1820 revs->rewrite_parents = 1;
1821 revs->simplify_history = 0;
1822 revs->simplify_by_decoration = 1;
1823 revs->limited = 1;
1824 revs->prune = 1;
1825 load_ref_decorations(DECORATE_SHORT_REFS);
1826 } else if (!strcmp(arg, "--date-order")) {
1827 revs->sort_order = REV_SORT_BY_COMMIT_DATE;
1828 revs->topo_order = 1;
1829 } else if (!strcmp(arg, "--author-date-order")) {
1830 revs->sort_order = REV_SORT_BY_AUTHOR_DATE;
1831 revs->topo_order = 1;
1832 } else if (!strcmp(arg, "--early-output")) {
1833 revs->early_output = 100;
1834 revs->topo_order = 1;
1835 } else if (skip_prefix(arg, "--early-output=", &optarg)) {
1836 if (strtoul_ui(optarg, 10, &revs->early_output) < 0)
1837 die("'%s': not a non-negative integer", optarg);
1838 revs->topo_order = 1;
1839 } else if (!strcmp(arg, "--parents")) {
1840 revs->rewrite_parents = 1;
1841 revs->print_parents = 1;
1842 } else if (!strcmp(arg, "--dense")) {
1843 revs->dense = 1;
1844 } else if (!strcmp(arg, "--sparse")) {
1845 revs->dense = 0;
1846 } else if (!strcmp(arg, "--show-all")) {
1847 revs->show_all = 1;
1848 } else if (!strcmp(arg, "--remove-empty")) {
1849 revs->remove_empty_trees = 1;
1850 } else if (!strcmp(arg, "--merges")) {
1851 revs->min_parents = 2;
1852 } else if (!strcmp(arg, "--no-merges")) {
1853 revs->max_parents = 1;
1854 } else if (skip_prefix(arg, "--min-parents=", &optarg)) {
1855 revs->min_parents = atoi(optarg);
1856 } else if (!strcmp(arg, "--no-min-parents")) {
1857 revs->min_parents = 0;
1858 } else if (skip_prefix(arg, "--max-parents=", &optarg)) {
1859 revs->max_parents = atoi(optarg);
1860 } else if (!strcmp(arg, "--no-max-parents")) {
1861 revs->max_parents = -1;
1862 } else if (!strcmp(arg, "--boundary")) {
1863 revs->boundary = 1;
1864 } else if (!strcmp(arg, "--left-right")) {
1865 revs->left_right = 1;
1866 } else if (!strcmp(arg, "--left-only")) {
1867 if (revs->right_only)
1868 die("--left-only is incompatible with --right-only"
1869 " or --cherry");
1870 revs->left_only = 1;
1871 } else if (!strcmp(arg, "--right-only")) {
1872 if (revs->left_only)
1873 die("--right-only is incompatible with --left-only");
1874 revs->right_only = 1;
1875 } else if (!strcmp(arg, "--cherry")) {
1876 if (revs->left_only)
1877 die("--cherry is incompatible with --left-only");
1878 revs->cherry_mark = 1;
1879 revs->right_only = 1;
1880 revs->max_parents = 1;
1881 revs->limited = 1;
1882 } else if (!strcmp(arg, "--count")) {
1883 revs->count = 1;
1884 } else if (!strcmp(arg, "--cherry-mark")) {
1885 if (revs->cherry_pick)
1886 die("--cherry-mark is incompatible with --cherry-pick");
1887 revs->cherry_mark = 1;
1888 revs->limited = 1; /* needs limit_list() */
1889 } else if (!strcmp(arg, "--cherry-pick")) {
1890 if (revs->cherry_mark)
1891 die("--cherry-pick is incompatible with --cherry-mark");
1892 revs->cherry_pick = 1;
1893 revs->limited = 1;
1894 } else if (!strcmp(arg, "--objects")) {
1895 revs->tag_objects = 1;
1896 revs->tree_objects = 1;
1897 revs->blob_objects = 1;
1898 } else if (!strcmp(arg, "--objects-edge")) {
1899 revs->tag_objects = 1;
1900 revs->tree_objects = 1;
1901 revs->blob_objects = 1;
1902 revs->edge_hint = 1;
1903 } else if (!strcmp(arg, "--objects-edge-aggressive")) {
1904 revs->tag_objects = 1;
1905 revs->tree_objects = 1;
1906 revs->blob_objects = 1;
1907 revs->edge_hint = 1;
1908 revs->edge_hint_aggressive = 1;
1909 } else if (!strcmp(arg, "--verify-objects")) {
1910 revs->tag_objects = 1;
1911 revs->tree_objects = 1;
1912 revs->blob_objects = 1;
1913 revs->verify_objects = 1;
1914 } else if (!strcmp(arg, "--unpacked")) {
1915 revs->unpacked = 1;
1916 } else if (starts_with(arg, "--unpacked=")) {
1917 die("--unpacked=<packfile> no longer supported.");
1918 } else if (!strcmp(arg, "-r")) {
1919 revs->diff = 1;
1920 DIFF_OPT_SET(&revs->diffopt, RECURSIVE);
1921 } else if (!strcmp(arg, "-t")) {
1922 revs->diff = 1;
1923 DIFF_OPT_SET(&revs->diffopt, RECURSIVE);
1924 DIFF_OPT_SET(&revs->diffopt, TREE_IN_RECURSIVE);
1925 } else if (!strcmp(arg, "-m")) {
1926 revs->ignore_merges = 0;
1927 } else if (!strcmp(arg, "-c")) {
1928 revs->diff = 1;
1929 revs->dense_combined_merges = 0;
1930 revs->combine_merges = 1;
1931 } else if (!strcmp(arg, "--cc")) {
1932 revs->diff = 1;
1933 revs->dense_combined_merges = 1;
1934 revs->combine_merges = 1;
1935 } else if (!strcmp(arg, "-v")) {
1936 revs->verbose_header = 1;
1937 } else if (!strcmp(arg, "--pretty")) {
1938 revs->verbose_header = 1;
1939 revs->pretty_given = 1;
1940 get_commit_format(NULL, revs);
1941 } else if (skip_prefix(arg, "--pretty=", &optarg) ||
1942 skip_prefix(arg, "--format=", &optarg)) {
1944 * Detached form ("--pretty X" as opposed to "--pretty=X")
1945 * not allowed, since the argument is optional.
1947 revs->verbose_header = 1;
1948 revs->pretty_given = 1;
1949 get_commit_format(optarg, revs);
1950 } else if (!strcmp(arg, "--expand-tabs")) {
1951 revs->expand_tabs_in_log = 8;
1952 } else if (!strcmp(arg, "--no-expand-tabs")) {
1953 revs->expand_tabs_in_log = 0;
1954 } else if (skip_prefix(arg, "--expand-tabs=", &arg)) {
1955 int val;
1956 if (strtol_i(arg, 10, &val) < 0 || val < 0)
1957 die("'%s': not a non-negative integer", arg);
1958 revs->expand_tabs_in_log = val;
1959 } else if (!strcmp(arg, "--show-notes") || !strcmp(arg, "--notes")) {
1960 revs->show_notes = 1;
1961 revs->show_notes_given = 1;
1962 revs->notes_opt.use_default_notes = 1;
1963 } else if (!strcmp(arg, "--show-signature")) {
1964 revs->show_signature = 1;
1965 } else if (!strcmp(arg, "--no-show-signature")) {
1966 revs->show_signature = 0;
1967 } else if (!strcmp(arg, "--show-linear-break")) {
1968 revs->break_bar = " ..........";
1969 revs->track_linear = 1;
1970 revs->track_first_time = 1;
1971 } else if (skip_prefix(arg, "--show-linear-break=", &optarg)) {
1972 revs->break_bar = xstrdup(optarg);
1973 revs->track_linear = 1;
1974 revs->track_first_time = 1;
1975 } else if (skip_prefix(arg, "--show-notes=", &optarg) ||
1976 skip_prefix(arg, "--notes=", &optarg)) {
1977 struct strbuf buf = STRBUF_INIT;
1978 revs->show_notes = 1;
1979 revs->show_notes_given = 1;
1980 if (starts_with(arg, "--show-notes=") &&
1981 revs->notes_opt.use_default_notes < 0)
1982 revs->notes_opt.use_default_notes = 1;
1983 strbuf_addstr(&buf, optarg);
1984 expand_notes_ref(&buf);
1985 string_list_append(&revs->notes_opt.extra_notes_refs,
1986 strbuf_detach(&buf, NULL));
1987 } else if (!strcmp(arg, "--no-notes")) {
1988 revs->show_notes = 0;
1989 revs->show_notes_given = 1;
1990 revs->notes_opt.use_default_notes = -1;
1991 /* we have been strdup'ing ourselves, so trick
1992 * string_list into free()ing strings */
1993 revs->notes_opt.extra_notes_refs.strdup_strings = 1;
1994 string_list_clear(&revs->notes_opt.extra_notes_refs, 0);
1995 revs->notes_opt.extra_notes_refs.strdup_strings = 0;
1996 } else if (!strcmp(arg, "--standard-notes")) {
1997 revs->show_notes_given = 1;
1998 revs->notes_opt.use_default_notes = 1;
1999 } else if (!strcmp(arg, "--no-standard-notes")) {
2000 revs->notes_opt.use_default_notes = 0;
2001 } else if (!strcmp(arg, "--oneline")) {
2002 revs->verbose_header = 1;
2003 get_commit_format("oneline", revs);
2004 revs->pretty_given = 1;
2005 revs->abbrev_commit = 1;
2006 } else if (!strcmp(arg, "--graph")) {
2007 revs->topo_order = 1;
2008 revs->rewrite_parents = 1;
2009 revs->graph = graph_init(revs);
2010 } else if (!strcmp(arg, "--root")) {
2011 revs->show_root_diff = 1;
2012 } else if (!strcmp(arg, "--no-commit-id")) {
2013 revs->no_commit_id = 1;
2014 } else if (!strcmp(arg, "--always")) {
2015 revs->always_show_header = 1;
2016 } else if (!strcmp(arg, "--no-abbrev")) {
2017 revs->abbrev = 0;
2018 } else if (!strcmp(arg, "--abbrev")) {
2019 revs->abbrev = DEFAULT_ABBREV;
2020 } else if (skip_prefix(arg, "--abbrev=", &optarg)) {
2021 revs->abbrev = strtoul(optarg, NULL, 10);
2022 if (revs->abbrev < MINIMUM_ABBREV)
2023 revs->abbrev = MINIMUM_ABBREV;
2024 else if (revs->abbrev > 40)
2025 revs->abbrev = 40;
2026 } else if (!strcmp(arg, "--abbrev-commit")) {
2027 revs->abbrev_commit = 1;
2028 revs->abbrev_commit_given = 1;
2029 } else if (!strcmp(arg, "--no-abbrev-commit")) {
2030 revs->abbrev_commit = 0;
2031 } else if (!strcmp(arg, "--full-diff")) {
2032 revs->diff = 1;
2033 revs->full_diff = 1;
2034 } else if (!strcmp(arg, "--full-history")) {
2035 revs->simplify_history = 0;
2036 } else if (!strcmp(arg, "--relative-date")) {
2037 revs->date_mode.type = DATE_RELATIVE;
2038 revs->date_mode_explicit = 1;
2039 } else if ((argcount = parse_long_opt("date", argv, &optarg))) {
2040 parse_date_format(optarg, &revs->date_mode);
2041 revs->date_mode_explicit = 1;
2042 return argcount;
2043 } else if (!strcmp(arg, "--log-size")) {
2044 revs->show_log_size = 1;
2047 * Grepping the commit log
2049 else if ((argcount = parse_long_opt("author", argv, &optarg))) {
2050 add_header_grep(revs, GREP_HEADER_AUTHOR, optarg);
2051 return argcount;
2052 } else if ((argcount = parse_long_opt("committer", argv, &optarg))) {
2053 add_header_grep(revs, GREP_HEADER_COMMITTER, optarg);
2054 return argcount;
2055 } else if ((argcount = parse_long_opt("grep-reflog", argv, &optarg))) {
2056 add_header_grep(revs, GREP_HEADER_REFLOG, optarg);
2057 return argcount;
2058 } else if ((argcount = parse_long_opt("grep", argv, &optarg))) {
2059 add_message_grep(revs, optarg);
2060 return argcount;
2061 } else if (!strcmp(arg, "--grep-debug")) {
2062 revs->grep_filter.debug = 1;
2063 } else if (!strcmp(arg, "--basic-regexp")) {
2064 revs->grep_filter.pattern_type_option = GREP_PATTERN_TYPE_BRE;
2065 } else if (!strcmp(arg, "--extended-regexp") || !strcmp(arg, "-E")) {
2066 revs->grep_filter.pattern_type_option = GREP_PATTERN_TYPE_ERE;
2067 } else if (!strcmp(arg, "--regexp-ignore-case") || !strcmp(arg, "-i")) {
2068 revs->grep_filter.ignore_case = 1;
2069 DIFF_OPT_SET(&revs->diffopt, PICKAXE_IGNORE_CASE);
2070 } else if (!strcmp(arg, "--fixed-strings") || !strcmp(arg, "-F")) {
2071 revs->grep_filter.pattern_type_option = GREP_PATTERN_TYPE_FIXED;
2072 } else if (!strcmp(arg, "--perl-regexp") || !strcmp(arg, "-P")) {
2073 revs->grep_filter.pattern_type_option = GREP_PATTERN_TYPE_PCRE;
2074 } else if (!strcmp(arg, "--all-match")) {
2075 revs->grep_filter.all_match = 1;
2076 } else if (!strcmp(arg, "--invert-grep")) {
2077 revs->invert_grep = 1;
2078 } else if ((argcount = parse_long_opt("encoding", argv, &optarg))) {
2079 if (strcmp(optarg, "none"))
2080 git_log_output_encoding = xstrdup(optarg);
2081 else
2082 git_log_output_encoding = "";
2083 return argcount;
2084 } else if (!strcmp(arg, "--reverse")) {
2085 revs->reverse ^= 1;
2086 } else if (!strcmp(arg, "--children")) {
2087 revs->children.name = "children";
2088 revs->limited = 1;
2089 } else if (!strcmp(arg, "--ignore-missing")) {
2090 revs->ignore_missing = 1;
2091 } else {
2092 int opts = diff_opt_parse(&revs->diffopt, argv, argc, revs->prefix);
2093 if (!opts)
2094 unkv[(*unkc)++] = arg;
2095 return opts;
2097 if (revs->graph && revs->track_linear)
2098 die("--show-linear-break and --graph are incompatible");
2100 return 1;
2103 void parse_revision_opt(struct rev_info *revs, struct parse_opt_ctx_t *ctx,
2104 const struct option *options,
2105 const char * const usagestr[])
2107 int n = handle_revision_opt(revs, ctx->argc, ctx->argv,
2108 &ctx->cpidx, ctx->out);
2109 if (n <= 0) {
2110 error("unknown option `%s'", ctx->argv[0]);
2111 usage_with_options(usagestr, options);
2113 ctx->argv += n;
2114 ctx->argc -= n;
2117 static int for_each_bisect_ref(struct ref_store *refs, each_ref_fn fn,
2118 void *cb_data, const char *term)
2120 struct strbuf bisect_refs = STRBUF_INIT;
2121 int status;
2122 strbuf_addf(&bisect_refs, "refs/bisect/%s", term);
2123 status = refs_for_each_fullref_in(refs, bisect_refs.buf, fn, cb_data, 0);
2124 strbuf_release(&bisect_refs);
2125 return status;
2128 static int for_each_bad_bisect_ref(struct ref_store *refs, each_ref_fn fn, void *cb_data)
2130 return for_each_bisect_ref(refs, fn, cb_data, term_bad);
2133 static int for_each_good_bisect_ref(struct ref_store *refs, each_ref_fn fn, void *cb_data)
2135 return for_each_bisect_ref(refs, fn, cb_data, term_good);
2138 static int handle_revision_pseudo_opt(const char *submodule,
2139 struct rev_info *revs,
2140 int argc, const char **argv, int *flags)
2142 const char *arg = argv[0];
2143 const char *optarg;
2144 struct ref_store *refs;
2145 int argcount;
2147 if (submodule) {
2149 * We need some something like get_submodule_worktrees()
2150 * before we can go through all worktrees of a submodule,
2151 * .e.g with adding all HEADs from --all, which is not
2152 * supported right now, so stick to single worktree.
2154 if (!revs->single_worktree)
2155 die("BUG: --single-worktree cannot be used together with submodule");
2156 refs = get_submodule_ref_store(submodule);
2157 } else
2158 refs = get_main_ref_store();
2161 * NOTE!
2163 * Commands like "git shortlog" will not accept the options below
2164 * unless parse_revision_opt queues them (as opposed to erroring
2165 * out).
2167 * When implementing your new pseudo-option, remember to
2168 * register it in the list at the top of handle_revision_opt.
2170 if (!strcmp(arg, "--all")) {
2171 handle_refs(refs, revs, *flags, refs_for_each_ref);
2172 handle_refs(refs, revs, *flags, refs_head_ref);
2173 if (!revs->single_worktree) {
2174 struct all_refs_cb cb;
2176 init_all_refs_cb(&cb, revs, *flags);
2177 other_head_refs(handle_one_ref, &cb);
2179 clear_ref_exclusion(&revs->ref_excludes);
2180 } else if (!strcmp(arg, "--branches")) {
2181 handle_refs(refs, revs, *flags, refs_for_each_branch_ref);
2182 clear_ref_exclusion(&revs->ref_excludes);
2183 } else if (!strcmp(arg, "--bisect")) {
2184 read_bisect_terms(&term_bad, &term_good);
2185 handle_refs(refs, revs, *flags, for_each_bad_bisect_ref);
2186 handle_refs(refs, revs, *flags ^ (UNINTERESTING | BOTTOM),
2187 for_each_good_bisect_ref);
2188 revs->bisect = 1;
2189 } else if (!strcmp(arg, "--tags")) {
2190 handle_refs(refs, revs, *flags, refs_for_each_tag_ref);
2191 clear_ref_exclusion(&revs->ref_excludes);
2192 } else if (!strcmp(arg, "--remotes")) {
2193 handle_refs(refs, revs, *flags, refs_for_each_remote_ref);
2194 clear_ref_exclusion(&revs->ref_excludes);
2195 } else if ((argcount = parse_long_opt("glob", argv, &optarg))) {
2196 struct all_refs_cb cb;
2197 init_all_refs_cb(&cb, revs, *flags);
2198 for_each_glob_ref(handle_one_ref, optarg, &cb);
2199 clear_ref_exclusion(&revs->ref_excludes);
2200 return argcount;
2201 } else if ((argcount = parse_long_opt("exclude", argv, &optarg))) {
2202 add_ref_exclusion(&revs->ref_excludes, optarg);
2203 return argcount;
2204 } else if (skip_prefix(arg, "--branches=", &optarg)) {
2205 struct all_refs_cb cb;
2206 init_all_refs_cb(&cb, revs, *flags);
2207 for_each_glob_ref_in(handle_one_ref, optarg, "refs/heads/", &cb);
2208 clear_ref_exclusion(&revs->ref_excludes);
2209 } else if (skip_prefix(arg, "--tags=", &optarg)) {
2210 struct all_refs_cb cb;
2211 init_all_refs_cb(&cb, revs, *flags);
2212 for_each_glob_ref_in(handle_one_ref, optarg, "refs/tags/", &cb);
2213 clear_ref_exclusion(&revs->ref_excludes);
2214 } else if (skip_prefix(arg, "--remotes=", &optarg)) {
2215 struct all_refs_cb cb;
2216 init_all_refs_cb(&cb, revs, *flags);
2217 for_each_glob_ref_in(handle_one_ref, optarg, "refs/remotes/", &cb);
2218 clear_ref_exclusion(&revs->ref_excludes);
2219 } else if (!strcmp(arg, "--reflog")) {
2220 add_reflogs_to_pending(revs, *flags);
2221 } else if (!strcmp(arg, "--indexed-objects")) {
2222 add_index_objects_to_pending(revs, *flags);
2223 } else if (!strcmp(arg, "--not")) {
2224 *flags ^= UNINTERESTING | BOTTOM;
2225 } else if (!strcmp(arg, "--no-walk")) {
2226 revs->no_walk = REVISION_WALK_NO_WALK_SORTED;
2227 } else if (skip_prefix(arg, "--no-walk=", &optarg)) {
2229 * Detached form ("--no-walk X" as opposed to "--no-walk=X")
2230 * not allowed, since the argument is optional.
2232 if (!strcmp(optarg, "sorted"))
2233 revs->no_walk = REVISION_WALK_NO_WALK_SORTED;
2234 else if (!strcmp(optarg, "unsorted"))
2235 revs->no_walk = REVISION_WALK_NO_WALK_UNSORTED;
2236 else
2237 return error("invalid argument to --no-walk");
2238 } else if (!strcmp(arg, "--do-walk")) {
2239 revs->no_walk = 0;
2240 } else if (!strcmp(arg, "--single-worktree")) {
2241 revs->single_worktree = 1;
2242 } else {
2243 return 0;
2246 return 1;
2249 static void NORETURN diagnose_missing_default(const char *def)
2251 int flags;
2252 const char *refname;
2254 refname = resolve_ref_unsafe(def, 0, NULL, &flags);
2255 if (!refname || !(flags & REF_ISSYMREF) || (flags & REF_ISBROKEN))
2256 die(_("your current branch appears to be broken"));
2258 skip_prefix(refname, "refs/heads/", &refname);
2259 die(_("your current branch '%s' does not have any commits yet"),
2260 refname);
2264 * Parse revision information, filling in the "rev_info" structure,
2265 * and removing the used arguments from the argument list.
2267 * Returns the number of arguments left that weren't recognized
2268 * (which are also moved to the head of the argument list)
2270 int setup_revisions(int argc, const char **argv, struct rev_info *revs, struct setup_revision_opt *opt)
2272 int i, flags, left, seen_dashdash, read_from_stdin, got_rev_arg = 0, revarg_opt;
2273 struct argv_array prune_data = ARGV_ARRAY_INIT;
2274 const char *submodule = NULL;
2276 if (opt)
2277 submodule = opt->submodule;
2279 /* First, search for "--" */
2280 if (opt && opt->assume_dashdash) {
2281 seen_dashdash = 1;
2282 } else {
2283 seen_dashdash = 0;
2284 for (i = 1; i < argc; i++) {
2285 const char *arg = argv[i];
2286 if (strcmp(arg, "--"))
2287 continue;
2288 argv[i] = NULL;
2289 argc = i;
2290 if (argv[i + 1])
2291 argv_array_pushv(&prune_data, argv + i + 1);
2292 seen_dashdash = 1;
2293 break;
2297 /* Second, deal with arguments and options */
2298 flags = 0;
2299 revarg_opt = opt ? opt->revarg_opt : 0;
2300 if (seen_dashdash)
2301 revarg_opt |= REVARG_CANNOT_BE_FILENAME;
2302 read_from_stdin = 0;
2303 for (left = i = 1; i < argc; i++) {
2304 const char *arg = argv[i];
2305 if (*arg == '-') {
2306 int opts;
2308 opts = handle_revision_pseudo_opt(submodule,
2309 revs, argc - i, argv + i,
2310 &flags);
2311 if (opts > 0) {
2312 i += opts - 1;
2313 continue;
2316 if (!strcmp(arg, "--stdin")) {
2317 if (revs->disable_stdin) {
2318 argv[left++] = arg;
2319 continue;
2321 if (read_from_stdin++)
2322 die("--stdin given twice?");
2323 read_revisions_from_stdin(revs, &prune_data);
2324 continue;
2327 opts = handle_revision_opt(revs, argc - i, argv + i, &left, argv);
2328 if (opts > 0) {
2329 i += opts - 1;
2330 continue;
2332 if (opts < 0)
2333 exit(128);
2334 continue;
2338 if (handle_revision_arg(arg, revs, flags, revarg_opt)) {
2339 int j;
2340 if (seen_dashdash || *arg == '^')
2341 die("bad revision '%s'", arg);
2343 /* If we didn't have a "--":
2344 * (1) all filenames must exist;
2345 * (2) all rev-args must not be interpretable
2346 * as a valid filename.
2347 * but the latter we have checked in the main loop.
2349 for (j = i; j < argc; j++)
2350 verify_filename(revs->prefix, argv[j], j == i);
2352 argv_array_pushv(&prune_data, argv + i);
2353 break;
2355 else
2356 got_rev_arg = 1;
2359 if (prune_data.argc) {
2361 * If we need to introduce the magic "a lone ':' means no
2362 * pathspec whatsoever", here is the place to do so.
2364 * if (prune_data.nr == 1 && !strcmp(prune_data[0], ":")) {
2365 * prune_data.nr = 0;
2366 * prune_data.alloc = 0;
2367 * free(prune_data.path);
2368 * prune_data.path = NULL;
2369 * } else {
2370 * terminate prune_data.alloc with NULL and
2371 * call init_pathspec() to set revs->prune_data here.
2374 parse_pathspec(&revs->prune_data, 0, 0,
2375 revs->prefix, prune_data.argv);
2377 argv_array_clear(&prune_data);
2379 if (revs->def == NULL)
2380 revs->def = opt ? opt->def : NULL;
2381 if (opt && opt->tweak)
2382 opt->tweak(revs, opt);
2383 if (revs->show_merge)
2384 prepare_show_merge(revs);
2385 if (revs->def && !revs->pending.nr && !revs->rev_input_given && !got_rev_arg) {
2386 struct object_id oid;
2387 struct object *object;
2388 struct object_context oc;
2389 if (get_oid_with_context(revs->def, 0, &oid, &oc))
2390 diagnose_missing_default(revs->def);
2391 object = get_reference(revs, revs->def, &oid, 0);
2392 add_pending_object_with_mode(revs, object, revs->def, oc.mode);
2395 /* Did the user ask for any diff output? Run the diff! */
2396 if (revs->diffopt.output_format & ~DIFF_FORMAT_NO_OUTPUT)
2397 revs->diff = 1;
2399 /* Pickaxe, diff-filter and rename following need diffs */
2400 if (revs->diffopt.pickaxe ||
2401 revs->diffopt.filter ||
2402 DIFF_OPT_TST(&revs->diffopt, FOLLOW_RENAMES))
2403 revs->diff = 1;
2405 if (revs->topo_order)
2406 revs->limited = 1;
2408 if (revs->prune_data.nr) {
2409 copy_pathspec(&revs->pruning.pathspec, &revs->prune_data);
2410 /* Can't prune commits with rename following: the paths change.. */
2411 if (!DIFF_OPT_TST(&revs->diffopt, FOLLOW_RENAMES))
2412 revs->prune = 1;
2413 if (!revs->full_diff)
2414 copy_pathspec(&revs->diffopt.pathspec,
2415 &revs->prune_data);
2417 if (revs->combine_merges)
2418 revs->ignore_merges = 0;
2419 revs->diffopt.abbrev = revs->abbrev;
2421 if (revs->line_level_traverse) {
2422 revs->limited = 1;
2423 revs->topo_order = 1;
2426 diff_setup_done(&revs->diffopt);
2428 grep_commit_pattern_type(GREP_PATTERN_TYPE_UNSPECIFIED,
2429 &revs->grep_filter);
2430 compile_grep_patterns(&revs->grep_filter);
2432 if (revs->reverse && revs->reflog_info)
2433 die("cannot combine --reverse with --walk-reflogs");
2434 if (revs->reflog_info && revs->limited)
2435 die("cannot combine --walk-reflogs with history-limiting options");
2436 if (revs->rewrite_parents && revs->children.name)
2437 die("cannot combine --parents and --children");
2440 * Limitations on the graph functionality
2442 if (revs->reverse && revs->graph)
2443 die("cannot combine --reverse with --graph");
2445 if (revs->reflog_info && revs->graph)
2446 die("cannot combine --walk-reflogs with --graph");
2447 if (revs->no_walk && revs->graph)
2448 die("cannot combine --no-walk with --graph");
2449 if (!revs->reflog_info && revs->grep_filter.use_reflog_filter)
2450 die("cannot use --grep-reflog without --walk-reflogs");
2452 if (revs->first_parent_only && revs->bisect)
2453 die(_("--first-parent is incompatible with --bisect"));
2455 if (revs->expand_tabs_in_log < 0)
2456 revs->expand_tabs_in_log = revs->expand_tabs_in_log_default;
2458 return left;
2461 static void add_child(struct rev_info *revs, struct commit *parent, struct commit *child)
2463 struct commit_list *l = xcalloc(1, sizeof(*l));
2465 l->item = child;
2466 l->next = add_decoration(&revs->children, &parent->object, l);
2469 static int remove_duplicate_parents(struct rev_info *revs, struct commit *commit)
2471 struct treesame_state *ts = lookup_decoration(&revs->treesame, &commit->object);
2472 struct commit_list **pp, *p;
2473 int surviving_parents;
2475 /* Examine existing parents while marking ones we have seen... */
2476 pp = &commit->parents;
2477 surviving_parents = 0;
2478 while ((p = *pp) != NULL) {
2479 struct commit *parent = p->item;
2480 if (parent->object.flags & TMP_MARK) {
2481 *pp = p->next;
2482 if (ts)
2483 compact_treesame(revs, commit, surviving_parents);
2484 continue;
2486 parent->object.flags |= TMP_MARK;
2487 surviving_parents++;
2488 pp = &p->next;
2490 /* clear the temporary mark */
2491 for (p = commit->parents; p; p = p->next) {
2492 p->item->object.flags &= ~TMP_MARK;
2494 /* no update_treesame() - removing duplicates can't affect TREESAME */
2495 return surviving_parents;
2498 struct merge_simplify_state {
2499 struct commit *simplified;
2502 static struct merge_simplify_state *locate_simplify_state(struct rev_info *revs, struct commit *commit)
2504 struct merge_simplify_state *st;
2506 st = lookup_decoration(&revs->merge_simplification, &commit->object);
2507 if (!st) {
2508 st = xcalloc(1, sizeof(*st));
2509 add_decoration(&revs->merge_simplification, &commit->object, st);
2511 return st;
2514 static int mark_redundant_parents(struct rev_info *revs, struct commit *commit)
2516 struct commit_list *h = reduce_heads(commit->parents);
2517 int i = 0, marked = 0;
2518 struct commit_list *po, *pn;
2520 /* Want these for sanity-checking only */
2521 int orig_cnt = commit_list_count(commit->parents);
2522 int cnt = commit_list_count(h);
2525 * Not ready to remove items yet, just mark them for now, based
2526 * on the output of reduce_heads(). reduce_heads outputs the reduced
2527 * set in its original order, so this isn't too hard.
2529 po = commit->parents;
2530 pn = h;
2531 while (po) {
2532 if (pn && po->item == pn->item) {
2533 pn = pn->next;
2534 i++;
2535 } else {
2536 po->item->object.flags |= TMP_MARK;
2537 marked++;
2539 po=po->next;
2542 if (i != cnt || cnt+marked != orig_cnt)
2543 die("mark_redundant_parents %d %d %d %d", orig_cnt, cnt, i, marked);
2545 free_commit_list(h);
2547 return marked;
2550 static int mark_treesame_root_parents(struct rev_info *revs, struct commit *commit)
2552 struct commit_list *p;
2553 int marked = 0;
2555 for (p = commit->parents; p; p = p->next) {
2556 struct commit *parent = p->item;
2557 if (!parent->parents && (parent->object.flags & TREESAME)) {
2558 parent->object.flags |= TMP_MARK;
2559 marked++;
2563 return marked;
2567 * Awkward naming - this means one parent we are TREESAME to.
2568 * cf mark_treesame_root_parents: root parents that are TREESAME (to an
2569 * empty tree). Better name suggestions?
2571 static int leave_one_treesame_to_parent(struct rev_info *revs, struct commit *commit)
2573 struct treesame_state *ts = lookup_decoration(&revs->treesame, &commit->object);
2574 struct commit *unmarked = NULL, *marked = NULL;
2575 struct commit_list *p;
2576 unsigned n;
2578 for (p = commit->parents, n = 0; p; p = p->next, n++) {
2579 if (ts->treesame[n]) {
2580 if (p->item->object.flags & TMP_MARK) {
2581 if (!marked)
2582 marked = p->item;
2583 } else {
2584 if (!unmarked) {
2585 unmarked = p->item;
2586 break;
2593 * If we are TREESAME to a marked-for-deletion parent, but not to any
2594 * unmarked parents, unmark the first TREESAME parent. This is the
2595 * parent that the default simplify_history==1 scan would have followed,
2596 * and it doesn't make sense to omit that path when asking for a
2597 * simplified full history. Retaining it improves the chances of
2598 * understanding odd missed merges that took an old version of a file.
2600 * Example:
2602 * I--------*X A modified the file, but mainline merge X used
2603 * \ / "-s ours", so took the version from I. X is
2604 * `-*A--' TREESAME to I and !TREESAME to A.
2606 * Default log from X would produce "I". Without this check,
2607 * --full-history --simplify-merges would produce "I-A-X", showing
2608 * the merge commit X and that it changed A, but not making clear that
2609 * it had just taken the I version. With this check, the topology above
2610 * is retained.
2612 * Note that it is possible that the simplification chooses a different
2613 * TREESAME parent from the default, in which case this test doesn't
2614 * activate, and we _do_ drop the default parent. Example:
2616 * I------X A modified the file, but it was reverted in B,
2617 * \ / meaning mainline merge X is TREESAME to both
2618 * *A-*B parents.
2620 * Default log would produce "I" by following the first parent;
2621 * --full-history --simplify-merges will produce "I-A-B". But this is a
2622 * reasonable result - it presents a logical full history leading from
2623 * I to X, and X is not an important merge.
2625 if (!unmarked && marked) {
2626 marked->object.flags &= ~TMP_MARK;
2627 return 1;
2630 return 0;
2633 static int remove_marked_parents(struct rev_info *revs, struct commit *commit)
2635 struct commit_list **pp, *p;
2636 int nth_parent, removed = 0;
2638 pp = &commit->parents;
2639 nth_parent = 0;
2640 while ((p = *pp) != NULL) {
2641 struct commit *parent = p->item;
2642 if (parent->object.flags & TMP_MARK) {
2643 parent->object.flags &= ~TMP_MARK;
2644 *pp = p->next;
2645 free(p);
2646 removed++;
2647 compact_treesame(revs, commit, nth_parent);
2648 continue;
2650 pp = &p->next;
2651 nth_parent++;
2654 /* Removing parents can only increase TREESAMEness */
2655 if (removed && !(commit->object.flags & TREESAME))
2656 update_treesame(revs, commit);
2658 return nth_parent;
2661 static struct commit_list **simplify_one(struct rev_info *revs, struct commit *commit, struct commit_list **tail)
2663 struct commit_list *p;
2664 struct commit *parent;
2665 struct merge_simplify_state *st, *pst;
2666 int cnt;
2668 st = locate_simplify_state(revs, commit);
2671 * Have we handled this one?
2673 if (st->simplified)
2674 return tail;
2677 * An UNINTERESTING commit simplifies to itself, so does a
2678 * root commit. We do not rewrite parents of such commit
2679 * anyway.
2681 if ((commit->object.flags & UNINTERESTING) || !commit->parents) {
2682 st->simplified = commit;
2683 return tail;
2687 * Do we know what commit all of our parents that matter
2688 * should be rewritten to? Otherwise we are not ready to
2689 * rewrite this one yet.
2691 for (cnt = 0, p = commit->parents; p; p = p->next) {
2692 pst = locate_simplify_state(revs, p->item);
2693 if (!pst->simplified) {
2694 tail = &commit_list_insert(p->item, tail)->next;
2695 cnt++;
2697 if (revs->first_parent_only)
2698 break;
2700 if (cnt) {
2701 tail = &commit_list_insert(commit, tail)->next;
2702 return tail;
2706 * Rewrite our list of parents. Note that this cannot
2707 * affect our TREESAME flags in any way - a commit is
2708 * always TREESAME to its simplification.
2710 for (p = commit->parents; p; p = p->next) {
2711 pst = locate_simplify_state(revs, p->item);
2712 p->item = pst->simplified;
2713 if (revs->first_parent_only)
2714 break;
2717 if (revs->first_parent_only)
2718 cnt = 1;
2719 else
2720 cnt = remove_duplicate_parents(revs, commit);
2723 * It is possible that we are a merge and one side branch
2724 * does not have any commit that touches the given paths;
2725 * in such a case, the immediate parent from that branch
2726 * will be rewritten to be the merge base.
2728 * o----X X: the commit we are looking at;
2729 * / / o: a commit that touches the paths;
2730 * ---o----'
2732 * Further, a merge of an independent branch that doesn't
2733 * touch the path will reduce to a treesame root parent:
2735 * ----o----X X: the commit we are looking at;
2736 * / o: a commit that touches the paths;
2737 * r r: a root commit not touching the paths
2739 * Detect and simplify both cases.
2741 if (1 < cnt) {
2742 int marked = mark_redundant_parents(revs, commit);
2743 marked += mark_treesame_root_parents(revs, commit);
2744 if (marked)
2745 marked -= leave_one_treesame_to_parent(revs, commit);
2746 if (marked)
2747 cnt = remove_marked_parents(revs, commit);
2751 * A commit simplifies to itself if it is a root, if it is
2752 * UNINTERESTING, if it touches the given paths, or if it is a
2753 * merge and its parents don't simplify to one relevant commit
2754 * (the first two cases are already handled at the beginning of
2755 * this function).
2757 * Otherwise, it simplifies to what its sole relevant parent
2758 * simplifies to.
2760 if (!cnt ||
2761 (commit->object.flags & UNINTERESTING) ||
2762 !(commit->object.flags & TREESAME) ||
2763 (parent = one_relevant_parent(revs, commit->parents)) == NULL)
2764 st->simplified = commit;
2765 else {
2766 pst = locate_simplify_state(revs, parent);
2767 st->simplified = pst->simplified;
2769 return tail;
2772 static void simplify_merges(struct rev_info *revs)
2774 struct commit_list *list, *next;
2775 struct commit_list *yet_to_do, **tail;
2776 struct commit *commit;
2778 if (!revs->prune)
2779 return;
2781 /* feed the list reversed */
2782 yet_to_do = NULL;
2783 for (list = revs->commits; list; list = next) {
2784 commit = list->item;
2785 next = list->next;
2787 * Do not free(list) here yet; the original list
2788 * is used later in this function.
2790 commit_list_insert(commit, &yet_to_do);
2792 while (yet_to_do) {
2793 list = yet_to_do;
2794 yet_to_do = NULL;
2795 tail = &yet_to_do;
2796 while (list) {
2797 commit = pop_commit(&list);
2798 tail = simplify_one(revs, commit, tail);
2802 /* clean up the result, removing the simplified ones */
2803 list = revs->commits;
2804 revs->commits = NULL;
2805 tail = &revs->commits;
2806 while (list) {
2807 struct merge_simplify_state *st;
2809 commit = pop_commit(&list);
2810 st = locate_simplify_state(revs, commit);
2811 if (st->simplified == commit)
2812 tail = &commit_list_insert(commit, tail)->next;
2816 static void set_children(struct rev_info *revs)
2818 struct commit_list *l;
2819 for (l = revs->commits; l; l = l->next) {
2820 struct commit *commit = l->item;
2821 struct commit_list *p;
2823 for (p = commit->parents; p; p = p->next)
2824 add_child(revs, p->item, commit);
2828 void reset_revision_walk(void)
2830 clear_object_flags(SEEN | ADDED | SHOWN);
2833 int prepare_revision_walk(struct rev_info *revs)
2835 int i;
2836 struct object_array old_pending;
2837 struct commit_list **next = &revs->commits;
2839 memcpy(&old_pending, &revs->pending, sizeof(old_pending));
2840 revs->pending.nr = 0;
2841 revs->pending.alloc = 0;
2842 revs->pending.objects = NULL;
2843 for (i = 0; i < old_pending.nr; i++) {
2844 struct object_array_entry *e = old_pending.objects + i;
2845 struct commit *commit = handle_commit(revs, e);
2846 if (commit) {
2847 if (!(commit->object.flags & SEEN)) {
2848 commit->object.flags |= SEEN;
2849 next = commit_list_append(commit, next);
2853 if (!revs->leak_pending)
2854 object_array_clear(&old_pending);
2856 /* Signal whether we need per-parent treesame decoration */
2857 if (revs->simplify_merges ||
2858 (revs->limited && limiting_can_increase_treesame(revs)))
2859 revs->treesame.name = "treesame";
2861 if (revs->no_walk != REVISION_WALK_NO_WALK_UNSORTED)
2862 commit_list_sort_by_date(&revs->commits);
2863 if (revs->no_walk)
2864 return 0;
2865 if (revs->limited)
2866 if (limit_list(revs) < 0)
2867 return -1;
2868 if (revs->topo_order)
2869 sort_in_topological_order(&revs->commits, revs->sort_order);
2870 if (revs->line_level_traverse)
2871 line_log_filter(revs);
2872 if (revs->simplify_merges)
2873 simplify_merges(revs);
2874 if (revs->children.name)
2875 set_children(revs);
2876 return 0;
2879 static enum rewrite_result rewrite_one(struct rev_info *revs, struct commit **pp)
2881 struct commit_list *cache = NULL;
2883 for (;;) {
2884 struct commit *p = *pp;
2885 if (!revs->limited)
2886 if (add_parents_to_list(revs, p, &revs->commits, &cache) < 0)
2887 return rewrite_one_error;
2888 if (p->object.flags & UNINTERESTING)
2889 return rewrite_one_ok;
2890 if (!(p->object.flags & TREESAME))
2891 return rewrite_one_ok;
2892 if (!p->parents)
2893 return rewrite_one_noparents;
2894 if ((p = one_relevant_parent(revs, p->parents)) == NULL)
2895 return rewrite_one_ok;
2896 *pp = p;
2900 int rewrite_parents(struct rev_info *revs, struct commit *commit,
2901 rewrite_parent_fn_t rewrite_parent)
2903 struct commit_list **pp = &commit->parents;
2904 while (*pp) {
2905 struct commit_list *parent = *pp;
2906 switch (rewrite_parent(revs, &parent->item)) {
2907 case rewrite_one_ok:
2908 break;
2909 case rewrite_one_noparents:
2910 *pp = parent->next;
2911 continue;
2912 case rewrite_one_error:
2913 return -1;
2915 pp = &parent->next;
2917 remove_duplicate_parents(revs, commit);
2918 return 0;
2921 static int commit_rewrite_person(struct strbuf *buf, const char *what, struct string_list *mailmap)
2923 char *person, *endp;
2924 size_t len, namelen, maillen;
2925 const char *name;
2926 const char *mail;
2927 struct ident_split ident;
2929 person = strstr(buf->buf, what);
2930 if (!person)
2931 return 0;
2933 person += strlen(what);
2934 endp = strchr(person, '\n');
2935 if (!endp)
2936 return 0;
2938 len = endp - person;
2940 if (split_ident_line(&ident, person, len))
2941 return 0;
2943 mail = ident.mail_begin;
2944 maillen = ident.mail_end - ident.mail_begin;
2945 name = ident.name_begin;
2946 namelen = ident.name_end - ident.name_begin;
2948 if (map_user(mailmap, &mail, &maillen, &name, &namelen)) {
2949 struct strbuf namemail = STRBUF_INIT;
2951 strbuf_addf(&namemail, "%.*s <%.*s>",
2952 (int)namelen, name, (int)maillen, mail);
2954 strbuf_splice(buf, ident.name_begin - buf->buf,
2955 ident.mail_end - ident.name_begin + 1,
2956 namemail.buf, namemail.len);
2958 strbuf_release(&namemail);
2960 return 1;
2963 return 0;
2966 static int commit_match(struct commit *commit, struct rev_info *opt)
2968 int retval;
2969 const char *encoding;
2970 const char *message;
2971 struct strbuf buf = STRBUF_INIT;
2973 if (!opt->grep_filter.pattern_list && !opt->grep_filter.header_list)
2974 return 1;
2976 /* Prepend "fake" headers as needed */
2977 if (opt->grep_filter.use_reflog_filter) {
2978 strbuf_addstr(&buf, "reflog ");
2979 get_reflog_message(&buf, opt->reflog_info);
2980 strbuf_addch(&buf, '\n');
2984 * We grep in the user's output encoding, under the assumption that it
2985 * is the encoding they are most likely to write their grep pattern
2986 * for. In addition, it means we will match the "notes" encoding below,
2987 * so we will not end up with a buffer that has two different encodings
2988 * in it.
2990 encoding = get_log_output_encoding();
2991 message = logmsg_reencode(commit, NULL, encoding);
2993 /* Copy the commit to temporary if we are using "fake" headers */
2994 if (buf.len)
2995 strbuf_addstr(&buf, message);
2997 if (opt->grep_filter.header_list && opt->mailmap) {
2998 if (!buf.len)
2999 strbuf_addstr(&buf, message);
3001 commit_rewrite_person(&buf, "\nauthor ", opt->mailmap);
3002 commit_rewrite_person(&buf, "\ncommitter ", opt->mailmap);
3005 /* Append "fake" message parts as needed */
3006 if (opt->show_notes) {
3007 if (!buf.len)
3008 strbuf_addstr(&buf, message);
3009 format_display_notes(&commit->object.oid, &buf, encoding, 1);
3013 * Find either in the original commit message, or in the temporary.
3014 * Note that we cast away the constness of "message" here. It is
3015 * const because it may come from the cached commit buffer. That's OK,
3016 * because we know that it is modifiable heap memory, and that while
3017 * grep_buffer may modify it for speed, it will restore any
3018 * changes before returning.
3020 if (buf.len)
3021 retval = grep_buffer(&opt->grep_filter, buf.buf, buf.len);
3022 else
3023 retval = grep_buffer(&opt->grep_filter,
3024 (char *)message, strlen(message));
3025 strbuf_release(&buf);
3026 unuse_commit_buffer(commit, message);
3027 return opt->invert_grep ? !retval : retval;
3030 static inline int want_ancestry(const struct rev_info *revs)
3032 return (revs->rewrite_parents || revs->children.name);
3036 * Return a timestamp to be used for --since/--until comparisons for this
3037 * commit, based on the revision options.
3039 static timestamp_t comparison_date(const struct rev_info *revs,
3040 struct commit *commit)
3042 return revs->reflog_info ?
3043 get_reflog_timestamp(revs->reflog_info) :
3044 commit->date;
3047 enum commit_action get_commit_action(struct rev_info *revs, struct commit *commit)
3049 if (commit->object.flags & SHOWN)
3050 return commit_ignore;
3051 if (revs->unpacked && has_sha1_pack(commit->object.oid.hash))
3052 return commit_ignore;
3053 if (revs->show_all)
3054 return commit_show;
3055 if (commit->object.flags & UNINTERESTING)
3056 return commit_ignore;
3057 if (revs->min_age != -1 &&
3058 comparison_date(revs, commit) > revs->min_age)
3059 return commit_ignore;
3060 if (revs->min_parents || (revs->max_parents >= 0)) {
3061 int n = commit_list_count(commit->parents);
3062 if ((n < revs->min_parents) ||
3063 ((revs->max_parents >= 0) && (n > revs->max_parents)))
3064 return commit_ignore;
3066 if (!commit_match(commit, revs))
3067 return commit_ignore;
3068 if (revs->prune && revs->dense) {
3069 /* Commit without changes? */
3070 if (commit->object.flags & TREESAME) {
3071 int n;
3072 struct commit_list *p;
3073 /* drop merges unless we want parenthood */
3074 if (!want_ancestry(revs))
3075 return commit_ignore;
3077 * If we want ancestry, then need to keep any merges
3078 * between relevant commits to tie together topology.
3079 * For consistency with TREESAME and simplification
3080 * use "relevant" here rather than just INTERESTING,
3081 * to treat bottom commit(s) as part of the topology.
3083 for (n = 0, p = commit->parents; p; p = p->next)
3084 if (relevant_commit(p->item))
3085 if (++n >= 2)
3086 return commit_show;
3087 return commit_ignore;
3090 return commit_show;
3093 define_commit_slab(saved_parents, struct commit_list *);
3095 #define EMPTY_PARENT_LIST ((struct commit_list *)-1)
3098 * You may only call save_parents() once per commit (this is checked
3099 * for non-root commits).
3101 static void save_parents(struct rev_info *revs, struct commit *commit)
3103 struct commit_list **pp;
3105 if (!revs->saved_parents_slab) {
3106 revs->saved_parents_slab = xmalloc(sizeof(struct saved_parents));
3107 init_saved_parents(revs->saved_parents_slab);
3110 pp = saved_parents_at(revs->saved_parents_slab, commit);
3113 * When walking with reflogs, we may visit the same commit
3114 * several times: once for each appearance in the reflog.
3116 * In this case, save_parents() will be called multiple times.
3117 * We want to keep only the first set of parents. We need to
3118 * store a sentinel value for an empty (i.e., NULL) parent
3119 * list to distinguish it from a not-yet-saved list, however.
3121 if (*pp)
3122 return;
3123 if (commit->parents)
3124 *pp = copy_commit_list(commit->parents);
3125 else
3126 *pp = EMPTY_PARENT_LIST;
3129 static void free_saved_parents(struct rev_info *revs)
3131 if (revs->saved_parents_slab)
3132 clear_saved_parents(revs->saved_parents_slab);
3135 struct commit_list *get_saved_parents(struct rev_info *revs, const struct commit *commit)
3137 struct commit_list *parents;
3139 if (!revs->saved_parents_slab)
3140 return commit->parents;
3142 parents = *saved_parents_at(revs->saved_parents_slab, commit);
3143 if (parents == EMPTY_PARENT_LIST)
3144 return NULL;
3145 return parents;
3148 enum commit_action simplify_commit(struct rev_info *revs, struct commit *commit)
3150 enum commit_action action = get_commit_action(revs, commit);
3152 if (action == commit_show &&
3153 !revs->show_all &&
3154 revs->prune && revs->dense && want_ancestry(revs)) {
3156 * --full-diff on simplified parents is no good: it
3157 * will show spurious changes from the commits that
3158 * were elided. So we save the parents on the side
3159 * when --full-diff is in effect.
3161 if (revs->full_diff)
3162 save_parents(revs, commit);
3163 if (rewrite_parents(revs, commit, rewrite_one) < 0)
3164 return commit_error;
3166 return action;
3169 static void track_linear(struct rev_info *revs, struct commit *commit)
3171 if (revs->track_first_time) {
3172 revs->linear = 1;
3173 revs->track_first_time = 0;
3174 } else {
3175 struct commit_list *p;
3176 for (p = revs->previous_parents; p; p = p->next)
3177 if (p->item == NULL || /* first commit */
3178 !oidcmp(&p->item->object.oid, &commit->object.oid))
3179 break;
3180 revs->linear = p != NULL;
3182 if (revs->reverse) {
3183 if (revs->linear)
3184 commit->object.flags |= TRACK_LINEAR;
3186 free_commit_list(revs->previous_parents);
3187 revs->previous_parents = copy_commit_list(commit->parents);
3190 static struct commit *get_revision_1(struct rev_info *revs)
3192 while (1) {
3193 struct commit *commit;
3195 if (revs->reflog_info)
3196 commit = next_reflog_entry(revs->reflog_info);
3197 else
3198 commit = pop_commit(&revs->commits);
3200 if (!commit)
3201 return NULL;
3203 if (revs->reflog_info)
3204 commit->object.flags &= ~(ADDED | SEEN | SHOWN);
3207 * If we haven't done the list limiting, we need to look at
3208 * the parents here. We also need to do the date-based limiting
3209 * that we'd otherwise have done in limit_list().
3211 if (!revs->limited) {
3212 if (revs->max_age != -1 &&
3213 comparison_date(revs, commit) < revs->max_age)
3214 continue;
3216 if (revs->reflog_info)
3217 try_to_simplify_commit(revs, commit);
3218 else if (add_parents_to_list(revs, commit, &revs->commits, NULL) < 0) {
3219 if (!revs->ignore_missing_links)
3220 die("Failed to traverse parents of commit %s",
3221 oid_to_hex(&commit->object.oid));
3225 switch (simplify_commit(revs, commit)) {
3226 case commit_ignore:
3227 continue;
3228 case commit_error:
3229 die("Failed to simplify parents of commit %s",
3230 oid_to_hex(&commit->object.oid));
3231 default:
3232 if (revs->track_linear)
3233 track_linear(revs, commit);
3234 return commit;
3240 * Return true for entries that have not yet been shown. (This is an
3241 * object_array_each_func_t.)
3243 static int entry_unshown(struct object_array_entry *entry, void *cb_data_unused)
3245 return !(entry->item->flags & SHOWN);
3249 * If array is on the verge of a realloc, garbage-collect any entries
3250 * that have already been shown to try to free up some space.
3252 static void gc_boundary(struct object_array *array)
3254 if (array->nr == array->alloc)
3255 object_array_filter(array, entry_unshown, NULL);
3258 static void create_boundary_commit_list(struct rev_info *revs)
3260 unsigned i;
3261 struct commit *c;
3262 struct object_array *array = &revs->boundary_commits;
3263 struct object_array_entry *objects = array->objects;
3266 * If revs->commits is non-NULL at this point, an error occurred in
3267 * get_revision_1(). Ignore the error and continue printing the
3268 * boundary commits anyway. (This is what the code has always
3269 * done.)
3271 if (revs->commits) {
3272 free_commit_list(revs->commits);
3273 revs->commits = NULL;
3277 * Put all of the actual boundary commits from revs->boundary_commits
3278 * into revs->commits
3280 for (i = 0; i < array->nr; i++) {
3281 c = (struct commit *)(objects[i].item);
3282 if (!c)
3283 continue;
3284 if (!(c->object.flags & CHILD_SHOWN))
3285 continue;
3286 if (c->object.flags & (SHOWN | BOUNDARY))
3287 continue;
3288 c->object.flags |= BOUNDARY;
3289 commit_list_insert(c, &revs->commits);
3293 * If revs->topo_order is set, sort the boundary commits
3294 * in topological order
3296 sort_in_topological_order(&revs->commits, revs->sort_order);
3299 static struct commit *get_revision_internal(struct rev_info *revs)
3301 struct commit *c = NULL;
3302 struct commit_list *l;
3304 if (revs->boundary == 2) {
3306 * All of the normal commits have already been returned,
3307 * and we are now returning boundary commits.
3308 * create_boundary_commit_list() has populated
3309 * revs->commits with the remaining commits to return.
3311 c = pop_commit(&revs->commits);
3312 if (c)
3313 c->object.flags |= SHOWN;
3314 return c;
3318 * If our max_count counter has reached zero, then we are done. We
3319 * don't simply return NULL because we still might need to show
3320 * boundary commits. But we want to avoid calling get_revision_1, which
3321 * might do a considerable amount of work finding the next commit only
3322 * for us to throw it away.
3324 * If it is non-zero, then either we don't have a max_count at all
3325 * (-1), or it is still counting, in which case we decrement.
3327 if (revs->max_count) {
3328 c = get_revision_1(revs);
3329 if (c) {
3330 while (revs->skip_count > 0) {
3331 revs->skip_count--;
3332 c = get_revision_1(revs);
3333 if (!c)
3334 break;
3338 if (revs->max_count > 0)
3339 revs->max_count--;
3342 if (c)
3343 c->object.flags |= SHOWN;
3345 if (!revs->boundary)
3346 return c;
3348 if (!c) {
3350 * get_revision_1() runs out the commits, and
3351 * we are done computing the boundaries.
3352 * switch to boundary commits output mode.
3354 revs->boundary = 2;
3357 * Update revs->commits to contain the list of
3358 * boundary commits.
3360 create_boundary_commit_list(revs);
3362 return get_revision_internal(revs);
3366 * boundary commits are the commits that are parents of the
3367 * ones we got from get_revision_1() but they themselves are
3368 * not returned from get_revision_1(). Before returning
3369 * 'c', we need to mark its parents that they could be boundaries.
3372 for (l = c->parents; l; l = l->next) {
3373 struct object *p;
3374 p = &(l->item->object);
3375 if (p->flags & (CHILD_SHOWN | SHOWN))
3376 continue;
3377 p->flags |= CHILD_SHOWN;
3378 gc_boundary(&revs->boundary_commits);
3379 add_object_array(p, NULL, &revs->boundary_commits);
3382 return c;
3385 struct commit *get_revision(struct rev_info *revs)
3387 struct commit *c;
3388 struct commit_list *reversed;
3390 if (revs->reverse) {
3391 reversed = NULL;
3392 while ((c = get_revision_internal(revs)))
3393 commit_list_insert(c, &reversed);
3394 revs->commits = reversed;
3395 revs->reverse = 0;
3396 revs->reverse_output_stage = 1;
3399 if (revs->reverse_output_stage) {
3400 c = pop_commit(&revs->commits);
3401 if (revs->track_linear)
3402 revs->linear = !!(c && c->object.flags & TRACK_LINEAR);
3403 return c;
3406 c = get_revision_internal(revs);
3407 if (c && revs->graph)
3408 graph_update(revs->graph, c);
3409 if (!c) {
3410 free_saved_parents(revs);
3411 if (revs->previous_parents) {
3412 free_commit_list(revs->previous_parents);
3413 revs->previous_parents = NULL;
3416 return c;
3419 char *get_revision_mark(const struct rev_info *revs, const struct commit *commit)
3421 if (commit->object.flags & BOUNDARY)
3422 return "-";
3423 else if (commit->object.flags & UNINTERESTING)
3424 return "^";
3425 else if (commit->object.flags & PATCHSAME)
3426 return "=";
3427 else if (!revs || revs->left_right) {
3428 if (commit->object.flags & SYMMETRIC_LEFT)
3429 return "<";
3430 else
3431 return ">";
3432 } else if (revs->graph)
3433 return "*";
3434 else if (revs->cherry_mark)
3435 return "+";
3436 return "";
3439 void put_revision_mark(const struct rev_info *revs, const struct commit *commit)
3441 char *mark = get_revision_mark(revs, commit);
3442 if (!strlen(mark))
3443 return;
3444 fputs(mark, stdout);
3445 putchar(' ');