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