2 * "git rm" builtin command
4 * Copyright (C) Linus Torvalds 2006
6 #define USE_THE_INDEX_VARIABLE
14 #include "tree-walk.h"
15 #include "object-name.h"
16 #include "parse-options.h"
17 #include "read-cache.h"
18 #include "repository.h"
19 #include "string-list.h"
21 #include "sparse-index.h"
22 #include "submodule.h"
25 static const char * const builtin_rm_usage
[] = {
26 N_("git rm [-f | --force] [-n] [-r] [--cached] [--ignore-unmatch]\n"
27 " [--quiet] [--pathspec-from-file=<file> [--pathspec-file-nul]]\n"
28 " [--] [<pathspec>...]"),
40 static int get_ours_cache_pos(const char *path
, int pos
)
44 while ((i
< the_index
.cache_nr
) && !strcmp(the_index
.cache
[i
]->name
, path
)) {
45 if (ce_stage(the_index
.cache
[i
]) == 2)
52 static void print_error_files(struct string_list
*files_list
,
54 const char *hints_msg
,
59 struct strbuf err_msg
= STRBUF_INIT
;
61 strbuf_addstr(&err_msg
, main_msg
);
62 for (i
= 0; i
< files_list
->nr
; i
++)
65 files_list
->items
[i
].string
);
66 if (advice_enabled(ADVICE_RM_HINTS
))
67 strbuf_addstr(&err_msg
, hints_msg
);
68 *errs
= error("%s", err_msg
.buf
);
69 strbuf_release(&err_msg
);
73 static void submodules_absorb_gitdir_if_needed(void)
76 for (i
= 0; i
< list
.nr
; i
++) {
77 const char *name
= list
.entry
[i
].name
;
79 const struct cache_entry
*ce
;
81 pos
= index_name_pos(&the_index
, name
, strlen(name
));
83 pos
= get_ours_cache_pos(name
, pos
);
87 ce
= the_index
.cache
[pos
];
89 if (!S_ISGITLINK(ce
->ce_mode
) ||
90 !file_exists(ce
->name
) ||
94 if (!submodule_uses_gitfile(name
))
95 absorb_git_dir_into_superproject(name
, NULL
);
99 static int check_local_mod(struct object_id
*head
, int index_only
)
102 * Items in list are already sorted in the cache order,
103 * so we could do this a lot more efficiently by using
104 * tree_desc based traversal if we wanted to, but I am
105 * lazy, and who cares if removal of files is a tad
106 * slower than the theoretical maximum speed?
110 struct string_list files_staged
= STRING_LIST_INIT_NODUP
;
111 struct string_list files_cached
= STRING_LIST_INIT_NODUP
;
112 struct string_list files_local
= STRING_LIST_INIT_NODUP
;
114 no_head
= is_null_oid(head
);
115 for (i
= 0; i
< list
.nr
; i
++) {
118 const struct cache_entry
*ce
;
119 const char *name
= list
.entry
[i
].name
;
120 struct object_id oid
;
122 int local_changes
= 0;
123 int staged_changes
= 0;
125 pos
= index_name_pos(&the_index
, name
, strlen(name
));
128 * Skip unmerged entries except for populated submodules
129 * that could lose history when removed.
131 pos
= get_ours_cache_pos(name
, pos
);
135 if (!S_ISGITLINK(the_index
.cache
[pos
]->ce_mode
) ||
139 ce
= the_index
.cache
[pos
];
141 if (lstat(ce
->name
, &st
) < 0) {
142 if (!is_missing_file_error(errno
))
143 warning_errno(_("failed to stat '%s'"), ce
->name
);
144 /* It already vanished from the working tree */
147 else if (S_ISDIR(st
.st_mode
)) {
148 /* if a file was removed and it is now a
149 * directory, that is the same as ENOENT as
150 * far as git is concerned; we do not track
151 * directories unless they are submodules.
153 if (!S_ISGITLINK(ce
->ce_mode
))
158 * "rm" of a path that has changes need to be treated
159 * carefully not to allow losing local changes
160 * accidentally. A local change could be (1) file in
161 * work tree is different since the index; and/or (2)
162 * the user staged a content that is different from
163 * the current commit in the index.
165 * In such a case, you would need to --force the
166 * removal. However, "rm --cached" (remove only from
167 * the index) is safe if the index matches the file in
168 * the work tree or the HEAD commit, as it means that
169 * the content being removed is available elsewhere.
173 * Is the index different from the file in the work tree?
174 * If it's a submodule, is its work tree modified?
176 if (ie_match_stat(&the_index
, ce
, &st
, 0) ||
177 (S_ISGITLINK(ce
->ce_mode
) &&
178 bad_to_remove_submodule(ce
->name
,
179 SUBMODULE_REMOVAL_DIE_ON_ERROR
|
180 SUBMODULE_REMOVAL_IGNORE_IGNORED_UNTRACKED
)))
184 * Is the index different from the HEAD commit? By
185 * definition, before the very initial commit,
186 * anything staged in the index is treated by the same
187 * way as changed from the HEAD.
190 || get_tree_entry(the_repository
, head
, name
, &oid
, &mode
)
191 || ce
->ce_mode
!= create_ce_mode(mode
)
192 || !oideq(&ce
->oid
, &oid
))
196 * If the index does not match the file in the work
197 * tree and if it does not match the HEAD commit
198 * either, (1) "git rm" without --cached definitely
199 * will lose information; (2) "git rm --cached" will
200 * lose information unless it is about removing an
201 * "intent to add" entry.
203 if (local_changes
&& staged_changes
) {
204 if (!index_only
|| !ce_intent_to_add(ce
))
205 string_list_append(&files_staged
, name
);
207 else if (!index_only
) {
209 string_list_append(&files_cached
, name
);
211 string_list_append(&files_local
, name
);
214 print_error_files(&files_staged
,
215 Q_("the following file has staged content different "
216 "from both the\nfile and the HEAD:",
217 "the following files have staged content different"
218 " from both the\nfile and the HEAD:",
220 _("\n(use -f to force removal)"),
222 string_list_clear(&files_staged
, 0);
223 print_error_files(&files_cached
,
224 Q_("the following file has changes "
225 "staged in the index:",
226 "the following files have changes "
227 "staged in the index:", files_cached
.nr
),
228 _("\n(use --cached to keep the file,"
229 " or -f to force removal)"),
231 string_list_clear(&files_cached
, 0);
233 print_error_files(&files_local
,
234 Q_("the following file has local modifications:",
235 "the following files have local modifications:",
237 _("\n(use --cached to keep the file,"
238 " or -f to force removal)"),
240 string_list_clear(&files_local
, 0);
245 static int show_only
= 0, force
= 0, index_only
= 0, recursive
= 0, quiet
= 0;
246 static int ignore_unmatch
= 0, pathspec_file_nul
;
247 static int include_sparse
;
248 static char *pathspec_from_file
;
250 static struct option builtin_rm_options
[] = {
251 OPT__DRY_RUN(&show_only
, N_("dry run")),
252 OPT__QUIET(&quiet
, N_("do not list removed files")),
253 OPT_BOOL( 0 , "cached", &index_only
, N_("only remove from the index")),
254 OPT__FORCE(&force
, N_("override the up-to-date check"), PARSE_OPT_NOCOMPLETE
),
255 OPT_BOOL('r', NULL
, &recursive
, N_("allow recursive removal")),
256 OPT_BOOL( 0 , "ignore-unmatch", &ignore_unmatch
,
257 N_("exit with a zero status even if nothing matched")),
258 OPT_BOOL(0, "sparse", &include_sparse
, N_("allow updating entries outside of the sparse-checkout cone")),
259 OPT_PATHSPEC_FROM_FILE(&pathspec_from_file
),
260 OPT_PATHSPEC_FILE_NUL(&pathspec_file_nul
),
264 int cmd_rm(int argc
, const char **argv
, const char *prefix
)
266 struct lock_file lock_file
= LOCK_INIT
;
268 struct pathspec pathspec
;
271 git_config(git_default_config
, NULL
);
273 argc
= parse_options(argc
, argv
, prefix
, builtin_rm_options
,
274 builtin_rm_usage
, 0);
276 parse_pathspec(&pathspec
, 0,
280 if (pathspec_from_file
) {
282 die(_("'%s' and pathspec arguments cannot be used together"), "--pathspec-from-file");
284 parse_pathspec_file(&pathspec
, 0,
286 prefix
, pathspec_from_file
, pathspec_file_nul
);
287 } else if (pathspec_file_nul
) {
288 die(_("the option '%s' requires '%s'"), "--pathspec-file-nul", "--pathspec-from-file");
292 die(_("No pathspec was given. Which files should I remove?"));
297 prepare_repo_settings(the_repository
);
298 the_repository
->settings
.command_requires_full_index
= 0;
299 repo_hold_locked_index(the_repository
, &lock_file
, LOCK_DIE_ON_ERROR
);
301 if (repo_read_index(the_repository
) < 0)
302 die(_("index file corrupt"));
304 refresh_index(&the_index
, REFRESH_QUIET
|REFRESH_UNMERGED
, &pathspec
, NULL
, NULL
);
306 seen
= xcalloc(pathspec
.nr
, 1);
308 if (pathspec_needs_expanded_index(&the_index
, &pathspec
))
309 ensure_full_index(&the_index
);
311 for (i
= 0; i
< the_index
.cache_nr
; i
++) {
312 const struct cache_entry
*ce
= the_index
.cache
[i
];
314 if (!include_sparse
&&
315 (ce_skip_worktree(ce
) ||
316 !path_in_sparse_checkout(ce
->name
, &the_index
)))
318 if (!ce_path_match(&the_index
, ce
, &pathspec
, seen
))
320 ALLOC_GROW(list
.entry
, list
.nr
+ 1, list
.alloc
);
321 list
.entry
[list
.nr
].name
= xstrdup(ce
->name
);
322 list
.entry
[list
.nr
].is_submodule
= S_ISGITLINK(ce
->ce_mode
);
323 if (list
.entry
[list
.nr
++].is_submodule
&&
324 !is_staging_gitmodules_ok(&the_index
))
325 die(_("please stage your changes to .gitmodules or stash them to proceed"));
329 const char *original
;
331 char *skip_worktree_seen
= NULL
;
332 struct string_list only_match_skip_worktree
= STRING_LIST_INIT_NODUP
;
334 for (i
= 0; i
< pathspec
.nr
; i
++) {
335 original
= pathspec
.items
[i
].original
;
338 else if (ignore_unmatch
)
340 else if (!include_sparse
&&
341 matches_skip_worktree(&pathspec
, i
, &skip_worktree_seen
))
342 string_list_append(&only_match_skip_worktree
, original
);
344 die(_("pathspec '%s' did not match any files"), original
);
346 if (!recursive
&& seen
[i
] == MATCHED_RECURSIVELY
)
347 die(_("not removing '%s' recursively without -r"),
348 *original
? original
: ".");
351 if (only_match_skip_worktree
.nr
) {
352 advise_on_updating_sparse_paths(&only_match_skip_worktree
);
355 free(skip_worktree_seen
);
356 string_list_clear(&only_match_skip_worktree
, 0);
361 clear_pathspec(&pathspec
);
365 submodules_absorb_gitdir_if_needed();
368 * If not forced, the file, the index and the HEAD (if exists)
369 * must match; but the file can already been removed, since
370 * this sequence is a natural "novice" way:
374 * Further, if HEAD commit exists, "diff-index --cached" must
375 * report no changes unless forced.
378 struct object_id oid
;
379 if (repo_get_oid(the_repository
, "HEAD", &oid
))
381 if (check_local_mod(&oid
, index_only
))
386 * First remove the names from the index: we won't commit
387 * the index unless all of them succeed.
389 for (i
= 0; i
< list
.nr
; i
++) {
390 const char *path
= list
.entry
[i
].name
;
392 printf("rm '%s'\n", path
);
394 if (remove_file_from_index(&the_index
, path
))
395 die(_("git rm: unable to remove %s"), path
);
402 * Then, unless we used "--cached", remove the filenames from
403 * the workspace. If we fail to remove the first one, we
404 * abort the "git rm" (but once we've successfully removed
405 * any file at all, we'll go ahead and commit to it all:
406 * by then we've already committed ourselves and can't fail
410 int removed
= 0, gitmodules_modified
= 0;
411 struct strbuf buf
= STRBUF_INIT
;
412 int flag
= force
? REMOVE_DIR_PURGE_ORIGINAL_CWD
: 0;
413 for (i
= 0; i
< list
.nr
; i
++) {
414 const char *path
= list
.entry
[i
].name
;
415 if (list
.entry
[i
].is_submodule
) {
417 strbuf_addstr(&buf
, path
);
418 if (remove_dir_recursively(&buf
, flag
))
419 die(_("could not remove '%s'"), path
);
422 if (!remove_path_from_gitmodules(path
))
423 gitmodules_modified
= 1;
426 if (!remove_path(path
)) {
431 die_errno("git rm: '%s'", path
);
433 strbuf_release(&buf
);
434 if (gitmodules_modified
)
435 stage_updated_gitmodules(&the_index
);
438 if (write_locked_index(&the_index
, &lock_file
,
439 COMMIT_LOCK
| SKIP_IF_UNCHANGED
))
440 die(_("Unable to write new index file"));