]>
Commit | Line | Data |
---|---|---|
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" |
58680165 | 11 | #include "run-command.h" |
5c46f754 | 12 | #include "parse-options.h" |
0d781539 | 13 | |
5c46f754 | 14 | static const char * const builtin_add_usage[] = { |
1b1dd23f | 15 | "git add [options] [--] <filepattern>...", |
5c46f754 KH |
16 | NULL |
17 | }; | |
9ccb3bca | 18 | static int patch_interactive, add_interactive; |
93c44d49 | 19 | static int take_worktree_changes; |
896bdfa2 | 20 | |
1e5f764c JH |
21 | static void fill_pathspec_matches(const char **pathspec, char *seen, int specs) |
22 | { | |
23 | int num_unmatched = 0, i; | |
24 | ||
25 | /* | |
71fe9451 | 26 | * Since we are walking the index as if we were walking the directory, |
1e5f764c JH |
27 | * we have to mark the matched pathspec as seen; otherwise we will |
28 | * mistakenly think that the user gave a pathspec that did not match | |
29 | * anything. | |
30 | */ | |
31 | for (i = 0; i < specs; i++) | |
32 | if (!seen[i]) | |
33 | num_unmatched++; | |
34 | if (!num_unmatched) | |
35 | return; | |
36 | for (i = 0; i < active_nr; i++) { | |
37 | struct cache_entry *ce = active_cache[i]; | |
38 | match_pathspec(pathspec, ce->name, ce_namelen(ce), 0, seen); | |
39 | } | |
40 | } | |
41 | ||
0d781539 LT |
42 | static void prune_directory(struct dir_struct *dir, const char **pathspec, int prefix) |
43 | { | |
f2593398 LT |
44 | char *seen; |
45 | int i, specs; | |
0d781539 LT |
46 | struct dir_entry **src, **dst; |
47 | ||
f2593398 LT |
48 | for (specs = 0; pathspec[specs]; specs++) |
49 | /* nothing */; | |
28f75818 | 50 | seen = xcalloc(specs, 1); |
f2593398 | 51 | |
0d781539 LT |
52 | src = dst = dir->entries; |
53 | i = dir->nr; | |
54 | while (--i >= 0) { | |
55 | struct dir_entry *entry = *src++; | |
4d06f8ac JH |
56 | if (match_pathspec(pathspec, entry->name, entry->len, |
57 | prefix, seen)) | |
58 | *dst++ = entry; | |
0d781539 LT |
59 | } |
60 | dir->nr = dst - dir->entries; | |
1e5f764c | 61 | fill_pathspec_matches(pathspec, seen, specs); |
f2593398 LT |
62 | |
63 | for (i = 0; i < specs; i++) { | |
e96980ef JK |
64 | if (!seen[i] && !file_exists(pathspec[i])) |
65 | die("pathspec '%s' did not match any files", | |
66 | pathspec[i]); | |
f2593398 | 67 | } |
399f0a8e | 68 | free(seen); |
0d781539 LT |
69 | } |
70 | ||
2ce53f9b JS |
71 | static void treat_gitlinks(const char **pathspec) |
72 | { | |
73 | int i; | |
74 | ||
75 | if (!pathspec || !*pathspec) | |
76 | return; | |
77 | ||
78 | for (i = 0; i < active_nr; i++) { | |
79 | struct cache_entry *ce = active_cache[i]; | |
80 | if (S_ISGITLINK(ce->ce_mode)) { | |
81 | int len = ce_namelen(ce), j; | |
82 | for (j = 0; pathspec[j]; j++) { | |
83 | int len2 = strlen(pathspec[j]); | |
84 | if (len2 <= len || pathspec[j][len] != '/' || | |
85 | memcmp(ce->name, pathspec[j], len)) | |
86 | continue; | |
87 | if (len2 == len + 1) | |
88 | /* strip trailing slash */ | |
89 | pathspec[j] = xstrndup(ce->name, len); | |
90 | else | |
91 | die ("Path '%s' is in submodule '%.*s'", | |
92 | pathspec[j], len, ce->name); | |
93 | } | |
94 | } | |
95 | } | |
96 | } | |
97 | ||
e96980ef JK |
98 | static void fill_directory(struct dir_struct *dir, const char **pathspec, |
99 | int ignored_too) | |
0d781539 LT |
100 | { |
101 | const char *path, *base; | |
102 | int baselen; | |
103 | ||
104 | /* Set up the default git porcelain excludes */ | |
105 | memset(dir, 0, sizeof(*dir)); | |
e96980ef | 106 | if (!ignored_too) { |
7c4c97c0 | 107 | dir->flags |= DIR_COLLECT_IGNORED; |
039bc64e | 108 | setup_standard_excludes(dir); |
e96980ef | 109 | } |
0d781539 LT |
110 | |
111 | /* | |
112 | * Calculate common prefix for the pathspec, and | |
113 | * use that to optimize the directory walk | |
114 | */ | |
115 | baselen = common_prefix(pathspec); | |
116 | path = "."; | |
117 | base = ""; | |
182af834 PH |
118 | if (baselen) |
119 | path = base = xmemdupz(*pathspec, baselen); | |
0d781539 LT |
120 | |
121 | /* Read the directory and prune it */ | |
9fc42d60 | 122 | read_directory(dir, path, base, baselen, pathspec); |
0d781539 LT |
123 | if (pathspec) |
124 | prune_directory(dir, pathspec, baselen); | |
125 | } | |
126 | ||
d616813d AJ |
127 | static void refresh(int verbose, const char **pathspec) |
128 | { | |
129 | char *seen; | |
130 | int i, specs; | |
131 | ||
132 | for (specs = 0; pathspec[specs]; specs++) | |
133 | /* nothing */; | |
134 | seen = xcalloc(specs, 1); | |
d14e7407 JH |
135 | refresh_index(&the_index, verbose ? REFRESH_SAY_CHANGED : REFRESH_QUIET, |
136 | pathspec, seen); | |
d616813d AJ |
137 | for (i = 0; i < specs; i++) { |
138 | if (!seen[i]) | |
139 | die("pathspec '%s' did not match any files", pathspec[i]); | |
140 | } | |
399f0a8e | 141 | free(seen); |
d616813d AJ |
142 | } |
143 | ||
3f061887 JH |
144 | static const char **validate_pathspec(int argc, const char **argv, const char *prefix) |
145 | { | |
146 | const char **pathspec = get_pathspec(prefix, argv); | |
147 | ||
725b0605 JH |
148 | if (pathspec) { |
149 | const char **p; | |
150 | for (p = pathspec; *p; p++) { | |
57199892 | 151 | if (has_symlink_leading_path(*p, strlen(*p))) { |
725b0605 JH |
152 | int len = prefix ? strlen(prefix) : 0; |
153 | die("'%s' is beyond a symbolic link", *p + len); | |
154 | } | |
155 | } | |
156 | } | |
157 | ||
3f061887 JH |
158 | return pathspec; |
159 | } | |
160 | ||
161 | int interactive_add(int argc, const char **argv, const char *prefix) | |
58680165 | 162 | { |
b63e9950 | 163 | int status, ac; |
3f061887 JH |
164 | const char **args; |
165 | const char **pathspec = NULL; | |
166 | ||
167 | if (argc) { | |
168 | pathspec = validate_pathspec(argc, argv, prefix); | |
169 | if (!pathspec) | |
170 | return -1; | |
171 | } | |
324ccbd6 | 172 | |
b63e9950 WC |
173 | args = xcalloc(sizeof(const char *), (argc + 4)); |
174 | ac = 0; | |
175 | args[ac++] = "add--interactive"; | |
176 | if (patch_interactive) | |
177 | args[ac++] = "--patch"; | |
178 | args[ac++] = "--"; | |
179 | if (argc) { | |
180 | memcpy(&(args[ac]), pathspec, sizeof(const char *) * argc); | |
181 | ac += argc; | |
182 | } | |
183 | args[ac] = NULL; | |
7c0ab445 WC |
184 | |
185 | status = run_command_v_opt(args, RUN_GIT_CMD); | |
186 | free(args); | |
187 | return status; | |
58680165 KH |
188 | } |
189 | ||
021b6e45 | 190 | static struct lock_file lock_file; |
0d781539 | 191 | |
b39c53e6 | 192 | static const char ignore_error[] = |
6a1ad325 JH |
193 | "The following paths are ignored by one of your .gitignore files:\n"; |
194 | ||
5c46f754 | 195 | static int verbose = 0, show_only = 0, ignored_too = 0, refresh_only = 0; |
39425819 | 196 | static int ignore_add_errors, addremove, intent_to_add; |
5c46f754 KH |
197 | |
198 | static struct option builtin_add_options[] = { | |
199 | OPT__DRY_RUN(&show_only), | |
200 | OPT__VERBOSE(&verbose), | |
201 | OPT_GROUP(""), | |
202 | OPT_BOOLEAN('i', "interactive", &add_interactive, "interactive picking"), | |
b63e9950 | 203 | OPT_BOOLEAN('p', "patch", &patch_interactive, "interactive patching"), |
69c61c4f SG |
204 | OPT_BOOLEAN('f', "force", &ignored_too, "allow adding otherwise ignored files"), |
205 | OPT_BOOLEAN('u', "update", &take_worktree_changes, "update tracked files"), | |
39425819 | 206 | OPT_BOOLEAN('N', "intent-to-add", &intent_to_add, "record only the fact that the path will be added later"), |
3ba1f114 | 207 | OPT_BOOLEAN('A', "all", &addremove, "add all, noticing removal of tracked files"), |
5c46f754 | 208 | OPT_BOOLEAN( 0 , "refresh", &refresh_only, "don't add, only refresh the index"), |
984b83ef | 209 | OPT_BOOLEAN( 0 , "ignore-errors", &ignore_add_errors, "just skip files which cannot be added because of errors"), |
5c46f754 KH |
210 | OPT_END(), |
211 | }; | |
212 | ||
9bd81e42 | 213 | static int add_config(const char *var, const char *value, void *cb) |
dad25e4a AR |
214 | { |
215 | if (!strcasecmp(var, "add.ignore-errors")) { | |
216 | ignore_add_errors = git_config_bool(var, value); | |
217 | return 0; | |
218 | } | |
9bd81e42 | 219 | return git_default_config(var, value, cb); |
dad25e4a AR |
220 | } |
221 | ||
c972ec04 JH |
222 | static int add_files(struct dir_struct *dir, int flags) |
223 | { | |
224 | int i, exit_status = 0; | |
225 | ||
226 | if (dir->ignored_nr) { | |
227 | fprintf(stderr, ignore_error); | |
228 | for (i = 0; i < dir->ignored_nr; i++) | |
229 | fprintf(stderr, "%s\n", dir->ignored[i]->name); | |
230 | fprintf(stderr, "Use -f if you really want to add them.\n"); | |
231 | die("no files added"); | |
232 | } | |
233 | ||
234 | for (i = 0; i < dir->nr; i++) | |
235 | if (add_file_to_cache(dir->entries[i]->name, flags)) { | |
236 | if (!ignore_add_errors) | |
237 | die("adding files failed"); | |
238 | exit_status = 1; | |
239 | } | |
240 | return exit_status; | |
241 | } | |
242 | ||
a633fca0 | 243 | int cmd_add(int argc, const char **argv, const char *prefix) |
0d781539 | 244 | { |
7ae02a30 | 245 | int exit_status = 0; |
c972ec04 | 246 | int newfd; |
0d781539 LT |
247 | const char **pathspec; |
248 | struct dir_struct dir; | |
205ffa94 | 249 | int flags; |
c972ec04 JH |
250 | int add_new_files; |
251 | int require_pathspec; | |
5cde71d6 | 252 | |
5c46f754 KH |
253 | argc = parse_options(argc, argv, builtin_add_options, |
254 | builtin_add_usage, 0); | |
b63e9950 WC |
255 | if (patch_interactive) |
256 | add_interactive = 1; | |
7c0ab445 | 257 | if (add_interactive) |
3f061887 | 258 | exit(interactive_add(argc, argv, prefix)); |
0d781539 | 259 | |
9bd81e42 | 260 | git_config(add_config, NULL); |
0d781539 | 261 | |
3ba1f114 JH |
262 | if (addremove && take_worktree_changes) |
263 | die("-A and -u are mutually incompatible"); | |
1e5f764c | 264 | if ((addremove || take_worktree_changes) && !argc) { |
3ba1f114 JH |
265 | static const char *here[2] = { ".", NULL }; |
266 | argc = 1; | |
267 | argv = here; | |
268 | } | |
269 | ||
c972ec04 JH |
270 | add_new_files = !take_worktree_changes && !refresh_only; |
271 | require_pathspec = !take_worktree_changes; | |
272 | ||
30ca07a2 | 273 | newfd = hold_locked_index(&lock_file, 1); |
0d781539 | 274 | |
205ffa94 | 275 | flags = ((verbose ? ADD_CACHE_VERBOSE : 0) | |
01665924 | 276 | (show_only ? ADD_CACHE_PRETEND : 0) | |
39425819 | 277 | (intent_to_add ? ADD_CACHE_INTENT : 0) | |
1e5f764c JH |
278 | (ignore_add_errors ? ADD_CACHE_IGNORE_ERRORS : 0) | |
279 | (!(addremove || take_worktree_changes) | |
280 | ? ADD_CACHE_IGNORE_REMOVAL : 0)); | |
205ffa94 | 281 | |
c972ec04 | 282 | if (require_pathspec && argc == 0) { |
93b0d86a JH |
283 | fprintf(stderr, "Nothing specified, nothing added.\n"); |
284 | fprintf(stderr, "Maybe you wanted to say 'git add .'?\n"); | |
285 | return 0; | |
286 | } | |
725b0605 | 287 | pathspec = validate_pathspec(argc, argv, prefix); |
0d781539 | 288 | |
366bfcb6 NP |
289 | if (read_cache() < 0) |
290 | die("index file corrupt"); | |
2ce53f9b | 291 | treat_gitlinks(pathspec); |
366bfcb6 | 292 | |
1e5f764c JH |
293 | if (add_new_files) |
294 | /* This picks up the paths that are not tracked */ | |
295 | fill_directory(&dir, pathspec, ignored_too); | |
296 | ||
c972ec04 JH |
297 | if (refresh_only) { |
298 | refresh(verbose, pathspec); | |
299 | goto finish; | |
6a1ad325 JH |
300 | } |
301 | ||
1e5f764c | 302 | exit_status |= add_files_to_cache(prefix, pathspec, flags); |
c972ec04 JH |
303 | |
304 | if (add_new_files) | |
305 | exit_status |= add_files(&dir, flags); | |
0d781539 | 306 | |
dfdac5d9 | 307 | finish: |
0d781539 LT |
308 | if (active_cache_changed) { |
309 | if (write_cache(newfd, active_cache, active_nr) || | |
4ed7cd3a | 310 | commit_locked_index(&lock_file)) |
0d781539 LT |
311 | die("Unable to write new index file"); |
312 | } | |
313 | ||
7ae02a30 | 314 | return exit_status; |
0d781539 | 315 | } |