Merge branch 'jk/path-name-safety-2.4' into jk/path-name-safety-2.5
[git/debian.git] / revision.c
blob8f30ab1e46b4a07148b7dfe0e8f7950fd241c325
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 void show_object_with_name(FILE *out, struct object *obj, const char *name)
26 const char *p;
28 fprintf(out, "%s ", sha1_to_hex(obj->sha1));
29 for (p = name; *p && *p != '\n'; p++)
30 fputc(*p, out);
31 fputc('\n', out);
34 static void mark_blob_uninteresting(struct blob *blob)
36 if (!blob)
37 return;
38 if (blob->object.flags & UNINTERESTING)
39 return;
40 blob->object.flags |= UNINTERESTING;
43 static void mark_tree_contents_uninteresting(struct tree *tree)
45 struct tree_desc desc;
46 struct name_entry entry;
47 struct object *obj = &tree->object;
49 if (!has_sha1_file(obj->sha1))
50 return;
51 if (parse_tree(tree) < 0)
52 die("bad tree %s", sha1_to_hex(obj->sha1));
54 init_tree_desc(&desc, tree->buffer, tree->size);
55 while (tree_entry(&desc, &entry)) {
56 switch (object_type(entry.mode)) {
57 case OBJ_TREE:
58 mark_tree_uninteresting(lookup_tree(entry.sha1));
59 break;
60 case OBJ_BLOB:
61 mark_blob_uninteresting(lookup_blob(entry.sha1));
62 break;
63 default:
64 /* Subproject commit - not in this repository */
65 break;
70 * We don't care about the tree any more
71 * after it has been marked uninteresting.
73 free_tree_buffer(tree);
76 void mark_tree_uninteresting(struct tree *tree)
78 struct object *obj = &tree->object;
80 if (!tree)
81 return;
82 if (obj->flags & UNINTERESTING)
83 return;
84 obj->flags |= UNINTERESTING;
85 mark_tree_contents_uninteresting(tree);
88 void mark_parents_uninteresting(struct commit *commit)
90 struct commit_list *parents = NULL, *l;
92 for (l = commit->parents; l; l = l->next)
93 commit_list_insert(l->item, &parents);
95 while (parents) {
96 struct commit *commit = parents->item;
97 l = parents;
98 parents = parents->next;
99 free(l);
101 while (commit) {
103 * A missing commit is ok iff its parent is marked
104 * uninteresting.
106 * We just mark such a thing parsed, so that when
107 * it is popped next time around, we won't be trying
108 * to parse it and get an error.
110 if (!has_sha1_file(commit->object.sha1))
111 commit->object.parsed = 1;
113 if (commit->object.flags & UNINTERESTING)
114 break;
116 commit->object.flags |= UNINTERESTING;
119 * Normally we haven't parsed the parent
120 * yet, so we won't have a parent of a parent
121 * here. However, it may turn out that we've
122 * reached this commit some other way (where it
123 * wasn't uninteresting), in which case we need
124 * to mark its parents recursively too..
126 if (!commit->parents)
127 break;
129 for (l = commit->parents->next; l; l = l->next)
130 commit_list_insert(l->item, &parents);
131 commit = commit->parents->item;
136 static void add_pending_object_with_path(struct rev_info *revs,
137 struct object *obj,
138 const char *name, unsigned mode,
139 const char *path)
141 if (!obj)
142 return;
143 if (revs->no_walk && (obj->flags & UNINTERESTING))
144 revs->no_walk = 0;
145 if (revs->reflog_info && obj->type == OBJ_COMMIT) {
146 struct strbuf buf = STRBUF_INIT;
147 int len = interpret_branch_name(name, 0, &buf);
148 int st;
150 if (0 < len && name[len] && buf.len)
151 strbuf_addstr(&buf, name + len);
152 st = add_reflog_for_walk(revs->reflog_info,
153 (struct commit *)obj,
154 buf.buf[0] ? buf.buf: name);
155 strbuf_release(&buf);
156 if (st)
157 return;
159 add_object_array_with_path(obj, name, &revs->pending, mode, path);
162 static void add_pending_object_with_mode(struct rev_info *revs,
163 struct object *obj,
164 const char *name, unsigned mode)
166 add_pending_object_with_path(revs, obj, name, mode, NULL);
169 void add_pending_object(struct rev_info *revs,
170 struct object *obj, const char *name)
172 add_pending_object_with_mode(revs, obj, name, S_IFINVALID);
175 void add_head_to_pending(struct rev_info *revs)
177 unsigned char sha1[20];
178 struct object *obj;
179 if (get_sha1("HEAD", sha1))
180 return;
181 obj = parse_object(sha1);
182 if (!obj)
183 return;
184 add_pending_object(revs, obj, "HEAD");
187 static struct object *get_reference(struct rev_info *revs, const char *name,
188 const unsigned char *sha1,
189 unsigned int flags)
191 struct object *object;
193 object = parse_object(sha1);
194 if (!object) {
195 if (revs->ignore_missing)
196 return object;
197 die("bad object %s", name);
199 object->flags |= flags;
200 return object;
203 void add_pending_sha1(struct rev_info *revs, const char *name,
204 const unsigned char *sha1, unsigned int flags)
206 struct object *object = get_reference(revs, name, sha1, flags);
207 add_pending_object(revs, object, name);
210 static struct commit *handle_commit(struct rev_info *revs,
211 struct object_array_entry *entry)
213 struct object *object = entry->item;
214 const char *name = entry->name;
215 const char *path = entry->path;
216 unsigned int mode = entry->mode;
217 unsigned long flags = object->flags;
220 * Tag object? Look what it points to..
222 while (object->type == OBJ_TAG) {
223 struct tag *tag = (struct tag *) object;
224 if (revs->tag_objects && !(flags & UNINTERESTING))
225 add_pending_object(revs, object, tag->tag);
226 if (!tag->tagged)
227 die("bad tag");
228 object = parse_object(tag->tagged->sha1);
229 if (!object) {
230 if (flags & UNINTERESTING)
231 return NULL;
232 die("bad object %s", sha1_to_hex(tag->tagged->sha1));
234 object->flags |= flags;
236 * We'll handle the tagged object by looping or dropping
237 * through to the non-tag handlers below. Do not
238 * propagate data from the tag's pending entry.
240 name = "";
241 path = NULL;
242 mode = 0;
246 * Commit object? Just return it, we'll do all the complex
247 * reachability crud.
249 if (object->type == OBJ_COMMIT) {
250 struct commit *commit = (struct commit *)object;
251 if (parse_commit(commit) < 0)
252 die("unable to parse commit %s", name);
253 if (flags & UNINTERESTING) {
254 mark_parents_uninteresting(commit);
255 revs->limited = 1;
257 if (revs->show_source && !commit->util)
258 commit->util = xstrdup(name);
259 return commit;
263 * Tree object? Either mark it uninteresting, or add it
264 * to the list of objects to look at later..
266 if (object->type == OBJ_TREE) {
267 struct tree *tree = (struct tree *)object;
268 if (!revs->tree_objects)
269 return NULL;
270 if (flags & UNINTERESTING) {
271 mark_tree_contents_uninteresting(tree);
272 return NULL;
274 add_pending_object_with_path(revs, object, name, mode, path);
275 return NULL;
279 * Blob object? You know the drill by now..
281 if (object->type == OBJ_BLOB) {
282 if (!revs->blob_objects)
283 return NULL;
284 if (flags & UNINTERESTING)
285 return NULL;
286 add_pending_object_with_path(revs, object, name, mode, path);
287 return NULL;
289 die("%s is unknown object", name);
292 static int everybody_uninteresting(struct commit_list *orig,
293 struct commit **interesting_cache)
295 struct commit_list *list = orig;
297 if (*interesting_cache) {
298 struct commit *commit = *interesting_cache;
299 if (!(commit->object.flags & UNINTERESTING))
300 return 0;
303 while (list) {
304 struct commit *commit = list->item;
305 list = list->next;
306 if (commit->object.flags & UNINTERESTING)
307 continue;
309 *interesting_cache = commit;
310 return 0;
312 return 1;
316 * A definition of "relevant" commit that we can use to simplify limited graphs
317 * by eliminating side branches.
319 * A "relevant" commit is one that is !UNINTERESTING (ie we are including it
320 * in our list), or that is a specified BOTTOM commit. Then after computing
321 * a limited list, during processing we can generally ignore boundary merges
322 * coming from outside the graph, (ie from irrelevant parents), and treat
323 * those merges as if they were single-parent. TREESAME is defined to consider
324 * only relevant parents, if any. If we are TREESAME to our on-graph parents,
325 * we don't care if we were !TREESAME to non-graph parents.
327 * Treating bottom commits as relevant ensures that a limited graph's
328 * connection to the actual bottom commit is not viewed as a side branch, but
329 * treated as part of the graph. For example:
331 * ....Z...A---X---o---o---B
332 * . /
333 * W---Y
335 * When computing "A..B", the A-X connection is at least as important as
336 * Y-X, despite A being flagged UNINTERESTING.
338 * And when computing --ancestry-path "A..B", the A-X connection is more
339 * important than Y-X, despite both A and Y being flagged UNINTERESTING.
341 static inline int relevant_commit(struct commit *commit)
343 return (commit->object.flags & (UNINTERESTING | BOTTOM)) != UNINTERESTING;
347 * Return a single relevant commit from a parent list. If we are a TREESAME
348 * commit, and this selects one of our parents, then we can safely simplify to
349 * that parent.
351 static struct commit *one_relevant_parent(const struct rev_info *revs,
352 struct commit_list *orig)
354 struct commit_list *list = orig;
355 struct commit *relevant = NULL;
357 if (!orig)
358 return NULL;
361 * For 1-parent commits, or if first-parent-only, then return that
362 * first parent (even if not "relevant" by the above definition).
363 * TREESAME will have been set purely on that parent.
365 if (revs->first_parent_only || !orig->next)
366 return orig->item;
369 * For multi-parent commits, identify a sole relevant parent, if any.
370 * If we have only one relevant parent, then TREESAME will be set purely
371 * with regard to that parent, and we can simplify accordingly.
373 * If we have more than one relevant parent, or no relevant parents
374 * (and multiple irrelevant ones), then we can't select a parent here
375 * and return NULL.
377 while (list) {
378 struct commit *commit = list->item;
379 list = list->next;
380 if (relevant_commit(commit)) {
381 if (relevant)
382 return NULL;
383 relevant = commit;
386 return relevant;
390 * The goal is to get REV_TREE_NEW as the result only if the
391 * diff consists of all '+' (and no other changes), REV_TREE_OLD
392 * if the whole diff is removal of old data, and otherwise
393 * REV_TREE_DIFFERENT (of course if the trees are the same we
394 * want REV_TREE_SAME).
395 * That means that once we get to REV_TREE_DIFFERENT, we do not
396 * have to look any further.
398 static int tree_difference = REV_TREE_SAME;
400 static void file_add_remove(struct diff_options *options,
401 int addremove, unsigned mode,
402 const unsigned char *sha1,
403 int sha1_valid,
404 const char *fullpath, unsigned dirty_submodule)
406 int diff = addremove == '+' ? REV_TREE_NEW : REV_TREE_OLD;
408 tree_difference |= diff;
409 if (tree_difference == REV_TREE_DIFFERENT)
410 DIFF_OPT_SET(options, HAS_CHANGES);
413 static void file_change(struct diff_options *options,
414 unsigned old_mode, unsigned new_mode,
415 const unsigned char *old_sha1,
416 const unsigned char *new_sha1,
417 int old_sha1_valid, int new_sha1_valid,
418 const char *fullpath,
419 unsigned old_dirty_submodule, unsigned new_dirty_submodule)
421 tree_difference = REV_TREE_DIFFERENT;
422 DIFF_OPT_SET(options, HAS_CHANGES);
425 static int rev_compare_tree(struct rev_info *revs,
426 struct commit *parent, struct commit *commit)
428 struct tree *t1 = parent->tree;
429 struct tree *t2 = commit->tree;
431 if (!t1)
432 return REV_TREE_NEW;
433 if (!t2)
434 return REV_TREE_OLD;
436 if (revs->simplify_by_decoration) {
438 * If we are simplifying by decoration, then the commit
439 * is worth showing if it has a tag pointing at it.
441 if (get_name_decoration(&commit->object))
442 return REV_TREE_DIFFERENT;
444 * A commit that is not pointed by a tag is uninteresting
445 * if we are not limited by path. This means that you will
446 * see the usual "commits that touch the paths" plus any
447 * tagged commit by specifying both --simplify-by-decoration
448 * and pathspec.
450 if (!revs->prune_data.nr)
451 return REV_TREE_SAME;
454 tree_difference = REV_TREE_SAME;
455 DIFF_OPT_CLR(&revs->pruning, HAS_CHANGES);
456 if (diff_tree_sha1(t1->object.sha1, t2->object.sha1, "",
457 &revs->pruning) < 0)
458 return REV_TREE_DIFFERENT;
459 return tree_difference;
462 static int rev_same_tree_as_empty(struct rev_info *revs, struct commit *commit)
464 int retval;
465 struct tree *t1 = commit->tree;
467 if (!t1)
468 return 0;
470 tree_difference = REV_TREE_SAME;
471 DIFF_OPT_CLR(&revs->pruning, HAS_CHANGES);
472 retval = diff_tree_sha1(NULL, t1->object.sha1, "", &revs->pruning);
474 return retval >= 0 && (tree_difference == REV_TREE_SAME);
477 struct treesame_state {
478 unsigned int nparents;
479 unsigned char treesame[FLEX_ARRAY];
482 static struct treesame_state *initialise_treesame(struct rev_info *revs, struct commit *commit)
484 unsigned n = commit_list_count(commit->parents);
485 struct treesame_state *st = xcalloc(1, sizeof(*st) + n);
486 st->nparents = n;
487 add_decoration(&revs->treesame, &commit->object, st);
488 return st;
492 * Must be called immediately after removing the nth_parent from a commit's
493 * parent list, if we are maintaining the per-parent treesame[] decoration.
494 * This does not recalculate the master TREESAME flag - update_treesame()
495 * should be called to update it after a sequence of treesame[] modifications
496 * that may have affected it.
498 static int compact_treesame(struct rev_info *revs, struct commit *commit, unsigned nth_parent)
500 struct treesame_state *st;
501 int old_same;
503 if (!commit->parents) {
505 * Have just removed the only parent from a non-merge.
506 * Different handling, as we lack decoration.
508 if (nth_parent != 0)
509 die("compact_treesame %u", nth_parent);
510 old_same = !!(commit->object.flags & TREESAME);
511 if (rev_same_tree_as_empty(revs, commit))
512 commit->object.flags |= TREESAME;
513 else
514 commit->object.flags &= ~TREESAME;
515 return old_same;
518 st = lookup_decoration(&revs->treesame, &commit->object);
519 if (!st || nth_parent >= st->nparents)
520 die("compact_treesame %u", nth_parent);
522 old_same = st->treesame[nth_parent];
523 memmove(st->treesame + nth_parent,
524 st->treesame + nth_parent + 1,
525 st->nparents - nth_parent - 1);
528 * If we've just become a non-merge commit, update TREESAME
529 * immediately, and remove the no-longer-needed decoration.
530 * If still a merge, defer update until update_treesame().
532 if (--st->nparents == 1) {
533 if (commit->parents->next)
534 die("compact_treesame parents mismatch");
535 if (st->treesame[0] && revs->dense)
536 commit->object.flags |= TREESAME;
537 else
538 commit->object.flags &= ~TREESAME;
539 free(add_decoration(&revs->treesame, &commit->object, NULL));
542 return old_same;
545 static unsigned update_treesame(struct rev_info *revs, struct commit *commit)
547 if (commit->parents && commit->parents->next) {
548 unsigned n;
549 struct treesame_state *st;
550 struct commit_list *p;
551 unsigned relevant_parents;
552 unsigned relevant_change, irrelevant_change;
554 st = lookup_decoration(&revs->treesame, &commit->object);
555 if (!st)
556 die("update_treesame %s", sha1_to_hex(commit->object.sha1));
557 relevant_parents = 0;
558 relevant_change = irrelevant_change = 0;
559 for (p = commit->parents, n = 0; p; n++, p = p->next) {
560 if (relevant_commit(p->item)) {
561 relevant_change |= !st->treesame[n];
562 relevant_parents++;
563 } else
564 irrelevant_change |= !st->treesame[n];
566 if (relevant_parents ? relevant_change : irrelevant_change)
567 commit->object.flags &= ~TREESAME;
568 else
569 commit->object.flags |= TREESAME;
572 return commit->object.flags & TREESAME;
575 static inline int limiting_can_increase_treesame(const struct rev_info *revs)
578 * TREESAME is irrelevant unless prune && dense;
579 * if simplify_history is set, we can't have a mixture of TREESAME and
580 * !TREESAME INTERESTING parents (and we don't have treesame[]
581 * decoration anyway);
582 * if first_parent_only is set, then the TREESAME flag is locked
583 * against the first parent (and again we lack treesame[] decoration).
585 return revs->prune && revs->dense &&
586 !revs->simplify_history &&
587 !revs->first_parent_only;
590 static void try_to_simplify_commit(struct rev_info *revs, struct commit *commit)
592 struct commit_list **pp, *parent;
593 struct treesame_state *ts = NULL;
594 int relevant_change = 0, irrelevant_change = 0;
595 int relevant_parents, nth_parent;
598 * If we don't do pruning, everything is interesting
600 if (!revs->prune)
601 return;
603 if (!commit->tree)
604 return;
606 if (!commit->parents) {
607 if (rev_same_tree_as_empty(revs, commit))
608 commit->object.flags |= TREESAME;
609 return;
613 * Normal non-merge commit? If we don't want to make the
614 * history dense, we consider it always to be a change..
616 if (!revs->dense && !commit->parents->next)
617 return;
619 for (pp = &commit->parents, nth_parent = 0, relevant_parents = 0;
620 (parent = *pp) != NULL;
621 pp = &parent->next, nth_parent++) {
622 struct commit *p = parent->item;
623 if (relevant_commit(p))
624 relevant_parents++;
626 if (nth_parent == 1) {
628 * This our second loop iteration - so we now know
629 * we're dealing with a merge.
631 * Do not compare with later parents when we care only about
632 * the first parent chain, in order to avoid derailing the
633 * traversal to follow a side branch that brought everything
634 * in the path we are limited to by the pathspec.
636 if (revs->first_parent_only)
637 break;
639 * If this will remain a potentially-simplifiable
640 * merge, remember per-parent treesame if needed.
641 * Initialise the array with the comparison from our
642 * first iteration.
644 if (revs->treesame.name &&
645 !revs->simplify_history &&
646 !(commit->object.flags & UNINTERESTING)) {
647 ts = initialise_treesame(revs, commit);
648 if (!(irrelevant_change || relevant_change))
649 ts->treesame[0] = 1;
652 if (parse_commit(p) < 0)
653 die("cannot simplify commit %s (because of %s)",
654 sha1_to_hex(commit->object.sha1),
655 sha1_to_hex(p->object.sha1));
656 switch (rev_compare_tree(revs, p, commit)) {
657 case REV_TREE_SAME:
658 if (!revs->simplify_history || !relevant_commit(p)) {
659 /* Even if a merge with an uninteresting
660 * side branch brought the entire change
661 * we are interested in, we do not want
662 * to lose the other branches of this
663 * merge, so we just keep going.
665 if (ts)
666 ts->treesame[nth_parent] = 1;
667 continue;
669 parent->next = NULL;
670 commit->parents = parent;
671 commit->object.flags |= TREESAME;
672 return;
674 case REV_TREE_NEW:
675 if (revs->remove_empty_trees &&
676 rev_same_tree_as_empty(revs, p)) {
677 /* We are adding all the specified
678 * paths from this parent, so the
679 * history beyond this parent is not
680 * interesting. Remove its parents
681 * (they are grandparents for us).
682 * IOW, we pretend this parent is a
683 * "root" commit.
685 if (parse_commit(p) < 0)
686 die("cannot simplify commit %s (invalid %s)",
687 sha1_to_hex(commit->object.sha1),
688 sha1_to_hex(p->object.sha1));
689 p->parents = NULL;
691 /* fallthrough */
692 case REV_TREE_OLD:
693 case REV_TREE_DIFFERENT:
694 if (relevant_commit(p))
695 relevant_change = 1;
696 else
697 irrelevant_change = 1;
698 continue;
700 die("bad tree compare for commit %s", sha1_to_hex(commit->object.sha1));
704 * TREESAME is straightforward for single-parent commits. For merge
705 * commits, it is most useful to define it so that "irrelevant"
706 * parents cannot make us !TREESAME - if we have any relevant
707 * parents, then we only consider TREESAMEness with respect to them,
708 * allowing irrelevant merges from uninteresting branches to be
709 * simplified away. Only if we have only irrelevant parents do we
710 * base TREESAME on them. Note that this logic is replicated in
711 * update_treesame, which should be kept in sync.
713 if (relevant_parents ? !relevant_change : !irrelevant_change)
714 commit->object.flags |= TREESAME;
717 static void commit_list_insert_by_date_cached(struct commit *p, struct commit_list **head,
718 struct commit_list *cached_base, struct commit_list **cache)
720 struct commit_list *new_entry;
722 if (cached_base && p->date < cached_base->item->date)
723 new_entry = commit_list_insert_by_date(p, &cached_base->next);
724 else
725 new_entry = commit_list_insert_by_date(p, head);
727 if (cache && (!*cache || p->date < (*cache)->item->date))
728 *cache = new_entry;
731 static int add_parents_to_list(struct rev_info *revs, struct commit *commit,
732 struct commit_list **list, struct commit_list **cache_ptr)
734 struct commit_list *parent = commit->parents;
735 unsigned left_flag;
736 struct commit_list *cached_base = cache_ptr ? *cache_ptr : NULL;
738 if (commit->object.flags & ADDED)
739 return 0;
740 commit->object.flags |= ADDED;
742 if (revs->include_check &&
743 !revs->include_check(commit, revs->include_check_data))
744 return 0;
747 * If the commit is uninteresting, don't try to
748 * prune parents - we want the maximal uninteresting
749 * set.
751 * Normally we haven't parsed the parent
752 * yet, so we won't have a parent of a parent
753 * here. However, it may turn out that we've
754 * reached this commit some other way (where it
755 * wasn't uninteresting), in which case we need
756 * to mark its parents recursively too..
758 if (commit->object.flags & UNINTERESTING) {
759 while (parent) {
760 struct commit *p = parent->item;
761 parent = parent->next;
762 if (p)
763 p->object.flags |= UNINTERESTING;
764 if (parse_commit_gently(p, 1) < 0)
765 continue;
766 if (p->parents)
767 mark_parents_uninteresting(p);
768 if (p->object.flags & SEEN)
769 continue;
770 p->object.flags |= SEEN;
771 commit_list_insert_by_date_cached(p, list, cached_base, cache_ptr);
773 return 0;
777 * Ok, the commit wasn't uninteresting. Try to
778 * simplify the commit history and find the parent
779 * that has no differences in the path set if one exists.
781 try_to_simplify_commit(revs, commit);
783 if (revs->no_walk)
784 return 0;
786 left_flag = (commit->object.flags & SYMMETRIC_LEFT);
788 for (parent = commit->parents; parent; parent = parent->next) {
789 struct commit *p = parent->item;
791 if (parse_commit_gently(p, revs->ignore_missing_links) < 0)
792 return -1;
793 if (revs->show_source && !p->util)
794 p->util = commit->util;
795 p->object.flags |= left_flag;
796 if (!(p->object.flags & SEEN)) {
797 p->object.flags |= SEEN;
798 commit_list_insert_by_date_cached(p, list, cached_base, cache_ptr);
800 if (revs->first_parent_only)
801 break;
803 return 0;
806 static void cherry_pick_list(struct commit_list *list, struct rev_info *revs)
808 struct commit_list *p;
809 int left_count = 0, right_count = 0;
810 int left_first;
811 struct patch_ids ids;
812 unsigned cherry_flag;
814 /* First count the commits on the left and on the right */
815 for (p = list; p; p = p->next) {
816 struct commit *commit = p->item;
817 unsigned flags = commit->object.flags;
818 if (flags & BOUNDARY)
820 else if (flags & SYMMETRIC_LEFT)
821 left_count++;
822 else
823 right_count++;
826 if (!left_count || !right_count)
827 return;
829 left_first = left_count < right_count;
830 init_patch_ids(&ids);
831 ids.diffopts.pathspec = revs->diffopt.pathspec;
833 /* Compute patch-ids for one side */
834 for (p = list; p; p = p->next) {
835 struct commit *commit = p->item;
836 unsigned flags = commit->object.flags;
838 if (flags & BOUNDARY)
839 continue;
841 * If we have fewer left, left_first is set and we omit
842 * commits on the right branch in this loop. If we have
843 * fewer right, we skip the left ones.
845 if (left_first != !!(flags & SYMMETRIC_LEFT))
846 continue;
847 commit->util = add_commit_patch_id(commit, &ids);
850 /* either cherry_mark or cherry_pick are true */
851 cherry_flag = revs->cherry_mark ? PATCHSAME : SHOWN;
853 /* Check the other side */
854 for (p = list; p; p = p->next) {
855 struct commit *commit = p->item;
856 struct patch_id *id;
857 unsigned flags = commit->object.flags;
859 if (flags & BOUNDARY)
860 continue;
862 * If we have fewer left, left_first is set and we omit
863 * commits on the left branch in this loop.
865 if (left_first == !!(flags & SYMMETRIC_LEFT))
866 continue;
869 * Have we seen the same patch id?
871 id = has_commit_patch_id(commit, &ids);
872 if (!id)
873 continue;
874 id->seen = 1;
875 commit->object.flags |= cherry_flag;
878 /* Now check the original side for seen ones */
879 for (p = list; p; p = p->next) {
880 struct commit *commit = p->item;
881 struct patch_id *ent;
883 ent = commit->util;
884 if (!ent)
885 continue;
886 if (ent->seen)
887 commit->object.flags |= cherry_flag;
888 commit->util = NULL;
891 free_patch_ids(&ids);
894 /* How many extra uninteresting commits we want to see.. */
895 #define SLOP 5
897 static int still_interesting(struct commit_list *src, unsigned long date, int slop,
898 struct commit **interesting_cache)
901 * No source list at all? We're definitely done..
903 if (!src)
904 return 0;
907 * Does the destination list contain entries with a date
908 * before the source list? Definitely _not_ done.
910 if (date <= src->item->date)
911 return SLOP;
914 * Does the source list still have interesting commits in
915 * it? Definitely not done..
917 if (!everybody_uninteresting(src, interesting_cache))
918 return SLOP;
920 /* Ok, we're closing in.. */
921 return slop-1;
925 * "rev-list --ancestry-path A..B" computes commits that are ancestors
926 * of B but not ancestors of A but further limits the result to those
927 * that are descendants of A. This takes the list of bottom commits and
928 * the result of "A..B" without --ancestry-path, and limits the latter
929 * further to the ones that can reach one of the commits in "bottom".
931 static void limit_to_ancestry(struct commit_list *bottom, struct commit_list *list)
933 struct commit_list *p;
934 struct commit_list *rlist = NULL;
935 int made_progress;
938 * Reverse the list so that it will be likely that we would
939 * process parents before children.
941 for (p = list; p; p = p->next)
942 commit_list_insert(p->item, &rlist);
944 for (p = bottom; p; p = p->next)
945 p->item->object.flags |= TMP_MARK;
948 * Mark the ones that can reach bottom commits in "list",
949 * in a bottom-up fashion.
951 do {
952 made_progress = 0;
953 for (p = rlist; p; p = p->next) {
954 struct commit *c = p->item;
955 struct commit_list *parents;
956 if (c->object.flags & (TMP_MARK | UNINTERESTING))
957 continue;
958 for (parents = c->parents;
959 parents;
960 parents = parents->next) {
961 if (!(parents->item->object.flags & TMP_MARK))
962 continue;
963 c->object.flags |= TMP_MARK;
964 made_progress = 1;
965 break;
968 } while (made_progress);
971 * NEEDSWORK: decide if we want to remove parents that are
972 * not marked with TMP_MARK from commit->parents for commits
973 * in the resulting list. We may not want to do that, though.
977 * The ones that are not marked with TMP_MARK are uninteresting
979 for (p = list; p; p = p->next) {
980 struct commit *c = p->item;
981 if (c->object.flags & TMP_MARK)
982 continue;
983 c->object.flags |= UNINTERESTING;
986 /* We are done with the TMP_MARK */
987 for (p = list; p; p = p->next)
988 p->item->object.flags &= ~TMP_MARK;
989 for (p = bottom; p; p = p->next)
990 p->item->object.flags &= ~TMP_MARK;
991 free_commit_list(rlist);
995 * Before walking the history, keep the set of "negative" refs the
996 * caller has asked to exclude.
998 * This is used to compute "rev-list --ancestry-path A..B", as we need
999 * to filter the result of "A..B" further to the ones that can actually
1000 * reach A.
1002 static struct commit_list *collect_bottom_commits(struct commit_list *list)
1004 struct commit_list *elem, *bottom = NULL;
1005 for (elem = list; elem; elem = elem->next)
1006 if (elem->item->object.flags & BOTTOM)
1007 commit_list_insert(elem->item, &bottom);
1008 return bottom;
1011 /* Assumes either left_only or right_only is set */
1012 static void limit_left_right(struct commit_list *list, struct rev_info *revs)
1014 struct commit_list *p;
1016 for (p = list; p; p = p->next) {
1017 struct commit *commit = p->item;
1019 if (revs->right_only) {
1020 if (commit->object.flags & SYMMETRIC_LEFT)
1021 commit->object.flags |= SHOWN;
1022 } else /* revs->left_only is set */
1023 if (!(commit->object.flags & SYMMETRIC_LEFT))
1024 commit->object.flags |= SHOWN;
1028 static int limit_list(struct rev_info *revs)
1030 int slop = SLOP;
1031 unsigned long date = ~0ul;
1032 struct commit_list *list = revs->commits;
1033 struct commit_list *newlist = NULL;
1034 struct commit_list **p = &newlist;
1035 struct commit_list *bottom = NULL;
1036 struct commit *interesting_cache = NULL;
1038 if (revs->ancestry_path) {
1039 bottom = collect_bottom_commits(list);
1040 if (!bottom)
1041 die("--ancestry-path given but there are no bottom commits");
1044 while (list) {
1045 struct commit_list *entry = list;
1046 struct commit *commit = list->item;
1047 struct object *obj = &commit->object;
1048 show_early_output_fn_t show;
1050 list = list->next;
1051 free(entry);
1053 if (commit == interesting_cache)
1054 interesting_cache = NULL;
1056 if (revs->max_age != -1 && (commit->date < revs->max_age))
1057 obj->flags |= UNINTERESTING;
1058 if (add_parents_to_list(revs, commit, &list, NULL) < 0)
1059 return -1;
1060 if (obj->flags & UNINTERESTING) {
1061 mark_parents_uninteresting(commit);
1062 if (revs->show_all)
1063 p = &commit_list_insert(commit, p)->next;
1064 slop = still_interesting(list, date, slop, &interesting_cache);
1065 if (slop)
1066 continue;
1067 /* If showing all, add the whole pending list to the end */
1068 if (revs->show_all)
1069 *p = list;
1070 break;
1072 if (revs->min_age != -1 && (commit->date > revs->min_age))
1073 continue;
1074 date = commit->date;
1075 p = &commit_list_insert(commit, p)->next;
1077 show = show_early_output;
1078 if (!show)
1079 continue;
1081 show(revs, newlist);
1082 show_early_output = NULL;
1084 if (revs->cherry_pick || revs->cherry_mark)
1085 cherry_pick_list(newlist, revs);
1087 if (revs->left_only || revs->right_only)
1088 limit_left_right(newlist, revs);
1090 if (bottom) {
1091 limit_to_ancestry(bottom, newlist);
1092 free_commit_list(bottom);
1096 * Check if any commits have become TREESAME by some of their parents
1097 * becoming UNINTERESTING.
1099 if (limiting_can_increase_treesame(revs))
1100 for (list = newlist; list; list = list->next) {
1101 struct commit *c = list->item;
1102 if (c->object.flags & (UNINTERESTING | TREESAME))
1103 continue;
1104 update_treesame(revs, c);
1107 revs->commits = newlist;
1108 return 0;
1112 * Add an entry to refs->cmdline with the specified information.
1113 * *name is copied.
1115 static void add_rev_cmdline(struct rev_info *revs,
1116 struct object *item,
1117 const char *name,
1118 int whence,
1119 unsigned flags)
1121 struct rev_cmdline_info *info = &revs->cmdline;
1122 int nr = info->nr;
1124 ALLOC_GROW(info->rev, nr + 1, info->alloc);
1125 info->rev[nr].item = item;
1126 info->rev[nr].name = xstrdup(name);
1127 info->rev[nr].whence = whence;
1128 info->rev[nr].flags = flags;
1129 info->nr++;
1132 static void add_rev_cmdline_list(struct rev_info *revs,
1133 struct commit_list *commit_list,
1134 int whence,
1135 unsigned flags)
1137 while (commit_list) {
1138 struct object *object = &commit_list->item->object;
1139 add_rev_cmdline(revs, object, sha1_to_hex(object->sha1),
1140 whence, flags);
1141 commit_list = commit_list->next;
1145 struct all_refs_cb {
1146 int all_flags;
1147 int warned_bad_reflog;
1148 struct rev_info *all_revs;
1149 const char *name_for_errormsg;
1152 int ref_excluded(struct string_list *ref_excludes, const char *path)
1154 struct string_list_item *item;
1156 if (!ref_excludes)
1157 return 0;
1158 for_each_string_list_item(item, ref_excludes) {
1159 if (!wildmatch(item->string, path, 0, NULL))
1160 return 1;
1162 return 0;
1165 static int handle_one_ref(const char *path, const struct object_id *oid,
1166 int flag, void *cb_data)
1168 struct all_refs_cb *cb = cb_data;
1169 struct object *object;
1171 if (ref_excluded(cb->all_revs->ref_excludes, path))
1172 return 0;
1174 object = get_reference(cb->all_revs, path, oid->hash, cb->all_flags);
1175 add_rev_cmdline(cb->all_revs, object, path, REV_CMD_REF, cb->all_flags);
1176 add_pending_sha1(cb->all_revs, path, oid->hash, cb->all_flags);
1177 return 0;
1180 static void init_all_refs_cb(struct all_refs_cb *cb, struct rev_info *revs,
1181 unsigned flags)
1183 cb->all_revs = revs;
1184 cb->all_flags = flags;
1187 void clear_ref_exclusion(struct string_list **ref_excludes_p)
1189 if (*ref_excludes_p) {
1190 string_list_clear(*ref_excludes_p, 0);
1191 free(*ref_excludes_p);
1193 *ref_excludes_p = NULL;
1196 void add_ref_exclusion(struct string_list **ref_excludes_p, const char *exclude)
1198 if (!*ref_excludes_p) {
1199 *ref_excludes_p = xcalloc(1, sizeof(**ref_excludes_p));
1200 (*ref_excludes_p)->strdup_strings = 1;
1202 string_list_append(*ref_excludes_p, exclude);
1205 static void handle_refs(const char *submodule, struct rev_info *revs, unsigned flags,
1206 int (*for_each)(const char *, each_ref_fn, void *))
1208 struct all_refs_cb cb;
1209 init_all_refs_cb(&cb, revs, flags);
1210 for_each(submodule, handle_one_ref, &cb);
1213 static void handle_one_reflog_commit(unsigned char *sha1, void *cb_data)
1215 struct all_refs_cb *cb = cb_data;
1216 if (!is_null_sha1(sha1)) {
1217 struct object *o = parse_object(sha1);
1218 if (o) {
1219 o->flags |= cb->all_flags;
1220 /* ??? CMDLINEFLAGS ??? */
1221 add_pending_object(cb->all_revs, o, "");
1223 else if (!cb->warned_bad_reflog) {
1224 warning("reflog of '%s' references pruned commits",
1225 cb->name_for_errormsg);
1226 cb->warned_bad_reflog = 1;
1231 static int handle_one_reflog_ent(unsigned char *osha1, unsigned char *nsha1,
1232 const char *email, unsigned long timestamp, int tz,
1233 const char *message, void *cb_data)
1235 handle_one_reflog_commit(osha1, cb_data);
1236 handle_one_reflog_commit(nsha1, cb_data);
1237 return 0;
1240 static int handle_one_reflog(const char *path, const struct object_id *oid,
1241 int flag, void *cb_data)
1243 struct all_refs_cb *cb = cb_data;
1244 cb->warned_bad_reflog = 0;
1245 cb->name_for_errormsg = path;
1246 for_each_reflog_ent(path, handle_one_reflog_ent, cb_data);
1247 return 0;
1250 void add_reflogs_to_pending(struct rev_info *revs, unsigned flags)
1252 struct all_refs_cb cb;
1254 cb.all_revs = revs;
1255 cb.all_flags = flags;
1256 for_each_reflog(handle_one_reflog, &cb);
1259 static void add_cache_tree(struct cache_tree *it, struct rev_info *revs,
1260 struct strbuf *path)
1262 size_t baselen = path->len;
1263 int i;
1265 if (it->entry_count >= 0) {
1266 struct tree *tree = lookup_tree(it->sha1);
1267 add_pending_object_with_path(revs, &tree->object, "",
1268 040000, path->buf);
1271 for (i = 0; i < it->subtree_nr; i++) {
1272 struct cache_tree_sub *sub = it->down[i];
1273 strbuf_addf(path, "%s%s", baselen ? "/" : "", sub->name);
1274 add_cache_tree(sub->cache_tree, revs, path);
1275 strbuf_setlen(path, baselen);
1280 void add_index_objects_to_pending(struct rev_info *revs, unsigned flags)
1282 int i;
1284 read_cache();
1285 for (i = 0; i < active_nr; i++) {
1286 struct cache_entry *ce = active_cache[i];
1287 struct blob *blob;
1289 if (S_ISGITLINK(ce->ce_mode))
1290 continue;
1292 blob = lookup_blob(ce->sha1);
1293 if (!blob)
1294 die("unable to add index blob to traversal");
1295 add_pending_object_with_path(revs, &blob->object, "",
1296 ce->ce_mode, ce->name);
1299 if (active_cache_tree) {
1300 struct strbuf path = STRBUF_INIT;
1301 add_cache_tree(active_cache_tree, revs, &path);
1302 strbuf_release(&path);
1306 static int add_parents_only(struct rev_info *revs, const char *arg_, int flags)
1308 unsigned char sha1[20];
1309 struct object *it;
1310 struct commit *commit;
1311 struct commit_list *parents;
1312 const char *arg = arg_;
1314 if (*arg == '^') {
1315 flags ^= UNINTERESTING | BOTTOM;
1316 arg++;
1318 if (get_sha1_committish(arg, sha1))
1319 return 0;
1320 while (1) {
1321 it = get_reference(revs, arg, sha1, 0);
1322 if (!it && revs->ignore_missing)
1323 return 0;
1324 if (it->type != OBJ_TAG)
1325 break;
1326 if (!((struct tag*)it)->tagged)
1327 return 0;
1328 hashcpy(sha1, ((struct tag*)it)->tagged->sha1);
1330 if (it->type != OBJ_COMMIT)
1331 return 0;
1332 commit = (struct commit *)it;
1333 for (parents = commit->parents; parents; parents = parents->next) {
1334 it = &parents->item->object;
1335 it->flags |= flags;
1336 add_rev_cmdline(revs, it, arg_, REV_CMD_PARENTS_ONLY, flags);
1337 add_pending_object(revs, it, arg);
1339 return 1;
1342 void init_revisions(struct rev_info *revs, const char *prefix)
1344 memset(revs, 0, sizeof(*revs));
1346 revs->abbrev = DEFAULT_ABBREV;
1347 revs->ignore_merges = 1;
1348 revs->simplify_history = 1;
1349 DIFF_OPT_SET(&revs->pruning, RECURSIVE);
1350 DIFF_OPT_SET(&revs->pruning, QUICK);
1351 revs->pruning.add_remove = file_add_remove;
1352 revs->pruning.change = file_change;
1353 revs->sort_order = REV_SORT_IN_GRAPH_ORDER;
1354 revs->dense = 1;
1355 revs->prefix = prefix;
1356 revs->max_age = -1;
1357 revs->min_age = -1;
1358 revs->skip_count = -1;
1359 revs->max_count = -1;
1360 revs->max_parents = -1;
1362 revs->commit_format = CMIT_FMT_DEFAULT;
1364 init_grep_defaults();
1365 grep_init(&revs->grep_filter, prefix);
1366 revs->grep_filter.status_only = 1;
1367 revs->grep_filter.regflags = REG_NEWLINE;
1369 diff_setup(&revs->diffopt);
1370 if (prefix && !revs->diffopt.prefix) {
1371 revs->diffopt.prefix = prefix;
1372 revs->diffopt.prefix_length = strlen(prefix);
1375 revs->notes_opt.use_default_notes = -1;
1378 static void add_pending_commit_list(struct rev_info *revs,
1379 struct commit_list *commit_list,
1380 unsigned int flags)
1382 while (commit_list) {
1383 struct object *object = &commit_list->item->object;
1384 object->flags |= flags;
1385 add_pending_object(revs, object, sha1_to_hex(object->sha1));
1386 commit_list = commit_list->next;
1390 static void prepare_show_merge(struct rev_info *revs)
1392 struct commit_list *bases;
1393 struct commit *head, *other;
1394 unsigned char sha1[20];
1395 const char **prune = NULL;
1396 int i, prune_num = 1; /* counting terminating NULL */
1398 if (get_sha1("HEAD", sha1))
1399 die("--merge without HEAD?");
1400 head = lookup_commit_or_die(sha1, "HEAD");
1401 if (get_sha1("MERGE_HEAD", sha1))
1402 die("--merge without MERGE_HEAD?");
1403 other = lookup_commit_or_die(sha1, "MERGE_HEAD");
1404 add_pending_object(revs, &head->object, "HEAD");
1405 add_pending_object(revs, &other->object, "MERGE_HEAD");
1406 bases = get_merge_bases(head, other);
1407 add_rev_cmdline_list(revs, bases, REV_CMD_MERGE_BASE, UNINTERESTING | BOTTOM);
1408 add_pending_commit_list(revs, bases, UNINTERESTING | BOTTOM);
1409 free_commit_list(bases);
1410 head->object.flags |= SYMMETRIC_LEFT;
1412 if (!active_nr)
1413 read_cache();
1414 for (i = 0; i < active_nr; i++) {
1415 const struct cache_entry *ce = active_cache[i];
1416 if (!ce_stage(ce))
1417 continue;
1418 if (ce_path_match(ce, &revs->prune_data, NULL)) {
1419 prune_num++;
1420 REALLOC_ARRAY(prune, prune_num);
1421 prune[prune_num-2] = ce->name;
1422 prune[prune_num-1] = NULL;
1424 while ((i+1 < active_nr) &&
1425 ce_same_name(ce, active_cache[i+1]))
1426 i++;
1428 free_pathspec(&revs->prune_data);
1429 parse_pathspec(&revs->prune_data, PATHSPEC_ALL_MAGIC & ~PATHSPEC_LITERAL,
1430 PATHSPEC_PREFER_FULL | PATHSPEC_LITERAL_PATH, "", prune);
1431 revs->limited = 1;
1434 int handle_revision_arg(const char *arg_, struct rev_info *revs, int flags, unsigned revarg_opt)
1436 struct object_context oc;
1437 char *dotdot;
1438 struct object *object;
1439 unsigned char sha1[20];
1440 int local_flags;
1441 const char *arg = arg_;
1442 int cant_be_filename = revarg_opt & REVARG_CANNOT_BE_FILENAME;
1443 unsigned get_sha1_flags = 0;
1445 flags = flags & UNINTERESTING ? flags | BOTTOM : flags & ~BOTTOM;
1447 dotdot = strstr(arg, "..");
1448 if (dotdot) {
1449 unsigned char from_sha1[20];
1450 const char *next = dotdot + 2;
1451 const char *this = arg;
1452 int symmetric = *next == '.';
1453 unsigned int flags_exclude = flags ^ (UNINTERESTING | BOTTOM);
1454 static const char head_by_default[] = "HEAD";
1455 unsigned int a_flags;
1457 *dotdot = 0;
1458 next += symmetric;
1460 if (!*next)
1461 next = head_by_default;
1462 if (dotdot == arg)
1463 this = head_by_default;
1464 if (this == head_by_default && next == head_by_default &&
1465 !symmetric) {
1467 * Just ".."? That is not a range but the
1468 * pathspec for the parent directory.
1470 if (!cant_be_filename) {
1471 *dotdot = '.';
1472 return -1;
1475 if (!get_sha1_committish(this, from_sha1) &&
1476 !get_sha1_committish(next, sha1)) {
1477 struct object *a_obj, *b_obj;
1479 if (!cant_be_filename) {
1480 *dotdot = '.';
1481 verify_non_filename(revs->prefix, arg);
1484 a_obj = parse_object(from_sha1);
1485 b_obj = parse_object(sha1);
1486 if (!a_obj || !b_obj) {
1487 missing:
1488 if (revs->ignore_missing)
1489 return 0;
1490 die(symmetric
1491 ? "Invalid symmetric difference expression %s"
1492 : "Invalid revision range %s", arg);
1495 if (!symmetric) {
1496 /* just A..B */
1497 a_flags = flags_exclude;
1498 } else {
1499 /* A...B -- find merge bases between the two */
1500 struct commit *a, *b;
1501 struct commit_list *exclude;
1503 a = (a_obj->type == OBJ_COMMIT
1504 ? (struct commit *)a_obj
1505 : lookup_commit_reference(a_obj->sha1));
1506 b = (b_obj->type == OBJ_COMMIT
1507 ? (struct commit *)b_obj
1508 : lookup_commit_reference(b_obj->sha1));
1509 if (!a || !b)
1510 goto missing;
1511 exclude = get_merge_bases(a, b);
1512 add_rev_cmdline_list(revs, exclude,
1513 REV_CMD_MERGE_BASE,
1514 flags_exclude);
1515 add_pending_commit_list(revs, exclude,
1516 flags_exclude);
1517 free_commit_list(exclude);
1519 a_flags = flags | SYMMETRIC_LEFT;
1522 a_obj->flags |= a_flags;
1523 b_obj->flags |= flags;
1524 add_rev_cmdline(revs, a_obj, this,
1525 REV_CMD_LEFT, a_flags);
1526 add_rev_cmdline(revs, b_obj, next,
1527 REV_CMD_RIGHT, flags);
1528 add_pending_object(revs, a_obj, this);
1529 add_pending_object(revs, b_obj, next);
1530 return 0;
1532 *dotdot = '.';
1534 dotdot = strstr(arg, "^@");
1535 if (dotdot && !dotdot[2]) {
1536 *dotdot = 0;
1537 if (add_parents_only(revs, arg, flags))
1538 return 0;
1539 *dotdot = '^';
1541 dotdot = strstr(arg, "^!");
1542 if (dotdot && !dotdot[2]) {
1543 *dotdot = 0;
1544 if (!add_parents_only(revs, arg, flags ^ (UNINTERESTING | BOTTOM)))
1545 *dotdot = '^';
1548 local_flags = 0;
1549 if (*arg == '^') {
1550 local_flags = UNINTERESTING | BOTTOM;
1551 arg++;
1554 if (revarg_opt & REVARG_COMMITTISH)
1555 get_sha1_flags = GET_SHA1_COMMITTISH;
1557 if (get_sha1_with_context(arg, get_sha1_flags, sha1, &oc))
1558 return revs->ignore_missing ? 0 : -1;
1559 if (!cant_be_filename)
1560 verify_non_filename(revs->prefix, arg);
1561 object = get_reference(revs, arg, sha1, flags ^ local_flags);
1562 add_rev_cmdline(revs, object, arg_, REV_CMD_REV, flags ^ local_flags);
1563 add_pending_object_with_mode(revs, object, arg, oc.mode);
1564 return 0;
1567 struct cmdline_pathspec {
1568 int alloc;
1569 int nr;
1570 const char **path;
1573 static void append_prune_data(struct cmdline_pathspec *prune, const char **av)
1575 while (*av) {
1576 ALLOC_GROW(prune->path, prune->nr + 1, prune->alloc);
1577 prune->path[prune->nr++] = *(av++);
1581 static void read_pathspec_from_stdin(struct rev_info *revs, struct strbuf *sb,
1582 struct cmdline_pathspec *prune)
1584 while (strbuf_getwholeline(sb, stdin, '\n') != EOF) {
1585 int len = sb->len;
1586 if (len && sb->buf[len - 1] == '\n')
1587 sb->buf[--len] = '\0';
1588 ALLOC_GROW(prune->path, prune->nr + 1, prune->alloc);
1589 prune->path[prune->nr++] = xstrdup(sb->buf);
1593 static void read_revisions_from_stdin(struct rev_info *revs,
1594 struct cmdline_pathspec *prune)
1596 struct strbuf sb;
1597 int seen_dashdash = 0;
1598 int save_warning;
1600 save_warning = warn_on_object_refname_ambiguity;
1601 warn_on_object_refname_ambiguity = 0;
1603 strbuf_init(&sb, 1000);
1604 while (strbuf_getwholeline(&sb, stdin, '\n') != EOF) {
1605 int len = sb.len;
1606 if (len && sb.buf[len - 1] == '\n')
1607 sb.buf[--len] = '\0';
1608 if (!len)
1609 break;
1610 if (sb.buf[0] == '-') {
1611 if (len == 2 && sb.buf[1] == '-') {
1612 seen_dashdash = 1;
1613 break;
1615 die("options not supported in --stdin mode");
1617 if (handle_revision_arg(sb.buf, revs, 0,
1618 REVARG_CANNOT_BE_FILENAME))
1619 die("bad revision '%s'", sb.buf);
1621 if (seen_dashdash)
1622 read_pathspec_from_stdin(revs, &sb, prune);
1624 strbuf_release(&sb);
1625 warn_on_object_refname_ambiguity = save_warning;
1628 static void add_grep(struct rev_info *revs, const char *ptn, enum grep_pat_token what)
1630 append_grep_pattern(&revs->grep_filter, ptn, "command line", 0, what);
1633 static void add_header_grep(struct rev_info *revs, enum grep_header_field field, const char *pattern)
1635 append_header_grep_pattern(&revs->grep_filter, field, pattern);
1638 static void add_message_grep(struct rev_info *revs, const char *pattern)
1640 add_grep(revs, pattern, GREP_PATTERN_BODY);
1643 static int handle_revision_opt(struct rev_info *revs, int argc, const char **argv,
1644 int *unkc, const char **unkv)
1646 const char *arg = argv[0];
1647 const char *optarg;
1648 int argcount;
1650 /* pseudo revision arguments */
1651 if (!strcmp(arg, "--all") || !strcmp(arg, "--branches") ||
1652 !strcmp(arg, "--tags") || !strcmp(arg, "--remotes") ||
1653 !strcmp(arg, "--reflog") || !strcmp(arg, "--not") ||
1654 !strcmp(arg, "--no-walk") || !strcmp(arg, "--do-walk") ||
1655 !strcmp(arg, "--bisect") || starts_with(arg, "--glob=") ||
1656 !strcmp(arg, "--indexed-objects") ||
1657 starts_with(arg, "--exclude=") ||
1658 starts_with(arg, "--branches=") || starts_with(arg, "--tags=") ||
1659 starts_with(arg, "--remotes=") || starts_with(arg, "--no-walk="))
1661 unkv[(*unkc)++] = arg;
1662 return 1;
1665 if ((argcount = parse_long_opt("max-count", argv, &optarg))) {
1666 revs->max_count = atoi(optarg);
1667 revs->no_walk = 0;
1668 return argcount;
1669 } else if ((argcount = parse_long_opt("skip", argv, &optarg))) {
1670 revs->skip_count = atoi(optarg);
1671 return argcount;
1672 } else if ((*arg == '-') && isdigit(arg[1])) {
1673 /* accept -<digit>, like traditional "head" */
1674 if (strtol_i(arg + 1, 10, &revs->max_count) < 0 ||
1675 revs->max_count < 0)
1676 die("'%s': not a non-negative integer", arg + 1);
1677 revs->no_walk = 0;
1678 } else if (!strcmp(arg, "-n")) {
1679 if (argc <= 1)
1680 return error("-n requires an argument");
1681 revs->max_count = atoi(argv[1]);
1682 revs->no_walk = 0;
1683 return 2;
1684 } else if (starts_with(arg, "-n")) {
1685 revs->max_count = atoi(arg + 2);
1686 revs->no_walk = 0;
1687 } else if ((argcount = parse_long_opt("max-age", argv, &optarg))) {
1688 revs->max_age = atoi(optarg);
1689 return argcount;
1690 } else if ((argcount = parse_long_opt("since", argv, &optarg))) {
1691 revs->max_age = approxidate(optarg);
1692 return argcount;
1693 } else if ((argcount = parse_long_opt("after", argv, &optarg))) {
1694 revs->max_age = approxidate(optarg);
1695 return argcount;
1696 } else if ((argcount = parse_long_opt("min-age", argv, &optarg))) {
1697 revs->min_age = atoi(optarg);
1698 return argcount;
1699 } else if ((argcount = parse_long_opt("before", argv, &optarg))) {
1700 revs->min_age = approxidate(optarg);
1701 return argcount;
1702 } else if ((argcount = parse_long_opt("until", argv, &optarg))) {
1703 revs->min_age = approxidate(optarg);
1704 return argcount;
1705 } else if (!strcmp(arg, "--first-parent")) {
1706 revs->first_parent_only = 1;
1707 } else if (!strcmp(arg, "--ancestry-path")) {
1708 revs->ancestry_path = 1;
1709 revs->simplify_history = 0;
1710 revs->limited = 1;
1711 } else if (!strcmp(arg, "-g") || !strcmp(arg, "--walk-reflogs")) {
1712 init_reflog_walk(&revs->reflog_info);
1713 } else if (!strcmp(arg, "--default")) {
1714 if (argc <= 1)
1715 return error("bad --default argument");
1716 revs->def = argv[1];
1717 return 2;
1718 } else if (!strcmp(arg, "--merge")) {
1719 revs->show_merge = 1;
1720 } else if (!strcmp(arg, "--topo-order")) {
1721 revs->sort_order = REV_SORT_IN_GRAPH_ORDER;
1722 revs->topo_order = 1;
1723 } else if (!strcmp(arg, "--simplify-merges")) {
1724 revs->simplify_merges = 1;
1725 revs->topo_order = 1;
1726 revs->rewrite_parents = 1;
1727 revs->simplify_history = 0;
1728 revs->limited = 1;
1729 } else if (!strcmp(arg, "--simplify-by-decoration")) {
1730 revs->simplify_merges = 1;
1731 revs->topo_order = 1;
1732 revs->rewrite_parents = 1;
1733 revs->simplify_history = 0;
1734 revs->simplify_by_decoration = 1;
1735 revs->limited = 1;
1736 revs->prune = 1;
1737 load_ref_decorations(DECORATE_SHORT_REFS);
1738 } else if (!strcmp(arg, "--date-order")) {
1739 revs->sort_order = REV_SORT_BY_COMMIT_DATE;
1740 revs->topo_order = 1;
1741 } else if (!strcmp(arg, "--author-date-order")) {
1742 revs->sort_order = REV_SORT_BY_AUTHOR_DATE;
1743 revs->topo_order = 1;
1744 } else if (starts_with(arg, "--early-output")) {
1745 int count = 100;
1746 switch (arg[14]) {
1747 case '=':
1748 count = atoi(arg+15);
1749 /* Fallthrough */
1750 case 0:
1751 revs->topo_order = 1;
1752 revs->early_output = count;
1754 } else if (!strcmp(arg, "--parents")) {
1755 revs->rewrite_parents = 1;
1756 revs->print_parents = 1;
1757 } else if (!strcmp(arg, "--dense")) {
1758 revs->dense = 1;
1759 } else if (!strcmp(arg, "--sparse")) {
1760 revs->dense = 0;
1761 } else if (!strcmp(arg, "--show-all")) {
1762 revs->show_all = 1;
1763 } else if (!strcmp(arg, "--remove-empty")) {
1764 revs->remove_empty_trees = 1;
1765 } else if (!strcmp(arg, "--merges")) {
1766 revs->min_parents = 2;
1767 } else if (!strcmp(arg, "--no-merges")) {
1768 revs->max_parents = 1;
1769 } else if (starts_with(arg, "--min-parents=")) {
1770 revs->min_parents = atoi(arg+14);
1771 } else if (starts_with(arg, "--no-min-parents")) {
1772 revs->min_parents = 0;
1773 } else if (starts_with(arg, "--max-parents=")) {
1774 revs->max_parents = atoi(arg+14);
1775 } else if (starts_with(arg, "--no-max-parents")) {
1776 revs->max_parents = -1;
1777 } else if (!strcmp(arg, "--boundary")) {
1778 revs->boundary = 1;
1779 } else if (!strcmp(arg, "--left-right")) {
1780 revs->left_right = 1;
1781 } else if (!strcmp(arg, "--left-only")) {
1782 if (revs->right_only)
1783 die("--left-only is incompatible with --right-only"
1784 " or --cherry");
1785 revs->left_only = 1;
1786 } else if (!strcmp(arg, "--right-only")) {
1787 if (revs->left_only)
1788 die("--right-only is incompatible with --left-only");
1789 revs->right_only = 1;
1790 } else if (!strcmp(arg, "--cherry")) {
1791 if (revs->left_only)
1792 die("--cherry is incompatible with --left-only");
1793 revs->cherry_mark = 1;
1794 revs->right_only = 1;
1795 revs->max_parents = 1;
1796 revs->limited = 1;
1797 } else if (!strcmp(arg, "--count")) {
1798 revs->count = 1;
1799 } else if (!strcmp(arg, "--cherry-mark")) {
1800 if (revs->cherry_pick)
1801 die("--cherry-mark is incompatible with --cherry-pick");
1802 revs->cherry_mark = 1;
1803 revs->limited = 1; /* needs limit_list() */
1804 } else if (!strcmp(arg, "--cherry-pick")) {
1805 if (revs->cherry_mark)
1806 die("--cherry-pick is incompatible with --cherry-mark");
1807 revs->cherry_pick = 1;
1808 revs->limited = 1;
1809 } else if (!strcmp(arg, "--objects")) {
1810 revs->tag_objects = 1;
1811 revs->tree_objects = 1;
1812 revs->blob_objects = 1;
1813 } else if (!strcmp(arg, "--objects-edge")) {
1814 revs->tag_objects = 1;
1815 revs->tree_objects = 1;
1816 revs->blob_objects = 1;
1817 revs->edge_hint = 1;
1818 } else if (!strcmp(arg, "--objects-edge-aggressive")) {
1819 revs->tag_objects = 1;
1820 revs->tree_objects = 1;
1821 revs->blob_objects = 1;
1822 revs->edge_hint = 1;
1823 revs->edge_hint_aggressive = 1;
1824 } else if (!strcmp(arg, "--verify-objects")) {
1825 revs->tag_objects = 1;
1826 revs->tree_objects = 1;
1827 revs->blob_objects = 1;
1828 revs->verify_objects = 1;
1829 } else if (!strcmp(arg, "--unpacked")) {
1830 revs->unpacked = 1;
1831 } else if (starts_with(arg, "--unpacked=")) {
1832 die("--unpacked=<packfile> no longer supported.");
1833 } else if (!strcmp(arg, "-r")) {
1834 revs->diff = 1;
1835 DIFF_OPT_SET(&revs->diffopt, RECURSIVE);
1836 } else if (!strcmp(arg, "-t")) {
1837 revs->diff = 1;
1838 DIFF_OPT_SET(&revs->diffopt, RECURSIVE);
1839 DIFF_OPT_SET(&revs->diffopt, TREE_IN_RECURSIVE);
1840 } else if (!strcmp(arg, "-m")) {
1841 revs->ignore_merges = 0;
1842 } else if (!strcmp(arg, "-c")) {
1843 revs->diff = 1;
1844 revs->dense_combined_merges = 0;
1845 revs->combine_merges = 1;
1846 } else if (!strcmp(arg, "--cc")) {
1847 revs->diff = 1;
1848 revs->dense_combined_merges = 1;
1849 revs->combine_merges = 1;
1850 } else if (!strcmp(arg, "-v")) {
1851 revs->verbose_header = 1;
1852 } else if (!strcmp(arg, "--pretty")) {
1853 revs->verbose_header = 1;
1854 revs->pretty_given = 1;
1855 get_commit_format(NULL, revs);
1856 } else if (starts_with(arg, "--pretty=") || starts_with(arg, "--format=")) {
1858 * Detached form ("--pretty X" as opposed to "--pretty=X")
1859 * not allowed, since the argument is optional.
1861 revs->verbose_header = 1;
1862 revs->pretty_given = 1;
1863 get_commit_format(arg+9, revs);
1864 } else if (!strcmp(arg, "--show-notes") || !strcmp(arg, "--notes")) {
1865 revs->show_notes = 1;
1866 revs->show_notes_given = 1;
1867 revs->notes_opt.use_default_notes = 1;
1868 } else if (!strcmp(arg, "--show-signature")) {
1869 revs->show_signature = 1;
1870 } else if (!strcmp(arg, "--show-linear-break") ||
1871 starts_with(arg, "--show-linear-break=")) {
1872 if (starts_with(arg, "--show-linear-break="))
1873 revs->break_bar = xstrdup(arg + 20);
1874 else
1875 revs->break_bar = " ..........";
1876 revs->track_linear = 1;
1877 revs->track_first_time = 1;
1878 } else if (starts_with(arg, "--show-notes=") ||
1879 starts_with(arg, "--notes=")) {
1880 struct strbuf buf = STRBUF_INIT;
1881 revs->show_notes = 1;
1882 revs->show_notes_given = 1;
1883 if (starts_with(arg, "--show-notes")) {
1884 if (revs->notes_opt.use_default_notes < 0)
1885 revs->notes_opt.use_default_notes = 1;
1886 strbuf_addstr(&buf, arg+13);
1888 else
1889 strbuf_addstr(&buf, arg+8);
1890 expand_notes_ref(&buf);
1891 string_list_append(&revs->notes_opt.extra_notes_refs,
1892 strbuf_detach(&buf, NULL));
1893 } else if (!strcmp(arg, "--no-notes")) {
1894 revs->show_notes = 0;
1895 revs->show_notes_given = 1;
1896 revs->notes_opt.use_default_notes = -1;
1897 /* we have been strdup'ing ourselves, so trick
1898 * string_list into free()ing strings */
1899 revs->notes_opt.extra_notes_refs.strdup_strings = 1;
1900 string_list_clear(&revs->notes_opt.extra_notes_refs, 0);
1901 revs->notes_opt.extra_notes_refs.strdup_strings = 0;
1902 } else if (!strcmp(arg, "--standard-notes")) {
1903 revs->show_notes_given = 1;
1904 revs->notes_opt.use_default_notes = 1;
1905 } else if (!strcmp(arg, "--no-standard-notes")) {
1906 revs->notes_opt.use_default_notes = 0;
1907 } else if (!strcmp(arg, "--oneline")) {
1908 revs->verbose_header = 1;
1909 get_commit_format("oneline", revs);
1910 revs->pretty_given = 1;
1911 revs->abbrev_commit = 1;
1912 } else if (!strcmp(arg, "--graph")) {
1913 revs->topo_order = 1;
1914 revs->rewrite_parents = 1;
1915 revs->graph = graph_init(revs);
1916 } else if (!strcmp(arg, "--root")) {
1917 revs->show_root_diff = 1;
1918 } else if (!strcmp(arg, "--no-commit-id")) {
1919 revs->no_commit_id = 1;
1920 } else if (!strcmp(arg, "--always")) {
1921 revs->always_show_header = 1;
1922 } else if (!strcmp(arg, "--no-abbrev")) {
1923 revs->abbrev = 0;
1924 } else if (!strcmp(arg, "--abbrev")) {
1925 revs->abbrev = DEFAULT_ABBREV;
1926 } else if (starts_with(arg, "--abbrev=")) {
1927 revs->abbrev = strtoul(arg + 9, NULL, 10);
1928 if (revs->abbrev < MINIMUM_ABBREV)
1929 revs->abbrev = MINIMUM_ABBREV;
1930 else if (revs->abbrev > 40)
1931 revs->abbrev = 40;
1932 } else if (!strcmp(arg, "--abbrev-commit")) {
1933 revs->abbrev_commit = 1;
1934 revs->abbrev_commit_given = 1;
1935 } else if (!strcmp(arg, "--no-abbrev-commit")) {
1936 revs->abbrev_commit = 0;
1937 } else if (!strcmp(arg, "--full-diff")) {
1938 revs->diff = 1;
1939 revs->full_diff = 1;
1940 } else if (!strcmp(arg, "--full-history")) {
1941 revs->simplify_history = 0;
1942 } else if (!strcmp(arg, "--relative-date")) {
1943 revs->date_mode = DATE_RELATIVE;
1944 revs->date_mode_explicit = 1;
1945 } else if ((argcount = parse_long_opt("date", argv, &optarg))) {
1946 revs->date_mode = parse_date_format(optarg);
1947 revs->date_mode_explicit = 1;
1948 return argcount;
1949 } else if (!strcmp(arg, "--log-size")) {
1950 revs->show_log_size = 1;
1953 * Grepping the commit log
1955 else if ((argcount = parse_long_opt("author", argv, &optarg))) {
1956 add_header_grep(revs, GREP_HEADER_AUTHOR, optarg);
1957 return argcount;
1958 } else if ((argcount = parse_long_opt("committer", argv, &optarg))) {
1959 add_header_grep(revs, GREP_HEADER_COMMITTER, optarg);
1960 return argcount;
1961 } else if ((argcount = parse_long_opt("grep-reflog", argv, &optarg))) {
1962 add_header_grep(revs, GREP_HEADER_REFLOG, optarg);
1963 return argcount;
1964 } else if ((argcount = parse_long_opt("grep", argv, &optarg))) {
1965 add_message_grep(revs, optarg);
1966 return argcount;
1967 } else if (!strcmp(arg, "--grep-debug")) {
1968 revs->grep_filter.debug = 1;
1969 } else if (!strcmp(arg, "--basic-regexp")) {
1970 grep_set_pattern_type_option(GREP_PATTERN_TYPE_BRE, &revs->grep_filter);
1971 } else if (!strcmp(arg, "--extended-regexp") || !strcmp(arg, "-E")) {
1972 grep_set_pattern_type_option(GREP_PATTERN_TYPE_ERE, &revs->grep_filter);
1973 } else if (!strcmp(arg, "--regexp-ignore-case") || !strcmp(arg, "-i")) {
1974 revs->grep_filter.regflags |= REG_ICASE;
1975 DIFF_OPT_SET(&revs->diffopt, PICKAXE_IGNORE_CASE);
1976 } else if (!strcmp(arg, "--fixed-strings") || !strcmp(arg, "-F")) {
1977 grep_set_pattern_type_option(GREP_PATTERN_TYPE_FIXED, &revs->grep_filter);
1978 } else if (!strcmp(arg, "--perl-regexp")) {
1979 grep_set_pattern_type_option(GREP_PATTERN_TYPE_PCRE, &revs->grep_filter);
1980 } else if (!strcmp(arg, "--all-match")) {
1981 revs->grep_filter.all_match = 1;
1982 } else if (!strcmp(arg, "--invert-grep")) {
1983 revs->invert_grep = 1;
1984 } else if ((argcount = parse_long_opt("encoding", argv, &optarg))) {
1985 if (strcmp(optarg, "none"))
1986 git_log_output_encoding = xstrdup(optarg);
1987 else
1988 git_log_output_encoding = "";
1989 return argcount;
1990 } else if (!strcmp(arg, "--reverse")) {
1991 revs->reverse ^= 1;
1992 } else if (!strcmp(arg, "--children")) {
1993 revs->children.name = "children";
1994 revs->limited = 1;
1995 } else if (!strcmp(arg, "--ignore-missing")) {
1996 revs->ignore_missing = 1;
1997 } else {
1998 int opts = diff_opt_parse(&revs->diffopt, argv, argc);
1999 if (!opts)
2000 unkv[(*unkc)++] = arg;
2001 return opts;
2003 if (revs->graph && revs->track_linear)
2004 die("--show-linear-break and --graph are incompatible");
2006 return 1;
2009 void parse_revision_opt(struct rev_info *revs, struct parse_opt_ctx_t *ctx,
2010 const struct option *options,
2011 const char * const usagestr[])
2013 int n = handle_revision_opt(revs, ctx->argc, ctx->argv,
2014 &ctx->cpidx, ctx->out);
2015 if (n <= 0) {
2016 error("unknown option `%s'", ctx->argv[0]);
2017 usage_with_options(usagestr, options);
2019 ctx->argv += n;
2020 ctx->argc -= n;
2023 static int for_each_bad_bisect_ref(const char *submodule, each_ref_fn fn, void *cb_data)
2025 return for_each_ref_in_submodule(submodule, "refs/bisect/bad", fn, cb_data);
2028 static int for_each_good_bisect_ref(const char *submodule, each_ref_fn fn, void *cb_data)
2030 return for_each_ref_in_submodule(submodule, "refs/bisect/good", fn, cb_data);
2033 static int handle_revision_pseudo_opt(const char *submodule,
2034 struct rev_info *revs,
2035 int argc, const char **argv, int *flags)
2037 const char *arg = argv[0];
2038 const char *optarg;
2039 int argcount;
2042 * NOTE!
2044 * Commands like "git shortlog" will not accept the options below
2045 * unless parse_revision_opt queues them (as opposed to erroring
2046 * out).
2048 * When implementing your new pseudo-option, remember to
2049 * register it in the list at the top of handle_revision_opt.
2051 if (!strcmp(arg, "--all")) {
2052 handle_refs(submodule, revs, *flags, for_each_ref_submodule);
2053 handle_refs(submodule, revs, *flags, head_ref_submodule);
2054 clear_ref_exclusion(&revs->ref_excludes);
2055 } else if (!strcmp(arg, "--branches")) {
2056 handle_refs(submodule, revs, *flags, for_each_branch_ref_submodule);
2057 clear_ref_exclusion(&revs->ref_excludes);
2058 } else if (!strcmp(arg, "--bisect")) {
2059 handle_refs(submodule, revs, *flags, for_each_bad_bisect_ref);
2060 handle_refs(submodule, revs, *flags ^ (UNINTERESTING | BOTTOM), for_each_good_bisect_ref);
2061 revs->bisect = 1;
2062 } else if (!strcmp(arg, "--tags")) {
2063 handle_refs(submodule, revs, *flags, for_each_tag_ref_submodule);
2064 clear_ref_exclusion(&revs->ref_excludes);
2065 } else if (!strcmp(arg, "--remotes")) {
2066 handle_refs(submodule, revs, *flags, for_each_remote_ref_submodule);
2067 clear_ref_exclusion(&revs->ref_excludes);
2068 } else if ((argcount = parse_long_opt("glob", argv, &optarg))) {
2069 struct all_refs_cb cb;
2070 init_all_refs_cb(&cb, revs, *flags);
2071 for_each_glob_ref(handle_one_ref, optarg, &cb);
2072 clear_ref_exclusion(&revs->ref_excludes);
2073 return argcount;
2074 } else if ((argcount = parse_long_opt("exclude", argv, &optarg))) {
2075 add_ref_exclusion(&revs->ref_excludes, optarg);
2076 return argcount;
2077 } else if (starts_with(arg, "--branches=")) {
2078 struct all_refs_cb cb;
2079 init_all_refs_cb(&cb, revs, *flags);
2080 for_each_glob_ref_in(handle_one_ref, arg + 11, "refs/heads/", &cb);
2081 clear_ref_exclusion(&revs->ref_excludes);
2082 } else if (starts_with(arg, "--tags=")) {
2083 struct all_refs_cb cb;
2084 init_all_refs_cb(&cb, revs, *flags);
2085 for_each_glob_ref_in(handle_one_ref, arg + 7, "refs/tags/", &cb);
2086 clear_ref_exclusion(&revs->ref_excludes);
2087 } else if (starts_with(arg, "--remotes=")) {
2088 struct all_refs_cb cb;
2089 init_all_refs_cb(&cb, revs, *flags);
2090 for_each_glob_ref_in(handle_one_ref, arg + 10, "refs/remotes/", &cb);
2091 clear_ref_exclusion(&revs->ref_excludes);
2092 } else if (!strcmp(arg, "--reflog")) {
2093 add_reflogs_to_pending(revs, *flags);
2094 } else if (!strcmp(arg, "--indexed-objects")) {
2095 add_index_objects_to_pending(revs, *flags);
2096 } else if (!strcmp(arg, "--not")) {
2097 *flags ^= UNINTERESTING | BOTTOM;
2098 } else if (!strcmp(arg, "--no-walk")) {
2099 revs->no_walk = REVISION_WALK_NO_WALK_SORTED;
2100 } else if (starts_with(arg, "--no-walk=")) {
2102 * Detached form ("--no-walk X" as opposed to "--no-walk=X")
2103 * not allowed, since the argument is optional.
2105 if (!strcmp(arg + 10, "sorted"))
2106 revs->no_walk = REVISION_WALK_NO_WALK_SORTED;
2107 else if (!strcmp(arg + 10, "unsorted"))
2108 revs->no_walk = REVISION_WALK_NO_WALK_UNSORTED;
2109 else
2110 return error("invalid argument to --no-walk");
2111 } else if (!strcmp(arg, "--do-walk")) {
2112 revs->no_walk = 0;
2113 } else {
2114 return 0;
2117 return 1;
2120 static void NORETURN diagnose_missing_default(const char *def)
2122 unsigned char sha1[20];
2123 int flags;
2124 const char *refname;
2126 refname = resolve_ref_unsafe(def, 0, sha1, &flags);
2127 if (!refname || !(flags & REF_ISSYMREF) || (flags & REF_ISBROKEN))
2128 die(_("your current branch appears to be broken"));
2130 skip_prefix(refname, "refs/heads/", &refname);
2131 die(_("your current branch '%s' does not have any commits yet"),
2132 refname);
2136 * Parse revision information, filling in the "rev_info" structure,
2137 * and removing the used arguments from the argument list.
2139 * Returns the number of arguments left that weren't recognized
2140 * (which are also moved to the head of the argument list)
2142 int setup_revisions(int argc, const char **argv, struct rev_info *revs, struct setup_revision_opt *opt)
2144 int i, flags, left, seen_dashdash, read_from_stdin, got_rev_arg = 0, revarg_opt;
2145 struct cmdline_pathspec prune_data;
2146 const char *submodule = NULL;
2148 memset(&prune_data, 0, sizeof(prune_data));
2149 if (opt)
2150 submodule = opt->submodule;
2152 /* First, search for "--" */
2153 if (opt && opt->assume_dashdash) {
2154 seen_dashdash = 1;
2155 } else {
2156 seen_dashdash = 0;
2157 for (i = 1; i < argc; i++) {
2158 const char *arg = argv[i];
2159 if (strcmp(arg, "--"))
2160 continue;
2161 argv[i] = NULL;
2162 argc = i;
2163 if (argv[i + 1])
2164 append_prune_data(&prune_data, argv + i + 1);
2165 seen_dashdash = 1;
2166 break;
2170 /* Second, deal with arguments and options */
2171 flags = 0;
2172 revarg_opt = opt ? opt->revarg_opt : 0;
2173 if (seen_dashdash)
2174 revarg_opt |= REVARG_CANNOT_BE_FILENAME;
2175 read_from_stdin = 0;
2176 for (left = i = 1; i < argc; i++) {
2177 const char *arg = argv[i];
2178 if (*arg == '-') {
2179 int opts;
2181 opts = handle_revision_pseudo_opt(submodule,
2182 revs, argc - i, argv + i,
2183 &flags);
2184 if (opts > 0) {
2185 i += opts - 1;
2186 continue;
2189 if (!strcmp(arg, "--stdin")) {
2190 if (revs->disable_stdin) {
2191 argv[left++] = arg;
2192 continue;
2194 if (read_from_stdin++)
2195 die("--stdin given twice?");
2196 read_revisions_from_stdin(revs, &prune_data);
2197 continue;
2200 opts = handle_revision_opt(revs, argc - i, argv + i, &left, argv);
2201 if (opts > 0) {
2202 i += opts - 1;
2203 continue;
2205 if (opts < 0)
2206 exit(128);
2207 continue;
2211 if (handle_revision_arg(arg, revs, flags, revarg_opt)) {
2212 int j;
2213 if (seen_dashdash || *arg == '^')
2214 die("bad revision '%s'", arg);
2216 /* If we didn't have a "--":
2217 * (1) all filenames must exist;
2218 * (2) all rev-args must not be interpretable
2219 * as a valid filename.
2220 * but the latter we have checked in the main loop.
2222 for (j = i; j < argc; j++)
2223 verify_filename(revs->prefix, argv[j], j == i);
2225 append_prune_data(&prune_data, argv + i);
2226 break;
2228 else
2229 got_rev_arg = 1;
2232 if (prune_data.nr) {
2234 * If we need to introduce the magic "a lone ':' means no
2235 * pathspec whatsoever", here is the place to do so.
2237 * if (prune_data.nr == 1 && !strcmp(prune_data[0], ":")) {
2238 * prune_data.nr = 0;
2239 * prune_data.alloc = 0;
2240 * free(prune_data.path);
2241 * prune_data.path = NULL;
2242 * } else {
2243 * terminate prune_data.alloc with NULL and
2244 * call init_pathspec() to set revs->prune_data here.
2247 ALLOC_GROW(prune_data.path, prune_data.nr + 1, prune_data.alloc);
2248 prune_data.path[prune_data.nr++] = NULL;
2249 parse_pathspec(&revs->prune_data, 0, 0,
2250 revs->prefix, prune_data.path);
2253 if (revs->def == NULL)
2254 revs->def = opt ? opt->def : NULL;
2255 if (opt && opt->tweak)
2256 opt->tweak(revs, opt);
2257 if (revs->show_merge)
2258 prepare_show_merge(revs);
2259 if (revs->def && !revs->pending.nr && !got_rev_arg) {
2260 unsigned char sha1[20];
2261 struct object *object;
2262 struct object_context oc;
2263 if (get_sha1_with_context(revs->def, 0, sha1, &oc))
2264 diagnose_missing_default(revs->def);
2265 object = get_reference(revs, revs->def, sha1, 0);
2266 add_pending_object_with_mode(revs, object, revs->def, oc.mode);
2269 /* Did the user ask for any diff output? Run the diff! */
2270 if (revs->diffopt.output_format & ~DIFF_FORMAT_NO_OUTPUT)
2271 revs->diff = 1;
2273 /* Pickaxe, diff-filter and rename following need diffs */
2274 if (revs->diffopt.pickaxe ||
2275 revs->diffopt.filter ||
2276 DIFF_OPT_TST(&revs->diffopt, FOLLOW_RENAMES))
2277 revs->diff = 1;
2279 if (revs->topo_order)
2280 revs->limited = 1;
2282 if (revs->prune_data.nr) {
2283 copy_pathspec(&revs->pruning.pathspec, &revs->prune_data);
2284 /* Can't prune commits with rename following: the paths change.. */
2285 if (!DIFF_OPT_TST(&revs->diffopt, FOLLOW_RENAMES))
2286 revs->prune = 1;
2287 if (!revs->full_diff)
2288 copy_pathspec(&revs->diffopt.pathspec,
2289 &revs->prune_data);
2291 if (revs->combine_merges)
2292 revs->ignore_merges = 0;
2293 revs->diffopt.abbrev = revs->abbrev;
2295 if (revs->line_level_traverse) {
2296 revs->limited = 1;
2297 revs->topo_order = 1;
2300 diff_setup_done(&revs->diffopt);
2302 grep_commit_pattern_type(GREP_PATTERN_TYPE_UNSPECIFIED,
2303 &revs->grep_filter);
2304 compile_grep_patterns(&revs->grep_filter);
2306 if (revs->reverse && revs->reflog_info)
2307 die("cannot combine --reverse with --walk-reflogs");
2308 if (revs->rewrite_parents && revs->children.name)
2309 die("cannot combine --parents and --children");
2312 * Limitations on the graph functionality
2314 if (revs->reverse && revs->graph)
2315 die("cannot combine --reverse with --graph");
2317 if (revs->reflog_info && revs->graph)
2318 die("cannot combine --walk-reflogs with --graph");
2319 if (revs->no_walk && revs->graph)
2320 die("cannot combine --no-walk with --graph");
2321 if (!revs->reflog_info && revs->grep_filter.use_reflog_filter)
2322 die("cannot use --grep-reflog without --walk-reflogs");
2324 if (revs->first_parent_only && revs->bisect)
2325 die(_("--first-parent is incompatible with --bisect"));
2327 return left;
2330 static void add_child(struct rev_info *revs, struct commit *parent, struct commit *child)
2332 struct commit_list *l = xcalloc(1, sizeof(*l));
2334 l->item = child;
2335 l->next = add_decoration(&revs->children, &parent->object, l);
2338 static int remove_duplicate_parents(struct rev_info *revs, struct commit *commit)
2340 struct treesame_state *ts = lookup_decoration(&revs->treesame, &commit->object);
2341 struct commit_list **pp, *p;
2342 int surviving_parents;
2344 /* Examine existing parents while marking ones we have seen... */
2345 pp = &commit->parents;
2346 surviving_parents = 0;
2347 while ((p = *pp) != NULL) {
2348 struct commit *parent = p->item;
2349 if (parent->object.flags & TMP_MARK) {
2350 *pp = p->next;
2351 if (ts)
2352 compact_treesame(revs, commit, surviving_parents);
2353 continue;
2355 parent->object.flags |= TMP_MARK;
2356 surviving_parents++;
2357 pp = &p->next;
2359 /* clear the temporary mark */
2360 for (p = commit->parents; p; p = p->next) {
2361 p->item->object.flags &= ~TMP_MARK;
2363 /* no update_treesame() - removing duplicates can't affect TREESAME */
2364 return surviving_parents;
2367 struct merge_simplify_state {
2368 struct commit *simplified;
2371 static struct merge_simplify_state *locate_simplify_state(struct rev_info *revs, struct commit *commit)
2373 struct merge_simplify_state *st;
2375 st = lookup_decoration(&revs->merge_simplification, &commit->object);
2376 if (!st) {
2377 st = xcalloc(1, sizeof(*st));
2378 add_decoration(&revs->merge_simplification, &commit->object, st);
2380 return st;
2383 static int mark_redundant_parents(struct rev_info *revs, struct commit *commit)
2385 struct commit_list *h = reduce_heads(commit->parents);
2386 int i = 0, marked = 0;
2387 struct commit_list *po, *pn;
2389 /* Want these for sanity-checking only */
2390 int orig_cnt = commit_list_count(commit->parents);
2391 int cnt = commit_list_count(h);
2394 * Not ready to remove items yet, just mark them for now, based
2395 * on the output of reduce_heads(). reduce_heads outputs the reduced
2396 * set in its original order, so this isn't too hard.
2398 po = commit->parents;
2399 pn = h;
2400 while (po) {
2401 if (pn && po->item == pn->item) {
2402 pn = pn->next;
2403 i++;
2404 } else {
2405 po->item->object.flags |= TMP_MARK;
2406 marked++;
2408 po=po->next;
2411 if (i != cnt || cnt+marked != orig_cnt)
2412 die("mark_redundant_parents %d %d %d %d", orig_cnt, cnt, i, marked);
2414 free_commit_list(h);
2416 return marked;
2419 static int mark_treesame_root_parents(struct rev_info *revs, struct commit *commit)
2421 struct commit_list *p;
2422 int marked = 0;
2424 for (p = commit->parents; p; p = p->next) {
2425 struct commit *parent = p->item;
2426 if (!parent->parents && (parent->object.flags & TREESAME)) {
2427 parent->object.flags |= TMP_MARK;
2428 marked++;
2432 return marked;
2436 * Awkward naming - this means one parent we are TREESAME to.
2437 * cf mark_treesame_root_parents: root parents that are TREESAME (to an
2438 * empty tree). Better name suggestions?
2440 static int leave_one_treesame_to_parent(struct rev_info *revs, struct commit *commit)
2442 struct treesame_state *ts = lookup_decoration(&revs->treesame, &commit->object);
2443 struct commit *unmarked = NULL, *marked = NULL;
2444 struct commit_list *p;
2445 unsigned n;
2447 for (p = commit->parents, n = 0; p; p = p->next, n++) {
2448 if (ts->treesame[n]) {
2449 if (p->item->object.flags & TMP_MARK) {
2450 if (!marked)
2451 marked = p->item;
2452 } else {
2453 if (!unmarked) {
2454 unmarked = p->item;
2455 break;
2462 * If we are TREESAME to a marked-for-deletion parent, but not to any
2463 * unmarked parents, unmark the first TREESAME parent. This is the
2464 * parent that the default simplify_history==1 scan would have followed,
2465 * and it doesn't make sense to omit that path when asking for a
2466 * simplified full history. Retaining it improves the chances of
2467 * understanding odd missed merges that took an old version of a file.
2469 * Example:
2471 * I--------*X A modified the file, but mainline merge X used
2472 * \ / "-s ours", so took the version from I. X is
2473 * `-*A--' TREESAME to I and !TREESAME to A.
2475 * Default log from X would produce "I". Without this check,
2476 * --full-history --simplify-merges would produce "I-A-X", showing
2477 * the merge commit X and that it changed A, but not making clear that
2478 * it had just taken the I version. With this check, the topology above
2479 * is retained.
2481 * Note that it is possible that the simplification chooses a different
2482 * TREESAME parent from the default, in which case this test doesn't
2483 * activate, and we _do_ drop the default parent. Example:
2485 * I------X A modified the file, but it was reverted in B,
2486 * \ / meaning mainline merge X is TREESAME to both
2487 * *A-*B parents.
2489 * Default log would produce "I" by following the first parent;
2490 * --full-history --simplify-merges will produce "I-A-B". But this is a
2491 * reasonable result - it presents a logical full history leading from
2492 * I to X, and X is not an important merge.
2494 if (!unmarked && marked) {
2495 marked->object.flags &= ~TMP_MARK;
2496 return 1;
2499 return 0;
2502 static int remove_marked_parents(struct rev_info *revs, struct commit *commit)
2504 struct commit_list **pp, *p;
2505 int nth_parent, removed = 0;
2507 pp = &commit->parents;
2508 nth_parent = 0;
2509 while ((p = *pp) != NULL) {
2510 struct commit *parent = p->item;
2511 if (parent->object.flags & TMP_MARK) {
2512 parent->object.flags &= ~TMP_MARK;
2513 *pp = p->next;
2514 free(p);
2515 removed++;
2516 compact_treesame(revs, commit, nth_parent);
2517 continue;
2519 pp = &p->next;
2520 nth_parent++;
2523 /* Removing parents can only increase TREESAMEness */
2524 if (removed && !(commit->object.flags & TREESAME))
2525 update_treesame(revs, commit);
2527 return nth_parent;
2530 static struct commit_list **simplify_one(struct rev_info *revs, struct commit *commit, struct commit_list **tail)
2532 struct commit_list *p;
2533 struct commit *parent;
2534 struct merge_simplify_state *st, *pst;
2535 int cnt;
2537 st = locate_simplify_state(revs, commit);
2540 * Have we handled this one?
2542 if (st->simplified)
2543 return tail;
2546 * An UNINTERESTING commit simplifies to itself, so does a
2547 * root commit. We do not rewrite parents of such commit
2548 * anyway.
2550 if ((commit->object.flags & UNINTERESTING) || !commit->parents) {
2551 st->simplified = commit;
2552 return tail;
2556 * Do we know what commit all of our parents that matter
2557 * should be rewritten to? Otherwise we are not ready to
2558 * rewrite this one yet.
2560 for (cnt = 0, p = commit->parents; p; p = p->next) {
2561 pst = locate_simplify_state(revs, p->item);
2562 if (!pst->simplified) {
2563 tail = &commit_list_insert(p->item, tail)->next;
2564 cnt++;
2566 if (revs->first_parent_only)
2567 break;
2569 if (cnt) {
2570 tail = &commit_list_insert(commit, tail)->next;
2571 return tail;
2575 * Rewrite our list of parents. Note that this cannot
2576 * affect our TREESAME flags in any way - a commit is
2577 * always TREESAME to its simplification.
2579 for (p = commit->parents; p; p = p->next) {
2580 pst = locate_simplify_state(revs, p->item);
2581 p->item = pst->simplified;
2582 if (revs->first_parent_only)
2583 break;
2586 if (revs->first_parent_only)
2587 cnt = 1;
2588 else
2589 cnt = remove_duplicate_parents(revs, commit);
2592 * It is possible that we are a merge and one side branch
2593 * does not have any commit that touches the given paths;
2594 * in such a case, the immediate parent from that branch
2595 * will be rewritten to be the merge base.
2597 * o----X X: the commit we are looking at;
2598 * / / o: a commit that touches the paths;
2599 * ---o----'
2601 * Further, a merge of an independent branch that doesn't
2602 * touch the path will reduce to a treesame root parent:
2604 * ----o----X X: the commit we are looking at;
2605 * / o: a commit that touches the paths;
2606 * r r: a root commit not touching the paths
2608 * Detect and simplify both cases.
2610 if (1 < cnt) {
2611 int marked = mark_redundant_parents(revs, commit);
2612 marked += mark_treesame_root_parents(revs, commit);
2613 if (marked)
2614 marked -= leave_one_treesame_to_parent(revs, commit);
2615 if (marked)
2616 cnt = remove_marked_parents(revs, commit);
2620 * A commit simplifies to itself if it is a root, if it is
2621 * UNINTERESTING, if it touches the given paths, or if it is a
2622 * merge and its parents don't simplify to one relevant commit
2623 * (the first two cases are already handled at the beginning of
2624 * this function).
2626 * Otherwise, it simplifies to what its sole relevant parent
2627 * simplifies to.
2629 if (!cnt ||
2630 (commit->object.flags & UNINTERESTING) ||
2631 !(commit->object.flags & TREESAME) ||
2632 (parent = one_relevant_parent(revs, commit->parents)) == NULL)
2633 st->simplified = commit;
2634 else {
2635 pst = locate_simplify_state(revs, parent);
2636 st->simplified = pst->simplified;
2638 return tail;
2641 static void simplify_merges(struct rev_info *revs)
2643 struct commit_list *list, *next;
2644 struct commit_list *yet_to_do, **tail;
2645 struct commit *commit;
2647 if (!revs->prune)
2648 return;
2650 /* feed the list reversed */
2651 yet_to_do = NULL;
2652 for (list = revs->commits; list; list = next) {
2653 commit = list->item;
2654 next = list->next;
2656 * Do not free(list) here yet; the original list
2657 * is used later in this function.
2659 commit_list_insert(commit, &yet_to_do);
2661 while (yet_to_do) {
2662 list = yet_to_do;
2663 yet_to_do = NULL;
2664 tail = &yet_to_do;
2665 while (list) {
2666 commit = list->item;
2667 next = list->next;
2668 free(list);
2669 list = next;
2670 tail = simplify_one(revs, commit, tail);
2674 /* clean up the result, removing the simplified ones */
2675 list = revs->commits;
2676 revs->commits = NULL;
2677 tail = &revs->commits;
2678 while (list) {
2679 struct merge_simplify_state *st;
2681 commit = list->item;
2682 next = list->next;
2683 free(list);
2684 list = next;
2685 st = locate_simplify_state(revs, commit);
2686 if (st->simplified == commit)
2687 tail = &commit_list_insert(commit, tail)->next;
2691 static void set_children(struct rev_info *revs)
2693 struct commit_list *l;
2694 for (l = revs->commits; l; l = l->next) {
2695 struct commit *commit = l->item;
2696 struct commit_list *p;
2698 for (p = commit->parents; p; p = p->next)
2699 add_child(revs, p->item, commit);
2703 void reset_revision_walk(void)
2705 clear_object_flags(SEEN | ADDED | SHOWN);
2708 int prepare_revision_walk(struct rev_info *revs)
2710 int i;
2711 struct object_array old_pending;
2712 struct commit_list **next = &revs->commits;
2714 memcpy(&old_pending, &revs->pending, sizeof(old_pending));
2715 revs->pending.nr = 0;
2716 revs->pending.alloc = 0;
2717 revs->pending.objects = NULL;
2718 for (i = 0; i < old_pending.nr; i++) {
2719 struct object_array_entry *e = old_pending.objects + i;
2720 struct commit *commit = handle_commit(revs, e);
2721 if (commit) {
2722 if (!(commit->object.flags & SEEN)) {
2723 commit->object.flags |= SEEN;
2724 next = commit_list_append(commit, next);
2728 if (!revs->leak_pending)
2729 object_array_clear(&old_pending);
2731 /* Signal whether we need per-parent treesame decoration */
2732 if (revs->simplify_merges ||
2733 (revs->limited && limiting_can_increase_treesame(revs)))
2734 revs->treesame.name = "treesame";
2736 if (revs->no_walk != REVISION_WALK_NO_WALK_UNSORTED)
2737 commit_list_sort_by_date(&revs->commits);
2738 if (revs->no_walk)
2739 return 0;
2740 if (revs->limited)
2741 if (limit_list(revs) < 0)
2742 return -1;
2743 if (revs->topo_order)
2744 sort_in_topological_order(&revs->commits, revs->sort_order);
2745 if (revs->line_level_traverse)
2746 line_log_filter(revs);
2747 if (revs->simplify_merges)
2748 simplify_merges(revs);
2749 if (revs->children.name)
2750 set_children(revs);
2751 return 0;
2754 static enum rewrite_result rewrite_one(struct rev_info *revs, struct commit **pp)
2756 struct commit_list *cache = NULL;
2758 for (;;) {
2759 struct commit *p = *pp;
2760 if (!revs->limited)
2761 if (add_parents_to_list(revs, p, &revs->commits, &cache) < 0)
2762 return rewrite_one_error;
2763 if (p->object.flags & UNINTERESTING)
2764 return rewrite_one_ok;
2765 if (!(p->object.flags & TREESAME))
2766 return rewrite_one_ok;
2767 if (!p->parents)
2768 return rewrite_one_noparents;
2769 if ((p = one_relevant_parent(revs, p->parents)) == NULL)
2770 return rewrite_one_ok;
2771 *pp = p;
2775 int rewrite_parents(struct rev_info *revs, struct commit *commit,
2776 rewrite_parent_fn_t rewrite_parent)
2778 struct commit_list **pp = &commit->parents;
2779 while (*pp) {
2780 struct commit_list *parent = *pp;
2781 switch (rewrite_parent(revs, &parent->item)) {
2782 case rewrite_one_ok:
2783 break;
2784 case rewrite_one_noparents:
2785 *pp = parent->next;
2786 continue;
2787 case rewrite_one_error:
2788 return -1;
2790 pp = &parent->next;
2792 remove_duplicate_parents(revs, commit);
2793 return 0;
2796 static int commit_rewrite_person(struct strbuf *buf, const char *what, struct string_list *mailmap)
2798 char *person, *endp;
2799 size_t len, namelen, maillen;
2800 const char *name;
2801 const char *mail;
2802 struct ident_split ident;
2804 person = strstr(buf->buf, what);
2805 if (!person)
2806 return 0;
2808 person += strlen(what);
2809 endp = strchr(person, '\n');
2810 if (!endp)
2811 return 0;
2813 len = endp - person;
2815 if (split_ident_line(&ident, person, len))
2816 return 0;
2818 mail = ident.mail_begin;
2819 maillen = ident.mail_end - ident.mail_begin;
2820 name = ident.name_begin;
2821 namelen = ident.name_end - ident.name_begin;
2823 if (map_user(mailmap, &mail, &maillen, &name, &namelen)) {
2824 struct strbuf namemail = STRBUF_INIT;
2826 strbuf_addf(&namemail, "%.*s <%.*s>",
2827 (int)namelen, name, (int)maillen, mail);
2829 strbuf_splice(buf, ident.name_begin - buf->buf,
2830 ident.mail_end - ident.name_begin + 1,
2831 namemail.buf, namemail.len);
2833 strbuf_release(&namemail);
2835 return 1;
2838 return 0;
2841 static int commit_match(struct commit *commit, struct rev_info *opt)
2843 int retval;
2844 const char *encoding;
2845 const char *message;
2846 struct strbuf buf = STRBUF_INIT;
2848 if (!opt->grep_filter.pattern_list && !opt->grep_filter.header_list)
2849 return 1;
2851 /* Prepend "fake" headers as needed */
2852 if (opt->grep_filter.use_reflog_filter) {
2853 strbuf_addstr(&buf, "reflog ");
2854 get_reflog_message(&buf, opt->reflog_info);
2855 strbuf_addch(&buf, '\n');
2859 * We grep in the user's output encoding, under the assumption that it
2860 * is the encoding they are most likely to write their grep pattern
2861 * for. In addition, it means we will match the "notes" encoding below,
2862 * so we will not end up with a buffer that has two different encodings
2863 * in it.
2865 encoding = get_log_output_encoding();
2866 message = logmsg_reencode(commit, NULL, encoding);
2868 /* Copy the commit to temporary if we are using "fake" headers */
2869 if (buf.len)
2870 strbuf_addstr(&buf, message);
2872 if (opt->grep_filter.header_list && opt->mailmap) {
2873 if (!buf.len)
2874 strbuf_addstr(&buf, message);
2876 commit_rewrite_person(&buf, "\nauthor ", opt->mailmap);
2877 commit_rewrite_person(&buf, "\ncommitter ", opt->mailmap);
2880 /* Append "fake" message parts as needed */
2881 if (opt->show_notes) {
2882 if (!buf.len)
2883 strbuf_addstr(&buf, message);
2884 format_display_notes(commit->object.sha1, &buf, encoding, 1);
2888 * Find either in the original commit message, or in the temporary.
2889 * Note that we cast away the constness of "message" here. It is
2890 * const because it may come from the cached commit buffer. That's OK,
2891 * because we know that it is modifiable heap memory, and that while
2892 * grep_buffer may modify it for speed, it will restore any
2893 * changes before returning.
2895 if (buf.len)
2896 retval = grep_buffer(&opt->grep_filter, buf.buf, buf.len);
2897 else
2898 retval = grep_buffer(&opt->grep_filter,
2899 (char *)message, strlen(message));
2900 strbuf_release(&buf);
2901 unuse_commit_buffer(commit, message);
2902 return opt->invert_grep ? !retval : retval;
2905 static inline int want_ancestry(const struct rev_info *revs)
2907 return (revs->rewrite_parents || revs->children.name);
2910 enum commit_action get_commit_action(struct rev_info *revs, struct commit *commit)
2912 if (commit->object.flags & SHOWN)
2913 return commit_ignore;
2914 if (revs->unpacked && has_sha1_pack(commit->object.sha1))
2915 return commit_ignore;
2916 if (revs->show_all)
2917 return commit_show;
2918 if (commit->object.flags & UNINTERESTING)
2919 return commit_ignore;
2920 if (revs->min_age != -1 && (commit->date > revs->min_age))
2921 return commit_ignore;
2922 if (revs->min_parents || (revs->max_parents >= 0)) {
2923 int n = commit_list_count(commit->parents);
2924 if ((n < revs->min_parents) ||
2925 ((revs->max_parents >= 0) && (n > revs->max_parents)))
2926 return commit_ignore;
2928 if (!commit_match(commit, revs))
2929 return commit_ignore;
2930 if (revs->prune && revs->dense) {
2931 /* Commit without changes? */
2932 if (commit->object.flags & TREESAME) {
2933 int n;
2934 struct commit_list *p;
2935 /* drop merges unless we want parenthood */
2936 if (!want_ancestry(revs))
2937 return commit_ignore;
2939 * If we want ancestry, then need to keep any merges
2940 * between relevant commits to tie together topology.
2941 * For consistency with TREESAME and simplification
2942 * use "relevant" here rather than just INTERESTING,
2943 * to treat bottom commit(s) as part of the topology.
2945 for (n = 0, p = commit->parents; p; p = p->next)
2946 if (relevant_commit(p->item))
2947 if (++n >= 2)
2948 return commit_show;
2949 return commit_ignore;
2952 return commit_show;
2955 define_commit_slab(saved_parents, struct commit_list *);
2957 #define EMPTY_PARENT_LIST ((struct commit_list *)-1)
2960 * You may only call save_parents() once per commit (this is checked
2961 * for non-root commits).
2963 static void save_parents(struct rev_info *revs, struct commit *commit)
2965 struct commit_list **pp;
2967 if (!revs->saved_parents_slab) {
2968 revs->saved_parents_slab = xmalloc(sizeof(struct saved_parents));
2969 init_saved_parents(revs->saved_parents_slab);
2972 pp = saved_parents_at(revs->saved_parents_slab, commit);
2975 * When walking with reflogs, we may visit the same commit
2976 * several times: once for each appearance in the reflog.
2978 * In this case, save_parents() will be called multiple times.
2979 * We want to keep only the first set of parents. We need to
2980 * store a sentinel value for an empty (i.e., NULL) parent
2981 * list to distinguish it from a not-yet-saved list, however.
2983 if (*pp)
2984 return;
2985 if (commit->parents)
2986 *pp = copy_commit_list(commit->parents);
2987 else
2988 *pp = EMPTY_PARENT_LIST;
2991 static void free_saved_parents(struct rev_info *revs)
2993 if (revs->saved_parents_slab)
2994 clear_saved_parents(revs->saved_parents_slab);
2997 struct commit_list *get_saved_parents(struct rev_info *revs, const struct commit *commit)
2999 struct commit_list *parents;
3001 if (!revs->saved_parents_slab)
3002 return commit->parents;
3004 parents = *saved_parents_at(revs->saved_parents_slab, commit);
3005 if (parents == EMPTY_PARENT_LIST)
3006 return NULL;
3007 return parents;
3010 enum commit_action simplify_commit(struct rev_info *revs, struct commit *commit)
3012 enum commit_action action = get_commit_action(revs, commit);
3014 if (action == commit_show &&
3015 !revs->show_all &&
3016 revs->prune && revs->dense && want_ancestry(revs)) {
3018 * --full-diff on simplified parents is no good: it
3019 * will show spurious changes from the commits that
3020 * were elided. So we save the parents on the side
3021 * when --full-diff is in effect.
3023 if (revs->full_diff)
3024 save_parents(revs, commit);
3025 if (rewrite_parents(revs, commit, rewrite_one) < 0)
3026 return commit_error;
3028 return action;
3031 static void track_linear(struct rev_info *revs, struct commit *commit)
3033 if (revs->track_first_time) {
3034 revs->linear = 1;
3035 revs->track_first_time = 0;
3036 } else {
3037 struct commit_list *p;
3038 for (p = revs->previous_parents; p; p = p->next)
3039 if (p->item == NULL || /* first commit */
3040 !hashcmp(p->item->object.sha1, commit->object.sha1))
3041 break;
3042 revs->linear = p != NULL;
3044 if (revs->reverse) {
3045 if (revs->linear)
3046 commit->object.flags |= TRACK_LINEAR;
3048 free_commit_list(revs->previous_parents);
3049 revs->previous_parents = copy_commit_list(commit->parents);
3052 static struct commit *get_revision_1(struct rev_info *revs)
3054 if (!revs->commits)
3055 return NULL;
3057 do {
3058 struct commit_list *entry = revs->commits;
3059 struct commit *commit = entry->item;
3061 revs->commits = entry->next;
3062 free(entry);
3064 if (revs->reflog_info) {
3065 save_parents(revs, commit);
3066 fake_reflog_parent(revs->reflog_info, commit);
3067 commit->object.flags &= ~(ADDED | SEEN | SHOWN);
3071 * If we haven't done the list limiting, we need to look at
3072 * the parents here. We also need to do the date-based limiting
3073 * that we'd otherwise have done in limit_list().
3075 if (!revs->limited) {
3076 if (revs->max_age != -1 &&
3077 (commit->date < revs->max_age))
3078 continue;
3079 if (add_parents_to_list(revs, commit, &revs->commits, NULL) < 0) {
3080 if (!revs->ignore_missing_links)
3081 die("Failed to traverse parents of commit %s",
3082 sha1_to_hex(commit->object.sha1));
3086 switch (simplify_commit(revs, commit)) {
3087 case commit_ignore:
3088 continue;
3089 case commit_error:
3090 die("Failed to simplify parents of commit %s",
3091 sha1_to_hex(commit->object.sha1));
3092 default:
3093 if (revs->track_linear)
3094 track_linear(revs, commit);
3095 return commit;
3097 } while (revs->commits);
3098 return NULL;
3102 * Return true for entries that have not yet been shown. (This is an
3103 * object_array_each_func_t.)
3105 static int entry_unshown(struct object_array_entry *entry, void *cb_data_unused)
3107 return !(entry->item->flags & SHOWN);
3111 * If array is on the verge of a realloc, garbage-collect any entries
3112 * that have already been shown to try to free up some space.
3114 static void gc_boundary(struct object_array *array)
3116 if (array->nr == array->alloc)
3117 object_array_filter(array, entry_unshown, NULL);
3120 static void create_boundary_commit_list(struct rev_info *revs)
3122 unsigned i;
3123 struct commit *c;
3124 struct object_array *array = &revs->boundary_commits;
3125 struct object_array_entry *objects = array->objects;
3128 * If revs->commits is non-NULL at this point, an error occurred in
3129 * get_revision_1(). Ignore the error and continue printing the
3130 * boundary commits anyway. (This is what the code has always
3131 * done.)
3133 if (revs->commits) {
3134 free_commit_list(revs->commits);
3135 revs->commits = NULL;
3139 * Put all of the actual boundary commits from revs->boundary_commits
3140 * into revs->commits
3142 for (i = 0; i < array->nr; i++) {
3143 c = (struct commit *)(objects[i].item);
3144 if (!c)
3145 continue;
3146 if (!(c->object.flags & CHILD_SHOWN))
3147 continue;
3148 if (c->object.flags & (SHOWN | BOUNDARY))
3149 continue;
3150 c->object.flags |= BOUNDARY;
3151 commit_list_insert(c, &revs->commits);
3155 * If revs->topo_order is set, sort the boundary commits
3156 * in topological order
3158 sort_in_topological_order(&revs->commits, revs->sort_order);
3161 static struct commit *get_revision_internal(struct rev_info *revs)
3163 struct commit *c = NULL;
3164 struct commit_list *l;
3166 if (revs->boundary == 2) {
3168 * All of the normal commits have already been returned,
3169 * and we are now returning boundary commits.
3170 * create_boundary_commit_list() has populated
3171 * revs->commits with the remaining commits to return.
3173 c = pop_commit(&revs->commits);
3174 if (c)
3175 c->object.flags |= SHOWN;
3176 return c;
3180 * If our max_count counter has reached zero, then we are done. We
3181 * don't simply return NULL because we still might need to show
3182 * boundary commits. But we want to avoid calling get_revision_1, which
3183 * might do a considerable amount of work finding the next commit only
3184 * for us to throw it away.
3186 * If it is non-zero, then either we don't have a max_count at all
3187 * (-1), or it is still counting, in which case we decrement.
3189 if (revs->max_count) {
3190 c = get_revision_1(revs);
3191 if (c) {
3192 while (revs->skip_count > 0) {
3193 revs->skip_count--;
3194 c = get_revision_1(revs);
3195 if (!c)
3196 break;
3200 if (revs->max_count > 0)
3201 revs->max_count--;
3204 if (c)
3205 c->object.flags |= SHOWN;
3207 if (!revs->boundary)
3208 return c;
3210 if (!c) {
3212 * get_revision_1() runs out the commits, and
3213 * we are done computing the boundaries.
3214 * switch to boundary commits output mode.
3216 revs->boundary = 2;
3219 * Update revs->commits to contain the list of
3220 * boundary commits.
3222 create_boundary_commit_list(revs);
3224 return get_revision_internal(revs);
3228 * boundary commits are the commits that are parents of the
3229 * ones we got from get_revision_1() but they themselves are
3230 * not returned from get_revision_1(). Before returning
3231 * 'c', we need to mark its parents that they could be boundaries.
3234 for (l = c->parents; l; l = l->next) {
3235 struct object *p;
3236 p = &(l->item->object);
3237 if (p->flags & (CHILD_SHOWN | SHOWN))
3238 continue;
3239 p->flags |= CHILD_SHOWN;
3240 gc_boundary(&revs->boundary_commits);
3241 add_object_array(p, NULL, &revs->boundary_commits);
3244 return c;
3247 struct commit *get_revision(struct rev_info *revs)
3249 struct commit *c;
3250 struct commit_list *reversed;
3252 if (revs->reverse) {
3253 reversed = NULL;
3254 while ((c = get_revision_internal(revs)))
3255 commit_list_insert(c, &reversed);
3256 revs->commits = reversed;
3257 revs->reverse = 0;
3258 revs->reverse_output_stage = 1;
3261 if (revs->reverse_output_stage) {
3262 c = pop_commit(&revs->commits);
3263 if (revs->track_linear)
3264 revs->linear = !!(c && c->object.flags & TRACK_LINEAR);
3265 return c;
3268 c = get_revision_internal(revs);
3269 if (c && revs->graph)
3270 graph_update(revs->graph, c);
3271 if (!c) {
3272 free_saved_parents(revs);
3273 if (revs->previous_parents) {
3274 free_commit_list(revs->previous_parents);
3275 revs->previous_parents = NULL;
3278 return c;
3281 char *get_revision_mark(const struct rev_info *revs, const struct commit *commit)
3283 if (commit->object.flags & BOUNDARY)
3284 return "-";
3285 else if (commit->object.flags & UNINTERESTING)
3286 return "^";
3287 else if (commit->object.flags & PATCHSAME)
3288 return "=";
3289 else if (!revs || revs->left_right) {
3290 if (commit->object.flags & SYMMETRIC_LEFT)
3291 return "<";
3292 else
3293 return ">";
3294 } else if (revs->graph)
3295 return "*";
3296 else if (revs->cherry_mark)
3297 return "+";
3298 return "";
3301 void put_revision_mark(const struct rev_info *revs, const struct commit *commit)
3303 char *mark = get_revision_mark(revs, commit);
3304 if (!strlen(mark))
3305 return;
3306 fputs(mark, stdout);
3307 putchar(' ');