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