2 * "git rm" builtin command
4 * Copyright (C) Linus Torvalds 2006
10 #include "cache-tree.h"
11 #include "tree-walk.h"
12 #include "parse-options.h"
13 #include "string-list.h"
14 #include "submodule.h"
17 static const char * const builtin_rm_usage
[] = {
18 N_("git rm [<options>] [--] <file>..."),
30 static int get_ours_cache_pos(const char *path
, int pos
)
34 while ((i
< active_nr
) && !strcmp(active_cache
[i
]->name
, path
)) {
35 if (ce_stage(active_cache
[i
]) == 2)
42 static void print_error_files(struct string_list
*files_list
,
44 const char *hints_msg
,
49 struct strbuf err_msg
= STRBUF_INIT
;
51 strbuf_addstr(&err_msg
, main_msg
);
52 for (i
= 0; i
< files_list
->nr
; i
++)
55 files_list
->items
[i
].string
);
57 strbuf_addstr(&err_msg
, hints_msg
);
58 *errs
= error("%s", err_msg
.buf
);
59 strbuf_release(&err_msg
);
63 static void submodules_absorb_gitdir_if_needed(const char *prefix
)
66 for (i
= 0; i
< list
.nr
; i
++) {
67 const char *name
= list
.entry
[i
].name
;
69 const struct cache_entry
*ce
;
71 pos
= cache_name_pos(name
, strlen(name
));
73 pos
= get_ours_cache_pos(name
, pos
);
77 ce
= active_cache
[pos
];
79 if (!S_ISGITLINK(ce
->ce_mode
) ||
80 !file_exists(ce
->name
) ||
84 if (!submodule_uses_gitfile(name
))
85 absorb_git_dir_into_superproject(prefix
, name
,
86 ABSORB_GITDIR_RECURSE_SUBMODULES
);
90 static int check_local_mod(struct object_id
*head
, int index_only
)
93 * Items in list are already sorted in the cache order,
94 * so we could do this a lot more efficiently by using
95 * tree_desc based traversal if we wanted to, but I am
96 * lazy, and who cares if removal of files is a tad
97 * slower than the theoretical maximum speed?
101 struct string_list files_staged
= STRING_LIST_INIT_NODUP
;
102 struct string_list files_cached
= STRING_LIST_INIT_NODUP
;
103 struct string_list files_local
= STRING_LIST_INIT_NODUP
;
105 no_head
= is_null_oid(head
);
106 for (i
= 0; i
< list
.nr
; i
++) {
109 const struct cache_entry
*ce
;
110 const char *name
= list
.entry
[i
].name
;
111 struct object_id oid
;
113 int local_changes
= 0;
114 int staged_changes
= 0;
116 pos
= cache_name_pos(name
, strlen(name
));
119 * Skip unmerged entries except for populated submodules
120 * that could lose history when removed.
122 pos
= get_ours_cache_pos(name
, pos
);
126 if (!S_ISGITLINK(active_cache
[pos
]->ce_mode
) ||
130 ce
= active_cache
[pos
];
132 if (lstat(ce
->name
, &st
) < 0) {
133 if (!is_missing_file_error(errno
))
134 warning_errno(_("failed to stat '%s'"), ce
->name
);
135 /* It already vanished from the working tree */
138 else if (S_ISDIR(st
.st_mode
)) {
139 /* if a file was removed and it is now a
140 * directory, that is the same as ENOENT as
141 * far as git is concerned; we do not track
142 * directories unless they are submodules.
144 if (!S_ISGITLINK(ce
->ce_mode
))
149 * "rm" of a path that has changes need to be treated
150 * carefully not to allow losing local changes
151 * accidentally. A local change could be (1) file in
152 * work tree is different since the index; and/or (2)
153 * the user staged a content that is different from
154 * the current commit in the index.
156 * In such a case, you would need to --force the
157 * removal. However, "rm --cached" (remove only from
158 * the index) is safe if the index matches the file in
159 * the work tree or the HEAD commit, as it means that
160 * the content being removed is available elsewhere.
164 * Is the index different from the file in the work tree?
165 * If it's a submodule, is its work tree modified?
167 if (ce_match_stat(ce
, &st
, 0) ||
168 (S_ISGITLINK(ce
->ce_mode
) &&
169 bad_to_remove_submodule(ce
->name
,
170 SUBMODULE_REMOVAL_DIE_ON_ERROR
|
171 SUBMODULE_REMOVAL_IGNORE_IGNORED_UNTRACKED
)))
175 * Is the index different from the HEAD commit? By
176 * definition, before the very initial commit,
177 * anything staged in the index is treated by the same
178 * way as changed from the HEAD.
181 || get_tree_entry(head
, name
, &oid
, &mode
)
182 || ce
->ce_mode
!= create_ce_mode(mode
)
183 || oidcmp(&ce
->oid
, &oid
))
187 * If the index does not match the file in the work
188 * tree and if it does not match the HEAD commit
189 * either, (1) "git rm" without --cached definitely
190 * will lose information; (2) "git rm --cached" will
191 * lose information unless it is about removing an
192 * "intent to add" entry.
194 if (local_changes
&& staged_changes
) {
195 if (!index_only
|| !ce_intent_to_add(ce
))
196 string_list_append(&files_staged
, name
);
198 else if (!index_only
) {
200 string_list_append(&files_cached
, name
);
202 string_list_append(&files_local
, name
);
205 print_error_files(&files_staged
,
206 Q_("the following file has staged content different "
207 "from both the\nfile and the HEAD:",
208 "the following files have staged content different"
209 " from both the\nfile and the HEAD:",
211 _("\n(use -f to force removal)"),
213 string_list_clear(&files_staged
, 0);
214 print_error_files(&files_cached
,
215 Q_("the following file has changes "
216 "staged in the index:",
217 "the following files have changes "
218 "staged in the index:", files_cached
.nr
),
219 _("\n(use --cached to keep the file,"
220 " or -f to force removal)"),
222 string_list_clear(&files_cached
, 0);
224 print_error_files(&files_local
,
225 Q_("the following file has local modifications:",
226 "the following files have local modifications:",
228 _("\n(use --cached to keep the file,"
229 " or -f to force removal)"),
231 string_list_clear(&files_local
, 0);
236 static int show_only
= 0, force
= 0, index_only
= 0, recursive
= 0, quiet
= 0;
237 static int ignore_unmatch
= 0;
239 static struct option builtin_rm_options
[] = {
240 OPT__DRY_RUN(&show_only
, N_("dry run")),
241 OPT__QUIET(&quiet
, N_("do not list removed files")),
242 OPT_BOOL( 0 , "cached", &index_only
, N_("only remove from the index")),
243 OPT__FORCE(&force
, N_("override the up-to-date check"), PARSE_OPT_NOCOMPLETE
),
244 OPT_BOOL('r', NULL
, &recursive
, N_("allow recursive removal")),
245 OPT_BOOL( 0 , "ignore-unmatch", &ignore_unmatch
,
246 N_("exit with a zero status even if nothing matched")),
250 int cmd_rm(int argc
, const char **argv
, const char *prefix
)
252 struct lock_file lock_file
= LOCK_INIT
;
254 struct pathspec pathspec
;
257 git_config(git_default_config
, NULL
);
259 argc
= parse_options(argc
, argv
, prefix
, builtin_rm_options
,
260 builtin_rm_usage
, 0);
262 usage_with_options(builtin_rm_usage
, builtin_rm_options
);
267 hold_locked_index(&lock_file
, LOCK_DIE_ON_ERROR
);
269 if (read_cache() < 0)
270 die(_("index file corrupt"));
272 parse_pathspec(&pathspec
, 0,
275 refresh_index(&the_index
, REFRESH_QUIET
, &pathspec
, NULL
, NULL
);
277 seen
= xcalloc(pathspec
.nr
, 1);
279 for (i
= 0; i
< active_nr
; i
++) {
280 const struct cache_entry
*ce
= active_cache
[i
];
281 if (!ce_path_match(ce
, &pathspec
, seen
))
283 ALLOC_GROW(list
.entry
, list
.nr
+ 1, list
.alloc
);
284 list
.entry
[list
.nr
].name
= xstrdup(ce
->name
);
285 list
.entry
[list
.nr
].is_submodule
= S_ISGITLINK(ce
->ce_mode
);
286 if (list
.entry
[list
.nr
++].is_submodule
&&
287 !is_staging_gitmodules_ok(&the_index
))
288 die(_("please stage your changes to .gitmodules or stash them to proceed"));
292 const char *original
;
294 for (i
= 0; i
< pathspec
.nr
; i
++) {
295 original
= pathspec
.items
[i
].original
;
297 if (!ignore_unmatch
) {
298 die(_("pathspec '%s' did not match any files"),
305 if (!recursive
&& seen
[i
] == MATCHED_RECURSIVELY
)
306 die(_("not removing '%s' recursively without -r"),
307 *original
? original
: ".");
315 submodules_absorb_gitdir_if_needed(prefix
);
318 * If not forced, the file, the index and the HEAD (if exists)
319 * must match; but the file can already been removed, since
320 * this sequence is a natural "novice" way:
324 * Further, if HEAD commit exists, "diff-index --cached" must
325 * report no changes unless forced.
328 struct object_id oid
;
329 if (get_oid("HEAD", &oid
))
331 if (check_local_mod(&oid
, index_only
))
336 * First remove the names from the index: we won't commit
337 * the index unless all of them succeed.
339 for (i
= 0; i
< list
.nr
; i
++) {
340 const char *path
= list
.entry
[i
].name
;
342 printf("rm '%s'\n", path
);
344 if (remove_file_from_cache(path
))
345 die(_("git rm: unable to remove %s"), path
);
352 * Then, unless we used "--cached", remove the filenames from
353 * the workspace. If we fail to remove the first one, we
354 * abort the "git rm" (but once we've successfully removed
355 * any file at all, we'll go ahead and commit to it all:
356 * by then we've already committed ourselves and can't fail
360 int removed
= 0, gitmodules_modified
= 0;
361 struct strbuf buf
= STRBUF_INIT
;
362 for (i
= 0; i
< list
.nr
; i
++) {
363 const char *path
= list
.entry
[i
].name
;
364 if (list
.entry
[i
].is_submodule
) {
366 strbuf_addstr(&buf
, path
);
367 if (remove_dir_recursively(&buf
, 0))
368 die(_("could not remove '%s'"), path
);
371 if (!remove_path_from_gitmodules(path
))
372 gitmodules_modified
= 1;
375 if (!remove_path(path
)) {
380 die_errno("git rm: '%s'", path
);
382 strbuf_release(&buf
);
383 if (gitmodules_modified
)
384 stage_updated_gitmodules(&the_index
);
387 if (write_locked_index(&the_index
, &lock_file
,
388 COMMIT_LOCK
| SKIP_IF_UNCHANGED
))
389 die(_("Unable to write new index file"));