]> git.ipfire.org Git - thirdparty/git.git/blame - revision.c
revision.c: add BOTTOM flag for commits
[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 */
7f34a46f 912static struct commit_list *collect_bottom_commits(struct commit_list *list)
ebdc94f3 913{
7f34a46f
KB
914 struct commit_list *elem, *bottom = NULL;
915 for (elem = list; elem; elem = elem->next)
916 if (elem->item->object.flags & BOTTOM)
917 commit_list_insert(elem->item, &bottom);
ebdc94f3
JH
918 return bottom;
919}
920
60adf7d7
MG
921/* Assumes either left_only or right_only is set */
922static void limit_left_right(struct commit_list *list, struct rev_info *revs)
923{
924 struct commit_list *p;
925
926 for (p = list; p; p = p->next) {
927 struct commit *commit = p->item;
928
929 if (revs->right_only) {
930 if (commit->object.flags & SYMMETRIC_LEFT)
931 commit->object.flags |= SHOWN;
932 } else /* revs->left_only is set */
933 if (!(commit->object.flags & SYMMETRIC_LEFT))
934 commit->object.flags |= SHOWN;
935 }
936}
937
cc0e6c5a 938static int limit_list(struct rev_info *revs)
a4a88b2b 939{
7d004199
LT
940 int slop = SLOP;
941 unsigned long date = ~0ul;
a4a88b2b
LT
942 struct commit_list *list = revs->commits;
943 struct commit_list *newlist = NULL;
944 struct commit_list **p = &newlist;
ebdc94f3
JH
945 struct commit_list *bottom = NULL;
946
947 if (revs->ancestry_path) {
7f34a46f 948 bottom = collect_bottom_commits(list);
ebdc94f3 949 if (!bottom)
97b03c35 950 die("--ancestry-path given but there are no bottom commits");
ebdc94f3 951 }
a4a88b2b
LT
952
953 while (list) {
954 struct commit_list *entry = list;
955 struct commit *commit = list->item;
956 struct object *obj = &commit->object;
cdcefbc9 957 show_early_output_fn_t show;
a4a88b2b
LT
958
959 list = list->next;
960 free(entry);
961
962 if (revs->max_age != -1 && (commit->date < revs->max_age))
963 obj->flags |= UNINTERESTING;
fce87ae5 964 if (add_parents_to_list(revs, commit, &list, NULL) < 0)
cc0e6c5a 965 return -1;
a4a88b2b
LT
966 if (obj->flags & UNINTERESTING) {
967 mark_parents_uninteresting(commit);
7d004199
LT
968 if (revs->show_all)
969 p = &commit_list_insert(commit, p)->next;
970 slop = still_interesting(list, date, slop);
971 if (slop)
3131b713 972 continue;
7d004199
LT
973 /* If showing all, add the whole pending list to the end */
974 if (revs->show_all)
975 *p = list;
976 break;
a4a88b2b
LT
977 }
978 if (revs->min_age != -1 && (commit->date > revs->min_age))
979 continue;
7d004199 980 date = commit->date;
a4a88b2b 981 p = &commit_list_insert(commit, p)->next;
cdcefbc9
LT
982
983 show = show_early_output;
984 if (!show)
985 continue;
986
987 show(revs, newlist);
988 show_early_output = NULL;
a4a88b2b 989 }
adbbb31e 990 if (revs->cherry_pick || revs->cherry_mark)
36d56de6 991 cherry_pick_list(newlist, revs);
d7a17cad 992
60adf7d7
MG
993 if (revs->left_only || revs->right_only)
994 limit_left_right(newlist, revs);
995
ebdc94f3
JH
996 if (bottom) {
997 limit_to_ancestry(bottom, newlist);
998 free_commit_list(bottom);
999 }
1000
a4a88b2b 1001 revs->commits = newlist;
cc0e6c5a 1002 return 0;
a4a88b2b
LT
1003}
1004
281eee47
JH
1005static void add_rev_cmdline(struct rev_info *revs,
1006 struct object *item,
1007 const char *name,
1008 int whence,
1009 unsigned flags)
1010{
1011 struct rev_cmdline_info *info = &revs->cmdline;
1012 int nr = info->nr;
1013
1014 ALLOC_GROW(info->rev, nr + 1, info->alloc);
1015 info->rev[nr].item = item;
1016 info->rev[nr].name = name;
1017 info->rev[nr].whence = whence;
1018 info->rev[nr].flags = flags;
1019 info->nr++;
1020}
1021
a765499a
KB
1022static void add_rev_cmdline_list(struct rev_info *revs,
1023 struct commit_list *commit_list,
1024 int whence,
1025 unsigned flags)
1026{
1027 while (commit_list) {
1028 struct object *object = &commit_list->item->object;
1029 add_rev_cmdline(revs, object, sha1_to_hex(object->sha1),
1030 whence, flags);
1031 commit_list = commit_list->next;
1032 }
1033}
1034
63049292
JH
1035struct all_refs_cb {
1036 int all_flags;
71b03b42 1037 int warned_bad_reflog;
63049292
JH
1038 struct rev_info *all_revs;
1039 const char *name_for_errormsg;
1040};
ae563542 1041
8da19775 1042static int handle_one_ref(const char *path, const unsigned char *sha1, int flag, void *cb_data)
ae563542 1043{
63049292
JH
1044 struct all_refs_cb *cb = cb_data;
1045 struct object *object = get_reference(cb->all_revs, path, sha1,
1046 cb->all_flags);
281eee47 1047 add_rev_cmdline(cb->all_revs, object, path, REV_CMD_REF, cb->all_flags);
26c3177e 1048 add_pending_sha1(cb->all_revs, path, sha1, cb->all_flags);
ae563542
LT
1049 return 0;
1050}
1051
d08bae7e
IL
1052static void init_all_refs_cb(struct all_refs_cb *cb, struct rev_info *revs,
1053 unsigned flags)
1054{
1055 cb->all_revs = revs;
1056 cb->all_flags = flags;
1057}
1058
9ef6aeb0
HV
1059static void handle_refs(const char *submodule, struct rev_info *revs, unsigned flags,
1060 int (*for_each)(const char *, each_ref_fn, void *))
ae563542 1061{
63049292 1062 struct all_refs_cb cb;
d08bae7e 1063 init_all_refs_cb(&cb, revs, flags);
9ef6aeb0 1064 for_each(submodule, handle_one_ref, &cb);
63049292
JH
1065}
1066
71b03b42 1067static void handle_one_reflog_commit(unsigned char *sha1, void *cb_data)
63049292
JH
1068{
1069 struct all_refs_cb *cb = cb_data;
71b03b42
SP
1070 if (!is_null_sha1(sha1)) {
1071 struct object *o = parse_object(sha1);
1072 if (o) {
1073 o->flags |= cb->all_flags;
281eee47 1074 /* ??? CMDLINEFLAGS ??? */
71b03b42
SP
1075 add_pending_object(cb->all_revs, o, "");
1076 }
1077 else if (!cb->warned_bad_reflog) {
46efd2d9 1078 warning("reflog of '%s' references pruned commits",
71b03b42
SP
1079 cb->name_for_errormsg);
1080 cb->warned_bad_reflog = 1;
1081 }
63049292 1082 }
71b03b42
SP
1083}
1084
883d60fa
JS
1085static int handle_one_reflog_ent(unsigned char *osha1, unsigned char *nsha1,
1086 const char *email, unsigned long timestamp, int tz,
1087 const char *message, void *cb_data)
71b03b42
SP
1088{
1089 handle_one_reflog_commit(osha1, cb_data);
1090 handle_one_reflog_commit(nsha1, cb_data);
63049292
JH
1091 return 0;
1092}
1093
1094static int handle_one_reflog(const char *path, const unsigned char *sha1, int flag, void *cb_data)
1095{
1096 struct all_refs_cb *cb = cb_data;
71b03b42 1097 cb->warned_bad_reflog = 0;
63049292
JH
1098 cb->name_for_errormsg = path;
1099 for_each_reflog_ent(path, handle_one_reflog_ent, cb_data);
1100 return 0;
1101}
1102
1103static void handle_reflog(struct rev_info *revs, unsigned flags)
1104{
1105 struct all_refs_cb cb;
1106 cb.all_revs = revs;
1107 cb.all_flags = flags;
25b51e2c 1108 for_each_reflog(handle_one_reflog, &cb);
ae563542
LT
1109}
1110
281eee47 1111static int add_parents_only(struct rev_info *revs, const char *arg_, int flags)
ea4a19e1
JH
1112{
1113 unsigned char sha1[20];
1114 struct object *it;
1115 struct commit *commit;
1116 struct commit_list *parents;
281eee47 1117 const char *arg = arg_;
ea4a19e1
JH
1118
1119 if (*arg == '^') {
7f34a46f 1120 flags ^= UNINTERESTING | BOTTOM;
ea4a19e1
JH
1121 arg++;
1122 }
cd74e473 1123 if (get_sha1_committish(arg, sha1))
ea4a19e1
JH
1124 return 0;
1125 while (1) {
1126 it = get_reference(revs, arg, sha1, 0);
cc243c3c
JH
1127 if (!it && revs->ignore_missing)
1128 return 0;
1974632c 1129 if (it->type != OBJ_TAG)
ea4a19e1 1130 break;
9684afd9
MK
1131 if (!((struct tag*)it)->tagged)
1132 return 0;
e702496e 1133 hashcpy(sha1, ((struct tag*)it)->tagged->sha1);
ea4a19e1 1134 }
1974632c 1135 if (it->type != OBJ_COMMIT)
ea4a19e1
JH
1136 return 0;
1137 commit = (struct commit *)it;
1138 for (parents = commit->parents; parents; parents = parents->next) {
1139 it = &parents->item->object;
1140 it->flags |= flags;
281eee47 1141 add_rev_cmdline(revs, it, arg_, REV_CMD_PARENTS_ONLY, flags);
ea4a19e1
JH
1142 add_pending_object(revs, it, arg);
1143 }
1144 return 1;
1145}
1146
db6296a5 1147void init_revisions(struct rev_info *revs, const char *prefix)
8efdc326
FK
1148{
1149 memset(revs, 0, sizeof(*revs));
8e8f9987 1150
6b9c58f4 1151 revs->abbrev = DEFAULT_ABBREV;
8e8f9987 1152 revs->ignore_merges = 1;
9202434c 1153 revs->simplify_history = 1;
8f67f8ae 1154 DIFF_OPT_SET(&revs->pruning, RECURSIVE);
90b19941 1155 DIFF_OPT_SET(&revs->pruning, QUICK);
cd2bdc53
LT
1156 revs->pruning.add_remove = file_add_remove;
1157 revs->pruning.change = file_change;
8efdc326
FK
1158 revs->lifo = 1;
1159 revs->dense = 1;
db6296a5 1160 revs->prefix = prefix;
8efdc326
FK
1161 revs->max_age = -1;
1162 revs->min_age = -1;
d5db6c9e 1163 revs->skip_count = -1;
8efdc326 1164 revs->max_count = -1;
ad5aeede 1165 revs->max_parents = -1;
8efdc326 1166
cd2bdc53
LT
1167 revs->commit_format = CMIT_FMT_DEFAULT;
1168
918d4e1c
JH
1169 init_grep_defaults();
1170 grep_init(&revs->grep_filter, prefix);
0843acfd 1171 revs->grep_filter.status_only = 1;
0843acfd
JK
1172 revs->grep_filter.regflags = REG_NEWLINE;
1173
cd2bdc53 1174 diff_setup(&revs->diffopt);
c0cb4a06 1175 if (prefix && !revs->diffopt.prefix) {
cd676a51
JH
1176 revs->diffopt.prefix = prefix;
1177 revs->diffopt.prefix_length = strlen(prefix);
1178 }
3a03cf6b
JK
1179
1180 revs->notes_opt.use_default_notes = -1;
8efdc326
FK
1181}
1182
0d2c9d67
RS
1183static void add_pending_commit_list(struct rev_info *revs,
1184 struct commit_list *commit_list,
1185 unsigned int flags)
1186{
1187 while (commit_list) {
1188 struct object *object = &commit_list->item->object;
1189 object->flags |= flags;
1190 add_pending_object(revs, object, sha1_to_hex(object->sha1));
1191 commit_list = commit_list->next;
1192 }
1193}
1194
ae3e5e1e
JH
1195static void prepare_show_merge(struct rev_info *revs)
1196{
1197 struct commit_list *bases;
1198 struct commit *head, *other;
1199 unsigned char sha1[20];
1200 const char **prune = NULL;
1201 int i, prune_num = 1; /* counting terminating NULL */
1202
baf18fc2 1203 if (get_sha1("HEAD", sha1))
ae3e5e1e 1204 die("--merge without HEAD?");
baf18fc2
NTND
1205 head = lookup_commit_or_die(sha1, "HEAD");
1206 if (get_sha1("MERGE_HEAD", sha1))
ae3e5e1e 1207 die("--merge without MERGE_HEAD?");
baf18fc2 1208 other = lookup_commit_or_die(sha1, "MERGE_HEAD");
ae3e5e1e
JH
1209 add_pending_object(revs, &head->object, "HEAD");
1210 add_pending_object(revs, &other->object, "MERGE_HEAD");
1211 bases = get_merge_bases(head, other, 1);
7f34a46f
KB
1212 add_rev_cmdline_list(revs, bases, REV_CMD_MERGE_BASE, UNINTERESTING | BOTTOM);
1213 add_pending_commit_list(revs, bases, UNINTERESTING | BOTTOM);
e82447b1
JH
1214 free_commit_list(bases);
1215 head->object.flags |= SYMMETRIC_LEFT;
ae3e5e1e
JH
1216
1217 if (!active_nr)
1218 read_cache();
1219 for (i = 0; i < active_nr; i++) {
1220 struct cache_entry *ce = active_cache[i];
1221 if (!ce_stage(ce))
1222 continue;
eb9cb55b 1223 if (ce_path_match(ce, &revs->prune_data)) {
ae3e5e1e
JH
1224 prune_num++;
1225 prune = xrealloc(prune, sizeof(*prune) * prune_num);
1226 prune[prune_num-2] = ce->name;
1227 prune[prune_num-1] = NULL;
1228 }
1229 while ((i+1 < active_nr) &&
1230 ce_same_name(ce, active_cache[i+1]))
1231 i++;
1232 }
afe069d1
NTND
1233 free_pathspec(&revs->prune_data);
1234 init_pathspec(&revs->prune_data, prune);
e82447b1 1235 revs->limited = 1;
ae3e5e1e
JH
1236}
1237
8e676e8b 1238int handle_revision_arg(const char *arg_, struct rev_info *revs, int flags, unsigned revarg_opt)
5d6f0935 1239{
249c8f4a 1240 struct object_context oc;
5d6f0935
JH
1241 char *dotdot;
1242 struct object *object;
1243 unsigned char sha1[20];
1244 int local_flags;
281eee47 1245 const char *arg = arg_;
8e676e8b 1246 int cant_be_filename = revarg_opt & REVARG_CANNOT_BE_FILENAME;
d5f6b1d7 1247 unsigned get_sha1_flags = 0;
5d6f0935 1248
7f34a46f
KB
1249 flags = flags & UNINTERESTING ? flags | BOTTOM : flags & ~BOTTOM;
1250
5d6f0935
JH
1251 dotdot = strstr(arg, "..");
1252 if (dotdot) {
1253 unsigned char from_sha1[20];
1254 const char *next = dotdot + 2;
1255 const char *this = arg;
1256 int symmetric = *next == '.';
7f34a46f 1257 unsigned int flags_exclude = flags ^ (UNINTERESTING | BOTTOM);
003c84f6 1258 static const char head_by_default[] = "HEAD";
281eee47 1259 unsigned int a_flags;
5d6f0935
JH
1260
1261 *dotdot = 0;
1262 next += symmetric;
1263
1264 if (!*next)
003c84f6 1265 next = head_by_default;
5d6f0935 1266 if (dotdot == arg)
003c84f6
JH
1267 this = head_by_default;
1268 if (this == head_by_default && next == head_by_default &&
1269 !symmetric) {
1270 /*
1271 * Just ".."? That is not a range but the
1272 * pathspec for the parent directory.
1273 */
1274 if (!cant_be_filename) {
1275 *dotdot = '.';
1276 return -1;
1277 }
1278 }
cd74e473
JH
1279 if (!get_sha1_committish(this, from_sha1) &&
1280 !get_sha1_committish(next, sha1)) {
5d6f0935
JH
1281 struct commit *a, *b;
1282 struct commit_list *exclude;
1283
1284 a = lookup_commit_reference(from_sha1);
1285 b = lookup_commit_reference(sha1);
1286 if (!a || !b) {
cc243c3c
JH
1287 if (revs->ignore_missing)
1288 return 0;
5d6f0935
JH
1289 die(symmetric ?
1290 "Invalid symmetric difference expression %s...%s" :
1291 "Invalid revision range %s..%s",
1292 arg, next);
1293 }
1294
1295 if (!cant_be_filename) {
1296 *dotdot = '.';
1297 verify_non_filename(revs->prefix, arg);
1298 }
1299
1300 if (symmetric) {
1301 exclude = get_merge_bases(a, b, 1);
a765499a
KB
1302 add_rev_cmdline_list(revs, exclude,
1303 REV_CMD_MERGE_BASE,
1304 flags_exclude);
5d6f0935
JH
1305 add_pending_commit_list(revs, exclude,
1306 flags_exclude);
1307 free_commit_list(exclude);
281eee47 1308 a_flags = flags | SYMMETRIC_LEFT;
5d6f0935 1309 } else
281eee47
JH
1310 a_flags = flags_exclude;
1311 a->object.flags |= a_flags;
5d6f0935 1312 b->object.flags |= flags;
281eee47
JH
1313 add_rev_cmdline(revs, &a->object, this,
1314 REV_CMD_LEFT, a_flags);
1315 add_rev_cmdline(revs, &b->object, next,
1316 REV_CMD_RIGHT, flags);
5d6f0935
JH
1317 add_pending_object(revs, &a->object, this);
1318 add_pending_object(revs, &b->object, next);
1319 return 0;
1320 }
1321 *dotdot = '.';
1322 }
1323 dotdot = strstr(arg, "^@");
1324 if (dotdot && !dotdot[2]) {
1325 *dotdot = 0;
1326 if (add_parents_only(revs, arg, flags))
1327 return 0;
1328 *dotdot = '^';
1329 }
62476c8e
JH
1330 dotdot = strstr(arg, "^!");
1331 if (dotdot && !dotdot[2]) {
1332 *dotdot = 0;
7f34a46f 1333 if (!add_parents_only(revs, arg, flags ^ (UNINTERESTING | BOTTOM)))
62476c8e
JH
1334 *dotdot = '^';
1335 }
1336
5d6f0935
JH
1337 local_flags = 0;
1338 if (*arg == '^') {
7f34a46f 1339 local_flags = UNINTERESTING | BOTTOM;
5d6f0935
JH
1340 arg++;
1341 }
d5f6b1d7
JH
1342
1343 if (revarg_opt & REVARG_COMMITTISH)
1344 get_sha1_flags = GET_SHA1_COMMITTISH;
1345
1346 if (get_sha1_with_context(arg, get_sha1_flags, sha1, &oc))
cc243c3c 1347 return revs->ignore_missing ? 0 : -1;
5d6f0935
JH
1348 if (!cant_be_filename)
1349 verify_non_filename(revs->prefix, arg);
1350 object = get_reference(revs, arg, sha1, flags ^ local_flags);
281eee47 1351 add_rev_cmdline(revs, object, arg_, REV_CMD_REV, flags ^ local_flags);
249c8f4a 1352 add_pending_object_with_mode(revs, object, arg, oc.mode);
5d6f0935
JH
1353 return 0;
1354}
1355
4da5af31
JH
1356struct cmdline_pathspec {
1357 int alloc;
1358 int nr;
1359 const char **path;
1360};
1fc561d1 1361
4da5af31
JH
1362static void append_prune_data(struct cmdline_pathspec *prune, const char **av)
1363{
1364 while (*av) {
1365 ALLOC_GROW(prune->path, prune->nr+1, prune->alloc);
1366 prune->path[prune->nr++] = *(av++);
1367 }
1368}
60da8b15 1369
4da5af31
JH
1370static void read_pathspec_from_stdin(struct rev_info *revs, struct strbuf *sb,
1371 struct cmdline_pathspec *prune)
1372{
60da8b15
JH
1373 while (strbuf_getwholeline(sb, stdin, '\n') != EOF) {
1374 int len = sb->len;
1375 if (len && sb->buf[len - 1] == '\n')
1376 sb->buf[--len] = '\0';
4da5af31
JH
1377 ALLOC_GROW(prune->path, prune->nr+1, prune->alloc);
1378 prune->path[prune->nr++] = xstrdup(sb->buf);
60da8b15 1379 }
60da8b15
JH
1380}
1381
4da5af31
JH
1382static void read_revisions_from_stdin(struct rev_info *revs,
1383 struct cmdline_pathspec *prune)
1fc561d1 1384{
63d564b3 1385 struct strbuf sb;
60da8b15 1386 int seen_dashdash = 0;
1fc561d1 1387
63d564b3
JH
1388 strbuf_init(&sb, 1000);
1389 while (strbuf_getwholeline(&sb, stdin, '\n') != EOF) {
1390 int len = sb.len;
1391 if (len && sb.buf[len - 1] == '\n')
1392 sb.buf[--len] = '\0';
1fc561d1
AB
1393 if (!len)
1394 break;
60da8b15
JH
1395 if (sb.buf[0] == '-') {
1396 if (len == 2 && sb.buf[1] == '-') {
1397 seen_dashdash = 1;
1398 break;
1399 }
1fc561d1 1400 die("options not supported in --stdin mode");
60da8b15 1401 }
70d26c6e
TR
1402 if (handle_revision_arg(xstrdup(sb.buf), revs, 0,
1403 REVARG_CANNOT_BE_FILENAME))
63d564b3 1404 die("bad revision '%s'", sb.buf);
1fc561d1 1405 }
60da8b15
JH
1406 if (seen_dashdash)
1407 read_pathspec_from_stdin(revs, &sb, prune);
63d564b3 1408 strbuf_release(&sb);
1fc561d1
AB
1409}
1410
2d10c555 1411static void add_grep(struct rev_info *revs, const char *ptn, enum grep_pat_token what)
bd95fcd3 1412{
0843acfd 1413 append_grep_pattern(&revs->grep_filter, ptn, "command line", 0, what);
2d10c555
JH
1414}
1415
a4d7d2c6 1416static void add_header_grep(struct rev_info *revs, enum grep_header_field field, const char *pattern)
2d10c555 1417{
a4d7d2c6 1418 append_header_grep_pattern(&revs->grep_filter, field, pattern);
bd95fcd3
JH
1419}
1420
1421static void add_message_grep(struct rev_info *revs, const char *pattern)
1422{
2d10c555 1423 add_grep(revs, pattern, GREP_PATTERN_BODY);
bd95fcd3
JH
1424}
1425
6b61ec05
PH
1426static int handle_revision_opt(struct rev_info *revs, int argc, const char **argv,
1427 int *unkc, const char **unkv)
02e54220
PH
1428{
1429 const char *arg = argv[0];
7d7b86f7
MM
1430 const char *optarg;
1431 int argcount;
02e54220
PH
1432
1433 /* pseudo revision arguments */
1434 if (!strcmp(arg, "--all") || !strcmp(arg, "--branches") ||
1435 !strcmp(arg, "--tags") || !strcmp(arg, "--remotes") ||
1436 !strcmp(arg, "--reflog") || !strcmp(arg, "--not") ||
ad3f9a71 1437 !strcmp(arg, "--no-walk") || !strcmp(arg, "--do-walk") ||
0fc63ec4
JN
1438 !strcmp(arg, "--bisect") || !prefixcmp(arg, "--glob=") ||
1439 !prefixcmp(arg, "--branches=") || !prefixcmp(arg, "--tags=") ||
ca92e59e 1440 !prefixcmp(arg, "--remotes=") || !prefixcmp(arg, "--no-walk="))
02e54220
PH
1441 {
1442 unkv[(*unkc)++] = arg;
0fe8c138 1443 return 1;
02e54220
PH
1444 }
1445
7d7b86f7
MM
1446 if ((argcount = parse_long_opt("max-count", argv, &optarg))) {
1447 revs->max_count = atoi(optarg);
5853caec 1448 revs->no_walk = 0;
7d7b86f7
MM
1449 return argcount;
1450 } else if ((argcount = parse_long_opt("skip", argv, &optarg))) {
1451 revs->skip_count = atoi(optarg);
1452 return argcount;
02e54220
PH
1453 } else if ((*arg == '-') && isdigit(arg[1])) {
1454 /* accept -<digit>, like traditional "head" */
1455 revs->max_count = atoi(arg + 1);
5853caec 1456 revs->no_walk = 0;
02e54220
PH
1457 } else if (!strcmp(arg, "-n")) {
1458 if (argc <= 1)
1459 return error("-n requires an argument");
1460 revs->max_count = atoi(argv[1]);
5853caec 1461 revs->no_walk = 0;
02e54220
PH
1462 return 2;
1463 } else if (!prefixcmp(arg, "-n")) {
1464 revs->max_count = atoi(arg + 2);
5853caec 1465 revs->no_walk = 0;
7d7b86f7
MM
1466 } else if ((argcount = parse_long_opt("max-age", argv, &optarg))) {
1467 revs->max_age = atoi(optarg);
1468 return argcount;
1469 } else if ((argcount = parse_long_opt("since", argv, &optarg))) {
1470 revs->max_age = approxidate(optarg);
1471 return argcount;
1472 } else if ((argcount = parse_long_opt("after", argv, &optarg))) {
1473 revs->max_age = approxidate(optarg);
1474 return argcount;
1475 } else if ((argcount = parse_long_opt("min-age", argv, &optarg))) {
1476 revs->min_age = atoi(optarg);
1477 return argcount;
1478 } else if ((argcount = parse_long_opt("before", argv, &optarg))) {
1479 revs->min_age = approxidate(optarg);
1480 return argcount;
1481 } else if ((argcount = parse_long_opt("until", argv, &optarg))) {
1482 revs->min_age = approxidate(optarg);
1483 return argcount;
02e54220
PH
1484 } else if (!strcmp(arg, "--first-parent")) {
1485 revs->first_parent_only = 1;
ebdc94f3
JH
1486 } else if (!strcmp(arg, "--ancestry-path")) {
1487 revs->ancestry_path = 1;
cb7529e1 1488 revs->simplify_history = 0;
ebdc94f3 1489 revs->limited = 1;
02e54220
PH
1490 } else if (!strcmp(arg, "-g") || !strcmp(arg, "--walk-reflogs")) {
1491 init_reflog_walk(&revs->reflog_info);
1492 } else if (!strcmp(arg, "--default")) {
1493 if (argc <= 1)
1494 return error("bad --default argument");
1495 revs->def = argv[1];
1496 return 2;
1497 } else if (!strcmp(arg, "--merge")) {
1498 revs->show_merge = 1;
1499 } else if (!strcmp(arg, "--topo-order")) {
1500 revs->lifo = 1;
1501 revs->topo_order = 1;
6546b593
JH
1502 } else if (!strcmp(arg, "--simplify-merges")) {
1503 revs->simplify_merges = 1;
a52f0071 1504 revs->topo_order = 1;
6546b593
JH
1505 revs->rewrite_parents = 1;
1506 revs->simplify_history = 0;
1507 revs->limited = 1;
78892e32
LT
1508 } else if (!strcmp(arg, "--simplify-by-decoration")) {
1509 revs->simplify_merges = 1;
a52f0071 1510 revs->topo_order = 1;
78892e32
LT
1511 revs->rewrite_parents = 1;
1512 revs->simplify_history = 0;
1513 revs->simplify_by_decoration = 1;
1514 revs->limited = 1;
1515 revs->prune = 1;
33e7018c 1516 load_ref_decorations(DECORATE_SHORT_REFS);
02e54220
PH
1517 } else if (!strcmp(arg, "--date-order")) {
1518 revs->lifo = 0;
1519 revs->topo_order = 1;
1520 } else if (!prefixcmp(arg, "--early-output")) {
1521 int count = 100;
1522 switch (arg[14]) {
1523 case '=':
1524 count = atoi(arg+15);
1525 /* Fallthrough */
1526 case 0:
1527 revs->topo_order = 1;
1528 revs->early_output = count;
1529 }
1530 } else if (!strcmp(arg, "--parents")) {
1531 revs->rewrite_parents = 1;
1532 revs->print_parents = 1;
1533 } else if (!strcmp(arg, "--dense")) {
1534 revs->dense = 1;
1535 } else if (!strcmp(arg, "--sparse")) {
1536 revs->dense = 0;
1537 } else if (!strcmp(arg, "--show-all")) {
1538 revs->show_all = 1;
1539 } else if (!strcmp(arg, "--remove-empty")) {
1540 revs->remove_empty_trees = 1;
b8e8db28 1541 } else if (!strcmp(arg, "--merges")) {
ad5aeede 1542 revs->min_parents = 2;
02e54220 1543 } else if (!strcmp(arg, "--no-merges")) {
ad5aeede
MG
1544 revs->max_parents = 1;
1545 } else if (!prefixcmp(arg, "--min-parents=")) {
1546 revs->min_parents = atoi(arg+14);
1547 } else if (!prefixcmp(arg, "--no-min-parents")) {
1548 revs->min_parents = 0;
1549 } else if (!prefixcmp(arg, "--max-parents=")) {
1550 revs->max_parents = atoi(arg+14);
1551 } else if (!prefixcmp(arg, "--no-max-parents")) {
1552 revs->max_parents = -1;
02e54220
PH
1553 } else if (!strcmp(arg, "--boundary")) {
1554 revs->boundary = 1;
1555 } else if (!strcmp(arg, "--left-right")) {
1556 revs->left_right = 1;
60adf7d7 1557 } else if (!strcmp(arg, "--left-only")) {
24852d91 1558 if (revs->right_only)
94f605ec
MG
1559 die("--left-only is incompatible with --right-only"
1560 " or --cherry");
60adf7d7
MG
1561 revs->left_only = 1;
1562 } else if (!strcmp(arg, "--right-only")) {
24852d91
JH
1563 if (revs->left_only)
1564 die("--right-only is incompatible with --left-only");
60adf7d7 1565 revs->right_only = 1;
94f605ec
MG
1566 } else if (!strcmp(arg, "--cherry")) {
1567 if (revs->left_only)
1568 die("--cherry is incompatible with --left-only");
1569 revs->cherry_mark = 1;
1570 revs->right_only = 1;
ad5aeede 1571 revs->max_parents = 1;
94f605ec 1572 revs->limited = 1;
f69c5018
TR
1573 } else if (!strcmp(arg, "--count")) {
1574 revs->count = 1;
adbbb31e
MG
1575 } else if (!strcmp(arg, "--cherry-mark")) {
1576 if (revs->cherry_pick)
1577 die("--cherry-mark is incompatible with --cherry-pick");
1578 revs->cherry_mark = 1;
1579 revs->limited = 1; /* needs limit_list() */
02e54220 1580 } else if (!strcmp(arg, "--cherry-pick")) {
adbbb31e
MG
1581 if (revs->cherry_mark)
1582 die("--cherry-pick is incompatible with --cherry-mark");
02e54220
PH
1583 revs->cherry_pick = 1;
1584 revs->limited = 1;
1585 } else if (!strcmp(arg, "--objects")) {
1586 revs->tag_objects = 1;
1587 revs->tree_objects = 1;
1588 revs->blob_objects = 1;
1589 } else if (!strcmp(arg, "--objects-edge")) {
1590 revs->tag_objects = 1;
1591 revs->tree_objects = 1;
1592 revs->blob_objects = 1;
1593 revs->edge_hint = 1;
5a48d240
JH
1594 } else if (!strcmp(arg, "--verify-objects")) {
1595 revs->tag_objects = 1;
1596 revs->tree_objects = 1;
1597 revs->blob_objects = 1;
1598 revs->verify_objects = 1;
02e54220
PH
1599 } else if (!strcmp(arg, "--unpacked")) {
1600 revs->unpacked = 1;
03a9683d
JH
1601 } else if (!prefixcmp(arg, "--unpacked=")) {
1602 die("--unpacked=<packfile> no longer supported.");
02e54220
PH
1603 } else if (!strcmp(arg, "-r")) {
1604 revs->diff = 1;
1605 DIFF_OPT_SET(&revs->diffopt, RECURSIVE);
1606 } else if (!strcmp(arg, "-t")) {
1607 revs->diff = 1;
1608 DIFF_OPT_SET(&revs->diffopt, RECURSIVE);
1609 DIFF_OPT_SET(&revs->diffopt, TREE_IN_RECURSIVE);
1610 } else if (!strcmp(arg, "-m")) {
1611 revs->ignore_merges = 0;
1612 } else if (!strcmp(arg, "-c")) {
1613 revs->diff = 1;
1614 revs->dense_combined_merges = 0;
1615 revs->combine_merges = 1;
1616 } else if (!strcmp(arg, "--cc")) {
1617 revs->diff = 1;
1618 revs->dense_combined_merges = 1;
1619 revs->combine_merges = 1;
1620 } else if (!strcmp(arg, "-v")) {
1621 revs->verbose_header = 1;
1622 } else if (!strcmp(arg, "--pretty")) {
1623 revs->verbose_header = 1;
66b2ed09 1624 revs->pretty_given = 1;
02e54220 1625 get_commit_format(arg+8, revs);
3a4c1a5e 1626 } else if (!prefixcmp(arg, "--pretty=") || !prefixcmp(arg, "--format=")) {
7d7b86f7
MM
1627 /*
1628 * Detached form ("--pretty X" as opposed to "--pretty=X")
1629 * not allowed, since the argument is optional.
1630 */
02e54220 1631 revs->verbose_header = 1;
66b2ed09 1632 revs->pretty_given = 1;
02e54220 1633 get_commit_format(arg+9, revs);
7249e912 1634 } else if (!strcmp(arg, "--show-notes") || !strcmp(arg, "--notes")) {
66b2ed09
JH
1635 revs->show_notes = 1;
1636 revs->show_notes_given = 1;
3a03cf6b 1637 revs->notes_opt.use_default_notes = 1;
0c37f1fc
JH
1638 } else if (!strcmp(arg, "--show-signature")) {
1639 revs->show_signature = 1;
7249e912
JK
1640 } else if (!prefixcmp(arg, "--show-notes=") ||
1641 !prefixcmp(arg, "--notes=")) {
894a9d33
TR
1642 struct strbuf buf = STRBUF_INIT;
1643 revs->show_notes = 1;
1644 revs->show_notes_given = 1;
7249e912
JK
1645 if (!prefixcmp(arg, "--show-notes")) {
1646 if (revs->notes_opt.use_default_notes < 0)
1647 revs->notes_opt.use_default_notes = 1;
1648 strbuf_addstr(&buf, arg+13);
1649 }
1650 else
1651 strbuf_addstr(&buf, arg+8);
c063f0a9 1652 expand_notes_ref(&buf);
304cc11c 1653 string_list_append(&revs->notes_opt.extra_notes_refs,
1d2f80fa 1654 strbuf_detach(&buf, NULL));
66b2ed09
JH
1655 } else if (!strcmp(arg, "--no-notes")) {
1656 revs->show_notes = 0;
1657 revs->show_notes_given = 1;
92e0d425
JK
1658 revs->notes_opt.use_default_notes = -1;
1659 /* we have been strdup'ing ourselves, so trick
1660 * string_list into free()ing strings */
1661 revs->notes_opt.extra_notes_refs.strdup_strings = 1;
1662 string_list_clear(&revs->notes_opt.extra_notes_refs, 0);
1663 revs->notes_opt.extra_notes_refs.strdup_strings = 0;
894a9d33
TR
1664 } else if (!strcmp(arg, "--standard-notes")) {
1665 revs->show_notes_given = 1;
3a03cf6b 1666 revs->notes_opt.use_default_notes = 1;
894a9d33 1667 } else if (!strcmp(arg, "--no-standard-notes")) {
3a03cf6b 1668 revs->notes_opt.use_default_notes = 0;
de84accc
NS
1669 } else if (!strcmp(arg, "--oneline")) {
1670 revs->verbose_header = 1;
1671 get_commit_format("oneline", revs);
7dccadf3 1672 revs->pretty_given = 1;
de84accc 1673 revs->abbrev_commit = 1;
02e54220
PH
1674 } else if (!strcmp(arg, "--graph")) {
1675 revs->topo_order = 1;
1676 revs->rewrite_parents = 1;
1677 revs->graph = graph_init(revs);
1678 } else if (!strcmp(arg, "--root")) {
1679 revs->show_root_diff = 1;
1680 } else if (!strcmp(arg, "--no-commit-id")) {
1681 revs->no_commit_id = 1;
1682 } else if (!strcmp(arg, "--always")) {
1683 revs->always_show_header = 1;
1684 } else if (!strcmp(arg, "--no-abbrev")) {
1685 revs->abbrev = 0;
1686 } else if (!strcmp(arg, "--abbrev")) {
1687 revs->abbrev = DEFAULT_ABBREV;
1688 } else if (!prefixcmp(arg, "--abbrev=")) {
1689 revs->abbrev = strtoul(arg + 9, NULL, 10);
1690 if (revs->abbrev < MINIMUM_ABBREV)
1691 revs->abbrev = MINIMUM_ABBREV;
1692 else if (revs->abbrev > 40)
1693 revs->abbrev = 40;
1694 } else if (!strcmp(arg, "--abbrev-commit")) {
1695 revs->abbrev_commit = 1;
0c47695a
JS
1696 revs->abbrev_commit_given = 1;
1697 } else if (!strcmp(arg, "--no-abbrev-commit")) {
1698 revs->abbrev_commit = 0;
02e54220
PH
1699 } else if (!strcmp(arg, "--full-diff")) {
1700 revs->diff = 1;
1701 revs->full_diff = 1;
1702 } else if (!strcmp(arg, "--full-history")) {
1703 revs->simplify_history = 0;
1704 } else if (!strcmp(arg, "--relative-date")) {
1705 revs->date_mode = DATE_RELATIVE;
f4ea32f0 1706 revs->date_mode_explicit = 1;
7d7b86f7
MM
1707 } else if ((argcount = parse_long_opt("date", argv, &optarg))) {
1708 revs->date_mode = parse_date_format(optarg);
f4ea32f0 1709 revs->date_mode_explicit = 1;
7d7b86f7 1710 return argcount;
02e54220
PH
1711 } else if (!strcmp(arg, "--log-size")) {
1712 revs->show_log_size = 1;
1713 }
1714 /*
1715 * Grepping the commit log
1716 */
7d7b86f7
MM
1717 else if ((argcount = parse_long_opt("author", argv, &optarg))) {
1718 add_header_grep(revs, GREP_HEADER_AUTHOR, optarg);
1719 return argcount;
1720 } else if ((argcount = parse_long_opt("committer", argv, &optarg))) {
1721 add_header_grep(revs, GREP_HEADER_COMMITTER, optarg);
1722 return argcount;
72fd13f7
NTND
1723 } else if ((argcount = parse_long_opt("grep-reflog", argv, &optarg))) {
1724 add_header_grep(revs, GREP_HEADER_REFLOG, optarg);
1725 return argcount;
7d7b86f7
MM
1726 } else if ((argcount = parse_long_opt("grep", argv, &optarg))) {
1727 add_message_grep(revs, optarg);
1728 return argcount;
17bf35a3
JH
1729 } else if (!strcmp(arg, "--grep-debug")) {
1730 revs->grep_filter.debug = 1;
727b6fc3
JH
1731 } else if (!strcmp(arg, "--basic-regexp")) {
1732 grep_set_pattern_type_option(GREP_PATTERN_TYPE_BRE, &revs->grep_filter);
02e54220 1733 } else if (!strcmp(arg, "--extended-regexp") || !strcmp(arg, "-E")) {
34a4ae55 1734 grep_set_pattern_type_option(GREP_PATTERN_TYPE_ERE, &revs->grep_filter);
02e54220 1735 } else if (!strcmp(arg, "--regexp-ignore-case") || !strcmp(arg, "-i")) {
0843acfd 1736 revs->grep_filter.regflags |= REG_ICASE;
accccde4 1737 DIFF_OPT_SET(&revs->diffopt, PICKAXE_IGNORE_CASE);
02e54220 1738 } else if (!strcmp(arg, "--fixed-strings") || !strcmp(arg, "-F")) {
34a4ae55 1739 grep_set_pattern_type_option(GREP_PATTERN_TYPE_FIXED, &revs->grep_filter);
727b6fc3
JH
1740 } else if (!strcmp(arg, "--perl-regexp")) {
1741 grep_set_pattern_type_option(GREP_PATTERN_TYPE_PCRE, &revs->grep_filter);
02e54220 1742 } else if (!strcmp(arg, "--all-match")) {
0843acfd 1743 revs->grep_filter.all_match = 1;
7d7b86f7
MM
1744 } else if ((argcount = parse_long_opt("encoding", argv, &optarg))) {
1745 if (strcmp(optarg, "none"))
1746 git_log_output_encoding = xstrdup(optarg);
02e54220
PH
1747 else
1748 git_log_output_encoding = "";
7d7b86f7 1749 return argcount;
02e54220
PH
1750 } else if (!strcmp(arg, "--reverse")) {
1751 revs->reverse ^= 1;
1752 } else if (!strcmp(arg, "--children")) {
1753 revs->children.name = "children";
1754 revs->limited = 1;
cc243c3c
JH
1755 } else if (!strcmp(arg, "--ignore-missing")) {
1756 revs->ignore_missing = 1;
02e54220
PH
1757 } else {
1758 int opts = diff_opt_parse(&revs->diffopt, argv, argc);
1759 if (!opts)
1760 unkv[(*unkc)++] = arg;
1761 return opts;
1762 }
1763
1764 return 1;
1765}
1766
6b61ec05
PH
1767void parse_revision_opt(struct rev_info *revs, struct parse_opt_ctx_t *ctx,
1768 const struct option *options,
1769 const char * const usagestr[])
1770{
1771 int n = handle_revision_opt(revs, ctx->argc, ctx->argv,
1772 &ctx->cpidx, ctx->out);
1773 if (n <= 0) {
1774 error("unknown option `%s'", ctx->argv[0]);
1775 usage_with_options(usagestr, options);
1776 }
1777 ctx->argv += n;
1778 ctx->argc -= n;
1779}
1780
9ef6aeb0 1781static int for_each_bad_bisect_ref(const char *submodule, each_ref_fn fn, void *cb_data)
ad3f9a71 1782{
9ef6aeb0 1783 return for_each_ref_in_submodule(submodule, "refs/bisect/bad", fn, cb_data);
ad3f9a71
LT
1784}
1785
9ef6aeb0 1786static int for_each_good_bisect_ref(const char *submodule, each_ref_fn fn, void *cb_data)
ad3f9a71 1787{
9ef6aeb0 1788 return for_each_ref_in_submodule(submodule, "refs/bisect/good", fn, cb_data);
ad3f9a71
LT
1789}
1790
f6aca0dc
JN
1791static int handle_revision_pseudo_opt(const char *submodule,
1792 struct rev_info *revs,
1793 int argc, const char **argv, int *flags)
1794{
1795 const char *arg = argv[0];
1796 const char *optarg;
1797 int argcount;
1798
0fc63ec4
JN
1799 /*
1800 * NOTE!
1801 *
1802 * Commands like "git shortlog" will not accept the options below
1803 * unless parse_revision_opt queues them (as opposed to erroring
1804 * out).
1805 *
1806 * When implementing your new pseudo-option, remember to
1807 * register it in the list at the top of handle_revision_opt.
1808 */
f6aca0dc
JN
1809 if (!strcmp(arg, "--all")) {
1810 handle_refs(submodule, revs, *flags, for_each_ref_submodule);
1811 handle_refs(submodule, revs, *flags, head_ref_submodule);
1812 } else if (!strcmp(arg, "--branches")) {
1813 handle_refs(submodule, revs, *flags, for_each_branch_ref_submodule);
1814 } else if (!strcmp(arg, "--bisect")) {
1815 handle_refs(submodule, revs, *flags, for_each_bad_bisect_ref);
7f34a46f 1816 handle_refs(submodule, revs, *flags ^ (UNINTERESTING | BOTTOM), for_each_good_bisect_ref);
f6aca0dc
JN
1817 revs->bisect = 1;
1818 } else if (!strcmp(arg, "--tags")) {
1819 handle_refs(submodule, revs, *flags, for_each_tag_ref_submodule);
1820 } else if (!strcmp(arg, "--remotes")) {
1821 handle_refs(submodule, revs, *flags, for_each_remote_ref_submodule);
1822 } else if ((argcount = parse_long_opt("glob", argv, &optarg))) {
1823 struct all_refs_cb cb;
1824 init_all_refs_cb(&cb, revs, *flags);
1825 for_each_glob_ref(handle_one_ref, optarg, &cb);
1826 return argcount;
1827 } else if (!prefixcmp(arg, "--branches=")) {
1828 struct all_refs_cb cb;
1829 init_all_refs_cb(&cb, revs, *flags);
1830 for_each_glob_ref_in(handle_one_ref, arg + 11, "refs/heads/", &cb);
1831 } else if (!prefixcmp(arg, "--tags=")) {
1832 struct all_refs_cb cb;
1833 init_all_refs_cb(&cb, revs, *flags);
1834 for_each_glob_ref_in(handle_one_ref, arg + 7, "refs/tags/", &cb);
1835 } else if (!prefixcmp(arg, "--remotes=")) {
1836 struct all_refs_cb cb;
1837 init_all_refs_cb(&cb, revs, *flags);
1838 for_each_glob_ref_in(handle_one_ref, arg + 10, "refs/remotes/", &cb);
1839 } else if (!strcmp(arg, "--reflog")) {
1840 handle_reflog(revs, *flags);
1841 } else if (!strcmp(arg, "--not")) {
7f34a46f 1842 *flags ^= UNINTERESTING | BOTTOM;
f6aca0dc 1843 } else if (!strcmp(arg, "--no-walk")) {
ca92e59e
MZ
1844 revs->no_walk = REVISION_WALK_NO_WALK_SORTED;
1845 } else if (!prefixcmp(arg, "--no-walk=")) {
1846 /*
1847 * Detached form ("--no-walk X" as opposed to "--no-walk=X")
1848 * not allowed, since the argument is optional.
1849 */
1850 if (!strcmp(arg + 10, "sorted"))
1851 revs->no_walk = REVISION_WALK_NO_WALK_SORTED;
1852 else if (!strcmp(arg + 10, "unsorted"))
1853 revs->no_walk = REVISION_WALK_NO_WALK_UNSORTED;
1854 else
1855 return error("invalid argument to --no-walk");
f6aca0dc
JN
1856 } else if (!strcmp(arg, "--do-walk")) {
1857 revs->no_walk = 0;
1858 } else {
1859 return 0;
1860 }
1861
1862 return 1;
1863}
1864
ae563542
LT
1865/*
1866 * Parse revision information, filling in the "rev_info" structure,
1867 * and removing the used arguments from the argument list.
1868 *
765ac8ec
LT
1869 * Returns the number of arguments left that weren't recognized
1870 * (which are also moved to the head of the argument list)
ae563542 1871 */
32962c9b 1872int setup_revisions(int argc, const char **argv, struct rev_info *revs, struct setup_revision_opt *opt)
ae563542 1873{
8e676e8b 1874 int i, flags, left, seen_dashdash, read_from_stdin, got_rev_arg = 0, revarg_opt;
4da5af31 1875 struct cmdline_pathspec prune_data;
9ef6aeb0
HV
1876 const char *submodule = NULL;
1877
4da5af31 1878 memset(&prune_data, 0, sizeof(prune_data));
9ef6aeb0
HV
1879 if (opt)
1880 submodule = opt->submodule;
ae563542 1881
ae563542 1882 /* First, search for "--" */
6d5b93f2 1883 if (opt && opt->assume_dashdash) {
ae563542 1884 seen_dashdash = 1;
6d5b93f2
CB
1885 } else {
1886 seen_dashdash = 0;
1887 for (i = 1; i < argc; i++) {
1888 const char *arg = argv[i];
1889 if (strcmp(arg, "--"))
1890 continue;
1891 argv[i] = NULL;
1892 argc = i;
1893 if (argv[i + 1])
1894 append_prune_data(&prune_data, argv + i + 1);
1895 seen_dashdash = 1;
1896 break;
1897 }
ae563542
LT
1898 }
1899
02e54220
PH
1900 /* Second, deal with arguments and options */
1901 flags = 0;
d5f6b1d7
JH
1902 revarg_opt = opt ? opt->revarg_opt : 0;
1903 if (seen_dashdash)
1904 revarg_opt |= REVARG_CANNOT_BE_FILENAME;
8b3dce56 1905 read_from_stdin = 0;
02e54220 1906 for (left = i = 1; i < argc; i++) {
ae563542 1907 const char *arg = argv[i];
ae563542 1908 if (*arg == '-') {
cd2bdc53 1909 int opts;
02e54220 1910
f6aca0dc
JN
1911 opts = handle_revision_pseudo_opt(submodule,
1912 revs, argc - i, argv + i,
1913 &flags);
1914 if (opts > 0) {
1915 i += opts - 1;
8e64006e
JS
1916 continue;
1917 }
f6aca0dc 1918
8b3dce56
JH
1919 if (!strcmp(arg, "--stdin")) {
1920 if (revs->disable_stdin) {
1921 argv[left++] = arg;
1922 continue;
1923 }
1924 if (read_from_stdin++)
1925 die("--stdin given twice?");
60da8b15 1926 read_revisions_from_stdin(revs, &prune_data);
8b3dce56
JH
1927 continue;
1928 }
2d10c555 1929
02e54220 1930 opts = handle_revision_opt(revs, argc - i, argv + i, &left, argv);
cd2bdc53 1931 if (opts > 0) {
cd2bdc53
LT
1932 i += opts - 1;
1933 continue;
1934 }
02e54220
PH
1935 if (opts < 0)
1936 exit(128);
ae563542
LT
1937 continue;
1938 }
ae563542 1939
8e676e8b
JH
1940
1941 if (handle_revision_arg(arg, revs, flags, revarg_opt)) {
5d6f0935
JH
1942 int j;
1943 if (seen_dashdash || *arg == '^')
ae563542
LT
1944 die("bad revision '%s'", arg);
1945
ea92f41f
JH
1946 /* If we didn't have a "--":
1947 * (1) all filenames must exist;
1948 * (2) all rev-args must not be interpretable
1949 * as a valid filename.
1950 * but the latter we have checked in the main loop.
1951 */
e23d0b4a 1952 for (j = i; j < argc; j++)
023e37c3 1953 verify_filename(revs->prefix, argv[j], j == i);
e23d0b4a 1954
60da8b15 1955 append_prune_data(&prune_data, argv + i);
ae563542
LT
1956 break;
1957 }
8fcaca3f
DO
1958 else
1959 got_rev_arg = 1;
ae563542 1960 }
5d6f0935 1961
4da5af31 1962 if (prune_data.nr) {
93e7d672
JH
1963 /*
1964 * If we need to introduce the magic "a lone ':' means no
1965 * pathspec whatsoever", here is the place to do so.
1966 *
1967 * if (prune_data.nr == 1 && !strcmp(prune_data[0], ":")) {
1968 * prune_data.nr = 0;
1969 * prune_data.alloc = 0;
1970 * free(prune_data.path);
1971 * prune_data.path = NULL;
1972 * } else {
1973 * terminate prune_data.alloc with NULL and
1974 * call init_pathspec() to set revs->prune_data here.
1975 * }
1976 */
4da5af31
JH
1977 ALLOC_GROW(prune_data.path, prune_data.nr+1, prune_data.alloc);
1978 prune_data.path[prune_data.nr++] = NULL;
8f2d4b19
JH
1979 init_pathspec(&revs->prune_data,
1980 get_pathspec(revs->prefix, prune_data.path));
4da5af31 1981 }
5486ef0e 1982
02e54220 1983 if (revs->def == NULL)
32962c9b 1984 revs->def = opt ? opt->def : NULL;
b4490059
JH
1985 if (opt && opt->tweak)
1986 opt->tweak(revs, opt);
02e54220 1987 if (revs->show_merge)
ae3e5e1e 1988 prepare_show_merge(revs);
8fcaca3f 1989 if (revs->def && !revs->pending.nr && !got_rev_arg) {
ae563542 1990 unsigned char sha1[20];
cd2bdc53 1991 struct object *object;
249c8f4a 1992 struct object_context oc;
33bd598c 1993 if (get_sha1_with_context(revs->def, 0, sha1, &oc))
02e54220
PH
1994 die("bad default revision '%s'", revs->def);
1995 object = get_reference(revs, revs->def, sha1, 0);
249c8f4a 1996 add_pending_object_with_mode(revs, object, revs->def, oc.mode);
ae563542 1997 }
8efdc326 1998
b7bb760d
LT
1999 /* Did the user ask for any diff output? Run the diff! */
2000 if (revs->diffopt.output_format & ~DIFF_FORMAT_NO_OUTPUT)
2001 revs->diff = 1;
2002
0faf2da7
AL
2003 /* Pickaxe, diff-filter and rename following need diffs */
2004 if (revs->diffopt.pickaxe ||
2005 revs->diffopt.filter ||
2006 DIFF_OPT_TST(&revs->diffopt, FOLLOW_RENAMES))
b7bb760d
LT
2007 revs->diff = 1;
2008
9dad9d2e 2009 if (revs->topo_order)
53069686
JH
2010 revs->limited = 1;
2011
afe069d1
NTND
2012 if (revs->prune_data.nr) {
2013 diff_tree_setup_paths(revs->prune_data.raw, &revs->pruning);
750f7b66 2014 /* Can't prune commits with rename following: the paths change.. */
8f67f8ae 2015 if (!DIFF_OPT_TST(&revs->diffopt, FOLLOW_RENAMES))
53b2c823 2016 revs->prune = 1;
cd2bdc53 2017 if (!revs->full_diff)
afe069d1 2018 diff_tree_setup_paths(revs->prune_data.raw, &revs->diffopt);
8efdc326 2019 }
b4490059 2020 if (revs->combine_merges)
cd2bdc53 2021 revs->ignore_merges = 0;
cd2bdc53 2022 revs->diffopt.abbrev = revs->abbrev;
28452655 2023 diff_setup_done(&revs->diffopt);
8efdc326 2024
918d4e1c
JH
2025 grep_commit_pattern_type(GREP_PATTERN_TYPE_UNSPECIFIED,
2026 &revs->grep_filter);
0843acfd 2027 compile_grep_patterns(&revs->grep_filter);
bd95fcd3 2028
d56651c0
SP
2029 if (revs->reverse && revs->reflog_info)
2030 die("cannot combine --reverse with --walk-reflogs");
8bb65883 2031 if (revs->rewrite_parents && revs->children.name)
f35f5603 2032 die("cannot combine --parents and --children");
d56651c0 2033
7fefda5c
AS
2034 /*
2035 * Limitations on the graph functionality
2036 */
2037 if (revs->reverse && revs->graph)
2038 die("cannot combine --reverse with --graph");
2039
2040 if (revs->reflog_info && revs->graph)
2041 die("cannot combine --walk-reflogs with --graph");
baa6378f
JH
2042 if (!revs->reflog_info && revs->grep_filter.use_reflog_filter)
2043 die("cannot use --grep-reflog without --walk-reflogs");
7fefda5c 2044
ae563542
LT
2045 return left;
2046}
a4a88b2b 2047
f35f5603
JH
2048static void add_child(struct rev_info *revs, struct commit *parent, struct commit *child)
2049{
2050 struct commit_list *l = xcalloc(1, sizeof(*l));
2051
2052 l->item = child;
2053 l->next = add_decoration(&revs->children, &parent->object, l);
2054}
2055
d0af663e 2056static int remove_duplicate_parents(struct rev_info *revs, struct commit *commit)
6546b593 2057{
d0af663e 2058 struct treesame_state *ts = lookup_decoration(&revs->treesame, &commit->object);
6546b593
JH
2059 struct commit_list **pp, *p;
2060 int surviving_parents;
2061
2062 /* Examine existing parents while marking ones we have seen... */
2063 pp = &commit->parents;
d0af663e 2064 surviving_parents = 0;
6546b593
JH
2065 while ((p = *pp) != NULL) {
2066 struct commit *parent = p->item;
2067 if (parent->object.flags & TMP_MARK) {
2068 *pp = p->next;
d0af663e
KB
2069 if (ts)
2070 compact_treesame(revs, commit, surviving_parents);
6546b593
JH
2071 continue;
2072 }
2073 parent->object.flags |= TMP_MARK;
d0af663e 2074 surviving_parents++;
6546b593
JH
2075 pp = &p->next;
2076 }
d0af663e 2077 /* clear the temporary mark */
6546b593
JH
2078 for (p = commit->parents; p; p = p->next) {
2079 p->item->object.flags &= ~TMP_MARK;
6546b593 2080 }
d0af663e 2081 /* no update_treesame() - removing duplicates can't affect TREESAME */
6546b593
JH
2082 return surviving_parents;
2083}
2084
faf0156b
JH
2085struct merge_simplify_state {
2086 struct commit *simplified;
2087};
2088
2089static struct merge_simplify_state *locate_simplify_state(struct rev_info *revs, struct commit *commit)
2090{
2091 struct merge_simplify_state *st;
2092
2093 st = lookup_decoration(&revs->merge_simplification, &commit->object);
2094 if (!st) {
2095 st = xcalloc(1, sizeof(*st));
2096 add_decoration(&revs->merge_simplification, &commit->object, st);
2097 }
2098 return st;
2099}
2100
d0af663e
KB
2101static int mark_redundant_parents(struct rev_info *revs, struct commit *commit)
2102{
2103 struct commit_list *h = reduce_heads(commit->parents);
2104 int i = 0, marked = 0;
2105 struct commit_list *po, *pn;
2106
2107 /* Want these for sanity-checking only */
2108 int orig_cnt = commit_list_count(commit->parents);
2109 int cnt = commit_list_count(h);
2110
2111 /*
2112 * Not ready to remove items yet, just mark them for now, based
2113 * on the output of reduce_heads(). reduce_heads outputs the reduced
2114 * set in its original order, so this isn't too hard.
2115 */
2116 po = commit->parents;
2117 pn = h;
2118 while (po) {
2119 if (pn && po->item == pn->item) {
2120 pn = pn->next;
2121 i++;
2122 } else {
2123 po->item->object.flags |= TMP_MARK;
2124 marked++;
2125 }
2126 po=po->next;
2127 }
2128
2129 if (i != cnt || cnt+marked != orig_cnt)
2130 die("mark_redundant_parents %d %d %d %d", orig_cnt, cnt, i, marked);
2131
2132 free_commit_list(h);
2133
2134 return marked;
2135}
2136
143f1eaf
KB
2137static int mark_treesame_root_parents(struct rev_info *revs, struct commit *commit)
2138{
2139 struct commit_list *p;
2140 int marked = 0;
2141
2142 for (p = commit->parents; p; p = p->next) {
2143 struct commit *parent = p->item;
2144 if (!parent->parents && (parent->object.flags & TREESAME)) {
2145 parent->object.flags |= TMP_MARK;
2146 marked++;
2147 }
2148 }
2149
2150 return marked;
2151}
2152
9c129eab
KB
2153/*
2154 * Awkward naming - this means one parent we are TREESAME to.
2155 * cf mark_treesame_root_parents: root parents that are TREESAME (to an
2156 * empty tree). Better name suggestions?
2157 */
2158static int leave_one_treesame_to_parent(struct rev_info *revs, struct commit *commit)
2159{
2160 struct treesame_state *ts = lookup_decoration(&revs->treesame, &commit->object);
2161 struct commit *unmarked = NULL, *marked = NULL;
2162 struct commit_list *p;
2163 unsigned n;
2164
2165 for (p = commit->parents, n = 0; p; p = p->next, n++) {
2166 if (ts->treesame[n]) {
2167 if (p->item->object.flags & TMP_MARK) {
2168 if (!marked)
2169 marked = p->item;
2170 } else {
2171 if (!unmarked) {
2172 unmarked = p->item;
2173 break;
2174 }
2175 }
2176 }
2177 }
2178
2179 /*
2180 * If we are TREESAME to a marked-for-deletion parent, but not to any
2181 * unmarked parents, unmark the first TREESAME parent. This is the
2182 * parent that the default simplify_history==1 scan would have followed,
2183 * and it doesn't make sense to omit that path when asking for a
2184 * simplified full history. Retaining it improves the chances of
2185 * understanding odd missed merges that took an old version of a file.
2186 *
2187 * Example:
2188 *
2189 * I--------*X A modified the file, but mainline merge X used
2190 * \ / "-s ours", so took the version from I. X is
2191 * `-*A--' TREESAME to I and !TREESAME to A.
2192 *
2193 * Default log from X would produce "I". Without this check,
2194 * --full-history --simplify-merges would produce "I-A-X", showing
2195 * the merge commit X and that it changed A, but not making clear that
2196 * it had just taken the I version. With this check, the topology above
2197 * is retained.
2198 *
2199 * Note that it is possible that the simplification chooses a different
2200 * TREESAME parent from the default, in which case this test doesn't
2201 * activate, and we _do_ drop the default parent. Example:
2202 *
2203 * I------X A modified the file, but it was reverted in B,
2204 * \ / meaning mainline merge X is TREESAME to both
2205 * *A-*B parents.
2206 *
2207 * Default log would produce "I" by following the first parent;
2208 * --full-history --simplify-merges will produce "I-A-B". But this is a
2209 * reasonable result - it presents a logical full history leading from
2210 * I to X, and X is not an important merge.
2211 */
2212 if (!unmarked && marked) {
2213 marked->object.flags &= ~TMP_MARK;
2214 return 1;
2215 }
2216
2217 return 0;
2218}
2219
d0af663e
KB
2220static int remove_marked_parents(struct rev_info *revs, struct commit *commit)
2221{
2222 struct commit_list **pp, *p;
2223 int nth_parent, removed = 0;
2224
2225 pp = &commit->parents;
2226 nth_parent = 0;
2227 while ((p = *pp) != NULL) {
2228 struct commit *parent = p->item;
2229 if (parent->object.flags & TMP_MARK) {
2230 parent->object.flags &= ~TMP_MARK;
2231 *pp = p->next;
2232 free(p);
2233 removed++;
2234 compact_treesame(revs, commit, nth_parent);
2235 continue;
2236 }
2237 pp = &p->next;
2238 nth_parent++;
2239 }
2240
2241 /* Removing parents can only increase TREESAMEness */
2242 if (removed && !(commit->object.flags & TREESAME))
2243 update_treesame(revs, commit);
2244
2245 return nth_parent;
2246}
2247
faf0156b 2248static struct commit_list **simplify_one(struct rev_info *revs, struct commit *commit, struct commit_list **tail)
6546b593
JH
2249{
2250 struct commit_list *p;
faf0156b 2251 struct merge_simplify_state *st, *pst;
6546b593
JH
2252 int cnt;
2253
faf0156b
JH
2254 st = locate_simplify_state(revs, commit);
2255
6546b593 2256 /*
6546b593
JH
2257 * Have we handled this one?
2258 */
faf0156b 2259 if (st->simplified)
6546b593
JH
2260 return tail;
2261
2262 /*
2263 * An UNINTERESTING commit simplifies to itself, so does a
2264 * root commit. We do not rewrite parents of such commit
2265 * anyway.
2266 */
2267 if ((commit->object.flags & UNINTERESTING) || !commit->parents) {
faf0156b 2268 st->simplified = commit;
6546b593
JH
2269 return tail;
2270 }
2271
2272 /*
6e513ba3
JH
2273 * Do we know what commit all of our parents that matter
2274 * should be rewritten to? Otherwise we are not ready to
2275 * rewrite this one yet.
6546b593
JH
2276 */
2277 for (cnt = 0, p = commit->parents; p; p = p->next) {
faf0156b
JH
2278 pst = locate_simplify_state(revs, p->item);
2279 if (!pst->simplified) {
6546b593
JH
2280 tail = &commit_list_insert(p->item, tail)->next;
2281 cnt++;
2282 }
6e513ba3
JH
2283 if (revs->first_parent_only)
2284 break;
6546b593 2285 }
53030f8d
JH
2286 if (cnt) {
2287 tail = &commit_list_insert(commit, tail)->next;
6546b593 2288 return tail;
53030f8d 2289 }
6546b593
JH
2290
2291 /*
d0af663e
KB
2292 * Rewrite our list of parents. Note that this cannot
2293 * affect our TREESAME flags in any way - a commit is
2294 * always TREESAME to its simplification.
6546b593 2295 */
faf0156b
JH
2296 for (p = commit->parents; p; p = p->next) {
2297 pst = locate_simplify_state(revs, p->item);
2298 p->item = pst->simplified;
6e513ba3
JH
2299 if (revs->first_parent_only)
2300 break;
faf0156b 2301 }
4b7f53da 2302
0290bf12 2303 if (revs->first_parent_only)
6e513ba3 2304 cnt = 1;
0290bf12 2305 else
d0af663e 2306 cnt = remove_duplicate_parents(revs, commit);
6546b593
JH
2307
2308 /*
2309 * It is possible that we are a merge and one side branch
2310 * does not have any commit that touches the given paths;
d0af663e
KB
2311 * in such a case, the immediate parent from that branch
2312 * will be rewritten to be the merge base.
6546b593
JH
2313 *
2314 * o----X X: the commit we are looking at;
2315 * / / o: a commit that touches the paths;
2316 * ---o----'
2317 *
143f1eaf
KB
2318 * Further, a merge of an independent branch that doesn't
2319 * touch the path will reduce to a treesame root parent:
2320 *
2321 * ----o----X X: the commit we are looking at;
2322 * / o: a commit that touches the paths;
2323 * r r: a root commit not touching the paths
2324 *
2325 * Detect and simplify both cases.
6546b593
JH
2326 */
2327 if (1 < cnt) {
d0af663e 2328 int marked = mark_redundant_parents(revs, commit);
143f1eaf 2329 marked += mark_treesame_root_parents(revs, commit);
9c129eab
KB
2330 if (marked)
2331 marked -= leave_one_treesame_to_parent(revs, commit);
d0af663e
KB
2332 if (marked)
2333 cnt = remove_marked_parents(revs, commit);
6546b593
JH
2334 }
2335
2336 /*
2337 * A commit simplifies to itself if it is a root, if it is
2338 * UNINTERESTING, if it touches the given paths, or if it is a
d0af663e 2339 * merge and its parents simplify to more than one commit
6546b593
JH
2340 * (the first two cases are already handled at the beginning of
2341 * this function).
2342 *
2343 * Otherwise, it simplifies to what its sole parent simplifies to.
2344 */
2345 if (!cnt ||
2346 (commit->object.flags & UNINTERESTING) ||
2347 !(commit->object.flags & TREESAME) ||
2348 (1 < cnt))
faf0156b
JH
2349 st->simplified = commit;
2350 else {
2351 pst = locate_simplify_state(revs, commit->parents->item);
2352 st->simplified = pst->simplified;
2353 }
6546b593
JH
2354 return tail;
2355}
2356
2357static void simplify_merges(struct rev_info *revs)
2358{
ab9d75a8 2359 struct commit_list *list, *next;
6546b593 2360 struct commit_list *yet_to_do, **tail;
ab9d75a8 2361 struct commit *commit;
6546b593 2362
5eac739e
JH
2363 if (!revs->prune)
2364 return;
65347030 2365
6546b593
JH
2366 /* feed the list reversed */
2367 yet_to_do = NULL;
ab9d75a8
JH
2368 for (list = revs->commits; list; list = next) {
2369 commit = list->item;
2370 next = list->next;
2371 /*
2372 * Do not free(list) here yet; the original list
2373 * is used later in this function.
2374 */
2375 commit_list_insert(commit, &yet_to_do);
2376 }
6546b593
JH
2377 while (yet_to_do) {
2378 list = yet_to_do;
2379 yet_to_do = NULL;
2380 tail = &yet_to_do;
2381 while (list) {
ab9d75a8
JH
2382 commit = list->item;
2383 next = list->next;
6546b593
JH
2384 free(list);
2385 list = next;
faf0156b 2386 tail = simplify_one(revs, commit, tail);
6546b593
JH
2387 }
2388 }
2389
2390 /* clean up the result, removing the simplified ones */
2391 list = revs->commits;
2392 revs->commits = NULL;
2393 tail = &revs->commits;
2394 while (list) {
faf0156b 2395 struct merge_simplify_state *st;
ab9d75a8
JH
2396
2397 commit = list->item;
2398 next = list->next;
6546b593
JH
2399 free(list);
2400 list = next;
faf0156b
JH
2401 st = locate_simplify_state(revs, commit);
2402 if (st->simplified == commit)
6546b593
JH
2403 tail = &commit_list_insert(commit, tail)->next;
2404 }
6546b593
JH
2405}
2406
f35f5603
JH
2407static void set_children(struct rev_info *revs)
2408{
2409 struct commit_list *l;
2410 for (l = revs->commits; l; l = l->next) {
2411 struct commit *commit = l->item;
2412 struct commit_list *p;
2413
2414 for (p = commit->parents; p; p = p->next)
2415 add_child(revs, p->item, commit);
2416 }
2417}
2418
bcc0a3ea
HV
2419void reset_revision_walk(void)
2420{
2421 clear_object_flags(SEEN | ADDED | SHOWN);
2422}
2423
cc0e6c5a 2424int prepare_revision_walk(struct rev_info *revs)
a4a88b2b 2425{
1f1e895f 2426 int nr = revs->pending.nr;
94d23673 2427 struct object_array_entry *e, *list;
2e7da8e9 2428 struct commit_list **next = &revs->commits;
cd2bdc53 2429
94d23673 2430 e = list = revs->pending.objects;
1f1e895f
LT
2431 revs->pending.nr = 0;
2432 revs->pending.alloc = 0;
2433 revs->pending.objects = NULL;
2434 while (--nr >= 0) {
94d23673 2435 struct commit *commit = handle_commit(revs, e->item, e->name);
cd2bdc53
LT
2436 if (commit) {
2437 if (!(commit->object.flags & SEEN)) {
2438 commit->object.flags |= SEEN;
2e7da8e9 2439 next = commit_list_append(commit, next);
cd2bdc53
LT
2440 }
2441 }
94d23673 2442 e++;
cd2bdc53 2443 }
4a43d374
RS
2444 if (!revs->leak_pending)
2445 free(list);
cd2bdc53 2446
d0af663e
KB
2447 /* Signal whether we need per-parent treesame decoration */
2448 if (revs->simplify_merges)
2449 revs->treesame.name = "treesame";
2450
ca92e59e
MZ
2451 if (revs->no_walk != REVISION_WALK_NO_WALK_UNSORTED)
2452 commit_list_sort_by_date(&revs->commits);
ba1d4505 2453 if (revs->no_walk)
cc0e6c5a 2454 return 0;
a4a88b2b 2455 if (revs->limited)
cc0e6c5a
AR
2456 if (limit_list(revs) < 0)
2457 return -1;
a4a88b2b 2458 if (revs->topo_order)
23c17d4a 2459 sort_in_topological_order(&revs->commits, revs->lifo);
6546b593
JH
2460 if (revs->simplify_merges)
2461 simplify_merges(revs);
f35f5603
JH
2462 if (revs->children.name)
2463 set_children(revs);
cc0e6c5a 2464 return 0;
a4a88b2b
LT
2465}
2466
cc0e6c5a
AR
2467enum rewrite_result {
2468 rewrite_one_ok,
2469 rewrite_one_noparents,
4b05548f 2470 rewrite_one_error
cc0e6c5a
AR
2471};
2472
2473static enum rewrite_result rewrite_one(struct rev_info *revs, struct commit **pp)
765ac8ec 2474{
fce87ae5
AG
2475 struct commit_list *cache = NULL;
2476
765ac8ec
LT
2477 for (;;) {
2478 struct commit *p = *pp;
3381c790 2479 if (!revs->limited)
fce87ae5 2480 if (add_parents_to_list(revs, p, &revs->commits, &cache) < 0)
cc0e6c5a 2481 return rewrite_one_error;
6631c736 2482 if (p->parents && p->parents->next)
cc0e6c5a 2483 return rewrite_one_ok;
7dc0fe3b
LT
2484 if (p->object.flags & UNINTERESTING)
2485 return rewrite_one_ok;
2486 if (!(p->object.flags & TREESAME))
cc0e6c5a 2487 return rewrite_one_ok;
765ac8ec 2488 if (!p->parents)
cc0e6c5a 2489 return rewrite_one_noparents;
765ac8ec
LT
2490 *pp = p->parents->item;
2491 }
2492}
2493
cc0e6c5a 2494static int rewrite_parents(struct rev_info *revs, struct commit *commit)
765ac8ec
LT
2495{
2496 struct commit_list **pp = &commit->parents;
2497 while (*pp) {
2498 struct commit_list *parent = *pp;
cc0e6c5a
AR
2499 switch (rewrite_one(revs, &parent->item)) {
2500 case rewrite_one_ok:
2501 break;
2502 case rewrite_one_noparents:
765ac8ec
LT
2503 *pp = parent->next;
2504 continue;
cc0e6c5a
AR
2505 case rewrite_one_error:
2506 return -1;
765ac8ec
LT
2507 }
2508 pp = &parent->next;
2509 }
d0af663e 2510 remove_duplicate_parents(revs, commit);
cc0e6c5a 2511 return 0;
765ac8ec
LT
2512}
2513
d72fbe81
AP
2514static int commit_rewrite_person(struct strbuf *buf, const char *what, struct string_list *mailmap)
2515{
2516 char *person, *endp;
2517 size_t len, namelen, maillen;
2518 const char *name;
2519 const char *mail;
2520 struct ident_split ident;
2521
2522 person = strstr(buf->buf, what);
2523 if (!person)
2524 return 0;
2525
2526 person += strlen(what);
2527 endp = strchr(person, '\n');
2528 if (!endp)
2529 return 0;
2530
2531 len = endp - person;
2532
2533 if (split_ident_line(&ident, person, len))
2534 return 0;
2535
2536 mail = ident.mail_begin;
2537 maillen = ident.mail_end - ident.mail_begin;
2538 name = ident.name_begin;
2539 namelen = ident.name_end - ident.name_begin;
2540
2541 if (map_user(mailmap, &mail, &maillen, &name, &namelen)) {
2542 struct strbuf namemail = STRBUF_INIT;
2543
2544 strbuf_addf(&namemail, "%.*s <%.*s>",
2545 (int)namelen, name, (int)maillen, mail);
2546
2547 strbuf_splice(buf, ident.name_begin - buf->buf,
2548 ident.mail_end - ident.name_begin + 1,
2549 namemail.buf, namemail.len);
2550
2551 strbuf_release(&namemail);
2552
2553 return 1;
2554 }
2555
2556 return 0;
2557}
2558
8ecae9b0
JH
2559static int commit_match(struct commit *commit, struct rev_info *opt)
2560{
72fd13f7 2561 int retval;
04deccda
JK
2562 const char *encoding;
2563 char *message;
72fd13f7 2564 struct strbuf buf = STRBUF_INIT;
04deccda 2565
80235ba7 2566 if (!opt->grep_filter.pattern_list && !opt->grep_filter.header_list)
8ecae9b0 2567 return 1;
baa6378f
JH
2568
2569 /* Prepend "fake" headers as needed */
2570 if (opt->grep_filter.use_reflog_filter) {
72fd13f7
NTND
2571 strbuf_addstr(&buf, "reflog ");
2572 get_reflog_message(&buf, opt->reflog_info);
2573 strbuf_addch(&buf, '\n');
72fd13f7 2574 }
baa6378f 2575
04deccda
JK
2576 /*
2577 * We grep in the user's output encoding, under the assumption that it
2578 * is the encoding they are most likely to write their grep pattern
2579 * for. In addition, it means we will match the "notes" encoding below,
2580 * so we will not end up with a buffer that has two different encodings
2581 * in it.
2582 */
2583 encoding = get_log_output_encoding();
5a10d236 2584 message = logmsg_reencode(commit, NULL, encoding);
04deccda 2585
baa6378f
JH
2586 /* Copy the commit to temporary if we are using "fake" headers */
2587 if (buf.len)
04deccda 2588 strbuf_addstr(&buf, message);
baa6378f 2589
df874fa8 2590 if (opt->grep_filter.header_list && opt->mailmap) {
d72fbe81 2591 if (!buf.len)
04deccda 2592 strbuf_addstr(&buf, message);
d72fbe81
AP
2593
2594 commit_rewrite_person(&buf, "\nauthor ", opt->mailmap);
2595 commit_rewrite_person(&buf, "\ncommitter ", opt->mailmap);
2596 }
2597
38cfe915
NTND
2598 /* Append "fake" message parts as needed */
2599 if (opt->show_notes) {
2600 if (!buf.len)
04deccda
JK
2601 strbuf_addstr(&buf, message);
2602 format_display_notes(commit->object.sha1, &buf, encoding, 1);
38cfe915
NTND
2603 }
2604
04deccda 2605 /* Find either in the original commit message, or in the temporary */
72fd13f7
NTND
2606 if (buf.len)
2607 retval = grep_buffer(&opt->grep_filter, buf.buf, buf.len);
2608 else
2609 retval = grep_buffer(&opt->grep_filter,
04deccda 2610 message, strlen(message));
72fd13f7 2611 strbuf_release(&buf);
04deccda 2612 logmsg_free(message, commit);
72fd13f7 2613 return retval;
8ecae9b0
JH
2614}
2615
f35f5603
JH
2616static inline int want_ancestry(struct rev_info *revs)
2617{
8bb65883 2618 return (revs->rewrite_parents || revs->children.name);
f35f5603
JH
2619}
2620
beb5af43 2621enum commit_action get_commit_action(struct rev_info *revs, struct commit *commit)
252a7c02
LT
2622{
2623 if (commit->object.flags & SHOWN)
2624 return commit_ignore;
4d6acb70 2625 if (revs->unpacked && has_sha1_pack(commit->object.sha1))
252a7c02 2626 return commit_ignore;
3131b713
LT
2627 if (revs->show_all)
2628 return commit_show;
252a7c02
LT
2629 if (commit->object.flags & UNINTERESTING)
2630 return commit_ignore;
2631 if (revs->min_age != -1 && (commit->date > revs->min_age))
2632 return commit_ignore;
ad5aeede
MG
2633 if (revs->min_parents || (revs->max_parents >= 0)) {
2634 int n = 0;
2635 struct commit_list *p;
2636 for (p = commit->parents; p; p = p->next)
2637 n++;
2638 if ((n < revs->min_parents) ||
2639 ((revs->max_parents >= 0) && (n > revs->max_parents)))
2640 return commit_ignore;
2641 }
252a7c02
LT
2642 if (!commit_match(commit, revs))
2643 return commit_ignore;
53b2c823 2644 if (revs->prune && revs->dense) {
252a7c02 2645 /* Commit without changes? */
7dc0fe3b 2646 if (commit->object.flags & TREESAME) {
252a7c02 2647 /* drop merges unless we want parenthood */
f35f5603 2648 if (!want_ancestry(revs))
252a7c02
LT
2649 return commit_ignore;
2650 /* non-merge - always ignore it */
2651 if (!commit->parents || !commit->parents->next)
2652 return commit_ignore;
2653 }
252a7c02
LT
2654 }
2655 return commit_show;
2656}
2657
beb5af43
AS
2658enum commit_action simplify_commit(struct rev_info *revs, struct commit *commit)
2659{
2660 enum commit_action action = get_commit_action(revs, commit);
2661
2662 if (action == commit_show &&
2663 !revs->show_all &&
2664 revs->prune && revs->dense && want_ancestry(revs)) {
2665 if (rewrite_parents(revs, commit) < 0)
2666 return commit_error;
2667 }
2668 return action;
2669}
2670
d5db6c9e 2671static struct commit *get_revision_1(struct rev_info *revs)
a4a88b2b 2672{
d5db6c9e 2673 if (!revs->commits)
a4a88b2b 2674 return NULL;
a4a88b2b 2675
765ac8ec 2676 do {
cb115748
LT
2677 struct commit_list *entry = revs->commits;
2678 struct commit *commit = entry->item;
ea5ed3ab 2679
cb115748
LT
2680 revs->commits = entry->next;
2681 free(entry);
2a0925be 2682
ffa1eeae 2683 if (revs->reflog_info) {
8860fd42 2684 fake_reflog_parent(revs->reflog_info, commit);
ffa1eeae
JK
2685 commit->object.flags &= ~(ADDED | SEEN | SHOWN);
2686 }
8860fd42 2687
2a0925be
LT
2688 /*
2689 * If we haven't done the list limiting, we need to look at
be7db6e5
LT
2690 * the parents here. We also need to do the date-based limiting
2691 * that we'd otherwise have done in limit_list().
2a0925be 2692 */
be7db6e5 2693 if (!revs->limited) {
744f4985 2694 if (revs->max_age != -1 &&
86ab4906
JH
2695 (commit->date < revs->max_age))
2696 continue;
fce87ae5 2697 if (add_parents_to_list(revs, commit, &revs->commits, NULL) < 0)
ed62089c
JH
2698 die("Failed to traverse parents of commit %s",
2699 sha1_to_hex(commit->object.sha1));
be7db6e5 2700 }
744f4985 2701
252a7c02
LT
2702 switch (simplify_commit(revs, commit)) {
2703 case commit_ignore:
8ecae9b0 2704 continue;
252a7c02 2705 case commit_error:
ed62089c
JH
2706 die("Failed to simplify parents of commit %s",
2707 sha1_to_hex(commit->object.sha1));
252a7c02
LT
2708 default:
2709 return commit;
384e99a4 2710 }
765ac8ec
LT
2711 } while (revs->commits);
2712 return NULL;
2713}
d5db6c9e 2714
86ab4906
JH
2715static void gc_boundary(struct object_array *array)
2716{
2717 unsigned nr = array->nr;
2718 unsigned alloc = array->alloc;
2719 struct object_array_entry *objects = array->objects;
2720
2721 if (alloc <= nr) {
2722 unsigned i, j;
2723 for (i = j = 0; i < nr; i++) {
2724 if (objects[i].item->flags & SHOWN)
2725 continue;
2726 if (i != j)
2727 objects[j] = objects[i];
2728 j++;
2729 }
892ae6bf 2730 for (i = j; i < nr; i++)
86ab4906
JH
2731 objects[i].item = NULL;
2732 array->nr = j;
2733 }
2734}
2735
4603ec0f
AS
2736static void create_boundary_commit_list(struct rev_info *revs)
2737{
2738 unsigned i;
2739 struct commit *c;
2740 struct object_array *array = &revs->boundary_commits;
2741 struct object_array_entry *objects = array->objects;
2742
2743 /*
2744 * If revs->commits is non-NULL at this point, an error occurred in
2745 * get_revision_1(). Ignore the error and continue printing the
2746 * boundary commits anyway. (This is what the code has always
2747 * done.)
2748 */
2749 if (revs->commits) {
2750 free_commit_list(revs->commits);
2751 revs->commits = NULL;
2752 }
2753
2754 /*
2755 * Put all of the actual boundary commits from revs->boundary_commits
2756 * into revs->commits
2757 */
2758 for (i = 0; i < array->nr; i++) {
2759 c = (struct commit *)(objects[i].item);
2760 if (!c)
2761 continue;
2762 if (!(c->object.flags & CHILD_SHOWN))
2763 continue;
2764 if (c->object.flags & (SHOWN | BOUNDARY))
2765 continue;
2766 c->object.flags |= BOUNDARY;
2767 commit_list_insert(c, &revs->commits);
2768 }
2769
2770 /*
2771 * If revs->topo_order is set, sort the boundary commits
2772 * in topological order
2773 */
2774 sort_in_topological_order(&revs->commits, revs->lifo);
2775}
2776
7fefda5c 2777static struct commit *get_revision_internal(struct rev_info *revs)
d5db6c9e
JH
2778{
2779 struct commit *c = NULL;
86ab4906
JH
2780 struct commit_list *l;
2781
2782 if (revs->boundary == 2) {
4603ec0f
AS
2783 /*
2784 * All of the normal commits have already been returned,
2785 * and we are now returning boundary commits.
2786 * create_boundary_commit_list() has populated
2787 * revs->commits with the remaining commits to return.
2788 */
2789 c = pop_commit(&revs->commits);
2790 if (c)
2791 c->object.flags |= SHOWN;
9c5e66e9
JS
2792 return c;
2793 }
2794
86ab4906 2795 /*
b72a1904
JK
2796 * If our max_count counter has reached zero, then we are done. We
2797 * don't simply return NULL because we still might need to show
2798 * boundary commits. But we want to avoid calling get_revision_1, which
2799 * might do a considerable amount of work finding the next commit only
2800 * for us to throw it away.
2801 *
2802 * If it is non-zero, then either we don't have a max_count at all
2803 * (-1), or it is still counting, in which case we decrement.
86ab4906 2804 */
b72a1904
JK
2805 if (revs->max_count) {
2806 c = get_revision_1(revs);
2807 if (c) {
2808 while (0 < revs->skip_count) {
2809 revs->skip_count--;
2810 c = get_revision_1(revs);
2811 if (!c)
2812 break;
2813 }
8839ac94 2814 }
86ab4906 2815
b72a1904
JK
2816 if (revs->max_count > 0)
2817 revs->max_count--;
d5db6c9e 2818 }
86ab4906 2819
c33d8593
JH
2820 if (c)
2821 c->object.flags |= SHOWN;
2822
2823 if (!revs->boundary) {
d5db6c9e 2824 return c;
c33d8593 2825 }
86ab4906
JH
2826
2827 if (!c) {
2828 /*
2829 * get_revision_1() runs out the commits, and
2830 * we are done computing the boundaries.
2831 * switch to boundary commits output mode.
2832 */
2833 revs->boundary = 2;
4603ec0f
AS
2834
2835 /*
2836 * Update revs->commits to contain the list of
2837 * boundary commits.
2838 */
2839 create_boundary_commit_list(revs);
2840
3c68d67b 2841 return get_revision_internal(revs);
86ab4906
JH
2842 }
2843
2844 /*
2845 * boundary commits are the commits that are parents of the
2846 * ones we got from get_revision_1() but they themselves are
2847 * not returned from get_revision_1(). Before returning
2848 * 'c', we need to mark its parents that they could be boundaries.
2849 */
2850
2851 for (l = c->parents; l; l = l->next) {
2852 struct object *p;
2853 p = &(l->item->object);
c33d8593 2854 if (p->flags & (CHILD_SHOWN | SHOWN))
86ab4906
JH
2855 continue;
2856 p->flags |= CHILD_SHOWN;
2857 gc_boundary(&revs->boundary_commits);
2858 add_object_array(p, NULL, &revs->boundary_commits);
2859 }
2860
2861 return c;
d5db6c9e 2862}
7fefda5c
AS
2863
2864struct commit *get_revision(struct rev_info *revs)
2865{
498bcd31
TR
2866 struct commit *c;
2867 struct commit_list *reversed;
2868
2869 if (revs->reverse) {
2870 reversed = NULL;
2871 while ((c = get_revision_internal(revs))) {
2872 commit_list_insert(c, &reversed);
2873 }
2874 revs->commits = reversed;
2875 revs->reverse = 0;
2876 revs->reverse_output_stage = 1;
2877 }
2878
2879 if (revs->reverse_output_stage)
2880 return pop_commit(&revs->commits);
2881
2882 c = get_revision_internal(revs);
7fefda5c
AS
2883 if (c && revs->graph)
2884 graph_update(revs->graph, c);
2885 return c;
2886}
1df2d656
MG
2887
2888char *get_revision_mark(const struct rev_info *revs, const struct commit *commit)
2889{
2890 if (commit->object.flags & BOUNDARY)
2891 return "-";
2892 else if (commit->object.flags & UNINTERESTING)
2893 return "^";
adbbb31e
MG
2894 else if (commit->object.flags & PATCHSAME)
2895 return "=";
1df2d656
MG
2896 else if (!revs || revs->left_right) {
2897 if (commit->object.flags & SYMMETRIC_LEFT)
2898 return "<";
2899 else
2900 return ">";
2901 } else if (revs->graph)
2902 return "*";
adbbb31e
MG
2903 else if (revs->cherry_mark)
2904 return "+";
1df2d656
MG
2905 return "";
2906}
b1b47554
MG
2907
2908void put_revision_mark(const struct rev_info *revs, const struct commit *commit)
2909{
2910 char *mark = get_revision_mark(revs, commit);
2911 if (!strlen(mark))
2912 return;
2913 fputs(mark, stdout);
2914 putchar(' ');
2915}