bisect--helper: add "--next-exit" to output bisect results
[alt-git.git] / bisect.c
blob5902e83fac76cb419d62cfbbae09048872ba526e
1 #include "cache.h"
2 #include "commit.h"
3 #include "diff.h"
4 #include "revision.h"
5 #include "refs.h"
6 #include "list-objects.h"
7 #include "quote.h"
8 #include "sha1-lookup.h"
9 #include "run-command.h"
10 #include "bisect.h"
12 static unsigned char (*skipped_sha1)[20];
13 static int skipped_sha1_nr;
14 static int skipped_sha1_alloc;
16 static const char **rev_argv;
17 static int rev_argv_nr;
18 static int rev_argv_alloc;
20 static const unsigned char *current_bad_sha1;
22 static const char *argv_diff_tree[] = {"diff-tree", "--pretty", NULL, NULL};
23 static const char *argv_checkout[] = {"checkout", "-q", NULL, "--", NULL};
24 static const char *argv_show_branch[] = {"show-branch", NULL, NULL};
26 /* bits #0-15 in revision.h */
28 #define COUNTED (1u<<16)
31 * This is a truly stupid algorithm, but it's only
32 * used for bisection, and we just don't care enough.
34 * We care just barely enough to avoid recursing for
35 * non-merge entries.
37 static int count_distance(struct commit_list *entry)
39 int nr = 0;
41 while (entry) {
42 struct commit *commit = entry->item;
43 struct commit_list *p;
45 if (commit->object.flags & (UNINTERESTING | COUNTED))
46 break;
47 if (!(commit->object.flags & TREESAME))
48 nr++;
49 commit->object.flags |= COUNTED;
50 p = commit->parents;
51 entry = p;
52 if (p) {
53 p = p->next;
54 while (p) {
55 nr += count_distance(p);
56 p = p->next;
61 return nr;
64 static void clear_distance(struct commit_list *list)
66 while (list) {
67 struct commit *commit = list->item;
68 commit->object.flags &= ~COUNTED;
69 list = list->next;
73 #define DEBUG_BISECT 0
75 static inline int weight(struct commit_list *elem)
77 return *((int*)(elem->item->util));
80 static inline void weight_set(struct commit_list *elem, int weight)
82 *((int*)(elem->item->util)) = weight;
85 static int count_interesting_parents(struct commit *commit)
87 struct commit_list *p;
88 int count;
90 for (count = 0, p = commit->parents; p; p = p->next) {
91 if (p->item->object.flags & UNINTERESTING)
92 continue;
93 count++;
95 return count;
98 static inline int halfway(struct commit_list *p, int nr)
101 * Don't short-cut something we are not going to return!
103 if (p->item->object.flags & TREESAME)
104 return 0;
105 if (DEBUG_BISECT)
106 return 0;
108 * 2 and 3 are halfway of 5.
109 * 3 is halfway of 6 but 2 and 4 are not.
111 switch (2 * weight(p) - nr) {
112 case -1: case 0: case 1:
113 return 1;
114 default:
115 return 0;
119 #if !DEBUG_BISECT
120 #define show_list(a,b,c,d) do { ; } while (0)
121 #else
122 static void show_list(const char *debug, int counted, int nr,
123 struct commit_list *list)
125 struct commit_list *p;
127 fprintf(stderr, "%s (%d/%d)\n", debug, counted, nr);
129 for (p = list; p; p = p->next) {
130 struct commit_list *pp;
131 struct commit *commit = p->item;
132 unsigned flags = commit->object.flags;
133 enum object_type type;
134 unsigned long size;
135 char *buf = read_sha1_file(commit->object.sha1, &type, &size);
136 char *ep, *sp;
138 fprintf(stderr, "%c%c%c ",
139 (flags & TREESAME) ? ' ' : 'T',
140 (flags & UNINTERESTING) ? 'U' : ' ',
141 (flags & COUNTED) ? 'C' : ' ');
142 if (commit->util)
143 fprintf(stderr, "%3d", weight(p));
144 else
145 fprintf(stderr, "---");
146 fprintf(stderr, " %.*s", 8, sha1_to_hex(commit->object.sha1));
147 for (pp = commit->parents; pp; pp = pp->next)
148 fprintf(stderr, " %.*s", 8,
149 sha1_to_hex(pp->item->object.sha1));
151 sp = strstr(buf, "\n\n");
152 if (sp) {
153 sp += 2;
154 for (ep = sp; *ep && *ep != '\n'; ep++)
156 fprintf(stderr, " %.*s", (int)(ep - sp), sp);
158 fprintf(stderr, "\n");
161 #endif /* DEBUG_BISECT */
163 static struct commit_list *best_bisection(struct commit_list *list, int nr)
165 struct commit_list *p, *best;
166 int best_distance = -1;
168 best = list;
169 for (p = list; p; p = p->next) {
170 int distance;
171 unsigned flags = p->item->object.flags;
173 if (flags & TREESAME)
174 continue;
175 distance = weight(p);
176 if (nr - distance < distance)
177 distance = nr - distance;
178 if (distance > best_distance) {
179 best = p;
180 best_distance = distance;
184 return best;
187 struct commit_dist {
188 struct commit *commit;
189 int distance;
192 static int compare_commit_dist(const void *a_, const void *b_)
194 struct commit_dist *a, *b;
196 a = (struct commit_dist *)a_;
197 b = (struct commit_dist *)b_;
198 if (a->distance != b->distance)
199 return b->distance - a->distance; /* desc sort */
200 return hashcmp(a->commit->object.sha1, b->commit->object.sha1);
203 static struct commit_list *best_bisection_sorted(struct commit_list *list, int nr)
205 struct commit_list *p;
206 struct commit_dist *array = xcalloc(nr, sizeof(*array));
207 int cnt, i;
209 for (p = list, cnt = 0; p; p = p->next) {
210 int distance;
211 unsigned flags = p->item->object.flags;
213 if (flags & TREESAME)
214 continue;
215 distance = weight(p);
216 if (nr - distance < distance)
217 distance = nr - distance;
218 array[cnt].commit = p->item;
219 array[cnt].distance = distance;
220 cnt++;
222 qsort(array, cnt, sizeof(*array), compare_commit_dist);
223 for (p = list, i = 0; i < cnt; i++) {
224 struct name_decoration *r = xmalloc(sizeof(*r) + 100);
225 struct object *obj = &(array[i].commit->object);
227 sprintf(r->name, "dist=%d", array[i].distance);
228 r->next = add_decoration(&name_decoration, obj, r);
229 p->item = array[i].commit;
230 p = p->next;
232 if (p)
233 p->next = NULL;
234 free(array);
235 return list;
239 * zero or positive weight is the number of interesting commits it can
240 * reach, including itself. Especially, weight = 0 means it does not
241 * reach any tree-changing commits (e.g. just above uninteresting one
242 * but traversal is with pathspec).
244 * weight = -1 means it has one parent and its distance is yet to
245 * be computed.
247 * weight = -2 means it has more than one parent and its distance is
248 * unknown. After running count_distance() first, they will get zero
249 * or positive distance.
251 static struct commit_list *do_find_bisection(struct commit_list *list,
252 int nr, int *weights,
253 int find_all)
255 int n, counted;
256 struct commit_list *p;
258 counted = 0;
260 for (n = 0, p = list; p; p = p->next) {
261 struct commit *commit = p->item;
262 unsigned flags = commit->object.flags;
264 p->item->util = &weights[n++];
265 switch (count_interesting_parents(commit)) {
266 case 0:
267 if (!(flags & TREESAME)) {
268 weight_set(p, 1);
269 counted++;
270 show_list("bisection 2 count one",
271 counted, nr, list);
274 * otherwise, it is known not to reach any
275 * tree-changing commit and gets weight 0.
277 break;
278 case 1:
279 weight_set(p, -1);
280 break;
281 default:
282 weight_set(p, -2);
283 break;
287 show_list("bisection 2 initialize", counted, nr, list);
290 * If you have only one parent in the resulting set
291 * then you can reach one commit more than that parent
292 * can reach. So we do not have to run the expensive
293 * count_distance() for single strand of pearls.
295 * However, if you have more than one parents, you cannot
296 * just add their distance and one for yourself, since
297 * they usually reach the same ancestor and you would
298 * end up counting them twice that way.
300 * So we will first count distance of merges the usual
301 * way, and then fill the blanks using cheaper algorithm.
303 for (p = list; p; p = p->next) {
304 if (p->item->object.flags & UNINTERESTING)
305 continue;
306 if (weight(p) != -2)
307 continue;
308 weight_set(p, count_distance(p));
309 clear_distance(list);
311 /* Does it happen to be at exactly half-way? */
312 if (!find_all && halfway(p, nr))
313 return p;
314 counted++;
317 show_list("bisection 2 count_distance", counted, nr, list);
319 while (counted < nr) {
320 for (p = list; p; p = p->next) {
321 struct commit_list *q;
322 unsigned flags = p->item->object.flags;
324 if (0 <= weight(p))
325 continue;
326 for (q = p->item->parents; q; q = q->next) {
327 if (q->item->object.flags & UNINTERESTING)
328 continue;
329 if (0 <= weight(q))
330 break;
332 if (!q)
333 continue;
336 * weight for p is unknown but q is known.
337 * add one for p itself if p is to be counted,
338 * otherwise inherit it from q directly.
340 if (!(flags & TREESAME)) {
341 weight_set(p, weight(q)+1);
342 counted++;
343 show_list("bisection 2 count one",
344 counted, nr, list);
346 else
347 weight_set(p, weight(q));
349 /* Does it happen to be at exactly half-way? */
350 if (!find_all && halfway(p, nr))
351 return p;
355 show_list("bisection 2 counted all", counted, nr, list);
357 if (!find_all)
358 return best_bisection(list, nr);
359 else
360 return best_bisection_sorted(list, nr);
363 struct commit_list *find_bisection(struct commit_list *list,
364 int *reaches, int *all,
365 int find_all)
367 int nr, on_list;
368 struct commit_list *p, *best, *next, *last;
369 int *weights;
371 show_list("bisection 2 entry", 0, 0, list);
374 * Count the number of total and tree-changing items on the
375 * list, while reversing the list.
377 for (nr = on_list = 0, last = NULL, p = list;
379 p = next) {
380 unsigned flags = p->item->object.flags;
382 next = p->next;
383 if (flags & UNINTERESTING)
384 continue;
385 p->next = last;
386 last = p;
387 if (!(flags & TREESAME))
388 nr++;
389 on_list++;
391 list = last;
392 show_list("bisection 2 sorted", 0, nr, list);
394 *all = nr;
395 weights = xcalloc(on_list, sizeof(*weights));
397 /* Do the real work of finding bisection commit. */
398 best = do_find_bisection(list, nr, weights, find_all);
399 if (best) {
400 if (!find_all)
401 best->next = NULL;
402 *reaches = weight(best);
404 free(weights);
405 return best;
408 static int register_ref(const char *refname, const unsigned char *sha1,
409 int flags, void *cb_data)
411 if (!strcmp(refname, "bad")) {
412 ALLOC_GROW(rev_argv, rev_argv_nr + 1, rev_argv_alloc);
413 current_bad_sha1 = sha1;
414 rev_argv[rev_argv_nr++] = xstrdup(sha1_to_hex(sha1));
415 } else if (!prefixcmp(refname, "good-")) {
416 const char *hex = sha1_to_hex(sha1);
417 char *good = xmalloc(strlen(hex) + 2);
418 *good = '^';
419 memcpy(good + 1, hex, strlen(hex) + 1);
420 ALLOC_GROW(rev_argv, rev_argv_nr + 1, rev_argv_alloc);
421 rev_argv[rev_argv_nr++] = good;
422 } else if (!prefixcmp(refname, "skip-")) {
423 ALLOC_GROW(skipped_sha1, skipped_sha1_nr + 1,
424 skipped_sha1_alloc);
425 hashcpy(skipped_sha1[skipped_sha1_nr++], sha1);
428 return 0;
431 static int read_bisect_refs(void)
433 return for_each_ref_in("refs/bisect/", register_ref, NULL);
436 void read_bisect_paths(void)
438 struct strbuf str = STRBUF_INIT;
439 const char *filename = git_path("BISECT_NAMES");
440 FILE *fp = fopen(filename, "r");
442 if (!fp)
443 die("Could not open file '%s': %s", filename, strerror(errno));
445 while (strbuf_getline(&str, fp, '\n') != EOF) {
446 char *quoted;
447 int res;
449 strbuf_trim(&str);
450 quoted = strbuf_detach(&str, NULL);
451 res = sq_dequote_to_argv(quoted, &rev_argv,
452 &rev_argv_nr, &rev_argv_alloc);
453 if (res)
454 die("Badly quoted content in file '%s': %s",
455 filename, quoted);
458 strbuf_release(&str);
459 fclose(fp);
462 static int skipcmp(const void *a, const void *b)
464 return hashcmp(a, b);
467 static void prepare_skipped(void)
469 qsort(skipped_sha1, skipped_sha1_nr, sizeof(*skipped_sha1), skipcmp);
472 static const unsigned char *skipped_sha1_access(size_t index, void *table)
474 unsigned char (*skipped)[20] = table;
475 return skipped[index];
478 static int lookup_skipped(unsigned char *sha1)
480 return sha1_pos(sha1, skipped_sha1, skipped_sha1_nr,
481 skipped_sha1_access);
484 struct commit_list *filter_skipped(struct commit_list *list,
485 struct commit_list **tried,
486 int show_all)
488 struct commit_list *filtered = NULL, **f = &filtered;
490 *tried = NULL;
492 if (!skipped_sha1_nr)
493 return list;
495 prepare_skipped();
497 while (list) {
498 struct commit_list *next = list->next;
499 list->next = NULL;
500 if (0 <= lookup_skipped(list->item->object.sha1)) {
501 /* Move current to tried list */
502 *tried = list;
503 tried = &list->next;
504 } else {
505 if (!show_all)
506 return list;
507 /* Move current to filtered list */
508 *f = list;
509 f = &list->next;
511 list = next;
514 return filtered;
517 static void bisect_rev_setup(struct rev_info *revs, const char *prefix)
519 init_revisions(revs, prefix);
520 revs->abbrev = 0;
521 revs->commit_format = CMIT_FMT_UNSPECIFIED;
523 /* argv[0] will be ignored by setup_revisions */
524 ALLOC_GROW(rev_argv, rev_argv_nr + 1, rev_argv_alloc);
525 rev_argv[rev_argv_nr++] = xstrdup("bisect_rev_setup");
527 if (read_bisect_refs())
528 die("reading bisect refs failed");
530 ALLOC_GROW(rev_argv, rev_argv_nr + 1, rev_argv_alloc);
531 rev_argv[rev_argv_nr++] = xstrdup("--");
533 read_bisect_paths();
535 ALLOC_GROW(rev_argv, rev_argv_nr + 1, rev_argv_alloc);
536 rev_argv[rev_argv_nr++] = NULL;
538 setup_revisions(rev_argv_nr, rev_argv, revs, NULL);
540 revs->limited = 1;
543 static void bisect_common(struct rev_info *revs, const char *prefix,
544 int *reaches, int *all)
546 bisect_rev_setup(revs, prefix);
548 if (prepare_revision_walk(revs))
549 die("revision walk setup failed");
550 if (revs->tree_objects)
551 mark_edges_uninteresting(revs->commits, revs, NULL);
553 revs->commits = find_bisection(revs->commits, reaches, all,
554 !!skipped_sha1_nr);
557 int bisect_next_vars(const char *prefix)
559 struct rev_info revs;
560 struct rev_list_info info;
561 int reaches = 0, all = 0;
563 memset(&info, 0, sizeof(info));
564 info.revs = &revs;
565 info.bisect_show_flags = BISECT_SHOW_TRIED | BISECT_SHOW_STRINGED;
567 bisect_common(&revs, prefix, &reaches, &all);
569 return show_bisect_vars(&info, reaches, all);
572 static void exit_if_skipped_commits(struct commit_list *tried,
573 const unsigned char *bad)
575 if (!tried)
576 return;
578 printf("There are only 'skip'ped commits left to test.\n"
579 "The first bad commit could be any of:\n");
580 print_commit_list(tried, "%s\n", "%s\n");
581 if (bad)
582 printf("%s\n", sha1_to_hex(bad));
583 printf("We cannot bisect more!\n");
584 exit(2);
587 static void mark_expected_rev(char *bisect_rev_hex)
589 int len = strlen(bisect_rev_hex);
590 const char *filename = git_path("BISECT_EXPECTED_REV");
591 int fd = open(filename, O_CREAT | O_TRUNC | O_WRONLY, 0600);
593 if (fd < 0)
594 die("could not create file '%s': %s",
595 filename, strerror(errno));
597 bisect_rev_hex[len] = '\n';
598 write_or_die(fd, bisect_rev_hex, len + 1);
599 bisect_rev_hex[len] = '\0';
601 if (close(fd) < 0)
602 die("closing file %s: %s", filename, strerror(errno));
605 static int bisect_checkout(char *bisect_rev_hex)
607 int res;
609 mark_expected_rev(bisect_rev_hex);
611 argv_checkout[2] = bisect_rev_hex;
612 res = run_command_v_opt(argv_checkout, RUN_GIT_CMD);
613 if (res)
614 exit(res);
616 argv_show_branch[1] = bisect_rev_hex;
617 return run_command_v_opt(argv_show_branch, RUN_GIT_CMD);
621 * We use the convention that exiting with an exit code 10 means that
622 * the bisection process finished successfully.
623 * In this case the calling shell script should exit 0.
625 int bisect_next_exit(const char *prefix)
627 struct rev_info revs;
628 struct commit_list *tried;
629 int reaches = 0, all = 0, nr;
630 const unsigned char *bisect_rev;
631 char bisect_rev_hex[41];
633 bisect_common(&revs, prefix, &reaches, &all);
635 revs.commits = filter_skipped(revs.commits, &tried, 0);
637 if (!revs.commits) {
639 * We should exit here only if the "bad"
640 * commit is also a "skip" commit.
642 exit_if_skipped_commits(tried, NULL);
644 printf("%s was both good and bad\n",
645 sha1_to_hex(current_bad_sha1));
646 exit(1);
649 bisect_rev = revs.commits->item->object.sha1;
650 memcpy(bisect_rev_hex, sha1_to_hex(bisect_rev), 41);
652 if (!hashcmp(bisect_rev, current_bad_sha1)) {
653 exit_if_skipped_commits(tried, current_bad_sha1);
654 printf("%s is first bad commit\n", bisect_rev_hex);
655 argv_diff_tree[2] = bisect_rev_hex;
656 run_command_v_opt(argv_diff_tree, RUN_GIT_CMD);
657 /* This means the bisection process succeeded. */
658 exit(10);
661 nr = all - reaches - 1;
662 printf("Bisecting: %d revisions left to test after this "
663 "(roughly %d steps)\n", nr, estimate_bisect_steps(all));
665 return bisect_checkout(bisect_rev_hex);