show_object_with_name: simplify by using path_name()
[alt-git.git] / revision.c
blobcf544b6e9660ca53885b99396cd73e10179fcb28
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 void show_object_with_name(FILE *out, struct object *obj,
49 const struct name_path *path, const char *component)
51 char *name = path_name(path, component);
52 char *p;
54 fprintf(out, "%s ", sha1_to_hex(obj->sha1));
55 for (p = name; *p && *p != '\n'; p++)
56 fputc(*p, out);
57 fputc('\n', out);
59 free(name);
62 static void mark_blob_uninteresting(struct blob *blob)
64 if (!blob)
65 return;
66 if (blob->object.flags & UNINTERESTING)
67 return;
68 blob->object.flags |= UNINTERESTING;
71 static void mark_tree_contents_uninteresting(struct tree *tree)
73 struct tree_desc desc;
74 struct name_entry entry;
75 struct object *obj = &tree->object;
77 if (!has_sha1_file(obj->sha1))
78 return;
79 if (parse_tree(tree) < 0)
80 die("bad tree %s", sha1_to_hex(obj->sha1));
82 init_tree_desc(&desc, tree->buffer, tree->size);
83 while (tree_entry(&desc, &entry)) {
84 switch (object_type(entry.mode)) {
85 case OBJ_TREE:
86 mark_tree_uninteresting(lookup_tree(entry.sha1));
87 break;
88 case OBJ_BLOB:
89 mark_blob_uninteresting(lookup_blob(entry.sha1));
90 break;
91 default:
92 /* Subproject commit - not in this repository */
93 break;
98 * We don't care about the tree any more
99 * after it has been marked uninteresting.
101 free_tree_buffer(tree);
104 void mark_tree_uninteresting(struct tree *tree)
106 struct object *obj = &tree->object;
108 if (!tree)
109 return;
110 if (obj->flags & UNINTERESTING)
111 return;
112 obj->flags |= UNINTERESTING;
113 mark_tree_contents_uninteresting(tree);
116 void mark_parents_uninteresting(struct commit *commit)
118 struct commit_list *parents = NULL, *l;
120 for (l = commit->parents; l; l = l->next)
121 commit_list_insert(l->item, &parents);
123 while (parents) {
124 struct commit *commit = parents->item;
125 l = parents;
126 parents = parents->next;
127 free(l);
129 while (commit) {
131 * A missing commit is ok iff its parent is marked
132 * uninteresting.
134 * We just mark such a thing parsed, so that when
135 * it is popped next time around, we won't be trying
136 * to parse it and get an error.
138 if (!has_sha1_file(commit->object.sha1))
139 commit->object.parsed = 1;
141 if (commit->object.flags & UNINTERESTING)
142 break;
144 commit->object.flags |= UNINTERESTING;
147 * Normally we haven't parsed the parent
148 * yet, so we won't have a parent of a parent
149 * here. However, it may turn out that we've
150 * reached this commit some other way (where it
151 * wasn't uninteresting), in which case we need
152 * to mark its parents recursively too..
154 if (!commit->parents)
155 break;
157 for (l = commit->parents->next; l; l = l->next)
158 commit_list_insert(l->item, &parents);
159 commit = commit->parents->item;
164 static void add_pending_object_with_path(struct rev_info *revs,
165 struct object *obj,
166 const char *name, unsigned mode,
167 const char *path)
169 if (!obj)
170 return;
171 if (revs->no_walk && (obj->flags & UNINTERESTING))
172 revs->no_walk = 0;
173 if (revs->reflog_info && obj->type == OBJ_COMMIT) {
174 struct strbuf buf = STRBUF_INIT;
175 int len = interpret_branch_name(name, 0, &buf);
176 int st;
178 if (0 < len && name[len] && buf.len)
179 strbuf_addstr(&buf, name + len);
180 st = add_reflog_for_walk(revs->reflog_info,
181 (struct commit *)obj,
182 buf.buf[0] ? buf.buf: name);
183 strbuf_release(&buf);
184 if (st)
185 return;
187 add_object_array_with_path(obj, name, &revs->pending, mode, path);
190 static void add_pending_object_with_mode(struct rev_info *revs,
191 struct object *obj,
192 const char *name, unsigned mode)
194 add_pending_object_with_path(revs, obj, name, mode, NULL);
197 void add_pending_object(struct rev_info *revs,
198 struct object *obj, const char *name)
200 add_pending_object_with_mode(revs, obj, name, S_IFINVALID);
203 void add_head_to_pending(struct rev_info *revs)
205 unsigned char sha1[20];
206 struct object *obj;
207 if (get_sha1("HEAD", sha1))
208 return;
209 obj = parse_object(sha1);
210 if (!obj)
211 return;
212 add_pending_object(revs, obj, "HEAD");
215 static struct object *get_reference(struct rev_info *revs, const char *name,
216 const unsigned char *sha1,
217 unsigned int flags)
219 struct object *object;
221 object = parse_object(sha1);
222 if (!object) {
223 if (revs->ignore_missing)
224 return object;
225 die("bad object %s", name);
227 object->flags |= flags;
228 return object;
231 void add_pending_sha1(struct rev_info *revs, const char *name,
232 const unsigned char *sha1, unsigned int flags)
234 struct object *object = get_reference(revs, name, sha1, flags);
235 add_pending_object(revs, object, name);
238 static struct commit *handle_commit(struct rev_info *revs,
239 struct object_array_entry *entry)
241 struct object *object = entry->item;
242 const char *name = entry->name;
243 const char *path = entry->path;
244 unsigned int mode = entry->mode;
245 unsigned long flags = object->flags;
248 * Tag object? Look what it points to..
250 while (object->type == OBJ_TAG) {
251 struct tag *tag = (struct tag *) object;
252 if (revs->tag_objects && !(flags & UNINTERESTING))
253 add_pending_object(revs, object, tag->tag);
254 if (!tag->tagged)
255 die("bad tag");
256 object = parse_object(tag->tagged->sha1);
257 if (!object) {
258 if (flags & UNINTERESTING)
259 return NULL;
260 die("bad object %s", sha1_to_hex(tag->tagged->sha1));
262 object->flags |= flags;
264 * We'll handle the tagged object by looping or dropping
265 * through to the non-tag handlers below. Do not
266 * propagate data from the tag's pending entry.
268 name = "";
269 path = NULL;
270 mode = 0;
274 * Commit object? Just return it, we'll do all the complex
275 * reachability crud.
277 if (object->type == OBJ_COMMIT) {
278 struct commit *commit = (struct commit *)object;
279 if (parse_commit(commit) < 0)
280 die("unable to parse commit %s", name);
281 if (flags & UNINTERESTING) {
282 mark_parents_uninteresting(commit);
283 revs->limited = 1;
285 if (revs->show_source && !commit->util)
286 commit->util = xstrdup(name);
287 return commit;
291 * Tree object? Either mark it uninteresting, or add it
292 * to the list of objects to look at later..
294 if (object->type == OBJ_TREE) {
295 struct tree *tree = (struct tree *)object;
296 if (!revs->tree_objects)
297 return NULL;
298 if (flags & UNINTERESTING) {
299 mark_tree_contents_uninteresting(tree);
300 return NULL;
302 add_pending_object_with_path(revs, object, name, mode, path);
303 return NULL;
307 * Blob object? You know the drill by now..
309 if (object->type == OBJ_BLOB) {
310 if (!revs->blob_objects)
311 return NULL;
312 if (flags & UNINTERESTING)
313 return NULL;
314 add_pending_object_with_path(revs, object, name, mode, path);
315 return NULL;
317 die("%s is unknown object", name);
320 static int everybody_uninteresting(struct commit_list *orig,
321 struct commit **interesting_cache)
323 struct commit_list *list = orig;
325 if (*interesting_cache) {
326 struct commit *commit = *interesting_cache;
327 if (!(commit->object.flags & UNINTERESTING))
328 return 0;
331 while (list) {
332 struct commit *commit = list->item;
333 list = list->next;
334 if (commit->object.flags & UNINTERESTING)
335 continue;
336 if (interesting_cache)
337 *interesting_cache = commit;
338 return 0;
340 return 1;
344 * A definition of "relevant" commit that we can use to simplify limited graphs
345 * by eliminating side branches.
347 * A "relevant" commit is one that is !UNINTERESTING (ie we are including it
348 * in our list), or that is a specified BOTTOM commit. Then after computing
349 * a limited list, during processing we can generally ignore boundary merges
350 * coming from outside the graph, (ie from irrelevant parents), and treat
351 * those merges as if they were single-parent. TREESAME is defined to consider
352 * only relevant parents, if any. If we are TREESAME to our on-graph parents,
353 * we don't care if we were !TREESAME to non-graph parents.
355 * Treating bottom commits as relevant ensures that a limited graph's
356 * connection to the actual bottom commit is not viewed as a side branch, but
357 * treated as part of the graph. For example:
359 * ....Z...A---X---o---o---B
360 * . /
361 * W---Y
363 * When computing "A..B", the A-X connection is at least as important as
364 * Y-X, despite A being flagged UNINTERESTING.
366 * And when computing --ancestry-path "A..B", the A-X connection is more
367 * important than Y-X, despite both A and Y being flagged UNINTERESTING.
369 static inline int relevant_commit(struct commit *commit)
371 return (commit->object.flags & (UNINTERESTING | BOTTOM)) != UNINTERESTING;
375 * Return a single relevant commit from a parent list. If we are a TREESAME
376 * commit, and this selects one of our parents, then we can safely simplify to
377 * that parent.
379 static struct commit *one_relevant_parent(const struct rev_info *revs,
380 struct commit_list *orig)
382 struct commit_list *list = orig;
383 struct commit *relevant = NULL;
385 if (!orig)
386 return NULL;
389 * For 1-parent commits, or if first-parent-only, then return that
390 * first parent (even if not "relevant" by the above definition).
391 * TREESAME will have been set purely on that parent.
393 if (revs->first_parent_only || !orig->next)
394 return orig->item;
397 * For multi-parent commits, identify a sole relevant parent, if any.
398 * If we have only one relevant parent, then TREESAME will be set purely
399 * with regard to that parent, and we can simplify accordingly.
401 * If we have more than one relevant parent, or no relevant parents
402 * (and multiple irrelevant ones), then we can't select a parent here
403 * and return NULL.
405 while (list) {
406 struct commit *commit = list->item;
407 list = list->next;
408 if (relevant_commit(commit)) {
409 if (relevant)
410 return NULL;
411 relevant = commit;
414 return relevant;
418 * The goal is to get REV_TREE_NEW as the result only if the
419 * diff consists of all '+' (and no other changes), REV_TREE_OLD
420 * if the whole diff is removal of old data, and otherwise
421 * REV_TREE_DIFFERENT (of course if the trees are the same we
422 * want REV_TREE_SAME).
423 * That means that once we get to REV_TREE_DIFFERENT, we do not
424 * have to look any further.
426 static int tree_difference = REV_TREE_SAME;
428 static void file_add_remove(struct diff_options *options,
429 int addremove, unsigned mode,
430 const unsigned char *sha1,
431 int sha1_valid,
432 const char *fullpath, unsigned dirty_submodule)
434 int diff = addremove == '+' ? REV_TREE_NEW : REV_TREE_OLD;
436 tree_difference |= diff;
437 if (tree_difference == REV_TREE_DIFFERENT)
438 DIFF_OPT_SET(options, HAS_CHANGES);
441 static void file_change(struct diff_options *options,
442 unsigned old_mode, unsigned new_mode,
443 const unsigned char *old_sha1,
444 const unsigned char *new_sha1,
445 int old_sha1_valid, int new_sha1_valid,
446 const char *fullpath,
447 unsigned old_dirty_submodule, unsigned new_dirty_submodule)
449 tree_difference = REV_TREE_DIFFERENT;
450 DIFF_OPT_SET(options, HAS_CHANGES);
453 static int rev_compare_tree(struct rev_info *revs,
454 struct commit *parent, struct commit *commit)
456 struct tree *t1 = parent->tree;
457 struct tree *t2 = commit->tree;
459 if (!t1)
460 return REV_TREE_NEW;
461 if (!t2)
462 return REV_TREE_OLD;
464 if (revs->simplify_by_decoration) {
466 * If we are simplifying by decoration, then the commit
467 * is worth showing if it has a tag pointing at it.
469 if (get_name_decoration(&commit->object))
470 return REV_TREE_DIFFERENT;
472 * A commit that is not pointed by a tag is uninteresting
473 * if we are not limited by path. This means that you will
474 * see the usual "commits that touch the paths" plus any
475 * tagged commit by specifying both --simplify-by-decoration
476 * and pathspec.
478 if (!revs->prune_data.nr)
479 return REV_TREE_SAME;
482 tree_difference = REV_TREE_SAME;
483 DIFF_OPT_CLR(&revs->pruning, HAS_CHANGES);
484 if (diff_tree_sha1(t1->object.sha1, t2->object.sha1, "",
485 &revs->pruning) < 0)
486 return REV_TREE_DIFFERENT;
487 return tree_difference;
490 static int rev_same_tree_as_empty(struct rev_info *revs, struct commit *commit)
492 int retval;
493 struct tree *t1 = commit->tree;
495 if (!t1)
496 return 0;
498 tree_difference = REV_TREE_SAME;
499 DIFF_OPT_CLR(&revs->pruning, HAS_CHANGES);
500 retval = diff_tree_sha1(NULL, t1->object.sha1, "", &revs->pruning);
502 return retval >= 0 && (tree_difference == REV_TREE_SAME);
505 struct treesame_state {
506 unsigned int nparents;
507 unsigned char treesame[FLEX_ARRAY];
510 static struct treesame_state *initialise_treesame(struct rev_info *revs, struct commit *commit)
512 unsigned n = commit_list_count(commit->parents);
513 struct treesame_state *st = xcalloc(1, sizeof(*st) + n);
514 st->nparents = n;
515 add_decoration(&revs->treesame, &commit->object, st);
516 return st;
520 * Must be called immediately after removing the nth_parent from a commit's
521 * parent list, if we are maintaining the per-parent treesame[] decoration.
522 * This does not recalculate the master TREESAME flag - update_treesame()
523 * should be called to update it after a sequence of treesame[] modifications
524 * that may have affected it.
526 static int compact_treesame(struct rev_info *revs, struct commit *commit, unsigned nth_parent)
528 struct treesame_state *st;
529 int old_same;
531 if (!commit->parents) {
533 * Have just removed the only parent from a non-merge.
534 * Different handling, as we lack decoration.
536 if (nth_parent != 0)
537 die("compact_treesame %u", nth_parent);
538 old_same = !!(commit->object.flags & TREESAME);
539 if (rev_same_tree_as_empty(revs, commit))
540 commit->object.flags |= TREESAME;
541 else
542 commit->object.flags &= ~TREESAME;
543 return old_same;
546 st = lookup_decoration(&revs->treesame, &commit->object);
547 if (!st || nth_parent >= st->nparents)
548 die("compact_treesame %u", nth_parent);
550 old_same = st->treesame[nth_parent];
551 memmove(st->treesame + nth_parent,
552 st->treesame + nth_parent + 1,
553 st->nparents - nth_parent - 1);
556 * If we've just become a non-merge commit, update TREESAME
557 * immediately, and remove the no-longer-needed decoration.
558 * If still a merge, defer update until update_treesame().
560 if (--st->nparents == 1) {
561 if (commit->parents->next)
562 die("compact_treesame parents mismatch");
563 if (st->treesame[0] && revs->dense)
564 commit->object.flags |= TREESAME;
565 else
566 commit->object.flags &= ~TREESAME;
567 free(add_decoration(&revs->treesame, &commit->object, NULL));
570 return old_same;
573 static unsigned update_treesame(struct rev_info *revs, struct commit *commit)
575 if (commit->parents && commit->parents->next) {
576 unsigned n;
577 struct treesame_state *st;
578 struct commit_list *p;
579 unsigned relevant_parents;
580 unsigned relevant_change, irrelevant_change;
582 st = lookup_decoration(&revs->treesame, &commit->object);
583 if (!st)
584 die("update_treesame %s", sha1_to_hex(commit->object.sha1));
585 relevant_parents = 0;
586 relevant_change = irrelevant_change = 0;
587 for (p = commit->parents, n = 0; p; n++, p = p->next) {
588 if (relevant_commit(p->item)) {
589 relevant_change |= !st->treesame[n];
590 relevant_parents++;
591 } else
592 irrelevant_change |= !st->treesame[n];
594 if (relevant_parents ? relevant_change : irrelevant_change)
595 commit->object.flags &= ~TREESAME;
596 else
597 commit->object.flags |= TREESAME;
600 return commit->object.flags & TREESAME;
603 static inline int limiting_can_increase_treesame(const struct rev_info *revs)
606 * TREESAME is irrelevant unless prune && dense;
607 * if simplify_history is set, we can't have a mixture of TREESAME and
608 * !TREESAME INTERESTING parents (and we don't have treesame[]
609 * decoration anyway);
610 * if first_parent_only is set, then the TREESAME flag is locked
611 * against the first parent (and again we lack treesame[] decoration).
613 return revs->prune && revs->dense &&
614 !revs->simplify_history &&
615 !revs->first_parent_only;
618 static void try_to_simplify_commit(struct rev_info *revs, struct commit *commit)
620 struct commit_list **pp, *parent;
621 struct treesame_state *ts = NULL;
622 int relevant_change = 0, irrelevant_change = 0;
623 int relevant_parents, nth_parent;
626 * If we don't do pruning, everything is interesting
628 if (!revs->prune)
629 return;
631 if (!commit->tree)
632 return;
634 if (!commit->parents) {
635 if (rev_same_tree_as_empty(revs, commit))
636 commit->object.flags |= TREESAME;
637 return;
641 * Normal non-merge commit? If we don't want to make the
642 * history dense, we consider it always to be a change..
644 if (!revs->dense && !commit->parents->next)
645 return;
647 for (pp = &commit->parents, nth_parent = 0, relevant_parents = 0;
648 (parent = *pp) != NULL;
649 pp = &parent->next, nth_parent++) {
650 struct commit *p = parent->item;
651 if (relevant_commit(p))
652 relevant_parents++;
654 if (nth_parent == 1) {
656 * This our second loop iteration - so we now know
657 * we're dealing with a merge.
659 * Do not compare with later parents when we care only about
660 * the first parent chain, in order to avoid derailing the
661 * traversal to follow a side branch that brought everything
662 * in the path we are limited to by the pathspec.
664 if (revs->first_parent_only)
665 break;
667 * If this will remain a potentially-simplifiable
668 * merge, remember per-parent treesame if needed.
669 * Initialise the array with the comparison from our
670 * first iteration.
672 if (revs->treesame.name &&
673 !revs->simplify_history &&
674 !(commit->object.flags & UNINTERESTING)) {
675 ts = initialise_treesame(revs, commit);
676 if (!(irrelevant_change || relevant_change))
677 ts->treesame[0] = 1;
680 if (parse_commit(p) < 0)
681 die("cannot simplify commit %s (because of %s)",
682 sha1_to_hex(commit->object.sha1),
683 sha1_to_hex(p->object.sha1));
684 switch (rev_compare_tree(revs, p, commit)) {
685 case REV_TREE_SAME:
686 if (!revs->simplify_history || !relevant_commit(p)) {
687 /* Even if a merge with an uninteresting
688 * side branch brought the entire change
689 * we are interested in, we do not want
690 * to lose the other branches of this
691 * merge, so we just keep going.
693 if (ts)
694 ts->treesame[nth_parent] = 1;
695 continue;
697 parent->next = NULL;
698 commit->parents = parent;
699 commit->object.flags |= TREESAME;
700 return;
702 case REV_TREE_NEW:
703 if (revs->remove_empty_trees &&
704 rev_same_tree_as_empty(revs, p)) {
705 /* We are adding all the specified
706 * paths from this parent, so the
707 * history beyond this parent is not
708 * interesting. Remove its parents
709 * (they are grandparents for us).
710 * IOW, we pretend this parent is a
711 * "root" commit.
713 if (parse_commit(p) < 0)
714 die("cannot simplify commit %s (invalid %s)",
715 sha1_to_hex(commit->object.sha1),
716 sha1_to_hex(p->object.sha1));
717 p->parents = NULL;
719 /* fallthrough */
720 case REV_TREE_OLD:
721 case REV_TREE_DIFFERENT:
722 if (relevant_commit(p))
723 relevant_change = 1;
724 else
725 irrelevant_change = 1;
726 continue;
728 die("bad tree compare for commit %s", sha1_to_hex(commit->object.sha1));
732 * TREESAME is straightforward for single-parent commits. For merge
733 * commits, it is most useful to define it so that "irrelevant"
734 * parents cannot make us !TREESAME - if we have any relevant
735 * parents, then we only consider TREESAMEness with respect to them,
736 * allowing irrelevant merges from uninteresting branches to be
737 * simplified away. Only if we have only irrelevant parents do we
738 * base TREESAME on them. Note that this logic is replicated in
739 * update_treesame, which should be kept in sync.
741 if (relevant_parents ? !relevant_change : !irrelevant_change)
742 commit->object.flags |= TREESAME;
745 static void commit_list_insert_by_date_cached(struct commit *p, struct commit_list **head,
746 struct commit_list *cached_base, struct commit_list **cache)
748 struct commit_list *new_entry;
750 if (cached_base && p->date < cached_base->item->date)
751 new_entry = commit_list_insert_by_date(p, &cached_base->next);
752 else
753 new_entry = commit_list_insert_by_date(p, head);
755 if (cache && (!*cache || p->date < (*cache)->item->date))
756 *cache = new_entry;
759 static int add_parents_to_list(struct rev_info *revs, struct commit *commit,
760 struct commit_list **list, struct commit_list **cache_ptr)
762 struct commit_list *parent = commit->parents;
763 unsigned left_flag;
764 struct commit_list *cached_base = cache_ptr ? *cache_ptr : NULL;
766 if (commit->object.flags & ADDED)
767 return 0;
768 commit->object.flags |= ADDED;
770 if (revs->include_check &&
771 !revs->include_check(commit, revs->include_check_data))
772 return 0;
775 * If the commit is uninteresting, don't try to
776 * prune parents - we want the maximal uninteresting
777 * set.
779 * Normally we haven't parsed the parent
780 * yet, so we won't have a parent of a parent
781 * here. However, it may turn out that we've
782 * reached this commit some other way (where it
783 * wasn't uninteresting), in which case we need
784 * to mark its parents recursively too..
786 if (commit->object.flags & UNINTERESTING) {
787 while (parent) {
788 struct commit *p = parent->item;
789 parent = parent->next;
790 if (p)
791 p->object.flags |= UNINTERESTING;
792 if (parse_commit_gently(p, 1) < 0)
793 continue;
794 if (p->parents)
795 mark_parents_uninteresting(p);
796 if (p->object.flags & SEEN)
797 continue;
798 p->object.flags |= SEEN;
799 commit_list_insert_by_date_cached(p, list, cached_base, cache_ptr);
801 return 0;
805 * Ok, the commit wasn't uninteresting. Try to
806 * simplify the commit history and find the parent
807 * that has no differences in the path set if one exists.
809 try_to_simplify_commit(revs, commit);
811 if (revs->no_walk)
812 return 0;
814 left_flag = (commit->object.flags & SYMMETRIC_LEFT);
816 for (parent = commit->parents; parent; parent = parent->next) {
817 struct commit *p = parent->item;
819 if (parse_commit_gently(p, revs->ignore_missing_links) < 0)
820 return -1;
821 if (revs->show_source && !p->util)
822 p->util = commit->util;
823 p->object.flags |= left_flag;
824 if (!(p->object.flags & SEEN)) {
825 p->object.flags |= SEEN;
826 commit_list_insert_by_date_cached(p, list, cached_base, cache_ptr);
828 if (revs->first_parent_only)
829 break;
831 return 0;
834 static void cherry_pick_list(struct commit_list *list, struct rev_info *revs)
836 struct commit_list *p;
837 int left_count = 0, right_count = 0;
838 int left_first;
839 struct patch_ids ids;
840 unsigned cherry_flag;
842 /* First count the commits on the left and on the right */
843 for (p = list; p; p = p->next) {
844 struct commit *commit = p->item;
845 unsigned flags = commit->object.flags;
846 if (flags & BOUNDARY)
848 else if (flags & SYMMETRIC_LEFT)
849 left_count++;
850 else
851 right_count++;
854 if (!left_count || !right_count)
855 return;
857 left_first = left_count < right_count;
858 init_patch_ids(&ids);
859 ids.diffopts.pathspec = revs->diffopt.pathspec;
861 /* Compute patch-ids for one side */
862 for (p = list; p; p = p->next) {
863 struct commit *commit = p->item;
864 unsigned flags = commit->object.flags;
866 if (flags & BOUNDARY)
867 continue;
869 * If we have fewer left, left_first is set and we omit
870 * commits on the right branch in this loop. If we have
871 * fewer right, we skip the left ones.
873 if (left_first != !!(flags & SYMMETRIC_LEFT))
874 continue;
875 commit->util = add_commit_patch_id(commit, &ids);
878 /* either cherry_mark or cherry_pick are true */
879 cherry_flag = revs->cherry_mark ? PATCHSAME : SHOWN;
881 /* Check the other side */
882 for (p = list; p; p = p->next) {
883 struct commit *commit = p->item;
884 struct patch_id *id;
885 unsigned flags = commit->object.flags;
887 if (flags & BOUNDARY)
888 continue;
890 * If we have fewer left, left_first is set and we omit
891 * commits on the left branch in this loop.
893 if (left_first == !!(flags & SYMMETRIC_LEFT))
894 continue;
897 * Have we seen the same patch id?
899 id = has_commit_patch_id(commit, &ids);
900 if (!id)
901 continue;
902 id->seen = 1;
903 commit->object.flags |= cherry_flag;
906 /* Now check the original side for seen ones */
907 for (p = list; p; p = p->next) {
908 struct commit *commit = p->item;
909 struct patch_id *ent;
911 ent = commit->util;
912 if (!ent)
913 continue;
914 if (ent->seen)
915 commit->object.flags |= cherry_flag;
916 commit->util = NULL;
919 free_patch_ids(&ids);
922 /* How many extra uninteresting commits we want to see.. */
923 #define SLOP 5
925 static int still_interesting(struct commit_list *src, unsigned long date, int slop,
926 struct commit **interesting_cache)
929 * No source list at all? We're definitely done..
931 if (!src)
932 return 0;
935 * Does the destination list contain entries with a date
936 * before the source list? Definitely _not_ done.
938 if (date <= src->item->date)
939 return SLOP;
942 * Does the source list still have interesting commits in
943 * it? Definitely not done..
945 if (!everybody_uninteresting(src, interesting_cache))
946 return SLOP;
948 /* Ok, we're closing in.. */
949 return slop-1;
953 * "rev-list --ancestry-path A..B" computes commits that are ancestors
954 * of B but not ancestors of A but further limits the result to those
955 * that are descendants of A. This takes the list of bottom commits and
956 * the result of "A..B" without --ancestry-path, and limits the latter
957 * further to the ones that can reach one of the commits in "bottom".
959 static void limit_to_ancestry(struct commit_list *bottom, struct commit_list *list)
961 struct commit_list *p;
962 struct commit_list *rlist = NULL;
963 int made_progress;
966 * Reverse the list so that it will be likely that we would
967 * process parents before children.
969 for (p = list; p; p = p->next)
970 commit_list_insert(p->item, &rlist);
972 for (p = bottom; p; p = p->next)
973 p->item->object.flags |= TMP_MARK;
976 * Mark the ones that can reach bottom commits in "list",
977 * in a bottom-up fashion.
979 do {
980 made_progress = 0;
981 for (p = rlist; p; p = p->next) {
982 struct commit *c = p->item;
983 struct commit_list *parents;
984 if (c->object.flags & (TMP_MARK | UNINTERESTING))
985 continue;
986 for (parents = c->parents;
987 parents;
988 parents = parents->next) {
989 if (!(parents->item->object.flags & TMP_MARK))
990 continue;
991 c->object.flags |= TMP_MARK;
992 made_progress = 1;
993 break;
996 } while (made_progress);
999 * NEEDSWORK: decide if we want to remove parents that are
1000 * not marked with TMP_MARK from commit->parents for commits
1001 * in the resulting list. We may not want to do that, though.
1005 * The ones that are not marked with TMP_MARK are uninteresting
1007 for (p = list; p; p = p->next) {
1008 struct commit *c = p->item;
1009 if (c->object.flags & TMP_MARK)
1010 continue;
1011 c->object.flags |= UNINTERESTING;
1014 /* We are done with the TMP_MARK */
1015 for (p = list; p; p = p->next)
1016 p->item->object.flags &= ~TMP_MARK;
1017 for (p = bottom; p; p = p->next)
1018 p->item->object.flags &= ~TMP_MARK;
1019 free_commit_list(rlist);
1023 * Before walking the history, keep the set of "negative" refs the
1024 * caller has asked to exclude.
1026 * This is used to compute "rev-list --ancestry-path A..B", as we need
1027 * to filter the result of "A..B" further to the ones that can actually
1028 * reach A.
1030 static struct commit_list *collect_bottom_commits(struct commit_list *list)
1032 struct commit_list *elem, *bottom = NULL;
1033 for (elem = list; elem; elem = elem->next)
1034 if (elem->item->object.flags & BOTTOM)
1035 commit_list_insert(elem->item, &bottom);
1036 return bottom;
1039 /* Assumes either left_only or right_only is set */
1040 static void limit_left_right(struct commit_list *list, struct rev_info *revs)
1042 struct commit_list *p;
1044 for (p = list; p; p = p->next) {
1045 struct commit *commit = p->item;
1047 if (revs->right_only) {
1048 if (commit->object.flags & SYMMETRIC_LEFT)
1049 commit->object.flags |= SHOWN;
1050 } else /* revs->left_only is set */
1051 if (!(commit->object.flags & SYMMETRIC_LEFT))
1052 commit->object.flags |= SHOWN;
1056 static int limit_list(struct rev_info *revs)
1058 int slop = SLOP;
1059 unsigned long date = ~0ul;
1060 struct commit_list *list = revs->commits;
1061 struct commit_list *newlist = NULL;
1062 struct commit_list **p = &newlist;
1063 struct commit_list *bottom = NULL;
1064 struct commit *interesting_cache = NULL;
1066 if (revs->ancestry_path) {
1067 bottom = collect_bottom_commits(list);
1068 if (!bottom)
1069 die("--ancestry-path given but there are no bottom commits");
1072 while (list) {
1073 struct commit_list *entry = list;
1074 struct commit *commit = list->item;
1075 struct object *obj = &commit->object;
1076 show_early_output_fn_t show;
1078 list = list->next;
1079 free(entry);
1081 if (commit == interesting_cache)
1082 interesting_cache = NULL;
1084 if (revs->max_age != -1 && (commit->date < revs->max_age))
1085 obj->flags |= UNINTERESTING;
1086 if (add_parents_to_list(revs, commit, &list, NULL) < 0)
1087 return -1;
1088 if (obj->flags & UNINTERESTING) {
1089 mark_parents_uninteresting(commit);
1090 if (revs->show_all)
1091 p = &commit_list_insert(commit, p)->next;
1092 slop = still_interesting(list, date, slop, &interesting_cache);
1093 if (slop)
1094 continue;
1095 /* If showing all, add the whole pending list to the end */
1096 if (revs->show_all)
1097 *p = list;
1098 break;
1100 if (revs->min_age != -1 && (commit->date > revs->min_age))
1101 continue;
1102 date = commit->date;
1103 p = &commit_list_insert(commit, p)->next;
1105 show = show_early_output;
1106 if (!show)
1107 continue;
1109 show(revs, newlist);
1110 show_early_output = NULL;
1112 if (revs->cherry_pick || revs->cherry_mark)
1113 cherry_pick_list(newlist, revs);
1115 if (revs->left_only || revs->right_only)
1116 limit_left_right(newlist, revs);
1118 if (bottom) {
1119 limit_to_ancestry(bottom, newlist);
1120 free_commit_list(bottom);
1124 * Check if any commits have become TREESAME by some of their parents
1125 * becoming UNINTERESTING.
1127 if (limiting_can_increase_treesame(revs))
1128 for (list = newlist; list; list = list->next) {
1129 struct commit *c = list->item;
1130 if (c->object.flags & (UNINTERESTING | TREESAME))
1131 continue;
1132 update_treesame(revs, c);
1135 revs->commits = newlist;
1136 return 0;
1140 * Add an entry to refs->cmdline with the specified information.
1141 * *name is copied.
1143 static void add_rev_cmdline(struct rev_info *revs,
1144 struct object *item,
1145 const char *name,
1146 int whence,
1147 unsigned flags)
1149 struct rev_cmdline_info *info = &revs->cmdline;
1150 int nr = info->nr;
1152 ALLOC_GROW(info->rev, nr + 1, info->alloc);
1153 info->rev[nr].item = item;
1154 info->rev[nr].name = xstrdup(name);
1155 info->rev[nr].whence = whence;
1156 info->rev[nr].flags = flags;
1157 info->nr++;
1160 static void add_rev_cmdline_list(struct rev_info *revs,
1161 struct commit_list *commit_list,
1162 int whence,
1163 unsigned flags)
1165 while (commit_list) {
1166 struct object *object = &commit_list->item->object;
1167 add_rev_cmdline(revs, object, sha1_to_hex(object->sha1),
1168 whence, flags);
1169 commit_list = commit_list->next;
1173 struct all_refs_cb {
1174 int all_flags;
1175 int warned_bad_reflog;
1176 struct rev_info *all_revs;
1177 const char *name_for_errormsg;
1180 int ref_excluded(struct string_list *ref_excludes, const char *path)
1182 struct string_list_item *item;
1184 if (!ref_excludes)
1185 return 0;
1186 for_each_string_list_item(item, ref_excludes) {
1187 if (!wildmatch(item->string, path, 0, NULL))
1188 return 1;
1190 return 0;
1193 static int handle_one_ref(const char *path, const unsigned char *sha1, int flag, void *cb_data)
1195 struct all_refs_cb *cb = cb_data;
1196 struct object *object;
1198 if (ref_excluded(cb->all_revs->ref_excludes, path))
1199 return 0;
1201 object = get_reference(cb->all_revs, path, sha1, cb->all_flags);
1202 add_rev_cmdline(cb->all_revs, object, path, REV_CMD_REF, cb->all_flags);
1203 add_pending_sha1(cb->all_revs, path, sha1, cb->all_flags);
1204 return 0;
1207 static void init_all_refs_cb(struct all_refs_cb *cb, struct rev_info *revs,
1208 unsigned flags)
1210 cb->all_revs = revs;
1211 cb->all_flags = flags;
1214 void clear_ref_exclusion(struct string_list **ref_excludes_p)
1216 if (*ref_excludes_p) {
1217 string_list_clear(*ref_excludes_p, 0);
1218 free(*ref_excludes_p);
1220 *ref_excludes_p = NULL;
1223 void add_ref_exclusion(struct string_list **ref_excludes_p, const char *exclude)
1225 if (!*ref_excludes_p) {
1226 *ref_excludes_p = xcalloc(1, sizeof(**ref_excludes_p));
1227 (*ref_excludes_p)->strdup_strings = 1;
1229 string_list_append(*ref_excludes_p, exclude);
1232 static void handle_refs(const char *submodule, struct rev_info *revs, unsigned flags,
1233 int (*for_each)(const char *, each_ref_fn, void *))
1235 struct all_refs_cb cb;
1236 init_all_refs_cb(&cb, revs, flags);
1237 for_each(submodule, handle_one_ref, &cb);
1240 static void handle_one_reflog_commit(unsigned char *sha1, void *cb_data)
1242 struct all_refs_cb *cb = cb_data;
1243 if (!is_null_sha1(sha1)) {
1244 struct object *o = parse_object(sha1);
1245 if (o) {
1246 o->flags |= cb->all_flags;
1247 /* ??? CMDLINEFLAGS ??? */
1248 add_pending_object(cb->all_revs, o, "");
1250 else if (!cb->warned_bad_reflog) {
1251 warning("reflog of '%s' references pruned commits",
1252 cb->name_for_errormsg);
1253 cb->warned_bad_reflog = 1;
1258 static int handle_one_reflog_ent(unsigned char *osha1, unsigned char *nsha1,
1259 const char *email, unsigned long timestamp, int tz,
1260 const char *message, void *cb_data)
1262 handle_one_reflog_commit(osha1, cb_data);
1263 handle_one_reflog_commit(nsha1, cb_data);
1264 return 0;
1267 static int handle_one_reflog(const char *path, const unsigned char *sha1, int flag, void *cb_data)
1269 struct all_refs_cb *cb = cb_data;
1270 cb->warned_bad_reflog = 0;
1271 cb->name_for_errormsg = path;
1272 for_each_reflog_ent(path, handle_one_reflog_ent, cb_data);
1273 return 0;
1276 void add_reflogs_to_pending(struct rev_info *revs, unsigned flags)
1278 struct all_refs_cb cb;
1279 cb.all_revs = revs;
1280 cb.all_flags = flags;
1281 for_each_reflog(handle_one_reflog, &cb);
1284 static void add_cache_tree(struct cache_tree *it, struct rev_info *revs,
1285 struct strbuf *path)
1287 size_t baselen = path->len;
1288 int i;
1290 if (it->entry_count >= 0) {
1291 struct tree *tree = lookup_tree(it->sha1);
1292 add_pending_object_with_path(revs, &tree->object, "",
1293 040000, path->buf);
1296 for (i = 0; i < it->subtree_nr; i++) {
1297 struct cache_tree_sub *sub = it->down[i];
1298 strbuf_addf(path, "%s%s", baselen ? "/" : "", sub->name);
1299 add_cache_tree(sub->cache_tree, revs, path);
1300 strbuf_setlen(path, baselen);
1305 void add_index_objects_to_pending(struct rev_info *revs, unsigned flags)
1307 int i;
1309 read_cache();
1310 for (i = 0; i < active_nr; i++) {
1311 struct cache_entry *ce = active_cache[i];
1312 struct blob *blob;
1314 if (S_ISGITLINK(ce->ce_mode))
1315 continue;
1317 blob = lookup_blob(ce->sha1);
1318 if (!blob)
1319 die("unable to add index blob to traversal");
1320 add_pending_object_with_path(revs, &blob->object, "",
1321 ce->ce_mode, ce->name);
1324 if (active_cache_tree) {
1325 struct strbuf path = STRBUF_INIT;
1326 add_cache_tree(active_cache_tree, revs, &path);
1327 strbuf_release(&path);
1331 static int add_parents_only(struct rev_info *revs, const char *arg_, int flags)
1333 unsigned char sha1[20];
1334 struct object *it;
1335 struct commit *commit;
1336 struct commit_list *parents;
1337 const char *arg = arg_;
1339 if (*arg == '^') {
1340 flags ^= UNINTERESTING | BOTTOM;
1341 arg++;
1343 if (get_sha1_committish(arg, sha1))
1344 return 0;
1345 while (1) {
1346 it = get_reference(revs, arg, sha1, 0);
1347 if (!it && revs->ignore_missing)
1348 return 0;
1349 if (it->type != OBJ_TAG)
1350 break;
1351 if (!((struct tag*)it)->tagged)
1352 return 0;
1353 hashcpy(sha1, ((struct tag*)it)->tagged->sha1);
1355 if (it->type != OBJ_COMMIT)
1356 return 0;
1357 commit = (struct commit *)it;
1358 for (parents = commit->parents; parents; parents = parents->next) {
1359 it = &parents->item->object;
1360 it->flags |= flags;
1361 add_rev_cmdline(revs, it, arg_, REV_CMD_PARENTS_ONLY, flags);
1362 add_pending_object(revs, it, arg);
1364 return 1;
1367 void init_revisions(struct rev_info *revs, const char *prefix)
1369 memset(revs, 0, sizeof(*revs));
1371 revs->abbrev = DEFAULT_ABBREV;
1372 revs->ignore_merges = 1;
1373 revs->simplify_history = 1;
1374 DIFF_OPT_SET(&revs->pruning, RECURSIVE);
1375 DIFF_OPT_SET(&revs->pruning, QUICK);
1376 revs->pruning.add_remove = file_add_remove;
1377 revs->pruning.change = file_change;
1378 revs->sort_order = REV_SORT_IN_GRAPH_ORDER;
1379 revs->dense = 1;
1380 revs->prefix = prefix;
1381 revs->max_age = -1;
1382 revs->min_age = -1;
1383 revs->skip_count = -1;
1384 revs->max_count = -1;
1385 revs->max_parents = -1;
1387 revs->commit_format = CMIT_FMT_DEFAULT;
1389 init_grep_defaults();
1390 grep_init(&revs->grep_filter, prefix);
1391 revs->grep_filter.status_only = 1;
1392 revs->grep_filter.regflags = REG_NEWLINE;
1394 diff_setup(&revs->diffopt);
1395 if (prefix && !revs->diffopt.prefix) {
1396 revs->diffopt.prefix = prefix;
1397 revs->diffopt.prefix_length = strlen(prefix);
1400 revs->notes_opt.use_default_notes = -1;
1403 static void add_pending_commit_list(struct rev_info *revs,
1404 struct commit_list *commit_list,
1405 unsigned int flags)
1407 while (commit_list) {
1408 struct object *object = &commit_list->item->object;
1409 object->flags |= flags;
1410 add_pending_object(revs, object, sha1_to_hex(object->sha1));
1411 commit_list = commit_list->next;
1415 static void prepare_show_merge(struct rev_info *revs)
1417 struct commit_list *bases;
1418 struct commit *head, *other;
1419 unsigned char sha1[20];
1420 const char **prune = NULL;
1421 int i, prune_num = 1; /* counting terminating NULL */
1423 if (get_sha1("HEAD", sha1))
1424 die("--merge without HEAD?");
1425 head = lookup_commit_or_die(sha1, "HEAD");
1426 if (get_sha1("MERGE_HEAD", sha1))
1427 die("--merge without MERGE_HEAD?");
1428 other = lookup_commit_or_die(sha1, "MERGE_HEAD");
1429 add_pending_object(revs, &head->object, "HEAD");
1430 add_pending_object(revs, &other->object, "MERGE_HEAD");
1431 bases = get_merge_bases(head, other);
1432 add_rev_cmdline_list(revs, bases, REV_CMD_MERGE_BASE, UNINTERESTING | BOTTOM);
1433 add_pending_commit_list(revs, bases, UNINTERESTING | BOTTOM);
1434 free_commit_list(bases);
1435 head->object.flags |= SYMMETRIC_LEFT;
1437 if (!active_nr)
1438 read_cache();
1439 for (i = 0; i < active_nr; i++) {
1440 const struct cache_entry *ce = active_cache[i];
1441 if (!ce_stage(ce))
1442 continue;
1443 if (ce_path_match(ce, &revs->prune_data, NULL)) {
1444 prune_num++;
1445 REALLOC_ARRAY(prune, prune_num);
1446 prune[prune_num-2] = ce->name;
1447 prune[prune_num-1] = NULL;
1449 while ((i+1 < active_nr) &&
1450 ce_same_name(ce, active_cache[i+1]))
1451 i++;
1453 free_pathspec(&revs->prune_data);
1454 parse_pathspec(&revs->prune_data, PATHSPEC_ALL_MAGIC & ~PATHSPEC_LITERAL,
1455 PATHSPEC_PREFER_FULL | PATHSPEC_LITERAL_PATH, "", prune);
1456 revs->limited = 1;
1459 int handle_revision_arg(const char *arg_, struct rev_info *revs, int flags, unsigned revarg_opt)
1461 struct object_context oc;
1462 char *dotdot;
1463 struct object *object;
1464 unsigned char sha1[20];
1465 int local_flags;
1466 const char *arg = arg_;
1467 int cant_be_filename = revarg_opt & REVARG_CANNOT_BE_FILENAME;
1468 unsigned get_sha1_flags = 0;
1470 flags = flags & UNINTERESTING ? flags | BOTTOM : flags & ~BOTTOM;
1472 dotdot = strstr(arg, "..");
1473 if (dotdot) {
1474 unsigned char from_sha1[20];
1475 const char *next = dotdot + 2;
1476 const char *this = arg;
1477 int symmetric = *next == '.';
1478 unsigned int flags_exclude = flags ^ (UNINTERESTING | BOTTOM);
1479 static const char head_by_default[] = "HEAD";
1480 unsigned int a_flags;
1482 *dotdot = 0;
1483 next += symmetric;
1485 if (!*next)
1486 next = head_by_default;
1487 if (dotdot == arg)
1488 this = head_by_default;
1489 if (this == head_by_default && next == head_by_default &&
1490 !symmetric) {
1492 * Just ".."? That is not a range but the
1493 * pathspec for the parent directory.
1495 if (!cant_be_filename) {
1496 *dotdot = '.';
1497 return -1;
1500 if (!get_sha1_committish(this, from_sha1) &&
1501 !get_sha1_committish(next, sha1)) {
1502 struct object *a_obj, *b_obj;
1504 if (!cant_be_filename) {
1505 *dotdot = '.';
1506 verify_non_filename(revs->prefix, arg);
1509 a_obj = parse_object(from_sha1);
1510 b_obj = parse_object(sha1);
1511 if (!a_obj || !b_obj) {
1512 missing:
1513 if (revs->ignore_missing)
1514 return 0;
1515 die(symmetric
1516 ? "Invalid symmetric difference expression %s"
1517 : "Invalid revision range %s", arg);
1520 if (!symmetric) {
1521 /* just A..B */
1522 a_flags = flags_exclude;
1523 } else {
1524 /* A...B -- find merge bases between the two */
1525 struct commit *a, *b;
1526 struct commit_list *exclude;
1528 a = (a_obj->type == OBJ_COMMIT
1529 ? (struct commit *)a_obj
1530 : lookup_commit_reference(a_obj->sha1));
1531 b = (b_obj->type == OBJ_COMMIT
1532 ? (struct commit *)b_obj
1533 : lookup_commit_reference(b_obj->sha1));
1534 if (!a || !b)
1535 goto missing;
1536 exclude = get_merge_bases(a, b);
1537 add_rev_cmdline_list(revs, exclude,
1538 REV_CMD_MERGE_BASE,
1539 flags_exclude);
1540 add_pending_commit_list(revs, exclude,
1541 flags_exclude);
1542 free_commit_list(exclude);
1544 a_flags = flags | SYMMETRIC_LEFT;
1547 a_obj->flags |= a_flags;
1548 b_obj->flags |= flags;
1549 add_rev_cmdline(revs, a_obj, this,
1550 REV_CMD_LEFT, a_flags);
1551 add_rev_cmdline(revs, b_obj, next,
1552 REV_CMD_RIGHT, flags);
1553 add_pending_object(revs, a_obj, this);
1554 add_pending_object(revs, b_obj, next);
1555 return 0;
1557 *dotdot = '.';
1559 dotdot = strstr(arg, "^@");
1560 if (dotdot && !dotdot[2]) {
1561 *dotdot = 0;
1562 if (add_parents_only(revs, arg, flags))
1563 return 0;
1564 *dotdot = '^';
1566 dotdot = strstr(arg, "^!");
1567 if (dotdot && !dotdot[2]) {
1568 *dotdot = 0;
1569 if (!add_parents_only(revs, arg, flags ^ (UNINTERESTING | BOTTOM)))
1570 *dotdot = '^';
1573 local_flags = 0;
1574 if (*arg == '^') {
1575 local_flags = UNINTERESTING | BOTTOM;
1576 arg++;
1579 if (revarg_opt & REVARG_COMMITTISH)
1580 get_sha1_flags = GET_SHA1_COMMITTISH;
1582 if (get_sha1_with_context(arg, get_sha1_flags, sha1, &oc))
1583 return revs->ignore_missing ? 0 : -1;
1584 if (!cant_be_filename)
1585 verify_non_filename(revs->prefix, arg);
1586 object = get_reference(revs, arg, sha1, flags ^ local_flags);
1587 add_rev_cmdline(revs, object, arg_, REV_CMD_REV, flags ^ local_flags);
1588 add_pending_object_with_mode(revs, object, arg, oc.mode);
1589 return 0;
1592 struct cmdline_pathspec {
1593 int alloc;
1594 int nr;
1595 const char **path;
1598 static void append_prune_data(struct cmdline_pathspec *prune, const char **av)
1600 while (*av) {
1601 ALLOC_GROW(prune->path, prune->nr + 1, prune->alloc);
1602 prune->path[prune->nr++] = *(av++);
1606 static void read_pathspec_from_stdin(struct rev_info *revs, struct strbuf *sb,
1607 struct cmdline_pathspec *prune)
1609 while (strbuf_getwholeline(sb, stdin, '\n') != EOF) {
1610 int len = sb->len;
1611 if (len && sb->buf[len - 1] == '\n')
1612 sb->buf[--len] = '\0';
1613 ALLOC_GROW(prune->path, prune->nr + 1, prune->alloc);
1614 prune->path[prune->nr++] = xstrdup(sb->buf);
1618 static void read_revisions_from_stdin(struct rev_info *revs,
1619 struct cmdline_pathspec *prune)
1621 struct strbuf sb;
1622 int seen_dashdash = 0;
1623 int save_warning;
1625 save_warning = warn_on_object_refname_ambiguity;
1626 warn_on_object_refname_ambiguity = 0;
1628 strbuf_init(&sb, 1000);
1629 while (strbuf_getwholeline(&sb, stdin, '\n') != EOF) {
1630 int len = sb.len;
1631 if (len && sb.buf[len - 1] == '\n')
1632 sb.buf[--len] = '\0';
1633 if (!len)
1634 break;
1635 if (sb.buf[0] == '-') {
1636 if (len == 2 && sb.buf[1] == '-') {
1637 seen_dashdash = 1;
1638 break;
1640 die("options not supported in --stdin mode");
1642 if (handle_revision_arg(sb.buf, revs, 0,
1643 REVARG_CANNOT_BE_FILENAME))
1644 die("bad revision '%s'", sb.buf);
1646 if (seen_dashdash)
1647 read_pathspec_from_stdin(revs, &sb, prune);
1649 strbuf_release(&sb);
1650 warn_on_object_refname_ambiguity = save_warning;
1653 static void add_grep(struct rev_info *revs, const char *ptn, enum grep_pat_token what)
1655 append_grep_pattern(&revs->grep_filter, ptn, "command line", 0, what);
1658 static void add_header_grep(struct rev_info *revs, enum grep_header_field field, const char *pattern)
1660 append_header_grep_pattern(&revs->grep_filter, field, pattern);
1663 static void add_message_grep(struct rev_info *revs, const char *pattern)
1665 add_grep(revs, pattern, GREP_PATTERN_BODY);
1668 static int handle_revision_opt(struct rev_info *revs, int argc, const char **argv,
1669 int *unkc, const char **unkv)
1671 const char *arg = argv[0];
1672 const char *optarg;
1673 int argcount;
1675 /* pseudo revision arguments */
1676 if (!strcmp(arg, "--all") || !strcmp(arg, "--branches") ||
1677 !strcmp(arg, "--tags") || !strcmp(arg, "--remotes") ||
1678 !strcmp(arg, "--reflog") || !strcmp(arg, "--not") ||
1679 !strcmp(arg, "--no-walk") || !strcmp(arg, "--do-walk") ||
1680 !strcmp(arg, "--bisect") || starts_with(arg, "--glob=") ||
1681 !strcmp(arg, "--indexed-objects") ||
1682 starts_with(arg, "--exclude=") ||
1683 starts_with(arg, "--branches=") || starts_with(arg, "--tags=") ||
1684 starts_with(arg, "--remotes=") || starts_with(arg, "--no-walk="))
1686 unkv[(*unkc)++] = arg;
1687 return 1;
1690 if ((argcount = parse_long_opt("max-count", argv, &optarg))) {
1691 revs->max_count = atoi(optarg);
1692 revs->no_walk = 0;
1693 return argcount;
1694 } else if ((argcount = parse_long_opt("skip", argv, &optarg))) {
1695 revs->skip_count = atoi(optarg);
1696 return argcount;
1697 } else if ((*arg == '-') && isdigit(arg[1])) {
1698 /* accept -<digit>, like traditional "head" */
1699 if (strtol_i(arg + 1, 10, &revs->max_count) < 0 ||
1700 revs->max_count < 0)
1701 die("'%s': not a non-negative integer", arg + 1);
1702 revs->no_walk = 0;
1703 } else if (!strcmp(arg, "-n")) {
1704 if (argc <= 1)
1705 return error("-n requires an argument");
1706 revs->max_count = atoi(argv[1]);
1707 revs->no_walk = 0;
1708 return 2;
1709 } else if (starts_with(arg, "-n")) {
1710 revs->max_count = atoi(arg + 2);
1711 revs->no_walk = 0;
1712 } else if ((argcount = parse_long_opt("max-age", argv, &optarg))) {
1713 revs->max_age = atoi(optarg);
1714 return argcount;
1715 } else if ((argcount = parse_long_opt("since", argv, &optarg))) {
1716 revs->max_age = approxidate(optarg);
1717 return argcount;
1718 } else if ((argcount = parse_long_opt("after", argv, &optarg))) {
1719 revs->max_age = approxidate(optarg);
1720 return argcount;
1721 } else if ((argcount = parse_long_opt("min-age", argv, &optarg))) {
1722 revs->min_age = atoi(optarg);
1723 return argcount;
1724 } else if ((argcount = parse_long_opt("before", argv, &optarg))) {
1725 revs->min_age = approxidate(optarg);
1726 return argcount;
1727 } else if ((argcount = parse_long_opt("until", argv, &optarg))) {
1728 revs->min_age = approxidate(optarg);
1729 return argcount;
1730 } else if (!strcmp(arg, "--first-parent")) {
1731 revs->first_parent_only = 1;
1732 } else if (!strcmp(arg, "--ancestry-path")) {
1733 revs->ancestry_path = 1;
1734 revs->simplify_history = 0;
1735 revs->limited = 1;
1736 } else if (!strcmp(arg, "-g") || !strcmp(arg, "--walk-reflogs")) {
1737 init_reflog_walk(&revs->reflog_info);
1738 } else if (!strcmp(arg, "--default")) {
1739 if (argc <= 1)
1740 return error("bad --default argument");
1741 revs->def = argv[1];
1742 return 2;
1743 } else if (!strcmp(arg, "--merge")) {
1744 revs->show_merge = 1;
1745 } else if (!strcmp(arg, "--topo-order")) {
1746 revs->sort_order = REV_SORT_IN_GRAPH_ORDER;
1747 revs->topo_order = 1;
1748 } else if (!strcmp(arg, "--simplify-merges")) {
1749 revs->simplify_merges = 1;
1750 revs->topo_order = 1;
1751 revs->rewrite_parents = 1;
1752 revs->simplify_history = 0;
1753 revs->limited = 1;
1754 } else if (!strcmp(arg, "--simplify-by-decoration")) {
1755 revs->simplify_merges = 1;
1756 revs->topo_order = 1;
1757 revs->rewrite_parents = 1;
1758 revs->simplify_history = 0;
1759 revs->simplify_by_decoration = 1;
1760 revs->limited = 1;
1761 revs->prune = 1;
1762 load_ref_decorations(DECORATE_SHORT_REFS);
1763 } else if (!strcmp(arg, "--date-order")) {
1764 revs->sort_order = REV_SORT_BY_COMMIT_DATE;
1765 revs->topo_order = 1;
1766 } else if (!strcmp(arg, "--author-date-order")) {
1767 revs->sort_order = REV_SORT_BY_AUTHOR_DATE;
1768 revs->topo_order = 1;
1769 } else if (starts_with(arg, "--early-output")) {
1770 int count = 100;
1771 switch (arg[14]) {
1772 case '=':
1773 count = atoi(arg+15);
1774 /* Fallthrough */
1775 case 0:
1776 revs->topo_order = 1;
1777 revs->early_output = count;
1779 } else if (!strcmp(arg, "--parents")) {
1780 revs->rewrite_parents = 1;
1781 revs->print_parents = 1;
1782 } else if (!strcmp(arg, "--dense")) {
1783 revs->dense = 1;
1784 } else if (!strcmp(arg, "--sparse")) {
1785 revs->dense = 0;
1786 } else if (!strcmp(arg, "--show-all")) {
1787 revs->show_all = 1;
1788 } else if (!strcmp(arg, "--remove-empty")) {
1789 revs->remove_empty_trees = 1;
1790 } else if (!strcmp(arg, "--merges")) {
1791 revs->min_parents = 2;
1792 } else if (!strcmp(arg, "--no-merges")) {
1793 revs->max_parents = 1;
1794 } else if (starts_with(arg, "--min-parents=")) {
1795 revs->min_parents = atoi(arg+14);
1796 } else if (starts_with(arg, "--no-min-parents")) {
1797 revs->min_parents = 0;
1798 } else if (starts_with(arg, "--max-parents=")) {
1799 revs->max_parents = atoi(arg+14);
1800 } else if (starts_with(arg, "--no-max-parents")) {
1801 revs->max_parents = -1;
1802 } else if (!strcmp(arg, "--boundary")) {
1803 revs->boundary = 1;
1804 } else if (!strcmp(arg, "--left-right")) {
1805 revs->left_right = 1;
1806 } else if (!strcmp(arg, "--left-only")) {
1807 if (revs->right_only)
1808 die("--left-only is incompatible with --right-only"
1809 " or --cherry");
1810 revs->left_only = 1;
1811 } else if (!strcmp(arg, "--right-only")) {
1812 if (revs->left_only)
1813 die("--right-only is incompatible with --left-only");
1814 revs->right_only = 1;
1815 } else if (!strcmp(arg, "--cherry")) {
1816 if (revs->left_only)
1817 die("--cherry is incompatible with --left-only");
1818 revs->cherry_mark = 1;
1819 revs->right_only = 1;
1820 revs->max_parents = 1;
1821 revs->limited = 1;
1822 } else if (!strcmp(arg, "--count")) {
1823 revs->count = 1;
1824 } else if (!strcmp(arg, "--cherry-mark")) {
1825 if (revs->cherry_pick)
1826 die("--cherry-mark is incompatible with --cherry-pick");
1827 revs->cherry_mark = 1;
1828 revs->limited = 1; /* needs limit_list() */
1829 } else if (!strcmp(arg, "--cherry-pick")) {
1830 if (revs->cherry_mark)
1831 die("--cherry-pick is incompatible with --cherry-mark");
1832 revs->cherry_pick = 1;
1833 revs->limited = 1;
1834 } else if (!strcmp(arg, "--objects")) {
1835 revs->tag_objects = 1;
1836 revs->tree_objects = 1;
1837 revs->blob_objects = 1;
1838 } else if (!strcmp(arg, "--objects-edge")) {
1839 revs->tag_objects = 1;
1840 revs->tree_objects = 1;
1841 revs->blob_objects = 1;
1842 revs->edge_hint = 1;
1843 } else if (!strcmp(arg, "--objects-edge-aggressive")) {
1844 revs->tag_objects = 1;
1845 revs->tree_objects = 1;
1846 revs->blob_objects = 1;
1847 revs->edge_hint = 1;
1848 revs->edge_hint_aggressive = 1;
1849 } else if (!strcmp(arg, "--verify-objects")) {
1850 revs->tag_objects = 1;
1851 revs->tree_objects = 1;
1852 revs->blob_objects = 1;
1853 revs->verify_objects = 1;
1854 } else if (!strcmp(arg, "--unpacked")) {
1855 revs->unpacked = 1;
1856 } else if (starts_with(arg, "--unpacked=")) {
1857 die("--unpacked=<packfile> no longer supported.");
1858 } else if (!strcmp(arg, "-r")) {
1859 revs->diff = 1;
1860 DIFF_OPT_SET(&revs->diffopt, RECURSIVE);
1861 } else if (!strcmp(arg, "-t")) {
1862 revs->diff = 1;
1863 DIFF_OPT_SET(&revs->diffopt, RECURSIVE);
1864 DIFF_OPT_SET(&revs->diffopt, TREE_IN_RECURSIVE);
1865 } else if (!strcmp(arg, "-m")) {
1866 revs->ignore_merges = 0;
1867 } else if (!strcmp(arg, "-c")) {
1868 revs->diff = 1;
1869 revs->dense_combined_merges = 0;
1870 revs->combine_merges = 1;
1871 } else if (!strcmp(arg, "--cc")) {
1872 revs->diff = 1;
1873 revs->dense_combined_merges = 1;
1874 revs->combine_merges = 1;
1875 } else if (!strcmp(arg, "-v")) {
1876 revs->verbose_header = 1;
1877 } else if (!strcmp(arg, "--pretty")) {
1878 revs->verbose_header = 1;
1879 revs->pretty_given = 1;
1880 get_commit_format(NULL, revs);
1881 } else if (starts_with(arg, "--pretty=") || starts_with(arg, "--format=")) {
1883 * Detached form ("--pretty X" as opposed to "--pretty=X")
1884 * not allowed, since the argument is optional.
1886 revs->verbose_header = 1;
1887 revs->pretty_given = 1;
1888 get_commit_format(arg+9, revs);
1889 } else if (!strcmp(arg, "--show-notes") || !strcmp(arg, "--notes")) {
1890 revs->show_notes = 1;
1891 revs->show_notes_given = 1;
1892 revs->notes_opt.use_default_notes = 1;
1893 } else if (!strcmp(arg, "--show-signature")) {
1894 revs->show_signature = 1;
1895 } else if (!strcmp(arg, "--show-linear-break") ||
1896 starts_with(arg, "--show-linear-break=")) {
1897 if (starts_with(arg, "--show-linear-break="))
1898 revs->break_bar = xstrdup(arg + 20);
1899 else
1900 revs->break_bar = " ..........";
1901 revs->track_linear = 1;
1902 revs->track_first_time = 1;
1903 } else if (starts_with(arg, "--show-notes=") ||
1904 starts_with(arg, "--notes=")) {
1905 struct strbuf buf = STRBUF_INIT;
1906 revs->show_notes = 1;
1907 revs->show_notes_given = 1;
1908 if (starts_with(arg, "--show-notes")) {
1909 if (revs->notes_opt.use_default_notes < 0)
1910 revs->notes_opt.use_default_notes = 1;
1911 strbuf_addstr(&buf, arg+13);
1913 else
1914 strbuf_addstr(&buf, arg+8);
1915 expand_notes_ref(&buf);
1916 string_list_append(&revs->notes_opt.extra_notes_refs,
1917 strbuf_detach(&buf, NULL));
1918 } else if (!strcmp(arg, "--no-notes")) {
1919 revs->show_notes = 0;
1920 revs->show_notes_given = 1;
1921 revs->notes_opt.use_default_notes = -1;
1922 /* we have been strdup'ing ourselves, so trick
1923 * string_list into free()ing strings */
1924 revs->notes_opt.extra_notes_refs.strdup_strings = 1;
1925 string_list_clear(&revs->notes_opt.extra_notes_refs, 0);
1926 revs->notes_opt.extra_notes_refs.strdup_strings = 0;
1927 } else if (!strcmp(arg, "--standard-notes")) {
1928 revs->show_notes_given = 1;
1929 revs->notes_opt.use_default_notes = 1;
1930 } else if (!strcmp(arg, "--no-standard-notes")) {
1931 revs->notes_opt.use_default_notes = 0;
1932 } else if (!strcmp(arg, "--oneline")) {
1933 revs->verbose_header = 1;
1934 get_commit_format("oneline", revs);
1935 revs->pretty_given = 1;
1936 revs->abbrev_commit = 1;
1937 } else if (!strcmp(arg, "--graph")) {
1938 revs->topo_order = 1;
1939 revs->rewrite_parents = 1;
1940 revs->graph = graph_init(revs);
1941 } else if (!strcmp(arg, "--root")) {
1942 revs->show_root_diff = 1;
1943 } else if (!strcmp(arg, "--no-commit-id")) {
1944 revs->no_commit_id = 1;
1945 } else if (!strcmp(arg, "--always")) {
1946 revs->always_show_header = 1;
1947 } else if (!strcmp(arg, "--no-abbrev")) {
1948 revs->abbrev = 0;
1949 } else if (!strcmp(arg, "--abbrev")) {
1950 revs->abbrev = DEFAULT_ABBREV;
1951 } else if (starts_with(arg, "--abbrev=")) {
1952 revs->abbrev = strtoul(arg + 9, NULL, 10);
1953 if (revs->abbrev < MINIMUM_ABBREV)
1954 revs->abbrev = MINIMUM_ABBREV;
1955 else if (revs->abbrev > 40)
1956 revs->abbrev = 40;
1957 } else if (!strcmp(arg, "--abbrev-commit")) {
1958 revs->abbrev_commit = 1;
1959 revs->abbrev_commit_given = 1;
1960 } else if (!strcmp(arg, "--no-abbrev-commit")) {
1961 revs->abbrev_commit = 0;
1962 } else if (!strcmp(arg, "--full-diff")) {
1963 revs->diff = 1;
1964 revs->full_diff = 1;
1965 } else if (!strcmp(arg, "--full-history")) {
1966 revs->simplify_history = 0;
1967 } else if (!strcmp(arg, "--relative-date")) {
1968 revs->date_mode = DATE_RELATIVE;
1969 revs->date_mode_explicit = 1;
1970 } else if ((argcount = parse_long_opt("date", argv, &optarg))) {
1971 revs->date_mode = parse_date_format(optarg);
1972 revs->date_mode_explicit = 1;
1973 return argcount;
1974 } else if (!strcmp(arg, "--log-size")) {
1975 revs->show_log_size = 1;
1978 * Grepping the commit log
1980 else if ((argcount = parse_long_opt("author", argv, &optarg))) {
1981 add_header_grep(revs, GREP_HEADER_AUTHOR, optarg);
1982 return argcount;
1983 } else if ((argcount = parse_long_opt("committer", argv, &optarg))) {
1984 add_header_grep(revs, GREP_HEADER_COMMITTER, optarg);
1985 return argcount;
1986 } else if ((argcount = parse_long_opt("grep-reflog", argv, &optarg))) {
1987 add_header_grep(revs, GREP_HEADER_REFLOG, optarg);
1988 return argcount;
1989 } else if ((argcount = parse_long_opt("grep", argv, &optarg))) {
1990 add_message_grep(revs, optarg);
1991 return argcount;
1992 } else if (!strcmp(arg, "--grep-debug")) {
1993 revs->grep_filter.debug = 1;
1994 } else if (!strcmp(arg, "--basic-regexp")) {
1995 grep_set_pattern_type_option(GREP_PATTERN_TYPE_BRE, &revs->grep_filter);
1996 } else if (!strcmp(arg, "--extended-regexp") || !strcmp(arg, "-E")) {
1997 grep_set_pattern_type_option(GREP_PATTERN_TYPE_ERE, &revs->grep_filter);
1998 } else if (!strcmp(arg, "--regexp-ignore-case") || !strcmp(arg, "-i")) {
1999 revs->grep_filter.regflags |= REG_ICASE;
2000 DIFF_OPT_SET(&revs->diffopt, PICKAXE_IGNORE_CASE);
2001 } else if (!strcmp(arg, "--fixed-strings") || !strcmp(arg, "-F")) {
2002 grep_set_pattern_type_option(GREP_PATTERN_TYPE_FIXED, &revs->grep_filter);
2003 } else if (!strcmp(arg, "--perl-regexp")) {
2004 grep_set_pattern_type_option(GREP_PATTERN_TYPE_PCRE, &revs->grep_filter);
2005 } else if (!strcmp(arg, "--all-match")) {
2006 revs->grep_filter.all_match = 1;
2007 } else if (!strcmp(arg, "--invert-grep")) {
2008 revs->invert_grep = 1;
2009 } else if ((argcount = parse_long_opt("encoding", argv, &optarg))) {
2010 if (strcmp(optarg, "none"))
2011 git_log_output_encoding = xstrdup(optarg);
2012 else
2013 git_log_output_encoding = "";
2014 return argcount;
2015 } else if (!strcmp(arg, "--reverse")) {
2016 revs->reverse ^= 1;
2017 } else if (!strcmp(arg, "--children")) {
2018 revs->children.name = "children";
2019 revs->limited = 1;
2020 } else if (!strcmp(arg, "--ignore-missing")) {
2021 revs->ignore_missing = 1;
2022 } else {
2023 int opts = diff_opt_parse(&revs->diffopt, argv, argc);
2024 if (!opts)
2025 unkv[(*unkc)++] = arg;
2026 return opts;
2028 if (revs->graph && revs->track_linear)
2029 die("--show-linear-break and --graph are incompatible");
2031 return 1;
2034 void parse_revision_opt(struct rev_info *revs, struct parse_opt_ctx_t *ctx,
2035 const struct option *options,
2036 const char * const usagestr[])
2038 int n = handle_revision_opt(revs, ctx->argc, ctx->argv,
2039 &ctx->cpidx, ctx->out);
2040 if (n <= 0) {
2041 error("unknown option `%s'", ctx->argv[0]);
2042 usage_with_options(usagestr, options);
2044 ctx->argv += n;
2045 ctx->argc -= n;
2048 static int for_each_bad_bisect_ref(const char *submodule, each_ref_fn fn, void *cb_data)
2050 return for_each_ref_in_submodule(submodule, "refs/bisect/bad", fn, cb_data);
2053 static int for_each_good_bisect_ref(const char *submodule, each_ref_fn fn, void *cb_data)
2055 return for_each_ref_in_submodule(submodule, "refs/bisect/good", fn, cb_data);
2058 static int handle_revision_pseudo_opt(const char *submodule,
2059 struct rev_info *revs,
2060 int argc, const char **argv, int *flags)
2062 const char *arg = argv[0];
2063 const char *optarg;
2064 int argcount;
2067 * NOTE!
2069 * Commands like "git shortlog" will not accept the options below
2070 * unless parse_revision_opt queues them (as opposed to erroring
2071 * out).
2073 * When implementing your new pseudo-option, remember to
2074 * register it in the list at the top of handle_revision_opt.
2076 if (!strcmp(arg, "--all")) {
2077 handle_refs(submodule, revs, *flags, for_each_ref_submodule);
2078 handle_refs(submodule, revs, *flags, head_ref_submodule);
2079 clear_ref_exclusion(&revs->ref_excludes);
2080 } else if (!strcmp(arg, "--branches")) {
2081 handle_refs(submodule, revs, *flags, for_each_branch_ref_submodule);
2082 clear_ref_exclusion(&revs->ref_excludes);
2083 } else if (!strcmp(arg, "--bisect")) {
2084 handle_refs(submodule, revs, *flags, for_each_bad_bisect_ref);
2085 handle_refs(submodule, revs, *flags ^ (UNINTERESTING | BOTTOM), for_each_good_bisect_ref);
2086 revs->bisect = 1;
2087 } else if (!strcmp(arg, "--tags")) {
2088 handle_refs(submodule, revs, *flags, for_each_tag_ref_submodule);
2089 clear_ref_exclusion(&revs->ref_excludes);
2090 } else if (!strcmp(arg, "--remotes")) {
2091 handle_refs(submodule, revs, *flags, for_each_remote_ref_submodule);
2092 clear_ref_exclusion(&revs->ref_excludes);
2093 } else if ((argcount = parse_long_opt("glob", argv, &optarg))) {
2094 struct all_refs_cb cb;
2095 init_all_refs_cb(&cb, revs, *flags);
2096 for_each_glob_ref(handle_one_ref, optarg, &cb);
2097 clear_ref_exclusion(&revs->ref_excludes);
2098 return argcount;
2099 } else if ((argcount = parse_long_opt("exclude", argv, &optarg))) {
2100 add_ref_exclusion(&revs->ref_excludes, optarg);
2101 return argcount;
2102 } else if (starts_with(arg, "--branches=")) {
2103 struct all_refs_cb cb;
2104 init_all_refs_cb(&cb, revs, *flags);
2105 for_each_glob_ref_in(handle_one_ref, arg + 11, "refs/heads/", &cb);
2106 clear_ref_exclusion(&revs->ref_excludes);
2107 } else if (starts_with(arg, "--tags=")) {
2108 struct all_refs_cb cb;
2109 init_all_refs_cb(&cb, revs, *flags);
2110 for_each_glob_ref_in(handle_one_ref, arg + 7, "refs/tags/", &cb);
2111 clear_ref_exclusion(&revs->ref_excludes);
2112 } else if (starts_with(arg, "--remotes=")) {
2113 struct all_refs_cb cb;
2114 init_all_refs_cb(&cb, revs, *flags);
2115 for_each_glob_ref_in(handle_one_ref, arg + 10, "refs/remotes/", &cb);
2116 clear_ref_exclusion(&revs->ref_excludes);
2117 } else if (!strcmp(arg, "--reflog")) {
2118 add_reflogs_to_pending(revs, *flags);
2119 } else if (!strcmp(arg, "--indexed-objects")) {
2120 add_index_objects_to_pending(revs, *flags);
2121 } else if (!strcmp(arg, "--not")) {
2122 *flags ^= UNINTERESTING | BOTTOM;
2123 } else if (!strcmp(arg, "--no-walk")) {
2124 revs->no_walk = REVISION_WALK_NO_WALK_SORTED;
2125 } else if (starts_with(arg, "--no-walk=")) {
2127 * Detached form ("--no-walk X" as opposed to "--no-walk=X")
2128 * not allowed, since the argument is optional.
2130 if (!strcmp(arg + 10, "sorted"))
2131 revs->no_walk = REVISION_WALK_NO_WALK_SORTED;
2132 else if (!strcmp(arg + 10, "unsorted"))
2133 revs->no_walk = REVISION_WALK_NO_WALK_UNSORTED;
2134 else
2135 return error("invalid argument to --no-walk");
2136 } else if (!strcmp(arg, "--do-walk")) {
2137 revs->no_walk = 0;
2138 } else {
2139 return 0;
2142 return 1;
2146 * Parse revision information, filling in the "rev_info" structure,
2147 * and removing the used arguments from the argument list.
2149 * Returns the number of arguments left that weren't recognized
2150 * (which are also moved to the head of the argument list)
2152 int setup_revisions(int argc, const char **argv, struct rev_info *revs, struct setup_revision_opt *opt)
2154 int i, flags, left, seen_dashdash, read_from_stdin, got_rev_arg = 0, revarg_opt;
2155 struct cmdline_pathspec prune_data;
2156 const char *submodule = NULL;
2158 memset(&prune_data, 0, sizeof(prune_data));
2159 if (opt)
2160 submodule = opt->submodule;
2162 /* First, search for "--" */
2163 if (opt && opt->assume_dashdash) {
2164 seen_dashdash = 1;
2165 } else {
2166 seen_dashdash = 0;
2167 for (i = 1; i < argc; i++) {
2168 const char *arg = argv[i];
2169 if (strcmp(arg, "--"))
2170 continue;
2171 argv[i] = NULL;
2172 argc = i;
2173 if (argv[i + 1])
2174 append_prune_data(&prune_data, argv + i + 1);
2175 seen_dashdash = 1;
2176 break;
2180 /* Second, deal with arguments and options */
2181 flags = 0;
2182 revarg_opt = opt ? opt->revarg_opt : 0;
2183 if (seen_dashdash)
2184 revarg_opt |= REVARG_CANNOT_BE_FILENAME;
2185 read_from_stdin = 0;
2186 for (left = i = 1; i < argc; i++) {
2187 const char *arg = argv[i];
2188 if (*arg == '-') {
2189 int opts;
2191 opts = handle_revision_pseudo_opt(submodule,
2192 revs, argc - i, argv + i,
2193 &flags);
2194 if (opts > 0) {
2195 i += opts - 1;
2196 continue;
2199 if (!strcmp(arg, "--stdin")) {
2200 if (revs->disable_stdin) {
2201 argv[left++] = arg;
2202 continue;
2204 if (read_from_stdin++)
2205 die("--stdin given twice?");
2206 read_revisions_from_stdin(revs, &prune_data);
2207 continue;
2210 opts = handle_revision_opt(revs, argc - i, argv + i, &left, argv);
2211 if (opts > 0) {
2212 i += opts - 1;
2213 continue;
2215 if (opts < 0)
2216 exit(128);
2217 continue;
2221 if (handle_revision_arg(arg, revs, flags, revarg_opt)) {
2222 int j;
2223 if (seen_dashdash || *arg == '^')
2224 die("bad revision '%s'", arg);
2226 /* If we didn't have a "--":
2227 * (1) all filenames must exist;
2228 * (2) all rev-args must not be interpretable
2229 * as a valid filename.
2230 * but the latter we have checked in the main loop.
2232 for (j = i; j < argc; j++)
2233 verify_filename(revs->prefix, argv[j], j == i);
2235 append_prune_data(&prune_data, argv + i);
2236 break;
2238 else
2239 got_rev_arg = 1;
2242 if (prune_data.nr) {
2244 * If we need to introduce the magic "a lone ':' means no
2245 * pathspec whatsoever", here is the place to do so.
2247 * if (prune_data.nr == 1 && !strcmp(prune_data[0], ":")) {
2248 * prune_data.nr = 0;
2249 * prune_data.alloc = 0;
2250 * free(prune_data.path);
2251 * prune_data.path = NULL;
2252 * } else {
2253 * terminate prune_data.alloc with NULL and
2254 * call init_pathspec() to set revs->prune_data here.
2257 ALLOC_GROW(prune_data.path, prune_data.nr + 1, prune_data.alloc);
2258 prune_data.path[prune_data.nr++] = NULL;
2259 parse_pathspec(&revs->prune_data, 0, 0,
2260 revs->prefix, prune_data.path);
2263 if (revs->def == NULL)
2264 revs->def = opt ? opt->def : NULL;
2265 if (opt && opt->tweak)
2266 opt->tweak(revs, opt);
2267 if (revs->show_merge)
2268 prepare_show_merge(revs);
2269 if (revs->def && !revs->pending.nr && !got_rev_arg) {
2270 unsigned char sha1[20];
2271 struct object *object;
2272 struct object_context oc;
2273 if (get_sha1_with_context(revs->def, 0, sha1, &oc))
2274 die("bad default revision '%s'", revs->def);
2275 object = get_reference(revs, revs->def, sha1, 0);
2276 add_pending_object_with_mode(revs, object, revs->def, oc.mode);
2279 /* Did the user ask for any diff output? Run the diff! */
2280 if (revs->diffopt.output_format & ~DIFF_FORMAT_NO_OUTPUT)
2281 revs->diff = 1;
2283 /* Pickaxe, diff-filter and rename following need diffs */
2284 if (revs->diffopt.pickaxe ||
2285 revs->diffopt.filter ||
2286 DIFF_OPT_TST(&revs->diffopt, FOLLOW_RENAMES))
2287 revs->diff = 1;
2289 if (revs->topo_order)
2290 revs->limited = 1;
2292 if (revs->prune_data.nr) {
2293 copy_pathspec(&revs->pruning.pathspec, &revs->prune_data);
2294 /* Can't prune commits with rename following: the paths change.. */
2295 if (!DIFF_OPT_TST(&revs->diffopt, FOLLOW_RENAMES))
2296 revs->prune = 1;
2297 if (!revs->full_diff)
2298 copy_pathspec(&revs->diffopt.pathspec,
2299 &revs->prune_data);
2301 if (revs->combine_merges)
2302 revs->ignore_merges = 0;
2303 revs->diffopt.abbrev = revs->abbrev;
2305 if (revs->line_level_traverse) {
2306 revs->limited = 1;
2307 revs->topo_order = 1;
2310 diff_setup_done(&revs->diffopt);
2312 grep_commit_pattern_type(GREP_PATTERN_TYPE_UNSPECIFIED,
2313 &revs->grep_filter);
2314 compile_grep_patterns(&revs->grep_filter);
2316 if (revs->reverse && revs->reflog_info)
2317 die("cannot combine --reverse with --walk-reflogs");
2318 if (revs->rewrite_parents && revs->children.name)
2319 die("cannot combine --parents and --children");
2322 * Limitations on the graph functionality
2324 if (revs->reverse && revs->graph)
2325 die("cannot combine --reverse with --graph");
2327 if (revs->reflog_info && revs->graph)
2328 die("cannot combine --walk-reflogs with --graph");
2329 if (revs->no_walk && revs->graph)
2330 die("cannot combine --no-walk with --graph");
2331 if (!revs->reflog_info && revs->grep_filter.use_reflog_filter)
2332 die("cannot use --grep-reflog without --walk-reflogs");
2334 if (revs->first_parent_only && revs->bisect)
2335 die(_("--first-parent is incompatible with --bisect"));
2337 return left;
2340 static void add_child(struct rev_info *revs, struct commit *parent, struct commit *child)
2342 struct commit_list *l = xcalloc(1, sizeof(*l));
2344 l->item = child;
2345 l->next = add_decoration(&revs->children, &parent->object, l);
2348 static int remove_duplicate_parents(struct rev_info *revs, struct commit *commit)
2350 struct treesame_state *ts = lookup_decoration(&revs->treesame, &commit->object);
2351 struct commit_list **pp, *p;
2352 int surviving_parents;
2354 /* Examine existing parents while marking ones we have seen... */
2355 pp = &commit->parents;
2356 surviving_parents = 0;
2357 while ((p = *pp) != NULL) {
2358 struct commit *parent = p->item;
2359 if (parent->object.flags & TMP_MARK) {
2360 *pp = p->next;
2361 if (ts)
2362 compact_treesame(revs, commit, surviving_parents);
2363 continue;
2365 parent->object.flags |= TMP_MARK;
2366 surviving_parents++;
2367 pp = &p->next;
2369 /* clear the temporary mark */
2370 for (p = commit->parents; p; p = p->next) {
2371 p->item->object.flags &= ~TMP_MARK;
2373 /* no update_treesame() - removing duplicates can't affect TREESAME */
2374 return surviving_parents;
2377 struct merge_simplify_state {
2378 struct commit *simplified;
2381 static struct merge_simplify_state *locate_simplify_state(struct rev_info *revs, struct commit *commit)
2383 struct merge_simplify_state *st;
2385 st = lookup_decoration(&revs->merge_simplification, &commit->object);
2386 if (!st) {
2387 st = xcalloc(1, sizeof(*st));
2388 add_decoration(&revs->merge_simplification, &commit->object, st);
2390 return st;
2393 static int mark_redundant_parents(struct rev_info *revs, struct commit *commit)
2395 struct commit_list *h = reduce_heads(commit->parents);
2396 int i = 0, marked = 0;
2397 struct commit_list *po, *pn;
2399 /* Want these for sanity-checking only */
2400 int orig_cnt = commit_list_count(commit->parents);
2401 int cnt = commit_list_count(h);
2404 * Not ready to remove items yet, just mark them for now, based
2405 * on the output of reduce_heads(). reduce_heads outputs the reduced
2406 * set in its original order, so this isn't too hard.
2408 po = commit->parents;
2409 pn = h;
2410 while (po) {
2411 if (pn && po->item == pn->item) {
2412 pn = pn->next;
2413 i++;
2414 } else {
2415 po->item->object.flags |= TMP_MARK;
2416 marked++;
2418 po=po->next;
2421 if (i != cnt || cnt+marked != orig_cnt)
2422 die("mark_redundant_parents %d %d %d %d", orig_cnt, cnt, i, marked);
2424 free_commit_list(h);
2426 return marked;
2429 static int mark_treesame_root_parents(struct rev_info *revs, struct commit *commit)
2431 struct commit_list *p;
2432 int marked = 0;
2434 for (p = commit->parents; p; p = p->next) {
2435 struct commit *parent = p->item;
2436 if (!parent->parents && (parent->object.flags & TREESAME)) {
2437 parent->object.flags |= TMP_MARK;
2438 marked++;
2442 return marked;
2446 * Awkward naming - this means one parent we are TREESAME to.
2447 * cf mark_treesame_root_parents: root parents that are TREESAME (to an
2448 * empty tree). Better name suggestions?
2450 static int leave_one_treesame_to_parent(struct rev_info *revs, struct commit *commit)
2452 struct treesame_state *ts = lookup_decoration(&revs->treesame, &commit->object);
2453 struct commit *unmarked = NULL, *marked = NULL;
2454 struct commit_list *p;
2455 unsigned n;
2457 for (p = commit->parents, n = 0; p; p = p->next, n++) {
2458 if (ts->treesame[n]) {
2459 if (p->item->object.flags & TMP_MARK) {
2460 if (!marked)
2461 marked = p->item;
2462 } else {
2463 if (!unmarked) {
2464 unmarked = p->item;
2465 break;
2472 * If we are TREESAME to a marked-for-deletion parent, but not to any
2473 * unmarked parents, unmark the first TREESAME parent. This is the
2474 * parent that the default simplify_history==1 scan would have followed,
2475 * and it doesn't make sense to omit that path when asking for a
2476 * simplified full history. Retaining it improves the chances of
2477 * understanding odd missed merges that took an old version of a file.
2479 * Example:
2481 * I--------*X A modified the file, but mainline merge X used
2482 * \ / "-s ours", so took the version from I. X is
2483 * `-*A--' TREESAME to I and !TREESAME to A.
2485 * Default log from X would produce "I". Without this check,
2486 * --full-history --simplify-merges would produce "I-A-X", showing
2487 * the merge commit X and that it changed A, but not making clear that
2488 * it had just taken the I version. With this check, the topology above
2489 * is retained.
2491 * Note that it is possible that the simplification chooses a different
2492 * TREESAME parent from the default, in which case this test doesn't
2493 * activate, and we _do_ drop the default parent. Example:
2495 * I------X A modified the file, but it was reverted in B,
2496 * \ / meaning mainline merge X is TREESAME to both
2497 * *A-*B parents.
2499 * Default log would produce "I" by following the first parent;
2500 * --full-history --simplify-merges will produce "I-A-B". But this is a
2501 * reasonable result - it presents a logical full history leading from
2502 * I to X, and X is not an important merge.
2504 if (!unmarked && marked) {
2505 marked->object.flags &= ~TMP_MARK;
2506 return 1;
2509 return 0;
2512 static int remove_marked_parents(struct rev_info *revs, struct commit *commit)
2514 struct commit_list **pp, *p;
2515 int nth_parent, removed = 0;
2517 pp = &commit->parents;
2518 nth_parent = 0;
2519 while ((p = *pp) != NULL) {
2520 struct commit *parent = p->item;
2521 if (parent->object.flags & TMP_MARK) {
2522 parent->object.flags &= ~TMP_MARK;
2523 *pp = p->next;
2524 free(p);
2525 removed++;
2526 compact_treesame(revs, commit, nth_parent);
2527 continue;
2529 pp = &p->next;
2530 nth_parent++;
2533 /* Removing parents can only increase TREESAMEness */
2534 if (removed && !(commit->object.flags & TREESAME))
2535 update_treesame(revs, commit);
2537 return nth_parent;
2540 static struct commit_list **simplify_one(struct rev_info *revs, struct commit *commit, struct commit_list **tail)
2542 struct commit_list *p;
2543 struct commit *parent;
2544 struct merge_simplify_state *st, *pst;
2545 int cnt;
2547 st = locate_simplify_state(revs, commit);
2550 * Have we handled this one?
2552 if (st->simplified)
2553 return tail;
2556 * An UNINTERESTING commit simplifies to itself, so does a
2557 * root commit. We do not rewrite parents of such commit
2558 * anyway.
2560 if ((commit->object.flags & UNINTERESTING) || !commit->parents) {
2561 st->simplified = commit;
2562 return tail;
2566 * Do we know what commit all of our parents that matter
2567 * should be rewritten to? Otherwise we are not ready to
2568 * rewrite this one yet.
2570 for (cnt = 0, p = commit->parents; p; p = p->next) {
2571 pst = locate_simplify_state(revs, p->item);
2572 if (!pst->simplified) {
2573 tail = &commit_list_insert(p->item, tail)->next;
2574 cnt++;
2576 if (revs->first_parent_only)
2577 break;
2579 if (cnt) {
2580 tail = &commit_list_insert(commit, tail)->next;
2581 return tail;
2585 * Rewrite our list of parents. Note that this cannot
2586 * affect our TREESAME flags in any way - a commit is
2587 * always TREESAME to its simplification.
2589 for (p = commit->parents; p; p = p->next) {
2590 pst = locate_simplify_state(revs, p->item);
2591 p->item = pst->simplified;
2592 if (revs->first_parent_only)
2593 break;
2596 if (revs->first_parent_only)
2597 cnt = 1;
2598 else
2599 cnt = remove_duplicate_parents(revs, commit);
2602 * It is possible that we are a merge and one side branch
2603 * does not have any commit that touches the given paths;
2604 * in such a case, the immediate parent from that branch
2605 * will be rewritten to be the merge base.
2607 * o----X X: the commit we are looking at;
2608 * / / o: a commit that touches the paths;
2609 * ---o----'
2611 * Further, a merge of an independent branch that doesn't
2612 * touch the path will reduce to a treesame root parent:
2614 * ----o----X X: the commit we are looking at;
2615 * / o: a commit that touches the paths;
2616 * r r: a root commit not touching the paths
2618 * Detect and simplify both cases.
2620 if (1 < cnt) {
2621 int marked = mark_redundant_parents(revs, commit);
2622 marked += mark_treesame_root_parents(revs, commit);
2623 if (marked)
2624 marked -= leave_one_treesame_to_parent(revs, commit);
2625 if (marked)
2626 cnt = remove_marked_parents(revs, commit);
2630 * A commit simplifies to itself if it is a root, if it is
2631 * UNINTERESTING, if it touches the given paths, or if it is a
2632 * merge and its parents don't simplify to one relevant commit
2633 * (the first two cases are already handled at the beginning of
2634 * this function).
2636 * Otherwise, it simplifies to what its sole relevant parent
2637 * simplifies to.
2639 if (!cnt ||
2640 (commit->object.flags & UNINTERESTING) ||
2641 !(commit->object.flags & TREESAME) ||
2642 (parent = one_relevant_parent(revs, commit->parents)) == NULL)
2643 st->simplified = commit;
2644 else {
2645 pst = locate_simplify_state(revs, parent);
2646 st->simplified = pst->simplified;
2648 return tail;
2651 static void simplify_merges(struct rev_info *revs)
2653 struct commit_list *list, *next;
2654 struct commit_list *yet_to_do, **tail;
2655 struct commit *commit;
2657 if (!revs->prune)
2658 return;
2660 /* feed the list reversed */
2661 yet_to_do = NULL;
2662 for (list = revs->commits; list; list = next) {
2663 commit = list->item;
2664 next = list->next;
2666 * Do not free(list) here yet; the original list
2667 * is used later in this function.
2669 commit_list_insert(commit, &yet_to_do);
2671 while (yet_to_do) {
2672 list = yet_to_do;
2673 yet_to_do = NULL;
2674 tail = &yet_to_do;
2675 while (list) {
2676 commit = list->item;
2677 next = list->next;
2678 free(list);
2679 list = next;
2680 tail = simplify_one(revs, commit, tail);
2684 /* clean up the result, removing the simplified ones */
2685 list = revs->commits;
2686 revs->commits = NULL;
2687 tail = &revs->commits;
2688 while (list) {
2689 struct merge_simplify_state *st;
2691 commit = list->item;
2692 next = list->next;
2693 free(list);
2694 list = next;
2695 st = locate_simplify_state(revs, commit);
2696 if (st->simplified == commit)
2697 tail = &commit_list_insert(commit, tail)->next;
2701 static void set_children(struct rev_info *revs)
2703 struct commit_list *l;
2704 for (l = revs->commits; l; l = l->next) {
2705 struct commit *commit = l->item;
2706 struct commit_list *p;
2708 for (p = commit->parents; p; p = p->next)
2709 add_child(revs, p->item, commit);
2713 void reset_revision_walk(void)
2715 clear_object_flags(SEEN | ADDED | SHOWN);
2718 int prepare_revision_walk(struct rev_info *revs)
2720 int i;
2721 struct object_array old_pending;
2722 struct commit_list **next = &revs->commits;
2724 memcpy(&old_pending, &revs->pending, sizeof(old_pending));
2725 revs->pending.nr = 0;
2726 revs->pending.alloc = 0;
2727 revs->pending.objects = NULL;
2728 for (i = 0; i < old_pending.nr; i++) {
2729 struct object_array_entry *e = old_pending.objects + i;
2730 struct commit *commit = handle_commit(revs, e);
2731 if (commit) {
2732 if (!(commit->object.flags & SEEN)) {
2733 commit->object.flags |= SEEN;
2734 next = commit_list_append(commit, next);
2738 if (!revs->leak_pending)
2739 object_array_clear(&old_pending);
2741 /* Signal whether we need per-parent treesame decoration */
2742 if (revs->simplify_merges ||
2743 (revs->limited && limiting_can_increase_treesame(revs)))
2744 revs->treesame.name = "treesame";
2746 if (revs->no_walk != REVISION_WALK_NO_WALK_UNSORTED)
2747 commit_list_sort_by_date(&revs->commits);
2748 if (revs->no_walk)
2749 return 0;
2750 if (revs->limited)
2751 if (limit_list(revs) < 0)
2752 return -1;
2753 if (revs->topo_order)
2754 sort_in_topological_order(&revs->commits, revs->sort_order);
2755 if (revs->line_level_traverse)
2756 line_log_filter(revs);
2757 if (revs->simplify_merges)
2758 simplify_merges(revs);
2759 if (revs->children.name)
2760 set_children(revs);
2761 return 0;
2764 static enum rewrite_result rewrite_one(struct rev_info *revs, struct commit **pp)
2766 struct commit_list *cache = NULL;
2768 for (;;) {
2769 struct commit *p = *pp;
2770 if (!revs->limited)
2771 if (add_parents_to_list(revs, p, &revs->commits, &cache) < 0)
2772 return rewrite_one_error;
2773 if (p->object.flags & UNINTERESTING)
2774 return rewrite_one_ok;
2775 if (!(p->object.flags & TREESAME))
2776 return rewrite_one_ok;
2777 if (!p->parents)
2778 return rewrite_one_noparents;
2779 if ((p = one_relevant_parent(revs, p->parents)) == NULL)
2780 return rewrite_one_ok;
2781 *pp = p;
2785 int rewrite_parents(struct rev_info *revs, struct commit *commit,
2786 rewrite_parent_fn_t rewrite_parent)
2788 struct commit_list **pp = &commit->parents;
2789 while (*pp) {
2790 struct commit_list *parent = *pp;
2791 switch (rewrite_parent(revs, &parent->item)) {
2792 case rewrite_one_ok:
2793 break;
2794 case rewrite_one_noparents:
2795 *pp = parent->next;
2796 continue;
2797 case rewrite_one_error:
2798 return -1;
2800 pp = &parent->next;
2802 remove_duplicate_parents(revs, commit);
2803 return 0;
2806 static int commit_rewrite_person(struct strbuf *buf, const char *what, struct string_list *mailmap)
2808 char *person, *endp;
2809 size_t len, namelen, maillen;
2810 const char *name;
2811 const char *mail;
2812 struct ident_split ident;
2814 person = strstr(buf->buf, what);
2815 if (!person)
2816 return 0;
2818 person += strlen(what);
2819 endp = strchr(person, '\n');
2820 if (!endp)
2821 return 0;
2823 len = endp - person;
2825 if (split_ident_line(&ident, person, len))
2826 return 0;
2828 mail = ident.mail_begin;
2829 maillen = ident.mail_end - ident.mail_begin;
2830 name = ident.name_begin;
2831 namelen = ident.name_end - ident.name_begin;
2833 if (map_user(mailmap, &mail, &maillen, &name, &namelen)) {
2834 struct strbuf namemail = STRBUF_INIT;
2836 strbuf_addf(&namemail, "%.*s <%.*s>",
2837 (int)namelen, name, (int)maillen, mail);
2839 strbuf_splice(buf, ident.name_begin - buf->buf,
2840 ident.mail_end - ident.name_begin + 1,
2841 namemail.buf, namemail.len);
2843 strbuf_release(&namemail);
2845 return 1;
2848 return 0;
2851 static int commit_match(struct commit *commit, struct rev_info *opt)
2853 int retval;
2854 const char *encoding;
2855 const char *message;
2856 struct strbuf buf = STRBUF_INIT;
2858 if (!opt->grep_filter.pattern_list && !opt->grep_filter.header_list)
2859 return 1;
2861 /* Prepend "fake" headers as needed */
2862 if (opt->grep_filter.use_reflog_filter) {
2863 strbuf_addstr(&buf, "reflog ");
2864 get_reflog_message(&buf, opt->reflog_info);
2865 strbuf_addch(&buf, '\n');
2869 * We grep in the user's output encoding, under the assumption that it
2870 * is the encoding they are most likely to write their grep pattern
2871 * for. In addition, it means we will match the "notes" encoding below,
2872 * so we will not end up with a buffer that has two different encodings
2873 * in it.
2875 encoding = get_log_output_encoding();
2876 message = logmsg_reencode(commit, NULL, encoding);
2878 /* Copy the commit to temporary if we are using "fake" headers */
2879 if (buf.len)
2880 strbuf_addstr(&buf, message);
2882 if (opt->grep_filter.header_list && opt->mailmap) {
2883 if (!buf.len)
2884 strbuf_addstr(&buf, message);
2886 commit_rewrite_person(&buf, "\nauthor ", opt->mailmap);
2887 commit_rewrite_person(&buf, "\ncommitter ", opt->mailmap);
2890 /* Append "fake" message parts as needed */
2891 if (opt->show_notes) {
2892 if (!buf.len)
2893 strbuf_addstr(&buf, message);
2894 format_display_notes(commit->object.sha1, &buf, encoding, 1);
2898 * Find either in the original commit message, or in the temporary.
2899 * Note that we cast away the constness of "message" here. It is
2900 * const because it may come from the cached commit buffer. That's OK,
2901 * because we know that it is modifiable heap memory, and that while
2902 * grep_buffer may modify it for speed, it will restore any
2903 * changes before returning.
2905 if (buf.len)
2906 retval = grep_buffer(&opt->grep_filter, buf.buf, buf.len);
2907 else
2908 retval = grep_buffer(&opt->grep_filter,
2909 (char *)message, strlen(message));
2910 strbuf_release(&buf);
2911 unuse_commit_buffer(commit, message);
2912 return opt->invert_grep ? !retval : retval;
2915 static inline int want_ancestry(const struct rev_info *revs)
2917 return (revs->rewrite_parents || revs->children.name);
2920 enum commit_action get_commit_action(struct rev_info *revs, struct commit *commit)
2922 if (commit->object.flags & SHOWN)
2923 return commit_ignore;
2924 if (revs->unpacked && has_sha1_pack(commit->object.sha1))
2925 return commit_ignore;
2926 if (revs->show_all)
2927 return commit_show;
2928 if (commit->object.flags & UNINTERESTING)
2929 return commit_ignore;
2930 if (revs->min_age != -1 && (commit->date > revs->min_age))
2931 return commit_ignore;
2932 if (revs->min_parents || (revs->max_parents >= 0)) {
2933 int n = commit_list_count(commit->parents);
2934 if ((n < revs->min_parents) ||
2935 ((revs->max_parents >= 0) && (n > revs->max_parents)))
2936 return commit_ignore;
2938 if (!commit_match(commit, revs))
2939 return commit_ignore;
2940 if (revs->prune && revs->dense) {
2941 /* Commit without changes? */
2942 if (commit->object.flags & TREESAME) {
2943 int n;
2944 struct commit_list *p;
2945 /* drop merges unless we want parenthood */
2946 if (!want_ancestry(revs))
2947 return commit_ignore;
2949 * If we want ancestry, then need to keep any merges
2950 * between relevant commits to tie together topology.
2951 * For consistency with TREESAME and simplification
2952 * use "relevant" here rather than just INTERESTING,
2953 * to treat bottom commit(s) as part of the topology.
2955 for (n = 0, p = commit->parents; p; p = p->next)
2956 if (relevant_commit(p->item))
2957 if (++n >= 2)
2958 return commit_show;
2959 return commit_ignore;
2962 return commit_show;
2965 define_commit_slab(saved_parents, struct commit_list *);
2967 #define EMPTY_PARENT_LIST ((struct commit_list *)-1)
2970 * You may only call save_parents() once per commit (this is checked
2971 * for non-root commits).
2973 static void save_parents(struct rev_info *revs, struct commit *commit)
2975 struct commit_list **pp;
2977 if (!revs->saved_parents_slab) {
2978 revs->saved_parents_slab = xmalloc(sizeof(struct saved_parents));
2979 init_saved_parents(revs->saved_parents_slab);
2982 pp = saved_parents_at(revs->saved_parents_slab, commit);
2985 * When walking with reflogs, we may visit the same commit
2986 * several times: once for each appearance in the reflog.
2988 * In this case, save_parents() will be called multiple times.
2989 * We want to keep only the first set of parents. We need to
2990 * store a sentinel value for an empty (i.e., NULL) parent
2991 * list to distinguish it from a not-yet-saved list, however.
2993 if (*pp)
2994 return;
2995 if (commit->parents)
2996 *pp = copy_commit_list(commit->parents);
2997 else
2998 *pp = EMPTY_PARENT_LIST;
3001 static void free_saved_parents(struct rev_info *revs)
3003 if (revs->saved_parents_slab)
3004 clear_saved_parents(revs->saved_parents_slab);
3007 struct commit_list *get_saved_parents(struct rev_info *revs, const struct commit *commit)
3009 struct commit_list *parents;
3011 if (!revs->saved_parents_slab)
3012 return commit->parents;
3014 parents = *saved_parents_at(revs->saved_parents_slab, commit);
3015 if (parents == EMPTY_PARENT_LIST)
3016 return NULL;
3017 return parents;
3020 enum commit_action simplify_commit(struct rev_info *revs, struct commit *commit)
3022 enum commit_action action = get_commit_action(revs, commit);
3024 if (action == commit_show &&
3025 !revs->show_all &&
3026 revs->prune && revs->dense && want_ancestry(revs)) {
3028 * --full-diff on simplified parents is no good: it
3029 * will show spurious changes from the commits that
3030 * were elided. So we save the parents on the side
3031 * when --full-diff is in effect.
3033 if (revs->full_diff)
3034 save_parents(revs, commit);
3035 if (rewrite_parents(revs, commit, rewrite_one) < 0)
3036 return commit_error;
3038 return action;
3041 static void track_linear(struct rev_info *revs, struct commit *commit)
3043 if (revs->track_first_time) {
3044 revs->linear = 1;
3045 revs->track_first_time = 0;
3046 } else {
3047 struct commit_list *p;
3048 for (p = revs->previous_parents; p; p = p->next)
3049 if (p->item == NULL || /* first commit */
3050 !hashcmp(p->item->object.sha1, commit->object.sha1))
3051 break;
3052 revs->linear = p != NULL;
3054 if (revs->reverse) {
3055 if (revs->linear)
3056 commit->object.flags |= TRACK_LINEAR;
3058 free_commit_list(revs->previous_parents);
3059 revs->previous_parents = copy_commit_list(commit->parents);
3062 static struct commit *get_revision_1(struct rev_info *revs)
3064 if (!revs->commits)
3065 return NULL;
3067 do {
3068 struct commit_list *entry = revs->commits;
3069 struct commit *commit = entry->item;
3071 revs->commits = entry->next;
3072 free(entry);
3074 if (revs->reflog_info) {
3075 save_parents(revs, commit);
3076 fake_reflog_parent(revs->reflog_info, commit);
3077 commit->object.flags &= ~(ADDED | SEEN | SHOWN);
3081 * If we haven't done the list limiting, we need to look at
3082 * the parents here. We also need to do the date-based limiting
3083 * that we'd otherwise have done in limit_list().
3085 if (!revs->limited) {
3086 if (revs->max_age != -1 &&
3087 (commit->date < revs->max_age))
3088 continue;
3089 if (add_parents_to_list(revs, commit, &revs->commits, NULL) < 0) {
3090 if (!revs->ignore_missing_links)
3091 die("Failed to traverse parents of commit %s",
3092 sha1_to_hex(commit->object.sha1));
3096 switch (simplify_commit(revs, commit)) {
3097 case commit_ignore:
3098 continue;
3099 case commit_error:
3100 die("Failed to simplify parents of commit %s",
3101 sha1_to_hex(commit->object.sha1));
3102 default:
3103 if (revs->track_linear)
3104 track_linear(revs, commit);
3105 return commit;
3107 } while (revs->commits);
3108 return NULL;
3112 * Return true for entries that have not yet been shown. (This is an
3113 * object_array_each_func_t.)
3115 static int entry_unshown(struct object_array_entry *entry, void *cb_data_unused)
3117 return !(entry->item->flags & SHOWN);
3121 * If array is on the verge of a realloc, garbage-collect any entries
3122 * that have already been shown to try to free up some space.
3124 static void gc_boundary(struct object_array *array)
3126 if (array->nr == array->alloc)
3127 object_array_filter(array, entry_unshown, NULL);
3130 static void create_boundary_commit_list(struct rev_info *revs)
3132 unsigned i;
3133 struct commit *c;
3134 struct object_array *array = &revs->boundary_commits;
3135 struct object_array_entry *objects = array->objects;
3138 * If revs->commits is non-NULL at this point, an error occurred in
3139 * get_revision_1(). Ignore the error and continue printing the
3140 * boundary commits anyway. (This is what the code has always
3141 * done.)
3143 if (revs->commits) {
3144 free_commit_list(revs->commits);
3145 revs->commits = NULL;
3149 * Put all of the actual boundary commits from revs->boundary_commits
3150 * into revs->commits
3152 for (i = 0; i < array->nr; i++) {
3153 c = (struct commit *)(objects[i].item);
3154 if (!c)
3155 continue;
3156 if (!(c->object.flags & CHILD_SHOWN))
3157 continue;
3158 if (c->object.flags & (SHOWN | BOUNDARY))
3159 continue;
3160 c->object.flags |= BOUNDARY;
3161 commit_list_insert(c, &revs->commits);
3165 * If revs->topo_order is set, sort the boundary commits
3166 * in topological order
3168 sort_in_topological_order(&revs->commits, revs->sort_order);
3171 static struct commit *get_revision_internal(struct rev_info *revs)
3173 struct commit *c = NULL;
3174 struct commit_list *l;
3176 if (revs->boundary == 2) {
3178 * All of the normal commits have already been returned,
3179 * and we are now returning boundary commits.
3180 * create_boundary_commit_list() has populated
3181 * revs->commits with the remaining commits to return.
3183 c = pop_commit(&revs->commits);
3184 if (c)
3185 c->object.flags |= SHOWN;
3186 return c;
3190 * If our max_count counter has reached zero, then we are done. We
3191 * don't simply return NULL because we still might need to show
3192 * boundary commits. But we want to avoid calling get_revision_1, which
3193 * might do a considerable amount of work finding the next commit only
3194 * for us to throw it away.
3196 * If it is non-zero, then either we don't have a max_count at all
3197 * (-1), or it is still counting, in which case we decrement.
3199 if (revs->max_count) {
3200 c = get_revision_1(revs);
3201 if (c) {
3202 while (revs->skip_count > 0) {
3203 revs->skip_count--;
3204 c = get_revision_1(revs);
3205 if (!c)
3206 break;
3210 if (revs->max_count > 0)
3211 revs->max_count--;
3214 if (c)
3215 c->object.flags |= SHOWN;
3217 if (!revs->boundary)
3218 return c;
3220 if (!c) {
3222 * get_revision_1() runs out the commits, and
3223 * we are done computing the boundaries.
3224 * switch to boundary commits output mode.
3226 revs->boundary = 2;
3229 * Update revs->commits to contain the list of
3230 * boundary commits.
3232 create_boundary_commit_list(revs);
3234 return get_revision_internal(revs);
3238 * boundary commits are the commits that are parents of the
3239 * ones we got from get_revision_1() but they themselves are
3240 * not returned from get_revision_1(). Before returning
3241 * 'c', we need to mark its parents that they could be boundaries.
3244 for (l = c->parents; l; l = l->next) {
3245 struct object *p;
3246 p = &(l->item->object);
3247 if (p->flags & (CHILD_SHOWN | SHOWN))
3248 continue;
3249 p->flags |= CHILD_SHOWN;
3250 gc_boundary(&revs->boundary_commits);
3251 add_object_array(p, NULL, &revs->boundary_commits);
3254 return c;
3257 struct commit *get_revision(struct rev_info *revs)
3259 struct commit *c;
3260 struct commit_list *reversed;
3262 if (revs->reverse) {
3263 reversed = NULL;
3264 while ((c = get_revision_internal(revs)))
3265 commit_list_insert(c, &reversed);
3266 revs->commits = reversed;
3267 revs->reverse = 0;
3268 revs->reverse_output_stage = 1;
3271 if (revs->reverse_output_stage) {
3272 c = pop_commit(&revs->commits);
3273 if (revs->track_linear)
3274 revs->linear = !!(c && c->object.flags & TRACK_LINEAR);
3275 return c;
3278 c = get_revision_internal(revs);
3279 if (c && revs->graph)
3280 graph_update(revs->graph, c);
3281 if (!c) {
3282 free_saved_parents(revs);
3283 if (revs->previous_parents) {
3284 free_commit_list(revs->previous_parents);
3285 revs->previous_parents = NULL;
3288 return c;
3291 char *get_revision_mark(const struct rev_info *revs, const struct commit *commit)
3293 if (commit->object.flags & BOUNDARY)
3294 return "-";
3295 else if (commit->object.flags & UNINTERESTING)
3296 return "^";
3297 else if (commit->object.flags & PATCHSAME)
3298 return "=";
3299 else if (!revs || revs->left_right) {
3300 if (commit->object.flags & SYMMETRIC_LEFT)
3301 return "<";
3302 else
3303 return ">";
3304 } else if (revs->graph)
3305 return "*";
3306 else if (revs->cherry_mark)
3307 return "+";
3308 return "";
3311 void put_revision_mark(const struct rev_info *revs, const struct commit *commit)
3313 char *mark = get_revision_mark(revs, commit);
3314 if (!strlen(mark))
3315 return;
3316 fputs(mark, stdout);
3317 putchar(' ');