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 | */ |
Nguyễn Thái Ngọc Duy | f8adbec | 2019-01-24 15:29:12 +0700 | [diff] [blame] | 6 | #define USE_THE_INDEX_COMPATIBILITY_MACROS |
Linus Torvalds | d9b814c | 2006-05-19 16:19:34 -0700 | [diff] [blame] | 7 | #include "builtin.h" |
Brandon Williams | b2141fc | 2017-06-14 11:07:36 -0700 | [diff] [blame] | 8 | #include "config.h" |
Michael Haggerty | 697cc8e | 2014-10-01 12:28:42 +0200 | [diff] [blame] | 9 | #include "lockfile.h" |
Linus Torvalds | d9b814c | 2006-05-19 16:19:34 -0700 | [diff] [blame] | 10 | #include "dir.h" |
Junio C Hamano | 3fb3cc6 | 2006-05-23 01:31:38 -0700 | [diff] [blame] | 11 | #include "cache-tree.h" |
Junio C Hamano | 9f95069 | 2006-12-25 03:11:00 -0800 | [diff] [blame] | 12 | #include "tree-walk.h" |
Pierre Habouzit | f09985c | 2007-10-05 21:09:19 +0200 | [diff] [blame] | 13 | #include "parse-options.h" |
Mathieu Lienard--Mayor | 914dc02 | 2013-06-12 10:06:43 +0200 | [diff] [blame] | 14 | #include "string-list.h" |
Jens Lehmann | 293ab15 | 2012-09-26 20:21:13 +0200 | [diff] [blame] | 15 | #include "submodule.h" |
Nguyễn Thái Ngọc Duy | 29211a9 | 2013-07-14 15:35:42 +0700 | [diff] [blame] | 16 | #include "pathspec.h" |
Linus Torvalds | d9b814c | 2006-05-19 16:19:34 -0700 | [diff] [blame] | 17 | |
Pierre Habouzit | f09985c | 2007-10-05 21:09:19 +0200 | [diff] [blame] | 18 | static const char * const builtin_rm_usage[] = { |
Alex Henrie | 9c9b4f2 | 2015-01-13 00:44:47 -0700 | [diff] [blame] | 19 | N_("git rm [<options>] [--] <file>..."), |
Pierre Habouzit | f09985c | 2007-10-05 21:09:19 +0200 | [diff] [blame] | 20 | NULL |
| 21 | }; |
Linus Torvalds | d9b814c | 2006-05-19 16:19:34 -0700 | [diff] [blame] | 22 | |
| 23 | static struct { |
| 24 | int nr, alloc; |
Jens Lehmann | 293ab15 | 2012-09-26 20:21:13 +0200 | [diff] [blame] | 25 | struct { |
| 26 | const char *name; |
| 27 | char is_submodule; |
| 28 | } *entry; |
Linus Torvalds | d9b814c | 2006-05-19 16:19:34 -0700 | [diff] [blame] | 29 | } list; |
| 30 | |
Jens Lehmann | 293ab15 | 2012-09-26 20:21:13 +0200 | [diff] [blame] | 31 | static 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--Mayor | 914dc02 | 2013-06-12 10:06:43 +0200 | [diff] [blame] | 43 | static 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--Mayor | 7e30944 | 2013-06-12 10:06:44 +0200 | [diff] [blame] | 57 | if (advice_rm_hints) |
| 58 | strbuf_addstr(&err_msg, hints_msg); |
Mathieu Lienard--Mayor | 914dc02 | 2013-06-12 10:06:43 +0200 | [diff] [blame] | 59 | *errs = error("%s", err_msg.buf); |
| 60 | strbuf_release(&err_msg); |
| 61 | } |
| 62 | } |
| 63 | |
Jeff King | cf7a901 | 2019-05-09 17:27:31 -0400 | [diff] [blame] | 64 | static void submodules_absorb_gitdir_if_needed(void) |
Jens Lehmann | 293ab15 | 2012-09-26 20:21:13 +0200 | [diff] [blame] | 65 | { |
| 66 | int i; |
Jens Lehmann | 293ab15 | 2012-09-26 20:21:13 +0200 | [diff] [blame] | 67 | for (i = 0; i < list.nr; i++) { |
| 68 | const char *name = list.entry[i].name; |
| 69 | int pos; |
Nguyễn Thái Ngọc Duy | 9c5e6c8 | 2013-07-09 22:29:00 +0700 | [diff] [blame] | 70 | const struct cache_entry *ce; |
Jens Lehmann | 293ab15 | 2012-09-26 20:21:13 +0200 | [diff] [blame] | 71 | |
| 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é Scharfe | dbe44fa | 2015-05-19 23:44:23 +0200 | [diff] [blame] | 81 | !file_exists(ce->name) || |
Jens Lehmann | 293ab15 | 2012-09-26 20:21:13 +0200 | [diff] [blame] | 82 | is_empty_dir(name)) |
| 83 | continue; |
| 84 | |
| 85 | if (!submodule_uses_gitfile(name)) |
Jeff King | cf7a901 | 2019-05-09 17:27:31 -0400 | [diff] [blame] | 86 | absorb_git_dir_into_superproject(name, |
Stefan Beller | 55856a3 | 2016-12-27 11:03:14 -0800 | [diff] [blame] | 87 | ABSORB_GITDIR_RECURSE_SUBMODULES); |
Jens Lehmann | 293ab15 | 2012-09-26 20:21:13 +0200 | [diff] [blame] | 88 | } |
Jens Lehmann | 293ab15 | 2012-09-26 20:21:13 +0200 | [diff] [blame] | 89 | } |
| 90 | |
brian m. carlson | 8ec46d7 | 2016-09-05 20:08:04 +0000 | [diff] [blame] | 91 | 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] | 92 | { |
Junio C Hamano | 69530cb | 2008-11-28 18:41:21 -0800 | [diff] [blame] | 93 | /* |
| 94 | * Items in list are already sorted in the cache order, |
Junio C Hamano | 9f95069 | 2006-12-25 03:11:00 -0800 | [diff] [blame] | 95 | * 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--Mayor | 914dc02 | 2013-06-12 10:06:43 +0200 | [diff] [blame] | 102 | struct string_list files_staged = STRING_LIST_INIT_NODUP; |
| 103 | struct string_list files_cached = STRING_LIST_INIT_NODUP; |
Mathieu Lienard--Mayor | 914dc02 | 2013-06-12 10:06:43 +0200 | [diff] [blame] | 104 | struct string_list files_local = STRING_LIST_INIT_NODUP; |
Junio C Hamano | 9f95069 | 2006-12-25 03:11:00 -0800 | [diff] [blame] | 105 | |
brian m. carlson | 8ec46d7 | 2016-09-05 20:08:04 +0000 | [diff] [blame] | 106 | no_head = is_null_oid(head); |
Junio C Hamano | 9f95069 | 2006-12-25 03:11:00 -0800 | [diff] [blame] | 107 | for (i = 0; i < list.nr; i++) { |
| 108 | struct stat st; |
| 109 | int pos; |
Nguyễn Thái Ngọc Duy | 9c5e6c8 | 2013-07-09 22:29:00 +0700 | [diff] [blame] | 110 | const struct cache_entry *ce; |
Jens Lehmann | 293ab15 | 2012-09-26 20:21:13 +0200 | [diff] [blame] | 111 | const char *name = list.entry[i].name; |
brian m. carlson | 8ec46d7 | 2016-09-05 20:08:04 +0000 | [diff] [blame] | 112 | struct object_id oid; |
Elijah Newren | 5ec1e72 | 2019-04-05 08:00:12 -0700 | [diff] [blame] | 113 | unsigned short mode; |
Matthieu Moy | bdecd9d | 2007-07-13 19:41:38 +0200 | [diff] [blame] | 114 | int local_changes = 0; |
| 115 | int staged_changes = 0; |
Junio C Hamano | 9f95069 | 2006-12-25 03:11:00 -0800 | [diff] [blame] | 116 | |
| 117 | pos = cache_name_pos(name, strlen(name)); |
Jens Lehmann | 293ab15 | 2012-09-26 20:21:13 +0200 | [diff] [blame] | 118 | 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 Hamano | 9f95069 | 2006-12-25 03:11:00 -0800 | [diff] [blame] | 131 | ce = active_cache[pos]; |
| 132 | |
| 133 | if (lstat(ce->name, &st) < 0) { |
Junio C Hamano | c705420 | 2017-05-30 09:23:33 +0900 | [diff] [blame] | 134 | if (!is_missing_file_error(errno)) |
Nguyễn Thái Ngọc Duy | 7dcf3d9 | 2016-05-08 16:47:31 +0700 | [diff] [blame] | 135 | warning_errno(_("failed to stat '%s'"), ce->name); |
Junio C Hamano | 9f95069 | 2006-12-25 03:11:00 -0800 | [diff] [blame] | 136 | /* 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 Lehmann | 293ab15 | 2012-09-26 20:21:13 +0200 | [diff] [blame] | 143 | * directories unless they are submodules. |
Junio C Hamano | 9f95069 | 2006-12-25 03:11:00 -0800 | [diff] [blame] | 144 | */ |
Jens Lehmann | 293ab15 | 2012-09-26 20:21:13 +0200 | [diff] [blame] | 145 | if (!S_ISGITLINK(ce->ce_mode)) |
| 146 | continue; |
Junio C Hamano | 9f95069 | 2006-12-25 03:11:00 -0800 | [diff] [blame] | 147 | } |
Junio C Hamano | 69530cb | 2008-11-28 18:41:21 -0800 | [diff] [blame] | 148 | |
| 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 Lehmann | 293ab15 | 2012-09-26 20:21:13 +0200 | [diff] [blame] | 166 | * If it's a submodule, is its work tree modified? |
Junio C Hamano | 69530cb | 2008-11-28 18:41:21 -0800 | [diff] [blame] | 167 | */ |
Jens Lehmann | 293ab15 | 2012-09-26 20:21:13 +0200 | [diff] [blame] | 168 | if (ce_match_stat(ce, &st, 0) || |
| 169 | (S_ISGITLINK(ce->ce_mode) && |
Stefan Beller | 83b7696 | 2016-12-20 15:20:11 -0800 | [diff] [blame] | 170 | bad_to_remove_submodule(ce->name, |
| 171 | SUBMODULE_REMOVAL_DIE_ON_ERROR | |
| 172 | SUBMODULE_REMOVAL_IGNORE_IGNORED_UNTRACKED))) |
Matthieu Moy | bdecd9d | 2007-07-13 19:41:38 +0200 | [diff] [blame] | 173 | local_changes = 1; |
Junio C Hamano | 69530cb | 2008-11-28 18:41:21 -0800 | [diff] [blame] | 174 | |
| 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 King | e4d9516 | 2007-03-26 14:55:39 -0400 | [diff] [blame] | 181 | if (no_head |
Nguyễn Thái Ngọc Duy | 50ddb08 | 2019-06-27 16:28:49 +0700 | [diff] [blame] | 182 | || get_tree_entry(the_repository, head, name, &oid, &mode) |
Jeff King | e4d9516 | 2007-03-26 14:55:39 -0400 | [diff] [blame] | 183 | || ce->ce_mode != create_ce_mode(mode) |
Jeff King | 9001dc2 | 2018-08-28 17:22:48 -0400 | [diff] [blame] | 184 | || !oideq(&ce->oid, &oid)) |
Matthieu Moy | bdecd9d | 2007-07-13 19:41:38 +0200 | [diff] [blame] | 185 | staged_changes = 1; |
| 186 | |
Junio C Hamano | 69530cb | 2008-11-28 18:41:21 -0800 | [diff] [blame] | 187 | /* |
| 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 Duy | 895ff3b | 2015-08-22 08:08:05 +0700 | [diff] [blame] | 196 | if (!index_only || !ce_intent_to_add(ce)) |
Mathieu Lienard--Mayor | 914dc02 | 2013-06-12 10:06:43 +0200 | [diff] [blame] | 197 | string_list_append(&files_staged, name); |
Junio C Hamano | 69530cb | 2008-11-28 18:41:21 -0800 | [diff] [blame] | 198 | } |
Matthieu Moy | bdecd9d | 2007-07-13 19:41:38 +0200 | [diff] [blame] | 199 | else if (!index_only) { |
Matthieu Moy | bdecd9d | 2007-07-13 19:41:38 +0200 | [diff] [blame] | 200 | if (staged_changes) |
Mathieu Lienard--Mayor | 914dc02 | 2013-06-12 10:06:43 +0200 | [diff] [blame] | 201 | string_list_append(&files_cached, name); |
Stefan Beller | 55856a3 | 2016-12-27 11:03:14 -0800 | [diff] [blame] | 202 | if (local_changes) |
| 203 | string_list_append(&files_local, name); |
Matthieu Moy | bdecd9d | 2007-07-13 19:41:38 +0200 | [diff] [blame] | 204 | } |
Junio C Hamano | 9f95069 | 2006-12-25 03:11:00 -0800 | [diff] [blame] | 205 | } |
Mathieu Lienard--Mayor | 914dc02 | 2013-06-12 10:06:43 +0200 | [diff] [blame] | 206 | 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 Hamano | 658ff47 | 2013-07-25 23:05:17 -0700 | [diff] [blame] | 224 | |
Mathieu Lienard--Mayor | 914dc02 | 2013-06-12 10:06:43 +0200 | [diff] [blame] | 225 | 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 Hamano | 9f95069 | 2006-12-25 03:11:00 -0800 | [diff] [blame] | 234 | return errs; |
| 235 | } |
| 236 | |
Pierre Habouzit | f09985c | 2007-10-05 21:09:19 +0200 | [diff] [blame] | 237 | 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] | 238 | static int ignore_unmatch = 0, pathspec_file_nul; |
| 239 | static char *pathspec_from_file; |
Pierre Habouzit | f09985c | 2007-10-05 21:09:19 +0200 | [diff] [blame] | 240 | |
| 241 | static struct option builtin_rm_options[] = { |
Nguyễn Thái Ngọc Duy | 72bba2a | 2012-08-20 19:32:42 +0700 | [diff] [blame] | 242 | OPT__DRY_RUN(&show_only, N_("dry run")), |
| 243 | OPT__QUIET(&quiet, N_("do not list removed files")), |
Stefan Beller | d5d09d4 | 2013-08-03 13:51:19 +0200 | [diff] [blame] | 244 | 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] | 245 | 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] | 246 | OPT_BOOL('r', NULL, &recursive, N_("allow recursive removal")), |
| 247 | OPT_BOOL( 0 , "ignore-unmatch", &ignore_unmatch, |
Nguyễn Thái Ngọc Duy | 72bba2a | 2012-08-20 19:32:42 +0700 | [diff] [blame] | 248 | N_("exit with a zero status even if nothing matched")), |
Alexandr Miloslavskiy | 5f393dc | 2020-02-17 17:25:16 +0000 | [diff] [blame] | 249 | OPT_PATHSPEC_FROM_FILE(&pathspec_from_file), |
| 250 | OPT_PATHSPEC_FILE_NUL(&pathspec_file_nul), |
Pierre Habouzit | f09985c | 2007-10-05 21:09:19 +0200 | [diff] [blame] | 251 | OPT_END(), |
| 252 | }; |
| 253 | |
Linus Torvalds | a633fca | 2006-07-28 22:44:25 -0700 | [diff] [blame] | 254 | int cmd_rm(int argc, const char **argv, const char *prefix) |
Linus Torvalds | d9b814c | 2006-05-19 16:19:34 -0700 | [diff] [blame] | 255 | { |
Martin Ågren | 0fa5a2e | 2018-05-09 22:55:39 +0200 | [diff] [blame] | 256 | struct lock_file lock_file = LOCK_INIT; |
Nguyễn Thái Ngọc Duy | 03b8664 | 2014-06-13 19:19:23 +0700 | [diff] [blame] | 257 | int i; |
Nguyễn Thái Ngọc Duy | 29211a9 | 2013-07-14 15:35:42 +0700 | [diff] [blame] | 258 | struct pathspec pathspec; |
Linus Torvalds | d9b814c | 2006-05-19 16:19:34 -0700 | [diff] [blame] | 259 | char *seen; |
| 260 | |
Johannes Schindelin | ef90d6d | 2008-05-14 18:46:53 +0100 | [diff] [blame] | 261 | git_config(git_default_config, NULL); |
Linus Torvalds | d9b814c | 2006-05-19 16:19:34 -0700 | [diff] [blame] | 262 | |
Stephen Boyd | 3778292 | 2009-05-23 11:53:12 -0700 | [diff] [blame] | 263 | argc = parse_options(argc, argv, prefix, builtin_rm_options, |
| 264 | builtin_rm_usage, 0); |
Alexandr Miloslavskiy | 5f393dc | 2020-02-17 17:25:16 +0000 | [diff] [blame] | 265 | |
| 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 Torvalds | d9b814c | 2006-05-19 16:19:34 -0700 | [diff] [blame] | 283 | |
Mike Hommey | 271bb08 | 2007-11-03 12:23:13 +0100 | [diff] [blame] | 284 | if (!index_only) |
| 285 | setup_work_tree(); |
| 286 | |
Junio C Hamano | b3e83cc | 2016-12-07 10:33:54 -0800 | [diff] [blame] | 287 | hold_locked_index(&lock_file, LOCK_DIE_ON_ERROR); |
Olivier Marin | 4d26467 | 2008-07-19 18:24:46 +0200 | [diff] [blame] | 288 | |
| 289 | if (read_cache() < 0) |
Ævar Arnfjörð Bjarmason | b9b537f | 2011-02-22 23:42:05 +0000 | [diff] [blame] | 290 | die(_("index file corrupt")); |
Olivier Marin | 4d26467 | 2008-07-19 18:24:46 +0200 | [diff] [blame] | 291 | |
Junio C Hamano | b2b1f61 | 2019-07-17 13:28:24 -0700 | [diff] [blame] | 292 | 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] | 293 | |
Nguyễn Thái Ngọc Duy | 29211a9 | 2013-07-14 15:35:42 +0700 | [diff] [blame] | 294 | seen = xcalloc(pathspec.nr, 1); |
Linus Torvalds | d9b814c | 2006-05-19 16:19:34 -0700 | [diff] [blame] | 295 | |
Derrick Stolee | e43e2a1 | 2021-04-01 01:49:51 +0000 | [diff] [blame^] | 296 | /* TODO: audit for interaction with sparse-index. */ |
| 297 | ensure_full_index(&the_index); |
Linus Torvalds | d9b814c | 2006-05-19 16:19:34 -0700 | [diff] [blame] | 298 | for (i = 0; i < active_nr; i++) { |
Nguyễn Thái Ngọc Duy | 9c5e6c8 | 2013-07-09 22:29:00 +0700 | [diff] [blame] | 299 | const struct cache_entry *ce = active_cache[i]; |
Nguyễn Thái Ngọc Duy | 6d2df28 | 2018-08-13 18:14:22 +0200 | [diff] [blame] | 300 | if (!ce_path_match(&the_index, ce, &pathspec, seen)) |
Linus Torvalds | d9b814c | 2006-05-19 16:19:34 -0700 | [diff] [blame] | 301 | continue; |
Jens Lehmann | 293ab15 | 2012-09-26 20:21:13 +0200 | [diff] [blame] | 302 | ALLOC_GROW(list.entry, list.nr + 1, list.alloc); |
Karsten Blees | 5699d17 | 2013-11-14 20:24:37 +0100 | [diff] [blame] | 303 | list.entry[list.nr].name = xstrdup(ce->name); |
Jens Lehmann | 95c1641 | 2013-08-06 21:15:25 +0200 | [diff] [blame] | 304 | list.entry[list.nr].is_submodule = S_ISGITLINK(ce->ce_mode); |
| 305 | if (list.entry[list.nr++].is_submodule && |
Brandon Williams | 91b8348 | 2017-08-02 12:49:20 -0700 | [diff] [blame] | 306 | !is_staging_gitmodules_ok(&the_index)) |
Nguyễn Thái Ngọc Duy | 1a07e59 | 2018-07-21 09:49:19 +0200 | [diff] [blame] | 307 | die(_("please stage your changes to .gitmodules or stash them to proceed")); |
Linus Torvalds | d9b814c | 2006-05-19 16:19:34 -0700 | [diff] [blame] | 308 | } |
| 309 | |
Nguyễn Thái Ngọc Duy | 29211a9 | 2013-07-14 15:35:42 +0700 | [diff] [blame] | 310 | if (pathspec.nr) { |
| 311 | const char *original; |
Steven Grimm | bb1faf0 | 2007-04-16 00:53:24 -0700 | [diff] [blame] | 312 | int seen_any = 0; |
Nguyễn Thái Ngọc Duy | 29211a9 | 2013-07-14 15:35:42 +0700 | [diff] [blame] | 313 | for (i = 0; i < pathspec.nr; i++) { |
| 314 | original = pathspec.items[i].original; |
Steven Grimm | bb1faf0 | 2007-04-16 00:53:24 -0700 | [diff] [blame] | 315 | if (!seen[i]) { |
| 316 | if (!ignore_unmatch) { |
Ævar Arnfjörð Bjarmason | b9b537f | 2011-02-22 23:42:05 +0000 | [diff] [blame] | 317 | die(_("pathspec '%s' did not match any files"), |
Nguyễn Thái Ngọc Duy | 29211a9 | 2013-07-14 15:35:42 +0700 | [diff] [blame] | 318 | original); |
Steven Grimm | bb1faf0 | 2007-04-16 00:53:24 -0700 | [diff] [blame] | 319 | } |
Stefan Beller | f8aae0b | 2013-08-08 19:52:41 +0200 | [diff] [blame] | 320 | } |
Steven Grimm | bb1faf0 | 2007-04-16 00:53:24 -0700 | [diff] [blame] | 321 | else { |
| 322 | seen_any = 1; |
| 323 | } |
Junio C Hamano | 9f95069 | 2006-12-25 03:11:00 -0800 | [diff] [blame] | 324 | if (!recursive && seen[i] == MATCHED_RECURSIVELY) |
Ævar Arnfjörð Bjarmason | b9b537f | 2011-02-22 23:42:05 +0000 | [diff] [blame] | 325 | die(_("not removing '%s' recursively without -r"), |
Nguyễn Thái Ngọc Duy | 29211a9 | 2013-07-14 15:35:42 +0700 | [diff] [blame] | 326 | *original ? original : "."); |
Stefan Beller | f8aae0b | 2013-08-08 19:52:41 +0200 | [diff] [blame] | 327 | } |
Steven Grimm | bb1faf0 | 2007-04-16 00:53:24 -0700 | [diff] [blame] | 328 | |
Junio C Hamano | b02f5ae | 2013-09-09 14:36:15 -0700 | [diff] [blame] | 329 | if (!seen_any) |
Steven Grimm | bb1faf0 | 2007-04-16 00:53:24 -0700 | [diff] [blame] | 330 | exit(0); |
Linus Torvalds | d9b814c | 2006-05-19 16:19:34 -0700 | [diff] [blame] | 331 | } |
| 332 | |
Stefan Beller | 55856a3 | 2016-12-27 11:03:14 -0800 | [diff] [blame] | 333 | if (!index_only) |
Jeff King | cf7a901 | 2019-05-09 17:27:31 -0400 | [diff] [blame] | 334 | submodules_absorb_gitdir_if_needed(); |
Stefan Beller | 55856a3 | 2016-12-27 11:03:14 -0800 | [diff] [blame] | 335 | |
Linus Torvalds | d9b814c | 2006-05-19 16:19:34 -0700 | [diff] [blame] | 336 | /* |
Junio C Hamano | 9f95069 | 2006-12-25 03:11:00 -0800 | [diff] [blame] | 337 | * 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 Grimm | 9474eda | 2007-04-16 01:17:32 -0700 | [diff] [blame] | 341 | * rm F; git rm F |
Junio C Hamano | 9f95069 | 2006-12-25 03:11:00 -0800 | [diff] [blame] | 342 | * |
| 343 | * Further, if HEAD commit exists, "diff-index --cached" must |
| 344 | * report no changes unless forced. |
| 345 | */ |
| 346 | if (!force) { |
brian m. carlson | 8ec46d7 | 2016-09-05 20:08:04 +0000 | [diff] [blame] | 347 | struct object_id oid; |
| 348 | if (get_oid("HEAD", &oid)) |
| 349 | oidclr(&oid); |
| 350 | if (check_local_mod(&oid, index_only)) |
Junio C Hamano | 9f95069 | 2006-12-25 03:11:00 -0800 | [diff] [blame] | 351 | exit(1); |
| 352 | } |
| 353 | |
| 354 | /* |
Linus Torvalds | d9b814c | 2006-05-19 16:19:34 -0700 | [diff] [blame] | 355 | * First remove the names from the index: we won't commit |
Junio C Hamano | 9f95069 | 2006-12-25 03:11:00 -0800 | [diff] [blame] | 356 | * the index unless all of them succeed. |
Linus Torvalds | d9b814c | 2006-05-19 16:19:34 -0700 | [diff] [blame] | 357 | */ |
| 358 | for (i = 0; i < list.nr; i++) { |
Jens Lehmann | 293ab15 | 2012-09-26 20:21:13 +0200 | [diff] [blame] | 359 | const char *path = list.entry[i].name; |
Steven Grimm | b48caa2 | 2007-04-16 00:46:48 -0700 | [diff] [blame] | 360 | if (!quiet) |
| 361 | printf("rm '%s'\n", path); |
Linus Torvalds | d9b814c | 2006-05-19 16:19:34 -0700 | [diff] [blame] | 362 | |
| 363 | if (remove_file_from_cache(path)) |
Ævar Arnfjörð Bjarmason | b9b537f | 2011-02-22 23:42:05 +0000 | [diff] [blame] | 364 | die(_("git rm: unable to remove %s"), path); |
Linus Torvalds | d9b814c | 2006-05-19 16:19:34 -0700 | [diff] [blame] | 365 | } |
| 366 | |
Junio C Hamano | 7612a1e | 2006-06-08 21:11:25 -0700 | [diff] [blame] | 367 | if (show_only) |
| 368 | return 0; |
| 369 | |
Linus Torvalds | d9b814c | 2006-05-19 16:19:34 -0700 | [diff] [blame] | 370 | /* |
Junio C Hamano | 646ac22 | 2007-01-11 14:58:47 -0800 | [diff] [blame] | 371 | * Then, unless we used "--cached", remove the filenames from |
Junio C Hamano | 9f95069 | 2006-12-25 03:11:00 -0800 | [diff] [blame] | 372 | * the workspace. If we fail to remove the first one, we |
Linus Torvalds | d9b814c | 2006-05-19 16:19:34 -0700 | [diff] [blame] | 373 | * 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 Roskin | 82e5a82 | 2006-07-10 01:50:18 -0400 | [diff] [blame] | 375 | * by then we've already committed ourselves and can't fail |
Linus Torvalds | d9b814c | 2006-05-19 16:19:34 -0700 | [diff] [blame] | 376 | * in the middle) |
| 377 | */ |
Junio C Hamano | 9f95069 | 2006-12-25 03:11:00 -0800 | [diff] [blame] | 378 | if (!index_only) { |
Jens Lehmann | 95c1641 | 2013-08-06 21:15:25 +0200 | [diff] [blame] | 379 | int removed = 0, gitmodules_modified = 0; |
René Scharfe | 590fc05 | 2017-02-11 20:51:08 +0100 | [diff] [blame] | 380 | struct strbuf buf = STRBUF_INIT; |
Linus Torvalds | d9b814c | 2006-05-19 16:19:34 -0700 | [diff] [blame] | 381 | for (i = 0; i < list.nr; i++) { |
Jens Lehmann | 293ab15 | 2012-09-26 20:21:13 +0200 | [diff] [blame] | 382 | const char *path = list.entry[i].name; |
| 383 | if (list.entry[i].is_submodule) { |
René Scharfe | 590fc05 | 2017-02-11 20:51:08 +0100 | [diff] [blame] | 384 | strbuf_reset(&buf); |
Stefan Beller | 55856a3 | 2016-12-27 11:03:14 -0800 | [diff] [blame] | 385 | strbuf_addstr(&buf, path); |
| 386 | if (remove_dir_recursively(&buf, 0)) |
| 387 | die(_("could not remove '%s'"), path); |
Stefan Beller | 55856a3 | 2016-12-27 11:03:14 -0800 | [diff] [blame] | 388 | |
| 389 | removed = 1; |
| 390 | if (!remove_path_from_gitmodules(path)) |
| 391 | gitmodules_modified = 1; |
| 392 | continue; |
Jens Lehmann | 293ab15 | 2012-09-26 20:21:13 +0200 | [diff] [blame] | 393 | } |
Alex Riesen | 175a494 | 2008-09-27 00:59:14 +0200 | [diff] [blame] | 394 | if (!remove_path(path)) { |
Linus Torvalds | d9b814c | 2006-05-19 16:19:34 -0700 | [diff] [blame] | 395 | removed = 1; |
| 396 | continue; |
| 397 | } |
| 398 | if (!removed) |
Thomas Rast | d824cbb | 2009-06-27 17:58:46 +0200 | [diff] [blame] | 399 | die_errno("git rm: '%s'", path); |
Linus Torvalds | d9b814c | 2006-05-19 16:19:34 -0700 | [diff] [blame] | 400 | } |
René Scharfe | 590fc05 | 2017-02-11 20:51:08 +0100 | [diff] [blame] | 401 | strbuf_release(&buf); |
Jens Lehmann | 95c1641 | 2013-08-06 21:15:25 +0200 | [diff] [blame] | 402 | if (gitmodules_modified) |
Brandon Williams | 3b8317a | 2017-12-12 11:53:50 -0800 | [diff] [blame] | 403 | stage_updated_gitmodules(&the_index); |
Linus Torvalds | d9b814c | 2006-05-19 16:19:34 -0700 | [diff] [blame] | 404 | } |
| 405 | |
Martin Ågren | 6100081 | 2018-03-01 21:40:20 +0100 | [diff] [blame] | 406 | if (write_locked_index(&the_index, &lock_file, |
| 407 | COMMIT_LOCK | SKIP_IF_UNCHANGED)) |
| 408 | die(_("Unable to write new index file")); |
Linus Torvalds | d9b814c | 2006-05-19 16:19:34 -0700 | [diff] [blame] | 409 | |
| 410 | return 0; |
| 411 | } |