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