git

Форк
0
/
bisect.c 
1454 строки · 36.7 Кб
1
#include "builtin.h"
2
#include "copy.h"
3
#include "environment.h"
4
#include "gettext.h"
5
#include "hex.h"
6
#include "object-name.h"
7
#include "parse-options.h"
8
#include "bisect.h"
9
#include "refs.h"
10
#include "strvec.h"
11
#include "run-command.h"
12
#include "oid-array.h"
13
#include "path.h"
14
#include "prompt.h"
15
#include "quote.h"
16
#include "revision.h"
17

18
static GIT_PATH_FUNC(git_path_bisect_terms, "BISECT_TERMS")
19
static GIT_PATH_FUNC(git_path_bisect_ancestors_ok, "BISECT_ANCESTORS_OK")
20
static GIT_PATH_FUNC(git_path_bisect_start, "BISECT_START")
21
static GIT_PATH_FUNC(git_path_bisect_log, "BISECT_LOG")
22
static GIT_PATH_FUNC(git_path_bisect_names, "BISECT_NAMES")
23
static GIT_PATH_FUNC(git_path_bisect_first_parent, "BISECT_FIRST_PARENT")
24
static GIT_PATH_FUNC(git_path_bisect_run, "BISECT_RUN")
25

26
#define BUILTIN_GIT_BISECT_START_USAGE \
27
	N_("git bisect start [--term-(new|bad)=<term> --term-(old|good)=<term>]" \
28
	   "    [--no-checkout] [--first-parent] [<bad> [<good>...]] [--]" \
29
	   "    [<pathspec>...]")
30
#define BUILTIN_GIT_BISECT_STATE_USAGE \
31
	N_("git bisect (good|bad) [<rev>...]")
32
#define BUILTIN_GIT_BISECT_TERMS_USAGE \
33
	"git bisect terms [--term-good | --term-bad]"
34
#define BUILTIN_GIT_BISECT_SKIP_USAGE \
35
	N_("git bisect skip [(<rev>|<range>)...]")
36
#define BUILTIN_GIT_BISECT_NEXT_USAGE \
37
	"git bisect next"
38
#define BUILTIN_GIT_BISECT_RESET_USAGE \
39
	N_("git bisect reset [<commit>]")
40
#define BUILTIN_GIT_BISECT_VISUALIZE_USAGE \
41
	"git bisect visualize"
42
#define BUILTIN_GIT_BISECT_REPLAY_USAGE \
43
	N_("git bisect replay <logfile>")
44
#define BUILTIN_GIT_BISECT_LOG_USAGE \
45
	"git bisect log"
46
#define BUILTIN_GIT_BISECT_RUN_USAGE \
47
	N_("git bisect run <cmd> [<arg>...]")
48

49
static const char * const git_bisect_usage[] = {
50
	BUILTIN_GIT_BISECT_START_USAGE,
51
	BUILTIN_GIT_BISECT_STATE_USAGE,
52
	BUILTIN_GIT_BISECT_TERMS_USAGE,
53
	BUILTIN_GIT_BISECT_SKIP_USAGE,
54
	BUILTIN_GIT_BISECT_NEXT_USAGE,
55
	BUILTIN_GIT_BISECT_RESET_USAGE,
56
	BUILTIN_GIT_BISECT_VISUALIZE_USAGE,
57
	BUILTIN_GIT_BISECT_REPLAY_USAGE,
58
	BUILTIN_GIT_BISECT_LOG_USAGE,
59
	BUILTIN_GIT_BISECT_RUN_USAGE,
60
	NULL
61
};
62

63
struct add_bisect_ref_data {
64
	struct rev_info *revs;
65
	unsigned int object_flags;
66
};
67

68
struct bisect_terms {
69
	char *term_good;
70
	char *term_bad;
71
};
72

73
static void free_terms(struct bisect_terms *terms)
74
{
75
	FREE_AND_NULL(terms->term_good);
76
	FREE_AND_NULL(terms->term_bad);
77
}
78

79
static void set_terms(struct bisect_terms *terms, const char *bad,
80
		      const char *good)
81
{
82
	free((void *)terms->term_good);
83
	terms->term_good = xstrdup(good);
84
	free((void *)terms->term_bad);
85
	terms->term_bad = xstrdup(bad);
86
}
87

88
static const char vocab_bad[] = "bad|new";
89
static const char vocab_good[] = "good|old";
90

91
static int bisect_autostart(struct bisect_terms *terms);
92

93
/*
94
 * Check whether the string `term` belongs to the set of strings
95
 * included in the variable arguments.
96
 */
97
LAST_ARG_MUST_BE_NULL
98
static int one_of(const char *term, ...)
99
{
100
	int res = 0;
101
	va_list matches;
102
	const char *match;
103

104
	va_start(matches, term);
105
	while (!res && (match = va_arg(matches, const char *)))
106
		res = !strcmp(term, match);
107
	va_end(matches);
108

109
	return res;
110
}
111

112
/*
113
 * return code BISECT_INTERNAL_SUCCESS_MERGE_BASE
114
 * and BISECT_INTERNAL_SUCCESS_1ST_BAD_FOUND are codes
115
 * that indicate special success.
116
 */
117

118
static int is_bisect_success(enum bisect_error res)
119
{
120
	return !res ||
121
		res == BISECT_INTERNAL_SUCCESS_1ST_BAD_FOUND ||
122
		res == BISECT_INTERNAL_SUCCESS_MERGE_BASE;
123
}
124

125
static int write_in_file(const char *path, const char *mode, const char *format, va_list args)
126
{
127
	FILE *fp = NULL;
128
	int res = 0;
129

130
	if (strcmp(mode, "w") && strcmp(mode, "a"))
131
		BUG("write-in-file does not support '%s' mode", mode);
132
	fp = fopen(path, mode);
133
	if (!fp)
134
		return error_errno(_("cannot open file '%s' in mode '%s'"), path, mode);
135
	res = vfprintf(fp, format, args);
136

137
	if (res < 0) {
138
		int saved_errno = errno;
139
		fclose(fp);
140
		errno = saved_errno;
141
		return error_errno(_("could not write to file '%s'"), path);
142
	}
143

144
	return fclose(fp);
145
}
146

147
__attribute__((format (printf, 2, 3)))
148
static int write_to_file(const char *path, const char *format, ...)
149
{
150
	int res;
151
	va_list args;
152

153
	va_start(args, format);
154
	res = write_in_file(path, "w", format, args);
155
	va_end(args);
156

157
	return res;
158
}
159

160
__attribute__((format (printf, 2, 3)))
161
static int append_to_file(const char *path, const char *format, ...)
162
{
163
	int res;
164
	va_list args;
165

166
	va_start(args, format);
167
	res = write_in_file(path, "a", format, args);
168
	va_end(args);
169

170
	return res;
171
}
172

