builtin-add.con commit test for add with non-existent pathspec (1e7ef74)
   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#include "diff.h"
  14#include "revision.h"
  15
  16static const char * const builtin_add_usage[] = {
  17        "git add [options] [--] <filepattern>...",
  18        NULL
  19};
  20static int patch_interactive, add_interactive, edit_interactive;
  21static int take_worktree_changes;
  22
  23static void fill_pathspec_matches(const char **pathspec, char *seen, int specs)
  24{
  25        int num_unmatched = 0, i;
  26
  27        /*
  28         * Since we are walking the index as if we were walking the directory,
  29         * we have to mark the matched pathspec as seen; otherwise we will
  30         * mistakenly think that the user gave a pathspec that did not match
  31         * anything.
  32         */
  33        for (i = 0; i < specs; i++)
  34                if (!seen[i])
  35                        num_unmatched++;
  36        if (!num_unmatched)
  37                return;
  38        for (i = 0; i < active_nr; i++) {
  39                struct cache_entry *ce = active_cache[i];
  40                match_pathspec(pathspec, ce->name, ce_namelen(ce), 0, seen);
  41        }
  42}
  43
  44static char *find_used_pathspec(const char **pathspec)
  45{
  46        char *seen;
  47        int i;
  48
  49        for (i = 0; pathspec[i];  i++)
  50                ; /* just counting */
  51        seen = xcalloc(i, 1);
  52        fill_pathspec_matches(pathspec, seen, i);
  53        return seen;
  54}
  55
  56static char *prune_directory(struct dir_struct *dir, const char **pathspec, int prefix)
  57{
  58        char *seen;
  59        int i, specs;
  60        struct dir_entry **src, **dst;
  61
  62        for (specs = 0; pathspec[specs];  specs++)
  63                /* nothing */;
  64        seen = xcalloc(specs, 1);
  65
  66        src = dst = dir->entries;
  67        i = dir->nr;
  68        while (--i >= 0) {
  69                struct dir_entry *entry = *src++;
  70                if (match_pathspec(pathspec, entry->name, entry->len,
  71                                   prefix, seen))
  72                        *dst++ = entry;
  73        }
  74        dir->nr = dst - dir->entries;
  75        fill_pathspec_matches(pathspec, seen, specs);
  76        return seen;
  77}
  78
  79static void treat_gitlinks(const char **pathspec)
  80{
  81        int i;
  82
  83        if (!pathspec || !*pathspec)
  84                return;
  85
  86        for (i = 0; i < active_nr; i++) {
  87                struct cache_entry *ce = active_cache[i];
  88                if (S_ISGITLINK(ce->ce_mode)) {
  89                        int len = ce_namelen(ce), j;
  90                        for (j = 0; pathspec[j]; j++) {
  91                                int len2 = strlen(pathspec[j]);
  92                                if (len2 <= len || pathspec[j][len] != '/' ||
  93                                    memcmp(ce->name, pathspec[j], len))
  94                                        continue;
  95                                if (len2 == len + 1)
  96                                        /* strip trailing slash */
  97                                        pathspec[j] = xstrndup(ce->name, len);
  98                                else
  99                                        die ("Path '%s' is in submodule '%.*s'",
 100                                                pathspec[j], len, ce->name);
 101                        }
 102                }
 103        }
 104}
 105
 106static void refresh(int verbose, const char **pathspec)
 107{
 108        char *seen;
 109        int i, specs;
 110
 111        for (specs = 0; pathspec[specs];  specs++)
 112                /* nothing */;
 113        seen = xcalloc(specs, 1);
 114        refresh_index(&the_index, verbose ? REFRESH_IN_PORCELAIN : REFRESH_QUIET,
 115                      pathspec, seen, "Unstaged changes after refreshing the index:");
 116        for (i = 0; i < specs; i++) {
 117                if (!seen[i])
 118                        die("pathspec '%s' did not match any files", pathspec[i]);
 119        }
 120        free(seen);
 121}
 122
 123static const char **validate_pathspec(int argc, const char **argv, const char *prefix)
 124{
 125        const char **pathspec = get_pathspec(prefix, argv);
 126
 127        if (pathspec) {
 128                const char **p;
 129                for (p = pathspec; *p; p++) {
 130                        if (has_symlink_leading_path(*p, strlen(*p))) {
 131                                int len = prefix ? strlen(prefix) : 0;
 132                                die("'%s' is beyond a symbolic link", *p + len);
 133                        }
 134                }
 135        }
 136
 137        return pathspec;
 138}
 139
 140int run_add_interactive(const char *revision, const char *patch_mode,
 141                        const char **pathspec)
 142{
 143        int status, ac, pc = 0;
 144        const char **args;
 145
 146        if (pathspec)
 147                while (pathspec[pc])
 148                        pc++;
 149
 150        args = xcalloc(sizeof(const char *), (pc + 5));
 151        ac = 0;
 152        args[ac++] = "add--interactive";
 153        if (patch_mode)
 154                args[ac++] = patch_mode;
 155        if (revision)
 156                args[ac++] = revision;
 157        args[ac++] = "--";
 158        if (pc) {
 159                memcpy(&(args[ac]), pathspec, sizeof(const char *) * pc);
 160                ac += pc;
 161        }
 162        args[ac] = NULL;
 163
 164        status = run_command_v_opt(args, RUN_GIT_CMD);
 165        free(args);
 166        return status;
 167}
 168
 169int interactive_add(int argc, const char **argv, const char *prefix)
 170{
 171        const char **pathspec = NULL;
 172
 173        if (argc) {
 174                pathspec = validate_pathspec(argc, argv, prefix);
 175                if (!pathspec)
 176                        return -1;
 177        }
 178
 179        return run_add_interactive(NULL,
 180                                   patch_interactive ? "--patch" : NULL,
 181                                   pathspec);
 182}
 183
 184static int edit_patch(int argc, const char **argv, const char *prefix)
 185{
 186        char *file = xstrdup(git_path("ADD_EDIT.patch"));
 187        const char *apply_argv[] = { "apply", "--recount", "--cached",
 188                file, NULL };
 189        struct child_process child;
 190        struct rev_info rev;
 191        int out;
 192        struct stat st;
 193
 194        git_config(git_diff_basic_config, NULL); /* no "diff" UI options */
 195
 196        if (read_cache() < 0)
 197                die ("Could not read the index");
 198
 199        init_revisions(&rev, prefix);
 200        rev.diffopt.context = 7;
 201
 202        argc = setup_revisions(argc, argv, &rev, NULL);
 203        rev.diffopt.output_format = DIFF_FORMAT_PATCH;
 204        out = open(file, O_CREAT | O_WRONLY, 0644);
 205        if (out < 0)
 206                die ("Could not open '%s' for writing.", file);
 207        rev.diffopt.file = xfdopen(out, "w");
 208        rev.diffopt.close_file = 1;
 209        if (run_diff_files(&rev, 0))
 210                die ("Could not write patch");
 211
 212        launch_editor(file, NULL, NULL);
 213
 214        if (stat(file, &st))
 215                die_errno("Could not stat '%s'", file);
 216        if (!st.st_size)
 217                die("Empty patch. Aborted.");
 218
 219        memset(&child, 0, sizeof(child));
 220        child.git_cmd = 1;
 221        child.argv = apply_argv;
 222        if (run_command(&child))
 223                die ("Could not apply '%s'", file);
 224
 225        unlink(file);
 226        return 0;
 227}
 228
 229static struct lock_file lock_file;
 230
 231static const char ignore_error[] =
 232"The following paths are ignored by one of your .gitignore files:\n";
 233
 234static int verbose = 0, show_only = 0, ignored_too = 0, refresh_only = 0;
 235static int ignore_add_errors, addremove, intent_to_add;
 236
 237static struct option builtin_add_options[] = {
 238        OPT__DRY_RUN(&show_only),
 239        OPT__VERBOSE(&verbose),
 240        OPT_GROUP(""),
 241        OPT_BOOLEAN('i', "interactive", &add_interactive, "interactive picking"),
 242        OPT_BOOLEAN('p', "patch", &patch_interactive, "interactive patching"),
 243        OPT_BOOLEAN('e', "edit", &edit_interactive, "edit current diff and apply"),
 244        OPT_BOOLEAN('f', "force", &ignored_too, "allow adding otherwise ignored files"),
 245        OPT_BOOLEAN('u', "update", &take_worktree_changes, "update tracked files"),
 246        OPT_BOOLEAN('N', "intent-to-add", &intent_to_add, "record only the fact that the path will be added later"),
 247        OPT_BOOLEAN('A', "all", &addremove, "add all, noticing removal of tracked files"),
 248        OPT_BOOLEAN( 0 , "refresh", &refresh_only, "don't add, only refresh the index"),
 249        OPT_BOOLEAN( 0 , "ignore-errors", &ignore_add_errors, "just skip files which cannot be added because of errors"),
 250        OPT_END(),
 251};
 252
 253static int add_config(const char *var, const char *value, void *cb)
 254{
 255        if (!strcasecmp(var, "add.ignore-errors")) {
 256                ignore_add_errors = git_config_bool(var, value);
 257                return 0;
 258        }
 259        return git_default_config(var, value, cb);
 260}
 261
 262static int add_files(struct dir_struct *dir, int flags)
 263{
 264        int i, exit_status = 0;
 265
 266        if (dir->ignored_nr) {
 267                fprintf(stderr, ignore_error);
 268                for (i = 0; i < dir->ignored_nr; i++)
 269                        fprintf(stderr, "%s\n", dir->ignored[i]->name);
 270                fprintf(stderr, "Use -f if you really want to add them.\n");
 271                die("no files added");
 272        }
 273
 274        for (i = 0; i < dir->nr; i++)
 275                if (add_file_to_cache(dir->entries[i]->name, flags)) {
 276                        if (!ignore_add_errors)
 277                                die("adding files failed");
 278                        exit_status = 1;
 279                }
 280        return exit_status;
 281}
 282
 283int cmd_add(int argc, const char **argv, const char *prefix)
 284{
 285        int exit_status = 0;
 286        int newfd;
 287        const char **pathspec;
 288        struct dir_struct dir;
 289        int flags;
 290        int add_new_files;
 291        int require_pathspec;
 292        char *seen = NULL;
 293
 294        git_config(add_config, NULL);
 295
 296        argc = parse_options(argc, argv, prefix, builtin_add_options,
 297                          builtin_add_usage, PARSE_OPT_KEEP_ARGV0);
 298        if (patch_interactive)
 299                add_interactive = 1;
 300        if (add_interactive)
 301                exit(interactive_add(argc - 1, argv + 1, prefix));
 302
 303        if (edit_interactive)
 304                return(edit_patch(argc, argv, prefix));
 305        argc--;
 306        argv++;
 307
 308        if (addremove && take_worktree_changes)
 309                die("-A and -u are mutually incompatible");
 310        if ((addremove || take_worktree_changes) && !argc) {
 311                static const char *here[2] = { ".", NULL };
 312                argc = 1;
 313                argv = here;
 314        }
 315
 316        add_new_files = !take_worktree_changes && !refresh_only;
 317        require_pathspec = !take_worktree_changes;
 318
 319        newfd = hold_locked_index(&lock_file, 1);
 320
 321        flags = ((verbose ? ADD_CACHE_VERBOSE : 0) |
 322                 (show_only ? ADD_CACHE_PRETEND : 0) |
 323                 (intent_to_add ? ADD_CACHE_INTENT : 0) |
 324                 (ignore_add_errors ? ADD_CACHE_IGNORE_ERRORS : 0) |
 325                 (!(addremove || take_worktree_changes)
 326                  ? ADD_CACHE_IGNORE_REMOVAL : 0));
 327
 328        if (require_pathspec && argc == 0) {
 329                fprintf(stderr, "Nothing specified, nothing added.\n");
 330                fprintf(stderr, "Maybe you wanted to say 'git add .'?\n");
 331                return 0;
 332        }
 333        pathspec = validate_pathspec(argc, argv, prefix);
 334
 335        if (read_cache() < 0)
 336                die("index file corrupt");
 337        treat_gitlinks(pathspec);
 338
 339        if (add_new_files) {
 340                int baselen;
 341
 342                /* Set up the default git porcelain excludes */
 343                memset(&dir, 0, sizeof(dir));
 344                if (!ignored_too) {
 345                        dir.flags |= DIR_COLLECT_IGNORED;
 346                        setup_standard_excludes(&dir);
 347                }
 348
 349                /* This picks up the paths that are not tracked */
 350                baselen = fill_directory(&dir, pathspec);
 351                if (pathspec)
 352                        seen = prune_directory(&dir, pathspec, baselen);
 353        }
 354
 355        if (refresh_only) {
 356                refresh(verbose, pathspec);
 357                goto finish;
 358        }
 359
 360        if (pathspec) {
 361                int i;
 362                if (!seen)
 363                        seen = find_used_pathspec(pathspec);
 364                for (i = 0; pathspec[i]; i++) {
 365                        if (!seen[i] && pathspec[i][0]
 366                            && !file_exists(pathspec[i]))
 367                                die("pathspec '%s' did not match any files",
 368                                    pathspec[i]);
 369                }
 370                free(seen);
 371        }
 372
 373        exit_status |= add_files_to_cache(prefix, pathspec, flags);
 374
 375        if (add_new_files)
 376                exit_status |= add_files(&dir, flags);
 377
 378 finish:
 379        if (active_cache_changed) {
 380                if (write_cache(newfd, active_cache, active_nr) ||
 381                    commit_locked_index(&lock_file))
 382                        die("Unable to write new index file");
 383        }
 384
 385        return exit_status;
 386}