Merge branch 'jk/fetch-prune-doc' into maint
[git.git] / builtin / add.c
blobb1dddb4ac635ab7e2cc55be546002f8e68efa331
1 /*
2 * "git add" builtin command
4 * Copyright (C) 2006 Linus Torvalds
5 */
6 #include "cache.h"
7 #include "builtin.h"
8 #include "lockfile.h"
9 #include "dir.h"
10 #include "pathspec.h"
11 #include "exec_cmd.h"
12 #include "cache-tree.h"
13 #include "run-command.h"
14 #include "parse-options.h"
15 #include "diff.h"
16 #include "diffcore.h"
17 #include "revision.h"
18 #include "bulk-checkin.h"
19 #include "argv-array.h"
21 static const char * const builtin_add_usage[] = {
22 N_("git add [<options>] [--] <pathspec>..."),
23 NULL
25 static int patch_interactive, add_interactive, edit_interactive;
26 static int take_worktree_changes;
28 struct update_callback_data {
29 int flags, force_mode;
30 int add_errors;
33 static int fix_unmerged_status(struct diff_filepair *p,
34 struct update_callback_data *data)
36 if (p->status != DIFF_STATUS_UNMERGED)
37 return p->status;
38 if (!(data->flags & ADD_CACHE_IGNORE_REMOVAL) && !p->two->mode)
40 * This is not an explicit add request, and the
41 * path is missing from the working tree (deleted)
43 return DIFF_STATUS_DELETED;
44 else
46 * Either an explicit add request, or path exists
47 * in the working tree. An attempt to explicitly
48 * add a path that does not exist in the working tree
49 * will be caught as an error by the caller immediately.
51 return DIFF_STATUS_MODIFIED;
54 static void update_callback(struct diff_queue_struct *q,
55 struct diff_options *opt, void *cbdata)
57 int i;
58 struct update_callback_data *data = cbdata;
60 for (i = 0; i < q->nr; i++) {
61 struct diff_filepair *p = q->queue[i];
62 const char *path = p->one->path;
63 switch (fix_unmerged_status(p, data)) {
64 default:
65 die(_("unexpected diff status %c"), p->status);
66 case DIFF_STATUS_MODIFIED:
67 case DIFF_STATUS_TYPE_CHANGED:
68 if (add_file_to_index(&the_index, path,
69 data->flags, data->force_mode)) {
70 if (!(data->flags & ADD_CACHE_IGNORE_ERRORS))
71 die(_("updating files failed"));
72 data->add_errors++;
74 break;
75 case DIFF_STATUS_DELETED:
76 if (data->flags & ADD_CACHE_IGNORE_REMOVAL)
77 break;
78 if (!(data->flags & ADD_CACHE_PRETEND))
79 remove_file_from_index(&the_index, path);
80 if (data->flags & (ADD_CACHE_PRETEND|ADD_CACHE_VERBOSE))
81 printf(_("remove '%s'\n"), path);
82 break;
87 int add_files_to_cache(const char *prefix, const struct pathspec *pathspec,
88 int flags, int force_mode)
90 struct update_callback_data data;
91 struct rev_info rev;
93 memset(&data, 0, sizeof(data));
94 data.flags = flags;
95 data.force_mode = force_mode;
97 init_revisions(&rev, prefix);
98 setup_revisions(0, NULL, &rev, NULL);
99 if (pathspec)
100 copy_pathspec(&rev.prune_data, pathspec);
101 rev.diffopt.output_format = DIFF_FORMAT_CALLBACK;
102 rev.diffopt.format_callback = update_callback;
103 rev.diffopt.format_callback_data = &data;
104 rev.max_count = 0; /* do not compare unmerged paths with stage #2 */
105 run_diff_files(&rev, DIFF_RACY_IS_MODIFIED);
106 return !!data.add_errors;
109 static char *prune_directory(struct dir_struct *dir, struct pathspec *pathspec, int prefix)
111 char *seen;
112 int i;
113 struct dir_entry **src, **dst;
115 seen = xcalloc(pathspec->nr, 1);
117 src = dst = dir->entries;
118 i = dir->nr;
119 while (--i >= 0) {
120 struct dir_entry *entry = *src++;
121 if (dir_path_match(entry, pathspec, prefix, seen))
122 *dst++ = entry;
124 dir->nr = dst - dir->entries;
125 add_pathspec_matches_against_index(pathspec, seen);
126 return seen;
129 static void refresh(int verbose, const struct pathspec *pathspec)
131 char *seen;
132 int i;
134 seen = xcalloc(pathspec->nr, 1);
135 refresh_index(&the_index, verbose ? REFRESH_IN_PORCELAIN : REFRESH_QUIET,
136 pathspec, seen, _("Unstaged changes after refreshing the index:"));
137 for (i = 0; i < pathspec->nr; i++) {
138 if (!seen[i])
139 die(_("pathspec '%s' did not match any files"),
140 pathspec->items[i].match);
142 free(seen);
145 int run_add_interactive(const char *revision, const char *patch_mode,
146 const struct pathspec *pathspec)
148 int status, i;
149 struct argv_array argv = ARGV_ARRAY_INIT;
151 argv_array_push(&argv, "add--interactive");
152 if (patch_mode)
153 argv_array_push(&argv, patch_mode);
154 if (revision)
155 argv_array_push(&argv, revision);
156 argv_array_push(&argv, "--");
157 for (i = 0; i < pathspec->nr; i++)
158 /* pass original pathspec, to be re-parsed */
159 argv_array_push(&argv, pathspec->items[i].original);
161 status = run_command_v_opt(argv.argv, RUN_GIT_CMD);
162 argv_array_clear(&argv);
163 return status;
166 int interactive_add(int argc, const char **argv, const char *prefix, int patch)
168 struct pathspec pathspec;
170 parse_pathspec(&pathspec, 0,
171 PATHSPEC_PREFER_FULL |
172 PATHSPEC_SYMLINK_LEADING_PATH |
173 PATHSPEC_PREFIX_ORIGIN,
174 prefix, argv);
176 return run_add_interactive(NULL,
177 patch ? "--patch" : NULL,
178 &pathspec);
181 static int edit_patch(int argc, const char **argv, const char *prefix)
183 char *file = git_pathdup("ADD_EDIT.patch");
184 const char *apply_argv[] = { "apply", "--recount", "--cached",
185 NULL, NULL };
186 struct child_process child = CHILD_PROCESS_INIT;
187 struct rev_info rev;
188 int out;
189 struct stat st;
191 apply_argv[3] = file;
193 git_config(git_diff_basic_config, NULL); /* no "diff" UI options */
195 if (read_cache() < 0)
196 die(_("Could not read the index"));
198 init_revisions(&rev, prefix);
199 rev.diffopt.context = 7;
201 argc = setup_revisions(argc, argv, &rev, NULL);
202 rev.diffopt.output_format = DIFF_FORMAT_PATCH;
203 rev.diffopt.use_color = 0;
204 DIFF_OPT_SET(&rev.diffopt, IGNORE_DIRTY_SUBMODULES);
205 out = open(file, O_CREAT | O_WRONLY, 0666);
206 if (out < 0)
207 die(_("Could not open '%s' for writing."), file);
208 rev.diffopt.file = xfdopen(out, "w");
209 rev.diffopt.close_file = 1;
210 if (run_diff_files(&rev, 0))
211 die(_("Could not write patch"));
213 if (launch_editor(file, NULL, NULL))
214 die(_("editing patch failed"));
216 if (stat(file, &st))
217 die_errno(_("Could not stat '%s'"), file);
218 if (!st.st_size)
219 die(_("Empty patch. Aborted."));
221 child.git_cmd = 1;
222 child.argv = apply_argv;
223 if (run_command(&child))
224 die(_("Could not apply '%s'"), file);
226 unlink(file);
227 free(file);
228 return 0;
231 static struct lock_file lock_file;
233 static const char ignore_error[] =
234 N_("The following paths are ignored by one of your .gitignore files:\n");
236 static int verbose, show_only, ignored_too, refresh_only;
237 static int ignore_add_errors, intent_to_add, ignore_missing;
239 #define ADDREMOVE_DEFAULT 1
240 static int addremove = ADDREMOVE_DEFAULT;
241 static int addremove_explicit = -1; /* unspecified */
243 static char *chmod_arg;
245 static int ignore_removal_cb(const struct option *opt, const char *arg, int unset)
247 /* if we are told to ignore, we are not adding removals */
248 *(int *)opt->value = !unset ? 0 : 1;
249 return 0;
252 static struct option builtin_add_options[] = {
253 OPT__DRY_RUN(&show_only, N_("dry run")),
254 OPT__VERBOSE(&verbose, N_("be verbose")),
255 OPT_GROUP(""),
256 OPT_BOOL('i', "interactive", &add_interactive, N_("interactive picking")),
257 OPT_BOOL('p', "patch", &patch_interactive, N_("select hunks interactively")),
258 OPT_BOOL('e', "edit", &edit_interactive, N_("edit current diff and apply")),
259 OPT__FORCE(&ignored_too, N_("allow adding otherwise ignored files")),
260 OPT_BOOL('u', "update", &take_worktree_changes, N_("update tracked files")),
261 OPT_BOOL('N', "intent-to-add", &intent_to_add, N_("record only the fact that the path will be added later")),
262 OPT_BOOL('A', "all", &addremove_explicit, N_("add changes from all tracked and untracked files")),
263 { OPTION_CALLBACK, 0, "ignore-removal", &addremove_explicit,
264 NULL /* takes no arguments */,
265 N_("ignore paths removed in the working tree (same as --no-all)"),
266 PARSE_OPT_NOARG, ignore_removal_cb },
267 OPT_BOOL( 0 , "refresh", &refresh_only, N_("don't add, only refresh the index")),
268 OPT_BOOL( 0 , "ignore-errors", &ignore_add_errors, N_("just skip files which cannot be added because of errors")),
269 OPT_BOOL( 0 , "ignore-missing", &ignore_missing, N_("check if - even missing - files are ignored in dry run")),
270 OPT_STRING( 0 , "chmod", &chmod_arg, N_("(+/-)x"), N_("override the executable bit of the listed files")),
271 OPT_END(),
274 static int add_config(const char *var, const char *value, void *cb)
276 if (!strcmp(var, "add.ignoreerrors") ||
277 !strcmp(var, "add.ignore-errors")) {
278 ignore_add_errors = git_config_bool(var, value);
279 return 0;
281 return git_default_config(var, value, cb);
284 static int add_files(struct dir_struct *dir, int flags, int force_mode)
286 int i, exit_status = 0;
288 if (dir->ignored_nr) {
289 fprintf(stderr, _(ignore_error));
290 for (i = 0; i < dir->ignored_nr; i++)
291 fprintf(stderr, "%s\n", dir->ignored[i]->name);
292 fprintf(stderr, _("Use -f if you really want to add them.\n"));
293 exit_status = 1;
296 for (i = 0; i < dir->nr; i++)
297 if (add_file_to_index(&the_index, dir->entries[i]->name,
298 flags, force_mode)) {
299 if (!ignore_add_errors)
300 die(_("adding files failed"));
301 exit_status = 1;
303 return exit_status;
306 int cmd_add(int argc, const char **argv, const char *prefix)
308 int exit_status = 0;
309 struct pathspec pathspec;
310 struct dir_struct dir;
311 int flags, force_mode;
312 int add_new_files;
313 int require_pathspec;
314 char *seen = NULL;
316 git_config(add_config, NULL);
318 argc = parse_options(argc, argv, prefix, builtin_add_options,
319 builtin_add_usage, PARSE_OPT_KEEP_ARGV0);
320 if (patch_interactive)
321 add_interactive = 1;
322 if (add_interactive)
323 exit(interactive_add(argc - 1, argv + 1, prefix, patch_interactive));
325 if (edit_interactive)
326 return(edit_patch(argc, argv, prefix));
327 argc--;
328 argv++;
330 if (0 <= addremove_explicit)
331 addremove = addremove_explicit;
332 else if (take_worktree_changes && ADDREMOVE_DEFAULT)
333 addremove = 0; /* "-u" was given but not "-A" */
335 if (addremove && take_worktree_changes)
336 die(_("-A and -u are mutually incompatible"));
338 if (!take_worktree_changes && addremove_explicit < 0 && argc)
339 /* Turn "git add pathspec..." to "git add -A pathspec..." */
340 addremove = 1;
342 if (!show_only && ignore_missing)
343 die(_("Option --ignore-missing can only be used together with --dry-run"));
345 if (!chmod_arg)
346 force_mode = 0;
347 else if (!strcmp(chmod_arg, "-x"))
348 force_mode = 0666;
349 else if (!strcmp(chmod_arg, "+x"))
350 force_mode = 0777;
351 else
352 die(_("--chmod param '%s' must be either -x or +x"), chmod_arg);
354 add_new_files = !take_worktree_changes && !refresh_only;
355 require_pathspec = !(take_worktree_changes || (0 < addremove_explicit));
357 hold_locked_index(&lock_file, 1);
359 flags = ((verbose ? ADD_CACHE_VERBOSE : 0) |
360 (show_only ? ADD_CACHE_PRETEND : 0) |
361 (intent_to_add ? ADD_CACHE_INTENT : 0) |
362 (ignore_add_errors ? ADD_CACHE_IGNORE_ERRORS : 0) |
363 (!(addremove || take_worktree_changes)
364 ? ADD_CACHE_IGNORE_REMOVAL : 0));
366 if (require_pathspec && argc == 0) {
367 fprintf(stderr, _("Nothing specified, nothing added.\n"));
368 fprintf(stderr, _("Maybe you wanted to say 'git add .'?\n"));
369 return 0;
372 if (read_cache() < 0)
373 die(_("index file corrupt"));
376 * Check the "pathspec '%s' did not match any files" block
377 * below before enabling new magic.
379 parse_pathspec(&pathspec, 0,
380 PATHSPEC_PREFER_FULL |
381 PATHSPEC_SYMLINK_LEADING_PATH |
382 PATHSPEC_STRIP_SUBMODULE_SLASH_EXPENSIVE,
383 prefix, argv);
385 if (add_new_files) {
386 int baselen;
388 /* Set up the default git porcelain excludes */
389 memset(&dir, 0, sizeof(dir));
390 if (!ignored_too) {
391 dir.flags |= DIR_COLLECT_IGNORED;
392 setup_standard_excludes(&dir);
395 /* This picks up the paths that are not tracked */
396 baselen = fill_directory(&dir, &pathspec);
397 if (pathspec.nr)
398 seen = prune_directory(&dir, &pathspec, baselen);
401 if (refresh_only) {
402 refresh(verbose, &pathspec);
403 goto finish;
406 if (pathspec.nr) {
407 int i;
409 if (!seen)
410 seen = find_pathspecs_matching_against_index(&pathspec);
413 * file_exists() assumes exact match
415 GUARD_PATHSPEC(&pathspec,
416 PATHSPEC_FROMTOP |
417 PATHSPEC_LITERAL |
418 PATHSPEC_GLOB |
419 PATHSPEC_ICASE |
420 PATHSPEC_EXCLUDE);
422 for (i = 0; i < pathspec.nr; i++) {
423 const char *path = pathspec.items[i].match;
424 if (pathspec.items[i].magic & PATHSPEC_EXCLUDE)
425 continue;
426 if (!seen[i] && path[0] &&
427 ((pathspec.items[i].magic &
428 (PATHSPEC_GLOB | PATHSPEC_ICASE)) ||
429 !file_exists(path))) {
430 if (ignore_missing) {
431 int dtype = DT_UNKNOWN;
432 if (is_excluded(&dir, path, &dtype))
433 dir_add_ignored(&dir, path, pathspec.items[i].len);
434 } else
435 die(_("pathspec '%s' did not match any files"),
436 pathspec.items[i].original);
439 free(seen);
442 plug_bulk_checkin();
444 exit_status |= add_files_to_cache(prefix, &pathspec, flags, force_mode);
446 if (add_new_files)
447 exit_status |= add_files(&dir, flags, force_mode);
449 unplug_bulk_checkin();
451 finish:
452 if (active_cache_changed) {
453 if (write_locked_index(&the_index, &lock_file, COMMIT_LOCK))
454 die(_("Unable to write new index file"));
457 return exit_status;