2 * "git add" builtin command
4 * Copyright (C) 2006 Linus Torvalds
10 #include "cache-tree.h"
11 #include "run-command.h"
12 #include "parse-options.h"
14 static const char * const builtin_add_usage
[] = {
15 "git add [options] [--] <filepattern>...",
18 static int patch_interactive
, add_interactive
;
19 static int take_worktree_changes
;
21 static void fill_pathspec_matches(const char **pathspec
, char *seen
, int specs
)
23 int num_unmatched
= 0, i
;
26 * Since we are walking the index as if we were walking the directory,
27 * we have to mark the matched pathspec as seen; otherwise we will
28 * mistakenly think that the user gave a pathspec that did not match
31 for (i
= 0; i
< specs
; i
++)
36 for (i
= 0; i
< active_nr
; i
++) {
37 struct cache_entry
*ce
= active_cache
[i
];
38 match_pathspec(pathspec
, ce
->name
, ce_namelen(ce
), 0, seen
);
42 static void prune_directory(struct dir_struct
*dir
, const char **pathspec
, int prefix
)
46 struct dir_entry
**src
, **dst
;
48 for (specs
= 0; pathspec
[specs
]; specs
++)
50 seen
= xcalloc(specs
, 1);
52 src
= dst
= dir
->entries
;
55 struct dir_entry
*entry
= *src
++;
56 if (match_pathspec(pathspec
, entry
->name
, entry
->len
,
60 dir
->nr
= dst
- dir
->entries
;
61 fill_pathspec_matches(pathspec
, seen
, specs
);
63 for (i
= 0; i
< specs
; i
++) {
64 if (!seen
[i
] && !file_exists(pathspec
[i
]))
65 die("pathspec '%s' did not match any files",
71 static void treat_gitlinks(const char **pathspec
)
75 if (!pathspec
|| !*pathspec
)
78 for (i
= 0; i
< active_nr
; i
++) {
79 struct cache_entry
*ce
= active_cache
[i
];
80 if (S_ISGITLINK(ce
->ce_mode
)) {
81 int len
= ce_namelen(ce
), j
;
82 for (j
= 0; pathspec
[j
]; j
++) {
83 int len2
= strlen(pathspec
[j
]);
84 if (len2
<= len
|| pathspec
[j
][len
] != '/' ||
85 memcmp(ce
->name
, pathspec
[j
], len
))
88 /* strip trailing slash */
89 pathspec
[j
] = xstrndup(ce
->name
, len
);
91 die ("Path '%s' is in submodule '%.*s'",
92 pathspec
[j
], len
, ce
->name
);
98 static void fill_directory(struct dir_struct
*dir
, const char **pathspec
,
101 const char *path
, *base
;
104 /* Set up the default git porcelain excludes */
105 memset(dir
, 0, sizeof(*dir
));
107 dir
->collect_ignored
= 1;
108 setup_standard_excludes(dir
);
112 * Calculate common prefix for the pathspec, and
113 * use that to optimize the directory walk
115 baselen
= common_prefix(pathspec
);
119 path
= base
= xmemdupz(*pathspec
, baselen
);
121 /* Read the directory and prune it */
122 read_directory(dir
, path
, base
, baselen
, pathspec
);
124 prune_directory(dir
, pathspec
, baselen
);
127 static void refresh(int verbose
, const char **pathspec
)
132 for (specs
= 0; pathspec
[specs
]; specs
++)
134 seen
= xcalloc(specs
, 1);
135 refresh_index(&the_index
, verbose
? REFRESH_SAY_CHANGED
: REFRESH_QUIET
,
137 for (i
= 0; i
< specs
; i
++) {
139 die("pathspec '%s' did not match any files", pathspec
[i
]);
144 static const char **validate_pathspec(int argc
, const char **argv
, const char *prefix
)
146 const char **pathspec
= get_pathspec(prefix
, argv
);
150 for (p
= pathspec
; *p
; p
++) {
151 if (has_symlink_leading_path(strlen(*p
), *p
)) {
152 int len
= prefix
? strlen(prefix
) : 0;
153 die("'%s' is beyond a symbolic link", *p
+ len
);
161 int interactive_add(int argc
, const char **argv
, const char *prefix
)
165 const char **pathspec
= NULL
;
168 pathspec
= validate_pathspec(argc
, argv
, prefix
);
173 args
= xcalloc(sizeof(const char *), (argc
+ 4));
175 args
[ac
++] = "add--interactive";
176 if (patch_interactive
)
177 args
[ac
++] = "--patch";
180 memcpy(&(args
[ac
]), pathspec
, sizeof(const char *) * argc
);
185 status
= run_command_v_opt(args
, RUN_GIT_CMD
);
190 static struct lock_file lock_file
;
192 static const char ignore_error
[] =
193 "The following paths are ignored by one of your .gitignore files:\n";
195 static int verbose
= 0, show_only
= 0, ignored_too
= 0, refresh_only
= 0;
196 static int ignore_add_errors
, addremove
, intent_to_add
;
198 static struct option builtin_add_options
[] = {
199 OPT__DRY_RUN(&show_only
),
200 OPT__VERBOSE(&verbose
),
202 OPT_BOOLEAN('i', "interactive", &add_interactive
, "interactive picking"),
203 OPT_BOOLEAN('p', "patch", &patch_interactive
, "interactive patching"),
204 OPT_BOOLEAN('f', "force", &ignored_too
, "allow adding otherwise ignored files"),
205 OPT_BOOLEAN('u', "update", &take_worktree_changes
, "update tracked files"),
206 OPT_BOOLEAN('N', "intent-to-add", &intent_to_add
, "record only the fact that the path will be added later"),
207 OPT_BOOLEAN('A', "all", &addremove
, "add all, noticing removal of tracked files"),
208 OPT_BOOLEAN( 0 , "refresh", &refresh_only
, "don't add, only refresh the index"),
209 OPT_BOOLEAN( 0 , "ignore-errors", &ignore_add_errors
, "just skip files which cannot be added because of errors"),
213 static int add_config(const char *var
, const char *value
, void *cb
)
215 if (!strcasecmp(var
, "add.ignore-errors")) {
216 ignore_add_errors
= git_config_bool(var
, value
);
219 return git_default_config(var
, value
, cb
);
222 static int add_files(struct dir_struct
*dir
, int flags
)
224 int i
, exit_status
= 0;
226 if (dir
->ignored_nr
) {
227 fprintf(stderr
, ignore_error
);
228 for (i
= 0; i
< dir
->ignored_nr
; i
++)
229 fprintf(stderr
, "%s\n", dir
->ignored
[i
]->name
);
230 fprintf(stderr
, "Use -f if you really want to add them.\n");
231 die("no files added");
234 for (i
= 0; i
< dir
->nr
; i
++)
235 if (add_file_to_cache(dir
->entries
[i
]->name
, flags
)) {
236 if (!ignore_add_errors
)
237 die("adding files failed");
243 int cmd_add(int argc
, const char **argv
, const char *prefix
)
247 const char **pathspec
;
248 struct dir_struct dir
;
251 int require_pathspec
;
253 argc
= parse_options(argc
, argv
, builtin_add_options
,
254 builtin_add_usage
, 0);
255 if (patch_interactive
)
258 exit(interactive_add(argc
, argv
, prefix
));
260 git_config(add_config
, NULL
);
262 if (addremove
&& take_worktree_changes
)
263 die("-A and -u are mutually incompatible");
264 if ((addremove
|| take_worktree_changes
) && !argc
) {
265 static const char *here
[2] = { ".", NULL
};
270 add_new_files
= !take_worktree_changes
&& !refresh_only
;
271 require_pathspec
= !take_worktree_changes
;
273 newfd
= hold_locked_index(&lock_file
, 1);
275 flags
= ((verbose
? ADD_CACHE_VERBOSE
: 0) |
276 (show_only
? ADD_CACHE_PRETEND
: 0) |
277 (intent_to_add
? ADD_CACHE_INTENT
: 0) |
278 (ignore_add_errors
? ADD_CACHE_IGNORE_ERRORS
: 0) |
279 (!(addremove
|| take_worktree_changes
)
280 ? ADD_CACHE_IGNORE_REMOVAL
: 0));
282 if (require_pathspec
&& argc
== 0) {
283 fprintf(stderr
, "Nothing specified, nothing added.\n");
284 fprintf(stderr
, "Maybe you wanted to say 'git add .'?\n");
287 pathspec
= validate_pathspec(argc
, argv
, prefix
);
289 if (read_cache() < 0)
290 die("index file corrupt");
291 treat_gitlinks(pathspec
);
294 /* This picks up the paths that are not tracked */
295 fill_directory(&dir
, pathspec
, ignored_too
);
298 refresh(verbose
, pathspec
);
302 exit_status
|= add_files_to_cache(prefix
, pathspec
, flags
);
305 exit_status
|= add_files(&dir
, flags
);
308 if (active_cache_changed
) {
309 if (write_cache(newfd
, active_cache
, active_nr
) ||
310 commit_locked_index(&lock_file
))
311 die("Unable to write new index file");