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