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

The parsing of "--index" is done in a callback, but it does not handle an "unset" option. We don't necessarily expect anyone to use this, but the current behavior is to treat it exactly like "--index", which would probably be surprising. Instead, let's just turn it into an OPT_BOOL, and handle it after we're done parsing. This makes "--no-index" just work (it cancels a previous "--index"). As a bonus, this makes the logic easier to follow. The old code opened the index during the option parsing, leaving the reader to wonder if there was some timing issue (there isn't; none of the other options care that we've opened it). And then if we found that "--prefix" had been given, we had to rollback the index. Now we can simply avoid opening it in the first place. Note that it might make more sense for checkout-index to complain when "--index --prefix=foo" is given (rather than silently ignoring "--index"), but since it has been that way since 415e96c ([PATCH] Implement git-checkout-cache -u to update stat information in the cache., 2005-05-15), it's safer to leave it as-is. 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:29 UTC 6a6df8aa45aa68df737d6c59d7abec499e9451da
1 file changed +10 -24
builtin/checkout-index.c
+10 -24
@@ -130,18 +130,6 @@ static const char * const builtin_checkout_index_usage[] = {
130
131 static struct lock_file lock_file;
132
133 -static int option_parse_u(const struct option *opt,
134 - const char *arg, int unset)
135 -{
136 - int *newfd = opt->value;
137 -
138 - state.refresh_cache = 1;
139 - state.istate = &the_index;
140 - if (*newfd < 0)
141 - *newfd = hold_locked_index(&lock_file, 1);
142 - return 0;
143 -}
144 -
133 static int option_parse_stage(const struct option *opt,
134 const char *arg, int unset)
135 {
@@ -166,6 +154,7 @@ int cmd_checkout_index(int argc, const char **argv, const char *prefix)
154 int read_from_stdin = 0;
155 int prefix_length;
156 int force = 0, quiet = 0, not_new = 0;
157 + int index_opt = 0;
158 struct option builtin_checkout_index_options[] = {
159 OPT_BOOL('a', "all", &all,
160 N_("check out all files in the index")),
@@ -174,9 +163,8 @@ int cmd_checkout_index(int argc, const char **argv, const char *prefix)
163 N_("no warning for existing files and files not in index")),
164 OPT_BOOL('n', "no-create", &not_new,
165 N_("don't checkout new files")),
177 - { OPTION_CALLBACK, 'u', "index", &newfd, NULL,
178 - N_("update stat information in the index file"),
179 - PARSE_OPT_NOARG, option_parse_u },
166 + OPT_BOOL('u', "index", &index_opt,
167 + N_("update stat information in the index file")),
168 OPT_BOOL('z', NULL, &nul_term_line,
169 N_("paths are separated with NUL character")),
170 OPT_BOOL(0, "stdin", &read_from_stdin,
@@ -211,15 +199,13 @@ int cmd_checkout_index(int argc, const char **argv, const char *prefix)
199 state.base_dir = "";
200 state.base_dir_len = strlen(state.base_dir);
201
214 - if (state.base_dir_len || to_tempfile) {
215 - /* when --prefix is specified we do not
216 - * want to update cache.
217 - */
218 - if (state.refresh_cache) {
219 - rollback_lock_file(&lock_file);
220 - newfd = -1;
221 - }
222 - state.refresh_cache = 0;
202 + /*
203 + * when --prefix is specified we do not want to update cache.
204 + */
205 + if (index_opt && !state.base_dir_len && !to_tempfile) {
206 + state.refresh_cache = 1;
207 + state.istate = &the_index;
208 + newfd = hold_locked_index(&lock_file, 1);
209 }
210
211 /* Check out named files first */