pathspec: catch prepending :(prefix) on pathspec with short magic
[git.git] / builtin / rm.c
blobfe3faad15835fcd2e0ce50e89cbdc9a564599dbe
1 /*
2 * "git rm" builtin command
4 * Copyright (C) Linus Torvalds 2006
5 */
6 #include "cache.h"
7 #include "builtin.h"
8 #include "dir.h"
9 #include "cache-tree.h"
10 #include "tree-walk.h"
11 #include "parse-options.h"
12 #include "string-list.h"
13 #include "submodule.h"
14 #include "pathspec.h"
16 static const char * const builtin_rm_usage[] = {
17 N_("git rm [options] [--] <file>..."),
18 NULL
21 static struct {
22 int nr, alloc;
23 struct {
24 const char *name;
25 char is_submodule;
26 } *entry;
27 } list;
29 static int get_ours_cache_pos(const char *path, int pos)
31 int i = -pos - 1;
33 while ((i < active_nr) && !strcmp(active_cache[i]->name, path)) {
34 if (ce_stage(active_cache[i]) == 2)
35 return i;
36 i++;
38 return -1;
41 static void print_error_files(struct string_list *files_list,
42 const char *main_msg,
43 const char *hints_msg,
44 int *errs)
46 if (files_list->nr) {
47 int i;
48 struct strbuf err_msg = STRBUF_INIT;
50 strbuf_addstr(&err_msg, main_msg);
51 for (i = 0; i < files_list->nr; i++)
52 strbuf_addf(&err_msg,
53 "\n %s",
54 files_list->items[i].string);
55 if (advice_rm_hints)
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)
64 int i;
65 int errs = 0;
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;
70 int pos;
71 struct cache_entry *ce;
72 struct stat st;
74 pos = cache_name_pos(name, strlen(name));
75 if (pos < 0) {
76 pos = get_ours_cache_pos(name, pos);
77 if (pos < 0)
78 continue;
80 ce = active_cache[pos];
82 if (!S_ISGITLINK(ce->ce_mode) ||
83 (lstat(ce->name, &st) < 0) ||
84 is_empty_dir(name))
85 continue;
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:",
95 files.nr),
96 _("\n(use 'rm -rf' if you really want to remove "
97 "it including all of its history)"),
98 &errs);
99 string_list_clear(&files, 0);
101 return errs;
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?
113 int i, no_head;
114 int errs = 0;
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++) {
122 struct stat st;
123 int pos;
124 struct cache_entry *ce;
125 const char *name = list.entry[i].name;
126 unsigned char sha1[20];
127 unsigned mode;
128 int local_changes = 0;
129 int staged_changes = 0;
131 pos = cache_name_pos(name, strlen(name));
132 if (pos < 0) {
134 * Skip unmerged entries except for populated submodules
135 * that could lose history when removed.
137 pos = get_ours_cache_pos(name, pos);
138 if (pos < 0)
139 continue;
141 if (!S_ISGITLINK(active_cache[pos]->ce_mode) ||
142 is_empty_dir(name))
143 continue;
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 */
151 continue;
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))
160 continue;
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)))
185 local_changes = 1;
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.
193 if (no_head
194 || get_tree_entry(head, name, sha1, &mode)
195 || ce->ce_mode != create_ce_mode(mode)
196 || hashcmp(ce->sha1, sha1))
197 staged_changes = 1;
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) {
212 if (staged_changes)
213 string_list_append(&files_cached, name);
214 if (local_changes) {
215 if (S_ISGITLINK(ce->ce_mode) &&
216 !submodule_uses_gitfile(name))
217 string_list_append(&files_submodule, name);
218 else
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:",
228 files_staged.nr),
229 _("\n(use -f to force removal)"),
230 &errs);
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)"),
239 &errs);
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:",
246 files_submodule.nr),
247 _("\n(use 'rm -rf' if you really "
248 "want to remove it including all "
249 "of its history)"),
250 &errs);
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:",
255 files_local.nr),
256 _("\n(use --cached to keep the file,"
257 " or -f to force removal)"),
258 &errs);
259 string_list_clear(&files_local, 0);
261 return errs;
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")),
277 OPT_END(),
280 int cmd_rm(int argc, const char **argv, const char *prefix)
282 int i, newfd;
283 struct pathspec pathspec;
284 char *seen;
286 git_config(git_default_config, NULL);
288 argc = parse_options(argc, argv, prefix, builtin_rm_options,
289 builtin_rm_usage, 0);
290 if (!argc)
291 usage_with_options(builtin_rm_usage, builtin_rm_options);
293 if (!index_only)
294 setup_work_tree();
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])) {
309 do {
310 pathlen--;
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);
319 seen = xcalloc(pathspec.nr, 1);
321 for (i = 0; i < active_nr; i++) {
322 struct cache_entry *ce = active_cache[i];
323 if (!match_pathspec_depth(&pathspec, ce->name, ce_namelen(ce), 0, seen))
324 continue;
325 ALLOC_GROW(list.entry, list.nr + 1, list.alloc);
326 list.entry[list.nr].name = ce->name;
327 list.entry[list.nr++].is_submodule = S_ISGITLINK(ce->ce_mode);
330 if (pathspec.nr) {
331 const char *original;
332 int seen_any = 0;
333 for (i = 0; i < pathspec.nr; i++) {
334 original = pathspec.items[i].original;
335 if (!seen[i]) {
336 if (!ignore_unmatch) {
337 die(_("pathspec '%s' did not match any files"),
338 original);
341 else {
342 seen_any = 1;
344 if (!recursive && seen[i] == MATCHED_RECURSIVELY)
345 die(_("not removing '%s' recursively without -r"),
346 *original ? original : ".");
349 if (! seen_any)
350 exit(0);
354 * If not forced, the file, the index and the HEAD (if exists)
355 * must match; but the file can already been removed, since
356 * this sequence is a natural "novice" way:
358 * rm F; git rm F
360 * Further, if HEAD commit exists, "diff-index --cached" must
361 * report no changes unless forced.
363 if (!force) {
364 unsigned char sha1[20];
365 if (get_sha1("HEAD", sha1))
366 hashclr(sha1);
367 if (check_local_mod(sha1, index_only))
368 exit(1);
369 } else if (!index_only) {
370 if (check_submodules_use_gitfiles())
371 exit(1);
375 * First remove the names from the index: we won't commit
376 * the index unless all of them succeed.
378 for (i = 0; i < list.nr; i++) {
379 const char *path = list.entry[i].name;
380 if (!quiet)
381 printf("rm '%s'\n", path);
383 if (remove_file_from_cache(path))
384 die(_("git rm: unable to remove %s"), path);
387 if (show_only)
388 return 0;
391 * Then, unless we used "--cached", remove the filenames from
392 * the workspace. If we fail to remove the first one, we
393 * abort the "git rm" (but once we've successfully removed
394 * any file at all, we'll go ahead and commit to it all:
395 * by then we've already committed ourselves and can't fail
396 * in the middle)
398 if (!index_only) {
399 int removed = 0;
400 for (i = 0; i < list.nr; i++) {
401 const char *path = list.entry[i].name;
402 if (list.entry[i].is_submodule) {
403 if (is_empty_dir(path)) {
404 if (!rmdir(path)) {
405 removed = 1;
406 continue;
408 } else {
409 struct strbuf buf = STRBUF_INIT;
410 strbuf_addstr(&buf, path);
411 if (!remove_dir_recursively(&buf, 0)) {
412 removed = 1;
413 strbuf_release(&buf);
414 continue;
416 strbuf_release(&buf);
417 /* Fallthrough and let remove_path() fail. */
420 if (!remove_path(path)) {
421 removed = 1;
422 continue;
424 if (!removed)
425 die_errno("git rm: '%s'", path);
429 if (active_cache_changed) {
430 if (write_cache(newfd, active_cache, active_nr) ||
431 commit_locked_index(&lock_file))
432 die(_("Unable to write new index file"));
435 return 0;