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