6 #include "list-objects.h"
8 #include "sha1-lookup.h"
9 #include "run-command.h"
12 static unsigned char (*skipped_sha1
)[20];
13 static int skipped_sha1_nr
;
14 static int skipped_sha1_alloc
;
16 static const char **rev_argv
;
17 static int rev_argv_nr
;
18 static int rev_argv_alloc
;
20 static const unsigned char *current_bad_sha1
;
22 static const char *argv_diff_tree
[] = {"diff-tree", "--pretty", NULL
, NULL
};
23 static const char *argv_checkout
[] = {"checkout", "-q", NULL
, "--", NULL
};
24 static const char *argv_show_branch
[] = {"show-branch", NULL
, NULL
};
26 /* bits #0-15 in revision.h */
28 #define COUNTED (1u<<16)
31 * This is a truly stupid algorithm, but it's only
32 * used for bisection, and we just don't care enough.
34 * We care just barely enough to avoid recursing for
37 static int count_distance(struct commit_list
*entry
)
42 struct commit
*commit
= entry
->item
;
43 struct commit_list
*p
;
45 if (commit
->object
.flags
& (UNINTERESTING
| COUNTED
))
47 if (!(commit
->object
.flags
& TREESAME
))
49 commit
->object
.flags
|= COUNTED
;
55 nr
+= count_distance(p
);
64 static void clear_distance(struct commit_list
*list
)
67 struct commit
*commit
= list
->item
;
68 commit
->object
.flags
&= ~COUNTED
;
73 #define DEBUG_BISECT 0
75 static inline int weight(struct commit_list
*elem
)
77 return *((int*)(elem
->item
->util
));
80 static inline void weight_set(struct commit_list
*elem
, int weight
)
82 *((int*)(elem
->item
->util
)) = weight
;
85 static int count_interesting_parents(struct commit
*commit
)
87 struct commit_list
*p
;
90 for (count
= 0, p
= commit
->parents
; p
; p
= p
->next
) {
91 if (p
->item
->object
.flags
& UNINTERESTING
)
98 static inline int halfway(struct commit_list
*p
, int nr
)
101 * Don't short-cut something we are not going to return!
103 if (p
->item
->object
.flags
& TREESAME
)
108 * 2 and 3 are halfway of 5.
109 * 3 is halfway of 6 but 2 and 4 are not.
111 switch (2 * weight(p
) - nr
) {
112 case -1: case 0: case 1:
120 #define show_list(a,b,c,d) do { ; } while (0)
122 static void show_list(const char *debug
, int counted
, int nr
,
123 struct commit_list
*list
)
125 struct commit_list
*p
;
127 fprintf(stderr
, "%s (%d/%d)\n", debug
, counted
, nr
);
129 for (p
= list
; p
; p
= p
->next
) {
130 struct commit_list
*pp
;
131 struct commit
*commit
= p
->item
;
132 unsigned flags
= commit
->object
.flags
;
133 enum object_type type
;
135 char *buf
= read_sha1_file(commit
->object
.sha1
, &type
, &size
);
138 fprintf(stderr
, "%c%c%c ",
139 (flags
& TREESAME
) ? ' ' : 'T',
140 (flags
& UNINTERESTING
) ? 'U' : ' ',
141 (flags
& COUNTED
) ? 'C' : ' ');
143 fprintf(stderr
, "%3d", weight(p
));
145 fprintf(stderr
, "---");
146 fprintf(stderr
, " %.*s", 8, sha1_to_hex(commit
->object
.sha1
));
147 for (pp
= commit
->parents
; pp
; pp
= pp
->next
)
148 fprintf(stderr
, " %.*s", 8,
149 sha1_to_hex(pp
->item
->object
.sha1
));
151 sp
= strstr(buf
, "\n\n");
154 for (ep
= sp
; *ep
&& *ep
!= '\n'; ep
++)
156 fprintf(stderr
, " %.*s", (int)(ep
- sp
), sp
);
158 fprintf(stderr
, "\n");
161 #endif /* DEBUG_BISECT */
163 static struct commit_list
*best_bisection(struct commit_list
*list
, int nr
)
165 struct commit_list
*p
, *best
;
166 int best_distance
= -1;
169 for (p
= list
; p
; p
= p
->next
) {
171 unsigned flags
= p
->item
->object
.flags
;
173 if (flags
& TREESAME
)
175 distance
= weight(p
);
176 if (nr
- distance
< distance
)
177 distance
= nr
- distance
;
178 if (distance
> best_distance
) {
180 best_distance
= distance
;
188 struct commit
*commit
;
192 static int compare_commit_dist(const void *a_
, const void *b_
)
194 struct commit_dist
*a
, *b
;
196 a
= (struct commit_dist
*)a_
;
197 b
= (struct commit_dist
*)b_
;
198 if (a
->distance
!= b
->distance
)
199 return b
->distance
- a
->distance
; /* desc sort */
200 return hashcmp(a
->commit
->object
.sha1
, b
->commit
->object
.sha1
);
203 static struct commit_list
*best_bisection_sorted(struct commit_list
*list
, int nr
)
205 struct commit_list
*p
;
206 struct commit_dist
*array
= xcalloc(nr
, sizeof(*array
));
209 for (p
= list
, cnt
= 0; p
; p
= p
->next
) {
211 unsigned flags
= p
->item
->object
.flags
;
213 if (flags
& TREESAME
)
215 distance
= weight(p
);
216 if (nr
- distance
< distance
)
217 distance
= nr
- distance
;
218 array
[cnt
].commit
= p
->item
;
219 array
[cnt
].distance
= distance
;
222 qsort(array
, cnt
, sizeof(*array
), compare_commit_dist
);
223 for (p
= list
, i
= 0; i
< cnt
; i
++) {
224 struct name_decoration
*r
= xmalloc(sizeof(*r
) + 100);
225 struct object
*obj
= &(array
[i
].commit
->object
);
227 sprintf(r
->name
, "dist=%d", array
[i
].distance
);
228 r
->next
= add_decoration(&name_decoration
, obj
, r
);
229 p
->item
= array
[i
].commit
;
239 * zero or positive weight is the number of interesting commits it can
240 * reach, including itself. Especially, weight = 0 means it does not
241 * reach any tree-changing commits (e.g. just above uninteresting one
242 * but traversal is with pathspec).
244 * weight = -1 means it has one parent and its distance is yet to
247 * weight = -2 means it has more than one parent and its distance is
248 * unknown. After running count_distance() first, they will get zero
249 * or positive distance.
251 static struct commit_list
*do_find_bisection(struct commit_list
*list
,
252 int nr
, int *weights
,
256 struct commit_list
*p
;
260 for (n
= 0, p
= list
; p
; p
= p
->next
) {
261 struct commit
*commit
= p
->item
;
262 unsigned flags
= commit
->object
.flags
;
264 p
->item
->util
= &weights
[n
++];
265 switch (count_interesting_parents(commit
)) {
267 if (!(flags
& TREESAME
)) {
270 show_list("bisection 2 count one",
274 * otherwise, it is known not to reach any
275 * tree-changing commit and gets weight 0.
287 show_list("bisection 2 initialize", counted
, nr
, list
);
290 * If you have only one parent in the resulting set
291 * then you can reach one commit more than that parent
292 * can reach. So we do not have to run the expensive
293 * count_distance() for single strand of pearls.
295 * However, if you have more than one parents, you cannot
296 * just add their distance and one for yourself, since
297 * they usually reach the same ancestor and you would
298 * end up counting them twice that way.
300 * So we will first count distance of merges the usual
301 * way, and then fill the blanks using cheaper algorithm.
303 for (p
= list
; p
; p
= p
->next
) {
304 if (p
->item
->object
.flags
& UNINTERESTING
)
308 weight_set(p
, count_distance(p
));
309 clear_distance(list
);
311 /* Does it happen to be at exactly half-way? */
312 if (!find_all
&& halfway(p
, nr
))
317 show_list("bisection 2 count_distance", counted
, nr
, list
);
319 while (counted
< nr
) {
320 for (p
= list
; p
; p
= p
->next
) {
321 struct commit_list
*q
;
322 unsigned flags
= p
->item
->object
.flags
;
326 for (q
= p
->item
->parents
; q
; q
= q
->next
) {
327 if (q
->item
->object
.flags
& UNINTERESTING
)
336 * weight for p is unknown but q is known.
337 * add one for p itself if p is to be counted,
338 * otherwise inherit it from q directly.
340 if (!(flags
& TREESAME
)) {
341 weight_set(p
, weight(q
)+1);
343 show_list("bisection 2 count one",
347 weight_set(p
, weight(q
));
349 /* Does it happen to be at exactly half-way? */
350 if (!find_all
&& halfway(p
, nr
))
355 show_list("bisection 2 counted all", counted
, nr
, list
);
358 return best_bisection(list
, nr
);
360 return best_bisection_sorted(list
, nr
);
363 struct commit_list
*find_bisection(struct commit_list
*list
,
364 int *reaches
, int *all
,
368 struct commit_list
*p
, *best
, *next
, *last
;
371 show_list("bisection 2 entry", 0, 0, list
);
374 * Count the number of total and tree-changing items on the
375 * list, while reversing the list.
377 for (nr
= on_list
= 0, last
= NULL
, p
= list
;
380 unsigned flags
= p
->item
->object
.flags
;
383 if (flags
& UNINTERESTING
)
387 if (!(flags
& TREESAME
))
392 show_list("bisection 2 sorted", 0, nr
, list
);
395 weights
= xcalloc(on_list
, sizeof(*weights
));
397 /* Do the real work of finding bisection commit. */
398 best
= do_find_bisection(list
, nr
, weights
, find_all
);
402 *reaches
= weight(best
);
408 static int register_ref(const char *refname
, const unsigned char *sha1
,
409 int flags
, void *cb_data
)
411 if (!strcmp(refname
, "bad")) {
412 ALLOC_GROW(rev_argv
, rev_argv_nr
+ 1, rev_argv_alloc
);
413 current_bad_sha1
= sha1
;
414 rev_argv
[rev_argv_nr
++] = xstrdup(sha1_to_hex(sha1
));
415 } else if (!prefixcmp(refname
, "good-")) {
416 const char *hex
= sha1_to_hex(sha1
);
417 char *good
= xmalloc(strlen(hex
) + 2);
419 memcpy(good
+ 1, hex
, strlen(hex
) + 1);
420 ALLOC_GROW(rev_argv
, rev_argv_nr
+ 1, rev_argv_alloc
);
421 rev_argv
[rev_argv_nr
++] = good
;
422 } else if (!prefixcmp(refname
, "skip-")) {
423 ALLOC_GROW(skipped_sha1
, skipped_sha1_nr
+ 1,
425 hashcpy(skipped_sha1
[skipped_sha1_nr
++], sha1
);
431 static int read_bisect_refs(void)
433 return for_each_ref_in("refs/bisect/", register_ref
, NULL
);
436 void read_bisect_paths(void)
438 struct strbuf str
= STRBUF_INIT
;
439 const char *filename
= git_path("BISECT_NAMES");
440 FILE *fp
= fopen(filename
, "r");
443 die("Could not open file '%s': %s", filename
, strerror(errno
));
445 while (strbuf_getline(&str
, fp
, '\n') != EOF
) {
450 quoted
= strbuf_detach(&str
, NULL
);
451 res
= sq_dequote_to_argv(quoted
, &rev_argv
,
452 &rev_argv_nr
, &rev_argv_alloc
);
454 die("Badly quoted content in file '%s': %s",
458 strbuf_release(&str
);
462 static int skipcmp(const void *a
, const void *b
)
464 return hashcmp(a
, b
);
467 static void prepare_skipped(void)
469 qsort(skipped_sha1
, skipped_sha1_nr
, sizeof(*skipped_sha1
), skipcmp
);
472 static const unsigned char *skipped_sha1_access(size_t index
, void *table
)
474 unsigned char (*skipped
)[20] = table
;
475 return skipped
[index
];
478 static int lookup_skipped(unsigned char *sha1
)
480 return sha1_pos(sha1
, skipped_sha1
, skipped_sha1_nr
,
481 skipped_sha1_access
);
484 struct commit_list
*filter_skipped(struct commit_list
*list
,
485 struct commit_list
**tried
,
488 struct commit_list
*filtered
= NULL
, **f
= &filtered
;
492 if (!skipped_sha1_nr
)
498 struct commit_list
*next
= list
->next
;
500 if (0 <= lookup_skipped(list
->item
->object
.sha1
)) {
501 /* Move current to tried list */
507 /* Move current to filtered list */
517 static void bisect_rev_setup(struct rev_info
*revs
, const char *prefix
)
519 init_revisions(revs
, prefix
);
521 revs
->commit_format
= CMIT_FMT_UNSPECIFIED
;
523 /* argv[0] will be ignored by setup_revisions */
524 ALLOC_GROW(rev_argv
, rev_argv_nr
+ 1, rev_argv_alloc
);
525 rev_argv
[rev_argv_nr
++] = xstrdup("bisect_rev_setup");
527 if (read_bisect_refs())
528 die("reading bisect refs failed");
530 ALLOC_GROW(rev_argv
, rev_argv_nr
+ 1, rev_argv_alloc
);
531 rev_argv
[rev_argv_nr
++] = xstrdup("--");
535 ALLOC_GROW(rev_argv
, rev_argv_nr
+ 1, rev_argv_alloc
);
536 rev_argv
[rev_argv_nr
++] = NULL
;
538 setup_revisions(rev_argv_nr
, rev_argv
, revs
, NULL
);
543 static void bisect_common(struct rev_info
*revs
, const char *prefix
,
544 int *reaches
, int *all
)
546 bisect_rev_setup(revs
, prefix
);
548 if (prepare_revision_walk(revs
))
549 die("revision walk setup failed");
550 if (revs
->tree_objects
)
551 mark_edges_uninteresting(revs
->commits
, revs
, NULL
);
553 revs
->commits
= find_bisection(revs
->commits
, reaches
, all
,
557 static void exit_if_skipped_commits(struct commit_list
*tried
,
558 const unsigned char *bad
)
563 printf("There are only 'skip'ped commits left to test.\n"
564 "The first bad commit could be any of:\n");
565 print_commit_list(tried
, "%s\n", "%s\n");
567 printf("%s\n", sha1_to_hex(bad
));
568 printf("We cannot bisect more!\n");
572 static void mark_expected_rev(char *bisect_rev_hex
)
574 int len
= strlen(bisect_rev_hex
);
575 const char *filename
= git_path("BISECT_EXPECTED_REV");
576 int fd
= open(filename
, O_CREAT
| O_TRUNC
| O_WRONLY
, 0600);
579 die("could not create file '%s': %s",
580 filename
, strerror(errno
));
582 bisect_rev_hex
[len
] = '\n';
583 write_or_die(fd
, bisect_rev_hex
, len
+ 1);
584 bisect_rev_hex
[len
] = '\0';
587 die("closing file %s: %s", filename
, strerror(errno
));
590 static int bisect_checkout(char *bisect_rev_hex
)
594 mark_expected_rev(bisect_rev_hex
);
596 argv_checkout
[2] = bisect_rev_hex
;
597 res
= run_command_v_opt(argv_checkout
, RUN_GIT_CMD
);
601 argv_show_branch
[1] = bisect_rev_hex
;
602 return run_command_v_opt(argv_show_branch
, RUN_GIT_CMD
);
606 * We use the convention that exiting with an exit code 10 means that
607 * the bisection process finished successfully.
608 * In this case the calling shell script should exit 0.
610 int bisect_next_exit(const char *prefix
)
612 struct rev_info revs
;
613 struct commit_list
*tried
;
614 int reaches
= 0, all
= 0, nr
;
615 const unsigned char *bisect_rev
;
616 char bisect_rev_hex
[41];
618 bisect_common(&revs
, prefix
, &reaches
, &all
);
620 revs
.commits
= filter_skipped(revs
.commits
, &tried
, 0);
624 * We should exit here only if the "bad"
625 * commit is also a "skip" commit.
627 exit_if_skipped_commits(tried
, NULL
);
629 printf("%s was both good and bad\n",
630 sha1_to_hex(current_bad_sha1
));
634 bisect_rev
= revs
.commits
->item
->object
.sha1
;
635 memcpy(bisect_rev_hex
, sha1_to_hex(bisect_rev
), 41);
637 if (!hashcmp(bisect_rev
, current_bad_sha1
)) {
638 exit_if_skipped_commits(tried
, current_bad_sha1
);
639 printf("%s is first bad commit\n", bisect_rev_hex
);
640 argv_diff_tree
[2] = bisect_rev_hex
;
641 run_command_v_opt(argv_diff_tree
, RUN_GIT_CMD
);
642 /* This means the bisection process succeeded. */
646 nr
= all
- reaches
- 1;
647 printf("Bisecting: %d revisions left to test after this "
648 "(roughly %d steps)\n", nr
, estimate_bisect_steps(all
));
650 return bisect_checkout(bisect_rev_hex
);