2 * "git rm" builtin command
4 * Copyright (C) Linus Torvalds 2006
6 #define USE_THE_INDEX_VARIABLE
13 #include "cache-tree.h"
16 #include "tree-walk.h"
17 #include "object-name.h"
18 #include "parse-options.h"
19 #include "read-cache.h"
20 #include "repository.h"
21 #include "string-list.h"
23 #include "sparse-index.h"
24 #include "submodule.h"
27 static const char * const builtin_rm_usage
[] = {
28 N_("git rm [-f | --force] [-n] [-r] [--cached] [--ignore-unmatch]\n"
29 " [--quiet] [--pathspec-from-file=<file> [--pathspec-file-nul]]\n"
30 " [--] [<pathspec>...]"),
42 static int get_ours_cache_pos(const char *path
, int pos
)
46 while ((i
< the_index
.cache_nr
) && !strcmp(the_index
.cache
[i
]->name
, path
)) {
47 if (ce_stage(the_index
.cache
[i
]) == 2)
54 static void print_error_files(struct string_list
*files_list
,
56 const char *hints_msg
,
61 struct strbuf err_msg
= STRBUF_INIT
;
63 strbuf_addstr(&err_msg
, main_msg
);
64 for (i
= 0; i
< files_list
->nr
; i
++)
67 files_list
->items
[i
].string
);
68 if (advice_enabled(ADVICE_RM_HINTS
))
69 strbuf_addstr(&err_msg
, hints_msg
);
70 *errs
= error("%s", err_msg
.buf
);
71 strbuf_release(&err_msg
);
75 static void submodules_absorb_gitdir_if_needed(void)
78 for (i
= 0; i
< list
.nr
; i
++) {
79 const char *name
= list
.entry
[i
].name
;
81 const struct cache_entry
*ce
;
83 pos
= index_name_pos(&the_index
, name
, strlen(name
));
85 pos
= get_ours_cache_pos(name
, pos
);
89 ce
= the_index
.cache
[pos
];
91 if (!S_ISGITLINK(ce
->ce_mode
) ||
92 !file_exists(ce
->name
) ||
96 if (!submodule_uses_gitfile(name
))
97 absorb_git_dir_into_superproject(name
, NULL
);
101 static int check_local_mod(struct object_id
*head
, int index_only
)
104 * Items in list are already sorted in the cache order,
105 * so we could do this a lot more efficiently by using
106 * tree_desc based traversal if we wanted to, but I am
107 * lazy, and who cares if removal of files is a tad
108 * slower than the theoretical maximum speed?
112 struct string_list files_staged
= STRING_LIST_INIT_NODUP
;
113 struct string_list files_cached
= STRING_LIST_INIT_NODUP
;
114 struct string_list files_local
= STRING_LIST_INIT_NODUP
;
116 no_head
= is_null_oid(head
);
117 for (i
= 0; i
< list
.nr
; i
++) {
120 const struct cache_entry
*ce
;
121 const char *name
= list
.entry
[i
].name
;
122 struct object_id oid
;
124 int local_changes
= 0;
125 int staged_changes
= 0;
127 pos
= index_name_pos(&the_index
, name
, strlen(name
));
130 * Skip unmerged entries except for populated submodules
131 * that could lose history when removed.
133 pos
= get_ours_cache_pos(name
, pos
);
137 if (!S_ISGITLINK(the_index
.cache
[pos
]->ce_mode
) ||
141 ce
= the_index
.cache
[pos
];
143 if (lstat(ce
->name
, &st
) < 0) {
144 if (!is_missing_file_error(errno
))
145 warning_errno(_("failed to stat '%s'"), ce
->name
);
146 /* It already vanished from the working tree */
149 else if (S_ISDIR(st
.st_mode
)) {
150 /* if a file was removed and it is now a
151 * directory, that is the same as ENOENT as
152 * far as git is concerned; we do not track
153 * directories unless they are submodules.
155 if (!S_ISGITLINK(ce
->ce_mode
))
160 * "rm" of a path that has changes need to be treated
161 * carefully not to allow losing local changes
162 * accidentally. A local change could be (1) file in
163 * work tree is different since the index; and/or (2)
164 * the user staged a content that is different from
165 * the current commit in the index.
167 * In such a case, you would need to --force the
168 * removal. However, "rm --cached" (remove only from
169 * the index) is safe if the index matches the file in
170 * the work tree or the HEAD commit, as it means that
171 * the content being removed is available elsewhere.
175 * Is the index different from the file in the work tree?
176 * If it's a submodule, is its work tree modified?
178 if (ie_match_stat(&the_index
, ce
, &st
, 0) ||
179 (S_ISGITLINK(ce
->ce_mode
) &&
180 bad_to_remove_submodule(ce
->name
,
181 SUBMODULE_REMOVAL_DIE_ON_ERROR
|
182 SUBMODULE_REMOVAL_IGNORE_IGNORED_UNTRACKED
)))
186 * Is the index different from the HEAD commit? By
187 * definition, before the very initial commit,
188 * anything staged in the index is treated by the same
189 * way as changed from the HEAD.
192 || get_tree_entry(the_repository
, head
, name
, &oid
, &mode
)
193 || ce
->ce_mode
!= create_ce_mode(mode
)
194 || !oideq(&ce
->oid
, &oid
))
198 * If the index does not match the file in the work
199 * tree and if it does not match the HEAD commit
200 * either, (1) "git rm" without --cached definitely
201 * will lose information; (2) "git rm --cached" will
202 * lose information unless it is about removing an
203 * "intent to add" entry.
205 if (local_changes
&& staged_changes
) {
206 if (!index_only
|| !ce_intent_to_add(ce
))
207 string_list_append(&files_staged
, name
);
209 else if (!index_only
) {
211 string_list_append(&files_cached
, name
);
213 string_list_append(&files_local
, name
);
216 print_error_files(&files_staged
,
217 Q_("the following file has staged content different "
218 "from both the\nfile and the HEAD:",
219 "the following files have staged content different"
220 " from both the\nfile and the HEAD:",
222 _("\n(use -f to force removal)"),
224 string_list_clear(&files_staged
, 0);
225 print_error_files(&files_cached
,
226 Q_("the following file has changes "
227 "staged in the index:",
228 "the following files have changes "
229 "staged in the index:", files_cached
.nr
),
230 _("\n(use --cached to keep the file,"
231 " or -f to force removal)"),
233 string_list_clear(&files_cached
, 0);
235 print_error_files(&files_local
,
236 Q_("the following file has local modifications:",
237 "the following files have local modifications:",
239 _("\n(use --cached to keep the file,"
240 " or -f to force removal)"),
242 string_list_clear(&files_local
, 0);
247 static int show_only
= 0, force
= 0, index_only
= 0, recursive
= 0, quiet
= 0;
248 static int ignore_unmatch
= 0, pathspec_file_nul
;
249 static int include_sparse
;
250 static char *pathspec_from_file
;
252 static struct option builtin_rm_options
[] = {
253 OPT__DRY_RUN(&show_only
, N_("dry run")),
254 OPT__QUIET(&quiet
, N_("do not list removed files")),
255 OPT_BOOL( 0 , "cached", &index_only
, N_("only remove from the index")),
256 OPT__FORCE(&force
, N_("override the up-to-date check"), PARSE_OPT_NOCOMPLETE
),
257 OPT_BOOL('r', NULL
, &recursive
, N_("allow recursive removal")),
258 OPT_BOOL( 0 , "ignore-unmatch", &ignore_unmatch
,
259 N_("exit with a zero status even if nothing matched")),
260 OPT_BOOL(0, "sparse", &include_sparse
, N_("allow updating entries outside of the sparse-checkout cone")),
261 OPT_PATHSPEC_FROM_FILE(&pathspec_from_file
),
262 OPT_PATHSPEC_FILE_NUL(&pathspec_file_nul
),
266 int cmd_rm(int argc
, const char **argv
, const char *prefix
)
268 struct lock_file lock_file
= LOCK_INIT
;
270 struct pathspec pathspec
;
273 git_config(git_default_config
, NULL
);
275 argc
= parse_options(argc
, argv
, prefix
, builtin_rm_options
,
276 builtin_rm_usage
, 0);
278 parse_pathspec(&pathspec
, 0,
282 if (pathspec_from_file
) {
284 die(_("'%s' and pathspec arguments cannot be used together"), "--pathspec-from-file");
286 parse_pathspec_file(&pathspec
, 0,
288 prefix
, pathspec_from_file
, pathspec_file_nul
);
289 } else if (pathspec_file_nul
) {
290 die(_("the option '%s' requires '%s'"), "--pathspec-file-nul", "--pathspec-from-file");
294 die(_("No pathspec was given. Which files should I remove?"));
299 prepare_repo_settings(the_repository
);
300 the_repository
->settings
.command_requires_full_index
= 0;
301 repo_hold_locked_index(the_repository
, &lock_file
, LOCK_DIE_ON_ERROR
);
303 if (repo_read_index(the_repository
) < 0)
304 die(_("index file corrupt"));
306 refresh_index(&the_index
, REFRESH_QUIET
|REFRESH_UNMERGED
, &pathspec
, NULL
, NULL
);
308 seen
= xcalloc(pathspec
.nr
, 1);
310 if (pathspec_needs_expanded_index(&the_index
, &pathspec
))
311 ensure_full_index(&the_index
);
313 for (i
= 0; i
< the_index
.cache_nr
; i
++) {
314 const struct cache_entry
*ce
= the_index
.cache
[i
];
316 if (!include_sparse
&&
317 (ce_skip_worktree(ce
) ||
318 !path_in_sparse_checkout(ce
->name
, &the_index
)))
320 if (!ce_path_match(&the_index
, ce
, &pathspec
, seen
))
322 ALLOC_GROW(list
.entry
, list
.nr
+ 1, list
.alloc
);
323 list
.entry
[list
.nr
].name
= xstrdup(ce
->name
);
324 list
.entry
[list
.nr
].is_submodule
= S_ISGITLINK(ce
->ce_mode
);
325 if (list
.entry
[list
.nr
++].is_submodule
&&
326 !is_staging_gitmodules_ok(&the_index
))
327 die(_("please stage your changes to .gitmodules or stash them to proceed"));
331 const char *original
;
333 char *skip_worktree_seen
= NULL
;
334 struct string_list only_match_skip_worktree
= STRING_LIST_INIT_NODUP
;
336 for (i
= 0; i
< pathspec
.nr
; i
++) {
337 original
= pathspec
.items
[i
].original
;
340 else if (ignore_unmatch
)
342 else if (!include_sparse
&&
343 matches_skip_worktree(&pathspec
, i
, &skip_worktree_seen
))
344 string_list_append(&only_match_skip_worktree
, original
);
346 die(_("pathspec '%s' did not match any files"), original
);
348 if (!recursive
&& seen
[i
] == MATCHED_RECURSIVELY
)
349 die(_("not removing '%s' recursively without -r"),
350 *original
? original
: ".");
353 if (only_match_skip_worktree
.nr
) {
354 advise_on_updating_sparse_paths(&only_match_skip_worktree
);
357 free(skip_worktree_seen
);
358 string_list_clear(&only_match_skip_worktree
, 0);
363 clear_pathspec(&pathspec
);
367 submodules_absorb_gitdir_if_needed();
370 * If not forced, the file, the index and the HEAD (if exists)
371 * must match; but the file can already been removed, since
372 * this sequence is a natural "novice" way:
376 * Further, if HEAD commit exists, "diff-index --cached" must
377 * report no changes unless forced.
380 struct object_id oid
;
381 if (repo_get_oid(the_repository
, "HEAD", &oid
))
383 if (check_local_mod(&oid
, index_only
))
388 * First remove the names from the index: we won't commit
389 * the index unless all of them succeed.
391 for (i
= 0; i
< list
.nr
; i
++) {
392 const char *path
= list
.entry
[i
].name
;
394 printf("rm '%s'\n", path
);
396 if (remove_file_from_index(&the_index
, path
))
397 die(_("git rm: unable to remove %s"), path
);
404 * Then, unless we used "--cached", remove the filenames from
405 * the workspace. If we fail to remove the first one, we
406 * abort the "git rm" (but once we've successfully removed
407 * any file at all, we'll go ahead and commit to it all:
408 * by then we've already committed ourselves and can't fail
412 int removed
= 0, gitmodules_modified
= 0;
413 struct strbuf buf
= STRBUF_INIT
;
414 int flag
= force
? REMOVE_DIR_PURGE_ORIGINAL_CWD
: 0;
415 for (i
= 0; i
< list
.nr
; i
++) {
416 const char *path
= list
.entry
[i
].name
;
417 if (list
.entry
[i
].is_submodule
) {
419 strbuf_addstr(&buf
, path
);
420 if (remove_dir_recursively(&buf
, flag
))
421 die(_("could not remove '%s'"), path
);
424 if (!remove_path_from_gitmodules(path
))
425 gitmodules_modified
= 1;
428 if (!remove_path(path
)) {
433 die_errno("git rm: '%s'", path
);
435 strbuf_release(&buf
);
436 if (gitmodules_modified
)
437 stage_updated_gitmodules(&the_index
);
440 if (write_locked_index(&the_index
, &lock_file
,
441 COMMIT_LOCK
| SKIP_IF_UNCHANGED
))
442 die(_("Unable to write new index file"));