2 * "git add" builtin command
4 * Copyright (C) 2006 Linus Torvalds
6 #define USE_THE_INDEX_COMPATIBILITY_MACROS
14 #include "cache-tree.h"
15 #include "run-command.h"
16 #include "parse-options.h"
20 #include "bulk-checkin.h"
22 #include "submodule.h"
23 #include "add-interactive.h"
25 static const char * const builtin_add_usage[] = {
26 N_("git add [<options>] [--] <pathspec>..."),
29 static int patch_interactive, add_interactive, edit_interactive;
30 static int take_worktree_changes;
31 static int add_renormalize;
32 static int pathspec_file_nul;
33 static const char *pathspec_from_file;
34 static int legacy_stash_p; /* support for the scripted `git stash` */
36 struct update_callback_data {
41 static int chmod_pathspec(struct pathspec *pathspec, char flip, int show_only)
45 for (i = 0; i < active_nr; i++) {
46 struct cache_entry *ce = active_cache[i];
49 if (pathspec && !ce_path_match(&the_index, ce, pathspec, NULL))
53 err = chmod_cache_entry(ce, flip);
55 err = S_ISREG(ce->ce_mode) ? 0 : -1;
58 ret = error(_("cannot chmod %cx '%s'"), flip, ce->name);
64 static int fix_unmerged_status(struct diff_filepair *p,
65 struct update_callback_data *data)
67 if (p->status != DIFF_STATUS_UNMERGED)
69 if (!(data->flags & ADD_CACHE_IGNORE_REMOVAL) && !p->two->mode)
71 * This is not an explicit add request, and the
72 * path is missing from the working tree (deleted)
74 return DIFF_STATUS_DELETED;
77 * Either an explicit add request, or path exists
78 * in the working tree. An attempt to explicitly
79 * add a path that does not exist in the working tree
80 * will be caught as an error by the caller immediately.
82 return DIFF_STATUS_MODIFIED;
85 static void update_callback(struct diff_queue_struct *q,
86 struct diff_options *opt, void *cbdata)
89 struct update_callback_data *data = cbdata;
91 for (i = 0; i < q->nr; i++) {
92 struct diff_filepair *p = q->queue[i];
93 const char *path = p->one->path;
94 switch (fix_unmerged_status(p, data)) {
96 die(_("unexpected diff status %c"), p->status);
97 case DIFF_STATUS_MODIFIED:
98 case DIFF_STATUS_TYPE_CHANGED:
99 if (add_file_to_index(&the_index, path, data->flags)) {
100 if (!(data->flags & ADD_CACHE_IGNORE_ERRORS))
101 die(_("updating files failed"));
105 case DIFF_STATUS_DELETED:
106 if (data->flags & ADD_CACHE_IGNORE_REMOVAL)
108 if (!(data->flags & ADD_CACHE_PRETEND))
109 remove_file_from_index(&the_index, path);
110 if (data->flags & (ADD_CACHE_PRETEND|ADD_CACHE_VERBOSE))
111 printf(_("remove '%s'\n"), path);
117 int add_files_to_cache(const char *prefix,
118 const struct pathspec *pathspec, int flags)
120 struct update_callback_data data;
123 memset(&data, 0, sizeof(data));
126 repo_init_revisions(the_repository, &rev, prefix);
127 setup_revisions(0, NULL, &rev, NULL);
129 copy_pathspec(&rev.prune_data, pathspec);
130 rev.diffopt.output_format = DIFF_FORMAT_CALLBACK;
131 rev.diffopt.format_callback = update_callback;
132 rev.diffopt.format_callback_data = &data;
133 rev.diffopt.flags.override_submodule_config = 1;
134 rev.max_count = 0; /* do not compare unmerged paths with stage #2 */
135 run_diff_files(&rev, DIFF_RACY_IS_MODIFIED);
136 clear_pathspec(&rev.prune_data);
137 return !!data.add_errors;
140 static int renormalize_tracked_files(const struct pathspec *pathspec, int flags)
144 /* TODO: audit for interaction with sparse-index. */
145 ensure_full_index(&the_index);
146 for (i = 0; i < active_nr; i++) {
147 struct cache_entry *ce = active_cache[i];
150 continue; /* do not touch unmerged paths */
151 if (!S_ISREG(ce->ce_mode) && !S_ISLNK(ce->ce_mode))
152 continue; /* do not touch non blobs */
153 if (pathspec && !ce_path_match(&the_index, ce, pathspec, NULL))
155 retval |= add_file_to_cache(ce->name, flags | ADD_CACHE_RENORMALIZE);
161 static char *prune_directory(struct dir_struct *dir, struct pathspec *pathspec, int prefix)
165 struct dir_entry **src, **dst;
167 seen = xcalloc(pathspec->nr, 1);
169 src = dst = dir->entries;
172 struct dir_entry *entry = *src++;
173 if (dir_path_match(&the_index, entry, pathspec, prefix, seen))
176 dir->nr = dst - dir->entries;
177 add_pathspec_matches_against_index(pathspec, &the_index, seen);
181 static void refresh(int verbose, const struct pathspec *pathspec)
186 seen = xcalloc(pathspec->nr, 1);
187 refresh_index(&the_index, verbose ? REFRESH_IN_PORCELAIN : REFRESH_QUIET,
188 pathspec, seen, _("Unstaged changes after refreshing the index:"));
189 for (i = 0; i < pathspec->nr; i++) {
191 die(_("pathspec '%s' did not match any files"),
192 pathspec->items[i].match);
197 int run_add_interactive(const char *revision, const char *patch_mode,
198 const struct pathspec *pathspec)
201 struct strvec argv = STRVEC_INIT;
202 int use_builtin_add_i =
203 git_env_bool("GIT_TEST_ADD_I_USE_BUILTIN", -1);
205 if (use_builtin_add_i < 0) {
207 if (!git_config_get_bool("add.interactive.usebuiltin",
210 else if (!git_config_get_bool("feature.experimental", &experimental) &&
212 use_builtin_add_i = 1;
215 if (use_builtin_add_i == 1) {
216 enum add_p_mode mode;
219 return !!run_add_i(the_repository, pathspec);
221 if (!strcmp(patch_mode, "--patch"))
223 else if (!strcmp(patch_mode, "--patch=stash"))
225 else if (!strcmp(patch_mode, "--patch=reset"))
227 else if (!strcmp(patch_mode, "--patch=checkout"))
228 mode = ADD_P_CHECKOUT;
229 else if (!strcmp(patch_mode, "--patch=worktree"))
230 mode = ADD_P_WORKTREE;
232 die("'%s' not supported", patch_mode);
234 return !!run_add_p(the_repository, mode, revision, pathspec);
237 strvec_push(&argv, "add--interactive");
239 strvec_push(&argv, patch_mode);
241 strvec_push(&argv, revision);
242 strvec_push(&argv, "--");
243 for (i = 0; i < pathspec->nr; i++)
244 /* pass original pathspec, to be re-parsed */
245 strvec_push(&argv, pathspec->items[i].original);
247 status = run_command_v_opt(argv.v, RUN_GIT_CMD);
252 int interactive_add(const char **argv, const char *prefix, int patch)
254 struct pathspec pathspec;
256 parse_pathspec(&pathspec, 0,
257 PATHSPEC_PREFER_FULL |
258 PATHSPEC_SYMLINK_LEADING_PATH |
259 PATHSPEC_PREFIX_ORIGIN,
262 return run_add_interactive(NULL,
263 patch ? "--patch" : NULL,
267 static int edit_patch(int argc, const char **argv, const char *prefix)
269 char *file = git_pathdup("ADD_EDIT.patch");
270 const char *apply_argv[] = { "apply", "--recount", "--cached",
272 struct child_process child = CHILD_PROCESS_INIT;
277 apply_argv[3] = file;
279 git_config(git_diff_basic_config, NULL); /* no "diff" UI options */
281 if (read_cache() < 0)
282 die(_("Could not read the index"));
284 repo_init_revisions(the_repository, &rev, prefix);
285 rev.diffopt.context = 7;
287 argc = setup_revisions(argc, argv, &rev, NULL);
288 rev.diffopt.output_format = DIFF_FORMAT_PATCH;
289 rev.diffopt.use_color = 0;
290 rev.diffopt.flags.ignore_dirty_submodules = 1;
291 out = open(file, O_CREAT | O_WRONLY | O_TRUNC, 0666);
293 die(_("Could not open '%s' for writing."), file);
294 rev.diffopt.file = xfdopen(out, "w");
295 rev.diffopt.close_file = 1;
296 if (run_diff_files(&rev, 0))
297 die(_("Could not write patch"));
299 if (launch_editor(file, NULL, NULL))
300 die(_("editing patch failed"));
303 die_errno(_("Could not stat '%s'"), file);
305 die(_("Empty patch. Aborted."));
308 child.argv = apply_argv;
309 if (run_command(&child))
310 die(_("Could not apply '%s'"), file);
317 static const char ignore_error[] =
318 N_("The following paths are ignored by one of your .gitignore files:\n");
320 static int verbose, show_only, ignored_too, refresh_only;
321 static int ignore_add_errors, intent_to_add, ignore_missing;
322 static int warn_on_embedded_repo = 1;
324 #define ADDREMOVE_DEFAULT 1
325 static int addremove = ADDREMOVE_DEFAULT;
326 static int addremove_explicit = -1; /* unspecified */
328 static char *chmod_arg;
330 static int ignore_removal_cb(const struct option *opt, const char *arg, int unset)
332 /* if we are told to ignore, we are not adding removals */
333 *(int *)opt->value = !unset ? 0 : 1;
337 static struct option builtin_add_options[] = {
338 OPT__DRY_RUN(&show_only, N_("dry run")),
339 OPT__VERBOSE(&verbose, N_("be verbose")),
341 OPT_BOOL('i', "interactive", &add_interactive, N_("interactive picking")),
342 OPT_BOOL('p', "patch", &patch_interactive, N_("select hunks interactively")),
343 OPT_BOOL('e', "edit", &edit_interactive, N_("edit current diff and apply")),
344 OPT__FORCE(&ignored_too, N_("allow adding otherwise ignored files"), 0),
345 OPT_BOOL('u', "update", &take_worktree_changes, N_("update tracked files")),
346 OPT_BOOL(0, "renormalize", &add_renormalize, N_("renormalize EOL of tracked files (implies -u)")),
347 OPT_BOOL('N', "intent-to-add", &intent_to_add, N_("record only the fact that the path will be added later")),
348 OPT_BOOL('A', "all", &addremove_explicit, N_("add changes from all tracked and untracked files")),
349 OPT_CALLBACK_F(0, "ignore-removal", &addremove_explicit,
350 NULL /* takes no arguments */,
351 N_("ignore paths removed in the working tree (same as --no-all)"),
352 PARSE_OPT_NOARG, ignore_removal_cb),
353 OPT_BOOL( 0 , "refresh", &refresh_only, N_("don't add, only refresh the index")),
354 OPT_BOOL( 0 , "ignore-errors", &ignore_add_errors, N_("just skip files which cannot be added because of errors")),
355 OPT_BOOL( 0 , "ignore-missing", &ignore_missing, N_("check if - even missing - files are ignored in dry run")),
356 OPT_STRING(0, "chmod", &chmod_arg, "(+|-)x",
357 N_("override the executable bit of the listed files")),
358 OPT_HIDDEN_BOOL(0, "warn-embedded-repo", &warn_on_embedded_repo,
359 N_("warn when adding an embedded repository")),
360 OPT_HIDDEN_BOOL(0, "legacy-stash-p", &legacy_stash_p,
361 N_("backend for `git stash -p`")),
362 OPT_PATHSPEC_FROM_FILE(&pathspec_from_file),
363 OPT_PATHSPEC_FILE_NUL(&pathspec_file_nul),
367 static int add_config(const char *var, const char *value, void *cb)
369 if (!strcmp(var, "add.ignoreerrors") ||
370 !strcmp(var, "add.ignore-errors")) {
371 ignore_add_errors = git_config_bool(var, value);
375 return git_default_config(var, value, cb);
378 static const char embedded_advice[] = N_(
379 "You've added another git repository inside your current repository.\n"
380 "Clones of the outer repository will not contain the contents of\n"
381 "the embedded repository and will not know how to obtain it.\n"
382 "If you meant to add a submodule, use:\n"
384 " git submodule add <url> %s\n"
386 "If you added this path by mistake, you can remove it from the\n"
389 " git rm --cached %s\n"
391 "See \"git help submodule\" for more information."
394 static void check_embedded_repo(const char *path)
396 struct strbuf name = STRBUF_INIT;
398 if (!warn_on_embedded_repo)
400 if (!ends_with(path, "/"))
403 /* Drop trailing slash for aesthetics */
404 strbuf_addstr(&name, path);
405 strbuf_strip_suffix(&name, "/");
407 warning(_("adding embedded git repository: %s"), name.buf);
408 if (advice_add_embedded_repo) {
409 advise(embedded_advice, name.buf, name.buf);
410 /* there may be multiple entries; advise only once */
411 advice_add_embedded_repo = 0;
414 strbuf_release(&name);
417 static int add_files(struct dir_struct *dir, int flags)
419 int i, exit_status = 0;
421 if (dir->ignored_nr) {
422 fprintf(stderr, _(ignore_error));
423 for (i = 0; i < dir->ignored_nr; i++)
424 fprintf(stderr, "%s\n", dir->ignored[i]->name);
425 if (advice_add_ignored_file)
426 advise(_("Use -f if you really want to add them.\n"
427 "Turn this message off by running\n"
428 "\"git config advice.addIgnoredFile false\""));
432 for (i = 0; i < dir->nr; i++) {
433 if (add_file_to_index(&the_index, dir->entries[i]->name, flags)) {
434 if (!ignore_add_errors)
435 die(_("adding files failed"));
438 check_embedded_repo(dir->entries[i]->name);
444 int cmd_add(int argc, const char **argv, const char *prefix)
447 struct pathspec pathspec;
448 struct dir_struct dir;
451 int require_pathspec;
453 struct lock_file lock_file = LOCK_INIT;
455 git_config(add_config, NULL);
457 argc = parse_options(argc, argv, prefix, builtin_add_options,
458 builtin_add_usage, PARSE_OPT_KEEP_ARGV0);
459 if (patch_interactive)
461 if (add_interactive) {
462 if (pathspec_from_file)
463 die(_("--pathspec-from-file is incompatible with --interactive/--patch"));
464 exit(interactive_add(argv + 1, prefix, patch_interactive));
466 if (legacy_stash_p) {
467 struct pathspec pathspec;
469 parse_pathspec(&pathspec, 0,
470 PATHSPEC_PREFER_FULL |
471 PATHSPEC_SYMLINK_LEADING_PATH |
472 PATHSPEC_PREFIX_ORIGIN,
475 return run_add_interactive(NULL, "--patch=stash", &pathspec);
478 if (edit_interactive) {
479 if (pathspec_from_file)
480 die(_("--pathspec-from-file is incompatible with --edit"));
481 return(edit_patch(argc, argv, prefix));
486 if (0 <= addremove_explicit)
487 addremove = addremove_explicit;
488 else if (take_worktree_changes && ADDREMOVE_DEFAULT)
489 addremove = 0; /* "-u" was given but not "-A" */
491 if (addremove && take_worktree_changes)
492 die(_("-A and -u are mutually incompatible"));
494 if (!show_only && ignore_missing)
495 die(_("Option --ignore-missing can only be used together with --dry-run"));
497 if (chmod_arg && ((chmod_arg[0] != '-' && chmod_arg[0] != '+') ||
498 chmod_arg[1] != 'x' || chmod_arg[2]))
499 die(_("--chmod param '%s' must be either -x or +x"), chmod_arg);
501 add_new_files = !take_worktree_changes && !refresh_only && !add_renormalize;
502 require_pathspec = !(take_worktree_changes || (0 < addremove_explicit));
504 hold_locked_index(&lock_file, LOCK_DIE_ON_ERROR);
507 * Check the "pathspec '%s' did not match any files" block
508 * below before enabling new magic.
510 parse_pathspec(&pathspec, PATHSPEC_ATTR,
511 PATHSPEC_PREFER_FULL |
512 PATHSPEC_SYMLINK_LEADING_PATH,
515 if (pathspec_from_file) {
517 die(_("--pathspec-from-file is incompatible with pathspec arguments"));
519 parse_pathspec_file(&pathspec, PATHSPEC_ATTR,
520 PATHSPEC_PREFER_FULL |
521 PATHSPEC_SYMLINK_LEADING_PATH,
522 prefix, pathspec_from_file, pathspec_file_nul);
523 } else if (pathspec_file_nul) {
524 die(_("--pathspec-file-nul requires --pathspec-from-file"));
527 if (require_pathspec && pathspec.nr == 0) {
528 fprintf(stderr, _("Nothing specified, nothing added.\n"));
529 if (advice_add_empty_pathspec)
530 advise( _("Maybe you wanted to say 'git add .'?\n"
531 "Turn this message off by running\n"
532 "\"git config advice.addEmptyPathspec false\""));
536 if (!take_worktree_changes && addremove_explicit < 0 && pathspec.nr)
537 /* Turn "git add pathspec..." to "git add -A pathspec..." */
540 flags = ((verbose ? ADD_CACHE_VERBOSE : 0) |
541 (show_only ? ADD_CACHE_PRETEND : 0) |
542 (intent_to_add ? ADD_CACHE_INTENT : 0) |
543 (ignore_add_errors ? ADD_CACHE_IGNORE_ERRORS : 0) |
544 (!(addremove || take_worktree_changes)
545 ? ADD_CACHE_IGNORE_REMOVAL : 0));
547 if (read_cache_preload(&pathspec) < 0)
548 die(_("index file corrupt"));
550 die_in_unpopulated_submodule(&the_index, prefix);
551 die_path_inside_submodule(&the_index, &pathspec);
557 /* Set up the default git porcelain excludes */
559 dir.flags |= DIR_COLLECT_IGNORED;
560 setup_standard_excludes(&dir);
563 /* This picks up the paths that are not tracked */
564 baselen = fill_directory(&dir, &the_index, &pathspec);
566 seen = prune_directory(&dir, &pathspec, baselen);
570 refresh(verbose, &pathspec);
578 seen = find_pathspecs_matching_against_index(&pathspec, &the_index);
581 * file_exists() assumes exact match
583 GUARD_PATHSPEC(&pathspec,
590 for (i = 0; i < pathspec.nr; i++) {
591 const char *path = pathspec.items[i].match;
592 if (pathspec.items[i].magic & PATHSPEC_EXCLUDE)
594 if (!seen[i] && path[0] &&
595 ((pathspec.items[i].magic &
596 (PATHSPEC_GLOB | PATHSPEC_ICASE)) ||
597 !file_exists(path))) {
598 if (ignore_missing) {
599 int dtype = DT_UNKNOWN;
600 if (is_excluded(&dir, &the_index, path, &dtype))
601 dir_add_ignored(&dir, &the_index,
602 path, pathspec.items[i].len);
604 die(_("pathspec '%s' did not match any files"),
605 pathspec.items[i].original);
614 exit_status |= renormalize_tracked_files(&pathspec, flags);
616 exit_status |= add_files_to_cache(prefix, &pathspec, flags);
619 exit_status |= add_files(&dir, flags);
621 if (chmod_arg && pathspec.nr)
622 exit_status |= chmod_pathspec(&pathspec, chmod_arg[0], show_only);
623 unplug_bulk_checkin();
626 if (write_locked_index(&the_index, &lock_file,
627 COMMIT_LOCK | SKIP_IF_UNCHANGED))
628 die(_("Unable to write new index file"));