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