@matheusbsbs / matheus-bagliano / commits / a6c7db1b19
Fork 0

parse-opt: migrate builtin-checkout-index.

Signed-off-by: Miklos Vajna <vmiklos@frugalware.org> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Miklos Vajna committed Oct 18, 2008 at 03:17 UTC a6c7db1b1945d656cc0dd559d93c7b2129c65d86
1 file changed +83 -69
builtin-checkout-index.c
+83 -69
@@ -40,6 +40,7 @@
40 #include "cache.h"
41 #include "quote.h"
42 #include "cache-tree.h"
43 +#include "parse-options.h"
44
45 #define CHECKOUT_ALL 4
46 static int line_termination = '\n';
@@ -153,11 +154,58 @@ static void checkout_all(const char *prefix, int prefix_length)
154 exit(128);
155 }
156
156 -static const char checkout_cache_usage[] =
157 -"git checkout-index [-u] [-q] [-a] [-f] [-n] [--stage=[123]|all] [--prefix=<string>] [--temp] [--] <file>...";
157 +static const char * const builtin_checkout_index_usage[] = {
158 + "git checkout-index [options] [--] <file>...",
159 + NULL
160 +};
161
162 static struct lock_file lock_file;
163
164 +static int option_parse_u(const struct option *opt,
165 + const char *arg, int unset)
166 +{
167 + int *newfd = opt->value;
168 +
169 + state.refresh_cache = 1;
170 + if (*newfd < 0)
171 + *newfd = hold_locked_index(&lock_file, 1);
172 + return 0;
173 +}
174 +
175 +static int option_parse_z(const struct option *opt,
176 + const char *arg, int unset)
177 +{
178 + if (unset)
179 + line_termination = '\n';
180 + else
181 + line_termination = 0;
182 + return 0;
183 +}
184 +
185 +static int option_parse_prefix(const struct option *opt,
186 + const char *arg, int unset)
187 +{
188 + state.base_dir = arg;
189 + state.base_dir_len = strlen(arg);
190 + return 0;
191 +}
192 +
193 +static int option_parse_stage(const struct option *opt,
194 + const char *arg, int unset)
195 +{
196 + if (!strcmp(arg, "all")) {
197 + to_tempfile = 1;
198 + checkout_stage = CHECKOUT_ALL;
199 + } else {
200 + int ch = arg[0];
201 + if ('1' <= ch && ch <= '3')
202 + checkout_stage = arg[0] - '0';
203 + else
204 + die("stage should be between 1 and 3 or all");
205 + }
206 + return 0;
207 +}
208 +
209 int cmd_checkout_index(int argc, const char **argv, const char *prefix)
210 {
211 int i;
@@ -165,6 +213,33 @@ int cmd_checkout_index(int argc, const char **argv, const char *prefix)
213 int all = 0;
214 int read_from_stdin = 0;
215 int prefix_length;
216 + int force = 0, quiet = 0, not_new = 0;
217 + struct option builtin_checkout_index_options[] = {
218 + OPT_BOOLEAN('a', "all", &all,
219 + "checks out all files in the index"),
220 + OPT_BOOLEAN('f', "force", &force,
221 + "forces overwrite of existing files"),
222 + OPT__QUIET(&quiet),
223 + OPT_BOOLEAN('n', "no-create", &not_new,
224 + "don't checkout new files"),
225 + { OPTION_CALLBACK, 'u', "index", &newfd, NULL,
226 + "update stat information in the index file",
227 + PARSE_OPT_NOARG, option_parse_u },
228 + { OPTION_CALLBACK, 'z', NULL, NULL, NULL,
229 + "paths are separated with NUL character",
230 + PARSE_OPT_NOARG, option_parse_z },
231 + OPT_BOOLEAN(0, "stdin", &read_from_stdin,
232 + "read list of paths from the standard input"),
233 + OPT_BOOLEAN(0, "temp", &to_tempfile,
234 + "write the content to temporary files"),
235 + OPT_CALLBACK(0, "prefix", NULL, "string",
236 + "when creating files, prepend <string>",
237 + option_parse_prefix),
238 + OPT_CALLBACK(0, "stage", NULL, NULL,
239 + "copy out the files from named stage",
240 + option_parse_stage),
241 + OPT_END()
242 + };
243
244 git_config(git_default_config, NULL);
245 state.base_dir = "";
@@ -174,72 +249,11 @@ int cmd_checkout_index(int argc, const char **argv, const char *prefix)
249 die("invalid cache");
250 }
251
177 - for (i = 1; i < argc; i++) {
178 - const char *arg = argv[i];
179 -
180 - if (!strcmp(arg, "--")) {
181 - i++;
182 - break;
183 - }
184 - if (!strcmp(arg, "-a") || !strcmp(arg, "--all")) {
185 - all = 1;
186 - continue;
187 - }
188 - if (!strcmp(arg, "-f") || !strcmp(arg, "--force")) {
189 - state.force = 1;
190 - continue;
191 - }
192 - if (!strcmp(arg, "-q") || !strcmp(arg, "--quiet")) {
193 - state.quiet = 1;
194 - continue;
195 - }
196 - if (!strcmp(arg, "-n") || !strcmp(arg, "--no-create")) {
197 - state.not_new = 1;
198 - continue;
199 - }
200 - if (!strcmp(arg, "-u") || !strcmp(arg, "--index")) {
201 - state.refresh_cache = 1;
202 - if (newfd < 0)
203 - newfd = hold_locked_index(&lock_file, 1);
204 - continue;
205 - }
206 - if (!strcmp(arg, "-z")) {
207 - line_termination = 0;
208 - continue;
209 - }
210 - if (!strcmp(arg, "--stdin")) {
211 - if (i != argc - 1)
212 - die("--stdin must be at the end");
213 - read_from_stdin = 1;
214 - i++; /* do not consider arg as a file name */
215 - break;
216 - }
217 - if (!strcmp(arg, "--temp")) {
218 - to_tempfile = 1;
219 - continue;
220 - }
221 - if (!prefixcmp(arg, "--prefix=")) {
222 - state.base_dir = arg+9;
223 - state.base_dir_len = strlen(state.base_dir);
224 - continue;
225 - }
226 - if (!prefixcmp(arg, "--stage=")) {
227 - if (!strcmp(arg + 8, "all")) {
228 - to_tempfile = 1;
229 - checkout_stage = CHECKOUT_ALL;
230 - } else {
231 - int ch = arg[8];
232 - if ('1' <= ch && ch <= '3')
233 - checkout_stage = arg[8] - '0';
234 - else
235 - die("stage should be between 1 and 3 or all");
236 - }
237 - continue;
238 - }
239 - if (arg[0] == '-')
240 - usage(checkout_cache_usage);
241 - break;
242 - }
252 + argc = parse_options(argc, argv, builtin_checkout_index_options,
253 + builtin_checkout_index_usage, 0);
254 + state.force = force;
255 + state.quiet = quiet;
256 + state.not_new = not_new;
257
258 if (state.base_dir_len || to_tempfile) {
259 /* when --prefix is specified we do not
@@ -253,7 +267,7 @@ int cmd_checkout_index(int argc, const char **argv, const char *prefix)
267 }
268
269 /* Check out named files first */
256 - for ( ; i < argc; i++) {
270 + for (i = 0; i < argc; i++) {
271 const char *arg = argv[i];
272 const char *p;
273