builtin-add.con commit git-svn: don't escape tilde ('~') for http(s) URLs (163f368)
   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 fill_pathspec_matches(const char **pathspec, char *seen, int specs)
  22{
  23        int num_unmatched = 0, i;
  24
  25        /*
  26         * Since we are walking the index as if we are warlking the directory,
  27         * we have to mark the matched pathspec as seen; otherwise we will
  28         * mistakenly think that the user gave a pathspec that did not match
  29         * anything.
  30         */
  31        for (i = 0; i < specs; i++)
  32                if (!seen[i])
  33                        num_unmatched++;
  34        if (!num_unmatched)
  35                return;
  36        for (i = 0; i < active_nr; i++) {
  37                struct cache_entry *ce = active_cache[i];
  38                match_pathspec(pathspec, ce->name, ce_namelen(ce), 0, seen);
  39        }
  40}
  41
  42static void prune_directory(struct dir_struct *dir, const char **pathspec, int prefix)
  43{
  44        char *seen;
  45        int i, specs;
  46        struct dir_entry **src, **dst;
  47
  48        for (specs = 0; pathspec[specs];  specs++)
  49                /* nothing */;
  50        seen = xcalloc(specs, 1);
  51
  52        src = dst = dir->entries;
  53        i = dir->nr;
  54        while (--i >= 0) {
  55                struct dir_entry *entry = *src++;
  56                if (match_pathspec(pathspec, entry->name, entry->len,
  57                                   prefix, seen))
  58                        *dst++ = entry;
  59        }
  60        dir->nr = dst - dir->entries;
  61        fill_pathspec_matches(pathspec, seen, specs);
  62
  63        for (i = 0; i < specs; i++) {
  64                if (!seen[i] && !file_exists(pathspec[i]))
  65                        die("pathspec '%s' did not match any files",
  66                                        pathspec[i]);
  67        }
  68        free(seen);
  69}
  70
  71static void fill_directory(struct dir_struct *dir, const char **pathspec,
  72                int ignored_too)
  73{
  74        const char *path, *base;
  75        int baselen;
  76
  77        /* Set up the default git porcelain excludes */
  78        memset(dir, 0, sizeof(*dir));
  79        if (!ignored_too) {
  80                dir->collect_ignored = 1;
  81                setup_standard_excludes(dir);
  82        }
  83
  84        /*
  85         * Calculate common prefix for the pathspec, and
  86         * use that to optimize the directory walk
  87         */
  88        baselen = common_prefix(pathspec);
  89        path = ".";
  90        base = "";
  91        if (baselen)
  92                path = base = xmemdupz(*pathspec, baselen);
  93
  94        /* Read the directory and prune it */
  95        read_directory(dir, path, base, baselen, pathspec);
  96        if (pathspec)
  97                prune_directory(dir, pathspec, baselen);
  98}
  99
 100static void refresh(int verbose, const char **pathspec)
 101{
 102        char *seen;
 103        int i, specs;
 104
 105        for (specs = 0; pathspec[specs];  specs++)
 106                /* nothing */;
 107        seen = xcalloc(specs, 1);
 108        refresh_index(&the_index, verbose ? REFRESH_SAY_CHANGED : REFRESH_QUIET,
 109                      pathspec, seen);
 110        for (i = 0; i < specs; i++) {
 111                if (!seen[i])
 112                        die("pathspec '%s' did not match any files", pathspec[i]);
 113        }
 114        free(seen);
 115}
 116
 117static const char **validate_pathspec(int argc, const char **argv, const char *prefix)
 118{
 119        const char **pathspec = get_pathspec(prefix, argv);
 120
 121        if (pathspec) {
 122                const char **p;
 123                for (p = pathspec; *p; p++) {
 124                        if (has_symlink_leading_path(strlen(*p), *p)) {
 125                                int len = prefix ? strlen(prefix) : 0;
 126                                die("'%s' is beyond a symbolic link", *p + len);
 127                        }
 128                }
 129        }
 130
 131        return pathspec;
 132}
 133
 134int interactive_add(int argc, const char **argv, const char *prefix)
 135{
 136        int status, ac;
 137        const char **args;
 138        const char **pathspec = NULL;
 139
 140        if (argc) {
 141                pathspec = validate_pathspec(argc, argv, prefix);
 142                if (!pathspec)
 143                        return -1;
 144        }
 145
 146        args = xcalloc(sizeof(const char *), (argc + 4));
 147        ac = 0;
 148        args[ac++] = "add--interactive";
 149        if (patch_interactive)
 150                args[ac++] = "--patch";
 151        args[ac++] = "--";
 152        if (argc) {
 153                memcpy(&(args[ac]), pathspec, sizeof(const char *) * argc);
 154                ac += argc;
 155        }
 156        args[ac] = NULL;
 157
 158        status = run_command_v_opt(args, RUN_GIT_CMD);
 159        free(args);
 160        return status;
 161}
 162
 163static struct lock_file lock_file;
 164
 165static const char ignore_error[] =
 166"The following paths are ignored by one of your .gitignore files:\n";
 167
 168static int verbose = 0, show_only = 0, ignored_too = 0, refresh_only = 0;
 169static int ignore_add_errors, addremove, intent_to_add;
 170
 171static struct option builtin_add_options[] = {
 172        OPT__DRY_RUN(&show_only),
 173        OPT__VERBOSE(&verbose),
 174        OPT_GROUP(""),
 175        OPT_BOOLEAN('i', "interactive", &add_interactive, "interactive picking"),
 176        OPT_BOOLEAN('p', "patch", &patch_interactive, "interactive patching"),
 177        OPT_BOOLEAN('f', "force", &ignored_too, "allow adding otherwise ignored files"),
 178        OPT_BOOLEAN('u', "update", &take_worktree_changes, "update tracked files"),
 179        OPT_BOOLEAN('N', "intent-to-add", &intent_to_add, "record only the fact that the path will be added later"),
 180        OPT_BOOLEAN('A', "all", &addremove, "add all, noticing removal of tracked files"),
 181        OPT_BOOLEAN( 0 , "refresh", &refresh_only, "don't add, only refresh the index"),
 182        OPT_BOOLEAN( 0 , "ignore-errors", &ignore_add_errors, "just skip files which cannot be added because of errors"),
 183        OPT_END(),
 184};
 185
 186static int add_config(const char *var, const char *value, void *cb)
 187{
 188        if (!strcasecmp(var, "add.ignore-errors")) {
 189                ignore_add_errors = git_config_bool(var, value);
 190                return 0;
 191        }
 192        return git_default_config(var, value, cb);
 193}
 194
 195static int add_files(struct dir_struct *dir, int flags)
 196{
 197        int i, exit_status = 0;
 198
 199        if (dir->ignored_nr) {
 200                fprintf(stderr, ignore_error);
 201                for (i = 0; i < dir->ignored_nr; i++)
 202                        fprintf(stderr, "%s\n", dir->ignored[i]->name);
 203                fprintf(stderr, "Use -f if you really want to add them.\n");
 204                die("no files added");
 205        }
 206
 207        for (i = 0; i < dir->nr; i++)
 208                if (add_file_to_cache(dir->entries[i]->name, flags)) {
 209                        if (!ignore_add_errors)
 210                                die("adding files failed");
 211                        exit_status = 1;
 212                }
 213        return exit_status;
 214}
 215
 216int cmd_add(int argc, const char **argv, const char *prefix)
 217{
 218        int exit_status = 0;
 219        int newfd;
 220        const char **pathspec;
 221        struct dir_struct dir;
 222        int flags;
 223        int add_new_files;
 224        int require_pathspec;
 225
 226        argc = parse_options(argc, argv, builtin_add_options,
 227                          builtin_add_usage, 0);
 228        if (patch_interactive)
 229                add_interactive = 1;
 230        if (add_interactive)
 231                exit(interactive_add(argc, argv, prefix));
 232
 233        git_config(add_config, NULL);
 234
 235        if (addremove && take_worktree_changes)
 236                die("-A and -u are mutually incompatible");
 237        if ((addremove || take_worktree_changes) && !argc) {
 238                static const char *here[2] = { ".", NULL };
 239                argc = 1;
 240                argv = here;
 241        }
 242
 243        add_new_files = !take_worktree_changes && !refresh_only;
 244        require_pathspec = !take_worktree_changes;
 245
 246        newfd = hold_locked_index(&lock_file, 1);
 247
 248        flags = ((verbose ? ADD_CACHE_VERBOSE : 0) |
 249                 (show_only ? ADD_CACHE_PRETEND : 0) |
 250                 (intent_to_add ? ADD_CACHE_INTENT : 0) |
 251                 (ignore_add_errors ? ADD_CACHE_IGNORE_ERRORS : 0) |
 252                 (!(addremove || take_worktree_changes)
 253                  ? ADD_CACHE_IGNORE_REMOVAL : 0));
 254
 255        if (require_pathspec && argc == 0) {
 256                fprintf(stderr, "Nothing specified, nothing added.\n");
 257                fprintf(stderr, "Maybe you wanted to say 'git add .'?\n");
 258                return 0;
 259        }
 260        pathspec = validate_pathspec(argc, argv, prefix);
 261
 262        if (read_cache() < 0)
 263                die("index file corrupt");
 264
 265        if (add_new_files)
 266                /* This picks up the paths that are not tracked */
 267                fill_directory(&dir, pathspec, ignored_too);
 268
 269        if (refresh_only) {
 270                refresh(verbose, pathspec);
 271                goto finish;
 272        }
 273
 274        exit_status |= add_files_to_cache(prefix, pathspec, flags);
 275
 276        if (add_new_files)
 277                exit_status |= add_files(&dir, flags);
 278
 279 finish:
 280        if (active_cache_changed) {
 281                if (write_cache(newfd, active_cache, active_nr) ||
 282                    commit_locked_index(&lock_file))
 283                        die("Unable to write new index file");
 284        }
 285
 286        return exit_status;
 287}