builtin-add.con commit Fix random segfaults in pack-objects. (f5b1b5a)
   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
  12static const char builtin_add_usage[] =
  13"git-add [-n] [-v] [-f] [--interactive] [--] <filepattern>...";
  14
  15static void prune_directory(struct dir_struct *dir, const char **pathspec, int prefix)
  16{
  17        char *seen;
  18        int i, specs;
  19        struct dir_entry **src, **dst;
  20
  21        for (specs = 0; pathspec[specs];  specs++)
  22                /* nothing */;
  23        seen = xcalloc(specs, 1);
  24
  25        src = dst = dir->entries;
  26        i = dir->nr;
  27        while (--i >= 0) {
  28                struct dir_entry *entry = *src++;
  29                if (match_pathspec(pathspec, entry->name, entry->len,
  30                                   prefix, seen))
  31                        *dst++ = entry;
  32        }
  33        dir->nr = dst - dir->entries;
  34
  35        for (i = 0; i < specs; i++) {
  36                struct stat st;
  37                const char *match;
  38                if (seen[i])
  39                        continue;
  40
  41                match = pathspec[i];
  42                if (!match[0])
  43                        continue;
  44
  45                /* Existing file? We must have ignored it */
  46                if (!lstat(match, &st)) {
  47                        struct dir_entry *ent;
  48
  49                        ent = dir_add_name(dir, match, strlen(match));
  50                        ent->ignored = 1;
  51                        if (S_ISDIR(st.st_mode))
  52                                ent->ignored_dir = 1;
  53                        continue;
  54                }
  55                die("pathspec '%s' did not match any files", match);
  56        }
  57}
  58
  59static void fill_directory(struct dir_struct *dir, const char **pathspec)
  60{
  61        const char *path, *base;
  62        int baselen;
  63
  64        /* Set up the default git porcelain excludes */
  65        memset(dir, 0, sizeof(*dir));
  66        dir->exclude_per_dir = ".gitignore";
  67        path = git_path("info/exclude");
  68        if (!access(path, R_OK))
  69                add_excludes_from_file(dir, path);
  70
  71        /*
  72         * Calculate common prefix for the pathspec, and
  73         * use that to optimize the directory walk
  74         */
  75        baselen = common_prefix(pathspec);
  76        path = ".";
  77        base = "";
  78        if (baselen) {
  79                char *common = xmalloc(baselen + 1);
  80                memcpy(common, *pathspec, baselen);
  81                common[baselen] = 0;
  82                path = base = common;
  83        }
  84
  85        /* Read the directory and prune it */
  86        read_directory(dir, path, base, baselen);
  87        if (pathspec)
  88                prune_directory(dir, pathspec, baselen);
  89}
  90
  91static struct lock_file lock_file;
  92
  93static const char ignore_warning[] =
  94"The following paths are ignored by one of your .gitignore files:\n";
  95
  96int cmd_add(int argc, const char **argv, const char *prefix)
  97{
  98        int i, newfd;
  99        int verbose = 0, show_only = 0, ignored_too = 0;
 100        const char **pathspec;
 101        struct dir_struct dir;
 102        int add_interactive = 0;
 103
 104        for (i = 1; i < argc; i++) {
 105                if (!strcmp("--interactive", argv[i]))
 106                        add_interactive++;
 107        }
 108        if (add_interactive) {
 109                const char *args[] = { "add--interactive", NULL };
 110
 111                if (add_interactive != 1 || argc != 2)
 112                        die("add --interactive does not take any parameters");
 113                execv_git_cmd(args);
 114                exit(1);
 115        }
 116
 117        git_config(git_default_config);
 118
 119        newfd = hold_lock_file_for_update(&lock_file, get_index_file(), 1);
 120
 121        for (i = 1; i < argc; i++) {
 122                const char *arg = argv[i];
 123
 124                if (arg[0] != '-')
 125                        break;
 126                if (!strcmp(arg, "--")) {
 127                        i++;
 128                        break;
 129                }
 130                if (!strcmp(arg, "-n")) {
 131                        show_only = 1;
 132                        continue;
 133                }
 134                if (!strcmp(arg, "-f")) {
 135                        ignored_too = 1;
 136                        continue;
 137                }
 138                if (!strcmp(arg, "-v")) {
 139                        verbose = 1;
 140                        continue;
 141                }
 142                usage(builtin_add_usage);
 143        }
 144        if (argc <= i) {
 145                fprintf(stderr, "Nothing specified, nothing added.\n");
 146                fprintf(stderr, "Maybe you wanted to say 'git add .'?\n");
 147                return 0;
 148        }
 149        pathspec = get_pathspec(prefix, argv + i);
 150
 151        fill_directory(&dir, pathspec);
 152
 153        if (show_only) {
 154                const char *sep = "", *eof = "";
 155                for (i = 0; i < dir.nr; i++) {
 156                        if (!ignored_too && dir.entries[i]->ignored)
 157                                continue;
 158                        printf("%s%s", sep, dir.entries[i]->name);
 159                        sep = " ";
 160                        eof = "\n";
 161                }
 162                fputs(eof, stdout);
 163                return 0;
 164        }
 165
 166        if (read_cache() < 0)
 167                die("index file corrupt");
 168
 169        if (!ignored_too) {
 170                int has_ignored = 0;
 171                for (i = 0; i < dir.nr; i++)
 172                        if (dir.entries[i]->ignored)
 173                                has_ignored = 1;
 174                if (has_ignored) {
 175                        fprintf(stderr, ignore_warning);
 176                        for (i = 0; i < dir.nr; i++) {
 177                                if (!dir.entries[i]->ignored)
 178                                        continue;
 179                                fprintf(stderr, "%s", dir.entries[i]->name);
 180                                if (dir.entries[i]->ignored_dir)
 181                                        fprintf(stderr, " (directory)");
 182                                fputc('\n', stderr);
 183                        }
 184                        fprintf(stderr,
 185                                "Use -f if you really want to add them.\n");
 186                        exit(1);
 187                }
 188        }
 189
 190        for (i = 0; i < dir.nr; i++)
 191                add_file_to_index(dir.entries[i]->name, verbose);
 192
 193        if (active_cache_changed) {
 194                if (write_cache(newfd, active_cache, active_nr) ||
 195                    close(newfd) || commit_lock_file(&lock_file))
 196                        die("Unable to write new index file");
 197        }
 198
 199        return 0;
 200}