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