]> git.ipfire.org Git - thirdparty/git.git/blame - revision.c
Make git-send-email aware of Cc: lines.
[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
LT
64
65 desc.buf = tree->buffer;
66 desc.size = 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;
278 struct tree_desc empty, real;
279
280 if (!t1)
281 return 0;
282
8e440259 283 tree = read_object_with_reference(t1->object.sha1, tree_type, &real.size, NULL);
a4a88b2b
LT
284 if (!tree)
285 return 0;
286 real.buf = tree;
287
288 empty.buf = "";
289 empty.size = 0;
290
0a4ba7f8 291 tree_difference = REV_TREE_SAME;
dd47aa31 292 revs->pruning.has_changes = 0;
cd2bdc53 293 retval = diff_tree(&empty, &real, "", &revs->pruning);
a4a88b2b
LT
294 free(tree);
295
0a4ba7f8 296 return retval >= 0 && (tree_difference == REV_TREE_SAME);
a4a88b2b
LT
297}
298
299static void try_to_simplify_commit(struct rev_info *revs, struct commit *commit)
300{
301 struct commit_list **pp, *parent;
6631c736 302 int tree_changed = 0, tree_same = 0;
a4a88b2b
LT
303
304 if (!commit->tree)
305 return;
306
307 if (!commit->parents) {
c4e05b1a 308 if (!rev_same_tree_as_empty(revs, commit->tree))
a4a88b2b
LT
309 commit->object.flags |= TREECHANGE;
310 return;
311 }
312
313 pp = &commit->parents;
314 while ((parent = *pp) != NULL) {
315 struct commit *p = parent->item;
316
a4a88b2b 317 parse_commit(p);
c4e05b1a 318 switch (rev_compare_tree(revs, p->tree, commit->tree)) {
8efdc326 319 case REV_TREE_SAME:
6631c736 320 tree_same = 1;
9202434c 321 if (!revs->simplify_history || (p->object.flags & UNINTERESTING)) {
f3219fbb
JH
322 /* Even if a merge with an uninteresting
323 * side branch brought the entire change
324 * we are interested in, we do not want
325 * to lose the other branches of this
326 * merge, so we just keep going.
327 */
328 pp = &parent->next;
329 continue;
330 }
a4a88b2b
LT
331 parent->next = NULL;
332 commit->parents = parent;
333 return;
334
8efdc326
FK
335 case REV_TREE_NEW:
336 if (revs->remove_empty_trees &&
c4e05b1a 337 rev_same_tree_as_empty(revs, p->tree)) {
c348f31a
JH
338 /* We are adding all the specified
339 * paths from this parent, so the
340 * history beyond this parent is not
341 * interesting. Remove its parents
342 * (they are grandparents for us).
343 * IOW, we pretend this parent is a
344 * "root" commit.
a41e109c 345 */
c348f31a
JH
346 parse_commit(p);
347 p->parents = NULL;
a4a88b2b
LT
348 }
349 /* fallthrough */
8efdc326 350 case REV_TREE_DIFFERENT:
f3219fbb 351 tree_changed = 1;
a4a88b2b
LT
352 pp = &parent->next;
353 continue;
354 }
355 die("bad tree compare for commit %s", sha1_to_hex(commit->object.sha1));
356 }
6631c736 357 if (tree_changed && !tree_same)
f3219fbb 358 commit->object.flags |= TREECHANGE;
a4a88b2b
LT
359}
360
361static void add_parents_to_list(struct rev_info *revs, struct commit *commit, struct commit_list **list)
362{
363 struct commit_list *parent = commit->parents;
577ed5c2 364 unsigned left_flag;
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);
a4a88b2b
LT
410 parent = commit->parents;
411 while (parent) {
412 struct commit *p = parent->item;
413
414 parent = parent->next;
415
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;
421 insert_by_date(p, list);
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) {
489 warn("reflog of '%s' references pruned commits",
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
FK
569 revs->max_count = -1;
570
571 revs->prune_fn = NULL;
572 revs->prune_data = NULL;
573
574 revs->topo_setter = topo_sort_default_setter;
575 revs->topo_getter = topo_sort_default_getter;
cd2bdc53 576
cd2bdc53
LT
577 revs->commit_format = CMIT_FMT_DEFAULT;
578
579 diff_setup(&revs->diffopt);
8efdc326
FK
580}
581
0d2c9d67
RS
582static void add_pending_commit_list(struct rev_info *revs,
583 struct commit_list *commit_list,
584 unsigned int flags)
585{
586 while (commit_list) {
587 struct object *object = &commit_list->item->object;
588 object->flags |= flags;
589 add_pending_object(revs, object, sha1_to_hex(object->sha1));
590 commit_list = commit_list->next;
591 }
592}
593
ae3e5e1e
JH
594static void prepare_show_merge(struct rev_info *revs)
595{
596 struct commit_list *bases;
597 struct commit *head, *other;
598 unsigned char sha1[20];
599 const char **prune = NULL;
600 int i, prune_num = 1; /* counting terminating NULL */
601
602 if (get_sha1("HEAD", sha1) || !(head = lookup_commit(sha1)))
603 die("--merge without HEAD?");
604 if (get_sha1("MERGE_HEAD", sha1) || !(other = lookup_commit(sha1)))
605 die("--merge without MERGE_HEAD?");
606 add_pending_object(revs, &head->object, "HEAD");
607 add_pending_object(revs, &other->object, "MERGE_HEAD");
608 bases = get_merge_bases(head, other, 1);
609 while (bases) {
610 struct commit *it = bases->item;
611 struct commit_list *n = bases->next;
612 free(bases);
613 bases = n;
614 it->object.flags |= UNINTERESTING;
615 add_pending_object(revs, &it->object, "(merge-base)");
616 }
617
618 if (!active_nr)
619 read_cache();
620 for (i = 0; i < active_nr; i++) {
621 struct cache_entry *ce = active_cache[i];
622 if (!ce_stage(ce))
623 continue;
624 if (ce_path_match(ce, revs->prune_data)) {
625 prune_num++;
626 prune = xrealloc(prune, sizeof(*prune) * prune_num);
627 prune[prune_num-2] = ce->name;
628 prune[prune_num-1] = NULL;
629 }
630 while ((i+1 < active_nr) &&
631 ce_same_name(ce, active_cache[i+1]))
632 i++;
633 }
634 revs->prune_data = prune;
635}
636
5d6f0935
JH
637int handle_revision_arg(const char *arg, struct rev_info *revs,
638 int flags,
639 int cant_be_filename)
640{
641 char *dotdot;
642 struct object *object;
643 unsigned char sha1[20];
644 int local_flags;
645
646 dotdot = strstr(arg, "..");
647 if (dotdot) {
648 unsigned char from_sha1[20];
649 const char *next = dotdot + 2;
650 const char *this = arg;
651 int symmetric = *next == '.';
652 unsigned int flags_exclude = flags ^ UNINTERESTING;
653
654 *dotdot = 0;
655 next += symmetric;
656
657 if (!*next)
658 next = "HEAD";
659 if (dotdot == arg)
660 this = "HEAD";
661 if (!get_sha1(this, from_sha1) &&
662 !get_sha1(next, sha1)) {
663 struct commit *a, *b;
664 struct commit_list *exclude;
665
666 a = lookup_commit_reference(from_sha1);
667 b = lookup_commit_reference(sha1);
668 if (!a || !b) {
669 die(symmetric ?
670 "Invalid symmetric difference expression %s...%s" :
671 "Invalid revision range %s..%s",
672 arg, next);
673 }
674
675 if (!cant_be_filename) {
676 *dotdot = '.';
677 verify_non_filename(revs->prefix, arg);
678 }
679
680 if (symmetric) {
681 exclude = get_merge_bases(a, b, 1);
682 add_pending_commit_list(revs, exclude,
683 flags_exclude);
684 free_commit_list(exclude);
577ed5c2 685 a->object.flags |= flags | SYMMETRIC_LEFT;
5d6f0935
JH
686 } else
687 a->object.flags |= flags_exclude;
688 b->object.flags |= flags;
689 add_pending_object(revs, &a->object, this);
690 add_pending_object(revs, &b->object, next);
691 return 0;
692 }
693 *dotdot = '.';
694 }
695 dotdot = strstr(arg, "^@");
696 if (dotdot && !dotdot[2]) {
697 *dotdot = 0;
698 if (add_parents_only(revs, arg, flags))
699 return 0;
700 *dotdot = '^';
701 }
62476c8e
JH
702 dotdot = strstr(arg, "^!");
703 if (dotdot && !dotdot[2]) {
704 *dotdot = 0;
705 if (!add_parents_only(revs, arg, flags ^ UNINTERESTING))
706 *dotdot = '^';
707 }
708
5d6f0935
JH
709 local_flags = 0;
710 if (*arg == '^') {
711 local_flags = UNINTERESTING;
712 arg++;
713 }
714 if (get_sha1(arg, sha1))
715 return -1;
716 if (!cant_be_filename)
717 verify_non_filename(revs->prefix, arg);
718 object = get_reference(revs, arg, sha1, flags ^ local_flags);
719 add_pending_object(revs, object, arg);
720 return 0;
721}
722
2d10c555 723static void add_grep(struct rev_info *revs, const char *ptn, enum grep_pat_token what)
bd95fcd3 724{
2d10c555 725 if (!revs->grep_filter) {
bd95fcd3
JH
726 struct grep_opt *opt = xcalloc(1, sizeof(*opt));
727 opt->status_only = 1;
728 opt->pattern_tail = &(opt->pattern_list);
729 opt->regflags = REG_NEWLINE;
2d10c555 730 revs->grep_filter = opt;
bd95fcd3 731 }
2d10c555
JH
732 append_grep_pattern(revs->grep_filter, ptn,
733 "command line", 0, what);
734}
735
736static void add_header_grep(struct rev_info *revs, const char *field, const char *pattern)
737{
738 char *pat;
739 const char *prefix;
740 int patlen, fldlen;
bd95fcd3
JH
741
742 fldlen = strlen(field);
743 patlen = strlen(pattern);
a2ed6ae4
LT
744 pat = xmalloc(patlen + fldlen + 10);
745 prefix = ".*";
746 if (*pattern == '^') {
747 prefix = "";
748 pattern++;
749 }
750 sprintf(pat, "^%s %s%s", field, prefix, pattern);
2d10c555 751 add_grep(revs, pat, GREP_PATTERN_HEAD);
bd95fcd3
JH
752}
753
754static void add_message_grep(struct rev_info *revs, const char *pattern)
755{
2d10c555 756 add_grep(revs, pattern, GREP_PATTERN_BODY);
bd95fcd3
JH
757}
758
106d710b
JH
759static void add_ignore_packed(struct rev_info *revs, const char *name)
760{
761 int num = ++revs->num_ignore_packed;
762
763 revs->ignore_packed = xrealloc(revs->ignore_packed,
764 sizeof(const char **) * (num + 1));
765 revs->ignore_packed[num-1] = name;
766 revs->ignore_packed[num] = NULL;
767}
768
ae563542
LT
769/*
770 * Parse revision information, filling in the "rev_info" structure,
771 * and removing the used arguments from the argument list.
772 *
765ac8ec
LT
773 * Returns the number of arguments left that weren't recognized
774 * (which are also moved to the head of the argument list)
ae563542 775 */
a4a88b2b 776int setup_revisions(int argc, const char **argv, struct rev_info *revs, const char *def)
ae563542 777{
ae3e5e1e 778 int i, flags, seen_dashdash, show_merge;
765ac8ec 779 const char **unrecognized = argv + 1;
ae563542 780 int left = 1;
70d0afba 781 int all_match = 0;
ae563542 782
ae563542
LT
783 /* First, search for "--" */
784 seen_dashdash = 0;
785 for (i = 1; i < argc; i++) {
786 const char *arg = argv[i];
787 if (strcmp(arg, "--"))
788 continue;
789 argv[i] = NULL;
790 argc = i;
8efdc326 791 revs->prune_data = get_pathspec(revs->prefix, argv + i + 1);
ae563542
LT
792 seen_dashdash = 1;
793 break;
794 }
795
ae3e5e1e 796 flags = show_merge = 0;
ae563542 797 for (i = 1; i < argc; i++) {
ae563542 798 const char *arg = argv[i];
ae563542 799 if (*arg == '-') {
cd2bdc53 800 int opts;
cc44c765 801 if (!prefixcmp(arg, "--max-count=")) {
ae563542
LT
802 revs->max_count = atoi(arg + 12);
803 continue;
804 }
cc44c765 805 if (!prefixcmp(arg, "--skip=")) {
d5db6c9e
JH
806 revs->skip_count = atoi(arg + 7);
807 continue;
808 }
de5f2bf3 809 /* accept -<digit>, like traditional "head" */
64bc6e3d
JH
810 if ((*arg == '-') && isdigit(arg[1])) {
811 revs->max_count = atoi(arg + 1);
812 continue;
813 }
814 if (!strcmp(arg, "-n")) {
815 if (argc <= i + 1)
816 die("-n requires an argument");
817 revs->max_count = atoi(argv[++i]);
818 continue;
819 }
1968d77d 820 if (!prefixcmp(arg, "-n")) {
64bc6e3d
JH
821 revs->max_count = atoi(arg + 2);
822 continue;
823 }
cc44c765 824 if (!prefixcmp(arg, "--max-age=")) {
ae563542 825 revs->max_age = atoi(arg + 10);
ae563542
LT
826 continue;
827 }
cc44c765 828 if (!prefixcmp(arg, "--since=")) {
fd751667 829 revs->max_age = approxidate(arg + 8);
fd751667
JH
830 continue;
831 }
cc44c765 832 if (!prefixcmp(arg, "--after=")) {
fd751667 833 revs->max_age = approxidate(arg + 8);
53069686
JH
834 continue;
835 }
cc44c765 836 if (!prefixcmp(arg, "--min-age=")) {
53069686 837 revs->min_age = atoi(arg + 10);
fd751667
JH
838 continue;
839 }
cc44c765 840 if (!prefixcmp(arg, "--before=")) {
fd751667 841 revs->min_age = approxidate(arg + 9);
fd751667
JH
842 continue;
843 }
cc44c765 844 if (!prefixcmp(arg, "--until=")) {
fd751667 845 revs->min_age = approxidate(arg + 8);
fd751667
JH
846 continue;
847 }
ae563542
LT
848 if (!strcmp(arg, "--all")) {
849 handle_all(revs, flags);
850 continue;
851 }
63049292
JH
852 if (!strcmp(arg, "--reflog")) {
853 handle_reflog(revs, flags);
854 continue;
855 }
084ae0a7
JS
856 if (!strcmp(arg, "-g") ||
857 !strcmp(arg, "--walk-reflogs")) {
8860fd42
JS
858 init_reflog_walk(&revs->reflog_info);
859 continue;
860 }
ae563542
LT
861 if (!strcmp(arg, "--not")) {
862 flags ^= UNINTERESTING;
863 continue;
864 }
865 if (!strcmp(arg, "--default")) {
866 if (++i >= argc)
867 die("bad --default argument");
868 def = argv[i];
869 continue;
870 }
ae3e5e1e
JH
871 if (!strcmp(arg, "--merge")) {
872 show_merge = 1;
873 continue;
874 }
ae563542
LT
875 if (!strcmp(arg, "--topo-order")) {
876 revs->topo_order = 1;
877 continue;
878 }
879 if (!strcmp(arg, "--date-order")) {
880 revs->lifo = 0;
881 revs->topo_order = 1;
882 continue;
883 }
7b0c9966
LT
884 if (!strcmp(arg, "--parents")) {
885 revs->parents = 1;
886 continue;
887 }
ae563542
LT
888 if (!strcmp(arg, "--dense")) {
889 revs->dense = 1;
890 continue;
891 }
892 if (!strcmp(arg, "--sparse")) {
893 revs->dense = 0;
894 continue;
895 }
896 if (!strcmp(arg, "--remove-empty")) {
897 revs->remove_empty_trees = 1;
898 continue;
899 }
5cdeae71 900 if (!strcmp(arg, "--no-merges")) {
765ac8ec
LT
901 revs->no_merges = 1;
902 continue;
903 }
384e99a4
JH
904 if (!strcmp(arg, "--boundary")) {
905 revs->boundary = 1;
906 continue;
907 }
8dce8235
JH
908 if (!strcmp(arg, "--left-right")) {
909 revs->left_right = 1;
74bd9029
JH
910 continue;
911 }
ae563542
LT
912 if (!strcmp(arg, "--objects")) {
913 revs->tag_objects = 1;
914 revs->tree_objects = 1;
915 revs->blob_objects = 1;
916 continue;
917 }
918 if (!strcmp(arg, "--objects-edge")) {
919 revs->tag_objects = 1;
920 revs->tree_objects = 1;
921 revs->blob_objects = 1;
922 revs->edge_hint = 1;
923 continue;
924 }
d9a83684
LT
925 if (!strcmp(arg, "--unpacked")) {
926 revs->unpacked = 1;
106d710b
JH
927 free(revs->ignore_packed);
928 revs->ignore_packed = NULL;
929 revs->num_ignore_packed = 0;
930 continue;
931 }
cc44c765 932 if (!prefixcmp(arg, "--unpacked=")) {
106d710b
JH
933 revs->unpacked = 1;
934 add_ignore_packed(revs, arg+11);
d9a83684
LT
935 continue;
936 }
cd2bdc53
LT
937 if (!strcmp(arg, "-r")) {
938 revs->diff = 1;
939 revs->diffopt.recursive = 1;
940 continue;
941 }
942 if (!strcmp(arg, "-t")) {
943 revs->diff = 1;
944 revs->diffopt.recursive = 1;
945 revs->diffopt.tree_in_recursive = 1;
946 continue;
947 }
948 if (!strcmp(arg, "-m")) {
949 revs->ignore_merges = 0;
950 continue;
951 }
952 if (!strcmp(arg, "-c")) {
953 revs->diff = 1;
0fe7c1de 954 revs->dense_combined_merges = 0;
cd2bdc53
LT
955 revs->combine_merges = 1;
956 continue;
957 }
958 if (!strcmp(arg, "--cc")) {
959 revs->diff = 1;
960 revs->dense_combined_merges = 1;
961 revs->combine_merges = 1;
962 continue;
963 }
964 if (!strcmp(arg, "-v")) {
965 revs->verbose_header = 1;
cd2bdc53
LT
966 continue;
967 }
cc44c765 968 if (!prefixcmp(arg, "--pretty")) {
cd2bdc53 969 revs->verbose_header = 1;
cd2bdc53
LT
970 revs->commit_format = get_commit_format(arg+8);
971 continue;
972 }
973 if (!strcmp(arg, "--root")) {
974 revs->show_root_diff = 1;
975 continue;
976 }
977 if (!strcmp(arg, "--no-commit-id")) {
978 revs->no_commit_id = 1;
979 continue;
980 }
981 if (!strcmp(arg, "--always")) {
982 revs->always_show_header = 1;
983 continue;
984 }
985 if (!strcmp(arg, "--no-abbrev")) {
986 revs->abbrev = 0;
987 continue;
988 }
989 if (!strcmp(arg, "--abbrev")) {
990 revs->abbrev = DEFAULT_ABBREV;
991 continue;
992 }
cc44c765 993 if (!prefixcmp(arg, "--abbrev=")) {
508d9e37
LT
994 revs->abbrev = strtoul(arg + 9, NULL, 10);
995 if (revs->abbrev < MINIMUM_ABBREV)
996 revs->abbrev = MINIMUM_ABBREV;
997 else if (revs->abbrev > 40)
998 revs->abbrev = 40;
999 continue;
1000 }
cd2bdc53
LT
1001 if (!strcmp(arg, "--abbrev-commit")) {
1002 revs->abbrev_commit = 1;
1003 continue;
1004 }
1005 if (!strcmp(arg, "--full-diff")) {
1006 revs->diff = 1;
1007 revs->full_diff = 1;
1008 continue;
1009 }
9202434c
LT
1010 if (!strcmp(arg, "--full-history")) {
1011 revs->simplify_history = 0;
1012 continue;
1013 }
3dfb9278
JF
1014 if (!strcmp(arg, "--relative-date")) {
1015 revs->relative_date = 1;
1016 continue;
1017 }
2d10c555
JH
1018
1019 /*
1020 * Grepping the commit log
1021 */
cc44c765 1022 if (!prefixcmp(arg, "--author=")) {
bd95fcd3
JH
1023 add_header_grep(revs, "author", arg+9);
1024 continue;
1025 }
cc44c765 1026 if (!prefixcmp(arg, "--committer=")) {
bd95fcd3
JH
1027 add_header_grep(revs, "committer", arg+12);
1028 continue;
1029 }
cc44c765 1030 if (!prefixcmp(arg, "--grep=")) {
bd95fcd3
JH
1031 add_message_grep(revs, arg+7);
1032 continue;
1033 }
70d0afba
JH
1034 if (!strcmp(arg, "--all-match")) {
1035 all_match = 1;
1036 continue;
1037 }
cc44c765 1038 if (!prefixcmp(arg, "--encoding=")) {
7cbcf4d5
JH
1039 arg += 11;
1040 if (strcmp(arg, "none"))
1041 git_log_output_encoding = strdup(arg);
1042 else
1043 git_log_output_encoding = "";
1044 continue;
1045 }
9c5e66e9
JS
1046 if (!strcmp(arg, "--reverse")) {
1047 revs->reverse ^= 1;
1048 continue;
1049 }
2d10c555 1050
cd2bdc53
LT
1051 opts = diff_opt_parse(&revs->diffopt, argv+i, argc-i);
1052 if (opts > 0) {
1053 revs->diff = 1;
1054 i += opts - 1;
1055 continue;
1056 }
ae563542
LT
1057 *unrecognized++ = arg;
1058 left++;
1059 continue;
1060 }
ae563542 1061
5d6f0935
JH
1062 if (handle_revision_arg(arg, revs, flags, seen_dashdash)) {
1063 int j;
1064 if (seen_dashdash || *arg == '^')
ae563542
LT
1065 die("bad revision '%s'", arg);
1066
ea92f41f
JH
1067 /* If we didn't have a "--":
1068 * (1) all filenames must exist;
1069 * (2) all rev-args must not be interpretable
1070 * as a valid filename.
1071 * but the latter we have checked in the main loop.
1072 */
e23d0b4a
LT
1073 for (j = i; j < argc; j++)
1074 verify_filename(revs->prefix, argv[j]);
1075
5d6f0935
JH
1076 revs->prune_data = get_pathspec(revs->prefix,
1077 argv + i);
ae563542
LT
1078 break;
1079 }
ae563542 1080 }
5d6f0935 1081
ae3e5e1e
JH
1082 if (show_merge)
1083 prepare_show_merge(revs);
1f1e895f 1084 if (def && !revs->pending.nr) {
ae563542 1085 unsigned char sha1[20];
cd2bdc53 1086 struct object *object;
31fff305 1087 if (get_sha1(def, sha1))
ae563542 1088 die("bad default revision '%s'", def);
cd2bdc53
LT
1089 object = get_reference(revs, def, sha1, 0);
1090 add_pending_object(revs, object, def);
ae563542 1091 }
8efdc326 1092
9dad9d2e 1093 if (revs->topo_order)
53069686
JH
1094 revs->limited = 1;
1095
8efdc326 1096 if (revs->prune_data) {
cd2bdc53 1097 diff_tree_setup_paths(revs->prune_data, &revs->pruning);
8efdc326 1098 revs->prune_fn = try_to_simplify_commit;
cd2bdc53
LT
1099 if (!revs->full_diff)
1100 diff_tree_setup_paths(revs->prune_data, &revs->diffopt);
8efdc326 1101 }
cd2bdc53
LT
1102 if (revs->combine_merges) {
1103 revs->ignore_merges = 0;
0e677e1a 1104 if (revs->dense_combined_merges && !revs->diffopt.output_format)
cd2bdc53
LT
1105 revs->diffopt.output_format = DIFF_FORMAT_PATCH;
1106 }
cd2bdc53 1107 revs->diffopt.abbrev = revs->abbrev;
72ee96c0
JH
1108 if (diff_setup_done(&revs->diffopt) < 0)
1109 die("diff_setup_done failed");
8efdc326 1110
70d0afba
JH
1111 if (revs->grep_filter) {
1112 revs->grep_filter->all_match = all_match;
2d10c555 1113 compile_grep_patterns(revs->grep_filter);
70d0afba 1114 }
bd95fcd3 1115
ae563542
LT
1116 return left;
1117}
a4a88b2b
LT
1118
1119void prepare_revision_walk(struct rev_info *revs)
1120{
1f1e895f 1121 int nr = revs->pending.nr;
94d23673 1122 struct object_array_entry *e, *list;
cd2bdc53 1123
94d23673 1124 e = list = revs->pending.objects;
1f1e895f
LT
1125 revs->pending.nr = 0;
1126 revs->pending.alloc = 0;
1127 revs->pending.objects = NULL;
1128 while (--nr >= 0) {
94d23673 1129 struct commit *commit = handle_commit(revs, e->item, e->name);
cd2bdc53
LT
1130 if (commit) {
1131 if (!(commit->object.flags & SEEN)) {
1132 commit->object.flags |= SEEN;
1133 insert_by_date(commit, &revs->commits);
1134 }
1135 }
94d23673 1136 e++;
cd2bdc53 1137 }
94d23673 1138 free(list);
cd2bdc53 1139
ba1d4505
LT
1140 if (revs->no_walk)
1141 return;
a4a88b2b
LT
1142 if (revs->limited)
1143 limit_list(revs);
1144 if (revs->topo_order)
8efdc326
FK
1145 sort_in_topological_order_fn(&revs->commits, revs->lifo,
1146 revs->topo_setter,
1147 revs->topo_getter);
a4a88b2b
LT
1148}
1149
3381c790 1150static int rewrite_one(struct rev_info *revs, struct commit **pp)
765ac8ec
LT
1151{
1152 for (;;) {
1153 struct commit *p = *pp;
3381c790
LT
1154 if (!revs->limited)
1155 add_parents_to_list(revs, p, &revs->commits);
6631c736
LT
1156 if (p->parents && p->parents->next)
1157 return 0;
765ac8ec
LT
1158 if (p->object.flags & (TREECHANGE | UNINTERESTING))
1159 return 0;
1160 if (!p->parents)
1161 return -1;
1162 *pp = p->parents->item;
1163 }
1164}
1165
3381c790 1166static void rewrite_parents(struct rev_info *revs, struct commit *commit)
765ac8ec
LT
1167{
1168 struct commit_list **pp = &commit->parents;
1169 while (*pp) {
1170 struct commit_list *parent = *pp;
3381c790 1171 if (rewrite_one(revs, &parent->item) < 0) {
765ac8ec
LT
1172 *pp = parent->next;
1173 continue;
1174 }
1175 pp = &parent->next;
1176 }
1177}
1178
8ecae9b0
JH
1179static int commit_match(struct commit *commit, struct rev_info *opt)
1180{
2d10c555 1181 if (!opt->grep_filter)
8ecae9b0 1182 return 1;
2d10c555
JH
1183 return grep_buffer(opt->grep_filter,
1184 NULL, /* we say nothing, not even filename */
1185 commit->buffer, strlen(commit->buffer));
8ecae9b0
JH
1186}
1187
d5db6c9e 1188static struct commit *get_revision_1(struct rev_info *revs)
a4a88b2b 1189{
d5db6c9e 1190 if (!revs->commits)
a4a88b2b 1191 return NULL;
a4a88b2b 1192
765ac8ec 1193 do {
cb115748
LT
1194 struct commit_list *entry = revs->commits;
1195 struct commit *commit = entry->item;
ea5ed3ab 1196
cb115748
LT
1197 revs->commits = entry->next;
1198 free(entry);
2a0925be 1199
8860fd42
JS
1200 if (revs->reflog_info)
1201 fake_reflog_parent(revs->reflog_info, commit);
1202
2a0925be
LT
1203 /*
1204 * If we haven't done the list limiting, we need to look at
be7db6e5
LT
1205 * the parents here. We also need to do the date-based limiting
1206 * that we'd otherwise have done in limit_list().
2a0925be 1207 */
be7db6e5 1208 if (!revs->limited) {
744f4985 1209 if (revs->max_age != -1 &&
86ab4906
JH
1210 (commit->date < revs->max_age))
1211 continue;
1212 add_parents_to_list(revs, commit, &revs->commits);
be7db6e5 1213 }
384e99a4 1214 if (commit->object.flags & SHOWN)
2a0925be 1215 continue;
1b65a5aa 1216
744f4985
JH
1217 if (revs->unpacked && has_sha1_pack(commit->object.sha1,
1218 revs->ignore_packed))
1219 continue;
1220
1b65a5aa 1221 if (commit->object.flags & UNINTERESTING)
2a0925be 1222 continue;
765ac8ec 1223 if (revs->min_age != -1 && (commit->date > revs->min_age))
2a0925be 1224 continue;
384e99a4
JH
1225 if (revs->no_merges &&
1226 commit->parents && commit->parents->next)
2a0925be 1227 continue;
8ecae9b0
JH
1228 if (!commit_match(commit, revs))
1229 continue;
8efdc326 1230 if (revs->prune_fn && revs->dense) {
6631c736
LT
1231 /* Commit without changes? */
1232 if (!(commit->object.flags & TREECHANGE)) {
1233 /* drop merges unless we want parenthood */
1234 if (!revs->parents)
1235 continue;
1236 /* non-merge - always ignore it */
02d3dca3 1237 if (!commit->parents || !commit->parents->next)
6631c736
LT
1238 continue;
1239 }
2a0925be 1240 if (revs->parents)
3381c790 1241 rewrite_parents(revs, commit);
384e99a4 1242 }
765ac8ec
LT
1243 return commit;
1244 } while (revs->commits);
1245 return NULL;
1246}
d5db6c9e 1247
86ab4906
JH
1248static void gc_boundary(struct object_array *array)
1249{
1250 unsigned nr = array->nr;
1251 unsigned alloc = array->alloc;
1252 struct object_array_entry *objects = array->objects;
1253
1254 if (alloc <= nr) {
1255 unsigned i, j;
1256 for (i = j = 0; i < nr; i++) {
1257 if (objects[i].item->flags & SHOWN)
1258 continue;
1259 if (i != j)
1260 objects[j] = objects[i];
1261 j++;
1262 }
892ae6bf 1263 for (i = j; i < nr; i++)
86ab4906
JH
1264 objects[i].item = NULL;
1265 array->nr = j;
1266 }
1267}
1268
d5db6c9e
JH
1269struct commit *get_revision(struct rev_info *revs)
1270{
1271 struct commit *c = NULL;
86ab4906
JH
1272 struct commit_list *l;
1273
1274 if (revs->boundary == 2) {
1275 unsigned i;
1276 struct object_array *array = &revs->boundary_commits;
1277 struct object_array_entry *objects = array->objects;
1278 for (i = 0; i < array->nr; i++) {
1279 c = (struct commit *)(objects[i].item);
1280 if (!c)
1281 continue;
1282 if (!(c->object.flags & CHILD_SHOWN))
1283 continue;
1284 if (!(c->object.flags & SHOWN))
1285 break;
9c5e66e9 1286 }
86ab4906 1287 if (array->nr <= i)
9c5e66e9 1288 return NULL;
86ab4906
JH
1289
1290 c->object.flags |= SHOWN | BOUNDARY;
9c5e66e9
JS
1291 return c;
1292 }
1293
86ab4906 1294 if (revs->reverse) {
c33d8593
JH
1295 int limit = -1;
1296
1297 if (0 <= revs->max_count) {
1298 limit = revs->max_count;
1299 if (0 < revs->skip_count)
1300 limit += revs->skip_count;
1301 }
86ab4906 1302 l = NULL;
c33d8593 1303 while ((c = get_revision_1(revs))) {
86ab4906 1304 commit_list_insert(c, &l);
c33d8593
JH
1305 if ((0 < limit) && !--limit)
1306 break;
1307 }
86ab4906
JH
1308 revs->commits = l;
1309 revs->reverse = 0;
c33d8593 1310 revs->max_count = -1;
2b064697 1311 c = NULL;
d5db6c9e
JH
1312 }
1313
86ab4906
JH
1314 /*
1315 * Now pick up what they want to give us
1316 */
8839ac94
JH
1317 c = get_revision_1(revs);
1318 if (c) {
1319 while (0 < revs->skip_count) {
1320 revs->skip_count--;
1321 c = get_revision_1(revs);
1322 if (!c)
1323 break;
1324 }
86ab4906
JH
1325 }
1326
1327 /*
1328 * Check the max_count.
1329 */
d5db6c9e
JH
1330 switch (revs->max_count) {
1331 case -1:
1332 break;
1333 case 0:
86ab4906
JH
1334 c = NULL;
1335 break;
d5db6c9e
JH
1336 default:
1337 revs->max_count--;
1338 }
86ab4906 1339
c33d8593
JH
1340 if (c)
1341 c->object.flags |= SHOWN;
1342
1343 if (!revs->boundary) {
d5db6c9e 1344 return c;
c33d8593 1345 }
86ab4906
JH
1346
1347 if (!c) {
1348 /*
1349 * get_revision_1() runs out the commits, and
1350 * we are done computing the boundaries.
1351 * switch to boundary commits output mode.
1352 */
1353 revs->boundary = 2;
1354 return get_revision(revs);
1355 }
1356
1357 /*
1358 * boundary commits are the commits that are parents of the
1359 * ones we got from get_revision_1() but they themselves are
1360 * not returned from get_revision_1(). Before returning
1361 * 'c', we need to mark its parents that they could be boundaries.
1362 */
1363
1364 for (l = c->parents; l; l = l->next) {
1365 struct object *p;
1366 p = &(l->item->object);
c33d8593 1367 if (p->flags & (CHILD_SHOWN | SHOWN))
86ab4906
JH
1368 continue;
1369 p->flags |= CHILD_SHOWN;
1370 gc_boundary(&revs->boundary_commits);
1371 add_object_array(p, NULL, &revs->boundary_commits);
1372 }
1373
1374 return c;
d5db6c9e 1375}