GIT 0.99.8b
[git/jnareb-git/bp-gitweb.git] / rev-list.c
blobc60aa72957cb65501266911aefd8aaaa2f2e4e16
1 #include "cache.h"
2 #include "refs.h"
3 #include "tag.h"
4 #include "commit.h"
5 #include "tree.h"
6 #include "blob.h"
7 #include "epoch.h"
9 #define SEEN (1u << 0)
10 #define INTERESTING (1u << 1)
11 #define COUNTED (1u << 2)
12 #define SHOWN (1u << 3)
14 static const char rev_list_usage[] =
15 "git-rev-list [OPTION] commit-id <commit-id>\n"
16 " --max-count=nr\n"
17 " --max-age=epoch\n"
18 " --min-age=epoch\n"
19 " --parents\n"
20 " --bisect\n"
21 " --objects\n"
22 " --unpacked\n"
23 " --header\n"
24 " --pretty\n"
25 " --no-merges\n"
26 " --merge-order [ --show-breaks ]\n"
27 " --topo-order";
29 static int unpacked = 0;
30 static int bisect_list = 0;
31 static int tag_objects = 0;
32 static int tree_objects = 0;
33 static int blob_objects = 0;
34 static int verbose_header = 0;
35 static int show_parents = 0;
36 static int hdr_termination = 0;
37 static const char *commit_prefix = "";
38 static unsigned long max_age = -1;
39 static unsigned long min_age = -1;
40 static int max_count = -1;
41 static enum cmit_fmt commit_format = CMIT_FMT_RAW;
42 static int merge_order = 0;
43 static int show_breaks = 0;
44 static int stop_traversal = 0;
45 static int topo_order = 0;
46 static int no_merges = 0;
48 static void show_commit(struct commit *commit)
50 commit->object.flags |= SHOWN;
51 if (show_breaks) {
52 commit_prefix = "| ";
53 if (commit->object.flags & DISCONTINUITY) {
54 commit_prefix = "^ ";
55 } else if (commit->object.flags & BOUNDARY) {
56 commit_prefix = "= ";
59 printf("%s%s", commit_prefix, sha1_to_hex(commit->object.sha1));
60 if (show_parents) {
61 struct commit_list *parents = commit->parents;
62 while (parents) {
63 printf(" %s", sha1_to_hex(parents->item->object.sha1));
64 parents = parents->next;
67 if (commit_format == CMIT_FMT_ONELINE)
68 putchar(' ');
69 else
70 putchar('\n');
72 if (verbose_header) {
73 static char pretty_header[16384];
74 pretty_print_commit(commit_format, commit->buffer, ~0, pretty_header, sizeof(pretty_header));
75 printf("%s%c", pretty_header, hdr_termination);
77 fflush(stdout);
80 static int filter_commit(struct commit * commit)
82 if (stop_traversal && (commit->object.flags & BOUNDARY))
83 return STOP;
84 if (commit->object.flags & (UNINTERESTING|SHOWN))
85 return CONTINUE;
86 if (min_age != -1 && (commit->date > min_age))
87 return CONTINUE;
88 if (max_age != -1 && (commit->date < max_age)) {
89 stop_traversal=1;
90 return CONTINUE;
92 if (max_count != -1 && !max_count--)
93 return STOP;
94 if (no_merges && (commit->parents && commit->parents->next))
95 return CONTINUE;
96 return DO;
99 static int process_commit(struct commit * commit)
101 int action=filter_commit(commit);
103 if (action == STOP) {
104 return STOP;
107 if (action == CONTINUE) {
108 return CONTINUE;
111 show_commit(commit);
113 return CONTINUE;
116 static struct object_list **add_object(struct object *obj, struct object_list **p, const char *name)
118 struct object_list *entry = xmalloc(sizeof(*entry));
119 entry->item = obj;
120 entry->next = *p;
121 entry->name = name;
122 *p = entry;
123 return &entry->next;
126 static struct object_list **process_blob(struct blob *blob, struct object_list **p, const char *name)
128 struct object *obj = &blob->object;
130 if (!blob_objects)
131 return p;
132 if (obj->flags & (UNINTERESTING | SEEN))
133 return p;
134 obj->flags |= SEEN;
135 return add_object(obj, p, name);
138 static struct object_list **process_tree(struct tree *tree, struct object_list **p, const char *name)
140 struct object *obj = &tree->object;
141 struct tree_entry_list *entry;
143 if (!tree_objects)
144 return p;
145 if (obj->flags & (UNINTERESTING | SEEN))
146 return p;
147 if (parse_tree(tree) < 0)
148 die("bad tree object %s", sha1_to_hex(obj->sha1));
149 obj->flags |= SEEN;
150 p = add_object(obj, p, name);
151 entry = tree->entries;
152 tree->entries = NULL;
153 while (entry) {
154 struct tree_entry_list *next = entry->next;
155 if (entry->directory)
156 p = process_tree(entry->item.tree, p, entry->name);
157 else
158 p = process_blob(entry->item.blob, p, entry->name);
159 free(entry);
160 entry = next;
162 return p;
165 static struct object_list *pending_objects = NULL;
167 static void show_commit_list(struct commit_list *list)
169 struct object_list *objects = NULL, **p = &objects, *pending;
170 while (list) {
171 struct commit *commit = pop_most_recent_commit(&list, SEEN);
173 p = process_tree(commit->tree, p, "");
174 if (process_commit(commit) == STOP)
175 break;
177 for (pending = pending_objects; pending; pending = pending->next) {
178 struct object *obj = pending->item;
179 const char *name = pending->name;
180 if (obj->flags & (UNINTERESTING | SEEN))
181 continue;
182 if (obj->type == tag_type) {
183 obj->flags |= SEEN;
184 p = add_object(obj, p, name);
185 continue;
187 if (obj->type == tree_type) {
188 p = process_tree((struct tree *)obj, p, name);
189 continue;
191 if (obj->type == blob_type) {
192 p = process_blob((struct blob *)obj, p, name);
193 continue;
195 die("unknown pending object %s (%s)", sha1_to_hex(obj->sha1), name);
197 while (objects) {
198 /* An object with name "foo\n0000000000000000000000000000000000000000"
199 * can be used confuse downstream git-pack-objects very badly.
201 const char *ep = strchr(objects->name, '\n');
202 if (ep) {
203 printf("%s %.*s\n", sha1_to_hex(objects->item->sha1),
204 (int) (ep - objects->name),
205 objects->name);
207 else
208 printf("%s %s\n", sha1_to_hex(objects->item->sha1), objects->name);
209 objects = objects->next;
213 static void mark_blob_uninteresting(struct blob *blob)
215 if (!blob_objects)
216 return;
217 if (blob->object.flags & UNINTERESTING)
218 return;
219 blob->object.flags |= UNINTERESTING;
222 static void mark_tree_uninteresting(struct tree *tree)
224 struct object *obj = &tree->object;
225 struct tree_entry_list *entry;
227 if (!tree_objects)
228 return;
229 if (obj->flags & UNINTERESTING)
230 return;
231 obj->flags |= UNINTERESTING;
232 if (!has_sha1_file(obj->sha1))
233 return;
234 if (parse_tree(tree) < 0)
235 die("bad tree %s", sha1_to_hex(obj->sha1));
236 entry = tree->entries;
237 tree->entries = NULL;
238 while (entry) {
239 struct tree_entry_list *next = entry->next;
240 if (entry->directory)
241 mark_tree_uninteresting(entry->item.tree);
242 else
243 mark_blob_uninteresting(entry->item.blob);
244 free(entry);
245 entry = next;
249 static void mark_parents_uninteresting(struct commit *commit)
251 struct commit_list *parents = commit->parents;
253 while (parents) {
254 struct commit *commit = parents->item;
255 commit->object.flags |= UNINTERESTING;
258 * Normally we haven't parsed the parent
259 * yet, so we won't have a parent of a parent
260 * here. However, it may turn out that we've
261 * reached this commit some other way (where it
262 * wasn't uninteresting), in which case we need
263 * to mark its parents recursively too..
265 if (commit->parents)
266 mark_parents_uninteresting(commit);
269 * A missing commit is ok iff its parent is marked
270 * uninteresting.
272 * We just mark such a thing parsed, so that when
273 * it is popped next time around, we won't be trying
274 * to parse it and get an error.
276 if (!has_sha1_file(commit->object.sha1))
277 commit->object.parsed = 1;
278 parents = parents->next;
282 static int everybody_uninteresting(struct commit_list *orig)
284 struct commit_list *list = orig;
285 while (list) {
286 struct commit *commit = list->item;
287 list = list->next;
288 if (commit->object.flags & UNINTERESTING)
289 continue;
290 return 0;
292 return 1;
296 * This is a truly stupid algorithm, but it's only
297 * used for bisection, and we just don't care enough.
299 * We care just barely enough to avoid recursing for
300 * non-merge entries.
302 static int count_distance(struct commit_list *entry)
304 int nr = 0;
306 while (entry) {
307 struct commit *commit = entry->item;
308 struct commit_list *p;
310 if (commit->object.flags & (UNINTERESTING | COUNTED))
311 break;
312 nr++;
313 commit->object.flags |= COUNTED;
314 p = commit->parents;
315 entry = p;
316 if (p) {
317 p = p->next;
318 while (p) {
319 nr += count_distance(p);
320 p = p->next;
324 return nr;
327 static void clear_distance(struct commit_list *list)
329 while (list) {
330 struct commit *commit = list->item;
331 commit->object.flags &= ~COUNTED;
332 list = list->next;
336 static struct commit_list *find_bisection(struct commit_list *list)
338 int nr, closest;
339 struct commit_list *p, *best;
341 nr = 0;
342 p = list;
343 while (p) {
344 nr++;
345 p = p->next;
347 closest = 0;
348 best = list;
350 p = list;
351 while (p) {
352 int distance = count_distance(p);
353 clear_distance(list);
354 if (nr - distance < distance)
355 distance = nr - distance;
356 if (distance > closest) {
357 best = p;
358 closest = distance;
360 p = p->next;
362 if (best)
363 best->next = NULL;
364 return best;
367 static void mark_edges_uninteresting(struct commit_list *list)
369 for ( ; list; list = list->next) {
370 struct commit_list *parents = list->item->parents;
372 for ( ; parents; parents = parents->next) {
373 struct commit *commit = parents->item;
374 if (commit->object.flags & UNINTERESTING)
375 mark_tree_uninteresting(commit->tree);
380 static struct commit_list *limit_list(struct commit_list *list)
382 struct commit_list *newlist = NULL;
383 struct commit_list **p = &newlist;
384 while (list) {
385 struct commit *commit = pop_most_recent_commit(&list, SEEN);
386 struct object *obj = &commit->object;
388 if (max_age != -1 && (commit->date < max_age))
389 obj->flags |= UNINTERESTING;
390 if (unpacked && has_sha1_pack(obj->sha1))
391 obj->flags |= UNINTERESTING;
392 if (obj->flags & UNINTERESTING) {
393 mark_parents_uninteresting(commit);
394 if (everybody_uninteresting(list))
395 break;
396 continue;
398 if (min_age != -1 && (commit->date > min_age))
399 continue;
400 p = &commit_list_insert(commit, p)->next;
402 if (tree_objects)
403 mark_edges_uninteresting(newlist);
404 if (bisect_list)
405 newlist = find_bisection(newlist);
406 return newlist;
409 static void add_pending_object(struct object *obj, const char *name)
411 add_object(obj, &pending_objects, name);
414 static struct commit *get_commit_reference(const char *name, unsigned int flags)
416 unsigned char sha1[20];
417 struct object *object;
419 if (get_sha1(name, sha1))
420 usage(rev_list_usage);
421 object = parse_object(sha1);
422 if (!object)
423 die("bad object %s", name);
426 * Tag object? Look what it points to..
428 while (object->type == tag_type) {
429 struct tag *tag = (struct tag *) object;
430 object->flags |= flags;
431 if (tag_objects && !(object->flags & UNINTERESTING))
432 add_pending_object(object, tag->tag);
433 object = parse_object(tag->tagged->sha1);
434 if (!object)
435 die("bad object %s", sha1_to_hex(tag->tagged->sha1));
439 * Commit object? Just return it, we'll do all the complex
440 * reachability crud.
442 if (object->type == commit_type) {
443 struct commit *commit = (struct commit *)object;
444 object->flags |= flags;
445 if (parse_commit(commit) < 0)
446 die("unable to parse commit %s", name);
447 if (flags & UNINTERESTING)
448 mark_parents_uninteresting(commit);
449 return commit;
453 * Tree object? Either mark it uniniteresting, or add it
454 * to the list of objects to look at later..
456 if (object->type == tree_type) {
457 struct tree *tree = (struct tree *)object;
458 if (!tree_objects)
459 return NULL;
460 if (flags & UNINTERESTING) {
461 mark_tree_uninteresting(tree);
462 return NULL;
464 add_pending_object(object, "");
465 return NULL;
469 * Blob object? You know the drill by now..
471 if (object->type == blob_type) {
472 struct blob *blob = (struct blob *)object;
473 if (!blob_objects)
474 return NULL;
475 if (flags & UNINTERESTING) {
476 mark_blob_uninteresting(blob);
477 return NULL;
479 add_pending_object(object, "");
480 return NULL;
482 die("%s is unknown object", name);
485 static void handle_one_commit(struct commit *com, struct commit_list **lst)
487 if (!com || com->object.flags & SEEN)
488 return;
489 com->object.flags |= SEEN;
490 commit_list_insert(com, lst);
493 /* for_each_ref() callback does not allow user data -- Yuck. */
494 static struct commit_list **global_lst;
496 static int include_one_commit(const char *path, const unsigned char *sha1)
498 struct commit *com = get_commit_reference(path, 0);
499 handle_one_commit(com, global_lst);
500 return 0;
503 static void handle_all(struct commit_list **lst)
505 global_lst = lst;
506 for_each_ref(include_one_commit);
507 global_lst = NULL;
510 int main(int argc, char **argv)
512 struct commit_list *list = NULL;
513 int i, limited = 0;
515 setup_git_directory();
516 for (i = 1 ; i < argc; i++) {
517 int flags;
518 char *arg = argv[i];
519 char *dotdot;
520 struct commit *commit;
522 if (!strncmp(arg, "--max-count=", 12)) {
523 max_count = atoi(arg + 12);
524 continue;
526 if (!strncmp(arg, "--max-age=", 10)) {
527 max_age = atoi(arg + 10);
528 limited = 1;
529 continue;
531 if (!strncmp(arg, "--min-age=", 10)) {
532 min_age = atoi(arg + 10);
533 limited = 1;
534 continue;
536 if (!strcmp(arg, "--header")) {
537 verbose_header = 1;
538 continue;
540 if (!strncmp(arg, "--pretty", 8)) {
541 commit_format = get_commit_format(arg+8);
542 verbose_header = 1;
543 hdr_termination = '\n';
544 if (commit_format == CMIT_FMT_ONELINE)
545 commit_prefix = "";
546 else
547 commit_prefix = "commit ";
548 continue;
550 if (!strncmp(arg, "--no-merges", 11)) {
551 no_merges = 1;
552 continue;
554 if (!strcmp(arg, "--parents")) {
555 show_parents = 1;
556 continue;
558 if (!strcmp(arg, "--bisect")) {
559 bisect_list = 1;
560 continue;
562 if (!strcmp(arg, "--all")) {
563 handle_all(&list);
564 continue;
566 if (!strcmp(arg, "--objects")) {
567 tag_objects = 1;
568 tree_objects = 1;
569 blob_objects = 1;
570 continue;
572 if (!strcmp(arg, "--unpacked")) {
573 unpacked = 1;
574 limited = 1;
575 continue;
577 if (!strcmp(arg, "--merge-order")) {
578 merge_order = 1;
579 continue;
581 if (!strcmp(arg, "--show-breaks")) {
582 show_breaks = 1;
583 continue;
585 if (!strcmp(arg, "--topo-order")) {
586 topo_order = 1;
587 limited = 1;
588 continue;
591 if (show_breaks && !merge_order)
592 usage(rev_list_usage);
594 flags = 0;
595 dotdot = strstr(arg, "..");
596 if (dotdot) {
597 char *next = dotdot + 2;
598 struct commit *exclude = NULL;
599 struct commit *include = NULL;
600 *dotdot = 0;
601 if (!*next)
602 next = "HEAD";
603 exclude = get_commit_reference(arg, UNINTERESTING);
604 include = get_commit_reference(next, 0);
605 if (exclude && include) {
606 limited = 1;
607 handle_one_commit(exclude, &list);
608 handle_one_commit(include, &list);
609 continue;
611 *dotdot = '.';
613 if (*arg == '^') {
614 flags = UNINTERESTING;
615 arg++;
616 limited = 1;
618 commit = get_commit_reference(arg, flags);
619 handle_one_commit(commit, &list);
622 save_commit_buffer = verbose_header;
623 track_object_refs = 0;
625 if (!merge_order) {
626 sort_by_date(&list);
627 if (limited)
628 list = limit_list(list);
629 if (topo_order)
630 sort_in_topological_order(&list);
631 show_commit_list(list);
632 } else {
633 #ifndef NO_OPENSSL
634 if (sort_list_in_merge_order(list, &process_commit)) {
635 die("merge order sort failed\n");
637 #else
638 die("merge order sort unsupported, OpenSSL not linked");
639 #endif
642 return 0;