blob: 8a24c715e02bab24098af5f3e354c631ee9abf3c [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"
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"
Junio C Hamano3fb3cc62006-05-23 01:31:38 -070012#include "cache-tree.h"
Junio C Hamano9f950692006-12-25 03:11:00 -080013#include "tree-walk.h"
Pierre Habouzitf09985c2007-10-05 21:09:19 +020014#include "parse-options.h"
Mathieu Lienard--Mayor914dc022013-06-12 10:06:43 +020015#include "string-list.h"
Jens Lehmann293ab152012-09-26 20:21:13 +020016#include "submodule.h"
Nguyễn Thái Ngọc Duy29211a92013-07-14 15:35:42 +070017#include "pathspec.h"
Linus Torvaldsd9b814c2006-05-19 16:19:34 -070018
Pierre Habouzitf09985c2007-10-05 21:09:19 +020019static const char * const builtin_rm_usage[] = {
Alex Henrie9c9b4f22015-01-13 00:44:47 -070020 N_("git rm [<options>] [--] <file>..."),
Pierre Habouzitf09985c2007-10-05 21:09:19 +020021 NULL
22};
Linus Torvaldsd9b814c2006-05-19 16:19:34 -070023
24static struct {
25 int nr, alloc;
Jens Lehmann293ab152012-09-26 20:21:13 +020026 struct {
27 const char *name;
28 char is_submodule;
29 } *entry;
Linus Torvaldsd9b814c2006-05-19 16:19:34 -070030} list;
31
Jens Lehmann293ab152012-09-26 20:21:13 +020032static 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
Mathieu Lienard--Mayor914dc022013-06-12 10:06:43 +020044static 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);
Mathieu Lienard--Mayor7e309442013-06-12 10:06:44 +020058 if (advice_rm_hints)
59 strbuf_addstr(&err_msg, hints_msg);
Mathieu Lienard--Mayor914dc022013-06-12 10:06:43 +020060 *errs = error("%s", err_msg.buf);
61 strbuf_release(&err_msg);
62 }
63}
64
Jeff Kingcf7a9012019-05-09 17:27:31 -040065static void submodules_absorb_gitdir_if_needed(void)
Jens Lehmann293ab152012-09-26 20:21:13 +020066{
67 int i;
Jens Lehmann293ab152012-09-26 20:21:13 +020068 for (i = 0; i < list.nr; i++) {
69 const char *name = list.entry[i].name;
70 int pos;
Nguyễn Thái Ngọc Duy9c5e6c82013-07-09 22:29:00 +070071 const struct cache_entry *ce;
Jens Lehmann293ab152012-09-26 20:21:13 +020072
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) ||
René Scharfedbe44fa2015-05-19 23:44:23 +020082 !file_exists(ce->name) ||
Jens Lehmann293ab152012-09-26 20:21:13 +020083 is_empty_dir(name))
84 continue;
85
86 if (!submodule_uses_gitfile(name))
Jeff Kingcf7a9012019-05-09 17:27:31 -040087 absorb_git_dir_into_superproject(name,
Stefan Beller55856a32016-12-27 11:03:14 -080088 ABSORB_GITDIR_RECURSE_SUBMODULES);
Jens Lehmann293ab152012-09-26 20:21:13 +020089 }
Jens Lehmann293ab152012-09-26 20:21:13 +020090}
91
brian m. carlson8ec46d72016-09-05 20:08:04 +000092static int check_local_mod(struct object_id *head, int index_only)
Junio C Hamano9f950692006-12-25 03:11:00 -080093{
Junio C Hamano69530cb2008-11-28 18:41:21 -080094 /*
95 * Items in list are already sorted in the cache order,
Junio C Hamano9f950692006-12-25 03:11:00 -080096 * 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;
Mathieu Lienard--Mayor914dc022013-06-12 10:06:43 +0200103 struct string_list files_staged = STRING_LIST_INIT_NODUP;
104 struct string_list files_cached = STRING_LIST_INIT_NODUP;
Mathieu Lienard--Mayor914dc022013-06-12 10:06:43 +0200105 struct string_list files_local = STRING_LIST_INIT_NODUP;
Junio C Hamano9f950692006-12-25 03:11:00 -0800106
brian m. carlson8ec46d72016-09-05 20:08:04 +0000107 no_head = is_null_oid(head);
Junio C Hamano9f950692006-12-25 03:11:00 -0800108 for (i = 0; i < list.nr; i++) {
109 struct stat st;
110 int pos;
Nguyễn Thái Ngọc Duy9c5e6c82013-07-09 22:29:00 +0700111 const struct cache_entry *ce;
Jens Lehmann293ab152012-09-26 20:21:13 +0200112 const char *name = list.entry[i].name;
brian m. carlson8ec46d72016-09-05 20:08:04 +0000113 struct object_id oid;
Elijah Newren5ec1e722019-04-05 08:00:12 -0700114 unsigned short mode;
Matthieu Moybdecd9d2007-07-13 19:41:38 +0200115 int local_changes = 0;
116 int staged_changes = 0;
Junio C Hamano9f950692006-12-25 03:11:00 -0800117
118 pos = cache_name_pos(name, strlen(name));
Jens Lehmann293ab152012-09-26 20:21:13 +0200119 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 }
Junio C Hamano9f950692006-12-25 03:11:00 -0800132 ce = active_cache[pos];
133
134 if (lstat(ce->name, &st) < 0) {
Junio C Hamanoc7054202017-05-30 09:23:33 +0900135 if (!is_missing_file_error(errno))
Nguyễn Thái Ngọc Duy7dcf3d92016-05-08 16:47:31 +0700136 warning_errno(_("failed to stat '%s'"), ce->name);
Junio C Hamano9f950692006-12-25 03:11:00 -0800137 /* 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
Jens Lehmann293ab152012-09-26 20:21:13 +0200144 * directories unless they are submodules.
Junio C Hamano9f950692006-12-25 03:11:00 -0800145 */
Jens Lehmann293ab152012-09-26 20:21:13 +0200146 if (!S_ISGITLINK(ce->ce_mode))
147 continue;
Junio C Hamano9f950692006-12-25 03:11:00 -0800148 }
Junio C Hamano69530cb2008-11-28 18:41:21 -0800149
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?
Jens Lehmann293ab152012-09-26 20:21:13 +0200167 * If it's a submodule, is its work tree modified?
Junio C Hamano69530cb2008-11-28 18:41:21 -0800168 */
Jens Lehmann293ab152012-09-26 20:21:13 +0200169 if (ce_match_stat(ce, &st, 0) ||
170 (S_ISGITLINK(ce->ce_mode) &&
Stefan Beller83b76962016-12-20 15:20:11 -0800171 bad_to_remove_submodule(ce->name,
172 SUBMODULE_REMOVAL_DIE_ON_ERROR |
173 SUBMODULE_REMOVAL_IGNORE_IGNORED_UNTRACKED)))
Matthieu Moybdecd9d2007-07-13 19:41:38 +0200174 local_changes = 1;
Junio C Hamano69530cb2008-11-28 18:41:21 -0800175
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 */
Jeff Kinge4d95162007-03-26 14:55:39 -0400182 if (no_head
Nguyễn Thái Ngọc Duy50ddb082019-06-27 16:28:49 +0700183 || get_tree_entry(the_repository, head, name, &oid, &mode)
Jeff Kinge4d95162007-03-26 14:55:39 -0400184 || ce->ce_mode != create_ce_mode(mode)
Jeff King9001dc22018-08-28 17:22:48 -0400185 || !oideq(&ce->oid, &oid))
Matthieu Moybdecd9d2007-07-13 19:41:38 +0200186 staged_changes = 1;
187
Junio C Hamano69530cb2008-11-28 18:41:21 -0800188 /*
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) {
Nguyễn Thái Ngọc Duy895ff3b2015-08-22 08:08:05 +0700197 if (!index_only || !ce_intent_to_add(ce))
Mathieu Lienard--Mayor914dc022013-06-12 10:06:43 +0200198 string_list_append(&files_staged, name);
Junio C Hamano69530cb2008-11-28 18:41:21 -0800199 }
Matthieu Moybdecd9d2007-07-13 19:41:38 +0200200 else if (!index_only) {
Matthieu Moybdecd9d2007-07-13 19:41:38 +0200201 if (staged_changes)
Mathieu Lienard--Mayor914dc022013-06-12 10:06:43 +0200202 string_list_append(&files_cached, name);
Stefan Beller55856a32016-12-27 11:03:14 -0800203 if (local_changes)
204 string_list_append(&files_local, name);
Matthieu Moybdecd9d2007-07-13 19:41:38 +0200205 }
Junio C Hamano9f950692006-12-25 03:11:00 -0800206 }
Mathieu Lienard--Mayor914dc022013-06-12 10:06:43 +0200207 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);
Junio C Hamano658ff472013-07-25 23:05:17 -0700225
Mathieu Lienard--Mayor914dc022013-06-12 10:06:43 +0200226 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
Junio C Hamano9f950692006-12-25 03:11:00 -0800235 return errs;
236}
237
Pierre Habouzitf09985c2007-10-05 21:09:19 +0200238static int show_only = 0, force = 0, index_only = 0, recursive = 0, quiet = 0;
Alexandr Miloslavskiy5f393dc2020-02-17 17:25:16 +0000239static int ignore_unmatch = 0, pathspec_file_nul;
240static char *pathspec_from_file;
Pierre Habouzitf09985c2007-10-05 21:09:19 +0200241
242static struct option builtin_rm_options[] = {
Nguyễn Thái Ngọc Duy72bba2a2012-08-20 19:32:42 +0700243 OPT__DRY_RUN(&show_only, N_("dry run")),
244 OPT__QUIET(&quiet, N_("do not list removed files")),
Stefan Bellerd5d09d42013-08-03 13:51:19 +0200245 OPT_BOOL( 0 , "cached", &index_only, N_("only remove from the index")),
Nguyễn Thái Ngọc Duy44c9a6d2018-02-09 18:02:16 +0700246 OPT__FORCE(&force, N_("override the up-to-date check"), PARSE_OPT_NOCOMPLETE),
Stefan Bellerd5d09d42013-08-03 13:51:19 +0200247 OPT_BOOL('r', NULL, &recursive, N_("allow recursive removal")),
248 OPT_BOOL( 0 , "ignore-unmatch", &ignore_unmatch,
Nguyễn Thái Ngọc Duy72bba2a2012-08-20 19:32:42 +0700249 N_("exit with a zero status even if nothing matched")),
Alexandr Miloslavskiy5f393dc2020-02-17 17:25:16 +0000250 OPT_PATHSPEC_FROM_FILE(&pathspec_from_file),
251 OPT_PATHSPEC_FILE_NUL(&pathspec_file_nul),
Pierre Habouzitf09985c2007-10-05 21:09:19 +0200252 OPT_END(),
253};
254
Linus Torvaldsa633fca2006-07-28 22:44:25 -0700255int cmd_rm(int argc, const char **argv, const char *prefix)
Linus Torvaldsd9b814c2006-05-19 16:19:34 -0700256{
Martin Ågren0fa5a2e2018-05-09 22:55:39 +0200257 struct lock_file lock_file = LOCK_INIT;
Matheus Tavaresd5f4b822021-04-08 17:41:28 -0300258 int i, ret = 0;
Nguyễn Thái Ngọc Duy29211a92013-07-14 15:35:42 +0700259 struct pathspec pathspec;
Linus Torvaldsd9b814c2006-05-19 16:19:34 -0700260 char *seen;
261
Johannes Schindelinef90d6d2008-05-14 18:46:53 +0100262 git_config(git_default_config, NULL);
Linus Torvaldsd9b814c2006-05-19 16:19:34 -0700263
Stephen Boyd37782922009-05-23 11:53:12 -0700264 argc = parse_options(argc, argv, prefix, builtin_rm_options,
265 builtin_rm_usage, 0);
Alexandr Miloslavskiy5f393dc2020-02-17 17:25:16 +0000266
267 parse_pathspec(&pathspec, 0,
268 PATHSPEC_PREFER_CWD,
269 prefix, argv);
270
271 if (pathspec_from_file) {
272 if (pathspec.nr)
273 die(_("--pathspec-from-file is incompatible with pathspec arguments"));
274
275 parse_pathspec_file(&pathspec, 0,
276 PATHSPEC_PREFER_CWD,
277 prefix, pathspec_from_file, pathspec_file_nul);
278 } else if (pathspec_file_nul) {
279 die(_("--pathspec-file-nul requires --pathspec-from-file"));
280 }
281
282 if (!pathspec.nr)
283 die(_("No pathspec was given. Which files should I remove?"));
Linus Torvaldsd9b814c2006-05-19 16:19:34 -0700284
Mike Hommey271bb082007-11-03 12:23:13 +0100285 if (!index_only)
286 setup_work_tree();
287
Junio C Hamanob3e83cc2016-12-07 10:33:54 -0800288 hold_locked_index(&lock_file, LOCK_DIE_ON_ERROR);
Olivier Marin4d264672008-07-19 18:24:46 +0200289
290 if (read_cache() < 0)
Ævar Arnfjörð Bjarmasonb9b537f2011-02-22 23:42:05 +0000291 die(_("index file corrupt"));
Olivier Marin4d264672008-07-19 18:24:46 +0200292
Junio C Hamanob2b1f612019-07-17 13:28:24 -0700293 refresh_index(&the_index, REFRESH_QUIET|REFRESH_UNMERGED, &pathspec, NULL, NULL);
Nguyễn Thái Ngọc Duy4e1a7ba2010-01-17 15:43:13 +0700294
Nguyễn Thái Ngọc Duy29211a92013-07-14 15:35:42 +0700295 seen = xcalloc(pathspec.nr, 1);
Linus Torvaldsd9b814c2006-05-19 16:19:34 -0700296
Derrick Stoleee43e2a12021-04-01 01:49:51 +0000297 /* TODO: audit for interaction with sparse-index. */
298 ensure_full_index(&the_index);
Linus Torvaldsd9b814c2006-05-19 16:19:34 -0700299 for (i = 0; i < active_nr; i++) {
Nguyễn Thái Ngọc Duy9c5e6c82013-07-09 22:29:00 +0700300 const struct cache_entry *ce = active_cache[i];
Matheus Tavaresd5f4b822021-04-08 17:41:28 -0300301 if (ce_skip_worktree(ce))
302 continue;
Nguyễn Thái Ngọc Duy6d2df282018-08-13 18:14:22 +0200303 if (!ce_path_match(&the_index, ce, &pathspec, seen))
Linus Torvaldsd9b814c2006-05-19 16:19:34 -0700304 continue;
Jens Lehmann293ab152012-09-26 20:21:13 +0200305 ALLOC_GROW(list.entry, list.nr + 1, list.alloc);
Karsten Blees5699d172013-11-14 20:24:37 +0100306 list.entry[list.nr].name = xstrdup(ce->name);
Jens Lehmann95c16412013-08-06 21:15:25 +0200307 list.entry[list.nr].is_submodule = S_ISGITLINK(ce->ce_mode);
308 if (list.entry[list.nr++].is_submodule &&
Brandon Williams91b83482017-08-02 12:49:20 -0700309 !is_staging_gitmodules_ok(&the_index))
Nguyễn Thái Ngọc Duy1a07e592018-07-21 09:49:19 +0200310 die(_("please stage your changes to .gitmodules or stash them to proceed"));
Linus Torvaldsd9b814c2006-05-19 16:19:34 -0700311 }
312
Nguyễn Thái Ngọc Duy29211a92013-07-14 15:35:42 +0700313 if (pathspec.nr) {
314 const char *original;
Steven Grimmbb1faf02007-04-16 00:53:24 -0700315 int seen_any = 0;
Matheus Tavaresd5f4b822021-04-08 17:41:28 -0300316 char *skip_worktree_seen = NULL;
317 struct string_list only_match_skip_worktree = STRING_LIST_INIT_NODUP;
318
Nguyễn Thái Ngọc Duy29211a92013-07-14 15:35:42 +0700319 for (i = 0; i < pathspec.nr; i++) {
320 original = pathspec.items[i].original;
Matheus Tavaresd5f4b822021-04-08 17:41:28 -0300321 if (seen[i])
Steven Grimmbb1faf02007-04-16 00:53:24 -0700322 seen_any = 1;
Matheus Tavaresd5f4b822021-04-08 17:41:28 -0300323 else if (ignore_unmatch)
324 continue;
325 else if (matches_skip_worktree(&pathspec, i, &skip_worktree_seen))
326 string_list_append(&only_match_skip_worktree, original);
327 else
328 die(_("pathspec '%s' did not match any files"), original);
329
Junio C Hamano9f950692006-12-25 03:11:00 -0800330 if (!recursive && seen[i] == MATCHED_RECURSIVELY)
Ævar Arnfjörð Bjarmasonb9b537f2011-02-22 23:42:05 +0000331 die(_("not removing '%s' recursively without -r"),
Nguyễn Thái Ngọc Duy29211a92013-07-14 15:35:42 +0700332 *original ? original : ".");
Stefan Bellerf8aae0b2013-08-08 19:52:41 +0200333 }
Steven Grimmbb1faf02007-04-16 00:53:24 -0700334
Matheus Tavaresd5f4b822021-04-08 17:41:28 -0300335 if (only_match_skip_worktree.nr) {
336 advise_on_updating_sparse_paths(&only_match_skip_worktree);
337 ret = 1;
338 }
339 free(skip_worktree_seen);
340 string_list_clear(&only_match_skip_worktree, 0);
341
Junio C Hamanob02f5ae2013-09-09 14:36:15 -0700342 if (!seen_any)
Matheus Tavaresd5f4b822021-04-08 17:41:28 -0300343 exit(ret);
Linus Torvaldsd9b814c2006-05-19 16:19:34 -0700344 }
Andrzej Hunt37be1192021-04-25 14:16:19 +0000345 clear_pathspec(&pathspec);
346 free(seen);
Linus Torvaldsd9b814c2006-05-19 16:19:34 -0700347
Stefan Beller55856a32016-12-27 11:03:14 -0800348 if (!index_only)
Jeff Kingcf7a9012019-05-09 17:27:31 -0400349 submodules_absorb_gitdir_if_needed();
Stefan Beller55856a32016-12-27 11:03:14 -0800350
Linus Torvaldsd9b814c2006-05-19 16:19:34 -0700351 /*
Junio C Hamano9f950692006-12-25 03:11:00 -0800352 * If not forced, the file, the index and the HEAD (if exists)
353 * must match; but the file can already been removed, since
354 * this sequence is a natural "novice" way:
355 *
Steven Grimm9474eda2007-04-16 01:17:32 -0700356 * rm F; git rm F
Junio C Hamano9f950692006-12-25 03:11:00 -0800357 *
358 * Further, if HEAD commit exists, "diff-index --cached" must
359 * report no changes unless forced.
360 */
361 if (!force) {
brian m. carlson8ec46d72016-09-05 20:08:04 +0000362 struct object_id oid;
363 if (get_oid("HEAD", &oid))
364 oidclr(&oid);
365 if (check_local_mod(&oid, index_only))
Junio C Hamano9f950692006-12-25 03:11:00 -0800366 exit(1);
367 }
368
369 /*
Linus Torvaldsd9b814c2006-05-19 16:19:34 -0700370 * First remove the names from the index: we won't commit
Junio C Hamano9f950692006-12-25 03:11:00 -0800371 * the index unless all of them succeed.
Linus Torvaldsd9b814c2006-05-19 16:19:34 -0700372 */
373 for (i = 0; i < list.nr; i++) {
Jens Lehmann293ab152012-09-26 20:21:13 +0200374 const char *path = list.entry[i].name;
Steven Grimmb48caa22007-04-16 00:46:48 -0700375 if (!quiet)
376 printf("rm '%s'\n", path);
Linus Torvaldsd9b814c2006-05-19 16:19:34 -0700377
378 if (remove_file_from_cache(path))
Ævar Arnfjörð Bjarmasonb9b537f2011-02-22 23:42:05 +0000379 die(_("git rm: unable to remove %s"), path);
Linus Torvaldsd9b814c2006-05-19 16:19:34 -0700380 }
381
Junio C Hamano7612a1e2006-06-08 21:11:25 -0700382 if (show_only)
383 return 0;
384
Linus Torvaldsd9b814c2006-05-19 16:19:34 -0700385 /*
Junio C Hamano646ac222007-01-11 14:58:47 -0800386 * Then, unless we used "--cached", remove the filenames from
Junio C Hamano9f950692006-12-25 03:11:00 -0800387 * the workspace. If we fail to remove the first one, we
Linus Torvaldsd9b814c2006-05-19 16:19:34 -0700388 * abort the "git rm" (but once we've successfully removed
389 * any file at all, we'll go ahead and commit to it all:
Pavel Roskin82e5a822006-07-10 01:50:18 -0400390 * by then we've already committed ourselves and can't fail
Linus Torvaldsd9b814c2006-05-19 16:19:34 -0700391 * in the middle)
392 */
Junio C Hamano9f950692006-12-25 03:11:00 -0800393 if (!index_only) {
Jens Lehmann95c16412013-08-06 21:15:25 +0200394 int removed = 0, gitmodules_modified = 0;
René Scharfe590fc052017-02-11 20:51:08 +0100395 struct strbuf buf = STRBUF_INIT;
Linus Torvaldsd9b814c2006-05-19 16:19:34 -0700396 for (i = 0; i < list.nr; i++) {
Jens Lehmann293ab152012-09-26 20:21:13 +0200397 const char *path = list.entry[i].name;
398 if (list.entry[i].is_submodule) {
René Scharfe590fc052017-02-11 20:51:08 +0100399 strbuf_reset(&buf);
Stefan Beller55856a32016-12-27 11:03:14 -0800400 strbuf_addstr(&buf, path);
401 if (remove_dir_recursively(&buf, 0))
402 die(_("could not remove '%s'"), path);
Stefan Beller55856a32016-12-27 11:03:14 -0800403
404 removed = 1;
405 if (!remove_path_from_gitmodules(path))
406 gitmodules_modified = 1;
407 continue;
Jens Lehmann293ab152012-09-26 20:21:13 +0200408 }
Alex Riesen175a4942008-09-27 00:59:14 +0200409 if (!remove_path(path)) {
Linus Torvaldsd9b814c2006-05-19 16:19:34 -0700410 removed = 1;
411 continue;
412 }
413 if (!removed)
Thomas Rastd824cbb2009-06-27 17:58:46 +0200414 die_errno("git rm: '%s'", path);
Linus Torvaldsd9b814c2006-05-19 16:19:34 -0700415 }
René Scharfe590fc052017-02-11 20:51:08 +0100416 strbuf_release(&buf);
Jens Lehmann95c16412013-08-06 21:15:25 +0200417 if (gitmodules_modified)
Brandon Williams3b8317a2017-12-12 11:53:50 -0800418 stage_updated_gitmodules(&the_index);
Linus Torvaldsd9b814c2006-05-19 16:19:34 -0700419 }
420
Martin Ågren61000812018-03-01 21:40:20 +0100421 if (write_locked_index(&the_index, &lock_file,
422 COMMIT_LOCK | SKIP_IF_UNCHANGED))
423 die(_("Unable to write new index file"));
Linus Torvaldsd9b814c2006-05-19 16:19:34 -0700424
Matheus Tavaresd5f4b822021-04-08 17:41:28 -0300425 return ret;
Linus Torvaldsd9b814c2006-05-19 16:19:34 -0700426}