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 unsigned char *current_bad_sha1
;
29 struct argv_array rev_argv
;
31 static const char *argv_diff_tree
[] = {"diff-tree", "--pretty", NULL
, NULL
};
32 static const char *argv_checkout
[] = {"checkout", "-q", NULL
, "--", NULL
};
33 static const char *argv_show_branch
[] = {"show-branch", NULL
, NULL
};
35 /* bits #0-15 in revision.h */
37 #define COUNTED (1u<<16)
40 * This is a truly stupid algorithm, but it's only
41 * used for bisection, and we just don't care enough.
43 * We care just barely enough to avoid recursing for
46 static int count_distance(struct commit_list
*entry
)
51 struct commit
*commit
= entry
->item
;
52 struct commit_list
*p
;
54 if (commit
->object
.flags
& (UNINTERESTING
| COUNTED
))
56 if (!(commit
->object
.flags
& TREESAME
))
58 commit
->object
.flags
|= COUNTED
;
64 nr
+= count_distance(p
);
73 static void clear_distance(struct commit_list
*list
)
76 struct commit
*commit
= list
->item
;
77 commit
->object
.flags
&= ~COUNTED
;
82 #define DEBUG_BISECT 0
84 static inline int weight(struct commit_list
*elem
)
86 return *((int*)(elem
->item
->util
));
89 static inline void weight_set(struct commit_list
*elem
, int weight
)
91 *((int*)(elem
->item
->util
)) = weight
;
94 static int count_interesting_parents(struct commit
*commit
)
96 struct commit_list
*p
;
99 for (count
= 0, p
= commit
->parents
; p
; p
= p
->next
) {
100 if (p
->item
->object
.flags
& UNINTERESTING
)
107 static inline int halfway(struct commit_list
*p
, int nr
)
110 * Don't short-cut something we are not going to return!
112 if (p
->item
->object
.flags
& TREESAME
)
117 * 2 and 3 are halfway of 5.
118 * 3 is halfway of 6 but 2 and 4 are not.
120 switch (2 * weight(p
) - nr
) {
121 case -1: case 0: case 1:
129 #define show_list(a,b,c,d) do { ; } while (0)
131 static void show_list(const char *debug
, int counted
, int nr
,
132 struct commit_list
*list
)
134 struct commit_list
*p
;
136 fprintf(stderr
, "%s (%d/%d)\n", debug
, counted
, nr
);
138 for (p
= list
; p
; p
= p
->next
) {
139 struct commit_list
*pp
;
140 struct commit
*commit
= p
->item
;
141 unsigned flags
= commit
->object
.flags
;
142 enum object_type type
;
144 char *buf
= read_sha1_file(commit
->object
.sha1
, &type
, &size
);
147 fprintf(stderr
, "%c%c%c ",
148 (flags
& TREESAME
) ? ' ' : 'T',
149 (flags
& UNINTERESTING
) ? 'U' : ' ',
150 (flags
& COUNTED
) ? 'C' : ' ');
152 fprintf(stderr
, "%3d", weight(p
));
154 fprintf(stderr
, "---");
155 fprintf(stderr
, " %.*s", 8, sha1_to_hex(commit
->object
.sha1
));
156 for (pp
= commit
->parents
; pp
; pp
= pp
->next
)
157 fprintf(stderr
, " %.*s", 8,
158 sha1_to_hex(pp
->item
->object
.sha1
));
160 sp
= strstr(buf
, "\n\n");
163 for (ep
= sp
; *ep
&& *ep
!= '\n'; ep
++)
165 fprintf(stderr
, " %.*s", (int)(ep
- sp
), sp
);
167 fprintf(stderr
, "\n");
170 #endif /* DEBUG_BISECT */
172 static struct commit_list
*best_bisection(struct commit_list
*list
, int nr
)
174 struct commit_list
*p
, *best
;
175 int best_distance
= -1;
178 for (p
= list
; p
; p
= p
->next
) {
180 unsigned flags
= p
->item
->object
.flags
;
182 if (flags
& TREESAME
)
184 distance
= weight(p
);
185 if (nr
- distance
< distance
)
186 distance
= nr
- distance
;
187 if (distance
> best_distance
) {
189 best_distance
= distance
;
197 struct commit
*commit
;
201 static int compare_commit_dist(const void *a_
, const void *b_
)
203 struct commit_dist
*a
, *b
;
205 a
= (struct commit_dist
*)a_
;
206 b
= (struct commit_dist
*)b_
;
207 if (a
->distance
!= b
->distance
)
208 return b
->distance
- a
->distance
; /* desc sort */
209 return hashcmp(a
->commit
->object
.sha1
, b
->commit
->object
.sha1
);
212 static struct commit_list
*best_bisection_sorted(struct commit_list
*list
, int nr
)
214 struct commit_list
*p
;
215 struct commit_dist
*array
= xcalloc(nr
, sizeof(*array
));
218 for (p
= list
, cnt
= 0; p
; p
= p
->next
) {
220 unsigned flags
= p
->item
->object
.flags
;
222 if (flags
& TREESAME
)
224 distance
= weight(p
);
225 if (nr
- distance
< distance
)
226 distance
= nr
- distance
;
227 array
[cnt
].commit
= p
->item
;
228 array
[cnt
].distance
= distance
;
231 qsort(array
, cnt
, sizeof(*array
), compare_commit_dist
);
232 for (p
= list
, i
= 0; i
< cnt
; i
++) {
233 struct name_decoration
*r
= xmalloc(sizeof(*r
) + 100);
234 struct object
*obj
= &(array
[i
].commit
->object
);
236 sprintf(r
->name
, "dist=%d", array
[i
].distance
);
237 r
->next
= add_decoration(&name_decoration
, obj
, r
);
238 p
->item
= array
[i
].commit
;
248 * zero or positive weight is the number of interesting commits it can
249 * reach, including itself. Especially, weight = 0 means it does not
250 * reach any tree-changing commits (e.g. just above uninteresting one
251 * but traversal is with pathspec).
253 * weight = -1 means it has one parent and its distance is yet to
256 * weight = -2 means it has more than one parent and its distance is
257 * unknown. After running count_distance() first, they will get zero
258 * or positive distance.
260 static struct commit_list
*do_find_bisection(struct commit_list
*list
,
261 int nr
, int *weights
,
265 struct commit_list
*p
;
269 for (n
= 0, p
= list
; p
; p
= p
->next
) {
270 struct commit
*commit
= p
->item
;
271 unsigned flags
= commit
->object
.flags
;
273 p
->item
->util
= &weights
[n
++];
274 switch (count_interesting_parents(commit
)) {
276 if (!(flags
& TREESAME
)) {
279 show_list("bisection 2 count one",
283 * otherwise, it is known not to reach any
284 * tree-changing commit and gets weight 0.
296 show_list("bisection 2 initialize", counted
, nr
, list
);
299 * If you have only one parent in the resulting set
300 * then you can reach one commit more than that parent
301 * can reach. So we do not have to run the expensive
302 * count_distance() for single strand of pearls.
304 * However, if you have more than one parents, you cannot
305 * just add their distance and one for yourself, since
306 * they usually reach the same ancestor and you would
307 * end up counting them twice that way.
309 * So we will first count distance of merges the usual
310 * way, and then fill the blanks using cheaper algorithm.
312 for (p
= list
; p
; p
= p
->next
) {
313 if (p
->item
->object
.flags
& UNINTERESTING
)
317 weight_set(p
, count_distance(p
));
318 clear_distance(list
);
320 /* Does it happen to be at exactly half-way? */
321 if (!find_all
&& halfway(p
, nr
))
326 show_list("bisection 2 count_distance", counted
, nr
, list
);
328 while (counted
< nr
) {
329 for (p
= list
; p
; p
= p
->next
) {
330 struct commit_list
*q
;
331 unsigned flags
= p
->item
->object
.flags
;
335 for (q
= p
->item
->parents
; q
; q
= q
->next
) {
336 if (q
->item
->object
.flags
& UNINTERESTING
)
345 * weight for p is unknown but q is known.
346 * add one for p itself if p is to be counted,
347 * otherwise inherit it from q directly.
349 if (!(flags
& TREESAME
)) {
350 weight_set(p
, weight(q
)+1);
352 show_list("bisection 2 count one",
356 weight_set(p
, weight(q
));
358 /* Does it happen to be at exactly half-way? */
359 if (!find_all
&& halfway(p
, nr
))
364 show_list("bisection 2 counted all", counted
, nr
, list
);
367 return best_bisection(list
, nr
);
369 return best_bisection_sorted(list
, nr
);
372 struct commit_list
*find_bisection(struct commit_list
*list
,
373 int *reaches
, int *all
,
377 struct commit_list
*p
, *best
, *next
, *last
;
380 show_list("bisection 2 entry", 0, 0, list
);
383 * Count the number of total and tree-changing items on the
384 * list, while reversing the list.
386 for (nr
= on_list
= 0, last
= NULL
, p
= list
;
389 unsigned flags
= p
->item
->object
.flags
;
392 if (flags
& UNINTERESTING
)
396 if (!(flags
& TREESAME
))
401 show_list("bisection 2 sorted", 0, nr
, list
);
404 weights
= xcalloc(on_list
, sizeof(*weights
));
406 /* Do the real work of finding bisection commit. */
407 best
= do_find_bisection(list
, nr
, weights
, find_all
);
411 *reaches
= weight(best
);
417 static void argv_array_push(struct argv_array
*array
, const char *string
)
419 ALLOC_GROW(array
->argv
, array
->argv_nr
+ 1, array
->argv_alloc
);
420 array
->argv
[array
->argv_nr
++] = string
;
423 static void argv_array_push_sha1(struct argv_array
*array
,
424 const unsigned char *sha1
,
427 struct strbuf buf
= STRBUF_INIT
;
428 strbuf_addf(&buf
, format
, sha1_to_hex(sha1
));
429 argv_array_push(array
, strbuf_detach(&buf
, NULL
));
432 static void sha1_array_push(struct sha1_array
*array
,
433 const unsigned char *sha1
)
435 ALLOC_GROW(array
->sha1
, array
->sha1_nr
+ 1, array
->sha1_alloc
);
436 hashcpy(array
->sha1
[array
->sha1_nr
++], sha1
);
439 static int register_ref(const char *refname
, const unsigned char *sha1
,
440 int flags
, void *cb_data
)
442 if (!strcmp(refname
, "bad")) {
443 current_bad_sha1
= sha1
;
444 } else if (!prefixcmp(refname
, "good-")) {
445 sha1_array_push(&good_revs
, sha1
);
446 } else if (!prefixcmp(refname
, "skip-")) {
447 sha1_array_push(&skipped_revs
, sha1
);
453 static int read_bisect_refs(void)
455 return for_each_ref_in("refs/bisect/", register_ref
, NULL
);
458 void read_bisect_paths(struct argv_array
*array
)
460 struct strbuf str
= STRBUF_INIT
;
461 const char *filename
= git_path("BISECT_NAMES");
462 FILE *fp
= fopen(filename
, "r");
465 die("Could not open file '%s': %s", filename
, strerror(errno
));
467 while (strbuf_getline(&str
, fp
, '\n') != EOF
) {
472 quoted
= strbuf_detach(&str
, NULL
);
473 res
= sq_dequote_to_argv(quoted
, &array
->argv
,
474 &array
->argv_nr
, &array
->argv_alloc
);
476 die("Badly quoted content in file '%s': %s",
480 strbuf_release(&str
);
484 static int skipcmp(const void *a
, const void *b
)
486 return hashcmp(a
, b
);
489 static void prepare_skipped(void)
491 qsort(skipped_revs
.sha1
, skipped_revs
.sha1_nr
,
492 sizeof(*skipped_revs
.sha1
), skipcmp
);
495 static const unsigned char *skipped_sha1_access(size_t index
, void *table
)
497 unsigned char (*skipped
)[20] = table
;
498 return skipped
[index
];
501 static int lookup_skipped(unsigned char *sha1
)
503 return sha1_pos(sha1
, skipped_revs
.sha1
, skipped_revs
.sha1_nr
,
504 skipped_sha1_access
);
507 struct commit_list
*filter_skipped(struct commit_list
*list
,
508 struct commit_list
**tried
,
511 struct commit_list
*filtered
= NULL
, **f
= &filtered
;
515 if (!skipped_revs
.sha1_nr
)
521 struct commit_list
*next
= list
->next
;
523 if (0 <= lookup_skipped(list
->item
->object
.sha1
)) {
524 /* Move current to tried list */
530 /* Move current to filtered list */
540 static void bisect_rev_setup(struct rev_info
*revs
, const char *prefix
)
544 init_revisions(revs
, prefix
);
546 revs
->commit_format
= CMIT_FMT_UNSPECIFIED
;
548 if (read_bisect_refs())
549 die("reading bisect refs failed");
551 /* rev_argv.argv[0] will be ignored by setup_revisions */
552 argv_array_push(&rev_argv
, xstrdup("bisect_rev_setup"));
553 argv_array_push_sha1(&rev_argv
, current_bad_sha1
, "%s");
554 for (i
= 0; i
< good_revs
.sha1_nr
; i
++)
555 argv_array_push_sha1(&rev_argv
, good_revs
.sha1
[i
], "^%s");
556 argv_array_push(&rev_argv
, xstrdup("--"));
557 read_bisect_paths(&rev_argv
);
558 argv_array_push(&rev_argv
, NULL
);
560 setup_revisions(rev_argv
.argv_nr
, rev_argv
.argv
, revs
, NULL
);
564 static void bisect_common(struct rev_info
*revs
, const char *prefix
,
565 int *reaches
, int *all
)
567 bisect_rev_setup(revs
, prefix
);
569 if (prepare_revision_walk(revs
))
570 die("revision walk setup failed");
571 if (revs
->tree_objects
)
572 mark_edges_uninteresting(revs
->commits
, revs
, NULL
);
574 revs
->commits
= find_bisection(revs
->commits
, reaches
, all
,
575 !!skipped_revs
.sha1_nr
);
578 static void exit_if_skipped_commits(struct commit_list
*tried
,
579 const unsigned char *bad
)
584 printf("There are only 'skip'ped commits left to test.\n"
585 "The first bad commit could be any of:\n");
586 print_commit_list(tried
, "%s\n", "%s\n");
588 printf("%s\n", sha1_to_hex(bad
));
589 printf("We cannot bisect more!\n");
593 static void mark_expected_rev(char *bisect_rev_hex
)
595 int len
= strlen(bisect_rev_hex
);
596 const char *filename
= git_path("BISECT_EXPECTED_REV");
597 int fd
= open(filename
, O_CREAT
| O_TRUNC
| O_WRONLY
, 0600);
600 die("could not create file '%s': %s",
601 filename
, strerror(errno
));
603 bisect_rev_hex
[len
] = '\n';
604 write_or_die(fd
, bisect_rev_hex
, len
+ 1);
605 bisect_rev_hex
[len
] = '\0';
608 die("closing file %s: %s", filename
, strerror(errno
));
611 static int bisect_checkout(char *bisect_rev_hex
)
615 mark_expected_rev(bisect_rev_hex
);
617 argv_checkout
[2] = bisect_rev_hex
;
618 res
= run_command_v_opt(argv_checkout
, RUN_GIT_CMD
);
622 argv_show_branch
[1] = bisect_rev_hex
;
623 return run_command_v_opt(argv_show_branch
, RUN_GIT_CMD
);
627 * We use the convention that exiting with an exit code 10 means that
628 * the bisection process finished successfully.
629 * In this case the calling shell script should exit 0.
631 int bisect_next_exit(const char *prefix
)
633 struct rev_info revs
;
634 struct commit_list
*tried
;
635 int reaches
= 0, all
= 0, nr
;
636 const unsigned char *bisect_rev
;
637 char bisect_rev_hex
[41];
639 bisect_common(&revs
, prefix
, &reaches
, &all
);
641 revs
.commits
= filter_skipped(revs
.commits
, &tried
, 0);
645 * We should exit here only if the "bad"
646 * commit is also a "skip" commit.
648 exit_if_skipped_commits(tried
, NULL
);
650 printf("%s was both good and bad\n",
651 sha1_to_hex(current_bad_sha1
));
655 bisect_rev
= revs
.commits
->item
->object
.sha1
;
656 memcpy(bisect_rev_hex
, sha1_to_hex(bisect_rev
), 41);
658 if (!hashcmp(bisect_rev
, current_bad_sha1
)) {
659 exit_if_skipped_commits(tried
, current_bad_sha1
);
660 printf("%s is first bad commit\n", bisect_rev_hex
);
661 argv_diff_tree
[2] = bisect_rev_hex
;
662 run_command_v_opt(argv_diff_tree
, RUN_GIT_CMD
);
663 /* This means the bisection process succeeded. */
667 nr
= all
- reaches
- 1;
668 printf("Bisecting: %d revisions left to test after this "
669 "(roughly %d steps)\n", nr
, estimate_bisect_steps(all
));
671 return bisect_checkout(bisect_rev_hex
);