blob: fd130cea2d2592829e3abd991ca6caae4a119897 [file] [log] [blame]
Linus Torvaldsd9b814c2006-05-19 16:19:34 -07001/*
2 * "git rm" builtin command
3 *
4 * Copyright (C) Linus Torvalds 2006
5 */
Ævar Arnfjörð Bjarmason6193aaa2023-02-10 11:28:34 +01006#define USE_THE_INDEX_VARIABLE
Linus Torvaldsd9b814c2006-05-19 16:19:34 -07007#include "builtin.h"
Matheus Tavaresd5f4b822021-04-08 17:41:28 -03008#include "advice.h"
Brandon Williamsb2141fc2017-06-14 11:07:36 -07009#include "config.h"
Michael Haggerty697cc8e2014-10-01 12:28:42 +020010#include "lockfile.h"
Linus Torvaldsd9b814c2006-05-19 16:19:34 -070011#include "dir.h"
Elijah Newrenf394e092023-03-21 06:25:54 +000012#include "gettext.h"
Elijah Newrend1cbe1e2023-04-22 20:17:20 +000013#include "hash.h"
Junio C Hamano9f950692006-12-25 03:11:00 -080014#include "tree-walk.h"
Elijah Newrendabab1d2023-04-11 00:41:49 -070015#include "object-name.h"
Pierre Habouzitf09985c2007-10-05 21:09:19 +020016#include "parse-options.h"
Elijah Newren08c46a42023-05-16 06:33:56 +000017#include "read-cache.h"
Elijah Newrend1cbe1e2023-04-22 20:17:20 +000018#include "repository.h"
Mathieu Lienard--Mayor914dc022013-06-12 10:06:43 +020019#include "string-list.h"
Elijah Newrene38da482023-03-21 06:26:05 +000020#include "setup.h"
Elijah Newrenbaf889c2023-05-16 06:33:51 +000021#include "sparse-index.h"
Jens Lehmann293ab152012-09-26 20:21:13 +020022#include "submodule.h"
Nguyễn Thái Ngọc Duy29211a92013-07-14 15:35:42 +070023#include "pathspec.h"
Linus Torvaldsd9b814c2006-05-19 16:19:34 -070024
Pierre Habouzitf09985c2007-10-05 21:09:19 +020025static const char * const builtin_rm_usage[] = {
Ævar Arnfjörð Bjarmasond9054a12022-10-13 17:39:18 +020026 N_("git rm [-f | --force] [-n] [-r] [--cached] [--ignore-unmatch]\n"
27 " [--quiet] [--pathspec-from-file=<file> [--pathspec-file-nul]]\n"
28 " [--] [<pathspec>...]"),
Pierre Habouzitf09985c2007-10-05 21:09:19 +020029 NULL
30};
Linus Torvaldsd9b814c2006-05-19 16:19:34 -070031
32static struct {
33 int nr, alloc;
Jens Lehmann293ab152012-09-26 20:21:13 +020034 struct {
35 const char *name;
36 char is_submodule;
37 } *entry;
Linus Torvaldsd9b814c2006-05-19 16:19:34 -070038} list;
39
Jens Lehmann293ab152012-09-26 20:21:13 +020040static int get_ours_cache_pos(const char *path, int pos)
41{
42 int i = -pos - 1;
43
Ævar Arnfjörð Bjarmasondc594182022-11-19 14:07:34 +010044 while ((i < the_index.cache_nr) && !strcmp(the_index.cache[i]->name, path)) {
45 if (ce_stage(the_index.cache[i]) == 2)
Jens Lehmann293ab152012-09-26 20:21:13 +020046 return i;
47 i++;
48 }
49 return -1;
50}
51
Mathieu Lienard--Mayor914dc022013-06-12 10:06:43 +020052static void print_error_files(struct string_list *files_list,
53 const char *main_msg,
54 const char *hints_msg,
55 int *errs)
56{
57 if (files_list->nr) {
58 int i;
59 struct strbuf err_msg = STRBUF_INIT;
60
61 strbuf_addstr(&err_msg, main_msg);
62 for (i = 0; i < files_list->nr; i++)
63 strbuf_addf(&err_msg,
64 "\n %s",
65 files_list->items[i].string);
Ben Boeckeled9bff02021-08-23 12:44:00 +020066 if (advice_enabled(ADVICE_RM_HINTS))
Mathieu Lienard--Mayor7e309442013-06-12 10:06:44 +020067 strbuf_addstr(&err_msg, hints_msg);
Mathieu Lienard--Mayor914dc022013-06-12 10:06:43 +020068 *errs = error("%s", err_msg.buf);
69 strbuf_release(&err_msg);
70 }
71}
72
Jeff Kingcf7a9012019-05-09 17:27:31 -040073static void submodules_absorb_gitdir_if_needed(void)
Jens Lehmann293ab152012-09-26 20:21:13 +020074{
75 int i;
Jens Lehmann293ab152012-09-26 20:21:13 +020076 for (i = 0; i < list.nr; i++) {
77 const char *name = list.entry[i].name;
78 int pos;
Nguyễn Thái Ngọc Duy9c5e6c82013-07-09 22:29:00 +070079 const struct cache_entry *ce;
Jens Lehmann293ab152012-09-26 20:21:13 +020080
Ævar Arnfjörð Bjarmason07047d62022-11-19 14:07:38 +010081 pos = index_name_pos(&the_index, name, strlen(name));
Jens Lehmann293ab152012-09-26 20:21:13 +020082 if (pos < 0) {
83 pos = get_ours_cache_pos(name, pos);
84 if (pos < 0)
85 continue;
86 }
Ævar Arnfjörð Bjarmasondc594182022-11-19 14:07:34 +010087 ce = the_index.cache[pos];
Jens Lehmann293ab152012-09-26 20:21:13 +020088
89 if (!S_ISGITLINK(ce->ce_mode) ||
René Scharfedbe44fa2015-05-19 23:44:23 +020090 !file_exists(ce->name) ||
Jens Lehmann293ab152012-09-26 20:21:13 +020091 is_empty_dir(name))
92 continue;
93
94 if (!submodule_uses_gitfile(name))
Ævar Arnfjörð Bjarmasonf0a5e5a2022-12-20 13:39:50 +010095 absorb_git_dir_into_superproject(name, NULL);
Jens Lehmann293ab152012-09-26 20:21:13 +020096 }
Jens Lehmann293ab152012-09-26 20:21:13 +020097}
98
brian m. carlson8ec46d72016-09-05 20:08:04 +000099static int check_local_mod(struct object_id *head, int index_only)
Junio C Hamano9f950692006-12-25 03:11:00 -0800100{
Junio C Hamano69530cb2008-11-28 18:41:21 -0800101 /*
102 * Items in list are already sorted in the cache order,
Junio C Hamano9f950692006-12-25 03:11:00 -0800103 * so we could do this a lot more efficiently by using
104 * tree_desc based traversal if we wanted to, but I am
105 * lazy, and who cares if removal of files is a tad
106 * slower than the theoretical maximum speed?
107 */
108 int i, no_head;
109 int errs = 0;
Mathieu Lienard--Mayor914dc022013-06-12 10:06:43 +0200110 struct string_list files_staged = STRING_LIST_INIT_NODUP;
111 struct string_list files_cached = STRING_LIST_INIT_NODUP;
Mathieu Lienard--Mayor914dc022013-06-12 10:06:43 +0200112 struct string_list files_local = STRING_LIST_INIT_NODUP;
Junio C Hamano9f950692006-12-25 03:11:00 -0800113
brian m. carlson8ec46d72016-09-05 20:08:04 +0000114 no_head = is_null_oid(head);
Junio C Hamano9f950692006-12-25 03:11:00 -0800115 for (i = 0; i < list.nr; i++) {
116 struct stat st;
117 int pos;
Nguyễn Thái Ngọc Duy9c5e6c82013-07-09 22:29:00 +0700118 const struct cache_entry *ce;
Jens Lehmann293ab152012-09-26 20:21:13 +0200119 const char *name = list.entry[i].name;
brian m. carlson8ec46d72016-09-05 20:08:04 +0000120 struct object_id oid;
Elijah Newren5ec1e722019-04-05 08:00:12 -0700121 unsigned short mode;
Matthieu Moybdecd9d2007-07-13 19:41:38 +0200122 int local_changes = 0;
123 int staged_changes = 0;
Junio C Hamano9f950692006-12-25 03:11:00 -0800124
Ævar Arnfjörð Bjarmason07047d62022-11-19 14:07:38 +0100125 pos = index_name_pos(&the_index, name, strlen(name));
Jens Lehmann293ab152012-09-26 20:21:13 +0200126 if (pos < 0) {
127 /*
128 * Skip unmerged entries except for populated submodules
129 * that could lose history when removed.
130 */
131 pos = get_ours_cache_pos(name, pos);
132 if (pos < 0)
133 continue;
134
Ævar Arnfjörð Bjarmasondc594182022-11-19 14:07:34 +0100135 if (!S_ISGITLINK(the_index.cache[pos]->ce_mode) ||
Jens Lehmann293ab152012-09-26 20:21:13 +0200136 is_empty_dir(name))
137 continue;
138 }
Ævar Arnfjörð Bjarmasondc594182022-11-19 14:07:34 +0100139 ce = the_index.cache[pos];
Junio C Hamano9f950692006-12-25 03:11:00 -0800140
141 if (lstat(ce->name, &st) < 0) {
Junio C Hamanoc7054202017-05-30 09:23:33 +0900142 if (!is_missing_file_error(errno))
Nguyễn Thái Ngọc Duy7dcf3d92016-05-08 16:47:31 +0700143 warning_errno(_("failed to stat '%s'"), ce->name);
Junio C Hamano9f950692006-12-25 03:11:00 -0800144 /* It already vanished from the working tree */
145 continue;
146 }
147 else if (S_ISDIR(st.st_mode)) {
148 /* if a file was removed and it is now a
149 * directory, that is the same as ENOENT as
150 * far as git is concerned; we do not track
Jens Lehmann293ab152012-09-26 20:21:13 +0200151 * directories unless they are submodules.
Junio C Hamano9f950692006-12-25 03:11:00 -0800152 */
Jens Lehmann293ab152012-09-26 20:21:13 +0200153 if (!S_ISGITLINK(ce->ce_mode))
154 continue;
Junio C Hamano9f950692006-12-25 03:11:00 -0800155 }
Junio C Hamano69530cb2008-11-28 18:41:21 -0800156
157 /*
158 * "rm" of a path that has changes need to be treated
159 * carefully not to allow losing local changes
160 * accidentally. A local change could be (1) file in
161 * work tree is different since the index; and/or (2)
162 * the user staged a content that is different from
163 * the current commit in the index.
164 *
165 * In such a case, you would need to --force the
166 * removal. However, "rm --cached" (remove only from
167 * the index) is safe if the index matches the file in
168 * the work tree or the HEAD commit, as it means that
169 * the content being removed is available elsewhere.
170 */
171
172 /*
173 * Is the index different from the file in the work tree?
Jens Lehmann293ab152012-09-26 20:21:13 +0200174 * If it's a submodule, is its work tree modified?
Junio C Hamano69530cb2008-11-28 18:41:21 -0800175 */
Ævar Arnfjörð Bjarmason031b2032022-11-19 14:07:33 +0100176 if (ie_match_stat(&the_index, ce, &st, 0) ||
Jens Lehmann293ab152012-09-26 20:21:13 +0200177 (S_ISGITLINK(ce->ce_mode) &&
Stefan Beller83b76962016-12-20 15:20:11 -0800178 bad_to_remove_submodule(ce->name,
179 SUBMODULE_REMOVAL_DIE_ON_ERROR |
180 SUBMODULE_REMOVAL_IGNORE_IGNORED_UNTRACKED)))
Matthieu Moybdecd9d2007-07-13 19:41:38 +0200181 local_changes = 1;
Junio C Hamano69530cb2008-11-28 18:41:21 -0800182
183 /*
184 * Is the index different from the HEAD commit? By
185 * definition, before the very initial commit,
186 * anything staged in the index is treated by the same
187 * way as changed from the HEAD.
188 */
Jeff Kinge4d95162007-03-26 14:55:39 -0400189 if (no_head
Nguyễn Thái Ngọc Duy50ddb082019-06-27 16:28:49 +0700190 || get_tree_entry(the_repository, head, name, &oid, &mode)
Jeff Kinge4d95162007-03-26 14:55:39 -0400191 || ce->ce_mode != create_ce_mode(mode)
Jeff King9001dc22018-08-28 17:22:48 -0400192 || !oideq(&ce->oid, &oid))
Matthieu Moybdecd9d2007-07-13 19:41:38 +0200193 staged_changes = 1;
194
Junio C Hamano69530cb2008-11-28 18:41:21 -0800195 /*
196 * If the index does not match the file in the work
197 * tree and if it does not match the HEAD commit
198 * either, (1) "git rm" without --cached definitely
199 * will lose information; (2) "git rm --cached" will
200 * lose information unless it is about removing an
201 * "intent to add" entry.
202 */
203 if (local_changes && staged_changes) {
Nguyễn Thái Ngọc Duy895ff3b2015-08-22 08:08:05 +0700204 if (!index_only || !ce_intent_to_add(ce))
Mathieu Lienard--Mayor914dc022013-06-12 10:06:43 +0200205 string_list_append(&files_staged, name);
Junio C Hamano69530cb2008-11-28 18:41:21 -0800206 }
Matthieu Moybdecd9d2007-07-13 19:41:38 +0200207 else if (!index_only) {
Matthieu Moybdecd9d2007-07-13 19:41:38 +0200208 if (staged_changes)
Mathieu Lienard--Mayor914dc022013-06-12 10:06:43 +0200209 string_list_append(&files_cached, name);
Stefan Beller55856a32016-12-27 11:03:14 -0800210 if (local_changes)
211 string_list_append(&files_local, name);
Matthieu Moybdecd9d2007-07-13 19:41:38 +0200212 }
Junio C Hamano9f950692006-12-25 03:11:00 -0800213 }
Mathieu Lienard--Mayor914dc022013-06-12 10:06:43 +0200214 print_error_files(&files_staged,
215 Q_("the following file has staged content different "
216 "from both the\nfile and the HEAD:",
217 "the following files have staged content different"
218 " from both the\nfile and the HEAD:",
219 files_staged.nr),
220 _("\n(use -f to force removal)"),
221 &errs);
222 string_list_clear(&files_staged, 0);
223 print_error_files(&files_cached,
224 Q_("the following file has changes "
225 "staged in the index:",
226 "the following files have changes "
227 "staged in the index:", files_cached.nr),
228 _("\n(use --cached to keep the file,"
229 " or -f to force removal)"),
230 &errs);
231 string_list_clear(&files_cached, 0);
Junio C Hamano658ff472013-07-25 23:05:17 -0700232
Mathieu Lienard--Mayor914dc022013-06-12 10:06:43 +0200233 print_error_files(&files_local,
234 Q_("the following file has local modifications:",
235 "the following files have local modifications:",
236 files_local.nr),
237 _("\n(use --cached to keep the file,"
238 " or -f to force removal)"),
239 &errs);
240 string_list_clear(&files_local, 0);
241
Junio C Hamano9f950692006-12-25 03:11:00 -0800242 return errs;
243}
244
Pierre Habouzitf09985c2007-10-05 21:09:19 +0200245static int show_only = 0, force = 0, index_only = 0, recursive = 0, quiet = 0;
Alexandr Miloslavskiy5f393dc2020-02-17 17:25:16 +0000246static int ignore_unmatch = 0, pathspec_file_nul;
Derrick Stoleef9786f92021-09-24 15:39:11 +0000247static int include_sparse;
Alexandr Miloslavskiy5f393dc2020-02-17 17:25:16 +0000248static char *pathspec_from_file;
Pierre Habouzitf09985c2007-10-05 21:09:19 +0200249
250static struct option builtin_rm_options[] = {
Nguyễn Thái Ngọc Duy72bba2a2012-08-20 19:32:42 +0700251 OPT__DRY_RUN(&show_only, N_("dry run")),
252 OPT__QUIET(&quiet, N_("do not list removed files")),
Stefan Bellerd5d09d42013-08-03 13:51:19 +0200253 OPT_BOOL( 0 , "cached", &index_only, N_("only remove from the index")),
Nguyễn Thái Ngọc Duy44c9a6d2018-02-09 18:02:16 +0700254 OPT__FORCE(&force, N_("override the up-to-date check"), PARSE_OPT_NOCOMPLETE),
Stefan Bellerd5d09d42013-08-03 13:51:19 +0200255 OPT_BOOL('r', NULL, &recursive, N_("allow recursive removal")),
256 OPT_BOOL( 0 , "ignore-unmatch", &ignore_unmatch,
Nguyễn Thái Ngọc Duy72bba2a2012-08-20 19:32:42 +0700257 N_("exit with a zero status even if nothing matched")),
Derrick Stoleef9786f92021-09-24 15:39:11 +0000258 OPT_BOOL(0, "sparse", &include_sparse, N_("allow updating entries outside of the sparse-checkout cone")),
Alexandr Miloslavskiy5f393dc2020-02-17 17:25:16 +0000259 OPT_PATHSPEC_FROM_FILE(&pathspec_from_file),
260 OPT_PATHSPEC_FILE_NUL(&pathspec_file_nul),
Pierre Habouzitf09985c2007-10-05 21:09:19 +0200261 OPT_END(),
262};
263
Linus Torvaldsa633fca2006-07-28 22:44:25 -0700264int cmd_rm(int argc, const char **argv, const char *prefix)
Linus Torvaldsd9b814c2006-05-19 16:19:34 -0700265{
Martin Ågren0fa5a2e2018-05-09 22:55:39 +0200266 struct lock_file lock_file = LOCK_INIT;
Matheus Tavaresd5f4b822021-04-08 17:41:28 -0300267 int i, ret = 0;
Nguyễn Thái Ngọc Duy29211a92013-07-14 15:35:42 +0700268 struct pathspec pathspec;
Linus Torvaldsd9b814c2006-05-19 16:19:34 -0700269 char *seen;
270
Johannes Schindelinef90d6d2008-05-14 18:46:53 +0100271 git_config(git_default_config, NULL);
Linus Torvaldsd9b814c2006-05-19 16:19:34 -0700272
Stephen Boyd37782922009-05-23 11:53:12 -0700273 argc = parse_options(argc, argv, prefix, builtin_rm_options,
274 builtin_rm_usage, 0);
Alexandr Miloslavskiy5f393dc2020-02-17 17:25:16 +0000275
276 parse_pathspec(&pathspec, 0,
277 PATHSPEC_PREFER_CWD,
278 prefix, argv);
279
280 if (pathspec_from_file) {
281 if (pathspec.nr)
Jean-Noël Avila246cac82022-01-05 20:02:24 +0000282 die(_("'%s' and pathspec arguments cannot be used together"), "--pathspec-from-file");
Alexandr Miloslavskiy5f393dc2020-02-17 17:25:16 +0000283
284 parse_pathspec_file(&pathspec, 0,
285 PATHSPEC_PREFER_CWD,
286 prefix, pathspec_from_file, pathspec_file_nul);
287 } else if (pathspec_file_nul) {
Jean-Noël Avila6fa00ee2022-01-05 20:02:19 +0000288 die(_("the option '%s' requires '%s'"), "--pathspec-file-nul", "--pathspec-from-file");
Alexandr Miloslavskiy5f393dc2020-02-17 17:25:16 +0000289 }
290
291 if (!pathspec.nr)
292 die(_("No pathspec was given. Which files should I remove?"));
Linus Torvaldsd9b814c2006-05-19 16:19:34 -0700293
Mike Hommey271bb082007-11-03 12:23:13 +0100294 if (!index_only)
295 setup_work_tree();
296
Shaoxuan Yuanede241c2022-08-07 12:13:35 +0800297 prepare_repo_settings(the_repository);
298 the_repository->settings.command_requires_full_index = 0;
Ævar Arnfjörð Bjarmason07047d62022-11-19 14:07:38 +0100299 repo_hold_locked_index(the_repository, &lock_file, LOCK_DIE_ON_ERROR);
Olivier Marin4d264672008-07-19 18:24:46 +0200300
Ævar Arnfjörð Bjarmason07047d62022-11-19 14:07:38 +0100301 if (repo_read_index(the_repository) < 0)
Ævar Arnfjörð Bjarmasonb9b537f2011-02-22 23:42:05 +0000302 die(_("index file corrupt"));
Olivier Marin4d264672008-07-19 18:24:46 +0200303
Junio C Hamanob2b1f612019-07-17 13:28:24 -0700304 refresh_index(&the_index, REFRESH_QUIET|REFRESH_UNMERGED, &pathspec, NULL, NULL);
Nguyễn Thái Ngọc Duy4e1a7ba2010-01-17 15:43:13 +0700305
Nguyễn Thái Ngọc Duy29211a92013-07-14 15:35:42 +0700306 seen = xcalloc(pathspec.nr, 1);
Linus Torvaldsd9b814c2006-05-19 16:19:34 -0700307
Shaoxuan Yuanbcf96cf2022-08-07 12:13:34 +0800308 if (pathspec_needs_expanded_index(&the_index, &pathspec))
309 ensure_full_index(&the_index);
310
Ævar Arnfjörð Bjarmasondc594182022-11-19 14:07:34 +0100311 for (i = 0; i < the_index.cache_nr; i++) {
312 const struct cache_entry *ce = the_index.cache[i];
Derrick Stoleef9786f92021-09-24 15:39:11 +0000313
Derrick Stoleed7c44152021-09-24 15:39:12 +0000314 if (!include_sparse &&
315 (ce_skip_worktree(ce) ||
316 !path_in_sparse_checkout(ce->name, &the_index)))
Matheus Tavaresd5f4b822021-04-08 17:41:28 -0300317 continue;
Nguyễn Thái Ngọc Duy6d2df282018-08-13 18:14:22 +0200318 if (!ce_path_match(&the_index, ce, &pathspec, seen))
Linus Torvaldsd9b814c2006-05-19 16:19:34 -0700319 continue;
Jens Lehmann293ab152012-09-26 20:21:13 +0200320 ALLOC_GROW(list.entry, list.nr + 1, list.alloc);
Karsten Blees5699d172013-11-14 20:24:37 +0100321 list.entry[list.nr].name = xstrdup(ce->name);
Jens Lehmann95c16412013-08-06 21:15:25 +0200322 list.entry[list.nr].is_submodule = S_ISGITLINK(ce->ce_mode);
323 if (list.entry[list.nr++].is_submodule &&
Brandon Williams91b83482017-08-02 12:49:20 -0700324 !is_staging_gitmodules_ok(&the_index))
Nguyễn Thái Ngọc Duy1a07e592018-07-21 09:49:19 +0200325 die(_("please stage your changes to .gitmodules or stash them to proceed"));
Linus Torvaldsd9b814c2006-05-19 16:19:34 -0700326 }
327
Nguyễn Thái Ngọc Duy29211a92013-07-14 15:35:42 +0700328 if (pathspec.nr) {
329 const char *original;
Steven Grimmbb1faf02007-04-16 00:53:24 -0700330 int seen_any = 0;
Matheus Tavaresd5f4b822021-04-08 17:41:28 -0300331 char *skip_worktree_seen = NULL;
332 struct string_list only_match_skip_worktree = STRING_LIST_INIT_NODUP;
333
Nguyễn Thái Ngọc Duy29211a92013-07-14 15:35:42 +0700334 for (i = 0; i < pathspec.nr; i++) {
335 original = pathspec.items[i].original;
Matheus Tavaresd5f4b822021-04-08 17:41:28 -0300336 if (seen[i])
Steven Grimmbb1faf02007-04-16 00:53:24 -0700337 seen_any = 1;
Matheus Tavaresd5f4b822021-04-08 17:41:28 -0300338 else if (ignore_unmatch)
339 continue;
Derrick Stoleef9786f92021-09-24 15:39:11 +0000340 else if (!include_sparse &&
341 matches_skip_worktree(&pathspec, i, &skip_worktree_seen))
Matheus Tavaresd5f4b822021-04-08 17:41:28 -0300342 string_list_append(&only_match_skip_worktree, original);
343 else
344 die(_("pathspec '%s' did not match any files"), original);
345
Junio C Hamano9f950692006-12-25 03:11:00 -0800346 if (!recursive && seen[i] == MATCHED_RECURSIVELY)
Ævar Arnfjörð Bjarmasonb9b537f2011-02-22 23:42:05 +0000347 die(_("not removing '%s' recursively without -r"),
Nguyễn Thái Ngọc Duy29211a92013-07-14 15:35:42 +0700348 *original ? original : ".");
Stefan Bellerf8aae0b2013-08-08 19:52:41 +0200349 }
Steven Grimmbb1faf02007-04-16 00:53:24 -0700350
Matheus Tavaresd5f4b822021-04-08 17:41:28 -0300351 if (only_match_skip_worktree.nr) {
352 advise_on_updating_sparse_paths(&only_match_skip_worktree);
353 ret = 1;
354 }
355 free(skip_worktree_seen);
356 string_list_clear(&only_match_skip_worktree, 0);
357
Junio C Hamanob02f5ae2013-09-09 14:36:15 -0700358 if (!seen_any)
Matheus Tavaresd5f4b822021-04-08 17:41:28 -0300359 exit(ret);
Linus Torvaldsd9b814c2006-05-19 16:19:34 -0700360 }
Andrzej Hunt37be1192021-04-25 14:16:19 +0000361 clear_pathspec(&pathspec);
362 free(seen);
Linus Torvaldsd9b814c2006-05-19 16:19:34 -0700363
Stefan Beller55856a32016-12-27 11:03:14 -0800364 if (!index_only)
Jeff Kingcf7a9012019-05-09 17:27:31 -0400365 submodules_absorb_gitdir_if_needed();
Stefan Beller55856a32016-12-27 11:03:14 -0800366
Linus Torvaldsd9b814c2006-05-19 16:19:34 -0700367 /*
Junio C Hamano9f950692006-12-25 03:11:00 -0800368 * If not forced, the file, the index and the HEAD (if exists)
369 * must match; but the file can already been removed, since
370 * this sequence is a natural "novice" way:
371 *
Steven Grimm9474eda2007-04-16 01:17:32 -0700372 * rm F; git rm F
Junio C Hamano9f950692006-12-25 03:11:00 -0800373 *
374 * Further, if HEAD commit exists, "diff-index --cached" must
375 * report no changes unless forced.
376 */
377 if (!force) {
brian m. carlson8ec46d72016-09-05 20:08:04 +0000378 struct object_id oid;
Ævar Arnfjörð Bjarmasond850b7a2023-03-28 15:58:46 +0200379 if (repo_get_oid(the_repository, "HEAD", &oid))
brian m. carlson8ec46d72016-09-05 20:08:04 +0000380 oidclr(&oid);
381 if (check_local_mod(&oid, index_only))
Junio C Hamano9f950692006-12-25 03:11:00 -0800382 exit(1);
383 }
384
385 /*
Linus Torvaldsd9b814c2006-05-19 16:19:34 -0700386 * First remove the names from the index: we won't commit
Junio C Hamano9f950692006-12-25 03:11:00 -0800387 * the index unless all of them succeed.
Linus Torvaldsd9b814c2006-05-19 16:19:34 -0700388 */
389 for (i = 0; i < list.nr; i++) {
Jens Lehmann293ab152012-09-26 20:21:13 +0200390 const char *path = list.entry[i].name;
Steven Grimmb48caa22007-04-16 00:46:48 -0700391 if (!quiet)
392 printf("rm '%s'\n", path);
Linus Torvaldsd9b814c2006-05-19 16:19:34 -0700393
Ævar Arnfjörð Bjarmason031b2032022-11-19 14:07:33 +0100394 if (remove_file_from_index(&the_index, path))
Ævar Arnfjörð Bjarmasonb9b537f2011-02-22 23:42:05 +0000395 die(_("git rm: unable to remove %s"), path);
Linus Torvaldsd9b814c2006-05-19 16:19:34 -0700396 }
397
Junio C Hamano7612a1e2006-06-08 21:11:25 -0700398 if (show_only)
399 return 0;
400
Linus Torvaldsd9b814c2006-05-19 16:19:34 -0700401 /*
Junio C Hamano646ac222007-01-11 14:58:47 -0800402 * Then, unless we used "--cached", remove the filenames from
Junio C Hamano9f950692006-12-25 03:11:00 -0800403 * the workspace. If we fail to remove the first one, we
Linus Torvaldsd9b814c2006-05-19 16:19:34 -0700404 * abort the "git rm" (but once we've successfully removed
405 * any file at all, we'll go ahead and commit to it all:
Pavel Roskin82e5a822006-07-10 01:50:18 -0400406 * by then we've already committed ourselves and can't fail
Linus Torvaldsd9b814c2006-05-19 16:19:34 -0700407 * in the middle)
408 */
Junio C Hamano9f950692006-12-25 03:11:00 -0800409 if (!index_only) {
Jens Lehmann95c16412013-08-06 21:15:25 +0200410 int removed = 0, gitmodules_modified = 0;
René Scharfe590fc052017-02-11 20:51:08 +0100411 struct strbuf buf = STRBUF_INIT;
Elijah Newren580a5d72021-12-09 05:08:34 +0000412 int flag = force ? REMOVE_DIR_PURGE_ORIGINAL_CWD : 0;
Linus Torvaldsd9b814c2006-05-19 16:19:34 -0700413 for (i = 0; i < list.nr; i++) {
Jens Lehmann293ab152012-09-26 20:21:13 +0200414 const char *path = list.entry[i].name;
415 if (list.entry[i].is_submodule) {
René Scharfe590fc052017-02-11 20:51:08 +0100416 strbuf_reset(&buf);
Stefan Beller55856a32016-12-27 11:03:14 -0800417 strbuf_addstr(&buf, path);
Elijah Newren580a5d72021-12-09 05:08:34 +0000418 if (remove_dir_recursively(&buf, flag))
Stefan Beller55856a32016-12-27 11:03:14 -0800419 die(_("could not remove '%s'"), path);
Stefan Beller55856a32016-12-27 11:03:14 -0800420
421 removed = 1;
422 if (!remove_path_from_gitmodules(path))
423 gitmodules_modified = 1;
424 continue;
Jens Lehmann293ab152012-09-26 20:21:13 +0200425 }
Alex Riesen175a4942008-09-27 00:59:14 +0200426 if (!remove_path(path)) {
Linus Torvaldsd9b814c2006-05-19 16:19:34 -0700427 removed = 1;
428 continue;
429 }
430 if (!removed)
Thomas Rastd824cbb2009-06-27 17:58:46 +0200431 die_errno("git rm: '%s'", path);
Linus Torvaldsd9b814c2006-05-19 16:19:34 -0700432 }
René Scharfe590fc052017-02-11 20:51:08 +0100433 strbuf_release(&buf);
Jens Lehmann95c16412013-08-06 21:15:25 +0200434 if (gitmodules_modified)
Brandon Williams3b8317a2017-12-12 11:53:50 -0800435 stage_updated_gitmodules(&the_index);
Linus Torvaldsd9b814c2006-05-19 16:19:34 -0700436 }
437
Martin Ågren61000812018-03-01 21:40:20 +0100438 if (write_locked_index(&the_index, &lock_file,
439 COMMIT_LOCK | SKIP_IF_UNCHANGED))
440 die(_("Unable to write new index file"));
Linus Torvaldsd9b814c2006-05-19 16:19:34 -0700441
Matheus Tavaresd5f4b822021-04-08 17:41:28 -0300442 return ret;
Linus Torvaldsd9b814c2006-05-19 16:19:34 -0700443}