[PATCH] Also handle CVS branches with a '/' in their name
[git.git] / rev-list.c
blobce5b8b2ad528830eb0c6572edcdc55d97422952c
1 #include "cache.h"
2 #include "tag.h"
3 #include "commit.h"
4 #include "tree.h"
5 #include "blob.h"
6 #include "epoch.h"
8 #define SEEN (1u << 0)
9 #define INTERESTING (1u << 1)
10 #define COUNTED (1u << 2)
11 #define SHOWN (1u << 3)
13 static const char rev_list_usage[] =
14 "git-rev-list [OPTION] commit-id <commit-id>\n"
15 " --max-count=nr\n"
16 " --max-age=epoch\n"
17 " --min-age=epoch\n"
18 " --parents\n"
19 " --bisect\n"
20 " --objects\n"
21 " --unpacked\n"
22 " --header\n"
23 " --pretty\n"
24 " --no-merges\n"
25 " --merge-order [ --show-breaks ]\n"
26 " --topo-order";
28 static int unpacked = 0;
29 static int bisect_list = 0;
30 static int tag_objects = 0;
31 static int tree_objects = 0;
32 static int blob_objects = 0;
33 static int verbose_header = 0;
34 static int show_parents = 0;
35 static int hdr_termination = 0;
36 static const char *prefix = "";
37 static unsigned long max_age = -1;
38 static unsigned long min_age = -1;
39 static int max_count = -1;
40 static enum cmit_fmt commit_format = CMIT_FMT_RAW;
41 static int merge_order = 0;
42 static int show_breaks = 0;
43 static int stop_traversal = 0;
44 static int topo_order = 0;
45 static int no_merges = 0;
47 static void show_commit(struct commit *commit)
49 commit->object.flags |= SHOWN;
50 if (show_breaks) {
51 prefix = "| ";
52 if (commit->object.flags & DISCONTINUITY) {
53 prefix = "^ ";
54 } else if (commit->object.flags & BOUNDARY) {
55 prefix = "= ";
58 printf("%s%s", prefix, sha1_to_hex(commit->object.sha1));
59 if (show_parents) {
60 struct commit_list *parents = commit->parents;
61 while (parents) {
62 printf(" %s", sha1_to_hex(parents->item->object.sha1));
63 parents = parents->next;
66 if (commit_format == CMIT_FMT_ONELINE)
67 putchar(' ');
68 else
69 putchar('\n');
71 if (verbose_header) {
72 static char pretty_header[16384];
73 pretty_print_commit(commit_format, commit->buffer, ~0, pretty_header, sizeof(pretty_header));
74 printf("%s%c", pretty_header, hdr_termination);
76 fflush(stdout);
79 static int filter_commit(struct commit * commit)
81 if (stop_traversal && (commit->object.flags & BOUNDARY))
82 return STOP;
83 if (commit->object.flags & (UNINTERESTING|SHOWN))
84 return CONTINUE;
85 if (min_age != -1 && (commit->date > min_age))
86 return CONTINUE;
87 if (max_age != -1 && (commit->date < max_age)) {
88 stop_traversal=1;
89 return merge_order?CONTINUE:STOP;
91 if (max_count != -1 && !max_count--)
92 return STOP;
93 if (no_merges && (commit->parents && commit->parents->next))
94 return CONTINUE;
95 return DO;
98 static int process_commit(struct commit * commit)
100 int action=filter_commit(commit);
102 if (action == STOP) {
103 return STOP;
106 if (action == CONTINUE) {
107 return CONTINUE;
110 show_commit(commit);
112 return CONTINUE;
115 static struct object_list **add_object(struct object *obj, struct object_list **p, const char *name)
117 struct object_list *entry = xmalloc(sizeof(*entry));
118 entry->item = obj;
119 entry->next = *p;
120 entry->name = name;
121 *p = entry;
122 return &entry->next;
125 static struct object_list **process_blob(struct blob *blob, struct object_list **p, const char *name)
127 struct object *obj = &blob->object;
129 if (!blob_objects)
130 return p;
131 if (obj->flags & (UNINTERESTING | SEEN))
132 return p;
133 obj->flags |= SEEN;
134 return add_object(obj, p, name);
137 static struct object_list **process_tree(struct tree *tree, struct object_list **p, const char *name)
139 struct object *obj = &tree->object;
140 struct tree_entry_list *entry;
142 if (!tree_objects)
143 return p;
144 if (obj->flags & (UNINTERESTING | SEEN))
145 return p;
146 if (parse_tree(tree) < 0)
147 die("bad tree object %s", sha1_to_hex(obj->sha1));
148 obj->flags |= SEEN;
149 p = add_object(obj, p, name);
150 for (entry = tree->entries ; entry ; entry = entry->next) {
151 if (entry->directory)
152 p = process_tree(entry->item.tree, p, entry->name);
153 else
154 p = process_blob(entry->item.blob, p, entry->name);
156 return p;
159 static struct object_list *pending_objects = NULL;
161 static void show_commit_list(struct commit_list *list)
163 struct object_list *objects = NULL, **p = &objects, *pending;
164 while (list) {
165 struct commit *commit = pop_most_recent_commit(&list, SEEN);
167 p = process_tree(commit->tree, p, "");
168 if (process_commit(commit) == STOP)
169 break;
171 for (pending = pending_objects; pending; pending = pending->next) {
172 struct object *obj = pending->item;
173 const char *name = pending->name;
174 if (obj->flags & (UNINTERESTING | SEEN))
175 continue;
176 if (obj->type == tag_type) {
177 obj->flags |= SEEN;
178 p = add_object(obj, p, name);
179 continue;
181 if (obj->type == tree_type) {
182 p = process_tree((struct tree *)obj, p, name);
183 continue;
185 if (obj->type == blob_type) {
186 p = process_blob((struct blob *)obj, p, name);
187 continue;
189 die("unknown pending object %s (%s)", sha1_to_hex(obj->sha1), name);
191 while (objects) {
192 printf("%s %s\n", sha1_to_hex(objects->item->sha1), objects->name);
193 objects = objects->next;
197 static void mark_blob_uninteresting(struct blob *blob)
199 if (!blob_objects)
200 return;
201 if (blob->object.flags & UNINTERESTING)
202 return;
203 blob->object.flags |= UNINTERESTING;
206 static void mark_tree_uninteresting(struct tree *tree)
208 struct object *obj = &tree->object;
209 struct tree_entry_list *entry;
211 if (!tree_objects)
212 return;
213 if (obj->flags & UNINTERESTING)
214 return;
215 obj->flags |= UNINTERESTING;
216 if (!has_sha1_file(obj->sha1))
217 return;
218 if (parse_tree(tree) < 0)
219 die("bad tree %s", sha1_to_hex(obj->sha1));
220 entry = tree->entries;
221 while (entry) {
222 if (entry->directory)
223 mark_tree_uninteresting(entry->item.tree);
224 else
225 mark_blob_uninteresting(entry->item.blob);
226 entry = entry->next;
230 static void mark_parents_uninteresting(struct commit *commit)
232 struct commit_list *parents = commit->parents;
234 if (tree_objects)
235 mark_tree_uninteresting(commit->tree);
236 while (parents) {
237 struct commit *commit = parents->item;
238 commit->object.flags |= UNINTERESTING;
241 * Normally we haven't parsed the parent
242 * yet, so we won't have a parent of a parent
243 * here. However, it may turn out that we've
244 * reached this commit some other way (where it
245 * wasn't uninteresting), in which case we need
246 * to mark its parents recursively too..
248 if (commit->parents)
249 mark_parents_uninteresting(commit);
252 * A missing commit is ok iff its parent is marked
253 * uninteresting.
255 * We just mark such a thing parsed, so that when
256 * it is popped next time around, we won't be trying
257 * to parse it and get an error.
259 if (!has_sha1_file(commit->object.sha1))
260 commit->object.parsed = 1;
261 parents = parents->next;
265 static int everybody_uninteresting(struct commit_list *orig)
267 struct commit_list *list = orig;
268 while (list) {
269 struct commit *commit = list->item;
270 list = list->next;
271 if (commit->object.flags & UNINTERESTING)
272 continue;
273 return 0;
277 * Ok, go back and mark all the edge trees uninteresting,
278 * since otherwise we can have situations where a parent
279 * that was marked uninteresting (and we never even had
280 * to look at) had lots of objects that we don't want to
281 * include.
283 * NOTE! This still doesn't mean that the object list is
284 * "correct", since we may end up listing objects that
285 * even older commits (that we don't list) do actually
286 * reference, but it gets us to a minimal list (or very
287 * close) in practice.
289 if (!tree_objects)
290 return 1;
292 while (orig) {
293 struct commit *commit = orig->item;
294 if (!parse_commit(commit) && commit->tree)
295 mark_tree_uninteresting(commit->tree);
296 orig = orig->next;
298 return 1;
302 * This is a truly stupid algorithm, but it's only
303 * used for bisection, and we just don't care enough.
305 * We care just barely enough to avoid recursing for
306 * non-merge entries.
308 static int count_distance(struct commit_list *entry)
310 int nr = 0;
312 while (entry) {
313 struct commit *commit = entry->item;
314 struct commit_list *p;
316 if (commit->object.flags & (UNINTERESTING | COUNTED))
317 break;
318 nr++;
319 commit->object.flags |= COUNTED;
320 p = commit->parents;
321 entry = p;
322 if (p) {
323 p = p->next;
324 while (p) {
325 nr += count_distance(p);
326 p = p->next;
330 return nr;
333 static void clear_distance(struct commit_list *list)
335 while (list) {
336 struct commit *commit = list->item;
337 commit->object.flags &= ~COUNTED;
338 list = list->next;
342 static struct commit_list *find_bisection(struct commit_list *list)
344 int nr, closest;
345 struct commit_list *p, *best;
347 nr = 0;
348 p = list;
349 while (p) {
350 nr++;
351 p = p->next;
353 closest = 0;
354 best = list;
356 p = list;
357 while (p) {
358 int distance = count_distance(p);
359 clear_distance(list);
360 if (nr - distance < distance)
361 distance = nr - distance;
362 if (distance > closest) {
363 best = p;
364 closest = distance;
366 p = p->next;
368 if (best)
369 best->next = NULL;
370 return best;
373 static struct commit_list *limit_list(struct commit_list *list)
375 struct commit_list *newlist = NULL;
376 struct commit_list **p = &newlist;
377 while (list) {
378 struct commit *commit = pop_most_recent_commit(&list, SEEN);
379 struct object *obj = &commit->object;
381 if (unpacked && has_sha1_pack(obj->sha1))
382 obj->flags |= UNINTERESTING;
383 if (obj->flags & UNINTERESTING) {
384 mark_parents_uninteresting(commit);
385 if (everybody_uninteresting(list))
386 break;
387 continue;
389 p = &commit_list_insert(commit, p)->next;
391 if (bisect_list)
392 newlist = find_bisection(newlist);
393 return newlist;
396 static void add_pending_object(struct object *obj, const char *name)
398 add_object(obj, &pending_objects, name);
401 static struct commit *get_commit_reference(const char *name, unsigned int flags)
403 unsigned char sha1[20];
404 struct object *object;
406 if (get_sha1(name, sha1))
407 usage(rev_list_usage);
408 object = parse_object(sha1);
409 if (!object)
410 die("bad object %s", name);
413 * Tag object? Look what it points to..
415 while (object->type == tag_type) {
416 struct tag *tag = (struct tag *) object;
417 object->flags |= flags;
418 if (tag_objects && !(object->flags & UNINTERESTING))
419 add_pending_object(object, tag->tag);
420 object = parse_object(tag->tagged->sha1);
424 * Commit object? Just return it, we'll do all the complex
425 * reachability crud.
427 if (object->type == commit_type) {
428 struct commit *commit = (struct commit *)object;
429 object->flags |= flags;
430 if (parse_commit(commit) < 0)
431 die("unable to parse commit %s", name);
432 if (flags & UNINTERESTING)
433 mark_parents_uninteresting(commit);
434 return commit;
438 * Tree object? Either mark it uniniteresting, or add it
439 * to the list of objects to look at later..
441 if (object->type == tree_type) {
442 struct tree *tree = (struct tree *)object;
443 if (!tree_objects)
444 return NULL;
445 if (flags & UNINTERESTING) {
446 mark_tree_uninteresting(tree);
447 return NULL;
449 add_pending_object(object, "");
450 return NULL;
454 * Blob object? You know the drill by now..
456 if (object->type == blob_type) {
457 struct blob *blob = (struct blob *)object;
458 if (!blob_objects)
459 return NULL;
460 if (flags & UNINTERESTING) {
461 mark_blob_uninteresting(blob);
462 return NULL;
464 add_pending_object(object, "");
465 return NULL;
467 die("%s is unknown object", name);
470 static void handle_one_commit(struct commit *com, struct commit_list **lst)
472 if (!com || com->object.flags & SEEN)
473 return;
474 com->object.flags |= SEEN;
475 commit_list_insert(com, lst);
479 int main(int argc, char **argv)
481 struct commit_list *list = NULL;
482 int i, limited = 0;
484 for (i = 1 ; i < argc; i++) {
485 int flags;
486 char *arg = argv[i];
487 char *dotdot;
488 struct commit *commit;
490 if (!strncmp(arg, "--max-count=", 12)) {
491 max_count = atoi(arg + 12);
492 continue;
494 if (!strncmp(arg, "--max-age=", 10)) {
495 max_age = atoi(arg + 10);
496 continue;
498 if (!strncmp(arg, "--min-age=", 10)) {
499 min_age = atoi(arg + 10);
500 continue;
502 if (!strcmp(arg, "--header")) {
503 verbose_header = 1;
504 continue;
506 if (!strncmp(arg, "--pretty", 8)) {
507 commit_format = get_commit_format(arg+8);
508 verbose_header = 1;
509 hdr_termination = '\n';
510 if (commit_format == CMIT_FMT_ONELINE)
511 prefix = "";
512 else
513 prefix = "commit ";
514 continue;
516 if (!strncmp(arg, "--no-merges", 11)) {
517 no_merges = 1;
518 continue;
520 if (!strcmp(arg, "--parents")) {
521 show_parents = 1;
522 continue;
524 if (!strcmp(arg, "--bisect")) {
525 bisect_list = 1;
526 continue;
528 if (!strcmp(arg, "--objects")) {
529 tag_objects = 1;
530 tree_objects = 1;
531 blob_objects = 1;
532 continue;
534 if (!strcmp(arg, "--unpacked")) {
535 unpacked = 1;
536 limited = 1;
537 continue;
539 if (!strcmp(arg, "--merge-order")) {
540 merge_order = 1;
541 continue;
543 if (!strcmp(arg, "--show-breaks")) {
544 show_breaks = 1;
545 continue;
547 if (!strcmp(arg, "--topo-order")) {
548 topo_order = 1;
549 limited = 1;
550 continue;
553 if (show_breaks && !merge_order)
554 usage(rev_list_usage);
556 flags = 0;
557 dotdot = strstr(arg, "..");
558 if (dotdot) {
559 char *next = dotdot + 2;
560 struct commit *exclude = NULL;
561 struct commit *include = NULL;
562 *dotdot = 0;
563 exclude = get_commit_reference(arg, UNINTERESTING);
564 include = get_commit_reference(next, 0);
565 if (exclude && include) {
566 limited = 1;
567 handle_one_commit(exclude, &list);
568 handle_one_commit(include, &list);
569 continue;
571 *next = '.';
573 if (*arg == '^') {
574 flags = UNINTERESTING;
575 arg++;
576 limited = 1;
578 commit = get_commit_reference(arg, flags);
579 handle_one_commit(commit, &list);
582 if (!merge_order) {
583 sort_by_date(&list);
584 if (limited)
585 list = limit_list(list);
586 if (topo_order)
587 sort_in_topological_order(&list);
588 show_commit_list(list);
589 } else {
590 #ifndef NO_OPENSSL
591 if (sort_list_in_merge_order(list, &process_commit)) {
592 die("merge order sort failed\n");
594 #else
595 die("merge order sort unsupported, OpenSSL not linked");
596 #endif
599 return 0;