173
static int print_file_to_stdout(const char *path)
174
{
175
	int fd = open(path, O_RDONLY);
176
	int ret = 0;
177

178
	if (fd < 0)
179
		return error_errno(_("cannot open file '%s' for reading"), path);
180
	if (copy_fd(fd, 1) < 0)
181
		ret = error_errno(_("failed to read '%s'"), path);
182
	close(fd);
183
	return ret;
184
}
185

186
static int check_term_format(const char *term, const char *orig_term)
187
{
188
	int res;
189
	char *new_term = xstrfmt("refs/bisect/%s", term);
190

191
	res = check_refname_format(new_term, 0);
192
	free(new_term);
193

194
	if (res)
195
		return error(_("'%s' is not a valid term"), term);
196

197
	if (one_of(term, "help", "start", "skip", "next", "reset",
198
			"visualize", "view", "replay", "log", "run", "terms", NULL))
199
		return error(_("can't use the builtin command '%s' as a term"), term);
200

201
	/*
202
	 * In theory, nothing prevents swapping completely good and bad,
203
	 * but this situation could be confusing and hasn't been tested
204
	 * enough. Forbid it for now.
205
	 */
206

207
	if ((strcmp(orig_term, "bad") && one_of(term, "bad", "new", NULL)) ||
208
		 (strcmp(orig_term, "good") && one_of(term, "good", "old", NULL)))
209
		return error(_("can't change the meaning of the term '%s'"), term);
210

211
	return 0;
212
}
213

214
static int write_terms(const char *bad, const char *good)
215
{
216
	int res;
217

218
	if (!strcmp(bad, good))
219
		return error(_("please use two different terms"));
220

221
	if (check_term_format(bad, "bad") || check_term_format(good, "good"))
222
		return -1;
223

224
	res = write_to_file(git_path_bisect_terms(), "%s\n%s\n", bad, good);
225

226
	return res;
227
}
228

229
static int bisect_reset(const char *commit)
230
{
231
	struct strbuf branch = STRBUF_INIT;
232

233
	if (!commit) {
234
		if (!strbuf_read_file(&branch, git_path_bisect_start(), 0))
235
			printf(_("We are not bisecting.\n"));
236
		else
237
			strbuf_rtrim(&branch);
238
	} else {
239
		struct object_id oid;
240

241
		if (repo_get_oid_commit(the_repository, commit, &oid))
242
			return error(_("'%s' is not a valid commit"), commit);
243
		strbuf_addstr(&branch, commit);
244
	}
245

246
	if (branch.len && !refs_ref_exists(get_main_ref_store(the_repository), "BISECT_HEAD")) {
247
		struct child_process cmd = CHILD_PROCESS_INIT;
248

249
		cmd.git_cmd = 1;
250
		strvec_pushl(&cmd.args, "checkout", "--ignore-other-worktrees",
251
				branch.buf, "--", NULL);
252
		if (run_command(&cmd)) {
253
			error(_("could not check out original"
254
				" HEAD '%s'. Try 'git bisect"
255
				" reset <commit>'."), branch.buf);
256
			strbuf_release(&branch);
257
			return -1;
258
		}
259
	}
260

261
	strbuf_release(&branch);
262
	return bisect_clean_state();
263
}
264

265
static void log_commit(FILE *fp,
266
		       const char *fmt, const char *state,
267
		       struct commit *commit)
268
{
269
	struct pretty_print_context pp = {0};
270
	struct strbuf commit_msg = STRBUF_INIT;
271
	char *label = xstrfmt(fmt, state);
272

273
	repo_format_commit_message(the_repository, commit, "%s", &commit_msg,
274
				   &pp);
275

276
	fprintf(fp, "# %s: [%s] %s\n", label, oid_to_hex(&commit->object.oid),
277
		commit_msg.buf);
278

279
	strbuf_release(&commit_msg);
280
	free(label);
281
}
282

283
static int bisect_write(const char *state, const char *rev,
284
			const struct bisect_terms *terms, int nolog)
285
{
286
	struct strbuf tag = STRBUF_INIT;
287
	struct object_id oid;
288
	struct commit *commit;
289
	FILE *fp = NULL;
290
	int res = 0;
291

292
	if (!strcmp(state, terms->term_bad)) {
293
		strbuf_addf(&tag, "refs/bisect/%s", state);
294
	} else if (one_of(state, terms->term_good, "skip", NULL)) {
295
		strbuf_addf(&tag, "refs/bisect/%s-%s", state, rev);
296
	} else {
297
		res = error(_("Bad bisect_write argument: %s"), state);
298
		goto finish;
299
	}
300

301
	if (repo_get_oid(the_repository, rev, &oid)) {
302
		res = error(_("couldn't get the oid of the rev '%s'"), rev);
303
		goto finish;
304
	}
305

306
	if (refs_update_ref(get_main_ref_store(the_repository), NULL, tag.buf, &oid, NULL, 0,
307
			    UPDATE_REFS_MSG_ON_ERR)) {
308
		res = -1;
309
		goto finish;
310
	}
311

312
	fp = fopen(git_path_bisect_log(), "a");
313
	if (!fp) {
314
		res = error_errno(_("couldn't open the file '%s'"), git_path_bisect_log());
315
		goto finish;
316
	}
317

318
	commit = lookup_commit_reference(the_repository, &oid);
319
	log_commit(fp, "%s", state, commit);
320

321
	if (!nolog)
322
		fprintf(fp, "git bisect %s %s\n", state, rev);
323

324
finish:
325
	if (fp)
326
		fclose(fp);
327
	strbuf_release(&tag);
328
	return res;
329
}
330

331
static int check_and_set_terms(struct bisect_terms *terms, const char *cmd)
332
{
333
	int has_term_file = !is_empty_or_missing_file(git_path_bisect_terms());
334

335
	if (one_of(cmd, "skip", "start", "terms", NULL))
336
		return 0;
337

338
	if (has_term_file && strcmp(cmd, terms->term_bad) &&
339
	    strcmp(cmd, terms->term_good))
340
		return error(_("Invalid command: you're currently in a "
341
				"%s/%s bisect"), terms->term_bad,
342
				terms->term_good);
343

344
	if (!has_term_file) {
345
		if (one_of(cmd, "bad", "good", NULL)) {
346
			set_terms(terms, "bad", "good");
347
			return write_terms(terms->term_bad, terms->term_good);
348
		}
349
		if (one_of(cmd, "new", "old", NULL)) {
350
			set_terms(terms, "new", "old");
351
			return write_terms(terms->term_bad, terms->term_good);
352
		}
353
	}
354

355
	return 0;
356
}
357

358
static int inc_nr(const char *refname UNUSED,
359
		  const char *referent UNUSED,
360
		  const struct object_id *oid UNUSED,
361
		  int flag UNUSED, void *cb_data)
