Code

Merge branch 'jc/parse-date-raw' into maint
[git.git] / builtin / add.c
index 2705f8d057a93f7b4a9351713b89fd9f4e041815..1c42900ff8c55a94ccfd1d214567d0f64d615412 100644 (file)
@@ -13,6 +13,7 @@
 #include "diff.h"
 #include "diffcore.h"
 #include "revision.h"
+#include "bulk-checkin.h"
 
 static const char * const builtin_add_usage[] = {
        "git add [options] [--] <filepattern>...",
@@ -21,12 +22,32 @@ static const char * const builtin_add_usage[] = {
 static int patch_interactive, add_interactive, edit_interactive;
 static int take_worktree_changes;
 
-struct update_callback_data
-{
+struct update_callback_data {
        int flags;
        int add_errors;
 };
 
+static int fix_unmerged_status(struct diff_filepair *p,
+                              struct update_callback_data *data)
+{
+       if (p->status != DIFF_STATUS_UNMERGED)
+               return p->status;
+       if (!(data->flags & ADD_CACHE_IGNORE_REMOVAL) && !p->two->mode)
+               /*
+                * This is not an explicit add request, and the
+                * path is missing from the working tree (deleted)
+                */
+               return DIFF_STATUS_DELETED;
+       else
+               /*
+                * Either an explicit add request, or path exists
+                * in the working tree.  An attempt to explicitly
+                * add a path that does not exist in the working tree
+                * will be caught as an error by the caller immediately.
+                */
+               return DIFF_STATUS_MODIFIED;
+}
+
 static void update_callback(struct diff_queue_struct *q,
                            struct diff_options *opt, void *cbdata)
 {
@@ -36,35 +57,14 @@ static void update_callback(struct diff_queue_struct *q,
        for (i = 0; i < q->nr; i++) {
                struct diff_filepair *p = q->queue[i];
                const char *path = p->one->path;
-               switch (p->status) {
+               switch (fix_unmerged_status(p, data)) {
                default:
-                       die("unexpected diff status %c", p->status);
-               case DIFF_STATUS_UNMERGED:
-                       /*
-                        * ADD_CACHE_IGNORE_REMOVAL is unset if "git
-                        * add -u" is calling us, In such a case, a
-                        * missing work tree file needs to be removed
-                        * if there is an unmerged entry at stage #2,
-                        * but such a diff record is followed by
-                        * another with DIFF_STATUS_DELETED (and if
-                        * there is no stage #2, we won't see DELETED
-                        * nor MODIFIED).  We can simply continue
-                        * either way.
-                        */
-                       if (!(data->flags & ADD_CACHE_IGNORE_REMOVAL))
-                               continue;
-                       /*
-                        * Otherwise, it is "git add path" is asking
-                        * to explicitly add it; we fall through.  A
-                        * missing work tree file is an error and is
-                        * caught by add_file_to_index() in such a
-                        * case.
-                        */
+                       die(_("unexpected diff status %c"), p->status);
                case DIFF_STATUS_MODIFIED:
                case DIFF_STATUS_TYPE_CHANGED:
                        if (add_file_to_index(&the_index, path, data->flags)) {
                                if (!(data->flags & ADD_CACHE_IGNORE_ERRORS))
-                                       die("updating files failed");
+                                       die(_("updating files failed"));
                                data->add_errors++;
                        }
                        break;
@@ -74,7 +74,7 @@ static void update_callback(struct diff_queue_struct *q,
                        if (!(data->flags & ADD_CACHE_PRETEND))
                                remove_file_from_index(&the_index, path);
                        if (data->flags & (ADD_CACHE_PRETEND|ADD_CACHE_VERBOSE))
-                               printf("remove '%s'\n", path);
+                               printf(_("remove '%s'\n"), path);
                        break;
                }
        }
@@ -86,12 +86,13 @@ int add_files_to_cache(const char *prefix, const char **pathspec, int flags)
        struct rev_info rev;
        init_revisions(&rev, prefix);
        setup_revisions(0, NULL, &rev, NULL);
-       rev.prune_data = pathspec;
+       init_pathspec(&rev.prune_data, pathspec);
        rev.diffopt.output_format = DIFF_FORMAT_CALLBACK;
        rev.diffopt.format_callback = update_callback;
        data.flags = flags;
        data.add_errors = 0;
        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);
        return !!data.add_errors;
 }
@@ -117,7 +118,19 @@ static void fill_pathspec_matches(const char **pathspec, char *seen, int specs)
        }
 }
 
-static void prune_directory(struct dir_struct *dir, const char **pathspec, int prefix)
+static char *find_used_pathspec(const char **pathspec)
+{
+       char *seen;
+       int i;
+
+       for (i = 0; pathspec[i];  i++)
+               ; /* just counting */
+       seen = xcalloc(i, 1);
+       fill_pathspec_matches(pathspec, seen, i);
+       return seen;
+}
+
+static char *prune_directory(struct dir_struct *dir, const char **pathspec, int prefix)
 {
        char *seen;
        int i, specs;
@@ -137,13 +150,7 @@ static void prune_directory(struct dir_struct *dir, const char **pathspec, int p
        }
        dir->nr = dst - dir->entries;
        fill_pathspec_matches(pathspec, seen, specs);
-
-       for (i = 0; i < specs; i++) {
-               if (!seen[i] && pathspec[i][0] && !file_exists(pathspec[i]))
-                       die("pathspec '%s' did not match any files",
-                                       pathspec[i]);
-       }
-        free(seen);
+       return seen;
 }
 
 static void treat_gitlinks(const char **pathspec)
@@ -166,7 +173,7 @@ static void treat_gitlinks(const char **pathspec)
                                        /* strip trailing slash */
                                        pathspec[j] = xstrndup(ce->name, len);
                                else
-                                       die ("Path '%s' is in submodule '%.*s'",
+                                       die (_("Path '%s' is in submodule '%.*s'"),
                                                pathspec[j], len, ce->name);
                        }
                }
@@ -182,10 +189,10 @@ static void refresh(int verbose, const char **pathspec)
                /* nothing */;
        seen = xcalloc(specs, 1);
        refresh_index(&the_index, verbose ? REFRESH_IN_PORCELAIN : REFRESH_QUIET,
-                     pathspec, seen, "Unstaged changes after refreshing the index:");
+                     pathspec, seen, _("Unstaged changes after refreshing the index:"));
        for (i = 0; i < specs; i++) {
                if (!seen[i])
-                       die("pathspec '%s' did not match any files", pathspec[i]);
+                       die(_("pathspec '%s' did not match any files"), pathspec[i]);
        }
         free(seen);
 }
