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