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