The twelfth batch
[git/gitster.git] / builtin / add.c
blob7d353077921ad8850088f237ce96fc481039cd0d
1 /*
2 * "git add" builtin command
4 * Copyright (C) 2006 Linus Torvalds
5 */
6 #include "builtin.h"
7 #include "advice.h"
8 #include "config.h"
9 #include "lockfile.h"
10 #include "editor.h"
11 #include "dir.h"
12 #include "gettext.h"
13 #include "pathspec.h"
14 #include "run-command.h"
15 #include "parse-options.h"
16 #include "path.h"
17 #include "preload-index.h"
18 #include "diff.h"
19 #include "read-cache.h"
20 #include "revision.h"
21 #include "bulk-checkin.h"
22 #include "strvec.h"
23 #include "submodule.h"
24 #include "add-interactive.h"
26 static const char * const builtin_add_usage[] = {
27 N_("git add [<options>] [--] <pathspec>..."),
28 NULL
30 static int patch_interactive, add_interactive, edit_interactive;
31 static int take_worktree_changes;
32 static int add_renormalize;
33 static int pathspec_file_nul;
34 static int include_sparse;
35 static const char *pathspec_from_file;
37 static int chmod_pathspec(struct repository *repo,
38 struct pathspec *pathspec,
39 char flip,
40 int show_only)
42 int i, ret = 0;
44 for (i = 0; i < repo->index->cache_nr; i++) {
45 struct cache_entry *ce = repo->index->cache[i];
46 int err;
48 if (!include_sparse &&
49 (ce_skip_worktree(ce) ||
50 !path_in_sparse_checkout(ce->name, repo->index)))
51 continue;
53 if (pathspec && !ce_path_match(repo->index, ce, pathspec, NULL))
54 continue;
56 if (!show_only)
57 err = chmod_index_entry(repo->index, ce, flip);
58 else
59 err = S_ISREG(ce->ce_mode) ? 0 : -1;
61 if (err < 0)
62 ret = error(_("cannot chmod %cx '%s'"), flip, ce->name);
65 return ret;
68 static int renormalize_tracked_files(struct repository *repo,
69 const struct pathspec *pathspec,
70 int flags)
72 int i, retval = 0;
74 for (i = 0; i < repo->index->cache_nr; i++) {
75 struct cache_entry *ce = repo->index->cache[i];
77 if (!include_sparse &&
78 (ce_skip_worktree(ce) ||
79 !path_in_sparse_checkout(ce->name, repo->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(repo->index, ce, pathspec, NULL))
86 continue;
87 retval |= add_file_to_index(repo->index, ce->name,
88 flags | ADD_CACHE_RENORMALIZE);
91 return retval;
94 static char *prune_directory(struct repository *repo,
95 struct dir_struct *dir,
96 struct pathspec *pathspec,
97 int prefix)
99 char *seen;
100 int i;
101 struct dir_entry **src, **dst;
103 seen = xcalloc(pathspec->nr, 1);
105 src = dst = dir->entries;
106 i = dir->nr;
107 while (--i >= 0) {
108 struct dir_entry *entry = *src++;
109 if (dir_path_match(repo->index, entry, pathspec, prefix, seen))
110 *dst++ = entry;
112 dir->nr = dst - dir->entries;
113 add_pathspec_matches_against_index(pathspec, repo->index, seen,
114 PS_IGNORE_SKIP_WORKTREE);
115 return seen;
118 static int refresh(struct repository *repo, int verbose, const struct pathspec *pathspec)
120 char *seen;
121 int i, ret = 0;
122 char *skip_worktree_seen = NULL;
123 struct string_list only_match_skip_worktree = STRING_LIST_INIT_NODUP;
124 unsigned int flags = REFRESH_IGNORE_SKIP_WORKTREE |
125 (verbose ? REFRESH_IN_PORCELAIN : REFRESH_QUIET);
127 seen = xcalloc(pathspec->nr, 1);
128 refresh_index(repo->index, flags, pathspec, seen,
129 _("Unstaged changes after refreshing the index:"));
130 for (i = 0; i < pathspec->nr; i++) {
131 if (!seen[i]) {
132 const char *path = pathspec->items[i].original;
134 if (matches_skip_worktree(pathspec, i, &skip_worktree_seen) ||
135 !path_in_sparse_checkout(path, repo->index)) {
136 string_list_append(&only_match_skip_worktree,
137 pathspec->items[i].original);
138 } else {
139 die(_("pathspec '%s' did not match any files"),
140 pathspec->items[i].original);
145 if (only_match_skip_worktree.nr) {
146 advise_on_updating_sparse_paths(&only_match_skip_worktree);
147 ret = 1;
150 free(seen);
151 free(skip_worktree_seen);
152 string_list_clear(&only_match_skip_worktree, 0);
153 return ret;
156 int interactive_add(struct repository *repo,
157 const char **argv,
158 const char *prefix,
159 int patch)
161 struct pathspec pathspec;
162 int ret;
164 parse_pathspec(&pathspec, 0,
165 PATHSPEC_PREFER_FULL |
166 PATHSPEC_SYMLINK_LEADING_PATH |
167 PATHSPEC_PREFIX_ORIGIN,
168 prefix, argv);
170 if (patch)
171 ret = !!run_add_p(repo, ADD_P_ADD, NULL, &pathspec);
172 else
173 ret = !!run_add_i(repo, &pathspec);
175 clear_pathspec(&pathspec);
176 return ret;
179 static int edit_patch(struct repository *repo,
180 int argc,
181 const char **argv,
182 const char *prefix)
184 char *file = repo_git_path(repo, "ADD_EDIT.patch");
185 struct child_process child = CHILD_PROCESS_INIT;
186 struct rev_info rev;
187 int out;
188 struct stat st;
190 repo_config(repo, git_diff_basic_config, NULL);
192 if (repo_read_index(repo) < 0)
193 die(_("could not read the index"));
195 repo_init_revisions(repo, &rev, prefix);
196 rev.diffopt.context = 7;
198 argc = setup_revisions(argc, argv, &rev, NULL);
199 rev.diffopt.output_format = DIFF_FORMAT_PATCH;
200 rev.diffopt.use_color = 0;
201 rev.diffopt.flags.ignore_dirty_submodules = 1;
202 out = xopen(file, O_CREAT | O_WRONLY | O_TRUNC, 0666);
203 rev.diffopt.file = xfdopen(out, "w");
204 rev.diffopt.close_file = 1;
205 run_diff_files(&rev, 0);
207 if (launch_editor(file, NULL, NULL))
208 die(_("editing patch failed"));
210 if (stat(file, &st))
211 die_errno(_("could not stat '%s'"), file);
212 if (!st.st_size)
213 die(_("empty patch. aborted"));
215 child.git_cmd = 1;
216 strvec_pushl(&child.args, "apply", "--recount", "--cached", file,
217 NULL);
218 if (run_command(&child))
219 die(_("could not apply '%s'"), file);
221 unlink(file);
222 free(file);
223 release_revisions(&rev);
224 return 0;
227 static const char ignore_error[] =
228 N_("The following paths are ignored by one of your .gitignore files:\n");
230 static int verbose, show_only, ignored_too, refresh_only;
231 static int ignore_add_errors, intent_to_add, ignore_missing;
232 static int warn_on_embedded_repo = 1;
234 #define ADDREMOVE_DEFAULT 1
235 static int addremove = ADDREMOVE_DEFAULT;
236 static int addremove_explicit = -1; /* unspecified */
238 static char *chmod_arg;
240 static int ignore_removal_cb(const struct option *opt, const char *arg, int unset)
242 BUG_ON_OPT_ARG(arg);
244 /* if we are told to ignore, we are not adding removals */
245 *(int *)opt->value = !unset ? 0 : 1;
246 return 0;
249 static struct option builtin_add_options[] = {
250 OPT__DRY_RUN(&show_only, N_("dry run")),
251 OPT__VERBOSE(&verbose, N_("be verbose")),
252 OPT_GROUP(""),
253 OPT_BOOL('i', "interactive", &add_interactive, N_("interactive picking")),
254 OPT_BOOL('p', "patch", &patch_interactive, N_("select hunks interactively")),
255 OPT_BOOL('e', "edit", &edit_interactive, N_("edit current diff and apply")),
256 OPT__FORCE(&ignored_too, N_("allow adding otherwise ignored files"), 0),
257 OPT_BOOL('u', "update", &take_worktree_changes, N_("update tracked files")),
258 OPT_BOOL(0, "renormalize", &add_renormalize, N_("renormalize EOL of tracked files (implies -u)")),
259 OPT_BOOL('N', "intent-to-add", &intent_to_add, N_("record only the fact that the path will be added later")),
260 OPT_BOOL('A', "all", &addremove_explicit, N_("add changes from all tracked and untracked files")),
261 OPT_CALLBACK_F(0, "ignore-removal", &addremove_explicit,
262 NULL /* takes no arguments */,
263 N_("ignore paths removed in the working tree (same as --no-all)"),
264 PARSE_OPT_NOARG, ignore_removal_cb),
265 OPT_BOOL( 0 , "refresh", &refresh_only, N_("don't add, only refresh the index")),
266 OPT_BOOL( 0 , "ignore-errors", &ignore_add_errors, N_("just skip files which cannot be added because of errors")),
267 OPT_BOOL( 0 , "ignore-missing", &ignore_missing, N_("check if - even missing - files are ignored in dry run")),
268 OPT_BOOL(0, "sparse", &include_sparse, N_("allow updating entries outside of the sparse-checkout cone")),
269 OPT_STRING(0, "chmod", &chmod_arg, "(+|-)x",
270 N_("override the executable bit of the listed files")),
271 OPT_HIDDEN_BOOL(0, "warn-embedded-repo", &warn_on_embedded_repo,
272 N_("warn when adding an embedded repository")),
273 OPT_PATHSPEC_FROM_FILE(&pathspec_from_file),
274 OPT_PATHSPEC_FILE_NUL(&pathspec_file_nul),
275 OPT_END(),
278 static int add_config(const char *var, const char *value,
279 const struct config_context *ctx, void *cb)
281 if (!strcmp(var, "add.ignoreerrors") ||
282 !strcmp(var, "add.ignore-errors")) {
283 ignore_add_errors = git_config_bool(var, value);
284 return 0;
287 if (git_color_config(var, value, cb) < 0)
288 return -1;
290 return git_default_config(var, value, ctx, cb);
293 static const char embedded_advice[] = N_(
294 "You've added another git repository inside your current repository.\n"
295 "Clones of the outer repository will not contain the contents of\n"
296 "the embedded repository and will not know how to obtain it.\n"
297 "If you meant to add a submodule, use:\n"
298 "\n"
299 " git submodule add <url> %s\n"
300 "\n"
301 "If you added this path by mistake, you can remove it from the\n"
302 "index with:\n"
303 "\n"
304 " git rm --cached %s\n"
305 "\n"
306 "See \"git help submodule\" for more information."
309 static void check_embedded_repo(const char *path)
311 struct strbuf name = STRBUF_INIT;
312 static int adviced_on_embedded_repo = 0;
314 if (!warn_on_embedded_repo)
315 return;
316 if (!ends_with(path, "/"))
317 return;
319 /* Drop trailing slash for aesthetics */
320 strbuf_addstr(&name, path);
321 strbuf_strip_suffix(&name, "/");
323 warning(_("adding embedded git repository: %s"), name.buf);
324 if (!adviced_on_embedded_repo) {
325 advise_if_enabled(ADVICE_ADD_EMBEDDED_REPO,
326 embedded_advice, name.buf, name.buf);
327 adviced_on_embedded_repo = 1;
330 strbuf_release(&name);
333 static int add_files(struct repository *repo, struct dir_struct *dir, int flags)
335 int i, exit_status = 0;
336 struct string_list matched_sparse_paths = STRING_LIST_INIT_NODUP;
338 if (dir->ignored_nr) {
339 fprintf(stderr, _(ignore_error));
340 for (i = 0; i < dir->ignored_nr; i++)
341 fprintf(stderr, "%s\n", dir->ignored[i]->name);
342 advise_if_enabled(ADVICE_ADD_IGNORED_FILE,
343 _("Use -f if you really want to add them."));
344 exit_status = 1;
347 for (i = 0; i < dir->nr; i++) {
348 if (!include_sparse &&
349 !path_in_sparse_checkout(dir->entries[i]->name, repo->index)) {
350 string_list_append(&matched_sparse_paths,
351 dir->entries[i]->name);
352 continue;
354 if (add_file_to_index(repo->index, dir->entries[i]->name, flags)) {
355 if (!ignore_add_errors)
356 die(_("adding files failed"));
357 exit_status = 1;
358 } else {
359 check_embedded_repo(dir->entries[i]->name);
363 if (matched_sparse_paths.nr) {
364 advise_on_updating_sparse_paths(&matched_sparse_paths);
365 exit_status = 1;
368 string_list_clear(&matched_sparse_paths, 0);
370 return exit_status;
373 int cmd_add(int argc,
374 const char **argv,
375 const char *prefix,
376 struct repository *repo)
378 int exit_status = 0;
379 struct pathspec pathspec;
380 struct dir_struct dir = DIR_INIT;
381 int flags;
382 int add_new_files;
383 int require_pathspec;
384 char *seen = NULL;
385 char *ps_matched = NULL;
386 struct lock_file lock_file = LOCK_INIT;
388 if (repo)
389 repo_config(repo, add_config, NULL);
391 argc = parse_options(argc, argv, prefix, builtin_add_options,
392 builtin_add_usage, PARSE_OPT_KEEP_ARGV0);
393 if (patch_interactive)
394 add_interactive = 1;
395 if (add_interactive) {
396 if (show_only)
397 die(_("options '%s' and '%s' cannot be used together"), "--dry-run", "--interactive/--patch");
398 if (pathspec_from_file)
399 die(_("options '%s' and '%s' cannot be used together"), "--pathspec-from-file", "--interactive/--patch");
400 exit(interactive_add(repo, argv + 1, prefix, patch_interactive));
403 if (edit_interactive) {
404 if (pathspec_from_file)
405 die(_("options '%s' and '%s' cannot be used together"), "--pathspec-from-file", "--edit");
406 return(edit_patch(repo, argc, argv, prefix));
408 argc--;
409 argv++;
411 if (0 <= addremove_explicit)
412 addremove = addremove_explicit;
413 else if (take_worktree_changes && ADDREMOVE_DEFAULT)
414 addremove = 0; /* "-u" was given but not "-A" */
416 if (addremove && take_worktree_changes)
417 die(_("options '%s' and '%s' cannot be used together"), "-A", "-u");
419 if (!show_only && ignore_missing)
420 die(_("the option '%s' requires '%s'"), "--ignore-missing", "--dry-run");
422 if (chmod_arg && ((chmod_arg[0] != '-' && chmod_arg[0] != '+') ||
423 chmod_arg[1] != 'x' || chmod_arg[2]))
424 die(_("--chmod param '%s' must be either -x or +x"), chmod_arg);
426 add_new_files = !take_worktree_changes && !refresh_only && !add_renormalize;
427 require_pathspec = !(take_worktree_changes || (0 < addremove_explicit));
429 prepare_repo_settings(repo);
430 repo->settings.command_requires_full_index = 0;
432 repo_hold_locked_index(repo, &lock_file, LOCK_DIE_ON_ERROR);
435 * Check the "pathspec '%s' did not match any files" block
436 * below before enabling new magic.
438 parse_pathspec(&pathspec, 0,
439 PATHSPEC_PREFER_FULL |
440 PATHSPEC_SYMLINK_LEADING_PATH,
441 prefix, argv);
443 if (pathspec_from_file) {
444 if (pathspec.nr)
445 die(_("'%s' and pathspec arguments cannot be used together"), "--pathspec-from-file");
447 parse_pathspec_file(&pathspec, 0,
448 PATHSPEC_PREFER_FULL |
449 PATHSPEC_SYMLINK_LEADING_PATH,
450 prefix, pathspec_from_file, pathspec_file_nul);
451 } else if (pathspec_file_nul) {
452 die(_("the option '%s' requires '%s'"), "--pathspec-file-nul", "--pathspec-from-file");
455 if (require_pathspec && pathspec.nr == 0) {
456 fprintf(stderr, _("Nothing specified, nothing added.\n"));
457 advise_if_enabled(ADVICE_ADD_EMPTY_PATHSPEC,
458 _("Maybe you wanted to say 'git add .'?"));
459 return 0;
462 if (!take_worktree_changes && addremove_explicit < 0 && pathspec.nr)
463 /* Turn "git add pathspec..." to "git add -A pathspec..." */
464 addremove = 1;
466 flags = ((verbose ? ADD_CACHE_VERBOSE : 0) |
467 (show_only ? ADD_CACHE_PRETEND : 0) |
468 (intent_to_add ? ADD_CACHE_INTENT : 0) |
469 (ignore_add_errors ? ADD_CACHE_IGNORE_ERRORS : 0) |
470 (!(addremove || take_worktree_changes)
471 ? ADD_CACHE_IGNORE_REMOVAL : 0));
473 if (repo_read_index_preload(repo, &pathspec, 0) < 0)
474 die(_("index file corrupt"));
476 die_in_unpopulated_submodule(repo->index, prefix);
477 die_path_inside_submodule(repo->index, &pathspec);
479 if (add_new_files) {
480 int baselen;
482 /* Set up the default git porcelain excludes */
483 if (!ignored_too) {
484 dir.flags |= DIR_COLLECT_IGNORED;
485 setup_standard_excludes(&dir);
488 /* This picks up the paths that are not tracked */
489 baselen = fill_directory(&dir, repo->index, &pathspec);
490 if (pathspec.nr)
491 seen = prune_directory(repo, &dir, &pathspec, baselen);
494 if (refresh_only) {
495 exit_status |= refresh(repo, verbose, &pathspec);
496 goto finish;
499 if (pathspec.nr) {
500 int i;
501 char *skip_worktree_seen = NULL;
502 struct string_list only_match_skip_worktree = STRING_LIST_INIT_NODUP;
504 if (!seen)
505 seen = find_pathspecs_matching_against_index(&pathspec,
506 repo->index, PS_IGNORE_SKIP_WORKTREE);
509 * file_exists() assumes exact match
511 GUARD_PATHSPEC(&pathspec,
512 PATHSPEC_FROMTOP |
513 PATHSPEC_LITERAL |
514 PATHSPEC_GLOB |
515 PATHSPEC_ICASE |
516 PATHSPEC_EXCLUDE |
517 PATHSPEC_ATTR);
519 for (i = 0; i < pathspec.nr; i++) {
520 const char *path = pathspec.items[i].match;
522 if (pathspec.items[i].magic & PATHSPEC_EXCLUDE)
523 continue;
524 if (seen[i])
525 continue;
527 if (!include_sparse &&
528 matches_skip_worktree(&pathspec, i, &skip_worktree_seen)) {
529 string_list_append(&only_match_skip_worktree,
530 pathspec.items[i].original);
531 continue;
534 /* Don't complain at 'git add .' on empty repo */
535 if (!path[0])
536 continue;
538 if ((pathspec.items[i].magic & (PATHSPEC_GLOB | PATHSPEC_ICASE)) ||
539 !file_exists(path)) {
540 if (ignore_missing) {
541 int dtype = DT_UNKNOWN;
542 if (is_excluded(&dir, repo->index, path, &dtype))
543 dir_add_ignored(&dir, repo->index,
544 path, pathspec.items[i].len);
545 } else
546 die(_("pathspec '%s' did not match any files"),
547 pathspec.items[i].original);
552 if (only_match_skip_worktree.nr) {
553 advise_on_updating_sparse_paths(&only_match_skip_worktree);
554 exit_status = 1;
557 free(seen);
558 free(skip_worktree_seen);
559 string_list_clear(&only_match_skip_worktree, 0);
562 begin_odb_transaction();
564 ps_matched = xcalloc(pathspec.nr, 1);
565 if (add_renormalize)
566 exit_status |= renormalize_tracked_files(repo, &pathspec, flags);
567 else
568 exit_status |= add_files_to_cache(repo, prefix,
569 &pathspec, ps_matched,
570 include_sparse, flags);
572 if (take_worktree_changes && !add_renormalize && !ignore_add_errors &&
573 report_path_error(ps_matched, &pathspec))
574 exit(128);
576 if (add_new_files)
577 exit_status |= add_files(repo, &dir, flags);
579 if (chmod_arg && pathspec.nr)
580 exit_status |= chmod_pathspec(repo, &pathspec, chmod_arg[0], show_only);
581 end_odb_transaction();
583 finish:
584 if (write_locked_index(repo->index, &lock_file,
585 COMMIT_LOCK | SKIP_IF_UNCHANGED))
586 die(_("unable to write new index file"));
588 free(ps_matched);
589 dir_clear(&dir);
590 clear_pathspec(&pathspec);
591 return exit_status;