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;
43 static void show_commit(struct commit *commit)
45 commit->object.flags |= SHOWN;
48 if (commit->object.flags & DISCONTINUITY) {
50 } else if (commit->object.flags & BOUNDARY) {
54 printf("%s%s", prefix, sha1_to_hex(commit->object.sha1));
56 struct commit_list *parents = commit->parents;
58 printf(" %s", sha1_to_hex(parents->item->object.sha1));
59 parents = parents->next;
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);
71 static int filter_commit(struct commit * commit)
73 if (merge_order && stop_traversal && commit->object.flags & BOUNDARY)
75 if (commit->object.flags & (UNINTERESTING|SHOWN))
77 if (min_age != -1 && (commit->date > min_age))
79 if (max_age != -1 && (commit->date < max_age)) {
87 if (max_count != -1 && !max_count--)
92 static int process_commit(struct commit * commit)
94 int action=filter_commit(commit);
100 if (action == CONTINUE) {
109 static struct object_list **add_object(struct object *obj, struct object_list **p, const char *name)
111 struct object_list *entry = xmalloc(sizeof(*entry));
119 static struct object_list **process_blob(struct blob *blob, struct object_list **p, const char *name)
121 struct object *obj = &blob->object;
125 if (obj->flags & (UNINTERESTING | SEEN))
128 return add_object(obj, p, name);
131 static struct object_list **process_tree(struct tree *tree, struct object_list **p, const char *name)
133 struct object *obj = &tree->object;
134 struct tree_entry_list *entry;
138 if (obj->flags & (UNINTERESTING | SEEN))
140 if (parse_tree(tree) < 0)
141 die("bad tree object %s", sha1_to_hex(obj->sha1));
143 p = add_object(obj, p, name);
144 for (entry = tree->entries ; entry ; entry = entry->next) {
145 if (entry->directory)
146 p = process_tree(entry->item.tree, p, entry->name);
148 p = process_blob(entry->item.blob, p, entry->name);
153 static struct object_list *pending_objects = NULL;
155 static void show_commit_list(struct commit_list *list)
157 struct object_list *objects = NULL, **p = &objects, *pending;
159 struct commit *commit = pop_most_recent_commit(&list, SEEN);
161 p = process_tree(commit->tree, p, "");
162 if (process_commit(commit) == STOP)
165 for (pending = pending_objects; pending; pending = pending->next) {
166 struct object *obj = pending->item;
167 const char *name = pending->name;
168 if (obj->flags & (UNINTERESTING | SEEN))
170 if (obj->type == tag_type) {
172 p = add_object(obj, p, name);
175 if (obj->type == tree_type) {
176 p = process_tree((struct tree *)obj, p, name);
179 if (obj->type == blob_type) {
180 p = process_blob((struct blob *)obj, p, name);
183 die("unknown pending object %s (%s)", sha1_to_hex(obj->sha1), name);
186 printf("%s %s\n", sha1_to_hex(objects->item->sha1), objects->name);
187 objects = objects->next;
191 static void mark_blob_uninteresting(struct blob *blob)
195 if (blob->object.flags & UNINTERESTING)
197 blob->object.flags |= UNINTERESTING;
200 static void mark_tree_uninteresting(struct tree *tree)
202 struct object *obj = &tree->object;
203 struct tree_entry_list *entry;
207 if (obj->flags & UNINTERESTING)
209 obj->flags |= UNINTERESTING;
210 if (parse_tree(tree) < 0)
211 die("bad tree %s", sha1_to_hex(obj->sha1));
212 entry = tree->entries;
214 if (entry->directory)
215 mark_tree_uninteresting(entry->item.tree);
217 mark_blob_uninteresting(entry->item.blob);
222 static void mark_parents_uninteresting(struct commit *commit)
224 struct commit_list *parents = commit->parents;
227 mark_tree_uninteresting(commit->tree);
229 struct commit *commit = parents->item;
230 commit->object.flags |= UNINTERESTING;
231 parents = parents->next;
235 static int everybody_uninteresting(struct commit_list *list)
238 struct commit *commit = list->item;
240 if (commit->object.flags & UNINTERESTING)
248 * This is a truly stupid algorithm, but it's only
249 * used for bisection, and we just don't care enough.
251 * We care just barely enough to avoid recursing for
254 static int count_distance(struct commit_list *entry)
259 struct commit *commit = entry->item;
260 struct commit_list *p;
262 if (commit->object.flags & (UNINTERESTING | COUNTED))
265 commit->object.flags |= COUNTED;
271 nr += count_distance(p);
279 static void clear_distance(struct commit_list *list)
282 struct commit *commit = list->item;
283 commit->object.flags &= ~COUNTED;
288 static struct commit_list *find_bisection(struct commit_list *list)
291 struct commit_list *p, *best;
304 int distance = count_distance(p);
305 clear_distance(list);
306 if (nr - distance < distance)
307 distance = nr - distance;
308 if (distance > closest) {
319 static struct commit_list *limit_list(struct commit_list *list)
321 struct commit_list *newlist = NULL;
322 struct commit_list **p = &newlist;
324 struct commit *commit = pop_most_recent_commit(&list, SEEN);
325 struct object *obj = &commit->object;
327 if (unpacked && has_sha1_pack(obj->sha1))
328 obj->flags |= UNINTERESTING;
329 if (obj->flags & UNINTERESTING) {
330 mark_parents_uninteresting(commit);
331 if (everybody_uninteresting(list))
335 p = &commit_list_insert(commit, p)->next;
338 newlist = find_bisection(newlist);
342 static void add_pending_object(struct object *obj, const char *name)
344 add_object(obj, &pending_objects, name);
347 static struct commit *get_commit_reference(const char *name, unsigned int flags)
349 unsigned char sha1[20];
350 struct object *object;
352 if (get_sha1(name, sha1))
353 usage(rev_list_usage);
354 object = parse_object(sha1);
356 die("bad object %s", name);
359 * Tag object? Look what it points to..
361 if (object->type == tag_type) {
362 struct tag *tag = (struct tag *) object;
363 object->flags |= flags;
364 if (tag_objects && !(object->flags & UNINTERESTING))
365 add_pending_object(object, tag->tag);
366 object = tag->tagged;
370 * Commit object? Just return it, we'll do all the complex
373 if (object->type == commit_type) {
374 struct commit *commit = (struct commit *)object;
375 object->flags |= flags;
376 if (parse_commit(commit) < 0)
377 die("unable to parse commit %s", name);
382 * Tree object? Either mark it uniniteresting, or add it
383 * to the list of objects to look at later..
385 if (object->type == tree_type) {
386 struct tree *tree = (struct tree *)object;
389 if (flags & UNINTERESTING) {
390 mark_tree_uninteresting(tree);
393 add_pending_object(object, "");
398 * Blob object? You know the drill by now..
400 if (object->type == blob_type) {
401 struct blob *blob = (struct blob *)object;
404 if (flags & UNINTERESTING) {
405 mark_blob_uninteresting(blob);
408 add_pending_object(object, "");
411 die("%s is unknown object", name);
414 int main(int argc, char **argv)
416 struct commit_list *list = NULL;
417 struct commit_list *(*insert)(struct commit *, struct commit_list **);
420 insert = insert_by_date;
421 for (i = 1 ; i < argc; i++) {
424 struct commit *commit;
426 if (!strncmp(arg, "--max-count=", 12)) {
427 max_count = atoi(arg + 12);
430 if (!strncmp(arg, "--max-age=", 10)) {
431 max_age = atoi(arg + 10);
434 if (!strncmp(arg, "--min-age=", 10)) {
435 min_age = atoi(arg + 10);
438 if (!strcmp(arg, "--header")) {
442 if (!strncmp(arg, "--pretty", 8)) {
443 commit_format = get_commit_format(arg+8);
445 hdr_termination = '\n';
449 if (!strcmp(arg, "--parents")) {
453 if (!strcmp(arg, "--bisect")) {
457 if (!strcmp(arg, "--objects")) {
463 if (!strcmp(arg, "--unpacked")) {
468 if (!strcmp(arg, "--merge-order")) {
470 insert = commit_list_insert;
473 if (!strcmp(arg, "--show-breaks")) {
480 flags = UNINTERESTING;
484 if (show_breaks && !merge_order)
485 usage(rev_list_usage);
486 commit = get_commit_reference(arg, flags);
489 if (commit->object.flags & DUPCHECK)
491 commit->object.flags |= DUPCHECK;
492 insert(commit, &list);
497 list = limit_list(list);
498 show_commit_list(list);
500 if (sort_list_in_merge_order(list, &process_commit)) {
501 die("merge order sort failed\n");