2 * "git rm" builtin command
4 * Copyright (C) Linus Torvalds 2006
6 #define USE_THE_INDEX_COMPATIBILITY_MACROS
12 #include "cache-tree.h"
13 #include "tree-walk.h"
14 #include "parse-options.h"
15 #include "string-list.h"
16 #include "submodule.h"
19 static const char * const builtin_rm_usage
[] = {
20 N_("git rm [-f | --force] [-n] [-r] [--cached] [--ignore-unmatch]\n"
21 " [--quiet] [--pathspec-from-file=<file> [--pathspec-file-nul]]\n"
22 " [--] [<pathspec>...]"),
34 static int get_ours_cache_pos(const char *path
, int pos
)
38 while ((i
< the_index
.cache_nr
) && !strcmp(the_index
.cache
[i
]->name
, path
)) {
39 if (ce_stage(the_index
.cache
[i
]) == 2)
46 static void print_error_files(struct string_list
*files_list
,
48 const char *hints_msg
,
53 struct strbuf err_msg
= STRBUF_INIT
;
55 strbuf_addstr(&err_msg
, main_msg
);
56 for (i
= 0; i
< files_list
->nr
; i
++)
59 files_list
->items
[i
].string
);
60 if (advice_enabled(ADVICE_RM_HINTS
))
61 strbuf_addstr(&err_msg
, hints_msg
);
62 *errs
= error("%s", err_msg
.buf
);
63 strbuf_release(&err_msg
);
67 static void submodules_absorb_gitdir_if_needed(void)
70 for (i
= 0; i
< list
.nr
; i
++) {
71 const char *name
= list
.entry
[i
].name
;
73 const struct cache_entry
*ce
;
75 pos
= index_name_pos(&the_index
, name
, strlen(name
));
77 pos
= get_ours_cache_pos(name
, pos
);
81 ce
= the_index
.cache
[pos
];
83 if (!S_ISGITLINK(ce
->ce_mode
) ||
84 !file_exists(ce
->name
) ||
88 if (!submodule_uses_gitfile(name
))
89 absorb_git_dir_into_superproject(name
,
90 ABSORB_GITDIR_RECURSE_SUBMODULES
);
94 static int check_local_mod(struct object_id
*head
, int index_only
)
97 * Items in list are already sorted in the cache order,
98 * so we could do this a lot more efficiently by using
99 * tree_desc based traversal if we wanted to, but I am
100 * lazy, and who cares if removal of files is a tad
101 * slower than the theoretical maximum speed?
105 struct string_list files_staged
= STRING_LIST_INIT_NODUP
;
106 struct string_list files_cached
= STRING_LIST_INIT_NODUP
;
107 struct string_list files_local
= STRING_LIST_INIT_NODUP
;
109 no_head
= is_null_oid(head
);
110 for (i
= 0; i
< list
.nr
; i
++) {
113 const struct cache_entry
*ce
;
114 const char *name
= list
.entry
[i
].name
;
115 struct object_id oid
;
117 int local_changes
= 0;
118 int staged_changes
= 0;
120 pos
= index_name_pos(&the_index
, name
, strlen(name
));
123 * Skip unmerged entries except for populated submodules
124 * that could lose history when removed.
126 pos
= get_ours_cache_pos(name
, pos
);
130 if (!S_ISGITLINK(the_index
.cache
[pos
]->ce_mode
) ||
134 ce
= the_index
.cache
[pos
];
136 if (lstat(ce
->name
, &st
) < 0) {
137 if (!is_missing_file_error(errno
))
138 warning_errno(_("failed to stat '%s'"), ce
->name
);
139 /* It already vanished from the working tree */
142 else if (S_ISDIR(st
.st_mode
)) {
143 /* if a file was removed and it is now a
144 * directory, that is the same as ENOENT as
145 * far as git is concerned; we do not track
146 * directories unless they are submodules.
148 if (!S_ISGITLINK(ce
->ce_mode
))
153 * "rm" of a path that has changes need to be treated
154 * carefully not to allow losing local changes
155 * accidentally. A local change could be (1) file in
156 * work tree is different since the index; and/or (2)
157 * the user staged a content that is different from
158 * the current commit in the index.
160 * In such a case, you would need to --force the
161 * removal. However, "rm --cached" (remove only from
162 * the index) is safe if the index matches the file in
163 * the work tree or the HEAD commit, as it means that
164 * the content being removed is available elsewhere.
168 * Is the index different from the file in the work tree?
169 * If it's a submodule, is its work tree modified?
171 if (ie_match_stat(&the_index
, ce
, &st
, 0) ||
172 (S_ISGITLINK(ce
->ce_mode
) &&
173 bad_to_remove_submodule(ce
->name
,
174 SUBMODULE_REMOVAL_DIE_ON_ERROR
|
175 SUBMODULE_REMOVAL_IGNORE_IGNORED_UNTRACKED
)))
179 * Is the index different from the HEAD commit? By
180 * definition, before the very initial commit,
181 * anything staged in the index is treated by the same
182 * way as changed from the HEAD.
185 || get_tree_entry(the_repository
, head
, name
, &oid
, &mode
)
186 || ce
->ce_mode
!= create_ce_mode(mode
)
187 || !oideq(&ce
->oid
, &oid
))
191 * If the index does not match the file in the work
192 * tree and if it does not match the HEAD commit
193 * either, (1) "git rm" without --cached definitely
194 * will lose information; (2) "git rm --cached" will
195 * lose information unless it is about removing an
196 * "intent to add" entry.
198 if (local_changes
&& staged_changes
) {
199 if (!index_only
|| !ce_intent_to_add(ce
))
200 string_list_append(&files_staged
, name
);
202 else if (!index_only
) {
204 string_list_append(&files_cached
, name
);
206 string_list_append(&files_local
, name
);
209 print_error_files(&files_staged
,
210 Q_("the following file has staged content different "
211 "from both the\nfile and the HEAD:",
212 "the following files have staged content different"
213 " from both the\nfile and the HEAD:",
215 _("\n(use -f to force removal)"),
217 string_list_clear(&files_staged
, 0);
218 print_error_files(&files_cached
,
219 Q_("the following file has changes "
220 "staged in the index:",
221 "the following files have changes "
222 "staged in the index:", files_cached
.nr
),
223 _("\n(use --cached to keep the file,"
224 " or -f to force removal)"),
226 string_list_clear(&files_cached
, 0);
228 print_error_files(&files_local
,
229 Q_("the following file has local modifications:",
230 "the following files have local modifications:",
232 _("\n(use --cached to keep the file,"
233 " or -f to force removal)"),
235 string_list_clear(&files_local
, 0);
240 static int show_only
= 0, force
= 0, index_only
= 0, recursive
= 0, quiet
= 0;
241 static int ignore_unmatch
= 0, pathspec_file_nul
;
242 static int include_sparse
;
243 static char *pathspec_from_file
;
245 static struct option builtin_rm_options
[] = {
246 OPT__DRY_RUN(&show_only
, N_("dry run")),
247 OPT__QUIET(&quiet
, N_("do not list removed files")),
248 OPT_BOOL( 0 , "cached", &index_only
, N_("only remove from the index")),
249 OPT__FORCE(&force
, N_("override the up-to-date check"), PARSE_OPT_NOCOMPLETE
),
250 OPT_BOOL('r', NULL
, &recursive
, N_("allow recursive removal")),
251 OPT_BOOL( 0 , "ignore-unmatch", &ignore_unmatch
,
252 N_("exit with a zero status even if nothing matched")),
253 OPT_BOOL(0, "sparse", &include_sparse
, N_("allow updating entries outside of the sparse-checkout cone")),
254 OPT_PATHSPEC_FROM_FILE(&pathspec_from_file
),
255 OPT_PATHSPEC_FILE_NUL(&pathspec_file_nul
),
259 int cmd_rm(int argc
, const char **argv
, const char *prefix
)
261 struct lock_file lock_file
= LOCK_INIT
;
263 struct pathspec pathspec
;
266 git_config(git_default_config
, NULL
);
268 argc
= parse_options(argc
, argv
, prefix
, builtin_rm_options
,
269 builtin_rm_usage
, 0);
271 parse_pathspec(&pathspec
, 0,
275 if (pathspec_from_file
) {
277 die(_("'%s' and pathspec arguments cannot be used together"), "--pathspec-from-file");
279 parse_pathspec_file(&pathspec
, 0,
281 prefix
, pathspec_from_file
, pathspec_file_nul
);
282 } else if (pathspec_file_nul
) {
283 die(_("the option '%s' requires '%s'"), "--pathspec-file-nul", "--pathspec-from-file");
287 die(_("No pathspec was given. Which files should I remove?"));
292 prepare_repo_settings(the_repository
);
293 the_repository
->settings
.command_requires_full_index
= 0;
294 repo_hold_locked_index(the_repository
, &lock_file
, LOCK_DIE_ON_ERROR
);
296 if (repo_read_index(the_repository
) < 0)
297 die(_("index file corrupt"));
299 refresh_index(&the_index
, REFRESH_QUIET
|REFRESH_UNMERGED
, &pathspec
, NULL
, NULL
);
301 seen
= xcalloc(pathspec
.nr
, 1);
303 if (pathspec_needs_expanded_index(&the_index
, &pathspec
))
304 ensure_full_index(&the_index
);
306 for (i
= 0; i
< the_index
.cache_nr
; i
++) {
307 const struct cache_entry
*ce
= the_index
.cache
[i
];
309 if (!include_sparse
&&
310 (ce_skip_worktree(ce
) ||
311 !path_in_sparse_checkout(ce
->name
, &the_index
)))
313 if (!ce_path_match(&the_index
, ce
, &pathspec
, seen
))
315 ALLOC_GROW(list
.entry
, list
.nr
+ 1, list
.alloc
);
316 list
.entry
[list
.nr
].name
= xstrdup(ce
->name
);
317 list
.entry
[list
.nr
].is_submodule
= S_ISGITLINK(ce
->ce_mode
);
318 if (list
.entry
[list
.nr
++].is_submodule
&&
319 !is_staging_gitmodules_ok(&the_index
))
320 die(_("please stage your changes to .gitmodules or stash them to proceed"));
324 const char *original
;
326 char *skip_worktree_seen
= NULL
;
327 struct string_list only_match_skip_worktree
= STRING_LIST_INIT_NODUP
;
329 for (i
= 0; i
< pathspec
.nr
; i
++) {
330 original
= pathspec
.items
[i
].original
;
333 else if (ignore_unmatch
)
335 else if (!include_sparse
&&
336 matches_skip_worktree(&pathspec
, i
, &skip_worktree_seen
))
337 string_list_append(&only_match_skip_worktree
, original
);
339 die(_("pathspec '%s' did not match any files"), original
);
341 if (!recursive
&& seen
[i
] == MATCHED_RECURSIVELY
)
342 die(_("not removing '%s' recursively without -r"),
343 *original
? original
: ".");
346 if (only_match_skip_worktree
.nr
) {
347 advise_on_updating_sparse_paths(&only_match_skip_worktree
);
350 free(skip_worktree_seen
);
351 string_list_clear(&only_match_skip_worktree
, 0);
356 clear_pathspec(&pathspec
);
360 submodules_absorb_gitdir_if_needed();
363 * If not forced, the file, the index and the HEAD (if exists)
364 * must match; but the file can already been removed, since
365 * this sequence is a natural "novice" way:
369 * Further, if HEAD commit exists, "diff-index --cached" must
370 * report no changes unless forced.
373 struct object_id oid
;
374 if (get_oid("HEAD", &oid
))
376 if (check_local_mod(&oid
, index_only
))
381 * First remove the names from the index: we won't commit
382 * the index unless all of them succeed.
384 for (i
= 0; i
< list
.nr
; i
++) {
385 const char *path
= list
.entry
[i
].name
;
387 printf("rm '%s'\n", path
);
389 if (remove_file_from_index(&the_index
, path
))
390 die(_("git rm: unable to remove %s"), path
);
397 * Then, unless we used "--cached", remove the filenames from
398 * the workspace. If we fail to remove the first one, we
399 * abort the "git rm" (but once we've successfully removed
400 * any file at all, we'll go ahead and commit to it all:
401 * by then we've already committed ourselves and can't fail
405 int removed
= 0, gitmodules_modified
= 0;
406 struct strbuf buf
= STRBUF_INIT
;
407 int flag
= force
? REMOVE_DIR_PURGE_ORIGINAL_CWD
: 0;
408 for (i
= 0; i
< list
.nr
; i
++) {
409 const char *path
= list
.entry
[i
].name
;
410 if (list
.entry
[i
].is_submodule
) {
412 strbuf_addstr(&buf
, path
);
413 if (remove_dir_recursively(&buf
, flag
))
414 die(_("could not remove '%s'"), path
);
417 if (!remove_path_from_gitmodules(path
))
418 gitmodules_modified
= 1;
421 if (!remove_path(path
)) {
426 die_errno("git rm: '%s'", path
);
428 strbuf_release(&buf
);
429 if (gitmodules_modified
)
430 stage_updated_gitmodules(&the_index
);
433 if (write_locked_index(&the_index
, &lock_file
,
434 COMMIT_LOCK
| SKIP_IF_UNCHANGED
))
435 die(_("Unable to write new index file"));