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