builtin-add.con commit Merge branch 'maint' to catch up with 1.5.3.5 (afc05f9)
   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] [--refresh] [--] <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        free(seen);
  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
 110static void update(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        if (read_cache() < 0)
 120                die("index file corrupt");
 121        run_diff_files(&rev, 0);
 122}
 123
 124static void refresh(int verbose, const char **pathspec)
 125{
 126        char *seen;
 127        int i, specs;
 128
 129        for (specs = 0; pathspec[specs];  specs++)
 130                /* nothing */;
 131        seen = xcalloc(specs, 1);
 132        if (read_cache() < 0)
 133                die("index file corrupt");
 134        refresh_index(&the_index, verbose ? 0 : REFRESH_QUIET, pathspec, seen);
 135        for (i = 0; i < specs; i++) {
 136                if (!seen[i])
 137                        die("pathspec '%s' did not match any files", pathspec[i]);
 138        }
 139        free(seen);
 140}
 141
 142static int git_add_config(const char *var, const char *value)
 143{
 144        if (!strcmp(var, "core.excludesfile")) {
 145                if (!value)
 146                        die("core.excludesfile without value");
 147                excludes_file = xstrdup(value);
 148                return 0;
 149        }
 150
 151        return git_default_config(var, value);
 152}
 153
 154static struct lock_file lock_file;
 155
 156static const char ignore_error[] =
 157"The following paths are ignored by one of your .gitignore files:\n";
 158
 159int cmd_add(int argc, const char **argv, const char *prefix)
 160{
 161        int i, newfd;
 162        int verbose = 0, show_only = 0, ignored_too = 0, refresh_only = 0;
 163        const char **pathspec;
 164        struct dir_struct dir;
 165        int add_interactive = 0;
 166
 167        for (i = 1; i < argc; i++) {
 168                if (!strcmp("--interactive", argv[i]) ||
 169                    !strcmp("-i", argv[i]))
 170                        add_interactive++;
 171        }
 172        if (add_interactive) {
 173                const char *args[] = { "add--interactive", NULL };
 174
 175                if (add_interactive != 1 || argc != 2)
 176                        die("add --interactive does not take any parameters");
 177                execv_git_cmd(args);
 178                exit(1);
 179        }
 180
 181        git_config(git_add_config);
 182
 183        newfd = hold_locked_index(&lock_file, 1);
 184
 185        for (i = 1; i < argc; i++) {
 186                const char *arg = argv[i];
 187
 188                if (arg[0] != '-')
 189                        break;
 190                if (!strcmp(arg, "--")) {
 191                        i++;
 192                        break;
 193                }
 194                if (!strcmp(arg, "-n")) {
 195                        show_only = 1;
 196                        continue;
 197                }
 198                if (!strcmp(arg, "-f")) {
 199                        ignored_too = 1;
 200                        continue;
 201                }
 202                if (!strcmp(arg, "-v")) {
 203                        verbose = 1;
 204                        continue;
 205                }
 206                if (!strcmp(arg, "-u")) {
 207                        take_worktree_changes = 1;
 208                        continue;
 209                }
 210                if (!strcmp(arg, "--refresh")) {
 211                        refresh_only = 1;
 212                        continue;
 213                }
 214                usage(builtin_add_usage);
 215        }
 216
 217        if (take_worktree_changes) {
 218                update(verbose, prefix, argv + i);
 219                goto finish;
 220        }
 221
 222        if (argc <= i) {
 223                fprintf(stderr, "Nothing specified, nothing added.\n");
 224                fprintf(stderr, "Maybe you wanted to say 'git add .'?\n");
 225                return 0;
 226        }
 227        pathspec = get_pathspec(prefix, argv + i);
 228
 229        if (refresh_only) {
 230                refresh(verbose, pathspec);
 231                goto finish;
 232        }
 233
 234        fill_directory(&dir, pathspec, ignored_too);
 235
 236        if (show_only) {
 237                const char *sep = "", *eof = "";
 238                for (i = 0; i < dir.nr; i++) {
 239                        printf("%s%s", sep, dir.entries[i]->name);
 240                        sep = " ";
 241                        eof = "\n";
 242                }
 243                fputs(eof, stdout);
 244                return 0;
 245        }
 246
 247        if (read_cache() < 0)
 248                die("index file corrupt");
 249
 250        if (dir.ignored_nr) {
 251                fprintf(stderr, ignore_error);
 252                for (i = 0; i < dir.ignored_nr; i++) {
 253                        fprintf(stderr, "%s\n", dir.ignored[i]->name);
 254                }
 255                fprintf(stderr, "Use -f if you really want to add them.\n");
 256                die("no files added");
 257        }
 258
 259        for (i = 0; i < dir.nr; i++)
 260                add_file_to_cache(dir.entries[i]->name, verbose);
 261
 262 finish:
 263        if (active_cache_changed) {
 264                if (write_cache(newfd, active_cache, active_nr) ||
 265                    close(newfd) || commit_locked_index(&lock_file))
 266                        die("Unable to write new index file");
 267        }
 268
 269        return 0;
 270}