From: Kristian Høgsberg <krh@xxxxxxxxxx> Signed-off-by: Kristian Høgsberg <krh@xxxxxxxxxx> --- builtin-add.c | 69 +++++++++++++++++++------------------------------------- 1 files changed, 24 insertions(+), 45 deletions(-) diff --git a/builtin-add.c b/builtin-add.c index 966e145..e7d8550 100644 --- a/builtin-add.c +++ b/builtin-add.c @@ -13,9 +13,12 @@ #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>..."; +static const char * const builtin_add_usage[] = { + "git-add [options] [--] <filepattern>...", + NULL +}; static int take_worktree_changes; static const char *excludes_file; @@ -160,21 +163,29 @@ 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[] = { + OPT_BOOLEAN('i', "interactive", &add_interactive, "interactive picking"), + OPT_BOOLEAN('n', NULL, &show_only, "dry-run"), + OPT_BOOLEAN('f', NULL, &ignored_too, "allow adding otherwise ignored files"), + OPT_BOOLEAN('v', NULL, &verbose, "be verbose"), + OPT_BOOLEAN('u', NULL, &take_worktree_changes, "update only files that git already knows about"), + OPT_BOOLEAN( 0 , "refresh", &refresh_only, "don't add, only refresh stat() informations in the index"), +}; + 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, 0); if (add_interactive) { - if (argc != 2) + if (i > 0) die("add --interactive does not take any parameters"); exit(interactive_add()); } @@ -183,51 +194,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.3.GIT - 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