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