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