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