]>
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" | |
6f525e71 | 9 | #include "pathspec.h" |
5cde71d6 | 10 | #include "exec_cmd.h" |
93872e07 | 11 | #include "cache-tree.h" |
58680165 | 12 | #include "run-command.h" |
5c46f754 | 13 | #include "parse-options.h" |
c59cb03a | 14 | #include "diff.h" |
fb7d3f32 | 15 | #include "diffcore.h" |
c59cb03a | 16 | #include "revision.h" |
568508e7 | 17 | #include "bulk-checkin.h" |
c45a18e8 | 18 | #include "argv-array.h" |
0d781539 | 19 | |
5c46f754 | 20 | static const char * const builtin_add_usage[] = { |
d32805dc | 21 | N_("git add [options] [--] <pathspec>..."), |
5c46f754 KH |
22 | NULL |
23 | }; | |
c59cb03a | 24 | static int patch_interactive, add_interactive, edit_interactive; |
93c44d49 | 25 | static int take_worktree_changes; |
896bdfa2 | 26 | |
9cba13ca | 27 | struct update_callback_data { |
fb7d3f32 LT |
28 | int flags; |
29 | int add_errors; | |
30 | }; | |
31 | ||
75973b2c JH |
32 | static int fix_unmerged_status(struct diff_filepair *p, |
33 | struct update_callback_data *data) | |
34 | { | |
35 | if (p->status != DIFF_STATUS_UNMERGED) | |
36 | return p->status; | |
37 | if (!(data->flags & ADD_CACHE_IGNORE_REMOVAL) && !p->two->mode) | |
38 | /* | |
39 | * This is not an explicit add request, and the | |
40 | * path is missing from the working tree (deleted) | |
41 | */ | |
42 | return DIFF_STATUS_DELETED; | |
43 | else | |
44 | /* | |
45 | * Either an explicit add request, or path exists | |
46 | * in the working tree. An attempt to explicitly | |
47 | * add a path that does not exist in the working tree | |
48 | * will be caught as an error by the caller immediately. | |
49 | */ | |
50 | return DIFF_STATUS_MODIFIED; | |
51 | } | |
52 | ||
fb7d3f32 LT |
53 | static void update_callback(struct diff_queue_struct *q, |
54 | struct diff_options *opt, void *cbdata) | |
55 | { | |
56 | int i; | |
57 | struct update_callback_data *data = cbdata; | |
58 | ||
59 | for (i = 0; i < q->nr; i++) { | |
60 | struct diff_filepair *p = q->queue[i]; | |
61 | const char *path = p->one->path; | |
75973b2c | 62 | switch (fix_unmerged_status(p, data)) { |
fb7d3f32 | 63 | default: |
990ac4be | 64 | die(_("unexpected diff status %c"), p->status); |
fb7d3f32 LT |
65 | case DIFF_STATUS_MODIFIED: |
66 | case DIFF_STATUS_TYPE_CHANGED: | |
67 | if (add_file_to_index(&the_index, path, data->flags)) { | |
68 | if (!(data->flags & ADD_CACHE_IGNORE_ERRORS)) | |
990ac4be | 69 | die(_("updating files failed")); |
fb7d3f32 LT |
70 | data->add_errors++; |
71 | } | |
72 | break; | |
73 | case DIFF_STATUS_DELETED: | |
74 | if (data->flags & ADD_CACHE_IGNORE_REMOVAL) | |
75 | break; | |
76 | if (!(data->flags & ADD_CACHE_PRETEND)) | |
77 | remove_file_from_index(&the_index, path); | |
78 | if (data->flags & (ADD_CACHE_PRETEND|ADD_CACHE_VERBOSE)) | |
475c73eb | 79 | printf(_("remove '%s'\n"), path); |
fb7d3f32 LT |
80 | break; |
81 | } | |
82 | } | |
83 | } | |
84 | ||
160c4b18 JH |
85 | int add_files_to_cache(const char *prefix, |
86 | const struct pathspec *pathspec, int flags) | |
fb7d3f32 | 87 | { |
160c4b18 | 88 | struct update_callback_data data; |
fb7d3f32 | 89 | struct rev_info rev; |
71c7b053 | 90 | |
160c4b18 JH |
91 | memset(&data, 0, sizeof(data)); |
92 | data.flags = flags; | |
93 | ||
fb7d3f32 LT |
94 | init_revisions(&rev, prefix); |
95 | setup_revisions(0, NULL, &rev, NULL); | |
3efe8e43 NTND |
96 | if (pathspec) |
97 | copy_pathspec(&rev.prune_data, pathspec); | |
fb7d3f32 LT |
98 | rev.diffopt.output_format = DIFF_FORMAT_CALLBACK; |
99 | rev.diffopt.format_callback = update_callback; | |
160c4b18 | 100 | rev.diffopt.format_callback_data = &data; |
75973b2c | 101 | rev.max_count = 0; /* do not compare unmerged paths with stage #2 */ |
fb7d3f32 LT |
102 | run_diff_files(&rev, DIFF_RACY_IS_MODIFIED); |
103 | return !!data.add_errors; | |
104 | } | |
105 | ||
053a6b18 | 106 | static char *prune_directory(struct dir_struct *dir, struct pathspec *pathspec, int prefix) |
0d781539 | 107 | { |
f2593398 | 108 | char *seen; |
84b8b5d1 | 109 | int i; |
0d781539 LT |
110 | struct dir_entry **src, **dst; |
111 | ||
84b8b5d1 | 112 | seen = xcalloc(pathspec->nr, 1); |
f2593398 | 113 | |
0d781539 LT |
114 | src = dst = dir->entries; |
115 | i = dir->nr; | |
116 | while (--i >= 0) { | |
117 | struct dir_entry *entry = *src++; | |
ebb32893 | 118 | if (dir_path_match(entry, pathspec, prefix, seen)) |
4d06f8ac | 119 | *dst++ = entry; |
0d781539 LT |
120 | } |
121 | dir->nr = dst - dir->entries; | |
84b8b5d1 | 122 | add_pathspec_matches_against_index(pathspec, seen); |
81f45e7d | 123 | return seen; |
0d781539 LT |
124 | } |
125 | ||
9b2d6149 | 126 | static void refresh(int verbose, const struct pathspec *pathspec) |
d616813d AJ |
127 | { |
128 | char *seen; | |
9b2d6149 | 129 | int i; |
d616813d | 130 | |
9b2d6149 | 131 | seen = xcalloc(pathspec->nr, 1); |
43673fdd | 132 | refresh_index(&the_index, verbose ? REFRESH_IN_PORCELAIN : REFRESH_QUIET, |
ed2a808d | 133 | pathspec, seen, _("Unstaged changes after refreshing the index:")); |
9b2d6149 | 134 | for (i = 0; i < pathspec->nr; i++) { |
d616813d | 135 | if (!seen[i]) |
9b2d6149 NTND |
136 | die(_("pathspec '%s' did not match any files"), |
137 | pathspec->items[i].match); | |
d616813d | 138 | } |
399f0a8e | 139 | free(seen); |
d616813d AJ |
140 | } |
141 | ||
46b5139c | 142 | int run_add_interactive(const char *revision, const char *patch_mode, |
480ca644 | 143 | const struct pathspec *pathspec) |
58680165 | 144 | { |
c45a18e8 FR |
145 | int status, i; |
146 | struct argv_array argv = ARGV_ARRAY_INIT; | |
3f061887 | 147 | |
c45a18e8 | 148 | argv_array_push(&argv, "add--interactive"); |
46b5139c | 149 | if (patch_mode) |
c45a18e8 | 150 | argv_array_push(&argv, patch_mode); |
46b5139c | 151 | if (revision) |
c45a18e8 FR |
152 | argv_array_push(&argv, revision); |
153 | argv_array_push(&argv, "--"); | |
480ca644 NTND |
154 | for (i = 0; i < pathspec->nr; i++) |
155 | /* pass original pathspec, to be re-parsed */ | |
c45a18e8 | 156 | argv_array_push(&argv, pathspec->items[i].original); |
7c0ab445 | 157 | |
c45a18e8 FR |
158 | status = run_command_v_opt(argv.argv, RUN_GIT_CMD); |
159 | argv_array_clear(&argv); | |
7c0ab445 | 160 | return status; |
58680165 KH |
161 | } |
162 | ||
b4bd4668 | 163 | int interactive_add(int argc, const char **argv, const char *prefix, int patch) |
46b5139c | 164 | { |
5a76aff1 | 165 | struct pathspec pathspec; |
46b5139c | 166 | |
625c3304 | 167 | parse_pathspec(&pathspec, 0, |
5a76aff1 | 168 | PATHSPEC_PREFER_FULL | |
480ca644 NTND |
169 | PATHSPEC_SYMLINK_LEADING_PATH | |
170 | PATHSPEC_PREFIX_ORIGIN, | |
5a76aff1 | 171 | prefix, argv); |
46b5139c TR |
172 | |
173 | return run_add_interactive(NULL, | |
b4bd4668 | 174 | patch ? "--patch" : NULL, |
480ca644 | 175 | &pathspec); |
46b5139c TR |
176 | } |
177 | ||
2af202be | 178 | static int edit_patch(int argc, const char **argv, const char *prefix) |
c59cb03a | 179 | { |
d292bfaf | 180 | char *file = git_pathdup("ADD_EDIT.patch"); |
c59cb03a | 181 | const char *apply_argv[] = { "apply", "--recount", "--cached", |
66dbfd55 | 182 | NULL, NULL }; |
c59cb03a JS |
183 | struct child_process child; |
184 | struct rev_info rev; | |
185 | int out; | |
186 | struct stat st; | |
187 | ||
66dbfd55 GV |
188 | apply_argv[3] = file; |
189 | ||
c59cb03a JS |
190 | git_config(git_diff_basic_config, NULL); /* no "diff" UI options */ |
191 | ||
192 | if (read_cache() < 0) | |
d521abf8 | 193 | die(_("Could not read the index")); |
c59cb03a JS |
194 | |
195 | init_revisions(&rev, prefix); | |
196 | rev.diffopt.context = 7; | |
197 | ||
198 | argc = setup_revisions(argc, argv, &rev, NULL); | |
199 | rev.diffopt.output_format = DIFF_FORMAT_PATCH; | |
7f3b8c62 | 200 | rev.diffopt.use_color = 0; |
701825de | 201 | DIFF_OPT_SET(&rev.diffopt, IGNORE_DIRTY_SUBMODULES); |
6ff2b729 | 202 | out = open(file, O_CREAT | O_WRONLY, 0666); |
c59cb03a | 203 | if (out < 0) |
d521abf8 | 204 | die(_("Could not open '%s' for writing."), file); |
41698375 | 205 | rev.diffopt.file = xfdopen(out, "w"); |
c59cb03a JS |
206 | rev.diffopt.close_file = 1; |
207 | if (run_diff_files(&rev, 0)) | |
d521abf8 | 208 | die(_("Could not write patch")); |
c59cb03a JS |
209 | |
210 | launch_editor(file, NULL, NULL); | |
211 | ||
212 | if (stat(file, &st)) | |
990ac4be | 213 | die_errno(_("Could not stat '%s'"), file); |
c59cb03a | 214 | if (!st.st_size) |
990ac4be | 215 | die(_("Empty patch. Aborted.")); |
c59cb03a JS |
216 | |
217 | memset(&child, 0, sizeof(child)); | |
218 | child.git_cmd = 1; | |
219 | child.argv = apply_argv; | |
220 | if (run_command(&child)) | |
d521abf8 | 221 | die(_("Could not apply '%s'"), file); |
c59cb03a JS |
222 | |
223 | unlink(file); | |
d292bfaf | 224 | free(file); |
c59cb03a JS |
225 | return 0; |
226 | } | |
227 | ||
021b6e45 | 228 | static struct lock_file lock_file; |
0d781539 | 229 | |
b39c53e6 | 230 | static const char ignore_error[] = |
439fb829 | 231 | N_("The following paths are ignored by one of your .gitignore files:\n"); |
6a1ad325 | 232 | |
300c0a22 | 233 | static int verbose, show_only, ignored_too, refresh_only; |
45c45e30 JH |
234 | static int ignore_add_errors, intent_to_add, ignore_missing; |
235 | ||
fdc97abd | 236 | #define ADDREMOVE_DEFAULT 1 |
45c45e30 JH |
237 | static int addremove = ADDREMOVE_DEFAULT; |
238 | static int addremove_explicit = -1; /* unspecified */ | |
5c46f754 | 239 | |
9f60f49b JH |
240 | static int ignore_removal_cb(const struct option *opt, const char *arg, int unset) |
241 | { | |
242 | /* if we are told to ignore, we are not adding removals */ | |
243 | *(int *)opt->value = !unset ? 0 : 1; | |
244 | return 0; | |
245 | } | |
246 | ||
5c46f754 | 247 | static struct option builtin_add_options[] = { |
1b56024c NTND |
248 | OPT__DRY_RUN(&show_only, N_("dry run")), |
249 | OPT__VERBOSE(&verbose, N_("be verbose")), | |
5c46f754 | 250 | OPT_GROUP(""), |
300c0a22 JH |
251 | OPT_BOOL('i', "interactive", &add_interactive, N_("interactive picking")), |
252 | OPT_BOOL('p', "patch", &patch_interactive, N_("select hunks interactively")), | |
253 | OPT_BOOL('e', "edit", &edit_interactive, N_("edit current diff and apply")), | |
1b56024c | 254 | OPT__FORCE(&ignored_too, N_("allow adding otherwise ignored files")), |
300c0a22 JH |
255 | OPT_BOOL('u', "update", &take_worktree_changes, N_("update tracked files")), |
256 | OPT_BOOL('N', "intent-to-add", &intent_to_add, N_("record only the fact that the path will be added later")), | |
45c45e30 | 257 | OPT_BOOL('A', "all", &addremove_explicit, N_("add changes from all tracked and untracked files")), |
9f60f49b JH |
258 | { OPTION_CALLBACK, 0, "ignore-removal", &addremove_explicit, |
259 | NULL /* takes no arguments */, | |
260 | N_("ignore paths removed in the working tree (same as --no-all)"), | |
261 | PARSE_OPT_NOARG, ignore_removal_cb }, | |
300c0a22 JH |
262 | OPT_BOOL( 0 , "refresh", &refresh_only, N_("don't add, only refresh the index")), |
263 | OPT_BOOL( 0 , "ignore-errors", &ignore_add_errors, N_("just skip files which cannot be added because of errors")), | |
264 | OPT_BOOL( 0 , "ignore-missing", &ignore_missing, N_("check if - even missing - files are ignored in dry run")), | |
5c46f754 KH |
265 | OPT_END(), |
266 | }; | |
267 | ||
9bd81e42 | 268 | static int add_config(const char *var, const char *value, void *cb) |
dad25e4a | 269 | { |
8c2be75f JN |
270 | if (!strcmp(var, "add.ignoreerrors") || |
271 | !strcmp(var, "add.ignore-errors")) { | |
dad25e4a AR |
272 | ignore_add_errors = git_config_bool(var, value); |
273 | return 0; | |
274 | } | |
9bd81e42 | 275 | return git_default_config(var, value, cb); |
dad25e4a AR |
276 | } |
277 | ||
c972ec04 JH |
278 | static int add_files(struct dir_struct *dir, int flags) |
279 | { | |
280 | int i, exit_status = 0; | |
281 | ||
282 | if (dir->ignored_nr) { | |
439fb829 | 283 | fprintf(stderr, _(ignore_error)); |
c972ec04 JH |
284 | for (i = 0; i < dir->ignored_nr; i++) |
285 | fprintf(stderr, "%s\n", dir->ignored[i]->name); | |
439fb829 ÆAB |
286 | fprintf(stderr, _("Use -f if you really want to add them.\n")); |
287 | die(_("no files added")); | |
c972ec04 JH |
288 | } |
289 | ||
290 | for (i = 0; i < dir->nr; i++) | |
291 | if (add_file_to_cache(dir->entries[i]->name, flags)) { | |
292 | if (!ignore_add_errors) | |
990ac4be | 293 | die(_("adding files failed")); |
c972ec04 JH |
294 | exit_status = 1; |
295 | } | |
296 | return exit_status; | |
297 | } | |
298 | ||
a633fca0 | 299 | int cmd_add(int argc, const char **argv, const char *prefix) |
0d781539 | 300 | { |
7ae02a30 | 301 | int exit_status = 0; |
c972ec04 | 302 | int newfd; |
5a76aff1 | 303 | struct pathspec pathspec; |
0d781539 | 304 | struct dir_struct dir; |
205ffa94 | 305 | int flags; |
c972ec04 JH |
306 | int add_new_files; |
307 | int require_pathspec; | |
81f45e7d | 308 | char *seen = NULL; |
5cde71d6 | 309 | |
ed342fde SB |
310 | git_config(add_config, NULL); |
311 | ||
37782920 | 312 | argc = parse_options(argc, argv, prefix, builtin_add_options, |
c59cb03a | 313 | builtin_add_usage, PARSE_OPT_KEEP_ARGV0); |
b63e9950 WC |
314 | if (patch_interactive) |
315 | add_interactive = 1; | |
7c0ab445 | 316 | if (add_interactive) |
b4bd4668 | 317 | exit(interactive_add(argc - 1, argv + 1, prefix, patch_interactive)); |
0d781539 | 318 | |
c59cb03a JS |
319 | if (edit_interactive) |
320 | return(edit_patch(argc, argv, prefix)); | |
321 | argc--; | |
322 | argv++; | |
323 | ||
45c45e30 JH |
324 | if (0 <= addremove_explicit) |
325 | addremove = addremove_explicit; | |
326 | else if (take_worktree_changes && ADDREMOVE_DEFAULT) | |
327 | addremove = 0; /* "-u" was given but not "-A" */ | |
328 | ||
3ba1f114 | 329 | if (addremove && take_worktree_changes) |
990ac4be | 330 | die(_("-A and -u are mutually incompatible")); |
45c45e30 | 331 | |
45c45e30 | 332 | if (!take_worktree_changes && addremove_explicit < 0 && argc) |
fdc97abd JH |
333 | /* Turn "git add pathspec..." to "git add -A pathspec..." */ |
334 | addremove = 1; | |
45c45e30 | 335 | |
108da0db | 336 | if (!show_only && ignore_missing) |
990ac4be | 337 | die(_("Option --ignore-missing can only be used together with --dry-run")); |
808d3d71 | 338 | |
160c4b18 | 339 | if ((0 < addremove_explicit || take_worktree_changes) && !argc) { |
808d3d71 | 340 | static const char *whole[2] = { ":/", NULL }; |
3ba1f114 | 341 | argc = 1; |
808d3d71 | 342 | argv = whole; |
3ba1f114 JH |
343 | } |
344 | ||
c972ec04 JH |
345 | add_new_files = !take_worktree_changes && !refresh_only; |
346 | require_pathspec = !take_worktree_changes; | |
347 | ||
30ca07a2 | 348 | newfd = hold_locked_index(&lock_file, 1); |
0d781539 | 349 | |
205ffa94 | 350 | flags = ((verbose ? ADD_CACHE_VERBOSE : 0) | |
01665924 | 351 | (show_only ? ADD_CACHE_PRETEND : 0) | |
39425819 | 352 | (intent_to_add ? ADD_CACHE_INTENT : 0) | |
1e5f764c JH |
353 | (ignore_add_errors ? ADD_CACHE_IGNORE_ERRORS : 0) | |
354 | (!(addremove || take_worktree_changes) | |
808d3d71 | 355 | ? ADD_CACHE_IGNORE_REMOVAL : 0)); |
205ffa94 | 356 | |
c972ec04 | 357 | if (require_pathspec && argc == 0) { |
990ac4be ÆAB |
358 | fprintf(stderr, _("Nothing specified, nothing added.\n")); |
359 | fprintf(stderr, _("Maybe you wanted to say 'git add .'?\n")); | |
93b0d86a JH |
360 | return 0; |
361 | } | |
0d781539 | 362 | |
366bfcb6 | 363 | if (read_cache() < 0) |
990ac4be | 364 | die(_("index file corrupt")); |
5a76aff1 NTND |
365 | |
366 | /* | |
367 | * Check the "pathspec '%s' did not match any files" block | |
368 | * below before enabling new magic. | |
369 | */ | |
370 | parse_pathspec(&pathspec, 0, | |
371 | PATHSPEC_PREFER_FULL | | |
372 | PATHSPEC_SYMLINK_LEADING_PATH | | |
373 | PATHSPEC_STRIP_SUBMODULE_SLASH_EXPENSIVE, | |
374 | prefix, argv); | |
366bfcb6 | 375 | |
1d8842d9 LT |
376 | if (add_new_files) { |
377 | int baselen; | |
7327d3d1 | 378 | struct pathspec empty_pathspec; |
1d8842d9 LT |
379 | |
380 | /* Set up the default git porcelain excludes */ | |
381 | memset(&dir, 0, sizeof(dir)); | |
382 | if (!ignored_too) { | |
383 | dir.flags |= DIR_COLLECT_IGNORED; | |
384 | setup_standard_excludes(&dir); | |
385 | } | |
386 | ||
7327d3d1 | 387 | memset(&empty_pathspec, 0, sizeof(empty_pathspec)); |
1e5f764c | 388 | /* This picks up the paths that are not tracked */ |
053a6b18 | 389 | baselen = fill_directory(&dir, &pathspec); |
5a76aff1 | 390 | if (pathspec.nr) |
053a6b18 | 391 | seen = prune_directory(&dir, &pathspec, baselen); |
1d8842d9 | 392 | } |
1e5f764c | 393 | |
c972ec04 | 394 | if (refresh_only) { |
9b2d6149 | 395 | refresh(verbose, &pathspec); |
c972ec04 | 396 | goto finish; |
6a1ad325 JH |
397 | } |
398 | ||
5a76aff1 | 399 | if (pathspec.nr) { |
81f45e7d | 400 | int i; |
eb69934b | 401 | |
81f45e7d | 402 | if (!seen) |
84b8b5d1 | 403 | seen = find_pathspecs_matching_against_index(&pathspec); |
5a76aff1 NTND |
404 | |
405 | /* | |
406 | * file_exists() assumes exact match | |
407 | */ | |
bd30c2e4 NTND |
408 | GUARD_PATHSPEC(&pathspec, |
409 | PATHSPEC_FROMTOP | | |
410 | PATHSPEC_LITERAL | | |
93d93537 | 411 | PATHSPEC_GLOB | |
ef79b1f8 NTND |
412 | PATHSPEC_ICASE | |
413 | PATHSPEC_EXCLUDE); | |
5a76aff1 | 414 | |
84b8b5d1 NTND |
415 | for (i = 0; i < pathspec.nr; i++) { |
416 | const char *path = pathspec.items[i].match; | |
ef79b1f8 NTND |
417 | if (pathspec.items[i].magic & PATHSPEC_EXCLUDE) |
418 | continue; | |
64ed07ce | 419 | if (!seen[i] && path[0] && |
93d93537 NTND |
420 | ((pathspec.items[i].magic & |
421 | (PATHSPEC_GLOB | PATHSPEC_ICASE)) || | |
bd30c2e4 | 422 | !file_exists(path))) { |
108da0db | 423 | if (ignore_missing) { |
0188f6b3 | 424 | int dtype = DT_UNKNOWN; |
84b8b5d1 NTND |
425 | if (is_excluded(&dir, path, &dtype)) |
426 | dir_add_ignored(&dir, path, pathspec.items[i].len); | |
108da0db | 427 | } else |
48168851 | 428 | die(_("pathspec '%s' did not match any files"), |
84b8b5d1 | 429 | pathspec.items[i].original); |
108da0db | 430 | } |
81f45e7d JH |
431 | } |
432 | free(seen); | |
433 | } | |
434 | ||
568508e7 JH |
435 | plug_bulk_checkin(); |
436 | ||
160c4b18 | 437 | exit_status |= add_files_to_cache(prefix, &pathspec, flags); |
c972ec04 JH |
438 | |
439 | if (add_new_files) | |
440 | exit_status |= add_files(&dir, flags); | |
0d781539 | 441 | |
568508e7 JH |
442 | unplug_bulk_checkin(); |
443 | ||
d521abf8 | 444 | finish: |
0d781539 LT |
445 | if (active_cache_changed) { |
446 | if (write_cache(newfd, active_cache, active_nr) || | |
4ed7cd3a | 447 | commit_locked_index(&lock_file)) |
990ac4be | 448 | die(_("Unable to write new index file")); |
0d781539 LT |
449 | } |
450 | ||
7ae02a30 | 451 | return exit_status; |
0d781539 | 452 | } |