2 * "git add" builtin command
4 * Copyright (C) 2006 Linus Torvalds
11 #include "cache-tree.h"
12 #include "run-command.h"
13 #include "parse-options.h"
17 #include "bulk-checkin.h"
19 static const char * const builtin_add_usage
[] = {
20 N_("git add [options] [--] <pathspec>..."),
23 static int patch_interactive
, add_interactive
, edit_interactive
;
24 static int take_worktree_changes
;
26 struct update_callback_data
{
31 static int fix_unmerged_status(struct diff_filepair
*p
,
32 struct update_callback_data
*data
)
34 if (p
->status
!= DIFF_STATUS_UNMERGED
)
36 if (!(data
->flags
& ADD_CACHE_IGNORE_REMOVAL
) && !p
->two
->mode
)
38 * This is not an explicit add request, and the
39 * path is missing from the working tree (deleted)
41 return DIFF_STATUS_DELETED
;
44 * Either an explicit add request, or path exists
45 * in the working tree. An attempt to explicitly
46 * add a path that does not exist in the working tree
47 * will be caught as an error by the caller immediately.
49 return DIFF_STATUS_MODIFIED
;
52 static void update_callback(struct diff_queue_struct
*q
,
53 struct diff_options
*opt
, void *cbdata
)
56 struct update_callback_data
*data
= cbdata
;
58 for (i
= 0; i
< q
->nr
; i
++) {
59 struct diff_filepair
*p
= q
->queue
[i
];
60 const char *path
= p
->one
->path
;
61 switch (fix_unmerged_status(p
, data
)) {
63 die(_("unexpected diff status %c"), p
->status
);
64 case DIFF_STATUS_MODIFIED
:
65 case DIFF_STATUS_TYPE_CHANGED
:
66 if (add_file_to_index(&the_index
, path
, data
->flags
)) {
67 if (!(data
->flags
& ADD_CACHE_IGNORE_ERRORS
))
68 die(_("updating files failed"));
72 case DIFF_STATUS_DELETED
:
73 if (data
->flags
& ADD_CACHE_IGNORE_REMOVAL
)
75 if (!(data
->flags
& ADD_CACHE_PRETEND
))
76 remove_file_from_index(&the_index
, path
);
77 if (data
->flags
& (ADD_CACHE_PRETEND
|ADD_CACHE_VERBOSE
))
78 printf(_("remove '%s'\n"), path
);
84 int add_files_to_cache(const char *prefix
,
85 const struct pathspec
*pathspec
, int flags
)
87 struct update_callback_data data
;
90 memset(&data
, 0, sizeof(data
));
93 init_revisions(&rev
, prefix
);
94 setup_revisions(0, NULL
, &rev
, NULL
);
96 copy_pathspec(&rev
.prune_data
, pathspec
);
97 rev
.diffopt
.output_format
= DIFF_FORMAT_CALLBACK
;
98 rev
.diffopt
.format_callback
= update_callback
;
99 rev
.diffopt
.format_callback_data
= &data
;
100 rev
.max_count
= 0; /* do not compare unmerged paths with stage #2 */
101 run_diff_files(&rev
, DIFF_RACY_IS_MODIFIED
);
102 return !!data
.add_errors
;
105 static char *prune_directory(struct dir_struct
*dir
, struct pathspec
*pathspec
, int prefix
)
109 struct dir_entry
**src
, **dst
;
111 seen
= xcalloc(pathspec
->nr
, 1);
113 src
= dst
= dir
->entries
;
116 struct dir_entry
*entry
= *src
++;
117 if (dir_path_match(entry
, pathspec
, prefix
, seen
))
120 dir
->nr
= dst
- dir
->entries
;
121 add_pathspec_matches_against_index(pathspec
, seen
);
125 static void refresh(int verbose
, const struct pathspec
*pathspec
)
130 seen
= xcalloc(pathspec
->nr
, 1);
131 refresh_index(&the_index
, verbose
? REFRESH_IN_PORCELAIN
: REFRESH_QUIET
,
132 pathspec
, seen
, _("Unstaged changes after refreshing the index:"));
133 for (i
= 0; i
< pathspec
->nr
; i
++) {
135 die(_("pathspec '%s' did not match any files"),
136 pathspec
->items
[i
].match
);
141 int run_add_interactive(const char *revision
, const char *patch_mode
,
142 const struct pathspec
*pathspec
)
147 args
= xcalloc(sizeof(const char *), (pathspec
->nr
+ 6));
149 args
[ac
++] = "add--interactive";
151 args
[ac
++] = patch_mode
;
153 args
[ac
++] = revision
;
155 for (i
= 0; i
< pathspec
->nr
; i
++)
156 /* pass original pathspec, to be re-parsed */
157 args
[ac
++] = pathspec
->items
[i
].original
;
159 status
= run_command_v_opt(args
, RUN_GIT_CMD
);
164 int interactive_add(int argc
, const char **argv
, const char *prefix
, int patch
)
166 struct pathspec pathspec
;
168 parse_pathspec(&pathspec
, 0,
169 PATHSPEC_PREFER_FULL
|
170 PATHSPEC_SYMLINK_LEADING_PATH
|
171 PATHSPEC_PREFIX_ORIGIN
,
174 return run_add_interactive(NULL
,
175 patch
? "--patch" : NULL
,
179 static int edit_patch(int argc
, const char **argv
, const char *prefix
)
181 char *file
= git_pathdup("ADD_EDIT.patch");
182 const char *apply_argv
[] = { "apply", "--recount", "--cached",
184 struct child_process child
;
189 apply_argv
[3] = file
;
191 git_config(git_diff_basic_config
, NULL
); /* no "diff" UI options */
193 if (read_cache() < 0)
194 die(_("Could not read the index"));
196 init_revisions(&rev
, prefix
);
197 rev
.diffopt
.context
= 7;
199 argc
= setup_revisions(argc
, argv
, &rev
, NULL
);
200 rev
.diffopt
.output_format
= DIFF_FORMAT_PATCH
;
201 rev
.diffopt
.use_color
= 0;
202 DIFF_OPT_SET(&rev
.diffopt
, IGNORE_DIRTY_SUBMODULES
);
203 out
= open(file
, O_CREAT
| O_WRONLY
, 0666);
205 die(_("Could not open '%s' for writing."), file
);
206 rev
.diffopt
.file
= xfdopen(out
, "w");
207 rev
.diffopt
.close_file
= 1;
208 if (run_diff_files(&rev
, 0))
209 die(_("Could not write patch"));
211 launch_editor(file
, NULL
, NULL
);
214 die_errno(_("Could not stat '%s'"), file
);
216 die(_("Empty patch. Aborted."));
218 memset(&child
, 0, sizeof(child
));
220 child
.argv
= apply_argv
;
221 if (run_command(&child
))
222 die(_("Could not apply '%s'"), file
);
229 static struct lock_file lock_file
;
231 static const char ignore_error
[] =
232 N_("The following paths are ignored by one of your .gitignore files:\n");
234 static int verbose
, show_only
, ignored_too
, refresh_only
;
235 static int ignore_add_errors
, intent_to_add
, ignore_missing
;
237 #define ADDREMOVE_DEFAULT 1
238 static int addremove
= ADDREMOVE_DEFAULT
;
239 static int addremove_explicit
= -1; /* unspecified */
241 static int ignore_removal_cb(const struct option
*opt
, const char *arg
, int unset
)
243 /* if we are told to ignore, we are not adding removals */
244 *(int *)opt
->value
= !unset
? 0 : 1;
248 static struct option builtin_add_options
[] = {
249 OPT__DRY_RUN(&show_only
, N_("dry run")),
250 OPT__VERBOSE(&verbose
, N_("be verbose")),
252 OPT_BOOL('i', "interactive", &add_interactive
, N_("interactive picking")),
253 OPT_BOOL('p', "patch", &patch_interactive
, N_("select hunks interactively")),
254 OPT_BOOL('e', "edit", &edit_interactive
, N_("edit current diff and apply")),
255 OPT__FORCE(&ignored_too
, N_("allow adding otherwise ignored files")),
256 OPT_BOOL('u', "update", &take_worktree_changes
, N_("update tracked files")),
257 OPT_BOOL('N', "intent-to-add", &intent_to_add
, N_("record only the fact that the path will be added later")),
258 OPT_BOOL('A', "all", &addremove_explicit
, N_("add changes from all tracked and untracked files")),
259 { OPTION_CALLBACK
, 0, "ignore-removal", &addremove_explicit
,
260 NULL
/* takes no arguments */,
261 N_("ignore paths removed in the working tree (same as --no-all)"),
262 PARSE_OPT_NOARG
, ignore_removal_cb
},
263 OPT_BOOL( 0 , "refresh", &refresh_only
, N_("don't add, only refresh the index")),
264 OPT_BOOL( 0 , "ignore-errors", &ignore_add_errors
, N_("just skip files which cannot be added because of errors")),
265 OPT_BOOL( 0 , "ignore-missing", &ignore_missing
, N_("check if - even missing - files are ignored in dry run")),
269 static int add_config(const char *var
, const char *value
, void *cb
)
271 if (!strcmp(var
, "add.ignoreerrors") ||
272 !strcmp(var
, "add.ignore-errors")) {
273 ignore_add_errors
= git_config_bool(var
, value
);
276 return git_default_config(var
, value
, cb
);
279 static int add_files(struct dir_struct
*dir
, int flags
)
281 int i
, exit_status
= 0;
283 if (dir
->ignored_nr
) {
284 fprintf(stderr
, _(ignore_error
));
285 for (i
= 0; i
< dir
->ignored_nr
; i
++)
286 fprintf(stderr
, "%s\n", dir
->ignored
[i
]->name
);
287 fprintf(stderr
, _("Use -f if you really want to add them.\n"));
288 die(_("no files added"));
291 for (i
= 0; i
< dir
->nr
; i
++)
292 if (add_file_to_cache(dir
->entries
[i
]->name
, flags
)) {
293 if (!ignore_add_errors
)
294 die(_("adding files failed"));
300 int cmd_add(int argc
, const char **argv
, const char *prefix
)
304 struct pathspec pathspec
;
305 struct dir_struct dir
;
308 int require_pathspec
;
311 git_config(add_config
, NULL
);
313 argc
= parse_options(argc
, argv
, prefix
, builtin_add_options
,
314 builtin_add_usage
, PARSE_OPT_KEEP_ARGV0
);
315 if (patch_interactive
)
318 exit(interactive_add(argc
- 1, argv
+ 1, prefix
, patch_interactive
));
320 if (edit_interactive
)
321 return(edit_patch(argc
, argv
, prefix
));
325 if (0 <= addremove_explicit
)
326 addremove
= addremove_explicit
;
327 else if (take_worktree_changes
&& ADDREMOVE_DEFAULT
)
328 addremove
= 0; /* "-u" was given but not "-A" */
330 if (addremove
&& take_worktree_changes
)
331 die(_("-A and -u are mutually incompatible"));
333 if (!take_worktree_changes
&& addremove_explicit
< 0 && argc
)
334 /* Turn "git add pathspec..." to "git add -A pathspec..." */
337 if (!show_only
&& ignore_missing
)
338 die(_("Option --ignore-missing can only be used together with --dry-run"));
340 if ((0 < addremove_explicit
|| take_worktree_changes
) && !argc
) {
341 static const char *whole
[2] = { ":/", NULL
};
346 add_new_files
= !take_worktree_changes
&& !refresh_only
;
347 require_pathspec
= !take_worktree_changes
;
349 newfd
= hold_locked_index(&lock_file
, 1);
351 flags
= ((verbose
? ADD_CACHE_VERBOSE
: 0) |
352 (show_only
? ADD_CACHE_PRETEND
: 0) |
353 (intent_to_add
? ADD_CACHE_INTENT
: 0) |
354 (ignore_add_errors
? ADD_CACHE_IGNORE_ERRORS
: 0) |
355 (!(addremove
|| take_worktree_changes
)
356 ? ADD_CACHE_IGNORE_REMOVAL
: 0));
358 if (require_pathspec
&& argc
== 0) {
359 fprintf(stderr
, _("Nothing specified, nothing added.\n"));
360 fprintf(stderr
, _("Maybe you wanted to say 'git add .'?\n"));
364 if (read_cache() < 0)
365 die(_("index file corrupt"));
368 * Check the "pathspec '%s' did not match any files" block
369 * below before enabling new magic.
371 parse_pathspec(&pathspec
, 0,
372 PATHSPEC_PREFER_FULL
|
373 PATHSPEC_SYMLINK_LEADING_PATH
|
374 PATHSPEC_STRIP_SUBMODULE_SLASH_EXPENSIVE
,
379 struct pathspec empty_pathspec
;
381 /* Set up the default git porcelain excludes */
382 memset(&dir
, 0, sizeof(dir
));
384 dir
.flags
|= DIR_COLLECT_IGNORED
;
385 setup_standard_excludes(&dir
);
388 memset(&empty_pathspec
, 0, sizeof(empty_pathspec
));
389 /* This picks up the paths that are not tracked */
390 baselen
= fill_directory(&dir
, &pathspec
);
392 seen
= prune_directory(&dir
, &pathspec
, baselen
);
396 refresh(verbose
, &pathspec
);
404 seen
= find_pathspecs_matching_against_index(&pathspec
);
407 * file_exists() assumes exact match
409 GUARD_PATHSPEC(&pathspec
,
416 for (i
= 0; i
< pathspec
.nr
; i
++) {
417 const char *path
= pathspec
.items
[i
].match
;
418 if (pathspec
.items
[i
].magic
& PATHSPEC_EXCLUDE
)
420 if (!seen
[i
] && path
[0] &&
421 ((pathspec
.items
[i
].magic
&
422 (PATHSPEC_GLOB
| PATHSPEC_ICASE
)) ||
423 !file_exists(path
))) {
424 if (ignore_missing
) {
425 int dtype
= DT_UNKNOWN
;
426 if (is_excluded(&dir
, path
, &dtype
))
427 dir_add_ignored(&dir
, path
, pathspec
.items
[i
].len
);
429 die(_("pathspec '%s' did not match any files"),
430 pathspec
.items
[i
].original
);
438 exit_status
|= add_files_to_cache(prefix
, &pathspec
, flags
);
441 exit_status
|= add_files(&dir
, flags
);
443 unplug_bulk_checkin();
446 if (active_cache_changed
) {
447 if (write_cache(newfd
, active_cache
, active_nr
) ||
448 commit_locked_index(&lock_file
))
449 die(_("Unable to write new index file"));