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