Allow using UNC path for git repository
[git/mingw/4msysgit.git] / revision.c
blob37a411819687fdbfc433c79dc9db034fb45f28d1
1 #include "cache.h"
2 #include "tag.h"
3 #include "blob.h"
4 #include "tree.h"
5 #include "commit.h"
6 #include "diff.h"
7 #include "refs.h"
8 #include "revision.h"
9 #include "graph.h"
10 #include "grep.h"
11 #include "reflog-walk.h"
12 #include "patch-ids.h"
13 #include "decorate.h"
14 #include "log-tree.h"
15 #include "string-list.h"
16 #include "line.h"
18 volatile show_early_output_fn_t show_early_output;
20 char *path_name(const struct name_path *path, const char *name)
22 const struct name_path *p;
23 char *n, *m;
24 int nlen = strlen(name);
25 int len = nlen + 1;
27 for (p = path; p; p = p->up) {
28 if (p->elem_len)
29 len += p->elem_len + 1;
31 n = xmalloc(len);
32 m = n + len - (nlen + 1);
33 strcpy(m, name);
34 for (p = path; p; p = p->up) {
35 if (p->elem_len) {
36 m -= p->elem_len + 1;
37 memcpy(m, p->elem, p->elem_len);
38 m[p->elem_len] = '/';
41 return n;
44 void add_object(struct object *obj,
45 struct object_array *p,
46 struct name_path *path,
47 const char *name)
49 add_object_array(obj, path_name(path, name), p);
52 static void mark_blob_uninteresting(struct blob *blob)
54 if (!blob)
55 return;
56 if (blob->object.flags & UNINTERESTING)
57 return;
58 blob->object.flags |= UNINTERESTING;
61 void mark_tree_uninteresting(struct tree *tree)
63 struct tree_desc desc;
64 struct name_entry entry;
65 struct object *obj = &tree->object;
67 if (!tree)
68 return;
69 if (obj->flags & UNINTERESTING)
70 return;
71 obj->flags |= UNINTERESTING;
72 if (!has_sha1_file(obj->sha1))
73 return;
74 if (parse_tree(tree) < 0)
75 die("bad tree %s", sha1_to_hex(obj->sha1));
77 init_tree_desc(&desc, tree->buffer, tree->size);
78 while (tree_entry(&desc, &entry)) {
79 switch (object_type(entry.mode)) {
80 case OBJ_TREE:
81 mark_tree_uninteresting(lookup_tree(entry.sha1));
82 break;
83 case OBJ_BLOB:
84 mark_blob_uninteresting(lookup_blob(entry.sha1));
85 break;
86 default:
87 /* Subproject commit - not in this repository */
88 break;
93 * We don't care about the tree any more
94 * after it has been marked uninteresting.
96 free(tree->buffer);
97 tree->buffer = NULL;
100 void mark_parents_uninteresting(struct commit *commit)
102 struct commit_list *parents = commit->parents;
104 while (parents) {
105 struct commit *commit = parents->item;
106 if (!(commit->object.flags & UNINTERESTING)) {
107 commit->object.flags |= UNINTERESTING;
110 * Normally we haven't parsed the parent
111 * yet, so we won't have a parent of a parent
112 * here. However, it may turn out that we've
113 * reached this commit some other way (where it
114 * wasn't uninteresting), in which case we need
115 * to mark its parents recursively too..
117 if (commit->parents)
118 mark_parents_uninteresting(commit);
122 * A missing commit is ok iff its parent is marked
123 * uninteresting.
125 * We just mark such a thing parsed, so that when
126 * it is popped next time around, we won't be trying
127 * to parse it and get an error.
129 if (!has_sha1_file(commit->object.sha1))
130 commit->object.parsed = 1;
131 parents = parents->next;
135 static void add_pending_object_with_mode(struct rev_info *revs, struct object *obj, const char *name, unsigned mode)
137 if (revs->no_walk && (obj->flags & UNINTERESTING))
138 revs->no_walk = 0;
139 if (revs->reflog_info && obj->type == OBJ_COMMIT) {
140 struct strbuf buf = STRBUF_INIT;
141 int len = interpret_branch_name(name, &buf);
142 int st;
144 if (0 < len && name[len] && buf.len)
145 strbuf_addstr(&buf, name + len);
146 st = add_reflog_for_walk(revs->reflog_info,
147 (struct commit *)obj,
148 buf.buf[0] ? buf.buf: name);
149 strbuf_release(&buf);
150 if (st)
151 return;
153 add_object_array_with_mode(obj, name, &revs->pending, mode);
156 void add_pending_object(struct rev_info *revs, struct object *obj, const char *name)
158 add_pending_object_with_mode(revs, obj, name, S_IFINVALID);
161 void add_head_to_pending(struct rev_info *revs)
163 unsigned char sha1[20];
164 struct object *obj;
165 if (get_sha1("HEAD", sha1))
166 return;
167 obj = parse_object(sha1);
168 if (!obj)
169 return;
170 add_pending_object(revs, obj, "HEAD");
173 static struct object *get_reference(struct rev_info *revs, const char *name, const unsigned char *sha1, unsigned int flags)
175 struct object *object;
177 object = parse_object(sha1);
178 if (!object)
179 die("bad object %s", name);
180 object->flags |= flags;
181 return object;
184 static struct commit *handle_commit(struct rev_info *revs, struct object *object, const char *name)
186 unsigned long flags = object->flags;
189 * Tag object? Look what it points to..
191 while (object->type == OBJ_TAG) {
192 struct tag *tag = (struct tag *) object;
193 if (revs->tag_objects && !(flags & UNINTERESTING))
194 add_pending_object(revs, object, tag->tag);
195 if (!tag->tagged)
196 die("bad tag");
197 object = parse_object(tag->tagged->sha1);
198 if (!object) {
199 if (flags & UNINTERESTING)
200 return NULL;
201 die("bad object %s", sha1_to_hex(tag->tagged->sha1));
206 * Commit object? Just return it, we'll do all the complex
207 * reachability crud.
209 if (object->type == OBJ_COMMIT) {
210 struct commit *commit = (struct commit *)object;
211 if (parse_commit(commit) < 0)
212 die("unable to parse commit %s", name);
213 if (flags & UNINTERESTING) {
214 commit->object.flags |= UNINTERESTING;
215 mark_parents_uninteresting(commit);
216 revs->limited = 1;
218 if (revs->show_source && !commit->util)
219 commit->util = (void *) name;
220 return commit;
224 * Tree object? Either mark it uninteresting, or add it
225 * to the list of objects to look at later..
227 if (object->type == OBJ_TREE) {
228 struct tree *tree = (struct tree *)object;
229 if (!revs->tree_objects)
230 return NULL;
231 if (flags & UNINTERESTING) {
232 mark_tree_uninteresting(tree);
233 return NULL;
235 add_pending_object(revs, object, "");
236 return NULL;
240 * Blob object? You know the drill by now..
242 if (object->type == OBJ_BLOB) {
243 struct blob *blob = (struct blob *)object;
244 if (!revs->blob_objects)
245 return NULL;
246 if (flags & UNINTERESTING) {
247 mark_blob_uninteresting(blob);
248 return NULL;
250 add_pending_object(revs, object, "");
251 return NULL;
253 die("%s is unknown object", name);
256 static int everybody_uninteresting(struct commit_list *orig)
258 struct commit_list *list = orig;
259 while (list) {
260 struct commit *commit = list->item;
261 list = list->next;
262 if (commit->object.flags & UNINTERESTING)
263 continue;
264 return 0;
266 return 1;
270 * The goal is to get REV_TREE_NEW as the result only if the
271 * diff consists of all '+' (and no other changes), REV_TREE_OLD
272 * if the whole diff is removal of old data, and otherwise
273 * REV_TREE_DIFFERENT (of course if the trees are the same we
274 * want REV_TREE_SAME).
275 * That means that once we get to REV_TREE_DIFFERENT, we do not
276 * have to look any further.
278 static int tree_difference = REV_TREE_SAME;
280 static void file_add_remove(struct diff_options *options,
281 int addremove, unsigned mode,
282 const unsigned char *sha1,
283 const char *fullpath, unsigned dirty_submodule)
285 int diff = addremove == '+' ? REV_TREE_NEW : REV_TREE_OLD;
287 tree_difference |= diff;
288 if (tree_difference == REV_TREE_DIFFERENT)
289 DIFF_OPT_SET(options, HAS_CHANGES);
292 static void file_change(struct diff_options *options,
293 unsigned old_mode, unsigned new_mode,
294 const unsigned char *old_sha1,
295 const unsigned char *new_sha1,
296 const char *fullpath,
297 unsigned old_dirty_submodule, unsigned new_dirty_submodule)
299 tree_difference = REV_TREE_DIFFERENT;
300 DIFF_OPT_SET(options, HAS_CHANGES);
303 static int rev_compare_tree(struct rev_info *revs, struct commit *parent, struct commit *commit)
305 struct tree *t1 = parent->tree;
306 struct tree *t2 = commit->tree;
308 if (!t1)
309 return REV_TREE_NEW;
310 if (!t2)
311 return REV_TREE_OLD;
313 if (revs->simplify_by_decoration) {
315 * If we are simplifying by decoration, then the commit
316 * is worth showing if it has a tag pointing at it.
318 if (lookup_decoration(&name_decoration, &commit->object))
319 return REV_TREE_DIFFERENT;
321 * A commit that is not pointed by a tag is uninteresting
322 * if we are not limited by path. This means that you will
323 * see the usual "commits that touch the paths" plus any
324 * tagged commit by specifying both --simplify-by-decoration
325 * and pathspec.
327 if (!revs->prune_data)
328 return REV_TREE_SAME;
331 tree_difference = REV_TREE_SAME;
332 DIFF_OPT_CLR(&revs->pruning, HAS_CHANGES);
333 if (diff_tree_sha1(t1->object.sha1, t2->object.sha1, "",
334 &revs->pruning) < 0)
335 return REV_TREE_DIFFERENT;
336 return tree_difference;
339 static int rev_same_tree_as_empty(struct rev_info *revs, struct commit *commit)
341 int retval;
342 void *tree;
343 unsigned long size;
344 struct tree_desc empty, real;
345 struct tree *t1 = commit->tree;
347 if (!t1)
348 return 0;
350 tree = read_object_with_reference(t1->object.sha1, tree_type, &size, NULL);
351 if (!tree)
352 return 0;
353 init_tree_desc(&real, tree, size);
354 init_tree_desc(&empty, "", 0);
356 tree_difference = REV_TREE_SAME;
357 DIFF_OPT_CLR(&revs->pruning, HAS_CHANGES);
358 retval = diff_tree(&empty, &real, "", &revs->pruning);
359 free(tree);
361 return retval >= 0 && (tree_difference == REV_TREE_SAME);
364 static void try_to_simplify_commit(struct rev_info *revs, struct commit *commit)
366 struct commit_list **pp, *parent;
367 int tree_changed = 0, tree_same = 0;
370 * If we don't do pruning, everything is interesting
372 if (!revs->prune)
373 return;
375 if (!commit->tree)
376 return;
378 if (!commit->parents) {
379 if (rev_same_tree_as_empty(revs, commit))
380 commit->object.flags |= TREESAME;
381 return;
385 * Normal non-merge commit? If we don't want to make the
386 * history dense, we consider it always to be a change..
388 if (!revs->dense && !commit->parents->next)
389 return;
391 pp = &commit->parents;
392 while ((parent = *pp) != NULL) {
393 struct commit *p = parent->item;
395 if (parse_commit(p) < 0)
396 die("cannot simplify commit %s (because of %s)",
397 sha1_to_hex(commit->object.sha1),
398 sha1_to_hex(p->object.sha1));
399 switch (rev_compare_tree(revs, p, commit)) {
400 case REV_TREE_SAME:
401 tree_same = 1;
402 if (!revs->simplify_history || (p->object.flags & UNINTERESTING)) {
403 /* Even if a merge with an uninteresting
404 * side branch brought the entire change
405 * we are interested in, we do not want
406 * to lose the other branches of this
407 * merge, so we just keep going.
409 pp = &parent->next;
410 continue;
412 parent->next = NULL;
413 commit->parents = parent;
414 commit->object.flags |= TREESAME;
415 return;
417 case REV_TREE_NEW:
418 if (revs->remove_empty_trees &&
419 rev_same_tree_as_empty(revs, p)) {
420 /* We are adding all the specified
421 * paths from this parent, so the
422 * history beyond this parent is not
423 * interesting. Remove its parents
424 * (they are grandparents for us).
425 * IOW, we pretend this parent is a
426 * "root" commit.
428 if (parse_commit(p) < 0)
429 die("cannot simplify commit %s (invalid %s)",
430 sha1_to_hex(commit->object.sha1),
431 sha1_to_hex(p->object.sha1));
432 p->parents = NULL;
434 /* fallthrough */
435 case REV_TREE_OLD:
436 case REV_TREE_DIFFERENT:
437 tree_changed = 1;
438 pp = &parent->next;
439 continue;
441 die("bad tree compare for commit %s", sha1_to_hex(commit->object.sha1));
443 if (tree_changed && !tree_same)
444 return;
445 commit->object.flags |= TREESAME;
448 static void insert_by_date_cached(struct commit *p, struct commit_list **head,
449 struct commit_list *cached_base, struct commit_list **cache)
451 struct commit_list *new_entry;
453 if (cached_base && p->date < cached_base->item->date)
454 new_entry = insert_by_date(p, &cached_base->next);
455 else
456 new_entry = insert_by_date(p, head);
458 if (cache && (!*cache || p->date < (*cache)->item->date))
459 *cache = new_entry;
462 static int add_parents_to_list(struct rev_info *revs, struct commit *commit,
463 struct commit_list **list, struct commit_list **cache_ptr)
465 struct commit_list *parent = commit->parents;
466 unsigned left_flag;
467 struct commit_list *cached_base = cache_ptr ? *cache_ptr : NULL;
469 if (commit->object.flags & ADDED)
470 return 0;
471 commit->object.flags |= ADDED;
474 * If the commit is uninteresting, don't try to
475 * prune parents - we want the maximal uninteresting
476 * set.
478 * Normally we haven't parsed the parent
479 * yet, so we won't have a parent of a parent
480 * here. However, it may turn out that we've
481 * reached this commit some other way (where it
482 * wasn't uninteresting), in which case we need
483 * to mark its parents recursively too..
485 if (commit->object.flags & UNINTERESTING) {
486 while (parent) {
487 struct commit *p = parent->item;
488 parent = parent->next;
489 if (p)
490 p->object.flags |= UNINTERESTING;
491 if (parse_commit(p) < 0)
492 continue;
493 if (p->parents)
494 mark_parents_uninteresting(p);
495 if (p->object.flags & SEEN)
496 continue;
497 p->object.flags |= SEEN;
498 insert_by_date_cached(p, list, cached_base, cache_ptr);
500 return 0;
504 * Ok, the commit wasn't uninteresting. Try to
505 * simplify the commit history and find the parent
506 * that has no differences in the path set if one exists.
508 try_to_simplify_commit(revs, commit);
510 if (revs->no_walk)
511 return 0;
513 left_flag = (commit->object.flags & SYMMETRIC_LEFT);
515 for (parent = commit->parents; parent; parent = parent->next) {
516 struct commit *p = parent->item;
518 if (parse_commit(p) < 0)
519 return -1;
520 if (revs->show_source && !p->util)
521 p->util = commit->util;
522 p->object.flags |= left_flag;
523 if (!(p->object.flags & SEEN)) {
524 p->object.flags |= SEEN;
525 insert_by_date_cached(p, list, cached_base, cache_ptr);
527 if (revs->first_parent_only)
528 break;
530 return 0;
533 static void cherry_pick_list(struct commit_list *list, struct rev_info *revs)
535 struct commit_list *p;
536 int left_count = 0, right_count = 0;
537 int left_first;
538 struct patch_ids ids;
540 /* First count the commits on the left and on the right */
541 for (p = list; p; p = p->next) {
542 struct commit *commit = p->item;
543 unsigned flags = commit->object.flags;
544 if (flags & BOUNDARY)
546 else if (flags & SYMMETRIC_LEFT)
547 left_count++;
548 else
549 right_count++;
552 if (!left_count || !right_count)
553 return;
555 left_first = left_count < right_count;
556 init_patch_ids(&ids);
557 if (revs->diffopt.nr_paths) {
558 ids.diffopts.nr_paths = revs->diffopt.nr_paths;
559 ids.diffopts.paths = revs->diffopt.paths;
560 ids.diffopts.pathlens = revs->diffopt.pathlens;
563 /* Compute patch-ids for one side */
564 for (p = list; p; p = p->next) {
565 struct commit *commit = p->item;
566 unsigned flags = commit->object.flags;
568 if (flags & BOUNDARY)
569 continue;
571 * If we have fewer left, left_first is set and we omit
572 * commits on the right branch in this loop. If we have
573 * fewer right, we skip the left ones.
575 if (left_first != !!(flags & SYMMETRIC_LEFT))
576 continue;
577 commit->util = add_commit_patch_id(commit, &ids);
580 /* Check the other side */
581 for (p = list; p; p = p->next) {
582 struct commit *commit = p->item;
583 struct patch_id *id;
584 unsigned flags = commit->object.flags;
586 if (flags & BOUNDARY)
587 continue;
589 * If we have fewer left, left_first is set and we omit
590 * commits on the left branch in this loop.
592 if (left_first == !!(flags & SYMMETRIC_LEFT))
593 continue;
596 * Have we seen the same patch id?
598 id = has_commit_patch_id(commit, &ids);
599 if (!id)
600 continue;
601 id->seen = 1;
602 commit->object.flags |= SHOWN;
605 /* Now check the original side for seen ones */
606 for (p = list; p; p = p->next) {
607 struct commit *commit = p->item;
608 struct patch_id *ent;
610 ent = commit->util;
611 if (!ent)
612 continue;
613 if (ent->seen)
614 commit->object.flags |= SHOWN;
615 commit->util = NULL;
618 free_patch_ids(&ids);
621 /* How many extra uninteresting commits we want to see.. */
622 #define SLOP 5
624 static int still_interesting(struct commit_list *src, unsigned long date, int slop)
627 * No source list at all? We're definitely done..
629 if (!src)
630 return 0;
633 * Does the destination list contain entries with a date
634 * before the source list? Definitely _not_ done.
636 if (date < src->item->date)
637 return SLOP;
640 * Does the source list still have interesting commits in
641 * it? Definitely not done..
643 if (!everybody_uninteresting(src))
644 return SLOP;
646 /* Ok, we're closing in.. */
647 return slop-1;
651 * "rev-list --ancestry-path A..B" computes commits that are ancestors
652 * of B but not ancestors of A but further limits the result to those
653 * that are descendants of A. This takes the list of bottom commits and
654 * the result of "A..B" without --ancestry-path, and limits the latter
655 * further to the ones that can reach one of the commits in "bottom".
657 static void limit_to_ancestry(struct commit_list *bottom, struct commit_list *list)
659 struct commit_list *p;
660 struct commit_list *rlist = NULL;
661 int made_progress;
664 * Reverse the list so that it will be likely that we would
665 * process parents before children.
667 for (p = list; p; p = p->next)
668 commit_list_insert(p->item, &rlist);
670 for (p = bottom; p; p = p->next)
671 p->item->object.flags |= TMP_MARK;
674 * Mark the ones that can reach bottom commits in "list",
675 * in a bottom-up fashion.
677 do {
678 made_progress = 0;
679 for (p = rlist; p; p = p->next) {
680 struct commit *c = p->item;
681 struct commit_list *parents;
682 if (c->object.flags & (TMP_MARK | UNINTERESTING))
683 continue;
684 for (parents = c->parents;
685 parents;
686 parents = parents->next) {
687 if (!(parents->item->object.flags & TMP_MARK))
688 continue;
689 c->object.flags |= TMP_MARK;
690 made_progress = 1;
691 break;
694 } while (made_progress);
697 * NEEDSWORK: decide if we want to remove parents that are
698 * not marked with TMP_MARK from commit->parents for commits
699 * in the resulting list. We may not want to do that, though.
703 * The ones that are not marked with TMP_MARK are uninteresting
705 for (p = list; p; p = p->next) {
706 struct commit *c = p->item;
707 if (c->object.flags & TMP_MARK)
708 continue;
709 c->object.flags |= UNINTERESTING;
712 /* We are done with the TMP_MARK */
713 for (p = list; p; p = p->next)
714 p->item->object.flags &= ~TMP_MARK;
715 for (p = bottom; p; p = p->next)
716 p->item->object.flags &= ~TMP_MARK;
717 free_commit_list(rlist);
721 * Before walking the history, keep the set of "negative" refs the
722 * caller has asked to exclude.
724 * This is used to compute "rev-list --ancestry-path A..B", as we need
725 * to filter the result of "A..B" further to the ones that can actually
726 * reach A.
728 static struct commit_list *collect_bottom_commits(struct commit_list *list)
730 struct commit_list *elem, *bottom = NULL;
731 for (elem = list; elem; elem = elem->next)
732 if (elem->item->object.flags & UNINTERESTING)
733 commit_list_insert(elem->item, &bottom);
734 return bottom;
737 static int limit_list(struct rev_info *revs)
739 int slop = SLOP;
740 unsigned long date = ~0ul;
741 struct commit_list *list = revs->commits;
742 struct commit_list *newlist = NULL;
743 struct commit_list **p = &newlist;
744 struct commit_list *bottom = NULL;
746 if (revs->ancestry_path) {
747 bottom = collect_bottom_commits(list);
748 if (!bottom)
749 die("--ancestry-path given but there are no bottom commits");
752 while (list) {
753 struct commit_list *entry = list;
754 struct commit *commit = list->item;
755 struct object *obj = &commit->object;
756 show_early_output_fn_t show;
758 list = list->next;
759 free(entry);
761 if (revs->max_age != -1 && (commit->date < revs->max_age))
762 obj->flags |= UNINTERESTING;
763 if (add_parents_to_list(revs, commit, &list, NULL) < 0)
764 return -1;
765 if (obj->flags & UNINTERESTING) {
766 mark_parents_uninteresting(commit);
767 if (revs->show_all)
768 p = &commit_list_insert(commit, p)->next;
769 slop = still_interesting(list, date, slop);
770 if (slop)
771 continue;
772 /* If showing all, add the whole pending list to the end */
773 if (revs->show_all)
774 *p = list;
775 break;
777 if (revs->min_age != -1 && (commit->date > revs->min_age))
778 continue;
779 date = commit->date;
780 p = &commit_list_insert(commit, p)->next;
782 show = show_early_output;
783 if (!show)
784 continue;
786 show(revs, newlist);
787 show_early_output = NULL;
789 if (revs->cherry_pick)
790 cherry_pick_list(newlist, revs);
792 if (bottom) {
793 limit_to_ancestry(bottom, newlist);
794 free_commit_list(bottom);
797 revs->commits = newlist;
798 return 0;
801 struct all_refs_cb {
802 int all_flags;
803 int warned_bad_reflog;
804 struct rev_info *all_revs;
805 const char *name_for_errormsg;
808 static int handle_one_ref(const char *path, const unsigned char *sha1, int flag, void *cb_data)
810 struct all_refs_cb *cb = cb_data;
811 struct object *object = get_reference(cb->all_revs, path, sha1,
812 cb->all_flags);
813 add_pending_object(cb->all_revs, object, path);
814 return 0;
817 static void init_all_refs_cb(struct all_refs_cb *cb, struct rev_info *revs,
818 unsigned flags)
820 cb->all_revs = revs;
821 cb->all_flags = flags;
824 static void handle_refs(const char *submodule, struct rev_info *revs, unsigned flags,
825 int (*for_each)(const char *, each_ref_fn, void *))
827 struct all_refs_cb cb;
828 init_all_refs_cb(&cb, revs, flags);
829 for_each(submodule, handle_one_ref, &cb);
832 static void handle_one_reflog_commit(unsigned char *sha1, void *cb_data)
834 struct all_refs_cb *cb = cb_data;
835 if (!is_null_sha1(sha1)) {
836 struct object *o = parse_object(sha1);
837 if (o) {
838 o->flags |= cb->all_flags;
839 add_pending_object(cb->all_revs, o, "");
841 else if (!cb->warned_bad_reflog) {
842 warning("reflog of '%s' references pruned commits",
843 cb->name_for_errormsg);
844 cb->warned_bad_reflog = 1;
849 static int handle_one_reflog_ent(unsigned char *osha1, unsigned char *nsha1,
850 const char *email, unsigned long timestamp, int tz,
851 const char *message, void *cb_data)
853 handle_one_reflog_commit(osha1, cb_data);
854 handle_one_reflog_commit(nsha1, cb_data);
855 return 0;
858 static int handle_one_reflog(const char *path, const unsigned char *sha1, int flag, void *cb_data)
860 struct all_refs_cb *cb = cb_data;
861 cb->warned_bad_reflog = 0;
862 cb->name_for_errormsg = path;
863 for_each_reflog_ent(path, handle_one_reflog_ent, cb_data);
864 return 0;
867 static void handle_reflog(struct rev_info *revs, unsigned flags)
869 struct all_refs_cb cb;
870 cb.all_revs = revs;
871 cb.all_flags = flags;
872 for_each_reflog(handle_one_reflog, &cb);
875 static int add_parents_only(struct rev_info *revs, const char *arg, int flags)
877 unsigned char sha1[20];
878 struct object *it;
879 struct commit *commit;
880 struct commit_list *parents;
882 if (*arg == '^') {
883 flags ^= UNINTERESTING;
884 arg++;
886 if (get_sha1(arg, sha1))
887 return 0;
888 while (1) {
889 it = get_reference(revs, arg, sha1, 0);
890 if (it->type != OBJ_TAG)
891 break;
892 if (!((struct tag*)it)->tagged)
893 return 0;
894 hashcpy(sha1, ((struct tag*)it)->tagged->sha1);
896 if (it->type != OBJ_COMMIT)
897 return 0;
898 commit = (struct commit *)it;
899 for (parents = commit->parents; parents; parents = parents->next) {
900 it = &parents->item->object;
901 it->flags |= flags;
902 add_pending_object(revs, it, arg);
904 return 1;
907 void init_revisions(struct rev_info *revs, const char *prefix)
909 memset(revs, 0, sizeof(*revs));
911 revs->abbrev = DEFAULT_ABBREV;
912 revs->ignore_merges = 1;
913 revs->simplify_history = 1;
914 DIFF_OPT_SET(&revs->pruning, RECURSIVE);
915 DIFF_OPT_SET(&revs->pruning, QUICK);
916 revs->pruning.add_remove = file_add_remove;
917 revs->pruning.change = file_change;
918 revs->lifo = 1;
919 revs->dense = 1;
920 revs->prefix = prefix;
921 revs->max_age = -1;
922 revs->min_age = -1;
923 revs->skip_count = -1;
924 revs->max_count = -1;
926 revs->commit_format = CMIT_FMT_DEFAULT;
928 revs->grep_filter.status_only = 1;
929 revs->grep_filter.pattern_tail = &(revs->grep_filter.pattern_list);
930 revs->grep_filter.header_tail = &(revs->grep_filter.header_list);
931 revs->grep_filter.regflags = REG_NEWLINE;
933 diff_setup(&revs->diffopt);
934 if (prefix && !revs->diffopt.prefix) {
935 revs->diffopt.prefix = prefix;
936 revs->diffopt.prefix_length = strlen(prefix);
940 static void add_pending_commit_list(struct rev_info *revs,
941 struct commit_list *commit_list,
942 unsigned int flags)
944 while (commit_list) {
945 struct object *object = &commit_list->item->object;
946 object->flags |= flags;
947 add_pending_object(revs, object, sha1_to_hex(object->sha1));
948 commit_list = commit_list->next;
952 static void prepare_show_merge(struct rev_info *revs)
954 struct commit_list *bases;
955 struct commit *head, *other;
956 unsigned char sha1[20];
957 const char **prune = NULL;
958 int i, prune_num = 1; /* counting terminating NULL */
960 if (get_sha1("HEAD", sha1) || !(head = lookup_commit(sha1)))
961 die("--merge without HEAD?");
962 if (get_sha1("MERGE_HEAD", sha1) || !(other = lookup_commit(sha1)))
963 die("--merge without MERGE_HEAD?");
964 add_pending_object(revs, &head->object, "HEAD");
965 add_pending_object(revs, &other->object, "MERGE_HEAD");
966 bases = get_merge_bases(head, other, 1);
967 add_pending_commit_list(revs, bases, UNINTERESTING);
968 free_commit_list(bases);
969 head->object.flags |= SYMMETRIC_LEFT;
971 if (!active_nr)
972 read_cache();
973 for (i = 0; i < active_nr; i++) {
974 struct cache_entry *ce = active_cache[i];
975 if (!ce_stage(ce))
976 continue;
977 if (ce_path_match(ce, revs->prune_data)) {
978 prune_num++;
979 prune = xrealloc(prune, sizeof(*prune) * prune_num);
980 prune[prune_num-2] = ce->name;
981 prune[prune_num-1] = NULL;
983 while ((i+1 < active_nr) &&
984 ce_same_name(ce, active_cache[i+1]))
985 i++;
987 revs->prune_data = prune;
988 revs->limited = 1;
991 int handle_revision_arg(const char *arg, struct rev_info *revs,
992 int flags,
993 int cant_be_filename)
995 unsigned mode;
996 char *dotdot;
997 struct object *object;
998 unsigned char sha1[20];
999 int local_flags;
1001 dotdot = strstr(arg, "..");
1002 if (dotdot) {
1003 unsigned char from_sha1[20];
1004 const char *next = dotdot + 2;
1005 const char *this = arg;
1006 int symmetric = *next == '.';
1007 unsigned int flags_exclude = flags ^ UNINTERESTING;
1009 *dotdot = 0;
1010 next += symmetric;
1012 if (!*next)
1013 next = "HEAD";
1014 if (dotdot == arg)
1015 this = "HEAD";
1016 if (!get_sha1(this, from_sha1) &&
1017 !get_sha1(next, sha1)) {
1018 struct commit *a, *b;
1019 struct commit_list *exclude;
1021 a = lookup_commit_reference(from_sha1);
1022 b = lookup_commit_reference(sha1);
1023 if (!a || !b) {
1024 die(symmetric ?
1025 "Invalid symmetric difference expression %s...%s" :
1026 "Invalid revision range %s..%s",
1027 arg, next);
1030 if (!cant_be_filename) {
1031 *dotdot = '.';
1032 verify_non_filename(revs->prefix, arg);
1035 if (symmetric) {
1036 exclude = get_merge_bases(a, b, 1);
1037 add_pending_commit_list(revs, exclude,
1038 flags_exclude);
1039 free_commit_list(exclude);
1040 a->object.flags |= flags | SYMMETRIC_LEFT;
1041 } else
1042 a->object.flags |= flags_exclude;
1043 b->object.flags |= flags;
1044 add_pending_object(revs, &a->object, this);
1045 add_pending_object(revs, &b->object, next);
1046 return 0;
1048 *dotdot = '.';
1050 dotdot = strstr(arg, "^@");
1051 if (dotdot && !dotdot[2]) {
1052 *dotdot = 0;
1053 if (add_parents_only(revs, arg, flags))
1054 return 0;
1055 *dotdot = '^';
1057 dotdot = strstr(arg, "^!");
1058 if (dotdot && !dotdot[2]) {
1059 *dotdot = 0;
1060 if (!add_parents_only(revs, arg, flags ^ UNINTERESTING))
1061 *dotdot = '^';
1064 local_flags = 0;
1065 if (*arg == '^') {
1066 local_flags = UNINTERESTING;
1067 arg++;
1069 if (get_sha1_with_mode(arg, sha1, &mode))
1070 return -1;
1071 if (!cant_be_filename)
1072 verify_non_filename(revs->prefix, arg);
1073 object = get_reference(revs, arg, sha1, flags ^ local_flags);
1074 add_pending_object_with_mode(revs, object, arg, mode);
1075 return 0;
1078 static void read_pathspec_from_stdin(struct rev_info *revs, struct strbuf *sb, const char ***prune_data)
1080 const char **prune = *prune_data;
1081 int prune_nr;
1082 int prune_alloc;
1084 /* count existing ones */
1085 if (!prune)
1086 prune_nr = 0;
1087 else
1088 for (prune_nr = 0; prune[prune_nr]; prune_nr++)
1090 prune_alloc = prune_nr; /* not really, but we do not know */
1092 while (strbuf_getwholeline(sb, stdin, '\n') != EOF) {
1093 int len = sb->len;
1094 if (len && sb->buf[len - 1] == '\n')
1095 sb->buf[--len] = '\0';
1096 ALLOC_GROW(prune, prune_nr+1, prune_alloc);
1097 prune[prune_nr++] = xstrdup(sb->buf);
1099 if (prune) {
1100 ALLOC_GROW(prune, prune_nr+1, prune_alloc);
1101 prune[prune_nr] = NULL;
1103 *prune_data = prune;
1106 static void read_revisions_from_stdin(struct rev_info *revs, const char ***prune)
1108 struct strbuf sb;
1109 int seen_dashdash = 0;
1111 strbuf_init(&sb, 1000);
1112 while (strbuf_getwholeline(&sb, stdin, '\n') != EOF) {
1113 int len = sb.len;
1114 if (len && sb.buf[len - 1] == '\n')
1115 sb.buf[--len] = '\0';
1116 if (!len)
1117 break;
1118 if (sb.buf[0] == '-') {
1119 if (len == 2 && sb.buf[1] == '-') {
1120 seen_dashdash = 1;
1121 break;
1123 die("options not supported in --stdin mode");
1125 if (handle_revision_arg(sb.buf, revs, 0, 1))
1126 die("bad revision '%s'", sb.buf);
1128 if (seen_dashdash)
1129 read_pathspec_from_stdin(revs, &sb, prune);
1130 strbuf_release(&sb);
1133 static void add_grep(struct rev_info *revs, const char *ptn, enum grep_pat_token what)
1135 append_grep_pattern(&revs->grep_filter, ptn, "command line", 0, what);
1138 static void add_header_grep(struct rev_info *revs, enum grep_header_field field, const char *pattern)
1140 append_header_grep_pattern(&revs->grep_filter, field, pattern);
1143 static void add_message_grep(struct rev_info *revs, const char *pattern)
1145 add_grep(revs, pattern, GREP_PATTERN_BODY);
1148 static int handle_revision_opt(struct rev_info *revs, int argc, const char **argv,
1149 int *unkc, const char **unkv)
1151 const char *arg = argv[0];
1152 const char *optarg;
1153 int argcount;
1155 /* pseudo revision arguments */
1156 if (!strcmp(arg, "--all") || !strcmp(arg, "--branches") ||
1157 !strcmp(arg, "--tags") || !strcmp(arg, "--remotes") ||
1158 !strcmp(arg, "--reflog") || !strcmp(arg, "--not") ||
1159 !strcmp(arg, "--no-walk") || !strcmp(arg, "--do-walk") ||
1160 !strcmp(arg, "--bisect"))
1162 unkv[(*unkc)++] = arg;
1163 return 1;
1166 if ((argcount = parse_long_opt("max-count", argv, &optarg))) {
1167 revs->max_count = atoi(optarg);
1168 revs->no_walk = 0;
1169 return argcount;
1170 } else if ((argcount = parse_long_opt("skip", argv, &optarg))) {
1171 revs->skip_count = atoi(optarg);
1172 return argcount;
1173 } else if ((*arg == '-') && isdigit(arg[1])) {
1174 /* accept -<digit>, like traditional "head" */
1175 revs->max_count = atoi(arg + 1);
1176 revs->no_walk = 0;
1177 } else if (!strcmp(arg, "-n")) {
1178 if (argc <= 1)
1179 return error("-n requires an argument");
1180 revs->max_count = atoi(argv[1]);
1181 revs->no_walk = 0;
1182 return 2;
1183 } else if (!prefixcmp(arg, "-n")) {
1184 revs->max_count = atoi(arg + 2);
1185 revs->no_walk = 0;
1186 } else if ((argcount = parse_long_opt("max-age", argv, &optarg))) {
1187 revs->max_age = atoi(optarg);
1188 return argcount;
1189 } else if ((argcount = parse_long_opt("since", argv, &optarg))) {
1190 revs->max_age = approxidate(optarg);
1191 return argcount;
1192 } else if ((argcount = parse_long_opt("after", argv, &optarg))) {
1193 revs->max_age = approxidate(optarg);
1194 return argcount;
1195 } else if ((argcount = parse_long_opt("min-age", argv, &optarg))) {
1196 revs->min_age = atoi(optarg);
1197 return argcount;
1198 } else if ((argcount = parse_long_opt("before", argv, &optarg))) {
1199 revs->min_age = approxidate(optarg);
1200 return argcount;
1201 } else if ((argcount = parse_long_opt("until", argv, &optarg))) {
1202 revs->min_age = approxidate(optarg);
1203 return argcount;
1204 } else if (!strcmp(arg, "--first-parent")) {
1205 revs->first_parent_only = 1;
1206 } else if (!strcmp(arg, "--ancestry-path")) {
1207 revs->ancestry_path = 1;
1208 revs->simplify_history = 0;
1209 revs->limited = 1;
1210 } else if (!strcmp(arg, "-g") || !strcmp(arg, "--walk-reflogs")) {
1211 init_reflog_walk(&revs->reflog_info);
1212 } else if (!strcmp(arg, "--default")) {
1213 if (argc <= 1)
1214 return error("bad --default argument");
1215 revs->def = argv[1];
1216 return 2;
1217 } else if (!strcmp(arg, "--merge")) {
1218 revs->show_merge = 1;
1219 } else if (!strcmp(arg, "--topo-order")) {
1220 revs->lifo = 1;
1221 revs->topo_order = 1;
1222 } else if (!strcmp(arg, "--simplify-merges")) {
1223 revs->simplify_merges = 1;
1224 revs->rewrite_parents = 1;
1225 revs->simplify_history = 0;
1226 revs->limited = 1;
1227 } else if (!strcmp(arg, "--simplify-by-decoration")) {
1228 revs->simplify_merges = 1;
1229 revs->rewrite_parents = 1;
1230 revs->simplify_history = 0;
1231 revs->simplify_by_decoration = 1;
1232 revs->limited = 1;
1233 revs->prune = 1;
1234 load_ref_decorations(DECORATE_SHORT_REFS);
1235 } else if (!strcmp(arg, "--date-order")) {
1236 revs->lifo = 0;
1237 revs->topo_order = 1;
1238 } else if (!prefixcmp(arg, "--early-output")) {
1239 int count = 100;
1240 switch (arg[14]) {
1241 case '=':
1242 count = atoi(arg+15);
1243 /* Fallthrough */
1244 case 0:
1245 revs->topo_order = 1;
1246 revs->early_output = count;
1248 } else if (!strcmp(arg, "--parents")) {
1249 revs->rewrite_parents = 1;
1250 revs->print_parents = 1;
1251 } else if (!strcmp(arg, "--dense")) {
1252 revs->dense = 1;
1253 } else if (!strcmp(arg, "--sparse")) {
1254 revs->dense = 0;
1255 } else if (!strcmp(arg, "--show-all")) {
1256 revs->show_all = 1;
1257 } else if (!strcmp(arg, "--remove-empty")) {
1258 revs->remove_empty_trees = 1;
1259 } else if (!strcmp(arg, "--merges")) {
1260 revs->merges_only = 1;
1261 } else if (!strcmp(arg, "--no-merges")) {
1262 revs->no_merges = 1;
1263 } else if (!strcmp(arg, "--boundary")) {
1264 revs->boundary = 1;
1265 } else if (!strcmp(arg, "--left-right")) {
1266 revs->left_right = 1;
1267 } else if (!strcmp(arg, "--count")) {
1268 revs->count = 1;
1269 } else if (!strcmp(arg, "--cherry-pick")) {
1270 revs->cherry_pick = 1;
1271 revs->limited = 1;
1272 } else if (!strcmp(arg, "--objects")) {
1273 revs->tag_objects = 1;
1274 revs->tree_objects = 1;
1275 revs->blob_objects = 1;
1276 } else if (!strcmp(arg, "--objects-edge")) {
1277 revs->tag_objects = 1;
1278 revs->tree_objects = 1;
1279 revs->blob_objects = 1;
1280 revs->edge_hint = 1;
1281 } else if (!strcmp(arg, "--unpacked")) {
1282 revs->unpacked = 1;
1283 } else if (!prefixcmp(arg, "--unpacked=")) {
1284 die("--unpacked=<packfile> no longer supported.");
1285 } else if (!strcmp(arg, "-r")) {
1286 revs->diff = 1;
1287 DIFF_OPT_SET(&revs->diffopt, RECURSIVE);
1288 } else if (!strcmp(arg, "-t")) {
1289 revs->diff = 1;
1290 DIFF_OPT_SET(&revs->diffopt, RECURSIVE);
1291 DIFF_OPT_SET(&revs->diffopt, TREE_IN_RECURSIVE);
1292 } else if (!strcmp(arg, "-m")) {
1293 revs->ignore_merges = 0;
1294 } else if (!strcmp(arg, "-c")) {
1295 revs->diff = 1;
1296 revs->dense_combined_merges = 0;
1297 revs->combine_merges = 1;
1298 } else if (!strcmp(arg, "--cc")) {
1299 revs->diff = 1;
1300 revs->dense_combined_merges = 1;
1301 revs->combine_merges = 1;
1302 } else if (!strcmp(arg, "-v")) {
1303 revs->verbose_header = 1;
1304 } else if (!strcmp(arg, "--pretty")) {
1305 revs->verbose_header = 1;
1306 revs->pretty_given = 1;
1307 get_commit_format(arg+8, revs);
1308 } else if (!prefixcmp(arg, "--pretty=") || !prefixcmp(arg, "--format=")) {
1310 * Detached form ("--pretty X" as opposed to "--pretty=X")
1311 * not allowed, since the argument is optional.
1313 revs->verbose_header = 1;
1314 revs->pretty_given = 1;
1315 get_commit_format(arg+9, revs);
1316 } else if (!strcmp(arg, "--show-notes")) {
1317 revs->show_notes = 1;
1318 revs->show_notes_given = 1;
1319 } else if (!prefixcmp(arg, "--show-notes=")) {
1320 struct strbuf buf = STRBUF_INIT;
1321 revs->show_notes = 1;
1322 revs->show_notes_given = 1;
1323 if (!revs->notes_opt.extra_notes_refs)
1324 revs->notes_opt.extra_notes_refs = xcalloc(1, sizeof(struct string_list));
1325 if (!prefixcmp(arg+13, "refs/"))
1326 /* happy */;
1327 else if (!prefixcmp(arg+13, "notes/"))
1328 strbuf_addstr(&buf, "refs/");
1329 else
1330 strbuf_addstr(&buf, "refs/notes/");
1331 strbuf_addstr(&buf, arg+13);
1332 string_list_append(revs->notes_opt.extra_notes_refs,
1333 strbuf_detach(&buf, NULL));
1334 } else if (!strcmp(arg, "--no-notes")) {
1335 revs->show_notes = 0;
1336 revs->show_notes_given = 1;
1337 } else if (!strcmp(arg, "--standard-notes")) {
1338 revs->show_notes_given = 1;
1339 revs->notes_opt.suppress_default_notes = 0;
1340 } else if (!strcmp(arg, "--no-standard-notes")) {
1341 revs->notes_opt.suppress_default_notes = 1;
1342 } else if (!strcmp(arg, "--oneline")) {
1343 revs->verbose_header = 1;
1344 get_commit_format("oneline", revs);
1345 revs->pretty_given = 1;
1346 revs->abbrev_commit = 1;
1347 } else if (!strcmp(arg, "--graph")) {
1348 revs->topo_order = 1;
1349 revs->rewrite_parents = 1;
1350 revs->graph = graph_init(revs);
1351 } else if (!strcmp(arg, "--root")) {
1352 revs->show_root_diff = 1;
1353 } else if (!strcmp(arg, "--no-commit-id")) {
1354 revs->no_commit_id = 1;
1355 } else if (!strcmp(arg, "--always")) {
1356 revs->always_show_header = 1;
1357 } else if (!strcmp(arg, "--no-abbrev")) {
1358 revs->abbrev = 0;
1359 } else if (!strcmp(arg, "--abbrev")) {
1360 revs->abbrev = DEFAULT_ABBREV;
1361 } else if (!prefixcmp(arg, "--abbrev=")) {
1362 revs->abbrev = strtoul(arg + 9, NULL, 10);
1363 if (revs->abbrev < MINIMUM_ABBREV)
1364 revs->abbrev = MINIMUM_ABBREV;
1365 else if (revs->abbrev > 40)
1366 revs->abbrev = 40;
1367 } else if (!strcmp(arg, "--abbrev-commit")) {
1368 revs->abbrev_commit = 1;
1369 } else if (!strcmp(arg, "--full-diff")) {
1370 revs->diff = 1;
1371 revs->full_diff = 1;
1372 } else if (!strcmp(arg, "--full-history")) {
1373 revs->simplify_history = 0;
1374 } else if (!strcmp(arg, "--relative-date")) {
1375 revs->date_mode = DATE_RELATIVE;
1376 revs->date_mode_explicit = 1;
1377 } else if ((argcount = parse_long_opt("date", argv, &optarg))) {
1378 revs->date_mode = parse_date_format(optarg);
1379 revs->date_mode_explicit = 1;
1380 return argcount;
1381 } else if (!strcmp(arg, "--log-size")) {
1382 revs->show_log_size = 1;
1385 * Grepping the commit log
1387 else if ((argcount = parse_long_opt("author", argv, &optarg))) {
1388 add_header_grep(revs, GREP_HEADER_AUTHOR, optarg);
1389 return argcount;
1390 } else if ((argcount = parse_long_opt("committer", argv, &optarg))) {
1391 add_header_grep(revs, GREP_HEADER_COMMITTER, optarg);
1392 return argcount;
1393 } else if ((argcount = parse_long_opt("grep", argv, &optarg))) {
1394 add_message_grep(revs, optarg);
1395 return argcount;
1396 } else if (!strcmp(arg, "--extended-regexp") || !strcmp(arg, "-E")) {
1397 revs->grep_filter.regflags |= REG_EXTENDED;
1398 } else if (!strcmp(arg, "--regexp-ignore-case") || !strcmp(arg, "-i")) {
1399 revs->grep_filter.regflags |= REG_ICASE;
1400 } else if (!strcmp(arg, "--fixed-strings") || !strcmp(arg, "-F")) {
1401 revs->grep_filter.fixed = 1;
1402 } else if (!strcmp(arg, "--all-match")) {
1403 revs->grep_filter.all_match = 1;
1404 } else if ((argcount = parse_long_opt("encoding", argv, &optarg))) {
1405 if (strcmp(optarg, "none"))
1406 git_log_output_encoding = xstrdup(optarg);
1407 else
1408 git_log_output_encoding = "";
1409 return argcount;
1410 } else if (!strcmp(arg, "--reverse")) {
1411 revs->reverse ^= 1;
1412 } else if (!strcmp(arg, "--children")) {
1413 revs->children.name = "children";
1414 revs->limited = 1;
1415 } else {
1416 int opts = diff_opt_parse(&revs->diffopt, argv, argc);
1417 if (!opts)
1418 unkv[(*unkc)++] = arg;
1419 return opts;
1422 return 1;
1425 void parse_revision_opt(struct rev_info *revs, struct parse_opt_ctx_t *ctx,
1426 const struct option *options,
1427 const char * const usagestr[])
1429 int n = handle_revision_opt(revs, ctx->argc, ctx->argv,
1430 &ctx->cpidx, ctx->out);
1431 if (n <= 0) {
1432 error("unknown option `%s'", ctx->argv[0]);
1433 usage_with_options(usagestr, options);
1435 ctx->argv += n;
1436 ctx->argc -= n;
1439 static int for_each_bad_bisect_ref(const char *submodule, each_ref_fn fn, void *cb_data)
1441 return for_each_ref_in_submodule(submodule, "refs/bisect/bad", fn, cb_data);
1444 static int for_each_good_bisect_ref(const char *submodule, each_ref_fn fn, void *cb_data)
1446 return for_each_ref_in_submodule(submodule, "refs/bisect/good", fn, cb_data);
1449 static void append_prune_data(const char ***prune_data, const char **av)
1451 const char **prune = *prune_data;
1452 int prune_nr;
1453 int prune_alloc;
1455 if (!prune) {
1456 *prune_data = av;
1457 return;
1460 /* count existing ones */
1461 for (prune_nr = 0; prune[prune_nr]; prune_nr++)
1463 prune_alloc = prune_nr; /* not really, but we do not know */
1465 while (*av) {
1466 ALLOC_GROW(prune, prune_nr+1, prune_alloc);
1467 prune[prune_nr++] = *av;
1468 av++;
1470 if (prune) {
1471 ALLOC_GROW(prune, prune_nr+1, prune_alloc);
1472 prune[prune_nr] = NULL;
1474 *prune_data = prune;
1478 * Parse revision information, filling in the "rev_info" structure,
1479 * and removing the used arguments from the argument list.
1481 * Returns the number of arguments left that weren't recognized
1482 * (which are also moved to the head of the argument list)
1484 int setup_revisions(int argc, const char **argv, struct rev_info *revs, struct setup_revision_opt *opt)
1486 int i, flags, left, seen_dashdash, read_from_stdin, got_rev_arg = 0;
1487 const char **prune_data = NULL;
1488 const char *submodule = NULL;
1489 const char *optarg;
1490 int argcount;
1492 if (opt)
1493 submodule = opt->submodule;
1495 /* First, search for "--" */
1496 seen_dashdash = 0;
1497 for (i = 1; i < argc; i++) {
1498 const char *arg = argv[i];
1499 if (strcmp(arg, "--"))
1500 continue;
1501 argv[i] = NULL;
1502 argc = i;
1503 if (argv[i + 1])
1504 prune_data = argv + i + 1;
1505 seen_dashdash = 1;
1506 break;
1509 /* Second, deal with arguments and options */
1510 flags = 0;
1511 read_from_stdin = 0;
1512 for (left = i = 1; i < argc; i++) {
1513 const char *arg = argv[i];
1514 if (*arg == '-') {
1515 int opts;
1517 if (!strcmp(arg, "--all")) {
1518 handle_refs(submodule, revs, flags, for_each_ref_submodule);
1519 handle_refs(submodule, revs, flags, head_ref_submodule);
1520 continue;
1522 if (!strcmp(arg, "--branches")) {
1523 handle_refs(submodule, revs, flags, for_each_branch_ref_submodule);
1524 continue;
1526 if (!strcmp(arg, "--bisect")) {
1527 handle_refs(submodule, revs, flags, for_each_bad_bisect_ref);
1528 handle_refs(submodule, revs, flags ^ UNINTERESTING, for_each_good_bisect_ref);
1529 revs->bisect = 1;
1530 continue;
1532 if (!strcmp(arg, "--tags")) {
1533 handle_refs(submodule, revs, flags, for_each_tag_ref_submodule);
1534 continue;
1536 if (!strcmp(arg, "--remotes")) {
1537 handle_refs(submodule, revs, flags, for_each_remote_ref_submodule);
1538 continue;
1540 if ((argcount = parse_long_opt("glob", argv + i, &optarg))) {
1541 struct all_refs_cb cb;
1542 i += argcount - 1;
1543 init_all_refs_cb(&cb, revs, flags);
1544 for_each_glob_ref(handle_one_ref, optarg, &cb);
1545 continue;
1547 if (!prefixcmp(arg, "--branches=")) {
1548 struct all_refs_cb cb;
1549 init_all_refs_cb(&cb, revs, flags);
1550 for_each_glob_ref_in(handle_one_ref, arg + 11, "refs/heads/", &cb);
1551 continue;
1553 if (!prefixcmp(arg, "--tags=")) {
1554 struct all_refs_cb cb;
1555 init_all_refs_cb(&cb, revs, flags);
1556 for_each_glob_ref_in(handle_one_ref, arg + 7, "refs/tags/", &cb);
1557 continue;
1559 if (!prefixcmp(arg, "--remotes=")) {
1560 struct all_refs_cb cb;
1561 init_all_refs_cb(&cb, revs, flags);
1562 for_each_glob_ref_in(handle_one_ref, arg + 10, "refs/remotes/", &cb);
1563 continue;
1565 if (!strcmp(arg, "--reflog")) {
1566 handle_reflog(revs, flags);
1567 continue;
1569 if (!strcmp(arg, "--not")) {
1570 flags ^= UNINTERESTING;
1571 continue;
1573 if (!strcmp(arg, "--no-walk")) {
1574 revs->no_walk = 1;
1575 continue;
1577 if (!strcmp(arg, "--do-walk")) {
1578 revs->no_walk = 0;
1579 continue;
1581 if (!strcmp(arg, "--stdin")) {
1582 if (revs->disable_stdin) {
1583 argv[left++] = arg;
1584 continue;
1586 if (read_from_stdin++)
1587 die("--stdin given twice?");
1588 read_revisions_from_stdin(revs, &prune_data);
1589 continue;
1592 opts = handle_revision_opt(revs, argc - i, argv + i, &left, argv);
1593 if (opts > 0) {
1594 i += opts - 1;
1595 continue;
1597 if (opts < 0)
1598 exit(128);
1599 continue;
1602 if (handle_revision_arg(arg, revs, flags, seen_dashdash)) {
1603 int j;
1604 if (seen_dashdash || *arg == '^')
1605 die("bad revision '%s'", arg);
1607 /* If we didn't have a "--":
1608 * (1) all filenames must exist;
1609 * (2) all rev-args must not be interpretable
1610 * as a valid filename.
1611 * but the latter we have checked in the main loop.
1613 for (j = i; j < argc; j++)
1614 verify_filename(revs->prefix, argv[j]);
1616 append_prune_data(&prune_data, argv + i);
1617 break;
1619 else
1620 got_rev_arg = 1;
1623 if (prune_data)
1624 revs->prune_data = get_pathspec(revs->prefix, prune_data);
1626 if (revs->def == NULL)
1627 revs->def = opt ? opt->def : NULL;
1628 if (opt && opt->tweak)
1629 opt->tweak(revs, opt);
1630 if (revs->show_merge)
1631 prepare_show_merge(revs);
1632 if (revs->def && !revs->pending.nr && !got_rev_arg) {
1633 unsigned char sha1[20];
1634 struct object *object;
1635 unsigned mode;
1636 if (get_sha1_with_mode(revs->def, sha1, &mode))
1637 die("bad default revision '%s'", revs->def);
1638 object = get_reference(revs, revs->def, sha1, 0);
1639 add_pending_object_with_mode(revs, object, revs->def, mode);
1642 /* Did the user ask for any diff output? Run the diff! */
1643 if (revs->diffopt.output_format & ~DIFF_FORMAT_NO_OUTPUT)
1644 revs->diff = 1;
1646 /* Pickaxe, diff-filter and rename following need diffs */
1647 if (revs->diffopt.pickaxe ||
1648 revs->diffopt.filter ||
1649 DIFF_OPT_TST(&revs->diffopt, FOLLOW_RENAMES))
1650 revs->diff = 1;
1652 if (revs->topo_order)
1653 revs->limited = 1;
1655 if (revs->prune_data) {
1656 diff_tree_setup_paths(revs->prune_data, &revs->pruning);
1657 /* Can't prune commits with rename following: the paths change.. */
1658 if (!DIFF_OPT_TST(&revs->diffopt, FOLLOW_RENAMES))
1659 revs->prune = 1;
1660 if (!revs->full_diff)
1661 diff_tree_setup_paths(revs->prune_data, &revs->diffopt);
1663 if (revs->combine_merges)
1664 revs->ignore_merges = 0;
1665 revs->diffopt.abbrev = revs->abbrev;
1667 if (revs->line_level_traverse) {
1668 revs->limited = 1;
1669 revs->topo_order = 1;
1672 if (diff_setup_done(&revs->diffopt) < 0)
1673 die("diff_setup_done failed");
1675 compile_grep_patterns(&revs->grep_filter);
1677 if (revs->reverse && revs->reflog_info)
1678 die("cannot combine --reverse with --walk-reflogs");
1679 if (revs->rewrite_parents && revs->children.name)
1680 die("cannot combine --parents and --children");
1683 * Limitations on the graph functionality
1685 if (revs->reverse && revs->graph)
1686 die("cannot combine --reverse with --graph");
1688 if (revs->reflog_info && revs->graph)
1689 die("cannot combine --walk-reflogs with --graph");
1691 return left;
1694 static void add_child(struct rev_info *revs, struct commit *parent, struct commit *child)
1696 struct commit_list *l = xcalloc(1, sizeof(*l));
1698 l->item = child;
1699 l->next = add_decoration(&revs->children, &parent->object, l);
1702 static int remove_duplicate_parents(struct commit *commit)
1704 struct commit_list **pp, *p;
1705 int surviving_parents;
1707 /* Examine existing parents while marking ones we have seen... */
1708 pp = &commit->parents;
1709 while ((p = *pp) != NULL) {
1710 struct commit *parent = p->item;
1711 if (parent->object.flags & TMP_MARK) {
1712 *pp = p->next;
1713 continue;
1715 parent->object.flags |= TMP_MARK;
1716 pp = &p->next;
1718 /* count them while clearing the temporary mark */
1719 surviving_parents = 0;
1720 for (p = commit->parents; p; p = p->next) {
1721 p->item->object.flags &= ~TMP_MARK;
1722 surviving_parents++;
1724 return surviving_parents;
1727 struct merge_simplify_state {
1728 struct commit *simplified;
1731 static struct merge_simplify_state *locate_simplify_state(struct rev_info *revs, struct commit *commit)
1733 struct merge_simplify_state *st;
1735 st = lookup_decoration(&revs->merge_simplification, &commit->object);
1736 if (!st) {
1737 st = xcalloc(1, sizeof(*st));
1738 add_decoration(&revs->merge_simplification, &commit->object, st);
1740 return st;
1743 static struct commit_list **simplify_one(struct rev_info *revs, struct commit *commit, struct commit_list **tail)
1745 struct commit_list *p;
1746 struct merge_simplify_state *st, *pst;
1747 int cnt;
1749 st = locate_simplify_state(revs, commit);
1752 * Have we handled this one?
1754 if (st->simplified)
1755 return tail;
1758 * An UNINTERESTING commit simplifies to itself, so does a
1759 * root commit. We do not rewrite parents of such commit
1760 * anyway.
1762 if ((commit->object.flags & UNINTERESTING) || !commit->parents) {
1763 st->simplified = commit;
1764 return tail;
1768 * Do we know what commit all of our parents should be rewritten to?
1769 * Otherwise we are not ready to rewrite this one yet.
1771 for (cnt = 0, p = commit->parents; p; p = p->next) {
1772 pst = locate_simplify_state(revs, p->item);
1773 if (!pst->simplified) {
1774 tail = &commit_list_insert(p->item, tail)->next;
1775 cnt++;
1778 if (cnt) {
1779 tail = &commit_list_insert(commit, tail)->next;
1780 return tail;
1784 * Rewrite our list of parents.
1786 for (p = commit->parents; p; p = p->next) {
1787 pst = locate_simplify_state(revs, p->item);
1788 p->item = pst->simplified;
1790 cnt = remove_duplicate_parents(commit);
1793 * It is possible that we are a merge and one side branch
1794 * does not have any commit that touches the given paths;
1795 * in such a case, the immediate parents will be rewritten
1796 * to different commits.
1798 * o----X X: the commit we are looking at;
1799 * / / o: a commit that touches the paths;
1800 * ---o----'
1802 * Further reduce the parents by removing redundant parents.
1804 if (1 < cnt) {
1805 struct commit_list *h = reduce_heads(commit->parents);
1806 cnt = commit_list_count(h);
1807 free_commit_list(commit->parents);
1808 commit->parents = h;
1812 * A commit simplifies to itself if it is a root, if it is
1813 * UNINTERESTING, if it touches the given paths, or if it is a
1814 * merge and its parents simplifies to more than one commits
1815 * (the first two cases are already handled at the beginning of
1816 * this function).
1818 * Otherwise, it simplifies to what its sole parent simplifies to.
1820 if (!cnt ||
1821 (commit->object.flags & UNINTERESTING) ||
1822 !(commit->object.flags & TREESAME) ||
1823 (1 < cnt))
1824 st->simplified = commit;
1825 else {
1826 pst = locate_simplify_state(revs, commit->parents->item);
1827 st->simplified = pst->simplified;
1829 return tail;
1832 static void simplify_merges(struct rev_info *revs)
1834 struct commit_list *list;
1835 struct commit_list *yet_to_do, **tail;
1837 if (!revs->topo_order)
1838 sort_in_topological_order(&revs->commits, revs->lifo);
1839 if (!revs->prune)
1840 return;
1842 /* feed the list reversed */
1843 yet_to_do = NULL;
1844 for (list = revs->commits; list; list = list->next)
1845 commit_list_insert(list->item, &yet_to_do);
1846 while (yet_to_do) {
1847 list = yet_to_do;
1848 yet_to_do = NULL;
1849 tail = &yet_to_do;
1850 while (list) {
1851 struct commit *commit = list->item;
1852 struct commit_list *next = list->next;
1853 free(list);
1854 list = next;
1855 tail = simplify_one(revs, commit, tail);
1859 /* clean up the result, removing the simplified ones */
1860 list = revs->commits;
1861 revs->commits = NULL;
1862 tail = &revs->commits;
1863 while (list) {
1864 struct commit *commit = list->item;
1865 struct commit_list *next = list->next;
1866 struct merge_simplify_state *st;
1867 free(list);
1868 list = next;
1869 st = locate_simplify_state(revs, commit);
1870 if (st->simplified == commit)
1871 tail = &commit_list_insert(commit, tail)->next;
1875 static void set_children(struct rev_info *revs)
1877 struct commit_list *l;
1878 for (l = revs->commits; l; l = l->next) {
1879 struct commit *commit = l->item;
1880 struct commit_list *p;
1882 for (p = commit->parents; p; p = p->next)
1883 add_child(revs, p->item, commit);
1887 int prepare_revision_walk(struct rev_info *revs)
1889 int nr = revs->pending.nr;
1890 struct object_array_entry *e, *list;
1892 e = list = revs->pending.objects;
1893 revs->pending.nr = 0;
1894 revs->pending.alloc = 0;
1895 revs->pending.objects = NULL;
1896 while (--nr >= 0) {
1897 struct commit *commit = handle_commit(revs, e->item, e->name);
1898 if (commit) {
1899 if (!(commit->object.flags & SEEN)) {
1900 commit->object.flags |= SEEN;
1901 insert_by_date(commit, &revs->commits);
1904 e++;
1906 free(list);
1908 if (revs->no_walk)
1909 return 0;
1910 if (revs->limited)
1911 if (limit_list(revs) < 0)
1912 return -1;
1913 if (revs->topo_order)
1914 sort_in_topological_order(&revs->commits, revs->lifo);
1915 if (revs->full_line_diff)
1916 revs->dense = 0;
1917 if (revs->rewrite_parents && revs->line_level_traverse
1918 && !revs->full_line_diff)
1919 limit_list_line(revs);
1920 if (revs->simplify_merges)
1921 simplify_merges(revs);
1922 if (revs->children.name)
1923 set_children(revs);
1924 return 0;
1927 static enum rewrite_result rewrite_one(struct rev_info *revs, struct commit **pp)
1929 struct commit_list *cache = NULL;
1931 for (;;) {
1932 struct commit *p = *pp;
1933 if (!revs->limited)
1934 if (add_parents_to_list(revs, p, &revs->commits, &cache) < 0)
1935 return rewrite_one_error;
1936 if (p->parents && p->parents->next)
1937 return rewrite_one_ok;
1938 if (p->object.flags & UNINTERESTING)
1939 return rewrite_one_ok;
1940 if (!(p->object.flags & TREESAME))
1941 return rewrite_one_ok;
1942 if (!p->parents)
1943 return rewrite_one_noparents;
1944 *pp = p->parents->item;
1948 int rewrite_parents(struct rev_info *revs, struct commit *commit,
1949 rewrite_parent_fn_t rewrite_parent)
1951 struct commit_list **pp = &commit->parents;
1952 while (*pp) {
1953 struct commit_list *parent = *pp;
1954 switch (rewrite_parent(revs, &parent->item)) {
1955 case rewrite_one_ok:
1956 break;
1957 case rewrite_one_noparents:
1958 *pp = parent->next;
1959 continue;
1960 case rewrite_one_error:
1961 return -1;
1963 pp = &parent->next;
1965 remove_duplicate_parents(commit);
1966 return 0;
1969 static int commit_match(struct commit *commit, struct rev_info *opt)
1971 if (!opt->grep_filter.pattern_list && !opt->grep_filter.header_list)
1972 return 1;
1973 return grep_buffer(&opt->grep_filter,
1974 NULL, /* we say nothing, not even filename */
1975 commit->buffer, strlen(commit->buffer));
1978 static inline int want_ancestry(struct rev_info *revs)
1980 return (revs->rewrite_parents || revs->children.name);
1983 enum commit_action get_commit_action(struct rev_info *revs, struct commit *commit)
1985 if (commit->object.flags & SHOWN)
1986 return commit_ignore;
1987 if (revs->unpacked && has_sha1_pack(commit->object.sha1))
1988 return commit_ignore;
1989 if (revs->show_all)
1990 return commit_show;
1991 if (commit->object.flags & UNINTERESTING)
1992 return commit_ignore;
1993 if (revs->min_age != -1 && (commit->date > revs->min_age))
1994 return commit_ignore;
1995 if (revs->no_merges && commit->parents && commit->parents->next)
1996 return commit_ignore;
1997 if (revs->merges_only && !(commit->parents && commit->parents->next))
1998 return commit_ignore;
1999 if (!commit_match(commit, revs))
2000 return commit_ignore;
2001 if (revs->prune && revs->dense) {
2002 /* Commit without changes? */
2003 if (commit->object.flags & TREESAME) {
2004 /* drop merges unless we want parenthood */
2005 if (!want_ancestry(revs))
2006 return commit_ignore;
2007 /* non-merge - always ignore it */
2008 if (!commit->parents || !commit->parents->next)
2009 return commit_ignore;
2012 return commit_show;
2015 enum commit_action simplify_commit(struct rev_info *revs, struct commit *commit)
2017 enum commit_action action = get_commit_action(revs, commit);
2019 if (action == commit_show &&
2020 !revs->show_all &&
2021 revs->prune && revs->dense && want_ancestry(revs)) {
2022 if (rewrite_parents(revs, commit, rewrite_one) < 0)
2023 return commit_error;
2025 return action;
2028 static struct commit *get_revision_1(struct rev_info *revs)
2030 if (!revs->commits)
2031 return NULL;
2033 do {
2034 struct commit_list *entry = revs->commits;
2035 struct commit *commit = entry->item;
2037 revs->commits = entry->next;
2038 free(entry);
2040 if (revs->reflog_info)
2041 fake_reflog_parent(revs->reflog_info, commit);
2044 * If we haven't done the list limiting, we need to look at
2045 * the parents here. We also need to do the date-based limiting
2046 * that we'd otherwise have done in limit_list().
2048 if (!revs->limited) {
2049 if (revs->max_age != -1 &&
2050 (commit->date < revs->max_age))
2051 continue;
2052 if (add_parents_to_list(revs, commit, &revs->commits, NULL) < 0)
2053 die("Failed to traverse parents of commit %s",
2054 sha1_to_hex(commit->object.sha1));
2057 switch (simplify_commit(revs, commit)) {
2058 case commit_ignore:
2059 continue;
2060 case commit_error:
2061 die("Failed to simplify parents of commit %s",
2062 sha1_to_hex(commit->object.sha1));
2063 default:
2064 return commit;
2066 } while (revs->commits);
2067 return NULL;
2070 static void gc_boundary(struct object_array *array)
2072 unsigned nr = array->nr;
2073 unsigned alloc = array->alloc;
2074 struct object_array_entry *objects = array->objects;
2076 if (alloc <= nr) {
2077 unsigned i, j;
2078 for (i = j = 0; i < nr; i++) {
2079 if (objects[i].item->flags & SHOWN)
2080 continue;
2081 if (i != j)
2082 objects[j] = objects[i];
2083 j++;
2085 for (i = j; i < nr; i++)
2086 objects[i].item = NULL;
2087 array->nr = j;
2091 static void create_boundary_commit_list(struct rev_info *revs)
2093 unsigned i;
2094 struct commit *c;
2095 struct object_array *array = &revs->boundary_commits;
2096 struct object_array_entry *objects = array->objects;
2099 * If revs->commits is non-NULL at this point, an error occurred in
2100 * get_revision_1(). Ignore the error and continue printing the
2101 * boundary commits anyway. (This is what the code has always
2102 * done.)
2104 if (revs->commits) {
2105 free_commit_list(revs->commits);
2106 revs->commits = NULL;
2110 * Put all of the actual boundary commits from revs->boundary_commits
2111 * into revs->commits
2113 for (i = 0; i < array->nr; i++) {
2114 c = (struct commit *)(objects[i].item);
2115 if (!c)
2116 continue;
2117 if (!(c->object.flags & CHILD_SHOWN))
2118 continue;
2119 if (c->object.flags & (SHOWN | BOUNDARY))
2120 continue;
2121 c->object.flags |= BOUNDARY;
2122 commit_list_insert(c, &revs->commits);
2126 * If revs->topo_order is set, sort the boundary commits
2127 * in topological order
2129 sort_in_topological_order(&revs->commits, revs->lifo);
2132 static struct commit *get_revision_internal(struct rev_info *revs)
2134 struct commit *c = NULL;
2135 struct commit_list *l;
2137 if (revs->boundary == 2) {
2139 * All of the normal commits have already been returned,
2140 * and we are now returning boundary commits.
2141 * create_boundary_commit_list() has populated
2142 * revs->commits with the remaining commits to return.
2144 c = pop_commit(&revs->commits);
2145 if (c)
2146 c->object.flags |= SHOWN;
2147 return c;
2151 * Now pick up what they want to give us
2153 c = get_revision_1(revs);
2154 if (c) {
2155 while (0 < revs->skip_count) {
2156 revs->skip_count--;
2157 c = get_revision_1(revs);
2158 if (!c)
2159 break;
2164 * Check the max_count.
2166 switch (revs->max_count) {
2167 case -1:
2168 break;
2169 case 0:
2170 c = NULL;
2171 break;
2172 default:
2173 revs->max_count--;
2176 if (c)
2177 c->object.flags |= SHOWN;
2179 if (!revs->boundary) {
2180 return c;
2183 if (!c) {
2185 * get_revision_1() runs out the commits, and
2186 * we are done computing the boundaries.
2187 * switch to boundary commits output mode.
2189 revs->boundary = 2;
2192 * Update revs->commits to contain the list of
2193 * boundary commits.
2195 create_boundary_commit_list(revs);
2197 return get_revision_internal(revs);
2201 * boundary commits are the commits that are parents of the
2202 * ones we got from get_revision_1() but they themselves are
2203 * not returned from get_revision_1(). Before returning
2204 * 'c', we need to mark its parents that they could be boundaries.
2207 for (l = c->parents; l; l = l->next) {
2208 struct object *p;
2209 p = &(l->item->object);
2210 if (p->flags & (CHILD_SHOWN | SHOWN))
2211 continue;
2212 p->flags |= CHILD_SHOWN;
2213 gc_boundary(&revs->boundary_commits);
2214 add_object_array(p, NULL, &revs->boundary_commits);
2217 return c;
2220 struct commit *get_revision(struct rev_info *revs)
2222 struct commit *c;
2223 struct commit_list *reversed;
2225 if (revs->reverse) {
2226 reversed = NULL;
2227 while ((c = get_revision_internal(revs))) {
2228 commit_list_insert(c, &reversed);
2230 revs->commits = reversed;
2231 revs->reverse = 0;
2232 revs->reverse_output_stage = 1;
2235 if (revs->reverse_output_stage)
2236 return pop_commit(&revs->commits);
2238 c = get_revision_internal(revs);
2239 if (c && revs->graph)
2240 graph_update(revs->graph, c);
2241 return c;