]> git.ipfire.org Git - thirdparty/git.git/blame - revision.c
The third batch
[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
bdbebe57 1647static int handle_one_ref(const struct reference *ref, void *cb_data)
ae563542 1648{
63049292 1649 struct all_refs_cb *cb = cb_data;
e7b432c5
JH
1650 struct object *object;
1651
bdbebe57 1652 if (ref_excluded(&cb->all_revs->ref_excludes, ref->name))
e7b432c5
JH
1653 return 0;
1654
bdbebe57
PS
1655 object = get_reference(cb->all_revs, ref->name, ref->oid, cb->all_flags);
1656 add_rev_cmdline(cb->all_revs, object, ref->name, REV_CMD_REF, cb->all_flags);
1657 add_pending_object(cb->all_revs, object, ref->name);
ae563542
LT
1658 return 0;
1659}
1660
d08bae7e
IL
1661static void init_all_refs_cb(struct all_refs_cb *cb, struct rev_info *revs,
1662 unsigned flags)
1663{
1664 cb->all_revs = revs;
1665 cb->all_flags = flags;
7ba82629 1666 revs->rev_input_given = 1;
ab3e1f78 1667 cb->wt = NULL;
d08bae7e
IL
1668}
1669
073cf63c
NTND
1670static void handle_refs(struct ref_store *refs,
1671 struct rev_info *revs, unsigned flags,
1672 int (*for_each)(struct ref_store *, each_ref_fn, void *))
ae563542 1673{
63049292 1674 struct all_refs_cb cb;
073cf63c
NTND
1675
1676 if (!refs) {
1677 /* this could happen with uninitialized submodules */
1678 return;
1679 }
1680
d08bae7e 1681 init_all_refs_cb(&cb, revs, flags);
073cf63c 1682 for_each(refs, handle_one_ref, &cb);
63049292
JH
1683}
1684
9461d272 1685static void handle_one_reflog_commit(struct object_id *oid, void *cb_data)
63049292
JH
1686{
1687 struct all_refs_cb *cb = cb_data;
9461d272 1688 if (!is_null_oid(oid)) {
b3c7eef9 1689 struct object *o = parse_object(cb->all_revs->repo, oid);
71b03b42
SP
1690 if (o) {
1691 o->flags |= cb->all_flags;
281eee47 1692 /* ??? CMDLINEFLAGS ??? */
71b03b42
SP
1693 add_pending_object(cb->all_revs, o, "");
1694 }
1695 else if (!cb->warned_bad_reflog) {
46efd2d9 1696 warning("reflog of '%s' references pruned commits",
71b03b42
SP
1697 cb->name_for_errormsg);
1698 cb->warned_bad_reflog = 1;
1699 }
63049292 1700 }
71b03b42
SP
1701}
1702
b9fd73a2
PS
1703static int handle_one_reflog_ent(const char *refname UNUSED,
1704 struct object_id *ooid, struct object_id *noid,
5cf88fd8
ÆAB
1705 const char *email UNUSED,
1706 timestamp_t timestamp UNUSED,
1707 int tz UNUSED,
1708 const char *message UNUSED,
c006e9fa 1709 void *cb_data)
71b03b42 1710{
9461d272 1711 handle_one_reflog_commit(ooid, cb_data);
1712 handle_one_reflog_commit(noid, cb_data);
63049292
JH
1713 return 0;
1714}
1715
31f89839 1716static int handle_one_reflog(const char *refname_in_wt, void *cb_data)
63049292
JH
1717{
1718 struct all_refs_cb *cb = cb_data;
ab3e1f78
NTND
1719 struct strbuf refname = STRBUF_INIT;
1720
71b03b42 1721 cb->warned_bad_reflog = 0;
ab3e1f78
NTND
1722 strbuf_worktree_ref(cb->wt, &refname, refname_in_wt);
1723 cb->name_for_errormsg = refname.buf;
1724 refs_for_each_reflog_ent(get_main_ref_store(the_repository),
1725 refname.buf,
acd9544a 1726 handle_one_reflog_ent, cb_data);
ab3e1f78 1727 strbuf_release(&refname);
63049292
JH
1728 return 0;
1729}
1730
acd9544a
NTND
1731static void add_other_reflogs_to_pending(struct all_refs_cb *cb)
1732{
1733 struct worktree **worktrees, **p;
1734
03f2465b 1735 worktrees = get_worktrees();
acd9544a
NTND
1736 for (p = worktrees; *p; p++) {
1737 struct worktree *wt = *p;
1738
1739 if (wt->is_current)
1740 continue;
1741
ab3e1f78
NTND
1742 cb->wt = wt;
1743 refs_for_each_reflog(get_worktree_ref_store(wt),
acd9544a
NTND
1744 handle_one_reflog,
1745 cb);
1746 }
1747 free_worktrees(worktrees);
1748}
1749
718ccc97 1750void add_reflogs_to_pending(struct rev_info *revs, unsigned flags)
63049292
JH
1751{
1752 struct all_refs_cb cb;
2b2a5be3 1753
63049292
JH
1754 cb.all_revs = revs;
1755 cb.all_flags = flags;
ab3e1f78 1756 cb.wt = NULL;
2e5c4758
PS
1757 refs_for_each_reflog(get_main_ref_store(the_repository),
1758 handle_one_reflog, &cb);
acd9544a
NTND
1759
1760 if (!revs->single_worktree)
1761 add_other_reflogs_to_pending(&cb);
ae563542
LT
1762}
1763
4fe10219 1764static void add_cache_tree(struct cache_tree *it, struct rev_info *revs,
b4cfcde4 1765 struct strbuf *path, unsigned int flags)
4fe10219
JK
1766{
1767 size_t baselen = path->len;
1768 int i;
1769
1770 if (it->entry_count >= 0) {
b3c7eef9 1771 struct tree *tree = lookup_tree(revs->repo, &it->oid);
b4cfcde4 1772 tree->object.flags |= flags;
4fe10219
JK
1773 add_pending_object_with_path(revs, &tree->object, "",
1774 040000, path->buf);
1775 }
1776
1777 for (i = 0; i < it->subtree_nr; i++) {
1778 struct cache_tree_sub *sub = it->down[i];
1779 strbuf_addf(path, "%s%s", baselen ? "/" : "", sub->name);
b4cfcde4 1780 add_cache_tree(sub->cache_tree, revs, path, flags);
4fe10219
JK
1781 strbuf_setlen(path, baselen);
1782 }
1783
1784}
1785
5a5ea141
JH
1786static void add_resolve_undo_to_pending(struct index_state *istate, struct rev_info *revs)
1787{
1788 struct string_list_item *item;
1789 struct string_list *resolve_undo = istate->resolve_undo;
1790
1791 if (!resolve_undo)
1792 return;
1793
1794 for_each_string_list_item(item, resolve_undo) {
1795 const char *path = item->string;
1796 struct resolve_undo_info *ru = item->util;
1797 int i;
1798
1799 if (!ru)
1800 continue;
1801 for (i = 0; i < 3; i++) {
1802 struct blob *blob;
1803
1804 if (!ru->mode[i] || !S_ISREG(ru->mode[i]))
1805 continue;
1806
1807 blob = lookup_blob(revs->repo, &ru->oid[i]);
1808 if (!blob) {
1809 warning(_("resolve-undo records `%s` which is missing"),
1810 oid_to_hex(&ru->oid[i]));
1811 continue;
1812 }
1813 add_pending_object_with_path(revs, &blob->object, "",
1814 ru->mode[i], path);
1815 }
1816 }
1817}
1818
6c3d8181 1819static void do_add_index_objects_to_pending(struct rev_info *revs,
b4cfcde4
JK
1820 struct index_state *istate,
1821 unsigned int flags)
4fe10219
JK
1822{
1823 int i;
1824
f5fed74f
DS
1825 /* TODO: audit for interaction with sparse-index. */
1826 ensure_full_index(istate);
6c3d8181
NTND
1827 for (i = 0; i < istate->cache_nr; i++) {
1828 struct cache_entry *ce = istate->cache[i];
4fe10219
JK
1829 struct blob *blob;
1830
1831 if (S_ISGITLINK(ce->ce_mode))
1832 continue;
1833
b3c7eef9 1834 blob = lookup_blob(revs->repo, &ce->oid);
4fe10219
JK
1835 if (!blob)
1836 die("unable to add index blob to traversal");
b4cfcde4 1837 blob->object.flags |= flags;
4fe10219
JK
1838 add_pending_object_with_path(revs, &blob->object, "",
1839 ce->ce_mode, ce->name);
1840 }
1841
6c3d8181 1842 if (istate->cache_tree) {
4fe10219 1843 struct strbuf path = STRBUF_INIT;
b4cfcde4 1844 add_cache_tree(istate->cache_tree, revs, &path, flags);
4fe10219
JK
1845 strbuf_release(&path);
1846 }
5a5ea141
JH
1847
1848 add_resolve_undo_to_pending(istate, revs);
4fe10219
JK
1849}
1850
6c3d8181
NTND
1851void add_index_objects_to_pending(struct rev_info *revs, unsigned int flags)
1852{
be489d02
NTND
1853 struct worktree **worktrees, **p;
1854
e1ff0a32 1855 repo_read_index(revs->repo);
b4cfcde4 1856 do_add_index_objects_to_pending(revs, revs->repo->index, flags);
be489d02
NTND
1857
1858 if (revs->single_worktree)
1859 return;
1860
03f2465b 1861 worktrees = get_worktrees();
be489d02
NTND
1862 for (p = worktrees; *p; p++) {
1863 struct worktree *wt = *p;
6269f8ea 1864 struct index_state istate = INDEX_STATE_INIT(revs->repo);
8e4710f0 1865 char *wt_gitdir;
be489d02
NTND
1866
1867 if (wt->is_current)
1868 continue; /* current index already taken care of */
1869
8e4710f0
PS
1870 wt_gitdir = get_worktree_git_dir(wt);
1871
be489d02 1872 if (read_index_from(&istate,
a973f60d 1873 worktree_git_path(the_repository, wt, "index"),
8e4710f0 1874 wt_gitdir) > 0)
b4cfcde4 1875 do_add_index_objects_to_pending(revs, &istate, flags);
8e4710f0 1876
be489d02 1877 discard_index(&istate);
8e4710f0 1878 free(wt_gitdir);
be489d02
NTND
1879 }
1880 free_worktrees(worktrees);
6c3d8181
NTND
1881}
1882
39b44ba7
JK
1883struct add_alternate_refs_data {
1884 struct rev_info *revs;
1885 unsigned int flags;
1886};
1887
1888static void add_one_alternate_ref(const struct object_id *oid,
1889 void *vdata)
1890{
1891 const char *name = ".alternate";
1892 struct add_alternate_refs_data *data = vdata;
1893 struct object *obj;
1894
1895 obj = get_reference(data->revs, name, oid, data->flags);
1896 add_rev_cmdline(data->revs, obj, name, REV_CMD_REV, data->flags);
1897 add_pending_object(data->revs, obj, name);
1898}
1899
1900static void add_alternate_refs_to_pending(struct rev_info *revs,
1901 unsigned int flags)
1902{
1903 struct add_alternate_refs_data data;
1904 data.revs = revs;
1905 data.flags = flags;
798c661c
PS
1906 odb_for_each_alternate_ref(the_repository->objects,
1907 add_one_alternate_ref, &data);
39b44ba7
JK
1908}
1909
8779351d
VN
1910static int add_parents_only(struct rev_info *revs, const char *arg_, int flags,
1911 int exclude_parent)
ea4a19e1 1912{
654b9a90 1913 struct object_id oid;
ea4a19e1
JH
1914 struct object *it;
1915 struct commit *commit;
1916 struct commit_list *parents;
8779351d 1917 int parent_number;
281eee47 1918 const char *arg = arg_;
ea4a19e1
JH
1919
1920 if (*arg == '^') {
7f34a46f 1921 flags ^= UNINTERESTING | BOTTOM;
ea4a19e1
JH
1922 arg++;
1923 }
d850b7a5 1924 if (repo_get_oid_committish(the_repository, arg, &oid))
ea4a19e1
JH
1925 return 0;
1926 while (1) {
654b9a90 1927 it = get_reference(revs, arg, &oid, 0);
cc243c3c
JH
1928 if (!it && revs->ignore_missing)
1929 return 0;
1974632c 1930 if (it->type != OBJ_TAG)
ea4a19e1 1931 break;
9684afd9
MK
1932 if (!((struct tag*)it)->tagged)
1933 return 0;
654b9a90 1934 oidcpy(&oid, &((struct tag*)it)->tagged->oid);
ea4a19e1 1935 }
1974632c 1936 if (it->type != OBJ_COMMIT)
ea4a19e1
JH
1937 return 0;
1938 commit = (struct commit *)it;
8779351d
VN
1939 if (exclude_parent &&
1940 exclude_parent > commit_list_count(commit->parents))
1941 return 0;
1942 for (parents = commit->parents, parent_number = 1;
1943 parents;
1944 parents = parents->next, parent_number++) {
1945 if (exclude_parent && parent_number != exclude_parent)
1946 continue;
1947
ea4a19e1
JH
1948 it = &parents->item->object;
1949 it->flags |= flags;
281eee47 1950 add_rev_cmdline(revs, it, arg_, REV_CMD_PARENTS_ONLY, flags);
ea4a19e1
JH
1951 add_pending_object(revs, it, arg);
1952 }
1953 return 1;
1954}
1955
2abf3503
NTND
1956void repo_init_revisions(struct repository *r,
1957 struct rev_info *revs,
1958 const char *prefix)
8efdc326 1959{
916ebb32
ÆAB
1960 struct rev_info blank = REV_INFO_INIT;
1961 memcpy(revs, &blank, sizeof(*revs));
8e8f9987 1962
2abf3503 1963 revs->repo = r;
67022e02 1964 revs->pruning.repo = r;
cd2bdc53
LT
1965 revs->pruning.add_remove = file_add_remove;
1966 revs->pruning.change = file_change;
a937b37e 1967 revs->pruning.change_fn_data = revs;
db6296a5 1968 revs->prefix = prefix;
cd2bdc53 1969
9725c8dd 1970 grep_init(&revs->grep_filter, revs->repo);
0843acfd 1971 revs->grep_filter.status_only = 1;
0843acfd 1972
2abf3503 1973 repo_diff_setup(revs->repo, &revs->diffopt);
c0cb4a06 1974 if (prefix && !revs->diffopt.prefix) {
cd676a51
JH
1975 revs->diffopt.prefix = prefix;
1976 revs->diffopt.prefix_length = strlen(prefix);
1977 }
3a03cf6b 1978
e6e230ee 1979 init_display_notes(&revs->notes_opt);
2a01bded 1980 list_objects_filter_init(&revs->filter);
1e9f273a 1981 init_ref_exclusions(&revs->ref_excludes);
7b644c8c 1982 oidset_init(&revs->missing_commits, 0);
8efdc326
FK
1983}
1984
0d2c9d67 1985static void add_pending_commit_list(struct rev_info *revs,
ec36c42a
NTND
1986 struct commit_list *commit_list,
1987 unsigned int flags)
0d2c9d67
RS
1988{
1989 while (commit_list) {
1990 struct object *object = &commit_list->item->object;
1991 object->flags |= flags;
f2fd0760 1992 add_pending_object(revs, object, oid_to_hex(&object->oid));
0d2c9d67
RS
1993 commit_list = commit_list->next;
1994 }
1995}
1996
f3fc5d9c
ML
1997static const char *lookup_other_head(struct object_id *oid)
1998{
1999 int i;
2000 static const char *const other_head[] = {
2001 "MERGE_HEAD", "CHERRY_PICK_HEAD", "REVERT_HEAD", "REBASE_HEAD"
2002 };
2003
2004 for (i = 0; i < ARRAY_SIZE(other_head); i++)
2e5c4758
PS
2005 if (!refs_read_ref_full(get_main_ref_store(the_repository), other_head[i],
2006 RESOLVE_REF_READING | RESOLVE_REF_NO_RECURSE,
2007 oid, NULL)) {
f3fc5d9c
ML
2008 if (is_null_oid(oid))
2009 die(_("%s exists but is a symbolic ref"), other_head[i]);
2010 return other_head[i];
2011 }
2012
2013 die(_("--merge requires one of the pseudorefs MERGE_HEAD, CHERRY_PICK_HEAD, REVERT_HEAD or REBASE_HEAD"));
2014}
2015
ae3e5e1e
JH
2016static void prepare_show_merge(struct rev_info *revs)
2017{
76e2a099 2018 struct commit_list *bases = NULL;
ae3e5e1e 2019 struct commit *head, *other;
68ab61dd 2020 struct object_id oid;
f3fc5d9c 2021 const char *other_name;
ae3e5e1e
JH
2022 const char **prune = NULL;
2023 int i, prune_num = 1; /* counting terminating NULL */
2abf3503 2024 struct index_state *istate = revs->repo->index;
ae3e5e1e 2025
d850b7a5 2026 if (repo_get_oid(the_repository, "HEAD", &oid))
ae3e5e1e 2027 die("--merge without HEAD?");
bc83266a 2028 head = lookup_commit_or_die(&oid, "HEAD");
f3fc5d9c
ML
2029 other_name = lookup_other_head(&oid);
2030 other = lookup_commit_or_die(&oid, other_name);
ae3e5e1e 2031 add_pending_object(revs, &head->object, "HEAD");
f3fc5d9c 2032 add_pending_object(revs, &other->object, other_name);
76e2a099
JS
2033 if (repo_get_merge_bases(the_repository, head, other, &bases) < 0)
2034 exit(128);
7f34a46f
KB
2035 add_rev_cmdline_list(revs, bases, REV_CMD_MERGE_BASE, UNINTERESTING | BOTTOM);
2036 add_pending_commit_list(revs, bases, UNINTERESTING | BOTTOM);
e82447b1
JH
2037 free_commit_list(bases);
2038 head->object.flags |= SYMMETRIC_LEFT;
ae3e5e1e 2039
2abf3503 2040 if (!istate->cache_nr)
e1ff0a32 2041 repo_read_index(revs->repo);
2abf3503
NTND
2042 for (i = 0; i < istate->cache_nr; i++) {
2043 const struct cache_entry *ce = istate->cache[i];
ae3e5e1e
JH
2044 if (!ce_stage(ce))
2045 continue;
2abf3503 2046 if (ce_path_match(istate, ce, &revs->prune_data, NULL)) {
ae3e5e1e 2047 prune_num++;
2756ca43 2048 REALLOC_ARRAY(prune, prune_num);
ae3e5e1e
JH
2049 prune[prune_num-2] = ce->name;
2050 prune[prune_num-1] = NULL;
2051 }
2abf3503
NTND
2052 while ((i+1 < istate->cache_nr) &&
2053 ce_same_name(ce, istate->cache[i+1]))
ae3e5e1e
JH
2054 i++;
2055 }
ed6e8038 2056 clear_pathspec(&revs->prune_data);
4a2d5ae2 2057 parse_pathspec(&revs->prune_data, PATHSPEC_ALL_MAGIC & ~PATHSPEC_LITERAL,
c6f1b920 2058 PATHSPEC_PREFER_FULL | PATHSPEC_LITERAL_PATH, "", prune);
e82447b1 2059 revs->limited = 1;
a3d278bb 2060 free(prune);
ae3e5e1e
JH
2061}
2062
62faad5a
JK
2063static int dotdot_missing(const char *arg, char *dotdot,
2064 struct rev_info *revs, int symmetric)
2065{
2066 if (revs->ignore_missing)
2067 return 0;
2068 /* de-munge so we report the full argument */
2069 *dotdot = '.';
2070 die(symmetric
2071 ? "Invalid symmetric difference expression %s"
2072 : "Invalid revision range %s", arg);
2073}
2074
2075static int handle_dotdot_1(const char *arg, char *dotdot,
2076 struct rev_info *revs, int flags,
18f1ad76
JK
2077 int cant_be_filename,
2078 struct object_context *a_oc,
2079 struct object_context *b_oc)
62faad5a
JK
2080{
2081 const char *a_name, *b_name;
2082 struct object_id a_oid, b_oid;
2083 struct object *a_obj, *b_obj;
2084 unsigned int a_flags, b_flags;
2085 int symmetric = 0;
2086 unsigned int flags_exclude = flags ^ (UNINTERESTING | BOTTOM);
321c89bf 2087 unsigned int oc_flags = GET_OID_COMMITTISH | GET_OID_RECORD_PATH;
62faad5a
JK
2088
2089 a_name = arg;
2090 if (!*a_name)
2091 a_name = "HEAD";
2092
2093 b_name = dotdot + 2;
2094 if (*b_name == '.') {
2095 symmetric = 1;
2096 b_name++;
2097 }
2098 if (!*b_name)
2099 b_name = "HEAD";
2100
3a7a698e
NTND
2101 if (get_oid_with_context(revs->repo, a_name, oc_flags, &a_oid, a_oc) ||
2102 get_oid_with_context(revs->repo, b_name, oc_flags, &b_oid, b_oc))
62faad5a
JK
2103 return -1;
2104
2105 if (!cant_be_filename) {
2106 *dotdot = '.';
2107 verify_non_filename(revs->prefix, arg);
2108 *dotdot = '\0';
2109 }
2110
b3c7eef9
NTND
2111 a_obj = parse_object(revs->repo, &a_oid);
2112 b_obj = parse_object(revs->repo, &b_oid);
62faad5a
JK
2113 if (!a_obj || !b_obj)
2114 return dotdot_missing(arg, dotdot, revs, symmetric);
2115
2116 if (!symmetric) {
2117 /* just A..B */
2118 b_flags = flags;
2119 a_flags = flags_exclude;
2120 } else {
2121 /* A...B -- find merge bases between the two */
2122 struct commit *a, *b;
76e2a099 2123 struct commit_list *exclude = NULL;
62faad5a 2124
b3c7eef9
NTND
2125 a = lookup_commit_reference(revs->repo, &a_obj->oid);
2126 b = lookup_commit_reference(revs->repo, &b_obj->oid);
62faad5a
JK
2127 if (!a || !b)
2128 return dotdot_missing(arg, dotdot, revs, symmetric);
2129
76e2a099
JS
2130 if (repo_get_merge_bases(the_repository, a, b, &exclude) < 0) {
2131 free_commit_list(exclude);
2132 return -1;
2133 }
62faad5a
JK
2134 add_rev_cmdline_list(revs, exclude, REV_CMD_MERGE_BASE,
2135 flags_exclude);
2136 add_pending_commit_list(revs, exclude, flags_exclude);
2137 free_commit_list(exclude);
2138
2139 b_flags = flags;
2140 a_flags = flags | SYMMETRIC_LEFT;
2141 }
2142
2143 a_obj->flags |= a_flags;
2144 b_obj->flags |= b_flags;
2145 add_rev_cmdline(revs, a_obj, a_name, REV_CMD_LEFT, a_flags);
2146 add_rev_cmdline(revs, b_obj, b_name, REV_CMD_RIGHT, b_flags);
18f1ad76
JK
2147 add_pending_object_with_path(revs, a_obj, a_name, a_oc->mode, a_oc->path);
2148 add_pending_object_with_path(revs, b_obj, b_name, b_oc->mode, b_oc->path);
62faad5a
JK
2149 return 0;
2150}
2151
2152static int handle_dotdot(const char *arg,
2153 struct rev_info *revs, int flags,
2154 int cant_be_filename)
2155{
f87c55c2 2156 struct object_context a_oc = {0}, b_oc = {0};
62faad5a
JK
2157 char *dotdot = strstr(arg, "..");
2158 int ret;
2159
2160 if (!dotdot)
2161 return -1;
2162
2163 *dotdot = '\0';
18f1ad76
JK
2164 ret = handle_dotdot_1(arg, dotdot, revs, flags, cant_be_filename,
2165 &a_oc, &b_oc);
62faad5a
JK
2166 *dotdot = '.';
2167
f87c55c2
PS
2168 object_context_release(&a_oc);
2169 object_context_release(&b_oc);
62faad5a
JK
2170 return ret;
2171}
2172
04a0e985 2173static int handle_revision_arg_1(const char *arg_, struct rev_info *revs, int flags, unsigned revarg_opt)
5d6f0935 2174{
f87c55c2 2175 struct object_context oc = {0};
f632dedd 2176 char *mark;
5d6f0935 2177 struct object *object;
654b9a90 2178 struct object_id oid;
5d6f0935 2179 int local_flags;
281eee47 2180 const char *arg = arg_;
8e676e8b 2181 int cant_be_filename = revarg_opt & REVARG_CANNOT_BE_FILENAME;
321c89bf 2182 unsigned get_sha1_flags = GET_OID_RECORD_PATH;
f87c55c2 2183 int ret;
5d6f0935 2184
7f34a46f
KB
2185 flags = flags & UNINTERESTING ? flags | BOTTOM : flags & ~BOTTOM;
2186
d89797fe
JK
2187 if (!cant_be_filename && !strcmp(arg, "..")) {
2188 /*
2189 * Just ".."? That is not a range but the
2190 * pathspec for the parent directory.
2191 */
f87c55c2
PS
2192 ret = -1;
2193 goto out;
5d6f0935 2194 }
8779351d 2195
f87c55c2
PS
2196 if (!handle_dotdot(arg, revs, flags, revarg_opt)) {
2197 ret = 0;
2198 goto out;
2199 }
8779351d 2200
f632dedd
JK
2201 mark = strstr(arg, "^@");
2202 if (mark && !mark[2]) {
2203 *mark = 0;
f87c55c2
PS
2204 if (add_parents_only(revs, arg, flags, 0)) {
2205 ret = 0;
2206 goto out;
2207 }
f632dedd 2208 *mark = '^';
5d6f0935 2209 }
f632dedd
JK
2210 mark = strstr(arg, "^!");
2211 if (mark && !mark[2]) {
2212 *mark = 0;
8779351d 2213 if (!add_parents_only(revs, arg, flags ^ (UNINTERESTING | BOTTOM), 0))
f632dedd 2214 *mark = '^';
8779351d 2215 }
f632dedd
JK
2216 mark = strstr(arg, "^-");
2217 if (mark) {
8779351d
VN
2218 int exclude_parent = 1;
2219
f632dedd 2220 if (mark[2]) {
793c2118 2221 if (strtol_i(mark + 2, 10, &exclude_parent) ||
f87c55c2
PS
2222 exclude_parent < 1) {
2223 ret = -1;
2224 goto out;
2225 }
8779351d
VN
2226 }
2227
f632dedd 2228 *mark = 0;
8779351d 2229 if (!add_parents_only(revs, arg, flags ^ (UNINTERESTING | BOTTOM), exclude_parent))
f632dedd 2230 *mark = '^';
62476c8e
JH
2231 }
2232
5d6f0935
JH
2233 local_flags = 0;
2234 if (*arg == '^') {
7f34a46f 2235 local_flags = UNINTERESTING | BOTTOM;
5d6f0935
JH
2236 arg++;
2237 }
d5f6b1d7
JH
2238
2239 if (revarg_opt & REVARG_COMMITTISH)
321c89bf 2240 get_sha1_flags |= GET_OID_COMMITTISH;
d5f6b1d7 2241
7b644c8c
CC
2242 /*
2243 * Even if revs->do_not_die_on_missing_objects is set, we
2244 * should error out if we can't even get an oid, as
2245 * `--missing=print` should be able to report missing oids.
2246 */
f87c55c2
PS
2247 if (get_oid_with_context(revs->repo, arg, get_sha1_flags, &oid, &oc)) {
2248 ret = revs->ignore_missing ? 0 : -1;
2249 goto out;
2250 }
5d6f0935
JH
2251 if (!cant_be_filename)
2252 verify_non_filename(revs->prefix, arg);
654b9a90 2253 object = get_reference(revs, arg, &oid, flags ^ local_flags);
f87c55c2
PS
2254 if (!object) {
2255 ret = (revs->ignore_missing || revs->do_not_die_on_missing_objects) ? 0 : -1;
2256 goto out;
2257 }
281eee47 2258 add_rev_cmdline(revs, object, arg_, REV_CMD_REV, flags ^ local_flags);
18f1ad76 2259 add_pending_object_with_path(revs, object, arg, oc.mode, oc.path);
f87c55c2
PS
2260
2261 ret = 0;
2262
2263out:
2264 object_context_release(&oc);
2265 return ret;
5d6f0935
JH
2266}
2267
04a0e985
JK
2268int handle_revision_arg(const char *arg, struct rev_info *revs, int flags, unsigned revarg_opt)
2269{
2270 int ret = handle_revision_arg_1(arg, revs, flags, revarg_opt);
2271 if (!ret)
2272 revs->rev_input_given = 1;
2273 return ret;
2274}
2275
91633995 2276static void read_pathspec_from_stdin(struct strbuf *sb,
c972bf4c 2277 struct strvec *prune)
4da5af31 2278{
7fa3c2ad 2279 while (strbuf_getline(sb, stdin) != EOF)
c972bf4c 2280 strvec_push(prune, sb->buf);
60da8b15
JH
2281}
2282
2d10c555 2283static void add_grep(struct rev_info *revs, const char *ptn, enum grep_pat_token what)
bd95fcd3 2284{
0843acfd 2285 append_grep_pattern(&revs->grep_filter, ptn, "command line", 0, what);
2d10c555
JH
2286}
2287
a4d7d2c6 2288static void add_header_grep(struct rev_info *revs, enum grep_header_field field, const char *pattern)
2d10c555 2289{
a4d7d2c6 2290 append_header_grep_pattern(&revs->grep_filter, field, pattern);
bd95fcd3
JH
2291}
2292
2293static void add_message_grep(struct rev_info *revs, const char *pattern)
2294{
2d10c555 2295 add_grep(revs, pattern, GREP_PATTERN_BODY);
bd95fcd3
JH
2296}
2297
71a1e948
JH
2298static int parse_count(const char *arg)
2299{
2300 int count;
2301
2302 if (strtol_i(arg, 10, &count) < 0)
2303 die("'%s': not an integer", arg);
2304 return count;
2305}
2306
2307static timestamp_t parse_age(const char *arg)
2308{
2309 timestamp_t num;
2310 char *p;
2311
2312 errno = 0;
2313 num = parse_timestamp(arg, &p, 10);
2314 if (errno || *p || p == arg)
2315 die("'%s': not a number of seconds since epoch", arg);
2316 return num;
2317}
2318
cd439487
JK
2319static void overwrite_argv(int *argc, const char **argv,
2320 const char **value,
2321 const struct setup_revision_opt *opt)
2322{
2323 /*
2324 * Detect the case when we are overwriting ourselves. The assignment
2325 * itself would be a noop either way, but this lets us avoid corner
2326 * cases around the free() and NULL operations.
2327 */
2328 if (*value != argv[*argc]) {
2329 if (opt && opt->free_removed_argv_elements)
2330 free((char *)argv[*argc]);
2331 argv[*argc] = *value;
2332 *value = NULL;
2333 }
2334 (*argc)++;
2335}
2336
6b61ec05 2337static int handle_revision_opt(struct rev_info *revs, int argc, const char **argv,
bbcde41a
MD
2338 int *unkc, const char **unkv,
2339 const struct setup_revision_opt* opt)
02e54220
PH
2340{
2341 const char *arg = argv[0];
257418c5 2342 const char *optarg = NULL;
7d7b86f7 2343 int argcount;
fd521245 2344 const unsigned hexsz = the_hash_algo->hexsz;
02e54220
PH
2345
2346 /* pseudo revision arguments */
2347 if (!strcmp(arg, "--all") || !strcmp(arg, "--branches") ||
2348 !strcmp(arg, "--tags") || !strcmp(arg, "--remotes") ||
2349 !strcmp(arg, "--reflog") || !strcmp(arg, "--not") ||
ad3f9a71 2350 !strcmp(arg, "--no-walk") || !strcmp(arg, "--do-walk") ||
59556548 2351 !strcmp(arg, "--bisect") || starts_with(arg, "--glob=") ||
4fe10219 2352 !strcmp(arg, "--indexed-objects") ||
39b44ba7 2353 !strcmp(arg, "--alternate-refs") ||
8c1bc2a7 2354 starts_with(arg, "--exclude=") || starts_with(arg, "--exclude-hidden=") ||
59556548
CC
2355 starts_with(arg, "--branches=") || starts_with(arg, "--tags=") ||
2356 starts_with(arg, "--remotes=") || starts_with(arg, "--no-walk="))
02e54220 2357 {
cd439487 2358 overwrite_argv(unkc, unkv, &argv[0], opt);
0fe8c138 2359 return 1;
02e54220
PH
2360 }
2361
7d7b86f7 2362 if ((argcount = parse_long_opt("max-count", argv, &optarg))) {
71a1e948 2363 revs->max_count = parse_count(optarg);
5853caec 2364 revs->no_walk = 0;
7d7b86f7
MM
2365 return argcount;
2366 } else if ((argcount = parse_long_opt("skip", argv, &optarg))) {
71a1e948 2367 revs->skip_count = parse_count(optarg);
7d7b86f7 2368 return argcount;
02e54220 2369 } else if ((*arg == '-') && isdigit(arg[1])) {
e3fa568c 2370 /* accept -<digit>, like traditional "head" */
71a1e948 2371 revs->max_count = parse_count(arg + 1);
5853caec 2372 revs->no_walk = 0;
02e54220
PH
2373 } else if (!strcmp(arg, "-n")) {
2374 if (argc <= 1)
2375 return error("-n requires an argument");
71a1e948 2376 revs->max_count = parse_count(argv[1]);
5853caec 2377 revs->no_walk = 0;
02e54220 2378 return 2;
479b3d97 2379 } else if (skip_prefix(arg, "-n", &optarg)) {
71a1e948 2380 revs->max_count = parse_count(optarg);
5853caec 2381 revs->no_walk = 0;
7d7b86f7 2382 } else if ((argcount = parse_long_opt("max-age", argv, &optarg))) {
71a1e948 2383 revs->max_age = parse_age(optarg);
7d7b86f7
MM
2384 return argcount;
2385 } else if ((argcount = parse_long_opt("since", argv, &optarg))) {
2386 revs->max_age = approxidate(optarg);
2387 return argcount;
96697781
MV
2388 } else if ((argcount = parse_long_opt("since-as-filter", argv, &optarg))) {
2389 revs->max_age_as_filter = approxidate(optarg);
2390 return argcount;
7d7b86f7
MM
2391 } else if ((argcount = parse_long_opt("after", argv, &optarg))) {
2392 revs->max_age = approxidate(optarg);
2393 return argcount;
2394 } else if ((argcount = parse_long_opt("min-age", argv, &optarg))) {
71a1e948 2395 revs->min_age = parse_age(optarg);
7d7b86f7
MM
2396 return argcount;
2397 } else if ((argcount = parse_long_opt("before", argv, &optarg))) {
2398 revs->min_age = approxidate(optarg);
2399 return argcount;
2400 } else if ((argcount = parse_long_opt("until", argv, &optarg))) {
2401 revs->min_age = approxidate(optarg);
2402 return argcount;
02e54220
PH
2403 } else if (!strcmp(arg, "--first-parent")) {
2404 revs->first_parent_only = 1;
9d505b7b
JZ
2405 } else if (!strcmp(arg, "--exclude-first-parent-only")) {
2406 revs->exclude_first_parent_only = 1;
ebdc94f3
JH
2407 } else if (!strcmp(arg, "--ancestry-path")) {
2408 revs->ancestry_path = 1;
cb7529e1 2409 revs->simplify_history = 0;
ebdc94f3 2410 revs->limited = 1;
257418c5
EN
2411 revs->ancestry_path_implicit_bottoms = 1;
2412 } else if (skip_prefix(arg, "--ancestry-path=", &optarg)) {
2413 struct commit *c;
2414 struct object_id oid;
781fb7b4 2415 const char *msg = _("could not get commit for --ancestry-path argument %s");
257418c5
EN
2416
2417 revs->ancestry_path = 1;
2418 revs->simplify_history = 0;
2419 revs->limited = 1;
2420
2421 if (repo_get_oid_committish(revs->repo, optarg, &oid))
2422 return error(msg, optarg);
2423 get_reference(revs, optarg, &oid, ANCESTRY_PATH);
2424 c = lookup_commit_reference(revs->repo, &oid);
2425 if (!c)
2426 return error(msg, optarg);
2427 commit_list_insert(c, &revs->ancestry_path_bottoms);
02e54220
PH
2428 } else if (!strcmp(arg, "-g") || !strcmp(arg, "--walk-reflogs")) {
2429 init_reflog_walk(&revs->reflog_info);
2430 } else if (!strcmp(arg, "--default")) {
2431 if (argc <= 1)
2432 return error("bad --default argument");
2433 revs->def = argv[1];
2434 return 2;
2435 } else if (!strcmp(arg, "--merge")) {
2436 revs->show_merge = 1;
2437 } else if (!strcmp(arg, "--topo-order")) {
08f704f2 2438 revs->sort_order = REV_SORT_IN_GRAPH_ORDER;
02e54220 2439 revs->topo_order = 1;
6546b593
JH
2440 } else if (!strcmp(arg, "--simplify-merges")) {
2441 revs->simplify_merges = 1;
a52f0071 2442 revs->topo_order = 1;
6546b593
JH
2443 revs->rewrite_parents = 1;
2444 revs->simplify_history = 0;
2445 revs->limited = 1;
78892e32
LT
2446 } else if (!strcmp(arg, "--simplify-by-decoration")) {
2447 revs->simplify_merges = 1;
a52f0071 2448 revs->topo_order = 1;
78892e32
LT
2449 revs->rewrite_parents = 1;
2450 revs->simplify_history = 0;
2451 revs->simplify_by_decoration = 1;
2452 revs->limited = 1;
2453 revs->prune = 1;
02e54220 2454 } else if (!strcmp(arg, "--date-order")) {
08f704f2 2455 revs->sort_order = REV_SORT_BY_COMMIT_DATE;
02e54220 2456 revs->topo_order = 1;
81c6b38b
JH
2457 } else if (!strcmp(arg, "--author-date-order")) {
2458 revs->sort_order = REV_SORT_BY_AUTHOR_DATE;
02e54220 2459 revs->topo_order = 1;
02e54220
PH
2460 } else if (!strcmp(arg, "--parents")) {
2461 revs->rewrite_parents = 1;
2462 revs->print_parents = 1;
2463 } else if (!strcmp(arg, "--dense")) {
2464 revs->dense = 1;
2465 } else if (!strcmp(arg, "--sparse")) {
2466 revs->dense = 0;
ce5b6f9b
SB
2467 } else if (!strcmp(arg, "--in-commit-order")) {
2468 revs->tree_blobs_in_commit_order = 1;
02e54220
PH
2469 } else if (!strcmp(arg, "--remove-empty")) {
2470 revs->remove_empty_trees = 1;
b8e8db28 2471 } else if (!strcmp(arg, "--merges")) {
ad5aeede 2472 revs->min_parents = 2;
02e54220 2473 } else if (!strcmp(arg, "--no-merges")) {
ad5aeede 2474 revs->max_parents = 1;
479b3d97 2475 } else if (skip_prefix(arg, "--min-parents=", &optarg)) {
71a1e948 2476 revs->min_parents = parse_count(optarg);
9ada7aee 2477 } else if (!strcmp(arg, "--no-min-parents")) {
ad5aeede 2478 revs->min_parents = 0;
479b3d97 2479 } else if (skip_prefix(arg, "--max-parents=", &optarg)) {
71a1e948 2480 revs->max_parents = parse_count(optarg);
9ada7aee 2481 } else if (!strcmp(arg, "--no-max-parents")) {
ad5aeede 2482 revs->max_parents = -1;
02e54220
PH
2483 } else if (!strcmp(arg, "--boundary")) {
2484 revs->boundary = 1;
2485 } else if (!strcmp(arg, "--left-right")) {
2486 revs->left_right = 1;
60adf7d7 2487 } else if (!strcmp(arg, "--left-only")) {
24852d91 2488 if (revs->right_only)
7854bf49
RS
2489 die(_("options '%s' and '%s' cannot be used together"),
2490 "--left-only", "--right-only/--cherry");
60adf7d7 2491 revs->left_only = 1;
e7ef4be7 2492 revs->limited = 1;
60adf7d7 2493 } else if (!strcmp(arg, "--right-only")) {
24852d91 2494 if (revs->left_only)
12909b6b 2495 die(_("options '%s' and '%s' cannot be used together"), "--right-only", "--left-only");
60adf7d7 2496 revs->right_only = 1;
e7ef4be7 2497 revs->limited = 1;
94f605ec
MG
2498 } else if (!strcmp(arg, "--cherry")) {
2499 if (revs->left_only)
12909b6b 2500 die(_("options '%s' and '%s' cannot be used together"), "--cherry", "--left-only");
94f605ec
MG
2501 revs->cherry_mark = 1;
2502 revs->right_only = 1;
ad5aeede 2503 revs->max_parents = 1;
94f605ec 2504 revs->limited = 1;
f69c5018
TR
2505 } else if (!strcmp(arg, "--count")) {
2506 revs->count = 1;
adbbb31e
MG
2507 } else if (!strcmp(arg, "--cherry-mark")) {
2508 if (revs->cherry_pick)
12909b6b 2509 die(_("options '%s' and '%s' cannot be used together"), "--cherry-mark", "--cherry-pick");
adbbb31e
MG
2510 revs->cherry_mark = 1;
2511 revs->limited = 1; /* needs limit_list() */
02e54220 2512 } else if (!strcmp(arg, "--cherry-pick")) {
adbbb31e 2513 if (revs->cherry_mark)
12909b6b 2514 die(_("options '%s' and '%s' cannot be used together"), "--cherry-pick", "--cherry-mark");
02e54220
PH
2515 revs->cherry_pick = 1;
2516 revs->limited = 1;
2517 } else if (!strcmp(arg, "--objects")) {
2518 revs->tag_objects = 1;
2519 revs->tree_objects = 1;
2520 revs->blob_objects = 1;
2521 } else if (!strcmp(arg, "--objects-edge")) {
2522 revs->tag_objects = 1;
2523 revs->tree_objects = 1;
2524 revs->blob_objects = 1;
2525 revs->edge_hint = 1;
1684c1b2 2526 } else if (!strcmp(arg, "--objects-edge-aggressive")) {
2527 revs->tag_objects = 1;
2528 revs->tree_objects = 1;
2529 revs->blob_objects = 1;
2530 revs->edge_hint = 1;
2531 revs->edge_hint_aggressive = 1;
5a48d240
JH
2532 } else if (!strcmp(arg, "--verify-objects")) {
2533 revs->tag_objects = 1;
2534 revs->tree_objects = 1;
2535 revs->blob_objects = 1;
2536 revs->verify_objects = 1;
b27ccae3 2537 disable_commit_graph(revs->repo);
02e54220
PH
2538 } else if (!strcmp(arg, "--unpacked")) {
2539 revs->unpacked = 1;
59556548 2540 } else if (starts_with(arg, "--unpacked=")) {
f649aaaf 2541 die(_("--unpacked=<packfile> no longer supported"));
c9fff000
TB
2542 } else if (!strcmp(arg, "--no-kept-objects")) {
2543 revs->no_kept_objects = 1;
2544 revs->keep_pack_cache_flags |= IN_CORE_KEEP_PACKS;
2545 revs->keep_pack_cache_flags |= ON_DISK_KEEP_PACKS;
2546 } else if (skip_prefix(arg, "--no-kept-objects=", &optarg)) {
2547 revs->no_kept_objects = 1;
2548 if (!strcmp(optarg, "in-core"))
2549 revs->keep_pack_cache_flags |= IN_CORE_KEEP_PACKS;
2550 if (!strcmp(optarg, "on-disk"))
2551 revs->keep_pack_cache_flags |= ON_DISK_KEEP_PACKS;
02e54220
PH
2552 } else if (!strcmp(arg, "-r")) {
2553 revs->diff = 1;
0d1e0e78 2554 revs->diffopt.flags.recursive = 1;
02e54220
PH
2555 } else if (!strcmp(arg, "-t")) {
2556 revs->diff = 1;
0d1e0e78
BW
2557 revs->diffopt.flags.recursive = 1;
2558 revs->diffopt.flags.tree_in_recursive = 1;
18f09473 2559 } else if ((argcount = diff_merges_parse_opts(revs, argv))) {
6501580f 2560 return argcount;
02e54220
PH
2561 } else if (!strcmp(arg, "-v")) {
2562 revs->verbose_header = 1;
2563 } else if (!strcmp(arg, "--pretty")) {
2564 revs->verbose_header = 1;
66b2ed09 2565 revs->pretty_given = 1;
ae18165f 2566 get_commit_format(NULL, revs);
479b3d97
SG
2567 } else if (skip_prefix(arg, "--pretty=", &optarg) ||
2568 skip_prefix(arg, "--format=", &optarg)) {
7d7b86f7
MM
2569 /*
2570 * Detached form ("--pretty X" as opposed to "--pretty=X")
2571 * not allowed, since the argument is optional.
2572 */
02e54220 2573 revs->verbose_header = 1;
66b2ed09 2574 revs->pretty_given = 1;
479b3d97 2575 get_commit_format(optarg, revs);
7cc13c71 2576 } else if (!strcmp(arg, "--expand-tabs")) {
fe37a9c5 2577 revs->expand_tabs_in_log = 8;
0893eec8
JH
2578 } else if (!strcmp(arg, "--no-expand-tabs")) {
2579 revs->expand_tabs_in_log = 0;
fe37a9c5
JH
2580 } else if (skip_prefix(arg, "--expand-tabs=", &arg)) {
2581 int val;
2582 if (strtol_i(arg, 10, &val) < 0 || val < 0)
2583 die("'%s': not a non-negative integer", arg);
2584 revs->expand_tabs_in_log = val;
7249e912 2585 } else if (!strcmp(arg, "--show-notes") || !strcmp(arg, "--notes")) {
1d729751 2586 enable_default_display_notes(&revs->notes_opt, &revs->show_notes);
66b2ed09 2587 revs->show_notes_given = 1;
0c37f1fc
JH
2588 } else if (!strcmp(arg, "--show-signature")) {
2589 revs->show_signature = 1;
aa379999
MJ
2590 } else if (!strcmp(arg, "--no-show-signature")) {
2591 revs->show_signature = 0;
479b3d97
SG
2592 } else if (!strcmp(arg, "--show-linear-break")) {
2593 revs->break_bar = " ..........";
1b32dece
NTND
2594 revs->track_linear = 1;
2595 revs->track_first_time = 1;
479b3d97
SG
2596 } else if (skip_prefix(arg, "--show-linear-break=", &optarg)) {
2597 revs->break_bar = xstrdup(optarg);
1b32dece
NTND
2598 revs->track_linear = 1;
2599 revs->track_first_time = 1;
2e0d30d9
KH
2600 } else if (!strcmp(arg, "--show-notes-by-default")) {
2601 revs->show_notes_by_default = 1;
479b3d97
SG
2602 } else if (skip_prefix(arg, "--show-notes=", &optarg) ||
2603 skip_prefix(arg, "--notes=", &optarg)) {
479b3d97
SG
2604 if (starts_with(arg, "--show-notes=") &&
2605 revs->notes_opt.use_default_notes < 0)
2606 revs->notes_opt.use_default_notes = 1;
1d729751 2607 enable_ref_display_notes(&revs->notes_opt, &revs->show_notes, optarg);
452538c3 2608 revs->show_notes_given = 1;
66b2ed09 2609 } else if (!strcmp(arg, "--no-notes")) {
1d729751 2610 disable_display_notes(&revs->notes_opt, &revs->show_notes);
66b2ed09 2611 revs->show_notes_given = 1;
894a9d33
TR
2612 } else if (!strcmp(arg, "--standard-notes")) {
2613 revs->show_notes_given = 1;
3a03cf6b 2614 revs->notes_opt.use_default_notes = 1;
894a9d33 2615 } else if (!strcmp(arg, "--no-standard-notes")) {
3a03cf6b 2616 revs->notes_opt.use_default_notes = 0;
de84accc
NS
2617 } else if (!strcmp(arg, "--oneline")) {
2618 revs->verbose_header = 1;
2619 get_commit_format("oneline", revs);
7dccadf3 2620 revs->pretty_given = 1;
de84accc 2621 revs->abbrev_commit = 1;
02e54220 2622 } else if (!strcmp(arg, "--graph")) {
dccf6c16 2623 graph_clear(revs->graph);
02e54220 2624 revs->graph = graph_init(revs);
087c7458
AH
2625 } else if (!strcmp(arg, "--no-graph")) {
2626 graph_clear(revs->graph);
2627 revs->graph = NULL;
19d097e3
EB
2628 } else if (!strcmp(arg, "--encode-email-headers")) {
2629 revs->encode_email_headers = 1;
2630 } else if (!strcmp(arg, "--no-encode-email-headers")) {
2631 revs->encode_email_headers = 0;
02e54220
PH
2632 } else if (!strcmp(arg, "--root")) {
2633 revs->show_root_diff = 1;
2634 } else if (!strcmp(arg, "--no-commit-id")) {
2635 revs->no_commit_id = 1;
2636 } else if (!strcmp(arg, "--always")) {
2637 revs->always_show_header = 1;
2638 } else if (!strcmp(arg, "--no-abbrev")) {
2639 revs->abbrev = 0;
2640 } else if (!strcmp(arg, "--abbrev")) {
2641 revs->abbrev = DEFAULT_ABBREV;
479b3d97
SG
2642 } else if (skip_prefix(arg, "--abbrev=", &optarg)) {
2643 revs->abbrev = strtoul(optarg, NULL, 10);
02e54220
PH
2644 if (revs->abbrev < MINIMUM_ABBREV)
2645 revs->abbrev = MINIMUM_ABBREV;
fd521245 2646 else if (revs->abbrev > hexsz)
2647 revs->abbrev = hexsz;
02e54220
PH
2648 } else if (!strcmp(arg, "--abbrev-commit")) {
2649 revs->abbrev_commit = 1;
0c47695a
JS
2650 revs->abbrev_commit_given = 1;
2651 } else if (!strcmp(arg, "--no-abbrev-commit")) {
2652 revs->abbrev_commit = 0;
02e54220
PH
2653 } else if (!strcmp(arg, "--full-diff")) {
2654 revs->diff = 1;
2655 revs->full_diff = 1;
8d049e18
DS
2656 } else if (!strcmp(arg, "--show-pulls")) {
2657 revs->show_pulls = 1;
02e54220
PH
2658 } else if (!strcmp(arg, "--full-history")) {
2659 revs->simplify_history = 0;
2660 } else if (!strcmp(arg, "--relative-date")) {
a5481a6c 2661 revs->date_mode.type = DATE_RELATIVE;
f4ea32f0 2662 revs->date_mode_explicit = 1;
7d7b86f7 2663 } else if ((argcount = parse_long_opt("date", argv, &optarg))) {
a5481a6c 2664 parse_date_format(optarg, &revs->date_mode);
f4ea32f0 2665 revs->date_mode_explicit = 1;
7d7b86f7 2666 return argcount;
02e54220
PH
2667 } else if (!strcmp(arg, "--log-size")) {
2668 revs->show_log_size = 1;
2669 }
2670 /*
2671 * Grepping the commit log
2672 */
7d7b86f7
MM
2673 else if ((argcount = parse_long_opt("author", argv, &optarg))) {
2674 add_header_grep(revs, GREP_HEADER_AUTHOR, optarg);
2675 return argcount;
2676 } else if ((argcount = parse_long_opt("committer", argv, &optarg))) {
2677 add_header_grep(revs, GREP_HEADER_COMMITTER, optarg);
2678 return argcount;
72fd13f7
NTND
2679 } else if ((argcount = parse_long_opt("grep-reflog", argv, &optarg))) {
2680 add_header_grep(revs, GREP_HEADER_REFLOG, optarg);
2681 return argcount;
7d7b86f7
MM
2682 } else if ((argcount = parse_long_opt("grep", argv, &optarg))) {
2683 add_message_grep(revs, optarg);
2684 return argcount;
727b6fc3 2685 } else if (!strcmp(arg, "--basic-regexp")) {
8465541e 2686 revs->grep_filter.pattern_type_option = GREP_PATTERN_TYPE_BRE;
02e54220 2687 } else if (!strcmp(arg, "--extended-regexp") || !strcmp(arg, "-E")) {
8465541e 2688 revs->grep_filter.pattern_type_option = GREP_PATTERN_TYPE_ERE;
02e54220 2689 } else if (!strcmp(arg, "--regexp-ignore-case") || !strcmp(arg, "-i")) {
9e3cbc59 2690 revs->grep_filter.ignore_case = 1;
c1ddc461 2691 revs->diffopt.pickaxe_opts |= DIFF_PICKAXE_IGNORE_CASE;
02e54220 2692 } else if (!strcmp(arg, "--fixed-strings") || !strcmp(arg, "-F")) {
8465541e 2693 revs->grep_filter.pattern_type_option = GREP_PATTERN_TYPE_FIXED;
7531a2dd 2694 } else if (!strcmp(arg, "--perl-regexp") || !strcmp(arg, "-P")) {
8465541e 2695 revs->grep_filter.pattern_type_option = GREP_PATTERN_TYPE_PCRE;
02e54220 2696 } else if (!strcmp(arg, "--all-match")) {
0843acfd 2697 revs->grep_filter.all_match = 1;
22dfa8a2 2698 } else if (!strcmp(arg, "--invert-grep")) {
794c0002 2699 revs->grep_filter.no_body_match = 1;
7d7b86f7 2700 } else if ((argcount = parse_long_opt("encoding", argv, &optarg))) {
844d1906 2701 free(git_log_output_encoding);
7d7b86f7
MM
2702 if (strcmp(optarg, "none"))
2703 git_log_output_encoding = xstrdup(optarg);
02e54220 2704 else
844d1906 2705 git_log_output_encoding = xstrdup("");
7d7b86f7 2706 return argcount;
02e54220
PH
2707 } else if (!strcmp(arg, "--reverse")) {
2708 revs->reverse ^= 1;
2709 } else if (!strcmp(arg, "--children")) {
2710 revs->children.name = "children";
2711 revs->limited = 1;
cc243c3c
JH
2712 } else if (!strcmp(arg, "--ignore-missing")) {
2713 revs->ignore_missing = 1;
bbcde41a 2714 } else if (opt && opt->allow_exclude_promisor_objects &&
669b1d2a 2715 !strcmp(arg, "--exclude-promisor-objects")) {
df11e196 2716 if (fetch_if_missing)
033abf97 2717 BUG("exclude_promisor_objects can only be used when fetch_if_missing is 0");
df11e196 2718 revs->exclude_promisor_objects = 1;
02e54220 2719 } else {
a97262c6 2720 int opts = diff_opt_parse(&revs->diffopt, argv, argc, revs->prefix);
02e54220 2721 if (!opts)
cd439487 2722 overwrite_argv(unkc, unkv, &argv[0], opt);
02e54220
PH
2723 return opts;
2724 }
2725
2726 return 1;
2727}
2728
6b61ec05
PH
2729void parse_revision_opt(struct rev_info *revs, struct parse_opt_ctx_t *ctx,
2730 const struct option *options,
2731 const char * const usagestr[])
2732{
2733 int n = handle_revision_opt(revs, ctx->argc, ctx->argv,
bbcde41a 2734 &ctx->cpidx, ctx->out, NULL);
6b61ec05
PH
2735 if (n <= 0) {
2736 error("unknown option `%s'", ctx->argv[0]);
2737 usage_with_options(usagestr, options);
2738 }
2739 ctx->argv += n;
2740 ctx->argc -= n;
2741}
2742
087c7458
AH
2743void revision_opts_finish(struct rev_info *revs)
2744{
2745 if (revs->graph && revs->track_linear)
2746 die(_("options '%s' and '%s' cannot be used together"), "--show-linear-break", "--graph");
2747
2748 if (revs->graph) {
2749 revs->topo_order = 1;
2750 revs->rewrite_parents = 1;
2751 }
2752}
2753
073cf63c
NTND
2754static int for_each_bisect_ref(struct ref_store *refs, each_ref_fn fn,
2755 void *cb_data, const char *term)
2756{
cb46d630
AD
2757 struct strbuf bisect_refs = STRBUF_INIT;
2758 int status;
2759 strbuf_addf(&bisect_refs, "refs/bisect/%s", term);
b269ac53 2760 status = refs_for_each_fullref_in(refs, bisect_refs.buf, NULL, fn, cb_data);
cb46d630
AD
2761 strbuf_release(&bisect_refs);
2762 return status;
2763}
2764
073cf63c 2765static int for_each_bad_bisect_ref(struct ref_store *refs, each_ref_fn fn, void *cb_data)
ad3f9a71 2766{
073cf63c 2767 return for_each_bisect_ref(refs, fn, cb_data, term_bad);
ad3f9a71
LT
2768}
2769
073cf63c 2770static int for_each_good_bisect_ref(struct ref_store *refs, each_ref_fn fn, void *cb_data)
ad3f9a71 2771{
073cf63c 2772 return for_each_bisect_ref(refs, fn, cb_data, term_good);
ad3f9a71
LT
2773}
2774
10a0d6ae 2775static int handle_revision_pseudo_opt(struct rev_info *revs,
e885a84f 2776 const char **argv, int *flags)
f6aca0dc
JN
2777{
2778 const char *arg = argv[0];
2779 const char *optarg;
073cf63c 2780 struct ref_store *refs;
f6aca0dc
JN
2781 int argcount;
2782
10a0d6ae 2783 if (revs->repo != the_repository) {
d0c39a49
NTND
2784 /*
2785 * We need some something like get_submodule_worktrees()
2786 * before we can go through all worktrees of a submodule,
2787 * .e.g with adding all HEADs from --all, which is not
2788 * supported right now, so stick to single worktree.
2789 */
2790 if (!revs->single_worktree)
033abf97 2791 BUG("--single-worktree cannot be used together with submodule");
10a0d6ae
JT
2792 }
2793 refs = get_main_ref_store(revs->repo);
073cf63c 2794
0fc63ec4
JN
2795 /*
2796 * NOTE!
2797 *
2798 * Commands like "git shortlog" will not accept the options below
2799 * unless parse_revision_opt queues them (as opposed to erroring
2800 * out).
2801 *
2802 * When implementing your new pseudo-option, remember to
2803 * register it in the list at the top of handle_revision_opt.
2804 */
f6aca0dc 2805 if (!strcmp(arg, "--all")) {
073cf63c
NTND
2806 handle_refs(refs, revs, *flags, refs_for_each_ref);
2807 handle_refs(refs, revs, *flags, refs_head_ref);
d0c39a49
NTND
2808 if (!revs->single_worktree) {
2809 struct all_refs_cb cb;
2810
2811 init_all_refs_cb(&cb, revs, *flags);
2812 other_head_refs(handle_one_ref, &cb);
2813 }
1e9f273a 2814 clear_ref_exclusions(&revs->ref_excludes);
f6aca0dc 2815 } else if (!strcmp(arg, "--branches")) {
8c1bc2a7 2816 if (revs->ref_excludes.hidden_refs_configured)
81fb70f5
RS
2817 return error(_("options '%s' and '%s' cannot be used together"),
2818 "--exclude-hidden", "--branches");
073cf63c 2819 handle_refs(refs, revs, *flags, refs_for_each_branch_ref);
1e9f273a 2820 clear_ref_exclusions(&revs->ref_excludes);
f6aca0dc 2821 } else if (!strcmp(arg, "--bisect")) {
cb46d630 2822 read_bisect_terms(&term_bad, &term_good);
073cf63c
NTND
2823 handle_refs(refs, revs, *flags, for_each_bad_bisect_ref);
2824 handle_refs(refs, revs, *flags ^ (UNINTERESTING | BOTTOM),
2825 for_each_good_bisect_ref);
f6aca0dc
JN
2826 revs->bisect = 1;
2827 } else if (!strcmp(arg, "--tags")) {
8c1bc2a7 2828 if (revs->ref_excludes.hidden_refs_configured)
81fb70f5
RS
2829 return error(_("options '%s' and '%s' cannot be used together"),
2830 "--exclude-hidden", "--tags");
073cf63c 2831 handle_refs(refs, revs, *flags, refs_for_each_tag_ref);
1e9f273a 2832 clear_ref_exclusions(&revs->ref_excludes);
f6aca0dc 2833 } else if (!strcmp(arg, "--remotes")) {
8c1bc2a7 2834 if (revs->ref_excludes.hidden_refs_configured)
81fb70f5
RS
2835 return error(_("options '%s' and '%s' cannot be used together"),
2836 "--exclude-hidden", "--remotes");
073cf63c 2837 handle_refs(refs, revs, *flags, refs_for_each_remote_ref);
1e9f273a 2838 clear_ref_exclusions(&revs->ref_excludes);
f6aca0dc
JN
2839 } else if ((argcount = parse_long_opt("glob", argv, &optarg))) {
2840 struct all_refs_cb cb;
2841 init_all_refs_cb(&cb, revs, *flags);
2e5c4758
PS
2842 refs_for_each_glob_ref(get_main_ref_store(the_repository),
2843 handle_one_ref, optarg, &cb);
1e9f273a 2844 clear_ref_exclusions(&revs->ref_excludes);
e7b432c5
JH
2845 return argcount;
2846 } else if ((argcount = parse_long_opt("exclude", argv, &optarg))) {
ff32d342 2847 add_ref_exclusion(&revs->ref_excludes, optarg);
f6aca0dc 2848 return argcount;
8c1bc2a7
PS
2849 } else if ((argcount = parse_long_opt("exclude-hidden", argv, &optarg))) {
2850 exclude_hidden_refs(&revs->ref_excludes, optarg);
2851 return argcount;
8b1d9136 2852 } else if (skip_prefix(arg, "--branches=", &optarg)) {
f6aca0dc 2853 struct all_refs_cb cb;
8c1bc2a7 2854 if (revs->ref_excludes.hidden_refs_configured)
81fb70f5
RS
2855 return error(_("options '%s' and '%s' cannot be used together"),
2856 "--exclude-hidden", "--branches");
f6aca0dc 2857 init_all_refs_cb(&cb, revs, *flags);
2e5c4758
PS
2858 refs_for_each_glob_ref_in(get_main_ref_store(the_repository),
2859 handle_one_ref, optarg,
2860 "refs/heads/", &cb);
1e9f273a 2861 clear_ref_exclusions(&revs->ref_excludes);
8b1d9136 2862 } else if (skip_prefix(arg, "--tags=", &optarg)) {
f6aca0dc 2863 struct all_refs_cb cb;
8c1bc2a7 2864 if (revs->ref_excludes.hidden_refs_configured)
81fb70f5
RS
2865 return error(_("options '%s' and '%s' cannot be used together"),
2866 "--exclude-hidden", "--tags");
f6aca0dc 2867 init_all_refs_cb(&cb, revs, *flags);
2e5c4758
PS
2868 refs_for_each_glob_ref_in(get_main_ref_store(the_repository),
2869 handle_one_ref, optarg,
2870 "refs/tags/", &cb);
1e9f273a 2871 clear_ref_exclusions(&revs->ref_excludes);
8b1d9136 2872 } else if (skip_prefix(arg, "--remotes=", &optarg)) {
f6aca0dc 2873 struct all_refs_cb cb;
8c1bc2a7 2874 if (revs->ref_excludes.hidden_refs_configured)
81fb70f5
RS
2875 return error(_("options '%s' and '%s' cannot be used together"),
2876 "--exclude-hidden", "--remotes");
f6aca0dc 2877 init_all_refs_cb(&cb, revs, *flags);
2e5c4758
PS
2878 refs_for_each_glob_ref_in(get_main_ref_store(the_repository),
2879 handle_one_ref, optarg,
2880 "refs/remotes/", &cb);
1e9f273a 2881 clear_ref_exclusions(&revs->ref_excludes);
f6aca0dc 2882 } else if (!strcmp(arg, "--reflog")) {
718ccc97 2883 add_reflogs_to_pending(revs, *flags);
4fe10219
JK
2884 } else if (!strcmp(arg, "--indexed-objects")) {
2885 add_index_objects_to_pending(revs, *flags);
39b44ba7
JK
2886 } else if (!strcmp(arg, "--alternate-refs")) {
2887 add_alternate_refs_to_pending(revs, *flags);
f6aca0dc 2888 } else if (!strcmp(arg, "--not")) {
7f34a46f 2889 *flags ^= UNINTERESTING | BOTTOM;
f6aca0dc 2890 } else if (!strcmp(arg, "--no-walk")) {
29ef1f27 2891 revs->no_walk = 1;
8b1d9136 2892 } else if (skip_prefix(arg, "--no-walk=", &optarg)) {
ca92e59e
MZ
2893 /*
2894 * Detached form ("--no-walk X" as opposed to "--no-walk=X")
2895 * not allowed, since the argument is optional.
2896 */
29ef1f27 2897 revs->no_walk = 1;
8b1d9136 2898 if (!strcmp(optarg, "sorted"))
29ef1f27 2899 revs->unsorted_input = 0;
8b1d9136 2900 else if (!strcmp(optarg, "unsorted"))
29ef1f27 2901 revs->unsorted_input = 1;
ca92e59e
MZ
2902 else
2903 return error("invalid argument to --no-walk");
f6aca0dc
JN
2904 } else if (!strcmp(arg, "--do-walk")) {
2905 revs->no_walk = 0;
32619f99
NTND
2906 } else if (!strcmp(arg, "--single-worktree")) {
2907 revs->single_worktree = 1;
cc910442 2908 } else if (skip_prefix(arg, ("--filter="), &arg)) {
c4ea513f 2909 parse_list_objects_filter(&revs->filter, arg);
cc910442 2910 } else if (!strcmp(arg, ("--no-filter"))) {
c4ea513f 2911 list_objects_filter_set_no_filter(&revs->filter);
f6aca0dc
JN
2912 } else {
2913 return 0;
2914 }
2915
2916 return 1;
2917}
2918
cc804501 2919static void read_revisions_from_stdin(struct rev_info *revs,
f97c8b1e 2920 struct strvec *prune)
cc804501
PS
2921{
2922 struct strbuf sb;
2923 int seen_dashdash = 0;
c40f0b78 2924 int seen_end_of_options = 0;
cc804501 2925 int save_warning;
f97c8b1e 2926 int flags = 0;
cc804501
PS
2927
2928 save_warning = warn_on_object_refname_ambiguity;
2929 warn_on_object_refname_ambiguity = 0;
2930
2931 strbuf_init(&sb, 1000);
2932 while (strbuf_getline(&sb, stdin) != EOF) {
af37a209
PS
2933 if (!sb.len)
2934 break;
2935
2936 if (!strcmp(sb.buf, "--")) {
2937 seen_dashdash = 1;
cc804501 2938 break;
cc804501 2939 }
af37a209 2940
c40f0b78
PS
2941 if (!seen_end_of_options && sb.buf[0] == '-') {
2942 const char *argv[] = { sb.buf, NULL };
2943
2944 if (!strcmp(sb.buf, "--end-of-options")) {
2945 seen_end_of_options = 1;
2946 continue;
2947 }
2948
f97c8b1e 2949 if (handle_revision_pseudo_opt(revs, argv, &flags) > 0)
c40f0b78
PS
2950 continue;
2951
2952 die(_("invalid option '%s' in --stdin mode"), sb.buf);
2953 }
af37a209 2954
f97c8b1e 2955 if (handle_revision_arg(sb.buf, revs, flags,
cc804501
PS
2956 REVARG_CANNOT_BE_FILENAME))
2957 die("bad revision '%s'", sb.buf);
2958 }
2959 if (seen_dashdash)
2960 read_pathspec_from_stdin(&sb, prune);
2961
2962 strbuf_release(&sb);
2963 warn_on_object_refname_ambiguity = save_warning;
2964}
2965
ce113604
JK
2966static void NORETURN diagnose_missing_default(const char *def)
2967{
ce113604
JK
2968 int flags;
2969 const char *refname;
2970
2e5c4758
PS
2971 refname = refs_resolve_ref_unsafe(get_main_ref_store(the_repository),
2972 def, 0, NULL, &flags);
ce113604
JK
2973 if (!refname || !(flags & REF_ISSYMREF) || (flags & REF_ISBROKEN))
2974 die(_("your current branch appears to be broken"));
2975
2976 skip_prefix(refname, "refs/heads/", &refname);
2977 die(_("your current branch '%s' does not have any commits yet"),
2978 refname);
2979}
2980
ae563542
LT
2981/*
2982 * Parse revision information, filling in the "rev_info" structure,
2983 * and removing the used arguments from the argument list.
2984 *
765ac8ec
LT
2985 * Returns the number of arguments left that weren't recognized
2986 * (which are also moved to the head of the argument list)
ae563542 2987 */
32962c9b 2988int setup_revisions(int argc, const char **argv, struct rev_info *revs, struct setup_revision_opt *opt)
ae563542 2989{
04a0e985 2990 int i, flags, left, seen_dashdash, revarg_opt;
c972bf4c 2991 struct strvec prune_data = STRVEC_INIT;
19e8789b 2992 int seen_end_of_options = 0;
9ef6aeb0 2993
ae563542 2994 /* First, search for "--" */
6d5b93f2 2995 if (opt && opt->assume_dashdash) {
ae563542 2996 seen_dashdash = 1;
6d5b93f2
CB
2997 } else {
2998 seen_dashdash = 0;
2999 for (i = 1; i < argc; i++) {
3000 const char *arg = argv[i];
3001 if (strcmp(arg, "--"))
3002 continue;
f92dbdbc
ÆAB
3003 if (opt && opt->free_removed_argv_elements)
3004 free((char *)argv[i]);
6d5b93f2
CB
3005 argv[i] = NULL;
3006 argc = i;
3007 if (argv[i + 1])
c972bf4c 3008 strvec_pushv(&prune_data, argv + i + 1);
6d5b93f2
CB
3009 seen_dashdash = 1;
3010 break;
3011 }
ae563542
LT
3012 }
3013
02e54220
PH
3014 /* Second, deal with arguments and options */
3015 flags = 0;
d5f6b1d7
JH
3016 revarg_opt = opt ? opt->revarg_opt : 0;
3017 if (seen_dashdash)
3018 revarg_opt |= REVARG_CANNOT_BE_FILENAME;
02e54220 3019 for (left = i = 1; i < argc; i++) {
ae563542 3020 const char *arg = argv[i];
19e8789b 3021 if (!seen_end_of_options && *arg == '-') {
cd2bdc53 3022 int opts;
02e54220 3023
10a0d6ae 3024 opts = handle_revision_pseudo_opt(
e885a84f 3025 revs, argv + i,
f6aca0dc
JN
3026 &flags);
3027 if (opts > 0) {
3028 i += opts - 1;
8e64006e
JS
3029 continue;
3030 }
f6aca0dc 3031
8b3dce56
JH
3032 if (!strcmp(arg, "--stdin")) {
3033 if (revs->disable_stdin) {
cd439487 3034 overwrite_argv(&left, argv, &argv[i], opt);
8b3dce56
JH
3035 continue;
3036 }
a12cbe23 3037 if (revs->read_from_stdin++)
8b3dce56 3038 die("--stdin given twice?");
f97c8b1e 3039 read_revisions_from_stdin(revs, &prune_data);
8b3dce56
JH
3040 continue;
3041 }
2d10c555 3042
19e8789b
JK
3043 if (!strcmp(arg, "--end-of-options")) {
3044 seen_end_of_options = 1;
3045 continue;
3046 }
3047
bbcde41a
MD
3048 opts = handle_revision_opt(revs, argc - i, argv + i,
3049 &left, argv, opt);
cd2bdc53 3050 if (opts > 0) {
cd2bdc53
LT
3051 i += opts - 1;
3052 continue;
3053 }
02e54220
PH
3054 if (opts < 0)
3055 exit(128);
ae563542
LT
3056 continue;
3057 }
ae563542 3058
8e676e8b
JH
3059
3060 if (handle_revision_arg(arg, revs, flags, revarg_opt)) {
5d6f0935
JH
3061 int j;
3062 if (seen_dashdash || *arg == '^')
ae563542
LT
3063 die("bad revision '%s'", arg);
3064
ea92f41f
JH
3065 /* If we didn't have a "--":
3066 * (1) all filenames must exist;
3067 * (2) all rev-args must not be interpretable
3068 * as a valid filename.
3069 * but the latter we have checked in the main loop.
3070 */
e23d0b4a 3071 for (j = i; j < argc; j++)
023e37c3 3072 verify_filename(revs->prefix, argv[j], j == i);
e23d0b4a 3073
c972bf4c 3074 strvec_pushv(&prune_data, argv + i);
ae563542
LT
3075 break;
3076 }
ae563542 3077 }
087c7458 3078 revision_opts_finish(revs);
5d6f0935 3079
d70a9eb6 3080 if (prune_data.nr) {
93e7d672
JH
3081 /*
3082 * If we need to introduce the magic "a lone ':' means no
3083 * pathspec whatsoever", here is the place to do so.
3084 *
3085 * if (prune_data.nr == 1 && !strcmp(prune_data[0], ":")) {
3086 * prune_data.nr = 0;
3087 * prune_data.alloc = 0;
3088 * free(prune_data.path);
3089 * prune_data.path = NULL;
3090 * } else {
3091 * terminate prune_data.alloc with NULL and
3092 * call init_pathspec() to set revs->prune_data here.
3093 * }
3094 */
0fdc2ae5 3095 parse_pathspec(&revs->prune_data, 0, 0,
d70a9eb6 3096 revs->prefix, prune_data.v);
4da5af31 3097 }
c972bf4c 3098 strvec_clear(&prune_data);
5486ef0e 3099
afe8a907 3100 if (!revs->def)
32962c9b 3101 revs->def = opt ? opt->def : NULL;
b4490059 3102 if (opt && opt->tweak)
cc88afad 3103 opt->tweak(revs);
02e54220 3104 if (revs->show_merge)
ae3e5e1e 3105 prepare_show_merge(revs);
04a0e985 3106 if (revs->def && !revs->pending.nr && !revs->rev_input_given) {
654b9a90 3107 struct object_id oid;
cd2bdc53 3108 struct object *object;
249c8f4a 3109 struct object_context oc;
3a7a698e 3110 if (get_oid_with_context(revs->repo, revs->def, 0, &oid, &oc))
ce113604 3111 diagnose_missing_default(revs->def);
654b9a90 3112 object = get_reference(revs, revs->def, &oid, 0);
249c8f4a 3113 add_pending_object_with_mode(revs, object, revs->def, oc.mode);
f87c55c2 3114 object_context_release(&oc);
ae563542 3115 }
8efdc326 3116
b7bb760d
LT
3117 /* Did the user ask for any diff output? Run the diff! */
3118 if (revs->diffopt.output_format & ~DIFF_FORMAT_NO_OUTPUT)
3119 revs->diff = 1;
3120
0faf2da7 3121 /* Pickaxe, diff-filter and rename following need diffs */
cf63051a 3122 if ((revs->diffopt.pickaxe_opts & DIFF_PICKAXE_KINDS_MASK) ||
375ac087 3123 revs->diffopt.filter || revs->diffopt.filter_not ||
0d1e0e78 3124 revs->diffopt.flags.follow_renames)
b7bb760d
LT
3125 revs->diff = 1;
3126
15af58c1
SB
3127 if (revs->diffopt.objfind)
3128 revs->simplify_history = 0;
3129
002933f3
SG
3130 if (revs->line_level_traverse) {
3131 if (want_ancestry(revs))
3132 revs->limited = 1;
3133 revs->topo_order = 1;
3134 }
3135
f0d9cc41 3136 if (revs->topo_order && !generation_numbers_enabled(the_repository))
53069686
JH
3137 revs->limited = 1;
3138
afe069d1 3139 if (revs->prune_data.nr) {
bd1928df 3140 copy_pathspec(&revs->pruning.pathspec, &revs->prune_data);
750f7b66 3141 /* Can't prune commits with rename following: the paths change.. */
0d1e0e78 3142 if (!revs->diffopt.flags.follow_renames)
53b2c823 3143 revs->prune = 1;
cd2bdc53 3144 if (!revs->full_diff)
bd1928df
NTND
3145 copy_pathspec(&revs->diffopt.pathspec,
3146 &revs->prune_data);
8efdc326 3147 }
299a6634 3148
18f09473 3149 diff_merges_setup_revs(revs);
d76ce4f7 3150
cd2bdc53 3151 revs->diffopt.abbrev = revs->abbrev;
12da1d1f 3152
28452655 3153 diff_setup_done(&revs->diffopt);
8efdc326 3154
44570188
ÆAB
3155 if (!is_encoding_utf8(get_log_output_encoding()))
3156 revs->grep_filter.ignore_locale = 1;
0843acfd 3157 compile_grep_patterns(&revs->grep_filter);
bd95fcd3 3158
82fd0f4a
JK
3159 if (revs->reflog_info && revs->limited)
3160 die("cannot combine --walk-reflogs with history-limiting options");
8bb65883 3161 if (revs->rewrite_parents && revs->children.name)
12909b6b 3162 die(_("options '%s' and '%s' cannot be used together"), "--parents", "--children");
c4ea513f
DS
3163 if (revs->filter.choice && !revs->blob_objects)
3164 die(_("object filtering requires --objects"));
d56651c0 3165
7fefda5c
AS
3166 /*
3167 * Limitations on the graph functionality
3168 */
fa518aef
RS
3169 die_for_incompatible_opt3(!!revs->graph, "--graph",
3170 !!revs->reverse, "--reverse",
3171 !!revs->reflog_info, "--walk-reflogs");
7fefda5c 3172
695985f4 3173 if (revs->no_walk && revs->graph)
12909b6b 3174 die(_("options '%s' and '%s' cannot be used together"), "--no-walk", "--graph");
baa6378f 3175 if (!revs->reflog_info && revs->grep_filter.use_reflog_filter)
6fa00ee8 3176 die(_("the option '%s' requires '%s'"), "--grep-reflog", "--walk-reflogs");
7fefda5c 3177
05314efa
JK
3178 if (revs->line_level_traverse &&
3179 (revs->diffopt.output_format & ~(DIFF_FORMAT_PATCH | DIFF_FORMAT_NO_OUTPUT)))
3180 die(_("-L does not yet support diff formats besides -p and -s"));
3181
0893eec8
JH
3182 if (revs->expand_tabs_in_log < 0)
3183 revs->expand_tabs_in_log = revs->expand_tabs_in_log_default;
3184
2e0d30d9
KH
3185 if (!revs->show_notes_given && revs->show_notes_by_default) {
3186 enable_default_display_notes(&revs->notes_opt, &revs->show_notes);
3187 revs->show_notes_given = 1;
3188 }
3189
a04bc717
JK
3190 if (argv) {
3191 if (opt && opt->free_removed_argv_elements)
3192 free((char *)argv[left]);
3193 argv[left] = NULL;
3194 }
3195
ae563542
LT
3196 return left;
3197}
a4a88b2b 3198
f93c1d86
JK
3199void setup_revisions_from_strvec(struct strvec *argv, struct rev_info *revs,
3200 struct setup_revision_opt *opt)
3201{
3202 struct setup_revision_opt fallback_opt;
3203 int ret;
3204
3205 if (!opt) {
3206 memset(&fallback_opt, 0, sizeof(fallback_opt));
3207 opt = &fallback_opt;
3208 }
3209 opt->free_removed_argv_elements = 1;
3210
3211 ret = setup_revisions(argv->nr, argv->v, revs, opt);
3212
3213 for (size_t i = ret; i < argv->nr; i++)
3214 free((char *)argv->v[i]);
3215 argv->nr = ret;
3216}
3217
7a98d9ab
ÆAB
3218static void release_revisions_cmdline(struct rev_cmdline_info *cmdline)
3219{
3220 unsigned int i;
3221
3222 for (i = 0; i < cmdline->nr; i++)
3223 free((char *)cmdline->rev[i].name);
3224 free(cmdline->rev);
3225}
3226
a52f07af
ÆAB
3227static void release_revisions_mailmap(struct string_list *mailmap)
3228{
3229 if (!mailmap)
3230 return;
3231 clear_mailmap(mailmap);
3232 free(mailmap);
3233}
3234
ae1b383d
ÆAB
3235static void release_revisions_topo_walk_info(struct topo_walk_info *info);
3236
90d5518a
LY
3237static void release_revisions_bloom_keyvecs(struct rev_info *revs)
3238{
3239 for (size_t nr = 0; nr < revs->bloom_keyvecs_nr; nr++)
3240 bloom_keyvec_free(revs->bloom_keyvecs[nr]);
3241 FREE_AND_NULL(revs->bloom_keyvecs);
3242 revs->bloom_keyvecs_nr = 0;
3243}
3244
8ef8da48
JK
3245static void free_void_commit_list(void *list)
3246{
3247 free_commit_list(list);
3248}
3249
1878b5ed
ÆAB
3250void release_revisions(struct rev_info *revs)
3251{
e966fc5a 3252 free_commit_list(revs->commits);
257418c5 3253 free_commit_list(revs->ancestry_path_bottoms);
97485374 3254 release_display_notes(&revs->notes_opt);
1878b5ed 3255 object_array_clear(&revs->pending);
ab1f6926 3256 object_array_clear(&revs->boundary_commits);
7a98d9ab 3257 release_revisions_cmdline(&revs->cmdline);
e75d2f7f 3258 list_objects_filter_release(&revs->filter);
689a8e80 3259 clear_pathspec(&revs->prune_data);
9d5a7df3 3260 date_mode_release(&revs->date_mode);
a52f07af 3261 release_revisions_mailmap(revs->mailmap);
f41fb662 3262 free_grep_patterns(&revs->grep_filter);
fc47252d 3263 graph_clear(revs->graph);
a90a0896 3264 diff_free(&revs->diffopt);
6ab75ac8 3265 diff_free(&revs->pruning);
81ffbf83 3266 reflog_walk_info_release(revs->reflog_info);
ae1b383d 3267 release_revisions_topo_walk_info(revs->topo_walk_info);
8ef8da48
JK
3268 clear_decoration(&revs->children, free_void_commit_list);
3269 clear_decoration(&revs->merge_simplification, free);
3270 clear_decoration(&revs->treesame, free);
3271 line_log_free(revs);
9830926c 3272 oidset_clear(&revs->missing_commits);
90d5518a 3273 release_revisions_bloom_keyvecs(revs);
1878b5ed
ÆAB
3274}
3275
f35f5603
JH
3276static void add_child(struct rev_info *revs, struct commit *parent, struct commit *child)
3277{
3278 struct commit_list *l = xcalloc(1, sizeof(*l));
3279
3280 l->item = child;
3281 l->next = add_decoration(&revs->children, &parent->object, l);
3282}
3283
d0af663e 3284static int remove_duplicate_parents(struct rev_info *revs, struct commit *commit)
6546b593 3285{
d0af663e 3286 struct treesame_state *ts = lookup_decoration(&revs->treesame, &commit->object);
6546b593
JH
3287 struct commit_list **pp, *p;
3288 int surviving_parents;
3289
3290 /* Examine existing parents while marking ones we have seen... */
3291 pp = &commit->parents;
d0af663e 3292 surviving_parents = 0;
6546b593
JH
3293 while ((p = *pp) != NULL) {
3294 struct commit *parent = p->item;
3295 if (parent->object.flags & TMP_MARK) {
3296 *pp = p->next;
4cc2cee5 3297 free(p);
d0af663e
KB
3298 if (ts)
3299 compact_treesame(revs, commit, surviving_parents);
6546b593
JH
3300 continue;
3301 }
3302 parent->object.flags |= TMP_MARK;
d0af663e 3303 surviving_parents++;
6546b593
JH
3304 pp = &p->next;
3305 }
d0af663e 3306 /* clear the temporary mark */
6546b593
JH
3307 for (p = commit->parents; p; p = p->next) {
3308 p->item->object.flags &= ~TMP_MARK;
6546b593 3309 }
d0af663e 3310 /* no update_treesame() - removing duplicates can't affect TREESAME */
6546b593
JH
3311 return surviving_parents;
3312}
3313
faf0156b
JH
3314struct merge_simplify_state {
3315 struct commit *simplified;
3316};
3317
3318static struct merge_simplify_state *locate_simplify_state(struct rev_info *revs, struct commit *commit)
3319{
3320 struct merge_simplify_state *st;
3321
3322 st = lookup_decoration(&revs->merge_simplification, &commit->object);
3323 if (!st) {
ca56dadb 3324 CALLOC_ARRAY(st, 1);
faf0156b
JH
3325 add_decoration(&revs->merge_simplification, &commit->object, st);
3326 }
3327 return st;
3328}
3329
91633995 3330static int mark_redundant_parents(struct commit *commit)
d0af663e
KB
3331{
3332 struct commit_list *h = reduce_heads(commit->parents);
3333 int i = 0, marked = 0;
3334 struct commit_list *po, *pn;
3335
3336 /* Want these for sanity-checking only */
3337 int orig_cnt = commit_list_count(commit->parents);
3338 int cnt = commit_list_count(h);
3339
3340 /*
3341 * Not ready to remove items yet, just mark them for now, based
3342 * on the output of reduce_heads(). reduce_heads outputs the reduced
3343 * set in its original order, so this isn't too hard.
3344 */
3345 po = commit->parents;
3346 pn = h;
3347 while (po) {
3348 if (pn && po->item == pn->item) {
3349 pn = pn->next;
3350 i++;
3351 } else {
3352 po->item->object.flags |= TMP_MARK;
3353 marked++;
3354 }
3355 po=po->next;
3356 }
3357
3358 if (i != cnt || cnt+marked != orig_cnt)
3359 die("mark_redundant_parents %d %d %d %d", orig_cnt, cnt, i, marked);
3360
3361 free_commit_list(h);
3362
3363 return marked;
3364}
3365
91633995 3366static int mark_treesame_root_parents(struct commit *commit)
143f1eaf
KB
3367{
3368 struct commit_list *p;
3369 int marked = 0;
3370
3371 for (p = commit->parents; p; p = p->next) {
3372 struct commit *parent = p->item;
3373 if (!parent->parents && (parent->object.flags & TREESAME)) {
3374 parent->object.flags |= TMP_MARK;
3375 marked++;
3376 }
3377 }
3378
3379 return marked;
3380}
3381
9c129eab
KB
3382/*
3383 * Awkward naming - this means one parent we are TREESAME to.
3384 * cf mark_treesame_root_parents: root parents that are TREESAME (to an
3385 * empty tree). Better name suggestions?
3386 */
3387static int leave_one_treesame_to_parent(struct rev_info *revs, struct commit *commit)
3388{
3389 struct treesame_state *ts = lookup_decoration(&revs->treesame, &commit->object);
3390 struct commit *unmarked = NULL, *marked = NULL;
3391 struct commit_list *p;
3392 unsigned n;
3393
3394 for (p = commit->parents, n = 0; p; p = p->next, n++) {
3395 if (ts->treesame[n]) {
3396 if (p->item->object.flags & TMP_MARK) {
3397 if (!marked)
3398 marked = p->item;
3399 } else {
3400 if (!unmarked) {
3401 unmarked = p->item;
3402 break;
3403 }
3404 }
3405 }
3406 }
3407
3408 /*
3409 * If we are TREESAME to a marked-for-deletion parent, but not to any
3410 * unmarked parents, unmark the first TREESAME parent. This is the
3411 * parent that the default simplify_history==1 scan would have followed,
3412 * and it doesn't make sense to omit that path when asking for a
3413 * simplified full history. Retaining it improves the chances of
3414 * understanding odd missed merges that took an old version of a file.
3415 *
3416 * Example:
3417 *
3418 * I--------*X A modified the file, but mainline merge X used
3419 * \ / "-s ours", so took the version from I. X is
3420 * `-*A--' TREESAME to I and !TREESAME to A.
3421 *
3422 * Default log from X would produce "I". Without this check,
3423 * --full-history --simplify-merges would produce "I-A-X", showing
3424 * the merge commit X and that it changed A, but not making clear that
3425 * it had just taken the I version. With this check, the topology above
3426 * is retained.
3427 *
3428 * Note that it is possible that the simplification chooses a different
3429 * TREESAME parent from the default, in which case this test doesn't
3430 * activate, and we _do_ drop the default parent. Example:
3431 *
3432 * I------X A modified the file, but it was reverted in B,
3433 * \ / meaning mainline merge X is TREESAME to both
3434 * *A-*B parents.
3435 *
3436 * Default log would produce "I" by following the first parent;
3437 * --full-history --simplify-merges will produce "I-A-B". But this is a
3438 * reasonable result - it presents a logical full history leading from
3439 * I to X, and X is not an important merge.
3440 */
3441 if (!unmarked && marked) {
3442 marked->object.flags &= ~TMP_MARK;
3443 return 1;
3444 }
3445
3446 return 0;
3447}
3448
d0af663e
KB
3449static int remove_marked_parents(struct rev_info *revs, struct commit *commit)
3450{
3451 struct commit_list **pp, *p;
3452 int nth_parent, removed = 0;
3453
3454 pp = &commit->parents;
3455 nth_parent = 0;
3456 while ((p = *pp) != NULL) {
3457 struct commit *parent = p->item;
3458 if (parent->object.flags & TMP_MARK) {
3459 parent->object.flags &= ~TMP_MARK;
3460 *pp = p->next;
3461 free(p);
3462 removed++;
3463 compact_treesame(revs, commit, nth_parent);
3464 continue;
3465 }
3466 pp = &p->next;
3467 nth_parent++;
3468 }
3469
3470 /* Removing parents can only increase TREESAMEness */
3471 if (removed && !(commit->object.flags & TREESAME))
3472 update_treesame(revs, commit);
3473
3474 return nth_parent;
3475}
3476
faf0156b 3477static struct commit_list **simplify_one(struct rev_info *revs, struct commit *commit, struct commit_list **tail)
6546b593
JH
3478{
3479 struct commit_list *p;
4d826608 3480 struct commit *parent;
faf0156b 3481 struct merge_simplify_state *st, *pst;
6546b593
JH
3482 int cnt;
3483
faf0156b
JH
3484 st = locate_simplify_state(revs, commit);
3485
6546b593 3486 /*
6546b593
JH
3487 * Have we handled this one?
3488 */
faf0156b 3489 if (st->simplified)
6546b593
JH
3490 return tail;
3491
3492 /*
3493 * An UNINTERESTING commit simplifies to itself, so does a
3494 * root commit. We do not rewrite parents of such commit
3495 * anyway.
3496 */
3497 if ((commit->object.flags & UNINTERESTING) || !commit->parents) {
faf0156b 3498 st->simplified = commit;
6546b593
JH
3499 return tail;
3500 }
3501
3502 /*
6e513ba3
JH
3503 * Do we know what commit all of our parents that matter
3504 * should be rewritten to? Otherwise we are not ready to
3505 * rewrite this one yet.
6546b593
JH
3506 */
3507 for (cnt = 0, p = commit->parents; p; p = p->next) {
faf0156b
JH
3508 pst = locate_simplify_state(revs, p->item);
3509 if (!pst->simplified) {
6546b593
JH
3510 tail = &commit_list_insert(p->item, tail)->next;
3511 cnt++;
3512 }
6e513ba3
JH
3513 if (revs->first_parent_only)
3514 break;
6546b593 3515 }
53030f8d
JH
3516 if (cnt) {
3517 tail = &commit_list_insert(commit, tail)->next;
6546b593 3518 return tail;
53030f8d 3519 }
6546b593
JH
3520
3521 /*
d0af663e
KB
3522 * Rewrite our list of parents. Note that this cannot
3523 * affect our TREESAME flags in any way - a commit is
3524 * always TREESAME to its simplification.
6546b593 3525 */
faf0156b
JH
3526 for (p = commit->parents; p; p = p->next) {
3527 pst = locate_simplify_state(revs, p->item);
3528 p->item = pst->simplified;
6e513ba3
JH
3529 if (revs->first_parent_only)
3530 break;
faf0156b 3531 }
4b7f53da 3532
0290bf12 3533 if (revs->first_parent_only)
6e513ba3 3534 cnt = 1;
0290bf12 3535 else
d0af663e 3536 cnt = remove_duplicate_parents(revs, commit);
6546b593
JH
3537
3538 /*
3539 * It is possible that we are a merge and one side branch
3540 * does not have any commit that touches the given paths;
d0af663e
KB
3541 * in such a case, the immediate parent from that branch
3542 * will be rewritten to be the merge base.
6546b593
JH
3543 *
3544 * o----X X: the commit we are looking at;
3545 * / / o: a commit that touches the paths;
3546 * ---o----'
3547 *
143f1eaf
KB
3548 * Further, a merge of an independent branch that doesn't
3549 * touch the path will reduce to a treesame root parent:
3550 *
3551 * ----o----X X: the commit we are looking at;
3552 * / o: a commit that touches the paths;
3553 * r r: a root commit not touching the paths
3554 *
3555 * Detect and simplify both cases.
6546b593
JH
3556 */
3557 if (1 < cnt) {
91633995
JK
3558 int marked = mark_redundant_parents(commit);
3559 marked += mark_treesame_root_parents(commit);
9c129eab
KB
3560 if (marked)
3561 marked -= leave_one_treesame_to_parent(revs, commit);
d0af663e
KB
3562 if (marked)
3563 cnt = remove_marked_parents(revs, commit);
6546b593
JH
3564 }
3565
3566 /*
3567 * A commit simplifies to itself if it is a root, if it is
3568 * UNINTERESTING, if it touches the given paths, or if it is a
4d826608 3569 * merge and its parents don't simplify to one relevant commit
6546b593
JH
3570 * (the first two cases are already handled at the beginning of
3571 * this function).
3572 *
4d826608
KB
3573 * Otherwise, it simplifies to what its sole relevant parent
3574 * simplifies to.
6546b593
JH
3575 */
3576 if (!cnt ||
3577 (commit->object.flags & UNINTERESTING) ||
3578 !(commit->object.flags & TREESAME) ||
8d049e18
DS
3579 (parent = one_relevant_parent(revs, commit->parents)) == NULL ||
3580 (revs->show_pulls && (commit->object.flags & PULL_MERGE)))
faf0156b
JH
3581 st->simplified = commit;
3582 else {
4d826608 3583 pst = locate_simplify_state(revs, parent);
faf0156b
JH
3584 st->simplified = pst->simplified;
3585 }
6546b593
JH
3586 return tail;
3587}
3588
3589static void simplify_merges(struct rev_info *revs)
3590{
ab9d75a8 3591 struct commit_list *list, *next;
6546b593 3592 struct commit_list *yet_to_do, **tail;
ab9d75a8 3593 struct commit *commit;
6546b593 3594
5eac739e
JH
3595 if (!revs->prune)
3596 return;
65347030 3597
6546b593
JH
3598 /* feed the list reversed */
3599 yet_to_do = NULL;
ab9d75a8
JH
3600 for (list = revs->commits; list; list = next) {
3601 commit = list->item;
3602 next = list->next;
3603 /*
3604 * Do not free(list) here yet; the original list
3605 * is used later in this function.
3606 */
3607 commit_list_insert(commit, &yet_to_do);
3608 }
6546b593
JH
3609 while (yet_to_do) {
3610 list = yet_to_do;
3611 yet_to_do = NULL;
3612 tail = &yet_to_do;
3613 while (list) {
e510ab89 3614 commit = pop_commit(&list);
faf0156b 3615 tail = simplify_one(revs, commit, tail);
6546b593
JH
3616 }
3617 }
3618
3619 /* clean up the result, removing the simplified ones */
3620 list = revs->commits;
3621 revs->commits = NULL;
3622 tail = &revs->commits;
3623 while (list) {
faf0156b 3624 struct merge_simplify_state *st;
ab9d75a8 3625
e510ab89 3626 commit = pop_commit(&list);
faf0156b
JH
3627 st = locate_simplify_state(revs, commit);
3628 if (st->simplified == commit)
6546b593
JH
3629 tail = &commit_list_insert(commit, tail)->next;
3630 }
6546b593
JH
3631}
3632
f35f5603
JH
3633static void set_children(struct rev_info *revs)
3634{
3635 struct commit_list *l;
3636 for (l = revs->commits; l; l = l->next) {
3637 struct commit *commit = l->item;
3638 struct commit_list *p;
3639
3640 for (p = commit->parents; p; p = p->next)
3641 add_child(revs, p->item, commit);
3642 }
3643}
3644
bcc0a3ea
HV
3645void reset_revision_walk(void)
3646{
74d414c9
PS
3647 clear_object_flags(the_repository,
3648 SEEN | ADDED | SHOWN | TOPO_WALK_EXPLORED | TOPO_WALK_INDEGREE);
bcc0a3ea
HV
3649}
3650
df11e196 3651static int mark_uninteresting(const struct object_id *oid,
be252d33
JK
3652 struct packed_git *pack UNUSED,
3653 uint32_t pos UNUSED,
b3c7eef9 3654 void *cb)
df11e196 3655{
b3c7eef9 3656 struct rev_info *revs = cb;
c1fa951d 3657 struct object *o = lookup_unknown_object(revs->repo, oid);
df11e196
JT
3658 o->flags |= UNINTERESTING | SEEN;
3659 return 0;
3660}
3661
b4542418
DS
3662define_commit_slab(indegree_slab, int);
3663define_commit_slab(author_date_slab, timestamp_t);
3664
3665struct topo_walk_info {
d7f92784 3666 timestamp_t min_generation;
b4542418
DS
3667 struct prio_queue explore_queue;
3668 struct prio_queue indegree_queue;
3669 struct prio_queue topo_queue;
3670 struct indegree_slab indegree;
3671 struct author_date_slab author_date;
3672};
3673
90b666da
DS
3674static int topo_walk_atexit_registered;
3675static unsigned int count_explore_walked;
3676static unsigned int count_indegree_walked;
3677static unsigned int count_topo_walked;
3678
3679static void trace2_topo_walk_statistics_atexit(void)
3680{
3681 struct json_writer jw = JSON_WRITER_INIT;
3682
3683 jw_object_begin(&jw, 0);
3684 jw_object_intmax(&jw, "count_explore_walked", count_explore_walked);
3685 jw_object_intmax(&jw, "count_indegree_walked", count_indegree_walked);
3686 jw_object_intmax(&jw, "count_topo_walked", count_topo_walked);
3687 jw_end(&jw);
3688
3689 trace2_data_json("topo_walk", the_repository, "statistics", &jw);
3690
3691 jw_release(&jw);
3692}
3693
b4542418
DS
3694static inline void test_flag_and_insert(struct prio_queue *q, struct commit *c, int flag)
3695{
3696 if (c->object.flags & flag)
3697 return;
3698
3699 c->object.flags |= flag;
3700 prio_queue_put(q, c);
3701}
3702
3703static void explore_walk_step(struct rev_info *revs)
3704{
3705 struct topo_walk_info *info = revs->topo_walk_info;
3706 struct commit_list *p;
3707 struct commit *c = prio_queue_get(&info->explore_queue);
3708
3709 if (!c)
3710 return;
3711
ea3f7e59 3712 if (repo_parse_commit_gently(revs->repo, c, 1) < 0)
b4542418
DS
3713 return;
3714
90b666da
DS
3715 count_explore_walked++;
3716
b4542418
DS
3717 if (revs->sort_order == REV_SORT_BY_AUTHOR_DATE)
3718 record_author_date(&info->author_date, c);
3719
3720 if (revs->max_age != -1 && (c->date < revs->max_age))
3721 c->object.flags |= UNINTERESTING;
3722
3723 if (process_parents(revs, c, NULL, NULL) < 0)
3724 return;
3725
3726 if (c->object.flags & UNINTERESTING)
9d505b7b 3727 mark_parents_uninteresting(revs, c);
b4542418
DS
3728
3729 for (p = c->parents; p; p = p->next)
3730 test_flag_and_insert(&info->explore_queue, p->item, TOPO_WALK_EXPLORED);
3731}
3732
3733static void explore_to_depth(struct rev_info *revs,
d7f92784 3734 timestamp_t gen_cutoff)
b4542418
DS
3735{
3736 struct topo_walk_info *info = revs->topo_walk_info;
3737 struct commit *c;
3738 while ((c = prio_queue_peek(&info->explore_queue)) &&
c49c82aa 3739 commit_graph_generation(c) >= gen_cutoff)
b4542418
DS
3740 explore_walk_step(revs);
3741}
3742
3743static void indegree_walk_step(struct rev_info *revs)
3744{
3745 struct commit_list *p;
3746 struct topo_walk_info *info = revs->topo_walk_info;
3747 struct commit *c = prio_queue_get(&info->indegree_queue);
3748
3749 if (!c)
3750 return;
3751
ea3f7e59 3752 if (repo_parse_commit_gently(revs->repo, c, 1) < 0)
b4542418
DS
3753 return;
3754
90b666da
DS
3755 count_indegree_walked++;
3756
c49c82aa 3757 explore_to_depth(revs, commit_graph_generation(c));
b4542418
DS
3758
3759 for (p = c->parents; p; p = p->next) {
3760 struct commit *parent = p->item;
3761 int *pi = indegree_slab_at(&info->indegree, parent);
3762
2f9bbb6d
AK
3763 if (repo_parse_commit_gently(revs->repo, parent, 1) < 0)
3764 return;
3765
b4542418
DS
3766 if (*pi)
3767 (*pi)++;
3768 else
3769 *pi = 2;
3770
3771 test_flag_and_insert(&info->indegree_queue, parent, TOPO_WALK_INDEGREE);
3772
3773 if (revs->first_parent_only)
3774 return;
3775 }
3776}
3777
3778static void compute_indegrees_to_depth(struct rev_info *revs,
d7f92784 3779 timestamp_t gen_cutoff)
b4542418
DS
3780{
3781 struct topo_walk_info *info = revs->topo_walk_info;
3782 struct commit *c;
3783 while ((c = prio_queue_peek(&info->indegree_queue)) &&
c49c82aa 3784 commit_graph_generation(c) >= gen_cutoff)
b4542418
DS
3785 indegree_walk_step(revs);
3786}
f0d9cc41 3787
ae1b383d 3788static void release_revisions_topo_walk_info(struct topo_walk_info *info)
0aa0c2b2 3789{
ae1b383d
ÆAB
3790 if (!info)
3791 return;
0aa0c2b2
MH
3792 clear_prio_queue(&info->explore_queue);
3793 clear_prio_queue(&info->indegree_queue);
3794 clear_prio_queue(&info->topo_queue);
3795 clear_indegree_slab(&info->indegree);
3796 clear_author_date_slab(&info->author_date);
ae1b383d
ÆAB
3797 free(info);
3798}
0aa0c2b2 3799
ae1b383d
ÆAB
3800static void reset_topo_walk(struct rev_info *revs)
3801{
3802 release_revisions_topo_walk_info(revs->topo_walk_info);
3803 revs->topo_walk_info = NULL;
0aa0c2b2
MH
3804}
3805
f0d9cc41
DS
3806static void init_topo_walk(struct rev_info *revs)
3807{
3808 struct topo_walk_info *info;
b4542418 3809 struct commit_list *list;
0aa0c2b2
MH
3810 if (revs->topo_walk_info)
3811 reset_topo_walk(revs);
3812
f0d9cc41
DS
3813 revs->topo_walk_info = xmalloc(sizeof(struct topo_walk_info));
3814 info = revs->topo_walk_info;
3815 memset(info, 0, sizeof(struct topo_walk_info));
3816
b4542418
DS
3817 init_indegree_slab(&info->indegree);
3818 memset(&info->explore_queue, 0, sizeof(info->explore_queue));
3819 memset(&info->indegree_queue, 0, sizeof(info->indegree_queue));
3820 memset(&info->topo_queue, 0, sizeof(info->topo_queue));
3821
3822 switch (revs->sort_order) {
3823 default: /* REV_SORT_IN_GRAPH_ORDER */
3824 info->topo_queue.compare = NULL;
3825 break;
3826 case REV_SORT_BY_COMMIT_DATE:
3827 info->topo_queue.compare = compare_commits_by_commit_date;
3828 break;
3829 case REV_SORT_BY_AUTHOR_DATE:
3830 init_author_date_slab(&info->author_date);
3831 info->topo_queue.compare = compare_commits_by_author_date;
3832 info->topo_queue.cb_data = &info->author_date;
3833 break;
3834 }
3835
3836 info->explore_queue.compare = compare_commits_by_gen_then_commit_date;
3837 info->indegree_queue.compare = compare_commits_by_gen_then_commit_date;
3838
3839 info->min_generation = GENERATION_NUMBER_INFINITY;
3840 for (list = revs->commits; list; list = list->next) {
3841 struct commit *c = list->item;
d7f92784 3842 timestamp_t generation;
b4542418 3843
ea3f7e59 3844 if (repo_parse_commit_gently(revs->repo, c, 1))
b4542418
DS
3845 continue;
3846
3847 test_flag_and_insert(&info->explore_queue, c, TOPO_WALK_EXPLORED);
3848 test_flag_and_insert(&info->indegree_queue, c, TOPO_WALK_INDEGREE);
3849
c752ad09
AK
3850 generation = commit_graph_generation(c);
3851 if (generation < info->min_generation)
3852 info->min_generation = generation;
b4542418
DS
3853
3854 *(indegree_slab_at(&info->indegree, c)) = 1;
3855
3856 if (revs->sort_order == REV_SORT_BY_AUTHOR_DATE)
3857 record_author_date(&info->author_date, c);
3858 }
3859 compute_indegrees_to_depth(revs, info->min_generation);
3860
3861 for (list = revs->commits; list; list = list->next) {
3862 struct commit *c = list->item;
3863
3864 if (*(indegree_slab_at(&info->indegree, c)) == 1)
3865 prio_queue_put(&info->topo_queue, c);
3866 }
3867
3868 /*
3869 * This is unfortunate; the initial tips need to be shown
3870 * in the order given from the revision traversal machinery.
3871 */
3872 if (revs->sort_order == REV_SORT_IN_GRAPH_ORDER)
3873 prio_queue_reverse(&info->topo_queue);
90b666da
DS
3874
3875 if (trace2_is_enabled() && !topo_walk_atexit_registered) {
3876 atexit(trace2_topo_walk_statistics_atexit);
3877 topo_walk_atexit_registered = 1;
3878 }
f0d9cc41
DS
3879}
3880
3881static struct commit *next_topo_commit(struct rev_info *revs)
3882{
b4542418
DS
3883 struct commit *c;
3884 struct topo_walk_info *info = revs->topo_walk_info;
3885
3886 /* pop next off of topo_queue */
3887 c = prio_queue_get(&info->topo_queue);
3888
3889 if (c)
3890 *(indegree_slab_at(&info->indegree, c)) = 0;
3891
3892 return c;
f0d9cc41
DS
3893}
3894
3895static void expand_topo_walk(struct rev_info *revs, struct commit *commit)
3896{
b4542418
DS
3897 struct commit_list *p;
3898 struct topo_walk_info *info = revs->topo_walk_info;
3899 if (process_parents(revs, commit, NULL, NULL) < 0) {
f0d9cc41
DS
3900 if (!revs->ignore_missing_links)
3901 die("Failed to traverse parents of commit %s",
3902 oid_to_hex(&commit->object.oid));
3903 }
b4542418 3904
90b666da
DS
3905 count_topo_walked++;
3906
b4542418
DS
3907 for (p = commit->parents; p; p = p->next) {
3908 struct commit *parent = p->item;
3909 int *pi;
d7f92784 3910 timestamp_t generation;
b4542418 3911
1d8e31a3
DS
3912 if (parent->object.flags & UNINTERESTING)
3913 continue;
3914
ea3f7e59 3915 if (repo_parse_commit_gently(revs->repo, parent, 1) < 0)
b4542418
DS
3916 continue;
3917
c752ad09
AK
3918 generation = commit_graph_generation(parent);
3919 if (generation < info->min_generation) {
3920 info->min_generation = generation;
b4542418
DS
3921 compute_indegrees_to_depth(revs, info->min_generation);
3922 }
3923
3924 pi = indegree_slab_at(&info->indegree, parent);
3925
3926 (*pi)--;
3927 if (*pi == 1)
3928 prio_queue_put(&info->topo_queue, parent);
3929
3930 if (revs->first_parent_only)
3931 return;
3932 }
f0d9cc41
DS
3933}
3934
cc0e6c5a 3935int prepare_revision_walk(struct rev_info *revs)
a4a88b2b 3936{
1da1e07c
JK
3937 int i;
3938 struct object_array old_pending;
2e7da8e9 3939 struct commit_list **next = &revs->commits;
cd2bdc53 3940
1da1e07c 3941 memcpy(&old_pending, &revs->pending, sizeof(old_pending));
1f1e895f
LT
3942 revs->pending.nr = 0;
3943 revs->pending.alloc = 0;
3944 revs->pending.objects = NULL;
1da1e07c
JK
3945 for (i = 0; i < old_pending.nr; i++) {
3946 struct object_array_entry *e = old_pending.objects + i;
20739490 3947 struct commit *commit = handle_commit(revs, e);
cd2bdc53
LT
3948 if (commit) {
3949 if (!(commit->object.flags & SEEN)) {
3950 commit->object.flags |= SEEN;
2e7da8e9 3951 next = commit_list_append(commit, next);
cd2bdc53
LT
3952 }
3953 }
cd2bdc53 3954 }
f1230fb5 3955 object_array_clear(&old_pending);
cd2bdc53 3956
d0af663e 3957 /* Signal whether we need per-parent treesame decoration */
4d826608
KB
3958 if (revs->simplify_merges ||
3959 (revs->limited && limiting_can_increase_treesame(revs)))
d0af663e
KB
3960 revs->treesame.name = "treesame";
3961
df11e196 3962 if (revs->exclude_promisor_objects) {
c87910b9 3963 for_each_packed_object(revs->repo, mark_uninteresting, revs,
df11e196
JT
3964 FOR_EACH_OBJECT_PROMISOR_ONLY);
3965 }
3966
f32dde8c 3967 if (!revs->reflog_info)
a56b9464 3968 prepare_to_use_bloom_filter(revs);
29ef1f27 3969 if (!revs->unsorted_input)
ca92e59e 3970 commit_list_sort_by_date(&revs->commits);
ba1d4505 3971 if (revs->no_walk)
cc0e6c5a 3972 return 0;
f0d9cc41 3973 if (revs->limited) {
cc0e6c5a
AR
3974 if (limit_list(revs) < 0)
3975 return -1;
f0d9cc41
DS
3976 if (revs->topo_order)
3977 sort_in_topological_order(&revs->commits, revs->sort_order);
3978 } else if (revs->topo_order)
3979 init_topo_walk(revs);
3cb9d2b6
SG
3980 if (revs->line_level_traverse && want_ancestry(revs))
3981 /*
3982 * At the moment we can only do line-level log with parent
3983 * rewriting by performing this expensive pre-filtering step.
3984 * If parent rewriting is not requested, then we rather
3985 * perform the line-level log filtering during the regular
3986 * history traversal.
3987 */
12da1d1f 3988 line_log_filter(revs);
6546b593
JH
3989 if (revs->simplify_merges)
3990 simplify_merges(revs);
f35f5603
JH
3991 if (revs->children.name)
3992 set_children(revs);
a56b9464 3993
cc0e6c5a 3994 return 0;
a4a88b2b
LT
3995}
3996
8320b1db
JK
3997static enum rewrite_result rewrite_one_1(struct rev_info *revs,
3998 struct commit **pp,
3999 struct prio_queue *queue)
765ac8ec
LT
4000{
4001 for (;;) {
4002 struct commit *p = *pp;
3381c790 4003 if (!revs->limited)
8320b1db 4004 if (process_parents(revs, p, NULL, queue) < 0)
cc0e6c5a 4005 return rewrite_one_error;
7dc0fe3b
LT
4006 if (p->object.flags & UNINTERESTING)
4007 return rewrite_one_ok;
4008 if (!(p->object.flags & TREESAME))
cc0e6c5a 4009 return rewrite_one_ok;
765ac8ec 4010 if (!p->parents)
cc0e6c5a 4011 return rewrite_one_noparents;
afe8a907 4012 if (!(p = one_relevant_parent(revs, p->parents)))
4d826608
KB
4013 return rewrite_one_ok;
4014 *pp = p;
765ac8ec
LT
4015 }
4016}
4017
8320b1db
JK
4018static void merge_queue_into_list(struct prio_queue *q, struct commit_list **list)
4019{
4020 while (q->nr) {
4021 struct commit *item = prio_queue_peek(q);
4022 struct commit_list *p = *list;
4023
4024 if (p && p->item->date >= item->date)
4025 list = &p->next;
4026 else {
4027 p = commit_list_insert(item, list);
4028 list = &p->next; /* skip newly added item */
4029 prio_queue_get(q); /* pop item */
4030 }
4031 }
4032}
4033
4034static enum rewrite_result rewrite_one(struct rev_info *revs, struct commit **pp)
4035{
4036 struct prio_queue queue = { compare_commits_by_commit_date };
4037 enum rewrite_result ret = rewrite_one_1(revs, pp, &queue);
4038 merge_queue_into_list(&queue, &revs->commits);
4039 clear_prio_queue(&queue);
4040 return ret;
4041}
4042
c7edcae0
BY
4043int rewrite_parents(struct rev_info *revs, struct commit *commit,
4044 rewrite_parent_fn_t rewrite_parent)
765ac8ec
LT
4045{
4046 struct commit_list **pp = &commit->parents;
4047 while (*pp) {
4048 struct commit_list *parent = *pp;
c7edcae0 4049 switch (rewrite_parent(revs, &parent->item)) {
cc0e6c5a
AR
4050 case rewrite_one_ok:
4051 break;
4052 case rewrite_one_noparents:
765ac8ec 4053 *pp = parent->next;
4cc2cee5 4054 free(parent);
765ac8ec 4055 continue;
cc0e6c5a
AR
4056 case rewrite_one_error:
4057 return -1;
765ac8ec
LT
4058 }
4059 pp = &parent->next;
4060 }
d0af663e 4061 remove_duplicate_parents(revs, commit);
cc0e6c5a 4062 return 0;
765ac8ec
LT
4063}
4064
8ecae9b0
JH
4065static int commit_match(struct commit *commit, struct rev_info *opt)
4066{
72fd13f7 4067 int retval;
04deccda 4068 const char *encoding;
b000c59b 4069 const char *message;
72fd13f7 4070 struct strbuf buf = STRBUF_INIT;
04deccda 4071
80235ba7 4072 if (!opt->grep_filter.pattern_list && !opt->grep_filter.header_list)
8ecae9b0 4073 return 1;
baa6378f
JH
4074
4075 /* Prepend "fake" headers as needed */
4076 if (opt->grep_filter.use_reflog_filter) {
72fd13f7
NTND
4077 strbuf_addstr(&buf, "reflog ");
4078 get_reflog_message(&buf, opt->reflog_info);
4079 strbuf_addch(&buf, '\n');
72fd13f7 4080 }
baa6378f 4081
04deccda
JK
4082 /*
4083 * We grep in the user's output encoding, under the assumption that it
4084 * is the encoding they are most likely to write their grep pattern
4085 * for. In addition, it means we will match the "notes" encoding below,
4086 * so we will not end up with a buffer that has two different encodings
4087 * in it.
4088 */
4089 encoding = get_log_output_encoding();
ecb5091f 4090 message = repo_logmsg_reencode(the_repository, commit, NULL, encoding);
04deccda 4091
baa6378f
JH
4092 /* Copy the commit to temporary if we are using "fake" headers */
4093 if (buf.len)
04deccda 4094 strbuf_addstr(&buf, message);
baa6378f 4095
df874fa8 4096 if (opt->grep_filter.header_list && opt->mailmap) {
e9c1b0e3
SA
4097 const char *commit_headers[] = { "author ", "committer ", NULL };
4098
d72fbe81 4099 if (!buf.len)
04deccda 4100 strbuf_addstr(&buf, message);
d72fbe81 4101
66a8a953 4102 apply_mailmap_to_header(&buf, commit_headers, opt->mailmap);
d72fbe81
AP
4103 }
4104
38cfe915
NTND
4105 /* Append "fake" message parts as needed */
4106 if (opt->show_notes) {
4107 if (!buf.len)
04deccda 4108 strbuf_addstr(&buf, message);
fb61e4d3 4109 format_display_notes(&commit->object.oid, &buf, encoding, 1);
38cfe915
NTND
4110 }
4111
b000c59b
JK
4112 /*
4113 * Find either in the original commit message, or in the temporary.
4114 * Note that we cast away the constness of "message" here. It is
4115 * const because it may come from the cached commit buffer. That's OK,
4116 * because we know that it is modifiable heap memory, and that while
4117 * grep_buffer may modify it for speed, it will restore any
4118 * changes before returning.
4119 */
72fd13f7
NTND
4120 if (buf.len)
4121 retval = grep_buffer(&opt->grep_filter, buf.buf, buf.len);
4122 else
4123 retval = grep_buffer(&opt->grep_filter,
b000c59b 4124 (char *)message, strlen(message));
72fd13f7 4125 strbuf_release(&buf);
ecb5091f 4126 repo_unuse_commit_buffer(the_repository, commit, message);
794c0002 4127 return retval;
8ecae9b0
JH
4128}
4129
53d00b39 4130static inline int want_ancestry(const struct rev_info *revs)
f35f5603 4131{
8bb65883 4132 return (revs->rewrite_parents || revs->children.name);
f35f5603
JH
4133}
4134
de239446
JK
4135/*
4136 * Return a timestamp to be used for --since/--until comparisons for this
4137 * commit, based on the revision options.
4138 */
4139static timestamp_t comparison_date(const struct rev_info *revs,
4140 struct commit *commit)
4141{
4142 return revs->reflog_info ?
4143 get_reflog_timestamp(revs->reflog_info) :
4144 commit->date;
4145}
4146
beb5af43 4147enum commit_action get_commit_action(struct rev_info *revs, struct commit *commit)
252a7c02
LT
4148{
4149 if (commit->object.flags & SHOWN)
4150 return commit_ignore;
cc656f4e 4151 if (revs->unpacked && has_object_pack(revs->repo, &commit->object.oid))
252a7c02 4152 return commit_ignore;
c9fff000 4153 if (revs->no_kept_objects) {
cc656f4e 4154 if (has_object_kept_pack(revs->repo, &commit->object.oid,
c9fff000
TB
4155 revs->keep_pack_cache_flags))
4156 return commit_ignore;
4157 }
252a7c02
LT
4158 if (commit->object.flags & UNINTERESTING)
4159 return commit_ignore;
3cb9d2b6
SG
4160 if (revs->line_level_traverse && !want_ancestry(revs)) {
4161 /*
4162 * In case of line-level log with parent rewriting
4163 * prepare_revision_walk() already took care of all line-level
4164 * log filtering, and there is nothing left to do here.
4165 *
4166 * If parent rewriting was not requested, then this is the
4167 * place to perform the line-level log filtering. Notably,
4168 * this check, though expensive, must come before the other,
4169 * cheaper filtering conditions, because the tracked line
4170 * ranges must be adjusted even when the commit will end up
4171 * being ignored based on other conditions.
4172 */
4173 if (!line_log_process_ranges_arbitrary_commit(revs, commit))
4174 return commit_ignore;
4175 }
de239446
JK
4176 if (revs->min_age != -1 &&
4177 comparison_date(revs, commit) > revs->min_age)
4178 return commit_ignore;
96697781
MV
4179 if (revs->max_age_as_filter != -1 &&
4180 comparison_date(revs, commit) < revs->max_age_as_filter)
4181 return commit_ignore;
ad5aeede 4182 if (revs->min_parents || (revs->max_parents >= 0)) {
bf3418b0 4183 int n = commit_list_count(commit->parents);
ad5aeede
MG
4184 if ((n < revs->min_parents) ||
4185 ((revs->max_parents >= 0) && (n > revs->max_parents)))
4186 return commit_ignore;
4187 }
252a7c02
LT
4188 if (!commit_match(commit, revs))
4189 return commit_ignore;
53b2c823 4190 if (revs->prune && revs->dense) {
252a7c02 4191 /* Commit without changes? */
7dc0fe3b 4192 if (commit->object.flags & TREESAME) {
bf3418b0
KB
4193 int n;
4194 struct commit_list *p;
252a7c02 4195 /* drop merges unless we want parenthood */
f35f5603 4196 if (!want_ancestry(revs))
252a7c02 4197 return commit_ignore;
8d049e18
DS
4198
4199 if (revs->show_pulls && (commit->object.flags & PULL_MERGE))
4200 return commit_show;
4201
bf3418b0
KB
4202 /*
4203 * If we want ancestry, then need to keep any merges
4204 * between relevant commits to tie together topology.
4205 * For consistency with TREESAME and simplification
4206 * use "relevant" here rather than just INTERESTING,
4207 * to treat bottom commit(s) as part of the topology.
4208 */
4209 for (n = 0, p = commit->parents; p; p = p->next)
4210 if (relevant_commit(p->item))
4211 if (++n >= 2)
4212 return commit_show;
4213 return commit_ignore;
252a7c02 4214 }
252a7c02
LT
4215 }
4216 return commit_show;
4217}
4218
0131c490
JH
4219define_commit_slab(saved_parents, struct commit_list *);
4220
4221#define EMPTY_PARENT_LIST ((struct commit_list *)-1)
4222
4223/*
4224 * You may only call save_parents() once per commit (this is checked
4225 * for non-root commits).
4226 */
4227static void save_parents(struct rev_info *revs, struct commit *commit)
4228{
4229 struct commit_list **pp;
4230
4231 if (!revs->saved_parents_slab) {
4232 revs->saved_parents_slab = xmalloc(sizeof(struct saved_parents));
4233 init_saved_parents(revs->saved_parents_slab);
4234 }
4235
4236 pp = saved_parents_at(revs->saved_parents_slab, commit);
4237
4238 /*
4239 * When walking with reflogs, we may visit the same commit
4240 * several times: once for each appearance in the reflog.
4241 *
4242 * In this case, save_parents() will be called multiple times.
4243 * We want to keep only the first set of parents. We need to
4244 * store a sentinel value for an empty (i.e., NULL) parent
4245 * list to distinguish it from a not-yet-saved list, however.
4246 */
4247 if (*pp)
4248 return;
4249 if (commit->parents)
4250 *pp = copy_commit_list(commit->parents);
4251 else
4252 *pp = EMPTY_PARENT_LIST;
4253}
4254
6512d6e4
PS
4255static void free_saved_parent(struct commit_list **parents)
4256{
4257 if (*parents != EMPTY_PARENT_LIST)
4258 free_commit_list(*parents);
4259}
4260
0131c490
JH
4261static void free_saved_parents(struct rev_info *revs)
4262{
6512d6e4
PS
4263 if (!revs->saved_parents_slab)
4264 return;
4265 deep_clear_saved_parents(revs->saved_parents_slab, free_saved_parent);
4266 FREE_AND_NULL(revs->saved_parents_slab);
0131c490
JH
4267}
4268
4269struct commit_list *get_saved_parents(struct rev_info *revs, const struct commit *commit)
4270{
4271 struct commit_list *parents;
4272
4273 if (!revs->saved_parents_slab)
4274 return commit->parents;
4275
4276 parents = *saved_parents_at(revs->saved_parents_slab, commit);
4277 if (parents == EMPTY_PARENT_LIST)
4278 return NULL;
4279 return parents;
4280}
4281
beb5af43
AS
4282enum commit_action simplify_commit(struct rev_info *revs, struct commit *commit)
4283{
4284 enum commit_action action = get_commit_action(revs, commit);
4285
4286 if (action == commit_show &&
beb5af43 4287 revs->prune && revs->dense && want_ancestry(revs)) {
53d00b39
TR
4288 /*
4289 * --full-diff on simplified parents is no good: it
4290 * will show spurious changes from the commits that
4291 * were elided. So we save the parents on the side
4292 * when --full-diff is in effect.
4293 */
4294 if (revs->full_diff)
4295 save_parents(revs, commit);
c7edcae0 4296 if (rewrite_parents(revs, commit, rewrite_one) < 0)
beb5af43
AS
4297 return commit_error;
4298 }
4299 return action;
4300}
4301
1b32dece
NTND
4302static void track_linear(struct rev_info *revs, struct commit *commit)
4303{
4304 if (revs->track_first_time) {
4305 revs->linear = 1;
4306 revs->track_first_time = 0;
4307 } else {
4308 struct commit_list *p;
4309 for (p = revs->previous_parents; p; p = p->next)
4310 if (p->item == NULL || /* first commit */
4a7e27e9 4311 oideq(&p->item->object.oid, &commit->object.oid))
1b32dece
NTND
4312 break;
4313 revs->linear = p != NULL;
4314 }
4315 if (revs->reverse) {
4316 if (revs->linear)
4317 commit->object.flags |= TRACK_LINEAR;
4318 }
4319 free_commit_list(revs->previous_parents);
4320 revs->previous_parents = copy_commit_list(commit->parents);
4321}
4322
d5db6c9e 4323static struct commit *get_revision_1(struct rev_info *revs)
a4a88b2b 4324{
7c2f08aa 4325 while (1) {
d08565bf 4326 struct commit *commit;
a4a88b2b 4327
d08565bf
JK
4328 if (revs->reflog_info)
4329 commit = next_reflog_entry(revs->reflog_info);
f0d9cc41
DS
4330 else if (revs->topo_walk_info)
4331 commit = next_topo_commit(revs);
d08565bf
JK
4332 else
4333 commit = pop_commit(&revs->commits);
2a0925be 4334
7c2f08aa
JK
4335 if (!commit)
4336 return NULL;
4337
d08565bf 4338 if (revs->reflog_info)
ffa1eeae 4339 commit->object.flags &= ~(ADDED | SEEN | SHOWN);
8860fd42 4340
2a0925be
LT
4341 /*
4342 * If we haven't done the list limiting, we need to look at
be7db6e5
LT
4343 * the parents here. We also need to do the date-based limiting
4344 * that we'd otherwise have done in limit_list().
2a0925be 4345 */
be7db6e5 4346 if (!revs->limited) {
744f4985 4347 if (revs->max_age != -1 &&
de239446 4348 comparison_date(revs, commit) < revs->max_age)
86ab4906 4349 continue;
d08565bf
JK
4350
4351 if (revs->reflog_info)
4352 try_to_simplify_commit(revs, commit);
f0d9cc41
DS
4353 else if (revs->topo_walk_info)
4354 expand_topo_walk(revs, commit);
5284fc5c 4355 else if (process_parents(revs, commit, &revs->commits, NULL) < 0) {
2db1a43f
VM
4356 if (!revs->ignore_missing_links)
4357 die("Failed to traverse parents of commit %s",
f2fd0760 4358 oid_to_hex(&commit->object.oid));
2db1a43f 4359 }
be7db6e5 4360 }
744f4985 4361
252a7c02
LT
4362 switch (simplify_commit(revs, commit)) {
4363 case commit_ignore:
8ecae9b0 4364 continue;
252a7c02 4365 case commit_error:
ed62089c 4366 die("Failed to simplify parents of commit %s",
f2fd0760 4367 oid_to_hex(&commit->object.oid));
252a7c02 4368 default:
1b32dece
NTND
4369 if (revs->track_linear)
4370 track_linear(revs, commit);
252a7c02 4371 return commit;
384e99a4 4372 }
7c2f08aa 4373 }
765ac8ec 4374}
d5db6c9e 4375
be6754c6
MH
4376/*
4377 * Return true for entries that have not yet been shown. (This is an
4378 * object_array_each_func_t.)
4379 */
d3dcfa04 4380static int entry_unshown(struct object_array_entry *entry, void *cb_data UNUSED)
86ab4906 4381{
be6754c6
MH
4382 return !(entry->item->flags & SHOWN);
4383}
86ab4906 4384
be6754c6
MH
4385/*
4386 * If array is on the verge of a realloc, garbage-collect any entries
4387 * that have already been shown to try to free up some space.
4388 */
4389static void gc_boundary(struct object_array *array)
4390{
4391 if (array->nr == array->alloc)
4392 object_array_filter(array, entry_unshown, NULL);
86ab4906
JH
4393}
4394
4603ec0f
AS
4395static void create_boundary_commit_list(struct rev_info *revs)
4396{
4397 unsigned i;
4398 struct commit *c;
4399 struct object_array *array = &revs->boundary_commits;
4400 struct object_array_entry *objects = array->objects;
4401
4402 /*
4403 * If revs->commits is non-NULL at this point, an error occurred in
4404 * get_revision_1(). Ignore the error and continue printing the
4405 * boundary commits anyway. (This is what the code has always
4406 * done.)
4407 */
bf20fe4c
ÆAB
4408 free_commit_list(revs->commits);
4409 revs->commits = NULL;
4603ec0f
AS
4410
4411 /*
4412 * Put all of the actual boundary commits from revs->boundary_commits
4413 * into revs->commits
4414 */
4415 for (i = 0; i < array->nr; i++) {
4416 c = (struct commit *)(objects[i].item);
4417 if (!c)
4418 continue;
4419 if (!(c->object.flags & CHILD_SHOWN))
4420 continue;
4421 if (c->object.flags & (SHOWN | BOUNDARY))
4422 continue;
4423 c->object.flags |= BOUNDARY;
4424 commit_list_insert(c, &revs->commits);
4425 }
4426
4427 /*
4428 * If revs->topo_order is set, sort the boundary commits
4429 * in topological order
4430 */
08f704f2 4431 sort_in_topological_order(&revs->commits, revs->sort_order);
4603ec0f
AS
4432}
4433
7fefda5c 4434static struct commit *get_revision_internal(struct rev_info *revs)
d5db6c9e
JH
4435{
4436 struct commit *c = NULL;
86ab4906
JH
4437 struct commit_list *l;
4438
4439 if (revs->boundary == 2) {
4603ec0f
AS
4440 /*
4441 * All of the normal commits have already been returned,
4442 * and we are now returning boundary commits.
4443 * create_boundary_commit_list() has populated
4444 * revs->commits with the remaining commits to return.
4445 */
4446 c = pop_commit(&revs->commits);
4447 if (c)
4448 c->object.flags |= SHOWN;
9c5e66e9
JS
4449 return c;
4450 }
4451
86ab4906 4452 /*
b72a1904
JK
4453 * If our max_count counter has reached zero, then we are done. We
4454 * don't simply return NULL because we still might need to show
4455 * boundary commits. But we want to avoid calling get_revision_1, which
4456 * might do a considerable amount of work finding the next commit only
4457 * for us to throw it away.
4458 *
4459 * If it is non-zero, then either we don't have a max_count at all
4460 * (-1), or it is still counting, in which case we decrement.
86ab4906 4461 */
b72a1904
JK
4462 if (revs->max_count) {
4463 c = get_revision_1(revs);
4464 if (c) {
9e57ac55 4465 while (revs->skip_count > 0) {
b72a1904
JK
4466 revs->skip_count--;
4467 c = get_revision_1(revs);
4468 if (!c)
4469 break;
6bd2ae67 4470 free_commit_buffer(revs->repo->parsed_objects, c);
b72a1904 4471 }
8839ac94 4472 }
86ab4906 4473
b72a1904
JK
4474 if (revs->max_count > 0)
4475 revs->max_count--;
d5db6c9e 4476 }
86ab4906 4477
c33d8593
JH
4478 if (c)
4479 c->object.flags |= SHOWN;
4480
9e57ac55 4481 if (!revs->boundary)
d5db6c9e 4482 return c;
86ab4906
JH
4483
4484 if (!c) {
4485 /*
4486 * get_revision_1() runs out the commits, and
4487 * we are done computing the boundaries.
4488 * switch to boundary commits output mode.
4489 */
4490 revs->boundary = 2;
4603ec0f
AS
4491
4492 /*
4493 * Update revs->commits to contain the list of
4494 * boundary commits.
4495 */
4496 create_boundary_commit_list(revs);
4497
3c68d67b 4498 return get_revision_internal(revs);
86ab4906
JH
4499 }
4500
4501 /*
4502 * boundary commits are the commits that are parents of the
4503 * ones we got from get_revision_1() but they themselves are
4504 * not returned from get_revision_1(). Before returning
4505 * 'c', we need to mark its parents that they could be boundaries.
4506 */
4507
4508 for (l = c->parents; l; l = l->next) {
4509 struct object *p;
4510 p = &(l->item->object);
c33d8593 4511 if (p->flags & (CHILD_SHOWN | SHOWN))
86ab4906
JH
4512 continue;
4513 p->flags |= CHILD_SHOWN;
4514 gc_boundary(&revs->boundary_commits);
4515 add_object_array(p, NULL, &revs->boundary_commits);
4516 }
4517
4518 return c;
d5db6c9e 4519}
7fefda5c
AS
4520
4521struct commit *get_revision(struct rev_info *revs)
4522{
498bcd31
TR
4523 struct commit *c;
4524 struct commit_list *reversed;
4525
4526 if (revs->reverse) {
4527 reversed = NULL;
9e57ac55 4528 while ((c = get_revision_internal(revs)))
498bcd31 4529 commit_list_insert(c, &reversed);
56931c4d 4530 free_commit_list(revs->commits);
498bcd31
TR
4531 revs->commits = reversed;
4532 revs->reverse = 0;
4533 revs->reverse_output_stage = 1;
4534 }
4535
1b32dece
NTND
4536 if (revs->reverse_output_stage) {
4537 c = pop_commit(&revs->commits);
4538 if (revs->track_linear)
4539 revs->linear = !!(c && c->object.flags & TRACK_LINEAR);
4540 return c;
4541 }
498bcd31
TR
4542
4543 c = get_revision_internal(revs);
7fefda5c
AS
4544 if (c && revs->graph)
4545 graph_update(revs->graph, c);
1b32dece 4546 if (!c) {
53d00b39 4547 free_saved_parents(revs);
bf20fe4c
ÆAB
4548 free_commit_list(revs->previous_parents);
4549 revs->previous_parents = NULL;
1b32dece 4550 }
7fefda5c
AS
4551 return c;
4552}
1df2d656 4553
49825164 4554const char *get_revision_mark(const struct rev_info *revs, const struct commit *commit)
1df2d656
MG
4555{
4556 if (commit->object.flags & BOUNDARY)
4557 return "-";
4558 else if (commit->object.flags & UNINTERESTING)
4559 return "^";
adbbb31e
MG
4560 else if (commit->object.flags & PATCHSAME)
4561 return "=";
1df2d656
MG
4562 else if (!revs || revs->left_right) {
4563 if (commit->object.flags & SYMMETRIC_LEFT)
4564 return "<";
4565 else
4566 return ">";
4567 } else if (revs->graph)
4568 return "*";
adbbb31e
MG
4569 else if (revs->cherry_mark)
4570 return "+";
1df2d656
MG
4571 return "";
4572}
b1b47554
MG
4573
4574void put_revision_mark(const struct rev_info *revs, const struct commit *commit)
4575{
49825164 4576 const char *mark = get_revision_mark(revs, commit);
b1b47554
MG
4577 if (!strlen(mark))
4578 return;
4579 fputs(mark, stdout);
4580 putchar(' ');
4581}