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