Christian Couder | a2ad79c | 2009-03-26 05:55:24 +0100 | [diff] [blame] | 1 | #include "cache.h" |
| 2 | #include "commit.h" |
| 3 | #include "diff.h" |
| 4 | #include "revision.h" |
Christian Couder | 1bf072e | 2009-03-26 05:55:54 +0100 | [diff] [blame] | 5 | #include "refs.h" |
| 6 | #include "list-objects.h" |
Christian Couder | 3b437b0 | 2009-03-26 05:55:59 +0100 | [diff] [blame] | 7 | #include "quote.h" |
Christian Couder | 4eb5b64 | 2009-04-04 22:59:36 +0200 | [diff] [blame] | 8 | #include "sha1-lookup.h" |
Christian Couder | ef24c7c | 2009-04-19 11:56:07 +0200 | [diff] [blame] | 9 | #include "run-command.h" |
Christian Couder | e22278c | 2009-05-28 23:21:16 +0200 | [diff] [blame] | 10 | #include "log-tree.h" |
Christian Couder | a2ad79c | 2009-03-26 05:55:24 +0100 | [diff] [blame] | 11 | #include "bisect.h" |
Jeff King | 902bb36 | 2011-05-19 17:34:33 -0400 | [diff] [blame] | 12 | #include "sha1-array.h" |
Jeff King | 8a534b6 | 2011-09-13 17:58:14 -0400 | [diff] [blame] | 13 | #include "argv-array.h" |
Christian Couder | 6212b1a | 2009-05-09 17:55:38 +0200 | [diff] [blame] | 14 | |
Christian Couder | fad2d31 | 2009-05-09 17:55:40 +0200 | [diff] [blame] | 15 | static struct sha1_array good_revs; |
Christian Couder | 6212b1a | 2009-05-09 17:55:38 +0200 | [diff] [blame] | 16 | static struct sha1_array skipped_revs; |
Christian Couder | 1bf072e | 2009-03-26 05:55:54 +0100 | [diff] [blame] | 17 | |
Christian Couder | ef24c7c | 2009-04-19 11:56:07 +0200 | [diff] [blame] | 18 | static const unsigned char *current_bad_sha1; |
| 19 | |
Christian Couder | ef24c7c | 2009-04-19 11:56:07 +0200 | [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 22:01:00 +1000 | [diff] [blame] | 22 | static const char *argv_update_ref[] = {"update-ref", "--no-deref", "BISECT_HEAD", NULL, NULL}; |
Christian Couder | ef24c7c | 2009-04-19 11:56:07 +0200 | [diff] [blame] | 23 | |
Christian Couder | a2ad79c | 2009-03-26 05:55:24 +0100 | [diff] [blame] | 24 | /* bits #0-15 in revision.h */ |
| 25 | |
| 26 | #define COUNTED (1u<<16) |
| 27 | |
| 28 | /* |
| 29 | * This is a truly stupid algorithm, but it's only |
| 30 | * used for bisection, and we just don't care enough. |
| 31 | * |
| 32 | * We care just barely enough to avoid recursing for |
| 33 | * non-merge entries. |
| 34 | */ |
| 35 | static int count_distance(struct commit_list *entry) |
| 36 | { |
| 37 | int nr = 0; |
| 38 | |
| 39 | while (entry) { |
| 40 | struct commit *commit = entry->item; |
| 41 | struct commit_list *p; |
| 42 | |
| 43 | if (commit->object.flags & (UNINTERESTING | COUNTED)) |
| 44 | break; |
| 45 | if (!(commit->object.flags & TREESAME)) |
| 46 | nr++; |
| 47 | commit->object.flags |= COUNTED; |
| 48 | p = commit->parents; |
| 49 | entry = p; |
| 50 | if (p) { |
| 51 | p = p->next; |
| 52 | while (p) { |
| 53 | nr += count_distance(p); |
| 54 | p = p->next; |
| 55 | } |
| 56 | } |
| 57 | } |
| 58 | |
| 59 | return nr; |
| 60 | } |
| 61 | |
| 62 | static void clear_distance(struct commit_list *list) |
| 63 | { |
| 64 | while (list) { |
| 65 | struct commit *commit = list->item; |
| 66 | commit->object.flags &= ~COUNTED; |
| 67 | list = list->next; |
| 68 | } |
| 69 | } |
| 70 | |
| 71 | #define DEBUG_BISECT 0 |
| 72 | |
| 73 | static inline int weight(struct commit_list *elem) |
| 74 | { |
| 75 | return *((int*)(elem->item->util)); |
| 76 | } |
| 77 | |
| 78 | static inline void weight_set(struct commit_list *elem, int weight) |
| 79 | { |
| 80 | *((int*)(elem->item->util)) = weight; |
| 81 | } |
| 82 | |
| 83 | static int count_interesting_parents(struct commit *commit) |
| 84 | { |
| 85 | struct commit_list *p; |
| 86 | int count; |
| 87 | |
| 88 | for (count = 0, p = commit->parents; p; p = p->next) { |
| 89 | if (p->item->object.flags & UNINTERESTING) |
| 90 | continue; |
| 91 | count++; |
| 92 | } |
| 93 | return count; |
| 94 | } |
| 95 | |
| 96 | static inline int halfway(struct commit_list *p, int nr) |
| 97 | { |
| 98 | /* |
| 99 | * Don't short-cut something we are not going to return! |
| 100 | */ |
| 101 | if (p->item->object.flags & TREESAME) |
| 102 | return 0; |
| 103 | if (DEBUG_BISECT) |
| 104 | return 0; |
| 105 | /* |
| 106 | * 2 and 3 are halfway of 5. |
| 107 | * 3 is halfway of 6 but 2 and 4 are not. |
| 108 | */ |
| 109 | switch (2 * weight(p) - nr) { |
| 110 | case -1: case 0: case 1: |
| 111 | return 1; |
| 112 | default: |
| 113 | return 0; |
| 114 | } |
| 115 | } |
| 116 | |
| 117 | #if !DEBUG_BISECT |
| 118 | #define show_list(a,b,c,d) do { ; } while (0) |
| 119 | #else |
| 120 | static void show_list(const char *debug, int counted, int nr, |
| 121 | struct commit_list *list) |
| 122 | { |
| 123 | struct commit_list *p; |
| 124 | |
| 125 | fprintf(stderr, "%s (%d/%d)\n", debug, counted, nr); |
| 126 | |
| 127 | for (p = list; p; p = p->next) { |
| 128 | struct commit_list *pp; |
| 129 | struct commit *commit = p->item; |
| 130 | unsigned flags = commit->object.flags; |
| 131 | enum object_type type; |
| 132 | unsigned long size; |
| 133 | char *buf = read_sha1_file(commit->object.sha1, &type, &size); |
Christian Couder | 56ff379 | 2010-07-22 15:18:33 +0200 | [diff] [blame] | 134 | const char *subject_start; |
| 135 | int subject_len; |
Christian Couder | a2ad79c | 2009-03-26 05:55:24 +0100 | [diff] [blame] | 136 | |
| 137 | fprintf(stderr, "%c%c%c ", |
| 138 | (flags & TREESAME) ? ' ' : 'T', |
| 139 | (flags & UNINTERESTING) ? 'U' : ' ', |
| 140 | (flags & COUNTED) ? 'C' : ' '); |
| 141 | if (commit->util) |
| 142 | fprintf(stderr, "%3d", weight(p)); |
| 143 | else |
| 144 | fprintf(stderr, "---"); |
| 145 | fprintf(stderr, " %.*s", 8, sha1_to_hex(commit->object.sha1)); |
| 146 | for (pp = commit->parents; pp; pp = pp->next) |
| 147 | fprintf(stderr, " %.*s", 8, |
| 148 | sha1_to_hex(pp->item->object.sha1)); |
| 149 | |
Christian Couder | 56ff379 | 2010-07-22 15:18:33 +0200 | [diff] [blame] | 150 | subject_len = find_commit_subject(buf, &subject_start); |
| 151 | if (subject_len) |
| 152 | fprintf(stderr, " %.*s", subject_len, subject_start); |
Christian Couder | a2ad79c | 2009-03-26 05:55:24 +0100 | [diff] [blame] | 153 | fprintf(stderr, "\n"); |
| 154 | } |
| 155 | } |
| 156 | #endif /* DEBUG_BISECT */ |
| 157 | |
| 158 | static struct commit_list *best_bisection(struct commit_list *list, int nr) |
| 159 | { |
| 160 | struct commit_list *p, *best; |
| 161 | int best_distance = -1; |
| 162 | |
| 163 | best = list; |
| 164 | for (p = list; p; p = p->next) { |
| 165 | int distance; |
| 166 | unsigned flags = p->item->object.flags; |
| 167 | |
| 168 | if (flags & TREESAME) |
| 169 | continue; |
| 170 | distance = weight(p); |
| 171 | if (nr - distance < distance) |
| 172 | distance = nr - distance; |
| 173 | if (distance > best_distance) { |
| 174 | best = p; |
| 175 | best_distance = distance; |
| 176 | } |
| 177 | } |
| 178 | |
| 179 | return best; |
| 180 | } |
| 181 | |
| 182 | struct commit_dist { |
| 183 | struct commit *commit; |
| 184 | int distance; |
| 185 | }; |
| 186 | |
| 187 | static int compare_commit_dist(const void *a_, const void *b_) |
| 188 | { |
| 189 | struct commit_dist *a, *b; |
| 190 | |
| 191 | a = (struct commit_dist *)a_; |
| 192 | b = (struct commit_dist *)b_; |
| 193 | if (a->distance != b->distance) |
| 194 | return b->distance - a->distance; /* desc sort */ |
| 195 | return hashcmp(a->commit->object.sha1, b->commit->object.sha1); |
| 196 | } |
| 197 | |
| 198 | static struct commit_list *best_bisection_sorted(struct commit_list *list, int nr) |
| 199 | { |
| 200 | struct commit_list *p; |
| 201 | struct commit_dist *array = xcalloc(nr, sizeof(*array)); |
| 202 | int cnt, i; |
| 203 | |
| 204 | for (p = list, cnt = 0; p; p = p->next) { |
| 205 | int distance; |
| 206 | unsigned flags = p->item->object.flags; |
| 207 | |
| 208 | if (flags & TREESAME) |
| 209 | continue; |
| 210 | distance = weight(p); |
| 211 | if (nr - distance < distance) |
| 212 | distance = nr - distance; |
| 213 | array[cnt].commit = p->item; |
| 214 | array[cnt].distance = distance; |
| 215 | cnt++; |
| 216 | } |
| 217 | qsort(array, cnt, sizeof(*array), compare_commit_dist); |
| 218 | for (p = list, i = 0; i < cnt; i++) { |
| 219 | struct name_decoration *r = xmalloc(sizeof(*r) + 100); |
| 220 | struct object *obj = &(array[i].commit->object); |
| 221 | |
| 222 | sprintf(r->name, "dist=%d", array[i].distance); |
| 223 | r->next = add_decoration(&name_decoration, obj, r); |
| 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 05:55:54 +0100 | [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")) { |
Christian Couder | ef24c7c | 2009-04-19 11:56:07 +0200 | [diff] [blame] | 407 | current_bad_sha1 = sha1; |
Christian Couder | 1bf072e | 2009-03-26 05:55:54 +0100 | [diff] [blame] | 408 | } else if (!prefixcmp(refname, "good-")) { |
Jeff King | 902bb36 | 2011-05-19 17:34:33 -0400 | [diff] [blame] | 409 | sha1_array_append(&good_revs, sha1); |
Christian Couder | 1bf072e | 2009-03-26 05:55:54 +0100 | [diff] [blame] | 410 | } else if (!prefixcmp(refname, "skip-")) { |
Jeff King | 902bb36 | 2011-05-19 17:34:33 -0400 | [diff] [blame] | 411 | sha1_array_append(&skipped_revs, sha1); |
Christian Couder | 1bf072e | 2009-03-26 05:55:54 +0100 | [diff] [blame] | 412 | } |
| 413 | |
| 414 | return 0; |
| 415 | } |
| 416 | |
| 417 | static int read_bisect_refs(void) |
| 418 | { |
| 419 | return for_each_ref_in("refs/bisect/", register_ref, NULL); |
| 420 | } |
| 421 | |
Linus Torvalds | 2af202b | 2009-06-18 10:28:43 -0700 | [diff] [blame] | 422 | static void read_bisect_paths(struct argv_array *array) |
Christian Couder | 3b437b0 | 2009-03-26 05:55:59 +0100 | [diff] [blame] | 423 | { |
| 424 | struct strbuf str = STRBUF_INIT; |
| 425 | const char *filename = git_path("BISECT_NAMES"); |
| 426 | FILE *fp = fopen(filename, "r"); |
| 427 | |
| 428 | if (!fp) |
Thomas Rast | d824cbb | 2009-06-27 17:58:46 +0200 | [diff] [blame] | 429 | die_errno("Could not open file '%s'", filename); |
Christian Couder | 3b437b0 | 2009-03-26 05:55:59 +0100 | [diff] [blame] | 430 | |
| 431 | while (strbuf_getline(&str, fp, '\n') != EOF) { |
Christian Couder | 3b437b0 | 2009-03-26 05:55:59 +0100 | [diff] [blame] | 432 | strbuf_trim(&str); |
Jeff King | 8a534b6 | 2011-09-13 17:58:14 -0400 | [diff] [blame] | 433 | if (sq_dequote_to_argv_array(str.buf, array)) |
Christian Couder | 3b437b0 | 2009-03-26 05:55:59 +0100 | [diff] [blame] | 434 | die("Badly quoted content in file '%s': %s", |
Jeff King | 8a534b6 | 2011-09-13 17:58:14 -0400 | [diff] [blame] | 435 | filename, str.buf); |
Christian Couder | 3b437b0 | 2009-03-26 05:55:59 +0100 | [diff] [blame] | 436 | } |
| 437 | |
| 438 | strbuf_release(&str); |
| 439 | fclose(fp); |
| 440 | } |
| 441 | |
Christian Couder | c053766 | 2009-05-09 17:55:45 +0200 | [diff] [blame] | 442 | static char *join_sha1_array_hex(struct sha1_array *array, char delim) |
| 443 | { |
| 444 | struct strbuf joined_hexs = STRBUF_INIT; |
| 445 | int i; |
| 446 | |
Jeff King | 902bb36 | 2011-05-19 17:34:33 -0400 | [diff] [blame] | 447 | for (i = 0; i < array->nr; i++) { |
Christian Couder | c053766 | 2009-05-09 17:55:45 +0200 | [diff] [blame] | 448 | strbuf_addstr(&joined_hexs, sha1_to_hex(array->sha1[i])); |
Jeff King | 902bb36 | 2011-05-19 17:34:33 -0400 | [diff] [blame] | 449 | if (i + 1 < array->nr) |
Christian Couder | c053766 | 2009-05-09 17:55:45 +0200 | [diff] [blame] | 450 | strbuf_addch(&joined_hexs, delim); |
| 451 | } |
| 452 | |
| 453 | return strbuf_detach(&joined_hexs, NULL); |
| 454 | } |
| 455 | |
Christian Couder | 9af3589 | 2009-06-06 06:41:33 +0200 | [diff] [blame] | 456 | /* |
| 457 | * In this function, passing a not NULL skipped_first is very special. |
| 458 | * It means that we want to know if the first commit in the list is |
| 459 | * skipped because we will want to test a commit away from it if it is |
| 460 | * indeed skipped. |
| 461 | * So if the first commit is skipped, we cannot take the shortcut to |
| 462 | * just "return list" when we find the first non skipped commit, we |
| 463 | * have to return a fully filtered list. |
| 464 | * |
| 465 | * We use (*skipped_first == -1) to mean "it has been found that the |
| 466 | * first commit is not skipped". In this case *skipped_first is set back |
| 467 | * to 0 just before the function returns. |
| 468 | */ |
Christian Couder | 9518864 | 2009-03-26 05:55:49 +0100 | [diff] [blame] | 469 | struct commit_list *filter_skipped(struct commit_list *list, |
| 470 | struct commit_list **tried, |
Christian Couder | 9af3589 | 2009-06-06 06:41:33 +0200 | [diff] [blame] | 471 | int show_all, |
| 472 | int *count, |
| 473 | int *skipped_first) |
Christian Couder | 9518864 | 2009-03-26 05:55:49 +0100 | [diff] [blame] | 474 | { |
| 475 | struct commit_list *filtered = NULL, **f = &filtered; |
| 476 | |
| 477 | *tried = NULL; |
| 478 | |
Christian Couder | 9af3589 | 2009-06-06 06:41:33 +0200 | [diff] [blame] | 479 | if (skipped_first) |
| 480 | *skipped_first = 0; |
| 481 | if (count) |
| 482 | *count = 0; |
| 483 | |
Jeff King | 902bb36 | 2011-05-19 17:34:33 -0400 | [diff] [blame] | 484 | if (!skipped_revs.nr) |
Christian Couder | 9518864 | 2009-03-26 05:55:49 +0100 | [diff] [blame] | 485 | return list; |
| 486 | |
Christian Couder | 9518864 | 2009-03-26 05:55:49 +0100 | [diff] [blame] | 487 | while (list) { |
| 488 | struct commit_list *next = list->next; |
| 489 | list->next = NULL; |
Jeff King | 902bb36 | 2011-05-19 17:34:33 -0400 | [diff] [blame] | 490 | if (0 <= sha1_array_lookup(&skipped_revs, |
Christian Couder | aaaff9e | 2009-05-09 17:55:43 +0200 | [diff] [blame] | 491 | list->item->object.sha1)) { |
Christian Couder | 9af3589 | 2009-06-06 06:41:33 +0200 | [diff] [blame] | 492 | if (skipped_first && !*skipped_first) |
| 493 | *skipped_first = 1; |
Christian Couder | 9518864 | 2009-03-26 05:55:49 +0100 | [diff] [blame] | 494 | /* Move current to tried list */ |
| 495 | *tried = list; |
| 496 | tried = &list->next; |
| 497 | } else { |
Christian Couder | 9af3589 | 2009-06-06 06:41:33 +0200 | [diff] [blame] | 498 | if (!show_all) { |
| 499 | if (!skipped_first || !*skipped_first) |
| 500 | return list; |
| 501 | } else if (skipped_first && !*skipped_first) { |
| 502 | /* This means we know it's not skipped */ |
| 503 | *skipped_first = -1; |
| 504 | } |
Christian Couder | 9518864 | 2009-03-26 05:55:49 +0100 | [diff] [blame] | 505 | /* Move current to filtered list */ |
| 506 | *f = list; |
| 507 | f = &list->next; |
Christian Couder | 9af3589 | 2009-06-06 06:41:33 +0200 | [diff] [blame] | 508 | if (count) |
| 509 | (*count)++; |
Christian Couder | 9518864 | 2009-03-26 05:55:49 +0100 | [diff] [blame] | 510 | } |
| 511 | list = next; |
| 512 | } |
| 513 | |
Christian Couder | 9af3589 | 2009-06-06 06:41:33 +0200 | [diff] [blame] | 514 | if (skipped_first && *skipped_first == -1) |
| 515 | *skipped_first = 0; |
| 516 | |
Christian Couder | 9518864 | 2009-03-26 05:55:49 +0100 | [diff] [blame] | 517 | return filtered; |
| 518 | } |
Christian Couder | 1bf072e | 2009-03-26 05:55:54 +0100 | [diff] [blame] | 519 | |
Christian Couder | ebc9529 | 2009-06-13 07:21:06 +0200 | [diff] [blame] | 520 | #define PRN_MODULO 32768 |
| 521 | |
| 522 | /* |
| 523 | * This is a pseudo random number generator based on "man 3 rand". |
| 524 | * It is not used properly because the seed is the argument and it |
| 525 | * is increased by one between each call, but that should not matter |
| 526 | * for this application. |
| 527 | */ |
Junio C Hamano | ebdc302 | 2010-01-11 21:20:55 -0800 | [diff] [blame] | 528 | static int get_prn(int count) { |
Christian Couder | ebc9529 | 2009-06-13 07:21:06 +0200 | [diff] [blame] | 529 | count = count * 1103515245 + 12345; |
| 530 | return ((unsigned)(count/65536) % PRN_MODULO); |
| 531 | } |
| 532 | |
| 533 | /* |
| 534 | * Custom integer square root from |
| 535 | * http://en.wikipedia.org/wiki/Integer_square_root |
| 536 | */ |
| 537 | static int sqrti(int val) |
Christian Couder | 62d0b0d | 2009-06-06 06:41:34 +0200 | [diff] [blame] | 538 | { |
Christian Couder | ebc9529 | 2009-06-13 07:21:06 +0200 | [diff] [blame] | 539 | float d, x = val; |
| 540 | |
| 541 | if (val == 0) |
| 542 | return 0; |
| 543 | |
| 544 | do { |
| 545 | float y = (x + (float)val / x) / 2; |
| 546 | d = (y > x) ? y - x : x - y; |
| 547 | x = y; |
| 548 | } while (d >= 0.5); |
| 549 | |
| 550 | return (int)x; |
| 551 | } |
| 552 | |
| 553 | static struct commit_list *skip_away(struct commit_list *list, int count) |
| 554 | { |
Christian Couder | 62d0b0d | 2009-06-06 06:41:34 +0200 | [diff] [blame] | 555 | struct commit_list *cur, *previous; |
Christian Couder | ebc9529 | 2009-06-13 07:21:06 +0200 | [diff] [blame] | 556 | int prn, index, i; |
| 557 | |
| 558 | prn = get_prn(count); |
| 559 | index = (count * prn / PRN_MODULO) * sqrti(prn) / sqrti(PRN_MODULO); |
Christian Couder | 62d0b0d | 2009-06-06 06:41:34 +0200 | [diff] [blame] | 560 | |
| 561 | cur = list; |
| 562 | previous = NULL; |
Christian Couder | 62d0b0d | 2009-06-06 06:41:34 +0200 | [diff] [blame] | 563 | |
| 564 | for (i = 0; cur; cur = cur->next, i++) { |
| 565 | if (i == index) { |
| 566 | if (hashcmp(cur->item->object.sha1, current_bad_sha1)) |
| 567 | return cur; |
| 568 | if (previous) |
| 569 | return previous; |
| 570 | return list; |
| 571 | } |
| 572 | previous = cur; |
| 573 | } |
| 574 | |
| 575 | return list; |
| 576 | } |
| 577 | |
| 578 | static struct commit_list *managed_skipped(struct commit_list *list, |
| 579 | struct commit_list **tried) |
| 580 | { |
| 581 | int count, skipped_first; |
Christian Couder | 62d0b0d | 2009-06-06 06:41:34 +0200 | [diff] [blame] | 582 | |
| 583 | *tried = NULL; |
| 584 | |
Jeff King | 902bb36 | 2011-05-19 17:34:33 -0400 | [diff] [blame] | 585 | if (!skipped_revs.nr) |
Christian Couder | 62d0b0d | 2009-06-06 06:41:34 +0200 | [diff] [blame] | 586 | return list; |
| 587 | |
| 588 | list = filter_skipped(list, tried, 0, &count, &skipped_first); |
| 589 | |
| 590 | if (!skipped_first) |
| 591 | return list; |
| 592 | |
Christian Couder | ebc9529 | 2009-06-13 07:21:06 +0200 | [diff] [blame] | 593 | return skip_away(list, count); |
Christian Couder | 62d0b0d | 2009-06-06 06:41:34 +0200 | [diff] [blame] | 594 | } |
| 595 | |
Christian Couder | a22347c | 2009-05-17 17:36:44 +0200 | [diff] [blame] | 596 | static void bisect_rev_setup(struct rev_info *revs, const char *prefix, |
| 597 | const char *bad_format, const char *good_format, |
| 598 | int read_paths) |
Christian Couder | 1bf072e | 2009-03-26 05:55:54 +0100 | [diff] [blame] | 599 | { |
Jeff King | 8a534b6 | 2011-09-13 17:58:14 -0400 | [diff] [blame] | 600 | struct argv_array rev_argv = ARGV_ARRAY_INIT; |
Christian Couder | fad2d31 | 2009-05-09 17:55:40 +0200 | [diff] [blame] | 601 | int i; |
| 602 | |
Christian Couder | 3b437b0 | 2009-03-26 05:55:59 +0100 | [diff] [blame] | 603 | init_revisions(revs, prefix); |
| 604 | revs->abbrev = 0; |
| 605 | revs->commit_format = CMIT_FMT_UNSPECIFIED; |
Christian Couder | 1bf072e | 2009-03-26 05:55:54 +0100 | [diff] [blame] | 606 | |
Christian Couder | 1c953a1 | 2009-05-09 17:55:41 +0200 | [diff] [blame] | 607 | /* rev_argv.argv[0] will be ignored by setup_revisions */ |
Jeff King | 8a534b6 | 2011-09-13 17:58:14 -0400 | [diff] [blame] | 608 | argv_array_push(&rev_argv, "bisect_rev_setup"); |
| 609 | argv_array_pushf(&rev_argv, bad_format, sha1_to_hex(current_bad_sha1)); |
Jeff King | 902bb36 | 2011-05-19 17:34:33 -0400 | [diff] [blame] | 610 | for (i = 0; i < good_revs.nr; i++) |
Jeff King | 8a534b6 | 2011-09-13 17:58:14 -0400 | [diff] [blame] | 611 | argv_array_pushf(&rev_argv, good_format, |
| 612 | sha1_to_hex(good_revs.sha1[i])); |
| 613 | argv_array_push(&rev_argv, "--"); |
Christian Couder | a22347c | 2009-05-17 17:36:44 +0200 | [diff] [blame] | 614 | if (read_paths) |
| 615 | read_bisect_paths(&rev_argv); |
Christian Couder | 1bf072e | 2009-03-26 05:55:54 +0100 | [diff] [blame] | 616 | |
Jeff King | 8a534b6 | 2011-09-13 17:58:14 -0400 | [diff] [blame] | 617 | setup_revisions(rev_argv.argc, rev_argv.argv, revs, NULL); |
| 618 | /* XXX leak rev_argv, as "revs" may still be pointing to it */ |
Christian Couder | 3b437b0 | 2009-03-26 05:55:59 +0100 | [diff] [blame] | 619 | } |
| 620 | |
Christian Couder | a22347c | 2009-05-17 17:36:44 +0200 | [diff] [blame] | 621 | static void bisect_common(struct rev_info *revs) |
Christian Couder | 2ace972 | 2009-04-19 11:55:57 +0200 | [diff] [blame] | 622 | { |
Christian Couder | 2ace972 | 2009-04-19 11:55:57 +0200 | [diff] [blame] | 623 | if (prepare_revision_walk(revs)) |
| 624 | die("revision walk setup failed"); |
| 625 | if (revs->tree_objects) |
| 626 | mark_edges_uninteresting(revs->commits, revs, NULL); |
Christian Couder | 2ace972 | 2009-04-19 11:55:57 +0200 | [diff] [blame] | 627 | } |
| 628 | |
Christian Couder | ef24c7c | 2009-04-19 11:56:07 +0200 | [diff] [blame] | 629 | static void exit_if_skipped_commits(struct commit_list *tried, |
| 630 | const unsigned char *bad) |
| 631 | { |
| 632 | if (!tried) |
| 633 | return; |
| 634 | |
| 635 | printf("There are only 'skip'ped commits left to test.\n" |
| 636 | "The first bad commit could be any of:\n"); |
| 637 | print_commit_list(tried, "%s\n", "%s\n"); |
| 638 | if (bad) |
| 639 | printf("%s\n", sha1_to_hex(bad)); |
| 640 | printf("We cannot bisect more!\n"); |
| 641 | exit(2); |
| 642 | } |
| 643 | |
Christian Couder | c053766 | 2009-05-09 17:55:45 +0200 | [diff] [blame] | 644 | static int is_expected_rev(const unsigned char *sha1) |
| 645 | { |
| 646 | const char *filename = git_path("BISECT_EXPECTED_REV"); |
| 647 | struct stat st; |
| 648 | struct strbuf str = STRBUF_INIT; |
| 649 | FILE *fp; |
| 650 | int res = 0; |
| 651 | |
| 652 | if (stat(filename, &st) || !S_ISREG(st.st_mode)) |
| 653 | return 0; |
| 654 | |
| 655 | fp = fopen(filename, "r"); |
| 656 | if (!fp) |
| 657 | return 0; |
| 658 | |
| 659 | if (strbuf_getline(&str, fp, '\n') != EOF) |
| 660 | res = !strcmp(str.buf, sha1_to_hex(sha1)); |
| 661 | |
| 662 | strbuf_release(&str); |
| 663 | fclose(fp); |
| 664 | |
| 665 | return res; |
| 666 | } |
| 667 | |
Christian Couder | ef24c7c | 2009-04-19 11:56:07 +0200 | [diff] [blame] | 668 | static void mark_expected_rev(char *bisect_rev_hex) |
| 669 | { |
| 670 | int len = strlen(bisect_rev_hex); |
| 671 | const char *filename = git_path("BISECT_EXPECTED_REV"); |
| 672 | int fd = open(filename, O_CREAT | O_TRUNC | O_WRONLY, 0600); |
| 673 | |
| 674 | if (fd < 0) |
Thomas Rast | d824cbb | 2009-06-27 17:58:46 +0200 | [diff] [blame] | 675 | die_errno("could not create file '%s'", filename); |
Christian Couder | ef24c7c | 2009-04-19 11:56:07 +0200 | [diff] [blame] | 676 | |
| 677 | bisect_rev_hex[len] = '\n'; |
| 678 | write_or_die(fd, bisect_rev_hex, len + 1); |
| 679 | bisect_rev_hex[len] = '\0'; |
| 680 | |
| 681 | if (close(fd) < 0) |
| 682 | die("closing file %s: %s", filename, strerror(errno)); |
| 683 | } |
| 684 | |
Jon Seymour | fee92fc | 2011-08-04 22:01:00 +1000 | [diff] [blame] | 685 | static int bisect_checkout(char *bisect_rev_hex, int no_checkout) |
Christian Couder | ef24c7c | 2009-04-19 11:56:07 +0200 | [diff] [blame] | 686 | { |
| 687 | int res; |
| 688 | |
| 689 | mark_expected_rev(bisect_rev_hex); |
| 690 | |
| 691 | argv_checkout[2] = bisect_rev_hex; |
Jon Seymour | fee92fc | 2011-08-04 22:01:00 +1000 | [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 { |
| 698 | res = run_command_v_opt(argv_checkout, RUN_GIT_CMD); |
| 699 | if (res) |
| 700 | exit(res); |
| 701 | } |
Christian Couder | ef24c7c | 2009-04-19 11:56:07 +0200 | [diff] [blame] | 702 | |
| 703 | argv_show_branch[1] = bisect_rev_hex; |
| 704 | return run_command_v_opt(argv_show_branch, RUN_GIT_CMD); |
| 705 | } |
| 706 | |
Christian Couder | c053766 | 2009-05-09 17:55:45 +0200 | [diff] [blame] | 707 | static struct commit *get_commit_reference(const unsigned char *sha1) |
| 708 | { |
| 709 | struct commit *r = lookup_commit_reference(sha1); |
| 710 | if (!r) |
| 711 | die("Not a valid commit name %s", sha1_to_hex(sha1)); |
| 712 | return r; |
| 713 | } |
| 714 | |
| 715 | static struct commit **get_bad_and_good_commits(int *rev_nr) |
| 716 | { |
Jeff King | 902bb36 | 2011-05-19 17:34:33 -0400 | [diff] [blame] | 717 | int len = 1 + good_revs.nr; |
Christian Couder | c053766 | 2009-05-09 17:55:45 +0200 | [diff] [blame] | 718 | struct commit **rev = xmalloc(len * sizeof(*rev)); |
| 719 | int i, n = 0; |
| 720 | |
| 721 | rev[n++] = get_commit_reference(current_bad_sha1); |
Jeff King | 902bb36 | 2011-05-19 17:34:33 -0400 | [diff] [blame] | 722 | for (i = 0; i < good_revs.nr; i++) |
Christian Couder | c053766 | 2009-05-09 17:55:45 +0200 | [diff] [blame] | 723 | rev[n++] = get_commit_reference(good_revs.sha1[i]); |
| 724 | *rev_nr = n; |
| 725 | |
| 726 | return rev; |
| 727 | } |
| 728 | |
| 729 | static void handle_bad_merge_base(void) |
| 730 | { |
| 731 | if (is_expected_rev(current_bad_sha1)) { |
| 732 | char *bad_hex = sha1_to_hex(current_bad_sha1); |
| 733 | char *good_hex = join_sha1_array_hex(&good_revs, ' '); |
| 734 | |
| 735 | fprintf(stderr, "The merge base %s is bad.\n" |
| 736 | "This means the bug has been fixed " |
| 737 | "between %s and [%s].\n", |
| 738 | bad_hex, bad_hex, good_hex); |
| 739 | |
| 740 | exit(3); |
| 741 | } |
| 742 | |
| 743 | fprintf(stderr, "Some good revs are not ancestor of the bad rev.\n" |
| 744 | "git bisect cannot work properly in this case.\n" |
| 745 | "Maybe you mistake good and bad revs?\n"); |
| 746 | exit(1); |
| 747 | } |
| 748 | |
Linus Torvalds | 2af202b | 2009-06-18 10:28:43 -0700 | [diff] [blame] | 749 | static void handle_skipped_merge_base(const unsigned char *mb) |
Christian Couder | c053766 | 2009-05-09 17:55:45 +0200 | [diff] [blame] | 750 | { |
| 751 | char *mb_hex = sha1_to_hex(mb); |
| 752 | char *bad_hex = sha1_to_hex(current_bad_sha1); |
| 753 | char *good_hex = join_sha1_array_hex(&good_revs, ' '); |
| 754 | |
Thiago Farina | bd757c1 | 2010-01-03 11:20:30 -0500 | [diff] [blame] | 755 | warning("the merge base between %s and [%s] " |
Christian Couder | c053766 | 2009-05-09 17:55:45 +0200 | [diff] [blame] | 756 | "must be skipped.\n" |
| 757 | "So we cannot be sure the first bad commit is " |
| 758 | "between %s and %s.\n" |
Thiago Farina | bd757c1 | 2010-01-03 11:20:30 -0500 | [diff] [blame] | 759 | "We continue anyway.", |
Christian Couder | c053766 | 2009-05-09 17:55:45 +0200 | [diff] [blame] | 760 | bad_hex, good_hex, mb_hex, bad_hex); |
| 761 | free(good_hex); |
| 762 | } |
| 763 | |
| 764 | /* |
| 765 | * "check_merge_bases" checks that merge bases are not "bad". |
| 766 | * |
| 767 | * - If one is "bad", it means the user assumed something wrong |
| 768 | * and we must exit with a non 0 error code. |
| 769 | * - If one is "good", that's good, we have nothing to do. |
| 770 | * - If one is "skipped", we can't know but we should warn. |
| 771 | * - If we don't know, we should check it out and ask the user to test. |
| 772 | */ |
Jon Seymour | fee92fc | 2011-08-04 22:01:00 +1000 | [diff] [blame] | 773 | static void check_merge_bases(int no_checkout) |
Christian Couder | c053766 | 2009-05-09 17:55:45 +0200 | [diff] [blame] | 774 | { |
| 775 | struct commit_list *result; |
| 776 | int rev_nr; |
| 777 | struct commit **rev = get_bad_and_good_commits(&rev_nr); |
| 778 | |
| 779 | result = get_merge_bases_many(rev[0], rev_nr - 1, rev + 1, 0); |
| 780 | |
| 781 | for (; result; result = result->next) { |
| 782 | const unsigned char *mb = result->item->object.sha1; |
| 783 | if (!hashcmp(mb, current_bad_sha1)) { |
| 784 | handle_bad_merge_base(); |
Jeff King | 902bb36 | 2011-05-19 17:34:33 -0400 | [diff] [blame] | 785 | } else if (0 <= sha1_array_lookup(&good_revs, mb)) { |
Christian Couder | c053766 | 2009-05-09 17:55:45 +0200 | [diff] [blame] | 786 | continue; |
Jeff King | 902bb36 | 2011-05-19 17:34:33 -0400 | [diff] [blame] | 787 | } else if (0 <= sha1_array_lookup(&skipped_revs, mb)) { |
Christian Couder | c053766 | 2009-05-09 17:55:45 +0200 | [diff] [blame] | 788 | handle_skipped_merge_base(mb); |
| 789 | } else { |
| 790 | printf("Bisecting: a merge base must be tested\n"); |
Jon Seymour | fee92fc | 2011-08-04 22:01:00 +1000 | [diff] [blame] | 791 | exit(bisect_checkout(sha1_to_hex(mb), no_checkout)); |
Christian Couder | c053766 | 2009-05-09 17:55:45 +0200 | [diff] [blame] | 792 | } |
| 793 | } |
| 794 | |
| 795 | free(rev); |
| 796 | free_commit_list(result); |
| 797 | } |
| 798 | |
Christian Couder | 2d938fc | 2009-05-17 17:36:46 +0200 | [diff] [blame] | 799 | static int check_ancestors(const char *prefix) |
Christian Couder | d937d4a | 2009-05-09 17:55:46 +0200 | [diff] [blame] | 800 | { |
Christian Couder | 2d938fc | 2009-05-17 17:36:46 +0200 | [diff] [blame] | 801 | struct rev_info revs; |
| 802 | struct object_array pending_copy; |
René Scharfe | 86a0a40 | 2011-10-01 18:16:08 +0200 | [diff] [blame] | 803 | int res; |
Christian Couder | d937d4a | 2009-05-09 17:55:46 +0200 | [diff] [blame] | 804 | |
Christian Couder | 2d938fc | 2009-05-17 17:36:46 +0200 | [diff] [blame] | 805 | bisect_rev_setup(&revs, prefix, "^%s", "%s", 0); |
Christian Couder | d937d4a | 2009-05-09 17:55:46 +0200 | [diff] [blame] | 806 | |
Christian Couder | 2d938fc | 2009-05-17 17:36:46 +0200 | [diff] [blame] | 807 | /* Save pending objects, so they can be cleaned up later. */ |
René Scharfe | 353f565 | 2011-10-01 18:01:12 +0200 | [diff] [blame] | 808 | pending_copy = revs.pending; |
| 809 | revs.leak_pending = 1; |
Christian Couder | 2d938fc | 2009-05-17 17:36:46 +0200 | [diff] [blame] | 810 | |
René Scharfe | 353f565 | 2011-10-01 18:01:12 +0200 | [diff] [blame] | 811 | /* |
| 812 | * bisect_common calls prepare_revision_walk right away, which |
| 813 | * (together with .leak_pending = 1) makes us the sole owner of |
| 814 | * the list of pending objects. |
| 815 | */ |
Christian Couder | 2d938fc | 2009-05-17 17:36:46 +0200 | [diff] [blame] | 816 | bisect_common(&revs); |
| 817 | res = (revs.commits != NULL); |
| 818 | |
| 819 | /* Clean up objects used, as they will be reused. */ |
René Scharfe | 86a0a40 | 2011-10-01 18:16:08 +0200 | [diff] [blame] | 820 | clear_commit_marks_for_object_array(&pending_copy, ALL_REV_FLAGS); |
René Scharfe | 353f565 | 2011-10-01 18:01:12 +0200 | [diff] [blame] | 821 | free(pending_copy.objects); |
Christian Couder | d937d4a | 2009-05-09 17:55:46 +0200 | [diff] [blame] | 822 | |
Christian Couder | 2d938fc | 2009-05-17 17:36:46 +0200 | [diff] [blame] | 823 | return res; |
Christian Couder | d937d4a | 2009-05-09 17:55:46 +0200 | [diff] [blame] | 824 | } |
| 825 | |
| 826 | /* |
| 827 | * "check_good_are_ancestors_of_bad" checks that all "good" revs are |
| 828 | * ancestor of the "bad" rev. |
| 829 | * |
| 830 | * If that's not the case, we need to check the merge bases. |
| 831 | * If a merge base must be tested by the user, its source code will be |
| 832 | * checked out to be tested by the user and we will exit. |
| 833 | */ |
Jon Seymour | fee92fc | 2011-08-04 22:01:00 +1000 | [diff] [blame] | 834 | static void check_good_are_ancestors_of_bad(const char *prefix, int no_checkout) |
Christian Couder | d937d4a | 2009-05-09 17:55:46 +0200 | [diff] [blame] | 835 | { |
| 836 | const char *filename = git_path("BISECT_ANCESTORS_OK"); |
| 837 | struct stat st; |
| 838 | int fd; |
| 839 | |
| 840 | if (!current_bad_sha1) |
| 841 | die("a bad revision is needed"); |
| 842 | |
| 843 | /* Check if file BISECT_ANCESTORS_OK exists. */ |
| 844 | if (!stat(filename, &st) && S_ISREG(st.st_mode)) |
| 845 | return; |
| 846 | |
| 847 | /* Bisecting with no good rev is ok. */ |
Jeff King | 902bb36 | 2011-05-19 17:34:33 -0400 | [diff] [blame] | 848 | if (good_revs.nr == 0) |
Christian Couder | d937d4a | 2009-05-09 17:55:46 +0200 | [diff] [blame] | 849 | return; |
| 850 | |
Christian Couder | 2d938fc | 2009-05-17 17:36:46 +0200 | [diff] [blame] | 851 | /* Check if all good revs are ancestor of the bad rev. */ |
| 852 | if (check_ancestors(prefix)) |
Jon Seymour | fee92fc | 2011-08-04 22:01:00 +1000 | [diff] [blame] | 853 | check_merge_bases(no_checkout); |
Christian Couder | d937d4a | 2009-05-09 17:55:46 +0200 | [diff] [blame] | 854 | |
| 855 | /* Create file BISECT_ANCESTORS_OK. */ |
| 856 | fd = open(filename, O_CREAT | O_TRUNC | O_WRONLY, 0600); |
| 857 | if (fd < 0) |
| 858 | warning("could not create file '%s': %s", |
| 859 | filename, strerror(errno)); |
| 860 | else |
| 861 | close(fd); |
| 862 | } |
| 863 | |
| 864 | /* |
Christian Couder | e22278c | 2009-05-28 23:21:16 +0200 | [diff] [blame] | 865 | * This does "git diff-tree --pretty COMMIT" without one fork+exec. |
| 866 | */ |
| 867 | static void show_diff_tree(const char *prefix, struct commit *commit) |
| 868 | { |
| 869 | struct rev_info opt; |
| 870 | |
| 871 | /* diff-tree init */ |
| 872 | init_revisions(&opt, prefix); |
| 873 | git_config(git_diff_basic_config, NULL); /* no "diff" UI options */ |
| 874 | opt.abbrev = 0; |
| 875 | opt.diff = 1; |
| 876 | |
| 877 | /* This is what "--pretty" does */ |
| 878 | opt.verbose_header = 1; |
| 879 | opt.use_terminator = 0; |
| 880 | opt.commit_format = CMIT_FMT_DEFAULT; |
| 881 | |
| 882 | /* diff-tree init */ |
| 883 | if (!opt.diffopt.output_format) |
| 884 | opt.diffopt.output_format = DIFF_FORMAT_RAW; |
| 885 | |
| 886 | log_tree_commit(&opt, commit); |
| 887 | } |
| 888 | |
| 889 | /* |
Christian Couder | ef24c7c | 2009-04-19 11:56:07 +0200 | [diff] [blame] | 890 | * We use the convention that exiting with an exit code 10 means that |
| 891 | * the bisection process finished successfully. |
| 892 | * In this case the calling shell script should exit 0. |
Jon Seymour | fee92fc | 2011-08-04 22:01:00 +1000 | [diff] [blame] | 893 | * |
| 894 | * If no_checkout is non-zero, the bisection process does not |
| 895 | * checkout the trial commit but instead simply updates BISECT_HEAD. |
Christian Couder | ef24c7c | 2009-04-19 11:56:07 +0200 | [diff] [blame] | 896 | */ |
Jon Seymour | fee92fc | 2011-08-04 22:01:00 +1000 | [diff] [blame] | 897 | int bisect_next_all(const char *prefix, int no_checkout) |
Christian Couder | ef24c7c | 2009-04-19 11:56:07 +0200 | [diff] [blame] | 898 | { |
| 899 | struct rev_info revs; |
| 900 | struct commit_list *tried; |
David Ripton | 6329bad | 2010-01-19 07:13:33 -0800 | [diff] [blame] | 901 | int reaches = 0, all = 0, nr, steps; |
Christian Couder | ef24c7c | 2009-04-19 11:56:07 +0200 | [diff] [blame] | 902 | const unsigned char *bisect_rev; |
| 903 | char bisect_rev_hex[41]; |
| 904 | |
Christian Couder | 2b02069 | 2009-05-09 17:55:42 +0200 | [diff] [blame] | 905 | if (read_bisect_refs()) |
| 906 | die("reading bisect refs failed"); |
| 907 | |
Jon Seymour | fee92fc | 2011-08-04 22:01:00 +1000 | [diff] [blame] | 908 | check_good_are_ancestors_of_bad(prefix, no_checkout); |
Christian Couder | 0871984 | 2009-05-09 17:55:47 +0200 | [diff] [blame] | 909 | |
Christian Couder | a22347c | 2009-05-17 17:36:44 +0200 | [diff] [blame] | 910 | bisect_rev_setup(&revs, prefix, "%s", "^%s", 1); |
| 911 | revs.limited = 1; |
Christian Couder | 2b02069 | 2009-05-09 17:55:42 +0200 | [diff] [blame] | 912 | |
Christian Couder | a22347c | 2009-05-17 17:36:44 +0200 | [diff] [blame] | 913 | bisect_common(&revs); |
Christian Couder | ef24c7c | 2009-04-19 11:56:07 +0200 | [diff] [blame] | 914 | |
Christian Couder | a22347c | 2009-05-17 17:36:44 +0200 | [diff] [blame] | 915 | revs.commits = find_bisection(revs.commits, &reaches, &all, |
Jeff King | 902bb36 | 2011-05-19 17:34:33 -0400 | [diff] [blame] | 916 | !!skipped_revs.nr); |
Christian Couder | 62d0b0d | 2009-06-06 06:41:34 +0200 | [diff] [blame] | 917 | revs.commits = managed_skipped(revs.commits, &tried); |
Christian Couder | ef24c7c | 2009-04-19 11:56:07 +0200 | [diff] [blame] | 918 | |
| 919 | if (!revs.commits) { |
| 920 | /* |
| 921 | * We should exit here only if the "bad" |
| 922 | * commit is also a "skip" commit. |
| 923 | */ |
| 924 | exit_if_skipped_commits(tried, NULL); |
| 925 | |
| 926 | printf("%s was both good and bad\n", |
| 927 | sha1_to_hex(current_bad_sha1)); |
| 928 | exit(1); |
| 929 | } |
| 930 | |
Christian Couder | 8f69f72 | 2010-02-28 23:19:09 +0100 | [diff] [blame] | 931 | if (!all) { |
| 932 | fprintf(stderr, "No testable commit found.\n" |
| 933 | "Maybe you started with bad path parameters?\n"); |
| 934 | exit(4); |
| 935 | } |
| 936 | |
Christian Couder | ef24c7c | 2009-04-19 11:56:07 +0200 | [diff] [blame] | 937 | bisect_rev = revs.commits->item->object.sha1; |
| 938 | memcpy(bisect_rev_hex, sha1_to_hex(bisect_rev), 41); |
| 939 | |
| 940 | if (!hashcmp(bisect_rev, current_bad_sha1)) { |
| 941 | exit_if_skipped_commits(tried, current_bad_sha1); |
Nanako Shiraishi | 21d0bc2 | 2009-08-26 17:38:50 +0900 | [diff] [blame] | 942 | printf("%s is the first bad commit\n", bisect_rev_hex); |
Christian Couder | e22278c | 2009-05-28 23:21:16 +0200 | [diff] [blame] | 943 | show_diff_tree(prefix, revs.commits->item); |
Christian Couder | ef24c7c | 2009-04-19 11:56:07 +0200 | [diff] [blame] | 944 | /* This means the bisection process succeeded. */ |
| 945 | exit(10); |
| 946 | } |
| 947 | |
| 948 | nr = all - reaches - 1; |
David Ripton | 6329bad | 2010-01-19 07:13:33 -0800 | [diff] [blame] | 949 | steps = estimate_bisect_steps(all); |
| 950 | printf("Bisecting: %d revision%s left to test after this " |
| 951 | "(roughly %d step%s)\n", nr, (nr == 1 ? "" : "s"), |
| 952 | steps, (steps == 1 ? "" : "s")); |
Christian Couder | ef24c7c | 2009-04-19 11:56:07 +0200 | [diff] [blame] | 953 | |
Jon Seymour | fee92fc | 2011-08-04 22:01:00 +1000 | [diff] [blame] | 954 | return bisect_checkout(bisect_rev_hex, no_checkout); |
Christian Couder | ef24c7c | 2009-04-19 11:56:07 +0200 | [diff] [blame] | 955 | } |
| 956 | |