X-Git-Url: https://git.tokkee.org/?a=blobdiff_plain;f=builtin-add.c;h=45b14e8a61764e18d8b89ebfef550a820ffbf831;hb=5410a02ab9e6a1987147724f8ea65e6a077b3832;hp=734547994f3a35e04fc7fdea10396c0c87bc6f0d;hpb=d60a6a662fac58f833efde93c962314fd5d83541;p=git.git diff --git a/builtin-add.c b/builtin-add.c index 734547994..45b14e8a6 100644 --- a/builtin-add.c +++ b/builtin-add.c @@ -12,9 +12,13 @@ #include "diffcore.h" #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] [--] ..."; +static const char * const builtin_add_usage[] = { + "git-add [options] [--] ...", + NULL +}; static int take_worktree_changes; static const char *excludes_file; @@ -44,6 +48,7 @@ static void prune_directory(struct dir_struct *dir, const char **pathspec, int p die("pathspec '%s' did not match any files", pathspec[i]); } + free(seen); } static void fill_directory(struct dir_struct *dir, const char **pathspec, @@ -60,7 +65,7 @@ static void fill_directory(struct dir_struct *dir, const char **pathspec, path = git_path("info/exclude"); if (!access(path, R_OK)) add_excludes_from_file(dir, path); - if (!access(excludes_file, R_OK)) + if (excludes_file != NULL && !access(excludes_file, R_OK)) add_excludes_from_file(dir, excludes_file); } @@ -71,12 +76,8 @@ static void fill_directory(struct dir_struct *dir, const char **pathspec, baselen = common_prefix(pathspec); path = "."; base = ""; - if (baselen) { - char *common = xmalloc(baselen + 1); - memcpy(common, *pathspec, baselen); - common[baselen] = 0; - path = base = common; - } + if (baselen) + path = base = xmemdupz(*pathspec, baselen); /* Read the directory and prune it */ read_directory(dir, path, base, baselen, pathspec); @@ -95,9 +96,10 @@ static void update_callback(struct diff_queue_struct *q, const char *path = p->one->path; switch (p->status) { default: - die("unexpacted diff status %c", p->status); + die("unexpected diff status %c", p->status); case DIFF_STATUS_UNMERGED: case DIFF_STATUS_MODIFIED: + case DIFF_STATUS_TYPE_CHANGED: add_file_to_cache(path, verbose); break; case DIFF_STATUS_DELETED: @@ -109,18 +111,34 @@ static void update_callback(struct diff_queue_struct *q, } } -static void update(int verbose, const char **files) +void add_files_to_cache(int verbose, const char *prefix, const char **files) { struct rev_info rev; - init_revisions(&rev, ""); + init_revisions(&rev, prefix); setup_revisions(0, NULL, &rev, NULL); - rev.prune_data = get_pathspec(rev.prefix, files); + rev.prune_data = get_pathspec(prefix, files); rev.diffopt.output_format = DIFF_FORMAT_CALLBACK; rev.diffopt.format_callback = update_callback; rev.diffopt.format_callback_data = &verbose; + run_diff_files(&rev, 0); +} + +static void refresh(int verbose, const char **pathspec) +{ + char *seen; + int i, specs; + + for (specs = 0; pathspec[specs]; specs++) + /* nothing */; + seen = xcalloc(specs, 1); if (read_cache() < 0) die("index file corrupt"); - run_diff_files(&rev, 0); + refresh_index(&the_index, verbose ? 0 : REFRESH_QUIET, pathspec, seen); + for (i = 0; i < specs; i++) { + if (!seen[i]) + die("pathspec '%s' did not match any files", pathspec[i]); + } + free(seen); } static int git_add_config(const char *var, const char *value) @@ -135,76 +153,68 @@ static int git_add_config(const char *var, const char *value) return git_default_config(var, value); } +int interactive_add(void) +{ + const char *argv[2] = { "add--interactive", NULL }; + + return run_command_v_opt(argv, RUN_GIT_CMD); +} + static struct lock_file lock_file; -static const char ignore_warning[] = +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__DRY_RUN(&show_only), + OPT__VERBOSE(&verbose), + OPT_GROUP(""), + OPT_BOOLEAN('i', "interactive", &add_interactive, "interactive picking"), + OPT_BOOLEAN('f', NULL, &ignored_too, "allow adding otherwise ignored files"), + OPT_BOOLEAN('u', NULL, &take_worktree_changes, "update tracked files"), + OPT_BOOLEAN( 0 , "refresh", &refresh_only, "don't add, only refresh the index"), + OPT_END(), +}; + int cmd_add(int argc, const char **argv, const char *prefix) { - int i, newfd; - int verbose = 0, show_only = 0, ignored_too = 0; + int i, newfd, orig_argc = argc; 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++; - } + argc = parse_options(argc, argv, builtin_add_options, + builtin_add_usage, 0); if (add_interactive) { - const char *args[] = { "add--interactive", NULL }; - - if (add_interactive != 1 || argc != 2) + if (add_interactive != 1 || orig_argc != 2) die("add --interactive does not take any parameters"); - execv_git_cmd(args); - exit(1); + exit(interactive_add()); } git_config(git_add_config); 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; - } - usage(builtin_add_usage); - } - if (take_worktree_changes) { - update(verbose, argv + i); + if (read_cache() < 0) + die("index file corrupt"); + add_files_to_cache(verbose, prefix, argv); goto finish; } - if (argc <= i) { + if (argc == 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); + goto finish; + } fill_directory(&dir, pathspec, ignored_too); @@ -223,12 +233,12 @@ int cmd_add(int argc, const char **argv, const char *prefix) die("index file corrupt"); if (dir.ignored_nr) { - fprintf(stderr, ignore_warning); + fprintf(stderr, ignore_error); for (i = 0; i < dir.ignored_nr; i++) { fprintf(stderr, "%s\n", dir.ignored[i]->name); } fprintf(stderr, "Use -f if you really want to add them.\n"); - exit(1); + die("no files added"); } for (i = 0; i < dir.nr; i++)