Merge branch 'jc/attr'
[git/fastimport.git] / revision.c
blobce70f48ce0880e8b43c3f62cd13bc185bfee8c4b
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 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);
132 if (!object)
133 die("bad object %s", name);
134 object->flags |= flags;
135 return object;
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);
150 if (!object)
151 die("bad object %s", sha1_to_hex(tag->tagged->sha1));
155 * Commit object? Just return it, we'll do all the complex
156 * reachability crud.
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);
165 revs->limited = 1;
167 return 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)
177 return NULL;
178 if (flags & UNINTERESTING) {
179 mark_tree_uninteresting(tree);
180 return NULL;
182 add_pending_object(revs, object, "");
183 return NULL;
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)
192 return NULL;
193 if (flags & UNINTERESTING) {
194 mark_blob_uninteresting(blob);
195 return NULL;
197 add_pending_object(revs, object, "");
198 return NULL;
200 die("%s is unknown object", name);
203 static int everybody_uninteresting(struct commit_list *orig)
205 struct commit_list *list = orig;
206 while (list) {
207 struct commit *commit = list->item;
208 list = list->next;
209 if (commit->object.flags & UNINTERESTING)
210 continue;
211 return 0;
213 return 1;
217 * The goal is to get REV_TREE_NEW as the result only if the
218 * diff consists of all '+' (and no other changes), and
219 * REV_TREE_DIFFERENT otherwise (of course if the trees are
220 * the same we want REV_TREE_SAME). That means that once we
221 * get to REV_TREE_DIFFERENT, we do not have to look any further.
223 static int tree_difference = REV_TREE_SAME;
225 static void file_add_remove(struct diff_options *options,
226 int addremove, unsigned mode,
227 const unsigned char *sha1,
228 const char *base, const char *path)
230 int diff = REV_TREE_DIFFERENT;
233 * Is it an add of a new file? It means that the old tree
234 * didn't have it at all, so we will turn "REV_TREE_SAME" ->
235 * "REV_TREE_NEW", but leave any "REV_TREE_DIFFERENT" alone
236 * (and if it already was "REV_TREE_NEW", we'll keep it
237 * "REV_TREE_NEW" of course).
239 if (addremove == '+') {
240 diff = tree_difference;
241 if (diff != REV_TREE_SAME)
242 return;
243 diff = REV_TREE_NEW;
245 tree_difference = diff;
246 if (tree_difference == REV_TREE_DIFFERENT)
247 options->has_changes = 1;
250 static void file_change(struct diff_options *options,
251 unsigned old_mode, unsigned new_mode,
252 const unsigned char *old_sha1,
253 const unsigned char *new_sha1,
254 const char *base, const char *path)
256 tree_difference = REV_TREE_DIFFERENT;
257 options->has_changes = 1;
260 int rev_compare_tree(struct rev_info *revs, struct tree *t1, struct tree *t2)
262 if (!t1)
263 return REV_TREE_NEW;
264 if (!t2)
265 return REV_TREE_DIFFERENT;
266 tree_difference = REV_TREE_SAME;
267 revs->pruning.has_changes = 0;
268 if (diff_tree_sha1(t1->object.sha1, t2->object.sha1, "",
269 &revs->pruning) < 0)
270 return REV_TREE_DIFFERENT;
271 return tree_difference;
274 int rev_same_tree_as_empty(struct rev_info *revs, struct tree *t1)
276 int retval;
277 void *tree;
278 unsigned long size;
279 struct tree_desc empty, real;
281 if (!t1)
282 return 0;
284 tree = read_object_with_reference(t1->object.sha1, tree_type, &size, NULL);
285 if (!tree)
286 return 0;
287 init_tree_desc(&real, tree, size);
288 init_tree_desc(&empty, "", 0);
290 tree_difference = REV_TREE_SAME;
291 revs->pruning.has_changes = 0;
292 retval = diff_tree(&empty, &real, "", &revs->pruning);
293 free(tree);
295 return retval >= 0 && (tree_difference == REV_TREE_SAME);
298 static void try_to_simplify_commit(struct rev_info *revs, struct commit *commit)
300 struct commit_list **pp, *parent;
301 int tree_changed = 0, tree_same = 0;
303 if (!commit->tree)
304 return;
306 if (!commit->parents) {
307 if (!rev_same_tree_as_empty(revs, commit->tree))
308 commit->object.flags |= TREECHANGE;
309 return;
312 pp = &commit->parents;
313 while ((parent = *pp) != NULL) {
314 struct commit *p = parent->item;
316 parse_commit(p);
317 switch (rev_compare_tree(revs, p->tree, commit->tree)) {
318 case REV_TREE_SAME:
319 tree_same = 1;
320 if (!revs->simplify_history || (p->object.flags & UNINTERESTING)) {
321 /* Even if a merge with an uninteresting
322 * side branch brought the entire change
323 * we are interested in, we do not want
324 * to lose the other branches of this
325 * merge, so we just keep going.
327 pp = &parent->next;
328 continue;
330 parent->next = NULL;
331 commit->parents = parent;
332 return;
334 case REV_TREE_NEW:
335 if (revs->remove_empty_trees &&
336 rev_same_tree_as_empty(revs, p->tree)) {
337 /* We are adding all the specified
338 * paths from this parent, so the
339 * history beyond this parent is not
340 * interesting. Remove its parents
341 * (they are grandparents for us).
342 * IOW, we pretend this parent is a
343 * "root" commit.
345 parse_commit(p);
346 p->parents = NULL;
348 /* fallthrough */
349 case REV_TREE_DIFFERENT:
350 tree_changed = 1;
351 pp = &parent->next;
352 continue;
354 die("bad tree compare for commit %s", sha1_to_hex(commit->object.sha1));
356 if (tree_changed && !tree_same)
357 commit->object.flags |= TREECHANGE;
360 static void add_parents_to_list(struct rev_info *revs, struct commit *commit, struct commit_list **list)
362 struct commit_list *parent = commit->parents;
363 unsigned left_flag;
364 int add, rest;
366 if (commit->object.flags & ADDED)
367 return;
368 commit->object.flags |= ADDED;
371 * If the commit is uninteresting, don't try to
372 * prune parents - we want the maximal uninteresting
373 * set.
375 * Normally we haven't parsed the parent
376 * yet, so we won't have a parent of a parent
377 * here. However, it may turn out that we've
378 * reached this commit some other way (where it
379 * wasn't uninteresting), in which case we need
380 * to mark its parents recursively too..
382 if (commit->object.flags & UNINTERESTING) {
383 while (parent) {
384 struct commit *p = parent->item;
385 parent = parent->next;
386 parse_commit(p);
387 p->object.flags |= UNINTERESTING;
388 if (p->parents)
389 mark_parents_uninteresting(p);
390 if (p->object.flags & SEEN)
391 continue;
392 p->object.flags |= SEEN;
393 insert_by_date(p, list);
395 return;
399 * Ok, the commit wasn't uninteresting. Try to
400 * simplify the commit history and find the parent
401 * that has no differences in the path set if one exists.
403 if (revs->prune_fn)
404 revs->prune_fn(revs, commit);
406 if (revs->no_walk)
407 return;
409 left_flag = (commit->object.flags & SYMMETRIC_LEFT);
411 rest = !revs->first_parent_only;
412 for (parent = commit->parents, add = 1; parent; add = rest) {
413 struct commit *p = parent->item;
415 parent = parent->next;
416 parse_commit(p);
417 p->object.flags |= left_flag;
418 if (p->object.flags & SEEN)
419 continue;
420 p->object.flags |= SEEN;
421 if (add)
422 insert_by_date(p, list);
426 static void cherry_pick_list(struct commit_list *list)
428 struct commit_list *p;
429 int left_count = 0, right_count = 0;
430 int left_first;
431 struct patch_ids ids;
433 /* First count the commits on the left and on the right */
434 for (p = list; p; p = p->next) {
435 struct commit *commit = p->item;
436 unsigned flags = commit->object.flags;
437 if (flags & BOUNDARY)
439 else if (flags & SYMMETRIC_LEFT)
440 left_count++;
441 else
442 right_count++;
445 left_first = left_count < right_count;
446 init_patch_ids(&ids);
448 /* Compute patch-ids for one side */
449 for (p = list; p; p = p->next) {
450 struct commit *commit = p->item;
451 unsigned flags = commit->object.flags;
453 if (flags & BOUNDARY)
454 continue;
456 * If we have fewer left, left_first is set and we omit
457 * commits on the right branch in this loop. If we have
458 * fewer right, we skip the left ones.
460 if (left_first != !!(flags & SYMMETRIC_LEFT))
461 continue;
462 commit->util = add_commit_patch_id(commit, &ids);
465 /* Check the other side */
466 for (p = list; p; p = p->next) {
467 struct commit *commit = p->item;
468 struct patch_id *id;
469 unsigned flags = commit->object.flags;
471 if (flags & BOUNDARY)
472 continue;
474 * If we have fewer left, left_first is set and we omit
475 * commits on the left branch in this loop.
477 if (left_first == !!(flags & SYMMETRIC_LEFT))
478 continue;
481 * Have we seen the same patch id?
483 id = has_commit_patch_id(commit, &ids);
484 if (!id)
485 continue;
486 id->seen = 1;
487 commit->object.flags |= SHOWN;
490 /* Now check the original side for seen ones */
491 for (p = list; p; p = p->next) {
492 struct commit *commit = p->item;
493 struct patch_id *ent;
495 ent = commit->util;
496 if (!ent)
497 continue;
498 if (ent->seen)
499 commit->object.flags |= SHOWN;
500 commit->util = NULL;
503 free_patch_ids(&ids);
506 static void limit_list(struct rev_info *revs)
508 struct commit_list *list = revs->commits;
509 struct commit_list *newlist = NULL;
510 struct commit_list **p = &newlist;
512 while (list) {
513 struct commit_list *entry = list;
514 struct commit *commit = list->item;
515 struct object *obj = &commit->object;
517 list = list->next;
518 free(entry);
520 if (revs->max_age != -1 && (commit->date < revs->max_age))
521 obj->flags |= UNINTERESTING;
522 add_parents_to_list(revs, commit, &list);
523 if (obj->flags & UNINTERESTING) {
524 mark_parents_uninteresting(commit);
525 if (everybody_uninteresting(list))
526 break;
527 continue;
529 if (revs->min_age != -1 && (commit->date > revs->min_age))
530 continue;
531 p = &commit_list_insert(commit, p)->next;
533 if (revs->cherry_pick)
534 cherry_pick_list(newlist);
536 revs->commits = newlist;
539 struct all_refs_cb {
540 int all_flags;
541 int warned_bad_reflog;
542 struct rev_info *all_revs;
543 const char *name_for_errormsg;
546 static int handle_one_ref(const char *path, const unsigned char *sha1, int flag, void *cb_data)
548 struct all_refs_cb *cb = cb_data;
549 struct object *object = get_reference(cb->all_revs, path, sha1,
550 cb->all_flags);
551 add_pending_object(cb->all_revs, object, path);
552 return 0;
555 static void handle_all(struct rev_info *revs, unsigned flags)
557 struct all_refs_cb cb;
558 cb.all_revs = revs;
559 cb.all_flags = flags;
560 for_each_ref(handle_one_ref, &cb);
563 static void handle_one_reflog_commit(unsigned char *sha1, void *cb_data)
565 struct all_refs_cb *cb = cb_data;
566 if (!is_null_sha1(sha1)) {
567 struct object *o = parse_object(sha1);
568 if (o) {
569 o->flags |= cb->all_flags;
570 add_pending_object(cb->all_revs, o, "");
572 else if (!cb->warned_bad_reflog) {
573 warning("reflog of '%s' references pruned commits",
574 cb->name_for_errormsg);
575 cb->warned_bad_reflog = 1;
580 static int handle_one_reflog_ent(unsigned char *osha1, unsigned char *nsha1,
581 const char *email, unsigned long timestamp, int tz,
582 const char *message, void *cb_data)
584 handle_one_reflog_commit(osha1, cb_data);
585 handle_one_reflog_commit(nsha1, cb_data);
586 return 0;
589 static int handle_one_reflog(const char *path, const unsigned char *sha1, int flag, void *cb_data)
591 struct all_refs_cb *cb = cb_data;
592 cb->warned_bad_reflog = 0;
593 cb->name_for_errormsg = path;
594 for_each_reflog_ent(path, handle_one_reflog_ent, cb_data);
595 return 0;
598 static void handle_reflog(struct rev_info *revs, unsigned flags)
600 struct all_refs_cb cb;
601 cb.all_revs = revs;
602 cb.all_flags = flags;
603 for_each_reflog(handle_one_reflog, &cb);
606 static int add_parents_only(struct rev_info *revs, const char *arg, int flags)
608 unsigned char sha1[20];
609 struct object *it;
610 struct commit *commit;
611 struct commit_list *parents;
613 if (*arg == '^') {
614 flags ^= UNINTERESTING;
615 arg++;
617 if (get_sha1(arg, sha1))
618 return 0;
619 while (1) {
620 it = get_reference(revs, arg, sha1, 0);
621 if (it->type != OBJ_TAG)
622 break;
623 hashcpy(sha1, ((struct tag*)it)->tagged->sha1);
625 if (it->type != OBJ_COMMIT)
626 return 0;
627 commit = (struct commit *)it;
628 for (parents = commit->parents; parents; parents = parents->next) {
629 it = &parents->item->object;
630 it->flags |= flags;
631 add_pending_object(revs, it, arg);
633 return 1;
636 void init_revisions(struct rev_info *revs, const char *prefix)
638 memset(revs, 0, sizeof(*revs));
640 revs->abbrev = DEFAULT_ABBREV;
641 revs->ignore_merges = 1;
642 revs->simplify_history = 1;
643 revs->pruning.recursive = 1;
644 revs->pruning.quiet = 1;
645 revs->pruning.add_remove = file_add_remove;
646 revs->pruning.change = file_change;
647 revs->lifo = 1;
648 revs->dense = 1;
649 revs->prefix = prefix;
650 revs->max_age = -1;
651 revs->min_age = -1;
652 revs->skip_count = -1;
653 revs->max_count = -1;
654 revs->subject_prefix = "PATCH";
656 revs->prune_fn = NULL;
657 revs->prune_data = NULL;
659 revs->topo_setter = topo_sort_default_setter;
660 revs->topo_getter = topo_sort_default_getter;
662 revs->commit_format = CMIT_FMT_DEFAULT;
664 diff_setup(&revs->diffopt);
667 static void add_pending_commit_list(struct rev_info *revs,
668 struct commit_list *commit_list,
669 unsigned int flags)
671 while (commit_list) {
672 struct object *object = &commit_list->item->object;
673 object->flags |= flags;
674 add_pending_object(revs, object, sha1_to_hex(object->sha1));
675 commit_list = commit_list->next;
679 static void prepare_show_merge(struct rev_info *revs)
681 struct commit_list *bases;
682 struct commit *head, *other;
683 unsigned char sha1[20];
684 const char **prune = NULL;
685 int i, prune_num = 1; /* counting terminating NULL */
687 if (get_sha1("HEAD", sha1) || !(head = lookup_commit(sha1)))
688 die("--merge without HEAD?");
689 if (get_sha1("MERGE_HEAD", sha1) || !(other = lookup_commit(sha1)))
690 die("--merge without MERGE_HEAD?");
691 add_pending_object(revs, &head->object, "HEAD");
692 add_pending_object(revs, &other->object, "MERGE_HEAD");
693 bases = get_merge_bases(head, other, 1);
694 while (bases) {
695 struct commit *it = bases->item;
696 struct commit_list *n = bases->next;
697 free(bases);
698 bases = n;
699 it->object.flags |= UNINTERESTING;
700 add_pending_object(revs, &it->object, "(merge-base)");
703 if (!active_nr)
704 read_cache();
705 for (i = 0; i < active_nr; i++) {
706 struct cache_entry *ce = active_cache[i];
707 if (!ce_stage(ce))
708 continue;
709 if (ce_path_match(ce, revs->prune_data)) {
710 prune_num++;
711 prune = xrealloc(prune, sizeof(*prune) * prune_num);
712 prune[prune_num-2] = ce->name;
713 prune[prune_num-1] = NULL;
715 while ((i+1 < active_nr) &&
716 ce_same_name(ce, active_cache[i+1]))
717 i++;
719 revs->prune_data = prune;
722 int handle_revision_arg(const char *arg, struct rev_info *revs,
723 int flags,
724 int cant_be_filename)
726 char *dotdot;
727 struct object *object;
728 unsigned char sha1[20];
729 int local_flags;
731 dotdot = strstr(arg, "..");
732 if (dotdot) {
733 unsigned char from_sha1[20];
734 const char *next = dotdot + 2;
735 const char *this = arg;
736 int symmetric = *next == '.';
737 unsigned int flags_exclude = flags ^ UNINTERESTING;
739 *dotdot = 0;
740 next += symmetric;
742 if (!*next)
743 next = "HEAD";
744 if (dotdot == arg)
745 this = "HEAD";
746 if (!get_sha1(this, from_sha1) &&
747 !get_sha1(next, sha1)) {
748 struct commit *a, *b;
749 struct commit_list *exclude;
751 a = lookup_commit_reference(from_sha1);
752 b = lookup_commit_reference(sha1);
753 if (!a || !b) {
754 die(symmetric ?
755 "Invalid symmetric difference expression %s...%s" :
756 "Invalid revision range %s..%s",
757 arg, next);
760 if (!cant_be_filename) {
761 *dotdot = '.';
762 verify_non_filename(revs->prefix, arg);
765 if (symmetric) {
766 exclude = get_merge_bases(a, b, 1);
767 add_pending_commit_list(revs, exclude,
768 flags_exclude);
769 free_commit_list(exclude);
770 a->object.flags |= flags | SYMMETRIC_LEFT;
771 } else
772 a->object.flags |= flags_exclude;
773 b->object.flags |= flags;
774 add_pending_object(revs, &a->object, this);
775 add_pending_object(revs, &b->object, next);
776 return 0;
778 *dotdot = '.';
780 dotdot = strstr(arg, "^@");
781 if (dotdot && !dotdot[2]) {
782 *dotdot = 0;
783 if (add_parents_only(revs, arg, flags))
784 return 0;
785 *dotdot = '^';
787 dotdot = strstr(arg, "^!");
788 if (dotdot && !dotdot[2]) {
789 *dotdot = 0;
790 if (!add_parents_only(revs, arg, flags ^ UNINTERESTING))
791 *dotdot = '^';
794 local_flags = 0;
795 if (*arg == '^') {
796 local_flags = UNINTERESTING;
797 arg++;
799 if (get_sha1(arg, sha1))
800 return -1;
801 if (!cant_be_filename)
802 verify_non_filename(revs->prefix, arg);
803 object = get_reference(revs, arg, sha1, flags ^ local_flags);
804 add_pending_object(revs, object, arg);
805 return 0;
808 static void add_grep(struct rev_info *revs, const char *ptn, enum grep_pat_token what)
810 if (!revs->grep_filter) {
811 struct grep_opt *opt = xcalloc(1, sizeof(*opt));
812 opt->status_only = 1;
813 opt->pattern_tail = &(opt->pattern_list);
814 opt->regflags = REG_NEWLINE;
815 revs->grep_filter = opt;
817 append_grep_pattern(revs->grep_filter, ptn,
818 "command line", 0, what);
821 static void add_header_grep(struct rev_info *revs, const char *field, const char *pattern)
823 char *pat;
824 const char *prefix;
825 int patlen, fldlen;
827 fldlen = strlen(field);
828 patlen = strlen(pattern);
829 pat = xmalloc(patlen + fldlen + 10);
830 prefix = ".*";
831 if (*pattern == '^') {
832 prefix = "";
833 pattern++;
835 sprintf(pat, "^%s %s%s", field, prefix, pattern);
836 add_grep(revs, pat, GREP_PATTERN_HEAD);
839 static void add_message_grep(struct rev_info *revs, const char *pattern)
841 add_grep(revs, pattern, GREP_PATTERN_BODY);
844 static void add_ignore_packed(struct rev_info *revs, const char *name)
846 int num = ++revs->num_ignore_packed;
848 revs->ignore_packed = xrealloc(revs->ignore_packed,
849 sizeof(const char **) * (num + 1));
850 revs->ignore_packed[num-1] = name;
851 revs->ignore_packed[num] = NULL;
855 * Parse revision information, filling in the "rev_info" structure,
856 * and removing the used arguments from the argument list.
858 * Returns the number of arguments left that weren't recognized
859 * (which are also moved to the head of the argument list)
861 int setup_revisions(int argc, const char **argv, struct rev_info *revs, const char *def)
863 int i, flags, seen_dashdash, show_merge;
864 const char **unrecognized = argv + 1;
865 int left = 1;
866 int all_match = 0;
868 /* First, search for "--" */
869 seen_dashdash = 0;
870 for (i = 1; i < argc; i++) {
871 const char *arg = argv[i];
872 if (strcmp(arg, "--"))
873 continue;
874 argv[i] = NULL;
875 argc = i;
876 revs->prune_data = get_pathspec(revs->prefix, argv + i + 1);
877 seen_dashdash = 1;
878 break;
881 flags = show_merge = 0;
882 for (i = 1; i < argc; i++) {
883 const char *arg = argv[i];
884 if (*arg == '-') {
885 int opts;
886 if (!prefixcmp(arg, "--max-count=")) {
887 revs->max_count = atoi(arg + 12);
888 continue;
890 if (!prefixcmp(arg, "--skip=")) {
891 revs->skip_count = atoi(arg + 7);
892 continue;
894 /* accept -<digit>, like traditional "head" */
895 if ((*arg == '-') && isdigit(arg[1])) {
896 revs->max_count = atoi(arg + 1);
897 continue;
899 if (!strcmp(arg, "-n")) {
900 if (argc <= i + 1)
901 die("-n requires an argument");
902 revs->max_count = atoi(argv[++i]);
903 continue;
905 if (!prefixcmp(arg, "-n")) {
906 revs->max_count = atoi(arg + 2);
907 continue;
909 if (!prefixcmp(arg, "--max-age=")) {
910 revs->max_age = atoi(arg + 10);
911 continue;
913 if (!prefixcmp(arg, "--since=")) {
914 revs->max_age = approxidate(arg + 8);
915 continue;
917 if (!prefixcmp(arg, "--after=")) {
918 revs->max_age = approxidate(arg + 8);
919 continue;
921 if (!prefixcmp(arg, "--min-age=")) {
922 revs->min_age = atoi(arg + 10);
923 continue;
925 if (!prefixcmp(arg, "--before=")) {
926 revs->min_age = approxidate(arg + 9);
927 continue;
929 if (!prefixcmp(arg, "--until=")) {
930 revs->min_age = approxidate(arg + 8);
931 continue;
933 if (!strcmp(arg, "--all")) {
934 handle_all(revs, flags);
935 continue;
937 if (!strcmp(arg, "--first-parent")) {
938 revs->first_parent_only = 1;
939 continue;
941 if (!strcmp(arg, "--reflog")) {
942 handle_reflog(revs, flags);
943 continue;
945 if (!strcmp(arg, "-g") ||
946 !strcmp(arg, "--walk-reflogs")) {
947 init_reflog_walk(&revs->reflog_info);
948 continue;
950 if (!strcmp(arg, "--not")) {
951 flags ^= UNINTERESTING;
952 continue;
954 if (!strcmp(arg, "--default")) {
955 if (++i >= argc)
956 die("bad --default argument");
957 def = argv[i];
958 continue;
960 if (!strcmp(arg, "--merge")) {
961 show_merge = 1;
962 continue;
964 if (!strcmp(arg, "--topo-order")) {
965 revs->topo_order = 1;
966 continue;
968 if (!strcmp(arg, "--date-order")) {
969 revs->lifo = 0;
970 revs->topo_order = 1;
971 continue;
973 if (!strcmp(arg, "--parents")) {
974 revs->parents = 1;
975 continue;
977 if (!strcmp(arg, "--dense")) {
978 revs->dense = 1;
979 continue;
981 if (!strcmp(arg, "--sparse")) {
982 revs->dense = 0;
983 continue;
985 if (!strcmp(arg, "--remove-empty")) {
986 revs->remove_empty_trees = 1;
987 continue;
989 if (!strcmp(arg, "--no-merges")) {
990 revs->no_merges = 1;
991 continue;
993 if (!strcmp(arg, "--boundary")) {
994 revs->boundary = 1;
995 continue;
997 if (!strcmp(arg, "--left-right")) {
998 revs->left_right = 1;
999 continue;
1001 if (!strcmp(arg, "--cherry-pick")) {
1002 revs->cherry_pick = 1;
1003 continue;
1005 if (!strcmp(arg, "--objects")) {
1006 revs->tag_objects = 1;
1007 revs->tree_objects = 1;
1008 revs->blob_objects = 1;
1009 continue;
1011 if (!strcmp(arg, "--objects-edge")) {
1012 revs->tag_objects = 1;
1013 revs->tree_objects = 1;
1014 revs->blob_objects = 1;
1015 revs->edge_hint = 1;
1016 continue;
1018 if (!strcmp(arg, "--unpacked")) {
1019 revs->unpacked = 1;
1020 free(revs->ignore_packed);
1021 revs->ignore_packed = NULL;
1022 revs->num_ignore_packed = 0;
1023 continue;
1025 if (!prefixcmp(arg, "--unpacked=")) {
1026 revs->unpacked = 1;
1027 add_ignore_packed(revs, arg+11);
1028 continue;
1030 if (!strcmp(arg, "-r")) {
1031 revs->diff = 1;
1032 revs->diffopt.recursive = 1;
1033 continue;
1035 if (!strcmp(arg, "-t")) {
1036 revs->diff = 1;
1037 revs->diffopt.recursive = 1;
1038 revs->diffopt.tree_in_recursive = 1;
1039 continue;
1041 if (!strcmp(arg, "-m")) {
1042 revs->ignore_merges = 0;
1043 continue;
1045 if (!strcmp(arg, "-c")) {
1046 revs->diff = 1;
1047 revs->dense_combined_merges = 0;
1048 revs->combine_merges = 1;
1049 continue;
1051 if (!strcmp(arg, "--cc")) {
1052 revs->diff = 1;
1053 revs->dense_combined_merges = 1;
1054 revs->combine_merges = 1;
1055 continue;
1057 if (!strcmp(arg, "-v")) {
1058 revs->verbose_header = 1;
1059 continue;
1061 if (!prefixcmp(arg, "--pretty")) {
1062 revs->verbose_header = 1;
1063 revs->commit_format = get_commit_format(arg+8);
1064 continue;
1066 if (!strcmp(arg, "--root")) {
1067 revs->show_root_diff = 1;
1068 continue;
1070 if (!strcmp(arg, "--no-commit-id")) {
1071 revs->no_commit_id = 1;
1072 continue;
1074 if (!strcmp(arg, "--always")) {
1075 revs->always_show_header = 1;
1076 continue;
1078 if (!strcmp(arg, "--no-abbrev")) {
1079 revs->abbrev = 0;
1080 continue;
1082 if (!strcmp(arg, "--abbrev")) {
1083 revs->abbrev = DEFAULT_ABBREV;
1084 continue;
1086 if (!prefixcmp(arg, "--abbrev=")) {
1087 revs->abbrev = strtoul(arg + 9, NULL, 10);
1088 if (revs->abbrev < MINIMUM_ABBREV)
1089 revs->abbrev = MINIMUM_ABBREV;
1090 else if (revs->abbrev > 40)
1091 revs->abbrev = 40;
1092 continue;
1094 if (!strcmp(arg, "--abbrev-commit")) {
1095 revs->abbrev_commit = 1;
1096 continue;
1098 if (!strcmp(arg, "--full-diff")) {
1099 revs->diff = 1;
1100 revs->full_diff = 1;
1101 continue;
1103 if (!strcmp(arg, "--full-history")) {
1104 revs->simplify_history = 0;
1105 continue;
1107 if (!strcmp(arg, "--relative-date")) {
1108 revs->relative_date = 1;
1109 continue;
1113 * Grepping the commit log
1115 if (!prefixcmp(arg, "--author=")) {
1116 add_header_grep(revs, "author", arg+9);
1117 continue;
1119 if (!prefixcmp(arg, "--committer=")) {
1120 add_header_grep(revs, "committer", arg+12);
1121 continue;
1123 if (!prefixcmp(arg, "--grep=")) {
1124 add_message_grep(revs, arg+7);
1125 continue;
1127 if (!strcmp(arg, "--all-match")) {
1128 all_match = 1;
1129 continue;
1131 if (!prefixcmp(arg, "--encoding=")) {
1132 arg += 11;
1133 if (strcmp(arg, "none"))
1134 git_log_output_encoding = xstrdup(arg);
1135 else
1136 git_log_output_encoding = "";
1137 continue;
1139 if (!strcmp(arg, "--reverse")) {
1140 revs->reverse ^= 1;
1141 continue;
1144 opts = diff_opt_parse(&revs->diffopt, argv+i, argc-i);
1145 if (opts > 0) {
1146 revs->diff = 1;
1147 i += opts - 1;
1148 continue;
1150 *unrecognized++ = arg;
1151 left++;
1152 continue;
1155 if (handle_revision_arg(arg, revs, flags, seen_dashdash)) {
1156 int j;
1157 if (seen_dashdash || *arg == '^')
1158 die("bad revision '%s'", arg);
1160 /* If we didn't have a "--":
1161 * (1) all filenames must exist;
1162 * (2) all rev-args must not be interpretable
1163 * as a valid filename.
1164 * but the latter we have checked in the main loop.
1166 for (j = i; j < argc; j++)
1167 verify_filename(revs->prefix, argv[j]);
1169 revs->prune_data = get_pathspec(revs->prefix,
1170 argv + i);
1171 break;
1175 if (show_merge)
1176 prepare_show_merge(revs);
1177 if (def && !revs->pending.nr) {
1178 unsigned char sha1[20];
1179 struct object *object;
1180 if (get_sha1(def, sha1))
1181 die("bad default revision '%s'", def);
1182 object = get_reference(revs, def, sha1, 0);
1183 add_pending_object(revs, object, def);
1186 if (revs->topo_order)
1187 revs->limited = 1;
1189 if (revs->prune_data) {
1190 diff_tree_setup_paths(revs->prune_data, &revs->pruning);
1191 revs->prune_fn = try_to_simplify_commit;
1192 if (!revs->full_diff)
1193 diff_tree_setup_paths(revs->prune_data, &revs->diffopt);
1195 if (revs->combine_merges) {
1196 revs->ignore_merges = 0;
1197 if (revs->dense_combined_merges && !revs->diffopt.output_format)
1198 revs->diffopt.output_format = DIFF_FORMAT_PATCH;
1200 revs->diffopt.abbrev = revs->abbrev;
1201 if (diff_setup_done(&revs->diffopt) < 0)
1202 die("diff_setup_done failed");
1204 if (revs->grep_filter) {
1205 revs->grep_filter->all_match = all_match;
1206 compile_grep_patterns(revs->grep_filter);
1209 return left;
1212 void prepare_revision_walk(struct rev_info *revs)
1214 int nr = revs->pending.nr;
1215 struct object_array_entry *e, *list;
1217 e = list = revs->pending.objects;
1218 revs->pending.nr = 0;
1219 revs->pending.alloc = 0;
1220 revs->pending.objects = NULL;
1221 while (--nr >= 0) {
1222 struct commit *commit = handle_commit(revs, e->item, e->name);
1223 if (commit) {
1224 if (!(commit->object.flags & SEEN)) {
1225 commit->object.flags |= SEEN;
1226 insert_by_date(commit, &revs->commits);
1229 e++;
1231 free(list);
1233 if (revs->no_walk)
1234 return;
1235 if (revs->limited)
1236 limit_list(revs);
1237 if (revs->topo_order)
1238 sort_in_topological_order_fn(&revs->commits, revs->lifo,
1239 revs->topo_setter,
1240 revs->topo_getter);
1243 static int rewrite_one(struct rev_info *revs, struct commit **pp)
1245 for (;;) {
1246 struct commit *p = *pp;
1247 if (!revs->limited)
1248 add_parents_to_list(revs, p, &revs->commits);
1249 if (p->parents && p->parents->next)
1250 return 0;
1251 if (p->object.flags & (TREECHANGE | UNINTERESTING))
1252 return 0;
1253 if (!p->parents)
1254 return -1;
1255 *pp = p->parents->item;
1259 static void rewrite_parents(struct rev_info *revs, struct commit *commit)
1261 struct commit_list **pp = &commit->parents;
1262 while (*pp) {
1263 struct commit_list *parent = *pp;
1264 if (rewrite_one(revs, &parent->item) < 0) {
1265 *pp = parent->next;
1266 continue;
1268 pp = &parent->next;
1272 static int commit_match(struct commit *commit, struct rev_info *opt)
1274 if (!opt->grep_filter)
1275 return 1;
1276 return grep_buffer(opt->grep_filter,
1277 NULL, /* we say nothing, not even filename */
1278 commit->buffer, strlen(commit->buffer));
1281 static struct commit *get_revision_1(struct rev_info *revs)
1283 if (!revs->commits)
1284 return NULL;
1286 do {
1287 struct commit_list *entry = revs->commits;
1288 struct commit *commit = entry->item;
1290 revs->commits = entry->next;
1291 free(entry);
1293 if (revs->reflog_info)
1294 fake_reflog_parent(revs->reflog_info, commit);
1297 * If we haven't done the list limiting, we need to look at
1298 * the parents here. We also need to do the date-based limiting
1299 * that we'd otherwise have done in limit_list().
1301 if (!revs->limited) {
1302 if (revs->max_age != -1 &&
1303 (commit->date < revs->max_age))
1304 continue;
1305 add_parents_to_list(revs, commit, &revs->commits);
1307 if (commit->object.flags & SHOWN)
1308 continue;
1310 if (revs->unpacked && has_sha1_pack(commit->object.sha1,
1311 revs->ignore_packed))
1312 continue;
1314 if (commit->object.flags & UNINTERESTING)
1315 continue;
1316 if (revs->min_age != -1 && (commit->date > revs->min_age))
1317 continue;
1318 if (revs->no_merges &&
1319 commit->parents && commit->parents->next)
1320 continue;
1321 if (!commit_match(commit, revs))
1322 continue;
1323 if (revs->prune_fn && revs->dense) {
1324 /* Commit without changes? */
1325 if (!(commit->object.flags & TREECHANGE)) {
1326 /* drop merges unless we want parenthood */
1327 if (!revs->parents)
1328 continue;
1329 /* non-merge - always ignore it */
1330 if (!commit->parents || !commit->parents->next)
1331 continue;
1333 if (revs->parents)
1334 rewrite_parents(revs, commit);
1336 return commit;
1337 } while (revs->commits);
1338 return NULL;
1341 static void gc_boundary(struct object_array *array)
1343 unsigned nr = array->nr;
1344 unsigned alloc = array->alloc;
1345 struct object_array_entry *objects = array->objects;
1347 if (alloc <= nr) {
1348 unsigned i, j;
1349 for (i = j = 0; i < nr; i++) {
1350 if (objects[i].item->flags & SHOWN)
1351 continue;
1352 if (i != j)
1353 objects[j] = objects[i];
1354 j++;
1356 for (i = j; i < nr; i++)
1357 objects[i].item = NULL;
1358 array->nr = j;
1362 struct commit *get_revision(struct rev_info *revs)
1364 struct commit *c = NULL;
1365 struct commit_list *l;
1367 if (revs->boundary == 2) {
1368 unsigned i;
1369 struct object_array *array = &revs->boundary_commits;
1370 struct object_array_entry *objects = array->objects;
1371 for (i = 0; i < array->nr; i++) {
1372 c = (struct commit *)(objects[i].item);
1373 if (!c)
1374 continue;
1375 if (!(c->object.flags & CHILD_SHOWN))
1376 continue;
1377 if (!(c->object.flags & SHOWN))
1378 break;
1380 if (array->nr <= i)
1381 return NULL;
1383 c->object.flags |= SHOWN | BOUNDARY;
1384 return c;
1387 if (revs->reverse) {
1388 int limit = -1;
1390 if (0 <= revs->max_count) {
1391 limit = revs->max_count;
1392 if (0 < revs->skip_count)
1393 limit += revs->skip_count;
1395 l = NULL;
1396 while ((c = get_revision_1(revs))) {
1397 commit_list_insert(c, &l);
1398 if ((0 < limit) && !--limit)
1399 break;
1401 revs->commits = l;
1402 revs->reverse = 0;
1403 revs->max_count = -1;
1404 c = NULL;
1408 * Now pick up what they want to give us
1410 c = get_revision_1(revs);
1411 if (c) {
1412 while (0 < revs->skip_count) {
1413 revs->skip_count--;
1414 c = get_revision_1(revs);
1415 if (!c)
1416 break;
1421 * Check the max_count.
1423 switch (revs->max_count) {
1424 case -1:
1425 break;
1426 case 0:
1427 c = NULL;
1428 break;
1429 default:
1430 revs->max_count--;
1433 if (c)
1434 c->object.flags |= SHOWN;
1436 if (!revs->boundary) {
1437 return c;
1440 if (!c) {
1442 * get_revision_1() runs out the commits, and
1443 * we are done computing the boundaries.
1444 * switch to boundary commits output mode.
1446 revs->boundary = 2;
1447 return get_revision(revs);
1451 * boundary commits are the commits that are parents of the
1452 * ones we got from get_revision_1() but they themselves are
1453 * not returned from get_revision_1(). Before returning
1454 * 'c', we need to mark its parents that they could be boundaries.
1457 for (l = c->parents; l; l = l->next) {
1458 struct object *p;
1459 p = &(l->item->object);
1460 if (p->flags & (CHILD_SHOWN | SHOWN))
1461 continue;
1462 p->flags |= CHILD_SHOWN;
1463 gc_boundary(&revs->boundary_commits);
1464 add_object_array(p, NULL, &revs->boundary_commits);
1467 return c;