Deb Packaging fixes: Build against Mozilla libs for Debian, conflict with "git"
[git/repo.git] / rev-list.c
blob46a35d30e154d493d9f5c7b16af067023cdf01d5
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 "usage: 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 " --bisect\n"
19 " --objects\n"
20 " --unpacked\n"
21 " --header\n"
22 " --pretty\n"
23 " --merge-order [ --show-breaks ]";
25 static int unpacked = 0;
26 static int bisect_list = 0;
27 static int tag_objects = 0;
28 static int tree_objects = 0;
29 static int blob_objects = 0;
30 static int verbose_header = 0;
31 static int show_parents = 0;
32 static int hdr_termination = 0;
33 static const char *prefix = "";
34 static unsigned long max_age = -1;
35 static unsigned long min_age = -1;
36 static int max_count = -1;
37 static enum cmit_fmt commit_format = CMIT_FMT_RAW;
38 static int merge_order = 0;
39 static int show_breaks = 0;
40 static int stop_traversal = 0;
41 static int topo_order = 0;
43 static void show_commit(struct commit *commit)
45 commit->object.flags |= SHOWN;
46 if (show_breaks) {
47 prefix = "| ";
48 if (commit->object.flags & DISCONTINUITY) {
49 prefix = "^ ";
50 } else if (commit->object.flags & BOUNDARY) {
51 prefix = "= ";
54 printf("%s%s", prefix, sha1_to_hex(commit->object.sha1));
55 if (show_parents) {
56 struct commit_list *parents = commit->parents;
57 while (parents) {
58 printf(" %s", sha1_to_hex(parents->item->object.sha1));
59 parents = parents->next;
62 putchar('\n');
63 if (verbose_header) {
64 static char pretty_header[16384];
65 pretty_print_commit(commit_format, commit->buffer, ~0, pretty_header, sizeof(pretty_header));
66 printf("%s%c", pretty_header, hdr_termination);
68 fflush(stdout);
71 static int filter_commit(struct commit * commit)
73 if (stop_traversal && (commit->object.flags & BOUNDARY))
74 return STOP;
75 if (commit->object.flags & (UNINTERESTING|SHOWN))
76 return CONTINUE;
77 if (min_age != -1 && (commit->date > min_age))
78 return CONTINUE;
79 if (max_age != -1 && (commit->date < max_age)) {
80 stop_traversal=1;
81 return merge_order?CONTINUE:STOP;
83 if (max_count != -1 && !max_count--)
84 return STOP;
85 return DO;
88 static int process_commit(struct commit * commit)
90 int action=filter_commit(commit);
92 if (action == STOP) {
93 return STOP;
96 if (action == CONTINUE) {
97 return CONTINUE;
100 show_commit(commit);
102 return CONTINUE;
105 static struct object_list **add_object(struct object *obj, struct object_list **p, const char *name)
107 struct object_list *entry = xmalloc(sizeof(*entry));
108 entry->item = obj;
109 entry->next = *p;
110 entry->name = name;
111 *p = entry;
112 return &entry->next;
115 static struct object_list **process_blob(struct blob *blob, struct object_list **p, const char *name)
117 struct object *obj = &blob->object;
119 if (!blob_objects)
120 return p;
121 if (obj->flags & (UNINTERESTING | SEEN))
122 return p;
123 obj->flags |= SEEN;
124 return add_object(obj, p, name);
127 static struct object_list **process_tree(struct tree *tree, struct object_list **p, const char *name)
129 struct object *obj = &tree->object;
130 struct tree_entry_list *entry;
132 if (!tree_objects)
133 return p;
134 if (obj->flags & (UNINTERESTING | SEEN))
135 return p;
136 if (parse_tree(tree) < 0)
137 die("bad tree object %s", sha1_to_hex(obj->sha1));
138 obj->flags |= SEEN;
139 p = add_object(obj, p, name);
140 for (entry = tree->entries ; entry ; entry = entry->next) {
141 if (entry->directory)
142 p = process_tree(entry->item.tree, p, entry->name);
143 else
144 p = process_blob(entry->item.blob, p, entry->name);
146 return p;
149 static struct object_list *pending_objects = NULL;
151 static void show_commit_list(struct commit_list *list)
153 struct object_list *objects = NULL, **p = &objects, *pending;
154 while (list) {
155 struct commit *commit = pop_most_recent_commit(&list, SEEN);
157 p = process_tree(commit->tree, p, "");
158 if (process_commit(commit) == STOP)
159 break;
161 for (pending = pending_objects; pending; pending = pending->next) {
162 struct object *obj = pending->item;
163 const char *name = pending->name;
164 if (obj->flags & (UNINTERESTING | SEEN))
165 continue;
166 if (obj->type == tag_type) {
167 obj->flags |= SEEN;
168 p = add_object(obj, p, name);
169 continue;
171 if (obj->type == tree_type) {
172 p = process_tree((struct tree *)obj, p, name);
173 continue;
175 if (obj->type == blob_type) {
176 p = process_blob((struct blob *)obj, p, name);
177 continue;
179 die("unknown pending object %s (%s)", sha1_to_hex(obj->sha1), name);
181 while (objects) {
182 printf("%s %s\n", sha1_to_hex(objects->item->sha1), objects->name);
183 objects = objects->next;
187 static void mark_blob_uninteresting(struct blob *blob)
189 if (!blob_objects)
190 return;
191 if (blob->object.flags & UNINTERESTING)
192 return;
193 blob->object.flags |= UNINTERESTING;
196 static void mark_tree_uninteresting(struct tree *tree)
198 struct object *obj = &tree->object;
199 struct tree_entry_list *entry;
201 if (!tree_objects)
202 return;
203 if (obj->flags & UNINTERESTING)
204 return;
205 obj->flags |= UNINTERESTING;
206 if (!has_sha1_file(obj->sha1))
207 return;
208 if (parse_tree(tree) < 0)
209 die("bad tree %s", sha1_to_hex(obj->sha1));
210 entry = tree->entries;
211 while (entry) {
212 if (entry->directory)
213 mark_tree_uninteresting(entry->item.tree);
214 else
215 mark_blob_uninteresting(entry->item.blob);
216 entry = entry->next;
220 static void mark_parents_uninteresting(struct commit *commit)
222 struct commit_list *parents = commit->parents;
224 if (tree_objects)
225 mark_tree_uninteresting(commit->tree);
226 while (parents) {
227 struct commit *commit = parents->item;
228 commit->object.flags |= UNINTERESTING;
231 * A missing commit is ok iff its parent is marked
232 * uninteresting.
234 * We just mark such a thing parsed, so that when
235 * it is popped next time around, we won't be trying
236 * to parse it and get an error.
238 if (!has_sha1_file(commit->object.sha1))
239 commit->object.parsed = 1;
240 parents = parents->next;
244 static int everybody_uninteresting(struct commit_list *orig)
246 struct commit_list *list = orig;
247 while (list) {
248 struct commit *commit = list->item;
249 list = list->next;
250 if (commit->object.flags & UNINTERESTING)
251 continue;
252 return 0;
256 * Ok, go back and mark all the edge trees uninteresting,
257 * since otherwise we can have situations where a parent
258 * that was marked uninteresting (and we never even had
259 * to look at) had lots of objects that we don't want to
260 * include.
262 * NOTE! This still doesn't mean that the object list is
263 * "correct", since we may end up listing objects that
264 * even older commits (that we don't list) do actually
265 * reference, but it gets us to a minimal list (or very
266 * close) in practice.
268 if (!tree_objects)
269 return 1;
271 while (orig) {
272 struct commit *commit = orig->item;
273 if (!parse_commit(commit) && commit->tree)
274 mark_tree_uninteresting(commit->tree);
275 orig = orig->next;
277 return 1;
281 * This is a truly stupid algorithm, but it's only
282 * used for bisection, and we just don't care enough.
284 * We care just barely enough to avoid recursing for
285 * non-merge entries.
287 static int count_distance(struct commit_list *entry)
289 int nr = 0;
291 while (entry) {
292 struct commit *commit = entry->item;
293 struct commit_list *p;
295 if (commit->object.flags & (UNINTERESTING | COUNTED))
296 break;
297 nr++;
298 commit->object.flags |= COUNTED;
299 p = commit->parents;
300 entry = p;
301 if (p) {
302 p = p->next;
303 while (p) {
304 nr += count_distance(p);
305 p = p->next;
309 return nr;
312 static void clear_distance(struct commit_list *list)
314 while (list) {
315 struct commit *commit = list->item;
316 commit->object.flags &= ~COUNTED;
317 list = list->next;
321 static struct commit_list *find_bisection(struct commit_list *list)
323 int nr, closest;
324 struct commit_list *p, *best;
326 nr = 0;
327 p = list;
328 while (p) {
329 nr++;
330 p = p->next;
332 closest = 0;
333 best = list;
335 p = list;
336 while (p) {
337 int distance = count_distance(p);
338 clear_distance(list);
339 if (nr - distance < distance)
340 distance = nr - distance;
341 if (distance > closest) {
342 best = p;
343 closest = distance;
345 p = p->next;
347 if (best)
348 best->next = NULL;
349 return best;
352 static struct commit_list *limit_list(struct commit_list *list)
354 struct commit_list *newlist = NULL;
355 struct commit_list **p = &newlist;
356 while (list) {
357 struct commit *commit = pop_most_recent_commit(&list, SEEN);
358 struct object *obj = &commit->object;
360 if (unpacked && has_sha1_pack(obj->sha1))
361 obj->flags |= UNINTERESTING;
362 if (obj->flags & UNINTERESTING) {
363 mark_parents_uninteresting(commit);
364 if (everybody_uninteresting(list))
365 break;
366 continue;
368 p = &commit_list_insert(commit, p)->next;
370 if (bisect_list)
371 newlist = find_bisection(newlist);
372 return newlist;
375 static void add_pending_object(struct object *obj, const char *name)
377 add_object(obj, &pending_objects, name);
380 static struct commit *get_commit_reference(const char *name, unsigned int flags)
382 unsigned char sha1[20];
383 struct object *object;
385 if (get_sha1(name, sha1))
386 usage(rev_list_usage);
387 object = parse_object(sha1);
388 if (!object)
389 die("bad object %s", name);
392 * Tag object? Look what it points to..
394 while (object->type == tag_type) {
395 struct tag *tag = (struct tag *) object;
396 object->flags |= flags;
397 if (tag_objects && !(object->flags & UNINTERESTING))
398 add_pending_object(object, tag->tag);
399 object = parse_object(tag->tagged->sha1);
403 * Commit object? Just return it, we'll do all the complex
404 * reachability crud.
406 if (object->type == commit_type) {
407 struct commit *commit = (struct commit *)object;
408 object->flags |= flags;
409 if (parse_commit(commit) < 0)
410 die("unable to parse commit %s", name);
411 if (flags & UNINTERESTING)
412 mark_parents_uninteresting(commit);
413 return commit;
417 * Tree object? Either mark it uniniteresting, or add it
418 * to the list of objects to look at later..
420 if (object->type == tree_type) {
421 struct tree *tree = (struct tree *)object;
422 if (!tree_objects)
423 return NULL;
424 if (flags & UNINTERESTING) {
425 mark_tree_uninteresting(tree);
426 return NULL;
428 add_pending_object(object, "");
429 return NULL;
433 * Blob object? You know the drill by now..
435 if (object->type == blob_type) {
436 struct blob *blob = (struct blob *)object;
437 if (!blob_objects)
438 return NULL;
439 if (flags & UNINTERESTING) {
440 mark_blob_uninteresting(blob);
441 return NULL;
443 add_pending_object(object, "");
444 return NULL;
446 die("%s is unknown object", name);
449 int main(int argc, char **argv)
451 struct commit_list *list = NULL;
452 int i, limited = 0;
454 for (i = 1 ; i < argc; i++) {
455 int flags;
456 char *arg = argv[i];
457 struct commit *commit;
459 if (!strncmp(arg, "--max-count=", 12)) {
460 max_count = atoi(arg + 12);
461 continue;
463 if (!strncmp(arg, "--max-age=", 10)) {
464 max_age = atoi(arg + 10);
465 continue;
467 if (!strncmp(arg, "--min-age=", 10)) {
468 min_age = atoi(arg + 10);
469 continue;
471 if (!strcmp(arg, "--header")) {
472 verbose_header = 1;
473 continue;
475 if (!strncmp(arg, "--pretty", 8)) {
476 commit_format = get_commit_format(arg+8);
477 verbose_header = 1;
478 hdr_termination = '\n';
479 prefix = "commit ";
480 continue;
482 if (!strcmp(arg, "--parents")) {
483 show_parents = 1;
484 continue;
486 if (!strcmp(arg, "--bisect")) {
487 bisect_list = 1;
488 continue;
490 if (!strcmp(arg, "--objects")) {
491 tag_objects = 1;
492 tree_objects = 1;
493 blob_objects = 1;
494 continue;
496 if (!strcmp(arg, "--unpacked")) {
497 unpacked = 1;
498 limited = 1;
499 continue;
501 if (!strcmp(arg, "--merge-order")) {
502 merge_order = 1;
503 continue;
505 if (!strcmp(arg, "--show-breaks")) {
506 show_breaks = 1;
507 continue;
509 if (!strcmp(arg, "--topo-order")) {
510 topo_order = 1;
511 limited = 1;
512 continue;
515 flags = 0;
516 if (*arg == '^') {
517 flags = UNINTERESTING;
518 arg++;
519 limited = 1;
521 if (show_breaks && !merge_order)
522 usage(rev_list_usage);
523 commit = get_commit_reference(arg, flags);
524 if (!commit)
525 continue;
526 if (commit->object.flags & SEEN)
527 continue;
528 commit->object.flags |= SEEN;
529 commit_list_insert(commit, &list);
532 if (!merge_order) {
533 sort_by_date(&list);
534 if (limited)
535 list = limit_list(list);
536 if (topo_order)
537 sort_in_topological_order(&list);
538 show_commit_list(list);
539 } else {
540 if (sort_list_in_merge_order(list, &process_commit)) {
541 die("merge order sort failed\n");
545 return 0;