]> git.ipfire.org Git - thirdparty/git.git/blame - revision.c
Merge branch 'jk/complete-merge-base'
[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"
894a9d33 15#include "string-list.h"
12da1d1f 16#include "line-log.h"
d72fbe81 17#include "mailmap.h"
53d00b39 18#include "commit-slab.h"
ae563542 19
cdcefbc9
LT
20volatile show_early_output_fn_t show_early_output;
21
cf2ab916 22char *path_name(const struct name_path *path, const char *name)
ae563542 23{
cf2ab916 24 const struct name_path *p;
ae563542
LT
25 char *n, *m;
26 int nlen = strlen(name);
27 int len = nlen + 1;
28
29 for (p = path; p; p = p->up) {
30 if (p->elem_len)
31 len += p->elem_len + 1;
32 }
33 n = xmalloc(len);
34 m = n + len - (nlen + 1);
35 strcpy(m, name);
36 for (p = path; p; p = p->up) {
37 if (p->elem_len) {
38 m -= p->elem_len + 1;
39 memcpy(m, p->elem, p->elem_len);
40 m[p->elem_len] = '/';
41 }
42 }
43 return n;
44}
45
beba25ab
JH
46static int show_path_component_truncated(FILE *out, const char *name, int len)
47{
48 int cnt;
49 for (cnt = 0; cnt < len; cnt++) {
50 int ch = name[cnt];
51 if (!ch || ch == '\n')
52 return -1;
53 fputc(ch, out);
54 }
55 return len;
56}
57
58static int show_path_truncated(FILE *out, const struct name_path *path)
59{
60 int emitted, ours;
61
62 if (!path)
63 return 0;
64 emitted = show_path_truncated(out, path->up);
65 if (emitted < 0)
66 return emitted;
67 if (emitted)
68 fputc('/', out);
69 ours = show_path_component_truncated(out, path->elem, path->elem_len);
70 if (ours < 0)
71 return ours;
72 return ours || emitted;
73}
74
ff5f5f26
MH
75void show_object_with_name(FILE *out, struct object *obj,
76 const struct name_path *path, const char *component)
91f17516 77{
beba25ab
JH
78 struct name_path leaf;
79 leaf.up = (struct name_path *)path;
80 leaf.elem = component;
81 leaf.elem_len = strlen(component);
91f17516 82
beba25ab
JH
83 fprintf(out, "%s ", sha1_to_hex(obj->sha1));
84 show_path_truncated(out, &leaf);
85 fputc('\n', out);
91f17516
JH
86}
87
1f1e895f
LT
88void add_object(struct object *obj,
89 struct object_array *p,
90 struct name_path *path,
91 const char *name)
ae563542 92{
31faeb20
MH
93 char *pn = path_name(path, name);
94 add_object_array(obj, pn, p);
95 free(pn);
ae563542
LT
96}
97
98static void mark_blob_uninteresting(struct blob *blob)
99{
c1ee9013
MK
100 if (!blob)
101 return;
ae563542
LT
102 if (blob->object.flags & UNINTERESTING)
103 return;
104 blob->object.flags |= UNINTERESTING;
105}
106
107void mark_tree_uninteresting(struct tree *tree)
108{
f75e53ed 109 struct tree_desc desc;
4c068a98 110 struct name_entry entry;
ae563542 111 struct object *obj = &tree->object;
ae563542 112
c1ee9013
MK
113 if (!tree)
114 return;
ae563542
LT
115 if (obj->flags & UNINTERESTING)
116 return;
117 obj->flags |= UNINTERESTING;
118 if (!has_sha1_file(obj->sha1))
119 return;
120 if (parse_tree(tree) < 0)
121 die("bad tree %s", sha1_to_hex(obj->sha1));
f75e53ed 122
6fda5e51 123 init_tree_desc(&desc, tree->buffer, tree->size);
4c068a98 124 while (tree_entry(&desc, &entry)) {
4d1012c3
LT
125 switch (object_type(entry.mode)) {
126 case OBJ_TREE:
4c068a98 127 mark_tree_uninteresting(lookup_tree(entry.sha1));
4d1012c3
LT
128 break;
129 case OBJ_BLOB:
4c068a98 130 mark_blob_uninteresting(lookup_blob(entry.sha1));
4d1012c3
LT
131 break;
132 default:
133 /* Subproject commit - not in this repository */
134 break;
135 }
ae563542 136 }
f75e53ed
LT
137
138 /*
139 * We don't care about the tree any more
140 * after it has been marked uninteresting.
141 */
6e454b9a 142 free_tree_buffer(tree);
ae563542
LT
143}
144
145void mark_parents_uninteresting(struct commit *commit)
146{
941ba8db
NTND
147 struct commit_list *parents = NULL, *l;
148
149 for (l = commit->parents; l; l = l->next)
150 commit_list_insert(l->item, &parents);
ae563542
LT
151
152 while (parents) {
153 struct commit *commit = parents->item;
941ba8db
NTND
154 l = parents;
155 parents = parents->next;
156 free(l);
157
158 while (commit) {
159 /*
160 * A missing commit is ok iff its parent is marked
161 * uninteresting.
162 *
163 * We just mark such a thing parsed, so that when
164 * it is popped next time around, we won't be trying
165 * to parse it and get an error.
166 */
167 if (!has_sha1_file(commit->object.sha1))
168 commit->object.parsed = 1;
169
170 if (commit->object.flags & UNINTERESTING)
171 break;
172
d2c4af73 173 commit->object.flags |= UNINTERESTING;
ae563542 174
d2c4af73
MU
175 /*
176 * Normally we haven't parsed the parent
177 * yet, so we won't have a parent of a parent
178 * here. However, it may turn out that we've
179 * reached this commit some other way (where it
180 * wasn't uninteresting), in which case we need
181 * to mark its parents recursively too..
182 */
941ba8db
NTND
183 if (!commit->parents)
184 break;
ae563542 185
941ba8db
NTND
186 for (l = commit->parents->next; l; l = l->next)
187 commit_list_insert(l->item, &parents);
188 commit = commit->parents->item;
189 }
ae563542
LT
190 }
191}
192
ff5f5f26
MH
193static void add_pending_object_with_mode(struct rev_info *revs,
194 struct object *obj,
195 const char *name, unsigned mode)
ae563542 196{
cc243c3c
JH
197 if (!obj)
198 return;
aa27e461 199 if (revs->no_walk && (obj->flags & UNINTERESTING))
f222abde 200 revs->no_walk = 0;
105e4733
JH
201 if (revs->reflog_info && obj->type == OBJ_COMMIT) {
202 struct strbuf buf = STRBUF_INIT;
cf99a761 203 int len = interpret_branch_name(name, 0, &buf);
105e4733
JH
204 int st;
205
206 if (0 < len && name[len] && buf.len)
207 strbuf_addstr(&buf, name + len);
208 st = add_reflog_for_walk(revs->reflog_info,
209 (struct commit *)obj,
210 buf.buf[0] ? buf.buf: name);
211 strbuf_release(&buf);
212 if (st)
213 return;
214 }
bb6c2fba 215 add_object_array_with_mode(obj, name, &revs->pending, mode);
ae563542
LT
216}
217
ff5f5f26
MH
218void add_pending_object(struct rev_info *revs,
219 struct object *obj, const char *name)
2d93b9fa
JH
220{
221 add_pending_object_with_mode(revs, obj, name, S_IFINVALID);
222}
223
3384a2df
JH
224void add_head_to_pending(struct rev_info *revs)
225{
226 unsigned char sha1[20];
227 struct object *obj;
228 if (get_sha1("HEAD", sha1))
229 return;
230 obj = parse_object(sha1);
231 if (!obj)
232 return;
233 add_pending_object(revs, obj, "HEAD");
234}
235
ff5f5f26
MH
236static struct object *get_reference(struct rev_info *revs, const char *name,
237 const unsigned char *sha1,
238 unsigned int flags)
ae563542
LT
239{
240 struct object *object;
241
242 object = parse_object(sha1);
cc243c3c
JH
243 if (!object) {
244 if (revs->ignore_missing)
245 return object;
ae563542 246 die("bad object %s", name);
cc243c3c 247 }
cd2bdc53
LT
248 object->flags |= flags;
249 return object;
250}
251
26c3177e
RS
252void add_pending_sha1(struct rev_info *revs, const char *name,
253 const unsigned char *sha1, unsigned int flags)
254{
255 struct object *object = get_reference(revs, name, sha1, flags);
256 add_pending_object(revs, object, name);
257}
258
ff5f5f26
MH
259static struct commit *handle_commit(struct rev_info *revs,
260 struct object *object, const char *name)
cd2bdc53
LT
261{
262 unsigned long flags = object->flags;
ae563542
LT
263
264 /*
265 * Tag object? Look what it points to..
266 */
1974632c 267 while (object->type == OBJ_TAG) {
ae563542 268 struct tag *tag = (struct tag *) object;
cd2bdc53 269 if (revs->tag_objects && !(flags & UNINTERESTING))
ae563542 270 add_pending_object(revs, object, tag->tag);
9684afd9
MK
271 if (!tag->tagged)
272 die("bad tag");
ae563542 273 object = parse_object(tag->tagged->sha1);
aeeae1b7
JH
274 if (!object) {
275 if (flags & UNINTERESTING)
276 return NULL;
ae563542 277 die("bad object %s", sha1_to_hex(tag->tagged->sha1));
aeeae1b7 278 }
ae563542
LT
279 }
280
281 /*
282 * Commit object? Just return it, we'll do all the complex
283 * reachability crud.
284 */
1974632c 285 if (object->type == OBJ_COMMIT) {
ae563542 286 struct commit *commit = (struct commit *)object;
ae563542
LT
287 if (parse_commit(commit) < 0)
288 die("unable to parse commit %s", name);
d9a83684 289 if (flags & UNINTERESTING) {
4262c1b0 290 commit->object.flags |= UNINTERESTING;
ae563542 291 mark_parents_uninteresting(commit);
d9a83684
LT
292 revs->limited = 1;
293 }
0f3a290b
LT
294 if (revs->show_source && !commit->util)
295 commit->util = (void *) name;
ae563542
LT
296 return commit;
297 }
298
299 /*
3ea3c215 300 * Tree object? Either mark it uninteresting, or add it
ae563542
LT
301 * to the list of objects to look at later..
302 */
1974632c 303 if (object->type == OBJ_TREE) {
ae563542
LT
304 struct tree *tree = (struct tree *)object;
305 if (!revs->tree_objects)
306 return NULL;
307 if (flags & UNINTERESTING) {
308 mark_tree_uninteresting(tree);
309 return NULL;
310 }
311 add_pending_object(revs, object, "");
312 return NULL;
313 }
314
315 /*
316 * Blob object? You know the drill by now..
317 */
1974632c 318 if (object->type == OBJ_BLOB) {
ae563542
LT
319 struct blob *blob = (struct blob *)object;
320 if (!revs->blob_objects)
321 return NULL;
322 if (flags & UNINTERESTING) {
323 mark_blob_uninteresting(blob);
324 return NULL;
325 }
326 add_pending_object(revs, object, "");
327 return NULL;
328 }
329 die("%s is unknown object", name);
330}
331
a4a88b2b
LT
332static int everybody_uninteresting(struct commit_list *orig)
333{
334 struct commit_list *list = orig;
335 while (list) {
336 struct commit *commit = list->item;
337 list = list->next;
338 if (commit->object.flags & UNINTERESTING)
339 continue;
340 return 0;
341 }
342 return 1;
343}
344
4d826608
KB
345/*
346 * A definition of "relevant" commit that we can use to simplify limited graphs
347 * by eliminating side branches.
348 *
349 * A "relevant" commit is one that is !UNINTERESTING (ie we are including it
350 * in our list), or that is a specified BOTTOM commit. Then after computing
351 * a limited list, during processing we can generally ignore boundary merges
352 * coming from outside the graph, (ie from irrelevant parents), and treat
353 * those merges as if they were single-parent. TREESAME is defined to consider
354 * only relevant parents, if any. If we are TREESAME to our on-graph parents,
355 * we don't care if we were !TREESAME to non-graph parents.
356 *
357 * Treating bottom commits as relevant ensures that a limited graph's
358 * connection to the actual bottom commit is not viewed as a side branch, but
359 * treated as part of the graph. For example:
360 *
361 * ....Z...A---X---o---o---B
362 * . /
363 * W---Y
364 *
365 * When computing "A..B", the A-X connection is at least as important as
366 * Y-X, despite A being flagged UNINTERESTING.
367 *
368 * And when computing --ancestry-path "A..B", the A-X connection is more
369 * important than Y-X, despite both A and Y being flagged UNINTERESTING.
370 */
371static inline int relevant_commit(struct commit *commit)
372{
373 return (commit->object.flags & (UNINTERESTING | BOTTOM)) != UNINTERESTING;
374}
375
376/*
377 * Return a single relevant commit from a parent list. If we are a TREESAME
378 * commit, and this selects one of our parents, then we can safely simplify to
379 * that parent.
380 */
381static struct commit *one_relevant_parent(const struct rev_info *revs,
382 struct commit_list *orig)
383{
384 struct commit_list *list = orig;
385 struct commit *relevant = NULL;
386
387 if (!orig)
388 return NULL;
389
390 /*
391 * For 1-parent commits, or if first-parent-only, then return that
392 * first parent (even if not "relevant" by the above definition).
393 * TREESAME will have been set purely on that parent.
394 */
395 if (revs->first_parent_only || !orig->next)
396 return orig->item;
397
398 /*
399 * For multi-parent commits, identify a sole relevant parent, if any.
400 * If we have only one relevant parent, then TREESAME will be set purely
401 * with regard to that parent, and we can simplify accordingly.
402 *
403 * If we have more than one relevant parent, or no relevant parents
404 * (and multiple irrelevant ones), then we can't select a parent here
405 * and return NULL.
406 */
407 while (list) {
408 struct commit *commit = list->item;
409 list = list->next;
410 if (relevant_commit(commit)) {
411 if (relevant)
412 return NULL;
413 relevant = commit;
414 }
415 }
416 return relevant;
417}
418
0a4ba7f8
JH
419/*
420 * The goal is to get REV_TREE_NEW as the result only if the
ceff8e7a
LT
421 * diff consists of all '+' (and no other changes), REV_TREE_OLD
422 * if the whole diff is removal of old data, and otherwise
423 * REV_TREE_DIFFERENT (of course if the trees are the same we
424 * want REV_TREE_SAME).
425 * That means that once we get to REV_TREE_DIFFERENT, we do not
426 * have to look any further.
0a4ba7f8 427 */
8efdc326 428static int tree_difference = REV_TREE_SAME;
a4a88b2b
LT
429
430static void file_add_remove(struct diff_options *options,
431 int addremove, unsigned mode,
432 const unsigned char *sha1,
e5450100 433 int sha1_valid,
e3d42c47 434 const char *fullpath, unsigned dirty_submodule)
a4a88b2b 435{
ceff8e7a 436 int diff = addremove == '+' ? REV_TREE_NEW : REV_TREE_OLD;
a4a88b2b 437
ceff8e7a 438 tree_difference |= diff;
dd47aa31 439 if (tree_difference == REV_TREE_DIFFERENT)
8f67f8ae 440 DIFF_OPT_SET(options, HAS_CHANGES);
a4a88b2b
LT
441}
442
443static void file_change(struct diff_options *options,
444 unsigned old_mode, unsigned new_mode,
445 const unsigned char *old_sha1,
446 const unsigned char *new_sha1,
e5450100 447 int old_sha1_valid, int new_sha1_valid,
e3d42c47
JL
448 const char *fullpath,
449 unsigned old_dirty_submodule, unsigned new_dirty_submodule)
a4a88b2b 450{
8efdc326 451 tree_difference = REV_TREE_DIFFERENT;
8f67f8ae 452 DIFF_OPT_SET(options, HAS_CHANGES);
a4a88b2b
LT
453}
454
ff5f5f26
MH
455static int rev_compare_tree(struct rev_info *revs,
456 struct commit *parent, struct commit *commit)
a4a88b2b 457{
3a5e8608
LT
458 struct tree *t1 = parent->tree;
459 struct tree *t2 = commit->tree;
460
a4a88b2b 461 if (!t1)
8efdc326 462 return REV_TREE_NEW;
ceff8e7a
LT
463 if (!t2)
464 return REV_TREE_OLD;
78892e32
LT
465
466 if (revs->simplify_by_decoration) {
467 /*
468 * If we are simplifying by decoration, then the commit
469 * is worth showing if it has a tag pointing at it.
470 */
471 if (lookup_decoration(&name_decoration, &commit->object))
472 return REV_TREE_DIFFERENT;
473 /*
474 * A commit that is not pointed by a tag is uninteresting
475 * if we are not limited by path. This means that you will
476 * see the usual "commits that touch the paths" plus any
477 * tagged commit by specifying both --simplify-by-decoration
478 * and pathspec.
479 */
afe069d1 480 if (!revs->prune_data.nr)
78892e32
LT
481 return REV_TREE_SAME;
482 }
ceff8e7a 483
8efdc326 484 tree_difference = REV_TREE_SAME;
8f67f8ae 485 DIFF_OPT_CLR(&revs->pruning, HAS_CHANGES);
c4e05b1a 486 if (diff_tree_sha1(t1->object.sha1, t2->object.sha1, "",
cd2bdc53 487 &revs->pruning) < 0)
8efdc326 488 return REV_TREE_DIFFERENT;
a4a88b2b
LT
489 return tree_difference;
490}
491
3a5e8608 492static int rev_same_tree_as_empty(struct rev_info *revs, struct commit *commit)
a4a88b2b
LT
493{
494 int retval;
495 void *tree;
6fda5e51 496 unsigned long size;
a4a88b2b 497 struct tree_desc empty, real;
3a5e8608 498 struct tree *t1 = commit->tree;
a4a88b2b
LT
499
500 if (!t1)
501 return 0;
502
6fda5e51 503 tree = read_object_with_reference(t1->object.sha1, tree_type, &size, NULL);
a4a88b2b
LT
504 if (!tree)
505 return 0;
6fda5e51
LT
506 init_tree_desc(&real, tree, size);
507 init_tree_desc(&empty, "", 0);
a4a88b2b 508
0a4ba7f8 509 tree_difference = REV_TREE_SAME;
8f67f8ae 510 DIFF_OPT_CLR(&revs->pruning, HAS_CHANGES);
cd2bdc53 511 retval = diff_tree(&empty, &real, "", &revs->pruning);
a4a88b2b
LT
512 free(tree);
513
0a4ba7f8 514 return retval >= 0 && (tree_difference == REV_TREE_SAME);
a4a88b2b
LT
515}
516
d0af663e
KB
517struct treesame_state {
518 unsigned int nparents;
519 unsigned char treesame[FLEX_ARRAY];
520};
521
522static struct treesame_state *initialise_treesame(struct rev_info *revs, struct commit *commit)
523{
524 unsigned n = commit_list_count(commit->parents);
525 struct treesame_state *st = xcalloc(1, sizeof(*st) + n);
526 st->nparents = n;
527 add_decoration(&revs->treesame, &commit->object, st);
528 return st;
529}
530
531/*
532 * Must be called immediately after removing the nth_parent from a commit's
533 * parent list, if we are maintaining the per-parent treesame[] decoration.
534 * This does not recalculate the master TREESAME flag - update_treesame()
535 * should be called to update it after a sequence of treesame[] modifications
536 * that may have affected it.
537 */
538static int compact_treesame(struct rev_info *revs, struct commit *commit, unsigned nth_parent)
539{
540 struct treesame_state *st;
541 int old_same;
542
543 if (!commit->parents) {
544 /*
545 * Have just removed the only parent from a non-merge.
546 * Different handling, as we lack decoration.
547 */
548 if (nth_parent != 0)
549 die("compact_treesame %u", nth_parent);
550 old_same = !!(commit->object.flags & TREESAME);
551 if (rev_same_tree_as_empty(revs, commit))
552 commit->object.flags |= TREESAME;
553 else
554 commit->object.flags &= ~TREESAME;
555 return old_same;
556 }
557
558 st = lookup_decoration(&revs->treesame, &commit->object);
559 if (!st || nth_parent >= st->nparents)
560 die("compact_treesame %u", nth_parent);
561
562 old_same = st->treesame[nth_parent];
563 memmove(st->treesame + nth_parent,
564 st->treesame + nth_parent + 1,
565 st->nparents - nth_parent - 1);
566
567 /*
568 * If we've just become a non-merge commit, update TREESAME
569 * immediately, and remove the no-longer-needed decoration.
570 * If still a merge, defer update until update_treesame().
571 */
572 if (--st->nparents == 1) {
573 if (commit->parents->next)
574 die("compact_treesame parents mismatch");
575 if (st->treesame[0] && revs->dense)
576 commit->object.flags |= TREESAME;
577 else
578 commit->object.flags &= ~TREESAME;
579 free(add_decoration(&revs->treesame, &commit->object, NULL));
580 }
581
582 return old_same;
583}
584
585static unsigned update_treesame(struct rev_info *revs, struct commit *commit)
586{
587 if (commit->parents && commit->parents->next) {
588 unsigned n;
589 struct treesame_state *st;
4d826608
KB
590 struct commit_list *p;
591 unsigned relevant_parents;
592 unsigned relevant_change, irrelevant_change;
d0af663e
KB
593
594 st = lookup_decoration(&revs->treesame, &commit->object);
595 if (!st)
596 die("update_treesame %s", sha1_to_hex(commit->object.sha1));
4d826608
KB
597 relevant_parents = 0;
598 relevant_change = irrelevant_change = 0;
599 for (p = commit->parents, n = 0; p; n++, p = p->next) {
600 if (relevant_commit(p->item)) {
601 relevant_change |= !st->treesame[n];
602 relevant_parents++;
603 } else
604 irrelevant_change |= !st->treesame[n];
d0af663e 605 }
4d826608
KB
606 if (relevant_parents ? relevant_change : irrelevant_change)
607 commit->object.flags &= ~TREESAME;
608 else
609 commit->object.flags |= TREESAME;
d0af663e
KB
610 }
611
612 return commit->object.flags & TREESAME;
613}
614
4d826608
KB
615static inline int limiting_can_increase_treesame(const struct rev_info *revs)
616{
617 /*
618 * TREESAME is irrelevant unless prune && dense;
619 * if simplify_history is set, we can't have a mixture of TREESAME and
620 * !TREESAME INTERESTING parents (and we don't have treesame[]
621 * decoration anyway);
622 * if first_parent_only is set, then the TREESAME flag is locked
623 * against the first parent (and again we lack treesame[] decoration).
624 */
625 return revs->prune && revs->dense &&
626 !revs->simplify_history &&
627 !revs->first_parent_only;
628}
629
a4a88b2b
LT
630static void try_to_simplify_commit(struct rev_info *revs, struct commit *commit)
631{
632 struct commit_list **pp, *parent;
d0af663e 633 struct treesame_state *ts = NULL;
4d826608
KB
634 int relevant_change = 0, irrelevant_change = 0;
635 int relevant_parents, nth_parent;
a4a88b2b 636
53b2c823
LT
637 /*
638 * If we don't do pruning, everything is interesting
639 */
7dc0fe3b 640 if (!revs->prune)
53b2c823 641 return;
53b2c823 642
a4a88b2b
LT
643 if (!commit->tree)
644 return;
645
646 if (!commit->parents) {
3a5e8608 647 if (rev_same_tree_as_empty(revs, commit))
7dc0fe3b 648 commit->object.flags |= TREESAME;
a4a88b2b
LT
649 return;
650 }
651
53b2c823
LT
652 /*
653 * Normal non-merge commit? If we don't want to make the
654 * history dense, we consider it always to be a change..
655 */
7dc0fe3b 656 if (!revs->dense && !commit->parents->next)
53b2c823 657 return;
53b2c823 658
4d826608 659 for (pp = &commit->parents, nth_parent = 0, relevant_parents = 0;
d0af663e
KB
660 (parent = *pp) != NULL;
661 pp = &parent->next, nth_parent++) {
a4a88b2b 662 struct commit *p = parent->item;
4d826608
KB
663 if (relevant_commit(p))
664 relevant_parents++;
a4a88b2b 665
d0af663e
KB
666 if (nth_parent == 1) {
667 /*
668 * This our second loop iteration - so we now know
669 * we're dealing with a merge.
670 *
671 * Do not compare with later parents when we care only about
672 * the first parent chain, in order to avoid derailing the
673 * traversal to follow a side branch that brought everything
674 * in the path we are limited to by the pathspec.
675 */
676 if (revs->first_parent_only)
677 break;
678 /*
679 * If this will remain a potentially-simplifiable
680 * merge, remember per-parent treesame if needed.
681 * Initialise the array with the comparison from our
682 * first iteration.
683 */
684 if (revs->treesame.name &&
685 !revs->simplify_history &&
686 !(commit->object.flags & UNINTERESTING)) {
687 ts = initialise_treesame(revs, commit);
4d826608 688 if (!(irrelevant_change || relevant_change))
d0af663e
KB
689 ts->treesame[0] = 1;
690 }
691 }
cc0e6c5a
AR
692 if (parse_commit(p) < 0)
693 die("cannot simplify commit %s (because of %s)",
694 sha1_to_hex(commit->object.sha1),
695 sha1_to_hex(p->object.sha1));
3a5e8608 696 switch (rev_compare_tree(revs, p, commit)) {
8efdc326 697 case REV_TREE_SAME:
141efdba 698 if (!revs->simplify_history || !relevant_commit(p)) {
f3219fbb
JH
699 /* Even if a merge with an uninteresting
700 * side branch brought the entire change
701 * we are interested in, we do not want
702 * to lose the other branches of this
703 * merge, so we just keep going.
704 */
d0af663e
KB
705 if (ts)
706 ts->treesame[nth_parent] = 1;
f3219fbb
JH
707 continue;
708 }
a4a88b2b
LT
709 parent->next = NULL;
710 commit->parents = parent;
7dc0fe3b 711 commit->object.flags |= TREESAME;
a4a88b2b
LT
712 return;
713
8efdc326
FK
714 case REV_TREE_NEW:
715 if (revs->remove_empty_trees &&
3a5e8608 716 rev_same_tree_as_empty(revs, p)) {
c348f31a
JH
717 /* We are adding all the specified
718 * paths from this parent, so the
719 * history beyond this parent is not
720 * interesting. Remove its parents
721 * (they are grandparents for us).
722 * IOW, we pretend this parent is a
723 * "root" commit.
a41e109c 724 */
cc0e6c5a
AR
725 if (parse_commit(p) < 0)
726 die("cannot simplify commit %s (invalid %s)",
727 sha1_to_hex(commit->object.sha1),
728 sha1_to_hex(p->object.sha1));
c348f31a 729 p->parents = NULL;
a4a88b2b
LT
730 }
731 /* fallthrough */
ceff8e7a 732 case REV_TREE_OLD:
8efdc326 733 case REV_TREE_DIFFERENT:
4d826608
KB
734 if (relevant_commit(p))
735 relevant_change = 1;
736 else
737 irrelevant_change = 1;
a4a88b2b
LT
738 continue;
739 }
740 die("bad tree compare for commit %s", sha1_to_hex(commit->object.sha1));
741 }
4d826608
KB
742
743 /*
744 * TREESAME is straightforward for single-parent commits. For merge
745 * commits, it is most useful to define it so that "irrelevant"
746 * parents cannot make us !TREESAME - if we have any relevant
747 * parents, then we only consider TREESAMEness with respect to them,
748 * allowing irrelevant merges from uninteresting branches to be
749 * simplified away. Only if we have only irrelevant parents do we
750 * base TREESAME on them. Note that this logic is replicated in
751 * update_treesame, which should be kept in sync.
752 */
753 if (relevant_parents ? !relevant_change : !irrelevant_change)
754 commit->object.flags |= TREESAME;
a4a88b2b
LT
755}
756
47e44ed1 757static void commit_list_insert_by_date_cached(struct commit *p, struct commit_list **head,
fce87ae5
AG
758 struct commit_list *cached_base, struct commit_list **cache)
759{
760 struct commit_list *new_entry;
761
762 if (cached_base && p->date < cached_base->item->date)
47e44ed1 763 new_entry = commit_list_insert_by_date(p, &cached_base->next);
fce87ae5 764 else
47e44ed1 765 new_entry = commit_list_insert_by_date(p, head);
fce87ae5
AG
766
767 if (cache && (!*cache || p->date < (*cache)->item->date))
768 *cache = new_entry;
769}
770
771static int add_parents_to_list(struct rev_info *revs, struct commit *commit,
772 struct commit_list **list, struct commit_list **cache_ptr)
a4a88b2b
LT
773{
774 struct commit_list *parent = commit->parents;
577ed5c2 775 unsigned left_flag;
fce87ae5 776 struct commit_list *cached_base = cache_ptr ? *cache_ptr : NULL;
a4a88b2b 777
3381c790 778 if (commit->object.flags & ADDED)
cc0e6c5a 779 return 0;
3381c790
LT
780 commit->object.flags |= ADDED;
781
a4a88b2b
LT
782 /*
783 * If the commit is uninteresting, don't try to
784 * prune parents - we want the maximal uninteresting
785 * set.
786 *
787 * Normally we haven't parsed the parent
788 * yet, so we won't have a parent of a parent
789 * here. However, it may turn out that we've
790 * reached this commit some other way (where it
791 * wasn't uninteresting), in which case we need
792 * to mark its parents recursively too..
793 */
794 if (commit->object.flags & UNINTERESTING) {
795 while (parent) {
796 struct commit *p = parent->item;
797 parent = parent->next;
aeeae1b7
JH
798 if (p)
799 p->object.flags |= UNINTERESTING;
cc0e6c5a 800 if (parse_commit(p) < 0)
aeeae1b7 801 continue;
a4a88b2b
LT
802 if (p->parents)
803 mark_parents_uninteresting(p);
804 if (p->object.flags & SEEN)
805 continue;
806 p->object.flags |= SEEN;
47e44ed1 807 commit_list_insert_by_date_cached(p, list, cached_base, cache_ptr);
a4a88b2b 808 }
cc0e6c5a 809 return 0;
a4a88b2b
LT
810 }
811
812 /*
813 * Ok, the commit wasn't uninteresting. Try to
814 * simplify the commit history and find the parent
815 * that has no differences in the path set if one exists.
816 */
53b2c823 817 try_to_simplify_commit(revs, commit);
a4a88b2b 818
ba1d4505 819 if (revs->no_walk)
cc0e6c5a 820 return 0;
ba1d4505 821
577ed5c2 822 left_flag = (commit->object.flags & SYMMETRIC_LEFT);
0053e902 823
d9c292e8 824 for (parent = commit->parents; parent; parent = parent->next) {
a4a88b2b
LT
825 struct commit *p = parent->item;
826
cc0e6c5a
AR
827 if (parse_commit(p) < 0)
828 return -1;
0f3a290b
LT
829 if (revs->show_source && !p->util)
830 p->util = commit->util;
577ed5c2 831 p->object.flags |= left_flag;
ad1012eb
LH
832 if (!(p->object.flags & SEEN)) {
833 p->object.flags |= SEEN;
47e44ed1 834 commit_list_insert_by_date_cached(p, list, cached_base, cache_ptr);
ad1012eb 835 }
60d30b02 836 if (revs->first_parent_only)
d9c292e8 837 break;
a4a88b2b 838 }
cc0e6c5a 839 return 0;
a4a88b2b
LT
840}
841
36d56de6 842static void cherry_pick_list(struct commit_list *list, struct rev_info *revs)
d7a17cad
JH
843{
844 struct commit_list *p;
845 int left_count = 0, right_count = 0;
846 int left_first;
847 struct patch_ids ids;
adbbb31e 848 unsigned cherry_flag;
d7a17cad
JH
849
850 /* First count the commits on the left and on the right */
851 for (p = list; p; p = p->next) {
852 struct commit *commit = p->item;
853 unsigned flags = commit->object.flags;
854 if (flags & BOUNDARY)
855 ;
856 else if (flags & SYMMETRIC_LEFT)
857 left_count++;
858 else
859 right_count++;
860 }
861
36c07975
TR
862 if (!left_count || !right_count)
863 return;
864
d7a17cad
JH
865 left_first = left_count < right_count;
866 init_patch_ids(&ids);
66f13625 867 ids.diffopts.pathspec = revs->diffopt.pathspec;
d7a17cad
JH
868
869 /* Compute patch-ids for one side */
870 for (p = list; p; p = p->next) {
871 struct commit *commit = p->item;
872 unsigned flags = commit->object.flags;
873
874 if (flags & BOUNDARY)
875 continue;
876 /*
877 * If we have fewer left, left_first is set and we omit
878 * commits on the right branch in this loop. If we have
879 * fewer right, we skip the left ones.
880 */
881 if (left_first != !!(flags & SYMMETRIC_LEFT))
882 continue;
883 commit->util = add_commit_patch_id(commit, &ids);
884 }
885
adbbb31e
MG
886 /* either cherry_mark or cherry_pick are true */
887 cherry_flag = revs->cherry_mark ? PATCHSAME : SHOWN;
888
d7a17cad
JH
889 /* Check the other side */
890 for (p = list; p; p = p->next) {
891 struct commit *commit = p->item;
892 struct patch_id *id;
893 unsigned flags = commit->object.flags;
894
895 if (flags & BOUNDARY)
896 continue;
897 /*
898 * If we have fewer left, left_first is set and we omit
899 * commits on the left branch in this loop.
900 */
901 if (left_first == !!(flags & SYMMETRIC_LEFT))
902 continue;
903
904 /*
905 * Have we seen the same patch id?
906 */
907 id = has_commit_patch_id(commit, &ids);
908 if (!id)
909 continue;
910 id->seen = 1;
adbbb31e 911 commit->object.flags |= cherry_flag;
d7a17cad
JH
912 }
913
914 /* Now check the original side for seen ones */
915 for (p = list; p; p = p->next) {
916 struct commit *commit = p->item;
917 struct patch_id *ent;
918
919 ent = commit->util;
920 if (!ent)
921 continue;
922 if (ent->seen)
adbbb31e 923 commit->object.flags |= cherry_flag;
d7a17cad
JH
924 commit->util = NULL;
925 }
926
927 free_patch_ids(&ids);
928}
929
7d004199
LT
930/* How many extra uninteresting commits we want to see.. */
931#define SLOP 5
932
933static int still_interesting(struct commit_list *src, unsigned long date, int slop)
3131b713 934{
7d004199
LT
935 /*
936 * No source list at all? We're definitely done..
937 */
938 if (!src)
939 return 0;
940
941 /*
942 * Does the destination list contain entries with a date
943 * before the source list? Definitely _not_ done.
944 */
c19d1b4e 945 if (date <= src->item->date)
7d004199
LT
946 return SLOP;
947
948 /*
949 * Does the source list still have interesting commits in
950 * it? Definitely not done..
951 */
952 if (!everybody_uninteresting(src))
953 return SLOP;
954
955 /* Ok, we're closing in.. */
956 return slop-1;
3131b713
LT
957}
958
ebdc94f3
JH
959/*
960 * "rev-list --ancestry-path A..B" computes commits that are ancestors
961 * of B but not ancestors of A but further limits the result to those
962 * that are descendants of A. This takes the list of bottom commits and
963 * the result of "A..B" without --ancestry-path, and limits the latter
964 * further to the ones that can reach one of the commits in "bottom".
965 */
966static void limit_to_ancestry(struct commit_list *bottom, struct commit_list *list)
967{
968 struct commit_list *p;
969 struct commit_list *rlist = NULL;
970 int made_progress;
971
972 /*
973 * Reverse the list so that it will be likely that we would
974 * process parents before children.
975 */
976 for (p = list; p; p = p->next)
977 commit_list_insert(p->item, &rlist);
978
979 for (p = bottom; p; p = p->next)
980 p->item->object.flags |= TMP_MARK;
981
982 /*
983 * Mark the ones that can reach bottom commits in "list",
984 * in a bottom-up fashion.
985 */
986 do {
987 made_progress = 0;
988 for (p = rlist; p; p = p->next) {
989 struct commit *c = p->item;
990 struct commit_list *parents;
991 if (c->object.flags & (TMP_MARK | UNINTERESTING))
992 continue;
993 for (parents = c->parents;
994 parents;
995 parents = parents->next) {
996 if (!(parents->item->object.flags & TMP_MARK))
997 continue;
998 c->object.flags |= TMP_MARK;
999 made_progress = 1;
1000 break;
1001 }
1002 }
1003 } while (made_progress);
1004
1005 /*
1006 * NEEDSWORK: decide if we want to remove parents that are
1007 * not marked with TMP_MARK from commit->parents for commits
1008 * in the resulting list. We may not want to do that, though.
1009 */
1010
1011 /*
1012 * The ones that are not marked with TMP_MARK are uninteresting
1013 */
1014 for (p = list; p; p = p->next) {
1015 struct commit *c = p->item;
1016 if (c->object.flags & TMP_MARK)
1017 continue;
1018 c->object.flags |= UNINTERESTING;
1019 }
1020
1021 /* We are done with the TMP_MARK */
1022 for (p = list; p; p = p->next)
1023 p->item->object.flags &= ~TMP_MARK;
1024 for (p = bottom; p; p = p->next)
1025 p->item->object.flags &= ~TMP_MARK;
1026 free_commit_list(rlist);
1027}
1028
1029/*
1030 * Before walking the history, keep the set of "negative" refs the
1031 * caller has asked to exclude.
1032 *
1033 * This is used to compute "rev-list --ancestry-path A..B", as we need
1034 * to filter the result of "A..B" further to the ones that can actually
1035 * reach A.
1036 */
7f34a46f 1037static struct commit_list *collect_bottom_commits(struct commit_list *list)
ebdc94f3 1038{
7f34a46f
KB
1039 struct commit_list *elem, *bottom = NULL;
1040 for (elem = list; elem; elem = elem->next)
1041 if (elem->item->object.flags & BOTTOM)
1042 commit_list_insert(elem->item, &bottom);
ebdc94f3
JH
1043 return bottom;
1044}
1045
60adf7d7
MG
1046/* Assumes either left_only or right_only is set */
1047static void limit_left_right(struct commit_list *list, struct rev_info *revs)
1048{
1049 struct commit_list *p;
1050
1051 for (p = list; p; p = p->next) {
1052 struct commit *commit = p->item;
1053
1054 if (revs->right_only) {
1055 if (commit->object.flags & SYMMETRIC_LEFT)
1056 commit->object.flags |= SHOWN;
1057 } else /* revs->left_only is set */
1058 if (!(commit->object.flags & SYMMETRIC_LEFT))
1059 commit->object.flags |= SHOWN;
1060 }
1061}
1062
cc0e6c5a 1063static int limit_list(struct rev_info *revs)
a4a88b2b 1064{
7d004199
LT
1065 int slop = SLOP;
1066 unsigned long date = ~0ul;
a4a88b2b
LT
1067 struct commit_list *list = revs->commits;
1068 struct commit_list *newlist = NULL;
1069 struct commit_list **p = &newlist;
ebdc94f3
JH
1070 struct commit_list *bottom = NULL;
1071
1072 if (revs->ancestry_path) {
7f34a46f 1073 bottom = collect_bottom_commits(list);
ebdc94f3 1074 if (!bottom)
97b03c35 1075 die("--ancestry-path given but there are no bottom commits");
ebdc94f3 1076 }
a4a88b2b
LT
1077
1078 while (list) {
1079 struct commit_list *entry = list;
1080 struct commit *commit = list->item;
1081 struct object *obj = &commit->object;
cdcefbc9 1082 show_early_output_fn_t show;
a4a88b2b
LT
1083
1084 list = list->next;
1085 free(entry);
1086
1087 if (revs->max_age != -1 && (commit->date < revs->max_age))
1088 obj->flags |= UNINTERESTING;
fce87ae5 1089 if (add_parents_to_list(revs, commit, &list, NULL) < 0)
cc0e6c5a 1090 return -1;
a4a88b2b
LT
1091 if (obj->flags & UNINTERESTING) {
1092 mark_parents_uninteresting(commit);
7d004199
LT
1093 if (revs->show_all)
1094 p = &commit_list_insert(commit, p)->next;
1095 slop = still_interesting(list, date, slop);
1096 if (slop)
3131b713 1097 continue;
7d004199
LT
1098 /* If showing all, add the whole pending list to the end */
1099 if (revs->show_all)
1100 *p = list;
1101 break;
a4a88b2b
LT
1102 }
1103 if (revs->min_age != -1 && (commit->date > revs->min_age))
1104 continue;
7d004199 1105 date = commit->date;
a4a88b2b 1106 p = &commit_list_insert(commit, p)->next;
cdcefbc9
LT
1107
1108 show = show_early_output;
1109 if (!show)
1110 continue;
1111
1112 show(revs, newlist);
1113 show_early_output = NULL;
a4a88b2b 1114 }
adbbb31e 1115 if (revs->cherry_pick || revs->cherry_mark)
36d56de6 1116 cherry_pick_list(newlist, revs);
d7a17cad 1117
60adf7d7
MG
1118 if (revs->left_only || revs->right_only)
1119 limit_left_right(newlist, revs);
1120
ebdc94f3
JH
1121 if (bottom) {
1122 limit_to_ancestry(bottom, newlist);
1123 free_commit_list(bottom);
1124 }
1125
4d826608
KB
1126 /*
1127 * Check if any commits have become TREESAME by some of their parents
1128 * becoming UNINTERESTING.
1129 */
1130 if (limiting_can_increase_treesame(revs))
1131 for (list = newlist; list; list = list->next) {
1132 struct commit *c = list->item;
1133 if (c->object.flags & (UNINTERESTING | TREESAME))
1134 continue;
1135 update_treesame(revs, c);
1136 }
1137
a4a88b2b 1138 revs->commits = newlist;
cc0e6c5a 1139 return 0;
a4a88b2b
LT
1140}
1141
df835d3a
MH
1142/*
1143 * Add an entry to refs->cmdline with the specified information.
1144 * *name is copied.
1145 */
281eee47
JH
1146static void add_rev_cmdline(struct rev_info *revs,
1147 struct object *item,
1148 const char *name,
1149 int whence,
1150 unsigned flags)
1151{
1152 struct rev_cmdline_info *info = &revs->cmdline;
1153 int nr = info->nr;
1154
1155 ALLOC_GROW(info->rev, nr + 1, info->alloc);
1156 info->rev[nr].item = item;
df835d3a 1157 info->rev[nr].name = xstrdup(name);
281eee47
JH
1158 info->rev[nr].whence = whence;
1159 info->rev[nr].flags = flags;
1160 info->nr++;
1161}
1162
a765499a
KB
1163static void add_rev_cmdline_list(struct rev_info *revs,
1164 struct commit_list *commit_list,
1165 int whence,
1166 unsigned flags)
1167{
1168 while (commit_list) {
1169 struct object *object = &commit_list->item->object;
1170 add_rev_cmdline(revs, object, sha1_to_hex(object->sha1),
1171 whence, flags);
1172 commit_list = commit_list->next;
1173 }
1174}
1175
63049292
JH
1176struct all_refs_cb {
1177 int all_flags;
71b03b42 1178 int warned_bad_reflog;
63049292
JH
1179 struct rev_info *all_revs;
1180 const char *name_for_errormsg;
1181};
ae563542 1182
ff32d342 1183int ref_excluded(struct string_list *ref_excludes, const char *path)
e7b432c5
JH
1184{
1185 struct string_list_item *item;
1186
ff32d342 1187 if (!ref_excludes)
e7b432c5 1188 return 0;
ff32d342 1189 for_each_string_list_item(item, ref_excludes) {
e7b432c5
JH
1190 if (!fnmatch(item->string, path, 0))
1191 return 1;
1192 }
1193 return 0;
1194}
1195
8da19775 1196static int handle_one_ref(const char *path, const unsigned char *sha1, int flag, void *cb_data)
ae563542 1197{
63049292 1198 struct all_refs_cb *cb = cb_data;
e7b432c5
JH
1199 struct object *object;
1200
ff32d342 1201 if (ref_excluded(cb->all_revs->ref_excludes, path))
e7b432c5
JH
1202 return 0;
1203
1204 object = get_reference(cb->all_revs, path, sha1, cb->all_flags);
281eee47 1205 add_rev_cmdline(cb->all_revs, object, path, REV_CMD_REF, cb->all_flags);
26c3177e 1206 add_pending_sha1(cb->all_revs, path, sha1, cb->all_flags);
ae563542
LT
1207 return 0;
1208}
1209
d08bae7e
IL
1210static void init_all_refs_cb(struct all_refs_cb *cb, struct rev_info *revs,
1211 unsigned flags)
1212{
1213 cb->all_revs = revs;
1214 cb->all_flags = flags;
1215}
1216
ff32d342 1217void clear_ref_exclusion(struct string_list **ref_excludes_p)
e7b432c5 1218{
ff32d342
JH
1219 if (*ref_excludes_p) {
1220 string_list_clear(*ref_excludes_p, 0);
1221 free(*ref_excludes_p);
e7b432c5 1222 }
ff32d342 1223 *ref_excludes_p = NULL;
e7b432c5
JH
1224}
1225
ff32d342 1226void add_ref_exclusion(struct string_list **ref_excludes_p, const char *exclude)
e7b432c5 1227{
ff32d342
JH
1228 if (!*ref_excludes_p) {
1229 *ref_excludes_p = xcalloc(1, sizeof(**ref_excludes_p));
1230 (*ref_excludes_p)->strdup_strings = 1;
e7b432c5 1231 }
ff32d342 1232 string_list_append(*ref_excludes_p, exclude);
e7b432c5
JH
1233}
1234
9ef6aeb0
HV
1235static void handle_refs(const char *submodule, struct rev_info *revs, unsigned flags,
1236 int (*for_each)(const char *, each_ref_fn, void *))
ae563542 1237{
63049292 1238 struct all_refs_cb cb;
d08bae7e 1239 init_all_refs_cb(&cb, revs, flags);
9ef6aeb0 1240 for_each(submodule, handle_one_ref, &cb);
63049292
JH
1241}
1242
71b03b42 1243static void handle_one_reflog_commit(unsigned char *sha1, void *cb_data)
63049292
JH
1244{
1245 struct all_refs_cb *cb = cb_data;
71b03b42
SP
1246 if (!is_null_sha1(sha1)) {
1247 struct object *o = parse_object(sha1);
1248 if (o) {
1249 o->flags |= cb->all_flags;
281eee47 1250 /* ??? CMDLINEFLAGS ??? */
71b03b42
SP
1251 add_pending_object(cb->all_revs, o, "");
1252 }
1253 else if (!cb->warned_bad_reflog) {
46efd2d9 1254 warning("reflog of '%s' references pruned commits",
71b03b42
SP
1255 cb->name_for_errormsg);
1256 cb->warned_bad_reflog = 1;
1257 }
63049292 1258 }
71b03b42
SP
1259}
1260
883d60fa
JS
1261static int handle_one_reflog_ent(unsigned char *osha1, unsigned char *nsha1,
1262 const char *email, unsigned long timestamp, int tz,
1263 const char *message, void *cb_data)
71b03b42
SP
1264{
1265 handle_one_reflog_commit(osha1, cb_data);
1266 handle_one_reflog_commit(nsha1, cb_data);
63049292
JH
1267 return 0;
1268}
1269
1270static int handle_one_reflog(const char *path, const unsigned char *sha1, int flag, void *cb_data)
1271{
1272 struct all_refs_cb *cb = cb_data;
71b03b42 1273 cb->warned_bad_reflog = 0;
63049292
JH
1274 cb->name_for_errormsg = path;
1275 for_each_reflog_ent(path, handle_one_reflog_ent, cb_data);
1276 return 0;
1277}
1278
1279static void handle_reflog(struct rev_info *revs, unsigned flags)
1280{
1281 struct all_refs_cb cb;
1282 cb.all_revs = revs;
1283 cb.all_flags = flags;
25b51e2c 1284 for_each_reflog(handle_one_reflog, &cb);
ae563542
LT
1285}
1286
281eee47 1287static int add_parents_only(struct rev_info *revs, const char *arg_, int flags)
ea4a19e1
JH
1288{
1289 unsigned char sha1[20];
1290 struct object *it;
1291 struct commit *commit;
1292 struct commit_list *parents;
281eee47 1293 const char *arg = arg_;
ea4a19e1
JH
1294
1295 if (*arg == '^') {
7f34a46f 1296 flags ^= UNINTERESTING | BOTTOM;
ea4a19e1
JH
1297 arg++;
1298 }
cd74e473 1299 if (get_sha1_committish(arg, sha1))
ea4a19e1
JH
1300 return 0;
1301 while (1) {
1302 it = get_reference(revs, arg, sha1, 0);
cc243c3c
JH
1303 if (!it && revs->ignore_missing)
1304 return 0;
1974632c 1305 if (it->type != OBJ_TAG)
ea4a19e1 1306 break;
9684afd9
MK
1307 if (!((struct tag*)it)->tagged)
1308 return 0;
e702496e 1309 hashcpy(sha1, ((struct tag*)it)->tagged->sha1);
ea4a19e1 1310 }
1974632c 1311 if (it->type != OBJ_COMMIT)
ea4a19e1
JH
1312 return 0;
1313 commit = (struct commit *)it;
1314 for (parents = commit->parents; parents; parents = parents->next) {
1315 it = &parents->item->object;
1316 it->flags |= flags;
281eee47 1317 add_rev_cmdline(revs, it, arg_, REV_CMD_PARENTS_ONLY, flags);
ea4a19e1
JH
1318 add_pending_object(revs, it, arg);
1319 }
1320 return 1;
1321}
1322
db6296a5 1323void init_revisions(struct rev_info *revs, const char *prefix)
8efdc326
FK
1324{
1325 memset(revs, 0, sizeof(*revs));
8e8f9987 1326
6b9c58f4 1327 revs->abbrev = DEFAULT_ABBREV;
8e8f9987 1328 revs->ignore_merges = 1;
9202434c 1329 revs->simplify_history = 1;
8f67f8ae 1330 DIFF_OPT_SET(&revs->pruning, RECURSIVE);
90b19941 1331 DIFF_OPT_SET(&revs->pruning, QUICK);
cd2bdc53
LT
1332 revs->pruning.add_remove = file_add_remove;
1333 revs->pruning.change = file_change;
08f704f2 1334 revs->sort_order = REV_SORT_IN_GRAPH_ORDER;
8efdc326 1335 revs->dense = 1;
db6296a5 1336 revs->prefix = prefix;
8efdc326
FK
1337 revs->max_age = -1;
1338 revs->min_age = -1;
d5db6c9e 1339 revs->skip_count = -1;
8efdc326 1340 revs->max_count = -1;
ad5aeede 1341 revs->max_parents = -1;
8efdc326 1342
cd2bdc53
LT
1343 revs->commit_format = CMIT_FMT_DEFAULT;
1344
918d4e1c
JH
1345 init_grep_defaults();
1346 grep_init(&revs->grep_filter, prefix);
0843acfd 1347 revs->grep_filter.status_only = 1;
0843acfd
JK
1348 revs->grep_filter.regflags = REG_NEWLINE;
1349
cd2bdc53 1350 diff_setup(&revs->diffopt);
c0cb4a06 1351 if (prefix && !revs->diffopt.prefix) {
cd676a51
JH
1352 revs->diffopt.prefix = prefix;
1353 revs->diffopt.prefix_length = strlen(prefix);
1354 }
3a03cf6b
JK
1355
1356 revs->notes_opt.use_default_notes = -1;
8efdc326
FK
1357}
1358
0d2c9d67
RS
1359static void add_pending_commit_list(struct rev_info *revs,
1360 struct commit_list *commit_list,
1361 unsigned int flags)
1362{
1363 while (commit_list) {
1364 struct object *object = &commit_list->item->object;
1365 object->flags |= flags;
1366 add_pending_object(revs, object, sha1_to_hex(object->sha1));
1367 commit_list = commit_list->next;
1368 }
1369}
1370
ae3e5e1e
JH
1371static void prepare_show_merge(struct rev_info *revs)
1372{
1373 struct commit_list *bases;
1374 struct commit *head, *other;
1375 unsigned char sha1[20];
1376 const char **prune = NULL;
1377 int i, prune_num = 1; /* counting terminating NULL */
1378
baf18fc2 1379 if (get_sha1("HEAD", sha1))
ae3e5e1e 1380 die("--merge without HEAD?");
baf18fc2
NTND
1381 head = lookup_commit_or_die(sha1, "HEAD");
1382 if (get_sha1("MERGE_HEAD", sha1))
ae3e5e1e 1383 die("--merge without MERGE_HEAD?");
baf18fc2 1384 other = lookup_commit_or_die(sha1, "MERGE_HEAD");
ae3e5e1e
JH
1385 add_pending_object(revs, &head->object, "HEAD");
1386 add_pending_object(revs, &other->object, "MERGE_HEAD");
1387 bases = get_merge_bases(head, other, 1);
7f34a46f
KB
1388 add_rev_cmdline_list(revs, bases, REV_CMD_MERGE_BASE, UNINTERESTING | BOTTOM);
1389 add_pending_commit_list(revs, bases, UNINTERESTING | BOTTOM);
e82447b1
JH
1390 free_commit_list(bases);
1391 head->object.flags |= SYMMETRIC_LEFT;
ae3e5e1e
JH
1392
1393 if (!active_nr)
1394 read_cache();
1395 for (i = 0; i < active_nr; i++) {
9c5e6c80 1396 const struct cache_entry *ce = active_cache[i];
ae3e5e1e
JH
1397 if (!ce_stage(ce))
1398 continue;
eb9cb55b 1399 if (ce_path_match(ce, &revs->prune_data)) {
ae3e5e1e
JH
1400 prune_num++;
1401 prune = xrealloc(prune, sizeof(*prune) * prune_num);
1402 prune[prune_num-2] = ce->name;
1403 prune[prune_num-1] = NULL;
1404 }
1405 while ((i+1 < active_nr) &&
1406 ce_same_name(ce, active_cache[i+1]))
1407 i++;
1408 }
afe069d1 1409 free_pathspec(&revs->prune_data);
4a2d5ae2 1410 parse_pathspec(&revs->prune_data, PATHSPEC_ALL_MAGIC & ~PATHSPEC_LITERAL,
c6f1b920 1411 PATHSPEC_PREFER_FULL | PATHSPEC_LITERAL_PATH, "", prune);
e82447b1 1412 revs->limited = 1;
ae3e5e1e
JH
1413}
1414
8e676e8b 1415int handle_revision_arg(const char *arg_, struct rev_info *revs, int flags, unsigned revarg_opt)
5d6f0935 1416{
249c8f4a 1417 struct object_context oc;
5d6f0935
JH
1418 char *dotdot;
1419 struct object *object;
1420 unsigned char sha1[20];
1421 int local_flags;
281eee47 1422 const char *arg = arg_;
8e676e8b 1423 int cant_be_filename = revarg_opt & REVARG_CANNOT_BE_FILENAME;
d5f6b1d7 1424 unsigned get_sha1_flags = 0;
5d6f0935 1425
7f34a46f
KB
1426 flags = flags & UNINTERESTING ? flags | BOTTOM : flags & ~BOTTOM;
1427
5d6f0935
JH
1428 dotdot = strstr(arg, "..");
1429 if (dotdot) {
1430 unsigned char from_sha1[20];
1431 const char *next = dotdot + 2;
1432 const char *this = arg;
1433 int symmetric = *next == '.';
7f34a46f 1434 unsigned int flags_exclude = flags ^ (UNINTERESTING | BOTTOM);
003c84f6 1435 static const char head_by_default[] = "HEAD";
281eee47 1436 unsigned int a_flags;
5d6f0935
JH
1437
1438 *dotdot = 0;
1439 next += symmetric;
1440
1441 if (!*next)
003c84f6 1442 next = head_by_default;
5d6f0935 1443 if (dotdot == arg)
003c84f6
JH
1444 this = head_by_default;
1445 if (this == head_by_default && next == head_by_default &&
1446 !symmetric) {
1447 /*
1448 * Just ".."? That is not a range but the
1449 * pathspec for the parent directory.
1450 */
1451 if (!cant_be_filename) {
1452 *dotdot = '.';
1453 return -1;
1454 }
1455 }
cd74e473
JH
1456 if (!get_sha1_committish(this, from_sha1) &&
1457 !get_sha1_committish(next, sha1)) {
895c5ba3 1458 struct object *a_obj, *b_obj;
5d6f0935
JH
1459
1460 if (!cant_be_filename) {
1461 *dotdot = '.';
1462 verify_non_filename(revs->prefix, arg);
1463 }
1464
895c5ba3
JH
1465 a_obj = parse_object(from_sha1);
1466 b_obj = parse_object(sha1);
1467 if (!a_obj || !b_obj) {
1468 missing:
1469 if (revs->ignore_missing)
1470 return 0;
1471 die(symmetric
1472 ? "Invalid symmetric difference expression %s"
1473 : "Invalid revision range %s", arg);
1474 }
1475
1476 if (!symmetric) {
1477 /* just A..B */
1478 a_flags = flags_exclude;
1479 } else {
1480 /* A...B -- find merge bases between the two */
1481 struct commit *a, *b;
1482 struct commit_list *exclude;
1483
1484 a = (a_obj->type == OBJ_COMMIT
1485 ? (struct commit *)a_obj
1486 : lookup_commit_reference(a_obj->sha1));
1487 b = (b_obj->type == OBJ_COMMIT
1488 ? (struct commit *)b_obj
1489 : lookup_commit_reference(b_obj->sha1));
1490 if (!a || !b)
1491 goto missing;
5d6f0935 1492 exclude = get_merge_bases(a, b, 1);
a765499a
KB
1493 add_rev_cmdline_list(revs, exclude,
1494 REV_CMD_MERGE_BASE,
1495 flags_exclude);
5d6f0935
JH
1496 add_pending_commit_list(revs, exclude,
1497 flags_exclude);
1498 free_commit_list(exclude);
895c5ba3 1499
281eee47 1500 a_flags = flags | SYMMETRIC_LEFT;
895c5ba3
JH
1501 }
1502
1503 a_obj->flags |= a_flags;
1504 b_obj->flags |= flags;
1505 add_rev_cmdline(revs, a_obj, this,
281eee47 1506 REV_CMD_LEFT, a_flags);
895c5ba3 1507 add_rev_cmdline(revs, b_obj, next,
281eee47 1508 REV_CMD_RIGHT, flags);
895c5ba3
JH
1509 add_pending_object(revs, a_obj, this);
1510 add_pending_object(revs, b_obj, next);
5d6f0935
JH
1511 return 0;
1512 }
1513 *dotdot = '.';
1514 }
1515 dotdot = strstr(arg, "^@");
1516 if (dotdot && !dotdot[2]) {
1517 *dotdot = 0;
1518 if (add_parents_only(revs, arg, flags))
1519 return 0;
1520 *dotdot = '^';
1521 }
62476c8e
JH
1522 dotdot = strstr(arg, "^!");
1523 if (dotdot && !dotdot[2]) {
1524 *dotdot = 0;
7f34a46f 1525 if (!add_parents_only(revs, arg, flags ^ (UNINTERESTING | BOTTOM)))
62476c8e
JH
1526 *dotdot = '^';
1527 }
1528
5d6f0935
JH
1529 local_flags = 0;
1530 if (*arg == '^') {
7f34a46f 1531 local_flags = UNINTERESTING | BOTTOM;
5d6f0935
JH
1532 arg++;
1533 }
d5f6b1d7
JH
1534
1535 if (revarg_opt & REVARG_COMMITTISH)
1536 get_sha1_flags = GET_SHA1_COMMITTISH;
1537
1538 if (get_sha1_with_context(arg, get_sha1_flags, sha1, &oc))
cc243c3c 1539 return revs->ignore_missing ? 0 : -1;
5d6f0935
JH
1540 if (!cant_be_filename)
1541 verify_non_filename(revs->prefix, arg);
1542 object = get_reference(revs, arg, sha1, flags ^ local_flags);
281eee47 1543 add_rev_cmdline(revs, object, arg_, REV_CMD_REV, flags ^ local_flags);
249c8f4a 1544 add_pending_object_with_mode(revs, object, arg, oc.mode);
5d6f0935
JH
1545 return 0;
1546}
1547
4da5af31
JH
1548struct cmdline_pathspec {
1549 int alloc;
1550 int nr;
1551 const char **path;
1552};
1fc561d1 1553
4da5af31
JH
1554static void append_prune_data(struct cmdline_pathspec *prune, const char **av)
1555{
1556 while (*av) {
9e57ac55 1557 ALLOC_GROW(prune->path, prune->nr + 1, prune->alloc);
4da5af31
JH
1558 prune->path[prune->nr++] = *(av++);
1559 }
1560}
60da8b15 1561
4da5af31
JH
1562static void read_pathspec_from_stdin(struct rev_info *revs, struct strbuf *sb,
1563 struct cmdline_pathspec *prune)
1564{
60da8b15
JH
1565 while (strbuf_getwholeline(sb, stdin, '\n') != EOF) {
1566 int len = sb->len;
1567 if (len && sb->buf[len - 1] == '\n')
1568 sb->buf[--len] = '\0';
9e57ac55 1569 ALLOC_GROW(prune->path, prune->nr + 1, prune->alloc);
4da5af31 1570 prune->path[prune->nr++] = xstrdup(sb->buf);
60da8b15 1571 }
60da8b15
JH
1572}
1573
4da5af31
JH
1574static void read_revisions_from_stdin(struct rev_info *revs,
1575 struct cmdline_pathspec *prune)
1fc561d1 1576{
63d564b3 1577 struct strbuf sb;
60da8b15 1578 int seen_dashdash = 0;
1fc561d1 1579
63d564b3
JH
1580 strbuf_init(&sb, 1000);
1581 while (strbuf_getwholeline(&sb, stdin, '\n') != EOF) {
1582 int len = sb.len;
1583 if (len && sb.buf[len - 1] == '\n')
1584 sb.buf[--len] = '\0';
1fc561d1
AB
1585 if (!len)
1586 break;
60da8b15
JH
1587 if (sb.buf[0] == '-') {
1588 if (len == 2 && sb.buf[1] == '-') {
1589 seen_dashdash = 1;
1590 break;
1591 }
1fc561d1 1592 die("options not supported in --stdin mode");
60da8b15 1593 }
31faeb20 1594 if (handle_revision_arg(sb.buf, revs, 0,
70d26c6e 1595 REVARG_CANNOT_BE_FILENAME))
63d564b3 1596 die("bad revision '%s'", sb.buf);
1fc561d1 1597 }
60da8b15
JH
1598 if (seen_dashdash)
1599 read_pathspec_from_stdin(revs, &sb, prune);
63d564b3 1600 strbuf_release(&sb);
1fc561d1
AB
1601}
1602
2d10c555 1603static void add_grep(struct rev_info *revs, const char *ptn, enum grep_pat_token what)
bd95fcd3 1604{
0843acfd 1605 append_grep_pattern(&revs->grep_filter, ptn, "command line", 0, what);
2d10c555
JH
1606}
1607
a4d7d2c6 1608static void add_header_grep(struct rev_info *revs, enum grep_header_field field, const char *pattern)
2d10c555 1609{
a4d7d2c6 1610 append_header_grep_pattern(&revs->grep_filter, field, pattern);
bd95fcd3
JH
1611}
1612
1613static void add_message_grep(struct rev_info *revs, const char *pattern)
1614{
2d10c555 1615 add_grep(revs, pattern, GREP_PATTERN_BODY);
bd95fcd3
JH
1616}
1617
6b61ec05
PH
1618static int handle_revision_opt(struct rev_info *revs, int argc, const char **argv,
1619 int *unkc, const char **unkv)
02e54220
PH
1620{
1621 const char *arg = argv[0];
7d7b86f7
MM
1622 const char *optarg;
1623 int argcount;
02e54220
PH
1624
1625 /* pseudo revision arguments */
1626 if (!strcmp(arg, "--all") || !strcmp(arg, "--branches") ||
1627 !strcmp(arg, "--tags") || !strcmp(arg, "--remotes") ||
1628 !strcmp(arg, "--reflog") || !strcmp(arg, "--not") ||
ad3f9a71 1629 !strcmp(arg, "--no-walk") || !strcmp(arg, "--do-walk") ||
59556548
CC
1630 !strcmp(arg, "--bisect") || starts_with(arg, "--glob=") ||
1631 starts_with(arg, "--branches=") || starts_with(arg, "--tags=") ||
1632 starts_with(arg, "--remotes=") || starts_with(arg, "--no-walk="))
02e54220
PH
1633 {
1634 unkv[(*unkc)++] = arg;
0fe8c138 1635 return 1;
02e54220
PH
1636 }
1637
7d7b86f7
MM
1638 if ((argcount = parse_long_opt("max-count", argv, &optarg))) {
1639 revs->max_count = atoi(optarg);
5853caec 1640 revs->no_walk = 0;
7d7b86f7
MM
1641 return argcount;
1642 } else if ((argcount = parse_long_opt("skip", argv, &optarg))) {
1643 revs->skip_count = atoi(optarg);
1644 return argcount;
02e54220
PH
1645 } else if ((*arg == '-') && isdigit(arg[1])) {
1646 /* accept -<digit>, like traditional "head" */
1647 revs->max_count = atoi(arg + 1);
5853caec 1648 revs->no_walk = 0;
02e54220
PH
1649 } else if (!strcmp(arg, "-n")) {
1650 if (argc <= 1)
1651 return error("-n requires an argument");
1652 revs->max_count = atoi(argv[1]);
5853caec 1653 revs->no_walk = 0;
02e54220 1654 return 2;
59556548 1655 } else if (starts_with(arg, "-n")) {
02e54220 1656 revs->max_count = atoi(arg + 2);
5853caec 1657 revs->no_walk = 0;
7d7b86f7
MM
1658 } else if ((argcount = parse_long_opt("max-age", argv, &optarg))) {
1659 revs->max_age = atoi(optarg);
1660 return argcount;
1661 } else if ((argcount = parse_long_opt("since", argv, &optarg))) {
1662 revs->max_age = approxidate(optarg);
1663 return argcount;
1664 } else if ((argcount = parse_long_opt("after", argv, &optarg))) {
1665 revs->max_age = approxidate(optarg);
1666 return argcount;
1667 } else if ((argcount = parse_long_opt("min-age", argv, &optarg))) {
1668 revs->min_age = atoi(optarg);
1669 return argcount;
1670 } else if ((argcount = parse_long_opt("before", argv, &optarg))) {
1671 revs->min_age = approxidate(optarg);
1672 return argcount;
1673 } else if ((argcount = parse_long_opt("until", argv, &optarg))) {
1674 revs->min_age = approxidate(optarg);
1675 return argcount;
02e54220
PH
1676 } else if (!strcmp(arg, "--first-parent")) {
1677 revs->first_parent_only = 1;
ebdc94f3
JH
1678 } else if (!strcmp(arg, "--ancestry-path")) {
1679 revs->ancestry_path = 1;
cb7529e1 1680 revs->simplify_history = 0;
ebdc94f3 1681 revs->limited = 1;
02e54220
PH
1682 } else if (!strcmp(arg, "-g") || !strcmp(arg, "--walk-reflogs")) {
1683 init_reflog_walk(&revs->reflog_info);
1684 } else if (!strcmp(arg, "--default")) {
1685 if (argc <= 1)
1686 return error("bad --default argument");
1687 revs->def = argv[1];
1688 return 2;
1689 } else if (!strcmp(arg, "--merge")) {
1690 revs->show_merge = 1;
1691 } else if (!strcmp(arg, "--topo-order")) {
08f704f2 1692 revs->sort_order = REV_SORT_IN_GRAPH_ORDER;
02e54220 1693 revs->topo_order = 1;
6546b593
JH
1694 } else if (!strcmp(arg, "--simplify-merges")) {
1695 revs->simplify_merges = 1;
a52f0071 1696 revs->topo_order = 1;
6546b593
JH
1697 revs->rewrite_parents = 1;
1698 revs->simplify_history = 0;
1699 revs->limited = 1;
78892e32
LT
1700 } else if (!strcmp(arg, "--simplify-by-decoration")) {
1701 revs->simplify_merges = 1;
a52f0071 1702 revs->topo_order = 1;
78892e32
LT
1703 revs->rewrite_parents = 1;
1704 revs->simplify_history = 0;
1705 revs->simplify_by_decoration = 1;
1706 revs->limited = 1;
1707 revs->prune = 1;
33e7018c 1708 load_ref_decorations(DECORATE_SHORT_REFS);
02e54220 1709 } else if (!strcmp(arg, "--date-order")) {
08f704f2 1710 revs->sort_order = REV_SORT_BY_COMMIT_DATE;
02e54220 1711 revs->topo_order = 1;
81c6b38b
JH
1712 } else if (!strcmp(arg, "--author-date-order")) {
1713 revs->sort_order = REV_SORT_BY_AUTHOR_DATE;
02e54220 1714 revs->topo_order = 1;
59556548 1715 } else if (starts_with(arg, "--early-output")) {
02e54220
PH
1716 int count = 100;
1717 switch (arg[14]) {
1718 case '=':
1719 count = atoi(arg+15);
1720 /* Fallthrough */
1721 case 0:
1722 revs->topo_order = 1;
1723 revs->early_output = count;
1724 }
1725 } else if (!strcmp(arg, "--parents")) {
1726 revs->rewrite_parents = 1;
1727 revs->print_parents = 1;
1728 } else if (!strcmp(arg, "--dense")) {
1729 revs->dense = 1;
1730 } else if (!strcmp(arg, "--sparse")) {
1731 revs->dense = 0;
1732 } else if (!strcmp(arg, "--show-all")) {
1733 revs->show_all = 1;
1734 } else if (!strcmp(arg, "--remove-empty")) {
1735 revs->remove_empty_trees = 1;
b8e8db28 1736 } else if (!strcmp(arg, "--merges")) {
ad5aeede 1737 revs->min_parents = 2;
02e54220 1738 } else if (!strcmp(arg, "--no-merges")) {
ad5aeede 1739 revs->max_parents = 1;
59556548 1740 } else if (starts_with(arg, "--min-parents=")) {
ad5aeede 1741 revs->min_parents = atoi(arg+14);
59556548 1742 } else if (starts_with(arg, "--no-min-parents")) {
ad5aeede 1743 revs->min_parents = 0;
59556548 1744 } else if (starts_with(arg, "--max-parents=")) {
ad5aeede 1745 revs->max_parents = atoi(arg+14);
59556548 1746 } else if (starts_with(arg, "--no-max-parents")) {
ad5aeede 1747 revs->max_parents = -1;
02e54220
PH
1748 } else if (!strcmp(arg, "--boundary")) {
1749 revs->boundary = 1;
1750 } else if (!strcmp(arg, "--left-right")) {
1751 revs->left_right = 1;
60adf7d7 1752 } else if (!strcmp(arg, "--left-only")) {
24852d91 1753 if (revs->right_only)
94f605ec
MG
1754 die("--left-only is incompatible with --right-only"
1755 " or --cherry");
60adf7d7
MG
1756 revs->left_only = 1;
1757 } else if (!strcmp(arg, "--right-only")) {
24852d91
JH
1758 if (revs->left_only)
1759 die("--right-only is incompatible with --left-only");
60adf7d7 1760 revs->right_only = 1;
94f605ec
MG
1761 } else if (!strcmp(arg, "--cherry")) {
1762 if (revs->left_only)
1763 die("--cherry is incompatible with --left-only");
1764 revs->cherry_mark = 1;
1765 revs->right_only = 1;
ad5aeede 1766 revs->max_parents = 1;
94f605ec 1767 revs->limited = 1;
f69c5018
TR
1768 } else if (!strcmp(arg, "--count")) {
1769 revs->count = 1;
adbbb31e
MG
1770 } else if (!strcmp(arg, "--cherry-mark")) {
1771 if (revs->cherry_pick)
1772 die("--cherry-mark is incompatible with --cherry-pick");
1773 revs->cherry_mark = 1;
1774 revs->limited = 1; /* needs limit_list() */
02e54220 1775 } else if (!strcmp(arg, "--cherry-pick")) {
adbbb31e
MG
1776 if (revs->cherry_mark)
1777 die("--cherry-pick is incompatible with --cherry-mark");
02e54220
PH
1778 revs->cherry_pick = 1;
1779 revs->limited = 1;
1780 } else if (!strcmp(arg, "--objects")) {
1781 revs->tag_objects = 1;
1782 revs->tree_objects = 1;
1783 revs->blob_objects = 1;
1784 } else if (!strcmp(arg, "--objects-edge")) {
1785 revs->tag_objects = 1;
1786 revs->tree_objects = 1;
1787 revs->blob_objects = 1;
1788 revs->edge_hint = 1;
5a48d240
JH
1789 } else if (!strcmp(arg, "--verify-objects")) {
1790 revs->tag_objects = 1;
1791 revs->tree_objects = 1;
1792 revs->blob_objects = 1;
1793 revs->verify_objects = 1;
02e54220
PH
1794 } else if (!strcmp(arg, "--unpacked")) {
1795 revs->unpacked = 1;
59556548 1796 } else if (starts_with(arg, "--unpacked=")) {
03a9683d 1797 die("--unpacked=<packfile> no longer supported.");
02e54220
PH
1798 } else if (!strcmp(arg, "-r")) {
1799 revs->diff = 1;
1800 DIFF_OPT_SET(&revs->diffopt, RECURSIVE);
1801 } else if (!strcmp(arg, "-t")) {
1802 revs->diff = 1;
1803 DIFF_OPT_SET(&revs->diffopt, RECURSIVE);
1804 DIFF_OPT_SET(&revs->diffopt, TREE_IN_RECURSIVE);
1805 } else if (!strcmp(arg, "-m")) {
1806 revs->ignore_merges = 0;
1807 } else if (!strcmp(arg, "-c")) {
1808 revs->diff = 1;
1809 revs->dense_combined_merges = 0;
1810 revs->combine_merges = 1;
1811 } else if (!strcmp(arg, "--cc")) {
1812 revs->diff = 1;
1813 revs->dense_combined_merges = 1;
1814 revs->combine_merges = 1;
1815 } else if (!strcmp(arg, "-v")) {
1816 revs->verbose_header = 1;
1817 } else if (!strcmp(arg, "--pretty")) {
1818 revs->verbose_header = 1;
66b2ed09 1819 revs->pretty_given = 1;
02e54220 1820 get_commit_format(arg+8, revs);
59556548 1821 } else if (starts_with(arg, "--pretty=") || starts_with(arg, "--format=")) {
7d7b86f7
MM
1822 /*
1823 * Detached form ("--pretty X" as opposed to "--pretty=X")
1824 * not allowed, since the argument is optional.
1825 */
02e54220 1826 revs->verbose_header = 1;
66b2ed09 1827 revs->pretty_given = 1;
02e54220 1828 get_commit_format(arg+9, revs);
7249e912 1829 } else if (!strcmp(arg, "--show-notes") || !strcmp(arg, "--notes")) {
66b2ed09
JH
1830 revs->show_notes = 1;
1831 revs->show_notes_given = 1;
3a03cf6b 1832 revs->notes_opt.use_default_notes = 1;
0c37f1fc
JH
1833 } else if (!strcmp(arg, "--show-signature")) {
1834 revs->show_signature = 1;
59556548
CC
1835 } else if (starts_with(arg, "--show-notes=") ||
1836 starts_with(arg, "--notes=")) {
894a9d33
TR
1837 struct strbuf buf = STRBUF_INIT;
1838 revs->show_notes = 1;
1839 revs->show_notes_given = 1;
59556548 1840 if (starts_with(arg, "--show-notes")) {
7249e912
JK
1841 if (revs->notes_opt.use_default_notes < 0)
1842 revs->notes_opt.use_default_notes = 1;
1843 strbuf_addstr(&buf, arg+13);
1844 }
1845 else
1846 strbuf_addstr(&buf, arg+8);
c063f0a9 1847 expand_notes_ref(&buf);
304cc11c 1848 string_list_append(&revs->notes_opt.extra_notes_refs,
1d2f80fa 1849 strbuf_detach(&buf, NULL));
66b2ed09
JH
1850 } else if (!strcmp(arg, "--no-notes")) {
1851 revs->show_notes = 0;
1852 revs->show_notes_given = 1;
92e0d425
JK
1853 revs->notes_opt.use_default_notes = -1;
1854 /* we have been strdup'ing ourselves, so trick
1855 * string_list into free()ing strings */
1856 revs->notes_opt.extra_notes_refs.strdup_strings = 1;
1857 string_list_clear(&revs->notes_opt.extra_notes_refs, 0);
1858 revs->notes_opt.extra_notes_refs.strdup_strings = 0;
894a9d33
TR
1859 } else if (!strcmp(arg, "--standard-notes")) {
1860 revs->show_notes_given = 1;
3a03cf6b 1861 revs->notes_opt.use_default_notes = 1;
894a9d33 1862 } else if (!strcmp(arg, "--no-standard-notes")) {
3a03cf6b 1863 revs->notes_opt.use_default_notes = 0;
de84accc
NS
1864 } else if (!strcmp(arg, "--oneline")) {
1865 revs->verbose_header = 1;
1866 get_commit_format("oneline", revs);
7dccadf3 1867 revs->pretty_given = 1;
de84accc 1868 revs->abbrev_commit = 1;
02e54220
PH
1869 } else if (!strcmp(arg, "--graph")) {
1870 revs->topo_order = 1;
1871 revs->rewrite_parents = 1;
1872 revs->graph = graph_init(revs);
1873 } else if (!strcmp(arg, "--root")) {
1874 revs->show_root_diff = 1;
1875 } else if (!strcmp(arg, "--no-commit-id")) {
1876 revs->no_commit_id = 1;
1877 } else if (!strcmp(arg, "--always")) {
1878 revs->always_show_header = 1;
1879 } else if (!strcmp(arg, "--no-abbrev")) {
1880 revs->abbrev = 0;
1881 } else if (!strcmp(arg, "--abbrev")) {
1882 revs->abbrev = DEFAULT_ABBREV;
59556548 1883 } else if (starts_with(arg, "--abbrev=")) {
02e54220
PH
1884 revs->abbrev = strtoul(arg + 9, NULL, 10);
1885 if (revs->abbrev < MINIMUM_ABBREV)
1886 revs->abbrev = MINIMUM_ABBREV;
1887 else if (revs->abbrev > 40)
1888 revs->abbrev = 40;
1889 } else if (!strcmp(arg, "--abbrev-commit")) {
1890 revs->abbrev_commit = 1;
0c47695a
JS
1891 revs->abbrev_commit_given = 1;
1892 } else if (!strcmp(arg, "--no-abbrev-commit")) {
1893 revs->abbrev_commit = 0;
02e54220
PH
1894 } else if (!strcmp(arg, "--full-diff")) {
1895 revs->diff = 1;
1896 revs->full_diff = 1;
1897 } else if (!strcmp(arg, "--full-history")) {
1898 revs->simplify_history = 0;
1899 } else if (!strcmp(arg, "--relative-date")) {
1900 revs->date_mode = DATE_RELATIVE;
f4ea32f0 1901 revs->date_mode_explicit = 1;
7d7b86f7
MM
1902 } else if ((argcount = parse_long_opt("date", argv, &optarg))) {
1903 revs->date_mode = parse_date_format(optarg);
f4ea32f0 1904 revs->date_mode_explicit = 1;
7d7b86f7 1905 return argcount;
02e54220
PH
1906 } else if (!strcmp(arg, "--log-size")) {
1907 revs->show_log_size = 1;
1908 }
1909 /*
1910 * Grepping the commit log
1911 */
7d7b86f7
MM
1912 else if ((argcount = parse_long_opt("author", argv, &optarg))) {
1913 add_header_grep(revs, GREP_HEADER_AUTHOR, optarg);
1914 return argcount;
1915 } else if ((argcount = parse_long_opt("committer", argv, &optarg))) {
1916 add_header_grep(revs, GREP_HEADER_COMMITTER, optarg);
1917 return argcount;
72fd13f7
NTND
1918 } else if ((argcount = parse_long_opt("grep-reflog", argv, &optarg))) {
1919 add_header_grep(revs, GREP_HEADER_REFLOG, optarg);
1920 return argcount;
7d7b86f7
MM
1921 } else if ((argcount = parse_long_opt("grep", argv, &optarg))) {
1922 add_message_grep(revs, optarg);
1923 return argcount;
17bf35a3
JH
1924 } else if (!strcmp(arg, "--grep-debug")) {
1925 revs->grep_filter.debug = 1;
727b6fc3
JH
1926 } else if (!strcmp(arg, "--basic-regexp")) {
1927 grep_set_pattern_type_option(GREP_PATTERN_TYPE_BRE, &revs->grep_filter);
02e54220 1928 } else if (!strcmp(arg, "--extended-regexp") || !strcmp(arg, "-E")) {
34a4ae55 1929 grep_set_pattern_type_option(GREP_PATTERN_TYPE_ERE, &revs->grep_filter);
02e54220 1930 } else if (!strcmp(arg, "--regexp-ignore-case") || !strcmp(arg, "-i")) {
0843acfd 1931 revs->grep_filter.regflags |= REG_ICASE;
accccde4 1932 DIFF_OPT_SET(&revs->diffopt, PICKAXE_IGNORE_CASE);
02e54220 1933 } else if (!strcmp(arg, "--fixed-strings") || !strcmp(arg, "-F")) {
34a4ae55 1934 grep_set_pattern_type_option(GREP_PATTERN_TYPE_FIXED, &revs->grep_filter);
727b6fc3
JH
1935 } else if (!strcmp(arg, "--perl-regexp")) {
1936 grep_set_pattern_type_option(GREP_PATTERN_TYPE_PCRE, &revs->grep_filter);
02e54220 1937 } else if (!strcmp(arg, "--all-match")) {
0843acfd 1938 revs->grep_filter.all_match = 1;
7d7b86f7
MM
1939 } else if ((argcount = parse_long_opt("encoding", argv, &optarg))) {
1940 if (strcmp(optarg, "none"))
1941 git_log_output_encoding = xstrdup(optarg);
02e54220
PH
1942 else
1943 git_log_output_encoding = "";
7d7b86f7 1944 return argcount;
02e54220
PH
1945 } else if (!strcmp(arg, "--reverse")) {
1946 revs->reverse ^= 1;
1947 } else if (!strcmp(arg, "--children")) {
1948 revs->children.name = "children";
1949 revs->limited = 1;
cc243c3c
JH
1950 } else if (!strcmp(arg, "--ignore-missing")) {
1951 revs->ignore_missing = 1;
02e54220
PH
1952 } else {
1953 int opts = diff_opt_parse(&revs->diffopt, argv, argc);
1954 if (!opts)
1955 unkv[(*unkc)++] = arg;
1956 return opts;
1957 }
1958
1959 return 1;
1960}
1961
6b61ec05
PH
1962void parse_revision_opt(struct rev_info *revs, struct parse_opt_ctx_t *ctx,
1963 const struct option *options,
1964 const char * const usagestr[])
1965{
1966 int n = handle_revision_opt(revs, ctx->argc, ctx->argv,
1967 &ctx->cpidx, ctx->out);
1968 if (n <= 0) {
1969 error("unknown option `%s'", ctx->argv[0]);
1970 usage_with_options(usagestr, options);
1971 }
1972 ctx->argv += n;
1973 ctx->argc -= n;
1974}
1975
9ef6aeb0 1976static int for_each_bad_bisect_ref(const char *submodule, each_ref_fn fn, void *cb_data)
ad3f9a71 1977{
9ef6aeb0 1978 return for_each_ref_in_submodule(submodule, "refs/bisect/bad", fn, cb_data);
ad3f9a71
LT
1979}
1980
9ef6aeb0 1981static int for_each_good_bisect_ref(const char *submodule, each_ref_fn fn, void *cb_data)
ad3f9a71 1982{
9ef6aeb0 1983 return for_each_ref_in_submodule(submodule, "refs/bisect/good", fn, cb_data);
ad3f9a71
LT
1984}
1985
f6aca0dc
JN
1986static int handle_revision_pseudo_opt(const char *submodule,
1987 struct rev_info *revs,
1988 int argc, const char **argv, int *flags)
1989{
1990 const char *arg = argv[0];
1991 const char *optarg;
1992 int argcount;
1993
0fc63ec4
JN
1994 /*
1995 * NOTE!
1996 *
1997 * Commands like "git shortlog" will not accept the options below
1998 * unless parse_revision_opt queues them (as opposed to erroring
1999 * out).
2000 *
2001 * When implementing your new pseudo-option, remember to
2002 * register it in the list at the top of handle_revision_opt.
2003 */
f6aca0dc
JN
2004 if (!strcmp(arg, "--all")) {
2005 handle_refs(submodule, revs, *flags, for_each_ref_submodule);
2006 handle_refs(submodule, revs, *flags, head_ref_submodule);
ff32d342 2007 clear_ref_exclusion(&revs->ref_excludes);
f6aca0dc
JN
2008 } else if (!strcmp(arg, "--branches")) {
2009 handle_refs(submodule, revs, *flags, for_each_branch_ref_submodule);
ff32d342 2010 clear_ref_exclusion(&revs->ref_excludes);
f6aca0dc
JN
2011 } else if (!strcmp(arg, "--bisect")) {
2012 handle_refs(submodule, revs, *flags, for_each_bad_bisect_ref);
7f34a46f 2013 handle_refs(submodule, revs, *flags ^ (UNINTERESTING | BOTTOM), for_each_good_bisect_ref);
f6aca0dc
JN
2014 revs->bisect = 1;
2015 } else if (!strcmp(arg, "--tags")) {
2016 handle_refs(submodule, revs, *flags, for_each_tag_ref_submodule);
ff32d342 2017 clear_ref_exclusion(&revs->ref_excludes);
f6aca0dc
JN
2018 } else if (!strcmp(arg, "--remotes")) {
2019 handle_refs(submodule, revs, *flags, for_each_remote_ref_submodule);
ff32d342 2020 clear_ref_exclusion(&revs->ref_excludes);
f6aca0dc
JN
2021 } else if ((argcount = parse_long_opt("glob", argv, &optarg))) {
2022 struct all_refs_cb cb;
2023 init_all_refs_cb(&cb, revs, *flags);
2024 for_each_glob_ref(handle_one_ref, optarg, &cb);
ff32d342 2025 clear_ref_exclusion(&revs->ref_excludes);
e7b432c5
JH
2026 return argcount;
2027 } else if ((argcount = parse_long_opt("exclude", argv, &optarg))) {
ff32d342 2028 add_ref_exclusion(&revs->ref_excludes, optarg);
f6aca0dc 2029 return argcount;
59556548 2030 } else if (starts_with(arg, "--branches=")) {
f6aca0dc
JN
2031 struct all_refs_cb cb;
2032 init_all_refs_cb(&cb, revs, *flags);
2033 for_each_glob_ref_in(handle_one_ref, arg + 11, "refs/heads/", &cb);
ff32d342 2034 clear_ref_exclusion(&revs->ref_excludes);
59556548 2035 } else if (starts_with(arg, "--tags=")) {
f6aca0dc
JN
2036 struct all_refs_cb cb;
2037 init_all_refs_cb(&cb, revs, *flags);
2038 for_each_glob_ref_in(handle_one_ref, arg + 7, "refs/tags/", &cb);
ff32d342 2039 clear_ref_exclusion(&revs->ref_excludes);
59556548 2040 } else if (starts_with(arg, "--remotes=")) {
f6aca0dc
JN
2041 struct all_refs_cb cb;
2042 init_all_refs_cb(&cb, revs, *flags);
2043 for_each_glob_ref_in(handle_one_ref, arg + 10, "refs/remotes/", &cb);
ff32d342 2044 clear_ref_exclusion(&revs->ref_excludes);
f6aca0dc
JN
2045 } else if (!strcmp(arg, "--reflog")) {
2046 handle_reflog(revs, *flags);
2047 } else if (!strcmp(arg, "--not")) {
7f34a46f 2048 *flags ^= UNINTERESTING | BOTTOM;
f6aca0dc 2049 } else if (!strcmp(arg, "--no-walk")) {
ca92e59e 2050 revs->no_walk = REVISION_WALK_NO_WALK_SORTED;
59556548 2051 } else if (starts_with(arg, "--no-walk=")) {
ca92e59e
MZ
2052 /*
2053 * Detached form ("--no-walk X" as opposed to "--no-walk=X")
2054 * not allowed, since the argument is optional.
2055 */
2056 if (!strcmp(arg + 10, "sorted"))
2057 revs->no_walk = REVISION_WALK_NO_WALK_SORTED;
2058 else if (!strcmp(arg + 10, "unsorted"))
2059 revs->no_walk = REVISION_WALK_NO_WALK_UNSORTED;
2060 else
2061 return error("invalid argument to --no-walk");
f6aca0dc
JN
2062 } else if (!strcmp(arg, "--do-walk")) {
2063 revs->no_walk = 0;
2064 } else {
2065 return 0;
2066 }
2067
2068 return 1;
2069}
2070
ae563542
LT
2071/*
2072 * Parse revision information, filling in the "rev_info" structure,
2073 * and removing the used arguments from the argument list.
2074 *
765ac8ec
LT
2075 * Returns the number of arguments left that weren't recognized
2076 * (which are also moved to the head of the argument list)
ae563542 2077 */
32962c9b 2078int setup_revisions(int argc, const char **argv, struct rev_info *revs, struct setup_revision_opt *opt)
ae563542 2079{
8e676e8b 2080 int i, flags, left, seen_dashdash, read_from_stdin, got_rev_arg = 0, revarg_opt;
4da5af31 2081 struct cmdline_pathspec prune_data;
9ef6aeb0
HV
2082 const char *submodule = NULL;
2083
4da5af31 2084 memset(&prune_data, 0, sizeof(prune_data));
9ef6aeb0
HV
2085 if (opt)
2086 submodule = opt->submodule;
ae563542 2087
ae563542 2088 /* First, search for "--" */
6d5b93f2 2089 if (opt && opt->assume_dashdash) {
ae563542 2090 seen_dashdash = 1;
6d5b93f2
CB
2091 } else {
2092 seen_dashdash = 0;
2093 for (i = 1; i < argc; i++) {
2094 const char *arg = argv[i];
2095 if (strcmp(arg, "--"))
2096 continue;
2097 argv[i] = NULL;
2098 argc = i;
2099 if (argv[i + 1])
2100 append_prune_data(&prune_data, argv + i + 1);
2101 seen_dashdash = 1;
2102 break;
2103 }
ae563542
LT
2104 }
2105
02e54220
PH
2106 /* Second, deal with arguments and options */
2107 flags = 0;
d5f6b1d7
JH
2108 revarg_opt = opt ? opt->revarg_opt : 0;
2109 if (seen_dashdash)
2110 revarg_opt |= REVARG_CANNOT_BE_FILENAME;
8b3dce56 2111 read_from_stdin = 0;
02e54220 2112 for (left = i = 1; i < argc; i++) {
ae563542 2113 const char *arg = argv[i];
ae563542 2114 if (*arg == '-') {
cd2bdc53 2115 int opts;
02e54220 2116
f6aca0dc
JN
2117 opts = handle_revision_pseudo_opt(submodule,
2118 revs, argc - i, argv + i,
2119 &flags);
2120 if (opts > 0) {
2121 i += opts - 1;
8e64006e
JS
2122 continue;
2123 }
f6aca0dc 2124
8b3dce56
JH
2125 if (!strcmp(arg, "--stdin")) {
2126 if (revs->disable_stdin) {
2127 argv[left++] = arg;
2128 continue;
2129 }
2130 if (read_from_stdin++)
2131 die("--stdin given twice?");
60da8b15 2132 read_revisions_from_stdin(revs, &prune_data);
8b3dce56
JH
2133 continue;
2134 }
2d10c555 2135
02e54220 2136 opts = handle_revision_opt(revs, argc - i, argv + i, &left, argv);
cd2bdc53 2137 if (opts > 0) {
cd2bdc53
LT
2138 i += opts - 1;
2139 continue;
2140 }
02e54220
PH
2141 if (opts < 0)
2142 exit(128);
ae563542
LT
2143 continue;
2144 }
ae563542 2145
8e676e8b
JH
2146
2147 if (handle_revision_arg(arg, revs, flags, revarg_opt)) {
5d6f0935
JH
2148 int j;
2149 if (seen_dashdash || *arg == '^')
ae563542
LT
2150 die("bad revision '%s'", arg);
2151
ea92f41f
JH
2152 /* If we didn't have a "--":
2153 * (1) all filenames must exist;
2154 * (2) all rev-args must not be interpretable
2155 * as a valid filename.
2156 * but the latter we have checked in the main loop.
2157 */
e23d0b4a 2158 for (j = i; j < argc; j++)
023e37c3 2159 verify_filename(revs->prefix, argv[j], j == i);
e23d0b4a 2160
60da8b15 2161 append_prune_data(&prune_data, argv + i);
ae563542
LT
2162 break;
2163 }
8fcaca3f
DO
2164 else
2165 got_rev_arg = 1;
ae563542 2166 }
5d6f0935 2167
4da5af31 2168 if (prune_data.nr) {
93e7d672
JH
2169 /*
2170 * If we need to introduce the magic "a lone ':' means no
2171 * pathspec whatsoever", here is the place to do so.
2172 *
2173 * if (prune_data.nr == 1 && !strcmp(prune_data[0], ":")) {
2174 * prune_data.nr = 0;
2175 * prune_data.alloc = 0;
2176 * free(prune_data.path);
2177 * prune_data.path = NULL;
2178 * } else {
2179 * terminate prune_data.alloc with NULL and
2180 * call init_pathspec() to set revs->prune_data here.
2181 * }
2182 */
9e57ac55 2183 ALLOC_GROW(prune_data.path, prune_data.nr + 1, prune_data.alloc);
4da5af31 2184 prune_data.path[prune_data.nr++] = NULL;
0fdc2ae5
NTND
2185 parse_pathspec(&revs->prune_data, 0, 0,
2186 revs->prefix, prune_data.path);
4da5af31 2187 }
5486ef0e 2188
02e54220 2189 if (revs->def == NULL)
32962c9b 2190 revs->def = opt ? opt->def : NULL;
b4490059
JH
2191 if (opt && opt->tweak)
2192 opt->tweak(revs, opt);
02e54220 2193 if (revs->show_merge)
ae3e5e1e 2194 prepare_show_merge(revs);
8fcaca3f 2195 if (revs->def && !revs->pending.nr && !got_rev_arg) {
ae563542 2196 unsigned char sha1[20];
cd2bdc53 2197 struct object *object;
249c8f4a 2198 struct object_context oc;
33bd598c 2199 if (get_sha1_with_context(revs->def, 0, sha1, &oc))
02e54220
PH
2200 die("bad default revision '%s'", revs->def);
2201 object = get_reference(revs, revs->def, sha1, 0);
249c8f4a 2202 add_pending_object_with_mode(revs, object, revs->def, oc.mode);
ae563542 2203 }
8efdc326 2204
b7bb760d
LT
2205 /* Did the user ask for any diff output? Run the diff! */
2206 if (revs->diffopt.output_format & ~DIFF_FORMAT_NO_OUTPUT)
2207 revs->diff = 1;
2208
0faf2da7
AL
2209 /* Pickaxe, diff-filter and rename following need diffs */
2210 if (revs->diffopt.pickaxe ||
2211 revs->diffopt.filter ||
2212 DIFF_OPT_TST(&revs->diffopt, FOLLOW_RENAMES))
b7bb760d
LT
2213 revs->diff = 1;
2214
9dad9d2e 2215 if (revs->topo_order)
53069686
JH
2216 revs->limited = 1;
2217
afe069d1 2218 if (revs->prune_data.nr) {
bd1928df 2219 copy_pathspec(&revs->pruning.pathspec, &revs->prune_data);
750f7b66 2220 /* Can't prune commits with rename following: the paths change.. */
8f67f8ae 2221 if (!DIFF_OPT_TST(&revs->diffopt, FOLLOW_RENAMES))
53b2c823 2222 revs->prune = 1;
cd2bdc53 2223 if (!revs->full_diff)
bd1928df
NTND
2224 copy_pathspec(&revs->diffopt.pathspec,
2225 &revs->prune_data);
8efdc326 2226 }
b4490059 2227 if (revs->combine_merges)
cd2bdc53 2228 revs->ignore_merges = 0;
cd2bdc53 2229 revs->diffopt.abbrev = revs->abbrev;
12da1d1f
TR
2230
2231 if (revs->line_level_traverse) {
2232 revs->limited = 1;
2233 revs->topo_order = 1;
2234 }
2235
28452655 2236 diff_setup_done(&revs->diffopt);
8efdc326 2237
918d4e1c
JH
2238 grep_commit_pattern_type(GREP_PATTERN_TYPE_UNSPECIFIED,
2239 &revs->grep_filter);
0843acfd 2240 compile_grep_patterns(&revs->grep_filter);
bd95fcd3 2241
d56651c0
SP
2242 if (revs->reverse && revs->reflog_info)
2243 die("cannot combine --reverse with --walk-reflogs");
8bb65883 2244 if (revs->rewrite_parents && revs->children.name)
f35f5603 2245 die("cannot combine --parents and --children");
d56651c0 2246
7fefda5c
AS
2247 /*
2248 * Limitations on the graph functionality
2249 */
2250 if (revs->reverse && revs->graph)
2251 die("cannot combine --reverse with --graph");
2252
2253 if (revs->reflog_info && revs->graph)
2254 die("cannot combine --walk-reflogs with --graph");
baa6378f
JH
2255 if (!revs->reflog_info && revs->grep_filter.use_reflog_filter)
2256 die("cannot use --grep-reflog without --walk-reflogs");
7fefda5c 2257
ae563542
LT
2258 return left;
2259}
a4a88b2b 2260
f35f5603
JH
2261static void add_child(struct rev_info *revs, struct commit *parent, struct commit *child)
2262{
2263 struct commit_list *l = xcalloc(1, sizeof(*l));
2264
2265 l->item = child;
2266 l->next = add_decoration(&revs->children, &parent->object, l);
2267}
2268
d0af663e 2269static int remove_duplicate_parents(struct rev_info *revs, struct commit *commit)
6546b593 2270{
d0af663e 2271 struct treesame_state *ts = lookup_decoration(&revs->treesame, &commit->object);
6546b593
JH
2272 struct commit_list **pp, *p;
2273 int surviving_parents;
2274
2275 /* Examine existing parents while marking ones we have seen... */
2276 pp = &commit->parents;
d0af663e 2277 surviving_parents = 0;
6546b593
JH
2278 while ((p = *pp) != NULL) {
2279 struct commit *parent = p->item;
2280 if (parent->object.flags & TMP_MARK) {
2281 *pp = p->next;
d0af663e
KB
2282 if (ts)
2283 compact_treesame(revs, commit, surviving_parents);
6546b593
JH
2284 continue;
2285 }
2286 parent->object.flags |= TMP_MARK;
d0af663e 2287 surviving_parents++;
6546b593
JH
2288 pp = &p->next;
2289 }
d0af663e 2290 /* clear the temporary mark */
6546b593
JH
2291 for (p = commit->parents; p; p = p->next) {
2292 p->item->object.flags &= ~TMP_MARK;
6546b593 2293 }
d0af663e 2294 /* no update_treesame() - removing duplicates can't affect TREESAME */
6546b593
JH
2295 return surviving_parents;
2296}
2297
faf0156b
JH
2298struct merge_simplify_state {
2299 struct commit *simplified;
2300};
2301
2302static struct merge_simplify_state *locate_simplify_state(struct rev_info *revs, struct commit *commit)
2303{
2304 struct merge_simplify_state *st;
2305
2306 st = lookup_decoration(&revs->merge_simplification, &commit->object);
2307 if (!st) {
2308 st = xcalloc(1, sizeof(*st));
2309 add_decoration(&revs->merge_simplification, &commit->object, st);
2310 }
2311 return st;
2312}
2313
d0af663e
KB
2314static int mark_redundant_parents(struct rev_info *revs, struct commit *commit)
2315{
2316 struct commit_list *h = reduce_heads(commit->parents);
2317 int i = 0, marked = 0;
2318 struct commit_list *po, *pn;
2319
2320 /* Want these for sanity-checking only */
2321 int orig_cnt = commit_list_count(commit->parents);
2322 int cnt = commit_list_count(h);
2323
2324 /*
2325 * Not ready to remove items yet, just mark them for now, based
2326 * on the output of reduce_heads(). reduce_heads outputs the reduced
2327 * set in its original order, so this isn't too hard.
2328 */
2329 po = commit->parents;
2330 pn = h;
2331 while (po) {
2332 if (pn && po->item == pn->item) {
2333 pn = pn->next;
2334 i++;
2335 } else {
2336 po->item->object.flags |= TMP_MARK;
2337 marked++;
2338 }
2339 po=po->next;
2340 }
2341
2342 if (i != cnt || cnt+marked != orig_cnt)
2343 die("mark_redundant_parents %d %d %d %d", orig_cnt, cnt, i, marked);
2344
2345 free_commit_list(h);
2346
2347 return marked;
2348}
2349
143f1eaf
KB
2350static int mark_treesame_root_parents(struct rev_info *revs, struct commit *commit)
2351{
2352 struct commit_list *p;
2353 int marked = 0;
2354
2355 for (p = commit->parents; p; p = p->next) {
2356 struct commit *parent = p->item;
2357 if (!parent->parents && (parent->object.flags & TREESAME)) {
2358 parent->object.flags |= TMP_MARK;
2359 marked++;
2360 }
2361 }
2362
2363 return marked;
2364}
2365
9c129eab
KB
2366/*
2367 * Awkward naming - this means one parent we are TREESAME to.
2368 * cf mark_treesame_root_parents: root parents that are TREESAME (to an
2369 * empty tree). Better name suggestions?
2370 */
2371static int leave_one_treesame_to_parent(struct rev_info *revs, struct commit *commit)
2372{
2373 struct treesame_state *ts = lookup_decoration(&revs->treesame, &commit->object);
2374 struct commit *unmarked = NULL, *marked = NULL;
2375 struct commit_list *p;
2376 unsigned n;
2377
2378 for (p = commit->parents, n = 0; p; p = p->next, n++) {
2379 if (ts->treesame[n]) {
2380 if (p->item->object.flags & TMP_MARK) {
2381 if (!marked)
2382 marked = p->item;
2383 } else {
2384 if (!unmarked) {
2385 unmarked = p->item;
2386 break;
2387 }
2388 }
2389 }
2390 }
2391
2392 /*
2393 * If we are TREESAME to a marked-for-deletion parent, but not to any
2394 * unmarked parents, unmark the first TREESAME parent. This is the
2395 * parent that the default simplify_history==1 scan would have followed,
2396 * and it doesn't make sense to omit that path when asking for a
2397 * simplified full history. Retaining it improves the chances of
2398 * understanding odd missed merges that took an old version of a file.
2399 *
2400 * Example:
2401 *
2402 * I--------*X A modified the file, but mainline merge X used
2403 * \ / "-s ours", so took the version from I. X is
2404 * `-*A--' TREESAME to I and !TREESAME to A.
2405 *
2406 * Default log from X would produce "I". Without this check,
2407 * --full-history --simplify-merges would produce "I-A-X", showing
2408 * the merge commit X and that it changed A, but not making clear that
2409 * it had just taken the I version. With this check, the topology above
2410 * is retained.
2411 *
2412 * Note that it is possible that the simplification chooses a different
2413 * TREESAME parent from the default, in which case this test doesn't
2414 * activate, and we _do_ drop the default parent. Example:
2415 *
2416 * I------X A modified the file, but it was reverted in B,
2417 * \ / meaning mainline merge X is TREESAME to both
2418 * *A-*B parents.
2419 *
2420 * Default log would produce "I" by following the first parent;
2421 * --full-history --simplify-merges will produce "I-A-B". But this is a
2422 * reasonable result - it presents a logical full history leading from
2423 * I to X, and X is not an important merge.
2424 */
2425 if (!unmarked && marked) {
2426 marked->object.flags &= ~TMP_MARK;
2427 return 1;
2428 }
2429
2430 return 0;
2431}
2432
d0af663e
KB
2433static int remove_marked_parents(struct rev_info *revs, struct commit *commit)
2434{
2435 struct commit_list **pp, *p;
2436 int nth_parent, removed = 0;
2437
2438 pp = &commit->parents;
2439 nth_parent = 0;
2440 while ((p = *pp) != NULL) {
2441 struct commit *parent = p->item;
2442 if (parent->object.flags & TMP_MARK) {
2443 parent->object.flags &= ~TMP_MARK;
2444 *pp = p->next;
2445 free(p);
2446 removed++;
2447 compact_treesame(revs, commit, nth_parent);
2448 continue;
2449 }
2450 pp = &p->next;
2451 nth_parent++;
2452 }
2453
2454 /* Removing parents can only increase TREESAMEness */
2455 if (removed && !(commit->object.flags & TREESAME))
2456 update_treesame(revs, commit);
2457
2458 return nth_parent;
2459}
2460
faf0156b 2461static struct commit_list **simplify_one(struct rev_info *revs, struct commit *commit, struct commit_list **tail)
6546b593
JH
2462{
2463 struct commit_list *p;
4d826608 2464 struct commit *parent;
faf0156b 2465 struct merge_simplify_state *st, *pst;
6546b593
JH
2466 int cnt;
2467
faf0156b
JH
2468 st = locate_simplify_state(revs, commit);
2469
6546b593 2470 /*
6546b593
JH
2471 * Have we handled this one?
2472 */
faf0156b 2473 if (st->simplified)
6546b593
JH
2474 return tail;
2475
2476 /*
2477 * An UNINTERESTING commit simplifies to itself, so does a
2478 * root commit. We do not rewrite parents of such commit
2479 * anyway.
2480 */
2481 if ((commit->object.flags & UNINTERESTING) || !commit->parents) {
faf0156b 2482 st->simplified = commit;
6546b593
JH
2483 return tail;
2484 }
2485
2486 /*
6e513ba3
JH
2487 * Do we know what commit all of our parents that matter
2488 * should be rewritten to? Otherwise we are not ready to
2489 * rewrite this one yet.
6546b593
JH
2490 */
2491 for (cnt = 0, p = commit->parents; p; p = p->next) {
faf0156b
JH
2492 pst = locate_simplify_state(revs, p->item);
2493 if (!pst->simplified) {
6546b593
JH
2494 tail = &commit_list_insert(p->item, tail)->next;
2495 cnt++;
2496 }
6e513ba3
JH
2497 if (revs->first_parent_only)
2498 break;
6546b593 2499 }
53030f8d
JH
2500 if (cnt) {
2501 tail = &commit_list_insert(commit, tail)->next;
6546b593 2502 return tail;
53030f8d 2503 }
6546b593
JH
2504
2505 /*
d0af663e
KB
2506 * Rewrite our list of parents. Note that this cannot
2507 * affect our TREESAME flags in any way - a commit is
2508 * always TREESAME to its simplification.
6546b593 2509 */
faf0156b
JH
2510 for (p = commit->parents; p; p = p->next) {
2511 pst = locate_simplify_state(revs, p->item);
2512 p->item = pst->simplified;
6e513ba3
JH
2513 if (revs->first_parent_only)
2514 break;
faf0156b 2515 }
4b7f53da 2516
0290bf12 2517 if (revs->first_parent_only)
6e513ba3 2518 cnt = 1;
0290bf12 2519 else
d0af663e 2520 cnt = remove_duplicate_parents(revs, commit);
6546b593
JH
2521
2522 /*
2523 * It is possible that we are a merge and one side branch
2524 * does not have any commit that touches the given paths;
d0af663e
KB
2525 * in such a case, the immediate parent from that branch
2526 * will be rewritten to be the merge base.
6546b593
JH
2527 *
2528 * o----X X: the commit we are looking at;
2529 * / / o: a commit that touches the paths;
2530 * ---o----'
2531 *
143f1eaf
KB
2532 * Further, a merge of an independent branch that doesn't
2533 * touch the path will reduce to a treesame root parent:
2534 *
2535 * ----o----X X: the commit we are looking at;
2536 * / o: a commit that touches the paths;
2537 * r r: a root commit not touching the paths
2538 *
2539 * Detect and simplify both cases.
6546b593
JH
2540 */
2541 if (1 < cnt) {
d0af663e 2542 int marked = mark_redundant_parents(revs, commit);
143f1eaf 2543 marked += mark_treesame_root_parents(revs, commit);
9c129eab
KB
2544 if (marked)
2545 marked -= leave_one_treesame_to_parent(revs, commit);
d0af663e
KB
2546 if (marked)
2547 cnt = remove_marked_parents(revs, commit);
6546b593
JH
2548 }
2549
2550 /*
2551 * A commit simplifies to itself if it is a root, if it is
2552 * UNINTERESTING, if it touches the given paths, or if it is a
4d826608 2553 * merge and its parents don't simplify to one relevant commit
6546b593
JH
2554 * (the first two cases are already handled at the beginning of
2555 * this function).
2556 *
4d826608
KB
2557 * Otherwise, it simplifies to what its sole relevant parent
2558 * simplifies to.
6546b593
JH
2559 */
2560 if (!cnt ||
2561 (commit->object.flags & UNINTERESTING) ||
2562 !(commit->object.flags & TREESAME) ||
4d826608 2563 (parent = one_relevant_parent(revs, commit->parents)) == NULL)
faf0156b
JH
2564 st->simplified = commit;
2565 else {
4d826608 2566 pst = locate_simplify_state(revs, parent);
faf0156b
JH
2567 st->simplified = pst->simplified;
2568 }
6546b593
JH
2569 return tail;
2570}
2571
2572static void simplify_merges(struct rev_info *revs)
2573{
ab9d75a8 2574 struct commit_list *list, *next;
6546b593 2575 struct commit_list *yet_to_do, **tail;
ab9d75a8 2576 struct commit *commit;
6546b593 2577
5eac739e
JH
2578 if (!revs->prune)
2579 return;
65347030 2580
6546b593
JH
2581 /* feed the list reversed */
2582 yet_to_do = NULL;
ab9d75a8
JH
2583 for (list = revs->commits; list; list = next) {
2584 commit = list->item;
2585 next = list->next;
2586 /*
2587 * Do not free(list) here yet; the original list
2588 * is used later in this function.
2589 */
2590 commit_list_insert(commit, &yet_to_do);
2591 }
6546b593
JH
2592 while (yet_to_do) {
2593 list = yet_to_do;
2594 yet_to_do = NULL;
2595 tail = &yet_to_do;
2596 while (list) {
ab9d75a8
JH
2597 commit = list->item;
2598 next = list->next;
6546b593
JH
2599 free(list);
2600 list = next;
faf0156b 2601 tail = simplify_one(revs, commit, tail);
6546b593
JH
2602 }
2603 }
2604
2605 /* clean up the result, removing the simplified ones */
2606 list = revs->commits;
2607 revs->commits = NULL;
2608 tail = &revs->commits;
2609 while (list) {
faf0156b 2610 struct merge_simplify_state *st;
ab9d75a8
JH
2611
2612 commit = list->item;
2613 next = list->next;
6546b593
JH
2614 free(list);
2615 list = next;
faf0156b
JH
2616 st = locate_simplify_state(revs, commit);
2617 if (st->simplified == commit)
6546b593
JH
2618 tail = &commit_list_insert(commit, tail)->next;
2619 }
6546b593
JH
2620}
2621
f35f5603
JH
2622static void set_children(struct rev_info *revs)
2623{
2624 struct commit_list *l;
2625 for (l = revs->commits; l; l = l->next) {
2626 struct commit *commit = l->item;
2627 struct commit_list *p;
2628
2629 for (p = commit->parents; p; p = p->next)
2630 add_child(revs, p->item, commit);
2631 }
2632}
2633
bcc0a3ea
HV
2634void reset_revision_walk(void)
2635{
2636 clear_object_flags(SEEN | ADDED | SHOWN);
2637}
2638
cc0e6c5a 2639int prepare_revision_walk(struct rev_info *revs)
a4a88b2b 2640{
1f1e895f 2641 int nr = revs->pending.nr;
94d23673 2642 struct object_array_entry *e, *list;
2e7da8e9 2643 struct commit_list **next = &revs->commits;
cd2bdc53 2644
94d23673 2645 e = list = revs->pending.objects;
1f1e895f
LT
2646 revs->pending.nr = 0;
2647 revs->pending.alloc = 0;
2648 revs->pending.objects = NULL;
2649 while (--nr >= 0) {
94d23673 2650 struct commit *commit = handle_commit(revs, e->item, e->name);
cd2bdc53
LT
2651 if (commit) {
2652 if (!(commit->object.flags & SEEN)) {
2653 commit->object.flags |= SEEN;
2e7da8e9 2654 next = commit_list_append(commit, next);
cd2bdc53
LT
2655 }
2656 }
94d23673 2657 e++;
cd2bdc53 2658 }
4a43d374
RS
2659 if (!revs->leak_pending)
2660 free(list);
cd2bdc53 2661
d0af663e 2662 /* Signal whether we need per-parent treesame decoration */
4d826608
KB
2663 if (revs->simplify_merges ||
2664 (revs->limited && limiting_can_increase_treesame(revs)))
d0af663e
KB
2665 revs->treesame.name = "treesame";
2666
ca92e59e
MZ
2667 if (revs->no_walk != REVISION_WALK_NO_WALK_UNSORTED)
2668 commit_list_sort_by_date(&revs->commits);
ba1d4505 2669 if (revs->no_walk)
cc0e6c5a 2670 return 0;
a4a88b2b 2671 if (revs->limited)
cc0e6c5a
AR
2672 if (limit_list(revs) < 0)
2673 return -1;
a4a88b2b 2674 if (revs->topo_order)
08f704f2 2675 sort_in_topological_order(&revs->commits, revs->sort_order);
12da1d1f
TR
2676 if (revs->line_level_traverse)
2677 line_log_filter(revs);
6546b593
JH
2678 if (revs->simplify_merges)
2679 simplify_merges(revs);
f35f5603
JH
2680 if (revs->children.name)
2681 set_children(revs);
cc0e6c5a 2682 return 0;
a4a88b2b
LT
2683}
2684
cc0e6c5a 2685static enum rewrite_result rewrite_one(struct rev_info *revs, struct commit **pp)
765ac8ec 2686{
fce87ae5
AG
2687 struct commit_list *cache = NULL;
2688
765ac8ec
LT
2689 for (;;) {
2690 struct commit *p = *pp;
3381c790 2691 if (!revs->limited)
fce87ae5 2692 if (add_parents_to_list(revs, p, &revs->commits, &cache) < 0)
cc0e6c5a 2693 return rewrite_one_error;
7dc0fe3b
LT
2694 if (p->object.flags & UNINTERESTING)
2695 return rewrite_one_ok;
2696 if (!(p->object.flags & TREESAME))
cc0e6c5a 2697 return rewrite_one_ok;
765ac8ec 2698 if (!p->parents)
cc0e6c5a 2699 return rewrite_one_noparents;
4d826608
KB
2700 if ((p = one_relevant_parent(revs, p->parents)) == NULL)
2701 return rewrite_one_ok;
2702 *pp = p;
765ac8ec
LT
2703 }
2704}
2705
c7edcae0
BY
2706int rewrite_parents(struct rev_info *revs, struct commit *commit,
2707 rewrite_parent_fn_t rewrite_parent)
765ac8ec
LT
2708{
2709 struct commit_list **pp = &commit->parents;
2710 while (*pp) {
2711 struct commit_list *parent = *pp;
c7edcae0 2712 switch (rewrite_parent(revs, &parent->item)) {
cc0e6c5a
AR
2713 case rewrite_one_ok:
2714 break;
2715 case rewrite_one_noparents:
765ac8ec
LT
2716 *pp = parent->next;
2717 continue;
cc0e6c5a
AR
2718 case rewrite_one_error:
2719 return -1;
765ac8ec
LT
2720 }
2721 pp = &parent->next;
2722 }
d0af663e 2723 remove_duplicate_parents(revs, commit);
cc0e6c5a 2724 return 0;
765ac8ec
LT
2725}
2726
d72fbe81
AP
2727static int commit_rewrite_person(struct strbuf *buf, const char *what, struct string_list *mailmap)
2728{
2729 char *person, *endp;
2730 size_t len, namelen, maillen;
2731 const char *name;
2732 const char *mail;
2733 struct ident_split ident;
2734
2735 person = strstr(buf->buf, what);
2736 if (!person)
2737 return 0;
2738
2739 person += strlen(what);
2740 endp = strchr(person, '\n');
2741 if (!endp)
2742 return 0;
2743
2744 len = endp - person;
2745
2746 if (split_ident_line(&ident, person, len))
2747 return 0;
2748
2749 mail = ident.mail_begin;
2750 maillen = ident.mail_end - ident.mail_begin;
2751 name = ident.name_begin;
2752 namelen = ident.name_end - ident.name_begin;
2753
2754 if (map_user(mailmap, &mail, &maillen, &name, &namelen)) {
2755 struct strbuf namemail = STRBUF_INIT;
2756
2757 strbuf_addf(&namemail, "%.*s <%.*s>",
2758 (int)namelen, name, (int)maillen, mail);
2759
2760 strbuf_splice(buf, ident.name_begin - buf->buf,
2761 ident.mail_end - ident.name_begin + 1,
2762 namemail.buf, namemail.len);
2763
2764 strbuf_release(&namemail);
2765
2766 return 1;
2767 }
2768
2769 return 0;
2770}
2771
8ecae9b0
JH
2772static int commit_match(struct commit *commit, struct rev_info *opt)
2773{
72fd13f7 2774 int retval;
04deccda
JK
2775 const char *encoding;
2776 char *message;
72fd13f7 2777 struct strbuf buf = STRBUF_INIT;
04deccda 2778
80235ba7 2779 if (!opt->grep_filter.pattern_list && !opt->grep_filter.header_list)
8ecae9b0 2780 return 1;
baa6378f
JH
2781
2782 /* Prepend "fake" headers as needed */
2783 if (opt->grep_filter.use_reflog_filter) {
72fd13f7
NTND
2784 strbuf_addstr(&buf, "reflog ");
2785 get_reflog_message(&buf, opt->reflog_info);
2786 strbuf_addch(&buf, '\n');
72fd13f7 2787 }
baa6378f 2788
04deccda
JK
2789 /*
2790 * We grep in the user's output encoding, under the assumption that it
2791 * is the encoding they are most likely to write their grep pattern
2792 * for. In addition, it means we will match the "notes" encoding below,
2793 * so we will not end up with a buffer that has two different encodings
2794 * in it.
2795 */
2796 encoding = get_log_output_encoding();
5a10d236 2797 message = logmsg_reencode(commit, NULL, encoding);
04deccda 2798
baa6378f
JH
2799 /* Copy the commit to temporary if we are using "fake" headers */
2800 if (buf.len)
04deccda 2801 strbuf_addstr(&buf, message);
baa6378f 2802
df874fa8 2803 if (opt->grep_filter.header_list && opt->mailmap) {
d72fbe81 2804 if (!buf.len)
04deccda 2805 strbuf_addstr(&buf, message);
d72fbe81
AP
2806
2807 commit_rewrite_person(&buf, "\nauthor ", opt->mailmap);
2808 commit_rewrite_person(&buf, "\ncommitter ", opt->mailmap);
2809 }
2810
38cfe915
NTND
2811 /* Append "fake" message parts as needed */
2812 if (opt->show_notes) {
2813 if (!buf.len)
04deccda
JK
2814 strbuf_addstr(&buf, message);
2815 format_display_notes(commit->object.sha1, &buf, encoding, 1);
38cfe915
NTND
2816 }
2817
04deccda 2818 /* Find either in the original commit message, or in the temporary */
72fd13f7
NTND
2819 if (buf.len)
2820 retval = grep_buffer(&opt->grep_filter, buf.buf, buf.len);
2821 else
2822 retval = grep_buffer(&opt->grep_filter,
04deccda 2823 message, strlen(message));
72fd13f7 2824 strbuf_release(&buf);
04deccda 2825 logmsg_free(message, commit);
72fd13f7 2826 return retval;
8ecae9b0
JH
2827}
2828
53d00b39 2829static inline int want_ancestry(const struct rev_info *revs)
f35f5603 2830{
8bb65883 2831 return (revs->rewrite_parents || revs->children.name);
f35f5603
JH
2832}
2833
beb5af43 2834enum commit_action get_commit_action(struct rev_info *revs, struct commit *commit)
252a7c02
LT
2835{
2836 if (commit->object.flags & SHOWN)
2837 return commit_ignore;
4d6acb70 2838 if (revs->unpacked && has_sha1_pack(commit->object.sha1))
252a7c02 2839 return commit_ignore;
3131b713
LT
2840 if (revs->show_all)
2841 return commit_show;
252a7c02
LT
2842 if (commit->object.flags & UNINTERESTING)
2843 return commit_ignore;
2844 if (revs->min_age != -1 && (commit->date > revs->min_age))
2845 return commit_ignore;
ad5aeede 2846 if (revs->min_parents || (revs->max_parents >= 0)) {
bf3418b0 2847 int n = commit_list_count(commit->parents);
ad5aeede
MG
2848 if ((n < revs->min_parents) ||
2849 ((revs->max_parents >= 0) && (n > revs->max_parents)))
2850 return commit_ignore;
2851 }
252a7c02
LT
2852 if (!commit_match(commit, revs))
2853 return commit_ignore;
53b2c823 2854 if (revs->prune && revs->dense) {
252a7c02 2855 /* Commit without changes? */
7dc0fe3b 2856 if (commit->object.flags & TREESAME) {
bf3418b0
KB
2857 int n;
2858 struct commit_list *p;
252a7c02 2859 /* drop merges unless we want parenthood */
f35f5603 2860 if (!want_ancestry(revs))
252a7c02 2861 return commit_ignore;
bf3418b0
KB
2862 /*
2863 * If we want ancestry, then need to keep any merges
2864 * between relevant commits to tie together topology.
2865 * For consistency with TREESAME and simplification
2866 * use "relevant" here rather than just INTERESTING,
2867 * to treat bottom commit(s) as part of the topology.
2868 */
2869 for (n = 0, p = commit->parents; p; p = p->next)
2870 if (relevant_commit(p->item))
2871 if (++n >= 2)
2872 return commit_show;
2873 return commit_ignore;
252a7c02 2874 }
252a7c02
LT
2875 }
2876 return commit_show;
2877}
2878
beb5af43
AS
2879enum commit_action simplify_commit(struct rev_info *revs, struct commit *commit)
2880{
2881 enum commit_action action = get_commit_action(revs, commit);
2882
2883 if (action == commit_show &&
2884 !revs->show_all &&
2885 revs->prune && revs->dense && want_ancestry(revs)) {
53d00b39
TR
2886 /*
2887 * --full-diff on simplified parents is no good: it
2888 * will show spurious changes from the commits that
2889 * were elided. So we save the parents on the side
2890 * when --full-diff is in effect.
2891 */
2892 if (revs->full_diff)
2893 save_parents(revs, commit);
c7edcae0 2894 if (rewrite_parents(revs, commit, rewrite_one) < 0)
beb5af43
AS
2895 return commit_error;
2896 }
2897 return action;
2898}
2899
d5db6c9e 2900static struct commit *get_revision_1(struct rev_info *revs)
a4a88b2b 2901{
d5db6c9e 2902 if (!revs->commits)
a4a88b2b 2903 return NULL;
a4a88b2b 2904
765ac8ec 2905 do {
cb115748
LT
2906 struct commit_list *entry = revs->commits;
2907 struct commit *commit = entry->item;
ea5ed3ab 2908
cb115748
LT
2909 revs->commits = entry->next;
2910 free(entry);
2a0925be 2911
ffa1eeae 2912 if (revs->reflog_info) {
838f9a15 2913 save_parents(revs, commit);
8860fd42 2914 fake_reflog_parent(revs->reflog_info, commit);
ffa1eeae
JK
2915 commit->object.flags &= ~(ADDED | SEEN | SHOWN);
2916 }
8860fd42 2917
2a0925be
LT
2918 /*
2919 * If we haven't done the list limiting, we need to look at
be7db6e5
LT
2920 * the parents here. We also need to do the date-based limiting
2921 * that we'd otherwise have done in limit_list().
2a0925be 2922 */
be7db6e5 2923 if (!revs->limited) {
744f4985 2924 if (revs->max_age != -1 &&
86ab4906
JH
2925 (commit->date < revs->max_age))
2926 continue;
fce87ae5 2927 if (add_parents_to_list(revs, commit, &revs->commits, NULL) < 0)
ed62089c
JH
2928 die("Failed to traverse parents of commit %s",
2929 sha1_to_hex(commit->object.sha1));
be7db6e5 2930 }
744f4985 2931
252a7c02
LT
2932 switch (simplify_commit(revs, commit)) {
2933 case commit_ignore:
8ecae9b0 2934 continue;
252a7c02 2935 case commit_error:
ed62089c
JH
2936 die("Failed to simplify parents of commit %s",
2937 sha1_to_hex(commit->object.sha1));
252a7c02
LT
2938 default:
2939 return commit;
384e99a4 2940 }
765ac8ec
LT
2941 } while (revs->commits);
2942 return NULL;
2943}
d5db6c9e 2944
be6754c6
MH
2945/*
2946 * Return true for entries that have not yet been shown. (This is an
2947 * object_array_each_func_t.)
2948 */
2949static int entry_unshown(struct object_array_entry *entry, void *cb_data_unused)
86ab4906 2950{
be6754c6
MH
2951 return !(entry->item->flags & SHOWN);
2952}
86ab4906 2953
be6754c6
MH
2954/*
2955 * If array is on the verge of a realloc, garbage-collect any entries
2956 * that have already been shown to try to free up some space.
2957 */
2958static void gc_boundary(struct object_array *array)
2959{
2960 if (array->nr == array->alloc)
2961 object_array_filter(array, entry_unshown, NULL);
86ab4906
JH
2962}
2963
4603ec0f
AS
2964static void create_boundary_commit_list(struct rev_info *revs)
2965{
2966 unsigned i;
2967 struct commit *c;
2968 struct object_array *array = &revs->boundary_commits;
2969 struct object_array_entry *objects = array->objects;
2970
2971 /*
2972 * If revs->commits is non-NULL at this point, an error occurred in
2973 * get_revision_1(). Ignore the error and continue printing the
2974 * boundary commits anyway. (This is what the code has always
2975 * done.)
2976 */
2977 if (revs->commits) {
2978 free_commit_list(revs->commits);
2979 revs->commits = NULL;
2980 }
2981
2982 /*
2983 * Put all of the actual boundary commits from revs->boundary_commits
2984 * into revs->commits
2985 */
2986 for (i = 0; i < array->nr; i++) {
2987 c = (struct commit *)(objects[i].item);
2988 if (!c)
2989 continue;
2990 if (!(c->object.flags & CHILD_SHOWN))
2991 continue;
2992 if (c->object.flags & (SHOWN | BOUNDARY))
2993 continue;
2994 c->object.flags |= BOUNDARY;
2995 commit_list_insert(c, &revs->commits);
2996 }
2997
2998 /*
2999 * If revs->topo_order is set, sort the boundary commits
3000 * in topological order
3001 */
08f704f2 3002 sort_in_topological_order(&revs->commits, revs->sort_order);
4603ec0f
AS
3003}
3004
7fefda5c 3005static struct commit *get_revision_internal(struct rev_info *revs)
d5db6c9e
JH
3006{
3007 struct commit *c = NULL;
86ab4906
JH
3008 struct commit_list *l;
3009
3010 if (revs->boundary == 2) {
4603ec0f
AS
3011 /*
3012 * All of the normal commits have already been returned,
3013 * and we are now returning boundary commits.
3014 * create_boundary_commit_list() has populated
3015 * revs->commits with the remaining commits to return.
3016 */
3017 c = pop_commit(&revs->commits);
3018 if (c)
3019 c->object.flags |= SHOWN;
9c5e66e9
JS
3020 return c;
3021 }
3022
86ab4906 3023 /*
b72a1904
JK
3024 * If our max_count counter has reached zero, then we are done. We
3025 * don't simply return NULL because we still might need to show
3026 * boundary commits. But we want to avoid calling get_revision_1, which
3027 * might do a considerable amount of work finding the next commit only
3028 * for us to throw it away.
3029 *
3030 * If it is non-zero, then either we don't have a max_count at all
3031 * (-1), or it is still counting, in which case we decrement.
86ab4906 3032 */
b72a1904
JK
3033 if (revs->max_count) {
3034 c = get_revision_1(revs);
3035 if (c) {
9e57ac55 3036 while (revs->skip_count > 0) {
b72a1904
JK
3037 revs->skip_count--;
3038 c = get_revision_1(revs);
3039 if (!c)
3040 break;
3041 }
8839ac94 3042 }
86ab4906 3043
b72a1904
JK
3044 if (revs->max_count > 0)
3045 revs->max_count--;
d5db6c9e 3046 }
86ab4906 3047
c33d8593
JH
3048 if (c)
3049 c->object.flags |= SHOWN;
3050
9e57ac55 3051 if (!revs->boundary)
d5db6c9e 3052 return c;
86ab4906
JH
3053
3054 if (!c) {
3055 /*
3056 * get_revision_1() runs out the commits, and
3057 * we are done computing the boundaries.
3058 * switch to boundary commits output mode.
3059 */
3060 revs->boundary = 2;
4603ec0f
AS
3061
3062 /*
3063 * Update revs->commits to contain the list of
3064 * boundary commits.
3065 */
3066 create_boundary_commit_list(revs);
3067
3c68d67b 3068 return get_revision_internal(revs);
86ab4906
JH
3069 }
3070
3071 /*
3072 * boundary commits are the commits that are parents of the
3073 * ones we got from get_revision_1() but they themselves are
3074 * not returned from get_revision_1(). Before returning
3075 * 'c', we need to mark its parents that they could be boundaries.
3076 */
3077
3078 for (l = c->parents; l; l = l->next) {
3079 struct object *p;
3080 p = &(l->item->object);
c33d8593 3081 if (p->flags & (CHILD_SHOWN | SHOWN))
86ab4906
JH
3082 continue;
3083 p->flags |= CHILD_SHOWN;
3084 gc_boundary(&revs->boundary_commits);
3085 add_object_array(p, NULL, &revs->boundary_commits);
3086 }
3087
3088 return c;
d5db6c9e 3089}
7fefda5c
AS
3090
3091struct commit *get_revision(struct rev_info *revs)
3092{
498bcd31
TR
3093 struct commit *c;
3094 struct commit_list *reversed;
3095
3096 if (revs->reverse) {
3097 reversed = NULL;
9e57ac55 3098 while ((c = get_revision_internal(revs)))
498bcd31 3099 commit_list_insert(c, &reversed);
498bcd31
TR
3100 revs->commits = reversed;
3101 revs->reverse = 0;
3102 revs->reverse_output_stage = 1;
3103 }
3104
3105 if (revs->reverse_output_stage)
3106 return pop_commit(&revs->commits);
3107
3108 c = get_revision_internal(revs);
7fefda5c
AS
3109 if (c && revs->graph)
3110 graph_update(revs->graph, c);
53d00b39
TR
3111 if (!c)
3112 free_saved_parents(revs);
7fefda5c
AS
3113 return c;
3114}
1df2d656
MG
3115
3116char *get_revision_mark(const struct rev_info *revs, const struct commit *commit)
3117{
3118 if (commit->object.flags & BOUNDARY)
3119 return "-";
3120 else if (commit->object.flags & UNINTERESTING)
3121 return "^";
adbbb31e
MG
3122 else if (commit->object.flags & PATCHSAME)
3123 return "=";
1df2d656
MG
3124 else if (!revs || revs->left_right) {
3125 if (commit->object.flags & SYMMETRIC_LEFT)
3126 return "<";
3127 else
3128 return ">";
3129 } else if (revs->graph)
3130 return "*";
adbbb31e
MG
3131 else if (revs->cherry_mark)
3132 return "+";
1df2d656
MG
3133 return "";
3134}
b1b47554
MG
3135
3136void put_revision_mark(const struct rev_info *revs, const struct commit *commit)
3137{
3138 char *mark = get_revision_mark(revs, commit);
3139 if (!strlen(mark))
3140 return;
3141 fputs(mark, stdout);
3142 putchar(' ');
3143}
53d00b39
TR
3144
3145define_commit_slab(saved_parents, struct commit_list *);
3146
838f9a15
TR
3147#define EMPTY_PARENT_LIST ((struct commit_list *)-1)
3148
53d00b39
TR
3149void save_parents(struct rev_info *revs, struct commit *commit)
3150{
3151 struct commit_list **pp;
3152
3153 if (!revs->saved_parents_slab) {
3154 revs->saved_parents_slab = xmalloc(sizeof(struct saved_parents));
3155 init_saved_parents(revs->saved_parents_slab);
3156 }
3157
3158 pp = saved_parents_at(revs->saved_parents_slab, commit);
838f9a15
TR
3159
3160 /*
3161 * When walking with reflogs, we may visit the same commit
3162 * several times: once for each appearance in the reflog.
3163 *
3164 * In this case, save_parents() will be called multiple times.
3165 * We want to keep only the first set of parents. We need to
3166 * store a sentinel value for an empty (i.e., NULL) parent
3167 * list to distinguish it from a not-yet-saved list, however.
3168 */
3169 if (*pp)
3170 return;
3171 if (commit->parents)
3172 *pp = copy_commit_list(commit->parents);
3173 else
3174 *pp = EMPTY_PARENT_LIST;
53d00b39
TR
3175}
3176
3177struct commit_list *get_saved_parents(struct rev_info *revs, const struct commit *commit)
3178{
838f9a15
TR
3179 struct commit_list *parents;
3180
53d00b39
TR
3181 if (!revs->saved_parents_slab)
3182 return commit->parents;
3183
838f9a15
TR
3184 parents = *saved_parents_at(revs->saved_parents_slab, commit);
3185 if (parents == EMPTY_PARENT_LIST)
3186 return NULL;
3187 return parents;
53d00b39
TR
3188}
3189
3190void free_saved_parents(struct rev_info *revs)
3191{
3192 if (revs->saved_parents_slab)
3193 clear_saved_parents(revs->saved_parents_slab);
3194}