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