2 * "git rm" builtin command
4 * Copyright (C) Linus Torvalds 2006
9 #include "cache-tree.h"
10 #include "tree-walk.h"
11 #include "parse-options.h"
12 #include "string-list.h"
13 #include "submodule.h"
15 static const char * const builtin_rm_usage
[] = {
16 N_("git rm [options] [--] <file>..."),
28 static int get_ours_cache_pos(const char *path
, int pos
)
32 while ((i
< active_nr
) && !strcmp(active_cache
[i
]->name
, path
)) {
33 if (ce_stage(active_cache
[i
]) == 2)
40 static void print_error_files(struct string_list
*files_list
,
42 const char *hints_msg
,
47 struct strbuf err_msg
= STRBUF_INIT
;
49 strbuf_addstr(&err_msg
, main_msg
);
50 for (i
= 0; i
< files_list
->nr
; i
++)
53 files_list
->items
[i
].string
);
55 strbuf_addstr(&err_msg
, hints_msg
);
56 *errs
= error("%s", err_msg
.buf
);
57 strbuf_release(&err_msg
);
61 static int check_submodules_use_gitfiles(void)
65 struct string_list files
= STRING_LIST_INIT_NODUP
;
67 for (i
= 0; i
< list
.nr
; i
++) {
68 const char *name
= list
.entry
[i
].name
;
70 const struct cache_entry
*ce
;
73 pos
= cache_name_pos(name
, strlen(name
));
75 pos
= get_ours_cache_pos(name
, pos
);
79 ce
= active_cache
[pos
];
81 if (!S_ISGITLINK(ce
->ce_mode
) ||
82 (lstat(ce
->name
, &st
) < 0) ||
86 if (!submodule_uses_gitfile(name
))
87 string_list_append(&files
, name
);
89 print_error_files(&files
,
90 Q_("the following submodule (or one of its nested "
91 "submodules)\n uses a .git directory:",
92 "the following submodules (or one of its nested "
93 "submodules)\n use a .git directory:",
95 _("\n(use 'rm -rf' if you really want to remove "
96 "it including all of its history)"),
98 string_list_clear(&files
, 0);
103 static int check_local_mod(unsigned char *head
, int index_only
)
106 * Items in list are already sorted in the cache order,
107 * so we could do this a lot more efficiently by using
108 * tree_desc based traversal if we wanted to, but I am
109 * lazy, and who cares if removal of files is a tad
110 * slower than the theoretical maximum speed?
114 struct string_list files_staged
= STRING_LIST_INIT_NODUP
;
115 struct string_list files_cached
= STRING_LIST_INIT_NODUP
;
116 struct string_list files_submodule
= STRING_LIST_INIT_NODUP
;
117 struct string_list files_local
= STRING_LIST_INIT_NODUP
;
119 no_head
= is_null_sha1(head
);
120 for (i
= 0; i
< list
.nr
; i
++) {
123 const struct cache_entry
*ce
;
124 const char *name
= list
.entry
[i
].name
;
125 unsigned char sha1
[20];
127 int local_changes
= 0;
128 int staged_changes
= 0;
130 pos
= cache_name_pos(name
, strlen(name
));
133 * Skip unmerged entries except for populated submodules
134 * that could lose history when removed.
136 pos
= get_ours_cache_pos(name
, pos
);
140 if (!S_ISGITLINK(active_cache
[pos
]->ce_mode
) ||
144 ce
= active_cache
[pos
];
146 if (lstat(ce
->name
, &st
) < 0) {
147 if (errno
!= ENOENT
&& errno
!= ENOTDIR
)
148 warning("'%s': %s", ce
->name
, strerror(errno
));
149 /* It already vanished from the working tree */
152 else if (S_ISDIR(st
.st_mode
)) {
153 /* if a file was removed and it is now a
154 * directory, that is the same as ENOENT as
155 * far as git is concerned; we do not track
156 * directories unless they are submodules.
158 if (!S_ISGITLINK(ce
->ce_mode
))
163 * "rm" of a path that has changes need to be treated
164 * carefully not to allow losing local changes
165 * accidentally. A local change could be (1) file in
166 * work tree is different since the index; and/or (2)
167 * the user staged a content that is different from
168 * the current commit in the index.
170 * In such a case, you would need to --force the
171 * removal. However, "rm --cached" (remove only from
172 * the index) is safe if the index matches the file in
173 * the work tree or the HEAD commit, as it means that
174 * the content being removed is available elsewhere.
178 * Is the index different from the file in the work tree?
179 * If it's a submodule, is its work tree modified?
181 if (ce_match_stat(ce
, &st
, 0) ||
182 (S_ISGITLINK(ce
->ce_mode
) &&
183 !ok_to_remove_submodule(ce
->name
)))
187 * Is the index different from the HEAD commit? By
188 * definition, before the very initial commit,
189 * anything staged in the index is treated by the same
190 * way as changed from the HEAD.
193 || get_tree_entry(head
, name
, sha1
, &mode
)
194 || ce
->ce_mode
!= create_ce_mode(mode
)
195 || hashcmp(ce
->sha1
, sha1
))
199 * If the index does not match the file in the work
200 * tree and if it does not match the HEAD commit
201 * either, (1) "git rm" without --cached definitely
202 * will lose information; (2) "git rm --cached" will
203 * lose information unless it is about removing an
204 * "intent to add" entry.
206 if (local_changes
&& staged_changes
) {
207 if (!index_only
|| !(ce
->ce_flags
& CE_INTENT_TO_ADD
))
208 string_list_append(&files_staged
, name
);
210 else if (!index_only
) {
212 string_list_append(&files_cached
, name
);
214 if (S_ISGITLINK(ce
->ce_mode
) &&
215 !submodule_uses_gitfile(name
))
216 string_list_append(&files_submodule
, name
);
218 string_list_append(&files_local
, name
);
222 print_error_files(&files_staged
,
223 Q_("the following file has staged content different "
224 "from both the\nfile and the HEAD:",
225 "the following files have staged content different"
226 " from both the\nfile and the HEAD:",
228 _("\n(use -f to force removal)"),
230 string_list_clear(&files_staged
, 0);
231 print_error_files(&files_cached
,
232 Q_("the following file has changes "
233 "staged in the index:",
234 "the following files have changes "
235 "staged in the index:", files_cached
.nr
),
236 _("\n(use --cached to keep the file,"
237 " or -f to force removal)"),
239 string_list_clear(&files_cached
, 0);
240 print_error_files(&files_submodule
,
241 Q_("the following submodule (or one of its nested "
242 "submodule)\nuses a .git directory:",
243 "the following submodules (or one of its nested "
244 "submodule)\nuse a .git directory:",
246 _("\n(use 'rm -rf' if you really "
247 "want to remove it including all "
250 string_list_clear(&files_submodule
, 0);
251 print_error_files(&files_local
,
252 Q_("the following file has local modifications:",
253 "the following files have local modifications:",
255 _("\n(use --cached to keep the file,"
256 " or -f to force removal)"),
258 string_list_clear(&files_local
, 0);
263 static struct lock_file lock_file
;
265 static int show_only
= 0, force
= 0, index_only
= 0, recursive
= 0, quiet
= 0;
266 static int ignore_unmatch
= 0;
268 static struct option builtin_rm_options
[] = {
269 OPT__DRY_RUN(&show_only
, N_("dry run")),
270 OPT__QUIET(&quiet
, N_("do not list removed files")),
271 OPT_BOOL( 0 , "cached", &index_only
, N_("only remove from the index")),
272 OPT__FORCE(&force
, N_("override the up-to-date check")),
273 OPT_BOOL('r', NULL
, &recursive
, N_("allow recursive removal")),
274 OPT_BOOL( 0 , "ignore-unmatch", &ignore_unmatch
,
275 N_("exit with a zero status even if nothing matched")),
279 int cmd_rm(int argc
, const char **argv
, const char *prefix
)
282 const char **pathspec
;
285 git_config(git_default_config
, NULL
);
287 argc
= parse_options(argc
, argv
, prefix
, builtin_rm_options
,
288 builtin_rm_usage
, 0);
290 usage_with_options(builtin_rm_usage
, builtin_rm_options
);
295 newfd
= hold_locked_index(&lock_file
, 1);
297 if (read_cache() < 0)
298 die(_("index file corrupt"));
301 * Drop trailing directory separators from directories so we'll find
302 * submodules in the index.
304 for (i
= 0; i
< argc
; i
++) {
305 size_t pathlen
= strlen(argv
[i
]);
306 if (pathlen
&& is_dir_sep(argv
[i
][pathlen
- 1]) &&
307 is_directory(argv
[i
])) {
310 } while (pathlen
&& is_dir_sep(argv
[i
][pathlen
- 1]));
311 argv
[i
] = xmemdupz(argv
[i
], pathlen
);
315 pathspec
= get_pathspec(prefix
, argv
);
316 refresh_index(&the_index
, REFRESH_QUIET
, pathspec
, NULL
, NULL
);
319 for (i
= 0; pathspec
[i
] ; i
++)
321 seen
= xcalloc(i
, 1);
323 for (i
= 0; i
< active_nr
; i
++) {
324 const struct cache_entry
*ce
= active_cache
[i
];
325 if (!match_pathspec(pathspec
, ce
->name
, ce_namelen(ce
), 0, seen
))
327 ALLOC_GROW(list
.entry
, list
.nr
+ 1, list
.alloc
);
328 list
.entry
[list
.nr
].name
= ce
->name
;
329 list
.entry
[list
.nr
++].is_submodule
= S_ISGITLINK(ce
->ce_mode
);
335 for (i
= 0; (match
= pathspec
[i
]) != NULL
; i
++) {
337 if (!ignore_unmatch
) {
338 die(_("pathspec '%s' did not match any files"),
345 if (!recursive
&& seen
[i
] == MATCHED_RECURSIVELY
)
346 die(_("not removing '%s' recursively without -r"),
347 *match
? match
: ".");
355 * If not forced, the file, the index and the HEAD (if exists)
356 * must match; but the file can already been removed, since
357 * this sequence is a natural "novice" way:
361 * Further, if HEAD commit exists, "diff-index --cached" must
362 * report no changes unless forced.
365 unsigned char sha1
[20];
366 if (get_sha1("HEAD", sha1
))
368 if (check_local_mod(sha1
, index_only
))
370 } else if (!index_only
) {
371 if (check_submodules_use_gitfiles())
376 * First remove the names from the index: we won't commit
377 * the index unless all of them succeed.
379 for (i
= 0; i
< list
.nr
; i
++) {
380 const char *path
= list
.entry
[i
].name
;
382 printf("rm '%s'\n", path
);
384 if (remove_file_from_cache(path
))
385 die(_("git rm: unable to remove %s"), path
);
392 * Then, unless we used "--cached", remove the filenames from
393 * the workspace. If we fail to remove the first one, we
394 * abort the "git rm" (but once we've successfully removed
395 * any file at all, we'll go ahead and commit to it all:
396 * by then we've already committed ourselves and can't fail
401 for (i
= 0; i
< list
.nr
; i
++) {
402 const char *path
= list
.entry
[i
].name
;
403 if (list
.entry
[i
].is_submodule
) {
404 if (is_empty_dir(path
)) {
410 struct strbuf buf
= STRBUF_INIT
;
411 strbuf_addstr(&buf
, path
);
412 if (!remove_dir_recursively(&buf
, 0)) {
414 strbuf_release(&buf
);
417 strbuf_release(&buf
);
418 /* Fallthrough and let remove_path() fail. */
421 if (!remove_path(path
)) {
426 die_errno("git rm: '%s'", path
);
430 if (active_cache_changed
) {
431 if (write_cache(newfd
, active_cache
, active_nr
) ||
432 commit_locked_index(&lock_file
))
433 die(_("Unable to write new index file"));