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