Sync with 2.38.5
[git.git] / builtin / rm.c
blobd4989d4d8637f6054f763ba516366a7eca7f316e
1 /*
2 * "git rm" builtin command
4 * Copyright (C) Linus Torvalds 2006
5 */
6 #define USE_THE_INDEX_COMPATIBILITY_MACROS
7 #include "builtin.h"
8 #include "advice.h"
9 #include "config.h"
10 #include "lockfile.h"
11 #include "dir.h"
12 #include "cache-tree.h"
13 #include "tree-walk.h"
14 #include "parse-options.h"
15 #include "string-list.h"
16 #include "submodule.h"
17 #include "pathspec.h"
19 static const char * const builtin_rm_usage[] = {
20 N_("git rm [-f | --force] [-n] [-r] [--cached] [--ignore-unmatch]\n"
21 " [--quiet] [--pathspec-from-file=<file> [--pathspec-file-nul]]\n"
22 " [--] [<pathspec>...]"),
23 NULL
26 static struct {
27 int nr, alloc;
28 struct {
29 const char *name;
30 char is_submodule;
31 } *entry;
32 } list;
34 static int get_ours_cache_pos(const char *path, int pos)
36 int i = -pos - 1;
38 while ((i < the_index.cache_nr) && !strcmp(the_index.cache[i]->name, path)) {
39 if (ce_stage(the_index.cache[i]) == 2)
40 return i;
41 i++;
43 return -1;
46 static void print_error_files(struct string_list *files_list,
47 const char *main_msg,
48 const char *hints_msg,
49 int *errs)
51 if (files_list->nr) {
52 int i;
53 struct strbuf err_msg = STRBUF_INIT;
55 strbuf_addstr(&err_msg, main_msg);
56 for (i = 0; i < files_list->nr; i++)
57 strbuf_addf(&err_msg,
58 "\n %s",
59 files_list->items[i].string);
60 if (advice_enabled(ADVICE_RM_HINTS))
61 strbuf_addstr(&err_msg, hints_msg);
62 *errs = error("%s", err_msg.buf);
63 strbuf_release(&err_msg);
67 static void submodules_absorb_gitdir_if_needed(void)
69 int i;
70 for (i = 0; i < list.nr; i++) {
71 const char *name = list.entry[i].name;
72 int pos;
73 const struct cache_entry *ce;
75 pos = index_name_pos(&the_index, name, strlen(name));
76 if (pos < 0) {
77 pos = get_ours_cache_pos(name, pos);
78 if (pos < 0)
79 continue;
81 ce = the_index.cache[pos];
83 if (!S_ISGITLINK(ce->ce_mode) ||
84 !file_exists(ce->name) ||
85 is_empty_dir(name))
86 continue;
88 if (!submodule_uses_gitfile(name))
89 absorb_git_dir_into_superproject(name);
93 static int check_local_mod(struct object_id *head, int index_only)
96 * Items in list are already sorted in the cache order,
97 * so we could do this a lot more efficiently by using
98 * tree_desc based traversal if we wanted to, but I am
99 * lazy, and who cares if removal of files is a tad
100 * slower than the theoretical maximum speed?
102 int i, no_head;
103 int errs = 0;
104 struct string_list files_staged = STRING_LIST_INIT_NODUP;
105 struct string_list files_cached = STRING_LIST_INIT_NODUP;
106 struct string_list files_local = STRING_LIST_INIT_NODUP;
108 no_head = is_null_oid(head);
109 for (i = 0; i < list.nr; i++) {
110 struct stat st;
111 int pos;
112 const struct cache_entry *ce;
113 const char *name = list.entry[i].name;
114 struct object_id oid;
115 unsigned short mode;
116 int local_changes = 0;
117 int staged_changes = 0;
119 pos = index_name_pos(&the_index, name, strlen(name));
120 if (pos < 0) {
122 * Skip unmerged entries except for populated submodules
123 * that could lose history when removed.
125 pos = get_ours_cache_pos(name, pos);
126 if (pos < 0)
127 continue;
129 if (!S_ISGITLINK(the_index.cache[pos]->ce_mode) ||
130 is_empty_dir(name))
131 continue;
133 ce = the_index.cache[pos];
135 if (lstat(ce->name, &st) < 0) {
136 if (!is_missing_file_error(errno))
137 warning_errno(_("failed to stat '%s'"), ce->name);
138 /* It already vanished from the working tree */
139 continue;
141 else if (S_ISDIR(st.st_mode)) {
142 /* if a file was removed and it is now a
143 * directory, that is the same as ENOENT as
144 * far as git is concerned; we do not track
145 * directories unless they are submodules.
147 if (!S_ISGITLINK(ce->ce_mode))
148 continue;
152 * "rm" of a path that has changes need to be treated
153 * carefully not to allow losing local changes
154 * accidentally. A local change could be (1) file in
155 * work tree is different since the index; and/or (2)
156 * the user staged a content that is different from
157 * the current commit in the index.
159 * In such a case, you would need to --force the
160 * removal. However, "rm --cached" (remove only from
161 * the index) is safe if the index matches the file in
162 * the work tree or the HEAD commit, as it means that
163 * the content being removed is available elsewhere.
167 * Is the index different from the file in the work tree?
168 * If it's a submodule, is its work tree modified?
170 if (ie_match_stat(&the_index, ce, &st, 0) ||
171 (S_ISGITLINK(ce->ce_mode) &&
172 bad_to_remove_submodule(ce->name,
173 SUBMODULE_REMOVAL_DIE_ON_ERROR |
174 SUBMODULE_REMOVAL_IGNORE_IGNORED_UNTRACKED)))
175 local_changes = 1;
178 * Is the index different from the HEAD commit? By
179 * definition, before the very initial commit,
180 * anything staged in the index is treated by the same
181 * way as changed from the HEAD.
183 if (no_head
184 || get_tree_entry(the_repository, head, name, &oid, &mode)
185 || ce->ce_mode != create_ce_mode(mode)
186 || !oideq(&ce->oid, &oid))
187 staged_changes = 1;
190 * If the index does not match the file in the work
191 * tree and if it does not match the HEAD commit
192 * either, (1) "git rm" without --cached definitely
193 * will lose information; (2) "git rm --cached" will
194 * lose information unless it is about removing an
195 * "intent to add" entry.
197 if (local_changes && staged_changes) {
198 if (!index_only || !ce_intent_to_add(ce))
199 string_list_append(&files_staged, name);
201 else if (!index_only) {
202 if (staged_changes)
203 string_list_append(&files_cached, name);
204 if (local_changes)
205 string_list_append(&files_local, name);
208 print_error_files(&files_staged,
209 Q_("the following file has staged content different "
210 "from both the\nfile and the HEAD:",
211 "the following files have staged content different"
212 " from both the\nfile and the HEAD:",
213 files_staged.nr),
214 _("\n(use -f to force removal)"),
215 &errs);
216 string_list_clear(&files_staged, 0);
217 print_error_files(&files_cached,
218 Q_("the following file has changes "
219 "staged in the index:",
220 "the following files have changes "
221 "staged in the index:", files_cached.nr),
222 _("\n(use --cached to keep the file,"
223 " or -f to force removal)"),
224 &errs);
225 string_list_clear(&files_cached, 0);
227 print_error_files(&files_local,
228 Q_("the following file has local modifications:",
229 "the following files have local modifications:",
230 files_local.nr),
231 _("\n(use --cached to keep the file,"
232 " or -f to force removal)"),
233 &errs);
234 string_list_clear(&files_local, 0);
236 return errs;
239 static int show_only = 0, force = 0, index_only = 0, recursive = 0, quiet = 0;
240 static int ignore_unmatch = 0, pathspec_file_nul;
241 static int include_sparse;
242 static char *pathspec_from_file;
244 static struct option builtin_rm_options[] = {
245 OPT__DRY_RUN(&show_only, N_("dry run")),
246 OPT__QUIET(&quiet, N_("do not list removed files")),
247 OPT_BOOL( 0 , "cached", &index_only, N_("only remove from the index")),
248 OPT__FORCE(&force, N_("override the up-to-date check"), PARSE_OPT_NOCOMPLETE),
249 OPT_BOOL('r', NULL, &recursive, N_("allow recursive removal")),
250 OPT_BOOL( 0 , "ignore-unmatch", &ignore_unmatch,
251 N_("exit with a zero status even if nothing matched")),
252 OPT_BOOL(0, "sparse", &include_sparse, N_("allow updating entries outside of the sparse-checkout cone")),
253 OPT_PATHSPEC_FROM_FILE(&pathspec_from_file),
254 OPT_PATHSPEC_FILE_NUL(&pathspec_file_nul),
255 OPT_END(),
258 int cmd_rm(int argc, const char **argv, const char *prefix)
260 struct lock_file lock_file = LOCK_INIT;
261 int i, ret = 0;
262 struct pathspec pathspec;
263 char *seen;
265 git_config(git_default_config, NULL);
267 argc = parse_options(argc, argv, prefix, builtin_rm_options,
268 builtin_rm_usage, 0);
270 parse_pathspec(&pathspec, 0,
271 PATHSPEC_PREFER_CWD,
272 prefix, argv);
274 if (pathspec_from_file) {
275 if (pathspec.nr)
276 die(_("'%s' and pathspec arguments cannot be used together"), "--pathspec-from-file");
278 parse_pathspec_file(&pathspec, 0,
279 PATHSPEC_PREFER_CWD,
280 prefix, pathspec_from_file, pathspec_file_nul);
281 } else if (pathspec_file_nul) {
282 die(_("the option '%s' requires '%s'"), "--pathspec-file-nul", "--pathspec-from-file");
285 if (!pathspec.nr)
286 die(_("No pathspec was given. Which files should I remove?"));
288 if (!index_only)
289 setup_work_tree();
291 prepare_repo_settings(the_repository);
292 the_repository->settings.command_requires_full_index = 0;
293 repo_hold_locked_index(the_repository, &lock_file, LOCK_DIE_ON_ERROR);
295 if (repo_read_index(the_repository) < 0)
296 die(_("index file corrupt"));
298 refresh_index(&the_index, REFRESH_QUIET|REFRESH_UNMERGED, &pathspec, NULL, NULL);
300 seen = xcalloc(pathspec.nr, 1);
302 if (pathspec_needs_expanded_index(&the_index, &pathspec))
303 ensure_full_index(&the_index);
305 for (i = 0; i < the_index.cache_nr; i++) {
306 const struct cache_entry *ce = the_index.cache[i];
308 if (!include_sparse &&
309 (ce_skip_worktree(ce) ||
310 !path_in_sparse_checkout(ce->name, &the_index)))
311 continue;
312 if (!ce_path_match(&the_index, ce, &pathspec, seen))
313 continue;
314 ALLOC_GROW(list.entry, list.nr + 1, list.alloc);
315 list.entry[list.nr].name = xstrdup(ce->name);
316 list.entry[list.nr].is_submodule = S_ISGITLINK(ce->ce_mode);
317 if (list.entry[list.nr++].is_submodule &&
318 !is_staging_gitmodules_ok(&the_index))
319 die(_("please stage your changes to .gitmodules or stash them to proceed"));
322 if (pathspec.nr) {
323 const char *original;
324 int seen_any = 0;
325 char *skip_worktree_seen = NULL;
326 struct string_list only_match_skip_worktree = STRING_LIST_INIT_NODUP;
328 for (i = 0; i < pathspec.nr; i++) {
329 original = pathspec.items[i].original;
330 if (seen[i])
331 seen_any = 1;
332 else if (ignore_unmatch)
333 continue;
334 else if (!include_sparse &&
335 matches_skip_worktree(&pathspec, i, &skip_worktree_seen))
336 string_list_append(&only_match_skip_worktree, original);
337 else
338 die(_("pathspec '%s' did not match any files"), original);
340 if (!recursive && seen[i] == MATCHED_RECURSIVELY)
341 die(_("not removing '%s' recursively without -r"),
342 *original ? original : ".");
345 if (only_match_skip_worktree.nr) {
346 advise_on_updating_sparse_paths(&only_match_skip_worktree);
347 ret = 1;
349 free(skip_worktree_seen);
350 string_list_clear(&only_match_skip_worktree, 0);
352 if (!seen_any)
353 exit(ret);
355 clear_pathspec(&pathspec);
356 free(seen);
358 if (!index_only)
359 submodules_absorb_gitdir_if_needed();
362 * If not forced, the file, the index and the HEAD (if exists)
363 * must match; but the file can already been removed, since
364 * this sequence is a natural "novice" way:
366 * rm F; git rm F
368 * Further, if HEAD commit exists, "diff-index --cached" must
369 * report no changes unless forced.
371 if (!force) {
372 struct object_id oid;
373 if (get_oid("HEAD", &oid))
374 oidclr(&oid);
375 if (check_local_mod(&oid, index_only))
376 exit(1);
380 * First remove the names from the index: we won't commit
381 * the index unless all of them succeed.
383 for (i = 0; i < list.nr; i++) {
384 const char *path = list.entry[i].name;
385 if (!quiet)
386 printf("rm '%s'\n", path);
388 if (remove_file_from_index(&the_index, path))
389 die(_("git rm: unable to remove %s"), path);
392 if (show_only)
393 return 0;
396 * Then, unless we used "--cached", remove the filenames from
397 * the workspace. If we fail to remove the first one, we
398 * abort the "git rm" (but once we've successfully removed
399 * any file at all, we'll go ahead and commit to it all:
400 * by then we've already committed ourselves and can't fail
401 * in the middle)
403 if (!index_only) {
404 int removed = 0, gitmodules_modified = 0;
405 struct strbuf buf = STRBUF_INIT;
406 int flag = force ? REMOVE_DIR_PURGE_ORIGINAL_CWD : 0;
407 for (i = 0; i < list.nr; i++) {
408 const char *path = list.entry[i].name;
409 if (list.entry[i].is_submodule) {
410 strbuf_reset(&buf);
411 strbuf_addstr(&buf, path);
412 if (remove_dir_recursively(&buf, flag))
413 die(_("could not remove '%s'"), path);
415 removed = 1;
416 if (!remove_path_from_gitmodules(path))
417 gitmodules_modified = 1;
418 continue;
420 if (!remove_path(path)) {
421 removed = 1;
422 continue;
424 if (!removed)
425 die_errno("git rm: '%s'", path);
427 strbuf_release(&buf);
428 if (gitmodules_modified)
429 stage_updated_gitmodules(&the_index);
432 if (write_locked_index(&the_index, &lock_file,
433 COMMIT_LOCK | SKIP_IF_UNCHANGED))
434 die(_("Unable to write new index file"));
436 return ret;