diff: do not use null sha1 as a sentinel value
[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 "graph.h"
10 #include "grep.h"
11 #include "reflog-walk.h"
12 #include "patch-ids.h"
13 #include "decorate.h"
14 #include "log-tree.h"
15 #include "string-list.h"
16
17 volatile show_early_output_fn_t show_early_output;
18
19 char *path_name(const struct name_path *path, const char *name)
20 {
21 const struct name_path *p;
22 char *n, *m;
23 int nlen = strlen(name);
24 int len = nlen + 1;
25
26 for (p = path; p; p = p->up) {
27 if (p->elem_len)
28 len += p->elem_len + 1;
29 }
30 n = xmalloc(len);
31 m = n + len - (nlen + 1);
32 strcpy(m, name);
33 for (p = path; p; p = p->up) {
34 if (p->elem_len) {
35 m -= p->elem_len + 1;
36 memcpy(m, p->elem, p->elem_len);
37 m[p->elem_len] = '/';
38 }
39 }
40 return n;
41 }
42
43 static int show_path_component_truncated(FILE *out, const char *name, int len)
44 {
45 int cnt;
46 for (cnt = 0; cnt < len; cnt++) {
47 int ch = name[cnt];
48 if (!ch || ch == '\n')
49 return -1;
50 fputc(ch, out);
51 }
52 return len;
53 }
54
55 static int show_path_truncated(FILE *out, const struct name_path *path)
56 {
57 int emitted, ours;
58
59 if (!path)
60 return 0;
61 emitted = show_path_truncated(out, path->up);
62 if (emitted < 0)
63 return emitted;
64 if (emitted)
65 fputc('/', out);
66 ours = show_path_component_truncated(out, path->elem, path->elem_len);
67 if (ours < 0)
68 return ours;
69 return ours || emitted;
70 }
71
72 void show_object_with_name(FILE *out, struct object *obj, const struct name_path *path, const char *component)
73 {
74 struct name_path leaf;
75 leaf.up = (struct name_path *)path;
76 leaf.elem = component;
77 leaf.elem_len = strlen(component);
78
79 fprintf(out, "%s ", sha1_to_hex(obj->sha1));
80 show_path_truncated(out, &leaf);
81 fputc('\n', out);
82 }
83
84 void add_object(struct object *obj,
85 struct object_array *p,
86 struct name_path *path,
87 const char *name)
88 {
89 add_object_array(obj, path_name(path, name), p);
90 }
91
92 static void mark_blob_uninteresting(struct blob *blob)
93 {
94 if (!blob)
95 return;
96 if (blob->object.flags & UNINTERESTING)
97 return;
98 blob->object.flags |= UNINTERESTING;
99 }
100
101 void mark_tree_uninteresting(struct tree *tree)
102 {
103 struct tree_desc desc;
104 struct name_entry entry;
105 struct object *obj = &tree->object;
106
107 if (!tree)
108 return;
109 if (obj->flags & UNINTERESTING)
110 return;
111 obj->flags |= UNINTERESTING;
112 if (!has_sha1_file(obj->sha1))
113 return;
114 if (parse_tree(tree) < 0)
115 die("bad tree %s", sha1_to_hex(obj->sha1));
116
117 init_tree_desc(&desc, tree->buffer, tree->size);
118 while (tree_entry(&desc, &entry)) {
119 switch (object_type(entry.mode)) {
120 case OBJ_TREE:
121 mark_tree_uninteresting(lookup_tree(entry.sha1));
122 break;
123 case OBJ_BLOB:
124 mark_blob_uninteresting(lookup_blob(entry.sha1));
125 break;
126 default:
127 /* Subproject commit - not in this repository */
128 break;
129 }
130 }
131
132 /*
133 * We don't care about the tree any more
134 * after it has been marked uninteresting.
135 */
136 free(tree->buffer);
137 tree->buffer = NULL;
138 }
139
140 void mark_parents_uninteresting(struct commit *commit)
141 {
142 struct commit_list *parents = commit->parents;
143
144 while (parents) {
145 struct commit *commit = parents->item;
146 if (!(commit->object.flags & UNINTERESTING)) {
147 commit->object.flags |= UNINTERESTING;
148
149 /*
150 * Normally we haven't parsed the parent
151 * yet, so we won't have a parent of a parent
152 * here. However, it may turn out that we've
153 * reached this commit some other way (where it
154 * wasn't uninteresting), in which case we need
155 * to mark its parents recursively too..
156 */
157 if (commit->parents)
158 mark_parents_uninteresting(commit);
159 }
160
161 /*
162 * A missing commit is ok iff its parent is marked
163 * uninteresting.
164 *
165 * We just mark such a thing parsed, so that when
166 * it is popped next time around, we won't be trying
167 * to parse it and get an error.
168 */
169 if (!has_sha1_file(commit->object.sha1))
170 commit->object.parsed = 1;
171 parents = parents->next;
172 }
173 }
174
175 static void add_pending_object_with_mode(struct rev_info *revs, struct object *obj, const char *name, unsigned mode)
176 {
177 if (!obj)
178 return;
179 if (revs->no_walk && (obj->flags & UNINTERESTING))
180 revs->no_walk = 0;
181 if (revs->reflog_info && obj->type == OBJ_COMMIT) {
182 struct strbuf buf = STRBUF_INIT;
183 int len = interpret_branch_name(name, &buf);
184 int st;
185
186 if (0 < len && name[len] && buf.len)
187 strbuf_addstr(&buf, name + len);
188 st = add_reflog_for_walk(revs->reflog_info,
189 (struct commit *)obj,
190 buf.buf[0] ? buf.buf: name);
191 strbuf_release(&buf);
192 if (st)
193 return;
194 }
195 add_object_array_with_mode(obj, name, &revs->pending, mode);
196 }
197
198 void add_pending_object(struct rev_info *revs, struct object *obj, const char *name)
199 {
200 add_pending_object_with_mode(revs, obj, name, S_IFINVALID);
201 }
202
203 void add_head_to_pending(struct rev_info *revs)
204 {
205 unsigned char sha1[20];
206 struct object *obj;
207 if (get_sha1("HEAD", sha1))
208 return;
209 obj = parse_object(sha1);
210 if (!obj)
211 return;
212 add_pending_object(revs, obj, "HEAD");
213 }
214
215 static struct object *get_reference(struct rev_info *revs, const char *name, const unsigned char *sha1, unsigned int flags)
216 {
217 struct object *object;
218
219 object = parse_object(sha1);
220 if (!object) {
221 if (revs->ignore_missing)
222 return object;
223 die("bad object %s", name);
224 }
225 object->flags |= flags;
226 return object;
227 }
228
229 void add_pending_sha1(struct rev_info *revs, const char *name,
230 const unsigned char *sha1, unsigned int flags)
231 {
232 struct object *object = get_reference(revs, name, sha1, flags);
233 add_pending_object(revs, object, name);
234 }
235
236 static struct commit *handle_commit(struct rev_info *revs, struct object *object, const char *name)
237 {
238 unsigned long flags = object->flags;
239
240 /*
241 * Tag object? Look what it points to..
242 */
243 while (object->type == OBJ_TAG) {
244 struct tag *tag = (struct tag *) object;
245 if (revs->tag_objects && !(flags & UNINTERESTING))
246 add_pending_object(revs, object, tag->tag);
247 if (!tag->tagged)
248 die("bad tag");
249 object = parse_object(tag->tagged->sha1);
250 if (!object) {
251 if (flags & UNINTERESTING)
252 return NULL;
253 die("bad object %s", sha1_to_hex(tag->tagged->sha1));
254 }
255 }
256
257 /*
258 * Commit object? Just return it, we'll do all the complex
259 * reachability crud.
260 */
261 if (object->type == OBJ_COMMIT) {
262 struct commit *commit = (struct commit *)object;
263 if (parse_commit(commit) < 0)
264 die("unable to parse commit %s", name);
265 if (flags & UNINTERESTING) {
266 commit->object.flags |= UNINTERESTING;
267 mark_parents_uninteresting(commit);
268 revs->limited = 1;
269 }
270 if (revs->show_source && !commit->util)
271 commit->util = (void *) name;
272 return commit;
273 }
274
275 /*
276 * Tree object? Either mark it uninteresting, or add it
277 * to the list of objects to look at later..
278 */
279 if (object->type == OBJ_TREE) {
280 struct tree *tree = (struct tree *)object;
281 if (!revs->tree_objects)
282 return NULL;
283 if (flags & UNINTERESTING) {
284 mark_tree_uninteresting(tree);
285 return NULL;
286 }
287 add_pending_object(revs, object, "");
288 return NULL;
289 }
290
291 /*
292 * Blob object? You know the drill by now..
293 */
294 if (object->type == OBJ_BLOB) {
295 struct blob *blob = (struct blob *)object;
296 if (!revs->blob_objects)
297 return NULL;
298 if (flags & UNINTERESTING) {
299 mark_blob_uninteresting(blob);
300 return NULL;
301 }
302 add_pending_object(revs, object, "");
303 return NULL;
304 }
305 die("%s is unknown object", name);
306 }
307
308 static int everybody_uninteresting(struct commit_list *orig)
309 {
310 struct commit_list *list = orig;
311 while (list) {
312 struct commit *commit = list->item;
313 list = list->next;
314 if (commit->object.flags & UNINTERESTING)
315 continue;
316 return 0;
317 }
318 return 1;
319 }
320
321 /*
322 * The goal is to get REV_TREE_NEW as the result only if the
323 * diff consists of all '+' (and no other changes), REV_TREE_OLD
324 * if the whole diff is removal of old data, and otherwise
325 * REV_TREE_DIFFERENT (of course if the trees are the same we
326 * want REV_TREE_SAME).
327 * That means that once we get to REV_TREE_DIFFERENT, we do not
328 * have to look any further.
329 */
330 static int tree_difference = REV_TREE_SAME;
331
332 static void file_add_remove(struct diff_options *options,
333 int addremove, unsigned mode,
334 const unsigned char *sha1,
335 int sha1_valid,
336 const char *fullpath, unsigned dirty_submodule)
337 {
338 int diff = addremove == '+' ? REV_TREE_NEW : REV_TREE_OLD;
339
340 tree_difference |= diff;
341 if (tree_difference == REV_TREE_DIFFERENT)
342 DIFF_OPT_SET(options, HAS_CHANGES);
343 }
344
345 static void file_change(struct diff_options *options,
346 unsigned old_mode, unsigned new_mode,
347 const unsigned char *old_sha1,
348 const unsigned char *new_sha1,
349 int old_sha1_valid, int new_sha1_valid,
350 const char *fullpath,
351 unsigned old_dirty_submodule, unsigned new_dirty_submodule)
352 {
353 tree_difference = REV_TREE_DIFFERENT;
354 DIFF_OPT_SET(options, HAS_CHANGES);
355 }
356
357 static int rev_compare_tree(struct rev_info *revs, struct commit *parent, struct commit *commit)
358 {
359 struct tree *t1 = parent->tree;
360 struct tree *t2 = commit->tree;
361
362 if (!t1)
363 return REV_TREE_NEW;
364 if (!t2)
365 return REV_TREE_OLD;
366
367 if (revs->simplify_by_decoration) {
368 /*
369 * If we are simplifying by decoration, then the commit
370 * is worth showing if it has a tag pointing at it.
371 */
372 if (lookup_decoration(&name_decoration, &commit->object))
373 return REV_TREE_DIFFERENT;
374 /*
375 * A commit that is not pointed by a tag is uninteresting
376 * if we are not limited by path. This means that you will
377 * see the usual "commits that touch the paths" plus any
378 * tagged commit by specifying both --simplify-by-decoration
379 * and pathspec.
380 */
381 if (!revs->prune_data.nr)
382 return REV_TREE_SAME;
383 }
384
385 tree_difference = REV_TREE_SAME;
386 DIFF_OPT_CLR(&revs->pruning, HAS_CHANGES);
387 if (diff_tree_sha1(t1->object.sha1, t2->object.sha1, "",
388 &revs->pruning) < 0)
389 return REV_TREE_DIFFERENT;
390 return tree_difference;
391 }
392
393 static int rev_same_tree_as_empty(struct rev_info *revs, struct commit *commit)
394 {
395 int retval;
396 void *tree;
397 unsigned long size;
398 struct tree_desc empty, real;
399 struct tree *t1 = commit->tree;
400
401 if (!t1)
402 return 0;
403
404 tree = read_object_with_reference(t1->object.sha1, tree_type, &size, NULL);
405 if (!tree)
406 return 0;
407 init_tree_desc(&real, tree, size);
408 init_tree_desc(&empty, "", 0);
409
410 tree_difference = REV_TREE_SAME;
411 DIFF_OPT_CLR(&revs->pruning, HAS_CHANGES);
412 retval = diff_tree(&empty, &real, "", &revs->pruning);
413 free(tree);
414
415 return retval >= 0 && (tree_difference == REV_TREE_SAME);
416 }
417
418 static void try_to_simplify_commit(struct rev_info *revs, struct commit *commit)
419 {
420 struct commit_list **pp, *parent;
421 int tree_changed = 0, tree_same = 0, nth_parent = 0;
422
423 /*
424 * If we don't do pruning, everything is interesting
425 */
426 if (!revs->prune)
427 return;
428
429 if (!commit->tree)
430 return;
431
432 if (!commit->parents) {
433 if (rev_same_tree_as_empty(revs, commit))
434 commit->object.flags |= TREESAME;
435 return;
436 }
437
438 /*
439 * Normal non-merge commit? If we don't want to make the
440 * history dense, we consider it always to be a change..
441 */
442 if (!revs->dense && !commit->parents->next)
443 return;
444
445 pp = &commit->parents;
446 while ((parent = *pp) != NULL) {
447 struct commit *p = parent->item;
448
449 /*
450 * Do not compare with later parents when we care only about
451 * the first parent chain, in order to avoid derailing the
452 * traversal to follow a side branch that brought everything
453 * in the path we are limited to by the pathspec.
454 */
455 if (revs->first_parent_only && nth_parent++)
456 break;
457 if (parse_commit(p) < 0)
458 die("cannot simplify commit %s (because of %s)",
459 sha1_to_hex(commit->object.sha1),
460 sha1_to_hex(p->object.sha1));
461 switch (rev_compare_tree(revs, p, commit)) {
462 case REV_TREE_SAME:
463 tree_same = 1;
464 if (!revs->simplify_history || (p->object.flags & UNINTERESTING)) {
465 /* Even if a merge with an uninteresting
466 * side branch brought the entire change
467 * we are interested in, we do not want
468 * to lose the other branches of this
469 * merge, so we just keep going.
470 */
471 pp = &parent->next;
472 continue;
473 }
474 parent->next = NULL;
475 commit->parents = parent;
476 commit->object.flags |= TREESAME;
477 return;
478
479 case REV_TREE_NEW:
480 if (revs->remove_empty_trees &&
481 rev_same_tree_as_empty(revs, p)) {
482 /* We are adding all the specified
483 * paths from this parent, so the
484 * history beyond this parent is not
485 * interesting. Remove its parents
486 * (they are grandparents for us).
487 * IOW, we pretend this parent is a
488 * "root" commit.
489 */
490 if (parse_commit(p) < 0)
491 die("cannot simplify commit %s (invalid %s)",
492 sha1_to_hex(commit->object.sha1),
493 sha1_to_hex(p->object.sha1));
494 p->parents = NULL;
495 }
496 /* fallthrough */
497 case REV_TREE_OLD:
498 case REV_TREE_DIFFERENT:
499 tree_changed = 1;
500 pp = &parent->next;
501 continue;
502 }
503 die("bad tree compare for commit %s", sha1_to_hex(commit->object.sha1));
504 }
505 if (tree_changed && !tree_same)
506 return;
507 commit->object.flags |= TREESAME;
508 }
509
510 static void commit_list_insert_by_date_cached(struct commit *p, struct commit_list **head,
511 struct commit_list *cached_base, struct commit_list **cache)
512 {
513 struct commit_list *new_entry;
514
515 if (cached_base && p->date < cached_base->item->date)
516 new_entry = commit_list_insert_by_date(p, &cached_base->next);
517 else
518 new_entry = commit_list_insert_by_date(p, head);
519
520 if (cache && (!*cache || p->date < (*cache)->item->date))
521 *cache = new_entry;
522 }
523
524 static int add_parents_to_list(struct rev_info *revs, struct commit *commit,
525 struct commit_list **list, struct commit_list **cache_ptr)
526 {
527 struct commit_list *parent = commit->parents;
528 unsigned left_flag;
529 struct commit_list *cached_base = cache_ptr ? *cache_ptr : NULL;
530
531 if (commit->object.flags & ADDED)
532 return 0;
533 commit->object.flags |= ADDED;
534
535 /*
536 * If the commit is uninteresting, don't try to
537 * prune parents - we want the maximal uninteresting
538 * set.
539 *
540 * Normally we haven't parsed the parent
541 * yet, so we won't have a parent of a parent
542 * here. However, it may turn out that we've
543 * reached this commit some other way (where it
544 * wasn't uninteresting), in which case we need
545 * to mark its parents recursively too..
546 */
547 if (commit->object.flags & UNINTERESTING) {
548 while (parent) {
549 struct commit *p = parent->item;
550 parent = parent->next;
551 if (p)
552 p->object.flags |= UNINTERESTING;
553 if (parse_commit(p) < 0)
554 continue;
555 if (p->parents)
556 mark_parents_uninteresting(p);
557 if (p->object.flags & SEEN)
558 continue;
559 p->object.flags |= SEEN;
560 commit_list_insert_by_date_cached(p, list, cached_base, cache_ptr);
561 }
562 return 0;
563 }
564
565 /*
566 * Ok, the commit wasn't uninteresting. Try to
567 * simplify the commit history and find the parent
568 * that has no differences in the path set if one exists.
569 */
570 try_to_simplify_commit(revs, commit);
571
572 if (revs->no_walk)
573 return 0;
574
575 left_flag = (commit->object.flags & SYMMETRIC_LEFT);
576
577 for (parent = commit->parents; parent; parent = parent->next) {
578 struct commit *p = parent->item;
579
580 if (parse_commit(p) < 0)
581 return -1;
582 if (revs->show_source && !p->util)
583 p->util = commit->util;
584 p->object.flags |= left_flag;
585 if (!(p->object.flags & SEEN)) {
586 p->object.flags |= SEEN;
587 commit_list_insert_by_date_cached(p, list, cached_base, cache_ptr);
588 }
589 if (revs->first_parent_only)
590 break;
591 }
592 return 0;
593 }
594
595 static void cherry_pick_list(struct commit_list *list, struct rev_info *revs)
596 {
597 struct commit_list *p;
598 int left_count = 0, right_count = 0;
599 int left_first;
600 struct patch_ids ids;
601 unsigned cherry_flag;
602
603 /* First count the commits on the left and on the right */
604 for (p = list; p; p = p->next) {
605 struct commit *commit = p->item;
606 unsigned flags = commit->object.flags;
607 if (flags & BOUNDARY)
608 ;
609 else if (flags & SYMMETRIC_LEFT)
610 left_count++;
611 else
612 right_count++;
613 }
614
615 if (!left_count || !right_count)
616 return;
617
618 left_first = left_count < right_count;
619 init_patch_ids(&ids);
620 ids.diffopts.pathspec = revs->diffopt.pathspec;
621
622 /* Compute patch-ids for one side */
623 for (p = list; p; p = p->next) {
624 struct commit *commit = p->item;
625 unsigned flags = commit->object.flags;
626
627 if (flags & BOUNDARY)
628 continue;
629 /*
630 * If we have fewer left, left_first is set and we omit
631 * commits on the right branch in this loop. If we have
632 * fewer right, we skip the left ones.
633 */
634 if (left_first != !!(flags & SYMMETRIC_LEFT))
635 continue;
636 commit->util = add_commit_patch_id(commit, &ids);
637 }
638
639 /* either cherry_mark or cherry_pick are true */
640 cherry_flag = revs->cherry_mark ? PATCHSAME : SHOWN;
641
642 /* Check the other side */
643 for (p = list; p; p = p->next) {
644 struct commit *commit = p->item;
645 struct patch_id *id;
646 unsigned flags = commit->object.flags;
647
648 if (flags & BOUNDARY)
649 continue;
650 /*
651 * If we have fewer left, left_first is set and we omit
652 * commits on the left branch in this loop.
653 */
654 if (left_first == !!(flags & SYMMETRIC_LEFT))
655 continue;
656
657 /*
658 * Have we seen the same patch id?
659 */
660 id = has_commit_patch_id(commit, &ids);
661 if (!id)
662 continue;
663 id->seen = 1;
664 commit->object.flags |= cherry_flag;
665 }
666
667 /* Now check the original side for seen ones */
668 for (p = list; p; p = p->next) {
669 struct commit *commit = p->item;
670 struct patch_id *ent;
671
672 ent = commit->util;
673 if (!ent)
674 continue;
675 if (ent->seen)
676 commit->object.flags |= cherry_flag;
677 commit->util = NULL;
678 }
679
680 free_patch_ids(&ids);
681 }
682
683 /* How many extra uninteresting commits we want to see.. */
684 #define SLOP 5
685
686 static int still_interesting(struct commit_list *src, unsigned long date, int slop)
687 {
688 /*
689 * No source list at all? We're definitely done..
690 */
691 if (!src)
692 return 0;
693
694 /*
695 * Does the destination list contain entries with a date
696 * before the source list? Definitely _not_ done.
697 */
698 if (date < src->item->date)
699 return SLOP;
700
701 /*
702 * Does the source list still have interesting commits in
703 * it? Definitely not done..
704 */
705 if (!everybody_uninteresting(src))
706 return SLOP;
707
708 /* Ok, we're closing in.. */
709 return slop-1;
710 }
711
712 /*
713 * "rev-list --ancestry-path A..B" computes commits that are ancestors
714 * of B but not ancestors of A but further limits the result to those
715 * that are descendants of A. This takes the list of bottom commits and
716 * the result of "A..B" without --ancestry-path, and limits the latter
717 * further to the ones that can reach one of the commits in "bottom".
718 */
719 static void limit_to_ancestry(struct commit_list *bottom, struct commit_list *list)
720 {
721 struct commit_list *p;
722 struct commit_list *rlist = NULL;
723 int made_progress;
724
725 /*
726 * Reverse the list so that it will be likely that we would
727 * process parents before children.
728 */
729 for (p = list; p; p = p->next)
730 commit_list_insert(p->item, &rlist);
731
732 for (p = bottom; p; p = p->next)
733 p->item->object.flags |= TMP_MARK;
734
735 /*
736 * Mark the ones that can reach bottom commits in "list",
737 * in a bottom-up fashion.
738 */
739 do {
740 made_progress = 0;
741 for (p = rlist; p; p = p->next) {
742 struct commit *c = p->item;
743 struct commit_list *parents;
744 if (c->object.flags & (TMP_MARK | UNINTERESTING))
745 continue;
746 for (parents = c->parents;
747 parents;
748 parents = parents->next) {
749 if (!(parents->item->object.flags & TMP_MARK))
750 continue;
751 c->object.flags |= TMP_MARK;
752 made_progress = 1;
753 break;
754 }
755 }
756 } while (made_progress);
757
758 /*
759 * NEEDSWORK: decide if we want to remove parents that are
760 * not marked with TMP_MARK from commit->parents for commits
761 * in the resulting list. We may not want to do that, though.
762 */
763
764 /*
765 * The ones that are not marked with TMP_MARK are uninteresting
766 */
767 for (p = list; p; p = p->next) {
768 struct commit *c = p->item;
769 if (c->object.flags & TMP_MARK)
770 continue;
771 c->object.flags |= UNINTERESTING;
772 }
773
774 /* We are done with the TMP_MARK */
775 for (p = list; p; p = p->next)
776 p->item->object.flags &= ~TMP_MARK;
777 for (p = bottom; p; p = p->next)
778 p->item->object.flags &= ~TMP_MARK;
779 free_commit_list(rlist);
780 }
781
782 /*
783 * Before walking the history, keep the set of "negative" refs the
784 * caller has asked to exclude.
785 *
786 * This is used to compute "rev-list --ancestry-path A..B", as we need
787 * to filter the result of "A..B" further to the ones that can actually
788 * reach A.
789 */
790 static struct commit_list *collect_bottom_commits(struct rev_info *revs)
791 {
792 struct commit_list *bottom = NULL;
793 int i;
794 for (i = 0; i < revs->cmdline.nr; i++) {
795 struct rev_cmdline_entry *elem = &revs->cmdline.rev[i];
796 if ((elem->flags & UNINTERESTING) &&
797 elem->item->type == OBJ_COMMIT)
798 commit_list_insert((struct commit *)elem->item, &bottom);
799 }
800 return bottom;
801 }
802
803 /* Assumes either left_only or right_only is set */
804 static void limit_left_right(struct commit_list *list, struct rev_info *revs)
805 {
806 struct commit_list *p;
807
808 for (p = list; p; p = p->next) {
809 struct commit *commit = p->item;
810
811 if (revs->right_only) {
812 if (commit->object.flags & SYMMETRIC_LEFT)
813 commit->object.flags |= SHOWN;
814 } else /* revs->left_only is set */
815 if (!(commit->object.flags & SYMMETRIC_LEFT))
816 commit->object.flags |= SHOWN;
817 }
818 }
819
820 static int limit_list(struct rev_info *revs)
821 {
822 int slop = SLOP;
823 unsigned long date = ~0ul;
824 struct commit_list *list = revs->commits;
825 struct commit_list *newlist = NULL;
826 struct commit_list **p = &newlist;
827 struct commit_list *bottom = NULL;
828
829 if (revs->ancestry_path) {
830 bottom = collect_bottom_commits(revs);
831 if (!bottom)
832 die("--ancestry-path given but there are no bottom commits");
833 }
834
835 while (list) {
836 struct commit_list *entry = list;
837 struct commit *commit = list->item;
838 struct object *obj = &commit->object;
839 show_early_output_fn_t show;
840
841 list = list->next;
842 free(entry);
843
844 if (revs->max_age != -1 && (commit->date < revs->max_age))
845 obj->flags |= UNINTERESTING;
846 if (add_parents_to_list(revs, commit, &list, NULL) < 0)
847 return -1;
848 if (obj->flags & UNINTERESTING) {
849 mark_parents_uninteresting(commit);
850 if (revs->show_all)
851 p = &commit_list_insert(commit, p)->next;
852 slop = still_interesting(list, date, slop);
853 if (slop)
854 continue;
855 /* If showing all, add the whole pending list to the end */
856 if (revs->show_all)
857 *p = list;
858 break;
859 }
860 if (revs->min_age != -1 && (commit->date > revs->min_age))
861 continue;
862 date = commit->date;
863 p = &commit_list_insert(commit, p)->next;
864
865 show = show_early_output;
866 if (!show)
867 continue;
868
869 show(revs, newlist);
870 show_early_output = NULL;
871 }
872 if (revs->cherry_pick || revs->cherry_mark)
873 cherry_pick_list(newlist, revs);
874
875 if (revs->left_only || revs->right_only)
876 limit_left_right(newlist, revs);
877
878 if (bottom) {
879 limit_to_ancestry(bottom, newlist);
880 free_commit_list(bottom);
881 }
882
883 revs->commits = newlist;
884 return 0;
885 }
886
887 static void add_rev_cmdline(struct rev_info *revs,
888 struct object *item,
889 const char *name,
890 int whence,
891 unsigned flags)
892 {
893 struct rev_cmdline_info *info = &revs->cmdline;
894 int nr = info->nr;
895
896 ALLOC_GROW(info->rev, nr + 1, info->alloc);
897 info->rev[nr].item = item;
898 info->rev[nr].name = name;
899 info->rev[nr].whence = whence;
900 info->rev[nr].flags = flags;
901 info->nr++;
902 }
903
904 struct all_refs_cb {
905 int all_flags;
906 int warned_bad_reflog;
907 struct rev_info *all_revs;
908 const char *name_for_errormsg;
909 };
910
911 static int handle_one_ref(const char *path, const unsigned char *sha1, int flag, void *cb_data)
912 {
913 struct all_refs_cb *cb = cb_data;
914 struct object *object = get_reference(cb->all_revs, path, sha1,
915 cb->all_flags);
916 add_rev_cmdline(cb->all_revs, object, path, REV_CMD_REF, cb->all_flags);
917 add_pending_sha1(cb->all_revs, path, sha1, cb->all_flags);
918 return 0;
919 }
920
921 static void init_all_refs_cb(struct all_refs_cb *cb, struct rev_info *revs,
922 unsigned flags)
923 {
924 cb->all_revs = revs;
925 cb->all_flags = flags;
926 }
927
928 static void handle_refs(const char *submodule, struct rev_info *revs, unsigned flags,
929 int (*for_each)(const char *, each_ref_fn, void *))
930 {
931 struct all_refs_cb cb;
932 init_all_refs_cb(&cb, revs, flags);
933 for_each(submodule, handle_one_ref, &cb);
934 }
935
936 static void handle_one_reflog_commit(unsigned char *sha1, void *cb_data)
937 {
938 struct all_refs_cb *cb = cb_data;
939 if (!is_null_sha1(sha1)) {
940 struct object *o = parse_object(sha1);
941 if (o) {
942 o->flags |= cb->all_flags;
943 /* ??? CMDLINEFLAGS ??? */
944 add_pending_object(cb->all_revs, o, "");
945 }
946 else if (!cb->warned_bad_reflog) {
947 warning("reflog of '%s' references pruned commits",
948 cb->name_for_errormsg);
949 cb->warned_bad_reflog = 1;
950 }
951 }
952 }
953
954 static int handle_one_reflog_ent(unsigned char *osha1, unsigned char *nsha1,
955 const char *email, unsigned long timestamp, int tz,
956 const char *message, void *cb_data)
957 {
958 handle_one_reflog_commit(osha1, cb_data);
959 handle_one_reflog_commit(nsha1, cb_data);
960 return 0;
961 }
962
963 static int handle_one_reflog(const char *path, const unsigned char *sha1, int flag, void *cb_data)
964 {
965 struct all_refs_cb *cb = cb_data;
966 cb->warned_bad_reflog = 0;
967 cb->name_for_errormsg = path;
968 for_each_reflog_ent(path, handle_one_reflog_ent, cb_data);
969 return 0;
970 }
971
972 static void handle_reflog(struct rev_info *revs, unsigned flags)
973 {
974 struct all_refs_cb cb;
975 cb.all_revs = revs;
976 cb.all_flags = flags;
977 for_each_reflog(handle_one_reflog, &cb);
978 }
979
980 static int add_parents_only(struct rev_info *revs, const char *arg_, int flags)
981 {
982 unsigned char sha1[20];
983 struct object *it;
984 struct commit *commit;
985 struct commit_list *parents;
986 const char *arg = arg_;
987
988 if (*arg == '^') {
989 flags ^= UNINTERESTING;
990 arg++;
991 }
992 if (get_sha1(arg, sha1))
993 return 0;
994 while (1) {
995 it = get_reference(revs, arg, sha1, 0);
996 if (!it && revs->ignore_missing)
997 return 0;
998 if (it->type != OBJ_TAG)
999 break;
1000 if (!((struct tag*)it)->tagged)
1001 return 0;
1002 hashcpy(sha1, ((struct tag*)it)->tagged->sha1);
1003 }
1004 if (it->type != OBJ_COMMIT)
1005 return 0;
1006 commit = (struct commit *)it;
1007 for (parents = commit->parents; parents; parents = parents->next) {
1008 it = &parents->item->object;
1009 it->flags |= flags;
1010 add_rev_cmdline(revs, it, arg_, REV_CMD_PARENTS_ONLY, flags);
1011 add_pending_object(revs, it, arg);
1012 }
1013 return 1;
1014 }
1015
1016 void init_revisions(struct rev_info *revs, const char *prefix)
1017 {
1018 memset(revs, 0, sizeof(*revs));
1019
1020 revs->abbrev = DEFAULT_ABBREV;
1021 revs->ignore_merges = 1;
1022 revs->simplify_history = 1;
1023 DIFF_OPT_SET(&revs->pruning, RECURSIVE);
1024 DIFF_OPT_SET(&revs->pruning, QUICK);
1025 revs->pruning.add_remove = file_add_remove;
1026 revs->pruning.change = file_change;
1027 revs->lifo = 1;
1028 revs->dense = 1;
1029 revs->prefix = prefix;
1030 revs->max_age = -1;
1031 revs->min_age = -1;
1032 revs->skip_count = -1;
1033 revs->max_count = -1;
1034 revs->max_parents = -1;
1035
1036 revs->commit_format = CMIT_FMT_DEFAULT;
1037
1038 revs->grep_filter.status_only = 1;
1039 revs->grep_filter.pattern_tail = &(revs->grep_filter.pattern_list);
1040 revs->grep_filter.header_tail = &(revs->grep_filter.header_list);
1041 revs->grep_filter.regflags = REG_NEWLINE;
1042
1043 diff_setup(&revs->diffopt);
1044 if (prefix && !revs->diffopt.prefix) {
1045 revs->diffopt.prefix = prefix;
1046 revs->diffopt.prefix_length = strlen(prefix);
1047 }
1048
1049 revs->notes_opt.use_default_notes = -1;
1050 }
1051
1052 static void add_pending_commit_list(struct rev_info *revs,
1053 struct commit_list *commit_list,
1054 unsigned int flags)
1055 {
1056 while (commit_list) {
1057 struct object *object = &commit_list->item->object;
1058 object->flags |= flags;
1059 add_pending_object(revs, object, sha1_to_hex(object->sha1));
1060 commit_list = commit_list->next;
1061 }
1062 }
1063
1064 static void prepare_show_merge(struct rev_info *revs)
1065 {
1066 struct commit_list *bases;
1067 struct commit *head, *other;
1068 unsigned char sha1[20];
1069 const char **prune = NULL;
1070 int i, prune_num = 1; /* counting terminating NULL */
1071
1072 if (get_sha1("HEAD", sha1))
1073 die("--merge without HEAD?");
1074 head = lookup_commit_or_die(sha1, "HEAD");
1075 if (get_sha1("MERGE_HEAD", sha1))
1076 die("--merge without MERGE_HEAD?");
1077 other = lookup_commit_or_die(sha1, "MERGE_HEAD");
1078 add_pending_object(revs, &head->object, "HEAD");
1079 add_pending_object(revs, &other->object, "MERGE_HEAD");
1080 bases = get_merge_bases(head, other, 1);
1081 add_pending_commit_list(revs, bases, UNINTERESTING);
1082 free_commit_list(bases);
1083 head->object.flags |= SYMMETRIC_LEFT;
1084
1085 if (!active_nr)
1086 read_cache();
1087 for (i = 0; i < active_nr; i++) {
1088 struct cache_entry *ce = active_cache[i];
1089 if (!ce_stage(ce))
1090 continue;
1091 if (ce_path_match(ce, &revs->prune_data)) {
1092 prune_num++;
1093 prune = xrealloc(prune, sizeof(*prune) * prune_num);
1094 prune[prune_num-2] = ce->name;
1095 prune[prune_num-1] = NULL;
1096 }
1097 while ((i+1 < active_nr) &&
1098 ce_same_name(ce, active_cache[i+1]))
1099 i++;
1100 }
1101 free_pathspec(&revs->prune_data);
1102 init_pathspec(&revs->prune_data, prune);
1103 revs->limited = 1;
1104 }
1105
1106 int handle_revision_arg(const char *arg_, struct rev_info *revs,
1107 int flags,
1108 int cant_be_filename)
1109 {
1110 unsigned mode;
1111 char *dotdot;
1112 struct object *object;
1113 unsigned char sha1[20];
1114 int local_flags;
1115 const char *arg = arg_;
1116
1117 dotdot = strstr(arg, "..");
1118 if (dotdot) {
1119 unsigned char from_sha1[20];
1120 const char *next = dotdot + 2;
1121 const char *this = arg;
1122 int symmetric = *next == '.';
1123 unsigned int flags_exclude = flags ^ UNINTERESTING;
1124 unsigned int a_flags;
1125
1126 *dotdot = 0;
1127 next += symmetric;
1128
1129 if (!*next)
1130 next = "HEAD";
1131 if (dotdot == arg)
1132 this = "HEAD";
1133 if (!get_sha1(this, from_sha1) &&
1134 !get_sha1(next, sha1)) {
1135 struct commit *a, *b;
1136 struct commit_list *exclude;
1137
1138 a = lookup_commit_reference(from_sha1);
1139 b = lookup_commit_reference(sha1);
1140 if (!a || !b) {
1141 if (revs->ignore_missing)
1142 return 0;
1143 die(symmetric ?
1144 "Invalid symmetric difference expression %s...%s" :
1145 "Invalid revision range %s..%s",
1146 arg, next);
1147 }
1148
1149 if (!cant_be_filename) {
1150 *dotdot = '.';
1151 verify_non_filename(revs->prefix, arg);
1152 }
1153
1154 if (symmetric) {
1155 exclude = get_merge_bases(a, b, 1);
1156 add_pending_commit_list(revs, exclude,
1157 flags_exclude);
1158 free_commit_list(exclude);
1159 a_flags = flags | SYMMETRIC_LEFT;
1160 } else
1161 a_flags = flags_exclude;
1162 a->object.flags |= a_flags;
1163 b->object.flags |= flags;
1164 add_rev_cmdline(revs, &a->object, this,
1165 REV_CMD_LEFT, a_flags);
1166 add_rev_cmdline(revs, &b->object, next,
1167 REV_CMD_RIGHT, flags);
1168 add_pending_object(revs, &a->object, this);
1169 add_pending_object(revs, &b->object, next);
1170 return 0;
1171 }
1172 *dotdot = '.';
1173 }
1174 dotdot = strstr(arg, "^@");
1175 if (dotdot && !dotdot[2]) {
1176 *dotdot = 0;
1177 if (add_parents_only(revs, arg, flags))
1178 return 0;
1179 *dotdot = '^';
1180 }
1181 dotdot = strstr(arg, "^!");
1182 if (dotdot && !dotdot[2]) {
1183 *dotdot = 0;
1184 if (!add_parents_only(revs, arg, flags ^ UNINTERESTING))
1185 *dotdot = '^';
1186 }
1187
1188 local_flags = 0;
1189 if (*arg == '^') {
1190 local_flags = UNINTERESTING;
1191 arg++;
1192 }
1193 if (get_sha1_with_mode(arg, sha1, &mode))
1194 return revs->ignore_missing ? 0 : -1;
1195 if (!cant_be_filename)
1196 verify_non_filename(revs->prefix, arg);
1197 object = get_reference(revs, arg, sha1, flags ^ local_flags);
1198 add_rev_cmdline(revs, object, arg_, REV_CMD_REV, flags ^ local_flags);
1199 add_pending_object_with_mode(revs, object, arg, mode);
1200 return 0;
1201 }
1202
1203 struct cmdline_pathspec {
1204 int alloc;
1205 int nr;
1206 const char **path;
1207 };
1208
1209 static void append_prune_data(struct cmdline_pathspec *prune, const char **av)
1210 {
1211 while (*av) {
1212 ALLOC_GROW(prune->path, prune->nr+1, prune->alloc);
1213 prune->path[prune->nr++] = *(av++);
1214 }
1215 }
1216
1217 static void read_pathspec_from_stdin(struct rev_info *revs, struct strbuf *sb,
1218 struct cmdline_pathspec *prune)
1219 {
1220 while (strbuf_getwholeline(sb, stdin, '\n') != EOF) {
1221 int len = sb->len;
1222 if (len && sb->buf[len - 1] == '\n')
1223 sb->buf[--len] = '\0';
1224 ALLOC_GROW(prune->path, prune->nr+1, prune->alloc);
1225 prune->path[prune->nr++] = xstrdup(sb->buf);
1226 }
1227 }
1228
1229 static void read_revisions_from_stdin(struct rev_info *revs,
1230 struct cmdline_pathspec *prune)
1231 {
1232 struct strbuf sb;
1233 int seen_dashdash = 0;
1234
1235 strbuf_init(&sb, 1000);
1236 while (strbuf_getwholeline(&sb, stdin, '\n') != EOF) {
1237 int len = sb.len;
1238 if (len && sb.buf[len - 1] == '\n')
1239 sb.buf[--len] = '\0';
1240 if (!len)
1241 break;
1242 if (sb.buf[0] == '-') {
1243 if (len == 2 && sb.buf[1] == '-') {
1244 seen_dashdash = 1;
1245 break;
1246 }
1247 die("options not supported in --stdin mode");
1248 }
1249 if (handle_revision_arg(sb.buf, revs, 0, 1))
1250 die("bad revision '%s'", sb.buf);
1251 }
1252 if (seen_dashdash)
1253 read_pathspec_from_stdin(revs, &sb, prune);
1254 strbuf_release(&sb);
1255 }
1256
1257 static void add_grep(struct rev_info *revs, const char *ptn, enum grep_pat_token what)
1258 {
1259 append_grep_pattern(&revs->grep_filter, ptn, "command line", 0, what);
1260 }
1261
1262 static void add_header_grep(struct rev_info *revs, enum grep_header_field field, const char *pattern)
1263 {
1264 append_header_grep_pattern(&revs->grep_filter, field, pattern);
1265 }
1266
1267 static void add_message_grep(struct rev_info *revs, const char *pattern)
1268 {
1269 add_grep(revs, pattern, GREP_PATTERN_BODY);
1270 }
1271
1272 static int handle_revision_opt(struct rev_info *revs, int argc, const char **argv,
1273 int *unkc, const char **unkv)
1274 {
1275 const char *arg = argv[0];
1276 const char *optarg;
1277 int argcount;
1278
1279 /* pseudo revision arguments */
1280 if (!strcmp(arg, "--all") || !strcmp(arg, "--branches") ||
1281 !strcmp(arg, "--tags") || !strcmp(arg, "--remotes") ||
1282 !strcmp(arg, "--reflog") || !strcmp(arg, "--not") ||
1283 !strcmp(arg, "--no-walk") || !strcmp(arg, "--do-walk") ||
1284 !strcmp(arg, "--bisect") || !prefixcmp(arg, "--glob=") ||
1285 !prefixcmp(arg, "--branches=") || !prefixcmp(arg, "--tags=") ||
1286 !prefixcmp(arg, "--remotes="))
1287 {
1288 unkv[(*unkc)++] = arg;
1289 return 1;
1290 }
1291
1292 if ((argcount = parse_long_opt("max-count", argv, &optarg))) {
1293 revs->max_count = atoi(optarg);
1294 revs->no_walk = 0;
1295 return argcount;
1296 } else if ((argcount = parse_long_opt("skip", argv, &optarg))) {
1297 revs->skip_count = atoi(optarg);
1298 return argcount;
1299 } else if ((*arg == '-') && isdigit(arg[1])) {
1300 /* accept -<digit>, like traditional "head" */
1301 revs->max_count = atoi(arg + 1);
1302 revs->no_walk = 0;
1303 } else if (!strcmp(arg, "-n")) {
1304 if (argc <= 1)
1305 return error("-n requires an argument");
1306 revs->max_count = atoi(argv[1]);
1307 revs->no_walk = 0;
1308 return 2;
1309 } else if (!prefixcmp(arg, "-n")) {
1310 revs->max_count = atoi(arg + 2);
1311 revs->no_walk = 0;
1312 } else if ((argcount = parse_long_opt("max-age", argv, &optarg))) {
1313 revs->max_age = atoi(optarg);
1314 return argcount;
1315 } else if ((argcount = parse_long_opt("since", argv, &optarg))) {
1316 revs->max_age = approxidate(optarg);
1317 return argcount;
1318 } else if ((argcount = parse_long_opt("after", argv, &optarg))) {
1319 revs->max_age = approxidate(optarg);
1320 return argcount;
1321 } else if ((argcount = parse_long_opt("min-age", argv, &optarg))) {
1322 revs->min_age = atoi(optarg);
1323 return argcount;
1324 } else if ((argcount = parse_long_opt("before", argv, &optarg))) {
1325 revs->min_age = approxidate(optarg);
1326 return argcount;
1327 } else if ((argcount = parse_long_opt("until", argv, &optarg))) {
1328 revs->min_age = approxidate(optarg);
1329 return argcount;
1330 } else if (!strcmp(arg, "--first-parent")) {
1331 revs->first_parent_only = 1;
1332 } else if (!strcmp(arg, "--ancestry-path")) {
1333 revs->ancestry_path = 1;
1334 revs->simplify_history = 0;
1335 revs->limited = 1;
1336 } else if (!strcmp(arg, "-g") || !strcmp(arg, "--walk-reflogs")) {
1337 init_reflog_walk(&revs->reflog_info);
1338 } else if (!strcmp(arg, "--default")) {
1339 if (argc <= 1)
1340 return error("bad --default argument");
1341 revs->def = argv[1];
1342 return 2;
1343 } else if (!strcmp(arg, "--merge")) {
1344 revs->show_merge = 1;
1345 } else if (!strcmp(arg, "--topo-order")) {
1346 revs->lifo = 1;
1347 revs->topo_order = 1;
1348 } else if (!strcmp(arg, "--simplify-merges")) {
1349 revs->simplify_merges = 1;
1350 revs->rewrite_parents = 1;
1351 revs->simplify_history = 0;
1352 revs->limited = 1;
1353 } else if (!strcmp(arg, "--simplify-by-decoration")) {
1354 revs->simplify_merges = 1;
1355 revs->rewrite_parents = 1;
1356 revs->simplify_history = 0;
1357 revs->simplify_by_decoration = 1;
1358 revs->limited = 1;
1359 revs->prune = 1;
1360 load_ref_decorations(DECORATE_SHORT_REFS);
1361 } else if (!strcmp(arg, "--date-order")) {
1362 revs->lifo = 0;
1363 revs->topo_order = 1;
1364 } else if (!prefixcmp(arg, "--early-output")) {
1365 int count = 100;
1366 switch (arg[14]) {
1367 case '=':
1368 count = atoi(arg+15);
1369 /* Fallthrough */
1370 case 0:
1371 revs->topo_order = 1;
1372 revs->early_output = count;
1373 }
1374 } else if (!strcmp(arg, "--parents")) {
1375 revs->rewrite_parents = 1;
1376 revs->print_parents = 1;
1377 } else if (!strcmp(arg, "--dense")) {
1378 revs->dense = 1;
1379 } else if (!strcmp(arg, "--sparse")) {
1380 revs->dense = 0;
1381 } else if (!strcmp(arg, "--show-all")) {
1382 revs->show_all = 1;
1383 } else if (!strcmp(arg, "--remove-empty")) {
1384 revs->remove_empty_trees = 1;
1385 } else if (!strcmp(arg, "--merges")) {
1386 revs->min_parents = 2;
1387 } else if (!strcmp(arg, "--no-merges")) {
1388 revs->max_parents = 1;
1389 } else if (!prefixcmp(arg, "--min-parents=")) {
1390 revs->min_parents = atoi(arg+14);
1391 } else if (!prefixcmp(arg, "--no-min-parents")) {
1392 revs->min_parents = 0;
1393 } else if (!prefixcmp(arg, "--max-parents=")) {
1394 revs->max_parents = atoi(arg+14);
1395 } else if (!prefixcmp(arg, "--no-max-parents")) {
1396 revs->max_parents = -1;
1397 } else if (!strcmp(arg, "--boundary")) {
1398 revs->boundary = 1;
1399 } else if (!strcmp(arg, "--left-right")) {
1400 revs->left_right = 1;
1401 } else if (!strcmp(arg, "--left-only")) {
1402 if (revs->right_only)
1403 die("--left-only is incompatible with --right-only"
1404 " or --cherry");
1405 revs->left_only = 1;
1406 } else if (!strcmp(arg, "--right-only")) {
1407 if (revs->left_only)
1408 die("--right-only is incompatible with --left-only");
1409 revs->right_only = 1;
1410 } else if (!strcmp(arg, "--cherry")) {
1411 if (revs->left_only)
1412 die("--cherry is incompatible with --left-only");
1413 revs->cherry_mark = 1;
1414 revs->right_only = 1;
1415 revs->max_parents = 1;
1416 revs->limited = 1;
1417 } else if (!strcmp(arg, "--count")) {
1418 revs->count = 1;
1419 } else if (!strcmp(arg, "--cherry-mark")) {
1420 if (revs->cherry_pick)
1421 die("--cherry-mark is incompatible with --cherry-pick");
1422 revs->cherry_mark = 1;
1423 revs->limited = 1; /* needs limit_list() */
1424 } else if (!strcmp(arg, "--cherry-pick")) {
1425 if (revs->cherry_mark)
1426 die("--cherry-pick is incompatible with --cherry-mark");
1427 revs->cherry_pick = 1;
1428 revs->limited = 1;
1429 } else if (!strcmp(arg, "--objects")) {
1430 revs->tag_objects = 1;
1431 revs->tree_objects = 1;
1432 revs->blob_objects = 1;
1433 } else if (!strcmp(arg, "--objects-edge")) {
1434 revs->tag_objects = 1;
1435 revs->tree_objects = 1;
1436 revs->blob_objects = 1;
1437 revs->edge_hint = 1;
1438 } else if (!strcmp(arg, "--verify-objects")) {
1439 revs->tag_objects = 1;
1440 revs->tree_objects = 1;
1441 revs->blob_objects = 1;
1442 revs->verify_objects = 1;
1443 } else if (!strcmp(arg, "--unpacked")) {
1444 revs->unpacked = 1;
1445 } else if (!prefixcmp(arg, "--unpacked=")) {
1446 die("--unpacked=<packfile> no longer supported.");
1447 } else if (!strcmp(arg, "-r")) {
1448 revs->diff = 1;
1449 DIFF_OPT_SET(&revs->diffopt, RECURSIVE);
1450 } else if (!strcmp(arg, "-t")) {
1451 revs->diff = 1;
1452 DIFF_OPT_SET(&revs->diffopt, RECURSIVE);
1453 DIFF_OPT_SET(&revs->diffopt, TREE_IN_RECURSIVE);
1454 } else if (!strcmp(arg, "-m")) {
1455 revs->ignore_merges = 0;
1456 } else if (!strcmp(arg, "-c")) {
1457 revs->diff = 1;
1458 revs->dense_combined_merges = 0;
1459 revs->combine_merges = 1;
1460 } else if (!strcmp(arg, "--cc")) {
1461 revs->diff = 1;
1462 revs->dense_combined_merges = 1;
1463 revs->combine_merges = 1;
1464 } else if (!strcmp(arg, "-v")) {
1465 revs->verbose_header = 1;
1466 } else if (!strcmp(arg, "--pretty")) {
1467 revs->verbose_header = 1;
1468 revs->pretty_given = 1;
1469 get_commit_format(arg+8, revs);
1470 } else if (!prefixcmp(arg, "--pretty=") || !prefixcmp(arg, "--format=")) {
1471 /*
1472 * Detached form ("--pretty X" as opposed to "--pretty=X")
1473 * not allowed, since the argument is optional.
1474 */
1475 revs->verbose_header = 1;
1476 revs->pretty_given = 1;
1477 get_commit_format(arg+9, revs);
1478 } else if (!strcmp(arg, "--show-notes") || !strcmp(arg, "--notes")) {
1479 revs->show_notes = 1;
1480 revs->show_notes_given = 1;
1481 revs->notes_opt.use_default_notes = 1;
1482 } else if (!strcmp(arg, "--show-signature")) {
1483 revs->show_signature = 1;
1484 } else if (!prefixcmp(arg, "--show-notes=") ||
1485 !prefixcmp(arg, "--notes=")) {
1486 struct strbuf buf = STRBUF_INIT;
1487 revs->show_notes = 1;
1488 revs->show_notes_given = 1;
1489 if (!prefixcmp(arg, "--show-notes")) {
1490 if (revs->notes_opt.use_default_notes < 0)
1491 revs->notes_opt.use_default_notes = 1;
1492 strbuf_addstr(&buf, arg+13);
1493 }
1494 else
1495 strbuf_addstr(&buf, arg+8);
1496 expand_notes_ref(&buf);
1497 string_list_append(&revs->notes_opt.extra_notes_refs,
1498 strbuf_detach(&buf, NULL));
1499 } else if (!strcmp(arg, "--no-notes")) {
1500 revs->show_notes = 0;
1501 revs->show_notes_given = 1;
1502 revs->notes_opt.use_default_notes = -1;
1503 /* we have been strdup'ing ourselves, so trick
1504 * string_list into free()ing strings */
1505 revs->notes_opt.extra_notes_refs.strdup_strings = 1;
1506 string_list_clear(&revs->notes_opt.extra_notes_refs, 0);
1507 revs->notes_opt.extra_notes_refs.strdup_strings = 0;
1508 } else if (!strcmp(arg, "--standard-notes")) {
1509 revs->show_notes_given = 1;
1510 revs->notes_opt.use_default_notes = 1;
1511 } else if (!strcmp(arg, "--no-standard-notes")) {
1512 revs->notes_opt.use_default_notes = 0;
1513 } else if (!strcmp(arg, "--oneline")) {
1514 revs->verbose_header = 1;
1515 get_commit_format("oneline", revs);
1516 revs->pretty_given = 1;
1517 revs->abbrev_commit = 1;
1518 } else if (!strcmp(arg, "--graph")) {
1519 revs->topo_order = 1;
1520 revs->rewrite_parents = 1;
1521 revs->graph = graph_init(revs);
1522 } else if (!strcmp(arg, "--root")) {
1523 revs->show_root_diff = 1;
1524 } else if (!strcmp(arg, "--no-commit-id")) {
1525 revs->no_commit_id = 1;
1526 } else if (!strcmp(arg, "--always")) {
1527 revs->always_show_header = 1;
1528 } else if (!strcmp(arg, "--no-abbrev")) {
1529 revs->abbrev = 0;
1530 } else if (!strcmp(arg, "--abbrev")) {
1531 revs->abbrev = DEFAULT_ABBREV;
1532 } else if (!prefixcmp(arg, "--abbrev=")) {
1533 revs->abbrev = strtoul(arg + 9, NULL, 10);
1534 if (revs->abbrev < MINIMUM_ABBREV)
1535 revs->abbrev = MINIMUM_ABBREV;
1536 else if (revs->abbrev > 40)
1537 revs->abbrev = 40;
1538 } else if (!strcmp(arg, "--abbrev-commit")) {
1539 revs->abbrev_commit = 1;
1540 revs->abbrev_commit_given = 1;
1541 } else if (!strcmp(arg, "--no-abbrev-commit")) {
1542 revs->abbrev_commit = 0;
1543 } else if (!strcmp(arg, "--full-diff")) {
1544 revs->diff = 1;
1545 revs->full_diff = 1;
1546 } else if (!strcmp(arg, "--full-history")) {
1547 revs->simplify_history = 0;
1548 } else if (!strcmp(arg, "--relative-date")) {
1549 revs->date_mode = DATE_RELATIVE;
1550 revs->date_mode_explicit = 1;
1551 } else if ((argcount = parse_long_opt("date", argv, &optarg))) {
1552 revs->date_mode = parse_date_format(optarg);
1553 revs->date_mode_explicit = 1;
1554 return argcount;
1555 } else if (!strcmp(arg, "--log-size")) {
1556 revs->show_log_size = 1;
1557 }
1558 /*
1559 * Grepping the commit log
1560 */
1561 else if ((argcount = parse_long_opt("author", argv, &optarg))) {
1562 add_header_grep(revs, GREP_HEADER_AUTHOR, optarg);
1563 return argcount;
1564 } else if ((argcount = parse_long_opt("committer", argv, &optarg))) {
1565 add_header_grep(revs, GREP_HEADER_COMMITTER, optarg);
1566 return argcount;
1567 } else if ((argcount = parse_long_opt("grep", argv, &optarg))) {
1568 add_message_grep(revs, optarg);
1569 return argcount;
1570 } else if (!strcmp(arg, "--extended-regexp") || !strcmp(arg, "-E")) {
1571 revs->grep_filter.regflags |= REG_EXTENDED;
1572 } else if (!strcmp(arg, "--regexp-ignore-case") || !strcmp(arg, "-i")) {
1573 revs->grep_filter.regflags |= REG_ICASE;
1574 } else if (!strcmp(arg, "--fixed-strings") || !strcmp(arg, "-F")) {
1575 revs->grep_filter.fixed = 1;
1576 } else if (!strcmp(arg, "--all-match")) {
1577 revs->grep_filter.all_match = 1;
1578 } else if ((argcount = parse_long_opt("encoding", argv, &optarg))) {
1579 if (strcmp(optarg, "none"))
1580 git_log_output_encoding = xstrdup(optarg);
1581 else
1582 git_log_output_encoding = "";
1583 return argcount;
1584 } else if (!strcmp(arg, "--reverse")) {
1585 revs->reverse ^= 1;
1586 } else if (!strcmp(arg, "--children")) {
1587 revs->children.name = "children";
1588 revs->limited = 1;
1589 } else if (!strcmp(arg, "--ignore-missing")) {
1590 revs->ignore_missing = 1;
1591 } else {
1592 int opts = diff_opt_parse(&revs->diffopt, argv, argc);
1593 if (!opts)
1594 unkv[(*unkc)++] = arg;
1595 return opts;
1596 }
1597
1598 return 1;
1599 }
1600
1601 void parse_revision_opt(struct rev_info *revs, struct parse_opt_ctx_t *ctx,
1602 const struct option *options,
1603 const char * const usagestr[])
1604 {
1605 int n = handle_revision_opt(revs, ctx->argc, ctx->argv,
1606 &ctx->cpidx, ctx->out);
1607 if (n <= 0) {
1608 error("unknown option `%s'", ctx->argv[0]);
1609 usage_with_options(usagestr, options);
1610 }
1611 ctx->argv += n;
1612 ctx->argc -= n;
1613 }
1614
1615 static int for_each_bad_bisect_ref(const char *submodule, each_ref_fn fn, void *cb_data)
1616 {
1617 return for_each_ref_in_submodule(submodule, "refs/bisect/bad", fn, cb_data);
1618 }
1619
1620 static int for_each_good_bisect_ref(const char *submodule, each_ref_fn fn, void *cb_data)
1621 {
1622 return for_each_ref_in_submodule(submodule, "refs/bisect/good", fn, cb_data);
1623 }
1624
1625 static int handle_revision_pseudo_opt(const char *submodule,
1626 struct rev_info *revs,
1627 int argc, const char **argv, int *flags)
1628 {
1629 const char *arg = argv[0];
1630 const char *optarg;
1631 int argcount;
1632
1633 /*
1634 * NOTE!
1635 *
1636 * Commands like "git shortlog" will not accept the options below
1637 * unless parse_revision_opt queues them (as opposed to erroring
1638 * out).
1639 *
1640 * When implementing your new pseudo-option, remember to
1641 * register it in the list at the top of handle_revision_opt.
1642 */
1643 if (!strcmp(arg, "--all")) {
1644 handle_refs(submodule, revs, *flags, for_each_ref_submodule);
1645 handle_refs(submodule, revs, *flags, head_ref_submodule);
1646 } else if (!strcmp(arg, "--branches")) {
1647 handle_refs(submodule, revs, *flags, for_each_branch_ref_submodule);
1648 } else if (!strcmp(arg, "--bisect")) {
1649 handle_refs(submodule, revs, *flags, for_each_bad_bisect_ref);
1650 handle_refs(submodule, revs, *flags ^ UNINTERESTING, for_each_good_bisect_ref);
1651 revs->bisect = 1;
1652 } else if (!strcmp(arg, "--tags")) {
1653 handle_refs(submodule, revs, *flags, for_each_tag_ref_submodule);
1654 } else if (!strcmp(arg, "--remotes")) {
1655 handle_refs(submodule, revs, *flags, for_each_remote_ref_submodule);
1656 } else if ((argcount = parse_long_opt("glob", argv, &optarg))) {
1657 struct all_refs_cb cb;
1658 init_all_refs_cb(&cb, revs, *flags);
1659 for_each_glob_ref(handle_one_ref, optarg, &cb);
1660 return argcount;
1661 } else if (!prefixcmp(arg, "--branches=")) {
1662 struct all_refs_cb cb;
1663 init_all_refs_cb(&cb, revs, *flags);
1664 for_each_glob_ref_in(handle_one_ref, arg + 11, "refs/heads/", &cb);
1665 } else if (!prefixcmp(arg, "--tags=")) {
1666 struct all_refs_cb cb;
1667 init_all_refs_cb(&cb, revs, *flags);
1668 for_each_glob_ref_in(handle_one_ref, arg + 7, "refs/tags/", &cb);
1669 } else if (!prefixcmp(arg, "--remotes=")) {
1670 struct all_refs_cb cb;
1671 init_all_refs_cb(&cb, revs, *flags);
1672 for_each_glob_ref_in(handle_one_ref, arg + 10, "refs/remotes/", &cb);
1673 } else if (!strcmp(arg, "--reflog")) {
1674 handle_reflog(revs, *flags);
1675 } else if (!strcmp(arg, "--not")) {
1676 *flags ^= UNINTERESTING;
1677 } else if (!strcmp(arg, "--no-walk")) {
1678 revs->no_walk = 1;
1679 } else if (!strcmp(arg, "--do-walk")) {
1680 revs->no_walk = 0;
1681 } else {
1682 return 0;
1683 }
1684
1685 return 1;
1686 }
1687
1688 /*
1689 * Parse revision information, filling in the "rev_info" structure,
1690 * and removing the used arguments from the argument list.
1691 *
1692 * Returns the number of arguments left that weren't recognized
1693 * (which are also moved to the head of the argument list)
1694 */
1695 int setup_revisions(int argc, const char **argv, struct rev_info *revs, struct setup_revision_opt *opt)
1696 {
1697 int i, flags, left, seen_dashdash, read_from_stdin, got_rev_arg = 0;
1698 struct cmdline_pathspec prune_data;
1699 const char *submodule = NULL;
1700
1701 memset(&prune_data, 0, sizeof(prune_data));
1702 if (opt)
1703 submodule = opt->submodule;
1704
1705 /* First, search for "--" */
1706 seen_dashdash = 0;
1707 for (i = 1; i < argc; i++) {
1708 const char *arg = argv[i];
1709 if (strcmp(arg, "--"))
1710 continue;
1711 argv[i] = NULL;
1712 argc = i;
1713 if (argv[i + 1])
1714 append_prune_data(&prune_data, argv + i + 1);
1715 seen_dashdash = 1;
1716 break;
1717 }
1718
1719 /* Second, deal with arguments and options */
1720 flags = 0;
1721 read_from_stdin = 0;
1722 for (left = i = 1; i < argc; i++) {
1723 const char *arg = argv[i];
1724 if (*arg == '-') {
1725 int opts;
1726
1727 opts = handle_revision_pseudo_opt(submodule,
1728 revs, argc - i, argv + i,
1729 &flags);
1730 if (opts > 0) {
1731 i += opts - 1;
1732 continue;
1733 }
1734
1735 if (!strcmp(arg, "--stdin")) {
1736 if (revs->disable_stdin) {
1737 argv[left++] = arg;
1738 continue;
1739 }
1740 if (read_from_stdin++)
1741 die("--stdin given twice?");
1742 read_revisions_from_stdin(revs, &prune_data);
1743 continue;
1744 }
1745
1746 opts = handle_revision_opt(revs, argc - i, argv + i, &left, argv);
1747 if (opts > 0) {
1748 i += opts - 1;
1749 continue;
1750 }
1751 if (opts < 0)
1752 exit(128);
1753 continue;
1754 }
1755
1756 if (handle_revision_arg(arg, revs, flags, seen_dashdash)) {
1757 int j;
1758 if (seen_dashdash || *arg == '^')
1759 die("bad revision '%s'", arg);
1760
1761 /* If we didn't have a "--":
1762 * (1) all filenames must exist;
1763 * (2) all rev-args must not be interpretable
1764 * as a valid filename.
1765 * but the latter we have checked in the main loop.
1766 */
1767 for (j = i; j < argc; j++)
1768 verify_filename(revs->prefix, argv[j]);
1769
1770 append_prune_data(&prune_data, argv + i);
1771 break;
1772 }
1773 else
1774 got_rev_arg = 1;
1775 }
1776
1777 if (prune_data.nr) {
1778 /*
1779 * If we need to introduce the magic "a lone ':' means no
1780 * pathspec whatsoever", here is the place to do so.
1781 *
1782 * if (prune_data.nr == 1 && !strcmp(prune_data[0], ":")) {
1783 * prune_data.nr = 0;
1784 * prune_data.alloc = 0;
1785 * free(prune_data.path);
1786 * prune_data.path = NULL;
1787 * } else {
1788 * terminate prune_data.alloc with NULL and
1789 * call init_pathspec() to set revs->prune_data here.
1790 * }
1791 */
1792 ALLOC_GROW(prune_data.path, prune_data.nr+1, prune_data.alloc);
1793 prune_data.path[prune_data.nr++] = NULL;
1794 init_pathspec(&revs->prune_data,
1795 get_pathspec(revs->prefix, prune_data.path));
1796 }
1797
1798 if (revs->def == NULL)
1799 revs->def = opt ? opt->def : NULL;
1800 if (opt && opt->tweak)
1801 opt->tweak(revs, opt);
1802 if (revs->show_merge)
1803 prepare_show_merge(revs);
1804 if (revs->def && !revs->pending.nr && !got_rev_arg) {
1805 unsigned char sha1[20];
1806 struct object *object;
1807 unsigned mode;
1808 if (get_sha1_with_mode(revs->def, sha1, &mode))
1809 die("bad default revision '%s'", revs->def);
1810 object = get_reference(revs, revs->def, sha1, 0);
1811 add_pending_object_with_mode(revs, object, revs->def, mode);
1812 }
1813
1814 /* Did the user ask for any diff output? Run the diff! */
1815 if (revs->diffopt.output_format & ~DIFF_FORMAT_NO_OUTPUT)
1816 revs->diff = 1;
1817
1818 /* Pickaxe, diff-filter and rename following need diffs */
1819 if (revs->diffopt.pickaxe ||
1820 revs->diffopt.filter ||
1821 DIFF_OPT_TST(&revs->diffopt, FOLLOW_RENAMES))
1822 revs->diff = 1;
1823
1824 if (revs->topo_order)
1825 revs->limited = 1;
1826
1827 if (revs->prune_data.nr) {
1828 diff_tree_setup_paths(revs->prune_data.raw, &revs->pruning);
1829 /* Can't prune commits with rename following: the paths change.. */
1830 if (!DIFF_OPT_TST(&revs->diffopt, FOLLOW_RENAMES))
1831 revs->prune = 1;
1832 if (!revs->full_diff)
1833 diff_tree_setup_paths(revs->prune_data.raw, &revs->diffopt);
1834 }
1835 if (revs->combine_merges)
1836 revs->ignore_merges = 0;
1837 revs->diffopt.abbrev = revs->abbrev;
1838 if (diff_setup_done(&revs->diffopt) < 0)
1839 die("diff_setup_done failed");
1840
1841 compile_grep_patterns(&revs->grep_filter);
1842
1843 if (revs->reverse && revs->reflog_info)
1844 die("cannot combine --reverse with --walk-reflogs");
1845 if (revs->rewrite_parents && revs->children.name)
1846 die("cannot combine --parents and --children");
1847
1848 /*
1849 * Limitations on the graph functionality
1850 */
1851 if (revs->reverse && revs->graph)
1852 die("cannot combine --reverse with --graph");
1853
1854 if (revs->reflog_info && revs->graph)
1855 die("cannot combine --walk-reflogs with --graph");
1856
1857 return left;
1858 }
1859
1860 static void add_child(struct rev_info *revs, struct commit *parent, struct commit *child)
1861 {
1862 struct commit_list *l = xcalloc(1, sizeof(*l));
1863
1864 l->item = child;
1865 l->next = add_decoration(&revs->children, &parent->object, l);
1866 }
1867
1868 static int remove_duplicate_parents(struct commit *commit)
1869 {
1870 struct commit_list **pp, *p;
1871 int surviving_parents;
1872
1873 /* Examine existing parents while marking ones we have seen... */
1874 pp = &commit->parents;
1875 while ((p = *pp) != NULL) {
1876 struct commit *parent = p->item;
1877 if (parent->object.flags & TMP_MARK) {
1878 *pp = p->next;
1879 continue;
1880 }
1881 parent->object.flags |= TMP_MARK;
1882 pp = &p->next;
1883 }
1884 /* count them while clearing the temporary mark */
1885 surviving_parents = 0;
1886 for (p = commit->parents; p; p = p->next) {
1887 p->item->object.flags &= ~TMP_MARK;
1888 surviving_parents++;
1889 }
1890 return surviving_parents;
1891 }
1892
1893 struct merge_simplify_state {
1894 struct commit *simplified;
1895 };
1896
1897 static struct merge_simplify_state *locate_simplify_state(struct rev_info *revs, struct commit *commit)
1898 {
1899 struct merge_simplify_state *st;
1900
1901 st = lookup_decoration(&revs->merge_simplification, &commit->object);
1902 if (!st) {
1903 st = xcalloc(1, sizeof(*st));
1904 add_decoration(&revs->merge_simplification, &commit->object, st);
1905 }
1906 return st;
1907 }
1908
1909 static struct commit_list **simplify_one(struct rev_info *revs, struct commit *commit, struct commit_list **tail)
1910 {
1911 struct commit_list *p;
1912 struct merge_simplify_state *st, *pst;
1913 int cnt;
1914
1915 st = locate_simplify_state(revs, commit);
1916
1917 /*
1918 * Have we handled this one?
1919 */
1920 if (st->simplified)
1921 return tail;
1922
1923 /*
1924 * An UNINTERESTING commit simplifies to itself, so does a
1925 * root commit. We do not rewrite parents of such commit
1926 * anyway.
1927 */
1928 if ((commit->object.flags & UNINTERESTING) || !commit->parents) {
1929 st->simplified = commit;
1930 return tail;
1931 }
1932
1933 /*
1934 * Do we know what commit all of our parents should be rewritten to?
1935 * Otherwise we are not ready to rewrite this one yet.
1936 */
1937 for (cnt = 0, p = commit->parents; p; p = p->next) {
1938 pst = locate_simplify_state(revs, p->item);
1939 if (!pst->simplified) {
1940 tail = &commit_list_insert(p->item, tail)->next;
1941 cnt++;
1942 }
1943 }
1944 if (cnt) {
1945 tail = &commit_list_insert(commit, tail)->next;
1946 return tail;
1947 }
1948
1949 /*
1950 * Rewrite our list of parents.
1951 */
1952 for (p = commit->parents; p; p = p->next) {
1953 pst = locate_simplify_state(revs, p->item);
1954 p->item = pst->simplified;
1955 }
1956 cnt = remove_duplicate_parents(commit);
1957
1958 /*
1959 * It is possible that we are a merge and one side branch
1960 * does not have any commit that touches the given paths;
1961 * in such a case, the immediate parents will be rewritten
1962 * to different commits.
1963 *
1964 * o----X X: the commit we are looking at;
1965 * / / o: a commit that touches the paths;
1966 * ---o----'
1967 *
1968 * Further reduce the parents by removing redundant parents.
1969 */
1970 if (1 < cnt) {
1971 struct commit_list *h = reduce_heads(commit->parents);
1972 cnt = commit_list_count(h);
1973 free_commit_list(commit->parents);
1974 commit->parents = h;
1975 }
1976
1977 /*
1978 * A commit simplifies to itself if it is a root, if it is
1979 * UNINTERESTING, if it touches the given paths, or if it is a
1980 * merge and its parents simplifies to more than one commits
1981 * (the first two cases are already handled at the beginning of
1982 * this function).
1983 *
1984 * Otherwise, it simplifies to what its sole parent simplifies to.
1985 */
1986 if (!cnt ||
1987 (commit->object.flags & UNINTERESTING) ||
1988 !(commit->object.flags & TREESAME) ||
1989 (1 < cnt))
1990 st->simplified = commit;
1991 else {
1992 pst = locate_simplify_state(revs, commit->parents->item);
1993 st->simplified = pst->simplified;
1994 }
1995 return tail;
1996 }
1997
1998 static void simplify_merges(struct rev_info *revs)
1999 {
2000 struct commit_list *list;
2001 struct commit_list *yet_to_do, **tail;
2002
2003 if (!revs->topo_order)
2004 sort_in_topological_order(&revs->commits, revs->lifo);
2005 if (!revs->prune)
2006 return;
2007
2008 /* feed the list reversed */
2009 yet_to_do = NULL;
2010 for (list = revs->commits; list; list = list->next)
2011 commit_list_insert(list->item, &yet_to_do);
2012 while (yet_to_do) {
2013 list = yet_to_do;
2014 yet_to_do = NULL;
2015 tail = &yet_to_do;
2016 while (list) {
2017 struct commit *commit = list->item;
2018 struct commit_list *next = list->next;
2019 free(list);
2020 list = next;
2021 tail = simplify_one(revs, commit, tail);
2022 }
2023 }
2024
2025 /* clean up the result, removing the simplified ones */
2026 list = revs->commits;
2027 revs->commits = NULL;
2028 tail = &revs->commits;
2029 while (list) {
2030 struct commit *commit = list->item;
2031 struct commit_list *next = list->next;
2032 struct merge_simplify_state *st;
2033 free(list);
2034 list = next;
2035 st = locate_simplify_state(revs, commit);
2036 if (st->simplified == commit)
2037 tail = &commit_list_insert(commit, tail)->next;
2038 }
2039 }
2040
2041 static void set_children(struct rev_info *revs)
2042 {
2043 struct commit_list *l;
2044 for (l = revs->commits; l; l = l->next) {
2045 struct commit *commit = l->item;
2046 struct commit_list *p;
2047
2048 for (p = commit->parents; p; p = p->next)
2049 add_child(revs, p->item, commit);
2050 }
2051 }
2052
2053 int prepare_revision_walk(struct rev_info *revs)
2054 {
2055 int nr = revs->pending.nr;
2056 struct object_array_entry *e, *list;
2057
2058 e = list = revs->pending.objects;
2059 revs->pending.nr = 0;
2060 revs->pending.alloc = 0;
2061 revs->pending.objects = NULL;
2062 while (--nr >= 0) {
2063 struct commit *commit = handle_commit(revs, e->item, e->name);
2064 if (commit) {
2065 if (!(commit->object.flags & SEEN)) {
2066 commit->object.flags |= SEEN;
2067 commit_list_insert_by_date(commit, &revs->commits);
2068 }
2069 }
2070 e++;
2071 }
2072 if (!revs->leak_pending)
2073 free(list);
2074
2075 if (revs->no_walk)
2076 return 0;
2077 if (revs->limited)
2078 if (limit_list(revs) < 0)
2079 return -1;
2080 if (revs->topo_order)
2081 sort_in_topological_order(&revs->commits, revs->lifo);
2082 if (revs->simplify_merges)
2083 simplify_merges(revs);
2084 if (revs->children.name)
2085 set_children(revs);
2086 return 0;
2087 }
2088
2089 enum rewrite_result {
2090 rewrite_one_ok,
2091 rewrite_one_noparents,
2092 rewrite_one_error
2093 };
2094
2095 static enum rewrite_result rewrite_one(struct rev_info *revs, struct commit **pp)
2096 {
2097 struct commit_list *cache = NULL;
2098
2099 for (;;) {
2100 struct commit *p = *pp;
2101 if (!revs->limited)
2102 if (add_parents_to_list(revs, p, &revs->commits, &cache) < 0)
2103 return rewrite_one_error;
2104 if (p->parents && p->parents->next)
2105 return rewrite_one_ok;
2106 if (p->object.flags & UNINTERESTING)
2107 return rewrite_one_ok;
2108 if (!(p->object.flags & TREESAME))
2109 return rewrite_one_ok;
2110 if (!p->parents)
2111 return rewrite_one_noparents;
2112 *pp = p->parents->item;
2113 }
2114 }
2115
2116 static int rewrite_parents(struct rev_info *revs, struct commit *commit)
2117 {
2118 struct commit_list **pp = &commit->parents;
2119 while (*pp) {
2120 struct commit_list *parent = *pp;
2121 switch (rewrite_one(revs, &parent->item)) {
2122 case rewrite_one_ok:
2123 break;
2124 case rewrite_one_noparents:
2125 *pp = parent->next;
2126 continue;
2127 case rewrite_one_error:
2128 return -1;
2129 }
2130 pp = &parent->next;
2131 }
2132 remove_duplicate_parents(commit);
2133 return 0;
2134 }
2135
2136 static int commit_match(struct commit *commit, struct rev_info *opt)
2137 {
2138 if (!opt->grep_filter.pattern_list && !opt->grep_filter.header_list)
2139 return 1;
2140 return grep_buffer(&opt->grep_filter,
2141 commit->buffer, strlen(commit->buffer));
2142 }
2143
2144 static inline int want_ancestry(struct rev_info *revs)
2145 {
2146 return (revs->rewrite_parents || revs->children.name);
2147 }
2148
2149 enum commit_action get_commit_action(struct rev_info *revs, struct commit *commit)
2150 {
2151 if (commit->object.flags & SHOWN)
2152 return commit_ignore;
2153 if (revs->unpacked && has_sha1_pack(commit->object.sha1))
2154 return commit_ignore;
2155 if (revs->show_all)
2156 return commit_show;
2157 if (commit->object.flags & UNINTERESTING)
2158 return commit_ignore;
2159 if (revs->min_age != -1 && (commit->date > revs->min_age))
2160 return commit_ignore;
2161 if (revs->min_parents || (revs->max_parents >= 0)) {
2162 int n = 0;
2163 struct commit_list *p;
2164 for (p = commit->parents; p; p = p->next)
2165 n++;
2166 if ((n < revs->min_parents) ||
2167 ((revs->max_parents >= 0) && (n > revs->max_parents)))
2168 return commit_ignore;
2169 }
2170 if (!commit_match(commit, revs))
2171 return commit_ignore;
2172 if (revs->prune && revs->dense) {
2173 /* Commit without changes? */
2174 if (commit->object.flags & TREESAME) {
2175 /* drop merges unless we want parenthood */
2176 if (!want_ancestry(revs))
2177 return commit_ignore;
2178 /* non-merge - always ignore it */
2179 if (!commit->parents || !commit->parents->next)
2180 return commit_ignore;
2181 }
2182 }
2183 return commit_show;
2184 }
2185
2186 enum commit_action simplify_commit(struct rev_info *revs, struct commit *commit)
2187 {
2188 enum commit_action action = get_commit_action(revs, commit);
2189
2190 if (action == commit_show &&
2191 !revs->show_all &&
2192 revs->prune && revs->dense && want_ancestry(revs)) {
2193 if (rewrite_parents(revs, commit) < 0)
2194 return commit_error;
2195 }
2196 return action;
2197 }
2198
2199 static struct commit *get_revision_1(struct rev_info *revs)
2200 {
2201 if (!revs->commits)
2202 return NULL;
2203
2204 do {
2205 struct commit_list *entry = revs->commits;
2206 struct commit *commit = entry->item;
2207
2208 revs->commits = entry->next;
2209 free(entry);
2210
2211 if (revs->reflog_info) {
2212 fake_reflog_parent(revs->reflog_info, commit);
2213 commit->object.flags &= ~(ADDED | SEEN | SHOWN);
2214 }
2215
2216 /*
2217 * If we haven't done the list limiting, we need to look at
2218 * the parents here. We also need to do the date-based limiting
2219 * that we'd otherwise have done in limit_list().
2220 */
2221 if (!revs->limited) {
2222 if (revs->max_age != -1 &&
2223 (commit->date < revs->max_age))
2224 continue;
2225 if (add_parents_to_list(revs, commit, &revs->commits, NULL) < 0)
2226 die("Failed to traverse parents of commit %s",
2227 sha1_to_hex(commit->object.sha1));
2228 }
2229
2230 switch (simplify_commit(revs, commit)) {
2231 case commit_ignore:
2232 continue;
2233 case commit_error:
2234 die("Failed to simplify parents of commit %s",
2235 sha1_to_hex(commit->object.sha1));
2236 default:
2237 return commit;
2238 }
2239 } while (revs->commits);
2240 return NULL;
2241 }
2242
2243 static void gc_boundary(struct object_array *array)
2244 {
2245 unsigned nr = array->nr;
2246 unsigned alloc = array->alloc;
2247 struct object_array_entry *objects = array->objects;
2248
2249 if (alloc <= nr) {
2250 unsigned i, j;
2251 for (i = j = 0; i < nr; i++) {
2252 if (objects[i].item->flags & SHOWN)
2253 continue;
2254 if (i != j)
2255 objects[j] = objects[i];
2256 j++;
2257 }
2258 for (i = j; i < nr; i++)
2259 objects[i].item = NULL;
2260 array->nr = j;
2261 }
2262 }
2263
2264 static void create_boundary_commit_list(struct rev_info *revs)
2265 {
2266 unsigned i;
2267 struct commit *c;
2268 struct object_array *array = &revs->boundary_commits;
2269 struct object_array_entry *objects = array->objects;
2270
2271 /*
2272 * If revs->commits is non-NULL at this point, an error occurred in
2273 * get_revision_1(). Ignore the error and continue printing the
2274 * boundary commits anyway. (This is what the code has always
2275 * done.)
2276 */
2277 if (revs->commits) {
2278 free_commit_list(revs->commits);
2279 revs->commits = NULL;
2280 }
2281
2282 /*
2283 * Put all of the actual boundary commits from revs->boundary_commits
2284 * into revs->commits
2285 */
2286 for (i = 0; i < array->nr; i++) {
2287 c = (struct commit *)(objects[i].item);
2288 if (!c)
2289 continue;
2290 if (!(c->object.flags & CHILD_SHOWN))
2291 continue;
2292 if (c->object.flags & (SHOWN | BOUNDARY))
2293 continue;
2294 c->object.flags |= BOUNDARY;
2295 commit_list_insert(c, &revs->commits);
2296 }
2297
2298 /*
2299 * If revs->topo_order is set, sort the boundary commits
2300 * in topological order
2301 */
2302 sort_in_topological_order(&revs->commits, revs->lifo);
2303 }
2304
2305 static struct commit *get_revision_internal(struct rev_info *revs)
2306 {
2307 struct commit *c = NULL;
2308 struct commit_list *l;
2309
2310 if (revs->boundary == 2) {
2311 /*
2312 * All of the normal commits have already been returned,
2313 * and we are now returning boundary commits.
2314 * create_boundary_commit_list() has populated
2315 * revs->commits with the remaining commits to return.
2316 */
2317 c = pop_commit(&revs->commits);
2318 if (c)
2319 c->object.flags |= SHOWN;
2320 return c;
2321 }
2322
2323 /*
2324 * Now pick up what they want to give us
2325 */
2326 c = get_revision_1(revs);
2327 if (c) {
2328 while (0 < revs->skip_count) {
2329 revs->skip_count--;
2330 c = get_revision_1(revs);
2331 if (!c)
2332 break;
2333 }
2334 }
2335
2336 /*
2337 * Check the max_count.
2338 */
2339 switch (revs->max_count) {
2340 case -1:
2341 break;
2342 case 0:
2343 c = NULL;
2344 break;
2345 default:
2346 revs->max_count--;
2347 }
2348
2349 if (c)
2350 c->object.flags |= SHOWN;
2351
2352 if (!revs->boundary) {
2353 return c;
2354 }
2355
2356 if (!c) {
2357 /*
2358 * get_revision_1() runs out the commits, and
2359 * we are done computing the boundaries.
2360 * switch to boundary commits output mode.
2361 */
2362 revs->boundary = 2;
2363
2364 /*
2365 * Update revs->commits to contain the list of
2366 * boundary commits.
2367 */
2368 create_boundary_commit_list(revs);
2369
2370 return get_revision_internal(revs);
2371 }
2372
2373 /*
2374 * boundary commits are the commits that are parents of the
2375 * ones we got from get_revision_1() but they themselves are
2376 * not returned from get_revision_1(). Before returning
2377 * 'c', we need to mark its parents that they could be boundaries.
2378 */
2379
2380 for (l = c->parents; l; l = l->next) {
2381 struct object *p;
2382 p = &(l->item->object);
2383 if (p->flags & (CHILD_SHOWN | SHOWN))
2384 continue;
2385 p->flags |= CHILD_SHOWN;
2386 gc_boundary(&revs->boundary_commits);
2387 add_object_array(p, NULL, &revs->boundary_commits);
2388 }
2389
2390 return c;
2391 }
2392
2393 struct commit *get_revision(struct rev_info *revs)
2394 {
2395 struct commit *c;
2396 struct commit_list *reversed;
2397
2398 if (revs->reverse) {
2399 reversed = NULL;
2400 while ((c = get_revision_internal(revs))) {
2401 commit_list_insert(c, &reversed);
2402 }
2403 revs->commits = reversed;
2404 revs->reverse = 0;
2405 revs->reverse_output_stage = 1;
2406 }
2407
2408 if (revs->reverse_output_stage)
2409 return pop_commit(&revs->commits);
2410
2411 c = get_revision_internal(revs);
2412 if (c && revs->graph)
2413 graph_update(revs->graph, c);
2414 return c;
2415 }
2416
2417 char *get_revision_mark(const struct rev_info *revs, const struct commit *commit)
2418 {
2419 if (commit->object.flags & BOUNDARY)
2420 return "-";
2421 else if (commit->object.flags & UNINTERESTING)
2422 return "^";
2423 else if (commit->object.flags & PATCHSAME)
2424 return "=";
2425 else if (!revs || revs->left_right) {
2426 if (commit->object.flags & SYMMETRIC_LEFT)
2427 return "<";
2428 else
2429 return ">";
2430 } else if (revs->graph)
2431 return "*";
2432 else if (revs->cherry_mark)
2433 return "+";
2434 return "";
2435 }
2436
2437 void put_revision_mark(const struct rev_info *revs, const struct commit *commit)
2438 {
2439 char *mark = get_revision_mark(revs, commit);
2440 if (!strlen(mark))
2441 return;
2442 fputs(mark, stdout);
2443 putchar(' ');
2444 }