]> git.ipfire.org Git - thirdparty/git.git/blame - revision.c
revision: implement `git log --merge` also for rebase/cherry-pick/revert
[thirdparty/git.git] / revision.c
CommitLineData
bc5c5ec0 1#include "git-compat-util.h"
8c1bc2a7 2#include "config.h"
32a8f510 3#include "environment.h"
f394e093 4#include "gettext.h"
41771fa4 5#include "hex.h"
dabab1d6 6#include "object-name.h"
87bed179 7#include "object-file.h"
a034e910 8#include "object-store-ll.h"
9830926c 9#include "oidset.h"
ae563542
LT
10#include "tag.h"
11#include "blob.h"
12#include "tree.h"
13#include "commit.h"
a4a88b2b 14#include "diff.h"
a37eec63 15#include "diff-merges.h"
ae563542
LT
16#include "refs.h"
17#include "revision.h"
23a3f0cb 18#include "repository.h"
7fefda5c 19#include "graph.h"
8ecae9b0 20#include "grep.h"
8860fd42 21#include "reflog-walk.h"
d7a17cad 22#include "patch-ids.h"
f35f5603 23#include "decorate.h"
894a9d33 24#include "string-list.h"
12da1d1f 25#include "line-log.h"
d72fbe81 26#include "mailmap.h"
53d00b39 27#include "commit-slab.h"
4fe10219 28#include "cache-tree.h"
cb46d630 29#include "bisect.h"
150e3001 30#include "packfile.h"
be489d02 31#include "worktree.h"
08c46a49 32#include "read-cache.h"
e38da487 33#include "setup.h"
baf889c2 34#include "sparse-index.h"
dbbcd44f 35#include "strvec.h"
74ea5c95 36#include "trace2.h"
64043556 37#include "commit-reach.h"
f0d9cc41 38#include "commit-graph.h"
b4542418 39#include "prio-queue.h"
d5d2e935 40#include "hashmap.h"
44570188 41#include "utf8.h"
a56b9464 42#include "bloom.h"
42e50e78 43#include "json-writer.h"
c4ea513f 44#include "list-objects-filter-options.h"
5a5ea141 45#include "resolve-undo.h"
49fd5511 46#include "parse-options.h"
dd77d587 47#include "wildmatch.h"
ae563542 48
cdcefbc9
LT
49volatile show_early_output_fn_t show_early_output;
50
cb46d630
AD
51static const char *term_bad;
52static const char *term_good;
53
87be2523
NTND
54implement_shared_commit_slab(revision_sources, char *);
55
3cb9d2b6
SG
56static inline int want_ancestry(const struct rev_info *revs);
57
de1e67d0 58void show_object_with_name(FILE *out, struct object *obj, const char *name)
ae563542 59{
f2fd0760 60 fprintf(out, "%s ", oid_to_hex(&obj->oid));
44ba10d6 61 for (const char *p = name; *p && *p != '\n'; p++)
f9fb9d0e 62 fputc(*p, out);
beba25ab 63 fputc('\n', out);
91f17516
JH
64}
65
ae563542
LT
66static void mark_blob_uninteresting(struct blob *blob)
67{
c1ee9013
MK
68 if (!blob)
69 return;
ae563542
LT
70 if (blob->object.flags & UNINTERESTING)
71 return;
72 blob->object.flags |= UNINTERESTING;
73}
74
b3c7eef9
NTND
75static void mark_tree_contents_uninteresting(struct repository *r,
76 struct tree *tree)
ae563542 77{
f75e53ed 78 struct tree_desc desc;
4c068a98 79 struct name_entry entry;
ae563542 80
a5411df0 81 if (parse_tree_gently(tree, 1) < 0)
ae563542 82 return;
f75e53ed 83
6fda5e51 84 init_tree_desc(&desc, tree->buffer, tree->size);
4c068a98 85 while (tree_entry(&desc, &entry)) {
4d1012c3
LT
86 switch (object_type(entry.mode)) {
87 case OBJ_TREE:
ea82b2a0 88 mark_tree_uninteresting(r, lookup_tree(r, &entry.oid));
4d1012c3
LT
89 break;
90 case OBJ_BLOB:
ea82b2a0 91 mark_blob_uninteresting(lookup_blob(r, &entry.oid));
4d1012c3
LT
92 break;
93 default:
94 /* Subproject commit - not in this repository */
95 break;
96 }
ae563542 97 }
f75e53ed
LT
98
99 /*
100 * We don't care about the tree any more
101 * after it has been marked uninteresting.
102 */
6e454b9a 103 free_tree_buffer(tree);
ae563542
LT
104}
105
b3c7eef9 106void mark_tree_uninteresting(struct repository *r, struct tree *tree)
2ac5e447 107{
a2678df3 108 struct object *obj;
2ac5e447
JH
109
110 if (!tree)
111 return;
a2678df3
SN
112
113 obj = &tree->object;
2ac5e447
JH
114 if (obj->flags & UNINTERESTING)
115 return;
116 obj->flags |= UNINTERESTING;
b3c7eef9 117 mark_tree_contents_uninteresting(r, tree);
ae563542
LT
118}
119
d5d2e935
DS
120struct path_and_oids_entry {
121 struct hashmap_entry ent;
122 char *path;
123 struct oidset trees;
124};
125
5cf88fd8 126static int path_and_oids_cmp(const void *hashmap_cmp_fn_data UNUSED,
939af16e
EW
127 const struct hashmap_entry *eptr,
128 const struct hashmap_entry *entry_or_key,
5cf88fd8 129 const void *keydata UNUSED)
d5d2e935 130{
939af16e
EW
131 const struct path_and_oids_entry *e1, *e2;
132
133 e1 = container_of(eptr, const struct path_and_oids_entry, ent);
134 e2 = container_of(entry_or_key, const struct path_and_oids_entry, ent);
135
d5d2e935
DS
136 return strcmp(e1->path, e2->path);
137}
138
d5d2e935
DS
139static void paths_and_oids_clear(struct hashmap *map)
140{
141 struct hashmap_iter iter;
142 struct path_and_oids_entry *entry;
d5d2e935 143
23dee69f 144 hashmap_for_each_entry(map, &iter, entry, ent /* member name */) {
d5d2e935
DS
145 oidset_clear(&entry->trees);
146 free(entry->path);
147 }
148
6da1a258 149 hashmap_clear_and_free(map, struct path_and_oids_entry, ent);
d5d2e935
DS
150}
151
152static void paths_and_oids_insert(struct hashmap *map,
153 const char *path,
154 const struct object_id *oid)
155{
156 int hash = strhash(path);
157 struct path_and_oids_entry key;
158 struct path_and_oids_entry *entry;
159
d22245a2 160 hashmap_entry_init(&key.ent, hash);
d5d2e935
DS
161
162 /* use a shallow copy for the lookup */
163 key.path = (char *)path;
164 oidset_init(&key.trees, 0);
165
404ab78e 166 entry = hashmap_get_entry(map, &key, ent, NULL);
b6c52416 167 if (!entry) {
ca56dadb 168 CALLOC_ARRAY(entry, 1);
d22245a2 169 hashmap_entry_init(&entry->ent, hash);
d5d2e935
DS
170 entry->path = xstrdup(key.path);
171 oidset_init(&entry->trees, 16);
26b455f2 172 hashmap_put(map, &entry->ent);
d5d2e935
DS
173 }
174
175 oidset_insert(&entry->trees, oid);
176}
177
178static void add_children_by_path(struct repository *r,
179 struct tree *tree,
180 struct hashmap *map)
181{
182 struct tree_desc desc;
183 struct name_entry entry;
184
185 if (!tree)
186 return;
187
188 if (parse_tree_gently(tree, 1) < 0)
189 return;
190
191 init_tree_desc(&desc, tree->buffer, tree->size);
192 while (tree_entry(&desc, &entry)) {
193 switch (object_type(entry.mode)) {
194 case OBJ_TREE:
5fda3433 195 paths_and_oids_insert(map, entry.path, &entry.oid);
d5d2e935
DS
196
197 if (tree->object.flags & UNINTERESTING) {
5fda3433 198 struct tree *child = lookup_tree(r, &entry.oid);
d5d2e935
DS
199 if (child)
200 child->object.flags |= UNINTERESTING;
201 }
202 break;
203 case OBJ_BLOB:
204 if (tree->object.flags & UNINTERESTING) {
5fda3433 205 struct blob *child = lookup_blob(r, &entry.oid);
d5d2e935
DS
206 if (child)
207 child->object.flags |= UNINTERESTING;
208 }
209 break;
210 default:
211 /* Subproject commit - not in this repository */
212 break;
213 }
214 }
215
216 free_tree_buffer(tree);
217}
218
f1f5de44
DS
219void mark_trees_uninteresting_sparse(struct repository *r,
220 struct oidset *trees)
221{
d5d2e935 222 unsigned has_interesting = 0, has_uninteresting = 0;
b19315d8 223 struct hashmap map = HASHMAP_INIT(path_and_oids_cmp, NULL);
d5d2e935
DS
224 struct hashmap_iter map_iter;
225 struct path_and_oids_entry *entry;
f1f5de44
DS
226 struct object_id *oid;
227 struct oidset_iter iter;
228
229 oidset_iter_init(trees, &iter);
d5d2e935
DS
230 while ((!has_interesting || !has_uninteresting) &&
231 (oid = oidset_iter_next(&iter))) {
f1f5de44
DS
232 struct tree *tree = lookup_tree(r, oid);
233
234 if (!tree)
235 continue;
236
d5d2e935
DS
237 if (tree->object.flags & UNINTERESTING)
238 has_uninteresting = 1;
239 else
240 has_interesting = 1;
f1f5de44 241 }
d5d2e935
DS
242
243 /* Do not walk unless we have both types of trees. */
244 if (!has_uninteresting || !has_interesting)
245 return;
246
d5d2e935
DS
247 oidset_iter_init(trees, &iter);
248 while ((oid = oidset_iter_next(&iter))) {
249 struct tree *tree = lookup_tree(r, oid);
250 add_children_by_path(r, tree, &map);
251 }
252
23dee69f 253 hashmap_for_each_entry(&map, &map_iter, entry, ent /* member name */)
d5d2e935
DS
254 mark_trees_uninteresting_sparse(r, &entry->trees);
255
256 paths_and_oids_clear(&map);
f1f5de44
DS
257}
258
43fc643b
JK
259struct commit_stack {
260 struct commit **items;
261 size_t nr, alloc;
262};
9865b6e6 263#define COMMIT_STACK_INIT { 0 }
43fc643b
JK
264
265static void commit_stack_push(struct commit_stack *stack, struct commit *commit)
266{
267 ALLOC_GROW(stack->items, stack->nr + 1, stack->alloc);
268 stack->items[stack->nr++] = commit;
269}
270
271static struct commit *commit_stack_pop(struct commit_stack *stack)
272{
273 return stack->nr ? stack->items[--stack->nr] : NULL;
274}
275
276static void commit_stack_clear(struct commit_stack *stack)
277{
278 FREE_AND_NULL(stack->items);
279 stack->nr = stack->alloc = 0;
280}
281
9d505b7b 282static void mark_one_parent_uninteresting(struct rev_info *revs, struct commit *commit,
8702b30f
JK
283 struct commit_stack *pending)
284{
285 struct commit_list *l;
286
287 if (commit->object.flags & UNINTERESTING)
288 return;
289 commit->object.flags |= UNINTERESTING;
290
291 /*
292 * Normally we haven't parsed the parent
293 * yet, so we won't have a parent of a parent
294 * here. However, it may turn out that we've
295 * reached this commit some other way (where it
296 * wasn't uninteresting), in which case we need
297 * to mark its parents recursively too..
298 */
9d505b7b 299 for (l = commit->parents; l; l = l->next) {
8702b30f 300 commit_stack_push(pending, l->item);
9d505b7b
JZ
301 if (revs && revs->exclude_first_parent_only)
302 break;
303 }
8702b30f
JK
304}
305
9d505b7b 306void mark_parents_uninteresting(struct rev_info *revs, struct commit *commit)
ae563542 307{
43fc643b
JK
308 struct commit_stack pending = COMMIT_STACK_INIT;
309 struct commit_list *l;
941ba8db 310
9d505b7b
JZ
311 for (l = commit->parents; l; l = l->next) {
312 mark_one_parent_uninteresting(revs, l->item, &pending);
313 if (revs && revs->exclude_first_parent_only)
314 break;
315 }
8702b30f
JK
316
317 while (pending.nr > 0)
9d505b7b 318 mark_one_parent_uninteresting(revs, commit_stack_pop(&pending),
8702b30f 319 &pending);
43fc643b
JK
320
321 commit_stack_clear(&pending);
ae563542
LT
322}
323
20739490 324static void add_pending_object_with_path(struct rev_info *revs,
ff5f5f26 325 struct object *obj,
20739490
JK
326 const char *name, unsigned mode,
327 const char *path)
ae563542 328{
a4f66a78 329 struct interpret_branch_name_options options = { 0 };
cc243c3c
JH
330 if (!obj)
331 return;
aa27e461 332 if (revs->no_walk && (obj->flags & UNINTERESTING))
f222abde 333 revs->no_walk = 0;
105e4733
JH
334 if (revs->reflog_info && obj->type == OBJ_COMMIT) {
335 struct strbuf buf = STRBUF_INIT;
1d72b604 336 size_t namelen = strlen(name);
d850b7a5
ÆAB
337 int len = repo_interpret_branch_name(the_repository, name,
338 namelen, &buf, &options);
105e4733 339
1d72b604 340 if (0 < len && len < namelen && buf.len)
105e4733 341 strbuf_addstr(&buf, name + len);
d08565bf
JK
342 add_reflog_for_walk(revs->reflog_info,
343 (struct commit *)obj,
344 buf.buf[0] ? buf.buf: name);
105e4733 345 strbuf_release(&buf);
d08565bf 346 return; /* do not add the commit itself */
105e4733 347 }
20739490
JK
348 add_object_array_with_path(obj, name, &revs->pending, mode, path);
349}
350
351static void add_pending_object_with_mode(struct rev_info *revs,
352 struct object *obj,
353 const char *name, unsigned mode)
354{
355 add_pending_object_with_path(revs, obj, name, mode, NULL);
ae563542
LT
356}
357
ff5f5f26
MH
358void add_pending_object(struct rev_info *revs,
359 struct object *obj, const char *name)
2d93b9fa
JH
360{
361 add_pending_object_with_mode(revs, obj, name, S_IFINVALID);
362}
363
3384a2df
JH
364void add_head_to_pending(struct rev_info *revs)
365{
654b9a90 366 struct object_id oid;
3384a2df 367 struct object *obj;
d850b7a5 368 if (repo_get_oid(the_repository, "HEAD", &oid))
3384a2df 369 return;
b3c7eef9 370 obj = parse_object(revs->repo, &oid);
3384a2df
JH
371 if (!obj)
372 return;
373 add_pending_object(revs, obj, "HEAD");
374}
375
ff5f5f26 376static struct object *get_reference(struct rev_info *revs, const char *name,
654b9a90 377 const struct object_id *oid,
ff5f5f26 378 unsigned int flags)
ae563542
LT
379{
380 struct object *object;
381
9a8c3c4a
JK
382 object = parse_object_with_flags(revs->repo, oid,
383 revs->verify_objects ? 0 :
384 PARSE_OBJECT_SKIP_HASH_CHECK);
ec0c5798 385
cc243c3c
JH
386 if (!object) {
387 if (revs->ignore_missing)
388 return object;
df11e196
JT
389 if (revs->exclude_promisor_objects && is_promisor_object(oid))
390 return NULL;
ae563542 391 die("bad object %s", name);
cc243c3c 392 }
cd2bdc53
LT
393 object->flags |= flags;
394 return object;
395}
396
a58a1b01 397void add_pending_oid(struct rev_info *revs, const char *name,
398 const struct object_id *oid, unsigned int flags)
26c3177e 399{
654b9a90 400 struct object *object = get_reference(revs, name, oid, flags);
26c3177e
RS
401 add_pending_object(revs, object, name);
402}
403
ff5f5f26 404static struct commit *handle_commit(struct rev_info *revs,
20739490 405 struct object_array_entry *entry)
cd2bdc53 406{
20739490
JK
407 struct object *object = entry->item;
408 const char *name = entry->name;
409 const char *path = entry->path;
410 unsigned int mode = entry->mode;
cd2bdc53 411 unsigned long flags = object->flags;
ae563542
LT
412
413 /*
414 * Tag object? Look what it points to..
415 */
1974632c 416 while (object->type == OBJ_TAG) {
ae563542 417 struct tag *tag = (struct tag *) object;
cd2bdc53 418 if (revs->tag_objects && !(flags & UNINTERESTING))
ae563542 419 add_pending_object(revs, object, tag->tag);
dad3f060 420 object = parse_object(revs->repo, get_tagged_oid(tag));
aeeae1b7 421 if (!object) {
a3ba6bf1 422 if (revs->ignore_missing_links || (flags & UNINTERESTING))
aeeae1b7 423 return NULL;
dc0a13f6
JT
424 if (revs->exclude_promisor_objects &&
425 is_promisor_object(&tag->tagged->oid))
426 return NULL;
f2fd0760 427 die("bad object %s", oid_to_hex(&tag->tagged->oid));
aeeae1b7 428 }
a7435286 429 object->flags |= flags;
20739490
JK
430 /*
431 * We'll handle the tagged object by looping or dropping
432 * through to the non-tag handlers below. Do not
728350b7 433 * propagate path data from the tag's pending entry.
20739490 434 */
20739490
JK
435 path = NULL;
436 mode = 0;
ae563542
LT
437 }
438
439 /*
440 * Commit object? Just return it, we'll do all the complex
441 * reachability crud.
442 */
1974632c 443 if (object->type == OBJ_COMMIT) {
ae563542 444 struct commit *commit = (struct commit *)object;
87be2523 445
ea3f7e59 446 if (repo_parse_commit(revs->repo, commit) < 0)
ae563542 447 die("unable to parse commit %s", name);
d9a83684 448 if (flags & UNINTERESTING) {
9d505b7b 449 mark_parents_uninteresting(revs, commit);
1b4d8827
DS
450
451 if (!revs->topo_order || !generation_numbers_enabled(the_repository))
452 revs->limited = 1;
d9a83684 453 }
87be2523
NTND
454 if (revs->sources) {
455 char **slot = revision_sources_at(revs->sources, commit);
456
457 if (!*slot)
458 *slot = xstrdup(name);
459 }
ae563542
LT
460 return commit;
461 }
462
463 /*
3ea3c215 464 * Tree object? Either mark it uninteresting, or add it
ae563542
LT
465 * to the list of objects to look at later..
466 */
1974632c 467 if (object->type == OBJ_TREE) {
ae563542
LT
468 struct tree *tree = (struct tree *)object;
469 if (!revs->tree_objects)
470 return NULL;
471 if (flags & UNINTERESTING) {
b3c7eef9 472 mark_tree_contents_uninteresting(revs->repo, tree);
ae563542
LT
473 return NULL;
474 }
20739490 475 add_pending_object_with_path(revs, object, name, mode, path);
ae563542
LT
476 return NULL;
477 }
478
479 /*
480 * Blob object? You know the drill by now..
481 */
1974632c 482 if (object->type == OBJ_BLOB) {
ae563542
LT
483 if (!revs->blob_objects)
484 return NULL;
a7435286 485 if (flags & UNINTERESTING)
ae563542 486 return NULL;
20739490 487 add_pending_object_with_path(revs, object, name, mode, path);
ae563542
LT
488 return NULL;
489 }
490 die("%s is unknown object", name);
491}
492
b6e8a3b5
JK
493static int everybody_uninteresting(struct commit_list *orig,
494 struct commit **interesting_cache)
a4a88b2b
LT
495{
496 struct commit_list *list = orig;
b6e8a3b5
JK
497
498 if (*interesting_cache) {
499 struct commit *commit = *interesting_cache;
500 if (!(commit->object.flags & UNINTERESTING))
501 return 0;
502 }
503
a4a88b2b
LT
504 while (list) {
505 struct commit *commit = list->item;
506 list = list->next;
507 if (commit->object.flags & UNINTERESTING)
508 continue;
ae40ebda
SB
509
510 *interesting_cache = commit;
a4a88b2b
LT
511 return 0;
512 }
513 return 1;
514}
515
4d826608
KB
516/*
517 * A definition of "relevant" commit that we can use to simplify limited graphs
518 * by eliminating side branches.
519 *
520 * A "relevant" commit is one that is !UNINTERESTING (ie we are including it
521 * in our list), or that is a specified BOTTOM commit. Then after computing
522 * a limited list, during processing we can generally ignore boundary merges
523 * coming from outside the graph, (ie from irrelevant parents), and treat
524 * those merges as if they were single-parent. TREESAME is defined to consider
525 * only relevant parents, if any. If we are TREESAME to our on-graph parents,
526 * we don't care if we were !TREESAME to non-graph parents.
527 *
528 * Treating bottom commits as relevant ensures that a limited graph's
529 * connection to the actual bottom commit is not viewed as a side branch, but
530 * treated as part of the graph. For example:
531 *
532 * ....Z...A---X---o---o---B
533 * . /
534 * W---Y
535 *
536 * When computing "A..B", the A-X connection is at least as important as
537 * Y-X, despite A being flagged UNINTERESTING.
538 *
539 * And when computing --ancestry-path "A..B", the A-X connection is more
540 * important than Y-X, despite both A and Y being flagged UNINTERESTING.
541 */
542static inline int relevant_commit(struct commit *commit)
543{
544 return (commit->object.flags & (UNINTERESTING | BOTTOM)) != UNINTERESTING;
545}
546
547/*
548 * Return a single relevant commit from a parent list. If we are a TREESAME
549 * commit, and this selects one of our parents, then we can safely simplify to
550 * that parent.
551 */
552static struct commit *one_relevant_parent(const struct rev_info *revs,
553 struct commit_list *orig)
554{
555 struct commit_list *list = orig;
556 struct commit *relevant = NULL;
557
558 if (!orig)
559 return NULL;
560
561 /*
562 * For 1-parent commits, or if first-parent-only, then return that
563 * first parent (even if not "relevant" by the above definition).
564 * TREESAME will have been set purely on that parent.
565 */
566 if (revs->first_parent_only || !orig->next)
567 return orig->item;
568
569 /*
570 * For multi-parent commits, identify a sole relevant parent, if any.
571 * If we have only one relevant parent, then TREESAME will be set purely
572 * with regard to that parent, and we can simplify accordingly.
573 *
574 * If we have more than one relevant parent, or no relevant parents
575 * (and multiple irrelevant ones), then we can't select a parent here
576 * and return NULL.
577 */
578 while (list) {
579 struct commit *commit = list->item;
580 list = list->next;
581 if (relevant_commit(commit)) {
582 if (relevant)
583 return NULL;
584 relevant = commit;
585 }
586 }
587 return relevant;
588}
589
0a4ba7f8
JH
590/*
591 * The goal is to get REV_TREE_NEW as the result only if the
ceff8e7a
LT
592 * diff consists of all '+' (and no other changes), REV_TREE_OLD
593 * if the whole diff is removal of old data, and otherwise
594 * REV_TREE_DIFFERENT (of course if the trees are the same we
595 * want REV_TREE_SAME).
a937b37e
JK
596 *
597 * The only time we care about the distinction is when
598 * remove_empty_trees is in effect, in which case we care only about
599 * whether the whole change is REV_TREE_NEW, or if there's another type
600 * of change. Which means we can stop the diff early in either of these
601 * cases:
602 *
603 * 1. We're not using remove_empty_trees at all.
604 *
605 * 2. We saw anything except REV_TREE_NEW.
0a4ba7f8 606 */
296a1438
ÆAB
607#define REV_TREE_SAME 0
608#define REV_TREE_NEW 1 /* Only new files */
609#define REV_TREE_OLD 2 /* Only files removed */
610#define REV_TREE_DIFFERENT 3 /* Mixed changes */
8efdc326 611static int tree_difference = REV_TREE_SAME;
a4a88b2b
LT
612
613static void file_add_remove(struct diff_options *options,
61bdc7c5
JK
614 int addremove,
615 unsigned mode UNUSED,
616 const struct object_id *oid UNUSED,
617 int oid_valid UNUSED,
618 const char *fullpath UNUSED,
619 unsigned dirty_submodule UNUSED)
a4a88b2b 620{
ceff8e7a 621 int diff = addremove == '+' ? REV_TREE_NEW : REV_TREE_OLD;
a937b37e 622 struct rev_info *revs = options->change_fn_data;
a4a88b2b 623
ceff8e7a 624 tree_difference |= diff;
a937b37e 625 if (!revs->remove_empty_trees || tree_difference != REV_TREE_NEW)
0d1e0e78 626 options->flags.has_changes = 1;
a4a88b2b
LT
627}
628
629static void file_change(struct diff_options *options,
61bdc7c5
JK
630 unsigned old_mode UNUSED,
631 unsigned new_mode UNUSED,
632 const struct object_id *old_oid UNUSED,
633 const struct object_id *new_oid UNUSED,
634 int old_oid_valid UNUSED,
635 int new_oid_valid UNUSED,
636 const char *fullpath UNUSED,
637 unsigned old_dirty_submodule UNUSED,
638 unsigned new_dirty_submodule UNUSED)
a4a88b2b 639{
8efdc326 640 tree_difference = REV_TREE_DIFFERENT;
0d1e0e78 641 options->flags.has_changes = 1;
a4a88b2b
LT
642}
643
42e50e78
GS
644static int bloom_filter_atexit_registered;
645static unsigned int count_bloom_filter_maybe;
646static unsigned int count_bloom_filter_definitely_not;
647static unsigned int count_bloom_filter_false_positive;
648static unsigned int count_bloom_filter_not_present;
42e50e78
GS
649
650static void trace2_bloom_filter_statistics_atexit(void)
651{
652 struct json_writer jw = JSON_WRITER_INIT;
653
654 jw_object_begin(&jw, 0);
655 jw_object_intmax(&jw, "filter_not_present", count_bloom_filter_not_present);
42e50e78
GS
656 jw_object_intmax(&jw, "maybe", count_bloom_filter_maybe);
657 jw_object_intmax(&jw, "definitely_not", count_bloom_filter_definitely_not);
658 jw_object_intmax(&jw, "false_positive", count_bloom_filter_false_positive);
659 jw_end(&jw);
660
661 trace2_data_json("bloom", the_repository, "statistics", &jw);
662
663 jw_release(&jw);
664}
665
8918e379
DS
666static int forbid_bloom_filters(struct pathspec *spec)
667{
668 if (spec->has_wildcard)
669 return 1;
670 if (spec->nr > 1)
671 return 1;
672 if (spec->magic & ~PATHSPEC_LITERAL)
673 return 1;
674 if (spec->nr && (spec->items[0].magic & ~PATHSPEC_LITERAL))
675 return 1;
676
677 return 0;
678}
679
a56b9464
GS
680static void prepare_to_use_bloom_filter(struct rev_info *revs)
681{
682 struct pathspec_item *pi;
683 char *path_alloc = NULL;
c525ce95 684 const char *path, *p;
c525ce95
SG
685 size_t len;
686 int path_component_nr = 1;
a56b9464
GS
687
688 if (!revs->commits)
8918e379
DS
689 return;
690
691 if (forbid_bloom_filters(&revs->prune_data))
692 return;
a56b9464
GS
693
694 repo_parse_commit(revs->repo, revs->commits->item);
695
4f364405 696 revs->bloom_filter_settings = get_bloom_filter_settings(revs->repo);
a56b9464
GS
697 if (!revs->bloom_filter_settings)
698 return;
699
f32dde8c
DS
700 if (!revs->pruning.pathspec.nr)
701 return;
702
a56b9464 703 pi = &revs->pruning.pathspec.items[0];
a56b9464
GS
704
705 /* remove single trailing slash from path, if needed */
fd9a631c
JK
706 if (pi->len > 0 && pi->match[pi->len - 1] == '/') {
707 path_alloc = xmemdupz(pi->match, pi->len - 1);
dc8e95ba 708 path = path_alloc;
a56b9464 709 } else
dc8e95ba 710 path = pi->match;
a56b9464
GS
711
712 len = strlen(path);
f3c2a368
TB
713 if (!len) {
714 revs->bloom_filter_settings = NULL;
398e659e 715 free(path_alloc);
f3c2a368
TB
716 return;
717 }
a56b9464 718
c525ce95
SG
719 p = path;
720 while (*p) {
721 /*
722 * At this point, the path is normalized to use Unix-style
723 * path separators. This is required due to how the
724 * changed-path Bloom filters store the paths.
725 */
726 if (*p == '/')
727 path_component_nr++;
728 p++;
729 }
730
731 revs->bloom_keys_nr = path_component_nr;
732 ALLOC_ARRAY(revs->bloom_keys, revs->bloom_keys_nr);
a56b9464 733
c525ce95
SG
734 fill_bloom_key(path, len, &revs->bloom_keys[0],
735 revs->bloom_filter_settings);
736 path_component_nr = 1;
737
738 p = path + len - 1;
739 while (p > path) {
740 if (*p == '/')
741 fill_bloom_key(path, p - path,
742 &revs->bloom_keys[path_component_nr++],
743 revs->bloom_filter_settings);
744 p--;
745 }
a56b9464 746
42e50e78
GS
747 if (trace2_is_enabled() && !bloom_filter_atexit_registered) {
748 atexit(trace2_bloom_filter_statistics_atexit);
749 bloom_filter_atexit_registered = 1;
750 }
751
a56b9464
GS
752 free(path_alloc);
753}
754
755static int check_maybe_different_in_bloom_filter(struct rev_info *revs,
756 struct commit *commit)
757{
758 struct bloom_filter *filter;
c525ce95 759 int result = 1, j;
a56b9464
GS
760
761 if (!revs->repo->objects->commit_graph)
762 return -1;
763
c49c82aa 764 if (commit_graph_generation(commit) == GENERATION_NUMBER_INFINITY)
a56b9464
GS
765 return -1;
766
312cff52 767 filter = get_bloom_filter(revs->repo, commit);
a56b9464
GS
768
769 if (!filter) {
42e50e78 770 count_bloom_filter_not_present++;
a56b9464
GS
771 return -1;
772 }
773
c525ce95
SG
774 for (j = 0; result && j < revs->bloom_keys_nr; j++) {
775 result = bloom_filter_contains(filter,
776 &revs->bloom_keys[j],
777 revs->bloom_filter_settings);
a56b9464
GS
778 }
779
42e50e78
GS
780 if (result)
781 count_bloom_filter_maybe++;
782 else
783 count_bloom_filter_definitely_not++;
784
a56b9464
GS
785 return result;
786}
787
ff5f5f26 788static int rev_compare_tree(struct rev_info *revs,
a56b9464 789 struct commit *parent, struct commit *commit, int nth_parent)
a4a88b2b 790{
ecb5091f
ÆAB
791 struct tree *t1 = repo_get_commit_tree(the_repository, parent);
792 struct tree *t2 = repo_get_commit_tree(the_repository, commit);
a56b9464 793 int bloom_ret = 1;
3a5e8608 794
a4a88b2b 795 if (!t1)
8efdc326 796 return REV_TREE_NEW;
ceff8e7a
LT
797 if (!t2)
798 return REV_TREE_OLD;
78892e32
LT
799
800 if (revs->simplify_by_decoration) {
801 /*
802 * If we are simplifying by decoration, then the commit
803 * is worth showing if it has a tag pointing at it.
804 */
2608c249 805 if (get_name_decoration(&commit->object))
78892e32
LT
806 return REV_TREE_DIFFERENT;
807 /*
808 * A commit that is not pointed by a tag is uninteresting
809 * if we are not limited by path. This means that you will
810 * see the usual "commits that touch the paths" plus any
811 * tagged commit by specifying both --simplify-by-decoration
812 * and pathspec.
813 */
afe069d1 814 if (!revs->prune_data.nr)
78892e32
LT
815 return REV_TREE_SAME;
816 }
ceff8e7a 817
c525ce95 818 if (revs->bloom_keys_nr && !nth_parent) {
a56b9464
GS
819 bloom_ret = check_maybe_different_in_bloom_filter(revs, commit);
820
821 if (bloom_ret == 0)
822 return REV_TREE_SAME;
823 }
824
8efdc326 825 tree_difference = REV_TREE_SAME;
0d1e0e78 826 revs->pruning.flags.has_changes = 0;
0ee3cb88 827 diff_tree_oid(&t1->object.oid, &t2->object.oid, "", &revs->pruning);
a56b9464 828
42e50e78
GS
829 if (!nth_parent)
830 if (bloom_ret == 1 && tree_difference == REV_TREE_SAME)
831 count_bloom_filter_false_positive++;
832
a4a88b2b
LT
833 return tree_difference;
834}
835
3a5e8608 836static int rev_same_tree_as_empty(struct rev_info *revs, struct commit *commit)
a4a88b2b 837{
ecb5091f 838 struct tree *t1 = repo_get_commit_tree(the_repository, commit);
a4a88b2b
LT
839
840 if (!t1)
841 return 0;
842
0a4ba7f8 843 tree_difference = REV_TREE_SAME;
0d1e0e78 844 revs->pruning.flags.has_changes = 0;
0ee3cb88 845 diff_tree_oid(NULL, &t1->object.oid, "", &revs->pruning);
a4a88b2b 846
0ee3cb88 847 return tree_difference == REV_TREE_SAME;
a4a88b2b
LT
848}
849
d0af663e
KB
850struct treesame_state {
851 unsigned int nparents;
852 unsigned char treesame[FLEX_ARRAY];
853};
854
855static struct treesame_state *initialise_treesame(struct rev_info *revs, struct commit *commit)
856{
857 unsigned n = commit_list_count(commit->parents);
50a6c8ef 858 struct treesame_state *st = xcalloc(1, st_add(sizeof(*st), n));
d0af663e
KB
859 st->nparents = n;
860 add_decoration(&revs->treesame, &commit->object, st);
861 return st;
862}
863
864/*
865 * Must be called immediately after removing the nth_parent from a commit's
866 * parent list, if we are maintaining the per-parent treesame[] decoration.
867 * This does not recalculate the master TREESAME flag - update_treesame()
868 * should be called to update it after a sequence of treesame[] modifications
869 * that may have affected it.
870 */
871static int compact_treesame(struct rev_info *revs, struct commit *commit, unsigned nth_parent)
872{
873 struct treesame_state *st;
874 int old_same;
875
876 if (!commit->parents) {
877 /*
878 * Have just removed the only parent from a non-merge.
879 * Different handling, as we lack decoration.
880 */
881 if (nth_parent != 0)
882 die("compact_treesame %u", nth_parent);
883 old_same = !!(commit->object.flags & TREESAME);
884 if (rev_same_tree_as_empty(revs, commit))
885 commit->object.flags |= TREESAME;
886 else
887 commit->object.flags &= ~TREESAME;
888 return old_same;
889 }
890
891 st = lookup_decoration(&revs->treesame, &commit->object);
892 if (!st || nth_parent >= st->nparents)
893 die("compact_treesame %u", nth_parent);
894
895 old_same = st->treesame[nth_parent];
896 memmove(st->treesame + nth_parent,
897 st->treesame + nth_parent + 1,
898 st->nparents - nth_parent - 1);
899
900 /*
901 * If we've just become a non-merge commit, update TREESAME
902 * immediately, and remove the no-longer-needed decoration.
903 * If still a merge, defer update until update_treesame().
904 */
905 if (--st->nparents == 1) {
906 if (commit->parents->next)
907 die("compact_treesame parents mismatch");
908 if (st->treesame[0] && revs->dense)
909 commit->object.flags |= TREESAME;
910 else
911 commit->object.flags &= ~TREESAME;
912 free(add_decoration(&revs->treesame, &commit->object, NULL));
913 }
914
915 return old_same;
916}
917
918static unsigned update_treesame(struct rev_info *revs, struct commit *commit)
919{
920 if (commit->parents && commit->parents->next) {
921 unsigned n;
922 struct treesame_state *st;
4d826608
KB
923 struct commit_list *p;
924 unsigned relevant_parents;
925 unsigned relevant_change, irrelevant_change;
d0af663e
KB
926
927 st = lookup_decoration(&revs->treesame, &commit->object);
928 if (!st)
f2fd0760 929 die("update_treesame %s", oid_to_hex(&commit->object.oid));
4d826608
KB
930 relevant_parents = 0;
931 relevant_change = irrelevant_change = 0;
932 for (p = commit->parents, n = 0; p; n++, p = p->next) {
933 if (relevant_commit(p->item)) {
934 relevant_change |= !st->treesame[n];
935 relevant_parents++;
936 } else
937 irrelevant_change |= !st->treesame[n];
d0af663e 938 }
4d826608
KB
939 if (relevant_parents ? relevant_change : irrelevant_change)
940 commit->object.flags &= ~TREESAME;
941 else
942 commit->object.flags |= TREESAME;
d0af663e
KB
943 }
944
945 return commit->object.flags & TREESAME;
946}
947
4d826608
KB
948static inline int limiting_can_increase_treesame(const struct rev_info *revs)
949{
950 /*
951 * TREESAME is irrelevant unless prune && dense;
952 * if simplify_history is set, we can't have a mixture of TREESAME and
953 * !TREESAME INTERESTING parents (and we don't have treesame[]
954 * decoration anyway);
955 * if first_parent_only is set, then the TREESAME flag is locked
956 * against the first parent (and again we lack treesame[] decoration).
957 */
958 return revs->prune && revs->dense &&
959 !revs->simplify_history &&
960 !revs->first_parent_only;
961}
962
a4a88b2b
LT
963static void try_to_simplify_commit(struct rev_info *revs, struct commit *commit)
964{
965 struct commit_list **pp, *parent;
d0af663e 966 struct treesame_state *ts = NULL;
4d826608
KB
967 int relevant_change = 0, irrelevant_change = 0;
968 int relevant_parents, nth_parent;
a4a88b2b 969
53b2c823
LT
970 /*
971 * If we don't do pruning, everything is interesting
972 */
7dc0fe3b 973 if (!revs->prune)
53b2c823 974 return;
53b2c823 975
ecb5091f 976 if (!repo_get_commit_tree(the_repository, commit))
a4a88b2b
LT
977 return;
978
979 if (!commit->parents) {
3a5e8608 980 if (rev_same_tree_as_empty(revs, commit))
7dc0fe3b 981 commit->object.flags |= TREESAME;
a4a88b2b
LT
982 return;
983 }
984
53b2c823
LT
985 /*
986 * Normal non-merge commit? If we don't want to make the
987 * history dense, we consider it always to be a change..
988 */
7dc0fe3b 989 if (!revs->dense && !commit->parents->next)
53b2c823 990 return;
53b2c823 991
4d826608 992 for (pp = &commit->parents, nth_parent = 0, relevant_parents = 0;
d0af663e
KB
993 (parent = *pp) != NULL;
994 pp = &parent->next, nth_parent++) {
a4a88b2b 995 struct commit *p = parent->item;
4d826608
KB
996 if (relevant_commit(p))
997 relevant_parents++;
a4a88b2b 998
d0af663e
KB
999 if (nth_parent == 1) {
1000 /*
1001 * This our second loop iteration - so we now know
1002 * we're dealing with a merge.
1003 *
1004 * Do not compare with later parents when we care only about
1005 * the first parent chain, in order to avoid derailing the
1006 * traversal to follow a side branch that brought everything
1007 * in the path we are limited to by the pathspec.
1008 */
1009 if (revs->first_parent_only)
1010 break;
1011 /*
1012 * If this will remain a potentially-simplifiable
1013 * merge, remember per-parent treesame if needed.
1014 * Initialise the array with the comparison from our
1015 * first iteration.
1016 */
1017 if (revs->treesame.name &&
1018 !revs->simplify_history &&
1019 !(commit->object.flags & UNINTERESTING)) {
1020 ts = initialise_treesame(revs, commit);
4d826608 1021 if (!(irrelevant_change || relevant_change))
d0af663e
KB
1022 ts->treesame[0] = 1;
1023 }
1024 }
ea3f7e59 1025 if (repo_parse_commit(revs->repo, p) < 0)
cc0e6c5a 1026 die("cannot simplify commit %s (because of %s)",
f2fd0760 1027 oid_to_hex(&commit->object.oid),
1028 oid_to_hex(&p->object.oid));
a56b9464 1029 switch (rev_compare_tree(revs, p, commit, nth_parent)) {
8efdc326 1030 case REV_TREE_SAME:
141efdba 1031 if (!revs->simplify_history || !relevant_commit(p)) {
f3219fbb
JH
1032 /* Even if a merge with an uninteresting
1033 * side branch brought the entire change
1034 * we are interested in, we do not want
1035 * to lose the other branches of this
1036 * merge, so we just keep going.
1037 */
d0af663e
KB
1038 if (ts)
1039 ts->treesame[nth_parent] = 1;
f3219fbb
JH
1040 continue;
1041 }
a4a88b2b
LT
1042 parent->next = NULL;
1043 commit->parents = parent;
8d049e18
DS
1044
1045 /*
1046 * A merge commit is a "diversion" if it is not
1047 * TREESAME to its first parent but is TREESAME
1048 * to a later parent. In the simplified history,
1049 * we "divert" the history walk to the later
1050 * parent. These commits are shown when "show_pulls"
1051 * is enabled, so do not mark the object as
1052 * TREESAME here.
1053 */
1054 if (!revs->show_pulls || !nth_parent)
1055 commit->object.flags |= TREESAME;
1056
a4a88b2b
LT
1057 return;
1058
8efdc326
FK
1059 case REV_TREE_NEW:
1060 if (revs->remove_empty_trees &&
3a5e8608 1061 rev_same_tree_as_empty(revs, p)) {
c348f31a
JH
1062 /* We are adding all the specified
1063 * paths from this parent, so the
1064 * history beyond this parent is not
1065 * interesting. Remove its parents
1066 * (they are grandparents for us).
1067 * IOW, we pretend this parent is a
1068 * "root" commit.
a41e109c 1069 */
ea3f7e59 1070 if (repo_parse_commit(revs->repo, p) < 0)
cc0e6c5a 1071 die("cannot simplify commit %s (invalid %s)",
f2fd0760 1072 oid_to_hex(&commit->object.oid),
1073 oid_to_hex(&p->object.oid));
c348f31a 1074 p->parents = NULL;
a4a88b2b
LT
1075 }
1076 /* fallthrough */
ceff8e7a 1077 case REV_TREE_OLD:
8efdc326 1078 case REV_TREE_DIFFERENT:
4d826608
KB
1079 if (relevant_commit(p))
1080 relevant_change = 1;
1081 else
1082 irrelevant_change = 1;
8d049e18
DS
1083
1084 if (!nth_parent)
1085 commit->object.flags |= PULL_MERGE;
1086
a4a88b2b
LT
1087 continue;
1088 }
f2fd0760 1089 die("bad tree compare for commit %s", oid_to_hex(&commit->object.oid));
a4a88b2b 1090 }
4d826608
KB
1091
1092 /*
1093 * TREESAME is straightforward for single-parent commits. For merge
1094 * commits, it is most useful to define it so that "irrelevant"
1095 * parents cannot make us !TREESAME - if we have any relevant
1096 * parents, then we only consider TREESAMEness with respect to them,
1097 * allowing irrelevant merges from uninteresting branches to be
1098 * simplified away. Only if we have only irrelevant parents do we
1099 * base TREESAME on them. Note that this logic is replicated in
1100 * update_treesame, which should be kept in sync.
1101 */
1102 if (relevant_parents ? !relevant_change : !irrelevant_change)
1103 commit->object.flags |= TREESAME;
a4a88b2b
LT
1104}
1105
5284fc5c 1106static int process_parents(struct rev_info *revs, struct commit *commit,
8320b1db 1107 struct commit_list **list, struct prio_queue *queue)
a4a88b2b
LT
1108{
1109 struct commit_list *parent = commit->parents;
257418c5 1110 unsigned pass_flags;
a4a88b2b 1111
3381c790 1112 if (commit->object.flags & ADDED)
cc0e6c5a 1113 return 0;
9830926c
KN
1114 if (revs->do_not_die_on_missing_objects &&
1115 oidset_contains(&revs->missing_commits, &commit->object.oid))
1116 return 0;
3381c790
LT
1117 commit->object.flags |= ADDED;
1118
a330de31
VM
1119 if (revs->include_check &&
1120 !revs->include_check(commit, revs->include_check_data))
1121 return 0;
1122
a4a88b2b
LT
1123 /*
1124 * If the commit is uninteresting, don't try to
1125 * prune parents - we want the maximal uninteresting
1126 * set.
1127 *
1128 * Normally we haven't parsed the parent
1129 * yet, so we won't have a parent of a parent
1130 * here. However, it may turn out that we've
1131 * reached this commit some other way (where it
1132 * wasn't uninteresting), in which case we need
1133 * to mark its parents recursively too..
1134 */
1135 if (commit->object.flags & UNINTERESTING) {
1136 while (parent) {
1137 struct commit *p = parent->item;
1138 parent = parent->next;
aeeae1b7
JH
1139 if (p)
1140 p->object.flags |= UNINTERESTING;
ea3f7e59 1141 if (repo_parse_commit_gently(revs->repo, p, 1) < 0)
aeeae1b7 1142 continue;
a4a88b2b 1143 if (p->parents)
9d505b7b 1144 mark_parents_uninteresting(revs, p);
a4a88b2b
LT
1145 if (p->object.flags & SEEN)
1146 continue;
b2025da3 1147 p->object.flags |= (SEEN | NOT_USER_GIVEN);
5284fc5c 1148 if (list)
8320b1db
JK
1149 commit_list_insert_by_date(p, list);
1150 if (queue)
1151 prio_queue_put(queue, p);
9d505b7b
JZ
1152 if (revs->exclude_first_parent_only)
1153 break;
a4a88b2b 1154 }
cc0e6c5a 1155 return 0;
a4a88b2b
LT
1156 }
1157
1158 /*
1159 * Ok, the commit wasn't uninteresting. Try to
1160 * simplify the commit history and find the parent
1161 * that has no differences in the path set if one exists.
1162 */
53b2c823 1163 try_to_simplify_commit(revs, commit);
a4a88b2b 1164
ba1d4505 1165 if (revs->no_walk)
cc0e6c5a 1166 return 0;
ba1d4505 1167
257418c5 1168 pass_flags = (commit->object.flags & (SYMMETRIC_LEFT | ANCESTRY_PATH));
0053e902 1169
d9c292e8 1170 for (parent = commit->parents; parent; parent = parent->next) {
a4a88b2b 1171 struct commit *p = parent->item;
df11e196 1172 int gently = revs->ignore_missing_links ||
9830926c
KN
1173 revs->exclude_promisor_objects ||
1174 revs->do_not_die_on_missing_objects;
ea3f7e59 1175 if (repo_parse_commit_gently(revs->repo, p, gently) < 0) {
df11e196
JT
1176 if (revs->exclude_promisor_objects &&
1177 is_promisor_object(&p->object.oid)) {
1178 if (revs->first_parent_only)
1179 break;
1180 continue;
1181 }
9830926c
KN
1182
1183 if (revs->do_not_die_on_missing_objects)
1184 oidset_insert(&revs->missing_commits, &p->object.oid);
1185 else
1186 return -1; /* corrupt repository */
df11e196 1187 }
87be2523
NTND
1188 if (revs->sources) {
1189 char **slot = revision_sources_at(revs->sources, p);
1190
1191 if (!*slot)
1192 *slot = *revision_sources_at(revs->sources, commit);
1193 }
257418c5 1194 p->object.flags |= pass_flags;
ad1012eb 1195 if (!(p->object.flags & SEEN)) {
b2025da3 1196 p->object.flags |= (SEEN | NOT_USER_GIVEN);
5284fc5c 1197 if (list)
8320b1db
JK
1198 commit_list_insert_by_date(p, list);
1199 if (queue)
1200 prio_queue_put(queue, p);
ad1012eb 1201 }
60d30b02 1202 if (revs->first_parent_only)
d9c292e8 1203 break;
a4a88b2b 1204 }
cc0e6c5a 1205 return 0;
a4a88b2b
LT
1206}
1207
36d56de6 1208static void cherry_pick_list(struct commit_list *list, struct rev_info *revs)
d7a17cad
JH
1209{
1210 struct commit_list *p;
1211 int left_count = 0, right_count = 0;
1212 int left_first;
1213 struct patch_ids ids;
adbbb31e 1214 unsigned cherry_flag;
d7a17cad
JH
1215
1216 /* First count the commits on the left and on the right */
1217 for (p = list; p; p = p->next) {
1218 struct commit *commit = p->item;
1219 unsigned flags = commit->object.flags;
1220 if (flags & BOUNDARY)
1221 ;
1222 else if (flags & SYMMETRIC_LEFT)
1223 left_count++;
1224 else
1225 right_count++;
1226 }
1227
36c07975
TR
1228 if (!left_count || !right_count)
1229 return;
1230
d7a17cad 1231 left_first = left_count < right_count;
2abf3503 1232 init_patch_ids(revs->repo, &ids);
66f13625 1233 ids.diffopts.pathspec = revs->diffopt.pathspec;
d7a17cad
JH
1234
1235 /* Compute patch-ids for one side */
1236 for (p = list; p; p = p->next) {
1237 struct commit *commit = p->item;
1238 unsigned flags = commit->object.flags;
1239
1240 if (flags & BOUNDARY)
1241 continue;
1242 /*
1243 * If we have fewer left, left_first is set and we omit
1244 * commits on the right branch in this loop. If we have
1245 * fewer right, we skip the left ones.
1246 */
1247 if (left_first != !!(flags & SYMMETRIC_LEFT))
1248 continue;
683f17ec 1249 add_commit_patch_id(commit, &ids);
d7a17cad
JH
1250 }
1251
adbbb31e
MG
1252 /* either cherry_mark or cherry_pick are true */
1253 cherry_flag = revs->cherry_mark ? PATCHSAME : SHOWN;
1254
d7a17cad
JH
1255 /* Check the other side */
1256 for (p = list; p; p = p->next) {
1257 struct commit *commit = p->item;
1258 struct patch_id *id;
1259 unsigned flags = commit->object.flags;
1260
1261 if (flags & BOUNDARY)
1262 continue;
1263 /*
1264 * If we have fewer left, left_first is set and we omit
1265 * commits on the left branch in this loop.
1266 */
1267 if (left_first == !!(flags & SYMMETRIC_LEFT))
1268 continue;
1269
1270 /*
1271 * Have we seen the same patch id?
1272 */
c9e3a4e7 1273 id = patch_id_iter_first(commit, &ids);
d7a17cad
JH
1274 if (!id)
1275 continue;
d7a17cad 1276
683f17ec 1277 commit->object.flags |= cherry_flag;
c9e3a4e7
JK
1278 do {
1279 id->commit->object.flags |= cherry_flag;
1280 } while ((id = patch_id_iter_next(id, &ids)));
d7a17cad
JH
1281 }
1282
1283 free_patch_ids(&ids);
1284}
1285
7d004199
LT
1286/* How many extra uninteresting commits we want to see.. */
1287#define SLOP 5
1288
dddbad72 1289static int still_interesting(struct commit_list *src, timestamp_t date, int slop,
b6e8a3b5 1290 struct commit **interesting_cache)
3131b713 1291{
7d004199
LT
1292 /*
1293 * No source list at all? We're definitely done..
1294 */
1295 if (!src)
1296 return 0;
1297
1298 /*
1299 * Does the destination list contain entries with a date
1300 * before the source list? Definitely _not_ done.
1301 */
c19d1b4e 1302 if (date <= src->item->date)
7d004199
LT
1303 return SLOP;
1304
1305 /*
1306 * Does the source list still have interesting commits in
1307 * it? Definitely not done..
1308 */
b6e8a3b5 1309 if (!everybody_uninteresting(src, interesting_cache))
7d004199
LT
1310 return SLOP;
1311
1312 /* Ok, we're closing in.. */
1313 return slop-1;
3131b713
LT
1314}
1315
ebdc94f3 1316/*
257418c5
EN
1317 * "rev-list --ancestry-path=C_0 [--ancestry-path=C_1 ...] A..B"
1318 * computes commits that are ancestors of B but not ancestors of A but
1319 * further limits the result to those that have any of C in their
1320 * ancestry path (i.e. are either ancestors of any of C, descendants
1321 * of any of C, or are any of C). If --ancestry-path is specified with
1322 * no commit, we use all bottom commits for C.
1323 *
1324 * Before this function is called, ancestors of C will have already
1325 * been marked with ANCESTRY_PATH previously.
1326 *
1327 * This takes the list of bottom commits and the result of "A..B"
1328 * without --ancestry-path, and limits the latter further to the ones
1329 * that have any of C in their ancestry path. Since the ancestors of C
1330 * have already been marked (a prerequisite of this function), we just
1331 * need to mark the descendants, then exclude any commit that does not
1332 * have any of these marks.
ebdc94f3 1333 */
257418c5 1334static void limit_to_ancestry(struct commit_list *bottoms, struct commit_list *list)
ebdc94f3
JH
1335{
1336 struct commit_list *p;
1337 struct commit_list *rlist = NULL;
1338 int made_progress;
1339
1340 /*
1341 * Reverse the list so that it will be likely that we would
1342 * process parents before children.
1343 */
1344 for (p = list; p; p = p->next)
1345 commit_list_insert(p->item, &rlist);
1346
257418c5 1347 for (p = bottoms; p; p = p->next)
ebdc94f3
JH
1348 p->item->object.flags |= TMP_MARK;
1349
1350 /*
1351 * Mark the ones that can reach bottom commits in "list",
1352 * in a bottom-up fashion.
1353 */
1354 do {
1355 made_progress = 0;
1356 for (p = rlist; p; p = p->next) {
1357 struct commit *c = p->item;
1358 struct commit_list *parents;
1359 if (c->object.flags & (TMP_MARK | UNINTERESTING))
1360 continue;
1361 for (parents = c->parents;
1362 parents;
1363 parents = parents->next) {
1364 if (!(parents->item->object.flags & TMP_MARK))
1365 continue;
1366 c->object.flags |= TMP_MARK;
1367 made_progress = 1;
1368 break;
1369 }
1370 }
1371 } while (made_progress);
1372
1373 /*
1374 * NEEDSWORK: decide if we want to remove parents that are
1375 * not marked with TMP_MARK from commit->parents for commits
1376 * in the resulting list. We may not want to do that, though.
1377 */
1378
1379 /*
257418c5
EN
1380 * The ones that are not marked with either TMP_MARK or
1381 * ANCESTRY_PATH are uninteresting
ebdc94f3
JH
1382 */
1383 for (p = list; p; p = p->next) {
1384 struct commit *c = p->item;
257418c5 1385 if (c->object.flags & (TMP_MARK | ANCESTRY_PATH))
ebdc94f3
JH
1386 continue;
1387 c->object.flags |= UNINTERESTING;
1388 }
1389
257418c5 1390 /* We are done with TMP_MARK and ANCESTRY_PATH */
ebdc94f3 1391 for (p = list; p; p = p->next)
257418c5
EN
1392 p->item->object.flags &= ~(TMP_MARK | ANCESTRY_PATH);
1393 for (p = bottoms; p; p = p->next)
1394 p->item->object.flags &= ~(TMP_MARK | ANCESTRY_PATH);
ebdc94f3
JH
1395 free_commit_list(rlist);
1396}
1397
1398/*
257418c5
EN
1399 * Before walking the history, add the set of "negative" refs the
1400 * caller has asked to exclude to the bottom list.
ebdc94f3
JH
1401 *
1402 * This is used to compute "rev-list --ancestry-path A..B", as we need
1403 * to filter the result of "A..B" further to the ones that can actually
1404 * reach A.
1405 */
257418c5
EN
1406static void collect_bottom_commits(struct commit_list *list,
1407 struct commit_list **bottom)
ebdc94f3 1408{
257418c5 1409 struct commit_list *elem;
7f34a46f
KB
1410 for (elem = list; elem; elem = elem->next)
1411 if (elem->item->object.flags & BOTTOM)
257418c5 1412 commit_list_insert(elem->item, bottom);
ebdc94f3
JH
1413}
1414
60adf7d7
MG
1415/* Assumes either left_only or right_only is set */
1416static void limit_left_right(struct commit_list *list, struct rev_info *revs)
1417{
1418 struct commit_list *p;
1419
1420 for (p = list; p; p = p->next) {
1421 struct commit *commit = p->item;
1422
1423 if (revs->right_only) {
1424 if (commit->object.flags & SYMMETRIC_LEFT)
1425 commit->object.flags |= SHOWN;
1426 } else /* revs->left_only is set */
1427 if (!(commit->object.flags & SYMMETRIC_LEFT))
1428 commit->object.flags |= SHOWN;
1429 }
1430}
1431
cc0e6c5a 1432static int limit_list(struct rev_info *revs)
a4a88b2b 1433{
7d004199 1434 int slop = SLOP;
dddbad72 1435 timestamp_t date = TIME_MAX;
db69bf60 1436 struct commit_list *original_list = revs->commits;
a4a88b2b
LT
1437 struct commit_list *newlist = NULL;
1438 struct commit_list **p = &newlist;
b6e8a3b5 1439 struct commit *interesting_cache = NULL;
ebdc94f3 1440
257418c5
EN
1441 if (revs->ancestry_path_implicit_bottoms) {
1442 collect_bottom_commits(original_list,
1443 &revs->ancestry_path_bottoms);
1444 if (!revs->ancestry_path_bottoms)
97b03c35 1445 die("--ancestry-path given but there are no bottom commits");
ebdc94f3 1446 }
a4a88b2b 1447
db69bf60
AH
1448 while (original_list) {
1449 struct commit *commit = pop_commit(&original_list);
a4a88b2b 1450 struct object *obj = &commit->object;
cdcefbc9 1451 show_early_output_fn_t show;
a4a88b2b 1452
b6e8a3b5
JK
1453 if (commit == interesting_cache)
1454 interesting_cache = NULL;
1455
a4a88b2b
LT
1456 if (revs->max_age != -1 && (commit->date < revs->max_age))
1457 obj->flags |= UNINTERESTING;
db69bf60 1458 if (process_parents(revs, commit, &original_list, NULL) < 0)
cc0e6c5a 1459 return -1;
a4a88b2b 1460 if (obj->flags & UNINTERESTING) {
9d505b7b 1461 mark_parents_uninteresting(revs, commit);
db69bf60 1462 slop = still_interesting(original_list, date, slop, &interesting_cache);
7d004199 1463 if (slop)
3131b713 1464 continue;
7d004199 1465 break;
a4a88b2b 1466 }
01faa91c
RS
1467 if (revs->min_age != -1 && (commit->date > revs->min_age) &&
1468 !revs->line_level_traverse)
a4a88b2b 1469 continue;
96697781
MV
1470 if (revs->max_age_as_filter != -1 &&
1471 (commit->date < revs->max_age_as_filter) && !revs->line_level_traverse)
1472 continue;
7d004199 1473 date = commit->date;
a4a88b2b 1474 p = &commit_list_insert(commit, p)->next;
cdcefbc9
LT
1475
1476 show = show_early_output;
1477 if (!show)
1478 continue;
1479
1480 show(revs, newlist);
1481 show_early_output = NULL;
a4a88b2b 1482 }
adbbb31e 1483 if (revs->cherry_pick || revs->cherry_mark)
36d56de6 1484 cherry_pick_list(newlist, revs);
d7a17cad 1485
60adf7d7
MG
1486 if (revs->left_only || revs->right_only)
1487 limit_left_right(newlist, revs);
1488
257418c5
EN
1489 if (revs->ancestry_path)
1490 limit_to_ancestry(revs->ancestry_path_bottoms, newlist);
ebdc94f3 1491
4d826608
KB
1492 /*
1493 * Check if any commits have become TREESAME by some of their parents
1494 * becoming UNINTERESTING.
1495 */
db69bf60
AH
1496 if (limiting_can_increase_treesame(revs)) {
1497 struct commit_list *list = NULL;
4d826608
KB
1498 for (list = newlist; list; list = list->next) {
1499 struct commit *c = list->item;
1500 if (c->object.flags & (UNINTERESTING | TREESAME))
1501 continue;
1502 update_treesame(revs, c);
1503 }
db69bf60 1504 }
4d826608 1505
db69bf60 1506 free_commit_list(original_list);
a4a88b2b 1507 revs->commits = newlist;
cc0e6c5a 1508 return 0;
a4a88b2b
LT
1509}
1510
df835d3a
MH
1511/*
1512 * Add an entry to refs->cmdline with the specified information.
1513 * *name is copied.
1514 */
281eee47
JH
1515static void add_rev_cmdline(struct rev_info *revs,
1516 struct object *item,
1517 const char *name,
1518 int whence,
1519 unsigned flags)
1520{
1521 struct rev_cmdline_info *info = &revs->cmdline;
071bcaab 1522 unsigned int nr = info->nr;
281eee47
JH
1523
1524 ALLOC_GROW(info->rev, nr + 1, info->alloc);
1525 info->rev[nr].item = item;
df835d3a 1526 info->rev[nr].name = xstrdup(name);
281eee47
JH
1527 info->rev[nr].whence = whence;
1528 info->rev[nr].flags = flags;
1529 info->nr++;
1530}
1531
a765499a
KB
1532static void add_rev_cmdline_list(struct rev_info *revs,
1533 struct commit_list *commit_list,
1534 int whence,
1535 unsigned flags)
1536{
1537 while (commit_list) {
1538 struct object *object = &commit_list->item->object;
f2fd0760 1539 add_rev_cmdline(revs, object, oid_to_hex(&object->oid),
a765499a
KB
1540 whence, flags);
1541 commit_list = commit_list->next;
1542 }
1543}
1544
1e9f273a 1545int ref_excluded(const struct ref_exclusions *exclusions, const char *path)
e7b432c5 1546{
8c1bc2a7 1547 const char *stripped_path = strip_namespace(path);
e7b432c5
JH
1548 struct string_list_item *item;
1549
1e9f273a 1550 for_each_string_list_item(item, &exclusions->excluded_refs) {
55d34269 1551 if (!wildmatch(item->string, path, 0))
e7b432c5
JH
1552 return 1;
1553 }
8c1bc2a7
PS
1554
1555 if (ref_is_hidden(stripped_path, path, &exclusions->hidden_refs))
1556 return 1;
1557
e7b432c5
JH
1558 return 0;
1559}
1560
1e9f273a 1561void init_ref_exclusions(struct ref_exclusions *exclusions)
05b94259 1562{
1e9f273a
PS
1563 struct ref_exclusions blank = REF_EXCLUSIONS_INIT;
1564 memcpy(exclusions, &blank, sizeof(*exclusions));
05b94259
PS
1565}
1566
1e9f273a 1567void clear_ref_exclusions(struct ref_exclusions *exclusions)
05b94259 1568{
1e9f273a 1569 string_list_clear(&exclusions->excluded_refs, 0);
c45841ff 1570 strvec_clear(&exclusions->hidden_refs);
8c1bc2a7 1571 exclusions->hidden_refs_configured = 0;
1e9f273a
PS
1572}
1573
1574void add_ref_exclusion(struct ref_exclusions *exclusions, const char *exclude)
1575{
1576 string_list_append(&exclusions->excluded_refs, exclude);
05b94259
PS
1577}
1578
8c1bc2a7
PS
1579struct exclude_hidden_refs_cb {
1580 struct ref_exclusions *exclusions;
1581 const char *section;
1582};
1583
a4e7e317
GC
1584static int hide_refs_config(const char *var, const char *value,
1585 const struct config_context *ctx UNUSED,
1586 void *cb_data)
8c1bc2a7
PS
1587{
1588 struct exclude_hidden_refs_cb *cb = cb_data;
1589 cb->exclusions->hidden_refs_configured = 1;
1590 return parse_hide_refs_config(var, value, cb->section,
1591 &cb->exclusions->hidden_refs);
1592}
1593
1594void exclude_hidden_refs(struct ref_exclusions *exclusions, const char *section)
1595{
1596 struct exclude_hidden_refs_cb cb;
1597
c6ce27ab
EW
1598 if (strcmp(section, "fetch") && strcmp(section, "receive") &&
1599 strcmp(section, "uploadpack"))
8c1bc2a7
PS
1600 die(_("unsupported section for hidden refs: %s"), section);
1601
1602 if (exclusions->hidden_refs_configured)
1603 die(_("--exclude-hidden= passed more than once"));
1604
1605 cb.exclusions = exclusions;
1606 cb.section = section;
1607
1608 git_config(hide_refs_config, &cb);
1609}
1610
05b94259
PS
1611struct all_refs_cb {
1612 int all_flags;
1613 int warned_bad_reflog;
1614 struct rev_info *all_revs;
1615 const char *name_for_errormsg;
1616 struct worktree *wt;
1617};
1618
a217dcbd 1619static int handle_one_ref(const char *path, const struct object_id *oid,
5cf88fd8 1620 int flag UNUSED,
63e14ee2 1621 void *cb_data)
ae563542 1622{
63049292 1623 struct all_refs_cb *cb = cb_data;
e7b432c5
JH
1624 struct object *object;
1625
1e9f273a 1626 if (ref_excluded(&cb->all_revs->ref_excludes, path))
e7b432c5
JH
1627 return 0;
1628
654b9a90 1629 object = get_reference(cb->all_revs, path, oid, cb->all_flags);
281eee47 1630 add_rev_cmdline(cb->all_revs, object, path, REV_CMD_REF, cb->all_flags);
bf9c0cbd 1631 add_pending_object(cb->all_revs, object, path);
ae563542
LT
1632 return 0;
1633}
1634
d08bae7e
IL
1635static void init_all_refs_cb(struct all_refs_cb *cb, struct rev_info *revs,
1636 unsigned flags)
1637{
1638 cb->all_revs = revs;
1639 cb->all_flags = flags;
7ba82629 1640 revs->rev_input_given = 1;
ab3e1f78 1641 cb->wt = NULL;
d08bae7e
IL
1642}
1643
073cf63c
NTND
1644static void handle_refs(struct ref_store *refs,
1645 struct rev_info *revs, unsigned flags,
1646 int (*for_each)(struct ref_store *, each_ref_fn, void *))
ae563542 1647{
63049292 1648 struct all_refs_cb cb;
073cf63c
NTND
1649
1650 if (!refs) {
1651 /* this could happen with uninitialized submodules */
1652 return;
1653 }
1654
d08bae7e 1655 init_all_refs_cb(&cb, revs, flags);
073cf63c 1656 for_each(refs, handle_one_ref, &cb);
63049292
JH
1657}
1658
9461d272 1659static void handle_one_reflog_commit(struct object_id *oid, void *cb_data)
63049292
JH
1660{
1661 struct all_refs_cb *cb = cb_data;
9461d272 1662 if (!is_null_oid(oid)) {
b3c7eef9 1663 struct object *o = parse_object(cb->all_revs->repo, oid);
71b03b42
SP
1664 if (o) {
1665 o->flags |= cb->all_flags;
281eee47 1666 /* ??? CMDLINEFLAGS ??? */
71b03b42
SP
1667 add_pending_object(cb->all_revs, o, "");
1668 }
1669 else if (!cb->warned_bad_reflog) {
46efd2d9 1670 warning("reflog of '%s' references pruned commits",
71b03b42
SP
1671 cb->name_for_errormsg);
1672 cb->warned_bad_reflog = 1;
1673 }
63049292 1674 }
71b03b42
SP
1675}
1676
9461d272 1677static int handle_one_reflog_ent(struct object_id *ooid, struct object_id *noid,
5cf88fd8
ÆAB
1678 const char *email UNUSED,
1679 timestamp_t timestamp UNUSED,
1680 int tz UNUSED,
1681 const char *message UNUSED,
c006e9fa 1682 void *cb_data)
71b03b42 1683{
9461d272 1684 handle_one_reflog_commit(ooid, cb_data);
1685 handle_one_reflog_commit(noid, cb_data);
63049292
JH
1686 return 0;
1687}
1688
ab3e1f78 1689static int handle_one_reflog(const char *refname_in_wt,
5cf88fd8
ÆAB
1690 const struct object_id *oid UNUSED,
1691 int flag UNUSED, void *cb_data)
63049292
JH
1692{
1693 struct all_refs_cb *cb = cb_data;
ab3e1f78
NTND
1694 struct strbuf refname = STRBUF_INIT;
1695
71b03b42 1696 cb->warned_bad_reflog = 0;
ab3e1f78
NTND
1697 strbuf_worktree_ref(cb->wt, &refname, refname_in_wt);
1698 cb->name_for_errormsg = refname.buf;
1699 refs_for_each_reflog_ent(get_main_ref_store(the_repository),
1700 refname.buf,
acd9544a 1701 handle_one_reflog_ent, cb_data);
ab3e1f78 1702 strbuf_release(&refname);
63049292
JH
1703 return 0;
1704}
1705
acd9544a
NTND
1706static void add_other_reflogs_to_pending(struct all_refs_cb *cb)
1707{
1708 struct worktree **worktrees, **p;
1709
03f2465b 1710 worktrees = get_worktrees();
acd9544a
NTND
1711 for (p = worktrees; *p; p++) {
1712 struct worktree *wt = *p;
1713
1714 if (wt->is_current)
1715 continue;
1716
ab3e1f78
NTND
1717 cb->wt = wt;
1718 refs_for_each_reflog(get_worktree_ref_store(wt),
acd9544a
NTND
1719 handle_one_reflog,
1720 cb);
1721 }
1722 free_worktrees(worktrees);
1723}
1724
718ccc97 1725void add_reflogs_to_pending(struct rev_info *revs, unsigned flags)
63049292
JH
1726{
1727 struct all_refs_cb cb;
2b2a5be3 1728
63049292
JH
1729 cb.all_revs = revs;
1730 cb.all_flags = flags;
ab3e1f78 1731 cb.wt = NULL;
a89caf4b 1732 for_each_reflog(handle_one_reflog, &cb);
acd9544a
NTND
1733
1734 if (!revs->single_worktree)
1735 add_other_reflogs_to_pending(&cb);
ae563542
LT
1736}
1737
4fe10219 1738static void add_cache_tree(struct cache_tree *it, struct rev_info *revs,
b4cfcde4 1739 struct strbuf *path, unsigned int flags)
4fe10219
JK
1740{
1741 size_t baselen = path->len;
1742 int i;
1743
1744 if (it->entry_count >= 0) {
b3c7eef9 1745 struct tree *tree = lookup_tree(revs->repo, &it->oid);
b4cfcde4 1746 tree->object.flags |= flags;
4fe10219
JK
1747 add_pending_object_with_path(revs, &tree->object, "",
1748 040000, path->buf);
1749 }
1750
1751 for (i = 0; i < it->subtree_nr; i++) {
1752 struct cache_tree_sub *sub = it->down[i];
1753 strbuf_addf(path, "%s%s", baselen ? "/" : "", sub->name);
b4cfcde4 1754 add_cache_tree(sub->cache_tree, revs, path, flags);
4fe10219
JK
1755 strbuf_setlen(path, baselen);
1756 }
1757
1758}
1759
5a5ea141
JH
1760static void add_resolve_undo_to_pending(struct index_state *istate, struct rev_info *revs)
1761{
1762 struct string_list_item *item;
1763 struct string_list *resolve_undo = istate->resolve_undo;
1764
1765 if (!resolve_undo)
1766 return;
1767
1768 for_each_string_list_item(item, resolve_undo) {
1769 const char *path = item->string;
1770 struct resolve_undo_info *ru = item->util;
1771 int i;
1772
1773 if (!ru)
1774 continue;
1775 for (i = 0; i < 3; i++) {
1776 struct blob *blob;
1777
1778 if (!ru->mode[i] || !S_ISREG(ru->mode[i]))
1779 continue;
1780
1781 blob = lookup_blob(revs->repo, &ru->oid[i]);
1782 if (!blob) {
1783 warning(_("resolve-undo records `%s` which is missing"),
1784 oid_to_hex(&ru->oid[i]));
1785 continue;
1786 }
1787 add_pending_object_with_path(revs, &blob->object, "",
1788 ru->mode[i], path);
1789 }
1790 }
1791}
1792
6c3d8181 1793static void do_add_index_objects_to_pending(struct rev_info *revs,
b4cfcde4
JK
1794 struct index_state *istate,
1795 unsigned int flags)
4fe10219
JK
1796{
1797 int i;
1798
f5fed74f
DS
1799 /* TODO: audit for interaction with sparse-index. */
1800 ensure_full_index(istate);
6c3d8181
NTND
1801 for (i = 0; i < istate->cache_nr; i++) {
1802 struct cache_entry *ce = istate->cache[i];
4fe10219
JK
1803 struct blob *blob;
1804
1805 if (S_ISGITLINK(ce->ce_mode))
1806 continue;
1807
b3c7eef9 1808 blob = lookup_blob(revs->repo, &ce->oid);
4fe10219
JK
1809 if (!blob)
1810 die("unable to add index blob to traversal");
b4cfcde4 1811 blob->object.flags |= flags;
4fe10219
JK
1812 add_pending_object_with_path(revs, &blob->object, "",
1813 ce->ce_mode, ce->name);
1814 }
1815
6c3d8181 1816 if (istate->cache_tree) {
4fe10219 1817 struct strbuf path = STRBUF_INIT;
b4cfcde4 1818 add_cache_tree(istate->cache_tree, revs, &path, flags);
4fe10219
JK
1819 strbuf_release(&path);
1820 }
5a5ea141
JH
1821
1822 add_resolve_undo_to_pending(istate, revs);
4fe10219
JK
1823}
1824
6c3d8181
NTND
1825void add_index_objects_to_pending(struct rev_info *revs, unsigned int flags)
1826{
be489d02
NTND
1827 struct worktree **worktrees, **p;
1828
e1ff0a32 1829 repo_read_index(revs->repo);
b4cfcde4 1830 do_add_index_objects_to_pending(revs, revs->repo->index, flags);
be489d02
NTND
1831
1832 if (revs->single_worktree)
1833 return;
1834
03f2465b 1835 worktrees = get_worktrees();
be489d02
NTND
1836 for (p = worktrees; *p; p++) {
1837 struct worktree *wt = *p;
6269f8ea 1838 struct index_state istate = INDEX_STATE_INIT(revs->repo);
be489d02
NTND
1839
1840 if (wt->is_current)
1841 continue; /* current index already taken care of */
1842
1843 if (read_index_from(&istate,
a125a223
TG
1844 worktree_git_path(wt, "index"),
1845 get_worktree_git_dir(wt)) > 0)
b4cfcde4 1846 do_add_index_objects_to_pending(revs, &istate, flags);
be489d02
NTND
1847 discard_index(&istate);
1848 }
1849 free_worktrees(worktrees);
6c3d8181
NTND
1850}
1851
39b44ba7
JK
1852struct add_alternate_refs_data {
1853 struct rev_info *revs;
1854 unsigned int flags;
1855};
1856
1857static void add_one_alternate_ref(const struct object_id *oid,
1858 void *vdata)
1859{
1860 const char *name = ".alternate";
1861 struct add_alternate_refs_data *data = vdata;
1862 struct object *obj;
1863
1864 obj = get_reference(data->revs, name, oid, data->flags);
1865 add_rev_cmdline(data->revs, obj, name, REV_CMD_REV, data->flags);
1866 add_pending_object(data->revs, obj, name);
1867}
1868
1869static void add_alternate_refs_to_pending(struct rev_info *revs,
1870 unsigned int flags)
1871{
1872 struct add_alternate_refs_data data;
1873 data.revs = revs;
1874 data.flags = flags;
1875 for_each_alternate_ref(add_one_alternate_ref, &data);
1876}
1877
8779351d
VN
1878static int add_parents_only(struct rev_info *revs, const char *arg_, int flags,
1879 int exclude_parent)
ea4a19e1 1880{
654b9a90 1881 struct object_id oid;
ea4a19e1
JH
1882 struct object *it;
1883 struct commit *commit;
1884 struct commit_list *parents;
8779351d 1885 int parent_number;
281eee47 1886 const char *arg = arg_;
ea4a19e1
JH
1887
1888 if (*arg == '^') {
7f34a46f 1889 flags ^= UNINTERESTING | BOTTOM;
ea4a19e1
JH
1890 arg++;
1891 }
d850b7a5 1892 if (repo_get_oid_committish(the_repository, arg, &oid))
ea4a19e1
JH
1893 return 0;
1894 while (1) {
654b9a90 1895 it = get_reference(revs, arg, &oid, 0);
cc243c3c
JH
1896 if (!it && revs->ignore_missing)
1897 return 0;
1974632c 1898 if (it->type != OBJ_TAG)
ea4a19e1 1899 break;
9684afd9
MK
1900 if (!((struct tag*)it)->tagged)
1901 return 0;
654b9a90 1902 oidcpy(&oid, &((struct tag*)it)->tagged->oid);
ea4a19e1 1903 }
1974632c 1904 if (it->type != OBJ_COMMIT)
ea4a19e1
JH
1905 return 0;
1906 commit = (struct commit *)it;
8779351d
VN
1907 if (exclude_parent &&
1908 exclude_parent > commit_list_count(commit->parents))
1909 return 0;
1910 for (parents = commit->parents, parent_number = 1;
1911 parents;
1912 parents = parents->next, parent_number++) {
1913 if (exclude_parent && parent_number != exclude_parent)
1914 continue;
1915
ea4a19e1
JH
1916 it = &parents->item->object;
1917 it->flags |= flags;
281eee47 1918 add_rev_cmdline(revs, it, arg_, REV_CMD_PARENTS_ONLY, flags);
ea4a19e1
JH
1919 add_pending_object(revs, it, arg);
1920 }
1921 return 1;
1922}
1923
2abf3503
NTND
1924void repo_init_revisions(struct repository *r,
1925 struct rev_info *revs,
1926 const char *prefix)
8efdc326 1927{
916ebb32
ÆAB
1928 struct rev_info blank = REV_INFO_INIT;
1929 memcpy(revs, &blank, sizeof(*revs));
8e8f9987 1930
2abf3503 1931 revs->repo = r;
67022e02 1932 revs->pruning.repo = r;
cd2bdc53
LT
1933 revs->pruning.add_remove = file_add_remove;
1934 revs->pruning.change = file_change;
a937b37e 1935 revs->pruning.change_fn_data = revs;
db6296a5 1936 revs->prefix = prefix;
cd2bdc53 1937
9725c8dd 1938 grep_init(&revs->grep_filter, revs->repo);
0843acfd 1939 revs->grep_filter.status_only = 1;
0843acfd 1940
2abf3503 1941 repo_diff_setup(revs->repo, &revs->diffopt);
c0cb4a06 1942 if (prefix && !revs->diffopt.prefix) {
cd676a51
JH
1943 revs->diffopt.prefix = prefix;
1944 revs->diffopt.prefix_length = strlen(prefix);
1945 }
3a03cf6b 1946
e6e230ee 1947 init_display_notes(&revs->notes_opt);
2a01bded 1948 list_objects_filter_init(&revs->filter);
1e9f273a 1949 init_ref_exclusions(&revs->ref_excludes);
8efdc326
FK
1950}
1951
0d2c9d67 1952static void add_pending_commit_list(struct rev_info *revs,
ec36c42a
NTND
1953 struct commit_list *commit_list,
1954 unsigned int flags)
0d2c9d67
RS
1955{
1956 while (commit_list) {
1957 struct object *object = &commit_list->item->object;
1958 object->flags |= flags;
f2fd0760 1959 add_pending_object(revs, object, oid_to_hex(&object->oid));
0d2c9d67
RS
1960 commit_list = commit_list->next;
1961 }
1962}
1963
f3fc5d9c
ML
1964static const char *lookup_other_head(struct object_id *oid)
1965{
1966 int i;
1967 static const char *const other_head[] = {
1968 "MERGE_HEAD", "CHERRY_PICK_HEAD", "REVERT_HEAD", "REBASE_HEAD"
1969 };
1970
1971 for (i = 0; i < ARRAY_SIZE(other_head); i++)
1972 if (!read_ref_full(other_head[i],
1973 RESOLVE_REF_READING | RESOLVE_REF_NO_RECURSE,
1974 oid, NULL)) {
1975 if (is_null_oid(oid))
1976 die(_("%s exists but is a symbolic ref"), other_head[i]);
1977 return other_head[i];
1978 }
1979
1980 die(_("--merge requires one of the pseudorefs MERGE_HEAD, CHERRY_PICK_HEAD, REVERT_HEAD or REBASE_HEAD"));
1981}
1982
ae3e5e1e
JH
1983static void prepare_show_merge(struct rev_info *revs)
1984{
1985 struct commit_list *bases;
1986 struct commit *head, *other;
68ab61dd 1987 struct object_id oid;
f3fc5d9c 1988 const char *other_name;
ae3e5e1e
JH
1989 const char **prune = NULL;
1990 int i, prune_num = 1; /* counting terminating NULL */
2abf3503 1991 struct index_state *istate = revs->repo->index;
ae3e5e1e 1992
d850b7a5 1993 if (repo_get_oid(the_repository, "HEAD", &oid))
ae3e5e1e 1994 die("--merge without HEAD?");
bc83266a 1995 head = lookup_commit_or_die(&oid, "HEAD");
f3fc5d9c
ML
1996 other_name = lookup_other_head(&oid);
1997 other = lookup_commit_or_die(&oid, other_name);
ae3e5e1e 1998 add_pending_object(revs, &head->object, "HEAD");
f3fc5d9c 1999 add_pending_object(revs, &other->object, other_name);
cb338c23 2000 bases = repo_get_merge_bases(the_repository, head, other);
7f34a46f
KB
2001 add_rev_cmdline_list(revs, bases, REV_CMD_MERGE_BASE, UNINTERESTING | BOTTOM);
2002 add_pending_commit_list(revs, bases, UNINTERESTING | BOTTOM);
e82447b1
JH
2003 free_commit_list(bases);
2004 head->object.flags |= SYMMETRIC_LEFT;
ae3e5e1e 2005
2abf3503 2006 if (!istate->cache_nr)
e1ff0a32 2007 repo_read_index(revs->repo);
2abf3503
NTND
2008 for (i = 0; i < istate->cache_nr; i++) {
2009 const struct cache_entry *ce = istate->cache[i];
ae3e5e1e
JH
2010 if (!ce_stage(ce))
2011 continue;
2abf3503 2012 if (ce_path_match(istate, ce, &revs->prune_data, NULL)) {
ae3e5e1e 2013 prune_num++;
2756ca43 2014 REALLOC_ARRAY(prune, prune_num);
ae3e5e1e
JH
2015 prune[prune_num-2] = ce->name;
2016 prune[prune_num-1] = NULL;
2017 }
2abf3503
NTND
2018 while ((i+1 < istate->cache_nr) &&
2019 ce_same_name(ce, istate->cache[i+1]))
ae3e5e1e
JH
2020 i++;
2021 }
ed6e8038 2022 clear_pathspec(&revs->prune_data);
4a2d5ae2 2023 parse_pathspec(&revs->prune_data, PATHSPEC_ALL_MAGIC & ~PATHSPEC_LITERAL,
c6f1b920 2024 PATHSPEC_PREFER_FULL | PATHSPEC_LITERAL_PATH, "", prune);
e82447b1 2025 revs->limited = 1;
ae3e5e1e
JH
2026}
2027
62faad5a
JK
2028static int dotdot_missing(const char *arg, char *dotdot,
2029 struct rev_info *revs, int symmetric)
2030{
2031 if (revs->ignore_missing)
2032 return 0;
2033 /* de-munge so we report the full argument */
2034 *dotdot = '.';
2035 die(symmetric
2036 ? "Invalid symmetric difference expression %s"
2037 : "Invalid revision range %s", arg);
2038}
2039
2040static int handle_dotdot_1(const char *arg, char *dotdot,
2041 struct rev_info *revs, int flags,
18f1ad76
JK
2042 int cant_be_filename,
2043 struct object_context *a_oc,
2044 struct object_context *b_oc)
62faad5a
JK
2045{
2046 const char *a_name, *b_name;
2047 struct object_id a_oid, b_oid;
2048 struct object *a_obj, *b_obj;
2049 unsigned int a_flags, b_flags;
2050 int symmetric = 0;
2051 unsigned int flags_exclude = flags ^ (UNINTERESTING | BOTTOM);
321c89bf 2052 unsigned int oc_flags = GET_OID_COMMITTISH | GET_OID_RECORD_PATH;
62faad5a
JK
2053
2054 a_name = arg;
2055 if (!*a_name)
2056 a_name = "HEAD";
2057
2058 b_name = dotdot + 2;
2059 if (*b_name == '.') {
2060 symmetric = 1;
2061 b_name++;
2062 }
2063 if (!*b_name)
2064 b_name = "HEAD";
2065
3a7a698e
NTND
2066 if (get_oid_with_context(revs->repo, a_name, oc_flags, &a_oid, a_oc) ||
2067 get_oid_with_context(revs->repo, b_name, oc_flags, &b_oid, b_oc))
62faad5a
JK
2068 return -1;
2069
2070 if (!cant_be_filename) {
2071 *dotdot = '.';
2072 verify_non_filename(revs->prefix, arg);
2073 *dotdot = '\0';
2074 }
2075
b3c7eef9
NTND
2076 a_obj = parse_object(revs->repo, &a_oid);
2077 b_obj = parse_object(revs->repo, &b_oid);
62faad5a
JK
2078 if (!a_obj || !b_obj)
2079 return dotdot_missing(arg, dotdot, revs, symmetric);
2080
2081 if (!symmetric) {
2082 /* just A..B */
2083 b_flags = flags;
2084 a_flags = flags_exclude;
2085 } else {
2086 /* A...B -- find merge bases between the two */
2087 struct commit *a, *b;
2088 struct commit_list *exclude;
2089
b3c7eef9
NTND
2090 a = lookup_commit_reference(revs->repo, &a_obj->oid);
2091 b = lookup_commit_reference(revs->repo, &b_obj->oid);
62faad5a
JK
2092 if (!a || !b)
2093 return dotdot_missing(arg, dotdot, revs, symmetric);
2094
cb338c23 2095 exclude = repo_get_merge_bases(the_repository, a, b);
62faad5a
JK
2096 add_rev_cmdline_list(revs, exclude, REV_CMD_MERGE_BASE,
2097 flags_exclude);
2098 add_pending_commit_list(revs, exclude, flags_exclude);
2099 free_commit_list(exclude);
2100
2101 b_flags = flags;
2102 a_flags = flags | SYMMETRIC_LEFT;
2103 }
2104
2105 a_obj->flags |= a_flags;
2106 b_obj->flags |= b_flags;
2107 add_rev_cmdline(revs, a_obj, a_name, REV_CMD_LEFT, a_flags);
2108 add_rev_cmdline(revs, b_obj, b_name, REV_CMD_RIGHT, b_flags);
18f1ad76
JK
2109 add_pending_object_with_path(revs, a_obj, a_name, a_oc->mode, a_oc->path);
2110 add_pending_object_with_path(revs, b_obj, b_name, b_oc->mode, b_oc->path);
62faad5a
JK
2111 return 0;
2112}
2113
2114static int handle_dotdot(const char *arg,
2115 struct rev_info *revs, int flags,
2116 int cant_be_filename)
2117{
18f1ad76 2118 struct object_context a_oc, b_oc;
62faad5a
JK
2119 char *dotdot = strstr(arg, "..");
2120 int ret;
2121
2122 if (!dotdot)
2123 return -1;
2124
18f1ad76
JK
2125 memset(&a_oc, 0, sizeof(a_oc));
2126 memset(&b_oc, 0, sizeof(b_oc));
2127
62faad5a 2128 *dotdot = '\0';
18f1ad76
JK
2129 ret = handle_dotdot_1(arg, dotdot, revs, flags, cant_be_filename,
2130 &a_oc, &b_oc);
62faad5a
JK
2131 *dotdot = '.';
2132
18f1ad76
JK
2133 free(a_oc.path);
2134 free(b_oc.path);
2135
62faad5a
JK
2136 return ret;
2137}
2138
04a0e985 2139static int handle_revision_arg_1(const char *arg_, struct rev_info *revs, int flags, unsigned revarg_opt)
5d6f0935 2140{
249c8f4a 2141 struct object_context oc;
f632dedd 2142 char *mark;
5d6f0935 2143 struct object *object;
654b9a90 2144 struct object_id oid;
5d6f0935 2145 int local_flags;
281eee47 2146 const char *arg = arg_;
8e676e8b 2147 int cant_be_filename = revarg_opt & REVARG_CANNOT_BE_FILENAME;
321c89bf 2148 unsigned get_sha1_flags = GET_OID_RECORD_PATH;
5d6f0935 2149
7f34a46f
KB
2150 flags = flags & UNINTERESTING ? flags | BOTTOM : flags & ~BOTTOM;
2151
d89797fe
JK
2152 if (!cant_be_filename && !strcmp(arg, "..")) {
2153 /*
2154 * Just ".."? That is not a range but the
2155 * pathspec for the parent directory.
2156 */
2157 return -1;
5d6f0935 2158 }
8779351d 2159
62faad5a
JK
2160 if (!handle_dotdot(arg, revs, flags, revarg_opt))
2161 return 0;
8779351d 2162
f632dedd
JK
2163 mark = strstr(arg, "^@");
2164 if (mark && !mark[2]) {
2165 *mark = 0;
8779351d 2166 if (add_parents_only(revs, arg, flags, 0))
5d6f0935 2167 return 0;
f632dedd 2168 *mark = '^';
5d6f0935 2169 }
f632dedd
JK
2170 mark = strstr(arg, "^!");
2171 if (mark && !mark[2]) {
2172 *mark = 0;
8779351d 2173 if (!add_parents_only(revs, arg, flags ^ (UNINTERESTING | BOTTOM), 0))
f632dedd 2174 *mark = '^';
8779351d 2175 }
f632dedd
JK
2176 mark = strstr(arg, "^-");
2177 if (mark) {
8779351d
VN
2178 int exclude_parent = 1;
2179
f632dedd 2180 if (mark[2]) {
793c2118
RS
2181 if (strtol_i(mark + 2, 10, &exclude_parent) ||
2182 exclude_parent < 1)
8779351d
VN
2183 return -1;
2184 }
2185
f632dedd 2186 *mark = 0;
8779351d 2187 if (!add_parents_only(revs, arg, flags ^ (UNINTERESTING | BOTTOM), exclude_parent))
f632dedd 2188 *mark = '^';
62476c8e
JH
2189 }
2190
5d6f0935
JH
2191 local_flags = 0;
2192 if (*arg == '^') {
7f34a46f 2193 local_flags = UNINTERESTING | BOTTOM;
5d6f0935
JH
2194 arg++;
2195 }
d5f6b1d7
JH
2196
2197 if (revarg_opt & REVARG_COMMITTISH)
321c89bf 2198 get_sha1_flags |= GET_OID_COMMITTISH;
d5f6b1d7 2199
3a7a698e 2200 if (get_oid_with_context(revs->repo, arg, get_sha1_flags, &oid, &oc))
cc243c3c 2201 return revs->ignore_missing ? 0 : -1;
5d6f0935
JH
2202 if (!cant_be_filename)
2203 verify_non_filename(revs->prefix, arg);
654b9a90 2204 object = get_reference(revs, arg, &oid, flags ^ local_flags);
4cf67869
MD
2205 if (!object)
2206 return revs->ignore_missing ? 0 : -1;
281eee47 2207 add_rev_cmdline(revs, object, arg_, REV_CMD_REV, flags ^ local_flags);
18f1ad76
JK
2208 add_pending_object_with_path(revs, object, arg, oc.mode, oc.path);
2209 free(oc.path);
5d6f0935
JH
2210 return 0;
2211}
2212
04a0e985
JK
2213int handle_revision_arg(const char *arg, struct rev_info *revs, int flags, unsigned revarg_opt)
2214{
2215 int ret = handle_revision_arg_1(arg, revs, flags, revarg_opt);
2216 if (!ret)
2217 revs->rev_input_given = 1;
2218 return ret;
2219}
2220
91633995 2221static void read_pathspec_from_stdin(struct strbuf *sb,
c972bf4c 2222 struct strvec *prune)
4da5af31 2223{
7fa3c2ad 2224 while (strbuf_getline(sb, stdin) != EOF)
c972bf4c 2225 strvec_push(prune, sb->buf);
60da8b15
JH
2226}
2227
2d10c555 2228static void add_grep(struct rev_info *revs, const char *ptn, enum grep_pat_token what)
bd95fcd3 2229{
0843acfd 2230 append_grep_pattern(&revs->grep_filter, ptn, "command line", 0, what);
2d10c555
JH
2231}
2232
a4d7d2c6 2233static void add_header_grep(struct rev_info *revs, enum grep_header_field field, const char *pattern)
2d10c555 2234{
a4d7d2c6 2235 append_header_grep_pattern(&revs->grep_filter, field, pattern);
bd95fcd3
JH
2236}
2237
2238static void add_message_grep(struct rev_info *revs, const char *pattern)
2239{
2d10c555 2240 add_grep(revs, pattern, GREP_PATTERN_BODY);
bd95fcd3
JH
2241}
2242
71a1e948
JH
2243static int parse_count(const char *arg)
2244{
2245 int count;
2246
2247 if (strtol_i(arg, 10, &count) < 0)
2248 die("'%s': not an integer", arg);
2249 return count;
2250}
2251
2252static timestamp_t parse_age(const char *arg)
2253{
2254 timestamp_t num;
2255 char *p;
2256
2257 errno = 0;
2258 num = parse_timestamp(arg, &p, 10);
2259 if (errno || *p || p == arg)
2260 die("'%s': not a number of seconds since epoch", arg);
2261 return num;
2262}
2263
6b61ec05 2264static int handle_revision_opt(struct rev_info *revs, int argc, const char **argv,
bbcde41a
MD
2265 int *unkc, const char **unkv,
2266 const struct setup_revision_opt* opt)
02e54220
PH
2267{
2268 const char *arg = argv[0];
257418c5 2269 const char *optarg = NULL;
7d7b86f7 2270 int argcount;
fd521245 2271 const unsigned hexsz = the_hash_algo->hexsz;
02e54220
PH
2272
2273 /* pseudo revision arguments */
2274 if (!strcmp(arg, "--all") || !strcmp(arg, "--branches") ||
2275 !strcmp(arg, "--tags") || !strcmp(arg, "--remotes") ||
2276 !strcmp(arg, "--reflog") || !strcmp(arg, "--not") ||
ad3f9a71 2277 !strcmp(arg, "--no-walk") || !strcmp(arg, "--do-walk") ||
59556548 2278 !strcmp(arg, "--bisect") || starts_with(arg, "--glob=") ||
4fe10219 2279 !strcmp(arg, "--indexed-objects") ||
39b44ba7 2280 !strcmp(arg, "--alternate-refs") ||
8c1bc2a7 2281 starts_with(arg, "--exclude=") || starts_with(arg, "--exclude-hidden=") ||
59556548
CC
2282 starts_with(arg, "--branches=") || starts_with(arg, "--tags=") ||
2283 starts_with(arg, "--remotes=") || starts_with(arg, "--no-walk="))
02e54220
PH
2284 {
2285 unkv[(*unkc)++] = arg;
0fe8c138 2286 return 1;
02e54220
PH
2287 }
2288
7d7b86f7 2289 if ((argcount = parse_long_opt("max-count", argv, &optarg))) {
71a1e948 2290 revs->max_count = parse_count(optarg);
5853caec 2291 revs->no_walk = 0;
7d7b86f7
MM
2292 return argcount;
2293 } else if ((argcount = parse_long_opt("skip", argv, &optarg))) {
71a1e948 2294 revs->skip_count = parse_count(optarg);
7d7b86f7 2295 return argcount;
02e54220 2296 } else if ((*arg == '-') && isdigit(arg[1])) {
e3fa568c 2297 /* accept -<digit>, like traditional "head" */
71a1e948 2298 revs->max_count = parse_count(arg + 1);
5853caec 2299 revs->no_walk = 0;
02e54220
PH
2300 } else if (!strcmp(arg, "-n")) {
2301 if (argc <= 1)
2302 return error("-n requires an argument");
71a1e948 2303 revs->max_count = parse_count(argv[1]);
5853caec 2304 revs->no_walk = 0;
02e54220 2305 return 2;
479b3d97 2306 } else if (skip_prefix(arg, "-n", &optarg)) {
71a1e948 2307 revs->max_count = parse_count(optarg);
5853caec 2308 revs->no_walk = 0;
7d7b86f7 2309 } else if ((argcount = parse_long_opt("max-age", argv, &optarg))) {
71a1e948 2310 revs->max_age = parse_age(optarg);
7d7b86f7
MM
2311 return argcount;
2312 } else if ((argcount = parse_long_opt("since", argv, &optarg))) {
2313 revs->max_age = approxidate(optarg);
2314 return argcount;
96697781
MV
2315 } else if ((argcount = parse_long_opt("since-as-filter", argv, &optarg))) {
2316 revs->max_age_as_filter = approxidate(optarg);
2317 return argcount;
7d7b86f7
MM
2318 } else if ((argcount = parse_long_opt("after", argv, &optarg))) {
2319 revs->max_age = approxidate(optarg);
2320 return argcount;
2321 } else if ((argcount = parse_long_opt("min-age", argv, &optarg))) {
71a1e948 2322 revs->min_age = parse_age(optarg);
7d7b86f7
MM
2323 return argcount;
2324 } else if ((argcount = parse_long_opt("before", argv, &optarg))) {
2325 revs->min_age = approxidate(optarg);
2326 return argcount;
2327 } else if ((argcount = parse_long_opt("until", argv, &optarg))) {
2328 revs->min_age = approxidate(optarg);
2329 return argcount;
02e54220
PH
2330 } else if (!strcmp(arg, "--first-parent")) {
2331 revs->first_parent_only = 1;
9d505b7b
JZ
2332 } else if (!strcmp(arg, "--exclude-first-parent-only")) {
2333 revs->exclude_first_parent_only = 1;
ebdc94f3
JH
2334 } else if (!strcmp(arg, "--ancestry-path")) {
2335 revs->ancestry_path = 1;
cb7529e1 2336 revs->simplify_history = 0;
ebdc94f3 2337 revs->limited = 1;
257418c5
EN
2338 revs->ancestry_path_implicit_bottoms = 1;
2339 } else if (skip_prefix(arg, "--ancestry-path=", &optarg)) {
2340 struct commit *c;
2341 struct object_id oid;
2342 const char *msg = _("could not get commit for ancestry-path argument %s");
2343
2344 revs->ancestry_path = 1;
2345 revs->simplify_history = 0;
2346 revs->limited = 1;
2347
2348 if (repo_get_oid_committish(revs->repo, optarg, &oid))
2349 return error(msg, optarg);
2350 get_reference(revs, optarg, &oid, ANCESTRY_PATH);
2351 c = lookup_commit_reference(revs->repo, &oid);
2352 if (!c)
2353 return error(msg, optarg);
2354 commit_list_insert(c, &revs->ancestry_path_bottoms);
02e54220
PH
2355 } else if (!strcmp(arg, "-g") || !strcmp(arg, "--walk-reflogs")) {
2356 init_reflog_walk(&revs->reflog_info);
2357 } else if (!strcmp(arg, "--default")) {
2358 if (argc <= 1)
2359 return error("bad --default argument");
2360 revs->def = argv[1];
2361 return 2;
2362 } else if (!strcmp(arg, "--merge")) {
2363 revs->show_merge = 1;
2364 } else if (!strcmp(arg, "--topo-order")) {
08f704f2 2365 revs->sort_order = REV_SORT_IN_GRAPH_ORDER;
02e54220 2366 revs->topo_order = 1;
6546b593
JH
2367 } else if (!strcmp(arg, "--simplify-merges")) {
2368 revs->simplify_merges = 1;
a52f0071 2369 revs->topo_order = 1;
6546b593
JH
2370 revs->rewrite_parents = 1;
2371 revs->simplify_history = 0;
2372 revs->limited = 1;
78892e32
LT
2373 } else if (!strcmp(arg, "--simplify-by-decoration")) {
2374 revs->simplify_merges = 1;
a52f0071 2375 revs->topo_order = 1;
78892e32
LT
2376 revs->rewrite_parents = 1;
2377 revs->simplify_history = 0;
2378 revs->simplify_by_decoration = 1;
2379 revs->limited = 1;
2380 revs->prune = 1;
02e54220 2381 } else if (!strcmp(arg, "--date-order")) {
08f704f2 2382 revs->sort_order = REV_SORT_BY_COMMIT_DATE;
02e54220 2383 revs->topo_order = 1;
81c6b38b
JH
2384 } else if (!strcmp(arg, "--author-date-order")) {
2385 revs->sort_order = REV_SORT_BY_AUTHOR_DATE;
02e54220 2386 revs->topo_order = 1;
dffc651e
SG
2387 } else if (!strcmp(arg, "--early-output")) {
2388 revs->early_output = 100;
2389 revs->topo_order = 1;
2390 } else if (skip_prefix(arg, "--early-output=", &optarg)) {
2391 if (strtoul_ui(optarg, 10, &revs->early_output) < 0)
2392 die("'%s': not a non-negative integer", optarg);
2393 revs->topo_order = 1;
02e54220
PH
2394 } else if (!strcmp(arg, "--parents")) {
2395 revs->rewrite_parents = 1;
2396 revs->print_parents = 1;
2397 } else if (!strcmp(arg, "--dense")) {
2398 revs->dense = 1;
2399 } else if (!strcmp(arg, "--sparse")) {
2400 revs->dense = 0;
ce5b6f9b
SB
2401 } else if (!strcmp(arg, "--in-commit-order")) {
2402 revs->tree_blobs_in_commit_order = 1;
02e54220
PH
2403 } else if (!strcmp(arg, "--remove-empty")) {
2404 revs->remove_empty_trees = 1;
b8e8db28 2405 } else if (!strcmp(arg, "--merges")) {
ad5aeede 2406 revs->min_parents = 2;
02e54220 2407 } else if (!strcmp(arg, "--no-merges")) {
ad5aeede 2408 revs->max_parents = 1;
479b3d97 2409 } else if (skip_prefix(arg, "--min-parents=", &optarg)) {
71a1e948 2410 revs->min_parents = parse_count(optarg);
9ada7aee 2411 } else if (!strcmp(arg, "--no-min-parents")) {
ad5aeede 2412 revs->min_parents = 0;
479b3d97 2413 } else if (skip_prefix(arg, "--max-parents=", &optarg)) {
71a1e948 2414 revs->max_parents = parse_count(optarg);
9ada7aee 2415 } else if (!strcmp(arg, "--no-max-parents")) {
ad5aeede 2416 revs->max_parents = -1;
02e54220
PH
2417 } else if (!strcmp(arg, "--boundary")) {
2418 revs->boundary = 1;
2419 } else if (!strcmp(arg, "--left-right")) {
2420 revs->left_right = 1;
60adf7d7 2421 } else if (!strcmp(arg, "--left-only")) {
24852d91 2422 if (revs->right_only)
7854bf49
RS
2423 die(_("options '%s' and '%s' cannot be used together"),
2424 "--left-only", "--right-only/--cherry");
60adf7d7
MG
2425 revs->left_only = 1;
2426 } else if (!strcmp(arg, "--right-only")) {
24852d91 2427 if (revs->left_only)
12909b6b 2428 die(_("options '%s' and '%s' cannot be used together"), "--right-only", "--left-only");
60adf7d7 2429 revs->right_only = 1;
94f605ec
MG
2430 } else if (!strcmp(arg, "--cherry")) {
2431 if (revs->left_only)
12909b6b 2432 die(_("options '%s' and '%s' cannot be used together"), "--cherry", "--left-only");
94f605ec
MG
2433 revs->cherry_mark = 1;
2434 revs->right_only = 1;
ad5aeede 2435 revs->max_parents = 1;
94f605ec 2436 revs->limited = 1;
f69c5018
TR
2437 } else if (!strcmp(arg, "--count")) {
2438 revs->count = 1;
adbbb31e
MG
2439 } else if (!strcmp(arg, "--cherry-mark")) {
2440 if (revs->cherry_pick)
12909b6b 2441 die(_("options '%s' and '%s' cannot be used together"), "--cherry-mark", "--cherry-pick");
adbbb31e
MG
2442 revs->cherry_mark = 1;
2443 revs->limited = 1; /* needs limit_list() */
02e54220 2444 } else if (!strcmp(arg, "--cherry-pick")) {
adbbb31e 2445 if (revs->cherry_mark)
12909b6b 2446 die(_("options '%s' and '%s' cannot be used together"), "--cherry-pick", "--cherry-mark");
02e54220
PH
2447 revs->cherry_pick = 1;
2448 revs->limited = 1;
2449 } else if (!strcmp(arg, "--objects")) {
2450 revs->tag_objects = 1;
2451 revs->tree_objects = 1;
2452 revs->blob_objects = 1;
2453 } else if (!strcmp(arg, "--objects-edge")) {
2454 revs->tag_objects = 1;
2455 revs->tree_objects = 1;
2456 revs->blob_objects = 1;
2457 revs->edge_hint = 1;
1684c1b2 2458 } else if (!strcmp(arg, "--objects-edge-aggressive")) {
2459 revs->tag_objects = 1;
2460 revs->tree_objects = 1;
2461 revs->blob_objects = 1;
2462 revs->edge_hint = 1;
2463 revs->edge_hint_aggressive = 1;
5a48d240
JH
2464 } else if (!strcmp(arg, "--verify-objects")) {
2465 revs->tag_objects = 1;
2466 revs->tree_objects = 1;
2467 revs->blob_objects = 1;
2468 revs->verify_objects = 1;
b27ccae3 2469 disable_commit_graph(revs->repo);
02e54220
PH
2470 } else if (!strcmp(arg, "--unpacked")) {
2471 revs->unpacked = 1;
59556548 2472 } else if (starts_with(arg, "--unpacked=")) {
f649aaaf 2473 die(_("--unpacked=<packfile> no longer supported"));
c9fff000
TB
2474 } else if (!strcmp(arg, "--no-kept-objects")) {
2475 revs->no_kept_objects = 1;
2476 revs->keep_pack_cache_flags |= IN_CORE_KEEP_PACKS;
2477 revs->keep_pack_cache_flags |= ON_DISK_KEEP_PACKS;
2478 } else if (skip_prefix(arg, "--no-kept-objects=", &optarg)) {
2479 revs->no_kept_objects = 1;
2480 if (!strcmp(optarg, "in-core"))
2481 revs->keep_pack_cache_flags |= IN_CORE_KEEP_PACKS;
2482 if (!strcmp(optarg, "on-disk"))
2483 revs->keep_pack_cache_flags |= ON_DISK_KEEP_PACKS;
02e54220
PH
2484 } else if (!strcmp(arg, "-r")) {
2485 revs->diff = 1;
0d1e0e78 2486 revs->diffopt.flags.recursive = 1;
02e54220
PH
2487 } else if (!strcmp(arg, "-t")) {
2488 revs->diff = 1;
0d1e0e78
BW
2489 revs->diffopt.flags.recursive = 1;
2490 revs->diffopt.flags.tree_in_recursive = 1;
18f09473 2491 } else if ((argcount = diff_merges_parse_opts(revs, argv))) {
6501580f 2492 return argcount;
02e54220
PH
2493 } else if (!strcmp(arg, "-v")) {
2494 revs->verbose_header = 1;
2495 } else if (!strcmp(arg, "--pretty")) {
2496 revs->verbose_header = 1;
66b2ed09 2497 revs->pretty_given = 1;
ae18165f 2498 get_commit_format(NULL, revs);
479b3d97
SG
2499 } else if (skip_prefix(arg, "--pretty=", &optarg) ||
2500 skip_prefix(arg, "--format=", &optarg)) {
7d7b86f7
MM
2501 /*
2502 * Detached form ("--pretty X" as opposed to "--pretty=X")
2503 * not allowed, since the argument is optional.
2504 */
02e54220 2505 revs->verbose_header = 1;
66b2ed09 2506 revs->pretty_given = 1;
479b3d97 2507 get_commit_format(optarg, revs);
7cc13c71 2508 } else if (!strcmp(arg, "--expand-tabs")) {
fe37a9c5 2509 revs->expand_tabs_in_log = 8;
0893eec8
JH
2510 } else if (!strcmp(arg, "--no-expand-tabs")) {
2511 revs->expand_tabs_in_log = 0;
fe37a9c5
JH
2512 } else if (skip_prefix(arg, "--expand-tabs=", &arg)) {
2513 int val;
2514 if (strtol_i(arg, 10, &val) < 0 || val < 0)
2515 die("'%s': not a non-negative integer", arg);
2516 revs->expand_tabs_in_log = val;
7249e912 2517 } else if (!strcmp(arg, "--show-notes") || !strcmp(arg, "--notes")) {
1d729751 2518 enable_default_display_notes(&revs->notes_opt, &revs->show_notes);
66b2ed09 2519 revs->show_notes_given = 1;
0c37f1fc
JH
2520 } else if (!strcmp(arg, "--show-signature")) {
2521 revs->show_signature = 1;
aa379999
MJ
2522 } else if (!strcmp(arg, "--no-show-signature")) {
2523 revs->show_signature = 0;
479b3d97
SG
2524 } else if (!strcmp(arg, "--show-linear-break")) {
2525 revs->break_bar = " ..........";
1b32dece
NTND
2526 revs->track_linear = 1;
2527 revs->track_first_time = 1;
479b3d97
SG
2528 } else if (skip_prefix(arg, "--show-linear-break=", &optarg)) {
2529 revs->break_bar = xstrdup(optarg);
1b32dece
NTND
2530 revs->track_linear = 1;
2531 revs->track_first_time = 1;
2e0d30d9
KH
2532 } else if (!strcmp(arg, "--show-notes-by-default")) {
2533 revs->show_notes_by_default = 1;
479b3d97
SG
2534 } else if (skip_prefix(arg, "--show-notes=", &optarg) ||
2535 skip_prefix(arg, "--notes=", &optarg)) {
479b3d97
SG
2536 if (starts_with(arg, "--show-notes=") &&
2537 revs->notes_opt.use_default_notes < 0)
2538 revs->notes_opt.use_default_notes = 1;
1d729751 2539 enable_ref_display_notes(&revs->notes_opt, &revs->show_notes, optarg);
452538c3 2540 revs->show_notes_given = 1;
66b2ed09 2541 } else if (!strcmp(arg, "--no-notes")) {
1d729751 2542 disable_display_notes(&revs->notes_opt, &revs->show_notes);
66b2ed09 2543 revs->show_notes_given = 1;
894a9d33
TR
2544 } else if (!strcmp(arg, "--standard-notes")) {
2545 revs->show_notes_given = 1;
3a03cf6b 2546 revs->notes_opt.use_default_notes = 1;
894a9d33 2547 } else if (!strcmp(arg, "--no-standard-notes")) {
3a03cf6b 2548 revs->notes_opt.use_default_notes = 0;
de84accc
NS
2549 } else if (!strcmp(arg, "--oneline")) {
2550 revs->verbose_header = 1;
2551 get_commit_format("oneline", revs);
7dccadf3 2552 revs->pretty_given = 1;
de84accc 2553 revs->abbrev_commit = 1;
02e54220 2554 } else if (!strcmp(arg, "--graph")) {
dccf6c16 2555 graph_clear(revs->graph);
02e54220 2556 revs->graph = graph_init(revs);
087c7458
AH
2557 } else if (!strcmp(arg, "--no-graph")) {
2558 graph_clear(revs->graph);
2559 revs->graph = NULL;
19d097e3
EB
2560 } else if (!strcmp(arg, "--encode-email-headers")) {
2561 revs->encode_email_headers = 1;
2562 } else if (!strcmp(arg, "--no-encode-email-headers")) {
2563 revs->encode_email_headers = 0;
02e54220
PH
2564 } else if (!strcmp(arg, "--root")) {
2565 revs->show_root_diff = 1;
2566 } else if (!strcmp(arg, "--no-commit-id")) {
2567 revs->no_commit_id = 1;
2568 } else if (!strcmp(arg, "--always")) {
2569 revs->always_show_header = 1;
2570 } else if (!strcmp(arg, "--no-abbrev")) {
2571 revs->abbrev = 0;
2572 } else if (!strcmp(arg, "--abbrev")) {
2573 revs->abbrev = DEFAULT_ABBREV;
479b3d97
SG
2574 } else if (skip_prefix(arg, "--abbrev=", &optarg)) {
2575 revs->abbrev = strtoul(optarg, NULL, 10);
02e54220
PH
2576 if (revs->abbrev < MINIMUM_ABBREV)
2577 revs->abbrev = MINIMUM_ABBREV;
fd521245 2578 else if (revs->abbrev > hexsz)
2579 revs->abbrev = hexsz;
02e54220
PH
2580 } else if (!strcmp(arg, "--abbrev-commit")) {
2581 revs->abbrev_commit = 1;
0c47695a
JS
2582 revs->abbrev_commit_given = 1;
2583 } else if (!strcmp(arg, "--no-abbrev-commit")) {
2584 revs->abbrev_commit = 0;
02e54220
PH
2585 } else if (!strcmp(arg, "--full-diff")) {
2586 revs->diff = 1;
2587 revs->full_diff = 1;
8d049e18
DS
2588 } else if (!strcmp(arg, "--show-pulls")) {
2589 revs->show_pulls = 1;
02e54220
PH
2590 } else if (!strcmp(arg, "--full-history")) {
2591 revs->simplify_history = 0;
2592 } else if (!strcmp(arg, "--relative-date")) {
a5481a6c 2593 revs->date_mode.type = DATE_RELATIVE;
f4ea32f0 2594 revs->date_mode_explicit = 1;
7d7b86f7 2595 } else if ((argcount = parse_long_opt("date", argv, &optarg))) {
a5481a6c 2596 parse_date_format(optarg, &revs->date_mode);
f4ea32f0 2597 revs->date_mode_explicit = 1;
7d7b86f7 2598 return argcount;
02e54220
PH
2599 } else if (!strcmp(arg, "--log-size")) {
2600 revs->show_log_size = 1;
2601 }
2602 /*
2603 * Grepping the commit log
2604 */
7d7b86f7
MM
2605 else if ((argcount = parse_long_opt("author", argv, &optarg))) {
2606 add_header_grep(revs, GREP_HEADER_AUTHOR, optarg);
2607 return argcount;
2608 } else if ((argcount = parse_long_opt("committer", argv, &optarg))) {
2609 add_header_grep(revs, GREP_HEADER_COMMITTER, optarg);
2610 return argcount;
72fd13f7
NTND
2611 } else if ((argcount = parse_long_opt("grep-reflog", argv, &optarg))) {
2612 add_header_grep(revs, GREP_HEADER_REFLOG, optarg);
2613 return argcount;
7d7b86f7
MM
2614 } else if ((argcount = parse_long_opt("grep", argv, &optarg))) {
2615 add_message_grep(revs, optarg);
2616 return argcount;
727b6fc3 2617 } else if (!strcmp(arg, "--basic-regexp")) {
8465541e 2618 revs->grep_filter.pattern_type_option = GREP_PATTERN_TYPE_BRE;
02e54220 2619 } else if (!strcmp(arg, "--extended-regexp") || !strcmp(arg, "-E")) {
8465541e 2620 revs->grep_filter.pattern_type_option = GREP_PATTERN_TYPE_ERE;
02e54220 2621 } else if (!strcmp(arg, "--regexp-ignore-case") || !strcmp(arg, "-i")) {
9e3cbc59 2622 revs->grep_filter.ignore_case = 1;
c1ddc461 2623 revs->diffopt.pickaxe_opts |= DIFF_PICKAXE_IGNORE_CASE;
02e54220 2624 } else if (!strcmp(arg, "--fixed-strings") || !strcmp(arg, "-F")) {
8465541e 2625 revs->grep_filter.pattern_type_option = GREP_PATTERN_TYPE_FIXED;
7531a2dd 2626 } else if (!strcmp(arg, "--perl-regexp") || !strcmp(arg, "-P")) {
8465541e 2627 revs->grep_filter.pattern_type_option = GREP_PATTERN_TYPE_PCRE;
02e54220 2628 } else if (!strcmp(arg, "--all-match")) {
0843acfd 2629 revs->grep_filter.all_match = 1;
22dfa8a2 2630 } else if (!strcmp(arg, "--invert-grep")) {
794c0002 2631 revs->grep_filter.no_body_match = 1;
7d7b86f7
MM
2632 } else if ((argcount = parse_long_opt("encoding", argv, &optarg))) {
2633 if (strcmp(optarg, "none"))
2634 git_log_output_encoding = xstrdup(optarg);
02e54220
PH
2635 else
2636 git_log_output_encoding = "";
7d7b86f7 2637 return argcount;
02e54220
PH
2638 } else if (!strcmp(arg, "--reverse")) {
2639 revs->reverse ^= 1;
2640 } else if (!strcmp(arg, "--children")) {
2641 revs->children.name = "children";
2642 revs->limited = 1;
cc243c3c
JH
2643 } else if (!strcmp(arg, "--ignore-missing")) {
2644 revs->ignore_missing = 1;
bbcde41a 2645 } else if (opt && opt->allow_exclude_promisor_objects &&
669b1d2a 2646 !strcmp(arg, "--exclude-promisor-objects")) {
df11e196 2647 if (fetch_if_missing)
033abf97 2648 BUG("exclude_promisor_objects can only be used when fetch_if_missing is 0");
df11e196 2649 revs->exclude_promisor_objects = 1;
02e54220 2650 } else {
a97262c6 2651 int opts = diff_opt_parse(&revs->diffopt, argv, argc, revs->prefix);
02e54220
PH
2652 if (!opts)
2653 unkv[(*unkc)++] = arg;
2654 return opts;
2655 }
2656
2657 return 1;
2658}
2659
6b61ec05
PH
2660void parse_revision_opt(struct rev_info *revs, struct parse_opt_ctx_t *ctx,
2661 const struct option *options,
2662 const char * const usagestr[])
2663{
2664 int n = handle_revision_opt(revs, ctx->argc, ctx->argv,
bbcde41a 2665 &ctx->cpidx, ctx->out, NULL);
6b61ec05
PH
2666 if (n <= 0) {
2667 error("unknown option `%s'", ctx->argv[0]);
2668 usage_with_options(usagestr, options);
2669 }
2670 ctx->argv += n;
2671 ctx->argc -= n;
2672}
2673
087c7458
AH
2674void revision_opts_finish(struct rev_info *revs)
2675{
2676 if (revs->graph && revs->track_linear)
2677 die(_("options '%s' and '%s' cannot be used together"), "--show-linear-break", "--graph");
2678
2679 if (revs->graph) {
2680 revs->topo_order = 1;
2681 revs->rewrite_parents = 1;
2682 }
2683}
2684
073cf63c
NTND
2685static int for_each_bisect_ref(struct ref_store *refs, each_ref_fn fn,
2686 void *cb_data, const char *term)
2687{
cb46d630
AD
2688 struct strbuf bisect_refs = STRBUF_INIT;
2689 int status;
2690 strbuf_addf(&bisect_refs, "refs/bisect/%s", term);
b269ac53 2691 status = refs_for_each_fullref_in(refs, bisect_refs.buf, NULL, fn, cb_data);
cb46d630
AD
2692 strbuf_release(&bisect_refs);
2693 return status;
2694}
2695
073cf63c 2696static int for_each_bad_bisect_ref(struct ref_store *refs, each_ref_fn fn, void *cb_data)
ad3f9a71 2697{
073cf63c 2698 return for_each_bisect_ref(refs, fn, cb_data, term_bad);
ad3f9a71
LT
2699}
2700
073cf63c 2701static int for_each_good_bisect_ref(struct ref_store *refs, each_ref_fn fn, void *cb_data)
ad3f9a71 2702{
073cf63c 2703 return for_each_bisect_ref(refs, fn, cb_data, term_good);
ad3f9a71
LT
2704}
2705
10a0d6ae 2706static int handle_revision_pseudo_opt(struct rev_info *revs,
e885a84f 2707 const char **argv, int *flags)
f6aca0dc
JN
2708{
2709 const char *arg = argv[0];
2710 const char *optarg;
073cf63c 2711 struct ref_store *refs;
f6aca0dc
JN
2712 int argcount;
2713
10a0d6ae 2714 if (revs->repo != the_repository) {
d0c39a49
NTND
2715 /*
2716 * We need some something like get_submodule_worktrees()
2717 * before we can go through all worktrees of a submodule,
2718 * .e.g with adding all HEADs from --all, which is not
2719 * supported right now, so stick to single worktree.
2720 */
2721 if (!revs->single_worktree)
033abf97 2722 BUG("--single-worktree cannot be used together with submodule");
10a0d6ae
JT
2723 }
2724 refs = get_main_ref_store(revs->repo);
073cf63c 2725
0fc63ec4
JN
2726 /*
2727 * NOTE!
2728 *
2729 * Commands like "git shortlog" will not accept the options below
2730 * unless parse_revision_opt queues them (as opposed to erroring
2731 * out).
2732 *
2733 * When implementing your new pseudo-option, remember to
2734 * register it in the list at the top of handle_revision_opt.
2735 */
f6aca0dc 2736 if (!strcmp(arg, "--all")) {
073cf63c
NTND
2737 handle_refs(refs, revs, *flags, refs_for_each_ref);
2738 handle_refs(refs, revs, *flags, refs_head_ref);
d0c39a49
NTND
2739 if (!revs->single_worktree) {
2740 struct all_refs_cb cb;
2741
2742 init_all_refs_cb(&cb, revs, *flags);
2743 other_head_refs(handle_one_ref, &cb);
2744 }
1e9f273a 2745 clear_ref_exclusions(&revs->ref_excludes);
f6aca0dc 2746 } else if (!strcmp(arg, "--branches")) {
8c1bc2a7 2747 if (revs->ref_excludes.hidden_refs_configured)
81fb70f5
RS
2748 return error(_("options '%s' and '%s' cannot be used together"),
2749 "--exclude-hidden", "--branches");
073cf63c 2750 handle_refs(refs, revs, *flags, refs_for_each_branch_ref);
1e9f273a 2751 clear_ref_exclusions(&revs->ref_excludes);
f6aca0dc 2752 } else if (!strcmp(arg, "--bisect")) {
cb46d630 2753 read_bisect_terms(&term_bad, &term_good);
073cf63c
NTND
2754 handle_refs(refs, revs, *flags, for_each_bad_bisect_ref);
2755 handle_refs(refs, revs, *flags ^ (UNINTERESTING | BOTTOM),
2756 for_each_good_bisect_ref);
f6aca0dc
JN
2757 revs->bisect = 1;
2758 } else if (!strcmp(arg, "--tags")) {
8c1bc2a7 2759 if (revs->ref_excludes.hidden_refs_configured)
81fb70f5
RS
2760 return error(_("options '%s' and '%s' cannot be used together"),
2761 "--exclude-hidden", "--tags");
073cf63c 2762 handle_refs(refs, revs, *flags, refs_for_each_tag_ref);
1e9f273a 2763 clear_ref_exclusions(&revs->ref_excludes);
f6aca0dc 2764 } else if (!strcmp(arg, "--remotes")) {
8c1bc2a7 2765 if (revs->ref_excludes.hidden_refs_configured)
81fb70f5
RS
2766 return error(_("options '%s' and '%s' cannot be used together"),
2767 "--exclude-hidden", "--remotes");
073cf63c 2768 handle_refs(refs, revs, *flags, refs_for_each_remote_ref);
1e9f273a 2769 clear_ref_exclusions(&revs->ref_excludes);
f6aca0dc
JN
2770 } else if ((argcount = parse_long_opt("glob", argv, &optarg))) {
2771 struct all_refs_cb cb;
2772 init_all_refs_cb(&cb, revs, *flags);
a217dcbd 2773 for_each_glob_ref(handle_one_ref, optarg, &cb);
1e9f273a 2774 clear_ref_exclusions(&revs->ref_excludes);
e7b432c5
JH
2775 return argcount;
2776 } else if ((argcount = parse_long_opt("exclude", argv, &optarg))) {
ff32d342 2777 add_ref_exclusion(&revs->ref_excludes, optarg);
f6aca0dc 2778 return argcount;
8c1bc2a7
PS
2779 } else if ((argcount = parse_long_opt("exclude-hidden", argv, &optarg))) {
2780 exclude_hidden_refs(&revs->ref_excludes, optarg);
2781 return argcount;
8b1d9136 2782 } else if (skip_prefix(arg, "--branches=", &optarg)) {
f6aca0dc 2783 struct all_refs_cb cb;
8c1bc2a7 2784 if (revs->ref_excludes.hidden_refs_configured)
81fb70f5
RS
2785 return error(_("options '%s' and '%s' cannot be used together"),
2786 "--exclude-hidden", "--branches");
f6aca0dc 2787 init_all_refs_cb(&cb, revs, *flags);
8b1d9136 2788 for_each_glob_ref_in(handle_one_ref, optarg, "refs/heads/", &cb);
1e9f273a 2789 clear_ref_exclusions(&revs->ref_excludes);
8b1d9136 2790 } else if (skip_prefix(arg, "--tags=", &optarg)) {
f6aca0dc 2791 struct all_refs_cb cb;
8c1bc2a7 2792 if (revs->ref_excludes.hidden_refs_configured)
81fb70f5
RS
2793 return error(_("options '%s' and '%s' cannot be used together"),
2794 "--exclude-hidden", "--tags");
f6aca0dc 2795 init_all_refs_cb(&cb, revs, *flags);
8b1d9136 2796 for_each_glob_ref_in(handle_one_ref, optarg, "refs/tags/", &cb);
1e9f273a 2797 clear_ref_exclusions(&revs->ref_excludes);
8b1d9136 2798 } else if (skip_prefix(arg, "--remotes=", &optarg)) {
f6aca0dc 2799 struct all_refs_cb cb;
8c1bc2a7 2800 if (revs->ref_excludes.hidden_refs_configured)
81fb70f5
RS
2801 return error(_("options '%s' and '%s' cannot be used together"),
2802 "--exclude-hidden", "--remotes");
f6aca0dc 2803 init_all_refs_cb(&cb, revs, *flags);
8b1d9136 2804 for_each_glob_ref_in(handle_one_ref, optarg, "refs/remotes/", &cb);
1e9f273a 2805 clear_ref_exclusions(&revs->ref_excludes);
f6aca0dc 2806 } else if (!strcmp(arg, "--reflog")) {
718ccc97 2807 add_reflogs_to_pending(revs, *flags);
4fe10219
JK
2808 } else if (!strcmp(arg, "--indexed-objects")) {
2809 add_index_objects_to_pending(revs, *flags);
39b44ba7
JK
2810 } else if (!strcmp(arg, "--alternate-refs")) {
2811 add_alternate_refs_to_pending(revs, *flags);
f6aca0dc 2812 } else if (!strcmp(arg, "--not")) {
7f34a46f 2813 *flags ^= UNINTERESTING | BOTTOM;
f6aca0dc 2814 } else if (!strcmp(arg, "--no-walk")) {
29ef1f27 2815 revs->no_walk = 1;
8b1d9136 2816 } else if (skip_prefix(arg, "--no-walk=", &optarg)) {
ca92e59e
MZ
2817 /*
2818 * Detached form ("--no-walk X" as opposed to "--no-walk=X")
2819 * not allowed, since the argument is optional.
2820 */
29ef1f27 2821 revs->no_walk = 1;
8b1d9136 2822 if (!strcmp(optarg, "sorted"))
29ef1f27 2823 revs->unsorted_input = 0;
8b1d9136 2824 else if (!strcmp(optarg, "unsorted"))
29ef1f27 2825 revs->unsorted_input = 1;
ca92e59e
MZ
2826 else
2827 return error("invalid argument to --no-walk");
f6aca0dc
JN
2828 } else if (!strcmp(arg, "--do-walk")) {
2829 revs->no_walk = 0;
32619f99
NTND
2830 } else if (!strcmp(arg, "--single-worktree")) {
2831 revs->single_worktree = 1;
cc910442 2832 } else if (skip_prefix(arg, ("--filter="), &arg)) {
c4ea513f 2833 parse_list_objects_filter(&revs->filter, arg);
cc910442 2834 } else if (!strcmp(arg, ("--no-filter"))) {
c4ea513f 2835 list_objects_filter_set_no_filter(&revs->filter);
f6aca0dc
JN
2836 } else {
2837 return 0;
2838 }
2839
2840 return 1;
2841}
2842
cc804501 2843static void read_revisions_from_stdin(struct rev_info *revs,
f97c8b1e 2844 struct strvec *prune)
cc804501
PS
2845{
2846 struct strbuf sb;
2847 int seen_dashdash = 0;
c40f0b78 2848 int seen_end_of_options = 0;
cc804501 2849 int save_warning;
f97c8b1e 2850 int flags = 0;
cc804501
PS
2851
2852 save_warning = warn_on_object_refname_ambiguity;
2853 warn_on_object_refname_ambiguity = 0;
2854
2855 strbuf_init(&sb, 1000);
2856 while (strbuf_getline(&sb, stdin) != EOF) {
af37a209
PS
2857 if (!sb.len)
2858 break;
2859
2860 if (!strcmp(sb.buf, "--")) {
2861 seen_dashdash = 1;
cc804501 2862 break;
cc804501 2863 }
af37a209 2864
c40f0b78
PS
2865 if (!seen_end_of_options && sb.buf[0] == '-') {
2866 const char *argv[] = { sb.buf, NULL };
2867
2868 if (!strcmp(sb.buf, "--end-of-options")) {
2869 seen_end_of_options = 1;
2870 continue;
2871 }
2872
f97c8b1e 2873 if (handle_revision_pseudo_opt(revs, argv, &flags) > 0)
c40f0b78
PS
2874 continue;
2875
2876 die(_("invalid option '%s' in --stdin mode"), sb.buf);
2877 }
af37a209 2878
f97c8b1e 2879 if (handle_revision_arg(sb.buf, revs, flags,
cc804501
PS
2880 REVARG_CANNOT_BE_FILENAME))
2881 die("bad revision '%s'", sb.buf);
2882 }
2883 if (seen_dashdash)
2884 read_pathspec_from_stdin(&sb, prune);
2885
2886 strbuf_release(&sb);
2887 warn_on_object_refname_ambiguity = save_warning;
2888}
2889
ce113604
JK
2890static void NORETURN diagnose_missing_default(const char *def)
2891{
ce113604
JK
2892 int flags;
2893 const char *refname;
2894
744c040b 2895 refname = resolve_ref_unsafe(def, 0, NULL, &flags);
ce113604
JK
2896 if (!refname || !(flags & REF_ISSYMREF) || (flags & REF_ISBROKEN))
2897 die(_("your current branch appears to be broken"));
2898
2899 skip_prefix(refname, "refs/heads/", &refname);
2900 die(_("your current branch '%s' does not have any commits yet"),
2901 refname);
2902}
2903
ae563542
LT
2904/*
2905 * Parse revision information, filling in the "rev_info" structure,
2906 * and removing the used arguments from the argument list.
2907 *
765ac8ec
LT
2908 * Returns the number of arguments left that weren't recognized
2909 * (which are also moved to the head of the argument list)
ae563542 2910 */
32962c9b 2911int setup_revisions(int argc, const char **argv, struct rev_info *revs, struct setup_revision_opt *opt)
ae563542 2912{
04a0e985 2913 int i, flags, left, seen_dashdash, revarg_opt;
c972bf4c 2914 struct strvec prune_data = STRVEC_INIT;
19e8789b 2915 int seen_end_of_options = 0;
9ef6aeb0 2916
ae563542 2917 /* First, search for "--" */
6d5b93f2 2918 if (opt && opt->assume_dashdash) {
ae563542 2919 seen_dashdash = 1;
6d5b93f2
CB
2920 } else {
2921 seen_dashdash = 0;
2922 for (i = 1; i < argc; i++) {
2923 const char *arg = argv[i];
2924 if (strcmp(arg, "--"))
2925 continue;
f92dbdbc
ÆAB
2926 if (opt && opt->free_removed_argv_elements)
2927 free((char *)argv[i]);
6d5b93f2
CB
2928 argv[i] = NULL;
2929 argc = i;
2930 if (argv[i + 1])
c972bf4c 2931 strvec_pushv(&prune_data, argv + i + 1);
6d5b93f2
CB
2932 seen_dashdash = 1;
2933 break;
2934 }
ae563542
LT
2935 }
2936
02e54220
PH
2937 /* Second, deal with arguments and options */
2938 flags = 0;
d5f6b1d7
JH
2939 revarg_opt = opt ? opt->revarg_opt : 0;
2940 if (seen_dashdash)
2941 revarg_opt |= REVARG_CANNOT_BE_FILENAME;
02e54220 2942 for (left = i = 1; i < argc; i++) {
ae563542 2943 const char *arg = argv[i];
19e8789b 2944 if (!seen_end_of_options && *arg == '-') {
cd2bdc53 2945 int opts;
02e54220 2946
10a0d6ae 2947 opts = handle_revision_pseudo_opt(
e885a84f 2948 revs, argv + i,
f6aca0dc
JN
2949 &flags);
2950 if (opts > 0) {
2951 i += opts - 1;
8e64006e
JS
2952 continue;
2953 }
f6aca0dc 2954
8b3dce56
JH
2955 if (!strcmp(arg, "--stdin")) {
2956 if (revs->disable_stdin) {
2957 argv[left++] = arg;
2958 continue;
2959 }
a12cbe23 2960 if (revs->read_from_stdin++)
8b3dce56 2961 die("--stdin given twice?");
f97c8b1e 2962 read_revisions_from_stdin(revs, &prune_data);
8b3dce56
JH
2963 continue;
2964 }
2d10c555 2965
19e8789b
JK
2966 if (!strcmp(arg, "--end-of-options")) {
2967 seen_end_of_options = 1;
2968 continue;
2969 }
2970
bbcde41a
MD
2971 opts = handle_revision_opt(revs, argc - i, argv + i,
2972 &left, argv, opt);
cd2bdc53 2973 if (opts > 0) {
cd2bdc53
LT
2974 i += opts - 1;
2975 continue;
2976 }
02e54220
PH
2977 if (opts < 0)
2978 exit(128);
ae563542
LT
2979 continue;
2980 }
ae563542 2981
8e676e8b
JH
2982
2983 if (handle_revision_arg(arg, revs, flags, revarg_opt)) {
5d6f0935
JH
2984 int j;
2985 if (seen_dashdash || *arg == '^')
ae563542
LT
2986 die("bad revision '%s'", arg);
2987
ea92f41f
JH
2988 /* If we didn't have a "--":
2989 * (1) all filenames must exist;
2990 * (2) all rev-args must not be interpretable
2991 * as a valid filename.
2992 * but the latter we have checked in the main loop.
2993 */
e23d0b4a 2994 for (j = i; j < argc; j++)
023e37c3 2995 verify_filename(revs->prefix, argv[j], j == i);
e23d0b4a 2996
c972bf4c 2997 strvec_pushv(&prune_data, argv + i);
ae563542
LT
2998 break;
2999 }
ae563542 3000 }
087c7458 3001 revision_opts_finish(revs);
5d6f0935 3002
d70a9eb6 3003 if (prune_data.nr) {
93e7d672
JH
3004 /*
3005 * If we need to introduce the magic "a lone ':' means no
3006 * pathspec whatsoever", here is the place to do so.
3007 *
3008 * if (prune_data.nr == 1 && !strcmp(prune_data[0], ":")) {
3009 * prune_data.nr = 0;
3010 * prune_data.alloc = 0;
3011 * free(prune_data.path);
3012 * prune_data.path = NULL;
3013 * } else {
3014 * terminate prune_data.alloc with NULL and
3015 * call init_pathspec() to set revs->prune_data here.
3016 * }
3017 */
0fdc2ae5 3018 parse_pathspec(&revs->prune_data, 0, 0,
d70a9eb6 3019 revs->prefix, prune_data.v);
4da5af31 3020 }
c972bf4c 3021 strvec_clear(&prune_data);
5486ef0e 3022
afe8a907 3023 if (!revs->def)
32962c9b 3024 revs->def = opt ? opt->def : NULL;
b4490059 3025 if (opt && opt->tweak)
cc88afad 3026 opt->tweak(revs);
02e54220 3027 if (revs->show_merge)
ae3e5e1e 3028 prepare_show_merge(revs);
04a0e985 3029 if (revs->def && !revs->pending.nr && !revs->rev_input_given) {
654b9a90 3030 struct object_id oid;
cd2bdc53 3031 struct object *object;
249c8f4a 3032 struct object_context oc;
3a7a698e 3033 if (get_oid_with_context(revs->repo, revs->def, 0, &oid, &oc))
ce113604 3034 diagnose_missing_default(revs->def);
654b9a90 3035 object = get_reference(revs, revs->def, &oid, 0);
249c8f4a 3036 add_pending_object_with_mode(revs, object, revs->def, oc.mode);
ae563542 3037 }
8efdc326 3038
b7bb760d
LT
3039 /* Did the user ask for any diff output? Run the diff! */
3040 if (revs->diffopt.output_format & ~DIFF_FORMAT_NO_OUTPUT)
3041 revs->diff = 1;
3042
0faf2da7 3043 /* Pickaxe, diff-filter and rename following need diffs */
cf63051a 3044 if ((revs->diffopt.pickaxe_opts & DIFF_PICKAXE_KINDS_MASK) ||
0faf2da7 3045 revs->diffopt.filter ||
0d1e0e78 3046 revs->diffopt.flags.follow_renames)
b7bb760d
LT
3047 revs->diff = 1;
3048
15af58c1
SB
3049 if (revs->diffopt.objfind)
3050 revs->simplify_history = 0;
3051
002933f3
SG
3052 if (revs->line_level_traverse) {
3053 if (want_ancestry(revs))
3054 revs->limited = 1;
3055 revs->topo_order = 1;
3056 }
3057
f0d9cc41 3058 if (revs->topo_order && !generation_numbers_enabled(the_repository))
53069686
JH
3059 revs->limited = 1;
3060
afe069d1 3061 if (revs->prune_data.nr) {
bd1928df 3062 copy_pathspec(&revs->pruning.pathspec, &revs->prune_data);
750f7b66 3063 /* Can't prune commits with rename following: the paths change.. */
0d1e0e78 3064 if (!revs->diffopt.flags.follow_renames)
53b2c823 3065 revs->prune = 1;
cd2bdc53 3066 if (!revs->full_diff)
bd1928df
NTND
3067 copy_pathspec(&revs->diffopt.pathspec,
3068 &revs->prune_data);
8efdc326 3069 }
299a6634 3070
18f09473 3071 diff_merges_setup_revs(revs);
d76ce4f7 3072
cd2bdc53 3073 revs->diffopt.abbrev = revs->abbrev;
12da1d1f 3074
28452655 3075 diff_setup_done(&revs->diffopt);
8efdc326 3076
44570188
ÆAB
3077 if (!is_encoding_utf8(get_log_output_encoding()))
3078 revs->grep_filter.ignore_locale = 1;
0843acfd 3079 compile_grep_patterns(&revs->grep_filter);
bd95fcd3 3080
82fd0f4a
JK
3081 if (revs->reflog_info && revs->limited)
3082 die("cannot combine --walk-reflogs with history-limiting options");
8bb65883 3083 if (revs->rewrite_parents && revs->children.name)
12909b6b 3084 die(_("options '%s' and '%s' cannot be used together"), "--parents", "--children");
c4ea513f
DS
3085 if (revs->filter.choice && !revs->blob_objects)
3086 die(_("object filtering requires --objects"));
d56651c0 3087
7fefda5c
AS
3088 /*
3089 * Limitations on the graph functionality
3090 */
fa518aef
RS
3091 die_for_incompatible_opt3(!!revs->graph, "--graph",
3092 !!revs->reverse, "--reverse",
3093 !!revs->reflog_info, "--walk-reflogs");
7fefda5c 3094
695985f4 3095 if (revs->no_walk && revs->graph)
12909b6b 3096 die(_("options '%s' and '%s' cannot be used together"), "--no-walk", "--graph");
baa6378f 3097 if (!revs->reflog_info && revs->grep_filter.use_reflog_filter)
6fa00ee8 3098 die(_("the option '%s' requires '%s'"), "--grep-reflog", "--walk-reflogs");
7fefda5c 3099
05314efa
JK
3100 if (revs->line_level_traverse &&
3101 (revs->diffopt.output_format & ~(DIFF_FORMAT_PATCH | DIFF_FORMAT_NO_OUTPUT)))
3102 die(_("-L does not yet support diff formats besides -p and -s"));
3103
0893eec8
JH
3104 if (revs->expand_tabs_in_log < 0)
3105 revs->expand_tabs_in_log = revs->expand_tabs_in_log_default;
3106
2e0d30d9
KH
3107 if (!revs->show_notes_given && revs->show_notes_by_default) {
3108 enable_default_display_notes(&revs->notes_opt, &revs->show_notes);
3109 revs->show_notes_given = 1;
3110 }
3111
ae563542
LT
3112 return left;
3113}
a4a88b2b 3114
7a98d9ab
ÆAB
3115static void release_revisions_cmdline(struct rev_cmdline_info *cmdline)
3116{
3117 unsigned int i;
3118
3119 for (i = 0; i < cmdline->nr; i++)
3120 free((char *)cmdline->rev[i].name);
3121 free(cmdline->rev);
3122}
3123
a52f07af
ÆAB
3124static void release_revisions_mailmap(struct string_list *mailmap)
3125{
3126 if (!mailmap)
3127 return;
3128 clear_mailmap(mailmap);
3129 free(mailmap);
3130}
3131
ae1b383d
ÆAB
3132static void release_revisions_topo_walk_info(struct topo_walk_info *info);
3133
8ef8da48
JK
3134static void free_void_commit_list(void *list)
3135{
3136 free_commit_list(list);
3137}
3138
1878b5ed
ÆAB
3139void release_revisions(struct rev_info *revs)
3140{
e966fc5a 3141 free_commit_list(revs->commits);
257418c5 3142 free_commit_list(revs->ancestry_path_bottoms);
1878b5ed 3143 object_array_clear(&revs->pending);
ab1f6926 3144 object_array_clear(&revs->boundary_commits);
7a98d9ab 3145 release_revisions_cmdline(&revs->cmdline);
e75d2f7f 3146 list_objects_filter_release(&revs->filter);
689a8e80 3147 clear_pathspec(&revs->prune_data);
9d5a7df3 3148 date_mode_release(&revs->date_mode);
a52f07af 3149 release_revisions_mailmap(revs->mailmap);
f41fb662 3150 free_grep_patterns(&revs->grep_filter);
fc47252d 3151 graph_clear(revs->graph);
54c8a7c3 3152 /* TODO (need to handle "no_free"): diff_free(&revs->diffopt) */
6ab75ac8 3153 diff_free(&revs->pruning);
81ffbf83 3154 reflog_walk_info_release(revs->reflog_info);
ae1b383d 3155 release_revisions_topo_walk_info(revs->topo_walk_info);
8ef8da48
JK
3156 clear_decoration(&revs->children, free_void_commit_list);
3157 clear_decoration(&revs->merge_simplification, free);
3158 clear_decoration(&revs->treesame, free);
3159 line_log_free(revs);
9830926c 3160 oidset_clear(&revs->missing_commits);
1878b5ed
ÆAB
3161}
3162
f35f5603
JH
3163static void add_child(struct rev_info *revs, struct commit *parent, struct commit *child)
3164{
3165 struct commit_list *l = xcalloc(1, sizeof(*l));
3166
3167 l->item = child;
3168 l->next = add_decoration(&revs->children, &parent->object, l);
3169}
3170
d0af663e 3171static int remove_duplicate_parents(struct rev_info *revs, struct commit *commit)
6546b593 3172{
d0af663e 3173 struct treesame_state *ts = lookup_decoration(&revs->treesame, &commit->object);
6546b593
JH
3174 struct commit_list **pp, *p;
3175 int surviving_parents;
3176
3177 /* Examine existing parents while marking ones we have seen... */
3178 pp = &commit->parents;
d0af663e 3179 surviving_parents = 0;
6546b593
JH
3180 while ((p = *pp) != NULL) {
3181 struct commit *parent = p->item;
3182 if (parent->object.flags & TMP_MARK) {
3183 *pp = p->next;
d0af663e
KB
3184 if (ts)
3185 compact_treesame(revs, commit, surviving_parents);
6546b593
JH
3186 continue;
3187 }
3188 parent->object.flags |= TMP_MARK;
d0af663e 3189 surviving_parents++;
6546b593
JH
3190 pp = &p->next;
3191 }
d0af663e 3192 /* clear the temporary mark */
6546b593
JH
3193 for (p = commit->parents; p; p = p->next) {
3194 p->item->object.flags &= ~TMP_MARK;
6546b593 3195 }
d0af663e 3196 /* no update_treesame() - removing duplicates can't affect TREESAME */
6546b593
JH
3197 return surviving_parents;
3198}
3199
faf0156b
JH
3200struct merge_simplify_state {
3201 struct commit *simplified;
3202};
3203
3204static struct merge_simplify_state *locate_simplify_state(struct rev_info *revs, struct commit *commit)
3205{
3206 struct merge_simplify_state *st;
3207
3208 st = lookup_decoration(&revs->merge_simplification, &commit->object);
3209 if (!st) {
ca56dadb 3210 CALLOC_ARRAY(st, 1);
faf0156b
JH
3211 add_decoration(&revs->merge_simplification, &commit->object, st);
3212 }
3213 return st;
3214}
3215
91633995 3216static int mark_redundant_parents(struct commit *commit)
d0af663e
KB
3217{
3218 struct commit_list *h = reduce_heads(commit->parents);
3219 int i = 0, marked = 0;
3220 struct commit_list *po, *pn;
3221
3222 /* Want these for sanity-checking only */
3223 int orig_cnt = commit_list_count(commit->parents);
3224 int cnt = commit_list_count(h);
3225
3226 /*
3227 * Not ready to remove items yet, just mark them for now, based
3228 * on the output of reduce_heads(). reduce_heads outputs the reduced
3229 * set in its original order, so this isn't too hard.
3230 */
3231 po = commit->parents;
3232 pn = h;
3233 while (po) {
3234 if (pn && po->item == pn->item) {
3235 pn = pn->next;
3236 i++;
3237 } else {
3238 po->item->object.flags |= TMP_MARK;
3239 marked++;
3240 }
3241 po=po->next;
3242 }
3243
3244 if (i != cnt || cnt+marked != orig_cnt)
3245 die("mark_redundant_parents %d %d %d %d", orig_cnt, cnt, i, marked);
3246
3247 free_commit_list(h);
3248
3249 return marked;
3250}
3251
91633995 3252static int mark_treesame_root_parents(struct commit *commit)
143f1eaf
KB
3253{
3254 struct commit_list *p;
3255 int marked = 0;
3256
3257 for (p = commit->parents; p; p = p->next) {
3258 struct commit *parent = p->item;
3259 if (!parent->parents && (parent->object.flags & TREESAME)) {
3260 parent->object.flags |= TMP_MARK;
3261 marked++;
3262 }
3263 }
3264
3265 return marked;
3266}
3267
9c129eab
KB
3268/*
3269 * Awkward naming - this means one parent we are TREESAME to.
3270 * cf mark_treesame_root_parents: root parents that are TREESAME (to an
3271 * empty tree). Better name suggestions?
3272 */
3273static int leave_one_treesame_to_parent(struct rev_info *revs, struct commit *commit)
3274{
3275 struct treesame_state *ts = lookup_decoration(&revs->treesame, &commit->object);
3276 struct commit *unmarked = NULL, *marked = NULL;
3277 struct commit_list *p;
3278 unsigned n;
3279
3280 for (p = commit->parents, n = 0; p; p = p->next, n++) {
3281 if (ts->treesame[n]) {
3282 if (p->item->object.flags & TMP_MARK) {
3283 if (!marked)
3284 marked = p->item;
3285 } else {
3286 if (!unmarked) {
3287 unmarked = p->item;
3288 break;
3289 }
3290 }
3291 }
3292 }
3293
3294 /*
3295 * If we are TREESAME to a marked-for-deletion parent, but not to any
3296 * unmarked parents, unmark the first TREESAME parent. This is the
3297 * parent that the default simplify_history==1 scan would have followed,
3298 * and it doesn't make sense to omit that path when asking for a
3299 * simplified full history. Retaining it improves the chances of
3300 * understanding odd missed merges that took an old version of a file.
3301 *
3302 * Example:
3303 *
3304 * I--------*X A modified the file, but mainline merge X used
3305 * \ / "-s ours", so took the version from I. X is
3306 * `-*A--' TREESAME to I and !TREESAME to A.
3307 *
3308 * Default log from X would produce "I". Without this check,
3309 * --full-history --simplify-merges would produce "I-A-X", showing
3310 * the merge commit X and that it changed A, but not making clear that
3311 * it had just taken the I version. With this check, the topology above
3312 * is retained.
3313 *
3314 * Note that it is possible that the simplification chooses a different
3315 * TREESAME parent from the default, in which case this test doesn't
3316 * activate, and we _do_ drop the default parent. Example:
3317 *
3318 * I------X A modified the file, but it was reverted in B,
3319 * \ / meaning mainline merge X is TREESAME to both
3320 * *A-*B parents.
3321 *
3322 * Default log would produce "I" by following the first parent;
3323 * --full-history --simplify-merges will produce "I-A-B". But this is a
3324 * reasonable result - it presents a logical full history leading from
3325 * I to X, and X is not an important merge.
3326 */
3327 if (!unmarked && marked) {
3328 marked->object.flags &= ~TMP_MARK;
3329 return 1;
3330 }
3331
3332 return 0;
3333}
3334
d0af663e
KB
3335static int remove_marked_parents(struct rev_info *revs, struct commit *commit)
3336{
3337 struct commit_list **pp, *p;
3338 int nth_parent, removed = 0;
3339
3340 pp = &commit->parents;
3341 nth_parent = 0;
3342 while ((p = *pp) != NULL) {
3343 struct commit *parent = p->item;
3344 if (parent->object.flags & TMP_MARK) {
3345 parent->object.flags &= ~TMP_MARK;
3346 *pp = p->next;
3347 free(p);
3348 removed++;
3349 compact_treesame(revs, commit, nth_parent);
3350 continue;
3351 }
3352 pp = &p->next;
3353 nth_parent++;
3354 }
3355
3356 /* Removing parents can only increase TREESAMEness */
3357 if (removed && !(commit->object.flags & TREESAME))
3358 update_treesame(revs, commit);
3359
3360 return nth_parent;
3361}
3362
faf0156b 3363static struct commit_list **simplify_one(struct rev_info *revs, struct commit *commit, struct commit_list **tail)
6546b593
JH
3364{
3365 struct commit_list *p;
4d826608 3366 struct commit *parent;
faf0156b 3367 struct merge_simplify_state *st, *pst;
6546b593
JH
3368 int cnt;
3369
faf0156b
JH
3370 st = locate_simplify_state(revs, commit);
3371
6546b593 3372 /*
6546b593
JH
3373 * Have we handled this one?
3374 */
faf0156b 3375 if (st->simplified)
6546b593
JH
3376 return tail;
3377
3378 /*
3379 * An UNINTERESTING commit simplifies to itself, so does a
3380 * root commit. We do not rewrite parents of such commit
3381 * anyway.
3382 */
3383 if ((commit->object.flags & UNINTERESTING) || !commit->parents) {
faf0156b 3384 st->simplified = commit;
6546b593
JH
3385 return tail;
3386 }
3387
3388 /*
6e513ba3
JH
3389 * Do we know what commit all of our parents that matter
3390 * should be rewritten to? Otherwise we are not ready to
3391 * rewrite this one yet.
6546b593
JH
3392 */
3393 for (cnt = 0, p = commit->parents; p; p = p->next) {
faf0156b
JH
3394 pst = locate_simplify_state(revs, p->item);
3395 if (!pst->simplified) {
6546b593
JH
3396 tail = &commit_list_insert(p->item, tail)->next;
3397 cnt++;
3398 }
6e513ba3
JH
3399 if (revs->first_parent_only)
3400 break;
6546b593 3401 }
53030f8d
JH
3402 if (cnt) {
3403 tail = &commit_list_insert(commit, tail)->next;
6546b593 3404 return tail;
53030f8d 3405 }
6546b593
JH
3406
3407 /*
d0af663e
KB
3408 * Rewrite our list of parents. Note that this cannot
3409 * affect our TREESAME flags in any way - a commit is
3410 * always TREESAME to its simplification.
6546b593 3411 */
faf0156b
JH
3412 for (p = commit->parents; p; p = p->next) {
3413 pst = locate_simplify_state(revs, p->item);
3414 p->item = pst->simplified;
6e513ba3
JH
3415 if (revs->first_parent_only)
3416 break;
faf0156b 3417 }
4b7f53da 3418
0290bf12 3419 if (revs->first_parent_only)
6e513ba3 3420 cnt = 1;
0290bf12 3421 else
d0af663e 3422 cnt = remove_duplicate_parents(revs, commit);
6546b593
JH
3423
3424 /*
3425 * It is possible that we are a merge and one side branch
3426 * does not have any commit that touches the given paths;
d0af663e
KB
3427 * in such a case, the immediate parent from that branch
3428 * will be rewritten to be the merge base.
6546b593
JH
3429 *
3430 * o----X X: the commit we are looking at;
3431 * / / o: a commit that touches the paths;
3432 * ---o----'
3433 *
143f1eaf
KB
3434 * Further, a merge of an independent branch that doesn't
3435 * touch the path will reduce to a treesame root parent:
3436 *
3437 * ----o----X X: the commit we are looking at;
3438 * / o: a commit that touches the paths;
3439 * r r: a root commit not touching the paths
3440 *
3441 * Detect and simplify both cases.
6546b593
JH
3442 */
3443 if (1 < cnt) {
91633995
JK
3444 int marked = mark_redundant_parents(commit);
3445 marked += mark_treesame_root_parents(commit);
9c129eab
KB
3446 if (marked)
3447 marked -= leave_one_treesame_to_parent(revs, commit);
d0af663e
KB
3448 if (marked)
3449 cnt = remove_marked_parents(revs, commit);
6546b593
JH
3450 }
3451
3452 /*
3453 * A commit simplifies to itself if it is a root, if it is
3454 * UNINTERESTING, if it touches the given paths, or if it is a
4d826608 3455 * merge and its parents don't simplify to one relevant commit
6546b593
JH
3456 * (the first two cases are already handled at the beginning of
3457 * this function).
3458 *
4d826608
KB
3459 * Otherwise, it simplifies to what its sole relevant parent
3460 * simplifies to.
6546b593
JH
3461 */
3462 if (!cnt ||
3463 (commit->object.flags & UNINTERESTING) ||
3464 !(commit->object.flags & TREESAME) ||
8d049e18
DS
3465 (parent = one_relevant_parent(revs, commit->parents)) == NULL ||
3466 (revs->show_pulls && (commit->object.flags & PULL_MERGE)))
faf0156b
JH
3467 st->simplified = commit;
3468 else {
4d826608 3469 pst = locate_simplify_state(revs, parent);
faf0156b
JH
3470 st->simplified = pst->simplified;
3471 }
6546b593
JH
3472 return tail;
3473}
3474
3475static void simplify_merges(struct rev_info *revs)
3476{
ab9d75a8 3477 struct commit_list *list, *next;
6546b593 3478 struct commit_list *yet_to_do, **tail;
ab9d75a8 3479 struct commit *commit;
6546b593 3480
5eac739e
JH
3481 if (!revs->prune)
3482 return;
65347030 3483
6546b593
JH
3484 /* feed the list reversed */
3485 yet_to_do = NULL;
ab9d75a8
JH
3486 for (list = revs->commits; list; list = next) {
3487 commit = list->item;
3488 next = list->next;
3489 /*
3490 * Do not free(list) here yet; the original list
3491 * is used later in this function.
3492 */
3493 commit_list_insert(commit, &yet_to_do);
3494 }
6546b593
JH
3495 while (yet_to_do) {
3496 list = yet_to_do;
3497 yet_to_do = NULL;
3498 tail = &yet_to_do;
3499 while (list) {
e510ab89 3500 commit = pop_commit(&list);
faf0156b 3501 tail = simplify_one(revs, commit, tail);
6546b593
JH
3502 }
3503 }
3504
3505 /* clean up the result, removing the simplified ones */
3506 list = revs->commits;
3507 revs->commits = NULL;
3508 tail = &revs->commits;
3509 while (list) {
faf0156b 3510 struct merge_simplify_state *st;
ab9d75a8 3511
e510ab89 3512 commit = pop_commit(&list);
faf0156b
JH
3513 st = locate_simplify_state(revs, commit);
3514 if (st->simplified == commit)
6546b593
JH
3515 tail = &commit_list_insert(commit, tail)->next;
3516 }
6546b593
JH
3517}
3518
f35f5603
JH
3519static void set_children(struct rev_info *revs)
3520{
3521 struct commit_list *l;
3522 for (l = revs->commits; l; l = l->next) {
3523 struct commit *commit = l->item;
3524 struct commit_list *p;
3525
3526 for (p = commit->parents; p; p = p->next)
3527 add_child(revs, p->item, commit);
3528 }
3529}
3530
bcc0a3ea
HV
3531void reset_revision_walk(void)
3532{
ffa1f28f 3533 clear_object_flags(SEEN | ADDED | SHOWN | TOPO_WALK_EXPLORED | TOPO_WALK_INDEGREE);
bcc0a3ea
HV
3534}
3535
df11e196 3536static int mark_uninteresting(const struct object_id *oid,
be252d33
JK
3537 struct packed_git *pack UNUSED,
3538 uint32_t pos UNUSED,
b3c7eef9 3539 void *cb)
df11e196 3540{
b3c7eef9 3541 struct rev_info *revs = cb;
c1fa951d 3542 struct object *o = lookup_unknown_object(revs->repo, oid);
df11e196
JT
3543 o->flags |= UNINTERESTING | SEEN;
3544 return 0;
3545}
3546
b4542418
DS
3547define_commit_slab(indegree_slab, int);
3548define_commit_slab(author_date_slab, timestamp_t);
3549
3550struct topo_walk_info {
d7f92784 3551 timestamp_t min_generation;
b4542418
DS
3552 struct prio_queue explore_queue;
3553 struct prio_queue indegree_queue;
3554 struct prio_queue topo_queue;
3555 struct indegree_slab indegree;
3556 struct author_date_slab author_date;
3557};
3558
90b666da
DS
3559static int topo_walk_atexit_registered;
3560static unsigned int count_explore_walked;
3561static unsigned int count_indegree_walked;
3562static unsigned int count_topo_walked;
3563
3564static void trace2_topo_walk_statistics_atexit(void)
3565{
3566 struct json_writer jw = JSON_WRITER_INIT;
3567
3568 jw_object_begin(&jw, 0);
3569 jw_object_intmax(&jw, "count_explore_walked", count_explore_walked);
3570 jw_object_intmax(&jw, "count_indegree_walked", count_indegree_walked);
3571 jw_object_intmax(&jw, "count_topo_walked", count_topo_walked);
3572 jw_end(&jw);
3573
3574 trace2_data_json("topo_walk", the_repository, "statistics", &jw);
3575
3576 jw_release(&jw);
3577}
3578
b4542418
DS
3579static inline void test_flag_and_insert(struct prio_queue *q, struct commit *c, int flag)
3580{
3581 if (c->object.flags & flag)
3582 return;
3583
3584 c->object.flags |= flag;
3585 prio_queue_put(q, c);
3586}
3587
3588static void explore_walk_step(struct rev_info *revs)
3589{
3590 struct topo_walk_info *info = revs->topo_walk_info;
3591 struct commit_list *p;
3592 struct commit *c = prio_queue_get(&info->explore_queue);
3593
3594 if (!c)
3595 return;
3596
ea3f7e59 3597 if (repo_parse_commit_gently(revs->repo, c, 1) < 0)
b4542418
DS
3598 return;
3599
90b666da
DS
3600 count_explore_walked++;
3601
b4542418
DS
3602 if (revs->sort_order == REV_SORT_BY_AUTHOR_DATE)
3603 record_author_date(&info->author_date, c);
3604
3605 if (revs->max_age != -1 && (c->date < revs->max_age))
3606 c->object.flags |= UNINTERESTING;
3607
3608 if (process_parents(revs, c, NULL, NULL) < 0)
3609 return;
3610
3611 if (c->object.flags & UNINTERESTING)
9d505b7b 3612 mark_parents_uninteresting(revs, c);
b4542418
DS
3613
3614 for (p = c->parents; p; p = p->next)
3615 test_flag_and_insert(&info->explore_queue, p->item, TOPO_WALK_EXPLORED);
3616}
3617
3618static void explore_to_depth(struct rev_info *revs,
d7f92784 3619 timestamp_t gen_cutoff)
b4542418
DS
3620{
3621 struct topo_walk_info *info = revs->topo_walk_info;
3622 struct commit *c;
3623 while ((c = prio_queue_peek(&info->explore_queue)) &&
c49c82aa 3624 commit_graph_generation(c) >= gen_cutoff)
b4542418
DS
3625 explore_walk_step(revs);
3626}
3627
3628static void indegree_walk_step(struct rev_info *revs)
3629{
3630 struct commit_list *p;
3631 struct topo_walk_info *info = revs->topo_walk_info;
3632 struct commit *c = prio_queue_get(&info->indegree_queue);
3633
3634 if (!c)
3635 return;
3636
ea3f7e59 3637 if (repo_parse_commit_gently(revs->repo, c, 1) < 0)
b4542418
DS
3638 return;
3639
90b666da
DS
3640 count_indegree_walked++;
3641
c49c82aa 3642 explore_to_depth(revs, commit_graph_generation(c));
b4542418
DS
3643
3644 for (p = c->parents; p; p = p->next) {
3645 struct commit *parent = p->item;
3646 int *pi = indegree_slab_at(&info->indegree, parent);
3647
2f9bbb6d
AK
3648 if (repo_parse_commit_gently(revs->repo, parent, 1) < 0)
3649 return;
3650
b4542418
DS
3651 if (*pi)
3652 (*pi)++;
3653 else
3654 *pi = 2;
3655
3656 test_flag_and_insert(&info->indegree_queue, parent, TOPO_WALK_INDEGREE);
3657
3658 if (revs->first_parent_only)
3659 return;
3660 }
3661}
3662
3663static void compute_indegrees_to_depth(struct rev_info *revs,
d7f92784 3664 timestamp_t gen_cutoff)
b4542418
DS
3665{
3666 struct topo_walk_info *info = revs->topo_walk_info;
3667 struct commit *c;
3668 while ((c = prio_queue_peek(&info->indegree_queue)) &&
c49c82aa 3669 commit_graph_generation(c) >= gen_cutoff)
b4542418
DS
3670 indegree_walk_step(revs);
3671}
f0d9cc41 3672
ae1b383d 3673static void release_revisions_topo_walk_info(struct topo_walk_info *info)
0aa0c2b2 3674{
ae1b383d
ÆAB
3675 if (!info)
3676 return;
0aa0c2b2
MH
3677 clear_prio_queue(&info->explore_queue);
3678 clear_prio_queue(&info->indegree_queue);
3679 clear_prio_queue(&info->topo_queue);
3680 clear_indegree_slab(&info->indegree);
3681 clear_author_date_slab(&info->author_date);
ae1b383d
ÆAB
3682 free(info);
3683}
0aa0c2b2 3684
ae1b383d
ÆAB
3685static void reset_topo_walk(struct rev_info *revs)
3686{
3687 release_revisions_topo_walk_info(revs->topo_walk_info);
3688 revs->topo_walk_info = NULL;
0aa0c2b2
MH
3689}
3690
f0d9cc41
DS
3691static void init_topo_walk(struct rev_info *revs)
3692{
3693 struct topo_walk_info *info;
b4542418 3694 struct commit_list *list;
0aa0c2b2
MH
3695 if (revs->topo_walk_info)
3696 reset_topo_walk(revs);
3697
f0d9cc41
DS
3698 revs->topo_walk_info = xmalloc(sizeof(struct topo_walk_info));
3699 info = revs->topo_walk_info;
3700 memset(info, 0, sizeof(struct topo_walk_info));
3701
b4542418
DS
3702 init_indegree_slab(&info->indegree);
3703 memset(&info->explore_queue, 0, sizeof(info->explore_queue));
3704 memset(&info->indegree_queue, 0, sizeof(info->indegree_queue));
3705 memset(&info->topo_queue, 0, sizeof(info->topo_queue));
3706
3707 switch (revs->sort_order) {
3708 default: /* REV_SORT_IN_GRAPH_ORDER */
3709 info->topo_queue.compare = NULL;
3710 break;
3711 case REV_SORT_BY_COMMIT_DATE:
3712 info->topo_queue.compare = compare_commits_by_commit_date;
3713 break;
3714 case REV_SORT_BY_AUTHOR_DATE:
3715 init_author_date_slab(&info->author_date);
3716 info->topo_queue.compare = compare_commits_by_author_date;
3717 info->topo_queue.cb_data = &info->author_date;
3718 break;
3719 }
3720
3721 info->explore_queue.compare = compare_commits_by_gen_then_commit_date;
3722 info->indegree_queue.compare = compare_commits_by_gen_then_commit_date;
3723
3724 info->min_generation = GENERATION_NUMBER_INFINITY;
3725 for (list = revs->commits; list; list = list->next) {
3726 struct commit *c = list->item;
d7f92784 3727 timestamp_t generation;
b4542418 3728
ea3f7e59 3729 if (repo_parse_commit_gently(revs->repo, c, 1))
b4542418
DS
3730 continue;
3731
3732 test_flag_and_insert(&info->explore_queue, c, TOPO_WALK_EXPLORED);
3733 test_flag_and_insert(&info->indegree_queue, c, TOPO_WALK_INDEGREE);
3734
c752ad09
AK
3735 generation = commit_graph_generation(c);
3736 if (generation < info->min_generation)
3737 info->min_generation = generation;
b4542418
DS
3738
3739 *(indegree_slab_at(&info->indegree, c)) = 1;
3740
3741 if (revs->sort_order == REV_SORT_BY_AUTHOR_DATE)
3742 record_author_date(&info->author_date, c);
3743 }
3744 compute_indegrees_to_depth(revs, info->min_generation);
3745
3746 for (list = revs->commits; list; list = list->next) {
3747 struct commit *c = list->item;
3748
3749 if (*(indegree_slab_at(&info->indegree, c)) == 1)
3750 prio_queue_put(&info->topo_queue, c);
3751 }
3752
3753 /*
3754 * This is unfortunate; the initial tips need to be shown
3755 * in the order given from the revision traversal machinery.
3756 */
3757 if (revs->sort_order == REV_SORT_IN_GRAPH_ORDER)
3758 prio_queue_reverse(&info->topo_queue);
90b666da
DS
3759
3760 if (trace2_is_enabled() && !topo_walk_atexit_registered) {
3761 atexit(trace2_topo_walk_statistics_atexit);
3762 topo_walk_atexit_registered = 1;
3763 }
f0d9cc41
DS
3764}
3765
3766static struct commit *next_topo_commit(struct rev_info *revs)
3767{
b4542418
DS
3768 struct commit *c;
3769 struct topo_walk_info *info = revs->topo_walk_info;
3770
3771 /* pop next off of topo_queue */
3772 c = prio_queue_get(&info->topo_queue);
3773
3774 if (c)
3775 *(indegree_slab_at(&info->indegree, c)) = 0;
3776
3777 return c;
f0d9cc41
DS
3778}
3779
3780static void expand_topo_walk(struct rev_info *revs, struct commit *commit)
3781{
b4542418
DS
3782 struct commit_list *p;
3783 struct topo_walk_info *info = revs->topo_walk_info;
3784 if (process_parents(revs, commit, NULL, NULL) < 0) {
f0d9cc41
DS
3785 if (!revs->ignore_missing_links)
3786 die("Failed to traverse parents of commit %s",
3787 oid_to_hex(&commit->object.oid));
3788 }
b4542418 3789
90b666da
DS
3790 count_topo_walked++;
3791
b4542418
DS
3792 for (p = commit->parents; p; p = p->next) {
3793 struct commit *parent = p->item;
3794 int *pi;
d7f92784 3795 timestamp_t generation;
b4542418 3796
1d8e31a3
DS
3797 if (parent->object.flags & UNINTERESTING)
3798 continue;
3799
ea3f7e59 3800 if (repo_parse_commit_gently(revs->repo, parent, 1) < 0)
b4542418
DS
3801 continue;
3802
c752ad09
AK
3803 generation = commit_graph_generation(parent);
3804 if (generation < info->min_generation) {
3805 info->min_generation = generation;
b4542418
DS
3806 compute_indegrees_to_depth(revs, info->min_generation);
3807 }
3808
3809 pi = indegree_slab_at(&info->indegree, parent);
3810
3811 (*pi)--;
3812 if (*pi == 1)
3813 prio_queue_put(&info->topo_queue, parent);
3814
3815 if (revs->first_parent_only)
3816 return;
3817 }
f0d9cc41
DS
3818}
3819
cc0e6c5a 3820int prepare_revision_walk(struct rev_info *revs)
a4a88b2b 3821{
1da1e07c
JK
3822 int i;
3823 struct object_array old_pending;
2e7da8e9 3824 struct commit_list **next = &revs->commits;
cd2bdc53 3825
1da1e07c 3826 memcpy(&old_pending, &revs->pending, sizeof(old_pending));
1f1e895f
LT
3827 revs->pending.nr = 0;
3828 revs->pending.alloc = 0;
3829 revs->pending.objects = NULL;
1da1e07c
JK
3830 for (i = 0; i < old_pending.nr; i++) {
3831 struct object_array_entry *e = old_pending.objects + i;
20739490 3832 struct commit *commit = handle_commit(revs, e);
cd2bdc53
LT
3833 if (commit) {
3834 if (!(commit->object.flags & SEEN)) {
3835 commit->object.flags |= SEEN;
2e7da8e9 3836 next = commit_list_append(commit, next);
cd2bdc53
LT
3837 }
3838 }
cd2bdc53 3839 }
f1230fb5 3840 object_array_clear(&old_pending);
cd2bdc53 3841
d0af663e 3842 /* Signal whether we need per-parent treesame decoration */
4d826608
KB
3843 if (revs->simplify_merges ||
3844 (revs->limited && limiting_can_increase_treesame(revs)))
d0af663e
KB
3845 revs->treesame.name = "treesame";
3846
df11e196 3847 if (revs->exclude_promisor_objects) {
b3c7eef9 3848 for_each_packed_object(mark_uninteresting, revs,
df11e196
JT
3849 FOR_EACH_OBJECT_PROMISOR_ONLY);
3850 }
3851
9830926c
KN
3852 oidset_init(&revs->missing_commits, 0);
3853
f32dde8c 3854 if (!revs->reflog_info)
a56b9464 3855 prepare_to_use_bloom_filter(revs);
29ef1f27 3856 if (!revs->unsorted_input)
ca92e59e 3857 commit_list_sort_by_date(&revs->commits);
ba1d4505 3858 if (revs->no_walk)
cc0e6c5a 3859 return 0;
f0d9cc41 3860 if (revs->limited) {
cc0e6c5a
AR
3861 if (limit_list(revs) < 0)
3862 return -1;
f0d9cc41
DS
3863 if (revs->topo_order)
3864 sort_in_topological_order(&revs->commits, revs->sort_order);
3865 } else if (revs->topo_order)
3866 init_topo_walk(revs);
3cb9d2b6
SG
3867 if (revs->line_level_traverse && want_ancestry(revs))
3868 /*
3869 * At the moment we can only do line-level log with parent
3870 * rewriting by performing this expensive pre-filtering step.
3871 * If parent rewriting is not requested, then we rather
3872 * perform the line-level log filtering during the regular
3873 * history traversal.
3874 */
12da1d1f 3875 line_log_filter(revs);
6546b593
JH
3876 if (revs->simplify_merges)
3877 simplify_merges(revs);
f35f5603
JH
3878 if (revs->children.name)
3879 set_children(revs);
a56b9464 3880
cc0e6c5a 3881 return 0;
a4a88b2b
LT
3882}
3883
8320b1db
JK
3884static enum rewrite_result rewrite_one_1(struct rev_info *revs,
3885 struct commit **pp,
3886 struct prio_queue *queue)
765ac8ec
LT
3887{
3888 for (;;) {
3889 struct commit *p = *pp;
3381c790 3890 if (!revs->limited)
8320b1db 3891 if (process_parents(revs, p, NULL, queue) < 0)
cc0e6c5a 3892 return rewrite_one_error;
7dc0fe3b
LT
3893 if (p->object.flags & UNINTERESTING)
3894 return rewrite_one_ok;
3895 if (!(p->object.flags & TREESAME))
cc0e6c5a 3896 return rewrite_one_ok;
765ac8ec 3897 if (!p->parents)
cc0e6c5a 3898 return rewrite_one_noparents;
afe8a907 3899 if (!(p = one_relevant_parent(revs, p->parents)))
4d826608
KB
3900 return rewrite_one_ok;
3901 *pp = p;
765ac8ec
LT
3902 }
3903}
3904
8320b1db
JK
3905static void merge_queue_into_list(struct prio_queue *q, struct commit_list **list)
3906{
3907 while (q->nr) {
3908 struct commit *item = prio_queue_peek(q);
3909 struct commit_list *p = *list;
3910
3911 if (p && p->item->date >= item->date)
3912 list = &p->next;
3913 else {
3914 p = commit_list_insert(item, list);
3915 list = &p->next; /* skip newly added item */
3916 prio_queue_get(q); /* pop item */
3917 }
3918 }
3919}
3920
3921static enum rewrite_result rewrite_one(struct rev_info *revs, struct commit **pp)
3922{
3923 struct prio_queue queue = { compare_commits_by_commit_date };
3924 enum rewrite_result ret = rewrite_one_1(revs, pp, &queue);
3925 merge_queue_into_list(&queue, &revs->commits);
3926 clear_prio_queue(&queue);
3927 return ret;
3928}
3929
c7edcae0
BY
3930int rewrite_parents(struct rev_info *revs, struct commit *commit,
3931 rewrite_parent_fn_t rewrite_parent)
765ac8ec
LT
3932{
3933 struct commit_list **pp = &commit->parents;
3934 while (*pp) {
3935 struct commit_list *parent = *pp;
c7edcae0 3936 switch (rewrite_parent(revs, &parent->item)) {
cc0e6c5a
AR
3937 case rewrite_one_ok:
3938 break;
3939 case rewrite_one_noparents:
765ac8ec
LT
3940 *pp = parent->next;
3941 continue;
cc0e6c5a
AR
3942 case rewrite_one_error:
3943 return -1;
765ac8ec
LT
3944 }
3945 pp = &parent->next;
3946 }
d0af663e 3947 remove_duplicate_parents(revs, commit);
cc0e6c5a 3948 return 0;
765ac8ec
LT
3949}
3950
8ecae9b0
JH
3951static int commit_match(struct commit *commit, struct rev_info *opt)
3952{
72fd13f7 3953 int retval;
04deccda 3954 const char *encoding;
b000c59b 3955 const char *message;
72fd13f7 3956 struct strbuf buf = STRBUF_INIT;
04deccda 3957
80235ba7 3958 if (!opt->grep_filter.pattern_list && !opt->grep_filter.header_list)
8ecae9b0 3959 return 1;
baa6378f
JH
3960
3961 /* Prepend "fake" headers as needed */
3962 if (opt->grep_filter.use_reflog_filter) {
72fd13f7
NTND
3963 strbuf_addstr(&buf, "reflog ");
3964 get_reflog_message(&buf, opt->reflog_info);
3965 strbuf_addch(&buf, '\n');
72fd13f7 3966 }
baa6378f 3967
04deccda
JK
3968 /*
3969 * We grep in the user's output encoding, under the assumption that it
3970 * is the encoding they are most likely to write their grep pattern
3971 * for. In addition, it means we will match the "notes" encoding below,
3972 * so we will not end up with a buffer that has two different encodings
3973 * in it.
3974 */
3975 encoding = get_log_output_encoding();
ecb5091f 3976 message = repo_logmsg_reencode(the_repository, commit, NULL, encoding);
04deccda 3977
baa6378f
JH
3978 /* Copy the commit to temporary if we are using "fake" headers */
3979 if (buf.len)
04deccda 3980 strbuf_addstr(&buf, message);
baa6378f 3981
df874fa8 3982 if (opt->grep_filter.header_list && opt->mailmap) {
e9c1b0e3
SA
3983 const char *commit_headers[] = { "author ", "committer ", NULL };
3984
d72fbe81 3985 if (!buf.len)
04deccda 3986 strbuf_addstr(&buf, message);
d72fbe81 3987
66a8a953 3988 apply_mailmap_to_header(&buf, commit_headers, opt->mailmap);
d72fbe81
AP
3989 }
3990
38cfe915
NTND
3991 /* Append "fake" message parts as needed */
3992 if (opt->show_notes) {
3993 if (!buf.len)
04deccda 3994 strbuf_addstr(&buf, message);
fb61e4d3 3995 format_display_notes(&commit->object.oid, &buf, encoding, 1);
38cfe915
NTND
3996 }
3997
b000c59b
JK
3998 /*
3999 * Find either in the original commit message, or in the temporary.
4000 * Note that we cast away the constness of "message" here. It is
4001 * const because it may come from the cached commit buffer. That's OK,
4002 * because we know that it is modifiable heap memory, and that while
4003 * grep_buffer may modify it for speed, it will restore any
4004 * changes before returning.
4005 */
72fd13f7
NTND
4006 if (buf.len)
4007 retval = grep_buffer(&opt->grep_filter, buf.buf, buf.len);
4008 else
4009 retval = grep_buffer(&opt->grep_filter,
b000c59b 4010 (char *)message, strlen(message));
72fd13f7 4011 strbuf_release(&buf);
ecb5091f 4012 repo_unuse_commit_buffer(the_repository, commit, message);
794c0002 4013 return retval;
8ecae9b0
JH
4014}
4015
53d00b39 4016static inline int want_ancestry(const struct rev_info *revs)
f35f5603 4017{
8bb65883 4018 return (revs->rewrite_parents || revs->children.name);
f35f5603
JH
4019}
4020
de239446
JK
4021/*
4022 * Return a timestamp to be used for --since/--until comparisons for this
4023 * commit, based on the revision options.
4024 */
4025static timestamp_t comparison_date(const struct rev_info *revs,
4026 struct commit *commit)
4027{
4028 return revs->reflog_info ?
4029 get_reflog_timestamp(revs->reflog_info) :
4030 commit->date;
4031}
4032
beb5af43 4033enum commit_action get_commit_action(struct rev_info *revs, struct commit *commit)
252a7c02
LT
4034{
4035 if (commit->object.flags & SHOWN)
4036 return commit_ignore;
14c3c80c 4037 if (revs->unpacked && has_object_pack(&commit->object.oid))
252a7c02 4038 return commit_ignore;
c9fff000
TB
4039 if (revs->no_kept_objects) {
4040 if (has_object_kept_pack(&commit->object.oid,
4041 revs->keep_pack_cache_flags))
4042 return commit_ignore;
4043 }
252a7c02
LT
4044 if (commit->object.flags & UNINTERESTING)
4045 return commit_ignore;
3cb9d2b6
SG
4046 if (revs->line_level_traverse && !want_ancestry(revs)) {
4047 /*
4048 * In case of line-level log with parent rewriting
4049 * prepare_revision_walk() already took care of all line-level
4050 * log filtering, and there is nothing left to do here.
4051 *
4052 * If parent rewriting was not requested, then this is the
4053 * place to perform the line-level log filtering. Notably,
4054 * this check, though expensive, must come before the other,
4055 * cheaper filtering conditions, because the tracked line
4056 * ranges must be adjusted even when the commit will end up
4057 * being ignored based on other conditions.
4058 */
4059 if (!line_log_process_ranges_arbitrary_commit(revs, commit))
4060 return commit_ignore;
4061 }
de239446
JK
4062 if (revs->min_age != -1 &&
4063 comparison_date(revs, commit) > revs->min_age)
4064 return commit_ignore;
96697781
MV
4065 if (revs->max_age_as_filter != -1 &&
4066 comparison_date(revs, commit) < revs->max_age_as_filter)
4067 return commit_ignore;
ad5aeede 4068 if (revs->min_parents || (revs->max_parents >= 0)) {
bf3418b0 4069 int n = commit_list_count(commit->parents);
ad5aeede
MG
4070 if ((n < revs->min_parents) ||
4071 ((revs->max_parents >= 0) && (n > revs->max_parents)))
4072 return commit_ignore;
4073 }
252a7c02
LT
4074 if (!commit_match(commit, revs))
4075 return commit_ignore;
53b2c823 4076 if (revs->prune && revs->dense) {
252a7c02 4077 /* Commit without changes? */
7dc0fe3b 4078 if (commit->object.flags & TREESAME) {
bf3418b0
KB
4079 int n;
4080 struct commit_list *p;
252a7c02 4081 /* drop merges unless we want parenthood */
f35f5603 4082 if (!want_ancestry(revs))
252a7c02 4083 return commit_ignore;
8d049e18
DS
4084
4085 if (revs->show_pulls && (commit->object.flags & PULL_MERGE))
4086 return commit_show;
4087
bf3418b0
KB
4088 /*
4089 * If we want ancestry, then need to keep any merges
4090 * between relevant commits to tie together topology.
4091 * For consistency with TREESAME and simplification
4092 * use "relevant" here rather than just INTERESTING,
4093 * to treat bottom commit(s) as part of the topology.
4094 */
4095 for (n = 0, p = commit->parents; p; p = p->next)
4096 if (relevant_commit(p->item))
4097 if (++n >= 2)
4098 return commit_show;
4099 return commit_ignore;
252a7c02 4100 }
252a7c02
LT
4101 }
4102 return commit_show;
4103}
4104
0131c490
JH
4105define_commit_slab(saved_parents, struct commit_list *);
4106
4107#define EMPTY_PARENT_LIST ((struct commit_list *)-1)
4108
4109/*
4110 * You may only call save_parents() once per commit (this is checked
4111 * for non-root commits).
4112 */
4113static void save_parents(struct rev_info *revs, struct commit *commit)
4114{
4115 struct commit_list **pp;
4116
4117 if (!revs->saved_parents_slab) {
4118 revs->saved_parents_slab = xmalloc(sizeof(struct saved_parents));
4119 init_saved_parents(revs->saved_parents_slab);
4120 }
4121
4122 pp = saved_parents_at(revs->saved_parents_slab, commit);
4123
4124 /*
4125 * When walking with reflogs, we may visit the same commit
4126 * several times: once for each appearance in the reflog.
4127 *
4128 * In this case, save_parents() will be called multiple times.
4129 * We want to keep only the first set of parents. We need to
4130 * store a sentinel value for an empty (i.e., NULL) parent
4131 * list to distinguish it from a not-yet-saved list, however.
4132 */
4133 if (*pp)
4134 return;
4135 if (commit->parents)
4136 *pp = copy_commit_list(commit->parents);
4137 else
4138 *pp = EMPTY_PARENT_LIST;
4139}
4140
4141static void free_saved_parents(struct rev_info *revs)
4142{
4143 if (revs->saved_parents_slab)
4144 clear_saved_parents(revs->saved_parents_slab);
4145}
4146
4147struct commit_list *get_saved_parents(struct rev_info *revs, const struct commit *commit)
4148{
4149 struct commit_list *parents;
4150
4151 if (!revs->saved_parents_slab)
4152 return commit->parents;
4153
4154 parents = *saved_parents_at(revs->saved_parents_slab, commit);
4155 if (parents == EMPTY_PARENT_LIST)
4156 return NULL;
4157 return parents;
4158}
4159
beb5af43
AS
4160enum commit_action simplify_commit(struct rev_info *revs, struct commit *commit)
4161{
4162 enum commit_action action = get_commit_action(revs, commit);
4163
4164 if (action == commit_show &&
beb5af43 4165 revs->prune && revs->dense && want_ancestry(revs)) {
53d00b39
TR
4166 /*
4167 * --full-diff on simplified parents is no good: it
4168 * will show spurious changes from the commits that
4169 * were elided. So we save the parents on the side
4170 * when --full-diff is in effect.
4171 */
4172 if (revs->full_diff)
4173 save_parents(revs, commit);
c7edcae0 4174 if (rewrite_parents(revs, commit, rewrite_one) < 0)
beb5af43
AS
4175 return commit_error;
4176 }
4177 return action;
4178}
4179
1b32dece
NTND
4180static void track_linear(struct rev_info *revs, struct commit *commit)
4181{
4182 if (revs->track_first_time) {
4183 revs->linear = 1;
4184 revs->track_first_time = 0;
4185 } else {
4186 struct commit_list *p;
4187 for (p = revs->previous_parents; p; p = p->next)
4188 if (p->item == NULL || /* first commit */
4a7e27e9 4189 oideq(&p->item->object.oid, &commit->object.oid))
1b32dece
NTND
4190 break;
4191 revs->linear = p != NULL;
4192 }
4193 if (revs->reverse) {
4194 if (revs->linear)
4195 commit->object.flags |= TRACK_LINEAR;
4196 }
4197 free_commit_list(revs->previous_parents);
4198 revs->previous_parents = copy_commit_list(commit->parents);
4199}
4200
d5db6c9e 4201static struct commit *get_revision_1(struct rev_info *revs)
a4a88b2b 4202{
7c2f08aa 4203 while (1) {
d08565bf 4204 struct commit *commit;
a4a88b2b 4205
d08565bf
JK
4206 if (revs->reflog_info)
4207 commit = next_reflog_entry(revs->reflog_info);
f0d9cc41
DS
4208 else if (revs->topo_walk_info)
4209 commit = next_topo_commit(revs);
d08565bf
JK
4210 else
4211 commit = pop_commit(&revs->commits);
2a0925be 4212
7c2f08aa
JK
4213 if (!commit)
4214 return NULL;
4215
d08565bf 4216 if (revs->reflog_info)
ffa1eeae 4217 commit->object.flags &= ~(ADDED | SEEN | SHOWN);
8860fd42 4218
2a0925be
LT
4219 /*
4220 * If we haven't done the list limiting, we need to look at
be7db6e5
LT
4221 * the parents here. We also need to do the date-based limiting
4222 * that we'd otherwise have done in limit_list().
2a0925be 4223 */
be7db6e5 4224 if (!revs->limited) {
744f4985 4225 if (revs->max_age != -1 &&
de239446 4226 comparison_date(revs, commit) < revs->max_age)
86ab4906 4227 continue;
d08565bf
JK
4228
4229 if (revs->reflog_info)
4230 try_to_simplify_commit(revs, commit);
f0d9cc41
DS
4231 else if (revs->topo_walk_info)
4232 expand_topo_walk(revs, commit);
5284fc5c 4233 else if (process_parents(revs, commit, &revs->commits, NULL) < 0) {
2db1a43f
VM
4234 if (!revs->ignore_missing_links)
4235 die("Failed to traverse parents of commit %s",
f2fd0760 4236 oid_to_hex(&commit->object.oid));
2db1a43f 4237 }
be7db6e5 4238 }
744f4985 4239
252a7c02
LT
4240 switch (simplify_commit(revs, commit)) {
4241 case commit_ignore:
8ecae9b0 4242 continue;
252a7c02 4243 case commit_error:
ed62089c 4244 die("Failed to simplify parents of commit %s",
f2fd0760 4245 oid_to_hex(&commit->object.oid));
252a7c02 4246 default:
1b32dece
NTND
4247 if (revs->track_linear)
4248 track_linear(revs, commit);
252a7c02 4249 return commit;
384e99a4 4250 }
7c2f08aa 4251 }
765ac8ec 4252}
d5db6c9e 4253
be6754c6
MH
4254/*
4255 * Return true for entries that have not yet been shown. (This is an
4256 * object_array_each_func_t.)
4257 */
d3dcfa04 4258static int entry_unshown(struct object_array_entry *entry, void *cb_data UNUSED)
86ab4906 4259{
be6754c6
MH
4260 return !(entry->item->flags & SHOWN);
4261}
86ab4906 4262
be6754c6
MH
4263/*
4264 * If array is on the verge of a realloc, garbage-collect any entries
4265 * that have already been shown to try to free up some space.
4266 */
4267static void gc_boundary(struct object_array *array)
4268{
4269 if (array->nr == array->alloc)
4270 object_array_filter(array, entry_unshown, NULL);
86ab4906
JH
4271}
4272
4603ec0f
AS
4273static void create_boundary_commit_list(struct rev_info *revs)
4274{
4275 unsigned i;
4276 struct commit *c;
4277 struct object_array *array = &revs->boundary_commits;
4278 struct object_array_entry *objects = array->objects;
4279
4280 /*
4281 * If revs->commits is non-NULL at this point, an error occurred in
4282 * get_revision_1(). Ignore the error and continue printing the
4283 * boundary commits anyway. (This is what the code has always
4284 * done.)
4285 */
bf20fe4c
ÆAB
4286 free_commit_list(revs->commits);
4287 revs->commits = NULL;
4603ec0f
AS
4288
4289 /*
4290 * Put all of the actual boundary commits from revs->boundary_commits
4291 * into revs->commits
4292 */
4293 for (i = 0; i < array->nr; i++) {
4294 c = (struct commit *)(objects[i].item);
4295 if (!c)
4296 continue;
4297 if (!(c->object.flags & CHILD_SHOWN))
4298 continue;
4299 if (c->object.flags & (SHOWN | BOUNDARY))
4300 continue;
4301 c->object.flags |= BOUNDARY;
4302 commit_list_insert(c, &revs->commits);
4303 }
4304
4305 /*
4306 * If revs->topo_order is set, sort the boundary commits
4307 * in topological order
4308 */
08f704f2 4309 sort_in_topological_order(&revs->commits, revs->sort_order);
4603ec0f
AS
4310}
4311
7fefda5c 4312static struct commit *get_revision_internal(struct rev_info *revs)
d5db6c9e
JH
4313{
4314 struct commit *c = NULL;
86ab4906
JH
4315 struct commit_list *l;
4316
4317 if (revs->boundary == 2) {
4603ec0f
AS
4318 /*
4319 * All of the normal commits have already been returned,
4320 * and we are now returning boundary commits.
4321 * create_boundary_commit_list() has populated
4322 * revs->commits with the remaining commits to return.
4323 */
4324 c = pop_commit(&revs->commits);
4325 if (c)
4326 c->object.flags |= SHOWN;
9c5e66e9
JS
4327 return c;
4328 }
4329
86ab4906 4330 /*
b72a1904
JK
4331 * If our max_count counter has reached zero, then we are done. We
4332 * don't simply return NULL because we still might need to show
4333 * boundary commits. But we want to avoid calling get_revision_1, which
4334 * might do a considerable amount of work finding the next commit only
4335 * for us to throw it away.
4336 *
4337 * If it is non-zero, then either we don't have a max_count at all
4338 * (-1), or it is still counting, in which case we decrement.
86ab4906 4339 */
b72a1904
JK
4340 if (revs->max_count) {
4341 c = get_revision_1(revs);
4342 if (c) {
9e57ac55 4343 while (revs->skip_count > 0) {
b72a1904
JK
4344 revs->skip_count--;
4345 c = get_revision_1(revs);
4346 if (!c)
4347 break;
4348 }
8839ac94 4349 }
86ab4906 4350
b72a1904
JK
4351 if (revs->max_count > 0)
4352 revs->max_count--;
d5db6c9e 4353 }
86ab4906 4354
c33d8593
JH
4355 if (c)
4356 c->object.flags |= SHOWN;
4357
9e57ac55 4358 if (!revs->boundary)
d5db6c9e 4359 return c;
86ab4906
JH
4360
4361 if (!c) {
4362 /*
4363 * get_revision_1() runs out the commits, and
4364 * we are done computing the boundaries.
4365 * switch to boundary commits output mode.
4366 */
4367 revs->boundary = 2;
4603ec0f
AS
4368
4369 /*
4370 * Update revs->commits to contain the list of
4371 * boundary commits.
4372 */
4373 create_boundary_commit_list(revs);
4374
3c68d67b 4375 return get_revision_internal(revs);
86ab4906
JH
4376 }
4377
4378 /*
4379 * boundary commits are the commits that are parents of the
4380 * ones we got from get_revision_1() but they themselves are
4381 * not returned from get_revision_1(). Before returning
4382 * 'c', we need to mark its parents that they could be boundaries.
4383 */
4384
4385 for (l = c->parents; l; l = l->next) {
4386 struct object *p;
4387 p = &(l->item->object);
c33d8593 4388 if (p->flags & (CHILD_SHOWN | SHOWN))
86ab4906
JH
4389 continue;
4390 p->flags |= CHILD_SHOWN;
4391 gc_boundary(&revs->boundary_commits);
4392 add_object_array(p, NULL, &revs->boundary_commits);
4393 }
4394
4395 return c;
d5db6c9e 4396}
7fefda5c
AS
4397
4398struct commit *get_revision(struct rev_info *revs)
4399{
498bcd31
TR
4400 struct commit *c;
4401 struct commit_list *reversed;
4402
4403 if (revs->reverse) {
4404 reversed = NULL;
9e57ac55 4405 while ((c = get_revision_internal(revs)))
498bcd31 4406 commit_list_insert(c, &reversed);
498bcd31
TR
4407 revs->commits = reversed;
4408 revs->reverse = 0;
4409 revs->reverse_output_stage = 1;
4410 }
4411
1b32dece
NTND
4412 if (revs->reverse_output_stage) {
4413 c = pop_commit(&revs->commits);
4414 if (revs->track_linear)
4415 revs->linear = !!(c && c->object.flags & TRACK_LINEAR);
4416 return c;
4417 }
498bcd31
TR
4418
4419 c = get_revision_internal(revs);
7fefda5c
AS
4420 if (c && revs->graph)
4421 graph_update(revs->graph, c);
1b32dece 4422 if (!c) {
53d00b39 4423 free_saved_parents(revs);
bf20fe4c
ÆAB
4424 free_commit_list(revs->previous_parents);
4425 revs->previous_parents = NULL;
1b32dece 4426 }
7fefda5c
AS
4427 return c;
4428}
1df2d656 4429
49825164 4430const char *get_revision_mark(const struct rev_info *revs, const struct commit *commit)
1df2d656
MG
4431{
4432 if (commit->object.flags & BOUNDARY)
4433 return "-";
4434 else if (commit->object.flags & UNINTERESTING)
4435 return "^";
adbbb31e
MG
4436 else if (commit->object.flags & PATCHSAME)
4437 return "=";
1df2d656
MG
4438 else if (!revs || revs->left_right) {
4439 if (commit->object.flags & SYMMETRIC_LEFT)
4440 return "<";
4441 else
4442 return ">";
4443 } else if (revs->graph)
4444 return "*";
adbbb31e
MG
4445 else if (revs->cherry_mark)
4446 return "+";
1df2d656
MG
4447 return "";
4448}
b1b47554
MG
4449
4450void put_revision_mark(const struct rev_info *revs, const struct commit *commit)
4451{
49825164 4452 const char *mark = get_revision_mark(revs, commit);
b1b47554
MG
4453 if (!strlen(mark))
4454 return;
4455 fputs(mark, stdout);
4456 putchar(' ');
4457}