362
{
363
	unsigned int *nr = (unsigned int *)cb_data;
364
	(*nr)++;
365
	return 0;
366
}
367

368
static const char need_bad_and_good_revision_warning[] =
369
	N_("You need to give me at least one %s and %s revision.\n"
370
	   "You can use \"git bisect %s\" and \"git bisect %s\" for that.");
371

372
static const char need_bisect_start_warning[] =
373
	N_("You need to start by \"git bisect start\".\n"
374
	   "You then need to give me at least one %s and %s revision.\n"
375
	   "You can use \"git bisect %s\" and \"git bisect %s\" for that.");
376

377
static int decide_next(const struct bisect_terms *terms,
378
		       const char *current_term, int missing_good,
379
		       int missing_bad)
380
{
381
	if (!missing_good && !missing_bad)
382
		return 0;
383
	if (!current_term)
384
		return -1;
385

386
	if (missing_good && !missing_bad &&
387
	    !strcmp(current_term, terms->term_good)) {
388
		char *yesno;
389
		/*
390
		 * have bad (or new) but not good (or old). We could bisect
391
		 * although this is less optimum.
392
		 */
393
		warning(_("bisecting only with a %s commit"), terms->term_bad);
394
		if (!isatty(0))
395
			return 0;
396
		/*
397
		 * TRANSLATORS: Make sure to include [Y] and [n] in your
398
		 * translation. The program will only accept English input
399
		 * at this point.
400
		 */
401
		yesno = git_prompt(_("Are you sure [Y/n]? "), PROMPT_ECHO);
402
		if (starts_with(yesno, "N") || starts_with(yesno, "n"))
403
			return -1;
404
		return 0;
405
	}
406

407
	if (!is_empty_or_missing_file(git_path_bisect_start()))
408
		return error(_(need_bad_and_good_revision_warning),
409
			     vocab_bad, vocab_good, vocab_bad, vocab_good);
410
	else
411
		return error(_(need_bisect_start_warning),
412
			     vocab_good, vocab_bad, vocab_good, vocab_bad);
413
}
414

415
static void bisect_status(struct bisect_state *state,
416
			  const struct bisect_terms *terms)
417
{
418
	char *bad_ref = xstrfmt("refs/bisect/%s", terms->term_bad);
419
	char *good_glob = xstrfmt("%s-*", terms->term_good);
420

421
	if (refs_ref_exists(get_main_ref_store(the_repository), bad_ref))
422
		state->nr_bad = 1;
423

424
	refs_for_each_glob_ref_in(get_main_ref_store(the_repository), inc_nr,
425
				  good_glob, "refs/bisect/",
426
				  (void *) &state->nr_good);
427

428
	free(good_glob);
429
	free(bad_ref);
430
}
431

432
__attribute__((format (printf, 1, 2)))
433
static void bisect_log_printf(const char *fmt, ...)
434
{
435
	struct strbuf buf = STRBUF_INIT;
436
	va_list ap;
437

438
	va_start(ap, fmt);
439
	strbuf_vaddf(&buf, fmt, ap);
440
	va_end(ap);
441

442
	printf("%s", buf.buf);
443
	append_to_file(git_path_bisect_log(), "# %s", buf.buf);
444

445
	strbuf_release(&buf);
446
}
447

448
static void bisect_print_status(const struct bisect_terms *terms)
449
{
450
	struct bisect_state state = { 0 };
451

452
	bisect_status(&state, terms);
453

454
	/* If we had both, we'd already be started, and shouldn't get here. */
455
	if (state.nr_good && state.nr_bad)
456
		return;
457

458
	if (!state.nr_good && !state.nr_bad)
459
		bisect_log_printf(_("status: waiting for both good and bad commits\n"));
460
	else if (state.nr_good)
461
		bisect_log_printf(Q_("status: waiting for bad commit, %d good commit known\n",
462
				     "status: waiting for bad commit, %d good commits known\n",
463
				     state.nr_good), state.nr_good);
464
	else
465
		bisect_log_printf(_("status: waiting for good commit(s), bad commit known\n"));
466
}
467

468
static int bisect_next_check(const struct bisect_terms *terms,
469
			     const char *current_term)
470
{
471
	struct bisect_state state = { 0 };
472
	bisect_status(&state, terms);
473
	return decide_next(terms, current_term, !state.nr_good, !state.nr_bad);
474
}
475

476
static int get_terms(struct bisect_terms *terms)
477
{
478
	struct strbuf str = STRBUF_INIT;
479
	FILE *fp = NULL;
480
	int res = 0;
481

482
	fp = fopen(git_path_bisect_terms(), "r");
483
	if (!fp) {
484
		res = -1;
485
		goto finish;
486
	}
487

488
	free_terms(terms);
489
	strbuf_getline_lf(&str, fp);
490
	terms->term_bad = strbuf_detach(&str, NULL);
491
	strbuf_getline_lf(&str, fp);
492
	terms->term_good = strbuf_detach(&str, NULL);
493

494
finish:
495
	if (fp)
496
		fclose(fp);
497
	strbuf_release(&str);
498
	return res;
499
}
500

501
static int bisect_terms(struct bisect_terms *terms, const char *option)
502
{
503
	if (get_terms(terms))
504
		return error(_("no terms defined"));
505

506
	if (!option) {
507
		printf(_("Your current terms are %s for the old state\n"
508
			 "and %s for the new state.\n"),
509
		       terms->term_good, terms->term_bad);
510
		return 0;
511
	}
512
	if (one_of(option, "--term-good", "--term-old", NULL))
513
		printf("%s\n", terms->term_good);
514
	else if (one_of(option, "--term-bad", "--term-new", NULL))
515
		printf("%s\n", terms->term_bad);
516
	else
517
		return error(_("invalid argument %s for 'git bisect terms'.\n"
518
			       "Supported options are: "
519
			       "--term-good|--term-old and "
520
			       "--term-bad|--term-new."), option);
521

522
	return 0;
523
}
524

525
static int bisect_append_log_quoted(const char **argv)
526
{
527
	int res = 0;
528
	FILE *fp = fopen(git_path_bisect_log(), "a");
529
	struct strbuf orig_args = STRBUF_INIT;
530

531
	if (!fp)
532
		return -1;
533

534
	if (fprintf(fp, "git bisect start") < 1) {
535
		res = -1;
536
		goto finish;
537
	}
538

539
	sq_quote_argv(&orig_args, argv);
540
	if (fprintf(fp, "%s\n", orig_args.buf) < 1)
541
		res = -1;
542

543
finish:
544
	fclose(fp);
545
	strbuf_release(&orig_args);
546
	return res;
547
}
548

549
static int add_bisect_ref(const char *refname, const char *referent UNUSED, const struct object_id *oid,
550
			  int flags UNUSED, void *cb)
