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