Signed-off-by: Kristian Høgsberg <krh@xxxxxxxxxx> --- builtin-add.c | 64 ++++++++++++++++++-------------------------------------- 1 files changed, 21 insertions(+), 43 deletions(-) diff --git a/builtin-add.c b/builtin-add.c index 966e145..66fd99d 100644 --- a/builtin-add.c +++ b/builtin-add.c @@ -13,6 +13,7 @@ #include "commit.h" #include "revision.h" #include "run-command.h" +#include "parse-options.h" static const char builtin_add_usage[] = "git-add [-n] [-v] [-f] [--interactive | -i] [-u] [--refresh] [--] <filepattern>..."; @@ -160,21 +161,30 @@ static struct lock_file lock_file; static const char ignore_error[] = "The following paths are ignored by one of your .gitignore files:\n"; +static int verbose = 0, show_only = 0, ignored_too = 0, refresh_only = 0; +static int add_interactive = 0; + +static struct option builtin_add_options[] = { + { OPTION_BOOLEAN, "interactive", 'i', &add_interactive }, + { OPTION_BOOLEAN, NULL, 'n', &show_only }, + { OPTION_BOOLEAN, NULL, 'f', &ignored_too }, + { OPTION_BOOLEAN, NULL, 'v',&verbose }, + { OPTION_BOOLEAN, NULL, 'u',&take_worktree_changes }, + { OPTION_BOOLEAN, "refresh", 0, &refresh_only } +}; + int cmd_add(int argc, const char **argv, const char *prefix) { int i, newfd; - int verbose = 0, show_only = 0, ignored_too = 0, refresh_only = 0; const char **pathspec; struct dir_struct dir; - int add_interactive = 0; - for (i = 1; i < argc; i++) { - if (!strcmp("--interactive", argv[i]) || - !strcmp("-i", argv[i])) - add_interactive++; - } + i = parse_options(argc, argv, builtin_add_options, + ARRAY_SIZE(builtin_add_options), + builtin_add_usage); + if (add_interactive) { - if (argc != 2) + if (i > 0) die("add --interactive does not take any parameters"); exit(interactive_add()); } @@ -183,51 +193,19 @@ int cmd_add(int argc, const char **argv, const char *prefix) newfd = hold_locked_index(&lock_file, 1); - for (i = 1; i < argc; i++) { - const char *arg = argv[i]; - - if (arg[0] != '-') - break; - if (!strcmp(arg, "--")) { - i++; - break; - } - if (!strcmp(arg, "-n")) { - show_only = 1; - continue; - } - if (!strcmp(arg, "-f")) { - ignored_too = 1; - continue; - } - if (!strcmp(arg, "-v")) { - verbose = 1; - continue; - } - if (!strcmp(arg, "-u")) { - take_worktree_changes = 1; - continue; - } - if (!strcmp(arg, "--refresh")) { - refresh_only = 1; - continue; - } - usage(builtin_add_usage); - } - if (take_worktree_changes) { if (read_cache() < 0) die("index file corrupt"); - add_files_to_cache(verbose, prefix, argv + i); + add_files_to_cache(verbose, prefix, argv); goto finish; } - if (argc <= i) { + if (i == 0) { fprintf(stderr, "Nothing specified, nothing added.\n"); fprintf(stderr, "Maybe you wanted to say 'git add .'?\n"); return 0; } - pathspec = get_pathspec(prefix, argv + i); + pathspec = get_pathspec(prefix, argv); if (refresh_only) { refresh(verbose, pathspec); -- 1.5.2.5 - To unsubscribe from this list: send the line "unsubscribe git" in the body of a message to majordomo@xxxxxxxxxxxxxxx More majordomo info at http://vger.kernel.org/majordomo-info.html