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