1 #include "builtin.h"
2 #include "cache.h"
3 #include "parse-options.h"
4 #include "bisect.h"
5 #include "refs.h"
6 #include "dir.h"
7 #include "strvec.h"
8 #include "run-command.h"
9 #include "prompt.h"
10 #include "quote.h"
11 #include "revision.h"
12 
13 static GIT_PATH_FUNC(git_path_bisect_terms, "BISECT_TERMS")
14 static GIT_PATH_FUNC(git_path_bisect_expected_rev, "BISECT_EXPECTED_REV")
15 static GIT_PATH_FUNC(git_path_bisect_ancestors_ok, "BISECT_ANCESTORS_OK")
16 static GIT_PATH_FUNC(git_path_bisect_start, "BISECT_START")
17 static GIT_PATH_FUNC(git_path_bisect_log, "BISECT_LOG")
18 static GIT_PATH_FUNC(git_path_head_name, "head-name")
19 static GIT_PATH_FUNC(git_path_bisect_names, "BISECT_NAMES")
20 static GIT_PATH_FUNC(git_path_bisect_first_parent, "BISECT_FIRST_PARENT")
21 static GIT_PATH_FUNC(git_path_bisect_run, "BISECT_RUN")
22 
23 static const char * const git_bisect_helper_usage[] = {
24 	N_("git bisect--helper --bisect-reset [<commit>]"),
25 	N_("git bisect--helper --bisect-terms [--term-good | --term-old | --term-bad | --term-new]"),
26 	N_("git bisect--helper --bisect-start [--term-{new,bad}=<term> --term-{old,good}=<term>]"
27 					    " [--no-checkout] [--first-parent] [<bad> [<good>...]] [--] [<paths>...]"),
28 	N_("git bisect--helper --bisect-next"),
29 	N_("git bisect--helper --bisect-state (bad|new) [<rev>]"),
30 	N_("git bisect--helper --bisect-state (good|old) [<rev>...]"),
31 	N_("git bisect--helper --bisect-replay <filename>"),
32 	N_("git bisect--helper --bisect-skip [(<rev>|<range>)...]"),
33 	N_("git bisect--helper --bisect-visualize"),
34 	N_("git bisect--helper --bisect-run <cmd>..."),
35 	NULL
36 };
37 
38 struct add_bisect_ref_data {
39 	struct rev_info *revs;
40 	unsigned int object_flags;
41 };
42 
43 struct bisect_terms {
44 	char *term_good;
45 	char *term_bad;
46 };
47 
free_terms(struct bisect_terms * terms)48 static void free_terms(struct bisect_terms *terms)
49 {
50 	FREE_AND_NULL(terms->term_good);
51 	FREE_AND_NULL(terms->term_bad);
52 }
53 
set_terms(struct bisect_terms * terms,const char * bad,const char * good)54 static void set_terms(struct bisect_terms *terms, const char *bad,
55 		      const char *good)
56 {
57 	free((void *)terms->term_good);
58 	terms->term_good = xstrdup(good);
59 	free((void *)terms->term_bad);
60 	terms->term_bad = xstrdup(bad);
61 }
62 
63 static const char vocab_bad[] = "bad|new";
64 static const char vocab_good[] = "good|old";
65 
66 static int bisect_autostart(struct bisect_terms *terms);
67 
68 /*
69  * Check whether the string `term` belongs to the set of strings
70  * included in the variable arguments.
71  */
72 LAST_ARG_MUST_BE_NULL
one_of(const char * term,...)73 static int one_of(const char *term, ...)
74 {
75 	int res = 0;
76 	va_list matches;
77 	const char *match;
78 
79 	va_start(matches, term);
80 	while (!res && (match = va_arg(matches, const char *)))
81 		res = !strcmp(term, match);
82 	va_end(matches);
83 
84 	return res;
85 }
86 
87 /*
88  * return code BISECT_INTERNAL_SUCCESS_MERGE_BASE
89  * and BISECT_INTERNAL_SUCCESS_1ST_BAD_FOUND are codes
90  * that indicate special success.
91  */
92 
is_bisect_success(enum bisect_error res)93 static int is_bisect_success(enum bisect_error res)
94 {
95 	return !res ||
96 		res == BISECT_INTERNAL_SUCCESS_1ST_BAD_FOUND ||
97 		res == BISECT_INTERNAL_SUCCESS_MERGE_BASE;
98 }
99 
write_in_file(const char * path,const char * mode,const char * format,va_list args)100 static int write_in_file(const char *path, const char *mode, const char *format, va_list args)
101 {
102 	FILE *fp = NULL;
103 	int res = 0;
104 
105 	if (strcmp(mode, "w") && strcmp(mode, "a"))
106 		BUG("write-in-file does not support '%s' mode", mode);
107 	fp = fopen(path, mode);
108 	if (!fp)
109 		return error_errno(_("cannot open file '%s' in mode '%s'"), path, mode);
110 	res = vfprintf(fp, format, args);
111 
112 	if (res < 0) {
113 		int saved_errno = errno;
114 		fclose(fp);
115 		errno = saved_errno;
116 		return error_errno(_("could not write to file '%s'"), path);
117 	}
118 
119 	return fclose(fp);
120 }
121 
122 __attribute__((format (printf, 2, 3)))
write_to_file(const char * path,const char * format,...)123 static int write_to_file(const char *path, const char *format, ...)
124 {
125 	int res;
126 	va_list args;
127 
128 	va_start(args, format);
129 	res = write_in_file(path, "w", format, args);
130 	va_end(args);
131 
132 	return res;
133 }
134 
135 __attribute__((format (printf, 2, 3)))
append_to_file(const char * path,const char * format,...)136 static int append_to_file(const char *path, const char *format, ...)
137 {
138 	int res;
139 	va_list args;
140 
141 	va_start(args, format);
142 	res = write_in_file(path, "a", format, args);
143 	va_end(args);
144 
145 	return res;
146 }
147 
print_file_to_stdout(const char * path)148 static int print_file_to_stdout(const char *path)
149 {
150 	int fd = open(path, O_RDONLY);
151 	int ret = 0;
152 
153 	if (fd < 0)
154 		return error_errno(_("cannot open file '%s' for reading"), path);
155 	if (copy_fd(fd, 1) < 0)
156 		ret = error_errno(_("failed to read '%s'"), path);
157 	close(fd);
158 	return ret;
159 }
160 
check_term_format(const char * term,const char * orig_term)161 static int check_term_format(const char *term, const char *orig_term)
162 {
163 	int res;
164 	char *new_term = xstrfmt("refs/bisect/%s", term);
165 
166 	res = check_refname_format(new_term, 0);
167 	free(new_term);
168 
169 	if (res)
170 		return error(_("'%s' is not a valid term"), term);
171 
172 	if (one_of(term, "help", "start", "skip", "next", "reset",
173 			"visualize", "view", "replay", "log", "run", "terms", NULL))
174 		return error(_("can't use the builtin command '%s' as a term"), term);
175 
176 	/*
177 	 * In theory, nothing prevents swapping completely good and bad,
178 	 * but this situation could be confusing and hasn't been tested
179 	 * enough. Forbid it for now.
180 	 */
181 
182 	if ((strcmp(orig_term, "bad") && one_of(term, "bad", "new", NULL)) ||
183 		 (strcmp(orig_term, "good") && one_of(term, "good", "old", NULL)))
184 		return error(_("can't change the meaning of the term '%s'"), term);
185 
186 	return 0;
187 }
188 
write_terms(const char * bad,const char * good)189 static int write_terms(const char *bad, const char *good)
190 {
191 	int res;
192 
193 	if (!strcmp(bad, good))
194 		return error(_("please use two different terms"));
195 
196 	if (check_term_format(bad, "bad") || check_term_format(good, "good"))
197 		return -1;
198 
199 	res = write_to_file(git_path_bisect_terms(), "%s\n%s\n", bad, good);
200 
201 	return res;
202 }
203 
bisect_reset(const char * commit)204 static int bisect_reset(const char *commit)
205 {
206 	struct strbuf branch = STRBUF_INIT;
207 
208 	if (!commit) {
209 		if (strbuf_read_file(&branch, git_path_bisect_start(), 0) < 1) {
210 			printf(_("We are not bisecting.\n"));
211 			return 0;
212 		}
213 		strbuf_rtrim(&branch);
214 	} else {
215 		struct object_id oid;
216 
217 		if (get_oid_commit(commit, &oid))
218 			return error(_("'%s' is not a valid commit"), commit);
219 		strbuf_addstr(&branch, commit);
220 	}
221 
222 	if (!ref_exists("BISECT_HEAD")) {
223 		struct strvec argv = STRVEC_INIT;
224 
225 		strvec_pushl(&argv, "checkout", branch.buf, "--", NULL);
226 		if (run_command_v_opt(argv.v, RUN_GIT_CMD)) {
227 			error(_("could not check out original"
228 				" HEAD '%s'. Try 'git bisect"
229 				" reset <commit>'."), branch.buf);
230 			strbuf_release(&branch);
231 			strvec_clear(&argv);
232 			return -1;
233 		}
234 		strvec_clear(&argv);
235 	}
236 
237 	strbuf_release(&branch);
238 	return bisect_clean_state();
239 }
240 
log_commit(FILE * fp,char * fmt,const char * state,struct commit * commit)241 static void log_commit(FILE *fp, char *fmt, const char *state,
242 		       struct commit *commit)
243 {
244 	struct pretty_print_context pp = {0};
245 	struct strbuf commit_msg = STRBUF_INIT;
246 	char *label = xstrfmt(fmt, state);
247 
248 	format_commit_message(commit, "%s", &commit_msg, &pp);
249 
250 	fprintf(fp, "# %s: [%s] %s\n", label, oid_to_hex(&commit->object.oid),
251 		commit_msg.buf);
252 
253 	strbuf_release(&commit_msg);
254 	free(label);
255 }
256 
bisect_write(const char * state,const char * rev,const struct bisect_terms * terms,int nolog)257 static int bisect_write(const char *state, const char *rev,
258 			const struct bisect_terms *terms, int nolog)
259 {
260 	struct strbuf tag = STRBUF_INIT;
261 	struct object_id oid;
262 	struct commit *commit;
263 	FILE *fp = NULL;
264 	int res = 0;
265 
266 	if (!strcmp(state, terms->term_bad)) {
267 		strbuf_addf(&tag, "refs/bisect/%s", state);
268 	} else if (one_of(state, terms->term_good, "skip", NULL)) {
269 		strbuf_addf(&tag, "refs/bisect/%s-%s", state, rev);
270 	} else {
271 		res = error(_("Bad bisect_write argument: %s"), state);
272 		goto finish;
273 	}
274 
275 	if (get_oid(rev, &oid)) {
276 		res = error(_("couldn't get the oid of the rev '%s'"), rev);
277 		goto finish;
278 	}
279 
280 	if (update_ref(NULL, tag.buf, &oid, NULL, 0,
281 		       UPDATE_REFS_MSG_ON_ERR)) {
282 		res = -1;
283 		goto finish;
284 	}
285 
286 	fp = fopen(git_path_bisect_log(), "a");
287 	if (!fp) {
288 		res = error_errno(_("couldn't open the file '%s'"), git_path_bisect_log());
289 		goto finish;
290 	}
291 
292 	commit = lookup_commit_reference(the_repository, &oid);
293 	log_commit(fp, "%s", state, commit);
294 
295 	if (!nolog)
296 		fprintf(fp, "git bisect %s %s\n", state, rev);
297 
298 finish:
299 	if (fp)
300 		fclose(fp);
301 	strbuf_release(&tag);
302 	return res;
303 }
304 
check_and_set_terms(struct bisect_terms * terms,const char * cmd)305 static int check_and_set_terms(struct bisect_terms *terms, const char *cmd)
306 {
307 	int has_term_file = !is_empty_or_missing_file(git_path_bisect_terms());
308 
309 	if (one_of(cmd, "skip", "start", "terms", NULL))
310 		return 0;
311 
312 	if (has_term_file && strcmp(cmd, terms->term_bad) &&
313 	    strcmp(cmd, terms->term_good))
314 		return error(_("Invalid command: you're currently in a "
315 				"%s/%s bisect"), terms->term_bad,
316 				terms->term_good);
317 
318 	if (!has_term_file) {
319 		if (one_of(cmd, "bad", "good", NULL)) {
320 			set_terms(terms, "bad", "good");
321 			return write_terms(terms->term_bad, terms->term_good);
322 		}
323 		if (one_of(cmd, "new", "old", NULL)) {
324 			set_terms(terms, "new", "old");
325 			return write_terms(terms->term_bad, terms->term_good);
326 		}
327 	}
328 
329 	return 0;
330 }
331 
mark_good(const char * refname,const struct object_id * oid,int flag,void * cb_data)332 static int mark_good(const char *refname, const struct object_id *oid,
333 		     int flag, void *cb_data)
334 {
335 	int *m_good = (int *)cb_data;
336 	*m_good = 0;
337 	return 1;
338 }
339 
340 static const char need_bad_and_good_revision_warning[] =
341 	N_("You need to give me at least one %s and %s revision.\n"
342 	   "You can use \"git bisect %s\" and \"git bisect %s\" for that.");
343 
344 static const char need_bisect_start_warning[] =
345 	N_("You need to start by \"git bisect start\".\n"
346 	   "You then need to give me at least one %s and %s revision.\n"
347 	   "You can use \"git bisect %s\" and \"git bisect %s\" for that.");
348 
decide_next(const struct bisect_terms * terms,const char * current_term,int missing_good,int missing_bad)349 static int decide_next(const struct bisect_terms *terms,
350 		       const char *current_term, int missing_good,
351 		       int missing_bad)
352 {
353 	if (!missing_good && !missing_bad)
354 		return 0;
355 	if (!current_term)
356 		return -1;
357 
358 	if (missing_good && !missing_bad &&
359 	    !strcmp(current_term, terms->term_good)) {
360 		char *yesno;
361 		/*
362 		 * have bad (or new) but not good (or old). We could bisect
363 		 * although this is less optimum.
364 		 */
365 		warning(_("bisecting only with a %s commit"), terms->term_bad);
366 		if (!isatty(0))
367 			return 0;
368 		/*
369 		 * TRANSLATORS: Make sure to include [Y] and [n] in your
370 		 * translation. The program will only accept English input
371 		 * at this point.
372 		 */
373 		yesno = git_prompt(_("Are you sure [Y/n]? "), PROMPT_ECHO);
374 		if (starts_with(yesno, "N") || starts_with(yesno, "n"))
375 			return -1;
376 		return 0;
377 	}
378 
379 	if (!is_empty_or_missing_file(git_path_bisect_start()))
380 		return error(_(need_bad_and_good_revision_warning),
381 			     vocab_bad, vocab_good, vocab_bad, vocab_good);
382 	else
383 		return error(_(need_bisect_start_warning),
384 			     vocab_good, vocab_bad, vocab_good, vocab_bad);
385 }
386 
bisect_next_check(const struct bisect_terms * terms,const char * current_term)387 static int bisect_next_check(const struct bisect_terms *terms,
388 			     const char *current_term)
389 {
390 	int missing_good = 1, missing_bad = 1;
391 	char *bad_ref = xstrfmt("refs/bisect/%s", terms->term_bad);
392 	char *good_glob = xstrfmt("%s-*", terms->term_good);
393 
394 	if (ref_exists(bad_ref))
395 		missing_bad = 0;
396 
397 	for_each_glob_ref_in(mark_good, good_glob, "refs/bisect/",
398 			     (void *) &missing_good);
399 
400 	free(good_glob);
401 	free(bad_ref);
402 
403 	return decide_next(terms, current_term, missing_good, missing_bad);
404 }
405 
get_terms(struct bisect_terms * terms)406 static int get_terms(struct bisect_terms *terms)
407 {
408 	struct strbuf str = STRBUF_INIT;
409 	FILE *fp = NULL;
410 	int res = 0;
411 
412 	fp = fopen(git_path_bisect_terms(), "r");
413 	if (!fp) {
414 		res = -1;
415 		goto finish;
416 	}
417 
418 	free_terms(terms);
419 	strbuf_getline_lf(&str, fp);
420 	terms->term_bad = strbuf_detach(&str, NULL);
421 	strbuf_getline_lf(&str, fp);
422 	terms->term_good = strbuf_detach(&str, NULL);
423 
424 finish:
425 	if (fp)
426 		fclose(fp);
427 	strbuf_release(&str);
428 	return res;
429 }
430 
bisect_terms(struct bisect_terms * terms,const char * option)431 static int bisect_terms(struct bisect_terms *terms, const char *option)
432 {
433 	if (get_terms(terms))
434 		return error(_("no terms defined"));
435 
436 	if (option == NULL) {
437 		printf(_("Your current terms are %s for the old state\n"
438 			 "and %s for the new state.\n"),
439 		       terms->term_good, terms->term_bad);
440 		return 0;
441 	}
442 	if (one_of(option, "--term-good", "--term-old", NULL))
443 		printf("%s\n", terms->term_good);
444 	else if (one_of(option, "--term-bad", "--term-new", NULL))
445 		printf("%s\n", terms->term_bad);
446 	else
447 		return error(_("invalid argument %s for 'git bisect terms'.\n"
448 			       "Supported options are: "
449 			       "--term-good|--term-old and "
450 			       "--term-bad|--term-new."), option);
451 
452 	return 0;
453 }
454 
bisect_append_log_quoted(const char ** argv)455 static int bisect_append_log_quoted(const char **argv)
456 {
457 	int res = 0;
458 	FILE *fp = fopen(git_path_bisect_log(), "a");
459 	struct strbuf orig_args = STRBUF_INIT;
460 
461 	if (!fp)
462 		return -1;
463 
464 	if (fprintf(fp, "git bisect start") < 1) {
465 		res = -1;
466 		goto finish;
467 	}
468 
469 	sq_quote_argv(&orig_args, argv);
470 	if (fprintf(fp, "%s\n", orig_args.buf) < 1)
471 		res = -1;
472 
473 finish:
474 	fclose(fp);
475 	strbuf_release(&orig_args);
476 	return res;
477 }
478 
add_bisect_ref(const char * refname,const struct object_id * oid,int flags,void * cb)479 static int add_bisect_ref(const char *refname, const struct object_id *oid,
480 			  int flags, void *cb)
481 {
482 	struct add_bisect_ref_data *data = cb;
483 
484 	add_pending_oid(data->revs, refname, oid, data->object_flags);
485 
486 	return 0;
487 }
488 
prepare_revs(struct bisect_terms * terms,struct rev_info * revs)489 static int prepare_revs(struct bisect_terms *terms, struct rev_info *revs)
490 {
491 	int res = 0;
492 	struct add_bisect_ref_data cb = { revs };
493 	char *good = xstrfmt("%s-*", terms->term_good);
494 
495 	/*
496 	 * We cannot use terms->term_bad directly in
497 	 * for_each_glob_ref_in() and we have to append a '*' to it,
498 	 * otherwise for_each_glob_ref_in() will append '/' and '*'.
499 	 */
500 	char *bad = xstrfmt("%s*", terms->term_bad);
501 
502 	/*
503 	 * It is important to reset the flags used by revision walks
504 	 * as the previous call to bisect_next_all() in turn
505 	 * sets up a revision walk.
506 	 */
507 	reset_revision_walk();
508 	init_revisions(revs, NULL);
509 	setup_revisions(0, NULL, revs, NULL);
510 	for_each_glob_ref_in(add_bisect_ref, bad, "refs/bisect/", &cb);
511 	cb.object_flags = UNINTERESTING;
512 	for_each_glob_ref_in(add_bisect_ref, good, "refs/bisect/", &cb);
513 	if (prepare_revision_walk(revs))
514 		res = error(_("revision walk setup failed\n"));
515 
516 	free(good);
517 	free(bad);
518 	return res;
519 }
520 
bisect_skipped_commits(struct bisect_terms * terms)521 static int bisect_skipped_commits(struct bisect_terms *terms)
522 {
523 	int res;
524 	FILE *fp = NULL;
525 	struct rev_info revs;
526 	struct commit *commit;
527 	struct pretty_print_context pp = {0};
528 	struct strbuf commit_name = STRBUF_INIT;
529 
530 	res = prepare_revs(terms, &revs);
531 	if (res)
532 		return res;
533 
534 	fp = fopen(git_path_bisect_log(), "a");
535 	if (!fp)
536 		return error_errno(_("could not open '%s' for appending"),
537 				  git_path_bisect_log());
538 
539 	if (fprintf(fp, "# only skipped commits left to test\n") < 0)
540 		return error_errno(_("failed to write to '%s'"), git_path_bisect_log());
541 
542 	while ((commit = get_revision(&revs)) != NULL) {
543 		strbuf_reset(&commit_name);
544 		format_commit_message(commit, "%s",
545 				      &commit_name, &pp);
546 		fprintf(fp, "# possible first %s commit: [%s] %s\n",
547 			terms->term_bad, oid_to_hex(&commit->object.oid),
548 			commit_name.buf);
549 	}
550 
551 	/*
552 	 * Reset the flags used by revision walks in case
553 	 * there is another revision walk after this one.
554 	 */
555 	reset_revision_walk();
556 
557 	strbuf_release(&commit_name);
558 	fclose(fp);
559 	return 0;
560 }
561 
bisect_successful(struct bisect_terms * terms)562 static int bisect_successful(struct bisect_terms *terms)
563 {
564 	struct object_id oid;
565 	struct commit *commit;
566 	struct pretty_print_context pp = {0};
567 	struct strbuf commit_name = STRBUF_INIT;
568 	char *bad_ref = xstrfmt("refs/bisect/%s",terms->term_bad);
569 	int res;
570 
571 	read_ref(bad_ref, &oid);
572 	commit = lookup_commit_reference_by_name(bad_ref);
573 	format_commit_message(commit, "%s", &commit_name, &pp);
574 
575 	res = append_to_file(git_path_bisect_log(), "# first %s commit: [%s] %s\n",
576 			    terms->term_bad, oid_to_hex(&commit->object.oid),
577 			    commit_name.buf);
578 
579 	strbuf_release(&commit_name);
580 	free(bad_ref);
581 	return res;
582 }
583 
bisect_next(struct bisect_terms * terms,const char * prefix)584 static enum bisect_error bisect_next(struct bisect_terms *terms, const char *prefix)
585 {
586 	enum bisect_error res;
587 
588 	if (bisect_autostart(terms))
589 		return BISECT_FAILED;
590 
591 	if (bisect_next_check(terms, terms->term_good))
592 		return BISECT_FAILED;
593 
594 	/* Perform all bisection computation */
595 	res = bisect_next_all(the_repository, prefix);
596 
597 	if (res == BISECT_INTERNAL_SUCCESS_1ST_BAD_FOUND) {
598 		res = bisect_successful(terms);
599 		return res ? res : BISECT_INTERNAL_SUCCESS_1ST_BAD_FOUND;
600 	} else if (res == BISECT_ONLY_SKIPPED_LEFT) {
601 		res = bisect_skipped_commits(terms);
602 		return res ? res : BISECT_ONLY_SKIPPED_LEFT;
603 	}
604 	return res;
605 }
606 
bisect_auto_next(struct bisect_terms * terms,const char * prefix)607 static enum bisect_error bisect_auto_next(struct bisect_terms *terms, const char *prefix)
608 {
609 	if (bisect_next_check(terms, NULL))
610 		return BISECT_OK;
611 
612 	return bisect_next(terms, prefix);
613 }
614 
bisect_start(struct bisect_terms * terms,const char ** argv,int argc)615 static enum bisect_error bisect_start(struct bisect_terms *terms, const char **argv, int argc)
616 {
617 	int no_checkout = 0;
618 	int first_parent_only = 0;
619 	int i, has_double_dash = 0, must_write_terms = 0, bad_seen = 0;
620 	int flags, pathspec_pos;
621 	enum bisect_error res = BISECT_OK;
622 	struct string_list revs = STRING_LIST_INIT_DUP;
623 	struct string_list states = STRING_LIST_INIT_DUP;
624 	struct strbuf start_head = STRBUF_INIT;
625 	struct strbuf bisect_names = STRBUF_INIT;
626 	struct object_id head_oid;
627 	struct object_id oid;
628 	const char *head;
629 
630 	if (is_bare_repository())
631 		no_checkout = 1;
632 
633 	/*
634 	 * Check for one bad and then some good revisions
635 	 */
636 	for (i = 0; i < argc; i++) {
637 		if (!strcmp(argv[i], "--")) {
638 			has_double_dash = 1;
639 			break;
640 		}
641 	}
642 
643 	for (i = 0; i < argc; i++) {
644 		const char *arg = argv[i];
645 		if (!strcmp(argv[i], "--")) {
646 			break;
647 		} else if (!strcmp(arg, "--no-checkout")) {
648 			no_checkout = 1;
649 		} else if (!strcmp(arg, "--first-parent")) {
650 			first_parent_only = 1;
651 		} else if (!strcmp(arg, "--term-good") ||
652 			 !strcmp(arg, "--term-old")) {
653 			i++;
654 			if (argc <= i)
655 				return error(_("'' is not a valid term"));
656 			must_write_terms = 1;
657 			free((void *) terms->term_good);
658 			terms->term_good = xstrdup(argv[i]);
659 		} else if (skip_prefix(arg, "--term-good=", &arg) ||
660 			   skip_prefix(arg, "--term-old=", &arg)) {
661 			must_write_terms = 1;
662 			free((void *) terms->term_good);
663 			terms->term_good = xstrdup(arg);
664 		} else if (!strcmp(arg, "--term-bad") ||
665 			 !strcmp(arg, "--term-new")) {
666 			i++;
667 			if (argc <= i)
668 				return error(_("'' is not a valid term"));
669 			must_write_terms = 1;
670 			free((void *) terms->term_bad);
671 			terms->term_bad = xstrdup(argv[i]);
672 		} else if (skip_prefix(arg, "--term-bad=", &arg) ||
673 			   skip_prefix(arg, "--term-new=", &arg)) {
674 			must_write_terms = 1;
675 			free((void *) terms->term_bad);
676 			terms->term_bad = xstrdup(arg);
677 		} else if (starts_with(arg, "--")) {
678 			return error(_("unrecognized option: '%s'"), arg);
679 		} else if (!get_oidf(&oid, "%s^{commit}", arg)) {
680 			string_list_append(&revs, oid_to_hex(&oid));
681 		} else if (has_double_dash) {
682 			die(_("'%s' does not appear to be a valid "
683 			      "revision"), arg);
684 		} else {
685 			break;
686 		}
687 	}
688 	pathspec_pos = i;
689 
690 	/*
691 	 * The user ran "git bisect start <sha1> <sha1>", hence did not
692 	 * explicitly specify the terms, but we are already starting to
693 	 * set references named with the default terms, and won't be able
694 	 * to change afterwards.
695 	 */
696 	if (revs.nr)
697 		must_write_terms = 1;
698 	for (i = 0; i < revs.nr; i++) {
699 		if (bad_seen) {
700 			string_list_append(&states, terms->term_good);
701 		} else {
702 			bad_seen = 1;
703 			string_list_append(&states, terms->term_bad);
704 		}
705 	}
706 
707 	/*
708 	 * Verify HEAD
709 	 */
710 	head = resolve_ref_unsafe("HEAD", 0, &head_oid, &flags);
711 	if (!head)
712 		if (get_oid("HEAD", &head_oid))
713 			return error(_("bad HEAD - I need a HEAD"));
714 
715 	/*
716 	 * Check if we are bisecting
717 	 */
718 	if (!is_empty_or_missing_file(git_path_bisect_start())) {
719 		/* Reset to the rev from where we started */
720 		strbuf_read_file(&start_head, git_path_bisect_start(), 0);
721 		strbuf_trim(&start_head);
722 		if (!no_checkout) {
723 			struct strvec argv = STRVEC_INIT;
724 
725 			strvec_pushl(&argv, "checkout", start_head.buf,
726 				     "--", NULL);
727 			if (run_command_v_opt(argv.v, RUN_GIT_CMD)) {
728 				res = error(_("checking out '%s' failed."
729 						 " Try 'git bisect start "
730 						 "<valid-branch>'."),
731 					       start_head.buf);
732 				goto finish;
733 			}
734 		}
735 	} else {
736 		/* Get the rev from where we start. */
737 		if (!get_oid(head, &head_oid) &&
738 		    !starts_with(head, "refs/heads/")) {
739 			strbuf_reset(&start_head);
740 			strbuf_addstr(&start_head, oid_to_hex(&head_oid));
741 		} else if (!get_oid(head, &head_oid) &&
742 			   skip_prefix(head, "refs/heads/", &head)) {
743 			/*
744 			 * This error message should only be triggered by
745 			 * cogito usage, and cogito users should understand
746 			 * it relates to cg-seek.
747 			 */
748 			if (!is_empty_or_missing_file(git_path_head_name()))
749 				return error(_("won't bisect on cg-seek'ed tree"));
750 			strbuf_addstr(&start_head, head);
751 		} else {
752 			return error(_("bad HEAD - strange symbolic ref"));
753 		}
754 	}
755 
756 	/*
757 	 * Get rid of any old bisect state.
758 	 */
759 	if (bisect_clean_state())
760 		return BISECT_FAILED;
761 
762 	/*
763 	 * Write new start state
764 	 */
765 	write_file(git_path_bisect_start(), "%s\n", start_head.buf);
766 
767 	if (first_parent_only)
768 		write_file(git_path_bisect_first_parent(), "\n");
769 
770 	if (no_checkout) {
771 		if (get_oid(start_head.buf, &oid) < 0) {
772 			res = error(_("invalid ref: '%s'"), start_head.buf);
773 			goto finish;
774 		}
775 		if (update_ref(NULL, "BISECT_HEAD", &oid, NULL, 0,
776 			       UPDATE_REFS_MSG_ON_ERR)) {
777 			res = BISECT_FAILED;
778 			goto finish;
779 		}
780 	}
781 
782 	if (pathspec_pos < argc - 1)
783 		sq_quote_argv(&bisect_names, argv + pathspec_pos);
784 	write_file(git_path_bisect_names(), "%s\n", bisect_names.buf);
785 
786 	for (i = 0; i < states.nr; i++)
787 		if (bisect_write(states.items[i].string,
788 				 revs.items[i].string, terms, 1)) {
789 			res = BISECT_FAILED;
790 			goto finish;
791 		}
792 
793 	if (must_write_terms && write_terms(terms->term_bad,
794 					    terms->term_good)) {
795 		res = BISECT_FAILED;
796 		goto finish;
797 	}
798 
799 	res = bisect_append_log_quoted(argv);
800 	if (res)
801 		res = BISECT_FAILED;
802 
803 finish:
804 	string_list_clear(&revs, 0);
805 	string_list_clear(&states, 0);
806 	strbuf_release(&start_head);
807 	strbuf_release(&bisect_names);
808 	if (res)
809 		return res;
810 
811 	res = bisect_auto_next(terms, NULL);
812 	if (!is_bisect_success(res))
813 		bisect_clean_state();
814 	return res;
815 }
816 
file_is_not_empty(const char * path)817 static inline int file_is_not_empty(const char *path)
818 {
819 	return !is_empty_or_missing_file(path);
820 }
821 
bisect_autostart(struct bisect_terms * terms)822 static int bisect_autostart(struct bisect_terms *terms)
823 {
824 	int res;
825 	const char *yesno;
826 
827 	if (file_is_not_empty(git_path_bisect_start()))
828 		return 0;
829 
830 	fprintf_ln(stderr, _("You need to start by \"git bisect "
831 			  "start\"\n"));
832 
833 	if (!isatty(STDIN_FILENO))
834 		return -1;
835 
836 	/*
837 	 * TRANSLATORS: Make sure to include [Y] and [n] in your
838 	 * translation. The program will only accept English input
839 	 * at this point.
840 	 */
841 	yesno = git_prompt(_("Do you want me to do it for you "
842 			     "[Y/n]? "), PROMPT_ECHO);
843 	res = tolower(*yesno) == 'n' ?
844 		-1 : bisect_start(terms, empty_strvec, 0);
845 
846 	return res;
847 }
848 
bisect_state(struct bisect_terms * terms,const char ** argv,int argc)849 static enum bisect_error bisect_state(struct bisect_terms *terms, const char **argv,
850 				      int argc)
851 {
852 	const char *state;
853 	int i, verify_expected = 1;
854 	struct object_id oid, expected;
855 	struct strbuf buf = STRBUF_INIT;
856 	struct oid_array revs = OID_ARRAY_INIT;
857 
858 	if (!argc)
859 		return error(_("Please call `--bisect-state` with at least one argument"));
860 
861 	if (bisect_autostart(terms))
862 		return BISECT_FAILED;
863 
864 	state = argv[0];
865 	if (check_and_set_terms(terms, state) ||
866 	    !one_of(state, terms->term_good, terms->term_bad, "skip", NULL))
867 		return BISECT_FAILED;
868 
869 	argv++;
870 	argc--;
871 	if (argc > 1 && !strcmp(state, terms->term_bad))
872 		return error(_("'git bisect %s' can take only one argument."), terms->term_bad);
873 
874 	if (argc == 0) {
875 		const char *head = "BISECT_HEAD";
876 		enum get_oid_result res_head = get_oid(head, &oid);
877 
878 		if (res_head == MISSING_OBJECT) {
879 			head = "HEAD";
880 			res_head = get_oid(head, &oid);
881 		}
882 
883 		if (res_head)
884 			error(_("Bad rev input: %s"), head);
885 		oid_array_append(&revs, &oid);
886 	}
887 
888 	/*
889 	 * All input revs must be checked before executing bisect_write()
890 	 * to discard junk revs.
891 	 */
892 
893 	for (; argc; argc--, argv++) {
894 		struct commit *commit;
895 
896 		if (get_oid(*argv, &oid)){
897 			error(_("Bad rev input: %s"), *argv);
898 			oid_array_clear(&revs);
899 			return BISECT_FAILED;
900 		}
901 
902 		commit = lookup_commit_reference(the_repository, &oid);
903 		if (!commit)
904 			die(_("Bad rev input (not a commit): %s"), *argv);
905 
906 		oid_array_append(&revs, &commit->object.oid);
907 	}
908 
909 	if (strbuf_read_file(&buf, git_path_bisect_expected_rev(), 0) < the_hash_algo->hexsz ||
910 	    get_oid_hex(buf.buf, &expected) < 0)
911 		verify_expected = 0; /* Ignore invalid file contents */
912 	strbuf_release(&buf);
913 
914 	for (i = 0; i < revs.nr; i++) {
915 		if (bisect_write(state, oid_to_hex(&revs.oid[i]), terms, 0)) {
916 			oid_array_clear(&revs);
917 			return BISECT_FAILED;
918 		}
919 		if (verify_expected && !oideq(&revs.oid[i], &expected)) {
920 			unlink_or_warn(git_path_bisect_ancestors_ok());
921 			unlink_or_warn(git_path_bisect_expected_rev());
922 			verify_expected = 0;
923 		}
924 	}
925 
926 	oid_array_clear(&revs);
927 	return bisect_auto_next(terms, NULL);
928 }
929 
bisect_log(void)930 static enum bisect_error bisect_log(void)
931 {
932 	int fd, status;
933 	const char* filename = git_path_bisect_log();
934 
935 	if (is_empty_or_missing_file(filename))
936 		return error(_("We are not bisecting."));
937 
938 	fd = open(filename, O_RDONLY);
939 	if (fd < 0)
940 		return BISECT_FAILED;
941 
942 	status = copy_fd(fd, STDOUT_FILENO);
943 	close(fd);
944 	return status ? BISECT_FAILED : BISECT_OK;
945 }
946 
process_replay_line(struct bisect_terms * terms,struct strbuf * line)947 static int process_replay_line(struct bisect_terms *terms, struct strbuf *line)
948 {
949 	const char *p = line->buf + strspn(line->buf, " \t");
950 	char *word_end, *rev;
951 
952 	if ((!skip_prefix(p, "git bisect", &p) &&
953 	!skip_prefix(p, "git-bisect", &p)) || !isspace(*p))
954 		return 0;
955 	p += strspn(p, " \t");
956 
957 	word_end = (char *)p + strcspn(p, " \t");
958 	rev = word_end + strspn(word_end, " \t");
959 	*word_end = '\0'; /* NUL-terminate the word */
960 
961 	get_terms(terms);
962 	if (check_and_set_terms(terms, p))
963 		return -1;
964 
965 	if (!strcmp(p, "start")) {
966 		struct strvec argv = STRVEC_INIT;
967 		int res;
968 		sq_dequote_to_strvec(rev, &argv);
969 		res = bisect_start(terms, argv.v, argv.nr);
970 		strvec_clear(&argv);
971 		return res;
972 	}
973 
974 	if (one_of(p, terms->term_good,
975 	   terms->term_bad, "skip", NULL))
976 		return bisect_write(p, rev, terms, 0);
977 
978 	if (!strcmp(p, "terms")) {
979 		struct strvec argv = STRVEC_INIT;
980 		int res;
981 		sq_dequote_to_strvec(rev, &argv);
982 		res = bisect_terms(terms, argv.nr == 1 ? argv.v[0] : NULL);
983 		strvec_clear(&argv);
984 		return res;
985 	}
986 	error(_("'%s'?? what are you talking about?"), p);
987 
988 	return -1;
989 }
990 
bisect_replay(struct bisect_terms * terms,const char * filename)991 static enum bisect_error bisect_replay(struct bisect_terms *terms, const char *filename)
992 {
993 	FILE *fp = NULL;
994 	enum bisect_error res = BISECT_OK;
995 	struct strbuf line = STRBUF_INIT;
996 
997 	if (is_empty_or_missing_file(filename))
998 		return error(_("cannot read file '%s' for replaying"), filename);
999 
1000 	if (bisect_reset(NULL))
1001 		return BISECT_FAILED;
1002 
1003 	fp = fopen(filename, "r");
1004 	if (!fp)
1005 		return BISECT_FAILED;
1006 
1007 	while ((strbuf_getline(&line, fp) != EOF) && !res)
1008 		res = process_replay_line(terms, &line);
1009 
1010 	strbuf_release(&line);
1011 	fclose(fp);
1012 
1013 	if (res)
1014 		return BISECT_FAILED;
1015 
1016 	return bisect_auto_next(terms, NULL);
1017 }
1018 
bisect_skip(struct bisect_terms * terms,const char ** argv,int argc)1019 static enum bisect_error bisect_skip(struct bisect_terms *terms, const char **argv, int argc)
1020 {
1021 	int i;
1022 	enum bisect_error res;
1023 	struct strvec argv_state = STRVEC_INIT;
1024 
1025 	strvec_push(&argv_state, "skip");
1026 
1027 	for (i = 0; i < argc; i++) {
1028 		const char *dotdot = strstr(argv[i], "..");
1029 
1030 		if (dotdot) {
1031 			struct rev_info revs;
1032 			struct commit *commit;
1033 
1034 			init_revisions(&revs, NULL);
1035 			setup_revisions(2, argv + i - 1, &revs, NULL);
1036 
1037 			if (prepare_revision_walk(&revs))
1038 				die(_("revision walk setup failed\n"));
1039 			while ((commit = get_revision(&revs)) != NULL)
1040 				strvec_push(&argv_state,
1041 						oid_to_hex(&commit->object.oid));
1042 
1043 			reset_revision_walk();
1044 		} else {
1045 			strvec_push(&argv_state, argv[i]);
1046 		}
1047 	}
1048 	res = bisect_state(terms, argv_state.v, argv_state.nr);
1049 
1050 	strvec_clear(&argv_state);
1051 	return res;
1052 }
1053 
bisect_visualize(struct bisect_terms * terms,const char ** argv,int argc)1054 static int bisect_visualize(struct bisect_terms *terms, const char **argv, int argc)
1055 {
1056 	struct strvec args = STRVEC_INIT;
1057 	int flags = RUN_COMMAND_NO_STDIN, res = 0;
1058 	struct strbuf sb = STRBUF_INIT;
1059 
1060 	if (bisect_next_check(terms, NULL) != 0)
1061 		return BISECT_FAILED;
1062 
1063 	if (!argc) {
1064 		if ((getenv("DISPLAY") || getenv("SESSIONNAME") || getenv("MSYSTEM") ||
1065 		     getenv("SECURITYSESSIONID")) && exists_in_PATH("gitk")) {
1066 			strvec_push(&args, "gitk");
1067 		} else {
1068 			strvec_push(&args, "log");
1069 			flags |= RUN_GIT_CMD;
1070 		}
1071 	} else {
1072 		if (argv[0][0] == '-') {
1073 			strvec_push(&args, "log");
1074 			flags |= RUN_GIT_CMD;
1075 		} else if (strcmp(argv[0], "tig") && !starts_with(argv[0], "git"))
1076 			flags |= RUN_GIT_CMD;
1077 
1078 		strvec_pushv(&args, argv);
1079 	}
1080 
1081 	strvec_pushl(&args, "--bisect", "--", NULL);
1082 
1083 	strbuf_read_file(&sb, git_path_bisect_names(), 0);
1084 	sq_dequote_to_strvec(sb.buf, &args);
1085 	strbuf_release(&sb);
1086 
1087 	res = run_command_v_opt(args.v, flags);
1088 	strvec_clear(&args);
1089 	return res;
1090 }
1091 
bisect_run(struct bisect_terms * terms,const char ** argv,int argc)1092 static int bisect_run(struct bisect_terms *terms, const char **argv, int argc)
1093 {
1094 	int res = BISECT_OK;
1095 	struct strbuf command = STRBUF_INIT;
1096 	struct strvec args = STRVEC_INIT;
1097 	struct strvec run_args = STRVEC_INIT;
1098 	const char *new_state;
1099 	int temporary_stdout_fd, saved_stdout;
1100 
1101 	if (bisect_next_check(terms, NULL))
1102 		return BISECT_FAILED;
1103 
1104 	if (argc)
1105 		sq_quote_argv(&command, argv);
1106 	else {
1107 		error(_("bisect run failed: no command provided."));
1108 		return BISECT_FAILED;
1109 	}
1110 
1111 	strvec_push(&run_args, command.buf);
1112 
1113 	while (1) {
1114 		strvec_clear(&args);
1115 
1116 		printf(_("running %s\n"), command.buf);
1117 		res = run_command_v_opt(run_args.v, RUN_USING_SHELL);
1118 
1119 		if (res < 0 || 128 <= res) {
1120 			error(_("bisect run failed: exit code %d from"
1121 				" '%s' is < 0 or >= 128"), res, command.buf);
1122 			strbuf_release(&command);
1123 			return res;
1124 		}
1125 
1126 		if (res == 125)
1127 			new_state = "skip";
1128 		else if (!res)
1129 			new_state = terms->term_good;
1130 		else
1131 			new_state = terms->term_bad;
1132 
1133 		temporary_stdout_fd = open(git_path_bisect_run(), O_CREAT | O_WRONLY | O_TRUNC, 0666);
1134 
1135 		if (temporary_stdout_fd < 0)
1136 			return error_errno(_("cannot open file '%s' for writing"), git_path_bisect_run());
1137 
1138 		fflush(stdout);
1139 		saved_stdout = dup(1);
1140 		dup2(temporary_stdout_fd, 1);
1141 
1142 		res = bisect_state(terms, &new_state, 1);
1143 
1144 		fflush(stdout);
1145 		dup2(saved_stdout, 1);
1146 		close(saved_stdout);
1147 		close(temporary_stdout_fd);
1148 
1149 		print_file_to_stdout(git_path_bisect_run());
1150 
1151 		if (res == BISECT_ONLY_SKIPPED_LEFT)
1152 			error(_("bisect run cannot continue any more"));
1153 		else if (res == BISECT_INTERNAL_SUCCESS_MERGE_BASE) {
1154 			printf(_("bisect run success"));
1155 			res = BISECT_OK;
1156 		} else if (res == BISECT_INTERNAL_SUCCESS_1ST_BAD_FOUND) {
1157 			printf(_("bisect found first bad commit"));
1158 			res = BISECT_OK;
1159 		} else if (res) {
1160 			error(_("bisect run failed: 'git bisect--helper --bisect-state"
1161 			" %s' exited with error code %d"), args.v[0], res);
1162 		} else {
1163 			continue;
1164 		}
1165 
1166 		strbuf_release(&command);
1167 		strvec_clear(&args);
1168 		strvec_clear(&run_args);
1169 		return res;
1170 	}
1171 }
1172 
cmd_bisect__helper(int argc,const char ** argv,const char * prefix)1173 int cmd_bisect__helper(int argc, const char **argv, const char *prefix)
1174 {
1175 	enum {
1176 		BISECT_RESET = 1,
1177 		BISECT_NEXT_CHECK,
1178 		BISECT_TERMS,
1179 		BISECT_START,
1180 		BISECT_AUTOSTART,
1181 		BISECT_NEXT,
1182 		BISECT_STATE,
1183 		BISECT_LOG,
1184 		BISECT_REPLAY,
1185 		BISECT_SKIP,
1186 		BISECT_VISUALIZE,
1187 		BISECT_RUN,
1188 	} cmdmode = 0;
1189 	int res = 0, nolog = 0;
1190 	struct option options[] = {
1191 		OPT_CMDMODE(0, "bisect-reset", &cmdmode,
1192 			 N_("reset the bisection state"), BISECT_RESET),
1193 		OPT_CMDMODE(0, "bisect-next-check", &cmdmode,
1194 			 N_("check whether bad or good terms exist"), BISECT_NEXT_CHECK),
1195 		OPT_CMDMODE(0, "bisect-terms", &cmdmode,
1196 			 N_("print out the bisect terms"), BISECT_TERMS),
1197 		OPT_CMDMODE(0, "bisect-start", &cmdmode,
1198 			 N_("start the bisect session"), BISECT_START),
1199 		OPT_CMDMODE(0, "bisect-next", &cmdmode,
1200 			 N_("find the next bisection commit"), BISECT_NEXT),
1201 		OPT_CMDMODE(0, "bisect-state", &cmdmode,
1202 			 N_("mark the state of ref (or refs)"), BISECT_STATE),
1203 		OPT_CMDMODE(0, "bisect-log", &cmdmode,
1204 			 N_("list the bisection steps so far"), BISECT_LOG),
1205 		OPT_CMDMODE(0, "bisect-replay", &cmdmode,
1206 			 N_("replay the bisection process from the given file"), BISECT_REPLAY),
1207 		OPT_CMDMODE(0, "bisect-skip", &cmdmode,
1208 			 N_("skip some commits for checkout"), BISECT_SKIP),
1209 		OPT_CMDMODE(0, "bisect-visualize", &cmdmode,
1210 			 N_("visualize the bisection"), BISECT_VISUALIZE),
1211 		OPT_CMDMODE(0, "bisect-run", &cmdmode,
1212 			 N_("use <cmd>... to automatically bisect."), BISECT_RUN),
1213 		OPT_BOOL(0, "no-log", &nolog,
1214 			 N_("no log for BISECT_WRITE")),
1215 		OPT_END()
1216 	};
1217 	struct bisect_terms terms = { .term_good = NULL, .term_bad = NULL };
1218 
1219 	argc = parse_options(argc, argv, prefix, options,
1220 			     git_bisect_helper_usage,
1221 			     PARSE_OPT_KEEP_DASHDASH | PARSE_OPT_KEEP_UNKNOWN);
1222 
1223 	if (!cmdmode)
1224 		usage_with_options(git_bisect_helper_usage, options);
1225 
1226 	switch (cmdmode) {
1227 	case BISECT_RESET:
1228 		if (argc > 1)
1229 			return error(_("--bisect-reset requires either no argument or a commit"));
1230 		res = bisect_reset(argc ? argv[0] : NULL);
1231 		break;
1232 	case BISECT_TERMS:
1233 		if (argc > 1)
1234 			return error(_("--bisect-terms requires 0 or 1 argument"));
1235 		res = bisect_terms(&terms, argc == 1 ? argv[0] : NULL);
1236 		break;
1237 	case BISECT_START:
1238 		set_terms(&terms, "bad", "good");
1239 		res = bisect_start(&terms, argv, argc);
1240 		break;
1241 	case BISECT_NEXT:
1242 		if (argc)
1243 			return error(_("--bisect-next requires 0 arguments"));
1244 		get_terms(&terms);
1245 		res = bisect_next(&terms, prefix);
1246 		break;
1247 	case BISECT_STATE:
1248 		set_terms(&terms, "bad", "good");
1249 		get_terms(&terms);
1250 		res = bisect_state(&terms, argv, argc);
1251 		break;
1252 	case BISECT_LOG:
1253 		if (argc)
1254 			return error(_("--bisect-log requires 0 arguments"));
1255 		res = bisect_log();
1256 		break;
1257 	case BISECT_REPLAY:
1258 		if (argc != 1)
1259 			return error(_("no logfile given"));
1260 		set_terms(&terms, "bad", "good");
1261 		res = bisect_replay(&terms, argv[0]);
1262 		break;
1263 	case BISECT_SKIP:
1264 		set_terms(&terms, "bad", "good");
1265 		get_terms(&terms);
1266 		res = bisect_skip(&terms, argv, argc);
1267 		break;
1268 	case BISECT_VISUALIZE:
1269 		get_terms(&terms);
1270 		res = bisect_visualize(&terms, argv, argc);
1271 		break;
1272 	case BISECT_RUN:
1273 		if (!argc)
1274 			return error(_("bisect run failed: no command provided."));
1275 		get_terms(&terms);
1276 		res = bisect_run(&terms, argv, argc);
1277 		break;
1278 	default:
1279 		BUG("unknown subcommand %d", cmdmode);
1280 	}
1281 	free_terms(&terms);
1282 
1283 	/*
1284 	 * Handle early success
1285 	 * From check_merge_bases > check_good_are_ancestors_of_bad > bisect_next_all
1286 	 */
1287 	if ((res == BISECT_INTERNAL_SUCCESS_MERGE_BASE) || (res == BISECT_INTERNAL_SUCCESS_1ST_BAD_FOUND))
1288 		res = BISECT_OK;
1289 
1290 	return -res;
1291 }
1292