2 * "git add" builtin command
4 * Copyright (C) 2006 Linus Torvalds
6 #define USE_THE_INDEX_COMPATIBILITY_MACROS
14 #include "cache-tree.h"
15 #include "run-command.h"
16 #include "parse-options.h"
20 #include "bulk-checkin.h"
22 #include "submodule.h"
23 #include "add-interactive.h"
25 static const char * const builtin_add_usage
[] = {
26 N_("git add [<options>] [--] <pathspec>..."),
29 static int patch_interactive
, add_interactive
, edit_interactive
;
30 static int take_worktree_changes
;
31 static int add_renormalize
;
32 static int pathspec_file_nul
;
33 static int include_sparse
;
34 static const char *pathspec_from_file
;
35 static int legacy_stash_p
; /* support for the scripted `git stash` */
37 struct update_callback_data
{
42 static int chmod_pathspec(struct pathspec
*pathspec
, char flip
, int show_only
)
46 for (i
= 0; i
< active_nr
; i
++) {
47 struct cache_entry
*ce
= active_cache
[i
];
50 if (!include_sparse
&&
51 (ce_skip_worktree(ce
) ||
52 !path_in_sparse_checkout(ce
->name
, &the_index
)))
55 if (pathspec
&& !ce_path_match(&the_index
, ce
, pathspec
, NULL
))
59 err
= chmod_cache_entry(ce
, flip
);
61 err
= S_ISREG(ce
->ce_mode
) ? 0 : -1;
64 ret
= error(_("cannot chmod %cx '%s'"), flip
, ce
->name
);
70 static int fix_unmerged_status(struct diff_filepair
*p
,
71 struct update_callback_data
*data
)
73 if (p
->status
!= DIFF_STATUS_UNMERGED
)
75 if (!(data
->flags
& ADD_CACHE_IGNORE_REMOVAL
) && !p
->two
->mode
)
77 * This is not an explicit add request, and the
78 * path is missing from the working tree (deleted)
80 return DIFF_STATUS_DELETED
;
83 * Either an explicit add request, or path exists
84 * in the working tree. An attempt to explicitly
85 * add a path that does not exist in the working tree
86 * will be caught as an error by the caller immediately.
88 return DIFF_STATUS_MODIFIED
;
91 static void update_callback(struct diff_queue_struct
*q
,
92 struct diff_options
*opt
, void *cbdata
)
95 struct update_callback_data
*data
= cbdata
;
97 for (i
= 0; i
< q
->nr
; i
++) {
98 struct diff_filepair
*p
= q
->queue
[i
];
99 const char *path
= p
->one
->path
;
101 if (!include_sparse
&& !path_in_sparse_checkout(path
, &the_index
))
104 switch (fix_unmerged_status(p
, data
)) {
106 die(_("unexpected diff status %c"), p
->status
);
107 case DIFF_STATUS_MODIFIED
:
108 case DIFF_STATUS_TYPE_CHANGED
:
109 if (add_file_to_index(&the_index
, path
, data
->flags
)) {
110 if (!(data
->flags
& ADD_CACHE_IGNORE_ERRORS
))
111 die(_("updating files failed"));
115 case DIFF_STATUS_DELETED
:
116 if (data
->flags
& ADD_CACHE_IGNORE_REMOVAL
)
118 if (!(data
->flags
& ADD_CACHE_PRETEND
))
119 remove_file_from_index(&the_index
, path
);
120 if (data
->flags
& (ADD_CACHE_PRETEND
|ADD_CACHE_VERBOSE
))
121 printf(_("remove '%s'\n"), path
);
127 int add_files_to_cache(const char *prefix
,
128 const struct pathspec
*pathspec
, int flags
)
130 struct update_callback_data data
;
133 memset(&data
, 0, sizeof(data
));
136 repo_init_revisions(the_repository
, &rev
, prefix
);
137 setup_revisions(0, NULL
, &rev
, NULL
);
139 copy_pathspec(&rev
.prune_data
, pathspec
);
140 rev
.diffopt
.output_format
= DIFF_FORMAT_CALLBACK
;
141 rev
.diffopt
.format_callback
= update_callback
;
142 rev
.diffopt
.format_callback_data
= &data
;
143 rev
.diffopt
.flags
.override_submodule_config
= 1;
144 rev
.max_count
= 0; /* do not compare unmerged paths with stage #2 */
145 run_diff_files(&rev
, DIFF_RACY_IS_MODIFIED
);
146 clear_pathspec(&rev
.prune_data
);
147 return !!data
.add_errors
;
150 static int renormalize_tracked_files(const struct pathspec
*pathspec
, int flags
)
154 for (i
= 0; i
< active_nr
; i
++) {
155 struct cache_entry
*ce
= active_cache
[i
];
157 if (!include_sparse
&&
158 (ce_skip_worktree(ce
) ||
159 !path_in_sparse_checkout(ce
->name
, &the_index
)))
162 continue; /* do not touch unmerged paths */
163 if (!S_ISREG(ce
->ce_mode
) && !S_ISLNK(ce
->ce_mode
))
164 continue; /* do not touch non blobs */
165 if (pathspec
&& !ce_path_match(&the_index
, ce
, pathspec
, NULL
))
167 retval
|= add_file_to_cache(ce
->name
, flags
| ADD_CACHE_RENORMALIZE
);
173 static char *prune_directory(struct dir_struct
*dir
, struct pathspec
*pathspec
, int prefix
)
177 struct dir_entry
**src
, **dst
;
179 seen
= xcalloc(pathspec
->nr
, 1);
181 src
= dst
= dir
->entries
;
184 struct dir_entry
*entry
= *src
++;
185 if (dir_path_match(&the_index
, entry
, pathspec
, prefix
, seen
))
188 dir
->nr
= dst
- dir
->entries
;
189 add_pathspec_matches_against_index(pathspec
, &the_index
, seen
,
190 PS_IGNORE_SKIP_WORKTREE
);
194 static int refresh(int verbose
, const struct pathspec
*pathspec
)
198 char *skip_worktree_seen
= NULL
;
199 struct string_list only_match_skip_worktree
= STRING_LIST_INIT_NODUP
;
200 int flags
= REFRESH_IGNORE_SKIP_WORKTREE
|
201 (verbose
? REFRESH_IN_PORCELAIN
: REFRESH_QUIET
);
203 seen
= xcalloc(pathspec
->nr
, 1);
204 refresh_index(&the_index
, flags
, pathspec
, seen
,
205 _("Unstaged changes after refreshing the index:"));
206 for (i
= 0; i
< pathspec
->nr
; i
++) {
208 const char *path
= pathspec
->items
[i
].original
;
210 if (matches_skip_worktree(pathspec
, i
, &skip_worktree_seen
) ||
211 !path_in_sparse_checkout(path
, &the_index
)) {
212 string_list_append(&only_match_skip_worktree
,
213 pathspec
->items
[i
].original
);
215 die(_("pathspec '%s' did not match any files"),
216 pathspec
->items
[i
].original
);
221 if (only_match_skip_worktree
.nr
) {
222 advise_on_updating_sparse_paths(&only_match_skip_worktree
);
227 free(skip_worktree_seen
);
228 string_list_clear(&only_match_skip_worktree
, 0);
232 int run_add_interactive(const char *revision
, const char *patch_mode
,
233 const struct pathspec
*pathspec
)
236 struct strvec argv
= STRVEC_INIT
;
237 int use_builtin_add_i
=
238 git_env_bool("GIT_TEST_ADD_I_USE_BUILTIN", -1);
240 if (use_builtin_add_i
< 0) {
242 if (!git_config_get_bool("add.interactive.usebuiltin",
245 else if (!git_config_get_bool("feature.experimental", &experimental
) &&
247 use_builtin_add_i
= 1;
250 if (use_builtin_add_i
== 1) {
251 enum add_p_mode mode
;
254 return !!run_add_i(the_repository
, pathspec
);
256 if (!strcmp(patch_mode
, "--patch"))
258 else if (!strcmp(patch_mode
, "--patch=stash"))
260 else if (!strcmp(patch_mode
, "--patch=reset"))
262 else if (!strcmp(patch_mode
, "--patch=checkout"))
263 mode
= ADD_P_CHECKOUT
;
264 else if (!strcmp(patch_mode
, "--patch=worktree"))
265 mode
= ADD_P_WORKTREE
;
267 die("'%s' not supported", patch_mode
);
269 return !!run_add_p(the_repository
, mode
, revision
, pathspec
);
272 strvec_push(&argv
, "add--interactive");
274 strvec_push(&argv
, patch_mode
);
276 strvec_push(&argv
, revision
);
277 strvec_push(&argv
, "--");
278 for (i
= 0; i
< pathspec
->nr
; i
++)
279 /* pass original pathspec, to be re-parsed */
280 strvec_push(&argv
, pathspec
->items
[i
].original
);
282 status
= run_command_v_opt(argv
.v
, RUN_GIT_CMD
);
287 int interactive_add(const char **argv
, const char *prefix
, int patch
)
289 struct pathspec pathspec
;
291 parse_pathspec(&pathspec
, 0,
292 PATHSPEC_PREFER_FULL
|
293 PATHSPEC_SYMLINK_LEADING_PATH
|
294 PATHSPEC_PREFIX_ORIGIN
,
297 return run_add_interactive(NULL
,
298 patch
? "--patch" : NULL
,
302 static int edit_patch(int argc
, const char **argv
, const char *prefix
)
304 char *file
= git_pathdup("ADD_EDIT.patch");
305 const char *apply_argv
[] = { "apply", "--recount", "--cached",
307 struct child_process child
= CHILD_PROCESS_INIT
;
312 apply_argv
[3] = file
;
314 git_config(git_diff_basic_config
, NULL
); /* no "diff" UI options */
316 if (read_cache() < 0)
317 die(_("Could not read the index"));
319 repo_init_revisions(the_repository
, &rev
, prefix
);
320 rev
.diffopt
.context
= 7;
322 argc
= setup_revisions(argc
, argv
, &rev
, NULL
);
323 rev
.diffopt
.output_format
= DIFF_FORMAT_PATCH
;
324 rev
.diffopt
.use_color
= 0;
325 rev
.diffopt
.flags
.ignore_dirty_submodules
= 1;
326 out
= xopen(file
, O_CREAT
| O_WRONLY
| O_TRUNC
, 0666);
327 rev
.diffopt
.file
= xfdopen(out
, "w");
328 rev
.diffopt
.close_file
= 1;
329 if (run_diff_files(&rev
, 0))
330 die(_("Could not write patch"));
332 if (launch_editor(file
, NULL
, NULL
))
333 die(_("editing patch failed"));
336 die_errno(_("Could not stat '%s'"), file
);
338 die(_("Empty patch. Aborted."));
341 child
.argv
= apply_argv
;
342 if (run_command(&child
))
343 die(_("Could not apply '%s'"), file
);
350 static const char ignore_error
[] =
351 N_("The following paths are ignored by one of your .gitignore files:\n");
353 static int verbose
, show_only
, ignored_too
, refresh_only
;
354 static int ignore_add_errors
, intent_to_add
, ignore_missing
;
355 static int warn_on_embedded_repo
= 1;
357 #define ADDREMOVE_DEFAULT 1
358 static int addremove
= ADDREMOVE_DEFAULT
;
359 static int addremove_explicit
= -1; /* unspecified */
361 static char *chmod_arg
;
363 static int ignore_removal_cb(const struct option
*opt
, const char *arg
, int unset
)
365 /* if we are told to ignore, we are not adding removals */
366 *(int *)opt
->value
= !unset
? 0 : 1;
370 static struct option builtin_add_options
[] = {
371 OPT__DRY_RUN(&show_only
, N_("dry run")),
372 OPT__VERBOSE(&verbose
, N_("be verbose")),
374 OPT_BOOL('i', "interactive", &add_interactive
, N_("interactive picking")),
375 OPT_BOOL('p', "patch", &patch_interactive
, N_("select hunks interactively")),
376 OPT_BOOL('e', "edit", &edit_interactive
, N_("edit current diff and apply")),
377 OPT__FORCE(&ignored_too
, N_("allow adding otherwise ignored files"), 0),
378 OPT_BOOL('u', "update", &take_worktree_changes
, N_("update tracked files")),
379 OPT_BOOL(0, "renormalize", &add_renormalize
, N_("renormalize EOL of tracked files (implies -u)")),
380 OPT_BOOL('N', "intent-to-add", &intent_to_add
, N_("record only the fact that the path will be added later")),
381 OPT_BOOL('A', "all", &addremove_explicit
, N_("add changes from all tracked and untracked files")),
382 OPT_CALLBACK_F(0, "ignore-removal", &addremove_explicit
,
383 NULL
/* takes no arguments */,
384 N_("ignore paths removed in the working tree (same as --no-all)"),
385 PARSE_OPT_NOARG
, ignore_removal_cb
),
386 OPT_BOOL( 0 , "refresh", &refresh_only
, N_("don't add, only refresh the index")),
387 OPT_BOOL( 0 , "ignore-errors", &ignore_add_errors
, N_("just skip files which cannot be added because of errors")),
388 OPT_BOOL( 0 , "ignore-missing", &ignore_missing
, N_("check if - even missing - files are ignored in dry run")),
389 OPT_BOOL(0, "sparse", &include_sparse
, N_("allow updating entries outside of the sparse-checkout cone")),
390 OPT_STRING(0, "chmod", &chmod_arg
, "(+|-)x",
391 N_("override the executable bit of the listed files")),
392 OPT_HIDDEN_BOOL(0, "warn-embedded-repo", &warn_on_embedded_repo
,
393 N_("warn when adding an embedded repository")),
394 OPT_HIDDEN_BOOL(0, "legacy-stash-p", &legacy_stash_p
,
395 N_("backend for `git stash -p`")),
396 OPT_PATHSPEC_FROM_FILE(&pathspec_from_file
),
397 OPT_PATHSPEC_FILE_NUL(&pathspec_file_nul
),
401 static int add_config(const char *var
, const char *value
, void *cb
)
403 if (!strcmp(var
, "add.ignoreerrors") ||
404 !strcmp(var
, "add.ignore-errors")) {
405 ignore_add_errors
= git_config_bool(var
, value
);
409 return git_default_config(var
, value
, cb
);
412 static const char embedded_advice
[] = N_(
413 "You've added another git repository inside your current repository.\n"
414 "Clones of the outer repository will not contain the contents of\n"
415 "the embedded repository and will not know how to obtain it.\n"
416 "If you meant to add a submodule, use:\n"
418 " git submodule add <url> %s\n"
420 "If you added this path by mistake, you can remove it from the\n"
423 " git rm --cached %s\n"
425 "See \"git help submodule\" for more information."
428 static void check_embedded_repo(const char *path
)
430 struct strbuf name
= STRBUF_INIT
;
431 static int adviced_on_embedded_repo
= 0;
433 if (!warn_on_embedded_repo
)
435 if (!ends_with(path
, "/"))
438 /* Drop trailing slash for aesthetics */
439 strbuf_addstr(&name
, path
);
440 strbuf_strip_suffix(&name
, "/");
442 warning(_("adding embedded git repository: %s"), name
.buf
);
443 if (!adviced_on_embedded_repo
&&
444 advice_enabled(ADVICE_ADD_EMBEDDED_REPO
)) {
445 advise(embedded_advice
, name
.buf
, name
.buf
);
446 adviced_on_embedded_repo
= 1;
449 strbuf_release(&name
);
452 static int add_files(struct dir_struct
*dir
, int flags
)
454 int i
, exit_status
= 0;
455 struct string_list matched_sparse_paths
= STRING_LIST_INIT_NODUP
;
457 if (dir
->ignored_nr
) {
458 fprintf(stderr
, _(ignore_error
));
459 for (i
= 0; i
< dir
->ignored_nr
; i
++)
460 fprintf(stderr
, "%s\n", dir
->ignored
[i
]->name
);
461 if (advice_enabled(ADVICE_ADD_IGNORED_FILE
))
462 advise(_("Use -f if you really want to add them.\n"
463 "Turn this message off by running\n"
464 "\"git config advice.addIgnoredFile false\""));
468 for (i
= 0; i
< dir
->nr
; i
++) {
469 if (!include_sparse
&&
470 !path_in_sparse_checkout(dir
->entries
[i
]->name
, &the_index
)) {
471 string_list_append(&matched_sparse_paths
,
472 dir
->entries
[i
]->name
);
475 if (add_file_to_index(&the_index
, dir
->entries
[i
]->name
, flags
)) {
476 if (!ignore_add_errors
)
477 die(_("adding files failed"));
480 check_embedded_repo(dir
->entries
[i
]->name
);
484 if (matched_sparse_paths
.nr
) {
485 advise_on_updating_sparse_paths(&matched_sparse_paths
);
489 string_list_clear(&matched_sparse_paths
, 0);
494 int cmd_add(int argc
, const char **argv
, const char *prefix
)
497 struct pathspec pathspec
;
498 struct dir_struct dir
= DIR_INIT
;
501 int require_pathspec
;
503 struct lock_file lock_file
= LOCK_INIT
;
505 git_config(add_config
, NULL
);
507 argc
= parse_options(argc
, argv
, prefix
, builtin_add_options
,
508 builtin_add_usage
, PARSE_OPT_KEEP_ARGV0
);
509 if (patch_interactive
)
511 if (add_interactive
) {
513 die(_("--dry-run is incompatible with --interactive/--patch"));
514 if (pathspec_from_file
)
515 die(_("--pathspec-from-file is incompatible with --interactive/--patch"));
516 exit(interactive_add(argv
+ 1, prefix
, patch_interactive
));
518 if (legacy_stash_p
) {
519 struct pathspec pathspec
;
521 parse_pathspec(&pathspec
, 0,
522 PATHSPEC_PREFER_FULL
|
523 PATHSPEC_SYMLINK_LEADING_PATH
|
524 PATHSPEC_PREFIX_ORIGIN
,
527 return run_add_interactive(NULL
, "--patch=stash", &pathspec
);
530 if (edit_interactive
) {
531 if (pathspec_from_file
)
532 die(_("--pathspec-from-file is incompatible with --edit"));
533 return(edit_patch(argc
, argv
, prefix
));
538 if (0 <= addremove_explicit
)
539 addremove
= addremove_explicit
;
540 else if (take_worktree_changes
&& ADDREMOVE_DEFAULT
)
541 addremove
= 0; /* "-u" was given but not "-A" */
543 if (addremove
&& take_worktree_changes
)
544 die(_("-A and -u are mutually incompatible"));
546 if (!show_only
&& ignore_missing
)
547 die(_("Option --ignore-missing can only be used together with --dry-run"));
549 if (chmod_arg
&& ((chmod_arg
[0] != '-' && chmod_arg
[0] != '+') ||
550 chmod_arg
[1] != 'x' || chmod_arg
[2]))
551 die(_("--chmod param '%s' must be either -x or +x"), chmod_arg
);
553 add_new_files
= !take_worktree_changes
&& !refresh_only
&& !add_renormalize
;
554 require_pathspec
= !(take_worktree_changes
|| (0 < addremove_explicit
));
556 prepare_repo_settings(the_repository
);
557 the_repository
->settings
.command_requires_full_index
= 0;
559 hold_locked_index(&lock_file
, LOCK_DIE_ON_ERROR
);
562 * Check the "pathspec '%s' did not match any files" block
563 * below before enabling new magic.
565 parse_pathspec(&pathspec
, PATHSPEC_ATTR
,
566 PATHSPEC_PREFER_FULL
|
567 PATHSPEC_SYMLINK_LEADING_PATH
,
570 if (pathspec_from_file
) {
572 die(_("--pathspec-from-file is incompatible with pathspec arguments"));
574 parse_pathspec_file(&pathspec
, PATHSPEC_ATTR
,
575 PATHSPEC_PREFER_FULL
|
576 PATHSPEC_SYMLINK_LEADING_PATH
,
577 prefix
, pathspec_from_file
, pathspec_file_nul
);
578 } else if (pathspec_file_nul
) {
579 die(_("--pathspec-file-nul requires --pathspec-from-file"));
582 if (require_pathspec
&& pathspec
.nr
== 0) {
583 fprintf(stderr
, _("Nothing specified, nothing added.\n"));
584 if (advice_enabled(ADVICE_ADD_EMPTY_PATHSPEC
))
585 advise( _("Maybe you wanted to say 'git add .'?\n"
586 "Turn this message off by running\n"
587 "\"git config advice.addEmptyPathspec false\""));
591 if (!take_worktree_changes
&& addremove_explicit
< 0 && pathspec
.nr
)
592 /* Turn "git add pathspec..." to "git add -A pathspec..." */
595 flags
= ((verbose
? ADD_CACHE_VERBOSE
: 0) |
596 (show_only
? ADD_CACHE_PRETEND
: 0) |
597 (intent_to_add
? ADD_CACHE_INTENT
: 0) |
598 (ignore_add_errors
? ADD_CACHE_IGNORE_ERRORS
: 0) |
599 (!(addremove
|| take_worktree_changes
)
600 ? ADD_CACHE_IGNORE_REMOVAL
: 0));
602 if (read_cache_preload(&pathspec
) < 0)
603 die(_("index file corrupt"));
605 die_in_unpopulated_submodule(&the_index
, prefix
);
606 die_path_inside_submodule(&the_index
, &pathspec
);
611 /* Set up the default git porcelain excludes */
613 dir
.flags
|= DIR_COLLECT_IGNORED
;
614 setup_standard_excludes(&dir
);
617 /* This picks up the paths that are not tracked */
618 baselen
= fill_directory(&dir
, &the_index
, &pathspec
);
620 seen
= prune_directory(&dir
, &pathspec
, baselen
);
624 exit_status
|= refresh(verbose
, &pathspec
);
630 char *skip_worktree_seen
= NULL
;
631 struct string_list only_match_skip_worktree
= STRING_LIST_INIT_NODUP
;
634 seen
= find_pathspecs_matching_against_index(&pathspec
,
635 &the_index
, PS_IGNORE_SKIP_WORKTREE
);
638 * file_exists() assumes exact match
640 GUARD_PATHSPEC(&pathspec
,
647 for (i
= 0; i
< pathspec
.nr
; i
++) {
648 const char *path
= pathspec
.items
[i
].match
;
650 if (pathspec
.items
[i
].magic
& PATHSPEC_EXCLUDE
)
655 if (!include_sparse
&&
656 matches_skip_worktree(&pathspec
, i
, &skip_worktree_seen
)) {
657 string_list_append(&only_match_skip_worktree
,
658 pathspec
.items
[i
].original
);
662 /* Don't complain at 'git add .' on empty repo */
666 if ((pathspec
.items
[i
].magic
& (PATHSPEC_GLOB
| PATHSPEC_ICASE
)) ||
667 !file_exists(path
)) {
668 if (ignore_missing
) {
669 int dtype
= DT_UNKNOWN
;
670 if (is_excluded(&dir
, &the_index
, path
, &dtype
))
671 dir_add_ignored(&dir
, &the_index
,
672 path
, pathspec
.items
[i
].len
);
674 die(_("pathspec '%s' did not match any files"),
675 pathspec
.items
[i
].original
);
680 if (only_match_skip_worktree
.nr
) {
681 advise_on_updating_sparse_paths(&only_match_skip_worktree
);
686 free(skip_worktree_seen
);
687 string_list_clear(&only_match_skip_worktree
, 0);
693 exit_status
|= renormalize_tracked_files(&pathspec
, flags
);
695 exit_status
|= add_files_to_cache(prefix
, &pathspec
, flags
);
698 exit_status
|= add_files(&dir
, flags
);
700 if (chmod_arg
&& pathspec
.nr
)
701 exit_status
|= chmod_pathspec(&pathspec
, chmod_arg
[0], show_only
);
702 unplug_bulk_checkin();
705 if (write_locked_index(&the_index
, &lock_file
,
706 COMMIT_LOCK
| SKIP_IF_UNCHANGED
))
707 die(_("Unable to write new index file"));