builtin-add.c: restructure the code for maintainability
[git/gitweb-caching.git] / builtin-add.c
blob0de516ad95b0c6fe9441a217cfd84b046f568bfe
1 /*
2 * "git add" builtin command
4 * Copyright (C) 2006 Linus Torvalds
5 */
6 #include "cache.h"
7 #include "builtin.h"
8 #include "dir.h"
9 #include "exec_cmd.h"
10 #include "cache-tree.h"
11 #include "run-command.h"
12 #include "parse-options.h"
14 static const char * const builtin_add_usage[] = {
15 "git add [options] [--] <filepattern>...",
16 NULL
18 static int patch_interactive = 0, add_interactive = 0;
19 static int take_worktree_changes;
21 static void prune_directory(struct dir_struct *dir, const char **pathspec, int prefix)
23 char *seen;
24 int i, specs;
25 struct dir_entry **src, **dst;
27 for (specs = 0; pathspec[specs]; specs++)
28 /* nothing */;
29 seen = xcalloc(specs, 1);
31 src = dst = dir->entries;
32 i = dir->nr;
33 while (--i >= 0) {
34 struct dir_entry *entry = *src++;
35 if (match_pathspec(pathspec, entry->name, entry->len,
36 prefix, seen))
37 *dst++ = entry;
39 dir->nr = dst - dir->entries;
41 for (i = 0; i < specs; i++) {
42 if (!seen[i] && !file_exists(pathspec[i]))
43 die("pathspec '%s' did not match any files",
44 pathspec[i]);
46 free(seen);
49 static void fill_directory(struct dir_struct *dir, const char **pathspec,
50 int ignored_too)
52 const char *path, *base;
53 int baselen;
55 /* Set up the default git porcelain excludes */
56 memset(dir, 0, sizeof(*dir));
57 if (!ignored_too) {
58 dir->collect_ignored = 1;
59 setup_standard_excludes(dir);
63 * Calculate common prefix for the pathspec, and
64 * use that to optimize the directory walk
66 baselen = common_prefix(pathspec);
67 path = ".";
68 base = "";
69 if (baselen)
70 path = base = xmemdupz(*pathspec, baselen);
72 /* Read the directory and prune it */
73 read_directory(dir, path, base, baselen, pathspec);
74 if (pathspec)
75 prune_directory(dir, pathspec, baselen);
78 static void refresh(int verbose, const char **pathspec)
80 char *seen;
81 int i, specs;
83 for (specs = 0; pathspec[specs]; specs++)
84 /* nothing */;
85 seen = xcalloc(specs, 1);
86 refresh_index(&the_index, verbose ? REFRESH_SAY_CHANGED : REFRESH_QUIET,
87 pathspec, seen);
88 for (i = 0; i < specs; i++) {
89 if (!seen[i])
90 die("pathspec '%s' did not match any files", pathspec[i]);
92 free(seen);
95 static const char **validate_pathspec(int argc, const char **argv, const char *prefix)
97 const char **pathspec = get_pathspec(prefix, argv);
99 return pathspec;
102 int interactive_add(int argc, const char **argv, const char *prefix)
104 int status, ac;
105 const char **args;
106 const char **pathspec = NULL;
108 if (argc) {
109 pathspec = validate_pathspec(argc, argv, prefix);
110 if (!pathspec)
111 return -1;
114 args = xcalloc(sizeof(const char *), (argc + 4));
115 ac = 0;
116 args[ac++] = "add--interactive";
117 if (patch_interactive)
118 args[ac++] = "--patch";
119 args[ac++] = "--";
120 if (argc) {
121 memcpy(&(args[ac]), pathspec, sizeof(const char *) * argc);
122 ac += argc;
124 args[ac] = NULL;
126 status = run_command_v_opt(args, RUN_GIT_CMD);
127 free(args);
128 return status;
131 static struct lock_file lock_file;
133 static const char ignore_error[] =
134 "The following paths are ignored by one of your .gitignore files:\n";
136 static int verbose = 0, show_only = 0, ignored_too = 0, refresh_only = 0;
137 static int ignore_add_errors, addremove;
139 static struct option builtin_add_options[] = {
140 OPT__DRY_RUN(&show_only),
141 OPT__VERBOSE(&verbose),
142 OPT_GROUP(""),
143 OPT_BOOLEAN('i', "interactive", &add_interactive, "interactive picking"),
144 OPT_BOOLEAN('p', "patch", &patch_interactive, "interactive patching"),
145 OPT_BOOLEAN('f', "force", &ignored_too, "allow adding otherwise ignored files"),
146 OPT_BOOLEAN('u', "update", &take_worktree_changes, "update tracked files"),
147 OPT_BOOLEAN('A', "all", &addremove, "add all, noticing removal of tracked files"),
148 OPT_BOOLEAN( 0 , "refresh", &refresh_only, "don't add, only refresh the index"),
149 OPT_BOOLEAN( 0 , "ignore-errors", &ignore_add_errors, "just skip files which cannot be added because of errors"),
150 OPT_END(),
153 static int add_config(const char *var, const char *value, void *cb)
155 if (!strcasecmp(var, "add.ignore-errors")) {
156 ignore_add_errors = git_config_bool(var, value);
157 return 0;
159 return git_default_config(var, value, cb);
162 static int add_files(struct dir_struct *dir, int flags)
164 int i, exit_status = 0;
166 if (dir->ignored_nr) {
167 fprintf(stderr, ignore_error);
168 for (i = 0; i < dir->ignored_nr; i++)
169 fprintf(stderr, "%s\n", dir->ignored[i]->name);
170 fprintf(stderr, "Use -f if you really want to add them.\n");
171 die("no files added");
174 for (i = 0; i < dir->nr; i++)
175 if (add_file_to_cache(dir->entries[i]->name, flags)) {
176 if (!ignore_add_errors)
177 die("adding files failed");
178 exit_status = 1;
180 return exit_status;
183 int cmd_add(int argc, const char **argv, const char *prefix)
185 int exit_status = 0;
186 int newfd;
187 const char **pathspec;
188 struct dir_struct dir;
189 int flags;
190 int add_new_files;
191 int require_pathspec;
193 argc = parse_options(argc, argv, builtin_add_options,
194 builtin_add_usage, 0);
195 if (patch_interactive)
196 add_interactive = 1;
197 if (add_interactive)
198 exit(interactive_add(argc, argv, prefix));
200 git_config(add_config, NULL);
202 if (addremove && take_worktree_changes)
203 die("-A and -u are mutually incompatible");
204 if (addremove && !argc) {
205 static const char *here[2] = { ".", NULL };
206 argc = 1;
207 argv = here;
210 add_new_files = !take_worktree_changes && !refresh_only;
211 require_pathspec = !take_worktree_changes;
213 newfd = hold_locked_index(&lock_file, 1);
215 flags = ((verbose ? ADD_CACHE_VERBOSE : 0) |
216 (show_only ? ADD_CACHE_PRETEND : 0) |
217 (ignore_add_errors ? ADD_CACHE_IGNORE_ERRORS : 0));
219 if (require_pathspec && argc == 0) {
220 fprintf(stderr, "Nothing specified, nothing added.\n");
221 fprintf(stderr, "Maybe you wanted to say 'git add .'?\n");
222 return 0;
224 pathspec = get_pathspec(prefix, argv);
227 * If we are adding new files, we need to scan the working
228 * tree to find the ones that match pathspecs; this needs
229 * to be done before we read the index.
231 if (add_new_files)
232 fill_directory(&dir, pathspec, ignored_too);
234 if (read_cache() < 0)
235 die("index file corrupt");
237 if (refresh_only) {
238 refresh(verbose, pathspec);
239 goto finish;
242 if (take_worktree_changes || addremove)
243 exit_status |= add_files_to_cache(prefix, pathspec, flags);
245 if (add_new_files)
246 exit_status |= add_files(&dir, flags);
248 finish:
249 if (active_cache_changed) {
250 if (write_cache(newfd, active_cache, active_nr) ||
251 commit_locked_index(&lock_file))
252 die("Unable to write new index file");
255 return exit_status;