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