THIS IS A TEST INSTANCE ONLY! REPOSITORIES CAN BE DELETED AT ANY TIME!

Git Source Code Mirror - This is a publish-only repository and all pull requests are ignored. Please follow Documentation/SubmittingPatches procedure for any of your improvements.
git
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1093 lines
27KB

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