2 * "git add" builtin command
4 * Copyright (C) 2006 Linus Torvalds
12 static const char builtin_add_usage[] =
13 "git-add [-n] [-v] <filepattern>...";
15 static int common_prefix(const char **pathspec)
17 const char *path, *slash, *next;
24 slash = strrchr(path, '/');
28 prefix = slash - path + 1;
29 while ((next = *++pathspec) != NULL) {
30 int len = strlen(next);
31 if (len >= prefix && !memcmp(path, next, len))
36 if (next[--len] != '/')
38 if (memcmp(path, next, len+1))
47 static int match(const char **pathspec, const char *name, int namelen, int prefix)
54 while ((match = *pathspec++) != NULL) {
58 matchlen = strlen(match);
61 if (!strncmp(match, name, matchlen)) {
62 if (match[matchlen-1] == '/')
64 switch (name[matchlen]) {
69 if (!fnmatch(match, name, 0))
75 static void prune_directory(struct dir_struct *dir, const char **pathspec, int prefix)
78 struct dir_entry **src, **dst;
80 src = dst = dir->entries;
83 struct dir_entry *entry = *src++;
84 if (!match(pathspec, entry->name, entry->len, prefix)) {
90 dir->nr = dst - dir->entries;
93 static void fill_directory(struct dir_struct *dir, const char **pathspec)
95 const char *path, *base;
98 /* Set up the default git porcelain excludes */
99 memset(dir, 0, sizeof(*dir));
100 dir->exclude_per_dir = ".gitignore";
101 path = git_path("info/exclude");
102 if (!access(path, R_OK))
103 add_excludes_from_file(dir, path);
106 * Calculate common prefix for the pathspec, and
107 * use that to optimize the directory walk
109 baselen = common_prefix(pathspec);
113 char *common = xmalloc(baselen + 1);
114 common = xmalloc(baselen + 1);
115 memcpy(common, *pathspec, baselen);
117 path = base = common;
120 /* Read the directory and prune it */
121 read_directory(dir, path, base, baselen);
123 prune_directory(dir, pathspec, baselen);
126 static int add_file_to_index(const char *path, int verbose)
130 struct cache_entry *ce;
132 if (lstat(path, &st))
133 die("%s: unable to stat (%s)", path, strerror(errno));
135 if (!S_ISREG(st.st_mode) && !S_ISLNK(st.st_mode))
136 die("%s: can only add regular files or symbolic links", path);
138 namelen = strlen(path);
139 size = cache_entry_size(namelen);
140 ce = xcalloc(1, size);
141 memcpy(ce->name, path, namelen);
142 ce->ce_flags = htons(namelen);
143 fill_stat_cache_info(ce, &st);
145 ce->ce_mode = create_ce_mode(st.st_mode);
146 if (!trust_executable_bit) {
147 /* If there is an existing entry, pick the mode bits
150 int pos = cache_name_pos(path, namelen);
152 ce->ce_mode = active_cache[pos]->ce_mode;
155 if (index_path(ce->sha1, path, &st, 1))
156 die("unable to index file %s", path);
157 if (add_cache_entry(ce, ADD_CACHE_OK_TO_ADD))
158 die("unable to add %s to index",path);
160 printf("add '%s'\n", path);
164 static struct cache_file cache_file;
166 int cmd_add(int argc, const char **argv, char **envp)
169 int verbose = 0, show_only = 0;
170 const char *prefix = setup_git_directory();
171 const char **pathspec;
172 struct dir_struct dir;
174 git_config(git_default_config);
176 newfd = hold_index_file_for_update(&cache_file, get_index_file());
178 die("unable to create new cachefile");
180 if (read_cache() < 0)
181 die("index file corrupt");
183 for (i = 1; i < argc; i++) {
184 const char *arg = argv[i];
188 if (!strcmp(arg, "--")) {
192 if (!strcmp(arg, "-n")) {
196 if (!strcmp(arg, "-v")) {
200 die(builtin_add_usage);
202 git_config(git_default_config);
203 pathspec = get_pathspec(prefix, argv + i);
205 fill_directory(&dir, pathspec);
208 const char *sep = "", *eof = "";
209 for (i = 0; i < dir.nr; i++) {
210 printf("%s%s", sep, dir.entries[i]->name);
218 for (i = 0; i < dir.nr; i++)
219 add_file_to_index(dir.entries[i]->name, verbose);
221 if (active_cache_changed) {
222 if (write_cache(newfd, active_cache, active_nr) ||
223 commit_index_file(&cache_file))
224 die("Unable to write new index file");