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