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