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