2 * "git add" builtin command
4 * Copyright (C) 2006 Linus Torvalds
6 #define USE_THE_INDEX_VARIABLE
16 #include "cache-tree.h"
17 #include "run-command.h"
18 #include "parse-options.h"
20 #include "preload-index.h"
23 #include "read-cache.h"
24 #include "repository.h"
26 #include "bulk-checkin.h"
28 #include "submodule.h"
29 #include "add-interactive.h"
31 static const char * const builtin_add_usage
[] = {
32 N_("git add [<options>] [--] <pathspec>..."),
35 static int patch_interactive
, add_interactive
, edit_interactive
;
36 static int take_worktree_changes
;
37 static int add_renormalize
;
38 static int pathspec_file_nul
;
39 static int include_sparse
;
40 static const char *pathspec_from_file
;
42 static int chmod_pathspec(struct pathspec
*pathspec
, char flip
, int show_only
)
46 for (i
= 0; i
< the_index
.cache_nr
; i
++) {
47 struct cache_entry
*ce
= the_index
.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_index_entry(&the_index
, ce
, flip
);
61 err
= S_ISREG(ce
->ce_mode
) ? 0 : -1;
64 ret
= error(_("cannot chmod %cx '%s'"), flip
, ce
->name
);
70 static int renormalize_tracked_files(const struct pathspec
*pathspec
, int flags
)
74 for (i
= 0; i
< the_index
.cache_nr
; i
++) {
75 struct cache_entry
*ce
= the_index
.cache
[i
];
77 if (!include_sparse
&&
78 (ce_skip_worktree(ce
) ||
79 !path_in_sparse_checkout(ce
->name
, &the_index
)))
82 continue; /* do not touch unmerged paths */
83 if (!S_ISREG(ce
->ce_mode
) && !S_ISLNK(ce
->ce_mode
))
84 continue; /* do not touch non blobs */
85 if (pathspec
&& !ce_path_match(&the_index
, ce
, pathspec
, NULL
))
87 retval
|= add_file_to_index(&the_index
, ce
->name
,
88 flags
| ADD_CACHE_RENORMALIZE
);
94 static char *prune_directory(struct dir_struct
*dir
, struct pathspec
*pathspec
, int prefix
)
98 struct dir_entry
**src
, **dst
;
100 seen
= xcalloc(pathspec
->nr
, 1);
102 src
= dst
= dir
->entries
;
105 struct dir_entry
*entry
= *src
++;
106 if (dir_path_match(&the_index
, entry
, pathspec
, prefix
, seen
))
109 dir
->nr
= dst
- dir
->entries
;
110 add_pathspec_matches_against_index(pathspec
, &the_index
, seen
,
111 PS_IGNORE_SKIP_WORKTREE
);
115 static int refresh(int verbose
, const struct pathspec
*pathspec
)
119 char *skip_worktree_seen
= NULL
;
120 struct string_list only_match_skip_worktree
= STRING_LIST_INIT_NODUP
;
121 int flags
= REFRESH_IGNORE_SKIP_WORKTREE
|
122 (verbose
? REFRESH_IN_PORCELAIN
: REFRESH_QUIET
);
124 seen
= xcalloc(pathspec
->nr
, 1);
125 refresh_index(&the_index
, flags
, pathspec
, seen
,
126 _("Unstaged changes after refreshing the index:"));
127 for (i
= 0; i
< pathspec
->nr
; i
++) {
129 const char *path
= pathspec
->items
[i
].original
;
131 if (matches_skip_worktree(pathspec
, i
, &skip_worktree_seen
) ||
132 !path_in_sparse_checkout(path
, &the_index
)) {
133 string_list_append(&only_match_skip_worktree
,
134 pathspec
->items
[i
].original
);
136 die(_("pathspec '%s' did not match any files"),
137 pathspec
->items
[i
].original
);
142 if (only_match_skip_worktree
.nr
) {
143 advise_on_updating_sparse_paths(&only_match_skip_worktree
);
148 free(skip_worktree_seen
);
149 string_list_clear(&only_match_skip_worktree
, 0);
153 int interactive_add(const char **argv
, const char *prefix
, int patch
)
155 struct pathspec pathspec
;
158 if (!git_config_get_bool("add.interactive.usebuiltin", &unused
))
159 warning(_("the add.interactive.useBuiltin setting has been removed!\n"
160 "See its entry in 'git help config' for details."));
162 parse_pathspec(&pathspec
, 0,
163 PATHSPEC_PREFER_FULL
|
164 PATHSPEC_SYMLINK_LEADING_PATH
|
165 PATHSPEC_PREFIX_ORIGIN
,
169 return !!run_add_p(the_repository
, ADD_P_ADD
, NULL
, &pathspec
);
171 return !!run_add_i(the_repository
, &pathspec
);
174 static int edit_patch(int argc
, const char **argv
, const char *prefix
)
176 char *file
= git_pathdup("ADD_EDIT.patch");
177 struct child_process child
= CHILD_PROCESS_INIT
;
182 git_config(git_diff_basic_config
, NULL
); /* no "diff" UI options */
184 if (repo_read_index(the_repository
) < 0)
185 die(_("could not read the index"));
187 repo_init_revisions(the_repository
, &rev
, prefix
);
188 rev
.diffopt
.context
= 7;
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);
199 if (launch_editor(file
, NULL
, NULL
))
200 die(_("editing patch failed"));
203 die_errno(_("could not stat '%s'"), file
);
205 die(_("empty patch. aborted"));
208 strvec_pushl(&child
.args
, "apply", "--recount", "--cached", file
,
210 if (run_command(&child
))
211 die(_("could not apply '%s'"), file
);
215 release_revisions(&rev
);
219 static const char ignore_error
[] =
220 N_("The following paths are ignored by one of your .gitignore files:\n");
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;
226 #define ADDREMOVE_DEFAULT 1
227 static int addremove
= ADDREMOVE_DEFAULT
;
228 static int addremove_explicit
= -1; /* unspecified */
230 static char *chmod_arg
;
232 static int ignore_removal_cb(const struct option
*opt
, const char *arg
, int unset
)
236 /* if we are told to ignore, we are not adding removals */
237 *(int *)opt
->value
= !unset
? 0 : 1;
241 static struct option builtin_add_options
[] = {
242 OPT__DRY_RUN(&show_only
, N_("dry run")),
243 OPT__VERBOSE(&verbose
, N_("be verbose")),
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
),
270 static int add_config(const char *var
, const char *value
,
271 const struct config_context
*ctx
, void *cb
)
273 if (!strcmp(var
, "add.ignoreerrors") ||
274 !strcmp(var
, "add.ignore-errors")) {
275 ignore_add_errors
= git_config_bool(var
, value
);
279 if (git_color_config(var
, value
, cb
) < 0)
282 return git_default_config(var
, value
, ctx
, cb
);
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"
291 " git submodule add <url> %s\n"
293 "If you added this path by mistake, you can remove it from the\n"
296 " git rm --cached %s\n"
298 "See \"git help submodule\" for more information."
301 static void check_embedded_repo(const char *path
)
303 struct strbuf name
= STRBUF_INIT
;
304 static int adviced_on_embedded_repo
= 0;
306 if (!warn_on_embedded_repo
)
308 if (!ends_with(path
, "/"))
311 /* Drop trailing slash for aesthetics */
312 strbuf_addstr(&name
, path
);
313 strbuf_strip_suffix(&name
, "/");
315 warning(_("adding embedded git repository: %s"), name
.buf
);
316 if (!adviced_on_embedded_repo
&&
317 advice_enabled(ADVICE_ADD_EMBEDDED_REPO
)) {
318 advise(embedded_advice
, name
.buf
, name
.buf
);
319 adviced_on_embedded_repo
= 1;
322 strbuf_release(&name
);
325 static int add_files(struct dir_struct
*dir
, int flags
)
327 int i
, exit_status
= 0;
328 struct string_list matched_sparse_paths
= STRING_LIST_INIT_NODUP
;
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 if (advice_enabled(ADVICE_ADD_IGNORED_FILE
))
335 advise(_("Use -f if you really want to add them.\n"
336 "Turn this message off by running\n"
337 "\"git config advice.addIgnoredFile false\""));
341 for (i
= 0; i
< dir
->nr
; i
++) {
342 if (!include_sparse
&&
343 !path_in_sparse_checkout(dir
->entries
[i
]->name
, &the_index
)) {
344 string_list_append(&matched_sparse_paths
,
345 dir
->entries
[i
]->name
);
348 if (add_file_to_index(&the_index
, dir
->entries
[i
]->name
, flags
)) {
349 if (!ignore_add_errors
)
350 die(_("adding files failed"));
353 check_embedded_repo(dir
->entries
[i
]->name
);
357 if (matched_sparse_paths
.nr
) {
358 advise_on_updating_sparse_paths(&matched_sparse_paths
);
362 string_list_clear(&matched_sparse_paths
, 0);
367 int cmd_add(int argc
, const char **argv
, const char *prefix
)
370 struct pathspec pathspec
;
371 struct dir_struct dir
= DIR_INIT
;
374 int require_pathspec
;
376 struct lock_file lock_file
= LOCK_INIT
;
378 git_config(add_config
, NULL
);
380 argc
= parse_options(argc
, argv
, prefix
, builtin_add_options
,
381 builtin_add_usage
, PARSE_OPT_KEEP_ARGV0
);
382 if (patch_interactive
)
384 if (add_interactive
) {
386 die(_("options '%s' and '%s' cannot be used together"), "--dry-run", "--interactive/--patch");
387 if (pathspec_from_file
)
388 die(_("options '%s' and '%s' cannot be used together"), "--pathspec-from-file", "--interactive/--patch");
389 exit(interactive_add(argv
+ 1, prefix
, patch_interactive
));
392 if (edit_interactive
) {
393 if (pathspec_from_file
)
394 die(_("options '%s' and '%s' cannot be used together"), "--pathspec-from-file", "--edit");
395 return(edit_patch(argc
, argv
, prefix
));
400 if (0 <= addremove_explicit
)
401 addremove
= addremove_explicit
;
402 else if (take_worktree_changes
&& ADDREMOVE_DEFAULT
)
403 addremove
= 0; /* "-u" was given but not "-A" */
405 if (addremove
&& take_worktree_changes
)
406 die(_("options '%s' and '%s' cannot be used together"), "-A", "-u");
408 if (!show_only
&& ignore_missing
)
409 die(_("the option '%s' requires '%s'"), "--ignore-missing", "--dry-run");
411 if (chmod_arg
&& ((chmod_arg
[0] != '-' && chmod_arg
[0] != '+') ||
412 chmod_arg
[1] != 'x' || chmod_arg
[2]))
413 die(_("--chmod param '%s' must be either -x or +x"), chmod_arg
);
415 add_new_files
= !take_worktree_changes
&& !refresh_only
&& !add_renormalize
;
416 require_pathspec
= !(take_worktree_changes
|| (0 < addremove_explicit
));
418 prepare_repo_settings(the_repository
);
419 the_repository
->settings
.command_requires_full_index
= 0;
421 repo_hold_locked_index(the_repository
, &lock_file
, LOCK_DIE_ON_ERROR
);
424 * Check the "pathspec '%s' did not match any files" block
425 * below before enabling new magic.
427 parse_pathspec(&pathspec
, PATHSPEC_ATTR
,
428 PATHSPEC_PREFER_FULL
|
429 PATHSPEC_SYMLINK_LEADING_PATH
,
432 if (pathspec_from_file
) {
434 die(_("'%s' and pathspec arguments cannot be used together"), "--pathspec-from-file");
436 parse_pathspec_file(&pathspec
, PATHSPEC_ATTR
,
437 PATHSPEC_PREFER_FULL
|
438 PATHSPEC_SYMLINK_LEADING_PATH
,
439 prefix
, pathspec_from_file
, pathspec_file_nul
);
440 } else if (pathspec_file_nul
) {
441 die(_("the option '%s' requires '%s'"), "--pathspec-file-nul", "--pathspec-from-file");
444 if (require_pathspec
&& pathspec
.nr
== 0) {
445 fprintf(stderr
, _("Nothing specified, nothing added.\n"));
446 if (advice_enabled(ADVICE_ADD_EMPTY_PATHSPEC
))
447 advise( _("Maybe you wanted to say 'git add .'?\n"
448 "Turn this message off by running\n"
449 "\"git config advice.addEmptyPathspec false\""));
453 if (!take_worktree_changes
&& addremove_explicit
< 0 && pathspec
.nr
)
454 /* Turn "git add pathspec..." to "git add -A pathspec..." */
457 flags
= ((verbose
? ADD_CACHE_VERBOSE
: 0) |
458 (show_only
? ADD_CACHE_PRETEND
: 0) |
459 (intent_to_add
? ADD_CACHE_INTENT
: 0) |
460 (ignore_add_errors
? ADD_CACHE_IGNORE_ERRORS
: 0) |
461 (!(addremove
|| take_worktree_changes
)
462 ? ADD_CACHE_IGNORE_REMOVAL
: 0));
464 if (repo_read_index_preload(the_repository
, &pathspec
, 0) < 0)
465 die(_("index file corrupt"));
467 die_in_unpopulated_submodule(&the_index
, prefix
);
468 die_path_inside_submodule(&the_index
, &pathspec
);
473 /* Set up the default git porcelain excludes */
475 dir
.flags
|= DIR_COLLECT_IGNORED
;
476 setup_standard_excludes(&dir
);
479 /* This picks up the paths that are not tracked */
480 baselen
= fill_directory(&dir
, &the_index
, &pathspec
);
482 seen
= prune_directory(&dir
, &pathspec
, baselen
);
486 exit_status
|= refresh(verbose
, &pathspec
);
492 char *skip_worktree_seen
= NULL
;
493 struct string_list only_match_skip_worktree
= STRING_LIST_INIT_NODUP
;
496 seen
= find_pathspecs_matching_against_index(&pathspec
,
497 &the_index
, PS_IGNORE_SKIP_WORKTREE
);
500 * file_exists() assumes exact match
502 GUARD_PATHSPEC(&pathspec
,
509 for (i
= 0; i
< pathspec
.nr
; i
++) {
510 const char *path
= pathspec
.items
[i
].match
;
512 if (pathspec
.items
[i
].magic
& PATHSPEC_EXCLUDE
)
517 if (!include_sparse
&&
518 matches_skip_worktree(&pathspec
, i
, &skip_worktree_seen
)) {
519 string_list_append(&only_match_skip_worktree
,
520 pathspec
.items
[i
].original
);
524 /* Don't complain at 'git add .' on empty repo */
528 if ((pathspec
.items
[i
].magic
& (PATHSPEC_GLOB
| PATHSPEC_ICASE
)) ||
529 !file_exists(path
)) {
530 if (ignore_missing
) {
531 int dtype
= DT_UNKNOWN
;
532 if (is_excluded(&dir
, &the_index
, path
, &dtype
))
533 dir_add_ignored(&dir
, &the_index
,
534 path
, pathspec
.items
[i
].len
);
536 die(_("pathspec '%s' did not match any files"),
537 pathspec
.items
[i
].original
);
542 if (only_match_skip_worktree
.nr
) {
543 advise_on_updating_sparse_paths(&only_match_skip_worktree
);
548 free(skip_worktree_seen
);
549 string_list_clear(&only_match_skip_worktree
, 0);
552 begin_odb_transaction();
555 exit_status
|= renormalize_tracked_files(&pathspec
, flags
);
557 exit_status
|= add_files_to_cache(the_repository
, prefix
,
558 &pathspec
, include_sparse
,
562 exit_status
|= add_files(&dir
, flags
);
564 if (chmod_arg
&& pathspec
.nr
)
565 exit_status
|= chmod_pathspec(&pathspec
, chmod_arg
[0], show_only
);
566 end_odb_transaction();
569 if (write_locked_index(&the_index
, &lock_file
,
570 COMMIT_LOCK
| SKIP_IF_UNCHANGED
))
571 die(_("unable to write new index file"));
574 clear_pathspec(&pathspec
);