checkout-index: handle "--no-prefix" option

We use a custom callback to parse "--prefix", but it does not handle the "unset" case. As a result, passing "--no-prefix" will cause a segfault. We can fix this by switching it to an OPT_STRING, which makes "--no-prefix" counteract a previous "--prefix". Note that this assigns NULL, so we bump our default-case initialization to lower in the main function. Signed-off-by: Jeff King <peff@peff.net> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Jeff King committed Jan 31, 2016 at 06:26 UTC 5ed5f671c4193f2b9d47c08e989574903f3bf9cc
1 file changed +6 -12
builtin/checkout-index.c
+6 -12
@@ -142,14 +142,6 @@ static int option_parse_u(const struct option *opt,
142 return 0;
143 }
144
145 -static int option_parse_prefix(const struct option *opt,
146 - const char *arg, int unset)
147 -{
148 - state.base_dir = arg;
149 - state.base_dir_len = strlen(arg);
150 - return 0;
151 -}
152 -
145 static int option_parse_stage(const struct option *opt,
146 const char *arg, int unset)
147 {
@@ -191,9 +183,8 @@ int cmd_checkout_index(int argc, const char **argv, const char *prefix)
183 N_("read list of paths from the standard input")),
184 OPT_BOOL(0, "temp", &to_tempfile,
185 N_("write the content to temporary files")),
194 - OPT_CALLBACK(0, "prefix", NULL, N_("string"),
195 - N_("when creating files, prepend <string>"),
196 - option_parse_prefix),
186 + OPT_STRING(0, "prefix", &state.base_dir, N_("string"),
187 + N_("when creating files, prepend <string>")),
188 OPT_CALLBACK(0, "stage", NULL, NULL,
189 N_("copy out the files from named stage"),
190 option_parse_stage),
@@ -204,7 +195,6 @@ int cmd_checkout_index(int argc, const char **argv, const char *prefix)
195 usage_with_options(builtin_checkout_index_usage,
196 builtin_checkout_index_options);
197 git_config(git_default_config, NULL);
207 - state.base_dir = "";
198 prefix_length = prefix ? strlen(prefix) : 0;
199
200 if (read_cache() < 0) {
@@ -217,6 +207,10 @@ int cmd_checkout_index(int argc, const char **argv, const char *prefix)
207 state.quiet = quiet;
208 state.not_new = not_new;
209
210 + if (!state.base_dir)
211 + state.base_dir = "";
212 + state.base_dir_len = strlen(state.base_dir);
213 +
214 if (state.base_dir_len || to_tempfile) {
215 /* when --prefix is specified we do not
216 * want to update cache.