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"
12
13static char *path_name(struct name_path *path, const char *name)
14{
15 struct name_path *p;
16 char *n, *m;
17 int nlen = strlen(name);
18 int len = nlen + 1;
19
20 for (p = path; p; p = p->up) {
21 if (p->elem_len)
22 len += p->elem_len + 1;
23 }
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] = '/';
32 }
33 }
34 return n;
35}
36
37void add_object(struct object *obj,
38 struct object_array *p,
39 struct name_path *path,
40 const char *name)
41{
42 add_object_array(obj, path_name(path, name), p);
43}
44
45static void mark_blob_uninteresting(struct blob *blob)
46{
47 if (blob->object.flags & UNINTERESTING)
48 return;
49 blob->object.flags |= UNINTERESTING;
50}
51
52void mark_tree_uninteresting(struct tree *tree)
53{
54 struct tree_desc desc;
55 struct name_entry entry;
56 struct object *obj = &tree->object;
57
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));
65
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));
72 }
73
74 /*
75 * We don't care about the tree any more
76 * after it has been marked uninteresting.
77 */
78 free(tree->buffer);
79 tree->buffer = NULL;
80}
81
82void mark_parents_uninteresting(struct commit *commit)
83{
84 struct commit_list *parents = commit->parents;
85
86 while (parents) {
87 struct commit *commit = parents->item;
88 if (!(commit->object.flags & UNINTERESTING)) {
89 commit->object.flags |= UNINTERESTING;
90
91 /*
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..
98 */
99 if (commit->parents)
100 mark_parents_uninteresting(commit);
101 }
102
103 /*
104 * A missing commit is ok iff its parent is marked
105 * uninteresting.
106 *
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.
110 */
111 if (!has_sha1_file(commit->object.sha1))
112 commit->object.parsed = 1;
113 parents = parents->next;
114 }
115}
116
117static void add_pending_object_with_mode(struct rev_info *revs, struct object *obj, const char *name, unsigned mode)
118{
119 if (revs->no_walk && (obj->flags & UNINTERESTING))
120 die("object ranges do not make sense when not walking revisions");
121 add_object_array_with_mode(obj, name, &revs->pending, mode);
122 if (revs->reflog_info && obj->type == OBJ_COMMIT)
123 add_reflog_for_walk(revs->reflog_info,
124 (struct commit *)obj, name);
125}
126
127void add_pending_object(struct rev_info *revs, struct object *obj, const char *name)
128{
129 add_pending_object_with_mode(revs, obj, name, S_IFINVALID);
130}
131
132static struct object *get_reference(struct rev_info *revs, const char *name, const unsigned char *sha1, unsigned int flags)
133{
134 struct object *object;
135
136 object = parse_object(sha1);
137 if (!object)
138 die("bad object %s", name);
139 object->flags |= flags;
140 return object;
141}
142
143static struct commit *handle_commit(struct rev_info *revs, struct object *object, const char *name)
144{
145 unsigned long flags = object->flags;
146
147 /*
148 * Tag object? Look what it points to..
149 */
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));
157 }
158
159 /*
160 * Commit object? Just return it, we'll do all the complex
161 * reachability crud.
162 */
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;
171 }
172 return commit;
173 }
174
175 /*
176 * Tree object? Either mark it uniniteresting, or add it
177 * to the list of objects to look at later..
178 */
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;
186 }
187 add_pending_object(revs, object, "");
188 return NULL;
189 }
190
191 /*
192 * Blob object? You know the drill by now..
193 */
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;
201 }
202 add_pending_object(revs, object, "");
203 return NULL;
204 }
205 die("%s is unknown object", name);
206}
207
208static int everybody_uninteresting(struct commit_list *orig)
209{
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;
217 }
218 return 1;
219}
220
221/*
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.
227 */
228static int tree_difference = REV_TREE_SAME;
229
230static 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)
234{
235 int diff = REV_TREE_DIFFERENT;
236
237 /*
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).
243 */
244 if (addremove == '+') {
245 diff = tree_difference;
246 if (diff != REV_TREE_SAME)
247 return;
248 diff = REV_TREE_NEW;
249 }
250 tree_difference = diff;
251 if (tree_difference == REV_TREE_DIFFERENT)
252 options->has_changes = 1;
253}
254
255static 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)
260{
261 tree_difference = REV_TREE_DIFFERENT;
262 options->has_changes = 1;
263}
264
265static int rev_compare_tree(struct rev_info *revs, struct tree *t1, struct tree *t2)
266{
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;
277}
278
279static int rev_same_tree_as_empty(struct rev_info *revs, struct tree *t1)
280{
281 int retval;
282 void *tree;
283 unsigned long size;
284 struct tree_desc empty, real;
285
286 if (!t1)
287 return 0;
288
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);
294
295 tree_difference = REV_TREE_SAME;
296 revs->pruning.has_changes = 0;
297 retval = diff_tree(&empty, &real, "", &revs->pruning);
298 free(tree);
299
300 return retval >= 0 && (tree_difference == REV_TREE_SAME);
301}
302
303static void try_to_simplify_commit(struct rev_info *revs, struct commit *commit)
304{
305 struct commit_list **pp, *parent;
306 int tree_changed = 0, tree_same = 0;
307
308 if (!commit->tree)
309 return;
310
311 if (!commit->parents) {
312 if (!rev_same_tree_as_empty(revs, commit->tree))
313 commit->object.flags |= TREECHANGE;
314 return;
315 }
316
317 pp = &commit->parents;
318 while ((parent = *pp) != NULL) {
319 struct commit *p = parent->item;
320
321 if (parse_commit(p) < 0)
322 die("cannot simplify commit %s (because of %s)",
323 sha1_to_hex(commit->object.sha1),
324 sha1_to_hex(p->object.sha1));
325 switch (rev_compare_tree(revs, p->tree, commit->tree)) {
326 case REV_TREE_SAME:
327 tree_same = 1;
328 if (!revs->simplify_history || (p->object.flags & UNINTERESTING)) {
329 /* Even if a merge with an uninteresting
330 * side branch brought the entire change
331 * we are interested in, we do not want
332 * to lose the other branches of this
333 * merge, so we just keep going.
334 */
335 pp = &parent->next;
336 continue;
337 }
338 parent->next = NULL;
339 commit->parents = parent;
340 return;
341
342 case REV_TREE_NEW:
343 if (revs->remove_empty_trees &&
344 rev_same_tree_as_empty(revs, p->tree)) {
345 /* We are adding all the specified
346 * paths from this parent, so the
347 * history beyond this parent is not
348 * interesting. Remove its parents
349 * (they are grandparents for us).
350 * IOW, we pretend this parent is a
351 * "root" commit.
352 */
353 if (parse_commit(p) < 0)
354 die("cannot simplify commit %s (invalid %s)",
355 sha1_to_hex(commit->object.sha1),
356 sha1_to_hex(p->object.sha1));
357 p->parents = NULL;
358 }
359 /* fallthrough */
360 case REV_TREE_DIFFERENT:
361 tree_changed = 1;
362 pp = &parent->next;
363 continue;
364 }
365 die("bad tree compare for commit %s", sha1_to_hex(commit->object.sha1));
366 }
367 if (tree_changed && !tree_same)
368 commit->object.flags |= TREECHANGE;
369}
370
371static int add_parents_to_list(struct rev_info *revs, struct commit *commit, struct commit_list **list)
372{
373 struct commit_list *parent = commit->parents;
374 unsigned left_flag;
375 int add, rest;
376
377 if (commit->object.flags & ADDED)
378 return 0;
379 commit->object.flags |= ADDED;
380
381 /*
382 * If the commit is uninteresting, don't try to
383 * prune parents - we want the maximal uninteresting
384 * set.
385 *
386 * Normally we haven't parsed the parent
387 * yet, so we won't have a parent of a parent
388 * here. However, it may turn out that we've
389 * reached this commit some other way (where it
390 * wasn't uninteresting), in which case we need
391 * to mark its parents recursively too..
392 */
393 if (commit->object.flags & UNINTERESTING) {
394 while (parent) {
395 struct commit *p = parent->item;
396 parent = parent->next;
397 if (parse_commit(p) < 0)
398 return -1;
399 p->object.flags |= UNINTERESTING;
400 if (p->parents)
401 mark_parents_uninteresting(p);
402 if (p->object.flags & SEEN)
403 continue;
404 p->object.flags |= SEEN;
405 insert_by_date(p, list);
406 }
407 return 0;
408 }
409
410 /*
411 * Ok, the commit wasn't uninteresting. Try to
412 * simplify the commit history and find the parent
413 * that has no differences in the path set if one exists.
414 */
415 if (revs->prune_fn)
416 revs->prune_fn(revs, commit);
417
418 if (revs->no_walk)
419 return 0;
420
421 left_flag = (commit->object.flags & SYMMETRIC_LEFT);
422
423 rest = !revs->first_parent_only;
424 for (parent = commit->parents, add = 1; parent; add = rest) {
425 struct commit *p = parent->item;
426
427 parent = parent->next;
428 if (parse_commit(p) < 0)
429 return -1;
430 p->object.flags |= left_flag;
431 if (p->object.flags & SEEN)
432 continue;
433 p->object.flags |= SEEN;
434 if (add)
435 insert_by_date(p, list);
436 }
437 return 0;
438}
439
440static void cherry_pick_list(struct commit_list *list)
441{
442 struct commit_list *p;
443 int left_count = 0, right_count = 0;
444 int left_first;
445 struct patch_ids ids;
446
447 /* First count the commits on the left and on the right */
448 for (p = list; p; p = p->next) {
449 struct commit *commit = p->item;
450 unsigned flags = commit->object.flags;
451 if (flags & BOUNDARY)
452 ;
453 else if (flags & SYMMETRIC_LEFT)
454 left_count++;
455 else
456 right_count++;
457 }
458
459 left_first = left_count < right_count;
460 init_patch_ids(&ids);
461
462 /* Compute patch-ids for one side */
463 for (p = list; p; p = p->next) {
464 struct commit *commit = p->item;
465 unsigned flags = commit->object.flags;
466
467 if (flags & BOUNDARY)
468 continue;
469 /*
470 * If we have fewer left, left_first is set and we omit
471 * commits on the right branch in this loop. If we have
472 * fewer right, we skip the left ones.
473 */
474 if (left_first != !!(flags & SYMMETRIC_LEFT))
475 continue;
476 commit->util = add_commit_patch_id(commit, &ids);
477 }
478
479 /* Check the other side */
480 for (p = list; p; p = p->next) {
481 struct commit *commit = p->item;
482 struct patch_id *id;
483 unsigned flags = commit->object.flags;
484
485 if (flags & BOUNDARY)
486 continue;
487 /*
488 * If we have fewer left, left_first is set and we omit
489 * commits on the left branch in this loop.
490 */
491 if (left_first == !!(flags & SYMMETRIC_LEFT))
492 continue;
493
494 /*
495 * Have we seen the same patch id?
496 */
497 id = has_commit_patch_id(commit, &ids);
498 if (!id)
499 continue;
500 id->seen = 1;
501 commit->object.flags |= SHOWN;
502 }
503
504 /* Now check the original side for seen ones */
505 for (p = list; p; p = p->next) {
506 struct commit *commit = p->item;
507 struct patch_id *ent;
508
509 ent = commit->util;
510 if (!ent)
511 continue;
512 if (ent->seen)
513 commit->object.flags |= SHOWN;
514 commit->util = NULL;
515 }
516
517 free_patch_ids(&ids);
518}
519
520static int limit_list(struct rev_info *revs)
521{
522 struct commit_list *list = revs->commits;
523 struct commit_list *newlist = NULL;
524 struct commit_list **p = &newlist;
525
526 while (list) {
527 struct commit_list *entry = list;
528 struct commit *commit = list->item;
529 struct object *obj = &commit->object;
530
531 list = list->next;
532 free(entry);
533
534 if (revs->max_age != -1 && (commit->date < revs->max_age))
535 obj->flags |= UNINTERESTING;
536 if (add_parents_to_list(revs, commit, &list) < 0)
537 return -1;
538 if (obj->flags & UNINTERESTING) {
539 mark_parents_uninteresting(commit);
540 if (everybody_uninteresting(list))
541 break;
542 continue;
543 }
544 if (revs->min_age != -1 && (commit->date > revs->min_age))
545 continue;
546 p = &commit_list_insert(commit, p)->next;
547 }
548 if (revs->cherry_pick)
549 cherry_pick_list(newlist);
550
551 revs->commits = newlist;
552 return 0;
553}
554
555struct all_refs_cb {
556 int all_flags;
557 int warned_bad_reflog;
558 struct rev_info *all_revs;
559 const char *name_for_errormsg;
560};
561
562static int handle_one_ref(const char *path, const unsigned char *sha1, int flag, void *cb_data)
563{
564 struct all_refs_cb *cb = cb_data;
565 struct object *object = get_reference(cb->all_revs, path, sha1,
566 cb->all_flags);
567 add_pending_object(cb->all_revs, object, path);
568 return 0;
569}
570
571static void handle_all(struct rev_info *revs, unsigned flags)
572{
573 struct all_refs_cb cb;
574 cb.all_revs = revs;
575 cb.all_flags = flags;
576 for_each_ref(handle_one_ref, &cb);
577}
578
579static void handle_one_reflog_commit(unsigned char *sha1, void *cb_data)
580{
581 struct all_refs_cb *cb = cb_data;
582 if (!is_null_sha1(sha1)) {
583 struct object *o = parse_object(sha1);
584 if (o) {
585 o->flags |= cb->all_flags;
586 add_pending_object(cb->all_revs, o, "");
587 }
588 else if (!cb->warned_bad_reflog) {
589 warning("reflog of '%s' references pruned commits",
590 cb->name_for_errormsg);
591 cb->warned_bad_reflog = 1;
592 }
593 }
594}
595
596static int handle_one_reflog_ent(unsigned char *osha1, unsigned char *nsha1,
597 const char *email, unsigned long timestamp, int tz,
598 const char *message, void *cb_data)
599{
600 handle_one_reflog_commit(osha1, cb_data);
601 handle_one_reflog_commit(nsha1, cb_data);
602 return 0;
603}
604
605static int handle_one_reflog(const char *path, const unsigned char *sha1, int flag, void *cb_data)
606{
607 struct all_refs_cb *cb = cb_data;
608 cb->warned_bad_reflog = 0;
609 cb->name_for_errormsg = path;
610 for_each_reflog_ent(path, handle_one_reflog_ent, cb_data);
611 return 0;
612}
613
614static void handle_reflog(struct rev_info *revs, unsigned flags)
615{
616 struct all_refs_cb cb;
617 cb.all_revs = revs;
618 cb.all_flags = flags;
619 for_each_reflog(handle_one_reflog, &cb);
620}
621
622static int add_parents_only(struct rev_info *revs, const char *arg, int flags)
623{
624 unsigned char sha1[20];
625 struct object *it;
626 struct commit *commit;
627 struct commit_list *parents;
628
629 if (*arg == '^') {
630 flags ^= UNINTERESTING;
631 arg++;
632 }
633 if (get_sha1(arg, sha1))
634 return 0;
635 while (1) {
636 it = get_reference(revs, arg, sha1, 0);
637 if (it->type != OBJ_TAG)
638 break;
639 hashcpy(sha1, ((struct tag*)it)->tagged->sha1);
640 }
641 if (it->type != OBJ_COMMIT)
642 return 0;
643 commit = (struct commit *)it;
644 for (parents = commit->parents; parents; parents = parents->next) {
645 it = &parents->item->object;
646 it->flags |= flags;
647 add_pending_object(revs, it, arg);
648 }
649 return 1;
650}
651
652void init_revisions(struct rev_info *revs, const char *prefix)
653{
654 memset(revs, 0, sizeof(*revs));
655
656 revs->abbrev = DEFAULT_ABBREV;
657 revs->ignore_merges = 1;
658 revs->simplify_history = 1;
659 revs->pruning.recursive = 1;
660 revs->pruning.quiet = 1;
661 revs->pruning.add_remove = file_add_remove;
662 revs->pruning.change = file_change;
663 revs->lifo = 1;
664 revs->dense = 1;
665 revs->prefix = prefix;
666 revs->max_age = -1;
667 revs->min_age = -1;
668 revs->skip_count = -1;
669 revs->max_count = -1;
670
671 revs->prune_fn = NULL;
672 revs->prune_data = NULL;
673
674 revs->topo_setter = topo_sort_default_setter;
675 revs->topo_getter = topo_sort_default_getter;
676
677 revs->commit_format = CMIT_FMT_DEFAULT;
678
679 diff_setup(&revs->diffopt);
680}
681
682static void add_pending_commit_list(struct rev_info *revs,
683 struct commit_list *commit_list,
684 unsigned int flags)
685{
686 while (commit_list) {
687 struct object *object = &commit_list->item->object;
688 object->flags |= flags;
689 add_pending_object(revs, object, sha1_to_hex(object->sha1));
690 commit_list = commit_list->next;
691 }
692}
693
694static void prepare_show_merge(struct rev_info *revs)
695{
696 struct commit_list *bases;
697 struct commit *head, *other;
698 unsigned char sha1[20];
699 const char **prune = NULL;
700 int i, prune_num = 1; /* counting terminating NULL */
701
702 if (get_sha1("HEAD", sha1) || !(head = lookup_commit(sha1)))
703 die("--merge without HEAD?");
704 if (get_sha1("MERGE_HEAD", sha1) || !(other = lookup_commit(sha1)))
705 die("--merge without MERGE_HEAD?");
706 add_pending_object(revs, &head->object, "HEAD");
707 add_pending_object(revs, &other->object, "MERGE_HEAD");
708 bases = get_merge_bases(head, other, 1);
709 while (bases) {
710 struct commit *it = bases->item;
711 struct commit_list *n = bases->next;
712 free(bases);
713 bases = n;
714 it->object.flags |= UNINTERESTING;
715 add_pending_object(revs, &it->object, "(merge-base)");
716 }
717
718 if (!active_nr)
719 read_cache();
720 for (i = 0; i < active_nr; i++) {
721 struct cache_entry *ce = active_cache[i];
722 if (!ce_stage(ce))
723 continue;
724 if (ce_path_match(ce, revs->prune_data)) {
725 prune_num++;
726 prune = xrealloc(prune, sizeof(*prune) * prune_num);
727 prune[prune_num-2] = ce->name;
728 prune[prune_num-1] = NULL;
729 }
730 while ((i+1 < active_nr) &&
731 ce_same_name(ce, active_cache[i+1]))
732 i++;
733 }
734 revs->prune_data = prune;
735}
736
737int handle_revision_arg(const char *arg, struct rev_info *revs,
738 int flags,
739 int cant_be_filename)
740{
741 unsigned mode;
742 char *dotdot;
743 struct object *object;
744 unsigned char sha1[20];
745 int local_flags;
746
747 dotdot = strstr(arg, "..");
748 if (dotdot) {
749 unsigned char from_sha1[20];
750 const char *next = dotdot + 2;
751 const char *this = arg;
752 int symmetric = *next == '.';
753 unsigned int flags_exclude = flags ^ UNINTERESTING;
754
755 *dotdot = 0;
756 next += symmetric;
757
758 if (!*next)
759 next = "HEAD";
760 if (dotdot == arg)
761 this = "HEAD";
762 if (!get_sha1(this, from_sha1) &&
763 !get_sha1(next, sha1)) {
764 struct commit *a, *b;
765 struct commit_list *exclude;
766
767 a = lookup_commit_reference(from_sha1);
768 b = lookup_commit_reference(sha1);
769 if (!a || !b) {
770 die(symmetric ?
771 "Invalid symmetric difference expression %s...%s" :
772 "Invalid revision range %s..%s",
773 arg, next);
774 }
775
776 if (!cant_be_filename) {
777 *dotdot = '.';
778 verify_non_filename(revs->prefix, arg);
779 }
780
781 if (symmetric) {
782 exclude = get_merge_bases(a, b, 1);
783 add_pending_commit_list(revs, exclude,
784 flags_exclude);
785 free_commit_list(exclude);
786 a->object.flags |= flags | SYMMETRIC_LEFT;
787 } else
788 a->object.flags |= flags_exclude;
789 b->object.flags |= flags;
790 add_pending_object(revs, &a->object, this);
791 add_pending_object(revs, &b->object, next);
792 return 0;
793 }
794 *dotdot = '.';
795 }
796 dotdot = strstr(arg, "^@");
797 if (dotdot && !dotdot[2]) {
798 *dotdot = 0;
799 if (add_parents_only(revs, arg, flags))
800 return 0;
801 *dotdot = '^';
802 }
803 dotdot = strstr(arg, "^!");
804 if (dotdot && !dotdot[2]) {
805 *dotdot = 0;
806 if (!add_parents_only(revs, arg, flags ^ UNINTERESTING))
807 *dotdot = '^';
808 }
809
810 local_flags = 0;
811 if (*arg == '^') {
812 local_flags = UNINTERESTING;
813 arg++;
814 }
815 if (get_sha1_with_mode(arg, sha1, &mode))
816 return -1;
817 if (!cant_be_filename)
818 verify_non_filename(revs->prefix, arg);
819 object = get_reference(revs, arg, sha1, flags ^ local_flags);
820 add_pending_object_with_mode(revs, object, arg, mode);
821 return 0;
822}
823
824static void add_grep(struct rev_info *revs, const char *ptn, enum grep_pat_token what)
825{
826 if (!revs->grep_filter) {
827 struct grep_opt *opt = xcalloc(1, sizeof(*opt));
828 opt->status_only = 1;
829 opt->pattern_tail = &(opt->pattern_list);
830 opt->regflags = REG_NEWLINE;
831 revs->grep_filter = opt;
832 }
833 append_grep_pattern(revs->grep_filter, ptn,
834 "command line", 0, what);
835}
836
837static void add_header_grep(struct rev_info *revs, const char *field, const char *pattern)
838{
839 char *pat;
840 const char *prefix;
841 int patlen, fldlen;
842
843 fldlen = strlen(field);
844 patlen = strlen(pattern);
845 pat = xmalloc(patlen + fldlen + 10);
846 prefix = ".*";
847 if (*pattern == '^') {
848 prefix = "";
849 pattern++;
850 }
851 sprintf(pat, "^%s %s%s", field, prefix, pattern);
852 add_grep(revs, pat, GREP_PATTERN_HEAD);
853}
854
855static void add_message_grep(struct rev_info *revs, const char *pattern)
856{
857 add_grep(revs, pattern, GREP_PATTERN_BODY);
858}
859
860static void add_ignore_packed(struct rev_info *revs, const char *name)
861{
862 int num = ++revs->num_ignore_packed;
863
864 revs->ignore_packed = xrealloc(revs->ignore_packed,
865 sizeof(const char **) * (num + 1));
866 revs->ignore_packed[num-1] = name;
867 revs->ignore_packed[num] = NULL;
868}
869
870/*
871 * Parse revision information, filling in the "rev_info" structure,
872 * and removing the used arguments from the argument list.
873 *
874 * Returns the number of arguments left that weren't recognized
875 * (which are also moved to the head of the argument list)
876 */
877int setup_revisions(int argc, const char **argv, struct rev_info *revs, const char *def)
878{
879 int i, flags, seen_dashdash, show_merge;
880 const char **unrecognized = argv + 1;
881 int left = 1;
882 int all_match = 0;
883 int regflags = 0;
884
885 /* First, search for "--" */
886 seen_dashdash = 0;
887 for (i = 1; i < argc; i++) {
888 const char *arg = argv[i];
889 if (strcmp(arg, "--"))
890 continue;
891 argv[i] = NULL;
892 argc = i;
893 revs->prune_data = get_pathspec(revs->prefix, argv + i + 1);
894 seen_dashdash = 1;
895 break;
896 }
897
898 flags = show_merge = 0;
899 for (i = 1; i < argc; i++) {
900 const char *arg = argv[i];
901 if (*arg == '-') {
902 int opts;
903 if (!prefixcmp(arg, "--max-count=")) {
904 revs->max_count = atoi(arg + 12);
905 continue;
906 }
907 if (!prefixcmp(arg, "--skip=")) {
908 revs->skip_count = atoi(arg + 7);
909 continue;
910 }
911 /* accept -<digit>, like traditional "head" */
912 if ((*arg == '-') && isdigit(arg[1])) {
913 revs->max_count = atoi(arg + 1);
914 continue;
915 }
916 if (!strcmp(arg, "-n")) {
917 if (argc <= i + 1)
918 die("-n requires an argument");
919 revs->max_count = atoi(argv[++i]);
920 continue;
921 }
922 if (!prefixcmp(arg, "-n")) {
923 revs->max_count = atoi(arg + 2);
924 continue;
925 }
926 if (!prefixcmp(arg, "--max-age=")) {
927 revs->max_age = atoi(arg + 10);
928 continue;
929 }
930 if (!prefixcmp(arg, "--since=")) {
931 revs->max_age = approxidate(arg + 8);
932 continue;
933 }
934 if (!prefixcmp(arg, "--after=")) {
935 revs->max_age = approxidate(arg + 8);
936 continue;
937 }
938 if (!prefixcmp(arg, "--min-age=")) {
939 revs->min_age = atoi(arg + 10);
940 continue;
941 }
942 if (!prefixcmp(arg, "--before=")) {
943 revs->min_age = approxidate(arg + 9);
944 continue;
945 }
946 if (!prefixcmp(arg, "--until=")) {
947 revs->min_age = approxidate(arg + 8);
948 continue;
949 }
950 if (!strcmp(arg, "--all")) {
951 handle_all(revs, flags);
952 continue;
953 }
954 if (!strcmp(arg, "--first-parent")) {
955 revs->first_parent_only = 1;
956 continue;
957 }
958 if (!strcmp(arg, "--reflog")) {
959 handle_reflog(revs, flags);
960 continue;
961 }
962 if (!strcmp(arg, "-g") ||
963 !strcmp(arg, "--walk-reflogs")) {
964 init_reflog_walk(&revs->reflog_info);
965 continue;
966 }
967 if (!strcmp(arg, "--not")) {
968 flags ^= UNINTERESTING;
969 continue;
970 }
971 if (!strcmp(arg, "--default")) {
972 if (++i >= argc)
973 die("bad --default argument");
974 def = argv[i];
975 continue;
976 }
977 if (!strcmp(arg, "--merge")) {
978 show_merge = 1;
979 continue;
980 }
981 if (!strcmp(arg, "--topo-order")) {
982 revs->topo_order = 1;
983 continue;
984 }
985 if (!strcmp(arg, "--date-order")) {
986 revs->lifo = 0;
987 revs->topo_order = 1;
988 continue;
989 }
990 if (!strcmp(arg, "--parents")) {
991 revs->parents = 1;
992 continue;
993 }
994 if (!strcmp(arg, "--dense")) {
995 revs->dense = 1;
996 continue;
997 }
998 if (!strcmp(arg, "--sparse")) {
999 revs->dense = 0;
1000 continue;
1001 }
1002 if (!strcmp(arg, "--remove-empty")) {
1003 revs->remove_empty_trees = 1;
1004 continue;
1005 }
1006 if (!strcmp(arg, "--no-merges")) {
1007 revs->no_merges = 1;
1008 continue;
1009 }
1010 if (!strcmp(arg, "--boundary")) {
1011 revs->boundary = 1;
1012 continue;
1013 }
1014 if (!strcmp(arg, "--left-right")) {
1015 revs->left_right = 1;
1016 continue;
1017 }
1018 if (!strcmp(arg, "--cherry-pick")) {
1019 revs->cherry_pick = 1;
1020 continue;
1021 }
1022 if (!strcmp(arg, "--objects")) {
1023 revs->tag_objects = 1;
1024 revs->tree_objects = 1;
1025 revs->blob_objects = 1;
1026 continue;
1027 }
1028 if (!strcmp(arg, "--objects-edge")) {
1029 revs->tag_objects = 1;
1030 revs->tree_objects = 1;
1031 revs->blob_objects = 1;
1032 revs->edge_hint = 1;
1033 continue;
1034 }
1035 if (!strcmp(arg, "--unpacked")) {
1036 revs->unpacked = 1;
1037 free(revs->ignore_packed);
1038 revs->ignore_packed = NULL;
1039 revs->num_ignore_packed = 0;
1040 continue;
1041 }
1042 if (!prefixcmp(arg, "--unpacked=")) {
1043 revs->unpacked = 1;
1044 add_ignore_packed(revs, arg+11);
1045 continue;
1046 }
1047 if (!strcmp(arg, "-r")) {
1048 revs->diff = 1;
1049 revs->diffopt.recursive = 1;
1050 continue;
1051 }
1052 if (!strcmp(arg, "-t")) {
1053 revs->diff = 1;
1054 revs->diffopt.recursive = 1;
1055 revs->diffopt.tree_in_recursive = 1;
1056 continue;
1057 }
1058 if (!strcmp(arg, "-m")) {
1059 revs->ignore_merges = 0;
1060 continue;
1061 }
1062 if (!strcmp(arg, "-c")) {
1063 revs->diff = 1;
1064 revs->dense_combined_merges = 0;
1065 revs->combine_merges = 1;
1066 continue;
1067 }
1068 if (!strcmp(arg, "--cc")) {
1069 revs->diff = 1;
1070 revs->dense_combined_merges = 1;
1071 revs->combine_merges = 1;
1072 continue;
1073 }
1074 if (!strcmp(arg, "-v")) {
1075 revs->verbose_header = 1;
1076 continue;
1077 }
1078 if (!prefixcmp(arg, "--pretty")) {
1079 revs->verbose_header = 1;
1080 revs->commit_format = get_commit_format(arg+8);
1081 continue;
1082 }
1083 if (!strcmp(arg, "--root")) {
1084 revs->show_root_diff = 1;
1085 continue;
1086 }
1087 if (!strcmp(arg, "--no-commit-id")) {
1088 revs->no_commit_id = 1;
1089 continue;
1090 }
1091 if (!strcmp(arg, "--always")) {
1092 revs->always_show_header = 1;
1093 continue;
1094 }
1095 if (!strcmp(arg, "--no-abbrev")) {
1096 revs->abbrev = 0;
1097 continue;
1098 }
1099 if (!strcmp(arg, "--abbrev")) {
1100 revs->abbrev = DEFAULT_ABBREV;
1101 continue;
1102 }
1103 if (!prefixcmp(arg, "--abbrev=")) {
1104 revs->abbrev = strtoul(arg + 9, NULL, 10);
1105 if (revs->abbrev < MINIMUM_ABBREV)
1106 revs->abbrev = MINIMUM_ABBREV;
1107 else if (revs->abbrev > 40)
1108 revs->abbrev = 40;
1109 continue;
1110 }
1111 if (!strcmp(arg, "--abbrev-commit")) {
1112 revs->abbrev_commit = 1;
1113 continue;
1114 }
1115 if (!strcmp(arg, "--full-diff")) {
1116 revs->diff = 1;
1117 revs->full_diff = 1;
1118 continue;
1119 }
1120 if (!strcmp(arg, "--full-history")) {
1121 revs->simplify_history = 0;
1122 continue;
1123 }
1124 if (!strcmp(arg, "--relative-date")) {
1125 revs->date_mode = DATE_RELATIVE;
1126 continue;
1127 }
1128 if (!strncmp(arg, "--date=", 7)) {
1129 if (!strcmp(arg + 7, "relative"))
1130 revs->date_mode = DATE_RELATIVE;
1131 else if (!strcmp(arg + 7, "local"))
1132 revs->date_mode = DATE_LOCAL;
1133 else if (!strcmp(arg + 7, "default"))
1134 revs->date_mode = DATE_NORMAL;
1135 else
1136 die("unknown date format %s", arg);
1137 continue;
1138 }
1139
1140 /*
1141 * Grepping the commit log
1142 */
1143 if (!prefixcmp(arg, "--author=")) {
1144 add_header_grep(revs, "author", arg+9);
1145 continue;
1146 }
1147 if (!prefixcmp(arg, "--committer=")) {
1148 add_header_grep(revs, "committer", arg+12);
1149 continue;
1150 }
1151 if (!prefixcmp(arg, "--grep=")) {
1152 add_message_grep(revs, arg+7);
1153 continue;
1154 }
1155 if (!prefixcmp(arg, "--extended-regexp")) {
1156 regflags |= REG_EXTENDED;
1157 continue;
1158 }
1159 if (!prefixcmp(arg, "--regexp-ignore-case")) {
1160 regflags |= REG_ICASE;
1161 continue;
1162 }
1163 if (!strcmp(arg, "--all-match")) {
1164 all_match = 1;
1165 continue;
1166 }
1167 if (!prefixcmp(arg, "--encoding=")) {
1168 arg += 11;
1169 if (strcmp(arg, "none"))
1170 git_log_output_encoding = xstrdup(arg);
1171 else
1172 git_log_output_encoding = "";
1173 continue;
1174 }
1175 if (!strcmp(arg, "--reverse")) {
1176 revs->reverse ^= 1;
1177 continue;
1178 }
1179
1180 opts = diff_opt_parse(&revs->diffopt, argv+i, argc-i);
1181 if (opts > 0) {
1182 if (strcmp(argv[i], "-z"))
1183 revs->diff = 1;
1184 i += opts - 1;
1185 continue;
1186 }
1187 *unrecognized++ = arg;
1188 left++;
1189 continue;
1190 }
1191
1192 if (handle_revision_arg(arg, revs, flags, seen_dashdash)) {
1193 int j;
1194 if (seen_dashdash || *arg == '^')
1195 die("bad revision '%s'", arg);
1196
1197 /* If we didn't have a "--":
1198 * (1) all filenames must exist;
1199 * (2) all rev-args must not be interpretable
1200 * as a valid filename.
1201 * but the latter we have checked in the main loop.
1202 */
1203 for (j = i; j < argc; j++)
1204 verify_filename(revs->prefix, argv[j]);
1205
1206 revs->prune_data = get_pathspec(revs->prefix,
1207 argv + i);
1208 break;
1209 }
1210 }
1211
1212 if (revs->grep_filter)
1213 revs->grep_filter->regflags |= regflags;
1214
1215 if (show_merge)
1216 prepare_show_merge(revs);
1217 if (def && !revs->pending.nr) {
1218 unsigned char sha1[20];
1219 struct object *object;
1220 unsigned mode;
1221 if (get_sha1_with_mode(def, sha1, &mode))
1222 die("bad default revision '%s'", def);
1223 object = get_reference(revs, def, sha1, 0);
1224 add_pending_object_with_mode(revs, object, def, mode);
1225 }
1226
1227 if (revs->topo_order)
1228 revs->limited = 1;
1229
1230 if (revs->prune_data) {
1231 diff_tree_setup_paths(revs->prune_data, &revs->pruning);
1232 /* Can't prune commits with rename following: the paths change.. */
1233 if (!revs->diffopt.follow_renames)
1234 revs->prune_fn = try_to_simplify_commit;
1235 if (!revs->full_diff)
1236 diff_tree_setup_paths(revs->prune_data, &revs->diffopt);
1237 }
1238 if (revs->combine_merges) {
1239 revs->ignore_merges = 0;
1240 if (revs->dense_combined_merges && !revs->diffopt.output_format)
1241 revs->diffopt.output_format = DIFF_FORMAT_PATCH;
1242 }
1243 revs->diffopt.abbrev = revs->abbrev;
1244 if (diff_setup_done(&revs->diffopt) < 0)
1245 die("diff_setup_done failed");
1246
1247 if (revs->grep_filter) {
1248 revs->grep_filter->all_match = all_match;
1249 compile_grep_patterns(revs->grep_filter);
1250 }
1251
1252 return left;
1253}
1254
1255int prepare_revision_walk(struct rev_info *revs)
1256{
1257 int nr = revs->pending.nr;
1258 struct object_array_entry *e, *list;
1259
1260 e = list = revs->pending.objects;
1261 revs->pending.nr = 0;
1262 revs->pending.alloc = 0;
1263 revs->pending.objects = NULL;
1264 while (--nr >= 0) {
1265 struct commit *commit = handle_commit(revs, e->item, e->name);
1266 if (commit) {
1267 if (!(commit->object.flags & SEEN)) {
1268 commit->object.flags |= SEEN;
1269 insert_by_date(commit, &revs->commits);
1270 }
1271 }
1272 e++;
1273 }
1274 free(list);
1275
1276 if (revs->no_walk)
1277 return 0;
1278 if (revs->limited)
1279 if (limit_list(revs) < 0)
1280 return -1;
1281 if (revs->topo_order)
1282 sort_in_topological_order_fn(&revs->commits, revs->lifo,
1283 revs->topo_setter,
1284 revs->topo_getter);
1285 return 0;
1286}
1287
1288enum rewrite_result {
1289 rewrite_one_ok,
1290 rewrite_one_noparents,
1291 rewrite_one_error,
1292};
1293
1294static enum rewrite_result rewrite_one(struct rev_info *revs, struct commit **pp)
1295{
1296 for (;;) {
1297 struct commit *p = *pp;
1298 if (!revs->limited)
1299 if (add_parents_to_list(revs, p, &revs->commits) < 0)
1300 return rewrite_one_error;
1301 if (p->parents && p->parents->next)
1302 return rewrite_one_ok;
1303 if (p->object.flags & (TREECHANGE | UNINTERESTING))
1304 return rewrite_one_ok;
1305 if (!p->parents)
1306 return rewrite_one_noparents;
1307 *pp = p->parents->item;
1308 }
1309}
1310
1311static int rewrite_parents(struct rev_info *revs, struct commit *commit)
1312{
1313 struct commit_list **pp = &commit->parents;
1314 while (*pp) {
1315 struct commit_list *parent = *pp;
1316 switch (rewrite_one(revs, &parent->item)) {
1317 case rewrite_one_ok:
1318 break;
1319 case rewrite_one_noparents:
1320 *pp = parent->next;
1321 continue;
1322 case rewrite_one_error:
1323 return -1;
1324 }
1325 pp = &parent->next;
1326 }
1327 return 0;
1328}
1329
1330static int commit_match(struct commit *commit, struct rev_info *opt)
1331{
1332 if (!opt->grep_filter)
1333 return 1;
1334 return grep_buffer(opt->grep_filter,
1335 NULL, /* we say nothing, not even filename */
1336 commit->buffer, strlen(commit->buffer));
1337}
1338
1339static struct commit *get_revision_1(struct rev_info *revs)
1340{
1341 if (!revs->commits)
1342 return NULL;
1343
1344 do {
1345 struct commit_list *entry = revs->commits;
1346 struct commit *commit = entry->item;
1347
1348 revs->commits = entry->next;
1349 free(entry);
1350
1351 if (revs->reflog_info)
1352 fake_reflog_parent(revs->reflog_info, commit);
1353
1354 /*
1355 * If we haven't done the list limiting, we need to look at
1356 * the parents here. We also need to do the date-based limiting
1357 * that we'd otherwise have done in limit_list().
1358 */
1359 if (!revs->limited) {
1360 if (revs->max_age != -1 &&
1361 (commit->date < revs->max_age))
1362 continue;
1363 if (add_parents_to_list(revs, commit, &revs->commits) < 0)
1364 return NULL;
1365 }
1366 if (commit->object.flags & SHOWN)
1367 continue;
1368
1369 if (revs->unpacked && has_sha1_pack(commit->object.sha1,
1370 revs->ignore_packed))
1371 continue;
1372
1373 if (commit->object.flags & UNINTERESTING)
1374 continue;
1375 if (revs->min_age != -1 && (commit->date > revs->min_age))
1376 continue;
1377 if (revs->no_merges &&
1378 commit->parents && commit->parents->next)
1379 continue;
1380 if (!commit_match(commit, revs))
1381 continue;
1382 if (revs->prune_fn && revs->dense) {
1383 /* Commit without changes? */
1384 if (!(commit->object.flags & TREECHANGE)) {
1385 /* drop merges unless we want parenthood */
1386 if (!revs->parents)
1387 continue;
1388 /* non-merge - always ignore it */
1389 if (!commit->parents || !commit->parents->next)
1390 continue;
1391 }
1392 if (revs->parents && rewrite_parents(revs, commit) < 0)
1393 return NULL;
1394 }
1395 return commit;
1396 } while (revs->commits);
1397 return NULL;
1398}
1399
1400static void gc_boundary(struct object_array *array)
1401{
1402 unsigned nr = array->nr;
1403 unsigned alloc = array->alloc;
1404 struct object_array_entry *objects = array->objects;
1405
1406 if (alloc <= nr) {
1407 unsigned i, j;
1408 for (i = j = 0; i < nr; i++) {
1409 if (objects[i].item->flags & SHOWN)
1410 continue;
1411 if (i != j)
1412 objects[j] = objects[i];
1413 j++;
1414 }
1415 for (i = j; i < nr; i++)
1416 objects[i].item = NULL;
1417 array->nr = j;
1418 }
1419}
1420
1421struct commit *get_revision(struct rev_info *revs)
1422{
1423 struct commit *c = NULL;
1424 struct commit_list *l;
1425
1426 if (revs->boundary == 2) {
1427 unsigned i;
1428 struct object_array *array = &revs->boundary_commits;
1429 struct object_array_entry *objects = array->objects;
1430 for (i = 0; i < array->nr; i++) {
1431 c = (struct commit *)(objects[i].item);
1432 if (!c)
1433 continue;
1434 if (!(c->object.flags & CHILD_SHOWN))
1435 continue;
1436 if (!(c->object.flags & SHOWN))
1437 break;
1438 }
1439 if (array->nr <= i)
1440 return NULL;
1441
1442 c->object.flags |= SHOWN | BOUNDARY;
1443 return c;
1444 }
1445
1446 if (revs->reverse) {
1447 int limit = -1;
1448
1449 if (0 <= revs->max_count) {
1450 limit = revs->max_count;
1451 if (0 < revs->skip_count)
1452 limit += revs->skip_count;
1453 }
1454 l = NULL;
1455 while ((c = get_revision_1(revs))) {
1456 commit_list_insert(c, &l);
1457 if ((0 < limit) && !--limit)
1458 break;
1459 }
1460 revs->commits = l;
1461 revs->reverse = 0;
1462 revs->max_count = -1;
1463 c = NULL;
1464 }
1465
1466 /*
1467 * Now pick up what they want to give us
1468 */
1469 c = get_revision_1(revs);
1470 if (c) {
1471 while (0 < revs->skip_count) {
1472 revs->skip_count--;
1473 c = get_revision_1(revs);
1474 if (!c)
1475 break;
1476 }
1477 }
1478
1479 /*
1480 * Check the max_count.
1481 */
1482 switch (revs->max_count) {
1483 case -1:
1484 break;
1485 case 0:
1486 c = NULL;
1487 break;
1488 default:
1489 revs->max_count--;
1490 }
1491
1492 if (c)
1493 c->object.flags |= SHOWN;
1494
1495 if (!revs->boundary) {
1496 return c;
1497 }
1498
1499 if (!c) {
1500 /*
1501 * get_revision_1() runs out the commits, and
1502 * we are done computing the boundaries.
1503 * switch to boundary commits output mode.
1504 */
1505 revs->boundary = 2;
1506 return get_revision(revs);
1507 }
1508
1509 /*
1510 * boundary commits are the commits that are parents of the
1511 * ones we got from get_revision_1() but they themselves are
1512 * not returned from get_revision_1(). Before returning
1513 * 'c', we need to mark its parents that they could be boundaries.
1514 */
1515
1516 for (l = c->parents; l; l = l->next) {
1517 struct object *p;
1518 p = &(l->item->object);
1519 if (p->flags & (CHILD_SHOWN | SHOWN))
1520 continue;
1521 p->flags |= CHILD_SHOWN;
1522 gc_boundary(&revs->boundary_commits);
1523 add_object_array(p, NULL, &revs->boundary_commits);
1524 }
1525
1526 return c;
1527}