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