builtin-add.con commit git-add: add ignored files when asked explicitly. (e23ca9e)
   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
  12static const char builtin_add_usage[] =
  13"git-add [-n] [-v] [--interactive] [--] <filepattern>...";
  14
  15static void prune_directory(struct dir_struct *dir, const char **pathspec, int prefix)
  16{
  17        char *seen;
  18        int i, specs;
  19        struct dir_entry **src, **dst;
  20
  21        for (specs = 0; pathspec[specs];  specs++)
  22                /* nothing */;
  23        seen = xcalloc(specs, 1);
  24
  25        src = dst = dir->entries;
  26        i = dir->nr;
  27        while (--i >= 0) {
  28                struct dir_entry *entry = *src++;
  29                int how = match_pathspec(pathspec, entry->name, entry->len,
  30                                         prefix, seen);
  31                /*
  32                 * ignored entries can be added with exact match,
  33                 * but not with glob nor recursive.
  34                 */
  35                if (!how ||
  36                    (entry->ignored_entry && how != MATCHED_EXACTLY)) {
  37                        free(entry);
  38                        continue;
  39                }
  40                *dst++ = entry;
  41        }
  42        dir->nr = dst - dir->entries;
  43
  44        for (i = 0; i < specs; i++) {
  45                struct stat st;
  46                const char *match;
  47                if (seen[i])
  48                        continue;
  49
  50                /* Existing file? We must have ignored it */
  51                match = pathspec[i];
  52                if (!match[0] || !lstat(match, &st))
  53                        continue;
  54                die("pathspec '%s' did not match any files", match);
  55        }
  56}
  57
  58static void fill_directory(struct dir_struct *dir, const char **pathspec)
  59{
  60        const char *path, *base;
  61        int baselen;
  62
  63        /* Set up the default git porcelain excludes */
  64        memset(dir, 0, sizeof(*dir));
  65        if (pathspec)
  66                dir->show_both = 1;
  67        dir->exclude_per_dir = ".gitignore";
  68        path = git_path("info/exclude");
  69        if (!access(path, R_OK))
  70                add_excludes_from_file(dir, path);
  71
  72        /*
  73         * Calculate common prefix for the pathspec, and
  74         * use that to optimize the directory walk
  75         */
  76        baselen = common_prefix(pathspec);
  77        path = ".";
  78        base = "";
  79        if (baselen) {
  80                char *common = xmalloc(baselen + 1);
  81                memcpy(common, *pathspec, baselen);
  82                common[baselen] = 0;
  83                path = base = common;
  84        }
  85
  86        /* Read the directory and prune it */
  87        read_directory(dir, path, base, baselen);
  88        if (pathspec)
  89                prune_directory(dir, pathspec, baselen);
  90}
  91
  92static struct lock_file lock_file;
  93
  94int cmd_add(int argc, const char **argv, const char *prefix)
  95{
  96        int i, newfd;
  97        int verbose = 0, show_only = 0;
  98        const char **pathspec;
  99        struct dir_struct dir;
 100        int add_interactive = 0;
 101
 102        for (i = 1; i < argc; i++) {
 103                if (!strcmp("--interactive", argv[i]))
 104                        add_interactive++;
 105        }
 106        if (add_interactive) {
 107                const char *args[] = { "add--interactive", NULL };
 108
 109                if (add_interactive != 1 || argc != 2)
 110                        die("add --interactive does not take any parameters");
 111                execv_git_cmd(args);
 112                exit(1);
 113        }
 114
 115        git_config(git_default_config);
 116
 117        newfd = hold_lock_file_for_update(&lock_file, get_index_file(), 1);
 118
 119        for (i = 1; i < argc; i++) {
 120                const char *arg = argv[i];
 121
 122                if (arg[0] != '-')
 123                        break;
 124                if (!strcmp(arg, "--")) {
 125                        i++;
 126                        break;
 127                }
 128                if (!strcmp(arg, "-n")) {
 129                        show_only = 1;
 130                        continue;
 131                }
 132                if (!strcmp(arg, "-v")) {
 133                        verbose = 1;
 134                        continue;
 135                }
 136                usage(builtin_add_usage);
 137        }
 138        if (argc <= i) {
 139                fprintf(stderr, "Nothing specified, nothing added.\n");
 140                fprintf(stderr, "Maybe you wanted to say 'git add .'?\n");
 141                return 0;
 142        }
 143        pathspec = get_pathspec(prefix, argv + i);
 144
 145        fill_directory(&dir, pathspec);
 146
 147        if (show_only) {
 148                const char *sep = "", *eof = "";
 149                for (i = 0; i < dir.nr; i++) {
 150                        printf("%s%s", sep, dir.entries[i]->name);
 151                        sep = " ";
 152                        eof = "\n";
 153                }
 154                fputs(eof, stdout);
 155                return 0;
 156        }
 157
 158        if (read_cache() < 0)
 159                die("index file corrupt");
 160
 161        for (i = 0; i < dir.nr; i++)
 162                add_file_to_index(dir.entries[i]->name, verbose);
 163
 164        if (active_cache_changed) {
 165                if (write_cache(newfd, active_cache, active_nr) ||
 166                    close(newfd) || commit_lock_file(&lock_file))
 167                        die("Unable to write new index file");
 168        }
 169
 170        return 0;
 171}