Add prefixcmp()
[git/git.git] / revision.c
CommitLineData
ae563542
LT
1#include "cache.h"
2#include "tag.h"
3#include "blob.h"
4#include "tree.h"
5#include "commit.h"
a4a88b2b 6#include "diff.h"
ae563542
LT
7#include "refs.h"
8#include "revision.h"
8ecae9b0 9#include "grep.h"
8860fd42 10#include "reflog-walk.h"
ae563542
LT
11
12static char *path_name(struct name_path *path, const char *name)
13{
14 struct name_path *p;
15 char *n, *m;
16 int nlen = strlen(name);
17 int len = nlen + 1;
18
19 for (p = path; p; p = p->up) {
20 if (p->elem_len)
21 len += p->elem_len + 1;
22 }
23 n = xmalloc(len);
24 m = n + len - (nlen + 1);
25 strcpy(m, name);
26 for (p = path; p; p = p->up) {
27 if (p->elem_len) {
28 m -= p->elem_len + 1;
29 memcpy(m, p->elem, p->elem_len);
30 m[p->elem_len] = '/';
31 }
32 }
33 return n;
34}
35
1f1e895f
LT
36void add_object(struct object *obj,
37 struct object_array *p,
38 struct name_path *path,
39 const char *name)
ae563542 40{
1f1e895f 41 add_object_array(obj, path_name(path, name), p);
ae563542
LT
42}
43
44static void mark_blob_uninteresting(struct blob *blob)
45{
46 if (blob->object.flags & UNINTERESTING)
47 return;
48 blob->object.flags |= UNINTERESTING;
49}
50
51void mark_tree_uninteresting(struct tree *tree)
52{
f75e53ed 53 struct tree_desc desc;
4c068a98 54 struct name_entry entry;
ae563542 55 struct object *obj = &tree->object;
ae563542
LT
56
57 if (obj->flags & UNINTERESTING)
58 return;
59 obj->flags |= UNINTERESTING;
60 if (!has_sha1_file(obj->sha1))
61 return;
62 if (parse_tree(tree) < 0)
63 die("bad tree %s", sha1_to_hex(obj->sha1));
f75e53ed
LT
64
65 desc.buf = tree->buffer;
66 desc.size = tree->size;
4c068a98
LT
67 while (tree_entry(&desc, &entry)) {
68 if (S_ISDIR(entry.mode))
69 mark_tree_uninteresting(lookup_tree(entry.sha1));
ae563542 70 else
4c068a98 71 mark_blob_uninteresting(lookup_blob(entry.sha1));
ae563542 72 }
f75e53ed
LT
73
74 /*
75 * We don't care about the tree any more
76 * after it has been marked uninteresting.
77 */
78 free(tree->buffer);
79 tree->buffer = NULL;
ae563542
LT
80}
81
82void mark_parents_uninteresting(struct commit *commit)
83{
84 struct commit_list *parents = commit->parents;
85
86 while (parents) {
87 struct commit *commit = parents->item;
d2c4af73
MU
88 if (!(commit->object.flags & UNINTERESTING)) {
89 commit->object.flags |= UNINTERESTING;
ae563542 90
d2c4af73
MU
91 /*
92 * Normally we haven't parsed the parent
93 * yet, so we won't have a parent of a parent
94 * here. However, it may turn out that we've
95 * reached this commit some other way (where it
96 * wasn't uninteresting), in which case we need
97 * to mark its parents recursively too..
98 */
99 if (commit->parents)
100 mark_parents_uninteresting(commit);
101 }
ae563542
LT
102
103 /*
104 * A missing commit is ok iff its parent is marked
105 * uninteresting.
106 *
107 * We just mark such a thing parsed, so that when
108 * it is popped next time around, we won't be trying
109 * to parse it and get an error.
110 */
111 if (!has_sha1_file(commit->object.sha1))
112 commit->object.parsed = 1;
113 parents = parents->next;
114 }
115}
116
1f1e895f 117void add_pending_object(struct rev_info *revs, struct object *obj, const char *name)
ae563542 118{
1f1e895f 119 add_object_array(obj, name, &revs->pending);
8860fd42
JS
120 if (revs->reflog_info && obj->type == OBJ_COMMIT)
121 add_reflog_for_walk(revs->reflog_info,
122 (struct commit *)obj, name);
ae563542
LT
123}
124
cd2bdc53 125static struct object *get_reference(struct rev_info *revs, const char *name, const unsigned char *sha1, unsigned int flags)
ae563542
LT
126{
127 struct object *object;
128
129 object = parse_object(sha1);
130 if (!object)
131 die("bad object %s", name);
cd2bdc53
LT
132 object->flags |= flags;
133 return object;
134}
135
136static struct commit *handle_commit(struct rev_info *revs, struct object *object, const char *name)
137{
138 unsigned long flags = object->flags;
ae563542
LT
139
140 /*
141 * Tag object? Look what it points to..
142 */
1974632c 143 while (object->type == OBJ_TAG) {
ae563542 144 struct tag *tag = (struct tag *) object;
cd2bdc53 145 if (revs->tag_objects && !(flags & UNINTERESTING))
ae563542
LT
146 add_pending_object(revs, object, tag->tag);
147 object = parse_object(tag->tagged->sha1);
148 if (!object)
149 die("bad object %s", sha1_to_hex(tag->tagged->sha1));
150 }
151
152 /*
153 * Commit object? Just return it, we'll do all the complex
154 * reachability crud.
155 */
1974632c 156 if (object->type == OBJ_COMMIT) {
ae563542 157 struct commit *commit = (struct commit *)object;
ae563542
LT
158 if (parse_commit(commit) < 0)
159 die("unable to parse commit %s", name);
d9a83684 160 if (flags & UNINTERESTING) {
4262c1b0 161 commit->object.flags |= UNINTERESTING;
ae563542 162 mark_parents_uninteresting(commit);
d9a83684
LT
163 revs->limited = 1;
164 }
ae563542
LT
165 return commit;
166 }
167
168 /*
169 * Tree object? Either mark it uniniteresting, or add it
170 * to the list of objects to look at later..
171 */
1974632c 172 if (object->type == OBJ_TREE) {
ae563542
LT
173 struct tree *tree = (struct tree *)object;
174 if (!revs->tree_objects)
175 return NULL;
176 if (flags & UNINTERESTING) {
177 mark_tree_uninteresting(tree);
178 return NULL;
179 }
180 add_pending_object(revs, object, "");
181 return NULL;
182 }
183
184 /*
185 * Blob object? You know the drill by now..
186 */
1974632c 187 if (object->type == OBJ_BLOB) {
ae563542
LT
188 struct blob *blob = (struct blob *)object;
189 if (!revs->blob_objects)
190 return NULL;
191 if (flags & UNINTERESTING) {
192 mark_blob_uninteresting(blob);
193 return NULL;
194 }
195 add_pending_object(revs, object, "");
196 return NULL;
197 }
198 die("%s is unknown object", name);
199}
200
a4a88b2b
LT
201static int everybody_uninteresting(struct commit_list *orig)
202{
203 struct commit_list *list = orig;
204 while (list) {
205 struct commit *commit = list->item;
206 list = list->next;
207 if (commit->object.flags & UNINTERESTING)
208 continue;
209 return 0;
210 }
211 return 1;
212}
213
8efdc326 214static int tree_difference = REV_TREE_SAME;
a4a88b2b
LT
215
216static void file_add_remove(struct diff_options *options,
217 int addremove, unsigned mode,
218 const unsigned char *sha1,
219 const char *base, const char *path)
220{
8efdc326 221 int diff = REV_TREE_DIFFERENT;
a4a88b2b
LT
222
223 /*
8efdc326
FK
224 * Is it an add of a new file? It means that the old tree
225 * didn't have it at all, so we will turn "REV_TREE_SAME" ->
226 * "REV_TREE_NEW", but leave any "REV_TREE_DIFFERENT" alone
227 * (and if it already was "REV_TREE_NEW", we'll keep it
228 * "REV_TREE_NEW" of course).
a4a88b2b
LT
229 */
230 if (addremove == '+') {
231 diff = tree_difference;
8efdc326 232 if (diff != REV_TREE_SAME)
a4a88b2b 233 return;
8efdc326 234 diff = REV_TREE_NEW;
a4a88b2b
LT
235 }
236 tree_difference = diff;
237}
238
239static void file_change(struct diff_options *options,
240 unsigned old_mode, unsigned new_mode,
241 const unsigned char *old_sha1,
242 const unsigned char *new_sha1,
243 const char *base, const char *path)
244{
8efdc326 245 tree_difference = REV_TREE_DIFFERENT;
a4a88b2b
LT
246}
247
c4e05b1a 248int rev_compare_tree(struct rev_info *revs, struct tree *t1, struct tree *t2)
a4a88b2b
LT
249{
250 if (!t1)
8efdc326 251 return REV_TREE_NEW;
a4a88b2b 252 if (!t2)
8efdc326
FK
253 return REV_TREE_DIFFERENT;
254 tree_difference = REV_TREE_SAME;
c4e05b1a 255 if (diff_tree_sha1(t1->object.sha1, t2->object.sha1, "",
cd2bdc53 256 &revs->pruning) < 0)
8efdc326 257 return REV_TREE_DIFFERENT;
a4a88b2b
LT
258 return tree_difference;
259}
260
c4e05b1a 261int rev_same_tree_as_empty(struct rev_info *revs, struct tree *t1)
a4a88b2b
LT
262{
263 int retval;
264 void *tree;
265 struct tree_desc empty, real;
266
267 if (!t1)
268 return 0;
269
8e440259 270 tree = read_object_with_reference(t1->object.sha1, tree_type, &real.size, NULL);
a4a88b2b
LT
271 if (!tree)
272 return 0;
273 real.buf = tree;
274
275 empty.buf = "";
276 empty.size = 0;
277
278 tree_difference = 0;
cd2bdc53 279 retval = diff_tree(&empty, &real, "", &revs->pruning);
a4a88b2b
LT
280 free(tree);
281
282 return retval >= 0 && !tree_difference;
283}
284
285static void try_to_simplify_commit(struct rev_info *revs, struct commit *commit)
286{
287 struct commit_list **pp, *parent;
6631c736 288 int tree_changed = 0, tree_same = 0;
a4a88b2b
LT
289
290 if (!commit->tree)
291 return;
292
293 if (!commit->parents) {
c4e05b1a 294 if (!rev_same_tree_as_empty(revs, commit->tree))
a4a88b2b
LT
295 commit->object.flags |= TREECHANGE;
296 return;
297 }
298
299 pp = &commit->parents;
300 while ((parent = *pp) != NULL) {
301 struct commit *p = parent->item;
302
a4a88b2b 303 parse_commit(p);
c4e05b1a 304 switch (rev_compare_tree(revs, p->tree, commit->tree)) {
8efdc326 305 case REV_TREE_SAME:
6631c736 306 tree_same = 1;
9202434c 307 if (!revs->simplify_history || (p->object.flags & UNINTERESTING)) {
f3219fbb
JH
308 /* Even if a merge with an uninteresting
309 * side branch brought the entire change
310 * we are interested in, we do not want
311 * to lose the other branches of this
312 * merge, so we just keep going.
313 */
314 pp = &parent->next;
315 continue;
316 }
a4a88b2b
LT
317 parent->next = NULL;
318 commit->parents = parent;
319 return;
320
8efdc326
FK
321 case REV_TREE_NEW:
322 if (revs->remove_empty_trees &&
c4e05b1a 323 rev_same_tree_as_empty(revs, p->tree)) {
c348f31a
JH
324 /* We are adding all the specified
325 * paths from this parent, so the
326 * history beyond this parent is not
327 * interesting. Remove its parents
328 * (they are grandparents for us).
329 * IOW, we pretend this parent is a
330 * "root" commit.
a41e109c 331 */
c348f31a
JH
332 parse_commit(p);
333 p->parents = NULL;
a4a88b2b
LT
334 }
335 /* fallthrough */
8efdc326 336 case REV_TREE_DIFFERENT:
f3219fbb 337 tree_changed = 1;
a4a88b2b
LT
338 pp = &parent->next;
339 continue;
340 }
341 die("bad tree compare for commit %s", sha1_to_hex(commit->object.sha1));
342 }
6631c736 343 if (tree_changed && !tree_same)
f3219fbb 344 commit->object.flags |= TREECHANGE;
a4a88b2b
LT
345}
346
347static void add_parents_to_list(struct rev_info *revs, struct commit *commit, struct commit_list **list)
348{
349 struct commit_list *parent = commit->parents;
577ed5c2 350 unsigned left_flag;
a4a88b2b 351
3381c790
LT
352 if (commit->object.flags & ADDED)
353 return;
354 commit->object.flags |= ADDED;
355
a4a88b2b
LT
356 /*
357 * If the commit is uninteresting, don't try to
358 * prune parents - we want the maximal uninteresting
359 * set.
360 *
361 * Normally we haven't parsed the parent
362 * yet, so we won't have a parent of a parent
363 * here. However, it may turn out that we've
364 * reached this commit some other way (where it
365 * wasn't uninteresting), in which case we need
366 * to mark its parents recursively too..
367 */
368 if (commit->object.flags & UNINTERESTING) {
369 while (parent) {
370 struct commit *p = parent->item;
371 parent = parent->next;
372 parse_commit(p);
373 p->object.flags |= UNINTERESTING;
374 if (p->parents)
375 mark_parents_uninteresting(p);
376 if (p->object.flags & SEEN)
377 continue;
378 p->object.flags |= SEEN;
379 insert_by_date(p, list);
380 }
381 return;
382 }
383
384 /*
385 * Ok, the commit wasn't uninteresting. Try to
386 * simplify the commit history and find the parent
387 * that has no differences in the path set if one exists.
388 */
8efdc326
FK
389 if (revs->prune_fn)
390 revs->prune_fn(revs, commit);
a4a88b2b 391
ba1d4505
LT
392 if (revs->no_walk)
393 return;
394
577ed5c2 395 left_flag = (commit->object.flags & SYMMETRIC_LEFT);
a4a88b2b
LT
396 parent = commit->parents;
397 while (parent) {
398 struct commit *p = parent->item;
399
400 parent = parent->next;
401
402 parse_commit(p);
577ed5c2 403 p->object.flags |= left_flag;
a4a88b2b
LT
404 if (p->object.flags & SEEN)
405 continue;
406 p->object.flags |= SEEN;
407 insert_by_date(p, list);
408 }
409}
410
411static void limit_list(struct rev_info *revs)
412{
413 struct commit_list *list = revs->commits;
414 struct commit_list *newlist = NULL;
415 struct commit_list **p = &newlist;
416
417 while (list) {
418 struct commit_list *entry = list;
419 struct commit *commit = list->item;
420 struct object *obj = &commit->object;
421
422 list = list->next;
423 free(entry);
424
425 if (revs->max_age != -1 && (commit->date < revs->max_age))
426 obj->flags |= UNINTERESTING;
a4a88b2b
LT
427 add_parents_to_list(revs, commit, &list);
428 if (obj->flags & UNINTERESTING) {
429 mark_parents_uninteresting(commit);
430 if (everybody_uninteresting(list))
431 break;
432 continue;
433 }
434 if (revs->min_age != -1 && (commit->date > revs->min_age))
435 continue;
436 p = &commit_list_insert(commit, p)->next;
437 }
384e99a4 438 if (revs->boundary) {
4c0fea0f
JH
439 /* mark the ones that are on the result list first */
440 for (list = newlist; list; list = list->next) {
441 struct commit *commit = list->item;
442 commit->object.flags |= TMP_MARK;
443 }
444 for (list = newlist; list; list = list->next) {
384e99a4
JH
445 struct commit *commit = list->item;
446 struct object *obj = &commit->object;
4c0fea0f
JH
447 struct commit_list *parent;
448 if (obj->flags & UNINTERESTING)
384e99a4 449 continue;
4c0fea0f
JH
450 for (parent = commit->parents;
451 parent;
452 parent = parent->next) {
384e99a4 453 struct commit *pcommit = parent->item;
384e99a4
JH
454 if (!(pcommit->object.flags & UNINTERESTING))
455 continue;
456 pcommit->object.flags |= BOUNDARY;
4c0fea0f
JH
457 if (pcommit->object.flags & TMP_MARK)
458 continue;
459 pcommit->object.flags |= TMP_MARK;
384e99a4
JH
460 p = &commit_list_insert(pcommit, p)->next;
461 }
4c0fea0f
JH
462 }
463 for (list = newlist; list; list = list->next) {
464 struct commit *commit = list->item;
465 commit->object.flags &= ~TMP_MARK;
384e99a4
JH
466 }
467 }
a4a88b2b
LT
468 revs->commits = newlist;
469}
470
63049292
JH
471struct all_refs_cb {
472 int all_flags;
71b03b42 473 int warned_bad_reflog;
63049292
JH
474 struct rev_info *all_revs;
475 const char *name_for_errormsg;
476};
ae563542 477
8da19775 478static int handle_one_ref(const char *path, const unsigned char *sha1, int flag, void *cb_data)
ae563542 479{
63049292
JH
480 struct all_refs_cb *cb = cb_data;
481 struct object *object = get_reference(cb->all_revs, path, sha1,
482 cb->all_flags);
483 add_pending_object(cb->all_revs, object, "");
ae563542
LT
484 return 0;
485}
486
487static void handle_all(struct rev_info *revs, unsigned flags)
488{
63049292
JH
489 struct all_refs_cb cb;
490 cb.all_revs = revs;
491 cb.all_flags = flags;
492 for_each_ref(handle_one_ref, &cb);
493}
494
71b03b42 495static void handle_one_reflog_commit(unsigned char *sha1, void *cb_data)
63049292
JH
496{
497 struct all_refs_cb *cb = cb_data;
71b03b42
SP
498 if (!is_null_sha1(sha1)) {
499 struct object *o = parse_object(sha1);
500 if (o) {
501 o->flags |= cb->all_flags;
502 add_pending_object(cb->all_revs, o, "");
503 }
504 else if (!cb->warned_bad_reflog) {
505 warn("reflog of '%s' references pruned commits",
506 cb->name_for_errormsg);
507 cb->warned_bad_reflog = 1;
508 }
63049292 509 }
71b03b42
SP
510}
511
883d60fa
JS
512static int handle_one_reflog_ent(unsigned char *osha1, unsigned char *nsha1,
513 const char *email, unsigned long timestamp, int tz,
514 const char *message, void *cb_data)
71b03b42
SP
515{
516 handle_one_reflog_commit(osha1, cb_data);
517 handle_one_reflog_commit(nsha1, cb_data);
63049292
JH
518 return 0;
519}
520
521static int handle_one_reflog(const char *path, const unsigned char *sha1, int flag, void *cb_data)
522{
523 struct all_refs_cb *cb = cb_data;
71b03b42 524 cb->warned_bad_reflog = 0;
63049292
JH
525 cb->name_for_errormsg = path;
526 for_each_reflog_ent(path, handle_one_reflog_ent, cb_data);
527 return 0;
528}
529
530static void handle_reflog(struct rev_info *revs, unsigned flags)
531{
532 struct all_refs_cb cb;
533 cb.all_revs = revs;
534 cb.all_flags = flags;
25b51e2c 535 for_each_reflog(handle_one_reflog, &cb);
ae563542
LT
536}
537
ea4a19e1
JH
538static int add_parents_only(struct rev_info *revs, const char *arg, int flags)
539{
540 unsigned char sha1[20];
541 struct object *it;
542 struct commit *commit;
543 struct commit_list *parents;
544
545 if (*arg == '^') {
546 flags ^= UNINTERESTING;
547 arg++;
548 }
549 if (get_sha1(arg, sha1))
550 return 0;
551 while (1) {
552 it = get_reference(revs, arg, sha1, 0);
1974632c 553 if (it->type != OBJ_TAG)
ea4a19e1 554 break;
e702496e 555 hashcpy(sha1, ((struct tag*)it)->tagged->sha1);
ea4a19e1 556 }
1974632c 557 if (it->type != OBJ_COMMIT)
ea4a19e1
JH
558 return 0;
559 commit = (struct commit *)it;
560 for (parents = commit->parents; parents; parents = parents->next) {
561 it = &parents->item->object;
562 it->flags |= flags;
563 add_pending_object(revs, it, arg);
564 }
565 return 1;
566}
567
db6296a5 568void init_revisions(struct rev_info *revs, const char *prefix)
8efdc326
FK
569{
570 memset(revs, 0, sizeof(*revs));
8e8f9987 571
6b9c58f4 572 revs->abbrev = DEFAULT_ABBREV;
8e8f9987 573 revs->ignore_merges = 1;
9202434c 574 revs->simplify_history = 1;
cd2bdc53
LT
575 revs->pruning.recursive = 1;
576 revs->pruning.add_remove = file_add_remove;
577 revs->pruning.change = file_change;
8efdc326
FK
578 revs->lifo = 1;
579 revs->dense = 1;
db6296a5 580 revs->prefix = prefix;
8efdc326
FK
581 revs->max_age = -1;
582 revs->min_age = -1;
d5db6c9e 583 revs->skip_count = -1;
8efdc326
FK
584 revs->max_count = -1;
585
586 revs->prune_fn = NULL;
587 revs->prune_data = NULL;
588
589 revs->topo_setter = topo_sort_default_setter;
590 revs->topo_getter = topo_sort_default_getter;
cd2bdc53 591
cd2bdc53
LT
592 revs->commit_format = CMIT_FMT_DEFAULT;
593
594 diff_setup(&revs->diffopt);
8efdc326
FK
595}
596
0d2c9d67
RS
597static void add_pending_commit_list(struct rev_info *revs,
598 struct commit_list *commit_list,
599 unsigned int flags)
600{
601 while (commit_list) {
602 struct object *object = &commit_list->item->object;
603 object->flags |= flags;
604 add_pending_object(revs, object, sha1_to_hex(object->sha1));
605 commit_list = commit_list->next;
606 }
607}
608
ae3e5e1e
JH
609static void prepare_show_merge(struct rev_info *revs)
610{
611 struct commit_list *bases;
612 struct commit *head, *other;
613 unsigned char sha1[20];
614 const char **prune = NULL;
615 int i, prune_num = 1; /* counting terminating NULL */
616
617 if (get_sha1("HEAD", sha1) || !(head = lookup_commit(sha1)))
618 die("--merge without HEAD?");
619 if (get_sha1("MERGE_HEAD", sha1) || !(other = lookup_commit(sha1)))
620 die("--merge without MERGE_HEAD?");
621 add_pending_object(revs, &head->object, "HEAD");
622 add_pending_object(revs, &other->object, "MERGE_HEAD");
623 bases = get_merge_bases(head, other, 1);
624 while (bases) {
625 struct commit *it = bases->item;
626 struct commit_list *n = bases->next;
627 free(bases);
628 bases = n;
629 it->object.flags |= UNINTERESTING;
630 add_pending_object(revs, &it->object, "(merge-base)");
631 }
632
633 if (!active_nr)
634 read_cache();
635 for (i = 0; i < active_nr; i++) {
636 struct cache_entry *ce = active_cache[i];
637 if (!ce_stage(ce))
638 continue;
639 if (ce_path_match(ce, revs->prune_data)) {
640 prune_num++;
641 prune = xrealloc(prune, sizeof(*prune) * prune_num);
642 prune[prune_num-2] = ce->name;
643 prune[prune_num-1] = NULL;
644 }
645 while ((i+1 < active_nr) &&
646 ce_same_name(ce, active_cache[i+1]))
647 i++;
648 }
649 revs->prune_data = prune;
650}
651
5d6f0935
JH
652int handle_revision_arg(const char *arg, struct rev_info *revs,
653 int flags,
654 int cant_be_filename)
655{
656 char *dotdot;
657 struct object *object;
658 unsigned char sha1[20];
659 int local_flags;
660
661 dotdot = strstr(arg, "..");
662 if (dotdot) {
663 unsigned char from_sha1[20];
664 const char *next = dotdot + 2;
665 const char *this = arg;
666 int symmetric = *next == '.';
667 unsigned int flags_exclude = flags ^ UNINTERESTING;
668
669 *dotdot = 0;
670 next += symmetric;
671
672 if (!*next)
673 next = "HEAD";
674 if (dotdot == arg)
675 this = "HEAD";
676 if (!get_sha1(this, from_sha1) &&
677 !get_sha1(next, sha1)) {
678 struct commit *a, *b;
679 struct commit_list *exclude;
680
681 a = lookup_commit_reference(from_sha1);
682 b = lookup_commit_reference(sha1);
683 if (!a || !b) {
684 die(symmetric ?
685 "Invalid symmetric difference expression %s...%s" :
686 "Invalid revision range %s..%s",
687 arg, next);
688 }
689
690 if (!cant_be_filename) {
691 *dotdot = '.';
692 verify_non_filename(revs->prefix, arg);
693 }
694
695 if (symmetric) {
696 exclude = get_merge_bases(a, b, 1);
697 add_pending_commit_list(revs, exclude,
698 flags_exclude);
699 free_commit_list(exclude);
577ed5c2 700 a->object.flags |= flags | SYMMETRIC_LEFT;
5d6f0935
JH
701 } else
702 a->object.flags |= flags_exclude;
703 b->object.flags |= flags;
704 add_pending_object(revs, &a->object, this);
705 add_pending_object(revs, &b->object, next);
706 return 0;
707 }
708 *dotdot = '.';
709 }
710 dotdot = strstr(arg, "^@");
711 if (dotdot && !dotdot[2]) {
712 *dotdot = 0;
713 if (add_parents_only(revs, arg, flags))
714 return 0;
715 *dotdot = '^';
716 }
62476c8e
JH
717 dotdot = strstr(arg, "^!");
718 if (dotdot && !dotdot[2]) {
719 *dotdot = 0;
720 if (!add_parents_only(revs, arg, flags ^ UNINTERESTING))
721 *dotdot = '^';
722 }
723
5d6f0935
JH
724 local_flags = 0;
725 if (*arg == '^') {
726 local_flags = UNINTERESTING;
727 arg++;
728 }
729 if (get_sha1(arg, sha1))
730 return -1;
731 if (!cant_be_filename)
732 verify_non_filename(revs->prefix, arg);
733 object = get_reference(revs, arg, sha1, flags ^ local_flags);
734 add_pending_object(revs, object, arg);
735 return 0;
736}
737
2d10c555 738static void add_grep(struct rev_info *revs, const char *ptn, enum grep_pat_token what)
bd95fcd3 739{
2d10c555 740 if (!revs->grep_filter) {
bd95fcd3
JH
741 struct grep_opt *opt = xcalloc(1, sizeof(*opt));
742 opt->status_only = 1;
743 opt->pattern_tail = &(opt->pattern_list);
744 opt->regflags = REG_NEWLINE;
2d10c555 745 revs->grep_filter = opt;
bd95fcd3 746 }
2d10c555
JH
747 append_grep_pattern(revs->grep_filter, ptn,
748 "command line", 0, what);
749}
750
751static void add_header_grep(struct rev_info *revs, const char *field, const char *pattern)
752{
753 char *pat;
754 const char *prefix;
755 int patlen, fldlen;
bd95fcd3
JH
756
757 fldlen = strlen(field);
758 patlen = strlen(pattern);
a2ed6ae4
LT
759 pat = xmalloc(patlen + fldlen + 10);
760 prefix = ".*";
761 if (*pattern == '^') {
762 prefix = "";
763 pattern++;
764 }
765 sprintf(pat, "^%s %s%s", field, prefix, pattern);
2d10c555 766 add_grep(revs, pat, GREP_PATTERN_HEAD);
bd95fcd3
JH
767}
768
769static void add_message_grep(struct rev_info *revs, const char *pattern)
770{
2d10c555 771 add_grep(revs, pattern, GREP_PATTERN_BODY);
bd95fcd3
JH
772}
773
106d710b
JH
774static void add_ignore_packed(struct rev_info *revs, const char *name)
775{
776 int num = ++revs->num_ignore_packed;
777
778 revs->ignore_packed = xrealloc(revs->ignore_packed,
779 sizeof(const char **) * (num + 1));
780 revs->ignore_packed[num-1] = name;
781 revs->ignore_packed[num] = NULL;
782}
783
ae563542
LT
784/*
785 * Parse revision information, filling in the "rev_info" structure,
786 * and removing the used arguments from the argument list.
787 *
765ac8ec
LT
788 * Returns the number of arguments left that weren't recognized
789 * (which are also moved to the head of the argument list)
ae563542 790 */
a4a88b2b 791int setup_revisions(int argc, const char **argv, struct rev_info *revs, const char *def)
ae563542 792{
ae3e5e1e 793 int i, flags, seen_dashdash, show_merge;
765ac8ec 794 const char **unrecognized = argv + 1;
ae563542 795 int left = 1;
70d0afba 796 int all_match = 0;
ae563542 797
ae563542
LT
798 /* First, search for "--" */
799 seen_dashdash = 0;
800 for (i = 1; i < argc; i++) {
801 const char *arg = argv[i];
802 if (strcmp(arg, "--"))
803 continue;
804 argv[i] = NULL;
805 argc = i;
8efdc326 806 revs->prune_data = get_pathspec(revs->prefix, argv + i + 1);
ae563542
LT
807 seen_dashdash = 1;
808 break;
809 }
810
ae3e5e1e 811 flags = show_merge = 0;
ae563542 812 for (i = 1; i < argc; i++) {
ae563542 813 const char *arg = argv[i];
ae563542 814 if (*arg == '-') {
cd2bdc53 815 int opts;
ae563542
LT
816 if (!strncmp(arg, "--max-count=", 12)) {
817 revs->max_count = atoi(arg + 12);
818 continue;
819 }
d5db6c9e
JH
820 if (!strncmp(arg, "--skip=", 7)) {
821 revs->skip_count = atoi(arg + 7);
822 continue;
823 }
de5f2bf3 824 /* accept -<digit>, like traditional "head" */
64bc6e3d
JH
825 if ((*arg == '-') && isdigit(arg[1])) {
826 revs->max_count = atoi(arg + 1);
827 continue;
828 }
829 if (!strcmp(arg, "-n")) {
830 if (argc <= i + 1)
831 die("-n requires an argument");
832 revs->max_count = atoi(argv[++i]);
833 continue;
834 }
835 if (!strncmp(arg,"-n",2)) {
836 revs->max_count = atoi(arg + 2);
837 continue;
838 }
ae563542
LT
839 if (!strncmp(arg, "--max-age=", 10)) {
840 revs->max_age = atoi(arg + 10);
ae563542
LT
841 continue;
842 }
fd751667
JH
843 if (!strncmp(arg, "--since=", 8)) {
844 revs->max_age = approxidate(arg + 8);
fd751667
JH
845 continue;
846 }
847 if (!strncmp(arg, "--after=", 8)) {
848 revs->max_age = approxidate(arg + 8);
53069686
JH
849 continue;
850 }
851 if (!strncmp(arg, "--min-age=", 10)) {
852 revs->min_age = atoi(arg + 10);
fd751667
JH
853 continue;
854 }
855 if (!strncmp(arg, "--before=", 9)) {
856 revs->min_age = approxidate(arg + 9);
fd751667
JH
857 continue;
858 }
859 if (!strncmp(arg, "--until=", 8)) {
860 revs->min_age = approxidate(arg + 8);
fd751667
JH
861 continue;
862 }
ae563542
LT
863 if (!strcmp(arg, "--all")) {
864 handle_all(revs, flags);
865 continue;
866 }
63049292
JH
867 if (!strcmp(arg, "--reflog")) {
868 handle_reflog(revs, flags);
869 continue;
870 }
084ae0a7
JS
871 if (!strcmp(arg, "-g") ||
872 !strcmp(arg, "--walk-reflogs")) {
8860fd42
JS
873 init_reflog_walk(&revs->reflog_info);
874 continue;
875 }
ae563542
LT
876 if (!strcmp(arg, "--not")) {
877 flags ^= UNINTERESTING;
878 continue;
879 }
880 if (!strcmp(arg, "--default")) {
881 if (++i >= argc)
882 die("bad --default argument");
883 def = argv[i];
884 continue;
885 }
ae3e5e1e
JH
886 if (!strcmp(arg, "--merge")) {
887 show_merge = 1;
888 continue;
889 }
ae563542
LT
890 if (!strcmp(arg, "--topo-order")) {
891 revs->topo_order = 1;
892 continue;
893 }
894 if (!strcmp(arg, "--date-order")) {
895 revs->lifo = 0;
896 revs->topo_order = 1;
897 continue;
898 }
7b0c9966
LT
899 if (!strcmp(arg, "--parents")) {
900 revs->parents = 1;
901 continue;
902 }
ae563542
LT
903 if (!strcmp(arg, "--dense")) {
904 revs->dense = 1;
905 continue;
906 }
907 if (!strcmp(arg, "--sparse")) {
908 revs->dense = 0;
909 continue;
910 }
911 if (!strcmp(arg, "--remove-empty")) {
912 revs->remove_empty_trees = 1;
913 continue;
914 }
5cdeae71 915 if (!strcmp(arg, "--no-merges")) {
765ac8ec
LT
916 revs->no_merges = 1;
917 continue;
918 }
384e99a4
JH
919 if (!strcmp(arg, "--boundary")) {
920 revs->boundary = 1;
921 continue;
922 }
8dce8235
JH
923 if (!strcmp(arg, "--left-right")) {
924 revs->left_right = 1;
74bd9029
JH
925 continue;
926 }
ae563542
LT
927 if (!strcmp(arg, "--objects")) {
928 revs->tag_objects = 1;
929 revs->tree_objects = 1;
930 revs->blob_objects = 1;
931 continue;
932 }
933 if (!strcmp(arg, "--objects-edge")) {
934 revs->tag_objects = 1;
935 revs->tree_objects = 1;
936 revs->blob_objects = 1;
937 revs->edge_hint = 1;
938 continue;
939 }
d9a83684
LT
940 if (!strcmp(arg, "--unpacked")) {
941 revs->unpacked = 1;
106d710b
JH
942 free(revs->ignore_packed);
943 revs->ignore_packed = NULL;
944 revs->num_ignore_packed = 0;
945 continue;
946 }
947 if (!strncmp(arg, "--unpacked=", 11)) {
948 revs->unpacked = 1;
949 add_ignore_packed(revs, arg+11);
d9a83684
LT
950 continue;
951 }
cd2bdc53
LT
952 if (!strcmp(arg, "-r")) {
953 revs->diff = 1;
954 revs->diffopt.recursive = 1;
955 continue;
956 }
957 if (!strcmp(arg, "-t")) {
958 revs->diff = 1;
959 revs->diffopt.recursive = 1;
960 revs->diffopt.tree_in_recursive = 1;
961 continue;
962 }
963 if (!strcmp(arg, "-m")) {
964 revs->ignore_merges = 0;
965 continue;
966 }
967 if (!strcmp(arg, "-c")) {
968 revs->diff = 1;
0fe7c1de 969 revs->dense_combined_merges = 0;
cd2bdc53
LT
970 revs->combine_merges = 1;
971 continue;
972 }
973 if (!strcmp(arg, "--cc")) {
974 revs->diff = 1;
975 revs->dense_combined_merges = 1;
976 revs->combine_merges = 1;
977 continue;
978 }
979 if (!strcmp(arg, "-v")) {
980 revs->verbose_header = 1;
cd2bdc53
LT
981 continue;
982 }
983 if (!strncmp(arg, "--pretty", 8)) {
984 revs->verbose_header = 1;
cd2bdc53
LT
985 revs->commit_format = get_commit_format(arg+8);
986 continue;
987 }
988 if (!strcmp(arg, "--root")) {
989 revs->show_root_diff = 1;
990 continue;
991 }
992 if (!strcmp(arg, "--no-commit-id")) {
993 revs->no_commit_id = 1;
994 continue;
995 }
996 if (!strcmp(arg, "--always")) {
997 revs->always_show_header = 1;
998 continue;
999 }
1000 if (!strcmp(arg, "--no-abbrev")) {
1001 revs->abbrev = 0;
1002 continue;
1003 }
1004 if (!strcmp(arg, "--abbrev")) {
1005 revs->abbrev = DEFAULT_ABBREV;
1006 continue;
1007 }
508d9e37
LT
1008 if (!strncmp(arg, "--abbrev=", 9)) {
1009 revs->abbrev = strtoul(arg + 9, NULL, 10);
1010 if (revs->abbrev < MINIMUM_ABBREV)
1011 revs->abbrev = MINIMUM_ABBREV;
1012 else if (revs->abbrev > 40)
1013 revs->abbrev = 40;
1014 continue;
1015 }
cd2bdc53
LT
1016 if (!strcmp(arg, "--abbrev-commit")) {
1017 revs->abbrev_commit = 1;
1018 continue;
1019 }
1020 if (!strcmp(arg, "--full-diff")) {
1021 revs->diff = 1;
1022 revs->full_diff = 1;
1023 continue;
1024 }
9202434c
LT
1025 if (!strcmp(arg, "--full-history")) {
1026 revs->simplify_history = 0;
1027 continue;
1028 }
3dfb9278
JF
1029 if (!strcmp(arg, "--relative-date")) {
1030 revs->relative_date = 1;
1031 continue;
1032 }
2d10c555
JH
1033
1034 /*
1035 * Grepping the commit log
1036 */
bd95fcd3
JH
1037 if (!strncmp(arg, "--author=", 9)) {
1038 add_header_grep(revs, "author", arg+9);
1039 continue;
1040 }
1041 if (!strncmp(arg, "--committer=", 12)) {
1042 add_header_grep(revs, "committer", arg+12);
1043 continue;
1044 }
1045 if (!strncmp(arg, "--grep=", 7)) {
1046 add_message_grep(revs, arg+7);
1047 continue;
1048 }
70d0afba
JH
1049 if (!strcmp(arg, "--all-match")) {
1050 all_match = 1;
1051 continue;
1052 }
7cbcf4d5
JH
1053 if (!strncmp(arg, "--encoding=", 11)) {
1054 arg += 11;
1055 if (strcmp(arg, "none"))
1056 git_log_output_encoding = strdup(arg);
1057 else
1058 git_log_output_encoding = "";
1059 continue;
1060 }
9c5e66e9
JS
1061 if (!strcmp(arg, "--reverse")) {
1062 revs->reverse ^= 1;
1063 continue;
1064 }
2d10c555 1065
cd2bdc53
LT
1066 opts = diff_opt_parse(&revs->diffopt, argv+i, argc-i);
1067 if (opts > 0) {
1068 revs->diff = 1;
1069 i += opts - 1;
1070 continue;
1071 }
ae563542
LT
1072 *unrecognized++ = arg;
1073 left++;
1074 continue;
1075 }
ae563542 1076
5d6f0935
JH
1077 if (handle_revision_arg(arg, revs, flags, seen_dashdash)) {
1078 int j;
1079 if (seen_dashdash || *arg == '^')
ae563542
LT
1080 die("bad revision '%s'", arg);
1081
ea92f41f
JH
1082 /* If we didn't have a "--":
1083 * (1) all filenames must exist;
1084 * (2) all rev-args must not be interpretable
1085 * as a valid filename.
1086 * but the latter we have checked in the main loop.
1087 */
e23d0b4a
LT
1088 for (j = i; j < argc; j++)
1089 verify_filename(revs->prefix, argv[j]);
1090
5d6f0935
JH
1091 revs->prune_data = get_pathspec(revs->prefix,
1092 argv + i);
ae563542
LT
1093 break;
1094 }
ae563542 1095 }
5d6f0935 1096
ae3e5e1e
JH
1097 if (show_merge)
1098 prepare_show_merge(revs);
1f1e895f 1099 if (def && !revs->pending.nr) {
ae563542 1100 unsigned char sha1[20];
cd2bdc53 1101 struct object *object;
31fff305 1102 if (get_sha1(def, sha1))
ae563542 1103 die("bad default revision '%s'", def);
cd2bdc53
LT
1104 object = get_reference(revs, def, sha1, 0);
1105 add_pending_object(revs, object, def);
ae563542 1106 }
8efdc326 1107
9dad9d2e 1108 if (revs->topo_order)
53069686
JH
1109 revs->limited = 1;
1110
8efdc326 1111 if (revs->prune_data) {
cd2bdc53 1112 diff_tree_setup_paths(revs->prune_data, &revs->pruning);
8efdc326 1113 revs->prune_fn = try_to_simplify_commit;
cd2bdc53
LT
1114 if (!revs->full_diff)
1115 diff_tree_setup_paths(revs->prune_data, &revs->diffopt);
8efdc326 1116 }
cd2bdc53
LT
1117 if (revs->combine_merges) {
1118 revs->ignore_merges = 0;
0e677e1a 1119 if (revs->dense_combined_merges && !revs->diffopt.output_format)
cd2bdc53
LT
1120 revs->diffopt.output_format = DIFF_FORMAT_PATCH;
1121 }
cd2bdc53 1122 revs->diffopt.abbrev = revs->abbrev;
72ee96c0
JH
1123 if (diff_setup_done(&revs->diffopt) < 0)
1124 die("diff_setup_done failed");
8efdc326 1125
70d0afba
JH
1126 if (revs->grep_filter) {
1127 revs->grep_filter->all_match = all_match;
2d10c555 1128 compile_grep_patterns(revs->grep_filter);
70d0afba 1129 }
bd95fcd3 1130
ae563542
LT
1131 return left;
1132}
a4a88b2b
LT
1133
1134void prepare_revision_walk(struct rev_info *revs)
1135{
1f1e895f 1136 int nr = revs->pending.nr;
94d23673 1137 struct object_array_entry *e, *list;
cd2bdc53 1138
94d23673 1139 e = list = revs->pending.objects;
1f1e895f
LT
1140 revs->pending.nr = 0;
1141 revs->pending.alloc = 0;
1142 revs->pending.objects = NULL;
1143 while (--nr >= 0) {
94d23673 1144 struct commit *commit = handle_commit(revs, e->item, e->name);
cd2bdc53
LT
1145 if (commit) {
1146 if (!(commit->object.flags & SEEN)) {
1147 commit->object.flags |= SEEN;
1148 insert_by_date(commit, &revs->commits);
1149 }
1150 }
94d23673 1151 e++;
cd2bdc53 1152 }
94d23673 1153 free(list);
cd2bdc53 1154
ba1d4505
LT
1155 if (revs->no_walk)
1156 return;
a4a88b2b
LT
1157 if (revs->limited)
1158 limit_list(revs);
1159 if (revs->topo_order)
8efdc326
FK
1160 sort_in_topological_order_fn(&revs->commits, revs->lifo,
1161 revs->topo_setter,
1162 revs->topo_getter);
a4a88b2b
LT
1163}
1164
3381c790 1165static int rewrite_one(struct rev_info *revs, struct commit **pp)
765ac8ec
LT
1166{
1167 for (;;) {
1168 struct commit *p = *pp;
3381c790
LT
1169 if (!revs->limited)
1170 add_parents_to_list(revs, p, &revs->commits);
6631c736
LT
1171 if (p->parents && p->parents->next)
1172 return 0;
765ac8ec
LT
1173 if (p->object.flags & (TREECHANGE | UNINTERESTING))
1174 return 0;
1175 if (!p->parents)
1176 return -1;
1177 *pp = p->parents->item;
1178 }
1179}
1180
3381c790 1181static void rewrite_parents(struct rev_info *revs, struct commit *commit)
765ac8ec
LT
1182{
1183 struct commit_list **pp = &commit->parents;
1184 while (*pp) {
1185 struct commit_list *parent = *pp;
3381c790 1186 if (rewrite_one(revs, &parent->item) < 0) {
765ac8ec
LT
1187 *pp = parent->next;
1188 continue;
1189 }
1190 pp = &parent->next;
1191 }
1192}
1193
1b65a5aa
JH
1194static void mark_boundary_to_show(struct commit *commit)
1195{
1196 struct commit_list *p = commit->parents;
1197 while (p) {
1198 commit = p->item;
1199 p = p->next;
1200 if (commit->object.flags & BOUNDARY)
1201 commit->object.flags |= BOUNDARY_SHOW;
1202 }
1203}
1204
8ecae9b0
JH
1205static int commit_match(struct commit *commit, struct rev_info *opt)
1206{
2d10c555 1207 if (!opt->grep_filter)
8ecae9b0 1208 return 1;
2d10c555
JH
1209 return grep_buffer(opt->grep_filter,
1210 NULL, /* we say nothing, not even filename */
1211 commit->buffer, strlen(commit->buffer));
8ecae9b0
JH
1212}
1213
d5db6c9e 1214static struct commit *get_revision_1(struct rev_info *revs)
a4a88b2b 1215{
d5db6c9e 1216 if (!revs->commits)
a4a88b2b 1217 return NULL;
a4a88b2b 1218
765ac8ec 1219 do {
cb115748
LT
1220 struct commit_list *entry = revs->commits;
1221 struct commit *commit = entry->item;
ea5ed3ab 1222
cb115748
LT
1223 revs->commits = entry->next;
1224 free(entry);
2a0925be 1225
8860fd42
JS
1226 if (revs->reflog_info)
1227 fake_reflog_parent(revs->reflog_info, commit);
1228
2a0925be
LT
1229 /*
1230 * If we haven't done the list limiting, we need to look at
be7db6e5
LT
1231 * the parents here. We also need to do the date-based limiting
1232 * that we'd otherwise have done in limit_list().
2a0925be 1233 */
be7db6e5 1234 if (!revs->limited) {
744f4985
JH
1235 if (revs->max_age != -1 &&
1236 (commit->date < revs->max_age))
be7db6e5 1237 continue;
2a0925be 1238 add_parents_to_list(revs, commit, &revs->commits);
be7db6e5 1239 }
384e99a4 1240 if (commit->object.flags & SHOWN)
2a0925be 1241 continue;
1b65a5aa 1242
744f4985
JH
1243 if (revs->unpacked && has_sha1_pack(commit->object.sha1,
1244 revs->ignore_packed))
1245 continue;
1246
1b65a5aa
JH
1247 /* We want to show boundary commits only when their
1248 * children are shown. When path-limiter is in effect,
1249 * rewrite_parents() drops some commits from getting shown,
1250 * and there is no point showing boundary parents that
1251 * are not shown. After rewrite_parents() rewrites the
1252 * parents of a commit that is shown, we mark the boundary
1253 * parents with BOUNDARY_SHOW.
1254 */
1255 if (commit->object.flags & BOUNDARY_SHOW) {
1256 commit->object.flags |= SHOWN;
1257 return commit;
1258 }
1259 if (commit->object.flags & UNINTERESTING)
2a0925be 1260 continue;
765ac8ec 1261 if (revs->min_age != -1 && (commit->date > revs->min_age))
2a0925be 1262 continue;
384e99a4
JH
1263 if (revs->no_merges &&
1264 commit->parents && commit->parents->next)
2a0925be 1265 continue;
8ecae9b0
JH
1266 if (!commit_match(commit, revs))
1267 continue;
8efdc326 1268 if (revs->prune_fn && revs->dense) {
6631c736
LT
1269 /* Commit without changes? */
1270 if (!(commit->object.flags & TREECHANGE)) {
1271 /* drop merges unless we want parenthood */
1272 if (!revs->parents)
1273 continue;
1274 /* non-merge - always ignore it */
02d3dca3 1275 if (!commit->parents || !commit->parents->next)
6631c736
LT
1276 continue;
1277 }
2a0925be 1278 if (revs->parents)
3381c790 1279 rewrite_parents(revs, commit);
384e99a4 1280 }
1b65a5aa
JH
1281 if (revs->boundary)
1282 mark_boundary_to_show(commit);
765ac8ec
LT
1283 commit->object.flags |= SHOWN;
1284 return commit;
1285 } while (revs->commits);
1286 return NULL;
1287}
d5db6c9e
JH
1288
1289struct commit *get_revision(struct rev_info *revs)
1290{
1291 struct commit *c = NULL;
1292
9c5e66e9
JS
1293 if (revs->reverse) {
1294 struct commit_list *list;
1295
1296 /*
1297 * rev_info.reverse is used to note the fact that we
1298 * want to output the list of revisions in reverse
1299 * order. To accomplish this goal, reverse can have
1300 * different values:
1301 *
1302 * 0 do nothing
1303 * 1 reverse the list
1304 * 2 internal use: we have already obtained and
1305 * reversed the list, now we only need to yield
1306 * its items.
1307 */
1308
1309 if (revs->reverse == 1) {
1310 revs->reverse = 0;
1311 list = NULL;
1312 while ((c = get_revision(revs)))
1313 commit_list_insert(c, &list);
1314 revs->commits = list;
1315 revs->reverse = 2;
1316 }
1317
1318 if (!revs->commits)
1319 return NULL;
1320 c = revs->commits->item;
1321 list = revs->commits->next;
1322 free(revs->commits);
1323 revs->commits = list;
1324 return c;
1325 }
1326
d5db6c9e
JH
1327 if (0 < revs->skip_count) {
1328 while ((c = get_revision_1(revs)) != NULL) {
1329 if (revs->skip_count-- <= 0)
1330 break;
1331 }
1332 }
1333
1334 /* Check the max_count ... */
1335 switch (revs->max_count) {
1336 case -1:
1337 break;
1338 case 0:
1339 return NULL;
1340 default:
1341 revs->max_count--;
1342 }
1343 if (c)
1344 return c;
1345 return get_revision_1(revs);
1346}