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