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