Code

archive: clarify description of path parameter
[git.git] / bisect.c
index dbeb28752adce639d8729fa7cbb003fc31bd240f..dc18db8af96b093d7cb16eb88d3f738f98dc30f6 100644 (file)
--- a/bisect.c
+++ b/bisect.c
@@ -461,7 +461,7 @@ static void read_bisect_paths(struct argv_array *array)
        FILE *fp = fopen(filename, "r");
 
        if (!fp)
-               die("Could not open file '%s': %s", filename, strerror(errno));
+               die_errno("Could not open file '%s'", filename);
 
        while (strbuf_getline(&str, fp, '\n') != EOF) {
                char *quoted;
@@ -585,16 +585,49 @@ struct commit_list *filter_skipped(struct commit_list *list,
        return filtered;
 }
 
-static struct commit_list *apply_skip_ratio(struct commit_list *list,
-                                           int count,
-                                           int skip_num, int skip_denom)
+#define PRN_MODULO 32768
+
+/*
+ * This is a pseudo random number generator based on "man 3 rand".
+ * It is not used properly because the seed is the argument and it
+ * is increased by one between each call, but that should not matter
+ * for this application.
+ */
+int get_prn(int count) {
+       count = count * 1103515245 + 12345;
+       return ((unsigned)(count/65536) % PRN_MODULO);
+}
+
+/*
+ * Custom integer square root from
+ * http://en.wikipedia.org/wiki/Integer_square_root
+ */
+static int sqrti(int val)
+{
+       float d, x = val;
+
+       if (val == 0)
+               return 0;
+
+       do {
+               float y = (x + (float)val / x) / 2;
+               d = (y > x) ? y - x : x - y;
+               x = y;
+       } while (d >= 0.5);
+
+       return (int)x;
+}
+
+static struct commit_list *skip_away(struct commit_list *list, int count)
 {
-       int index, i;
        struct commit_list *cur, *previous;
+       int prn, index, i;
+
+       prn = get_prn(count);
+       index = (count * prn / PRN_MODULO) * sqrti(prn) / sqrti(PRN_MODULO);
 
        cur = list;
        previous = NULL;
-       index = count * skip_num / skip_denom;
 
        for (i = 0; cur; cur = cur->next, i++) {
                if (i == index) {
@@ -614,7 +647,6 @@ static struct commit_list *managed_skipped(struct commit_list *list,
                                           struct commit_list **tried)
 {
        int count, skipped_first;
-       int skip_num, skip_denom;
 
        *tried = NULL;
 
@@ -626,11 +658,7 @@ static struct commit_list *managed_skipped(struct commit_list *list,
        if (!skipped_first)
                return list;
 
-       /* Use alternatively 1/5, 2/5 and 3/5 as skip ratio. */
-       skip_num = count % 3 + 1;
-       skip_denom = 5;
-
-       return apply_skip_ratio(list, count, skip_num, skip_denom);
+       return skip_away(list, count);
 }
 
 static void bisect_rev_setup(struct rev_info *revs, const char *prefix,
@@ -712,8 +740,7 @@ static void mark_expected_rev(char *bisect_rev_hex)
        int fd = open(filename, O_CREAT | O_TRUNC | O_WRONLY, 0600);
 
        if (fd < 0)
-               die("could not create file '%s': %s",
-                   filename, strerror(errno));
+               die_errno("could not create file '%s'", filename);
 
        bisect_rev_hex[len] = '\n';
        write_or_die(fd, bisect_rev_hex, len + 1);
@@ -964,7 +991,7 @@ int bisect_next_all(const char *prefix)
 
        if (!hashcmp(bisect_rev, current_bad_sha1)) {
                exit_if_skipped_commits(tried, current_bad_sha1);
-               printf("%s is first bad commit\n", bisect_rev_hex);
+               printf("%s is the first bad commit\n", bisect_rev_hex);
                show_diff_tree(prefix, revs.commits->item);
                /* This means the bisection process succeeded. */
                exit(10);