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