@@ -199,7 +206,7 @@ static const char **validate_pathspec(int argc, const char **argv, const char *p
                for (p = pathspec; *p; p++) {
                        if (has_symlink_leading_path(*p, strlen(*p))) {
                                int len = prefix ? strlen(prefix) : 0;
-                               die("'%s' is beyond a symbolic link", *p + len);
+                               die(_("'%s' is beyond a symbolic link"), *p + len);
                        }
                }
        }
@@ -236,7 +243,7 @@ int run_add_interactive(const char *revision, const char *patch_mode,
        return status;
 }
 
-int interactive_add(int argc, const char **argv, const char *prefix)
+int interactive_add(int argc, const char **argv, const char *prefix, int patch)
 {
        const char **pathspec = NULL;
 
@@ -247,7 +254,7 @@ int interactive_add(int argc, const char **argv, const char *prefix)
        }
 
        return run_add_interactive(NULL,
-                                  patch_interactive ? "--patch" : NULL,
+                                  patch ? "--patch" : NULL,
                                   pathspec);
 }
 
@@ -255,16 +262,18 @@ static int edit_patch(int argc, const char **argv, const char *prefix)
 {
        char *file = xstrdup(git_path("ADD_EDIT.patch"));
        const char *apply_argv[] = { "apply", "--recount", "--cached",
-               file, NULL };
+               NULL, NULL };
        struct child_process child;
        struct rev_info rev;
        int out;
        struct stat st;
 
