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