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