builtin-bundle: add a --basis option that specifies a basis
[git/pieter.git] / builtin-add.c
blob1834e2d7cd50ec7688ec6c5c7d2c79c8513ed132
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 fill_pathspec_matches(const char **pathspec, char *seen, int specs)
23 int num_unmatched = 0, i;
26 * Since we are walking the index as if we are warlking the directory,
27 * we have to mark the matched pathspec as seen; otherwise we will
28 * mistakenly think that the user gave a pathspec that did not match
29 * anything.
31 for (i = 0; i < specs; i++)
32 if (!seen[i])
33 num_unmatched++;
34 if (!num_unmatched)
35 return;
36 for (i = 0; i < active_nr; i++) {
37 struct cache_entry *ce = active_cache[i];
38 match_pathspec(pathspec, ce->name, ce_namelen(ce), 0, seen);
42 static void prune_directory(struct dir_struct *dir, const char **pathspec, int prefix)
44 char *seen;
45 int i, specs;
46 struct dir_entry **src, **dst;
48 for (specs = 0; pathspec[specs]; specs++)
49 /* nothing */;
50 seen = xcalloc(specs, 1);
52 src = dst = dir->entries;
53 i = dir->nr;
54 while (--i >= 0) {
55 struct dir_entry *entry = *src++;
56 if (match_pathspec(pathspec, entry->name, entry->len,
57 prefix, seen))
58 *dst++ = entry;
60 dir->nr = dst - dir->entries;
61 fill_pathspec_matches(pathspec, seen, specs);
63 for (i = 0; i < specs; i++) {
64 if (!seen[i] && !file_exists(pathspec[i]))
65 die("pathspec '%s' did not match any files",
66 pathspec[i]);
68 free(seen);
71 static void fill_directory(struct dir_struct *dir, const char **pathspec,
72 int ignored_too)
74 const char *path, *base;
75 int baselen;
77 /* Set up the default git porcelain excludes */
78 memset(dir, 0, sizeof(*dir));
79 if (!ignored_too) {
80 dir->collect_ignored = 1;
81 setup_standard_excludes(dir);
85 * Calculate common prefix for the pathspec, and
86 * use that to optimize the directory walk
88 baselen = common_prefix(pathspec);
89 path = ".";
90 base = "";
91 if (baselen)
92 path = base = xmemdupz(*pathspec, baselen);
94 /* Read the directory and prune it */
95 read_directory(dir, path, base, baselen, pathspec);
96 if (pathspec)
97 prune_directory(dir, pathspec, baselen);
100 static void refresh(int verbose, const char **pathspec)
102 char *seen;
103 int i, specs;
105 for (specs = 0; pathspec[specs]; specs++)
106 /* nothing */;
107 seen = xcalloc(specs, 1);
108 refresh_index(&the_index, verbose ? REFRESH_SAY_CHANGED : REFRESH_QUIET,
109 pathspec, seen);
110 for (i = 0; i < specs; i++) {
111 if (!seen[i])
112 die("pathspec '%s' did not match any files", pathspec[i]);
114 free(seen);
117 static const char **validate_pathspec(int argc, const char **argv, const char *prefix)
119 const char **pathspec = get_pathspec(prefix, argv);
121 return pathspec;
124 int interactive_add(int argc, const char **argv, const char *prefix)
126 int status, ac;
127 const char **args;
128 const char **pathspec = NULL;
130 if (argc) {
131 pathspec = validate_pathspec(argc, argv, prefix);
132 if (!pathspec)
133 return -1;
136 args = xcalloc(sizeof(const char *), (argc + 4));
137 ac = 0;
138 args[ac++] = "add--interactive";
139 if (patch_interactive)
140 args[ac++] = "--patch";
141 args[ac++] = "--";
142 if (argc) {
143 memcpy(&(args[ac]), pathspec, sizeof(const char *) * argc);
144 ac += argc;
146 args[ac] = NULL;
148 status = run_command_v_opt(args, RUN_GIT_CMD);
149 free(args);
150 return status;
153 static struct lock_file lock_file;
155 static const char ignore_error[] =
156 "The following paths are ignored by one of your .gitignore files:\n";
158 static int verbose = 0, show_only = 0, ignored_too = 0, refresh_only = 0;
159 static int ignore_add_errors, addremove;
161 static struct option builtin_add_options[] = {
162 OPT__DRY_RUN(&show_only),
163 OPT__VERBOSE(&verbose),
164 OPT_GROUP(""),
165 OPT_BOOLEAN('i', "interactive", &add_interactive, "interactive picking"),
166 OPT_BOOLEAN('p', "patch", &patch_interactive, "interactive patching"),
167 OPT_BOOLEAN('f', "force", &ignored_too, "allow adding otherwise ignored files"),
168 OPT_BOOLEAN('u', "update", &take_worktree_changes, "update tracked files"),
169 OPT_BOOLEAN('A', "all", &addremove, "add all, noticing removal of tracked files"),
170 OPT_BOOLEAN( 0 , "refresh", &refresh_only, "don't add, only refresh the index"),
171 OPT_BOOLEAN( 0 , "ignore-errors", &ignore_add_errors, "just skip files which cannot be added because of errors"),
172 OPT_END(),
175 static int add_config(const char *var, const char *value, void *cb)
177 if (!strcasecmp(var, "add.ignore-errors")) {
178 ignore_add_errors = git_config_bool(var, value);
179 return 0;
181 return git_default_config(var, value, cb);
184 static int add_files(struct dir_struct *dir, int flags)
186 int i, exit_status = 0;
188 if (dir->ignored_nr) {
189 fprintf(stderr, ignore_error);
190 for (i = 0; i < dir->ignored_nr; i++)
191 fprintf(stderr, "%s\n", dir->ignored[i]->name);
192 fprintf(stderr, "Use -f if you really want to add them.\n");
193 die("no files added");
196 for (i = 0; i < dir->nr; i++)
197 if (add_file_to_cache(dir->entries[i]->name, flags)) {
198 if (!ignore_add_errors)
199 die("adding files failed");
200 exit_status = 1;
202 return exit_status;
205 int cmd_add(int argc, const char **argv, const char *prefix)
207 int exit_status = 0;
208 int newfd;
209 const char **pathspec;
210 struct dir_struct dir;
211 int flags;
212 int add_new_files;
213 int require_pathspec;
215 argc = parse_options(argc, argv, builtin_add_options,
216 builtin_add_usage, 0);
217 if (patch_interactive)
218 add_interactive = 1;
219 if (add_interactive)
220 exit(interactive_add(argc, argv, prefix));
222 git_config(add_config, NULL);
224 if (addremove && take_worktree_changes)
225 die("-A and -u are mutually incompatible");
226 if ((addremove || take_worktree_changes) && !argc) {
227 static const char *here[2] = { ".", NULL };
228 argc = 1;
229 argv = here;
232 add_new_files = !take_worktree_changes && !refresh_only;
233 require_pathspec = !take_worktree_changes;
235 newfd = hold_locked_index(&lock_file, 1);
237 flags = ((verbose ? ADD_CACHE_VERBOSE : 0) |
238 (show_only ? ADD_CACHE_PRETEND : 0) |
239 (ignore_add_errors ? ADD_CACHE_IGNORE_ERRORS : 0) |
240 (!(addremove || take_worktree_changes)
241 ? ADD_CACHE_IGNORE_REMOVAL : 0));
243 if (require_pathspec && argc == 0) {
244 fprintf(stderr, "Nothing specified, nothing added.\n");
245 fprintf(stderr, "Maybe you wanted to say 'git add .'?\n");
246 return 0;
248 pathspec = get_pathspec(prefix, argv);
250 if (read_cache() < 0)
251 die("index file corrupt");
253 if (add_new_files)
254 /* This picks up the paths that are not tracked */
255 fill_directory(&dir, pathspec, ignored_too);
257 if (refresh_only) {
258 refresh(verbose, pathspec);
259 goto finish;
262 exit_status |= add_files_to_cache(prefix, pathspec, flags);
264 if (add_new_files)
265 exit_status |= add_files(&dir, flags);
267 finish:
268 if (active_cache_changed) {
269 if (write_cache(newfd, active_cache, active_nr) ||
270 commit_locked_index(&lock_file))
271 die("Unable to write new index file");
274 return exit_status;