0de516ad95b0c6fe9441a217cfd84b046f568bfe
   1/*
   2 * "git add" builtin command
   3 *
   4 * Copyright (C) 2006 Linus Torvalds
   5 */
   6#include "cache.h"
   7#include "builtin.h"
   8#include "dir.h"
   9#include "exec_cmd.h"
  10#include "cache-tree.h"
  11#include "run-command.h"
  12#include "parse-options.h"
  13
  14static const char * const builtin_add_usage[] = {
  15        "git add [options] [--] <filepattern>...",
  16        NULL
  17};
  18static int patch_interactive = 0, add_interactive = 0;
  19static int take_worktree_changes;
  20
  21static void prune_directory(struct dir_struct *dir, const char **pathspec, int prefix)
  22{
  23        char *seen;
  24        int i, specs;
  25        struct dir_entry **src, **dst;
  26
  27        for (specs = 0; pathspec[specs];  specs++)
  28                /* nothing */;
  29        seen = xcalloc(specs, 1);
  30
  31        src = dst = dir->entries;
  32        i = dir->nr;
  33        while (--i >= 0) {
  34                struct dir_entry *entry = *src++;
  35                if (match_pathspec(pathspec, entry->name, entry->len,
  36                                   prefix, seen))
  37                        *dst++ = entry;
  38        }
  39        dir->nr = dst - dir->entries;
  40
  41        for (i = 0; i < specs; i++) {
  42                if (!seen[i] && !file_exists(pathspec[i]))
  43                        die("pathspec '%s' did not match any files",
  44                                        pathspec[i]);
  45        }
  46        free(seen);
  47}
  48
  49static void fill_directory(struct dir_struct *dir, const char **pathspec,
  50                int ignored_too)
  51{
  52        const char *path, *base;
  53        int baselen;
  54
  55        /* Set up the default git porcelain excludes */
  56        memset(dir, 0, sizeof(*dir));
  57        if (!ignored_too) {
  58                dir->collect_ignored = 1;
  59                setup_standard_excludes(dir);
  60        }
  61
  62        /*
  63         * Calculate common prefix for the pathspec, and
  64         * use that to optimize the directory walk
  65         */
  66        baselen = common_prefix(pathspec);
  67        path = ".";
  68        base = "";
  69        if (baselen)
  70                path = base = xmemdupz(*pathspec, baselen);
  71
  72        /* Read the directory and prune it */
  73        read_directory(dir, path, base, baselen, pathspec);
  74        if (pathspec)
  75                prune_directory(dir, pathspec, baselen);
  76}
  77
  78static void refresh(int verbose, const char **pathspec)
  79{
  80        char *seen;
  81        int i, specs;
  82
  83        for (specs = 0; pathspec[specs];  specs++)
  84                /* nothing */;
  85        seen = xcalloc(specs, 1);
  86        refresh_index(&the_index, verbose ? REFRESH_SAY_CHANGED : REFRESH_QUIET,
  87                      pathspec, seen);
  88        for (i = 0; i < specs; i++) {
  89                if (!seen[i])
  90                        die("pathspec '%s' did not match any files", pathspec[i]);
  91        }
  92        free(seen);
  93}
  94
  95static const char **validate_pathspec(int argc, const char **argv, const char *prefix)
  96{
  97        const char **pathspec = get_pathspec(prefix, argv);
  98
  99        return pathspec;
 100}
 101
 102int interactive_add(int argc, const char **argv, const char *prefix)
 103{
 104        int status, ac;
 105        const char **args;
 106        const char **pathspec = NULL;
 107
 108        if (argc) {
 109                pathspec = validate_pathspec(argc, argv, prefix);
 110                if (!pathspec)
 111                        return -1;
 112        }
 113
 114        args = xcalloc(sizeof(const char *), (argc + 4));
 115        ac = 0;
 116        args[ac++] = "add--interactive";
 117        if (patch_interactive)
 118                args[ac++] = "--patch";
 119        args[ac++] = "--";
 120        if (argc) {
 121                memcpy(&(args[ac]), pathspec, sizeof(const char *) * argc);
 122                ac += argc;
 123        }
 124        args[ac] = NULL;
 125
 126        status = run_command_v_opt(args, RUN_GIT_CMD);
 127        free(args);
 128        return status;
 129}
 130
 131static struct lock_file lock_file;
 132
 133static const char ignore_error[] =
 134"The following paths are ignored by one of your .gitignore files:\n";
 135
 136static int verbose = 0, show_only = 0, ignored_too = 0, refresh_only = 0;
 137static int ignore_add_errors, addremove;
 138
 139static struct option builtin_add_options[] = {
 140        OPT__DRY_RUN(&show_only),
 141        OPT__VERBOSE(&verbose),
 142        OPT_GROUP(""),
 143        OPT_BOOLEAN('i', "interactive", &add_interactive, "interactive picking"),
 144        OPT_BOOLEAN('p', "patch", &patch_interactive, "interactive patching"),
 145        OPT_BOOLEAN('f', "force", &ignored_too, "allow adding otherwise ignored files"),
 146        OPT_BOOLEAN('u', "update", &take_worktree_changes, "update tracked files"),
 147        OPT_BOOLEAN('A', "all", &addremove, "add all, noticing removal of tracked files"),
 148        OPT_BOOLEAN( 0 , "refresh", &refresh_only, "don't add, only refresh the index"),
 149        OPT_BOOLEAN( 0 , "ignore-errors", &ignore_add_errors, "just skip files which cannot be added because of errors"),
 150        OPT_END(),
 151};
 152
 153static int add_config(const char *var, const char *value, void *cb)
 154{
 155        if (!strcasecmp(var, "add.ignore-errors")) {
 156                ignore_add_errors = git_config_bool(var, value);
 157                return 0;
 158        }
 159        return git_default_config(var, value, cb);
 160}
 161
 162static int add_files(struct dir_struct *dir, int flags)
 163{
 164        int i, exit_status = 0;
 165
 166        if (dir->ignored_nr) {
 167                fprintf(stderr, ignore_error);
 168                for (i = 0; i < dir->ignored_nr; i++)
 169                        fprintf(stderr, "%s\n", dir->ignored[i]->name);
 170                fprintf(stderr, "Use -f if you really want to add them.\n");
 171                die("no files added");
 172        }
 173
 174        for (i = 0; i < dir->nr; i++)
 175                if (add_file_to_cache(dir->entries[i]->name, flags)) {
 176                        if (!ignore_add_errors)
 177                                die("adding files failed");
 178                        exit_status = 1;
 179                }
 180        return exit_status;
 181}
 182
 183int cmd_add(int argc, const char **argv, const char *prefix)
 184{
 185        int exit_status = 0;
 186        int newfd;
 187        const char **pathspec;
 188        struct dir_struct dir;
 189        int flags;
 190        int add_new_files;
 191        int require_pathspec;
 192
 193        argc = parse_options(argc, argv, builtin_add_options,
 194                          builtin_add_usage, 0);
 195        if (patch_interactive)
 196                add_interactive = 1;
 197        if (add_interactive)
 198                exit(interactive_add(argc, argv, prefix));
 199
 200        git_config(add_config, NULL);
 201
 202        if (addremove && take_worktree_changes)
 203                die("-A and -u are mutually incompatible");
 204        if (addremove && !argc) {
 205                static const char *here[2] = { ".", NULL };
 206                argc = 1;
 207                argv = here;
 208        }
 209
 210        add_new_files = !take_worktree_changes && !refresh_only;
 211        require_pathspec = !take_worktree_changes;
 212
 213        newfd = hold_locked_index(&lock_file, 1);
 214
 215        flags = ((verbose ? ADD_CACHE_VERBOSE : 0) |
 216                 (show_only ? ADD_CACHE_PRETEND : 0) |
 217                 (ignore_add_errors ? ADD_CACHE_IGNORE_ERRORS : 0));
 218
 219        if (require_pathspec && argc == 0) {
 220                fprintf(stderr, "Nothing specified, nothing added.\n");
 221                fprintf(stderr, "Maybe you wanted to say 'git add .'?\n");
 222                return 0;
 223        }
 224        pathspec = get_pathspec(prefix, argv);
 225
 226        /*
 227         * If we are adding new files, we need to scan the working
 228         * tree to find the ones that match pathspecs; this needs
 229         * to be done before we read the index.
 230         */
 231        if (add_new_files)
 232                fill_directory(&dir, pathspec, ignored_too);
 233
 234        if (read_cache() < 0)
 235                die("index file corrupt");
 236
 237        if (refresh_only) {
 238                refresh(verbose, pathspec);
 239                goto finish;
 240        }
 241
 242        if (take_worktree_changes || addremove)
 243                exit_status |= add_files_to_cache(prefix, pathspec, flags);
 244
 245        if (add_new_files)
 246                exit_status |= add_files(&dir, flags);
 247
 248 finish:
 249        if (active_cache_changed) {
 250                if (write_cache(newfd, active_cache, active_nr) ||
 251                    commit_locked_index(&lock_file))
 252                        die("Unable to write new index file");
 253        }
 254
 255        return exit_status;
 256}