2 * "git rm" builtin command
4 * Copyright (C) Linus Torvalds 2006
6 #define USE_THE_REPOSITORY_VARIABLE
14 #include "tree-walk.h"
15 #include "object-name.h"
16 #include "parse-options.h"
17 #include "read-cache.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_repository
->index
->cache_nr
) && !strcmp(the_repository
->index
->cache
[i
]->name
, path
)) {
45 if (ce_stage(the_repository
->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_repository
->index
, name
, strlen(name
));
83 pos
= get_ours_cache_pos(name
, pos
);
87 ce
= the_repository
->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_repository
->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_repository
->index
->cache
[pos
]->ce_mode
) ||
139 ce
= the_repository
->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_repository
->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
),
267 struct repository
*repo UNUSED
)
269 struct lock_file lock_file
= LOCK_INIT
;
271 struct pathspec pathspec
;
274 git_config(git_default_config
, NULL
);
276 argc
= parse_options(argc
, argv
, prefix
, builtin_rm_options
,
277 builtin_rm_usage
, 0);
279 parse_pathspec(&pathspec
, 0,
283 if (pathspec_from_file
) {
285 die(_("'%s' and pathspec arguments cannot be used together"), "--pathspec-from-file");
287 parse_pathspec_file(&pathspec
, 0,
289 prefix
, pathspec_from_file
, pathspec_file_nul
);
290 } else if (pathspec_file_nul
) {
291 die(_("the option '%s' requires '%s'"), "--pathspec-file-nul", "--pathspec-from-file");
295 die(_("No pathspec was given. Which files should I remove?"));
300 prepare_repo_settings(the_repository
);
301 the_repository
->settings
.command_requires_full_index
= 0;
302 repo_hold_locked_index(the_repository
, &lock_file
, LOCK_DIE_ON_ERROR
);
304 if (repo_read_index(the_repository
) < 0)
305 die(_("index file corrupt"));
307 refresh_index(the_repository
->index
, REFRESH_QUIET
|REFRESH_UNMERGED
, &pathspec
, NULL
, NULL
);
309 seen
= xcalloc(pathspec
.nr
, 1);
311 if (pathspec_needs_expanded_index(the_repository
->index
, &pathspec
))
312 ensure_full_index(the_repository
->index
);
314 for (i
= 0; i
< the_repository
->index
->cache_nr
; i
++) {
315 const struct cache_entry
*ce
= the_repository
->index
->cache
[i
];
317 if (!include_sparse
&&
318 (ce_skip_worktree(ce
) ||
319 !path_in_sparse_checkout(ce
->name
, the_repository
->index
)))
321 if (!ce_path_match(the_repository
->index
, ce
, &pathspec
, seen
))
323 ALLOC_GROW(list
.entry
, list
.nr
+ 1, list
.alloc
);
324 list
.entry
[list
.nr
].name
= xstrdup(ce
->name
);
325 list
.entry
[list
.nr
].is_submodule
= S_ISGITLINK(ce
->ce_mode
);
326 if (list
.entry
[list
.nr
++].is_submodule
&&
327 !is_staging_gitmodules_ok(the_repository
->index
))
328 die(_("please stage your changes to .gitmodules or stash them to proceed"));
332 const char *original
;
334 char *skip_worktree_seen
= NULL
;
335 struct string_list only_match_skip_worktree
= STRING_LIST_INIT_NODUP
;
337 for (i
= 0; i
< pathspec
.nr
; i
++) {
338 original
= pathspec
.items
[i
].original
;
341 else if (ignore_unmatch
)
343 else if (!include_sparse
&&
344 matches_skip_worktree(&pathspec
, i
, &skip_worktree_seen
))
345 string_list_append(&only_match_skip_worktree
, original
);
347 die(_("pathspec '%s' did not match any files"), original
);
349 if (!recursive
&& seen
[i
] == MATCHED_RECURSIVELY
)
350 die(_("not removing '%s' recursively without -r"),
351 *original
? original
: ".");
354 if (only_match_skip_worktree
.nr
) {
355 advise_on_updating_sparse_paths(&only_match_skip_worktree
);
358 free(skip_worktree_seen
);
359 string_list_clear(&only_match_skip_worktree
, 0);
364 clear_pathspec(&pathspec
);
368 submodules_absorb_gitdir_if_needed();
371 * If not forced, the file, the index and the HEAD (if exists)
372 * must match; but the file can already been removed, since
373 * this sequence is a natural "novice" way:
377 * Further, if HEAD commit exists, "diff-index --cached" must
378 * report no changes unless forced.
381 struct object_id oid
;
382 if (repo_get_oid(the_repository
, "HEAD", &oid
))
383 oidclr(&oid
, the_repository
->hash_algo
);
384 if (check_local_mod(&oid
, index_only
))
389 * First remove the names from the index: we won't commit
390 * the index unless all of them succeed.
392 for (i
= 0; i
< list
.nr
; i
++) {
393 const char *path
= list
.entry
[i
].name
;
395 printf("rm '%s'\n", path
);
397 if (remove_file_from_index(the_repository
->index
, path
))
398 die(_("git rm: unable to remove %s"), path
);
405 * Then, unless we used "--cached", remove the filenames from
406 * the workspace. If we fail to remove the first one, we
407 * abort the "git rm" (but once we've successfully removed
408 * any file at all, we'll go ahead and commit to it all:
409 * by then we've already committed ourselves and can't fail
413 int removed
= 0, gitmodules_modified
= 0;
414 struct strbuf buf
= STRBUF_INIT
;
415 int flag
= force
? REMOVE_DIR_PURGE_ORIGINAL_CWD
: 0;
416 for (i
= 0; i
< list
.nr
; i
++) {
417 const char *path
= list
.entry
[i
].name
;
418 if (list
.entry
[i
].is_submodule
) {
420 strbuf_addstr(&buf
, path
);
421 if (remove_dir_recursively(&buf
, flag
))
422 die(_("could not remove '%s'"), path
);
425 if (!remove_path_from_gitmodules(path
))
426 gitmodules_modified
= 1;
429 if (!remove_path(path
)) {
434 die_errno("git rm: '%s'", path
);
436 strbuf_release(&buf
);
437 if (gitmodules_modified
)
438 stage_updated_gitmodules(the_repository
->index
);
441 if (write_locked_index(the_repository
->index
, &lock_file
,
442 COMMIT_LOCK
| SKIP_IF_UNCHANGED
))
443 die(_("Unable to write new index file"));