aa6495dc8413eb54294b593896a9a9bd7e64d8fa
1#include "builtin.h"
2#include "cache.h"
3#include "parse-options.h"
4#include "bisect.h"
5#include "refs.h"
6#include "dir.h"
7#include "argv-array.h"
8#include "run-command.h"
9
10static GIT_PATH_FUNC(git_path_bisect_terms, "BISECT_TERMS")
11static GIT_PATH_FUNC(git_path_bisect_expected_rev, "BISECT_EXPECTED_REV")
12static GIT_PATH_FUNC(git_path_bisect_ancestors_ok, "BISECT_ANCESTORS_OK")
13static GIT_PATH_FUNC(git_path_bisect_start, "BISECT_START")
14static GIT_PATH_FUNC(git_path_bisect_head, "BISECT_HEAD")
15
16static const char * const git_bisect_helper_usage[] = {
17 N_("git bisect--helper --next-all [--no-checkout]"),
18 N_("git bisect--helper --write-terms <bad_term> <good_term>"),
19 N_("git bisect--helper --bisect-clean-state"),
20 N_("git bisect--helper --bisect-reset [<commit>]"),
21 NULL
22};
23
24/*
25 * Check whether the string `term` belongs to the set of strings
26 * included in the variable arguments.
27 */
28LAST_ARG_MUST_BE_NULL
29static int one_of(const char *term, ...)
30{
31 int res = 0;
32 va_list matches;
33 const char *match;
34
35 va_start(matches, term);
36 while (!res && (match = va_arg(matches, const char *)))
37 res = !strcmp(term, match);
38 va_end(matches);
39
40 return res;
41}
42
43static int check_term_format(const char *term, const char *orig_term)
44{
45 int res;
46 char *new_term = xstrfmt("refs/bisect/%s", term);
47
48 res = check_refname_format(new_term, 0);
49 free(new_term);
50
51 if (res)
52 return error(_("'%s' is not a valid term"), term);
53
54 if (one_of(term, "help", "start", "skip", "next", "reset",
55 "visualize", "view", "replay", "log", "run", "terms", NULL))
56 return error(_("can't use the builtin command '%s' as a term"), term);
57
58 /*
59 * In theory, nothing prevents swapping completely good and bad,
60 * but this situation could be confusing and hasn't been tested
61 * enough. Forbid it for now.
62 */
63
64 if ((strcmp(orig_term, "bad") && one_of(term, "bad", "new", NULL)) ||
65 (strcmp(orig_term, "good") && one_of(term, "good", "old", NULL)))
66 return error(_("can't change the meaning of the term '%s'"), term);
67
68 return 0;
69}
70
71static int write_terms(const char *bad, const char *good)
72{
73 FILE *fp = NULL;
74 int res;
75
76 if (!strcmp(bad, good))
77 return error(_("please use two different terms"));
78
79 if (check_term_format(bad, "bad") || check_term_format(good, "good"))
80 return -1;
81
82 fp = fopen(git_path_bisect_terms(), "w");
83 if (!fp)
84 return error_errno(_("could not open the file BISECT_TERMS"));
85
86 res = fprintf(fp, "%s\n%s\n", bad, good);
87 res |= fclose(fp);
88 return (res < 0) ? -1 : 0;
89}
90
91static int is_expected_rev(const char *expected_hex)
92{
93 struct strbuf actual_hex = STRBUF_INIT;
94 int res = 0;
95 if (strbuf_read_file(&actual_hex, git_path_bisect_expected_rev(), 0) >= 40) {
96 strbuf_trim(&actual_hex);
97 res = !strcmp(actual_hex.buf, expected_hex);
98 }
99 strbuf_release(&actual_hex);
100 return res;
101}
102
103static void check_expected_revs(const char **revs, int rev_nr)
104{
105 int i;
106
107 for (i = 0; i < rev_nr; i++) {
108 if (!is_expected_rev(revs[i])) {
109 unlink_or_warn(git_path_bisect_ancestors_ok());
110 unlink_or_warn(git_path_bisect_expected_rev());
111 }
112 }
113}
114
115static int bisect_reset(const char *commit)
116{
117 struct strbuf branch = STRBUF_INIT;
118
119 if (!commit) {
120 if (strbuf_read_file(&branch, git_path_bisect_start(), 0) < 1) {
121 printf(_("We are not bisecting.\n"));
122 return 0;
123 }
124 strbuf_rtrim(&branch);
125 } else {
126 struct object_id oid;
127
128 if (get_oid_commit(commit, &oid))
129 return error(_("'%s' is not a valid commit"), commit);
130 strbuf_addstr(&branch, commit);
131 }
132
133 if (!file_exists(git_path_bisect_head())) {
134 struct argv_array argv = ARGV_ARRAY_INIT;
135
136 argv_array_pushl(&argv, "checkout", branch.buf, "--", NULL);
137 if (run_command_v_opt(argv.argv, RUN_GIT_CMD)) {
138 strbuf_release(&branch);
139 argv_array_clear(&argv);
140 return error(_("could not check out original"
141 " HEAD '%s'. Try 'git bisect"
142 "reset <commit>'."), branch.buf);
143 }
144 argv_array_clear(&argv);
145 }
146
147 strbuf_release(&branch);
148 return bisect_clean_state();
149}
150
151int cmd_bisect__helper(int argc, const char **argv, const char *prefix)
152{
153 enum {
154 NEXT_ALL = 1,
155 WRITE_TERMS,
156 BISECT_CLEAN_STATE,
157 CHECK_EXPECTED_REVS,
158 BISECT_RESET
159 } cmdmode = 0;
160 int no_checkout = 0;
161 struct option options[] = {
162 OPT_CMDMODE(0, "next-all", &cmdmode,
163 N_("perform 'git bisect next'"), NEXT_ALL),
164 OPT_CMDMODE(0, "write-terms", &cmdmode,
165 N_("write the terms to .git/BISECT_TERMS"), WRITE_TERMS),
166 OPT_CMDMODE(0, "bisect-clean-state", &cmdmode,
167 N_("cleanup the bisection state"), BISECT_CLEAN_STATE),
168 OPT_CMDMODE(0, "check-expected-revs", &cmdmode,
169 N_("check for expected revs"), CHECK_EXPECTED_REVS),
170 OPT_CMDMODE(0, "bisect-reset", &cmdmode,
171 N_("reset the bisection state"), BISECT_RESET),
172 OPT_BOOL(0, "no-checkout", &no_checkout,
173 N_("update BISECT_HEAD instead of checking out the current commit")),
174 OPT_END()
175 };
176
177 argc = parse_options(argc, argv, prefix, options,
178 git_bisect_helper_usage, 0);
179
180 if (!cmdmode)
181 usage_with_options(git_bisect_helper_usage, options);
182
183 switch (cmdmode) {
184 case NEXT_ALL:
185 return bisect_next_all(prefix, no_checkout);
186 case WRITE_TERMS:
187 if (argc != 2)
188 return error(_("--write-terms requires two arguments"));
189 return write_terms(argv[0], argv[1]);
190 case BISECT_CLEAN_STATE:
191 if (argc != 0)
192 return error(_("--bisect-clean-state requires no arguments"));
193 return bisect_clean_state();
194 case CHECK_EXPECTED_REVS:
195 check_expected_revs(argv, argc);
196 return 0;
197 case BISECT_RESET:
198 if (argc > 1)
199 return error(_("--bisect-reset requires either no argument or a commit"));
200 return !!bisect_reset(argc ? argv[0] : NULL);
201 default:
202 return error("BUG: unknown subcommand '%d'", cmdmode);
203 }
204 return 0;
205}