git-push doc: more visibility for -q option
[git.git] / builtin / add.c
blob4b0dd798df5909905f7be69479d4b3b2ce1b19ae
1 /*
2 * "git add" builtin command
4 * Copyright (C) 2006 Linus Torvalds
5 */
6 #define USE_THE_INDEX_VARIABLE
7 #include "builtin.h"
8 #include "advice.h"
9 #include "config.h"
10 #include "lockfile.h"
11 #include "editor.h"
12 #include "dir.h"
13 #include "gettext.h"
14 #include "pathspec.h"
15 #include "exec-cmd.h"
16 #include "cache-tree.h"
17 #include "run-command.h"
18 #include "parse-options.h"
19 #include "path.h"
20 #include "preload-index.h"
21 #include "diff.h"
22 #include "diffcore.h"
23 #include "read-cache.h"
24 #include "repository.h"
25 #include "revision.h"
26 #include "bulk-checkin.h"
27 #include "strvec.h"
28 #include "submodule.h"
29 #include "add-interactive.h"
31 static const char * const builtin_add_usage[] = {
32 N_("git add [<options>] [--] <pathspec>..."),
33 NULL
35 static int patch_interactive, add_interactive, edit_interactive;
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;
42 static int chmod_pathspec(struct pathspec *pathspec, char flip, int show_only)
44 int i, ret = 0;
46 for (i = 0; i < the_index.cache_nr; i++) {
47 struct cache_entry *ce = the_index.cache[i];
48 int err;
50 if (!include_sparse &&
51 (ce_skip_worktree(ce) ||
52 !path_in_sparse_checkout(ce->name, &the_index)))
53 continue;
55 if (pathspec && !ce_path_match(&the_index, ce, pathspec, NULL))
56 continue;
58 if (!show_only)
59 err = chmod_index_entry(&the_index, ce, flip);
60 else
61 err = S_ISREG(ce->ce_mode) ? 0 : -1;
63 if (err < 0)
64 ret = error(_("cannot chmod %cx '%s'"), flip, ce->name);
67 return ret;
70 static int renormalize_tracked_files(const struct pathspec *pathspec, int flags)
72 int i, retval = 0;
74 for (i = 0; i < the_index.cache_nr; i++) {
75 struct cache_entry *ce = the_index.cache[i];
77 if (!include_sparse &&
78 (ce_skip_worktree(ce) ||
79 !path_in_sparse_checkout(ce->name, &the_index)))
80 continue;
81 if (ce_stage(ce))
82 continue; /* do not touch unmerged paths */
83 if (!S_ISREG(ce->ce_mode) && !S_ISLNK(ce->ce_mode))
84 continue; /* do not touch non blobs */
85 if (pathspec && !ce_path_match(&the_index, ce, pathspec, NULL))
86 continue;
87 retval |= add_file_to_index(&the_index, ce->name,
88 flags | ADD_CACHE_RENORMALIZE);
91 return retval;
94 static char *prune_directory(struct dir_struct *dir, struct pathspec *pathspec, int prefix)
96 char *seen;
97 int i;
98 struct dir_entry **src, **dst;
100 seen = xcalloc(pathspec->nr, 1);
102 src = dst = dir->entries;
103 i = dir->nr;
104 while (--i >= 0) {
105 struct dir_entry *entry = *src++;
106 if (dir_path_match(&the_index, entry, pathspec, prefix, seen))
107 *dst++ = entry;
109 dir->nr = dst - dir->entries;
110 add_pathspec_matches_against_index(pathspec, &the_index, seen,
111 PS_IGNORE_SKIP_WORKTREE);
112 return seen;
115 static int refresh(int verbose, const struct pathspec *pathspec)
117 char *seen;
118 int i, ret = 0;
119 char *skip_worktree_seen = NULL;
120 struct string_list only_match_skip_worktree = STRING_LIST_INIT_NODUP;
121 int flags = REFRESH_IGNORE_SKIP_WORKTREE |
122 (verbose ? REFRESH_IN_PORCELAIN : REFRESH_QUIET);
124 seen = xcalloc(pathspec->nr, 1);
125 refresh_index(&the_index, flags, pathspec, seen,
126 _("Unstaged changes after refreshing the index:"));
127 for (i = 0; i < pathspec->nr; i++) {
128 if (!seen[i]) {
129 const char *path = pathspec->items[i].original;
131 if (matches_skip_worktree(pathspec, i, &skip_worktree_seen) ||
132 !path_in_sparse_checkout(path, &the_index)) {
133 string_list_append(&only_match_skip_worktree,
134 pathspec->items[i].original);
135 } else {
136 die(_("pathspec '%s' did not match any files"),
137 pathspec->items[i].original);
142 if (only_match_skip_worktree.nr) {
143 advise_on_updating_sparse_paths(&only_match_skip_worktree);
144 ret = 1;
147 free(seen);
148 free(skip_worktree_seen);
149 string_list_clear(&only_match_skip_worktree, 0);
150 return ret;
153 int interactive_add(const char **argv, const char *prefix, int patch)
155 struct pathspec pathspec;
156 int unused;
158 if (!git_config_get_bool("add.interactive.usebuiltin", &unused))
159 warning(_("the add.interactive.useBuiltin setting has been removed!\n"
160 "See its entry in 'git help config' for details."));
162 parse_pathspec(&pathspec, 0,
163 PATHSPEC_PREFER_FULL |
164 PATHSPEC_SYMLINK_LEADING_PATH |
165 PATHSPEC_PREFIX_ORIGIN,
166 prefix, argv);
168 if (patch)
169 return !!run_add_p(the_repository, ADD_P_ADD, NULL, &pathspec);
170 else
171 return !!run_add_i(the_repository, &pathspec);
174 static int edit_patch(int argc, const char **argv, const char *prefix)
176 char *file = git_pathdup("ADD_EDIT.patch");
177 struct child_process child = CHILD_PROCESS_INIT;
178 struct rev_info rev;
179 int out;
180 struct stat st;
182 git_config(git_diff_basic_config, NULL); /* no "diff" UI options */
184 if (repo_read_index(the_repository) < 0)
185 die(_("Could not read the index"));
187 repo_init_revisions(the_repository, &rev, prefix);
188 rev.diffopt.context = 7;
190 argc = setup_revisions(argc, argv, &rev, NULL);
191 rev.diffopt.output_format = DIFF_FORMAT_PATCH;
192 rev.diffopt.use_color = 0;
193 rev.diffopt.flags.ignore_dirty_submodules = 1;
194 out = xopen(file, O_CREAT | O_WRONLY | O_TRUNC, 0666);
195 rev.diffopt.file = xfdopen(out, "w");
196 rev.diffopt.close_file = 1;
197 if (run_diff_files(&rev, 0))
198 die(_("Could not write patch"));
200 if (launch_editor(file, NULL, NULL))
201 die(_("editing patch failed"));
203 if (stat(file, &st))
204 die_errno(_("Could not stat '%s'"), file);
205 if (!st.st_size)
206 die(_("Empty patch. Aborted."));
208 child.git_cmd = 1;
209 strvec_pushl(&child.args, "apply", "--recount", "--cached", file,
210 NULL);
211 if (run_command(&child))
212 die(_("Could not apply '%s'"), file);
214 unlink(file);
215 free(file);
216 release_revisions(&rev);
217 return 0;
220 static const char ignore_error[] =
221 N_("The following paths are ignored by one of your .gitignore files:\n");
223 static int verbose, show_only, ignored_too, refresh_only;
224 static int ignore_add_errors, intent_to_add, ignore_missing;
225 static int warn_on_embedded_repo = 1;
227 #define ADDREMOVE_DEFAULT 1
228 static int addremove = ADDREMOVE_DEFAULT;
229 static int addremove_explicit = -1; /* unspecified */
231 static char *chmod_arg;
233 static int ignore_removal_cb(const struct option *opt, const char *arg, int unset)
235 /* if we are told to ignore, we are not adding removals */
236 *(int *)opt->value = !unset ? 0 : 1;
237 return 0;
240 static struct option builtin_add_options[] = {
241 OPT__DRY_RUN(&show_only, N_("dry run")),
242 OPT__VERBOSE(&verbose, N_("be verbose")),
243 OPT_GROUP(""),
244 OPT_BOOL('i', "interactive", &add_interactive, N_("interactive picking")),
245 OPT_BOOL('p', "patch", &patch_interactive, N_("select hunks interactively")),
246 OPT_BOOL('e', "edit", &edit_interactive, N_("edit current diff and apply")),
247 OPT__FORCE(&ignored_too, N_("allow adding otherwise ignored files"), 0),
248 OPT_BOOL('u', "update", &take_worktree_changes, N_("update tracked files")),
249 OPT_BOOL(0, "renormalize", &add_renormalize, N_("renormalize EOL of tracked files (implies -u)")),
250 OPT_BOOL('N', "intent-to-add", &intent_to_add, N_("record only the fact that the path will be added later")),
251 OPT_BOOL('A', "all", &addremove_explicit, N_("add changes from all tracked and untracked files")),
252 OPT_CALLBACK_F(0, "ignore-removal", &addremove_explicit,
253 NULL /* takes no arguments */,
254 N_("ignore paths removed in the working tree (same as --no-all)"),
255 PARSE_OPT_NOARG, ignore_removal_cb),
256 OPT_BOOL( 0 , "refresh", &refresh_only, N_("don't add, only refresh the index")),
257 OPT_BOOL( 0 , "ignore-errors", &ignore_add_errors, N_("just skip files which cannot be added because of errors")),
258 OPT_BOOL( 0 , "ignore-missing", &ignore_missing, N_("check if - even missing - files are ignored in dry run")),
259 OPT_BOOL(0, "sparse", &include_sparse, N_("allow updating entries outside of the sparse-checkout cone")),
260 OPT_STRING(0, "chmod", &chmod_arg, "(+|-)x",
261 N_("override the executable bit of the listed files")),
262 OPT_HIDDEN_BOOL(0, "warn-embedded-repo", &warn_on_embedded_repo,
263 N_("warn when adding an embedded repository")),
264 OPT_PATHSPEC_FROM_FILE(&pathspec_from_file),
265 OPT_PATHSPEC_FILE_NUL(&pathspec_file_nul),
266 OPT_END(),
269 static int add_config(const char *var, const char *value,
270 const struct config_context *ctx, void *cb)
272 if (!strcmp(var, "add.ignoreerrors") ||
273 !strcmp(var, "add.ignore-errors")) {
274 ignore_add_errors = git_config_bool(var, value);
275 return 0;
278 if (git_color_config(var, value, cb) < 0)
279 return -1;
281 return git_default_config(var, value, ctx, cb);
284 static const char embedded_advice[] = N_(
285 "You've added another git repository inside your current repository.\n"
286 "Clones of the outer repository will not contain the contents of\n"
287 "the embedded repository and will not know how to obtain it.\n"
288 "If you meant to add a submodule, use:\n"
289 "\n"
290 " git submodule add <url> %s\n"
291 "\n"
292 "If you added this path by mistake, you can remove it from the\n"
293 "index with:\n"
294 "\n"
295 " git rm --cached %s\n"
296 "\n"
297 "See \"git help submodule\" for more information."
300 static void check_embedded_repo(const char *path)
302 struct strbuf name = STRBUF_INIT;
303 static int adviced_on_embedded_repo = 0;
305 if (!warn_on_embedded_repo)
306 return;
307 if (!ends_with(path, "/"))
308 return;
310 /* Drop trailing slash for aesthetics */
311 strbuf_addstr(&name, path);
312 strbuf_strip_suffix(&name, "/");
314 warning(_("adding embedded git repository: %s"), name.buf);
315 if (!adviced_on_embedded_repo &&
316 advice_enabled(ADVICE_ADD_EMBEDDED_REPO)) {
317 advise(embedded_advice, name.buf, name.buf);
318 adviced_on_embedded_repo = 1;
321 strbuf_release(&name);
324 static int add_files(struct dir_struct *dir, int flags)
326 int i, exit_status = 0;
327 struct string_list matched_sparse_paths = STRING_LIST_INIT_NODUP;
329 if (dir->ignored_nr) {
330 fprintf(stderr, _(ignore_error));
331 for (i = 0; i < dir->ignored_nr; i++)
332 fprintf(stderr, "%s\n", dir->ignored[i]->name);
333 if (advice_enabled(ADVICE_ADD_IGNORED_FILE))
334 advise(_("Use -f if you really want to add them.\n"
335 "Turn this message off by running\n"
336 "\"git config advice.addIgnoredFile false\""));
337 exit_status = 1;
340 for (i = 0; i < dir->nr; i++) {
341 if (!include_sparse &&
342 !path_in_sparse_checkout(dir->entries[i]->name, &the_index)) {
343 string_list_append(&matched_sparse_paths,
344 dir->entries[i]->name);
345 continue;
347 if (add_file_to_index(&the_index, dir->entries[i]->name, flags)) {
348 if (!ignore_add_errors)
349 die(_("adding files failed"));
350 exit_status = 1;
351 } else {
352 check_embedded_repo(dir->entries[i]->name);
356 if (matched_sparse_paths.nr) {
357 advise_on_updating_sparse_paths(&matched_sparse_paths);
358 exit_status = 1;
361 string_list_clear(&matched_sparse_paths, 0);
363 return exit_status;
366 int cmd_add(int argc, const char **argv, const char *prefix)
368 int exit_status = 0;
369 struct pathspec pathspec;
370 struct dir_struct dir = DIR_INIT;
371 int flags;
372 int add_new_files;
373 int require_pathspec;
374 char *seen = NULL;
375 struct lock_file lock_file = LOCK_INIT;
377 git_config(add_config, NULL);
379 argc = parse_options(argc, argv, prefix, builtin_add_options,
380 builtin_add_usage, PARSE_OPT_KEEP_ARGV0);
381 if (patch_interactive)
382 add_interactive = 1;
383 if (add_interactive) {
384 if (show_only)
385 die(_("options '%s' and '%s' cannot be used together"), "--dry-run", "--interactive/--patch");
386 if (pathspec_from_file)
387 die(_("options '%s' and '%s' cannot be used together"), "--pathspec-from-file", "--interactive/--patch");
388 exit(interactive_add(argv + 1, prefix, patch_interactive));
391 if (edit_interactive) {
392 if (pathspec_from_file)
393 die(_("options '%s' and '%s' cannot be used together"), "--pathspec-from-file", "--edit");
394 return(edit_patch(argc, argv, prefix));
396 argc--;
397 argv++;
399 if (0 <= addremove_explicit)
400 addremove = addremove_explicit;
401 else if (take_worktree_changes && ADDREMOVE_DEFAULT)
402 addremove = 0; /* "-u" was given but not "-A" */
404 if (addremove && take_worktree_changes)
405 die(_("options '%s' and '%s' cannot be used together"), "-A", "-u");
407 if (!show_only && ignore_missing)
408 die(_("the option '%s' requires '%s'"), "--ignore-missing", "--dry-run");
410 if (chmod_arg && ((chmod_arg[0] != '-' && chmod_arg[0] != '+') ||
411 chmod_arg[1] != 'x' || chmod_arg[2]))
412 die(_("--chmod param '%s' must be either -x or +x"), chmod_arg);
414 add_new_files = !take_worktree_changes && !refresh_only && !add_renormalize;
415 require_pathspec = !(take_worktree_changes || (0 < addremove_explicit));
417 prepare_repo_settings(the_repository);
418 the_repository->settings.command_requires_full_index = 0;
420 repo_hold_locked_index(the_repository, &lock_file, LOCK_DIE_ON_ERROR);
423 * Check the "pathspec '%s' did not match any files" block
424 * below before enabling new magic.
426 parse_pathspec(&pathspec, PATHSPEC_ATTR,
427 PATHSPEC_PREFER_FULL |
428 PATHSPEC_SYMLINK_LEADING_PATH,
429 prefix, argv);
431 if (pathspec_from_file) {
432 if (pathspec.nr)
433 die(_("'%s' and pathspec arguments cannot be used together"), "--pathspec-from-file");
435 parse_pathspec_file(&pathspec, PATHSPEC_ATTR,
436 PATHSPEC_PREFER_FULL |
437 PATHSPEC_SYMLINK_LEADING_PATH,
438 prefix, pathspec_from_file, pathspec_file_nul);
439 } else if (pathspec_file_nul) {
440 die(_("the option '%s' requires '%s'"), "--pathspec-file-nul", "--pathspec-from-file");
443 if (require_pathspec && pathspec.nr == 0) {
444 fprintf(stderr, _("Nothing specified, nothing added.\n"));
445 if (advice_enabled(ADVICE_ADD_EMPTY_PATHSPEC))
446 advise( _("Maybe you wanted to say 'git add .'?\n"
447 "Turn this message off by running\n"
448 "\"git config advice.addEmptyPathspec false\""));
449 return 0;
452 if (!take_worktree_changes && addremove_explicit < 0 && pathspec.nr)
453 /* Turn "git add pathspec..." to "git add -A pathspec..." */
454 addremove = 1;
456 flags = ((verbose ? ADD_CACHE_VERBOSE : 0) |
457 (show_only ? ADD_CACHE_PRETEND : 0) |
458 (intent_to_add ? ADD_CACHE_INTENT : 0) |
459 (ignore_add_errors ? ADD_CACHE_IGNORE_ERRORS : 0) |
460 (!(addremove || take_worktree_changes)
461 ? ADD_CACHE_IGNORE_REMOVAL : 0));
463 if (repo_read_index_preload(the_repository, &pathspec, 0) < 0)
464 die(_("index file corrupt"));
466 die_in_unpopulated_submodule(&the_index, prefix);
467 die_path_inside_submodule(&the_index, &pathspec);
469 if (add_new_files) {
470 int baselen;
472 /* Set up the default git porcelain excludes */
473 if (!ignored_too) {
474 dir.flags |= DIR_COLLECT_IGNORED;
475 setup_standard_excludes(&dir);
478 /* This picks up the paths that are not tracked */
479 baselen = fill_directory(&dir, &the_index, &pathspec);
480 if (pathspec.nr)
481 seen = prune_directory(&dir, &pathspec, baselen);
484 if (refresh_only) {
485 exit_status |= refresh(verbose, &pathspec);
486 goto finish;
489 if (pathspec.nr) {
490 int i;
491 char *skip_worktree_seen = NULL;
492 struct string_list only_match_skip_worktree = STRING_LIST_INIT_NODUP;
494 if (!seen)
495 seen = find_pathspecs_matching_against_index(&pathspec,
496 &the_index, PS_IGNORE_SKIP_WORKTREE);
499 * file_exists() assumes exact match
501 GUARD_PATHSPEC(&pathspec,
502 PATHSPEC_FROMTOP |
503 PATHSPEC_LITERAL |
504 PATHSPEC_GLOB |
505 PATHSPEC_ICASE |
506 PATHSPEC_EXCLUDE);
508 for (i = 0; i < pathspec.nr; i++) {
509 const char *path = pathspec.items[i].match;
511 if (pathspec.items[i].magic & PATHSPEC_EXCLUDE)
512 continue;
513 if (seen[i])
514 continue;
516 if (!include_sparse &&
517 matches_skip_worktree(&pathspec, i, &skip_worktree_seen)) {
518 string_list_append(&only_match_skip_worktree,
519 pathspec.items[i].original);
520 continue;
523 /* Don't complain at 'git add .' on empty repo */
524 if (!path[0])
525 continue;
527 if ((pathspec.items[i].magic & (PATHSPEC_GLOB | PATHSPEC_ICASE)) ||
528 !file_exists(path)) {
529 if (ignore_missing) {
530 int dtype = DT_UNKNOWN;
531 if (is_excluded(&dir, &the_index, path, &dtype))
532 dir_add_ignored(&dir, &the_index,
533 path, pathspec.items[i].len);
534 } else
535 die(_("pathspec '%s' did not match any files"),
536 pathspec.items[i].original);
541 if (only_match_skip_worktree.nr) {
542 advise_on_updating_sparse_paths(&only_match_skip_worktree);
543 exit_status = 1;
546 free(seen);
547 free(skip_worktree_seen);
548 string_list_clear(&only_match_skip_worktree, 0);
551 begin_odb_transaction();
553 if (add_renormalize)
554 exit_status |= renormalize_tracked_files(&pathspec, flags);
555 else
556 exit_status |= add_files_to_cache(the_repository, prefix,
557 &pathspec, include_sparse,
558 flags);
560 if (add_new_files)
561 exit_status |= add_files(&dir, flags);
563 if (chmod_arg && pathspec.nr)
564 exit_status |= chmod_pathspec(&pathspec, chmod_arg[0], show_only);
565 end_odb_transaction();
567 finish:
568 if (write_locked_index(&the_index, &lock_file,
569 COMMIT_LOCK | SKIP_IF_UNCHANGED))
570 die(_("Unable to write new index file"));
572 dir_clear(&dir);
573 clear_pathspec(&pathspec);
574 return exit_status;