blob: 54cd2d417d30408c8a4b13099efc6eee6a3d1e31 [file] [log] [blame]
Linus Torvalds0d781532006-05-17 09:33:32 -07001/*
2 * "git add" builtin command
3 *
4 * Copyright (C) 2006 Linus Torvalds
5 */
Linus Torvalds0d781532006-05-17 09:33:32 -07006#include "cache.h"
7#include "builtin.h"
8#include "dir.h"
Adam Spiers6f525e72013-01-06 16:58:08 +00009#include "pathspec.h"
Junio C Hamano5cde71d2006-12-10 20:55:50 -080010#include "exec_cmd.h"
Junio C Hamano93872e02006-05-20 01:28:49 -070011#include "cache-tree.h"
Kristian Høgsberg58680162007-09-17 20:06:44 -040012#include "run-command.h"
Kristian Høgsberg5c46f752007-10-03 17:45:02 -040013#include "parse-options.h"
Johannes Schindelinc59cb032009-04-08 23:30:24 +020014#include "diff.h"
Linus Torvaldsfb7d3f32010-01-21 11:37:38 -080015#include "diffcore.h"
Johannes Schindelinc59cb032009-04-08 23:30:24 +020016#include "revision.h"
Junio C Hamano568508e2011-10-28 14:48:40 -070017#include "bulk-checkin.h"
Linus Torvalds0d781532006-05-17 09:33:32 -070018
Kristian Høgsberg5c46f752007-10-03 17:45:02 -040019static const char * const builtin_add_usage[] = {
Matthieu Moyd32805d2013-02-12 10:24:44 +010020 N_("git add [options] [--] <pathspec>..."),
Kristian Høgsberg5c46f752007-10-03 17:45:02 -040021 NULL
22};
Johannes Schindelinc59cb032009-04-08 23:30:24 +020023static int patch_interactive, add_interactive, edit_interactive;
Jeff King93c44d42007-05-12 02:42:00 -040024static int take_worktree_changes;
James Bowes896bdfa2007-02-27 22:31:10 -050025
Jonathan Nieder9cba13c2011-03-16 02:08:34 -050026struct update_callback_data {
Linus Torvaldsfb7d3f32010-01-21 11:37:38 -080027 int flags;
28 int add_errors;
Jonathan Nieder71c7b052013-03-19 15:50:50 -070029 const char *implicit_dot;
30 size_t implicit_dot_len;
Junio C Hamanod226b142013-04-17 12:32:21 -070031
32 /* only needed for 2.0 transition preparation */
33 int warn_add_would_remove;
Linus Torvaldsfb7d3f32010-01-21 11:37:38 -080034};
35
Jonathan Niedere24afab2013-03-19 15:45:26 -070036static const char *option_with_implicit_dot;
37static const char *short_option_with_implicit_dot;
38
39static void warn_pathless_add(void)
40{
Jonathan Nieder16d41d42013-03-19 15:45:52 -070041 static int shown;
Jonathan Niedere24afab2013-03-19 15:45:26 -070042 assert(option_with_implicit_dot && short_option_with_implicit_dot);
43
Jonathan Nieder16d41d42013-03-19 15:45:52 -070044 if (shown)
45 return;
46 shown = 1;
47
Jonathan Niedere24afab2013-03-19 15:45:26 -070048 /*
49 * To be consistent with "git add -p" and most Git
50 * commands, we should default to being tree-wide, but
51 * this is not the original behavior and can't be
52 * changed until users trained themselves not to type
53 * "git add -u" or "git add -A". For now, we warn and
54 * keep the old behavior. Later, the behavior can be changed
55 * to tree-wide, keeping the warning for a while, and
56 * eventually we can drop the warning.
57 */
58 warning(_("The behavior of 'git add %s (or %s)' with no path argument from a\n"
59 "subdirectory of the tree will change in Git 2.0 and should not be used anymore.\n"
60 "To add content for the whole tree, run:\n"
61 "\n"
62 " git add %s :/\n"
63 " (or git add %s :/)\n"
64 "\n"
65 "To restrict the command to the current directory, run:\n"
66 "\n"
67 " git add %s .\n"
68 " (or git add %s .)\n"
69 "\n"
Matthieu Moyc9f35b82013-03-11 09:01:33 +010070 "With the current Git version, the command is restricted to "
71 "the current directory.\n"
72 ""),
Jonathan Niedere24afab2013-03-19 15:45:26 -070073 option_with_implicit_dot, short_option_with_implicit_dot,
74 option_with_implicit_dot, short_option_with_implicit_dot,
75 option_with_implicit_dot, short_option_with_implicit_dot);
76}
77
Junio C Hamano75973b22011-04-20 18:11:19 -070078static int fix_unmerged_status(struct diff_filepair *p,
79 struct update_callback_data *data)
80{
81 if (p->status != DIFF_STATUS_UNMERGED)
82 return p->status;
83 if (!(data->flags & ADD_CACHE_IGNORE_REMOVAL) && !p->two->mode)
84 /*
85 * This is not an explicit add request, and the
86 * path is missing from the working tree (deleted)
87 */
88 return DIFF_STATUS_DELETED;
89 else
90 /*
91 * Either an explicit add request, or path exists
92 * in the working tree. An attempt to explicitly
93 * add a path that does not exist in the working tree
94 * will be caught as an error by the caller immediately.
95 */
96 return DIFF_STATUS_MODIFIED;
97}
98
Junio C Hamanoccc663b2013-04-21 21:04:35 -070099static const char *add_would_remove_warning = N_(
100 "You ran 'git add' with neither '-A (--all)' or '--no-all', whose\n"
101"behaviour will change in Git 2.0 with respect to paths you removed from\n"
102"your working tree. Paths like '%s' that are\n"
103"removed are ignored with this version of Git.\n"
104"\n"
105"* 'git add --no-all <pathspec>', which is the current default, ignores\n"
106" paths you removed from your working tree.\n"
107"\n"
108"* 'git add --all <pathspec>' will let you also record the removals.\n"
109"\n"
110"Run 'git status' to check the paths you removed from your working tree.\n");
111
Junio C Hamanod226b142013-04-17 12:32:21 -0700112static void warn_add_would_remove(const char *path)
113{
Junio C Hamanoccc663b2013-04-21 21:04:35 -0700114 warning(_(add_would_remove_warning), path);
Junio C Hamanod226b142013-04-17 12:32:21 -0700115}
116
Linus Torvaldsfb7d3f32010-01-21 11:37:38 -0800117static void update_callback(struct diff_queue_struct *q,
118 struct diff_options *opt, void *cbdata)
119{
120 int i;
121 struct update_callback_data *data = cbdata;
Jonathan Nieder71c7b052013-03-19 15:50:50 -0700122 const char *implicit_dot = data->implicit_dot;
123 size_t implicit_dot_len = data->implicit_dot_len;
Linus Torvaldsfb7d3f32010-01-21 11:37:38 -0800124
125 for (i = 0; i < q->nr; i++) {
126 struct diff_filepair *p = q->queue[i];
127 const char *path = p->one->path;
Jonathan Nieder71c7b052013-03-19 15:50:50 -0700128 /*
129 * Check if "git add -A" or "git add -u" was run from a
130 * subdirectory with a modified file outside that directory,
131 * and warn if so.
132 *
133 * "git add -u" will behave like "git add -u :/" instead of
134 * "git add -u ." in the future. This warning prepares for
135 * that change.
136 */
137 if (implicit_dot &&
138 strncmp_icase(path, implicit_dot, implicit_dot_len)) {
139 warn_pathless_add();
140 continue;
141 }
Junio C Hamano75973b22011-04-20 18:11:19 -0700142 switch (fix_unmerged_status(p, data)) {
Linus Torvaldsfb7d3f32010-01-21 11:37:38 -0800143 default:
Ævar Arnfjörð Bjarmason990ac4b2011-02-22 23:41:29 +0000144 die(_("unexpected diff status %c"), p->status);
Linus Torvaldsfb7d3f32010-01-21 11:37:38 -0800145 case DIFF_STATUS_MODIFIED:
146 case DIFF_STATUS_TYPE_CHANGED:
147 if (add_file_to_index(&the_index, path, data->flags)) {
148 if (!(data->flags & ADD_CACHE_IGNORE_ERRORS))
Ævar Arnfjörð Bjarmason990ac4b2011-02-22 23:41:29 +0000149 die(_("updating files failed"));
Linus Torvaldsfb7d3f32010-01-21 11:37:38 -0800150 data->add_errors++;
151 }
152 break;
153 case DIFF_STATUS_DELETED:
Junio C Hamanod226b142013-04-17 12:32:21 -0700154 if (data->warn_add_would_remove) {
155 warn_add_would_remove(path);
156 data->warn_add_would_remove = 0;
157 }
Linus Torvaldsfb7d3f32010-01-21 11:37:38 -0800158 if (data->flags & ADD_CACHE_IGNORE_REMOVAL)
159 break;
160 if (!(data->flags & ADD_CACHE_PRETEND))
161 remove_file_from_index(&the_index, path);
162 if (data->flags & (ADD_CACHE_PRETEND|ADD_CACHE_VERBOSE))
Ævar Arnfjörð Bjarmason475c73e2011-02-22 23:41:32 +0000163 printf(_("remove '%s'\n"), path);
Linus Torvaldsfb7d3f32010-01-21 11:37:38 -0800164 break;
165 }
166 }
167}
168
Junio C Hamanod226b142013-04-17 12:32:21 -0700169static void update_files_in_cache(const char *prefix, const char **pathspec,
170 struct update_callback_data *data)
Linus Torvaldsfb7d3f32010-01-21 11:37:38 -0800171{
Linus Torvaldsfb7d3f32010-01-21 11:37:38 -0800172 struct rev_info rev;
Jonathan Nieder71c7b052013-03-19 15:50:50 -0700173
Linus Torvaldsfb7d3f32010-01-21 11:37:38 -0800174 init_revisions(&rev, prefix);
175 setup_revisions(0, NULL, &rev, NULL);
Nguyễn Thái Ngọc Duyafe069d2010-12-17 19:43:06 +0700176 init_pathspec(&rev.prune_data, pathspec);
Linus Torvaldsfb7d3f32010-01-21 11:37:38 -0800177 rev.diffopt.output_format = DIFF_FORMAT_CALLBACK;
178 rev.diffopt.format_callback = update_callback;
Junio C Hamanod226b142013-04-17 12:32:21 -0700179 rev.diffopt.format_callback_data = data;
Junio C Hamano75973b22011-04-20 18:11:19 -0700180 rev.max_count = 0; /* do not compare unmerged paths with stage #2 */
Linus Torvaldsfb7d3f32010-01-21 11:37:38 -0800181 run_diff_files(&rev, DIFF_RACY_IS_MODIFIED);
Junio C Hamanod226b142013-04-17 12:32:21 -0700182}
183
184int add_files_to_cache(const char *prefix, const char **pathspec, int flags)
185{
186 struct update_callback_data data;
187
188 memset(&data, 0, sizeof(data));
189 data.flags = flags;
190 update_files_in_cache(prefix, pathspec, &data);
Linus Torvaldsfb7d3f32010-01-21 11:37:38 -0800191 return !!data.add_errors;
192}
193
Jonathan Nieder9df84e92013-03-19 15:51:34 -0700194#define WARN_IMPLICIT_DOT (1u << 0)
195static char *prune_directory(struct dir_struct *dir, const char **pathspec,
196 int prefix, unsigned flag)
Linus Torvalds0d781532006-05-17 09:33:32 -0700197{
Linus Torvaldsf2593392006-05-17 13:23:19 -0700198 char *seen;
199 int i, specs;
Linus Torvalds0d781532006-05-17 09:33:32 -0700200 struct dir_entry **src, **dst;
201
Linus Torvaldsf2593392006-05-17 13:23:19 -0700202 for (specs = 0; pathspec[specs]; specs++)
203 /* nothing */;
Peter Eriksen28f75812006-07-25 09:30:18 +0200204 seen = xcalloc(specs, 1);
Linus Torvaldsf2593392006-05-17 13:23:19 -0700205
Linus Torvalds0d781532006-05-17 09:33:32 -0700206 src = dst = dir->entries;
207 i = dir->nr;
208 while (--i >= 0) {
209 struct dir_entry *entry = *src++;
Junio C Hamano4d06f8a2006-12-29 11:01:31 -0800210 if (match_pathspec(pathspec, entry->name, entry->len,
211 prefix, seen))
212 *dst++ = entry;
Jonathan Nieder9df84e92013-03-19 15:51:34 -0700213 else if (flag & WARN_IMPLICIT_DOT)
214 /*
215 * "git add -A" was run from a subdirectory with a
216 * new file outside that directory.
217 *
218 * "git add -A" will behave like "git add -A :/"
219 * instead of "git add -A ." in the future.
220 * Warn about the coming behavior change.
221 */
222 warn_pathless_add();
Linus Torvalds0d781532006-05-17 09:33:32 -0700223 }
224 dir->nr = dst - dir->entries;
Adam Spiers4b78d7b2013-01-06 16:58:09 +0000225 add_pathspec_matches_against_index(pathspec, seen, specs);
Junio C Hamano81f45e72010-02-09 17:30:49 -0500226 return seen;
Linus Torvalds0d781532006-05-17 09:33:32 -0700227}
228
Adam Spiers9d67b612013-01-06 16:58:10 +0000229/*
230 * Checks the index to see whether any path in pathspec refers to
231 * something inside a submodule. If so, dies with an error message.
232 */
Johannes Schindelin2ce53f92009-01-02 19:08:40 +0100233static void treat_gitlinks(const char **pathspec)
234{
235 int i;
236
237 if (!pathspec || !*pathspec)
238 return;
239
Adam Spiers9d67b612013-01-06 16:58:10 +0000240 for (i = 0; pathspec[i]; i++)
241 pathspec[i] = check_path_for_gitlink(pathspec[i]);
Johannes Schindelin2ce53f92009-01-02 19:08:40 +0100242}
243
Alexandre Julliardd6168132007-08-11 23:59:01 +0200244static void refresh(int verbose, const char **pathspec)
245{
246 char *seen;
247 int i, specs;
248
249 for (specs = 0; pathspec[specs]; specs++)
250 /* nothing */;
251 seen = xcalloc(specs, 1);
Matthieu Moy43673fd2009-08-21 10:57:58 +0200252 refresh_index(&the_index, verbose ? REFRESH_IN_PORCELAIN : REFRESH_QUIET,
Ævar Arnfjörð Bjarmasoned2a8082011-02-22 23:41:33 +0000253 pathspec, seen, _("Unstaged changes after refreshing the index:"));
Alexandre Julliardd6168132007-08-11 23:59:01 +0200254 for (i = 0; i < specs; i++) {
255 if (!seen[i])
Ævar Arnfjörð Bjarmason990ac4b2011-02-22 23:41:29 +0000256 die(_("pathspec '%s' did not match any files"), pathspec[i]);
Alexandre Julliardd6168132007-08-11 23:59:01 +0200257 }
Benoit Sigoure399f0a82007-10-29 08:00:33 +0100258 free(seen);
Alexandre Julliardd6168132007-08-11 23:59:01 +0200259}
260
Adam Spiers512aaf92013-01-06 16:58:11 +0000261/*
262 * Normalizes argv relative to prefix, via get_pathspec(), and then
263 * runs die_if_path_beyond_symlink() on each path in the normalized
264 * list.
265 */
Adam Spiersf8a11132013-01-06 16:58:07 +0000266static const char **validate_pathspec(const char **argv, const char *prefix)
Junio C Hamano3f061882007-11-25 10:10:10 -0800267{
268 const char **pathspec = get_pathspec(prefix, argv);
269
Junio C Hamano725b0602008-08-04 00:52:37 -0700270 if (pathspec) {
271 const char **p;
272 for (p = pathspec; *p; p++) {
Adam Spiers512aaf92013-01-06 16:58:11 +0000273 die_if_path_beyond_symlink(*p, prefix);
Junio C Hamano725b0602008-08-04 00:52:37 -0700274 }
275 }
276
Junio C Hamano3f061882007-11-25 10:10:10 -0800277 return pathspec;
278}
279
Thomas Rast46b51392009-08-13 14:29:41 +0200280int run_add_interactive(const char *revision, const char *patch_mode,
281 const char **pathspec)
282{
283 int status, ac, pc = 0;
284 const char **args;
285
286 if (pathspec)
287 while (pathspec[pc])
288 pc++;
289
290 args = xcalloc(sizeof(const char *), (pc + 5));
291 ac = 0;
292 args[ac++] = "add--interactive";
293 if (patch_mode)
294 args[ac++] = patch_mode;
295 if (revision)
296 args[ac++] = revision;
297 args[ac++] = "--";
298 if (pc) {
299 memcpy(&(args[ac]), pathspec, sizeof(const char *) * pc);
300 ac += pc;
301 }
302 args[ac] = NULL;
303
304 status = run_command_v_opt(args, RUN_GIT_CMD);
305 free(args);
306 return status;
307}
308
Conrad Irwinb4bd4662011-05-07 10:58:07 -0700309int interactive_add(int argc, const char **argv, const char *prefix, int patch)
Kristian Høgsberg58680162007-09-17 20:06:44 -0400310{
Junio C Hamano3f061882007-11-25 10:10:10 -0800311 const char **pathspec = NULL;
Junio C Hamano324ccbd2007-11-25 10:07:55 -0800312
Junio C Hamano3f061882007-11-25 10:10:10 -0800313 if (argc) {
Adam Spiersf8a11132013-01-06 16:58:07 +0000314 pathspec = validate_pathspec(argv, prefix);
Junio C Hamano3f061882007-11-25 10:10:10 -0800315 if (!pathspec)
316 return -1;
317 }
318
Thomas Rast46b51392009-08-13 14:29:41 +0200319 return run_add_interactive(NULL,
Conrad Irwinb4bd4662011-05-07 10:58:07 -0700320 patch ? "--patch" : NULL,
Thomas Rast46b51392009-08-13 14:29:41 +0200321 pathspec);
Kristian Høgsberg58680162007-09-17 20:06:44 -0400322}
323
Linus Torvalds2af202b2009-06-18 10:28:43 -0700324static int edit_patch(int argc, const char **argv, const char *prefix)
Johannes Schindelinc59cb032009-04-08 23:30:24 +0200325{
Ramsay Jonesd292bfa2012-09-04 18:30:21 +0100326 char *file = git_pathdup("ADD_EDIT.patch");
Johannes Schindelinc59cb032009-04-08 23:30:24 +0200327 const char *apply_argv[] = { "apply", "--recount", "--cached",
Gary V. Vaughan66dbfd52010-05-14 09:31:33 +0000328 NULL, NULL };
Johannes Schindelinc59cb032009-04-08 23:30:24 +0200329 struct child_process child;
330 struct rev_info rev;
331 int out;
332 struct stat st;
333
Gary V. Vaughan66dbfd52010-05-14 09:31:33 +0000334 apply_argv[3] = file;
335
Johannes Schindelinc59cb032009-04-08 23:30:24 +0200336 git_config(git_diff_basic_config, NULL); /* no "diff" UI options */
337
338 if (read_cache() < 0)
Ævar Arnfjörð Bjarmason990ac4b2011-02-22 23:41:29 +0000339 die (_("Could not read the index"));
Johannes Schindelinc59cb032009-04-08 23:30:24 +0200340
341 init_revisions(&rev, prefix);
342 rev.diffopt.context = 7;
343
344 argc = setup_revisions(argc, argv, &rev, NULL);
345 rev.diffopt.output_format = DIFF_FORMAT_PATCH;
Johannes Schindelin701825d2012-02-07 05:05:48 +0100346 DIFF_OPT_SET(&rev.diffopt, IGNORE_DIRTY_SUBMODULES);
Jeff King6ff2b722012-07-10 02:37:22 -0400347 out = open(file, O_CREAT | O_WRONLY, 0666);
Johannes Schindelinc59cb032009-04-08 23:30:24 +0200348 if (out < 0)
Ævar Arnfjörð Bjarmason990ac4b2011-02-22 23:41:29 +0000349 die (_("Could not open '%s' for writing."), file);
Jim Meyering41698372009-09-12 10:43:27 +0200350 rev.diffopt.file = xfdopen(out, "w");
Johannes Schindelinc59cb032009-04-08 23:30:24 +0200351 rev.diffopt.close_file = 1;
352 if (run_diff_files(&rev, 0))
Ævar Arnfjörð Bjarmason990ac4b2011-02-22 23:41:29 +0000353 die (_("Could not write patch"));
Johannes Schindelinc59cb032009-04-08 23:30:24 +0200354
355 launch_editor(file, NULL, NULL);
356
357 if (stat(file, &st))
Ævar Arnfjörð Bjarmason990ac4b2011-02-22 23:41:29 +0000358 die_errno(_("Could not stat '%s'"), file);
Johannes Schindelinc59cb032009-04-08 23:30:24 +0200359 if (!st.st_size)
Ævar Arnfjörð Bjarmason990ac4b2011-02-22 23:41:29 +0000360 die(_("Empty patch. Aborted."));
Johannes Schindelinc59cb032009-04-08 23:30:24 +0200361
362 memset(&child, 0, sizeof(child));
363 child.git_cmd = 1;
364 child.argv = apply_argv;
365 if (run_command(&child))
Ævar Arnfjörð Bjarmason990ac4b2011-02-22 23:41:29 +0000366 die (_("Could not apply '%s'"), file);
Johannes Schindelinc59cb032009-04-08 23:30:24 +0200367
368 unlink(file);
Ramsay Jonesd292bfa2012-09-04 18:30:21 +0100369 free(file);
Johannes Schindelinc59cb032009-04-08 23:30:24 +0200370 return 0;
371}
372
Junio C Hamano021b6e42006-06-06 12:51:49 -0700373static struct lock_file lock_file;
Linus Torvalds0d781532006-05-17 09:33:32 -0700374
Petr Baudisb39c53e2007-08-29 00:41:23 +0200375static const char ignore_error[] =
Ævar Arnfjörð Bjarmason439fb822011-02-22 23:41:30 +0000376N_("The following paths are ignored by one of your .gitignore files:\n");
Junio C Hamano6a1ad322006-12-25 17:46:38 -0800377
Junio C Hamano300c0a22013-03-08 22:21:13 -0800378static int verbose, show_only, ignored_too, refresh_only;
Junio C Hamano45c45e32011-04-19 12:18:20 -0700379static int ignore_add_errors, intent_to_add, ignore_missing;
380
381#define ADDREMOVE_DEFAULT 0 /* Change to 1 in Git 2.0 */
382static int addremove = ADDREMOVE_DEFAULT;
383static int addremove_explicit = -1; /* unspecified */
Kristian Høgsberg5c46f752007-10-03 17:45:02 -0400384
385static struct option builtin_add_options[] = {
Nguyễn Thái Ngọc Duy1b560242012-08-20 19:31:52 +0700386 OPT__DRY_RUN(&show_only, N_("dry run")),
387 OPT__VERBOSE(&verbose, N_("be verbose")),
Kristian Høgsberg5c46f752007-10-03 17:45:02 -0400388 OPT_GROUP(""),
Junio C Hamano300c0a22013-03-08 22:21:13 -0800389 OPT_BOOL('i', "interactive", &add_interactive, N_("interactive picking")),
390 OPT_BOOL('p', "patch", &patch_interactive, N_("select hunks interactively")),
391 OPT_BOOL('e', "edit", &edit_interactive, N_("edit current diff and apply")),
Nguyễn Thái Ngọc Duy1b560242012-08-20 19:31:52 +0700392 OPT__FORCE(&ignored_too, N_("allow adding otherwise ignored files")),
Junio C Hamano300c0a22013-03-08 22:21:13 -0800393 OPT_BOOL('u', "update", &take_worktree_changes, N_("update tracked files")),
394 OPT_BOOL('N', "intent-to-add", &intent_to_add, N_("record only the fact that the path will be added later")),
Junio C Hamano45c45e32011-04-19 12:18:20 -0700395 OPT_BOOL('A', "all", &addremove_explicit, N_("add changes from all tracked and untracked files")),
Junio C Hamano300c0a22013-03-08 22:21:13 -0800396 OPT_BOOL( 0 , "refresh", &refresh_only, N_("don't add, only refresh the index")),
397 OPT_BOOL( 0 , "ignore-errors", &ignore_add_errors, N_("just skip files which cannot be added because of errors")),
398 OPT_BOOL( 0 , "ignore-missing", &ignore_missing, N_("check if - even missing - files are ignored in dry run")),
Kristian Høgsberg5c46f752007-10-03 17:45:02 -0400399 OPT_END(),
400};
401
Junio C Hamano9bd81e42008-05-25 14:25:02 -0700402static int add_config(const char *var, const char *value, void *cb)
Alex Riesendad25e42008-05-12 19:59:23 +0200403{
Jonathan Nieder8c2be752011-05-14 15:19:21 -0500404 if (!strcmp(var, "add.ignoreerrors") ||
405 !strcmp(var, "add.ignore-errors")) {
Alex Riesendad25e42008-05-12 19:59:23 +0200406 ignore_add_errors = git_config_bool(var, value);
407 return 0;
408 }
Junio C Hamano9bd81e42008-05-25 14:25:02 -0700409 return git_default_config(var, value, cb);
Alex Riesendad25e42008-05-12 19:59:23 +0200410}
411
Junio C Hamanoc972ec02008-07-19 19:22:25 -0700412static int add_files(struct dir_struct *dir, int flags)
413{
414 int i, exit_status = 0;
415
416 if (dir->ignored_nr) {
Ævar Arnfjörð Bjarmason439fb822011-02-22 23:41:30 +0000417 fprintf(stderr, _(ignore_error));
Junio C Hamanoc972ec02008-07-19 19:22:25 -0700418 for (i = 0; i < dir->ignored_nr; i++)
419 fprintf(stderr, "%s\n", dir->ignored[i]->name);
Ævar Arnfjörð Bjarmason439fb822011-02-22 23:41:30 +0000420 fprintf(stderr, _("Use -f if you really want to add them.\n"));
421 die(_("no files added"));
Junio C Hamanoc972ec02008-07-19 19:22:25 -0700422 }
423
424 for (i = 0; i < dir->nr; i++)
425 if (add_file_to_cache(dir->entries[i]->name, flags)) {
426 if (!ignore_add_errors)
Ævar Arnfjörð Bjarmason990ac4b2011-02-22 23:41:29 +0000427 die(_("adding files failed"));
Junio C Hamanoc972ec02008-07-19 19:22:25 -0700428 exit_status = 1;
429 }
430 return exit_status;
431}
432
Linus Torvaldsa633fca2006-07-28 22:44:25 -0700433int cmd_add(int argc, const char **argv, const char *prefix)
Linus Torvalds0d781532006-05-17 09:33:32 -0700434{
Alex Riesen7ae02a32008-05-12 19:58:10 +0200435 int exit_status = 0;
Junio C Hamanoc972ec02008-07-19 19:22:25 -0700436 int newfd;
Linus Torvalds0d781532006-05-17 09:33:32 -0700437 const char **pathspec;
438 struct dir_struct dir;
Gustaf Hendeby205ffa92008-05-22 23:59:42 +0200439 int flags;
Junio C Hamanoc972ec02008-07-19 19:22:25 -0700440 int add_new_files;
441 int require_pathspec;
Junio C Hamano81f45e72010-02-09 17:30:49 -0500442 char *seen = NULL;
Jonathan Nieder71c7b052013-03-19 15:50:50 -0700443 int implicit_dot = 0;
Junio C Hamanod226b142013-04-17 12:32:21 -0700444 struct update_callback_data update_data;
Junio C Hamano5cde71d2006-12-10 20:55:50 -0800445
Stephen Boyded342fd2009-06-18 02:17:54 -0700446 git_config(add_config, NULL);
447
Stephen Boyd37782922009-05-23 11:53:12 -0700448 argc = parse_options(argc, argv, prefix, builtin_add_options,
Johannes Schindelinc59cb032009-04-08 23:30:24 +0200449 builtin_add_usage, PARSE_OPT_KEEP_ARGV0);
Wincent Colaiutab63e9952007-11-25 14:15:42 +0100450 if (patch_interactive)
451 add_interactive = 1;
Wincent Colaiuta7c0ab442007-11-22 01:02:52 +0100452 if (add_interactive)
Conrad Irwinb4bd4662011-05-07 10:58:07 -0700453 exit(interactive_add(argc - 1, argv + 1, prefix, patch_interactive));
Linus Torvalds0d781532006-05-17 09:33:32 -0700454
Johannes Schindelinc59cb032009-04-08 23:30:24 +0200455 if (edit_interactive)
456 return(edit_patch(argc, argv, prefix));
457 argc--;
458 argv++;
459
Junio C Hamano45c45e32011-04-19 12:18:20 -0700460 if (0 <= addremove_explicit)
461 addremove = addremove_explicit;
462 else if (take_worktree_changes && ADDREMOVE_DEFAULT)
463 addremove = 0; /* "-u" was given but not "-A" */
464
Junio C Hamano3ba1f112008-07-19 19:51:11 -0700465 if (addremove && take_worktree_changes)
Ævar Arnfjörð Bjarmason990ac4b2011-02-22 23:41:29 +0000466 die(_("-A and -u are mutually incompatible"));
Junio C Hamano45c45e32011-04-19 12:18:20 -0700467
468 /*
469 * Warn when "git add pathspec..." was given without "-u" or "-A"
Junio C Hamanod226b142013-04-17 12:32:21 -0700470 * and pathspec... covers a removed path.
Junio C Hamano45c45e32011-04-19 12:18:20 -0700471 */
Junio C Hamanod226b142013-04-17 12:32:21 -0700472 memset(&update_data, 0, sizeof(update_data));
473 if (!take_worktree_changes && addremove_explicit < 0)
474 update_data.warn_add_would_remove = 1;
Junio C Hamano45c45e32011-04-19 12:18:20 -0700475
476 if (!take_worktree_changes && addremove_explicit < 0 && argc)
477 /*
478 * Turn "git add pathspec..." to "git add -A pathspec..."
479 * in Git 2.0 but not yet
480 */
481 ; /* addremove = 1; */
482
Jens Lehmann108da0d2010-07-10 00:18:38 +0200483 if (!show_only && ignore_missing)
Ævar Arnfjörð Bjarmason990ac4b2011-02-22 23:41:29 +0000484 die(_("Option --ignore-missing can only be used together with --dry-run"));
Matthieu Moy0fa2eb52013-01-28 10:16:33 +0100485 if (addremove) {
486 option_with_implicit_dot = "--all";
487 short_option_with_implicit_dot = "-A";
488 }
489 if (take_worktree_changes) {
490 option_with_implicit_dot = "--update";
491 short_option_with_implicit_dot = "-u";
492 }
493 if (option_with_implicit_dot && !argc) {
Junio C Hamano3ba1f112008-07-19 19:51:11 -0700494 static const char *here[2] = { ".", NULL };
495 argc = 1;
496 argv = here;
Jonathan Nieder71c7b052013-03-19 15:50:50 -0700497 implicit_dot = 1;
Junio C Hamano3ba1f112008-07-19 19:51:11 -0700498 }
499
Junio C Hamanoc972ec02008-07-19 19:22:25 -0700500 add_new_files = !take_worktree_changes && !refresh_only;
501 require_pathspec = !take_worktree_changes;
502
Junio C Hamano30ca07a2007-03-31 23:09:02 -0700503 newfd = hold_locked_index(&lock_file, 1);
Linus Torvalds0d781532006-05-17 09:33:32 -0700504
Gustaf Hendeby205ffa92008-05-22 23:59:42 +0200505 flags = ((verbose ? ADD_CACHE_VERBOSE : 0) |
Junio C Hamano01665922008-05-25 14:03:50 -0700506 (show_only ? ADD_CACHE_PRETEND : 0) |
Junio C Hamano39425812008-08-21 01:44:53 -0700507 (intent_to_add ? ADD_CACHE_INTENT : 0) |
Junio C Hamano1e5f7642008-07-22 22:30:40 -0700508 (ignore_add_errors ? ADD_CACHE_IGNORE_ERRORS : 0) |
509 (!(addremove || take_worktree_changes)
Jonathan Nieder71c7b052013-03-19 15:50:50 -0700510 ? ADD_CACHE_IGNORE_REMOVAL : 0)) |
511 (implicit_dot ? ADD_CACHE_IMPLICIT_DOT : 0);
Gustaf Hendeby205ffa92008-05-22 23:59:42 +0200512
Junio C Hamanoc972ec02008-07-19 19:22:25 -0700513 if (require_pathspec && argc == 0) {
Ævar Arnfjörð Bjarmason990ac4b2011-02-22 23:41:29 +0000514 fprintf(stderr, _("Nothing specified, nothing added.\n"));
515 fprintf(stderr, _("Maybe you wanted to say 'git add .'?\n"));
Junio C Hamano93b0d862006-12-20 13:06:46 -0800516 return 0;
517 }
Adam Spiersf8a11132013-01-06 16:58:07 +0000518 pathspec = validate_pathspec(argv, prefix);
Linus Torvalds0d781532006-05-17 09:33:32 -0700519
Junio C Hamanoc972ec02008-07-19 19:22:25 -0700520 if (read_cache() < 0)
Ævar Arnfjörð Bjarmason990ac4b2011-02-22 23:41:29 +0000521 die(_("index file corrupt"));
Johannes Schindelin2ce53f92009-01-02 19:08:40 +0100522 treat_gitlinks(pathspec);
Junio C Hamanoc972ec02008-07-19 19:22:25 -0700523
Linus Torvalds1d8842d2009-05-14 13:22:36 -0700524 if (add_new_files) {
525 int baselen;
526
527 /* Set up the default git porcelain excludes */
528 memset(&dir, 0, sizeof(dir));
529 if (!ignored_too) {
530 dir.flags |= DIR_COLLECT_IGNORED;
531 setup_standard_excludes(&dir);
532 }
533
Junio C Hamano1e5f7642008-07-22 22:30:40 -0700534 /* This picks up the paths that are not tracked */
Jonathan Nieder9df84e92013-03-19 15:51:34 -0700535 baselen = fill_directory(&dir, implicit_dot ? NULL : pathspec);
Linus Torvalds1d8842d2009-05-14 13:22:36 -0700536 if (pathspec)
Jonathan Nieder9df84e92013-03-19 15:51:34 -0700537 seen = prune_directory(&dir, pathspec, baselen,
538 implicit_dot ? WARN_IMPLICIT_DOT : 0);
Linus Torvalds1d8842d2009-05-14 13:22:36 -0700539 }
Junio C Hamano1e5f7642008-07-22 22:30:40 -0700540
Alexandre Julliardd6168132007-08-11 23:59:01 +0200541 if (refresh_only) {
542 refresh(verbose, pathspec);
543 goto finish;
544 }
545
Junio C Hamano81f45e72010-02-09 17:30:49 -0500546 if (pathspec) {
547 int i;
Junio C Hamanoeb699342012-06-05 21:44:22 -0700548 struct path_exclude_check check;
549
550 path_exclude_check_init(&check, &dir);
Junio C Hamano81f45e72010-02-09 17:30:49 -0500551 if (!seen)
Adam Spiers4b78d7b2013-01-06 16:58:09 +0000552 seen = find_pathspecs_matching_against_index(pathspec);
Junio C Hamano81f45e72010-02-09 17:30:49 -0500553 for (i = 0; pathspec[i]; i++) {
554 if (!seen[i] && pathspec[i][0]
Jens Lehmann108da0d2010-07-10 00:18:38 +0200555 && !file_exists(pathspec[i])) {
556 if (ignore_missing) {
Nguyễn Thái Ngọc Duy0188f6b2010-11-11 20:03:22 +0700557 int dtype = DT_UNKNOWN;
Adam Spiers90130892012-12-27 02:32:23 +0000558 if (is_path_excluded(&check, pathspec[i], -1, &dtype))
Jens Lehmann108da0d2010-07-10 00:18:38 +0200559 dir_add_ignored(&dir, pathspec[i], strlen(pathspec[i]));
560 } else
Ævar Arnfjörð Bjarmason48168852011-02-22 23:41:31 +0000561 die(_("pathspec '%s' did not match any files"),
Jens Lehmann108da0d2010-07-10 00:18:38 +0200562 pathspec[i]);
563 }
Junio C Hamano81f45e72010-02-09 17:30:49 -0500564 }
565 free(seen);
Junio C Hamanoeb699342012-06-05 21:44:22 -0700566 path_exclude_check_clear(&check);
Junio C Hamano81f45e72010-02-09 17:30:49 -0500567 }
568
Junio C Hamano568508e2011-10-28 14:48:40 -0700569 plug_bulk_checkin();
570
Junio C Hamano561954b2013-04-22 11:11:45 -0700571 if ((flags & ADD_CACHE_IMPLICIT_DOT) && prefix) {
572 /*
573 * Check for modified files throughout the worktree so
574 * update_callback has a chance to warn about changes
575 * outside the cwd.
576 */
577 update_data.implicit_dot = prefix;
578 update_data.implicit_dot_len = strlen(prefix);
579 pathspec = NULL;
580 }
581 update_data.flags = flags & ~ADD_CACHE_IMPLICIT_DOT;
Junio C Hamanod226b142013-04-17 12:32:21 -0700582 update_files_in_cache(prefix, pathspec, &update_data);
Linus Torvalds0d781532006-05-17 09:33:32 -0700583
Junio C Hamanod226b142013-04-17 12:32:21 -0700584 exit_status |= !!update_data.add_errors;
Junio C Hamanoc972ec02008-07-19 19:22:25 -0700585 if (add_new_files)
586 exit_status |= add_files(&dir, flags);
Linus Torvalds0d781532006-05-17 09:33:32 -0700587
Junio C Hamano568508e2011-10-28 14:48:40 -0700588 unplug_bulk_checkin();
589
Junio C Hamanodfdac5d2007-04-20 01:39:39 -0700590 finish:
Linus Torvalds0d781532006-05-17 09:33:32 -0700591 if (active_cache_changed) {
592 if (write_cache(newfd, active_cache, active_nr) ||
Brandon Casey4ed7cd32008-01-16 13:12:46 -0600593 commit_locked_index(&lock_file))
Ævar Arnfjörð Bjarmason990ac4b2011-02-22 23:41:29 +0000594 die(_("Unable to write new index file"));
Linus Torvalds0d781532006-05-17 09:33:32 -0700595 }
596
Alex Riesen7ae02a32008-05-12 19:58:10 +0200597 return exit_status;
Linus Torvalds0d781532006-05-17 09:33:32 -0700598}