6 #include "list-objects.h"
8 #include "sha1-lookup.h"
9 #include "run-command.h"
13 unsigned char (*sha1
)[20];
18 static struct sha1_array good_revs
;
19 static struct sha1_array skipped_revs
;
21 static const char **rev_argv
;
22 static int rev_argv_nr
;
23 static int rev_argv_alloc
;
25 static const unsigned char *current_bad_sha1
;
27 static const char *argv_diff_tree
[] = {"diff-tree", "--pretty", NULL
, NULL
};
28 static const char *argv_checkout
[] = {"checkout", "-q", NULL
, "--", NULL
};
29 static const char *argv_show_branch
[] = {"show-branch", NULL
, NULL
};
31 /* bits #0-15 in revision.h */
33 #define COUNTED (1u<<16)
36 * This is a truly stupid algorithm, but it's only
37 * used for bisection, and we just don't care enough.
39 * We care just barely enough to avoid recursing for
42 static int count_distance(struct commit_list
*entry
)
47 struct commit
*commit
= entry
->item
;
48 struct commit_list
*p
;
50 if (commit
->object
.flags
& (UNINTERESTING
| COUNTED
))
52 if (!(commit
->object
.flags
& TREESAME
))
54 commit
->object
.flags
|= COUNTED
;
60 nr
+= count_distance(p
);
69 static void clear_distance(struct commit_list
*list
)
72 struct commit
*commit
= list
->item
;
73 commit
->object
.flags
&= ~COUNTED
;
78 #define DEBUG_BISECT 0
80 static inline int weight(struct commit_list
*elem
)
82 return *((int*)(elem
->item
->util
));
85 static inline void weight_set(struct commit_list
*elem
, int weight
)
87 *((int*)(elem
->item
->util
)) = weight
;
90 static int count_interesting_parents(struct commit
*commit
)
92 struct commit_list
*p
;
95 for (count
= 0, p
= commit
->parents
; p
; p
= p
->next
) {
96 if (p
->item
->object
.flags
& UNINTERESTING
)
103 static inline int halfway(struct commit_list
*p
, int nr
)
106 * Don't short-cut something we are not going to return!
108 if (p
->item
->object
.flags
& TREESAME
)
113 * 2 and 3 are halfway of 5.
114 * 3 is halfway of 6 but 2 and 4 are not.
116 switch (2 * weight(p
) - nr
) {
117 case -1: case 0: case 1:
125 #define show_list(a,b,c,d) do { ; } while (0)
127 static void show_list(const char *debug
, int counted
, int nr
,
128 struct commit_list
*list
)
130 struct commit_list
*p
;
132 fprintf(stderr
, "%s (%d/%d)\n", debug
, counted
, nr
);
134 for (p
= list
; p
; p
= p
->next
) {
135 struct commit_list
*pp
;
136 struct commit
*commit
= p
->item
;
137 unsigned flags
= commit
->object
.flags
;
138 enum object_type type
;
140 char *buf
= read_sha1_file(commit
->object
.sha1
, &type
, &size
);
143 fprintf(stderr
, "%c%c%c ",
144 (flags
& TREESAME
) ? ' ' : 'T',
145 (flags
& UNINTERESTING
) ? 'U' : ' ',
146 (flags
& COUNTED
) ? 'C' : ' ');
148 fprintf(stderr
, "%3d", weight(p
));
150 fprintf(stderr
, "---");
151 fprintf(stderr
, " %.*s", 8, sha1_to_hex(commit
->object
.sha1
));
152 for (pp
= commit
->parents
; pp
; pp
= pp
->next
)
153 fprintf(stderr
, " %.*s", 8,
154 sha1_to_hex(pp
->item
->object
.sha1
));
156 sp
= strstr(buf
, "\n\n");
159 for (ep
= sp
; *ep
&& *ep
!= '\n'; ep
++)
161 fprintf(stderr
, " %.*s", (int)(ep
- sp
), sp
);
163 fprintf(stderr
, "\n");
166 #endif /* DEBUG_BISECT */
168 static struct commit_list
*best_bisection(struct commit_list
*list
, int nr
)
170 struct commit_list
*p
, *best
;
171 int best_distance
= -1;
174 for (p
= list
; p
; p
= p
->next
) {
176 unsigned flags
= p
->item
->object
.flags
;
178 if (flags
& TREESAME
)
180 distance
= weight(p
);
181 if (nr
- distance
< distance
)
182 distance
= nr
- distance
;
183 if (distance
> best_distance
) {
185 best_distance
= distance
;
193 struct commit
*commit
;
197 static int compare_commit_dist(const void *a_
, const void *b_
)
199 struct commit_dist
*a
, *b
;
201 a
= (struct commit_dist
*)a_
;
202 b
= (struct commit_dist
*)b_
;
203 if (a
->distance
!= b
->distance
)
204 return b
->distance
- a
->distance
; /* desc sort */
205 return hashcmp(a
->commit
->object
.sha1
, b
->commit
->object
.sha1
);
208 static struct commit_list
*best_bisection_sorted(struct commit_list
*list
, int nr
)
210 struct commit_list
*p
;
211 struct commit_dist
*array
= xcalloc(nr
, sizeof(*array
));
214 for (p
= list
, cnt
= 0; p
; p
= p
->next
) {
216 unsigned flags
= p
->item
->object
.flags
;
218 if (flags
& TREESAME
)
220 distance
= weight(p
);
221 if (nr
- distance
< distance
)
222 distance
= nr
- distance
;
223 array
[cnt
].commit
= p
->item
;
224 array
[cnt
].distance
= distance
;
227 qsort(array
, cnt
, sizeof(*array
), compare_commit_dist
);
228 for (p
= list
, i
= 0; i
< cnt
; i
++) {
229 struct name_decoration
*r
= xmalloc(sizeof(*r
) + 100);
230 struct object
*obj
= &(array
[i
].commit
->object
);
232 sprintf(r
->name
, "dist=%d", array
[i
].distance
);
233 r
->next
= add_decoration(&name_decoration
, obj
, r
);
234 p
->item
= array
[i
].commit
;
244 * zero or positive weight is the number of interesting commits it can
245 * reach, including itself. Especially, weight = 0 means it does not
246 * reach any tree-changing commits (e.g. just above uninteresting one
247 * but traversal is with pathspec).
249 * weight = -1 means it has one parent and its distance is yet to
252 * weight = -2 means it has more than one parent and its distance is
253 * unknown. After running count_distance() first, they will get zero
254 * or positive distance.
256 static struct commit_list
*do_find_bisection(struct commit_list
*list
,
257 int nr
, int *weights
,
261 struct commit_list
*p
;
265 for (n
= 0, p
= list
; p
; p
= p
->next
) {
266 struct commit
*commit
= p
->item
;
267 unsigned flags
= commit
->object
.flags
;
269 p
->item
->util
= &weights
[n
++];
270 switch (count_interesting_parents(commit
)) {
272 if (!(flags
& TREESAME
)) {
275 show_list("bisection 2 count one",
279 * otherwise, it is known not to reach any
280 * tree-changing commit and gets weight 0.
292 show_list("bisection 2 initialize", counted
, nr
, list
);
295 * If you have only one parent in the resulting set
296 * then you can reach one commit more than that parent
297 * can reach. So we do not have to run the expensive
298 * count_distance() for single strand of pearls.
300 * However, if you have more than one parents, you cannot
301 * just add their distance and one for yourself, since
302 * they usually reach the same ancestor and you would
303 * end up counting them twice that way.
305 * So we will first count distance of merges the usual
306 * way, and then fill the blanks using cheaper algorithm.
308 for (p
= list
; p
; p
= p
->next
) {
309 if (p
->item
->object
.flags
& UNINTERESTING
)
313 weight_set(p
, count_distance(p
));
314 clear_distance(list
);
316 /* Does it happen to be at exactly half-way? */
317 if (!find_all
&& halfway(p
, nr
))
322 show_list("bisection 2 count_distance", counted
, nr
, list
);
324 while (counted
< nr
) {
325 for (p
= list
; p
; p
= p
->next
) {
326 struct commit_list
*q
;
327 unsigned flags
= p
->item
->object
.flags
;
331 for (q
= p
->item
->parents
; q
; q
= q
->next
) {
332 if (q
->item
->object
.flags
& UNINTERESTING
)
341 * weight for p is unknown but q is known.
342 * add one for p itself if p is to be counted,
343 * otherwise inherit it from q directly.
345 if (!(flags
& TREESAME
)) {
346 weight_set(p
, weight(q
)+1);
348 show_list("bisection 2 count one",
352 weight_set(p
, weight(q
));
354 /* Does it happen to be at exactly half-way? */
355 if (!find_all
&& halfway(p
, nr
))
360 show_list("bisection 2 counted all", counted
, nr
, list
);
363 return best_bisection(list
, nr
);
365 return best_bisection_sorted(list
, nr
);
368 struct commit_list
*find_bisection(struct commit_list
*list
,
369 int *reaches
, int *all
,
373 struct commit_list
*p
, *best
, *next
, *last
;
376 show_list("bisection 2 entry", 0, 0, list
);
379 * Count the number of total and tree-changing items on the
380 * list, while reversing the list.
382 for (nr
= on_list
= 0, last
= NULL
, p
= list
;
385 unsigned flags
= p
->item
->object
.flags
;
388 if (flags
& UNINTERESTING
)
392 if (!(flags
& TREESAME
))
397 show_list("bisection 2 sorted", 0, nr
, list
);
400 weights
= xcalloc(on_list
, sizeof(*weights
));
402 /* Do the real work of finding bisection commit. */
403 best
= do_find_bisection(list
, nr
, weights
, find_all
);
407 *reaches
= weight(best
);
413 static void rev_argv_push(const unsigned char *sha1
, const char *format
)
415 struct strbuf buf
= STRBUF_INIT
;
417 strbuf_addf(&buf
, format
, sha1_to_hex(sha1
));
418 ALLOC_GROW(rev_argv
, rev_argv_nr
+ 1, rev_argv_alloc
);
419 rev_argv
[rev_argv_nr
++] = strbuf_detach(&buf
, NULL
);
422 static void sha1_array_push(struct sha1_array
*array
,
423 const unsigned char *sha1
)
425 ALLOC_GROW(array
->sha1
, array
->sha1_nr
+ 1, array
->sha1_alloc
);
426 hashcpy(array
->sha1
[array
->sha1_nr
++], sha1
);
429 static int register_ref(const char *refname
, const unsigned char *sha1
,
430 int flags
, void *cb_data
)
432 if (!strcmp(refname
, "bad")) {
433 current_bad_sha1
= sha1
;
434 } else if (!prefixcmp(refname
, "good-")) {
435 sha1_array_push(&good_revs
, sha1
);
436 } else if (!prefixcmp(refname
, "skip-")) {
437 sha1_array_push(&skipped_revs
, sha1
);
443 static int read_bisect_refs(void)
445 return for_each_ref_in("refs/bisect/", register_ref
, NULL
);
448 void read_bisect_paths(void)
450 struct strbuf str
= STRBUF_INIT
;
451 const char *filename
= git_path("BISECT_NAMES");
452 FILE *fp
= fopen(filename
, "r");
455 die("Could not open file '%s': %s", filename
, strerror(errno
));
457 while (strbuf_getline(&str
, fp
, '\n') != EOF
) {
462 quoted
= strbuf_detach(&str
, NULL
);
463 res
= sq_dequote_to_argv(quoted
, &rev_argv
,
464 &rev_argv_nr
, &rev_argv_alloc
);
466 die("Badly quoted content in file '%s': %s",
470 strbuf_release(&str
);
474 static int skipcmp(const void *a
, const void *b
)
476 return hashcmp(a
, b
);
479 static void prepare_skipped(void)
481 qsort(skipped_revs
.sha1
, skipped_revs
.sha1_nr
,
482 sizeof(*skipped_revs
.sha1
), skipcmp
);
485 static const unsigned char *skipped_sha1_access(size_t index
, void *table
)
487 unsigned char (*skipped
)[20] = table
;
488 return skipped
[index
];
491 static int lookup_skipped(unsigned char *sha1
)
493 return sha1_pos(sha1
, skipped_revs
.sha1
, skipped_revs
.sha1_nr
,
494 skipped_sha1_access
);
497 struct commit_list
*filter_skipped(struct commit_list
*list
,
498 struct commit_list
**tried
,
501 struct commit_list
*filtered
= NULL
, **f
= &filtered
;
505 if (!skipped_revs
.sha1_nr
)
511 struct commit_list
*next
= list
->next
;
513 if (0 <= lookup_skipped(list
->item
->object
.sha1
)) {
514 /* Move current to tried list */
520 /* Move current to filtered list */
530 static void bisect_rev_setup(struct rev_info
*revs
, const char *prefix
)
534 init_revisions(revs
, prefix
);
536 revs
->commit_format
= CMIT_FMT_UNSPECIFIED
;
538 if (read_bisect_refs())
539 die("reading bisect refs failed");
541 /* argv[0] will be ignored by setup_revisions */
542 ALLOC_GROW(rev_argv
, rev_argv_nr
+ 1, rev_argv_alloc
);
543 rev_argv
[rev_argv_nr
++] = xstrdup("bisect_rev_setup");
545 rev_argv_push(current_bad_sha1
, "%s");
547 for (i
= 0; i
< good_revs
.sha1_nr
; i
++)
548 rev_argv_push(good_revs
.sha1
[i
], "^%s");
550 ALLOC_GROW(rev_argv
, rev_argv_nr
+ 1, rev_argv_alloc
);
551 rev_argv
[rev_argv_nr
++] = xstrdup("--");
555 ALLOC_GROW(rev_argv
, rev_argv_nr
+ 1, rev_argv_alloc
);
556 rev_argv
[rev_argv_nr
++] = NULL
;
558 setup_revisions(rev_argv_nr
, rev_argv
, revs
, NULL
);
563 static void bisect_common(struct rev_info
*revs
, const char *prefix
,
564 int *reaches
, int *all
)
566 bisect_rev_setup(revs
, prefix
);
568 if (prepare_revision_walk(revs
))
569 die("revision walk setup failed");
570 if (revs
->tree_objects
)
571 mark_edges_uninteresting(revs
->commits
, revs
, NULL
);
573 revs
->commits
= find_bisection(revs
->commits
, reaches
, all
,
574 !!skipped_revs
.sha1_nr
);
577 static void exit_if_skipped_commits(struct commit_list
*tried
,
578 const unsigned char *bad
)
583 printf("There are only 'skip'ped commits left to test.\n"
584 "The first bad commit could be any of:\n");
585 print_commit_list(tried
, "%s\n", "%s\n");
587 printf("%s\n", sha1_to_hex(bad
));
588 printf("We cannot bisect more!\n");
592 static void mark_expected_rev(char *bisect_rev_hex
)
594 int len
= strlen(bisect_rev_hex
);
595 const char *filename
= git_path("BISECT_EXPECTED_REV");
596 int fd
= open(filename
, O_CREAT
| O_TRUNC
| O_WRONLY
, 0600);
599 die("could not create file '%s': %s",
600 filename
, strerror(errno
));
602 bisect_rev_hex
[len
] = '\n';
603 write_or_die(fd
, bisect_rev_hex
, len
+ 1);
604 bisect_rev_hex
[len
] = '\0';
607 die("closing file %s: %s", filename
, strerror(errno
));
610 static int bisect_checkout(char *bisect_rev_hex
)
614 mark_expected_rev(bisect_rev_hex
);
616 argv_checkout
[2] = bisect_rev_hex
;
617 res
= run_command_v_opt(argv_checkout
, RUN_GIT_CMD
);
621 argv_show_branch
[1] = bisect_rev_hex
;
622 return run_command_v_opt(argv_show_branch
, RUN_GIT_CMD
);
626 * We use the convention that exiting with an exit code 10 means that
627 * the bisection process finished successfully.
628 * In this case the calling shell script should exit 0.
630 int bisect_next_exit(const char *prefix
)
632 struct rev_info revs
;
633 struct commit_list
*tried
;
634 int reaches
= 0, all
= 0, nr
;
635 const unsigned char *bisect_rev
;
636 char bisect_rev_hex
[41];
638 bisect_common(&revs
, prefix
, &reaches
, &all
);
640 revs
.commits
= filter_skipped(revs
.commits
, &tried
, 0);
644 * We should exit here only if the "bad"
645 * commit is also a "skip" commit.
647 exit_if_skipped_commits(tried
, NULL
);
649 printf("%s was both good and bad\n",
650 sha1_to_hex(current_bad_sha1
));
654 bisect_rev
= revs
.commits
->item
->object
.sha1
;
655 memcpy(bisect_rev_hex
, sha1_to_hex(bisect_rev
), 41);
657 if (!hashcmp(bisect_rev
, current_bad_sha1
)) {
658 exit_if_skipped_commits(tried
, current_bad_sha1
);
659 printf("%s is first bad commit\n", bisect_rev_hex
);
660 argv_diff_tree
[2] = bisect_rev_hex
;
661 run_command_v_opt(argv_diff_tree
, RUN_GIT_CMD
);
662 /* This means the bisection process succeeded. */
666 nr
= all
- reaches
- 1;
667 printf("Bisecting: %d revisions left to test after this "
668 "(roughly %d steps)\n", nr
, estimate_bisect_steps(all
));
670 return bisect_checkout(bisect_rev_hex
);