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 int bisect_next_vars(const char *prefix
)
559 struct rev_info revs
;
560 struct rev_list_info info
;
561 int reaches
= 0, all
= 0;
563 memset(&info
, 0, sizeof(info
));
565 info
.bisect_show_flags
= BISECT_SHOW_TRIED
| BISECT_SHOW_STRINGED
;
567 bisect_common(&revs
, prefix
, &reaches
, &all
);
569 return show_bisect_vars(&info
, reaches
, all
);
572 static void exit_if_skipped_commits(struct commit_list
*tried
,
573 const unsigned char *bad
)
578 printf("There are only 'skip'ped commits left to test.\n"
579 "The first bad commit could be any of:\n");
580 print_commit_list(tried
, "%s\n", "%s\n");
582 printf("%s\n", sha1_to_hex(bad
));
583 printf("We cannot bisect more!\n");
587 static void mark_expected_rev(char *bisect_rev_hex
)
589 int len
= strlen(bisect_rev_hex
);
590 const char *filename
= git_path("BISECT_EXPECTED_REV");
591 int fd
= open(filename
, O_CREAT
| O_TRUNC
| O_WRONLY
, 0600);
594 die("could not create file '%s': %s",
595 filename
, strerror(errno
));
597 bisect_rev_hex
[len
] = '\n';
598 write_or_die(fd
, bisect_rev_hex
, len
+ 1);
599 bisect_rev_hex
[len
] = '\0';
602 die("closing file %s: %s", filename
, strerror(errno
));
605 static int bisect_checkout(char *bisect_rev_hex
)
609 mark_expected_rev(bisect_rev_hex
);
611 argv_checkout
[2] = bisect_rev_hex
;
612 res
= run_command_v_opt(argv_checkout
, RUN_GIT_CMD
);
616 argv_show_branch
[1] = bisect_rev_hex
;
617 return run_command_v_opt(argv_show_branch
, RUN_GIT_CMD
);
621 * We use the convention that exiting with an exit code 10 means that
622 * the bisection process finished successfully.
623 * In this case the calling shell script should exit 0.
625 int bisect_next_exit(const char *prefix
)
627 struct rev_info revs
;
628 struct commit_list
*tried
;
629 int reaches
= 0, all
= 0, nr
;
630 const unsigned char *bisect_rev
;
631 char bisect_rev_hex
[41];
633 bisect_common(&revs
, prefix
, &reaches
, &all
);
635 revs
.commits
= filter_skipped(revs
.commits
, &tried
, 0);
639 * We should exit here only if the "bad"
640 * commit is also a "skip" commit.
642 exit_if_skipped_commits(tried
, NULL
);
644 printf("%s was both good and bad\n",
645 sha1_to_hex(current_bad_sha1
));
649 bisect_rev
= revs
.commits
->item
->object
.sha1
;
650 memcpy(bisect_rev_hex
, sha1_to_hex(bisect_rev
), 41);
652 if (!hashcmp(bisect_rev
, current_bad_sha1
)) {
653 exit_if_skipped_commits(tried
, current_bad_sha1
);
654 printf("%s is first bad commit\n", bisect_rev_hex
);
655 argv_diff_tree
[2] = bisect_rev_hex
;
656 run_command_v_opt(argv_diff_tree
, RUN_GIT_CMD
);
657 /* This means the bisection process succeeded. */
661 nr
= all
- reaches
- 1;
662 printf("Bisecting: %d revisions left to test after this "
663 "(roughly %d steps)\n", nr
, estimate_bisect_steps(all
));
665 return bisect_checkout(bisect_rev_hex
);