sparse-index.h: move declarations for sparse-index.c from cache.h
[git.git] / builtin / rm.c
blob377a0f66ad8156fb33b5486c3aa790f9463d8baa
1 /*
2 * "git rm" builtin command
4 * Copyright (C) Linus Torvalds 2006
5 */
6 #define USE_THE_INDEX_VARIABLE
7 #include "builtin.h"
8 #include "alloc.h"
9 #include "advice.h"
10 #include "config.h"
11 #include "lockfile.h"
12 #include "dir.h"
13 #include "cache-tree.h"
14 #include "gettext.h"
15 #include "hash.h"
16 #include "tree-walk.h"
17 #include "object-name.h"
18 #include "parse-options.h"
19 #include "repository.h"
20 #include "string-list.h"
21 #include "setup.h"
22 #include "sparse-index.h"
23 #include "submodule.h"
24 #include "pathspec.h"
26 static const char * const builtin_rm_usage[] = {
27 N_("git rm [-f | --force] [-n] [-r] [--cached] [--ignore-unmatch]\n"
28 " [--quiet] [--pathspec-from-file=<file> [--pathspec-file-nul]]\n"
29 " [--] [<pathspec>...]"),
30 NULL
33 static struct {
34 int nr, alloc;
35 struct {
36 const char *name;
37 char is_submodule;
38 } *entry;
39 } list;
41 static int get_ours_cache_pos(const char *path, int pos)
43 int i = -pos - 1;
45 while ((i < the_index.cache_nr) && !strcmp(the_index.cache[i]->name, path)) {
46 if (ce_stage(the_index.cache[i]) == 2)
47 return i;
48 i++;
50 return -1;
53 static void print_error_files(struct string_list *files_list,
54 const char *main_msg,
55 const char *hints_msg,
56 int *errs)
58 if (files_list->nr) {
59 int i;
60 struct strbuf err_msg = STRBUF_INIT;
62 strbuf_addstr(&err_msg, main_msg);
63 for (i = 0; i < files_list->nr; i++)
64 strbuf_addf(&err_msg,
65 "\n %s",
66 files_list->items[i].string);
67 if (advice_enabled(ADVICE_RM_HINTS))
68 strbuf_addstr(&err_msg, hints_msg);
69 *errs = error("%s", err_msg.buf);
70 strbuf_release(&err_msg);
74 static void submodules_absorb_gitdir_if_needed(void)
76 int i;
77 for (i = 0; i < list.nr; i++) {
78 const char *name = list.entry[i].name;
79 int pos;
80 const struct cache_entry *ce;
82 pos = index_name_pos(&the_index, name, strlen(name));
83 if (pos < 0) {
84 pos = get_ours_cache_pos(name, pos);
85 if (pos < 0)
86 continue;
88 ce = the_index.cache[pos];
90 if (!S_ISGITLINK(ce->ce_mode) ||
91 !file_exists(ce->name) ||
92 is_empty_dir(name))
93 continue;
95 if (!submodule_uses_gitfile(name))
96 absorb_git_dir_into_superproject(name, NULL);
100 static int check_local_mod(struct object_id *head, int index_only)
103 * Items in list are already sorted in the cache order,
104 * so we could do this a lot more efficiently by using
105 * tree_desc based traversal if we wanted to, but I am
106 * lazy, and who cares if removal of files is a tad
107 * slower than the theoretical maximum speed?
109 int i, no_head;
110 int errs = 0;
111 struct string_list files_staged = STRING_LIST_INIT_NODUP;
112 struct string_list files_cached = STRING_LIST_INIT_NODUP;
113 struct string_list files_local = STRING_LIST_INIT_NODUP;
115 no_head = is_null_oid(head);
116 for (i = 0; i < list.nr; i++) {
117 struct stat st;
118 int pos;
119 const struct cache_entry *ce;
120 const char *name = list.entry[i].name;
121 struct object_id oid;
122 unsigned short mode;
123 int local_changes = 0;
124 int staged_changes = 0;
126 pos = index_name_pos(&the_index, name, strlen(name));
127 if (pos < 0) {
129 * Skip unmerged entries except for populated submodules
130 * that could lose history when removed.
132 pos = get_ours_cache_pos(name, pos);
133 if (pos < 0)
134 continue;
136 if (!S_ISGITLINK(the_index.cache[pos]->ce_mode) ||
137 is_empty_dir(name))
138 continue;
140 ce = the_index.cache[pos];
142 if (lstat(ce->name, &st) < 0) {
143 if (!is_missing_file_error(errno))
144 warning_errno(_("failed to stat '%s'"), ce->name);
145 /* It already vanished from the working tree */
146 continue;
148 else if (S_ISDIR(st.st_mode)) {
149 /* if a file was removed and it is now a
150 * directory, that is the same as ENOENT as
151 * far as git is concerned; we do not track
152 * directories unless they are submodules.
154 if (!S_ISGITLINK(ce->ce_mode))
155 continue;
159 * "rm" of a path that has changes need to be treated
160 * carefully not to allow losing local changes
161 * accidentally. A local change could be (1) file in
162 * work tree is different since the index; and/or (2)
163 * the user staged a content that is different from
164 * the current commit in the index.
166 * In such a case, you would need to --force the
167 * removal. However, "rm --cached" (remove only from
168 * the index) is safe if the index matches the file in
169 * the work tree or the HEAD commit, as it means that
170 * the content being removed is available elsewhere.
174 * Is the index different from the file in the work tree?
175 * If it's a submodule, is its work tree modified?
177 if (ie_match_stat(&the_index, ce, &st, 0) ||
178 (S_ISGITLINK(ce->ce_mode) &&
179 bad_to_remove_submodule(ce->name,
180 SUBMODULE_REMOVAL_DIE_ON_ERROR |
181 SUBMODULE_REMOVAL_IGNORE_IGNORED_UNTRACKED)))
182 local_changes = 1;
185 * Is the index different from the HEAD commit? By
186 * definition, before the very initial commit,
187 * anything staged in the index is treated by the same
188 * way as changed from the HEAD.
190 if (no_head
191 || get_tree_entry(the_repository, head, name, &oid, &mode)
192 || ce->ce_mode != create_ce_mode(mode)
193 || !oideq(&ce->oid, &oid))
194 staged_changes = 1;
197 * If the index does not match the file in the work
198 * tree and if it does not match the HEAD commit
199 * either, (1) "git rm" without --cached definitely
200 * will lose information; (2) "git rm --cached" will
201 * lose information unless it is about removing an
202 * "intent to add" entry.
204 if (local_changes && staged_changes) {
205 if (!index_only || !ce_intent_to_add(ce))
206 string_list_append(&files_staged, name);
208 else if (!index_only) {
209 if (staged_changes)
210 string_list_append(&files_cached, name);
211 if (local_changes)
212 string_list_append(&files_local, name);
215 print_error_files(&files_staged,
216 Q_("the following file has staged content different "
217 "from both the\nfile and the HEAD:",
218 "the following files have staged content different"
219 " from both the\nfile and the HEAD:",
220 files_staged.nr),
221 _("\n(use -f to force removal)"),
222 &errs);
223 string_list_clear(&files_staged, 0);
224 print_error_files(&files_cached,
225 Q_("the following file has changes "
226 "staged in the index:",
227 "the following files have changes "
228 "staged in the index:", files_cached.nr),
229 _("\n(use --cached to keep the file,"
230 " or -f to force removal)"),
231 &errs);
232 string_list_clear(&files_cached, 0);
234 print_error_files(&files_local,
235 Q_("the following file has local modifications:",
236 "the following files have local modifications:",
237 files_local.nr),
238 _("\n(use --cached to keep the file,"
239 " or -f to force removal)"),
240 &errs);
241 string_list_clear(&files_local, 0);
243 return errs;
246 static int show_only = 0, force = 0, index_only = 0, recursive = 0, quiet = 0;
247 static int ignore_unmatch = 0, pathspec_file_nul;
248 static int include_sparse;
249 static char *pathspec_from_file;
251 static struct option builtin_rm_options[] = {
252 OPT__DRY_RUN(&show_only, N_("dry run")),
253 OPT__QUIET(&quiet, N_("do not list removed files")),
254 OPT_BOOL( 0 , "cached", &index_only, N_("only remove from the index")),
255 OPT__FORCE(&force, N_("override the up-to-date check"), PARSE_OPT_NOCOMPLETE),
256 OPT_BOOL('r', NULL, &recursive, N_("allow recursive removal")),
257 OPT_BOOL( 0 , "ignore-unmatch", &ignore_unmatch,
258 N_("exit with a zero status even if nothing matched")),
259 OPT_BOOL(0, "sparse", &include_sparse, N_("allow updating entries outside of the sparse-checkout cone")),
260 OPT_PATHSPEC_FROM_FILE(&pathspec_from_file),
261 OPT_PATHSPEC_FILE_NUL(&pathspec_file_nul),
262 OPT_END(),
265 int cmd_rm(int argc, const char **argv, const char *prefix)
267 struct lock_file lock_file = LOCK_INIT;
268 int i, ret = 0;
269 struct pathspec pathspec;
270 char *seen;
272 git_config(git_default_config, NULL);
274 argc = parse_options(argc, argv, prefix, builtin_rm_options,
275 builtin_rm_usage, 0);
277 parse_pathspec(&pathspec, 0,
278 PATHSPEC_PREFER_CWD,
279 prefix, argv);
281 if (pathspec_from_file) {
282 if (pathspec.nr)
283 die(_("'%s' and pathspec arguments cannot be used together"), "--pathspec-from-file");
285 parse_pathspec_file(&pathspec, 0,
286 PATHSPEC_PREFER_CWD,
287 prefix, pathspec_from_file, pathspec_file_nul);
288 } else if (pathspec_file_nul) {
289 die(_("the option '%s' requires '%s'"), "--pathspec-file-nul", "--pathspec-from-file");
292 if (!pathspec.nr)
293 die(_("No pathspec was given. Which files should I remove?"));
295 if (!index_only)
296 setup_work_tree();
298 prepare_repo_settings(the_repository);
299 the_repository->settings.command_requires_full_index = 0;
300 repo_hold_locked_index(the_repository, &lock_file, LOCK_DIE_ON_ERROR);
302 if (repo_read_index(the_repository) < 0)
303 die(_("index file corrupt"));
305 refresh_index(&the_index, REFRESH_QUIET|REFRESH_UNMERGED, &pathspec, NULL, NULL);
307 seen = xcalloc(pathspec.nr, 1);
309 if (pathspec_needs_expanded_index(&the_index, &pathspec))
310 ensure_full_index(&the_index);
312 for (i = 0; i < the_index.cache_nr; i++) {
313 const struct cache_entry *ce = the_index.cache[i];
315 if (!include_sparse &&
316 (ce_skip_worktree(ce) ||
317 !path_in_sparse_checkout(ce->name, &the_index)))
318 continue;
319 if (!ce_path_match(&the_index, ce, &pathspec, seen))
320 continue;
321 ALLOC_GROW(list.entry, list.nr + 1, list.alloc);
322 list.entry[list.nr].name = xstrdup(ce->name);
323 list.entry[list.nr].is_submodule = S_ISGITLINK(ce->ce_mode);
324 if (list.entry[list.nr++].is_submodule &&
325 !is_staging_gitmodules_ok(&the_index))
326 die(_("please stage your changes to .gitmodules or stash them to proceed"));
329 if (pathspec.nr) {
330 const char *original;
331 int seen_any = 0;
332 char *skip_worktree_seen = NULL;
333 struct string_list only_match_skip_worktree = STRING_LIST_INIT_NODUP;
335 for (i = 0; i < pathspec.nr; i++) {
336 original = pathspec.items[i].original;
337 if (seen[i])
338 seen_any = 1;
339 else if (ignore_unmatch)
340 continue;
341 else if (!include_sparse &&
342 matches_skip_worktree(&pathspec, i, &skip_worktree_seen))
343 string_list_append(&only_match_skip_worktree, original);
344 else
345 die(_("pathspec '%s' did not match any files"), original);
347 if (!recursive && seen[i] == MATCHED_RECURSIVELY)
348 die(_("not removing '%s' recursively without -r"),
349 *original ? original : ".");
352 if (only_match_skip_worktree.nr) {
353 advise_on_updating_sparse_paths(&only_match_skip_worktree);
354 ret = 1;
356 free(skip_worktree_seen);
357 string_list_clear(&only_match_skip_worktree, 0);
359 if (!seen_any)
360 exit(ret);
362 clear_pathspec(&pathspec);
363 free(seen);
365 if (!index_only)
366 submodules_absorb_gitdir_if_needed();
369 * If not forced, the file, the index and the HEAD (if exists)
370 * must match; but the file can already been removed, since
371 * this sequence is a natural "novice" way:
373 * rm F; git rm F
375 * Further, if HEAD commit exists, "diff-index --cached" must
376 * report no changes unless forced.
378 if (!force) {
379 struct object_id oid;
380 if (repo_get_oid(the_repository, "HEAD", &oid))
381 oidclr(&oid);
382 if (check_local_mod(&oid, index_only))
383 exit(1);
387 * First remove the names from the index: we won't commit
388 * the index unless all of them succeed.
390 for (i = 0; i < list.nr; i++) {
391 const char *path = list.entry[i].name;
392 if (!quiet)
393 printf("rm '%s'\n", path);
395 if (remove_file_from_index(&the_index, path))
396 die(_("git rm: unable to remove %s"), path);
399 if (show_only)
400 return 0;
403 * Then, unless we used "--cached", remove the filenames from
404 * the workspace. If we fail to remove the first one, we
405 * abort the "git rm" (but once we've successfully removed
406 * any file at all, we'll go ahead and commit to it all:
407 * by then we've already committed ourselves and can't fail
408 * in the middle)
410 if (!index_only) {
411 int removed = 0, gitmodules_modified = 0;
412 struct strbuf buf = STRBUF_INIT;
413 int flag = force ? REMOVE_DIR_PURGE_ORIGINAL_CWD : 0;
414 for (i = 0; i < list.nr; i++) {
415 const char *path = list.entry[i].name;
416 if (list.entry[i].is_submodule) {
417 strbuf_reset(&buf);
418 strbuf_addstr(&buf, path);
419 if (remove_dir_recursively(&buf, flag))
420 die(_("could not remove '%s'"), path);
422 removed = 1;
423 if (!remove_path_from_gitmodules(path))
424 gitmodules_modified = 1;
425 continue;
427 if (!remove_path(path)) {
428 removed = 1;
429 continue;
431 if (!removed)
432 die_errno("git rm: '%s'", path);
434 strbuf_release(&buf);
435 if (gitmodules_modified)
436 stage_updated_gitmodules(&the_index);
439 if (write_locked_index(&the_index, &lock_file,
440 COMMIT_LOCK | SKIP_IF_UNCHANGED))
441 die(_("Unable to write new index file"));
443 return ret;