| Christian Couder | a2ad79c | 2009-03-26 04:55:24 | [diff] [blame] | 1 | #include "cache.h" |
| 2 | #include "commit.h" |
| 3 | #include "diff.h" |
| 4 | #include "revision.h" |
| Christian Couder | 1bf072e | 2009-03-26 04:55:54 | [diff] [blame] | 5 | #include "refs.h" |
| 6 | #include "list-objects.h" |
| Christian Couder | 3b437b0 | 2009-03-26 04:55:59 | [diff] [blame] | 7 | #include "quote.h" |
| Christian Couder | 4eb5b64 | 2009-04-04 20:59:36 | [diff] [blame] | 8 | #include "sha1-lookup.h" |
| Christian Couder | ef24c7c | 2009-04-19 09:56:07 | [diff] [blame] | 9 | #include "run-command.h" |
| Christian Couder | e22278c | 2009-05-28 21:21:16 | [diff] [blame] | 10 | #include "log-tree.h" |
| Christian Couder | a2ad79c | 2009-03-26 04:55:24 | [diff] [blame] | 11 | #include "bisect.h" |
| Jeff King | 902bb36 | 2011-05-19 21:34:33 | [diff] [blame] | 12 | #include "sha1-array.h" |
| Jeff King | 8a534b6 | 2011-09-13 21:58:14 | [diff] [blame] | 13 | #include "argv-array.h" |
| Christian Couder | 6212b1a | 2009-05-09 15:55:38 | [diff] [blame] | 14 | |
| Christian Couder | fad2d31 | 2009-05-09 15:55:40 | [diff] [blame] | 15 | static struct sha1_array good_revs; |
| Christian Couder | 6212b1a | 2009-05-09 15:55:38 | [diff] [blame] | 16 | static struct sha1_array skipped_revs; |
| Christian Couder | 1bf072e | 2009-03-26 04:55:54 | [diff] [blame] | 17 | |
| Michael Haggerty | bf42772 | 2013-05-25 09:08:23 | [diff] [blame] | 18 | static unsigned char *current_bad_sha1; |
| Christian Couder | ef24c7c | 2009-04-19 09:56:07 | [diff] [blame] | 19 | |
| Christian Couder | ef24c7c | 2009-04-19 09:56:07 | [diff] [blame] | 20 | static const char *argv_checkout[] = {"checkout", "-q", NULL, "--", NULL}; |
| 21 | static const char *argv_show_branch[] = {"show-branch", NULL, NULL}; |
| Jon Seymour | fee92fc | 2011-08-04 12:01:00 | [diff] [blame] | 22 | static const char *argv_update_ref[] = {"update-ref", "--no-deref", "BISECT_HEAD", NULL, NULL}; |
| Christian Couder | ef24c7c | 2009-04-19 09:56:07 | [diff] [blame] | 23 | |
| Nguyễn Thái Ngọc Duy | 208acbf | 2014-03-25 13:23:26 | [diff] [blame] | 24 | /* Remember to update object flag allocation in object.h */ |
| Christian Couder | a2ad79c | 2009-03-26 04:55:24 | [diff] [blame] | 25 | #define COUNTED (1u<<16) |
| 26 | |
| 27 | /* |
| 28 | * This is a truly stupid algorithm, but it's only |
| 29 | * used for bisection, and we just don't care enough. |
| 30 | * |
| 31 | * We care just barely enough to avoid recursing for |
| 32 | * non-merge entries. |
| 33 | */ |
| 34 | static int count_distance(struct commit_list *entry) |
| 35 | { |
| 36 | int nr = 0; |
| 37 | |
| 38 | while (entry) { |
| 39 | struct commit *commit = entry->item; |
| 40 | struct commit_list *p; |
| 41 | |
| 42 | if (commit->object.flags & (UNINTERESTING | COUNTED)) |
| 43 | break; |
| 44 | if (!(commit->object.flags & TREESAME)) |
| 45 | nr++; |
| 46 | commit->object.flags |= COUNTED; |
| 47 | p = commit->parents; |
| 48 | entry = p; |
| 49 | if (p) { |
| 50 | p = p->next; |
| 51 | while (p) { |
| 52 | nr += count_distance(p); |
| 53 | p = p->next; |
| 54 | } |
| 55 | } |
| 56 | } |
| 57 | |
| 58 | return nr; |
| 59 | } |
| 60 | |
| 61 | static void clear_distance(struct commit_list *list) |
| 62 | { |
| 63 | while (list) { |
| 64 | struct commit *commit = list->item; |
| 65 | commit->object.flags &= ~COUNTED; |
| 66 | list = list->next; |
| 67 | } |
| 68 | } |
| 69 | |
| 70 | #define DEBUG_BISECT 0 |
| 71 | |
| 72 | static inline int weight(struct commit_list *elem) |
| 73 | { |
| 74 | return *((int*)(elem->item->util)); |
| 75 | } |
| 76 | |
| 77 | static inline void weight_set(struct commit_list *elem, int weight) |
| 78 | { |
| 79 | *((int*)(elem->item->util)) = weight; |
| 80 | } |
| 81 | |
| 82 | static int count_interesting_parents(struct commit *commit) |
| 83 | { |
| 84 | struct commit_list *p; |
| 85 | int count; |
| 86 | |
| 87 | for (count = 0, p = commit->parents; p; p = p->next) { |
| 88 | if (p->item->object.flags & UNINTERESTING) |
| 89 | continue; |
| 90 | count++; |
| 91 | } |
| 92 | return count; |
| 93 | } |
| 94 | |
| 95 | static inline int halfway(struct commit_list *p, int nr) |
| 96 | { |
| 97 | /* |
| 98 | * Don't short-cut something we are not going to return! |
| 99 | */ |
| 100 | if (p->item->object.flags & TREESAME) |
| 101 | return 0; |
| 102 | if (DEBUG_BISECT) |
| 103 | return 0; |
| 104 | /* |
| 105 | * 2 and 3 are halfway of 5. |
| 106 | * 3 is halfway of 6 but 2 and 4 are not. |
| 107 | */ |
| 108 | switch (2 * weight(p) - nr) { |
| 109 | case -1: case 0: case 1: |
| 110 | return 1; |
| 111 | default: |
| 112 | return 0; |
| 113 | } |
| 114 | } |
| 115 | |
| 116 | #if !DEBUG_BISECT |
| 117 | #define show_list(a,b,c,d) do { ; } while (0) |
| 118 | #else |
| 119 | static void show_list(const char *debug, int counted, int nr, |
| 120 | struct commit_list *list) |
| 121 | { |
| 122 | struct commit_list *p; |
| 123 | |
| 124 | fprintf(stderr, "%s (%d/%d)\n", debug, counted, nr); |
| 125 | |
| 126 | for (p = list; p; p = p->next) { |
| 127 | struct commit_list *pp; |
| 128 | struct commit *commit = p->item; |
| 129 | unsigned flags = commit->object.flags; |
| 130 | enum object_type type; |
| 131 | unsigned long size; |
| 132 | char *buf = read_sha1_file(commit->object.sha1, &type, &size); |
| Christian Couder | 56ff379 | 2010-07-22 13:18:33 | [diff] [blame] | 133 | const char *subject_start; |
| 134 | int subject_len; |
| Christian Couder | a2ad79c | 2009-03-26 04:55:24 | [diff] [blame] | 135 | |
| 136 | fprintf(stderr, "%c%c%c ", |
| 137 | (flags & TREESAME) ? ' ' : 'T', |
| 138 | (flags & UNINTERESTING) ? 'U' : ' ', |
| 139 | (flags & COUNTED) ? 'C' : ' '); |
| 140 | if (commit->util) |
| 141 | fprintf(stderr, "%3d", weight(p)); |
| 142 | else |
| 143 | fprintf(stderr, "---"); |
| 144 | fprintf(stderr, " %.*s", 8, sha1_to_hex(commit->object.sha1)); |
| 145 | for (pp = commit->parents; pp; pp = pp->next) |
| 146 | fprintf(stderr, " %.*s", 8, |
| 147 | sha1_to_hex(pp->item->object.sha1)); |
| 148 | |
| Christian Couder | 56ff379 | 2010-07-22 13:18:33 | [diff] [blame] | 149 | subject_len = find_commit_subject(buf, &subject_start); |
| 150 | if (subject_len) |
| 151 | fprintf(stderr, " %.*s", subject_len, subject_start); |
| Christian Couder | a2ad79c | 2009-03-26 04:55:24 | [diff] [blame] | 152 | fprintf(stderr, "\n"); |
| 153 | } |
| 154 | } |
| 155 | #endif /* DEBUG_BISECT */ |
| 156 | |
| 157 | static struct commit_list *best_bisection(struct commit_list *list, int nr) |
| 158 | { |
| 159 | struct commit_list *p, *best; |
| 160 | int best_distance = -1; |
| 161 | |
| 162 | best = list; |
| 163 | for (p = list; p; p = p->next) { |
| 164 | int distance; |
| 165 | unsigned flags = p->item->object.flags; |
| 166 | |
| 167 | if (flags & TREESAME) |
| 168 | continue; |
| 169 | distance = weight(p); |
| 170 | if (nr - distance < distance) |
| 171 | distance = nr - distance; |
| 172 | if (distance > best_distance) { |
| 173 | best = p; |
| 174 | best_distance = distance; |
| 175 | } |
| 176 | } |
| 177 | |
| 178 | return best; |
| 179 | } |
| 180 | |
| 181 | struct commit_dist { |
| 182 | struct commit *commit; |
| 183 | int distance; |
| 184 | }; |
| 185 | |
| 186 | static int compare_commit_dist(const void *a_, const void *b_) |
| 187 | { |
| 188 | struct commit_dist *a, *b; |
| 189 | |
| 190 | a = (struct commit_dist *)a_; |
| 191 | b = (struct commit_dist *)b_; |
| 192 | if (a->distance != b->distance) |
| 193 | return b->distance - a->distance; /* desc sort */ |
| 194 | return hashcmp(a->commit->object.sha1, b->commit->object.sha1); |
| 195 | } |
| 196 | |
| 197 | static struct commit_list *best_bisection_sorted(struct commit_list *list, int nr) |
| 198 | { |
| 199 | struct commit_list *p; |
| 200 | struct commit_dist *array = xcalloc(nr, sizeof(*array)); |
| 201 | int cnt, i; |
| 202 | |
| 203 | for (p = list, cnt = 0; p; p = p->next) { |
| 204 | int distance; |
| 205 | unsigned flags = p->item->object.flags; |
| 206 | |
| 207 | if (flags & TREESAME) |
| 208 | continue; |
| 209 | distance = weight(p); |
| 210 | if (nr - distance < distance) |
| 211 | distance = nr - distance; |
| 212 | array[cnt].commit = p->item; |
| 213 | array[cnt].distance = distance; |
| 214 | cnt++; |
| 215 | } |
| 216 | qsort(array, cnt, sizeof(*array), compare_commit_dist); |
| 217 | for (p = list, i = 0; i < cnt; i++) { |
| Jeff King | 662174d | 2014-08-26 10:23:36 | [diff] [blame] | 218 | char buf[100]; /* enough for dist=%d */ |
| Christian Couder | a2ad79c | 2009-03-26 04:55:24 | [diff] [blame] | 219 | struct object *obj = &(array[i].commit->object); |
| 220 | |
| Jeff King | 662174d | 2014-08-26 10:23:36 | [diff] [blame] | 221 | snprintf(buf, sizeof(buf), "dist=%d", array[i].distance); |
| 222 | add_name_decoration(DECORATION_NONE, buf, obj); |
| 223 | |
| Christian Couder | a2ad79c | 2009-03-26 04:55:24 | [diff] [blame] | 224 | p->item = array[i].commit; |
| 225 | p = p->next; |
| 226 | } |
| 227 | if (p) |
| 228 | p->next = NULL; |
| 229 | free(array); |
| 230 | return list; |
| 231 | } |
| 232 | |
| 233 | /* |
| 234 | * zero or positive weight is the number of interesting commits it can |
| 235 | * reach, including itself. Especially, weight = 0 means it does not |
| 236 | * reach any tree-changing commits (e.g. just above uninteresting one |
| 237 | * but traversal is with pathspec). |
| 238 | * |
| 239 | * weight = -1 means it has one parent and its distance is yet to |
| 240 | * be computed. |
| 241 | * |
| 242 | * weight = -2 means it has more than one parent and its distance is |
| 243 | * unknown. After running count_distance() first, they will get zero |
| 244 | * or positive distance. |
| 245 | */ |
| 246 | static struct commit_list *do_find_bisection(struct commit_list *list, |
| 247 | int nr, int *weights, |
| 248 | int find_all) |
| 249 | { |
| 250 | int n, counted; |
| 251 | struct commit_list *p; |
| 252 | |
| 253 | counted = 0; |
| 254 | |
| 255 | for (n = 0, p = list; p; p = p->next) { |
| 256 | struct commit *commit = p->item; |
| 257 | unsigned flags = commit->object.flags; |
| 258 | |
| 259 | p->item->util = &weights[n++]; |
| 260 | switch (count_interesting_parents(commit)) { |
| 261 | case 0: |
| 262 | if (!(flags & TREESAME)) { |
| 263 | weight_set(p, 1); |
| 264 | counted++; |
| 265 | show_list("bisection 2 count one", |
| 266 | counted, nr, list); |
| 267 | } |
| 268 | /* |
| 269 | * otherwise, it is known not to reach any |
| 270 | * tree-changing commit and gets weight 0. |
| 271 | */ |
| 272 | break; |
| 273 | case 1: |
| 274 | weight_set(p, -1); |
| 275 | break; |
| 276 | default: |
| 277 | weight_set(p, -2); |
| 278 | break; |
| 279 | } |
| 280 | } |
| 281 | |
| 282 | show_list("bisection 2 initialize", counted, nr, list); |
| 283 | |
| 284 | /* |
| 285 | * If you have only one parent in the resulting set |
| 286 | * then you can reach one commit more than that parent |
| 287 | * can reach. So we do not have to run the expensive |
| 288 | * count_distance() for single strand of pearls. |
| 289 | * |
| 290 | * However, if you have more than one parents, you cannot |
| 291 | * just add their distance and one for yourself, since |
| 292 | * they usually reach the same ancestor and you would |
| 293 | * end up counting them twice that way. |
| 294 | * |
| 295 | * So we will first count distance of merges the usual |
| 296 | * way, and then fill the blanks using cheaper algorithm. |
| 297 | */ |
| 298 | for (p = list; p; p = p->next) { |
| 299 | if (p->item->object.flags & UNINTERESTING) |
| 300 | continue; |
| 301 | if (weight(p) != -2) |
| 302 | continue; |
| 303 | weight_set(p, count_distance(p)); |
| 304 | clear_distance(list); |
| 305 | |
| 306 | /* Does it happen to be at exactly half-way? */ |
| 307 | if (!find_all && halfway(p, nr)) |
| 308 | return p; |
| 309 | counted++; |
| 310 | } |
| 311 | |
| 312 | show_list("bisection 2 count_distance", counted, nr, list); |
| 313 | |
| 314 | while (counted < nr) { |
| 315 | for (p = list; p; p = p->next) { |
| 316 | struct commit_list *q; |
| 317 | unsigned flags = p->item->object.flags; |
| 318 | |
| 319 | if (0 <= weight(p)) |
| 320 | continue; |
| 321 | for (q = p->item->parents; q; q = q->next) { |
| 322 | if (q->item->object.flags & UNINTERESTING) |
| 323 | continue; |
| 324 | if (0 <= weight(q)) |
| 325 | break; |
| 326 | } |
| 327 | if (!q) |
| 328 | continue; |
| 329 | |
| 330 | /* |
| 331 | * weight for p is unknown but q is known. |
| 332 | * add one for p itself if p is to be counted, |
| 333 | * otherwise inherit it from q directly. |
| 334 | */ |
| 335 | if (!(flags & TREESAME)) { |
| 336 | weight_set(p, weight(q)+1); |
| 337 | counted++; |
| 338 | show_list("bisection 2 count one", |
| 339 | counted, nr, list); |
| 340 | } |
| 341 | else |
| 342 | weight_set(p, weight(q)); |
| 343 | |
| 344 | /* Does it happen to be at exactly half-way? */ |
| 345 | if (!find_all && halfway(p, nr)) |
| 346 | return p; |
| 347 | } |
| 348 | } |
| 349 | |
| 350 | show_list("bisection 2 counted all", counted, nr, list); |
| 351 | |
| 352 | if (!find_all) |
| 353 | return best_bisection(list, nr); |
| 354 | else |
| 355 | return best_bisection_sorted(list, nr); |
| 356 | } |
| 357 | |
| 358 | struct commit_list *find_bisection(struct commit_list *list, |
| 359 | int *reaches, int *all, |
| 360 | int find_all) |
| 361 | { |
| 362 | int nr, on_list; |
| 363 | struct commit_list *p, *best, *next, *last; |
| 364 | int *weights; |
| 365 | |
| 366 | show_list("bisection 2 entry", 0, 0, list); |
| 367 | |
| 368 | /* |
| 369 | * Count the number of total and tree-changing items on the |
| 370 | * list, while reversing the list. |
| 371 | */ |
| 372 | for (nr = on_list = 0, last = NULL, p = list; |
| 373 | p; |
| 374 | p = next) { |
| 375 | unsigned flags = p->item->object.flags; |
| 376 | |
| 377 | next = p->next; |
| 378 | if (flags & UNINTERESTING) |
| 379 | continue; |
| 380 | p->next = last; |
| 381 | last = p; |
| 382 | if (!(flags & TREESAME)) |
| 383 | nr++; |
| 384 | on_list++; |
| 385 | } |
| 386 | list = last; |
| 387 | show_list("bisection 2 sorted", 0, nr, list); |
| 388 | |
| 389 | *all = nr; |
| 390 | weights = xcalloc(on_list, sizeof(*weights)); |
| 391 | |
| 392 | /* Do the real work of finding bisection commit. */ |
| 393 | best = do_find_bisection(list, nr, weights, find_all); |
| 394 | if (best) { |
| 395 | if (!find_all) |
| 396 | best->next = NULL; |
| 397 | *reaches = weight(best); |
| 398 | } |
| 399 | free(weights); |
| 400 | return best; |
| 401 | } |
| 402 | |
| Christian Couder | 1bf072e | 2009-03-26 04:55:54 | [diff] [blame] | 403 | static int register_ref(const char *refname, const unsigned char *sha1, |
| 404 | int flags, void *cb_data) |
| 405 | { |
| 406 | if (!strcmp(refname, "bad")) { |
| Michael Haggerty | bf42772 | 2013-05-25 09:08:23 | [diff] [blame] | 407 | current_bad_sha1 = xmalloc(20); |
| 408 | hashcpy(current_bad_sha1, sha1); |
| Christian Couder | 5955654 | 2013-11-30 20:55:40 | [diff] [blame] | 409 | } else if (starts_with(refname, "good-")) { |
| Jeff King | 902bb36 | 2011-05-19 21:34:33 | [diff] [blame] | 410 | sha1_array_append(&good_revs, sha1); |
| Christian Couder | 5955654 | 2013-11-30 20:55:40 | [diff] [blame] | 411 | } else if (starts_with(refname, "skip-")) { |
| Jeff King | 902bb36 | 2011-05-19 21:34:33 | [diff] [blame] | 412 | sha1_array_append(&skipped_revs, sha1); |
| Christian Couder | 1bf072e | 2009-03-26 04:55:54 | [diff] [blame] | 413 | } |
| 414 | |
| 415 | return 0; |
| 416 | } |
| 417 | |
| 418 | static int read_bisect_refs(void) |
| 419 | { |
| 420 | return for_each_ref_in("refs/bisect/", register_ref, NULL); |
| 421 | } |
| 422 | |
| Linus Torvalds | 2af202b | 2009-06-18 17:28:43 | [diff] [blame] | 423 | static void read_bisect_paths(struct argv_array *array) |
| Christian Couder | 3b437b0 | 2009-03-26 04:55:59 | [diff] [blame] | 424 | { |
| 425 | struct strbuf str = STRBUF_INIT; |
| 426 | const char *filename = git_path("BISECT_NAMES"); |
| 427 | FILE *fp = fopen(filename, "r"); |
| 428 | |
| 429 | if (!fp) |
| Thomas Rast | d824cbb | 2009-06-27 15:58:46 | [diff] [blame] | 430 | die_errno("Could not open file '%s'", filename); |
| Christian Couder | 3b437b0 | 2009-03-26 04:55:59 | [diff] [blame] | 431 | |
| 432 | while (strbuf_getline(&str, fp, '\n') != EOF) { |
| Christian Couder | 3b437b0 | 2009-03-26 04:55:59 | [diff] [blame] | 433 | strbuf_trim(&str); |
| Jeff King | 8a534b6 | 2011-09-13 21:58:14 | [diff] [blame] | 434 | if (sq_dequote_to_argv_array(str.buf, array)) |
| Christian Couder | 3b437b0 | 2009-03-26 04:55:59 | [diff] [blame] | 435 | die("Badly quoted content in file '%s': %s", |
| Jeff King | 8a534b6 | 2011-09-13 21:58:14 | [diff] [blame] | 436 | filename, str.buf); |
| Christian Couder | 3b437b0 | 2009-03-26 04:55:59 | [diff] [blame] | 437 | } |
| 438 | |
| 439 | strbuf_release(&str); |
| 440 | fclose(fp); |
| 441 | } |
| 442 | |
| Christian Couder | c053766 | 2009-05-09 15:55:45 | [diff] [blame] | 443 | static char *join_sha1_array_hex(struct sha1_array *array, char delim) |
| 444 | { |
| 445 | struct strbuf joined_hexs = STRBUF_INIT; |
| 446 | int i; |
| 447 | |
| Jeff King | 902bb36 | 2011-05-19 21:34:33 | [diff] [blame] | 448 | for (i = 0; i < array->nr; i++) { |
| Christian Couder | c053766 | 2009-05-09 15:55:45 | [diff] [blame] | 449 | strbuf_addstr(&joined_hexs, sha1_to_hex(array->sha1[i])); |
| Jeff King | 902bb36 | 2011-05-19 21:34:33 | [diff] [blame] | 450 | if (i + 1 < array->nr) |
| Christian Couder | c053766 | 2009-05-09 15:55:45 | [diff] [blame] | 451 | strbuf_addch(&joined_hexs, delim); |
| 452 | } |
| 453 | |
| 454 | return strbuf_detach(&joined_hexs, NULL); |
| 455 | } |
| 456 | |
| Christian Couder | 9af3589 | 2009-06-06 04:41:33 | [diff] [blame] | 457 | /* |
| 458 | * In this function, passing a not NULL skipped_first is very special. |
| 459 | * It means that we want to know if the first commit in the list is |
| 460 | * skipped because we will want to test a commit away from it if it is |
| 461 | * indeed skipped. |
| 462 | * So if the first commit is skipped, we cannot take the shortcut to |
| 463 | * just "return list" when we find the first non skipped commit, we |
| 464 | * have to return a fully filtered list. |
| 465 | * |
| 466 | * We use (*skipped_first == -1) to mean "it has been found that the |
| 467 | * first commit is not skipped". In this case *skipped_first is set back |
| 468 | * to 0 just before the function returns. |
| 469 | */ |
| Christian Couder | 9518864 | 2009-03-26 04:55:49 | [diff] [blame] | 470 | struct commit_list *filter_skipped(struct commit_list *list, |
| 471 | struct commit_list **tried, |
| Christian Couder | 9af3589 | 2009-06-06 04:41:33 | [diff] [blame] | 472 | int show_all, |
| 473 | int *count, |
| 474 | int *skipped_first) |
| Christian Couder | 9518864 | 2009-03-26 04:55:49 | [diff] [blame] | 475 | { |
| 476 | struct commit_list *filtered = NULL, **f = &filtered; |
| 477 | |
| 478 | *tried = NULL; |
| 479 | |
| Christian Couder | 9af3589 | 2009-06-06 04:41:33 | [diff] [blame] | 480 | if (skipped_first) |
| 481 | *skipped_first = 0; |
| 482 | if (count) |
| 483 | *count = 0; |
| 484 | |
| Jeff King | 902bb36 | 2011-05-19 21:34:33 | [diff] [blame] | 485 | if (!skipped_revs.nr) |
| Christian Couder | 9518864 | 2009-03-26 04:55:49 | [diff] [blame] | 486 | return list; |
| 487 | |
| Christian Couder | 9518864 | 2009-03-26 04:55:49 | [diff] [blame] | 488 | while (list) { |
| 489 | struct commit_list *next = list->next; |
| 490 | list->next = NULL; |
| Jeff King | 902bb36 | 2011-05-19 21:34:33 | [diff] [blame] | 491 | if (0 <= sha1_array_lookup(&skipped_revs, |
| Christian Couder | aaaff9e | 2009-05-09 15:55:43 | [diff] [blame] | 492 | list->item->object.sha1)) { |
| Christian Couder | 9af3589 | 2009-06-06 04:41:33 | [diff] [blame] | 493 | if (skipped_first && !*skipped_first) |
| 494 | *skipped_first = 1; |
| Christian Couder | 9518864 | 2009-03-26 04:55:49 | [diff] [blame] | 495 | /* Move current to tried list */ |
| 496 | *tried = list; |
| 497 | tried = &list->next; |
| 498 | } else { |
| Christian Couder | 9af3589 | 2009-06-06 04:41:33 | [diff] [blame] | 499 | if (!show_all) { |
| 500 | if (!skipped_first || !*skipped_first) |
| 501 | return list; |
| 502 | } else if (skipped_first && !*skipped_first) { |
| 503 | /* This means we know it's not skipped */ |
| 504 | *skipped_first = -1; |
| 505 | } |
| Christian Couder | 9518864 | 2009-03-26 04:55:49 | [diff] [blame] | 506 | /* Move current to filtered list */ |
| 507 | *f = list; |
| 508 | f = &list->next; |
| Christian Couder | 9af3589 | 2009-06-06 04:41:33 | [diff] [blame] | 509 | if (count) |
| 510 | (*count)++; |
| Christian Couder | 9518864 | 2009-03-26 04:55:49 | [diff] [blame] | 511 | } |
| 512 | list = next; |
| 513 | } |
| 514 | |
| Christian Couder | 9af3589 | 2009-06-06 04:41:33 | [diff] [blame] | 515 | if (skipped_first && *skipped_first == -1) |
| 516 | *skipped_first = 0; |
| 517 | |
| Christian Couder | 9518864 | 2009-03-26 04:55:49 | [diff] [blame] | 518 | return filtered; |
| 519 | } |
| Christian Couder | 1bf072e | 2009-03-26 04:55:54 | [diff] [blame] | 520 | |
| Christian Couder | ebc9529 | 2009-06-13 05:21:06 | [diff] [blame] | 521 | #define PRN_MODULO 32768 |
| 522 | |
| 523 | /* |
| 524 | * This is a pseudo random number generator based on "man 3 rand". |
| 525 | * It is not used properly because the seed is the argument and it |
| 526 | * is increased by one between each call, but that should not matter |
| 527 | * for this application. |
| 528 | */ |
| John Keeping | 7b96d88 | 2013-04-03 19:17:55 | [diff] [blame] | 529 | static unsigned get_prn(unsigned count) { |
| Christian Couder | ebc9529 | 2009-06-13 05:21:06 | [diff] [blame] | 530 | count = count * 1103515245 + 12345; |
| John Keeping | 7b96d88 | 2013-04-03 19:17:55 | [diff] [blame] | 531 | return (count/65536) % PRN_MODULO; |
| Christian Couder | ebc9529 | 2009-06-13 05:21:06 | [diff] [blame] | 532 | } |
| 533 | |
| 534 | /* |
| 535 | * Custom integer square root from |
| 536 | * http://en.wikipedia.org/wiki/Integer_square_root |
| 537 | */ |
| 538 | static int sqrti(int val) |
| Christian Couder | 62d0b0d | 2009-06-06 04:41:34 | [diff] [blame] | 539 | { |
| Christian Couder | ebc9529 | 2009-06-13 05:21:06 | [diff] [blame] | 540 | float d, x = val; |
| 541 | |
| 542 | if (val == 0) |
| 543 | return 0; |
| 544 | |
| 545 | do { |
| 546 | float y = (x + (float)val / x) / 2; |
| 547 | d = (y > x) ? y - x : x - y; |
| 548 | x = y; |
| 549 | } while (d >= 0.5); |
| 550 | |
| 551 | return (int)x; |
| 552 | } |
| 553 | |
| 554 | static struct commit_list *skip_away(struct commit_list *list, int count) |
| 555 | { |
| Christian Couder | 62d0b0d | 2009-06-06 04:41:34 | [diff] [blame] | 556 | struct commit_list *cur, *previous; |
| Christian Couder | ebc9529 | 2009-06-13 05:21:06 | [diff] [blame] | 557 | int prn, index, i; |
| 558 | |
| 559 | prn = get_prn(count); |
| 560 | index = (count * prn / PRN_MODULO) * sqrti(prn) / sqrti(PRN_MODULO); |
| Christian Couder | 62d0b0d | 2009-06-06 04:41:34 | [diff] [blame] | 561 | |
| 562 | cur = list; |
| 563 | previous = NULL; |
| Christian Couder | 62d0b0d | 2009-06-06 04:41:34 | [diff] [blame] | 564 | |
| 565 | for (i = 0; cur; cur = cur->next, i++) { |
| 566 | if (i == index) { |
| 567 | if (hashcmp(cur->item->object.sha1, current_bad_sha1)) |
| 568 | return cur; |
| 569 | if (previous) |
| 570 | return previous; |
| 571 | return list; |
| 572 | } |
| 573 | previous = cur; |
| 574 | } |
| 575 | |
| 576 | return list; |
| 577 | } |
| 578 | |
| 579 | static struct commit_list *managed_skipped(struct commit_list *list, |
| 580 | struct commit_list **tried) |
| 581 | { |
| 582 | int count, skipped_first; |
| Christian Couder | 62d0b0d | 2009-06-06 04:41:34 | [diff] [blame] | 583 | |
| 584 | *tried = NULL; |
| 585 | |
| Jeff King | 902bb36 | 2011-05-19 21:34:33 | [diff] [blame] | 586 | if (!skipped_revs.nr) |
| Christian Couder | 62d0b0d | 2009-06-06 04:41:34 | [diff] [blame] | 587 | return list; |
| 588 | |
| 589 | list = filter_skipped(list, tried, 0, &count, &skipped_first); |
| 590 | |
| 591 | if (!skipped_first) |
| 592 | return list; |
| 593 | |
| Christian Couder | ebc9529 | 2009-06-13 05:21:06 | [diff] [blame] | 594 | return skip_away(list, count); |
| Christian Couder | 62d0b0d | 2009-06-06 04:41:34 | [diff] [blame] | 595 | } |
| 596 | |
| Christian Couder | a22347c | 2009-05-17 15:36:44 | [diff] [blame] | 597 | static void bisect_rev_setup(struct rev_info *revs, const char *prefix, |
| 598 | const char *bad_format, const char *good_format, |
| 599 | int read_paths) |
| Christian Couder | 1bf072e | 2009-03-26 04:55:54 | [diff] [blame] | 600 | { |
| Jeff King | 8a534b6 | 2011-09-13 21:58:14 | [diff] [blame] | 601 | struct argv_array rev_argv = ARGV_ARRAY_INIT; |
| Christian Couder | fad2d31 | 2009-05-09 15:55:40 | [diff] [blame] | 602 | int i; |
| 603 | |
| Christian Couder | 3b437b0 | 2009-03-26 04:55:59 | [diff] [blame] | 604 | init_revisions(revs, prefix); |
| 605 | revs->abbrev = 0; |
| 606 | revs->commit_format = CMIT_FMT_UNSPECIFIED; |
| Christian Couder | 1bf072e | 2009-03-26 04:55:54 | [diff] [blame] | 607 | |
| Christian Couder | 1c953a1 | 2009-05-09 15:55:41 | [diff] [blame] | 608 | /* rev_argv.argv[0] will be ignored by setup_revisions */ |
| Jeff King | 8a534b6 | 2011-09-13 21:58:14 | [diff] [blame] | 609 | argv_array_push(&rev_argv, "bisect_rev_setup"); |
| 610 | argv_array_pushf(&rev_argv, bad_format, sha1_to_hex(current_bad_sha1)); |
| Jeff King | 902bb36 | 2011-05-19 21:34:33 | [diff] [blame] | 611 | for (i = 0; i < good_revs.nr; i++) |
| Jeff King | 8a534b6 | 2011-09-13 21:58:14 | [diff] [blame] | 612 | argv_array_pushf(&rev_argv, good_format, |
| 613 | sha1_to_hex(good_revs.sha1[i])); |
| 614 | argv_array_push(&rev_argv, "--"); |
| Christian Couder | a22347c | 2009-05-17 15:36:44 | [diff] [blame] | 615 | if (read_paths) |
| 616 | read_bisect_paths(&rev_argv); |
| Christian Couder | 1bf072e | 2009-03-26 04:55:54 | [diff] [blame] | 617 | |
| Jeff King | 8a534b6 | 2011-09-13 21:58:14 | [diff] [blame] | 618 | setup_revisions(rev_argv.argc, rev_argv.argv, revs, NULL); |
| 619 | /* XXX leak rev_argv, as "revs" may still be pointing to it */ |
| Christian Couder | 3b437b0 | 2009-03-26 04:55:59 | [diff] [blame] | 620 | } |
| 621 | |
| Christian Couder | a22347c | 2009-05-17 15:36:44 | [diff] [blame] | 622 | static void bisect_common(struct rev_info *revs) |
| Christian Couder | 2ace972 | 2009-04-19 09:55:57 | [diff] [blame] | 623 | { |
| Christian Couder | 2ace972 | 2009-04-19 09:55:57 | [diff] [blame] | 624 | if (prepare_revision_walk(revs)) |
| 625 | die("revision walk setup failed"); |
| 626 | if (revs->tree_objects) |
| Nguyễn Thái Ngọc Duy | e76a5fb | 2013-08-16 09:52:06 | [diff] [blame] | 627 | mark_edges_uninteresting(revs, NULL); |
| Christian Couder | 2ace972 | 2009-04-19 09:55:57 | [diff] [blame] | 628 | } |
| 629 | |
| Christian Couder | ef24c7c | 2009-04-19 09:56:07 | [diff] [blame] | 630 | static void exit_if_skipped_commits(struct commit_list *tried, |
| 631 | const unsigned char *bad) |
| 632 | { |
| 633 | if (!tried) |
| 634 | return; |
| 635 | |
| 636 | printf("There are only 'skip'ped commits left to test.\n" |
| 637 | "The first bad commit could be any of:\n"); |
| 638 | print_commit_list(tried, "%s\n", "%s\n"); |
| 639 | if (bad) |
| 640 | printf("%s\n", sha1_to_hex(bad)); |
| 641 | printf("We cannot bisect more!\n"); |
| 642 | exit(2); |
| 643 | } |
| 644 | |
| Christian Couder | c053766 | 2009-05-09 15:55:45 | [diff] [blame] | 645 | static int is_expected_rev(const unsigned char *sha1) |
| 646 | { |
| 647 | const char *filename = git_path("BISECT_EXPECTED_REV"); |
| 648 | struct stat st; |
| 649 | struct strbuf str = STRBUF_INIT; |
| 650 | FILE *fp; |
| 651 | int res = 0; |
| 652 | |
| 653 | if (stat(filename, &st) || !S_ISREG(st.st_mode)) |
| 654 | return 0; |
| 655 | |
| 656 | fp = fopen(filename, "r"); |
| 657 | if (!fp) |
| 658 | return 0; |
| 659 | |
| 660 | if (strbuf_getline(&str, fp, '\n') != EOF) |
| 661 | res = !strcmp(str.buf, sha1_to_hex(sha1)); |
| 662 | |
| 663 | strbuf_release(&str); |
| 664 | fclose(fp); |
| 665 | |
| 666 | return res; |
| 667 | } |
| 668 | |
| Christian Couder | ef24c7c | 2009-04-19 09:56:07 | [diff] [blame] | 669 | static void mark_expected_rev(char *bisect_rev_hex) |
| 670 | { |
| 671 | int len = strlen(bisect_rev_hex); |
| 672 | const char *filename = git_path("BISECT_EXPECTED_REV"); |
| 673 | int fd = open(filename, O_CREAT | O_TRUNC | O_WRONLY, 0600); |
| 674 | |
| 675 | if (fd < 0) |
| Thomas Rast | d824cbb | 2009-06-27 15:58:46 | [diff] [blame] | 676 | die_errno("could not create file '%s'", filename); |
| Christian Couder | ef24c7c | 2009-04-19 09:56:07 | [diff] [blame] | 677 | |
| 678 | bisect_rev_hex[len] = '\n'; |
| 679 | write_or_die(fd, bisect_rev_hex, len + 1); |
| 680 | bisect_rev_hex[len] = '\0'; |
| 681 | |
| 682 | if (close(fd) < 0) |
| 683 | die("closing file %s: %s", filename, strerror(errno)); |
| 684 | } |
| 685 | |
| Jon Seymour | fee92fc | 2011-08-04 12:01:00 | [diff] [blame] | 686 | static int bisect_checkout(char *bisect_rev_hex, int no_checkout) |
| Christian Couder | ef24c7c | 2009-04-19 09:56:07 | [diff] [blame] | 687 | { |
| Christian Couder | ef24c7c | 2009-04-19 09:56:07 | [diff] [blame] | 688 | |
| 689 | mark_expected_rev(bisect_rev_hex); |
| 690 | |
| 691 | argv_checkout[2] = bisect_rev_hex; |
| Jon Seymour | fee92fc | 2011-08-04 12:01:00 | [diff] [blame] | 692 | if (no_checkout) { |
| 693 | argv_update_ref[3] = bisect_rev_hex; |
| 694 | if (run_command_v_opt(argv_update_ref, RUN_GIT_CMD)) |
| 695 | die("update-ref --no-deref HEAD failed on %s", |
| 696 | bisect_rev_hex); |
| 697 | } else { |
| Elia Pinto | 4824d1b | 2014-01-29 13:25:00 | [diff] [blame] | 698 | int res; |
| Jon Seymour | fee92fc | 2011-08-04 12:01:00 | [diff] [blame] | 699 | res = run_command_v_opt(argv_checkout, RUN_GIT_CMD); |
| 700 | if (res) |
| 701 | exit(res); |
| 702 | } |
| Christian Couder | ef24c7c | 2009-04-19 09:56:07 | [diff] [blame] | 703 | |
| 704 | argv_show_branch[1] = bisect_rev_hex; |
| 705 | return run_command_v_opt(argv_show_branch, RUN_GIT_CMD); |
| 706 | } |
| 707 | |
| Christian Couder | c053766 | 2009-05-09 15:55:45 | [diff] [blame] | 708 | static struct commit *get_commit_reference(const unsigned char *sha1) |
| 709 | { |
| 710 | struct commit *r = lookup_commit_reference(sha1); |
| 711 | if (!r) |
| 712 | die("Not a valid commit name %s", sha1_to_hex(sha1)); |
| 713 | return r; |
| 714 | } |
| 715 | |
| 716 | static struct commit **get_bad_and_good_commits(int *rev_nr) |
| 717 | { |
| Jeff King | 902bb36 | 2011-05-19 21:34:33 | [diff] [blame] | 718 | int len = 1 + good_revs.nr; |
| Christian Couder | c053766 | 2009-05-09 15:55:45 | [diff] [blame] | 719 | struct commit **rev = xmalloc(len * sizeof(*rev)); |
| 720 | int i, n = 0; |
| 721 | |
| 722 | rev[n++] = get_commit_reference(current_bad_sha1); |
| Jeff King | 902bb36 | 2011-05-19 21:34:33 | [diff] [blame] | 723 | for (i = 0; i < good_revs.nr; i++) |
| Christian Couder | c053766 | 2009-05-09 15:55:45 | [diff] [blame] | 724 | rev[n++] = get_commit_reference(good_revs.sha1[i]); |
| 725 | *rev_nr = n; |
| 726 | |
| 727 | return rev; |
| 728 | } |
| 729 | |
| 730 | static void handle_bad_merge_base(void) |
| 731 | { |
| 732 | if (is_expected_rev(current_bad_sha1)) { |
| 733 | char *bad_hex = sha1_to_hex(current_bad_sha1); |
| 734 | char *good_hex = join_sha1_array_hex(&good_revs, ' '); |
| 735 | |
| 736 | fprintf(stderr, "The merge base %s is bad.\n" |
| 737 | "This means the bug has been fixed " |
| 738 | "between %s and [%s].\n", |
| 739 | bad_hex, bad_hex, good_hex); |
| 740 | |
| 741 | exit(3); |
| 742 | } |
| 743 | |
| 744 | fprintf(stderr, "Some good revs are not ancestor of the bad rev.\n" |
| 745 | "git bisect cannot work properly in this case.\n" |
| 746 | "Maybe you mistake good and bad revs?\n"); |
| 747 | exit(1); |
| 748 | } |
| 749 | |
| Linus Torvalds | 2af202b | 2009-06-18 17:28:43 | [diff] [blame] | 750 | static void handle_skipped_merge_base(const unsigned char *mb) |
| Christian Couder | c053766 | 2009-05-09 15:55:45 | [diff] [blame] | 751 | { |
| 752 | char *mb_hex = sha1_to_hex(mb); |
| 753 | char *bad_hex = sha1_to_hex(current_bad_sha1); |
| 754 | char *good_hex = join_sha1_array_hex(&good_revs, ' '); |
| 755 | |
| Thiago Farina | bd757c1 | 2010-01-03 16:20:30 | [diff] [blame] | 756 | warning("the merge base between %s and [%s] " |
| Christian Couder | c053766 | 2009-05-09 15:55:45 | [diff] [blame] | 757 | "must be skipped.\n" |
| 758 | "So we cannot be sure the first bad commit is " |
| 759 | "between %s and %s.\n" |
| Thiago Farina | bd757c1 | 2010-01-03 16:20:30 | [diff] [blame] | 760 | "We continue anyway.", |
| Christian Couder | c053766 | 2009-05-09 15:55:45 | [diff] [blame] | 761 | bad_hex, good_hex, mb_hex, bad_hex); |
| 762 | free(good_hex); |
| 763 | } |
| 764 | |
| 765 | /* |
| 766 | * "check_merge_bases" checks that merge bases are not "bad". |
| 767 | * |
| 768 | * - If one is "bad", it means the user assumed something wrong |
| 769 | * and we must exit with a non 0 error code. |
| 770 | * - If one is "good", that's good, we have nothing to do. |
| 771 | * - If one is "skipped", we can't know but we should warn. |
| 772 | * - If we don't know, we should check it out and ask the user to test. |
| 773 | */ |
| Jon Seymour | fee92fc | 2011-08-04 12:01:00 | [diff] [blame] | 774 | static void check_merge_bases(int no_checkout) |
| Christian Couder | c053766 | 2009-05-09 15:55:45 | [diff] [blame] | 775 | { |
| 776 | struct commit_list *result; |
| 777 | int rev_nr; |
| 778 | struct commit **rev = get_bad_and_good_commits(&rev_nr); |
| 779 | |
| Junio C Hamano | 2ce406c | 2014-10-30 19:20:44 | [diff] [blame] | 780 | result = get_merge_bases_many(rev[0], rev_nr - 1, rev + 1); |
| Christian Couder | c053766 | 2009-05-09 15:55:45 | [diff] [blame] | 781 | |
| 782 | for (; result; result = result->next) { |
| 783 | const unsigned char *mb = result->item->object.sha1; |
| 784 | if (!hashcmp(mb, current_bad_sha1)) { |
| 785 | handle_bad_merge_base(); |
| Jeff King | 902bb36 | 2011-05-19 21:34:33 | [diff] [blame] | 786 | } else if (0 <= sha1_array_lookup(&good_revs, mb)) { |
| Christian Couder | c053766 | 2009-05-09 15:55:45 | [diff] [blame] | 787 | continue; |
| Jeff King | 902bb36 | 2011-05-19 21:34:33 | [diff] [blame] | 788 | } else if (0 <= sha1_array_lookup(&skipped_revs, mb)) { |
| Christian Couder | c053766 | 2009-05-09 15:55:45 | [diff] [blame] | 789 | handle_skipped_merge_base(mb); |
| 790 | } else { |
| 791 | printf("Bisecting: a merge base must be tested\n"); |
| Jon Seymour | fee92fc | 2011-08-04 12:01:00 | [diff] [blame] | 792 | exit(bisect_checkout(sha1_to_hex(mb), no_checkout)); |
| Christian Couder | c053766 | 2009-05-09 15:55:45 | [diff] [blame] | 793 | } |
| 794 | } |
| 795 | |
| 796 | free(rev); |
| 797 | free_commit_list(result); |
| 798 | } |
| 799 | |
| Christian Couder | 2d938fc | 2009-05-17 15:36:46 | [diff] [blame] | 800 | static int check_ancestors(const char *prefix) |
| Christian Couder | d937d4a | 2009-05-09 15:55:46 | [diff] [blame] | 801 | { |
| Christian Couder | 2d938fc | 2009-05-17 15:36:46 | [diff] [blame] | 802 | struct rev_info revs; |
| 803 | struct object_array pending_copy; |
| René Scharfe | 86a0a40 | 2011-10-01 16:16:08 | [diff] [blame] | 804 | int res; |
| Christian Couder | d937d4a | 2009-05-09 15:55:46 | [diff] [blame] | 805 | |
| Christian Couder | 2d938fc | 2009-05-17 15:36:46 | [diff] [blame] | 806 | bisect_rev_setup(&revs, prefix, "^%s", "%s", 0); |
| Christian Couder | d937d4a | 2009-05-09 15:55:46 | [diff] [blame] | 807 | |
| Christian Couder | 2d938fc | 2009-05-17 15:36:46 | [diff] [blame] | 808 | /* Save pending objects, so they can be cleaned up later. */ |
| René Scharfe | 353f565 | 2011-10-01 16:01:12 | [diff] [blame] | 809 | pending_copy = revs.pending; |
| 810 | revs.leak_pending = 1; |
| Christian Couder | 2d938fc | 2009-05-17 15:36:46 | [diff] [blame] | 811 | |
| René Scharfe | 353f565 | 2011-10-01 16:01:12 | [diff] [blame] | 812 | /* |
| 813 | * bisect_common calls prepare_revision_walk right away, which |
| 814 | * (together with .leak_pending = 1) makes us the sole owner of |
| 815 | * the list of pending objects. |
| 816 | */ |
| Christian Couder | 2d938fc | 2009-05-17 15:36:46 | [diff] [blame] | 817 | bisect_common(&revs); |
| 818 | res = (revs.commits != NULL); |
| 819 | |
| 820 | /* Clean up objects used, as they will be reused. */ |
| René Scharfe | 86a0a40 | 2011-10-01 16:16:08 | [diff] [blame] | 821 | clear_commit_marks_for_object_array(&pending_copy, ALL_REV_FLAGS); |
| René Scharfe | 353f565 | 2011-10-01 16:01:12 | [diff] [blame] | 822 | free(pending_copy.objects); |
| Christian Couder | d937d4a | 2009-05-09 15:55:46 | [diff] [blame] | 823 | |
| Christian Couder | 2d938fc | 2009-05-17 15:36:46 | [diff] [blame] | 824 | return res; |
| Christian Couder | d937d4a | 2009-05-09 15:55:46 | [diff] [blame] | 825 | } |
| 826 | |
| 827 | /* |
| 828 | * "check_good_are_ancestors_of_bad" checks that all "good" revs are |
| 829 | * ancestor of the "bad" rev. |
| 830 | * |
| 831 | * If that's not the case, we need to check the merge bases. |
| 832 | * If a merge base must be tested by the user, its source code will be |
| 833 | * checked out to be tested by the user and we will exit. |
| 834 | */ |
| Jon Seymour | fee92fc | 2011-08-04 12:01:00 | [diff] [blame] | 835 | static void check_good_are_ancestors_of_bad(const char *prefix, int no_checkout) |
| Christian Couder | d937d4a | 2009-05-09 15:55:46 | [diff] [blame] | 836 | { |
| Ramsay Jones | d292bfa | 2012-09-04 17:30:21 | [diff] [blame] | 837 | char *filename = git_pathdup("BISECT_ANCESTORS_OK"); |
| Christian Couder | d937d4a | 2009-05-09 15:55:46 | [diff] [blame] | 838 | struct stat st; |
| 839 | int fd; |
| 840 | |
| 841 | if (!current_bad_sha1) |
| 842 | die("a bad revision is needed"); |
| 843 | |
| 844 | /* Check if file BISECT_ANCESTORS_OK exists. */ |
| 845 | if (!stat(filename, &st) && S_ISREG(st.st_mode)) |
| Michael Haggerty | 144e709 | 2012-04-26 22:26:59 | [diff] [blame] | 846 | goto done; |
| Christian Couder | d937d4a | 2009-05-09 15:55:46 | [diff] [blame] | 847 | |
| 848 | /* Bisecting with no good rev is ok. */ |
| Jeff King | 902bb36 | 2011-05-19 21:34:33 | [diff] [blame] | 849 | if (good_revs.nr == 0) |
| Michael Haggerty | 144e709 | 2012-04-26 22:26:59 | [diff] [blame] | 850 | goto done; |
| Christian Couder | d937d4a | 2009-05-09 15:55:46 | [diff] [blame] | 851 | |
| Christian Couder | 2d938fc | 2009-05-17 15:36:46 | [diff] [blame] | 852 | /* Check if all good revs are ancestor of the bad rev. */ |
| 853 | if (check_ancestors(prefix)) |
| Jon Seymour | fee92fc | 2011-08-04 12:01:00 | [diff] [blame] | 854 | check_merge_bases(no_checkout); |
| Christian Couder | d937d4a | 2009-05-09 15:55:46 | [diff] [blame] | 855 | |
| 856 | /* Create file BISECT_ANCESTORS_OK. */ |
| 857 | fd = open(filename, O_CREAT | O_TRUNC | O_WRONLY, 0600); |
| 858 | if (fd < 0) |
| 859 | warning("could not create file '%s': %s", |
| 860 | filename, strerror(errno)); |
| 861 | else |
| 862 | close(fd); |
| Michael Haggerty | 144e709 | 2012-04-26 22:26:59 | [diff] [blame] | 863 | done: |
| 864 | free(filename); |
| Christian Couder | d937d4a | 2009-05-09 15:55:46 | [diff] [blame] | 865 | } |
| 866 | |
| 867 | /* |
| Christian Couder | e22278c | 2009-05-28 21:21:16 | [diff] [blame] | 868 | * This does "git diff-tree --pretty COMMIT" without one fork+exec. |
| 869 | */ |
| 870 | static void show_diff_tree(const char *prefix, struct commit *commit) |
| 871 | { |
| 872 | struct rev_info opt; |
| 873 | |
| 874 | /* diff-tree init */ |
| 875 | init_revisions(&opt, prefix); |
| 876 | git_config(git_diff_basic_config, NULL); /* no "diff" UI options */ |
| 877 | opt.abbrev = 0; |
| 878 | opt.diff = 1; |
| 879 | |
| 880 | /* This is what "--pretty" does */ |
| 881 | opt.verbose_header = 1; |
| 882 | opt.use_terminator = 0; |
| 883 | opt.commit_format = CMIT_FMT_DEFAULT; |
| 884 | |
| 885 | /* diff-tree init */ |
| 886 | if (!opt.diffopt.output_format) |
| 887 | opt.diffopt.output_format = DIFF_FORMAT_RAW; |
| 888 | |
| 889 | log_tree_commit(&opt, commit); |
| 890 | } |
| 891 | |
| 892 | /* |
| Christian Couder | ef24c7c | 2009-04-19 09:56:07 | [diff] [blame] | 893 | * We use the convention that exiting with an exit code 10 means that |
| 894 | * the bisection process finished successfully. |
| 895 | * In this case the calling shell script should exit 0. |
| Jon Seymour | fee92fc | 2011-08-04 12:01:00 | [diff] [blame] | 896 | * |
| 897 | * If no_checkout is non-zero, the bisection process does not |
| 898 | * checkout the trial commit but instead simply updates BISECT_HEAD. |
| Christian Couder | ef24c7c | 2009-04-19 09:56:07 | [diff] [blame] | 899 | */ |
| Jon Seymour | fee92fc | 2011-08-04 12:01:00 | [diff] [blame] | 900 | int bisect_next_all(const char *prefix, int no_checkout) |
| Christian Couder | ef24c7c | 2009-04-19 09:56:07 | [diff] [blame] | 901 | { |
| 902 | struct rev_info revs; |
| 903 | struct commit_list *tried; |
| David Ripton | 6329bad | 2010-01-19 15:13:33 | [diff] [blame] | 904 | int reaches = 0, all = 0, nr, steps; |
| Christian Couder | ef24c7c | 2009-04-19 09:56:07 | [diff] [blame] | 905 | const unsigned char *bisect_rev; |
| 906 | char bisect_rev_hex[41]; |
| 907 | |
| Christian Couder | 2b02069 | 2009-05-09 15:55:42 | [diff] [blame] | 908 | if (read_bisect_refs()) |
| 909 | die("reading bisect refs failed"); |
| 910 | |
| Jon Seymour | fee92fc | 2011-08-04 12:01:00 | [diff] [blame] | 911 | check_good_are_ancestors_of_bad(prefix, no_checkout); |
| Christian Couder | 0871984 | 2009-05-09 15:55:47 | [diff] [blame] | 912 | |
| Christian Couder | a22347c | 2009-05-17 15:36:44 | [diff] [blame] | 913 | bisect_rev_setup(&revs, prefix, "%s", "^%s", 1); |
| 914 | revs.limited = 1; |
| Christian Couder | 2b02069 | 2009-05-09 15:55:42 | [diff] [blame] | 915 | |
| Christian Couder | a22347c | 2009-05-17 15:36:44 | [diff] [blame] | 916 | bisect_common(&revs); |
| Christian Couder | ef24c7c | 2009-04-19 09:56:07 | [diff] [blame] | 917 | |
| Christian Couder | a22347c | 2009-05-17 15:36:44 | [diff] [blame] | 918 | revs.commits = find_bisection(revs.commits, &reaches, &all, |
| Jeff King | 902bb36 | 2011-05-19 21:34:33 | [diff] [blame] | 919 | !!skipped_revs.nr); |
| Christian Couder | 62d0b0d | 2009-06-06 04:41:34 | [diff] [blame] | 920 | revs.commits = managed_skipped(revs.commits, &tried); |
| Christian Couder | ef24c7c | 2009-04-19 09:56:07 | [diff] [blame] | 921 | |
| 922 | if (!revs.commits) { |
| 923 | /* |
| 924 | * We should exit here only if the "bad" |
| 925 | * commit is also a "skip" commit. |
| 926 | */ |
| 927 | exit_if_skipped_commits(tried, NULL); |
| 928 | |
| 929 | printf("%s was both good and bad\n", |
| 930 | sha1_to_hex(current_bad_sha1)); |
| 931 | exit(1); |
| 932 | } |
| 933 | |
| Christian Couder | 8f69f72 | 2010-02-28 22:19:09 | [diff] [blame] | 934 | if (!all) { |
| 935 | fprintf(stderr, "No testable commit found.\n" |
| 936 | "Maybe you started with bad path parameters?\n"); |
| 937 | exit(4); |
| 938 | } |
| 939 | |
| Christian Couder | ef24c7c | 2009-04-19 09:56:07 | [diff] [blame] | 940 | bisect_rev = revs.commits->item->object.sha1; |
| 941 | memcpy(bisect_rev_hex, sha1_to_hex(bisect_rev), 41); |
| 942 | |
| 943 | if (!hashcmp(bisect_rev, current_bad_sha1)) { |
| 944 | exit_if_skipped_commits(tried, current_bad_sha1); |
| Nanako Shiraishi | 21d0bc2 | 2009-08-26 08:38:50 | [diff] [blame] | 945 | printf("%s is the first bad commit\n", bisect_rev_hex); |
| Christian Couder | e22278c | 2009-05-28 21:21:16 | [diff] [blame] | 946 | show_diff_tree(prefix, revs.commits->item); |
| Christian Couder | ef24c7c | 2009-04-19 09:56:07 | [diff] [blame] | 947 | /* This means the bisection process succeeded. */ |
| 948 | exit(10); |
| 949 | } |
| 950 | |
| 951 | nr = all - reaches - 1; |
| David Ripton | 6329bad | 2010-01-19 15:13:33 | [diff] [blame] | 952 | steps = estimate_bisect_steps(all); |
| 953 | printf("Bisecting: %d revision%s left to test after this " |
| 954 | "(roughly %d step%s)\n", nr, (nr == 1 ? "" : "s"), |
| 955 | steps, (steps == 1 ? "" : "s")); |
| Christian Couder | ef24c7c | 2009-04-19 09:56:07 | [diff] [blame] | 956 | |
| Jon Seymour | fee92fc | 2011-08-04 12:01:00 | [diff] [blame] | 957 | return bisect_checkout(bisect_rev_hex, no_checkout); |
| Christian Couder | ef24c7c | 2009-04-19 09:56:07 | [diff] [blame] | 958 | } |
| 959 | |
| Nguyễn Thái Ngọc Duy | c43cb38 | 2012-10-26 15:53:50 | [diff] [blame] | 960 | static inline int log2i(int n) |
| 961 | { |
| 962 | int log2 = 0; |
| 963 | |
| 964 | for (; n > 1; n >>= 1) |
| 965 | log2++; |
| 966 | |
| 967 | return log2; |
| 968 | } |
| 969 | |
| 970 | static inline int exp2i(int n) |
| 971 | { |
| 972 | return 1 << n; |
| 973 | } |
| 974 | |
| 975 | /* |
| 976 | * Estimate the number of bisect steps left (after the current step) |
| 977 | * |
| 978 | * For any x between 0 included and 2^n excluded, the probability for |
| 979 | * n - 1 steps left looks like: |
| 980 | * |
| 981 | * P(2^n + x) == (2^n - x) / (2^n + x) |
| 982 | * |
| 983 | * and P(2^n + x) < 0.5 means 2^n < 3x |
| 984 | */ |
| 985 | int estimate_bisect_steps(int all) |
| 986 | { |
| 987 | int n, x, e; |
| 988 | |
| 989 | if (all < 3) |
| 990 | return 0; |
| 991 | |
| 992 | n = log2i(all); |
| 993 | e = exp2i(n); |
| 994 | x = all - e; |
| 995 | |
| 996 | return (e < 3 * x) ? n : n - 1; |
| 997 | } |