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