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