use pop_commit() for consuming the first entry of a struct commit_list
[git/debian.git] / revision.c
blob3f65614963e515ed9971cae10b0bc33382cf7186
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"
22 volatile show_early_output_fn_t show_early_output;
24 char *path_name(const struct name_path *path, const char *name)
26 const struct name_path *p;
27 char *n, *m;
28 int nlen = strlen(name);
29 int len = nlen + 1;
31 for (p = path; p; p = p->up) {
32 if (p->elem_len)
33 len += p->elem_len + 1;
35 n = xmalloc(len);
36 m = n + len - (nlen + 1);
37 strcpy(m, name);
38 for (p = path; p; p = p->up) {
39 if (p->elem_len) {
40 m -= p->elem_len + 1;
41 memcpy(m, p->elem, p->elem_len);
42 m[p->elem_len] = '/';
45 return n;
48 static int show_path_component_truncated(FILE *out, const char *name, int len)
50 int cnt;
51 for (cnt = 0; cnt < len; cnt++) {
52 int ch = name[cnt];
53 if (!ch || ch == '\n')
54 return -1;
55 fputc(ch, out);
57 return len;
60 static int show_path_truncated(FILE *out, const struct name_path *path)
62 int emitted, ours;
64 if (!path)
65 return 0;
66 emitted = show_path_truncated(out, path->up);
67 if (emitted < 0)
68 return emitted;
69 if (emitted)
70 fputc('/', out);
71 ours = show_path_component_truncated(out, path->elem, path->elem_len);
72 if (ours < 0)
73 return ours;
74 return ours || emitted;
77 void show_object_with_name(FILE *out, struct object *obj,
78 const struct name_path *path, const char *component)
80 struct name_path leaf;
81 leaf.up = (struct name_path *)path;
82 leaf.elem = component;
83 leaf.elem_len = strlen(component);
85 fprintf(out, "%s ", sha1_to_hex(obj->sha1));
86 show_path_truncated(out, &leaf);
87 fputc('\n', out);
90 static void mark_blob_uninteresting(struct blob *blob)
92 if (!blob)
93 return;
94 if (blob->object.flags & UNINTERESTING)
95 return;
96 blob->object.flags |= UNINTERESTING;
99 static void mark_tree_contents_uninteresting(struct tree *tree)
101 struct tree_desc desc;
102 struct name_entry entry;
103 struct object *obj = &tree->object;
105 if (!has_sha1_file(obj->sha1))
106 return;
107 if (parse_tree(tree) < 0)
108 die("bad tree %s", sha1_to_hex(obj->sha1));
110 init_tree_desc(&desc, tree->buffer, tree->size);
111 while (tree_entry(&desc, &entry)) {
112 switch (object_type(entry.mode)) {
113 case OBJ_TREE:
114 mark_tree_uninteresting(lookup_tree(entry.sha1));
115 break;
116 case OBJ_BLOB:
117 mark_blob_uninteresting(lookup_blob(entry.sha1));
118 break;
119 default:
120 /* Subproject commit - not in this repository */
121 break;
126 * We don't care about the tree any more
127 * after it has been marked uninteresting.
129 free_tree_buffer(tree);
132 void mark_tree_uninteresting(struct tree *tree)
134 struct object *obj = &tree->object;
136 if (!tree)
137 return;
138 if (obj->flags & UNINTERESTING)
139 return;
140 obj->flags |= UNINTERESTING;
141 mark_tree_contents_uninteresting(tree);
144 void mark_parents_uninteresting(struct commit *commit)
146 struct commit_list *parents = NULL, *l;
148 for (l = commit->parents; l; l = l->next)
149 commit_list_insert(l->item, &parents);
151 while (parents) {
152 struct commit *commit = pop_commit(&parents);
154 while (commit) {
156 * A missing commit is ok iff its parent is marked
157 * uninteresting.
159 * We just mark such a thing parsed, so that when
160 * it is popped next time around, we won't be trying
161 * to parse it and get an error.
163 if (!has_sha1_file(commit->object.sha1))
164 commit->object.parsed = 1;
166 if (commit->object.flags & UNINTERESTING)
167 break;
169 commit->object.flags |= UNINTERESTING;
172 * Normally we haven't parsed the parent
173 * yet, so we won't have a parent of a parent
174 * here. However, it may turn out that we've
175 * reached this commit some other way (where it
176 * wasn't uninteresting), in which case we need
177 * to mark its parents recursively too..
179 if (!commit->parents)
180 break;
182 for (l = commit->parents->next; l; l = l->next)
183 commit_list_insert(l->item, &parents);
184 commit = commit->parents->item;
189 static void add_pending_object_with_path(struct rev_info *revs,
190 struct object *obj,
191 const char *name, unsigned mode,
192 const char *path)
194 if (!obj)
195 return;
196 if (revs->no_walk && (obj->flags & UNINTERESTING))
197 revs->no_walk = 0;
198 if (revs->reflog_info && obj->type == OBJ_COMMIT) {
199 struct strbuf buf = STRBUF_INIT;
200 int len = interpret_branch_name(name, 0, &buf);
201 int st;
203 if (0 < len && name[len] && buf.len)
204 strbuf_addstr(&buf, name + len);
205 st = add_reflog_for_walk(revs->reflog_info,
206 (struct commit *)obj,
207 buf.buf[0] ? buf.buf: name);
208 strbuf_release(&buf);
209 if (st)
210 return;
212 add_object_array_with_path(obj, name, &revs->pending, mode, path);
215 static void add_pending_object_with_mode(struct rev_info *revs,
216 struct object *obj,
217 const char *name, unsigned mode)
219 add_pending_object_with_path(revs, obj, name, mode, NULL);
222 void add_pending_object(struct rev_info *revs,
223 struct object *obj, const char *name)
225 add_pending_object_with_mode(revs, obj, name, S_IFINVALID);
228 void add_head_to_pending(struct rev_info *revs)
230 unsigned char sha1[20];
231 struct object *obj;
232 if (get_sha1("HEAD", sha1))
233 return;
234 obj = parse_object(sha1);
235 if (!obj)
236 return;
237 add_pending_object(revs, obj, "HEAD");
240 static struct object *get_reference(struct rev_info *revs, const char *name,
241 const unsigned char *sha1,
242 unsigned int flags)
244 struct object *object;
246 object = parse_object(sha1);
247 if (!object) {
248 if (revs->ignore_missing)
249 return object;
250 die("bad object %s", name);
252 object->flags |= flags;
253 return object;
256 void add_pending_sha1(struct rev_info *revs, const char *name,
257 const unsigned char *sha1, unsigned int flags)
259 struct object *object = get_reference(revs, name, sha1, flags);
260 add_pending_object(revs, object, name);
263 static struct commit *handle_commit(struct rev_info *revs,
264 struct object_array_entry *entry)
266 struct object *object = entry->item;
267 const char *name = entry->name;
268 const char *path = entry->path;
269 unsigned int mode = entry->mode;
270 unsigned long flags = object->flags;
273 * Tag object? Look what it points to..
275 while (object->type == OBJ_TAG) {
276 struct tag *tag = (struct tag *) object;
277 if (revs->tag_objects && !(flags & UNINTERESTING))
278 add_pending_object(revs, object, tag->tag);
279 if (!tag->tagged)
280 die("bad tag");
281 object = parse_object(tag->tagged->sha1);
282 if (!object) {
283 if (flags & UNINTERESTING)
284 return NULL;
285 die("bad object %s", sha1_to_hex(tag->tagged->sha1));
287 object->flags |= flags;
289 * We'll handle the tagged object by looping or dropping
290 * through to the non-tag handlers below. Do not
291 * propagate data from the tag's pending entry.
293 name = "";
294 path = NULL;
295 mode = 0;
299 * Commit object? Just return it, we'll do all the complex
300 * reachability crud.
302 if (object->type == OBJ_COMMIT) {
303 struct commit *commit = (struct commit *)object;
304 if (parse_commit(commit) < 0)
305 die("unable to parse commit %s", name);
306 if (flags & UNINTERESTING) {
307 mark_parents_uninteresting(commit);
308 revs->limited = 1;
310 if (revs->show_source && !commit->util)
311 commit->util = xstrdup(name);
312 return commit;
316 * Tree object? Either mark it uninteresting, or add it
317 * to the list of objects to look at later..
319 if (object->type == OBJ_TREE) {
320 struct tree *tree = (struct tree *)object;
321 if (!revs->tree_objects)
322 return NULL;
323 if (flags & UNINTERESTING) {
324 mark_tree_contents_uninteresting(tree);
325 return NULL;
327 add_pending_object_with_path(revs, object, name, mode, path);
328 return NULL;
332 * Blob object? You know the drill by now..
334 if (object->type == OBJ_BLOB) {
335 if (!revs->blob_objects)
336 return NULL;
337 if (flags & UNINTERESTING)
338 return NULL;
339 add_pending_object_with_path(revs, object, name, mode, path);
340 return NULL;
342 die("%s is unknown object", name);
345 static int everybody_uninteresting(struct commit_list *orig,
346 struct commit **interesting_cache)
348 struct commit_list *list = orig;
350 if (*interesting_cache) {
351 struct commit *commit = *interesting_cache;
352 if (!(commit->object.flags & UNINTERESTING))
353 return 0;
356 while (list) {
357 struct commit *commit = list->item;
358 list = list->next;
359 if (commit->object.flags & UNINTERESTING)
360 continue;
362 *interesting_cache = commit;
363 return 0;
365 return 1;
369 * A definition of "relevant" commit that we can use to simplify limited graphs
370 * by eliminating side branches.
372 * A "relevant" commit is one that is !UNINTERESTING (ie we are including it
373 * in our list), or that is a specified BOTTOM commit. Then after computing
374 * a limited list, during processing we can generally ignore boundary merges
375 * coming from outside the graph, (ie from irrelevant parents), and treat
376 * those merges as if they were single-parent. TREESAME is defined to consider
377 * only relevant parents, if any. If we are TREESAME to our on-graph parents,
378 * we don't care if we were !TREESAME to non-graph parents.
380 * Treating bottom commits as relevant ensures that a limited graph's
381 * connection to the actual bottom commit is not viewed as a side branch, but
382 * treated as part of the graph. For example:
384 * ....Z...A---X---o---o---B
385 * . /
386 * W---Y
388 * When computing "A..B", the A-X connection is at least as important as
389 * Y-X, despite A being flagged UNINTERESTING.
391 * And when computing --ancestry-path "A..B", the A-X connection is more
392 * important than Y-X, despite both A and Y being flagged UNINTERESTING.
394 static inline int relevant_commit(struct commit *commit)
396 return (commit->object.flags & (UNINTERESTING | BOTTOM)) != UNINTERESTING;
400 * Return a single relevant commit from a parent list. If we are a TREESAME
401 * commit, and this selects one of our parents, then we can safely simplify to
402 * that parent.
404 static struct commit *one_relevant_parent(const struct rev_info *revs,
405 struct commit_list *orig)
407 struct commit_list *list = orig;
408 struct commit *relevant = NULL;
410 if (!orig)
411 return NULL;
414 * For 1-parent commits, or if first-parent-only, then return that
415 * first parent (even if not "relevant" by the above definition).
416 * TREESAME will have been set purely on that parent.
418 if (revs->first_parent_only || !orig->next)
419 return orig->item;
422 * For multi-parent commits, identify a sole relevant parent, if any.
423 * If we have only one relevant parent, then TREESAME will be set purely
424 * with regard to that parent, and we can simplify accordingly.
426 * If we have more than one relevant parent, or no relevant parents
427 * (and multiple irrelevant ones), then we can't select a parent here
428 * and return NULL.
430 while (list) {
431 struct commit *commit = list->item;
432 list = list->next;
433 if (relevant_commit(commit)) {
434 if (relevant)
435 return NULL;
436 relevant = commit;
439 return relevant;
443 * The goal is to get REV_TREE_NEW as the result only if the
444 * diff consists of all '+' (and no other changes), REV_TREE_OLD
445 * if the whole diff is removal of old data, and otherwise
446 * REV_TREE_DIFFERENT (of course if the trees are the same we
447 * want REV_TREE_SAME).
448 * That means that once we get to REV_TREE_DIFFERENT, we do not
449 * have to look any further.
451 static int tree_difference = REV_TREE_SAME;
453 static void file_add_remove(struct diff_options *options,
454 int addremove, unsigned mode,
455 const unsigned char *sha1,
456 int sha1_valid,
457 const char *fullpath, unsigned dirty_submodule)
459 int diff = addremove == '+' ? REV_TREE_NEW : REV_TREE_OLD;
461 tree_difference |= diff;
462 if (tree_difference == REV_TREE_DIFFERENT)
463 DIFF_OPT_SET(options, HAS_CHANGES);
466 static void file_change(struct diff_options *options,
467 unsigned old_mode, unsigned new_mode,
468 const unsigned char *old_sha1,
469 const unsigned char *new_sha1,
470 int old_sha1_valid, int new_sha1_valid,
471 const char *fullpath,
472 unsigned old_dirty_submodule, unsigned new_dirty_submodule)
474 tree_difference = REV_TREE_DIFFERENT;
475 DIFF_OPT_SET(options, HAS_CHANGES);
478 static int rev_compare_tree(struct rev_info *revs,
479 struct commit *parent, struct commit *commit)
481 struct tree *t1 = parent->tree;
482 struct tree *t2 = commit->tree;
484 if (!t1)
485 return REV_TREE_NEW;
486 if (!t2)
487 return REV_TREE_OLD;
489 if (revs->simplify_by_decoration) {
491 * If we are simplifying by decoration, then the commit
492 * is worth showing if it has a tag pointing at it.
494 if (get_name_decoration(&commit->object))
495 return REV_TREE_DIFFERENT;
497 * A commit that is not pointed by a tag is uninteresting
498 * if we are not limited by path. This means that you will
499 * see the usual "commits that touch the paths" plus any
500 * tagged commit by specifying both --simplify-by-decoration
501 * and pathspec.
503 if (!revs->prune_data.nr)
504 return REV_TREE_SAME;
507 tree_difference = REV_TREE_SAME;
508 DIFF_OPT_CLR(&revs->pruning, HAS_CHANGES);
509 if (diff_tree_sha1(t1->object.sha1, t2->object.sha1, "",
510 &revs->pruning) < 0)
511 return REV_TREE_DIFFERENT;
512 return tree_difference;
515 static int rev_same_tree_as_empty(struct rev_info *revs, struct commit *commit)
517 int retval;
518 struct tree *t1 = commit->tree;
520 if (!t1)
521 return 0;
523 tree_difference = REV_TREE_SAME;
524 DIFF_OPT_CLR(&revs->pruning, HAS_CHANGES);
525 retval = diff_tree_sha1(NULL, t1->object.sha1, "", &revs->pruning);
527 return retval >= 0 && (tree_difference == REV_TREE_SAME);
530 struct treesame_state {
531 unsigned int nparents;
532 unsigned char treesame[FLEX_ARRAY];
535 static struct treesame_state *initialise_treesame(struct rev_info *revs, struct commit *commit)
537 unsigned n = commit_list_count(commit->parents);
538 struct treesame_state *st = xcalloc(1, sizeof(*st) + n);
539 st->nparents = n;
540 add_decoration(&revs->treesame, &commit->object, st);
541 return st;
545 * Must be called immediately after removing the nth_parent from a commit's
546 * parent list, if we are maintaining the per-parent treesame[] decoration.
547 * This does not recalculate the master TREESAME flag - update_treesame()
548 * should be called to update it after a sequence of treesame[] modifications
549 * that may have affected it.
551 static int compact_treesame(struct rev_info *revs, struct commit *commit, unsigned nth_parent)
553 struct treesame_state *st;
554 int old_same;
556 if (!commit->parents) {
558 * Have just removed the only parent from a non-merge.
559 * Different handling, as we lack decoration.
561 if (nth_parent != 0)
562 die("compact_treesame %u", nth_parent);
563 old_same = !!(commit->object.flags & TREESAME);
564 if (rev_same_tree_as_empty(revs, commit))
565 commit->object.flags |= TREESAME;
566 else
567 commit->object.flags &= ~TREESAME;
568 return old_same;
571 st = lookup_decoration(&revs->treesame, &commit->object);
572 if (!st || nth_parent >= st->nparents)
573 die("compact_treesame %u", nth_parent);
575 old_same = st->treesame[nth_parent];
576 memmove(st->treesame + nth_parent,
577 st->treesame + nth_parent + 1,
578 st->nparents - nth_parent - 1);
581 * If we've just become a non-merge commit, update TREESAME
582 * immediately, and remove the no-longer-needed decoration.
583 * If still a merge, defer update until update_treesame().
585 if (--st->nparents == 1) {
586 if (commit->parents->next)
587 die("compact_treesame parents mismatch");
588 if (st->treesame[0] && revs->dense)
589 commit->object.flags |= TREESAME;
590 else
591 commit->object.flags &= ~TREESAME;
592 free(add_decoration(&revs->treesame, &commit->object, NULL));
595 return old_same;
598 static unsigned update_treesame(struct rev_info *revs, struct commit *commit)
600 if (commit->parents && commit->parents->next) {
601 unsigned n;
602 struct treesame_state *st;
603 struct commit_list *p;
604 unsigned relevant_parents;
605 unsigned relevant_change, irrelevant_change;
607 st = lookup_decoration(&revs->treesame, &commit->object);
608 if (!st)
609 die("update_treesame %s", sha1_to_hex(commit->object.sha1));
610 relevant_parents = 0;
611 relevant_change = irrelevant_change = 0;
612 for (p = commit->parents, n = 0; p; n++, p = p->next) {
613 if (relevant_commit(p->item)) {
614 relevant_change |= !st->treesame[n];
615 relevant_parents++;
616 } else
617 irrelevant_change |= !st->treesame[n];
619 if (relevant_parents ? relevant_change : irrelevant_change)
620 commit->object.flags &= ~TREESAME;
621 else
622 commit->object.flags |= TREESAME;
625 return commit->object.flags & TREESAME;
628 static inline int limiting_can_increase_treesame(const struct rev_info *revs)
631 * TREESAME is irrelevant unless prune && dense;
632 * if simplify_history is set, we can't have a mixture of TREESAME and
633 * !TREESAME INTERESTING parents (and we don't have treesame[]
634 * decoration anyway);
635 * if first_parent_only is set, then the TREESAME flag is locked
636 * against the first parent (and again we lack treesame[] decoration).
638 return revs->prune && revs->dense &&
639 !revs->simplify_history &&
640 !revs->first_parent_only;
643 static void try_to_simplify_commit(struct rev_info *revs, struct commit *commit)
645 struct commit_list **pp, *parent;
646 struct treesame_state *ts = NULL;
647 int relevant_change = 0, irrelevant_change = 0;
648 int relevant_parents, nth_parent;
651 * If we don't do pruning, everything is interesting
653 if (!revs->prune)
654 return;
656 if (!commit->tree)
657 return;
659 if (!commit->parents) {
660 if (rev_same_tree_as_empty(revs, commit))
661 commit->object.flags |= TREESAME;
662 return;
666 * Normal non-merge commit? If we don't want to make the
667 * history dense, we consider it always to be a change..
669 if (!revs->dense && !commit->parents->next)
670 return;
672 for (pp = &commit->parents, nth_parent = 0, relevant_parents = 0;
673 (parent = *pp) != NULL;
674 pp = &parent->next, nth_parent++) {
675 struct commit *p = parent->item;
676 if (relevant_commit(p))
677 relevant_parents++;
679 if (nth_parent == 1) {
681 * This our second loop iteration - so we now know
682 * we're dealing with a merge.
684 * Do not compare with later parents when we care only about
685 * the first parent chain, in order to avoid derailing the
686 * traversal to follow a side branch that brought everything
687 * in the path we are limited to by the pathspec.
689 if (revs->first_parent_only)
690 break;
692 * If this will remain a potentially-simplifiable
693 * merge, remember per-parent treesame if needed.
694 * Initialise the array with the comparison from our
695 * first iteration.
697 if (revs->treesame.name &&
698 !revs->simplify_history &&
699 !(commit->object.flags & UNINTERESTING)) {
700 ts = initialise_treesame(revs, commit);
701 if (!(irrelevant_change || relevant_change))
702 ts->treesame[0] = 1;
705 if (parse_commit(p) < 0)
706 die("cannot simplify commit %s (because of %s)",
707 sha1_to_hex(commit->object.sha1),
708 sha1_to_hex(p->object.sha1));
709 switch (rev_compare_tree(revs, p, commit)) {
710 case REV_TREE_SAME:
711 if (!revs->simplify_history || !relevant_commit(p)) {
712 /* Even if a merge with an uninteresting
713 * side branch brought the entire change
714 * we are interested in, we do not want
715 * to lose the other branches of this
716 * merge, so we just keep going.
718 if (ts)
719 ts->treesame[nth_parent] = 1;
720 continue;
722 parent->next = NULL;
723 commit->parents = parent;
724 commit->object.flags |= TREESAME;
725 return;
727 case REV_TREE_NEW:
728 if (revs->remove_empty_trees &&
729 rev_same_tree_as_empty(revs, p)) {
730 /* We are adding all the specified
731 * paths from this parent, so the
732 * history beyond this parent is not
733 * interesting. Remove its parents
734 * (they are grandparents for us).
735 * IOW, we pretend this parent is a
736 * "root" commit.
738 if (parse_commit(p) < 0)
739 die("cannot simplify commit %s (invalid %s)",
740 sha1_to_hex(commit->object.sha1),
741 sha1_to_hex(p->object.sha1));
742 p->parents = NULL;
744 /* fallthrough */
745 case REV_TREE_OLD:
746 case REV_TREE_DIFFERENT:
747 if (relevant_commit(p))
748 relevant_change = 1;
749 else
750 irrelevant_change = 1;
751 continue;
753 die("bad tree compare for commit %s", sha1_to_hex(commit->object.sha1));
757 * TREESAME is straightforward for single-parent commits. For merge
758 * commits, it is most useful to define it so that "irrelevant"
759 * parents cannot make us !TREESAME - if we have any relevant
760 * parents, then we only consider TREESAMEness with respect to them,
761 * allowing irrelevant merges from uninteresting branches to be
762 * simplified away. Only if we have only irrelevant parents do we
763 * base TREESAME on them. Note that this logic is replicated in
764 * update_treesame, which should be kept in sync.
766 if (relevant_parents ? !relevant_change : !irrelevant_change)
767 commit->object.flags |= TREESAME;
770 static void commit_list_insert_by_date_cached(struct commit *p, struct commit_list **head,
771 struct commit_list *cached_base, struct commit_list **cache)
773 struct commit_list *new_entry;
775 if (cached_base && p->date < cached_base->item->date)
776 new_entry = commit_list_insert_by_date(p, &cached_base->next);
777 else
778 new_entry = commit_list_insert_by_date(p, head);
780 if (cache && (!*cache || p->date < (*cache)->item->date))
781 *cache = new_entry;
784 static int add_parents_to_list(struct rev_info *revs, struct commit *commit,
785 struct commit_list **list, struct commit_list **cache_ptr)
787 struct commit_list *parent = commit->parents;
788 unsigned left_flag;
789 struct commit_list *cached_base = cache_ptr ? *cache_ptr : NULL;
791 if (commit->object.flags & ADDED)
792 return 0;
793 commit->object.flags |= ADDED;
795 if (revs->include_check &&
796 !revs->include_check(commit, revs->include_check_data))
797 return 0;
800 * If the commit is uninteresting, don't try to
801 * prune parents - we want the maximal uninteresting
802 * set.
804 * Normally we haven't parsed the parent
805 * yet, so we won't have a parent of a parent
806 * here. However, it may turn out that we've
807 * reached this commit some other way (where it
808 * wasn't uninteresting), in which case we need
809 * to mark its parents recursively too..
811 if (commit->object.flags & UNINTERESTING) {
812 while (parent) {
813 struct commit *p = parent->item;
814 parent = parent->next;
815 if (p)
816 p->object.flags |= UNINTERESTING;
817 if (parse_commit_gently(p, 1) < 0)
818 continue;
819 if (p->parents)
820 mark_parents_uninteresting(p);
821 if (p->object.flags & SEEN)
822 continue;
823 p->object.flags |= SEEN;
824 commit_list_insert_by_date_cached(p, list, cached_base, cache_ptr);
826 return 0;
830 * Ok, the commit wasn't uninteresting. Try to
831 * simplify the commit history and find the parent
832 * that has no differences in the path set if one exists.
834 try_to_simplify_commit(revs, commit);
836 if (revs->no_walk)
837 return 0;
839 left_flag = (commit->object.flags & SYMMETRIC_LEFT);
841 for (parent = commit->parents; parent; parent = parent->next) {
842 struct commit *p = parent->item;
844 if (parse_commit_gently(p, revs->ignore_missing_links) < 0)
845 return -1;
846 if (revs->show_source && !p->util)
847 p->util = commit->util;
848 p->object.flags |= left_flag;
849 if (!(p->object.flags & SEEN)) {
850 p->object.flags |= SEEN;
851 commit_list_insert_by_date_cached(p, list, cached_base, cache_ptr);
853 if (revs->first_parent_only)
854 break;
856 return 0;
859 static void cherry_pick_list(struct commit_list *list, struct rev_info *revs)
861 struct commit_list *p;
862 int left_count = 0, right_count = 0;
863 int left_first;
864 struct patch_ids ids;
865 unsigned cherry_flag;
867 /* First count the commits on the left and on the right */
868 for (p = list; p; p = p->next) {
869 struct commit *commit = p->item;
870 unsigned flags = commit->object.flags;
871 if (flags & BOUNDARY)
873 else if (flags & SYMMETRIC_LEFT)
874 left_count++;
875 else
876 right_count++;
879 if (!left_count || !right_count)
880 return;
882 left_first = left_count < right_count;
883 init_patch_ids(&ids);
884 ids.diffopts.pathspec = revs->diffopt.pathspec;
886 /* Compute patch-ids for one side */
887 for (p = list; p; p = p->next) {
888 struct commit *commit = p->item;
889 unsigned flags = commit->object.flags;
891 if (flags & BOUNDARY)
892 continue;
894 * If we have fewer left, left_first is set and we omit
895 * commits on the right branch in this loop. If we have
896 * fewer right, we skip the left ones.
898 if (left_first != !!(flags & SYMMETRIC_LEFT))
899 continue;
900 commit->util = add_commit_patch_id(commit, &ids);
903 /* either cherry_mark or cherry_pick are true */
904 cherry_flag = revs->cherry_mark ? PATCHSAME : SHOWN;
906 /* Check the other side */
907 for (p = list; p; p = p->next) {
908 struct commit *commit = p->item;
909 struct patch_id *id;
910 unsigned flags = commit->object.flags;
912 if (flags & BOUNDARY)
913 continue;
915 * If we have fewer left, left_first is set and we omit
916 * commits on the left branch in this loop.
918 if (left_first == !!(flags & SYMMETRIC_LEFT))
919 continue;
922 * Have we seen the same patch id?
924 id = has_commit_patch_id(commit, &ids);
925 if (!id)
926 continue;
927 id->seen = 1;
928 commit->object.flags |= cherry_flag;
931 /* Now check the original side for seen ones */
932 for (p = list; p; p = p->next) {
933 struct commit *commit = p->item;
934 struct patch_id *ent;
936 ent = commit->util;
937 if (!ent)
938 continue;
939 if (ent->seen)
940 commit->object.flags |= cherry_flag;
941 commit->util = NULL;
944 free_patch_ids(&ids);
947 /* How many extra uninteresting commits we want to see.. */
948 #define SLOP 5
950 static int still_interesting(struct commit_list *src, unsigned long date, int slop,
951 struct commit **interesting_cache)
954 * No source list at all? We're definitely done..
956 if (!src)
957 return 0;
960 * Does the destination list contain entries with a date
961 * before the source list? Definitely _not_ done.
963 if (date <= src->item->date)
964 return SLOP;
967 * Does the source list still have interesting commits in
968 * it? Definitely not done..
970 if (!everybody_uninteresting(src, interesting_cache))
971 return SLOP;
973 /* Ok, we're closing in.. */
974 return slop-1;
978 * "rev-list --ancestry-path A..B" computes commits that are ancestors
979 * of B but not ancestors of A but further limits the result to those
980 * that are descendants of A. This takes the list of bottom commits and
981 * the result of "A..B" without --ancestry-path, and limits the latter
982 * further to the ones that can reach one of the commits in "bottom".
984 static void limit_to_ancestry(struct commit_list *bottom, struct commit_list *list)
986 struct commit_list *p;
987 struct commit_list *rlist = NULL;
988 int made_progress;
991 * Reverse the list so that it will be likely that we would
992 * process parents before children.
994 for (p = list; p; p = p->next)
995 commit_list_insert(p->item, &rlist);
997 for (p = bottom; p; p = p->next)
998 p->item->object.flags |= TMP_MARK;
1001 * Mark the ones that can reach bottom commits in "list",
1002 * in a bottom-up fashion.
1004 do {
1005 made_progress = 0;
1006 for (p = rlist; p; p = p->next) {
1007 struct commit *c = p->item;
1008 struct commit_list *parents;
1009 if (c->object.flags & (TMP_MARK | UNINTERESTING))
1010 continue;
1011 for (parents = c->parents;
1012 parents;
1013 parents = parents->next) {
1014 if (!(parents->item->object.flags & TMP_MARK))
1015 continue;
1016 c->object.flags |= TMP_MARK;
1017 made_progress = 1;
1018 break;
1021 } while (made_progress);
1024 * NEEDSWORK: decide if we want to remove parents that are
1025 * not marked with TMP_MARK from commit->parents for commits
1026 * in the resulting list. We may not want to do that, though.
1030 * The ones that are not marked with TMP_MARK are uninteresting
1032 for (p = list; p; p = p->next) {
1033 struct commit *c = p->item;
1034 if (c->object.flags & TMP_MARK)
1035 continue;
1036 c->object.flags |= UNINTERESTING;
1039 /* We are done with the TMP_MARK */
1040 for (p = list; p; p = p->next)
1041 p->item->object.flags &= ~TMP_MARK;
1042 for (p = bottom; p; p = p->next)
1043 p->item->object.flags &= ~TMP_MARK;
1044 free_commit_list(rlist);
1048 * Before walking the history, keep the set of "negative" refs the
1049 * caller has asked to exclude.
1051 * This is used to compute "rev-list --ancestry-path A..B", as we need
1052 * to filter the result of "A..B" further to the ones that can actually
1053 * reach A.
1055 static struct commit_list *collect_bottom_commits(struct commit_list *list)
1057 struct commit_list *elem, *bottom = NULL;
1058 for (elem = list; elem; elem = elem->next)
1059 if (elem->item->object.flags & BOTTOM)
1060 commit_list_insert(elem->item, &bottom);
1061 return bottom;
1064 /* Assumes either left_only or right_only is set */
1065 static void limit_left_right(struct commit_list *list, struct rev_info *revs)
1067 struct commit_list *p;
1069 for (p = list; p; p = p->next) {
1070 struct commit *commit = p->item;
1072 if (revs->right_only) {
1073 if (commit->object.flags & SYMMETRIC_LEFT)
1074 commit->object.flags |= SHOWN;
1075 } else /* revs->left_only is set */
1076 if (!(commit->object.flags & SYMMETRIC_LEFT))
1077 commit->object.flags |= SHOWN;
1081 static int limit_list(struct rev_info *revs)
1083 int slop = SLOP;
1084 unsigned long date = ~0ul;
1085 struct commit_list *list = revs->commits;
1086 struct commit_list *newlist = NULL;
1087 struct commit_list **p = &newlist;
1088 struct commit_list *bottom = NULL;
1089 struct commit *interesting_cache = NULL;
1091 if (revs->ancestry_path) {
1092 bottom = collect_bottom_commits(list);
1093 if (!bottom)
1094 die("--ancestry-path given but there are no bottom commits");
1097 while (list) {
1098 struct commit *commit = pop_commit(&list);
1099 struct object *obj = &commit->object;
1100 show_early_output_fn_t show;
1102 if (commit == interesting_cache)
1103 interesting_cache = NULL;
1105 if (revs->max_age != -1 && (commit->date < revs->max_age))
1106 obj->flags |= UNINTERESTING;
1107 if (add_parents_to_list(revs, commit, &list, NULL) < 0)
1108 return -1;
1109 if (obj->flags & UNINTERESTING) {
1110 mark_parents_uninteresting(commit);
1111 if (revs->show_all)
1112 p = &commit_list_insert(commit, p)->next;
1113 slop = still_interesting(list, date, slop, &interesting_cache);
1114 if (slop)
1115 continue;
1116 /* If showing all, add the whole pending list to the end */
1117 if (revs->show_all)
1118 *p = list;
1119 break;
1121 if (revs->min_age != -1 && (commit->date > revs->min_age))
1122 continue;
1123 date = commit->date;
1124 p = &commit_list_insert(commit, p)->next;
1126 show = show_early_output;
1127 if (!show)
1128 continue;
1130 show(revs, newlist);
1131 show_early_output = NULL;
1133 if (revs->cherry_pick || revs->cherry_mark)
1134 cherry_pick_list(newlist, revs);
1136 if (revs->left_only || revs->right_only)
1137 limit_left_right(newlist, revs);
1139 if (bottom) {
1140 limit_to_ancestry(bottom, newlist);
1141 free_commit_list(bottom);
1145 * Check if any commits have become TREESAME by some of their parents
1146 * becoming UNINTERESTING.
1148 if (limiting_can_increase_treesame(revs))
1149 for (list = newlist; list; list = list->next) {
1150 struct commit *c = list->item;
1151 if (c->object.flags & (UNINTERESTING | TREESAME))
1152 continue;
1153 update_treesame(revs, c);
1156 revs->commits = newlist;
1157 return 0;
1161 * Add an entry to refs->cmdline with the specified information.
1162 * *name is copied.
1164 static void add_rev_cmdline(struct rev_info *revs,
1165 struct object *item,
1166 const char *name,
1167 int whence,
1168 unsigned flags)
1170 struct rev_cmdline_info *info = &revs->cmdline;
1171 int nr = info->nr;
1173 ALLOC_GROW(info->rev, nr + 1, info->alloc);
1174 info->rev[nr].item = item;
1175 info->rev[nr].name = xstrdup(name);
1176 info->rev[nr].whence = whence;
1177 info->rev[nr].flags = flags;
1178 info->nr++;
1181 static void add_rev_cmdline_list(struct rev_info *revs,
1182 struct commit_list *commit_list,
1183 int whence,
1184 unsigned flags)
1186 while (commit_list) {
1187 struct object *object = &commit_list->item->object;
1188 add_rev_cmdline(revs, object, sha1_to_hex(object->sha1),
1189 whence, flags);
1190 commit_list = commit_list->next;
1194 struct all_refs_cb {
1195 int all_flags;
1196 int warned_bad_reflog;
1197 struct rev_info *all_revs;
1198 const char *name_for_errormsg;
1201 int ref_excluded(struct string_list *ref_excludes, const char *path)
1203 struct string_list_item *item;
1205 if (!ref_excludes)
1206 return 0;
1207 for_each_string_list_item(item, ref_excludes) {
1208 if (!wildmatch(item->string, path, 0, NULL))
1209 return 1;
1211 return 0;
1214 static int handle_one_ref(const char *path, const struct object_id *oid,
1215 int flag, void *cb_data)
1217 struct all_refs_cb *cb = cb_data;
1218 struct object *object;
1220 if (ref_excluded(cb->all_revs->ref_excludes, path))
1221 return 0;
1223 object = get_reference(cb->all_revs, path, oid->hash, cb->all_flags);
1224 add_rev_cmdline(cb->all_revs, object, path, REV_CMD_REF, cb->all_flags);
1225 add_pending_sha1(cb->all_revs, path, oid->hash, cb->all_flags);
1226 return 0;
1229 static void init_all_refs_cb(struct all_refs_cb *cb, struct rev_info *revs,
1230 unsigned flags)
1232 cb->all_revs = revs;
1233 cb->all_flags = flags;
1236 void clear_ref_exclusion(struct string_list **ref_excludes_p)
1238 if (*ref_excludes_p) {
1239 string_list_clear(*ref_excludes_p, 0);
1240 free(*ref_excludes_p);
1242 *ref_excludes_p = NULL;
1245 void add_ref_exclusion(struct string_list **ref_excludes_p, const char *exclude)
1247 if (!*ref_excludes_p) {
1248 *ref_excludes_p = xcalloc(1, sizeof(**ref_excludes_p));
1249 (*ref_excludes_p)->strdup_strings = 1;
1251 string_list_append(*ref_excludes_p, exclude);
1254 static void handle_refs(const char *submodule, struct rev_info *revs, unsigned flags,
1255 int (*for_each)(const char *, each_ref_fn, void *))
1257 struct all_refs_cb cb;
1258 init_all_refs_cb(&cb, revs, flags);
1259 for_each(submodule, handle_one_ref, &cb);
1262 static void handle_one_reflog_commit(unsigned char *sha1, void *cb_data)
1264 struct all_refs_cb *cb = cb_data;
1265 if (!is_null_sha1(sha1)) {
1266 struct object *o = parse_object(sha1);
1267 if (o) {
1268 o->flags |= cb->all_flags;
1269 /* ??? CMDLINEFLAGS ??? */
1270 add_pending_object(cb->all_revs, o, "");
1272 else if (!cb->warned_bad_reflog) {
1273 warning("reflog of '%s' references pruned commits",
1274 cb->name_for_errormsg);
1275 cb->warned_bad_reflog = 1;
1280 static int handle_one_reflog_ent(unsigned char *osha1, unsigned char *nsha1,
1281 const char *email, unsigned long timestamp, int tz,
1282 const char *message, void *cb_data)
1284 handle_one_reflog_commit(osha1, cb_data);
1285 handle_one_reflog_commit(nsha1, cb_data);
1286 return 0;
1289 static int handle_one_reflog(const char *path, const struct object_id *oid,
1290 int flag, void *cb_data)
1292 struct all_refs_cb *cb = cb_data;
1293 cb->warned_bad_reflog = 0;
1294 cb->name_for_errormsg = path;
1295 for_each_reflog_ent(path, handle_one_reflog_ent, cb_data);
1296 return 0;
1299 void add_reflogs_to_pending(struct rev_info *revs, unsigned flags)
1301 struct all_refs_cb cb;
1303 cb.all_revs = revs;
1304 cb.all_flags = flags;
1305 for_each_reflog(handle_one_reflog, &cb);
1308 static void add_cache_tree(struct cache_tree *it, struct rev_info *revs,
1309 struct strbuf *path)
1311 size_t baselen = path->len;
1312 int i;
1314 if (it->entry_count >= 0) {
1315 struct tree *tree = lookup_tree(it->sha1);
1316 add_pending_object_with_path(revs, &tree->object, "",
1317 040000, path->buf);
1320 for (i = 0; i < it->subtree_nr; i++) {
1321 struct cache_tree_sub *sub = it->down[i];
1322 strbuf_addf(path, "%s%s", baselen ? "/" : "", sub->name);
1323 add_cache_tree(sub->cache_tree, revs, path);
1324 strbuf_setlen(path, baselen);
1329 void add_index_objects_to_pending(struct rev_info *revs, unsigned flags)
1331 int i;
1333 read_cache();
1334 for (i = 0; i < active_nr; i++) {
1335 struct cache_entry *ce = active_cache[i];
1336 struct blob *blob;
1338 if (S_ISGITLINK(ce->ce_mode))
1339 continue;
1341 blob = lookup_blob(ce->sha1);
1342 if (!blob)
1343 die("unable to add index blob to traversal");
1344 add_pending_object_with_path(revs, &blob->object, "",
1345 ce->ce_mode, ce->name);
1348 if (active_cache_tree) {
1349 struct strbuf path = STRBUF_INIT;
1350 add_cache_tree(active_cache_tree, revs, &path);
1351 strbuf_release(&path);
1355 static int add_parents_only(struct rev_info *revs, const char *arg_, int flags)
1357 unsigned char sha1[20];
1358 struct object *it;
1359 struct commit *commit;
1360 struct commit_list *parents;
1361 const char *arg = arg_;
1363 if (*arg == '^') {
1364 flags ^= UNINTERESTING | BOTTOM;
1365 arg++;
1367 if (get_sha1_committish(arg, sha1))
1368 return 0;
1369 while (1) {
1370 it = get_reference(revs, arg, sha1, 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 hashcpy(sha1, ((struct tag*)it)->tagged->sha1);
1379 if (it->type != OBJ_COMMIT)
1380 return 0;
1381 commit = (struct commit *)it;
1382 for (parents = commit->parents; parents; parents = parents->next) {
1383 it = &parents->item->object;
1384 it->flags |= flags;
1385 add_rev_cmdline(revs, it, arg_, REV_CMD_PARENTS_ONLY, flags);
1386 add_pending_object(revs, it, arg);
1388 return 1;
1391 void init_revisions(struct rev_info *revs, const char *prefix)
1393 memset(revs, 0, sizeof(*revs));
1395 revs->abbrev = DEFAULT_ABBREV;
1396 revs->ignore_merges = 1;
1397 revs->simplify_history = 1;
1398 DIFF_OPT_SET(&revs->pruning, RECURSIVE);
1399 DIFF_OPT_SET(&revs->pruning, QUICK);
1400 revs->pruning.add_remove = file_add_remove;
1401 revs->pruning.change = file_change;
1402 revs->sort_order = REV_SORT_IN_GRAPH_ORDER;
1403 revs->dense = 1;
1404 revs->prefix = prefix;
1405 revs->max_age = -1;
1406 revs->min_age = -1;
1407 revs->skip_count = -1;
1408 revs->max_count = -1;
1409 revs->max_parents = -1;
1411 revs->commit_format = CMIT_FMT_DEFAULT;
1413 init_grep_defaults();
1414 grep_init(&revs->grep_filter, prefix);
1415 revs->grep_filter.status_only = 1;
1416 revs->grep_filter.regflags = REG_NEWLINE;
1418 diff_setup(&revs->diffopt);
1419 if (prefix && !revs->diffopt.prefix) {
1420 revs->diffopt.prefix = prefix;
1421 revs->diffopt.prefix_length = strlen(prefix);
1424 revs->notes_opt.use_default_notes = -1;
1427 static void add_pending_commit_list(struct rev_info *revs,
1428 struct commit_list *commit_list,
1429 unsigned int flags)
1431 while (commit_list) {
1432 struct object *object = &commit_list->item->object;
1433 object->flags |= flags;
1434 add_pending_object(revs, object, sha1_to_hex(object->sha1));
1435 commit_list = commit_list->next;
1439 static void prepare_show_merge(struct rev_info *revs)
1441 struct commit_list *bases;
1442 struct commit *head, *other;
1443 unsigned char sha1[20];
1444 const char **prune = NULL;
1445 int i, prune_num = 1; /* counting terminating NULL */
1447 if (get_sha1("HEAD", sha1))
1448 die("--merge without HEAD?");
1449 head = lookup_commit_or_die(sha1, "HEAD");
1450 if (get_sha1("MERGE_HEAD", sha1))
1451 die("--merge without MERGE_HEAD?");
1452 other = lookup_commit_or_die(sha1, "MERGE_HEAD");
1453 add_pending_object(revs, &head->object, "HEAD");
1454 add_pending_object(revs, &other->object, "MERGE_HEAD");
1455 bases = get_merge_bases(head, other);
1456 add_rev_cmdline_list(revs, bases, REV_CMD_MERGE_BASE, UNINTERESTING | BOTTOM);
1457 add_pending_commit_list(revs, bases, UNINTERESTING | BOTTOM);
1458 free_commit_list(bases);
1459 head->object.flags |= SYMMETRIC_LEFT;
1461 if (!active_nr)
1462 read_cache();
1463 for (i = 0; i < active_nr; i++) {
1464 const struct cache_entry *ce = active_cache[i];
1465 if (!ce_stage(ce))
1466 continue;
1467 if (ce_path_match(ce, &revs->prune_data, NULL)) {
1468 prune_num++;
1469 REALLOC_ARRAY(prune, prune_num);
1470 prune[prune_num-2] = ce->name;
1471 prune[prune_num-1] = NULL;
1473 while ((i+1 < active_nr) &&
1474 ce_same_name(ce, active_cache[i+1]))
1475 i++;
1477 free_pathspec(&revs->prune_data);
1478 parse_pathspec(&revs->prune_data, PATHSPEC_ALL_MAGIC & ~PATHSPEC_LITERAL,
1479 PATHSPEC_PREFER_FULL | PATHSPEC_LITERAL_PATH, "", prune);
1480 revs->limited = 1;
1483 int handle_revision_arg(const char *arg_, struct rev_info *revs, int flags, unsigned revarg_opt)
1485 struct object_context oc;
1486 char *dotdot;
1487 struct object *object;
1488 unsigned char sha1[20];
1489 int local_flags;
1490 const char *arg = arg_;
1491 int cant_be_filename = revarg_opt & REVARG_CANNOT_BE_FILENAME;
1492 unsigned get_sha1_flags = 0;
1494 flags = flags & UNINTERESTING ? flags | BOTTOM : flags & ~BOTTOM;
1496 dotdot = strstr(arg, "..");
1497 if (dotdot) {
1498 unsigned char from_sha1[20];
1499 const char *next = dotdot + 2;
1500 const char *this = arg;
1501 int symmetric = *next == '.';
1502 unsigned int flags_exclude = flags ^ (UNINTERESTING | BOTTOM);
1503 static const char head_by_default[] = "HEAD";
1504 unsigned int a_flags;
1506 *dotdot = 0;
1507 next += symmetric;
1509 if (!*next)
1510 next = head_by_default;
1511 if (dotdot == arg)
1512 this = head_by_default;
1513 if (this == head_by_default && next == head_by_default &&
1514 !symmetric) {
1516 * Just ".."? That is not a range but the
1517 * pathspec for the parent directory.
1519 if (!cant_be_filename) {
1520 *dotdot = '.';
1521 return -1;
1524 if (!get_sha1_committish(this, from_sha1) &&
1525 !get_sha1_committish(next, sha1)) {
1526 struct object *a_obj, *b_obj;
1528 if (!cant_be_filename) {
1529 *dotdot = '.';
1530 verify_non_filename(revs->prefix, arg);
1533 a_obj = parse_object(from_sha1);
1534 b_obj = parse_object(sha1);
1535 if (!a_obj || !b_obj) {
1536 missing:
1537 if (revs->ignore_missing)
1538 return 0;
1539 die(symmetric
1540 ? "Invalid symmetric difference expression %s"
1541 : "Invalid revision range %s", arg);
1544 if (!symmetric) {
1545 /* just A..B */
1546 a_flags = flags_exclude;
1547 } else {
1548 /* A...B -- find merge bases between the two */
1549 struct commit *a, *b;
1550 struct commit_list *exclude;
1552 a = (a_obj->type == OBJ_COMMIT
1553 ? (struct commit *)a_obj
1554 : lookup_commit_reference(a_obj->sha1));
1555 b = (b_obj->type == OBJ_COMMIT
1556 ? (struct commit *)b_obj
1557 : lookup_commit_reference(b_obj->sha1));
1558 if (!a || !b)
1559 goto missing;
1560 exclude = get_merge_bases(a, b);
1561 add_rev_cmdline_list(revs, exclude,
1562 REV_CMD_MERGE_BASE,
1563 flags_exclude);
1564 add_pending_commit_list(revs, exclude,
1565 flags_exclude);
1566 free_commit_list(exclude);
1568 a_flags = flags | SYMMETRIC_LEFT;
1571 a_obj->flags |= a_flags;
1572 b_obj->flags |= flags;
1573 add_rev_cmdline(revs, a_obj, this,
1574 REV_CMD_LEFT, a_flags);
1575 add_rev_cmdline(revs, b_obj, next,
1576 REV_CMD_RIGHT, flags);
1577 add_pending_object(revs, a_obj, this);
1578 add_pending_object(revs, b_obj, next);
1579 return 0;
1581 *dotdot = '.';
1583 dotdot = strstr(arg, "^@");
1584 if (dotdot && !dotdot[2]) {
1585 *dotdot = 0;
1586 if (add_parents_only(revs, arg, flags))
1587 return 0;
1588 *dotdot = '^';
1590 dotdot = strstr(arg, "^!");
1591 if (dotdot && !dotdot[2]) {
1592 *dotdot = 0;
1593 if (!add_parents_only(revs, arg, flags ^ (UNINTERESTING | BOTTOM)))
1594 *dotdot = '^';
1597 local_flags = 0;
1598 if (*arg == '^') {
1599 local_flags = UNINTERESTING | BOTTOM;
1600 arg++;
1603 if (revarg_opt & REVARG_COMMITTISH)
1604 get_sha1_flags = GET_SHA1_COMMITTISH;
1606 if (get_sha1_with_context(arg, get_sha1_flags, sha1, &oc))
1607 return revs->ignore_missing ? 0 : -1;
1608 if (!cant_be_filename)
1609 verify_non_filename(revs->prefix, arg);
1610 object = get_reference(revs, arg, sha1, flags ^ local_flags);
1611 add_rev_cmdline(revs, object, arg_, REV_CMD_REV, flags ^ local_flags);
1612 add_pending_object_with_mode(revs, object, arg, oc.mode);
1613 return 0;
1616 struct cmdline_pathspec {
1617 int alloc;
1618 int nr;
1619 const char **path;
1622 static void append_prune_data(struct cmdline_pathspec *prune, const char **av)
1624 while (*av) {
1625 ALLOC_GROW(prune->path, prune->nr + 1, prune->alloc);
1626 prune->path[prune->nr++] = *(av++);
1630 static void read_pathspec_from_stdin(struct rev_info *revs, struct strbuf *sb,
1631 struct cmdline_pathspec *prune)
1633 while (strbuf_getwholeline(sb, stdin, '\n') != EOF) {
1634 int len = sb->len;
1635 if (len && sb->buf[len - 1] == '\n')
1636 sb->buf[--len] = '\0';
1637 ALLOC_GROW(prune->path, prune->nr + 1, prune->alloc);
1638 prune->path[prune->nr++] = xstrdup(sb->buf);
1642 static void read_revisions_from_stdin(struct rev_info *revs,
1643 struct cmdline_pathspec *prune)
1645 struct strbuf sb;
1646 int seen_dashdash = 0;
1647 int save_warning;
1649 save_warning = warn_on_object_refname_ambiguity;
1650 warn_on_object_refname_ambiguity = 0;
1652 strbuf_init(&sb, 1000);
1653 while (strbuf_getwholeline(&sb, stdin, '\n') != EOF) {
1654 int len = sb.len;
1655 if (len && sb.buf[len - 1] == '\n')
1656 sb.buf[--len] = '\0';
1657 if (!len)
1658 break;
1659 if (sb.buf[0] == '-') {
1660 if (len == 2 && sb.buf[1] == '-') {
1661 seen_dashdash = 1;
1662 break;
1664 die("options not supported in --stdin mode");
1666 if (handle_revision_arg(sb.buf, revs, 0,
1667 REVARG_CANNOT_BE_FILENAME))
1668 die("bad revision '%s'", sb.buf);
1670 if (seen_dashdash)
1671 read_pathspec_from_stdin(revs, &sb, prune);
1673 strbuf_release(&sb);
1674 warn_on_object_refname_ambiguity = save_warning;
1677 static void add_grep(struct rev_info *revs, const char *ptn, enum grep_pat_token what)
1679 append_grep_pattern(&revs->grep_filter, ptn, "command line", 0, what);
1682 static void add_header_grep(struct rev_info *revs, enum grep_header_field field, const char *pattern)
1684 append_header_grep_pattern(&revs->grep_filter, field, pattern);
1687 static void add_message_grep(struct rev_info *revs, const char *pattern)
1689 add_grep(revs, pattern, GREP_PATTERN_BODY);
1692 static int handle_revision_opt(struct rev_info *revs, int argc, const char **argv,
1693 int *unkc, const char **unkv)
1695 const char *arg = argv[0];
1696 const char *optarg;
1697 int argcount;
1699 /* pseudo revision arguments */
1700 if (!strcmp(arg, "--all") || !strcmp(arg, "--branches") ||
1701 !strcmp(arg, "--tags") || !strcmp(arg, "--remotes") ||
1702 !strcmp(arg, "--reflog") || !strcmp(arg, "--not") ||
1703 !strcmp(arg, "--no-walk") || !strcmp(arg, "--do-walk") ||
1704 !strcmp(arg, "--bisect") || starts_with(arg, "--glob=") ||
1705 !strcmp(arg, "--indexed-objects") ||
1706 starts_with(arg, "--exclude=") ||
1707 starts_with(arg, "--branches=") || starts_with(arg, "--tags=") ||
1708 starts_with(arg, "--remotes=") || starts_with(arg, "--no-walk="))
1710 unkv[(*unkc)++] = arg;
1711 return 1;
1714 if ((argcount = parse_long_opt("max-count", argv, &optarg))) {
1715 revs->max_count = atoi(optarg);
1716 revs->no_walk = 0;
1717 return argcount;
1718 } else if ((argcount = parse_long_opt("skip", argv, &optarg))) {
1719 revs->skip_count = atoi(optarg);
1720 return argcount;
1721 } else if ((*arg == '-') && isdigit(arg[1])) {
1722 /* accept -<digit>, like traditional "head" */
1723 if (strtol_i(arg + 1, 10, &revs->max_count) < 0 ||
1724 revs->max_count < 0)
1725 die("'%s': not a non-negative integer", arg + 1);
1726 revs->no_walk = 0;
1727 } else if (!strcmp(arg, "-n")) {
1728 if (argc <= 1)
1729 return error("-n requires an argument");
1730 revs->max_count = atoi(argv[1]);
1731 revs->no_walk = 0;
1732 return 2;
1733 } else if (starts_with(arg, "-n")) {
1734 revs->max_count = atoi(arg + 2);
1735 revs->no_walk = 0;
1736 } else if ((argcount = parse_long_opt("max-age", argv, &optarg))) {
1737 revs->max_age = atoi(optarg);
1738 return argcount;
1739 } else if ((argcount = parse_long_opt("since", argv, &optarg))) {
1740 revs->max_age = approxidate(optarg);
1741 return argcount;
1742 } else if ((argcount = parse_long_opt("after", argv, &optarg))) {
1743 revs->max_age = approxidate(optarg);
1744 return argcount;
1745 } else if ((argcount = parse_long_opt("min-age", argv, &optarg))) {
1746 revs->min_age = atoi(optarg);
1747 return argcount;
1748 } else if ((argcount = parse_long_opt("before", argv, &optarg))) {
1749 revs->min_age = approxidate(optarg);
1750 return argcount;
1751 } else if ((argcount = parse_long_opt("until", argv, &optarg))) {
1752 revs->min_age = approxidate(optarg);
1753 return argcount;
1754 } else if (!strcmp(arg, "--first-parent")) {
1755 revs->first_parent_only = 1;
1756 } else if (!strcmp(arg, "--ancestry-path")) {
1757 revs->ancestry_path = 1;
1758 revs->simplify_history = 0;
1759 revs->limited = 1;
1760 } else if (!strcmp(arg, "-g") || !strcmp(arg, "--walk-reflogs")) {
1761 init_reflog_walk(&revs->reflog_info);
1762 } else if (!strcmp(arg, "--default")) {
1763 if (argc <= 1)
1764 return error("bad --default argument");
1765 revs->def = argv[1];
1766 return 2;
1767 } else if (!strcmp(arg, "--merge")) {
1768 revs->show_merge = 1;
1769 } else if (!strcmp(arg, "--topo-order")) {
1770 revs->sort_order = REV_SORT_IN_GRAPH_ORDER;
1771 revs->topo_order = 1;
1772 } else if (!strcmp(arg, "--simplify-merges")) {
1773 revs->simplify_merges = 1;
1774 revs->topo_order = 1;
1775 revs->rewrite_parents = 1;
1776 revs->simplify_history = 0;
1777 revs->limited = 1;
1778 } else if (!strcmp(arg, "--simplify-by-decoration")) {
1779 revs->simplify_merges = 1;
1780 revs->topo_order = 1;
1781 revs->rewrite_parents = 1;
1782 revs->simplify_history = 0;
1783 revs->simplify_by_decoration = 1;
1784 revs->limited = 1;
1785 revs->prune = 1;
1786 load_ref_decorations(DECORATE_SHORT_REFS);
1787 } else if (!strcmp(arg, "--date-order")) {
1788 revs->sort_order = REV_SORT_BY_COMMIT_DATE;
1789 revs->topo_order = 1;
1790 } else if (!strcmp(arg, "--author-date-order")) {
1791 revs->sort_order = REV_SORT_BY_AUTHOR_DATE;
1792 revs->topo_order = 1;
1793 } else if (starts_with(arg, "--early-output")) {
1794 int count = 100;
1795 switch (arg[14]) {
1796 case '=':
1797 count = atoi(arg+15);
1798 /* Fallthrough */
1799 case 0:
1800 revs->topo_order = 1;
1801 revs->early_output = count;
1803 } else if (!strcmp(arg, "--parents")) {
1804 revs->rewrite_parents = 1;
1805 revs->print_parents = 1;
1806 } else if (!strcmp(arg, "--dense")) {
1807 revs->dense = 1;
1808 } else if (!strcmp(arg, "--sparse")) {
1809 revs->dense = 0;
1810 } else if (!strcmp(arg, "--show-all")) {
1811 revs->show_all = 1;
1812 } else if (!strcmp(arg, "--remove-empty")) {
1813 revs->remove_empty_trees = 1;
1814 } else if (!strcmp(arg, "--merges")) {
1815 revs->min_parents = 2;
1816 } else if (!strcmp(arg, "--no-merges")) {
1817 revs->max_parents = 1;
1818 } else if (starts_with(arg, "--min-parents=")) {
1819 revs->min_parents = atoi(arg+14);
1820 } else if (starts_with(arg, "--no-min-parents")) {
1821 revs->min_parents = 0;
1822 } else if (starts_with(arg, "--max-parents=")) {
1823 revs->max_parents = atoi(arg+14);
1824 } else if (starts_with(arg, "--no-max-parents")) {
1825 revs->max_parents = -1;
1826 } else if (!strcmp(arg, "--boundary")) {
1827 revs->boundary = 1;
1828 } else if (!strcmp(arg, "--left-right")) {
1829 revs->left_right = 1;
1830 } else if (!strcmp(arg, "--left-only")) {
1831 if (revs->right_only)
1832 die("--left-only is incompatible with --right-only"
1833 " or --cherry");
1834 revs->left_only = 1;
1835 } else if (!strcmp(arg, "--right-only")) {
1836 if (revs->left_only)
1837 die("--right-only is incompatible with --left-only");
1838 revs->right_only = 1;
1839 } else if (!strcmp(arg, "--cherry")) {
1840 if (revs->left_only)
1841 die("--cherry is incompatible with --left-only");
1842 revs->cherry_mark = 1;
1843 revs->right_only = 1;
1844 revs->max_parents = 1;
1845 revs->limited = 1;
1846 } else if (!strcmp(arg, "--count")) {
1847 revs->count = 1;
1848 } else if (!strcmp(arg, "--cherry-mark")) {
1849 if (revs->cherry_pick)
1850 die("--cherry-mark is incompatible with --cherry-pick");
1851 revs->cherry_mark = 1;
1852 revs->limited = 1; /* needs limit_list() */
1853 } else if (!strcmp(arg, "--cherry-pick")) {
1854 if (revs->cherry_mark)
1855 die("--cherry-pick is incompatible with --cherry-mark");
1856 revs->cherry_pick = 1;
1857 revs->limited = 1;
1858 } else if (!strcmp(arg, "--objects")) {
1859 revs->tag_objects = 1;
1860 revs->tree_objects = 1;
1861 revs->blob_objects = 1;
1862 } else if (!strcmp(arg, "--objects-edge")) {
1863 revs->tag_objects = 1;
1864 revs->tree_objects = 1;
1865 revs->blob_objects = 1;
1866 revs->edge_hint = 1;
1867 } else if (!strcmp(arg, "--objects-edge-aggressive")) {
1868 revs->tag_objects = 1;
1869 revs->tree_objects = 1;
1870 revs->blob_objects = 1;
1871 revs->edge_hint = 1;
1872 revs->edge_hint_aggressive = 1;
1873 } else if (!strcmp(arg, "--verify-objects")) {
1874 revs->tag_objects = 1;
1875 revs->tree_objects = 1;
1876 revs->blob_objects = 1;
1877 revs->verify_objects = 1;
1878 } else if (!strcmp(arg, "--unpacked")) {
1879 revs->unpacked = 1;
1880 } else if (starts_with(arg, "--unpacked=")) {
1881 die("--unpacked=<packfile> no longer supported.");
1882 } else if (!strcmp(arg, "-r")) {
1883 revs->diff = 1;
1884 DIFF_OPT_SET(&revs->diffopt, RECURSIVE);
1885 } else if (!strcmp(arg, "-t")) {
1886 revs->diff = 1;
1887 DIFF_OPT_SET(&revs->diffopt, RECURSIVE);
1888 DIFF_OPT_SET(&revs->diffopt, TREE_IN_RECURSIVE);
1889 } else if (!strcmp(arg, "-m")) {
1890 revs->ignore_merges = 0;
1891 } else if (!strcmp(arg, "-c")) {
1892 revs->diff = 1;
1893 revs->dense_combined_merges = 0;
1894 revs->combine_merges = 1;
1895 } else if (!strcmp(arg, "--cc")) {
1896 revs->diff = 1;
1897 revs->dense_combined_merges = 1;
1898 revs->combine_merges = 1;
1899 } else if (!strcmp(arg, "-v")) {
1900 revs->verbose_header = 1;
1901 } else if (!strcmp(arg, "--pretty")) {
1902 revs->verbose_header = 1;
1903 revs->pretty_given = 1;
1904 get_commit_format(NULL, revs);
1905 } else if (starts_with(arg, "--pretty=") || starts_with(arg, "--format=")) {
1907 * Detached form ("--pretty X" as opposed to "--pretty=X")
1908 * not allowed, since the argument is optional.
1910 revs->verbose_header = 1;
1911 revs->pretty_given = 1;
1912 get_commit_format(arg+9, revs);
1913 } else if (!strcmp(arg, "--show-notes") || !strcmp(arg, "--notes")) {
1914 revs->show_notes = 1;
1915 revs->show_notes_given = 1;
1916 revs->notes_opt.use_default_notes = 1;
1917 } else if (!strcmp(arg, "--show-signature")) {
1918 revs->show_signature = 1;
1919 } else if (!strcmp(arg, "--show-linear-break") ||
1920 starts_with(arg, "--show-linear-break=")) {
1921 if (starts_with(arg, "--show-linear-break="))
1922 revs->break_bar = xstrdup(arg + 20);
1923 else
1924 revs->break_bar = " ..........";
1925 revs->track_linear = 1;
1926 revs->track_first_time = 1;
1927 } else if (starts_with(arg, "--show-notes=") ||
1928 starts_with(arg, "--notes=")) {
1929 struct strbuf buf = STRBUF_INIT;
1930 revs->show_notes = 1;
1931 revs->show_notes_given = 1;
1932 if (starts_with(arg, "--show-notes")) {
1933 if (revs->notes_opt.use_default_notes < 0)
1934 revs->notes_opt.use_default_notes = 1;
1935 strbuf_addstr(&buf, arg+13);
1937 else
1938 strbuf_addstr(&buf, arg+8);
1939 expand_notes_ref(&buf);
1940 string_list_append(&revs->notes_opt.extra_notes_refs,
1941 strbuf_detach(&buf, NULL));
1942 } else if (!strcmp(arg, "--no-notes")) {
1943 revs->show_notes = 0;
1944 revs->show_notes_given = 1;
1945 revs->notes_opt.use_default_notes = -1;
1946 /* we have been strdup'ing ourselves, so trick
1947 * string_list into free()ing strings */
1948 revs->notes_opt.extra_notes_refs.strdup_strings = 1;
1949 string_list_clear(&revs->notes_opt.extra_notes_refs, 0);
1950 revs->notes_opt.extra_notes_refs.strdup_strings = 0;
1951 } else if (!strcmp(arg, "--standard-notes")) {
1952 revs->show_notes_given = 1;
1953 revs->notes_opt.use_default_notes = 1;
1954 } else if (!strcmp(arg, "--no-standard-notes")) {
1955 revs->notes_opt.use_default_notes = 0;
1956 } else if (!strcmp(arg, "--oneline")) {
1957 revs->verbose_header = 1;
1958 get_commit_format("oneline", revs);
1959 revs->pretty_given = 1;
1960 revs->abbrev_commit = 1;
1961 } else if (!strcmp(arg, "--graph")) {
1962 revs->topo_order = 1;
1963 revs->rewrite_parents = 1;
1964 revs->graph = graph_init(revs);
1965 } else if (!strcmp(arg, "--root")) {
1966 revs->show_root_diff = 1;
1967 } else if (!strcmp(arg, "--no-commit-id")) {
1968 revs->no_commit_id = 1;
1969 } else if (!strcmp(arg, "--always")) {
1970 revs->always_show_header = 1;
1971 } else if (!strcmp(arg, "--no-abbrev")) {
1972 revs->abbrev = 0;
1973 } else if (!strcmp(arg, "--abbrev")) {
1974 revs->abbrev = DEFAULT_ABBREV;
1975 } else if (starts_with(arg, "--abbrev=")) {
1976 revs->abbrev = strtoul(arg + 9, NULL, 10);
1977 if (revs->abbrev < MINIMUM_ABBREV)
1978 revs->abbrev = MINIMUM_ABBREV;
1979 else if (revs->abbrev > 40)
1980 revs->abbrev = 40;
1981 } else if (!strcmp(arg, "--abbrev-commit")) {
1982 revs->abbrev_commit = 1;
1983 revs->abbrev_commit_given = 1;
1984 } else if (!strcmp(arg, "--no-abbrev-commit")) {
1985 revs->abbrev_commit = 0;
1986 } else if (!strcmp(arg, "--full-diff")) {
1987 revs->diff = 1;
1988 revs->full_diff = 1;
1989 } else if (!strcmp(arg, "--full-history")) {
1990 revs->simplify_history = 0;
1991 } else if (!strcmp(arg, "--relative-date")) {
1992 revs->date_mode = DATE_RELATIVE;
1993 revs->date_mode_explicit = 1;
1994 } else if ((argcount = parse_long_opt("date", argv, &optarg))) {
1995 revs->date_mode = parse_date_format(optarg);
1996 revs->date_mode_explicit = 1;
1997 return argcount;
1998 } else if (!strcmp(arg, "--log-size")) {
1999 revs->show_log_size = 1;
2002 * Grepping the commit log
2004 else if ((argcount = parse_long_opt("author", argv, &optarg))) {
2005 add_header_grep(revs, GREP_HEADER_AUTHOR, optarg);
2006 return argcount;
2007 } else if ((argcount = parse_long_opt("committer", argv, &optarg))) {
2008 add_header_grep(revs, GREP_HEADER_COMMITTER, optarg);
2009 return argcount;
2010 } else if ((argcount = parse_long_opt("grep-reflog", argv, &optarg))) {
2011 add_header_grep(revs, GREP_HEADER_REFLOG, optarg);
2012 return argcount;
2013 } else if ((argcount = parse_long_opt("grep", argv, &optarg))) {
2014 add_message_grep(revs, optarg);
2015 return argcount;
2016 } else if (!strcmp(arg, "--grep-debug")) {
2017 revs->grep_filter.debug = 1;
2018 } else if (!strcmp(arg, "--basic-regexp")) {
2019 grep_set_pattern_type_option(GREP_PATTERN_TYPE_BRE, &revs->grep_filter);
2020 } else if (!strcmp(arg, "--extended-regexp") || !strcmp(arg, "-E")) {
2021 grep_set_pattern_type_option(GREP_PATTERN_TYPE_ERE, &revs->grep_filter);
2022 } else if (!strcmp(arg, "--regexp-ignore-case") || !strcmp(arg, "-i")) {
2023 revs->grep_filter.regflags |= REG_ICASE;
2024 DIFF_OPT_SET(&revs->diffopt, PICKAXE_IGNORE_CASE);
2025 } else if (!strcmp(arg, "--fixed-strings") || !strcmp(arg, "-F")) {
2026 grep_set_pattern_type_option(GREP_PATTERN_TYPE_FIXED, &revs->grep_filter);
2027 } else if (!strcmp(arg, "--perl-regexp")) {
2028 grep_set_pattern_type_option(GREP_PATTERN_TYPE_PCRE, &revs->grep_filter);
2029 } else if (!strcmp(arg, "--all-match")) {
2030 revs->grep_filter.all_match = 1;
2031 } else if (!strcmp(arg, "--invert-grep")) {
2032 revs->invert_grep = 1;
2033 } else if ((argcount = parse_long_opt("encoding", argv, &optarg))) {
2034 if (strcmp(optarg, "none"))
2035 git_log_output_encoding = xstrdup(optarg);
2036 else
2037 git_log_output_encoding = "";
2038 return argcount;
2039 } else if (!strcmp(arg, "--reverse")) {
2040 revs->reverse ^= 1;
2041 } else if (!strcmp(arg, "--children")) {
2042 revs->children.name = "children";
2043 revs->limited = 1;
2044 } else if (!strcmp(arg, "--ignore-missing")) {
2045 revs->ignore_missing = 1;
2046 } else {
2047 int opts = diff_opt_parse(&revs->diffopt, argv, argc);
2048 if (!opts)
2049 unkv[(*unkc)++] = arg;
2050 return opts;
2052 if (revs->graph && revs->track_linear)
2053 die("--show-linear-break and --graph are incompatible");
2055 return 1;
2058 void parse_revision_opt(struct rev_info *revs, struct parse_opt_ctx_t *ctx,
2059 const struct option *options,
2060 const char * const usagestr[])
2062 int n = handle_revision_opt(revs, ctx->argc, ctx->argv,
2063 &ctx->cpidx, ctx->out);
2064 if (n <= 0) {
2065 error("unknown option `%s'", ctx->argv[0]);
2066 usage_with_options(usagestr, options);
2068 ctx->argv += n;
2069 ctx->argc -= n;
2072 static int for_each_bad_bisect_ref(const char *submodule, each_ref_fn fn, void *cb_data)
2074 return for_each_ref_in_submodule(submodule, "refs/bisect/bad", fn, cb_data);
2077 static int for_each_good_bisect_ref(const char *submodule, each_ref_fn fn, void *cb_data)
2079 return for_each_ref_in_submodule(submodule, "refs/bisect/good", fn, cb_data);
2082 static int handle_revision_pseudo_opt(const char *submodule,
2083 struct rev_info *revs,
2084 int argc, const char **argv, int *flags)
2086 const char *arg = argv[0];
2087 const char *optarg;
2088 int argcount;
2091 * NOTE!
2093 * Commands like "git shortlog" will not accept the options below
2094 * unless parse_revision_opt queues them (as opposed to erroring
2095 * out).
2097 * When implementing your new pseudo-option, remember to
2098 * register it in the list at the top of handle_revision_opt.
2100 if (!strcmp(arg, "--all")) {
2101 handle_refs(submodule, revs, *flags, for_each_ref_submodule);
2102 handle_refs(submodule, revs, *flags, head_ref_submodule);
2103 clear_ref_exclusion(&revs->ref_excludes);
2104 } else if (!strcmp(arg, "--branches")) {
2105 handle_refs(submodule, revs, *flags, for_each_branch_ref_submodule);
2106 clear_ref_exclusion(&revs->ref_excludes);
2107 } else if (!strcmp(arg, "--bisect")) {
2108 handle_refs(submodule, revs, *flags, for_each_bad_bisect_ref);
2109 handle_refs(submodule, revs, *flags ^ (UNINTERESTING | BOTTOM), for_each_good_bisect_ref);
2110 revs->bisect = 1;
2111 } else if (!strcmp(arg, "--tags")) {
2112 handle_refs(submodule, revs, *flags, for_each_tag_ref_submodule);
2113 clear_ref_exclusion(&revs->ref_excludes);
2114 } else if (!strcmp(arg, "--remotes")) {
2115 handle_refs(submodule, revs, *flags, for_each_remote_ref_submodule);
2116 clear_ref_exclusion(&revs->ref_excludes);
2117 } else if ((argcount = parse_long_opt("glob", argv, &optarg))) {
2118 struct all_refs_cb cb;
2119 init_all_refs_cb(&cb, revs, *flags);
2120 for_each_glob_ref(handle_one_ref, optarg, &cb);
2121 clear_ref_exclusion(&revs->ref_excludes);
2122 return argcount;
2123 } else if ((argcount = parse_long_opt("exclude", argv, &optarg))) {
2124 add_ref_exclusion(&revs->ref_excludes, optarg);
2125 return argcount;
2126 } else if (starts_with(arg, "--branches=")) {
2127 struct all_refs_cb cb;
2128 init_all_refs_cb(&cb, revs, *flags);
2129 for_each_glob_ref_in(handle_one_ref, arg + 11, "refs/heads/", &cb);
2130 clear_ref_exclusion(&revs->ref_excludes);
2131 } else if (starts_with(arg, "--tags=")) {
2132 struct all_refs_cb cb;
2133 init_all_refs_cb(&cb, revs, *flags);
2134 for_each_glob_ref_in(handle_one_ref, arg + 7, "refs/tags/", &cb);
2135 clear_ref_exclusion(&revs->ref_excludes);
2136 } else if (starts_with(arg, "--remotes=")) {
2137 struct all_refs_cb cb;
2138 init_all_refs_cb(&cb, revs, *flags);
2139 for_each_glob_ref_in(handle_one_ref, arg + 10, "refs/remotes/", &cb);
2140 clear_ref_exclusion(&revs->ref_excludes);
2141 } else if (!strcmp(arg, "--reflog")) {
2142 add_reflogs_to_pending(revs, *flags);
2143 } else if (!strcmp(arg, "--indexed-objects")) {
2144 add_index_objects_to_pending(revs, *flags);
2145 } else if (!strcmp(arg, "--not")) {
2146 *flags ^= UNINTERESTING | BOTTOM;
2147 } else if (!strcmp(arg, "--no-walk")) {
2148 revs->no_walk = REVISION_WALK_NO_WALK_SORTED;
2149 } else if (starts_with(arg, "--no-walk=")) {
2151 * Detached form ("--no-walk X" as opposed to "--no-walk=X")
2152 * not allowed, since the argument is optional.
2154 if (!strcmp(arg + 10, "sorted"))
2155 revs->no_walk = REVISION_WALK_NO_WALK_SORTED;
2156 else if (!strcmp(arg + 10, "unsorted"))
2157 revs->no_walk = REVISION_WALK_NO_WALK_UNSORTED;
2158 else
2159 return error("invalid argument to --no-walk");
2160 } else if (!strcmp(arg, "--do-walk")) {
2161 revs->no_walk = 0;
2162 } else {
2163 return 0;
2166 return 1;
2169 static void NORETURN diagnose_missing_default(const char *def)
2171 unsigned char sha1[20];
2172 int flags;
2173 const char *refname;
2175 refname = resolve_ref_unsafe(def, 0, sha1, &flags);
2176 if (!refname || !(flags & REF_ISSYMREF) || (flags & REF_ISBROKEN))
2177 die(_("your current branch appears to be broken"));
2179 skip_prefix(refname, "refs/heads/", &refname);
2180 die(_("your current branch '%s' does not have any commits yet"),
2181 refname);
2185 * Parse revision information, filling in the "rev_info" structure,
2186 * and removing the used arguments from the argument list.
2188 * Returns the number of arguments left that weren't recognized
2189 * (which are also moved to the head of the argument list)
2191 int setup_revisions(int argc, const char **argv, struct rev_info *revs, struct setup_revision_opt *opt)
2193 int i, flags, left, seen_dashdash, read_from_stdin, got_rev_arg = 0, revarg_opt;
2194 struct cmdline_pathspec prune_data;
2195 const char *submodule = NULL;
2197 memset(&prune_data, 0, sizeof(prune_data));
2198 if (opt)
2199 submodule = opt->submodule;
2201 /* First, search for "--" */
2202 if (opt && opt->assume_dashdash) {
2203 seen_dashdash = 1;
2204 } else {
2205 seen_dashdash = 0;
2206 for (i = 1; i < argc; i++) {
2207 const char *arg = argv[i];
2208 if (strcmp(arg, "--"))
2209 continue;
2210 argv[i] = NULL;
2211 argc = i;
2212 if (argv[i + 1])
2213 append_prune_data(&prune_data, argv + i + 1);
2214 seen_dashdash = 1;
2215 break;
2219 /* Second, deal with arguments and options */
2220 flags = 0;
2221 revarg_opt = opt ? opt->revarg_opt : 0;
2222 if (seen_dashdash)
2223 revarg_opt |= REVARG_CANNOT_BE_FILENAME;
2224 read_from_stdin = 0;
2225 for (left = i = 1; i < argc; i++) {
2226 const char *arg = argv[i];
2227 if (*arg == '-') {
2228 int opts;
2230 opts = handle_revision_pseudo_opt(submodule,
2231 revs, argc - i, argv + i,
2232 &flags);
2233 if (opts > 0) {
2234 i += opts - 1;
2235 continue;
2238 if (!strcmp(arg, "--stdin")) {
2239 if (revs->disable_stdin) {
2240 argv[left++] = arg;
2241 continue;
2243 if (read_from_stdin++)
2244 die("--stdin given twice?");
2245 read_revisions_from_stdin(revs, &prune_data);
2246 continue;
2249 opts = handle_revision_opt(revs, argc - i, argv + i, &left, argv);
2250 if (opts > 0) {
2251 i += opts - 1;
2252 continue;
2254 if (opts < 0)
2255 exit(128);
2256 continue;
2260 if (handle_revision_arg(arg, revs, flags, revarg_opt)) {
2261 int j;
2262 if (seen_dashdash || *arg == '^')
2263 die("bad revision '%s'", arg);
2265 /* If we didn't have a "--":
2266 * (1) all filenames must exist;
2267 * (2) all rev-args must not be interpretable
2268 * as a valid filename.
2269 * but the latter we have checked in the main loop.
2271 for (j = i; j < argc; j++)
2272 verify_filename(revs->prefix, argv[j], j == i);
2274 append_prune_data(&prune_data, argv + i);
2275 break;
2277 else
2278 got_rev_arg = 1;
2281 if (prune_data.nr) {
2283 * If we need to introduce the magic "a lone ':' means no
2284 * pathspec whatsoever", here is the place to do so.
2286 * if (prune_data.nr == 1 && !strcmp(prune_data[0], ":")) {
2287 * prune_data.nr = 0;
2288 * prune_data.alloc = 0;
2289 * free(prune_data.path);
2290 * prune_data.path = NULL;
2291 * } else {
2292 * terminate prune_data.alloc with NULL and
2293 * call init_pathspec() to set revs->prune_data here.
2296 ALLOC_GROW(prune_data.path, prune_data.nr + 1, prune_data.alloc);
2297 prune_data.path[prune_data.nr++] = NULL;
2298 parse_pathspec(&revs->prune_data, 0, 0,
2299 revs->prefix, prune_data.path);
2302 if (revs->def == NULL)
2303 revs->def = opt ? opt->def : NULL;
2304 if (opt && opt->tweak)
2305 opt->tweak(revs, opt);
2306 if (revs->show_merge)
2307 prepare_show_merge(revs);
2308 if (revs->def && !revs->pending.nr && !got_rev_arg) {
2309 unsigned char sha1[20];
2310 struct object *object;
2311 struct object_context oc;
2312 if (get_sha1_with_context(revs->def, 0, sha1, &oc))
2313 diagnose_missing_default(revs->def);
2314 object = get_reference(revs, revs->def, sha1, 0);
2315 add_pending_object_with_mode(revs, object, revs->def, oc.mode);
2318 /* Did the user ask for any diff output? Run the diff! */
2319 if (revs->diffopt.output_format & ~DIFF_FORMAT_NO_OUTPUT)
2320 revs->diff = 1;
2322 /* Pickaxe, diff-filter and rename following need diffs */
2323 if (revs->diffopt.pickaxe ||
2324 revs->diffopt.filter ||
2325 DIFF_OPT_TST(&revs->diffopt, FOLLOW_RENAMES))
2326 revs->diff = 1;
2328 if (revs->topo_order)
2329 revs->limited = 1;
2331 if (revs->prune_data.nr) {
2332 copy_pathspec(&revs->pruning.pathspec, &revs->prune_data);
2333 /* Can't prune commits with rename following: the paths change.. */
2334 if (!DIFF_OPT_TST(&revs->diffopt, FOLLOW_RENAMES))
2335 revs->prune = 1;
2336 if (!revs->full_diff)
2337 copy_pathspec(&revs->diffopt.pathspec,
2338 &revs->prune_data);
2340 if (revs->combine_merges)
2341 revs->ignore_merges = 0;
2342 revs->diffopt.abbrev = revs->abbrev;
2344 if (revs->line_level_traverse) {
2345 revs->limited = 1;
2346 revs->topo_order = 1;
2349 diff_setup_done(&revs->diffopt);
2351 grep_commit_pattern_type(GREP_PATTERN_TYPE_UNSPECIFIED,
2352 &revs->grep_filter);
2353 compile_grep_patterns(&revs->grep_filter);
2355 if (revs->reverse && revs->reflog_info)
2356 die("cannot combine --reverse with --walk-reflogs");
2357 if (revs->rewrite_parents && revs->children.name)
2358 die("cannot combine --parents and --children");
2361 * Limitations on the graph functionality
2363 if (revs->reverse && revs->graph)
2364 die("cannot combine --reverse with --graph");
2366 if (revs->reflog_info && revs->graph)
2367 die("cannot combine --walk-reflogs with --graph");
2368 if (revs->no_walk && revs->graph)
2369 die("cannot combine --no-walk with --graph");
2370 if (!revs->reflog_info && revs->grep_filter.use_reflog_filter)
2371 die("cannot use --grep-reflog without --walk-reflogs");
2373 if (revs->first_parent_only && revs->bisect)
2374 die(_("--first-parent is incompatible with --bisect"));
2376 return left;
2379 static void add_child(struct rev_info *revs, struct commit *parent, struct commit *child)
2381 struct commit_list *l = xcalloc(1, sizeof(*l));
2383 l->item = child;
2384 l->next = add_decoration(&revs->children, &parent->object, l);
2387 static int remove_duplicate_parents(struct rev_info *revs, struct commit *commit)
2389 struct treesame_state *ts = lookup_decoration(&revs->treesame, &commit->object);
2390 struct commit_list **pp, *p;
2391 int surviving_parents;
2393 /* Examine existing parents while marking ones we have seen... */
2394 pp = &commit->parents;
2395 surviving_parents = 0;
2396 while ((p = *pp) != NULL) {
2397 struct commit *parent = p->item;
2398 if (parent->object.flags & TMP_MARK) {
2399 *pp = p->next;
2400 if (ts)
2401 compact_treesame(revs, commit, surviving_parents);
2402 continue;
2404 parent->object.flags |= TMP_MARK;
2405 surviving_parents++;
2406 pp = &p->next;
2408 /* clear the temporary mark */
2409 for (p = commit->parents; p; p = p->next) {
2410 p->item->object.flags &= ~TMP_MARK;
2412 /* no update_treesame() - removing duplicates can't affect TREESAME */
2413 return surviving_parents;
2416 struct merge_simplify_state {
2417 struct commit *simplified;
2420 static struct merge_simplify_state *locate_simplify_state(struct rev_info *revs, struct commit *commit)
2422 struct merge_simplify_state *st;
2424 st = lookup_decoration(&revs->merge_simplification, &commit->object);
2425 if (!st) {
2426 st = xcalloc(1, sizeof(*st));
2427 add_decoration(&revs->merge_simplification, &commit->object, st);
2429 return st;
2432 static int mark_redundant_parents(struct rev_info *revs, struct commit *commit)
2434 struct commit_list *h = reduce_heads(commit->parents);
2435 int i = 0, marked = 0;
2436 struct commit_list *po, *pn;
2438 /* Want these for sanity-checking only */
2439 int orig_cnt = commit_list_count(commit->parents);
2440 int cnt = commit_list_count(h);
2443 * Not ready to remove items yet, just mark them for now, based
2444 * on the output of reduce_heads(). reduce_heads outputs the reduced
2445 * set in its original order, so this isn't too hard.
2447 po = commit->parents;
2448 pn = h;
2449 while (po) {
2450 if (pn && po->item == pn->item) {
2451 pn = pn->next;
2452 i++;
2453 } else {
2454 po->item->object.flags |= TMP_MARK;
2455 marked++;
2457 po=po->next;
2460 if (i != cnt || cnt+marked != orig_cnt)
2461 die("mark_redundant_parents %d %d %d %d", orig_cnt, cnt, i, marked);
2463 free_commit_list(h);
2465 return marked;
2468 static int mark_treesame_root_parents(struct rev_info *revs, struct commit *commit)
2470 struct commit_list *p;
2471 int marked = 0;
2473 for (p = commit->parents; p; p = p->next) {
2474 struct commit *parent = p->item;
2475 if (!parent->parents && (parent->object.flags & TREESAME)) {
2476 parent->object.flags |= TMP_MARK;
2477 marked++;
2481 return marked;
2485 * Awkward naming - this means one parent we are TREESAME to.
2486 * cf mark_treesame_root_parents: root parents that are TREESAME (to an
2487 * empty tree). Better name suggestions?
2489 static int leave_one_treesame_to_parent(struct rev_info *revs, struct commit *commit)
2491 struct treesame_state *ts = lookup_decoration(&revs->treesame, &commit->object);
2492 struct commit *unmarked = NULL, *marked = NULL;
2493 struct commit_list *p;
2494 unsigned n;
2496 for (p = commit->parents, n = 0; p; p = p->next, n++) {
2497 if (ts->treesame[n]) {
2498 if (p->item->object.flags & TMP_MARK) {
2499 if (!marked)
2500 marked = p->item;
2501 } else {
2502 if (!unmarked) {
2503 unmarked = p->item;
2504 break;
2511 * If we are TREESAME to a marked-for-deletion parent, but not to any
2512 * unmarked parents, unmark the first TREESAME parent. This is the
2513 * parent that the default simplify_history==1 scan would have followed,
2514 * and it doesn't make sense to omit that path when asking for a
2515 * simplified full history. Retaining it improves the chances of
2516 * understanding odd missed merges that took an old version of a file.
2518 * Example:
2520 * I--------*X A modified the file, but mainline merge X used
2521 * \ / "-s ours", so took the version from I. X is
2522 * `-*A--' TREESAME to I and !TREESAME to A.
2524 * Default log from X would produce "I". Without this check,
2525 * --full-history --simplify-merges would produce "I-A-X", showing
2526 * the merge commit X and that it changed A, but not making clear that
2527 * it had just taken the I version. With this check, the topology above
2528 * is retained.
2530 * Note that it is possible that the simplification chooses a different
2531 * TREESAME parent from the default, in which case this test doesn't
2532 * activate, and we _do_ drop the default parent. Example:
2534 * I------X A modified the file, but it was reverted in B,
2535 * \ / meaning mainline merge X is TREESAME to both
2536 * *A-*B parents.
2538 * Default log would produce "I" by following the first parent;
2539 * --full-history --simplify-merges will produce "I-A-B". But this is a
2540 * reasonable result - it presents a logical full history leading from
2541 * I to X, and X is not an important merge.
2543 if (!unmarked && marked) {
2544 marked->object.flags &= ~TMP_MARK;
2545 return 1;
2548 return 0;
2551 static int remove_marked_parents(struct rev_info *revs, struct commit *commit)
2553 struct commit_list **pp, *p;
2554 int nth_parent, removed = 0;
2556 pp = &commit->parents;
2557 nth_parent = 0;
2558 while ((p = *pp) != NULL) {
2559 struct commit *parent = p->item;
2560 if (parent->object.flags & TMP_MARK) {
2561 parent->object.flags &= ~TMP_MARK;
2562 *pp = p->next;
2563 free(p);
2564 removed++;
2565 compact_treesame(revs, commit, nth_parent);
2566 continue;
2568 pp = &p->next;
2569 nth_parent++;
2572 /* Removing parents can only increase TREESAMEness */
2573 if (removed && !(commit->object.flags & TREESAME))
2574 update_treesame(revs, commit);
2576 return nth_parent;
2579 static struct commit_list **simplify_one(struct rev_info *revs, struct commit *commit, struct commit_list **tail)
2581 struct commit_list *p;
2582 struct commit *parent;
2583 struct merge_simplify_state *st, *pst;
2584 int cnt;
2586 st = locate_simplify_state(revs, commit);
2589 * Have we handled this one?
2591 if (st->simplified)
2592 return tail;
2595 * An UNINTERESTING commit simplifies to itself, so does a
2596 * root commit. We do not rewrite parents of such commit
2597 * anyway.
2599 if ((commit->object.flags & UNINTERESTING) || !commit->parents) {
2600 st->simplified = commit;
2601 return tail;
2605 * Do we know what commit all of our parents that matter
2606 * should be rewritten to? Otherwise we are not ready to
2607 * rewrite this one yet.
2609 for (cnt = 0, p = commit->parents; p; p = p->next) {
2610 pst = locate_simplify_state(revs, p->item);
2611 if (!pst->simplified) {
2612 tail = &commit_list_insert(p->item, tail)->next;
2613 cnt++;
2615 if (revs->first_parent_only)
2616 break;
2618 if (cnt) {
2619 tail = &commit_list_insert(commit, tail)->next;
2620 return tail;
2624 * Rewrite our list of parents. Note that this cannot
2625 * affect our TREESAME flags in any way - a commit is
2626 * always TREESAME to its simplification.
2628 for (p = commit->parents; p; p = p->next) {
2629 pst = locate_simplify_state(revs, p->item);
2630 p->item = pst->simplified;
2631 if (revs->first_parent_only)
2632 break;
2635 if (revs->first_parent_only)
2636 cnt = 1;
2637 else
2638 cnt = remove_duplicate_parents(revs, commit);
2641 * It is possible that we are a merge and one side branch
2642 * does not have any commit that touches the given paths;
2643 * in such a case, the immediate parent from that branch
2644 * will be rewritten to be the merge base.
2646 * o----X X: the commit we are looking at;
2647 * / / o: a commit that touches the paths;
2648 * ---o----'
2650 * Further, a merge of an independent branch that doesn't
2651 * touch the path will reduce to a treesame root parent:
2653 * ----o----X X: the commit we are looking at;
2654 * / o: a commit that touches the paths;
2655 * r r: a root commit not touching the paths
2657 * Detect and simplify both cases.
2659 if (1 < cnt) {
2660 int marked = mark_redundant_parents(revs, commit);
2661 marked += mark_treesame_root_parents(revs, commit);
2662 if (marked)
2663 marked -= leave_one_treesame_to_parent(revs, commit);
2664 if (marked)
2665 cnt = remove_marked_parents(revs, commit);
2669 * A commit simplifies to itself if it is a root, if it is
2670 * UNINTERESTING, if it touches the given paths, or if it is a
2671 * merge and its parents don't simplify to one relevant commit
2672 * (the first two cases are already handled at the beginning of
2673 * this function).
2675 * Otherwise, it simplifies to what its sole relevant parent
2676 * simplifies to.
2678 if (!cnt ||
2679 (commit->object.flags & UNINTERESTING) ||
2680 !(commit->object.flags & TREESAME) ||
2681 (parent = one_relevant_parent(revs, commit->parents)) == NULL)
2682 st->simplified = commit;
2683 else {
2684 pst = locate_simplify_state(revs, parent);
2685 st->simplified = pst->simplified;
2687 return tail;
2690 static void simplify_merges(struct rev_info *revs)
2692 struct commit_list *list, *next;
2693 struct commit_list *yet_to_do, **tail;
2694 struct commit *commit;
2696 if (!revs->prune)
2697 return;
2699 /* feed the list reversed */
2700 yet_to_do = NULL;
2701 for (list = revs->commits; list; list = next) {
2702 commit = list->item;
2703 next = list->next;
2705 * Do not free(list) here yet; the original list
2706 * is used later in this function.
2708 commit_list_insert(commit, &yet_to_do);
2710 while (yet_to_do) {
2711 list = yet_to_do;
2712 yet_to_do = NULL;
2713 tail = &yet_to_do;
2714 while (list) {
2715 commit = pop_commit(&list);
2716 tail = simplify_one(revs, commit, tail);
2720 /* clean up the result, removing the simplified ones */
2721 list = revs->commits;
2722 revs->commits = NULL;
2723 tail = &revs->commits;
2724 while (list) {
2725 struct merge_simplify_state *st;
2727 commit = pop_commit(&list);
2728 st = locate_simplify_state(revs, commit);
2729 if (st->simplified == commit)
2730 tail = &commit_list_insert(commit, tail)->next;
2734 static void set_children(struct rev_info *revs)
2736 struct commit_list *l;
2737 for (l = revs->commits; l; l = l->next) {
2738 struct commit *commit = l->item;
2739 struct commit_list *p;
2741 for (p = commit->parents; p; p = p->next)
2742 add_child(revs, p->item, commit);
2746 void reset_revision_walk(void)
2748 clear_object_flags(SEEN | ADDED | SHOWN);
2751 int prepare_revision_walk(struct rev_info *revs)
2753 int i;
2754 struct object_array old_pending;
2755 struct commit_list **next = &revs->commits;
2757 memcpy(&old_pending, &revs->pending, sizeof(old_pending));
2758 revs->pending.nr = 0;
2759 revs->pending.alloc = 0;
2760 revs->pending.objects = NULL;
2761 for (i = 0; i < old_pending.nr; i++) {
2762 struct object_array_entry *e = old_pending.objects + i;
2763 struct commit *commit = handle_commit(revs, e);
2764 if (commit) {
2765 if (!(commit->object.flags & SEEN)) {
2766 commit->object.flags |= SEEN;
2767 next = commit_list_append(commit, next);
2771 if (!revs->leak_pending)
2772 object_array_clear(&old_pending);
2774 /* Signal whether we need per-parent treesame decoration */
2775 if (revs->simplify_merges ||
2776 (revs->limited && limiting_can_increase_treesame(revs)))
2777 revs->treesame.name = "treesame";
2779 if (revs->no_walk != REVISION_WALK_NO_WALK_UNSORTED)
2780 commit_list_sort_by_date(&revs->commits);
2781 if (revs->no_walk)
2782 return 0;
2783 if (revs->limited)
2784 if (limit_list(revs) < 0)
2785 return -1;
2786 if (revs->topo_order)
2787 sort_in_topological_order(&revs->commits, revs->sort_order);
2788 if (revs->line_level_traverse)
2789 line_log_filter(revs);
2790 if (revs->simplify_merges)
2791 simplify_merges(revs);
2792 if (revs->children.name)
2793 set_children(revs);
2794 return 0;
2797 static enum rewrite_result rewrite_one(struct rev_info *revs, struct commit **pp)
2799 struct commit_list *cache = NULL;
2801 for (;;) {
2802 struct commit *p = *pp;
2803 if (!revs->limited)
2804 if (add_parents_to_list(revs, p, &revs->commits, &cache) < 0)
2805 return rewrite_one_error;
2806 if (p->object.flags & UNINTERESTING)
2807 return rewrite_one_ok;
2808 if (!(p->object.flags & TREESAME))
2809 return rewrite_one_ok;
2810 if (!p->parents)
2811 return rewrite_one_noparents;
2812 if ((p = one_relevant_parent(revs, p->parents)) == NULL)
2813 return rewrite_one_ok;
2814 *pp = p;
2818 int rewrite_parents(struct rev_info *revs, struct commit *commit,
2819 rewrite_parent_fn_t rewrite_parent)
2821 struct commit_list **pp = &commit->parents;
2822 while (*pp) {
2823 struct commit_list *parent = *pp;
2824 switch (rewrite_parent(revs, &parent->item)) {
2825 case rewrite_one_ok:
2826 break;
2827 case rewrite_one_noparents:
2828 *pp = parent->next;
2829 continue;
2830 case rewrite_one_error:
2831 return -1;
2833 pp = &parent->next;
2835 remove_duplicate_parents(revs, commit);
2836 return 0;
2839 static int commit_rewrite_person(struct strbuf *buf, const char *what, struct string_list *mailmap)
2841 char *person, *endp;
2842 size_t len, namelen, maillen;
2843 const char *name;
2844 const char *mail;
2845 struct ident_split ident;
2847 person = strstr(buf->buf, what);
2848 if (!person)
2849 return 0;
2851 person += strlen(what);
2852 endp = strchr(person, '\n');
2853 if (!endp)
2854 return 0;
2856 len = endp - person;
2858 if (split_ident_line(&ident, person, len))
2859 return 0;
2861 mail = ident.mail_begin;
2862 maillen = ident.mail_end - ident.mail_begin;
2863 name = ident.name_begin;
2864 namelen = ident.name_end - ident.name_begin;
2866 if (map_user(mailmap, &mail, &maillen, &name, &namelen)) {
2867 struct strbuf namemail = STRBUF_INIT;
2869 strbuf_addf(&namemail, "%.*s <%.*s>",
2870 (int)namelen, name, (int)maillen, mail);
2872 strbuf_splice(buf, ident.name_begin - buf->buf,
2873 ident.mail_end - ident.name_begin + 1,
2874 namemail.buf, namemail.len);
2876 strbuf_release(&namemail);
2878 return 1;
2881 return 0;
2884 static int commit_match(struct commit *commit, struct rev_info *opt)
2886 int retval;
2887 const char *encoding;
2888 const char *message;
2889 struct strbuf buf = STRBUF_INIT;
2891 if (!opt->grep_filter.pattern_list && !opt->grep_filter.header_list)
2892 return 1;
2894 /* Prepend "fake" headers as needed */
2895 if (opt->grep_filter.use_reflog_filter) {
2896 strbuf_addstr(&buf, "reflog ");
2897 get_reflog_message(&buf, opt->reflog_info);
2898 strbuf_addch(&buf, '\n');
2902 * We grep in the user's output encoding, under the assumption that it
2903 * is the encoding they are most likely to write their grep pattern
2904 * for. In addition, it means we will match the "notes" encoding below,
2905 * so we will not end up with a buffer that has two different encodings
2906 * in it.
2908 encoding = get_log_output_encoding();
2909 message = logmsg_reencode(commit, NULL, encoding);
2911 /* Copy the commit to temporary if we are using "fake" headers */
2912 if (buf.len)
2913 strbuf_addstr(&buf, message);
2915 if (opt->grep_filter.header_list && opt->mailmap) {
2916 if (!buf.len)
2917 strbuf_addstr(&buf, message);
2919 commit_rewrite_person(&buf, "\nauthor ", opt->mailmap);
2920 commit_rewrite_person(&buf, "\ncommitter ", opt->mailmap);
2923 /* Append "fake" message parts as needed */
2924 if (opt->show_notes) {
2925 if (!buf.len)
2926 strbuf_addstr(&buf, message);
2927 format_display_notes(commit->object.sha1, &buf, encoding, 1);
2931 * Find either in the original commit message, or in the temporary.
2932 * Note that we cast away the constness of "message" here. It is
2933 * const because it may come from the cached commit buffer. That's OK,
2934 * because we know that it is modifiable heap memory, and that while
2935 * grep_buffer may modify it for speed, it will restore any
2936 * changes before returning.
2938 if (buf.len)
2939 retval = grep_buffer(&opt->grep_filter, buf.buf, buf.len);
2940 else
2941 retval = grep_buffer(&opt->grep_filter,
2942 (char *)message, strlen(message));
2943 strbuf_release(&buf);
2944 unuse_commit_buffer(commit, message);
2945 return opt->invert_grep ? !retval : retval;
2948 static inline int want_ancestry(const struct rev_info *revs)
2950 return (revs->rewrite_parents || revs->children.name);
2953 enum commit_action get_commit_action(struct rev_info *revs, struct commit *commit)
2955 if (commit->object.flags & SHOWN)
2956 return commit_ignore;
2957 if (revs->unpacked && has_sha1_pack(commit->object.sha1))
2958 return commit_ignore;
2959 if (revs->show_all)
2960 return commit_show;
2961 if (commit->object.flags & UNINTERESTING)
2962 return commit_ignore;
2963 if (revs->min_age != -1 && (commit->date > revs->min_age))
2964 return commit_ignore;
2965 if (revs->min_parents || (revs->max_parents >= 0)) {
2966 int n = commit_list_count(commit->parents);
2967 if ((n < revs->min_parents) ||
2968 ((revs->max_parents >= 0) && (n > revs->max_parents)))
2969 return commit_ignore;
2971 if (!commit_match(commit, revs))
2972 return commit_ignore;
2973 if (revs->prune && revs->dense) {
2974 /* Commit without changes? */
2975 if (commit->object.flags & TREESAME) {
2976 int n;
2977 struct commit_list *p;
2978 /* drop merges unless we want parenthood */
2979 if (!want_ancestry(revs))
2980 return commit_ignore;
2982 * If we want ancestry, then need to keep any merges
2983 * between relevant commits to tie together topology.
2984 * For consistency with TREESAME and simplification
2985 * use "relevant" here rather than just INTERESTING,
2986 * to treat bottom commit(s) as part of the topology.
2988 for (n = 0, p = commit->parents; p; p = p->next)
2989 if (relevant_commit(p->item))
2990 if (++n >= 2)
2991 return commit_show;
2992 return commit_ignore;
2995 return commit_show;
2998 define_commit_slab(saved_parents, struct commit_list *);
3000 #define EMPTY_PARENT_LIST ((struct commit_list *)-1)
3003 * You may only call save_parents() once per commit (this is checked
3004 * for non-root commits).
3006 static void save_parents(struct rev_info *revs, struct commit *commit)
3008 struct commit_list **pp;
3010 if (!revs->saved_parents_slab) {
3011 revs->saved_parents_slab = xmalloc(sizeof(struct saved_parents));
3012 init_saved_parents(revs->saved_parents_slab);
3015 pp = saved_parents_at(revs->saved_parents_slab, commit);
3018 * When walking with reflogs, we may visit the same commit
3019 * several times: once for each appearance in the reflog.
3021 * In this case, save_parents() will be called multiple times.
3022 * We want to keep only the first set of parents. We need to
3023 * store a sentinel value for an empty (i.e., NULL) parent
3024 * list to distinguish it from a not-yet-saved list, however.
3026 if (*pp)
3027 return;
3028 if (commit->parents)
3029 *pp = copy_commit_list(commit->parents);
3030 else
3031 *pp = EMPTY_PARENT_LIST;
3034 static void free_saved_parents(struct rev_info *revs)
3036 if (revs->saved_parents_slab)
3037 clear_saved_parents(revs->saved_parents_slab);
3040 struct commit_list *get_saved_parents(struct rev_info *revs, const struct commit *commit)
3042 struct commit_list *parents;
3044 if (!revs->saved_parents_slab)
3045 return commit->parents;
3047 parents = *saved_parents_at(revs->saved_parents_slab, commit);
3048 if (parents == EMPTY_PARENT_LIST)
3049 return NULL;
3050 return parents;
3053 enum commit_action simplify_commit(struct rev_info *revs, struct commit *commit)
3055 enum commit_action action = get_commit_action(revs, commit);
3057 if (action == commit_show &&
3058 !revs->show_all &&
3059 revs->prune && revs->dense && want_ancestry(revs)) {
3061 * --full-diff on simplified parents is no good: it
3062 * will show spurious changes from the commits that
3063 * were elided. So we save the parents on the side
3064 * when --full-diff is in effect.
3066 if (revs->full_diff)
3067 save_parents(revs, commit);
3068 if (rewrite_parents(revs, commit, rewrite_one) < 0)
3069 return commit_error;
3071 return action;
3074 static void track_linear(struct rev_info *revs, struct commit *commit)
3076 if (revs->track_first_time) {
3077 revs->linear = 1;
3078 revs->track_first_time = 0;
3079 } else {
3080 struct commit_list *p;
3081 for (p = revs->previous_parents; p; p = p->next)
3082 if (p->item == NULL || /* first commit */
3083 !hashcmp(p->item->object.sha1, commit->object.sha1))
3084 break;
3085 revs->linear = p != NULL;
3087 if (revs->reverse) {
3088 if (revs->linear)
3089 commit->object.flags |= TRACK_LINEAR;
3091 free_commit_list(revs->previous_parents);
3092 revs->previous_parents = copy_commit_list(commit->parents);
3095 static struct commit *get_revision_1(struct rev_info *revs)
3097 if (!revs->commits)
3098 return NULL;
3100 do {
3101 struct commit *commit = pop_commit(&revs->commits);
3103 if (revs->reflog_info) {
3104 save_parents(revs, commit);
3105 fake_reflog_parent(revs->reflog_info, commit);
3106 commit->object.flags &= ~(ADDED | SEEN | SHOWN);
3110 * If we haven't done the list limiting, we need to look at
3111 * the parents here. We also need to do the date-based limiting
3112 * that we'd otherwise have done in limit_list().
3114 if (!revs->limited) {
3115 if (revs->max_age != -1 &&
3116 (commit->date < revs->max_age))
3117 continue;
3118 if (add_parents_to_list(revs, commit, &revs->commits, NULL) < 0) {
3119 if (!revs->ignore_missing_links)
3120 die("Failed to traverse parents of commit %s",
3121 sha1_to_hex(commit->object.sha1));
3125 switch (simplify_commit(revs, commit)) {
3126 case commit_ignore:
3127 continue;
3128 case commit_error:
3129 die("Failed to simplify parents of commit %s",
3130 sha1_to_hex(commit->object.sha1));
3131 default:
3132 if (revs->track_linear)
3133 track_linear(revs, commit);
3134 return commit;
3136 } while (revs->commits);
3137 return NULL;
3141 * Return true for entries that have not yet been shown. (This is an
3142 * object_array_each_func_t.)
3144 static int entry_unshown(struct object_array_entry *entry, void *cb_data_unused)
3146 return !(entry->item->flags & SHOWN);
3150 * If array is on the verge of a realloc, garbage-collect any entries
3151 * that have already been shown to try to free up some space.
3153 static void gc_boundary(struct object_array *array)
3155 if (array->nr == array->alloc)
3156 object_array_filter(array, entry_unshown, NULL);
3159 static void create_boundary_commit_list(struct rev_info *revs)
3161 unsigned i;
3162 struct commit *c;
3163 struct object_array *array = &revs->boundary_commits;
3164 struct object_array_entry *objects = array->objects;
3167 * If revs->commits is non-NULL at this point, an error occurred in
3168 * get_revision_1(). Ignore the error and continue printing the
3169 * boundary commits anyway. (This is what the code has always
3170 * done.)
3172 if (revs->commits) {
3173 free_commit_list(revs->commits);
3174 revs->commits = NULL;
3178 * Put all of the actual boundary commits from revs->boundary_commits
3179 * into revs->commits
3181 for (i = 0; i < array->nr; i++) {
3182 c = (struct commit *)(objects[i].item);
3183 if (!c)
3184 continue;
3185 if (!(c->object.flags & CHILD_SHOWN))
3186 continue;
3187 if (c->object.flags & (SHOWN | BOUNDARY))
3188 continue;
3189 c->object.flags |= BOUNDARY;
3190 commit_list_insert(c, &revs->commits);
3194 * If revs->topo_order is set, sort the boundary commits
3195 * in topological order
3197 sort_in_topological_order(&revs->commits, revs->sort_order);
3200 static struct commit *get_revision_internal(struct rev_info *revs)
3202 struct commit *c = NULL;
3203 struct commit_list *l;
3205 if (revs->boundary == 2) {
3207 * All of the normal commits have already been returned,
3208 * and we are now returning boundary commits.
3209 * create_boundary_commit_list() has populated
3210 * revs->commits with the remaining commits to return.
3212 c = pop_commit(&revs->commits);
3213 if (c)
3214 c->object.flags |= SHOWN;
3215 return c;
3219 * If our max_count counter has reached zero, then we are done. We
3220 * don't simply return NULL because we still might need to show
3221 * boundary commits. But we want to avoid calling get_revision_1, which
3222 * might do a considerable amount of work finding the next commit only
3223 * for us to throw it away.
3225 * If it is non-zero, then either we don't have a max_count at all
3226 * (-1), or it is still counting, in which case we decrement.
3228 if (revs->max_count) {
3229 c = get_revision_1(revs);
3230 if (c) {
3231 while (revs->skip_count > 0) {
3232 revs->skip_count--;
3233 c = get_revision_1(revs);
3234 if (!c)
3235 break;
3239 if (revs->max_count > 0)
3240 revs->max_count--;
3243 if (c)
3244 c->object.flags |= SHOWN;
3246 if (!revs->boundary)
3247 return c;
3249 if (!c) {
3251 * get_revision_1() runs out the commits, and
3252 * we are done computing the boundaries.
3253 * switch to boundary commits output mode.
3255 revs->boundary = 2;
3258 * Update revs->commits to contain the list of
3259 * boundary commits.
3261 create_boundary_commit_list(revs);
3263 return get_revision_internal(revs);
3267 * boundary commits are the commits that are parents of the
3268 * ones we got from get_revision_1() but they themselves are
3269 * not returned from get_revision_1(). Before returning
3270 * 'c', we need to mark its parents that they could be boundaries.
3273 for (l = c->parents; l; l = l->next) {
3274 struct object *p;
3275 p = &(l->item->object);
3276 if (p->flags & (CHILD_SHOWN | SHOWN))
3277 continue;
3278 p->flags |= CHILD_SHOWN;
3279 gc_boundary(&revs->boundary_commits);
3280 add_object_array(p, NULL, &revs->boundary_commits);
3283 return c;
3286 struct commit *get_revision(struct rev_info *revs)
3288 struct commit *c;
3289 struct commit_list *reversed;
3291 if (revs->reverse) {
3292 reversed = NULL;
3293 while ((c = get_revision_internal(revs)))
3294 commit_list_insert(c, &reversed);
3295 revs->commits = reversed;
3296 revs->reverse = 0;
3297 revs->reverse_output_stage = 1;
3300 if (revs->reverse_output_stage) {
3301 c = pop_commit(&revs->commits);
3302 if (revs->track_linear)
3303 revs->linear = !!(c && c->object.flags & TRACK_LINEAR);
3304 return c;
3307 c = get_revision_internal(revs);
3308 if (c && revs->graph)
3309 graph_update(revs->graph, c);
3310 if (!c) {
3311 free_saved_parents(revs);
3312 if (revs->previous_parents) {
3313 free_commit_list(revs->previous_parents);
3314 revs->previous_parents = NULL;
3317 return c;
3320 char *get_revision_mark(const struct rev_info *revs, const struct commit *commit)
3322 if (commit->object.flags & BOUNDARY)
3323 return "-";
3324 else if (commit->object.flags & UNINTERESTING)
3325 return "^";
3326 else if (commit->object.flags & PATCHSAME)
3327 return "=";
3328 else if (!revs || revs->left_right) {
3329 if (commit->object.flags & SYMMETRIC_LEFT)
3330 return "<";
3331 else
3332 return ">";
3333 } else if (revs->graph)
3334 return "*";
3335 else if (revs->cherry_mark)
3336 return "+";
3337 return "";
3340 void put_revision_mark(const struct rev_info *revs, const struct commit *commit)
3342 char *mark = get_revision_mark(revs, commit);
3343 if (!strlen(mark))
3344 return;
3345 fputs(mark, stdout);
3346 putchar(' ');