]> git.ipfire.org Git - thirdparty/git.git/blame - builtin/add.c
add: implement the --sparse option
[thirdparty/git.git] / builtin / add.c
CommitLineData
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 25static const char * const builtin_add_usage[] = {
9c9b4f2f 26 N_("git add [<options>] [--] <pathspec>..."),
5c46f754
KH
27 NULL
28};
c59cb03a 29static int patch_interactive, add_interactive, edit_interactive;
93c44d49 30static int take_worktree_changes;
9472935d 31static int add_renormalize;
bebb5d6d 32static int pathspec_file_nul;
0299a696 33static int include_sparse;
bebb5d6d 34static const char *pathspec_from_file;
90a6bb98 35static int legacy_stash_p; /* support for the scripted `git stash` */
896bdfa2 36
9cba13ca 37struct update_callback_data {
610d55af 38 int flags;
fb7d3f32
LT
39 int add_errors;
40};
41
9ebd7fe1 42static 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
0299a696 50 if (!include_sparse && ce_skip_worktree(ce))
d73dbafc
MT
51 continue;
52
6d2df284 53 if (pathspec && !ce_path_match(&the_index, ce, pathspec, NULL))
610d55af
TG
54 continue;
55
c937d70b
MT
56 if (!show_only)
57 err = chmod_cache_entry(ce, flip);
58 else
59 err = S_ISREG(ce->ce_mode) ? 0 : -1;
60
61 if (err < 0)
9ebd7fe1 62 ret = error(_("cannot chmod %cx '%s'"), flip, ce->name);
610d55af 63 }
9ebd7fe1
MT
64
65 return ret;
610d55af
TG
66}
67
75973b2c
JH
68static int fix_unmerged_status(struct diff_filepair *p,
69 struct update_callback_data *data)
70{
71 if (p->status != DIFF_STATUS_UNMERGED)
72 return p->status;
73 if (!(data->flags & ADD_CACHE_IGNORE_REMOVAL) && !p->two->mode)
74 /*
75 * This is not an explicit add request, and the
76 * path is missing from the working tree (deleted)
77 */
78 return DIFF_STATUS_DELETED;
79 else
80 /*
81 * Either an explicit add request, or path exists
82 * in the working tree. An attempt to explicitly
83 * add a path that does not exist in the working tree
84 * will be caught as an error by the caller immediately.
85 */
86 return DIFF_STATUS_MODIFIED;
87}
88
fb7d3f32
LT
89static void update_callback(struct diff_queue_struct *q,
90 struct diff_options *opt, void *cbdata)
91{
92 int i;
93 struct update_callback_data *data = cbdata;
94
95 for (i = 0; i < q->nr; i++) {
96 struct diff_filepair *p = q->queue[i];
97 const char *path = p->one->path;
49fdd51a 98
0299a696 99 if (!include_sparse && !path_in_sparse_checkout(path, &the_index))
49fdd51a
DS
100 continue;
101
75973b2c 102 switch (fix_unmerged_status(p, data)) {
fb7d3f32 103 default:
990ac4be 104 die(_("unexpected diff status %c"), p->status);
fb7d3f32
LT
105 case DIFF_STATUS_MODIFIED:
106 case DIFF_STATUS_TYPE_CHANGED:
610d55af 107 if (add_file_to_index(&the_index, path, data->flags)) {
fb7d3f32 108 if (!(data->flags & ADD_CACHE_IGNORE_ERRORS))
990ac4be 109 die(_("updating files failed"));
fb7d3f32
LT
110 data->add_errors++;
111 }
112 break;
113 case DIFF_STATUS_DELETED:
114 if (data->flags & ADD_CACHE_IGNORE_REMOVAL)
115 break;
116 if (!(data->flags & ADD_CACHE_PRETEND))
117 remove_file_from_index(&the_index, path);
118 if (data->flags & (ADD_CACHE_PRETEND|ADD_CACHE_VERBOSE))
475c73eb 119 printf(_("remove '%s'\n"), path);
fb7d3f32
LT
120 break;
121 }
122 }
123}
124
610d55af
TG
125int add_files_to_cache(const char *prefix,
126 const struct pathspec *pathspec, int flags)
fb7d3f32 127{
160c4b18 128 struct update_callback_data data;
fb7d3f32 129 struct rev_info rev;
71c7b053 130
160c4b18
JH
131 memset(&data, 0, sizeof(data));
132 data.flags = flags;
133
2abf3503 134 repo_init_revisions(the_repository, &rev, prefix);
fb7d3f32 135 setup_revisions(0, NULL, &rev, NULL);
3efe8e43
NTND
136 if (pathspec)
137 copy_pathspec(&rev.prune_data, pathspec);
fb7d3f32
LT
138 rev.diffopt.output_format = DIFF_FORMAT_CALLBACK;
139 rev.diffopt.format_callback = update_callback;
160c4b18 140 rev.diffopt.format_callback_data = &data;
0d1e0e78 141 rev.diffopt.flags.override_submodule_config = 1;
75973b2c 142 rev.max_count = 0; /* do not compare unmerged paths with stage #2 */
fb7d3f32 143 run_diff_files(&rev, DIFF_RACY_IS_MODIFIED);
fe6a01af 144 clear_pathspec(&rev.prune_data);
fb7d3f32
LT
145 return !!data.add_errors;
146}
147
9472935d
TB
148static int renormalize_tracked_files(const struct pathspec *pathspec, int flags)
149{
150 int i, retval = 0;
151
152 for (i = 0; i < active_nr; i++) {
153 struct cache_entry *ce = active_cache[i];
154
d73dbafc
MT
155 if (ce_skip_worktree(ce))
156 continue;
9472935d
TB
157 if (ce_stage(ce))
158 continue; /* do not touch unmerged paths */
159 if (!S_ISREG(ce->ce_mode) && !S_ISLNK(ce->ce_mode))
160 continue; /* do not touch non blobs */
6d2df284 161 if (pathspec && !ce_path_match(&the_index, ce, pathspec, NULL))
9472935d 162 continue;
9e5da3d0 163 retval |= add_file_to_cache(ce->name, flags | ADD_CACHE_RENORMALIZE);
9472935d
TB
164 }
165
166 return retval;
167}
168
053a6b18 169static char *prune_directory(struct dir_struct *dir, struct pathspec *pathspec, int prefix)
0d781539 170{
f2593398 171 char *seen;
84b8b5d1 172 int i;
0d781539
LT
173 struct dir_entry **src, **dst;
174
84b8b5d1 175 seen = xcalloc(pathspec->nr, 1);
f2593398 176
0d781539
LT
177 src = dst = dir->entries;
178 i = dir->nr;
179 while (--i >= 0) {
180 struct dir_entry *entry = *src++;
6d2df284 181 if (dir_path_match(&the_index, entry, pathspec, prefix, seen))
4d06f8ac 182 *dst++ = entry;
0d781539
LT
183 }
184 dir->nr = dst - dir->entries;
719630eb 185 add_pathspec_matches_against_index(pathspec, &the_index, seen,
a20f7047 186 PS_IGNORE_SKIP_WORKTREE);
81f45e7d 187 return seen;
0d781539
LT
188}
189
a20f7047 190static int refresh(int verbose, const struct pathspec *pathspec)
d616813d
AJ
191{
192 char *seen;
a20f7047
MT
193 int i, ret = 0;
194 char *skip_worktree_seen = NULL;
195 struct string_list only_match_skip_worktree = STRING_LIST_INIT_NODUP;
196 int flags = REFRESH_IGNORE_SKIP_WORKTREE |
197 (verbose ? REFRESH_IN_PORCELAIN : REFRESH_QUIET);
d616813d 198
9b2d6149 199 seen = xcalloc(pathspec->nr, 1);
a20f7047
MT
200 refresh_index(&the_index, flags, pathspec, seen,
201 _("Unstaged changes after refreshing the index:"));
9b2d6149 202 for (i = 0; i < pathspec->nr; i++) {
a20f7047 203 if (!seen[i]) {
939fa075 204 const char *path = pathspec->items[i].original;
939fa075
DS
205
206 if (matches_skip_worktree(pathspec, i, &skip_worktree_seen) ||
02155c8c 207 !path_in_sparse_checkout(path, &the_index)) {
a20f7047
MT
208 string_list_append(&only_match_skip_worktree,
209 pathspec->items[i].original);
210 } else {
211 die(_("pathspec '%s' did not match any files"),
212 pathspec->items[i].original);
213 }
214 }
215 }
216
217 if (only_match_skip_worktree.nr) {
218 advise_on_updating_sparse_paths(&only_match_skip_worktree);
219 ret = 1;
d616813d 220 }
a20f7047 221
ec36c42a 222 free(seen);
a20f7047
MT
223 free(skip_worktree_seen);
224 string_list_clear(&only_match_skip_worktree, 0);
225 return ret;
d616813d
AJ
226}
227
46b5139c 228int run_add_interactive(const char *revision, const char *patch_mode,
480ca644 229 const struct pathspec *pathspec)
58680165 230{
c45a18e8 231 int status, i;
22f9b7f3 232 struct strvec argv = STRVEC_INIT;
f83dff60
JS
233 int use_builtin_add_i =
234 git_env_bool("GIT_TEST_ADD_I_USE_BUILTIN", -1);
235
2df2d81d
JH
236 if (use_builtin_add_i < 0) {
237 int experimental;
238 if (!git_config_get_bool("add.interactive.usebuiltin",
239 &use_builtin_add_i))
240 ; /* ok */
241 else if (!git_config_get_bool("feature.experimental", &experimental) &&
242 experimental)
243 use_builtin_add_i = 1;
244 }
f6aa7ecc
JS
245
246 if (use_builtin_add_i == 1) {
d2a233cb
JS
247 enum add_p_mode mode;
248
f6aa7ecc 249 if (!patch_mode)
f83dff60 250 return !!run_add_i(the_repository, pathspec);
d2a233cb
JS
251
252 if (!strcmp(patch_mode, "--patch"))
253 mode = ADD_P_ADD;
36bae1dc
JS
254 else if (!strcmp(patch_mode, "--patch=stash"))
255 mode = ADD_P_STASH;
256 else if (!strcmp(patch_mode, "--patch=reset"))
257 mode = ADD_P_RESET;
52628f94
JS
258 else if (!strcmp(patch_mode, "--patch=checkout"))
259 mode = ADD_P_CHECKOUT;
cee6cb73
JS
260 else if (!strcmp(patch_mode, "--patch=worktree"))
261 mode = ADD_P_WORKTREE;
d2a233cb 262 else
52628f94 263 die("'%s' not supported", patch_mode);
d2a233cb
JS
264
265 return !!run_add_p(the_repository, mode, revision, pathspec);
f83dff60 266 }
3f061887 267
22f9b7f3 268 strvec_push(&argv, "add--interactive");
46b5139c 269 if (patch_mode)
22f9b7f3 270 strvec_push(&argv, patch_mode);
46b5139c 271 if (revision)
22f9b7f3
JK
272 strvec_push(&argv, revision);
273 strvec_push(&argv, "--");
480ca644
NTND
274 for (i = 0; i < pathspec->nr; i++)
275 /* pass original pathspec, to be re-parsed */
22f9b7f3 276 strvec_push(&argv, pathspec->items[i].original);
7c0ab445 277
d70a9eb6 278 status = run_command_v_opt(argv.v, RUN_GIT_CMD);
22f9b7f3 279 strvec_clear(&argv);
7c0ab445 280 return status;
58680165
KH
281}
282
e885a84f 283int interactive_add(const char **argv, const char *prefix, int patch)
46b5139c 284{
5a76aff1 285 struct pathspec pathspec;
46b5139c 286
625c3304 287 parse_pathspec(&pathspec, 0,
5a76aff1 288 PATHSPEC_PREFER_FULL |
480ca644
NTND
289 PATHSPEC_SYMLINK_LEADING_PATH |
290 PATHSPEC_PREFIX_ORIGIN,
5a76aff1 291 prefix, argv);
46b5139c
TR
292
293 return run_add_interactive(NULL,
b4bd4668 294 patch ? "--patch" : NULL,
480ca644 295 &pathspec);
46b5139c
TR
296}
297
2af202be 298static int edit_patch(int argc, const char **argv, const char *prefix)
c59cb03a 299{
d292bfaf 300 char *file = git_pathdup("ADD_EDIT.patch");
c59cb03a 301 const char *apply_argv[] = { "apply", "--recount", "--cached",
66dbfd55 302 NULL, NULL };
d3180279 303 struct child_process child = CHILD_PROCESS_INIT;
c59cb03a
JS
304 struct rev_info rev;
305 int out;
306 struct stat st;
307
66dbfd55
GV
308 apply_argv[3] = file;
309
c59cb03a
JS
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;
fa6f225e 322 out = open(file, O_CREAT | O_WRONLY | O_TRUNC, 0666);
c59cb03a 323 if (out < 0)
d521abf8 324 die(_("Could not open '%s' for writing."), file);
41698375 325 rev.diffopt.file = xfdopen(out, "w");
c59cb03a
JS
326 rev.diffopt.close_file = 1;
327 if (run_diff_files(&rev, 0))
d521abf8 328 die(_("Could not write patch"));
c59cb03a 329
cb64800d
JK
330 if (launch_editor(file, NULL, NULL))
331 die(_("editing patch failed"));
c59cb03a
JS
332
333 if (stat(file, &st))
990ac4be 334 die_errno(_("Could not stat '%s'"), file);
c59cb03a 335 if (!st.st_size)
990ac4be 336 die(_("Empty patch. Aborted."));
c59cb03a 337
c59cb03a
JS
338 child.git_cmd = 1;
339 child.argv = apply_argv;
340 if (run_command(&child))
d521abf8 341 die(_("Could not apply '%s'"), file);
c59cb03a
JS
342
343 unlink(file);
d292bfaf 344 free(file);
c59cb03a
JS
345 return 0;
346}
347
b39c53e6 348static const char ignore_error[] =
439fb829 349N_("The following paths are ignored by one of your .gitignore files:\n");
6a1ad325 350
300c0a22 351static int verbose, show_only, ignored_too, refresh_only;
45c45e30 352static int ignore_add_errors, intent_to_add, ignore_missing;
53213994 353static int warn_on_embedded_repo = 1;
45c45e30 354
fdc97abd 355#define ADDREMOVE_DEFAULT 1
45c45e30
JH
356static int addremove = ADDREMOVE_DEFAULT;
357static int addremove_explicit = -1; /* unspecified */
5c46f754 358
4e55ed32
ET
359static char *chmod_arg;
360
9f60f49b
JH
361static int ignore_removal_cb(const struct option *opt, const char *arg, int unset)
362{
363 /* if we are told to ignore, we are not adding removals */
364 *(int *)opt->value = !unset ? 0 : 1;
365 return 0;
366}
367
5c46f754 368static struct option builtin_add_options[] = {
1b56024c
NTND
369 OPT__DRY_RUN(&show_only, N_("dry run")),
370 OPT__VERBOSE(&verbose, N_("be verbose")),
5c46f754 371 OPT_GROUP(""),
300c0a22
JH
372 OPT_BOOL('i', "interactive", &add_interactive, N_("interactive picking")),
373 OPT_BOOL('p', "patch", &patch_interactive, N_("select hunks interactively")),
374 OPT_BOOL('e', "edit", &edit_interactive, N_("edit current diff and apply")),
1224781d 375 OPT__FORCE(&ignored_too, N_("allow adding otherwise ignored files"), 0),
300c0a22 376 OPT_BOOL('u', "update", &take_worktree_changes, N_("update tracked files")),
9472935d 377 OPT_BOOL(0, "renormalize", &add_renormalize, N_("renormalize EOL of tracked files (implies -u)")),
300c0a22 378 OPT_BOOL('N', "intent-to-add", &intent_to_add, N_("record only the fact that the path will be added later")),
45c45e30 379 OPT_BOOL('A', "all", &addremove_explicit, N_("add changes from all tracked and untracked files")),
203c8533 380 OPT_CALLBACK_F(0, "ignore-removal", &addremove_explicit,
9f60f49b
JH
381 NULL /* takes no arguments */,
382 N_("ignore paths removed in the working tree (same as --no-all)"),
203c8533 383 PARSE_OPT_NOARG, ignore_removal_cb),
300c0a22
JH
384 OPT_BOOL( 0 , "refresh", &refresh_only, N_("don't add, only refresh the index")),
385 OPT_BOOL( 0 , "ignore-errors", &ignore_add_errors, N_("just skip files which cannot be added because of errors")),
386 OPT_BOOL( 0 , "ignore-missing", &ignore_missing, N_("check if - even missing - files are ignored in dry run")),
0299a696 387 OPT_BOOL(0, "sparse", &include_sparse, N_("allow updating entries outside of the sparse-checkout cone")),
5f0df44c
RS
388 OPT_STRING(0, "chmod", &chmod_arg, "(+|-)x",
389 N_("override the executable bit of the listed files")),
53213994
JK
390 OPT_HIDDEN_BOOL(0, "warn-embedded-repo", &warn_on_embedded_repo,
391 N_("warn when adding an embedded repository")),
90a6bb98
JS
392 OPT_HIDDEN_BOOL(0, "legacy-stash-p", &legacy_stash_p,
393 N_("backend for `git stash -p`")),
bebb5d6d
AM
394 OPT_PATHSPEC_FROM_FILE(&pathspec_from_file),
395 OPT_PATHSPEC_FILE_NUL(&pathspec_file_nul),
5c46f754
KH
396 OPT_END(),
397};
398
9bd81e42 399static int add_config(const char *var, const char *value, void *cb)
dad25e4a 400{
8c2be75f
JN
401 if (!strcmp(var, "add.ignoreerrors") ||
402 !strcmp(var, "add.ignore-errors")) {
dad25e4a
AR
403 ignore_add_errors = git_config_bool(var, value);
404 return 0;
405 }
f83dff60 406
9bd81e42 407 return git_default_config(var, value, cb);
dad25e4a
AR
408}
409
53213994
JK
410static const char embedded_advice[] = N_(
411"You've added another git repository inside your current repository.\n"
412"Clones of the outer repository will not contain the contents of\n"
413"the embedded repository and will not know how to obtain it.\n"
414"If you meant to add a submodule, use:\n"
415"\n"
416" git submodule add <url> %s\n"
417"\n"
418"If you added this path by mistake, you can remove it from the\n"
419"index with:\n"
420"\n"
421" git rm --cached %s\n"
422"\n"
423"See \"git help submodule\" for more information."
424);
425
426static void check_embedded_repo(const char *path)
427{
428 struct strbuf name = STRBUF_INIT;
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);
440 if (advice_add_embedded_repo) {
441 advise(embedded_advice, name.buf, name.buf);
442 /* there may be multiple entries; advise only once */
443 advice_add_embedded_repo = 0;
444 }
445
446 strbuf_release(&name);
447}
448
610d55af 449static 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);
887a0fd5
HW
458 if (advice_add_ignored_file)
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 491int 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
ØW
509 if (show_only)
510 die(_("--dry-run is incompatible with --interactive/--patch"));
bebb5d6d
AM
511 if (pathspec_from_file)
512 die(_("--pathspec-from-file is incompatible with --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)
529 die(_("--pathspec-from-file is incompatible with --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)
990ac4be 541 die(_("-A and -u are mutually incompatible"));
45c45e30 542
108da0db 543 if (!show_only && ignore_missing)
990ac4be 544 die(_("Option --ignore-missing can only be used together with --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)
569 die(_("--pathspec-from-file is incompatible with pathspec arguments"));
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) {
576 die(_("--pathspec-file-nul requires --pathspec-from-file"));
577 }
578
579 if (require_pathspec && pathspec.nr == 0) {
21bb3083 580 fprintf(stderr, _("Nothing specified, nothing added.\n"));
887a0fd5
HW
581 if (advice_add_empty_pathspec)
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 701finish:
61000814
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}