2 * "git rm" builtin command
4 * Copyright (C) Linus Torvalds 2006
9 #include "cache-tree.h"
10 #include "tree-walk.h"
11 #include "parse-options.h"
12 #include "string-list.h"
13 #include "submodule.h"
16 static const char * const builtin_rm_usage[] = {
17 N_("git rm [<options>] [--] <file>..."),
29 static int get_ours_cache_pos(const char *path, int pos)
33 while ((i < active_nr) && !strcmp(active_cache[i]->name, path)) {
34 if (ce_stage(active_cache[i]) == 2)
41 static void print_error_files(struct string_list *files_list,
43 const char *hints_msg,
48 struct strbuf err_msg = STRBUF_INIT;
50 strbuf_addstr(&err_msg, main_msg);
51 for (i = 0; i < files_list->nr; i++)
54 files_list->items[i].string);
56 strbuf_addstr(&err_msg, hints_msg);
57 *errs = error("%s", err_msg.buf);
58 strbuf_release(&err_msg);
62 static void error_removing_concrete_submodules(struct string_list *files, int *errs)
64 print_error_files(files,
65 Q_("the following submodule (or one of its nested "
67 "uses a .git directory:",
68 "the following submodules (or one of their nested "
70 "use a .git directory:", files->nr),
71 _("\n(use 'rm -rf' if you really want to remove "
72 "it including all of its history)"),
74 string_list_clear(files, 0);
77 static int check_submodules_use_gitfiles(void)
81 struct string_list files = STRING_LIST_INIT_NODUP;
83 for (i = 0; i < list.nr; i++) {
84 const char *name = list.entry[i].name;
86 const struct cache_entry *ce;
88 pos = cache_name_pos(name, strlen(name));
90 pos = get_ours_cache_pos(name, pos);
94 ce = active_cache[pos];
96 if (!S_ISGITLINK(ce->ce_mode) ||
97 !file_exists(ce->name) ||
101 if (!submodule_uses_gitfile(name))
102 string_list_append(&files, name);
105 error_removing_concrete_submodules(&files, &errs);
110 static int check_local_mod(struct object_id *head, int index_only)
113 * Items in list are already sorted in the cache order,
114 * so we could do this a lot more efficiently by using
115 * tree_desc based traversal if we wanted to, but I am
116 * lazy, and who cares if removal of files is a tad
117 * slower than the theoretical maximum speed?
121 struct string_list files_staged = STRING_LIST_INIT_NODUP;
122 struct string_list files_cached = STRING_LIST_INIT_NODUP;
123 struct string_list files_submodule = STRING_LIST_INIT_NODUP;
124 struct string_list files_local = STRING_LIST_INIT_NODUP;
126 no_head = is_null_oid(head);
127 for (i = 0; i < list.nr; i++) {
130 const struct cache_entry *ce;
131 const char *name = list.entry[i].name;
132 struct object_id oid;
134 int local_changes = 0;
135 int staged_changes = 0;
137 pos = cache_name_pos(name, strlen(name));
140 * Skip unmerged entries except for populated submodules
141 * that could lose history when removed.
143 pos = get_ours_cache_pos(name, pos);
147 if (!S_ISGITLINK(active_cache[pos]->ce_mode) ||
151 ce = active_cache[pos];
153 if (lstat(ce->name, &st) < 0) {
154 if (errno != ENOENT && errno != ENOTDIR)
155 warning_errno(_("failed to stat '%s'"), ce->name);
156 /* It already vanished from the working tree */
159 else if (S_ISDIR(st.st_mode)) {
160 /* if a file was removed and it is now a
161 * directory, that is the same as ENOENT as
162 * far as git is concerned; we do not track
163 * directories unless they are submodules.
165 if (!S_ISGITLINK(ce->ce_mode))
170 * "rm" of a path that has changes need to be treated
171 * carefully not to allow losing local changes
172 * accidentally. A local change could be (1) file in
173 * work tree is different since the index; and/or (2)
174 * the user staged a content that is different from
175 * the current commit in the index.
177 * In such a case, you would need to --force the
178 * removal. However, "rm --cached" (remove only from
179 * the index) is safe if the index matches the file in
180 * the work tree or the HEAD commit, as it means that
181 * the content being removed is available elsewhere.
185 * Is the index different from the file in the work tree?
186 * If it's a submodule, is its work tree modified?
188 if (ce_match_stat(ce, &st, 0) ||
189 (S_ISGITLINK(ce->ce_mode) &&
190 bad_to_remove_submodule(ce->name,
191 SUBMODULE_REMOVAL_DIE_ON_ERROR |
192 SUBMODULE_REMOVAL_IGNORE_IGNORED_UNTRACKED)))
196 * Is the index different from the HEAD commit? By
197 * definition, before the very initial commit,
198 * anything staged in the index is treated by the same
199 * way as changed from the HEAD.
202 || get_tree_entry(head->hash, name, oid.hash, &mode)
203 || ce->ce_mode != create_ce_mode(mode)
204 || oidcmp(&ce->oid, &oid))
208 * If the index does not match the file in the work
209 * tree and if it does not match the HEAD commit
210 * either, (1) "git rm" without --cached definitely
211 * will lose information; (2) "git rm --cached" will
212 * lose information unless it is about removing an
213 * "intent to add" entry.
215 if (local_changes && staged_changes) {
216 if (!index_only || !ce_intent_to_add(ce))
217 string_list_append(&files_staged, name);
219 else if (!index_only) {
221 string_list_append(&files_cached, name);
223 if (S_ISGITLINK(ce->ce_mode) &&
224 !submodule_uses_gitfile(name))
225 string_list_append(&files_submodule, name);
227 string_list_append(&files_local, name);
231 print_error_files(&files_staged,
232 Q_("the following file has staged content different "
233 "from both the\nfile and the HEAD:",
234 "the following files have staged content different"
235 " from both the\nfile and the HEAD:",
237 _("\n(use -f to force removal)"),
239 string_list_clear(&files_staged, 0);
240 print_error_files(&files_cached,
241 Q_("the following file has changes "
242 "staged in the index:",
243 "the following files have changes "
244 "staged in the index:", files_cached.nr),
245 _("\n(use --cached to keep the file,"
246 " or -f to force removal)"),
248 string_list_clear(&files_cached, 0);
250 error_removing_concrete_submodules(&files_submodule, &errs);
252 print_error_files(&files_local,
253 Q_("the following file has local modifications:",
254 "the following files have local modifications:",
256 _("\n(use --cached to keep the file,"
257 " or -f to force removal)"),
259 string_list_clear(&files_local, 0);
264 static struct lock_file lock_file;
266 static int show_only = 0, force = 0, index_only = 0, recursive = 0, quiet = 0;
267 static int ignore_unmatch = 0;
269 static struct option builtin_rm_options[] = {
270 OPT__DRY_RUN(&show_only, N_("dry run")),
271 OPT__QUIET(&quiet, N_("do not list removed files")),
272 OPT_BOOL( 0 , "cached", &index_only, N_("only remove from the index")),
273 OPT__FORCE(&force, N_("override the up-to-date check")),
274 OPT_BOOL('r', NULL, &recursive, N_("allow recursive removal")),
275 OPT_BOOL( 0 , "ignore-unmatch", &ignore_unmatch,
276 N_("exit with a zero status even if nothing matched")),
280 int cmd_rm(int argc, const char **argv, const char *prefix)
283 struct pathspec pathspec;
287 git_config(git_default_config, NULL);
289 argc = parse_options(argc, argv, prefix, builtin_rm_options,
290 builtin_rm_usage, 0);
292 usage_with_options(builtin_rm_usage, builtin_rm_options);
297 hold_locked_index(&lock_file, 1);
299 if (read_cache() < 0)
300 die(_("index file corrupt"));
302 parse_pathspec(&pathspec, 0,
303 PATHSPEC_PREFER_CWD |
304 PATHSPEC_STRIP_SUBMODULE_SLASH_CHEAP,
306 refresh_index(&the_index, REFRESH_QUIET, &pathspec, NULL, NULL);
308 seen = xcalloc(pathspec.nr, 1);
310 for (i = 0; i < active_nr; i++) {
311 const struct cache_entry *ce = active_cache[i];
312 if (!ce_path_match(ce, &pathspec, seen))
314 ALLOC_GROW(list.entry, list.nr + 1, list.alloc);
315 list.entry[list.nr].name = xstrdup(ce->name);
316 list.entry[list.nr].is_submodule = S_ISGITLINK(ce->ce_mode);
317 if (list.entry[list.nr++].is_submodule &&
318 !is_staging_gitmodules_ok())
319 die (_("Please stage your changes to .gitmodules or stash them to proceed"));
323 const char *original;
325 for (i = 0; i < pathspec.nr; i++) {
326 original = pathspec.items[i].original;
328 if (!ignore_unmatch) {
329 die(_("pathspec '%s' did not match any files"),
336 if (!recursive && seen[i] == MATCHED_RECURSIVELY)
337 die(_("not removing '%s' recursively without -r"),
338 *original ? original : ".");
346 * If not forced, the file, the index and the HEAD (if exists)
347 * must match; but the file can already been removed, since
348 * this sequence is a natural "novice" way:
352 * Further, if HEAD commit exists, "diff-index --cached" must
353 * report no changes unless forced.
356 struct object_id oid;
357 if (get_oid("HEAD", &oid))
359 if (check_local_mod(&oid, index_only))
361 } else if (!index_only) {
362 if (check_submodules_use_gitfiles())
367 * First remove the names from the index: we won't commit
368 * the index unless all of them succeed.
370 for (i = 0; i < list.nr; i++) {
371 const char *path = list.entry[i].name;
373 printf("rm '%s'\n", path);
375 if (remove_file_from_cache(path))
376 die(_("git rm: unable to remove %s"), path);
383 * Then, unless we used "--cached", remove the filenames from
384 * the workspace. If we fail to remove the first one, we
385 * abort the "git rm" (but once we've successfully removed
386 * any file at all, we'll go ahead and commit to it all:
387 * by then we've already committed ourselves and can't fail
391 int removed = 0, gitmodules_modified = 0;
392 struct strbuf buf = STRBUF_INIT;
393 for (i = 0; i < list.nr; i++) {
394 const char *path = list.entry[i].name;
395 if (list.entry[i].is_submodule) {
396 if (is_empty_dir(path)) {
399 if (!remove_path_from_gitmodules(path))
400 gitmodules_modified = 1;
405 strbuf_addstr(&buf, path);
406 if (!remove_dir_recursively(&buf, 0)) {
408 if (!remove_path_from_gitmodules(path))
409 gitmodules_modified = 1;
410 strbuf_release(&buf);
412 } else if (!file_exists(path))
413 /* Submodule was removed by user */
414 if (!remove_path_from_gitmodules(path))
415 gitmodules_modified = 1;
416 /* Fallthrough and let remove_path() fail. */
419 if (!remove_path(path)) {
424 die_errno("git rm: '%s'", path);
426 strbuf_release(&buf);
427 if (gitmodules_modified)
428 stage_updated_gitmodules();
431 if (active_cache_changed) {
432 if (write_locked_index(&the_index, &lock_file, COMMIT_LOCK))
433 die(_("Unable to write new index file"));