Raw
1 /*
2 * "git add" builtin command
3 *
4 * Copyright (C) 2006 Linus Torvalds
5 */
6
7 #include "builtin.h"
8 #include "advice.h"
9 #include "config.h"
10 #include "environment.h"
11 #include "lockfile.h"
12 #include "editor.h"
13 #include "dir.h"
14 #include "gettext.h"
15 #include "pathspec.h"
16 #include "run-command.h"
17 #include "object-file.h"
18 #include "odb.h"
19 #include "odb/transaction.h"
20 #include "parse-options.h"
21 #include "path.h"
22 #include "preload-index.h"
23 #include "diff.h"
24 #include "read-cache.h"
25 #include "revision.h"
26 #include "strvec.h"
27 #include "submodule.h"
28 #include "add-interactive.h"
29 #include "merge-ll.h"
30
31 static const char * const builtin_add_usage[] = {
32 N_("git add [<options>] [--] <pathspec>..."),
33 NULL
34 };
35 static int patch_interactive, add_interactive, edit_interactive;
36 static struct interactive_options interactive_opts = INTERACTIVE_OPTIONS_INIT;
37 static int take_worktree_changes;
38 static int add_renormalize;
39 static int add_resolved;
40 static int pathspec_file_nul;
41 static int include_sparse;
42 static const char *pathspec_from_file;
43
44 static int chmod_pathspec(struct repository *repo,
45 struct pathspec *pathspec,
46 char flip,
47 int show_only)
48 {
49 int ret = 0;
50
51 for (size_t i = 0; i < repo->index->cache_nr; i++) {
52 struct cache_entry *ce = repo->index->cache[i];
53 int err;
54
55 if (!include_sparse &&
56 (ce_skip_worktree(ce) ||
57 !path_in_sparse_checkout(ce->name, repo->index)))
58 continue;
59
60 if (pathspec && !ce_path_match(repo->index, ce, pathspec, NULL))
61 continue;
62
63 if (!show_only)
64 err = chmod_index_entry(repo->index, ce, flip);
65 else
66 err = S_ISREG(ce->ce_mode) ? 0 : -1;
67
68 if (err < 0)
69 ret = error(_("cannot chmod %cx '%s'"), flip, ce->name);
70 }
71
72 return ret;
73 }
74
75 static int renormalize_tracked_files(struct repository *repo,
76 const struct pathspec *pathspec,
77 int flags)
78 {
79 int retval = 0;
80
81 for (size_t i = 0; i < repo->index->cache_nr; i++) {
82 struct cache_entry *ce = repo->index->cache[i];
83
84 if (!include_sparse &&
85 (ce_skip_worktree(ce) ||
86 !path_in_sparse_checkout(ce->name, repo->index)))
87 continue;
88 if (ce_stage(ce))
89 continue; /* do not touch unmerged paths */
90 if (!S_ISREG(ce->ce_mode) && !S_ISLNK(ce->ce_mode))
91 continue; /* do not touch non blobs */
92 if (pathspec && !ce_path_match(repo->index, ce, pathspec, NULL))
93 continue;
94 retval |= add_file_to_index(repo->index, ce->name,
95 flags | ADD_CACHE_RENORMALIZE);
96 }
97
98 return retval;
99 }
100
101 static char *prune_directory(struct repository *repo,
102 struct dir_struct *dir,
103 struct pathspec *pathspec,
104 int prefix)
105 {
106 char *seen;
107 int i;
108 struct dir_entry **src, **dst;
109
110 seen = xcalloc(pathspec->nr, 1);
111
112 src = dst = dir->entries;
113 i = dir->nr;
114 while (--i >= 0) {
115 struct dir_entry *entry = *src++;
116 if (dir_path_match(repo->index, entry, pathspec, prefix, seen))
117 *dst++ = entry;
118 }
119 dir->nr = dst - dir->entries;
120 add_pathspec_matches_against_index(pathspec, repo->index, seen,
121 PS_IGNORE_SKIP_WORKTREE);
122 return seen;
123 }
124
125 static int refresh(struct repository *repo, int verbose, const struct pathspec *pathspec)
126 {
127 char *seen;
128 int i, ret = 0;
129 char *skip_worktree_seen = NULL;
130 struct string_list only_match_skip_worktree = STRING_LIST_INIT_NODUP;
131 unsigned int flags = REFRESH_IGNORE_SKIP_WORKTREE |
132 (verbose ? REFRESH_IN_PORCELAIN : REFRESH_QUIET);
133
134 seen = xcalloc(pathspec->nr, 1);
135 refresh_index(repo->index, flags, pathspec, seen,
136 _("Unstaged changes after refreshing the index:"));
137 for (i = 0; i < pathspec->nr; i++) {
138 if (!seen[i]) {
139 const char *path = pathspec->items[i].original;
140
141 if (matches_skip_worktree(pathspec, i, &skip_worktree_seen) ||
142 !path_in_sparse_checkout(path, repo->index)) {
143 string_list_append(&only_match_skip_worktree,
144 pathspec->items[i].original);
145 } else {
146 die(_("pathspec '%s' did not match any files"),
147 pathspec->items[i].original);
148 }
149 }
150 }
151
152 if (only_match_skip_worktree.nr) {
153 advise_on_updating_sparse_paths(&only_match_skip_worktree);
154 ret = 1;
155 }
156
157 free(seen);
158 free(skip_worktree_seen);
159 string_list_clear(&only_match_skip_worktree, 0);
160 return ret;
161 }
162
163 int interactive_add(struct repository *repo,
164 const char **argv,
165 const char *prefix,
166 int patch, struct interactive_options *interactive_opts)
167 {
168 struct pathspec pathspec;
169 int ret;
170
171 parse_pathspec(&pathspec, 0,
172 PATHSPEC_PREFER_FULL |
173 PATHSPEC_SYMLINK_LEADING_PATH |
174 PATHSPEC_PREFIX_ORIGIN,
175 prefix, argv);
176
177 if (patch)
178 ret = !!run_add_p(repo, ADD_P_ADD, interactive_opts, NULL, &pathspec, 0);
179 else
180 ret = !!run_add_i(repo, &pathspec, interactive_opts);
181
182 clear_pathspec(&pathspec);
183 return ret;
184 }
185
186 static int edit_patch(struct repository *repo,
187 int argc,
188 const char **argv,
189 const char *prefix)
190 {
191 char *file = repo_git_path(repo, "ADD_EDIT.patch");
192 struct child_process child = CHILD_PROCESS_INIT;
193 struct rev_info rev;
194 int out;
195 struct stat st;
196
197 repo_config(repo, git_diff_basic_config, NULL);
198
199 if (repo_read_index(repo) < 0)
200 die(_("could not read the index"));
201
202 repo_init_revisions(repo, &rev, prefix);
203 rev.diffopt.context = 7;
204
205 argc = setup_revisions(argc, argv, &rev, NULL);
206 rev.diffopt.output_format = DIFF_FORMAT_PATCH;
207 rev.diffopt.use_color = GIT_COLOR_NEVER;
208 rev.diffopt.flags.ignore_dirty_submodules = 1;
209 out = xopen(file, O_CREAT | O_WRONLY | O_TRUNC, 0666);
210 rev.diffopt.file = xfdopen(out, "w");
211 rev.diffopt.close_file = 1;
212 run_diff_files(&rev, 0);
213
214 if (launch_editor(file, NULL, NULL))
215 die(_("editing patch failed"));
216
217 if (stat(file, &st))
218 die_errno(_("could not stat '%s'"), file);
219 if (!st.st_size)
220 die(_("empty patch. aborted"));
221
222 child.git_cmd = 1;
223 strvec_pushl(&child.args, "apply", "--recount", "--cached", file,
224 NULL);
225 if (run_command(&child))
226 die(_("could not apply '%s'"), file);
227
228 unlink(file);
229 free(file);
230 release_revisions(&rev);
231 return 0;
232 }
233
234 static const char ignore_error[] =
235 N_("The following paths are ignored by one of your .gitignore files:\n");
236
237 static int verbose, show_only, ignored_too, refresh_only;
238 static int ignore_add_errors, intent_to_add, ignore_missing;
239 static int warn_on_embedded_repo = 1;
240
241 #define ADDREMOVE_DEFAULT 1
242 static int addremove = ADDREMOVE_DEFAULT;
243 static int addremove_explicit = -1; /* unspecified */
244
245 static char *chmod_arg;
246
247 static int ignore_removal_cb(const struct option *opt, const char *arg, int unset)
248 {
249 BUG_ON_OPT_ARG(arg);
250
251 /* if we are told to ignore, we are not adding removals */
252 *(int *)opt->value = !unset ? 0 : 1;
253 return 0;
254 }
255
256 static struct option builtin_add_options[] = {
257 OPT__DRY_RUN(&show_only, N_("dry run")),
258 OPT__VERBOSE(&verbose, N_("be verbose")),
259 OPT_GROUP(""),
260 OPT_BOOL('i', "interactive", &add_interactive, N_("interactive picking")),
261 OPT_BOOL('p', "patch", &patch_interactive, N_("select hunks interactively")),
262 OPT_BOOL(0, "auto-advance", &interactive_opts.auto_advance,
263 N_("auto advance to the next file when selecting hunks interactively")),
264 OPT_DIFF_UNIFIED(&interactive_opts.context),
265 OPT_DIFF_INTERHUNK_CONTEXT(&interactive_opts.interhunkcontext),
266 OPT_BOOL('e', "edit", &edit_interactive, N_("edit current diff and apply")),
267 OPT__FORCE(&ignored_too, N_("allow adding otherwise ignored files"), 0),
268 OPT_BOOL('u', "update", &take_worktree_changes, N_("update tracked files")),
269 OPT_BOOL(0, "renormalize", &add_renormalize, N_("renormalize EOL of tracked files (implies -u)")),
270 OPT_BOOL(0, "resolved", &add_resolved, N_("add conflict-resolved tracked files")),
271 OPT_BOOL('N', "intent-to-add", &intent_to_add, N_("record only the fact that the path will be added later")),
272 OPT_BOOL('A', "all", &addremove_explicit, N_("add changes from all tracked and untracked files")),
273 OPT_CALLBACK_F(0, "ignore-removal", &addremove_explicit,
274 NULL /* takes no arguments */,
275 N_("ignore paths removed in the working tree (same as --no-all)"),
276 PARSE_OPT_NOARG, ignore_removal_cb),
277 OPT_BOOL( 0 , "refresh", &refresh_only, N_("don't add, only refresh the index")),
278 OPT_BOOL( 0 , "ignore-errors", &ignore_add_errors, N_("just skip files which cannot be added because of errors")),
279 OPT_BOOL( 0 , "ignore-missing", &ignore_missing, N_("check if - even missing - files are ignored in dry run")),
280 OPT_BOOL(0, "sparse", &include_sparse, N_("allow updating entries outside of the sparse-checkout cone")),
281 OPT_STRING(0, "chmod", &chmod_arg, "(+|-)x",
282 N_("override the executable bit of the listed files")),
283 OPT_HIDDEN_BOOL(0, "warn-embedded-repo", &warn_on_embedded_repo,
284 N_("warn when adding an embedded repository")),
285 OPT_PATHSPEC_FROM_FILE(&pathspec_from_file),
286 OPT_PATHSPEC_FILE_NUL(&pathspec_file_nul),
287 OPT_END(),
288 };
289
290 static int add_config(const char *var, const char *value,
291 const struct config_context *ctx, void *cb)
292 {
293 if (!strcmp(var, "add.ignoreerrors") ||
294 !strcmp(var, "add.ignore-errors")) {
295 ignore_add_errors = git_config_bool(var, value);
296 return 0;
297 }
298
299 if (git_color_config(var, value, cb) < 0)
300 return -1;
301
302 return git_default_config(var, value, ctx, cb);
303 }
304
305 static const char embedded_advice[] = N_(
306 "You've added another git repository inside your current repository.\n"
307 "Clones of the outer repository will not contain the contents of\n"
308 "the embedded repository and will not know how to obtain it.\n"
309 "If you meant to add a submodule, use:\n"
310 "\n"
311 " git submodule add <url> %s\n"
312 "\n"
313 "If you added this path by mistake, you can remove it from the\n"
314 "index with:\n"
315 "\n"
316 " git rm --cached %s\n"
317 "\n"
318 "See \"git help submodule\" for more information."
319 );
320
321 static void check_embedded_repo(const char *path)
322 {
323 struct strbuf name = STRBUF_INIT;
324 static int adviced_on_embedded_repo = 0;
325
326 if (!warn_on_embedded_repo)
327 return;
328 if (!ends_with(path, "/"))
329 return;
330
331 /* Drop trailing slash for aesthetics */
332 strbuf_addstr(&name, path);
333 strbuf_strip_suffix(&name, "/");
334
335 warning(_("adding embedded git repository: %s"), name.buf);
336 if (!adviced_on_embedded_repo) {
337 advise_if_enabled(ADVICE_ADD_EMBEDDED_REPO,
338 embedded_advice, name.buf, name.buf);
339 adviced_on_embedded_repo = 1;
340 }
341
342 strbuf_release(&name);
343 }
344
345 static int add_files(struct repository *repo, struct dir_struct *dir, int flags)
346 {
347 int i, exit_status = 0;
348 struct string_list matched_sparse_paths = STRING_LIST_INIT_NODUP;
349
350 if (dir->ignored_nr) {
351 fprintf(stderr, _(ignore_error));
352 for (i = 0; i < dir->ignored_nr; i++)
353 fprintf(stderr, "%s\n", dir->ignored[i]->name);
354 advise_if_enabled(ADVICE_ADD_IGNORED_FILE,
355 _("Use -f if you really want to add them."));
356 exit_status = 1;
357 }
358
359 for (i = 0; i < dir->nr; i++) {
360 if (!include_sparse &&
361 !path_in_sparse_checkout(dir->entries[i]->name, repo->index)) {
362 string_list_append(&matched_sparse_paths,
363 dir->entries[i]->name);
364 continue;
365 }
366 if (add_file_to_index(repo->index, dir->entries[i]->name, flags)) {
367 if (!ignore_add_errors)
368 die(_("adding files failed"));
369 exit_status = 1;
370 } else {
371 check_embedded_repo(dir->entries[i]->name);
372 }
373 }
374
375 if (matched_sparse_paths.nr) {
376 advise_on_updating_sparse_paths(&matched_sparse_paths);
377 exit_status = 1;
378 }
379
380 string_list_clear(&matched_sparse_paths, 0);
381
382 return exit_status;
383 }
384
385 static int failed_to_add(int flags, const char *path)
386 {
387 if (!(flags & ADD_CACHE_IGNORE_ERRORS))
388 die(_("updating file '%s' failed"), path);
389 return 1;
390 }
391
392 static int add_resolved_files(struct repository *repo,
393 const struct pathspec *pathspec,
394 int flags)
395 {
396 struct index_state *istate = repo->index;
397 struct string_list unmerged_paths = STRING_LIST_INIT_DUP;
398 struct string_list unresolved_paths = STRING_LIST_INIT_DUP;
399 int exit_status = 0;
400 size_t i;
401
402 for (i = 0; i < istate->cache_nr; i++) {
403 struct cache_entry *ce = istate->cache[i];
404 if (!ce_stage(ce))
405 continue;
406 if (pathspec->nr && !ce_path_match(istate, ce, pathspec, NULL))
407 continue;
408 if (!unmerged_paths.nr ||
409 strcmp(unmerged_paths.items[unmerged_paths.nr - 1].string, ce->name))
410 string_list_append(&unmerged_paths, ce->name);
411 }
412
413 if (!unmerged_paths.nr) {
414 string_list_clear(&unmerged_paths, 0);
415 return 0;
416 }
417
418 for (i = 0; i < unmerged_paths.nr; i++) {
419 const char *path = unmerged_paths.items[i].string;
420 struct stat st;
421
422 if (!lstat(path, &st) && S_ISREG(st.st_mode)) {
423 if (has_conflict_markers(istate, path))
424 string_list_append(&unresolved_paths, path);
425 }
426 }
427
428 if (unresolved_paths.nr) {
429 struct strbuf sb = STRBUF_INIT;
430 for (i = 0; i < unresolved_paths.nr; i++)
431 strbuf_addf(&sb, "\t%s\n", unresolved_paths.items[i].string);
432 die(_("the following paths still have conflict markers:\n%s"), sb.buf);
433 }
434
435 for (i = 0; i < unmerged_paths.nr; i++) {
436 const char *path = unmerged_paths.items[i].string;
437 struct stat st;
438
439 if (lstat(path, &st)) {
440 if (errno != ENOENT)
441 die_errno(_("cannot lstat: '%s'"), path);
442 if (remove_file_from_index_with_flags(istate, path, flags))
443 exit_status = failed_to_add(flags, path);
444 } else {
445 if (add_file_to_index(istate, path, flags))
446 exit_status = failed_to_add(flags, path);
447 }
448 }
449
450 string_list_clear(&unmerged_paths, 0);
451 string_list_clear(&unresolved_paths, 0);
452 return exit_status;
453 }
454
455 int cmd_add(int argc,
456 const char **argv,
457 const char *prefix,
458 struct repository *repo)
459 {
460 int exit_status = 0;
461 struct pathspec pathspec;
462 struct dir_struct dir = DIR_INIT;
463 int flags;
464 int add_new_files;
465 int require_pathspec;
466 char *seen = NULL;
467 char *ps_matched = NULL;
468 struct lock_file lock_file = LOCK_INIT;
469 struct odb_transaction *transaction;
470
471 repo_config(repo, add_config, NULL);
472
473 argc = parse_options(argc, argv, prefix, builtin_add_options,
474 builtin_add_usage, PARSE_OPT_KEEP_ARGV0);
475
476 prepare_repo_settings(repo);
477 repo->settings.command_requires_full_index = 0;
478
479 if (interactive_opts.context < -1)
480 die(_("'%s' cannot be negative"), "--unified");
481 if (interactive_opts.interhunkcontext < -1)
482 die(_("'%s' cannot be negative"), "--inter-hunk-context");
483
484 if (patch_interactive)
485 add_interactive = 1;
486 if (add_interactive) {
487 if (show_only)
488 die(_("options '%s' and '%s' cannot be used together"), "--dry-run", "--interactive/--patch");
489 if (pathspec_from_file)
490 die(_("options '%s' and '%s' cannot be used together"), "--pathspec-from-file", "--interactive/--patch");
491 exit(interactive_add(repo, argv + 1, prefix, patch_interactive, &interactive_opts));
492 } else {
493 if (interactive_opts.context != -1)
494 die(_("the option '%s' requires '%s'"), "--unified", "--interactive/--patch");
495 if (interactive_opts.interhunkcontext != -1)
496 die(_("the option '%s' requires '%s'"), "--inter-hunk-context", "--interactive/--patch");
497 if (!interactive_opts.auto_advance)
498 die(_("the option '%s' requires '%s'"), "--no-auto-advance", "--interactive/--patch");
499 }
500
501 if (edit_interactive) {
502 if (pathspec_from_file)
503 die(_("options '%s' and '%s' cannot be used together"), "--pathspec-from-file", "--edit");
504 return(edit_patch(repo, argc, argv, prefix));
505 }
506 argc--;
507 argv++;
508
509 if (0 <= addremove_explicit)
510 addremove = addremove_explicit;
511 else if (take_worktree_changes && ADDREMOVE_DEFAULT)
512 addremove = 0; /* "-u" was given but not "-A" */
513
514 die_for_incompatible_opt3(take_worktree_changes, "-u/--update",
515 0 < addremove_explicit, "-A/--all",
516 add_resolved, "--resolved");
517
518 if (!show_only && ignore_missing)
519 die(_("the option '%s' requires '%s'"), "--ignore-missing", "--dry-run");
520
521 if (chmod_arg && ((chmod_arg[0] != '-' && chmod_arg[0] != '+') ||
522 chmod_arg[1] != 'x' || chmod_arg[2]))
523 die(_("--chmod param '%s' must be either -x or +x"), chmod_arg);
524
525 add_new_files = !take_worktree_changes && !refresh_only &&
526 !add_renormalize && !add_resolved;
527 require_pathspec = !(take_worktree_changes ||
528 (0 < addremove_explicit) ||
529 add_resolved);
530
531 repo_hold_locked_index(repo, &lock_file, LOCK_DIE_ON_ERROR);
532
533 /*
534 * Check the "pathspec '%s' did not match any files" block
535 * below before enabling new magic.
536 */
537 parse_pathspec(&pathspec, 0,
538 PATHSPEC_PREFER_FULL |
539 PATHSPEC_SYMLINK_LEADING_PATH,
540 prefix, argv);
541
542 if (pathspec_from_file) {
543 if (pathspec.nr)
544 die(_("'%s' and pathspec arguments cannot be used together"), "--pathspec-from-file");
545
546 parse_pathspec_file(&pathspec, 0,
547 PATHSPEC_PREFER_FULL |
548 PATHSPEC_SYMLINK_LEADING_PATH,
549 prefix, pathspec_from_file, pathspec_file_nul);
550 } else if (pathspec_file_nul) {
551 die(_("the option '%s' requires '%s'"), "--pathspec-file-nul", "--pathspec-from-file");
552 }
553
554 if (require_pathspec && pathspec.nr == 0) {
555 fprintf(stderr, _("Nothing specified, nothing added.\n"));
556 advise_if_enabled(ADVICE_ADD_EMPTY_PATHSPEC,
557 _("Maybe you wanted to say 'git add .'?"));
558 return 0;
559 }
560
561 if (!take_worktree_changes && !add_resolved &&
562 addremove_explicit < 0 && pathspec.nr)
563 /* Turn "git add pathspec..." to "git add -A pathspec..." */
564 addremove = 1;
565
566 flags = ((verbose ? ADD_CACHE_VERBOSE : 0) |
567 (show_only ? ADD_CACHE_PRETEND : 0) |
568 (intent_to_add ? ADD_CACHE_INTENT : 0) |
569 (ignore_add_errors ? ADD_CACHE_IGNORE_ERRORS : 0) |
570 (!(addremove || take_worktree_changes)
571 ? ADD_CACHE_IGNORE_REMOVAL : 0));
572
573 if (repo_read_index_preload(repo, &pathspec, 0) < 0)
574 die(_("index file corrupt"));
575
576 die_in_unpopulated_submodule(repo->index, prefix);
577 die_path_inside_submodule(repo->index, &pathspec);
578
579 if (add_new_files) {
580 int baselen;
581
582 /* Set up the default git porcelain excludes */
583 if (!ignored_too) {
584 dir.flags |= DIR_COLLECT_IGNORED;
585 setup_standard_excludes(&dir);
586 }
587
588 /* This picks up the paths that are not tracked */
589 baselen = fill_directory(&dir, repo->index, &pathspec);
590 if (pathspec.nr)
591 seen = prune_directory(repo, &dir, &pathspec, baselen);
592 }
593
594 if (refresh_only) {
595 exit_status |= refresh(repo, verbose, &pathspec);
596 goto finish;
597 }
598
599 if (pathspec.nr) {
600 int i;
601 char *skip_worktree_seen = NULL;
602 struct string_list only_match_skip_worktree = STRING_LIST_INIT_NODUP;
603
604 if (!seen)
605 seen = find_pathspecs_matching_against_index(&pathspec,
606 repo->index, PS_IGNORE_SKIP_WORKTREE);
607
608 /*
609 * file_exists() assumes exact match
610 */
611 GUARD_PATHSPEC(&pathspec,
612 PATHSPEC_FROMTOP |
613 PATHSPEC_LITERAL |
614 PATHSPEC_GLOB |
615 PATHSPEC_ICASE |
616 PATHSPEC_EXCLUDE |
617 PATHSPEC_ATTR);
618
619 for (i = 0; i < pathspec.nr; i++) {
620 const char *path = pathspec.items[i].match;
621
622 if (pathspec.items[i].magic & PATHSPEC_EXCLUDE)
623 continue;
624 if (seen[i])
625 continue;
626
627 if (!include_sparse &&
628 matches_skip_worktree(&pathspec, i, &skip_worktree_seen)) {
629 string_list_append(&only_match_skip_worktree,
630 pathspec.items[i].original);
631 continue;
632 }
633
634 /* Don't complain at 'git add .' on empty repo */
635 if (!path[0])
636 continue;
637
638 if ((pathspec.items[i].magic & (PATHSPEC_GLOB | PATHSPEC_ICASE)) ||
639 !file_exists(path)) {
640 if (ignore_missing) {
641 int dtype = DT_UNKNOWN;
642 if (is_excluded(&dir, repo->index, path, &dtype))
643 dir_add_ignored(&dir, repo->index,
644 path, pathspec.items[i].len);
645 } else
646 die(_("pathspec '%s' did not match any files"),
647 pathspec.items[i].original);
648 }
649 }
650
651
652 if (only_match_skip_worktree.nr) {
653 advise_on_updating_sparse_paths(&only_match_skip_worktree);
654 exit_status = 1;
655 }
656
657 free(seen);
658 free(skip_worktree_seen);
659 string_list_clear(&only_match_skip_worktree, 0);
660 }
661
662 odb_transaction_begin_or_die(repo->objects, &transaction, 0);
663
664 ps_matched = xcalloc(pathspec.nr, 1);
665 if (add_resolved)
666 exit_status |= add_resolved_files(repo, &pathspec, flags);
667 else if (add_renormalize)
668 exit_status |= renormalize_tracked_files(repo, &pathspec, flags);
669 else
670 exit_status |= add_files_to_cache(repo, prefix,
671 &pathspec, ps_matched,
672 include_sparse, flags, ignored_too);
673
674 if (take_worktree_changes && !add_renormalize && !ignore_add_errors &&
675 report_path_error(ps_matched, &pathspec))
676 exit(128);
677
678 if (add_new_files)
679 exit_status |= add_files(repo, &dir, flags);
680
681 if (chmod_arg && pathspec.nr)
682 exit_status |= chmod_pathspec(repo, &pathspec, chmod_arg[0], show_only);
683 odb_transaction_commit(transaction);
684
685 finish:
686 if (write_locked_index(repo->index, &lock_file,
687 COMMIT_LOCK | SKIP_IF_UNCHANGED))
688 die(_("unable to write new index file"));
689
690 free(ps_matched);
691 dir_clear(&dir);
692 clear_pathspec(&pathspec);
693 return exit_status;
694 }