10 #include "list-objects.h"
14 /* bits #0-15 in revision.h */
16 #define COUNTED (1u<<16)
18 static const char rev_list_usage
[] =
19 "git-rev-list [OPTION] <commit-id>... [ -- paths... ]\n"
37 " formatting output:\n"
40 " --objects | --objects-edge\n"
42 " --header | --pretty\n"
43 " --abbrev=nr | --no-abbrev\n"
52 static struct rev_info revs
;
54 static int bisect_list
;
55 static int show_timestamp
;
56 static int hdr_termination
;
57 static const char *header_prefix
;
59 static void finish_commit(struct commit
*commit
);
60 static void show_commit(struct commit
*commit
)
63 printf("%lu ", commit
->date
);
65 fputs(header_prefix
, stdout
);
66 if (commit
->object
.flags
& BOUNDARY
)
68 else if (commit
->object
.flags
& UNINTERESTING
)
70 else if (revs
.left_right
) {
71 if (commit
->object
.flags
& SYMMETRIC_LEFT
)
76 if (revs
.abbrev_commit
&& revs
.abbrev
)
77 fputs(find_unique_abbrev(commit
->object
.sha1
, revs
.abbrev
),
80 fputs(sha1_to_hex(commit
->object
.sha1
), stdout
);
82 struct commit_list
*parents
= commit
->parents
;
84 printf(" %s", sha1_to_hex(parents
->item
->object
.sha1
));
85 parents
= parents
->next
;
88 if (revs
.children
.name
) {
89 struct commit_list
*children
;
91 children
= lookup_decoration(&revs
.children
, &commit
->object
);
93 printf(" %s", sha1_to_hex(children
->item
->object
.sha1
));
94 children
= children
->next
;
97 show_decorations(commit
);
98 if (revs
.commit_format
== CMIT_FMT_ONELINE
)
103 if (revs
.verbose_header
&& commit
->buffer
) {
105 strbuf_init(&buf
, 0);
106 pretty_print_commit(revs
.commit_format
, commit
,
107 &buf
, revs
.abbrev
, NULL
, NULL
,
110 printf("%s%c", buf
.buf
, hdr_termination
);
111 strbuf_release(&buf
);
113 maybe_flush_or_die(stdout
, "stdout");
114 finish_commit(commit
);
117 static void finish_commit(struct commit
*commit
)
119 if (commit
->parents
) {
120 free_commit_list(commit
->parents
);
121 commit
->parents
= NULL
;
123 free(commit
->buffer
);
124 commit
->buffer
= NULL
;
127 static void finish_object(struct object_array_entry
*p
)
129 if (p
->item
->type
== OBJ_BLOB
&& !has_sha1_file(p
->item
->sha1
))
130 die("missing blob object '%s'", sha1_to_hex(p
->item
->sha1
));
133 static void show_object(struct object_array_entry
*p
)
135 /* An object with name "foo\n0000000..." can be used to
136 * confuse downstream git-pack-objects very badly.
138 const char *ep
= strchr(p
->name
, '\n');
142 printf("%s %.*s\n", sha1_to_hex(p
->item
->sha1
),
143 (int) (ep
- p
->name
),
147 printf("%s %s\n", sha1_to_hex(p
->item
->sha1
), p
->name
);
150 static void show_edge(struct commit
*commit
)
152 printf("-%s\n", sha1_to_hex(commit
->object
.sha1
));
156 * This is a truly stupid algorithm, but it's only
157 * used for bisection, and we just don't care enough.
159 * We care just barely enough to avoid recursing for
162 static int count_distance(struct commit_list
*entry
)
167 struct commit
*commit
= entry
->item
;
168 struct commit_list
*p
;
170 if (commit
->object
.flags
& (UNINTERESTING
| COUNTED
))
172 if (!(commit
->object
.flags
& TREESAME
))
174 commit
->object
.flags
|= COUNTED
;
180 nr
+= count_distance(p
);
189 static void clear_distance(struct commit_list
*list
)
192 struct commit
*commit
= list
->item
;
193 commit
->object
.flags
&= ~COUNTED
;
198 #define DEBUG_BISECT 0
200 static inline int weight(struct commit_list
*elem
)
202 return *((int*)(elem
->item
->util
));
205 static inline void weight_set(struct commit_list
*elem
, int weight
)
207 *((int*)(elem
->item
->util
)) = weight
;
210 static int count_interesting_parents(struct commit
*commit
)
212 struct commit_list
*p
;
215 for (count
= 0, p
= commit
->parents
; p
; p
= p
->next
) {
216 if (p
->item
->object
.flags
& UNINTERESTING
)
223 static inline int halfway(struct commit_list
*p
, int nr
)
226 * Don't short-cut something we are not going to return!
228 if (p
->item
->object
.flags
& TREESAME
)
233 * 2 and 3 are halfway of 5.
234 * 3 is halfway of 6 but 2 and 4 are not.
236 switch (2 * weight(p
) - nr
) {
237 case -1: case 0: case 1:
245 #define show_list(a,b,c,d) do { ; } while (0)
247 static void show_list(const char *debug
, int counted
, int nr
,
248 struct commit_list
*list
)
250 struct commit_list
*p
;
252 fprintf(stderr
, "%s (%d/%d)\n", debug
, counted
, nr
);
254 for (p
= list
; p
; p
= p
->next
) {
255 struct commit_list
*pp
;
256 struct commit
*commit
= p
->item
;
257 unsigned flags
= commit
->object
.flags
;
258 enum object_type type
;
260 char *buf
= read_sha1_file(commit
->object
.sha1
, &type
, &size
);
263 fprintf(stderr
, "%c%c%c ",
264 (flags
& TREESAME
) ? ' ' : 'T',
265 (flags
& UNINTERESTING
) ? 'U' : ' ',
266 (flags
& COUNTED
) ? 'C' : ' ');
268 fprintf(stderr
, "%3d", weight(p
));
270 fprintf(stderr
, "---");
271 fprintf(stderr
, " %.*s", 8, sha1_to_hex(commit
->object
.sha1
));
272 for (pp
= commit
->parents
; pp
; pp
= pp
->next
)
273 fprintf(stderr
, " %.*s", 8,
274 sha1_to_hex(pp
->item
->object
.sha1
));
276 sp
= strstr(buf
, "\n\n");
279 for (ep
= sp
; *ep
&& *ep
!= '\n'; ep
++)
281 fprintf(stderr
, " %.*s", (int)(ep
- sp
), sp
);
283 fprintf(stderr
, "\n");
286 #endif /* DEBUG_BISECT */
288 static struct commit_list
*best_bisection(struct commit_list
*list
, int nr
)
290 struct commit_list
*p
, *best
;
291 int best_distance
= -1;
294 for (p
= list
; p
; p
= p
->next
) {
296 unsigned flags
= p
->item
->object
.flags
;
298 if (flags
& TREESAME
)
300 distance
= weight(p
);
301 if (nr
- distance
< distance
)
302 distance
= nr
- distance
;
303 if (distance
> best_distance
) {
305 best_distance
= distance
;
313 struct commit
*commit
;
317 static int compare_commit_dist(const void *a_
, const void *b_
)
319 struct commit_dist
*a
, *b
;
321 a
= (struct commit_dist
*)a_
;
322 b
= (struct commit_dist
*)b_
;
323 if (a
->distance
!= b
->distance
)
324 return b
->distance
- a
->distance
; /* desc sort */
325 return hashcmp(a
->commit
->object
.sha1
, b
->commit
->object
.sha1
);
328 static struct commit_list
*best_bisection_sorted(struct commit_list
*list
, int nr
)
330 struct commit_list
*p
;
331 struct commit_dist
*array
= xcalloc(nr
, sizeof(*array
));
334 for (p
= list
, cnt
= 0; p
; p
= p
->next
) {
336 unsigned flags
= p
->item
->object
.flags
;
338 if (flags
& TREESAME
)
340 distance
= weight(p
);
341 if (nr
- distance
< distance
)
342 distance
= nr
- distance
;
343 array
[cnt
].commit
= p
->item
;
344 array
[cnt
].distance
= distance
;
347 qsort(array
, cnt
, sizeof(*array
), compare_commit_dist
);
348 for (p
= list
, i
= 0; i
< cnt
; i
++) {
349 struct name_decoration
*r
= xmalloc(sizeof(*r
) + 100);
350 struct object
*obj
= &(array
[i
].commit
->object
);
352 sprintf(r
->name
, "dist=%d", array
[i
].distance
);
353 r
->next
= add_decoration(&name_decoration
, obj
, r
);
354 p
->item
= array
[i
].commit
;
364 * zero or positive weight is the number of interesting commits it can
365 * reach, including itself. Especially, weight = 0 means it does not
366 * reach any tree-changing commits (e.g. just above uninteresting one
367 * but traversal is with pathspec).
369 * weight = -1 means it has one parent and its distance is yet to
372 * weight = -2 means it has more than one parent and its distance is
373 * unknown. After running count_distance() first, they will get zero
374 * or positive distance.
376 static struct commit_list
*do_find_bisection(struct commit_list
*list
,
377 int nr
, int *weights
,
381 struct commit_list
*p
;
385 for (n
= 0, p
= list
; p
; p
= p
->next
) {
386 struct commit
*commit
= p
->item
;
387 unsigned flags
= commit
->object
.flags
;
389 p
->item
->util
= &weights
[n
++];
390 switch (count_interesting_parents(commit
)) {
392 if (!(flags
& TREESAME
)) {
395 show_list("bisection 2 count one",
399 * otherwise, it is known not to reach any
400 * tree-changing commit and gets weight 0.
412 show_list("bisection 2 initialize", counted
, nr
, list
);
415 * If you have only one parent in the resulting set
416 * then you can reach one commit more than that parent
417 * can reach. So we do not have to run the expensive
418 * count_distance() for single strand of pearls.
420 * However, if you have more than one parents, you cannot
421 * just add their distance and one for yourself, since
422 * they usually reach the same ancestor and you would
423 * end up counting them twice that way.
425 * So we will first count distance of merges the usual
426 * way, and then fill the blanks using cheaper algorithm.
428 for (p
= list
; p
; p
= p
->next
) {
429 if (p
->item
->object
.flags
& UNINTERESTING
)
433 weight_set(p
, count_distance(p
));
434 clear_distance(list
);
436 /* Does it happen to be at exactly half-way? */
437 if (!find_all
&& halfway(p
, nr
))
442 show_list("bisection 2 count_distance", counted
, nr
, list
);
444 while (counted
< nr
) {
445 for (p
= list
; p
; p
= p
->next
) {
446 struct commit_list
*q
;
447 unsigned flags
= p
->item
->object
.flags
;
451 for (q
= p
->item
->parents
; q
; q
= q
->next
) {
452 if (q
->item
->object
.flags
& UNINTERESTING
)
461 * weight for p is unknown but q is known.
462 * add one for p itself if p is to be counted,
463 * otherwise inherit it from q directly.
465 if (!(flags
& TREESAME
)) {
466 weight_set(p
, weight(q
)+1);
468 show_list("bisection 2 count one",
472 weight_set(p
, weight(q
));
474 /* Does it happen to be at exactly half-way? */
475 if (!find_all
&& halfway(p
, nr
))
480 show_list("bisection 2 counted all", counted
, nr
, list
);
483 return best_bisection(list
, nr
);
485 return best_bisection_sorted(list
, nr
);
488 static struct commit_list
*find_bisection(struct commit_list
*list
,
489 int *reaches
, int *all
,
493 struct commit_list
*p
, *best
, *next
, *last
;
496 show_list("bisection 2 entry", 0, 0, list
);
499 * Count the number of total and tree-changing items on the
500 * list, while reversing the list.
502 for (nr
= on_list
= 0, last
= NULL
, p
= list
;
505 unsigned flags
= p
->item
->object
.flags
;
508 if (flags
& UNINTERESTING
)
512 if (!(flags
& TREESAME
))
517 show_list("bisection 2 sorted", 0, nr
, list
);
520 weights
= xcalloc(on_list
, sizeof(*weights
));
522 /* Do the real work of finding bisection commit. */
523 best
= do_find_bisection(list
, nr
, weights
, find_all
);
527 *reaches
= weight(best
);
533 static void read_revisions_from_stdin(struct rev_info
*revs
)
537 while (fgets(line
, sizeof(line
), stdin
) != NULL
) {
538 int len
= strlen(line
);
539 if (len
&& line
[len
- 1] == '\n')
544 die("options not supported in --stdin mode");
545 if (handle_revision_arg(line
, revs
, 0, 1))
546 die("bad revision '%s'", line
);
550 int cmd_rev_list(int argc
, const char **argv
, const char *prefix
)
552 struct commit_list
*list
;
554 int read_from_stdin
= 0;
555 int bisect_show_vars
= 0;
556 int bisect_find_all
= 0;
559 git_config(git_default_config
);
560 init_revisions(&revs
, prefix
);
562 revs
.commit_format
= CMIT_FMT_UNSPECIFIED
;
563 argc
= setup_revisions(argc
, argv
, &revs
, NULL
);
565 for (i
= 1 ; i
< argc
; i
++) {
566 const char *arg
= argv
[i
];
568 if (!strcmp(arg
, "--header")) {
569 revs
.verbose_header
= 1;
572 if (!strcmp(arg
, "--timestamp")) {
576 if (!strcmp(arg
, "--bisect")) {
580 if (!strcmp(arg
, "--bisect-all")) {
585 if (!strcmp(arg
, "--bisect-vars")) {
587 bisect_show_vars
= 1;
590 if (!strcmp(arg
, "--stdin")) {
591 if (read_from_stdin
++)
592 die("--stdin given twice?");
593 read_revisions_from_stdin(&revs
);
596 if (!strcmp(arg
, "--quiet")) {
600 usage(rev_list_usage
);
603 if (revs
.commit_format
!= CMIT_FMT_UNSPECIFIED
) {
604 /* The command line has a --pretty */
605 hdr_termination
= '\n';
606 if (revs
.commit_format
== CMIT_FMT_ONELINE
)
609 header_prefix
= "commit ";
611 else if (revs
.verbose_header
)
612 /* Only --header was specified */
613 revs
.commit_format
= CMIT_FMT_RAW
;
618 (!(revs
.tag_objects
||revs
.tree_objects
||revs
.blob_objects
) &&
619 !revs
.pending
.nr
)) ||
621 usage(rev_list_usage
);
623 save_commit_buffer
= revs
.verbose_header
|| revs
.grep_filter
;
627 if (prepare_revision_walk(&revs
))
628 die("revision walk setup failed");
629 if (revs
.tree_objects
)
630 mark_edges_uninteresting(revs
.commits
, &revs
, show_edge
);
633 int reaches
= reaches
, all
= all
;
635 revs
.commits
= find_bisection(revs
.commits
, &reaches
, &all
,
637 if (bisect_show_vars
) {
643 * revs.commits can reach "reaches" commits among
644 * "all" commits. If it is good, then there are
645 * (all-reaches) commits left to be bisected.
646 * On the other hand, if it is bad, then the set
647 * to bisect is "reaches".
648 * A bisect set of size N has (N-1) commits further
649 * to test, as we already know one bad one.
654 strcpy(hex
, sha1_to_hex(revs
.commits
->item
->object
.sha1
));
656 if (bisect_find_all
) {
657 traverse_commit_list(&revs
, show_commit
, show_object
);
661 printf("bisect_rev=%s\n"
675 traverse_commit_list(&revs
,
676 quiet
? finish_commit
: show_commit
,
677 quiet
? finish_object
: show_object
);