2 * "git add" builtin command
4 * Copyright (C) 2006 Linus Torvalds
11 #include "cache-tree.h"
13 static const char builtin_add_usage[] =
14 "git-add [-n] [-v] <filepattern>...";
16 static int common_prefix(const char **pathspec)
18 const char *path, *slash, *next;
25 slash = strrchr(path, '/');
29 prefix = slash - path + 1;
30 while ((next = *++pathspec) != NULL) {
31 int len = strlen(next);
32 if (len >= prefix && !memcmp(path, next, len))
37 if (next[--len] != '/')
39 if (memcmp(path, next, len+1))
48 static int match_one(const char *match, const char *name, int namelen)
52 /* If the match was just the prefix, we matched */
53 matchlen = strlen(match);
58 * If we don't match the matchstring exactly,
59 * we need to match by fnmatch
61 if (strncmp(match, name, matchlen))
62 return !fnmatch(match, name, 0);
65 * If we did match the string exactly, we still
66 * need to make sure that it happened on a path
67 * component boundary (ie either the last character
68 * of the match was '/', or the next character of
69 * the name was '/' or the terminating NUL.
71 return match[matchlen-1] == '/' ||
72 name[matchlen] == '/' ||
76 static int match(const char **pathspec, const char *name, int namelen, int prefix, char *seen)
84 for (retval = 0; (match = *pathspec++) != NULL; seen++) {
88 if (match_one(match, name, namelen)) {
96 static void prune_directory(struct dir_struct *dir, const char **pathspec, int prefix)
100 struct dir_entry **src, **dst;
102 for (specs = 0; pathspec[specs]; specs++)
104 seen = xmalloc(specs);
105 memset(seen, 0, specs);
107 src = dst = dir->entries;
110 struct dir_entry *entry = *src++;
111 if (!match(pathspec, entry->name, entry->len, prefix, seen)) {
117 dir->nr = dst - dir->entries;
119 for (i = 0; i < specs; i++) {
125 /* Existing file? We must have ignored it */
127 if (!lstat(match, &st))
129 die("pathspec '%s' did not match any files", match);
133 static void fill_directory(struct dir_struct *dir, const char **pathspec)
135 const char *path, *base;
138 /* Set up the default git porcelain excludes */
139 memset(dir, 0, sizeof(*dir));
140 dir->exclude_per_dir = ".gitignore";
141 path = git_path("info/exclude");
142 if (!access(path, R_OK))
143 add_excludes_from_file(dir, path);
146 * Calculate common prefix for the pathspec, and
147 * use that to optimize the directory walk
149 baselen = common_prefix(pathspec);
153 char *common = xmalloc(baselen + 1);
154 common = xmalloc(baselen + 1);
155 memcpy(common, *pathspec, baselen);
157 path = base = common;
160 /* Read the directory and prune it */
161 read_directory(dir, path, base, baselen);
163 prune_directory(dir, pathspec, baselen);
166 static int add_file_to_index(const char *path, int verbose)
170 struct cache_entry *ce;
172 if (lstat(path, &st))
173 die("%s: unable to stat (%s)", path, strerror(errno));
175 if (!S_ISREG(st.st_mode) && !S_ISLNK(st.st_mode))
176 die("%s: can only add regular files or symbolic links", path);
178 namelen = strlen(path);
179 size = cache_entry_size(namelen);
180 ce = xcalloc(1, size);
181 memcpy(ce->name, path, namelen);
182 ce->ce_flags = htons(namelen);
183 fill_stat_cache_info(ce, &st);
185 ce->ce_mode = create_ce_mode(st.st_mode);
186 if (!trust_executable_bit) {
187 /* If there is an existing entry, pick the mode bits
190 int pos = cache_name_pos(path, namelen);
192 ce->ce_mode = active_cache[pos]->ce_mode;
195 if (index_path(ce->sha1, path, &st, 1))
196 die("unable to index file %s", path);
197 if (add_cache_entry(ce, ADD_CACHE_OK_TO_ADD))
198 die("unable to add %s to index",path);
200 printf("add '%s'\n", path);
201 cache_tree_invalidate_path(active_cache_tree, path);
205 static struct cache_file cache_file;
207 int cmd_add(int argc, const char **argv, char **envp)
210 int verbose = 0, show_only = 0;
211 const char *prefix = setup_git_directory();
212 const char **pathspec;
213 struct dir_struct dir;
215 git_config(git_default_config);
217 newfd = hold_index_file_for_update(&cache_file, get_index_file());
219 die("unable to create new cachefile");
221 if (read_cache() < 0)
222 die("index file corrupt");
224 for (i = 1; i < argc; i++) {
225 const char *arg = argv[i];
229 if (!strcmp(arg, "--")) {
233 if (!strcmp(arg, "-n")) {
237 if (!strcmp(arg, "-v")) {
241 die(builtin_add_usage);
243 git_config(git_default_config);
244 pathspec = get_pathspec(prefix, argv + i);
246 fill_directory(&dir, pathspec);
249 const char *sep = "", *eof = "";
250 for (i = 0; i < dir.nr; i++) {
251 printf("%s%s", sep, dir.entries[i]->name);
259 for (i = 0; i < dir.nr; i++)
260 add_file_to_index(dir.entries[i]->name, verbose);
262 if (active_cache_changed) {
263 if (write_cache(newfd, active_cache, active_nr) ||
264 commit_index_file(&cache_file))
265 die("Unable to write new index file");