Merge with Linus' current tree
[alt-git.git] / rev-list.c
blob825006374a0b028b9a1980a2aee494132f3a4145
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 (LAST_EPOCH_FLAG << 2)
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;
42 static void show_commit(struct commit *commit)
44 commit->object.flags |= SHOWN;
45 if (show_breaks) {
46 prefix = "| ";
47 if (commit->object.flags & DISCONTINUITY) {
48 prefix = "^ ";
49 } else if (commit->object.flags & BOUNDARY) {
50 prefix = "= ";
53 printf("%s%s", prefix, sha1_to_hex(commit->object.sha1));
54 if (show_parents) {
55 struct commit_list *parents = commit->parents;
56 while (parents) {
57 printf(" %s", sha1_to_hex(parents->item->object.sha1));
58 parents = parents->next;
61 putchar('\n');
62 if (verbose_header) {
63 static char pretty_header[16384];
64 pretty_print_commit(commit_format, commit->buffer, ~0, pretty_header, sizeof(pretty_header));
65 printf("%s%c", pretty_header, hdr_termination);
67 fflush(stdout);
70 static int filter_commit(struct commit * commit)
72 if (merge_order && stop_traversal && commit->object.flags & BOUNDARY)
73 return STOP;
74 if (commit->object.flags & (UNINTERESTING|SHOWN))
75 return CONTINUE;
76 if (min_age != -1 && (commit->date > min_age))
77 return CONTINUE;
78 if (max_age != -1 && (commit->date < max_age)) {
79 if (!merge_order)
80 return STOP;
81 else {
82 stop_traversal = 1;
83 return CONTINUE;
86 if (max_count != -1 && !max_count--)
87 return STOP;
88 return DO;
91 static int process_commit(struct commit * commit)
93 int action=filter_commit(commit);
95 if (action == STOP) {
96 return STOP;
99 if (action == CONTINUE) {
100 return CONTINUE;
103 show_commit(commit);
105 return CONTINUE;
108 static struct object_list **add_object(struct object *obj, struct object_list **p, const char *name)
110 struct object_list *entry = xmalloc(sizeof(*entry));
111 entry->item = obj;
112 entry->next = *p;
113 entry->name = name;
114 *p = entry;
115 return &entry->next;
118 static struct object_list **process_blob(struct blob *blob, struct object_list **p, const char *name)
120 struct object *obj = &blob->object;
122 if (!blob_objects)
123 return p;
124 if (obj->flags & (UNINTERESTING | SEEN))
125 return p;
126 obj->flags |= SEEN;
127 return add_object(obj, p, name);
130 static struct object_list **process_tree(struct tree *tree, struct object_list **p, const char *name)
132 struct object *obj = &tree->object;
133 struct tree_entry_list *entry;
135 if (!tree_objects)
136 return p;
137 if (obj->flags & (UNINTERESTING | SEEN))
138 return p;
139 if (parse_tree(tree) < 0)
140 die("bad tree object %s", sha1_to_hex(obj->sha1));
141 obj->flags |= SEEN;
142 p = add_object(obj, p, name);
143 for (entry = tree->entries ; entry ; entry = entry->next) {
144 if (entry->directory)
145 p = process_tree(entry->item.tree, p, entry->name);
146 else
147 p = process_blob(entry->item.blob, p, entry->name);
149 return p;
152 static struct object_list *pending_objects = NULL;
154 static void show_commit_list(struct commit_list *list)
156 struct object_list *objects = NULL, **p = &objects, *pending;
157 while (list) {
158 struct commit *commit = pop_most_recent_commit(&list, SEEN);
160 p = process_tree(commit->tree, p, "");
161 if (process_commit(commit) == STOP)
162 break;
164 for (pending = pending_objects; pending; pending = pending->next) {
165 struct object *obj = pending->item;
166 const char *name = pending->name;
167 if (obj->flags & (UNINTERESTING | SEEN))
168 continue;
169 if (obj->type == tag_type) {
170 obj->flags |= SEEN;
171 p = add_object(obj, p, name);
172 continue;
174 if (obj->type == tree_type) {
175 p = process_tree((struct tree *)obj, p, name);
176 continue;
178 if (obj->type == blob_type) {
179 p = process_blob((struct blob *)obj, p, name);
180 continue;
182 die("unknown pending object %s (%s)", sha1_to_hex(obj->sha1), name);
184 while (objects) {
185 printf("%s %s\n", sha1_to_hex(objects->item->sha1), objects->name);
186 objects = objects->next;
190 static void mark_blob_uninteresting(struct blob *blob)
192 if (!blob_objects)
193 return;
194 if (blob->object.flags & UNINTERESTING)
195 return;
196 blob->object.flags |= UNINTERESTING;
199 static void mark_tree_uninteresting(struct tree *tree)
201 struct object *obj = &tree->object;
202 struct tree_entry_list *entry;
204 if (!tree_objects)
205 return;
206 if (obj->flags & UNINTERESTING)
207 return;
208 obj->flags |= UNINTERESTING;
209 if (parse_tree(tree) < 0)
210 die("bad tree %s", sha1_to_hex(obj->sha1));
211 entry = tree->entries;
212 while (entry) {
213 if (entry->directory)
214 mark_tree_uninteresting(entry->item.tree);
215 else
216 mark_blob_uninteresting(entry->item.blob);
217 entry = entry->next;
221 static void mark_parents_uninteresting(struct commit *commit)
223 struct commit_list *parents = commit->parents;
225 if (tree_objects)
226 mark_tree_uninteresting(commit->tree);
227 while (parents) {
228 struct commit *commit = parents->item;
229 commit->object.flags |= UNINTERESTING;
230 parents = parents->next;
234 static int everybody_uninteresting(struct commit_list *list)
236 while (list) {
237 struct commit *commit = list->item;
238 list = list->next;
239 if (commit->object.flags & UNINTERESTING)
240 continue;
241 return 0;
243 return 1;
247 * This is a truly stupid algorithm, but it's only
248 * used for bisection, and we just don't care enough.
250 * We care just barely enough to avoid recursing for
251 * non-merge entries.
253 static int count_distance(struct commit_list *entry)
255 int nr = 0;
257 while (entry) {
258 struct commit *commit = entry->item;
259 struct commit_list *p;
261 if (commit->object.flags & (UNINTERESTING | COUNTED))
262 break;
263 nr++;
264 commit->object.flags |= COUNTED;
265 p = commit->parents;
266 entry = p;
267 if (p) {
268 p = p->next;
269 while (p) {
270 nr += count_distance(p);
271 p = p->next;
275 return nr;
278 static void clear_distance(struct commit_list *list)
280 while (list) {
281 struct commit *commit = list->item;
282 commit->object.flags &= ~COUNTED;
283 list = list->next;
287 static struct commit_list *find_bisection(struct commit_list *list)
289 int nr, closest;
290 struct commit_list *p, *best;
292 nr = 0;
293 p = list;
294 while (p) {
295 nr++;
296 p = p->next;
298 closest = 0;
299 best = list;
301 p = list;
302 while (p) {
303 int distance = count_distance(p);
304 clear_distance(list);
305 if (nr - distance < distance)
306 distance = nr - distance;
307 if (distance > closest) {
308 best = p;
309 closest = distance;
311 p = p->next;
313 if (best)
314 best->next = NULL;
315 return best;
318 static struct commit_list *limit_list(struct commit_list *list)
320 struct commit_list *newlist = NULL;
321 struct commit_list **p = &newlist;
322 while (list) {
323 struct commit *commit = pop_most_recent_commit(&list, SEEN);
324 struct object *obj = &commit->object;
326 if (unpacked && has_sha1_pack(obj->sha1))
327 obj->flags |= UNINTERESTING;
328 if (obj->flags & UNINTERESTING) {
329 mark_parents_uninteresting(commit);
330 if (everybody_uninteresting(list))
331 break;
332 continue;
334 p = &commit_list_insert(commit, p)->next;
336 if (bisect_list)
337 newlist = find_bisection(newlist);
338 return newlist;
341 static void add_pending_object(struct object *obj, const char *name)
343 add_object(obj, &pending_objects, name);
346 static struct commit *get_commit_reference(const char *name, unsigned int flags)
348 unsigned char sha1[20];
349 struct object *object;
351 if (get_sha1(name, sha1))
352 usage(rev_list_usage);
353 object = parse_object(sha1);
354 if (!object)
355 die("bad object %s", name);
358 * Tag object? Look what it points to..
360 if (object->type == tag_type) {
361 struct tag *tag = (struct tag *) object;
362 object->flags |= flags;
363 if (tag_objects && !(object->flags & UNINTERESTING))
364 add_pending_object(object, tag->tag);
365 object = tag->tagged;
369 * Commit object? Just return it, we'll do all the complex
370 * reachability crud.
372 if (object->type == commit_type) {
373 struct commit *commit = (struct commit *)object;
374 object->flags |= flags;
375 if (parse_commit(commit) < 0)
376 die("unable to parse commit %s", name);
377 return commit;
381 * Tree object? Either mark it uniniteresting, or add it
382 * to the list of objects to look at later..
384 if (object->type == tree_type) {
385 struct tree *tree = (struct tree *)object;
386 if (!tree_objects)
387 return NULL;
388 if (flags & UNINTERESTING) {
389 mark_tree_uninteresting(tree);
390 return NULL;
392 add_pending_object(object, "");
393 return NULL;
397 * Blob object? You know the drill by now..
399 if (object->type == blob_type) {
400 struct blob *blob = (struct blob *)object;
401 if (!blob_objects)
402 return NULL;
403 if (flags & UNINTERESTING) {
404 mark_blob_uninteresting(blob);
405 return NULL;
407 add_pending_object(object, "");
408 return NULL;
410 die("%s is unknown object", name);
413 int main(int argc, char **argv)
415 struct commit_list *list = NULL;
416 int i, limited = 0;
418 for (i = 1 ; i < argc; i++) {
419 int flags;
420 char *arg = argv[i];
421 struct commit *commit;
423 if (!strncmp(arg, "--max-count=", 12)) {
424 max_count = atoi(arg + 12);
425 continue;
427 if (!strncmp(arg, "--max-age=", 10)) {
428 max_age = atoi(arg + 10);
429 continue;
431 if (!strncmp(arg, "--min-age=", 10)) {
432 min_age = atoi(arg + 10);
433 continue;
435 if (!strcmp(arg, "--header")) {
436 verbose_header = 1;
437 continue;
439 if (!strncmp(arg, "--pretty", 8)) {
440 commit_format = get_commit_format(arg+8);
441 verbose_header = 1;
442 hdr_termination = '\n';
443 prefix = "commit ";
444 continue;
446 if (!strcmp(arg, "--parents")) {
447 show_parents = 1;
448 continue;
450 if (!strcmp(arg, "--bisect")) {
451 bisect_list = 1;
452 continue;
454 if (!strcmp(arg, "--objects")) {
455 tag_objects = 1;
456 tree_objects = 1;
457 blob_objects = 1;
458 continue;
460 if (!strcmp(arg, "--unpacked")) {
461 unpacked = 1;
462 limited = 1;
463 continue;
465 if (!strncmp(arg, "--merge-order", 13)) {
466 merge_order = 1;
467 continue;
469 if (!strncmp(arg, "--show-breaks", 13)) {
470 show_breaks = 1;
471 continue;
474 flags = 0;
475 if (*arg == '^') {
476 flags = UNINTERESTING;
477 arg++;
478 limited = 1;
480 if (show_breaks && !merge_order)
481 usage(rev_list_usage);
482 commit = get_commit_reference(arg, flags);
483 if (!commit)
484 continue;
485 insert_by_date(&list, commit);
488 if (!merge_order) {
489 if (limited)
490 list = limit_list(list);
491 show_commit_list(list);
492 } else {
493 if (sort_list_in_merge_order(list, &process_commit)) {
494 die("merge order sort failed\n");
498 return 0;