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