add: convert to use parse_pathspec
[gitweb.git] / builtin / add.c
index daf02c6affb6115d058736a6859ceeb10e0791f0..9a7235e92cad64623b83e1450ff7302b173ec842 100644 (file)
@@ -28,6 +28,9 @@ struct update_callback_data {
        int add_errors;
        const char *implicit_dot;
        size_t implicit_dot_len;
+
+       /* only needed for 2.0 transition preparation */
+       int warn_add_would_remove;
 };
 
 static const char *option_with_implicit_dot;
@@ -93,6 +96,24 @@ static int fix_unmerged_status(struct diff_filepair *p,
                return DIFF_STATUS_MODIFIED;
 }
 
+static const char *add_would_remove_warning = N_(
+       "You ran 'git add' with neither '-A (--all)' or '--ignore-removal',\n"
+"whose behaviour will change in Git 2.0 with respect to paths you removed.\n"
+"Paths like '%s' that are\n"
+"removed from your working tree are ignored with this version of Git.\n"
+"\n"
+"* 'git add --ignore-removal <pathspec>', which is the current default,\n"
+"  ignores paths you removed from your working tree.\n"
+"\n"
+"* 'git add --all <pathspec>' will let you also record the removals.\n"
+"\n"
+"Run 'git status' to check the paths you removed from your working tree.\n");
+
+static void warn_add_would_remove(const char *path)
+{
+       warning(_(add_would_remove_warning), path);
+}
+
 static void update_callback(struct diff_queue_struct *q,
                            struct diff_options *opt, void *cbdata)
 {
@@ -130,6 +151,10 @@ static void update_callback(struct diff_queue_struct *q,
                        }
                        break;
                case DIFF_STATUS_DELETED:
+                       if (data->warn_add_would_remove) {
+                               warn_add_would_remove(path);
+                               data->warn_add_would_remove = 0;
+                       }
                        if (data->flags & ADD_CACHE_IGNORE_REMOVAL)
                                break;
                        if (!(data->flags & ADD_CACHE_PRETEND))
@@ -141,32 +166,28 @@ static void update_callback(struct diff_queue_struct *q,
        }
 }
 
-int add_files_to_cache(const char *prefix, const char **pathspec, int flags)
+static void update_files_in_cache(const char *prefix, const char **pathspec,
+                                 struct update_callback_data *data)
 {
-       struct update_callback_data data;
        struct rev_info rev;
 
-       memset(&data, 0, sizeof(data));
-       data.flags = flags & ~ADD_CACHE_IMPLICIT_DOT;
-       if ((flags & ADD_CACHE_IMPLICIT_DOT) && prefix) {
-               /*
-                * Check for modified files throughout the worktree so
-                * update_callback has a chance to warn about changes
-                * outside the cwd.
-                */
-               data.implicit_dot = prefix;
-               data.implicit_dot_len = strlen(prefix);
-               pathspec = NULL;
-       }
-
        init_revisions(&rev, prefix);
        setup_revisions(0, NULL, &rev, NULL);
        init_pathspec(&rev.prune_data, pathspec);
        rev.diffopt.output_format = DIFF_FORMAT_CALLBACK;
        rev.diffopt.format_callback = update_callback;
-       rev.diffopt.format_callback_data = &data;
+       rev.diffopt.format_callback_data = data;
        rev.max_count = 0; /* do not compare unmerged paths with stage #2 */
        run_diff_files(&rev, DIFF_RACY_IS_MODIFIED);
+}
+
+int add_files_to_cache(const char *prefix, const char **pathspec, int flags)
+{
+       struct update_callback_data data;
+
+       memset(&data, 0, sizeof(data));
+       data.flags = flags;
+       update_files_in_cache(prefix, pathspec, &data);
        return !!data.add_errors;
 }
 
@@ -205,21 +226,6 @@ static char *prune_directory(struct dir_struct *dir, const char **pathspec,
        return seen;
 }
 
