Merge branch 'ds/sparse-index-ignored-files'
[git/debian.git] / builtin / add.c
blob24da07578ffb1f5acc208a270f6e432917055ba8
1 /*
2 * "git add" builtin command
4 * Copyright (C) 2006 Linus Torvalds
5 */
6 #define USE_THE_INDEX_COMPATIBILITY_MACROS
7 #include "cache.h"
8 #include "config.h"
9 #include "builtin.h"
10 #include "lockfile.h"
11 #include "dir.h"
12 #include "pathspec.h"
13 #include "exec-cmd.h"
14 #include "cache-tree.h"
15 #include "run-command.h"
16 #include "parse-options.h"
17 #include "diff.h"
18 #include "diffcore.h"
19 #include "revision.h"
20 #include "bulk-checkin.h"
21 #include "strvec.h"
22 #include "submodule.h"
23 #include "add-interactive.h"
25 static const char * const builtin_add_usage[] = {
26 N_("git add [<options>] [--] <pathspec>..."),
27 NULL
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 {
37 int flags;
38 int add_errors;
41 static int chmod_pathspec(struct pathspec *pathspec, char flip, int show_only)
43 int i, ret = 0;
45 for (i = 0; i < active_nr; i++) {
46 struct cache_entry *ce = active_cache[i];
47 int err;
49 if (ce_skip_worktree(ce))
50 continue;
52 if (pathspec && !ce_path_match(&the_index, ce, pathspec, NULL))
53 continue;
55 if (!show_only)
56 err = chmod_cache_entry(ce, flip);
57 else
58 err = S_ISREG(ce->ce_mode) ? 0 : -1;
60 if (err < 0)
61 ret = error(_("cannot chmod %cx '%s'"), flip, ce->name);
64 return ret;
67 static int fix_unmerged_status(struct diff_filepair *p,
68 struct update_callback_data *data)
70 if (p->status != DIFF_STATUS_UNMERGED)
71 return p->status;
72 if (!(data->flags & ADD_CACHE_IGNORE_REMOVAL) && !p->two->mode)
74 * This is not an explicit add request, and the
75 * path is missing from the working tree (deleted)
77 return DIFF_STATUS_DELETED;
78 else
80 * Either an explicit add request, or path exists
81 * in the working tree. An attempt to explicitly
82 * add a path that does not exist in the working tree
83 * will be caught as an error by the caller immediately.
85 return DIFF_STATUS_MODIFIED;
88 static void update_callback(struct diff_queue_struct *q,
89 struct diff_options *opt, void *cbdata)
91 int i;
92 struct update_callback_data *data = cbdata;
94 for (i = 0; i < q->nr; i++) {
95 struct diff_filepair *p = q->queue[i];
96 const char *path = p->one->path;
97 switch (fix_unmerged_status(p, data)) {
98 default:
99 die(_("unexpected diff status %c"), p->status);
100 case DIFF_STATUS_MODIFIED:
101 case DIFF_STATUS_TYPE_CHANGED:
102 if (add_file_to_index(&the_index, path, data->flags)) {
103 if (!(data->flags & ADD_CACHE_IGNORE_ERRORS))
104 die(_("updating files failed"));
105 data->add_errors++;
107 break;
108 case DIFF_STATUS_DELETED:
109 if (data->flags & ADD_CACHE_IGNORE_REMOVAL)
110 break;
111 if (!(data->flags & ADD_CACHE_PRETEND))
112 remove_file_from_index(&the_index, path);
113 if (data->flags & (ADD_CACHE_PRETEND|ADD_CACHE_VERBOSE))
114 printf(_("remove '%s'\n"), path);
115 break;
120 int add_files_to_cache(const char *prefix,
121 const struct pathspec *pathspec, int flags)
123 struct update_callback_data data;
124 struct rev_info rev;
126 memset(&data, 0, sizeof(data));
127 data.flags = flags;
129 repo_init_revisions(the_repository, &rev, prefix);
130 setup_revisions(0, NULL, &rev, NULL);
131 if (pathspec)
132 copy_pathspec(&rev.prune_data, pathspec);
133 rev.diffopt.output_format = DIFF_FORMAT_CALLBACK;
134 rev.diffopt.format_callback = update_callback;
135 rev.diffopt.format_callback_data = &data;
136 rev.diffopt.flags.override_submodule_config = 1;
137 rev.max_count = 0; /* do not compare unmerged paths with stage #2 */
138 run_diff_files(&rev, DIFF_RACY_IS_MODIFIED);
139 clear_pathspec(&rev.prune_data);
140 return !!data.add_errors;
143 static int renormalize_tracked_files(const struct pathspec *pathspec, int flags)
145 int i, retval = 0;
147 for (i = 0; i < active_nr; i++) {
148 struct cache_entry *ce = active_cache[i];
150 if (ce_skip_worktree(ce))
151 continue;
152 if (ce_stage(ce))
153 continue; /* do not touch unmerged paths */
154 if (!S_ISREG(ce->ce_mode) && !S_ISLNK(ce->ce_mode))
155 continue; /* do not touch non blobs */
156 if (pathspec && !ce_path_match(&the_index, ce, pathspec, NULL))
157 continue;
158 retval |= add_file_to_cache(ce->name, flags | ADD_CACHE_RENORMALIZE);
161 return retval;
164 static char *prune_directory(struct dir_struct *dir, struct pathspec *pathspec, int prefix)
166 char *seen;
167 int i;
168 struct dir_entry **src, **dst;
170 seen = xcalloc(pathspec->nr, 1);
172 src = dst = dir->entries;
173 i = dir->nr;
174 while (--i >= 0) {
175 struct dir_entry *entry = *src++;
176 if (dir_path_match(&the_index, entry, pathspec, prefix, seen))
177 *dst++ = entry;
179 dir->nr = dst - dir->entries;
180 add_pathspec_matches_against_index(pathspec, &the_index, seen,
181 PS_IGNORE_SKIP_WORKTREE);
182 return seen;
185 static int refresh(int verbose, const struct pathspec *pathspec)
187 char *seen;
188 int i, ret = 0;
189 char *skip_worktree_seen = NULL;
190 struct string_list only_match_skip_worktree = STRING_LIST_INIT_NODUP;
191 int flags = REFRESH_IGNORE_SKIP_WORKTREE |
192 (verbose ? REFRESH_IN_PORCELAIN : REFRESH_QUIET);
194 seen = xcalloc(pathspec->nr, 1);
195 refresh_index(&the_index, flags, pathspec, seen,
196 _("Unstaged changes after refreshing the index:"));
197 for (i = 0; i < pathspec->nr; i++) {
198 if (!seen[i]) {
199 const char *path = pathspec->items[i].original;
201 if (matches_skip_worktree(pathspec, i, &skip_worktree_seen) ||
202 !path_in_sparse_checkout(path, &the_index)) {
203 string_list_append(&only_match_skip_worktree,
204 pathspec->items[i].original);
205 } else {
206 die(_("pathspec '%s' did not match any files"),
207 pathspec->items[i].original);
212 if (only_match_skip_worktree.nr) {
213 advise_on_updating_sparse_paths(&only_match_skip_worktree);
214 ret = 1;
217 free(seen);
218 free(skip_worktree_seen);
219 string_list_clear(&only_match_skip_worktree, 0);
220 return ret;
223 int run_add_interactive(const char *revision, const char *patch_mode,
224 const struct pathspec *pathspec)
226 int status, i;
227 struct strvec argv = STRVEC_INIT;
228 int use_builtin_add_i =
229 git_env_bool("GIT_TEST_ADD_I_USE_BUILTIN", -1);
231 if (use_builtin_add_i < 0) {
232 int experimental;
233 if (!git_config_get_bool("add.interactive.usebuiltin",
234 &use_builtin_add_i))
235 ; /* ok */
236 else if (!git_config_get_bool("feature.experimental", &experimental) &&
237 experimental)
238 use_builtin_add_i = 1;
241 if (use_builtin_add_i == 1) {
242 enum add_p_mode mode;
244 if (!patch_mode)
245 return !!run_add_i(the_repository, pathspec);
247 if (!strcmp(patch_mode, "--patch"))
248 mode = ADD_P_ADD;
249 else if (!strcmp(patch_mode, "--patch=stash"))
250 mode = ADD_P_STASH;
251 else if (!strcmp(patch_mode, "--patch=reset"))
252 mode = ADD_P_RESET;
253 else if (!strcmp(patch_mode, "--patch=checkout"))
254 mode = ADD_P_CHECKOUT;
255 else if (!strcmp(patch_mode, "--patch=worktree"))
256 mode = ADD_P_WORKTREE;
257 else
258 die("'%s' not supported", patch_mode);
260 return !!run_add_p(the_repository, mode, revision, pathspec);
263 strvec_push(&argv, "add--interactive");
264 if (patch_mode)
265 strvec_push(&argv, patch_mode);
266 if (revision)
267 strvec_push(&argv, revision);
268 strvec_push(&argv, "--");
269 for (i = 0; i < pathspec->nr; i++)
270 /* pass original pathspec, to be re-parsed */
271 strvec_push(&argv, pathspec->items[i].original);
273 status = run_command_v_opt(argv.v, RUN_GIT_CMD);
274 strvec_clear(&argv);
275 return status;
278 int interactive_add(const char **argv, const char *prefix, int patch)
280 struct pathspec pathspec;
282 parse_pathspec(&pathspec, 0,
283 PATHSPEC_PREFER_FULL |
284 PATHSPEC_SYMLINK_LEADING_PATH |
285 PATHSPEC_PREFIX_ORIGIN,
286 prefix, argv);
288 return run_add_interactive(NULL,
289 patch ? "--patch" : NULL,
290 &pathspec);
293 static int edit_patch(int argc, const char **argv, const char *prefix)
295 char *file = git_pathdup("ADD_EDIT.patch");
296 const char *apply_argv[] = { "apply", "--recount", "--cached",
297 NULL, NULL };
298 struct child_process child = CHILD_PROCESS_INIT;
299 struct rev_info rev;
300 int out;
301 struct stat st;
303 apply_argv[3] = file;
305 git_config(git_diff_basic_config, NULL); /* no "diff" UI options */
307 if (read_cache() < 0)
308 die(_("Could not read the index"));
310 repo_init_revisions(the_repository, &rev, prefix);
311 rev.diffopt.context = 7;
313 argc = setup_revisions(argc, argv, &rev, NULL);
314 rev.diffopt.output_format = DIFF_FORMAT_PATCH;
315 rev.diffopt.use_color = 0;
316 rev.diffopt.flags.ignore_dirty_submodules = 1;
317 out = xopen(file, O_CREAT | O_WRONLY | O_TRUNC, 0666);
318 rev.diffopt.file = xfdopen(out, "w");
319 rev.diffopt.close_file = 1;
320 if (run_diff_files(&rev, 0))
321 die(_("Could not write patch"));
323 if (launch_editor(file, NULL, NULL))
324 die(_("editing patch failed"));
326 if (stat(file, &st))
327 die_errno(_("Could not stat '%s'"), file);
328 if (!st.st_size)
329 die(_("Empty patch. Aborted."));
331 child.git_cmd = 1;
332 child.argv = apply_argv;
333 if (run_command(&child))
334 die(_("Could not apply '%s'"), file);
336 unlink(file);
337 free(file);
338 return 0;
341 static const char ignore_error[] =
342 N_("The following paths are ignored by one of your .gitignore files:\n");
344 static int verbose, show_only, ignored_too, refresh_only;
345 static int ignore_add_errors, intent_to_add, ignore_missing;
346 static int warn_on_embedded_repo = 1;
348 #define ADDREMOVE_DEFAULT 1
349 static int addremove = ADDREMOVE_DEFAULT;
350 static int addremove_explicit = -1; /* unspecified */
352 static char *chmod_arg;
354 static int ignore_removal_cb(const struct option *opt, const char *arg, int unset)
356 /* if we are told to ignore, we are not adding removals */
357 *(int *)opt->value = !unset ? 0 : 1;
358 return 0;
361 static struct option builtin_add_options[] = {
362 OPT__DRY_RUN(&show_only, N_("dry run")),
363 OPT__VERBOSE(&verbose, N_("be verbose")),
364 OPT_GROUP(""),
365 OPT_BOOL('i', "interactive", &add_interactive, N_("interactive picking")),
366 OPT_BOOL('p', "patch", &patch_interactive, N_("select hunks interactively")),
367 OPT_BOOL('e', "edit", &edit_interactive, N_("edit current diff and apply")),
368 OPT__FORCE(&ignored_too, N_("allow adding otherwise ignored files"), 0),
369 OPT_BOOL('u', "update", &take_worktree_changes, N_("update tracked files")),
370 OPT_BOOL(0, "renormalize", &add_renormalize, N_("renormalize EOL of tracked files (implies -u)")),
371 OPT_BOOL('N', "intent-to-add", &intent_to_add, N_("record only the fact that the path will be added later")),
372 OPT_BOOL('A', "all", &addremove_explicit, N_("add changes from all tracked and untracked files")),
373 OPT_CALLBACK_F(0, "ignore-removal", &addremove_explicit,
374 NULL /* takes no arguments */,
375 N_("ignore paths removed in the working tree (same as --no-all)"),
376 PARSE_OPT_NOARG, ignore_removal_cb),
377 OPT_BOOL( 0 , "refresh", &refresh_only, N_("don't add, only refresh the index")),
378 OPT_BOOL( 0 , "ignore-errors", &ignore_add_errors, N_("just skip files which cannot be added because of errors")),
379 OPT_BOOL( 0 , "ignore-missing", &ignore_missing, N_("check if - even missing - files are ignored in dry run")),
380 OPT_STRING(0, "chmod", &chmod_arg, "(+|-)x",
381 N_("override the executable bit of the listed files")),
382 OPT_HIDDEN_BOOL(0, "warn-embedded-repo", &warn_on_embedded_repo,
383 N_("warn when adding an embedded repository")),
384 OPT_HIDDEN_BOOL(0, "legacy-stash-p", &legacy_stash_p,
385 N_("backend for `git stash -p`")),
386 OPT_PATHSPEC_FROM_FILE(&pathspec_from_file),
387 OPT_PATHSPEC_FILE_NUL(&pathspec_file_nul),
388 OPT_END(),
391 static int add_config(const char *var, const char *value, void *cb)
393 if (!strcmp(var, "add.ignoreerrors") ||
394 !strcmp(var, "add.ignore-errors")) {
395 ignore_add_errors = git_config_bool(var, value);
396 return 0;
399 return git_default_config(var, value, cb);
402 static const char embedded_advice[] = N_(
403 "You've added another git repository inside your current repository.\n"
404 "Clones of the outer repository will not contain the contents of\n"
405 "the embedded repository and will not know how to obtain it.\n"
406 "If you meant to add a submodule, use:\n"
407 "\n"
408 " git submodule add <url> %s\n"
409 "\n"
410 "If you added this path by mistake, you can remove it from the\n"
411 "index with:\n"
412 "\n"
413 " git rm --cached %s\n"
414 "\n"
415 "See \"git help submodule\" for more information."
418 static void check_embedded_repo(const char *path)
420 struct strbuf name = STRBUF_INIT;
421 static int adviced_on_embedded_repo = 0;
423 if (!warn_on_embedded_repo)
424 return;
425 if (!ends_with(path, "/"))
426 return;
428 /* Drop trailing slash for aesthetics */
429 strbuf_addstr(&name, path);
430 strbuf_strip_suffix(&name, "/");
432 warning(_("adding embedded git repository: %s"), name.buf);
433 if (!adviced_on_embedded_repo &&
434 advice_enabled(ADVICE_ADD_EMBEDDED_REPO)) {
435 advise(embedded_advice, name.buf, name.buf);
436 adviced_on_embedded_repo = 1;
439 strbuf_release(&name);
442 static int add_files(struct dir_struct *dir, int flags)
444 int i, exit_status = 0;
446 if (dir->ignored_nr) {
447 fprintf(stderr, _(ignore_error));
448 for (i = 0; i < dir->ignored_nr; i++)
449 fprintf(stderr, "%s\n", dir->ignored[i]->name);
450 if (advice_enabled(ADVICE_ADD_IGNORED_FILE))
451 advise(_("Use -f if you really want to add them.\n"
452 "Turn this message off by running\n"
453 "\"git config advice.addIgnoredFile false\""));
454 exit_status = 1;
457 for (i = 0; i < dir->nr; i++) {
458 if (add_file_to_index(&the_index, dir->entries[i]->name, flags)) {
459 if (!ignore_add_errors)
460 die(_("adding files failed"));
461 exit_status = 1;
462 } else {
463 check_embedded_repo(dir->entries[i]->name);
466 return exit_status;
469 int cmd_add(int argc, const char **argv, const char *prefix)
471 int exit_status = 0;
472 struct pathspec pathspec;
473 struct dir_struct dir = DIR_INIT;
474 int flags;
475 int add_new_files;
476 int require_pathspec;
477 char *seen = NULL;
478 struct lock_file lock_file = LOCK_INIT;
480 git_config(add_config, NULL);
482 argc = parse_options(argc, argv, prefix, builtin_add_options,
483 builtin_add_usage, PARSE_OPT_KEEP_ARGV0);
484 if (patch_interactive)
485 add_interactive = 1;
486 if (add_interactive) {
487 if (show_only)
488 die(_("--dry-run is incompatible with --interactive/--patch"));
489 if (pathspec_from_file)
490 die(_("--pathspec-from-file is incompatible with --interactive/--patch"));
491 exit(interactive_add(argv + 1, prefix, patch_interactive));
493 if (legacy_stash_p) {
494 struct pathspec pathspec;
496 parse_pathspec(&pathspec, 0,
497 PATHSPEC_PREFER_FULL |
498 PATHSPEC_SYMLINK_LEADING_PATH |
499 PATHSPEC_PREFIX_ORIGIN,
500 prefix, argv);
502 return run_add_interactive(NULL, "--patch=stash", &pathspec);
505 if (edit_interactive) {
506 if (pathspec_from_file)
507 die(_("--pathspec-from-file is incompatible with --edit"));
508 return(edit_patch(argc, argv, prefix));
510 argc--;
511 argv++;
513 if (0 <= addremove_explicit)
514 addremove = addremove_explicit;
515 else if (take_worktree_changes && ADDREMOVE_DEFAULT)
516 addremove = 0; /* "-u" was given but not "-A" */
518 if (addremove && take_worktree_changes)
519 die(_("-A and -u are mutually incompatible"));
521 if (!show_only && ignore_missing)
522 die(_("Option --ignore-missing can only be used together with --dry-run"));
524 if (chmod_arg && ((chmod_arg[0] != '-' && chmod_arg[0] != '+') ||
525 chmod_arg[1] != 'x' || chmod_arg[2]))
526 die(_("--chmod param '%s' must be either -x or +x"), chmod_arg);
528 add_new_files = !take_worktree_changes && !refresh_only && !add_renormalize;
529 require_pathspec = !(take_worktree_changes || (0 < addremove_explicit));
531 prepare_repo_settings(the_repository);
532 the_repository->settings.command_requires_full_index = 0;
534 hold_locked_index(&lock_file, LOCK_DIE_ON_ERROR);
537 * Check the "pathspec '%s' did not match any files" block
538 * below before enabling new magic.
540 parse_pathspec(&pathspec, PATHSPEC_ATTR,
541 PATHSPEC_PREFER_FULL |
542 PATHSPEC_SYMLINK_LEADING_PATH,
543 prefix, argv);
545 if (pathspec_from_file) {
546 if (pathspec.nr)
547 die(_("--pathspec-from-file is incompatible with pathspec arguments"));
549 parse_pathspec_file(&pathspec, PATHSPEC_ATTR,
550 PATHSPEC_PREFER_FULL |
551 PATHSPEC_SYMLINK_LEADING_PATH,
552 prefix, pathspec_from_file, pathspec_file_nul);
553 } else if (pathspec_file_nul) {
554 die(_("--pathspec-file-nul requires --pathspec-from-file"));
557 if (require_pathspec && pathspec.nr == 0) {
558 fprintf(stderr, _("Nothing specified, nothing added.\n"));
559 if (advice_enabled(ADVICE_ADD_EMPTY_PATHSPEC))
560 advise( _("Maybe you wanted to say 'git add .'?\n"
561 "Turn this message off by running\n"
562 "\"git config advice.addEmptyPathspec false\""));
563 return 0;
566 if (!take_worktree_changes && addremove_explicit < 0 && pathspec.nr)
567 /* Turn "git add pathspec..." to "git add -A pathspec..." */
568 addremove = 1;
570 flags = ((verbose ? ADD_CACHE_VERBOSE : 0) |
571 (show_only ? ADD_CACHE_PRETEND : 0) |
572 (intent_to_add ? ADD_CACHE_INTENT : 0) |
573 (ignore_add_errors ? ADD_CACHE_IGNORE_ERRORS : 0) |
574 (!(addremove || take_worktree_changes)
575 ? ADD_CACHE_IGNORE_REMOVAL : 0));
577 if (read_cache_preload(&pathspec) < 0)
578 die(_("index file corrupt"));
580 die_in_unpopulated_submodule(&the_index, prefix);
581 die_path_inside_submodule(&the_index, &pathspec);
583 if (add_new_files) {
584 int baselen;
586 /* Set up the default git porcelain excludes */
587 if (!ignored_too) {
588 dir.flags |= DIR_COLLECT_IGNORED;
589 setup_standard_excludes(&dir);
592 /* This picks up the paths that are not tracked */
593 baselen = fill_directory(&dir, &the_index, &pathspec);
594 if (pathspec.nr)
595 seen = prune_directory(&dir, &pathspec, baselen);
598 if (refresh_only) {
599 exit_status |= refresh(verbose, &pathspec);
600 goto finish;
603 if (pathspec.nr) {
604 int i;
605 char *skip_worktree_seen = NULL;
606 struct string_list only_match_skip_worktree = STRING_LIST_INIT_NODUP;
608 if (!seen)
609 seen = find_pathspecs_matching_against_index(&pathspec,
610 &the_index, PS_IGNORE_SKIP_WORKTREE);
613 * file_exists() assumes exact match
615 GUARD_PATHSPEC(&pathspec,
616 PATHSPEC_FROMTOP |
617 PATHSPEC_LITERAL |
618 PATHSPEC_GLOB |
619 PATHSPEC_ICASE |
620 PATHSPEC_EXCLUDE);
622 for (i = 0; i < pathspec.nr; i++) {
623 const char *path = pathspec.items[i].match;
625 if (pathspec.items[i].magic & PATHSPEC_EXCLUDE)
626 continue;
627 if (seen[i])
628 continue;
630 if (matches_skip_worktree(&pathspec, i, &skip_worktree_seen)) {
631 string_list_append(&only_match_skip_worktree,
632 pathspec.items[i].original);
633 continue;
636 /* Don't complain at 'git add .' on empty repo */
637 if (!path[0])
638 continue;
640 if ((pathspec.items[i].magic & (PATHSPEC_GLOB | PATHSPEC_ICASE)) ||
641 !file_exists(path)) {
642 if (ignore_missing) {
643 int dtype = DT_UNKNOWN;
644 if (is_excluded(&dir, &the_index, path, &dtype))
645 dir_add_ignored(&dir, &the_index,
646 path, pathspec.items[i].len);
647 } else
648 die(_("pathspec '%s' did not match any files"),
649 pathspec.items[i].original);
654 if (only_match_skip_worktree.nr) {
655 advise_on_updating_sparse_paths(&only_match_skip_worktree);
656 exit_status = 1;
659 free(seen);
660 free(skip_worktree_seen);
661 string_list_clear(&only_match_skip_worktree, 0);
664 plug_bulk_checkin();
666 if (add_renormalize)
667 exit_status |= renormalize_tracked_files(&pathspec, flags);
668 else
669 exit_status |= add_files_to_cache(prefix, &pathspec, flags);
671 if (add_new_files)
672 exit_status |= add_files(&dir, flags);
674 if (chmod_arg && pathspec.nr)
675 exit_status |= chmod_pathspec(&pathspec, chmod_arg[0], show_only);
676 unplug_bulk_checkin();
678 finish:
679 if (write_locked_index(&the_index, &lock_file,
680 COMMIT_LOCK | SKIP_IF_UNCHANGED))
681 die(_("Unable to write new index file"));
683 dir_clear(&dir);
684 UNLEAK(pathspec);
685 return exit_status;