]>
Commit | Line | Data |
---|---|---|
0d781539 LT |
1 | /* |
2 | * "git add" builtin command | |
3 | * | |
4 | * Copyright (C) 2006 Linus Torvalds | |
5 | */ | |
0d781539 LT |
6 | #include "cache.h" |
7 | #include "builtin.h" | |
8 | #include "dir.h" | |
5cde71d6 | 9 | #include "exec_cmd.h" |
93872e07 | 10 | #include "cache-tree.h" |
dfdac5d9 JH |
11 | #include "diff.h" |
12 | #include "diffcore.h" | |
13 | #include "commit.h" | |
14 | #include "revision.h" | |
58680165 | 15 | #include "run-command.h" |
5c46f754 | 16 | #include "parse-options.h" |
0d781539 | 17 | |
5c46f754 KH |
18 | static const char * const builtin_add_usage[] = { |
19 | "git-add [options] [--] <filepattern>...", | |
20 | NULL | |
21 | }; | |
b63e9950 | 22 | static int patch_interactive = 0, add_interactive = 0; |
93c44d49 | 23 | static int take_worktree_changes; |
896bdfa2 | 24 | |
0d781539 LT |
25 | static void prune_directory(struct dir_struct *dir, const char **pathspec, int prefix) |
26 | { | |
f2593398 LT |
27 | char *seen; |
28 | int i, specs; | |
0d781539 LT |
29 | struct dir_entry **src, **dst; |
30 | ||
f2593398 LT |
31 | for (specs = 0; pathspec[specs]; specs++) |
32 | /* nothing */; | |
28f75818 | 33 | seen = xcalloc(specs, 1); |
f2593398 | 34 | |
0d781539 LT |
35 | src = dst = dir->entries; |
36 | i = dir->nr; | |
37 | while (--i >= 0) { | |
38 | struct dir_entry *entry = *src++; | |
4d06f8ac JH |
39 | if (match_pathspec(pathspec, entry->name, entry->len, |
40 | prefix, seen)) | |
41 | *dst++ = entry; | |
0d781539 LT |
42 | } |
43 | dir->nr = dst - dir->entries; | |
f2593398 LT |
44 | |
45 | for (i = 0; i < specs; i++) { | |
e96980ef JK |
46 | if (!seen[i] && !file_exists(pathspec[i])) |
47 | die("pathspec '%s' did not match any files", | |
48 | pathspec[i]); | |
f2593398 | 49 | } |
399f0a8e | 50 | free(seen); |
0d781539 LT |
51 | } |
52 | ||
e96980ef JK |
53 | static void fill_directory(struct dir_struct *dir, const char **pathspec, |
54 | int ignored_too) | |
0d781539 LT |
55 | { |
56 | const char *path, *base; | |
57 | int baselen; | |
58 | ||
59 | /* Set up the default git porcelain excludes */ | |
60 | memset(dir, 0, sizeof(*dir)); | |
e96980ef JK |
61 | if (!ignored_too) { |
62 | dir->collect_ignored = 1; | |
039bc64e | 63 | setup_standard_excludes(dir); |
e96980ef | 64 | } |
0d781539 LT |
65 | |
66 | /* | |
67 | * Calculate common prefix for the pathspec, and | |
68 | * use that to optimize the directory walk | |
69 | */ | |
70 | baselen = common_prefix(pathspec); | |
71 | path = "."; | |
72 | base = ""; | |
182af834 PH |
73 | if (baselen) |
74 | path = base = xmemdupz(*pathspec, baselen); | |
0d781539 LT |
75 | |
76 | /* Read the directory and prune it */ | |
9fc42d60 | 77 | read_directory(dir, path, base, baselen, pathspec); |
0d781539 LT |
78 | if (pathspec) |
79 | prune_directory(dir, pathspec, baselen); | |
80 | } | |
81 | ||
7ae02a30 AR |
82 | struct update_callback_data |
83 | { | |
84 | int flags; | |
85 | int add_errors; | |
86 | }; | |
87 | ||
dfdac5d9 JH |
88 | static void update_callback(struct diff_queue_struct *q, |
89 | struct diff_options *opt, void *cbdata) | |
90 | { | |
7ae02a30 AR |
91 | int i; |
92 | struct update_callback_data *data = cbdata; | |
dfdac5d9 | 93 | |
dfdac5d9 JH |
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: | |
43b98acc | 99 | die("unexpected diff status %c", p->status); |
dfdac5d9 JH |
100 | case DIFF_STATUS_UNMERGED: |
101 | case DIFF_STATUS_MODIFIED: | |
767c98a5 | 102 | case DIFF_STATUS_TYPE_CHANGED: |
01665924 JH |
103 | if (add_file_to_cache(path, data->flags)) { |
104 | if (!(data->flags & ADD_CACHE_IGNORE_ERRORS)) | |
7ae02a30 AR |
105 | die("updating files failed"); |
106 | data->add_errors++; | |
107 | } | |
dfdac5d9 JH |
108 | break; |
109 | case DIFF_STATUS_DELETED: | |
01665924 | 110 | if (!(data->flags & ADD_CACHE_PRETEND)) |
38ed1d89 | 111 | remove_file_from_cache(path); |
01665924 | 112 | if (data->flags & (ADD_CACHE_PRETEND|ADD_CACHE_VERBOSE)) |
dfdac5d9 JH |
113 | printf("remove '%s'\n", path); |
114 | break; | |
115 | } | |
116 | } | |
117 | } | |
118 | ||
7ae02a30 | 119 | int add_files_to_cache(const char *prefix, const char **pathspec, int flags) |
dfdac5d9 | 120 | { |
7ae02a30 | 121 | struct update_callback_data data; |
dfdac5d9 | 122 | struct rev_info rev; |
2ed2c222 | 123 | init_revisions(&rev, prefix); |
dfdac5d9 | 124 | setup_revisions(0, NULL, &rev, NULL); |
b6ec1d61 | 125 | rev.prune_data = pathspec; |
dfdac5d9 JH |
126 | rev.diffopt.output_format = DIFF_FORMAT_CALLBACK; |
127 | rev.diffopt.format_callback = update_callback; | |
7ae02a30 AR |
128 | data.flags = flags; |
129 | data.add_errors = 0; | |
130 | rev.diffopt.format_callback_data = &data; | |
fb63d7f8 | 131 | run_diff_files(&rev, DIFF_RACY_IS_MODIFIED); |
7ae02a30 | 132 | return !!data.add_errors; |
dfdac5d9 JH |
133 | } |
134 | ||
d616813d AJ |
135 | static void refresh(int verbose, const char **pathspec) |
136 | { | |
137 | char *seen; | |
138 | int i, specs; | |
139 | ||
140 | for (specs = 0; pathspec[specs]; specs++) | |
141 | /* nothing */; | |
142 | seen = xcalloc(specs, 1); | |
143 | if (read_cache() < 0) | |
144 | die("index file corrupt"); | |
145 | refresh_index(&the_index, verbose ? 0 : REFRESH_QUIET, pathspec, seen); | |
146 | for (i = 0; i < specs; i++) { | |
147 | if (!seen[i]) | |
148 | die("pathspec '%s' did not match any files", pathspec[i]); | |
149 | } | |
399f0a8e | 150 | free(seen); |
d616813d AJ |
151 | } |
152 | ||
3f061887 JH |
153 | static const char **validate_pathspec(int argc, const char **argv, const char *prefix) |
154 | { | |
155 | const char **pathspec = get_pathspec(prefix, argv); | |
156 | ||
157 | return pathspec; | |
158 | } | |
159 | ||
160 | int interactive_add(int argc, const char **argv, const char *prefix) | |
58680165 | 161 | { |
b63e9950 | 162 | int status, ac; |
3f061887 JH |
163 | const char **args; |
164 | const char **pathspec = NULL; | |
165 | ||
166 | if (argc) { | |
167 | pathspec = validate_pathspec(argc, argv, prefix); | |
168 | if (!pathspec) | |
169 | return -1; | |
170 | } | |
324ccbd6 | 171 | |
b63e9950 WC |
172 | args = xcalloc(sizeof(const char *), (argc + 4)); |
173 | ac = 0; | |
174 | args[ac++] = "add--interactive"; | |
175 | if (patch_interactive) | |
176 | args[ac++] = "--patch"; | |
177 | args[ac++] = "--"; | |
178 | if (argc) { | |
179 | memcpy(&(args[ac]), pathspec, sizeof(const char *) * argc); | |
180 | ac += argc; | |
181 | } | |
182 | args[ac] = NULL; | |
7c0ab445 WC |
183 | |
184 | status = run_command_v_opt(args, RUN_GIT_CMD); | |
185 | free(args); | |
186 | return status; | |
58680165 KH |
187 | } |
188 | ||
021b6e45 | 189 | static struct lock_file lock_file; |
0d781539 | 190 | |
b39c53e6 | 191 | static const char ignore_error[] = |
6a1ad325 JH |
192 | "The following paths are ignored by one of your .gitignore files:\n"; |
193 | ||
5c46f754 | 194 | static int verbose = 0, show_only = 0, ignored_too = 0, refresh_only = 0; |
984b83ef | 195 | static int ignore_add_errors; |
5c46f754 KH |
196 | |
197 | static struct option builtin_add_options[] = { | |
198 | OPT__DRY_RUN(&show_only), | |
199 | OPT__VERBOSE(&verbose), | |
200 | OPT_GROUP(""), | |
201 | OPT_BOOLEAN('i', "interactive", &add_interactive, "interactive picking"), | |
b63e9950 | 202 | OPT_BOOLEAN('p', "patch", &patch_interactive, "interactive patching"), |
69c61c4f SG |
203 | OPT_BOOLEAN('f', "force", &ignored_too, "allow adding otherwise ignored files"), |
204 | OPT_BOOLEAN('u', "update", &take_worktree_changes, "update tracked files"), | |
5c46f754 | 205 | OPT_BOOLEAN( 0 , "refresh", &refresh_only, "don't add, only refresh the index"), |
984b83ef | 206 | OPT_BOOLEAN( 0 , "ignore-errors", &ignore_add_errors, "just skip files which cannot be added because of errors"), |
5c46f754 KH |
207 | OPT_END(), |
208 | }; | |
209 | ||
9bd81e42 | 210 | static int add_config(const char *var, const char *value, void *cb) |
dad25e4a AR |
211 | { |
212 | if (!strcasecmp(var, "add.ignore-errors")) { | |
213 | ignore_add_errors = git_config_bool(var, value); | |
214 | return 0; | |
215 | } | |
9bd81e42 | 216 | return git_default_config(var, value, cb); |
dad25e4a AR |
217 | } |
218 | ||
a633fca0 | 219 | int cmd_add(int argc, const char **argv, const char *prefix) |
0d781539 | 220 | { |
7ae02a30 | 221 | int exit_status = 0; |
7c0ab445 | 222 | int i, newfd; |
0d781539 LT |
223 | const char **pathspec; |
224 | struct dir_struct dir; | |
205ffa94 | 225 | int flags; |
5cde71d6 | 226 | |
5c46f754 KH |
227 | argc = parse_options(argc, argv, builtin_add_options, |
228 | builtin_add_usage, 0); | |
b63e9950 WC |
229 | if (patch_interactive) |
230 | add_interactive = 1; | |
7c0ab445 | 231 | if (add_interactive) |
3f061887 | 232 | exit(interactive_add(argc, argv, prefix)); |
0d781539 | 233 | |
9bd81e42 | 234 | git_config(add_config, NULL); |
0d781539 | 235 | |
30ca07a2 | 236 | newfd = hold_locked_index(&lock_file, 1); |
0d781539 | 237 | |
205ffa94 | 238 | flags = ((verbose ? ADD_CACHE_VERBOSE : 0) | |
01665924 JH |
239 | (show_only ? ADD_CACHE_PRETEND : 0) | |
240 | (ignore_add_errors ? ADD_CACHE_IGNORE_ERRORS : 0)); | |
205ffa94 | 241 | |
93c44d49 | 242 | if (take_worktree_changes) { |
b6ec1d61 | 243 | const char **pathspec; |
58680165 KH |
244 | if (read_cache() < 0) |
245 | die("index file corrupt"); | |
b6ec1d61 | 246 | pathspec = get_pathspec(prefix, argv); |
7ae02a30 | 247 | exit_status = add_files_to_cache(prefix, pathspec, flags); |
dfdac5d9 JH |
248 | goto finish; |
249 | } | |
250 | ||
5c46f754 | 251 | if (argc == 0) { |
93b0d86a JH |
252 | fprintf(stderr, "Nothing specified, nothing added.\n"); |
253 | fprintf(stderr, "Maybe you wanted to say 'git add .'?\n"); | |
254 | return 0; | |
255 | } | |
5c46f754 | 256 | pathspec = get_pathspec(prefix, argv); |
0d781539 | 257 | |
d616813d AJ |
258 | if (refresh_only) { |
259 | refresh(verbose, pathspec); | |
260 | goto finish; | |
261 | } | |
262 | ||
e96980ef | 263 | fill_directory(&dir, pathspec, ignored_too); |
0d781539 | 264 | |
366bfcb6 NP |
265 | if (read_cache() < 0) |
266 | die("index file corrupt"); | |
267 | ||
e96980ef | 268 | if (dir.ignored_nr) { |
b39c53e6 | 269 | fprintf(stderr, ignore_error); |
e96980ef JK |
270 | for (i = 0; i < dir.ignored_nr; i++) { |
271 | fprintf(stderr, "%s\n", dir.ignored[i]->name); | |
6a1ad325 | 272 | } |
e96980ef | 273 | fprintf(stderr, "Use -f if you really want to add them.\n"); |
b39c53e6 | 274 | die("no files added"); |
6a1ad325 JH |
275 | } |
276 | ||
0d781539 | 277 | for (i = 0; i < dir.nr; i++) |
01665924 | 278 | if (add_file_to_cache(dir.entries[i]->name, flags)) { |
984b83ef AR |
279 | if (!ignore_add_errors) |
280 | die("adding files failed"); | |
281 | exit_status = 1; | |
282 | } | |
0d781539 | 283 | |
dfdac5d9 | 284 | finish: |
0d781539 LT |
285 | if (active_cache_changed) { |
286 | if (write_cache(newfd, active_cache, active_nr) || | |
4ed7cd3a | 287 | commit_locked_index(&lock_file)) |
0d781539 LT |
288 | die("Unable to write new index file"); |
289 | } | |
290 | ||
7ae02a30 | 291 | return exit_status; |
0d781539 | 292 | } |