551
{
552
	struct add_bisect_ref_data *data = cb;
553

554
	add_pending_oid(data->revs, refname, oid, data->object_flags);
555

556
	return 0;
557
}
558

559
static int prepare_revs(struct bisect_terms *terms, struct rev_info *revs)
560
{
561
	int res = 0;
562
	struct add_bisect_ref_data cb = { revs };
563
	char *good = xstrfmt("%s-*", terms->term_good);
564

565
	/*
566
	 * We cannot use terms->term_bad directly in
567
	 * for_each_glob_ref_in() and we have to append a '*' to it,
568
	 * otherwise for_each_glob_ref_in() will append '/' and '*'.
569
	 */
570
	char *bad = xstrfmt("%s*", terms->term_bad);
571

572
	/*
573
	 * It is important to reset the flags used by revision walks
574
	 * as the previous call to bisect_next_all() in turn
575
	 * sets up a revision walk.
576
	 */
577
	reset_revision_walk();
578
	repo_init_revisions(the_repository, revs, NULL);
579
	setup_revisions(0, NULL, revs, NULL);
580
	refs_for_each_glob_ref_in(get_main_ref_store(the_repository),
581
				  add_bisect_ref, bad, "refs/bisect/", &cb);
582
	cb.object_flags = UNINTERESTING;
583
	refs_for_each_glob_ref_in(get_main_ref_store(the_repository),
584
				  add_bisect_ref, good, "refs/bisect/", &cb);
585
	if (prepare_revision_walk(revs))
586
		res = error(_("revision walk setup failed\n"));
587

588
	free(good);
589
	free(bad);
590
	return res;
591
}
592

593
static int bisect_skipped_commits(struct bisect_terms *terms)
594
{
595
	int res;
596
	FILE *fp = NULL;
597
	struct rev_info revs;
598
	struct commit *commit;
599
	struct pretty_print_context pp = {0};
600
	struct strbuf commit_name = STRBUF_INIT;
601

602
	res = prepare_revs(terms, &revs);
603
	if (res)
604
		return res;
605

606
	fp = fopen(git_path_bisect_log(), "a");
607
	if (!fp)
608
		return error_errno(_("could not open '%s' for appending"),
609
				  git_path_bisect_log());
610

611
	if (fprintf(fp, "# only skipped commits left to test\n") < 0)
612
		return error_errno(_("failed to write to '%s'"), git_path_bisect_log());
613

614
	while ((commit = get_revision(&revs)) != NULL) {
615
		strbuf_reset(&commit_name);
616
		repo_format_commit_message(the_repository, commit, "%s",
617
					   &commit_name, &pp);
618
		fprintf(fp, "# possible first %s commit: [%s] %s\n",
619
			terms->term_bad, oid_to_hex(&commit->object.oid),
620
			commit_name.buf);
621
	}
622

623
	/*
624
	 * Reset the flags used by revision walks in case
625
	 * there is another revision walk after this one.
626
	 */
627
	reset_revision_walk();
628

629
	strbuf_release(&commit_name);
630
	release_revisions(&revs);
631
	fclose(fp);
632
	return 0;
633
}
634

635
static int bisect_successful(struct bisect_terms *terms)
636
{
637
	struct object_id oid;
638
	struct commit *commit;
639
	struct pretty_print_context pp = {0};
640
	struct strbuf commit_name = STRBUF_INIT;
641
	char *bad_ref = xstrfmt("refs/bisect/%s",terms->term_bad);
642
	int res;
643

644
	refs_read_ref(get_main_ref_store(the_repository), bad_ref, &oid);
645
	commit = lookup_commit_reference_by_name(bad_ref);
646
	repo_format_commit_message(the_repository, commit, "%s", &commit_name,
647
				   &pp);
648

649
	res = append_to_file(git_path_bisect_log(), "# first %s commit: [%s] %s\n",
650
			    terms->term_bad, oid_to_hex(&commit->object.oid),
651
			    commit_name.buf);
652

653
	strbuf_release(&commit_name);
654
	free(bad_ref);
655
	return res;
656
}
657

658
static enum bisect_error bisect_next(struct bisect_terms *terms, const char *prefix)
659
{
660
	enum bisect_error res;
661

662
	if (bisect_autostart(terms))
663
		return BISECT_FAILED;
664

665
	if (bisect_next_check(terms, terms->term_good))
666
		return BISECT_FAILED;
667

668
	/* Perform all bisection computation */
669
	res = bisect_next_all(the_repository, prefix);
670

671
	if (res == BISECT_INTERNAL_SUCCESS_1ST_BAD_FOUND) {
672
		res = bisect_successful(terms);
673
		return res ? res : BISECT_INTERNAL_SUCCESS_1ST_BAD_FOUND;
674
	} else if (res == BISECT_ONLY_SKIPPED_LEFT) {
675
		res = bisect_skipped_commits(terms);
676
		return res ? res : BISECT_ONLY_SKIPPED_LEFT;
677
	}
678
	return res;
679
}
680

681
static enum bisect_error bisect_auto_next(struct bisect_terms *terms, const char *prefix)
682
{
683
	if (bisect_next_check(terms, NULL)) {
684
		bisect_print_status(terms);
685
		return BISECT_OK;
686
	}
687

688
	return bisect_next(terms, prefix);
689
}
690

691
static enum bisect_error bisect_start(struct bisect_terms *terms, int argc,
692
				      const char **argv)
