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