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