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