Merge branch 'ml/color-when'
[git.git] / revision.c
blob438cc87b17fa3a5a7b57c3fcdf4289cd869b79a7
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"
16 volatile show_early_output_fn_t show_early_output;
18 char *path_name(const struct name_path *path, const char *name)
20 const struct name_path *p;
21 char *n, *m;
22 int nlen = strlen(name);
23 int len = nlen + 1;
25 for (p = path; p; p = p->up) {
26 if (p->elem_len)
27 len += p->elem_len + 1;
29 n = xmalloc(len);
30 m = n + len - (nlen + 1);
31 strcpy(m, name);
32 for (p = path; p; p = p->up) {
33 if (p->elem_len) {
34 m -= p->elem_len + 1;
35 memcpy(m, p->elem, p->elem_len);
36 m[p->elem_len] = '/';
39 return n;
42 void add_object(struct object *obj,
43 struct object_array *p,
44 struct name_path *path,
45 const char *name)
47 add_object_array(obj, path_name(path, name), p);
50 static void mark_blob_uninteresting(struct blob *blob)
52 if (!blob)
53 return;
54 if (blob->object.flags & UNINTERESTING)
55 return;
56 blob->object.flags |= UNINTERESTING;
59 void mark_tree_uninteresting(struct tree *tree)
61 struct tree_desc desc;
62 struct name_entry entry;
63 struct object *obj = &tree->object;
65 if (!tree)
66 return;
67 if (obj->flags & UNINTERESTING)
68 return;
69 obj->flags |= UNINTERESTING;
70 if (!has_sha1_file(obj->sha1))
71 return;
72 if (parse_tree(tree) < 0)
73 die("bad tree %s", sha1_to_hex(obj->sha1));
75 init_tree_desc(&desc, tree->buffer, tree->size);
76 while (tree_entry(&desc, &entry)) {
77 switch (object_type(entry.mode)) {
78 case OBJ_TREE:
79 mark_tree_uninteresting(lookup_tree(entry.sha1));
80 break;
81 case OBJ_BLOB:
82 mark_blob_uninteresting(lookup_blob(entry.sha1));
83 break;
84 default:
85 /* Subproject commit - not in this repository */
86 break;
91 * We don't care about the tree any more
92 * after it has been marked uninteresting.
94 free(tree->buffer);
95 tree->buffer = NULL;
98 void mark_parents_uninteresting(struct commit *commit)
100 struct commit_list *parents = commit->parents;
102 while (parents) {
103 struct commit *commit = parents->item;
104 if (!(commit->object.flags & UNINTERESTING)) {
105 commit->object.flags |= UNINTERESTING;
108 * Normally we haven't parsed the parent
109 * yet, so we won't have a parent of a parent
110 * here. However, it may turn out that we've
111 * reached this commit some other way (where it
112 * wasn't uninteresting), in which case we need
113 * to mark its parents recursively too..
115 if (commit->parents)
116 mark_parents_uninteresting(commit);
120 * A missing commit is ok iff its parent is marked
121 * uninteresting.
123 * We just mark such a thing parsed, so that when
124 * it is popped next time around, we won't be trying
125 * to parse it and get an error.
127 if (!has_sha1_file(commit->object.sha1))
128 commit->object.parsed = 1;
129 parents = parents->next;
133 static void add_pending_object_with_mode(struct rev_info *revs, struct object *obj, const char *name, unsigned mode)
135 if (revs->no_walk && (obj->flags & UNINTERESTING))
136 revs->no_walk = 0;
137 if (revs->reflog_info && obj->type == OBJ_COMMIT) {
138 struct strbuf buf = STRBUF_INIT;
139 int len = interpret_branch_name(name, &buf);
140 int st;
142 if (0 < len && name[len] && buf.len)
143 strbuf_addstr(&buf, name + len);
144 st = add_reflog_for_walk(revs->reflog_info,
145 (struct commit *)obj,
146 buf.buf[0] ? buf.buf: name);
147 strbuf_release(&buf);
148 if (st)
149 return;
151 add_object_array_with_mode(obj, name, &revs->pending, mode);
154 void add_pending_object(struct rev_info *revs, struct object *obj, const char *name)
156 add_pending_object_with_mode(revs, obj, name, S_IFINVALID);
159 void add_head_to_pending(struct rev_info *revs)
161 unsigned char sha1[20];
162 struct object *obj;
163 if (get_sha1("HEAD", sha1))
164 return;
165 obj = parse_object(sha1);
166 if (!obj)
167 return;
168 add_pending_object(revs, obj, "HEAD");
171 static struct object *get_reference(struct rev_info *revs, const char *name, const unsigned char *sha1, unsigned int flags)
173 struct object *object;
175 object = parse_object(sha1);
176 if (!object)
177 die("bad object %s", name);
178 object->flags |= flags;
179 return object;
182 static struct commit *handle_commit(struct rev_info *revs, struct object *object, const char *name)
184 unsigned long flags = object->flags;
187 * Tag object? Look what it points to..
189 while (object->type == OBJ_TAG) {
190 struct tag *tag = (struct tag *) object;
191 if (revs->tag_objects && !(flags & UNINTERESTING))
192 add_pending_object(revs, object, tag->tag);
193 if (!tag->tagged)
194 die("bad tag");
195 object = parse_object(tag->tagged->sha1);
196 if (!object) {
197 if (flags & UNINTERESTING)
198 return NULL;
199 die("bad object %s", sha1_to_hex(tag->tagged->sha1));
204 * Commit object? Just return it, we'll do all the complex
205 * reachability crud.
207 if (object->type == OBJ_COMMIT) {
208 struct commit *commit = (struct commit *)object;
209 if (parse_commit(commit) < 0)
210 die("unable to parse commit %s", name);
211 if (flags & UNINTERESTING) {
212 commit->object.flags |= UNINTERESTING;
213 mark_parents_uninteresting(commit);
214 revs->limited = 1;
216 if (revs->show_source && !commit->util)
217 commit->util = (void *) name;
218 return commit;
222 * Tree object? Either mark it uninteresting, or add it
223 * to the list of objects to look at later..
225 if (object->type == OBJ_TREE) {
226 struct tree *tree = (struct tree *)object;
227 if (!revs->tree_objects)
228 return NULL;
229 if (flags & UNINTERESTING) {
230 mark_tree_uninteresting(tree);
231 return NULL;
233 add_pending_object(revs, object, "");
234 return NULL;
238 * Blob object? You know the drill by now..
240 if (object->type == OBJ_BLOB) {
241 struct blob *blob = (struct blob *)object;
242 if (!revs->blob_objects)
243 return NULL;
244 if (flags & UNINTERESTING) {
245 mark_blob_uninteresting(blob);
246 return NULL;
248 add_pending_object(revs, object, "");
249 return NULL;
251 die("%s is unknown object", name);
254 static int everybody_uninteresting(struct commit_list *orig)
256 struct commit_list *list = orig;
257 while (list) {
258 struct commit *commit = list->item;
259 list = list->next;
260 if (commit->object.flags & UNINTERESTING)
261 continue;
262 return 0;
264 return 1;
268 * The goal is to get REV_TREE_NEW as the result only if the
269 * diff consists of all '+' (and no other changes), REV_TREE_OLD
270 * if the whole diff is removal of old data, and otherwise
271 * REV_TREE_DIFFERENT (of course if the trees are the same we
272 * want REV_TREE_SAME).
273 * That means that once we get to REV_TREE_DIFFERENT, we do not
274 * have to look any further.
276 static int tree_difference = REV_TREE_SAME;
278 static void file_add_remove(struct diff_options *options,
279 int addremove, unsigned mode,
280 const unsigned char *sha1,
281 const char *fullpath, unsigned dirty_submodule)
283 int diff = addremove == '+' ? REV_TREE_NEW : REV_TREE_OLD;
285 tree_difference |= diff;
286 if (tree_difference == REV_TREE_DIFFERENT)
287 DIFF_OPT_SET(options, HAS_CHANGES);
290 static void file_change(struct diff_options *options,
291 unsigned old_mode, unsigned new_mode,
292 const unsigned char *old_sha1,
293 const unsigned char *new_sha1,
294 const char *fullpath,
295 unsigned old_dirty_submodule, unsigned new_dirty_submodule)
297 tree_difference = REV_TREE_DIFFERENT;
298 DIFF_OPT_SET(options, HAS_CHANGES);
301 static int rev_compare_tree(struct rev_info *revs, struct commit *parent, struct commit *commit)
303 struct tree *t1 = parent->tree;
304 struct tree *t2 = commit->tree;
306 if (!t1)
307 return REV_TREE_NEW;
308 if (!t2)
309 return REV_TREE_OLD;
311 if (revs->simplify_by_decoration) {
313 * If we are simplifying by decoration, then the commit
314 * is worth showing if it has a tag pointing at it.
316 if (lookup_decoration(&name_decoration, &commit->object))
317 return REV_TREE_DIFFERENT;
319 * A commit that is not pointed by a tag is uninteresting
320 * if we are not limited by path. This means that you will
321 * see the usual "commits that touch the paths" plus any
322 * tagged commit by specifying both --simplify-by-decoration
323 * and pathspec.
325 if (!revs->prune_data)
326 return REV_TREE_SAME;
329 tree_difference = REV_TREE_SAME;
330 DIFF_OPT_CLR(&revs->pruning, HAS_CHANGES);
331 if (diff_tree_sha1(t1->object.sha1, t2->object.sha1, "",
332 &revs->pruning) < 0)
333 return REV_TREE_DIFFERENT;
334 return tree_difference;
337 static int rev_same_tree_as_empty(struct rev_info *revs, struct commit *commit)
339 int retval;
340 void *tree;
341 unsigned long size;
342 struct tree_desc empty, real;
343 struct tree *t1 = commit->tree;
345 if (!t1)
346 return 0;
348 tree = read_object_with_reference(t1->object.sha1, tree_type, &size, NULL);
349 if (!tree)
350 return 0;
351 init_tree_desc(&real, tree, size);
352 init_tree_desc(&empty, "", 0);
354 tree_difference = REV_TREE_SAME;
355 DIFF_OPT_CLR(&revs->pruning, HAS_CHANGES);
356 retval = diff_tree(&empty, &real, "", &revs->pruning);
357 free(tree);
359 return retval >= 0 && (tree_difference == REV_TREE_SAME);
362 static void try_to_simplify_commit(struct rev_info *revs, struct commit *commit)
364 struct commit_list **pp, *parent;
365 int tree_changed = 0, tree_same = 0;
368 * If we don't do pruning, everything is interesting
370 if (!revs->prune)
371 return;
373 if (!commit->tree)
374 return;
376 if (!commit->parents) {
377 if (rev_same_tree_as_empty(revs, commit))
378 commit->object.flags |= TREESAME;
379 return;
383 * Normal non-merge commit? If we don't want to make the
384 * history dense, we consider it always to be a change..
386 if (!revs->dense && !commit->parents->next)
387 return;
389 pp = &commit->parents;
390 while ((parent = *pp) != NULL) {
391 struct commit *p = parent->item;
393 if (parse_commit(p) < 0)
394 die("cannot simplify commit %s (because of %s)",
395 sha1_to_hex(commit->object.sha1),
396 sha1_to_hex(p->object.sha1));
397 switch (rev_compare_tree(revs, p, commit)) {
398 case REV_TREE_SAME:
399 tree_same = 1;
400 if (!revs->simplify_history || (p->object.flags & UNINTERESTING)) {
401 /* Even if a merge with an uninteresting
402 * side branch brought the entire change
403 * we are interested in, we do not want
404 * to lose the other branches of this
405 * merge, so we just keep going.
407 pp = &parent->next;
408 continue;
410 parent->next = NULL;
411 commit->parents = parent;
412 commit->object.flags |= TREESAME;
413 return;
415 case REV_TREE_NEW:
416 if (revs->remove_empty_trees &&
417 rev_same_tree_as_empty(revs, p)) {
418 /* We are adding all the specified
419 * paths from this parent, so the
420 * history beyond this parent is not
421 * interesting. Remove its parents
422 * (they are grandparents for us).
423 * IOW, we pretend this parent is a
424 * "root" commit.
426 if (parse_commit(p) < 0)
427 die("cannot simplify commit %s (invalid %s)",
428 sha1_to_hex(commit->object.sha1),
429 sha1_to_hex(p->object.sha1));
430 p->parents = NULL;
432 /* fallthrough */
433 case REV_TREE_OLD:
434 case REV_TREE_DIFFERENT:
435 tree_changed = 1;
436 pp = &parent->next;
437 continue;
439 die("bad tree compare for commit %s", sha1_to_hex(commit->object.sha1));
441 if (tree_changed && !tree_same)
442 return;
443 commit->object.flags |= TREESAME;
446 static void insert_by_date_cached(struct commit *p, struct commit_list **head,
447 struct commit_list *cached_base, struct commit_list **cache)
449 struct commit_list *new_entry;
451 if (cached_base && p->date < cached_base->item->date)
452 new_entry = insert_by_date(p, &cached_base->next);
453 else
454 new_entry = insert_by_date(p, head);
456 if (cache && (!*cache || p->date < (*cache)->item->date))
457 *cache = new_entry;
460 static int add_parents_to_list(struct rev_info *revs, struct commit *commit,
461 struct commit_list **list, struct commit_list **cache_ptr)
463 struct commit_list *parent = commit->parents;
464 unsigned left_flag;
465 struct commit_list *cached_base = cache_ptr ? *cache_ptr : NULL;
467 if (commit->object.flags & ADDED)
468 return 0;
469 commit->object.flags |= ADDED;
472 * If the commit is uninteresting, don't try to
473 * prune parents - we want the maximal uninteresting
474 * set.
476 * Normally we haven't parsed the parent
477 * yet, so we won't have a parent of a parent
478 * here. However, it may turn out that we've
479 * reached this commit some other way (where it
480 * wasn't uninteresting), in which case we need
481 * to mark its parents recursively too..
483 if (commit->object.flags & UNINTERESTING) {
484 while (parent) {
485 struct commit *p = parent->item;
486 parent = parent->next;
487 if (p)
488 p->object.flags |= UNINTERESTING;
489 if (parse_commit(p) < 0)
490 continue;
491 if (p->parents)
492 mark_parents_uninteresting(p);
493 if (p->object.flags & SEEN)
494 continue;
495 p->object.flags |= SEEN;
496 insert_by_date_cached(p, list, cached_base, cache_ptr);
498 return 0;
502 * Ok, the commit wasn't uninteresting. Try to
503 * simplify the commit history and find the parent
504 * that has no differences in the path set if one exists.
506 try_to_simplify_commit(revs, commit);
508 if (revs->no_walk)
509 return 0;
511 left_flag = (commit->object.flags & SYMMETRIC_LEFT);
513 for (parent = commit->parents; parent; parent = parent->next) {
514 struct commit *p = parent->item;
516 if (parse_commit(p) < 0)
517 return -1;
518 if (revs->show_source && !p->util)
519 p->util = commit->util;
520 p->object.flags |= left_flag;
521 if (!(p->object.flags & SEEN)) {
522 p->object.flags |= SEEN;
523 insert_by_date_cached(p, list, cached_base, cache_ptr);
525 if (revs->first_parent_only)
526 break;
528 return 0;
531 static void cherry_pick_list(struct commit_list *list, struct rev_info *revs)
533 struct commit_list *p;
534 int left_count = 0, right_count = 0;
535 int left_first;
536 struct patch_ids ids;
538 /* First count the commits on the left and on the right */
539 for (p = list; p; p = p->next) {
540 struct commit *commit = p->item;
541 unsigned flags = commit->object.flags;
542 if (flags & BOUNDARY)
544 else if (flags & SYMMETRIC_LEFT)
545 left_count++;
546 else
547 right_count++;
550 left_first = left_count < right_count;
551 init_patch_ids(&ids);
552 if (revs->diffopt.nr_paths) {
553 ids.diffopts.nr_paths = revs->diffopt.nr_paths;
554 ids.diffopts.paths = revs->diffopt.paths;
555 ids.diffopts.pathlens = revs->diffopt.pathlens;
558 /* Compute patch-ids for one side */
559 for (p = list; p; p = p->next) {
560 struct commit *commit = p->item;
561 unsigned flags = commit->object.flags;
563 if (flags & BOUNDARY)
564 continue;
566 * If we have fewer left, left_first is set and we omit
567 * commits on the right branch in this loop. If we have
568 * fewer right, we skip the left ones.
570 if (left_first != !!(flags & SYMMETRIC_LEFT))
571 continue;
572 commit->util = add_commit_patch_id(commit, &ids);
575 /* Check the other side */
576 for (p = list; p; p = p->next) {
577 struct commit *commit = p->item;
578 struct patch_id *id;
579 unsigned flags = commit->object.flags;
581 if (flags & BOUNDARY)
582 continue;
584 * If we have fewer left, left_first is set and we omit
585 * commits on the left branch in this loop.
587 if (left_first == !!(flags & SYMMETRIC_LEFT))
588 continue;
591 * Have we seen the same patch id?
593 id = has_commit_patch_id(commit, &ids);
594 if (!id)
595 continue;
596 id->seen = 1;
597 commit->object.flags |= SHOWN;
600 /* Now check the original side for seen ones */
601 for (p = list; p; p = p->next) {
602 struct commit *commit = p->item;
603 struct patch_id *ent;
605 ent = commit->util;
606 if (!ent)
607 continue;
608 if (ent->seen)
609 commit->object.flags |= SHOWN;
610 commit->util = NULL;
613 free_patch_ids(&ids);
616 /* How many extra uninteresting commits we want to see.. */
617 #define SLOP 5
619 static int still_interesting(struct commit_list *src, unsigned long date, int slop)
622 * No source list at all? We're definitely done..
624 if (!src)
625 return 0;
628 * Does the destination list contain entries with a date
629 * before the source list? Definitely _not_ done.
631 if (date < src->item->date)
632 return SLOP;
635 * Does the source list still have interesting commits in
636 * it? Definitely not done..
638 if (!everybody_uninteresting(src))
639 return SLOP;
641 /* Ok, we're closing in.. */
642 return slop-1;
645 static int limit_list(struct rev_info *revs)
647 int slop = SLOP;
648 unsigned long date = ~0ul;
649 struct commit_list *list = revs->commits;
650 struct commit_list *newlist = NULL;
651 struct commit_list **p = &newlist;
653 while (list) {
654 struct commit_list *entry = list;
655 struct commit *commit = list->item;
656 struct object *obj = &commit->object;
657 show_early_output_fn_t show;
659 list = list->next;
660 free(entry);
662 if (revs->max_age != -1 && (commit->date < revs->max_age))
663 obj->flags |= UNINTERESTING;
664 if (add_parents_to_list(revs, commit, &list, NULL) < 0)
665 return -1;
666 if (obj->flags & UNINTERESTING) {
667 mark_parents_uninteresting(commit);
668 if (revs->show_all)
669 p = &commit_list_insert(commit, p)->next;
670 slop = still_interesting(list, date, slop);
671 if (slop)
672 continue;
673 /* If showing all, add the whole pending list to the end */
674 if (revs->show_all)
675 *p = list;
676 break;
678 if (revs->min_age != -1 && (commit->date > revs->min_age))
679 continue;
680 date = commit->date;
681 p = &commit_list_insert(commit, p)->next;
683 show = show_early_output;
684 if (!show)
685 continue;
687 show(revs, newlist);
688 show_early_output = NULL;
690 if (revs->cherry_pick)
691 cherry_pick_list(newlist, revs);
693 revs->commits = newlist;
694 return 0;
697 struct all_refs_cb {
698 int all_flags;
699 int warned_bad_reflog;
700 struct rev_info *all_revs;
701 const char *name_for_errormsg;
704 static int handle_one_ref(const char *path, const unsigned char *sha1, int flag, void *cb_data)
706 struct all_refs_cb *cb = cb_data;
707 struct object *object = get_reference(cb->all_revs, path, sha1,
708 cb->all_flags);
709 add_pending_object(cb->all_revs, object, path);
710 return 0;
713 static void init_all_refs_cb(struct all_refs_cb *cb, struct rev_info *revs,
714 unsigned flags)
716 cb->all_revs = revs;
717 cb->all_flags = flags;
720 static void handle_refs(struct rev_info *revs, unsigned flags,
721 int (*for_each)(each_ref_fn, void *))
723 struct all_refs_cb cb;
724 init_all_refs_cb(&cb, revs, flags);
725 for_each(handle_one_ref, &cb);
728 static void handle_one_reflog_commit(unsigned char *sha1, void *cb_data)
730 struct all_refs_cb *cb = cb_data;
731 if (!is_null_sha1(sha1)) {
732 struct object *o = parse_object(sha1);
733 if (o) {
734 o->flags |= cb->all_flags;
735 add_pending_object(cb->all_revs, o, "");
737 else if (!cb->warned_bad_reflog) {
738 warning("reflog of '%s' references pruned commits",
739 cb->name_for_errormsg);
740 cb->warned_bad_reflog = 1;
745 static int handle_one_reflog_ent(unsigned char *osha1, unsigned char *nsha1,
746 const char *email, unsigned long timestamp, int tz,
747 const char *message, void *cb_data)
749 handle_one_reflog_commit(osha1, cb_data);
750 handle_one_reflog_commit(nsha1, cb_data);
751 return 0;
754 static int handle_one_reflog(const char *path, const unsigned char *sha1, int flag, void *cb_data)
756 struct all_refs_cb *cb = cb_data;
757 cb->warned_bad_reflog = 0;
758 cb->name_for_errormsg = path;
759 for_each_reflog_ent(path, handle_one_reflog_ent, cb_data);
760 return 0;
763 static void handle_reflog(struct rev_info *revs, unsigned flags)
765 struct all_refs_cb cb;
766 cb.all_revs = revs;
767 cb.all_flags = flags;
768 for_each_reflog(handle_one_reflog, &cb);
771 static int add_parents_only(struct rev_info *revs, const char *arg, int flags)
773 unsigned char sha1[20];
774 struct object *it;
775 struct commit *commit;
776 struct commit_list *parents;
778 if (*arg == '^') {
779 flags ^= UNINTERESTING;
780 arg++;
782 if (get_sha1(arg, sha1))
783 return 0;
784 while (1) {
785 it = get_reference(revs, arg, sha1, 0);
786 if (it->type != OBJ_TAG)
787 break;
788 if (!((struct tag*)it)->tagged)
789 return 0;
790 hashcpy(sha1, ((struct tag*)it)->tagged->sha1);
792 if (it->type != OBJ_COMMIT)
793 return 0;
794 commit = (struct commit *)it;
795 for (parents = commit->parents; parents; parents = parents->next) {
796 it = &parents->item->object;
797 it->flags |= flags;
798 add_pending_object(revs, it, arg);
800 return 1;
803 void init_revisions(struct rev_info *revs, const char *prefix)
805 memset(revs, 0, sizeof(*revs));
807 revs->abbrev = DEFAULT_ABBREV;
808 revs->ignore_merges = 1;
809 revs->simplify_history = 1;
810 DIFF_OPT_SET(&revs->pruning, RECURSIVE);
811 DIFF_OPT_SET(&revs->pruning, QUICK);
812 revs->pruning.add_remove = file_add_remove;
813 revs->pruning.change = file_change;
814 revs->lifo = 1;
815 revs->dense = 1;
816 revs->prefix = prefix;
817 revs->max_age = -1;
818 revs->min_age = -1;
819 revs->skip_count = -1;
820 revs->max_count = -1;
822 revs->commit_format = CMIT_FMT_DEFAULT;
824 revs->grep_filter.status_only = 1;
825 revs->grep_filter.pattern_tail = &(revs->grep_filter.pattern_list);
826 revs->grep_filter.header_tail = &(revs->grep_filter.header_list);
827 revs->grep_filter.regflags = REG_NEWLINE;
829 diff_setup(&revs->diffopt);
830 if (prefix && !revs->diffopt.prefix) {
831 revs->diffopt.prefix = prefix;
832 revs->diffopt.prefix_length = strlen(prefix);
836 static void add_pending_commit_list(struct rev_info *revs,
837 struct commit_list *commit_list,
838 unsigned int flags)
840 while (commit_list) {
841 struct object *object = &commit_list->item->object;
842 object->flags |= flags;
843 add_pending_object(revs, object, sha1_to_hex(object->sha1));
844 commit_list = commit_list->next;
848 static void prepare_show_merge(struct rev_info *revs)
850 struct commit_list *bases;
851 struct commit *head, *other;
852 unsigned char sha1[20];
853 const char **prune = NULL;
854 int i, prune_num = 1; /* counting terminating NULL */
856 if (get_sha1("HEAD", sha1) || !(head = lookup_commit(sha1)))
857 die("--merge without HEAD?");
858 if (get_sha1("MERGE_HEAD", sha1) || !(other = lookup_commit(sha1)))
859 die("--merge without MERGE_HEAD?");
860 add_pending_object(revs, &head->object, "HEAD");
861 add_pending_object(revs, &other->object, "MERGE_HEAD");
862 bases = get_merge_bases(head, other, 1);
863 add_pending_commit_list(revs, bases, UNINTERESTING);
864 free_commit_list(bases);
865 head->object.flags |= SYMMETRIC_LEFT;
867 if (!active_nr)
868 read_cache();
869 for (i = 0; i < active_nr; i++) {
870 struct cache_entry *ce = active_cache[i];
871 if (!ce_stage(ce))
872 continue;
873 if (ce_path_match(ce, revs->prune_data)) {
874 prune_num++;
875 prune = xrealloc(prune, sizeof(*prune) * prune_num);
876 prune[prune_num-2] = ce->name;
877 prune[prune_num-1] = NULL;
879 while ((i+1 < active_nr) &&
880 ce_same_name(ce, active_cache[i+1]))
881 i++;
883 revs->prune_data = prune;
884 revs->limited = 1;
887 int handle_revision_arg(const char *arg, struct rev_info *revs,
888 int flags,
889 int cant_be_filename)
891 unsigned mode;
892 char *dotdot;
893 struct object *object;
894 unsigned char sha1[20];
895 int local_flags;
897 dotdot = strstr(arg, "..");
898 if (dotdot) {
899 unsigned char from_sha1[20];
900 const char *next = dotdot + 2;
901 const char *this = arg;
902 int symmetric = *next == '.';
903 unsigned int flags_exclude = flags ^ UNINTERESTING;
905 *dotdot = 0;
906 next += symmetric;
908 if (!*next)
909 next = "HEAD";
910 if (dotdot == arg)
911 this = "HEAD";
912 if (!get_sha1(this, from_sha1) &&
913 !get_sha1(next, sha1)) {
914 struct commit *a, *b;
915 struct commit_list *exclude;
917 a = lookup_commit_reference(from_sha1);
918 b = lookup_commit_reference(sha1);
919 if (!a || !b) {
920 die(symmetric ?
921 "Invalid symmetric difference expression %s...%s" :
922 "Invalid revision range %s..%s",
923 arg, next);
926 if (!cant_be_filename) {
927 *dotdot = '.';
928 verify_non_filename(revs->prefix, arg);
931 if (symmetric) {
932 exclude = get_merge_bases(a, b, 1);
933 add_pending_commit_list(revs, exclude,
934 flags_exclude);
935 free_commit_list(exclude);
936 a->object.flags |= flags | SYMMETRIC_LEFT;
937 } else
938 a->object.flags |= flags_exclude;
939 b->object.flags |= flags;
940 add_pending_object(revs, &a->object, this);
941 add_pending_object(revs, &b->object, next);
942 return 0;
944 *dotdot = '.';
946 dotdot = strstr(arg, "^@");
947 if (dotdot && !dotdot[2]) {
948 *dotdot = 0;
949 if (add_parents_only(revs, arg, flags))
950 return 0;
951 *dotdot = '^';
953 dotdot = strstr(arg, "^!");
954 if (dotdot && !dotdot[2]) {
955 *dotdot = 0;
956 if (!add_parents_only(revs, arg, flags ^ UNINTERESTING))
957 *dotdot = '^';
960 local_flags = 0;
961 if (*arg == '^') {
962 local_flags = UNINTERESTING;
963 arg++;
965 if (get_sha1_with_mode(arg, sha1, &mode))
966 return -1;
967 if (!cant_be_filename)
968 verify_non_filename(revs->prefix, arg);
969 object = get_reference(revs, arg, sha1, flags ^ local_flags);
970 add_pending_object_with_mode(revs, object, arg, mode);
971 return 0;
974 static void read_pathspec_from_stdin(struct rev_info *revs, struct strbuf *sb, const char ***prune_data)
976 const char **prune = *prune_data;
977 int prune_nr;
978 int prune_alloc;
980 /* count existing ones */
981 if (!prune)
982 prune_nr = 0;
983 else
984 for (prune_nr = 0; prune[prune_nr]; prune_nr++)
986 prune_alloc = prune_nr; /* not really, but we do not know */
988 while (strbuf_getwholeline(sb, stdin, '\n') != EOF) {
989 int len = sb->len;
990 if (len && sb->buf[len - 1] == '\n')
991 sb->buf[--len] = '\0';
992 ALLOC_GROW(prune, prune_nr+1, prune_alloc);
993 prune[prune_nr++] = xstrdup(sb->buf);
995 if (prune) {
996 ALLOC_GROW(prune, prune_nr+1, prune_alloc);
997 prune[prune_nr] = NULL;
999 *prune_data = prune;
1002 static void read_revisions_from_stdin(struct rev_info *revs, const char ***prune)
1004 struct strbuf sb;
1005 int seen_dashdash = 0;
1007 strbuf_init(&sb, 1000);
1008 while (strbuf_getwholeline(&sb, stdin, '\n') != EOF) {
1009 int len = sb.len;
1010 if (len && sb.buf[len - 1] == '\n')
1011 sb.buf[--len] = '\0';
1012 if (!len)
1013 break;
1014 if (sb.buf[0] == '-') {
1015 if (len == 2 && sb.buf[1] == '-') {
1016 seen_dashdash = 1;
1017 break;
1019 die("options not supported in --stdin mode");
1021 if (handle_revision_arg(sb.buf, revs, 0, 1))
1022 die("bad revision '%s'", sb.buf);
1024 if (seen_dashdash)
1025 read_pathspec_from_stdin(revs, &sb, prune);
1026 strbuf_release(&sb);
1029 static void add_grep(struct rev_info *revs, const char *ptn, enum grep_pat_token what)
1031 append_grep_pattern(&revs->grep_filter, ptn, "command line", 0, what);
1034 static void add_header_grep(struct rev_info *revs, enum grep_header_field field, const char *pattern)
1036 append_header_grep_pattern(&revs->grep_filter, field, pattern);
1039 static void add_message_grep(struct rev_info *revs, const char *pattern)
1041 add_grep(revs, pattern, GREP_PATTERN_BODY);
1044 static int handle_revision_opt(struct rev_info *revs, int argc, const char **argv,
1045 int *unkc, const char **unkv)
1047 const char *arg = argv[0];
1049 /* pseudo revision arguments */
1050 if (!strcmp(arg, "--all") || !strcmp(arg, "--branches") ||
1051 !strcmp(arg, "--tags") || !strcmp(arg, "--remotes") ||
1052 !strcmp(arg, "--reflog") || !strcmp(arg, "--not") ||
1053 !strcmp(arg, "--no-walk") || !strcmp(arg, "--do-walk") ||
1054 !strcmp(arg, "--bisect"))
1056 unkv[(*unkc)++] = arg;
1057 return 1;
1060 if (!prefixcmp(arg, "--max-count=")) {
1061 revs->max_count = atoi(arg + 12);
1062 } else if (!prefixcmp(arg, "--skip=")) {
1063 revs->skip_count = atoi(arg + 7);
1064 } else if ((*arg == '-') && isdigit(arg[1])) {
1065 /* accept -<digit>, like traditional "head" */
1066 revs->max_count = atoi(arg + 1);
1067 } else if (!strcmp(arg, "-n")) {
1068 if (argc <= 1)
1069 return error("-n requires an argument");
1070 revs->max_count = atoi(argv[1]);
1071 return 2;
1072 } else if (!prefixcmp(arg, "-n")) {
1073 revs->max_count = atoi(arg + 2);
1074 } else if (!prefixcmp(arg, "--max-age=")) {
1075 revs->max_age = atoi(arg + 10);
1076 } else if (!prefixcmp(arg, "--since=")) {
1077 revs->max_age = approxidate(arg + 8);
1078 } else if (!prefixcmp(arg, "--after=")) {
1079 revs->max_age = approxidate(arg + 8);
1080 } else if (!prefixcmp(arg, "--min-age=")) {
1081 revs->min_age = atoi(arg + 10);
1082 } else if (!prefixcmp(arg, "--before=")) {
1083 revs->min_age = approxidate(arg + 9);
1084 } else if (!prefixcmp(arg, "--until=")) {
1085 revs->min_age = approxidate(arg + 8);
1086 } else if (!strcmp(arg, "--first-parent")) {
1087 revs->first_parent_only = 1;
1088 } else if (!strcmp(arg, "-g") || !strcmp(arg, "--walk-reflogs")) {
1089 init_reflog_walk(&revs->reflog_info);
1090 } else if (!strcmp(arg, "--default")) {
1091 if (argc <= 1)
1092 return error("bad --default argument");
1093 revs->def = argv[1];
1094 return 2;
1095 } else if (!strcmp(arg, "--merge")) {
1096 revs->show_merge = 1;
1097 } else if (!strcmp(arg, "--topo-order")) {
1098 revs->lifo = 1;
1099 revs->topo_order = 1;
1100 } else if (!strcmp(arg, "--simplify-merges")) {
1101 revs->simplify_merges = 1;
1102 revs->rewrite_parents = 1;
1103 revs->simplify_history = 0;
1104 revs->limited = 1;
1105 } else if (!strcmp(arg, "--simplify-by-decoration")) {
1106 revs->simplify_merges = 1;
1107 revs->rewrite_parents = 1;
1108 revs->simplify_history = 0;
1109 revs->simplify_by_decoration = 1;
1110 revs->limited = 1;
1111 revs->prune = 1;
1112 load_ref_decorations(DECORATE_SHORT_REFS);
1113 } else if (!strcmp(arg, "--date-order")) {
1114 revs->lifo = 0;
1115 revs->topo_order = 1;
1116 } else if (!prefixcmp(arg, "--early-output")) {
1117 int count = 100;
1118 switch (arg[14]) {
1119 case '=':
1120 count = atoi(arg+15);
1121 /* Fallthrough */
1122 case 0:
1123 revs->topo_order = 1;
1124 revs->early_output = count;
1126 } else if (!strcmp(arg, "--parents")) {
1127 revs->rewrite_parents = 1;
1128 revs->print_parents = 1;
1129 } else if (!strcmp(arg, "--dense")) {
1130 revs->dense = 1;
1131 } else if (!strcmp(arg, "--sparse")) {
1132 revs->dense = 0;
1133 } else if (!strcmp(arg, "--show-all")) {
1134 revs->show_all = 1;
1135 } else if (!strcmp(arg, "--remove-empty")) {
1136 revs->remove_empty_trees = 1;
1137 } else if (!strcmp(arg, "--merges")) {
1138 revs->merges_only = 1;
1139 } else if (!strcmp(arg, "--no-merges")) {
1140 revs->no_merges = 1;
1141 } else if (!strcmp(arg, "--boundary")) {
1142 revs->boundary = 1;
1143 } else if (!strcmp(arg, "--left-right")) {
1144 revs->left_right = 1;
1145 } else if (!strcmp(arg, "--cherry-pick")) {
1146 revs->cherry_pick = 1;
1147 revs->limited = 1;
1148 } else if (!strcmp(arg, "--objects")) {
1149 revs->tag_objects = 1;
1150 revs->tree_objects = 1;
1151 revs->blob_objects = 1;
1152 } else if (!strcmp(arg, "--objects-edge")) {
1153 revs->tag_objects = 1;
1154 revs->tree_objects = 1;
1155 revs->blob_objects = 1;
1156 revs->edge_hint = 1;
1157 } else if (!strcmp(arg, "--unpacked")) {
1158 revs->unpacked = 1;
1159 } else if (!prefixcmp(arg, "--unpacked=")) {
1160 die("--unpacked=<packfile> no longer supported.");
1161 } else if (!strcmp(arg, "-r")) {
1162 revs->diff = 1;
1163 DIFF_OPT_SET(&revs->diffopt, RECURSIVE);
1164 } else if (!strcmp(arg, "-t")) {
1165 revs->diff = 1;
1166 DIFF_OPT_SET(&revs->diffopt, RECURSIVE);
1167 DIFF_OPT_SET(&revs->diffopt, TREE_IN_RECURSIVE);
1168 } else if (!strcmp(arg, "-m")) {
1169 revs->ignore_merges = 0;
1170 } else if (!strcmp(arg, "-c")) {
1171 revs->diff = 1;
1172 revs->dense_combined_merges = 0;
1173 revs->combine_merges = 1;
1174 } else if (!strcmp(arg, "--cc")) {
1175 revs->diff = 1;
1176 revs->dense_combined_merges = 1;
1177 revs->combine_merges = 1;
1178 } else if (!strcmp(arg, "-v")) {
1179 revs->verbose_header = 1;
1180 } else if (!strcmp(arg, "--pretty")) {
1181 revs->verbose_header = 1;
1182 revs->pretty_given = 1;
1183 get_commit_format(arg+8, revs);
1184 } else if (!prefixcmp(arg, "--pretty=") || !prefixcmp(arg, "--format=")) {
1185 revs->verbose_header = 1;
1186 revs->pretty_given = 1;
1187 get_commit_format(arg+9, revs);
1188 } else if (!strcmp(arg, "--show-notes")) {
1189 revs->show_notes = 1;
1190 revs->show_notes_given = 1;
1191 } else if (!strcmp(arg, "--no-notes")) {
1192 revs->show_notes = 0;
1193 revs->show_notes_given = 1;
1194 } else if (!strcmp(arg, "--oneline")) {
1195 revs->verbose_header = 1;
1196 get_commit_format("oneline", revs);
1197 revs->pretty_given = 1;
1198 revs->abbrev_commit = 1;
1199 } else if (!strcmp(arg, "--graph")) {
1200 revs->topo_order = 1;
1201 revs->rewrite_parents = 1;
1202 revs->graph = graph_init(revs);
1203 } else if (!strcmp(arg, "--root")) {
1204 revs->show_root_diff = 1;
1205 } else if (!strcmp(arg, "--no-commit-id")) {
1206 revs->no_commit_id = 1;
1207 } else if (!strcmp(arg, "--always")) {
1208 revs->always_show_header = 1;
1209 } else if (!strcmp(arg, "--no-abbrev")) {
1210 revs->abbrev = 0;
1211 } else if (!strcmp(arg, "--abbrev")) {
1212 revs->abbrev = DEFAULT_ABBREV;
1213 } else if (!prefixcmp(arg, "--abbrev=")) {
1214 revs->abbrev = strtoul(arg + 9, NULL, 10);
1215 if (revs->abbrev < MINIMUM_ABBREV)
1216 revs->abbrev = MINIMUM_ABBREV;
1217 else if (revs->abbrev > 40)
1218 revs->abbrev = 40;
1219 } else if (!strcmp(arg, "--abbrev-commit")) {
1220 revs->abbrev_commit = 1;
1221 } else if (!strcmp(arg, "--full-diff")) {
1222 revs->diff = 1;
1223 revs->full_diff = 1;
1224 } else if (!strcmp(arg, "--full-history")) {
1225 revs->simplify_history = 0;
1226 } else if (!strcmp(arg, "--relative-date")) {
1227 revs->date_mode = DATE_RELATIVE;
1228 revs->date_mode_explicit = 1;
1229 } else if (!strncmp(arg, "--date=", 7)) {
1230 revs->date_mode = parse_date_format(arg + 7);
1231 revs->date_mode_explicit = 1;
1232 } else if (!strcmp(arg, "--log-size")) {
1233 revs->show_log_size = 1;
1236 * Grepping the commit log
1238 else if (!prefixcmp(arg, "--author=")) {
1239 add_header_grep(revs, GREP_HEADER_AUTHOR, arg+9);
1240 } else if (!prefixcmp(arg, "--committer=")) {
1241 add_header_grep(revs, GREP_HEADER_COMMITTER, arg+12);
1242 } else if (!prefixcmp(arg, "--grep=")) {
1243 add_message_grep(revs, arg+7);
1244 } else if (!strcmp(arg, "--extended-regexp") || !strcmp(arg, "-E")) {
1245 revs->grep_filter.regflags |= REG_EXTENDED;
1246 } else if (!strcmp(arg, "--regexp-ignore-case") || !strcmp(arg, "-i")) {
1247 revs->grep_filter.regflags |= REG_ICASE;
1248 } else if (!strcmp(arg, "--fixed-strings") || !strcmp(arg, "-F")) {
1249 revs->grep_filter.fixed = 1;
1250 } else if (!strcmp(arg, "--all-match")) {
1251 revs->grep_filter.all_match = 1;
1252 } else if (!prefixcmp(arg, "--encoding=")) {
1253 arg += 11;
1254 if (strcmp(arg, "none"))
1255 git_log_output_encoding = xstrdup(arg);
1256 else
1257 git_log_output_encoding = "";
1258 } else if (!strcmp(arg, "--reverse")) {
1259 revs->reverse ^= 1;
1260 } else if (!strcmp(arg, "--children")) {
1261 revs->children.name = "children";
1262 revs->limited = 1;
1263 } else {
1264 int opts = diff_opt_parse(&revs->diffopt, argv, argc);
1265 if (!opts)
1266 unkv[(*unkc)++] = arg;
1267 return opts;
1270 return 1;
1273 void parse_revision_opt(struct rev_info *revs, struct parse_opt_ctx_t *ctx,
1274 const struct option *options,
1275 const char * const usagestr[])
1277 int n = handle_revision_opt(revs, ctx->argc, ctx->argv,
1278 &ctx->cpidx, ctx->out);
1279 if (n <= 0) {
1280 error("unknown option `%s'", ctx->argv[0]);
1281 usage_with_options(usagestr, options);
1283 ctx->argv += n;
1284 ctx->argc -= n;
1287 static int for_each_bad_bisect_ref(each_ref_fn fn, void *cb_data)
1289 return for_each_ref_in("refs/bisect/bad", fn, cb_data);
1292 static int for_each_good_bisect_ref(each_ref_fn fn, void *cb_data)
1294 return for_each_ref_in("refs/bisect/good", fn, cb_data);
1297 static void append_prune_data(const char ***prune_data, const char **av)
1299 const char **prune = *prune_data;
1300 int prune_nr;
1301 int prune_alloc;
1303 if (!prune) {
1304 *prune_data = av;
1305 return;
1308 /* count existing ones */
1309 for (prune_nr = 0; prune[prune_nr]; prune_nr++)
1311 prune_alloc = prune_nr; /* not really, but we do not know */
1313 while (*av) {
1314 ALLOC_GROW(prune, prune_nr+1, prune_alloc);
1315 prune[prune_nr++] = *av;
1316 av++;
1318 if (prune) {
1319 ALLOC_GROW(prune, prune_nr+1, prune_alloc);
1320 prune[prune_nr] = NULL;
1322 *prune_data = prune;
1326 * Parse revision information, filling in the "rev_info" structure,
1327 * and removing the used arguments from the argument list.
1329 * Returns the number of arguments left that weren't recognized
1330 * (which are also moved to the head of the argument list)
1332 int setup_revisions(int argc, const char **argv, struct rev_info *revs, const char *def)
1334 int i, flags, left, seen_dashdash, read_from_stdin;
1335 const char **prune_data = NULL;
1337 /* First, search for "--" */
1338 seen_dashdash = 0;
1339 for (i = 1; i < argc; i++) {
1340 const char *arg = argv[i];
1341 if (strcmp(arg, "--"))
1342 continue;
1343 argv[i] = NULL;
1344 argc = i;
1345 if (argv[i + 1])
1346 prune_data = argv + i + 1;
1347 seen_dashdash = 1;
1348 break;
1351 /* Second, deal with arguments and options */
1352 flags = 0;
1353 read_from_stdin = 0;
1354 for (left = i = 1; i < argc; i++) {
1355 const char *arg = argv[i];
1356 if (*arg == '-') {
1357 int opts;
1359 if (!strcmp(arg, "--all")) {
1360 handle_refs(revs, flags, for_each_ref);
1361 handle_refs(revs, flags, head_ref);
1362 continue;
1364 if (!strcmp(arg, "--branches")) {
1365 handle_refs(revs, flags, for_each_branch_ref);
1366 continue;
1368 if (!strcmp(arg, "--bisect")) {
1369 handle_refs(revs, flags, for_each_bad_bisect_ref);
1370 handle_refs(revs, flags ^ UNINTERESTING, for_each_good_bisect_ref);
1371 revs->bisect = 1;
1372 continue;
1374 if (!strcmp(arg, "--tags")) {
1375 handle_refs(revs, flags, for_each_tag_ref);
1376 continue;
1378 if (!strcmp(arg, "--remotes")) {
1379 handle_refs(revs, flags, for_each_remote_ref);
1380 continue;
1382 if (!prefixcmp(arg, "--glob=")) {
1383 struct all_refs_cb cb;
1384 init_all_refs_cb(&cb, revs, flags);
1385 for_each_glob_ref(handle_one_ref, arg + 7, &cb);
1386 continue;
1388 if (!prefixcmp(arg, "--branches=")) {
1389 struct all_refs_cb cb;
1390 init_all_refs_cb(&cb, revs, flags);
1391 for_each_glob_ref_in(handle_one_ref, arg + 11, "refs/heads/", &cb);
1392 continue;
1394 if (!prefixcmp(arg, "--tags=")) {
1395 struct all_refs_cb cb;
1396 init_all_refs_cb(&cb, revs, flags);
1397 for_each_glob_ref_in(handle_one_ref, arg + 7, "refs/tags/", &cb);
1398 continue;
1400 if (!prefixcmp(arg, "--remotes=")) {
1401 struct all_refs_cb cb;
1402 init_all_refs_cb(&cb, revs, flags);
1403 for_each_glob_ref_in(handle_one_ref, arg + 10, "refs/remotes/", &cb);
1404 continue;
1406 if (!strcmp(arg, "--reflog")) {
1407 handle_reflog(revs, flags);
1408 continue;
1410 if (!strcmp(arg, "--not")) {
1411 flags ^= UNINTERESTING;
1412 continue;
1414 if (!strcmp(arg, "--no-walk")) {
1415 revs->no_walk = 1;
1416 continue;
1418 if (!strcmp(arg, "--do-walk")) {
1419 revs->no_walk = 0;
1420 continue;
1422 if (!strcmp(arg, "--stdin")) {
1423 if (revs->disable_stdin) {
1424 argv[left++] = arg;
1425 continue;
1427 if (read_from_stdin++)
1428 die("--stdin given twice?");
1429 read_revisions_from_stdin(revs, &prune_data);
1430 continue;
1433 opts = handle_revision_opt(revs, argc - i, argv + i, &left, argv);
1434 if (opts > 0) {
1435 i += opts - 1;
1436 continue;
1438 if (opts < 0)
1439 exit(128);
1440 continue;
1443 if (handle_revision_arg(arg, revs, flags, seen_dashdash)) {
1444 int j;
1445 if (seen_dashdash || *arg == '^')
1446 die("bad revision '%s'", arg);
1448 /* If we didn't have a "--":
1449 * (1) all filenames must exist;
1450 * (2) all rev-args must not be interpretable
1451 * as a valid filename.
1452 * but the latter we have checked in the main loop.
1454 for (j = i; j < argc; j++)
1455 verify_filename(revs->prefix, argv[j]);
1457 append_prune_data(&prune_data, argv + i);
1458 break;
1462 if (prune_data)
1463 revs->prune_data = get_pathspec(revs->prefix, prune_data);
1465 if (revs->def == NULL)
1466 revs->def = def;
1467 if (revs->show_merge)
1468 prepare_show_merge(revs);
1469 if (revs->def && !revs->pending.nr) {
1470 unsigned char sha1[20];
1471 struct object *object;
1472 unsigned mode;
1473 if (get_sha1_with_mode(revs->def, sha1, &mode))
1474 die("bad default revision '%s'", revs->def);
1475 object = get_reference(revs, revs->def, sha1, 0);
1476 add_pending_object_with_mode(revs, object, revs->def, mode);
1479 /* Did the user ask for any diff output? Run the diff! */
1480 if (revs->diffopt.output_format & ~DIFF_FORMAT_NO_OUTPUT)
1481 revs->diff = 1;
1483 /* Pickaxe, diff-filter and rename following need diffs */
1484 if (revs->diffopt.pickaxe ||
1485 revs->diffopt.filter ||
1486 DIFF_OPT_TST(&revs->diffopt, FOLLOW_RENAMES))
1487 revs->diff = 1;
1489 if (revs->topo_order)
1490 revs->limited = 1;
1492 if (revs->prune_data) {
1493 diff_tree_setup_paths(revs->prune_data, &revs->pruning);
1494 /* Can't prune commits with rename following: the paths change.. */
1495 if (!DIFF_OPT_TST(&revs->diffopt, FOLLOW_RENAMES))
1496 revs->prune = 1;
1497 if (!revs->full_diff)
1498 diff_tree_setup_paths(revs->prune_data, &revs->diffopt);
1500 if (revs->combine_merges) {
1501 revs->ignore_merges = 0;
1502 if (revs->dense_combined_merges && !revs->diffopt.output_format)
1503 revs->diffopt.output_format = DIFF_FORMAT_PATCH;
1505 revs->diffopt.abbrev = revs->abbrev;
1506 if (diff_setup_done(&revs->diffopt) < 0)
1507 die("diff_setup_done failed");
1509 compile_grep_patterns(&revs->grep_filter);
1511 if (revs->reverse && revs->reflog_info)
1512 die("cannot combine --reverse with --walk-reflogs");
1513 if (revs->rewrite_parents && revs->children.name)
1514 die("cannot combine --parents and --children");
1517 * Limitations on the graph functionality
1519 if (revs->reverse && revs->graph)
1520 die("cannot combine --reverse with --graph");
1522 if (revs->reflog_info && revs->graph)
1523 die("cannot combine --walk-reflogs with --graph");
1525 return left;
1528 static void add_child(struct rev_info *revs, struct commit *parent, struct commit *child)
1530 struct commit_list *l = xcalloc(1, sizeof(*l));
1532 l->item = child;
1533 l->next = add_decoration(&revs->children, &parent->object, l);
1536 static int remove_duplicate_parents(struct commit *commit)
1538 struct commit_list **pp, *p;
1539 int surviving_parents;
1541 /* Examine existing parents while marking ones we have seen... */
1542 pp = &commit->parents;
1543 while ((p = *pp) != NULL) {
1544 struct commit *parent = p->item;
1545 if (parent->object.flags & TMP_MARK) {
1546 *pp = p->next;
1547 continue;
1549 parent->object.flags |= TMP_MARK;
1550 pp = &p->next;
1552 /* count them while clearing the temporary mark */
1553 surviving_parents = 0;
1554 for (p = commit->parents; p; p = p->next) {
1555 p->item->object.flags &= ~TMP_MARK;
1556 surviving_parents++;
1558 return surviving_parents;
1561 struct merge_simplify_state {
1562 struct commit *simplified;
1565 static struct merge_simplify_state *locate_simplify_state(struct rev_info *revs, struct commit *commit)
1567 struct merge_simplify_state *st;
1569 st = lookup_decoration(&revs->merge_simplification, &commit->object);
1570 if (!st) {
1571 st = xcalloc(1, sizeof(*st));
1572 add_decoration(&revs->merge_simplification, &commit->object, st);
1574 return st;
1577 static struct commit_list **simplify_one(struct rev_info *revs, struct commit *commit, struct commit_list **tail)
1579 struct commit_list *p;
1580 struct merge_simplify_state *st, *pst;
1581 int cnt;
1583 st = locate_simplify_state(revs, commit);
1586 * Have we handled this one?
1588 if (st->simplified)
1589 return tail;
1592 * An UNINTERESTING commit simplifies to itself, so does a
1593 * root commit. We do not rewrite parents of such commit
1594 * anyway.
1596 if ((commit->object.flags & UNINTERESTING) || !commit->parents) {
1597 st->simplified = commit;
1598 return tail;
1602 * Do we know what commit all of our parents should be rewritten to?
1603 * Otherwise we are not ready to rewrite this one yet.
1605 for (cnt = 0, p = commit->parents; p; p = p->next) {
1606 pst = locate_simplify_state(revs, p->item);
1607 if (!pst->simplified) {
1608 tail = &commit_list_insert(p->item, tail)->next;
1609 cnt++;
1612 if (cnt) {
1613 tail = &commit_list_insert(commit, tail)->next;
1614 return tail;
1618 * Rewrite our list of parents.
1620 for (p = commit->parents; p; p = p->next) {
1621 pst = locate_simplify_state(revs, p->item);
1622 p->item = pst->simplified;
1624 cnt = remove_duplicate_parents(commit);
1627 * It is possible that we are a merge and one side branch
1628 * does not have any commit that touches the given paths;
1629 * in such a case, the immediate parents will be rewritten
1630 * to different commits.
1632 * o----X X: the commit we are looking at;
1633 * / / o: a commit that touches the paths;
1634 * ---o----'
1636 * Further reduce the parents by removing redundant parents.
1638 if (1 < cnt) {
1639 struct commit_list *h = reduce_heads(commit->parents);
1640 cnt = commit_list_count(h);
1641 free_commit_list(commit->parents);
1642 commit->parents = h;
1646 * A commit simplifies to itself if it is a root, if it is
1647 * UNINTERESTING, if it touches the given paths, or if it is a
1648 * merge and its parents simplifies to more than one commits
1649 * (the first two cases are already handled at the beginning of
1650 * this function).
1652 * Otherwise, it simplifies to what its sole parent simplifies to.
1654 if (!cnt ||
1655 (commit->object.flags & UNINTERESTING) ||
1656 !(commit->object.flags & TREESAME) ||
1657 (1 < cnt))
1658 st->simplified = commit;
1659 else {
1660 pst = locate_simplify_state(revs, commit->parents->item);
1661 st->simplified = pst->simplified;
1663 return tail;
1666 static void simplify_merges(struct rev_info *revs)
1668 struct commit_list *list;
1669 struct commit_list *yet_to_do, **tail;
1671 if (!revs->topo_order)
1672 sort_in_topological_order(&revs->commits, revs->lifo);
1673 if (!revs->prune)
1674 return;
1676 /* feed the list reversed */
1677 yet_to_do = NULL;
1678 for (list = revs->commits; list; list = list->next)
1679 commit_list_insert(list->item, &yet_to_do);
1680 while (yet_to_do) {
1681 list = yet_to_do;
1682 yet_to_do = NULL;
1683 tail = &yet_to_do;
1684 while (list) {
1685 struct commit *commit = list->item;
1686 struct commit_list *next = list->next;
1687 free(list);
1688 list = next;
1689 tail = simplify_one(revs, commit, tail);
1693 /* clean up the result, removing the simplified ones */
1694 list = revs->commits;
1695 revs->commits = NULL;
1696 tail = &revs->commits;
1697 while (list) {
1698 struct commit *commit = list->item;
1699 struct commit_list *next = list->next;
1700 struct merge_simplify_state *st;
1701 free(list);
1702 list = next;
1703 st = locate_simplify_state(revs, commit);
1704 if (st->simplified == commit)
1705 tail = &commit_list_insert(commit, tail)->next;
1709 static void set_children(struct rev_info *revs)
1711 struct commit_list *l;
1712 for (l = revs->commits; l; l = l->next) {
1713 struct commit *commit = l->item;
1714 struct commit_list *p;
1716 for (p = commit->parents; p; p = p->next)
1717 add_child(revs, p->item, commit);
1721 int prepare_revision_walk(struct rev_info *revs)
1723 int nr = revs->pending.nr;
1724 struct object_array_entry *e, *list;
1726 e = list = revs->pending.objects;
1727 revs->pending.nr = 0;
1728 revs->pending.alloc = 0;
1729 revs->pending.objects = NULL;
1730 while (--nr >= 0) {
1731 struct commit *commit = handle_commit(revs, e->item, e->name);
1732 if (commit) {
1733 if (!(commit->object.flags & SEEN)) {
1734 commit->object.flags |= SEEN;
1735 insert_by_date(commit, &revs->commits);
1738 e++;
1740 free(list);
1742 if (revs->no_walk)
1743 return 0;
1744 if (revs->limited)
1745 if (limit_list(revs) < 0)
1746 return -1;
1747 if (revs->topo_order)
1748 sort_in_topological_order(&revs->commits, revs->lifo);
1749 if (revs->simplify_merges)
1750 simplify_merges(revs);
1751 if (revs->children.name)
1752 set_children(revs);
1753 return 0;
1756 enum rewrite_result {
1757 rewrite_one_ok,
1758 rewrite_one_noparents,
1759 rewrite_one_error,
1762 static enum rewrite_result rewrite_one(struct rev_info *revs, struct commit **pp)
1764 struct commit_list *cache = NULL;
1766 for (;;) {
1767 struct commit *p = *pp;
1768 if (!revs->limited)
1769 if (add_parents_to_list(revs, p, &revs->commits, &cache) < 0)
1770 return rewrite_one_error;
1771 if (p->parents && p->parents->next)
1772 return rewrite_one_ok;
1773 if (p->object.flags & UNINTERESTING)
1774 return rewrite_one_ok;
1775 if (!(p->object.flags & TREESAME))
1776 return rewrite_one_ok;
1777 if (!p->parents)
1778 return rewrite_one_noparents;
1779 *pp = p->parents->item;
1783 static int rewrite_parents(struct rev_info *revs, struct commit *commit)
1785 struct commit_list **pp = &commit->parents;
1786 while (*pp) {
1787 struct commit_list *parent = *pp;
1788 switch (rewrite_one(revs, &parent->item)) {
1789 case rewrite_one_ok:
1790 break;
1791 case rewrite_one_noparents:
1792 *pp = parent->next;
1793 continue;
1794 case rewrite_one_error:
1795 return -1;
1797 pp = &parent->next;
1799 remove_duplicate_parents(commit);
1800 return 0;
1803 static int commit_match(struct commit *commit, struct rev_info *opt)
1805 if (!opt->grep_filter.pattern_list && !opt->grep_filter.header_list)
1806 return 1;
1807 return grep_buffer(&opt->grep_filter,
1808 NULL, /* we say nothing, not even filename */
1809 commit->buffer, strlen(commit->buffer));
1812 static inline int want_ancestry(struct rev_info *revs)
1814 return (revs->rewrite_parents || revs->children.name);
1817 enum commit_action get_commit_action(struct rev_info *revs, struct commit *commit)
1819 if (commit->object.flags & SHOWN)
1820 return commit_ignore;
1821 if (revs->unpacked && has_sha1_pack(commit->object.sha1))
1822 return commit_ignore;
1823 if (revs->show_all)
1824 return commit_show;
1825 if (commit->object.flags & UNINTERESTING)
1826 return commit_ignore;
1827 if (revs->min_age != -1 && (commit->date > revs->min_age))
1828 return commit_ignore;
1829 if (revs->no_merges && commit->parents && commit->parents->next)
1830 return commit_ignore;
1831 if (revs->merges_only && !(commit->parents && commit->parents->next))
1832 return commit_ignore;
1833 if (!commit_match(commit, revs))
1834 return commit_ignore;
1835 if (revs->prune && revs->dense) {
1836 /* Commit without changes? */
1837 if (commit->object.flags & TREESAME) {
1838 /* drop merges unless we want parenthood */
1839 if (!want_ancestry(revs))
1840 return commit_ignore;
1841 /* non-merge - always ignore it */
1842 if (!commit->parents || !commit->parents->next)
1843 return commit_ignore;
1846 return commit_show;
1849 enum commit_action simplify_commit(struct rev_info *revs, struct commit *commit)
1851 enum commit_action action = get_commit_action(revs, commit);
1853 if (action == commit_show &&
1854 !revs->show_all &&
1855 revs->prune && revs->dense && want_ancestry(revs)) {
1856 if (rewrite_parents(revs, commit) < 0)
1857 return commit_error;
1859 return action;
1862 static struct commit *get_revision_1(struct rev_info *revs)
1864 if (!revs->commits)
1865 return NULL;
1867 do {
1868 struct commit_list *entry = revs->commits;
1869 struct commit *commit = entry->item;
1871 revs->commits = entry->next;
1872 free(entry);
1874 if (revs->reflog_info)
1875 fake_reflog_parent(revs->reflog_info, commit);
1878 * If we haven't done the list limiting, we need to look at
1879 * the parents here. We also need to do the date-based limiting
1880 * that we'd otherwise have done in limit_list().
1882 if (!revs->limited) {
1883 if (revs->max_age != -1 &&
1884 (commit->date < revs->max_age))
1885 continue;
1886 if (add_parents_to_list(revs, commit, &revs->commits, NULL) < 0)
1887 die("Failed to traverse parents of commit %s",
1888 sha1_to_hex(commit->object.sha1));
1891 switch (simplify_commit(revs, commit)) {
1892 case commit_ignore:
1893 continue;
1894 case commit_error:
1895 die("Failed to simplify parents of commit %s",
1896 sha1_to_hex(commit->object.sha1));
1897 default:
1898 return commit;
1900 } while (revs->commits);
1901 return NULL;
1904 static void gc_boundary(struct object_array *array)
1906 unsigned nr = array->nr;
1907 unsigned alloc = array->alloc;
1908 struct object_array_entry *objects = array->objects;
1910 if (alloc <= nr) {
1911 unsigned i, j;
1912 for (i = j = 0; i < nr; i++) {
1913 if (objects[i].item->flags & SHOWN)
1914 continue;
1915 if (i != j)
1916 objects[j] = objects[i];
1917 j++;
1919 for (i = j; i < nr; i++)
1920 objects[i].item = NULL;
1921 array->nr = j;
1925 static void create_boundary_commit_list(struct rev_info *revs)
1927 unsigned i;
1928 struct commit *c;
1929 struct object_array *array = &revs->boundary_commits;
1930 struct object_array_entry *objects = array->objects;
1933 * If revs->commits is non-NULL at this point, an error occurred in
1934 * get_revision_1(). Ignore the error and continue printing the
1935 * boundary commits anyway. (This is what the code has always
1936 * done.)
1938 if (revs->commits) {
1939 free_commit_list(revs->commits);
1940 revs->commits = NULL;
1944 * Put all of the actual boundary commits from revs->boundary_commits
1945 * into revs->commits
1947 for (i = 0; i < array->nr; i++) {
1948 c = (struct commit *)(objects[i].item);
1949 if (!c)
1950 continue;
1951 if (!(c->object.flags & CHILD_SHOWN))
1952 continue;
1953 if (c->object.flags & (SHOWN | BOUNDARY))
1954 continue;
1955 c->object.flags |= BOUNDARY;
1956 commit_list_insert(c, &revs->commits);
1960 * If revs->topo_order is set, sort the boundary commits
1961 * in topological order
1963 sort_in_topological_order(&revs->commits, revs->lifo);
1966 static struct commit *get_revision_internal(struct rev_info *revs)
1968 struct commit *c = NULL;
1969 struct commit_list *l;
1971 if (revs->boundary == 2) {
1973 * All of the normal commits have already been returned,
1974 * and we are now returning boundary commits.
1975 * create_boundary_commit_list() has populated
1976 * revs->commits with the remaining commits to return.
1978 c = pop_commit(&revs->commits);
1979 if (c)
1980 c->object.flags |= SHOWN;
1981 return c;
1985 * Now pick up what they want to give us
1987 c = get_revision_1(revs);
1988 if (c) {
1989 while (0 < revs->skip_count) {
1990 revs->skip_count--;
1991 c = get_revision_1(revs);
1992 if (!c)
1993 break;
1998 * Check the max_count.
2000 switch (revs->max_count) {
2001 case -1:
2002 break;
2003 case 0:
2004 c = NULL;
2005 break;
2006 default:
2007 revs->max_count--;
2010 if (c)
2011 c->object.flags |= SHOWN;
2013 if (!revs->boundary) {
2014 return c;
2017 if (!c) {
2019 * get_revision_1() runs out the commits, and
2020 * we are done computing the boundaries.
2021 * switch to boundary commits output mode.
2023 revs->boundary = 2;
2026 * Update revs->commits to contain the list of
2027 * boundary commits.
2029 create_boundary_commit_list(revs);
2031 return get_revision_internal(revs);
2035 * boundary commits are the commits that are parents of the
2036 * ones we got from get_revision_1() but they themselves are
2037 * not returned from get_revision_1(). Before returning
2038 * 'c', we need to mark its parents that they could be boundaries.
2041 for (l = c->parents; l; l = l->next) {
2042 struct object *p;
2043 p = &(l->item->object);
2044 if (p->flags & (CHILD_SHOWN | SHOWN))
2045 continue;
2046 p->flags |= CHILD_SHOWN;
2047 gc_boundary(&revs->boundary_commits);
2048 add_object_array(p, NULL, &revs->boundary_commits);
2051 return c;
2054 struct commit *get_revision(struct rev_info *revs)
2056 struct commit *c;
2057 struct commit_list *reversed;
2059 if (revs->reverse) {
2060 reversed = NULL;
2061 while ((c = get_revision_internal(revs))) {
2062 commit_list_insert(c, &reversed);
2064 revs->commits = reversed;
2065 revs->reverse = 0;
2066 revs->reverse_output_stage = 1;
2069 if (revs->reverse_output_stage)
2070 return pop_commit(&revs->commits);
2072 c = get_revision_internal(revs);
2073 if (c && revs->graph)
2074 graph_update(revs->graph, c);
2075 return c;