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