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