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