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