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