Merge branch 'jk/cached-commit-buffer'
[git/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);
b6e8a3b5 1068 slop = still_interesting(list, date, slop, &interesting_cache);
7d004199 1069 if (slop)
3131b713 1070 continue;
7d004199 1071 break;
a4a88b2b
LT
1072 }
1073 if (revs->min_age != -1 && (commit->date > revs->min_age))
1074 continue;
7d004199 1075 date = commit->date;
a4a88b2b 1076 p = &commit_list_insert(commit, p)->next;
cdcefbc9
LT
1077
1078 show = show_early_output;
1079 if (!show)
1080 continue;
1081
1082 show(revs, newlist);
1083 show_early_output = NULL;
a4a88b2b 1084 }
adbbb31e 1085 if (revs->cherry_pick || revs->cherry_mark)
36d56de6 1086 cherry_pick_list(newlist, revs);
d7a17cad 1087
60adf7d7
MG
1088 if (revs->left_only || revs->right_only)
1089 limit_left_right(newlist, revs);
1090
ebdc94f3
JH
1091 if (bottom) {
1092 limit_to_ancestry(bottom, newlist);
1093 free_commit_list(bottom);
1094 }
1095
4d826608
KB
1096 /*
1097 * Check if any commits have become TREESAME by some of their parents
1098 * becoming UNINTERESTING.
1099 */
1100 if (limiting_can_increase_treesame(revs))
1101 for (list = newlist; list; list = list->next) {
1102 struct commit *c = list->item;
1103 if (c->object.flags & (UNINTERESTING | TREESAME))
1104 continue;
1105 update_treesame(revs, c);
1106 }
1107
a4a88b2b 1108 revs->commits = newlist;
cc0e6c5a 1109 return 0;
a4a88b2b
LT
1110}
1111
df835d3a
MH
1112/*
1113 * Add an entry to refs->cmdline with the specified information.
1114 * *name is copied.
1115 */
281eee47
JH
1116static void add_rev_cmdline(struct rev_info *revs,
1117 struct object *item,
1118 const char *name,
1119 int whence,
1120 unsigned flags)
1121{
1122 struct rev_cmdline_info *info = &revs->cmdline;
071bcaab 1123 unsigned int nr = info->nr;
281eee47
JH
1124
1125 ALLOC_GROW(info->rev, nr + 1, info->alloc);
1126 info->rev[nr].item = item;
df835d3a 1127 info->rev[nr].name = xstrdup(name);
281eee47
JH
1128 info->rev[nr].whence = whence;
1129 info->rev[nr].flags = flags;
1130 info->nr++;
1131}
1132
a765499a
KB
1133static void add_rev_cmdline_list(struct rev_info *revs,
1134 struct commit_list *commit_list,
1135 int whence,
1136 unsigned flags)
1137{
1138 while (commit_list) {
1139 struct object *object = &commit_list->item->object;
f2fd0760 1140 add_rev_cmdline(revs, object, oid_to_hex(&object->oid),
a765499a
KB
1141 whence, flags);
1142 commit_list = commit_list->next;
1143 }
1144}
1145
63049292
JH
1146struct all_refs_cb {
1147 int all_flags;
71b03b42 1148 int warned_bad_reflog;
63049292
JH
1149 struct rev_info *all_revs;
1150 const char *name_for_errormsg;
acd9544a 1151 struct ref_store *refs;
63049292 1152};
ae563542 1153
ff32d342 1154int ref_excluded(struct string_list *ref_excludes, const char *path)
e7b432c5
JH
1155{
1156 struct string_list_item *item;
1157
ff32d342 1158 if (!ref_excludes)
e7b432c5 1159 return 0;
ff32d342 1160 for_each_string_list_item(item, ref_excludes) {
55d34269 1161 if (!wildmatch(item->string, path, 0))
e7b432c5
JH
1162 return 1;
1163 }
1164 return 0;
1165}
1166
a217dcbd
MH
1167static int handle_one_ref(const char *path, const struct object_id *oid,
1168 int flag, void *cb_data)
ae563542 1169{
63049292 1170 struct all_refs_cb *cb = cb_data;
e7b432c5
JH
1171 struct object *object;
1172
ff32d342 1173 if (ref_excluded(cb->all_revs->ref_excludes, path))
e7b432c5
JH
1174 return 0;
1175
654b9a90 1176 object = get_reference(cb->all_revs, path, oid, cb->all_flags);
281eee47 1177 add_rev_cmdline(cb->all_revs, object, path, REV_CMD_REF, cb->all_flags);
a58a1b01 1178 add_pending_oid(cb->all_revs, path, oid, cb->all_flags);
ae563542
LT
1179 return 0;
1180}
1181
d08bae7e
IL
1182static void init_all_refs_cb(struct all_refs_cb *cb, struct rev_info *revs,
1183 unsigned flags)
1184{
1185 cb->all_revs = revs;
1186 cb->all_flags = flags;
7ba82629 1187 revs->rev_input_given = 1;
acd9544a 1188 cb->refs = NULL;
d08bae7e
IL
1189}
1190
ff32d342 1191void clear_ref_exclusion(struct string_list **ref_excludes_p)
e7b432c5 1192{
ff32d342
JH
1193 if (*ref_excludes_p) {
1194 string_list_clear(*ref_excludes_p, 0);
1195 free(*ref_excludes_p);
e7b432c5 1196 }
ff32d342 1197 *ref_excludes_p = NULL;
e7b432c5
JH
1198}
1199
ff32d342 1200void add_ref_exclusion(struct string_list **ref_excludes_p, const char *exclude)
e7b432c5 1201{
ff32d342
JH
1202 if (!*ref_excludes_p) {
1203 *ref_excludes_p = xcalloc(1, sizeof(**ref_excludes_p));
1204 (*ref_excludes_p)->strdup_strings = 1;
e7b432c5 1205 }
ff32d342 1206 string_list_append(*ref_excludes_p, exclude);
e7b432c5
JH
1207}
1208
073cf63c
NTND
1209static void handle_refs(struct ref_store *refs,
1210 struct rev_info *revs, unsigned flags,
1211 int (*for_each)(struct ref_store *, each_ref_fn, void *))
ae563542 1212{
63049292 1213 struct all_refs_cb cb;
073cf63c
NTND
1214
1215 if (!refs) {
1216 /* this could happen with uninitialized submodules */
1217 return;
1218 }
1219
d08bae7e 1220 init_all_refs_cb(&cb, revs, flags);
073cf63c 1221 for_each(refs, handle_one_ref, &cb);
63049292
JH
1222}
1223
9461d272 1224static void handle_one_reflog_commit(struct object_id *oid, void *cb_data)
63049292
JH
1225{
1226 struct all_refs_cb *cb = cb_data;
9461d272 1227 if (!is_null_oid(oid)) {
c251c83d 1228 struct object *o = parse_object(oid);
71b03b42
SP
1229 if (o) {
1230 o->flags |= cb->all_flags;
281eee47 1231 /* ??? CMDLINEFLAGS ??? */
71b03b42
SP
1232 add_pending_object(cb->all_revs, o, "");
1233 }
1234 else if (!cb->warned_bad_reflog) {
46efd2d9 1235 warning("reflog of '%s' references pruned commits",
71b03b42
SP
1236 cb->name_for_errormsg);
1237 cb->warned_bad_reflog = 1;
1238 }
63049292 1239 }
71b03b42
SP
1240}
1241
9461d272 1242static int handle_one_reflog_ent(struct object_id *ooid, struct object_id *noid,
dddbad72 1243 const char *email, timestamp_t timestamp, int tz,
883d60fa 1244 const char *message, void *cb_data)
71b03b42 1245{
9461d272 1246 handle_one_reflog_commit(ooid, cb_data);
1247 handle_one_reflog_commit(noid, cb_data);
63049292
JH
1248 return 0;
1249}
1250
a89caf4b
MH
1251static int handle_one_reflog(const char *path, const struct object_id *oid,
1252 int flag, void *cb_data)
63049292
JH
1253{
1254 struct all_refs_cb *cb = cb_data;
71b03b42 1255 cb->warned_bad_reflog = 0;
63049292 1256 cb->name_for_errormsg = path;
acd9544a
NTND
1257 refs_for_each_reflog_ent(cb->refs, path,
1258 handle_one_reflog_ent, cb_data);
63049292
JH
1259 return 0;
1260}
1261
acd9544a
NTND
1262static void add_other_reflogs_to_pending(struct all_refs_cb *cb)
1263{
1264 struct worktree **worktrees, **p;
1265
1266 worktrees = get_worktrees(0);
1267 for (p = worktrees; *p; p++) {
1268 struct worktree *wt = *p;
1269
1270 if (wt->is_current)
1271 continue;
1272
1273 cb->refs = get_worktree_ref_store(wt);
1274 refs_for_each_reflog(cb->refs,
1275 handle_one_reflog,
1276 cb);
1277 }
1278 free_worktrees(worktrees);
1279}
1280
718ccc97 1281void add_reflogs_to_pending(struct rev_info *revs, unsigned flags)
63049292
JH
1282{
1283 struct all_refs_cb cb;
2b2a5be3 1284
63049292
JH
1285 cb.all_revs = revs;
1286 cb.all_flags = flags;
acd9544a 1287 cb.refs = get_main_ref_store();
a89caf4b 1288 for_each_reflog(handle_one_reflog, &cb);
acd9544a
NTND
1289
1290 if (!revs->single_worktree)
1291 add_other_reflogs_to_pending(&cb);
ae563542
LT
1292}
1293
4fe10219
JK
1294static void add_cache_tree(struct cache_tree *it, struct rev_info *revs,
1295 struct strbuf *path)
1296{
1297 size_t baselen = path->len;
1298 int i;
1299
1300 if (it->entry_count >= 0) {
740ee055 1301 struct tree *tree = lookup_tree(&it->oid);
4fe10219
JK
1302 add_pending_object_with_path(revs, &tree->object, "",
1303 040000, path->buf);
1304 }
1305
1306 for (i = 0; i < it->subtree_nr; i++) {
1307 struct cache_tree_sub *sub = it->down[i];
1308 strbuf_addf(path, "%s%s", baselen ? "/" : "", sub->name);
1309 add_cache_tree(sub->cache_tree, revs, path);
1310 strbuf_setlen(path, baselen);
1311 }
1312
1313}
1314
6c3d8181
NTND
1315static void do_add_index_objects_to_pending(struct rev_info *revs,
1316 struct index_state *istate)
4fe10219
JK
1317{
1318 int i;
1319
6c3d8181
NTND
1320 for (i = 0; i < istate->cache_nr; i++) {
1321 struct cache_entry *ce = istate->cache[i];
4fe10219
JK
1322 struct blob *blob;
1323
1324 if (S_ISGITLINK(ce->ce_mode))
1325 continue;
1326
3aca1fc6 1327 blob = lookup_blob(&ce->oid);
4fe10219
JK
1328 if (!blob)
1329 die("unable to add index blob to traversal");
1330 add_pending_object_with_path(revs, &blob->object, "",
1331 ce->ce_mode, ce->name);
1332 }
1333
6c3d8181 1334 if (istate->cache_tree) {
4fe10219 1335 struct strbuf path = STRBUF_INIT;
6c3d8181 1336 add_cache_tree(istate->cache_tree, revs, &path);
4fe10219
JK
1337 strbuf_release(&path);
1338 }
1339}
1340
6c3d8181
NTND
1341void add_index_objects_to_pending(struct rev_info *revs, unsigned int flags)
1342{
be489d02
NTND
1343 struct worktree **worktrees, **p;
1344
6c3d8181
NTND
1345 read_cache();
1346 do_add_index_objects_to_pending(revs, &the_index);
be489d02
NTND
1347
1348 if (revs->single_worktree)
1349 return;
1350
1351 worktrees = get_worktrees(0);
1352 for (p = worktrees; *p; p++) {
1353 struct worktree *wt = *p;
1354 struct index_state istate = { NULL };
1355
1356 if (wt->is_current)
1357 continue; /* current index already taken care of */
1358
1359 if (read_index_from(&istate,
a125a223
TG
1360 worktree_git_path(wt, "index"),
1361 get_worktree_git_dir(wt)) > 0)
be489d02
NTND
1362 do_add_index_objects_to_pending(revs, &istate);
1363 discard_index(&istate);
1364 }
1365 free_worktrees(worktrees);
6c3d8181
NTND
1366}
1367
8779351d
VN
1368static int add_parents_only(struct rev_info *revs, const char *arg_, int flags,
1369 int exclude_parent)
ea4a19e1 1370{
654b9a90 1371 struct object_id oid;
ea4a19e1
JH
1372 struct object *it;
1373 struct commit *commit;
1374 struct commit_list *parents;
8779351d 1375 int parent_number;
281eee47 1376 const char *arg = arg_;
ea4a19e1
JH
1377
1378 if (*arg == '^') {
7f34a46f 1379 flags ^= UNINTERESTING | BOTTOM;
ea4a19e1
JH
1380 arg++;
1381 }
e82caf38 1382 if (get_oid_committish(arg, &oid))
ea4a19e1
JH
1383 return 0;
1384 while (1) {
654b9a90 1385 it = get_reference(revs, arg, &oid, 0);
cc243c3c
JH
1386 if (!it && revs->ignore_missing)
1387 return 0;
1974632c 1388 if (it->type != OBJ_TAG)
ea4a19e1 1389 break;
9684afd9
MK
1390 if (!((struct tag*)it)->tagged)
1391 return 0;
654b9a90 1392 oidcpy(&oid, &((struct tag*)it)->tagged->oid);
ea4a19e1 1393 }
1974632c 1394 if (it->type != OBJ_COMMIT)
ea4a19e1
JH
1395 return 0;
1396 commit = (struct commit *)it;
8779351d
VN
1397 if (exclude_parent &&
1398 exclude_parent > commit_list_count(commit->parents))
1399 return 0;
1400 for (parents = commit->parents, parent_number = 1;
1401 parents;
1402 parents = parents->next, parent_number++) {
1403 if (exclude_parent && parent_number != exclude_parent)
1404 continue;
1405
ea4a19e1
JH
1406 it = &parents->item->object;
1407 it->flags |= flags;
281eee47 1408 add_rev_cmdline(revs, it, arg_, REV_CMD_PARENTS_ONLY, flags);
ea4a19e1
JH
1409 add_pending_object(revs, it, arg);
1410 }
1411 return 1;
1412}
1413
db6296a5 1414void init_revisions(struct rev_info *revs, const char *prefix)
8efdc326
FK
1415{
1416 memset(revs, 0, sizeof(*revs));
8e8f9987 1417
6b9c58f4 1418 revs->abbrev = DEFAULT_ABBREV;
8e8f9987 1419 revs->ignore_merges = 1;
9202434c 1420 revs->simplify_history = 1;
0d1e0e78
BW
1421 revs->pruning.flags.recursive = 1;
1422 revs->pruning.flags.quick = 1;
cd2bdc53
LT
1423 revs->pruning.add_remove = file_add_remove;
1424 revs->pruning.change = file_change;
a937b37e 1425 revs->pruning.change_fn_data = revs;
08f704f2 1426 revs->sort_order = REV_SORT_IN_GRAPH_ORDER;
8efdc326 1427 revs->dense = 1;
db6296a5 1428 revs->prefix = prefix;
8efdc326
FK
1429 revs->max_age = -1;
1430 revs->min_age = -1;
d5db6c9e 1431 revs->skip_count = -1;
8efdc326 1432 revs->max_count = -1;
ad5aeede 1433 revs->max_parents = -1;
0893eec8 1434 revs->expand_tabs_in_log = -1;
8efdc326 1435
cd2bdc53 1436 revs->commit_format = CMIT_FMT_DEFAULT;
fe37a9c5 1437 revs->expand_tabs_in_log_default = 8;
cd2bdc53 1438
918d4e1c
JH
1439 init_grep_defaults();
1440 grep_init(&revs->grep_filter, prefix);
0843acfd 1441 revs->grep_filter.status_only = 1;
0843acfd 1442
cd2bdc53 1443 diff_setup(&revs->diffopt);
c0cb4a06 1444 if (prefix && !revs->diffopt.prefix) {
cd676a51
JH
1445 revs->diffopt.prefix = prefix;
1446 revs->diffopt.prefix_length = strlen(prefix);
1447 }
3a03cf6b
JK
1448
1449 revs->notes_opt.use_default_notes = -1;
8efdc326
FK
1450}
1451
0d2c9d67
RS
1452static void add_pending_commit_list(struct rev_info *revs,
1453 struct commit_list *commit_list,
1454 unsigned int flags)
1455{
1456 while (commit_list) {
1457 struct object *object = &commit_list->item->object;
1458 object->flags |= flags;
f2fd0760 1459 add_pending_object(revs, object, oid_to_hex(&object->oid));
0d2c9d67
RS
1460 commit_list = commit_list->next;
1461 }
1462}
1463
ae3e5e1e
JH
1464static void prepare_show_merge(struct rev_info *revs)
1465{
1466 struct commit_list *bases;
1467 struct commit *head, *other;
68ab61dd 1468 struct object_id oid;
ae3e5e1e
JH
1469 const char **prune = NULL;
1470 int i, prune_num = 1; /* counting terminating NULL */
1471
68ab61dd 1472 if (get_oid("HEAD", &oid))
ae3e5e1e 1473 die("--merge without HEAD?");
bc83266a 1474 head = lookup_commit_or_die(&oid, "HEAD");
68ab61dd 1475 if (get_oid("MERGE_HEAD", &oid))
ae3e5e1e 1476 die("--merge without MERGE_HEAD?");
bc83266a 1477 other = lookup_commit_or_die(&oid, "MERGE_HEAD");
ae3e5e1e
JH
1478 add_pending_object(revs, &head->object, "HEAD");
1479 add_pending_object(revs, &other->object, "MERGE_HEAD");
2ce406cc 1480 bases = get_merge_bases(head, other);
7f34a46f
KB
1481 add_rev_cmdline_list(revs, bases, REV_CMD_MERGE_BASE, UNINTERESTING | BOTTOM);
1482 add_pending_commit_list(revs, bases, UNINTERESTING | BOTTOM);
e82447b1
JH
1483 free_commit_list(bases);
1484 head->object.flags |= SYMMETRIC_LEFT;
ae3e5e1e
JH
1485
1486 if (!active_nr)
1487 read_cache();
1488 for (i = 0; i < active_nr; i++) {
9c5e6c80 1489 const struct cache_entry *ce = active_cache[i];
ae3e5e1e
JH
1490 if (!ce_stage(ce))
1491 continue;
429bb40a 1492 if (ce_path_match(ce, &revs->prune_data, NULL)) {
ae3e5e1e 1493 prune_num++;
2756ca43 1494 REALLOC_ARRAY(prune, prune_num);
ae3e5e1e
JH
1495 prune[prune_num-2] = ce->name;
1496 prune[prune_num-1] = NULL;
1497 }
1498 while ((i+1 < active_nr) &&
1499 ce_same_name(ce, active_cache[i+1]))
1500 i++;
1501 }
ed6e8038 1502 clear_pathspec(&revs->prune_data);
4a2d5ae2 1503 parse_pathspec(&revs->prune_data, PATHSPEC_ALL_MAGIC & ~PATHSPEC_LITERAL,
c6f1b920 1504 PATHSPEC_PREFER_FULL | PATHSPEC_LITERAL_PATH, "", prune);
e82447b1 1505 revs->limited = 1;
ae3e5e1e
JH
1506}
1507
62faad5a
JK
1508static int dotdot_missing(const char *arg, char *dotdot,
1509 struct rev_info *revs, int symmetric)
1510{
1511 if (revs->ignore_missing)
1512 return 0;
1513 /* de-munge so we report the full argument */
1514 *dotdot = '.';
1515 die(symmetric
1516 ? "Invalid symmetric difference expression %s"
1517 : "Invalid revision range %s", arg);
1518}
1519
1520static int handle_dotdot_1(const char *arg, char *dotdot,
1521 struct rev_info *revs, int flags,
18f1ad76
JK
1522 int cant_be_filename,
1523 struct object_context *a_oc,
1524 struct object_context *b_oc)
62faad5a
JK
1525{
1526 const char *a_name, *b_name;
1527 struct object_id a_oid, b_oid;
1528 struct object *a_obj, *b_obj;
1529 unsigned int a_flags, b_flags;
1530 int symmetric = 0;
1531 unsigned int flags_exclude = flags ^ (UNINTERESTING | BOTTOM);
321c89bf 1532 unsigned int oc_flags = GET_OID_COMMITTISH | GET_OID_RECORD_PATH;
62faad5a
JK
1533
1534 a_name = arg;
1535 if (!*a_name)
1536 a_name = "HEAD";
1537
1538 b_name = dotdot + 2;
1539 if (*b_name == '.') {
1540 symmetric = 1;
1541 b_name++;
1542 }
1543 if (!*b_name)
1544 b_name = "HEAD";
1545
e82caf38 1546 if (get_oid_with_context(a_name, oc_flags, &a_oid, a_oc) ||
1547 get_oid_with_context(b_name, oc_flags, &b_oid, b_oc))
62faad5a
JK
1548 return -1;
1549
1550 if (!cant_be_filename) {
1551 *dotdot = '.';
1552 verify_non_filename(revs->prefix, arg);
1553 *dotdot = '\0';
1554 }
1555
7ef0d047
JH
1556 a_obj = parse_object(&a_oid);
1557 b_obj = parse_object(&b_oid);
62faad5a
JK
1558 if (!a_obj || !b_obj)
1559 return dotdot_missing(arg, dotdot, revs, symmetric);
1560
1561 if (!symmetric) {
1562 /* just A..B */
1563 b_flags = flags;
1564 a_flags = flags_exclude;
1565 } else {
1566 /* A...B -- find merge bases between the two */
1567 struct commit *a, *b;
1568 struct commit_list *exclude;
1569
7ef0d047
JH
1570 a = lookup_commit_reference(&a_obj->oid);
1571 b = lookup_commit_reference(&b_obj->oid);
62faad5a
JK
1572 if (!a || !b)
1573 return dotdot_missing(arg, dotdot, revs, symmetric);
1574
1575 exclude = get_merge_bases(a, b);
1576 add_rev_cmdline_list(revs, exclude, REV_CMD_MERGE_BASE,
1577 flags_exclude);
1578 add_pending_commit_list(revs, exclude, flags_exclude);
1579 free_commit_list(exclude);
1580
1581 b_flags = flags;
1582 a_flags = flags | SYMMETRIC_LEFT;
1583 }
1584
1585 a_obj->flags |= a_flags;
1586 b_obj->flags |= b_flags;
1587 add_rev_cmdline(revs, a_obj, a_name, REV_CMD_LEFT, a_flags);
1588 add_rev_cmdline(revs, b_obj, b_name, REV_CMD_RIGHT, b_flags);
18f1ad76
JK
1589 add_pending_object_with_path(revs, a_obj, a_name, a_oc->mode, a_oc->path);
1590 add_pending_object_with_path(revs, b_obj, b_name, b_oc->mode, b_oc->path);
62faad5a
JK
1591 return 0;
1592}
1593
1594static int handle_dotdot(const char *arg,
1595 struct rev_info *revs, int flags,
1596 int cant_be_filename)
1597{
18f1ad76 1598 struct object_context a_oc, b_oc;
62faad5a
JK
1599 char *dotdot = strstr(arg, "..");
1600 int ret;
1601
1602 if (!dotdot)
1603 return -1;
1604
18f1ad76
JK
1605 memset(&a_oc, 0, sizeof(a_oc));
1606 memset(&b_oc, 0, sizeof(b_oc));
1607
62faad5a 1608 *dotdot = '\0';
18f1ad76
JK
1609 ret = handle_dotdot_1(arg, dotdot, revs, flags, cant_be_filename,
1610 &a_oc, &b_oc);
62faad5a
JK
1611 *dotdot = '.';
1612
18f1ad76
JK
1613 free(a_oc.path);
1614 free(b_oc.path);
1615
62faad5a
JK
1616 return ret;
1617}
1618
8e676e8b 1619int handle_revision_arg(const char *arg_, struct rev_info *revs, int flags, unsigned revarg_opt)
5d6f0935 1620{
249c8f4a 1621 struct object_context oc;
f632dedd 1622 char *mark;
5d6f0935 1623 struct object *object;
654b9a90 1624 struct object_id oid;
5d6f0935 1625 int local_flags;
281eee47 1626 const char *arg = arg_;
8e676e8b 1627 int cant_be_filename = revarg_opt & REVARG_CANNOT_BE_FILENAME;
321c89bf 1628 unsigned get_sha1_flags = GET_OID_RECORD_PATH;
5d6f0935 1629
7f34a46f
KB
1630 flags = flags & UNINTERESTING ? flags | BOTTOM : flags & ~BOTTOM;
1631
d89797fe
JK
1632 if (!cant_be_filename && !strcmp(arg, "..")) {
1633 /*
1634 * Just ".."? That is not a range but the
1635 * pathspec for the parent directory.
1636 */
1637 return -1;
5d6f0935 1638 }
8779351d 1639
62faad5a
JK
1640 if (!handle_dotdot(arg, revs, flags, revarg_opt))
1641 return 0;
8779351d 1642
f632dedd
JK
1643 mark = strstr(arg, "^@");
1644 if (mark && !mark[2]) {
1645 *mark = 0;
8779351d 1646 if (add_parents_only(revs, arg, flags, 0))
5d6f0935 1647 return 0;
f632dedd 1648 *mark = '^';
5d6f0935 1649 }
f632dedd
JK
1650 mark = strstr(arg, "^!");
1651 if (mark && !mark[2]) {
1652 *mark = 0;
8779351d 1653 if (!add_parents_only(revs, arg, flags ^ (UNINTERESTING | BOTTOM), 0))
f632dedd 1654 *mark = '^';
8779351d 1655 }
f632dedd
JK
1656 mark = strstr(arg, "^-");
1657 if (mark) {
8779351d
VN
1658 int exclude_parent = 1;
1659
f632dedd 1660 if (mark[2]) {
8779351d 1661 char *end;
f632dedd 1662 exclude_parent = strtoul(mark + 2, &end, 10);
8779351d
VN
1663 if (*end != '\0' || !exclude_parent)
1664 return -1;
1665 }
1666
f632dedd 1667 *mark = 0;
8779351d 1668 if (!add_parents_only(revs, arg, flags ^ (UNINTERESTING | BOTTOM), exclude_parent))
f632dedd 1669 *mark = '^';
62476c8e
JH
1670 }
1671
5d6f0935
JH
1672 local_flags = 0;
1673 if (*arg == '^') {
7f34a46f 1674 local_flags = UNINTERESTING | BOTTOM;
5d6f0935
JH
1675 arg++;
1676 }
d5f6b1d7
JH
1677
1678 if (revarg_opt & REVARG_COMMITTISH)
321c89bf 1679 get_sha1_flags |= GET_OID_COMMITTISH;
d5f6b1d7 1680
e82caf38 1681 if (get_oid_with_context(arg, get_sha1_flags, &oid, &oc))
cc243c3c 1682 return revs->ignore_missing ? 0 : -1;
5d6f0935
JH
1683 if (!cant_be_filename)
1684 verify_non_filename(revs->prefix, arg);
654b9a90 1685 object = get_reference(revs, arg, &oid, flags ^ local_flags);
281eee47 1686 add_rev_cmdline(revs, object, arg_, REV_CMD_REV, flags ^ local_flags);
18f1ad76
JK
1687 add_pending_object_with_path(revs, object, arg, oc.mode, oc.path);
1688 free(oc.path);
5d6f0935
JH
1689 return 0;
1690}
1691
4da5af31 1692static void read_pathspec_from_stdin(struct rev_info *revs, struct strbuf *sb,
7fa3c2ad 1693 struct argv_array *prune)
4da5af31 1694{
7fa3c2ad
JK
1695 while (strbuf_getline(sb, stdin) != EOF)
1696 argv_array_push(prune, sb->buf);
60da8b15
JH
1697}
1698
4da5af31 1699static void read_revisions_from_stdin(struct rev_info *revs,
7fa3c2ad 1700 struct argv_array *prune)
1fc561d1 1701{
63d564b3 1702 struct strbuf sb;
60da8b15 1703 int seen_dashdash = 0;
4c30d504
JK
1704 int save_warning;
1705
1706 save_warning = warn_on_object_refname_ambiguity;
1707 warn_on_object_refname_ambiguity = 0;
1fc561d1 1708
63d564b3 1709 strbuf_init(&sb, 1000);
6e8d46f9 1710 while (strbuf_getline(&sb, stdin) != EOF) {
63d564b3 1711 int len = sb.len;
1fc561d1
AB
1712 if (!len)
1713 break;
60da8b15
JH
1714 if (sb.buf[0] == '-') {
1715 if (len == 2 && sb.buf[1] == '-') {
1716 seen_dashdash = 1;
1717 break;
1718 }
1fc561d1 1719 die("options not supported in --stdin mode");
60da8b15 1720 }
31faeb20 1721 if (handle_revision_arg(sb.buf, revs, 0,
70d26c6e 1722 REVARG_CANNOT_BE_FILENAME))
63d564b3 1723 die("bad revision '%s'", sb.buf);
1fc561d1 1724 }
60da8b15
JH
1725 if (seen_dashdash)
1726 read_pathspec_from_stdin(revs, &sb, prune);
4c30d504 1727
63d564b3 1728 strbuf_release(&sb);
4c30d504 1729 warn_on_object_refname_ambiguity = save_warning;
1fc561d1
AB
1730}
1731
2d10c555 1732static void add_grep(struct rev_info *revs, const char *ptn, enum grep_pat_token what)
bd95fcd3 1733{
0843acfd 1734 append_grep_pattern(&revs->grep_filter, ptn, "command line", 0, what);
2d10c555
JH
1735}
1736
a4d7d2c6 1737static void add_header_grep(struct rev_info *revs, enum grep_header_field field, const char *pattern)
2d10c555 1738{
a4d7d2c6 1739 append_header_grep_pattern(&revs->grep_filter, field, pattern);
bd95fcd3
JH
1740}
1741
1742static void add_message_grep(struct rev_info *revs, const char *pattern)
1743{
2d10c555 1744 add_grep(revs, pattern, GREP_PATTERN_BODY);
bd95fcd3
JH
1745}
1746
6b61ec05
PH
1747static int handle_revision_opt(struct rev_info *revs, int argc, const char **argv,
1748 int *unkc, const char **unkv)
02e54220
PH
1749{
1750 const char *arg = argv[0];
7d7b86f7
MM
1751 const char *optarg;
1752 int argcount;
02e54220
PH
1753
1754 /* pseudo revision arguments */
1755 if (!strcmp(arg, "--all") || !strcmp(arg, "--branches") ||
1756 !strcmp(arg, "--tags") || !strcmp(arg, "--remotes") ||
1757 !strcmp(arg, "--reflog") || !strcmp(arg, "--not") ||
ad3f9a71 1758 !strcmp(arg, "--no-walk") || !strcmp(arg, "--do-walk") ||
59556548 1759 !strcmp(arg, "--bisect") || starts_with(arg, "--glob=") ||
4fe10219 1760 !strcmp(arg, "--indexed-objects") ||
07768e03 1761 starts_with(arg, "--exclude=") ||
59556548
CC
1762 starts_with(arg, "--branches=") || starts_with(arg, "--tags=") ||
1763 starts_with(arg, "--remotes=") || starts_with(arg, "--no-walk="))
02e54220
PH
1764 {
1765 unkv[(*unkc)++] = arg;
0fe8c138 1766 return 1;
02e54220
PH
1767 }
1768
7d7b86f7
MM
1769 if ((argcount = parse_long_opt("max-count", argv, &optarg))) {
1770 revs->max_count = atoi(optarg);
5853caec 1771 revs->no_walk = 0;
7d7b86f7
MM
1772 return argcount;
1773 } else if ((argcount = parse_long_opt("skip", argv, &optarg))) {
1774 revs->skip_count = atoi(optarg);
1775 return argcount;
02e54220 1776 } else if ((*arg == '-') && isdigit(arg[1])) {
e3fa568c
JH
1777 /* accept -<digit>, like traditional "head" */
1778 if (strtol_i(arg + 1, 10, &revs->max_count) < 0 ||
1779 revs->max_count < 0)
1780 die("'%s': not a non-negative integer", arg + 1);
5853caec 1781 revs->no_walk = 0;
02e54220
PH
1782 } else if (!strcmp(arg, "-n")) {
1783 if (argc <= 1)
1784 return error("-n requires an argument");
1785 revs->max_count = atoi(argv[1]);
5853caec 1786 revs->no_walk = 0;
02e54220 1787 return 2;
479b3d97
SG
1788 } else if (skip_prefix(arg, "-n", &optarg)) {
1789 revs->max_count = atoi(optarg);
5853caec 1790 revs->no_walk = 0;
7d7b86f7
MM
1791 } else if ((argcount = parse_long_opt("max-age", argv, &optarg))) {
1792 revs->max_age = atoi(optarg);
1793 return argcount;
1794 } else if ((argcount = parse_long_opt("since", argv, &optarg))) {
1795 revs->max_age = approxidate(optarg);
1796 return argcount;
1797 } else if ((argcount = parse_long_opt("after", argv, &optarg))) {
1798 revs->max_age = approxidate(optarg);
1799 return argcount;
1800 } else if ((argcount = parse_long_opt("min-age", argv, &optarg))) {
1801 revs->min_age = atoi(optarg);
1802 return argcount;
1803 } else if ((argcount = parse_long_opt("before", argv, &optarg))) {
1804 revs->min_age = approxidate(optarg);
1805 return argcount;
1806 } else if ((argcount = parse_long_opt("until", argv, &optarg))) {
1807 revs->min_age = approxidate(optarg);
1808 return argcount;
02e54220
PH
1809 } else if (!strcmp(arg, "--first-parent")) {
1810 revs->first_parent_only = 1;
ebdc94f3
JH
1811 } else if (!strcmp(arg, "--ancestry-path")) {
1812 revs->ancestry_path = 1;
cb7529e1 1813 revs->simplify_history = 0;
ebdc94f3 1814 revs->limited = 1;
02e54220
PH
1815 } else if (!strcmp(arg, "-g") || !strcmp(arg, "--walk-reflogs")) {
1816 init_reflog_walk(&revs->reflog_info);
1817 } else if (!strcmp(arg, "--default")) {
1818 if (argc <= 1)
1819 return error("bad --default argument");
1820 revs->def = argv[1];
1821 return 2;
1822 } else if (!strcmp(arg, "--merge")) {
1823 revs->show_merge = 1;
1824 } else if (!strcmp(arg, "--topo-order")) {
08f704f2 1825 revs->sort_order = REV_SORT_IN_GRAPH_ORDER;
02e54220 1826 revs->topo_order = 1;
6546b593
JH
1827 } else if (!strcmp(arg, "--simplify-merges")) {
1828 revs->simplify_merges = 1;
a52f0071 1829 revs->topo_order = 1;
6546b593
JH
1830 revs->rewrite_parents = 1;
1831 revs->simplify_history = 0;
1832 revs->limited = 1;
78892e32
LT
1833 } else if (!strcmp(arg, "--simplify-by-decoration")) {
1834 revs->simplify_merges = 1;
a52f0071 1835 revs->topo_order = 1;
78892e32
LT
1836 revs->rewrite_parents = 1;
1837 revs->simplify_history = 0;
1838 revs->simplify_by_decoration = 1;
1839 revs->limited = 1;
1840 revs->prune = 1;
65516f58 1841 load_ref_decorations(NULL, DECORATE_SHORT_REFS);
02e54220 1842 } else if (!strcmp(arg, "--date-order")) {
08f704f2 1843 revs->sort_order = REV_SORT_BY_COMMIT_DATE;
02e54220 1844 revs->topo_order = 1;
81c6b38b
JH
1845 } else if (!strcmp(arg, "--author-date-order")) {
1846 revs->sort_order = REV_SORT_BY_AUTHOR_DATE;
02e54220 1847 revs->topo_order = 1;
dffc651e
SG
1848 } else if (!strcmp(arg, "--early-output")) {
1849 revs->early_output = 100;
1850 revs->topo_order = 1;
1851 } else if (skip_prefix(arg, "--early-output=", &optarg)) {
1852 if (strtoul_ui(optarg, 10, &revs->early_output) < 0)
1853 die("'%s': not a non-negative integer", optarg);
1854 revs->topo_order = 1;
02e54220
PH
1855 } else if (!strcmp(arg, "--parents")) {
1856 revs->rewrite_parents = 1;
1857 revs->print_parents = 1;
1858 } else if (!strcmp(arg, "--dense")) {
1859 revs->dense = 1;
1860 } else if (!strcmp(arg, "--sparse")) {
1861 revs->dense = 0;
ce5b6f9b
SB
1862 } else if (!strcmp(arg, "--in-commit-order")) {
1863 revs->tree_blobs_in_commit_order = 1;
02e54220
PH
1864 } else if (!strcmp(arg, "--remove-empty")) {
1865 revs->remove_empty_trees = 1;
b8e8db28 1866 } else if (!strcmp(arg, "--merges")) {
ad5aeede 1867 revs->min_parents = 2;
02e54220 1868 } else if (!strcmp(arg, "--no-merges")) {
ad5aeede 1869 revs->max_parents = 1;
479b3d97
SG
1870 } else if (skip_prefix(arg, "--min-parents=", &optarg)) {
1871 revs->min_parents = atoi(optarg);
9ada7aee 1872 } else if (!strcmp(arg, "--no-min-parents")) {
ad5aeede 1873 revs->min_parents = 0;
479b3d97
SG
1874 } else if (skip_prefix(arg, "--max-parents=", &optarg)) {
1875 revs->max_parents = atoi(optarg);
9ada7aee 1876 } else if (!strcmp(arg, "--no-max-parents")) {
ad5aeede 1877 revs->max_parents = -1;
02e54220
PH
1878 } else if (!strcmp(arg, "--boundary")) {
1879 revs->boundary = 1;
1880 } else if (!strcmp(arg, "--left-right")) {
1881 revs->left_right = 1;
60adf7d7 1882 } else if (!strcmp(arg, "--left-only")) {
24852d91 1883 if (revs->right_only)
94f605ec
MG
1884 die("--left-only is incompatible with --right-only"
1885 " or --cherry");
60adf7d7
MG
1886 revs->left_only = 1;
1887 } else if (!strcmp(arg, "--right-only")) {
24852d91
JH
1888 if (revs->left_only)
1889 die("--right-only is incompatible with --left-only");
60adf7d7 1890 revs->right_only = 1;
94f605ec
MG
1891 } else if (!strcmp(arg, "--cherry")) {
1892 if (revs->left_only)
1893 die("--cherry is incompatible with --left-only");
1894 revs->cherry_mark = 1;
1895 revs->right_only = 1;
ad5aeede 1896 revs->max_parents = 1;
94f605ec 1897 revs->limited = 1;
f69c5018
TR
1898 } else if (!strcmp(arg, "--count")) {
1899 revs->count = 1;
adbbb31e
MG
1900 } else if (!strcmp(arg, "--cherry-mark")) {
1901 if (revs->cherry_pick)
1902 die("--cherry-mark is incompatible with --cherry-pick");
1903 revs->cherry_mark = 1;
1904 revs->limited = 1; /* needs limit_list() */
02e54220 1905 } else if (!strcmp(arg, "--cherry-pick")) {
adbbb31e
MG
1906 if (revs->cherry_mark)
1907 die("--cherry-pick is incompatible with --cherry-mark");
02e54220
PH
1908 revs->cherry_pick = 1;
1909 revs->limited = 1;
1910 } else if (!strcmp(arg, "--objects")) {
1911 revs->tag_objects = 1;
1912 revs->tree_objects = 1;
1913 revs->blob_objects = 1;
1914 } else if (!strcmp(arg, "--objects-edge")) {
1915 revs->tag_objects = 1;
1916 revs->tree_objects = 1;
1917 revs->blob_objects = 1;
1918 revs->edge_hint = 1;
1684c1b2 1919 } else if (!strcmp(arg, "--objects-edge-aggressive")) {
1920 revs->tag_objects = 1;
1921 revs->tree_objects = 1;
1922 revs->blob_objects = 1;
1923 revs->edge_hint = 1;
1924 revs->edge_hint_aggressive = 1;
5a48d240
JH
1925 } else if (!strcmp(arg, "--verify-objects")) {
1926 revs->tag_objects = 1;
1927 revs->tree_objects = 1;
1928 revs->blob_objects = 1;
1929 revs->verify_objects = 1;
02e54220
PH
1930 } else if (!strcmp(arg, "--unpacked")) {
1931 revs->unpacked = 1;
59556548 1932 } else if (starts_with(arg, "--unpacked=")) {
03a9683d 1933 die("--unpacked=<packfile> no longer supported.");
02e54220
PH
1934 } else if (!strcmp(arg, "-r")) {
1935 revs->diff = 1;
0d1e0e78 1936 revs->diffopt.flags.recursive = 1;
02e54220
PH
1937 } else if (!strcmp(arg, "-t")) {
1938 revs->diff = 1;
0d1e0e78
BW
1939 revs->diffopt.flags.recursive = 1;
1940 revs->diffopt.flags.tree_in_recursive = 1;
02e54220
PH
1941 } else if (!strcmp(arg, "-m")) {
1942 revs->ignore_merges = 0;
1943 } else if (!strcmp(arg, "-c")) {
1944 revs->diff = 1;
1945 revs->dense_combined_merges = 0;
1946 revs->combine_merges = 1;
1947 } else if (!strcmp(arg, "--cc")) {
1948 revs->diff = 1;
1949 revs->dense_combined_merges = 1;
1950 revs->combine_merges = 1;
1951 } else if (!strcmp(arg, "-v")) {
1952 revs->verbose_header = 1;
1953 } else if (!strcmp(arg, "--pretty")) {
1954 revs->verbose_header = 1;
66b2ed09 1955 revs->pretty_given = 1;
ae18165f 1956 get_commit_format(NULL, revs);
479b3d97
SG
1957 } else if (skip_prefix(arg, "--pretty=", &optarg) ||
1958 skip_prefix(arg, "--format=", &optarg)) {
7d7b86f7
MM
1959 /*
1960 * Detached form ("--pretty X" as opposed to "--pretty=X")
1961 * not allowed, since the argument is optional.
1962 */
02e54220 1963 revs->verbose_header = 1;
66b2ed09 1964 revs->pretty_given = 1;
479b3d97 1965 get_commit_format(optarg, revs);
7cc13c71 1966 } else if (!strcmp(arg, "--expand-tabs")) {
fe37a9c5 1967 revs->expand_tabs_in_log = 8;
0893eec8
JH
1968 } else if (!strcmp(arg, "--no-expand-tabs")) {
1969 revs->expand_tabs_in_log = 0;
fe37a9c5
JH
1970 } else if (skip_prefix(arg, "--expand-tabs=", &arg)) {
1971 int val;
1972 if (strtol_i(arg, 10, &val) < 0 || val < 0)
1973 die("'%s': not a non-negative integer", arg);
1974 revs->expand_tabs_in_log = val;
7249e912 1975 } else if (!strcmp(arg, "--show-notes") || !strcmp(arg, "--notes")) {
66b2ed09
JH
1976 revs->show_notes = 1;
1977 revs->show_notes_given = 1;
3a03cf6b 1978 revs->notes_opt.use_default_notes = 1;
0c37f1fc
JH
1979 } else if (!strcmp(arg, "--show-signature")) {
1980 revs->show_signature = 1;
aa379999
MJ
1981 } else if (!strcmp(arg, "--no-show-signature")) {
1982 revs->show_signature = 0;
479b3d97
SG
1983 } else if (!strcmp(arg, "--show-linear-break")) {
1984 revs->break_bar = " ..........";
1b32dece
NTND
1985 revs->track_linear = 1;
1986 revs->track_first_time = 1;
479b3d97
SG
1987 } else if (skip_prefix(arg, "--show-linear-break=", &optarg)) {
1988 revs->break_bar = xstrdup(optarg);
1b32dece
NTND
1989 revs->track_linear = 1;
1990 revs->track_first_time = 1;
479b3d97
SG
1991 } else if (skip_prefix(arg, "--show-notes=", &optarg) ||
1992 skip_prefix(arg, "--notes=", &optarg)) {
894a9d33
TR
1993 struct strbuf buf = STRBUF_INIT;
1994 revs->show_notes = 1;
1995 revs->show_notes_given = 1;
479b3d97
SG
1996 if (starts_with(arg, "--show-notes=") &&
1997 revs->notes_opt.use_default_notes < 0)
1998 revs->notes_opt.use_default_notes = 1;
1999 strbuf_addstr(&buf, optarg);
c063f0a9 2000 expand_notes_ref(&buf);
304cc11c 2001 string_list_append(&revs->notes_opt.extra_notes_refs,
1d2f80fa 2002 strbuf_detach(&buf, NULL));
66b2ed09
JH
2003 } else if (!strcmp(arg, "--no-notes")) {
2004 revs->show_notes = 0;
2005 revs->show_notes_given = 1;
92e0d425
JK
2006 revs->notes_opt.use_default_notes = -1;
2007 /* we have been strdup'ing ourselves, so trick
2008 * string_list into free()ing strings */
2009 revs->notes_opt.extra_notes_refs.strdup_strings = 1;
2010 string_list_clear(&revs->notes_opt.extra_notes_refs, 0);
2011 revs->notes_opt.extra_notes_refs.strdup_strings = 0;
894a9d33
TR
2012 } else if (!strcmp(arg, "--standard-notes")) {
2013 revs->show_notes_given = 1;
3a03cf6b 2014 revs->notes_opt.use_default_notes = 1;
894a9d33 2015 } else if (!strcmp(arg, "--no-standard-notes")) {
3a03cf6b 2016 revs->notes_opt.use_default_notes = 0;
de84accc
NS
2017 } else if (!strcmp(arg, "--oneline")) {
2018 revs->verbose_header = 1;
2019 get_commit_format("oneline", revs);
7dccadf3 2020 revs->pretty_given = 1;
de84accc 2021 revs->abbrev_commit = 1;
02e54220
PH
2022 } else if (!strcmp(arg, "--graph")) {
2023 revs->topo_order = 1;
2024 revs->rewrite_parents = 1;
2025 revs->graph = graph_init(revs);
2026 } else if (!strcmp(arg, "--root")) {
2027 revs->show_root_diff = 1;
2028 } else if (!strcmp(arg, "--no-commit-id")) {
2029 revs->no_commit_id = 1;
2030 } else if (!strcmp(arg, "--always")) {
2031 revs->always_show_header = 1;
2032 } else if (!strcmp(arg, "--no-abbrev")) {
2033 revs->abbrev = 0;
2034 } else if (!strcmp(arg, "--abbrev")) {
2035 revs->abbrev = DEFAULT_ABBREV;
479b3d97
SG
2036 } else if (skip_prefix(arg, "--abbrev=", &optarg)) {
2037 revs->abbrev = strtoul(optarg, NULL, 10);
02e54220
PH
2038 if (revs->abbrev < MINIMUM_ABBREV)
2039 revs->abbrev = MINIMUM_ABBREV;
2040 else if (revs->abbrev > 40)
2041 revs->abbrev = 40;
2042 } else if (!strcmp(arg, "--abbrev-commit")) {
2043 revs->abbrev_commit = 1;
0c47695a
JS
2044 revs->abbrev_commit_given = 1;
2045 } else if (!strcmp(arg, "--no-abbrev-commit")) {
2046 revs->abbrev_commit = 0;
02e54220
PH
2047 } else if (!strcmp(arg, "--full-diff")) {
2048 revs->diff = 1;
2049 revs->full_diff = 1;
2050 } else if (!strcmp(arg, "--full-history")) {
2051 revs->simplify_history = 0;
2052 } else if (!strcmp(arg, "--relative-date")) {
a5481a6c 2053 revs->date_mode.type = DATE_RELATIVE;
f4ea32f0 2054 revs->date_mode_explicit = 1;
7d7b86f7 2055 } else if ((argcount = parse_long_opt("date", argv, &optarg))) {
a5481a6c 2056 parse_date_format(optarg, &revs->date_mode);
f4ea32f0 2057 revs->date_mode_explicit = 1;
7d7b86f7 2058 return argcount;
02e54220
PH
2059 } else if (!strcmp(arg, "--log-size")) {
2060 revs->show_log_size = 1;
2061 }
2062 /*
2063 * Grepping the commit log
2064 */
7d7b86f7
MM
2065 else if ((argcount = parse_long_opt("author", argv, &optarg))) {
2066 add_header_grep(revs, GREP_HEADER_AUTHOR, optarg);
2067 return argcount;
2068 } else if ((argcount = parse_long_opt("committer", argv, &optarg))) {
2069 add_header_grep(revs, GREP_HEADER_COMMITTER, optarg);
2070 return argcount;
72fd13f7
NTND
2071 } else if ((argcount = parse_long_opt("grep-reflog", argv, &optarg))) {
2072 add_header_grep(revs, GREP_HEADER_REFLOG, optarg);
2073 return argcount;
7d7b86f7
MM
2074 } else if ((argcount = parse_long_opt("grep", argv, &optarg))) {
2075 add_message_grep(revs, optarg);
2076 return argcount;
17bf35a3
JH
2077 } else if (!strcmp(arg, "--grep-debug")) {
2078 revs->grep_filter.debug = 1;
727b6fc3 2079 } else if (!strcmp(arg, "--basic-regexp")) {
8465541e 2080 revs->grep_filter.pattern_type_option = GREP_PATTERN_TYPE_BRE;
02e54220 2081 } else if (!strcmp(arg, "--extended-regexp") || !strcmp(arg, "-E")) {
8465541e 2082 revs->grep_filter.pattern_type_option = GREP_PATTERN_TYPE_ERE;
02e54220 2083 } else if (!strcmp(arg, "--regexp-ignore-case") || !strcmp(arg, "-i")) {
9e3cbc59 2084 revs->grep_filter.ignore_case = 1;
c1ddc461 2085 revs->diffopt.pickaxe_opts |= DIFF_PICKAXE_IGNORE_CASE;
02e54220 2086 } else if (!strcmp(arg, "--fixed-strings") || !strcmp(arg, "-F")) {
8465541e 2087 revs->grep_filter.pattern_type_option = GREP_PATTERN_TYPE_FIXED;
7531a2dd 2088 } else if (!strcmp(arg, "--perl-regexp") || !strcmp(arg, "-P")) {
8465541e 2089 revs->grep_filter.pattern_type_option = GREP_PATTERN_TYPE_PCRE;
02e54220 2090 } else if (!strcmp(arg, "--all-match")) {
0843acfd 2091 revs->grep_filter.all_match = 1;
22dfa8a2
CJ
2092 } else if (!strcmp(arg, "--invert-grep")) {
2093 revs->invert_grep = 1;
7d7b86f7
MM
2094 } else if ((argcount = parse_long_opt("encoding", argv, &optarg))) {
2095 if (strcmp(optarg, "none"))
2096 git_log_output_encoding = xstrdup(optarg);
02e54220
PH
2097 else
2098 git_log_output_encoding = "";
7d7b86f7 2099 return argcount;
02e54220
PH
2100 } else if (!strcmp(arg, "--reverse")) {
2101 revs->reverse ^= 1;
2102 } else if (!strcmp(arg, "--children")) {
2103 revs->children.name = "children";
2104 revs->limited = 1;
cc243c3c
JH
2105 } else if (!strcmp(arg, "--ignore-missing")) {
2106 revs->ignore_missing = 1;
df11e196
JT
2107 } else if (!strcmp(arg, "--exclude-promisor-objects")) {
2108 if (fetch_if_missing)
2109 die("BUG: exclude_promisor_objects can only be used when fetch_if_missing is 0");
2110 revs->exclude_promisor_objects = 1;
02e54220 2111 } else {
a97262c6 2112 int opts = diff_opt_parse(&revs->diffopt, argv, argc, revs->prefix);
02e54220
PH
2113 if (!opts)
2114 unkv[(*unkc)++] = arg;
2115 return opts;
2116 }
1b32dece
NTND
2117 if (revs->graph && revs->track_linear)
2118 die("--show-linear-break and --graph are incompatible");
02e54220
PH
2119
2120 return 1;
2121}
2122
6b61ec05
PH
2123void parse_revision_opt(struct rev_info *revs, struct parse_opt_ctx_t *ctx,
2124 const struct option *options,
2125 const char * const usagestr[])
2126{
2127 int n = handle_revision_opt(revs, ctx->argc, ctx->argv,
2128 &ctx->cpidx, ctx->out);
2129 if (n <= 0) {
2130 error("unknown option `%s'", ctx->argv[0]);
2131 usage_with_options(usagestr, options);
2132 }
2133 ctx->argv += n;
2134 ctx->argc -= n;
2135}
2136
073cf63c
NTND
2137static int for_each_bisect_ref(struct ref_store *refs, each_ref_fn fn,
2138 void *cb_data, const char *term)
2139{
cb46d630
AD
2140 struct strbuf bisect_refs = STRBUF_INIT;
2141 int status;
2142 strbuf_addf(&bisect_refs, "refs/bisect/%s", term);
073cf63c 2143 status = refs_for_each_fullref_in(refs, bisect_refs.buf, fn, cb_data, 0);
cb46d630
AD
2144 strbuf_release(&bisect_refs);
2145 return status;
2146}
2147
073cf63c 2148static int for_each_bad_bisect_ref(struct ref_store *refs, each_ref_fn fn, void *cb_data)
ad3f9a71 2149{
073cf63c 2150 return for_each_bisect_ref(refs, fn, cb_data, term_bad);
ad3f9a71
LT
2151}
2152
073cf63c 2153static int for_each_good_bisect_ref(struct ref_store *refs, each_ref_fn fn, void *cb_data)
ad3f9a71 2154{
073cf63c 2155 return for_each_bisect_ref(refs, fn, cb_data, term_good);
ad3f9a71
LT
2156}
2157
f6aca0dc
JN
2158static int handle_revision_pseudo_opt(const char *submodule,
2159 struct rev_info *revs,
2160 int argc, const char **argv, int *flags)
2161{
2162 const char *arg = argv[0];
2163 const char *optarg;
073cf63c 2164 struct ref_store *refs;
f6aca0dc
JN
2165 int argcount;
2166
073cf63c 2167 if (submodule) {
d0c39a49
NTND
2168 /*
2169 * We need some something like get_submodule_worktrees()
2170 * before we can go through all worktrees of a submodule,
2171 * .e.g with adding all HEADs from --all, which is not
2172 * supported right now, so stick to single worktree.
2173 */
2174 if (!revs->single_worktree)
2175 die("BUG: --single-worktree cannot be used together with submodule");
073cf63c
NTND
2176 refs = get_submodule_ref_store(submodule);
2177 } else
2178 refs = get_main_ref_store();
2179
0fc63ec4
JN
2180 /*
2181 * NOTE!
2182 *
2183 * Commands like "git shortlog" will not accept the options below
2184 * unless parse_revision_opt queues them (as opposed to erroring
2185 * out).
2186 *
2187 * When implementing your new pseudo-option, remember to
2188 * register it in the list at the top of handle_revision_opt.
2189 */
f6aca0dc 2190 if (!strcmp(arg, "--all")) {
073cf63c
NTND
2191 handle_refs(refs, revs, *flags, refs_for_each_ref);
2192 handle_refs(refs, revs, *flags, refs_head_ref);
d0c39a49
NTND
2193 if (!revs->single_worktree) {
2194 struct all_refs_cb cb;
2195
2196 init_all_refs_cb(&cb, revs, *flags);
2197 other_head_refs(handle_one_ref, &cb);
2198 }
ff32d342 2199 clear_ref_exclusion(&revs->ref_excludes);
f6aca0dc 2200 } else if (!strcmp(arg, "--branches")) {
073cf63c 2201 handle_refs(refs, revs, *flags, refs_for_each_branch_ref);
ff32d342 2202 clear_ref_exclusion(&revs->ref_excludes);
f6aca0dc 2203 } else if (!strcmp(arg, "--bisect")) {
cb46d630 2204 read_bisect_terms(&term_bad, &term_good);
073cf63c
NTND
2205 handle_refs(refs, revs, *flags, for_each_bad_bisect_ref);
2206 handle_refs(refs, revs, *flags ^ (UNINTERESTING | BOTTOM),
2207 for_each_good_bisect_ref);
f6aca0dc
JN
2208 revs->bisect = 1;
2209 } else if (!strcmp(arg, "--tags")) {
073cf63c 2210 handle_refs(refs, revs, *flags, refs_for_each_tag_ref);
ff32d342 2211 clear_ref_exclusion(&revs->ref_excludes);
f6aca0dc 2212 } else if (!strcmp(arg, "--remotes")) {
073cf63c 2213 handle_refs(refs, revs, *flags, refs_for_each_remote_ref);
ff32d342 2214 clear_ref_exclusion(&revs->ref_excludes);
f6aca0dc
JN
2215 } else if ((argcount = parse_long_opt("glob", argv, &optarg))) {
2216 struct all_refs_cb cb;
2217 init_all_refs_cb(&cb, revs, *flags);
a217dcbd 2218 for_each_glob_ref(handle_one_ref, optarg, &cb);
ff32d342 2219 clear_ref_exclusion(&revs->ref_excludes);
e7b432c5
JH
2220 return argcount;
2221 } else if ((argcount = parse_long_opt("exclude", argv, &optarg))) {
ff32d342 2222 add_ref_exclusion(&revs->ref_excludes, optarg);
f6aca0dc 2223 return argcount;
8b1d9136 2224 } else if (skip_prefix(arg, "--branches=", &optarg)) {
f6aca0dc
JN
2225 struct all_refs_cb cb;
2226 init_all_refs_cb(&cb, revs, *flags);
8b1d9136 2227 for_each_glob_ref_in(handle_one_ref, optarg, "refs/heads/", &cb);
ff32d342 2228 clear_ref_exclusion(&revs->ref_excludes);
8b1d9136 2229 } else if (skip_prefix(arg, "--tags=", &optarg)) {
f6aca0dc
JN
2230 struct all_refs_cb cb;
2231 init_all_refs_cb(&cb, revs, *flags);
8b1d9136 2232 for_each_glob_ref_in(handle_one_ref, optarg, "refs/tags/", &cb);
ff32d342 2233 clear_ref_exclusion(&revs->ref_excludes);
8b1d9136 2234 } else if (skip_prefix(arg, "--remotes=", &optarg)) {
f6aca0dc
JN
2235 struct all_refs_cb cb;
2236 init_all_refs_cb(&cb, revs, *flags);
8b1d9136 2237 for_each_glob_ref_in(handle_one_ref, optarg, "refs/remotes/", &cb);
ff32d342 2238 clear_ref_exclusion(&revs->ref_excludes);
f6aca0dc 2239 } else if (!strcmp(arg, "--reflog")) {
718ccc97 2240 add_reflogs_to_pending(revs, *flags);
4fe10219
JK
2241 } else if (!strcmp(arg, "--indexed-objects")) {
2242 add_index_objects_to_pending(revs, *flags);
f6aca0dc 2243 } else if (!strcmp(arg, "--not")) {
7f34a46f 2244 *flags ^= UNINTERESTING | BOTTOM;
f6aca0dc 2245 } else if (!strcmp(arg, "--no-walk")) {
ca92e59e 2246 revs->no_walk = REVISION_WALK_NO_WALK_SORTED;
8b1d9136 2247 } else if (skip_prefix(arg, "--no-walk=", &optarg)) {
ca92e59e
MZ
2248 /*
2249 * Detached form ("--no-walk X" as opposed to "--no-walk=X")
2250 * not allowed, since the argument is optional.
2251 */
8b1d9136 2252 if (!strcmp(optarg, "sorted"))
ca92e59e 2253 revs->no_walk = REVISION_WALK_NO_WALK_SORTED;
8b1d9136 2254 else if (!strcmp(optarg, "unsorted"))
ca92e59e
MZ
2255 revs->no_walk = REVISION_WALK_NO_WALK_UNSORTED;
2256 else
2257 return error("invalid argument to --no-walk");
f6aca0dc
JN
2258 } else if (!strcmp(arg, "--do-walk")) {
2259 revs->no_walk = 0;
32619f99
NTND
2260 } else if (!strcmp(arg, "--single-worktree")) {
2261 revs->single_worktree = 1;
f6aca0dc
JN
2262 } else {
2263 return 0;
2264 }
2265
2266 return 1;
2267}
2268
ce113604
JK
2269static void NORETURN diagnose_missing_default(const char *def)
2270{
ce113604
JK
2271 int flags;
2272 const char *refname;
2273
744c040b 2274 refname = resolve_ref_unsafe(def, 0, NULL, &flags);
ce113604
JK
2275 if (!refname || !(flags & REF_ISSYMREF) || (flags & REF_ISBROKEN))
2276 die(_("your current branch appears to be broken"));
2277
2278 skip_prefix(refname, "refs/heads/", &refname);
2279 die(_("your current branch '%s' does not have any commits yet"),
2280 refname);
2281}
2282
ae563542
LT
2283/*
2284 * Parse revision information, filling in the "rev_info" structure,
2285 * and removing the used arguments from the argument list.
2286 *
765ac8ec
LT
2287 * Returns the number of arguments left that weren't recognized
2288 * (which are also moved to the head of the argument list)
ae563542 2289 */
32962c9b 2290int setup_revisions(int argc, const char **argv, struct rev_info *revs, struct setup_revision_opt *opt)
ae563542 2291{
8e676e8b 2292 int i, flags, left, seen_dashdash, read_from_stdin, got_rev_arg = 0, revarg_opt;
7fa3c2ad 2293 struct argv_array prune_data = ARGV_ARRAY_INIT;
9ef6aeb0
HV
2294 const char *submodule = NULL;
2295
2296 if (opt)
2297 submodule = opt->submodule;
ae563542 2298
ae563542 2299 /* First, search for "--" */
6d5b93f2 2300 if (opt && opt->assume_dashdash) {
ae563542 2301 seen_dashdash = 1;
6d5b93f2
CB
2302 } else {
2303 seen_dashdash = 0;
2304 for (i = 1; i < argc; i++) {
2305 const char *arg = argv[i];
2306 if (strcmp(arg, "--"))
2307 continue;
2308 argv[i] = NULL;
2309 argc = i;
2310 if (argv[i + 1])
7fa3c2ad 2311 argv_array_pushv(&prune_data, argv + i + 1);
6d5b93f2
CB
2312 seen_dashdash = 1;
2313 break;
2314 }
ae563542
LT
2315 }
2316
02e54220
PH
2317 /* Second, deal with arguments and options */
2318 flags = 0;
d5f6b1d7
JH
2319 revarg_opt = opt ? opt->revarg_opt : 0;
2320 if (seen_dashdash)
2321 revarg_opt |= REVARG_CANNOT_BE_FILENAME;
8b3dce56 2322 read_from_stdin = 0;
02e54220 2323 for (left = i = 1; i < argc; i++) {
ae563542 2324 const char *arg = argv[i];
ae563542 2325 if (*arg == '-') {
cd2bdc53 2326 int opts;
02e54220 2327
f6aca0dc
JN
2328 opts = handle_revision_pseudo_opt(submodule,
2329 revs, argc - i, argv + i,
2330 &flags);
2331 if (opts > 0) {
2332 i += opts - 1;
8e64006e
JS
2333 continue;
2334 }
f6aca0dc 2335
8b3dce56
JH
2336 if (!strcmp(arg, "--stdin")) {
2337 if (revs->disable_stdin) {
2338 argv[left++] = arg;
2339 continue;
2340 }
2341 if (read_from_stdin++)
2342 die("--stdin given twice?");
60da8b15 2343 read_revisions_from_stdin(revs, &prune_data);
8b3dce56
JH
2344 continue;
2345 }
2d10c555 2346
02e54220 2347 opts = handle_revision_opt(revs, argc - i, argv + i, &left, argv);
cd2bdc53 2348 if (opts > 0) {
cd2bdc53
LT
2349 i += opts - 1;
2350 continue;
2351 }
02e54220
PH
2352 if (opts < 0)
2353 exit(128);
ae563542
LT
2354 continue;
2355 }
ae563542 2356
8e676e8b
JH
2357
2358 if (handle_revision_arg(arg, revs, flags, revarg_opt)) {
5d6f0935
JH
2359 int j;
2360 if (seen_dashdash || *arg == '^')
ae563542
LT
2361 die("bad revision '%s'", arg);
2362
ea92f41f
JH
2363 /* If we didn't have a "--":
2364 * (1) all filenames must exist;
2365 * (2) all rev-args must not be interpretable
2366 * as a valid filename.
2367 * but the latter we have checked in the main loop.
2368 */
e23d0b4a 2369 for (j = i; j < argc; j++)
023e37c3 2370 verify_filename(revs->prefix, argv[j], j == i);
e23d0b4a 2371
7fa3c2ad 2372 argv_array_pushv(&prune_data, argv + i);
ae563542
LT
2373 break;
2374 }
8fcaca3f
DO
2375 else
2376 got_rev_arg = 1;
ae563542 2377 }
5d6f0935 2378
7fa3c2ad 2379 if (prune_data.argc) {
93e7d672
JH
2380 /*
2381 * If we need to introduce the magic "a lone ':' means no
2382 * pathspec whatsoever", here is the place to do so.
2383 *
2384 * if (prune_data.nr == 1 && !strcmp(prune_data[0], ":")) {
2385 * prune_data.nr = 0;
2386 * prune_data.alloc = 0;
2387 * free(prune_data.path);
2388 * prune_data.path = NULL;
2389 * } else {
2390 * terminate prune_data.alloc with NULL and
2391 * call init_pathspec() to set revs->prune_data here.
2392 * }
2393 */
0fdc2ae5 2394 parse_pathspec(&revs->prune_data, 0, 0,
7fa3c2ad 2395 revs->prefix, prune_data.argv);
4da5af31 2396 }
7fa3c2ad 2397 argv_array_clear(&prune_data);
5486ef0e 2398
02e54220 2399 if (revs->def == NULL)
32962c9b 2400 revs->def = opt ? opt->def : NULL;
b4490059
JH
2401 if (opt && opt->tweak)
2402 opt->tweak(revs, opt);
02e54220 2403 if (revs->show_merge)
ae3e5e1e 2404 prepare_show_merge(revs);
5d34d1ac 2405 if (revs->def && !revs->pending.nr && !revs->rev_input_given && !got_rev_arg) {
654b9a90 2406 struct object_id oid;
cd2bdc53 2407 struct object *object;
249c8f4a 2408 struct object_context oc;
e82caf38 2409 if (get_oid_with_context(revs->def, 0, &oid, &oc))
ce113604 2410 diagnose_missing_default(revs->def);
654b9a90 2411 object = get_reference(revs, revs->def, &oid, 0);
249c8f4a 2412 add_pending_object_with_mode(revs, object, revs->def, oc.mode);
ae563542 2413 }
8efdc326 2414
b7bb760d
LT
2415 /* Did the user ask for any diff output? Run the diff! */
2416 if (revs->diffopt.output_format & ~DIFF_FORMAT_NO_OUTPUT)
2417 revs->diff = 1;
2418
0faf2da7 2419 /* Pickaxe, diff-filter and rename following need diffs */
cf63051a 2420 if ((revs->diffopt.pickaxe_opts & DIFF_PICKAXE_KINDS_MASK) ||
0faf2da7 2421 revs->diffopt.filter ||
0d1e0e78 2422 revs->diffopt.flags.follow_renames)
b7bb760d
LT
2423 revs->diff = 1;
2424
15af58c1
SB
2425 if (revs->diffopt.objfind)
2426 revs->simplify_history = 0;
2427
9dad9d2e 2428 if (revs->topo_order)
53069686
JH
2429 revs->limited = 1;
2430
afe069d1 2431 if (revs->prune_data.nr) {
bd1928df 2432 copy_pathspec(&revs->pruning.pathspec, &revs->prune_data);
750f7b66 2433 /* Can't prune commits with rename following: the paths change.. */
0d1e0e78 2434 if (!revs->diffopt.flags.follow_renames)
53b2c823 2435 revs->prune = 1;
cd2bdc53 2436 if (!revs->full_diff)
bd1928df
NTND
2437 copy_pathspec(&revs->diffopt.pathspec,
2438 &revs->prune_data);
8efdc326 2439 }
b4490059 2440 if (revs->combine_merges)
cd2bdc53 2441 revs->ignore_merges = 0;
cd2bdc53 2442 revs->diffopt.abbrev = revs->abbrev;
12da1d1f
TR
2443
2444 if (revs->line_level_traverse) {
2445 revs->limited = 1;
2446 revs->topo_order = 1;
2447 }
2448
28452655 2449 diff_setup_done(&revs->diffopt);
8efdc326 2450
918d4e1c
JH
2451 grep_commit_pattern_type(GREP_PATTERN_TYPE_UNSPECIFIED,
2452 &revs->grep_filter);
0843acfd 2453 compile_grep_patterns(&revs->grep_filter);
bd95fcd3 2454
d56651c0
SP
2455 if (revs->reverse && revs->reflog_info)
2456 die("cannot combine --reverse with --walk-reflogs");
82fd0f4a
JK
2457 if (revs->reflog_info && revs->limited)
2458 die("cannot combine --walk-reflogs with history-limiting options");
8bb65883 2459 if (revs->rewrite_parents && revs->children.name)
f35f5603 2460 die("cannot combine --parents and --children");
d56651c0 2461
7fefda5c
AS
2462 /*
2463 * Limitations on the graph functionality
2464 */
2465 if (revs->reverse && revs->graph)
2466 die("cannot combine --reverse with --graph");
2467
2468 if (revs->reflog_info && revs->graph)
2469 die("cannot combine --walk-reflogs with --graph");
695985f4
DJ
2470 if (revs->no_walk && revs->graph)
2471 die("cannot combine --no-walk with --graph");
baa6378f
JH
2472 if (!revs->reflog_info && revs->grep_filter.use_reflog_filter)
2473 die("cannot use --grep-reflog without --walk-reflogs");
7fefda5c 2474
f88851c6
KD
2475 if (revs->first_parent_only && revs->bisect)
2476 die(_("--first-parent is incompatible with --bisect"));
2477
0893eec8
JH
2478 if (revs->expand_tabs_in_log < 0)
2479 revs->expand_tabs_in_log = revs->expand_tabs_in_log_default;
2480
ae563542
LT
2481 return left;
2482}
a4a88b2b 2483
f35f5603
JH
2484static void add_child(struct rev_info *revs, struct commit *parent, struct commit *child)
2485{
2486 struct commit_list *l = xcalloc(1, sizeof(*l));
2487
2488 l->item = child;
2489 l->next = add_decoration(&revs->children, &parent->object, l);
2490}
2491
d0af663e 2492static int remove_duplicate_parents(struct rev_info *revs, struct commit *commit)
6546b593 2493{
d0af663e 2494 struct treesame_state *ts = lookup_decoration(&revs->treesame, &commit->object);
6546b593
JH
2495 struct commit_list **pp, *p;
2496 int surviving_parents;
2497
2498 /* Examine existing parents while marking ones we have seen... */
2499 pp = &commit->parents;
d0af663e 2500 surviving_parents = 0;
6546b593
JH
2501 while ((p = *pp) != NULL) {
2502 struct commit *parent = p->item;
2503 if (parent->object.flags & TMP_MARK) {
2504 *pp = p->next;
d0af663e
KB
2505 if (ts)
2506 compact_treesame(revs, commit, surviving_parents);
6546b593
JH
2507 continue;
2508 }
2509 parent->object.flags |= TMP_MARK;
d0af663e 2510 surviving_parents++;
6546b593
JH
2511 pp = &p->next;
2512 }
d0af663e 2513 /* clear the temporary mark */
6546b593
JH
2514 for (p = commit->parents; p; p = p->next) {
2515 p->item->object.flags &= ~TMP_MARK;
6546b593 2516 }
d0af663e 2517 /* no update_treesame() - removing duplicates can't affect TREESAME */
6546b593
JH
2518 return surviving_parents;
2519}
2520
faf0156b
JH
2521struct merge_simplify_state {
2522 struct commit *simplified;
2523};
2524
2525static struct merge_simplify_state *locate_simplify_state(struct rev_info *revs, struct commit *commit)
2526{
2527 struct merge_simplify_state *st;
2528
2529 st = lookup_decoration(&revs->merge_simplification, &commit->object);
2530 if (!st) {
2531 st = xcalloc(1, sizeof(*st));
2532 add_decoration(&revs->merge_simplification, &commit->object, st);
2533 }
2534 return st;
2535}
2536
d0af663e
KB
2537static int mark_redundant_parents(struct rev_info *revs, struct commit *commit)
2538{
2539 struct commit_list *h = reduce_heads(commit->parents);
2540 int i = 0, marked = 0;
2541 struct commit_list *po, *pn;
2542
2543 /* Want these for sanity-checking only */
2544 int orig_cnt = commit_list_count(commit->parents);
2545 int cnt = commit_list_count(h);
2546
2547 /*
2548 * Not ready to remove items yet, just mark them for now, based
2549 * on the output of reduce_heads(). reduce_heads outputs the reduced
2550 * set in its original order, so this isn't too hard.
2551 */
2552 po = commit->parents;
2553 pn = h;
2554 while (po) {
2555 if (pn && po->item == pn->item) {
2556 pn = pn->next;
2557 i++;
2558 } else {
2559 po->item->object.flags |= TMP_MARK;
2560 marked++;
2561 }
2562 po=po->next;
2563 }
2564
2565 if (i != cnt || cnt+marked != orig_cnt)
2566 die("mark_redundant_parents %d %d %d %d", orig_cnt, cnt, i, marked);
2567
2568 free_commit_list(h);
2569
2570 return marked;
2571}
2572
143f1eaf
KB
2573static int mark_treesame_root_parents(struct rev_info *revs, struct commit *commit)
2574{
2575 struct commit_list *p;
2576 int marked = 0;
2577
2578 for (p = commit->parents; p; p = p->next) {
2579 struct commit *parent = p->item;
2580 if (!parent->parents && (parent->object.flags & TREESAME)) {
2581 parent->object.flags |= TMP_MARK;
2582 marked++;
2583 }
2584 }
2585
2586 return marked;
2587}
2588
9c129eab
KB
2589/*
2590 * Awkward naming - this means one parent we are TREESAME to.
2591 * cf mark_treesame_root_parents: root parents that are TREESAME (to an
2592 * empty tree). Better name suggestions?
2593 */
2594static int leave_one_treesame_to_parent(struct rev_info *revs, struct commit *commit)
2595{
2596 struct treesame_state *ts = lookup_decoration(&revs->treesame, &commit->object);
2597 struct commit *unmarked = NULL, *marked = NULL;
2598 struct commit_list *p;
2599 unsigned n;
2600
2601 for (p = commit->parents, n = 0; p; p = p->next, n++) {
2602 if (ts->treesame[n]) {
2603 if (p->item->object.flags & TMP_MARK) {
2604 if (!marked)
2605 marked = p->item;
2606 } else {
2607 if (!unmarked) {
2608 unmarked = p->item;
2609 break;
2610 }
2611 }
2612 }
2613 }
2614
2615 /*
2616 * If we are TREESAME to a marked-for-deletion parent, but not to any
2617 * unmarked parents, unmark the first TREESAME parent. This is the
2618 * parent that the default simplify_history==1 scan would have followed,
2619 * and it doesn't make sense to omit that path when asking for a
2620 * simplified full history. Retaining it improves the chances of
2621 * understanding odd missed merges that took an old version of a file.
2622 *
2623 * Example:
2624 *
2625 * I--------*X A modified the file, but mainline merge X used
2626 * \ / "-s ours", so took the version from I. X is
2627 * `-*A--' TREESAME to I and !TREESAME to A.
2628 *
2629 * Default log from X would produce "I". Without this check,
2630 * --full-history --simplify-merges would produce "I-A-X", showing
2631 * the merge commit X and that it changed A, but not making clear that
2632 * it had just taken the I version. With this check, the topology above
2633 * is retained.
2634 *
2635 * Note that it is possible that the simplification chooses a different
2636 * TREESAME parent from the default, in which case this test doesn't
2637 * activate, and we _do_ drop the default parent. Example:
2638 *
2639 * I------X A modified the file, but it was reverted in B,
2640 * \ / meaning mainline merge X is TREESAME to both
2641 * *A-*B parents.
2642 *
2643 * Default log would produce "I" by following the first parent;
2644 * --full-history --simplify-merges will produce "I-A-B". But this is a
2645 * reasonable result - it presents a logical full history leading from
2646 * I to X, and X is not an important merge.
2647 */
2648 if (!unmarked && marked) {
2649 marked->object.flags &= ~TMP_MARK;
2650 return 1;
2651 }
2652
2653 return 0;
2654}
2655
d0af663e
KB
2656static int remove_marked_parents(struct rev_info *revs, struct commit *commit)
2657{
2658 struct commit_list **pp, *p;
2659 int nth_parent, removed = 0;
2660
2661 pp = &commit->parents;
2662 nth_parent = 0;
2663 while ((p = *pp) != NULL) {
2664 struct commit *parent = p->item;
2665 if (parent->object.flags & TMP_MARK) {
2666 parent->object.flags &= ~TMP_MARK;
2667 *pp = p->next;
2668 free(p);
2669 removed++;
2670 compact_treesame(revs, commit, nth_parent);
2671 continue;
2672 }
2673 pp = &p->next;
2674 nth_parent++;
2675 }
2676
2677 /* Removing parents can only increase TREESAMEness */
2678 if (removed && !(commit->object.flags & TREESAME))
2679 update_treesame(revs, commit);
2680
2681 return nth_parent;
2682}
2683
faf0156b 2684static struct commit_list **simplify_one(struct rev_info *revs, struct commit *commit, struct commit_list **tail)
6546b593
JH
2685{
2686 struct commit_list *p;
4d826608 2687 struct commit *parent;
faf0156b 2688 struct merge_simplify_state *st, *pst;
6546b593
JH
2689 int cnt;
2690
faf0156b
JH
2691 st = locate_simplify_state(revs, commit);
2692
6546b593 2693 /*
6546b593
JH
2694 * Have we handled this one?
2695 */
faf0156b 2696 if (st->simplified)
6546b593
JH
2697 return tail;
2698
2699 /*
2700 * An UNINTERESTING commit simplifies to itself, so does a
2701 * root commit. We do not rewrite parents of such commit
2702 * anyway.
2703 */
2704 if ((commit->object.flags & UNINTERESTING) || !commit->parents) {
faf0156b 2705 st->simplified = commit;
6546b593
JH
2706 return tail;
2707 }
2708
2709 /*
6e513ba3
JH
2710 * Do we know what commit all of our parents that matter
2711 * should be rewritten to? Otherwise we are not ready to
2712 * rewrite this one yet.
6546b593
JH
2713 */
2714 for (cnt = 0, p = commit->parents; p; p = p->next) {
faf0156b
JH
2715 pst = locate_simplify_state(revs, p->item);
2716 if (!pst->simplified) {
6546b593
JH
2717 tail = &commit_list_insert(p->item, tail)->next;
2718 cnt++;
2719 }
6e513ba3
JH
2720 if (revs->first_parent_only)
2721 break;
6546b593 2722 }
53030f8d
JH
2723 if (cnt) {
2724 tail = &commit_list_insert(commit, tail)->next;
6546b593 2725 return tail;
53030f8d 2726 }
6546b593
JH
2727
2728 /*
d0af663e
KB
2729 * Rewrite our list of parents. Note that this cannot
2730 * affect our TREESAME flags in any way - a commit is
2731 * always TREESAME to its simplification.
6546b593 2732 */
faf0156b
JH
2733 for (p = commit->parents; p; p = p->next) {
2734 pst = locate_simplify_state(revs, p->item);
2735 p->item = pst->simplified;
6e513ba3
JH
2736 if (revs->first_parent_only)
2737 break;
faf0156b 2738 }
4b7f53da 2739
0290bf12 2740 if (revs->first_parent_only)
6e513ba3 2741 cnt = 1;
0290bf12 2742 else
d0af663e 2743 cnt = remove_duplicate_parents(revs, commit);
6546b593
JH
2744
2745 /*
2746 * It is possible that we are a merge and one side branch
2747 * does not have any commit that touches the given paths;
d0af663e
KB
2748 * in such a case, the immediate parent from that branch
2749 * will be rewritten to be the merge base.
6546b593
JH
2750 *
2751 * o----X X: the commit we are looking at;
2752 * / / o: a commit that touches the paths;
2753 * ---o----'
2754 *
143f1eaf
KB
2755 * Further, a merge of an independent branch that doesn't
2756 * touch the path will reduce to a treesame root parent:
2757 *
2758 * ----o----X X: the commit we are looking at;
2759 * / o: a commit that touches the paths;
2760 * r r: a root commit not touching the paths
2761 *
2762 * Detect and simplify both cases.
6546b593
JH
2763 */
2764 if (1 < cnt) {
d0af663e 2765 int marked = mark_redundant_parents(revs, commit);
143f1eaf 2766 marked += mark_treesame_root_parents(revs, commit);
9c129eab
KB
2767 if (marked)
2768 marked -= leave_one_treesame_to_parent(revs, commit);
d0af663e
KB
2769 if (marked)
2770 cnt = remove_marked_parents(revs, commit);
6546b593
JH
2771 }
2772
2773 /*
2774 * A commit simplifies to itself if it is a root, if it is
2775 * UNINTERESTING, if it touches the given paths, or if it is a
4d826608 2776 * merge and its parents don't simplify to one relevant commit
6546b593
JH
2777 * (the first two cases are already handled at the beginning of
2778 * this function).
2779 *
4d826608
KB
2780 * Otherwise, it simplifies to what its sole relevant parent
2781 * simplifies to.
6546b593
JH
2782 */
2783 if (!cnt ||
2784 (commit->object.flags & UNINTERESTING) ||
2785 !(commit->object.flags & TREESAME) ||
4d826608 2786 (parent = one_relevant_parent(revs, commit->parents)) == NULL)
faf0156b
JH
2787 st->simplified = commit;
2788 else {
4d826608 2789 pst = locate_simplify_state(revs, parent);
faf0156b
JH
2790 st->simplified = pst->simplified;
2791 }
6546b593
JH
2792 return tail;
2793}
2794
2795static void simplify_merges(struct rev_info *revs)
2796{
ab9d75a8 2797 struct commit_list *list, *next;
6546b593 2798 struct commit_list *yet_to_do, **tail;
ab9d75a8 2799 struct commit *commit;
6546b593 2800
5eac739e
JH
2801 if (!revs->prune)
2802 return;
65347030 2803
6546b593
JH
2804 /* feed the list reversed */
2805 yet_to_do = NULL;
ab9d75a8
JH
2806 for (list = revs->commits; list; list = next) {
2807 commit = list->item;
2808 next = list->next;
2809 /*
2810 * Do not free(list) here yet; the original list
2811 * is used later in this function.
2812 */
2813 commit_list_insert(commit, &yet_to_do);
2814 }
6546b593
JH
2815 while (yet_to_do) {
2816 list = yet_to_do;
2817 yet_to_do = NULL;
2818 tail = &yet_to_do;
2819 while (list) {
e510ab89 2820 commit = pop_commit(&list);
faf0156b 2821 tail = simplify_one(revs, commit, tail);
6546b593
JH
2822 }
2823 }
2824
2825 /* clean up the result, removing the simplified ones */
2826 list = revs->commits;
2827 revs->commits = NULL;
2828 tail = &revs->commits;
2829 while (list) {
faf0156b 2830 struct merge_simplify_state *st;
ab9d75a8 2831
e510ab89 2832 commit = pop_commit(&list);
faf0156b
JH
2833 st = locate_simplify_state(revs, commit);
2834 if (st->simplified == commit)
6546b593
JH
2835 tail = &commit_list_insert(commit, tail)->next;
2836 }
6546b593
JH
2837}
2838
f35f5603
JH
2839static void set_children(struct rev_info *revs)
2840{
2841 struct commit_list *l;
2842 for (l = revs->commits; l; l = l->next) {
2843 struct commit *commit = l->item;
2844 struct commit_list *p;
2845
2846 for (p = commit->parents; p; p = p->next)
2847 add_child(revs, p->item, commit);
2848 }
2849}
2850
bcc0a3ea
HV
2851void reset_revision_walk(void)
2852{
2853 clear_object_flags(SEEN | ADDED | SHOWN);
2854}
2855
df11e196
JT
2856static int mark_uninteresting(const struct object_id *oid,
2857 struct packed_git *pack,
2858 uint32_t pos,
2859 void *unused)
2860{
2861 struct object *o = parse_object(oid);
2862 o->flags |= UNINTERESTING | SEEN;
2863 return 0;
2864}
2865
cc0e6c5a 2866int prepare_revision_walk(struct rev_info *revs)
a4a88b2b 2867{
1da1e07c
JK
2868 int i;
2869 struct object_array old_pending;
2e7da8e9 2870 struct commit_list **next = &revs->commits;
cd2bdc53 2871
1da1e07c 2872 memcpy(&old_pending, &revs->pending, sizeof(old_pending));
1f1e895f
LT
2873 revs->pending.nr = 0;
2874 revs->pending.alloc = 0;
2875 revs->pending.objects = NULL;
1da1e07c
JK
2876 for (i = 0; i < old_pending.nr; i++) {
2877 struct object_array_entry *e = old_pending.objects + i;
20739490 2878 struct commit *commit = handle_commit(revs, e);
cd2bdc53
LT
2879 if (commit) {
2880 if (!(commit->object.flags & SEEN)) {
2881 commit->object.flags |= SEEN;
2e7da8e9 2882 next = commit_list_append(commit, next);
cd2bdc53
LT
2883 }
2884 }
cd2bdc53 2885 }
f1230fb5 2886 object_array_clear(&old_pending);
cd2bdc53 2887
d0af663e 2888 /* Signal whether we need per-parent treesame decoration */
4d826608
KB
2889 if (revs->simplify_merges ||
2890 (revs->limited && limiting_can_increase_treesame(revs)))
d0af663e
KB
2891 revs->treesame.name = "treesame";
2892
df11e196
JT
2893 if (revs->exclude_promisor_objects) {
2894 for_each_packed_object(mark_uninteresting, NULL,
2895 FOR_EACH_OBJECT_PROMISOR_ONLY);
2896 }
2897
ca92e59e
MZ
2898 if (revs->no_walk != REVISION_WALK_NO_WALK_UNSORTED)
2899 commit_list_sort_by_date(&revs->commits);
ba1d4505 2900 if (revs->no_walk)
cc0e6c5a 2901 return 0;
a4a88b2b 2902 if (revs->limited)
cc0e6c5a
AR
2903 if (limit_list(revs) < 0)
2904 return -1;
a4a88b2b 2905 if (revs->topo_order)
08f704f2 2906 sort_in_topological_order(&revs->commits, revs->sort_order);
12da1d1f
TR
2907 if (revs->line_level_traverse)
2908 line_log_filter(revs);
6546b593
JH
2909 if (revs->simplify_merges)
2910 simplify_merges(revs);
f35f5603
JH
2911 if (revs->children.name)
2912 set_children(revs);
cc0e6c5a 2913 return 0;
a4a88b2b
LT
2914}
2915
cc0e6c5a 2916static enum rewrite_result rewrite_one(struct rev_info *revs, struct commit **pp)
765ac8ec 2917{
fce87ae5
AG
2918 struct commit_list *cache = NULL;
2919
765ac8ec
LT
2920 for (;;) {
2921 struct commit *p = *pp;
3381c790 2922 if (!revs->limited)
fce87ae5 2923 if (add_parents_to_list(revs, p, &revs->commits, &cache) < 0)
cc0e6c5a 2924 return rewrite_one_error;
7dc0fe3b
LT
2925 if (p->object.flags & UNINTERESTING)
2926 return rewrite_one_ok;
2927 if (!(p->object.flags & TREESAME))
cc0e6c5a 2928 return rewrite_one_ok;
765ac8ec 2929 if (!p->parents)
cc0e6c5a 2930 return rewrite_one_noparents;
4d826608
KB
2931 if ((p = one_relevant_parent(revs, p->parents)) == NULL)
2932 return rewrite_one_ok;
2933 *pp = p;
765ac8ec
LT
2934 }
2935}
2936
c7edcae0
BY
2937int rewrite_parents(struct rev_info *revs, struct commit *commit,
2938 rewrite_parent_fn_t rewrite_parent)
765ac8ec
LT
2939{
2940 struct commit_list **pp = &commit->parents;
2941 while (*pp) {
2942 struct commit_list *parent = *pp;
c7edcae0 2943 switch (rewrite_parent(revs, &parent->item)) {
cc0e6c5a
AR
2944 case rewrite_one_ok:
2945 break;
2946 case rewrite_one_noparents:
765ac8ec
LT
2947 *pp = parent->next;
2948 continue;
cc0e6c5a
AR
2949 case rewrite_one_error:
2950 return -1;
765ac8ec
LT
2951 }
2952 pp = &parent->next;
2953 }
d0af663e 2954 remove_duplicate_parents(revs, commit);
cc0e6c5a 2955 return 0;
765ac8ec
LT
2956}
2957
d72fbe81
AP
2958static int commit_rewrite_person(struct strbuf *buf, const char *what, struct string_list *mailmap)
2959{
2960 char *person, *endp;
2961 size_t len, namelen, maillen;
2962 const char *name;
2963 const char *mail;
2964 struct ident_split ident;
2965
2966 person = strstr(buf->buf, what);
2967 if (!person)
2968 return 0;
2969
2970 person += strlen(what);
2971 endp = strchr(person, '\n');
2972 if (!endp)
2973 return 0;
2974
2975 len = endp - person;
2976
2977 if (split_ident_line(&ident, person, len))
2978 return 0;
2979
2980 mail = ident.mail_begin;
2981 maillen = ident.mail_end - ident.mail_begin;
2982 name = ident.name_begin;
2983 namelen = ident.name_end - ident.name_begin;
2984
2985 if (map_user(mailmap, &mail, &maillen, &name, &namelen)) {
2986 struct strbuf namemail = STRBUF_INIT;
2987
2988 strbuf_addf(&namemail, "%.*s <%.*s>",
2989 (int)namelen, name, (int)maillen, mail);
2990
2991 strbuf_splice(buf, ident.name_begin - buf->buf,
2992 ident.mail_end - ident.name_begin + 1,
2993 namemail.buf, namemail.len);
2994
2995 strbuf_release(&namemail);
2996
2997 return 1;
2998 }
2999
3000 return 0;
3001}
3002
8ecae9b0
JH
3003static int commit_match(struct commit *commit, struct rev_info *opt)
3004{
72fd13f7 3005 int retval;
04deccda 3006 const char *encoding;
b000c59b 3007 const char *message;
72fd13f7 3008 struct strbuf buf = STRBUF_INIT;
04deccda 3009
80235ba7 3010 if (!opt->grep_filter.pattern_list && !opt->grep_filter.header_list)
8ecae9b0 3011 return 1;
baa6378f
JH
3012
3013 /* Prepend "fake" headers as needed */
3014 if (opt->grep_filter.use_reflog_filter) {
72fd13f7
NTND
3015 strbuf_addstr(&buf, "reflog ");
3016 get_reflog_message(&buf, opt->reflog_info);
3017 strbuf_addch(&buf, '\n');
72fd13f7 3018 }
baa6378f 3019
04deccda
JK
3020 /*
3021 * We grep in the user's output encoding, under the assumption that it
3022 * is the encoding they are most likely to write their grep pattern
3023 * for. In addition, it means we will match the "notes" encoding below,
3024 * so we will not end up with a buffer that has two different encodings
3025 * in it.
3026 */
3027 encoding = get_log_output_encoding();
5a10d236 3028 message = logmsg_reencode(commit, NULL, encoding);
04deccda 3029
baa6378f
JH
3030 /* Copy the commit to temporary if we are using "fake" headers */
3031 if (buf.len)
04deccda 3032 strbuf_addstr(&buf, message);
baa6378f 3033
df874fa8 3034 if (opt->grep_filter.header_list && opt->mailmap) {
d72fbe81 3035 if (!buf.len)
04deccda 3036 strbuf_addstr(&buf, message);
d72fbe81
AP
3037
3038 commit_rewrite_person(&buf, "\nauthor ", opt->mailmap);
3039 commit_rewrite_person(&buf, "\ncommitter ", opt->mailmap);
3040 }
3041
38cfe915
NTND
3042 /* Append "fake" message parts as needed */
3043 if (opt->show_notes) {
3044 if (!buf.len)
04deccda 3045 strbuf_addstr(&buf, message);
fb61e4d3 3046 format_display_notes(&commit->object.oid, &buf, encoding, 1);
38cfe915
NTND
3047 }
3048
b000c59b
JK
3049 /*
3050 * Find either in the original commit message, or in the temporary.
3051 * Note that we cast away the constness of "message" here. It is
3052 * const because it may come from the cached commit buffer. That's OK,
3053 * because we know that it is modifiable heap memory, and that while
3054 * grep_buffer may modify it for speed, it will restore any
3055 * changes before returning.
3056 */
72fd13f7
NTND
3057 if (buf.len)
3058 retval = grep_buffer(&opt->grep_filter, buf.buf, buf.len);
3059 else
3060 retval = grep_buffer(&opt->grep_filter,
b000c59b 3061 (char *)message, strlen(message));
72fd13f7 3062 strbuf_release(&buf);
b66103c3 3063 unuse_commit_buffer(commit, message);
22dfa8a2 3064 return opt->invert_grep ? !retval : retval;
8ecae9b0
JH
3065}
3066
53d00b39 3067static inline int want_ancestry(const struct rev_info *revs)
f35f5603 3068{
8bb65883 3069 return (revs->rewrite_parents || revs->children.name);
f35f5603
JH
3070}
3071
de239446
JK
3072/*
3073 * Return a timestamp to be used for --since/--until comparisons for this
3074 * commit, based on the revision options.
3075 */
3076static timestamp_t comparison_date(const struct rev_info *revs,
3077 struct commit *commit)
3078{
3079 return revs->reflog_info ?
3080 get_reflog_timestamp(revs->reflog_info) :
3081 commit->date;
3082}
3083
beb5af43 3084enum commit_action get_commit_action(struct rev_info *revs, struct commit *commit)
252a7c02
LT
3085{
3086 if (commit->object.flags & SHOWN)
3087 return commit_ignore;
ed1c9977 3088 if (revs->unpacked && has_sha1_pack(commit->object.oid.hash))
252a7c02
LT
3089 return commit_ignore;
3090 if (commit->object.flags & UNINTERESTING)
3091 return commit_ignore;
de239446
JK
3092 if (revs->min_age != -1 &&
3093 comparison_date(revs, commit) > revs->min_age)
3094 return commit_ignore;
ad5aeede 3095 if (revs->min_parents || (revs->max_parents >= 0)) {
bf3418b0 3096 int n = commit_list_count(commit->parents);
ad5aeede
MG
3097 if ((n < revs->min_parents) ||
3098 ((revs->max_parents >= 0) && (n > revs->max_parents)))
3099 return commit_ignore;
3100 }
252a7c02
LT
3101 if (!commit_match(commit, revs))
3102 return commit_ignore;
53b2c823 3103 if (revs->prune && revs->dense) {
252a7c02 3104 /* Commit without changes? */
7dc0fe3b 3105 if (commit->object.flags & TREESAME) {
bf3418b0
KB
3106 int n;
3107 struct commit_list *p;
252a7c02 3108 /* drop merges unless we want parenthood */
f35f5603 3109 if (!want_ancestry(revs))
252a7c02 3110 return commit_ignore;
bf3418b0
KB
3111 /*
3112 * If we want ancestry, then need to keep any merges
3113 * between relevant commits to tie together topology.
3114 * For consistency with TREESAME and simplification
3115 * use "relevant" here rather than just INTERESTING,
3116 * to treat bottom commit(s) as part of the topology.
3117 */
3118 for (n = 0, p = commit->parents; p; p = p->next)
3119 if (relevant_commit(p->item))
3120 if (++n >= 2)
3121 return commit_show;
3122 return commit_ignore;
252a7c02 3123 }
252a7c02
LT
3124 }
3125 return commit_show;
3126}
3127
0131c490
JH
3128define_commit_slab(saved_parents, struct commit_list *);
3129
3130#define EMPTY_PARENT_LIST ((struct commit_list *)-1)
3131
3132/*
3133 * You may only call save_parents() once per commit (this is checked
3134 * for non-root commits).
3135 */
3136static void save_parents(struct rev_info *revs, struct commit *commit)
3137{
3138 struct commit_list **pp;
3139
3140 if (!revs->saved_parents_slab) {
3141 revs->saved_parents_slab = xmalloc(sizeof(struct saved_parents));
3142 init_saved_parents(revs->saved_parents_slab);
3143 }
3144
3145 pp = saved_parents_at(revs->saved_parents_slab, commit);
3146
3147 /*
3148 * When walking with reflogs, we may visit the same commit
3149 * several times: once for each appearance in the reflog.
3150 *
3151 * In this case, save_parents() will be called multiple times.
3152 * We want to keep only the first set of parents. We need to
3153 * store a sentinel value for an empty (i.e., NULL) parent
3154 * list to distinguish it from a not-yet-saved list, however.
3155 */
3156 if (*pp)
3157 return;
3158 if (commit->parents)
3159 *pp = copy_commit_list(commit->parents);
3160 else
3161 *pp = EMPTY_PARENT_LIST;
3162}
3163
3164static void free_saved_parents(struct rev_info *revs)
3165{
3166 if (revs->saved_parents_slab)
3167 clear_saved_parents(revs->saved_parents_slab);
3168}
3169
3170struct commit_list *get_saved_parents(struct rev_info *revs, const struct commit *commit)
3171{
3172 struct commit_list *parents;
3173
3174 if (!revs->saved_parents_slab)
3175 return commit->parents;
3176
3177 parents = *saved_parents_at(revs->saved_parents_slab, commit);
3178 if (parents == EMPTY_PARENT_LIST)
3179 return NULL;
3180 return parents;
3181}
3182
beb5af43
AS
3183enum commit_action simplify_commit(struct rev_info *revs, struct commit *commit)
3184{
3185 enum commit_action action = get_commit_action(revs, commit);
3186
3187 if (action == commit_show &&
beb5af43 3188 revs->prune && revs->dense && want_ancestry(revs)) {
53d00b39
TR
3189 /*
3190 * --full-diff on simplified parents is no good: it
3191 * will show spurious changes from the commits that
3192 * were elided. So we save the parents on the side
3193 * when --full-diff is in effect.
3194 */
3195 if (revs->full_diff)
3196 save_parents(revs, commit);
c7edcae0 3197 if (rewrite_parents(revs, commit, rewrite_one) < 0)
beb5af43
AS
3198 return commit_error;
3199 }
3200 return action;
3201}
3202
1b32dece
NTND
3203static void track_linear(struct rev_info *revs, struct commit *commit)
3204{
3205 if (revs->track_first_time) {
3206 revs->linear = 1;
3207 revs->track_first_time = 0;
3208 } else {
3209 struct commit_list *p;
3210 for (p = revs->previous_parents; p; p = p->next)
3211 if (p->item == NULL || /* first commit */
f2fd0760 3212 !oidcmp(&p->item->object.oid, &commit->object.oid))
1b32dece
NTND
3213 break;
3214 revs->linear = p != NULL;
3215 }
3216 if (revs->reverse) {
3217 if (revs->linear)
3218 commit->object.flags |= TRACK_LINEAR;
3219 }
3220 free_commit_list(revs->previous_parents);
3221 revs->previous_parents = copy_commit_list(commit->parents);
3222}
3223
d5db6c9e 3224static struct commit *get_revision_1(struct rev_info *revs)
a4a88b2b 3225{
7c2f08aa 3226 while (1) {
d08565bf 3227 struct commit *commit;
a4a88b2b 3228
d08565bf
JK
3229 if (revs->reflog_info)
3230 commit = next_reflog_entry(revs->reflog_info);
3231 else
3232 commit = pop_commit(&revs->commits);
2a0925be 3233
7c2f08aa
JK
3234 if (!commit)
3235 return NULL;
3236
d08565bf 3237 if (revs->reflog_info)
ffa1eeae 3238 commit->object.flags &= ~(ADDED | SEEN | SHOWN);
8860fd42 3239
2a0925be
LT
3240 /*
3241 * If we haven't done the list limiting, we need to look at
be7db6e5
LT
3242 * the parents here. We also need to do the date-based limiting
3243 * that we'd otherwise have done in limit_list().
2a0925be 3244 */
be7db6e5 3245 if (!revs->limited) {
744f4985 3246 if (revs->max_age != -1 &&
de239446 3247 comparison_date(revs, commit) < revs->max_age)
86ab4906 3248 continue;
d08565bf
JK
3249
3250 if (revs->reflog_info)
3251 try_to_simplify_commit(revs, commit);
3252 else if (add_parents_to_list(revs, commit, &revs->commits, NULL) < 0) {
2db1a43f
VM
3253 if (!revs->ignore_missing_links)
3254 die("Failed to traverse parents of commit %s",
f2fd0760 3255 oid_to_hex(&commit->object.oid));
2db1a43f 3256 }
be7db6e5 3257 }
744f4985 3258
252a7c02
LT
3259 switch (simplify_commit(revs, commit)) {
3260 case commit_ignore:
8ecae9b0 3261 continue;
252a7c02 3262 case commit_error:
ed62089c 3263 die("Failed to simplify parents of commit %s",
f2fd0760 3264 oid_to_hex(&commit->object.oid));
252a7c02 3265 default:
1b32dece
NTND
3266 if (revs->track_linear)
3267 track_linear(revs, commit);
252a7c02 3268 return commit;
384e99a4 3269 }
7c2f08aa 3270 }
765ac8ec 3271}
d5db6c9e 3272
be6754c6
MH
3273/*
3274 * Return true for entries that have not yet been shown. (This is an
3275 * object_array_each_func_t.)
3276 */
3277static int entry_unshown(struct object_array_entry *entry, void *cb_data_unused)
86ab4906 3278{
be6754c6
MH
3279 return !(entry->item->flags & SHOWN);
3280}
86ab4906 3281
be6754c6
MH
3282/*
3283 * If array is on the verge of a realloc, garbage-collect any entries
3284 * that have already been shown to try to free up some space.
3285 */
3286static void gc_boundary(struct object_array *array)
3287{
3288 if (array->nr == array->alloc)
3289 object_array_filter(array, entry_unshown, NULL);
86ab4906
JH
3290}
3291
4603ec0f
AS
3292static void create_boundary_commit_list(struct rev_info *revs)
3293{
3294 unsigned i;
3295 struct commit *c;
3296 struct object_array *array = &revs->boundary_commits;
3297 struct object_array_entry *objects = array->objects;
3298
3299 /*
3300 * If revs->commits is non-NULL at this point, an error occurred in
3301 * get_revision_1(). Ignore the error and continue printing the
3302 * boundary commits anyway. (This is what the code has always
3303 * done.)
3304 */
3305 if (revs->commits) {
3306 free_commit_list(revs->commits);
3307 revs->commits = NULL;
3308 }
3309
3310 /*
3311 * Put all of the actual boundary commits from revs->boundary_commits
3312 * into revs->commits
3313 */
3314 for (i = 0; i < array->nr; i++) {
3315 c = (struct commit *)(objects[i].item);
3316 if (!c)
3317 continue;
3318 if (!(c->object.flags & CHILD_SHOWN))
3319 continue;
3320 if (c->object.flags & (SHOWN | BOUNDARY))
3321 continue;
3322 c->object.flags |= BOUNDARY;
3323 commit_list_insert(c, &revs->commits);
3324 }
3325
3326 /*
3327 * If revs->topo_order is set, sort the boundary commits
3328 * in topological order
3329 */
08f704f2 3330 sort_in_topological_order(&revs->commits, revs->sort_order);
4603ec0f
AS
3331}
3332
7fefda5c 3333static struct commit *get_revision_internal(struct rev_info *revs)
d5db6c9e
JH
3334{
3335 struct commit *c = NULL;
86ab4906
JH
3336 struct commit_list *l;
3337
3338 if (revs->boundary == 2) {
4603ec0f
AS
3339 /*
3340 * All of the normal commits have already been returned,
3341 * and we are now returning boundary commits.
3342 * create_boundary_commit_list() has populated
3343 * revs->commits with the remaining commits to return.
3344 */
3345 c = pop_commit(&revs->commits);
3346 if (c)
3347 c->object.flags |= SHOWN;
9c5e66e9
JS
3348 return c;
3349 }
3350
86ab4906 3351 /*
b72a1904
JK
3352 * If our max_count counter has reached zero, then we are done. We
3353 * don't simply return NULL because we still might need to show
3354 * boundary commits. But we want to avoid calling get_revision_1, which
3355 * might do a considerable amount of work finding the next commit only
3356 * for us to throw it away.
3357 *
3358 * If it is non-zero, then either we don't have a max_count at all
3359 * (-1), or it is still counting, in which case we decrement.
86ab4906 3360 */
b72a1904
JK
3361 if (revs->max_count) {
3362 c = get_revision_1(revs);
3363 if (c) {
9e57ac55 3364 while (revs->skip_count > 0) {
b72a1904
JK
3365 revs->skip_count--;
3366 c = get_revision_1(revs);
3367 if (!c)
3368 break;
3369 }
8839ac94 3370 }
86ab4906 3371
b72a1904
JK
3372 if (revs->max_count > 0)
3373 revs->max_count--;
d5db6c9e 3374 }
86ab4906 3375
c33d8593
JH
3376 if (c)
3377 c->object.flags |= SHOWN;
3378
9e57ac55 3379 if (!revs->boundary)
d5db6c9e 3380 return c;
86ab4906
JH
3381
3382 if (!c) {
3383 /*
3384 * get_revision_1() runs out the commits, and
3385 * we are done computing the boundaries.
3386 * switch to boundary commits output mode.
3387 */
3388 revs->boundary = 2;
4603ec0f
AS
3389
3390 /*
3391 * Update revs->commits to contain the list of
3392 * boundary commits.
3393 */
3394 create_boundary_commit_list(revs);
3395
3c68d67b 3396 return get_revision_internal(revs);
86ab4906
JH
3397 }
3398
3399 /*
3400 * boundary commits are the commits that are parents of the
3401 * ones we got from get_revision_1() but they themselves are
3402 * not returned from get_revision_1(). Before returning
3403 * 'c', we need to mark its parents that they could be boundaries.
3404 */
3405
3406 for (l = c->parents; l; l = l->next) {
3407 struct object *p;
3408 p = &(l->item->object);
c33d8593 3409 if (p->flags & (CHILD_SHOWN | SHOWN))
86ab4906
JH
3410 continue;
3411 p->flags |= CHILD_SHOWN;
3412 gc_boundary(&revs->boundary_commits);
3413 add_object_array(p, NULL, &revs->boundary_commits);
3414 }
3415
3416 return c;
d5db6c9e 3417}
7fefda5c
AS
3418
3419struct commit *get_revision(struct rev_info *revs)
3420{
498bcd31
TR
3421 struct commit *c;
3422 struct commit_list *reversed;
3423
3424 if (revs->reverse) {
3425 reversed = NULL;
9e57ac55 3426 while ((c = get_revision_internal(revs)))
498bcd31 3427 commit_list_insert(c, &reversed);
498bcd31
TR
3428 revs->commits = reversed;
3429 revs->reverse = 0;
3430 revs->reverse_output_stage = 1;
3431 }
3432
1b32dece
NTND
3433 if (revs->reverse_output_stage) {
3434 c = pop_commit(&revs->commits);
3435 if (revs->track_linear)
3436 revs->linear = !!(c && c->object.flags & TRACK_LINEAR);
3437 return c;
3438 }
498bcd31
TR
3439
3440 c = get_revision_internal(revs);
7fefda5c
AS
3441 if (c && revs->graph)
3442 graph_update(revs->graph, c);
1b32dece 3443 if (!c) {
53d00b39 3444 free_saved_parents(revs);
1b32dece
NTND
3445 if (revs->previous_parents) {
3446 free_commit_list(revs->previous_parents);
3447 revs->previous_parents = NULL;
3448 }
3449 }
7fefda5c
AS
3450 return c;
3451}
1df2d656
MG
3452
3453char *get_revision_mark(const struct rev_info *revs, const struct commit *commit)
3454{
3455 if (commit->object.flags & BOUNDARY)
3456 return "-";
3457 else if (commit->object.flags & UNINTERESTING)
3458 return "^";
adbbb31e
MG
3459 else if (commit->object.flags & PATCHSAME)
3460 return "=";
1df2d656
MG
3461 else if (!revs || revs->left_right) {
3462 if (commit->object.flags & SYMMETRIC_LEFT)
3463 return "<";
3464 else
3465 return ">";
3466 } else if (revs->graph)
3467 return "*";
adbbb31e
MG
3468 else if (revs->cherry_mark)
3469 return "+";
1df2d656
MG
3470 return "";
3471}
b1b47554
MG
3472
3473void put_revision_mark(const struct rev_info *revs, const struct commit *commit)
3474{
3475 char *mark = get_revision_mark(revs, commit);
3476 if (!strlen(mark))
3477 return;
3478 fputs(mark, stdout);
3479 putchar(' ');
3480}