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