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