693
{
694
	int no_checkout = 0;
695
	int first_parent_only = 0;
696
	int i, has_double_dash = 0, must_write_terms = 0, bad_seen = 0;
697
	int flags, pathspec_pos;
698
	enum bisect_error res = BISECT_OK;
699
	struct string_list revs = STRING_LIST_INIT_DUP;
700
	struct string_list states = STRING_LIST_INIT_DUP;
701
	struct strbuf start_head = STRBUF_INIT;
702
	struct strbuf bisect_names = STRBUF_INIT;
703
	struct object_id head_oid;
704
	struct object_id oid;
705
	const char *head;
706

707
	if (is_bare_repository())
708
		no_checkout = 1;
709

710
	/*
711
	 * Check for one bad and then some good revisions
712
	 */
713
	for (i = 0; i < argc; i++) {
714
		if (!strcmp(argv[i], "--")) {
715
			has_double_dash = 1;
716
			break;
717
		}
718
	}
719

720
	for (i = 0; i < argc; i++) {
721
		const char *arg = argv[i];
722
		if (!strcmp(argv[i], "--")) {
723
			break;
724
		} else if (!strcmp(arg, "--no-checkout")) {
725
			no_checkout = 1;
726
		} else if (!strcmp(arg, "--first-parent")) {
727
			first_parent_only = 1;
728
		} else if (!strcmp(arg, "--term-good") ||
729
			 !strcmp(arg, "--term-old")) {
730
			i++;
731
			if (argc <= i)
732
				return error(_("'' is not a valid term"));
733
			must_write_terms = 1;
734
			free((void *) terms->term_good);
735
			terms->term_good = xstrdup(argv[i]);
736
		} else if (skip_prefix(arg, "--term-good=", &arg) ||
737
			   skip_prefix(arg, "--term-old=", &arg)) {
738
			must_write_terms = 1;
739
			free((void *) terms->term_good);
740
			terms->term_good = xstrdup(arg);
741
		} else if (!strcmp(arg, "--term-bad") ||
742
			 !strcmp(arg, "--term-new")) {
743
			i++;
744
			if (argc <= i)
745
				return error(_("'' is not a valid term"));
746
			must_write_terms = 1;
747
			free((void *) terms->term_bad);
748
			terms->term_bad = xstrdup(argv[i]);
749
		} else if (skip_prefix(arg, "--term-bad=", &arg) ||
750
			   skip_prefix(arg, "--term-new=", &arg)) {
751
			must_write_terms = 1;
752
			free((void *) terms->term_bad);
753
			terms->term_bad = xstrdup(arg);
754
		} else if (starts_with(arg, "--")) {
755
			return error(_("unrecognized option: '%s'"), arg);
756
		} else if (!get_oidf(&oid, "%s^{commit}", arg)) {
757
			string_list_append(&revs, oid_to_hex(&oid));
758
		} else if (has_double_dash) {
759
			die(_("'%s' does not appear to be a valid "
760
			      "revision"), arg);
761
		} else {
762
			break;
763
		}
764
	}
765
	pathspec_pos = i;
766

767
	/*
768
	 * The user ran "git bisect start <sha1> <sha1>", hence did not
769
	 * explicitly specify the terms, but we are already starting to
770
	 * set references named with the default terms, and won't be able
771
	 * to change afterwards.
772
	 */
773
	if (revs.nr)
774
		must_write_terms = 1;
775
	for (i = 0; i < revs.nr; i++) {
776
		if (bad_seen) {
777
			string_list_append(&states, terms->term_good);
778
		} else {
779
			bad_seen = 1;
780
			string_list_append(&states, terms->term_bad);
781
		}
782
	}
783

784
	/*
785
	 * Verify HEAD
786
	 */
787
	head = refs_resolve_ref_unsafe(get_main_ref_store(the_repository),
788
				       "HEAD", 0, &head_oid, &flags);
789
	if (!head)
790
		if (repo_get_oid(the_repository, "HEAD", &head_oid))
791
			return error(_("bad HEAD - I need a HEAD"));
792

793
	/*
794
	 * Check if we are bisecting
795
	 */
796
	if (!is_empty_or_missing_file(git_path_bisect_start())) {
797
		/* Reset to the rev from where we started */
798
		strbuf_read_file(&start_head, git_path_bisect_start(), 0);
799
		strbuf_trim(&start_head);
800
		if (!no_checkout) {
801
			struct child_process cmd = CHILD_PROCESS_INIT;
802

803
			cmd.git_cmd = 1;
804
			strvec_pushl(&cmd.args, "checkout", start_head.buf,
805
				     "--", NULL);
806
			if (run_command(&cmd)) {
807
				res = error(_("checking out '%s' failed."
808
						 " Try 'git bisect start "
809
						 "<valid-branch>'."),
810
					       start_head.buf);
811
				goto finish;
812
			}
813
		}
814
	} else {
815
		/* Get the rev from where we start. */
816
		if (!repo_get_oid(the_repository, head, &head_oid) &&
817
		    !starts_with(head, "refs/heads/")) {
818
			strbuf_reset(&start_head);
819
			strbuf_addstr(&start_head, oid_to_hex(&head_oid));
820
		} else if (!repo_get_oid(the_repository, head, &head_oid) &&
821
			   skip_prefix(head, "refs/heads/", &head)) {
822
			strbuf_addstr(&start_head, head);
823
		} else {
824
			return error(_("bad HEAD - strange symbolic ref"));
825
		}
826
	}
827

828
	/*
829
	 * Get rid of any old bisect state.
830
	 */
831
	if (bisect_clean_state())
832
		return BISECT_FAILED;
833

834
	/*
835
	 * Write new start state
836
	 */
837
	write_file(git_path_bisect_start(), "%s\n", start_head.buf);
838

839
	if (first_parent_only)
840
		write_file(git_path_bisect_first_parent(), "\n");
841

842
	if (no_checkout) {
843
		if (repo_get_oid(the_repository, start_head.buf, &oid) < 0) {
844
			res = error(_("invalid ref: '%s'"), start_head.buf);
845
			goto finish;
846
		}
847
		if (refs_update_ref(get_main_ref_store(the_repository), NULL, "BISECT_HEAD", &oid, NULL, 0,
848
				    UPDATE_REFS_MSG_ON_ERR)) {
849
			res = BISECT_FAILED;
850
			goto finish;
851
		}
852
	}
853

854
	if (pathspec_pos < argc - 1)
855
		sq_quote_argv(&bisect_names, argv + pathspec_pos);
856
	write_file(git_path_bisect_names(), "%s\n", bisect_names.buf);
857

858
	for (i = 0; i < states.nr; i++)
859
		if (bisect_write(states.items[i].string,
860
				 revs.items[i].string, terms, 1)) {
861
			res = BISECT_FAILED;
862
			goto finish;
863
		}
864

865
	if (must_write_terms && write_terms(terms->term_bad,
866
					    terms->term_good)) {
867
		res = BISECT_FAILED;
868
		goto finish;
869
	}
870

871
	res = bisect_append_log_quoted(argv);
872
	if (res)
873
		res = BISECT_FAILED;
874

875
finish:
876
	string_list_clear(&revs, 0);
877
	string_list_clear(&states, 0);
878
	strbuf_release(&start_head);
879
	strbuf_release(&bisect_names);
880
	if (res)
881
		return res;
882

883
	res = bisect_auto_next(terms, NULL);
884
	if (!is_bisect_success(res))
885
		bisect_clean_state();
886
	return res;
887
}
888

889
static inline int file_is_not_empty(const char *path)
890
{
891
	return !is_empty_or_missing_file(path);
892
}
893

894
static int bisect_autostart(struct bisect_terms *terms)
895
{
896
	int res;
897
	const char *yesno;
898

899
	if (file_is_not_empty(git_path_bisect_start()))
900
		return 0;
901

902
	fprintf_ln(stderr, _("You need to start by \"git bisect "
903
			  "start\"\n"));
904

905
	if (!isatty(STDIN_FILENO))
906
		return -1;
907

908
	/*
909
	 * TRANSLATORS: Make sure to include [Y] and [n] in your
910
	 * translation. The program will only accept English input
911
	 * at this point.
912
	 */
913
	yesno = git_prompt(_("Do you want me to do it for you "
914
			     "[Y/n]? "), PROMPT_ECHO);
915
	res = tolower(*yesno) == 'n' ?
916
		-1 : bisect_start(terms, 0, empty_strvec);
917

918
	return res;
919
}
920

