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"
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;
47 if (commit->object.flags & DISCONTINUITY) {
49 } else if (commit->object.flags & BOUNDARY) {
53 printf("%s%s", prefix, sha1_to_hex(commit->object.sha1));
55 struct commit_list *parents = commit->parents;
57 printf(" %s", sha1_to_hex(parents->item->object.sha1));
58 parents = parents->next;
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);
69 static int filter_commit(struct commit * commit)
71 if (merge_order && stop_traversal && commit->object.flags & BOUNDARY)
73 if (commit->object.flags & (UNINTERESTING|SHOWN))
75 if (min_age != -1 && (commit->date > min_age))
77 if (max_age != -1 && (commit->date < max_age)) {
85 if (max_count != -1 && !max_count--)
90 static int process_commit(struct commit * commit)
92 int action=filter_commit(commit);
98 if (action == CONTINUE) {
107 static struct object_list **add_object(struct object *obj, struct object_list **p, const char *name)
109 struct object_list *entry = xmalloc(sizeof(*entry));
117 static struct object_list **process_blob(struct blob *blob, struct object_list **p, const char *name)
119 struct object *obj = &blob->object;
123 if (obj->flags & (UNINTERESTING | SEEN))
126 return add_object(obj, p, name);
129 static struct object_list **process_tree(struct tree *tree, struct object_list **p, const char *name)
131 struct object *obj = &tree->object;
132 struct tree_entry_list *entry;
136 if (obj->flags & (UNINTERESTING | SEEN))
138 if (parse_tree(tree) < 0)
139 die("bad tree object %s", sha1_to_hex(obj->sha1));
141 p = add_object(obj, p, name);
142 for (entry = tree->entries ; entry ; entry = entry->next) {
143 if (entry->directory)
144 p = process_tree(entry->item.tree, p, entry->name);
146 p = process_blob(entry->item.blob, p, entry->name);
151 static struct object_list *pending_objects = NULL;
153 static void show_commit_list(struct commit_list *list)
155 struct object_list *objects = NULL, **p = &objects, *pending;
157 struct commit *commit = pop_most_recent_commit(&list, SEEN);
159 p = process_tree(commit->tree, p, "");
160 if (process_commit(commit) == STOP)
163 for (pending = pending_objects; pending; pending = pending->next) {
164 struct object *obj = pending->item;
165 const char *name = pending->name;
166 if (obj->flags & (UNINTERESTING | SEEN))
168 if (obj->type == tag_type) {
170 p = add_object(obj, p, name);
173 if (obj->type == tree_type) {
174 p = process_tree((struct tree *)obj, p, name);
177 if (obj->type == blob_type) {
178 p = process_blob((struct blob *)obj, p, name);
181 die("unknown pending object %s (%s)", sha1_to_hex(obj->sha1), name);
184 printf("%s %s\n", sha1_to_hex(objects->item->sha1), objects->name);
185 objects = objects->next;
189 static void mark_blob_uninteresting(struct blob *blob)
193 if (blob->object.flags & UNINTERESTING)
195 blob->object.flags |= UNINTERESTING;
198 static void mark_tree_uninteresting(struct tree *tree)
200 struct object *obj = &tree->object;
201 struct tree_entry_list *entry;
205 if (obj->flags & UNINTERESTING)
207 obj->flags |= UNINTERESTING;
208 if (parse_tree(tree) < 0)
209 die("bad tree %s", sha1_to_hex(obj->sha1));
210 entry = tree->entries;
212 if (entry->directory)
213 mark_tree_uninteresting(entry->item.tree);
215 mark_blob_uninteresting(entry->item.blob);
220 static void mark_parents_uninteresting(struct commit *commit)
222 struct commit_list *parents = commit->parents;
225 mark_tree_uninteresting(commit->tree);
227 struct commit *commit = parents->item;
228 commit->object.flags |= UNINTERESTING;
229 parents = parents->next;
233 static int everybody_uninteresting(struct commit_list *list)
236 struct commit *commit = list->item;
238 if (commit->object.flags & UNINTERESTING)
246 * This is a truly stupid algorithm, but it's only
247 * used for bisection, and we just don't care enough.
249 * We care just barely enough to avoid recursing for
252 static int count_distance(struct commit_list *entry)
257 struct commit *commit = entry->item;
258 struct commit_list *p;
260 if (commit->object.flags & (UNINTERESTING | COUNTED))
263 commit->object.flags |= COUNTED;
269 nr += count_distance(p);
277 static void clear_distance(struct commit_list *list)
280 struct commit *commit = list->item;
281 commit->object.flags &= ~COUNTED;
286 static struct commit_list *find_bisection(struct commit_list *list)
289 struct commit_list *p, *best;
302 int distance = count_distance(p);
303 clear_distance(list);
304 if (nr - distance < distance)
305 distance = nr - distance;
306 if (distance > closest) {
317 static struct commit_list *limit_list(struct commit_list *list)
319 struct commit_list *newlist = NULL;
320 struct commit_list **p = &newlist;
322 struct commit *commit = pop_most_recent_commit(&list, SEEN);
323 struct object *obj = &commit->object;
325 if (unpacked && has_sha1_pack(obj->sha1))
326 obj->flags |= UNINTERESTING;
327 if (obj->flags & UNINTERESTING) {
328 mark_parents_uninteresting(commit);
329 if (everybody_uninteresting(list))
333 p = &commit_list_insert(commit, p)->next;
336 newlist = find_bisection(newlist);
340 static void add_pending_object(struct object *obj, const char *name)
342 add_object(obj, &pending_objects, name);
345 static struct commit *get_commit_reference(const char *name, unsigned int flags)
347 unsigned char sha1[20];
348 struct object *object;
350 if (get_sha1(name, sha1))
351 usage(rev_list_usage);
352 object = parse_object(sha1);
354 die("bad object %s", name);
357 * Tag object? Look what it points to..
359 if (object->type == tag_type) {
360 struct tag *tag = (struct tag *) object;
361 object->flags |= flags;
362 if (tag_objects && !(object->flags & UNINTERESTING))
363 add_pending_object(object, tag->tag);
364 object = tag->tagged;
368 * Commit object? Just return it, we'll do all the complex
371 if (object->type == commit_type) {
372 struct commit *commit = (struct commit *)object;
373 object->flags |= flags;
374 if (parse_commit(commit) < 0)
375 die("unable to parse commit %s", name);
380 * Tree object? Either mark it uniniteresting, or add it
381 * to the list of objects to look at later..
383 if (object->type == tree_type) {
384 struct tree *tree = (struct tree *)object;
387 if (flags & UNINTERESTING) {
388 mark_tree_uninteresting(tree);
391 add_pending_object(object, "");
396 * Blob object? You know the drill by now..
398 if (object->type == blob_type) {
399 struct blob *blob = (struct blob *)object;
402 if (flags & UNINTERESTING) {
403 mark_blob_uninteresting(blob);
406 add_pending_object(object, "");
409 die("%s is unknown object", name);
412 int main(int argc, char **argv)
414 struct commit_list *list = NULL;
417 for (i = 1 ; i < argc; i++) {
420 struct commit *commit;
422 if (!strncmp(arg, "--max-count=", 12)) {
423 max_count = atoi(arg + 12);
426 if (!strncmp(arg, "--max-age=", 10)) {
427 max_age = atoi(arg + 10);
430 if (!strncmp(arg, "--min-age=", 10)) {
431 min_age = atoi(arg + 10);
434 if (!strcmp(arg, "--header")) {
438 if (!strncmp(arg, "--pretty", 8)) {
439 commit_format = get_commit_format(arg+8);
441 hdr_termination = '\n';
445 if (!strcmp(arg, "--parents")) {
449 if (!strcmp(arg, "--bisect")) {
453 if (!strcmp(arg, "--objects")) {
459 if (!strcmp(arg, "--unpacked")) {
464 if (!strncmp(arg, "--merge-order", 13)) {
468 if (!strncmp(arg, "--show-breaks", 13)) {
475 flags = UNINTERESTING;
479 if (show_breaks && !merge_order)
480 usage(rev_list_usage);
481 commit = get_commit_reference(arg, flags);
484 commit_list_insert(commit, &list);
489 list = limit_list(list);
490 show_commit_list(list);
492 if (sort_list_in_merge_order(list, &process_commit)) {
493 die("merge order sort failed\n");