t4202: add test for "log --graph --stat -p" separator lines
[git/jrn.git] / revision.c
blob3d2deedaf8e50c6a71b9750dacf602bb5b9238b9
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 (!obj)
137 return;
138 if (revs->no_walk && (obj->flags & UNINTERESTING))
139 revs->no_walk = 0;
140 if (revs->reflog_info && obj->type == OBJ_COMMIT) {
141 struct strbuf buf = STRBUF_INIT;
142 int len = interpret_branch_name(name, &buf);
143 int st;
145 if (0 < len && name[len] && buf.len)
146 strbuf_addstr(&buf, name + len);
147 st = add_reflog_for_walk(revs->reflog_info,
148 (struct commit *)obj,
149 buf.buf[0] ? buf.buf: name);
150 strbuf_release(&buf);
151 if (st)
152 return;
154 add_object_array_with_mode(obj, name, &revs->pending, mode);
157 void add_pending_object(struct rev_info *revs, struct object *obj, const char *name)
159 add_pending_object_with_mode(revs, obj, name, S_IFINVALID);
162 void add_head_to_pending(struct rev_info *revs)
164 unsigned char sha1[20];
165 struct object *obj;
166 if (get_sha1("HEAD", sha1))
167 return;
168 obj = parse_object(sha1);
169 if (!obj)
170 return;
171 add_pending_object(revs, obj, "HEAD");
174 static struct object *get_reference(struct rev_info *revs, const char *name, const unsigned char *sha1, unsigned int flags)
176 struct object *object;
178 object = parse_object(sha1);
179 if (!object) {
180 if (revs->ignore_missing)
181 return object;
182 die("bad object %s", name);
184 object->flags |= flags;
185 return object;
188 static struct commit *handle_commit(struct rev_info *revs, struct object *object, const char *name)
190 unsigned long flags = object->flags;
193 * Tag object? Look what it points to..
195 while (object->type == OBJ_TAG) {
196 struct tag *tag = (struct tag *) object;
197 if (revs->tag_objects && !(flags & UNINTERESTING))
198 add_pending_object(revs, object, tag->tag);
199 if (!tag->tagged)
200 die("bad tag");
201 object = parse_object(tag->tagged->sha1);
202 if (!object) {
203 if (flags & UNINTERESTING)
204 return NULL;
205 die("bad object %s", sha1_to_hex(tag->tagged->sha1));
210 * Commit object? Just return it, we'll do all the complex
211 * reachability crud.
213 if (object->type == OBJ_COMMIT) {
214 struct commit *commit = (struct commit *)object;
215 if (parse_commit(commit) < 0)
216 die("unable to parse commit %s", name);
217 if (flags & UNINTERESTING) {
218 commit->object.flags |= UNINTERESTING;
219 mark_parents_uninteresting(commit);
220 revs->limited = 1;
222 if (revs->show_source && !commit->util)
223 commit->util = (void *) name;
224 return commit;
228 * Tree object? Either mark it uninteresting, or add it
229 * to the list of objects to look at later..
231 if (object->type == OBJ_TREE) {
232 struct tree *tree = (struct tree *)object;
233 if (!revs->tree_objects)
234 return NULL;
235 if (flags & UNINTERESTING) {
236 mark_tree_uninteresting(tree);
237 return NULL;
239 add_pending_object(revs, object, "");
240 return NULL;
244 * Blob object? You know the drill by now..
246 if (object->type == OBJ_BLOB) {
247 struct blob *blob = (struct blob *)object;
248 if (!revs->blob_objects)
249 return NULL;
250 if (flags & UNINTERESTING) {
251 mark_blob_uninteresting(blob);
252 return NULL;
254 add_pending_object(revs, object, "");
255 return NULL;
257 die("%s is unknown object", name);
260 static int everybody_uninteresting(struct commit_list *orig)
262 struct commit_list *list = orig;
263 while (list) {
264 struct commit *commit = list->item;
265 list = list->next;
266 if (commit->object.flags & UNINTERESTING)
267 continue;
268 return 0;
270 return 1;
274 * The goal is to get REV_TREE_NEW as the result only if the
275 * diff consists of all '+' (and no other changes), REV_TREE_OLD
276 * if the whole diff is removal of old data, and otherwise
277 * REV_TREE_DIFFERENT (of course if the trees are the same we
278 * want REV_TREE_SAME).
279 * That means that once we get to REV_TREE_DIFFERENT, we do not
280 * have to look any further.
282 static int tree_difference = REV_TREE_SAME;
284 static void file_add_remove(struct diff_options *options,
285 int addremove, unsigned mode,
286 const unsigned char *sha1,
287 const char *fullpath, unsigned dirty_submodule)
289 int diff = addremove == '+' ? REV_TREE_NEW : REV_TREE_OLD;
291 tree_difference |= diff;
292 if (tree_difference == REV_TREE_DIFFERENT)
293 DIFF_OPT_SET(options, HAS_CHANGES);
296 static void file_change(struct diff_options *options,
297 unsigned old_mode, unsigned new_mode,
298 const unsigned char *old_sha1,
299 const unsigned char *new_sha1,
300 const char *fullpath,
301 unsigned old_dirty_submodule, unsigned new_dirty_submodule)
303 tree_difference = REV_TREE_DIFFERENT;
304 DIFF_OPT_SET(options, HAS_CHANGES);
307 static int rev_compare_tree(struct rev_info *revs, struct commit *parent, struct commit *commit)
309 struct tree *t1 = parent->tree;
310 struct tree *t2 = commit->tree;
312 if (!t1)
313 return REV_TREE_NEW;
314 if (!t2)
315 return REV_TREE_OLD;
317 if (revs->simplify_by_decoration) {
319 * If we are simplifying by decoration, then the commit
320 * is worth showing if it has a tag pointing at it.
322 if (lookup_decoration(&name_decoration, &commit->object))
323 return REV_TREE_DIFFERENT;
325 * A commit that is not pointed by a tag is uninteresting
326 * if we are not limited by path. This means that you will
327 * see the usual "commits that touch the paths" plus any
328 * tagged commit by specifying both --simplify-by-decoration
329 * and pathspec.
331 if (!revs->prune_data.nr)
332 return REV_TREE_SAME;
335 tree_difference = REV_TREE_SAME;
336 DIFF_OPT_CLR(&revs->pruning, HAS_CHANGES);
337 if (diff_tree_sha1(t1->object.sha1, t2->object.sha1, "",
338 &revs->pruning) < 0)
339 return REV_TREE_DIFFERENT;
340 return tree_difference;
343 static int rev_same_tree_as_empty(struct rev_info *revs, struct commit *commit)
345 int retval;
346 void *tree;
347 unsigned long size;
348 struct tree_desc empty, real;
349 struct tree *t1 = commit->tree;
351 if (!t1)
352 return 0;
354 tree = read_object_with_reference(t1->object.sha1, tree_type, &size, NULL);
355 if (!tree)
356 return 0;
357 init_tree_desc(&real, tree, size);
358 init_tree_desc(&empty, "", 0);
360 tree_difference = REV_TREE_SAME;
361 DIFF_OPT_CLR(&revs->pruning, HAS_CHANGES);
362 retval = diff_tree(&empty, &real, "", &revs->pruning);
363 free(tree);
365 return retval >= 0 && (tree_difference == REV_TREE_SAME);
368 static void try_to_simplify_commit(struct rev_info *revs, struct commit *commit)
370 struct commit_list **pp, *parent;
371 int tree_changed = 0, tree_same = 0;
374 * If we don't do pruning, everything is interesting
376 if (!revs->prune)
377 return;
379 if (!commit->tree)
380 return;
382 if (!commit->parents) {
383 if (rev_same_tree_as_empty(revs, commit))
384 commit->object.flags |= TREESAME;
385 return;
389 * Normal non-merge commit? If we don't want to make the
390 * history dense, we consider it always to be a change..
392 if (!revs->dense && !commit->parents->next)
393 return;
395 pp = &commit->parents;
396 while ((parent = *pp) != NULL) {
397 struct commit *p = parent->item;
399 if (parse_commit(p) < 0)
400 die("cannot simplify commit %s (because of %s)",
401 sha1_to_hex(commit->object.sha1),
402 sha1_to_hex(p->object.sha1));
403 switch (rev_compare_tree(revs, p, commit)) {
404 case REV_TREE_SAME:
405 tree_same = 1;
406 if (!revs->simplify_history || (p->object.flags & UNINTERESTING)) {
407 /* Even if a merge with an uninteresting
408 * side branch brought the entire change
409 * we are interested in, we do not want
410 * to lose the other branches of this
411 * merge, so we just keep going.
413 pp = &parent->next;
414 continue;
416 parent->next = NULL;
417 commit->parents = parent;
418 commit->object.flags |= TREESAME;
419 return;
421 case REV_TREE_NEW:
422 if (revs->remove_empty_trees &&
423 rev_same_tree_as_empty(revs, p)) {
424 /* We are adding all the specified
425 * paths from this parent, so the
426 * history beyond this parent is not
427 * interesting. Remove its parents
428 * (they are grandparents for us).
429 * IOW, we pretend this parent is a
430 * "root" commit.
432 if (parse_commit(p) < 0)
433 die("cannot simplify commit %s (invalid %s)",
434 sha1_to_hex(commit->object.sha1),
435 sha1_to_hex(p->object.sha1));
436 p->parents = NULL;
438 /* fallthrough */
439 case REV_TREE_OLD:
440 case REV_TREE_DIFFERENT:
441 tree_changed = 1;
442 pp = &parent->next;
443 continue;
445 die("bad tree compare for commit %s", sha1_to_hex(commit->object.sha1));
447 if (tree_changed && !tree_same)
448 return;
449 commit->object.flags |= TREESAME;
452 static void commit_list_insert_by_date_cached(struct commit *p, struct commit_list **head,
453 struct commit_list *cached_base, struct commit_list **cache)
455 struct commit_list *new_entry;
457 if (cached_base && p->date < cached_base->item->date)
458 new_entry = commit_list_insert_by_date(p, &cached_base->next);
459 else
460 new_entry = commit_list_insert_by_date(p, head);
462 if (cache && (!*cache || p->date < (*cache)->item->date))
463 *cache = new_entry;
466 static int add_parents_to_list(struct rev_info *revs, struct commit *commit,
467 struct commit_list **list, struct commit_list **cache_ptr)
469 struct commit_list *parent = commit->parents;
470 unsigned left_flag;
471 struct commit_list *cached_base = cache_ptr ? *cache_ptr : NULL;
473 if (commit->object.flags & ADDED)
474 return 0;
475 commit->object.flags |= ADDED;
478 * If the commit is uninteresting, don't try to
479 * prune parents - we want the maximal uninteresting
480 * set.
482 * Normally we haven't parsed the parent
483 * yet, so we won't have a parent of a parent
484 * here. However, it may turn out that we've
485 * reached this commit some other way (where it
486 * wasn't uninteresting), in which case we need
487 * to mark its parents recursively too..
489 if (commit->object.flags & UNINTERESTING) {
490 while (parent) {
491 struct commit *p = parent->item;
492 parent = parent->next;
493 if (p)
494 p->object.flags |= UNINTERESTING;
495 if (parse_commit(p) < 0)
496 continue;
497 if (p->parents)
498 mark_parents_uninteresting(p);
499 if (p->object.flags & SEEN)
500 continue;
501 p->object.flags |= SEEN;
502 commit_list_insert_by_date_cached(p, list, cached_base, cache_ptr);
504 return 0;
508 * Ok, the commit wasn't uninteresting. Try to
509 * simplify the commit history and find the parent
510 * that has no differences in the path set if one exists.
512 try_to_simplify_commit(revs, commit);
514 if (revs->no_walk)
515 return 0;
517 left_flag = (commit->object.flags & SYMMETRIC_LEFT);
519 for (parent = commit->parents; parent; parent = parent->next) {
520 struct commit *p = parent->item;
522 if (parse_commit(p) < 0)
523 return -1;
524 if (revs->show_source && !p->util)
525 p->util = commit->util;
526 p->object.flags |= left_flag;
527 if (!(p->object.flags & SEEN)) {
528 p->object.flags |= SEEN;
529 commit_list_insert_by_date_cached(p, list, cached_base, cache_ptr);
531 if (revs->first_parent_only)
532 break;
534 return 0;
537 static void cherry_pick_list(struct commit_list *list, struct rev_info *revs)
539 struct commit_list *p;
540 int left_count = 0, right_count = 0;
541 int left_first;
542 struct patch_ids ids;
543 unsigned cherry_flag;
545 /* First count the commits on the left and on the right */
546 for (p = list; p; p = p->next) {
547 struct commit *commit = p->item;
548 unsigned flags = commit->object.flags;
549 if (flags & BOUNDARY)
551 else if (flags & SYMMETRIC_LEFT)
552 left_count++;
553 else
554 right_count++;
557 if (!left_count || !right_count)
558 return;
560 left_first = left_count < right_count;
561 init_patch_ids(&ids);
562 ids.diffopts.pathspec = revs->diffopt.pathspec;
564 /* Compute patch-ids for one side */
565 for (p = list; p; p = p->next) {
566 struct commit *commit = p->item;
567 unsigned flags = commit->object.flags;
569 if (flags & BOUNDARY)
570 continue;
572 * If we have fewer left, left_first is set and we omit
573 * commits on the right branch in this loop. If we have
574 * fewer right, we skip the left ones.
576 if (left_first != !!(flags & SYMMETRIC_LEFT))
577 continue;
578 commit->util = add_commit_patch_id(commit, &ids);
581 /* either cherry_mark or cherry_pick are true */
582 cherry_flag = revs->cherry_mark ? PATCHSAME : SHOWN;
584 /* Check the other side */
585 for (p = list; p; p = p->next) {
586 struct commit *commit = p->item;
587 struct patch_id *id;
588 unsigned flags = commit->object.flags;
590 if (flags & BOUNDARY)
591 continue;
593 * If we have fewer left, left_first is set and we omit
594 * commits on the left branch in this loop.
596 if (left_first == !!(flags & SYMMETRIC_LEFT))
597 continue;
600 * Have we seen the same patch id?
602 id = has_commit_patch_id(commit, &ids);
603 if (!id)
604 continue;
605 id->seen = 1;
606 commit->object.flags |= cherry_flag;
609 /* Now check the original side for seen ones */
610 for (p = list; p; p = p->next) {
611 struct commit *commit = p->item;
612 struct patch_id *ent;
614 ent = commit->util;
615 if (!ent)
616 continue;
617 if (ent->seen)
618 commit->object.flags |= cherry_flag;
619 commit->util = NULL;
622 free_patch_ids(&ids);
625 /* How many extra uninteresting commits we want to see.. */
626 #define SLOP 5
628 static int still_interesting(struct commit_list *src, unsigned long date, int slop)
631 * No source list at all? We're definitely done..
633 if (!src)
634 return 0;
637 * Does the destination list contain entries with a date
638 * before the source list? Definitely _not_ done.
640 if (date < src->item->date)
641 return SLOP;
644 * Does the source list still have interesting commits in
645 * it? Definitely not done..
647 if (!everybody_uninteresting(src))
648 return SLOP;
650 /* Ok, we're closing in.. */
651 return slop-1;
655 * "rev-list --ancestry-path A..B" computes commits that are ancestors
656 * of B but not ancestors of A but further limits the result to those
657 * that are descendants of A. This takes the list of bottom commits and
658 * the result of "A..B" without --ancestry-path, and limits the latter
659 * further to the ones that can reach one of the commits in "bottom".
661 static void limit_to_ancestry(struct commit_list *bottom, struct commit_list *list)
663 struct commit_list *p;
664 struct commit_list *rlist = NULL;
665 int made_progress;
668 * Reverse the list so that it will be likely that we would
669 * process parents before children.
671 for (p = list; p; p = p->next)
672 commit_list_insert(p->item, &rlist);
674 for (p = bottom; p; p = p->next)
675 p->item->object.flags |= TMP_MARK;
678 * Mark the ones that can reach bottom commits in "list",
679 * in a bottom-up fashion.
681 do {
682 made_progress = 0;
683 for (p = rlist; p; p = p->next) {
684 struct commit *c = p->item;
685 struct commit_list *parents;
686 if (c->object.flags & (TMP_MARK | UNINTERESTING))
687 continue;
688 for (parents = c->parents;
689 parents;
690 parents = parents->next) {
691 if (!(parents->item->object.flags & TMP_MARK))
692 continue;
693 c->object.flags |= TMP_MARK;
694 made_progress = 1;
695 break;
698 } while (made_progress);
701 * NEEDSWORK: decide if we want to remove parents that are
702 * not marked with TMP_MARK from commit->parents for commits
703 * in the resulting list. We may not want to do that, though.
707 * The ones that are not marked with TMP_MARK are uninteresting
709 for (p = list; p; p = p->next) {
710 struct commit *c = p->item;
711 if (c->object.flags & TMP_MARK)
712 continue;
713 c->object.flags |= UNINTERESTING;
716 /* We are done with the TMP_MARK */
717 for (p = list; p; p = p->next)
718 p->item->object.flags &= ~TMP_MARK;
719 for (p = bottom; p; p = p->next)
720 p->item->object.flags &= ~TMP_MARK;
721 free_commit_list(rlist);
725 * Before walking the history, keep the set of "negative" refs the
726 * caller has asked to exclude.
728 * This is used to compute "rev-list --ancestry-path A..B", as we need
729 * to filter the result of "A..B" further to the ones that can actually
730 * reach A.
732 static struct commit_list *collect_bottom_commits(struct rev_info *revs)
734 struct commit_list *bottom = NULL;
735 int i;
736 for (i = 0; i < revs->cmdline.nr; i++) {
737 struct rev_cmdline_entry *elem = &revs->cmdline.rev[i];
738 if ((elem->flags & UNINTERESTING) &&
739 elem->item->type == OBJ_COMMIT)
740 commit_list_insert((struct commit *)elem->item, &bottom);
742 return bottom;
745 /* Assumes either left_only or right_only is set */
746 static void limit_left_right(struct commit_list *list, struct rev_info *revs)
748 struct commit_list *p;
750 for (p = list; p; p = p->next) {
751 struct commit *commit = p->item;
753 if (revs->right_only) {
754 if (commit->object.flags & SYMMETRIC_LEFT)
755 commit->object.flags |= SHOWN;
756 } else /* revs->left_only is set */
757 if (!(commit->object.flags & SYMMETRIC_LEFT))
758 commit->object.flags |= SHOWN;
762 static int limit_list(struct rev_info *revs)
764 int slop = SLOP;
765 unsigned long date = ~0ul;
766 struct commit_list *list = revs->commits;
767 struct commit_list *newlist = NULL;
768 struct commit_list **p = &newlist;
769 struct commit_list *bottom = NULL;
771 if (revs->ancestry_path) {
772 bottom = collect_bottom_commits(revs);
773 if (!bottom)
774 die("--ancestry-path given but there are no bottom commits");
777 while (list) {
778 struct commit_list *entry = list;
779 struct commit *commit = list->item;
780 struct object *obj = &commit->object;
781 show_early_output_fn_t show;
783 list = list->next;
784 free(entry);
786 if (revs->max_age != -1 && (commit->date < revs->max_age))
787 obj->flags |= UNINTERESTING;
788 if (add_parents_to_list(revs, commit, &list, NULL) < 0)
789 return -1;
790 if (obj->flags & UNINTERESTING) {
791 mark_parents_uninteresting(commit);
792 if (revs->show_all)
793 p = &commit_list_insert(commit, p)->next;
794 slop = still_interesting(list, date, slop);
795 if (slop)
796 continue;
797 /* If showing all, add the whole pending list to the end */
798 if (revs->show_all)
799 *p = list;
800 break;
802 if (revs->min_age != -1 && (commit->date > revs->min_age))
803 continue;
804 date = commit->date;
805 p = &commit_list_insert(commit, p)->next;
807 show = show_early_output;
808 if (!show)
809 continue;
811 show(revs, newlist);
812 show_early_output = NULL;
814 if (revs->cherry_pick || revs->cherry_mark)
815 cherry_pick_list(newlist, revs);
817 if (revs->left_only || revs->right_only)
818 limit_left_right(newlist, revs);
820 if (bottom) {
821 limit_to_ancestry(bottom, newlist);
822 free_commit_list(bottom);
825 revs->commits = newlist;
826 return 0;
829 static void add_rev_cmdline(struct rev_info *revs,
830 struct object *item,
831 const char *name,
832 int whence,
833 unsigned flags)
835 struct rev_cmdline_info *info = &revs->cmdline;
836 int nr = info->nr;
838 ALLOC_GROW(info->rev, nr + 1, info->alloc);
839 info->rev[nr].item = item;
840 info->rev[nr].name = name;
841 info->rev[nr].whence = whence;
842 info->rev[nr].flags = flags;
843 info->nr++;
846 struct all_refs_cb {
847 int all_flags;
848 int warned_bad_reflog;
849 struct rev_info *all_revs;
850 const char *name_for_errormsg;
853 static int handle_one_ref(const char *path, const unsigned char *sha1, int flag, void *cb_data)
855 struct all_refs_cb *cb = cb_data;
856 struct object *object = get_reference(cb->all_revs, path, sha1,
857 cb->all_flags);
858 add_rev_cmdline(cb->all_revs, object, path, REV_CMD_REF, cb->all_flags);
859 add_pending_object(cb->all_revs, object, path);
860 return 0;
863 static void init_all_refs_cb(struct all_refs_cb *cb, struct rev_info *revs,
864 unsigned flags)
866 cb->all_revs = revs;
867 cb->all_flags = flags;
870 static void handle_refs(const char *submodule, struct rev_info *revs, unsigned flags,
871 int (*for_each)(const char *, each_ref_fn, void *))
873 struct all_refs_cb cb;
874 init_all_refs_cb(&cb, revs, flags);
875 for_each(submodule, handle_one_ref, &cb);
878 static void handle_one_reflog_commit(unsigned char *sha1, void *cb_data)
880 struct all_refs_cb *cb = cb_data;
881 if (!is_null_sha1(sha1)) {
882 struct object *o = parse_object(sha1);
883 if (o) {
884 o->flags |= cb->all_flags;
885 /* ??? CMDLINEFLAGS ??? */
886 add_pending_object(cb->all_revs, o, "");
888 else if (!cb->warned_bad_reflog) {
889 warning("reflog of '%s' references pruned commits",
890 cb->name_for_errormsg);
891 cb->warned_bad_reflog = 1;
896 static int handle_one_reflog_ent(unsigned char *osha1, unsigned char *nsha1,
897 const char *email, unsigned long timestamp, int tz,
898 const char *message, void *cb_data)
900 handle_one_reflog_commit(osha1, cb_data);
901 handle_one_reflog_commit(nsha1, cb_data);
902 return 0;
905 static int handle_one_reflog(const char *path, const unsigned char *sha1, int flag, void *cb_data)
907 struct all_refs_cb *cb = cb_data;
908 cb->warned_bad_reflog = 0;
909 cb->name_for_errormsg = path;
910 for_each_reflog_ent(path, handle_one_reflog_ent, cb_data);
911 return 0;
914 static void handle_reflog(struct rev_info *revs, unsigned flags)
916 struct all_refs_cb cb;
917 cb.all_revs = revs;
918 cb.all_flags = flags;
919 for_each_reflog(handle_one_reflog, &cb);
922 static int add_parents_only(struct rev_info *revs, const char *arg_, int flags)
924 unsigned char sha1[20];
925 struct object *it;
926 struct commit *commit;
927 struct commit_list *parents;
928 const char *arg = arg_;
930 if (*arg == '^') {
931 flags ^= UNINTERESTING;
932 arg++;
934 if (get_sha1(arg, sha1))
935 return 0;
936 while (1) {
937 it = get_reference(revs, arg, sha1, 0);
938 if (!it && revs->ignore_missing)
939 return 0;
940 if (it->type != OBJ_TAG)
941 break;
942 if (!((struct tag*)it)->tagged)
943 return 0;
944 hashcpy(sha1, ((struct tag*)it)->tagged->sha1);
946 if (it->type != OBJ_COMMIT)
947 return 0;
948 commit = (struct commit *)it;
949 for (parents = commit->parents; parents; parents = parents->next) {
950 it = &parents->item->object;
951 it->flags |= flags;
952 add_rev_cmdline(revs, it, arg_, REV_CMD_PARENTS_ONLY, flags);
953 add_pending_object(revs, it, arg);
955 return 1;
958 void init_revisions(struct rev_info *revs, const char *prefix)
960 memset(revs, 0, sizeof(*revs));
962 revs->abbrev = DEFAULT_ABBREV;
963 revs->ignore_merges = 1;
964 revs->simplify_history = 1;
965 DIFF_OPT_SET(&revs->pruning, RECURSIVE);
966 DIFF_OPT_SET(&revs->pruning, QUICK);
967 revs->pruning.add_remove = file_add_remove;
968 revs->pruning.change = file_change;
969 revs->lifo = 1;
970 revs->dense = 1;
971 revs->prefix = prefix;
972 revs->max_age = -1;
973 revs->min_age = -1;
974 revs->skip_count = -1;
975 revs->max_count = -1;
976 revs->max_parents = -1;
978 revs->commit_format = CMIT_FMT_DEFAULT;
980 revs->grep_filter.status_only = 1;
981 revs->grep_filter.pattern_tail = &(revs->grep_filter.pattern_list);
982 revs->grep_filter.header_tail = &(revs->grep_filter.header_list);
983 revs->grep_filter.regflags = REG_NEWLINE;
985 diff_setup(&revs->diffopt);
986 if (prefix && !revs->diffopt.prefix) {
987 revs->diffopt.prefix = prefix;
988 revs->diffopt.prefix_length = strlen(prefix);
991 revs->notes_opt.use_default_notes = -1;
994 static void add_pending_commit_list(struct rev_info *revs,
995 struct commit_list *commit_list,
996 unsigned int flags)
998 while (commit_list) {
999 struct object *object = &commit_list->item->object;
1000 object->flags |= flags;
1001 add_pending_object(revs, object, sha1_to_hex(object->sha1));
1002 commit_list = commit_list->next;
1006 static void prepare_show_merge(struct rev_info *revs)
1008 struct commit_list *bases;
1009 struct commit *head, *other;
1010 unsigned char sha1[20];
1011 const char **prune = NULL;
1012 int i, prune_num = 1; /* counting terminating NULL */
1014 if (get_sha1("HEAD", sha1))
1015 die("--merge without HEAD?");
1016 head = lookup_commit_or_die(sha1, "HEAD");
1017 if (get_sha1("MERGE_HEAD", sha1))
1018 die("--merge without MERGE_HEAD?");
1019 other = lookup_commit_or_die(sha1, "MERGE_HEAD");
1020 add_pending_object(revs, &head->object, "HEAD");
1021 add_pending_object(revs, &other->object, "MERGE_HEAD");
1022 bases = get_merge_bases(head, other, 1);
1023 add_pending_commit_list(revs, bases, UNINTERESTING);
1024 free_commit_list(bases);
1025 head->object.flags |= SYMMETRIC_LEFT;
1027 if (!active_nr)
1028 read_cache();
1029 for (i = 0; i < active_nr; i++) {
1030 struct cache_entry *ce = active_cache[i];
1031 if (!ce_stage(ce))
1032 continue;
1033 if (ce_path_match(ce, &revs->prune_data)) {
1034 prune_num++;
1035 prune = xrealloc(prune, sizeof(*prune) * prune_num);
1036 prune[prune_num-2] = ce->name;
1037 prune[prune_num-1] = NULL;
1039 while ((i+1 < active_nr) &&
1040 ce_same_name(ce, active_cache[i+1]))
1041 i++;
1043 free_pathspec(&revs->prune_data);
1044 init_pathspec(&revs->prune_data, prune);
1045 revs->limited = 1;
1048 int handle_revision_arg(const char *arg_, struct rev_info *revs,
1049 int flags,
1050 int cant_be_filename)
1052 unsigned mode;
1053 char *dotdot;
1054 struct object *object;
1055 unsigned char sha1[20];
1056 int local_flags;
1057 const char *arg = arg_;
1059 dotdot = strstr(arg, "..");
1060 if (dotdot) {
1061 unsigned char from_sha1[20];
1062 const char *next = dotdot + 2;
1063 const char *this = arg;
1064 int symmetric = *next == '.';
1065 unsigned int flags_exclude = flags ^ UNINTERESTING;
1066 unsigned int a_flags;
1068 *dotdot = 0;
1069 next += symmetric;
1071 if (!*next)
1072 next = "HEAD";
1073 if (dotdot == arg)
1074 this = "HEAD";
1075 if (!get_sha1(this, from_sha1) &&
1076 !get_sha1(next, sha1)) {
1077 struct commit *a, *b;
1078 struct commit_list *exclude;
1080 a = lookup_commit_reference(from_sha1);
1081 b = lookup_commit_reference(sha1);
1082 if (!a || !b) {
1083 if (revs->ignore_missing)
1084 return 0;
1085 die(symmetric ?
1086 "Invalid symmetric difference expression %s...%s" :
1087 "Invalid revision range %s..%s",
1088 arg, next);
1091 if (!cant_be_filename) {
1092 *dotdot = '.';
1093 verify_non_filename(revs->prefix, arg);
1096 if (symmetric) {
1097 exclude = get_merge_bases(a, b, 1);
1098 add_pending_commit_list(revs, exclude,
1099 flags_exclude);
1100 free_commit_list(exclude);
1101 a_flags = flags | SYMMETRIC_LEFT;
1102 } else
1103 a_flags = flags_exclude;
1104 a->object.flags |= a_flags;
1105 b->object.flags |= flags;
1106 add_rev_cmdline(revs, &a->object, this,
1107 REV_CMD_LEFT, a_flags);
1108 add_rev_cmdline(revs, &b->object, next,
1109 REV_CMD_RIGHT, flags);
1110 add_pending_object(revs, &a->object, this);
1111 add_pending_object(revs, &b->object, next);
1112 return 0;
1114 *dotdot = '.';
1116 dotdot = strstr(arg, "^@");
1117 if (dotdot && !dotdot[2]) {
1118 *dotdot = 0;
1119 if (add_parents_only(revs, arg, flags))
1120 return 0;
1121 *dotdot = '^';
1123 dotdot = strstr(arg, "^!");
1124 if (dotdot && !dotdot[2]) {
1125 *dotdot = 0;
1126 if (!add_parents_only(revs, arg, flags ^ UNINTERESTING))
1127 *dotdot = '^';
1130 local_flags = 0;
1131 if (*arg == '^') {
1132 local_flags = UNINTERESTING;
1133 arg++;
1135 if (get_sha1_with_mode(arg, sha1, &mode))
1136 return revs->ignore_missing ? 0 : -1;
1137 if (!cant_be_filename)
1138 verify_non_filename(revs->prefix, arg);
1139 object = get_reference(revs, arg, sha1, flags ^ local_flags);
1140 add_rev_cmdline(revs, object, arg_, REV_CMD_REV, flags ^ local_flags);
1141 add_pending_object_with_mode(revs, object, arg, mode);
1142 return 0;
1145 struct cmdline_pathspec {
1146 int alloc;
1147 int nr;
1148 const char **path;
1151 static void append_prune_data(struct cmdline_pathspec *prune, const char **av)
1153 while (*av) {
1154 ALLOC_GROW(prune->path, prune->nr+1, prune->alloc);
1155 prune->path[prune->nr++] = *(av++);
1159 static void read_pathspec_from_stdin(struct rev_info *revs, struct strbuf *sb,
1160 struct cmdline_pathspec *prune)
1162 while (strbuf_getwholeline(sb, stdin, '\n') != EOF) {
1163 int len = sb->len;
1164 if (len && sb->buf[len - 1] == '\n')
1165 sb->buf[--len] = '\0';
1166 ALLOC_GROW(prune->path, prune->nr+1, prune->alloc);
1167 prune->path[prune->nr++] = xstrdup(sb->buf);
1171 static void read_revisions_from_stdin(struct rev_info *revs,
1172 struct cmdline_pathspec *prune)
1174 struct strbuf sb;
1175 int seen_dashdash = 0;
1177 strbuf_init(&sb, 1000);
1178 while (strbuf_getwholeline(&sb, stdin, '\n') != EOF) {
1179 int len = sb.len;
1180 if (len && sb.buf[len - 1] == '\n')
1181 sb.buf[--len] = '\0';
1182 if (!len)
1183 break;
1184 if (sb.buf[0] == '-') {
1185 if (len == 2 && sb.buf[1] == '-') {
1186 seen_dashdash = 1;
1187 break;
1189 die("options not supported in --stdin mode");
1191 if (handle_revision_arg(sb.buf, revs, 0, 1))
1192 die("bad revision '%s'", sb.buf);
1194 if (seen_dashdash)
1195 read_pathspec_from_stdin(revs, &sb, prune);
1196 strbuf_release(&sb);
1199 static void add_grep(struct rev_info *revs, const char *ptn, enum grep_pat_token what)
1201 append_grep_pattern(&revs->grep_filter, ptn, "command line", 0, what);
1204 static void add_header_grep(struct rev_info *revs, enum grep_header_field field, const char *pattern)
1206 append_header_grep_pattern(&revs->grep_filter, field, pattern);
1209 static void add_message_grep(struct rev_info *revs, const char *pattern)
1211 add_grep(revs, pattern, GREP_PATTERN_BODY);
1214 static int handle_revision_opt(struct rev_info *revs, int argc, const char **argv,
1215 int *unkc, const char **unkv)
1217 const char *arg = argv[0];
1218 const char *optarg;
1219 int argcount;
1221 /* pseudo revision arguments */
1222 if (!strcmp(arg, "--all") || !strcmp(arg, "--branches") ||
1223 !strcmp(arg, "--tags") || !strcmp(arg, "--remotes") ||
1224 !strcmp(arg, "--reflog") || !strcmp(arg, "--not") ||
1225 !strcmp(arg, "--no-walk") || !strcmp(arg, "--do-walk") ||
1226 !strcmp(arg, "--bisect") || !prefixcmp(arg, "--glob=") ||
1227 !prefixcmp(arg, "--branches=") || !prefixcmp(arg, "--tags=") ||
1228 !prefixcmp(arg, "--remotes="))
1230 unkv[(*unkc)++] = arg;
1231 return 1;
1234 if ((argcount = parse_long_opt("max-count", argv, &optarg))) {
1235 revs->max_count = atoi(optarg);
1236 revs->no_walk = 0;
1237 return argcount;
1238 } else if ((argcount = parse_long_opt("skip", argv, &optarg))) {
1239 revs->skip_count = atoi(optarg);
1240 return argcount;
1241 } else if ((*arg == '-') && isdigit(arg[1])) {
1242 /* accept -<digit>, like traditional "head" */
1243 revs->max_count = atoi(arg + 1);
1244 revs->no_walk = 0;
1245 } else if (!strcmp(arg, "-n")) {
1246 if (argc <= 1)
1247 return error("-n requires an argument");
1248 revs->max_count = atoi(argv[1]);
1249 revs->no_walk = 0;
1250 return 2;
1251 } else if (!prefixcmp(arg, "-n")) {
1252 revs->max_count = atoi(arg + 2);
1253 revs->no_walk = 0;
1254 } else if ((argcount = parse_long_opt("max-age", argv, &optarg))) {
1255 revs->max_age = atoi(optarg);
1256 return argcount;
1257 } else if ((argcount = parse_long_opt("since", argv, &optarg))) {
1258 revs->max_age = approxidate(optarg);
1259 return argcount;
1260 } else if ((argcount = parse_long_opt("after", argv, &optarg))) {
1261 revs->max_age = approxidate(optarg);
1262 return argcount;
1263 } else if ((argcount = parse_long_opt("min-age", argv, &optarg))) {
1264 revs->min_age = atoi(optarg);
1265 return argcount;
1266 } else if ((argcount = parse_long_opt("before", argv, &optarg))) {
1267 revs->min_age = approxidate(optarg);
1268 return argcount;
1269 } else if ((argcount = parse_long_opt("until", argv, &optarg))) {
1270 revs->min_age = approxidate(optarg);
1271 return argcount;
1272 } else if (!strcmp(arg, "--first-parent")) {
1273 revs->first_parent_only = 1;
1274 } else if (!strcmp(arg, "--ancestry-path")) {
1275 revs->ancestry_path = 1;
1276 revs->simplify_history = 0;
1277 revs->limited = 1;
1278 } else if (!strcmp(arg, "-g") || !strcmp(arg, "--walk-reflogs")) {
1279 init_reflog_walk(&revs->reflog_info);
1280 } else if (!strcmp(arg, "--default")) {
1281 if (argc <= 1)
1282 return error("bad --default argument");
1283 revs->def = argv[1];
1284 return 2;
1285 } else if (!strcmp(arg, "--merge")) {
1286 revs->show_merge = 1;
1287 } else if (!strcmp(arg, "--topo-order")) {
1288 revs->lifo = 1;
1289 revs->topo_order = 1;
1290 } else if (!strcmp(arg, "--simplify-merges")) {
1291 revs->simplify_merges = 1;
1292 revs->rewrite_parents = 1;
1293 revs->simplify_history = 0;
1294 revs->limited = 1;
1295 } else if (!strcmp(arg, "--simplify-by-decoration")) {
1296 revs->simplify_merges = 1;
1297 revs->rewrite_parents = 1;
1298 revs->simplify_history = 0;
1299 revs->simplify_by_decoration = 1;
1300 revs->limited = 1;
1301 revs->prune = 1;
1302 load_ref_decorations(DECORATE_SHORT_REFS);
1303 } else if (!strcmp(arg, "--date-order")) {
1304 revs->lifo = 0;
1305 revs->topo_order = 1;
1306 } else if (!prefixcmp(arg, "--early-output")) {
1307 int count = 100;
1308 switch (arg[14]) {
1309 case '=':
1310 count = atoi(arg+15);
1311 /* Fallthrough */
1312 case 0:
1313 revs->topo_order = 1;
1314 revs->early_output = count;
1316 } else if (!strcmp(arg, "--parents")) {
1317 revs->rewrite_parents = 1;
1318 revs->print_parents = 1;
1319 } else if (!strcmp(arg, "--dense")) {
1320 revs->dense = 1;
1321 } else if (!strcmp(arg, "--sparse")) {
1322 revs->dense = 0;
1323 } else if (!strcmp(arg, "--show-all")) {
1324 revs->show_all = 1;
1325 } else if (!strcmp(arg, "--remove-empty")) {
1326 revs->remove_empty_trees = 1;
1327 } else if (!strcmp(arg, "--merges")) {
1328 revs->min_parents = 2;
1329 } else if (!strcmp(arg, "--no-merges")) {
1330 revs->max_parents = 1;
1331 } else if (!prefixcmp(arg, "--min-parents=")) {
1332 revs->min_parents = atoi(arg+14);
1333 } else if (!prefixcmp(arg, "--no-min-parents")) {
1334 revs->min_parents = 0;
1335 } else if (!prefixcmp(arg, "--max-parents=")) {
1336 revs->max_parents = atoi(arg+14);
1337 } else if (!prefixcmp(arg, "--no-max-parents")) {
1338 revs->max_parents = -1;
1339 } else if (!strcmp(arg, "--boundary")) {
1340 revs->boundary = 1;
1341 } else if (!strcmp(arg, "--left-right")) {
1342 revs->left_right = 1;
1343 } else if (!strcmp(arg, "--left-only")) {
1344 if (revs->right_only)
1345 die("--left-only is incompatible with --right-only"
1346 " or --cherry");
1347 revs->left_only = 1;
1348 } else if (!strcmp(arg, "--right-only")) {
1349 if (revs->left_only)
1350 die("--right-only is incompatible with --left-only");
1351 revs->right_only = 1;
1352 } else if (!strcmp(arg, "--cherry")) {
1353 if (revs->left_only)
1354 die("--cherry is incompatible with --left-only");
1355 revs->cherry_mark = 1;
1356 revs->right_only = 1;
1357 revs->max_parents = 1;
1358 revs->limited = 1;
1359 } else if (!strcmp(arg, "--count")) {
1360 revs->count = 1;
1361 } else if (!strcmp(arg, "--cherry-mark")) {
1362 if (revs->cherry_pick)
1363 die("--cherry-mark is incompatible with --cherry-pick");
1364 revs->cherry_mark = 1;
1365 revs->limited = 1; /* needs limit_list() */
1366 } else if (!strcmp(arg, "--cherry-pick")) {
1367 if (revs->cherry_mark)
1368 die("--cherry-pick is incompatible with --cherry-mark");
1369 revs->cherry_pick = 1;
1370 revs->limited = 1;
1371 } else if (!strcmp(arg, "--objects")) {
1372 revs->tag_objects = 1;
1373 revs->tree_objects = 1;
1374 revs->blob_objects = 1;
1375 } else if (!strcmp(arg, "--objects-edge")) {
1376 revs->tag_objects = 1;
1377 revs->tree_objects = 1;
1378 revs->blob_objects = 1;
1379 revs->edge_hint = 1;
1380 } else if (!strcmp(arg, "--unpacked")) {
1381 revs->unpacked = 1;
1382 } else if (!prefixcmp(arg, "--unpacked=")) {
1383 die("--unpacked=<packfile> no longer supported.");
1384 } else if (!strcmp(arg, "-r")) {
1385 revs->diff = 1;
1386 DIFF_OPT_SET(&revs->diffopt, RECURSIVE);
1387 } else if (!strcmp(arg, "-t")) {
1388 revs->diff = 1;
1389 DIFF_OPT_SET(&revs->diffopt, RECURSIVE);
1390 DIFF_OPT_SET(&revs->diffopt, TREE_IN_RECURSIVE);
1391 } else if (!strcmp(arg, "-m")) {
1392 revs->ignore_merges = 0;
1393 } else if (!strcmp(arg, "-c")) {
1394 revs->diff = 1;
1395 revs->dense_combined_merges = 0;
1396 revs->combine_merges = 1;
1397 } else if (!strcmp(arg, "--cc")) {
1398 revs->diff = 1;
1399 revs->dense_combined_merges = 1;
1400 revs->combine_merges = 1;
1401 } else if (!strcmp(arg, "-v")) {
1402 revs->verbose_header = 1;
1403 } else if (!strcmp(arg, "--pretty")) {
1404 revs->verbose_header = 1;
1405 revs->pretty_given = 1;
1406 get_commit_format(arg+8, revs);
1407 } else if (!prefixcmp(arg, "--pretty=") || !prefixcmp(arg, "--format=")) {
1409 * Detached form ("--pretty X" as opposed to "--pretty=X")
1410 * not allowed, since the argument is optional.
1412 revs->verbose_header = 1;
1413 revs->pretty_given = 1;
1414 get_commit_format(arg+9, revs);
1415 } else if (!strcmp(arg, "--show-notes") || !strcmp(arg, "--notes")) {
1416 revs->show_notes = 1;
1417 revs->show_notes_given = 1;
1418 revs->notes_opt.use_default_notes = 1;
1419 } else if (!prefixcmp(arg, "--show-notes=") ||
1420 !prefixcmp(arg, "--notes=")) {
1421 struct strbuf buf = STRBUF_INIT;
1422 revs->show_notes = 1;
1423 revs->show_notes_given = 1;
1424 if (!prefixcmp(arg, "--show-notes")) {
1425 if (revs->notes_opt.use_default_notes < 0)
1426 revs->notes_opt.use_default_notes = 1;
1427 strbuf_addstr(&buf, arg+13);
1429 else
1430 strbuf_addstr(&buf, arg+8);
1431 expand_notes_ref(&buf);
1432 string_list_append(&revs->notes_opt.extra_notes_refs,
1433 strbuf_detach(&buf, NULL));
1434 } else if (!strcmp(arg, "--no-notes")) {
1435 revs->show_notes = 0;
1436 revs->show_notes_given = 1;
1437 revs->notes_opt.use_default_notes = -1;
1438 /* we have been strdup'ing ourselves, so trick
1439 * string_list into free()ing strings */
1440 revs->notes_opt.extra_notes_refs.strdup_strings = 1;
1441 string_list_clear(&revs->notes_opt.extra_notes_refs, 0);
1442 revs->notes_opt.extra_notes_refs.strdup_strings = 0;
1443 } else if (!strcmp(arg, "--standard-notes")) {
1444 revs->show_notes_given = 1;
1445 revs->notes_opt.use_default_notes = 1;
1446 } else if (!strcmp(arg, "--no-standard-notes")) {
1447 revs->notes_opt.use_default_notes = 0;
1448 } else if (!strcmp(arg, "--oneline")) {
1449 revs->verbose_header = 1;
1450 get_commit_format("oneline", revs);
1451 revs->pretty_given = 1;
1452 revs->abbrev_commit = 1;
1453 } else if (!strcmp(arg, "--graph")) {
1454 revs->topo_order = 1;
1455 revs->rewrite_parents = 1;
1456 revs->graph = graph_init(revs);
1457 } else if (!strcmp(arg, "--root")) {
1458 revs->show_root_diff = 1;
1459 } else if (!strcmp(arg, "--no-commit-id")) {
1460 revs->no_commit_id = 1;
1461 } else if (!strcmp(arg, "--always")) {
1462 revs->always_show_header = 1;
1463 } else if (!strcmp(arg, "--no-abbrev")) {
1464 revs->abbrev = 0;
1465 } else if (!strcmp(arg, "--abbrev")) {
1466 revs->abbrev = DEFAULT_ABBREV;
1467 } else if (!prefixcmp(arg, "--abbrev=")) {
1468 revs->abbrev = strtoul(arg + 9, NULL, 10);
1469 if (revs->abbrev < MINIMUM_ABBREV)
1470 revs->abbrev = MINIMUM_ABBREV;
1471 else if (revs->abbrev > 40)
1472 revs->abbrev = 40;
1473 } else if (!strcmp(arg, "--abbrev-commit")) {
1474 revs->abbrev_commit = 1;
1475 revs->abbrev_commit_given = 1;
1476 } else if (!strcmp(arg, "--no-abbrev-commit")) {
1477 revs->abbrev_commit = 0;
1478 } else if (!strcmp(arg, "--full-diff")) {
1479 revs->diff = 1;
1480 revs->full_diff = 1;
1481 } else if (!strcmp(arg, "--full-history")) {
1482 revs->simplify_history = 0;
1483 } else if (!strcmp(arg, "--relative-date")) {
1484 revs->date_mode = DATE_RELATIVE;
1485 revs->date_mode_explicit = 1;
1486 } else if ((argcount = parse_long_opt("date", argv, &optarg))) {
1487 revs->date_mode = parse_date_format(optarg);
1488 revs->date_mode_explicit = 1;
1489 return argcount;
1490 } else if (!strcmp(arg, "--log-size")) {
1491 revs->show_log_size = 1;
1494 * Grepping the commit log
1496 else if ((argcount = parse_long_opt("author", argv, &optarg))) {
1497 add_header_grep(revs, GREP_HEADER_AUTHOR, optarg);
1498 return argcount;
1499 } else if ((argcount = parse_long_opt("committer", argv, &optarg))) {
1500 add_header_grep(revs, GREP_HEADER_COMMITTER, optarg);
1501 return argcount;
1502 } else if ((argcount = parse_long_opt("grep", argv, &optarg))) {
1503 add_message_grep(revs, optarg);
1504 return argcount;
1505 } else if (!strcmp(arg, "--extended-regexp") || !strcmp(arg, "-E")) {
1506 revs->grep_filter.regflags |= REG_EXTENDED;
1507 } else if (!strcmp(arg, "--regexp-ignore-case") || !strcmp(arg, "-i")) {
1508 revs->grep_filter.regflags |= REG_ICASE;
1509 } else if (!strcmp(arg, "--fixed-strings") || !strcmp(arg, "-F")) {
1510 revs->grep_filter.fixed = 1;
1511 } else if (!strcmp(arg, "--all-match")) {
1512 revs->grep_filter.all_match = 1;
1513 } else if ((argcount = parse_long_opt("encoding", argv, &optarg))) {
1514 if (strcmp(optarg, "none"))
1515 git_log_output_encoding = xstrdup(optarg);
1516 else
1517 git_log_output_encoding = "";
1518 return argcount;
1519 } else if (!strcmp(arg, "--reverse")) {
1520 revs->reverse ^= 1;
1521 } else if (!strcmp(arg, "--children")) {
1522 revs->children.name = "children";
1523 revs->limited = 1;
1524 } else if (!strcmp(arg, "--ignore-missing")) {
1525 revs->ignore_missing = 1;
1526 } else {
1527 int opts = diff_opt_parse(&revs->diffopt, argv, argc);
1528 if (!opts)
1529 unkv[(*unkc)++] = arg;
1530 return opts;
1533 return 1;
1536 void parse_revision_opt(struct rev_info *revs, struct parse_opt_ctx_t *ctx,
1537 const struct option *options,
1538 const char * const usagestr[])
1540 int n = handle_revision_opt(revs, ctx->argc, ctx->argv,
1541 &ctx->cpidx, ctx->out);
1542 if (n <= 0) {
1543 error("unknown option `%s'", ctx->argv[0]);
1544 usage_with_options(usagestr, options);
1546 ctx->argv += n;
1547 ctx->argc -= n;
1550 static int for_each_bad_bisect_ref(const char *submodule, each_ref_fn fn, void *cb_data)
1552 return for_each_ref_in_submodule(submodule, "refs/bisect/bad", fn, cb_data);
1555 static int for_each_good_bisect_ref(const char *submodule, each_ref_fn fn, void *cb_data)
1557 return for_each_ref_in_submodule(submodule, "refs/bisect/good", fn, cb_data);
1560 static int handle_revision_pseudo_opt(const char *submodule,
1561 struct rev_info *revs,
1562 int argc, const char **argv, int *flags)
1564 const char *arg = argv[0];
1565 const char *optarg;
1566 int argcount;
1569 * NOTE!
1571 * Commands like "git shortlog" will not accept the options below
1572 * unless parse_revision_opt queues them (as opposed to erroring
1573 * out).
1575 * When implementing your new pseudo-option, remember to
1576 * register it in the list at the top of handle_revision_opt.
1578 if (!strcmp(arg, "--all")) {
1579 handle_refs(submodule, revs, *flags, for_each_ref_submodule);
1580 handle_refs(submodule, revs, *flags, head_ref_submodule);
1581 } else if (!strcmp(arg, "--branches")) {
1582 handle_refs(submodule, revs, *flags, for_each_branch_ref_submodule);
1583 } else if (!strcmp(arg, "--bisect")) {
1584 handle_refs(submodule, revs, *flags, for_each_bad_bisect_ref);
1585 handle_refs(submodule, revs, *flags ^ UNINTERESTING, for_each_good_bisect_ref);
1586 revs->bisect = 1;
1587 } else if (!strcmp(arg, "--tags")) {
1588 handle_refs(submodule, revs, *flags, for_each_tag_ref_submodule);
1589 } else if (!strcmp(arg, "--remotes")) {
1590 handle_refs(submodule, revs, *flags, for_each_remote_ref_submodule);
1591 } else if ((argcount = parse_long_opt("glob", argv, &optarg))) {
1592 struct all_refs_cb cb;
1593 init_all_refs_cb(&cb, revs, *flags);
1594 for_each_glob_ref(handle_one_ref, optarg, &cb);
1595 return argcount;
1596 } else if (!prefixcmp(arg, "--branches=")) {
1597 struct all_refs_cb cb;
1598 init_all_refs_cb(&cb, revs, *flags);
1599 for_each_glob_ref_in(handle_one_ref, arg + 11, "refs/heads/", &cb);
1600 } else if (!prefixcmp(arg, "--tags=")) {
1601 struct all_refs_cb cb;
1602 init_all_refs_cb(&cb, revs, *flags);
1603 for_each_glob_ref_in(handle_one_ref, arg + 7, "refs/tags/", &cb);
1604 } else if (!prefixcmp(arg, "--remotes=")) {
1605 struct all_refs_cb cb;
1606 init_all_refs_cb(&cb, revs, *flags);
1607 for_each_glob_ref_in(handle_one_ref, arg + 10, "refs/remotes/", &cb);
1608 } else if (!strcmp(arg, "--reflog")) {
1609 handle_reflog(revs, *flags);
1610 } else if (!strcmp(arg, "--not")) {
1611 *flags ^= UNINTERESTING;
1612 } else if (!strcmp(arg, "--no-walk")) {
1613 revs->no_walk = 1;
1614 } else if (!strcmp(arg, "--do-walk")) {
1615 revs->no_walk = 0;
1616 } else {
1617 return 0;
1620 return 1;
1624 * Parse revision information, filling in the "rev_info" structure,
1625 * and removing the used arguments from the argument list.
1627 * Returns the number of arguments left that weren't recognized
1628 * (which are also moved to the head of the argument list)
1630 int setup_revisions(int argc, const char **argv, struct rev_info *revs, struct setup_revision_opt *opt)
1632 int i, flags, left, seen_dashdash, read_from_stdin, got_rev_arg = 0;
1633 struct cmdline_pathspec prune_data;
1634 const char *submodule = NULL;
1636 memset(&prune_data, 0, sizeof(prune_data));
1637 if (opt)
1638 submodule = opt->submodule;
1640 /* First, search for "--" */
1641 seen_dashdash = 0;
1642 for (i = 1; i < argc; i++) {
1643 const char *arg = argv[i];
1644 if (strcmp(arg, "--"))
1645 continue;
1646 argv[i] = NULL;
1647 argc = i;
1648 if (argv[i + 1])
1649 append_prune_data(&prune_data, argv + i + 1);
1650 seen_dashdash = 1;
1651 break;
1654 /* Second, deal with arguments and options */
1655 flags = 0;
1656 read_from_stdin = 0;
1657 for (left = i = 1; i < argc; i++) {
1658 const char *arg = argv[i];
1659 if (*arg == '-') {
1660 int opts;
1662 opts = handle_revision_pseudo_opt(submodule,
1663 revs, argc - i, argv + i,
1664 &flags);
1665 if (opts > 0) {
1666 i += opts - 1;
1667 continue;
1670 if (!strcmp(arg, "--stdin")) {
1671 if (revs->disable_stdin) {
1672 argv[left++] = arg;
1673 continue;
1675 if (read_from_stdin++)
1676 die("--stdin given twice?");
1677 read_revisions_from_stdin(revs, &prune_data);
1678 continue;
1681 opts = handle_revision_opt(revs, argc - i, argv + i, &left, argv);
1682 if (opts > 0) {
1683 i += opts - 1;
1684 continue;
1686 if (opts < 0)
1687 exit(128);
1688 continue;
1691 if (handle_revision_arg(arg, revs, flags, seen_dashdash)) {
1692 int j;
1693 if (seen_dashdash || *arg == '^')
1694 die("bad revision '%s'", arg);
1696 /* If we didn't have a "--":
1697 * (1) all filenames must exist;
1698 * (2) all rev-args must not be interpretable
1699 * as a valid filename.
1700 * but the latter we have checked in the main loop.
1702 for (j = i; j < argc; j++)
1703 verify_filename(revs->prefix, argv[j]);
1705 append_prune_data(&prune_data, argv + i);
1706 break;
1708 else
1709 got_rev_arg = 1;
1712 if (prune_data.nr) {
1714 * If we need to introduce the magic "a lone ':' means no
1715 * pathspec whatsoever", here is the place to do so.
1717 * if (prune_data.nr == 1 && !strcmp(prune_data[0], ":")) {
1718 * prune_data.nr = 0;
1719 * prune_data.alloc = 0;
1720 * free(prune_data.path);
1721 * prune_data.path = NULL;
1722 * } else {
1723 * terminate prune_data.alloc with NULL and
1724 * call init_pathspec() to set revs->prune_data here.
1727 ALLOC_GROW(prune_data.path, prune_data.nr+1, prune_data.alloc);
1728 prune_data.path[prune_data.nr++] = NULL;
1729 init_pathspec(&revs->prune_data,
1730 get_pathspec(revs->prefix, prune_data.path));
1733 if (revs->def == NULL)
1734 revs->def = opt ? opt->def : NULL;
1735 if (opt && opt->tweak)
1736 opt->tweak(revs, opt);
1737 if (revs->show_merge)
1738 prepare_show_merge(revs);
1739 if (revs->def && !revs->pending.nr && !got_rev_arg) {
1740 unsigned char sha1[20];
1741 struct object *object;
1742 unsigned mode;
1743 if (get_sha1_with_mode(revs->def, sha1, &mode))
1744 die("bad default revision '%s'", revs->def);
1745 object = get_reference(revs, revs->def, sha1, 0);
1746 add_pending_object_with_mode(revs, object, revs->def, mode);
1749 /* Did the user ask for any diff output? Run the diff! */
1750 if (revs->diffopt.output_format & ~DIFF_FORMAT_NO_OUTPUT)
1751 revs->diff = 1;
1753 /* Pickaxe, diff-filter and rename following need diffs */
1754 if (revs->diffopt.pickaxe ||
1755 revs->diffopt.filter ||
1756 DIFF_OPT_TST(&revs->diffopt, FOLLOW_RENAMES))
1757 revs->diff = 1;
1759 if (revs->topo_order)
1760 revs->limited = 1;
1762 if (revs->prune_data.nr) {
1763 diff_tree_setup_paths(revs->prune_data.raw, &revs->pruning);
1764 /* Can't prune commits with rename following: the paths change.. */
1765 if (!DIFF_OPT_TST(&revs->diffopt, FOLLOW_RENAMES))
1766 revs->prune = 1;
1767 if (!revs->full_diff)
1768 diff_tree_setup_paths(revs->prune_data.raw, &revs->diffopt);
1770 if (revs->combine_merges)
1771 revs->ignore_merges = 0;
1772 revs->diffopt.abbrev = revs->abbrev;
1773 if (diff_setup_done(&revs->diffopt) < 0)
1774 die("diff_setup_done failed");
1776 compile_grep_patterns(&revs->grep_filter);
1778 if (revs->reverse && revs->reflog_info)
1779 die("cannot combine --reverse with --walk-reflogs");
1780 if (revs->rewrite_parents && revs->children.name)
1781 die("cannot combine --parents and --children");
1784 * Limitations on the graph functionality
1786 if (revs->reverse && revs->graph)
1787 die("cannot combine --reverse with --graph");
1789 if (revs->reflog_info && revs->graph)
1790 die("cannot combine --walk-reflogs with --graph");
1792 return left;
1795 static void add_child(struct rev_info *revs, struct commit *parent, struct commit *child)
1797 struct commit_list *l = xcalloc(1, sizeof(*l));
1799 l->item = child;
1800 l->next = add_decoration(&revs->children, &parent->object, l);
1803 static int remove_duplicate_parents(struct commit *commit)
1805 struct commit_list **pp, *p;
1806 int surviving_parents;
1808 /* Examine existing parents while marking ones we have seen... */
1809 pp = &commit->parents;
1810 while ((p = *pp) != NULL) {
1811 struct commit *parent = p->item;
1812 if (parent->object.flags & TMP_MARK) {
1813 *pp = p->next;
1814 continue;
1816 parent->object.flags |= TMP_MARK;
1817 pp = &p->next;
1819 /* count them while clearing the temporary mark */
1820 surviving_parents = 0;
1821 for (p = commit->parents; p; p = p->next) {
1822 p->item->object.flags &= ~TMP_MARK;
1823 surviving_parents++;
1825 return surviving_parents;
1828 struct merge_simplify_state {
1829 struct commit *simplified;
1832 static struct merge_simplify_state *locate_simplify_state(struct rev_info *revs, struct commit *commit)
1834 struct merge_simplify_state *st;
1836 st = lookup_decoration(&revs->merge_simplification, &commit->object);
1837 if (!st) {
1838 st = xcalloc(1, sizeof(*st));
1839 add_decoration(&revs->merge_simplification, &commit->object, st);
1841 return st;
1844 static struct commit_list **simplify_one(struct rev_info *revs, struct commit *commit, struct commit_list **tail)
1846 struct commit_list *p;
1847 struct merge_simplify_state *st, *pst;
1848 int cnt;
1850 st = locate_simplify_state(revs, commit);
1853 * Have we handled this one?
1855 if (st->simplified)
1856 return tail;
1859 * An UNINTERESTING commit simplifies to itself, so does a
1860 * root commit. We do not rewrite parents of such commit
1861 * anyway.
1863 if ((commit->object.flags & UNINTERESTING) || !commit->parents) {
1864 st->simplified = commit;
1865 return tail;
1869 * Do we know what commit all of our parents should be rewritten to?
1870 * Otherwise we are not ready to rewrite this one yet.
1872 for (cnt = 0, p = commit->parents; p; p = p->next) {
1873 pst = locate_simplify_state(revs, p->item);
1874 if (!pst->simplified) {
1875 tail = &commit_list_insert(p->item, tail)->next;
1876 cnt++;
1879 if (cnt) {
1880 tail = &commit_list_insert(commit, tail)->next;
1881 return tail;
1885 * Rewrite our list of parents.
1887 for (p = commit->parents; p; p = p->next) {
1888 pst = locate_simplify_state(revs, p->item);
1889 p->item = pst->simplified;
1891 cnt = remove_duplicate_parents(commit);
1894 * It is possible that we are a merge and one side branch
1895 * does not have any commit that touches the given paths;
1896 * in such a case, the immediate parents will be rewritten
1897 * to different commits.
1899 * o----X X: the commit we are looking at;
1900 * / / o: a commit that touches the paths;
1901 * ---o----'
1903 * Further reduce the parents by removing redundant parents.
1905 if (1 < cnt) {
1906 struct commit_list *h = reduce_heads(commit->parents);
1907 cnt = commit_list_count(h);
1908 free_commit_list(commit->parents);
1909 commit->parents = h;
1913 * A commit simplifies to itself if it is a root, if it is
1914 * UNINTERESTING, if it touches the given paths, or if it is a
1915 * merge and its parents simplifies to more than one commits
1916 * (the first two cases are already handled at the beginning of
1917 * this function).
1919 * Otherwise, it simplifies to what its sole parent simplifies to.
1921 if (!cnt ||
1922 (commit->object.flags & UNINTERESTING) ||
1923 !(commit->object.flags & TREESAME) ||
1924 (1 < cnt))
1925 st->simplified = commit;
1926 else {
1927 pst = locate_simplify_state(revs, commit->parents->item);
1928 st->simplified = pst->simplified;
1930 return tail;
1933 static void simplify_merges(struct rev_info *revs)
1935 struct commit_list *list;
1936 struct commit_list *yet_to_do, **tail;
1938 if (!revs->topo_order)
1939 sort_in_topological_order(&revs->commits, revs->lifo);
1940 if (!revs->prune)
1941 return;
1943 /* feed the list reversed */
1944 yet_to_do = NULL;
1945 for (list = revs->commits; list; list = list->next)
1946 commit_list_insert(list->item, &yet_to_do);
1947 while (yet_to_do) {
1948 list = yet_to_do;
1949 yet_to_do = NULL;
1950 tail = &yet_to_do;
1951 while (list) {
1952 struct commit *commit = list->item;
1953 struct commit_list *next = list->next;
1954 free(list);
1955 list = next;
1956 tail = simplify_one(revs, commit, tail);
1960 /* clean up the result, removing the simplified ones */
1961 list = revs->commits;
1962 revs->commits = NULL;
1963 tail = &revs->commits;
1964 while (list) {
1965 struct commit *commit = list->item;
1966 struct commit_list *next = list->next;
1967 struct merge_simplify_state *st;
1968 free(list);
1969 list = next;
1970 st = locate_simplify_state(revs, commit);
1971 if (st->simplified == commit)
1972 tail = &commit_list_insert(commit, tail)->next;
1976 static void set_children(struct rev_info *revs)
1978 struct commit_list *l;
1979 for (l = revs->commits; l; l = l->next) {
1980 struct commit *commit = l->item;
1981 struct commit_list *p;
1983 for (p = commit->parents; p; p = p->next)
1984 add_child(revs, p->item, commit);
1988 int prepare_revision_walk(struct rev_info *revs)
1990 int nr = revs->pending.nr;
1991 struct object_array_entry *e, *list;
1993 e = list = revs->pending.objects;
1994 revs->pending.nr = 0;
1995 revs->pending.alloc = 0;
1996 revs->pending.objects = NULL;
1997 while (--nr >= 0) {
1998 struct commit *commit = handle_commit(revs, e->item, e->name);
1999 if (commit) {
2000 if (!(commit->object.flags & SEEN)) {
2001 commit->object.flags |= SEEN;
2002 commit_list_insert_by_date(commit, &revs->commits);
2005 e++;
2007 free(list);
2009 if (revs->no_walk)
2010 return 0;
2011 if (revs->limited)
2012 if (limit_list(revs) < 0)
2013 return -1;
2014 if (revs->topo_order)
2015 sort_in_topological_order(&revs->commits, revs->lifo);
2016 if (revs->simplify_merges)
2017 simplify_merges(revs);
2018 if (revs->children.name)
2019 set_children(revs);
2020 return 0;
2023 enum rewrite_result {
2024 rewrite_one_ok,
2025 rewrite_one_noparents,
2026 rewrite_one_error
2029 static enum rewrite_result rewrite_one(struct rev_info *revs, struct commit **pp)
2031 struct commit_list *cache = NULL;
2033 for (;;) {
2034 struct commit *p = *pp;
2035 if (!revs->limited)
2036 if (add_parents_to_list(revs, p, &revs->commits, &cache) < 0)
2037 return rewrite_one_error;
2038 if (p->parents && p->parents->next)
2039 return rewrite_one_ok;
2040 if (p->object.flags & UNINTERESTING)
2041 return rewrite_one_ok;
2042 if (!(p->object.flags & TREESAME))
2043 return rewrite_one_ok;
2044 if (!p->parents)
2045 return rewrite_one_noparents;
2046 *pp = p->parents->item;
2050 static int rewrite_parents(struct rev_info *revs, struct commit *commit)
2052 struct commit_list **pp = &commit->parents;
2053 while (*pp) {
2054 struct commit_list *parent = *pp;
2055 switch (rewrite_one(revs, &parent->item)) {
2056 case rewrite_one_ok:
2057 break;
2058 case rewrite_one_noparents:
2059 *pp = parent->next;
2060 continue;
2061 case rewrite_one_error:
2062 return -1;
2064 pp = &parent->next;
2066 remove_duplicate_parents(commit);
2067 return 0;
2070 static int commit_match(struct commit *commit, struct rev_info *opt)
2072 if (!opt->grep_filter.pattern_list && !opt->grep_filter.header_list)
2073 return 1;
2074 return grep_buffer(&opt->grep_filter,
2075 NULL, /* we say nothing, not even filename */
2076 commit->buffer, strlen(commit->buffer));
2079 static inline int want_ancestry(struct rev_info *revs)
2081 return (revs->rewrite_parents || revs->children.name);
2084 enum commit_action get_commit_action(struct rev_info *revs, struct commit *commit)
2086 if (commit->object.flags & SHOWN)
2087 return commit_ignore;
2088 if (revs->unpacked && has_sha1_pack(commit->object.sha1))
2089 return commit_ignore;
2090 if (revs->show_all)
2091 return commit_show;
2092 if (commit->object.flags & UNINTERESTING)
2093 return commit_ignore;
2094 if (revs->min_age != -1 && (commit->date > revs->min_age))
2095 return commit_ignore;
2096 if (revs->min_parents || (revs->max_parents >= 0)) {
2097 int n = 0;
2098 struct commit_list *p;
2099 for (p = commit->parents; p; p = p->next)
2100 n++;
2101 if ((n < revs->min_parents) ||
2102 ((revs->max_parents >= 0) && (n > revs->max_parents)))
2103 return commit_ignore;
2105 if (!commit_match(commit, revs))
2106 return commit_ignore;
2107 if (revs->prune && revs->dense) {
2108 /* Commit without changes? */
2109 if (commit->object.flags & TREESAME) {
2110 /* drop merges unless we want parenthood */
2111 if (!want_ancestry(revs))
2112 return commit_ignore;
2113 /* non-merge - always ignore it */
2114 if (!commit->parents || !commit->parents->next)
2115 return commit_ignore;
2118 return commit_show;
2121 enum commit_action simplify_commit(struct rev_info *revs, struct commit *commit)
2123 enum commit_action action = get_commit_action(revs, commit);
2125 if (action == commit_show &&
2126 !revs->show_all &&
2127 revs->prune && revs->dense && want_ancestry(revs)) {
2128 if (rewrite_parents(revs, commit) < 0)
2129 return commit_error;
2131 return action;
2134 static struct commit *get_revision_1(struct rev_info *revs)
2136 if (!revs->commits)
2137 return NULL;
2139 do {
2140 struct commit_list *entry = revs->commits;
2141 struct commit *commit = entry->item;
2143 revs->commits = entry->next;
2144 free(entry);
2146 if (revs->reflog_info) {
2147 fake_reflog_parent(revs->reflog_info, commit);
2148 commit->object.flags &= ~(ADDED | SEEN | SHOWN);
2152 * If we haven't done the list limiting, we need to look at
2153 * the parents here. We also need to do the date-based limiting
2154 * that we'd otherwise have done in limit_list().
2156 if (!revs->limited) {
2157 if (revs->max_age != -1 &&
2158 (commit->date < revs->max_age))
2159 continue;
2160 if (add_parents_to_list(revs, commit, &revs->commits, NULL) < 0)
2161 die("Failed to traverse parents of commit %s",
2162 sha1_to_hex(commit->object.sha1));
2165 switch (simplify_commit(revs, commit)) {
2166 case commit_ignore:
2167 continue;
2168 case commit_error:
2169 die("Failed to simplify parents of commit %s",
2170 sha1_to_hex(commit->object.sha1));
2171 default:
2172 return commit;
2174 } while (revs->commits);
2175 return NULL;
2178 static void gc_boundary(struct object_array *array)
2180 unsigned nr = array->nr;
2181 unsigned alloc = array->alloc;
2182 struct object_array_entry *objects = array->objects;
2184 if (alloc <= nr) {
2185 unsigned i, j;
2186 for (i = j = 0; i < nr; i++) {
2187 if (objects[i].item->flags & SHOWN)
2188 continue;
2189 if (i != j)
2190 objects[j] = objects[i];
2191 j++;
2193 for (i = j; i < nr; i++)
2194 objects[i].item = NULL;
2195 array->nr = j;
2199 static void create_boundary_commit_list(struct rev_info *revs)
2201 unsigned i;
2202 struct commit *c;
2203 struct object_array *array = &revs->boundary_commits;
2204 struct object_array_entry *objects = array->objects;
2207 * If revs->commits is non-NULL at this point, an error occurred in
2208 * get_revision_1(). Ignore the error and continue printing the
2209 * boundary commits anyway. (This is what the code has always
2210 * done.)
2212 if (revs->commits) {
2213 free_commit_list(revs->commits);
2214 revs->commits = NULL;
2218 * Put all of the actual boundary commits from revs->boundary_commits
2219 * into revs->commits
2221 for (i = 0; i < array->nr; i++) {
2222 c = (struct commit *)(objects[i].item);
2223 if (!c)
2224 continue;
2225 if (!(c->object.flags & CHILD_SHOWN))
2226 continue;
2227 if (c->object.flags & (SHOWN | BOUNDARY))
2228 continue;
2229 c->object.flags |= BOUNDARY;
2230 commit_list_insert(c, &revs->commits);
2234 * If revs->topo_order is set, sort the boundary commits
2235 * in topological order
2237 sort_in_topological_order(&revs->commits, revs->lifo);
2240 static struct commit *get_revision_internal(struct rev_info *revs)
2242 struct commit *c = NULL;
2243 struct commit_list *l;
2245 if (revs->boundary == 2) {
2247 * All of the normal commits have already been returned,
2248 * and we are now returning boundary commits.
2249 * create_boundary_commit_list() has populated
2250 * revs->commits with the remaining commits to return.
2252 c = pop_commit(&revs->commits);
2253 if (c)
2254 c->object.flags |= SHOWN;
2255 return c;
2259 * Now pick up what they want to give us
2261 c = get_revision_1(revs);
2262 if (c) {
2263 while (0 < revs->skip_count) {
2264 revs->skip_count--;
2265 c = get_revision_1(revs);
2266 if (!c)
2267 break;
2272 * Check the max_count.
2274 switch (revs->max_count) {
2275 case -1:
2276 break;
2277 case 0:
2278 c = NULL;
2279 break;
2280 default:
2281 revs->max_count--;
2284 if (c)
2285 c->object.flags |= SHOWN;
2287 if (!revs->boundary) {
2288 return c;
2291 if (!c) {
2293 * get_revision_1() runs out the commits, and
2294 * we are done computing the boundaries.
2295 * switch to boundary commits output mode.
2297 revs->boundary = 2;
2300 * Update revs->commits to contain the list of
2301 * boundary commits.
2303 create_boundary_commit_list(revs);
2305 return get_revision_internal(revs);
2309 * boundary commits are the commits that are parents of the
2310 * ones we got from get_revision_1() but they themselves are
2311 * not returned from get_revision_1(). Before returning
2312 * 'c', we need to mark its parents that they could be boundaries.
2315 for (l = c->parents; l; l = l->next) {
2316 struct object *p;
2317 p = &(l->item->object);
2318 if (p->flags & (CHILD_SHOWN | SHOWN))
2319 continue;
2320 p->flags |= CHILD_SHOWN;
2321 gc_boundary(&revs->boundary_commits);
2322 add_object_array(p, NULL, &revs->boundary_commits);
2325 return c;
2328 struct commit *get_revision(struct rev_info *revs)
2330 struct commit *c;
2331 struct commit_list *reversed;
2333 if (revs->reverse) {
2334 reversed = NULL;
2335 while ((c = get_revision_internal(revs))) {
2336 commit_list_insert(c, &reversed);
2338 revs->commits = reversed;
2339 revs->reverse = 0;
2340 revs->reverse_output_stage = 1;
2343 if (revs->reverse_output_stage)
2344 return pop_commit(&revs->commits);
2346 c = get_revision_internal(revs);
2347 if (c && revs->graph)
2348 graph_update(revs->graph, c);
2349 return c;
2352 char *get_revision_mark(const struct rev_info *revs, const struct commit *commit)
2354 if (commit->object.flags & BOUNDARY)
2355 return "-";
2356 else if (commit->object.flags & UNINTERESTING)
2357 return "^";
2358 else if (commit->object.flags & PATCHSAME)
2359 return "=";
2360 else if (!revs || revs->left_right) {
2361 if (commit->object.flags & SYMMETRIC_LEFT)
2362 return "<";
2363 else
2364 return ">";
2365 } else if (revs->graph)
2366 return "*";
2367 else if (revs->cherry_mark)
2368 return "+";
2369 return "";
2372 void put_revision_mark(const struct rev_info *revs, const struct commit *commit)
2374 char *mark = get_revision_mark(revs, commit);
2375 if (!strlen(mark))
2376 return;
2377 fputs(mark, stdout);
2378 putchar(' ');