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