Raw
1 /*
2 * "git add" builtin command
3 *
4 * Copyright (C) 2006 Linus Torvalds
5 */
6
7 #include "builtin.h"
8 #include "advice.h"
9 #include "config.h"
10 #include "environment.h"
11 #include "lockfile.h"
12 #include "editor.h"
13 #include "dir.h"
14 #include "gettext.h"
15 #include "pathspec.h"
16 #include "object-file.h"
17 #include "odb.h"
18 #include "odb/transaction.h"
19 #include "parse-options.h"
20 #include "path.h"
21 #include "preload-index.h"
22 #include "diff.h"
23 #include "read-cache.h"
24 #include "revision.h"
25 #include "submodule.h"
26 #include "add-interactive.h"
27 #include "apply.h"
28
29 static const char * const builtin_add_usage[] = {
30 N_("git add [<options>] [--] <pathspec>..."),
31 NULL
32 };
33 static int patch_interactive, add_interactive, edit_interactive;
34 static struct interactive_options interactive_opts = INTERACTIVE_OPTIONS_INIT;
35 static int take_worktree_changes;
36 static int add_renormalize;
37 static int pathspec_file_nul;
38 static int include_sparse;
39 static const char *pathspec_from_file;
40
41 static int chmod_pathspec(struct repository *repo,
42 struct pathspec *pathspec,
43 char flip,
44 int show_only)
45 {
46 int ret = 0;
47
48 for (size_t i = 0; i < repo->index->cache_nr; i++) {
49 struct cache_entry *ce = repo->index->cache[i];
50 int err;
51
52 if (!include_sparse &&
53 (ce_skip_worktree(ce) ||
54 !path_in_sparse_checkout(ce->name, repo->index)))
55 continue;
56
57 if (pathspec && !ce_path_match(repo->index, ce, pathspec, NULL))
58 continue;
59
60 if (!show_only)
61 err = chmod_index_entry(repo->index, ce, flip);
62 else
63 err = S_ISREG(ce->ce_mode) ? 0 : -1;
64
65 if (err < 0)
66 ret = error(_("cannot chmod %cx '%s'"), flip, ce->name);
67 }
68
69 return ret;
70 }
71
72 static int renormalize_tracked_files(struct repository *repo,
73 const struct pathspec *pathspec,
74 int flags)
75 {
76 int retval = 0;
77
78 for (size_t i = 0; i < repo->index->cache_nr; i++) {
79 struct cache_entry *ce = repo->index->cache[i];
80
81 if (!include_sparse &&
82 (ce_skip_worktree(ce) ||
83 !path_in_sparse_checkout(ce->name, repo->index)))
84 continue;
85 if (ce_stage(ce))
86 continue; /* do not touch unmerged paths */
87 if (!S_ISREG(ce->ce_mode) && !S_ISLNK(ce->ce_mode))
88 continue; /* do not touch non blobs */
89 if (pathspec && !ce_path_match(repo->index, ce, pathspec, NULL))
90 continue;
91 retval |= add_file_to_index(repo->index, ce->name,
92 flags | ADD_CACHE_RENORMALIZE);
93 }
94
95 return retval;
96 }
97
98 static char *prune_directory(struct repository *repo,
99 struct dir_struct *dir,
100 struct pathspec *pathspec,
101 int prefix)
102 {
103 char *seen;
104 int i;
105 struct dir_entry **src, **dst;
106
107 seen = xcalloc(pathspec->nr, 1);
108
109 src = dst = dir->entries;
110 i = dir->nr;
111 while (--i >= 0) {
112 struct dir_entry *entry = *src++;
113 if (dir_path_match(repo->index, entry, pathspec, prefix, seen))
114 *dst++ = entry;
115 }
116 dir->nr = dst - dir->entries;
117 add_pathspec_matches_against_index(pathspec, repo->index, seen,
118 PS_IGNORE_SKIP_WORKTREE);
119 return seen;
120 }
121
122 static int refresh(struct repository *repo, int verbose, const struct pathspec *pathspec)
123 {
124 char *seen;
125 int i, ret = 0;
126 char *skip_worktree_seen = NULL;
127 struct string_list only_match_skip_worktree = STRING_LIST_INIT_NODUP;
128 unsigned int flags = REFRESH_IGNORE_SKIP_WORKTREE |
129 (verbose ? REFRESH_IN_PORCELAIN : REFRESH_QUIET);
130
131 seen = xcalloc(pathspec->nr, 1);
132 refresh_index(repo->index, flags, pathspec, seen,
133 _("Unstaged changes after refreshing the index:"));
134 for (i = 0; i < pathspec->nr; i++) {
135 if (!seen[i]) {
136 const char *path = pathspec->items[i].original;
137
138 if (matches_skip_worktree(pathspec, i, &skip_worktree_seen) ||
139 !path_in_sparse_checkout(path, repo->index)) {
140 string_list_append(&only_match_skip_worktree,
141 pathspec->items[i].original);
142 } else {
143 die(_("pathspec '%s' did not match any files"),
144 pathspec->items[i].original);
145 }
146 }
147 }
148
149 if (only_match_skip_worktree.nr) {
150 advise_on_updating_sparse_paths(&only_match_skip_worktree);
151 ret = 1;
152 }
153
154 free(seen);
155 free(skip_worktree_seen);
156 string_list_clear(&only_match_skip_worktree, 0);
157 return ret;
158 }
159
160 int interactive_add(struct repository *repo,
161 const char **argv,
162 const char *prefix,
163 int patch, struct interactive_options *interactive_opts)
164 {
165 struct pathspec pathspec;
166 int ret;
167
168 parse_pathspec(&pathspec, 0,
169 PATHSPEC_PREFER_FULL |
170 PATHSPEC_SYMLINK_LEADING_PATH |
171 PATHSPEC_PREFIX_ORIGIN,
172 prefix, argv);
173
174 if (patch)
175 ret = !!run_add_p(repo, ADD_P_ADD, interactive_opts, NULL, &pathspec, 0);
176 else
177 ret = !!run_add_i(repo, &pathspec, interactive_opts);
178
179 clear_pathspec(&pathspec);
180 return ret;
181 }
182
183 static int edit_patch(struct repository *repo,
184 int argc,
185 const char **argv,
186 const char *prefix)
187 {
188 char *file = repo_git_path(repo, "ADD_EDIT.patch");
189 struct apply_state state;
190 const char *apply_argv[2];
191 struct rev_info rev;
192 int out;
193 struct stat st;
194
195 repo_config(repo, git_diff_basic_config, NULL);
196
197 if (repo_read_index(repo) < 0)
198 die(_("could not read the index"));
199
200 repo_init_revisions(repo, &rev, prefix);
201 rev.diffopt.context = 7;
202
203 argc = setup_revisions(argc, argv, &rev, NULL);
204 rev.diffopt.output_format = DIFF_FORMAT_PATCH;
205 rev.diffopt.use_color = GIT_COLOR_NEVER;
206 rev.diffopt.flags.ignore_dirty_submodules = 1;
207 out = xopen(file, O_CREAT | O_WRONLY | O_TRUNC, 0666);
208 rev.diffopt.file = xfdopen(out, "w");
209 rev.diffopt.close_file = 1;
210 run_diff_files(&rev, 0);
211
212 if (launch_editor(file, NULL, NULL))
213 die(_("editing patch failed"));
214
215 if (stat(file, &st))
216 die_errno(_("could not stat '%s'"), file);
217 if (!st.st_size)
218 die(_("empty patch. aborted"));
219
220 apply_argv[0] = file;
221 apply_argv[1] = NULL;
222 if (init_apply_state(&state, repo, NULL))
223 die(_("could not initialize apply state"));
224 state.cached = 1;
225 if (check_apply_state(&state, 0))
226 die(_("could not check apply state"));
227 if (apply_all_patches(&state, 1, apply_argv, APPLY_OPT_RECOUNT))
228 die(_("could not apply '%s'"), file);
229 clear_apply_state(&state);
230
231 unlink(file);
232 free(file);
233 release_revisions(&rev);
234 return 0;
235 }
236
237 static const char ignore_error[] =
238 N_("The following paths are ignored by one of your .gitignore files:\n");
239
240 static int verbose, show_only, ignored_too, refresh_only;
241 static int ignore_add_errors, intent_to_add, ignore_missing;
242 static int warn_on_embedded_repo = 1;
243
244 #define ADDREMOVE_DEFAULT 1
245 static int addremove = ADDREMOVE_DEFAULT;
246 static int addremove_explicit = -1; /* unspecified */
247
248 static char *chmod_arg;
249
250 static int ignore_removal_cb(const struct option *opt, const char *arg, int unset)
251 {
252 BUG_ON_OPT_ARG(arg);
253
254 /* if we are told to ignore, we are not adding removals */
255 *(int *)opt->value = !unset ? 0 : 1;
256 return 0;
257 }
258
259 static struct option builtin_add_options[] = {
260 OPT__DRY_RUN(&show_only, N_("dry run")),
261 OPT__VERBOSE(&verbose, N_("be verbose")),
262 OPT_GROUP(""),
263 OPT_BOOL('i', "interactive", &add_interactive, N_("interactive picking")),
264 OPT_BOOL('p', "patch", &patch_interactive, N_("select hunks interactively")),
265 OPT_BOOL(0, "auto-advance", &interactive_opts.auto_advance,
266 N_("auto advance to the next file when selecting hunks interactively")),
267 OPT_DIFF_UNIFIED(&interactive_opts.context),
268 OPT_DIFF_INTERHUNK_CONTEXT(&interactive_opts.interhunkcontext),
269 OPT_BOOL('e', "edit", &edit_interactive, N_("edit current diff and apply")),
270 OPT__FORCE(&ignored_too, N_("allow adding otherwise ignored files"), 0),
271 OPT_BOOL('u', "update", &take_worktree_changes, N_("update tracked files")),
272 OPT_BOOL(0, "renormalize", &add_renormalize, N_("renormalize EOL of tracked files (implies -u)")),
273 OPT_BOOL('N', "intent-to-add", &intent_to_add, N_("record only the fact that the path will be added later")),
274 OPT_BOOL('A', "all", &addremove_explicit, N_("add changes from all tracked and untracked files")),
275 OPT_CALLBACK_F(0, "ignore-removal", &addremove_explicit,
276 NULL /* takes no arguments */,
277 N_("ignore paths removed in the working tree (same as --no-all)"),
278 PARSE_OPT_NOARG, ignore_removal_cb),
279 OPT_BOOL( 0 , "refresh", &refresh_only, N_("don't add, only refresh the index")),
280 OPT_BOOL( 0 , "ignore-errors", &ignore_add_errors, N_("just skip files which cannot be added because of errors")),
281 OPT_BOOL( 0 , "ignore-missing", &ignore_missing, N_("check if - even missing - files are ignored in dry run")),
282 OPT_BOOL(0, "sparse", &include_sparse, N_("allow updating entries outside of the sparse-checkout cone")),
283 OPT_STRING(0, "chmod", &chmod_arg, "(+|-)x",
284 N_("override the executable bit of the listed files")),
285 OPT_HIDDEN_BOOL(0, "warn-embedded-repo", &warn_on_embedded_repo,
286 N_("warn when adding an embedded repository")),
287 OPT_PATHSPEC_FROM_FILE(&pathspec_from_file),
288 OPT_PATHSPEC_FILE_NUL(&pathspec_file_nul),
289 OPT_END(),
290 };
291
292 static int add_config(const char *var, const char *value,
293 const struct config_context *ctx, void *cb)
294 {
295 if (!strcmp(var, "add.ignoreerrors") ||
296 !strcmp(var, "add.ignore-errors")) {
297 ignore_add_errors = git_config_bool(var, value);
298 return 0;
299 }
300
301 if (git_color_config(var, value, cb) < 0)
302 return -1;
303
304 return git_default_config(var, value, ctx, cb);
305 }
306
307 static const char embedded_advice[] = N_(
308 "You've added another git repository inside your current repository.\n"
309 "Clones of the outer repository will not contain the contents of\n"
310 "the embedded repository and will not know how to obtain it.\n"
311 "If you meant to add a submodule, use:\n"
312 "\n"
313 " git submodule add <url> %s\n"
314 "\n"
315 "If you added this path by mistake, you can remove it from the\n"
316 "index with:\n"
317 "\n"
318 " git rm --cached %s\n"
319 "\n"
320 "See \"git help submodule\" for more information."
321 );
322
323 static void check_embedded_repo(const char *path)
324 {
325 struct strbuf name = STRBUF_INIT;
326 static int adviced_on_embedded_repo = 0;
327
328 if (!warn_on_embedded_repo)
329 return;
330 if (!ends_with(path, "/"))
331 return;
332
333 /* Drop trailing slash for aesthetics */
334 strbuf_addstr(&name, path);
335 strbuf_strip_suffix(&name, "/");
336
337 warning(_("adding embedded git repository: %s"), name.buf);
338 if (!adviced_on_embedded_repo) {
339 advise_if_enabled(ADVICE_ADD_EMBEDDED_REPO,
340 embedded_advice, name.buf, name.buf);
341 adviced_on_embedded_repo = 1;
342 }
343
344 strbuf_release(&name);
345 }
346
347 static int add_files(struct repository *repo, struct dir_struct *dir, int flags)
348 {
349 int i, exit_status = 0;
350 struct string_list matched_sparse_paths = STRING_LIST_INIT_NODUP;
351
352 if (dir->ignored_nr) {
353 fprintf(stderr, _(ignore_error));
354 for (i = 0; i < dir->ignored_nr; i++)
355 fprintf(stderr, "%s\n", dir->ignored[i]->name);
356 advise_if_enabled(ADVICE_ADD_IGNORED_FILE,
357 _("Use -f if you really want to add them."));
358 exit_status = 1;
359 }
360
361 for (i = 0; i < dir->nr; i++) {
362 if (!include_sparse &&
363 !path_in_sparse_checkout(dir->entries[i]->name, repo->index)) {
364 string_list_append(&matched_sparse_paths,
365 dir->entries[i]->name);
366 continue;
367 }
368 if (add_file_to_index(repo->index, dir->entries[i]->name, flags)) {
369 if (!ignore_add_errors)
370 die(_("adding files failed"));
371 exit_status = 1;
372 } else {
373 check_embedded_repo(dir->entries[i]->name);
374 }
375 }
376
377 if (matched_sparse_paths.nr) {
378 advise_on_updating_sparse_paths(&matched_sparse_paths);
379 exit_status = 1;
380 }
381
382 string_list_clear(&matched_sparse_paths, 0);
383
384 return exit_status;
385 }
386
387 int cmd_add(int argc,
388 const char **argv,
389 const char *prefix,
390 struct repository *repo)
391 {
392 int exit_status = 0;
393 struct pathspec pathspec;
394 struct dir_struct dir = DIR_INIT;
395 int flags;
396 int add_new_files;
397 int require_pathspec;
398 char *seen = NULL;
399 char *ps_matched = NULL;
400 struct lock_file lock_file = LOCK_INIT;
401 struct odb_transaction *transaction;
402
403 repo_config(repo, add_config, NULL);
404
405 argc = parse_options(argc, argv, prefix, builtin_add_options,
406 builtin_add_usage, PARSE_OPT_KEEP_ARGV0);
407
408 prepare_repo_settings(repo);
409 repo->settings.command_requires_full_index = 0;
410
411 if (interactive_opts.context < -1)
412 die(_("'%s' cannot be negative"), "--unified");
413 if (interactive_opts.interhunkcontext < -1)
414 die(_("'%s' cannot be negative"), "--inter-hunk-context");
415
416 if (patch_interactive)
417 add_interactive = 1;
418 if (add_interactive) {
419 if (show_only)
420 die(_("options '%s' and '%s' cannot be used together"), "--dry-run", "--interactive/--patch");
421 if (pathspec_from_file)
422 die(_("options '%s' and '%s' cannot be used together"), "--pathspec-from-file", "--interactive/--patch");
423 exit(interactive_add(repo, argv + 1, prefix, patch_interactive, &interactive_opts));
424 } else {
425 if (interactive_opts.context != -1)
426 die(_("the option '%s' requires '%s'"), "--unified", "--interactive/--patch");
427 if (interactive_opts.interhunkcontext != -1)
428 die(_("the option '%s' requires '%s'"), "--inter-hunk-context", "--interactive/--patch");
429 if (!interactive_opts.auto_advance)
430 die(_("the option '%s' requires '%s'"), "--no-auto-advance", "--interactive/--patch");
431 }
432
433 if (edit_interactive) {
434 if (pathspec_from_file)
435 die(_("options '%s' and '%s' cannot be used together"), "--pathspec-from-file", "--edit");
436 return(edit_patch(repo, argc, argv, prefix));
437 }
438 argc--;
439 argv++;
440
441 if (0 <= addremove_explicit)
442 addremove = addremove_explicit;
443 else if (take_worktree_changes && ADDREMOVE_DEFAULT)
444 addremove = 0; /* "-u" was given but not "-A" */
445
446 if (addremove && take_worktree_changes)
447 die(_("options '%s' and '%s' cannot be used together"), "-A", "-u");
448
449 if (!show_only && ignore_missing)
450 die(_("the option '%s' requires '%s'"), "--ignore-missing", "--dry-run");
451
452 if (chmod_arg && ((chmod_arg[0] != '-' && chmod_arg[0] != '+') ||
453 chmod_arg[1] != 'x' || chmod_arg[2]))
454 die(_("--chmod param '%s' must be either -x or +x"), chmod_arg);
455
456 add_new_files = !take_worktree_changes && !refresh_only && !add_renormalize;
457 require_pathspec = !(take_worktree_changes || (0 < addremove_explicit));
458
459 repo_hold_locked_index(repo, &lock_file, LOCK_DIE_ON_ERROR);
460
461 /*
462 * Check the "pathspec '%s' did not match any files" block
463 * below before enabling new magic.
464 */
465 parse_pathspec(&pathspec, 0,
466 PATHSPEC_PREFER_FULL |
467 PATHSPEC_SYMLINK_LEADING_PATH,
468 prefix, argv);
469
470 if (pathspec_from_file) {
471 if (pathspec.nr)
472 die(_("'%s' and pathspec arguments cannot be used together"), "--pathspec-from-file");
473
474 parse_pathspec_file(&pathspec, 0,
475 PATHSPEC_PREFER_FULL |
476 PATHSPEC_SYMLINK_LEADING_PATH,
477 prefix, pathspec_from_file, pathspec_file_nul);
478 } else if (pathspec_file_nul) {
479 die(_("the option '%s' requires '%s'"), "--pathspec-file-nul", "--pathspec-from-file");
480 }
481
482 if (require_pathspec && pathspec.nr == 0) {
483 fprintf(stderr, _("Nothing specified, nothing added.\n"));
484 advise_if_enabled(ADVICE_ADD_EMPTY_PATHSPEC,
485 _("Maybe you wanted to say 'git add .'?"));
486 return 0;
487 }
488
489 if (!take_worktree_changes && addremove_explicit < 0 && pathspec.nr)
490 /* Turn "git add pathspec..." to "git add -A pathspec..." */
491 addremove = 1;
492
493 flags = ((verbose ? ADD_CACHE_VERBOSE : 0) |
494 (show_only ? ADD_CACHE_PRETEND : 0) |
495 (intent_to_add ? ADD_CACHE_INTENT : 0) |
496 (ignore_add_errors ? ADD_CACHE_IGNORE_ERRORS : 0) |
497 (!(addremove || take_worktree_changes)
498 ? ADD_CACHE_IGNORE_REMOVAL : 0));
499
500 if (repo_read_index_preload(repo, &pathspec, 0) < 0)
501 die(_("index file corrupt"));
502
503 die_in_unpopulated_submodule(repo->index, prefix);
504 die_path_inside_submodule(repo->index, &pathspec);
505
506 if (add_new_files) {
507 int baselen;
508
509 /* Set up the default git porcelain excludes */
510 if (!ignored_too) {
511 dir.flags |= DIR_COLLECT_IGNORED;
512 setup_standard_excludes(&dir);
513 }
514
515 /* This picks up the paths that are not tracked */
516 baselen = fill_directory(&dir, repo->index, &pathspec);
517 if (pathspec.nr)
518 seen = prune_directory(repo, &dir, &pathspec, baselen);
519 }
520
521 if (refresh_only) {
522 exit_status |= refresh(repo, verbose, &pathspec);
523 goto finish;
524 }
525
526 if (pathspec.nr) {
527 int i;
528 char *skip_worktree_seen = NULL;
529 struct string_list only_match_skip_worktree = STRING_LIST_INIT_NODUP;
530
531 if (!seen)
532 seen = find_pathspecs_matching_against_index(&pathspec,
533 repo->index, PS_IGNORE_SKIP_WORKTREE);
534
535 /*
536 * file_exists() assumes exact match
537 */
538 GUARD_PATHSPEC(&pathspec,
539 PATHSPEC_FROMTOP |
540 PATHSPEC_LITERAL |
541 PATHSPEC_GLOB |
542 PATHSPEC_ICASE |
543 PATHSPEC_EXCLUDE |
544 PATHSPEC_ATTR);
545
546 for (i = 0; i < pathspec.nr; i++) {
547 const char *path = pathspec.items[i].match;
548
549 if (pathspec.items[i].magic & PATHSPEC_EXCLUDE)
550 continue;
551 if (seen[i])
552 continue;
553
554 if (!include_sparse &&
555 matches_skip_worktree(&pathspec, i, &skip_worktree_seen)) {
556 string_list_append(&only_match_skip_worktree,
557 pathspec.items[i].original);
558 continue;
559 }
560
561 /* Don't complain at 'git add .' on empty repo */
562 if (!path[0])
563 continue;
564
565 if ((pathspec.items[i].magic & (PATHSPEC_GLOB | PATHSPEC_ICASE)) ||
566 !file_exists(path)) {
567 if (ignore_missing) {
568 int dtype = DT_UNKNOWN;
569 if (is_excluded(&dir, repo->index, path, &dtype))
570 dir_add_ignored(&dir, repo->index,
571 path, pathspec.items[i].len);
572 } else
573 die(_("pathspec '%s' did not match any files"),
574 pathspec.items[i].original);
575 }
576 }
577
578
579 if (only_match_skip_worktree.nr) {
580 advise_on_updating_sparse_paths(&only_match_skip_worktree);
581 exit_status = 1;
582 }
583
584 free(seen);
585 free(skip_worktree_seen);
586 string_list_clear(&only_match_skip_worktree, 0);
587 }
588
589 odb_transaction_begin_or_die(repo->objects, &transaction, 0);
590
591 ps_matched = xcalloc(pathspec.nr, 1);
592 if (add_renormalize)
593 exit_status |= renormalize_tracked_files(repo, &pathspec, flags);
594 else
595 exit_status |= add_files_to_cache(repo, prefix,
596 &pathspec, ps_matched,
597 include_sparse, flags, ignored_too);
598
599 if (take_worktree_changes && !add_renormalize && !ignore_add_errors &&
600 report_path_error(ps_matched, &pathspec))
601 exit(128);
602
603 if (add_new_files)
604 exit_status |= add_files(repo, &dir, flags);
605
606 if (chmod_arg && pathspec.nr)
607 exit_status |= chmod_pathspec(repo, &pathspec, chmod_arg[0], show_only);
608 odb_transaction_commit(transaction);
609
610 finish:
611 if (write_locked_index(repo->index, &lock_file,
612 COMMIT_LOCK | SKIP_IF_UNCHANGED))
613 die(_("unable to write new index file"));
614
615 free(ps_matched);
616 dir_clear(&dir);
617 clear_pathspec(&pathspec);
618 return exit_status;
619 }