Teach packing about "tag" objects
[git/dscho.git] / rev-list.c
blobbf61b74db84f5befafaad2d0c70de0e5da22d218
1 #include "cache.h"
2 #include "commit.h"
3 #include "tree.h"
4 #include "blob.h"
5 #include "epoch.h"
7 #define SEEN (1u << 0)
8 #define INTERESTING (1u << 1)
9 #define COUNTED (1u << 2)
10 #define SHOWN (LAST_EPOCH_FLAG << 2)
12 static const char rev_list_usage[] =
13 "usage: git-rev-list [OPTION] commit-id <commit-id>\n"
14 " --max-count=nr\n"
15 " --max-age=epoch\n"
16 " --min-age=epoch\n"
17 " --header\n"
18 " --pretty\n"
19 " --merge-order [ --show-breaks ]";
21 static int bisect_list = 0;
22 static int tree_objects = 0;
23 static int blob_objects = 0;
24 static int verbose_header = 0;
25 static int show_parents = 0;
26 static int hdr_termination = 0;
27 static const char *prefix = "";
28 static unsigned long max_age = -1;
29 static unsigned long min_age = -1;
30 static int max_count = -1;
31 static enum cmit_fmt commit_format = CMIT_FMT_RAW;
32 static int merge_order = 0;
33 static int show_breaks = 0;
34 static int stop_traversal = 0;
36 static void show_commit(struct commit *commit)
38 commit->object.flags |= SHOWN;
39 if (show_breaks) {
40 prefix = "| ";
41 if (commit->object.flags & DISCONTINUITY) {
42 prefix = "^ ";
43 } else if (commit->object.flags & BOUNDARY) {
44 prefix = "= ";
47 printf("%s%s", prefix, sha1_to_hex(commit->object.sha1));
48 if (show_parents) {
49 struct commit_list *parents = commit->parents;
50 while (parents) {
51 printf(" %s", sha1_to_hex(parents->item->object.sha1));
52 parents = parents->next;
55 putchar('\n');
56 if (verbose_header) {
57 static char pretty_header[16384];
58 pretty_print_commit(commit_format, commit->buffer, ~0, pretty_header, sizeof(pretty_header));
59 printf("%s%c", pretty_header, hdr_termination);
63 static int filter_commit(struct commit * commit)
65 if (merge_order && stop_traversal && commit->object.flags & BOUNDARY)
66 return STOP;
67 if (commit->object.flags & (UNINTERESTING|SHOWN))
68 return CONTINUE;
69 if (min_age != -1 && (commit->date > min_age))
70 return CONTINUE;
71 if (max_age != -1 && (commit->date < max_age)) {
72 if (!merge_order)
73 return STOP;
74 else {
75 stop_traversal = 1;
76 return CONTINUE;
79 if (max_count != -1 && !max_count--)
80 return STOP;
81 return DO;
84 static int process_commit(struct commit * commit)
86 int action=filter_commit(commit);
88 if (action == STOP) {
89 return STOP;
92 if (action == CONTINUE) {
93 return CONTINUE;
96 show_commit(commit);
98 return CONTINUE;
101 static struct object_list **add_object(struct object *obj, struct object_list **p, const char *name)
103 struct object_list *entry = xmalloc(sizeof(*entry));
104 entry->item = obj;
105 entry->next = NULL;
106 entry->name = name;
107 *p = entry;
108 return &entry->next;
111 static struct object_list **process_blob(struct blob *blob, struct object_list **p, const char *name)
113 struct object *obj = &blob->object;
115 if (!blob_objects)
116 return p;
117 if (obj->flags & (UNINTERESTING | SEEN))
118 return p;
119 obj->flags |= SEEN;
120 return add_object(obj, p, name);
123 static struct object_list **process_tree(struct tree *tree, struct object_list **p, const char *name)
125 struct object *obj = &tree->object;
126 struct tree_entry_list *entry;
128 if (!tree_objects)
129 return p;
130 if (obj->flags & (UNINTERESTING | SEEN))
131 return p;
132 if (parse_tree(tree) < 0)
133 die("bad tree object %s", sha1_to_hex(obj->sha1));
134 obj->flags |= SEEN;
135 p = add_object(obj, p, name);
136 for (entry = tree->entries ; entry ; entry = entry->next) {
137 if (entry->directory)
138 p = process_tree(entry->item.tree, p, entry->name);
139 else
140 p = process_blob(entry->item.blob, p, entry->name);
142 return p;
145 static void show_commit_list(struct commit_list *list)
147 struct object_list *objects = NULL, **p = &objects;
148 while (list) {
149 struct commit *commit = pop_most_recent_commit(&list, SEEN);
151 p = process_tree(commit->tree, p, "");
152 if (process_commit(commit) == STOP)
153 break;
155 while (objects) {
156 printf("%s %s\n", sha1_to_hex(objects->item->sha1), objects->name);
157 objects = objects->next;
161 static void mark_blob_uninteresting(struct blob *blob)
163 if (!blob_objects)
164 return;
165 if (blob->object.flags & UNINTERESTING)
166 return;
167 blob->object.flags |= UNINTERESTING;
170 static void mark_tree_uninteresting(struct tree *tree)
172 struct object *obj = &tree->object;
173 struct tree_entry_list *entry;
175 if (!tree_objects)
176 return;
177 if (obj->flags & UNINTERESTING)
178 return;
179 obj->flags |= UNINTERESTING;
180 if (parse_tree(tree) < 0)
181 die("bad tree %s", sha1_to_hex(obj->sha1));
182 entry = tree->entries;
183 while (entry) {
184 if (entry->directory)
185 mark_tree_uninteresting(entry->item.tree);
186 else
187 mark_blob_uninteresting(entry->item.blob);
188 entry = entry->next;
192 static void mark_parents_uninteresting(struct commit *commit)
194 struct commit_list *parents = commit->parents;
196 if (tree_objects)
197 mark_tree_uninteresting(commit->tree);
198 while (parents) {
199 struct commit *commit = parents->item;
200 commit->object.flags |= UNINTERESTING;
201 parents = parents->next;
205 static int everybody_uninteresting(struct commit_list *list)
207 while (list) {
208 struct commit *commit = list->item;
209 list = list->next;
210 if (commit->object.flags & UNINTERESTING)
211 continue;
212 return 0;
214 return 1;
218 * This is a truly stupid algorithm, but it's only
219 * used for bisection, and we just don't care enough.
221 * We care just barely enough to avoid recursing for
222 * non-merge entries.
224 static int count_distance(struct commit_list *entry)
226 int nr = 0;
228 while (entry) {
229 struct commit *commit = entry->item;
230 struct commit_list *p;
232 if (commit->object.flags & (UNINTERESTING | COUNTED))
233 break;
234 nr++;
235 commit->object.flags |= COUNTED;
236 p = commit->parents;
237 entry = p;
238 if (p) {
239 p = p->next;
240 while (p) {
241 nr += count_distance(p);
242 p = p->next;
246 return nr;
249 static void clear_distance(struct commit_list *list)
251 while (list) {
252 struct commit *commit = list->item;
253 commit->object.flags &= ~COUNTED;
254 list = list->next;
258 static struct commit_list *find_bisection(struct commit_list *list)
260 int nr, closest;
261 struct commit_list *p, *best;
263 nr = 0;
264 p = list;
265 while (p) {
266 nr++;
267 p = p->next;
269 closest = 0;
270 best = list;
272 p = list;
273 while (p) {
274 int distance = count_distance(p);
275 clear_distance(list);
276 if (nr - distance < distance)
277 distance = nr - distance;
278 if (distance > closest) {
279 best = p;
280 closest = distance;
282 p = p->next;
284 if (best)
285 best->next = NULL;
286 return best;
289 struct commit_list *limit_list(struct commit_list *list)
291 struct commit_list *newlist = NULL;
292 struct commit_list **p = &newlist;
293 do {
294 struct commit *commit = pop_most_recent_commit(&list, SEEN);
295 struct object *obj = &commit->object;
297 if (obj->flags & UNINTERESTING) {
298 mark_parents_uninteresting(commit);
299 if (everybody_uninteresting(list))
300 break;
301 continue;
303 p = &commit_list_insert(commit, p)->next;
304 } while (list);
305 if (bisect_list)
306 newlist = find_bisection(newlist);
307 return newlist;
310 int main(int argc, char **argv)
312 struct commit_list *list = NULL;
313 int i, limited = 0;
315 for (i = 1 ; i < argc; i++) {
316 int flags;
317 char *arg = argv[i];
318 unsigned char sha1[20];
319 struct commit *commit;
321 if (!strncmp(arg, "--max-count=", 12)) {
322 max_count = atoi(arg + 12);
323 continue;
325 if (!strncmp(arg, "--max-age=", 10)) {
326 max_age = atoi(arg + 10);
327 continue;
329 if (!strncmp(arg, "--min-age=", 10)) {
330 min_age = atoi(arg + 10);
331 continue;
333 if (!strcmp(arg, "--header")) {
334 verbose_header = 1;
335 continue;
337 if (!strncmp(arg, "--pretty", 8)) {
338 commit_format = get_commit_format(arg+8);
339 verbose_header = 1;
340 hdr_termination = '\n';
341 prefix = "commit ";
342 continue;
344 if (!strcmp(arg, "--parents")) {
345 show_parents = 1;
346 continue;
348 if (!strcmp(arg, "--bisect")) {
349 bisect_list = 1;
350 continue;
352 if (!strcmp(arg, "--objects")) {
353 tree_objects = 1;
354 blob_objects = 1;
355 continue;
357 if (!strncmp(arg, "--merge-order", 13)) {
358 merge_order = 1;
359 continue;
361 if (!strncmp(arg, "--show-breaks", 13)) {
362 show_breaks = 1;
363 continue;
366 flags = 0;
367 if (*arg == '^') {
368 flags = UNINTERESTING;
369 arg++;
370 limited = 1;
372 if (get_sha1(arg, sha1) || (show_breaks && !merge_order))
373 usage(rev_list_usage);
374 commit = lookup_commit_reference(sha1);
375 if (!commit || parse_commit(commit) < 0)
376 die("bad commit object %s", arg);
377 commit->object.flags |= flags;
378 commit_list_insert(commit, &list);
381 if (!list)
382 usage(rev_list_usage);
384 if (!merge_order) {
385 if (limited)
386 list = limit_list(list);
387 show_commit_list(list);
388 } else {
389 if (sort_list_in_merge_order(list, &process_commit)) {
390 die("merge order sort failed\n");
394 return 0;