bisect: store good revisions in a "sha1_array"
[git/dscho.git] / bisect.c
blob7976cbfcd791e0ddd69e04d8f0e2902661b4ec74
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 struct sha1_array {
13 unsigned char (*sha1)[20];
14 int sha1_nr;
15 int sha1_alloc;
18 static struct sha1_array good_revs;
19 static struct sha1_array skipped_revs;
21 static const char **rev_argv;
22 static int rev_argv_nr;
23 static int rev_argv_alloc;
25 static const unsigned char *current_bad_sha1;
27 static const char *argv_diff_tree[] = {"diff-tree", "--pretty", NULL, NULL};
28 static const char *argv_checkout[] = {"checkout", "-q", NULL, "--", NULL};
29 static const char *argv_show_branch[] = {"show-branch", NULL, NULL};
31 /* bits #0-15 in revision.h */
33 #define COUNTED (1u<<16)
36 * This is a truly stupid algorithm, but it's only
37 * used for bisection, and we just don't care enough.
39 * We care just barely enough to avoid recursing for
40 * non-merge entries.
42 static int count_distance(struct commit_list *entry)
44 int nr = 0;
46 while (entry) {
47 struct commit *commit = entry->item;
48 struct commit_list *p;
50 if (commit->object.flags & (UNINTERESTING | COUNTED))
51 break;
52 if (!(commit->object.flags & TREESAME))
53 nr++;
54 commit->object.flags |= COUNTED;
55 p = commit->parents;
56 entry = p;
57 if (p) {
58 p = p->next;
59 while (p) {
60 nr += count_distance(p);
61 p = p->next;
66 return nr;
69 static void clear_distance(struct commit_list *list)
71 while (list) {
72 struct commit *commit = list->item;
73 commit->object.flags &= ~COUNTED;
74 list = list->next;
78 #define DEBUG_BISECT 0
80 static inline int weight(struct commit_list *elem)
82 return *((int*)(elem->item->util));
85 static inline void weight_set(struct commit_list *elem, int weight)
87 *((int*)(elem->item->util)) = weight;
90 static int count_interesting_parents(struct commit *commit)
92 struct commit_list *p;
93 int count;
95 for (count = 0, p = commit->parents; p; p = p->next) {
96 if (p->item->object.flags & UNINTERESTING)
97 continue;
98 count++;
100 return count;
103 static inline int halfway(struct commit_list *p, int nr)
106 * Don't short-cut something we are not going to return!
108 if (p->item->object.flags & TREESAME)
109 return 0;
110 if (DEBUG_BISECT)
111 return 0;
113 * 2 and 3 are halfway of 5.
114 * 3 is halfway of 6 but 2 and 4 are not.
116 switch (2 * weight(p) - nr) {
117 case -1: case 0: case 1:
118 return 1;
119 default:
120 return 0;
124 #if !DEBUG_BISECT
125 #define show_list(a,b,c,d) do { ; } while (0)
126 #else
127 static void show_list(const char *debug, int counted, int nr,
128 struct commit_list *list)
130 struct commit_list *p;
132 fprintf(stderr, "%s (%d/%d)\n", debug, counted, nr);
134 for (p = list; p; p = p->next) {
135 struct commit_list *pp;
136 struct commit *commit = p->item;
137 unsigned flags = commit->object.flags;
138 enum object_type type;
139 unsigned long size;
140 char *buf = read_sha1_file(commit->object.sha1, &type, &size);
141 char *ep, *sp;
143 fprintf(stderr, "%c%c%c ",
144 (flags & TREESAME) ? ' ' : 'T',
145 (flags & UNINTERESTING) ? 'U' : ' ',
146 (flags & COUNTED) ? 'C' : ' ');
147 if (commit->util)
148 fprintf(stderr, "%3d", weight(p));
149 else
150 fprintf(stderr, "---");
151 fprintf(stderr, " %.*s", 8, sha1_to_hex(commit->object.sha1));
152 for (pp = commit->parents; pp; pp = pp->next)
153 fprintf(stderr, " %.*s", 8,
154 sha1_to_hex(pp->item->object.sha1));
156 sp = strstr(buf, "\n\n");
157 if (sp) {
158 sp += 2;
159 for (ep = sp; *ep && *ep != '\n'; ep++)
161 fprintf(stderr, " %.*s", (int)(ep - sp), sp);
163 fprintf(stderr, "\n");
166 #endif /* DEBUG_BISECT */
168 static struct commit_list *best_bisection(struct commit_list *list, int nr)
170 struct commit_list *p, *best;
171 int best_distance = -1;
173 best = list;
174 for (p = list; p; p = p->next) {
175 int distance;
176 unsigned flags = p->item->object.flags;
178 if (flags & TREESAME)
179 continue;
180 distance = weight(p);
181 if (nr - distance < distance)
182 distance = nr - distance;
183 if (distance > best_distance) {
184 best = p;
185 best_distance = distance;
189 return best;
192 struct commit_dist {
193 struct commit *commit;
194 int distance;
197 static int compare_commit_dist(const void *a_, const void *b_)
199 struct commit_dist *a, *b;
201 a = (struct commit_dist *)a_;
202 b = (struct commit_dist *)b_;
203 if (a->distance != b->distance)
204 return b->distance - a->distance; /* desc sort */
205 return hashcmp(a->commit->object.sha1, b->commit->object.sha1);
208 static struct commit_list *best_bisection_sorted(struct commit_list *list, int nr)
210 struct commit_list *p;
211 struct commit_dist *array = xcalloc(nr, sizeof(*array));
212 int cnt, i;
214 for (p = list, cnt = 0; p; p = p->next) {
215 int distance;
216 unsigned flags = p->item->object.flags;
218 if (flags & TREESAME)
219 continue;
220 distance = weight(p);
221 if (nr - distance < distance)
222 distance = nr - distance;
223 array[cnt].commit = p->item;
224 array[cnt].distance = distance;
225 cnt++;
227 qsort(array, cnt, sizeof(*array), compare_commit_dist);
228 for (p = list, i = 0; i < cnt; i++) {
229 struct name_decoration *r = xmalloc(sizeof(*r) + 100);
230 struct object *obj = &(array[i].commit->object);
232 sprintf(r->name, "dist=%d", array[i].distance);
233 r->next = add_decoration(&name_decoration, obj, r);
234 p->item = array[i].commit;
235 p = p->next;
237 if (p)
238 p->next = NULL;
239 free(array);
240 return list;
244 * zero or positive weight is the number of interesting commits it can
245 * reach, including itself. Especially, weight = 0 means it does not
246 * reach any tree-changing commits (e.g. just above uninteresting one
247 * but traversal is with pathspec).
249 * weight = -1 means it has one parent and its distance is yet to
250 * be computed.
252 * weight = -2 means it has more than one parent and its distance is
253 * unknown. After running count_distance() first, they will get zero
254 * or positive distance.
256 static struct commit_list *do_find_bisection(struct commit_list *list,
257 int nr, int *weights,
258 int find_all)
260 int n, counted;
261 struct commit_list *p;
263 counted = 0;
265 for (n = 0, p = list; p; p = p->next) {
266 struct commit *commit = p->item;
267 unsigned flags = commit->object.flags;
269 p->item->util = &weights[n++];
270 switch (count_interesting_parents(commit)) {
271 case 0:
272 if (!(flags & TREESAME)) {
273 weight_set(p, 1);
274 counted++;
275 show_list("bisection 2 count one",
276 counted, nr, list);
279 * otherwise, it is known not to reach any
280 * tree-changing commit and gets weight 0.
282 break;
283 case 1:
284 weight_set(p, -1);
285 break;
286 default:
287 weight_set(p, -2);
288 break;
292 show_list("bisection 2 initialize", counted, nr, list);
295 * If you have only one parent in the resulting set
296 * then you can reach one commit more than that parent
297 * can reach. So we do not have to run the expensive
298 * count_distance() for single strand of pearls.
300 * However, if you have more than one parents, you cannot
301 * just add their distance and one for yourself, since
302 * they usually reach the same ancestor and you would
303 * end up counting them twice that way.
305 * So we will first count distance of merges the usual
306 * way, and then fill the blanks using cheaper algorithm.
308 for (p = list; p; p = p->next) {
309 if (p->item->object.flags & UNINTERESTING)
310 continue;
311 if (weight(p) != -2)
312 continue;
313 weight_set(p, count_distance(p));
314 clear_distance(list);
316 /* Does it happen to be at exactly half-way? */
317 if (!find_all && halfway(p, nr))
318 return p;
319 counted++;
322 show_list("bisection 2 count_distance", counted, nr, list);
324 while (counted < nr) {
325 for (p = list; p; p = p->next) {
326 struct commit_list *q;
327 unsigned flags = p->item->object.flags;
329 if (0 <= weight(p))
330 continue;
331 for (q = p->item->parents; q; q = q->next) {
332 if (q->item->object.flags & UNINTERESTING)
333 continue;
334 if (0 <= weight(q))
335 break;
337 if (!q)
338 continue;
341 * weight for p is unknown but q is known.
342 * add one for p itself if p is to be counted,
343 * otherwise inherit it from q directly.
345 if (!(flags & TREESAME)) {
346 weight_set(p, weight(q)+1);
347 counted++;
348 show_list("bisection 2 count one",
349 counted, nr, list);
351 else
352 weight_set(p, weight(q));
354 /* Does it happen to be at exactly half-way? */
355 if (!find_all && halfway(p, nr))
356 return p;
360 show_list("bisection 2 counted all", counted, nr, list);
362 if (!find_all)
363 return best_bisection(list, nr);
364 else
365 return best_bisection_sorted(list, nr);
368 struct commit_list *find_bisection(struct commit_list *list,
369 int *reaches, int *all,
370 int find_all)
372 int nr, on_list;
373 struct commit_list *p, *best, *next, *last;
374 int *weights;
376 show_list("bisection 2 entry", 0, 0, list);
379 * Count the number of total and tree-changing items on the
380 * list, while reversing the list.
382 for (nr = on_list = 0, last = NULL, p = list;
384 p = next) {
385 unsigned flags = p->item->object.flags;
387 next = p->next;
388 if (flags & UNINTERESTING)
389 continue;
390 p->next = last;
391 last = p;
392 if (!(flags & TREESAME))
393 nr++;
394 on_list++;
396 list = last;
397 show_list("bisection 2 sorted", 0, nr, list);
399 *all = nr;
400 weights = xcalloc(on_list, sizeof(*weights));
402 /* Do the real work of finding bisection commit. */
403 best = do_find_bisection(list, nr, weights, find_all);
404 if (best) {
405 if (!find_all)
406 best->next = NULL;
407 *reaches = weight(best);
409 free(weights);
410 return best;
413 static void rev_argv_push(const unsigned char *sha1, const char *format)
415 struct strbuf buf = STRBUF_INIT;
417 strbuf_addf(&buf, format, sha1_to_hex(sha1));
418 ALLOC_GROW(rev_argv, rev_argv_nr + 1, rev_argv_alloc);
419 rev_argv[rev_argv_nr++] = strbuf_detach(&buf, NULL);
422 static void sha1_array_push(struct sha1_array *array,
423 const unsigned char *sha1)
425 ALLOC_GROW(array->sha1, array->sha1_nr + 1, array->sha1_alloc);
426 hashcpy(array->sha1[array->sha1_nr++], sha1);
429 static int register_ref(const char *refname, const unsigned char *sha1,
430 int flags, void *cb_data)
432 if (!strcmp(refname, "bad")) {
433 current_bad_sha1 = sha1;
434 } else if (!prefixcmp(refname, "good-")) {
435 sha1_array_push(&good_revs, sha1);
436 } else if (!prefixcmp(refname, "skip-")) {
437 sha1_array_push(&skipped_revs, sha1);
440 return 0;
443 static int read_bisect_refs(void)
445 return for_each_ref_in("refs/bisect/", register_ref, NULL);
448 void read_bisect_paths(void)
450 struct strbuf str = STRBUF_INIT;
451 const char *filename = git_path("BISECT_NAMES");
452 FILE *fp = fopen(filename, "r");
454 if (!fp)
455 die("Could not open file '%s': %s", filename, strerror(errno));
457 while (strbuf_getline(&str, fp, '\n') != EOF) {
458 char *quoted;
459 int res;
461 strbuf_trim(&str);
462 quoted = strbuf_detach(&str, NULL);
463 res = sq_dequote_to_argv(quoted, &rev_argv,
464 &rev_argv_nr, &rev_argv_alloc);
465 if (res)
466 die("Badly quoted content in file '%s': %s",
467 filename, quoted);
470 strbuf_release(&str);
471 fclose(fp);
474 static int skipcmp(const void *a, const void *b)
476 return hashcmp(a, b);
479 static void prepare_skipped(void)
481 qsort(skipped_revs.sha1, skipped_revs.sha1_nr,
482 sizeof(*skipped_revs.sha1), skipcmp);
485 static const unsigned char *skipped_sha1_access(size_t index, void *table)
487 unsigned char (*skipped)[20] = table;
488 return skipped[index];
491 static int lookup_skipped(unsigned char *sha1)
493 return sha1_pos(sha1, skipped_revs.sha1, skipped_revs.sha1_nr,
494 skipped_sha1_access);
497 struct commit_list *filter_skipped(struct commit_list *list,
498 struct commit_list **tried,
499 int show_all)
501 struct commit_list *filtered = NULL, **f = &filtered;
503 *tried = NULL;
505 if (!skipped_revs.sha1_nr)
506 return list;
508 prepare_skipped();
510 while (list) {
511 struct commit_list *next = list->next;
512 list->next = NULL;
513 if (0 <= lookup_skipped(list->item->object.sha1)) {
514 /* Move current to tried list */
515 *tried = list;
516 tried = &list->next;
517 } else {
518 if (!show_all)
519 return list;
520 /* Move current to filtered list */
521 *f = list;
522 f = &list->next;
524 list = next;
527 return filtered;
530 static void bisect_rev_setup(struct rev_info *revs, const char *prefix)
532 int i;
534 init_revisions(revs, prefix);
535 revs->abbrev = 0;
536 revs->commit_format = CMIT_FMT_UNSPECIFIED;
538 if (read_bisect_refs())
539 die("reading bisect refs failed");
541 /* argv[0] will be ignored by setup_revisions */
542 ALLOC_GROW(rev_argv, rev_argv_nr + 1, rev_argv_alloc);
543 rev_argv[rev_argv_nr++] = xstrdup("bisect_rev_setup");
545 rev_argv_push(current_bad_sha1, "%s");
547 for (i = 0; i < good_revs.sha1_nr; i++)
548 rev_argv_push(good_revs.sha1[i], "^%s");
550 ALLOC_GROW(rev_argv, rev_argv_nr + 1, rev_argv_alloc);
551 rev_argv[rev_argv_nr++] = xstrdup("--");
553 read_bisect_paths();
555 ALLOC_GROW(rev_argv, rev_argv_nr + 1, rev_argv_alloc);
556 rev_argv[rev_argv_nr++] = NULL;
558 setup_revisions(rev_argv_nr, rev_argv, revs, NULL);
560 revs->limited = 1;
563 static void bisect_common(struct rev_info *revs, const char *prefix,
564 int *reaches, int *all)
566 bisect_rev_setup(revs, prefix);
568 if (prepare_revision_walk(revs))
569 die("revision walk setup failed");
570 if (revs->tree_objects)
571 mark_edges_uninteresting(revs->commits, revs, NULL);
573 revs->commits = find_bisection(revs->commits, reaches, all,
574 !!skipped_revs.sha1_nr);
577 static void exit_if_skipped_commits(struct commit_list *tried,
578 const unsigned char *bad)
580 if (!tried)
581 return;
583 printf("There are only 'skip'ped commits left to test.\n"
584 "The first bad commit could be any of:\n");
585 print_commit_list(tried, "%s\n", "%s\n");
586 if (bad)
587 printf("%s\n", sha1_to_hex(bad));
588 printf("We cannot bisect more!\n");
589 exit(2);
592 static void mark_expected_rev(char *bisect_rev_hex)
594 int len = strlen(bisect_rev_hex);
595 const char *filename = git_path("BISECT_EXPECTED_REV");
596 int fd = open(filename, O_CREAT | O_TRUNC | O_WRONLY, 0600);
598 if (fd < 0)
599 die("could not create file '%s': %s",
600 filename, strerror(errno));
602 bisect_rev_hex[len] = '\n';
603 write_or_die(fd, bisect_rev_hex, len + 1);
604 bisect_rev_hex[len] = '\0';
606 if (close(fd) < 0)
607 die("closing file %s: %s", filename, strerror(errno));
610 static int bisect_checkout(char *bisect_rev_hex)
612 int res;
614 mark_expected_rev(bisect_rev_hex);
616 argv_checkout[2] = bisect_rev_hex;
617 res = run_command_v_opt(argv_checkout, RUN_GIT_CMD);
618 if (res)
619 exit(res);
621 argv_show_branch[1] = bisect_rev_hex;
622 return run_command_v_opt(argv_show_branch, RUN_GIT_CMD);
626 * We use the convention that exiting with an exit code 10 means that
627 * the bisection process finished successfully.
628 * In this case the calling shell script should exit 0.
630 int bisect_next_exit(const char *prefix)
632 struct rev_info revs;
633 struct commit_list *tried;
634 int reaches = 0, all = 0, nr;
635 const unsigned char *bisect_rev;
636 char bisect_rev_hex[41];
638 bisect_common(&revs, prefix, &reaches, &all);
640 revs.commits = filter_skipped(revs.commits, &tried, 0);
642 if (!revs.commits) {
644 * We should exit here only if the "bad"
645 * commit is also a "skip" commit.
647 exit_if_skipped_commits(tried, NULL);
649 printf("%s was both good and bad\n",
650 sha1_to_hex(current_bad_sha1));
651 exit(1);
654 bisect_rev = revs.commits->item->object.sha1;
655 memcpy(bisect_rev_hex, sha1_to_hex(bisect_rev), 41);
657 if (!hashcmp(bisect_rev, current_bad_sha1)) {
658 exit_if_skipped_commits(tried, current_bad_sha1);
659 printf("%s is first bad commit\n", bisect_rev_hex);
660 argv_diff_tree[2] = bisect_rev_hex;
661 run_command_v_opt(argv_diff_tree, RUN_GIT_CMD);
662 /* This means the bisection process succeeded. */
663 exit(10);
666 nr = all - reaches - 1;
667 printf("Bisecting: %d revisions left to test after this "
668 "(roughly %d steps)\n", nr, estimate_bisect_steps(all));
670 return bisect_checkout(bisect_rev_hex);