2 * "git add" builtin command
4 * Copyright (C) 2006 Linus Torvalds
11 #include "cache-tree.h"
12 #include "run-command.h"
13 #include "parse-options.h"
17 #include "bulk-checkin.h"
19 static const char * const builtin_add_usage[] = {
20 N_("git add [options] [--] <pathspec>..."),
23 static int patch_interactive, add_interactive, edit_interactive;
24 static int take_worktree_changes;
26 struct update_callback_data {
30 /* only needed for 2.0 transition preparation */
31 int warn_add_would_remove;
34 static int fix_unmerged_status(struct diff_filepair *p,
35 struct update_callback_data *data)
37 if (p->status != DIFF_STATUS_UNMERGED)
39 if (!(data->flags & ADD_CACHE_IGNORE_REMOVAL) && !p->two->mode)
41 * This is not an explicit add request, and the
42 * path is missing from the working tree (deleted)
44 return DIFF_STATUS_DELETED;
47 * Either an explicit add request, or path exists
48 * in the working tree. An attempt to explicitly
49 * add a path that does not exist in the working tree
50 * will be caught as an error by the caller immediately.
52 return DIFF_STATUS_MODIFIED;
55 static void warn_add_would_remove(const char *path)
57 warning(_("In Git 2.0, 'git add <pathspec>...' will also update the\n"
58 "index for paths removed from the working tree that match\n"
59 "the given pathspec. If you want to 'add' only changed\n"
60 "or newly created paths, say 'git add --no-all <pathspec>...'"
62 "'%s' would be removed from the index without --no-all."),
66 static void update_callback(struct diff_queue_struct *q,
67 struct diff_options *opt, void *cbdata)
70 struct update_callback_data *data = cbdata;
72 for (i = 0; i < q->nr; i++) {
73 struct diff_filepair *p = q->queue[i];
74 const char *path = p->one->path;
75 switch (fix_unmerged_status(p, data)) {
77 die(_("unexpected diff status %c"), p->status);
78 case DIFF_STATUS_MODIFIED:
79 case DIFF_STATUS_TYPE_CHANGED:
80 if (add_file_to_index(&the_index, path, data->flags)) {
81 if (!(data->flags & ADD_CACHE_IGNORE_ERRORS))
82 die(_("updating files failed"));
86 case DIFF_STATUS_DELETED:
87 if (data->warn_add_would_remove) {
88 warn_add_would_remove(path);
89 data->warn_add_would_remove = 0;
91 if (data->flags & ADD_CACHE_IGNORE_REMOVAL)
93 if (!(data->flags & ADD_CACHE_PRETEND))
94 remove_file_from_index(&the_index, path);
95 if (data->flags & (ADD_CACHE_PRETEND|ADD_CACHE_VERBOSE))
96 printf(_("remove '%s'\n"), path);
102 static void update_files_in_cache(const char *prefix, const char **pathspec,
103 struct update_callback_data *data)
106 init_revisions(&rev, prefix);
107 setup_revisions(0, NULL, &rev, NULL);
108 init_pathspec(&rev.prune_data, pathspec);
109 rev.diffopt.output_format = DIFF_FORMAT_CALLBACK;
110 rev.diffopt.format_callback = update_callback;
111 rev.diffopt.format_callback_data = data;
112 rev.max_count = 0; /* do not compare unmerged paths with stage #2 */
113 run_diff_files(&rev, DIFF_RACY_IS_MODIFIED);
116 int add_files_to_cache(const char *prefix, const char **pathspec, int flags)
118 struct update_callback_data data;
120 memset(&data, 0, sizeof(data));
122 update_files_in_cache(prefix, pathspec, &data);
123 return !!data.add_errors;
126 static char *prune_directory(struct dir_struct *dir, const char **pathspec, int prefix)
130 struct dir_entry **src, **dst;
132 for (specs = 0; pathspec[specs]; specs++)
134 seen = xcalloc(specs, 1);
136 src = dst = dir->entries;
139 struct dir_entry *entry = *src++;
140 if (match_pathspec(pathspec, entry->name, entry->len,
144 dir->nr = dst - dir->entries;
145 add_pathspec_matches_against_index(pathspec, seen, specs);
150 * Checks the index to see whether any path in pathspec refers to
151 * something inside a submodule. If so, dies with an error message.
153 static void treat_gitlinks(const char **pathspec)
157 if (!pathspec || !*pathspec)
160 for (i = 0; pathspec[i]; i++)
161 pathspec[i] = check_path_for_gitlink(pathspec[i]);
164 static void refresh(int verbose, const char **pathspec)
169 for (specs = 0; pathspec[specs]; specs++)
171 seen = xcalloc(specs, 1);
172 refresh_index(&the_index, verbose ? REFRESH_IN_PORCELAIN : REFRESH_QUIET,
173 pathspec, seen, _("Unstaged changes after refreshing the index:"));
174 for (i = 0; i < specs; i++) {
176 die(_("pathspec '%s' did not match any files"), pathspec[i]);
182 * Normalizes argv relative to prefix, via get_pathspec(), and then
183 * runs die_if_path_beyond_symlink() on each path in the normalized
186 static const char **validate_pathspec(const char **argv, const char *prefix)
188 const char **pathspec = get_pathspec(prefix, argv);
192 for (p = pathspec; *p; p++) {
193 die_if_path_beyond_symlink(*p, prefix);
200 int run_add_interactive(const char *revision, const char *patch_mode,
201 const char **pathspec)
203 int status, ac, pc = 0;
210 args = xcalloc(sizeof(const char *), (pc + 5));
212 args[ac++] = "add--interactive";
214 args[ac++] = patch_mode;
216 args[ac++] = revision;
219 memcpy(&(args[ac]), pathspec, sizeof(const char *) * pc);
224 status = run_command_v_opt(args, RUN_GIT_CMD);
229 int interactive_add(int argc, const char **argv, const char *prefix, int patch)
231 const char **pathspec = NULL;
234 pathspec = validate_pathspec(argv, prefix);
239 return run_add_interactive(NULL,
240 patch ? "--patch" : NULL,
244 static int edit_patch(int argc, const char **argv, const char *prefix)
246 char *file = git_pathdup("ADD_EDIT.patch");
247 const char *apply_argv[] = { "apply", "--recount", "--cached",
249 struct child_process child;
254 apply_argv[3] = file;
256 git_config(git_diff_basic_config, NULL); /* no "diff" UI options */
258 if (read_cache() < 0)
259 die (_("Could not read the index"));
261 init_revisions(&rev, prefix);
262 rev.diffopt.context = 7;
264 argc = setup_revisions(argc, argv, &rev, NULL);
265 rev.diffopt.output_format = DIFF_FORMAT_PATCH;
266 DIFF_OPT_SET(&rev.diffopt, IGNORE_DIRTY_SUBMODULES);
267 out = open(file, O_CREAT | O_WRONLY, 0666);
269 die (_("Could not open '%s' for writing."), file);
270 rev.diffopt.file = xfdopen(out, "w");
271 rev.diffopt.close_file = 1;
272 if (run_diff_files(&rev, 0))
273 die (_("Could not write patch"));
275 launch_editor(file, NULL, NULL);
278 die_errno(_("Could not stat '%s'"), file);
280 die(_("Empty patch. Aborted."));
282 memset(&child, 0, sizeof(child));
284 child.argv = apply_argv;
285 if (run_command(&child))
286 die (_("Could not apply '%s'"), file);
293 static struct lock_file lock_file;
295 static const char ignore_error[] =
296 N_("The following paths are ignored by one of your .gitignore files:\n");
298 static int verbose, show_only, ignored_too, refresh_only;
299 static int ignore_add_errors, intent_to_add, ignore_missing;
301 #define ADDREMOVE_DEFAULT 0 /* Change to 1 in Git 2.0 */
302 static int addremove = ADDREMOVE_DEFAULT;
303 static int addremove_explicit = -1; /* unspecified */
305 static struct option builtin_add_options[] = {
306 OPT__DRY_RUN(&show_only, N_("dry run")),
307 OPT__VERBOSE(&verbose, N_("be verbose")),
309 OPT_BOOL('i', "interactive", &add_interactive, N_("interactive picking")),
310 OPT_BOOL('p', "patch", &patch_interactive, N_("select hunks interactively")),
311 OPT_BOOL('e', "edit", &edit_interactive, N_("edit current diff and apply")),
312 OPT__FORCE(&ignored_too, N_("allow adding otherwise ignored files")),
313 OPT_BOOL('u', "update", &take_worktree_changes, N_("update tracked files")),
314 OPT_BOOL('N', "intent-to-add", &intent_to_add, N_("record only the fact that the path will be added later")),
315 OPT_BOOL('A', "all", &addremove_explicit, N_("add changes from all tracked and untracked files")),
316 OPT_BOOL( 0 , "refresh", &refresh_only, N_("don't add, only refresh the index")),
317 OPT_BOOL( 0 , "ignore-errors", &ignore_add_errors, N_("just skip files which cannot be added because of errors")),
318 OPT_BOOL( 0 , "ignore-missing", &ignore_missing, N_("check if - even missing - files are ignored in dry run")),
322 static int add_config(const char *var, const char *value, void *cb)
324 if (!strcmp(var, "add.ignoreerrors") ||
325 !strcmp(var, "add.ignore-errors")) {
326 ignore_add_errors = git_config_bool(var, value);
329 return git_default_config(var, value, cb);
332 static int add_files(struct dir_struct *dir, int flags)
334 int i, exit_status = 0;
336 if (dir->ignored_nr) {
337 fprintf(stderr, _(ignore_error));
338 for (i = 0; i < dir->ignored_nr; i++)
339 fprintf(stderr, "%s\n", dir->ignored[i]->name);
340 fprintf(stderr, _("Use -f if you really want to add them.\n"));
341 die(_("no files added"));
344 for (i = 0; i < dir->nr; i++)
345 if (add_file_to_cache(dir->entries[i]->name, flags)) {
346 if (!ignore_add_errors)
347 die(_("adding files failed"));
353 static void warn_pathless_add(const char *option_name, const char *short_name) {
355 * To be consistent with "git add -p" and most Git
356 * commands, we should default to being tree-wide, but
357 * this is not the original behavior and can't be
358 * changed until users trained themselves not to type
359 * "git add -u" or "git add -A". For now, we warn and
360 * keep the old behavior. Later, this warning can be
361 * turned into a die(...), and eventually we may
362 * reallow the command with a new behavior.
364 warning(_("The behavior of 'git add %s (or %s)' with no path argument from a\n"
365 "subdirectory of the tree will change in Git 2.0 and should not be used anymore.\n"
366 "To add content for the whole tree, run:\n"
369 " (or git add %s :/)\n"
371 "To restrict the command to the current directory, run:\n"
374 " (or git add %s .)\n"
376 "With the current Git version, the command is restricted to the current directory."),
377 option_name, short_name,
378 option_name, short_name,
379 option_name, short_name);
382 int cmd_add(int argc, const char **argv, const char *prefix)
386 const char **pathspec;
387 struct dir_struct dir;
390 int require_pathspec;
392 const char *option_with_implicit_dot = NULL;
393 const char *short_option_with_implicit_dot = NULL;
394 struct update_callback_data update_data;
396 git_config(add_config, NULL);
398 argc = parse_options(argc, argv, prefix, builtin_add_options,
399 builtin_add_usage, PARSE_OPT_KEEP_ARGV0);
400 if (patch_interactive)
403 exit(interactive_add(argc - 1, argv + 1, prefix, patch_interactive));
405 if (edit_interactive)
406 return(edit_patch(argc, argv, prefix));
410 if (0 <= addremove_explicit)
411 addremove = addremove_explicit;
412 else if (take_worktree_changes && ADDREMOVE_DEFAULT)
413 addremove = 0; /* "-u" was given but not "-A" */
415 if (addremove && take_worktree_changes)
416 die(_("-A and -u are mutually incompatible"));
419 * Warn when "git add pathspec..." was given without "-u" or "-A"
420 * and pathspec... covers a removed path.
422 memset(&update_data, 0, sizeof(update_data));
423 if (!take_worktree_changes && addremove_explicit < 0)
424 update_data.warn_add_would_remove = 1;
426 if (!take_worktree_changes && addremove_explicit < 0 && argc)
428 * Turn "git add pathspec..." to "git add -A pathspec..."
429 * in Git 2.0 but not yet
431 ; /* addremove = 1; */
433 if (!show_only && ignore_missing)
434 die(_("Option --ignore-missing can only be used together with --dry-run"));
436 option_with_implicit_dot = "--all";
437 short_option_with_implicit_dot = "-A";
439 if (take_worktree_changes) {
440 option_with_implicit_dot = "--update";
441 short_option_with_implicit_dot = "-u";
443 if (option_with_implicit_dot && !argc) {
444 static const char *here[2] = { ".", NULL };
446 warn_pathless_add(option_with_implicit_dot,
447 short_option_with_implicit_dot);
452 add_new_files = !take_worktree_changes && !refresh_only;
453 require_pathspec = !take_worktree_changes;
455 newfd = hold_locked_index(&lock_file, 1);
457 flags = ((verbose ? ADD_CACHE_VERBOSE : 0) |
458 (show_only ? ADD_CACHE_PRETEND : 0) |
459 (intent_to_add ? ADD_CACHE_INTENT : 0) |
460 (ignore_add_errors ? ADD_CACHE_IGNORE_ERRORS : 0) |
461 (!(addremove || take_worktree_changes)
462 ? ADD_CACHE_IGNORE_REMOVAL : 0));
464 if (require_pathspec && argc == 0) {
465 fprintf(stderr, _("Nothing specified, nothing added.\n"));
466 fprintf(stderr, _("Maybe you wanted to say 'git add .'?\n"));
469 pathspec = validate_pathspec(argv, prefix);
471 if (read_cache() < 0)
472 die(_("index file corrupt"));
473 treat_gitlinks(pathspec);
478 /* Set up the default git porcelain excludes */
479 memset(&dir, 0, sizeof(dir));
481 dir.flags |= DIR_COLLECT_IGNORED;
482 setup_standard_excludes(&dir);
485 /* This picks up the paths that are not tracked */
486 baselen = fill_directory(&dir, pathspec);
488 seen = prune_directory(&dir, pathspec, baselen);
492 refresh(verbose, pathspec);
498 struct path_exclude_check check;
500 path_exclude_check_init(&check, &dir);
502 seen = find_pathspecs_matching_against_index(pathspec);
503 for (i = 0; pathspec[i]; i++) {
504 if (!seen[i] && pathspec[i][0]
505 && !file_exists(pathspec[i])) {
506 if (ignore_missing) {
507 int dtype = DT_UNKNOWN;
508 if (is_path_excluded(&check, pathspec[i], -1, &dtype))
509 dir_add_ignored(&dir, pathspec[i], strlen(pathspec[i]));
511 die(_("pathspec '%s' did not match any files"),
516 path_exclude_check_clear(&check);
521 update_data.flags = flags;
522 update_files_in_cache(prefix, pathspec, &update_data);
524 exit_status |= !!update_data.add_errors;
526 exit_status |= add_files(&dir, flags);
528 unplug_bulk_checkin();
531 if (active_cache_changed) {
532 if (write_cache(newfd, active_cache, active_nr) ||
533 commit_locked_index(&lock_file))
534 die(_("Unable to write new index file"));