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