921
static enum bisect_error bisect_state(struct bisect_terms *terms, int argc,
922
				      const char **argv)
923
{
924
	const char *state;
925
	int i, verify_expected = 1;
926
	struct object_id oid, expected;
927
	struct oid_array revs = OID_ARRAY_INIT;
928

929
	if (!argc)
930
		return error(_("Please call `--bisect-state` with at least one argument"));
931

932
	if (bisect_autostart(terms))
933
		return BISECT_FAILED;
934

935
	state = argv[0];
936
	if (check_and_set_terms(terms, state) ||
937
	    !one_of(state, terms->term_good, terms->term_bad, "skip", NULL))
938
		return BISECT_FAILED;
939

940
	argv++;
941
	argc--;
942
	if (argc > 1 && !strcmp(state, terms->term_bad))
943
		return error(_("'git bisect %s' can take only one argument."), terms->term_bad);
944

945
	if (argc == 0) {
946
		const char *head = "BISECT_HEAD";
947
		enum get_oid_result res_head = repo_get_oid(the_repository,
948
							    head, &oid);
949

950
		if (res_head == MISSING_OBJECT) {
951
			head = "HEAD";
952
			res_head = repo_get_oid(the_repository, head, &oid);
953
		}
954

955
		if (res_head)
956
			error(_("Bad rev input: %s"), head);
957
		oid_array_append(&revs, &oid);
958
	}
959

960
	/*
961
	 * All input revs must be checked before executing bisect_write()
962
	 * to discard junk revs.
963
	 */
964

965
	for (; argc; argc--, argv++) {
966
		struct commit *commit;
967

968
		if (repo_get_oid(the_repository, *argv, &oid)){
969
			error(_("Bad rev input: %s"), *argv);
970
			oid_array_clear(&revs);
971
			return BISECT_FAILED;
972
		}
973

974
		commit = lookup_commit_reference(the_repository, &oid);
975
		if (!commit)
976
			die(_("Bad rev input (not a commit): %s"), *argv);
977

978
		oid_array_append(&revs, &commit->object.oid);
979
	}
980

981
	if (refs_read_ref(get_main_ref_store(the_repository), "BISECT_EXPECTED_REV", &expected))
982
		verify_expected = 0; /* Ignore invalid file contents */
983

984
	for (i = 0; i < revs.nr; i++) {
985
		if (bisect_write(state, oid_to_hex(&revs.oid[i]), terms, 0)) {
986
			oid_array_clear(&revs);
987
			return BISECT_FAILED;
988
		}
989
		if (verify_expected && !oideq(&revs.oid[i], &expected)) {
990
			unlink_or_warn(git_path_bisect_ancestors_ok());
991
			refs_delete_ref(get_main_ref_store(the_repository),
992
					NULL, "BISECT_EXPECTED_REV", NULL,
993
					REF_NO_DEREF);
994
			verify_expected = 0;
995
		}
996
	}
997

998
	oid_array_clear(&revs);
999
	return bisect_auto_next(terms, NULL);
1000
}
1001

1002
static enum bisect_error bisect_log(void)
1003
{
1004
	int fd, status;
1005
	const char* filename = git_path_bisect_log();
1006

1007
	if (is_empty_or_missing_file(filename))
1008
		return error(_("We are not bisecting."));
1009

1010
	fd = open(filename, O_RDONLY);
1011
	if (fd < 0)
1012
		return BISECT_FAILED;
1013

1014
	status = copy_fd(fd, STDOUT_FILENO);
1015
	close(fd);
1016
	return status ? BISECT_FAILED : BISECT_OK;
1017
}
1018

1019
static int process_replay_line(struct bisect_terms *terms, struct strbuf *line)
1020
{
1021
	const char *p = line->buf + strspn(line->buf, " \t");
1022
	char *word_end, *rev;
1023

1024
	if ((!skip_prefix(p, "git bisect", &p) &&
1025
	!skip_prefix(p, "git-bisect", &p)) || !isspace(*p))
1026
		return 0;
1027
	p += strspn(p, " \t");
1028

1029
	word_end = (char *)p + strcspn(p, " \t");
1030
	rev = word_end + strspn(word_end, " \t");
1031
	*word_end = '\0'; /* NUL-terminate the word */
1032

1033
	get_terms(terms);
1034
	if (check_and_set_terms(terms, p))
1035
		return -1;
1036

1037
	if (!strcmp(p, "start")) {
1038
		struct strvec argv = STRVEC_INIT;
1039
		int res;
1040
		sq_dequote_to_strvec(rev, &argv);
1041
		res = bisect_start(terms, argv.nr, argv.v);
1042
		strvec_clear(&argv);
1043
		return res;
1044
	}
1045

1046
	if (one_of(p, terms->term_good,
1047
	   terms->term_bad, "skip", NULL))
1048
		return bisect_write(p, rev, terms, 0);
1049

1050
	if (!strcmp(p, "terms")) {
1051
		struct strvec argv = STRVEC_INIT;
1052
		int res;
1053
		sq_dequote_to_strvec(rev, &argv);
1054
		res = bisect_terms(terms, argv.nr == 1 ? argv.v[0] : NULL);
1055
		strvec_clear(&argv);
1056
		return res;
1057
	}
1058
	error(_("'%s'?? what are you talking about?"), p);
1059

1060
	return -1;
1061
}
1062

1063
static enum bisect_error bisect_replay(struct bisect_terms *terms, const char *filename)
1064
{
1065
	FILE *fp = NULL;
1066
	enum bisect_error res = BISECT_OK;
1067
	struct strbuf line = STRBUF_INIT;
1068

1069
	if (is_empty_or_missing_file(filename))
1070
		return error(_("cannot read file '%s' for replaying"), filename);
1071

1072
	if (bisect_reset(NULL))
1073
		return BISECT_FAILED;
1074

1075
	fp = fopen(filename, "r");
1076
	if (!fp)
1077
		return BISECT_FAILED;
1078

1079
	while ((strbuf_getline(&line, fp) != EOF) && !res)
1080
		res = process_replay_line(terms, &line);
1081

1082
	strbuf_release(&line);
1083
	fclose(fp);
1084

1085
	if (res)
1086
		return BISECT_FAILED;
1087

1088
	return bisect_auto_next(terms, NULL);
1089
}
1090

1091
static enum bisect_error bisect_skip(struct bisect_terms *terms, int argc,
1092
				     const char **argv)
