Skip tests that fail due to incomplete implementations, missing tools...
[git/mingw/j6t.git] / revision.c
blob5b7398f6f506f761323f014a14d63b6f76ae5772
1 #include "cache.h"
2 #include "tag.h"
3 #include "blob.h"
4 #include "tree.h"
5 #include "commit.h"
6 #include "diff.h"
7 #include "refs.h"
8 #include "revision.h"
9 #include "graph.h"
10 #include "grep.h"
11 #include "reflog-walk.h"
12 #include "patch-ids.h"
13 #include "decorate.h"
14 #include "log-tree.h"
15 #include "string-list.h"
16 #include "line-log.h"
17 #include "mailmap.h"
18 #include "commit-slab.h"
19 #include "dir.h"
20 #include "cache-tree.h"
21 #include "bisect.h"
23 volatile show_early_output_fn_t show_early_output;
25 static const char *term_bad;
26 static const char *term_good;
28 char *path_name(const struct name_path *path, const char *name)
30 const struct name_path *p;
31 char *n, *m;
32 int nlen = strlen(name);
33 int len = nlen + 1;
35 for (p = path; p; p = p->up) {
36 if (p->elem_len)
37 len += p->elem_len + 1;
39 n = xmalloc(len);
40 m = n + len - (nlen + 1);
41 memcpy(m, name, nlen + 1);
42 for (p = path; p; p = p->up) {
43 if (p->elem_len) {
44 m -= p->elem_len + 1;
45 memcpy(m, p->elem, p->elem_len);
46 m[p->elem_len] = '/';
49 return n;
52 static int show_path_component_truncated(FILE *out, const char *name, int len)
54 int cnt;
55 for (cnt = 0; cnt < len; cnt++) {
56 int ch = name[cnt];
57 if (!ch || ch == '\n')
58 return -1;
59 fputc(ch, out);
61 return len;
64 static int show_path_truncated(FILE *out, const struct name_path *path)
66 int emitted, ours;
68 if (!path)
69 return 0;
70 emitted = show_path_truncated(out, path->up);
71 if (emitted < 0)
72 return emitted;
73 if (emitted)
74 fputc('/', out);
75 ours = show_path_component_truncated(out, path->elem, path->elem_len);
76 if (ours < 0)
77 return ours;
78 return ours || emitted;
81 void show_object_with_name(FILE *out, struct object *obj,
82 const struct name_path *path, const char *component)
84 struct name_path leaf;
85 leaf.up = (struct name_path *)path;
86 leaf.elem = component;
87 leaf.elem_len = strlen(component);
89 fprintf(out, "%s ", sha1_to_hex(obj->sha1));
90 show_path_truncated(out, &leaf);
91 fputc('\n', out);
94 static void mark_blob_uninteresting(struct blob *blob)
96 if (!blob)
97 return;
98 if (blob->object.flags & UNINTERESTING)
99 return;
100 blob->object.flags |= UNINTERESTING;
103 static void mark_tree_contents_uninteresting(struct tree *tree)
105 struct tree_desc desc;
106 struct name_entry entry;
107 struct object *obj = &tree->object;
109 if (!has_sha1_file(obj->sha1))
110 return;
111 if (parse_tree(tree) < 0)
112 die("bad tree %s", sha1_to_hex(obj->sha1));
114 init_tree_desc(&desc, tree->buffer, tree->size);
115 while (tree_entry(&desc, &entry)) {
116 switch (object_type(entry.mode)) {
117 case OBJ_TREE:
118 mark_tree_uninteresting(lookup_tree(entry.sha1));
119 break;
120 case OBJ_BLOB:
121 mark_blob_uninteresting(lookup_blob(entry.sha1));
122 break;
123 default:
124 /* Subproject commit - not in this repository */
125 break;
130 * We don't care about the tree any more
131 * after it has been marked uninteresting.
133 free_tree_buffer(tree);
136 void mark_tree_uninteresting(struct tree *tree)
138 struct object *obj = &tree->object;
140 if (!tree)
141 return;
142 if (obj->flags & UNINTERESTING)
143 return;
144 obj->flags |= UNINTERESTING;
145 mark_tree_contents_uninteresting(tree);
148 void mark_parents_uninteresting(struct commit *commit)
150 struct commit_list *parents = NULL, *l;
152 for (l = commit->parents; l; l = l->next)
153 commit_list_insert(l->item, &parents);
155 while (parents) {
156 struct commit *commit = pop_commit(&parents);
158 while (commit) {
160 * A missing commit is ok iff its parent is marked
161 * uninteresting.
163 * We just mark such a thing parsed, so that when
164 * it is popped next time around, we won't be trying
165 * to parse it and get an error.
167 if (!has_sha1_file(commit->object.sha1))
168 commit->object.parsed = 1;
170 if (commit->object.flags & UNINTERESTING)
171 break;
173 commit->object.flags |= UNINTERESTING;
176 * Normally we haven't parsed the parent
177 * yet, so we won't have a parent of a parent
178 * here. However, it may turn out that we've
179 * reached this commit some other way (where it
180 * wasn't uninteresting), in which case we need
181 * to mark its parents recursively too..
183 if (!commit->parents)
184 break;
186 for (l = commit->parents->next; l; l = l->next)
187 commit_list_insert(l->item, &parents);
188 commit = commit->parents->item;
193 static void add_pending_object_with_path(struct rev_info *revs,
194 struct object *obj,
195 const char *name, unsigned mode,
196 const char *path)
198 if (!obj)
199 return;
200 if (revs->no_walk && (obj->flags & UNINTERESTING))
201 revs->no_walk = 0;
202 if (revs->reflog_info && obj->type == OBJ_COMMIT) {
203 struct strbuf buf = STRBUF_INIT;
204 int len = interpret_branch_name(name, 0, &buf);
205 int st;
207 if (0 < len && name[len] && buf.len)
208 strbuf_addstr(&buf, name + len);
209 st = add_reflog_for_walk(revs->reflog_info,
210 (struct commit *)obj,
211 buf.buf[0] ? buf.buf: name);
212 strbuf_release(&buf);
213 if (st)
214 return;
216 add_object_array_with_path(obj, name, &revs->pending, mode, path);
219 static void add_pending_object_with_mode(struct rev_info *revs,
220 struct object *obj,
221 const char *name, unsigned mode)
223 add_pending_object_with_path(revs, obj, name, mode, NULL);
226 void add_pending_object(struct rev_info *revs,
227 struct object *obj, const char *name)
229 add_pending_object_with_mode(revs, obj, name, S_IFINVALID);
232 void add_head_to_pending(struct rev_info *revs)
234 unsigned char sha1[20];
235 struct object *obj;
236 if (get_sha1("HEAD", sha1))
237 return;
238 obj = parse_object(sha1);
239 if (!obj)
240 return;
241 add_pending_object(revs, obj, "HEAD");
244 static struct object *get_reference(struct rev_info *revs, const char *name,
245 const unsigned char *sha1,
246 unsigned int flags)
248 struct object *object;
250 object = parse_object(sha1);
251 if (!object) {
252 if (revs->ignore_missing)
253 return object;
254 die("bad object %s", name);
256 object->flags |= flags;
257 return object;
260 void add_pending_sha1(struct rev_info *revs, const char *name,
261 const unsigned char *sha1, unsigned int flags)
263 struct object *object = get_reference(revs, name, sha1, flags);
264 add_pending_object(revs, object, name);
267 static struct commit *handle_commit(struct rev_info *revs,
268 struct object_array_entry *entry)
270 struct object *object = entry->item;
271 const char *name = entry->name;
272 const char *path = entry->path;
273 unsigned int mode = entry->mode;
274 unsigned long flags = object->flags;
277 * Tag object? Look what it points to..
279 while (object->type == OBJ_TAG) {
280 struct tag *tag = (struct tag *) object;
281 if (revs->tag_objects && !(flags & UNINTERESTING))
282 add_pending_object(revs, object, tag->tag);
283 if (!tag->tagged)
284 die("bad tag");
285 object = parse_object(tag->tagged->sha1);
286 if (!object) {
287 if (flags & UNINTERESTING)
288 return NULL;
289 die("bad object %s", sha1_to_hex(tag->tagged->sha1));
291 object->flags |= flags;
293 * We'll handle the tagged object by looping or dropping
294 * through to the non-tag handlers below. Do not
295 * propagate data from the tag's pending entry.
297 name = "";
298 path = NULL;
299 mode = 0;
303 * Commit object? Just return it, we'll do all the complex
304 * reachability crud.
306 if (object->type == OBJ_COMMIT) {
307 struct commit *commit = (struct commit *)object;
308 if (parse_commit(commit) < 0)
309 die("unable to parse commit %s", name);
310 if (flags & UNINTERESTING) {
311 mark_parents_uninteresting(commit);
312 revs->limited = 1;
314 if (revs->show_source && !commit->util)
315 commit->util = xstrdup(name);
316 return commit;
320 * Tree object? Either mark it uninteresting, or add it
321 * to the list of objects to look at later..
323 if (object->type == OBJ_TREE) {
324 struct tree *tree = (struct tree *)object;
325 if (!revs->tree_objects)
326 return NULL;
327 if (flags & UNINTERESTING) {
328 mark_tree_contents_uninteresting(tree);
329 return NULL;
331 add_pending_object_with_path(revs, object, name, mode, path);
332 return NULL;
336 * Blob object? You know the drill by now..
338 if (object->type == OBJ_BLOB) {
339 if (!revs->blob_objects)
340 return NULL;
341 if (flags & UNINTERESTING)
342 return NULL;
343 add_pending_object_with_path(revs, object, name, mode, path);
344 return NULL;
346 die("%s is unknown object", name);
349 static int everybody_uninteresting(struct commit_list *orig,
350 struct commit **interesting_cache)
352 struct commit_list *list = orig;
354 if (*interesting_cache) {
355 struct commit *commit = *interesting_cache;
356 if (!(commit->object.flags & UNINTERESTING))
357 return 0;
360 while (list) {
361 struct commit *commit = list->item;
362 list = list->next;
363 if (commit->object.flags & UNINTERESTING)
364 continue;
366 *interesting_cache = commit;
367 return 0;
369 return 1;
373 * A definition of "relevant" commit that we can use to simplify limited graphs
374 * by eliminating side branches.
376 * A "relevant" commit is one that is !UNINTERESTING (ie we are including it
377 * in our list), or that is a specified BOTTOM commit. Then after computing
378 * a limited list, during processing we can generally ignore boundary merges
379 * coming from outside the graph, (ie from irrelevant parents), and treat
380 * those merges as if they were single-parent. TREESAME is defined to consider
381 * only relevant parents, if any. If we are TREESAME to our on-graph parents,
382 * we don't care if we were !TREESAME to non-graph parents.
384 * Treating bottom commits as relevant ensures that a limited graph's
385 * connection to the actual bottom commit is not viewed as a side branch, but
386 * treated as part of the graph. For example:
388 * ....Z...A---X---o---o---B
389 * . /
390 * W---Y
392 * When computing "A..B", the A-X connection is at least as important as
393 * Y-X, despite A being flagged UNINTERESTING.
395 * And when computing --ancestry-path "A..B", the A-X connection is more
396 * important than Y-X, despite both A and Y being flagged UNINTERESTING.
398 static inline int relevant_commit(struct commit *commit)
400 return (commit->object.flags & (UNINTERESTING | BOTTOM)) != UNINTERESTING;
404 * Return a single relevant commit from a parent list. If we are a TREESAME
405 * commit, and this selects one of our parents, then we can safely simplify to
406 * that parent.
408 static struct commit *one_relevant_parent(const struct rev_info *revs,
409 struct commit_list *orig)
411 struct commit_list *list = orig;
412 struct commit *relevant = NULL;
414 if (!orig)
415 return NULL;
418 * For 1-parent commits, or if first-parent-only, then return that
419 * first parent (even if not "relevant" by the above definition).
420 * TREESAME will have been set purely on that parent.
422 if (revs->first_parent_only || !orig->next)
423 return orig->item;
426 * For multi-parent commits, identify a sole relevant parent, if any.
427 * If we have only one relevant parent, then TREESAME will be set purely
428 * with regard to that parent, and we can simplify accordingly.
430 * If we have more than one relevant parent, or no relevant parents
431 * (and multiple irrelevant ones), then we can't select a parent here
432 * and return NULL.
434 while (list) {
435 struct commit *commit = list->item;
436 list = list->next;
437 if (relevant_commit(commit)) {
438 if (relevant)
439 return NULL;
440 relevant = commit;
443 return relevant;
447 * The goal is to get REV_TREE_NEW as the result only if the
448 * diff consists of all '+' (and no other changes), REV_TREE_OLD
449 * if the whole diff is removal of old data, and otherwise
450 * REV_TREE_DIFFERENT (of course if the trees are the same we
451 * want REV_TREE_SAME).
452 * That means that once we get to REV_TREE_DIFFERENT, we do not
453 * have to look any further.
455 static int tree_difference = REV_TREE_SAME;
457 static void file_add_remove(struct diff_options *options,
458 int addremove, unsigned mode,
459 const unsigned char *sha1,
460 int sha1_valid,
461 const char *fullpath, unsigned dirty_submodule)
463 int diff = addremove == '+' ? REV_TREE_NEW : REV_TREE_OLD;
465 tree_difference |= diff;
466 if (tree_difference == REV_TREE_DIFFERENT)
467 DIFF_OPT_SET(options, HAS_CHANGES);
470 static void file_change(struct diff_options *options,
471 unsigned old_mode, unsigned new_mode,
472 const unsigned char *old_sha1,
473 const unsigned char *new_sha1,
474 int old_sha1_valid, int new_sha1_valid,
475 const char *fullpath,
476 unsigned old_dirty_submodule, unsigned new_dirty_submodule)
478 tree_difference = REV_TREE_DIFFERENT;
479 DIFF_OPT_SET(options, HAS_CHANGES);
482 static int rev_compare_tree(struct rev_info *revs,
483 struct commit *parent, struct commit *commit)
485 struct tree *t1 = parent->tree;
486 struct tree *t2 = commit->tree;
488 if (!t1)
489 return REV_TREE_NEW;
490 if (!t2)
491 return REV_TREE_OLD;
493 if (revs->simplify_by_decoration) {
495 * If we are simplifying by decoration, then the commit
496 * is worth showing if it has a tag pointing at it.
498 if (get_name_decoration(&commit->object))
499 return REV_TREE_DIFFERENT;
501 * A commit that is not pointed by a tag is uninteresting
502 * if we are not limited by path. This means that you will
503 * see the usual "commits that touch the paths" plus any
504 * tagged commit by specifying both --simplify-by-decoration
505 * and pathspec.
507 if (!revs->prune_data.nr)
508 return REV_TREE_SAME;
511 tree_difference = REV_TREE_SAME;
512 DIFF_OPT_CLR(&revs->pruning, HAS_CHANGES);
513 if (diff_tree_sha1(t1->object.sha1, t2->object.sha1, "",
514 &revs->pruning) < 0)
515 return REV_TREE_DIFFERENT;
516 return tree_difference;
519 static int rev_same_tree_as_empty(struct rev_info *revs, struct commit *commit)
521 int retval;
522 struct tree *t1 = commit->tree;
524 if (!t1)
525 return 0;
527 tree_difference = REV_TREE_SAME;
528 DIFF_OPT_CLR(&revs->pruning, HAS_CHANGES);
529 retval = diff_tree_sha1(NULL, t1->object.sha1, "", &revs->pruning);
531 return retval >= 0 && (tree_difference == REV_TREE_SAME);
534 struct treesame_state {
535 unsigned int nparents;
536 unsigned char treesame[FLEX_ARRAY];
539 static struct treesame_state *initialise_treesame(struct rev_info *revs, struct commit *commit)
541 unsigned n = commit_list_count(commit->parents);
542 struct treesame_state *st = xcalloc(1, sizeof(*st) + n);
543 st->nparents = n;
544 add_decoration(&revs->treesame, &commit->object, st);
545 return st;
549 * Must be called immediately after removing the nth_parent from a commit's
550 * parent list, if we are maintaining the per-parent treesame[] decoration.
551 * This does not recalculate the master TREESAME flag - update_treesame()
552 * should be called to update it after a sequence of treesame[] modifications
553 * that may have affected it.
555 static int compact_treesame(struct rev_info *revs, struct commit *commit, unsigned nth_parent)
557 struct treesame_state *st;
558 int old_same;
560 if (!commit->parents) {
562 * Have just removed the only parent from a non-merge.
563 * Different handling, as we lack decoration.
565 if (nth_parent != 0)
566 die("compact_treesame %u", nth_parent);
567 old_same = !!(commit->object.flags & TREESAME);
568 if (rev_same_tree_as_empty(revs, commit))
569 commit->object.flags |= TREESAME;
570 else
571 commit->object.flags &= ~TREESAME;
572 return old_same;
575 st = lookup_decoration(&revs->treesame, &commit->object);
576 if (!st || nth_parent >= st->nparents)
577 die("compact_treesame %u", nth_parent);
579 old_same = st->treesame[nth_parent];
580 memmove(st->treesame + nth_parent,
581 st->treesame + nth_parent + 1,
582 st->nparents - nth_parent - 1);
585 * If we've just become a non-merge commit, update TREESAME
586 * immediately, and remove the no-longer-needed decoration.
587 * If still a merge, defer update until update_treesame().
589 if (--st->nparents == 1) {
590 if (commit->parents->next)
591 die("compact_treesame parents mismatch");
592 if (st->treesame[0] && revs->dense)
593 commit->object.flags |= TREESAME;
594 else
595 commit->object.flags &= ~TREESAME;
596 free(add_decoration(&revs->treesame, &commit->object, NULL));
599 return old_same;
602 static unsigned update_treesame(struct rev_info *revs, struct commit *commit)
604 if (commit->parents && commit->parents->next) {
605 unsigned n;
606 struct treesame_state *st;
607 struct commit_list *p;
608 unsigned relevant_parents;
609 unsigned relevant_change, irrelevant_change;
611 st = lookup_decoration(&revs->treesame, &commit->object);
612 if (!st)
613 die("update_treesame %s", sha1_to_hex(commit->object.sha1));
614 relevant_parents = 0;
615 relevant_change = irrelevant_change = 0;
616 for (p = commit->parents, n = 0; p; n++, p = p->next) {
617 if (relevant_commit(p->item)) {
618 relevant_change |= !st->treesame[n];
619 relevant_parents++;
620 } else
621 irrelevant_change |= !st->treesame[n];
623 if (relevant_parents ? relevant_change : irrelevant_change)
624 commit->object.flags &= ~TREESAME;
625 else
626 commit->object.flags |= TREESAME;
629 return commit->object.flags & TREESAME;
632 static inline int limiting_can_increase_treesame(const struct rev_info *revs)
635 * TREESAME is irrelevant unless prune && dense;
636 * if simplify_history is set, we can't have a mixture of TREESAME and
637 * !TREESAME INTERESTING parents (and we don't have treesame[]
638 * decoration anyway);
639 * if first_parent_only is set, then the TREESAME flag is locked
640 * against the first parent (and again we lack treesame[] decoration).
642 return revs->prune && revs->dense &&
643 !revs->simplify_history &&
644 !revs->first_parent_only;
647 static void try_to_simplify_commit(struct rev_info *revs, struct commit *commit)
649 struct commit_list **pp, *parent;
650 struct treesame_state *ts = NULL;
651 int relevant_change = 0, irrelevant_change = 0;
652 int relevant_parents, nth_parent;
655 * If we don't do pruning, everything is interesting
657 if (!revs->prune)
658 return;
660 if (!commit->tree)
661 return;
663 if (!commit->parents) {
664 if (rev_same_tree_as_empty(revs, commit))
665 commit->object.flags |= TREESAME;
666 return;
670 * Normal non-merge commit? If we don't want to make the
671 * history dense, we consider it always to be a change..
673 if (!revs->dense && !commit->parents->next)
674 return;
676 for (pp = &commit->parents, nth_parent = 0, relevant_parents = 0;
677 (parent = *pp) != NULL;
678 pp = &parent->next, nth_parent++) {
679 struct commit *p = parent->item;
680 if (relevant_commit(p))
681 relevant_parents++;
683 if (nth_parent == 1) {
685 * This our second loop iteration - so we now know
686 * we're dealing with a merge.
688 * Do not compare with later parents when we care only about
689 * the first parent chain, in order to avoid derailing the
690 * traversal to follow a side branch that brought everything
691 * in the path we are limited to by the pathspec.
693 if (revs->first_parent_only)
694 break;
696 * If this will remain a potentially-simplifiable
697 * merge, remember per-parent treesame if needed.
698 * Initialise the array with the comparison from our
699 * first iteration.
701 if (revs->treesame.name &&
702 !revs->simplify_history &&
703 !(commit->object.flags & UNINTERESTING)) {
704 ts = initialise_treesame(revs, commit);
705 if (!(irrelevant_change || relevant_change))
706 ts->treesame[0] = 1;
709 if (parse_commit(p) < 0)
710 die("cannot simplify commit %s (because of %s)",
711 sha1_to_hex(commit->object.sha1),
712 sha1_to_hex(p->object.sha1));
713 switch (rev_compare_tree(revs, p, commit)) {
714 case REV_TREE_SAME:
715 if (!revs->simplify_history || !relevant_commit(p)) {
716 /* Even if a merge with an uninteresting
717 * side branch brought the entire change
718 * we are interested in, we do not want
719 * to lose the other branches of this
720 * merge, so we just keep going.
722 if (ts)
723 ts->treesame[nth_parent] = 1;
724 continue;
726 parent->next = NULL;
727 commit->parents = parent;
728 commit->object.flags |= TREESAME;
729 return;
731 case REV_TREE_NEW:
732 if (revs->remove_empty_trees &&
733 rev_same_tree_as_empty(revs, p)) {
734 /* We are adding all the specified
735 * paths from this parent, so the
736 * history beyond this parent is not
737 * interesting. Remove its parents
738 * (they are grandparents for us).
739 * IOW, we pretend this parent is a
740 * "root" commit.
742 if (parse_commit(p) < 0)
743 die("cannot simplify commit %s (invalid %s)",
744 sha1_to_hex(commit->object.sha1),
745 sha1_to_hex(p->object.sha1));
746 p->parents = NULL;
748 /* fallthrough */
749 case REV_TREE_OLD:
750 case REV_TREE_DIFFERENT:
751 if (relevant_commit(p))
752 relevant_change = 1;
753 else
754 irrelevant_change = 1;
755 continue;
757 die("bad tree compare for commit %s", sha1_to_hex(commit->object.sha1));
761 * TREESAME is straightforward for single-parent commits. For merge
762 * commits, it is most useful to define it so that "irrelevant"
763 * parents cannot make us !TREESAME - if we have any relevant
764 * parents, then we only consider TREESAMEness with respect to them,
765 * allowing irrelevant merges from uninteresting branches to be
766 * simplified away. Only if we have only irrelevant parents do we
767 * base TREESAME on them. Note that this logic is replicated in
768 * update_treesame, which should be kept in sync.
770 if (relevant_parents ? !relevant_change : !irrelevant_change)
771 commit->object.flags |= TREESAME;
774 static void commit_list_insert_by_date_cached(struct commit *p, struct commit_list **head,
775 struct commit_list *cached_base, struct commit_list **cache)
777 struct commit_list *new_entry;
779 if (cached_base && p->date < cached_base->item->date)
780 new_entry = commit_list_insert_by_date(p, &cached_base->next);
781 else
782 new_entry = commit_list_insert_by_date(p, head);
784 if (cache && (!*cache || p->date < (*cache)->item->date))
785 *cache = new_entry;
788 static int add_parents_to_list(struct rev_info *revs, struct commit *commit,
789 struct commit_list **list, struct commit_list **cache_ptr)
791 struct commit_list *parent = commit->parents;
792 unsigned left_flag;
793 struct commit_list *cached_base = cache_ptr ? *cache_ptr : NULL;
795 if (commit->object.flags & ADDED)
796 return 0;
797 commit->object.flags |= ADDED;
799 if (revs->include_check &&
800 !revs->include_check(commit, revs->include_check_data))
801 return 0;
804 * If the commit is uninteresting, don't try to
805 * prune parents - we want the maximal uninteresting
806 * set.
808 * Normally we haven't parsed the parent
809 * yet, so we won't have a parent of a parent
810 * here. However, it may turn out that we've
811 * reached this commit some other way (where it
812 * wasn't uninteresting), in which case we need
813 * to mark its parents recursively too..
815 if (commit->object.flags & UNINTERESTING) {
816 while (parent) {
817 struct commit *p = parent->item;
818 parent = parent->next;
819 if (p)
820 p->object.flags |= UNINTERESTING;
821 if (parse_commit_gently(p, 1) < 0)
822 continue;
823 if (p->parents)
824 mark_parents_uninteresting(p);
825 if (p->object.flags & SEEN)
826 continue;
827 p->object.flags |= SEEN;
828 commit_list_insert_by_date_cached(p, list, cached_base, cache_ptr);
830 return 0;
834 * Ok, the commit wasn't uninteresting. Try to
835 * simplify the commit history and find the parent
836 * that has no differences in the path set if one exists.
838 try_to_simplify_commit(revs, commit);
840 if (revs->no_walk)
841 return 0;
843 left_flag = (commit->object.flags & SYMMETRIC_LEFT);
845 for (parent = commit->parents; parent; parent = parent->next) {
846 struct commit *p = parent->item;
848 if (parse_commit_gently(p, revs->ignore_missing_links) < 0)
849 return -1;
850 if (revs->show_source && !p->util)
851 p->util = commit->util;
852 p->object.flags |= left_flag;
853 if (!(p->object.flags & SEEN)) {
854 p->object.flags |= SEEN;
855 commit_list_insert_by_date_cached(p, list, cached_base, cache_ptr);
857 if (revs->first_parent_only)
858 break;
860 return 0;
863 static void cherry_pick_list(struct commit_list *list, struct rev_info *revs)
865 struct commit_list *p;
866 int left_count = 0, right_count = 0;
867 int left_first;
868 struct patch_ids ids;
869 unsigned cherry_flag;
871 /* First count the commits on the left and on the right */
872 for (p = list; p; p = p->next) {
873 struct commit *commit = p->item;
874 unsigned flags = commit->object.flags;
875 if (flags & BOUNDARY)
877 else if (flags & SYMMETRIC_LEFT)
878 left_count++;
879 else
880 right_count++;
883 if (!left_count || !right_count)
884 return;
886 left_first = left_count < right_count;
887 init_patch_ids(&ids);
888 ids.diffopts.pathspec = revs->diffopt.pathspec;
890 /* Compute patch-ids for one side */
891 for (p = list; p; p = p->next) {
892 struct commit *commit = p->item;
893 unsigned flags = commit->object.flags;
895 if (flags & BOUNDARY)
896 continue;
898 * If we have fewer left, left_first is set and we omit
899 * commits on the right branch in this loop. If we have
900 * fewer right, we skip the left ones.
902 if (left_first != !!(flags & SYMMETRIC_LEFT))
903 continue;
904 commit->util = add_commit_patch_id(commit, &ids);
907 /* either cherry_mark or cherry_pick are true */
908 cherry_flag = revs->cherry_mark ? PATCHSAME : SHOWN;
910 /* Check the other side */
911 for (p = list; p; p = p->next) {
912 struct commit *commit = p->item;
913 struct patch_id *id;
914 unsigned flags = commit->object.flags;
916 if (flags & BOUNDARY)
917 continue;
919 * If we have fewer left, left_first is set and we omit
920 * commits on the left branch in this loop.
922 if (left_first == !!(flags & SYMMETRIC_LEFT))
923 continue;
926 * Have we seen the same patch id?
928 id = has_commit_patch_id(commit, &ids);
929 if (!id)
930 continue;
931 id->seen = 1;
932 commit->object.flags |= cherry_flag;
935 /* Now check the original side for seen ones */
936 for (p = list; p; p = p->next) {
937 struct commit *commit = p->item;
938 struct patch_id *ent;
940 ent = commit->util;
941 if (!ent)
942 continue;
943 if (ent->seen)
944 commit->object.flags |= cherry_flag;
945 commit->util = NULL;
948 free_patch_ids(&ids);
951 /* How many extra uninteresting commits we want to see.. */
952 #define SLOP 5
954 static int still_interesting(struct commit_list *src, unsigned long date, int slop,
955 struct commit **interesting_cache)
958 * No source list at all? We're definitely done..
960 if (!src)
961 return 0;
964 * Does the destination list contain entries with a date
965 * before the source list? Definitely _not_ done.
967 if (date <= src->item->date)
968 return SLOP;
971 * Does the source list still have interesting commits in
972 * it? Definitely not done..
974 if (!everybody_uninteresting(src, interesting_cache))
975 return SLOP;
977 /* Ok, we're closing in.. */
978 return slop-1;
982 * "rev-list --ancestry-path A..B" computes commits that are ancestors
983 * of B but not ancestors of A but further limits the result to those
984 * that are descendants of A. This takes the list of bottom commits and
985 * the result of "A..B" without --ancestry-path, and limits the latter
986 * further to the ones that can reach one of the commits in "bottom".
988 static void limit_to_ancestry(struct commit_list *bottom, struct commit_list *list)
990 struct commit_list *p;
991 struct commit_list *rlist = NULL;
992 int made_progress;
995 * Reverse the list so that it will be likely that we would
996 * process parents before children.
998 for (p = list; p; p = p->next)
999 commit_list_insert(p->item, &rlist);
1001 for (p = bottom; p; p = p->next)
1002 p->item->object.flags |= TMP_MARK;
1005 * Mark the ones that can reach bottom commits in "list",
1006 * in a bottom-up fashion.
1008 do {
1009 made_progress = 0;
1010 for (p = rlist; p; p = p->next) {
1011 struct commit *c = p->item;
1012 struct commit_list *parents;
1013 if (c->object.flags & (TMP_MARK | UNINTERESTING))
1014 continue;
1015 for (parents = c->parents;
1016 parents;
1017 parents = parents->next) {
1018 if (!(parents->item->object.flags & TMP_MARK))
1019 continue;
1020 c->object.flags |= TMP_MARK;
1021 made_progress = 1;
1022 break;
1025 } while (made_progress);
1028 * NEEDSWORK: decide if we want to remove parents that are
1029 * not marked with TMP_MARK from commit->parents for commits
1030 * in the resulting list. We may not want to do that, though.
1034 * The ones that are not marked with TMP_MARK are uninteresting
1036 for (p = list; p; p = p->next) {
1037 struct commit *c = p->item;
1038 if (c->object.flags & TMP_MARK)
1039 continue;
1040 c->object.flags |= UNINTERESTING;
1043 /* We are done with the TMP_MARK */
1044 for (p = list; p; p = p->next)
1045 p->item->object.flags &= ~TMP_MARK;
1046 for (p = bottom; p; p = p->next)
1047 p->item->object.flags &= ~TMP_MARK;
1048 free_commit_list(rlist);
1052 * Before walking the history, keep the set of "negative" refs the
1053 * caller has asked to exclude.
1055 * This is used to compute "rev-list --ancestry-path A..B", as we need
1056 * to filter the result of "A..B" further to the ones that can actually
1057 * reach A.
1059 static struct commit_list *collect_bottom_commits(struct commit_list *list)
1061 struct commit_list *elem, *bottom = NULL;
1062 for (elem = list; elem; elem = elem->next)
1063 if (elem->item->object.flags & BOTTOM)
1064 commit_list_insert(elem->item, &bottom);
1065 return bottom;
1068 /* Assumes either left_only or right_only is set */
1069 static void limit_left_right(struct commit_list *list, struct rev_info *revs)
1071 struct commit_list *p;
1073 for (p = list; p; p = p->next) {
1074 struct commit *commit = p->item;
1076 if (revs->right_only) {
1077 if (commit->object.flags & SYMMETRIC_LEFT)
1078 commit->object.flags |= SHOWN;
1079 } else /* revs->left_only is set */
1080 if (!(commit->object.flags & SYMMETRIC_LEFT))
1081 commit->object.flags |= SHOWN;
1085 static int limit_list(struct rev_info *revs)
1087 int slop = SLOP;
1088 unsigned long date = ~0ul;
1089 struct commit_list *list = revs->commits;
1090 struct commit_list *newlist = NULL;
1091 struct commit_list **p = &newlist;
1092 struct commit_list *bottom = NULL;
1093 struct commit *interesting_cache = NULL;
1095 if (revs->ancestry_path) {
1096 bottom = collect_bottom_commits(list);
1097 if (!bottom)
1098 die("--ancestry-path given but there are no bottom commits");
1101 while (list) {
1102 struct commit *commit = pop_commit(&list);
1103 struct object *obj = &commit->object;
1104 show_early_output_fn_t show;
1106 if (commit == interesting_cache)
1107 interesting_cache = NULL;
1109 if (revs->max_age != -1 && (commit->date < revs->max_age))
1110 obj->flags |= UNINTERESTING;
1111 if (add_parents_to_list(revs, commit, &list, NULL) < 0)
1112 return -1;
1113 if (obj->flags & UNINTERESTING) {
1114 mark_parents_uninteresting(commit);
1115 if (revs->show_all)
1116 p = &commit_list_insert(commit, p)->next;
1117 slop = still_interesting(list, date, slop, &interesting_cache);
1118 if (slop)
1119 continue;
1120 /* If showing all, add the whole pending list to the end */
1121 if (revs->show_all)
1122 *p = list;
1123 break;
1125 if (revs->min_age != -1 && (commit->date > revs->min_age))
1126 continue;
1127 date = commit->date;
1128 p = &commit_list_insert(commit, p)->next;
1130 show = show_early_output;
1131 if (!show)
1132 continue;
1134 show(revs, newlist);
1135 show_early_output = NULL;
1137 if (revs->cherry_pick || revs->cherry_mark)
1138 cherry_pick_list(newlist, revs);
1140 if (revs->left_only || revs->right_only)
1141 limit_left_right(newlist, revs);
1143 if (bottom) {
1144 limit_to_ancestry(bottom, newlist);
1145 free_commit_list(bottom);
1149 * Check if any commits have become TREESAME by some of their parents
1150 * becoming UNINTERESTING.
1152 if (limiting_can_increase_treesame(revs))
1153 for (list = newlist; list; list = list->next) {
1154 struct commit *c = list->item;
1155 if (c->object.flags & (UNINTERESTING | TREESAME))
1156 continue;
1157 update_treesame(revs, c);
1160 revs->commits = newlist;
1161 return 0;
1165 * Add an entry to refs->cmdline with the specified information.
1166 * *name is copied.
1168 static void add_rev_cmdline(struct rev_info *revs,
1169 struct object *item,
1170 const char *name,
1171 int whence,
1172 unsigned flags)
1174 struct rev_cmdline_info *info = &revs->cmdline;
1175 int nr = info->nr;
1177 ALLOC_GROW(info->rev, nr + 1, info->alloc);
1178 info->rev[nr].item = item;
1179 info->rev[nr].name = xstrdup(name);
1180 info->rev[nr].whence = whence;
1181 info->rev[nr].flags = flags;
1182 info->nr++;
1185 static void add_rev_cmdline_list(struct rev_info *revs,
1186 struct commit_list *commit_list,
1187 int whence,
1188 unsigned flags)
1190 while (commit_list) {
1191 struct object *object = &commit_list->item->object;
1192 add_rev_cmdline(revs, object, sha1_to_hex(object->sha1),
1193 whence, flags);
1194 commit_list = commit_list->next;
1198 struct all_refs_cb {
1199 int all_flags;
1200 int warned_bad_reflog;
1201 struct rev_info *all_revs;
1202 const char *name_for_errormsg;
1205 int ref_excluded(struct string_list *ref_excludes, const char *path)
1207 struct string_list_item *item;
1209 if (!ref_excludes)
1210 return 0;
1211 for_each_string_list_item(item, ref_excludes) {
1212 if (!wildmatch(item->string, path, 0, NULL))
1213 return 1;
1215 return 0;
1218 static int handle_one_ref(const char *path, const struct object_id *oid,
1219 int flag, void *cb_data)
1221 struct all_refs_cb *cb = cb_data;
1222 struct object *object;
1224 if (ref_excluded(cb->all_revs->ref_excludes, path))
1225 return 0;
1227 object = get_reference(cb->all_revs, path, oid->hash, cb->all_flags);
1228 add_rev_cmdline(cb->all_revs, object, path, REV_CMD_REF, cb->all_flags);
1229 add_pending_sha1(cb->all_revs, path, oid->hash, cb->all_flags);
1230 return 0;
1233 static void init_all_refs_cb(struct all_refs_cb *cb, struct rev_info *revs,
1234 unsigned flags)
1236 cb->all_revs = revs;
1237 cb->all_flags = flags;
1240 void clear_ref_exclusion(struct string_list **ref_excludes_p)
1242 if (*ref_excludes_p) {
1243 string_list_clear(*ref_excludes_p, 0);
1244 free(*ref_excludes_p);
1246 *ref_excludes_p = NULL;
1249 void add_ref_exclusion(struct string_list **ref_excludes_p, const char *exclude)
1251 if (!*ref_excludes_p) {
1252 *ref_excludes_p = xcalloc(1, sizeof(**ref_excludes_p));
1253 (*ref_excludes_p)->strdup_strings = 1;
1255 string_list_append(*ref_excludes_p, exclude);
1258 static void handle_refs(const char *submodule, struct rev_info *revs, unsigned flags,
1259 int (*for_each)(const char *, each_ref_fn, void *))
1261 struct all_refs_cb cb;
1262 init_all_refs_cb(&cb, revs, flags);
1263 for_each(submodule, handle_one_ref, &cb);
1266 static void handle_one_reflog_commit(unsigned char *sha1, void *cb_data)
1268 struct all_refs_cb *cb = cb_data;
1269 if (!is_null_sha1(sha1)) {
1270 struct object *o = parse_object(sha1);
1271 if (o) {
1272 o->flags |= cb->all_flags;
1273 /* ??? CMDLINEFLAGS ??? */
1274 add_pending_object(cb->all_revs, o, "");
1276 else if (!cb->warned_bad_reflog) {
1277 warning("reflog of '%s' references pruned commits",
1278 cb->name_for_errormsg);
1279 cb->warned_bad_reflog = 1;
1284 static int handle_one_reflog_ent(unsigned char *osha1, unsigned char *nsha1,
1285 const char *email, unsigned long timestamp, int tz,
1286 const char *message, void *cb_data)
1288 handle_one_reflog_commit(osha1, cb_data);
1289 handle_one_reflog_commit(nsha1, cb_data);
1290 return 0;
1293 static int handle_one_reflog(const char *path, const struct object_id *oid,
1294 int flag, void *cb_data)
1296 struct all_refs_cb *cb = cb_data;
1297 cb->warned_bad_reflog = 0;
1298 cb->name_for_errormsg = path;
1299 for_each_reflog_ent(path, handle_one_reflog_ent, cb_data);
1300 return 0;
1303 void add_reflogs_to_pending(struct rev_info *revs, unsigned flags)
1305 struct all_refs_cb cb;
1307 cb.all_revs = revs;
1308 cb.all_flags = flags;
1309 for_each_reflog(handle_one_reflog, &cb);
1312 static void add_cache_tree(struct cache_tree *it, struct rev_info *revs,
1313 struct strbuf *path)
1315 size_t baselen = path->len;
1316 int i;
1318 if (it->entry_count >= 0) {
1319 struct tree *tree = lookup_tree(it->sha1);
1320 add_pending_object_with_path(revs, &tree->object, "",
1321 040000, path->buf);
1324 for (i = 0; i < it->subtree_nr; i++) {
1325 struct cache_tree_sub *sub = it->down[i];
1326 strbuf_addf(path, "%s%s", baselen ? "/" : "", sub->name);
1327 add_cache_tree(sub->cache_tree, revs, path);
1328 strbuf_setlen(path, baselen);
1333 void add_index_objects_to_pending(struct rev_info *revs, unsigned flags)
1335 int i;
1337 read_cache();
1338 for (i = 0; i < active_nr; i++) {
1339 struct cache_entry *ce = active_cache[i];
1340 struct blob *blob;
1342 if (S_ISGITLINK(ce->ce_mode))
1343 continue;
1345 blob = lookup_blob(ce->sha1);
1346 if (!blob)
1347 die("unable to add index blob to traversal");
1348 add_pending_object_with_path(revs, &blob->object, "",
1349 ce->ce_mode, ce->name);
1352 if (active_cache_tree) {
1353 struct strbuf path = STRBUF_INIT;
1354 add_cache_tree(active_cache_tree, revs, &path);
1355 strbuf_release(&path);
1359 static int add_parents_only(struct rev_info *revs, const char *arg_, int flags)
1361 unsigned char sha1[20];
1362 struct object *it;
1363 struct commit *commit;
1364 struct commit_list *parents;
1365 const char *arg = arg_;
1367 if (*arg == '^') {
1368 flags ^= UNINTERESTING | BOTTOM;
1369 arg++;
1371 if (get_sha1_committish(arg, sha1))
1372 return 0;
1373 while (1) {
1374 it = get_reference(revs, arg, sha1, 0);
1375 if (!it && revs->ignore_missing)
1376 return 0;
1377 if (it->type != OBJ_TAG)
1378 break;
1379 if (!((struct tag*)it)->tagged)
1380 return 0;
1381 hashcpy(sha1, ((struct tag*)it)->tagged->sha1);
1383 if (it->type != OBJ_COMMIT)
1384 return 0;
1385 commit = (struct commit *)it;
1386 for (parents = commit->parents; parents; parents = parents->next) {
1387 it = &parents->item->object;
1388 it->flags |= flags;
1389 add_rev_cmdline(revs, it, arg_, REV_CMD_PARENTS_ONLY, flags);
1390 add_pending_object(revs, it, arg);
1392 return 1;
1395 void init_revisions(struct rev_info *revs, const char *prefix)
1397 memset(revs, 0, sizeof(*revs));
1399 revs->abbrev = DEFAULT_ABBREV;
1400 revs->ignore_merges = 1;
1401 revs->simplify_history = 1;
1402 DIFF_OPT_SET(&revs->pruning, RECURSIVE);
1403 DIFF_OPT_SET(&revs->pruning, QUICK);
1404 revs->pruning.add_remove = file_add_remove;
1405 revs->pruning.change = file_change;
1406 revs->sort_order = REV_SORT_IN_GRAPH_ORDER;
1407 revs->dense = 1;
1408 revs->prefix = prefix;
1409 revs->max_age = -1;
1410 revs->min_age = -1;
1411 revs->skip_count = -1;
1412 revs->max_count = -1;
1413 revs->max_parents = -1;
1415 revs->commit_format = CMIT_FMT_DEFAULT;
1417 init_grep_defaults();
1418 grep_init(&revs->grep_filter, prefix);
1419 revs->grep_filter.status_only = 1;
1420 revs->grep_filter.regflags = REG_NEWLINE;
1422 diff_setup(&revs->diffopt);
1423 if (prefix && !revs->diffopt.prefix) {
1424 revs->diffopt.prefix = prefix;
1425 revs->diffopt.prefix_length = strlen(prefix);
1428 revs->notes_opt.use_default_notes = -1;
1431 static struct commit *lookup_other_merge_candidate(const char **other_name)
1433 unsigned char sha1[20];
1434 int i;
1435 static const char *const other_head[] = {
1436 "MERGE_HEAD", "CHERRY_PICK_HEAD", "REVERT_HEAD"
1439 for (i = 0; i < ARRAY_SIZE(other_head); i++)
1440 if (!get_sha1(other_head[i], sha1)) {
1441 *other_name = other_head[i];
1442 return lookup_commit_or_die(sha1, *other_name);
1445 die("--merge without MERGE_HEAD, CHERRY_PICK_HEAD, or REVERT_HEAD?");
1448 static void add_pending_commit_list(struct rev_info *revs,
1449 struct commit_list *commit_list,
1450 unsigned int flags)
1452 while (commit_list) {
1453 struct object *object = &commit_list->item->object;
1454 object->flags |= flags;
1455 add_pending_object(revs, object, sha1_to_hex(object->sha1));
1456 commit_list = commit_list->next;
1460 static void prepare_show_merge(struct rev_info *revs)
1462 struct commit_list *bases;
1463 struct commit *head, *other;
1464 unsigned char sha1[20];
1465 const char *other_name;
1466 const char **prune = NULL;
1467 int i, prune_num = 1; /* counting terminating NULL */
1469 if (get_sha1("HEAD", sha1))
1470 die("--merge without HEAD?");
1471 head = lookup_commit_or_die(sha1, "HEAD");
1472 other = lookup_other_merge_candidate(&other_name);
1473 add_pending_object(revs, &head->object, "HEAD");
1474 add_pending_object(revs, &other->object, other_name);
1475 bases = get_merge_bases(head, other);
1476 add_rev_cmdline_list(revs, bases, REV_CMD_MERGE_BASE, UNINTERESTING | BOTTOM);
1477 add_pending_commit_list(revs, bases, UNINTERESTING | BOTTOM);
1478 free_commit_list(bases);
1479 head->object.flags |= SYMMETRIC_LEFT;
1481 if (!active_nr)
1482 read_cache();
1483 for (i = 0; i < active_nr; i++) {
1484 const struct cache_entry *ce = active_cache[i];
1485 if (!ce_stage(ce))
1486 continue;
1487 if (ce_path_match(ce, &revs->prune_data, NULL)) {
1488 prune_num++;
1489 REALLOC_ARRAY(prune, prune_num);
1490 prune[prune_num-2] = ce->name;
1491 prune[prune_num-1] = NULL;
1493 while ((i+1 < active_nr) &&
1494 ce_same_name(ce, active_cache[i+1]))
1495 i++;
1497 free_pathspec(&revs->prune_data);
1498 parse_pathspec(&revs->prune_data, PATHSPEC_ALL_MAGIC & ~PATHSPEC_LITERAL,
1499 PATHSPEC_PREFER_FULL | PATHSPEC_LITERAL_PATH, "", prune);
1500 revs->limited = 1;
1503 int handle_revision_arg(const char *arg_, struct rev_info *revs, int flags, unsigned revarg_opt)
1505 struct object_context oc;
1506 char *dotdot;
1507 struct object *object;
1508 unsigned char sha1[20];
1509 int local_flags;
1510 const char *arg = arg_;
1511 int cant_be_filename = revarg_opt & REVARG_CANNOT_BE_FILENAME;
1512 unsigned get_sha1_flags = 0;
1514 flags = flags & UNINTERESTING ? flags | BOTTOM : flags & ~BOTTOM;
1516 dotdot = strstr(arg, "..");
1517 if (dotdot) {
1518 unsigned char from_sha1[20];
1519 const char *next = dotdot + 2;
1520 const char *this = arg;
1521 int symmetric = *next == '.';
1522 unsigned int flags_exclude = flags ^ (UNINTERESTING | BOTTOM);
1523 static const char head_by_default[] = "HEAD";
1524 unsigned int a_flags;
1526 *dotdot = 0;
1527 next += symmetric;
1529 if (!*next)
1530 next = head_by_default;
1531 if (dotdot == arg)
1532 this = head_by_default;
1533 if (this == head_by_default && next == head_by_default &&
1534 !symmetric) {
1536 * Just ".."? That is not a range but the
1537 * pathspec for the parent directory.
1539 if (!cant_be_filename) {
1540 *dotdot = '.';
1541 return -1;
1544 if (!get_sha1_committish(this, from_sha1) &&
1545 !get_sha1_committish(next, sha1)) {
1546 struct object *a_obj, *b_obj;
1548 if (!cant_be_filename) {
1549 *dotdot = '.';
1550 verify_non_filename(revs->prefix, arg);
1553 a_obj = parse_object(from_sha1);
1554 b_obj = parse_object(sha1);
1555 if (!a_obj || !b_obj) {
1556 missing:
1557 if (revs->ignore_missing)
1558 return 0;
1559 die(symmetric
1560 ? "Invalid symmetric difference expression %s"
1561 : "Invalid revision range %s", arg);
1564 if (!symmetric) {
1565 /* just A..B */
1566 a_flags = flags_exclude;
1567 } else {
1568 /* A...B -- find merge bases between the two */
1569 struct commit *a, *b;
1570 struct commit_list *exclude;
1572 a = (a_obj->type == OBJ_COMMIT
1573 ? (struct commit *)a_obj
1574 : lookup_commit_reference(a_obj->sha1));
1575 b = (b_obj->type == OBJ_COMMIT
1576 ? (struct commit *)b_obj
1577 : lookup_commit_reference(b_obj->sha1));
1578 if (!a || !b)
1579 goto missing;
1580 exclude = get_merge_bases(a, b);
1581 add_rev_cmdline_list(revs, exclude,
1582 REV_CMD_MERGE_BASE,
1583 flags_exclude);
1584 add_pending_commit_list(revs, exclude,
1585 flags_exclude);
1586 free_commit_list(exclude);
1588 a_flags = flags | SYMMETRIC_LEFT;
1591 a_obj->flags |= a_flags;
1592 b_obj->flags |= flags;
1593 add_rev_cmdline(revs, a_obj, this,
1594 REV_CMD_LEFT, a_flags);
1595 add_rev_cmdline(revs, b_obj, next,
1596 REV_CMD_RIGHT, flags);
1597 add_pending_object(revs, a_obj, this);
1598 add_pending_object(revs, b_obj, next);
1599 return 0;
1601 *dotdot = '.';
1603 dotdot = strstr(arg, "^@");
1604 if (dotdot && !dotdot[2]) {
1605 *dotdot = 0;
1606 if (add_parents_only(revs, arg, flags))
1607 return 0;
1608 *dotdot = '^';
1610 dotdot = strstr(arg, "^!");
1611 if (dotdot && !dotdot[2]) {
1612 *dotdot = 0;
1613 if (!add_parents_only(revs, arg, flags ^ (UNINTERESTING | BOTTOM)))
1614 *dotdot = '^';
1617 local_flags = 0;
1618 if (*arg == '^') {
1619 local_flags = UNINTERESTING | BOTTOM;
1620 arg++;
1623 if (revarg_opt & REVARG_COMMITTISH)
1624 get_sha1_flags = GET_SHA1_COMMITTISH;
1626 if (get_sha1_with_context(arg, get_sha1_flags, sha1, &oc))
1627 return revs->ignore_missing ? 0 : -1;
1628 if (!cant_be_filename)
1629 verify_non_filename(revs->prefix, arg);
1630 object = get_reference(revs, arg, sha1, flags ^ local_flags);
1631 add_rev_cmdline(revs, object, arg_, REV_CMD_REV, flags ^ local_flags);
1632 add_pending_object_with_mode(revs, object, arg, oc.mode);
1633 return 0;
1636 struct cmdline_pathspec {
1637 int alloc;
1638 int nr;
1639 const char **path;
1642 static void append_prune_data(struct cmdline_pathspec *prune, const char **av)
1644 while (*av) {
1645 ALLOC_GROW(prune->path, prune->nr + 1, prune->alloc);
1646 prune->path[prune->nr++] = *(av++);
1650 static void read_pathspec_from_stdin(struct rev_info *revs, struct strbuf *sb,
1651 struct cmdline_pathspec *prune)
1653 while (strbuf_getwholeline(sb, stdin, '\n') != EOF) {
1654 int len = sb->len;
1655 if (len && sb->buf[len - 1] == '\n')
1656 sb->buf[--len] = '\0';
1657 if (len && sb->buf[len - 1] == '\r')
1658 sb->buf[--len] = '\0';
1659 ALLOC_GROW(prune->path, prune->nr + 1, prune->alloc);
1660 prune->path[prune->nr++] = xstrdup(sb->buf);
1664 static void read_revisions_from_stdin(struct rev_info *revs,
1665 struct cmdline_pathspec *prune)
1667 struct strbuf sb;
1668 int seen_dashdash = 0;
1669 int save_warning;
1671 save_warning = warn_on_object_refname_ambiguity;
1672 warn_on_object_refname_ambiguity = 0;
1674 strbuf_init(&sb, 1000);
1675 while (strbuf_getwholeline(&sb, stdin, '\n') != EOF) {
1676 int len = sb.len;
1677 if (len && sb.buf[len - 1] == '\n')
1678 sb.buf[--len] = '\0';
1679 if (len && sb.buf[len - 1] == '\r')
1680 sb.buf[--len] = '\0';
1681 if (!len)
1682 break;
1683 if (sb.buf[0] == '-') {
1684 if (len == 2 && sb.buf[1] == '-') {
1685 seen_dashdash = 1;
1686 break;
1688 die("options not supported in --stdin mode");
1690 if (handle_revision_arg(sb.buf, revs, 0,
1691 REVARG_CANNOT_BE_FILENAME))
1692 die("bad revision '%s'", sb.buf);
1694 if (seen_dashdash)
1695 read_pathspec_from_stdin(revs, &sb, prune);
1697 strbuf_release(&sb);
1698 warn_on_object_refname_ambiguity = save_warning;
1701 static void add_grep(struct rev_info *revs, const char *ptn, enum grep_pat_token what)
1703 append_grep_pattern(&revs->grep_filter, ptn, "command line", 0, what);
1706 static void add_header_grep(struct rev_info *revs, enum grep_header_field field, const char *pattern)
1708 append_header_grep_pattern(&revs->grep_filter, field, pattern);
1711 static void add_message_grep(struct rev_info *revs, const char *pattern)
1713 add_grep(revs, pattern, GREP_PATTERN_BODY);
1716 static int handle_revision_opt(struct rev_info *revs, int argc, const char **argv,
1717 int *unkc, const char **unkv)
1719 const char *arg = argv[0];
1720 const char *optarg;
1721 int argcount;
1723 /* pseudo revision arguments */
1724 if (!strcmp(arg, "--all") || !strcmp(arg, "--branches") ||
1725 !strcmp(arg, "--tags") || !strcmp(arg, "--remotes") ||
1726 !strcmp(arg, "--reflog") || !strcmp(arg, "--not") ||
1727 !strcmp(arg, "--no-walk") || !strcmp(arg, "--do-walk") ||
1728 !strcmp(arg, "--bisect") || starts_with(arg, "--glob=") ||
1729 !strcmp(arg, "--indexed-objects") ||
1730 starts_with(arg, "--exclude=") ||
1731 starts_with(arg, "--branches=") || starts_with(arg, "--tags=") ||
1732 starts_with(arg, "--remotes=") || starts_with(arg, "--no-walk="))
1734 unkv[(*unkc)++] = arg;
1735 return 1;
1738 if ((argcount = parse_long_opt("max-count", argv, &optarg))) {
1739 revs->max_count = atoi(optarg);
1740 revs->no_walk = 0;
1741 return argcount;
1742 } else if ((argcount = parse_long_opt("skip", argv, &optarg))) {
1743 revs->skip_count = atoi(optarg);
1744 return argcount;
1745 } else if ((*arg == '-') && isdigit(arg[1])) {
1746 /* accept -<digit>, like traditional "head" */
1747 if (strtol_i(arg + 1, 10, &revs->max_count) < 0 ||
1748 revs->max_count < 0)
1749 die("'%s': not a non-negative integer", arg + 1);
1750 revs->no_walk = 0;
1751 } else if (!strcmp(arg, "-n")) {
1752 if (argc <= 1)
1753 return error("-n requires an argument");
1754 revs->max_count = atoi(argv[1]);
1755 revs->no_walk = 0;
1756 return 2;
1757 } else if (starts_with(arg, "-n")) {
1758 revs->max_count = atoi(arg + 2);
1759 revs->no_walk = 0;
1760 } else if ((argcount = parse_long_opt("max-age", argv, &optarg))) {
1761 revs->max_age = atoi(optarg);
1762 return argcount;
1763 } else if ((argcount = parse_long_opt("since", argv, &optarg))) {
1764 revs->max_age = approxidate(optarg);
1765 return argcount;
1766 } else if ((argcount = parse_long_opt("after", argv, &optarg))) {
1767 revs->max_age = approxidate(optarg);
1768 return argcount;
1769 } else if ((argcount = parse_long_opt("min-age", argv, &optarg))) {
1770 revs->min_age = atoi(optarg);
1771 return argcount;
1772 } else if ((argcount = parse_long_opt("before", argv, &optarg))) {
1773 revs->min_age = approxidate(optarg);
1774 return argcount;
1775 } else if ((argcount = parse_long_opt("until", argv, &optarg))) {
1776 revs->min_age = approxidate(optarg);
1777 return argcount;
1778 } else if (!strcmp(arg, "--first-parent")) {
1779 revs->first_parent_only = 1;
1780 } else if (!strcmp(arg, "--ancestry-path")) {
1781 revs->ancestry_path = 1;
1782 revs->simplify_history = 0;
1783 revs->limited = 1;
1784 } else if (!strcmp(arg, "-g") || !strcmp(arg, "--walk-reflogs")) {
1785 init_reflog_walk(&revs->reflog_info);
1786 } else if (!strcmp(arg, "--default")) {
1787 if (argc <= 1)
1788 return error("bad --default argument");
1789 revs->def = argv[1];
1790 return 2;
1791 } else if (!strcmp(arg, "--merge")) {
1792 revs->show_merge = 1;
1793 } else if (!strcmp(arg, "--topo-order")) {
1794 revs->sort_order = REV_SORT_IN_GRAPH_ORDER;
1795 revs->topo_order = 1;
1796 } else if (!strcmp(arg, "--simplify-merges")) {
1797 revs->simplify_merges = 1;
1798 revs->topo_order = 1;
1799 revs->rewrite_parents = 1;
1800 revs->simplify_history = 0;
1801 revs->limited = 1;
1802 } else if (!strcmp(arg, "--simplify-by-decoration")) {
1803 revs->simplify_merges = 1;
1804 revs->topo_order = 1;
1805 revs->rewrite_parents = 1;
1806 revs->simplify_history = 0;
1807 revs->simplify_by_decoration = 1;
1808 revs->limited = 1;
1809 revs->prune = 1;
1810 load_ref_decorations(DECORATE_SHORT_REFS);
1811 } else if (!strcmp(arg, "--date-order")) {
1812 revs->sort_order = REV_SORT_BY_COMMIT_DATE;
1813 revs->topo_order = 1;
1814 } else if (!strcmp(arg, "--author-date-order")) {
1815 revs->sort_order = REV_SORT_BY_AUTHOR_DATE;
1816 revs->topo_order = 1;
1817 } else if (starts_with(arg, "--early-output")) {
1818 int count = 100;
1819 switch (arg[14]) {
1820 case '=':
1821 count = atoi(arg+15);
1822 /* Fallthrough */
1823 case 0:
1824 revs->topo_order = 1;
1825 revs->early_output = count;
1827 } else if (!strcmp(arg, "--parents")) {
1828 revs->rewrite_parents = 1;
1829 revs->print_parents = 1;
1830 } else if (!strcmp(arg, "--dense")) {
1831 revs->dense = 1;
1832 } else if (!strcmp(arg, "--sparse")) {
1833 revs->dense = 0;
1834 } else if (!strcmp(arg, "--show-all")) {
1835 revs->show_all = 1;
1836 } else if (!strcmp(arg, "--remove-empty")) {
1837 revs->remove_empty_trees = 1;
1838 } else if (!strcmp(arg, "--merges")) {
1839 revs->min_parents = 2;
1840 } else if (!strcmp(arg, "--no-merges")) {
1841 revs->max_parents = 1;
1842 } else if (starts_with(arg, "--min-parents=")) {
1843 revs->min_parents = atoi(arg+14);
1844 } else if (starts_with(arg, "--no-min-parents")) {
1845 revs->min_parents = 0;
1846 } else if (starts_with(arg, "--max-parents=")) {
1847 revs->max_parents = atoi(arg+14);
1848 } else if (starts_with(arg, "--no-max-parents")) {
1849 revs->max_parents = -1;
1850 } else if (!strcmp(arg, "--boundary")) {
1851 revs->boundary = 1;
1852 } else if (!strcmp(arg, "--left-right")) {
1853 revs->left_right = 1;
1854 } else if (!strcmp(arg, "--left-only")) {
1855 if (revs->right_only)
1856 die("--left-only is incompatible with --right-only"
1857 " or --cherry");
1858 revs->left_only = 1;
1859 } else if (!strcmp(arg, "--right-only")) {
1860 if (revs->left_only)
1861 die("--right-only is incompatible with --left-only");
1862 revs->right_only = 1;
1863 } else if (!strcmp(arg, "--cherry")) {
1864 if (revs->left_only)
1865 die("--cherry is incompatible with --left-only");
1866 revs->cherry_mark = 1;
1867 revs->right_only = 1;
1868 revs->max_parents = 1;
1869 revs->limited = 1;
1870 } else if (!strcmp(arg, "--count")) {
1871 revs->count = 1;
1872 } else if (!strcmp(arg, "--cherry-mark")) {
1873 if (revs->cherry_pick)
1874 die("--cherry-mark is incompatible with --cherry-pick");
1875 revs->cherry_mark = 1;
1876 revs->limited = 1; /* needs limit_list() */
1877 } else if (!strcmp(arg, "--cherry-pick")) {
1878 if (revs->cherry_mark)
1879 die("--cherry-pick is incompatible with --cherry-mark");
1880 revs->cherry_pick = 1;
1881 revs->limited = 1;
1882 } else if (!strcmp(arg, "--objects")) {
1883 revs->tag_objects = 1;
1884 revs->tree_objects = 1;
1885 revs->blob_objects = 1;
1886 } else if (!strcmp(arg, "--objects-edge")) {
1887 revs->tag_objects = 1;
1888 revs->tree_objects = 1;
1889 revs->blob_objects = 1;
1890 revs->edge_hint = 1;
1891 } else if (!strcmp(arg, "--objects-edge-aggressive")) {
1892 revs->tag_objects = 1;
1893 revs->tree_objects = 1;
1894 revs->blob_objects = 1;
1895 revs->edge_hint = 1;
1896 revs->edge_hint_aggressive = 1;
1897 } else if (!strcmp(arg, "--verify-objects")) {
1898 revs->tag_objects = 1;
1899 revs->tree_objects = 1;
1900 revs->blob_objects = 1;
1901 revs->verify_objects = 1;
1902 } else if (!strcmp(arg, "--unpacked")) {
1903 revs->unpacked = 1;
1904 } else if (starts_with(arg, "--unpacked=")) {
1905 die("--unpacked=<packfile> no longer supported.");
1906 } else if (!strcmp(arg, "-r")) {
1907 revs->diff = 1;
1908 DIFF_OPT_SET(&revs->diffopt, RECURSIVE);
1909 } else if (!strcmp(arg, "-t")) {
1910 revs->diff = 1;
1911 DIFF_OPT_SET(&revs->diffopt, RECURSIVE);
1912 DIFF_OPT_SET(&revs->diffopt, TREE_IN_RECURSIVE);
1913 } else if (!strcmp(arg, "-m")) {
1914 revs->ignore_merges = 0;
1915 } else if (!strcmp(arg, "-c")) {
1916 revs->diff = 1;
1917 revs->dense_combined_merges = 0;
1918 revs->combine_merges = 1;
1919 } else if (!strcmp(arg, "--cc")) {
1920 revs->diff = 1;
1921 revs->dense_combined_merges = 1;
1922 revs->combine_merges = 1;
1923 } else if (!strcmp(arg, "-v")) {
1924 revs->verbose_header = 1;
1925 } else if (!strcmp(arg, "--pretty")) {
1926 revs->verbose_header = 1;
1927 revs->pretty_given = 1;
1928 get_commit_format(NULL, revs);
1929 } else if (starts_with(arg, "--pretty=") || starts_with(arg, "--format=")) {
1931 * Detached form ("--pretty X" as opposed to "--pretty=X")
1932 * not allowed, since the argument is optional.
1934 revs->verbose_header = 1;
1935 revs->pretty_given = 1;
1936 get_commit_format(arg+9, revs);
1937 } else if (!strcmp(arg, "--show-notes") || !strcmp(arg, "--notes")) {
1938 revs->show_notes = 1;
1939 revs->show_notes_given = 1;
1940 revs->notes_opt.use_default_notes = 1;
1941 } else if (!strcmp(arg, "--show-signature")) {
1942 revs->show_signature = 1;
1943 } else if (!strcmp(arg, "--show-linear-break") ||
1944 starts_with(arg, "--show-linear-break=")) {
1945 if (starts_with(arg, "--show-linear-break="))
1946 revs->break_bar = xstrdup(arg + 20);
1947 else
1948 revs->break_bar = " ..........";
1949 revs->track_linear = 1;
1950 revs->track_first_time = 1;
1951 } else if (starts_with(arg, "--show-notes=") ||
1952 starts_with(arg, "--notes=")) {
1953 struct strbuf buf = STRBUF_INIT;
1954 revs->show_notes = 1;
1955 revs->show_notes_given = 1;
1956 if (starts_with(arg, "--show-notes")) {
1957 if (revs->notes_opt.use_default_notes < 0)
1958 revs->notes_opt.use_default_notes = 1;
1959 strbuf_addstr(&buf, arg+13);
1961 else
1962 strbuf_addstr(&buf, arg+8);
1963 expand_notes_ref(&buf);
1964 string_list_append(&revs->notes_opt.extra_notes_refs,
1965 strbuf_detach(&buf, NULL));
1966 } else if (!strcmp(arg, "--no-notes")) {
1967 revs->show_notes = 0;
1968 revs->show_notes_given = 1;
1969 revs->notes_opt.use_default_notes = -1;
1970 /* we have been strdup'ing ourselves, so trick
1971 * string_list into free()ing strings */
1972 revs->notes_opt.extra_notes_refs.strdup_strings = 1;
1973 string_list_clear(&revs->notes_opt.extra_notes_refs, 0);
1974 revs->notes_opt.extra_notes_refs.strdup_strings = 0;
1975 } else if (!strcmp(arg, "--standard-notes")) {
1976 revs->show_notes_given = 1;
1977 revs->notes_opt.use_default_notes = 1;
1978 } else if (!strcmp(arg, "--no-standard-notes")) {
1979 revs->notes_opt.use_default_notes = 0;
1980 } else if (!strcmp(arg, "--oneline")) {
1981 revs->verbose_header = 1;
1982 get_commit_format("oneline", revs);
1983 revs->pretty_given = 1;
1984 revs->abbrev_commit = 1;
1985 } else if (!strcmp(arg, "--graph")) {
1986 revs->topo_order = 1;
1987 revs->rewrite_parents = 1;
1988 revs->graph = graph_init(revs);
1989 } else if (!strcmp(arg, "--root")) {
1990 revs->show_root_diff = 1;
1991 } else if (!strcmp(arg, "--no-commit-id")) {
1992 revs->no_commit_id = 1;
1993 } else if (!strcmp(arg, "--always")) {
1994 revs->always_show_header = 1;
1995 } else if (!strcmp(arg, "--no-abbrev")) {
1996 revs->abbrev = 0;
1997 } else if (!strcmp(arg, "--abbrev")) {
1998 revs->abbrev = DEFAULT_ABBREV;
1999 } else if (starts_with(arg, "--abbrev=")) {
2000 revs->abbrev = strtoul(arg + 9, NULL, 10);
2001 if (revs->abbrev < MINIMUM_ABBREV)
2002 revs->abbrev = MINIMUM_ABBREV;
2003 else if (revs->abbrev > 40)
2004 revs->abbrev = 40;
2005 } else if (!strcmp(arg, "--abbrev-commit")) {
2006 revs->abbrev_commit = 1;
2007 revs->abbrev_commit_given = 1;
2008 } else if (!strcmp(arg, "--no-abbrev-commit")) {
2009 revs->abbrev_commit = 0;
2010 } else if (!strcmp(arg, "--full-diff")) {
2011 revs->diff = 1;
2012 revs->full_diff = 1;
2013 } else if (!strcmp(arg, "--full-history")) {
2014 revs->simplify_history = 0;
2015 } else if (!strcmp(arg, "--relative-date")) {
2016 revs->date_mode.type = DATE_RELATIVE;
2017 revs->date_mode_explicit = 1;
2018 } else if ((argcount = parse_long_opt("date", argv, &optarg))) {
2019 parse_date_format(optarg, &revs->date_mode);
2020 revs->date_mode_explicit = 1;
2021 return argcount;
2022 } else if (!strcmp(arg, "--log-size")) {
2023 revs->show_log_size = 1;
2026 * Grepping the commit log
2028 else if ((argcount = parse_long_opt("author", argv, &optarg))) {
2029 add_header_grep(revs, GREP_HEADER_AUTHOR, optarg);
2030 return argcount;
2031 } else if ((argcount = parse_long_opt("committer", argv, &optarg))) {
2032 add_header_grep(revs, GREP_HEADER_COMMITTER, optarg);
2033 return argcount;
2034 } else if ((argcount = parse_long_opt("grep-reflog", argv, &optarg))) {
2035 add_header_grep(revs, GREP_HEADER_REFLOG, optarg);
2036 return argcount;
2037 } else if ((argcount = parse_long_opt("grep", argv, &optarg))) {
2038 add_message_grep(revs, optarg);
2039 return argcount;
2040 } else if (!strcmp(arg, "--grep-debug")) {
2041 revs->grep_filter.debug = 1;
2042 } else if (!strcmp(arg, "--basic-regexp")) {
2043 grep_set_pattern_type_option(GREP_PATTERN_TYPE_BRE, &revs->grep_filter);
2044 } else if (!strcmp(arg, "--extended-regexp") || !strcmp(arg, "-E")) {
2045 grep_set_pattern_type_option(GREP_PATTERN_TYPE_ERE, &revs->grep_filter);
2046 } else if (!strcmp(arg, "--regexp-ignore-case") || !strcmp(arg, "-i")) {
2047 revs->grep_filter.regflags |= REG_ICASE;
2048 DIFF_OPT_SET(&revs->diffopt, PICKAXE_IGNORE_CASE);
2049 } else if (!strcmp(arg, "--fixed-strings") || !strcmp(arg, "-F")) {
2050 grep_set_pattern_type_option(GREP_PATTERN_TYPE_FIXED, &revs->grep_filter);
2051 } else if (!strcmp(arg, "--perl-regexp")) {
2052 grep_set_pattern_type_option(GREP_PATTERN_TYPE_PCRE, &revs->grep_filter);
2053 } else if (!strcmp(arg, "--all-match")) {
2054 revs->grep_filter.all_match = 1;
2055 } else if (!strcmp(arg, "--invert-grep")) {
2056 revs->invert_grep = 1;
2057 } else if ((argcount = parse_long_opt("encoding", argv, &optarg))) {
2058 if (strcmp(optarg, "none"))
2059 git_log_output_encoding = xstrdup(optarg);
2060 else
2061 git_log_output_encoding = "";
2062 return argcount;
2063 } else if (!strcmp(arg, "--reverse")) {
2064 revs->reverse ^= 1;
2065 } else if (!strcmp(arg, "--children")) {
2066 revs->children.name = "children";
2067 revs->limited = 1;
2068 } else if (!strcmp(arg, "--ignore-missing")) {
2069 revs->ignore_missing = 1;
2070 } else {
2071 int opts = diff_opt_parse(&revs->diffopt, argv, argc);
2072 if (!opts)
2073 unkv[(*unkc)++] = arg;
2074 return opts;
2076 if (revs->graph && revs->track_linear)
2077 die("--show-linear-break and --graph are incompatible");
2079 return 1;
2082 void parse_revision_opt(struct rev_info *revs, struct parse_opt_ctx_t *ctx,
2083 const struct option *options,
2084 const char * const usagestr[])
2086 int n = handle_revision_opt(revs, ctx->argc, ctx->argv,
2087 &ctx->cpidx, ctx->out);
2088 if (n <= 0) {
2089 error("unknown option `%s'", ctx->argv[0]);
2090 usage_with_options(usagestr, options);
2092 ctx->argv += n;
2093 ctx->argc -= n;
2096 static int for_each_bisect_ref(const char *submodule, each_ref_fn fn, void *cb_data, const char *term) {
2097 struct strbuf bisect_refs = STRBUF_INIT;
2098 int status;
2099 strbuf_addf(&bisect_refs, "refs/bisect/%s", term);
2100 status = for_each_ref_in_submodule(submodule, bisect_refs.buf, fn, cb_data);
2101 strbuf_release(&bisect_refs);
2102 return status;
2105 static int for_each_bad_bisect_ref(const char *submodule, each_ref_fn fn, void *cb_data)
2107 return for_each_bisect_ref(submodule, fn, cb_data, term_bad);
2110 static int for_each_good_bisect_ref(const char *submodule, each_ref_fn fn, void *cb_data)
2112 return for_each_bisect_ref(submodule, fn, cb_data, term_good);
2115 static int handle_revision_pseudo_opt(const char *submodule,
2116 struct rev_info *revs,
2117 int argc, const char **argv, int *flags)
2119 const char *arg = argv[0];
2120 const char *optarg;
2121 int argcount;
2124 * NOTE!
2126 * Commands like "git shortlog" will not accept the options below
2127 * unless parse_revision_opt queues them (as opposed to erroring
2128 * out).
2130 * When implementing your new pseudo-option, remember to
2131 * register it in the list at the top of handle_revision_opt.
2133 if (!strcmp(arg, "--all")) {
2134 handle_refs(submodule, revs, *flags, for_each_ref_submodule);
2135 handle_refs(submodule, revs, *flags, head_ref_submodule);
2136 clear_ref_exclusion(&revs->ref_excludes);
2137 } else if (!strcmp(arg, "--branches")) {
2138 handle_refs(submodule, revs, *flags, for_each_branch_ref_submodule);
2139 clear_ref_exclusion(&revs->ref_excludes);
2140 } else if (!strcmp(arg, "--bisect")) {
2141 read_bisect_terms(&term_bad, &term_good);
2142 handle_refs(submodule, revs, *flags, for_each_bad_bisect_ref);
2143 handle_refs(submodule, revs, *flags ^ (UNINTERESTING | BOTTOM), for_each_good_bisect_ref);
2144 revs->bisect = 1;
2145 } else if (!strcmp(arg, "--tags")) {
2146 handle_refs(submodule, revs, *flags, for_each_tag_ref_submodule);
2147 clear_ref_exclusion(&revs->ref_excludes);
2148 } else if (!strcmp(arg, "--remotes")) {
2149 handle_refs(submodule, revs, *flags, for_each_remote_ref_submodule);
2150 clear_ref_exclusion(&revs->ref_excludes);
2151 } else if ((argcount = parse_long_opt("glob", argv, &optarg))) {
2152 struct all_refs_cb cb;
2153 init_all_refs_cb(&cb, revs, *flags);
2154 for_each_glob_ref(handle_one_ref, optarg, &cb);
2155 clear_ref_exclusion(&revs->ref_excludes);
2156 return argcount;
2157 } else if ((argcount = parse_long_opt("exclude", argv, &optarg))) {
2158 add_ref_exclusion(&revs->ref_excludes, optarg);
2159 return argcount;
2160 } else if (starts_with(arg, "--branches=")) {
2161 struct all_refs_cb cb;
2162 init_all_refs_cb(&cb, revs, *flags);
2163 for_each_glob_ref_in(handle_one_ref, arg + 11, "refs/heads/", &cb);
2164 clear_ref_exclusion(&revs->ref_excludes);
2165 } else if (starts_with(arg, "--tags=")) {
2166 struct all_refs_cb cb;
2167 init_all_refs_cb(&cb, revs, *flags);
2168 for_each_glob_ref_in(handle_one_ref, arg + 7, "refs/tags/", &cb);
2169 clear_ref_exclusion(&revs->ref_excludes);
2170 } else if (starts_with(arg, "--remotes=")) {
2171 struct all_refs_cb cb;
2172 init_all_refs_cb(&cb, revs, *flags);
2173 for_each_glob_ref_in(handle_one_ref, arg + 10, "refs/remotes/", &cb);
2174 clear_ref_exclusion(&revs->ref_excludes);
2175 } else if (!strcmp(arg, "--reflog")) {
2176 add_reflogs_to_pending(revs, *flags);
2177 } else if (!strcmp(arg, "--indexed-objects")) {
2178 add_index_objects_to_pending(revs, *flags);
2179 } else if (!strcmp(arg, "--not")) {
2180 *flags ^= UNINTERESTING | BOTTOM;
2181 } else if (!strcmp(arg, "--no-walk")) {
2182 revs->no_walk = REVISION_WALK_NO_WALK_SORTED;
2183 } else if (starts_with(arg, "--no-walk=")) {
2185 * Detached form ("--no-walk X" as opposed to "--no-walk=X")
2186 * not allowed, since the argument is optional.
2188 if (!strcmp(arg + 10, "sorted"))
2189 revs->no_walk = REVISION_WALK_NO_WALK_SORTED;
2190 else if (!strcmp(arg + 10, "unsorted"))
2191 revs->no_walk = REVISION_WALK_NO_WALK_UNSORTED;
2192 else
2193 return error("invalid argument to --no-walk");
2194 } else if (!strcmp(arg, "--do-walk")) {
2195 revs->no_walk = 0;
2196 } else {
2197 return 0;
2200 return 1;
2203 static void NORETURN diagnose_missing_default(const char *def)
2205 unsigned char sha1[20];
2206 int flags;
2207 const char *refname;
2209 refname = resolve_ref_unsafe(def, 0, sha1, &flags);
2210 if (!refname || !(flags & REF_ISSYMREF) || (flags & REF_ISBROKEN))
2211 die(_("your current branch appears to be broken"));
2213 skip_prefix(refname, "refs/heads/", &refname);
2214 die(_("your current branch '%s' does not have any commits yet"),
2215 refname);
2219 * Parse revision information, filling in the "rev_info" structure,
2220 * and removing the used arguments from the argument list.
2222 * Returns the number of arguments left that weren't recognized
2223 * (which are also moved to the head of the argument list)
2225 int setup_revisions(int argc, const char **argv, struct rev_info *revs, struct setup_revision_opt *opt)
2227 int i, flags, left, seen_dashdash, read_from_stdin, got_rev_arg = 0, revarg_opt;
2228 struct cmdline_pathspec prune_data;
2229 const char *submodule = NULL;
2231 memset(&prune_data, 0, sizeof(prune_data));
2232 if (opt)
2233 submodule = opt->submodule;
2235 /* First, search for "--" */
2236 if (opt && opt->assume_dashdash) {
2237 seen_dashdash = 1;
2238 } else {
2239 seen_dashdash = 0;
2240 for (i = 1; i < argc; i++) {
2241 const char *arg = argv[i];
2242 if (strcmp(arg, "--"))
2243 continue;
2244 argv[i] = NULL;
2245 argc = i;
2246 if (argv[i + 1])
2247 append_prune_data(&prune_data, argv + i + 1);
2248 seen_dashdash = 1;
2249 break;
2253 /* Second, deal with arguments and options */
2254 flags = 0;
2255 revarg_opt = opt ? opt->revarg_opt : 0;
2256 if (seen_dashdash)
2257 revarg_opt |= REVARG_CANNOT_BE_FILENAME;
2258 read_from_stdin = 0;
2259 for (left = i = 1; i < argc; i++) {
2260 const char *arg = argv[i];
2261 if (*arg == '-') {
2262 int opts;
2264 opts = handle_revision_pseudo_opt(submodule,
2265 revs, argc - i, argv + i,
2266 &flags);
2267 if (opts > 0) {
2268 i += opts - 1;
2269 continue;
2272 if (!strcmp(arg, "--stdin")) {
2273 if (revs->disable_stdin) {
2274 argv[left++] = arg;
2275 continue;
2277 if (read_from_stdin++)
2278 die("--stdin given twice?");
2279 read_revisions_from_stdin(revs, &prune_data);
2280 continue;
2283 opts = handle_revision_opt(revs, argc - i, argv + i, &left, argv);
2284 if (opts > 0) {
2285 i += opts - 1;
2286 continue;
2288 if (opts < 0)
2289 exit(128);
2290 continue;
2294 if (handle_revision_arg(arg, revs, flags, revarg_opt)) {
2295 int j;
2296 if (seen_dashdash || *arg == '^')
2297 die("bad revision '%s'", arg);
2299 /* If we didn't have a "--":
2300 * (1) all filenames must exist;
2301 * (2) all rev-args must not be interpretable
2302 * as a valid filename.
2303 * but the latter we have checked in the main loop.
2305 for (j = i; j < argc; j++)
2306 verify_filename(revs->prefix, argv[j], j == i);
2308 append_prune_data(&prune_data, argv + i);
2309 break;
2311 else
2312 got_rev_arg = 1;
2315 if (prune_data.nr) {
2317 * If we need to introduce the magic "a lone ':' means no
2318 * pathspec whatsoever", here is the place to do so.
2320 * if (prune_data.nr == 1 && !strcmp(prune_data[0], ":")) {
2321 * prune_data.nr = 0;
2322 * prune_data.alloc = 0;
2323 * free(prune_data.path);
2324 * prune_data.path = NULL;
2325 * } else {
2326 * terminate prune_data.alloc with NULL and
2327 * call init_pathspec() to set revs->prune_data here.
2330 ALLOC_GROW(prune_data.path, prune_data.nr + 1, prune_data.alloc);
2331 prune_data.path[prune_data.nr++] = NULL;
2332 parse_pathspec(&revs->prune_data, 0, 0,
2333 revs->prefix, prune_data.path);
2336 if (revs->def == NULL)
2337 revs->def = opt ? opt->def : NULL;
2338 if (opt && opt->tweak)
2339 opt->tweak(revs, opt);
2340 if (revs->show_merge)
2341 prepare_show_merge(revs);
2342 if (revs->def && !revs->pending.nr && !got_rev_arg) {
2343 unsigned char sha1[20];
2344 struct object *object;
2345 struct object_context oc;
2346 if (get_sha1_with_context(revs->def, 0, sha1, &oc))
2347 diagnose_missing_default(revs->def);
2348 object = get_reference(revs, revs->def, sha1, 0);
2349 add_pending_object_with_mode(revs, object, revs->def, oc.mode);
2352 /* Did the user ask for any diff output? Run the diff! */
2353 if (revs->diffopt.output_format & ~DIFF_FORMAT_NO_OUTPUT)
2354 revs->diff = 1;
2356 /* Pickaxe, diff-filter and rename following need diffs */
2357 if (revs->diffopt.pickaxe ||
2358 revs->diffopt.filter ||
2359 DIFF_OPT_TST(&revs->diffopt, FOLLOW_RENAMES))
2360 revs->diff = 1;
2362 if (revs->topo_order)
2363 revs->limited = 1;
2365 if (revs->prune_data.nr) {
2366 copy_pathspec(&revs->pruning.pathspec, &revs->prune_data);
2367 /* Can't prune commits with rename following: the paths change.. */
2368 if (!DIFF_OPT_TST(&revs->diffopt, FOLLOW_RENAMES))
2369 revs->prune = 1;
2370 if (!revs->full_diff)
2371 copy_pathspec(&revs->diffopt.pathspec,
2372 &revs->prune_data);
2374 if (revs->combine_merges)
2375 revs->ignore_merges = 0;
2376 revs->diffopt.abbrev = revs->abbrev;
2378 if (revs->line_level_traverse) {
2379 revs->limited = 1;
2380 revs->topo_order = 1;
2383 diff_setup_done(&revs->diffopt);
2385 grep_commit_pattern_type(GREP_PATTERN_TYPE_UNSPECIFIED,
2386 &revs->grep_filter);
2387 compile_grep_patterns(&revs->grep_filter);
2389 if (revs->reverse && revs->reflog_info)
2390 die("cannot combine --reverse with --walk-reflogs");
2391 if (revs->rewrite_parents && revs->children.name)
2392 die("cannot combine --parents and --children");
2395 * Limitations on the graph functionality
2397 if (revs->reverse && revs->graph)
2398 die("cannot combine --reverse with --graph");
2400 if (revs->reflog_info && revs->graph)
2401 die("cannot combine --walk-reflogs with --graph");
2402 if (revs->no_walk && revs->graph)
2403 die("cannot combine --no-walk with --graph");
2404 if (!revs->reflog_info && revs->grep_filter.use_reflog_filter)
2405 die("cannot use --grep-reflog without --walk-reflogs");
2407 if (revs->first_parent_only && revs->bisect)
2408 die(_("--first-parent is incompatible with --bisect"));
2410 return left;
2413 static void add_child(struct rev_info *revs, struct commit *parent, struct commit *child)
2415 struct commit_list *l = xcalloc(1, sizeof(*l));
2417 l->item = child;
2418 l->next = add_decoration(&revs->children, &parent->object, l);
2421 static int remove_duplicate_parents(struct rev_info *revs, struct commit *commit)
2423 struct treesame_state *ts = lookup_decoration(&revs->treesame, &commit->object);
2424 struct commit_list **pp, *p;
2425 int surviving_parents;
2427 /* Examine existing parents while marking ones we have seen... */
2428 pp = &commit->parents;
2429 surviving_parents = 0;
2430 while ((p = *pp) != NULL) {
2431 struct commit *parent = p->item;
2432 if (parent->object.flags & TMP_MARK) {
2433 *pp = p->next;
2434 if (ts)
2435 compact_treesame(revs, commit, surviving_parents);
2436 continue;
2438 parent->object.flags |= TMP_MARK;
2439 surviving_parents++;
2440 pp = &p->next;
2442 /* clear the temporary mark */
2443 for (p = commit->parents; p; p = p->next) {
2444 p->item->object.flags &= ~TMP_MARK;
2446 /* no update_treesame() - removing duplicates can't affect TREESAME */
2447 return surviving_parents;
2450 struct merge_simplify_state {
2451 struct commit *simplified;
2454 static struct merge_simplify_state *locate_simplify_state(struct rev_info *revs, struct commit *commit)
2456 struct merge_simplify_state *st;
2458 st = lookup_decoration(&revs->merge_simplification, &commit->object);
2459 if (!st) {
2460 st = xcalloc(1, sizeof(*st));
2461 add_decoration(&revs->merge_simplification, &commit->object, st);
2463 return st;
2466 static int mark_redundant_parents(struct rev_info *revs, struct commit *commit)
2468 struct commit_list *h = reduce_heads(commit->parents);
2469 int i = 0, marked = 0;
2470 struct commit_list *po, *pn;
2472 /* Want these for sanity-checking only */
2473 int orig_cnt = commit_list_count(commit->parents);
2474 int cnt = commit_list_count(h);
2477 * Not ready to remove items yet, just mark them for now, based
2478 * on the output of reduce_heads(). reduce_heads outputs the reduced
2479 * set in its original order, so this isn't too hard.
2481 po = commit->parents;
2482 pn = h;
2483 while (po) {
2484 if (pn && po->item == pn->item) {
2485 pn = pn->next;
2486 i++;
2487 } else {
2488 po->item->object.flags |= TMP_MARK;
2489 marked++;
2491 po=po->next;
2494 if (i != cnt || cnt+marked != orig_cnt)
2495 die("mark_redundant_parents %d %d %d %d", orig_cnt, cnt, i, marked);
2497 free_commit_list(h);
2499 return marked;
2502 static int mark_treesame_root_parents(struct rev_info *revs, struct commit *commit)
2504 struct commit_list *p;
2505 int marked = 0;
2507 for (p = commit->parents; p; p = p->next) {
2508 struct commit *parent = p->item;
2509 if (!parent->parents && (parent->object.flags & TREESAME)) {
2510 parent->object.flags |= TMP_MARK;
2511 marked++;
2515 return marked;
2519 * Awkward naming - this means one parent we are TREESAME to.
2520 * cf mark_treesame_root_parents: root parents that are TREESAME (to an
2521 * empty tree). Better name suggestions?
2523 static int leave_one_treesame_to_parent(struct rev_info *revs, struct commit *commit)
2525 struct treesame_state *ts = lookup_decoration(&revs->treesame, &commit->object);
2526 struct commit *unmarked = NULL, *marked = NULL;
2527 struct commit_list *p;
2528 unsigned n;
2530 for (p = commit->parents, n = 0; p; p = p->next, n++) {
2531 if (ts->treesame[n]) {
2532 if (p->item->object.flags & TMP_MARK) {
2533 if (!marked)
2534 marked = p->item;
2535 } else {
2536 if (!unmarked) {
2537 unmarked = p->item;
2538 break;
2545 * If we are TREESAME to a marked-for-deletion parent, but not to any
2546 * unmarked parents, unmark the first TREESAME parent. This is the
2547 * parent that the default simplify_history==1 scan would have followed,
2548 * and it doesn't make sense to omit that path when asking for a
2549 * simplified full history. Retaining it improves the chances of
2550 * understanding odd missed merges that took an old version of a file.
2552 * Example:
2554 * I--------*X A modified the file, but mainline merge X used
2555 * \ / "-s ours", so took the version from I. X is
2556 * `-*A--' TREESAME to I and !TREESAME to A.
2558 * Default log from X would produce "I". Without this check,
2559 * --full-history --simplify-merges would produce "I-A-X", showing
2560 * the merge commit X and that it changed A, but not making clear that
2561 * it had just taken the I version. With this check, the topology above
2562 * is retained.
2564 * Note that it is possible that the simplification chooses a different
2565 * TREESAME parent from the default, in which case this test doesn't
2566 * activate, and we _do_ drop the default parent. Example:
2568 * I------X A modified the file, but it was reverted in B,
2569 * \ / meaning mainline merge X is TREESAME to both
2570 * *A-*B parents.
2572 * Default log would produce "I" by following the first parent;
2573 * --full-history --simplify-merges will produce "I-A-B". But this is a
2574 * reasonable result - it presents a logical full history leading from
2575 * I to X, and X is not an important merge.
2577 if (!unmarked && marked) {
2578 marked->object.flags &= ~TMP_MARK;
2579 return 1;
2582 return 0;
2585 static int remove_marked_parents(struct rev_info *revs, struct commit *commit)
2587 struct commit_list **pp, *p;
2588 int nth_parent, removed = 0;
2590 pp = &commit->parents;
2591 nth_parent = 0;
2592 while ((p = *pp) != NULL) {
2593 struct commit *parent = p->item;
2594 if (parent->object.flags & TMP_MARK) {
2595 parent->object.flags &= ~TMP_MARK;
2596 *pp = p->next;
2597 free(p);
2598 removed++;
2599 compact_treesame(revs, commit, nth_parent);
2600 continue;
2602 pp = &p->next;
2603 nth_parent++;
2606 /* Removing parents can only increase TREESAMEness */
2607 if (removed && !(commit->object.flags & TREESAME))
2608 update_treesame(revs, commit);
2610 return nth_parent;
2613 static struct commit_list **simplify_one(struct rev_info *revs, struct commit *commit, struct commit_list **tail)
2615 struct commit_list *p;
2616 struct commit *parent;
2617 struct merge_simplify_state *st, *pst;
2618 int cnt;
2620 st = locate_simplify_state(revs, commit);
2623 * Have we handled this one?
2625 if (st->simplified)
2626 return tail;
2629 * An UNINTERESTING commit simplifies to itself, so does a
2630 * root commit. We do not rewrite parents of such commit
2631 * anyway.
2633 if ((commit->object.flags & UNINTERESTING) || !commit->parents) {
2634 st->simplified = commit;
2635 return tail;
2639 * Do we know what commit all of our parents that matter
2640 * should be rewritten to? Otherwise we are not ready to
2641 * rewrite this one yet.
2643 for (cnt = 0, p = commit->parents; p; p = p->next) {
2644 pst = locate_simplify_state(revs, p->item);
2645 if (!pst->simplified) {
2646 tail = &commit_list_insert(p->item, tail)->next;
2647 cnt++;
2649 if (revs->first_parent_only)
2650 break;
2652 if (cnt) {
2653 tail = &commit_list_insert(commit, tail)->next;
2654 return tail;
2658 * Rewrite our list of parents. Note that this cannot
2659 * affect our TREESAME flags in any way - a commit is
2660 * always TREESAME to its simplification.
2662 for (p = commit->parents; p; p = p->next) {
2663 pst = locate_simplify_state(revs, p->item);
2664 p->item = pst->simplified;
2665 if (revs->first_parent_only)
2666 break;
2669 if (revs->first_parent_only)
2670 cnt = 1;
2671 else
2672 cnt = remove_duplicate_parents(revs, commit);
2675 * It is possible that we are a merge and one side branch
2676 * does not have any commit that touches the given paths;
2677 * in such a case, the immediate parent from that branch
2678 * will be rewritten to be the merge base.
2680 * o----X X: the commit we are looking at;
2681 * / / o: a commit that touches the paths;
2682 * ---o----'
2684 * Further, a merge of an independent branch that doesn't
2685 * touch the path will reduce to a treesame root parent:
2687 * ----o----X X: the commit we are looking at;
2688 * / o: a commit that touches the paths;
2689 * r r: a root commit not touching the paths
2691 * Detect and simplify both cases.
2693 if (1 < cnt) {
2694 int marked = mark_redundant_parents(revs, commit);
2695 marked += mark_treesame_root_parents(revs, commit);
2696 if (marked)
2697 marked -= leave_one_treesame_to_parent(revs, commit);
2698 if (marked)
2699 cnt = remove_marked_parents(revs, commit);
2703 * A commit simplifies to itself if it is a root, if it is
2704 * UNINTERESTING, if it touches the given paths, or if it is a
2705 * merge and its parents don't simplify to one relevant commit
2706 * (the first two cases are already handled at the beginning of
2707 * this function).
2709 * Otherwise, it simplifies to what its sole relevant parent
2710 * simplifies to.
2712 if (!cnt ||
2713 (commit->object.flags & UNINTERESTING) ||
2714 !(commit->object.flags & TREESAME) ||
2715 (parent = one_relevant_parent(revs, commit->parents)) == NULL)
2716 st->simplified = commit;
2717 else {
2718 pst = locate_simplify_state(revs, parent);
2719 st->simplified = pst->simplified;
2721 return tail;
2724 static void simplify_merges(struct rev_info *revs)
2726 struct commit_list *list, *next;
2727 struct commit_list *yet_to_do, **tail;
2728 struct commit *commit;
2730 if (!revs->prune)
2731 return;
2733 /* feed the list reversed */
2734 yet_to_do = NULL;
2735 for (list = revs->commits; list; list = next) {
2736 commit = list->item;
2737 next = list->next;
2739 * Do not free(list) here yet; the original list
2740 * is used later in this function.
2742 commit_list_insert(commit, &yet_to_do);
2744 while (yet_to_do) {
2745 list = yet_to_do;
2746 yet_to_do = NULL;
2747 tail = &yet_to_do;
2748 while (list) {
2749 commit = pop_commit(&list);
2750 tail = simplify_one(revs, commit, tail);
2754 /* clean up the result, removing the simplified ones */
2755 list = revs->commits;
2756 revs->commits = NULL;
2757 tail = &revs->commits;
2758 while (list) {
2759 struct merge_simplify_state *st;
2761 commit = pop_commit(&list);
2762 st = locate_simplify_state(revs, commit);
2763 if (st->simplified == commit)
2764 tail = &commit_list_insert(commit, tail)->next;
2768 static void set_children(struct rev_info *revs)
2770 struct commit_list *l;
2771 for (l = revs->commits; l; l = l->next) {
2772 struct commit *commit = l->item;
2773 struct commit_list *p;
2775 for (p = commit->parents; p; p = p->next)
2776 add_child(revs, p->item, commit);
2780 void reset_revision_walk(void)
2782 clear_object_flags(SEEN | ADDED | SHOWN);
2785 int prepare_revision_walk(struct rev_info *revs)
2787 int i;
2788 struct object_array old_pending;
2789 struct commit_list **next = &revs->commits;
2791 memcpy(&old_pending, &revs->pending, sizeof(old_pending));
2792 revs->pending.nr = 0;
2793 revs->pending.alloc = 0;
2794 revs->pending.objects = NULL;
2795 for (i = 0; i < old_pending.nr; i++) {
2796 struct object_array_entry *e = old_pending.objects + i;
2797 struct commit *commit = handle_commit(revs, e);
2798 if (commit) {
2799 if (!(commit->object.flags & SEEN)) {
2800 commit->object.flags |= SEEN;
2801 next = commit_list_append(commit, next);
2805 if (!revs->leak_pending)
2806 object_array_clear(&old_pending);
2808 /* Signal whether we need per-parent treesame decoration */
2809 if (revs->simplify_merges ||
2810 (revs->limited && limiting_can_increase_treesame(revs)))
2811 revs->treesame.name = "treesame";
2813 if (revs->no_walk != REVISION_WALK_NO_WALK_UNSORTED)
2814 commit_list_sort_by_date(&revs->commits);
2815 if (revs->no_walk)
2816 return 0;
2817 if (revs->limited)
2818 if (limit_list(revs) < 0)
2819 return -1;
2820 if (revs->topo_order)
2821 sort_in_topological_order(&revs->commits, revs->sort_order);
2822 if (revs->line_level_traverse)
2823 line_log_filter(revs);
2824 if (revs->simplify_merges)
2825 simplify_merges(revs);
2826 if (revs->children.name)
2827 set_children(revs);
2828 return 0;
2831 static enum rewrite_result rewrite_one(struct rev_info *revs, struct commit **pp)
2833 struct commit_list *cache = NULL;
2835 for (;;) {
2836 struct commit *p = *pp;
2837 if (!revs->limited)
2838 if (add_parents_to_list(revs, p, &revs->commits, &cache) < 0)
2839 return rewrite_one_error;
2840 if (p->object.flags & UNINTERESTING)
2841 return rewrite_one_ok;
2842 if (!(p->object.flags & TREESAME))
2843 return rewrite_one_ok;
2844 if (!p->parents)
2845 return rewrite_one_noparents;
2846 if ((p = one_relevant_parent(revs, p->parents)) == NULL)
2847 return rewrite_one_ok;
2848 *pp = p;
2852 int rewrite_parents(struct rev_info *revs, struct commit *commit,
2853 rewrite_parent_fn_t rewrite_parent)
2855 struct commit_list **pp = &commit->parents;
2856 while (*pp) {
2857 struct commit_list *parent = *pp;
2858 switch (rewrite_parent(revs, &parent->item)) {
2859 case rewrite_one_ok:
2860 break;
2861 case rewrite_one_noparents:
2862 *pp = parent->next;
2863 continue;
2864 case rewrite_one_error:
2865 return -1;
2867 pp = &parent->next;
2869 remove_duplicate_parents(revs, commit);
2870 return 0;
2873 static int commit_rewrite_person(struct strbuf *buf, const char *what, struct string_list *mailmap)
2875 char *person, *endp;
2876 size_t len, namelen, maillen;
2877 const char *name;
2878 const char *mail;
2879 struct ident_split ident;
2881 person = strstr(buf->buf, what);
2882 if (!person)
2883 return 0;
2885 person += strlen(what);
2886 endp = strchr(person, '\n');
2887 if (!endp)
2888 return 0;
2890 len = endp - person;
2892 if (split_ident_line(&ident, person, len))
2893 return 0;
2895 mail = ident.mail_begin;
2896 maillen = ident.mail_end - ident.mail_begin;
2897 name = ident.name_begin;
2898 namelen = ident.name_end - ident.name_begin;
2900 if (map_user(mailmap, &mail, &maillen, &name, &namelen)) {
2901 struct strbuf namemail = STRBUF_INIT;
2903 strbuf_addf(&namemail, "%.*s <%.*s>",
2904 (int)namelen, name, (int)maillen, mail);
2906 strbuf_splice(buf, ident.name_begin - buf->buf,
2907 ident.mail_end - ident.name_begin + 1,
2908 namemail.buf, namemail.len);
2910 strbuf_release(&namemail);
2912 return 1;
2915 return 0;
2918 static int commit_match(struct commit *commit, struct rev_info *opt)
2920 int retval;
2921 const char *encoding;
2922 const char *message;
2923 struct strbuf buf = STRBUF_INIT;
2925 if (!opt->grep_filter.pattern_list && !opt->grep_filter.header_list)
2926 return 1;
2928 /* Prepend "fake" headers as needed */
2929 if (opt->grep_filter.use_reflog_filter) {
2930 strbuf_addstr(&buf, "reflog ");
2931 get_reflog_message(&buf, opt->reflog_info);
2932 strbuf_addch(&buf, '\n');
2936 * We grep in the user's output encoding, under the assumption that it
2937 * is the encoding they are most likely to write their grep pattern
2938 * for. In addition, it means we will match the "notes" encoding below,
2939 * so we will not end up with a buffer that has two different encodings
2940 * in it.
2942 encoding = get_log_output_encoding();
2943 message = logmsg_reencode(commit, NULL, encoding);
2945 /* Copy the commit to temporary if we are using "fake" headers */
2946 if (buf.len)
2947 strbuf_addstr(&buf, message);
2949 if (opt->grep_filter.header_list && opt->mailmap) {
2950 if (!buf.len)
2951 strbuf_addstr(&buf, message);
2953 commit_rewrite_person(&buf, "\nauthor ", opt->mailmap);
2954 commit_rewrite_person(&buf, "\ncommitter ", opt->mailmap);
2957 /* Append "fake" message parts as needed */
2958 if (opt->show_notes) {
2959 if (!buf.len)
2960 strbuf_addstr(&buf, message);
2961 format_display_notes(commit->object.sha1, &buf, encoding, 1);
2965 * Find either in the original commit message, or in the temporary.
2966 * Note that we cast away the constness of "message" here. It is
2967 * const because it may come from the cached commit buffer. That's OK,
2968 * because we know that it is modifiable heap memory, and that while
2969 * grep_buffer may modify it for speed, it will restore any
2970 * changes before returning.
2972 if (buf.len)
2973 retval = grep_buffer(&opt->grep_filter, buf.buf, buf.len);
2974 else
2975 retval = grep_buffer(&opt->grep_filter,
2976 (char *)message, strlen(message));
2977 strbuf_release(&buf);
2978 unuse_commit_buffer(commit, message);
2979 return opt->invert_grep ? !retval : retval;
2982 static inline int want_ancestry(const struct rev_info *revs)
2984 return (revs->rewrite_parents || revs->children.name);
2987 enum commit_action get_commit_action(struct rev_info *revs, struct commit *commit)
2989 if (commit->object.flags & SHOWN)
2990 return commit_ignore;
2991 if (revs->unpacked && has_sha1_pack(commit->object.sha1))
2992 return commit_ignore;
2993 if (revs->show_all)
2994 return commit_show;
2995 if (commit->object.flags & UNINTERESTING)
2996 return commit_ignore;
2997 if (revs->min_age != -1 && (commit->date > revs->min_age))
2998 return commit_ignore;
2999 if (revs->min_parents || (revs->max_parents >= 0)) {
3000 int n = commit_list_count(commit->parents);
3001 if ((n < revs->min_parents) ||
3002 ((revs->max_parents >= 0) && (n > revs->max_parents)))
3003 return commit_ignore;
3005 if (!commit_match(commit, revs))
3006 return commit_ignore;
3007 if (revs->prune && revs->dense) {
3008 /* Commit without changes? */
3009 if (commit->object.flags & TREESAME) {
3010 int n;
3011 struct commit_list *p;
3012 /* drop merges unless we want parenthood */
3013 if (!want_ancestry(revs))
3014 return commit_ignore;
3016 * If we want ancestry, then need to keep any merges
3017 * between relevant commits to tie together topology.
3018 * For consistency with TREESAME and simplification
3019 * use "relevant" here rather than just INTERESTING,
3020 * to treat bottom commit(s) as part of the topology.
3022 for (n = 0, p = commit->parents; p; p = p->next)
3023 if (relevant_commit(p->item))
3024 if (++n >= 2)
3025 return commit_show;
3026 return commit_ignore;
3029 return commit_show;
3032 define_commit_slab(saved_parents, struct commit_list *);
3034 #define EMPTY_PARENT_LIST ((struct commit_list *)-1)
3037 * You may only call save_parents() once per commit (this is checked
3038 * for non-root commits).
3040 static void save_parents(struct rev_info *revs, struct commit *commit)
3042 struct commit_list **pp;
3044 if (!revs->saved_parents_slab) {
3045 revs->saved_parents_slab = xmalloc(sizeof(struct saved_parents));
3046 init_saved_parents(revs->saved_parents_slab);
3049 pp = saved_parents_at(revs->saved_parents_slab, commit);
3052 * When walking with reflogs, we may visit the same commit
3053 * several times: once for each appearance in the reflog.
3055 * In this case, save_parents() will be called multiple times.
3056 * We want to keep only the first set of parents. We need to
3057 * store a sentinel value for an empty (i.e., NULL) parent
3058 * list to distinguish it from a not-yet-saved list, however.
3060 if (*pp)
3061 return;
3062 if (commit->parents)
3063 *pp = copy_commit_list(commit->parents);
3064 else
3065 *pp = EMPTY_PARENT_LIST;
3068 static void free_saved_parents(struct rev_info *revs)
3070 if (revs->saved_parents_slab)
3071 clear_saved_parents(revs->saved_parents_slab);
3074 struct commit_list *get_saved_parents(struct rev_info *revs, const struct commit *commit)
3076 struct commit_list *parents;
3078 if (!revs->saved_parents_slab)
3079 return commit->parents;
3081 parents = *saved_parents_at(revs->saved_parents_slab, commit);
3082 if (parents == EMPTY_PARENT_LIST)
3083 return NULL;
3084 return parents;
3087 enum commit_action simplify_commit(struct rev_info *revs, struct commit *commit)
3089 enum commit_action action = get_commit_action(revs, commit);
3091 if (action == commit_show &&
3092 !revs->show_all &&
3093 revs->prune && revs->dense && want_ancestry(revs)) {
3095 * --full-diff on simplified parents is no good: it
3096 * will show spurious changes from the commits that
3097 * were elided. So we save the parents on the side
3098 * when --full-diff is in effect.
3100 if (revs->full_diff)
3101 save_parents(revs, commit);
3102 if (rewrite_parents(revs, commit, rewrite_one) < 0)
3103 return commit_error;
3105 return action;
3108 static void track_linear(struct rev_info *revs, struct commit *commit)
3110 if (revs->track_first_time) {
3111 revs->linear = 1;
3112 revs->track_first_time = 0;
3113 } else {
3114 struct commit_list *p;
3115 for (p = revs->previous_parents; p; p = p->next)
3116 if (p->item == NULL || /* first commit */
3117 !hashcmp(p->item->object.sha1, commit->object.sha1))
3118 break;
3119 revs->linear = p != NULL;
3121 if (revs->reverse) {
3122 if (revs->linear)
3123 commit->object.flags |= TRACK_LINEAR;
3125 free_commit_list(revs->previous_parents);
3126 revs->previous_parents = copy_commit_list(commit->parents);
3129 static struct commit *get_revision_1(struct rev_info *revs)
3131 if (!revs->commits)
3132 return NULL;
3134 do {
3135 struct commit *commit = pop_commit(&revs->commits);
3137 if (revs->reflog_info) {
3138 save_parents(revs, commit);
3139 fake_reflog_parent(revs->reflog_info, commit);
3140 commit->object.flags &= ~(ADDED | SEEN | SHOWN);
3144 * If we haven't done the list limiting, we need to look at
3145 * the parents here. We also need to do the date-based limiting
3146 * that we'd otherwise have done in limit_list().
3148 if (!revs->limited) {
3149 if (revs->max_age != -1 &&
3150 (commit->date < revs->max_age))
3151 continue;
3152 if (add_parents_to_list(revs, commit, &revs->commits, NULL) < 0) {
3153 if (!revs->ignore_missing_links)
3154 die("Failed to traverse parents of commit %s",
3155 sha1_to_hex(commit->object.sha1));
3159 switch (simplify_commit(revs, commit)) {
3160 case commit_ignore:
3161 continue;
3162 case commit_error:
3163 die("Failed to simplify parents of commit %s",
3164 sha1_to_hex(commit->object.sha1));
3165 default:
3166 if (revs->track_linear)
3167 track_linear(revs, commit);
3168 return commit;
3170 } while (revs->commits);
3171 return NULL;
3175 * Return true for entries that have not yet been shown. (This is an
3176 * object_array_each_func_t.)
3178 static int entry_unshown(struct object_array_entry *entry, void *cb_data_unused)
3180 return !(entry->item->flags & SHOWN);
3184 * If array is on the verge of a realloc, garbage-collect any entries
3185 * that have already been shown to try to free up some space.
3187 static void gc_boundary(struct object_array *array)
3189 if (array->nr == array->alloc)
3190 object_array_filter(array, entry_unshown, NULL);
3193 static void create_boundary_commit_list(struct rev_info *revs)
3195 unsigned i;
3196 struct commit *c;
3197 struct object_array *array = &revs->boundary_commits;
3198 struct object_array_entry *objects = array->objects;
3201 * If revs->commits is non-NULL at this point, an error occurred in
3202 * get_revision_1(). Ignore the error and continue printing the
3203 * boundary commits anyway. (This is what the code has always
3204 * done.)
3206 if (revs->commits) {
3207 free_commit_list(revs->commits);
3208 revs->commits = NULL;
3212 * Put all of the actual boundary commits from revs->boundary_commits
3213 * into revs->commits
3215 for (i = 0; i < array->nr; i++) {
3216 c = (struct commit *)(objects[i].item);
3217 if (!c)
3218 continue;
3219 if (!(c->object.flags & CHILD_SHOWN))
3220 continue;
3221 if (c->object.flags & (SHOWN | BOUNDARY))
3222 continue;
3223 c->object.flags |= BOUNDARY;
3224 commit_list_insert(c, &revs->commits);
3228 * If revs->topo_order is set, sort the boundary commits
3229 * in topological order
3231 sort_in_topological_order(&revs->commits, revs->sort_order);
3234 static struct commit *get_revision_internal(struct rev_info *revs)
3236 struct commit *c = NULL;
3237 struct commit_list *l;
3239 if (revs->boundary == 2) {
3241 * All of the normal commits have already been returned,
3242 * and we are now returning boundary commits.
3243 * create_boundary_commit_list() has populated
3244 * revs->commits with the remaining commits to return.
3246 c = pop_commit(&revs->commits);
3247 if (c)
3248 c->object.flags |= SHOWN;
3249 return c;
3253 * If our max_count counter has reached zero, then we are done. We
3254 * don't simply return NULL because we still might need to show
3255 * boundary commits. But we want to avoid calling get_revision_1, which
3256 * might do a considerable amount of work finding the next commit only
3257 * for us to throw it away.
3259 * If it is non-zero, then either we don't have a max_count at all
3260 * (-1), or it is still counting, in which case we decrement.
3262 if (revs->max_count) {
3263 c = get_revision_1(revs);
3264 if (c) {
3265 while (revs->skip_count > 0) {
3266 revs->skip_count--;
3267 c = get_revision_1(revs);
3268 if (!c)
3269 break;
3273 if (revs->max_count > 0)
3274 revs->max_count--;
3277 if (c)
3278 c->object.flags |= SHOWN;
3280 if (!revs->boundary)
3281 return c;
3283 if (!c) {
3285 * get_revision_1() runs out the commits, and
3286 * we are done computing the boundaries.
3287 * switch to boundary commits output mode.
3289 revs->boundary = 2;
3292 * Update revs->commits to contain the list of
3293 * boundary commits.
3295 create_boundary_commit_list(revs);
3297 return get_revision_internal(revs);
3301 * boundary commits are the commits that are parents of the
3302 * ones we got from get_revision_1() but they themselves are
3303 * not returned from get_revision_1(). Before returning
3304 * 'c', we need to mark its parents that they could be boundaries.
3307 for (l = c->parents; l; l = l->next) {
3308 struct object *p;
3309 p = &(l->item->object);
3310 if (p->flags & (CHILD_SHOWN | SHOWN))
3311 continue;
3312 p->flags |= CHILD_SHOWN;
3313 gc_boundary(&revs->boundary_commits);
3314 add_object_array(p, NULL, &revs->boundary_commits);
3317 return c;
3320 struct commit *get_revision(struct rev_info *revs)
3322 struct commit *c;
3323 struct commit_list *reversed;
3325 if (revs->reverse) {
3326 reversed = NULL;
3327 while ((c = get_revision_internal(revs)))
3328 commit_list_insert(c, &reversed);
3329 revs->commits = reversed;
3330 revs->reverse = 0;
3331 revs->reverse_output_stage = 1;
3334 if (revs->reverse_output_stage) {
3335 c = pop_commit(&revs->commits);
3336 if (revs->track_linear)
3337 revs->linear = !!(c && c->object.flags & TRACK_LINEAR);
3338 return c;
3341 c = get_revision_internal(revs);
3342 if (c && revs->graph)
3343 graph_update(revs->graph, c);
3344 if (!c) {
3345 free_saved_parents(revs);
3346 if (revs->previous_parents) {
3347 free_commit_list(revs->previous_parents);
3348 revs->previous_parents = NULL;
3351 return c;
3354 char *get_revision_mark(const struct rev_info *revs, const struct commit *commit)
3356 if (commit->object.flags & BOUNDARY)
3357 return "-";
3358 else if (commit->object.flags & UNINTERESTING)
3359 return "^";
3360 else if (commit->object.flags & PATCHSAME)
3361 return "=";
3362 else if (!revs || revs->left_right) {
3363 if (commit->object.flags & SYMMETRIC_LEFT)
3364 return "<";
3365 else
3366 return ">";
3367 } else if (revs->graph)
3368 return "*";
3369 else if (revs->cherry_mark)
3370 return "+";
3371 return "";
3374 void put_revision_mark(const struct rev_info *revs, const struct commit *commit)
3376 char *mark = get_revision_mark(revs, commit);
3377 if (!strlen(mark))
3378 return;
3379 fputs(mark, stdout);
3380 putchar(' ');