bisect: trap critical errors in "bisect_start"
[alt-git.git] / builtin-add.c
blob73235ed08a9d60f8134235d9b68632303f359ca9
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 "diff.h"
12 #include "diffcore.h"
13 #include "commit.h"
14 #include "revision.h"
15 #include "run-command.h"
16 #include "parse-options.h"
18 static const char * const builtin_add_usage[] = {
19 "git-add [options] [--] <filepattern>...",
20 NULL
22 static int patch_interactive = 0, add_interactive = 0;
23 static int take_worktree_changes;
25 static void prune_directory(struct dir_struct *dir, const char **pathspec, int prefix)
27 char *seen;
28 int i, specs;
29 struct dir_entry **src, **dst;
31 for (specs = 0; pathspec[specs]; specs++)
32 /* nothing */;
33 seen = xcalloc(specs, 1);
35 src = dst = dir->entries;
36 i = dir->nr;
37 while (--i >= 0) {
38 struct dir_entry *entry = *src++;
39 if (match_pathspec(pathspec, entry->name, entry->len,
40 prefix, seen))
41 *dst++ = entry;
43 dir->nr = dst - dir->entries;
45 for (i = 0; i < specs; i++) {
46 if (!seen[i] && !file_exists(pathspec[i]))
47 die("pathspec '%s' did not match any files",
48 pathspec[i]);
50 free(seen);
53 static void fill_directory(struct dir_struct *dir, const char **pathspec,
54 int ignored_too)
56 const char *path, *base;
57 int baselen;
59 /* Set up the default git porcelain excludes */
60 memset(dir, 0, sizeof(*dir));
61 if (!ignored_too) {
62 dir->collect_ignored = 1;
63 setup_standard_excludes(dir);
67 * Calculate common prefix for the pathspec, and
68 * use that to optimize the directory walk
70 baselen = common_prefix(pathspec);
71 path = ".";
72 base = "";
73 if (baselen)
74 path = base = xmemdupz(*pathspec, baselen);
76 /* Read the directory and prune it */
77 read_directory(dir, path, base, baselen, pathspec);
78 if (pathspec)
79 prune_directory(dir, pathspec, baselen);
82 struct update_callback_data
84 int flags;
85 int add_errors;
88 static void update_callback(struct diff_queue_struct *q,
89 struct diff_options *opt, void *cbdata)
91 int i;
92 struct update_callback_data *data = cbdata;
94 for (i = 0; i < q->nr; i++) {
95 struct diff_filepair *p = q->queue[i];
96 const char *path = p->one->path;
97 switch (p->status) {
98 default:
99 die("unexpected diff status %c", p->status);
100 case DIFF_STATUS_UNMERGED:
101 case DIFF_STATUS_MODIFIED:
102 case DIFF_STATUS_TYPE_CHANGED:
103 if (add_file_to_cache(path, data->flags & ADD_FILES_VERBOSE)) {
104 if (!(data->flags & ADD_FILES_IGNORE_ERRORS))
105 die("updating files failed");
106 data->add_errors++;
108 break;
109 case DIFF_STATUS_DELETED:
110 remove_file_from_cache(path);
111 if (data->flags & ADD_FILES_VERBOSE)
112 printf("remove '%s'\n", path);
113 break;
118 int add_files_to_cache(const char *prefix, const char **pathspec, int flags)
120 struct update_callback_data data;
121 struct rev_info rev;
122 init_revisions(&rev, prefix);
123 setup_revisions(0, NULL, &rev, NULL);
124 rev.prune_data = pathspec;
125 rev.diffopt.output_format = DIFF_FORMAT_CALLBACK;
126 rev.diffopt.format_callback = update_callback;
127 data.flags = flags;
128 data.add_errors = 0;
129 rev.diffopt.format_callback_data = &data;
130 run_diff_files(&rev, DIFF_RACY_IS_MODIFIED);
131 return !!data.add_errors;
134 static void refresh(int verbose, const char **pathspec)
136 char *seen;
137 int i, specs;
139 for (specs = 0; pathspec[specs]; specs++)
140 /* nothing */;
141 seen = xcalloc(specs, 1);
142 if (read_cache() < 0)
143 die("index file corrupt");
144 refresh_index(&the_index, verbose ? 0 : REFRESH_QUIET, pathspec, seen);
145 for (i = 0; i < specs; i++) {
146 if (!seen[i])
147 die("pathspec '%s' did not match any files", pathspec[i]);
149 free(seen);
152 static const char **validate_pathspec(int argc, const char **argv, const char *prefix)
154 const char **pathspec = get_pathspec(prefix, argv);
156 return pathspec;
159 int interactive_add(int argc, const char **argv, const char *prefix)
161 int status, ac;
162 const char **args;
163 const char **pathspec = NULL;
165 if (argc) {
166 pathspec = validate_pathspec(argc, argv, prefix);
167 if (!pathspec)
168 return -1;
171 args = xcalloc(sizeof(const char *), (argc + 4));
172 ac = 0;
173 args[ac++] = "add--interactive";
174 if (patch_interactive)
175 args[ac++] = "--patch";
176 args[ac++] = "--";
177 if (argc) {
178 memcpy(&(args[ac]), pathspec, sizeof(const char *) * argc);
179 ac += argc;
181 args[ac] = NULL;
183 status = run_command_v_opt(args, RUN_GIT_CMD);
184 free(args);
185 return status;
188 static struct lock_file lock_file;
190 static const char ignore_error[] =
191 "The following paths are ignored by one of your .gitignore files:\n";
193 static int verbose = 0, show_only = 0, ignored_too = 0, refresh_only = 0;
194 static int ignore_add_errors;
196 static struct option builtin_add_options[] = {
197 OPT__DRY_RUN(&show_only),
198 OPT__VERBOSE(&verbose),
199 OPT_GROUP(""),
200 OPT_BOOLEAN('i', "interactive", &add_interactive, "interactive picking"),
201 OPT_BOOLEAN('p', "patch", &patch_interactive, "interactive patching"),
202 OPT_BOOLEAN('f', NULL, &ignored_too, "allow adding otherwise ignored files"),
203 OPT_BOOLEAN('u', NULL, &take_worktree_changes, "update tracked files"),
204 OPT_BOOLEAN( 0 , "refresh", &refresh_only, "don't add, only refresh the index"),
205 OPT_BOOLEAN( 0 , "ignore-errors", &ignore_add_errors, "just skip files which cannot be added because of errors"),
206 OPT_END(),
209 static int add_config(const char *var, const char *value)
211 if (!strcasecmp(var, "add.ignore-errors")) {
212 ignore_add_errors = git_config_bool(var, value);
213 return 0;
215 return git_default_config(var, value);
218 int cmd_add(int argc, const char **argv, const char *prefix)
220 int exit_status = 0;
221 int i, newfd;
222 const char **pathspec;
223 struct dir_struct dir;
225 argc = parse_options(argc, argv, builtin_add_options,
226 builtin_add_usage, 0);
227 if (patch_interactive)
228 add_interactive = 1;
229 if (add_interactive)
230 exit(interactive_add(argc, argv, prefix));
232 git_config(add_config);
234 newfd = hold_locked_index(&lock_file, 1);
236 if (take_worktree_changes) {
237 int flags = 0;
238 const char **pathspec;
239 if (read_cache() < 0)
240 die("index file corrupt");
241 pathspec = get_pathspec(prefix, argv);
243 if (verbose)
244 flags |= ADD_FILES_VERBOSE;
245 if (ignore_add_errors)
246 flags |= ADD_FILES_IGNORE_ERRORS;
248 exit_status = add_files_to_cache(prefix, pathspec, flags);
249 goto finish;
252 if (argc == 0) {
253 fprintf(stderr, "Nothing specified, nothing added.\n");
254 fprintf(stderr, "Maybe you wanted to say 'git add .'?\n");
255 return 0;
257 pathspec = get_pathspec(prefix, argv);
259 if (refresh_only) {
260 refresh(verbose, pathspec);
261 goto finish;
264 fill_directory(&dir, pathspec, ignored_too);
266 if (show_only) {
267 const char *sep = "", *eof = "";
268 for (i = 0; i < dir.nr; i++) {
269 printf("%s%s", sep, dir.entries[i]->name);
270 sep = " ";
271 eof = "\n";
273 fputs(eof, stdout);
274 return 0;
277 if (read_cache() < 0)
278 die("index file corrupt");
280 if (dir.ignored_nr) {
281 fprintf(stderr, ignore_error);
282 for (i = 0; i < dir.ignored_nr; i++) {
283 fprintf(stderr, "%s\n", dir.ignored[i]->name);
285 fprintf(stderr, "Use -f if you really want to add them.\n");
286 die("no files added");
289 for (i = 0; i < dir.nr; i++)
290 if (add_file_to_cache(dir.entries[i]->name, verbose)) {
291 if (!ignore_add_errors)
292 die("adding files failed");
293 exit_status = 1;
296 finish:
297 if (active_cache_changed) {
298 if (write_cache(newfd, active_cache, active_nr) ||
299 commit_locked_index(&lock_file))
300 die("Unable to write new index file");
303 return exit_status;