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