+       apply_argv[3] = file;
+
        git_config(git_diff_basic_config, NULL); /* no "diff" UI options */
 
        if (read_cache() < 0)
-               die ("Could not read the index");
+               die (_("Could not read the index"));
 
        init_revisions(&rev, prefix);
        rev.diffopt.context = 7;
@@ -273,24 +282,24 @@ static int edit_patch(int argc, const char **argv, const char *prefix)
        rev.diffopt.output_format = DIFF_FORMAT_PATCH;
        out = open(file, O_CREAT | O_WRONLY, 0644);
        if (out < 0)
-               die ("Could not open '%s' for writing.", file);
+               die (_("Could not open '%s' for writing."), file);
        rev.diffopt.file = xfdopen(out, "w");
        rev.diffopt.close_file = 1;
        if (run_diff_files(&rev, 0))
-               die ("Could not write patch");
+               die (_("Could not write patch"));
 
        launch_editor(file, NULL, NULL);
 
        if (stat(file, &st))
-               die_errno("Could not stat '%s'", file);
+               die_errno(_("Could not stat '%s'"), file);
        if (!st.st_size)
-               die("Empty patch. Aborted.");
+               die(_("Empty patch. Aborted."));
 
        memset(&child, 0, sizeof(child));
        child.git_cmd = 1;
        child.argv = apply_argv;
        if (run_command(&child))
-               die ("Could not apply '%s'", file);
+               die (_("Could not apply '%s'"), file);
 
        unlink(file);
        return 0;
@@ -299,30 +308,32 @@ static int edit_patch(int argc, const char **argv, const char *prefix)
 static struct lock_file lock_file;
 
 static const char ignore_error[] =
