10 #define INTERESTING (1u << 1)
11 #define COUNTED (1u << 2)
12 #define SHOWN (1u << 3)
14 static const char rev_list_usage
[] =
15 "git-rev-list [OPTION] commit-id <commit-id>\n"
26 " --merge-order [ --show-breaks ]\n"
29 static int unpacked
= 0;
30 static int bisect_list
= 0;
31 static int tag_objects
= 0;
32 static int tree_objects
= 0;
33 static int blob_objects
= 0;
34 static int verbose_header
= 0;
35 static int show_parents
= 0;
36 static int hdr_termination
= 0;
37 static const char *commit_prefix
= "";
38 static unsigned long max_age
= -1;
39 static unsigned long min_age
= -1;
40 static int max_count
= -1;
41 static enum cmit_fmt commit_format
= CMIT_FMT_RAW
;
42 static int merge_order
= 0;
43 static int show_breaks
= 0;
44 static int stop_traversal
= 0;
45 static int topo_order
= 0;
46 static int no_merges
= 0;
48 static void show_commit(struct commit
*commit
)
50 commit
->object
.flags
|= SHOWN
;
53 if (commit
->object
.flags
& DISCONTINUITY
) {
55 } else if (commit
->object
.flags
& BOUNDARY
) {
59 printf("%s%s", commit_prefix
, sha1_to_hex(commit
->object
.sha1
));
61 struct commit_list
*parents
= commit
->parents
;
63 printf(" %s", sha1_to_hex(parents
->item
->object
.sha1
));
64 parents
= parents
->next
;
67 if (commit_format
== CMIT_FMT_ONELINE
)
73 static char pretty_header
[16384];
74 pretty_print_commit(commit_format
, commit
->buffer
, ~0, pretty_header
, sizeof(pretty_header
));
75 printf("%s%c", pretty_header
, hdr_termination
);
80 static int filter_commit(struct commit
* commit
)
82 if (stop_traversal
&& (commit
->object
.flags
& BOUNDARY
))
84 if (commit
->object
.flags
& (UNINTERESTING
|SHOWN
))
86 if (min_age
!= -1 && (commit
->date
> min_age
))
88 if (max_age
!= -1 && (commit
->date
< max_age
)) {
92 if (max_count
!= -1 && !max_count
--)
94 if (no_merges
&& (commit
->parents
&& commit
->parents
->next
))
99 static int process_commit(struct commit
* commit
)
101 int action
=filter_commit(commit
);
103 if (action
== STOP
) {
107 if (action
== CONTINUE
) {
116 static struct object_list
**add_object(struct object
*obj
, struct object_list
**p
, const char *name
)
118 struct object_list
*entry
= xmalloc(sizeof(*entry
));
126 static struct object_list
**process_blob(struct blob
*blob
, struct object_list
**p
, const char *name
)
128 struct object
*obj
= &blob
->object
;
132 if (obj
->flags
& (UNINTERESTING
| SEEN
))
135 return add_object(obj
, p
, name
);
138 static struct object_list
**process_tree(struct tree
*tree
, struct object_list
**p
, const char *name
)
140 struct object
*obj
= &tree
->object
;
141 struct tree_entry_list
*entry
;
145 if (obj
->flags
& (UNINTERESTING
| SEEN
))
147 if (parse_tree(tree
) < 0)
148 die("bad tree object %s", sha1_to_hex(obj
->sha1
));
150 p
= add_object(obj
, p
, name
);
151 entry
= tree
->entries
;
152 tree
->entries
= NULL
;
154 struct tree_entry_list
*next
= entry
->next
;
155 if (entry
->directory
)
156 p
= process_tree(entry
->item
.tree
, p
, entry
->name
);
158 p
= process_blob(entry
->item
.blob
, p
, entry
->name
);
165 static struct object_list
*pending_objects
= NULL
;
167 static void show_commit_list(struct commit_list
*list
)
169 struct object_list
*objects
= NULL
, **p
= &objects
, *pending
;
171 struct commit
*commit
= pop_most_recent_commit(&list
, SEEN
);
173 p
= process_tree(commit
->tree
, p
, "");
174 if (process_commit(commit
) == STOP
)
177 for (pending
= pending_objects
; pending
; pending
= pending
->next
) {
178 struct object
*obj
= pending
->item
;
179 const char *name
= pending
->name
;
180 if (obj
->flags
& (UNINTERESTING
| SEEN
))
182 if (obj
->type
== tag_type
) {
184 p
= add_object(obj
, p
, name
);
187 if (obj
->type
== tree_type
) {
188 p
= process_tree((struct tree
*)obj
, p
, name
);
191 if (obj
->type
== blob_type
) {
192 p
= process_blob((struct blob
*)obj
, p
, name
);
195 die("unknown pending object %s (%s)", sha1_to_hex(obj
->sha1
), name
);
198 /* An object with name "foo\n0000000000000000000000000000000000000000"
199 * can be used confuse downstream git-pack-objects very badly.
201 const char *ep
= strchr(objects
->name
, '\n');
203 printf("%s %.*s\n", sha1_to_hex(objects
->item
->sha1
),
204 (int) (ep
- objects
->name
),
208 printf("%s %s\n", sha1_to_hex(objects
->item
->sha1
), objects
->name
);
209 objects
= objects
->next
;
213 static void mark_blob_uninteresting(struct blob
*blob
)
217 if (blob
->object
.flags
& UNINTERESTING
)
219 blob
->object
.flags
|= UNINTERESTING
;
222 static void mark_tree_uninteresting(struct tree
*tree
)
224 struct object
*obj
= &tree
->object
;
225 struct tree_entry_list
*entry
;
229 if (obj
->flags
& UNINTERESTING
)
231 obj
->flags
|= UNINTERESTING
;
232 if (!has_sha1_file(obj
->sha1
))
234 if (parse_tree(tree
) < 0)
235 die("bad tree %s", sha1_to_hex(obj
->sha1
));
236 entry
= tree
->entries
;
237 tree
->entries
= NULL
;
239 struct tree_entry_list
*next
= entry
->next
;
240 if (entry
->directory
)
241 mark_tree_uninteresting(entry
->item
.tree
);
243 mark_blob_uninteresting(entry
->item
.blob
);
249 static void mark_parents_uninteresting(struct commit
*commit
)
251 struct commit_list
*parents
= commit
->parents
;
254 struct commit
*commit
= parents
->item
;
255 commit
->object
.flags
|= UNINTERESTING
;
258 * Normally we haven't parsed the parent
259 * yet, so we won't have a parent of a parent
260 * here. However, it may turn out that we've
261 * reached this commit some other way (where it
262 * wasn't uninteresting), in which case we need
263 * to mark its parents recursively too..
266 mark_parents_uninteresting(commit
);
269 * A missing commit is ok iff its parent is marked
272 * We just mark such a thing parsed, so that when
273 * it is popped next time around, we won't be trying
274 * to parse it and get an error.
276 if (!has_sha1_file(commit
->object
.sha1
))
277 commit
->object
.parsed
= 1;
278 parents
= parents
->next
;
282 static int everybody_uninteresting(struct commit_list
*orig
)
284 struct commit_list
*list
= orig
;
286 struct commit
*commit
= list
->item
;
288 if (commit
->object
.flags
& UNINTERESTING
)
296 * This is a truly stupid algorithm, but it's only
297 * used for bisection, and we just don't care enough.
299 * We care just barely enough to avoid recursing for
302 static int count_distance(struct commit_list
*entry
)
307 struct commit
*commit
= entry
->item
;
308 struct commit_list
*p
;
310 if (commit
->object
.flags
& (UNINTERESTING
| COUNTED
))
313 commit
->object
.flags
|= COUNTED
;
319 nr
+= count_distance(p
);
327 static void clear_distance(struct commit_list
*list
)
330 struct commit
*commit
= list
->item
;
331 commit
->object
.flags
&= ~COUNTED
;
336 static struct commit_list
*find_bisection(struct commit_list
*list
)
339 struct commit_list
*p
, *best
;
352 int distance
= count_distance(p
);
353 clear_distance(list
);
354 if (nr
- distance
< distance
)
355 distance
= nr
- distance
;
356 if (distance
> closest
) {
367 static void mark_edges_uninteresting(struct commit_list
*list
)
369 for ( ; list
; list
= list
->next
) {
370 struct commit_list
*parents
= list
->item
->parents
;
372 for ( ; parents
; parents
= parents
->next
) {
373 struct commit
*commit
= parents
->item
;
374 if (commit
->object
.flags
& UNINTERESTING
)
375 mark_tree_uninteresting(commit
->tree
);
380 static struct commit_list
*limit_list(struct commit_list
*list
)
382 struct commit_list
*newlist
= NULL
;
383 struct commit_list
**p
= &newlist
;
385 struct commit
*commit
= pop_most_recent_commit(&list
, SEEN
);
386 struct object
*obj
= &commit
->object
;
388 if (max_age
!= -1 && (commit
->date
< max_age
))
389 obj
->flags
|= UNINTERESTING
;
390 if (unpacked
&& has_sha1_pack(obj
->sha1
))
391 obj
->flags
|= UNINTERESTING
;
392 if (obj
->flags
& UNINTERESTING
) {
393 mark_parents_uninteresting(commit
);
394 if (everybody_uninteresting(list
))
398 if (min_age
!= -1 && (commit
->date
> min_age
))
400 p
= &commit_list_insert(commit
, p
)->next
;
403 mark_edges_uninteresting(newlist
);
405 newlist
= find_bisection(newlist
);
409 static void add_pending_object(struct object
*obj
, const char *name
)
411 add_object(obj
, &pending_objects
, name
);
414 static struct commit
*get_commit_reference(const char *name
, unsigned int flags
)
416 unsigned char sha1
[20];
417 struct object
*object
;
419 if (get_sha1(name
, sha1
))
420 usage(rev_list_usage
);
421 object
= parse_object(sha1
);
423 die("bad object %s", name
);
426 * Tag object? Look what it points to..
428 while (object
->type
== tag_type
) {
429 struct tag
*tag
= (struct tag
*) object
;
430 object
->flags
|= flags
;
431 if (tag_objects
&& !(object
->flags
& UNINTERESTING
))
432 add_pending_object(object
, tag
->tag
);
433 object
= parse_object(tag
->tagged
->sha1
);
435 die("bad object %s", sha1_to_hex(tag
->tagged
->sha1
));
439 * Commit object? Just return it, we'll do all the complex
442 if (object
->type
== commit_type
) {
443 struct commit
*commit
= (struct commit
*)object
;
444 object
->flags
|= flags
;
445 if (parse_commit(commit
) < 0)
446 die("unable to parse commit %s", name
);
447 if (flags
& UNINTERESTING
)
448 mark_parents_uninteresting(commit
);
453 * Tree object? Either mark it uniniteresting, or add it
454 * to the list of objects to look at later..
456 if (object
->type
== tree_type
) {
457 struct tree
*tree
= (struct tree
*)object
;
460 if (flags
& UNINTERESTING
) {
461 mark_tree_uninteresting(tree
);
464 add_pending_object(object
, "");
469 * Blob object? You know the drill by now..
471 if (object
->type
== blob_type
) {
472 struct blob
*blob
= (struct blob
*)object
;
475 if (flags
& UNINTERESTING
) {
476 mark_blob_uninteresting(blob
);
479 add_pending_object(object
, "");
482 die("%s is unknown object", name
);
485 static void handle_one_commit(struct commit
*com
, struct commit_list
**lst
)
487 if (!com
|| com
->object
.flags
& SEEN
)
489 com
->object
.flags
|= SEEN
;
490 commit_list_insert(com
, lst
);
493 /* for_each_ref() callback does not allow user data -- Yuck. */
494 static struct commit_list
**global_lst
;
496 static int include_one_commit(const char *path
, const unsigned char *sha1
)
498 struct commit
*com
= get_commit_reference(path
, 0);
499 handle_one_commit(com
, global_lst
);
503 static void handle_all(struct commit_list
**lst
)
506 for_each_ref(include_one_commit
);
510 int main(int argc
, char **argv
)
512 struct commit_list
*list
= NULL
;
515 setup_git_directory();
516 for (i
= 1 ; i
< argc
; i
++) {
520 struct commit
*commit
;
522 if (!strncmp(arg
, "--max-count=", 12)) {
523 max_count
= atoi(arg
+ 12);
526 if (!strncmp(arg
, "--max-age=", 10)) {
527 max_age
= atoi(arg
+ 10);
531 if (!strncmp(arg
, "--min-age=", 10)) {
532 min_age
= atoi(arg
+ 10);
536 if (!strcmp(arg
, "--header")) {
540 if (!strncmp(arg
, "--pretty", 8)) {
541 commit_format
= get_commit_format(arg
+8);
543 hdr_termination
= '\n';
544 if (commit_format
== CMIT_FMT_ONELINE
)
547 commit_prefix
= "commit ";
550 if (!strncmp(arg
, "--no-merges", 11)) {
554 if (!strcmp(arg
, "--parents")) {
558 if (!strcmp(arg
, "--bisect")) {
562 if (!strcmp(arg
, "--all")) {
566 if (!strcmp(arg
, "--objects")) {
572 if (!strcmp(arg
, "--unpacked")) {
577 if (!strcmp(arg
, "--merge-order")) {
581 if (!strcmp(arg
, "--show-breaks")) {
585 if (!strcmp(arg
, "--topo-order")) {
591 if (show_breaks
&& !merge_order
)
592 usage(rev_list_usage
);
595 dotdot
= strstr(arg
, "..");
597 char *next
= dotdot
+ 2;
598 struct commit
*exclude
= NULL
;
599 struct commit
*include
= NULL
;
603 exclude
= get_commit_reference(arg
, UNINTERESTING
);
604 include
= get_commit_reference(next
, 0);
605 if (exclude
&& include
) {
607 handle_one_commit(exclude
, &list
);
608 handle_one_commit(include
, &list
);
614 flags
= UNINTERESTING
;
618 commit
= get_commit_reference(arg
, flags
);
619 handle_one_commit(commit
, &list
);
622 save_commit_buffer
= verbose_header
;
623 track_object_refs
= 0;
627 if (list
&& !limited
&& max_count
== 1 &&
628 !tag_objects
&& !tree_objects
&& !blob_objects
) {
629 show_commit(list
->item
);
633 list
= limit_list(list
);
635 sort_in_topological_order(&list
);
636 show_commit_list(list
);
639 if (sort_list_in_merge_order(list
, &process_commit
)) {
640 die("merge order sort failed\n");
643 die("merge order sort unsupported, OpenSSL not linked");