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