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