read-cache*.h: move declarations for read-cache.c functions from cache.h
[alt-git.git] / builtin / add.c
blob9fe6b6ce2908c5946316fa324c28851f93ca7f0e
1 /*
2 * "git add" builtin command
4 * Copyright (C) 2006 Linus Torvalds
5 */
6 #define USE_THE_INDEX_VARIABLE
7 #include "cache.h"
8 #include "advice.h"
9 #include "config.h"
10 #include "builtin.h"
11 #include "lockfile.h"
12 #include "editor.h"
13 #include "dir.h"
14 #include "gettext.h"
15 #include "pathspec.h"
16 #include "exec-cmd.h"
17 #include "cache-tree.h"
18 #include "run-command.h"
19 #include "parse-options.h"
20 #include "preload-index.h"
21 #include "diff.h"
22 #include "diffcore.h"
23 #include "read-cache.h"
24 #include "revision.h"
25 #include "bulk-checkin.h"
26 #include "strvec.h"
27 #include "submodule.h"
28 #include "add-interactive.h"
30 static const char * const builtin_add_usage[] = {
31 N_("git add [<options>] [--] <pathspec>..."),
32 NULL
34 static int patch_interactive, add_interactive, edit_interactive;
35 static int take_worktree_changes;
36 static int add_renormalize;
37 static int pathspec_file_nul;
38 static int include_sparse;
39 static const char *pathspec_from_file;
41 static int chmod_pathspec(struct pathspec *pathspec, char flip, int show_only)
43 int i, ret = 0;
45 for (i = 0; i < the_index.cache_nr; i++) {
46 struct cache_entry *ce = the_index.cache[i];
47 int err;
49 if (!include_sparse &&
50 (ce_skip_worktree(ce) ||
51 !path_in_sparse_checkout(ce->name, &the_index)))
52 continue;
54 if (pathspec && !ce_path_match(&the_index, ce, pathspec, NULL))
55 continue;
57 if (!show_only)
58 err = chmod_index_entry(&the_index, ce, flip);
59 else
60 err = S_ISREG(ce->ce_mode) ? 0 : -1;
62 if (err < 0)
63 ret = error(_("cannot chmod %cx '%s'"), flip, ce->name);
66 return ret;
69 static int renormalize_tracked_files(const struct pathspec *pathspec, int flags)
71 int i, retval = 0;
73 for (i = 0; i < the_index.cache_nr; i++) {
74 struct cache_entry *ce = the_index.cache[i];
76 if (!include_sparse &&
77 (ce_skip_worktree(ce) ||
78 !path_in_sparse_checkout(ce->name, &the_index)))
79 continue;
80 if (ce_stage(ce))
81 continue; /* do not touch unmerged paths */
82 if (!S_ISREG(ce->ce_mode) && !S_ISLNK(ce->ce_mode))
83 continue; /* do not touch non blobs */
84 if (pathspec && !ce_path_match(&the_index, ce, pathspec, NULL))
85 continue;
86 retval |= add_file_to_index(&the_index, ce->name,
87 flags | ADD_CACHE_RENORMALIZE);
90 return retval;
93 static char *prune_directory(struct dir_struct *dir, struct pathspec *pathspec, int prefix)
95 char *seen;
96 int i;
97 struct dir_entry **src, **dst;
99 seen = xcalloc(pathspec->nr, 1);
101 src = dst = dir->entries;
102 i = dir->nr;
103 while (--i >= 0) {
104 struct dir_entry *entry = *src++;
105 if (dir_path_match(&the_index, entry, pathspec, prefix, seen))
106 *dst++ = entry;
108 dir->nr = dst - dir->entries;
109 add_pathspec_matches_against_index(pathspec, &the_index, seen,
110 PS_IGNORE_SKIP_WORKTREE);
111 return seen;
114 static int refresh(int verbose, const struct pathspec *pathspec)
116 char *seen;
117 int i, ret = 0;
118 char *skip_worktree_seen = NULL;
119 struct string_list only_match_skip_worktree = STRING_LIST_INIT_NODUP;
120 int flags = REFRESH_IGNORE_SKIP_WORKTREE |
121 (verbose ? REFRESH_IN_PORCELAIN : REFRESH_QUIET);
123 seen = xcalloc(pathspec->nr, 1);
124 refresh_index(&the_index, flags, pathspec, seen,
125 _("Unstaged changes after refreshing the index:"));
126 for (i = 0; i < pathspec->nr; i++) {
127 if (!seen[i]) {
128 const char *path = pathspec->items[i].original;
130 if (matches_skip_worktree(pathspec, i, &skip_worktree_seen) ||
131 !path_in_sparse_checkout(path, &the_index)) {
132 string_list_append(&only_match_skip_worktree,
133 pathspec->items[i].original);
134 } else {
135 die(_("pathspec '%s' did not match any files"),
136 pathspec->items[i].original);
141 if (only_match_skip_worktree.nr) {
142 advise_on_updating_sparse_paths(&only_match_skip_worktree);
143 ret = 1;
146 free(seen);
147 free(skip_worktree_seen);
148 string_list_clear(&only_match_skip_worktree, 0);
149 return ret;
152 int interactive_add(const char **argv, const char *prefix, int patch)
154 struct pathspec pathspec;
155 int unused;
157 if (!git_config_get_bool("add.interactive.usebuiltin", &unused))
158 warning(_("the add.interactive.useBuiltin setting has been removed!\n"
159 "See its entry in 'git help config' for details."));
161 parse_pathspec(&pathspec, 0,
162 PATHSPEC_PREFER_FULL |
163 PATHSPEC_SYMLINK_LEADING_PATH |
164 PATHSPEC_PREFIX_ORIGIN,
165 prefix, argv);
167 if (patch)
168 return !!run_add_p(the_repository, ADD_P_ADD, NULL, &pathspec);
169 else
170 return !!run_add_i(the_repository, &pathspec);
173 static int edit_patch(int argc, const char **argv, const char *prefix)
175 char *file = git_pathdup("ADD_EDIT.patch");
176 struct child_process child = CHILD_PROCESS_INIT;
177 struct rev_info rev;
178 int out;
179 struct stat st;
181 git_config(git_diff_basic_config, NULL); /* no "diff" UI options */
183 if (repo_read_index(the_repository) < 0)
184 die(_("Could not read the index"));
186 repo_init_revisions(the_repository, &rev, prefix);
187 rev.diffopt.context = 7;
189 argc = setup_revisions(argc, argv, &rev, NULL);
190 rev.diffopt.output_format = DIFF_FORMAT_PATCH;
191 rev.diffopt.use_color = 0;
192 rev.diffopt.flags.ignore_dirty_submodules = 1;
193 out = xopen(file, O_CREAT | O_WRONLY | O_TRUNC, 0666);
194 rev.diffopt.file = xfdopen(out, "w");
195 rev.diffopt.close_file = 1;
196 if (run_diff_files(&rev, 0))
197 die(_("Could not write patch"));
199 if (launch_editor(file, NULL, NULL))
200 die(_("editing patch failed"));
202 if (stat(file, &st))
203 die_errno(_("Could not stat '%s'"), file);
204 if (!st.st_size)
205 die(_("Empty patch. Aborted."));
207 child.git_cmd = 1;
208 strvec_pushl(&child.args, "apply", "--recount", "--cached", file,
209 NULL);
210 if (run_command(&child))
211 die(_("Could not apply '%s'"), file);
213 unlink(file);
214 free(file);
215 release_revisions(&rev);
216 return 0;
219 static const char ignore_error[] =
220 N_("The following paths are ignored by one of your .gitignore files:\n");
222 static int verbose, show_only, ignored_too, refresh_only;
223 static int ignore_add_errors, intent_to_add, ignore_missing;
224 static int warn_on_embedded_repo = 1;
226 #define ADDREMOVE_DEFAULT 1
227 static int addremove = ADDREMOVE_DEFAULT;
228 static int addremove_explicit = -1; /* unspecified */
230 static char *chmod_arg;
232 static int ignore_removal_cb(const struct option *opt, const char *arg, int unset)
234 /* if we are told to ignore, we are not adding removals */
235 *(int *)opt->value = !unset ? 0 : 1;
236 return 0;
239 static struct option builtin_add_options[] = {
240 OPT__DRY_RUN(&show_only, N_("dry run")),
241 OPT__VERBOSE(&verbose, N_("be verbose")),
242 OPT_GROUP(""),
243 OPT_BOOL('i', "interactive", &add_interactive, N_("interactive picking")),
244 OPT_BOOL('p', "patch", &patch_interactive, N_("select hunks interactively")),
245 OPT_BOOL('e', "edit", &edit_interactive, N_("edit current diff and apply")),
246 OPT__FORCE(&ignored_too, N_("allow adding otherwise ignored files"), 0),
247 OPT_BOOL('u', "update", &take_worktree_changes, N_("update tracked files")),
248 OPT_BOOL(0, "renormalize", &add_renormalize, N_("renormalize EOL of tracked files (implies -u)")),
249 OPT_BOOL('N', "intent-to-add", &intent_to_add, N_("record only the fact that the path will be added later")),
250 OPT_BOOL('A', "all", &addremove_explicit, N_("add changes from all tracked and untracked files")),
251 OPT_CALLBACK_F(0, "ignore-removal", &addremove_explicit,
252 NULL /* takes no arguments */,
253 N_("ignore paths removed in the working tree (same as --no-all)"),
254 PARSE_OPT_NOARG, ignore_removal_cb),
255 OPT_BOOL( 0 , "refresh", &refresh_only, N_("don't add, only refresh the index")),
256 OPT_BOOL( 0 , "ignore-errors", &ignore_add_errors, N_("just skip files which cannot be added because of errors")),
257 OPT_BOOL( 0 , "ignore-missing", &ignore_missing, N_("check if - even missing - files are ignored in dry run")),
258 OPT_BOOL(0, "sparse", &include_sparse, N_("allow updating entries outside of the sparse-checkout cone")),
259 OPT_STRING(0, "chmod", &chmod_arg, "(+|-)x",
260 N_("override the executable bit of the listed files")),
261 OPT_HIDDEN_BOOL(0, "warn-embedded-repo", &warn_on_embedded_repo,
262 N_("warn when adding an embedded repository")),
263 OPT_PATHSPEC_FROM_FILE(&pathspec_from_file),
264 OPT_PATHSPEC_FILE_NUL(&pathspec_file_nul),
265 OPT_END(),
268 static int add_config(const char *var, const char *value, void *cb)
270 if (!strcmp(var, "add.ignoreerrors") ||
271 !strcmp(var, "add.ignore-errors")) {
272 ignore_add_errors = git_config_bool(var, value);
273 return 0;
276 return git_default_config(var, value, cb);
279 static const char embedded_advice[] = N_(
280 "You've added another git repository inside your current repository.\n"
281 "Clones of the outer repository will not contain the contents of\n"
282 "the embedded repository and will not know how to obtain it.\n"
283 "If you meant to add a submodule, use:\n"
284 "\n"
285 " git submodule add <url> %s\n"
286 "\n"
287 "If you added this path by mistake, you can remove it from the\n"
288 "index with:\n"
289 "\n"
290 " git rm --cached %s\n"
291 "\n"
292 "See \"git help submodule\" for more information."
295 static void check_embedded_repo(const char *path)
297 struct strbuf name = STRBUF_INIT;
298 static int adviced_on_embedded_repo = 0;
300 if (!warn_on_embedded_repo)
301 return;
302 if (!ends_with(path, "/"))
303 return;
305 /* Drop trailing slash for aesthetics */
306 strbuf_addstr(&name, path);
307 strbuf_strip_suffix(&name, "/");
309 warning(_("adding embedded git repository: %s"), name.buf);
310 if (!adviced_on_embedded_repo &&
311 advice_enabled(ADVICE_ADD_EMBEDDED_REPO)) {
312 advise(embedded_advice, name.buf, name.buf);
313 adviced_on_embedded_repo = 1;
316 strbuf_release(&name);
319 static int add_files(struct dir_struct *dir, int flags)
321 int i, exit_status = 0;
322 struct string_list matched_sparse_paths = STRING_LIST_INIT_NODUP;
324 if (dir->ignored_nr) {
325 fprintf(stderr, _(ignore_error));
326 for (i = 0; i < dir->ignored_nr; i++)
327 fprintf(stderr, "%s\n", dir->ignored[i]->name);
328 if (advice_enabled(ADVICE_ADD_IGNORED_FILE))
329 advise(_("Use -f if you really want to add them.\n"
330 "Turn this message off by running\n"
331 "\"git config advice.addIgnoredFile false\""));
332 exit_status = 1;
335 for (i = 0; i < dir->nr; i++) {
336 if (!include_sparse &&
337 !path_in_sparse_checkout(dir->entries[i]->name, &the_index)) {
338 string_list_append(&matched_sparse_paths,
339 dir->entries[i]->name);
340 continue;
342 if (add_file_to_index(&the_index, dir->entries[i]->name, flags)) {
343 if (!ignore_add_errors)
344 die(_("adding files failed"));
345 exit_status = 1;
346 } else {
347 check_embedded_repo(dir->entries[i]->name);
351 if (matched_sparse_paths.nr) {
352 advise_on_updating_sparse_paths(&matched_sparse_paths);
353 exit_status = 1;
356 string_list_clear(&matched_sparse_paths, 0);
358 return exit_status;
361 int cmd_add(int argc, const char **argv, const char *prefix)
363 int exit_status = 0;
364 struct pathspec pathspec;
365 struct dir_struct dir = DIR_INIT;
366 int flags;
367 int add_new_files;
368 int require_pathspec;
369 char *seen = NULL;
370 struct lock_file lock_file = LOCK_INIT;
372 git_config(add_config, NULL);
374 argc = parse_options(argc, argv, prefix, builtin_add_options,
375 builtin_add_usage, PARSE_OPT_KEEP_ARGV0);
376 if (patch_interactive)
377 add_interactive = 1;
378 if (add_interactive) {
379 if (show_only)
380 die(_("options '%s' and '%s' cannot be used together"), "--dry-run", "--interactive/--patch");
381 if (pathspec_from_file)
382 die(_("options '%s' and '%s' cannot be used together"), "--pathspec-from-file", "--interactive/--patch");
383 exit(interactive_add(argv + 1, prefix, patch_interactive));
386 if (edit_interactive) {
387 if (pathspec_from_file)
388 die(_("options '%s' and '%s' cannot be used together"), "--pathspec-from-file", "--edit");
389 return(edit_patch(argc, argv, prefix));
391 argc--;
392 argv++;
394 if (0 <= addremove_explicit)
395 addremove = addremove_explicit;
396 else if (take_worktree_changes && ADDREMOVE_DEFAULT)
397 addremove = 0; /* "-u" was given but not "-A" */
399 if (addremove && take_worktree_changes)
400 die(_("options '%s' and '%s' cannot be used together"), "-A", "-u");
402 if (!show_only && ignore_missing)
403 die(_("the option '%s' requires '%s'"), "--ignore-missing", "--dry-run");
405 if (chmod_arg && ((chmod_arg[0] != '-' && chmod_arg[0] != '+') ||
406 chmod_arg[1] != 'x' || chmod_arg[2]))
407 die(_("--chmod param '%s' must be either -x or +x"), chmod_arg);
409 add_new_files = !take_worktree_changes && !refresh_only && !add_renormalize;
410 require_pathspec = !(take_worktree_changes || (0 < addremove_explicit));
412 prepare_repo_settings(the_repository);
413 the_repository->settings.command_requires_full_index = 0;
415 repo_hold_locked_index(the_repository, &lock_file, LOCK_DIE_ON_ERROR);
418 * Check the "pathspec '%s' did not match any files" block
419 * below before enabling new magic.
421 parse_pathspec(&pathspec, PATHSPEC_ATTR,
422 PATHSPEC_PREFER_FULL |
423 PATHSPEC_SYMLINK_LEADING_PATH,
424 prefix, argv);
426 if (pathspec_from_file) {
427 if (pathspec.nr)
428 die(_("'%s' and pathspec arguments cannot be used together"), "--pathspec-from-file");
430 parse_pathspec_file(&pathspec, PATHSPEC_ATTR,
431 PATHSPEC_PREFER_FULL |
432 PATHSPEC_SYMLINK_LEADING_PATH,
433 prefix, pathspec_from_file, pathspec_file_nul);
434 } else if (pathspec_file_nul) {
435 die(_("the option '%s' requires '%s'"), "--pathspec-file-nul", "--pathspec-from-file");
438 if (require_pathspec && pathspec.nr == 0) {
439 fprintf(stderr, _("Nothing specified, nothing added.\n"));
440 if (advice_enabled(ADVICE_ADD_EMPTY_PATHSPEC))
441 advise( _("Maybe you wanted to say 'git add .'?\n"
442 "Turn this message off by running\n"
443 "\"git config advice.addEmptyPathspec false\""));
444 return 0;
447 if (!take_worktree_changes && addremove_explicit < 0 && pathspec.nr)
448 /* Turn "git add pathspec..." to "git add -A pathspec..." */
449 addremove = 1;
451 flags = ((verbose ? ADD_CACHE_VERBOSE : 0) |
452 (show_only ? ADD_CACHE_PRETEND : 0) |
453 (intent_to_add ? ADD_CACHE_INTENT : 0) |
454 (ignore_add_errors ? ADD_CACHE_IGNORE_ERRORS : 0) |
455 (!(addremove || take_worktree_changes)
456 ? ADD_CACHE_IGNORE_REMOVAL : 0));
458 if (repo_read_index_preload(the_repository, &pathspec, 0) < 0)
459 die(_("index file corrupt"));
461 die_in_unpopulated_submodule(&the_index, prefix);
462 die_path_inside_submodule(&the_index, &pathspec);
464 if (add_new_files) {
465 int baselen;
467 /* Set up the default git porcelain excludes */
468 if (!ignored_too) {
469 dir.flags |= DIR_COLLECT_IGNORED;
470 setup_standard_excludes(&dir);
473 /* This picks up the paths that are not tracked */
474 baselen = fill_directory(&dir, &the_index, &pathspec);
475 if (pathspec.nr)
476 seen = prune_directory(&dir, &pathspec, baselen);
479 if (refresh_only) {
480 exit_status |= refresh(verbose, &pathspec);
481 goto finish;
484 if (pathspec.nr) {
485 int i;
486 char *skip_worktree_seen = NULL;
487 struct string_list only_match_skip_worktree = STRING_LIST_INIT_NODUP;
489 if (!seen)
490 seen = find_pathspecs_matching_against_index(&pathspec,
491 &the_index, PS_IGNORE_SKIP_WORKTREE);
494 * file_exists() assumes exact match
496 GUARD_PATHSPEC(&pathspec,
497 PATHSPEC_FROMTOP |
498 PATHSPEC_LITERAL |
499 PATHSPEC_GLOB |
500 PATHSPEC_ICASE |
501 PATHSPEC_EXCLUDE);
503 for (i = 0; i < pathspec.nr; i++) {
504 const char *path = pathspec.items[i].match;
506 if (pathspec.items[i].magic & PATHSPEC_EXCLUDE)
507 continue;
508 if (seen[i])
509 continue;
511 if (!include_sparse &&
512 matches_skip_worktree(&pathspec, i, &skip_worktree_seen)) {
513 string_list_append(&only_match_skip_worktree,
514 pathspec.items[i].original);
515 continue;
518 /* Don't complain at 'git add .' on empty repo */
519 if (!path[0])
520 continue;
522 if ((pathspec.items[i].magic & (PATHSPEC_GLOB | PATHSPEC_ICASE)) ||
523 !file_exists(path)) {
524 if (ignore_missing) {
525 int dtype = DT_UNKNOWN;
526 if (is_excluded(&dir, &the_index, path, &dtype))
527 dir_add_ignored(&dir, &the_index,
528 path, pathspec.items[i].len);
529 } else
530 die(_("pathspec '%s' did not match any files"),
531 pathspec.items[i].original);
536 if (only_match_skip_worktree.nr) {
537 advise_on_updating_sparse_paths(&only_match_skip_worktree);
538 exit_status = 1;
541 free(seen);
542 free(skip_worktree_seen);
543 string_list_clear(&only_match_skip_worktree, 0);
546 begin_odb_transaction();
548 if (add_renormalize)
549 exit_status |= renormalize_tracked_files(&pathspec, flags);
550 else
551 exit_status |= add_files_to_cache(the_repository, prefix,
552 &pathspec, include_sparse,
553 flags);
555 if (add_new_files)
556 exit_status |= add_files(&dir, flags);
558 if (chmod_arg && pathspec.nr)
559 exit_status |= chmod_pathspec(&pathspec, chmod_arg[0], show_only);
560 end_odb_transaction();
562 finish:
563 if (write_locked_index(&the_index, &lock_file,
564 COMMIT_LOCK | SKIP_IF_UNCHANGED))
565 die(_("Unable to write new index file"));
567 dir_clear(&dir);
568 clear_pathspec(&pathspec);
569 return exit_status;