Linus Torvalds | d9b814c | 2006-05-19 16:19:34 -0700 | [diff] [blame] | 1 | /* |
| 2 | * "git rm" builtin command |
| 3 | * |
| 4 | * Copyright (C) Linus Torvalds 2006 |
| 5 | */ |
Ævar Arnfjörð Bjarmason | 6193aaa | 2023-02-10 11:28:34 +0100 | [diff] [blame] | 6 | #define USE_THE_INDEX_VARIABLE |
Linus Torvalds | d9b814c | 2006-05-19 16:19:34 -0700 | [diff] [blame] | 7 | #include "builtin.h" |
Matheus Tavares | d5f4b82 | 2021-04-08 17:41:28 -0300 | [diff] [blame] | 8 | #include "advice.h" |
Brandon Williams | b2141fc | 2017-06-14 11:07:36 -0700 | [diff] [blame] | 9 | #include "config.h" |
Michael Haggerty | 697cc8e | 2014-10-01 12:28:42 +0200 | [diff] [blame] | 10 | #include "lockfile.h" |
Linus Torvalds | d9b814c | 2006-05-19 16:19:34 -0700 | [diff] [blame] | 11 | #include "dir.h" |
Elijah Newren | f394e09 | 2023-03-21 06:25:54 +0000 | [diff] [blame] | 12 | #include "gettext.h" |
Elijah Newren | d1cbe1e | 2023-04-22 20:17:20 +0000 | [diff] [blame] | 13 | #include "hash.h" |
Junio C Hamano | 9f95069 | 2006-12-25 03:11:00 -0800 | [diff] [blame] | 14 | #include "tree-walk.h" |
Elijah Newren | dabab1d | 2023-04-11 00:41:49 -0700 | [diff] [blame] | 15 | #include "object-name.h" |
Pierre Habouzit | f09985c | 2007-10-05 21:09:19 +0200 | [diff] [blame] | 16 | #include "parse-options.h" |
Elijah Newren | 08c46a4 | 2023-05-16 06:33:56 +0000 | [diff] [blame] | 17 | #include "read-cache.h" |
Elijah Newren | d1cbe1e | 2023-04-22 20:17:20 +0000 | [diff] [blame] | 18 | #include "repository.h" |
Mathieu Lienard--Mayor | 914dc02 | 2013-06-12 10:06:43 +0200 | [diff] [blame] | 19 | #include "string-list.h" |
Elijah Newren | e38da48 | 2023-03-21 06:26:05 +0000 | [diff] [blame] | 20 | #include "setup.h" |
Elijah Newren | baf889c | 2023-05-16 06:33:51 +0000 | [diff] [blame] | 21 | #include "sparse-index.h" |
Jens Lehmann | 293ab15 | 2012-09-26 20:21:13 +0200 | [diff] [blame] | 22 | #include "submodule.h" |
Nguyễn Thái Ngọc Duy | 29211a9 | 2013-07-14 15:35:42 +0700 | [diff] [blame] | 23 | #include "pathspec.h" |
Linus Torvalds | d9b814c | 2006-05-19 16:19:34 -0700 | [diff] [blame] | 24 | |
Pierre Habouzit | f09985c | 2007-10-05 21:09:19 +0200 | [diff] [blame] | 25 | static const char * const builtin_rm_usage[] = { |
Ævar Arnfjörð Bjarmason | d9054a1 | 2022-10-13 17:39:18 +0200 | [diff] [blame] | 26 | N_("git rm [-f | --force] [-n] [-r] [--cached] [--ignore-unmatch]\n" |
| 27 | " [--quiet] [--pathspec-from-file=<file> [--pathspec-file-nul]]\n" |
| 28 | " [--] [<pathspec>...]"), |
Pierre Habouzit | f09985c | 2007-10-05 21:09:19 +0200 | [diff] [blame] | 29 | NULL |
| 30 | }; |
Linus Torvalds | d9b814c | 2006-05-19 16:19:34 -0700 | [diff] [blame] | 31 | |
| 32 | static struct { |
| 33 | int nr, alloc; |
Jens Lehmann | 293ab15 | 2012-09-26 20:21:13 +0200 | [diff] [blame] | 34 | struct { |
| 35 | const char *name; |
| 36 | char is_submodule; |
| 37 | } *entry; |
Linus Torvalds | d9b814c | 2006-05-19 16:19:34 -0700 | [diff] [blame] | 38 | } list; |
| 39 | |
Jens Lehmann | 293ab15 | 2012-09-26 20:21:13 +0200 | [diff] [blame] | 40 | static int get_ours_cache_pos(const char *path, int pos) |
| 41 | { |
| 42 | int i = -pos - 1; |
| 43 | |
Ævar Arnfjörð Bjarmason | dc59418 | 2022-11-19 14:07:34 +0100 | [diff] [blame] | 44 | while ((i < the_index.cache_nr) && !strcmp(the_index.cache[i]->name, path)) { |
| 45 | if (ce_stage(the_index.cache[i]) == 2) |
Jens Lehmann | 293ab15 | 2012-09-26 20:21:13 +0200 | [diff] [blame] | 46 | return i; |
| 47 | i++; |
| 48 | } |
| 49 | return -1; |
| 50 | } |
| 51 | |
Mathieu Lienard--Mayor | 914dc02 | 2013-06-12 10:06:43 +0200 | [diff] [blame] | 52 | static 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 Boeckel | ed9bff0 | 2021-08-23 12:44:00 +0200 | [diff] [blame] | 66 | if (advice_enabled(ADVICE_RM_HINTS)) |
Mathieu Lienard--Mayor | 7e30944 | 2013-06-12 10:06:44 +0200 | [diff] [blame] | 67 | strbuf_addstr(&err_msg, hints_msg); |
Mathieu Lienard--Mayor | 914dc02 | 2013-06-12 10:06:43 +0200 | [diff] [blame] | 68 | *errs = error("%s", err_msg.buf); |
| 69 | strbuf_release(&err_msg); |
| 70 | } |
| 71 | } |
| 72 | |
Jeff King | cf7a901 | 2019-05-09 17:27:31 -0400 | [diff] [blame] | 73 | static void submodules_absorb_gitdir_if_needed(void) |
Jens Lehmann | 293ab15 | 2012-09-26 20:21:13 +0200 | [diff] [blame] | 74 | { |
| 75 | int i; |
Jens Lehmann | 293ab15 | 2012-09-26 20:21:13 +0200 | [diff] [blame] | 76 | for (i = 0; i < list.nr; i++) { |
| 77 | const char *name = list.entry[i].name; |
| 78 | int pos; |
Nguyễn Thái Ngọc Duy | 9c5e6c8 | 2013-07-09 22:29:00 +0700 | [diff] [blame] | 79 | const struct cache_entry *ce; |
Jens Lehmann | 293ab15 | 2012-09-26 20:21:13 +0200 | [diff] [blame] | 80 | |
Ævar Arnfjörð Bjarmason | 07047d6 | 2022-11-19 14:07:38 +0100 | [diff] [blame] | 81 | pos = index_name_pos(&the_index, name, strlen(name)); |
Jens Lehmann | 293ab15 | 2012-09-26 20:21:13 +0200 | [diff] [blame] | 82 | if (pos < 0) { |
| 83 | pos = get_ours_cache_pos(name, pos); |
| 84 | if (pos < 0) |
| 85 | continue; |
| 86 | } |
Ævar Arnfjörð Bjarmason | dc59418 | 2022-11-19 14:07:34 +0100 | [diff] [blame] | 87 | ce = the_index.cache[pos]; |
Jens Lehmann | 293ab15 | 2012-09-26 20:21:13 +0200 | [diff] [blame] | 88 | |
| 89 | if (!S_ISGITLINK(ce->ce_mode) || |
René Scharfe | dbe44fa | 2015-05-19 23:44:23 +0200 | [diff] [blame] | 90 | !file_exists(ce->name) || |
Jens Lehmann | 293ab15 | 2012-09-26 20:21:13 +0200 | [diff] [blame] | 91 | is_empty_dir(name)) |
| 92 | continue; |
| 93 | |
| 94 | if (!submodule_uses_gitfile(name)) |
Ævar Arnfjörð Bjarmason | f0a5e5a | 2022-12-20 13:39:50 +0100 | [diff] [blame] | 95 | absorb_git_dir_into_superproject(name, NULL); |
Jens Lehmann | 293ab15 | 2012-09-26 20:21:13 +0200 | [diff] [blame] | 96 | } |
Jens Lehmann | 293ab15 | 2012-09-26 20:21:13 +0200 | [diff] [blame] | 97 | } |
| 98 | |
brian m. carlson | 8ec46d7 | 2016-09-05 20:08:04 +0000 | [diff] [blame] | 99 | static int check_local_mod(struct object_id *head, int index_only) |
Junio C Hamano | 9f95069 | 2006-12-25 03:11:00 -0800 | [diff] [blame] | 100 | { |
Junio C Hamano | 69530cb | 2008-11-28 18:41:21 -0800 | [diff] [blame] | 101 | /* |
| 102 | * Items in list are already sorted in the cache order, |
Junio C Hamano | 9f95069 | 2006-12-25 03:11:00 -0800 | [diff] [blame] | 103 | * 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--Mayor | 914dc02 | 2013-06-12 10:06:43 +0200 | [diff] [blame] | 110 | struct string_list files_staged = STRING_LIST_INIT_NODUP; |
| 111 | struct string_list files_cached = STRING_LIST_INIT_NODUP; |
Mathieu Lienard--Mayor | 914dc02 | 2013-06-12 10:06:43 +0200 | [diff] [blame] | 112 | struct string_list files_local = STRING_LIST_INIT_NODUP; |
Junio C Hamano | 9f95069 | 2006-12-25 03:11:00 -0800 | [diff] [blame] | 113 | |
brian m. carlson | 8ec46d7 | 2016-09-05 20:08:04 +0000 | [diff] [blame] | 114 | no_head = is_null_oid(head); |
Junio C Hamano | 9f95069 | 2006-12-25 03:11:00 -0800 | [diff] [blame] | 115 | for (i = 0; i < list.nr; i++) { |
| 116 | struct stat st; |
| 117 | int pos; |
Nguyễn Thái Ngọc Duy | 9c5e6c8 | 2013-07-09 22:29:00 +0700 | [diff] [blame] | 118 | const struct cache_entry *ce; |
Jens Lehmann | 293ab15 | 2012-09-26 20:21:13 +0200 | [diff] [blame] | 119 | const char *name = list.entry[i].name; |
brian m. carlson | 8ec46d7 | 2016-09-05 20:08:04 +0000 | [diff] [blame] | 120 | struct object_id oid; |
Elijah Newren | 5ec1e72 | 2019-04-05 08:00:12 -0700 | [diff] [blame] | 121 | unsigned short mode; |
Matthieu Moy | bdecd9d | 2007-07-13 19:41:38 +0200 | [diff] [blame] | 122 | int local_changes = 0; |
| 123 | int staged_changes = 0; |
Junio C Hamano | 9f95069 | 2006-12-25 03:11:00 -0800 | [diff] [blame] | 124 | |
Ævar Arnfjörð Bjarmason | 07047d6 | 2022-11-19 14:07:38 +0100 | [diff] [blame] | 125 | pos = index_name_pos(&the_index, name, strlen(name)); |
Jens Lehmann | 293ab15 | 2012-09-26 20:21:13 +0200 | [diff] [blame] | 126 | 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ð Bjarmason | dc59418 | 2022-11-19 14:07:34 +0100 | [diff] [blame] | 135 | if (!S_ISGITLINK(the_index.cache[pos]->ce_mode) || |
Jens Lehmann | 293ab15 | 2012-09-26 20:21:13 +0200 | [diff] [blame] | 136 | is_empty_dir(name)) |
| 137 | continue; |
| 138 | } |
Ævar Arnfjörð Bjarmason | dc59418 | 2022-11-19 14:07:34 +0100 | [diff] [blame] | 139 | ce = the_index.cache[pos]; |
Junio C Hamano | 9f95069 | 2006-12-25 03:11:00 -0800 | [diff] [blame] | 140 | |
| 141 | if (lstat(ce->name, &st) < 0) { |
Junio C Hamano | c705420 | 2017-05-30 09:23:33 +0900 | [diff] [blame] | 142 | if (!is_missing_file_error(errno)) |
Nguyễn Thái Ngọc Duy | 7dcf3d9 | 2016-05-08 16:47:31 +0700 | [diff] [blame] | 143 | warning_errno(_("failed to stat '%s'"), ce->name); |
Junio C Hamano | 9f95069 | 2006-12-25 03:11:00 -0800 | [diff] [blame] | 144 | /* 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 Lehmann | 293ab15 | 2012-09-26 20:21:13 +0200 | [diff] [blame] | 151 | * directories unless they are submodules. |
Junio C Hamano | 9f95069 | 2006-12-25 03:11:00 -0800 | [diff] [blame] | 152 | */ |
Jens Lehmann | 293ab15 | 2012-09-26 20:21:13 +0200 | [diff] [blame] | 153 | if (!S_ISGITLINK(ce->ce_mode)) |
| 154 | continue; |
Junio C Hamano | 9f95069 | 2006-12-25 03:11:00 -0800 | [diff] [blame] | 155 | } |
Junio C Hamano | 69530cb | 2008-11-28 18:41:21 -0800 | [diff] [blame] | 156 | |
| 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 Lehmann | 293ab15 | 2012-09-26 20:21:13 +0200 | [diff] [blame] | 174 | * If it's a submodule, is its work tree modified? |
Junio C Hamano | 69530cb | 2008-11-28 18:41:21 -0800 | [diff] [blame] | 175 | */ |
Ævar Arnfjörð Bjarmason | 031b203 | 2022-11-19 14:07:33 +0100 | [diff] [blame] | 176 | if (ie_match_stat(&the_index, ce, &st, 0) || |
Jens Lehmann | 293ab15 | 2012-09-26 20:21:13 +0200 | [diff] [blame] | 177 | (S_ISGITLINK(ce->ce_mode) && |
Stefan Beller | 83b7696 | 2016-12-20 15:20:11 -0800 | [diff] [blame] | 178 | bad_to_remove_submodule(ce->name, |
| 179 | SUBMODULE_REMOVAL_DIE_ON_ERROR | |
| 180 | SUBMODULE_REMOVAL_IGNORE_IGNORED_UNTRACKED))) |
Matthieu Moy | bdecd9d | 2007-07-13 19:41:38 +0200 | [diff] [blame] | 181 | local_changes = 1; |
Junio C Hamano | 69530cb | 2008-11-28 18:41:21 -0800 | [diff] [blame] | 182 | |
| 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 King | e4d9516 | 2007-03-26 14:55:39 -0400 | [diff] [blame] | 189 | if (no_head |
Nguyễn Thái Ngọc Duy | 50ddb08 | 2019-06-27 16:28:49 +0700 | [diff] [blame] | 190 | || get_tree_entry(the_repository, head, name, &oid, &mode) |
Jeff King | e4d9516 | 2007-03-26 14:55:39 -0400 | [diff] [blame] | 191 | || ce->ce_mode != create_ce_mode(mode) |
Jeff King | 9001dc2 | 2018-08-28 17:22:48 -0400 | [diff] [blame] | 192 | || !oideq(&ce->oid, &oid)) |
Matthieu Moy | bdecd9d | 2007-07-13 19:41:38 +0200 | [diff] [blame] | 193 | staged_changes = 1; |
| 194 | |
Junio C Hamano | 69530cb | 2008-11-28 18:41:21 -0800 | [diff] [blame] | 195 | /* |
| 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 Duy | 895ff3b | 2015-08-22 08:08:05 +0700 | [diff] [blame] | 204 | if (!index_only || !ce_intent_to_add(ce)) |
Mathieu Lienard--Mayor | 914dc02 | 2013-06-12 10:06:43 +0200 | [diff] [blame] | 205 | string_list_append(&files_staged, name); |
Junio C Hamano | 69530cb | 2008-11-28 18:41:21 -0800 | [diff] [blame] | 206 | } |
Matthieu Moy | bdecd9d | 2007-07-13 19:41:38 +0200 | [diff] [blame] | 207 | else if (!index_only) { |
Matthieu Moy | bdecd9d | 2007-07-13 19:41:38 +0200 | [diff] [blame] | 208 | if (staged_changes) |
Mathieu Lienard--Mayor | 914dc02 | 2013-06-12 10:06:43 +0200 | [diff] [blame] | 209 | string_list_append(&files_cached, name); |
Stefan Beller | 55856a3 | 2016-12-27 11:03:14 -0800 | [diff] [blame] | 210 | if (local_changes) |
| 211 | string_list_append(&files_local, name); |
Matthieu Moy | bdecd9d | 2007-07-13 19:41:38 +0200 | [diff] [blame] | 212 | } |
Junio C Hamano | 9f95069 | 2006-12-25 03:11:00 -0800 | [diff] [blame] | 213 | } |
Mathieu Lienard--Mayor | 914dc02 | 2013-06-12 10:06:43 +0200 | [diff] [blame] | 214 | 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 Hamano | 658ff47 | 2013-07-25 23:05:17 -0700 | [diff] [blame] | 232 | |
Mathieu Lienard--Mayor | 914dc02 | 2013-06-12 10:06:43 +0200 | [diff] [blame] | 233 | 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 Hamano | 9f95069 | 2006-12-25 03:11:00 -0800 | [diff] [blame] | 242 | return errs; |
| 243 | } |
| 244 | |
Pierre Habouzit | f09985c | 2007-10-05 21:09:19 +0200 | [diff] [blame] | 245 | static int show_only = 0, force = 0, index_only = 0, recursive = 0, quiet = 0; |
Alexandr Miloslavskiy | 5f393dc | 2020-02-17 17:25:16 +0000 | [diff] [blame] | 246 | static int ignore_unmatch = 0, pathspec_file_nul; |
Derrick Stolee | f9786f9 | 2021-09-24 15:39:11 +0000 | [diff] [blame] | 247 | static int include_sparse; |
Alexandr Miloslavskiy | 5f393dc | 2020-02-17 17:25:16 +0000 | [diff] [blame] | 248 | static char *pathspec_from_file; |
Pierre Habouzit | f09985c | 2007-10-05 21:09:19 +0200 | [diff] [blame] | 249 | |
| 250 | static struct option builtin_rm_options[] = { |
Nguyễn Thái Ngọc Duy | 72bba2a | 2012-08-20 19:32:42 +0700 | [diff] [blame] | 251 | OPT__DRY_RUN(&show_only, N_("dry run")), |
| 252 | OPT__QUIET(&quiet, N_("do not list removed files")), |
Stefan Beller | d5d09d4 | 2013-08-03 13:51:19 +0200 | [diff] [blame] | 253 | OPT_BOOL( 0 , "cached", &index_only, N_("only remove from the index")), |
Nguyễn Thái Ngọc Duy | 44c9a6d | 2018-02-09 18:02:16 +0700 | [diff] [blame] | 254 | OPT__FORCE(&force, N_("override the up-to-date check"), PARSE_OPT_NOCOMPLETE), |
Stefan Beller | d5d09d4 | 2013-08-03 13:51:19 +0200 | [diff] [blame] | 255 | OPT_BOOL('r', NULL, &recursive, N_("allow recursive removal")), |
| 256 | OPT_BOOL( 0 , "ignore-unmatch", &ignore_unmatch, |
Nguyễn Thái Ngọc Duy | 72bba2a | 2012-08-20 19:32:42 +0700 | [diff] [blame] | 257 | N_("exit with a zero status even if nothing matched")), |
Derrick Stolee | f9786f9 | 2021-09-24 15:39:11 +0000 | [diff] [blame] | 258 | OPT_BOOL(0, "sparse", &include_sparse, N_("allow updating entries outside of the sparse-checkout cone")), |
Alexandr Miloslavskiy | 5f393dc | 2020-02-17 17:25:16 +0000 | [diff] [blame] | 259 | OPT_PATHSPEC_FROM_FILE(&pathspec_from_file), |
| 260 | OPT_PATHSPEC_FILE_NUL(&pathspec_file_nul), |
Pierre Habouzit | f09985c | 2007-10-05 21:09:19 +0200 | [diff] [blame] | 261 | OPT_END(), |
| 262 | }; |
| 263 | |
Linus Torvalds | a633fca | 2006-07-28 22:44:25 -0700 | [diff] [blame] | 264 | int cmd_rm(int argc, const char **argv, const char *prefix) |
Linus Torvalds | d9b814c | 2006-05-19 16:19:34 -0700 | [diff] [blame] | 265 | { |
Martin Ågren | 0fa5a2e | 2018-05-09 22:55:39 +0200 | [diff] [blame] | 266 | struct lock_file lock_file = LOCK_INIT; |
Matheus Tavares | d5f4b82 | 2021-04-08 17:41:28 -0300 | [diff] [blame] | 267 | int i, ret = 0; |
Nguyễn Thái Ngọc Duy | 29211a9 | 2013-07-14 15:35:42 +0700 | [diff] [blame] | 268 | struct pathspec pathspec; |
Linus Torvalds | d9b814c | 2006-05-19 16:19:34 -0700 | [diff] [blame] | 269 | char *seen; |
| 270 | |
Johannes Schindelin | ef90d6d | 2008-05-14 18:46:53 +0100 | [diff] [blame] | 271 | git_config(git_default_config, NULL); |
Linus Torvalds | d9b814c | 2006-05-19 16:19:34 -0700 | [diff] [blame] | 272 | |
Stephen Boyd | 3778292 | 2009-05-23 11:53:12 -0700 | [diff] [blame] | 273 | argc = parse_options(argc, argv, prefix, builtin_rm_options, |
| 274 | builtin_rm_usage, 0); |
Alexandr Miloslavskiy | 5f393dc | 2020-02-17 17:25:16 +0000 | [diff] [blame] | 275 | |
| 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 Avila | 246cac8 | 2022-01-05 20:02:24 +0000 | [diff] [blame] | 282 | die(_("'%s' and pathspec arguments cannot be used together"), "--pathspec-from-file"); |
Alexandr Miloslavskiy | 5f393dc | 2020-02-17 17:25:16 +0000 | [diff] [blame] | 283 | |
| 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 Avila | 6fa00ee | 2022-01-05 20:02:19 +0000 | [diff] [blame] | 288 | die(_("the option '%s' requires '%s'"), "--pathspec-file-nul", "--pathspec-from-file"); |
Alexandr Miloslavskiy | 5f393dc | 2020-02-17 17:25:16 +0000 | [diff] [blame] | 289 | } |
| 290 | |
| 291 | if (!pathspec.nr) |
| 292 | die(_("No pathspec was given. Which files should I remove?")); |
Linus Torvalds | d9b814c | 2006-05-19 16:19:34 -0700 | [diff] [blame] | 293 | |
Mike Hommey | 271bb08 | 2007-11-03 12:23:13 +0100 | [diff] [blame] | 294 | if (!index_only) |
| 295 | setup_work_tree(); |
| 296 | |
Shaoxuan Yuan | ede241c | 2022-08-07 12:13:35 +0800 | [diff] [blame] | 297 | prepare_repo_settings(the_repository); |
| 298 | the_repository->settings.command_requires_full_index = 0; |
Ævar Arnfjörð Bjarmason | 07047d6 | 2022-11-19 14:07:38 +0100 | [diff] [blame] | 299 | repo_hold_locked_index(the_repository, &lock_file, LOCK_DIE_ON_ERROR); |
Olivier Marin | 4d26467 | 2008-07-19 18:24:46 +0200 | [diff] [blame] | 300 | |
Ævar Arnfjörð Bjarmason | 07047d6 | 2022-11-19 14:07:38 +0100 | [diff] [blame] | 301 | if (repo_read_index(the_repository) < 0) |
Ævar Arnfjörð Bjarmason | b9b537f | 2011-02-22 23:42:05 +0000 | [diff] [blame] | 302 | die(_("index file corrupt")); |
Olivier Marin | 4d26467 | 2008-07-19 18:24:46 +0200 | [diff] [blame] | 303 | |
Junio C Hamano | b2b1f61 | 2019-07-17 13:28:24 -0700 | [diff] [blame] | 304 | refresh_index(&the_index, REFRESH_QUIET|REFRESH_UNMERGED, &pathspec, NULL, NULL); |
Nguyễn Thái Ngọc Duy | 4e1a7ba | 2010-01-17 15:43:13 +0700 | [diff] [blame] | 305 | |
Nguyễn Thái Ngọc Duy | 29211a9 | 2013-07-14 15:35:42 +0700 | [diff] [blame] | 306 | seen = xcalloc(pathspec.nr, 1); |
Linus Torvalds | d9b814c | 2006-05-19 16:19:34 -0700 | [diff] [blame] | 307 | |
Shaoxuan Yuan | bcf96cf | 2022-08-07 12:13:34 +0800 | [diff] [blame] | 308 | if (pathspec_needs_expanded_index(&the_index, &pathspec)) |
| 309 | ensure_full_index(&the_index); |
| 310 | |
Ævar Arnfjörð Bjarmason | dc59418 | 2022-11-19 14:07:34 +0100 | [diff] [blame] | 311 | for (i = 0; i < the_index.cache_nr; i++) { |
| 312 | const struct cache_entry *ce = the_index.cache[i]; |
Derrick Stolee | f9786f9 | 2021-09-24 15:39:11 +0000 | [diff] [blame] | 313 | |
Derrick Stolee | d7c4415 | 2021-09-24 15:39:12 +0000 | [diff] [blame] | 314 | if (!include_sparse && |
| 315 | (ce_skip_worktree(ce) || |
| 316 | !path_in_sparse_checkout(ce->name, &the_index))) |
Matheus Tavares | d5f4b82 | 2021-04-08 17:41:28 -0300 | [diff] [blame] | 317 | continue; |
Nguyễn Thái Ngọc Duy | 6d2df28 | 2018-08-13 18:14:22 +0200 | [diff] [blame] | 318 | if (!ce_path_match(&the_index, ce, &pathspec, seen)) |
Linus Torvalds | d9b814c | 2006-05-19 16:19:34 -0700 | [diff] [blame] | 319 | continue; |
Jens Lehmann | 293ab15 | 2012-09-26 20:21:13 +0200 | [diff] [blame] | 320 | ALLOC_GROW(list.entry, list.nr + 1, list.alloc); |
Karsten Blees | 5699d17 | 2013-11-14 20:24:37 +0100 | [diff] [blame] | 321 | list.entry[list.nr].name = xstrdup(ce->name); |
Jens Lehmann | 95c1641 | 2013-08-06 21:15:25 +0200 | [diff] [blame] | 322 | list.entry[list.nr].is_submodule = S_ISGITLINK(ce->ce_mode); |
| 323 | if (list.entry[list.nr++].is_submodule && |
Brandon Williams | 91b8348 | 2017-08-02 12:49:20 -0700 | [diff] [blame] | 324 | !is_staging_gitmodules_ok(&the_index)) |
Nguyễn Thái Ngọc Duy | 1a07e59 | 2018-07-21 09:49:19 +0200 | [diff] [blame] | 325 | die(_("please stage your changes to .gitmodules or stash them to proceed")); |
Linus Torvalds | d9b814c | 2006-05-19 16:19:34 -0700 | [diff] [blame] | 326 | } |
| 327 | |
Nguyễn Thái Ngọc Duy | 29211a9 | 2013-07-14 15:35:42 +0700 | [diff] [blame] | 328 | if (pathspec.nr) { |
| 329 | const char *original; |
Steven Grimm | bb1faf0 | 2007-04-16 00:53:24 -0700 | [diff] [blame] | 330 | int seen_any = 0; |
Matheus Tavares | d5f4b82 | 2021-04-08 17:41:28 -0300 | [diff] [blame] | 331 | char *skip_worktree_seen = NULL; |
| 332 | struct string_list only_match_skip_worktree = STRING_LIST_INIT_NODUP; |
| 333 | |
Nguyễn Thái Ngọc Duy | 29211a9 | 2013-07-14 15:35:42 +0700 | [diff] [blame] | 334 | for (i = 0; i < pathspec.nr; i++) { |
| 335 | original = pathspec.items[i].original; |
Matheus Tavares | d5f4b82 | 2021-04-08 17:41:28 -0300 | [diff] [blame] | 336 | if (seen[i]) |
Steven Grimm | bb1faf0 | 2007-04-16 00:53:24 -0700 | [diff] [blame] | 337 | seen_any = 1; |
Matheus Tavares | d5f4b82 | 2021-04-08 17:41:28 -0300 | [diff] [blame] | 338 | else if (ignore_unmatch) |
| 339 | continue; |
Derrick Stolee | f9786f9 | 2021-09-24 15:39:11 +0000 | [diff] [blame] | 340 | else if (!include_sparse && |
| 341 | matches_skip_worktree(&pathspec, i, &skip_worktree_seen)) |
Matheus Tavares | d5f4b82 | 2021-04-08 17:41:28 -0300 | [diff] [blame] | 342 | string_list_append(&only_match_skip_worktree, original); |
| 343 | else |
| 344 | die(_("pathspec '%s' did not match any files"), original); |
| 345 | |
Junio C Hamano | 9f95069 | 2006-12-25 03:11:00 -0800 | [diff] [blame] | 346 | if (!recursive && seen[i] == MATCHED_RECURSIVELY) |
Ævar Arnfjörð Bjarmason | b9b537f | 2011-02-22 23:42:05 +0000 | [diff] [blame] | 347 | die(_("not removing '%s' recursively without -r"), |
Nguyễn Thái Ngọc Duy | 29211a9 | 2013-07-14 15:35:42 +0700 | [diff] [blame] | 348 | *original ? original : "."); |
Stefan Beller | f8aae0b | 2013-08-08 19:52:41 +0200 | [diff] [blame] | 349 | } |
Steven Grimm | bb1faf0 | 2007-04-16 00:53:24 -0700 | [diff] [blame] | 350 | |
Matheus Tavares | d5f4b82 | 2021-04-08 17:41:28 -0300 | [diff] [blame] | 351 | 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 Hamano | b02f5ae | 2013-09-09 14:36:15 -0700 | [diff] [blame] | 358 | if (!seen_any) |
Matheus Tavares | d5f4b82 | 2021-04-08 17:41:28 -0300 | [diff] [blame] | 359 | exit(ret); |
Linus Torvalds | d9b814c | 2006-05-19 16:19:34 -0700 | [diff] [blame] | 360 | } |
Andrzej Hunt | 37be119 | 2021-04-25 14:16:19 +0000 | [diff] [blame] | 361 | clear_pathspec(&pathspec); |
| 362 | free(seen); |
Linus Torvalds | d9b814c | 2006-05-19 16:19:34 -0700 | [diff] [blame] | 363 | |
Stefan Beller | 55856a3 | 2016-12-27 11:03:14 -0800 | [diff] [blame] | 364 | if (!index_only) |
Jeff King | cf7a901 | 2019-05-09 17:27:31 -0400 | [diff] [blame] | 365 | submodules_absorb_gitdir_if_needed(); |
Stefan Beller | 55856a3 | 2016-12-27 11:03:14 -0800 | [diff] [blame] | 366 | |
Linus Torvalds | d9b814c | 2006-05-19 16:19:34 -0700 | [diff] [blame] | 367 | /* |
Junio C Hamano | 9f95069 | 2006-12-25 03:11:00 -0800 | [diff] [blame] | 368 | * 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 Grimm | 9474eda | 2007-04-16 01:17:32 -0700 | [diff] [blame] | 372 | * rm F; git rm F |
Junio C Hamano | 9f95069 | 2006-12-25 03:11:00 -0800 | [diff] [blame] | 373 | * |
| 374 | * Further, if HEAD commit exists, "diff-index --cached" must |
| 375 | * report no changes unless forced. |
| 376 | */ |
| 377 | if (!force) { |
brian m. carlson | 8ec46d7 | 2016-09-05 20:08:04 +0000 | [diff] [blame] | 378 | struct object_id oid; |
Ævar Arnfjörð Bjarmason | d850b7a | 2023-03-28 15:58:46 +0200 | [diff] [blame] | 379 | if (repo_get_oid(the_repository, "HEAD", &oid)) |
brian m. carlson | 8ec46d7 | 2016-09-05 20:08:04 +0000 | [diff] [blame] | 380 | oidclr(&oid); |
| 381 | if (check_local_mod(&oid, index_only)) |
Junio C Hamano | 9f95069 | 2006-12-25 03:11:00 -0800 | [diff] [blame] | 382 | exit(1); |
| 383 | } |
| 384 | |
| 385 | /* |
Linus Torvalds | d9b814c | 2006-05-19 16:19:34 -0700 | [diff] [blame] | 386 | * First remove the names from the index: we won't commit |
Junio C Hamano | 9f95069 | 2006-12-25 03:11:00 -0800 | [diff] [blame] | 387 | * the index unless all of them succeed. |
Linus Torvalds | d9b814c | 2006-05-19 16:19:34 -0700 | [diff] [blame] | 388 | */ |
| 389 | for (i = 0; i < list.nr; i++) { |
Jens Lehmann | 293ab15 | 2012-09-26 20:21:13 +0200 | [diff] [blame] | 390 | const char *path = list.entry[i].name; |
Steven Grimm | b48caa2 | 2007-04-16 00:46:48 -0700 | [diff] [blame] | 391 | if (!quiet) |
| 392 | printf("rm '%s'\n", path); |
Linus Torvalds | d9b814c | 2006-05-19 16:19:34 -0700 | [diff] [blame] | 393 | |
Ævar Arnfjörð Bjarmason | 031b203 | 2022-11-19 14:07:33 +0100 | [diff] [blame] | 394 | if (remove_file_from_index(&the_index, path)) |
Ævar Arnfjörð Bjarmason | b9b537f | 2011-02-22 23:42:05 +0000 | [diff] [blame] | 395 | die(_("git rm: unable to remove %s"), path); |
Linus Torvalds | d9b814c | 2006-05-19 16:19:34 -0700 | [diff] [blame] | 396 | } |
| 397 | |
Junio C Hamano | 7612a1e | 2006-06-08 21:11:25 -0700 | [diff] [blame] | 398 | if (show_only) |
| 399 | return 0; |
| 400 | |
Linus Torvalds | d9b814c | 2006-05-19 16:19:34 -0700 | [diff] [blame] | 401 | /* |
Junio C Hamano | 646ac22 | 2007-01-11 14:58:47 -0800 | [diff] [blame] | 402 | * Then, unless we used "--cached", remove the filenames from |
Junio C Hamano | 9f95069 | 2006-12-25 03:11:00 -0800 | [diff] [blame] | 403 | * the workspace. If we fail to remove the first one, we |
Linus Torvalds | d9b814c | 2006-05-19 16:19:34 -0700 | [diff] [blame] | 404 | * 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 Roskin | 82e5a82 | 2006-07-10 01:50:18 -0400 | [diff] [blame] | 406 | * by then we've already committed ourselves and can't fail |
Linus Torvalds | d9b814c | 2006-05-19 16:19:34 -0700 | [diff] [blame] | 407 | * in the middle) |
| 408 | */ |
Junio C Hamano | 9f95069 | 2006-12-25 03:11:00 -0800 | [diff] [blame] | 409 | if (!index_only) { |
Jens Lehmann | 95c1641 | 2013-08-06 21:15:25 +0200 | [diff] [blame] | 410 | int removed = 0, gitmodules_modified = 0; |
René Scharfe | 590fc05 | 2017-02-11 20:51:08 +0100 | [diff] [blame] | 411 | struct strbuf buf = STRBUF_INIT; |
Elijah Newren | 580a5d7 | 2021-12-09 05:08:34 +0000 | [diff] [blame] | 412 | int flag = force ? REMOVE_DIR_PURGE_ORIGINAL_CWD : 0; |
Linus Torvalds | d9b814c | 2006-05-19 16:19:34 -0700 | [diff] [blame] | 413 | for (i = 0; i < list.nr; i++) { |
Jens Lehmann | 293ab15 | 2012-09-26 20:21:13 +0200 | [diff] [blame] | 414 | const char *path = list.entry[i].name; |
| 415 | if (list.entry[i].is_submodule) { |
René Scharfe | 590fc05 | 2017-02-11 20:51:08 +0100 | [diff] [blame] | 416 | strbuf_reset(&buf); |
Stefan Beller | 55856a3 | 2016-12-27 11:03:14 -0800 | [diff] [blame] | 417 | strbuf_addstr(&buf, path); |
Elijah Newren | 580a5d7 | 2021-12-09 05:08:34 +0000 | [diff] [blame] | 418 | if (remove_dir_recursively(&buf, flag)) |
Stefan Beller | 55856a3 | 2016-12-27 11:03:14 -0800 | [diff] [blame] | 419 | die(_("could not remove '%s'"), path); |
Stefan Beller | 55856a3 | 2016-12-27 11:03:14 -0800 | [diff] [blame] | 420 | |
| 421 | removed = 1; |
| 422 | if (!remove_path_from_gitmodules(path)) |
| 423 | gitmodules_modified = 1; |
| 424 | continue; |
Jens Lehmann | 293ab15 | 2012-09-26 20:21:13 +0200 | [diff] [blame] | 425 | } |
Alex Riesen | 175a494 | 2008-09-27 00:59:14 +0200 | [diff] [blame] | 426 | if (!remove_path(path)) { |
Linus Torvalds | d9b814c | 2006-05-19 16:19:34 -0700 | [diff] [blame] | 427 | removed = 1; |
| 428 | continue; |
| 429 | } |
| 430 | if (!removed) |
Thomas Rast | d824cbb | 2009-06-27 17:58:46 +0200 | [diff] [blame] | 431 | die_errno("git rm: '%s'", path); |
Linus Torvalds | d9b814c | 2006-05-19 16:19:34 -0700 | [diff] [blame] | 432 | } |
René Scharfe | 590fc05 | 2017-02-11 20:51:08 +0100 | [diff] [blame] | 433 | strbuf_release(&buf); |
Jens Lehmann | 95c1641 | 2013-08-06 21:15:25 +0200 | [diff] [blame] | 434 | if (gitmodules_modified) |
Brandon Williams | 3b8317a | 2017-12-12 11:53:50 -0800 | [diff] [blame] | 435 | stage_updated_gitmodules(&the_index); |
Linus Torvalds | d9b814c | 2006-05-19 16:19:34 -0700 | [diff] [blame] | 436 | } |
| 437 | |
Martin Ågren | 6100081 | 2018-03-01 21:40:20 +0100 | [diff] [blame] | 438 | if (write_locked_index(&the_index, &lock_file, |
| 439 | COMMIT_LOCK | SKIP_IF_UNCHANGED)) |
| 440 | die(_("Unable to write new index file")); |
Linus Torvalds | d9b814c | 2006-05-19 16:19:34 -0700 | [diff] [blame] | 441 | |
Matheus Tavares | d5f4b82 | 2021-04-08 17:41:28 -0300 | [diff] [blame] | 442 | return ret; |
Linus Torvalds | d9b814c | 2006-05-19 16:19:34 -0700 | [diff] [blame] | 443 | } |