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