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