-/*
- * Checks the index to see whether any path in pathspec refers to
- * something inside a submodule.  If so, dies with an error message.
- */
-static void treat_gitlinks(const char **pathspec)
-{
-       int i;
-
-       if (!pathspec || !*pathspec)
-               return;
-
-       for (i = 0; pathspec[i]; i++)
-               pathspec[i] = check_path_for_gitlink(pathspec[i]);
-}
-
 static void refresh(int verbose, const char **pathspec)
 {
        char *seen;
@@ -237,25 +243,6 @@ static void refresh(int verbose, const char **pathspec)
         free(seen);
 }
 
-/*
- * Normalizes argv relative to prefix, via get_pathspec(), and then
- * runs die_if_path_beyond_symlink() on each path in the normalized
- * list.
- */
-static const char **validate_pathspec(const char **argv, const char *prefix)
-{
-       const char **pathspec = get_pathspec(prefix, argv);
-
-       if (pathspec) {
-               const char **p;
-               for (p = pathspec; *p; p++) {
-                       die_if_path_beyond_symlink(*p, prefix);
-               }
-       }
-
-       return pathspec;
-}
-
 int run_add_interactive(const char *revision, const char *patch_mode,
                        const char **pathspec)
 {
@@ -287,17 +274,23 @@ int run_add_interactive(const char *revision, const char *patch_mode,
 
 int interactive_add(int argc, const char **argv, const char *prefix, int patch)
 {
-       const char **pathspec = NULL;
+       struct pathspec pathspec;
 
-       if (argc) {
-               pathspec = validate_pathspec(argv, prefix);
-               if (!pathspec)
-                       return -1;
-       }
+       /*
+        * git-add--interactive itself does not parse pathspec. It
+        * simply passes the pathspec to other builtin commands. Let's
+        * hope all of them support all magic, or we'll need to limit
+        * the magic here. There is still a problem with prefix. But
+        * that'll be worked on later on.
+        */
+       parse_pathspec(&pathspec, PATHSPEC_ALL_MAGIC & ~PATHSPEC_FROMTOP,
+                      PATHSPEC_PREFER_FULL |
+                      PATHSPEC_SYMLINK_LEADING_PATH,
+                      prefix, argv);
 
        return run_add_interactive(NULL,
                                   patch ? "--patch" : NULL,
-                                  pathspec);
+                                  pathspec.raw);
 }
 
 static int edit_patch(int argc, const char **argv, const char *prefix)
@@ -354,23 +347,38 @@ static struct lock_file lock_file;
 static const char ignore_error[] =
 N_("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 ignore_add_errors, addremove, intent_to_add, ignore_missing = 0;
+static int verbose, show_only, ignored_too, refresh_only;
+static int ignore_add_errors, intent_to_add, ignore_missing;
+
+#define ADDREMOVE_DEFAULT 0 /* Change to 1 in Git 2.0 */
+static int addremove = ADDREMOVE_DEFAULT;
+static int addremove_explicit = -1; /* unspecified */
+
+static int ignore_removal_cb(const struct option *opt, const char *arg, int unset)
+{
+       /* if we are told to ignore, we are not adding removals */
+       *(int *)opt->value = !unset ? 0 : 1;
+       return 0;
+}
 
 static struct option builtin_add_options[] = {
        OPT__DRY_RUN(&show_only, N_("dry run")),
        OPT__VERBOSE(&verbose, N_("be verbose")),
        OPT_GROUP(""),
-       OPT_BOOLEAN('i', "interactive", &add_interactive, N_("interactive picking")),
-       OPT_BOOLEAN('p', "patch", &patch_interactive, N_("select hunks interactively")),
-       OPT_BOOLEAN('e', "edit", &edit_interactive, N_("edit current diff and apply")),
+       OPT_BOOL('i', "interactive", &add_interactive, N_("interactive picking")),
+       OPT_BOOL('p', "patch", &patch_interactive, N_("select hunks interactively")),
+       OPT_BOOL('e', "edit", &edit_interactive, N_("edit current diff and apply")),
        OPT__FORCE(&ignored_too, N_("allow adding otherwise ignored files")),
-       OPT_BOOLEAN('u', "update", &take_worktree_changes, N_("update tracked files")),
-       OPT_BOOLEAN('N', "intent-to-add", &intent_to_add, N_("record only the fact that the path will be added later")),
-       OPT_BOOLEAN('A', "all", &addremove, N_("add changes from all tracked and untracked files")),
-       OPT_BOOLEAN( 0 , "refresh", &refresh_only, N_("don't add, only refresh the index")),
-       OPT_BOOLEAN( 0 , "ignore-errors", &ignore_add_errors, N_("just skip files which cannot be added because of errors")),
-       OPT_BOOLEAN( 0 , "ignore-missing", &ignore_missing, N_("check if - even missing - files are ignored in dry run")),
+       OPT_BOOL('u', "update", &take_worktree_changes, N_("update tracked files")),
+       OPT_BOOL('N', "intent-to-add", &intent_to_add, N_("record only the fact that the path will be added later")),
+       OPT_BOOL('A', "all", &addremove_explicit, N_("add changes from all tracked and untracked files")),
+       { OPTION_CALLBACK, 0, "ignore-removal", &addremove_explicit,
+         NULL /* takes no arguments */,
+         N_("ignore paths removed in the working tree (same as --no-all)"),
+         PARSE_OPT_NOARG, ignore_removal_cb },
+       OPT_BOOL( 0 , "refresh", &refresh_only, N_("don't add, only refresh the index")),
+       OPT_BOOL( 0 , "ignore-errors", &ignore_add_errors, N_("just skip files which cannot be added because of errors")),
+       OPT_BOOL( 0 , "ignore-missing", &ignore_missing, N_("check if - even missing - files are ignored in dry run")),
        OPT_END(),
 };
 
@@ -409,13 +417,14 @@ int cmd_add(int argc, const char **argv, const char *prefix)
 {
        int exit_status = 0;
        int newfd;
-       const char **pathspec;
+       struct pathspec pathspec;
        struct dir_struct dir;
        int flags;
        int add_new_files;
        int require_pathspec;
        char *seen = NULL;
        int implicit_dot = 0;
+       struct update_callback_data update_data;
 
        git_config(add_config, NULL);
 
@@ -431,8 +440,29 @@ int cmd_add(int argc, const char **argv, const char *prefix)
        argc--;
        argv++;
 
+       if (0 <= addremove_explicit)
+               addremove = addremove_explicit;
+       else if (take_worktree_changes && ADDREMOVE_DEFAULT)
+               addremove = 0; /* "-u" was given but not "-A" */
+
        if (addremove && take_worktree_changes)
                die(_("-A and -u are mutually incompatible"));
+
+       /*
+        * Warn when "git add pathspec..." was given without "-u" or "-A"
+        * and pathspec... covers a removed path.
+        */
+       memset(&update_data, 0, sizeof(update_data));
+       if (!take_worktree_changes && addremove_explicit < 0)
+               update_data.warn_add_would_remove = 1;
+
+       if (!take_worktree_changes && addremove_explicit < 0 && argc)
+               /*
+                * Turn "git add pathspec..." to "git add -A pathspec..."
+                * in Git 2.0 but not yet
+                */
+               ; /* addremove = 1; */
+
        if (!show_only && ignore_missing)
                die(_("Option --ignore-missing can only be used together with --dry-run"));
        if (addremove) {
@@ -468,11 +498,19 @@ int cmd_add(int argc, const char **argv, const char *prefix)
                fprintf(stderr, _("Maybe you wanted to say 'git add .'?\n"));
                return 0;
        }
-       pathspec = validate_pathspec(argv, prefix);
 
        if (read_cache() < 0)
                die(_("index file corrupt"));
-       treat_gitlinks(pathspec);
+
+       /*
+        * Check the "pathspec '%s' did not match any files" block
+        * below before enabling new magic.
+        */
+       parse_pathspec(&pathspec, 0,
+                      PATHSPEC_PREFER_FULL |
+                      PATHSPEC_SYMLINK_LEADING_PATH |
+                      PATHSPEC_STRIP_SUBMODULE_SLASH_EXPENSIVE,
+                      prefix, argv);
 
        if (add_new_files) {
                int baselen;
@@ -485,44 +523,62 @@ int cmd_add(int argc, const char **argv, const char *prefix)
                }
 
                /* This picks up the paths that are not tracked */
-               baselen = fill_directory(&dir, implicit_dot ? NULL : pathspec);
-               if (pathspec)
-                       seen = prune_directory(&dir, pathspec, baselen,
+               baselen = fill_directory(&dir, implicit_dot ? NULL : pathspec.raw);
+               if (pathspec.nr)
+                       seen = prune_directory(&dir, pathspec.raw, baselen,
                                        implicit_dot ? WARN_IMPLICIT_DOT : 0);
        }
 
        if (refresh_only) {
-               refresh(verbose, pathspec);
+               refresh(verbose, pathspec.raw);
                goto finish;
        }
+       if (implicit_dot && prefix)
+               refresh_cache(REFRESH_QUIET);
 
-       if (pathspec) {
+       if (pathspec.nr) {
                int i;
-               struct path_exclude_check check;
 
-               path_exclude_check_init(&check, &dir);
                if (!seen)
-                       seen = find_pathspecs_matching_against_index(pathspec);
-               for (i = 0; pathspec[i]; i++) {
-                       if (!seen[i] && pathspec[i][0]
-                           && !file_exists(pathspec[i])) {
+                       seen = find_pathspecs_matching_against_index(pathspec.raw);
+
+               /*
+                * file_exists() assumes exact match
+                */
+               GUARD_PATHSPEC(&pathspec, PATHSPEC_FROMTOP);
+
+               for (i = 0; pathspec.raw[i]; i++) {
+                       if (!seen[i] && pathspec.raw[i][0]
+                           && !file_exists(pathspec.raw[i])) {
                                if (ignore_missing) {
                                        int dtype = DT_UNKNOWN;
-                                       if (is_path_excluded(&check, pathspec[i], -1, &dtype))
-                                               dir_add_ignored(&dir, pathspec[i], strlen(pathspec[i]));
+                                       if (is_excluded(&dir, pathspec.raw[i], &dtype))
+                                               dir_add_ignored(&dir, pathspec.raw[i], strlen(pathspec.raw[i]));
                                } else
                                        die(_("pathspec '%s' did not match any files"),
-                                           pathspec[i]);
+                                           pathspec.raw[i]);
                        }
                }
                free(seen);
-               path_exclude_check_clear(&check);
        }
 
        plug_bulk_checkin();
 
-       exit_status |= add_files_to_cache(prefix, pathspec, flags);
+       if ((flags & ADD_CACHE_IMPLICIT_DOT) && prefix) {
+               /*
+                * Check for modified files throughout the worktree so
+                * update_callback has a chance to warn about changes
+                * outside the cwd.
+                */
+               update_data.implicit_dot = prefix;
+               update_data.implicit_dot_len = strlen(prefix);
+               free_pathspec(&pathspec);
+               memset(&pathspec, 0, sizeof(pathspec));
+       }
+       update_data.flags = flags & ~ADD_CACHE_IMPLICIT_DOT;
+       update_files_in_cache(prefix, pathspec.raw, &update_data);
 
+       exit_status |= !!update_data.add_errors;
        if (add_new_files)
                exit_status |= add_files(&dir, flags);