]> git.ipfire.org Git - thirdparty/git.git/blame - builtin/bisect--helper.c
bisect: introduce first-parent flag
[thirdparty/git.git] / builtin / bisect--helper.c
CommitLineData
1bf072e3
CC
1#include "builtin.h"
2#include "cache.h"
3#include "parse-options.h"
4#include "bisect.h"
4ba1e5c4 5#include "refs.h"
5e82c3dd
PB
6#include "dir.h"
7#include "argv-array.h"
8#include "run-command.h"
129a6cf3 9#include "prompt.h"
06f5608c 10#include "quote.h"
1bf072e3 11
ecb3f373 12static GIT_PATH_FUNC(git_path_bisect_terms, "BISECT_TERMS")
b903674b
PB
13static GIT_PATH_FUNC(git_path_bisect_expected_rev, "BISECT_EXPECTED_REV")
14static GIT_PATH_FUNC(git_path_bisect_ancestors_ok, "BISECT_ANCESTORS_OK")
5e82c3dd
PB
15static GIT_PATH_FUNC(git_path_bisect_start, "BISECT_START")
16static GIT_PATH_FUNC(git_path_bisect_head, "BISECT_HEAD")
0f30233a 17static GIT_PATH_FUNC(git_path_bisect_log, "BISECT_LOG")
06f5608c
PB
18static GIT_PATH_FUNC(git_path_head_name, "head-name")
19static GIT_PATH_FUNC(git_path_bisect_names, "BISECT_NAMES")
e8861ffc 20static GIT_PATH_FUNC(git_path_bisect_first_parent, "BISECT_FIRST_PARENT")
ecb3f373 21
1bf072e3 22static const char * const git_bisect_helper_usage[] = {
be5fe200 23 N_("git bisect--helper --next-all"),
ecb3f373 24 N_("git bisect--helper --write-terms <bad_term> <good_term>"),
fb71a329 25 N_("git bisect--helper --bisect-clean-state"),
5e82c3dd 26 N_("git bisect--helper --bisect-reset [<commit>]"),
0f30233a 27 N_("git bisect--helper --bisect-write [--no-log] <state> <revision> <good_term> <bad_term>"),
4fbdbd5b 28 N_("git bisect--helper --bisect-check-and-set-terms <command> <good_term> <bad_term>"),
129a6cf3 29 N_("git bisect--helper --bisect-next-check <good_term> <bad_term> [<term>]"),
450ebb73 30 N_("git bisect--helper --bisect-terms [--term-good | --term-old | --term-bad | --term-new]"),
06f5608c 31 N_("git bisect--helper --bisect-start [--term-{old,good}=<term> --term-{new,bad}=<term>]"
e8861ffc 32 " [--no-checkout] [--first-parent] [<bad> [<good>...]] [--] [<paths>...]"),
1bf072e3
CC
33 NULL
34};
35
0f30233a
PB
36struct bisect_terms {
37 char *term_good;
38 char *term_bad;
39};
40
41static void free_terms(struct bisect_terms *terms)
42{
43 FREE_AND_NULL(terms->term_good);
44 FREE_AND_NULL(terms->term_bad);
45}
46
47static void set_terms(struct bisect_terms *terms, const char *bad,
48 const char *good)
49{
50 free((void *)terms->term_good);
51 terms->term_good = xstrdup(good);
52 free((void *)terms->term_bad);
53 terms->term_bad = xstrdup(bad);
54}
55
16538bfd
MR
56static const char vocab_bad[] = "bad|new";
57static const char vocab_good[] = "good|old";
129a6cf3 58
4ba1e5c4
PB
59/*
60 * Check whether the string `term` belongs to the set of strings
61 * included in the variable arguments.
62 */
63LAST_ARG_MUST_BE_NULL
64static int one_of(const char *term, ...)
65{
66 int res = 0;
67 va_list matches;
68 const char *match;
69
70 va_start(matches, term);
71 while (!res && (match = va_arg(matches, const char *)))
72 res = !strcmp(term, match);
73 va_end(matches);
74
75 return res;
76}
77
78static int check_term_format(const char *term, const char *orig_term)
79{
80 int res;
81 char *new_term = xstrfmt("refs/bisect/%s", term);
82
83 res = check_refname_format(new_term, 0);
84 free(new_term);
85
86 if (res)
87 return error(_("'%s' is not a valid term"), term);
88
89 if (one_of(term, "help", "start", "skip", "next", "reset",
dbc349bb 90 "visualize", "view", "replay", "log", "run", "terms", NULL))
4ba1e5c4
PB
91 return error(_("can't use the builtin command '%s' as a term"), term);
92
93 /*
94 * In theory, nothing prevents swapping completely good and bad,
95 * but this situation could be confusing and hasn't been tested
96 * enough. Forbid it for now.
97 */
98
99 if ((strcmp(orig_term, "bad") && one_of(term, "bad", "new", NULL)) ||
100 (strcmp(orig_term, "good") && one_of(term, "good", "old", NULL)))
101 return error(_("can't change the meaning of the term '%s'"), term);
102
103 return 0;
104}
105
ecb3f373
PB
106static int write_terms(const char *bad, const char *good)
107{
108 FILE *fp = NULL;
109 int res;
110
111 if (!strcmp(bad, good))
112 return error(_("please use two different terms"));
113
114 if (check_term_format(bad, "bad") || check_term_format(good, "good"))
115 return -1;
116
117 fp = fopen(git_path_bisect_terms(), "w");
118 if (!fp)
119 return error_errno(_("could not open the file BISECT_TERMS"));
120
121 res = fprintf(fp, "%s\n%s\n", bad, good);
122 res |= fclose(fp);
123 return (res < 0) ? -1 : 0;
124}
125
b903674b
PB
126static int is_expected_rev(const char *expected_hex)
127{
128 struct strbuf actual_hex = STRBUF_INIT;
129 int res = 0;
130 if (strbuf_read_file(&actual_hex, git_path_bisect_expected_rev(), 0) >= 40) {
131 strbuf_trim(&actual_hex);
132 res = !strcmp(actual_hex.buf, expected_hex);
133 }
134 strbuf_release(&actual_hex);
135 return res;
136}
137
138static void check_expected_revs(const char **revs, int rev_nr)
139{
140 int i;
141
142 for (i = 0; i < rev_nr; i++) {
143 if (!is_expected_rev(revs[i])) {
144 unlink_or_warn(git_path_bisect_ancestors_ok());
145 unlink_or_warn(git_path_bisect_expected_rev());
146 }
147 }
148}
149
5e82c3dd
PB
150static int bisect_reset(const char *commit)
151{
152 struct strbuf branch = STRBUF_INIT;
153
154 if (!commit) {
155 if (strbuf_read_file(&branch, git_path_bisect_start(), 0) < 1) {
156 printf(_("We are not bisecting.\n"));
157 return 0;
158 }
159 strbuf_rtrim(&branch);
160 } else {
161 struct object_id oid;
162
163 if (get_oid_commit(commit, &oid))
164 return error(_("'%s' is not a valid commit"), commit);
165 strbuf_addstr(&branch, commit);
166 }
167
168 if (!file_exists(git_path_bisect_head())) {
169 struct argv_array argv = ARGV_ARRAY_INIT;
170
171 argv_array_pushl(&argv, "checkout", branch.buf, "--", NULL);
172 if (run_command_v_opt(argv.argv, RUN_GIT_CMD)) {
51a0a4ed
TT
173 error(_("could not check out original"
174 " HEAD '%s'. Try 'git bisect"
175 " reset <commit>'."), branch.buf);
5e82c3dd
PB
176 strbuf_release(&branch);
177 argv_array_clear(&argv);
51a0a4ed 178 return -1;
5e82c3dd
PB
179 }
180 argv_array_clear(&argv);
181 }
182
183 strbuf_release(&branch);
184 return bisect_clean_state();
185}
186
0f30233a
PB
187static void log_commit(FILE *fp, char *fmt, const char *state,
188 struct commit *commit)
189{
190 struct pretty_print_context pp = {0};
191 struct strbuf commit_msg = STRBUF_INIT;
192 char *label = xstrfmt(fmt, state);
193
194 format_commit_message(commit, "%s", &commit_msg, &pp);
195
196 fprintf(fp, "# %s: [%s] %s\n", label, oid_to_hex(&commit->object.oid),
197 commit_msg.buf);
198
199 strbuf_release(&commit_msg);
200 free(label);
201}
202
203static int bisect_write(const char *state, const char *rev,
204 const struct bisect_terms *terms, int nolog)
205{
206 struct strbuf tag = STRBUF_INIT;
207 struct object_id oid;
208 struct commit *commit;
209 FILE *fp = NULL;
292731c4 210 int res = 0;
0f30233a
PB
211
212 if (!strcmp(state, terms->term_bad)) {
213 strbuf_addf(&tag, "refs/bisect/%s", state);
214 } else if (one_of(state, terms->term_good, "skip", NULL)) {
215 strbuf_addf(&tag, "refs/bisect/%s-%s", state, rev);
216 } else {
292731c4 217 res = error(_("Bad bisect_write argument: %s"), state);
0f30233a
PB
218 goto finish;
219 }
220
221 if (get_oid(rev, &oid)) {
292731c4 222 res = error(_("couldn't get the oid of the rev '%s'"), rev);
0f30233a
PB
223 goto finish;
224 }
225
226 if (update_ref(NULL, tag.buf, &oid, NULL, 0,
227 UPDATE_REFS_MSG_ON_ERR)) {
292731c4 228 res = -1;
0f30233a
PB
229 goto finish;
230 }
231
232 fp = fopen(git_path_bisect_log(), "a");
233 if (!fp) {
292731c4 234 res = error_errno(_("couldn't open the file '%s'"), git_path_bisect_log());
0f30233a
PB
235 goto finish;
236 }
237
238 commit = lookup_commit_reference(the_repository, &oid);
239 log_commit(fp, "%s", state, commit);
240
241 if (!nolog)
242 fprintf(fp, "git bisect %s %s\n", state, rev);
243
244finish:
245 if (fp)
246 fclose(fp);
247 strbuf_release(&tag);
292731c4 248 return res;
0f30233a
PB
249}
250
4fbdbd5b
PB
251static int check_and_set_terms(struct bisect_terms *terms, const char *cmd)
252{
253 int has_term_file = !is_empty_or_missing_file(git_path_bisect_terms());
254
255 if (one_of(cmd, "skip", "start", "terms", NULL))
256 return 0;
257
258 if (has_term_file && strcmp(cmd, terms->term_bad) &&
259 strcmp(cmd, terms->term_good))
260 return error(_("Invalid command: you're currently in a "
261 "%s/%s bisect"), terms->term_bad,
262 terms->term_good);
263
264 if (!has_term_file) {
265 if (one_of(cmd, "bad", "good", NULL)) {
266 set_terms(terms, "bad", "good");
267 return write_terms(terms->term_bad, terms->term_good);
268 }
269 if (one_of(cmd, "new", "old", NULL)) {
270 set_terms(terms, "new", "old");
271 return write_terms(terms->term_bad, terms->term_good);
272 }
273 }
274
275 return 0;
276}
277
129a6cf3
PB
278static int mark_good(const char *refname, const struct object_id *oid,
279 int flag, void *cb_data)
280{
281 int *m_good = (int *)cb_data;
282 *m_good = 0;
283 return 1;
284}
285
7c5cea72 286static const char need_bad_and_good_revision_warning[] =
129a6cf3
PB
287 N_("You need to give me at least one %s and %s revision.\n"
288 "You can use \"git bisect %s\" and \"git bisect %s\" for that.");
289
7c5cea72 290static const char need_bisect_start_warning[] =
129a6cf3
PB
291 N_("You need to start by \"git bisect start\".\n"
292 "You then need to give me at least one %s and %s revision.\n"
293 "You can use \"git bisect %s\" and \"git bisect %s\" for that.");
294
bfacfce7
TT
295static int decide_next(const struct bisect_terms *terms,
296 const char *current_term, int missing_good,
297 int missing_bad)
129a6cf3 298{
129a6cf3 299 if (!missing_good && !missing_bad)
bfacfce7
TT
300 return 0;
301 if (!current_term)
302 return -1;
129a6cf3
PB
303
304 if (missing_good && !missing_bad &&
305 !strcmp(current_term, terms->term_good)) {
306 char *yesno;
307 /*
308 * have bad (or new) but not good (or old). We could bisect
309 * although this is less optimum.
310 */
311 warning(_("bisecting only with a %s commit"), terms->term_bad);
312 if (!isatty(0))
bfacfce7 313 return 0;
129a6cf3
PB
314 /*
315 * TRANSLATORS: Make sure to include [Y] and [n] in your
316 * translation. The program will only accept English input
317 * at this point.
318 */
319 yesno = git_prompt(_("Are you sure [Y/n]? "), PROMPT_ECHO);
320 if (starts_with(yesno, "N") || starts_with(yesno, "n"))
bfacfce7
TT
321 return -1;
322 return 0;
129a6cf3
PB
323 }
324
bfacfce7
TT
325 if (!is_empty_or_missing_file(git_path_bisect_start()))
326 return error(_(need_bad_and_good_revision_warning),
327 vocab_bad, vocab_good, vocab_bad, vocab_good);
328 else
329 return error(_(need_bisect_start_warning),
330 vocab_good, vocab_bad, vocab_good, vocab_bad);
331}
332
333static int bisect_next_check(const struct bisect_terms *terms,
334 const char *current_term)
335{
336 int missing_good = 1, missing_bad = 1;
337 char *bad_ref = xstrfmt("refs/bisect/%s", terms->term_bad);
338 char *good_glob = xstrfmt("%s-*", terms->term_good);
339
340 if (ref_exists(bad_ref))
341 missing_bad = 0;
342
343 for_each_glob_ref_in(mark_good, good_glob, "refs/bisect/",
344 (void *) &missing_good);
345
346 free(good_glob);
347 free(bad_ref);
348
349 return decide_next(terms, current_term, missing_good, missing_bad);
129a6cf3
PB
350}
351
450ebb73
PB
352static int get_terms(struct bisect_terms *terms)
353{
354 struct strbuf str = STRBUF_INIT;
355 FILE *fp = NULL;
356 int res = 0;
357
358 fp = fopen(git_path_bisect_terms(), "r");
359 if (!fp) {
360 res = -1;
361 goto finish;
362 }
363
364 free_terms(terms);
365 strbuf_getline_lf(&str, fp);
366 terms->term_bad = strbuf_detach(&str, NULL);
367 strbuf_getline_lf(&str, fp);
368 terms->term_good = strbuf_detach(&str, NULL);
369
370finish:
371 if (fp)
372 fclose(fp);
373 strbuf_release(&str);
374 return res;
375}
376
377static int bisect_terms(struct bisect_terms *terms, const char *option)
378{
379 if (get_terms(terms))
380 return error(_("no terms defined"));
381
382 if (option == NULL) {
383 printf(_("Your current terms are %s for the old state\n"
384 "and %s for the new state.\n"),
385 terms->term_good, terms->term_bad);
386 return 0;
387 }
388 if (one_of(option, "--term-good", "--term-old", NULL))
389 printf("%s\n", terms->term_good);
390 else if (one_of(option, "--term-bad", "--term-new", NULL))
391 printf("%s\n", terms->term_bad);
392 else
393 return error(_("invalid argument %s for 'git bisect terms'.\n"
394 "Supported options are: "
395 "--term-good|--term-old and "
396 "--term-bad|--term-new."), option);
397
398 return 0;
399}
400
06f5608c
PB
401static int bisect_append_log_quoted(const char **argv)
402{
292731c4 403 int res = 0;
06f5608c
PB
404 FILE *fp = fopen(git_path_bisect_log(), "a");
405 struct strbuf orig_args = STRBUF_INIT;
406
407 if (!fp)
408 return -1;
409
410 if (fprintf(fp, "git bisect start") < 1) {
292731c4 411 res = -1;
06f5608c
PB
412 goto finish;
413 }
414
415 sq_quote_argv(&orig_args, argv);
416 if (fprintf(fp, "%s\n", orig_args.buf) < 1)
292731c4 417 res = -1;
06f5608c
PB
418
419finish:
420 fclose(fp);
421 strbuf_release(&orig_args);
292731c4 422 return res;
06f5608c
PB
423}
424
be5fe200 425static int bisect_start(struct bisect_terms *terms, const char **argv, int argc)
06f5608c 426{
be5fe200 427 int no_checkout = 0;
e8861ffc 428 int first_parent_only = 0;
06f5608c 429 int i, has_double_dash = 0, must_write_terms = 0, bad_seen = 0;
292731c4 430 int flags, pathspec_pos, res = 0;
06f5608c
PB
431 struct string_list revs = STRING_LIST_INIT_DUP;
432 struct string_list states = STRING_LIST_INIT_DUP;
433 struct strbuf start_head = STRBUF_INIT;
434 struct strbuf bisect_names = STRBUF_INIT;
435 struct object_id head_oid;
436 struct object_id oid;
437 const char *head;
438
439 if (is_bare_repository())
440 no_checkout = 1;
441
442 /*
443 * Check for one bad and then some good revisions
444 */
445 for (i = 0; i < argc; i++) {
446 if (!strcmp(argv[i], "--")) {
447 has_double_dash = 1;
448 break;
449 }
450 }
451
452 for (i = 0; i < argc; i++) {
453 const char *arg = argv[i];
454 if (!strcmp(argv[i], "--")) {
455 break;
456 } else if (!strcmp(arg, "--no-checkout")) {
457 no_checkout = 1;
e8861ffc
AL
458 } else if (!strcmp(arg, "--first-parent")) {
459 first_parent_only = 1;
06f5608c
PB
460 } else if (!strcmp(arg, "--term-good") ||
461 !strcmp(arg, "--term-old")) {
4d9005ff
CMAB
462 i++;
463 if (argc <= i)
464 return error(_("'' is not a valid term"));
06f5608c
PB
465 must_write_terms = 1;
466 free((void *) terms->term_good);
4d9005ff 467 terms->term_good = xstrdup(argv[i]);
06f5608c
PB
468 } else if (skip_prefix(arg, "--term-good=", &arg) ||
469 skip_prefix(arg, "--term-old=", &arg)) {
470 must_write_terms = 1;
471 free((void *) terms->term_good);
472 terms->term_good = xstrdup(arg);
473 } else if (!strcmp(arg, "--term-bad") ||
474 !strcmp(arg, "--term-new")) {
4d9005ff
CMAB
475 i++;
476 if (argc <= i)
477 return error(_("'' is not a valid term"));
06f5608c
PB
478 must_write_terms = 1;
479 free((void *) terms->term_bad);
4d9005ff 480 terms->term_bad = xstrdup(argv[i]);
06f5608c
PB
481 } else if (skip_prefix(arg, "--term-bad=", &arg) ||
482 skip_prefix(arg, "--term-new=", &arg)) {
483 must_write_terms = 1;
484 free((void *) terms->term_bad);
485 terms->term_bad = xstrdup(arg);
4d9005ff 486 } else if (starts_with(arg, "--")) {
06f5608c
PB
487 return error(_("unrecognized option: '%s'"), arg);
488 } else {
489 char *commit_id = xstrfmt("%s^{commit}", arg);
490 if (get_oid(commit_id, &oid) && has_double_dash)
491 die(_("'%s' does not appear to be a valid "
492 "revision"), arg);
493
494 string_list_append(&revs, oid_to_hex(&oid));
495 free(commit_id);
496 }
497 }
498 pathspec_pos = i;
499
500 /*
501 * The user ran "git bisect start <sha1> <sha1>", hence did not
502 * explicitly specify the terms, but we are already starting to
503 * set references named with the default terms, and won't be able
504 * to change afterwards.
505 */
506 if (revs.nr)
507 must_write_terms = 1;
508 for (i = 0; i < revs.nr; i++) {
509 if (bad_seen) {
510 string_list_append(&states, terms->term_good);
511 } else {
512 bad_seen = 1;
513 string_list_append(&states, terms->term_bad);
514 }
515 }
516
517 /*
518 * Verify HEAD
519 */
520 head = resolve_ref_unsafe("HEAD", 0, &head_oid, &flags);
521 if (!head)
522 if (get_oid("HEAD", &head_oid))
523 return error(_("bad HEAD - I need a HEAD"));
524
525 /*
526 * Check if we are bisecting
527 */
528 if (!is_empty_or_missing_file(git_path_bisect_start())) {
529 /* Reset to the rev from where we started */
530 strbuf_read_file(&start_head, git_path_bisect_start(), 0);
531 strbuf_trim(&start_head);
532 if (!no_checkout) {
533 struct argv_array argv = ARGV_ARRAY_INIT;
534
535 argv_array_pushl(&argv, "checkout", start_head.buf,
536 "--", NULL);
537 if (run_command_v_opt(argv.argv, RUN_GIT_CMD)) {
292731c4 538 res = error(_("checking out '%s' failed."
06f5608c
PB
539 " Try 'git bisect start "
540 "<valid-branch>'."),
541 start_head.buf);
542 goto finish;
543 }
544 }
545 } else {
546 /* Get the rev from where we start. */
547 if (!get_oid(head, &head_oid) &&
548 !starts_with(head, "refs/heads/")) {
549 strbuf_reset(&start_head);
550 strbuf_addstr(&start_head, oid_to_hex(&head_oid));
551 } else if (!get_oid(head, &head_oid) &&
552 skip_prefix(head, "refs/heads/", &head)) {
553 /*
554 * This error message should only be triggered by
555 * cogito usage, and cogito users should understand
556 * it relates to cg-seek.
557 */
558 if (!is_empty_or_missing_file(git_path_head_name()))
559 return error(_("won't bisect on cg-seek'ed tree"));
560 strbuf_addstr(&start_head, head);
561 } else {
562 return error(_("bad HEAD - strange symbolic ref"));
563 }
564 }
565
566 /*
567 * Get rid of any old bisect state.
568 */
569 if (bisect_clean_state())
570 return -1;
571
572 /*
573 * In case of mistaken revs or checkout error, or signals received,
574 * "bisect_auto_next" below may exit or misbehave.
575 * We have to trap this to be able to clean up using
576 * "bisect_clean_state".
577 */
578
579 /*
580 * Write new start state
581 */
582 write_file(git_path_bisect_start(), "%s\n", start_head.buf);
583
e8861ffc
AL
584 if (first_parent_only)
585 write_file(git_path_bisect_first_parent(), "\n");
586
06f5608c 587 if (no_checkout) {
7877ac3d 588 if (get_oid(start_head.buf, &oid) < 0) {
292731c4 589 res = error(_("invalid ref: '%s'"), start_head.buf);
7877ac3d
JS
590 goto finish;
591 }
06f5608c
PB
592 if (update_ref(NULL, "BISECT_HEAD", &oid, NULL, 0,
593 UPDATE_REFS_MSG_ON_ERR)) {
292731c4 594 res = -1;
06f5608c
PB
595 goto finish;
596 }
597 }
598
599 if (pathspec_pos < argc - 1)
600 sq_quote_argv(&bisect_names, argv + pathspec_pos);
601 write_file(git_path_bisect_names(), "%s\n", bisect_names.buf);
602
603 for (i = 0; i < states.nr; i++)
604 if (bisect_write(states.items[i].string,
605 revs.items[i].string, terms, 1)) {
292731c4 606 res = -1;
06f5608c
PB
607 goto finish;
608 }
609
610 if (must_write_terms && write_terms(terms->term_bad,
611 terms->term_good)) {
292731c4 612 res = -1;
06f5608c
PB
613 goto finish;
614 }
615
292731c4
TT
616 res = bisect_append_log_quoted(argv);
617 if (res)
618 res = -1;
06f5608c
PB
619
620finish:
621 string_list_clear(&revs, 0);
622 string_list_clear(&states, 0);
623 strbuf_release(&start_head);
624 strbuf_release(&bisect_names);
292731c4 625 return res;
06f5608c
PB
626}
627
1bf072e3
CC
628int cmd_bisect__helper(int argc, const char **argv, const char *prefix)
629{
4ba1e5c4
PB
630 enum {
631 NEXT_ALL = 1,
fb71a329 632 WRITE_TERMS,
b903674b 633 BISECT_CLEAN_STATE,
5e82c3dd 634 CHECK_EXPECTED_REVS,
0f30233a 635 BISECT_RESET,
4fbdbd5b 636 BISECT_WRITE,
129a6cf3 637 CHECK_AND_SET_TERMS,
450ebb73 638 BISECT_NEXT_CHECK,
06f5608c
PB
639 BISECT_TERMS,
640 BISECT_START
4ba1e5c4 641 } cmdmode = 0;
be5fe200 642 int res = 0, nolog = 0;
1bf072e3 643 struct option options[] = {
9e1c84df
PB
644 OPT_CMDMODE(0, "next-all", &cmdmode,
645 N_("perform 'git bisect next'"), NEXT_ALL),
ecb3f373
PB
646 OPT_CMDMODE(0, "write-terms", &cmdmode,
647 N_("write the terms to .git/BISECT_TERMS"), WRITE_TERMS),
fb71a329
PB
648 OPT_CMDMODE(0, "bisect-clean-state", &cmdmode,
649 N_("cleanup the bisection state"), BISECT_CLEAN_STATE),
b903674b
PB
650 OPT_CMDMODE(0, "check-expected-revs", &cmdmode,
651 N_("check for expected revs"), CHECK_EXPECTED_REVS),
5e82c3dd
PB
652 OPT_CMDMODE(0, "bisect-reset", &cmdmode,
653 N_("reset the bisection state"), BISECT_RESET),
0f30233a
PB
654 OPT_CMDMODE(0, "bisect-write", &cmdmode,
655 N_("write out the bisection state in BISECT_LOG"), BISECT_WRITE),
4fbdbd5b
PB
656 OPT_CMDMODE(0, "check-and-set-terms", &cmdmode,
657 N_("check and set terms in a bisection state"), CHECK_AND_SET_TERMS),
129a6cf3
PB
658 OPT_CMDMODE(0, "bisect-next-check", &cmdmode,
659 N_("check whether bad or good terms exist"), BISECT_NEXT_CHECK),
450ebb73
PB
660 OPT_CMDMODE(0, "bisect-terms", &cmdmode,
661 N_("print out the bisect terms"), BISECT_TERMS),
06f5608c
PB
662 OPT_CMDMODE(0, "bisect-start", &cmdmode,
663 N_("start the bisect session"), BISECT_START),
0f30233a 664 OPT_BOOL(0, "no-log", &nolog,
32ceace3 665 N_("no log for BISECT_WRITE")),
1bf072e3
CC
666 OPT_END()
667 };
0f30233a 668 struct bisect_terms terms = { .term_good = NULL, .term_bad = NULL };
1bf072e3 669
37782920 670 argc = parse_options(argc, argv, prefix, options,
06f5608c
PB
671 git_bisect_helper_usage,
672 PARSE_OPT_KEEP_DASHDASH | PARSE_OPT_KEEP_UNKNOWN);
1bf072e3 673
9e1c84df 674 if (!cmdmode)
1bf072e3
CC
675 usage_with_options(git_bisect_helper_usage, options);
676
9e1c84df
PB
677 switch (cmdmode) {
678 case NEXT_ALL:
be5fe200 679 res = bisect_next_all(the_repository, prefix);
45b63708 680 break;
ecb3f373 681 case WRITE_TERMS:
4ba1e5c4 682 if (argc != 2)
ecb3f373
PB
683 return error(_("--write-terms requires two arguments"));
684 return write_terms(argv[0], argv[1]);
fb71a329
PB
685 case BISECT_CLEAN_STATE:
686 if (argc != 0)
687 return error(_("--bisect-clean-state requires no arguments"));
688 return bisect_clean_state();
b903674b
PB
689 case CHECK_EXPECTED_REVS:
690 check_expected_revs(argv, argc);
691 return 0;
5e82c3dd
PB
692 case BISECT_RESET:
693 if (argc > 1)
694 return error(_("--bisect-reset requires either no argument or a commit"));
695 return !!bisect_reset(argc ? argv[0] : NULL);
0f30233a
PB
696 case BISECT_WRITE:
697 if (argc != 4 && argc != 5)
698 return error(_("--bisect-write requires either 4 or 5 arguments"));
699 set_terms(&terms, argv[3], argv[2]);
700 res = bisect_write(argv[0], argv[1], &terms, nolog);
701 break;
4fbdbd5b
PB
702 case CHECK_AND_SET_TERMS:
703 if (argc != 3)
704 return error(_("--check-and-set-terms requires 3 arguments"));
705 set_terms(&terms, argv[2], argv[1]);
706 res = check_and_set_terms(&terms, argv[0]);
707 break;
129a6cf3
PB
708 case BISECT_NEXT_CHECK:
709 if (argc != 2 && argc != 3)
710 return error(_("--bisect-next-check requires 2 or 3 arguments"));
711 set_terms(&terms, argv[1], argv[0]);
712 res = bisect_next_check(&terms, argc == 3 ? argv[2] : NULL);
713 break;
450ebb73
PB
714 case BISECT_TERMS:
715 if (argc > 1)
716 return error(_("--bisect-terms requires 0 or 1 argument"));
717 res = bisect_terms(&terms, argc == 1 ? argv[0] : NULL);
718 break;
06f5608c
PB
719 case BISECT_START:
720 set_terms(&terms, "bad", "good");
be5fe200 721 res = bisect_start(&terms, argv, argc);
06f5608c 722 break;
9e1c84df
PB
723 default:
724 return error("BUG: unknown subcommand '%d'", cmdmode);
725 }
0f30233a 726 free_terms(&terms);
45b63708
PB
727
728 /*
729 * Handle early success
730 * From check_merge_bases > check_good_are_ancestors_of_bad > bisect_next_all
731 */
732 if (res == BISECT_INTERNAL_SUCCESS_MERGE_BASE)
733 res = BISECT_OK;
734
7613ec59 735 return abs(res);
1bf072e3 736}