2 * "git add" builtin command
4 * Copyright (C) 2006 Linus Torvalds
15 #include "run-command.h"
16 #include "parse-options.h"
18 #include "preload-index.h"
20 #include "read-cache.h"
21 #include "repository.h"
23 #include "bulk-checkin.h"
25 #include "submodule.h"
26 #include "add-interactive.h"
28 static const char * const builtin_add_usage
[] = {
29 N_("git add [<options>] [--] <pathspec>..."),
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
;
39 static int chmod_pathspec(struct pathspec
*pathspec
, char flip
, int show_only
)
43 for (i
= 0; i
< the_repository
->index
->cache_nr
; i
++) {
44 struct cache_entry
*ce
= the_repository
->index
->cache
[i
];
47 if (!include_sparse
&&
48 (ce_skip_worktree(ce
) ||
49 !path_in_sparse_checkout(ce
->name
, the_repository
->index
)))
52 if (pathspec
&& !ce_path_match(the_repository
->index
, ce
, pathspec
, NULL
))
56 err
= chmod_index_entry(the_repository
->index
, ce
, flip
);
58 err
= S_ISREG(ce
->ce_mode
) ? 0 : -1;
61 ret
= error(_("cannot chmod %cx '%s'"), flip
, ce
->name
);
67 static int renormalize_tracked_files(const struct pathspec
*pathspec
, int flags
)
71 for (i
= 0; i
< the_repository
->index
->cache_nr
; i
++) {
72 struct cache_entry
*ce
= the_repository
->index
->cache
[i
];
74 if (!include_sparse
&&
75 (ce_skip_worktree(ce
) ||
76 !path_in_sparse_checkout(ce
->name
, the_repository
->index
)))
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
))
84 retval
|= add_file_to_index(the_repository
->index
, ce
->name
,
85 flags
| ADD_CACHE_RENORMALIZE
);
91 static char *prune_directory(struct dir_struct
*dir
, struct pathspec
*pathspec
, int prefix
)
95 struct dir_entry
**src
, **dst
;
97 seen
= xcalloc(pathspec
->nr
, 1);
99 src
= dst
= dir
->entries
;
102 struct dir_entry
*entry
= *src
++;
103 if (dir_path_match(the_repository
->index
, entry
, pathspec
, prefix
, seen
))
106 dir
->nr
= dst
- dir
->entries
;
107 add_pathspec_matches_against_index(pathspec
, the_repository
->index
, seen
,
108 PS_IGNORE_SKIP_WORKTREE
);
112 static int refresh(int verbose
, const struct pathspec
*pathspec
)
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
);
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
++) {
126 const char *path
= pathspec
->items
[i
].original
;
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
);
133 die(_("pathspec '%s' did not match any files"),
134 pathspec
->items
[i
].original
);
139 if (only_match_skip_worktree
.nr
) {
140 advise_on_updating_sparse_paths(&only_match_skip_worktree
);
145 free(skip_worktree_seen
);
146 string_list_clear(&only_match_skip_worktree
, 0);
150 int interactive_add(const char **argv
, const char *prefix
, int patch
)
152 struct pathspec pathspec
;
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."));
159 parse_pathspec(&pathspec
, 0,
160 PATHSPEC_PREFER_FULL
|
161 PATHSPEC_SYMLINK_LEADING_PATH
|
162 PATHSPEC_PREFIX_ORIGIN
,
166 return !!run_add_p(the_repository
, ADD_P_ADD
, NULL
, &pathspec
);
168 return !!run_add_i(the_repository
, &pathspec
);
171 static int edit_patch(int argc
, const char **argv
, const char *prefix
)
173 char *file
= git_pathdup("ADD_EDIT.patch");
174 struct child_process child
= CHILD_PROCESS_INIT
;
179 git_config(git_diff_basic_config
, NULL
); /* no "diff" UI options */
181 if (repo_read_index(the_repository
) < 0)
182 die(_("could not read the index"));
184 repo_init_revisions(the_repository
, &rev
, prefix
);
185 rev
.diffopt
.context
= 7;
187 argc
= setup_revisions(argc
, argv
, &rev
, NULL
);
188 rev
.diffopt
.output_format
= DIFF_FORMAT_PATCH
;
189 rev
.diffopt
.use_color
= 0;
190 rev
.diffopt
.flags
.ignore_dirty_submodules
= 1;
191 out
= xopen(file
, O_CREAT
| O_WRONLY
| O_TRUNC
, 0666);
192 rev
.diffopt
.file
= xfdopen(out
, "w");
193 rev
.diffopt
.close_file
= 1;
194 run_diff_files(&rev
, 0);
196 if (launch_editor(file
, NULL
, NULL
))
197 die(_("editing patch failed"));
200 die_errno(_("could not stat '%s'"), file
);
202 die(_("empty patch. aborted"));
205 strvec_pushl(&child
.args
, "apply", "--recount", "--cached", file
,
207 if (run_command(&child
))
208 die(_("could not apply '%s'"), file
);
212 release_revisions(&rev
);
216 static const char ignore_error
[] =
217 N_("The following paths are ignored by one of your .gitignore files:\n");
219 static int verbose
, show_only
, ignored_too
, refresh_only
;
220 static int ignore_add_errors
, intent_to_add
, ignore_missing
;
221 static int warn_on_embedded_repo
= 1;
223 #define ADDREMOVE_DEFAULT 1
224 static int addremove
= ADDREMOVE_DEFAULT
;
225 static int addremove_explicit
= -1; /* unspecified */
227 static char *chmod_arg
;
229 static int ignore_removal_cb(const struct option
*opt
, const char *arg
, int unset
)
233 /* if we are told to ignore, we are not adding removals */
234 *(int *)opt
->value
= !unset
? 0 : 1;
238 static struct option builtin_add_options
[] = {
239 OPT__DRY_RUN(&show_only
, N_("dry run")),
240 OPT__VERBOSE(&verbose
, N_("be verbose")),
242 OPT_BOOL('i', "interactive", &add_interactive
, N_("interactive picking")),
243 OPT_BOOL('p', "patch", &patch_interactive
, N_("select hunks interactively")),
244 OPT_BOOL('e', "edit", &edit_interactive
, N_("edit current diff and apply")),
245 OPT__FORCE(&ignored_too
, N_("allow adding otherwise ignored files"), 0),
246 OPT_BOOL('u', "update", &take_worktree_changes
, N_("update tracked files")),
247 OPT_BOOL(0, "renormalize", &add_renormalize
, N_("renormalize EOL of tracked files (implies -u)")),
248 OPT_BOOL('N', "intent-to-add", &intent_to_add
, N_("record only the fact that the path will be added later")),
249 OPT_BOOL('A', "all", &addremove_explicit
, N_("add changes from all tracked and untracked files")),
250 OPT_CALLBACK_F(0, "ignore-removal", &addremove_explicit
,
251 NULL
/* takes no arguments */,
252 N_("ignore paths removed in the working tree (same as --no-all)"),
253 PARSE_OPT_NOARG
, ignore_removal_cb
),
254 OPT_BOOL( 0 , "refresh", &refresh_only
, N_("don't add, only refresh the index")),
255 OPT_BOOL( 0 , "ignore-errors", &ignore_add_errors
, N_("just skip files which cannot be added because of errors")),
256 OPT_BOOL( 0 , "ignore-missing", &ignore_missing
, N_("check if - even missing - files are ignored in dry run")),
257 OPT_BOOL(0, "sparse", &include_sparse
, N_("allow updating entries outside of the sparse-checkout cone")),
258 OPT_STRING(0, "chmod", &chmod_arg
, "(+|-)x",
259 N_("override the executable bit of the listed files")),
260 OPT_HIDDEN_BOOL(0, "warn-embedded-repo", &warn_on_embedded_repo
,
261 N_("warn when adding an embedded repository")),
262 OPT_PATHSPEC_FROM_FILE(&pathspec_from_file
),
263 OPT_PATHSPEC_FILE_NUL(&pathspec_file_nul
),
267 static int add_config(const char *var
, const char *value
,
268 const struct config_context
*ctx
, void *cb
)
270 if (!strcmp(var
, "add.ignoreerrors") ||
271 !strcmp(var
, "add.ignore-errors")) {
272 ignore_add_errors
= git_config_bool(var
, value
);
276 if (git_color_config(var
, value
, cb
) < 0)
279 return git_default_config(var
, value
, ctx
, cb
);
282 static const char embedded_advice
[] = N_(
283 "You've added another git repository inside your current repository.\n"
284 "Clones of the outer repository will not contain the contents of\n"
285 "the embedded repository and will not know how to obtain it.\n"
286 "If you meant to add a submodule, use:\n"
288 " git submodule add <url> %s\n"
290 "If you added this path by mistake, you can remove it from the\n"
293 " git rm --cached %s\n"
295 "See \"git help submodule\" for more information."
298 static void check_embedded_repo(const char *path
)
300 struct strbuf name
= STRBUF_INIT
;
301 static int adviced_on_embedded_repo
= 0;
303 if (!warn_on_embedded_repo
)
305 if (!ends_with(path
, "/"))
308 /* Drop trailing slash for aesthetics */
309 strbuf_addstr(&name
, path
);
310 strbuf_strip_suffix(&name
, "/");
312 warning(_("adding embedded git repository: %s"), name
.buf
);
313 if (!adviced_on_embedded_repo
) {
314 advise_if_enabled(ADVICE_ADD_EMBEDDED_REPO
,
315 embedded_advice
, name
.buf
, name
.buf
);
316 adviced_on_embedded_repo
= 1;
319 strbuf_release(&name
);
322 static int add_files(struct dir_struct
*dir
, int flags
)
324 int i
, exit_status
= 0;
325 struct string_list matched_sparse_paths
= STRING_LIST_INIT_NODUP
;
327 if (dir
->ignored_nr
) {
328 fprintf(stderr
, _(ignore_error
));
329 for (i
= 0; i
< dir
->ignored_nr
; i
++)
330 fprintf(stderr
, "%s\n", dir
->ignored
[i
]->name
);
331 advise_if_enabled(ADVICE_ADD_IGNORED_FILE
,
332 _("Use -f if you really want to add them."));
336 for (i
= 0; i
< dir
->nr
; i
++) {
337 if (!include_sparse
&&
338 !path_in_sparse_checkout(dir
->entries
[i
]->name
, the_repository
->index
)) {
339 string_list_append(&matched_sparse_paths
,
340 dir
->entries
[i
]->name
);
343 if (add_file_to_index(the_repository
->index
, dir
->entries
[i
]->name
, flags
)) {
344 if (!ignore_add_errors
)
345 die(_("adding files failed"));
348 check_embedded_repo(dir
->entries
[i
]->name
);
352 if (matched_sparse_paths
.nr
) {
353 advise_on_updating_sparse_paths(&matched_sparse_paths
);
357 string_list_clear(&matched_sparse_paths
, 0);
362 int cmd_add(int argc
, const char **argv
, const char *prefix
)
365 struct pathspec pathspec
;
366 struct dir_struct dir
= DIR_INIT
;
369 int require_pathspec
;
371 char *ps_matched
= NULL
;
372 struct lock_file lock_file
= LOCK_INIT
;
374 git_config(add_config
, NULL
);
376 argc
= parse_options(argc
, argv
, prefix
, builtin_add_options
,
377 builtin_add_usage
, PARSE_OPT_KEEP_ARGV0
);
378 if (patch_interactive
)
380 if (add_interactive
) {
382 die(_("options '%s' and '%s' cannot be used together"), "--dry-run", "--interactive/--patch");
383 if (pathspec_from_file
)
384 die(_("options '%s' and '%s' cannot be used together"), "--pathspec-from-file", "--interactive/--patch");
385 exit(interactive_add(argv
+ 1, prefix
, patch_interactive
));
388 if (edit_interactive
) {
389 if (pathspec_from_file
)
390 die(_("options '%s' and '%s' cannot be used together"), "--pathspec-from-file", "--edit");
391 return(edit_patch(argc
, argv
, prefix
));
396 if (0 <= addremove_explicit
)
397 addremove
= addremove_explicit
;
398 else if (take_worktree_changes
&& ADDREMOVE_DEFAULT
)
399 addremove
= 0; /* "-u" was given but not "-A" */
401 if (addremove
&& take_worktree_changes
)
402 die(_("options '%s' and '%s' cannot be used together"), "-A", "-u");
404 if (!show_only
&& ignore_missing
)
405 die(_("the option '%s' requires '%s'"), "--ignore-missing", "--dry-run");
407 if (chmod_arg
&& ((chmod_arg
[0] != '-' && chmod_arg
[0] != '+') ||
408 chmod_arg
[1] != 'x' || chmod_arg
[2]))
409 die(_("--chmod param '%s' must be either -x or +x"), chmod_arg
);
411 add_new_files
= !take_worktree_changes
&& !refresh_only
&& !add_renormalize
;
412 require_pathspec
= !(take_worktree_changes
|| (0 < addremove_explicit
));
414 prepare_repo_settings(the_repository
);
415 the_repository
->settings
.command_requires_full_index
= 0;
417 repo_hold_locked_index(the_repository
, &lock_file
, LOCK_DIE_ON_ERROR
);
420 * Check the "pathspec '%s' did not match any files" block
421 * below before enabling new magic.
423 parse_pathspec(&pathspec
, 0,
424 PATHSPEC_PREFER_FULL
|
425 PATHSPEC_SYMLINK_LEADING_PATH
,
428 if (pathspec_from_file
) {
430 die(_("'%s' and pathspec arguments cannot be used together"), "--pathspec-from-file");
432 parse_pathspec_file(&pathspec
, 0,
433 PATHSPEC_PREFER_FULL
|
434 PATHSPEC_SYMLINK_LEADING_PATH
,
435 prefix
, pathspec_from_file
, pathspec_file_nul
);
436 } else if (pathspec_file_nul
) {
437 die(_("the option '%s' requires '%s'"), "--pathspec-file-nul", "--pathspec-from-file");
440 if (require_pathspec
&& pathspec
.nr
== 0) {
441 fprintf(stderr
, _("Nothing specified, nothing added.\n"));
442 advise_if_enabled(ADVICE_ADD_EMPTY_PATHSPEC
,
443 _("Maybe you wanted to say 'git add .'?"));
447 if (!take_worktree_changes
&& addremove_explicit
< 0 && pathspec
.nr
)
448 /* Turn "git add pathspec..." to "git add -A pathspec..." */
451 flags
= ((verbose
? ADD_CACHE_VERBOSE
: 0) |
452 (show_only
? ADD_CACHE_PRETEND
: 0) |
453 (intent_to_add
? ADD_CACHE_INTENT
: 0) |
454 (ignore_add_errors
? ADD_CACHE_IGNORE_ERRORS
: 0) |
455 (!(addremove
|| take_worktree_changes
)
456 ? ADD_CACHE_IGNORE_REMOVAL
: 0));
458 if (repo_read_index_preload(the_repository
, &pathspec
, 0) < 0)
459 die(_("index file corrupt"));
461 die_in_unpopulated_submodule(the_repository
->index
, prefix
);
462 die_path_inside_submodule(the_repository
->index
, &pathspec
);
467 /* Set up the default git porcelain excludes */
469 dir
.flags
|= DIR_COLLECT_IGNORED
;
470 setup_standard_excludes(&dir
);
473 /* This picks up the paths that are not tracked */
474 baselen
= fill_directory(&dir
, the_repository
->index
, &pathspec
);
476 seen
= prune_directory(&dir
, &pathspec
, baselen
);
480 exit_status
|= refresh(verbose
, &pathspec
);
486 char *skip_worktree_seen
= NULL
;
487 struct string_list only_match_skip_worktree
= STRING_LIST_INIT_NODUP
;
490 seen
= find_pathspecs_matching_against_index(&pathspec
,
491 the_repository
->index
, PS_IGNORE_SKIP_WORKTREE
);
494 * file_exists() assumes exact match
496 GUARD_PATHSPEC(&pathspec
,
504 for (i
= 0; i
< pathspec
.nr
; i
++) {
505 const char *path
= pathspec
.items
[i
].match
;
507 if (pathspec
.items
[i
].magic
& PATHSPEC_EXCLUDE
)
512 if (!include_sparse
&&
513 matches_skip_worktree(&pathspec
, i
, &skip_worktree_seen
)) {
514 string_list_append(&only_match_skip_worktree
,
515 pathspec
.items
[i
].original
);
519 /* Don't complain at 'git add .' on empty repo */
523 if ((pathspec
.items
[i
].magic
& (PATHSPEC_GLOB
| PATHSPEC_ICASE
)) ||
524 !file_exists(path
)) {
525 if (ignore_missing
) {
526 int dtype
= DT_UNKNOWN
;
527 if (is_excluded(&dir
, the_repository
->index
, path
, &dtype
))
528 dir_add_ignored(&dir
, the_repository
->index
,
529 path
, pathspec
.items
[i
].len
);
531 die(_("pathspec '%s' did not match any files"),
532 pathspec
.items
[i
].original
);
537 if (only_match_skip_worktree
.nr
) {
538 advise_on_updating_sparse_paths(&only_match_skip_worktree
);
543 free(skip_worktree_seen
);
544 string_list_clear(&only_match_skip_worktree
, 0);
547 begin_odb_transaction();
549 ps_matched
= xcalloc(pathspec
.nr
, 1);
551 exit_status
|= renormalize_tracked_files(&pathspec
, flags
);
553 exit_status
|= add_files_to_cache(the_repository
, prefix
,
554 &pathspec
, ps_matched
,
555 include_sparse
, flags
);
557 if (take_worktree_changes
&& !add_renormalize
&& !ignore_add_errors
&&
558 report_path_error(ps_matched
, &pathspec
))
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_repository
->index
, &lock_file
,
570 COMMIT_LOCK
| SKIP_IF_UNCHANGED
))
571 die(_("unable to write new index file"));
575 clear_pathspec(&pathspec
);