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