git-svn: don't allow globs to match regular files
[git/jnareb-git/bp-gitweb.git] / revision.c
blob486393cb0835ce70f685d665b916e1b67974f184
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 "grep.h"
10 #include "reflog-walk.h"
12 static char *path_name(struct name_path *path, const char *name)
14 struct name_path *p;
15 char *n, *m;
16 int nlen = strlen(name);
17 int len = nlen + 1;
19 for (p = path; p; p = p->up) {
20 if (p->elem_len)
21 len += p->elem_len + 1;
23 n = xmalloc(len);
24 m = n + len - (nlen + 1);
25 strcpy(m, name);
26 for (p = path; p; p = p->up) {
27 if (p->elem_len) {
28 m -= p->elem_len + 1;
29 memcpy(m, p->elem, p->elem_len);
30 m[p->elem_len] = '/';
33 return n;
36 void add_object(struct object *obj,
37 struct object_array *p,
38 struct name_path *path,
39 const char *name)
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)
47 return;
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)
58 return;
59 obj->flags |= UNINTERESTING;
60 if (!has_sha1_file(obj->sha1))
61 return;
62 if (parse_tree(tree) < 0)
63 die("bad tree %s", sha1_to_hex(obj->sha1));
65 init_tree_desc(&desc, tree->buffer, tree->size);
66 while (tree_entry(&desc, &entry)) {
67 if (S_ISDIR(entry.mode))
68 mark_tree_uninteresting(lookup_tree(entry.sha1));
69 else
70 mark_blob_uninteresting(lookup_blob(entry.sha1));
74 * We don't care about the tree any more
75 * after it has been marked uninteresting.
77 free(tree->buffer);
78 tree->buffer = NULL;
81 void mark_parents_uninteresting(struct commit *commit)
83 struct commit_list *parents = commit->parents;
85 while (parents) {
86 struct commit *commit = parents->item;
87 if (!(commit->object.flags & UNINTERESTING)) {
88 commit->object.flags |= UNINTERESTING;
91 * Normally we haven't parsed the parent
92 * yet, so we won't have a parent of a parent
93 * here. However, it may turn out that we've
94 * reached this commit some other way (where it
95 * wasn't uninteresting), in which case we need
96 * to mark its parents recursively too..
98 if (commit->parents)
99 mark_parents_uninteresting(commit);
103 * A missing commit is ok iff its parent is marked
104 * uninteresting.
106 * We just mark such a thing parsed, so that when
107 * it is popped next time around, we won't be trying
108 * to parse it and get an error.
110 if (!has_sha1_file(commit->object.sha1))
111 commit->object.parsed = 1;
112 parents = parents->next;
116 void add_pending_object(struct rev_info *revs, struct object *obj, const char *name)
118 if (revs->no_walk && (obj->flags & UNINTERESTING))
119 die("object ranges do not make sense when not walking revisions");
120 add_object_array(obj, name, &revs->pending);
121 if (revs->reflog_info && obj->type == OBJ_COMMIT)
122 add_reflog_for_walk(revs->reflog_info,
123 (struct commit *)obj, name);
126 static struct object *get_reference(struct rev_info *revs, const char *name, const unsigned char *sha1, unsigned int flags)
128 struct object *object;
130 object = parse_object(sha1);
131 if (!object)
132 die("bad object %s", name);
133 object->flags |= flags;
134 return object;
137 static struct commit *handle_commit(struct rev_info *revs, struct object *object, const char *name)
139 unsigned long flags = object->flags;
142 * Tag object? Look what it points to..
144 while (object->type == OBJ_TAG) {
145 struct tag *tag = (struct tag *) object;
146 if (revs->tag_objects && !(flags & UNINTERESTING))
147 add_pending_object(revs, object, tag->tag);
148 object = parse_object(tag->tagged->sha1);
149 if (!object)
150 die("bad object %s", sha1_to_hex(tag->tagged->sha1));
154 * Commit object? Just return it, we'll do all the complex
155 * reachability crud.
157 if (object->type == OBJ_COMMIT) {
158 struct commit *commit = (struct commit *)object;
159 if (parse_commit(commit) < 0)
160 die("unable to parse commit %s", name);
161 if (flags & UNINTERESTING) {
162 commit->object.flags |= UNINTERESTING;
163 mark_parents_uninteresting(commit);
164 revs->limited = 1;
166 return commit;
170 * Tree object? Either mark it uniniteresting, or add it
171 * to the list of objects to look at later..
173 if (object->type == OBJ_TREE) {
174 struct tree *tree = (struct tree *)object;
175 if (!revs->tree_objects)
176 return NULL;
177 if (flags & UNINTERESTING) {
178 mark_tree_uninteresting(tree);
179 return NULL;
181 add_pending_object(revs, object, "");
182 return NULL;
186 * Blob object? You know the drill by now..
188 if (object->type == OBJ_BLOB) {
189 struct blob *blob = (struct blob *)object;
190 if (!revs->blob_objects)
191 return NULL;
192 if (flags & UNINTERESTING) {
193 mark_blob_uninteresting(blob);
194 return NULL;
196 add_pending_object(revs, object, "");
197 return NULL;
199 die("%s is unknown object", name);
202 static int everybody_uninteresting(struct commit_list *orig)
204 struct commit_list *list = orig;
205 while (list) {
206 struct commit *commit = list->item;
207 list = list->next;
208 if (commit->object.flags & UNINTERESTING)
209 continue;
210 return 0;
212 return 1;
216 * The goal is to get REV_TREE_NEW as the result only if the
217 * diff consists of all '+' (and no other changes), and
218 * REV_TREE_DIFFERENT otherwise (of course if the trees are
219 * the same we want REV_TREE_SAME). That means that once we
220 * get to REV_TREE_DIFFERENT, we do not have to look any further.
222 static int tree_difference = REV_TREE_SAME;
224 static void file_add_remove(struct diff_options *options,
225 int addremove, unsigned mode,
226 const unsigned char *sha1,
227 const char *base, const char *path)
229 int diff = REV_TREE_DIFFERENT;
232 * Is it an add of a new file? It means that the old tree
233 * didn't have it at all, so we will turn "REV_TREE_SAME" ->
234 * "REV_TREE_NEW", but leave any "REV_TREE_DIFFERENT" alone
235 * (and if it already was "REV_TREE_NEW", we'll keep it
236 * "REV_TREE_NEW" of course).
238 if (addremove == '+') {
239 diff = tree_difference;
240 if (diff != REV_TREE_SAME)
241 return;
242 diff = REV_TREE_NEW;
244 tree_difference = diff;
245 if (tree_difference == REV_TREE_DIFFERENT)
246 options->has_changes = 1;
249 static void file_change(struct diff_options *options,
250 unsigned old_mode, unsigned new_mode,
251 const unsigned char *old_sha1,
252 const unsigned char *new_sha1,
253 const char *base, const char *path)
255 tree_difference = REV_TREE_DIFFERENT;
256 options->has_changes = 1;
259 int rev_compare_tree(struct rev_info *revs, struct tree *t1, struct tree *t2)
261 if (!t1)
262 return REV_TREE_NEW;
263 if (!t2)
264 return REV_TREE_DIFFERENT;
265 tree_difference = REV_TREE_SAME;
266 revs->pruning.has_changes = 0;
267 if (diff_tree_sha1(t1->object.sha1, t2->object.sha1, "",
268 &revs->pruning) < 0)
269 return REV_TREE_DIFFERENT;
270 return tree_difference;
273 int rev_same_tree_as_empty(struct rev_info *revs, struct tree *t1)
275 int retval;
276 void *tree;
277 unsigned long size;
278 struct tree_desc empty, real;
280 if (!t1)
281 return 0;
283 tree = read_object_with_reference(t1->object.sha1, tree_type, &size, NULL);
284 if (!tree)
285 return 0;
286 init_tree_desc(&real, tree, size);
287 init_tree_desc(&empty, "", 0);
289 tree_difference = REV_TREE_SAME;
290 revs->pruning.has_changes = 0;
291 retval = diff_tree(&empty, &real, "", &revs->pruning);
292 free(tree);
294 return retval >= 0 && (tree_difference == REV_TREE_SAME);
297 static void try_to_simplify_commit(struct rev_info *revs, struct commit *commit)
299 struct commit_list **pp, *parent;
300 int tree_changed = 0, tree_same = 0;
302 if (!commit->tree)
303 return;
305 if (!commit->parents) {
306 if (!rev_same_tree_as_empty(revs, commit->tree))
307 commit->object.flags |= TREECHANGE;
308 return;
311 pp = &commit->parents;
312 while ((parent = *pp) != NULL) {
313 struct commit *p = parent->item;
315 parse_commit(p);
316 switch (rev_compare_tree(revs, p->tree, commit->tree)) {
317 case REV_TREE_SAME:
318 tree_same = 1;
319 if (!revs->simplify_history || (p->object.flags & UNINTERESTING)) {
320 /* Even if a merge with an uninteresting
321 * side branch brought the entire change
322 * we are interested in, we do not want
323 * to lose the other branches of this
324 * merge, so we just keep going.
326 pp = &parent->next;
327 continue;
329 parent->next = NULL;
330 commit->parents = parent;
331 return;
333 case REV_TREE_NEW:
334 if (revs->remove_empty_trees &&
335 rev_same_tree_as_empty(revs, p->tree)) {
336 /* We are adding all the specified
337 * paths from this parent, so the
338 * history beyond this parent is not
339 * interesting. Remove its parents
340 * (they are grandparents for us).
341 * IOW, we pretend this parent is a
342 * "root" commit.
344 parse_commit(p);
345 p->parents = NULL;
347 /* fallthrough */
348 case REV_TREE_DIFFERENT:
349 tree_changed = 1;
350 pp = &parent->next;
351 continue;
353 die("bad tree compare for commit %s", sha1_to_hex(commit->object.sha1));
355 if (tree_changed && !tree_same)
356 commit->object.flags |= TREECHANGE;
359 static void add_parents_to_list(struct rev_info *revs, struct commit *commit, struct commit_list **list)
361 struct commit_list *parent = commit->parents;
362 unsigned left_flag;
363 int add, rest;
365 if (commit->object.flags & ADDED)
366 return;
367 commit->object.flags |= ADDED;
370 * If the commit is uninteresting, don't try to
371 * prune parents - we want the maximal uninteresting
372 * set.
374 * Normally we haven't parsed the parent
375 * yet, so we won't have a parent of a parent
376 * here. However, it may turn out that we've
377 * reached this commit some other way (where it
378 * wasn't uninteresting), in which case we need
379 * to mark its parents recursively too..
381 if (commit->object.flags & UNINTERESTING) {
382 while (parent) {
383 struct commit *p = parent->item;
384 parent = parent->next;
385 parse_commit(p);
386 p->object.flags |= UNINTERESTING;
387 if (p->parents)
388 mark_parents_uninteresting(p);
389 if (p->object.flags & SEEN)
390 continue;
391 p->object.flags |= SEEN;
392 insert_by_date(p, list);
394 return;
398 * Ok, the commit wasn't uninteresting. Try to
399 * simplify the commit history and find the parent
400 * that has no differences in the path set if one exists.
402 if (revs->prune_fn)
403 revs->prune_fn(revs, commit);
405 if (revs->no_walk)
406 return;
408 left_flag = (commit->object.flags & SYMMETRIC_LEFT);
410 rest = !revs->first_parent_only;
411 for (parent = commit->parents, add = 1; parent; add = rest) {
412 struct commit *p = parent->item;
414 parent = parent->next;
415 parse_commit(p);
416 p->object.flags |= left_flag;
417 if (p->object.flags & SEEN)
418 continue;
419 p->object.flags |= SEEN;
420 if (add)
421 insert_by_date(p, list);
425 static void limit_list(struct rev_info *revs)
427 struct commit_list *list = revs->commits;
428 struct commit_list *newlist = NULL;
429 struct commit_list **p = &newlist;
431 while (list) {
432 struct commit_list *entry = list;
433 struct commit *commit = list->item;
434 struct object *obj = &commit->object;
436 list = list->next;
437 free(entry);
439 if (revs->max_age != -1 && (commit->date < revs->max_age))
440 obj->flags |= UNINTERESTING;
441 add_parents_to_list(revs, commit, &list);
442 if (obj->flags & UNINTERESTING) {
443 mark_parents_uninteresting(commit);
444 if (everybody_uninteresting(list))
445 break;
446 continue;
448 if (revs->min_age != -1 && (commit->date > revs->min_age))
449 continue;
450 p = &commit_list_insert(commit, p)->next;
452 revs->commits = newlist;
455 struct all_refs_cb {
456 int all_flags;
457 int warned_bad_reflog;
458 struct rev_info *all_revs;
459 const char *name_for_errormsg;
462 static int handle_one_ref(const char *path, const unsigned char *sha1, int flag, void *cb_data)
464 struct all_refs_cb *cb = cb_data;
465 struct object *object = get_reference(cb->all_revs, path, sha1,
466 cb->all_flags);
467 add_pending_object(cb->all_revs, object, path);
468 return 0;
471 static void handle_all(struct rev_info *revs, unsigned flags)
473 struct all_refs_cb cb;
474 cb.all_revs = revs;
475 cb.all_flags = flags;
476 for_each_ref(handle_one_ref, &cb);
479 static void handle_one_reflog_commit(unsigned char *sha1, void *cb_data)
481 struct all_refs_cb *cb = cb_data;
482 if (!is_null_sha1(sha1)) {
483 struct object *o = parse_object(sha1);
484 if (o) {
485 o->flags |= cb->all_flags;
486 add_pending_object(cb->all_revs, o, "");
488 else if (!cb->warned_bad_reflog) {
489 warning("reflog of '%s' references pruned commits",
490 cb->name_for_errormsg);
491 cb->warned_bad_reflog = 1;
496 static int handle_one_reflog_ent(unsigned char *osha1, unsigned char *nsha1,
497 const char *email, unsigned long timestamp, int tz,
498 const char *message, void *cb_data)
500 handle_one_reflog_commit(osha1, cb_data);
501 handle_one_reflog_commit(nsha1, cb_data);
502 return 0;
505 static int handle_one_reflog(const char *path, const unsigned char *sha1, int flag, void *cb_data)
507 struct all_refs_cb *cb = cb_data;
508 cb->warned_bad_reflog = 0;
509 cb->name_for_errormsg = path;
510 for_each_reflog_ent(path, handle_one_reflog_ent, cb_data);
511 return 0;
514 static void handle_reflog(struct rev_info *revs, unsigned flags)
516 struct all_refs_cb cb;
517 cb.all_revs = revs;
518 cb.all_flags = flags;
519 for_each_reflog(handle_one_reflog, &cb);
522 static int add_parents_only(struct rev_info *revs, const char *arg, int flags)
524 unsigned char sha1[20];
525 struct object *it;
526 struct commit *commit;
527 struct commit_list *parents;
529 if (*arg == '^') {
530 flags ^= UNINTERESTING;
531 arg++;
533 if (get_sha1(arg, sha1))
534 return 0;
535 while (1) {
536 it = get_reference(revs, arg, sha1, 0);
537 if (it->type != OBJ_TAG)
538 break;
539 hashcpy(sha1, ((struct tag*)it)->tagged->sha1);
541 if (it->type != OBJ_COMMIT)
542 return 0;
543 commit = (struct commit *)it;
544 for (parents = commit->parents; parents; parents = parents->next) {
545 it = &parents->item->object;
546 it->flags |= flags;
547 add_pending_object(revs, it, arg);
549 return 1;
552 void init_revisions(struct rev_info *revs, const char *prefix)
554 memset(revs, 0, sizeof(*revs));
556 revs->abbrev = DEFAULT_ABBREV;
557 revs->ignore_merges = 1;
558 revs->simplify_history = 1;
559 revs->pruning.recursive = 1;
560 revs->pruning.quiet = 1;
561 revs->pruning.add_remove = file_add_remove;
562 revs->pruning.change = file_change;
563 revs->lifo = 1;
564 revs->dense = 1;
565 revs->prefix = prefix;
566 revs->max_age = -1;
567 revs->min_age = -1;
568 revs->skip_count = -1;
569 revs->max_count = -1;
571 revs->prune_fn = NULL;
572 revs->prune_data = NULL;
574 revs->topo_setter = topo_sort_default_setter;
575 revs->topo_getter = topo_sort_default_getter;
577 revs->commit_format = CMIT_FMT_DEFAULT;
579 diff_setup(&revs->diffopt);
582 static void add_pending_commit_list(struct rev_info *revs,
583 struct commit_list *commit_list,
584 unsigned int flags)
586 while (commit_list) {
587 struct object *object = &commit_list->item->object;
588 object->flags |= flags;
589 add_pending_object(revs, object, sha1_to_hex(object->sha1));
590 commit_list = commit_list->next;
594 static void prepare_show_merge(struct rev_info *revs)
596 struct commit_list *bases;
597 struct commit *head, *other;
598 unsigned char sha1[20];
599 const char **prune = NULL;
600 int i, prune_num = 1; /* counting terminating NULL */
602 if (get_sha1("HEAD", sha1) || !(head = lookup_commit(sha1)))
603 die("--merge without HEAD?");
604 if (get_sha1("MERGE_HEAD", sha1) || !(other = lookup_commit(sha1)))
605 die("--merge without MERGE_HEAD?");
606 add_pending_object(revs, &head->object, "HEAD");
607 add_pending_object(revs, &other->object, "MERGE_HEAD");
608 bases = get_merge_bases(head, other, 1);
609 while (bases) {
610 struct commit *it = bases->item;
611 struct commit_list *n = bases->next;
612 free(bases);
613 bases = n;
614 it->object.flags |= UNINTERESTING;
615 add_pending_object(revs, &it->object, "(merge-base)");
618 if (!active_nr)
619 read_cache();
620 for (i = 0; i < active_nr; i++) {
621 struct cache_entry *ce = active_cache[i];
622 if (!ce_stage(ce))
623 continue;
624 if (ce_path_match(ce, revs->prune_data)) {
625 prune_num++;
626 prune = xrealloc(prune, sizeof(*prune) * prune_num);
627 prune[prune_num-2] = ce->name;
628 prune[prune_num-1] = NULL;
630 while ((i+1 < active_nr) &&
631 ce_same_name(ce, active_cache[i+1]))
632 i++;
634 revs->prune_data = prune;
637 int handle_revision_arg(const char *arg, struct rev_info *revs,
638 int flags,
639 int cant_be_filename)
641 char *dotdot;
642 struct object *object;
643 unsigned char sha1[20];
644 int local_flags;
646 dotdot = strstr(arg, "..");
647 if (dotdot) {
648 unsigned char from_sha1[20];
649 const char *next = dotdot + 2;
650 const char *this = arg;
651 int symmetric = *next == '.';
652 unsigned int flags_exclude = flags ^ UNINTERESTING;
654 *dotdot = 0;
655 next += symmetric;
657 if (!*next)
658 next = "HEAD";
659 if (dotdot == arg)
660 this = "HEAD";
661 if (!get_sha1(this, from_sha1) &&
662 !get_sha1(next, sha1)) {
663 struct commit *a, *b;
664 struct commit_list *exclude;
666 a = lookup_commit_reference(from_sha1);
667 b = lookup_commit_reference(sha1);
668 if (!a || !b) {
669 die(symmetric ?
670 "Invalid symmetric difference expression %s...%s" :
671 "Invalid revision range %s..%s",
672 arg, next);
675 if (!cant_be_filename) {
676 *dotdot = '.';
677 verify_non_filename(revs->prefix, arg);
680 if (symmetric) {
681 exclude = get_merge_bases(a, b, 1);
682 add_pending_commit_list(revs, exclude,
683 flags_exclude);
684 free_commit_list(exclude);
685 a->object.flags |= flags | SYMMETRIC_LEFT;
686 } else
687 a->object.flags |= flags_exclude;
688 b->object.flags |= flags;
689 add_pending_object(revs, &a->object, this);
690 add_pending_object(revs, &b->object, next);
691 return 0;
693 *dotdot = '.';
695 dotdot = strstr(arg, "^@");
696 if (dotdot && !dotdot[2]) {
697 *dotdot = 0;
698 if (add_parents_only(revs, arg, flags))
699 return 0;
700 *dotdot = '^';
702 dotdot = strstr(arg, "^!");
703 if (dotdot && !dotdot[2]) {
704 *dotdot = 0;
705 if (!add_parents_only(revs, arg, flags ^ UNINTERESTING))
706 *dotdot = '^';
709 local_flags = 0;
710 if (*arg == '^') {
711 local_flags = UNINTERESTING;
712 arg++;
714 if (get_sha1(arg, sha1))
715 return -1;
716 if (!cant_be_filename)
717 verify_non_filename(revs->prefix, arg);
718 object = get_reference(revs, arg, sha1, flags ^ local_flags);
719 add_pending_object(revs, object, arg);
720 return 0;
723 static void add_grep(struct rev_info *revs, const char *ptn, enum grep_pat_token what)
725 if (!revs->grep_filter) {
726 struct grep_opt *opt = xcalloc(1, sizeof(*opt));
727 opt->status_only = 1;
728 opt->pattern_tail = &(opt->pattern_list);
729 opt->regflags = REG_NEWLINE;
730 revs->grep_filter = opt;
732 append_grep_pattern(revs->grep_filter, ptn,
733 "command line", 0, what);
736 static void add_header_grep(struct rev_info *revs, const char *field, const char *pattern)
738 char *pat;
739 const char *prefix;
740 int patlen, fldlen;
742 fldlen = strlen(field);
743 patlen = strlen(pattern);
744 pat = xmalloc(patlen + fldlen + 10);
745 prefix = ".*";
746 if (*pattern == '^') {
747 prefix = "";
748 pattern++;
750 sprintf(pat, "^%s %s%s", field, prefix, pattern);
751 add_grep(revs, pat, GREP_PATTERN_HEAD);
754 static void add_message_grep(struct rev_info *revs, const char *pattern)
756 add_grep(revs, pattern, GREP_PATTERN_BODY);
759 static void add_ignore_packed(struct rev_info *revs, const char *name)
761 int num = ++revs->num_ignore_packed;
763 revs->ignore_packed = xrealloc(revs->ignore_packed,
764 sizeof(const char **) * (num + 1));
765 revs->ignore_packed[num-1] = name;
766 revs->ignore_packed[num] = NULL;
770 * Parse revision information, filling in the "rev_info" structure,
771 * and removing the used arguments from the argument list.
773 * Returns the number of arguments left that weren't recognized
774 * (which are also moved to the head of the argument list)
776 int setup_revisions(int argc, const char **argv, struct rev_info *revs, const char *def)
778 int i, flags, seen_dashdash, show_merge;
779 const char **unrecognized = argv + 1;
780 int left = 1;
781 int all_match = 0;
783 /* First, search for "--" */
784 seen_dashdash = 0;
785 for (i = 1; i < argc; i++) {
786 const char *arg = argv[i];
787 if (strcmp(arg, "--"))
788 continue;
789 argv[i] = NULL;
790 argc = i;
791 revs->prune_data = get_pathspec(revs->prefix, argv + i + 1);
792 seen_dashdash = 1;
793 break;
796 flags = show_merge = 0;
797 for (i = 1; i < argc; i++) {
798 const char *arg = argv[i];
799 if (*arg == '-') {
800 int opts;
801 if (!prefixcmp(arg, "--max-count=")) {
802 revs->max_count = atoi(arg + 12);
803 continue;
805 if (!prefixcmp(arg, "--skip=")) {
806 revs->skip_count = atoi(arg + 7);
807 continue;
809 /* accept -<digit>, like traditional "head" */
810 if ((*arg == '-') && isdigit(arg[1])) {
811 revs->max_count = atoi(arg + 1);
812 continue;
814 if (!strcmp(arg, "-n")) {
815 if (argc <= i + 1)
816 die("-n requires an argument");
817 revs->max_count = atoi(argv[++i]);
818 continue;
820 if (!prefixcmp(arg, "-n")) {
821 revs->max_count = atoi(arg + 2);
822 continue;
824 if (!prefixcmp(arg, "--max-age=")) {
825 revs->max_age = atoi(arg + 10);
826 continue;
828 if (!prefixcmp(arg, "--since=")) {
829 revs->max_age = approxidate(arg + 8);
830 continue;
832 if (!prefixcmp(arg, "--after=")) {
833 revs->max_age = approxidate(arg + 8);
834 continue;
836 if (!prefixcmp(arg, "--min-age=")) {
837 revs->min_age = atoi(arg + 10);
838 continue;
840 if (!prefixcmp(arg, "--before=")) {
841 revs->min_age = approxidate(arg + 9);
842 continue;
844 if (!prefixcmp(arg, "--until=")) {
845 revs->min_age = approxidate(arg + 8);
846 continue;
848 if (!strcmp(arg, "--all")) {
849 handle_all(revs, flags);
850 continue;
852 if (!strcmp(arg, "--first-parent")) {
853 revs->first_parent_only = 1;
854 continue;
856 if (!strcmp(arg, "--reflog")) {
857 handle_reflog(revs, flags);
858 continue;
860 if (!strcmp(arg, "-g") ||
861 !strcmp(arg, "--walk-reflogs")) {
862 init_reflog_walk(&revs->reflog_info);
863 continue;
865 if (!strcmp(arg, "--not")) {
866 flags ^= UNINTERESTING;
867 continue;
869 if (!strcmp(arg, "--default")) {
870 if (++i >= argc)
871 die("bad --default argument");
872 def = argv[i];
873 continue;
875 if (!strcmp(arg, "--merge")) {
876 show_merge = 1;
877 continue;
879 if (!strcmp(arg, "--topo-order")) {
880 revs->topo_order = 1;
881 continue;
883 if (!strcmp(arg, "--date-order")) {
884 revs->lifo = 0;
885 revs->topo_order = 1;
886 continue;
888 if (!strcmp(arg, "--parents")) {
889 revs->parents = 1;
890 continue;
892 if (!strcmp(arg, "--dense")) {
893 revs->dense = 1;
894 continue;
896 if (!strcmp(arg, "--sparse")) {
897 revs->dense = 0;
898 continue;
900 if (!strcmp(arg, "--remove-empty")) {
901 revs->remove_empty_trees = 1;
902 continue;
904 if (!strcmp(arg, "--no-merges")) {
905 revs->no_merges = 1;
906 continue;
908 if (!strcmp(arg, "--boundary")) {
909 revs->boundary = 1;
910 continue;
912 if (!strcmp(arg, "--left-right")) {
913 revs->left_right = 1;
914 continue;
916 if (!strcmp(arg, "--objects")) {
917 revs->tag_objects = 1;
918 revs->tree_objects = 1;
919 revs->blob_objects = 1;
920 continue;
922 if (!strcmp(arg, "--objects-edge")) {
923 revs->tag_objects = 1;
924 revs->tree_objects = 1;
925 revs->blob_objects = 1;
926 revs->edge_hint = 1;
927 continue;
929 if (!strcmp(arg, "--unpacked")) {
930 revs->unpacked = 1;
931 free(revs->ignore_packed);
932 revs->ignore_packed = NULL;
933 revs->num_ignore_packed = 0;
934 continue;
936 if (!prefixcmp(arg, "--unpacked=")) {
937 revs->unpacked = 1;
938 add_ignore_packed(revs, arg+11);
939 continue;
941 if (!strcmp(arg, "-r")) {
942 revs->diff = 1;
943 revs->diffopt.recursive = 1;
944 continue;
946 if (!strcmp(arg, "-t")) {
947 revs->diff = 1;
948 revs->diffopt.recursive = 1;
949 revs->diffopt.tree_in_recursive = 1;
950 continue;
952 if (!strcmp(arg, "-m")) {
953 revs->ignore_merges = 0;
954 continue;
956 if (!strcmp(arg, "-c")) {
957 revs->diff = 1;
958 revs->dense_combined_merges = 0;
959 revs->combine_merges = 1;
960 continue;
962 if (!strcmp(arg, "--cc")) {
963 revs->diff = 1;
964 revs->dense_combined_merges = 1;
965 revs->combine_merges = 1;
966 continue;
968 if (!strcmp(arg, "-v")) {
969 revs->verbose_header = 1;
970 continue;
972 if (!prefixcmp(arg, "--pretty")) {
973 revs->verbose_header = 1;
974 revs->commit_format = get_commit_format(arg+8);
975 continue;
977 if (!strcmp(arg, "--root")) {
978 revs->show_root_diff = 1;
979 continue;
981 if (!strcmp(arg, "--no-commit-id")) {
982 revs->no_commit_id = 1;
983 continue;
985 if (!strcmp(arg, "--always")) {
986 revs->always_show_header = 1;
987 continue;
989 if (!strcmp(arg, "--no-abbrev")) {
990 revs->abbrev = 0;
991 continue;
993 if (!strcmp(arg, "--abbrev")) {
994 revs->abbrev = DEFAULT_ABBREV;
995 continue;
997 if (!prefixcmp(arg, "--abbrev=")) {
998 revs->abbrev = strtoul(arg + 9, NULL, 10);
999 if (revs->abbrev < MINIMUM_ABBREV)
1000 revs->abbrev = MINIMUM_ABBREV;
1001 else if (revs->abbrev > 40)
1002 revs->abbrev = 40;
1003 continue;
1005 if (!strcmp(arg, "--abbrev-commit")) {
1006 revs->abbrev_commit = 1;
1007 continue;
1009 if (!strcmp(arg, "--full-diff")) {
1010 revs->diff = 1;
1011 revs->full_diff = 1;
1012 continue;
1014 if (!strcmp(arg, "--full-history")) {
1015 revs->simplify_history = 0;
1016 continue;
1018 if (!strcmp(arg, "--relative-date")) {
1019 revs->relative_date = 1;
1020 continue;
1024 * Grepping the commit log
1026 if (!prefixcmp(arg, "--author=")) {
1027 add_header_grep(revs, "author", arg+9);
1028 continue;
1030 if (!prefixcmp(arg, "--committer=")) {
1031 add_header_grep(revs, "committer", arg+12);
1032 continue;
1034 if (!prefixcmp(arg, "--grep=")) {
1035 add_message_grep(revs, arg+7);
1036 continue;
1038 if (!strcmp(arg, "--all-match")) {
1039 all_match = 1;
1040 continue;
1042 if (!prefixcmp(arg, "--encoding=")) {
1043 arg += 11;
1044 if (strcmp(arg, "none"))
1045 git_log_output_encoding = xstrdup(arg);
1046 else
1047 git_log_output_encoding = "";
1048 continue;
1050 if (!strcmp(arg, "--reverse")) {
1051 revs->reverse ^= 1;
1052 continue;
1055 opts = diff_opt_parse(&revs->diffopt, argv+i, argc-i);
1056 if (opts > 0) {
1057 revs->diff = 1;
1058 i += opts - 1;
1059 continue;
1061 *unrecognized++ = arg;
1062 left++;
1063 continue;
1066 if (handle_revision_arg(arg, revs, flags, seen_dashdash)) {
1067 int j;
1068 if (seen_dashdash || *arg == '^')
1069 die("bad revision '%s'", arg);
1071 /* If we didn't have a "--":
1072 * (1) all filenames must exist;
1073 * (2) all rev-args must not be interpretable
1074 * as a valid filename.
1075 * but the latter we have checked in the main loop.
1077 for (j = i; j < argc; j++)
1078 verify_filename(revs->prefix, argv[j]);
1080 revs->prune_data = get_pathspec(revs->prefix,
1081 argv + i);
1082 break;
1086 if (show_merge)
1087 prepare_show_merge(revs);
1088 if (def && !revs->pending.nr) {
1089 unsigned char sha1[20];
1090 struct object *object;
1091 if (get_sha1(def, sha1))
1092 die("bad default revision '%s'", def);
1093 object = get_reference(revs, def, sha1, 0);
1094 add_pending_object(revs, object, def);
1097 if (revs->topo_order)
1098 revs->limited = 1;
1100 if (revs->prune_data) {
1101 diff_tree_setup_paths(revs->prune_data, &revs->pruning);
1102 revs->prune_fn = try_to_simplify_commit;
1103 if (!revs->full_diff)
1104 diff_tree_setup_paths(revs->prune_data, &revs->diffopt);
1106 if (revs->combine_merges) {
1107 revs->ignore_merges = 0;
1108 if (revs->dense_combined_merges && !revs->diffopt.output_format)
1109 revs->diffopt.output_format = DIFF_FORMAT_PATCH;
1111 revs->diffopt.abbrev = revs->abbrev;
1112 if (diff_setup_done(&revs->diffopt) < 0)
1113 die("diff_setup_done failed");
1115 if (revs->grep_filter) {
1116 revs->grep_filter->all_match = all_match;
1117 compile_grep_patterns(revs->grep_filter);
1120 return left;
1123 void prepare_revision_walk(struct rev_info *revs)
1125 int nr = revs->pending.nr;
1126 struct object_array_entry *e, *list;
1128 e = list = revs->pending.objects;
1129 revs->pending.nr = 0;
1130 revs->pending.alloc = 0;
1131 revs->pending.objects = NULL;
1132 while (--nr >= 0) {
1133 struct commit *commit = handle_commit(revs, e->item, e->name);
1134 if (commit) {
1135 if (!(commit->object.flags & SEEN)) {
1136 commit->object.flags |= SEEN;
1137 insert_by_date(commit, &revs->commits);
1140 e++;
1142 free(list);
1144 if (revs->no_walk)
1145 return;
1146 if (revs->limited)
1147 limit_list(revs);
1148 if (revs->topo_order)
1149 sort_in_topological_order_fn(&revs->commits, revs->lifo,
1150 revs->topo_setter,
1151 revs->topo_getter);
1154 static int rewrite_one(struct rev_info *revs, struct commit **pp)
1156 for (;;) {
1157 struct commit *p = *pp;
1158 if (!revs->limited)
1159 add_parents_to_list(revs, p, &revs->commits);
1160 if (p->parents && p->parents->next)
1161 return 0;
1162 if (p->object.flags & (TREECHANGE | UNINTERESTING))
1163 return 0;
1164 if (!p->parents)
1165 return -1;
1166 *pp = p->parents->item;
1170 static void rewrite_parents(struct rev_info *revs, struct commit *commit)
1172 struct commit_list **pp = &commit->parents;
1173 while (*pp) {
1174 struct commit_list *parent = *pp;
1175 if (rewrite_one(revs, &parent->item) < 0) {
1176 *pp = parent->next;
1177 continue;
1179 pp = &parent->next;
1183 static int commit_match(struct commit *commit, struct rev_info *opt)
1185 if (!opt->grep_filter)
1186 return 1;
1187 return grep_buffer(opt->grep_filter,
1188 NULL, /* we say nothing, not even filename */
1189 commit->buffer, strlen(commit->buffer));
1192 static struct commit *get_revision_1(struct rev_info *revs)
1194 if (!revs->commits)
1195 return NULL;
1197 do {
1198 struct commit_list *entry = revs->commits;
1199 struct commit *commit = entry->item;
1201 revs->commits = entry->next;
1202 free(entry);
1204 if (revs->reflog_info)
1205 fake_reflog_parent(revs->reflog_info, commit);
1208 * If we haven't done the list limiting, we need to look at
1209 * the parents here. We also need to do the date-based limiting
1210 * that we'd otherwise have done in limit_list().
1212 if (!revs->limited) {
1213 if (revs->max_age != -1 &&
1214 (commit->date < revs->max_age))
1215 continue;
1216 add_parents_to_list(revs, commit, &revs->commits);
1218 if (commit->object.flags & SHOWN)
1219 continue;
1221 if (revs->unpacked && has_sha1_pack(commit->object.sha1,
1222 revs->ignore_packed))
1223 continue;
1225 if (commit->object.flags & UNINTERESTING)
1226 continue;
1227 if (revs->min_age != -1 && (commit->date > revs->min_age))
1228 continue;
1229 if (revs->no_merges &&
1230 commit->parents && commit->parents->next)
1231 continue;
1232 if (!commit_match(commit, revs))
1233 continue;
1234 if (revs->prune_fn && revs->dense) {
1235 /* Commit without changes? */
1236 if (!(commit->object.flags & TREECHANGE)) {
1237 /* drop merges unless we want parenthood */
1238 if (!revs->parents)
1239 continue;
1240 /* non-merge - always ignore it */
1241 if (!commit->parents || !commit->parents->next)
1242 continue;
1244 if (revs->parents)
1245 rewrite_parents(revs, commit);
1247 return commit;
1248 } while (revs->commits);
1249 return NULL;
1252 static void gc_boundary(struct object_array *array)
1254 unsigned nr = array->nr;
1255 unsigned alloc = array->alloc;
1256 struct object_array_entry *objects = array->objects;
1258 if (alloc <= nr) {
1259 unsigned i, j;
1260 for (i = j = 0; i < nr; i++) {
1261 if (objects[i].item->flags & SHOWN)
1262 continue;
1263 if (i != j)
1264 objects[j] = objects[i];
1265 j++;
1267 for (i = j; i < nr; i++)
1268 objects[i].item = NULL;
1269 array->nr = j;
1273 struct commit *get_revision(struct rev_info *revs)
1275 struct commit *c = NULL;
1276 struct commit_list *l;
1278 if (revs->boundary == 2) {
1279 unsigned i;
1280 struct object_array *array = &revs->boundary_commits;
1281 struct object_array_entry *objects = array->objects;
1282 for (i = 0; i < array->nr; i++) {
1283 c = (struct commit *)(objects[i].item);
1284 if (!c)
1285 continue;
1286 if (!(c->object.flags & CHILD_SHOWN))
1287 continue;
1288 if (!(c->object.flags & SHOWN))
1289 break;
1291 if (array->nr <= i)
1292 return NULL;
1294 c->object.flags |= SHOWN | BOUNDARY;
1295 return c;
1298 if (revs->reverse) {
1299 int limit = -1;
1301 if (0 <= revs->max_count) {
1302 limit = revs->max_count;
1303 if (0 < revs->skip_count)
1304 limit += revs->skip_count;
1306 l = NULL;
1307 while ((c = get_revision_1(revs))) {
1308 commit_list_insert(c, &l);
1309 if ((0 < limit) && !--limit)
1310 break;
1312 revs->commits = l;
1313 revs->reverse = 0;
1314 revs->max_count = -1;
1315 c = NULL;
1319 * Now pick up what they want to give us
1321 c = get_revision_1(revs);
1322 if (c) {
1323 while (0 < revs->skip_count) {
1324 revs->skip_count--;
1325 c = get_revision_1(revs);
1326 if (!c)
1327 break;
1332 * Check the max_count.
1334 switch (revs->max_count) {
1335 case -1:
1336 break;
1337 case 0:
1338 c = NULL;
1339 break;
1340 default:
1341 revs->max_count--;
1344 if (c)
1345 c->object.flags |= SHOWN;
1347 if (!revs->boundary) {
1348 return c;
1351 if (!c) {
1353 * get_revision_1() runs out the commits, and
1354 * we are done computing the boundaries.
1355 * switch to boundary commits output mode.
1357 revs->boundary = 2;
1358 return get_revision(revs);
1362 * boundary commits are the commits that are parents of the
1363 * ones we got from get_revision_1() but they themselves are
1364 * not returned from get_revision_1(). Before returning
1365 * 'c', we need to mark its parents that they could be boundaries.
1368 for (l = c->parents; l; l = l->next) {
1369 struct object *p;
1370 p = &(l->item->object);
1371 if (p->flags & (CHILD_SHOWN | SHOWN))
1372 continue;
1373 p->flags |= CHILD_SHOWN;
1374 gc_boundary(&revs->boundary_commits);
1375 add_object_array(p, NULL, &revs->boundary_commits);
1378 return c;