Merge branch 'ab/make-tags-cleanup'
[alt-git.git] / builtin / bisect--helper.c
blobf184eaeac6d0fd60ae8d255c458f6b873f2bd3cf
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"
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")
22 static const char * const git_bisect_helper_usage[] = {
23 N_("git bisect--helper --bisect-reset [<commit>]"),
24 N_("git bisect--helper --bisect-next-check <good_term> <bad_term> [<term>]"),
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 NULL
36 struct add_bisect_ref_data {
37 struct rev_info *revs;
38 unsigned int object_flags;
41 struct bisect_terms {
42 char *term_good;
43 char *term_bad;
46 static void free_terms(struct bisect_terms *terms)
48 FREE_AND_NULL(terms->term_good);
49 FREE_AND_NULL(terms->term_bad);
52 static void set_terms(struct bisect_terms *terms, const char *bad,
53 const char *good)
55 free((void *)terms->term_good);
56 terms->term_good = xstrdup(good);
57 free((void *)terms->term_bad);
58 terms->term_bad = xstrdup(bad);
61 static const char vocab_bad[] = "bad|new";
62 static const char vocab_good[] = "good|old";
64 static int bisect_autostart(struct bisect_terms *terms);
67 * Check whether the string `term` belongs to the set of strings
68 * included in the variable arguments.
70 LAST_ARG_MUST_BE_NULL
71 static int one_of(const char *term, ...)
73 int res = 0;
74 va_list matches;
75 const char *match;
77 va_start(matches, term);
78 while (!res && (match = va_arg(matches, const char *)))
79 res = !strcmp(term, match);
80 va_end(matches);
82 return res;
86 * return code BISECT_INTERNAL_SUCCESS_MERGE_BASE
87 * and BISECT_INTERNAL_SUCCESS_1ST_BAD_FOUND are codes
88 * that indicate special success.
91 static int is_bisect_success(enum bisect_error res)
93 return !res ||
94 res == BISECT_INTERNAL_SUCCESS_1ST_BAD_FOUND ||
95 res == BISECT_INTERNAL_SUCCESS_MERGE_BASE;
98 static int write_in_file(const char *path, const char *mode, const char *format, va_list args)
100 FILE *fp = NULL;
101 int res = 0;
103 if (strcmp(mode, "w") && strcmp(mode, "a"))
104 BUG("write-in-file does not support '%s' mode", mode);
105 fp = fopen(path, mode);
106 if (!fp)
107 return error_errno(_("cannot open file '%s' in mode '%s'"), path, mode);
108 res = vfprintf(fp, format, args);
110 if (res < 0) {
111 int saved_errno = errno;
112 fclose(fp);
113 errno = saved_errno;
114 return error_errno(_("could not write to file '%s'"), path);
117 return fclose(fp);
120 __attribute__((format (printf, 2, 3)))
121 static int write_to_file(const char *path, const char *format, ...)
123 int res;
124 va_list args;
126 va_start(args, format);
127 res = write_in_file(path, "w", format, args);
128 va_end(args);
130 return res;
133 __attribute__((format (printf, 2, 3)))
134 static int append_to_file(const char *path, const char *format, ...)
136 int res;
137 va_list args;
139 va_start(args, format);
140 res = write_in_file(path, "a", format, args);
141 va_end(args);
143 return res;
146 static int check_term_format(const char *term, const char *orig_term)
148 int res;
149 char *new_term = xstrfmt("refs/bisect/%s", term);
151 res = check_refname_format(new_term, 0);
152 free(new_term);
154 if (res)
155 return error(_("'%s' is not a valid term"), term);
157 if (one_of(term, "help", "start", "skip", "next", "reset",
158 "visualize", "view", "replay", "log", "run", "terms", NULL))
159 return error(_("can't use the builtin command '%s' as a term"), term);
162 * In theory, nothing prevents swapping completely good and bad,
163 * but this situation could be confusing and hasn't been tested
164 * enough. Forbid it for now.
167 if ((strcmp(orig_term, "bad") && one_of(term, "bad", "new", NULL)) ||
168 (strcmp(orig_term, "good") && one_of(term, "good", "old", NULL)))
169 return error(_("can't change the meaning of the term '%s'"), term);
171 return 0;
174 static int write_terms(const char *bad, const char *good)
176 int res;
178 if (!strcmp(bad, good))
179 return error(_("please use two different terms"));
181 if (check_term_format(bad, "bad") || check_term_format(good, "good"))
182 return -1;
184 res = write_to_file(git_path_bisect_terms(), "%s\n%s\n", bad, good);
186 return res;
189 static int bisect_reset(const char *commit)
191 struct strbuf branch = STRBUF_INIT;
193 if (!commit) {
194 if (strbuf_read_file(&branch, git_path_bisect_start(), 0) < 1) {
195 printf(_("We are not bisecting.\n"));
196 return 0;
198 strbuf_rtrim(&branch);
199 } else {
200 struct object_id oid;
202 if (get_oid_commit(commit, &oid))
203 return error(_("'%s' is not a valid commit"), commit);
204 strbuf_addstr(&branch, commit);
207 if (!ref_exists("BISECT_HEAD")) {
208 struct strvec argv = STRVEC_INIT;
210 strvec_pushl(&argv, "checkout", branch.buf, "--", NULL);
211 if (run_command_v_opt(argv.v, RUN_GIT_CMD)) {
212 error(_("could not check out original"
213 " HEAD '%s'. Try 'git bisect"
214 " reset <commit>'."), branch.buf);
215 strbuf_release(&branch);
216 strvec_clear(&argv);
217 return -1;
219 strvec_clear(&argv);
222 strbuf_release(&branch);
223 return bisect_clean_state();
226 static void log_commit(FILE *fp, char *fmt, const char *state,
227 struct commit *commit)
229 struct pretty_print_context pp = {0};
230 struct strbuf commit_msg = STRBUF_INIT;
231 char *label = xstrfmt(fmt, state);
233 format_commit_message(commit, "%s", &commit_msg, &pp);
235 fprintf(fp, "# %s: [%s] %s\n", label, oid_to_hex(&commit->object.oid),
236 commit_msg.buf);
238 strbuf_release(&commit_msg);
239 free(label);
242 static int bisect_write(const char *state, const char *rev,
243 const struct bisect_terms *terms, int nolog)
245 struct strbuf tag = STRBUF_INIT;
246 struct object_id oid;
247 struct commit *commit;
248 FILE *fp = NULL;
249 int res = 0;
251 if (!strcmp(state, terms->term_bad)) {
252 strbuf_addf(&tag, "refs/bisect/%s", state);
253 } else if (one_of(state, terms->term_good, "skip", NULL)) {
254 strbuf_addf(&tag, "refs/bisect/%s-%s", state, rev);
255 } else {
256 res = error(_("Bad bisect_write argument: %s"), state);
257 goto finish;
260 if (get_oid(rev, &oid)) {
261 res = error(_("couldn't get the oid of the rev '%s'"), rev);
262 goto finish;
265 if (update_ref(NULL, tag.buf, &oid, NULL, 0,
266 UPDATE_REFS_MSG_ON_ERR)) {
267 res = -1;
268 goto finish;
271 fp = fopen(git_path_bisect_log(), "a");
272 if (!fp) {
273 res = error_errno(_("couldn't open the file '%s'"), git_path_bisect_log());
274 goto finish;
277 commit = lookup_commit_reference(the_repository, &oid);
278 log_commit(fp, "%s", state, commit);
280 if (!nolog)
281 fprintf(fp, "git bisect %s %s\n", state, rev);
283 finish:
284 if (fp)
285 fclose(fp);
286 strbuf_release(&tag);
287 return res;
290 static int check_and_set_terms(struct bisect_terms *terms, const char *cmd)
292 int has_term_file = !is_empty_or_missing_file(git_path_bisect_terms());
294 if (one_of(cmd, "skip", "start", "terms", NULL))
295 return 0;
297 if (has_term_file && strcmp(cmd, terms->term_bad) &&
298 strcmp(cmd, terms->term_good))
299 return error(_("Invalid command: you're currently in a "
300 "%s/%s bisect"), terms->term_bad,
301 terms->term_good);
303 if (!has_term_file) {
304 if (one_of(cmd, "bad", "good", NULL)) {
305 set_terms(terms, "bad", "good");
306 return write_terms(terms->term_bad, terms->term_good);
308 if (one_of(cmd, "new", "old", NULL)) {
309 set_terms(terms, "new", "old");
310 return write_terms(terms->term_bad, terms->term_good);
314 return 0;
317 static int mark_good(const char *refname, const struct object_id *oid,
318 int flag, void *cb_data)
320 int *m_good = (int *)cb_data;
321 *m_good = 0;
322 return 1;
325 static const char need_bad_and_good_revision_warning[] =
326 N_("You need to give me at least one %s and %s revision.\n"
327 "You can use \"git bisect %s\" and \"git bisect %s\" for that.");
329 static const char need_bisect_start_warning[] =
330 N_("You need to start by \"git bisect start\".\n"
331 "You then need to give me at least one %s and %s revision.\n"
332 "You can use \"git bisect %s\" and \"git bisect %s\" for that.");
334 static int decide_next(const struct bisect_terms *terms,
335 const char *current_term, int missing_good,
336 int missing_bad)
338 if (!missing_good && !missing_bad)
339 return 0;
340 if (!current_term)
341 return -1;
343 if (missing_good && !missing_bad &&
344 !strcmp(current_term, terms->term_good)) {
345 char *yesno;
347 * have bad (or new) but not good (or old). We could bisect
348 * although this is less optimum.
350 warning(_("bisecting only with a %s commit"), terms->term_bad);
351 if (!isatty(0))
352 return 0;
354 * TRANSLATORS: Make sure to include [Y] and [n] in your
355 * translation. The program will only accept English input
356 * at this point.
358 yesno = git_prompt(_("Are you sure [Y/n]? "), PROMPT_ECHO);
359 if (starts_with(yesno, "N") || starts_with(yesno, "n"))
360 return -1;
361 return 0;
364 if (!is_empty_or_missing_file(git_path_bisect_start()))
365 return error(_(need_bad_and_good_revision_warning),
366 vocab_bad, vocab_good, vocab_bad, vocab_good);
367 else
368 return error(_(need_bisect_start_warning),
369 vocab_good, vocab_bad, vocab_good, vocab_bad);
372 static int bisect_next_check(const struct bisect_terms *terms,
373 const char *current_term)
375 int missing_good = 1, missing_bad = 1;
376 char *bad_ref = xstrfmt("refs/bisect/%s", terms->term_bad);
377 char *good_glob = xstrfmt("%s-*", terms->term_good);
379 if (ref_exists(bad_ref))
380 missing_bad = 0;
382 for_each_glob_ref_in(mark_good, good_glob, "refs/bisect/",
383 (void *) &missing_good);
385 free(good_glob);
386 free(bad_ref);
388 return decide_next(terms, current_term, missing_good, missing_bad);
391 static int get_terms(struct bisect_terms *terms)
393 struct strbuf str = STRBUF_INIT;
394 FILE *fp = NULL;
395 int res = 0;
397 fp = fopen(git_path_bisect_terms(), "r");
398 if (!fp) {
399 res = -1;
400 goto finish;
403 free_terms(terms);
404 strbuf_getline_lf(&str, fp);
405 terms->term_bad = strbuf_detach(&str, NULL);
406 strbuf_getline_lf(&str, fp);
407 terms->term_good = strbuf_detach(&str, NULL);
409 finish:
410 if (fp)
411 fclose(fp);
412 strbuf_release(&str);
413 return res;
416 static int bisect_terms(struct bisect_terms *terms, const char *option)
418 if (get_terms(terms))
419 return error(_("no terms defined"));
421 if (option == NULL) {
422 printf(_("Your current terms are %s for the old state\n"
423 "and %s for the new state.\n"),
424 terms->term_good, terms->term_bad);
425 return 0;
427 if (one_of(option, "--term-good", "--term-old", NULL))
428 printf("%s\n", terms->term_good);
429 else if (one_of(option, "--term-bad", "--term-new", NULL))
430 printf("%s\n", terms->term_bad);
431 else
432 return error(_("invalid argument %s for 'git bisect terms'.\n"
433 "Supported options are: "
434 "--term-good|--term-old and "
435 "--term-bad|--term-new."), option);
437 return 0;
440 static int bisect_append_log_quoted(const char **argv)
442 int res = 0;
443 FILE *fp = fopen(git_path_bisect_log(), "a");
444 struct strbuf orig_args = STRBUF_INIT;
446 if (!fp)
447 return -1;
449 if (fprintf(fp, "git bisect start") < 1) {
450 res = -1;
451 goto finish;
454 sq_quote_argv(&orig_args, argv);
455 if (fprintf(fp, "%s\n", orig_args.buf) < 1)
456 res = -1;
458 finish:
459 fclose(fp);
460 strbuf_release(&orig_args);
461 return res;
464 static int add_bisect_ref(const char *refname, const struct object_id *oid,
465 int flags, void *cb)
467 struct add_bisect_ref_data *data = cb;
469 add_pending_oid(data->revs, refname, oid, data->object_flags);
471 return 0;
474 static int prepare_revs(struct bisect_terms *terms, struct rev_info *revs)
476 int res = 0;
477 struct add_bisect_ref_data cb = { revs };
478 char *good = xstrfmt("%s-*", terms->term_good);
481 * We cannot use terms->term_bad directly in
482 * for_each_glob_ref_in() and we have to append a '*' to it,
483 * otherwise for_each_glob_ref_in() will append '/' and '*'.
485 char *bad = xstrfmt("%s*", terms->term_bad);
488 * It is important to reset the flags used by revision walks
489 * as the previous call to bisect_next_all() in turn
490 * sets up a revision walk.
492 reset_revision_walk();
493 init_revisions(revs, NULL);
494 setup_revisions(0, NULL, revs, NULL);
495 for_each_glob_ref_in(add_bisect_ref, bad, "refs/bisect/", &cb);
496 cb.object_flags = UNINTERESTING;
497 for_each_glob_ref_in(add_bisect_ref, good, "refs/bisect/", &cb);
498 if (prepare_revision_walk(revs))
499 res = error(_("revision walk setup failed\n"));
501 free(good);
502 free(bad);
503 return res;
506 static int bisect_skipped_commits(struct bisect_terms *terms)
508 int res;
509 FILE *fp = NULL;
510 struct rev_info revs;
511 struct commit *commit;
512 struct pretty_print_context pp = {0};
513 struct strbuf commit_name = STRBUF_INIT;
515 res = prepare_revs(terms, &revs);
516 if (res)
517 return res;
519 fp = fopen(git_path_bisect_log(), "a");
520 if (!fp)
521 return error_errno(_("could not open '%s' for appending"),
522 git_path_bisect_log());
524 if (fprintf(fp, "# only skipped commits left to test\n") < 0)
525 return error_errno(_("failed to write to '%s'"), git_path_bisect_log());
527 while ((commit = get_revision(&revs)) != NULL) {
528 strbuf_reset(&commit_name);
529 format_commit_message(commit, "%s",
530 &commit_name, &pp);
531 fprintf(fp, "# possible first %s commit: [%s] %s\n",
532 terms->term_bad, oid_to_hex(&commit->object.oid),
533 commit_name.buf);
537 * Reset the flags used by revision walks in case
538 * there is another revision walk after this one.
540 reset_revision_walk();
542 strbuf_release(&commit_name);
543 fclose(fp);
544 return 0;
547 static int bisect_successful(struct bisect_terms *terms)
549 struct object_id oid;
550 struct commit *commit;
551 struct pretty_print_context pp = {0};
552 struct strbuf commit_name = STRBUF_INIT;
553 char *bad_ref = xstrfmt("refs/bisect/%s",terms->term_bad);
554 int res;
556 read_ref(bad_ref, &oid);
557 commit = lookup_commit_reference_by_name(bad_ref);
558 format_commit_message(commit, "%s", &commit_name, &pp);
560 res = append_to_file(git_path_bisect_log(), "# first %s commit: [%s] %s\n",
561 terms->term_bad, oid_to_hex(&commit->object.oid),
562 commit_name.buf);
564 strbuf_release(&commit_name);
565 free(bad_ref);
566 return res;
569 static enum bisect_error bisect_next(struct bisect_terms *terms, const char *prefix)
571 enum bisect_error res;
573 if (bisect_autostart(terms))
574 return BISECT_FAILED;
576 if (bisect_next_check(terms, terms->term_good))
577 return BISECT_FAILED;
579 /* Perform all bisection computation */
580 res = bisect_next_all(the_repository, prefix);
582 if (res == BISECT_INTERNAL_SUCCESS_1ST_BAD_FOUND) {
583 res = bisect_successful(terms);
584 return res ? res : BISECT_INTERNAL_SUCCESS_1ST_BAD_FOUND;
585 } else if (res == BISECT_ONLY_SKIPPED_LEFT) {
586 res = bisect_skipped_commits(terms);
587 return res ? res : BISECT_ONLY_SKIPPED_LEFT;
589 return res;
592 static enum bisect_error bisect_auto_next(struct bisect_terms *terms, const char *prefix)
594 if (bisect_next_check(terms, NULL))
595 return BISECT_OK;
597 return bisect_next(terms, prefix);
600 static enum bisect_error bisect_start(struct bisect_terms *terms, const char **argv, int argc)
602 int no_checkout = 0;
603 int first_parent_only = 0;
604 int i, has_double_dash = 0, must_write_terms = 0, bad_seen = 0;
605 int flags, pathspec_pos;
606 enum bisect_error res = BISECT_OK;
607 struct string_list revs = STRING_LIST_INIT_DUP;
608 struct string_list states = STRING_LIST_INIT_DUP;
609 struct strbuf start_head = STRBUF_INIT;
610 struct strbuf bisect_names = STRBUF_INIT;
611 struct object_id head_oid;
612 struct object_id oid;
613 const char *head;
615 if (is_bare_repository())
616 no_checkout = 1;
619 * Check for one bad and then some good revisions
621 for (i = 0; i < argc; i++) {
622 if (!strcmp(argv[i], "--")) {
623 has_double_dash = 1;
624 break;
628 for (i = 0; i < argc; i++) {
629 const char *arg = argv[i];
630 if (!strcmp(argv[i], "--")) {
631 break;
632 } else if (!strcmp(arg, "--no-checkout")) {
633 no_checkout = 1;
634 } else if (!strcmp(arg, "--first-parent")) {
635 first_parent_only = 1;
636 } else if (!strcmp(arg, "--term-good") ||
637 !strcmp(arg, "--term-old")) {
638 i++;
639 if (argc <= i)
640 return error(_("'' is not a valid term"));
641 must_write_terms = 1;
642 free((void *) terms->term_good);
643 terms->term_good = xstrdup(argv[i]);
644 } else if (skip_prefix(arg, "--term-good=", &arg) ||
645 skip_prefix(arg, "--term-old=", &arg)) {
646 must_write_terms = 1;
647 free((void *) terms->term_good);
648 terms->term_good = xstrdup(arg);
649 } else if (!strcmp(arg, "--term-bad") ||
650 !strcmp(arg, "--term-new")) {
651 i++;
652 if (argc <= i)
653 return error(_("'' is not a valid term"));
654 must_write_terms = 1;
655 free((void *) terms->term_bad);
656 terms->term_bad = xstrdup(argv[i]);
657 } else if (skip_prefix(arg, "--term-bad=", &arg) ||
658 skip_prefix(arg, "--term-new=", &arg)) {
659 must_write_terms = 1;
660 free((void *) terms->term_bad);
661 terms->term_bad = xstrdup(arg);
662 } else if (starts_with(arg, "--")) {
663 return error(_("unrecognized option: '%s'"), arg);
664 } else if (!get_oidf(&oid, "%s^{commit}", arg)) {
665 string_list_append(&revs, oid_to_hex(&oid));
666 } else if (has_double_dash) {
667 die(_("'%s' does not appear to be a valid "
668 "revision"), arg);
669 } else {
670 break;
673 pathspec_pos = i;
676 * The user ran "git bisect start <sha1> <sha1>", hence did not
677 * explicitly specify the terms, but we are already starting to
678 * set references named with the default terms, and won't be able
679 * to change afterwards.
681 if (revs.nr)
682 must_write_terms = 1;
683 for (i = 0; i < revs.nr; i++) {
684 if (bad_seen) {
685 string_list_append(&states, terms->term_good);
686 } else {
687 bad_seen = 1;
688 string_list_append(&states, terms->term_bad);
693 * Verify HEAD
695 head = resolve_ref_unsafe("HEAD", 0, &head_oid, &flags);
696 if (!head)
697 if (get_oid("HEAD", &head_oid))
698 return error(_("bad HEAD - I need a HEAD"));
701 * Check if we are bisecting
703 if (!is_empty_or_missing_file(git_path_bisect_start())) {
704 /* Reset to the rev from where we started */
705 strbuf_read_file(&start_head, git_path_bisect_start(), 0);
706 strbuf_trim(&start_head);
707 if (!no_checkout) {
708 struct strvec argv = STRVEC_INIT;
710 strvec_pushl(&argv, "checkout", start_head.buf,
711 "--", NULL);
712 if (run_command_v_opt(argv.v, RUN_GIT_CMD)) {
713 res = error(_("checking out '%s' failed."
714 " Try 'git bisect start "
715 "<valid-branch>'."),
716 start_head.buf);
717 goto finish;
720 } else {
721 /* Get the rev from where we start. */
722 if (!get_oid(head, &head_oid) &&
723 !starts_with(head, "refs/heads/")) {
724 strbuf_reset(&start_head);
725 strbuf_addstr(&start_head, oid_to_hex(&head_oid));
726 } else if (!get_oid(head, &head_oid) &&
727 skip_prefix(head, "refs/heads/", &head)) {
729 * This error message should only be triggered by
730 * cogito usage, and cogito users should understand
731 * it relates to cg-seek.
733 if (!is_empty_or_missing_file(git_path_head_name()))
734 return error(_("won't bisect on cg-seek'ed tree"));
735 strbuf_addstr(&start_head, head);
736 } else {
737 return error(_("bad HEAD - strange symbolic ref"));
742 * Get rid of any old bisect state.
744 if (bisect_clean_state())
745 return BISECT_FAILED;
748 * Write new start state
750 write_file(git_path_bisect_start(), "%s\n", start_head.buf);
752 if (first_parent_only)
753 write_file(git_path_bisect_first_parent(), "\n");
755 if (no_checkout) {
756 if (get_oid(start_head.buf, &oid) < 0) {
757 res = error(_("invalid ref: '%s'"), start_head.buf);
758 goto finish;
760 if (update_ref(NULL, "BISECT_HEAD", &oid, NULL, 0,
761 UPDATE_REFS_MSG_ON_ERR)) {
762 res = BISECT_FAILED;
763 goto finish;
767 if (pathspec_pos < argc - 1)
768 sq_quote_argv(&bisect_names, argv + pathspec_pos);
769 write_file(git_path_bisect_names(), "%s\n", bisect_names.buf);
771 for (i = 0; i < states.nr; i++)
772 if (bisect_write(states.items[i].string,
773 revs.items[i].string, terms, 1)) {
774 res = BISECT_FAILED;
775 goto finish;
778 if (must_write_terms && write_terms(terms->term_bad,
779 terms->term_good)) {
780 res = BISECT_FAILED;
781 goto finish;
784 res = bisect_append_log_quoted(argv);
785 if (res)
786 res = BISECT_FAILED;
788 finish:
789 string_list_clear(&revs, 0);
790 string_list_clear(&states, 0);
791 strbuf_release(&start_head);
792 strbuf_release(&bisect_names);
793 if (res)
794 return res;
796 res = bisect_auto_next(terms, NULL);
797 if (!is_bisect_success(res))
798 bisect_clean_state();
799 return res;
802 static inline int file_is_not_empty(const char *path)
804 return !is_empty_or_missing_file(path);
807 static int bisect_autostart(struct bisect_terms *terms)
809 int res;
810 const char *yesno;
812 if (file_is_not_empty(git_path_bisect_start()))
813 return 0;
815 fprintf_ln(stderr, _("You need to start by \"git bisect "
816 "start\"\n"));
818 if (!isatty(STDIN_FILENO))
819 return -1;
822 * TRANSLATORS: Make sure to include [Y] and [n] in your
823 * translation. The program will only accept English input
824 * at this point.
826 yesno = git_prompt(_("Do you want me to do it for you "
827 "[Y/n]? "), PROMPT_ECHO);
828 res = tolower(*yesno) == 'n' ?
829 -1 : bisect_start(terms, empty_strvec, 0);
831 return res;
834 static enum bisect_error bisect_state(struct bisect_terms *terms, const char **argv,
835 int argc)
837 const char *state;
838 int i, verify_expected = 1;
839 struct object_id oid, expected;
840 struct strbuf buf = STRBUF_INIT;
841 struct oid_array revs = OID_ARRAY_INIT;
843 if (!argc)
844 return error(_("Please call `--bisect-state` with at least one argument"));
846 if (bisect_autostart(terms))
847 return BISECT_FAILED;
849 state = argv[0];
850 if (check_and_set_terms(terms, state) ||
851 !one_of(state, terms->term_good, terms->term_bad, "skip", NULL))
852 return BISECT_FAILED;
854 argv++;
855 argc--;
856 if (argc > 1 && !strcmp(state, terms->term_bad))
857 return error(_("'git bisect %s' can take only one argument."), terms->term_bad);
859 if (argc == 0) {
860 const char *head = "BISECT_HEAD";
861 enum get_oid_result res_head = get_oid(head, &oid);
863 if (res_head == MISSING_OBJECT) {
864 head = "HEAD";
865 res_head = get_oid(head, &oid);
868 if (res_head)
869 error(_("Bad rev input: %s"), head);
870 oid_array_append(&revs, &oid);
874 * All input revs must be checked before executing bisect_write()
875 * to discard junk revs.
878 for (; argc; argc--, argv++) {
879 struct commit *commit;
881 if (get_oid(*argv, &oid)){
882 error(_("Bad rev input: %s"), *argv);
883 oid_array_clear(&revs);
884 return BISECT_FAILED;
887 commit = lookup_commit_reference(the_repository, &oid);
888 if (!commit)
889 die(_("Bad rev input (not a commit): %s"), *argv);
891 oid_array_append(&revs, &commit->object.oid);
894 if (strbuf_read_file(&buf, git_path_bisect_expected_rev(), 0) < the_hash_algo->hexsz ||
895 get_oid_hex(buf.buf, &expected) < 0)
896 verify_expected = 0; /* Ignore invalid file contents */
897 strbuf_release(&buf);
899 for (i = 0; i < revs.nr; i++) {
900 if (bisect_write(state, oid_to_hex(&revs.oid[i]), terms, 0)) {
901 oid_array_clear(&revs);
902 return BISECT_FAILED;
904 if (verify_expected && !oideq(&revs.oid[i], &expected)) {
905 unlink_or_warn(git_path_bisect_ancestors_ok());
906 unlink_or_warn(git_path_bisect_expected_rev());
907 verify_expected = 0;
911 oid_array_clear(&revs);
912 return bisect_auto_next(terms, NULL);
915 static enum bisect_error bisect_log(void)
917 int fd, status;
918 const char* filename = git_path_bisect_log();
920 if (is_empty_or_missing_file(filename))
921 return error(_("We are not bisecting."));
923 fd = open(filename, O_RDONLY);
924 if (fd < 0)
925 return BISECT_FAILED;
927 status = copy_fd(fd, STDOUT_FILENO);
928 close(fd);
929 return status ? BISECT_FAILED : BISECT_OK;
932 static int process_replay_line(struct bisect_terms *terms, struct strbuf *line)
934 const char *p = line->buf + strspn(line->buf, " \t");
935 char *word_end, *rev;
937 if ((!skip_prefix(p, "git bisect", &p) &&
938 !skip_prefix(p, "git-bisect", &p)) || !isspace(*p))
939 return 0;
940 p += strspn(p, " \t");
942 word_end = (char *)p + strcspn(p, " \t");
943 rev = word_end + strspn(word_end, " \t");
944 *word_end = '\0'; /* NUL-terminate the word */
946 get_terms(terms);
947 if (check_and_set_terms(terms, p))
948 return -1;
950 if (!strcmp(p, "start")) {
951 struct strvec argv = STRVEC_INIT;
952 int res;
953 sq_dequote_to_strvec(rev, &argv);
954 res = bisect_start(terms, argv.v, argv.nr);
955 strvec_clear(&argv);
956 return res;
959 if (one_of(p, terms->term_good,
960 terms->term_bad, "skip", NULL))
961 return bisect_write(p, rev, terms, 0);
963 if (!strcmp(p, "terms")) {
964 struct strvec argv = STRVEC_INIT;
965 int res;
966 sq_dequote_to_strvec(rev, &argv);
967 res = bisect_terms(terms, argv.nr == 1 ? argv.v[0] : NULL);
968 strvec_clear(&argv);
969 return res;
971 error(_("'%s'?? what are you talking about?"), p);
973 return -1;
976 static enum bisect_error bisect_replay(struct bisect_terms *terms, const char *filename)
978 FILE *fp = NULL;
979 enum bisect_error res = BISECT_OK;
980 struct strbuf line = STRBUF_INIT;
982 if (is_empty_or_missing_file(filename))
983 return error(_("cannot read file '%s' for replaying"), filename);
985 if (bisect_reset(NULL))
986 return BISECT_FAILED;
988 fp = fopen(filename, "r");
989 if (!fp)
990 return BISECT_FAILED;
992 while ((strbuf_getline(&line, fp) != EOF) && !res)
993 res = process_replay_line(terms, &line);
995 strbuf_release(&line);
996 fclose(fp);
998 if (res)
999 return BISECT_FAILED;
1001 return bisect_auto_next(terms, NULL);
1004 static enum bisect_error bisect_skip(struct bisect_terms *terms, const char **argv, int argc)
1006 int i;
1007 enum bisect_error res;
1008 struct strvec argv_state = STRVEC_INIT;
1010 strvec_push(&argv_state, "skip");
1012 for (i = 0; i < argc; i++) {
1013 const char *dotdot = strstr(argv[i], "..");
1015 if (dotdot) {
1016 struct rev_info revs;
1017 struct commit *commit;
1019 init_revisions(&revs, NULL);
1020 setup_revisions(2, argv + i - 1, &revs, NULL);
1022 if (prepare_revision_walk(&revs))
1023 die(_("revision walk setup failed\n"));
1024 while ((commit = get_revision(&revs)) != NULL)
1025 strvec_push(&argv_state,
1026 oid_to_hex(&commit->object.oid));
1028 reset_revision_walk();
1029 } else {
1030 strvec_push(&argv_state, argv[i]);
1033 res = bisect_state(terms, argv_state.v, argv_state.nr);
1035 strvec_clear(&argv_state);
1036 return res;
1039 int cmd_bisect__helper(int argc, const char **argv, const char *prefix)
1041 enum {
1042 BISECT_RESET = 1,
1043 BISECT_NEXT_CHECK,
1044 BISECT_TERMS,
1045 BISECT_START,
1046 BISECT_AUTOSTART,
1047 BISECT_NEXT,
1048 BISECT_STATE,
1049 BISECT_LOG,
1050 BISECT_REPLAY,
1051 BISECT_SKIP
1052 } cmdmode = 0;
1053 int res = 0, nolog = 0;
1054 struct option options[] = {
1055 OPT_CMDMODE(0, "bisect-reset", &cmdmode,
1056 N_("reset the bisection state"), BISECT_RESET),
1057 OPT_CMDMODE(0, "bisect-next-check", &cmdmode,
1058 N_("check whether bad or good terms exist"), BISECT_NEXT_CHECK),
1059 OPT_CMDMODE(0, "bisect-terms", &cmdmode,
1060 N_("print out the bisect terms"), BISECT_TERMS),
1061 OPT_CMDMODE(0, "bisect-start", &cmdmode,
1062 N_("start the bisect session"), BISECT_START),
1063 OPT_CMDMODE(0, "bisect-next", &cmdmode,
1064 N_("find the next bisection commit"), BISECT_NEXT),
1065 OPT_CMDMODE(0, "bisect-state", &cmdmode,
1066 N_("mark the state of ref (or refs)"), BISECT_STATE),
1067 OPT_CMDMODE(0, "bisect-log", &cmdmode,
1068 N_("list the bisection steps so far"), BISECT_LOG),
1069 OPT_CMDMODE(0, "bisect-replay", &cmdmode,
1070 N_("replay the bisection process from the given file"), BISECT_REPLAY),
1071 OPT_CMDMODE(0, "bisect-skip", &cmdmode,
1072 N_("skip some commits for checkout"), BISECT_SKIP),
1073 OPT_BOOL(0, "no-log", &nolog,
1074 N_("no log for BISECT_WRITE")),
1075 OPT_END()
1077 struct bisect_terms terms = { .term_good = NULL, .term_bad = NULL };
1079 argc = parse_options(argc, argv, prefix, options,
1080 git_bisect_helper_usage,
1081 PARSE_OPT_KEEP_DASHDASH | PARSE_OPT_KEEP_UNKNOWN);
1083 if (!cmdmode)
1084 usage_with_options(git_bisect_helper_usage, options);
1086 switch (cmdmode) {
1087 case BISECT_RESET:
1088 if (argc > 1)
1089 return error(_("--bisect-reset requires either no argument or a commit"));
1090 res = bisect_reset(argc ? argv[0] : NULL);
1091 break;
1092 case BISECT_NEXT_CHECK:
1093 if (argc != 2 && argc != 3)
1094 return error(_("--bisect-next-check requires 2 or 3 arguments"));
1095 set_terms(&terms, argv[1], argv[0]);
1096 res = bisect_next_check(&terms, argc == 3 ? argv[2] : NULL);
1097 break;
1098 case BISECT_TERMS:
1099 if (argc > 1)
1100 return error(_("--bisect-terms requires 0 or 1 argument"));
1101 res = bisect_terms(&terms, argc == 1 ? argv[0] : NULL);
1102 break;
1103 case BISECT_START:
1104 set_terms(&terms, "bad", "good");
1105 res = bisect_start(&terms, argv, argc);
1106 break;
1107 case BISECT_NEXT:
1108 if (argc)
1109 return error(_("--bisect-next requires 0 arguments"));
1110 get_terms(&terms);
1111 res = bisect_next(&terms, prefix);
1112 break;
1113 case BISECT_STATE:
1114 set_terms(&terms, "bad", "good");
1115 get_terms(&terms);
1116 res = bisect_state(&terms, argv, argc);
1117 break;
1118 case BISECT_LOG:
1119 if (argc)
1120 return error(_("--bisect-log requires 0 arguments"));
1121 res = bisect_log();
1122 break;
1123 case BISECT_REPLAY:
1124 if (argc != 1)
1125 return error(_("no logfile given"));
1126 set_terms(&terms, "bad", "good");
1127 res = bisect_replay(&terms, argv[0]);
1128 break;
1129 case BISECT_SKIP:
1130 set_terms(&terms, "bad", "good");
1131 get_terms(&terms);
1132 res = bisect_skip(&terms, argv, argc);
1133 break;
1134 default:
1135 BUG("unknown subcommand %d", cmdmode);
1137 free_terms(&terms);
1140 * Handle early success
1141 * From check_merge_bases > check_good_are_ancestors_of_bad > bisect_next_all
1143 if ((res == BISECT_INTERNAL_SUCCESS_MERGE_BASE) || (res == BISECT_INTERNAL_SUCCESS_1ST_BAD_FOUND))
1144 res = BISECT_OK;
1146 return -res;