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