9 #define INTERESTING (1u << 1)
10 #define COUNTED (1u << 2)
11 #define SHOWN (1u << 3)
12 #define DUPCHECK (1u << 4)
14 static const char rev_list_usage[] =
15 "usage: git-rev-list [OPTION] commit-id <commit-id>\n"
24 " --merge-order [ --show-breaks ]";
26 static int unpacked = 0;
27 static int bisect_list = 0;
28 static int tag_objects = 0;
29 static int tree_objects = 0;
30 static int blob_objects = 0;
31 static int verbose_header = 0;
32 static int show_parents = 0;
33 static int hdr_termination = 0;
34 static const char *prefix = "";
35 static unsigned long max_age = -1;
36 static unsigned long min_age = -1;
37 static int max_count = -1;
38 static enum cmit_fmt commit_format = CMIT_FMT_RAW;
39 static int merge_order = 0;
40 static int show_breaks = 0;
41 static int stop_traversal = 0;
42 static int topo_order = 0;
44 static void show_commit(struct commit *commit)
46 commit->object.flags |= SHOWN;
49 if (commit->object.flags & DISCONTINUITY) {
51 } else if (commit->object.flags & BOUNDARY) {
55 printf("%s%s", prefix, sha1_to_hex(commit->object.sha1));
57 struct commit_list *parents = commit->parents;
59 printf(" %s", sha1_to_hex(parents->item->object.sha1));
60 parents = parents->next;
65 static char pretty_header[16384];
66 pretty_print_commit(commit_format, commit->buffer, ~0, pretty_header, sizeof(pretty_header));
67 printf("%s%c", pretty_header, hdr_termination);
72 static int filter_commit(struct commit * commit)
74 if (stop_traversal && (commit->object.flags & BOUNDARY))
76 if (commit->object.flags & (UNINTERESTING|SHOWN))
78 if (min_age != -1 && (commit->date > min_age))
80 if (max_age != -1 && (commit->date < max_age)) {
82 return merge_order?CONTINUE:STOP;
84 if (max_count != -1 && !max_count--)
89 static int process_commit(struct commit * commit)
91 int action=filter_commit(commit);
97 if (action == CONTINUE) {
106 static struct object_list **add_object(struct object *obj, struct object_list **p, const char *name)
108 struct object_list *entry = xmalloc(sizeof(*entry));
116 static struct object_list **process_blob(struct blob *blob, struct object_list **p, const char *name)
118 struct object *obj = &blob->object;
122 if (obj->flags & (UNINTERESTING | SEEN))
125 return add_object(obj, p, name);
128 static struct object_list **process_tree(struct tree *tree, struct object_list **p, const char *name)
130 struct object *obj = &tree->object;
131 struct tree_entry_list *entry;
135 if (obj->flags & (UNINTERESTING | SEEN))
137 if (parse_tree(tree) < 0)
138 die("bad tree object %s", sha1_to_hex(obj->sha1));
140 p = add_object(obj, p, name);
141 for (entry = tree->entries ; entry ; entry = entry->next) {
142 if (entry->directory)
143 p = process_tree(entry->item.tree, p, entry->name);
145 p = process_blob(entry->item.blob, p, entry->name);
150 static struct object_list *pending_objects = NULL;
152 static void show_commit_list(struct commit_list *list)
154 struct object_list *objects = NULL, **p = &objects, *pending;
156 struct commit *commit = pop_most_recent_commit(&list, SEEN);
158 p = process_tree(commit->tree, p, "");
159 if (process_commit(commit) == STOP)
162 for (pending = pending_objects; pending; pending = pending->next) {
163 struct object *obj = pending->item;
164 const char *name = pending->name;
165 if (obj->flags & (UNINTERESTING | SEEN))
167 if (obj->type == tag_type) {
169 p = add_object(obj, p, name);
172 if (obj->type == tree_type) {
173 p = process_tree((struct tree *)obj, p, name);
176 if (obj->type == blob_type) {
177 p = process_blob((struct blob *)obj, p, name);
180 die("unknown pending object %s (%s)", sha1_to_hex(obj->sha1), name);
183 printf("%s %s\n", sha1_to_hex(objects->item->sha1), objects->name);
184 objects = objects->next;
188 static void mark_blob_uninteresting(struct blob *blob)
192 if (blob->object.flags & UNINTERESTING)
194 blob->object.flags |= UNINTERESTING;
197 static void mark_tree_uninteresting(struct tree *tree)
199 struct object *obj = &tree->object;
200 struct tree_entry_list *entry;
204 if (obj->flags & UNINTERESTING)
206 obj->flags |= UNINTERESTING;
207 if (parse_tree(tree) < 0)
208 die("bad tree %s", sha1_to_hex(obj->sha1));
209 entry = tree->entries;
211 if (entry->directory)
212 mark_tree_uninteresting(entry->item.tree);
214 mark_blob_uninteresting(entry->item.blob);
219 static void mark_parents_uninteresting(struct commit *commit)
221 struct commit_list *parents = commit->parents;
224 mark_tree_uninteresting(commit->tree);
226 struct commit *commit = parents->item;
227 commit->object.flags |= UNINTERESTING;
228 parents = parents->next;
232 static int everybody_uninteresting(struct commit_list *list)
235 struct commit *commit = list->item;
237 if (commit->object.flags & UNINTERESTING)
245 * This is a truly stupid algorithm, but it's only
246 * used for bisection, and we just don't care enough.
248 * We care just barely enough to avoid recursing for
251 static int count_distance(struct commit_list *entry)
256 struct commit *commit = entry->item;
257 struct commit_list *p;
259 if (commit->object.flags & (UNINTERESTING | COUNTED))
262 commit->object.flags |= COUNTED;
268 nr += count_distance(p);
276 static void clear_distance(struct commit_list *list)
279 struct commit *commit = list->item;
280 commit->object.flags &= ~COUNTED;
285 static struct commit_list *find_bisection(struct commit_list *list)
288 struct commit_list *p, *best;
301 int distance = count_distance(p);
302 clear_distance(list);
303 if (nr - distance < distance)
304 distance = nr - distance;
305 if (distance > closest) {
316 static struct commit_list *limit_list(struct commit_list *list)
318 struct commit_list *newlist = NULL;
319 struct commit_list **p = &newlist;
321 struct commit *commit = pop_most_recent_commit(&list, SEEN);
322 struct object *obj = &commit->object;
324 if (unpacked && has_sha1_pack(obj->sha1))
325 obj->flags |= UNINTERESTING;
326 if (obj->flags & UNINTERESTING) {
327 mark_parents_uninteresting(commit);
328 if (everybody_uninteresting(list))
332 p = &commit_list_insert(commit, p)->next;
335 newlist = find_bisection(newlist);
339 static void add_pending_object(struct object *obj, const char *name)
341 add_object(obj, &pending_objects, name);
344 static struct commit *get_commit_reference(const char *name, unsigned int flags)
346 unsigned char sha1[20];
347 struct object *object;
349 if (get_sha1(name, sha1))
350 usage(rev_list_usage);
351 object = parse_object(sha1);
353 die("bad object %s", name);
356 * Tag object? Look what it points to..
358 if (object->type == tag_type) {
359 struct tag *tag = (struct tag *) object;
360 object->flags |= flags;
361 if (tag_objects && !(object->flags & UNINTERESTING))
362 add_pending_object(object, tag->tag);
363 object = tag->tagged;
367 * Commit object? Just return it, we'll do all the complex
370 if (object->type == commit_type) {
371 struct commit *commit = (struct commit *)object;
372 object->flags |= flags;
373 if (parse_commit(commit) < 0)
374 die("unable to parse commit %s", name);
379 * Tree object? Either mark it uniniteresting, or add it
380 * to the list of objects to look at later..
382 if (object->type == tree_type) {
383 struct tree *tree = (struct tree *)object;
386 if (flags & UNINTERESTING) {
387 mark_tree_uninteresting(tree);
390 add_pending_object(object, "");
395 * Blob object? You know the drill by now..
397 if (object->type == blob_type) {
398 struct blob *blob = (struct blob *)object;
401 if (flags & UNINTERESTING) {
402 mark_blob_uninteresting(blob);
405 add_pending_object(object, "");
408 die("%s is unknown object", name);
411 int main(int argc, char **argv)
413 struct commit_list *list = NULL;
414 struct commit_list *(*insert)(struct commit *, struct commit_list **);
417 insert = insert_by_date;
418 for (i = 1 ; i < argc; i++) {
421 struct commit *commit;
423 if (!strncmp(arg, "--max-count=", 12)) {
424 max_count = atoi(arg + 12);
427 if (!strncmp(arg, "--max-age=", 10)) {
428 max_age = atoi(arg + 10);
431 if (!strncmp(arg, "--min-age=", 10)) {
432 min_age = atoi(arg + 10);
435 if (!strcmp(arg, "--header")) {
439 if (!strncmp(arg, "--pretty", 8)) {
440 commit_format = get_commit_format(arg+8);
442 hdr_termination = '\n';
446 if (!strcmp(arg, "--parents")) {
450 if (!strcmp(arg, "--bisect")) {
454 if (!strcmp(arg, "--objects")) {
460 if (!strcmp(arg, "--unpacked")) {
465 if (!strcmp(arg, "--merge-order")) {
467 insert = commit_list_insert;
470 if (!strcmp(arg, "--show-breaks")) {
474 if (!strcmp(arg, "--topo-order")) {
482 flags = UNINTERESTING;
486 if (show_breaks && !merge_order)
487 usage(rev_list_usage);
488 commit = get_commit_reference(arg, flags);
491 if (commit->object.flags & DUPCHECK)
493 commit->object.flags |= DUPCHECK;
494 insert(commit, &list);
499 list = limit_list(list);
501 sort_in_topological_order(&list);
502 show_commit_list(list);
504 if (sort_list_in_merge_order(list, &process_commit)) {
505 die("merge order sort failed\n");