commit --amend: copy notes to the new commit
[git.git] / revision.c
blob1c514d120bc1d54e2bfd2ebb094d5ee06a95a6fd
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"
17 volatile show_early_output_fn_t show_early_output;
19 char *path_name(const struct name_path *path, const char *name)
21 const struct name_path *p;
22 char *n, *m;
23 int nlen = strlen(name);
24 int len = nlen + 1;
26 for (p = path; p; p = p->up) {
27 if (p->elem_len)
28 len += p->elem_len + 1;
30 n = xmalloc(len);
31 m = n + len - (nlen + 1);
32 strcpy(m, name);
33 for (p = path; p; p = p->up) {
34 if (p->elem_len) {
35 m -= p->elem_len + 1;
36 memcpy(m, p->elem, p->elem_len);
37 m[p->elem_len] = '/';
40 return n;
43 void add_object(struct object *obj,
44 struct object_array *p,
45 struct name_path *path,
46 const char *name)
48 add_object_array(obj, path_name(path, name), p);
51 static void mark_blob_uninteresting(struct blob *blob)
53 if (!blob)
54 return;
55 if (blob->object.flags & UNINTERESTING)
56 return;
57 blob->object.flags |= UNINTERESTING;
60 void mark_tree_uninteresting(struct tree *tree)
62 struct tree_desc desc;
63 struct name_entry entry;
64 struct object *obj = &tree->object;
66 if (!tree)
67 return;
68 if (obj->flags & UNINTERESTING)
69 return;
70 obj->flags |= UNINTERESTING;
71 if (!has_sha1_file(obj->sha1))
72 return;
73 if (parse_tree(tree) < 0)
74 die("bad tree %s", sha1_to_hex(obj->sha1));
76 init_tree_desc(&desc, tree->buffer, tree->size);
77 while (tree_entry(&desc, &entry)) {
78 switch (object_type(entry.mode)) {
79 case OBJ_TREE:
80 mark_tree_uninteresting(lookup_tree(entry.sha1));
81 break;
82 case OBJ_BLOB:
83 mark_blob_uninteresting(lookup_blob(entry.sha1));
84 break;
85 default:
86 /* Subproject commit - not in this repository */
87 break;
92 * We don't care about the tree any more
93 * after it has been marked uninteresting.
95 free(tree->buffer);
96 tree->buffer = NULL;
99 void mark_parents_uninteresting(struct commit *commit)
101 struct commit_list *parents = commit->parents;
103 while (parents) {
104 struct commit *commit = parents->item;
105 if (!(commit->object.flags & UNINTERESTING)) {
106 commit->object.flags |= UNINTERESTING;
109 * Normally we haven't parsed the parent
110 * yet, so we won't have a parent of a parent
111 * here. However, it may turn out that we've
112 * reached this commit some other way (where it
113 * wasn't uninteresting), in which case we need
114 * to mark its parents recursively too..
116 if (commit->parents)
117 mark_parents_uninteresting(commit);
121 * A missing commit is ok iff its parent is marked
122 * uninteresting.
124 * We just mark such a thing parsed, so that when
125 * it is popped next time around, we won't be trying
126 * to parse it and get an error.
128 if (!has_sha1_file(commit->object.sha1))
129 commit->object.parsed = 1;
130 parents = parents->next;
134 static void add_pending_object_with_mode(struct rev_info *revs, struct object *obj, const char *name, unsigned mode)
136 if (revs->no_walk && (obj->flags & UNINTERESTING))
137 revs->no_walk = 0;
138 if (revs->reflog_info && obj->type == OBJ_COMMIT &&
139 add_reflog_for_walk(revs->reflog_info,
140 (struct commit *)obj, name))
141 return;
142 add_object_array_with_mode(obj, name, &revs->pending, mode);
145 void add_pending_object(struct rev_info *revs, struct object *obj, const char *name)
147 add_pending_object_with_mode(revs, obj, name, S_IFINVALID);
150 void add_head_to_pending(struct rev_info *revs)
152 unsigned char sha1[20];
153 struct object *obj;
154 if (get_sha1("HEAD", sha1))
155 return;
156 obj = parse_object(sha1);
157 if (!obj)
158 return;
159 add_pending_object(revs, obj, "HEAD");
162 static struct object *get_reference(struct rev_info *revs, const char *name, const unsigned char *sha1, unsigned int flags)
164 struct object *object;
166 object = parse_object(sha1);
167 if (!object)
168 die("bad object %s", name);
169 object->flags |= flags;
170 return object;
173 static struct commit *handle_commit(struct rev_info *revs, struct object *object, const char *name)
175 unsigned long flags = object->flags;
178 * Tag object? Look what it points to..
180 while (object->type == OBJ_TAG) {
181 struct tag *tag = (struct tag *) object;
182 if (revs->tag_objects && !(flags & UNINTERESTING))
183 add_pending_object(revs, object, tag->tag);
184 if (!tag->tagged)
185 die("bad tag");
186 object = parse_object(tag->tagged->sha1);
187 if (!object) {
188 if (flags & UNINTERESTING)
189 return NULL;
190 die("bad object %s", sha1_to_hex(tag->tagged->sha1));
195 * Commit object? Just return it, we'll do all the complex
196 * reachability crud.
198 if (object->type == OBJ_COMMIT) {
199 struct commit *commit = (struct commit *)object;
200 if (parse_commit(commit) < 0)
201 die("unable to parse commit %s", name);
202 if (flags & UNINTERESTING) {
203 commit->object.flags |= UNINTERESTING;
204 mark_parents_uninteresting(commit);
205 revs->limited = 1;
207 if (revs->show_source && !commit->util)
208 commit->util = (void *) name;
209 return commit;
213 * Tree object? Either mark it uninteresting, or add it
214 * to the list of objects to look at later..
216 if (object->type == OBJ_TREE) {
217 struct tree *tree = (struct tree *)object;
218 if (!revs->tree_objects)
219 return NULL;
220 if (flags & UNINTERESTING) {
221 mark_tree_uninteresting(tree);
222 return NULL;
224 add_pending_object(revs, object, "");
225 return NULL;
229 * Blob object? You know the drill by now..
231 if (object->type == OBJ_BLOB) {
232 struct blob *blob = (struct blob *)object;
233 if (!revs->blob_objects)
234 return NULL;
235 if (flags & UNINTERESTING) {
236 mark_blob_uninteresting(blob);
237 return NULL;
239 add_pending_object(revs, object, "");
240 return NULL;
242 die("%s is unknown object", name);
245 static int everybody_uninteresting(struct commit_list *orig)
247 struct commit_list *list = orig;
248 while (list) {
249 struct commit *commit = list->item;
250 list = list->next;
251 if (commit->object.flags & UNINTERESTING)
252 continue;
253 return 0;
255 return 1;
259 * The goal is to get REV_TREE_NEW as the result only if the
260 * diff consists of all '+' (and no other changes), REV_TREE_OLD
261 * if the whole diff is removal of old data, and otherwise
262 * REV_TREE_DIFFERENT (of course if the trees are the same we
263 * want REV_TREE_SAME).
264 * That means that once we get to REV_TREE_DIFFERENT, we do not
265 * have to look any further.
267 static int tree_difference = REV_TREE_SAME;
269 static void file_add_remove(struct diff_options *options,
270 int addremove, unsigned mode,
271 const unsigned char *sha1,
272 const char *fullpath)
274 int diff = addremove == '+' ? REV_TREE_NEW : REV_TREE_OLD;
276 tree_difference |= diff;
277 if (tree_difference == REV_TREE_DIFFERENT)
278 DIFF_OPT_SET(options, HAS_CHANGES);
281 static void file_change(struct diff_options *options,
282 unsigned old_mode, unsigned new_mode,
283 const unsigned char *old_sha1,
284 const unsigned char *new_sha1,
285 const char *fullpath)
287 tree_difference = REV_TREE_DIFFERENT;
288 DIFF_OPT_SET(options, HAS_CHANGES);
291 static int rev_compare_tree(struct rev_info *revs, struct commit *parent, struct commit *commit)
293 struct tree *t1 = parent->tree;
294 struct tree *t2 = commit->tree;
296 if (!t1)
297 return REV_TREE_NEW;
298 if (!t2)
299 return REV_TREE_OLD;
301 if (revs->simplify_by_decoration) {
303 * If we are simplifying by decoration, then the commit
304 * is worth showing if it has a tag pointing at it.
306 if (lookup_decoration(&name_decoration, &commit->object))
307 return REV_TREE_DIFFERENT;
309 * A commit that is not pointed by a tag is uninteresting
310 * if we are not limited by path. This means that you will
311 * see the usual "commits that touch the paths" plus any
312 * tagged commit by specifying both --simplify-by-decoration
313 * and pathspec.
315 if (!revs->prune_data)
316 return REV_TREE_SAME;
319 tree_difference = REV_TREE_SAME;
320 DIFF_OPT_CLR(&revs->pruning, HAS_CHANGES);
321 if (diff_tree_sha1(t1->object.sha1, t2->object.sha1, "",
322 &revs->pruning) < 0)
323 return REV_TREE_DIFFERENT;
324 return tree_difference;
327 static int rev_same_tree_as_empty(struct rev_info *revs, struct commit *commit)
329 int retval;
330 void *tree;
331 unsigned long size;
332 struct tree_desc empty, real;
333 struct tree *t1 = commit->tree;
335 if (!t1)
336 return 0;
338 tree = read_object_with_reference(t1->object.sha1, tree_type, &size, NULL);
339 if (!tree)
340 return 0;
341 init_tree_desc(&real, tree, size);
342 init_tree_desc(&empty, "", 0);
344 tree_difference = REV_TREE_SAME;
345 DIFF_OPT_CLR(&revs->pruning, HAS_CHANGES);
346 retval = diff_tree(&empty, &real, "", &revs->pruning);
347 free(tree);
349 return retval >= 0 && (tree_difference == REV_TREE_SAME);
352 static void try_to_simplify_commit(struct rev_info *revs, struct commit *commit)
354 struct commit_list **pp, *parent;
355 int tree_changed = 0, tree_same = 0;
358 * If we don't do pruning, everything is interesting
360 if (!revs->prune)
361 return;
363 if (!commit->tree)
364 return;
366 if (!commit->parents) {
367 if (rev_same_tree_as_empty(revs, commit))
368 commit->object.flags |= TREESAME;
369 return;
373 * Normal non-merge commit? If we don't want to make the
374 * history dense, we consider it always to be a change..
376 if (!revs->dense && !commit->parents->next)
377 return;
379 pp = &commit->parents;
380 while ((parent = *pp) != NULL) {
381 struct commit *p = parent->item;
383 if (parse_commit(p) < 0)
384 die("cannot simplify commit %s (because of %s)",
385 sha1_to_hex(commit->object.sha1),
386 sha1_to_hex(p->object.sha1));
387 switch (rev_compare_tree(revs, p, commit)) {
388 case REV_TREE_SAME:
389 tree_same = 1;
390 if (!revs->simplify_history || (p->object.flags & UNINTERESTING)) {
391 /* Even if a merge with an uninteresting
392 * side branch brought the entire change
393 * we are interested in, we do not want
394 * to lose the other branches of this
395 * merge, so we just keep going.
397 pp = &parent->next;
398 continue;
400 parent->next = NULL;
401 commit->parents = parent;
402 commit->object.flags |= TREESAME;
403 return;
405 case REV_TREE_NEW:
406 if (revs->remove_empty_trees &&
407 rev_same_tree_as_empty(revs, p)) {
408 /* We are adding all the specified
409 * paths from this parent, so the
410 * history beyond this parent is not
411 * interesting. Remove its parents
412 * (they are grandparents for us).
413 * IOW, we pretend this parent is a
414 * "root" commit.
416 if (parse_commit(p) < 0)
417 die("cannot simplify commit %s (invalid %s)",
418 sha1_to_hex(commit->object.sha1),
419 sha1_to_hex(p->object.sha1));
420 p->parents = NULL;
422 /* fallthrough */
423 case REV_TREE_OLD:
424 case REV_TREE_DIFFERENT:
425 tree_changed = 1;
426 pp = &parent->next;
427 continue;
429 die("bad tree compare for commit %s", sha1_to_hex(commit->object.sha1));
431 if (tree_changed && !tree_same)
432 return;
433 commit->object.flags |= TREESAME;
436 static void insert_by_date_cached(struct commit *p, struct commit_list **head,
437 struct commit_list *cached_base, struct commit_list **cache)
439 struct commit_list *new_entry;
441 if (cached_base && p->date < cached_base->item->date)
442 new_entry = insert_by_date(p, &cached_base->next);
443 else
444 new_entry = insert_by_date(p, head);
446 if (cache && (!*cache || p->date < (*cache)->item->date))
447 *cache = new_entry;
450 static int add_parents_to_list(struct rev_info *revs, struct commit *commit,
451 struct commit_list **list, struct commit_list **cache_ptr)
453 struct commit_list *parent = commit->parents;
454 unsigned left_flag;
455 struct commit_list *cached_base = cache_ptr ? *cache_ptr : NULL;
457 if (commit->object.flags & ADDED)
458 return 0;
459 commit->object.flags |= ADDED;
462 * If the commit is uninteresting, don't try to
463 * prune parents - we want the maximal uninteresting
464 * set.
466 * Normally we haven't parsed the parent
467 * yet, so we won't have a parent of a parent
468 * here. However, it may turn out that we've
469 * reached this commit some other way (where it
470 * wasn't uninteresting), in which case we need
471 * to mark its parents recursively too..
473 if (commit->object.flags & UNINTERESTING) {
474 while (parent) {
475 struct commit *p = parent->item;
476 parent = parent->next;
477 if (p)
478 p->object.flags |= UNINTERESTING;
479 if (parse_commit(p) < 0)
480 continue;
481 if (p->parents)
482 mark_parents_uninteresting(p);
483 if (p->object.flags & SEEN)
484 continue;
485 p->object.flags |= SEEN;
486 insert_by_date_cached(p, list, cached_base, cache_ptr);
488 return 0;
492 * Ok, the commit wasn't uninteresting. Try to
493 * simplify the commit history and find the parent
494 * that has no differences in the path set if one exists.
496 try_to_simplify_commit(revs, commit);
498 if (revs->no_walk)
499 return 0;
501 left_flag = (commit->object.flags & SYMMETRIC_LEFT);
503 for (parent = commit->parents; parent; parent = parent->next) {
504 struct commit *p = parent->item;
506 if (parse_commit(p) < 0)
507 return -1;
508 if (revs->show_source && !p->util)
509 p->util = commit->util;
510 p->object.flags |= left_flag;
511 if (!(p->object.flags & SEEN)) {
512 p->object.flags |= SEEN;
513 insert_by_date_cached(p, list, cached_base, cache_ptr);
515 if (revs->first_parent_only)
516 break;
518 return 0;
521 static void cherry_pick_list(struct commit_list *list, struct rev_info *revs)
523 struct commit_list *p;
524 int left_count = 0, right_count = 0;
525 int left_first;
526 struct patch_ids ids;
528 /* First count the commits on the left and on the right */
529 for (p = list; p; p = p->next) {
530 struct commit *commit = p->item;
531 unsigned flags = commit->object.flags;
532 if (flags & BOUNDARY)
534 else if (flags & SYMMETRIC_LEFT)
535 left_count++;
536 else
537 right_count++;
540 left_first = left_count < right_count;
541 init_patch_ids(&ids);
542 if (revs->diffopt.nr_paths) {
543 ids.diffopts.nr_paths = revs->diffopt.nr_paths;
544 ids.diffopts.paths = revs->diffopt.paths;
545 ids.diffopts.pathlens = revs->diffopt.pathlens;
548 /* Compute patch-ids for one side */
549 for (p = list; p; p = p->next) {
550 struct commit *commit = p->item;
551 unsigned flags = commit->object.flags;
553 if (flags & BOUNDARY)
554 continue;
556 * If we have fewer left, left_first is set and we omit
557 * commits on the right branch in this loop. If we have
558 * fewer right, we skip the left ones.
560 if (left_first != !!(flags & SYMMETRIC_LEFT))
561 continue;
562 commit->util = add_commit_patch_id(commit, &ids);
565 /* Check the other side */
566 for (p = list; p; p = p->next) {
567 struct commit *commit = p->item;
568 struct patch_id *id;
569 unsigned flags = commit->object.flags;
571 if (flags & BOUNDARY)
572 continue;
574 * If we have fewer left, left_first is set and we omit
575 * commits on the left branch in this loop.
577 if (left_first == !!(flags & SYMMETRIC_LEFT))
578 continue;
581 * Have we seen the same patch id?
583 id = has_commit_patch_id(commit, &ids);
584 if (!id)
585 continue;
586 id->seen = 1;
587 commit->object.flags |= SHOWN;
590 /* Now check the original side for seen ones */
591 for (p = list; p; p = p->next) {
592 struct commit *commit = p->item;
593 struct patch_id *ent;
595 ent = commit->util;
596 if (!ent)
597 continue;
598 if (ent->seen)
599 commit->object.flags |= SHOWN;
600 commit->util = NULL;
603 free_patch_ids(&ids);
606 /* How many extra uninteresting commits we want to see.. */
607 #define SLOP 5
609 static int still_interesting(struct commit_list *src, unsigned long date, int slop)
612 * No source list at all? We're definitely done..
614 if (!src)
615 return 0;
618 * Does the destination list contain entries with a date
619 * before the source list? Definitely _not_ done.
621 if (date < src->item->date)
622 return SLOP;
625 * Does the source list still have interesting commits in
626 * it? Definitely not done..
628 if (!everybody_uninteresting(src))
629 return SLOP;
631 /* Ok, we're closing in.. */
632 return slop-1;
635 static int limit_list(struct rev_info *revs)
637 int slop = SLOP;
638 unsigned long date = ~0ul;
639 struct commit_list *list = revs->commits;
640 struct commit_list *newlist = NULL;
641 struct commit_list **p = &newlist;
643 while (list) {
644 struct commit_list *entry = list;
645 struct commit *commit = list->item;
646 struct object *obj = &commit->object;
647 show_early_output_fn_t show;
649 list = list->next;
650 free(entry);
652 if (revs->max_age != -1 && (commit->date < revs->max_age))
653 obj->flags |= UNINTERESTING;
654 if (add_parents_to_list(revs, commit, &list, NULL) < 0)
655 return -1;
656 if (obj->flags & UNINTERESTING) {
657 mark_parents_uninteresting(commit);
658 if (revs->show_all)
659 p = &commit_list_insert(commit, p)->next;
660 slop = still_interesting(list, date, slop);
661 if (slop)
662 continue;
663 /* If showing all, add the whole pending list to the end */
664 if (revs->show_all)
665 *p = list;
666 break;
668 if (revs->min_age != -1 && (commit->date > revs->min_age))
669 continue;
670 date = commit->date;
671 p = &commit_list_insert(commit, p)->next;
673 show = show_early_output;
674 if (!show)
675 continue;
677 show(revs, newlist);
678 show_early_output = NULL;
680 if (revs->cherry_pick)
681 cherry_pick_list(newlist, revs);
683 revs->commits = newlist;
684 return 0;
687 struct all_refs_cb {
688 int all_flags;
689 int warned_bad_reflog;
690 struct rev_info *all_revs;
691 const char *name_for_errormsg;
694 static int handle_one_ref(const char *path, const unsigned char *sha1, int flag, void *cb_data)
696 struct all_refs_cb *cb = cb_data;
697 struct object *object = get_reference(cb->all_revs, path, sha1,
698 cb->all_flags);
699 add_pending_object(cb->all_revs, object, path);
700 return 0;
703 static void init_all_refs_cb(struct all_refs_cb *cb, struct rev_info *revs,
704 unsigned flags)
706 cb->all_revs = revs;
707 cb->all_flags = flags;
710 static void handle_refs(struct rev_info *revs, unsigned flags,
711 int (*for_each)(each_ref_fn, void *))
713 struct all_refs_cb cb;
714 init_all_refs_cb(&cb, revs, flags);
715 for_each(handle_one_ref, &cb);
718 static void handle_one_reflog_commit(unsigned char *sha1, void *cb_data)
720 struct all_refs_cb *cb = cb_data;
721 if (!is_null_sha1(sha1)) {
722 struct object *o = parse_object(sha1);
723 if (o) {
724 o->flags |= cb->all_flags;
725 add_pending_object(cb->all_revs, o, "");
727 else if (!cb->warned_bad_reflog) {
728 warning("reflog of '%s' references pruned commits",
729 cb->name_for_errormsg);
730 cb->warned_bad_reflog = 1;
735 static int handle_one_reflog_ent(unsigned char *osha1, unsigned char *nsha1,
736 const char *email, unsigned long timestamp, int tz,
737 const char *message, void *cb_data)
739 handle_one_reflog_commit(osha1, cb_data);
740 handle_one_reflog_commit(nsha1, cb_data);
741 return 0;
744 static int handle_one_reflog(const char *path, const unsigned char *sha1, int flag, void *cb_data)
746 struct all_refs_cb *cb = cb_data;
747 cb->warned_bad_reflog = 0;
748 cb->name_for_errormsg = path;
749 for_each_reflog_ent(path, handle_one_reflog_ent, cb_data);
750 return 0;
753 static void handle_reflog(struct rev_info *revs, unsigned flags)
755 struct all_refs_cb cb;
756 cb.all_revs = revs;
757 cb.all_flags = flags;
758 for_each_reflog(handle_one_reflog, &cb);
761 static int add_parents_only(struct rev_info *revs, const char *arg, int flags)
763 unsigned char sha1[20];
764 struct object *it;
765 struct commit *commit;
766 struct commit_list *parents;
768 if (*arg == '^') {
769 flags ^= UNINTERESTING;
770 arg++;
772 if (get_sha1(arg, sha1))
773 return 0;
774 while (1) {
775 it = get_reference(revs, arg, sha1, 0);
776 if (it->type != OBJ_TAG)
777 break;
778 if (!((struct tag*)it)->tagged)
779 return 0;
780 hashcpy(sha1, ((struct tag*)it)->tagged->sha1);
782 if (it->type != OBJ_COMMIT)
783 return 0;
784 commit = (struct commit *)it;
785 for (parents = commit->parents; parents; parents = parents->next) {
786 it = &parents->item->object;
787 it->flags |= flags;
788 add_pending_object(revs, it, arg);
790 return 1;
793 void init_revisions(struct rev_info *revs, const char *prefix)
795 memset(revs, 0, sizeof(*revs));
797 revs->abbrev = DEFAULT_ABBREV;
798 revs->ignore_merges = 1;
799 revs->simplify_history = 1;
800 DIFF_OPT_SET(&revs->pruning, RECURSIVE);
801 DIFF_OPT_SET(&revs->pruning, QUICK);
802 revs->pruning.add_remove = file_add_remove;
803 revs->pruning.change = file_change;
804 revs->lifo = 1;
805 revs->dense = 1;
806 revs->prefix = prefix;
807 revs->max_age = -1;
808 revs->min_age = -1;
809 revs->skip_count = -1;
810 revs->max_count = -1;
812 revs->commit_format = CMIT_FMT_DEFAULT;
814 revs->grep_filter.status_only = 1;
815 revs->grep_filter.pattern_tail = &(revs->grep_filter.pattern_list);
816 revs->grep_filter.regflags = REG_NEWLINE;
818 diff_setup(&revs->diffopt);
819 if (prefix && !revs->diffopt.prefix) {
820 revs->diffopt.prefix = prefix;
821 revs->diffopt.prefix_length = strlen(prefix);
825 static void add_pending_commit_list(struct rev_info *revs,
826 struct commit_list *commit_list,
827 unsigned int flags)
829 while (commit_list) {
830 struct object *object = &commit_list->item->object;
831 object->flags |= flags;
832 add_pending_object(revs, object, sha1_to_hex(object->sha1));
833 commit_list = commit_list->next;
837 static void prepare_show_merge(struct rev_info *revs)
839 struct commit_list *bases;
840 struct commit *head, *other;
841 unsigned char sha1[20];
842 const char **prune = NULL;
843 int i, prune_num = 1; /* counting terminating NULL */
845 if (get_sha1("HEAD", sha1) || !(head = lookup_commit(sha1)))
846 die("--merge without HEAD?");
847 if (get_sha1("MERGE_HEAD", sha1) || !(other = lookup_commit(sha1)))
848 die("--merge without MERGE_HEAD?");
849 add_pending_object(revs, &head->object, "HEAD");
850 add_pending_object(revs, &other->object, "MERGE_HEAD");
851 bases = get_merge_bases(head, other, 1);
852 add_pending_commit_list(revs, bases, UNINTERESTING);
853 free_commit_list(bases);
854 head->object.flags |= SYMMETRIC_LEFT;
856 if (!active_nr)
857 read_cache();
858 for (i = 0; i < active_nr; i++) {
859 struct cache_entry *ce = active_cache[i];
860 if (!ce_stage(ce))
861 continue;
862 if (ce_path_match(ce, revs->prune_data)) {
863 prune_num++;
864 prune = xrealloc(prune, sizeof(*prune) * prune_num);
865 prune[prune_num-2] = ce->name;
866 prune[prune_num-1] = NULL;
868 while ((i+1 < active_nr) &&
869 ce_same_name(ce, active_cache[i+1]))
870 i++;
872 revs->prune_data = prune;
873 revs->limited = 1;
876 int handle_revision_arg(const char *arg, struct rev_info *revs,
877 int flags,
878 int cant_be_filename)
880 unsigned mode;
881 char *dotdot;
882 struct object *object;
883 unsigned char sha1[20];
884 int local_flags;
886 dotdot = strstr(arg, "..");
887 if (dotdot) {
888 unsigned char from_sha1[20];
889 const char *next = dotdot + 2;
890 const char *this = arg;
891 int symmetric = *next == '.';
892 unsigned int flags_exclude = flags ^ UNINTERESTING;
894 *dotdot = 0;
895 next += symmetric;
897 if (!*next)
898 next = "HEAD";
899 if (dotdot == arg)
900 this = "HEAD";
901 if (!get_sha1(this, from_sha1) &&
902 !get_sha1(next, sha1)) {
903 struct commit *a, *b;
904 struct commit_list *exclude;
906 a = lookup_commit_reference(from_sha1);
907 b = lookup_commit_reference(sha1);
908 if (!a || !b) {
909 die(symmetric ?
910 "Invalid symmetric difference expression %s...%s" :
911 "Invalid revision range %s..%s",
912 arg, next);
915 if (!cant_be_filename) {
916 *dotdot = '.';
917 verify_non_filename(revs->prefix, arg);
920 if (symmetric) {
921 exclude = get_merge_bases(a, b, 1);
922 add_pending_commit_list(revs, exclude,
923 flags_exclude);
924 free_commit_list(exclude);
925 a->object.flags |= flags | SYMMETRIC_LEFT;
926 } else
927 a->object.flags |= flags_exclude;
928 b->object.flags |= flags;
929 add_pending_object(revs, &a->object, this);
930 add_pending_object(revs, &b->object, next);
931 return 0;
933 *dotdot = '.';
935 dotdot = strstr(arg, "^@");
936 if (dotdot && !dotdot[2]) {
937 *dotdot = 0;
938 if (add_parents_only(revs, arg, flags))
939 return 0;
940 *dotdot = '^';
942 dotdot = strstr(arg, "^!");
943 if (dotdot && !dotdot[2]) {
944 *dotdot = 0;
945 if (!add_parents_only(revs, arg, flags ^ UNINTERESTING))
946 *dotdot = '^';
949 local_flags = 0;
950 if (*arg == '^') {
951 local_flags = UNINTERESTING;
952 arg++;
954 if (get_sha1_with_mode(arg, sha1, &mode))
955 return -1;
956 if (!cant_be_filename)
957 verify_non_filename(revs->prefix, arg);
958 object = get_reference(revs, arg, sha1, flags ^ local_flags);
959 add_pending_object_with_mode(revs, object, arg, mode);
960 return 0;
963 static void read_pathspec_from_stdin(struct rev_info *revs, struct strbuf *sb, const char ***prune_data)
965 const char **prune = *prune_data;
966 int prune_nr;
967 int prune_alloc;
969 /* count existing ones */
970 if (!prune)
971 prune_nr = 0;
972 else
973 for (prune_nr = 0; prune[prune_nr]; prune_nr++)
975 prune_alloc = prune_nr; /* not really, but we do not know */
977 while (strbuf_getwholeline(sb, stdin, '\n') != EOF) {
978 int len = sb->len;
979 if (len && sb->buf[len - 1] == '\n')
980 sb->buf[--len] = '\0';
981 ALLOC_GROW(prune, prune_nr+1, prune_alloc);
982 prune[prune_nr++] = xstrdup(sb->buf);
984 if (prune) {
985 ALLOC_GROW(prune, prune_nr+1, prune_alloc);
986 prune[prune_nr] = NULL;
988 *prune_data = prune;
991 static void read_revisions_from_stdin(struct rev_info *revs, const char ***prune)
993 struct strbuf sb;
994 int seen_dashdash = 0;
996 strbuf_init(&sb, 1000);
997 while (strbuf_getwholeline(&sb, stdin, '\n') != EOF) {
998 int len = sb.len;
999 if (len && sb.buf[len - 1] == '\n')
1000 sb.buf[--len] = '\0';
1001 if (!len)
1002 break;
1003 if (sb.buf[0] == '-') {
1004 if (len == 2 && sb.buf[1] == '-') {
1005 seen_dashdash = 1;
1006 break;
1008 die("options not supported in --stdin mode");
1010 if (handle_revision_arg(sb.buf, revs, 0, 1))
1011 die("bad revision '%s'", sb.buf);
1013 if (seen_dashdash)
1014 read_pathspec_from_stdin(revs, &sb, prune);
1015 strbuf_release(&sb);
1018 static void add_grep(struct rev_info *revs, const char *ptn, enum grep_pat_token what)
1020 append_grep_pattern(&revs->grep_filter, ptn, "command line", 0, what);
1023 static void add_header_grep(struct rev_info *revs, enum grep_header_field field, const char *pattern)
1025 append_header_grep_pattern(&revs->grep_filter, field, pattern);
1028 static void add_message_grep(struct rev_info *revs, const char *pattern)
1030 add_grep(revs, pattern, GREP_PATTERN_BODY);
1033 static int handle_revision_opt(struct rev_info *revs, int argc, const char **argv,
1034 int *unkc, const char **unkv)
1036 const char *arg = argv[0];
1038 /* pseudo revision arguments */
1039 if (!strcmp(arg, "--all") || !strcmp(arg, "--branches") ||
1040 !strcmp(arg, "--tags") || !strcmp(arg, "--remotes") ||
1041 !strcmp(arg, "--reflog") || !strcmp(arg, "--not") ||
1042 !strcmp(arg, "--no-walk") || !strcmp(arg, "--do-walk") ||
1043 !strcmp(arg, "--bisect"))
1045 unkv[(*unkc)++] = arg;
1046 return 1;
1049 if (!prefixcmp(arg, "--max-count=")) {
1050 revs->max_count = atoi(arg + 12);
1051 } else if (!prefixcmp(arg, "--skip=")) {
1052 revs->skip_count = atoi(arg + 7);
1053 } else if ((*arg == '-') && isdigit(arg[1])) {
1054 /* accept -<digit>, like traditional "head" */
1055 revs->max_count = atoi(arg + 1);
1056 } else if (!strcmp(arg, "-n")) {
1057 if (argc <= 1)
1058 return error("-n requires an argument");
1059 revs->max_count = atoi(argv[1]);
1060 return 2;
1061 } else if (!prefixcmp(arg, "-n")) {
1062 revs->max_count = atoi(arg + 2);
1063 } else if (!prefixcmp(arg, "--max-age=")) {
1064 revs->max_age = atoi(arg + 10);
1065 } else if (!prefixcmp(arg, "--since=")) {
1066 revs->max_age = approxidate(arg + 8);
1067 } else if (!prefixcmp(arg, "--after=")) {
1068 revs->max_age = approxidate(arg + 8);
1069 } else if (!prefixcmp(arg, "--min-age=")) {
1070 revs->min_age = atoi(arg + 10);
1071 } else if (!prefixcmp(arg, "--before=")) {
1072 revs->min_age = approxidate(arg + 9);
1073 } else if (!prefixcmp(arg, "--until=")) {
1074 revs->min_age = approxidate(arg + 8);
1075 } else if (!strcmp(arg, "--first-parent")) {
1076 revs->first_parent_only = 1;
1077 } else if (!strcmp(arg, "-g") || !strcmp(arg, "--walk-reflogs")) {
1078 init_reflog_walk(&revs->reflog_info);
1079 } else if (!strcmp(arg, "--default")) {
1080 if (argc <= 1)
1081 return error("bad --default argument");
1082 revs->def = argv[1];
1083 return 2;
1084 } else if (!strcmp(arg, "--merge")) {
1085 revs->show_merge = 1;
1086 } else if (!strcmp(arg, "--topo-order")) {
1087 revs->lifo = 1;
1088 revs->topo_order = 1;
1089 } else if (!strcmp(arg, "--simplify-merges")) {
1090 revs->simplify_merges = 1;
1091 revs->rewrite_parents = 1;
1092 revs->simplify_history = 0;
1093 revs->limited = 1;
1094 } else if (!strcmp(arg, "--simplify-by-decoration")) {
1095 revs->simplify_merges = 1;
1096 revs->rewrite_parents = 1;
1097 revs->simplify_history = 0;
1098 revs->simplify_by_decoration = 1;
1099 revs->limited = 1;
1100 revs->prune = 1;
1101 load_ref_decorations(DECORATE_SHORT_REFS);
1102 } else if (!strcmp(arg, "--date-order")) {
1103 revs->lifo = 0;
1104 revs->topo_order = 1;
1105 } else if (!prefixcmp(arg, "--early-output")) {
1106 int count = 100;
1107 switch (arg[14]) {
1108 case '=':
1109 count = atoi(arg+15);
1110 /* Fallthrough */
1111 case 0:
1112 revs->topo_order = 1;
1113 revs->early_output = count;
1115 } else if (!strcmp(arg, "--parents")) {
1116 revs->rewrite_parents = 1;
1117 revs->print_parents = 1;
1118 } else if (!strcmp(arg, "--dense")) {
1119 revs->dense = 1;
1120 } else if (!strcmp(arg, "--sparse")) {
1121 revs->dense = 0;
1122 } else if (!strcmp(arg, "--show-all")) {
1123 revs->show_all = 1;
1124 } else if (!strcmp(arg, "--remove-empty")) {
1125 revs->remove_empty_trees = 1;
1126 } else if (!strcmp(arg, "--merges")) {
1127 revs->merges_only = 1;
1128 } else if (!strcmp(arg, "--no-merges")) {
1129 revs->no_merges = 1;
1130 } else if (!strcmp(arg, "--boundary")) {
1131 revs->boundary = 1;
1132 } else if (!strcmp(arg, "--left-right")) {
1133 revs->left_right = 1;
1134 } else if (!strcmp(arg, "--cherry-pick")) {
1135 revs->cherry_pick = 1;
1136 revs->limited = 1;
1137 } else if (!strcmp(arg, "--objects")) {
1138 revs->tag_objects = 1;
1139 revs->tree_objects = 1;
1140 revs->blob_objects = 1;
1141 } else if (!strcmp(arg, "--objects-edge")) {
1142 revs->tag_objects = 1;
1143 revs->tree_objects = 1;
1144 revs->blob_objects = 1;
1145 revs->edge_hint = 1;
1146 } else if (!strcmp(arg, "--unpacked")) {
1147 revs->unpacked = 1;
1148 } else if (!prefixcmp(arg, "--unpacked=")) {
1149 die("--unpacked=<packfile> no longer supported.");
1150 } else if (!strcmp(arg, "-r")) {
1151 revs->diff = 1;
1152 DIFF_OPT_SET(&revs->diffopt, RECURSIVE);
1153 } else if (!strcmp(arg, "-t")) {
1154 revs->diff = 1;
1155 DIFF_OPT_SET(&revs->diffopt, RECURSIVE);
1156 DIFF_OPT_SET(&revs->diffopt, TREE_IN_RECURSIVE);
1157 } else if (!strcmp(arg, "-m")) {
1158 revs->ignore_merges = 0;
1159 } else if (!strcmp(arg, "-c")) {
1160 revs->diff = 1;
1161 revs->dense_combined_merges = 0;
1162 revs->combine_merges = 1;
1163 } else if (!strcmp(arg, "--cc")) {
1164 revs->diff = 1;
1165 revs->dense_combined_merges = 1;
1166 revs->combine_merges = 1;
1167 } else if (!strcmp(arg, "-v")) {
1168 revs->verbose_header = 1;
1169 } else if (!strcmp(arg, "--pretty")) {
1170 revs->verbose_header = 1;
1171 revs->pretty_given = 1;
1172 get_commit_format(arg+8, revs);
1173 } else if (!prefixcmp(arg, "--pretty=") || !prefixcmp(arg, "--format=")) {
1174 revs->verbose_header = 1;
1175 revs->pretty_given = 1;
1176 get_commit_format(arg+9, revs);
1177 } else if (!strcmp(arg, "--show-notes")) {
1178 revs->show_notes = 1;
1179 revs->show_notes_given = 1;
1180 } else if (!prefixcmp(arg, "--show-notes=")) {
1181 struct strbuf buf = STRBUF_INIT;
1182 revs->show_notes = 1;
1183 revs->show_notes_given = 1;
1184 if (!revs->notes_opt.extra_notes_refs)
1185 revs->notes_opt.extra_notes_refs = xcalloc(1, sizeof(struct string_list));
1186 if (!prefixcmp(arg+13, "refs/"))
1187 /* happy */;
1188 else if (!prefixcmp(arg+13, "notes/"))
1189 strbuf_addstr(&buf, "refs/");
1190 else
1191 strbuf_addstr(&buf, "refs/notes/");
1192 strbuf_addstr(&buf, arg+13);
1193 string_list_append(strbuf_detach(&buf, NULL),
1194 revs->notes_opt.extra_notes_refs);
1195 } else if (!strcmp(arg, "--no-notes")) {
1196 revs->show_notes = 0;
1197 revs->show_notes_given = 1;
1198 } else if (!strcmp(arg, "--standard-notes")) {
1199 revs->show_notes_given = 1;
1200 revs->notes_opt.suppress_default_notes = 0;
1201 } else if (!strcmp(arg, "--no-standard-notes")) {
1202 revs->notes_opt.suppress_default_notes = 1;
1203 } else if (!strcmp(arg, "--oneline")) {
1204 revs->verbose_header = 1;
1205 get_commit_format("oneline", revs);
1206 revs->pretty_given = 1;
1207 revs->abbrev_commit = 1;
1208 } else if (!strcmp(arg, "--graph")) {
1209 revs->topo_order = 1;
1210 revs->rewrite_parents = 1;
1211 revs->graph = graph_init(revs);
1212 } else if (!strcmp(arg, "--root")) {
1213 revs->show_root_diff = 1;
1214 } else if (!strcmp(arg, "--no-commit-id")) {
1215 revs->no_commit_id = 1;
1216 } else if (!strcmp(arg, "--always")) {
1217 revs->always_show_header = 1;
1218 } else if (!strcmp(arg, "--no-abbrev")) {
1219 revs->abbrev = 0;
1220 } else if (!strcmp(arg, "--abbrev")) {
1221 revs->abbrev = DEFAULT_ABBREV;
1222 } else if (!prefixcmp(arg, "--abbrev=")) {
1223 revs->abbrev = strtoul(arg + 9, NULL, 10);
1224 if (revs->abbrev < MINIMUM_ABBREV)
1225 revs->abbrev = MINIMUM_ABBREV;
1226 else if (revs->abbrev > 40)
1227 revs->abbrev = 40;
1228 } else if (!strcmp(arg, "--abbrev-commit")) {
1229 revs->abbrev_commit = 1;
1230 } else if (!strcmp(arg, "--full-diff")) {
1231 revs->diff = 1;
1232 revs->full_diff = 1;
1233 } else if (!strcmp(arg, "--full-history")) {
1234 revs->simplify_history = 0;
1235 } else if (!strcmp(arg, "--relative-date")) {
1236 revs->date_mode = DATE_RELATIVE;
1237 revs->date_mode_explicit = 1;
1238 } else if (!strncmp(arg, "--date=", 7)) {
1239 revs->date_mode = parse_date_format(arg + 7);
1240 revs->date_mode_explicit = 1;
1241 } else if (!strcmp(arg, "--log-size")) {
1242 revs->show_log_size = 1;
1245 * Grepping the commit log
1247 else if (!prefixcmp(arg, "--author=")) {
1248 add_header_grep(revs, GREP_HEADER_AUTHOR, arg+9);
1249 } else if (!prefixcmp(arg, "--committer=")) {
1250 add_header_grep(revs, GREP_HEADER_COMMITTER, arg+12);
1251 } else if (!prefixcmp(arg, "--grep=")) {
1252 add_message_grep(revs, arg+7);
1253 } else if (!strcmp(arg, "--extended-regexp") || !strcmp(arg, "-E")) {
1254 revs->grep_filter.regflags |= REG_EXTENDED;
1255 } else if (!strcmp(arg, "--regexp-ignore-case") || !strcmp(arg, "-i")) {
1256 revs->grep_filter.regflags |= REG_ICASE;
1257 } else if (!strcmp(arg, "--fixed-strings") || !strcmp(arg, "-F")) {
1258 revs->grep_filter.fixed = 1;
1259 } else if (!strcmp(arg, "--all-match")) {
1260 revs->grep_filter.all_match = 1;
1261 } else if (!prefixcmp(arg, "--encoding=")) {
1262 arg += 11;
1263 if (strcmp(arg, "none"))
1264 git_log_output_encoding = xstrdup(arg);
1265 else
1266 git_log_output_encoding = "";
1267 } else if (!strcmp(arg, "--reverse")) {
1268 revs->reverse ^= 1;
1269 } else if (!strcmp(arg, "--children")) {
1270 revs->children.name = "children";
1271 revs->limited = 1;
1272 } else {
1273 int opts = diff_opt_parse(&revs->diffopt, argv, argc);
1274 if (!opts)
1275 unkv[(*unkc)++] = arg;
1276 return opts;
1279 return 1;
1282 void parse_revision_opt(struct rev_info *revs, struct parse_opt_ctx_t *ctx,
1283 const struct option *options,
1284 const char * const usagestr[])
1286 int n = handle_revision_opt(revs, ctx->argc, ctx->argv,
1287 &ctx->cpidx, ctx->out);
1288 if (n <= 0) {
1289 error("unknown option `%s'", ctx->argv[0]);
1290 usage_with_options(usagestr, options);
1292 ctx->argv += n;
1293 ctx->argc -= n;
1296 static int for_each_bad_bisect_ref(each_ref_fn fn, void *cb_data)
1298 return for_each_ref_in("refs/bisect/bad", fn, cb_data);
1301 static int for_each_good_bisect_ref(each_ref_fn fn, void *cb_data)
1303 return for_each_ref_in("refs/bisect/good", fn, cb_data);
1306 static void append_prune_data(const char ***prune_data, const char **av)
1308 const char **prune = *prune_data;
1309 int prune_nr;
1310 int prune_alloc;
1312 if (!prune) {
1313 *prune_data = av;
1314 return;
1317 /* count existing ones */
1318 for (prune_nr = 0; prune[prune_nr]; prune_nr++)
1320 prune_alloc = prune_nr; /* not really, but we do not know */
1322 while (*av) {
1323 ALLOC_GROW(prune, prune_nr+1, prune_alloc);
1324 prune[prune_nr++] = *av;
1325 av++;
1327 if (prune) {
1328 ALLOC_GROW(prune, prune_nr+1, prune_alloc);
1329 prune[prune_nr] = NULL;
1331 *prune_data = prune;
1335 * Parse revision information, filling in the "rev_info" structure,
1336 * and removing the used arguments from the argument list.
1338 * Returns the number of arguments left that weren't recognized
1339 * (which are also moved to the head of the argument list)
1341 int setup_revisions(int argc, const char **argv, struct rev_info *revs, const char *def)
1343 int i, flags, left, seen_dashdash, read_from_stdin;
1344 const char **prune_data = NULL;
1346 /* First, search for "--" */
1347 seen_dashdash = 0;
1348 for (i = 1; i < argc; i++) {
1349 const char *arg = argv[i];
1350 if (strcmp(arg, "--"))
1351 continue;
1352 argv[i] = NULL;
1353 argc = i;
1354 if (argv[i + 1])
1355 prune_data = argv + i + 1;
1356 seen_dashdash = 1;
1357 break;
1360 /* Second, deal with arguments and options */
1361 flags = 0;
1362 read_from_stdin = 0;
1363 for (left = i = 1; i < argc; i++) {
1364 const char *arg = argv[i];
1365 if (*arg == '-') {
1366 int opts;
1368 if (!strcmp(arg, "--all")) {
1369 handle_refs(revs, flags, for_each_ref);
1370 handle_refs(revs, flags, head_ref);
1371 continue;
1373 if (!strcmp(arg, "--branches")) {
1374 handle_refs(revs, flags, for_each_branch_ref);
1375 continue;
1377 if (!strcmp(arg, "--bisect")) {
1378 handle_refs(revs, flags, for_each_bad_bisect_ref);
1379 handle_refs(revs, flags ^ UNINTERESTING, for_each_good_bisect_ref);
1380 revs->bisect = 1;
1381 continue;
1383 if (!strcmp(arg, "--tags")) {
1384 handle_refs(revs, flags, for_each_tag_ref);
1385 continue;
1387 if (!strcmp(arg, "--remotes")) {
1388 handle_refs(revs, flags, for_each_remote_ref);
1389 continue;
1391 if (!prefixcmp(arg, "--glob=")) {
1392 struct all_refs_cb cb;
1393 init_all_refs_cb(&cb, revs, flags);
1394 for_each_glob_ref(handle_one_ref, arg + 7, &cb);
1395 continue;
1397 if (!prefixcmp(arg, "--branches=")) {
1398 struct all_refs_cb cb;
1399 init_all_refs_cb(&cb, revs, flags);
1400 for_each_glob_ref_in(handle_one_ref, arg + 11, "refs/heads/", &cb);
1401 continue;
1403 if (!prefixcmp(arg, "--tags=")) {
1404 struct all_refs_cb cb;
1405 init_all_refs_cb(&cb, revs, flags);
1406 for_each_glob_ref_in(handle_one_ref, arg + 7, "refs/tags/", &cb);
1407 continue;
1409 if (!prefixcmp(arg, "--remotes=")) {
1410 struct all_refs_cb cb;
1411 init_all_refs_cb(&cb, revs, flags);
1412 for_each_glob_ref_in(handle_one_ref, arg + 10, "refs/remotes/", &cb);
1413 continue;
1415 if (!strcmp(arg, "--reflog")) {
1416 handle_reflog(revs, flags);
1417 continue;
1419 if (!strcmp(arg, "--not")) {
1420 flags ^= UNINTERESTING;
1421 continue;
1423 if (!strcmp(arg, "--no-walk")) {
1424 revs->no_walk = 1;
1425 continue;
1427 if (!strcmp(arg, "--do-walk")) {
1428 revs->no_walk = 0;
1429 continue;
1431 if (!strcmp(arg, "--stdin")) {
1432 if (revs->disable_stdin) {
1433 argv[left++] = arg;
1434 continue;
1436 if (read_from_stdin++)
1437 die("--stdin given twice?");
1438 read_revisions_from_stdin(revs, &prune_data);
1439 continue;
1442 opts = handle_revision_opt(revs, argc - i, argv + i, &left, argv);
1443 if (opts > 0) {
1444 i += opts - 1;
1445 continue;
1447 if (opts < 0)
1448 exit(128);
1449 continue;
1452 if (handle_revision_arg(arg, revs, flags, seen_dashdash)) {
1453 int j;
1454 if (seen_dashdash || *arg == '^')
1455 die("bad revision '%s'", arg);
1457 /* If we didn't have a "--":
1458 * (1) all filenames must exist;
1459 * (2) all rev-args must not be interpretable
1460 * as a valid filename.
1461 * but the latter we have checked in the main loop.
1463 for (j = i; j < argc; j++)
1464 verify_filename(revs->prefix, argv[j]);
1466 append_prune_data(&prune_data, argv + i);
1467 break;
1471 if (prune_data)
1472 revs->prune_data = get_pathspec(revs->prefix, prune_data);
1474 if (revs->def == NULL)
1475 revs->def = def;
1476 if (revs->show_merge)
1477 prepare_show_merge(revs);
1478 if (revs->def && !revs->pending.nr) {
1479 unsigned char sha1[20];
1480 struct object *object;
1481 unsigned mode;
1482 if (get_sha1_with_mode(revs->def, sha1, &mode))
1483 die("bad default revision '%s'", revs->def);
1484 object = get_reference(revs, revs->def, sha1, 0);
1485 add_pending_object_with_mode(revs, object, revs->def, mode);
1488 /* Did the user ask for any diff output? Run the diff! */
1489 if (revs->diffopt.output_format & ~DIFF_FORMAT_NO_OUTPUT)
1490 revs->diff = 1;
1492 /* Pickaxe, diff-filter and rename following need diffs */
1493 if (revs->diffopt.pickaxe ||
1494 revs->diffopt.filter ||
1495 DIFF_OPT_TST(&revs->diffopt, FOLLOW_RENAMES))
1496 revs->diff = 1;
1498 if (revs->topo_order)
1499 revs->limited = 1;
1501 if (revs->prune_data) {
1502 diff_tree_setup_paths(revs->prune_data, &revs->pruning);
1503 /* Can't prune commits with rename following: the paths change.. */
1504 if (!DIFF_OPT_TST(&revs->diffopt, FOLLOW_RENAMES))
1505 revs->prune = 1;
1506 if (!revs->full_diff)
1507 diff_tree_setup_paths(revs->prune_data, &revs->diffopt);
1509 if (revs->combine_merges) {
1510 revs->ignore_merges = 0;
1511 if (revs->dense_combined_merges && !revs->diffopt.output_format)
1512 revs->diffopt.output_format = DIFF_FORMAT_PATCH;
1514 revs->diffopt.abbrev = revs->abbrev;
1515 if (diff_setup_done(&revs->diffopt) < 0)
1516 die("diff_setup_done failed");
1518 compile_grep_patterns(&revs->grep_filter);
1520 if (revs->reverse && revs->reflog_info)
1521 die("cannot combine --reverse with --walk-reflogs");
1522 if (revs->rewrite_parents && revs->children.name)
1523 die("cannot combine --parents and --children");
1526 * Limitations on the graph functionality
1528 if (revs->reverse && revs->graph)
1529 die("cannot combine --reverse with --graph");
1531 if (revs->reflog_info && revs->graph)
1532 die("cannot combine --walk-reflogs with --graph");
1534 return left;
1537 static void add_child(struct rev_info *revs, struct commit *parent, struct commit *child)
1539 struct commit_list *l = xcalloc(1, sizeof(*l));
1541 l->item = child;
1542 l->next = add_decoration(&revs->children, &parent->object, l);
1545 static int remove_duplicate_parents(struct commit *commit)
1547 struct commit_list **pp, *p;
1548 int surviving_parents;
1550 /* Examine existing parents while marking ones we have seen... */
1551 pp = &commit->parents;
1552 while ((p = *pp) != NULL) {
1553 struct commit *parent = p->item;
1554 if (parent->object.flags & TMP_MARK) {
1555 *pp = p->next;
1556 continue;
1558 parent->object.flags |= TMP_MARK;
1559 pp = &p->next;
1561 /* count them while clearing the temporary mark */
1562 surviving_parents = 0;
1563 for (p = commit->parents; p; p = p->next) {
1564 p->item->object.flags &= ~TMP_MARK;
1565 surviving_parents++;
1567 return surviving_parents;
1570 struct merge_simplify_state {
1571 struct commit *simplified;
1574 static struct merge_simplify_state *locate_simplify_state(struct rev_info *revs, struct commit *commit)
1576 struct merge_simplify_state *st;
1578 st = lookup_decoration(&revs->merge_simplification, &commit->object);
1579 if (!st) {
1580 st = xcalloc(1, sizeof(*st));
1581 add_decoration(&revs->merge_simplification, &commit->object, st);
1583 return st;
1586 static struct commit_list **simplify_one(struct rev_info *revs, struct commit *commit, struct commit_list **tail)
1588 struct commit_list *p;
1589 struct merge_simplify_state *st, *pst;
1590 int cnt;
1592 st = locate_simplify_state(revs, commit);
1595 * Have we handled this one?
1597 if (st->simplified)
1598 return tail;
1601 * An UNINTERESTING commit simplifies to itself, so does a
1602 * root commit. We do not rewrite parents of such commit
1603 * anyway.
1605 if ((commit->object.flags & UNINTERESTING) || !commit->parents) {
1606 st->simplified = commit;
1607 return tail;
1611 * Do we know what commit all of our parents should be rewritten to?
1612 * Otherwise we are not ready to rewrite this one yet.
1614 for (cnt = 0, p = commit->parents; p; p = p->next) {
1615 pst = locate_simplify_state(revs, p->item);
1616 if (!pst->simplified) {
1617 tail = &commit_list_insert(p->item, tail)->next;
1618 cnt++;
1621 if (cnt) {
1622 tail = &commit_list_insert(commit, tail)->next;
1623 return tail;
1627 * Rewrite our list of parents.
1629 for (p = commit->parents; p; p = p->next) {
1630 pst = locate_simplify_state(revs, p->item);
1631 p->item = pst->simplified;
1633 cnt = remove_duplicate_parents(commit);
1636 * It is possible that we are a merge and one side branch
1637 * does not have any commit that touches the given paths;
1638 * in such a case, the immediate parents will be rewritten
1639 * to different commits.
1641 * o----X X: the commit we are looking at;
1642 * / / o: a commit that touches the paths;
1643 * ---o----'
1645 * Further reduce the parents by removing redundant parents.
1647 if (1 < cnt) {
1648 struct commit_list *h = reduce_heads(commit->parents);
1649 cnt = commit_list_count(h);
1650 free_commit_list(commit->parents);
1651 commit->parents = h;
1655 * A commit simplifies to itself if it is a root, if it is
1656 * UNINTERESTING, if it touches the given paths, or if it is a
1657 * merge and its parents simplifies to more than one commits
1658 * (the first two cases are already handled at the beginning of
1659 * this function).
1661 * Otherwise, it simplifies to what its sole parent simplifies to.
1663 if (!cnt ||
1664 (commit->object.flags & UNINTERESTING) ||
1665 !(commit->object.flags & TREESAME) ||
1666 (1 < cnt))
1667 st->simplified = commit;
1668 else {
1669 pst = locate_simplify_state(revs, commit->parents->item);
1670 st->simplified = pst->simplified;
1672 return tail;
1675 static void simplify_merges(struct rev_info *revs)
1677 struct commit_list *list;
1678 struct commit_list *yet_to_do, **tail;
1680 if (!revs->topo_order)
1681 sort_in_topological_order(&revs->commits, revs->lifo);
1682 if (!revs->prune)
1683 return;
1685 /* feed the list reversed */
1686 yet_to_do = NULL;
1687 for (list = revs->commits; list; list = list->next)
1688 commit_list_insert(list->item, &yet_to_do);
1689 while (yet_to_do) {
1690 list = yet_to_do;
1691 yet_to_do = NULL;
1692 tail = &yet_to_do;
1693 while (list) {
1694 struct commit *commit = list->item;
1695 struct commit_list *next = list->next;
1696 free(list);
1697 list = next;
1698 tail = simplify_one(revs, commit, tail);
1702 /* clean up the result, removing the simplified ones */
1703 list = revs->commits;
1704 revs->commits = NULL;
1705 tail = &revs->commits;
1706 while (list) {
1707 struct commit *commit = list->item;
1708 struct commit_list *next = list->next;
1709 struct merge_simplify_state *st;
1710 free(list);
1711 list = next;
1712 st = locate_simplify_state(revs, commit);
1713 if (st->simplified == commit)
1714 tail = &commit_list_insert(commit, tail)->next;
1718 static void set_children(struct rev_info *revs)
1720 struct commit_list *l;
1721 for (l = revs->commits; l; l = l->next) {
1722 struct commit *commit = l->item;
1723 struct commit_list *p;
1725 for (p = commit->parents; p; p = p->next)
1726 add_child(revs, p->item, commit);
1730 int prepare_revision_walk(struct rev_info *revs)
1732 int nr = revs->pending.nr;
1733 struct object_array_entry *e, *list;
1735 e = list = revs->pending.objects;
1736 revs->pending.nr = 0;
1737 revs->pending.alloc = 0;
1738 revs->pending.objects = NULL;
1739 while (--nr >= 0) {
1740 struct commit *commit = handle_commit(revs, e->item, e->name);
1741 if (commit) {
1742 if (!(commit->object.flags & SEEN)) {
1743 commit->object.flags |= SEEN;
1744 insert_by_date(commit, &revs->commits);
1747 e++;
1749 free(list);
1751 if (revs->no_walk)
1752 return 0;
1753 if (revs->limited)
1754 if (limit_list(revs) < 0)
1755 return -1;
1756 if (revs->topo_order)
1757 sort_in_topological_order(&revs->commits, revs->lifo);
1758 if (revs->simplify_merges)
1759 simplify_merges(revs);
1760 if (revs->children.name)
1761 set_children(revs);
1762 return 0;
1765 enum rewrite_result {
1766 rewrite_one_ok,
1767 rewrite_one_noparents,
1768 rewrite_one_error,
1771 static enum rewrite_result rewrite_one(struct rev_info *revs, struct commit **pp)
1773 struct commit_list *cache = NULL;
1775 for (;;) {
1776 struct commit *p = *pp;
1777 if (!revs->limited)
1778 if (add_parents_to_list(revs, p, &revs->commits, &cache) < 0)
1779 return rewrite_one_error;
1780 if (p->parents && p->parents->next)
1781 return rewrite_one_ok;
1782 if (p->object.flags & UNINTERESTING)
1783 return rewrite_one_ok;
1784 if (!(p->object.flags & TREESAME))
1785 return rewrite_one_ok;
1786 if (!p->parents)
1787 return rewrite_one_noparents;
1788 *pp = p->parents->item;
1792 static int rewrite_parents(struct rev_info *revs, struct commit *commit)
1794 struct commit_list **pp = &commit->parents;
1795 while (*pp) {
1796 struct commit_list *parent = *pp;
1797 switch (rewrite_one(revs, &parent->item)) {
1798 case rewrite_one_ok:
1799 break;
1800 case rewrite_one_noparents:
1801 *pp = parent->next;
1802 continue;
1803 case rewrite_one_error:
1804 return -1;
1806 pp = &parent->next;
1808 remove_duplicate_parents(commit);
1809 return 0;
1812 static int commit_match(struct commit *commit, struct rev_info *opt)
1814 if (!opt->grep_filter.pattern_list)
1815 return 1;
1816 return grep_buffer(&opt->grep_filter,
1817 NULL, /* we say nothing, not even filename */
1818 commit->buffer, strlen(commit->buffer));
1821 static inline int want_ancestry(struct rev_info *revs)
1823 return (revs->rewrite_parents || revs->children.name);
1826 enum commit_action get_commit_action(struct rev_info *revs, struct commit *commit)
1828 if (commit->object.flags & SHOWN)
1829 return commit_ignore;
1830 if (revs->unpacked && has_sha1_pack(commit->object.sha1))
1831 return commit_ignore;
1832 if (revs->show_all)
1833 return commit_show;
1834 if (commit->object.flags & UNINTERESTING)
1835 return commit_ignore;
1836 if (revs->min_age != -1 && (commit->date > revs->min_age))
1837 return commit_ignore;
1838 if (revs->no_merges && commit->parents && commit->parents->next)
1839 return commit_ignore;
1840 if (revs->merges_only && !(commit->parents && commit->parents->next))
1841 return commit_ignore;
1842 if (!commit_match(commit, revs))
1843 return commit_ignore;
1844 if (revs->prune && revs->dense) {
1845 /* Commit without changes? */
1846 if (commit->object.flags & TREESAME) {
1847 /* drop merges unless we want parenthood */
1848 if (!want_ancestry(revs))
1849 return commit_ignore;
1850 /* non-merge - always ignore it */
1851 if (!commit->parents || !commit->parents->next)
1852 return commit_ignore;
1855 return commit_show;
1858 enum commit_action simplify_commit(struct rev_info *revs, struct commit *commit)
1860 enum commit_action action = get_commit_action(revs, commit);
1862 if (action == commit_show &&
1863 !revs->show_all &&
1864 revs->prune && revs->dense && want_ancestry(revs)) {
1865 if (rewrite_parents(revs, commit) < 0)
1866 return commit_error;
1868 return action;
1871 static struct commit *get_revision_1(struct rev_info *revs)
1873 if (!revs->commits)
1874 return NULL;
1876 do {
1877 struct commit_list *entry = revs->commits;
1878 struct commit *commit = entry->item;
1880 revs->commits = entry->next;
1881 free(entry);
1883 if (revs->reflog_info)
1884 fake_reflog_parent(revs->reflog_info, commit);
1887 * If we haven't done the list limiting, we need to look at
1888 * the parents here. We also need to do the date-based limiting
1889 * that we'd otherwise have done in limit_list().
1891 if (!revs->limited) {
1892 if (revs->max_age != -1 &&
1893 (commit->date < revs->max_age))
1894 continue;
1895 if (add_parents_to_list(revs, commit, &revs->commits, NULL) < 0)
1896 die("Failed to traverse parents of commit %s",
1897 sha1_to_hex(commit->object.sha1));
1900 switch (simplify_commit(revs, commit)) {
1901 case commit_ignore:
1902 continue;
1903 case commit_error:
1904 die("Failed to simplify parents of commit %s",
1905 sha1_to_hex(commit->object.sha1));
1906 default:
1907 return commit;
1909 } while (revs->commits);
1910 return NULL;
1913 static void gc_boundary(struct object_array *array)
1915 unsigned nr = array->nr;
1916 unsigned alloc = array->alloc;
1917 struct object_array_entry *objects = array->objects;
1919 if (alloc <= nr) {
1920 unsigned i, j;
1921 for (i = j = 0; i < nr; i++) {
1922 if (objects[i].item->flags & SHOWN)
1923 continue;
1924 if (i != j)
1925 objects[j] = objects[i];
1926 j++;
1928 for (i = j; i < nr; i++)
1929 objects[i].item = NULL;
1930 array->nr = j;
1934 static void create_boundary_commit_list(struct rev_info *revs)
1936 unsigned i;
1937 struct commit *c;
1938 struct object_array *array = &revs->boundary_commits;
1939 struct object_array_entry *objects = array->objects;
1942 * If revs->commits is non-NULL at this point, an error occurred in
1943 * get_revision_1(). Ignore the error and continue printing the
1944 * boundary commits anyway. (This is what the code has always
1945 * done.)
1947 if (revs->commits) {
1948 free_commit_list(revs->commits);
1949 revs->commits = NULL;
1953 * Put all of the actual boundary commits from revs->boundary_commits
1954 * into revs->commits
1956 for (i = 0; i < array->nr; i++) {
1957 c = (struct commit *)(objects[i].item);
1958 if (!c)
1959 continue;
1960 if (!(c->object.flags & CHILD_SHOWN))
1961 continue;
1962 if (c->object.flags & (SHOWN | BOUNDARY))
1963 continue;
1964 c->object.flags |= BOUNDARY;
1965 commit_list_insert(c, &revs->commits);
1969 * If revs->topo_order is set, sort the boundary commits
1970 * in topological order
1972 sort_in_topological_order(&revs->commits, revs->lifo);
1975 static struct commit *get_revision_internal(struct rev_info *revs)
1977 struct commit *c = NULL;
1978 struct commit_list *l;
1980 if (revs->boundary == 2) {
1982 * All of the normal commits have already been returned,
1983 * and we are now returning boundary commits.
1984 * create_boundary_commit_list() has populated
1985 * revs->commits with the remaining commits to return.
1987 c = pop_commit(&revs->commits);
1988 if (c)
1989 c->object.flags |= SHOWN;
1990 return c;
1994 * Now pick up what they want to give us
1996 c = get_revision_1(revs);
1997 if (c) {
1998 while (0 < revs->skip_count) {
1999 revs->skip_count--;
2000 c = get_revision_1(revs);
2001 if (!c)
2002 break;
2007 * Check the max_count.
2009 switch (revs->max_count) {
2010 case -1:
2011 break;
2012 case 0:
2013 c = NULL;
2014 break;
2015 default:
2016 revs->max_count--;
2019 if (c)
2020 c->object.flags |= SHOWN;
2022 if (!revs->boundary) {
2023 return c;
2026 if (!c) {
2028 * get_revision_1() runs out the commits, and
2029 * we are done computing the boundaries.
2030 * switch to boundary commits output mode.
2032 revs->boundary = 2;
2035 * Update revs->commits to contain the list of
2036 * boundary commits.
2038 create_boundary_commit_list(revs);
2040 return get_revision_internal(revs);
2044 * boundary commits are the commits that are parents of the
2045 * ones we got from get_revision_1() but they themselves are
2046 * not returned from get_revision_1(). Before returning
2047 * 'c', we need to mark its parents that they could be boundaries.
2050 for (l = c->parents; l; l = l->next) {
2051 struct object *p;
2052 p = &(l->item->object);
2053 if (p->flags & (CHILD_SHOWN | SHOWN))
2054 continue;
2055 p->flags |= CHILD_SHOWN;
2056 gc_boundary(&revs->boundary_commits);
2057 add_object_array(p, NULL, &revs->boundary_commits);
2060 return c;
2063 struct commit *get_revision(struct rev_info *revs)
2065 struct commit *c;
2066 struct commit_list *reversed;
2068 if (revs->reverse) {
2069 reversed = NULL;
2070 while ((c = get_revision_internal(revs))) {
2071 commit_list_insert(c, &reversed);
2073 revs->commits = reversed;
2074 revs->reverse = 0;
2075 revs->reverse_output_stage = 1;
2078 if (revs->reverse_output_stage)
2079 return pop_commit(&revs->commits);
2081 c = get_revision_internal(revs);
2082 if (c && revs->graph)
2083 graph_update(revs->graph, c);
2084 return c;