10 #include "reflog-walk.h"
12 static char *path_name(struct name_path *path, const char *name)
16 int nlen = strlen(name);
19 for (p = path; p; p = p->up) {
21 len += p->elem_len + 1;
24 m = n + len - (nlen + 1);
26 for (p = path; p; p = p->up) {
29 memcpy(m, p->elem, p->elem_len);
36 void add_object(struct object *obj,
37 struct object_array *p,
38 struct name_path *path,
41 add_object_array(obj, path_name(path, name), p);
44 static void mark_blob_uninteresting(struct blob *blob)
46 if (blob->object.flags & UNINTERESTING)
48 blob->object.flags |= UNINTERESTING;
51 void mark_tree_uninteresting(struct tree *tree)
53 struct tree_desc desc;
54 struct name_entry entry;
55 struct object *obj = &tree->object;
57 if (obj->flags & UNINTERESTING)
59 obj->flags |= UNINTERESTING;
60 if (!has_sha1_file(obj->sha1))
62 if (parse_tree(tree) < 0)
63 die("bad tree %s", sha1_to_hex(obj->sha1));
65 desc.buf = tree->buffer;
66 desc.size = tree->size;
67 while (tree_entry(&desc, &entry)) {
68 if (S_ISDIR(entry.mode))
69 mark_tree_uninteresting(lookup_tree(entry.sha1));
71 mark_blob_uninteresting(lookup_blob(entry.sha1));
75 * We don't care about the tree any more
76 * after it has been marked uninteresting.
82 void mark_parents_uninteresting(struct commit *commit)
84 struct commit_list *parents = commit->parents;
87 struct commit *commit = parents->item;
88 if (!(commit->object.flags & UNINTERESTING)) {
89 commit->object.flags |= UNINTERESTING;
92 * Normally we haven't parsed the parent
93 * yet, so we won't have a parent of a parent
94 * here. However, it may turn out that we've
95 * reached this commit some other way (where it
96 * wasn't uninteresting), in which case we need
97 * to mark its parents recursively too..
100 mark_parents_uninteresting(commit);
104 * A missing commit is ok iff its parent is marked
107 * We just mark such a thing parsed, so that when
108 * it is popped next time around, we won't be trying
109 * to parse it and get an error.
111 if (!has_sha1_file(commit->object.sha1))
112 commit->object.parsed = 1;
113 parents = parents->next;
117 void add_pending_object(struct rev_info *revs, struct object *obj, const char *name)
119 if (revs->no_walk && (obj->flags & UNINTERESTING))
120 die("object ranges do not make sense when not walking revisions");
121 add_object_array(obj, name, &revs->pending);
122 if (revs->reflog_info && obj->type == OBJ_COMMIT)
123 add_reflog_for_walk(revs->reflog_info,
124 (struct commit *)obj, name);
127 static struct object *get_reference(struct rev_info *revs, const char *name, const unsigned char *sha1, unsigned int flags)
129 struct object *object;
131 object = parse_object(sha1);
133 die("bad object %s", name);
134 object->flags |= flags;
138 static struct commit *handle_commit(struct rev_info *revs, struct object *object, const char *name)
140 unsigned long flags = object->flags;
143 * Tag object? Look what it points to..
145 while (object->type == OBJ_TAG) {
146 struct tag *tag = (struct tag *) object;
147 if (revs->tag_objects && !(flags & UNINTERESTING))
148 add_pending_object(revs, object, tag->tag);
149 object = parse_object(tag->tagged->sha1);
151 die("bad object %s", sha1_to_hex(tag->tagged->sha1));
155 * Commit object? Just return it, we'll do all the complex
158 if (object->type == OBJ_COMMIT) {
159 struct commit *commit = (struct commit *)object;
160 if (parse_commit(commit) < 0)
161 die("unable to parse commit %s", name);
162 if (flags & UNINTERESTING) {
163 commit->object.flags |= UNINTERESTING;
164 mark_parents_uninteresting(commit);
171 * Tree object? Either mark it uniniteresting, or add it
172 * to the list of objects to look at later..
174 if (object->type == OBJ_TREE) {
175 struct tree *tree = (struct tree *)object;
176 if (!revs->tree_objects)
178 if (flags & UNINTERESTING) {
179 mark_tree_uninteresting(tree);
182 add_pending_object(revs, object, "");
187 * Blob object? You know the drill by now..
189 if (object->type == OBJ_BLOB) {
190 struct blob *blob = (struct blob *)object;
191 if (!revs->blob_objects)
193 if (flags & UNINTERESTING) {
194 mark_blob_uninteresting(blob);
197 add_pending_object(revs, object, "");
200 die("%s is unknown object", name);
203 static int everybody_uninteresting(struct commit_list *orig)
205 struct commit_list *list = orig;
207 struct commit *commit = list->item;
209 if (commit->object.flags & UNINTERESTING)
216 static int tree_difference = REV_TREE_SAME;
218 static void file_add_remove(struct diff_options *options,
219 int addremove, unsigned mode,
220 const unsigned char *sha1,
221 const char *base, const char *path)
223 int diff = REV_TREE_DIFFERENT;
226 * Is it an add of a new file? It means that the old tree
227 * didn't have it at all, so we will turn "REV_TREE_SAME" ->
228 * "REV_TREE_NEW", but leave any "REV_TREE_DIFFERENT" alone
229 * (and if it already was "REV_TREE_NEW", we'll keep it
230 * "REV_TREE_NEW" of course).
232 if (addremove == '+') {
233 diff = tree_difference;
234 if (diff != REV_TREE_SAME)
238 tree_difference = diff;
241 static void file_change(struct diff_options *options,
242 unsigned old_mode, unsigned new_mode,
243 const unsigned char *old_sha1,
244 const unsigned char *new_sha1,
245 const char *base, const char *path)
247 tree_difference = REV_TREE_DIFFERENT;
250 int rev_compare_tree(struct rev_info *revs, struct tree *t1, struct tree *t2)
255 return REV_TREE_DIFFERENT;
256 tree_difference = REV_TREE_SAME;
257 if (diff_tree_sha1(t1->object.sha1, t2->object.sha1, "",
259 return REV_TREE_DIFFERENT;
260 return tree_difference;
263 int rev_same_tree_as_empty(struct rev_info *revs, struct tree *t1)
267 struct tree_desc empty, real;
272 tree = read_object_with_reference(t1->object.sha1, tree_type, &real.size, NULL);
281 retval = diff_tree(&empty, &real, "", &revs->pruning);
284 return retval >= 0 && !tree_difference;
287 static void try_to_simplify_commit(struct rev_info *revs, struct commit *commit)
289 struct commit_list **pp, *parent;
290 int tree_changed = 0, tree_same = 0;
295 if (!commit->parents) {
296 if (!rev_same_tree_as_empty(revs, commit->tree))
297 commit->object.flags |= TREECHANGE;
301 pp = &commit->parents;
302 while ((parent = *pp) != NULL) {
303 struct commit *p = parent->item;
306 switch (rev_compare_tree(revs, p->tree, commit->tree)) {
309 if (!revs->simplify_history || (p->object.flags & UNINTERESTING)) {
310 /* Even if a merge with an uninteresting
311 * side branch brought the entire change
312 * we are interested in, we do not want
313 * to lose the other branches of this
314 * merge, so we just keep going.
320 commit->parents = parent;
324 if (revs->remove_empty_trees &&
325 rev_same_tree_as_empty(revs, p->tree)) {
326 /* We are adding all the specified
327 * paths from this parent, so the
328 * history beyond this parent is not
329 * interesting. Remove its parents
330 * (they are grandparents for us).
331 * IOW, we pretend this parent is a
338 case REV_TREE_DIFFERENT:
343 die("bad tree compare for commit %s", sha1_to_hex(commit->object.sha1));
345 if (tree_changed && !tree_same)
346 commit->object.flags |= TREECHANGE;
349 static void add_parents_to_list(struct rev_info *revs, struct commit *commit, struct commit_list **list)
351 struct commit_list *parent = commit->parents;
354 if (commit->object.flags & ADDED)
356 commit->object.flags |= ADDED;
359 * If the commit is uninteresting, don't try to
360 * prune parents - we want the maximal uninteresting
363 * Normally we haven't parsed the parent
364 * yet, so we won't have a parent of a parent
365 * here. However, it may turn out that we've
366 * reached this commit some other way (where it
367 * wasn't uninteresting), in which case we need
368 * to mark its parents recursively too..
370 if (commit->object.flags & UNINTERESTING) {
372 struct commit *p = parent->item;
373 parent = parent->next;
375 p->object.flags |= UNINTERESTING;
377 mark_parents_uninteresting(p);
378 if (p->object.flags & SEEN)
380 p->object.flags |= SEEN;
381 insert_by_date(p, list);
387 * Ok, the commit wasn't uninteresting. Try to
388 * simplify the commit history and find the parent
389 * that has no differences in the path set if one exists.
392 revs->prune_fn(revs, commit);
397 left_flag = (commit->object.flags & SYMMETRIC_LEFT);
398 parent = commit->parents;
400 struct commit *p = parent->item;
402 parent = parent->next;
405 p->object.flags |= left_flag;
406 if (p->object.flags & SEEN)
408 p->object.flags |= SEEN;
409 insert_by_date(p, list);
413 static void limit_list(struct rev_info *revs)
415 struct commit_list *list = revs->commits;
416 struct commit_list *newlist = NULL;
417 struct commit_list **p = &newlist;
420 struct commit_list *entry = list;
421 struct commit *commit = list->item;
422 struct object *obj = &commit->object;
427 if (revs->max_age != -1 && (commit->date < revs->max_age))
428 obj->flags |= UNINTERESTING;
429 add_parents_to_list(revs, commit, &list);
430 if (obj->flags & UNINTERESTING) {
431 mark_parents_uninteresting(commit);
432 if (everybody_uninteresting(list))
436 if (revs->min_age != -1 && (commit->date > revs->min_age))
438 p = &commit_list_insert(commit, p)->next;
440 revs->commits = newlist;
445 int warned_bad_reflog;
446 struct rev_info *all_revs;
447 const char *name_for_errormsg;
450 static int handle_one_ref(const char *path, const unsigned char *sha1, int flag, void *cb_data)
452 struct all_refs_cb *cb = cb_data;
453 struct object *object = get_reference(cb->all_revs, path, sha1,
455 add_pending_object(cb->all_revs, object, path);
459 static void handle_all(struct rev_info *revs, unsigned flags)
461 struct all_refs_cb cb;
463 cb.all_flags = flags;
464 for_each_ref(handle_one_ref, &cb);
467 static void handle_one_reflog_commit(unsigned char *sha1, void *cb_data)
469 struct all_refs_cb *cb = cb_data;
470 if (!is_null_sha1(sha1)) {
471 struct object *o = parse_object(sha1);
473 o->flags |= cb->all_flags;
474 add_pending_object(cb->all_revs, o, "");
476 else if (!cb->warned_bad_reflog) {
477 warn("reflog of '%s' references pruned commits",
478 cb->name_for_errormsg);
479 cb->warned_bad_reflog = 1;
484 static int handle_one_reflog_ent(unsigned char *osha1, unsigned char *nsha1,
485 const char *email, unsigned long timestamp, int tz,
486 const char *message, void *cb_data)
488 handle_one_reflog_commit(osha1, cb_data);
489 handle_one_reflog_commit(nsha1, cb_data);
493 static int handle_one_reflog(const char *path, const unsigned char *sha1, int flag, void *cb_data)
495 struct all_refs_cb *cb = cb_data;
496 cb->warned_bad_reflog = 0;
497 cb->name_for_errormsg = path;
498 for_each_reflog_ent(path, handle_one_reflog_ent, cb_data);
502 static void handle_reflog(struct rev_info *revs, unsigned flags)
504 struct all_refs_cb cb;
506 cb.all_flags = flags;
507 for_each_reflog(handle_one_reflog, &cb);
510 static int add_parents_only(struct rev_info *revs, const char *arg, int flags)
512 unsigned char sha1[20];
514 struct commit *commit;
515 struct commit_list *parents;
518 flags ^= UNINTERESTING;
521 if (get_sha1(arg, sha1))
524 it = get_reference(revs, arg, sha1, 0);
525 if (it->type != OBJ_TAG)
527 hashcpy(sha1, ((struct tag*)it)->tagged->sha1);
529 if (it->type != OBJ_COMMIT)
531 commit = (struct commit *)it;
532 for (parents = commit->parents; parents; parents = parents->next) {
533 it = &parents->item->object;
535 add_pending_object(revs, it, arg);
540 void init_revisions(struct rev_info *revs, const char *prefix)
542 memset(revs, 0, sizeof(*revs));
544 revs->abbrev = DEFAULT_ABBREV;
545 revs->ignore_merges = 1;
546 revs->simplify_history = 1;
547 revs->pruning.recursive = 1;
548 revs->pruning.add_remove = file_add_remove;
549 revs->pruning.change = file_change;
552 revs->prefix = prefix;
555 revs->skip_count = -1;
556 revs->max_count = -1;
558 revs->prune_fn = NULL;
559 revs->prune_data = NULL;
561 revs->topo_setter = topo_sort_default_setter;
562 revs->topo_getter = topo_sort_default_getter;
564 revs->commit_format = CMIT_FMT_DEFAULT;
566 diff_setup(&revs->diffopt);
569 static void add_pending_commit_list(struct rev_info *revs,
570 struct commit_list *commit_list,
573 while (commit_list) {
574 struct object *object = &commit_list->item->object;
575 object->flags |= flags;
576 add_pending_object(revs, object, sha1_to_hex(object->sha1));
577 commit_list = commit_list->next;
581 static void prepare_show_merge(struct rev_info *revs)
583 struct commit_list *bases;
584 struct commit *head, *other;
585 unsigned char sha1[20];
586 const char **prune = NULL;
587 int i, prune_num = 1; /* counting terminating NULL */
589 if (get_sha1("HEAD", sha1) || !(head = lookup_commit(sha1)))
590 die("--merge without HEAD?");
591 if (get_sha1("MERGE_HEAD", sha1) || !(other = lookup_commit(sha1)))
592 die("--merge without MERGE_HEAD?");
593 add_pending_object(revs, &head->object, "HEAD");
594 add_pending_object(revs, &other->object, "MERGE_HEAD");
595 bases = get_merge_bases(head, other, 1);
597 struct commit *it = bases->item;
598 struct commit_list *n = bases->next;
601 it->object.flags |= UNINTERESTING;
602 add_pending_object(revs, &it->object, "(merge-base)");
607 for (i = 0; i < active_nr; i++) {
608 struct cache_entry *ce = active_cache[i];
611 if (ce_path_match(ce, revs->prune_data)) {
613 prune = xrealloc(prune, sizeof(*prune) * prune_num);
614 prune[prune_num-2] = ce->name;
615 prune[prune_num-1] = NULL;
617 while ((i+1 < active_nr) &&
618 ce_same_name(ce, active_cache[i+1]))
621 revs->prune_data = prune;
624 int handle_revision_arg(const char *arg, struct rev_info *revs,
626 int cant_be_filename)
629 struct object *object;
630 unsigned char sha1[20];
633 dotdot = strstr(arg, "..");
635 unsigned char from_sha1[20];
636 const char *next = dotdot + 2;
637 const char *this = arg;
638 int symmetric = *next == '.';
639 unsigned int flags_exclude = flags ^ UNINTERESTING;
648 if (!get_sha1(this, from_sha1) &&
649 !get_sha1(next, sha1)) {
650 struct commit *a, *b;
651 struct commit_list *exclude;
653 a = lookup_commit_reference(from_sha1);
654 b = lookup_commit_reference(sha1);
657 "Invalid symmetric difference expression %s...%s" :
658 "Invalid revision range %s..%s",
662 if (!cant_be_filename) {
664 verify_non_filename(revs->prefix, arg);
668 exclude = get_merge_bases(a, b, 1);
669 add_pending_commit_list(revs, exclude,
671 free_commit_list(exclude);
672 a->object.flags |= flags | SYMMETRIC_LEFT;
674 a->object.flags |= flags_exclude;
675 b->object.flags |= flags;
676 add_pending_object(revs, &a->object, this);
677 add_pending_object(revs, &b->object, next);
682 dotdot = strstr(arg, "^@");
683 if (dotdot && !dotdot[2]) {
685 if (add_parents_only(revs, arg, flags))
689 dotdot = strstr(arg, "^!");
690 if (dotdot && !dotdot[2]) {
692 if (!add_parents_only(revs, arg, flags ^ UNINTERESTING))
698 local_flags = UNINTERESTING;
701 if (get_sha1(arg, sha1))
703 if (!cant_be_filename)
704 verify_non_filename(revs->prefix, arg);
705 object = get_reference(revs, arg, sha1, flags ^ local_flags);
706 add_pending_object(revs, object, arg);
710 static void add_grep(struct rev_info *revs, const char *ptn, enum grep_pat_token what)
712 if (!revs->grep_filter) {
713 struct grep_opt *opt = xcalloc(1, sizeof(*opt));
714 opt->status_only = 1;
715 opt->pattern_tail = &(opt->pattern_list);
716 opt->regflags = REG_NEWLINE;
717 revs->grep_filter = opt;
719 append_grep_pattern(revs->grep_filter, ptn,
720 "command line", 0, what);
723 static void add_header_grep(struct rev_info *revs, const char *field, const char *pattern)
729 fldlen = strlen(field);
730 patlen = strlen(pattern);
731 pat = xmalloc(patlen + fldlen + 10);
733 if (*pattern == '^') {
737 sprintf(pat, "^%s %s%s", field, prefix, pattern);
738 add_grep(revs, pat, GREP_PATTERN_HEAD);
741 static void add_message_grep(struct rev_info *revs, const char *pattern)
743 add_grep(revs, pattern, GREP_PATTERN_BODY);
746 static void add_ignore_packed(struct rev_info *revs, const char *name)
748 int num = ++revs->num_ignore_packed;
750 revs->ignore_packed = xrealloc(revs->ignore_packed,
751 sizeof(const char **) * (num + 1));
752 revs->ignore_packed[num-1] = name;
753 revs->ignore_packed[num] = NULL;
757 * Parse revision information, filling in the "rev_info" structure,
758 * and removing the used arguments from the argument list.
760 * Returns the number of arguments left that weren't recognized
761 * (which are also moved to the head of the argument list)
763 int setup_revisions(int argc, const char **argv, struct rev_info *revs, const char *def)
765 int i, flags, seen_dashdash, show_merge;
766 const char **unrecognized = argv + 1;
770 /* First, search for "--" */
772 for (i = 1; i < argc; i++) {
773 const char *arg = argv[i];
774 if (strcmp(arg, "--"))
778 revs->prune_data = get_pathspec(revs->prefix, argv + i + 1);
783 flags = show_merge = 0;
784 for (i = 1; i < argc; i++) {
785 const char *arg = argv[i];
788 if (!prefixcmp(arg, "--max-count=")) {
789 revs->max_count = atoi(arg + 12);
792 if (!prefixcmp(arg, "--skip=")) {
793 revs->skip_count = atoi(arg + 7);
796 /* accept -<digit>, like traditional "head" */
797 if ((*arg == '-') && isdigit(arg[1])) {
798 revs->max_count = atoi(arg + 1);
801 if (!strcmp(arg, "-n")) {
803 die("-n requires an argument");
804 revs->max_count = atoi(argv[++i]);
807 if (!prefixcmp(arg, "-n")) {
808 revs->max_count = atoi(arg + 2);
811 if (!prefixcmp(arg, "--max-age=")) {
812 revs->max_age = atoi(arg + 10);
815 if (!prefixcmp(arg, "--since=")) {
816 revs->max_age = approxidate(arg + 8);
819 if (!prefixcmp(arg, "--after=")) {
820 revs->max_age = approxidate(arg + 8);
823 if (!prefixcmp(arg, "--min-age=")) {
824 revs->min_age = atoi(arg + 10);
827 if (!prefixcmp(arg, "--before=")) {
828 revs->min_age = approxidate(arg + 9);
831 if (!prefixcmp(arg, "--until=")) {
832 revs->min_age = approxidate(arg + 8);
835 if (!strcmp(arg, "--all")) {
836 handle_all(revs, flags);
839 if (!strcmp(arg, "--reflog")) {
840 handle_reflog(revs, flags);
843 if (!strcmp(arg, "-g") ||
844 !strcmp(arg, "--walk-reflogs")) {
845 init_reflog_walk(&revs->reflog_info);
848 if (!strcmp(arg, "--not")) {
849 flags ^= UNINTERESTING;
852 if (!strcmp(arg, "--default")) {
854 die("bad --default argument");
858 if (!strcmp(arg, "--merge")) {
862 if (!strcmp(arg, "--topo-order")) {
863 revs->topo_order = 1;
866 if (!strcmp(arg, "--date-order")) {
868 revs->topo_order = 1;
871 if (!strcmp(arg, "--parents")) {
875 if (!strcmp(arg, "--dense")) {
879 if (!strcmp(arg, "--sparse")) {
883 if (!strcmp(arg, "--remove-empty")) {
884 revs->remove_empty_trees = 1;
887 if (!strcmp(arg, "--no-merges")) {
891 if (!strcmp(arg, "--boundary")) {
895 if (!strcmp(arg, "--left-right")) {
896 revs->left_right = 1;
899 if (!strcmp(arg, "--objects")) {
900 revs->tag_objects = 1;
901 revs->tree_objects = 1;
902 revs->blob_objects = 1;
905 if (!strcmp(arg, "--objects-edge")) {
906 revs->tag_objects = 1;
907 revs->tree_objects = 1;
908 revs->blob_objects = 1;
912 if (!strcmp(arg, "--unpacked")) {
914 free(revs->ignore_packed);
915 revs->ignore_packed = NULL;
916 revs->num_ignore_packed = 0;
919 if (!prefixcmp(arg, "--unpacked=")) {
921 add_ignore_packed(revs, arg+11);
924 if (!strcmp(arg, "-r")) {
926 revs->diffopt.recursive = 1;
929 if (!strcmp(arg, "-t")) {
931 revs->diffopt.recursive = 1;
932 revs->diffopt.tree_in_recursive = 1;
935 if (!strcmp(arg, "-m")) {
936 revs->ignore_merges = 0;
939 if (!strcmp(arg, "-c")) {
941 revs->dense_combined_merges = 0;
942 revs->combine_merges = 1;
945 if (!strcmp(arg, "--cc")) {
947 revs->dense_combined_merges = 1;
948 revs->combine_merges = 1;
951 if (!strcmp(arg, "-v")) {
952 revs->verbose_header = 1;
955 if (!prefixcmp(arg, "--pretty")) {
956 revs->verbose_header = 1;
957 revs->commit_format = get_commit_format(arg+8);
960 if (!strcmp(arg, "--root")) {
961 revs->show_root_diff = 1;
964 if (!strcmp(arg, "--no-commit-id")) {
965 revs->no_commit_id = 1;
968 if (!strcmp(arg, "--always")) {
969 revs->always_show_header = 1;
972 if (!strcmp(arg, "--no-abbrev")) {
976 if (!strcmp(arg, "--abbrev")) {
977 revs->abbrev = DEFAULT_ABBREV;
980 if (!prefixcmp(arg, "--abbrev=")) {
981 revs->abbrev = strtoul(arg + 9, NULL, 10);
982 if (revs->abbrev < MINIMUM_ABBREV)
983 revs->abbrev = MINIMUM_ABBREV;
984 else if (revs->abbrev > 40)
988 if (!strcmp(arg, "--abbrev-commit")) {
989 revs->abbrev_commit = 1;
992 if (!strcmp(arg, "--full-diff")) {
997 if (!strcmp(arg, "--full-history")) {
998 revs->simplify_history = 0;
1001 if (!strcmp(arg, "--relative-date")) {
1002 revs->relative_date = 1;
1007 * Grepping the commit log
1009 if (!prefixcmp(arg, "--author=")) {
1010 add_header_grep(revs, "author", arg+9);
1013 if (!prefixcmp(arg, "--committer=")) {
1014 add_header_grep(revs, "committer", arg+12);
1017 if (!prefixcmp(arg, "--grep=")) {
1018 add_message_grep(revs, arg+7);
1021 if (!strcmp(arg, "--all-match")) {
1025 if (!prefixcmp(arg, "--encoding=")) {
1027 if (strcmp(arg, "none"))
1028 git_log_output_encoding = strdup(arg);
1030 git_log_output_encoding = "";
1033 if (!strcmp(arg, "--reverse")) {
1038 opts = diff_opt_parse(&revs->diffopt, argv+i, argc-i);
1044 *unrecognized++ = arg;
1049 if (handle_revision_arg(arg, revs, flags, seen_dashdash)) {
1051 if (seen_dashdash || *arg == '^')
1052 die("bad revision '%s'", arg);
1054 /* If we didn't have a "--":
1055 * (1) all filenames must exist;
1056 * (2) all rev-args must not be interpretable
1057 * as a valid filename.
1058 * but the latter we have checked in the main loop.
1060 for (j = i; j < argc; j++)
1061 verify_filename(revs->prefix, argv[j]);
1063 revs->prune_data = get_pathspec(revs->prefix,
1070 prepare_show_merge(revs);
1071 if (def && !revs->pending.nr) {
1072 unsigned char sha1[20];
1073 struct object *object;
1074 if (get_sha1(def, sha1))
1075 die("bad default revision '%s'", def);
1076 object = get_reference(revs, def, sha1, 0);
1077 add_pending_object(revs, object, def);
1080 if (revs->topo_order)
1083 if (revs->prune_data) {
1084 diff_tree_setup_paths(revs->prune_data, &revs->pruning);
1085 revs->prune_fn = try_to_simplify_commit;
1086 if (!revs->full_diff)
1087 diff_tree_setup_paths(revs->prune_data, &revs->diffopt);
1089 if (revs->combine_merges) {
1090 revs->ignore_merges = 0;
1091 if (revs->dense_combined_merges && !revs->diffopt.output_format)
1092 revs->diffopt.output_format = DIFF_FORMAT_PATCH;
1094 revs->diffopt.abbrev = revs->abbrev;
1095 if (diff_setup_done(&revs->diffopt) < 0)
1096 die("diff_setup_done failed");
1098 if (revs->grep_filter) {
1099 revs->grep_filter->all_match = all_match;
1100 compile_grep_patterns(revs->grep_filter);
1106 void prepare_revision_walk(struct rev_info *revs)
1108 int nr = revs->pending.nr;
1109 struct object_array_entry *e, *list;
1111 e = list = revs->pending.objects;
1112 revs->pending.nr = 0;
1113 revs->pending.alloc = 0;
1114 revs->pending.objects = NULL;
1116 struct commit *commit = handle_commit(revs, e->item, e->name);
1118 if (!(commit->object.flags & SEEN)) {
1119 commit->object.flags |= SEEN;
1120 insert_by_date(commit, &revs->commits);
1131 if (revs->topo_order)
1132 sort_in_topological_order_fn(&revs->commits, revs->lifo,
1137 static int rewrite_one(struct rev_info *revs, struct commit **pp)
1140 struct commit *p = *pp;
1142 add_parents_to_list(revs, p, &revs->commits);
1143 if (p->parents && p->parents->next)
1145 if (p->object.flags & (TREECHANGE | UNINTERESTING))
1149 *pp = p->parents->item;
1153 static void rewrite_parents(struct rev_info *revs, struct commit *commit)
1155 struct commit_list **pp = &commit->parents;
1157 struct commit_list *parent = *pp;
1158 if (rewrite_one(revs, &parent->item) < 0) {
1166 static int commit_match(struct commit *commit, struct rev_info *opt)
1168 if (!opt->grep_filter)
1170 return grep_buffer(opt->grep_filter,
1171 NULL, /* we say nothing, not even filename */
1172 commit->buffer, strlen(commit->buffer));
1175 static struct commit *get_revision_1(struct rev_info *revs)
1181 struct commit_list *entry = revs->commits;
1182 struct commit *commit = entry->item;
1184 revs->commits = entry->next;
1187 if (revs->reflog_info)
1188 fake_reflog_parent(revs->reflog_info, commit);
1191 * If we haven't done the list limiting, we need to look at
1192 * the parents here. We also need to do the date-based limiting
1193 * that we'd otherwise have done in limit_list().
1195 if (!revs->limited) {
1196 if (revs->max_age != -1 &&
1197 (commit->date < revs->max_age))
1199 add_parents_to_list(revs, commit, &revs->commits);
1201 if (commit->object.flags & SHOWN)
1204 if (revs->unpacked && has_sha1_pack(commit->object.sha1,
1205 revs->ignore_packed))
1208 if (commit->object.flags & UNINTERESTING)
1210 if (revs->min_age != -1 && (commit->date > revs->min_age))
1212 if (revs->no_merges &&
1213 commit->parents && commit->parents->next)
1215 if (!commit_match(commit, revs))
1217 if (revs->prune_fn && revs->dense) {
1218 /* Commit without changes? */
1219 if (!(commit->object.flags & TREECHANGE)) {
1220 /* drop merges unless we want parenthood */
1223 /* non-merge - always ignore it */
1224 if (!commit->parents || !commit->parents->next)
1228 rewrite_parents(revs, commit);
1231 } while (revs->commits);
1235 static void gc_boundary(struct object_array *array)
1237 unsigned nr = array->nr;
1238 unsigned alloc = array->alloc;
1239 struct object_array_entry *objects = array->objects;
1243 for (i = j = 0; i < nr; i++) {
1244 if (objects[i].item->flags & SHOWN)
1247 objects[j] = objects[i];
1250 for (i = j; i < nr; i++)
1251 objects[i].item = NULL;
1256 struct commit *get_revision(struct rev_info *revs)
1258 struct commit *c = NULL;
1259 struct commit_list *l;
1261 if (revs->boundary == 2) {
1263 struct object_array *array = &revs->boundary_commits;
1264 struct object_array_entry *objects = array->objects;
1265 for (i = 0; i < array->nr; i++) {
1266 c = (struct commit *)(objects[i].item);
1269 if (!(c->object.flags & CHILD_SHOWN))
1271 if (!(c->object.flags & SHOWN))
1277 c->object.flags |= SHOWN | BOUNDARY;
1281 if (revs->reverse) {
1284 if (0 <= revs->max_count) {
1285 limit = revs->max_count;
1286 if (0 < revs->skip_count)
1287 limit += revs->skip_count;
1290 while ((c = get_revision_1(revs))) {
1291 commit_list_insert(c, &l);
1292 if ((0 < limit) && !--limit)
1297 revs->max_count = -1;
1302 * Now pick up what they want to give us
1304 c = get_revision_1(revs);
1306 while (0 < revs->skip_count) {
1308 c = get_revision_1(revs);
1315 * Check the max_count.
1317 switch (revs->max_count) {
1328 c->object.flags |= SHOWN;
1330 if (!revs->boundary) {
1336 * get_revision_1() runs out the commits, and
1337 * we are done computing the boundaries.
1338 * switch to boundary commits output mode.
1341 return get_revision(revs);
1345 * boundary commits are the commits that are parents of the
1346 * ones we got from get_revision_1() but they themselves are
1347 * not returned from get_revision_1(). Before returning
1348 * 'c', we need to mark its parents that they could be boundaries.
1351 for (l = c->parents; l; l = l->next) {
1353 p = &(l->item->object);
1354 if (p->flags & (CHILD_SHOWN | SHOWN))
1356 p->flags |= CHILD_SHOWN;
1357 gc_boundary(&revs->boundary_commits);
1358 add_object_array(p, NULL, &revs->boundary_commits);