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