blob: 5559a0b453a3566d85f6a550332ecd9ee8ebea88 [file] [log] [blame]
Linus Torvaldsd9b814c2006-05-19 16:19:34 -07001/*
2 * "git rm" builtin command
3 *
4 * Copyright (C) Linus Torvalds 2006
5 */
Nguyễn Thái Ngọc Duyf8adbec2019-01-24 15:29:12 +07006#define USE_THE_INDEX_COMPATIBILITY_MACROS
Linus Torvaldsd9b814c2006-05-19 16:19:34 -07007#include "builtin.h"
Brandon Williamsb2141fc2017-06-14 11:07:36 -07008#include "config.h"
Michael Haggerty697cc8e2014-10-01 12:28:42 +02009#include "lockfile.h"
Linus Torvaldsd9b814c2006-05-19 16:19:34 -070010#include "dir.h"
Junio C Hamano3fb3cc62006-05-23 01:31:38 -070011#include "cache-tree.h"
Junio C Hamano9f950692006-12-25 03:11:00 -080012#include "tree-walk.h"
Pierre Habouzitf09985c2007-10-05 21:09:19 +020013#include "parse-options.h"
Mathieu Lienard--Mayor914dc022013-06-12 10:06:43 +020014#include "string-list.h"
Jens Lehmann293ab152012-09-26 20:21:13 +020015#include "submodule.h"
Nguyễn Thái Ngọc Duy29211a92013-07-14 15:35:42 +070016#include "pathspec.h"
Linus Torvaldsd9b814c2006-05-19 16:19:34 -070017
Pierre Habouzitf09985c2007-10-05 21:09:19 +020018static const char * const builtin_rm_usage[] = {
Alex Henrie9c9b4f22015-01-13 00:44:47 -070019 N_("git rm [<options>] [--] <file>..."),
Pierre Habouzitf09985c2007-10-05 21:09:19 +020020 NULL
21};
Linus Torvaldsd9b814c2006-05-19 16:19:34 -070022
23static struct {
24 int nr, alloc;
Jens Lehmann293ab152012-09-26 20:21:13 +020025 struct {
26 const char *name;
27 char is_submodule;
28 } *entry;
Linus Torvaldsd9b814c2006-05-19 16:19:34 -070029} list;
30
Jens Lehmann293ab152012-09-26 20:21:13 +020031static 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
Mathieu Lienard--Mayor914dc022013-06-12 10:06:43 +020043static 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);
Mathieu Lienard--Mayor7e309442013-06-12 10:06:44 +020057 if (advice_rm_hints)
58 strbuf_addstr(&err_msg, hints_msg);
Mathieu Lienard--Mayor914dc022013-06-12 10:06:43 +020059 *errs = error("%s", err_msg.buf);
60 strbuf_release(&err_msg);
61 }
62}
63
Jeff Kingcf7a9012019-05-09 17:27:31 -040064static void submodules_absorb_gitdir_if_needed(void)
Jens Lehmann293ab152012-09-26 20:21:13 +020065{
66 int i;
Jens Lehmann293ab152012-09-26 20:21:13 +020067 for (i = 0; i < list.nr; i++) {
68 const char *name = list.entry[i].name;
69 int pos;
Nguyễn Thái Ngọc Duy9c5e6c82013-07-09 22:29:00 +070070 const struct cache_entry *ce;
Jens Lehmann293ab152012-09-26 20:21:13 +020071
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) ||
René Scharfedbe44fa2015-05-19 23:44:23 +020081 !file_exists(ce->name) ||
Jens Lehmann293ab152012-09-26 20:21:13 +020082 is_empty_dir(name))
83 continue;
84
85 if (!submodule_uses_gitfile(name))
Jeff Kingcf7a9012019-05-09 17:27:31 -040086 absorb_git_dir_into_superproject(name,
Stefan Beller55856a32016-12-27 11:03:14 -080087 ABSORB_GITDIR_RECURSE_SUBMODULES);
Jens Lehmann293ab152012-09-26 20:21:13 +020088 }
Jens Lehmann293ab152012-09-26 20:21:13 +020089}
90
brian m. carlson8ec46d72016-09-05 20:08:04 +000091static int check_local_mod(struct object_id *head, int index_only)
Junio C Hamano9f950692006-12-25 03:11:00 -080092{
Junio C Hamano69530cb2008-11-28 18:41:21 -080093 /*
94 * Items in list are already sorted in the cache order,
Junio C Hamano9f950692006-12-25 03:11:00 -080095 * 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;
Mathieu Lienard--Mayor914dc022013-06-12 10:06:43 +0200102 struct string_list files_staged = STRING_LIST_INIT_NODUP;
103 struct string_list files_cached = STRING_LIST_INIT_NODUP;
Mathieu Lienard--Mayor914dc022013-06-12 10:06:43 +0200104 struct string_list files_local = STRING_LIST_INIT_NODUP;
Junio C Hamano9f950692006-12-25 03:11:00 -0800105
brian m. carlson8ec46d72016-09-05 20:08:04 +0000106 no_head = is_null_oid(head);
Junio C Hamano9f950692006-12-25 03:11:00 -0800107 for (i = 0; i < list.nr; i++) {
108 struct stat st;
109 int pos;
Nguyễn Thái Ngọc Duy9c5e6c82013-07-09 22:29:00 +0700110 const struct cache_entry *ce;
Jens Lehmann293ab152012-09-26 20:21:13 +0200111 const char *name = list.entry[i].name;
brian m. carlson8ec46d72016-09-05 20:08:04 +0000112 struct object_id oid;
Elijah Newren5ec1e722019-04-05 08:00:12 -0700113 unsigned short mode;
Matthieu Moybdecd9d2007-07-13 19:41:38 +0200114 int local_changes = 0;
115 int staged_changes = 0;
Junio C Hamano9f950692006-12-25 03:11:00 -0800116
117 pos = cache_name_pos(name, strlen(name));
Jens Lehmann293ab152012-09-26 20:21:13 +0200118 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 }
Junio C Hamano9f950692006-12-25 03:11:00 -0800131 ce = active_cache[pos];
132
133 if (lstat(ce->name, &st) < 0) {
Junio C Hamanoc7054202017-05-30 09:23:33 +0900134 if (!is_missing_file_error(errno))
Nguyễn Thái Ngọc Duy7dcf3d92016-05-08 16:47:31 +0700135 warning_errno(_("failed to stat '%s'"), ce->name);
Junio C Hamano9f950692006-12-25 03:11:00 -0800136 /* 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
Jens Lehmann293ab152012-09-26 20:21:13 +0200143 * directories unless they are submodules.
Junio C Hamano9f950692006-12-25 03:11:00 -0800144 */
Jens Lehmann293ab152012-09-26 20:21:13 +0200145 if (!S_ISGITLINK(ce->ce_mode))
146 continue;
Junio C Hamano9f950692006-12-25 03:11:00 -0800147 }
Junio C Hamano69530cb2008-11-28 18:41:21 -0800148
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?
Jens Lehmann293ab152012-09-26 20:21:13 +0200166 * If it's a submodule, is its work tree modified?
Junio C Hamano69530cb2008-11-28 18:41:21 -0800167 */
Jens Lehmann293ab152012-09-26 20:21:13 +0200168 if (ce_match_stat(ce, &st, 0) ||
169 (S_ISGITLINK(ce->ce_mode) &&
Stefan Beller83b76962016-12-20 15:20:11 -0800170 bad_to_remove_submodule(ce->name,
171 SUBMODULE_REMOVAL_DIE_ON_ERROR |
172 SUBMODULE_REMOVAL_IGNORE_IGNORED_UNTRACKED)))
Matthieu Moybdecd9d2007-07-13 19:41:38 +0200173 local_changes = 1;
Junio C Hamano69530cb2008-11-28 18:41:21 -0800174
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 */
Jeff Kinge4d95162007-03-26 14:55:39 -0400181 if (no_head
Nguyễn Thái Ngọc Duy50ddb082019-06-27 16:28:49 +0700182 || get_tree_entry(the_repository, head, name, &oid, &mode)
Jeff Kinge4d95162007-03-26 14:55:39 -0400183 || ce->ce_mode != create_ce_mode(mode)
Jeff King9001dc22018-08-28 17:22:48 -0400184 || !oideq(&ce->oid, &oid))
Matthieu Moybdecd9d2007-07-13 19:41:38 +0200185 staged_changes = 1;
186
Junio C Hamano69530cb2008-11-28 18:41:21 -0800187 /*
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) {
Nguyễn Thái Ngọc Duy895ff3b2015-08-22 08:08:05 +0700196 if (!index_only || !ce_intent_to_add(ce))
Mathieu Lienard--Mayor914dc022013-06-12 10:06:43 +0200197 string_list_append(&files_staged, name);
Junio C Hamano69530cb2008-11-28 18:41:21 -0800198 }
Matthieu Moybdecd9d2007-07-13 19:41:38 +0200199 else if (!index_only) {
Matthieu Moybdecd9d2007-07-13 19:41:38 +0200200 if (staged_changes)
Mathieu Lienard--Mayor914dc022013-06-12 10:06:43 +0200201 string_list_append(&files_cached, name);
Stefan Beller55856a32016-12-27 11:03:14 -0800202 if (local_changes)
203 string_list_append(&files_local, name);
Matthieu Moybdecd9d2007-07-13 19:41:38 +0200204 }
Junio C Hamano9f950692006-12-25 03:11:00 -0800205 }
Mathieu Lienard--Mayor914dc022013-06-12 10:06:43 +0200206 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);
Junio C Hamano658ff472013-07-25 23:05:17 -0700224
Mathieu Lienard--Mayor914dc022013-06-12 10:06:43 +0200225 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
Junio C Hamano9f950692006-12-25 03:11:00 -0800234 return errs;
235}
236
Pierre Habouzitf09985c2007-10-05 21:09:19 +0200237static int show_only = 0, force = 0, index_only = 0, recursive = 0, quiet = 0;
Alexandr Miloslavskiy5f393dc2020-02-17 17:25:16 +0000238static int ignore_unmatch = 0, pathspec_file_nul;
239static char *pathspec_from_file;
Pierre Habouzitf09985c2007-10-05 21:09:19 +0200240
241static struct option builtin_rm_options[] = {
Nguyễn Thái Ngọc Duy72bba2a2012-08-20 19:32:42 +0700242 OPT__DRY_RUN(&show_only, N_("dry run")),
243 OPT__QUIET(&quiet, N_("do not list removed files")),
Stefan Bellerd5d09d42013-08-03 13:51:19 +0200244 OPT_BOOL( 0 , "cached", &index_only, N_("only remove from the index")),
Nguyễn Thái Ngọc Duy44c9a6d2018-02-09 18:02:16 +0700245 OPT__FORCE(&force, N_("override the up-to-date check"), PARSE_OPT_NOCOMPLETE),
Stefan Bellerd5d09d42013-08-03 13:51:19 +0200246 OPT_BOOL('r', NULL, &recursive, N_("allow recursive removal")),
247 OPT_BOOL( 0 , "ignore-unmatch", &ignore_unmatch,
Nguyễn Thái Ngọc Duy72bba2a2012-08-20 19:32:42 +0700248 N_("exit with a zero status even if nothing matched")),
Alexandr Miloslavskiy5f393dc2020-02-17 17:25:16 +0000249 OPT_PATHSPEC_FROM_FILE(&pathspec_from_file),
250 OPT_PATHSPEC_FILE_NUL(&pathspec_file_nul),
Pierre Habouzitf09985c2007-10-05 21:09:19 +0200251 OPT_END(),
252};
253
Linus Torvaldsa633fca2006-07-28 22:44:25 -0700254int cmd_rm(int argc, const char **argv, const char *prefix)
Linus Torvaldsd9b814c2006-05-19 16:19:34 -0700255{
Martin Ågren0fa5a2e2018-05-09 22:55:39 +0200256 struct lock_file lock_file = LOCK_INIT;
Nguyễn Thái Ngọc Duy03b86642014-06-13 19:19:23 +0700257 int i;
Nguyễn Thái Ngọc Duy29211a92013-07-14 15:35:42 +0700258 struct pathspec pathspec;
Linus Torvaldsd9b814c2006-05-19 16:19:34 -0700259 char *seen;
260
Johannes Schindelinef90d6d2008-05-14 18:46:53 +0100261 git_config(git_default_config, NULL);
Linus Torvaldsd9b814c2006-05-19 16:19:34 -0700262
Stephen Boyd37782922009-05-23 11:53:12 -0700263 argc = parse_options(argc, argv, prefix, builtin_rm_options,
264 builtin_rm_usage, 0);
Alexandr Miloslavskiy5f393dc2020-02-17 17:25:16 +0000265
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?"));
Linus Torvaldsd9b814c2006-05-19 16:19:34 -0700283
Mike Hommey271bb082007-11-03 12:23:13 +0100284 if (!index_only)
285 setup_work_tree();
286
Junio C Hamanob3e83cc2016-12-07 10:33:54 -0800287 hold_locked_index(&lock_file, LOCK_DIE_ON_ERROR);
Olivier Marin4d264672008-07-19 18:24:46 +0200288
289 if (read_cache() < 0)
Ævar Arnfjörð Bjarmasonb9b537f2011-02-22 23:42:05 +0000290 die(_("index file corrupt"));
Olivier Marin4d264672008-07-19 18:24:46 +0200291
Junio C Hamanob2b1f612019-07-17 13:28:24 -0700292 refresh_index(&the_index, REFRESH_QUIET|REFRESH_UNMERGED, &pathspec, NULL, NULL);
Nguyễn Thái Ngọc Duy4e1a7ba2010-01-17 15:43:13 +0700293
Nguyễn Thái Ngọc Duy29211a92013-07-14 15:35:42 +0700294 seen = xcalloc(pathspec.nr, 1);
Linus Torvaldsd9b814c2006-05-19 16:19:34 -0700295
Derrick Stoleee43e2a12021-04-01 01:49:51 +0000296 /* TODO: audit for interaction with sparse-index. */
297 ensure_full_index(&the_index);
Linus Torvaldsd9b814c2006-05-19 16:19:34 -0700298 for (i = 0; i < active_nr; i++) {
Nguyễn Thái Ngọc Duy9c5e6c82013-07-09 22:29:00 +0700299 const struct cache_entry *ce = active_cache[i];
Nguyễn Thái Ngọc Duy6d2df282018-08-13 18:14:22 +0200300 if (!ce_path_match(&the_index, ce, &pathspec, seen))
Linus Torvaldsd9b814c2006-05-19 16:19:34 -0700301 continue;
Jens Lehmann293ab152012-09-26 20:21:13 +0200302 ALLOC_GROW(list.entry, list.nr + 1, list.alloc);
Karsten Blees5699d172013-11-14 20:24:37 +0100303 list.entry[list.nr].name = xstrdup(ce->name);
Jens Lehmann95c16412013-08-06 21:15:25 +0200304 list.entry[list.nr].is_submodule = S_ISGITLINK(ce->ce_mode);
305 if (list.entry[list.nr++].is_submodule &&
Brandon Williams91b83482017-08-02 12:49:20 -0700306 !is_staging_gitmodules_ok(&the_index))
Nguyễn Thái Ngọc Duy1a07e592018-07-21 09:49:19 +0200307 die(_("please stage your changes to .gitmodules or stash them to proceed"));
Linus Torvaldsd9b814c2006-05-19 16:19:34 -0700308 }
309
Nguyễn Thái Ngọc Duy29211a92013-07-14 15:35:42 +0700310 if (pathspec.nr) {
311 const char *original;
Steven Grimmbb1faf02007-04-16 00:53:24 -0700312 int seen_any = 0;
Nguyễn Thái Ngọc Duy29211a92013-07-14 15:35:42 +0700313 for (i = 0; i < pathspec.nr; i++) {
314 original = pathspec.items[i].original;
Steven Grimmbb1faf02007-04-16 00:53:24 -0700315 if (!seen[i]) {
316 if (!ignore_unmatch) {
Ævar Arnfjörð Bjarmasonb9b537f2011-02-22 23:42:05 +0000317 die(_("pathspec '%s' did not match any files"),
Nguyễn Thái Ngọc Duy29211a92013-07-14 15:35:42 +0700318 original);
Steven Grimmbb1faf02007-04-16 00:53:24 -0700319 }
Stefan Bellerf8aae0b2013-08-08 19:52:41 +0200320 }
Steven Grimmbb1faf02007-04-16 00:53:24 -0700321 else {
322 seen_any = 1;
323 }
Junio C Hamano9f950692006-12-25 03:11:00 -0800324 if (!recursive && seen[i] == MATCHED_RECURSIVELY)
Ævar Arnfjörð Bjarmasonb9b537f2011-02-22 23:42:05 +0000325 die(_("not removing '%s' recursively without -r"),
Nguyễn Thái Ngọc Duy29211a92013-07-14 15:35:42 +0700326 *original ? original : ".");
Stefan Bellerf8aae0b2013-08-08 19:52:41 +0200327 }
Steven Grimmbb1faf02007-04-16 00:53:24 -0700328
Junio C Hamanob02f5ae2013-09-09 14:36:15 -0700329 if (!seen_any)
Steven Grimmbb1faf02007-04-16 00:53:24 -0700330 exit(0);
Linus Torvaldsd9b814c2006-05-19 16:19:34 -0700331 }
332
Stefan Beller55856a32016-12-27 11:03:14 -0800333 if (!index_only)
Jeff Kingcf7a9012019-05-09 17:27:31 -0400334 submodules_absorb_gitdir_if_needed();
Stefan Beller55856a32016-12-27 11:03:14 -0800335
Linus Torvaldsd9b814c2006-05-19 16:19:34 -0700336 /*
Junio C Hamano9f950692006-12-25 03:11:00 -0800337 * If not forced, the file, the index and the HEAD (if exists)
338 * must match; but the file can already been removed, since
339 * this sequence is a natural "novice" way:
340 *
Steven Grimm9474eda2007-04-16 01:17:32 -0700341 * rm F; git rm F
Junio C Hamano9f950692006-12-25 03:11:00 -0800342 *
343 * Further, if HEAD commit exists, "diff-index --cached" must
344 * report no changes unless forced.
345 */
346 if (!force) {
brian m. carlson8ec46d72016-09-05 20:08:04 +0000347 struct object_id oid;
348 if (get_oid("HEAD", &oid))
349 oidclr(&oid);
350 if (check_local_mod(&oid, index_only))
Junio C Hamano9f950692006-12-25 03:11:00 -0800351 exit(1);
352 }
353
354 /*
Linus Torvaldsd9b814c2006-05-19 16:19:34 -0700355 * First remove the names from the index: we won't commit
Junio C Hamano9f950692006-12-25 03:11:00 -0800356 * the index unless all of them succeed.
Linus Torvaldsd9b814c2006-05-19 16:19:34 -0700357 */
358 for (i = 0; i < list.nr; i++) {
Jens Lehmann293ab152012-09-26 20:21:13 +0200359 const char *path = list.entry[i].name;
Steven Grimmb48caa22007-04-16 00:46:48 -0700360 if (!quiet)
361 printf("rm '%s'\n", path);
Linus Torvaldsd9b814c2006-05-19 16:19:34 -0700362
363 if (remove_file_from_cache(path))
Ævar Arnfjörð Bjarmasonb9b537f2011-02-22 23:42:05 +0000364 die(_("git rm: unable to remove %s"), path);
Linus Torvaldsd9b814c2006-05-19 16:19:34 -0700365 }
366
Junio C Hamano7612a1e2006-06-08 21:11:25 -0700367 if (show_only)
368 return 0;
369
Linus Torvaldsd9b814c2006-05-19 16:19:34 -0700370 /*
Junio C Hamano646ac222007-01-11 14:58:47 -0800371 * Then, unless we used "--cached", remove the filenames from
Junio C Hamano9f950692006-12-25 03:11:00 -0800372 * the workspace. If we fail to remove the first one, we
Linus Torvaldsd9b814c2006-05-19 16:19:34 -0700373 * abort the "git rm" (but once we've successfully removed
374 * any file at all, we'll go ahead and commit to it all:
Pavel Roskin82e5a822006-07-10 01:50:18 -0400375 * by then we've already committed ourselves and can't fail
Linus Torvaldsd9b814c2006-05-19 16:19:34 -0700376 * in the middle)
377 */
Junio C Hamano9f950692006-12-25 03:11:00 -0800378 if (!index_only) {
Jens Lehmann95c16412013-08-06 21:15:25 +0200379 int removed = 0, gitmodules_modified = 0;
René Scharfe590fc052017-02-11 20:51:08 +0100380 struct strbuf buf = STRBUF_INIT;
Linus Torvaldsd9b814c2006-05-19 16:19:34 -0700381 for (i = 0; i < list.nr; i++) {
Jens Lehmann293ab152012-09-26 20:21:13 +0200382 const char *path = list.entry[i].name;
383 if (list.entry[i].is_submodule) {
René Scharfe590fc052017-02-11 20:51:08 +0100384 strbuf_reset(&buf);
Stefan Beller55856a32016-12-27 11:03:14 -0800385 strbuf_addstr(&buf, path);
386 if (remove_dir_recursively(&buf, 0))
387 die(_("could not remove '%s'"), path);
Stefan Beller55856a32016-12-27 11:03:14 -0800388
389 removed = 1;
390 if (!remove_path_from_gitmodules(path))
391 gitmodules_modified = 1;
392 continue;
Jens Lehmann293ab152012-09-26 20:21:13 +0200393 }
Alex Riesen175a4942008-09-27 00:59:14 +0200394 if (!remove_path(path)) {
Linus Torvaldsd9b814c2006-05-19 16:19:34 -0700395 removed = 1;
396 continue;
397 }
398 if (!removed)
Thomas Rastd824cbb2009-06-27 17:58:46 +0200399 die_errno("git rm: '%s'", path);
Linus Torvaldsd9b814c2006-05-19 16:19:34 -0700400 }
René Scharfe590fc052017-02-11 20:51:08 +0100401 strbuf_release(&buf);
Jens Lehmann95c16412013-08-06 21:15:25 +0200402 if (gitmodules_modified)
Brandon Williams3b8317a2017-12-12 11:53:50 -0800403 stage_updated_gitmodules(&the_index);
Linus Torvaldsd9b814c2006-05-19 16:19:34 -0700404 }
405
Martin Ågren61000812018-03-01 21:40:20 +0100406 if (write_locked_index(&the_index, &lock_file,
407 COMMIT_LOCK | SKIP_IF_UNCHANGED))
408 die(_("Unable to write new index file"));
Linus Torvaldsd9b814c2006-05-19 16:19:34 -0700409
410 return 0;
411}