builtin-add.con commit Make git-mv a builtin (11be42a)
   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 "cache-tree.h"
  12
  13static const char builtin_add_usage[] =
  14"git-add [-n] [-v] <filepattern>...";
  15
  16static void prune_directory(struct dir_struct *dir, const char **pathspec, int prefix)
  17{
  18        char *seen;
  19        int i, specs;
  20        struct dir_entry **src, **dst;
  21
  22        for (specs = 0; pathspec[specs];  specs++)
  23                /* nothing */;
  24        seen = xcalloc(specs, 1);
  25
  26        src = dst = dir->entries;
  27        i = dir->nr;
  28        while (--i >= 0) {
  29                struct dir_entry *entry = *src++;
  30                if (!match_pathspec(pathspec, entry->name, entry->len, prefix, seen)) {
  31                        free(entry);
  32                        continue;
  33                }
  34                *dst++ = entry;
  35        }
  36        dir->nr = dst - dir->entries;
  37
  38        for (i = 0; i < specs; i++) {
  39                struct stat st;
  40                const char *match;
  41                if (seen[i])
  42                        continue;
  43
  44                /* Existing file? We must have ignored it */
  45                match = pathspec[i];
  46                if (!match[0] || !lstat(match, &st))
  47                        continue;
  48                die("pathspec '%s' did not match any files", match);
  49        }
  50}
  51
  52static void fill_directory(struct dir_struct *dir, const char **pathspec)
  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        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
  64        /*
  65         * Calculate common prefix for the pathspec, and
  66         * use that to optimize the directory walk
  67         */
  68        baselen = common_prefix(pathspec);
  69        path = ".";
  70        base = "";
  71        if (baselen) {
  72                char *common = xmalloc(baselen + 1);
  73                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, char **envp)
  88{
  89        int i, newfd;
  90        int verbose = 0, show_only = 0;
  91        const char *prefix = setup_git_directory();
  92        const char **pathspec;
  93        struct dir_struct dir;
  94
  95        git_config(git_default_config);
  96
  97        newfd = hold_lock_file_for_update(&lock_file, get_index_file());
  98        if (newfd < 0)
  99                die("unable to create new index file");
 100
 101        if (read_cache() < 0)
 102                die("index file corrupt");
 103
 104        for (i = 1; i < argc; i++) {
 105                const char *arg = argv[i];
 106
 107                if (arg[0] != '-')
 108                        break;
 109                if (!strcmp(arg, "--")) {
 110                        i++;
 111                        break;
 112                }
 113                if (!strcmp(arg, "-n")) {
 114                        show_only = 1;
 115                        continue;
 116                }
 117                if (!strcmp(arg, "-v")) {
 118                        verbose = 1;
 119                        continue;
 120                }
 121                die(builtin_add_usage);
 122        }
 123        pathspec = get_pathspec(prefix, argv + i);
 124
 125        fill_directory(&dir, pathspec);
 126
 127        if (show_only) {
 128                const char *sep = "", *eof = "";
 129                for (i = 0; i < dir.nr; i++) {
 130                        printf("%s%s", sep, dir.entries[i]->name);
 131                        sep = " ";
 132                        eof = "\n";
 133                }
 134                fputs(eof, stdout);
 135                return 0;
 136        }
 137
 138        for (i = 0; i < dir.nr; i++)
 139                add_file_to_index(dir.entries[i]->name, verbose);
 140
 141        if (active_cache_changed) {
 142                if (write_cache(newfd, active_cache, active_nr) ||
 143                    close(newfd) || commit_lock_file(&lock_file))
 144                        die("Unable to write new index file");
 145        }
 146
 147        return 0;
 148}