1093
{
1094
	int i;
1095
	enum bisect_error res;
1096
	struct strvec argv_state = STRVEC_INIT;
1097

1098
	strvec_push(&argv_state, "skip");
1099

1100
	for (i = 0; i < argc; i++) {
1101
		const char *dotdot = strstr(argv[i], "..");
1102

1103
		if (dotdot) {
1104
			struct rev_info revs;
1105
			struct commit *commit;
1106

1107
			repo_init_revisions(the_repository, &revs, NULL);
1108
			setup_revisions(2, argv + i - 1, &revs, NULL);
1109

1110
			if (prepare_revision_walk(&revs))
1111
				die(_("revision walk setup failed\n"));
1112
			while ((commit = get_revision(&revs)) != NULL)
1113
				strvec_push(&argv_state,
1114
						oid_to_hex(&commit->object.oid));
1115

1116
			reset_revision_walk();
1117
			release_revisions(&revs);
1118
		} else {
1119
			strvec_push(&argv_state, argv[i]);
1120
		}
1121
	}
1122
	res = bisect_state(terms, argv_state.nr, argv_state.v);
1123

1124
	strvec_clear(&argv_state);
1125
	return res;
1126
}
1127

1128
static int bisect_visualize(struct bisect_terms *terms, int argc,
1129
			    const char **argv)
1130
{
1131
	struct child_process cmd = CHILD_PROCESS_INIT;
1132
	struct strbuf sb = STRBUF_INIT;
1133

1134
	if (bisect_next_check(terms, NULL) != 0)
1135
		return BISECT_FAILED;
1136

1137
	cmd.no_stdin = 1;
1138
	if (!argc) {
1139
		if ((getenv("DISPLAY") || getenv("SESSIONNAME") || getenv("MSYSTEM") ||
1140
		     getenv("SECURITYSESSIONID")) && exists_in_PATH("gitk")) {
1141
			strvec_push(&cmd.args, "gitk");
1142
		} else {
1143
			strvec_push(&cmd.args, "log");
1144
			cmd.git_cmd = 1;
1145
		}
1146
	} else {
1147
		if (argv[0][0] == '-') {
1148
			strvec_push(&cmd.args, "log");
1149
			cmd.git_cmd = 1;
1150
		} else if (strcmp(argv[0], "tig") && !starts_with(argv[0], "git"))
1151
			cmd.git_cmd = 1;
1152

1153
		strvec_pushv(&cmd.args, argv);
1154
	}
1155

1156
	strvec_pushl(&cmd.args, "--bisect", "--", NULL);
1157

1158
	strbuf_read_file(&sb, git_path_bisect_names(), 0);
1159
	sq_dequote_to_strvec(sb.buf, &cmd.args);
1160
	strbuf_release(&sb);
1161

1162
	return run_command(&cmd);
1163
}
1164

1165
static int get_first_good(const char *refname UNUSED,
1166
			  const char *referent UNUSED,
1167
			  const struct object_id *oid,
1168
			  int flag UNUSED, void *cb_data)
1169
{
1170
	oidcpy(cb_data, oid);
1171
	return 1;
1172
}
1173

1174
static int do_bisect_run(const char *command)
1175
{
1176
	struct child_process cmd = CHILD_PROCESS_INIT;
1177

1178
	printf(_("running %s\n"), command);
1179
	cmd.use_shell = 1;
1180
	strvec_push(&cmd.args, command);
1181
	return run_command(&cmd);
1182
}
1183

1184
static int verify_good(const struct bisect_terms *terms, const char *command)
1185
{
1186
	int rc;
1187
	enum bisect_error res;
1188
	struct object_id good_rev;
1189
	struct object_id current_rev;
1190
	char *good_glob = xstrfmt("%s-*", terms->term_good);
1191
	int no_checkout = refs_ref_exists(get_main_ref_store(the_repository),
1192
					  "BISECT_HEAD");
1193

1194
	refs_for_each_glob_ref_in(get_main_ref_store(the_repository),
1195
				  get_first_good, good_glob, "refs/bisect/",
1196
				  &good_rev);
1197
	free(good_glob);
1198

1199
	if (refs_read_ref(get_main_ref_store(the_repository), no_checkout ? "BISECT_HEAD" : "HEAD", &current_rev))
1200
		return -1;
1201

1202
	res = bisect_checkout(&good_rev, no_checkout);
1203
	if (res != BISECT_OK)
1204
		return -1;
1205

1206
	rc = do_bisect_run(command);
1207

1208
	res = bisect_checkout(&current_rev, no_checkout);
1209
	if (res != BISECT_OK)
1210
		return -1;
1211

1212
	return rc;
1213
}
1214

1215
static int bisect_run(struct bisect_terms *terms, int argc, const char **argv)
1216
{
1217
	int res = BISECT_OK;
1218
	struct strbuf command = STRBUF_INIT;
1219
	const char *new_state;
1220
	int temporary_stdout_fd, saved_stdout;
1221
	int is_first_run = 1;
1222

1223
	if (bisect_next_check(terms, NULL))
1224
		return BISECT_FAILED;
1225

1226
	if (!argc) {
1227
		error(_("bisect run failed: no command provided."));
1228
		return BISECT_FAILED;
1229
	}
1230

1231
	sq_quote_argv(&command, argv);
1232
	strbuf_ltrim(&command);
1233
	while (1) {
1234
		res = do_bisect_run(command.buf);
1235

1236
		/*
1237
		 * Exit code 126 and 127 can either come from the shell
1238
		 * if it was unable to execute or even find the script,
1239
		 * or from the script itself.  Check with a known-good
1240
		 * revision to avoid trashing the bisect run due to a
1241
		 * missing or non-executable script.
1242
		 */
1243
		if (is_first_run && (res == 126 || res == 127)) {
1244
			int rc = verify_good(terms, command.buf);
1245
			is_first_run = 0;
1246
			if (rc < 0 || 128 <= rc) {
1247
				error(_("unable to verify %s on good"
1248
					" revision"), command.buf);
1249
				res = BISECT_FAILED;
1250
				break;
1251
			}
1252
			if (rc == res) {
1253
				error(_("bogus exit code %d for good revision"),
1254
				      rc);
1255
				res = BISECT_FAILED;
1256
				break;
1257
			}
1258
		}
1259

1260
		if (res < 0 || 128 <= res) {
1261
			error(_("bisect run failed: exit code %d from"
1262
				" %s is < 0 or >= 128"), res, command.buf);
1263
			break;
1264
		}
1265

1266
		if (res == 125)
1267
			new_state = "skip";
1268
		else if (!res)
1269
			new_state = terms->term_good;
1270
		else
1271
			new_state = terms->term_bad;
1272

1273
		temporary_stdout_fd = open(git_path_bisect_run(), O_CREAT | O_WRONLY | O_TRUNC, 0666);
1274

1275
		if (temporary_stdout_fd < 0) {
1276
			res = error_errno(_("cannot open file '%s' for writing"), git_path_bisect_run());
1277
			break;
1278
		}
1279

1280
		fflush(stdout);
1281
		saved_stdout = dup(1);
1282
		dup2(temporary_stdout_fd, 1);
1283

1284
		res = bisect_state(terms, 1, &new_state);
1285

1286
		fflush(stdout);
1287
		dup2(saved_stdout, 1);
1288
		close(saved_stdout);
1289
		close(temporary_stdout_fd);
1290

1291
		print_file_to_stdout(git_path_bisect_run());
1292

1293
		if (res == BISECT_ONLY_SKIPPED_LEFT)
1294
			error(_("bisect run cannot continue any more"));
1295
		else if (res == BISECT_INTERNAL_SUCCESS_MERGE_BASE) {
1296
			puts(_("bisect run success"));
1297
			res = BISECT_OK;
1298
		} else if (res == BISECT_INTERNAL_SUCCESS_1ST_BAD_FOUND) {
1299
			puts(_("bisect found first bad commit"));
1300
			res = BISECT_OK;
1301
		} else if (res) {
1302
			error(_("bisect run failed: 'git bisect %s'"
1303
				" exited with error code %d"), new_state, res);
1304
		} else {
1305
			continue;
1306
		}
1307
		break;
1308
	}
1309

1310
	strbuf_release(&command);
1311
	return res;
1312
}
1313

