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 "object-name.h"
17 #include "parse-options.h"
18 #include "string-list.h"
20 #include "submodule.h"
23 static const char * const builtin_rm_usage
[] = {
24 N_("git rm [-f | --force] [-n] [-r] [--cached] [--ignore-unmatch]\n"
25 " [--quiet] [--pathspec-from-file=<file> [--pathspec-file-nul]]\n"
26 " [--] [<pathspec>...]"),
38 static int get_ours_cache_pos(const char *path
, int pos
)
42 while ((i
< the_index
.cache_nr
) && !strcmp(the_index
.cache
[i
]->name
, path
)) {
43 if (ce_stage(the_index
.cache
[i
]) == 2)
50 static void print_error_files(struct string_list
*files_list
,
52 const char *hints_msg
,
57 struct strbuf err_msg
= STRBUF_INIT
;
59 strbuf_addstr(&err_msg
, main_msg
);
60 for (i
= 0; i
< files_list
->nr
; i
++)
63 files_list
->items
[i
].string
);
64 if (advice_enabled(ADVICE_RM_HINTS
))
65 strbuf_addstr(&err_msg
, hints_msg
);
66 *errs
= error("%s", err_msg
.buf
);
67 strbuf_release(&err_msg
);
71 static void submodules_absorb_gitdir_if_needed(void)
74 for (i
= 0; i
< list
.nr
; i
++) {
75 const char *name
= list
.entry
[i
].name
;
77 const struct cache_entry
*ce
;
79 pos
= index_name_pos(&the_index
, name
, strlen(name
));
81 pos
= get_ours_cache_pos(name
, pos
);
85 ce
= the_index
.cache
[pos
];
87 if (!S_ISGITLINK(ce
->ce_mode
) ||
88 !file_exists(ce
->name
) ||
92 if (!submodule_uses_gitfile(name
))
93 absorb_git_dir_into_superproject(name
, NULL
);
97 static int check_local_mod(struct object_id
*head
, int index_only
)
100 * Items in list are already sorted in the cache order,
101 * so we could do this a lot more efficiently by using
102 * tree_desc based traversal if we wanted to, but I am
103 * lazy, and who cares if removal of files is a tad
104 * slower than the theoretical maximum speed?
108 struct string_list files_staged
= STRING_LIST_INIT_NODUP
;
109 struct string_list files_cached
= STRING_LIST_INIT_NODUP
;
110 struct string_list files_local
= STRING_LIST_INIT_NODUP
;
112 no_head
= is_null_oid(head
);
113 for (i
= 0; i
< list
.nr
; i
++) {
116 const struct cache_entry
*ce
;
117 const char *name
= list
.entry
[i
].name
;
118 struct object_id oid
;
120 int local_changes
= 0;
121 int staged_changes
= 0;
123 pos
= index_name_pos(&the_index
, name
, strlen(name
));
126 * Skip unmerged entries except for populated submodules
127 * that could lose history when removed.
129 pos
= get_ours_cache_pos(name
, pos
);
133 if (!S_ISGITLINK(the_index
.cache
[pos
]->ce_mode
) ||
137 ce
= the_index
.cache
[pos
];
139 if (lstat(ce
->name
, &st
) < 0) {
140 if (!is_missing_file_error(errno
))
141 warning_errno(_("failed to stat '%s'"), ce
->name
);
142 /* It already vanished from the working tree */
145 else if (S_ISDIR(st
.st_mode
)) {
146 /* if a file was removed and it is now a
147 * directory, that is the same as ENOENT as
148 * far as git is concerned; we do not track
149 * directories unless they are submodules.
151 if (!S_ISGITLINK(ce
->ce_mode
))
156 * "rm" of a path that has changes need to be treated
157 * carefully not to allow losing local changes
158 * accidentally. A local change could be (1) file in
159 * work tree is different since the index; and/or (2)
160 * the user staged a content that is different from
161 * the current commit in the index.
163 * In such a case, you would need to --force the
164 * removal. However, "rm --cached" (remove only from
165 * the index) is safe if the index matches the file in
166 * the work tree or the HEAD commit, as it means that
167 * the content being removed is available elsewhere.
171 * Is the index different from the file in the work tree?
172 * If it's a submodule, is its work tree modified?
174 if (ie_match_stat(&the_index
, ce
, &st
, 0) ||
175 (S_ISGITLINK(ce
->ce_mode
) &&
176 bad_to_remove_submodule(ce
->name
,
177 SUBMODULE_REMOVAL_DIE_ON_ERROR
|
178 SUBMODULE_REMOVAL_IGNORE_IGNORED_UNTRACKED
)))
182 * Is the index different from the HEAD commit? By
183 * definition, before the very initial commit,
184 * anything staged in the index is treated by the same
185 * way as changed from the HEAD.
188 || get_tree_entry(the_repository
, head
, name
, &oid
, &mode
)
189 || ce
->ce_mode
!= create_ce_mode(mode
)
190 || !oideq(&ce
->oid
, &oid
))
194 * If the index does not match the file in the work
195 * tree and if it does not match the HEAD commit
196 * either, (1) "git rm" without --cached definitely
197 * will lose information; (2) "git rm --cached" will
198 * lose information unless it is about removing an
199 * "intent to add" entry.
201 if (local_changes
&& staged_changes
) {
202 if (!index_only
|| !ce_intent_to_add(ce
))
203 string_list_append(&files_staged
, name
);
205 else if (!index_only
) {
207 string_list_append(&files_cached
, name
);
209 string_list_append(&files_local
, name
);
212 print_error_files(&files_staged
,
213 Q_("the following file has staged content different "
214 "from both the\nfile and the HEAD:",
215 "the following files have staged content different"
216 " from both the\nfile and the HEAD:",
218 _("\n(use -f to force removal)"),
220 string_list_clear(&files_staged
, 0);
221 print_error_files(&files_cached
,
222 Q_("the following file has changes "
223 "staged in the index:",
224 "the following files have changes "
225 "staged in the index:", files_cached
.nr
),
226 _("\n(use --cached to keep the file,"
227 " or -f to force removal)"),
229 string_list_clear(&files_cached
, 0);
231 print_error_files(&files_local
,
232 Q_("the following file has local modifications:",
233 "the following files have local modifications:",
235 _("\n(use --cached to keep the file,"
236 " or -f to force removal)"),
238 string_list_clear(&files_local
, 0);
243 static int show_only
= 0, force
= 0, index_only
= 0, recursive
= 0, quiet
= 0;
244 static int ignore_unmatch
= 0, pathspec_file_nul
;
245 static int include_sparse
;
246 static char *pathspec_from_file
;
248 static struct option builtin_rm_options
[] = {
249 OPT__DRY_RUN(&show_only
, N_("dry run")),
250 OPT__QUIET(&quiet
, N_("do not list removed files")),
251 OPT_BOOL( 0 , "cached", &index_only
, N_("only remove from the index")),
252 OPT__FORCE(&force
, N_("override the up-to-date check"), PARSE_OPT_NOCOMPLETE
),
253 OPT_BOOL('r', NULL
, &recursive
, N_("allow recursive removal")),
254 OPT_BOOL( 0 , "ignore-unmatch", &ignore_unmatch
,
255 N_("exit with a zero status even if nothing matched")),
256 OPT_BOOL(0, "sparse", &include_sparse
, N_("allow updating entries outside of the sparse-checkout cone")),
257 OPT_PATHSPEC_FROM_FILE(&pathspec_from_file
),
258 OPT_PATHSPEC_FILE_NUL(&pathspec_file_nul
),
262 int cmd_rm(int argc
, const char **argv
, const char *prefix
)
264 struct lock_file lock_file
= LOCK_INIT
;
266 struct pathspec pathspec
;
269 git_config(git_default_config
, NULL
);
271 argc
= parse_options(argc
, argv
, prefix
, builtin_rm_options
,
272 builtin_rm_usage
, 0);
274 parse_pathspec(&pathspec
, 0,
278 if (pathspec_from_file
) {
280 die(_("'%s' and pathspec arguments cannot be used together"), "--pathspec-from-file");
282 parse_pathspec_file(&pathspec
, 0,
284 prefix
, pathspec_from_file
, pathspec_file_nul
);
285 } else if (pathspec_file_nul
) {
286 die(_("the option '%s' requires '%s'"), "--pathspec-file-nul", "--pathspec-from-file");
290 die(_("No pathspec was given. Which files should I remove?"));
295 prepare_repo_settings(the_repository
);
296 the_repository
->settings
.command_requires_full_index
= 0;
297 repo_hold_locked_index(the_repository
, &lock_file
, LOCK_DIE_ON_ERROR
);
299 if (repo_read_index(the_repository
) < 0)
300 die(_("index file corrupt"));
302 refresh_index(&the_index
, REFRESH_QUIET
|REFRESH_UNMERGED
, &pathspec
, NULL
, NULL
);
304 seen
= xcalloc(pathspec
.nr
, 1);
306 if (pathspec_needs_expanded_index(&the_index
, &pathspec
))
307 ensure_full_index(&the_index
);
309 for (i
= 0; i
< the_index
.cache_nr
; i
++) {
310 const struct cache_entry
*ce
= the_index
.cache
[i
];
312 if (!include_sparse
&&
313 (ce_skip_worktree(ce
) ||
314 !path_in_sparse_checkout(ce
->name
, &the_index
)))
316 if (!ce_path_match(&the_index
, ce
, &pathspec
, seen
))
318 ALLOC_GROW(list
.entry
, list
.nr
+ 1, list
.alloc
);
319 list
.entry
[list
.nr
].name
= xstrdup(ce
->name
);
320 list
.entry
[list
.nr
].is_submodule
= S_ISGITLINK(ce
->ce_mode
);
321 if (list
.entry
[list
.nr
++].is_submodule
&&
322 !is_staging_gitmodules_ok(&the_index
))
323 die(_("please stage your changes to .gitmodules or stash them to proceed"));
327 const char *original
;
329 char *skip_worktree_seen
= NULL
;
330 struct string_list only_match_skip_worktree
= STRING_LIST_INIT_NODUP
;
332 for (i
= 0; i
< pathspec
.nr
; i
++) {
333 original
= pathspec
.items
[i
].original
;
336 else if (ignore_unmatch
)
338 else if (!include_sparse
&&
339 matches_skip_worktree(&pathspec
, i
, &skip_worktree_seen
))
340 string_list_append(&only_match_skip_worktree
, original
);
342 die(_("pathspec '%s' did not match any files"), original
);
344 if (!recursive
&& seen
[i
] == MATCHED_RECURSIVELY
)
345 die(_("not removing '%s' recursively without -r"),
346 *original
? original
: ".");
349 if (only_match_skip_worktree
.nr
) {
350 advise_on_updating_sparse_paths(&only_match_skip_worktree
);
353 free(skip_worktree_seen
);
354 string_list_clear(&only_match_skip_worktree
, 0);
359 clear_pathspec(&pathspec
);
363 submodules_absorb_gitdir_if_needed();
366 * If not forced, the file, the index and the HEAD (if exists)
367 * must match; but the file can already been removed, since
368 * this sequence is a natural "novice" way:
372 * Further, if HEAD commit exists, "diff-index --cached" must
373 * report no changes unless forced.
376 struct object_id oid
;
377 if (repo_get_oid(the_repository
, "HEAD", &oid
))
379 if (check_local_mod(&oid
, index_only
))
384 * First remove the names from the index: we won't commit
385 * the index unless all of them succeed.
387 for (i
= 0; i
< list
.nr
; i
++) {
388 const char *path
= list
.entry
[i
].name
;
390 printf("rm '%s'\n", path
);
392 if (remove_file_from_index(&the_index
, path
))
393 die(_("git rm: unable to remove %s"), path
);
400 * Then, unless we used "--cached", remove the filenames from
401 * the workspace. If we fail to remove the first one, we
402 * abort the "git rm" (but once we've successfully removed
403 * any file at all, we'll go ahead and commit to it all:
404 * by then we've already committed ourselves and can't fail
408 int removed
= 0, gitmodules_modified
= 0;
409 struct strbuf buf
= STRBUF_INIT
;
410 int flag
= force
? REMOVE_DIR_PURGE_ORIGINAL_CWD
: 0;
411 for (i
= 0; i
< list
.nr
; i
++) {
412 const char *path
= list
.entry
[i
].name
;
413 if (list
.entry
[i
].is_submodule
) {
415 strbuf_addstr(&buf
, path
);
416 if (remove_dir_recursively(&buf
, flag
))
417 die(_("could not remove '%s'"), path
);
420 if (!remove_path_from_gitmodules(path
))
421 gitmodules_modified
= 1;
424 if (!remove_path(path
)) {
429 die_errno("git rm: '%s'", path
);
431 strbuf_release(&buf
);
432 if (gitmodules_modified
)
433 stage_updated_gitmodules(&the_index
);
436 if (write_locked_index(&the_index
, &lock_file
,
437 COMMIT_LOCK
| SKIP_IF_UNCHANGED
))
438 die(_("Unable to write new index file"));