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