builtin-add.con commit Merge branch 'jc/add-stop-at-symlink' (4a871de)
   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 "diff.h"
  12#include "diffcore.h"
  13#include "commit.h"
  14#include "revision.h"
  15#include "run-command.h"
  16#include "parse-options.h"
  17
  18static const char * const builtin_add_usage[] = {
  19        "git add [options] [--] <filepattern>...",
  20        NULL
  21};
  22static int patch_interactive = 0, add_interactive = 0;
  23static int take_worktree_changes;
  24
  25static void prune_directory(struct dir_struct *dir, const char **pathspec, int prefix)
  26{
  27        char *seen;
  28        int i, specs;
  29        struct dir_entry **src, **dst;
  30
  31        for (specs = 0; pathspec[specs];  specs++)
  32                /* nothing */;
  33        seen = xcalloc(specs, 1);
  34
  35        src = dst = dir->entries;
  36        i = dir->nr;
  37        while (--i >= 0) {
  38                struct dir_entry *entry = *src++;
  39                if (match_pathspec(pathspec, entry->name, entry->len,
  40                                   prefix, seen))
  41                        *dst++ = entry;
  42        }
  43        dir->nr = dst - dir->entries;
  44
  45        for (i = 0; i < specs; i++) {
  46                if (!seen[i] && !file_exists(pathspec[i]))
  47                        die("pathspec '%s' did not match any files",
  48                                        pathspec[i]);
  49        }
  50        free(seen);
  51}
  52
  53static void fill_directory(struct dir_struct *dir, const char **pathspec,
  54                int ignored_too)
  55{
  56        const char *path, *base;
  57        int baselen;
  58
  59        /* Set up the default git porcelain excludes */
  60        memset(dir, 0, sizeof(*dir));
  61        if (!ignored_too) {
  62                dir->collect_ignored = 1;
  63                setup_standard_excludes(dir);
  64        }
  65
  66        /*
  67         * Calculate common prefix for the pathspec, and
  68         * use that to optimize the directory walk
  69         */
  70        baselen = common_prefix(pathspec);
  71        path = ".";
  72        base = "";
  73        if (baselen)
  74                path = base = xmemdupz(*pathspec, baselen);
  75
  76        /* Read the directory and prune it */
  77        read_directory(dir, path, base, baselen, pathspec);
  78        if (pathspec)
  79                prune_directory(dir, pathspec, baselen);
  80}
  81
  82struct update_callback_data
  83{
  84        int flags;
  85        int add_errors;
  86};
  87
  88static void update_callback(struct diff_queue_struct *q,
  89                            struct diff_options *opt, void *cbdata)
  90{
  91        int i;
  92        struct update_callback_data *data = cbdata;
  93
  94        for (i = 0; i < q->nr; i++) {
  95                struct diff_filepair *p = q->queue[i];
  96                const char *path = p->one->path;
  97                switch (p->status) {
  98                default:
  99                        die("unexpected diff status %c", p->status);
 100                case DIFF_STATUS_UNMERGED:
 101                case DIFF_STATUS_MODIFIED:
 102                case DIFF_STATUS_TYPE_CHANGED:
 103                        if (add_file_to_cache(path, data->flags)) {
 104                                if (!(data->flags & ADD_CACHE_IGNORE_ERRORS))
 105                                        die("updating files failed");
 106                                data->add_errors++;
 107                        }
 108                        break;
 109                case DIFF_STATUS_DELETED:
 110                        if (!(data->flags & ADD_CACHE_PRETEND))
 111                                remove_file_from_cache(path);
 112                        if (data->flags & (ADD_CACHE_PRETEND|ADD_CACHE_VERBOSE))
 113                                printf("remove '%s'\n", path);
 114                        break;
 115                }
 116        }
 117}
 118
 119int add_files_to_cache(const char *prefix, const char **pathspec, int flags)
 120{
 121        struct update_callback_data data;
 122        struct rev_info rev;
 123        init_revisions(&rev, prefix);
 124        setup_revisions(0, NULL, &rev, NULL);
 125        rev.prune_data = pathspec;
 126        rev.diffopt.output_format = DIFF_FORMAT_CALLBACK;
 127        rev.diffopt.format_callback = update_callback;
 128        data.flags = flags;
 129        data.add_errors = 0;
 130        rev.diffopt.format_callback_data = &data;
 131        run_diff_files(&rev, DIFF_RACY_IS_MODIFIED);
 132        return !!data.add_errors;
 133}
 134
 135static void refresh(int verbose, const char **pathspec)
 136{
 137        char *seen;
 138        int i, specs;
 139
 140        for (specs = 0; pathspec[specs];  specs++)
 141                /* nothing */;
 142        seen = xcalloc(specs, 1);
 143        refresh_index(&the_index, verbose ? REFRESH_SAY_CHANGED : REFRESH_QUIET,
 144                      pathspec, seen);
 145        for (i = 0; i < specs; i++) {
 146                if (!seen[i])
 147                        die("pathspec '%s' did not match any files", pathspec[i]);
 148        }
 149        free(seen);
 150}
 151
 152static const char **validate_pathspec(int argc, const char **argv, const char *prefix)
 153{
 154        const char **pathspec = get_pathspec(prefix, argv);
 155
 156        if (pathspec) {
 157                const char **p;
 158                for (p = pathspec; *p; p++) {
 159                        if (has_symlink_leading_path(strlen(*p), *p)) {
 160                                int len = prefix ? strlen(prefix) : 0;
 161                                die("'%s' is beyond a symbolic link", *p + len);
 162                        }
 163                }
 164        }
 165
 166        return pathspec;
 167}
 168
 169int interactive_add(int argc, const char **argv, const char *prefix)
 170{
 171        int status, ac;
 172        const char **args;
 173        const char **pathspec = NULL;
 174
 175        if (argc) {
 176                pathspec = validate_pathspec(argc, argv, prefix);
 177                if (!pathspec)
 178                        return -1;
 179        }
 180
 181        args = xcalloc(sizeof(const char *), (argc + 4));
 182        ac = 0;
 183        args[ac++] = "add--interactive";
 184        if (patch_interactive)
 185                args[ac++] = "--patch";
 186        args[ac++] = "--";
 187        if (argc) {
 188                memcpy(&(args[ac]), pathspec, sizeof(const char *) * argc);
 189                ac += argc;
 190        }
 191        args[ac] = NULL;
 192
 193        status = run_command_v_opt(args, RUN_GIT_CMD);
 194        free(args);
 195        return status;
 196}
 197
 198static struct lock_file lock_file;
 199
 200static const char ignore_error[] =
 201"The following paths are ignored by one of your .gitignore files:\n";
 202
 203static int verbose = 0, show_only = 0, ignored_too = 0, refresh_only = 0;
 204static int ignore_add_errors, addremove;
 205
 206static struct option builtin_add_options[] = {
 207        OPT__DRY_RUN(&show_only),
 208        OPT__VERBOSE(&verbose),
 209        OPT_GROUP(""),
 210        OPT_BOOLEAN('i', "interactive", &add_interactive, "interactive picking"),
 211        OPT_BOOLEAN('p', "patch", &patch_interactive, "interactive patching"),
 212        OPT_BOOLEAN('f', "force", &ignored_too, "allow adding otherwise ignored files"),
 213        OPT_BOOLEAN('u', "update", &take_worktree_changes, "update tracked files"),
 214        OPT_BOOLEAN('A', "all", &addremove, "add all, noticing removal of tracked files"),
 215        OPT_BOOLEAN( 0 , "refresh", &refresh_only, "don't add, only refresh the index"),
 216        OPT_BOOLEAN( 0 , "ignore-errors", &ignore_add_errors, "just skip files which cannot be added because of errors"),
 217        OPT_END(),
 218};
 219
 220static int add_config(const char *var, const char *value, void *cb)
 221{
 222        if (!strcasecmp(var, "add.ignore-errors")) {
 223                ignore_add_errors = git_config_bool(var, value);
 224                return 0;
 225        }
 226        return git_default_config(var, value, cb);
 227}
 228
 229static int add_files(struct dir_struct *dir, int flags)
 230{
 231        int i, exit_status = 0;
 232
 233        if (dir->ignored_nr) {
 234                fprintf(stderr, ignore_error);
 235                for (i = 0; i < dir->ignored_nr; i++)
 236                        fprintf(stderr, "%s\n", dir->ignored[i]->name);
 237                fprintf(stderr, "Use -f if you really want to add them.\n");
 238                die("no files added");
 239        }
 240
 241        for (i = 0; i < dir->nr; i++)
 242                if (add_file_to_cache(dir->entries[i]->name, flags)) {
 243                        if (!ignore_add_errors)
 244                                die("adding files failed");
 245                        exit_status = 1;
 246                }
 247        return exit_status;
 248}
 249
 250int cmd_add(int argc, const char **argv, const char *prefix)
 251{
 252        int exit_status = 0;
 253        int newfd;
 254        const char **pathspec;
 255        struct dir_struct dir;
 256        int flags;
 257        int add_new_files;
 258        int require_pathspec;
 259
 260        argc = parse_options(argc, argv, builtin_add_options,
 261                          builtin_add_usage, 0);
 262        if (patch_interactive)
 263                add_interactive = 1;
 264        if (add_interactive)
 265                exit(interactive_add(argc, argv, prefix));
 266
 267        git_config(add_config, NULL);
 268
 269        if (addremove && take_worktree_changes)
 270                die("-A and -u are mutually incompatible");
 271        if (addremove && !argc) {
 272                static const char *here[2] = { ".", NULL };
 273                argc = 1;
 274                argv = here;
 275        }
 276
 277        add_new_files = !take_worktree_changes && !refresh_only;
 278        require_pathspec = !take_worktree_changes;
 279
 280        newfd = hold_locked_index(&lock_file, 1);
 281
 282        flags = ((verbose ? ADD_CACHE_VERBOSE : 0) |
 283                 (show_only ? ADD_CACHE_PRETEND : 0) |
 284                 (ignore_add_errors ? ADD_CACHE_IGNORE_ERRORS : 0));
 285
 286        if (require_pathspec && argc == 0) {
 287                fprintf(stderr, "Nothing specified, nothing added.\n");
 288                fprintf(stderr, "Maybe you wanted to say 'git add .'?\n");
 289                return 0;
 290        }
 291        pathspec = validate_pathspec(argc, argv, prefix);
 292
 293        /*
 294         * If we are adding new files, we need to scan the working
 295         * tree to find the ones that match pathspecs; this needs
 296         * to be done before we read the index.
 297         */
 298        if (add_new_files)
 299                fill_directory(&dir, pathspec, ignored_too);
 300
 301        if (read_cache() < 0)
 302                die("index file corrupt");
 303
 304        if (refresh_only) {
 305                refresh(verbose, pathspec);
 306                goto finish;
 307        }
 308
 309        if (take_worktree_changes || addremove)
 310                exit_status |= add_files_to_cache(prefix, pathspec, flags);
 311
 312        if (add_new_files)
 313                exit_status |= add_files(&dir, flags);
 314
 315 finish:
 316        if (active_cache_changed) {
 317                if (write_cache(newfd, active_cache, active_nr) ||
 318                    commit_locked_index(&lock_file))
 319                        die("Unable to write new index file");
 320        }
 321
 322        return exit_status;
 323}