]>
Commit | Line | Data |
---|---|---|
d9b814cc LT |
1 | /* |
2 | * "git rm" builtin command | |
3 | * | |
4 | * Copyright (C) Linus Torvalds 2006 | |
5 | */ | |
6193aaa9 | 6 | #define USE_THE_INDEX_VARIABLE |
d9b814cc | 7 | #include "builtin.h" |
36bf1958 | 8 | #include "alloc.h" |
d5f4b826 | 9 | #include "advice.h" |
b2141fc1 | 10 | #include "config.h" |
697cc8ef | 11 | #include "lockfile.h" |
d9b814cc | 12 | #include "dir.h" |
3fb3cc69 | 13 | #include "cache-tree.h" |
9f95069b | 14 | #include "tree-walk.h" |
f09985c2 | 15 | #include "parse-options.h" |
914dc028 | 16 | #include "string-list.h" |
293ab15e | 17 | #include "submodule.h" |
29211a93 | 18 | #include "pathspec.h" |
d9b814cc | 19 | |
f09985c2 | 20 | static const char * const builtin_rm_usage[] = { |
d9054a19 ÆAB |
21 | N_("git rm [-f | --force] [-n] [-r] [--cached] [--ignore-unmatch]\n" |
22 | " [--quiet] [--pathspec-from-file=<file> [--pathspec-file-nul]]\n" | |
23 | " [--] [<pathspec>...]"), | |
f09985c2 PH |
24 | NULL |
25 | }; | |
d9b814cc LT |
26 | |
27 | static struct { | |
28 | int nr, alloc; | |
293ab15e JL |
29 | struct { |
30 | const char *name; | |
31 | char is_submodule; | |
32 | } *entry; | |
d9b814cc LT |
33 | } list; |
34 | ||
293ab15e JL |
35 | static int get_ours_cache_pos(const char *path, int pos) |
36 | { | |
37 | int i = -pos - 1; | |
38 | ||
dc594180 ÆAB |
39 | while ((i < the_index.cache_nr) && !strcmp(the_index.cache[i]->name, path)) { |
40 | if (ce_stage(the_index.cache[i]) == 2) | |
293ab15e JL |
41 | return i; |
42 | i++; | |
43 | } | |
44 | return -1; | |
45 | } | |
46 | ||
914dc028 MLM |
47 | static void print_error_files(struct string_list *files_list, |
48 | const char *main_msg, | |
49 | const char *hints_msg, | |
50 | int *errs) | |
51 | { | |
52 | if (files_list->nr) { | |
53 | int i; | |
54 | struct strbuf err_msg = STRBUF_INIT; | |
55 | ||
56 | strbuf_addstr(&err_msg, main_msg); | |
57 | for (i = 0; i < files_list->nr; i++) | |
58 | strbuf_addf(&err_msg, | |
59 | "\n %s", | |
60 | files_list->items[i].string); | |
ed9bff08 | 61 | if (advice_enabled(ADVICE_RM_HINTS)) |
7e309446 | 62 | strbuf_addstr(&err_msg, hints_msg); |
914dc028 MLM |
63 | *errs = error("%s", err_msg.buf); |
64 | strbuf_release(&err_msg); | |
65 | } | |
66 | } | |
67 | ||
cf7a901a | 68 | static void submodules_absorb_gitdir_if_needed(void) |
293ab15e JL |
69 | { |
70 | int i; | |
293ab15e JL |
71 | for (i = 0; i < list.nr; i++) { |
72 | const char *name = list.entry[i].name; | |
73 | int pos; | |
9c5e6c80 | 74 | const struct cache_entry *ce; |
293ab15e | 75 | |
07047d68 | 76 | pos = index_name_pos(&the_index, name, strlen(name)); |
293ab15e JL |
77 | if (pos < 0) { |
78 | pos = get_ours_cache_pos(name, pos); | |
79 | if (pos < 0) | |
80 | continue; | |
81 | } | |
dc594180 | 82 | ce = the_index.cache[pos]; |
293ab15e JL |
83 | |
84 | if (!S_ISGITLINK(ce->ce_mode) || | |
dbe44faa | 85 | !file_exists(ce->name) || |
293ab15e JL |
86 | is_empty_dir(name)) |
87 | continue; | |
88 | ||
89 | if (!submodule_uses_gitfile(name)) | |
f0a5e5ad | 90 | absorb_git_dir_into_superproject(name, NULL); |
293ab15e | 91 | } |
293ab15e JL |
92 | } |
93 | ||
8ec46d7e | 94 | static int check_local_mod(struct object_id *head, int index_only) |
9f95069b | 95 | { |
69530cb0 JH |
96 | /* |
97 | * Items in list are already sorted in the cache order, | |
9f95069b JH |
98 | * so we could do this a lot more efficiently by using |
99 | * tree_desc based traversal if we wanted to, but I am | |
100 | * lazy, and who cares if removal of files is a tad | |
101 | * slower than the theoretical maximum speed? | |
102 | */ | |
103 | int i, no_head; | |
104 | int errs = 0; | |
914dc028 MLM |
105 | struct string_list files_staged = STRING_LIST_INIT_NODUP; |
106 | struct string_list files_cached = STRING_LIST_INIT_NODUP; | |
914dc028 | 107 | struct string_list files_local = STRING_LIST_INIT_NODUP; |
9f95069b | 108 | |
8ec46d7e | 109 | no_head = is_null_oid(head); |
9f95069b JH |
110 | for (i = 0; i < list.nr; i++) { |
111 | struct stat st; | |
112 | int pos; | |
9c5e6c80 | 113 | const struct cache_entry *ce; |
293ab15e | 114 | const char *name = list.entry[i].name; |
8ec46d7e | 115 | struct object_id oid; |
5ec1e728 | 116 | unsigned short mode; |
bdecd9d4 MM |
117 | int local_changes = 0; |
118 | int staged_changes = 0; | |
9f95069b | 119 | |
07047d68 | 120 | pos = index_name_pos(&the_index, name, strlen(name)); |
293ab15e JL |
121 | if (pos < 0) { |
122 | /* | |
123 | * Skip unmerged entries except for populated submodules | |
124 | * that could lose history when removed. | |
125 | */ | |
126 | pos = get_ours_cache_pos(name, pos); | |
127 | if (pos < 0) | |
128 | continue; | |
129 | ||
dc594180 | 130 | if (!S_ISGITLINK(the_index.cache[pos]->ce_mode) || |
293ab15e JL |
131 | is_empty_dir(name)) |
132 | continue; | |
133 | } | |
dc594180 | 134 | ce = the_index.cache[pos]; |
9f95069b JH |
135 | |
136 | if (lstat(ce->name, &st) < 0) { | |
c7054209 | 137 | if (!is_missing_file_error(errno)) |
7dcf3d97 | 138 | warning_errno(_("failed to stat '%s'"), ce->name); |
9f95069b JH |
139 | /* It already vanished from the working tree */ |
140 | continue; | |
141 | } | |
142 | else if (S_ISDIR(st.st_mode)) { | |
143 | /* if a file was removed and it is now a | |
144 | * directory, that is the same as ENOENT as | |
145 | * far as git is concerned; we do not track | |
293ab15e | 146 | * directories unless they are submodules. |
9f95069b | 147 | */ |
293ab15e JL |
148 | if (!S_ISGITLINK(ce->ce_mode)) |
149 | continue; | |
9f95069b | 150 | } |
69530cb0 JH |
151 | |
152 | /* | |
153 | * "rm" of a path that has changes need to be treated | |
154 | * carefully not to allow losing local changes | |
155 | * accidentally. A local change could be (1) file in | |
156 | * work tree is different since the index; and/or (2) | |
157 | * the user staged a content that is different from | |
158 | * the current commit in the index. | |
159 | * | |
160 | * In such a case, you would need to --force the | |
161 | * removal. However, "rm --cached" (remove only from | |
162 | * the index) is safe if the index matches the file in | |
163 | * the work tree or the HEAD commit, as it means that | |
164 | * the content being removed is available elsewhere. | |
165 | */ | |
166 | ||
167 | /* | |
168 | * Is the index different from the file in the work tree? | |
293ab15e | 169 | * If it's a submodule, is its work tree modified? |
69530cb0 | 170 | */ |
031b2033 | 171 | if (ie_match_stat(&the_index, ce, &st, 0) || |
293ab15e | 172 | (S_ISGITLINK(ce->ce_mode) && |
83b76966 SB |
173 | bad_to_remove_submodule(ce->name, |
174 | SUBMODULE_REMOVAL_DIE_ON_ERROR | | |
175 | SUBMODULE_REMOVAL_IGNORE_IGNORED_UNTRACKED))) | |
bdecd9d4 | 176 | local_changes = 1; |
69530cb0 JH |
177 | |
178 | /* | |
179 | * Is the index different from the HEAD commit? By | |
180 | * definition, before the very initial commit, | |
181 | * anything staged in the index is treated by the same | |
182 | * way as changed from the HEAD. | |
183 | */ | |
e4d9516b | 184 | if (no_head |
50ddb089 | 185 | || get_tree_entry(the_repository, head, name, &oid, &mode) |
e4d9516b | 186 | || ce->ce_mode != create_ce_mode(mode) |
9001dc2a | 187 | || !oideq(&ce->oid, &oid)) |
bdecd9d4 MM |
188 | staged_changes = 1; |
189 | ||
69530cb0 JH |
190 | /* |
191 | * If the index does not match the file in the work | |
192 | * tree and if it does not match the HEAD commit | |
193 | * either, (1) "git rm" without --cached definitely | |
194 | * will lose information; (2) "git rm --cached" will | |
195 | * lose information unless it is about removing an | |
196 | * "intent to add" entry. | |
197 | */ | |
198 | if (local_changes && staged_changes) { | |
895ff3b2 | 199 | if (!index_only || !ce_intent_to_add(ce)) |
914dc028 | 200 | string_list_append(&files_staged, name); |
69530cb0 | 201 | } |
bdecd9d4 | 202 | else if (!index_only) { |
bdecd9d4 | 203 | if (staged_changes) |
914dc028 | 204 | string_list_append(&files_cached, name); |
55856a35 SB |
205 | if (local_changes) |
206 | string_list_append(&files_local, name); | |
bdecd9d4 | 207 | } |
9f95069b | 208 | } |
914dc028 MLM |
209 | print_error_files(&files_staged, |
210 | Q_("the following file has staged content different " | |
211 | "from both the\nfile and the HEAD:", | |
212 | "the following files have staged content different" | |
213 | " from both the\nfile and the HEAD:", | |
214 | files_staged.nr), | |
215 | _("\n(use -f to force removal)"), | |
216 | &errs); | |
217 | string_list_clear(&files_staged, 0); | |
218 | print_error_files(&files_cached, | |
219 | Q_("the following file has changes " | |
220 | "staged in the index:", | |
221 | "the following files have changes " | |
222 | "staged in the index:", files_cached.nr), | |
223 | _("\n(use --cached to keep the file," | |
224 | " or -f to force removal)"), | |
225 | &errs); | |
226 | string_list_clear(&files_cached, 0); | |
658ff473 | 227 | |
914dc028 MLM |
228 | print_error_files(&files_local, |
229 | Q_("the following file has local modifications:", | |
230 | "the following files have local modifications:", | |
231 | files_local.nr), | |
232 | _("\n(use --cached to keep the file," | |
233 | " or -f to force removal)"), | |
234 | &errs); | |
235 | string_list_clear(&files_local, 0); | |
236 | ||
9f95069b JH |
237 | return errs; |
238 | } | |
239 | ||
f09985c2 | 240 | static int show_only = 0, force = 0, index_only = 0, recursive = 0, quiet = 0; |
5f393dc3 | 241 | static int ignore_unmatch = 0, pathspec_file_nul; |
f9786f9b | 242 | static int include_sparse; |
5f393dc3 | 243 | static char *pathspec_from_file; |
f09985c2 PH |
244 | |
245 | static struct option builtin_rm_options[] = { | |
72bba2a6 NTND |
246 | OPT__DRY_RUN(&show_only, N_("dry run")), |
247 | OPT__QUIET(&quiet, N_("do not list removed files")), | |
d5d09d47 | 248 | OPT_BOOL( 0 , "cached", &index_only, N_("only remove from the index")), |
44c9a6d2 | 249 | OPT__FORCE(&force, N_("override the up-to-date check"), PARSE_OPT_NOCOMPLETE), |
d5d09d47 SB |
250 | OPT_BOOL('r', NULL, &recursive, N_("allow recursive removal")), |
251 | OPT_BOOL( 0 , "ignore-unmatch", &ignore_unmatch, | |
72bba2a6 | 252 | N_("exit with a zero status even if nothing matched")), |
f9786f9b | 253 | OPT_BOOL(0, "sparse", &include_sparse, N_("allow updating entries outside of the sparse-checkout cone")), |
5f393dc3 AM |
254 | OPT_PATHSPEC_FROM_FILE(&pathspec_from_file), |
255 | OPT_PATHSPEC_FILE_NUL(&pathspec_file_nul), | |
f09985c2 PH |
256 | OPT_END(), |
257 | }; | |
258 | ||
a633fca0 | 259 | int cmd_rm(int argc, const char **argv, const char *prefix) |
d9b814cc | 260 | { |
0fa5a2ed | 261 | struct lock_file lock_file = LOCK_INIT; |
d5f4b826 | 262 | int i, ret = 0; |
29211a93 | 263 | struct pathspec pathspec; |
d9b814cc LT |
264 | char *seen; |
265 | ||
ef90d6d4 | 266 | git_config(git_default_config, NULL); |
d9b814cc | 267 | |
37782920 SB |
268 | argc = parse_options(argc, argv, prefix, builtin_rm_options, |
269 | builtin_rm_usage, 0); | |
5f393dc3 AM |
270 | |
271 | parse_pathspec(&pathspec, 0, | |
272 | PATHSPEC_PREFER_CWD, | |
273 | prefix, argv); | |
274 | ||
275 | if (pathspec_from_file) { | |
276 | if (pathspec.nr) | |
246cac85 | 277 | die(_("'%s' and pathspec arguments cannot be used together"), "--pathspec-from-file"); |
5f393dc3 AM |
278 | |
279 | parse_pathspec_file(&pathspec, 0, | |
280 | PATHSPEC_PREFER_CWD, | |
281 | prefix, pathspec_from_file, pathspec_file_nul); | |
282 | } else if (pathspec_file_nul) { | |
6fa00ee8 | 283 | die(_("the option '%s' requires '%s'"), "--pathspec-file-nul", "--pathspec-from-file"); |
5f393dc3 AM |
284 | } |
285 | ||
286 | if (!pathspec.nr) | |
287 | die(_("No pathspec was given. Which files should I remove?")); | |
d9b814cc | 288 | |
271bb087 MH |
289 | if (!index_only) |
290 | setup_work_tree(); | |
291 | ||
ede241c7 SY |
292 | prepare_repo_settings(the_repository); |
293 | the_repository->settings.command_requires_full_index = 0; | |
07047d68 | 294 | repo_hold_locked_index(the_repository, &lock_file, LOCK_DIE_ON_ERROR); |
4d264672 | 295 | |
07047d68 | 296 | if (repo_read_index(the_repository) < 0) |
b9b537f7 | 297 | die(_("index file corrupt")); |
4d264672 | 298 | |
b2b1f615 | 299 | refresh_index(&the_index, REFRESH_QUIET|REFRESH_UNMERGED, &pathspec, NULL, NULL); |
4e1a7baa | 300 | |
29211a93 | 301 | seen = xcalloc(pathspec.nr, 1); |
d9b814cc | 302 | |
bcf96cfc SY |
303 | if (pathspec_needs_expanded_index(&the_index, &pathspec)) |
304 | ensure_full_index(&the_index); | |
305 | ||
dc594180 ÆAB |
306 | for (i = 0; i < the_index.cache_nr; i++) { |
307 | const struct cache_entry *ce = the_index.cache[i]; | |
f9786f9b | 308 | |
d7c4415e DS |
309 | if (!include_sparse && |
310 | (ce_skip_worktree(ce) || | |
311 | !path_in_sparse_checkout(ce->name, &the_index))) | |
d5f4b826 | 312 | continue; |
6d2df284 | 313 | if (!ce_path_match(&the_index, ce, &pathspec, seen)) |
d9b814cc | 314 | continue; |
293ab15e | 315 | ALLOC_GROW(list.entry, list.nr + 1, list.alloc); |
5699d17e | 316 | list.entry[list.nr].name = xstrdup(ce->name); |
95c16418 JL |
317 | list.entry[list.nr].is_submodule = S_ISGITLINK(ce->ce_mode); |
318 | if (list.entry[list.nr++].is_submodule && | |
91b83480 | 319 | !is_staging_gitmodules_ok(&the_index)) |
1a07e59c | 320 | die(_("please stage your changes to .gitmodules or stash them to proceed")); |
d9b814cc LT |
321 | } |
322 | ||
29211a93 NTND |
323 | if (pathspec.nr) { |
324 | const char *original; | |
bb1faf0d | 325 | int seen_any = 0; |
d5f4b826 MT |
326 | char *skip_worktree_seen = NULL; |
327 | struct string_list only_match_skip_worktree = STRING_LIST_INIT_NODUP; | |
328 | ||
29211a93 NTND |
329 | for (i = 0; i < pathspec.nr; i++) { |
330 | original = pathspec.items[i].original; | |
d5f4b826 | 331 | if (seen[i]) |
bb1faf0d | 332 | seen_any = 1; |
d5f4b826 MT |
333 | else if (ignore_unmatch) |
334 | continue; | |
f9786f9b DS |
335 | else if (!include_sparse && |
336 | matches_skip_worktree(&pathspec, i, &skip_worktree_seen)) | |
d5f4b826 MT |
337 | string_list_append(&only_match_skip_worktree, original); |
338 | else | |
339 | die(_("pathspec '%s' did not match any files"), original); | |
340 | ||
9f95069b | 341 | if (!recursive && seen[i] == MATCHED_RECURSIVELY) |
b9b537f7 | 342 | die(_("not removing '%s' recursively without -r"), |
29211a93 | 343 | *original ? original : "."); |
d9b814cc | 344 | } |
bb1faf0d | 345 | |
d5f4b826 MT |
346 | if (only_match_skip_worktree.nr) { |
347 | advise_on_updating_sparse_paths(&only_match_skip_worktree); | |
348 | ret = 1; | |
349 | } | |
350 | free(skip_worktree_seen); | |
351 | string_list_clear(&only_match_skip_worktree, 0); | |
352 | ||
b02f5aed | 353 | if (!seen_any) |
d5f4b826 | 354 | exit(ret); |
d9b814cc | 355 | } |
37be1199 AH |
356 | clear_pathspec(&pathspec); |
357 | free(seen); | |
d9b814cc | 358 | |
55856a35 | 359 | if (!index_only) |
cf7a901a | 360 | submodules_absorb_gitdir_if_needed(); |
55856a35 | 361 | |
9f95069b JH |
362 | /* |
363 | * If not forced, the file, the index and the HEAD (if exists) | |
364 | * must match; but the file can already been removed, since | |
365 | * this sequence is a natural "novice" way: | |
366 | * | |
9474eda6 | 367 | * rm F; git rm F |
9f95069b JH |
368 | * |
369 | * Further, if HEAD commit exists, "diff-index --cached" must | |
370 | * report no changes unless forced. | |
371 | */ | |
372 | if (!force) { | |
8ec46d7e | 373 | struct object_id oid; |
374 | if (get_oid("HEAD", &oid)) | |
375 | oidclr(&oid); | |
376 | if (check_local_mod(&oid, index_only)) | |
9f95069b JH |
377 | exit(1); |
378 | } | |
379 | ||
d9b814cc LT |
380 | /* |
381 | * First remove the names from the index: we won't commit | |
9f95069b | 382 | * the index unless all of them succeed. |
d9b814cc LT |
383 | */ |
384 | for (i = 0; i < list.nr; i++) { | |
293ab15e | 385 | const char *path = list.entry[i].name; |
b48caa20 SG |
386 | if (!quiet) |
387 | printf("rm '%s'\n", path); | |
d9b814cc | 388 | |
031b2033 | 389 | if (remove_file_from_index(&the_index, path)) |
b9b537f7 | 390 | die(_("git rm: unable to remove %s"), path); |
d9b814cc LT |
391 | } |
392 | ||
7612a1ef JH |
393 | if (show_only) |
394 | return 0; | |
395 | ||
d9b814cc | 396 | /* |
646ac22b | 397 | * Then, unless we used "--cached", remove the filenames from |
9f95069b | 398 | * the workspace. If we fail to remove the first one, we |
d9b814cc LT |
399 | * abort the "git rm" (but once we've successfully removed |
400 | * any file at all, we'll go ahead and commit to it all: | |
82e5a82f | 401 | * by then we've already committed ourselves and can't fail |
d9b814cc LT |
402 | * in the middle) |
403 | */ | |
9f95069b | 404 | if (!index_only) { |
95c16418 | 405 | int removed = 0, gitmodules_modified = 0; |
590fc052 | 406 | struct strbuf buf = STRBUF_INIT; |
580a5d7f | 407 | int flag = force ? REMOVE_DIR_PURGE_ORIGINAL_CWD : 0; |
d9b814cc | 408 | for (i = 0; i < list.nr; i++) { |
293ab15e JL |
409 | const char *path = list.entry[i].name; |
410 | if (list.entry[i].is_submodule) { | |
590fc052 | 411 | strbuf_reset(&buf); |
55856a35 | 412 | strbuf_addstr(&buf, path); |
580a5d7f | 413 | if (remove_dir_recursively(&buf, flag)) |
55856a35 | 414 | die(_("could not remove '%s'"), path); |
55856a35 SB |
415 | |
416 | removed = 1; | |
417 | if (!remove_path_from_gitmodules(path)) | |
418 | gitmodules_modified = 1; | |
419 | continue; | |
293ab15e | 420 | } |
175a4948 | 421 | if (!remove_path(path)) { |
d9b814cc LT |
422 | removed = 1; |
423 | continue; | |
424 | } | |
425 | if (!removed) | |
d824cbba | 426 | die_errno("git rm: '%s'", path); |
d9b814cc | 427 | } |
590fc052 | 428 | strbuf_release(&buf); |
95c16418 | 429 | if (gitmodules_modified) |
3b8317a9 | 430 | stage_updated_gitmodules(&the_index); |
d9b814cc LT |
431 | } |
432 | ||
61000814 MÅ |
433 | if (write_locked_index(&the_index, &lock_file, |
434 | COMMIT_LOCK | SKIP_IF_UNCHANGED)) | |
435 | die(_("Unable to write new index file")); | |
d9b814cc | 436 | |
d5f4b826 | 437 | return ret; |
d9b814cc | 438 | } |