]>
Commit | Line | Data |
---|---|---|
0d781539 LT |
1 | /* |
2 | * "git add" builtin command | |
3 | * | |
4 | * Copyright (C) 2006 Linus Torvalds | |
5 | */ | |
f8adbec9 | 6 | #define USE_THE_INDEX_COMPATIBILITY_MACROS |
0d781539 | 7 | #include "cache.h" |
b2141fc1 | 8 | #include "config.h" |
0d781539 | 9 | #include "builtin.h" |
697cc8ef | 10 | #include "lockfile.h" |
0d781539 | 11 | #include "dir.h" |
6f525e71 | 12 | #include "pathspec.h" |
d807c4a0 | 13 | #include "exec-cmd.h" |
93872e07 | 14 | #include "cache-tree.h" |
58680165 | 15 | #include "run-command.h" |
5c46f754 | 16 | #include "parse-options.h" |
c59cb03a | 17 | #include "diff.h" |
fb7d3f32 | 18 | #include "diffcore.h" |
c59cb03a | 19 | #include "revision.h" |
568508e7 | 20 | #include "bulk-checkin.h" |
dbbcd44f | 21 | #include "strvec.h" |
bdab9721 | 22 | #include "submodule.h" |
f83dff60 | 23 | #include "add-interactive.h" |
0d781539 | 24 | |
5c46f754 | 25 | static const char * const builtin_add_usage[] = { |
9c9b4f2f | 26 | N_("git add [<options>] [--] <pathspec>..."), |
5c46f754 KH |
27 | NULL |
28 | }; | |
c59cb03a | 29 | static int patch_interactive, add_interactive, edit_interactive; |
93c44d49 | 30 | static int take_worktree_changes; |
9472935d | 31 | static int add_renormalize; |
bebb5d6d | 32 | static int pathspec_file_nul; |
0299a696 | 33 | static int include_sparse; |
bebb5d6d | 34 | static const char *pathspec_from_file; |
90a6bb98 | 35 | static int legacy_stash_p; /* support for the scripted `git stash` */ |
896bdfa2 | 36 | |
9cba13ca | 37 | struct update_callback_data { |
610d55af | 38 | int flags; |
fb7d3f32 LT |
39 | int add_errors; |
40 | }; | |
41 | ||
9ebd7fe1 | 42 | static int chmod_pathspec(struct pathspec *pathspec, char flip, int show_only) |
610d55af | 43 | { |
9ebd7fe1 | 44 | int i, ret = 0; |
610d55af TG |
45 | |
46 | for (i = 0; i < active_nr; i++) { | |
47 | struct cache_entry *ce = active_cache[i]; | |
c937d70b | 48 | int err; |
610d55af | 49 | |
63b60b3a DS |
50 | if (!include_sparse && |
51 | (ce_skip_worktree(ce) || | |
52 | !path_in_sparse_checkout(ce->name, &the_index))) | |
d73dbafc MT |
53 | continue; |
54 | ||
6d2df284 | 55 | if (pathspec && !ce_path_match(&the_index, ce, pathspec, NULL)) |
610d55af TG |
56 | continue; |
57 | ||
c937d70b MT |
58 | if (!show_only) |
59 | err = chmod_cache_entry(ce, flip); | |
60 | else | |
61 | err = S_ISREG(ce->ce_mode) ? 0 : -1; | |
62 | ||
63 | if (err < 0) | |
9ebd7fe1 | 64 | ret = error(_("cannot chmod %cx '%s'"), flip, ce->name); |
610d55af | 65 | } |
9ebd7fe1 MT |
66 | |
67 | return ret; | |
610d55af TG |
68 | } |
69 | ||
75973b2c JH |
70 | static int fix_unmerged_status(struct diff_filepair *p, |
71 | struct update_callback_data *data) | |
72 | { | |
73 | if (p->status != DIFF_STATUS_UNMERGED) | |
74 | return p->status; | |
75 | if (!(data->flags & ADD_CACHE_IGNORE_REMOVAL) && !p->two->mode) | |
76 | /* | |
77 | * This is not an explicit add request, and the | |
78 | * path is missing from the working tree (deleted) | |
79 | */ | |
80 | return DIFF_STATUS_DELETED; | |
81 | else | |
82 | /* | |
83 | * Either an explicit add request, or path exists | |
84 | * in the working tree. An attempt to explicitly | |
85 | * add a path that does not exist in the working tree | |
86 | * will be caught as an error by the caller immediately. | |
87 | */ | |
88 | return DIFF_STATUS_MODIFIED; | |
89 | } | |
90 | ||
fb7d3f32 LT |
91 | static void update_callback(struct diff_queue_struct *q, |
92 | struct diff_options *opt, void *cbdata) | |
93 | { | |
94 | int i; | |
95 | struct update_callback_data *data = cbdata; | |
96 | ||
97 | for (i = 0; i < q->nr; i++) { | |
98 | struct diff_filepair *p = q->queue[i]; | |
99 | const char *path = p->one->path; | |
49fdd51a | 100 | |
0299a696 | 101 | if (!include_sparse && !path_in_sparse_checkout(path, &the_index)) |
49fdd51a DS |
102 | continue; |
103 | ||
75973b2c | 104 | switch (fix_unmerged_status(p, data)) { |
fb7d3f32 | 105 | default: |
990ac4be | 106 | die(_("unexpected diff status %c"), p->status); |
fb7d3f32 LT |
107 | case DIFF_STATUS_MODIFIED: |
108 | case DIFF_STATUS_TYPE_CHANGED: | |
610d55af | 109 | if (add_file_to_index(&the_index, path, data->flags)) { |
fb7d3f32 | 110 | if (!(data->flags & ADD_CACHE_IGNORE_ERRORS)) |
990ac4be | 111 | die(_("updating files failed")); |
fb7d3f32 LT |
112 | data->add_errors++; |
113 | } | |
114 | break; | |
115 | case DIFF_STATUS_DELETED: | |
116 | if (data->flags & ADD_CACHE_IGNORE_REMOVAL) | |
117 | break; | |
118 | if (!(data->flags & ADD_CACHE_PRETEND)) | |
119 | remove_file_from_index(&the_index, path); | |
120 | if (data->flags & (ADD_CACHE_PRETEND|ADD_CACHE_VERBOSE)) | |
475c73eb | 121 | printf(_("remove '%s'\n"), path); |
fb7d3f32 LT |
122 | break; |
123 | } | |
124 | } | |
125 | } | |
126 | ||
610d55af TG |
127 | int add_files_to_cache(const char *prefix, |
128 | const struct pathspec *pathspec, int flags) | |
fb7d3f32 | 129 | { |
160c4b18 | 130 | struct update_callback_data data; |
fb7d3f32 | 131 | struct rev_info rev; |
71c7b053 | 132 | |
160c4b18 JH |
133 | memset(&data, 0, sizeof(data)); |
134 | data.flags = flags; | |
135 | ||
2abf3503 | 136 | repo_init_revisions(the_repository, &rev, prefix); |
fb7d3f32 | 137 | setup_revisions(0, NULL, &rev, NULL); |
3efe8e43 NTND |
138 | if (pathspec) |
139 | copy_pathspec(&rev.prune_data, pathspec); | |
fb7d3f32 LT |
140 | rev.diffopt.output_format = DIFF_FORMAT_CALLBACK; |
141 | rev.diffopt.format_callback = update_callback; | |
160c4b18 | 142 | rev.diffopt.format_callback_data = &data; |
0d1e0e78 | 143 | rev.diffopt.flags.override_submodule_config = 1; |
75973b2c | 144 | rev.max_count = 0; /* do not compare unmerged paths with stage #2 */ |
fb7d3f32 | 145 | run_diff_files(&rev, DIFF_RACY_IS_MODIFIED); |
fe6a01af | 146 | clear_pathspec(&rev.prune_data); |
fb7d3f32 LT |
147 | return !!data.add_errors; |
148 | } | |
149 | ||
9472935d TB |
150 | static int renormalize_tracked_files(const struct pathspec *pathspec, int flags) |
151 | { | |
152 | int i, retval = 0; | |
153 | ||
154 | for (i = 0; i < active_nr; i++) { | |
155 | struct cache_entry *ce = active_cache[i]; | |
156 | ||
61d450f0 DS |
157 | if (!include_sparse && |
158 | (ce_skip_worktree(ce) || | |
159 | !path_in_sparse_checkout(ce->name, &the_index))) | |
d73dbafc | 160 | continue; |
9472935d TB |
161 | if (ce_stage(ce)) |
162 | continue; /* do not touch unmerged paths */ | |
163 | if (!S_ISREG(ce->ce_mode) && !S_ISLNK(ce->ce_mode)) | |
164 | continue; /* do not touch non blobs */ | |
6d2df284 | 165 | if (pathspec && !ce_path_match(&the_index, ce, pathspec, NULL)) |
9472935d | 166 | continue; |
9e5da3d0 | 167 | retval |= add_file_to_cache(ce->name, flags | ADD_CACHE_RENORMALIZE); |
9472935d TB |
168 | } |
169 | ||
170 | return retval; | |
171 | } | |
172 | ||
053a6b18 | 173 | static char *prune_directory(struct dir_struct *dir, struct pathspec *pathspec, int prefix) |
0d781539 | 174 | { |
f2593398 | 175 | char *seen; |
84b8b5d1 | 176 | int i; |
0d781539 LT |
177 | struct dir_entry **src, **dst; |
178 | ||
84b8b5d1 | 179 | seen = xcalloc(pathspec->nr, 1); |
f2593398 | 180 | |
0d781539 LT |
181 | src = dst = dir->entries; |
182 | i = dir->nr; | |
183 | while (--i >= 0) { | |
184 | struct dir_entry *entry = *src++; | |
6d2df284 | 185 | if (dir_path_match(&the_index, entry, pathspec, prefix, seen)) |
4d06f8ac | 186 | *dst++ = entry; |
0d781539 LT |
187 | } |
188 | dir->nr = dst - dir->entries; | |
719630eb | 189 | add_pathspec_matches_against_index(pathspec, &the_index, seen, |
a20f7047 | 190 | PS_IGNORE_SKIP_WORKTREE); |
81f45e7d | 191 | return seen; |
0d781539 LT |
192 | } |
193 | ||
a20f7047 | 194 | static int refresh(int verbose, const struct pathspec *pathspec) |
d616813d AJ |
195 | { |
196 | char *seen; | |
a20f7047 MT |
197 | int i, ret = 0; |
198 | char *skip_worktree_seen = NULL; | |
199 | struct string_list only_match_skip_worktree = STRING_LIST_INIT_NODUP; | |
200 | int flags = REFRESH_IGNORE_SKIP_WORKTREE | | |
201 | (verbose ? REFRESH_IN_PORCELAIN : REFRESH_QUIET); | |
d616813d | 202 | |
9b2d6149 | 203 | seen = xcalloc(pathspec->nr, 1); |
a20f7047 MT |
204 | refresh_index(&the_index, flags, pathspec, seen, |
205 | _("Unstaged changes after refreshing the index:")); | |
9b2d6149 | 206 | for (i = 0; i < pathspec->nr; i++) { |
a20f7047 | 207 | if (!seen[i]) { |
939fa075 | 208 | const char *path = pathspec->items[i].original; |
939fa075 DS |
209 | |
210 | if (matches_skip_worktree(pathspec, i, &skip_worktree_seen) || | |
02155c8c | 211 | !path_in_sparse_checkout(path, &the_index)) { |
a20f7047 MT |
212 | string_list_append(&only_match_skip_worktree, |
213 | pathspec->items[i].original); | |
214 | } else { | |
215 | die(_("pathspec '%s' did not match any files"), | |
216 | pathspec->items[i].original); | |
217 | } | |
218 | } | |
219 | } | |
220 | ||
221 | if (only_match_skip_worktree.nr) { | |
222 | advise_on_updating_sparse_paths(&only_match_skip_worktree); | |
223 | ret = 1; | |
d616813d | 224 | } |
a20f7047 | 225 | |
ec36c42a | 226 | free(seen); |
a20f7047 MT |
227 | free(skip_worktree_seen); |
228 | string_list_clear(&only_match_skip_worktree, 0); | |
229 | return ret; | |
d616813d AJ |
230 | } |
231 | ||
46b5139c | 232 | int run_add_interactive(const char *revision, const char *patch_mode, |
480ca644 | 233 | const struct pathspec *pathspec) |
58680165 | 234 | { |
c45a18e8 | 235 | int status, i; |
22f9b7f3 | 236 | struct strvec argv = STRVEC_INIT; |
f83dff60 JS |
237 | int use_builtin_add_i = |
238 | git_env_bool("GIT_TEST_ADD_I_USE_BUILTIN", -1); | |
239 | ||
2df2d81d JH |
240 | if (use_builtin_add_i < 0) { |
241 | int experimental; | |
242 | if (!git_config_get_bool("add.interactive.usebuiltin", | |
243 | &use_builtin_add_i)) | |
244 | ; /* ok */ | |
245 | else if (!git_config_get_bool("feature.experimental", &experimental) && | |
246 | experimental) | |
247 | use_builtin_add_i = 1; | |
248 | } | |
f6aa7ecc JS |
249 | |
250 | if (use_builtin_add_i == 1) { | |
d2a233cb JS |
251 | enum add_p_mode mode; |
252 | ||
f6aa7ecc | 253 | if (!patch_mode) |
f83dff60 | 254 | return !!run_add_i(the_repository, pathspec); |
d2a233cb JS |
255 | |
256 | if (!strcmp(patch_mode, "--patch")) | |
257 | mode = ADD_P_ADD; | |
36bae1dc JS |
258 | else if (!strcmp(patch_mode, "--patch=stash")) |
259 | mode = ADD_P_STASH; | |
260 | else if (!strcmp(patch_mode, "--patch=reset")) | |
261 | mode = ADD_P_RESET; | |
52628f94 JS |
262 | else if (!strcmp(patch_mode, "--patch=checkout")) |
263 | mode = ADD_P_CHECKOUT; | |
cee6cb73 JS |
264 | else if (!strcmp(patch_mode, "--patch=worktree")) |
265 | mode = ADD_P_WORKTREE; | |
d2a233cb | 266 | else |
52628f94 | 267 | die("'%s' not supported", patch_mode); |
d2a233cb JS |
268 | |
269 | return !!run_add_p(the_repository, mode, revision, pathspec); | |
f83dff60 | 270 | } |
3f061887 | 271 | |
22f9b7f3 | 272 | strvec_push(&argv, "add--interactive"); |
46b5139c | 273 | if (patch_mode) |
22f9b7f3 | 274 | strvec_push(&argv, patch_mode); |
46b5139c | 275 | if (revision) |
22f9b7f3 JK |
276 | strvec_push(&argv, revision); |
277 | strvec_push(&argv, "--"); | |
480ca644 NTND |
278 | for (i = 0; i < pathspec->nr; i++) |
279 | /* pass original pathspec, to be re-parsed */ | |
22f9b7f3 | 280 | strvec_push(&argv, pathspec->items[i].original); |
7c0ab445 | 281 | |
d70a9eb6 | 282 | status = run_command_v_opt(argv.v, RUN_GIT_CMD); |
22f9b7f3 | 283 | strvec_clear(&argv); |
7c0ab445 | 284 | return status; |
58680165 KH |
285 | } |
286 | ||
e885a84f | 287 | int interactive_add(const char **argv, const char *prefix, int patch) |
46b5139c | 288 | { |
5a76aff1 | 289 | struct pathspec pathspec; |
46b5139c | 290 | |
625c3304 | 291 | parse_pathspec(&pathspec, 0, |
5a76aff1 | 292 | PATHSPEC_PREFER_FULL | |
480ca644 NTND |
293 | PATHSPEC_SYMLINK_LEADING_PATH | |
294 | PATHSPEC_PREFIX_ORIGIN, | |
5a76aff1 | 295 | prefix, argv); |
46b5139c TR |
296 | |
297 | return run_add_interactive(NULL, | |
b4bd4668 | 298 | patch ? "--patch" : NULL, |
480ca644 | 299 | &pathspec); |
46b5139c TR |
300 | } |
301 | ||
2af202be | 302 | static int edit_patch(int argc, const char **argv, const char *prefix) |
c59cb03a | 303 | { |
d292bfaf | 304 | char *file = git_pathdup("ADD_EDIT.patch"); |
d3180279 | 305 | struct child_process child = CHILD_PROCESS_INIT; |
c59cb03a JS |
306 | struct rev_info rev; |
307 | int out; | |
308 | struct stat st; | |
309 | ||
310 | git_config(git_diff_basic_config, NULL); /* no "diff" UI options */ | |
311 | ||
312 | if (read_cache() < 0) | |
d521abf8 | 313 | die(_("Could not read the index")); |
c59cb03a | 314 | |
2abf3503 | 315 | repo_init_revisions(the_repository, &rev, prefix); |
c59cb03a JS |
316 | rev.diffopt.context = 7; |
317 | ||
318 | argc = setup_revisions(argc, argv, &rev, NULL); | |
319 | rev.diffopt.output_format = DIFF_FORMAT_PATCH; | |
7f3b8c62 | 320 | rev.diffopt.use_color = 0; |
0d1e0e78 | 321 | rev.diffopt.flags.ignore_dirty_submodules = 1; |
66e905b7 | 322 | out = xopen(file, O_CREAT | O_WRONLY | O_TRUNC, 0666); |
41698375 | 323 | rev.diffopt.file = xfdopen(out, "w"); |
c59cb03a JS |
324 | rev.diffopt.close_file = 1; |
325 | if (run_diff_files(&rev, 0)) | |
d521abf8 | 326 | die(_("Could not write patch")); |
c59cb03a | 327 | |
cb64800d JK |
328 | if (launch_editor(file, NULL, NULL)) |
329 | die(_("editing patch failed")); | |
c59cb03a JS |
330 | |
331 | if (stat(file, &st)) | |
990ac4be | 332 | die_errno(_("Could not stat '%s'"), file); |
c59cb03a | 333 | if (!st.st_size) |
990ac4be | 334 | die(_("Empty patch. Aborted.")); |
c59cb03a | 335 | |
c59cb03a | 336 | child.git_cmd = 1; |
2b709893 ÆAB |
337 | strvec_pushl(&child.args, "apply", "--recount", "--cached", file, |
338 | NULL); | |
c59cb03a | 339 | if (run_command(&child)) |
d521abf8 | 340 | die(_("Could not apply '%s'"), file); |
c59cb03a JS |
341 | |
342 | unlink(file); | |
d292bfaf | 343 | free(file); |
c59cb03a JS |
344 | return 0; |
345 | } | |
346 | ||
b39c53e6 | 347 | static const char ignore_error[] = |
439fb829 | 348 | N_("The following paths are ignored by one of your .gitignore files:\n"); |
6a1ad325 | 349 | |
300c0a22 | 350 | static int verbose, show_only, ignored_too, refresh_only; |
45c45e30 | 351 | static int ignore_add_errors, intent_to_add, ignore_missing; |
53213994 | 352 | static int warn_on_embedded_repo = 1; |
45c45e30 | 353 | |
fdc97abd | 354 | #define ADDREMOVE_DEFAULT 1 |
45c45e30 JH |
355 | static int addremove = ADDREMOVE_DEFAULT; |
356 | static int addremove_explicit = -1; /* unspecified */ | |
5c46f754 | 357 | |
4e55ed32 ET |
358 | static char *chmod_arg; |
359 | ||
9f60f49b JH |
360 | static int ignore_removal_cb(const struct option *opt, const char *arg, int unset) |
361 | { | |
362 | /* if we are told to ignore, we are not adding removals */ | |
363 | *(int *)opt->value = !unset ? 0 : 1; | |
364 | return 0; | |
365 | } | |
366 | ||
5c46f754 | 367 | static struct option builtin_add_options[] = { |
1b56024c NTND |
368 | OPT__DRY_RUN(&show_only, N_("dry run")), |
369 | OPT__VERBOSE(&verbose, N_("be verbose")), | |
5c46f754 | 370 | OPT_GROUP(""), |
300c0a22 JH |
371 | OPT_BOOL('i', "interactive", &add_interactive, N_("interactive picking")), |
372 | OPT_BOOL('p', "patch", &patch_interactive, N_("select hunks interactively")), | |
373 | OPT_BOOL('e', "edit", &edit_interactive, N_("edit current diff and apply")), | |
1224781d | 374 | OPT__FORCE(&ignored_too, N_("allow adding otherwise ignored files"), 0), |
300c0a22 | 375 | OPT_BOOL('u', "update", &take_worktree_changes, N_("update tracked files")), |
9472935d | 376 | OPT_BOOL(0, "renormalize", &add_renormalize, N_("renormalize EOL of tracked files (implies -u)")), |
300c0a22 | 377 | OPT_BOOL('N', "intent-to-add", &intent_to_add, N_("record only the fact that the path will be added later")), |
45c45e30 | 378 | OPT_BOOL('A', "all", &addremove_explicit, N_("add changes from all tracked and untracked files")), |
203c8533 | 379 | OPT_CALLBACK_F(0, "ignore-removal", &addremove_explicit, |
9f60f49b JH |
380 | NULL /* takes no arguments */, |
381 | N_("ignore paths removed in the working tree (same as --no-all)"), | |
203c8533 | 382 | PARSE_OPT_NOARG, ignore_removal_cb), |
300c0a22 JH |
383 | OPT_BOOL( 0 , "refresh", &refresh_only, N_("don't add, only refresh the index")), |
384 | OPT_BOOL( 0 , "ignore-errors", &ignore_add_errors, N_("just skip files which cannot be added because of errors")), | |
385 | OPT_BOOL( 0 , "ignore-missing", &ignore_missing, N_("check if - even missing - files are ignored in dry run")), | |
0299a696 | 386 | OPT_BOOL(0, "sparse", &include_sparse, N_("allow updating entries outside of the sparse-checkout cone")), |
5f0df44c RS |
387 | OPT_STRING(0, "chmod", &chmod_arg, "(+|-)x", |
388 | N_("override the executable bit of the listed files")), | |
53213994 JK |
389 | OPT_HIDDEN_BOOL(0, "warn-embedded-repo", &warn_on_embedded_repo, |
390 | N_("warn when adding an embedded repository")), | |
90a6bb98 JS |
391 | OPT_HIDDEN_BOOL(0, "legacy-stash-p", &legacy_stash_p, |
392 | N_("backend for `git stash -p`")), | |
bebb5d6d AM |
393 | OPT_PATHSPEC_FROM_FILE(&pathspec_from_file), |
394 | OPT_PATHSPEC_FILE_NUL(&pathspec_file_nul), | |
5c46f754 KH |
395 | OPT_END(), |
396 | }; | |
397 | ||
9bd81e42 | 398 | static int add_config(const char *var, const char *value, void *cb) |
dad25e4a | 399 | { |
8c2be75f JN |
400 | if (!strcmp(var, "add.ignoreerrors") || |
401 | !strcmp(var, "add.ignore-errors")) { | |
dad25e4a AR |
402 | ignore_add_errors = git_config_bool(var, value); |
403 | return 0; | |
404 | } | |
f83dff60 | 405 | |
9bd81e42 | 406 | return git_default_config(var, value, cb); |
dad25e4a AR |
407 | } |
408 | ||
53213994 JK |
409 | static const char embedded_advice[] = N_( |
410 | "You've added another git repository inside your current repository.\n" | |
411 | "Clones of the outer repository will not contain the contents of\n" | |
412 | "the embedded repository and will not know how to obtain it.\n" | |
413 | "If you meant to add a submodule, use:\n" | |
414 | "\n" | |
415 | " git submodule add <url> %s\n" | |
416 | "\n" | |
417 | "If you added this path by mistake, you can remove it from the\n" | |
418 | "index with:\n" | |
419 | "\n" | |
420 | " git rm --cached %s\n" | |
421 | "\n" | |
422 | "See \"git help submodule\" for more information." | |
423 | ); | |
424 | ||
425 | static void check_embedded_repo(const char *path) | |
426 | { | |
427 | struct strbuf name = STRBUF_INIT; | |
c2a4b6d4 | 428 | static int adviced_on_embedded_repo = 0; |
53213994 JK |
429 | |
430 | if (!warn_on_embedded_repo) | |
431 | return; | |
432 | if (!ends_with(path, "/")) | |
433 | return; | |
434 | ||
435 | /* Drop trailing slash for aesthetics */ | |
436 | strbuf_addstr(&name, path); | |
437 | strbuf_strip_suffix(&name, "/"); | |
438 | ||
439 | warning(_("adding embedded git repository: %s"), name.buf); | |
c2a4b6d4 ÆAB |
440 | if (!adviced_on_embedded_repo && |
441 | advice_enabled(ADVICE_ADD_EMBEDDED_REPO)) { | |
53213994 | 442 | advise(embedded_advice, name.buf, name.buf); |
c2a4b6d4 | 443 | adviced_on_embedded_repo = 1; |
53213994 JK |
444 | } |
445 | ||
446 | strbuf_release(&name); | |
447 | } | |
448 | ||
610d55af | 449 | static int add_files(struct dir_struct *dir, int flags) |
c972ec04 JH |
450 | { |
451 | int i, exit_status = 0; | |
105e8b01 | 452 | struct string_list matched_sparse_paths = STRING_LIST_INIT_NODUP; |
c972ec04 JH |
453 | |
454 | if (dir->ignored_nr) { | |
439fb829 | 455 | fprintf(stderr, _(ignore_error)); |
c972ec04 JH |
456 | for (i = 0; i < dir->ignored_nr; i++) |
457 | fprintf(stderr, "%s\n", dir->ignored[i]->name); | |
ed9bff08 | 458 | if (advice_enabled(ADVICE_ADD_IGNORED_FILE)) |
887a0fd5 HW |
459 | advise(_("Use -f if you really want to add them.\n" |
460 | "Turn this message off by running\n" | |
461 | "\"git config advice.addIgnoredFile false\"")); | |
1d31e5a2 | 462 | exit_status = 1; |
c972ec04 JH |
463 | } |
464 | ||
53213994 | 465 | for (i = 0; i < dir->nr; i++) { |
0299a696 DS |
466 | if (!include_sparse && |
467 | !path_in_sparse_checkout(dir->entries[i]->name, &the_index)) { | |
105e8b01 DS |
468 | string_list_append(&matched_sparse_paths, |
469 | dir->entries[i]->name); | |
470 | continue; | |
471 | } | |
610d55af | 472 | if (add_file_to_index(&the_index, dir->entries[i]->name, flags)) { |
c972ec04 | 473 | if (!ignore_add_errors) |
990ac4be | 474 | die(_("adding files failed")); |
c972ec04 | 475 | exit_status = 1; |
f937bc2f KM |
476 | } else { |
477 | check_embedded_repo(dir->entries[i]->name); | |
c972ec04 | 478 | } |
53213994 | 479 | } |
105e8b01 DS |
480 | |
481 | if (matched_sparse_paths.nr) { | |
482 | advise_on_updating_sparse_paths(&matched_sparse_paths); | |
483 | exit_status = 1; | |
484 | } | |
485 | ||
486 | string_list_clear(&matched_sparse_paths, 0); | |
487 | ||
c972ec04 JH |
488 | return exit_status; |
489 | } | |
490 | ||
a633fca0 | 491 | int cmd_add(int argc, const char **argv, const char *prefix) |
0d781539 | 492 | { |
7ae02a30 | 493 | int exit_status = 0; |
5a76aff1 | 494 | struct pathspec pathspec; |
ce93a4c6 | 495 | struct dir_struct dir = DIR_INIT; |
610d55af | 496 | int flags; |
c972ec04 JH |
497 | int add_new_files; |
498 | int require_pathspec; | |
81f45e7d | 499 | char *seen = NULL; |
0fa5a2ed | 500 | struct lock_file lock_file = LOCK_INIT; |
5cde71d6 | 501 | |
ed342fde SB |
502 | git_config(add_config, NULL); |
503 | ||
37782920 | 504 | argc = parse_options(argc, argv, prefix, builtin_add_options, |
c59cb03a | 505 | builtin_add_usage, PARSE_OPT_KEEP_ARGV0); |
b63e9950 WC |
506 | if (patch_interactive) |
507 | add_interactive = 1; | |
bebb5d6d | 508 | if (add_interactive) { |
a1989cf7 | 509 | if (show_only) |
12909b6b | 510 | die(_("options '%s' and '%s' cannot be used together"), "--dry-run", "--interactive/--patch"); |
bebb5d6d | 511 | if (pathspec_from_file) |
12909b6b | 512 | die(_("options '%s' and '%s' cannot be used together"), "--pathspec-from-file", "--interactive/--patch"); |
e885a84f | 513 | exit(interactive_add(argv + 1, prefix, patch_interactive)); |
bebb5d6d | 514 | } |
90a6bb98 JS |
515 | if (legacy_stash_p) { |
516 | struct pathspec pathspec; | |
517 | ||
518 | parse_pathspec(&pathspec, 0, | |
519 | PATHSPEC_PREFER_FULL | | |
520 | PATHSPEC_SYMLINK_LEADING_PATH | | |
521 | PATHSPEC_PREFIX_ORIGIN, | |
522 | prefix, argv); | |
523 | ||
524 | return run_add_interactive(NULL, "--patch=stash", &pathspec); | |
525 | } | |
0d781539 | 526 | |
bebb5d6d AM |
527 | if (edit_interactive) { |
528 | if (pathspec_from_file) | |
12909b6b | 529 | die(_("options '%s' and '%s' cannot be used together"), "--pathspec-from-file", "--edit"); |
c59cb03a | 530 | return(edit_patch(argc, argv, prefix)); |
bebb5d6d | 531 | } |
c59cb03a JS |
532 | argc--; |
533 | argv++; | |
534 | ||
45c45e30 JH |
535 | if (0 <= addremove_explicit) |
536 | addremove = addremove_explicit; | |
537 | else if (take_worktree_changes && ADDREMOVE_DEFAULT) | |
538 | addremove = 0; /* "-u" was given but not "-A" */ | |
539 | ||
3ba1f114 | 540 | if (addremove && take_worktree_changes) |
12909b6b | 541 | die(_("options '%s' and '%s' cannot be used together"), "-A", "-u"); |
45c45e30 | 542 | |
108da0db | 543 | if (!show_only && ignore_missing) |
6fa00ee8 | 544 | die(_("the option '%s' requires '%s'"), "--ignore-missing", "--dry-run"); |
808d3d71 | 545 | |
610d55af TG |
546 | if (chmod_arg && ((chmod_arg[0] != '-' && chmod_arg[0] != '+') || |
547 | chmod_arg[1] != 'x' || chmod_arg[2])) | |
4e55ed32 ET |
548 | die(_("--chmod param '%s' must be either -x or +x"), chmod_arg); |
549 | ||
9472935d | 550 | add_new_files = !take_worktree_changes && !refresh_only && !add_renormalize; |
29abb339 | 551 | require_pathspec = !(take_worktree_changes || (0 < addremove_explicit)); |
c972ec04 | 552 | |
5e7cbab1 DS |
553 | prepare_repo_settings(the_repository); |
554 | the_repository->settings.command_requires_full_index = 0; | |
555 | ||
b3e83cc7 | 556 | hold_locked_index(&lock_file, LOCK_DIE_ON_ERROR); |
0d781539 | 557 | |
5a76aff1 NTND |
558 | /* |
559 | * Check the "pathspec '%s' did not match any files" block | |
560 | * below before enabling new magic. | |
561 | */ | |
84d938b7 | 562 | parse_pathspec(&pathspec, PATHSPEC_ATTR, |
5a76aff1 | 563 | PATHSPEC_PREFER_FULL | |
c08397e3 | 564 | PATHSPEC_SYMLINK_LEADING_PATH, |
5a76aff1 | 565 | prefix, argv); |
366bfcb6 | 566 | |
bebb5d6d AM |
567 | if (pathspec_from_file) { |
568 | if (pathspec.nr) | |
246cac85 | 569 | die(_("'%s' and pathspec arguments cannot be used together"), "--pathspec-from-file"); |
bebb5d6d AM |
570 | |
571 | parse_pathspec_file(&pathspec, PATHSPEC_ATTR, | |
572 | PATHSPEC_PREFER_FULL | | |
573 | PATHSPEC_SYMLINK_LEADING_PATH, | |
574 | prefix, pathspec_from_file, pathspec_file_nul); | |
575 | } else if (pathspec_file_nul) { | |
6fa00ee8 | 576 | die(_("the option '%s' requires '%s'"), "--pathspec-file-nul", "--pathspec-from-file"); |
bebb5d6d AM |
577 | } |
578 | ||
579 | if (require_pathspec && pathspec.nr == 0) { | |
21bb3083 | 580 | fprintf(stderr, _("Nothing specified, nothing added.\n")); |
ed9bff08 | 581 | if (advice_enabled(ADVICE_ADD_EMPTY_PATHSPEC)) |
887a0fd5 HW |
582 | advise( _("Maybe you wanted to say 'git add .'?\n" |
583 | "Turn this message off by running\n" | |
584 | "\"git config advice.addEmptyPathspec false\"")); | |
21bb3083 AM |
585 | return 0; |
586 | } | |
587 | ||
bebb5d6d | 588 | if (!take_worktree_changes && addremove_explicit < 0 && pathspec.nr) |
21bb3083 AM |
589 | /* Turn "git add pathspec..." to "git add -A pathspec..." */ |
590 | addremove = 1; | |
591 | ||
592 | flags = ((verbose ? ADD_CACHE_VERBOSE : 0) | | |
593 | (show_only ? ADD_CACHE_PRETEND : 0) | | |
594 | (intent_to_add ? ADD_CACHE_INTENT : 0) | | |
595 | (ignore_add_errors ? ADD_CACHE_IGNORE_ERRORS : 0) | | |
596 | (!(addremove || take_worktree_changes) | |
597 | ? ADD_CACHE_IGNORE_REMOVAL : 0)); | |
598 | ||
d1664e73 BP |
599 | if (read_cache_preload(&pathspec) < 0) |
600 | die(_("index file corrupt")); | |
601 | ||
602 | die_in_unpopulated_submodule(&the_index, prefix); | |
c08397e3 BW |
603 | die_path_inside_submodule(&the_index, &pathspec); |
604 | ||
1d8842d9 LT |
605 | if (add_new_files) { |
606 | int baselen; | |
607 | ||
608 | /* Set up the default git porcelain excludes */ | |
1d8842d9 LT |
609 | if (!ignored_too) { |
610 | dir.flags |= DIR_COLLECT_IGNORED; | |
611 | setup_standard_excludes(&dir); | |
612 | } | |
613 | ||
1e5f764c | 614 | /* This picks up the paths that are not tracked */ |
0d32c183 | 615 | baselen = fill_directory(&dir, &the_index, &pathspec); |
5a76aff1 | 616 | if (pathspec.nr) |
053a6b18 | 617 | seen = prune_directory(&dir, &pathspec, baselen); |
1d8842d9 | 618 | } |
1e5f764c | 619 | |
c972ec04 | 620 | if (refresh_only) { |
a20f7047 | 621 | exit_status |= refresh(verbose, &pathspec); |
c972ec04 | 622 | goto finish; |
6a1ad325 JH |
623 | } |
624 | ||
5a76aff1 | 625 | if (pathspec.nr) { |
81f45e7d | 626 | int i; |
a20f7047 MT |
627 | char *skip_worktree_seen = NULL; |
628 | struct string_list only_match_skip_worktree = STRING_LIST_INIT_NODUP; | |
eb69934b | 629 | |
81f45e7d | 630 | if (!seen) |
719630eb | 631 | seen = find_pathspecs_matching_against_index(&pathspec, |
a20f7047 | 632 | &the_index, PS_IGNORE_SKIP_WORKTREE); |
5a76aff1 NTND |
633 | |
634 | /* | |
635 | * file_exists() assumes exact match | |
636 | */ | |
bd30c2e4 NTND |
637 | GUARD_PATHSPEC(&pathspec, |
638 | PATHSPEC_FROMTOP | | |
639 | PATHSPEC_LITERAL | | |
93d93537 | 640 | PATHSPEC_GLOB | |
ef79b1f8 NTND |
641 | PATHSPEC_ICASE | |
642 | PATHSPEC_EXCLUDE); | |
5a76aff1 | 643 | |
84b8b5d1 NTND |
644 | for (i = 0; i < pathspec.nr; i++) { |
645 | const char *path = pathspec.items[i].match; | |
a20f7047 | 646 | |
ef79b1f8 NTND |
647 | if (pathspec.items[i].magic & PATHSPEC_EXCLUDE) |
648 | continue; | |
a20f7047 MT |
649 | if (seen[i]) |
650 | continue; | |
651 | ||
0299a696 DS |
652 | if (!include_sparse && |
653 | matches_skip_worktree(&pathspec, i, &skip_worktree_seen)) { | |
a20f7047 MT |
654 | string_list_append(&only_match_skip_worktree, |
655 | pathspec.items[i].original); | |
656 | continue; | |
657 | } | |
658 | ||
659 | /* Don't complain at 'git add .' on empty repo */ | |
660 | if (!path[0]) | |
661 | continue; | |
662 | ||
663 | if ((pathspec.items[i].magic & (PATHSPEC_GLOB | PATHSPEC_ICASE)) || | |
664 | !file_exists(path)) { | |
108da0db | 665 | if (ignore_missing) { |
0188f6b3 | 666 | int dtype = DT_UNKNOWN; |
a0bba65b | 667 | if (is_excluded(&dir, &the_index, path, &dtype)) |
9e58beca BW |
668 | dir_add_ignored(&dir, &the_index, |
669 | path, pathspec.items[i].len); | |
108da0db | 670 | } else |
48168851 | 671 | die(_("pathspec '%s' did not match any files"), |
84b8b5d1 | 672 | pathspec.items[i].original); |
108da0db | 673 | } |
81f45e7d | 674 | } |
a20f7047 MT |
675 | |
676 | ||
677 | if (only_match_skip_worktree.nr) { | |
678 | advise_on_updating_sparse_paths(&only_match_skip_worktree); | |
679 | exit_status = 1; | |
680 | } | |
681 | ||
81f45e7d | 682 | free(seen); |
a20f7047 MT |
683 | free(skip_worktree_seen); |
684 | string_list_clear(&only_match_skip_worktree, 0); | |
81f45e7d JH |
685 | } |
686 | ||
568508e7 JH |
687 | plug_bulk_checkin(); |
688 | ||
9472935d TB |
689 | if (add_renormalize) |
690 | exit_status |= renormalize_tracked_files(&pathspec, flags); | |
691 | else | |
692 | exit_status |= add_files_to_cache(prefix, &pathspec, flags); | |
c972ec04 JH |
693 | |
694 | if (add_new_files) | |
610d55af | 695 | exit_status |= add_files(&dir, flags); |
0d781539 | 696 | |
610d55af | 697 | if (chmod_arg && pathspec.nr) |
9ebd7fe1 | 698 | exit_status |= chmod_pathspec(&pathspec, chmod_arg[0], show_only); |
568508e7 JH |
699 | unplug_bulk_checkin(); |
700 | ||
d521abf8 | 701 | finish: |
61000814 MÅ |
702 | if (write_locked_index(&the_index, &lock_file, |
703 | COMMIT_LOCK | SKIP_IF_UNCHANGED)) | |
704 | die(_("Unable to write new index file")); | |
0d781539 | 705 | |
eceba532 | 706 | dir_clear(&dir); |
0e5bba53 | 707 | UNLEAK(pathspec); |
7ae02a30 | 708 | return exit_status; |
0d781539 | 709 | } |