2 * "git rm" builtin command
4 * Copyright (C) Linus Torvalds 2006
6 #define USE_THE_INDEX_VARIABLE
13 #include "cache-tree.h"
15 #include "tree-walk.h"
16 #include "parse-options.h"
17 #include "string-list.h"
19 #include "submodule.h"
22 static const char * const builtin_rm_usage
[] = {
23 N_("git rm [-f | --force] [-n] [-r] [--cached] [--ignore-unmatch]\n"
24 " [--quiet] [--pathspec-from-file=<file> [--pathspec-file-nul]]\n"
25 " [--] [<pathspec>...]"),
37 static int get_ours_cache_pos(const char *path
, int pos
)
41 while ((i
< the_index
.cache_nr
) && !strcmp(the_index
.cache
[i
]->name
, path
)) {
42 if (ce_stage(the_index
.cache
[i
]) == 2)
49 static void print_error_files(struct string_list
*files_list
,
51 const char *hints_msg
,
56 struct strbuf err_msg
= STRBUF_INIT
;
58 strbuf_addstr(&err_msg
, main_msg
);
59 for (i
= 0; i
< files_list
->nr
; i
++)
62 files_list
->items
[i
].string
);
63 if (advice_enabled(ADVICE_RM_HINTS
))
64 strbuf_addstr(&err_msg
, hints_msg
);
65 *errs
= error("%s", err_msg
.buf
);
66 strbuf_release(&err_msg
);
70 static void submodules_absorb_gitdir_if_needed(void)
73 for (i
= 0; i
< list
.nr
; i
++) {
74 const char *name
= list
.entry
[i
].name
;
76 const struct cache_entry
*ce
;
78 pos
= index_name_pos(&the_index
, name
, strlen(name
));
80 pos
= get_ours_cache_pos(name
, pos
);
84 ce
= the_index
.cache
[pos
];
86 if (!S_ISGITLINK(ce
->ce_mode
) ||
87 !file_exists(ce
->name
) ||
91 if (!submodule_uses_gitfile(name
))
92 absorb_git_dir_into_superproject(name
, NULL
);
96 static int check_local_mod(struct object_id
*head
, int index_only
)
99 * Items in list are already sorted in the cache order,
100 * so we could do this a lot more efficiently by using
101 * tree_desc based traversal if we wanted to, but I am
102 * lazy, and who cares if removal of files is a tad
103 * slower than the theoretical maximum speed?
107 struct string_list files_staged
= STRING_LIST_INIT_NODUP
;
108 struct string_list files_cached
= STRING_LIST_INIT_NODUP
;
109 struct string_list files_local
= STRING_LIST_INIT_NODUP
;
111 no_head
= is_null_oid(head
);
112 for (i
= 0; i
< list
.nr
; i
++) {
115 const struct cache_entry
*ce
;
116 const char *name
= list
.entry
[i
].name
;
117 struct object_id oid
;
119 int local_changes
= 0;
120 int staged_changes
= 0;
122 pos
= index_name_pos(&the_index
, name
, strlen(name
));
125 * Skip unmerged entries except for populated submodules
126 * that could lose history when removed.
128 pos
= get_ours_cache_pos(name
, pos
);
132 if (!S_ISGITLINK(the_index
.cache
[pos
]->ce_mode
) ||
136 ce
= the_index
.cache
[pos
];
138 if (lstat(ce
->name
, &st
) < 0) {
139 if (!is_missing_file_error(errno
))
140 warning_errno(_("failed to stat '%s'"), ce
->name
);
141 /* It already vanished from the working tree */
144 else if (S_ISDIR(st
.st_mode
)) {
145 /* if a file was removed and it is now a
146 * directory, that is the same as ENOENT as
147 * far as git is concerned; we do not track
148 * directories unless they are submodules.
150 if (!S_ISGITLINK(ce
->ce_mode
))
155 * "rm" of a path that has changes need to be treated
156 * carefully not to allow losing local changes
157 * accidentally. A local change could be (1) file in
158 * work tree is different since the index; and/or (2)
159 * the user staged a content that is different from
160 * the current commit in the index.
162 * In such a case, you would need to --force the
163 * removal. However, "rm --cached" (remove only from
164 * the index) is safe if the index matches the file in
165 * the work tree or the HEAD commit, as it means that
166 * the content being removed is available elsewhere.
170 * Is the index different from the file in the work tree?
171 * If it's a submodule, is its work tree modified?
173 if (ie_match_stat(&the_index
, ce
, &st
, 0) ||
174 (S_ISGITLINK(ce
->ce_mode
) &&
175 bad_to_remove_submodule(ce
->name
,
176 SUBMODULE_REMOVAL_DIE_ON_ERROR
|
177 SUBMODULE_REMOVAL_IGNORE_IGNORED_UNTRACKED
)))
181 * Is the index different from the HEAD commit? By
182 * definition, before the very initial commit,
183 * anything staged in the index is treated by the same
184 * way as changed from the HEAD.
187 || get_tree_entry(the_repository
, head
, name
, &oid
, &mode
)
188 || ce
->ce_mode
!= create_ce_mode(mode
)
189 || !oideq(&ce
->oid
, &oid
))
193 * If the index does not match the file in the work
194 * tree and if it does not match the HEAD commit
195 * either, (1) "git rm" without --cached definitely
196 * will lose information; (2) "git rm --cached" will
197 * lose information unless it is about removing an
198 * "intent to add" entry.
200 if (local_changes
&& staged_changes
) {
201 if (!index_only
|| !ce_intent_to_add(ce
))
202 string_list_append(&files_staged
, name
);
204 else if (!index_only
) {
206 string_list_append(&files_cached
, name
);
208 string_list_append(&files_local
, name
);
211 print_error_files(&files_staged
,
212 Q_("the following file has staged content different "
213 "from both the\nfile and the HEAD:",
214 "the following files have staged content different"
215 " from both the\nfile and the HEAD:",
217 _("\n(use -f to force removal)"),
219 string_list_clear(&files_staged
, 0);
220 print_error_files(&files_cached
,
221 Q_("the following file has changes "
222 "staged in the index:",
223 "the following files have changes "
224 "staged in the index:", files_cached
.nr
),
225 _("\n(use --cached to keep the file,"
226 " or -f to force removal)"),
228 string_list_clear(&files_cached
, 0);
230 print_error_files(&files_local
,
231 Q_("the following file has local modifications:",
232 "the following files have local modifications:",
234 _("\n(use --cached to keep the file,"
235 " or -f to force removal)"),
237 string_list_clear(&files_local
, 0);
242 static int show_only
= 0, force
= 0, index_only
= 0, recursive
= 0, quiet
= 0;
243 static int ignore_unmatch
= 0, pathspec_file_nul
;
244 static int include_sparse
;
245 static char *pathspec_from_file
;
247 static struct option builtin_rm_options
[] = {
248 OPT__DRY_RUN(&show_only
, N_("dry run")),
249 OPT__QUIET(&quiet
, N_("do not list removed files")),
250 OPT_BOOL( 0 , "cached", &index_only
, N_("only remove from the index")),
251 OPT__FORCE(&force
, N_("override the up-to-date check"), PARSE_OPT_NOCOMPLETE
),
252 OPT_BOOL('r', NULL
, &recursive
, N_("allow recursive removal")),
253 OPT_BOOL( 0 , "ignore-unmatch", &ignore_unmatch
,
254 N_("exit with a zero status even if nothing matched")),
255 OPT_BOOL(0, "sparse", &include_sparse
, N_("allow updating entries outside of the sparse-checkout cone")),
256 OPT_PATHSPEC_FROM_FILE(&pathspec_from_file
),
257 OPT_PATHSPEC_FILE_NUL(&pathspec_file_nul
),
261 int cmd_rm(int argc
, const char **argv
, const char *prefix
)
263 struct lock_file lock_file
= LOCK_INIT
;
265 struct pathspec pathspec
;
268 git_config(git_default_config
, NULL
);
270 argc
= parse_options(argc
, argv
, prefix
, builtin_rm_options
,
271 builtin_rm_usage
, 0);
273 parse_pathspec(&pathspec
, 0,
277 if (pathspec_from_file
) {
279 die(_("'%s' and pathspec arguments cannot be used together"), "--pathspec-from-file");
281 parse_pathspec_file(&pathspec
, 0,
283 prefix
, pathspec_from_file
, pathspec_file_nul
);
284 } else if (pathspec_file_nul
) {
285 die(_("the option '%s' requires '%s'"), "--pathspec-file-nul", "--pathspec-from-file");
289 die(_("No pathspec was given. Which files should I remove?"));
294 prepare_repo_settings(the_repository
);
295 the_repository
->settings
.command_requires_full_index
= 0;
296 repo_hold_locked_index(the_repository
, &lock_file
, LOCK_DIE_ON_ERROR
);
298 if (repo_read_index(the_repository
) < 0)
299 die(_("index file corrupt"));
301 refresh_index(&the_index
, REFRESH_QUIET
|REFRESH_UNMERGED
, &pathspec
, NULL
, NULL
);
303 seen
= xcalloc(pathspec
.nr
, 1);
305 if (pathspec_needs_expanded_index(&the_index
, &pathspec
))
306 ensure_full_index(&the_index
);
308 for (i
= 0; i
< the_index
.cache_nr
; i
++) {
309 const struct cache_entry
*ce
= the_index
.cache
[i
];
311 if (!include_sparse
&&
312 (ce_skip_worktree(ce
) ||
313 !path_in_sparse_checkout(ce
->name
, &the_index
)))
315 if (!ce_path_match(&the_index
, ce
, &pathspec
, seen
))
317 ALLOC_GROW(list
.entry
, list
.nr
+ 1, list
.alloc
);
318 list
.entry
[list
.nr
].name
= xstrdup(ce
->name
);
319 list
.entry
[list
.nr
].is_submodule
= S_ISGITLINK(ce
->ce_mode
);
320 if (list
.entry
[list
.nr
++].is_submodule
&&
321 !is_staging_gitmodules_ok(&the_index
))
322 die(_("please stage your changes to .gitmodules or stash them to proceed"));
326 const char *original
;
328 char *skip_worktree_seen
= NULL
;
329 struct string_list only_match_skip_worktree
= STRING_LIST_INIT_NODUP
;
331 for (i
= 0; i
< pathspec
.nr
; i
++) {
332 original
= pathspec
.items
[i
].original
;
335 else if (ignore_unmatch
)
337 else if (!include_sparse
&&
338 matches_skip_worktree(&pathspec
, i
, &skip_worktree_seen
))
339 string_list_append(&only_match_skip_worktree
, original
);
341 die(_("pathspec '%s' did not match any files"), original
);
343 if (!recursive
&& seen
[i
] == MATCHED_RECURSIVELY
)
344 die(_("not removing '%s' recursively without -r"),
345 *original
? original
: ".");
348 if (only_match_skip_worktree
.nr
) {
349 advise_on_updating_sparse_paths(&only_match_skip_worktree
);
352 free(skip_worktree_seen
);
353 string_list_clear(&only_match_skip_worktree
, 0);
358 clear_pathspec(&pathspec
);
362 submodules_absorb_gitdir_if_needed();
365 * If not forced, the file, the index and the HEAD (if exists)
366 * must match; but the file can already been removed, since
367 * this sequence is a natural "novice" way:
371 * Further, if HEAD commit exists, "diff-index --cached" must
372 * report no changes unless forced.
375 struct object_id oid
;
376 if (get_oid("HEAD", &oid
))
378 if (check_local_mod(&oid
, index_only
))
383 * First remove the names from the index: we won't commit
384 * the index unless all of them succeed.
386 for (i
= 0; i
< list
.nr
; i
++) {
387 const char *path
= list
.entry
[i
].name
;
389 printf("rm '%s'\n", path
);
391 if (remove_file_from_index(&the_index
, path
))
392 die(_("git rm: unable to remove %s"), path
);
399 * Then, unless we used "--cached", remove the filenames from
400 * the workspace. If we fail to remove the first one, we
401 * abort the "git rm" (but once we've successfully removed
402 * any file at all, we'll go ahead and commit to it all:
403 * by then we've already committed ourselves and can't fail
407 int removed
= 0, gitmodules_modified
= 0;
408 struct strbuf buf
= STRBUF_INIT
;
409 int flag
= force
? REMOVE_DIR_PURGE_ORIGINAL_CWD
: 0;
410 for (i
= 0; i
< list
.nr
; i
++) {
411 const char *path
= list
.entry
[i
].name
;
412 if (list
.entry
[i
].is_submodule
) {
414 strbuf_addstr(&buf
, path
);
415 if (remove_dir_recursively(&buf
, flag
))
416 die(_("could not remove '%s'"), path
);
419 if (!remove_path_from_gitmodules(path
))
420 gitmodules_modified
= 1;
423 if (!remove_path(path
)) {
428 die_errno("git rm: '%s'", path
);
430 strbuf_release(&buf
);
431 if (gitmodules_modified
)
432 stage_updated_gitmodules(&the_index
);
435 if (write_locked_index(&the_index
, &lock_file
,
436 COMMIT_LOCK
| SKIP_IF_UNCHANGED
))
437 die(_("Unable to write new index file"));