1314
static int cmd_bisect__reset(int argc, const char **argv, const char *prefix UNUSED)
1315
{
1316
	if (argc > 1)
1317
		return error(_("'%s' requires either no argument or a commit"),
1318
			     "git bisect reset");
1319
	return bisect_reset(argc ? argv[0] : NULL);
1320
}
1321

1322
static int cmd_bisect__terms(int argc, const char **argv, const char *prefix UNUSED)
1323
{
1324
	int res;
1325
	struct bisect_terms terms = { 0 };
1326

1327
	if (argc > 1)
1328
		return error(_("'%s' requires 0 or 1 argument"),
1329
			     "git bisect terms");
1330
	res = bisect_terms(&terms, argc == 1 ? argv[0] : NULL);
1331
	free_terms(&terms);
1332
	return res;
1333
}
1334

1335
static int cmd_bisect__start(int argc, const char **argv, const char *prefix UNUSED)
1336
{
1337
	int res;
1338
	struct bisect_terms terms = { 0 };
1339

1340
	set_terms(&terms, "bad", "good");
1341
	res = bisect_start(&terms, argc, argv);
1342
	free_terms(&terms);
1343
	return res;
1344
}
1345

1346
static int cmd_bisect__next(int argc, const char **argv UNUSED, const char *prefix)
1347
{
1348
	int res;
1349
	struct bisect_terms terms = { 0 };
1350

1351
	if (argc)
1352
		return error(_("'%s' requires 0 arguments"),
1353
			     "git bisect next");
1354
	get_terms(&terms);
1355
	res = bisect_next(&terms, prefix);
1356
	free_terms(&terms);
1357
	return res;
1358
}
1359

1360
static int cmd_bisect__log(int argc UNUSED, const char **argv UNUSED, const char *prefix UNUSED)
1361
{
1362
	return bisect_log();
1363
}
1364

1365
static int cmd_bisect__replay(int argc, const char **argv, const char *prefix UNUSED)
1366
{
1367
	int res;
1368
	struct bisect_terms terms = { 0 };
1369

1370
	if (argc != 1)
1371
		return error(_("no logfile given"));
1372
	set_terms(&terms, "bad", "good");
1373
	res = bisect_replay(&terms, argv[0]);
1374
	free_terms(&terms);
1375
	return res;
1376
}
1377

1378
static int cmd_bisect__skip(int argc, const char **argv, const char *prefix UNUSED)
1379
{
1380
	int res;
1381
	struct bisect_terms terms = { 0 };
1382

1383
	set_terms(&terms, "bad", "good");
1384
	get_terms(&terms);
1385
	res = bisect_skip(&terms, argc, argv);
1386
	free_terms(&terms);
1387
	return res;
1388
}
1389

1390
static int cmd_bisect__visualize(int argc, const char **argv, const char *prefix UNUSED)
1391
{
1392
	int res;
1393
	struct bisect_terms terms = { 0 };
1394

1395
	get_terms(&terms);
1396
	res = bisect_visualize(&terms, argc, argv);
1397
	free_terms(&terms);
1398
	return res;
1399
}
1400

1401
static int cmd_bisect__run(int argc, const char **argv, const char *prefix UNUSED)
1402
{
1403
	int res;
1404
	struct bisect_terms terms = { 0 };
1405

1406
	if (!argc)
1407
		return error(_("'%s' failed: no command provided."), "git bisect run");
1408
	get_terms(&terms);
1409
	res = bisect_run(&terms, argc, argv);
1410
	free_terms(&terms);
1411
	return res;
1412
}
1413

1414
int cmd_bisect(int argc, const char **argv, const char *prefix)
1415
{
1416
	int res = 0;
1417
	parse_opt_subcommand_fn *fn = NULL;
1418
	struct option options[] = {
1419
		OPT_SUBCOMMAND("reset", &fn, cmd_bisect__reset),
1420
		OPT_SUBCOMMAND("terms", &fn, cmd_bisect__terms),
1421
		OPT_SUBCOMMAND("start", &fn, cmd_bisect__start),
1422
		OPT_SUBCOMMAND("next", &fn, cmd_bisect__next),
1423
		OPT_SUBCOMMAND("log", &fn, cmd_bisect__log),
1424
		OPT_SUBCOMMAND("replay", &fn, cmd_bisect__replay),
1425
		OPT_SUBCOMMAND("skip", &fn, cmd_bisect__skip),
1426
		OPT_SUBCOMMAND("visualize", &fn, cmd_bisect__visualize),
1427
		OPT_SUBCOMMAND("view", &fn, cmd_bisect__visualize),
1428
		OPT_SUBCOMMAND("run", &fn, cmd_bisect__run),
1429
		OPT_END()
1430
	};
1431
	argc = parse_options(argc, argv, prefix, options, git_bisect_usage,
1432
			     PARSE_OPT_SUBCOMMAND_OPTIONAL);
1433

1434
	if (!fn) {
1435
		struct bisect_terms terms = { 0 };
1436

1437
		if (!argc)
1438
			usage_msg_opt(_("need a command"), git_bisect_usage, options);
1439

1440
		set_terms(&terms, "bad", "good");
1441
		get_terms(&terms);
1442
		if (check_and_set_terms(&terms, argv[0]))
1443
			usage_msg_optf(_("unknown command: '%s'"), git_bisect_usage,
1444
				       options, argv[0]);
1445
		res = bisect_state(&terms, argc, argv);
1446
		free_terms(&terms);
1447
	} else {
1448
		argc--;
1449
		argv++;
1450
		res = fn(argc, argv, prefix);
1451
	}
1452

1453
	return is_bisect_success(res) ? 0 : -res;
1454
}
1455

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.