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"
16 static const char * const builtin_rm_usage
[] = {
17 N_("git rm [options] [--] <file>..."),
29 static int get_ours_cache_pos(const char *path
, int pos
)
33 while ((i
< active_nr
) && !strcmp(active_cache
[i
]->name
, path
)) {
34 if (ce_stage(active_cache
[i
]) == 2)
41 static void print_error_files(struct string_list
*files_list
,
43 const char *hints_msg
,
48 struct strbuf err_msg
= STRBUF_INIT
;
50 strbuf_addstr(&err_msg
, main_msg
);
51 for (i
= 0; i
< files_list
->nr
; i
++)
54 files_list
->items
[i
].string
);
56 strbuf_addstr(&err_msg
, hints_msg
);
57 *errs
= error("%s", err_msg
.buf
);
58 strbuf_release(&err_msg
);
62 static int check_submodules_use_gitfiles(void)
66 struct string_list files
= STRING_LIST_INIT_NODUP
;
68 for (i
= 0; i
< list
.nr
; i
++) {
69 const char *name
= list
.entry
[i
].name
;
71 struct cache_entry
*ce
;
74 pos
= cache_name_pos(name
, strlen(name
));
76 pos
= get_ours_cache_pos(name
, pos
);
80 ce
= active_cache
[pos
];
82 if (!S_ISGITLINK(ce
->ce_mode
) ||
83 (lstat(ce
->name
, &st
) < 0) ||
87 if (!submodule_uses_gitfile(name
))
88 string_list_append(&files
, name
);
90 print_error_files(&files
,
91 Q_("the following submodule (or one of its nested "
92 "submodules)\n uses a .git directory:",
93 "the following submodules (or one of its nested "
94 "submodules)\n use a .git directory:",
96 _("\n(use 'rm -rf' if you really want to remove "
97 "it including all of its history)"),
99 string_list_clear(&files
, 0);
104 static int check_local_mod(unsigned char *head
, int index_only
)
107 * Items in list are already sorted in the cache order,
108 * so we could do this a lot more efficiently by using
109 * tree_desc based traversal if we wanted to, but I am
110 * lazy, and who cares if removal of files is a tad
111 * slower than the theoretical maximum speed?
115 struct string_list files_staged
= STRING_LIST_INIT_NODUP
;
116 struct string_list files_cached
= STRING_LIST_INIT_NODUP
;
117 struct string_list files_submodule
= STRING_LIST_INIT_NODUP
;
118 struct string_list files_local
= STRING_LIST_INIT_NODUP
;
120 no_head
= is_null_sha1(head
);
121 for (i
= 0; i
< list
.nr
; i
++) {
124 struct cache_entry
*ce
;
125 const char *name
= list
.entry
[i
].name
;
126 unsigned char sha1
[20];
128 int local_changes
= 0;
129 int staged_changes
= 0;
131 pos
= cache_name_pos(name
, strlen(name
));
134 * Skip unmerged entries except for populated submodules
135 * that could lose history when removed.
137 pos
= get_ours_cache_pos(name
, pos
);
141 if (!S_ISGITLINK(active_cache
[pos
]->ce_mode
) ||
145 ce
= active_cache
[pos
];
147 if (lstat(ce
->name
, &st
) < 0) {
148 if (errno
!= ENOENT
&& errno
!= ENOTDIR
)
149 warning("'%s': %s", ce
->name
, strerror(errno
));
150 /* It already vanished from the working tree */
153 else if (S_ISDIR(st
.st_mode
)) {
154 /* if a file was removed and it is now a
155 * directory, that is the same as ENOENT as
156 * far as git is concerned; we do not track
157 * directories unless they are submodules.
159 if (!S_ISGITLINK(ce
->ce_mode
))
164 * "rm" of a path that has changes need to be treated
165 * carefully not to allow losing local changes
166 * accidentally. A local change could be (1) file in
167 * work tree is different since the index; and/or (2)
168 * the user staged a content that is different from
169 * the current commit in the index.
171 * In such a case, you would need to --force the
172 * removal. However, "rm --cached" (remove only from
173 * the index) is safe if the index matches the file in
174 * the work tree or the HEAD commit, as it means that
175 * the content being removed is available elsewhere.
179 * Is the index different from the file in the work tree?
180 * If it's a submodule, is its work tree modified?
182 if (ce_match_stat(ce
, &st
, 0) ||
183 (S_ISGITLINK(ce
->ce_mode
) &&
184 !ok_to_remove_submodule(ce
->name
)))
188 * Is the index different from the HEAD commit? By
189 * definition, before the very initial commit,
190 * anything staged in the index is treated by the same
191 * way as changed from the HEAD.
194 || get_tree_entry(head
, name
, sha1
, &mode
)
195 || ce
->ce_mode
!= create_ce_mode(mode
)
196 || hashcmp(ce
->sha1
, sha1
))
200 * If the index does not match the file in the work
201 * tree and if it does not match the HEAD commit
202 * either, (1) "git rm" without --cached definitely
203 * will lose information; (2) "git rm --cached" will
204 * lose information unless it is about removing an
205 * "intent to add" entry.
207 if (local_changes
&& staged_changes
) {
208 if (!index_only
|| !(ce
->ce_flags
& CE_INTENT_TO_ADD
))
209 string_list_append(&files_staged
, name
);
211 else if (!index_only
) {
213 string_list_append(&files_cached
, name
);
215 if (S_ISGITLINK(ce
->ce_mode
) &&
216 !submodule_uses_gitfile(name
))
217 string_list_append(&files_submodule
, name
);
219 string_list_append(&files_local
, name
);
223 print_error_files(&files_staged
,
224 Q_("the following file has staged content different "
225 "from both the\nfile and the HEAD:",
226 "the following files have staged content different"
227 " from both the\nfile and the HEAD:",
229 _("\n(use -f to force removal)"),
231 string_list_clear(&files_staged
, 0);
232 print_error_files(&files_cached
,
233 Q_("the following file has changes "
234 "staged in the index:",
235 "the following files have changes "
236 "staged in the index:", files_cached
.nr
),
237 _("\n(use --cached to keep the file,"
238 " or -f to force removal)"),
240 string_list_clear(&files_cached
, 0);
241 print_error_files(&files_submodule
,
242 Q_("the following submodule (or one of its nested "
243 "submodule)\nuses a .git directory:",
244 "the following submodules (or one of its nested "
245 "submodule)\nuse a .git directory:",
247 _("\n(use 'rm -rf' if you really "
248 "want to remove it including all "
251 string_list_clear(&files_submodule
, 0);
252 print_error_files(&files_local
,
253 Q_("the following file has local modifications:",
254 "the following files have local modifications:",
256 _("\n(use --cached to keep the file,"
257 " or -f to force removal)"),
259 string_list_clear(&files_local
, 0);
264 static struct lock_file lock_file
;
266 static int show_only
= 0, force
= 0, index_only
= 0, recursive
= 0, quiet
= 0;
267 static int ignore_unmatch
= 0;
269 static struct option builtin_rm_options
[] = {
270 OPT__DRY_RUN(&show_only
, N_("dry run")),
271 OPT__QUIET(&quiet
, N_("do not list removed files")),
272 OPT_BOOLEAN( 0 , "cached", &index_only
, N_("only remove from the index")),
273 OPT__FORCE(&force
, N_("override the up-to-date check")),
274 OPT_BOOLEAN('r', NULL
, &recursive
, N_("allow recursive removal")),
275 OPT_BOOLEAN( 0 , "ignore-unmatch", &ignore_unmatch
,
276 N_("exit with a zero status even if nothing matched")),
280 int cmd_rm(int argc
, const char **argv
, const char *prefix
)
283 struct pathspec pathspec
;
286 git_config(git_default_config
, NULL
);
288 argc
= parse_options(argc
, argv
, prefix
, builtin_rm_options
,
289 builtin_rm_usage
, 0);
291 usage_with_options(builtin_rm_usage
, builtin_rm_options
);
296 newfd
= hold_locked_index(&lock_file
, 1);
298 if (read_cache() < 0)
299 die(_("index file corrupt"));
302 * Drop trailing directory separators from directories so we'll find
303 * submodules in the index.
305 for (i
= 0; i
< argc
; i
++) {
306 size_t pathlen
= strlen(argv
[i
]);
307 if (pathlen
&& is_dir_sep(argv
[i
][pathlen
- 1]) &&
308 is_directory(argv
[i
])) {
311 } while (pathlen
&& is_dir_sep(argv
[i
][pathlen
- 1]));
312 argv
[i
] = xmemdupz(argv
[i
], pathlen
);
316 parse_pathspec(&pathspec
, 0, PATHSPEC_PREFER_CWD
, prefix
, argv
);
317 refresh_index(&the_index
, REFRESH_QUIET
, &pathspec
, NULL
, NULL
);
320 seen
= xcalloc(pathspec
.nr
, 1);
322 for (i
= 0; i
< active_nr
; i
++) {
323 struct cache_entry
*ce
= active_cache
[i
];
324 if (!match_pathspec_depth(&pathspec
, ce
->name
, ce_namelen(ce
), 0, seen
))
326 ALLOC_GROW(list
.entry
, list
.nr
+ 1, list
.alloc
);
327 list
.entry
[list
.nr
].name
= ce
->name
;
328 list
.entry
[list
.nr
++].is_submodule
= S_ISGITLINK(ce
->ce_mode
);
332 const char *original
;
334 for (i
= 0; i
< pathspec
.nr
; i
++) {
335 original
= pathspec
.items
[i
].original
;
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 *original
? original
: ".");
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"));