]> git.ipfire.org Git - thirdparty/git.git/blame - builtin/add.c
RelNotes: minor wording fixes in 2.43.0 release notes
[thirdparty/git.git] / builtin / add.c
CommitLineData
0d781539
LT
1/*
2 * "git add" builtin command
3 *
4 * Copyright (C) 2006 Linus Torvalds
5 */
07047d68 6#define USE_THE_INDEX_VARIABLE
bc5c5ec0 7#include "builtin.h"
6c6ddf92 8#include "advice.h"
b2141fc1 9#include "config.h"
697cc8ef 10#include "lockfile.h"
4e120823 11#include "editor.h"
0d781539 12#include "dir.h"
f394e093 13#include "gettext.h"
6f525e71 14#include "pathspec.h"
d807c4a0 15#include "exec-cmd.h"
93872e07 16#include "cache-tree.h"
58680165 17#include "run-command.h"
5c46f754 18#include "parse-options.h"
c339932b 19#include "path.h"
fbffdfb1 20#include "preload-index.h"
c59cb03a 21#include "diff.h"
fb7d3f32 22#include "diffcore.h"
08c46a49 23#include "read-cache.h"
df6e8744 24#include "repository.h"
c59cb03a 25#include "revision.h"
568508e7 26#include "bulk-checkin.h"
dbbcd44f 27#include "strvec.h"
bdab9721 28#include "submodule.h"
f83dff60 29#include "add-interactive.h"
0d781539 30
5c46f754 31static const char * const builtin_add_usage[] = {
9c9b4f2f 32 N_("git add [<options>] [--] <pathspec>..."),
5c46f754
KH
33 NULL
34};
c59cb03a 35static int patch_interactive, add_interactive, edit_interactive;
93c44d49 36static int take_worktree_changes;
9472935d 37static int add_renormalize;
bebb5d6d 38static int pathspec_file_nul;
0299a696 39static int include_sparse;
bebb5d6d 40static const char *pathspec_from_file;
896bdfa2 41
9ebd7fe1 42static int chmod_pathspec(struct pathspec *pathspec, char flip, int show_only)
610d55af 43{
9ebd7fe1 44 int i, ret = 0;
610d55af 45
dc594180
ÆAB
46 for (i = 0; i < the_index.cache_nr; i++) {
47 struct cache_entry *ce = the_index.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 58 if (!show_only)
fbc1ed62 59 err = chmod_index_entry(&the_index, ce, flip);
c937d70b
MT
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
9472935d
TB
70static int renormalize_tracked_files(const struct pathspec *pathspec, int flags)
71{
72 int i, retval = 0;
73
dc594180
ÆAB
74 for (i = 0; i < the_index.cache_nr; i++) {
75 struct cache_entry *ce = the_index.cache[i];
9472935d 76
61d450f0
DS
77 if (!include_sparse &&
78 (ce_skip_worktree(ce) ||
79 !path_in_sparse_checkout(ce->name, &the_index)))
d73dbafc 80 continue;
9472935d
TB
81 if (ce_stage(ce))
82 continue; /* do not touch unmerged paths */
83 if (!S_ISREG(ce->ce_mode) && !S_ISLNK(ce->ce_mode))
84 continue; /* do not touch non blobs */
6d2df284 85 if (pathspec && !ce_path_match(&the_index, ce, pathspec, NULL))
9472935d 86 continue;
fbc1ed62
ÆAB
87 retval |= add_file_to_index(&the_index, ce->name,
88 flags | ADD_CACHE_RENORMALIZE);
9472935d
TB
89 }
90
91 return retval;
92}
93
053a6b18 94static char *prune_directory(struct dir_struct *dir, struct pathspec *pathspec, int prefix)
0d781539 95{
f2593398 96 char *seen;
84b8b5d1 97 int i;
0d781539
LT
98 struct dir_entry **src, **dst;
99
84b8b5d1 100 seen = xcalloc(pathspec->nr, 1);
f2593398 101
0d781539
LT
102 src = dst = dir->entries;
103 i = dir->nr;
104 while (--i >= 0) {
105 struct dir_entry *entry = *src++;
6d2df284 106 if (dir_path_match(&the_index, entry, pathspec, prefix, seen))
4d06f8ac 107 *dst++ = entry;
0d781539
LT
108 }
109 dir->nr = dst - dir->entries;
719630eb 110 add_pathspec_matches_against_index(pathspec, &the_index, seen,
a20f7047 111 PS_IGNORE_SKIP_WORKTREE);
81f45e7d 112 return seen;
0d781539
LT
113}
114
a20f7047 115static int refresh(int verbose, const struct pathspec *pathspec)
d616813d
AJ
116{
117 char *seen;
a20f7047
MT
118 int i, ret = 0;
119 char *skip_worktree_seen = NULL;
120 struct string_list only_match_skip_worktree = STRING_LIST_INIT_NODUP;
121 int flags = REFRESH_IGNORE_SKIP_WORKTREE |
122 (verbose ? REFRESH_IN_PORCELAIN : REFRESH_QUIET);
d616813d 123
9b2d6149 124 seen = xcalloc(pathspec->nr, 1);
a20f7047
MT
125 refresh_index(&the_index, flags, pathspec, seen,
126 _("Unstaged changes after refreshing the index:"));
9b2d6149 127 for (i = 0; i < pathspec->nr; i++) {
a20f7047 128 if (!seen[i]) {
939fa075 129 const char *path = pathspec->items[i].original;
939fa075
DS
130
131 if (matches_skip_worktree(pathspec, i, &skip_worktree_seen) ||
02155c8c 132 !path_in_sparse_checkout(path, &the_index)) {
a20f7047
MT
133 string_list_append(&only_match_skip_worktree,
134 pathspec->items[i].original);
135 } else {
136 die(_("pathspec '%s' did not match any files"),
137 pathspec->items[i].original);
138 }
139 }
140 }
141
142 if (only_match_skip_worktree.nr) {
143 advise_on_updating_sparse_paths(&only_match_skip_worktree);
144 ret = 1;
d616813d 145 }
a20f7047 146
ec36c42a 147 free(seen);
a20f7047
MT
148 free(skip_worktree_seen);
149 string_list_clear(&only_match_skip_worktree, 0);
150 return ret;
d616813d
AJ
151}
152
e885a84f 153int interactive_add(const char **argv, const char *prefix, int patch)
46b5139c 154{
5a76aff1 155 struct pathspec pathspec;
20b813d7
ÆAB
156 int unused;
157
158 if (!git_config_get_bool("add.interactive.usebuiltin", &unused))
159 warning(_("the add.interactive.useBuiltin setting has been removed!\n"
160 "See its entry in 'git help config' for details."));
46b5139c 161
625c3304 162 parse_pathspec(&pathspec, 0,
5a76aff1 163 PATHSPEC_PREFER_FULL |
480ca644
NTND
164 PATHSPEC_SYMLINK_LEADING_PATH |
165 PATHSPEC_PREFIX_ORIGIN,
5a76aff1 166 prefix, argv);
46b5139c 167
d21878f0
ÆAB
168 if (patch)
169 return !!run_add_p(the_repository, ADD_P_ADD, NULL, &pathspec);
170 else
171 return !!run_add_i(the_repository, &pathspec);
46b5139c
TR
172}
173
2af202be 174static int edit_patch(int argc, const char **argv, const char *prefix)
c59cb03a 175{
d292bfaf 176 char *file = git_pathdup("ADD_EDIT.patch");
d3180279 177 struct child_process child = CHILD_PROCESS_INIT;
c59cb03a
JS
178 struct rev_info rev;
179 int out;
180 struct stat st;
181
182 git_config(git_diff_basic_config, NULL); /* no "diff" UI options */
183
07047d68 184 if (repo_read_index(the_repository) < 0)
48399e9c 185 die(_("could not read the index"));
c59cb03a 186
2abf3503 187 repo_init_revisions(the_repository, &rev, prefix);
c59cb03a
JS
188 rev.diffopt.context = 7;
189
190 argc = setup_revisions(argc, argv, &rev, NULL);
191 rev.diffopt.output_format = DIFF_FORMAT_PATCH;
7f3b8c62 192 rev.diffopt.use_color = 0;
0d1e0e78 193 rev.diffopt.flags.ignore_dirty_submodules = 1;
66e905b7 194 out = xopen(file, O_CREAT | O_WRONLY | O_TRUNC, 0666);
41698375 195 rev.diffopt.file = xfdopen(out, "w");
c59cb03a 196 rev.diffopt.close_file = 1;
25bd3acd 197 run_diff_files(&rev, 0);
c59cb03a 198
cb64800d
JK
199 if (launch_editor(file, NULL, NULL))
200 die(_("editing patch failed"));
c59cb03a
JS
201
202 if (stat(file, &st))
48399e9c 203 die_errno(_("could not stat '%s'"), file);
c59cb03a 204 if (!st.st_size)
48399e9c 205 die(_("empty patch. aborted"));
c59cb03a 206
c59cb03a 207 child.git_cmd = 1;
2b709893
ÆAB
208 strvec_pushl(&child.args, "apply", "--recount", "--cached", file,
209 NULL);
c59cb03a 210 if (run_command(&child))
48399e9c 211 die(_("could not apply '%s'"), file);
c59cb03a
JS
212
213 unlink(file);
d292bfaf 214 free(file);
2108fe4a 215 release_revisions(&rev);
c59cb03a
JS
216 return 0;
217}
218
b39c53e6 219static const char ignore_error[] =
439fb829 220N_("The following paths are ignored by one of your .gitignore files:\n");
6a1ad325 221
300c0a22 222static int verbose, show_only, ignored_too, refresh_only;
45c45e30 223static int ignore_add_errors, intent_to_add, ignore_missing;
53213994 224static int warn_on_embedded_repo = 1;
45c45e30 225
fdc97abd 226#define ADDREMOVE_DEFAULT 1
45c45e30
JH
227static int addremove = ADDREMOVE_DEFAULT;
228static int addremove_explicit = -1; /* unspecified */
5c46f754 229
4e55ed32
ET
230static char *chmod_arg;
231
9f60f49b
JH
232static int ignore_removal_cb(const struct option *opt, const char *arg, int unset)
233{
abf2952f
JK
234 BUG_ON_OPT_ARG(arg);
235
9f60f49b
JH
236 /* if we are told to ignore, we are not adding removals */
237 *(int *)opt->value = !unset ? 0 : 1;
238 return 0;
239}
240
5c46f754 241static struct option builtin_add_options[] = {
1b56024c
NTND
242 OPT__DRY_RUN(&show_only, N_("dry run")),
243 OPT__VERBOSE(&verbose, N_("be verbose")),
5c46f754 244 OPT_GROUP(""),
300c0a22
JH
245 OPT_BOOL('i', "interactive", &add_interactive, N_("interactive picking")),
246 OPT_BOOL('p', "patch", &patch_interactive, N_("select hunks interactively")),
247 OPT_BOOL('e', "edit", &edit_interactive, N_("edit current diff and apply")),
1224781d 248 OPT__FORCE(&ignored_too, N_("allow adding otherwise ignored files"), 0),
300c0a22 249 OPT_BOOL('u', "update", &take_worktree_changes, N_("update tracked files")),
9472935d 250 OPT_BOOL(0, "renormalize", &add_renormalize, N_("renormalize EOL of tracked files (implies -u)")),
300c0a22 251 OPT_BOOL('N', "intent-to-add", &intent_to_add, N_("record only the fact that the path will be added later")),
45c45e30 252 OPT_BOOL('A', "all", &addremove_explicit, N_("add changes from all tracked and untracked files")),
203c8533 253 OPT_CALLBACK_F(0, "ignore-removal", &addremove_explicit,
9f60f49b
JH
254 NULL /* takes no arguments */,
255 N_("ignore paths removed in the working tree (same as --no-all)"),
203c8533 256 PARSE_OPT_NOARG, ignore_removal_cb),
300c0a22
JH
257 OPT_BOOL( 0 , "refresh", &refresh_only, N_("don't add, only refresh the index")),
258 OPT_BOOL( 0 , "ignore-errors", &ignore_add_errors, N_("just skip files which cannot be added because of errors")),
259 OPT_BOOL( 0 , "ignore-missing", &ignore_missing, N_("check if - even missing - files are ignored in dry run")),
0299a696 260 OPT_BOOL(0, "sparse", &include_sparse, N_("allow updating entries outside of the sparse-checkout cone")),
5f0df44c
RS
261 OPT_STRING(0, "chmod", &chmod_arg, "(+|-)x",
262 N_("override the executable bit of the listed files")),
53213994
JK
263 OPT_HIDDEN_BOOL(0, "warn-embedded-repo", &warn_on_embedded_repo,
264 N_("warn when adding an embedded repository")),
bebb5d6d
AM
265 OPT_PATHSPEC_FROM_FILE(&pathspec_from_file),
266 OPT_PATHSPEC_FILE_NUL(&pathspec_file_nul),
5c46f754
KH
267 OPT_END(),
268};
269
a4e7e317
GC
270static int add_config(const char *var, const char *value,
271 const struct config_context *ctx, void *cb)
dad25e4a 272{
8c2be75f
JN
273 if (!strcmp(var, "add.ignoreerrors") ||
274 !strcmp(var, "add.ignore-errors")) {
dad25e4a
AR
275 ignore_add_errors = git_config_bool(var, value);
276 return 0;
277 }
f83dff60 278
97eeeea2
GC
279 if (git_color_config(var, value, cb) < 0)
280 return -1;
281
a4e7e317 282 return git_default_config(var, value, ctx, cb);
dad25e4a
AR
283}
284
53213994
JK
285static const char embedded_advice[] = N_(
286"You've added another git repository inside your current repository.\n"
287"Clones of the outer repository will not contain the contents of\n"
288"the embedded repository and will not know how to obtain it.\n"
289"If you meant to add a submodule, use:\n"
290"\n"
291" git submodule add <url> %s\n"
292"\n"
293"If you added this path by mistake, you can remove it from the\n"
294"index with:\n"
295"\n"
296" git rm --cached %s\n"
297"\n"
298"See \"git help submodule\" for more information."
299);
300
301static void check_embedded_repo(const char *path)
302{
303 struct strbuf name = STRBUF_INIT;
c2a4b6d4 304 static int adviced_on_embedded_repo = 0;
53213994
JK
305
306 if (!warn_on_embedded_repo)
307 return;
308 if (!ends_with(path, "/"))
309 return;
310
311 /* Drop trailing slash for aesthetics */
312 strbuf_addstr(&name, path);
313 strbuf_strip_suffix(&name, "/");
314
315 warning(_("adding embedded git repository: %s"), name.buf);
c2a4b6d4
ÆAB
316 if (!adviced_on_embedded_repo &&
317 advice_enabled(ADVICE_ADD_EMBEDDED_REPO)) {
53213994 318 advise(embedded_advice, name.buf, name.buf);
c2a4b6d4 319 adviced_on_embedded_repo = 1;
53213994
JK
320 }
321
322 strbuf_release(&name);
323}
324
610d55af 325static int add_files(struct dir_struct *dir, int flags)
c972ec04
JH
326{
327 int i, exit_status = 0;
105e8b01 328 struct string_list matched_sparse_paths = STRING_LIST_INIT_NODUP;
c972ec04
JH
329
330 if (dir->ignored_nr) {
439fb829 331 fprintf(stderr, _(ignore_error));
c972ec04
JH
332 for (i = 0; i < dir->ignored_nr; i++)
333 fprintf(stderr, "%s\n", dir->ignored[i]->name);
ed9bff08 334 if (advice_enabled(ADVICE_ADD_IGNORED_FILE))
887a0fd5
HW
335 advise(_("Use -f if you really want to add them.\n"
336 "Turn this message off by running\n"
337 "\"git config advice.addIgnoredFile false\""));
1d31e5a2 338 exit_status = 1;
c972ec04
JH
339 }
340
53213994 341 for (i = 0; i < dir->nr; i++) {
0299a696
DS
342 if (!include_sparse &&
343 !path_in_sparse_checkout(dir->entries[i]->name, &the_index)) {
105e8b01
DS
344 string_list_append(&matched_sparse_paths,
345 dir->entries[i]->name);
346 continue;
347 }
610d55af 348 if (add_file_to_index(&the_index, dir->entries[i]->name, flags)) {
c972ec04 349 if (!ignore_add_errors)
990ac4be 350 die(_("adding files failed"));
c972ec04 351 exit_status = 1;
f937bc2f
KM
352 } else {
353 check_embedded_repo(dir->entries[i]->name);
c972ec04 354 }
53213994 355 }
105e8b01
DS
356
357 if (matched_sparse_paths.nr) {
358 advise_on_updating_sparse_paths(&matched_sparse_paths);
359 exit_status = 1;
360 }
361
362 string_list_clear(&matched_sparse_paths, 0);
363
c972ec04
JH
364 return exit_status;
365}
366
a633fca0 367int cmd_add(int argc, const char **argv, const char *prefix)
0d781539 368{
7ae02a30 369 int exit_status = 0;
5a76aff1 370 struct pathspec pathspec;
ce93a4c6 371 struct dir_struct dir = DIR_INIT;
610d55af 372 int flags;
c972ec04
JH
373 int add_new_files;
374 int require_pathspec;
81f45e7d 375 char *seen = NULL;
0fa5a2ed 376 struct lock_file lock_file = LOCK_INIT;
5cde71d6 377
ed342fde
SB
378 git_config(add_config, NULL);
379
37782920 380 argc = parse_options(argc, argv, prefix, builtin_add_options,
c59cb03a 381 builtin_add_usage, PARSE_OPT_KEEP_ARGV0);
b63e9950
WC
382 if (patch_interactive)
383 add_interactive = 1;
bebb5d6d 384 if (add_interactive) {
a1989cf7 385 if (show_only)
12909b6b 386 die(_("options '%s' and '%s' cannot be used together"), "--dry-run", "--interactive/--patch");
bebb5d6d 387 if (pathspec_from_file)
12909b6b 388 die(_("options '%s' and '%s' cannot be used together"), "--pathspec-from-file", "--interactive/--patch");
e885a84f 389 exit(interactive_add(argv + 1, prefix, patch_interactive));
bebb5d6d 390 }
0d781539 391
bebb5d6d
AM
392 if (edit_interactive) {
393 if (pathspec_from_file)
12909b6b 394 die(_("options '%s' and '%s' cannot be used together"), "--pathspec-from-file", "--edit");
c59cb03a 395 return(edit_patch(argc, argv, prefix));
bebb5d6d 396 }
c59cb03a
JS
397 argc--;
398 argv++;
399
45c45e30
JH
400 if (0 <= addremove_explicit)
401 addremove = addremove_explicit;
402 else if (take_worktree_changes && ADDREMOVE_DEFAULT)
403 addremove = 0; /* "-u" was given but not "-A" */
404
3ba1f114 405 if (addremove && take_worktree_changes)
12909b6b 406 die(_("options '%s' and '%s' cannot be used together"), "-A", "-u");
45c45e30 407
108da0db 408 if (!show_only && ignore_missing)
6fa00ee8 409 die(_("the option '%s' requires '%s'"), "--ignore-missing", "--dry-run");
808d3d71 410
610d55af
TG
411 if (chmod_arg && ((chmod_arg[0] != '-' && chmod_arg[0] != '+') ||
412 chmod_arg[1] != 'x' || chmod_arg[2]))
4e55ed32
ET
413 die(_("--chmod param '%s' must be either -x or +x"), chmod_arg);
414
9472935d 415 add_new_files = !take_worktree_changes && !refresh_only && !add_renormalize;
29abb339 416 require_pathspec = !(take_worktree_changes || (0 < addremove_explicit));
c972ec04 417
5e7cbab1
DS
418 prepare_repo_settings(the_repository);
419 the_repository->settings.command_requires_full_index = 0;
420
07047d68 421 repo_hold_locked_index(the_repository, &lock_file, LOCK_DIE_ON_ERROR);
0d781539 422
5a76aff1
NTND
423 /*
424 * Check the "pathspec '%s' did not match any files" block
425 * below before enabling new magic.
426 */
84d938b7 427 parse_pathspec(&pathspec, PATHSPEC_ATTR,
5a76aff1 428 PATHSPEC_PREFER_FULL |
c08397e3 429 PATHSPEC_SYMLINK_LEADING_PATH,
5a76aff1 430 prefix, argv);
366bfcb6 431
bebb5d6d
AM
432 if (pathspec_from_file) {
433 if (pathspec.nr)
246cac85 434 die(_("'%s' and pathspec arguments cannot be used together"), "--pathspec-from-file");
bebb5d6d
AM
435
436 parse_pathspec_file(&pathspec, PATHSPEC_ATTR,
437 PATHSPEC_PREFER_FULL |
438 PATHSPEC_SYMLINK_LEADING_PATH,
439 prefix, pathspec_from_file, pathspec_file_nul);
440 } else if (pathspec_file_nul) {
6fa00ee8 441 die(_("the option '%s' requires '%s'"), "--pathspec-file-nul", "--pathspec-from-file");
bebb5d6d
AM
442 }
443
444 if (require_pathspec && pathspec.nr == 0) {
21bb3083 445 fprintf(stderr, _("Nothing specified, nothing added.\n"));
ed9bff08 446 if (advice_enabled(ADVICE_ADD_EMPTY_PATHSPEC))
887a0fd5
HW
447 advise( _("Maybe you wanted to say 'git add .'?\n"
448 "Turn this message off by running\n"
449 "\"git config advice.addEmptyPathspec false\""));
21bb3083
AM
450 return 0;
451 }
452
bebb5d6d 453 if (!take_worktree_changes && addremove_explicit < 0 && pathspec.nr)
21bb3083
AM
454 /* Turn "git add pathspec..." to "git add -A pathspec..." */
455 addremove = 1;
456
457 flags = ((verbose ? ADD_CACHE_VERBOSE : 0) |
458 (show_only ? ADD_CACHE_PRETEND : 0) |
459 (intent_to_add ? ADD_CACHE_INTENT : 0) |
460 (ignore_add_errors ? ADD_CACHE_IGNORE_ERRORS : 0) |
461 (!(addremove || take_worktree_changes)
462 ? ADD_CACHE_IGNORE_REMOVAL : 0));
463
07047d68 464 if (repo_read_index_preload(the_repository, &pathspec, 0) < 0)
d1664e73
BP
465 die(_("index file corrupt"));
466
467 die_in_unpopulated_submodule(&the_index, prefix);
c08397e3
BW
468 die_path_inside_submodule(&the_index, &pathspec);
469
1d8842d9
LT
470 if (add_new_files) {
471 int baselen;
472
473 /* Set up the default git porcelain excludes */
1d8842d9
LT
474 if (!ignored_too) {
475 dir.flags |= DIR_COLLECT_IGNORED;
476 setup_standard_excludes(&dir);
477 }
478
1e5f764c 479 /* This picks up the paths that are not tracked */
0d32c183 480 baselen = fill_directory(&dir, &the_index, &pathspec);
5a76aff1 481 if (pathspec.nr)
053a6b18 482 seen = prune_directory(&dir, &pathspec, baselen);
1d8842d9 483 }
1e5f764c 484
c972ec04 485 if (refresh_only) {
a20f7047 486 exit_status |= refresh(verbose, &pathspec);
c972ec04 487 goto finish;
6a1ad325
JH
488 }
489
5a76aff1 490 if (pathspec.nr) {
81f45e7d 491 int i;
a20f7047
MT
492 char *skip_worktree_seen = NULL;
493 struct string_list only_match_skip_worktree = STRING_LIST_INIT_NODUP;
eb69934b 494
81f45e7d 495 if (!seen)
719630eb 496 seen = find_pathspecs_matching_against_index(&pathspec,
a20f7047 497 &the_index, PS_IGNORE_SKIP_WORKTREE);
5a76aff1
NTND
498
499 /*
500 * file_exists() assumes exact match
501 */
bd30c2e4
NTND
502 GUARD_PATHSPEC(&pathspec,
503 PATHSPEC_FROMTOP |
504 PATHSPEC_LITERAL |
93d93537 505 PATHSPEC_GLOB |
ef79b1f8
NTND
506 PATHSPEC_ICASE |
507 PATHSPEC_EXCLUDE);
5a76aff1 508
84b8b5d1
NTND
509 for (i = 0; i < pathspec.nr; i++) {
510 const char *path = pathspec.items[i].match;
a20f7047 511
ef79b1f8
NTND
512 if (pathspec.items[i].magic & PATHSPEC_EXCLUDE)
513 continue;
a20f7047
MT
514 if (seen[i])
515 continue;
516
0299a696
DS
517 if (!include_sparse &&
518 matches_skip_worktree(&pathspec, i, &skip_worktree_seen)) {
a20f7047
MT
519 string_list_append(&only_match_skip_worktree,
520 pathspec.items[i].original);
521 continue;
522 }
523
524 /* Don't complain at 'git add .' on empty repo */
525 if (!path[0])
526 continue;
527
528 if ((pathspec.items[i].magic & (PATHSPEC_GLOB | PATHSPEC_ICASE)) ||
529 !file_exists(path)) {
108da0db 530 if (ignore_missing) {
0188f6b3 531 int dtype = DT_UNKNOWN;
a0bba65b 532 if (is_excluded(&dir, &the_index, path, &dtype))
9e58beca
BW
533 dir_add_ignored(&dir, &the_index,
534 path, pathspec.items[i].len);
108da0db 535 } else
48168851 536 die(_("pathspec '%s' did not match any files"),
84b8b5d1 537 pathspec.items[i].original);
108da0db 538 }
81f45e7d 539 }
a20f7047
MT
540
541
542 if (only_match_skip_worktree.nr) {
543 advise_on_updating_sparse_paths(&only_match_skip_worktree);
544 exit_status = 1;
545 }
546
81f45e7d 547 free(seen);
a20f7047
MT
548 free(skip_worktree_seen);
549 string_list_clear(&only_match_skip_worktree, 0);
81f45e7d
JH
550 }
551
2c23d1b4 552 begin_odb_transaction();
568508e7 553
9472935d
TB
554 if (add_renormalize)
555 exit_status |= renormalize_tracked_files(&pathspec, flags);
556 else
50c37ee8
EN
557 exit_status |= add_files_to_cache(the_repository, prefix,
558 &pathspec, include_sparse,
559 flags);
c972ec04
JH
560
561 if (add_new_files)
610d55af 562 exit_status |= add_files(&dir, flags);
0d781539 563
610d55af 564 if (chmod_arg && pathspec.nr)
9ebd7fe1 565 exit_status |= chmod_pathspec(&pathspec, chmod_arg[0], show_only);
2c23d1b4 566 end_odb_transaction();
568508e7 567
d521abf8 568finish:
61000814
569 if (write_locked_index(&the_index, &lock_file,
570 COMMIT_LOCK | SKIP_IF_UNCHANGED))
48399e9c 571 die(_("unable to write new index file"));
0d781539 572
eceba532 573 dir_clear(&dir);
ac95f5d3 574 clear_pathspec(&pathspec);
7ae02a30 575 return exit_status;
0d781539 576}