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