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