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