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