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