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