-"The following paths are ignored by one of your .gitignore files:\n";
+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;
+static int ignore_add_errors, addremove, intent_to_add, ignore_missing = 0;
 
 static struct option builtin_add_options[] = {
-       OPT__DRY_RUN(&show_only),
-       OPT__VERBOSE(&verbose),
+       OPT__DRY_RUN(&show_only, "dry run"),
+       OPT__VERBOSE(&verbose, "be verbose"),
        OPT_GROUP(""),
        OPT_BOOLEAN('i', "interactive", &add_interactive, "interactive picking"),
-       OPT_BOOLEAN('p', "patch", &patch_interactive, "interactive patching"),
+       OPT_BOOLEAN('p', "patch", &patch_interactive, "select hunks interactively"),
        OPT_BOOLEAN('e', "edit", &edit_interactive, "edit current diff and apply"),
-       OPT_BOOLEAN('f', "force", &ignored_too, "allow adding otherwise ignored files"),
+       OPT__FORCE(&ignored_too, "allow adding otherwise ignored files"),
        OPT_BOOLEAN('u', "update", &take_worktree_changes, "update tracked files"),
        OPT_BOOLEAN('N', "intent-to-add", &intent_to_add, "record only the fact that the path will be added later"),
-       OPT_BOOLEAN('A', "all", &addremove, "add all, noticing removal of tracked files"),
+       OPT_BOOLEAN('A', "all", &addremove, "add changes from all tracked and untracked files"),
        OPT_BOOLEAN( 0 , "refresh", &refresh_only, "don't add, only refresh the index"),
        OPT_BOOLEAN( 0 , "ignore-errors", &ignore_add_errors, "just skip files which cannot be added because of errors"),
+       OPT_BOOLEAN( 0 , "ignore-missing", &ignore_missing, "check if - even missing - files are ignored in dry run"),
        OPT_END(),
 };
 
 static int add_config(const char *var, const char *value, void *cb)
 {
-       if (!strcasecmp(var, "add.ignore-errors")) {
+       if (!strcmp(var, "add.ignoreerrors") ||
+           !strcmp(var, "add.ignore-errors")) {
                ignore_add_errors = git_config_bool(var, value);
                return 0;
        }
@@ -334,17 +345,17 @@ static int add_files(struct dir_struct *dir, int flags)
        int i, exit_status = 0;
 
        if (dir->ignored_nr) {
-               fprintf(stderr, ignore_error);
+               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");
-               die("no files added");
+               fprintf(stderr, _("Use -f if you really want to add them.\n"));
+               die(_("no files added"));
        }
 
        for (i = 0; i < dir->nr; i++)
                if (add_file_to_cache(dir->entries[i]->name, flags)) {
                        if (!ignore_add_errors)
-                               die("adding files failed");
+                               die(_("adding files failed"));
                        exit_status = 1;
                }
        return exit_status;
@@ -359,6 +370,7 @@ int cmd_add(int argc, const char **argv, const char *prefix)
        int flags;
        int add_new_files;
        int require_pathspec;
+       char *seen = NULL;
 
        git_config(add_config, NULL);
 
@@ -367,7 +379,7 @@ int cmd_add(int argc, const char **argv, const char *prefix)
        if (patch_interactive)
                add_interactive = 1;
        if (add_interactive)
-               exit(interactive_add(argc - 1, argv + 1, prefix));
+               exit(interactive_add(argc - 1, argv + 1, prefix, patch_interactive));
 
        if (edit_interactive)
                return(edit_patch(argc, argv, prefix));
@@ -375,7 +387,9 @@ int cmd_add(int argc, const char **argv, const char *prefix)
        argv++;
 
        if (addremove && take_worktree_changes)
-               die("-A and -u are mutually incompatible");
+               die(_("-A and -u are mutually incompatible"));
+       if (!show_only && ignore_missing)
+               die(_("Option --ignore-missing can only be used together with --dry-run"));
        if ((addremove || take_worktree_changes) && !argc) {
                static const char *here[2] = { ".", NULL };
                argc = 1;
@@ -395,14 +409,14 @@ int cmd_add(int argc, const char **argv, const char *prefix)
                  ? ADD_CACHE_IGNORE_REMOVAL : 0));
 
        if (require_pathspec && argc == 0) {
-               fprintf(stderr, "Nothing specified, nothing added.\n");
-               fprintf(stderr, "Maybe you wanted to say 'git add .'?\n");
+               fprintf(stderr, _("Nothing specified, nothing added.\n"));
+               fprintf(stderr, _("Maybe you wanted to say 'git add .'?\n"));
                return 0;
        }
        pathspec = validate_pathspec(argc, argv, prefix);
 
        if (read_cache() < 0)
-               die("index file corrupt");
+               die(_("index file corrupt"));
        treat_gitlinks(pathspec);
 
        if (add_new_files) {
@@ -418,7 +432,7 @@ int cmd_add(int argc, const char **argv, const char *prefix)
                /* This picks up the paths that are not tracked */
                baselen = fill_directory(&dir, pathspec);
                if (pathspec)
-                       prune_directory(&dir, pathspec, baselen);
+                       seen = prune_directory(&dir, pathspec, baselen);
        }
 
        if (refresh_only) {
@@ -426,16 +440,39 @@ int cmd_add(int argc, const char **argv, const char *prefix)
                goto finish;
        }
 
+       if (pathspec) {
+               int i;
+               if (!seen)
+                       seen = find_used_pathspec(pathspec);
+               for (i = 0; pathspec[i]; i++) {
+                       if (!seen[i] && pathspec[i][0]
+                           && !file_exists(pathspec[i])) {
+                               if (ignore_missing) {
+                                       int dtype = DT_UNKNOWN;
+                                       if (excluded(&dir, pathspec[i], &dtype))
+                                               dir_add_ignored(&dir, pathspec[i], strlen(pathspec[i]));
+                               } else
+                                       die(_("pathspec '%s' did not match any files"),
+                                           pathspec[i]);
+                       }
+               }
+               free(seen);
+       }
+
+       plug_bulk_checkin();
+
        exit_status |= add_files_to_cache(prefix, pathspec, flags);
 
        if (add_new_files)
                exit_status |= add_files(&dir, flags);
 
+       unplug_bulk_checkin();
+
  finish:
        if (active_cache_changed) {
                if (write_cache(newfd, active_cache, active_nr) ||
                    commit_locked_index(&lock_file))
-                       die("Unable to write new index file");
+                       die(_("Unable to write new index file"));
        }
 
        return exit_status;