2 * "git add" builtin command
4 * Copyright (C) 2006 Linus Torvalds
10 #include "cache-tree.h"
11 #include "run-command.h"
12 #include "parse-options.h"
16 static const char * const builtin_add_usage[] = {
17 "git add [options] [--] <filepattern>...",
20 static int patch_interactive, add_interactive, edit_interactive;
21 static int take_worktree_changes;
23 static void fill_pathspec_matches(const char **pathspec, char *seen, int specs)
25 int num_unmatched = 0, i;
28 * Since we are walking the index as if we were walking the directory,
29 * we have to mark the matched pathspec as seen; otherwise we will
30 * mistakenly think that the user gave a pathspec that did not match
33 for (i = 0; i < specs; i++)
38 for (i = 0; i < active_nr; i++) {
39 struct cache_entry *ce = active_cache[i];
40 match_pathspec(pathspec, ce->name, ce_namelen(ce), 0, seen);
44 static void prune_directory(struct dir_struct *dir, const char **pathspec, int prefix)
48 struct dir_entry **src, **dst;
50 for (specs = 0; pathspec[specs]; specs++)
52 seen = xcalloc(specs, 1);
54 src = dst = dir->entries;
57 struct dir_entry *entry = *src++;
58 if (match_pathspec(pathspec, entry->name, entry->len,
62 dir->nr = dst - dir->entries;
63 fill_pathspec_matches(pathspec, seen, specs);
65 for (i = 0; i < specs; i++) {
66 if (!seen[i] && pathspec[i][0] && !file_exists(pathspec[i]))
67 die("pathspec '%s' did not match any files",
73 static void treat_gitlinks(const char **pathspec)
77 if (!pathspec || !*pathspec)
80 for (i = 0; i < active_nr; i++) {
81 struct cache_entry *ce = active_cache[i];
82 if (S_ISGITLINK(ce->ce_mode)) {
83 int len = ce_namelen(ce), j;
84 for (j = 0; pathspec[j]; j++) {
85 int len2 = strlen(pathspec[j]);
86 if (len2 <= len || pathspec[j][len] != '/' ||
87 memcmp(ce->name, pathspec[j], len))
90 /* strip trailing slash */
91 pathspec[j] = xstrndup(ce->name, len);
93 die ("Path '%s' is in submodule '%.*s'",
94 pathspec[j], len, ce->name);
100 static void fill_directory(struct dir_struct *dir, const char **pathspec,
103 const char *path, *base;
106 /* Set up the default git porcelain excludes */
107 memset(dir, 0, sizeof(*dir));
109 dir->flags |= DIR_COLLECT_IGNORED;
110 setup_standard_excludes(dir);
114 * Calculate common prefix for the pathspec, and
115 * use that to optimize the directory walk
117 baselen = common_prefix(pathspec);
121 path = base = xmemdupz(*pathspec, baselen);
123 /* Read the directory and prune it */
124 read_directory(dir, path, base, baselen, pathspec);
126 prune_directory(dir, pathspec, baselen);
129 static void refresh(int verbose, const char **pathspec)
134 for (specs = 0; pathspec[specs]; specs++)
136 seen = xcalloc(specs, 1);
137 refresh_index(&the_index, verbose ? REFRESH_SAY_CHANGED : REFRESH_QUIET,
139 for (i = 0; i < specs; i++) {
141 die("pathspec '%s' did not match any files", pathspec[i]);
146 static const char **validate_pathspec(int argc, const char **argv, const char *prefix)
148 const char **pathspec = get_pathspec(prefix, argv);
152 for (p = pathspec; *p; p++) {
153 if (has_symlink_leading_path(*p, strlen(*p))) {
154 int len = prefix ? strlen(prefix) : 0;
155 die("'%s' is beyond a symbolic link", *p + len);
163 int interactive_add(int argc, const char **argv, const char *prefix)
167 const char **pathspec = NULL;
170 pathspec = validate_pathspec(argc, argv, prefix);
175 args = xcalloc(sizeof(const char *), (argc + 4));
177 args[ac++] = "add--interactive";
178 if (patch_interactive)
179 args[ac++] = "--patch";
182 memcpy(&(args[ac]), pathspec, sizeof(const char *) * argc);
187 status = run_command_v_opt(args, RUN_GIT_CMD);
192 int edit_patch(int argc, const char **argv, const char *prefix)
194 char *file = xstrdup(git_path("ADD_EDIT.patch"));
195 const char *apply_argv[] = { "apply", "--recount", "--cached",
197 struct child_process child;
202 git_config(git_diff_basic_config, NULL); /* no "diff" UI options */
204 if (read_cache() < 0)
205 die ("Could not read the index");
207 init_revisions(&rev, prefix);
208 rev.diffopt.context = 7;
210 argc = setup_revisions(argc, argv, &rev, NULL);
211 rev.diffopt.output_format = DIFF_FORMAT_PATCH;
212 out = open(file, O_CREAT | O_WRONLY, 0644);
214 die ("Could not open '%s' for writing.", file);
215 rev.diffopt.file = fdopen(out, "w");
216 rev.diffopt.close_file = 1;
217 if (run_diff_files(&rev, 0))
218 die ("Could not write patch");
220 launch_editor(file, NULL, NULL);
223 die("Could not stat '%s'", file);
225 die("Empty patch. Aborted.");
227 memset(&child, 0, sizeof(child));
229 child.argv = apply_argv;
230 if (run_command(&child))
231 die ("Could not apply '%s'", file);
237 static struct lock_file lock_file;
239 static const char ignore_error[] =
240 "The following paths are ignored by one of your .gitignore files:\n";
242 static int verbose = 0, show_only = 0, ignored_too = 0, refresh_only = 0;
243 static int ignore_add_errors, addremove, intent_to_add;
245 static struct option builtin_add_options[] = {
246 OPT__DRY_RUN(&show_only),
247 OPT__VERBOSE(&verbose),
249 OPT_BOOLEAN('i', "interactive", &add_interactive, "interactive picking"),
250 OPT_BOOLEAN('p', "patch", &patch_interactive, "interactive patching"),
251 OPT_BOOLEAN('e', "edit", &edit_interactive, "edit current diff and apply"),
252 OPT_BOOLEAN('f', "force", &ignored_too, "allow adding otherwise ignored files"),
253 OPT_BOOLEAN('u', "update", &take_worktree_changes, "update tracked files"),
254 OPT_BOOLEAN('N', "intent-to-add", &intent_to_add, "record only the fact that the path will be added later"),
255 OPT_BOOLEAN('A', "all", &addremove, "add all, noticing removal of tracked files"),
256 OPT_BOOLEAN( 0 , "refresh", &refresh_only, "don't add, only refresh the index"),
257 OPT_BOOLEAN( 0 , "ignore-errors", &ignore_add_errors, "just skip files which cannot be added because of errors"),
261 static int add_config(const char *var, const char *value, void *cb)
263 if (!strcasecmp(var, "add.ignore-errors")) {
264 ignore_add_errors = git_config_bool(var, value);
267 return git_default_config(var, value, cb);
270 static int add_files(struct dir_struct *dir, int flags)
272 int i, exit_status = 0;
274 if (dir->ignored_nr) {
275 fprintf(stderr, ignore_error);
276 for (i = 0; i < dir->ignored_nr; i++)
277 fprintf(stderr, "%s\n", dir->ignored[i]->name);
278 fprintf(stderr, "Use -f if you really want to add them.\n");
279 die("no files added");
282 for (i = 0; i < dir->nr; i++)
283 if (add_file_to_cache(dir->entries[i]->name, flags)) {
284 if (!ignore_add_errors)
285 die("adding files failed");
291 int cmd_add(int argc, const char **argv, const char *prefix)
295 const char **pathspec;
296 struct dir_struct dir;
299 int require_pathspec;
301 argc = parse_options(argc, argv, prefix, builtin_add_options,
302 builtin_add_usage, PARSE_OPT_KEEP_ARGV0);
303 if (patch_interactive)
306 exit(interactive_add(argc - 1, argv + 1, prefix));
308 git_config(add_config, NULL);
310 if (edit_interactive)
311 return(edit_patch(argc, argv, prefix));
315 if (addremove && take_worktree_changes)
316 die("-A and -u are mutually incompatible");
317 if ((addremove || take_worktree_changes) && !argc) {
318 static const char *here[2] = { ".", NULL };
323 add_new_files = !take_worktree_changes && !refresh_only;
324 require_pathspec = !take_worktree_changes;
326 newfd = hold_locked_index(&lock_file, 1);
328 flags = ((verbose ? ADD_CACHE_VERBOSE : 0) |
329 (show_only ? ADD_CACHE_PRETEND : 0) |
330 (intent_to_add ? ADD_CACHE_INTENT : 0) |
331 (ignore_add_errors ? ADD_CACHE_IGNORE_ERRORS : 0) |
332 (!(addremove || take_worktree_changes)
333 ? ADD_CACHE_IGNORE_REMOVAL : 0));
335 if (require_pathspec && argc == 0) {
336 fprintf(stderr, "Nothing specified, nothing added.\n");
337 fprintf(stderr, "Maybe you wanted to say 'git add .'?\n");
340 pathspec = validate_pathspec(argc, argv, prefix);
342 if (read_cache() < 0)
343 die("index file corrupt");
344 treat_gitlinks(pathspec);
347 /* This picks up the paths that are not tracked */
348 fill_directory(&dir, pathspec, ignored_too);
351 refresh(verbose, pathspec);
355 exit_status |= add_files_to_cache(prefix, pathspec, flags);
358 exit_status |= add_files(&dir, flags);
361 if (active_cache_changed) {
362 if (write_cache(newfd, active_cache, active_nr) ||
363 commit_locked_index(&lock_file))
364 die("Unable to write new index file");