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