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