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