blob: 1be8e0a2711df9d29c1ba903fd4e8901379ea406 [file] [log] [blame]
Elijah Newrend812c3b2023-04-11 00:41:56 -07001#include "git-compat-util.h"
Brandon Williamsb2141fc2017-06-14 11:07:36 -07002#include "config.h"
Christian Coudera2ad79c2009-03-26 05:55:24 +01003#include "commit.h"
4#include "diff.h"
Elijah Newren32a8f512023-03-21 06:26:03 +00005#include "environment.h"
Elijah Newrenf394e092023-03-21 06:25:54 +00006#include "gettext.h"
Elijah Newren41771fa2023-02-24 00:09:27 +00007#include "hex.h"
Christian Coudera2ad79c2009-03-26 05:55:24 +01008#include "revision.h"
Christian Couder1bf072e2009-03-26 05:55:54 +01009#include "refs.h"
10#include "list-objects.h"
Christian Couder3b437b02009-03-26 05:55:59 +010011#include "quote.h"
Martin Ågrenbc626922020-12-31 12:56:23 +010012#include "hash-lookup.h"
Christian Couderef24c7c2009-04-19 11:56:07 +020013#include "run-command.h"
Christian Coudere22278c2009-05-28 23:21:16 +020014#include "log-tree.h"
Christian Coudera2ad79c2009-03-26 05:55:24 +010015#include "bisect.h"
Jeff Kingfe299ec2020-03-30 10:03:46 -040016#include "oid-array.h"
Jeff Kingdbbcd442020-07-28 16:23:39 -040017#include "strvec.h"
Nguyễn Thái Ngọc Duybb408ac2018-05-19 07:28:25 +020018#include "commit-slab.h"
Derrick Stolee64043552018-07-20 16:33:04 +000019#include "commit-reach.h"
Elijah Newrendabab1d2023-04-11 00:41:49 -070020#include "object-name.h"
Elijah Newrena034e912023-05-16 06:34:06 +000021#include "object-store-ll.h"
Elijah Newrenc3399322023-05-16 06:33:59 +000022#include "path.h"
Aaron Lipmane8861ff2020-08-07 17:58:37 -040023#include "dir.h"
Christian Couder6212b1a2009-05-09 17:55:38 +020024
brian m. carlson910650d2017-03-31 01:40:00 +000025static struct oid_array good_revs;
26static struct oid_array skipped_revs;
Christian Couder1bf072e2009-03-26 05:55:54 +010027
brian m. carlson3c5ff992015-03-13 23:39:29 +000028static struct object_id *current_bad_oid;
Christian Couderef24c7c2009-04-19 11:56:07 +020029
Antoine Delaite43f9d9f2015-06-29 17:40:29 +020030static const char *term_bad;
31static const char *term_good;
32
Nguyễn Thái Ngọc Duy208acbf2014-03-25 20:23:26 +070033/* Remember to update object flag allocation in object.h */
Christian Coudera2ad79c2009-03-26 05:55:24 +010034#define COUNTED (1u<<16)
35
36/*
37 * This is a truly stupid algorithm, but it's only
38 * used for bisection, and we just don't care enough.
39 *
40 * We care just barely enough to avoid recursing for
41 * non-merge entries.
42 */
43static int count_distance(struct commit_list *entry)
44{
45 int nr = 0;
46
47 while (entry) {
48 struct commit *commit = entry->item;
49 struct commit_list *p;
50
51 if (commit->object.flags & (UNINTERESTING | COUNTED))
52 break;
53 if (!(commit->object.flags & TREESAME))
54 nr++;
55 commit->object.flags |= COUNTED;
56 p = commit->parents;
57 entry = p;
58 if (p) {
59 p = p->next;
60 while (p) {
61 nr += count_distance(p);
62 p = p->next;
63 }
64 }
65 }
66
67 return nr;
68}
69
70static void clear_distance(struct commit_list *list)
71{
72 while (list) {
73 struct commit *commit = list->item;
74 commit->object.flags &= ~COUNTED;
75 list = list->next;
76 }
77}
78
Nguyễn Thái Ngọc Duybb408ac2018-05-19 07:28:25 +020079define_commit_slab(commit_weight, int *);
80static struct commit_weight commit_weight;
81
Christian Coudera2ad79c2009-03-26 05:55:24 +010082#define DEBUG_BISECT 0
83
84static inline int weight(struct commit_list *elem)
85{
Nguyễn Thái Ngọc Duybb408ac2018-05-19 07:28:25 +020086 return **commit_weight_at(&commit_weight, elem->item);
Christian Coudera2ad79c2009-03-26 05:55:24 +010087}
88
89static inline void weight_set(struct commit_list *elem, int weight)
90{
Nguyễn Thái Ngọc Duybb408ac2018-05-19 07:28:25 +020091 **commit_weight_at(&commit_weight, elem->item) = weight;
Christian Coudera2ad79c2009-03-26 05:55:24 +010092}
93
Aaron Lipmanad464a42020-08-07 17:58:38 -040094static int count_interesting_parents(struct commit *commit, unsigned bisect_flags)
Christian Coudera2ad79c2009-03-26 05:55:24 +010095{
96 struct commit_list *p;
97 int count;
98
99 for (count = 0, p = commit->parents; p; p = p->next) {
Aaron Lipman0fe305a2020-08-07 17:58:35 -0400100 if (!(p->item->object.flags & UNINTERESTING))
101 count++;
Aaron Lipmanad464a42020-08-07 17:58:38 -0400102 if (bisect_flags & FIND_BISECTION_FIRST_PARENT_ONLY)
Aaron Lipman0fe305a2020-08-07 17:58:35 -0400103 break;
Christian Coudera2ad79c2009-03-26 05:55:24 +0100104 }
105 return count;
106}
107
SZEDER Gábor0afcea72020-11-12 17:19:38 +0100108static inline int approx_halfway(struct commit_list *p, int nr)
Christian Coudera2ad79c2009-03-26 05:55:24 +0100109{
SZEDER Gábor0afcea72020-11-12 17:19:38 +0100110 int diff;
111
Christian Coudera2ad79c2009-03-26 05:55:24 +0100112 /*
113 * Don't short-cut something we are not going to return!
114 */
115 if (p->item->object.flags & TREESAME)
116 return 0;
117 if (DEBUG_BISECT)
118 return 0;
119 /*
SZEDER Gábor0afcea72020-11-12 17:19:38 +0100120 * For small number of commits 2 and 3 are halfway of 5, and
Christian Coudera2ad79c2009-03-26 05:55:24 +0100121 * 3 is halfway of 6 but 2 and 4 are not.
122 */
SZEDER Gábor0afcea72020-11-12 17:19:38 +0100123 diff = 2 * weight(p) - nr;
124 switch (diff) {
Christian Coudera2ad79c2009-03-26 05:55:24 +0100125 case -1: case 0: case 1:
126 return 1;
127 default:
SZEDER Gábor0afcea72020-11-12 17:19:38 +0100128 /*
129 * For large number of commits we are not so strict, it's
130 * good enough if it's within ~0.1% of the halfway point,
131 * e.g. 5000 is exactly halfway of 10000, but we consider
132 * the values [4996, 5004] as halfway as well.
133 */
134 if (abs(diff) < nr / 1024)
135 return 1;
Christian Coudera2ad79c2009-03-26 05:55:24 +0100136 return 0;
137 }
138}
139
Christian Coudera2ad79c2009-03-26 05:55:24 +0100140static void show_list(const char *debug, int counted, int nr,
141 struct commit_list *list)
142{
143 struct commit_list *p;
144
Nguyễn Thái Ngọc Duyb0eb92b2018-09-02 09:42:50 +0200145 if (!DEBUG_BISECT)
146 return;
147
Christian Coudera2ad79c2009-03-26 05:55:24 +0100148 fprintf(stderr, "%s (%d/%d)\n", debug, counted, nr);
149
150 for (p = list; p; p = p->next) {
151 struct commit_list *pp;
152 struct commit *commit = p->item;
Aaron Lipmanad464a42020-08-07 17:58:38 -0400153 unsigned commit_flags = commit->object.flags;
Christian Coudera2ad79c2009-03-26 05:55:24 +0100154 enum object_type type;
155 unsigned long size;
Ævar Arnfjörð Bjarmasonbc726bd2023-03-28 15:58:50 +0200156 char *buf = repo_read_object_file(the_repository,
157 &commit->object.oid, &type,
158 &size);
Christian Couder56ff3792010-07-22 15:18:33 +0200159 const char *subject_start;
160 int subject_len;
Christian Coudera2ad79c2009-03-26 05:55:24 +0100161
162 fprintf(stderr, "%c%c%c ",
Aaron Lipmanad464a42020-08-07 17:58:38 -0400163 (commit_flags & TREESAME) ? ' ' : 'T',
164 (commit_flags & UNINTERESTING) ? 'U' : ' ',
165 (commit_flags & COUNTED) ? 'C' : ' ');
Nguyễn Thái Ngọc Duyb0eb92b2018-09-02 09:42:50 +0200166 if (*commit_weight_at(&commit_weight, p->item))
Christian Coudera2ad79c2009-03-26 05:55:24 +0100167 fprintf(stderr, "%3d", weight(p));
168 else
169 fprintf(stderr, "---");
René Scharfe14ced552018-03-25 12:57:36 +0200170 fprintf(stderr, " %.*s", 8, oid_to_hex(&commit->object.oid));
Christian Coudera2ad79c2009-03-26 05:55:24 +0100171 for (pp = commit->parents; pp; pp = pp->next)
172 fprintf(stderr, " %.*s", 8,
René Scharfe14ced552018-03-25 12:57:36 +0200173 oid_to_hex(&pp->item->object.oid));
Christian Coudera2ad79c2009-03-26 05:55:24 +0100174
Christian Couder56ff3792010-07-22 15:18:33 +0200175 subject_len = find_commit_subject(buf, &subject_start);
176 if (subject_len)
177 fprintf(stderr, " %.*s", subject_len, subject_start);
Christian Coudera2ad79c2009-03-26 05:55:24 +0100178 fprintf(stderr, "\n");
179 }
180}
Christian Coudera2ad79c2009-03-26 05:55:24 +0100181
182static struct commit_list *best_bisection(struct commit_list *list, int nr)
183{
184 struct commit_list *p, *best;
185 int best_distance = -1;
186
187 best = list;
188 for (p = list; p; p = p->next) {
189 int distance;
Aaron Lipmanad464a42020-08-07 17:58:38 -0400190 unsigned commit_flags = p->item->object.flags;
Christian Coudera2ad79c2009-03-26 05:55:24 +0100191
Aaron Lipmanad464a42020-08-07 17:58:38 -0400192 if (commit_flags & TREESAME)
Christian Coudera2ad79c2009-03-26 05:55:24 +0100193 continue;
194 distance = weight(p);
195 if (nr - distance < distance)
196 distance = nr - distance;
197 if (distance > best_distance) {
198 best = p;
199 best_distance = distance;
200 }
201 }
202
203 return best;
204}
205
206struct commit_dist {
207 struct commit *commit;
208 int distance;
209};
210
211static int compare_commit_dist(const void *a_, const void *b_)
212{
213 struct commit_dist *a, *b;
214
215 a = (struct commit_dist *)a_;
216 b = (struct commit_dist *)b_;
217 if (a->distance != b->distance)
218 return b->distance - a->distance; /* desc sort */
brian m. carlsonf2fd0762015-11-10 02:22:28 +0000219 return oidcmp(&a->commit->object.oid, &b->commit->object.oid);
Christian Coudera2ad79c2009-03-26 05:55:24 +0100220}
221
222static struct commit_list *best_bisection_sorted(struct commit_list *list, int nr)
223{
224 struct commit_list *p;
225 struct commit_dist *array = xcalloc(nr, sizeof(*array));
Jeff King5b1ef2c2017-03-28 15:46:50 -0400226 struct strbuf buf = STRBUF_INIT;
Christian Coudera2ad79c2009-03-26 05:55:24 +0100227 int cnt, i;
228
229 for (p = list, cnt = 0; p; p = p->next) {
230 int distance;
Aaron Lipmanad464a42020-08-07 17:58:38 -0400231 unsigned commit_flags = p->item->object.flags;
Christian Coudera2ad79c2009-03-26 05:55:24 +0100232
Aaron Lipmanad464a42020-08-07 17:58:38 -0400233 if (commit_flags & TREESAME)
Christian Coudera2ad79c2009-03-26 05:55:24 +0100234 continue;
235 distance = weight(p);
236 if (nr - distance < distance)
237 distance = nr - distance;
238 array[cnt].commit = p->item;
239 array[cnt].distance = distance;
240 cnt++;
241 }
René Scharfe9ed0d8d2016-09-29 17:27:31 +0200242 QSORT(array, cnt, compare_commit_dist);
Christian Coudera2ad79c2009-03-26 05:55:24 +0100243 for (p = list, i = 0; i < cnt; i++) {
Christian Coudera2ad79c2009-03-26 05:55:24 +0100244 struct object *obj = &(array[i].commit->object);
245
Jeff King5b1ef2c2017-03-28 15:46:50 -0400246 strbuf_reset(&buf);
247 strbuf_addf(&buf, "dist=%d", array[i].distance);
248 add_name_decoration(DECORATION_NONE, buf.buf, obj);
Jeff King662174d2014-08-26 06:23:36 -0400249
Christian Coudera2ad79c2009-03-26 05:55:24 +0100250 p->item = array[i].commit;
Martin Ågren7c117182017-11-05 21:24:30 +0100251 if (i < cnt - 1)
252 p = p->next;
Christian Coudera2ad79c2009-03-26 05:55:24 +0100253 }
Ævar Arnfjörð Bjarmason2e9fdc72018-01-03 18:48:52 +0000254 if (p) {
255 free_commit_list(p->next);
256 p->next = NULL;
257 }
Jeff King5b1ef2c2017-03-28 15:46:50 -0400258 strbuf_release(&buf);
Christian Coudera2ad79c2009-03-26 05:55:24 +0100259 free(array);
260 return list;
261}
262
263/*
264 * zero or positive weight is the number of interesting commits it can
265 * reach, including itself. Especially, weight = 0 means it does not
266 * reach any tree-changing commits (e.g. just above uninteresting one
267 * but traversal is with pathspec).
268 *
269 * weight = -1 means it has one parent and its distance is yet to
270 * be computed.
271 *
272 * weight = -2 means it has more than one parent and its distance is
273 * unknown. After running count_distance() first, they will get zero
274 * or positive distance.
275 */
276static struct commit_list *do_find_bisection(struct commit_list *list,
277 int nr, int *weights,
Aaron Lipmanad464a42020-08-07 17:58:38 -0400278 unsigned bisect_flags)
Christian Coudera2ad79c2009-03-26 05:55:24 +0100279{
280 int n, counted;
281 struct commit_list *p;
282
283 counted = 0;
284
285 for (n = 0, p = list; p; p = p->next) {
286 struct commit *commit = p->item;
Aaron Lipmanad464a42020-08-07 17:58:38 -0400287 unsigned commit_flags = commit->object.flags;
Christian Coudera2ad79c2009-03-26 05:55:24 +0100288
Nguyễn Thái Ngọc Duybb408ac2018-05-19 07:28:25 +0200289 *commit_weight_at(&commit_weight, p->item) = &weights[n++];
Aaron Lipmanad464a42020-08-07 17:58:38 -0400290 switch (count_interesting_parents(commit, bisect_flags)) {
Christian Coudera2ad79c2009-03-26 05:55:24 +0100291 case 0:
Aaron Lipmanad464a42020-08-07 17:58:38 -0400292 if (!(commit_flags & TREESAME)) {
Christian Coudera2ad79c2009-03-26 05:55:24 +0100293 weight_set(p, 1);
294 counted++;
295 show_list("bisection 2 count one",
296 counted, nr, list);
297 }
298 /*
299 * otherwise, it is known not to reach any
300 * tree-changing commit and gets weight 0.
301 */
302 break;
303 case 1:
304 weight_set(p, -1);
305 break;
306 default:
307 weight_set(p, -2);
308 break;
309 }
310 }
311
312 show_list("bisection 2 initialize", counted, nr, list);
313
314 /*
315 * If you have only one parent in the resulting set
316 * then you can reach one commit more than that parent
317 * can reach. So we do not have to run the expensive
318 * count_distance() for single strand of pearls.
319 *
320 * However, if you have more than one parents, you cannot
321 * just add their distance and one for yourself, since
322 * they usually reach the same ancestor and you would
323 * end up counting them twice that way.
324 *
325 * So we will first count distance of merges the usual
326 * way, and then fill the blanks using cheaper algorithm.
327 */
328 for (p = list; p; p = p->next) {
329 if (p->item->object.flags & UNINTERESTING)
330 continue;
331 if (weight(p) != -2)
332 continue;
Aaron Lipmanad464a42020-08-07 17:58:38 -0400333 if (bisect_flags & FIND_BISECTION_FIRST_PARENT_ONLY)
Aaron Lipman0fe305a2020-08-07 17:58:35 -0400334 BUG("shouldn't be calling count-distance in fp mode");
Christian Coudera2ad79c2009-03-26 05:55:24 +0100335 weight_set(p, count_distance(p));
336 clear_distance(list);
337
SZEDER Gábor0afcea72020-11-12 17:19:38 +0100338 /* Does it happen to be at half-way? */
339 if (!(bisect_flags & FIND_BISECTION_ALL) &&
340 approx_halfway(p, nr))
Christian Coudera2ad79c2009-03-26 05:55:24 +0100341 return p;
342 counted++;
343 }
344
345 show_list("bisection 2 count_distance", counted, nr, list);
346
347 while (counted < nr) {
348 for (p = list; p; p = p->next) {
349 struct commit_list *q;
Aaron Lipmanad464a42020-08-07 17:58:38 -0400350 unsigned commit_flags = p->item->object.flags;
Christian Coudera2ad79c2009-03-26 05:55:24 +0100351
352 if (0 <= weight(p))
353 continue;
Aaron Lipman0fe305a2020-08-07 17:58:35 -0400354
355 for (q = p->item->parents;
356 q;
Aaron Lipmanad464a42020-08-07 17:58:38 -0400357 q = bisect_flags & FIND_BISECTION_FIRST_PARENT_ONLY ? NULL : q->next) {
Christian Coudera2ad79c2009-03-26 05:55:24 +0100358 if (q->item->object.flags & UNINTERESTING)
359 continue;
360 if (0 <= weight(q))
361 break;
362 }
363 if (!q)
364 continue;
365
366 /*
367 * weight for p is unknown but q is known.
368 * add one for p itself if p is to be counted,
369 * otherwise inherit it from q directly.
370 */
Aaron Lipmanad464a42020-08-07 17:58:38 -0400371 if (!(commit_flags & TREESAME)) {
Christian Coudera2ad79c2009-03-26 05:55:24 +0100372 weight_set(p, weight(q)+1);
373 counted++;
374 show_list("bisection 2 count one",
375 counted, nr, list);
376 }
377 else
378 weight_set(p, weight(q));
379
SZEDER Gábor0afcea72020-11-12 17:19:38 +0100380 /* Does it happen to be at half-way? */
381 if (!(bisect_flags & FIND_BISECTION_ALL) &&
382 approx_halfway(p, nr))
Christian Coudera2ad79c2009-03-26 05:55:24 +0100383 return p;
384 }
385 }
386
387 show_list("bisection 2 counted all", counted, nr, list);
388
Aaron Lipmanad464a42020-08-07 17:58:38 -0400389 if (!(bisect_flags & FIND_BISECTION_ALL))
Christian Coudera2ad79c2009-03-26 05:55:24 +0100390 return best_bisection(list, nr);
391 else
392 return best_bisection_sorted(list, nr);
393}
394
Martin Ågren24d707f2017-11-05 21:24:28 +0100395void find_bisection(struct commit_list **commit_list, int *reaches,
Aaron Lipmanad464a42020-08-07 17:58:38 -0400396 int *all, unsigned bisect_flags)
Christian Coudera2ad79c2009-03-26 05:55:24 +0100397{
398 int nr, on_list;
Martin Ågren24d707f2017-11-05 21:24:28 +0100399 struct commit_list *list, *p, *best, *next, *last;
Christian Coudera2ad79c2009-03-26 05:55:24 +0100400 int *weights;
401
Martin Ågren24d707f2017-11-05 21:24:28 +0100402 show_list("bisection 2 entry", 0, 0, *commit_list);
Nguyễn Thái Ngọc Duybb408ac2018-05-19 07:28:25 +0200403 init_commit_weight(&commit_weight);
Christian Coudera2ad79c2009-03-26 05:55:24 +0100404
405 /*
406 * Count the number of total and tree-changing items on the
407 * list, while reversing the list.
408 */
Martin Ågren24d707f2017-11-05 21:24:28 +0100409 for (nr = on_list = 0, last = NULL, p = *commit_list;
Christian Coudera2ad79c2009-03-26 05:55:24 +0100410 p;
411 p = next) {
Aaron Lipmanad464a42020-08-07 17:58:38 -0400412 unsigned commit_flags = p->item->object.flags;
Christian Coudera2ad79c2009-03-26 05:55:24 +0100413
414 next = p->next;
Aaron Lipmanad464a42020-08-07 17:58:38 -0400415 if (commit_flags & UNINTERESTING) {
Martin Ågrenfc5c40b2017-11-05 21:24:29 +0100416 free(p);
Christian Coudera2ad79c2009-03-26 05:55:24 +0100417 continue;
Martin Ågrenfc5c40b2017-11-05 21:24:29 +0100418 }
Christian Coudera2ad79c2009-03-26 05:55:24 +0100419 p->next = last;
420 last = p;
Aaron Lipmanad464a42020-08-07 17:58:38 -0400421 if (!(commit_flags & TREESAME))
Christian Coudera2ad79c2009-03-26 05:55:24 +0100422 nr++;
423 on_list++;
424 }
425 list = last;
426 show_list("bisection 2 sorted", 0, nr, list);
427
428 *all = nr;
René Scharfeca56dad2021-03-13 17:17:22 +0100429 CALLOC_ARRAY(weights, on_list);
Christian Coudera2ad79c2009-03-26 05:55:24 +0100430
431 /* Do the real work of finding bisection commit. */
Aaron Lipmanad464a42020-08-07 17:58:38 -0400432 best = do_find_bisection(list, nr, weights, bisect_flags);
Christian Coudera2ad79c2009-03-26 05:55:24 +0100433 if (best) {
Aaron Lipmanad464a42020-08-07 17:58:38 -0400434 if (!(bisect_flags & FIND_BISECTION_ALL)) {
Martin Ågrenf4e45cb2017-11-05 21:24:31 +0100435 list->item = best->item;
436 free_commit_list(list->next);
437 best = list;
Christian Coudera2ad79c2009-03-26 05:55:24 +0100438 best->next = NULL;
Martin Ågrenf4e45cb2017-11-05 21:24:31 +0100439 }
Christian Coudera2ad79c2009-03-26 05:55:24 +0100440 *reaches = weight(best);
441 }
442 free(weights);
Martin Ågren24d707f2017-11-05 21:24:28 +0100443 *commit_list = best;
Nguyễn Thái Ngọc Duybb408ac2018-05-19 07:28:25 +0200444 clear_commit_weight(&commit_weight);
Christian Coudera2ad79c2009-03-26 05:55:24 +0100445}
446
Michael Haggertyeed25142015-05-25 18:38:31 +0000447static int register_ref(const char *refname, const struct object_id *oid,
Ævar Arnfjörð Bjarmason5cf88fd2022-08-25 19:09:48 +0200448 int flags UNUSED, void *cb_data UNUSED)
Christian Couder1bf072e2009-03-26 05:55:54 +0100449{
Antoine Delaite43f9d9f2015-06-29 17:40:29 +0200450 struct strbuf good_prefix = STRBUF_INIT;
451 strbuf_addstr(&good_prefix, term_good);
452 strbuf_addstr(&good_prefix, "-");
453
454 if (!strcmp(refname, term_bad)) {
brian m. carlson3c5ff992015-03-13 23:39:29 +0000455 current_bad_oid = xmalloc(sizeof(*current_bad_oid));
Michael Haggertyeed25142015-05-25 18:38:31 +0000456 oidcpy(current_bad_oid, oid);
Antoine Delaite43f9d9f2015-06-29 17:40:29 +0200457 } else if (starts_with(refname, good_prefix.buf)) {
brian m. carlson910650d2017-03-31 01:40:00 +0000458 oid_array_append(&good_revs, oid);
Christian Couder59556542013-11-30 21:55:40 +0100459 } else if (starts_with(refname, "skip-")) {
brian m. carlson910650d2017-03-31 01:40:00 +0000460 oid_array_append(&skipped_revs, oid);
Christian Couder1bf072e2009-03-26 05:55:54 +0100461 }
462
Antoine Delaite43f9d9f2015-06-29 17:40:29 +0200463 strbuf_release(&good_prefix);
464
Christian Couder1bf072e2009-03-26 05:55:54 +0100465 return 0;
466}
467
468static int read_bisect_refs(void)
469{
Michael Haggertyeed25142015-05-25 18:38:31 +0000470 return for_each_ref_in("refs/bisect/", register_ref, NULL);
Christian Couder1bf072e2009-03-26 05:55:54 +0100471}
472
Jeff Kingf9327292015-08-10 05:38:57 -0400473static GIT_PATH_FUNC(git_path_bisect_names, "BISECT_NAMES")
474static GIT_PATH_FUNC(git_path_bisect_expected_rev, "BISECT_EXPECTED_REV")
Pranit Bauvafb71a322017-09-29 06:49:39 +0000475static GIT_PATH_FUNC(git_path_bisect_ancestors_ok, "BISECT_ANCESTORS_OK")
476static GIT_PATH_FUNC(git_path_bisect_run, "BISECT_RUN")
477static GIT_PATH_FUNC(git_path_bisect_start, "BISECT_START")
478static GIT_PATH_FUNC(git_path_bisect_log, "BISECT_LOG")
Jeff Kingf5d284f2017-04-20 17:08:25 -0400479static GIT_PATH_FUNC(git_path_bisect_terms, "BISECT_TERMS")
Aaron Lipmane8861ff2020-08-07 17:58:37 -0400480static GIT_PATH_FUNC(git_path_bisect_first_parent, "BISECT_FIRST_PARENT")
Jeff Kingf9327292015-08-10 05:38:57 -0400481
Jeff Kingef8d7ac2020-07-28 16:24:53 -0400482static void read_bisect_paths(struct strvec *array)
Christian Couder3b437b02009-03-26 05:55:59 +0100483{
484 struct strbuf str = STRBUF_INIT;
Jeff Kingf9327292015-08-10 05:38:57 -0400485 const char *filename = git_path_bisect_names();
Nguyễn Thái Ngọc Duy23a9e072017-05-03 17:16:46 +0700486 FILE *fp = xfopen(filename, "r");
Christian Couder3b437b02009-03-26 05:55:59 +0100487
Junio C Hamano8f309ae2016-01-13 15:31:17 -0800488 while (strbuf_getline_lf(&str, fp) != EOF) {
Christian Couder3b437b02009-03-26 05:55:59 +0100489 strbuf_trim(&str);
Jeff King2745b6b2020-07-28 16:24:02 -0400490 if (sq_dequote_to_strvec(str.buf, array))
Vasco Almeida14dc4892016-06-17 20:21:12 +0000491 die(_("Badly quoted content in file '%s': %s"),
Jeff King8a534b62011-09-13 17:58:14 -0400492 filename, str.buf);
Christian Couder3b437b02009-03-26 05:55:59 +0100493 }
494
495 strbuf_release(&str);
496 fclose(fp);
497}
498
Jeff King7383b252020-03-30 10:04:06 -0400499static char *join_oid_array_hex(struct oid_array *array, char delim)
Christian Couderc0537662009-05-09 17:55:45 +0200500{
501 struct strbuf joined_hexs = STRBUF_INIT;
502 int i;
503
Jeff King902bb362011-05-19 17:34:33 -0400504 for (i = 0; i < array->nr; i++) {
brian m. carlsonee3051b2017-03-26 16:01:37 +0000505 strbuf_addstr(&joined_hexs, oid_to_hex(array->oid + i));
Jeff King902bb362011-05-19 17:34:33 -0400506 if (i + 1 < array->nr)
Christian Couderc0537662009-05-09 17:55:45 +0200507 strbuf_addch(&joined_hexs, delim);
508 }
509
510 return strbuf_detach(&joined_hexs, NULL);
511}
512
Christian Couder9af35892009-06-06 06:41:33 +0200513/*
514 * In this function, passing a not NULL skipped_first is very special.
515 * It means that we want to know if the first commit in the list is
516 * skipped because we will want to test a commit away from it if it is
517 * indeed skipped.
518 * So if the first commit is skipped, we cannot take the shortcut to
519 * just "return list" when we find the first non skipped commit, we
520 * have to return a fully filtered list.
521 *
522 * We use (*skipped_first == -1) to mean "it has been found that the
523 * first commit is not skipped". In this case *skipped_first is set back
524 * to 0 just before the function returns.
525 */
Christian Couder95188642009-03-26 05:55:49 +0100526struct commit_list *filter_skipped(struct commit_list *list,
527 struct commit_list **tried,
Christian Couder9af35892009-06-06 06:41:33 +0200528 int show_all,
529 int *count,
530 int *skipped_first)
Christian Couder95188642009-03-26 05:55:49 +0100531{
532 struct commit_list *filtered = NULL, **f = &filtered;
533
534 *tried = NULL;
535
Christian Couder9af35892009-06-06 06:41:33 +0200536 if (skipped_first)
537 *skipped_first = 0;
538 if (count)
539 *count = 0;
540
Jeff King902bb362011-05-19 17:34:33 -0400541 if (!skipped_revs.nr)
Christian Couder95188642009-03-26 05:55:49 +0100542 return list;
543
Christian Couder95188642009-03-26 05:55:49 +0100544 while (list) {
545 struct commit_list *next = list->next;
546 list->next = NULL;
brian m. carlson910650d2017-03-31 01:40:00 +0000547 if (0 <= oid_array_lookup(&skipped_revs, &list->item->object.oid)) {
Christian Couder9af35892009-06-06 06:41:33 +0200548 if (skipped_first && !*skipped_first)
549 *skipped_first = 1;
Christian Couder95188642009-03-26 05:55:49 +0100550 /* Move current to tried list */
551 *tried = list;
552 tried = &list->next;
553 } else {
Christian Couder9af35892009-06-06 06:41:33 +0200554 if (!show_all) {
555 if (!skipped_first || !*skipped_first)
556 return list;
557 } else if (skipped_first && !*skipped_first) {
558 /* This means we know it's not skipped */
559 *skipped_first = -1;
560 }
Christian Couder95188642009-03-26 05:55:49 +0100561 /* Move current to filtered list */
562 *f = list;
563 f = &list->next;
Christian Couder9af35892009-06-06 06:41:33 +0200564 if (count)
565 (*count)++;
Christian Couder95188642009-03-26 05:55:49 +0100566 }
567 list = next;
568 }
569
Christian Couder9af35892009-06-06 06:41:33 +0200570 if (skipped_first && *skipped_first == -1)
571 *skipped_first = 0;
572
Christian Couder95188642009-03-26 05:55:49 +0100573 return filtered;
574}
Christian Couder1bf072e2009-03-26 05:55:54 +0100575
Christian Couderebc95292009-06-13 07:21:06 +0200576#define PRN_MODULO 32768
577
578/*
579 * This is a pseudo random number generator based on "man 3 rand".
580 * It is not used properly because the seed is the argument and it
581 * is increased by one between each call, but that should not matter
582 * for this application.
583 */
Nguyễn Thái Ngọc Duy3b335762018-12-09 11:25:21 +0100584static unsigned get_prn(unsigned count)
585{
Christian Couderebc95292009-06-13 07:21:06 +0200586 count = count * 1103515245 + 12345;
John Keeping7b96d882013-04-03 20:17:55 +0100587 return (count/65536) % PRN_MODULO;
Christian Couderebc95292009-06-13 07:21:06 +0200588}
589
590/*
591 * Custom integer square root from
Sven Strickroth5e687292017-05-13 11:54:51 +0200592 * https://en.wikipedia.org/wiki/Integer_square_root
Christian Couderebc95292009-06-13 07:21:06 +0200593 */
594static int sqrti(int val)
Christian Couder62d0b0d2009-06-06 06:41:34 +0200595{
Christian Couderebc95292009-06-13 07:21:06 +0200596 float d, x = val;
597
Miriam Rubiob8e3b2f2020-02-17 09:40:30 +0100598 if (!val)
Christian Couderebc95292009-06-13 07:21:06 +0200599 return 0;
600
601 do {
602 float y = (x + (float)val / x) / 2;
603 d = (y > x) ? y - x : x - y;
604 x = y;
605 } while (d >= 0.5);
606
607 return (int)x;
608}
609
610static struct commit_list *skip_away(struct commit_list *list, int count)
611{
Christian Couder62d0b0d2009-06-06 06:41:34 +0200612 struct commit_list *cur, *previous;
Christian Couderebc95292009-06-13 07:21:06 +0200613 int prn, index, i;
614
615 prn = get_prn(count);
616 index = (count * prn / PRN_MODULO) * sqrti(prn) / sqrti(PRN_MODULO);
Christian Couder62d0b0d2009-06-06 06:41:34 +0200617
618 cur = list;
619 previous = NULL;
Christian Couder62d0b0d2009-06-06 06:41:34 +0200620
621 for (i = 0; cur; cur = cur->next, i++) {
622 if (i == index) {
Jeff King9001dc22018-08-28 17:22:48 -0400623 if (!oideq(&cur->item->object.oid, current_bad_oid))
Christian Couder62d0b0d2009-06-06 06:41:34 +0200624 return cur;
625 if (previous)
626 return previous;
627 return list;
628 }
629 previous = cur;
630 }
631
632 return list;
633}
634
635static struct commit_list *managed_skipped(struct commit_list *list,
636 struct commit_list **tried)
637{
638 int count, skipped_first;
Christian Couder62d0b0d2009-06-06 06:41:34 +0200639
640 *tried = NULL;
641
Jeff King902bb362011-05-19 17:34:33 -0400642 if (!skipped_revs.nr)
Christian Couder62d0b0d2009-06-06 06:41:34 +0200643 return list;
644
645 list = filter_skipped(list, tried, 0, &count, &skipped_first);
646
647 if (!skipped_first)
648 return list;
649
Christian Couderebc95292009-06-13 07:21:06 +0200650 return skip_away(list, count);
Christian Couder62d0b0d2009-06-06 06:41:34 +0200651}
652
Nguyễn Thái Ngọc Duy69d2cfe2018-11-10 06:48:59 +0100653static void bisect_rev_setup(struct repository *r, struct rev_info *revs,
Ævar Arnfjörð Bjarmason57efebb2022-08-02 17:33:15 +0200654 struct strvec *rev_argv,
Nguyễn Thái Ngọc Duy69d2cfe2018-11-10 06:48:59 +0100655 const char *prefix,
Christian Coudera22347c2009-05-17 17:36:44 +0200656 const char *bad_format, const char *good_format,
657 int read_paths)
Christian Couder1bf072e2009-03-26 05:55:54 +0100658{
Ævar Arnfjörð Bjarmasonf92dbdb2022-08-02 17:33:16 +0200659 struct setup_revision_opt opt = {
660 .free_removed_argv_elements = 1,
661 };
Christian Couderfad2d312009-05-09 17:55:40 +0200662 int i;
663
Nguyễn Thái Ngọc Duy69d2cfe2018-11-10 06:48:59 +0100664 repo_init_revisions(r, revs, prefix);
Christian Couder3b437b02009-03-26 05:55:59 +0100665 revs->abbrev = 0;
666 revs->commit_format = CMIT_FMT_UNSPECIFIED;
Christian Couder1bf072e2009-03-26 05:55:54 +0100667
Christian Couder1c953a12009-05-09 17:55:41 +0200668 /* rev_argv.argv[0] will be ignored by setup_revisions */
Ævar Arnfjörð Bjarmason57efebb2022-08-02 17:33:15 +0200669 strvec_push(rev_argv, "bisect_rev_setup");
670 strvec_pushf(rev_argv, bad_format, oid_to_hex(current_bad_oid));
Jeff King902bb362011-05-19 17:34:33 -0400671 for (i = 0; i < good_revs.nr; i++)
Ævar Arnfjörð Bjarmason57efebb2022-08-02 17:33:15 +0200672 strvec_pushf(rev_argv, good_format,
Jeff Kingf6d89422020-07-28 16:26:31 -0400673 oid_to_hex(good_revs.oid + i));
Ævar Arnfjörð Bjarmason57efebb2022-08-02 17:33:15 +0200674 strvec_push(rev_argv, "--");
Christian Coudera22347c2009-05-17 17:36:44 +0200675 if (read_paths)
Ævar Arnfjörð Bjarmason57efebb2022-08-02 17:33:15 +0200676 read_bisect_paths(rev_argv);
Christian Couder1bf072e2009-03-26 05:55:54 +0100677
Ævar Arnfjörð Bjarmasonf92dbdb2022-08-02 17:33:16 +0200678 setup_revisions(rev_argv->nr, rev_argv->v, revs, &opt);
Christian Couder3b437b02009-03-26 05:55:59 +0100679}
680
Christian Coudera22347c2009-05-17 17:36:44 +0200681static void bisect_common(struct rev_info *revs)
Christian Couder2ace9722009-04-19 11:55:57 +0200682{
Christian Couder2ace9722009-04-19 11:55:57 +0200683 if (prepare_revision_walk(revs))
684 die("revision walk setup failed");
685 if (revs->tree_objects)
Derrick Stolee4f6d26b2019-01-16 10:25:58 -0800686 mark_edges_uninteresting(revs, NULL, 0);
Christian Couder2ace9722009-04-19 11:55:57 +0200687}
688
Pranit Bauvace58b5d2020-02-17 09:40:34 +0100689static enum bisect_error error_if_skipped_commits(struct commit_list *tried,
brian m. carlson3c5ff992015-03-13 23:39:29 +0000690 const struct object_id *bad)
Christian Couderef24c7c2009-04-19 11:56:07 +0200691{
692 if (!tried)
Pranit Bauvace58b5d2020-02-17 09:40:34 +0100693 return BISECT_OK;
Christian Couderef24c7c2009-04-19 11:56:07 +0200694
695 printf("There are only 'skip'ped commits left to test.\n"
Antoine Delaite43f9d9f2015-06-29 17:40:29 +0200696 "The first %s commit could be any of:\n", term_bad);
Junio C Hamano54307ea2016-07-08 10:09:28 -0700697
698 for ( ; tried; tried = tried->next)
699 printf("%s\n", oid_to_hex(&tried->item->object.oid));
700
Christian Couderef24c7c2009-04-19 11:56:07 +0200701 if (bad)
brian m. carlson3c5ff992015-03-13 23:39:29 +0000702 printf("%s\n", oid_to_hex(bad));
Vasco Almeida14dc4892016-06-17 20:21:12 +0000703 printf(_("We cannot bisect more!\n"));
Pranit Bauvace58b5d2020-02-17 09:40:34 +0100704
705 return BISECT_ONLY_SKIPPED_LEFT;
Christian Couderef24c7c2009-04-19 11:56:07 +0200706}
707
brian m. carlson3c5ff992015-03-13 23:39:29 +0000708static int is_expected_rev(const struct object_id *oid)
Christian Couderc0537662009-05-09 17:55:45 +0200709{
Jeff Kingf9327292015-08-10 05:38:57 -0400710 const char *filename = git_path_bisect_expected_rev();
Christian Couderc0537662009-05-09 17:55:45 +0200711 struct stat st;
712 struct strbuf str = STRBUF_INIT;
713 FILE *fp;
714 int res = 0;
715
716 if (stat(filename, &st) || !S_ISREG(st.st_mode))
717 return 0;
718
Nguyễn Thái Ngọc Duye9d983f2017-05-03 17:16:50 +0700719 fp = fopen_or_warn(filename, "r");
Christian Couderc0537662009-05-09 17:55:45 +0200720 if (!fp)
721 return 0;
722
Junio C Hamano8f309ae2016-01-13 15:31:17 -0800723 if (strbuf_getline_lf(&str, fp) != EOF)
brian m. carlson3c5ff992015-03-13 23:39:29 +0000724 res = !strcmp(str.buf, oid_to_hex(oid));
Christian Couderc0537662009-05-09 17:55:45 +0200725
726 strbuf_release(&str);
727 fclose(fp);
728
729 return res;
730}
731
René Scharfe48af1fd2022-01-18 13:46:32 +0100732enum bisect_error bisect_checkout(const struct object_id *bisect_rev,
733 int no_checkout)
Christian Couderef24c7c2009-04-19 11:56:07 +0200734{
Junio C Hamanoffcb4e92021-07-27 11:22:18 -0700735 struct commit *commit;
736 struct pretty_print_context pp = {0};
737 struct strbuf commit_msg = STRBUF_INIT;
Christian Couderef24c7c2009-04-19 11:56:07 +0200738
brian m. carlsonae077772017-10-15 22:06:51 +0000739 update_ref(NULL, "BISECT_EXPECTED_REV", bisect_rev, NULL, 0, UPDATE_REFS_DIE_ON_ERR);
Christian Couderef24c7c2009-04-19 11:56:07 +0200740
Jon Seymourfee92fc2011-08-04 22:01:00 +1000741 if (no_checkout) {
brian m. carlsonae077772017-10-15 22:06:51 +0000742 update_ref(NULL, "BISECT_HEAD", bisect_rev, NULL, 0,
743 UPDATE_REFS_DIE_ON_ERR);
Jon Seymourfee92fc2011-08-04 22:01:00 +1000744 } else {
René Scharfeddbb47f2022-10-30 12:55:06 +0100745 struct child_process cmd = CHILD_PROCESS_INIT;
René Scharfe48750b22022-10-30 12:47:02 +0100746
René Scharfeddbb47f2022-10-30 12:55:06 +0100747 cmd.git_cmd = 1;
748 strvec_pushl(&cmd.args, "checkout", "-q",
749 oid_to_hex(bisect_rev), "--", NULL);
750 if (run_command(&cmd))
Pranit Bauvae8e3ce62020-02-17 09:40:35 +0100751 /*
752 * Errors in `run_command()` itself, signaled by res < 0,
753 * and errors in the child process, signaled by res > 0
Junio C Hamano1fcc40c2021-07-28 10:07:02 -0700754 * can both be treated as regular BISECT_FAILED (-1).
Pranit Bauvae8e3ce62020-02-17 09:40:35 +0100755 */
Junio C Hamano1fcc40c2021-07-28 10:07:02 -0700756 return BISECT_FAILED;
Jon Seymourfee92fc2011-08-04 22:01:00 +1000757 }
Christian Couderef24c7c2009-04-19 11:56:07 +0200758
Junio C Hamanoffcb4e92021-07-27 11:22:18 -0700759 commit = lookup_commit_reference(the_repository, bisect_rev);
Ævar Arnfjörð Bjarmasonbab82162023-03-28 15:58:51 +0200760 repo_format_commit_message(the_repository, commit, "[%H] %s%n",
761 &commit_msg, &pp);
Junio C Hamanoffcb4e92021-07-27 11:22:18 -0700762 fputs(commit_msg.buf, stdout);
763 strbuf_release(&commit_msg);
764
Junio C Hamano1fcc40c2021-07-28 10:07:02 -0700765 return BISECT_OK;
Christian Couderef24c7c2009-04-19 11:56:07 +0200766}
767
Nguyễn Thái Ngọc Duy69d2cfe2018-11-10 06:48:59 +0100768static struct commit *get_commit_reference(struct repository *r,
769 const struct object_id *oid)
Christian Couderc0537662009-05-09 17:55:45 +0200770{
Nguyễn Thái Ngọc Duy69d2cfe2018-11-10 06:48:59 +0100771 struct commit *c = lookup_commit_reference(r, oid);
772 if (!c)
brian m. carlsonee3051b2017-03-26 16:01:37 +0000773 die(_("Not a valid commit name %s"), oid_to_hex(oid));
Nguyễn Thái Ngọc Duy69d2cfe2018-11-10 06:48:59 +0100774 return c;
Christian Couderc0537662009-05-09 17:55:45 +0200775}
776
Nguyễn Thái Ngọc Duy69d2cfe2018-11-10 06:48:59 +0100777static struct commit **get_bad_and_good_commits(struct repository *r,
778 int *rev_nr)
Christian Couderc0537662009-05-09 17:55:45 +0200779{
Jeff Kingb32fa952016-02-22 17:44:25 -0500780 struct commit **rev;
Christian Couderc0537662009-05-09 17:55:45 +0200781 int i, n = 0;
782
Jeff Kingb32fa952016-02-22 17:44:25 -0500783 ALLOC_ARRAY(rev, 1 + good_revs.nr);
Nguyễn Thái Ngọc Duy69d2cfe2018-11-10 06:48:59 +0100784 rev[n++] = get_commit_reference(r, current_bad_oid);
Jeff King902bb362011-05-19 17:34:33 -0400785 for (i = 0; i < good_revs.nr; i++)
Nguyễn Thái Ngọc Duy69d2cfe2018-11-10 06:48:59 +0100786 rev[n++] = get_commit_reference(r, good_revs.oid + i);
Christian Couderc0537662009-05-09 17:55:45 +0200787 *rev_nr = n;
788
789 return rev;
790}
791
Pranit Bauva9ec598e2020-02-17 09:40:38 +0100792static enum bisect_error handle_bad_merge_base(void)
Christian Couderc0537662009-05-09 17:55:45 +0200793{
brian m. carlson3c5ff992015-03-13 23:39:29 +0000794 if (is_expected_rev(current_bad_oid)) {
795 char *bad_hex = oid_to_hex(current_bad_oid);
Jeff King7383b252020-03-30 10:04:06 -0400796 char *good_hex = join_oid_array_hex(&good_revs, ' ');
Antoine Delaite43f9d9f2015-06-29 17:40:29 +0200797 if (!strcmp(term_bad, "bad") && !strcmp(term_good, "good")) {
Vasco Almeida14dc4892016-06-17 20:21:12 +0000798 fprintf(stderr, _("The merge base %s is bad.\n"
Antoine Delaite43f9d9f2015-06-29 17:40:29 +0200799 "This means the bug has been fixed "
Vasco Almeida14dc4892016-06-17 20:21:12 +0000800 "between %s and [%s].\n"),
Antoine Delaite43f9d9f2015-06-29 17:40:29 +0200801 bad_hex, bad_hex, good_hex);
Antoine Delaite21e5cfd2015-06-29 17:40:33 +0200802 } else if (!strcmp(term_bad, "new") && !strcmp(term_good, "old")) {
Vasco Almeida14dc4892016-06-17 20:21:12 +0000803 fprintf(stderr, _("The merge base %s is new.\n"
Antoine Delaite21e5cfd2015-06-29 17:40:33 +0200804 "The property has changed "
Vasco Almeida14dc4892016-06-17 20:21:12 +0000805 "between %s and [%s].\n"),
Antoine Delaite21e5cfd2015-06-29 17:40:33 +0200806 bad_hex, bad_hex, good_hex);
Antoine Delaite43f9d9f2015-06-29 17:40:29 +0200807 } else {
Vasco Almeida14dc4892016-06-17 20:21:12 +0000808 fprintf(stderr, _("The merge base %s is %s.\n"
Antoine Delaite43f9d9f2015-06-29 17:40:29 +0200809 "This means the first '%s' commit is "
Vasco Almeida14dc4892016-06-17 20:21:12 +0000810 "between %s and [%s].\n"),
Antoine Delaite43f9d9f2015-06-29 17:40:29 +0200811 bad_hex, term_bad, term_good, bad_hex, good_hex);
812 }
Pranit Bauva9ec598e2020-02-17 09:40:38 +0100813 return BISECT_MERGE_BASE_CHECK;
Christian Couderc0537662009-05-09 17:55:45 +0200814 }
815
Alex Henrie3f407b72016-12-04 15:04:23 -0700816 fprintf(stderr, _("Some %s revs are not ancestors of the %s rev.\n"
Christian Couderc0537662009-05-09 17:55:45 +0200817 "git bisect cannot work properly in this case.\n"
Vasco Almeida14dc4892016-06-17 20:21:12 +0000818 "Maybe you mistook %s and %s revs?\n"),
Antoine Delaite43f9d9f2015-06-29 17:40:29 +0200819 term_good, term_bad, term_good, term_bad);
Pranit Bauva9ec598e2020-02-17 09:40:38 +0100820 return BISECT_FAILED;
Christian Couderc0537662009-05-09 17:55:45 +0200821}
822
brian m. carlson4ce36212017-03-31 01:39:57 +0000823static void handle_skipped_merge_base(const struct object_id *mb)
Christian Couderc0537662009-05-09 17:55:45 +0200824{
brian m. carlson4ce36212017-03-31 01:39:57 +0000825 char *mb_hex = oid_to_hex(mb);
brian m. carlsonc368dde2016-06-24 23:09:22 +0000826 char *bad_hex = oid_to_hex(current_bad_oid);
Jeff King7383b252020-03-30 10:04:06 -0400827 char *good_hex = join_oid_array_hex(&good_revs, ' ');
Christian Couderc0537662009-05-09 17:55:45 +0200828
Vasco Almeida14dc4892016-06-17 20:21:12 +0000829 warning(_("the merge base between %s and [%s] "
Christian Couderc0537662009-05-09 17:55:45 +0200830 "must be skipped.\n"
Antoine Delaite43f9d9f2015-06-29 17:40:29 +0200831 "So we cannot be sure the first %s commit is "
Christian Couderc0537662009-05-09 17:55:45 +0200832 "between %s and %s.\n"
Vasco Almeida14dc4892016-06-17 20:21:12 +0000833 "We continue anyway."),
Antoine Delaite43f9d9f2015-06-29 17:40:29 +0200834 bad_hex, good_hex, term_bad, mb_hex, bad_hex);
Christian Couderc0537662009-05-09 17:55:45 +0200835 free(good_hex);
836}
837
838/*
Antoine Delaite21e5cfd2015-06-29 17:40:33 +0200839 * "check_merge_bases" checks that merge bases are not "bad" (or "new").
Christian Couderc0537662009-05-09 17:55:45 +0200840 *
Antoine Delaite21e5cfd2015-06-29 17:40:33 +0200841 * - If one is "bad" (or "new"), it means the user assumed something wrong
Pranit Bauvacdd4dc22020-02-17 09:40:36 +0100842 * and we must return error with a non 0 error code.
Antoine Delaite21e5cfd2015-06-29 17:40:33 +0200843 * - If one is "good" (or "old"), that's good, we have nothing to do.
Christian Couderc0537662009-05-09 17:55:45 +0200844 * - If one is "skipped", we can't know but we should warn.
845 * - If we don't know, we should check it out and ask the user to test.
Pranit Bauvacdd4dc22020-02-17 09:40:36 +0100846 * - If a merge base must be tested, on success return
847 * BISECT_INTERNAL_SUCCESS_MERGE_BASE (-11) a special condition
848 * for early success, this will be converted back to 0 in
849 * check_good_are_ancestors_of_bad().
Christian Couderc0537662009-05-09 17:55:45 +0200850 */
Pranit Bauvacdd4dc22020-02-17 09:40:36 +0100851static enum bisect_error check_merge_bases(int rev_nr, struct commit **rev, int no_checkout)
Christian Couderc0537662009-05-09 17:55:45 +0200852{
Pranit Bauvacdd4dc22020-02-17 09:40:36 +0100853 enum bisect_error res = BISECT_OK;
Christian Couderc0537662009-05-09 17:55:45 +0200854 struct commit_list *result;
Christian Couderc0537662009-05-09 17:55:45 +0200855
Ævar Arnfjörð Bjarmasoncb338c22023-03-28 15:58:47 +0200856 result = repo_get_merge_bases_many(the_repository, rev[0], rev_nr - 1,
857 rev + 1);
Christian Couderc0537662009-05-09 17:55:45 +0200858
859 for (; result; result = result->next) {
brian m. carlson4ce36212017-03-31 01:39:57 +0000860 const struct object_id *mb = &result->item->object.oid;
Jeff King4a7e27e2018-08-28 17:22:40 -0400861 if (oideq(mb, current_bad_oid)) {
Pranit Bauva9ec598e2020-02-17 09:40:38 +0100862 res = handle_bad_merge_base();
863 break;
brian m. carlson910650d2017-03-31 01:40:00 +0000864 } else if (0 <= oid_array_lookup(&good_revs, mb)) {
Christian Couderc0537662009-05-09 17:55:45 +0200865 continue;
brian m. carlson910650d2017-03-31 01:40:00 +0000866 } else if (0 <= oid_array_lookup(&skipped_revs, mb)) {
Christian Couderc0537662009-05-09 17:55:45 +0200867 handle_skipped_merge_base(mb);
868 } else {
Vasco Almeida14dc4892016-06-17 20:21:12 +0000869 printf(_("Bisecting: a merge base must be tested\n"));
Pranit Bauvacdd4dc22020-02-17 09:40:36 +0100870 res = bisect_checkout(mb, no_checkout);
871 if (!res)
872 /* indicate early success */
873 res = BISECT_INTERNAL_SUCCESS_MERGE_BASE;
874 break;
Christian Couderc0537662009-05-09 17:55:45 +0200875 }
876 }
877
Christian Couderc0537662009-05-09 17:55:45 +0200878 free_commit_list(result);
Pranit Bauvacdd4dc22020-02-17 09:40:36 +0100879 return res;
Christian Couderc0537662009-05-09 17:55:45 +0200880}
881
Nguyễn Thái Ngọc Duy69d2cfe2018-11-10 06:48:59 +0100882static int check_ancestors(struct repository *r, int rev_nr,
883 struct commit **rev, const char *prefix)
Christian Couderd937d4a2009-05-09 17:55:46 +0200884{
Ævar Arnfjörð Bjarmason57efebb2022-08-02 17:33:15 +0200885 struct strvec rev_argv = STRVEC_INIT;
Christian Couder2d938fc2009-05-17 17:36:46 +0200886 struct rev_info revs;
René Scharfe86a0a402011-10-01 18:16:08 +0200887 int res;
Christian Couderd937d4a2009-05-09 17:55:46 +0200888
Ævar Arnfjörð Bjarmason57efebb2022-08-02 17:33:15 +0200889 bisect_rev_setup(r, &revs, &rev_argv, prefix, "^%s", "%s", 0);
Christian Couderd937d4a2009-05-09 17:55:46 +0200890
Christian Couder2d938fc2009-05-17 17:36:46 +0200891 bisect_common(&revs);
892 res = (revs.commits != NULL);
893
894 /* Clean up objects used, as they will be reused. */
René Scharfe148f14a2017-12-25 18:45:36 +0100895 clear_commit_marks_many(rev_nr, rev, ALL_REV_FLAGS);
Christian Couderd937d4a2009-05-09 17:55:46 +0200896
Ævar Arnfjörð Bjarmason2108fe42022-04-13 22:01:36 +0200897 release_revisions(&revs);
Ævar Arnfjörð Bjarmason57efebb2022-08-02 17:33:15 +0200898 strvec_clear(&rev_argv);
Christian Couder2d938fc2009-05-17 17:36:46 +0200899 return res;
Christian Couderd937d4a2009-05-09 17:55:46 +0200900}
901
902/*
903 * "check_good_are_ancestors_of_bad" checks that all "good" revs are
904 * ancestor of the "bad" rev.
905 *
906 * If that's not the case, we need to check the merge bases.
907 * If a merge base must be tested by the user, its source code will be
Pranit Bauva45b63702020-02-17 09:40:37 +0100908 * checked out to be tested by the user and we will return.
Christian Couderd937d4a2009-05-09 17:55:46 +0200909 */
Pranit Bauva45b63702020-02-17 09:40:37 +0100910
911static enum bisect_error check_good_are_ancestors_of_bad(struct repository *r,
Nguyễn Thái Ngọc Duy69d2cfe2018-11-10 06:48:59 +0100912 const char *prefix,
913 int no_checkout)
Christian Couderd937d4a2009-05-09 17:55:46 +0200914{
Pranit Bauva45b63702020-02-17 09:40:37 +0100915 char *filename;
Christian Couderd937d4a2009-05-09 17:55:46 +0200916 struct stat st;
René Scharfe148f14a2017-12-25 18:45:36 +0100917 int fd, rev_nr;
Pranit Bauvacdd4dc22020-02-17 09:40:36 +0100918 enum bisect_error res = BISECT_OK;
René Scharfe148f14a2017-12-25 18:45:36 +0100919 struct commit **rev;
Christian Couderd937d4a2009-05-09 17:55:46 +0200920
brian m. carlson3c5ff992015-03-13 23:39:29 +0000921 if (!current_bad_oid)
Pranit Bauva45b63702020-02-17 09:40:37 +0100922 return error(_("a %s revision is needed"), term_bad);
923
924 filename = git_pathdup("BISECT_ANCESTORS_OK");
Christian Couderd937d4a2009-05-09 17:55:46 +0200925
926 /* Check if file BISECT_ANCESTORS_OK exists. */
927 if (!stat(filename, &st) && S_ISREG(st.st_mode))
Michael Haggerty144e7092012-04-27 00:26:59 +0200928 goto done;
Christian Couderd937d4a2009-05-09 17:55:46 +0200929
930 /* Bisecting with no good rev is ok. */
Miriam Rubiob8e3b2f2020-02-17 09:40:30 +0100931 if (!good_revs.nr)
Michael Haggerty144e7092012-04-27 00:26:59 +0200932 goto done;
Christian Couderd937d4a2009-05-09 17:55:46 +0200933
Christian Couder2d938fc2009-05-17 17:36:46 +0200934 /* Check if all good revs are ancestor of the bad rev. */
Pranit Bauvacdd4dc22020-02-17 09:40:36 +0100935
Nguyễn Thái Ngọc Duy69d2cfe2018-11-10 06:48:59 +0100936 rev = get_bad_and_good_commits(r, &rev_nr);
937 if (check_ancestors(r, rev_nr, rev, prefix))
Pranit Bauvacdd4dc22020-02-17 09:40:36 +0100938 res = check_merge_bases(rev_nr, rev, no_checkout);
René Scharfe148f14a2017-12-25 18:45:36 +0100939 free(rev);
Christian Couderd937d4a2009-05-09 17:55:46 +0200940
Pranit Bauva45b63702020-02-17 09:40:37 +0100941 if (!res) {
942 /* Create file BISECT_ANCESTORS_OK. */
943 fd = open(filename, O_CREAT | O_TRUNC | O_WRONLY, 0600);
944 if (fd < 0)
945 /*
946 * BISECT_ANCESTORS_OK file is not absolutely necessary,
947 * the bisection process will continue at the next
948 * bisection step.
949 * So, just signal with a warning that something
950 * might be wrong.
951 */
952 warning_errno(_("could not create file '%s'"),
953 filename);
954 else
955 close(fd);
956 }
Michael Haggerty144e7092012-04-27 00:26:59 +0200957 done:
958 free(filename);
Pranit Bauva45b63702020-02-17 09:40:37 +0100959 return res;
Christian Couderd937d4a2009-05-09 17:55:46 +0200960}
961
962/*
Christian Coudere22278c2009-05-28 23:21:16 +0200963 * This does "git diff-tree --pretty COMMIT" without one fork+exec.
964 */
Nguyễn Thái Ngọc Duy69d2cfe2018-11-10 06:48:59 +0100965static void show_diff_tree(struct repository *r,
966 const char *prefix,
967 struct commit *commit)
Christian Coudere22278c2009-05-28 23:21:16 +0200968{
Jeff King2008f292019-02-22 01:20:37 -0500969 const char *argv[] = {
Jeff Kingb02be8b2019-02-22 01:23:28 -0500970 "diff-tree", "--pretty", "--stat", "--summary", "--cc", NULL
Jeff King2008f292019-02-22 01:20:37 -0500971 };
Christian Coudere22278c2009-05-28 23:21:16 +0200972 struct rev_info opt;
973
Jeff Kingb02be8b2019-02-22 01:23:28 -0500974 git_config(git_diff_ui_config, NULL);
Jeff King40ae3d32019-02-22 01:21:33 -0500975 repo_init_revisions(r, &opt, prefix);
Christian Coudere22278c2009-05-28 23:21:16 +0200976
Jeff King2008f292019-02-22 01:20:37 -0500977 setup_revisions(ARRAY_SIZE(argv) - 1, argv, &opt, NULL);
Christian Coudere22278c2009-05-28 23:21:16 +0200978 log_tree_commit(&opt, commit);
Ævar Arnfjörð Bjarmason2108fe42022-04-13 22:01:36 +0200979 release_revisions(&opt);
Christian Coudere22278c2009-05-28 23:21:16 +0200980}
981
982/*
Antoine Delaitecb46d632015-06-29 17:40:30 +0200983 * The terms used for this bisect session are stored in BISECT_TERMS.
984 * We read them and store them to adapt the messages accordingly.
985 * Default is bad/good.
986 */
987void read_bisect_terms(const char **read_bad, const char **read_good)
988{
989 struct strbuf str = STRBUF_INIT;
Jeff Kingf5d284f2017-04-20 17:08:25 -0400990 const char *filename = git_path_bisect_terms();
Antoine Delaitecb46d632015-06-29 17:40:30 +0200991 FILE *fp = fopen(filename, "r");
992
993 if (!fp) {
994 if (errno == ENOENT) {
995 *read_bad = "bad";
996 *read_good = "good";
997 return;
998 } else {
Vasco Almeida14dc4892016-06-17 20:21:12 +0000999 die_errno(_("could not read file '%s'"), filename);
Antoine Delaitecb46d632015-06-29 17:40:30 +02001000 }
1001 } else {
Junio C Hamano8f309ae2016-01-13 15:31:17 -08001002 strbuf_getline_lf(&str, fp);
Antoine Delaitecb46d632015-06-29 17:40:30 +02001003 *read_bad = strbuf_detach(&str, NULL);
Junio C Hamano8f309ae2016-01-13 15:31:17 -08001004 strbuf_getline_lf(&str, fp);
Antoine Delaitecb46d632015-06-29 17:40:30 +02001005 *read_good = strbuf_detach(&str, NULL);
1006 }
1007 strbuf_release(&str);
1008 fclose(fp);
1009}
1010
1011/*
Pranit Bauva6c69f222020-02-17 09:40:39 +01001012 * We use the convention that return BISECT_INTERNAL_SUCCESS_1ST_BAD_FOUND (-10) means
Christian Couderef24c7c2009-04-19 11:56:07 +02001013 * the bisection process finished successfully.
Pranit Bauva6c69f222020-02-17 09:40:39 +01001014 * In this case the calling function or command should not turn a
1015 * BISECT_INTERNAL_SUCCESS_1ST_BAD_FOUND return code into an error or a non zero exit code.
Pranit Bauva517ecb32020-09-24 14:33:40 +02001016 *
1017 * Checking BISECT_INTERNAL_SUCCESS_1ST_BAD_FOUND
1018 * in bisect_helper::bisect_next() and only transforming it to 0 at
1019 * the end of bisect_helper::cmd_bisect__helper() helps bypassing
1020 * all the code related to finding a commit to test.
Christian Couderef24c7c2009-04-19 11:56:07 +02001021 */
Aaron Lipmanbe5fe202020-08-07 17:58:36 -04001022enum bisect_error bisect_next_all(struct repository *r, const char *prefix)
Christian Couderef24c7c2009-04-19 11:56:07 +02001023{
Ævar Arnfjörð Bjarmason57efebb2022-08-02 17:33:15 +02001024 struct strvec rev_argv = STRVEC_INIT;
Ævar Arnfjörð Bjarmasonf196c1e2022-04-13 22:01:38 +02001025 struct rev_info revs = REV_INFO_INIT;
Christian Couderef24c7c2009-04-19 11:56:07 +02001026 struct commit_list *tried;
David Ripton6329bad2010-01-19 07:13:33 -08001027 int reaches = 0, all = 0, nr, steps;
Pranit Bauvace58b5d2020-02-17 09:40:34 +01001028 enum bisect_error res = BISECT_OK;
brian m. carlson4be0dee2017-07-13 23:49:24 +00001029 struct object_id *bisect_rev;
Maxim Moseychuk2cfa8352017-02-16 20:07:12 +03001030 char *steps_msg;
Pranit Bauva517ecb32020-09-24 14:33:40 +02001031 /*
1032 * If no_checkout is non-zero, the bisection process does not
1033 * checkout the trial commit but instead simply updates BISECT_HEAD.
1034 */
Aaron Lipmanbe5fe202020-08-07 17:58:36 -04001035 int no_checkout = ref_exists("BISECT_HEAD");
Aaron Lipmanad464a42020-08-07 17:58:38 -04001036 unsigned bisect_flags = 0;
Christian Couderef24c7c2009-04-19 11:56:07 +02001037
Antoine Delaitecb46d632015-06-29 17:40:30 +02001038 read_bisect_terms(&term_bad, &term_good);
Christian Couder2b020692009-05-09 17:55:42 +02001039 if (read_bisect_refs())
Vasco Almeida14dc4892016-06-17 20:21:12 +00001040 die(_("reading bisect refs failed"));
Christian Couder2b020692009-05-09 17:55:42 +02001041
Aaron Lipmanad464a42020-08-07 17:58:38 -04001042 if (file_exists(git_path_bisect_first_parent()))
1043 bisect_flags |= FIND_BISECTION_FIRST_PARENT_ONLY;
1044
1045 if (skipped_revs.nr)
1046 bisect_flags |= FIND_BISECTION_ALL;
1047
Pranit Bauva45b63702020-02-17 09:40:37 +01001048 res = check_good_are_ancestors_of_bad(r, prefix, no_checkout);
1049 if (res)
Ævar Arnfjörð Bjarmasonf196c1e2022-04-13 22:01:38 +02001050 goto cleanup;
Christian Couder08719842009-05-09 17:55:47 +02001051
Ævar Arnfjörð Bjarmason57efebb2022-08-02 17:33:15 +02001052 bisect_rev_setup(r, &revs, &rev_argv, prefix, "%s", "^%s", 1);
Aaron Lipmanad464a42020-08-07 17:58:38 -04001053
1054 revs.first_parent_only = !!(bisect_flags & FIND_BISECTION_FIRST_PARENT_ONLY);
Christian Coudera22347c2009-05-17 17:36:44 +02001055 revs.limited = 1;
Christian Couder2b020692009-05-09 17:55:42 +02001056
Christian Coudera22347c2009-05-17 17:36:44 +02001057 bisect_common(&revs);
Christian Couderef24c7c2009-04-19 11:56:07 +02001058
Aaron Lipmanad464a42020-08-07 17:58:38 -04001059 find_bisection(&revs.commits, &reaches, &all, bisect_flags);
Christian Couder62d0b0d2009-06-06 06:41:34 +02001060 revs.commits = managed_skipped(revs.commits, &tried);
Christian Couderef24c7c2009-04-19 11:56:07 +02001061
1062 if (!revs.commits) {
1063 /*
Pranit Bauva6c69f222020-02-17 09:40:39 +01001064 * We should return error here only if the "bad"
Christian Couderef24c7c2009-04-19 11:56:07 +02001065 * commit is also a "skip" commit.
1066 */
Pranit Bauvace58b5d2020-02-17 09:40:34 +01001067 res = error_if_skipped_commits(tried, NULL);
1068 if (res < 0)
Ævar Arnfjörð Bjarmasonc5365e92022-08-02 17:33:11 +02001069 goto cleanup;
Vasco Almeida14dc4892016-06-17 20:21:12 +00001070 printf(_("%s was both %s and %s\n"),
Antoine Delaite43f9d9f2015-06-29 17:40:29 +02001071 oid_to_hex(current_bad_oid),
1072 term_good,
1073 term_bad);
Pranit Bauva6c69f222020-02-17 09:40:39 +01001074
Ævar Arnfjörð Bjarmasonf196c1e2022-04-13 22:01:38 +02001075 res = BISECT_FAILED;
1076 goto cleanup;
Christian Couderef24c7c2009-04-19 11:56:07 +02001077 }
1078
Christian Couder8f69f722010-02-28 23:19:09 +01001079 if (!all) {
Vasco Almeida14dc4892016-06-17 20:21:12 +00001080 fprintf(stderr, _("No testable commit found.\n"
Johannes Sixtb8657342021-02-23 22:11:32 +01001081 "Maybe you started with bad path arguments?\n"));
Pranit Bauva6c69f222020-02-17 09:40:39 +01001082
Ævar Arnfjörð Bjarmasonf196c1e2022-04-13 22:01:38 +02001083 res = BISECT_NO_TESTABLE_COMMIT;
1084 goto cleanup;
Christian Couder8f69f722010-02-28 23:19:09 +01001085 }
1086
brian m. carlson4be0dee2017-07-13 23:49:24 +00001087 bisect_rev = &revs.commits->item->object.oid;
Christian Couderef24c7c2009-04-19 11:56:07 +02001088
Jeff King4a7e27e2018-08-28 17:22:40 -04001089 if (oideq(bisect_rev, current_bad_oid)) {
Pranit Bauvace58b5d2020-02-17 09:40:34 +01001090 res = error_if_skipped_commits(tried, current_bad_oid);
1091 if (res)
Pranit Bauva6c69f222020-02-17 09:40:39 +01001092 return res;
brian m. carlson4be0dee2017-07-13 23:49:24 +00001093 printf("%s is the first %s commit\n", oid_to_hex(bisect_rev),
Antoine Delaite43f9d9f2015-06-29 17:40:29 +02001094 term_bad);
Pranit Bauva6c69f222020-02-17 09:40:39 +01001095
Nguyễn Thái Ngọc Duy69d2cfe2018-11-10 06:48:59 +01001096 show_diff_tree(r, prefix, revs.commits->item);
Pranit Bauva6c69f222020-02-17 09:40:39 +01001097 /*
1098 * This means the bisection process succeeded.
1099 * Using BISECT_INTERNAL_SUCCESS_1ST_BAD_FOUND (-10)
1100 * so that the call chain can simply check
1101 * for negative return values for early returns up
1102 * until the cmd_bisect__helper() caller.
1103 */
Ævar Arnfjörð Bjarmasonf196c1e2022-04-13 22:01:38 +02001104 res = BISECT_INTERNAL_SUCCESS_1ST_BAD_FOUND;
1105 goto cleanup;
Christian Couderef24c7c2009-04-19 11:56:07 +02001106 }
1107
1108 nr = all - reaches - 1;
David Ripton6329bad2010-01-19 07:13:33 -08001109 steps = estimate_bisect_steps(all);
Maxim Moseychuk2cfa8352017-02-16 20:07:12 +03001110
1111 steps_msg = xstrfmt(Q_("(roughly %d step)", "(roughly %d steps)",
1112 steps), steps);
Ævar Arnfjörð Bjarmason66f5f6d2017-05-11 21:20:12 +00001113 /*
1114 * TRANSLATORS: the last %s will be replaced with "(roughly %d
1115 * steps)" translation.
1116 */
Vasco Almeida14dc4892016-06-17 20:21:12 +00001117 printf(Q_("Bisecting: %d revision left to test after this %s\n",
1118 "Bisecting: %d revisions left to test after this %s\n",
1119 nr), nr, steps_msg);
Maxim Moseychuk2cfa8352017-02-16 20:07:12 +03001120 free(steps_msg);
Miriam Rubioc7a7f482020-09-24 14:33:39 +02001121 /* Clean up objects used, as they will be reused. */
René Scharfe0795df42020-10-31 13:47:58 +01001122 repo_clear_commit_marks(r, ALL_REV_FLAGS);
Christian Couderef24c7c2009-04-19 11:56:07 +02001123
Ævar Arnfjörð Bjarmasonf196c1e2022-04-13 22:01:38 +02001124 res = bisect_checkout(bisect_rev, no_checkout);
1125cleanup:
1126 release_revisions(&revs);
Ævar Arnfjörð Bjarmason57efebb2022-08-02 17:33:15 +02001127 strvec_clear(&rev_argv);
Ævar Arnfjörð Bjarmasonf196c1e2022-04-13 22:01:38 +02001128 return res;
Christian Couderef24c7c2009-04-19 11:56:07 +02001129}
1130
Nguyễn Thái Ngọc Duyc43cb382012-10-26 22:53:50 +07001131static inline int log2i(int n)
1132{
1133 int log2 = 0;
1134
1135 for (; n > 1; n >>= 1)
1136 log2++;
1137
1138 return log2;
1139}
1140
1141static inline int exp2i(int n)
1142{
1143 return 1 << n;
1144}
1145
1146/*
1147 * Estimate the number of bisect steps left (after the current step)
1148 *
1149 * For any x between 0 included and 2^n excluded, the probability for
1150 * n - 1 steps left looks like:
1151 *
1152 * P(2^n + x) == (2^n - x) / (2^n + x)
1153 *
1154 * and P(2^n + x) < 0.5 means 2^n < 3x
1155 */
1156int estimate_bisect_steps(int all)
1157{
1158 int n, x, e;
1159
1160 if (all < 3)
1161 return 0;
1162
1163 n = log2i(all);
1164 e = exp2i(n);
1165 x = all - e;
1166
1167 return (e < 3 * x) ? n : n - 1;
1168}
Pranit Bauvafb71a322017-09-29 06:49:39 +00001169
Jeff King63e14ee2022-08-19 06:08:32 -04001170static int mark_for_removal(const char *refname,
Ævar Arnfjörð Bjarmason5cf88fd2022-08-25 19:09:48 +02001171 const struct object_id *oid UNUSED,
1172 int flag UNUSED, void *cb_data)
Pranit Bauvafb71a322017-09-29 06:49:39 +00001173{
1174 struct string_list *refs = cb_data;
1175 char *ref = xstrfmt("refs/bisect%s", refname);
1176 string_list_append(refs, ref);
1177 return 0;
1178}
1179
1180int bisect_clean_state(void)
1181{
1182 int result = 0;
1183
1184 /* There may be some refs packed during bisection */
1185 struct string_list refs_for_removal = STRING_LIST_INIT_NODUP;
1186 for_each_ref_in("refs/bisect", mark_for_removal, (void *) &refs_for_removal);
1187 string_list_append(&refs_for_removal, xstrdup("BISECT_HEAD"));
Junio C Hamanoa9722292017-11-15 12:14:29 +09001188 result = delete_refs("bisect: remove", &refs_for_removal, REF_NO_DEREF);
Pranit Bauvafb71a322017-09-29 06:49:39 +00001189 refs_for_removal.strdup_strings = 1;
1190 string_list_clear(&refs_for_removal, 0);
1191 unlink_or_warn(git_path_bisect_expected_rev());
1192 unlink_or_warn(git_path_bisect_ancestors_ok());
1193 unlink_or_warn(git_path_bisect_log());
1194 unlink_or_warn(git_path_bisect_names());
1195 unlink_or_warn(git_path_bisect_run());
1196 unlink_or_warn(git_path_bisect_terms());
Aaron Lipmane8861ff2020-08-07 17:58:37 -04001197 unlink_or_warn(git_path_bisect_first_parent());
Pranit Bauvafb71a322017-09-29 06:49:39 +00001198 /*
1199 * Cleanup BISECT_START last to support the --no-checkout option
1200 * introduced in the commit 4796e823a.
1201 */
1202 unlink_or_warn(git_path_bisect_start());
1203
1204 return result;
1205}