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