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