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