read_cache_from(): small simplification
[git/dscho.git] / revision.c
blob49bd29225bd3be8b06cf4316e06761d5d3d6c011
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"
11 #include "patch-ids.h"
13 static char *path_name(struct name_path *path, const char *name)
15 struct name_path *p;
16 char *n, *m;
17 int nlen = strlen(name);
18 int len = nlen + 1;
20 for (p = path; p; p = p->up) {
21 if (p->elem_len)
22 len += p->elem_len + 1;
24 n = xmalloc(len);
25 m = n + len - (nlen + 1);
26 strcpy(m, name);
27 for (p = path; p; p = p->up) {
28 if (p->elem_len) {
29 m -= p->elem_len + 1;
30 memcpy(m, p->elem, p->elem_len);
31 m[p->elem_len] = '/';
34 return n;
37 void add_object(struct object *obj,
38 struct object_array *p,
39 struct name_path *path,
40 const char *name)
42 add_object_array(obj, path_name(path, name), p);
45 static void mark_blob_uninteresting(struct blob *blob)
47 if (blob->object.flags & UNINTERESTING)
48 return;
49 blob->object.flags |= UNINTERESTING;
52 void mark_tree_uninteresting(struct tree *tree)
54 struct tree_desc desc;
55 struct name_entry entry;
56 struct object *obj = &tree->object;
58 if (obj->flags & UNINTERESTING)
59 return;
60 obj->flags |= UNINTERESTING;
61 if (!has_sha1_file(obj->sha1))
62 return;
63 if (parse_tree(tree) < 0)
64 die("bad tree %s", sha1_to_hex(obj->sha1));
66 init_tree_desc(&desc, tree->buffer, tree->size);
67 while (tree_entry(&desc, &entry)) {
68 if (S_ISDIR(entry.mode))
69 mark_tree_uninteresting(lookup_tree(entry.sha1));
70 else
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.
78 free(tree->buffer);
79 tree->buffer = NULL;
82 void mark_parents_uninteresting(struct commit *commit)
84 struct commit_list *parents = commit->parents;
86 while (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..
99 if (commit->parents)
100 mark_parents_uninteresting(commit);
104 * A missing commit is ok iff its parent is marked
105 * uninteresting.
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 add_pending_object_with_mode(revs, obj, name, S_IFINVALID);
122 void add_pending_object_with_mode(struct rev_info *revs, struct object *obj, const char *name, unsigned mode)
124 if (revs->no_walk && (obj->flags & UNINTERESTING))
125 die("object ranges do not make sense when not walking revisions");
126 add_object_array_with_mode(obj, name, &revs->pending, mode);
127 if (revs->reflog_info && obj->type == OBJ_COMMIT)
128 add_reflog_for_walk(revs->reflog_info,
129 (struct commit *)obj, name);
132 static struct object *get_reference(struct rev_info *revs, const char *name, const unsigned char *sha1, unsigned int flags)
134 struct object *object;
136 object = parse_object(sha1);
137 if (!object)
138 die("bad object %s", name);
139 object->flags |= flags;
140 return object;
143 static struct commit *handle_commit(struct rev_info *revs, struct object *object, const char *name)
145 unsigned long flags = object->flags;
148 * Tag object? Look what it points to..
150 while (object->type == OBJ_TAG) {
151 struct tag *tag = (struct tag *) object;
152 if (revs->tag_objects && !(flags & UNINTERESTING))
153 add_pending_object(revs, object, tag->tag);
154 object = parse_object(tag->tagged->sha1);
155 if (!object)
156 die("bad object %s", sha1_to_hex(tag->tagged->sha1));
160 * Commit object? Just return it, we'll do all the complex
161 * reachability crud.
163 if (object->type == OBJ_COMMIT) {
164 struct commit *commit = (struct commit *)object;
165 if (parse_commit(commit) < 0)
166 die("unable to parse commit %s", name);
167 if (flags & UNINTERESTING) {
168 commit->object.flags |= UNINTERESTING;
169 mark_parents_uninteresting(commit);
170 revs->limited = 1;
172 return commit;
176 * Tree object? Either mark it uniniteresting, or add it
177 * to the list of objects to look at later..
179 if (object->type == OBJ_TREE) {
180 struct tree *tree = (struct tree *)object;
181 if (!revs->tree_objects)
182 return NULL;
183 if (flags & UNINTERESTING) {
184 mark_tree_uninteresting(tree);
185 return NULL;
187 add_pending_object(revs, object, "");
188 return NULL;
192 * Blob object? You know the drill by now..
194 if (object->type == OBJ_BLOB) {
195 struct blob *blob = (struct blob *)object;
196 if (!revs->blob_objects)
197 return NULL;
198 if (flags & UNINTERESTING) {
199 mark_blob_uninteresting(blob);
200 return NULL;
202 add_pending_object(revs, object, "");
203 return NULL;
205 die("%s is unknown object", name);
208 static int everybody_uninteresting(struct commit_list *orig)
210 struct commit_list *list = orig;
211 while (list) {
212 struct commit *commit = list->item;
213 list = list->next;
214 if (commit->object.flags & UNINTERESTING)
215 continue;
216 return 0;
218 return 1;
222 * The goal is to get REV_TREE_NEW as the result only if the
223 * diff consists of all '+' (and no other changes), and
224 * REV_TREE_DIFFERENT otherwise (of course if the trees are
225 * the same we want REV_TREE_SAME). That means that once we
226 * get to REV_TREE_DIFFERENT, we do not have to look any further.
228 static int tree_difference = REV_TREE_SAME;
230 static void file_add_remove(struct diff_options *options,
231 int addremove, unsigned mode,
232 const unsigned char *sha1,
233 const char *base, const char *path)
235 int diff = REV_TREE_DIFFERENT;
238 * Is it an add of a new file? It means that the old tree
239 * didn't have it at all, so we will turn "REV_TREE_SAME" ->
240 * "REV_TREE_NEW", but leave any "REV_TREE_DIFFERENT" alone
241 * (and if it already was "REV_TREE_NEW", we'll keep it
242 * "REV_TREE_NEW" of course).
244 if (addremove == '+') {
245 diff = tree_difference;
246 if (diff != REV_TREE_SAME)
247 return;
248 diff = REV_TREE_NEW;
250 tree_difference = diff;
251 if (tree_difference == REV_TREE_DIFFERENT)
252 options->has_changes = 1;
255 static void file_change(struct diff_options *options,
256 unsigned old_mode, unsigned new_mode,
257 const unsigned char *old_sha1,
258 const unsigned char *new_sha1,
259 const char *base, const char *path)
261 tree_difference = REV_TREE_DIFFERENT;
262 options->has_changes = 1;
265 int rev_compare_tree(struct rev_info *revs, struct tree *t1, struct tree *t2)
267 if (!t1)
268 return REV_TREE_NEW;
269 if (!t2)
270 return REV_TREE_DIFFERENT;
271 tree_difference = REV_TREE_SAME;
272 revs->pruning.has_changes = 0;
273 if (diff_tree_sha1(t1->object.sha1, t2->object.sha1, "",
274 &revs->pruning) < 0)
275 return REV_TREE_DIFFERENT;
276 return tree_difference;
279 int rev_same_tree_as_empty(struct rev_info *revs, struct tree *t1)
281 int retval;
282 void *tree;
283 unsigned long size;
284 struct tree_desc empty, real;
286 if (!t1)
287 return 0;
289 tree = read_object_with_reference(t1->object.sha1, tree_type, &size, NULL);
290 if (!tree)
291 return 0;
292 init_tree_desc(&real, tree, size);
293 init_tree_desc(&empty, "", 0);
295 tree_difference = REV_TREE_SAME;
296 revs->pruning.has_changes = 0;
297 retval = diff_tree(&empty, &real, "", &revs->pruning);
298 free(tree);
300 return retval >= 0 && (tree_difference == REV_TREE_SAME);
303 static void try_to_simplify_commit(struct rev_info *revs, struct commit *commit)
305 struct commit_list **pp, *parent;
306 int tree_changed = 0, tree_same = 0;
308 if (!commit->tree)
309 return;
311 if (!commit->parents) {
312 if (!rev_same_tree_as_empty(revs, commit->tree))
313 commit->object.flags |= TREECHANGE;
314 return;
317 pp = &commit->parents;
318 while ((parent = *pp) != NULL) {
319 struct commit *p = parent->item;
321 parse_commit(p);
322 switch (rev_compare_tree(revs, p->tree, commit->tree)) {
323 case REV_TREE_SAME:
324 tree_same = 1;
325 if (!revs->simplify_history || (p->object.flags & UNINTERESTING)) {
326 /* Even if a merge with an uninteresting
327 * side branch brought the entire change
328 * we are interested in, we do not want
329 * to lose the other branches of this
330 * merge, so we just keep going.
332 pp = &parent->next;
333 continue;
335 parent->next = NULL;
336 commit->parents = parent;
337 return;
339 case REV_TREE_NEW:
340 if (revs->remove_empty_trees &&
341 rev_same_tree_as_empty(revs, p->tree)) {
342 /* We are adding all the specified
343 * paths from this parent, so the
344 * history beyond this parent is not
345 * interesting. Remove its parents
346 * (they are grandparents for us).
347 * IOW, we pretend this parent is a
348 * "root" commit.
350 parse_commit(p);
351 p->parents = NULL;
353 /* fallthrough */
354 case REV_TREE_DIFFERENT:
355 tree_changed = 1;
356 pp = &parent->next;
357 continue;
359 die("bad tree compare for commit %s", sha1_to_hex(commit->object.sha1));
361 if (tree_changed && !tree_same)
362 commit->object.flags |= TREECHANGE;
365 static void add_parents_to_list(struct rev_info *revs, struct commit *commit, struct commit_list **list)
367 struct commit_list *parent = commit->parents;
368 unsigned left_flag;
369 int add, rest;
371 if (commit->object.flags & ADDED)
372 return;
373 commit->object.flags |= ADDED;
376 * If the commit is uninteresting, don't try to
377 * prune parents - we want the maximal uninteresting
378 * set.
380 * Normally we haven't parsed the parent
381 * yet, so we won't have a parent of a parent
382 * here. However, it may turn out that we've
383 * reached this commit some other way (where it
384 * wasn't uninteresting), in which case we need
385 * to mark its parents recursively too..
387 if (commit->object.flags & UNINTERESTING) {
388 while (parent) {
389 struct commit *p = parent->item;
390 parent = parent->next;
391 parse_commit(p);
392 p->object.flags |= UNINTERESTING;
393 if (p->parents)
394 mark_parents_uninteresting(p);
395 if (p->object.flags & SEEN)
396 continue;
397 p->object.flags |= SEEN;
398 insert_by_date(p, list);
400 return;
404 * Ok, the commit wasn't uninteresting. Try to
405 * simplify the commit history and find the parent
406 * that has no differences in the path set if one exists.
408 if (revs->prune_fn)
409 revs->prune_fn(revs, commit);
411 if (revs->no_walk)
412 return;
414 left_flag = (commit->object.flags & SYMMETRIC_LEFT);
416 rest = !revs->first_parent_only;
417 for (parent = commit->parents, add = 1; parent; add = rest) {
418 struct commit *p = parent->item;
420 parent = parent->next;
421 parse_commit(p);
422 p->object.flags |= left_flag;
423 if (p->object.flags & SEEN)
424 continue;
425 p->object.flags |= SEEN;
426 if (add)
427 insert_by_date(p, list);
431 static void cherry_pick_list(struct commit_list *list)
433 struct commit_list *p;
434 int left_count = 0, right_count = 0;
435 int left_first;
436 struct patch_ids ids;
438 /* First count the commits on the left and on the right */
439 for (p = list; p; p = p->next) {
440 struct commit *commit = p->item;
441 unsigned flags = commit->object.flags;
442 if (flags & BOUNDARY)
444 else if (flags & SYMMETRIC_LEFT)
445 left_count++;
446 else
447 right_count++;
450 left_first = left_count < right_count;
451 init_patch_ids(&ids);
453 /* Compute patch-ids for one side */
454 for (p = list; p; p = p->next) {
455 struct commit *commit = p->item;
456 unsigned flags = commit->object.flags;
458 if (flags & BOUNDARY)
459 continue;
461 * If we have fewer left, left_first is set and we omit
462 * commits on the right branch in this loop. If we have
463 * fewer right, we skip the left ones.
465 if (left_first != !!(flags & SYMMETRIC_LEFT))
466 continue;
467 commit->util = add_commit_patch_id(commit, &ids);
470 /* Check the other side */
471 for (p = list; p; p = p->next) {
472 struct commit *commit = p->item;
473 struct patch_id *id;
474 unsigned flags = commit->object.flags;
476 if (flags & BOUNDARY)
477 continue;
479 * If we have fewer left, left_first is set and we omit
480 * commits on the left branch in this loop.
482 if (left_first == !!(flags & SYMMETRIC_LEFT))
483 continue;
486 * Have we seen the same patch id?
488 id = has_commit_patch_id(commit, &ids);
489 if (!id)
490 continue;
491 id->seen = 1;
492 commit->object.flags |= SHOWN;
495 /* Now check the original side for seen ones */
496 for (p = list; p; p = p->next) {
497 struct commit *commit = p->item;
498 struct patch_id *ent;
500 ent = commit->util;
501 if (!ent)
502 continue;
503 if (ent->seen)
504 commit->object.flags |= SHOWN;
505 commit->util = NULL;
508 free_patch_ids(&ids);
511 static void limit_list(struct rev_info *revs)
513 struct commit_list *list = revs->commits;
514 struct commit_list *newlist = NULL;
515 struct commit_list **p = &newlist;
517 while (list) {
518 struct commit_list *entry = list;
519 struct commit *commit = list->item;
520 struct object *obj = &commit->object;
522 list = list->next;
523 free(entry);
525 if (revs->max_age != -1 && (commit->date < revs->max_age))
526 obj->flags |= UNINTERESTING;
527 add_parents_to_list(revs, commit, &list);
528 if (obj->flags & UNINTERESTING) {
529 mark_parents_uninteresting(commit);
530 if (everybody_uninteresting(list))
531 break;
532 continue;
534 if (revs->min_age != -1 && (commit->date > revs->min_age))
535 continue;
536 p = &commit_list_insert(commit, p)->next;
538 if (revs->cherry_pick)
539 cherry_pick_list(newlist);
541 revs->commits = newlist;
544 struct all_refs_cb {
545 int all_flags;
546 int warned_bad_reflog;
547 struct rev_info *all_revs;
548 const char *name_for_errormsg;
551 static int handle_one_ref(const char *path, const unsigned char *sha1, int flag, void *cb_data)
553 struct all_refs_cb *cb = cb_data;
554 struct object *object = get_reference(cb->all_revs, path, sha1,
555 cb->all_flags);
556 add_pending_object(cb->all_revs, object, path);
557 return 0;
560 static void handle_all(struct rev_info *revs, unsigned flags)
562 struct all_refs_cb cb;
563 cb.all_revs = revs;
564 cb.all_flags = flags;
565 for_each_ref(handle_one_ref, &cb);
568 static void handle_one_reflog_commit(unsigned char *sha1, void *cb_data)
570 struct all_refs_cb *cb = cb_data;
571 if (!is_null_sha1(sha1)) {
572 struct object *o = parse_object(sha1);
573 if (o) {
574 o->flags |= cb->all_flags;
575 add_pending_object(cb->all_revs, o, "");
577 else if (!cb->warned_bad_reflog) {
578 warning("reflog of '%s' references pruned commits",
579 cb->name_for_errormsg);
580 cb->warned_bad_reflog = 1;
585 static int handle_one_reflog_ent(unsigned char *osha1, unsigned char *nsha1,
586 const char *email, unsigned long timestamp, int tz,
587 const char *message, void *cb_data)
589 handle_one_reflog_commit(osha1, cb_data);
590 handle_one_reflog_commit(nsha1, cb_data);
591 return 0;
594 static int handle_one_reflog(const char *path, const unsigned char *sha1, int flag, void *cb_data)
596 struct all_refs_cb *cb = cb_data;
597 cb->warned_bad_reflog = 0;
598 cb->name_for_errormsg = path;
599 for_each_reflog_ent(path, handle_one_reflog_ent, cb_data);
600 return 0;
603 static void handle_reflog(struct rev_info *revs, unsigned flags)
605 struct all_refs_cb cb;
606 cb.all_revs = revs;
607 cb.all_flags = flags;
608 for_each_reflog(handle_one_reflog, &cb);
611 static int add_parents_only(struct rev_info *revs, const char *arg, int flags)
613 unsigned char sha1[20];
614 struct object *it;
615 struct commit *commit;
616 struct commit_list *parents;
618 if (*arg == '^') {
619 flags ^= UNINTERESTING;
620 arg++;
622 if (get_sha1(arg, sha1))
623 return 0;
624 while (1) {
625 it = get_reference(revs, arg, sha1, 0);
626 if (it->type != OBJ_TAG)
627 break;
628 hashcpy(sha1, ((struct tag*)it)->tagged->sha1);
630 if (it->type != OBJ_COMMIT)
631 return 0;
632 commit = (struct commit *)it;
633 for (parents = commit->parents; parents; parents = parents->next) {
634 it = &parents->item->object;
635 it->flags |= flags;
636 add_pending_object(revs, it, arg);
638 return 1;
641 void init_revisions(struct rev_info *revs, const char *prefix)
643 memset(revs, 0, sizeof(*revs));
645 revs->abbrev = DEFAULT_ABBREV;
646 revs->ignore_merges = 1;
647 revs->simplify_history = 1;
648 revs->pruning.recursive = 1;
649 revs->pruning.quiet = 1;
650 revs->pruning.add_remove = file_add_remove;
651 revs->pruning.change = file_change;
652 revs->lifo = 1;
653 revs->dense = 1;
654 revs->prefix = prefix;
655 revs->max_age = -1;
656 revs->min_age = -1;
657 revs->skip_count = -1;
658 revs->max_count = -1;
659 revs->subject_prefix = "PATCH";
661 revs->prune_fn = NULL;
662 revs->prune_data = NULL;
664 revs->topo_setter = topo_sort_default_setter;
665 revs->topo_getter = topo_sort_default_getter;
667 revs->commit_format = CMIT_FMT_DEFAULT;
669 diff_setup(&revs->diffopt);
672 static void add_pending_commit_list(struct rev_info *revs,
673 struct commit_list *commit_list,
674 unsigned int flags)
676 while (commit_list) {
677 struct object *object = &commit_list->item->object;
678 object->flags |= flags;
679 add_pending_object(revs, object, sha1_to_hex(object->sha1));
680 commit_list = commit_list->next;
684 static void prepare_show_merge(struct rev_info *revs)
686 struct commit_list *bases;
687 struct commit *head, *other;
688 unsigned char sha1[20];
689 const char **prune = NULL;
690 int i, prune_num = 1; /* counting terminating NULL */
692 if (get_sha1("HEAD", sha1) || !(head = lookup_commit(sha1)))
693 die("--merge without HEAD?");
694 if (get_sha1("MERGE_HEAD", sha1) || !(other = lookup_commit(sha1)))
695 die("--merge without MERGE_HEAD?");
696 add_pending_object(revs, &head->object, "HEAD");
697 add_pending_object(revs, &other->object, "MERGE_HEAD");
698 bases = get_merge_bases(head, other, 1);
699 while (bases) {
700 struct commit *it = bases->item;
701 struct commit_list *n = bases->next;
702 free(bases);
703 bases = n;
704 it->object.flags |= UNINTERESTING;
705 add_pending_object(revs, &it->object, "(merge-base)");
708 if (!active_nr)
709 read_cache();
710 for (i = 0; i < active_nr; i++) {
711 struct cache_entry *ce = active_cache[i];
712 if (!ce_stage(ce))
713 continue;
714 if (ce_path_match(ce, revs->prune_data)) {
715 prune_num++;
716 prune = xrealloc(prune, sizeof(*prune) * prune_num);
717 prune[prune_num-2] = ce->name;
718 prune[prune_num-1] = NULL;
720 while ((i+1 < active_nr) &&
721 ce_same_name(ce, active_cache[i+1]))
722 i++;
724 revs->prune_data = prune;
727 int handle_revision_arg(const char *arg, struct rev_info *revs,
728 int flags,
729 int cant_be_filename)
731 unsigned mode;
732 char *dotdot;
733 struct object *object;
734 unsigned char sha1[20];
735 int local_flags;
737 dotdot = strstr(arg, "..");
738 if (dotdot) {
739 unsigned char from_sha1[20];
740 const char *next = dotdot + 2;
741 const char *this = arg;
742 int symmetric = *next == '.';
743 unsigned int flags_exclude = flags ^ UNINTERESTING;
745 *dotdot = 0;
746 next += symmetric;
748 if (!*next)
749 next = "HEAD";
750 if (dotdot == arg)
751 this = "HEAD";
752 if (!get_sha1(this, from_sha1) &&
753 !get_sha1(next, sha1)) {
754 struct commit *a, *b;
755 struct commit_list *exclude;
757 a = lookup_commit_reference(from_sha1);
758 b = lookup_commit_reference(sha1);
759 if (!a || !b) {
760 die(symmetric ?
761 "Invalid symmetric difference expression %s...%s" :
762 "Invalid revision range %s..%s",
763 arg, next);
766 if (!cant_be_filename) {
767 *dotdot = '.';
768 verify_non_filename(revs->prefix, arg);
771 if (symmetric) {
772 exclude = get_merge_bases(a, b, 1);
773 add_pending_commit_list(revs, exclude,
774 flags_exclude);
775 free_commit_list(exclude);
776 a->object.flags |= flags | SYMMETRIC_LEFT;
777 } else
778 a->object.flags |= flags_exclude;
779 b->object.flags |= flags;
780 add_pending_object(revs, &a->object, this);
781 add_pending_object(revs, &b->object, next);
782 return 0;
784 *dotdot = '.';
786 dotdot = strstr(arg, "^@");
787 if (dotdot && !dotdot[2]) {
788 *dotdot = 0;
789 if (add_parents_only(revs, arg, flags))
790 return 0;
791 *dotdot = '^';
793 dotdot = strstr(arg, "^!");
794 if (dotdot && !dotdot[2]) {
795 *dotdot = 0;
796 if (!add_parents_only(revs, arg, flags ^ UNINTERESTING))
797 *dotdot = '^';
800 local_flags = 0;
801 if (*arg == '^') {
802 local_flags = UNINTERESTING;
803 arg++;
805 if (get_sha1_with_mode(arg, sha1, &mode))
806 return -1;
807 if (!cant_be_filename)
808 verify_non_filename(revs->prefix, arg);
809 object = get_reference(revs, arg, sha1, flags ^ local_flags);
810 add_pending_object_with_mode(revs, object, arg, mode);
811 return 0;
814 static void add_grep(struct rev_info *revs, const char *ptn, enum grep_pat_token what)
816 if (!revs->grep_filter) {
817 struct grep_opt *opt = xcalloc(1, sizeof(*opt));
818 opt->status_only = 1;
819 opt->pattern_tail = &(opt->pattern_list);
820 opt->regflags = REG_NEWLINE;
821 revs->grep_filter = opt;
823 append_grep_pattern(revs->grep_filter, ptn,
824 "command line", 0, what);
827 static void add_header_grep(struct rev_info *revs, const char *field, const char *pattern)
829 char *pat;
830 const char *prefix;
831 int patlen, fldlen;
833 fldlen = strlen(field);
834 patlen = strlen(pattern);
835 pat = xmalloc(patlen + fldlen + 10);
836 prefix = ".*";
837 if (*pattern == '^') {
838 prefix = "";
839 pattern++;
841 sprintf(pat, "^%s %s%s", field, prefix, pattern);
842 add_grep(revs, pat, GREP_PATTERN_HEAD);
845 static void add_message_grep(struct rev_info *revs, const char *pattern)
847 add_grep(revs, pattern, GREP_PATTERN_BODY);
850 static void add_ignore_packed(struct rev_info *revs, const char *name)
852 int num = ++revs->num_ignore_packed;
854 revs->ignore_packed = xrealloc(revs->ignore_packed,
855 sizeof(const char **) * (num + 1));
856 revs->ignore_packed[num-1] = name;
857 revs->ignore_packed[num] = NULL;
861 * Parse revision information, filling in the "rev_info" structure,
862 * and removing the used arguments from the argument list.
864 * Returns the number of arguments left that weren't recognized
865 * (which are also moved to the head of the argument list)
867 int setup_revisions(int argc, const char **argv, struct rev_info *revs, const char *def)
869 int i, flags, seen_dashdash, show_merge;
870 const char **unrecognized = argv + 1;
871 int left = 1;
872 int all_match = 0;
874 /* First, search for "--" */
875 seen_dashdash = 0;
876 for (i = 1; i < argc; i++) {
877 const char *arg = argv[i];
878 if (strcmp(arg, "--"))
879 continue;
880 argv[i] = NULL;
881 argc = i;
882 revs->prune_data = get_pathspec(revs->prefix, argv + i + 1);
883 seen_dashdash = 1;
884 break;
887 flags = show_merge = 0;
888 for (i = 1; i < argc; i++) {
889 const char *arg = argv[i];
890 if (*arg == '-') {
891 int opts;
892 if (!prefixcmp(arg, "--max-count=")) {
893 revs->max_count = atoi(arg + 12);
894 continue;
896 if (!prefixcmp(arg, "--skip=")) {
897 revs->skip_count = atoi(arg + 7);
898 continue;
900 /* accept -<digit>, like traditional "head" */
901 if ((*arg == '-') && isdigit(arg[1])) {
902 revs->max_count = atoi(arg + 1);
903 continue;
905 if (!strcmp(arg, "-n")) {
906 if (argc <= i + 1)
907 die("-n requires an argument");
908 revs->max_count = atoi(argv[++i]);
909 continue;
911 if (!prefixcmp(arg, "-n")) {
912 revs->max_count = atoi(arg + 2);
913 continue;
915 if (!prefixcmp(arg, "--max-age=")) {
916 revs->max_age = atoi(arg + 10);
917 continue;
919 if (!prefixcmp(arg, "--since=")) {
920 revs->max_age = approxidate(arg + 8);
921 continue;
923 if (!prefixcmp(arg, "--after=")) {
924 revs->max_age = approxidate(arg + 8);
925 continue;
927 if (!prefixcmp(arg, "--min-age=")) {
928 revs->min_age = atoi(arg + 10);
929 continue;
931 if (!prefixcmp(arg, "--before=")) {
932 revs->min_age = approxidate(arg + 9);
933 continue;
935 if (!prefixcmp(arg, "--until=")) {
936 revs->min_age = approxidate(arg + 8);
937 continue;
939 if (!strcmp(arg, "--all")) {
940 handle_all(revs, flags);
941 continue;
943 if (!strcmp(arg, "--first-parent")) {
944 revs->first_parent_only = 1;
945 continue;
947 if (!strcmp(arg, "--reflog")) {
948 handle_reflog(revs, flags);
949 continue;
951 if (!strcmp(arg, "-g") ||
952 !strcmp(arg, "--walk-reflogs")) {
953 init_reflog_walk(&revs->reflog_info);
954 continue;
956 if (!strcmp(arg, "--not")) {
957 flags ^= UNINTERESTING;
958 continue;
960 if (!strcmp(arg, "--default")) {
961 if (++i >= argc)
962 die("bad --default argument");
963 def = argv[i];
964 continue;
966 if (!strcmp(arg, "--merge")) {
967 show_merge = 1;
968 continue;
970 if (!strcmp(arg, "--topo-order")) {
971 revs->topo_order = 1;
972 continue;
974 if (!strcmp(arg, "--date-order")) {
975 revs->lifo = 0;
976 revs->topo_order = 1;
977 continue;
979 if (!strcmp(arg, "--parents")) {
980 revs->parents = 1;
981 continue;
983 if (!strcmp(arg, "--dense")) {
984 revs->dense = 1;
985 continue;
987 if (!strcmp(arg, "--sparse")) {
988 revs->dense = 0;
989 continue;
991 if (!strcmp(arg, "--remove-empty")) {
992 revs->remove_empty_trees = 1;
993 continue;
995 if (!strcmp(arg, "--no-merges")) {
996 revs->no_merges = 1;
997 continue;
999 if (!strcmp(arg, "--boundary")) {
1000 revs->boundary = 1;
1001 continue;
1003 if (!strcmp(arg, "--left-right")) {
1004 revs->left_right = 1;
1005 continue;
1007 if (!strcmp(arg, "--cherry-pick")) {
1008 revs->cherry_pick = 1;
1009 continue;
1011 if (!strcmp(arg, "--objects")) {
1012 revs->tag_objects = 1;
1013 revs->tree_objects = 1;
1014 revs->blob_objects = 1;
1015 continue;
1017 if (!strcmp(arg, "--objects-edge")) {
1018 revs->tag_objects = 1;
1019 revs->tree_objects = 1;
1020 revs->blob_objects = 1;
1021 revs->edge_hint = 1;
1022 continue;
1024 if (!strcmp(arg, "--unpacked")) {
1025 revs->unpacked = 1;
1026 free(revs->ignore_packed);
1027 revs->ignore_packed = NULL;
1028 revs->num_ignore_packed = 0;
1029 continue;
1031 if (!prefixcmp(arg, "--unpacked=")) {
1032 revs->unpacked = 1;
1033 add_ignore_packed(revs, arg+11);
1034 continue;
1036 if (!strcmp(arg, "-r")) {
1037 revs->diff = 1;
1038 revs->diffopt.recursive = 1;
1039 continue;
1041 if (!strcmp(arg, "-t")) {
1042 revs->diff = 1;
1043 revs->diffopt.recursive = 1;
1044 revs->diffopt.tree_in_recursive = 1;
1045 continue;
1047 if (!strcmp(arg, "-m")) {
1048 revs->ignore_merges = 0;
1049 continue;
1051 if (!strcmp(arg, "-c")) {
1052 revs->diff = 1;
1053 revs->dense_combined_merges = 0;
1054 revs->combine_merges = 1;
1055 continue;
1057 if (!strcmp(arg, "--cc")) {
1058 revs->diff = 1;
1059 revs->dense_combined_merges = 1;
1060 revs->combine_merges = 1;
1061 continue;
1063 if (!strcmp(arg, "-v")) {
1064 revs->verbose_header = 1;
1065 continue;
1067 if (!prefixcmp(arg, "--pretty")) {
1068 revs->verbose_header = 1;
1069 revs->commit_format = get_commit_format(arg+8);
1070 continue;
1072 if (!strcmp(arg, "--root")) {
1073 revs->show_root_diff = 1;
1074 continue;
1076 if (!strcmp(arg, "--no-commit-id")) {
1077 revs->no_commit_id = 1;
1078 continue;
1080 if (!strcmp(arg, "--always")) {
1081 revs->always_show_header = 1;
1082 continue;
1084 if (!strcmp(arg, "--no-abbrev")) {
1085 revs->abbrev = 0;
1086 continue;
1088 if (!strcmp(arg, "--abbrev")) {
1089 revs->abbrev = DEFAULT_ABBREV;
1090 continue;
1092 if (!prefixcmp(arg, "--abbrev=")) {
1093 revs->abbrev = strtoul(arg + 9, NULL, 10);
1094 if (revs->abbrev < MINIMUM_ABBREV)
1095 revs->abbrev = MINIMUM_ABBREV;
1096 else if (revs->abbrev > 40)
1097 revs->abbrev = 40;
1098 continue;
1100 if (!strcmp(arg, "--abbrev-commit")) {
1101 revs->abbrev_commit = 1;
1102 continue;
1104 if (!strcmp(arg, "--full-diff")) {
1105 revs->diff = 1;
1106 revs->full_diff = 1;
1107 continue;
1109 if (!strcmp(arg, "--full-history")) {
1110 revs->simplify_history = 0;
1111 continue;
1113 if (!strcmp(arg, "--relative-date")) {
1114 revs->relative_date = 1;
1115 continue;
1119 * Grepping the commit log
1121 if (!prefixcmp(arg, "--author=")) {
1122 add_header_grep(revs, "author", arg+9);
1123 continue;
1125 if (!prefixcmp(arg, "--committer=")) {
1126 add_header_grep(revs, "committer", arg+12);
1127 continue;
1129 if (!prefixcmp(arg, "--grep=")) {
1130 add_message_grep(revs, arg+7);
1131 continue;
1133 if (!strcmp(arg, "--all-match")) {
1134 all_match = 1;
1135 continue;
1137 if (!prefixcmp(arg, "--encoding=")) {
1138 arg += 11;
1139 if (strcmp(arg, "none"))
1140 git_log_output_encoding = xstrdup(arg);
1141 else
1142 git_log_output_encoding = "";
1143 continue;
1145 if (!strcmp(arg, "--reverse")) {
1146 revs->reverse ^= 1;
1147 continue;
1150 opts = diff_opt_parse(&revs->diffopt, argv+i, argc-i);
1151 if (opts > 0) {
1152 revs->diff = 1;
1153 i += opts - 1;
1154 continue;
1156 *unrecognized++ = arg;
1157 left++;
1158 continue;
1161 if (handle_revision_arg(arg, revs, flags, seen_dashdash)) {
1162 int j;
1163 if (seen_dashdash || *arg == '^')
1164 die("bad revision '%s'", arg);
1166 /* If we didn't have a "--":
1167 * (1) all filenames must exist;
1168 * (2) all rev-args must not be interpretable
1169 * as a valid filename.
1170 * but the latter we have checked in the main loop.
1172 for (j = i; j < argc; j++)
1173 verify_filename(revs->prefix, argv[j]);
1175 revs->prune_data = get_pathspec(revs->prefix,
1176 argv + i);
1177 break;
1181 if (show_merge)
1182 prepare_show_merge(revs);
1183 if (def && !revs->pending.nr) {
1184 unsigned char sha1[20];
1185 struct object *object;
1186 unsigned mode;
1187 if (get_sha1_with_mode(def, sha1, &mode))
1188 die("bad default revision '%s'", def);
1189 object = get_reference(revs, def, sha1, 0);
1190 add_pending_object_with_mode(revs, object, def, mode);
1193 if (revs->topo_order)
1194 revs->limited = 1;
1196 if (revs->prune_data) {
1197 diff_tree_setup_paths(revs->prune_data, &revs->pruning);
1198 revs->prune_fn = try_to_simplify_commit;
1199 if (!revs->full_diff)
1200 diff_tree_setup_paths(revs->prune_data, &revs->diffopt);
1202 if (revs->combine_merges) {
1203 revs->ignore_merges = 0;
1204 if (revs->dense_combined_merges && !revs->diffopt.output_format)
1205 revs->diffopt.output_format = DIFF_FORMAT_PATCH;
1207 revs->diffopt.abbrev = revs->abbrev;
1208 if (diff_setup_done(&revs->diffopt) < 0)
1209 die("diff_setup_done failed");
1211 if (revs->grep_filter) {
1212 revs->grep_filter->all_match = all_match;
1213 compile_grep_patterns(revs->grep_filter);
1216 return left;
1219 void prepare_revision_walk(struct rev_info *revs)
1221 int nr = revs->pending.nr;
1222 struct object_array_entry *e, *list;
1224 e = list = revs->pending.objects;
1225 revs->pending.nr = 0;
1226 revs->pending.alloc = 0;
1227 revs->pending.objects = NULL;
1228 while (--nr >= 0) {
1229 struct commit *commit = handle_commit(revs, e->item, e->name);
1230 if (commit) {
1231 if (!(commit->object.flags & SEEN)) {
1232 commit->object.flags |= SEEN;
1233 insert_by_date(commit, &revs->commits);
1236 e++;
1238 free(list);
1240 if (revs->no_walk)
1241 return;
1242 if (revs->limited)
1243 limit_list(revs);
1244 if (revs->topo_order)
1245 sort_in_topological_order_fn(&revs->commits, revs->lifo,
1246 revs->topo_setter,
1247 revs->topo_getter);
1250 static int rewrite_one(struct rev_info *revs, struct commit **pp)
1252 for (;;) {
1253 struct commit *p = *pp;
1254 if (!revs->limited)
1255 add_parents_to_list(revs, p, &revs->commits);
1256 if (p->parents && p->parents->next)
1257 return 0;
1258 if (p->object.flags & (TREECHANGE | UNINTERESTING))
1259 return 0;
1260 if (!p->parents)
1261 return -1;
1262 *pp = p->parents->item;
1266 static void rewrite_parents(struct rev_info *revs, struct commit *commit)
1268 struct commit_list **pp = &commit->parents;
1269 while (*pp) {
1270 struct commit_list *parent = *pp;
1271 if (rewrite_one(revs, &parent->item) < 0) {
1272 *pp = parent->next;
1273 continue;
1275 pp = &parent->next;
1279 static int commit_match(struct commit *commit, struct rev_info *opt)
1281 if (!opt->grep_filter)
1282 return 1;
1283 return grep_buffer(opt->grep_filter,
1284 NULL, /* we say nothing, not even filename */
1285 commit->buffer, strlen(commit->buffer));
1288 static struct commit *get_revision_1(struct rev_info *revs)
1290 if (!revs->commits)
1291 return NULL;
1293 do {
1294 struct commit_list *entry = revs->commits;
1295 struct commit *commit = entry->item;
1297 revs->commits = entry->next;
1298 free(entry);
1300 if (revs->reflog_info)
1301 fake_reflog_parent(revs->reflog_info, commit);
1304 * If we haven't done the list limiting, we need to look at
1305 * the parents here. We also need to do the date-based limiting
1306 * that we'd otherwise have done in limit_list().
1308 if (!revs->limited) {
1309 if (revs->max_age != -1 &&
1310 (commit->date < revs->max_age))
1311 continue;
1312 add_parents_to_list(revs, commit, &revs->commits);
1314 if (commit->object.flags & SHOWN)
1315 continue;
1317 if (revs->unpacked && has_sha1_pack(commit->object.sha1,
1318 revs->ignore_packed))
1319 continue;
1321 if (commit->object.flags & UNINTERESTING)
1322 continue;
1323 if (revs->min_age != -1 && (commit->date > revs->min_age))
1324 continue;
1325 if (revs->no_merges &&
1326 commit->parents && commit->parents->next)
1327 continue;
1328 if (!commit_match(commit, revs))
1329 continue;
1330 if (revs->prune_fn && revs->dense) {
1331 /* Commit without changes? */
1332 if (!(commit->object.flags & TREECHANGE)) {
1333 /* drop merges unless we want parenthood */
1334 if (!revs->parents)
1335 continue;
1336 /* non-merge - always ignore it */
1337 if (!commit->parents || !commit->parents->next)
1338 continue;
1340 if (revs->parents)
1341 rewrite_parents(revs, commit);
1343 return commit;
1344 } while (revs->commits);
1345 return NULL;
1348 static void gc_boundary(struct object_array *array)
1350 unsigned nr = array->nr;
1351 unsigned alloc = array->alloc;
1352 struct object_array_entry *objects = array->objects;
1354 if (alloc <= nr) {
1355 unsigned i, j;
1356 for (i = j = 0; i < nr; i++) {
1357 if (objects[i].item->flags & SHOWN)
1358 continue;
1359 if (i != j)
1360 objects[j] = objects[i];
1361 j++;
1363 for (i = j; i < nr; i++)
1364 objects[i].item = NULL;
1365 array->nr = j;
1369 struct commit *get_revision(struct rev_info *revs)
1371 struct commit *c = NULL;
1372 struct commit_list *l;
1374 if (revs->boundary == 2) {
1375 unsigned i;
1376 struct object_array *array = &revs->boundary_commits;
1377 struct object_array_entry *objects = array->objects;
1378 for (i = 0; i < array->nr; i++) {
1379 c = (struct commit *)(objects[i].item);
1380 if (!c)
1381 continue;
1382 if (!(c->object.flags & CHILD_SHOWN))
1383 continue;
1384 if (!(c->object.flags & SHOWN))
1385 break;
1387 if (array->nr <= i)
1388 return NULL;
1390 c->object.flags |= SHOWN | BOUNDARY;
1391 return c;
1394 if (revs->reverse) {
1395 int limit = -1;
1397 if (0 <= revs->max_count) {
1398 limit = revs->max_count;
1399 if (0 < revs->skip_count)
1400 limit += revs->skip_count;
1402 l = NULL;
1403 while ((c = get_revision_1(revs))) {
1404 commit_list_insert(c, &l);
1405 if ((0 < limit) && !--limit)
1406 break;
1408 revs->commits = l;
1409 revs->reverse = 0;
1410 revs->max_count = -1;
1411 c = NULL;
1415 * Now pick up what they want to give us
1417 c = get_revision_1(revs);
1418 if (c) {
1419 while (0 < revs->skip_count) {
1420 revs->skip_count--;
1421 c = get_revision_1(revs);
1422 if (!c)
1423 break;
1428 * Check the max_count.
1430 switch (revs->max_count) {
1431 case -1:
1432 break;
1433 case 0:
1434 c = NULL;
1435 break;
1436 default:
1437 revs->max_count--;
1440 if (c)
1441 c->object.flags |= SHOWN;
1443 if (!revs->boundary) {
1444 return c;
1447 if (!c) {
1449 * get_revision_1() runs out the commits, and
1450 * we are done computing the boundaries.
1451 * switch to boundary commits output mode.
1453 revs->boundary = 2;
1454 return get_revision(revs);
1458 * boundary commits are the commits that are parents of the
1459 * ones we got from get_revision_1() but they themselves are
1460 * not returned from get_revision_1(). Before returning
1461 * 'c', we need to mark its parents that they could be boundaries.
1464 for (l = c->parents; l; l = l->next) {
1465 struct object *p;
1466 p = &(l->item->object);
1467 if (p->flags & (CHILD_SHOWN | SHOWN))
1468 continue;
1469 p->flags |= CHILD_SHOWN;
1470 gc_boundary(&revs->boundary_commits);
1471 add_object_array(p, NULL, &revs->boundary_commits);
1474 return c;