builtin-add.con commit Improved hint on how to set identity (180787c)
   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
  16static const char builtin_add_usage[] =
  17"git-add [-n] [-v] [-f] [--interactive | -i] [-u] [--] <filepattern>...";
  18
  19static int take_worktree_changes;
  20static const char *excludes_file;
  21
  22static void prune_directory(struct dir_struct *dir, const char **pathspec, int prefix)
  23{
  24        char *seen;
  25        int i, specs;
  26        struct dir_entry **src, **dst;
  27
  28        for (specs = 0; pathspec[specs];  specs++)
  29                /* nothing */;
  30        seen = xcalloc(specs, 1);
  31
  32        src = dst = dir->entries;
  33        i = dir->nr;
  34        while (--i >= 0) {
  35                struct dir_entry *entry = *src++;
  36                if (match_pathspec(pathspec, entry->name, entry->len,
  37                                   prefix, seen))
  38                        *dst++ = entry;
  39        }
  40        dir->nr = dst - dir->entries;
  41
  42        for (i = 0; i < specs; i++) {
  43                if (!seen[i] && !file_exists(pathspec[i]))
  44                        die("pathspec '%s' did not match any files",
  45                                        pathspec[i]);
  46        }
  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                dir->exclude_per_dir = ".gitignore";
  60                path = git_path("info/exclude");
  61                if (!access(path, R_OK))
  62                        add_excludes_from_file(dir, path);
  63                if (excludes_file != NULL && !access(excludes_file, R_OK))
  64                        add_excludes_from_file(dir, excludes_file);
  65        }
  66
  67        /*
  68         * Calculate common prefix for the pathspec, and
  69         * use that to optimize the directory walk
  70         */
  71        baselen = common_prefix(pathspec);
  72        path = ".";
  73        base = "";
  74        if (baselen) {
  75                char *common = xmalloc(baselen + 1);
  76                memcpy(common, *pathspec, baselen);
  77                common[baselen] = 0;
  78                path = base = common;
  79        }
  80
  81        /* Read the directory and prune it */
  82        read_directory(dir, path, base, baselen, pathspec);
  83        if (pathspec)
  84                prune_directory(dir, pathspec, baselen);
  85}
  86
  87static void update_callback(struct diff_queue_struct *q,
  88                            struct diff_options *opt, void *cbdata)
  89{
  90        int i, verbose;
  91
  92        verbose = *((int *)cbdata);
  93        for (i = 0; i < q->nr; i++) {
  94                struct diff_filepair *p = q->queue[i];
  95                const char *path = p->one->path;
  96                switch (p->status) {
  97                default:
  98                        die("unexpacted diff status %c", p->status);
  99                case DIFF_STATUS_UNMERGED:
 100                case DIFF_STATUS_MODIFIED:
 101                        add_file_to_cache(path, verbose);
 102                        break;
 103                case DIFF_STATUS_DELETED:
 104                        remove_file_from_cache(path);
 105                        if (verbose)
 106                                printf("remove '%s'\n", path);
 107                        break;
 108                }
 109        }
 110}
 111
 112static void update(int verbose, const char **files)
 113{
 114        struct rev_info rev;
 115        init_revisions(&rev, "");
 116        setup_revisions(0, NULL, &rev, NULL);
 117        rev.prune_data = get_pathspec(rev.prefix, files);
 118        rev.diffopt.output_format = DIFF_FORMAT_CALLBACK;
 119        rev.diffopt.format_callback = update_callback;
 120        rev.diffopt.format_callback_data = &verbose;
 121        if (read_cache() < 0)
 122                die("index file corrupt");
 123        run_diff_files(&rev, 0);
 124}
 125
 126static void refresh(int verbose, const char **pathspec)
 127{
 128        char *seen;
 129        int i, specs;
 130
 131        for (specs = 0; pathspec[specs];  specs++)
 132                /* nothing */;
 133        seen = xcalloc(specs, 1);
 134        if (read_cache() < 0)
 135                die("index file corrupt");
 136        refresh_index(&the_index, verbose ? 0 : REFRESH_QUIET, pathspec, seen);
 137        for (i = 0; i < specs; i++) {
 138                if (!seen[i])
 139                        die("pathspec '%s' did not match any files", pathspec[i]);
 140        }
 141}
 142
 143static int git_add_config(const char *var, const char *value)
 144{
 145        if (!strcmp(var, "core.excludesfile")) {
 146                if (!value)
 147                        die("core.excludesfile without value");
 148                excludes_file = xstrdup(value);
 149                return 0;
 150        }
 151
 152        return git_default_config(var, value);
 153}
 154
 155static struct lock_file lock_file;
 156
 157static const char ignore_warning[] =
 158"The following paths are ignored by one of your .gitignore files:\n";
 159
 160int cmd_add(int argc, const char **argv, const char *prefix)
 161{
 162        int i, newfd;
 163        int verbose = 0, show_only = 0, ignored_too = 0, refresh_only = 0;
 164        const char **pathspec;
 165        struct dir_struct dir;
 166        int add_interactive = 0;
 167
 168        for (i = 1; i < argc; i++) {
 169                if (!strcmp("--interactive", argv[i]) ||
 170                    !strcmp("-i", argv[i]))
 171                        add_interactive++;
 172        }
 173        if (add_interactive) {
 174                const char *args[] = { "add--interactive", NULL };
 175
 176                if (add_interactive != 1 || argc != 2)
 177                        die("add --interactive does not take any parameters");
 178                execv_git_cmd(args);
 179                exit(1);
 180        }
 181
 182        git_config(git_add_config);
 183
 184        newfd = hold_locked_index(&lock_file, 1);
 185
 186        for (i = 1; i < argc; i++) {
 187                const char *arg = argv[i];
 188
 189                if (arg[0] != '-')
 190                        break;
 191                if (!strcmp(arg, "--")) {
 192                        i++;
 193                        break;
 194                }
 195                if (!strcmp(arg, "-n")) {
 196                        show_only = 1;
 197                        continue;
 198                }
 199                if (!strcmp(arg, "-f")) {
 200                        ignored_too = 1;
 201                        continue;
 202                }
 203                if (!strcmp(arg, "-v")) {
 204                        verbose = 1;
 205                        continue;
 206                }
 207                if (!strcmp(arg, "-u")) {
 208                        take_worktree_changes = 1;
 209                        continue;
 210                }
 211                if (!strcmp(arg, "--refresh")) {
 212                        refresh_only = 1;
 213                        continue;
 214                }
 215                usage(builtin_add_usage);
 216        }
 217
 218        if (take_worktree_changes) {
 219                update(verbose, argv + i);
 220                goto finish;
 221        }
 222
 223        if (argc <= i) {
 224                fprintf(stderr, "Nothing specified, nothing added.\n");
 225                fprintf(stderr, "Maybe you wanted to say 'git add .'?\n");
 226                return 0;
 227        }
 228        pathspec = get_pathspec(prefix, argv + i);
 229
 230        if (refresh_only) {
 231                refresh(verbose, pathspec);
 232                goto finish;
 233        }
 234
 235        fill_directory(&dir, pathspec, ignored_too);
 236
 237        if (show_only) {
 238                const char *sep = "", *eof = "";
 239                for (i = 0; i < dir.nr; i++) {
 240                        printf("%s%s", sep, dir.entries[i]->name);
 241                        sep = " ";
 242                        eof = "\n";
 243                }
 244                fputs(eof, stdout);
 245                return 0;
 246        }
 247
 248        if (read_cache() < 0)
 249                die("index file corrupt");
 250
 251        if (dir.ignored_nr) {
 252                fprintf(stderr, ignore_warning);
 253                for (i = 0; i < dir.ignored_nr; i++) {
 254                        fprintf(stderr, "%s\n", dir.ignored[i]->name);
 255                }
 256                fprintf(stderr, "Use -f if you really want to add them.\n");
 257                exit(1);
 258        }
 259
 260        for (i = 0; i < dir.nr; i++)
 261                add_file_to_cache(dir.entries[i]->name, verbose);
 262
 263 finish:
 264        if (active_cache_changed) {
 265                if (write_cache(newfd, active_cache, active_nr) ||
 266                    close(newfd) || commit_locked_index(&lock_file))
 267                        die("Unable to write new index file");
 268        }
 269
 270        return 0;
 271}