]> git.ipfire.org Git - thirdparty/git.git/blame - revision.c
am: fix patch format detection for Thunderbird "Save As" emails
[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"
7fefda5c 9#include "graph.h"
8ecae9b0 10#include "grep.h"
8860fd42 11#include "reflog-walk.h"
d7a17cad 12#include "patch-ids.h"
f35f5603 13#include "decorate.h"
78892e32 14#include "log-tree.h"
ae563542 15
cdcefbc9
LT
16volatile show_early_output_fn_t show_early_output;
17
cf2ab916 18char *path_name(const struct name_path *path, const char *name)
ae563542 19{
cf2ab916 20 const struct name_path *p;
ae563542
LT
21 char *n, *m;
22 int nlen = strlen(name);
23 int len = nlen + 1;
24
25 for (p = path; p; p = p->up) {
26 if (p->elem_len)
27 len += p->elem_len + 1;
28 }
29 n = xmalloc(len);
30 m = n + len - (nlen + 1);
31 strcpy(m, name);
32 for (p = path; p; p = p->up) {
33 if (p->elem_len) {
34 m -= p->elem_len + 1;
35 memcpy(m, p->elem, p->elem_len);
36 m[p->elem_len] = '/';
37 }
38 }
39 return n;
40}
41
1f1e895f
LT
42void add_object(struct object *obj,
43 struct object_array *p,
44 struct name_path *path,
45 const char *name)
ae563542 46{
1f1e895f 47 add_object_array(obj, path_name(path, name), p);
ae563542
LT
48}
49
50static void mark_blob_uninteresting(struct blob *blob)
51{
c1ee9013
MK
52 if (!blob)
53 return;
ae563542
LT
54 if (blob->object.flags & UNINTERESTING)
55 return;
56 blob->object.flags |= UNINTERESTING;
57}
58
59void mark_tree_uninteresting(struct tree *tree)
60{
f75e53ed 61 struct tree_desc desc;
4c068a98 62 struct name_entry entry;
ae563542 63 struct object *obj = &tree->object;
ae563542 64
c1ee9013
MK
65 if (!tree)
66 return;
ae563542
LT
67 if (obj->flags & UNINTERESTING)
68 return;
69 obj->flags |= UNINTERESTING;
70 if (!has_sha1_file(obj->sha1))
71 return;
72 if (parse_tree(tree) < 0)
73 die("bad tree %s", sha1_to_hex(obj->sha1));
f75e53ed 74
6fda5e51 75 init_tree_desc(&desc, tree->buffer, tree->size);
4c068a98 76 while (tree_entry(&desc, &entry)) {
4d1012c3
LT
77 switch (object_type(entry.mode)) {
78 case OBJ_TREE:
4c068a98 79 mark_tree_uninteresting(lookup_tree(entry.sha1));
4d1012c3
LT
80 break;
81 case OBJ_BLOB:
4c068a98 82 mark_blob_uninteresting(lookup_blob(entry.sha1));
4d1012c3
LT
83 break;
84 default:
85 /* Subproject commit - not in this repository */
86 break;
87 }
ae563542 88 }
f75e53ed
LT
89
90 /*
91 * We don't care about the tree any more
92 * after it has been marked uninteresting.
93 */
94 free(tree->buffer);
95 tree->buffer = NULL;
ae563542
LT
96}
97
98void mark_parents_uninteresting(struct commit *commit)
99{
100 struct commit_list *parents = commit->parents;
101
102 while (parents) {
103 struct commit *commit = parents->item;
d2c4af73
MU
104 if (!(commit->object.flags & UNINTERESTING)) {
105 commit->object.flags |= UNINTERESTING;
ae563542 106
d2c4af73
MU
107 /*
108 * Normally we haven't parsed the parent
109 * yet, so we won't have a parent of a parent
110 * here. However, it may turn out that we've
111 * reached this commit some other way (where it
112 * wasn't uninteresting), in which case we need
113 * to mark its parents recursively too..
114 */
115 if (commit->parents)
116 mark_parents_uninteresting(commit);
117 }
ae563542
LT
118
119 /*
120 * A missing commit is ok iff its parent is marked
121 * uninteresting.
122 *
123 * We just mark such a thing parsed, so that when
124 * it is popped next time around, we won't be trying
125 * to parse it and get an error.
126 */
127 if (!has_sha1_file(commit->object.sha1))
128 commit->object.parsed = 1;
129 parents = parents->next;
130 }
131}
132
2d93b9fa 133static void add_pending_object_with_mode(struct rev_info *revs, struct object *obj, const char *name, unsigned mode)
ae563542 134{
aa27e461 135 if (revs->no_walk && (obj->flags & UNINTERESTING))
f222abde 136 revs->no_walk = 0;
7b69b873
JS
137 if (revs->reflog_info && obj->type == OBJ_COMMIT &&
138 add_reflog_for_walk(revs->reflog_info,
139 (struct commit *)obj, name))
140 return;
bb6c2fba 141 add_object_array_with_mode(obj, name, &revs->pending, mode);
ae563542
LT
142}
143
2d93b9fa
JH
144void add_pending_object(struct rev_info *revs, struct object *obj, const char *name)
145{
146 add_pending_object_with_mode(revs, obj, name, S_IFINVALID);
147}
148
3384a2df
JH
149void add_head_to_pending(struct rev_info *revs)
150{
151 unsigned char sha1[20];
152 struct object *obj;
153 if (get_sha1("HEAD", sha1))
154 return;
155 obj = parse_object(sha1);
156 if (!obj)
157 return;
158 add_pending_object(revs, obj, "HEAD");
159}
160
cd2bdc53 161static struct object *get_reference(struct rev_info *revs, const char *name, const unsigned char *sha1, unsigned int flags)
ae563542
LT
162{
163 struct object *object;
164
165 object = parse_object(sha1);
166 if (!object)
167 die("bad object %s", name);
cd2bdc53
LT
168 object->flags |= flags;
169 return object;
170}
171
172static struct commit *handle_commit(struct rev_info *revs, struct object *object, const char *name)
173{
174 unsigned long flags = object->flags;
ae563542
LT
175
176 /*
177 * Tag object? Look what it points to..
178 */
1974632c 179 while (object->type == OBJ_TAG) {
ae563542 180 struct tag *tag = (struct tag *) object;
cd2bdc53 181 if (revs->tag_objects && !(flags & UNINTERESTING))
ae563542 182 add_pending_object(revs, object, tag->tag);
9684afd9
MK
183 if (!tag->tagged)
184 die("bad tag");
ae563542 185 object = parse_object(tag->tagged->sha1);
aeeae1b7
JH
186 if (!object) {
187 if (flags & UNINTERESTING)
188 return NULL;
ae563542 189 die("bad object %s", sha1_to_hex(tag->tagged->sha1));
aeeae1b7 190 }
ae563542
LT
191 }
192
193 /*
194 * Commit object? Just return it, we'll do all the complex
195 * reachability crud.
196 */
1974632c 197 if (object->type == OBJ_COMMIT) {
ae563542 198 struct commit *commit = (struct commit *)object;
ae563542
LT
199 if (parse_commit(commit) < 0)
200 die("unable to parse commit %s", name);
d9a83684 201 if (flags & UNINTERESTING) {
4262c1b0 202 commit->object.flags |= UNINTERESTING;
ae563542 203 mark_parents_uninteresting(commit);
d9a83684
LT
204 revs->limited = 1;
205 }
0f3a290b
LT
206 if (revs->show_source && !commit->util)
207 commit->util = (void *) name;
ae563542
LT
208 return commit;
209 }
210
211 /*
3ea3c215 212 * Tree object? Either mark it uninteresting, or add it
ae563542
LT
213 * to the list of objects to look at later..
214 */
1974632c 215 if (object->type == OBJ_TREE) {
ae563542
LT
216 struct tree *tree = (struct tree *)object;
217 if (!revs->tree_objects)
218 return NULL;
219 if (flags & UNINTERESTING) {
220 mark_tree_uninteresting(tree);
221 return NULL;
222 }
223 add_pending_object(revs, object, "");
224 return NULL;
225 }
226
227 /*
228 * Blob object? You know the drill by now..
229 */
1974632c 230 if (object->type == OBJ_BLOB) {
ae563542
LT
231 struct blob *blob = (struct blob *)object;
232 if (!revs->blob_objects)
233 return NULL;
234 if (flags & UNINTERESTING) {
235 mark_blob_uninteresting(blob);
236 return NULL;
237 }
238 add_pending_object(revs, object, "");
239 return NULL;
240 }
241 die("%s is unknown object", name);
242}
243
a4a88b2b
LT
244static int everybody_uninteresting(struct commit_list *orig)
245{
246 struct commit_list *list = orig;
247 while (list) {
248 struct commit *commit = list->item;
249 list = list->next;
250 if (commit->object.flags & UNINTERESTING)
251 continue;
252 return 0;
253 }
254 return 1;
255}
256
0a4ba7f8
JH
257/*
258 * The goal is to get REV_TREE_NEW as the result only if the
ceff8e7a
LT
259 * diff consists of all '+' (and no other changes), REV_TREE_OLD
260 * if the whole diff is removal of old data, and otherwise
261 * REV_TREE_DIFFERENT (of course if the trees are the same we
262 * want REV_TREE_SAME).
263 * That means that once we get to REV_TREE_DIFFERENT, we do not
264 * have to look any further.
0a4ba7f8 265 */
8efdc326 266static int tree_difference = REV_TREE_SAME;
a4a88b2b
LT
267
268static void file_add_remove(struct diff_options *options,
269 int addremove, unsigned mode,
270 const unsigned char *sha1,
e3d42c47 271 const char *fullpath, unsigned dirty_submodule)
a4a88b2b 272{
ceff8e7a 273 int diff = addremove == '+' ? REV_TREE_NEW : REV_TREE_OLD;
a4a88b2b 274
ceff8e7a 275 tree_difference |= diff;
dd47aa31 276 if (tree_difference == REV_TREE_DIFFERENT)
8f67f8ae 277 DIFF_OPT_SET(options, HAS_CHANGES);
a4a88b2b
LT
278}
279
280static void file_change(struct diff_options *options,
281 unsigned old_mode, unsigned new_mode,
282 const unsigned char *old_sha1,
283 const unsigned char *new_sha1,
e3d42c47
JL
284 const char *fullpath,
285 unsigned old_dirty_submodule, unsigned new_dirty_submodule)
a4a88b2b 286{
8efdc326 287 tree_difference = REV_TREE_DIFFERENT;
8f67f8ae 288 DIFF_OPT_SET(options, HAS_CHANGES);
a4a88b2b
LT
289}
290
3a5e8608 291static int rev_compare_tree(struct rev_info *revs, struct commit *parent, struct commit *commit)
a4a88b2b 292{
3a5e8608
LT
293 struct tree *t1 = parent->tree;
294 struct tree *t2 = commit->tree;
295
a4a88b2b 296 if (!t1)
8efdc326 297 return REV_TREE_NEW;
ceff8e7a
LT
298 if (!t2)
299 return REV_TREE_OLD;
78892e32
LT
300
301 if (revs->simplify_by_decoration) {
302 /*
303 * If we are simplifying by decoration, then the commit
304 * is worth showing if it has a tag pointing at it.
305 */
306 if (lookup_decoration(&name_decoration, &commit->object))
307 return REV_TREE_DIFFERENT;
308 /*
309 * A commit that is not pointed by a tag is uninteresting
310 * if we are not limited by path. This means that you will
311 * see the usual "commits that touch the paths" plus any
312 * tagged commit by specifying both --simplify-by-decoration
313 * and pathspec.
314 */
315 if (!revs->prune_data)
316 return REV_TREE_SAME;
317 }
ceff8e7a 318
8efdc326 319 tree_difference = REV_TREE_SAME;
8f67f8ae 320 DIFF_OPT_CLR(&revs->pruning, HAS_CHANGES);
c4e05b1a 321 if (diff_tree_sha1(t1->object.sha1, t2->object.sha1, "",
cd2bdc53 322 &revs->pruning) < 0)
8efdc326 323 return REV_TREE_DIFFERENT;
a4a88b2b
LT
324 return tree_difference;
325}
326
3a5e8608 327static int rev_same_tree_as_empty(struct rev_info *revs, struct commit *commit)
a4a88b2b
LT
328{
329 int retval;
330 void *tree;
6fda5e51 331 unsigned long size;
a4a88b2b 332 struct tree_desc empty, real;
3a5e8608 333 struct tree *t1 = commit->tree;
a4a88b2b
LT
334
335 if (!t1)
336 return 0;
337
6fda5e51 338 tree = read_object_with_reference(t1->object.sha1, tree_type, &size, NULL);
a4a88b2b
LT
339 if (!tree)
340 return 0;
6fda5e51
LT
341 init_tree_desc(&real, tree, size);
342 init_tree_desc(&empty, "", 0);
a4a88b2b 343
0a4ba7f8 344 tree_difference = REV_TREE_SAME;
8f67f8ae 345 DIFF_OPT_CLR(&revs->pruning, HAS_CHANGES);
cd2bdc53 346 retval = diff_tree(&empty, &real, "", &revs->pruning);
a4a88b2b
LT
347 free(tree);
348
0a4ba7f8 349 return retval >= 0 && (tree_difference == REV_TREE_SAME);
a4a88b2b
LT
350}
351
352static void try_to_simplify_commit(struct rev_info *revs, struct commit *commit)
353{
354 struct commit_list **pp, *parent;
6631c736 355 int tree_changed = 0, tree_same = 0;
a4a88b2b 356
53b2c823
LT
357 /*
358 * If we don't do pruning, everything is interesting
359 */
7dc0fe3b 360 if (!revs->prune)
53b2c823 361 return;
53b2c823 362
a4a88b2b
LT
363 if (!commit->tree)
364 return;
365
366 if (!commit->parents) {
3a5e8608 367 if (rev_same_tree_as_empty(revs, commit))
7dc0fe3b 368 commit->object.flags |= TREESAME;
a4a88b2b
LT
369 return;
370 }
371
53b2c823
LT
372 /*
373 * Normal non-merge commit? If we don't want to make the
374 * history dense, we consider it always to be a change..
375 */
7dc0fe3b 376 if (!revs->dense && !commit->parents->next)
53b2c823 377 return;
53b2c823 378
a4a88b2b
LT
379 pp = &commit->parents;
380 while ((parent = *pp) != NULL) {
381 struct commit *p = parent->item;
382
cc0e6c5a
AR
383 if (parse_commit(p) < 0)
384 die("cannot simplify commit %s (because of %s)",
385 sha1_to_hex(commit->object.sha1),
386 sha1_to_hex(p->object.sha1));
3a5e8608 387 switch (rev_compare_tree(revs, p, commit)) {
8efdc326 388 case REV_TREE_SAME:
6631c736 389 tree_same = 1;
9202434c 390 if (!revs->simplify_history || (p->object.flags & UNINTERESTING)) {
f3219fbb
JH
391 /* Even if a merge with an uninteresting
392 * side branch brought the entire change
393 * we are interested in, we do not want
394 * to lose the other branches of this
395 * merge, so we just keep going.
396 */
397 pp = &parent->next;
398 continue;
399 }
a4a88b2b
LT
400 parent->next = NULL;
401 commit->parents = parent;
7dc0fe3b 402 commit->object.flags |= TREESAME;
a4a88b2b
LT
403 return;
404
8efdc326
FK
405 case REV_TREE_NEW:
406 if (revs->remove_empty_trees &&
3a5e8608 407 rev_same_tree_as_empty(revs, p)) {
c348f31a
JH
408 /* We are adding all the specified
409 * paths from this parent, so the
410 * history beyond this parent is not
411 * interesting. Remove its parents
412 * (they are grandparents for us).
413 * IOW, we pretend this parent is a
414 * "root" commit.
a41e109c 415 */
cc0e6c5a
AR
416 if (parse_commit(p) < 0)
417 die("cannot simplify commit %s (invalid %s)",
418 sha1_to_hex(commit->object.sha1),
419 sha1_to_hex(p->object.sha1));
c348f31a 420 p->parents = NULL;
a4a88b2b
LT
421 }
422 /* fallthrough */
ceff8e7a 423 case REV_TREE_OLD:
8efdc326 424 case REV_TREE_DIFFERENT:
f3219fbb 425 tree_changed = 1;
a4a88b2b
LT
426 pp = &parent->next;
427 continue;
428 }
429 die("bad tree compare for commit %s", sha1_to_hex(commit->object.sha1));
430 }
6631c736 431 if (tree_changed && !tree_same)
7dc0fe3b
LT
432 return;
433 commit->object.flags |= TREESAME;
a4a88b2b
LT
434}
435
fce87ae5
AG
436static void insert_by_date_cached(struct commit *p, struct commit_list **head,
437 struct commit_list *cached_base, struct commit_list **cache)
438{
439 struct commit_list *new_entry;
440
441 if (cached_base && p->date < cached_base->item->date)
442 new_entry = insert_by_date(p, &cached_base->next);
443 else
444 new_entry = insert_by_date(p, head);
445
446 if (cache && (!*cache || p->date < (*cache)->item->date))
447 *cache = new_entry;
448}
449
450static int add_parents_to_list(struct rev_info *revs, struct commit *commit,
451 struct commit_list **list, struct commit_list **cache_ptr)
a4a88b2b
LT
452{
453 struct commit_list *parent = commit->parents;
577ed5c2 454 unsigned left_flag;
fce87ae5 455 struct commit_list *cached_base = cache_ptr ? *cache_ptr : NULL;
a4a88b2b 456
3381c790 457 if (commit->object.flags & ADDED)
cc0e6c5a 458 return 0;
3381c790
LT
459 commit->object.flags |= ADDED;
460
a4a88b2b
LT
461 /*
462 * If the commit is uninteresting, don't try to
463 * prune parents - we want the maximal uninteresting
464 * set.
465 *
466 * Normally we haven't parsed the parent
467 * yet, so we won't have a parent of a parent
468 * here. However, it may turn out that we've
469 * reached this commit some other way (where it
470 * wasn't uninteresting), in which case we need
471 * to mark its parents recursively too..
472 */
473 if (commit->object.flags & UNINTERESTING) {
474 while (parent) {
475 struct commit *p = parent->item;
476 parent = parent->next;
aeeae1b7
JH
477 if (p)
478 p->object.flags |= UNINTERESTING;
cc0e6c5a 479 if (parse_commit(p) < 0)
aeeae1b7 480 continue;
a4a88b2b
LT
481 if (p->parents)
482 mark_parents_uninteresting(p);
483 if (p->object.flags & SEEN)
484 continue;
485 p->object.flags |= SEEN;
fce87ae5 486 insert_by_date_cached(p, list, cached_base, cache_ptr);
a4a88b2b 487 }
cc0e6c5a 488 return 0;
a4a88b2b
LT
489 }
490
491 /*
492 * Ok, the commit wasn't uninteresting. Try to
493 * simplify the commit history and find the parent
494 * that has no differences in the path set if one exists.
495 */
53b2c823 496 try_to_simplify_commit(revs, commit);
a4a88b2b 497
ba1d4505 498 if (revs->no_walk)
cc0e6c5a 499 return 0;
ba1d4505 500
577ed5c2 501 left_flag = (commit->object.flags & SYMMETRIC_LEFT);
0053e902 502
d9c292e8 503 for (parent = commit->parents; parent; parent = parent->next) {
a4a88b2b
LT
504 struct commit *p = parent->item;
505
cc0e6c5a
AR
506 if (parse_commit(p) < 0)
507 return -1;
0f3a290b
LT
508 if (revs->show_source && !p->util)
509 p->util = commit->util;
577ed5c2 510 p->object.flags |= left_flag;
ad1012eb
LH
511 if (!(p->object.flags & SEEN)) {
512 p->object.flags |= SEEN;
fce87ae5 513 insert_by_date_cached(p, list, cached_base, cache_ptr);
ad1012eb 514 }
60d30b02 515 if (revs->first_parent_only)
d9c292e8 516 break;
a4a88b2b 517 }
cc0e6c5a 518 return 0;
a4a88b2b
LT
519}
520
36d56de6 521static void cherry_pick_list(struct commit_list *list, struct rev_info *revs)
d7a17cad
JH
522{
523 struct commit_list *p;
524 int left_count = 0, right_count = 0;
525 int left_first;
526 struct patch_ids ids;
527
528 /* First count the commits on the left and on the right */
529 for (p = list; p; p = p->next) {
530 struct commit *commit = p->item;
531 unsigned flags = commit->object.flags;
532 if (flags & BOUNDARY)
533 ;
534 else if (flags & SYMMETRIC_LEFT)
535 left_count++;
536 else
537 right_count++;
538 }
539
540 left_first = left_count < right_count;
541 init_patch_ids(&ids);
36d56de6
JS
542 if (revs->diffopt.nr_paths) {
543 ids.diffopts.nr_paths = revs->diffopt.nr_paths;
544 ids.diffopts.paths = revs->diffopt.paths;
545 ids.diffopts.pathlens = revs->diffopt.pathlens;
546 }
d7a17cad
JH
547
548 /* Compute patch-ids for one side */
549 for (p = list; p; p = p->next) {
550 struct commit *commit = p->item;
551 unsigned flags = commit->object.flags;
552
553 if (flags & BOUNDARY)
554 continue;
555 /*
556 * If we have fewer left, left_first is set and we omit
557 * commits on the right branch in this loop. If we have
558 * fewer right, we skip the left ones.
559 */
560 if (left_first != !!(flags & SYMMETRIC_LEFT))
561 continue;
562 commit->util = add_commit_patch_id(commit, &ids);
563 }
564
565 /* Check the other side */
566 for (p = list; p; p = p->next) {
567 struct commit *commit = p->item;
568 struct patch_id *id;
569 unsigned flags = commit->object.flags;
570
571 if (flags & BOUNDARY)
572 continue;
573 /*
574 * If we have fewer left, left_first is set and we omit
575 * commits on the left branch in this loop.
576 */
577 if (left_first == !!(flags & SYMMETRIC_LEFT))
578 continue;
579
580 /*
581 * Have we seen the same patch id?
582 */
583 id = has_commit_patch_id(commit, &ids);
584 if (!id)
585 continue;
586 id->seen = 1;
587 commit->object.flags |= SHOWN;
588 }
589
590 /* Now check the original side for seen ones */
591 for (p = list; p; p = p->next) {
592 struct commit *commit = p->item;
593 struct patch_id *ent;
594
595 ent = commit->util;
596 if (!ent)
597 continue;
598 if (ent->seen)
599 commit->object.flags |= SHOWN;
600 commit->util = NULL;
601 }
602
603 free_patch_ids(&ids);
604}
605
7d004199
LT
606/* How many extra uninteresting commits we want to see.. */
607#define SLOP 5
608
609static int still_interesting(struct commit_list *src, unsigned long date, int slop)
3131b713 610{
7d004199
LT
611 /*
612 * No source list at all? We're definitely done..
613 */
614 if (!src)
615 return 0;
616
617 /*
618 * Does the destination list contain entries with a date
619 * before the source list? Definitely _not_ done.
620 */
621 if (date < src->item->date)
622 return SLOP;
623
624 /*
625 * Does the source list still have interesting commits in
626 * it? Definitely not done..
627 */
628 if (!everybody_uninteresting(src))
629 return SLOP;
630
631 /* Ok, we're closing in.. */
632 return slop-1;
3131b713
LT
633}
634
cc0e6c5a 635static int limit_list(struct rev_info *revs)
a4a88b2b 636{
7d004199
LT
637 int slop = SLOP;
638 unsigned long date = ~0ul;
a4a88b2b
LT
639 struct commit_list *list = revs->commits;
640 struct commit_list *newlist = NULL;
641 struct commit_list **p = &newlist;
642
643 while (list) {
644 struct commit_list *entry = list;
645 struct commit *commit = list->item;
646 struct object *obj = &commit->object;
cdcefbc9 647 show_early_output_fn_t show;
a4a88b2b
LT
648
649 list = list->next;
650 free(entry);
651
652 if (revs->max_age != -1 && (commit->date < revs->max_age))
653 obj->flags |= UNINTERESTING;
fce87ae5 654 if (add_parents_to_list(revs, commit, &list, NULL) < 0)
cc0e6c5a 655 return -1;
a4a88b2b
LT
656 if (obj->flags & UNINTERESTING) {
657 mark_parents_uninteresting(commit);
7d004199
LT
658 if (revs->show_all)
659 p = &commit_list_insert(commit, p)->next;
660 slop = still_interesting(list, date, slop);
661 if (slop)
3131b713 662 continue;
7d004199
LT
663 /* If showing all, add the whole pending list to the end */
664 if (revs->show_all)
665 *p = list;
666 break;
a4a88b2b
LT
667 }
668 if (revs->min_age != -1 && (commit->date > revs->min_age))
669 continue;
7d004199 670 date = commit->date;
a4a88b2b 671 p = &commit_list_insert(commit, p)->next;
cdcefbc9
LT
672
673 show = show_early_output;
674 if (!show)
675 continue;
676
677 show(revs, newlist);
678 show_early_output = NULL;
a4a88b2b 679 }
d7a17cad 680 if (revs->cherry_pick)
36d56de6 681 cherry_pick_list(newlist, revs);
d7a17cad 682
a4a88b2b 683 revs->commits = newlist;
cc0e6c5a 684 return 0;
a4a88b2b
LT
685}
686
63049292
JH
687struct all_refs_cb {
688 int all_flags;
71b03b42 689 int warned_bad_reflog;
63049292
JH
690 struct rev_info *all_revs;
691 const char *name_for_errormsg;
692};
ae563542 693
8da19775 694static int handle_one_ref(const char *path, const unsigned char *sha1, int flag, void *cb_data)
ae563542 695{
63049292
JH
696 struct all_refs_cb *cb = cb_data;
697 struct object *object = get_reference(cb->all_revs, path, sha1,
698 cb->all_flags);
3d1efd8f 699 add_pending_object(cb->all_revs, object, path);
ae563542
LT
700 return 0;
701}
702
d08bae7e
IL
703static void init_all_refs_cb(struct all_refs_cb *cb, struct rev_info *revs,
704 unsigned flags)
705{
706 cb->all_revs = revs;
707 cb->all_flags = flags;
708}
709
a5aa930d
UKK
710static void handle_refs(struct rev_info *revs, unsigned flags,
711 int (*for_each)(each_ref_fn, void *))
ae563542 712{
63049292 713 struct all_refs_cb cb;
d08bae7e 714 init_all_refs_cb(&cb, revs, flags);
a5aa930d 715 for_each(handle_one_ref, &cb);
63049292
JH
716}
717
71b03b42 718static void handle_one_reflog_commit(unsigned char *sha1, void *cb_data)
63049292
JH
719{
720 struct all_refs_cb *cb = cb_data;
71b03b42
SP
721 if (!is_null_sha1(sha1)) {
722 struct object *o = parse_object(sha1);
723 if (o) {
724 o->flags |= cb->all_flags;
725 add_pending_object(cb->all_revs, o, "");
726 }
727 else if (!cb->warned_bad_reflog) {
46efd2d9 728 warning("reflog of '%s' references pruned commits",
71b03b42
SP
729 cb->name_for_errormsg);
730 cb->warned_bad_reflog = 1;
731 }
63049292 732 }
71b03b42
SP
733}
734
883d60fa
JS
735static int handle_one_reflog_ent(unsigned char *osha1, unsigned char *nsha1,
736 const char *email, unsigned long timestamp, int tz,
737 const char *message, void *cb_data)
71b03b42
SP
738{
739 handle_one_reflog_commit(osha1, cb_data);
740 handle_one_reflog_commit(nsha1, cb_data);
63049292
JH
741 return 0;
742}
743
744static int handle_one_reflog(const char *path, const unsigned char *sha1, int flag, void *cb_data)
745{
746 struct all_refs_cb *cb = cb_data;
71b03b42 747 cb->warned_bad_reflog = 0;
63049292
JH
748 cb->name_for_errormsg = path;
749 for_each_reflog_ent(path, handle_one_reflog_ent, cb_data);
750 return 0;
751}
752
753static void handle_reflog(struct rev_info *revs, unsigned flags)
754{
755 struct all_refs_cb cb;
756 cb.all_revs = revs;
757 cb.all_flags = flags;
25b51e2c 758 for_each_reflog(handle_one_reflog, &cb);
ae563542
LT
759}
760
ea4a19e1
JH
761static int add_parents_only(struct rev_info *revs, const char *arg, int flags)
762{
763 unsigned char sha1[20];
764 struct object *it;
765 struct commit *commit;
766 struct commit_list *parents;
767
768 if (*arg == '^') {
769 flags ^= UNINTERESTING;
770 arg++;
771 }
772 if (get_sha1(arg, sha1))
773 return 0;
774 while (1) {
775 it = get_reference(revs, arg, sha1, 0);
1974632c 776 if (it->type != OBJ_TAG)
ea4a19e1 777 break;
9684afd9
MK
778 if (!((struct tag*)it)->tagged)
779 return 0;
e702496e 780 hashcpy(sha1, ((struct tag*)it)->tagged->sha1);
ea4a19e1 781 }
1974632c 782 if (it->type != OBJ_COMMIT)
ea4a19e1
JH
783 return 0;
784 commit = (struct commit *)it;
785 for (parents = commit->parents; parents; parents = parents->next) {
786 it = &parents->item->object;
787 it->flags |= flags;
788 add_pending_object(revs, it, arg);
789 }
790 return 1;
791}
792
db6296a5 793void init_revisions(struct rev_info *revs, const char *prefix)
8efdc326
FK
794{
795 memset(revs, 0, sizeof(*revs));
8e8f9987 796
6b9c58f4 797 revs->abbrev = DEFAULT_ABBREV;
8e8f9987 798 revs->ignore_merges = 1;
9202434c 799 revs->simplify_history = 1;
8f67f8ae 800 DIFF_OPT_SET(&revs->pruning, RECURSIVE);
90b19941 801 DIFF_OPT_SET(&revs->pruning, QUICK);
cd2bdc53
LT
802 revs->pruning.add_remove = file_add_remove;
803 revs->pruning.change = file_change;
8efdc326
FK
804 revs->lifo = 1;
805 revs->dense = 1;
db6296a5 806 revs->prefix = prefix;
8efdc326
FK
807 revs->max_age = -1;
808 revs->min_age = -1;
d5db6c9e 809 revs->skip_count = -1;
8efdc326
FK
810 revs->max_count = -1;
811
cd2bdc53
LT
812 revs->commit_format = CMIT_FMT_DEFAULT;
813
0843acfd
JK
814 revs->grep_filter.status_only = 1;
815 revs->grep_filter.pattern_tail = &(revs->grep_filter.pattern_list);
816 revs->grep_filter.regflags = REG_NEWLINE;
817
cd2bdc53 818 diff_setup(&revs->diffopt);
c0cb4a06 819 if (prefix && !revs->diffopt.prefix) {
cd676a51
JH
820 revs->diffopt.prefix = prefix;
821 revs->diffopt.prefix_length = strlen(prefix);
822 }
8efdc326
FK
823}
824
0d2c9d67
RS
825static void add_pending_commit_list(struct rev_info *revs,
826 struct commit_list *commit_list,
827 unsigned int flags)
828{
829 while (commit_list) {
830 struct object *object = &commit_list->item->object;
831 object->flags |= flags;
832 add_pending_object(revs, object, sha1_to_hex(object->sha1));
833 commit_list = commit_list->next;
834 }
835}
836
ae3e5e1e
JH
837static void prepare_show_merge(struct rev_info *revs)
838{
839 struct commit_list *bases;
840 struct commit *head, *other;
841 unsigned char sha1[20];
842 const char **prune = NULL;
843 int i, prune_num = 1; /* counting terminating NULL */
844
845 if (get_sha1("HEAD", sha1) || !(head = lookup_commit(sha1)))
846 die("--merge without HEAD?");
847 if (get_sha1("MERGE_HEAD", sha1) || !(other = lookup_commit(sha1)))
848 die("--merge without MERGE_HEAD?");
849 add_pending_object(revs, &head->object, "HEAD");
850 add_pending_object(revs, &other->object, "MERGE_HEAD");
851 bases = get_merge_bases(head, other, 1);
e82447b1
JH
852 add_pending_commit_list(revs, bases, UNINTERESTING);
853 free_commit_list(bases);
854 head->object.flags |= SYMMETRIC_LEFT;
ae3e5e1e
JH
855
856 if (!active_nr)
857 read_cache();
858 for (i = 0; i < active_nr; i++) {
859 struct cache_entry *ce = active_cache[i];
860 if (!ce_stage(ce))
861 continue;
862 if (ce_path_match(ce, revs->prune_data)) {
863 prune_num++;
864 prune = xrealloc(prune, sizeof(*prune) * prune_num);
865 prune[prune_num-2] = ce->name;
866 prune[prune_num-1] = NULL;
867 }
868 while ((i+1 < active_nr) &&
869 ce_same_name(ce, active_cache[i+1]))
870 i++;
871 }
872 revs->prune_data = prune;
e82447b1 873 revs->limited = 1;
ae3e5e1e
JH
874}
875
5d6f0935
JH
876int handle_revision_arg(const char *arg, struct rev_info *revs,
877 int flags,
878 int cant_be_filename)
879{
bb6c2fba 880 unsigned mode;
5d6f0935
JH
881 char *dotdot;
882 struct object *object;
883 unsigned char sha1[20];
884 int local_flags;
885
886 dotdot = strstr(arg, "..");
887 if (dotdot) {
888 unsigned char from_sha1[20];
889 const char *next = dotdot + 2;
890 const char *this = arg;
891 int symmetric = *next == '.';
892 unsigned int flags_exclude = flags ^ UNINTERESTING;
893
894 *dotdot = 0;
895 next += symmetric;
896
897 if (!*next)
898 next = "HEAD";
899 if (dotdot == arg)
900 this = "HEAD";
901 if (!get_sha1(this, from_sha1) &&
902 !get_sha1(next, sha1)) {
903 struct commit *a, *b;
904 struct commit_list *exclude;
905
906 a = lookup_commit_reference(from_sha1);
907 b = lookup_commit_reference(sha1);
908 if (!a || !b) {
909 die(symmetric ?
910 "Invalid symmetric difference expression %s...%s" :
911 "Invalid revision range %s..%s",
912 arg, next);
913 }
914
915 if (!cant_be_filename) {
916 *dotdot = '.';
917 verify_non_filename(revs->prefix, arg);
918 }
919
920 if (symmetric) {
921 exclude = get_merge_bases(a, b, 1);
922 add_pending_commit_list(revs, exclude,
923 flags_exclude);
924 free_commit_list(exclude);
577ed5c2 925 a->object.flags |= flags | SYMMETRIC_LEFT;
5d6f0935
JH
926 } else
927 a->object.flags |= flags_exclude;
928 b->object.flags |= flags;
929 add_pending_object(revs, &a->object, this);
930 add_pending_object(revs, &b->object, next);
931 return 0;
932 }
933 *dotdot = '.';
934 }
935 dotdot = strstr(arg, "^@");
936 if (dotdot && !dotdot[2]) {
937 *dotdot = 0;
938 if (add_parents_only(revs, arg, flags))
939 return 0;
940 *dotdot = '^';
941 }
62476c8e
JH
942 dotdot = strstr(arg, "^!");
943 if (dotdot && !dotdot[2]) {
944 *dotdot = 0;
945 if (!add_parents_only(revs, arg, flags ^ UNINTERESTING))
946 *dotdot = '^';
947 }
948
5d6f0935
JH
949 local_flags = 0;
950 if (*arg == '^') {
951 local_flags = UNINTERESTING;
952 arg++;
953 }
bb6c2fba 954 if (get_sha1_with_mode(arg, sha1, &mode))
5d6f0935
JH
955 return -1;
956 if (!cant_be_filename)
957 verify_non_filename(revs->prefix, arg);
958 object = get_reference(revs, arg, sha1, flags ^ local_flags);
bb6c2fba 959 add_pending_object_with_mode(revs, object, arg, mode);
5d6f0935
JH
960 return 0;
961}
962
60da8b15 963static void read_pathspec_from_stdin(struct rev_info *revs, struct strbuf *sb, const char ***prune_data)
1fc561d1 964{
60da8b15
JH
965 const char **prune = *prune_data;
966 int prune_nr;
967 int prune_alloc;
1fc561d1 968
60da8b15
JH
969 /* count existing ones */
970 if (!prune)
971 prune_nr = 0;
972 else
973 for (prune_nr = 0; prune[prune_nr]; prune_nr++)
974 ;
975 prune_alloc = prune_nr; /* not really, but we do not know */
976
977 while (strbuf_getwholeline(sb, stdin, '\n') != EOF) {
978 int len = sb->len;
979 if (len && sb->buf[len - 1] == '\n')
980 sb->buf[--len] = '\0';
981 ALLOC_GROW(prune, prune_nr+1, prune_alloc);
982 prune[prune_nr++] = xstrdup(sb->buf);
983 }
984 if (prune) {
985 ALLOC_GROW(prune, prune_nr+1, prune_alloc);
986 prune[prune_nr] = NULL;
987 }
988 *prune_data = prune;
989}
990
991static void read_revisions_from_stdin(struct rev_info *revs, const char ***prune)
1fc561d1 992{
63d564b3 993 struct strbuf sb;
60da8b15 994 int seen_dashdash = 0;
1fc561d1 995
63d564b3
JH
996 strbuf_init(&sb, 1000);
997 while (strbuf_getwholeline(&sb, stdin, '\n') != EOF) {
998 int len = sb.len;
999 if (len && sb.buf[len - 1] == '\n')
1000 sb.buf[--len] = '\0';
1fc561d1
AB
1001 if (!len)
1002 break;
60da8b15
JH
1003 if (sb.buf[0] == '-') {
1004 if (len == 2 && sb.buf[1] == '-') {
1005 seen_dashdash = 1;
1006 break;
1007 }
1fc561d1 1008 die("options not supported in --stdin mode");
60da8b15 1009 }
63d564b3
JH
1010 if (handle_revision_arg(sb.buf, revs, 0, 1))
1011 die("bad revision '%s'", sb.buf);
1fc561d1 1012 }
60da8b15
JH
1013 if (seen_dashdash)
1014 read_pathspec_from_stdin(revs, &sb, prune);
63d564b3 1015 strbuf_release(&sb);
1fc561d1
AB
1016}
1017
2d10c555 1018static void add_grep(struct rev_info *revs, const char *ptn, enum grep_pat_token what)
bd95fcd3 1019{
0843acfd 1020 append_grep_pattern(&revs->grep_filter, ptn, "command line", 0, what);
2d10c555
JH
1021}
1022
a4d7d2c6 1023static void add_header_grep(struct rev_info *revs, enum grep_header_field field, const char *pattern)
2d10c555 1024{
a4d7d2c6 1025 append_header_grep_pattern(&revs->grep_filter, field, pattern);
bd95fcd3
JH
1026}
1027
1028static void add_message_grep(struct rev_info *revs, const char *pattern)
1029{
2d10c555 1030 add_grep(revs, pattern, GREP_PATTERN_BODY);
bd95fcd3
JH
1031}
1032
6b61ec05
PH
1033static int handle_revision_opt(struct rev_info *revs, int argc, const char **argv,
1034 int *unkc, const char **unkv)
02e54220
PH
1035{
1036 const char *arg = argv[0];
1037
1038 /* pseudo revision arguments */
1039 if (!strcmp(arg, "--all") || !strcmp(arg, "--branches") ||
1040 !strcmp(arg, "--tags") || !strcmp(arg, "--remotes") ||
1041 !strcmp(arg, "--reflog") || !strcmp(arg, "--not") ||
ad3f9a71
LT
1042 !strcmp(arg, "--no-walk") || !strcmp(arg, "--do-walk") ||
1043 !strcmp(arg, "--bisect"))
02e54220
PH
1044 {
1045 unkv[(*unkc)++] = arg;
0fe8c138 1046 return 1;
02e54220
PH
1047 }
1048
1049 if (!prefixcmp(arg, "--max-count=")) {
1050 revs->max_count = atoi(arg + 12);
1051 } else if (!prefixcmp(arg, "--skip=")) {
1052 revs->skip_count = atoi(arg + 7);
1053 } else if ((*arg == '-') && isdigit(arg[1])) {
1054 /* accept -<digit>, like traditional "head" */
1055 revs->max_count = atoi(arg + 1);
1056 } else if (!strcmp(arg, "-n")) {
1057 if (argc <= 1)
1058 return error("-n requires an argument");
1059 revs->max_count = atoi(argv[1]);
1060 return 2;
1061 } else if (!prefixcmp(arg, "-n")) {
1062 revs->max_count = atoi(arg + 2);
1063 } else if (!prefixcmp(arg, "--max-age=")) {
1064 revs->max_age = atoi(arg + 10);
1065 } else if (!prefixcmp(arg, "--since=")) {
1066 revs->max_age = approxidate(arg + 8);
1067 } else if (!prefixcmp(arg, "--after=")) {
1068 revs->max_age = approxidate(arg + 8);
1069 } else if (!prefixcmp(arg, "--min-age=")) {
1070 revs->min_age = atoi(arg + 10);
1071 } else if (!prefixcmp(arg, "--before=")) {
1072 revs->min_age = approxidate(arg + 9);
1073 } else if (!prefixcmp(arg, "--until=")) {
1074 revs->min_age = approxidate(arg + 8);
1075 } else if (!strcmp(arg, "--first-parent")) {
1076 revs->first_parent_only = 1;
1077 } else if (!strcmp(arg, "-g") || !strcmp(arg, "--walk-reflogs")) {
1078 init_reflog_walk(&revs->reflog_info);
1079 } else if (!strcmp(arg, "--default")) {
1080 if (argc <= 1)
1081 return error("bad --default argument");
1082 revs->def = argv[1];
1083 return 2;
1084 } else if (!strcmp(arg, "--merge")) {
1085 revs->show_merge = 1;
1086 } else if (!strcmp(arg, "--topo-order")) {
1087 revs->lifo = 1;
1088 revs->topo_order = 1;
6546b593
JH
1089 } else if (!strcmp(arg, "--simplify-merges")) {
1090 revs->simplify_merges = 1;
1091 revs->rewrite_parents = 1;
1092 revs->simplify_history = 0;
1093 revs->limited = 1;
78892e32
LT
1094 } else if (!strcmp(arg, "--simplify-by-decoration")) {
1095 revs->simplify_merges = 1;
1096 revs->rewrite_parents = 1;
1097 revs->simplify_history = 0;
1098 revs->simplify_by_decoration = 1;
1099 revs->limited = 1;
1100 revs->prune = 1;
33e7018c 1101 load_ref_decorations(DECORATE_SHORT_REFS);
02e54220
PH
1102 } else if (!strcmp(arg, "--date-order")) {
1103 revs->lifo = 0;
1104 revs->topo_order = 1;
1105 } else if (!prefixcmp(arg, "--early-output")) {
1106 int count = 100;
1107 switch (arg[14]) {
1108 case '=':
1109 count = atoi(arg+15);
1110 /* Fallthrough */
1111 case 0:
1112 revs->topo_order = 1;
1113 revs->early_output = count;
1114 }
1115 } else if (!strcmp(arg, "--parents")) {
1116 revs->rewrite_parents = 1;
1117 revs->print_parents = 1;
1118 } else if (!strcmp(arg, "--dense")) {
1119 revs->dense = 1;
1120 } else if (!strcmp(arg, "--sparse")) {
1121 revs->dense = 0;
1122 } else if (!strcmp(arg, "--show-all")) {
1123 revs->show_all = 1;
1124 } else if (!strcmp(arg, "--remove-empty")) {
1125 revs->remove_empty_trees = 1;
b8e8db28
LT
1126 } else if (!strcmp(arg, "--merges")) {
1127 revs->merges_only = 1;
02e54220
PH
1128 } else if (!strcmp(arg, "--no-merges")) {
1129 revs->no_merges = 1;
1130 } else if (!strcmp(arg, "--boundary")) {
1131 revs->boundary = 1;
1132 } else if (!strcmp(arg, "--left-right")) {
1133 revs->left_right = 1;
1134 } else if (!strcmp(arg, "--cherry-pick")) {
1135 revs->cherry_pick = 1;
1136 revs->limited = 1;
1137 } else if (!strcmp(arg, "--objects")) {
1138 revs->tag_objects = 1;
1139 revs->tree_objects = 1;
1140 revs->blob_objects = 1;
1141 } else if (!strcmp(arg, "--objects-edge")) {
1142 revs->tag_objects = 1;
1143 revs->tree_objects = 1;
1144 revs->blob_objects = 1;
1145 revs->edge_hint = 1;
1146 } else if (!strcmp(arg, "--unpacked")) {
1147 revs->unpacked = 1;
03a9683d
JH
1148 } else if (!prefixcmp(arg, "--unpacked=")) {
1149 die("--unpacked=<packfile> no longer supported.");
02e54220
PH
1150 } else if (!strcmp(arg, "-r")) {
1151 revs->diff = 1;
1152 DIFF_OPT_SET(&revs->diffopt, RECURSIVE);
1153 } else if (!strcmp(arg, "-t")) {
1154 revs->diff = 1;
1155 DIFF_OPT_SET(&revs->diffopt, RECURSIVE);
1156 DIFF_OPT_SET(&revs->diffopt, TREE_IN_RECURSIVE);
1157 } else if (!strcmp(arg, "-m")) {
1158 revs->ignore_merges = 0;
1159 } else if (!strcmp(arg, "-c")) {
1160 revs->diff = 1;
1161 revs->dense_combined_merges = 0;
1162 revs->combine_merges = 1;
1163 } else if (!strcmp(arg, "--cc")) {
1164 revs->diff = 1;
1165 revs->dense_combined_merges = 1;
1166 revs->combine_merges = 1;
1167 } else if (!strcmp(arg, "-v")) {
1168 revs->verbose_header = 1;
1169 } else if (!strcmp(arg, "--pretty")) {
1170 revs->verbose_header = 1;
66b2ed09 1171 revs->pretty_given = 1;
02e54220 1172 get_commit_format(arg+8, revs);
3a4c1a5e 1173 } else if (!prefixcmp(arg, "--pretty=") || !prefixcmp(arg, "--format=")) {
02e54220 1174 revs->verbose_header = 1;
66b2ed09 1175 revs->pretty_given = 1;
02e54220 1176 get_commit_format(arg+9, revs);
66b2ed09
JH
1177 } else if (!strcmp(arg, "--show-notes")) {
1178 revs->show_notes = 1;
1179 revs->show_notes_given = 1;
1180 } else if (!strcmp(arg, "--no-notes")) {
1181 revs->show_notes = 0;
1182 revs->show_notes_given = 1;
de84accc
NS
1183 } else if (!strcmp(arg, "--oneline")) {
1184 revs->verbose_header = 1;
1185 get_commit_format("oneline", revs);
7dccadf3 1186 revs->pretty_given = 1;
de84accc 1187 revs->abbrev_commit = 1;
02e54220
PH
1188 } else if (!strcmp(arg, "--graph")) {
1189 revs->topo_order = 1;
1190 revs->rewrite_parents = 1;
1191 revs->graph = graph_init(revs);
1192 } else if (!strcmp(arg, "--root")) {
1193 revs->show_root_diff = 1;
1194 } else if (!strcmp(arg, "--no-commit-id")) {
1195 revs->no_commit_id = 1;
1196 } else if (!strcmp(arg, "--always")) {
1197 revs->always_show_header = 1;
1198 } else if (!strcmp(arg, "--no-abbrev")) {
1199 revs->abbrev = 0;
1200 } else if (!strcmp(arg, "--abbrev")) {
1201 revs->abbrev = DEFAULT_ABBREV;
1202 } else if (!prefixcmp(arg, "--abbrev=")) {
1203 revs->abbrev = strtoul(arg + 9, NULL, 10);
1204 if (revs->abbrev < MINIMUM_ABBREV)
1205 revs->abbrev = MINIMUM_ABBREV;
1206 else if (revs->abbrev > 40)
1207 revs->abbrev = 40;
1208 } else if (!strcmp(arg, "--abbrev-commit")) {
1209 revs->abbrev_commit = 1;
1210 } else if (!strcmp(arg, "--full-diff")) {
1211 revs->diff = 1;
1212 revs->full_diff = 1;
1213 } else if (!strcmp(arg, "--full-history")) {
1214 revs->simplify_history = 0;
1215 } else if (!strcmp(arg, "--relative-date")) {
1216 revs->date_mode = DATE_RELATIVE;
f4ea32f0 1217 revs->date_mode_explicit = 1;
02e54220
PH
1218 } else if (!strncmp(arg, "--date=", 7)) {
1219 revs->date_mode = parse_date_format(arg + 7);
f4ea32f0 1220 revs->date_mode_explicit = 1;
02e54220
PH
1221 } else if (!strcmp(arg, "--log-size")) {
1222 revs->show_log_size = 1;
1223 }
1224 /*
1225 * Grepping the commit log
1226 */
1227 else if (!prefixcmp(arg, "--author=")) {
a4d7d2c6 1228 add_header_grep(revs, GREP_HEADER_AUTHOR, arg+9);
02e54220 1229 } else if (!prefixcmp(arg, "--committer=")) {
a4d7d2c6 1230 add_header_grep(revs, GREP_HEADER_COMMITTER, arg+12);
02e54220
PH
1231 } else if (!prefixcmp(arg, "--grep=")) {
1232 add_message_grep(revs, arg+7);
1233 } else if (!strcmp(arg, "--extended-regexp") || !strcmp(arg, "-E")) {
0843acfd 1234 revs->grep_filter.regflags |= REG_EXTENDED;
02e54220 1235 } else if (!strcmp(arg, "--regexp-ignore-case") || !strcmp(arg, "-i")) {
0843acfd 1236 revs->grep_filter.regflags |= REG_ICASE;
02e54220 1237 } else if (!strcmp(arg, "--fixed-strings") || !strcmp(arg, "-F")) {
0843acfd 1238 revs->grep_filter.fixed = 1;
02e54220 1239 } else if (!strcmp(arg, "--all-match")) {
0843acfd 1240 revs->grep_filter.all_match = 1;
02e54220
PH
1241 } else if (!prefixcmp(arg, "--encoding=")) {
1242 arg += 11;
1243 if (strcmp(arg, "none"))
1244 git_log_output_encoding = xstrdup(arg);
1245 else
1246 git_log_output_encoding = "";
1247 } else if (!strcmp(arg, "--reverse")) {
1248 revs->reverse ^= 1;
1249 } else if (!strcmp(arg, "--children")) {
1250 revs->children.name = "children";
1251 revs->limited = 1;
1252 } else {
1253 int opts = diff_opt_parse(&revs->diffopt, argv, argc);
1254 if (!opts)
1255 unkv[(*unkc)++] = arg;
1256 return opts;
1257 }
1258
1259 return 1;
1260}
1261
6b61ec05
PH
1262void parse_revision_opt(struct rev_info *revs, struct parse_opt_ctx_t *ctx,
1263 const struct option *options,
1264 const char * const usagestr[])
1265{
1266 int n = handle_revision_opt(revs, ctx->argc, ctx->argv,
1267 &ctx->cpidx, ctx->out);
1268 if (n <= 0) {
1269 error("unknown option `%s'", ctx->argv[0]);
1270 usage_with_options(usagestr, options);
1271 }
1272 ctx->argv += n;
1273 ctx->argc -= n;
1274}
1275
ad3f9a71
LT
1276static int for_each_bad_bisect_ref(each_ref_fn fn, void *cb_data)
1277{
1278 return for_each_ref_in("refs/bisect/bad", fn, cb_data);
1279}
1280
1281static int for_each_good_bisect_ref(each_ref_fn fn, void *cb_data)
1282{
1283 return for_each_ref_in("refs/bisect/good", fn, cb_data);
1284}
1285
60da8b15
JH
1286static void append_prune_data(const char ***prune_data, const char **av)
1287{
1288 const char **prune = *prune_data;
1289 int prune_nr;
1290 int prune_alloc;
1291
1292 if (!prune) {
1293 *prune_data = av;
1294 return;
1295 }
1296
1297 /* count existing ones */
1298 for (prune_nr = 0; prune[prune_nr]; prune_nr++)
1299 ;
1300 prune_alloc = prune_nr; /* not really, but we do not know */
1301
1302 while (*av) {
1303 ALLOC_GROW(prune, prune_nr+1, prune_alloc);
1304 prune[prune_nr++] = *av;
1305 av++;
1306 }
1307 if (prune) {
1308 ALLOC_GROW(prune, prune_nr+1, prune_alloc);
1309 prune[prune_nr] = NULL;
1310 }
1311 *prune_data = prune;
1312}
1313
ae563542
LT
1314/*
1315 * Parse revision information, filling in the "rev_info" structure,
1316 * and removing the used arguments from the argument list.
1317 *
765ac8ec
LT
1318 * Returns the number of arguments left that weren't recognized
1319 * (which are also moved to the head of the argument list)
ae563542 1320 */
a4a88b2b 1321int setup_revisions(int argc, const char **argv, struct rev_info *revs, const char *def)
ae563542 1322{
8b3dce56 1323 int i, flags, left, seen_dashdash, read_from_stdin;
5486ef0e 1324 const char **prune_data = NULL;
ae563542 1325
ae563542
LT
1326 /* First, search for "--" */
1327 seen_dashdash = 0;
1328 for (i = 1; i < argc; i++) {
1329 const char *arg = argv[i];
1330 if (strcmp(arg, "--"))
1331 continue;
1332 argv[i] = NULL;
1333 argc = i;
a65f2005 1334 if (argv[i + 1])
5486ef0e 1335 prune_data = argv + i + 1;
ae563542
LT
1336 seen_dashdash = 1;
1337 break;
1338 }
1339
02e54220
PH
1340 /* Second, deal with arguments and options */
1341 flags = 0;
8b3dce56 1342 read_from_stdin = 0;
02e54220 1343 for (left = i = 1; i < argc; i++) {
ae563542 1344 const char *arg = argv[i];
ae563542 1345 if (*arg == '-') {
cd2bdc53 1346 int opts;
02e54220 1347
ae563542 1348 if (!strcmp(arg, "--all")) {
a5aa930d 1349 handle_refs(revs, flags, for_each_ref);
f0298cf1 1350 handle_refs(revs, flags, head_ref);
a5aa930d
UKK
1351 continue;
1352 }
1353 if (!strcmp(arg, "--branches")) {
1354 handle_refs(revs, flags, for_each_branch_ref);
1355 continue;
1356 }
ad3f9a71
LT
1357 if (!strcmp(arg, "--bisect")) {
1358 handle_refs(revs, flags, for_each_bad_bisect_ref);
1359 handle_refs(revs, flags ^ UNINTERESTING, for_each_good_bisect_ref);
1360 revs->bisect = 1;
1361 continue;
1362 }
a5aa930d
UKK
1363 if (!strcmp(arg, "--tags")) {
1364 handle_refs(revs, flags, for_each_tag_ref);
1365 continue;
1366 }
1367 if (!strcmp(arg, "--remotes")) {
1368 handle_refs(revs, flags, for_each_remote_ref);
ae563542
LT
1369 continue;
1370 }
d08bae7e
IL
1371 if (!prefixcmp(arg, "--glob=")) {
1372 struct all_refs_cb cb;
1373 init_all_refs_cb(&cb, revs, flags);
1374 for_each_glob_ref(handle_one_ref, arg + 7, &cb);
1375 continue;
1376 }
b09fe971
IL
1377 if (!prefixcmp(arg, "--branches=")) {
1378 struct all_refs_cb cb;
1379 init_all_refs_cb(&cb, revs, flags);
1380 for_each_glob_ref_in(handle_one_ref, arg + 11, "refs/heads/", &cb);
1381 continue;
1382 }
1383 if (!prefixcmp(arg, "--tags=")) {
1384 struct all_refs_cb cb;
1385 init_all_refs_cb(&cb, revs, flags);
1386 for_each_glob_ref_in(handle_one_ref, arg + 7, "refs/tags/", &cb);
1387 continue;
1388 }
1389 if (!prefixcmp(arg, "--remotes=")) {
1390 struct all_refs_cb cb;
1391 init_all_refs_cb(&cb, revs, flags);
1392 for_each_glob_ref_in(handle_one_ref, arg + 10, "refs/remotes/", &cb);
1393 continue;
1394 }
63049292
JH
1395 if (!strcmp(arg, "--reflog")) {
1396 handle_reflog(revs, flags);
1397 continue;
1398 }
ae563542
LT
1399 if (!strcmp(arg, "--not")) {
1400 flags ^= UNINTERESTING;
1401 continue;
1402 }
8e64006e
JS
1403 if (!strcmp(arg, "--no-walk")) {
1404 revs->no_walk = 1;
1405 continue;
1406 }
1407 if (!strcmp(arg, "--do-walk")) {
1408 revs->no_walk = 0;
1409 continue;
1410 }
8b3dce56
JH
1411 if (!strcmp(arg, "--stdin")) {
1412 if (revs->disable_stdin) {
1413 argv[left++] = arg;
1414 continue;
1415 }
1416 if (read_from_stdin++)
1417 die("--stdin given twice?");
60da8b15 1418 read_revisions_from_stdin(revs, &prune_data);
8b3dce56
JH
1419 continue;
1420 }
2d10c555 1421
02e54220 1422 opts = handle_revision_opt(revs, argc - i, argv + i, &left, argv);
cd2bdc53 1423 if (opts > 0) {
cd2bdc53
LT
1424 i += opts - 1;
1425 continue;
1426 }
02e54220
PH
1427 if (opts < 0)
1428 exit(128);
ae563542
LT
1429 continue;
1430 }
ae563542 1431
5d6f0935
JH
1432 if (handle_revision_arg(arg, revs, flags, seen_dashdash)) {
1433 int j;
1434 if (seen_dashdash || *arg == '^')
ae563542
LT
1435 die("bad revision '%s'", arg);
1436
ea92f41f
JH
1437 /* If we didn't have a "--":
1438 * (1) all filenames must exist;
1439 * (2) all rev-args must not be interpretable
1440 * as a valid filename.
1441 * but the latter we have checked in the main loop.
1442 */
e23d0b4a
LT
1443 for (j = i; j < argc; j++)
1444 verify_filename(revs->prefix, argv[j]);
1445
60da8b15 1446 append_prune_data(&prune_data, argv + i);
ae563542
LT
1447 break;
1448 }
ae563542 1449 }
5d6f0935 1450
5486ef0e
JH
1451 if (prune_data)
1452 revs->prune_data = get_pathspec(revs->prefix, prune_data);
1453
02e54220
PH
1454 if (revs->def == NULL)
1455 revs->def = def;
1456 if (revs->show_merge)
ae3e5e1e 1457 prepare_show_merge(revs);
02e54220 1458 if (revs->def && !revs->pending.nr) {
ae563542 1459 unsigned char sha1[20];
cd2bdc53 1460 struct object *object;
bb6c2fba 1461 unsigned mode;
02e54220
PH
1462 if (get_sha1_with_mode(revs->def, sha1, &mode))
1463 die("bad default revision '%s'", revs->def);
1464 object = get_reference(revs, revs->def, sha1, 0);
1465 add_pending_object_with_mode(revs, object, revs->def, mode);
ae563542 1466 }
8efdc326 1467
b7bb760d
LT
1468 /* Did the user ask for any diff output? Run the diff! */
1469 if (revs->diffopt.output_format & ~DIFF_FORMAT_NO_OUTPUT)
1470 revs->diff = 1;
1471
0faf2da7
AL
1472 /* Pickaxe, diff-filter and rename following need diffs */
1473 if (revs->diffopt.pickaxe ||
1474 revs->diffopt.filter ||
1475 DIFF_OPT_TST(&revs->diffopt, FOLLOW_RENAMES))
b7bb760d
LT
1476 revs->diff = 1;
1477
9dad9d2e 1478 if (revs->topo_order)
53069686
JH
1479 revs->limited = 1;
1480
8efdc326 1481 if (revs->prune_data) {
cd2bdc53 1482 diff_tree_setup_paths(revs->prune_data, &revs->pruning);
750f7b66 1483 /* Can't prune commits with rename following: the paths change.. */
8f67f8ae 1484 if (!DIFF_OPT_TST(&revs->diffopt, FOLLOW_RENAMES))
53b2c823 1485 revs->prune = 1;
cd2bdc53
LT
1486 if (!revs->full_diff)
1487 diff_tree_setup_paths(revs->prune_data, &revs->diffopt);
8efdc326 1488 }
cd2bdc53
LT
1489 if (revs->combine_merges) {
1490 revs->ignore_merges = 0;
0e677e1a 1491 if (revs->dense_combined_merges && !revs->diffopt.output_format)
cd2bdc53
LT
1492 revs->diffopt.output_format = DIFF_FORMAT_PATCH;
1493 }
cd2bdc53 1494 revs->diffopt.abbrev = revs->abbrev;
72ee96c0
JH
1495 if (diff_setup_done(&revs->diffopt) < 0)
1496 die("diff_setup_done failed");
8efdc326 1497
0843acfd 1498 compile_grep_patterns(&revs->grep_filter);
bd95fcd3 1499
d56651c0
SP
1500 if (revs->reverse && revs->reflog_info)
1501 die("cannot combine --reverse with --walk-reflogs");
8bb65883 1502 if (revs->rewrite_parents && revs->children.name)
f35f5603 1503 die("cannot combine --parents and --children");
d56651c0 1504
7fefda5c
AS
1505 /*
1506 * Limitations on the graph functionality
1507 */
1508 if (revs->reverse && revs->graph)
1509 die("cannot combine --reverse with --graph");
1510
1511 if (revs->reflog_info && revs->graph)
1512 die("cannot combine --walk-reflogs with --graph");
1513
ae563542
LT
1514 return left;
1515}
a4a88b2b 1516
f35f5603
JH
1517static void add_child(struct rev_info *revs, struct commit *parent, struct commit *child)
1518{
1519 struct commit_list *l = xcalloc(1, sizeof(*l));
1520
1521 l->item = child;
1522 l->next = add_decoration(&revs->children, &parent->object, l);
1523}
1524
6546b593
JH
1525static int remove_duplicate_parents(struct commit *commit)
1526{
1527 struct commit_list **pp, *p;
1528 int surviving_parents;
1529
1530 /* Examine existing parents while marking ones we have seen... */
1531 pp = &commit->parents;
1532 while ((p = *pp) != NULL) {
1533 struct commit *parent = p->item;
1534 if (parent->object.flags & TMP_MARK) {
1535 *pp = p->next;
1536 continue;
1537 }
1538 parent->object.flags |= TMP_MARK;
1539 pp = &p->next;
1540 }
1541 /* count them while clearing the temporary mark */
1542 surviving_parents = 0;
1543 for (p = commit->parents; p; p = p->next) {
1544 p->item->object.flags &= ~TMP_MARK;
1545 surviving_parents++;
1546 }
1547 return surviving_parents;
1548}
1549
faf0156b
JH
1550struct merge_simplify_state {
1551 struct commit *simplified;
1552};
1553
1554static struct merge_simplify_state *locate_simplify_state(struct rev_info *revs, struct commit *commit)
1555{
1556 struct merge_simplify_state *st;
1557
1558 st = lookup_decoration(&revs->merge_simplification, &commit->object);
1559 if (!st) {
1560 st = xcalloc(1, sizeof(*st));
1561 add_decoration(&revs->merge_simplification, &commit->object, st);
1562 }
1563 return st;
1564}
1565
1566static struct commit_list **simplify_one(struct rev_info *revs, struct commit *commit, struct commit_list **tail)
6546b593
JH
1567{
1568 struct commit_list *p;
faf0156b 1569 struct merge_simplify_state *st, *pst;
6546b593
JH
1570 int cnt;
1571
faf0156b
JH
1572 st = locate_simplify_state(revs, commit);
1573
6546b593 1574 /*
6546b593
JH
1575 * Have we handled this one?
1576 */
faf0156b 1577 if (st->simplified)
6546b593
JH
1578 return tail;
1579
1580 /*
1581 * An UNINTERESTING commit simplifies to itself, so does a
1582 * root commit. We do not rewrite parents of such commit
1583 * anyway.
1584 */
1585 if ((commit->object.flags & UNINTERESTING) || !commit->parents) {
faf0156b 1586 st->simplified = commit;
6546b593
JH
1587 return tail;
1588 }
1589
1590 /*
1591 * Do we know what commit all of our parents should be rewritten to?
1592 * Otherwise we are not ready to rewrite this one yet.
1593 */
1594 for (cnt = 0, p = commit->parents; p; p = p->next) {
faf0156b
JH
1595 pst = locate_simplify_state(revs, p->item);
1596 if (!pst->simplified) {
6546b593
JH
1597 tail = &commit_list_insert(p->item, tail)->next;
1598 cnt++;
1599 }
1600 }
53030f8d
JH
1601 if (cnt) {
1602 tail = &commit_list_insert(commit, tail)->next;
6546b593 1603 return tail;
53030f8d 1604 }
6546b593
JH
1605
1606 /*
1607 * Rewrite our list of parents.
1608 */
faf0156b
JH
1609 for (p = commit->parents; p; p = p->next) {
1610 pst = locate_simplify_state(revs, p->item);
1611 p->item = pst->simplified;
1612 }
6546b593
JH
1613 cnt = remove_duplicate_parents(commit);
1614
1615 /*
1616 * It is possible that we are a merge and one side branch
1617 * does not have any commit that touches the given paths;
1618 * in such a case, the immediate parents will be rewritten
1619 * to different commits.
1620 *
1621 * o----X X: the commit we are looking at;
1622 * / / o: a commit that touches the paths;
1623 * ---o----'
1624 *
1625 * Further reduce the parents by removing redundant parents.
1626 */
1627 if (1 < cnt) {
1628 struct commit_list *h = reduce_heads(commit->parents);
1629 cnt = commit_list_count(h);
1630 free_commit_list(commit->parents);
1631 commit->parents = h;
1632 }
1633
1634 /*
1635 * A commit simplifies to itself if it is a root, if it is
1636 * UNINTERESTING, if it touches the given paths, or if it is a
1637 * merge and its parents simplifies to more than one commits
1638 * (the first two cases are already handled at the beginning of
1639 * this function).
1640 *
1641 * Otherwise, it simplifies to what its sole parent simplifies to.
1642 */
1643 if (!cnt ||
1644 (commit->object.flags & UNINTERESTING) ||
1645 !(commit->object.flags & TREESAME) ||
1646 (1 < cnt))
faf0156b
JH
1647 st->simplified = commit;
1648 else {
1649 pst = locate_simplify_state(revs, commit->parents->item);
1650 st->simplified = pst->simplified;
1651 }
6546b593
JH
1652 return tail;
1653}
1654
1655static void simplify_merges(struct rev_info *revs)
1656{
1657 struct commit_list *list;
1658 struct commit_list *yet_to_do, **tail;
1659
5eac739e
JH
1660 if (!revs->topo_order)
1661 sort_in_topological_order(&revs->commits, revs->lifo);
1662 if (!revs->prune)
1663 return;
65347030 1664
6546b593
JH
1665 /* feed the list reversed */
1666 yet_to_do = NULL;
1667 for (list = revs->commits; list; list = list->next)
1668 commit_list_insert(list->item, &yet_to_do);
1669 while (yet_to_do) {
1670 list = yet_to_do;
1671 yet_to_do = NULL;
1672 tail = &yet_to_do;
1673 while (list) {
1674 struct commit *commit = list->item;
1675 struct commit_list *next = list->next;
1676 free(list);
1677 list = next;
faf0156b 1678 tail = simplify_one(revs, commit, tail);
6546b593
JH
1679 }
1680 }
1681
1682 /* clean up the result, removing the simplified ones */
1683 list = revs->commits;
1684 revs->commits = NULL;
1685 tail = &revs->commits;
1686 while (list) {
1687 struct commit *commit = list->item;
1688 struct commit_list *next = list->next;
faf0156b 1689 struct merge_simplify_state *st;
6546b593
JH
1690 free(list);
1691 list = next;
faf0156b
JH
1692 st = locate_simplify_state(revs, commit);
1693 if (st->simplified == commit)
6546b593
JH
1694 tail = &commit_list_insert(commit, tail)->next;
1695 }
6546b593
JH
1696}
1697
f35f5603
JH
1698static void set_children(struct rev_info *revs)
1699{
1700 struct commit_list *l;
1701 for (l = revs->commits; l; l = l->next) {
1702 struct commit *commit = l->item;
1703 struct commit_list *p;
1704
1705 for (p = commit->parents; p; p = p->next)
1706 add_child(revs, p->item, commit);
1707 }
1708}
1709
cc0e6c5a 1710int prepare_revision_walk(struct rev_info *revs)
a4a88b2b 1711{
1f1e895f 1712 int nr = revs->pending.nr;
94d23673 1713 struct object_array_entry *e, *list;
cd2bdc53 1714
94d23673 1715 e = list = revs->pending.objects;
1f1e895f
LT
1716 revs->pending.nr = 0;
1717 revs->pending.alloc = 0;
1718 revs->pending.objects = NULL;
1719 while (--nr >= 0) {
94d23673 1720 struct commit *commit = handle_commit(revs, e->item, e->name);
cd2bdc53
LT
1721 if (commit) {
1722 if (!(commit->object.flags & SEEN)) {
1723 commit->object.flags |= SEEN;
1724 insert_by_date(commit, &revs->commits);
1725 }
1726 }
94d23673 1727 e++;
cd2bdc53 1728 }
94d23673 1729 free(list);
cd2bdc53 1730
ba1d4505 1731 if (revs->no_walk)
cc0e6c5a 1732 return 0;
a4a88b2b 1733 if (revs->limited)
cc0e6c5a
AR
1734 if (limit_list(revs) < 0)
1735 return -1;
a4a88b2b 1736 if (revs->topo_order)
23c17d4a 1737 sort_in_topological_order(&revs->commits, revs->lifo);
6546b593
JH
1738 if (revs->simplify_merges)
1739 simplify_merges(revs);
f35f5603
JH
1740 if (revs->children.name)
1741 set_children(revs);
cc0e6c5a 1742 return 0;
a4a88b2b
LT
1743}
1744
cc0e6c5a
AR
1745enum rewrite_result {
1746 rewrite_one_ok,
1747 rewrite_one_noparents,
1748 rewrite_one_error,
1749};
1750
1751static enum rewrite_result rewrite_one(struct rev_info *revs, struct commit **pp)
765ac8ec 1752{
fce87ae5
AG
1753 struct commit_list *cache = NULL;
1754
765ac8ec
LT
1755 for (;;) {
1756 struct commit *p = *pp;
3381c790 1757 if (!revs->limited)
fce87ae5 1758 if (add_parents_to_list(revs, p, &revs->commits, &cache) < 0)
cc0e6c5a 1759 return rewrite_one_error;
6631c736 1760 if (p->parents && p->parents->next)
cc0e6c5a 1761 return rewrite_one_ok;
7dc0fe3b
LT
1762 if (p->object.flags & UNINTERESTING)
1763 return rewrite_one_ok;
1764 if (!(p->object.flags & TREESAME))
cc0e6c5a 1765 return rewrite_one_ok;
765ac8ec 1766 if (!p->parents)
cc0e6c5a 1767 return rewrite_one_noparents;
765ac8ec
LT
1768 *pp = p->parents->item;
1769 }
1770}
1771
cc0e6c5a 1772static int rewrite_parents(struct rev_info *revs, struct commit *commit)
765ac8ec
LT
1773{
1774 struct commit_list **pp = &commit->parents;
1775 while (*pp) {
1776 struct commit_list *parent = *pp;
cc0e6c5a
AR
1777 switch (rewrite_one(revs, &parent->item)) {
1778 case rewrite_one_ok:
1779 break;
1780 case rewrite_one_noparents:
765ac8ec
LT
1781 *pp = parent->next;
1782 continue;
cc0e6c5a
AR
1783 case rewrite_one_error:
1784 return -1;
765ac8ec
LT
1785 }
1786 pp = &parent->next;
1787 }
11d65967 1788 remove_duplicate_parents(commit);
cc0e6c5a 1789 return 0;
765ac8ec
LT
1790}
1791
8ecae9b0
JH
1792static int commit_match(struct commit *commit, struct rev_info *opt)
1793{
0843acfd 1794 if (!opt->grep_filter.pattern_list)
8ecae9b0 1795 return 1;
0843acfd 1796 return grep_buffer(&opt->grep_filter,
2d10c555
JH
1797 NULL, /* we say nothing, not even filename */
1798 commit->buffer, strlen(commit->buffer));
8ecae9b0
JH
1799}
1800
f35f5603
JH
1801static inline int want_ancestry(struct rev_info *revs)
1802{
8bb65883 1803 return (revs->rewrite_parents || revs->children.name);
f35f5603
JH
1804}
1805
beb5af43 1806enum commit_action get_commit_action(struct rev_info *revs, struct commit *commit)
252a7c02
LT
1807{
1808 if (commit->object.flags & SHOWN)
1809 return commit_ignore;
4d6acb70 1810 if (revs->unpacked && has_sha1_pack(commit->object.sha1))
252a7c02 1811 return commit_ignore;
3131b713
LT
1812 if (revs->show_all)
1813 return commit_show;
252a7c02
LT
1814 if (commit->object.flags & UNINTERESTING)
1815 return commit_ignore;
1816 if (revs->min_age != -1 && (commit->date > revs->min_age))
1817 return commit_ignore;
1818 if (revs->no_merges && commit->parents && commit->parents->next)
1819 return commit_ignore;
b8e8db28
LT
1820 if (revs->merges_only && !(commit->parents && commit->parents->next))
1821 return commit_ignore;
252a7c02
LT
1822 if (!commit_match(commit, revs))
1823 return commit_ignore;
53b2c823 1824 if (revs->prune && revs->dense) {
252a7c02 1825 /* Commit without changes? */
7dc0fe3b 1826 if (commit->object.flags & TREESAME) {
252a7c02 1827 /* drop merges unless we want parenthood */
f35f5603 1828 if (!want_ancestry(revs))
252a7c02
LT
1829 return commit_ignore;
1830 /* non-merge - always ignore it */
1831 if (!commit->parents || !commit->parents->next)
1832 return commit_ignore;
1833 }
252a7c02
LT
1834 }
1835 return commit_show;
1836}
1837
beb5af43
AS
1838enum commit_action simplify_commit(struct rev_info *revs, struct commit *commit)
1839{
1840 enum commit_action action = get_commit_action(revs, commit);
1841
1842 if (action == commit_show &&
1843 !revs->show_all &&
1844 revs->prune && revs->dense && want_ancestry(revs)) {
1845 if (rewrite_parents(revs, commit) < 0)
1846 return commit_error;
1847 }
1848 return action;
1849}
1850
d5db6c9e 1851static struct commit *get_revision_1(struct rev_info *revs)
a4a88b2b 1852{
d5db6c9e 1853 if (!revs->commits)
a4a88b2b 1854 return NULL;
a4a88b2b 1855
765ac8ec 1856 do {
cb115748
LT
1857 struct commit_list *entry = revs->commits;
1858 struct commit *commit = entry->item;
ea5ed3ab 1859
cb115748
LT
1860 revs->commits = entry->next;
1861 free(entry);
2a0925be 1862
8860fd42
JS
1863 if (revs->reflog_info)
1864 fake_reflog_parent(revs->reflog_info, commit);
1865
2a0925be
LT
1866 /*
1867 * If we haven't done the list limiting, we need to look at
be7db6e5
LT
1868 * the parents here. We also need to do the date-based limiting
1869 * that we'd otherwise have done in limit_list().
2a0925be 1870 */
be7db6e5 1871 if (!revs->limited) {
744f4985 1872 if (revs->max_age != -1 &&
86ab4906
JH
1873 (commit->date < revs->max_age))
1874 continue;
fce87ae5 1875 if (add_parents_to_list(revs, commit, &revs->commits, NULL) < 0)
ed62089c
JH
1876 die("Failed to traverse parents of commit %s",
1877 sha1_to_hex(commit->object.sha1));
be7db6e5 1878 }
744f4985 1879
252a7c02
LT
1880 switch (simplify_commit(revs, commit)) {
1881 case commit_ignore:
8ecae9b0 1882 continue;
252a7c02 1883 case commit_error:
ed62089c
JH
1884 die("Failed to simplify parents of commit %s",
1885 sha1_to_hex(commit->object.sha1));
252a7c02
LT
1886 default:
1887 return commit;
384e99a4 1888 }
765ac8ec
LT
1889 } while (revs->commits);
1890 return NULL;
1891}
d5db6c9e 1892
86ab4906
JH
1893static void gc_boundary(struct object_array *array)
1894{
1895 unsigned nr = array->nr;
1896 unsigned alloc = array->alloc;
1897 struct object_array_entry *objects = array->objects;
1898
1899 if (alloc <= nr) {
1900 unsigned i, j;
1901 for (i = j = 0; i < nr; i++) {
1902 if (objects[i].item->flags & SHOWN)
1903 continue;
1904 if (i != j)
1905 objects[j] = objects[i];
1906 j++;
1907 }
892ae6bf 1908 for (i = j; i < nr; i++)
86ab4906
JH
1909 objects[i].item = NULL;
1910 array->nr = j;
1911 }
1912}
1913
4603ec0f
AS
1914static void create_boundary_commit_list(struct rev_info *revs)
1915{
1916 unsigned i;
1917 struct commit *c;
1918 struct object_array *array = &revs->boundary_commits;
1919 struct object_array_entry *objects = array->objects;
1920
1921 /*
1922 * If revs->commits is non-NULL at this point, an error occurred in
1923 * get_revision_1(). Ignore the error and continue printing the
1924 * boundary commits anyway. (This is what the code has always
1925 * done.)
1926 */
1927 if (revs->commits) {
1928 free_commit_list(revs->commits);
1929 revs->commits = NULL;
1930 }
1931
1932 /*
1933 * Put all of the actual boundary commits from revs->boundary_commits
1934 * into revs->commits
1935 */
1936 for (i = 0; i < array->nr; i++) {
1937 c = (struct commit *)(objects[i].item);
1938 if (!c)
1939 continue;
1940 if (!(c->object.flags & CHILD_SHOWN))
1941 continue;
1942 if (c->object.flags & (SHOWN | BOUNDARY))
1943 continue;
1944 c->object.flags |= BOUNDARY;
1945 commit_list_insert(c, &revs->commits);
1946 }
1947
1948 /*
1949 * If revs->topo_order is set, sort the boundary commits
1950 * in topological order
1951 */
1952 sort_in_topological_order(&revs->commits, revs->lifo);
1953}
1954
7fefda5c 1955static struct commit *get_revision_internal(struct rev_info *revs)
d5db6c9e
JH
1956{
1957 struct commit *c = NULL;
86ab4906
JH
1958 struct commit_list *l;
1959
1960 if (revs->boundary == 2) {
4603ec0f
AS
1961 /*
1962 * All of the normal commits have already been returned,
1963 * and we are now returning boundary commits.
1964 * create_boundary_commit_list() has populated
1965 * revs->commits with the remaining commits to return.
1966 */
1967 c = pop_commit(&revs->commits);
1968 if (c)
1969 c->object.flags |= SHOWN;
9c5e66e9
JS
1970 return c;
1971 }
1972
86ab4906
JH
1973 /*
1974 * Now pick up what they want to give us
1975 */
8839ac94
JH
1976 c = get_revision_1(revs);
1977 if (c) {
1978 while (0 < revs->skip_count) {
1979 revs->skip_count--;
1980 c = get_revision_1(revs);
1981 if (!c)
1982 break;
1983 }
86ab4906
JH
1984 }
1985
1986 /*
1987 * Check the max_count.
1988 */
d5db6c9e
JH
1989 switch (revs->max_count) {
1990 case -1:
1991 break;
1992 case 0:
86ab4906
JH
1993 c = NULL;
1994 break;
d5db6c9e
JH
1995 default:
1996 revs->max_count--;
1997 }
86ab4906 1998
c33d8593
JH
1999 if (c)
2000 c->object.flags |= SHOWN;
2001
2002 if (!revs->boundary) {
d5db6c9e 2003 return c;
c33d8593 2004 }
86ab4906
JH
2005
2006 if (!c) {
2007 /*
2008 * get_revision_1() runs out the commits, and
2009 * we are done computing the boundaries.
2010 * switch to boundary commits output mode.
2011 */
2012 revs->boundary = 2;
4603ec0f
AS
2013
2014 /*
2015 * Update revs->commits to contain the list of
2016 * boundary commits.
2017 */
2018 create_boundary_commit_list(revs);
2019
3c68d67b 2020 return get_revision_internal(revs);
86ab4906
JH
2021 }
2022
2023 /*
2024 * boundary commits are the commits that are parents of the
2025 * ones we got from get_revision_1() but they themselves are
2026 * not returned from get_revision_1(). Before returning
2027 * 'c', we need to mark its parents that they could be boundaries.
2028 */
2029
2030 for (l = c->parents; l; l = l->next) {
2031 struct object *p;
2032 p = &(l->item->object);
c33d8593 2033 if (p->flags & (CHILD_SHOWN | SHOWN))
86ab4906
JH
2034 continue;
2035 p->flags |= CHILD_SHOWN;
2036 gc_boundary(&revs->boundary_commits);
2037 add_object_array(p, NULL, &revs->boundary_commits);
2038 }
2039
2040 return c;
d5db6c9e 2041}
7fefda5c
AS
2042
2043struct commit *get_revision(struct rev_info *revs)
2044{
498bcd31
TR
2045 struct commit *c;
2046 struct commit_list *reversed;
2047
2048 if (revs->reverse) {
2049 reversed = NULL;
2050 while ((c = get_revision_internal(revs))) {
2051 commit_list_insert(c, &reversed);
2052 }
2053 revs->commits = reversed;
2054 revs->reverse = 0;
2055 revs->reverse_output_stage = 1;
2056 }
2057
2058 if (revs->reverse_output_stage)
2059 return pop_commit(&revs->commits);
2060
2061 c = get_revision_internal(revs);
7fefda5c
AS
2062 if (c && revs->graph)
2063 graph_update(revs->graph, c);
2064 return c;
2065}