6 #include "list-objects.h"
8 #include "sha1-lookup.h"
9 #include "run-command.h"
12 #include "sha1-array.h"
14 static struct sha1_array good_revs
;
15 static struct sha1_array skipped_revs
;
17 static const unsigned char *current_bad_sha1
;
25 static const char *argv_checkout
[] = {"checkout", "-q", NULL
, "--", NULL
};
26 static const char *argv_show_branch
[] = {"show-branch", NULL
, NULL
};
28 /* bits #0-15 in revision.h */
30 #define COUNTED (1u<<16)
33 * This is a truly stupid algorithm, but it's only
34 * used for bisection, and we just don't care enough.
36 * We care just barely enough to avoid recursing for
39 static int count_distance(struct commit_list
*entry
)
44 struct commit
*commit
= entry
->item
;
45 struct commit_list
*p
;
47 if (commit
->object
.flags
& (UNINTERESTING
| COUNTED
))
49 if (!(commit
->object
.flags
& TREESAME
))
51 commit
->object
.flags
|= COUNTED
;
57 nr
+= count_distance(p
);
66 static void clear_distance(struct commit_list
*list
)
69 struct commit
*commit
= list
->item
;
70 commit
->object
.flags
&= ~COUNTED
;
75 #define DEBUG_BISECT 0
77 static inline int weight(struct commit_list
*elem
)
79 return *((int*)(elem
->item
->util
));
82 static inline void weight_set(struct commit_list
*elem
, int weight
)
84 *((int*)(elem
->item
->util
)) = weight
;
87 static int count_interesting_parents(struct commit
*commit
)
89 struct commit_list
*p
;
92 for (count
= 0, p
= commit
->parents
; p
; p
= p
->next
) {
93 if (p
->item
->object
.flags
& UNINTERESTING
)
100 static inline int halfway(struct commit_list
*p
, int nr
)
103 * Don't short-cut something we are not going to return!
105 if (p
->item
->object
.flags
& TREESAME
)
110 * 2 and 3 are halfway of 5.
111 * 3 is halfway of 6 but 2 and 4 are not.
113 switch (2 * weight(p
) - nr
) {
114 case -1: case 0: case 1:
122 #define show_list(a,b,c,d) do { ; } while (0)
124 static void show_list(const char *debug
, int counted
, int nr
,
125 struct commit_list
*list
)
127 struct commit_list
*p
;
129 fprintf(stderr
, "%s (%d/%d)\n", debug
, counted
, nr
);
131 for (p
= list
; p
; p
= p
->next
) {
132 struct commit_list
*pp
;
133 struct commit
*commit
= p
->item
;
134 unsigned flags
= commit
->object
.flags
;
135 enum object_type type
;
137 char *buf
= read_sha1_file(commit
->object
.sha1
, &type
, &size
);
138 const char *subject_start
;
141 fprintf(stderr
, "%c%c%c ",
142 (flags
& TREESAME
) ? ' ' : 'T',
143 (flags
& UNINTERESTING
) ? 'U' : ' ',
144 (flags
& COUNTED
) ? 'C' : ' ');
146 fprintf(stderr
, "%3d", weight(p
));
148 fprintf(stderr
, "---");
149 fprintf(stderr
, " %.*s", 8, sha1_to_hex(commit
->object
.sha1
));
150 for (pp
= commit
->parents
; pp
; pp
= pp
->next
)
151 fprintf(stderr
, " %.*s", 8,
152 sha1_to_hex(pp
->item
->object
.sha1
));
154 subject_len
= find_commit_subject(buf
, &subject_start
);
156 fprintf(stderr
, " %.*s", subject_len
, subject_start
);
157 fprintf(stderr
, "\n");
160 #endif /* DEBUG_BISECT */
162 static struct commit_list
*best_bisection(struct commit_list
*list
, int nr
)
164 struct commit_list
*p
, *best
;
165 int best_distance
= -1;
168 for (p
= list
; p
; p
= p
->next
) {
170 unsigned flags
= p
->item
->object
.flags
;
172 if (flags
& TREESAME
)
174 distance
= weight(p
);
175 if (nr
- distance
< distance
)
176 distance
= nr
- distance
;
177 if (distance
> best_distance
) {
179 best_distance
= distance
;
187 struct commit
*commit
;
191 static int compare_commit_dist(const void *a_
, const void *b_
)
193 struct commit_dist
*a
, *b
;
195 a
= (struct commit_dist
*)a_
;
196 b
= (struct commit_dist
*)b_
;
197 if (a
->distance
!= b
->distance
)
198 return b
->distance
- a
->distance
; /* desc sort */
199 return hashcmp(a
->commit
->object
.sha1
, b
->commit
->object
.sha1
);
202 static struct commit_list
*best_bisection_sorted(struct commit_list
*list
, int nr
)
204 struct commit_list
*p
;
205 struct commit_dist
*array
= xcalloc(nr
, sizeof(*array
));
208 for (p
= list
, cnt
= 0; p
; p
= p
->next
) {
210 unsigned flags
= p
->item
->object
.flags
;
212 if (flags
& TREESAME
)
214 distance
= weight(p
);
215 if (nr
- distance
< distance
)
216 distance
= nr
- distance
;
217 array
[cnt
].commit
= p
->item
;
218 array
[cnt
].distance
= distance
;
221 qsort(array
, cnt
, sizeof(*array
), compare_commit_dist
);
222 for (p
= list
, i
= 0; i
< cnt
; i
++) {
223 struct name_decoration
*r
= xmalloc(sizeof(*r
) + 100);
224 struct object
*obj
= &(array
[i
].commit
->object
);
226 sprintf(r
->name
, "dist=%d", array
[i
].distance
);
227 r
->next
= add_decoration(&name_decoration
, obj
, r
);
228 p
->item
= array
[i
].commit
;
238 * zero or positive weight is the number of interesting commits it can
239 * reach, including itself. Especially, weight = 0 means it does not
240 * reach any tree-changing commits (e.g. just above uninteresting one
241 * but traversal is with pathspec).
243 * weight = -1 means it has one parent and its distance is yet to
246 * weight = -2 means it has more than one parent and its distance is
247 * unknown. After running count_distance() first, they will get zero
248 * or positive distance.
250 static struct commit_list
*do_find_bisection(struct commit_list
*list
,
251 int nr
, int *weights
,
255 struct commit_list
*p
;
259 for (n
= 0, p
= list
; p
; p
= p
->next
) {
260 struct commit
*commit
= p
->item
;
261 unsigned flags
= commit
->object
.flags
;
263 p
->item
->util
= &weights
[n
++];
264 switch (count_interesting_parents(commit
)) {
266 if (!(flags
& TREESAME
)) {
269 show_list("bisection 2 count one",
273 * otherwise, it is known not to reach any
274 * tree-changing commit and gets weight 0.
286 show_list("bisection 2 initialize", counted
, nr
, list
);
289 * If you have only one parent in the resulting set
290 * then you can reach one commit more than that parent
291 * can reach. So we do not have to run the expensive
292 * count_distance() for single strand of pearls.
294 * However, if you have more than one parents, you cannot
295 * just add their distance and one for yourself, since
296 * they usually reach the same ancestor and you would
297 * end up counting them twice that way.
299 * So we will first count distance of merges the usual
300 * way, and then fill the blanks using cheaper algorithm.
302 for (p
= list
; p
; p
= p
->next
) {
303 if (p
->item
->object
.flags
& UNINTERESTING
)
307 weight_set(p
, count_distance(p
));
308 clear_distance(list
);
310 /* Does it happen to be at exactly half-way? */
311 if (!find_all
&& halfway(p
, nr
))
316 show_list("bisection 2 count_distance", counted
, nr
, list
);
318 while (counted
< nr
) {
319 for (p
= list
; p
; p
= p
->next
) {
320 struct commit_list
*q
;
321 unsigned flags
= p
->item
->object
.flags
;
325 for (q
= p
->item
->parents
; q
; q
= q
->next
) {
326 if (q
->item
->object
.flags
& UNINTERESTING
)
335 * weight for p is unknown but q is known.
336 * add one for p itself if p is to be counted,
337 * otherwise inherit it from q directly.
339 if (!(flags
& TREESAME
)) {
340 weight_set(p
, weight(q
)+1);
342 show_list("bisection 2 count one",
346 weight_set(p
, weight(q
));
348 /* Does it happen to be at exactly half-way? */
349 if (!find_all
&& halfway(p
, nr
))
354 show_list("bisection 2 counted all", counted
, nr
, list
);
357 return best_bisection(list
, nr
);
359 return best_bisection_sorted(list
, nr
);
362 struct commit_list
*find_bisection(struct commit_list
*list
,
363 int *reaches
, int *all
,
367 struct commit_list
*p
, *best
, *next
, *last
;
370 show_list("bisection 2 entry", 0, 0, list
);
373 * Count the number of total and tree-changing items on the
374 * list, while reversing the list.
376 for (nr
= on_list
= 0, last
= NULL
, p
= list
;
379 unsigned flags
= p
->item
->object
.flags
;
382 if (flags
& UNINTERESTING
)
386 if (!(flags
& TREESAME
))
391 show_list("bisection 2 sorted", 0, nr
, list
);
394 weights
= xcalloc(on_list
, sizeof(*weights
));
396 /* Do the real work of finding bisection commit. */
397 best
= do_find_bisection(list
, nr
, weights
, find_all
);
401 *reaches
= weight(best
);
407 static void argv_array_push(struct argv_array
*array
, const char *string
)
409 ALLOC_GROW(array
->argv
, array
->argv_nr
+ 1, array
->argv_alloc
);
410 array
->argv
[array
->argv_nr
++] = string
;
413 static void argv_array_push_sha1(struct argv_array
*array
,
414 const unsigned char *sha1
,
417 struct strbuf buf
= STRBUF_INIT
;
418 strbuf_addf(&buf
, format
, sha1_to_hex(sha1
));
419 argv_array_push(array
, strbuf_detach(&buf
, NULL
));
422 static int register_ref(const char *refname
, const unsigned char *sha1
,
423 int flags
, void *cb_data
)
425 if (!strcmp(refname
, "bad")) {
426 current_bad_sha1
= sha1
;
427 } else if (!prefixcmp(refname
, "good-")) {
428 sha1_array_append(&good_revs
, sha1
);
429 } else if (!prefixcmp(refname
, "skip-")) {
430 sha1_array_append(&skipped_revs
, sha1
);
436 static int read_bisect_refs(void)
438 return for_each_ref_in("refs/bisect/", register_ref
, NULL
);
441 static void read_bisect_paths(struct argv_array
*array
)
443 struct strbuf str
= STRBUF_INIT
;
444 const char *filename
= git_path("BISECT_NAMES");
445 FILE *fp
= fopen(filename
, "r");
448 die_errno("Could not open file '%s'", filename
);
450 while (strbuf_getline(&str
, fp
, '\n') != EOF
) {
455 quoted
= strbuf_detach(&str
, NULL
);
456 res
= sq_dequote_to_argv(quoted
, &array
->argv
,
457 &array
->argv_nr
, &array
->argv_alloc
);
459 die("Badly quoted content in file '%s': %s",
463 strbuf_release(&str
);
467 static char *join_sha1_array_hex(struct sha1_array
*array
, char delim
)
469 struct strbuf joined_hexs
= STRBUF_INIT
;
472 for (i
= 0; i
< array
->nr
; i
++) {
473 strbuf_addstr(&joined_hexs
, sha1_to_hex(array
->sha1
[i
]));
474 if (i
+ 1 < array
->nr
)
475 strbuf_addch(&joined_hexs
, delim
);
478 return strbuf_detach(&joined_hexs
, NULL
);
482 * In this function, passing a not NULL skipped_first is very special.
483 * It means that we want to know if the first commit in the list is
484 * skipped because we will want to test a commit away from it if it is
486 * So if the first commit is skipped, we cannot take the shortcut to
487 * just "return list" when we find the first non skipped commit, we
488 * have to return a fully filtered list.
490 * We use (*skipped_first == -1) to mean "it has been found that the
491 * first commit is not skipped". In this case *skipped_first is set back
492 * to 0 just before the function returns.
494 struct commit_list
*filter_skipped(struct commit_list
*list
,
495 struct commit_list
**tried
,
500 struct commit_list
*filtered
= NULL
, **f
= &filtered
;
509 if (!skipped_revs
.nr
)
513 struct commit_list
*next
= list
->next
;
515 if (0 <= sha1_array_lookup(&skipped_revs
,
516 list
->item
->object
.sha1
)) {
517 if (skipped_first
&& !*skipped_first
)
519 /* Move current to tried list */
524 if (!skipped_first
|| !*skipped_first
)
526 } else if (skipped_first
&& !*skipped_first
) {
527 /* This means we know it's not skipped */
530 /* Move current to filtered list */
539 if (skipped_first
&& *skipped_first
== -1)
545 #define PRN_MODULO 32768
548 * This is a pseudo random number generator based on "man 3 rand".
549 * It is not used properly because the seed is the argument and it
550 * is increased by one between each call, but that should not matter
551 * for this application.
553 static int get_prn(int count
) {
554 count
= count
* 1103515245 + 12345;
555 return ((unsigned)(count
/65536) % PRN_MODULO
);
559 * Custom integer square root from
560 * http://en.wikipedia.org/wiki/Integer_square_root
562 static int sqrti(int val
)
570 float y
= (x
+ (float)val
/ x
) / 2;
571 d
= (y
> x
) ? y
- x
: x
- y
;
578 static struct commit_list
*skip_away(struct commit_list
*list
, int count
)
580 struct commit_list
*cur
, *previous
;
583 prn
= get_prn(count
);
584 index
= (count
* prn
/ PRN_MODULO
) * sqrti(prn
) / sqrti(PRN_MODULO
);
589 for (i
= 0; cur
; cur
= cur
->next
, i
++) {
591 if (hashcmp(cur
->item
->object
.sha1
, current_bad_sha1
))
603 static struct commit_list
*managed_skipped(struct commit_list
*list
,
604 struct commit_list
**tried
)
606 int count
, skipped_first
;
610 if (!skipped_revs
.nr
)
613 list
= filter_skipped(list
, tried
, 0, &count
, &skipped_first
);
618 return skip_away(list
, count
);
621 static void bisect_rev_setup(struct rev_info
*revs
, const char *prefix
,
622 const char *bad_format
, const char *good_format
,
625 struct argv_array rev_argv
= { NULL
, 0, 0 };
628 init_revisions(revs
, prefix
);
630 revs
->commit_format
= CMIT_FMT_UNSPECIFIED
;
632 /* rev_argv.argv[0] will be ignored by setup_revisions */
633 argv_array_push(&rev_argv
, xstrdup("bisect_rev_setup"));
634 argv_array_push_sha1(&rev_argv
, current_bad_sha1
, bad_format
);
635 for (i
= 0; i
< good_revs
.nr
; i
++)
636 argv_array_push_sha1(&rev_argv
, good_revs
.sha1
[i
],
638 argv_array_push(&rev_argv
, xstrdup("--"));
640 read_bisect_paths(&rev_argv
);
641 argv_array_push(&rev_argv
, NULL
);
643 setup_revisions(rev_argv
.argv_nr
, rev_argv
.argv
, revs
, NULL
);
646 static void bisect_common(struct rev_info
*revs
)
648 if (prepare_revision_walk(revs
))
649 die("revision walk setup failed");
650 if (revs
->tree_objects
)
651 mark_edges_uninteresting(revs
->commits
, revs
, NULL
);
654 static void exit_if_skipped_commits(struct commit_list
*tried
,
655 const unsigned char *bad
)
660 printf("There are only 'skip'ped commits left to test.\n"
661 "The first bad commit could be any of:\n");
662 print_commit_list(tried
, "%s\n", "%s\n");
664 printf("%s\n", sha1_to_hex(bad
));
665 printf("We cannot bisect more!\n");
669 static int is_expected_rev(const unsigned char *sha1
)
671 const char *filename
= git_path("BISECT_EXPECTED_REV");
673 struct strbuf str
= STRBUF_INIT
;
677 if (stat(filename
, &st
) || !S_ISREG(st
.st_mode
))
680 fp
= fopen(filename
, "r");
684 if (strbuf_getline(&str
, fp
, '\n') != EOF
)
685 res
= !strcmp(str
.buf
, sha1_to_hex(sha1
));
687 strbuf_release(&str
);
693 static void mark_expected_rev(char *bisect_rev_hex
)
695 int len
= strlen(bisect_rev_hex
);
696 const char *filename
= git_path("BISECT_EXPECTED_REV");
697 int fd
= open(filename
, O_CREAT
| O_TRUNC
| O_WRONLY
, 0600);
700 die_errno("could not create file '%s'", filename
);
702 bisect_rev_hex
[len
] = '\n';
703 write_or_die(fd
, bisect_rev_hex
, len
+ 1);
704 bisect_rev_hex
[len
] = '\0';
707 die("closing file %s: %s", filename
, strerror(errno
));
710 static int bisect_checkout(char *bisect_rev_hex
)
714 mark_expected_rev(bisect_rev_hex
);
716 argv_checkout
[2] = bisect_rev_hex
;
717 res
= run_command_v_opt(argv_checkout
, RUN_GIT_CMD
);
721 argv_show_branch
[1] = bisect_rev_hex
;
722 return run_command_v_opt(argv_show_branch
, RUN_GIT_CMD
);
725 static struct commit
*get_commit_reference(const unsigned char *sha1
)
727 struct commit
*r
= lookup_commit_reference(sha1
);
729 die("Not a valid commit name %s", sha1_to_hex(sha1
));
733 static struct commit
**get_bad_and_good_commits(int *rev_nr
)
735 int len
= 1 + good_revs
.nr
;
736 struct commit
**rev
= xmalloc(len
* sizeof(*rev
));
739 rev
[n
++] = get_commit_reference(current_bad_sha1
);
740 for (i
= 0; i
< good_revs
.nr
; i
++)
741 rev
[n
++] = get_commit_reference(good_revs
.sha1
[i
]);
747 static void handle_bad_merge_base(void)
749 if (is_expected_rev(current_bad_sha1
)) {
750 char *bad_hex
= sha1_to_hex(current_bad_sha1
);
751 char *good_hex
= join_sha1_array_hex(&good_revs
, ' ');
753 fprintf(stderr
, "The merge base %s is bad.\n"
754 "This means the bug has been fixed "
755 "between %s and [%s].\n",
756 bad_hex
, bad_hex
, good_hex
);
761 fprintf(stderr
, "Some good revs are not ancestor of the bad rev.\n"
762 "git bisect cannot work properly in this case.\n"
763 "Maybe you mistake good and bad revs?\n");
767 static void handle_skipped_merge_base(const unsigned char *mb
)
769 char *mb_hex
= sha1_to_hex(mb
);
770 char *bad_hex
= sha1_to_hex(current_bad_sha1
);
771 char *good_hex
= join_sha1_array_hex(&good_revs
, ' ');
773 warning("the merge base between %s and [%s] "
775 "So we cannot be sure the first bad commit is "
776 "between %s and %s.\n"
777 "We continue anyway.",
778 bad_hex
, good_hex
, mb_hex
, bad_hex
);
783 * "check_merge_bases" checks that merge bases are not "bad".
785 * - If one is "bad", it means the user assumed something wrong
786 * and we must exit with a non 0 error code.
787 * - If one is "good", that's good, we have nothing to do.
788 * - If one is "skipped", we can't know but we should warn.
789 * - If we don't know, we should check it out and ask the user to test.
791 static void check_merge_bases(void)
793 struct commit_list
*result
;
795 struct commit
**rev
= get_bad_and_good_commits(&rev_nr
);
797 result
= get_merge_bases_many(rev
[0], rev_nr
- 1, rev
+ 1, 0);
799 for (; result
; result
= result
->next
) {
800 const unsigned char *mb
= result
->item
->object
.sha1
;
801 if (!hashcmp(mb
, current_bad_sha1
)) {
802 handle_bad_merge_base();
803 } else if (0 <= sha1_array_lookup(&good_revs
, mb
)) {
805 } else if (0 <= sha1_array_lookup(&skipped_revs
, mb
)) {
806 handle_skipped_merge_base(mb
);
808 printf("Bisecting: a merge base must be tested\n");
809 exit(bisect_checkout(sha1_to_hex(mb
)));
814 free_commit_list(result
);
817 static int check_ancestors(const char *prefix
)
819 struct rev_info revs
;
820 struct object_array pending_copy
;
823 bisect_rev_setup(&revs
, prefix
, "^%s", "%s", 0);
825 /* Save pending objects, so they can be cleaned up later. */
826 pending_copy
= revs
.pending
;
827 revs
.leak_pending
= 1;
830 * bisect_common calls prepare_revision_walk right away, which
831 * (together with .leak_pending = 1) makes us the sole owner of
832 * the list of pending objects.
834 bisect_common(&revs
);
835 res
= (revs
.commits
!= NULL
);
837 /* Clean up objects used, as they will be reused. */
838 clear_commit_marks_for_object_array(&pending_copy
, ALL_REV_FLAGS
);
839 free(pending_copy
.objects
);
845 * "check_good_are_ancestors_of_bad" checks that all "good" revs are
846 * ancestor of the "bad" rev.
848 * If that's not the case, we need to check the merge bases.
849 * If a merge base must be tested by the user, its source code will be
850 * checked out to be tested by the user and we will exit.
852 static void check_good_are_ancestors_of_bad(const char *prefix
)
854 const char *filename
= git_path("BISECT_ANCESTORS_OK");
858 if (!current_bad_sha1
)
859 die("a bad revision is needed");
861 /* Check if file BISECT_ANCESTORS_OK exists. */
862 if (!stat(filename
, &st
) && S_ISREG(st
.st_mode
))
865 /* Bisecting with no good rev is ok. */
866 if (good_revs
.nr
== 0)
869 /* Check if all good revs are ancestor of the bad rev. */
870 if (check_ancestors(prefix
))
873 /* Create file BISECT_ANCESTORS_OK. */
874 fd
= open(filename
, O_CREAT
| O_TRUNC
| O_WRONLY
, 0600);
876 warning("could not create file '%s': %s",
877 filename
, strerror(errno
));
883 * This does "git diff-tree --pretty COMMIT" without one fork+exec.
885 static void show_diff_tree(const char *prefix
, struct commit
*commit
)
890 init_revisions(&opt
, prefix
);
891 git_config(git_diff_basic_config
, NULL
); /* no "diff" UI options */
895 /* This is what "--pretty" does */
896 opt
.verbose_header
= 1;
897 opt
.use_terminator
= 0;
898 opt
.commit_format
= CMIT_FMT_DEFAULT
;
901 if (!opt
.diffopt
.output_format
)
902 opt
.diffopt
.output_format
= DIFF_FORMAT_RAW
;
904 log_tree_commit(&opt
, commit
);
908 * We use the convention that exiting with an exit code 10 means that
909 * the bisection process finished successfully.
910 * In this case the calling shell script should exit 0.
912 int bisect_next_all(const char *prefix
)
914 struct rev_info revs
;
915 struct commit_list
*tried
;
916 int reaches
= 0, all
= 0, nr
, steps
;
917 const unsigned char *bisect_rev
;
918 char bisect_rev_hex
[41];
920 if (read_bisect_refs())
921 die("reading bisect refs failed");
923 check_good_are_ancestors_of_bad(prefix
);
925 bisect_rev_setup(&revs
, prefix
, "%s", "^%s", 1);
928 bisect_common(&revs
);
930 revs
.commits
= find_bisection(revs
.commits
, &reaches
, &all
,
932 revs
.commits
= managed_skipped(revs
.commits
, &tried
);
936 * We should exit here only if the "bad"
937 * commit is also a "skip" commit.
939 exit_if_skipped_commits(tried
, NULL
);
941 printf("%s was both good and bad\n",
942 sha1_to_hex(current_bad_sha1
));
947 fprintf(stderr
, "No testable commit found.\n"
948 "Maybe you started with bad path parameters?\n");
952 bisect_rev
= revs
.commits
->item
->object
.sha1
;
953 memcpy(bisect_rev_hex
, sha1_to_hex(bisect_rev
), 41);
955 if (!hashcmp(bisect_rev
, current_bad_sha1
)) {
956 exit_if_skipped_commits(tried
, current_bad_sha1
);
957 printf("%s is the first bad commit\n", bisect_rev_hex
);
958 show_diff_tree(prefix
, revs
.commits
->item
);
959 /* This means the bisection process succeeded. */
963 nr
= all
- reaches
- 1;
964 steps
= estimate_bisect_steps(all
);
965 printf("Bisecting: %d revision%s left to test after this "
966 "(roughly %d step%s)\n", nr
, (nr
== 1 ? "" : "s"),
967 steps
, (steps
== 1 ? "" : "s"));
969 return bisect_checkout(bisect_rev_hex
);