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 | */ |
Linus Torvalds | d9b814c | 2006-05-19 16:19:34 -0700 | [diff] [blame] | 6 | #include "builtin.h" |
Michael Haggerty | 697cc8e | 2014-10-01 12:28:42 +0200 | [diff] [blame] | 7 | #include "lockfile.h" |
Linus Torvalds | d9b814c | 2006-05-19 16:19:34 -0700 | [diff] [blame] | 8 | #include "dir.h" |
Junio C Hamano | 3fb3cc6 | 2006-05-23 01:31:38 -0700 | [diff] [blame] | 9 | #include "cache-tree.h" |
Junio C Hamano | 9f95069 | 2006-12-25 03:11:00 -0800 | [diff] [blame] | 10 | #include "tree-walk.h" |
Pierre Habouzit | f09985c | 2007-10-05 21:09:19 +0200 | [diff] [blame] | 11 | #include "parse-options.h" |
Mathieu Lienard--Mayor | 914dc02 | 2013-06-12 10:06:43 +0200 | [diff] [blame] | 12 | #include "string-list.h" |
Jens Lehmann | 293ab15 | 2012-09-26 20:21:13 +0200 | [diff] [blame] | 13 | #include "submodule.h" |
Nguyễn Thái Ngọc Duy | 29211a9 | 2013-07-14 15:35:42 +0700 | [diff] [blame] | 14 | #include "pathspec.h" |
Linus Torvalds | d9b814c | 2006-05-19 16:19:34 -0700 | [diff] [blame] | 15 | |
Pierre Habouzit | f09985c | 2007-10-05 21:09:19 +0200 | [diff] [blame] | 16 | static const char * const builtin_rm_usage[] = { |
Nguyễn Thái Ngọc Duy | 72bba2a | 2012-08-20 19:32:42 +0700 | [diff] [blame] | 17 | N_("git rm [options] [--] <file>..."), |
Pierre Habouzit | f09985c | 2007-10-05 21:09:19 +0200 | [diff] [blame] | 18 | NULL |
| 19 | }; |
Linus Torvalds | d9b814c | 2006-05-19 16:19:34 -0700 | [diff] [blame] | 20 | |
| 21 | static struct { |
| 22 | int nr, alloc; |
Jens Lehmann | 293ab15 | 2012-09-26 20:21:13 +0200 | [diff] [blame] | 23 | struct { |
| 24 | const char *name; |
| 25 | char is_submodule; |
| 26 | } *entry; |
Linus Torvalds | d9b814c | 2006-05-19 16:19:34 -0700 | [diff] [blame] | 27 | } list; |
| 28 | |
Jens Lehmann | 293ab15 | 2012-09-26 20:21:13 +0200 | [diff] [blame] | 29 | static int get_ours_cache_pos(const char *path, int pos) |
| 30 | { |
| 31 | int i = -pos - 1; |
| 32 | |
| 33 | while ((i < active_nr) && !strcmp(active_cache[i]->name, path)) { |
| 34 | if (ce_stage(active_cache[i]) == 2) |
| 35 | return i; |
| 36 | i++; |
| 37 | } |
| 38 | return -1; |
| 39 | } |
| 40 | |
Mathieu Lienard--Mayor | 914dc02 | 2013-06-12 10:06:43 +0200 | [diff] [blame] | 41 | static void print_error_files(struct string_list *files_list, |
| 42 | const char *main_msg, |
| 43 | const char *hints_msg, |
| 44 | int *errs) |
| 45 | { |
| 46 | if (files_list->nr) { |
| 47 | int i; |
| 48 | struct strbuf err_msg = STRBUF_INIT; |
| 49 | |
| 50 | strbuf_addstr(&err_msg, main_msg); |
| 51 | for (i = 0; i < files_list->nr; i++) |
| 52 | strbuf_addf(&err_msg, |
| 53 | "\n %s", |
| 54 | files_list->items[i].string); |
Mathieu Lienard--Mayor | 7e30944 | 2013-06-12 10:06:44 +0200 | [diff] [blame] | 55 | if (advice_rm_hints) |
| 56 | strbuf_addstr(&err_msg, hints_msg); |
Mathieu Lienard--Mayor | 914dc02 | 2013-06-12 10:06:43 +0200 | [diff] [blame] | 57 | *errs = error("%s", err_msg.buf); |
| 58 | strbuf_release(&err_msg); |
| 59 | } |
| 60 | } |
| 61 | |
Junio C Hamano | 658ff47 | 2013-07-25 23:05:17 -0700 | [diff] [blame] | 62 | static void error_removing_concrete_submodules(struct string_list *files, int *errs) |
| 63 | { |
| 64 | print_error_files(files, |
| 65 | Q_("the following submodule (or one of its nested " |
| 66 | "submodules)\n" |
| 67 | "uses a .git directory:", |
Alex Henrie | ad5fe37 | 2014-08-30 13:56:01 -0600 | [diff] [blame] | 68 | "the following submodules (or one of their nested " |
Junio C Hamano | 658ff47 | 2013-07-25 23:05:17 -0700 | [diff] [blame] | 69 | "submodules)\n" |
| 70 | "use a .git directory:", files->nr), |
| 71 | _("\n(use 'rm -rf' if you really want to remove " |
| 72 | "it including all of its history)"), |
| 73 | errs); |
| 74 | string_list_clear(files, 0); |
| 75 | } |
| 76 | |
Jens Lehmann | 293ab15 | 2012-09-26 20:21:13 +0200 | [diff] [blame] | 77 | static int check_submodules_use_gitfiles(void) |
| 78 | { |
| 79 | int i; |
| 80 | int errs = 0; |
Mathieu Lienard--Mayor | 914dc02 | 2013-06-12 10:06:43 +0200 | [diff] [blame] | 81 | struct string_list files = STRING_LIST_INIT_NODUP; |
Jens Lehmann | 293ab15 | 2012-09-26 20:21:13 +0200 | [diff] [blame] | 82 | |
| 83 | for (i = 0; i < list.nr; i++) { |
| 84 | const char *name = list.entry[i].name; |
| 85 | int pos; |
Nguyễn Thái Ngọc Duy | 9c5e6c8 | 2013-07-09 22:29:00 +0700 | [diff] [blame] | 86 | const struct cache_entry *ce; |
Jens Lehmann | 293ab15 | 2012-09-26 20:21:13 +0200 | [diff] [blame] | 87 | struct stat st; |
| 88 | |
| 89 | pos = cache_name_pos(name, strlen(name)); |
| 90 | if (pos < 0) { |
| 91 | pos = get_ours_cache_pos(name, pos); |
| 92 | if (pos < 0) |
| 93 | continue; |
| 94 | } |
| 95 | ce = active_cache[pos]; |
| 96 | |
| 97 | if (!S_ISGITLINK(ce->ce_mode) || |
| 98 | (lstat(ce->name, &st) < 0) || |
| 99 | is_empty_dir(name)) |
| 100 | continue; |
| 101 | |
| 102 | if (!submodule_uses_gitfile(name)) |
Mathieu Lienard--Mayor | 914dc02 | 2013-06-12 10:06:43 +0200 | [diff] [blame] | 103 | string_list_append(&files, name); |
Jens Lehmann | 293ab15 | 2012-09-26 20:21:13 +0200 | [diff] [blame] | 104 | } |
Junio C Hamano | 658ff47 | 2013-07-25 23:05:17 -0700 | [diff] [blame] | 105 | |
| 106 | error_removing_concrete_submodules(&files, &errs); |
Jens Lehmann | 293ab15 | 2012-09-26 20:21:13 +0200 | [diff] [blame] | 107 | |
| 108 | return errs; |
| 109 | } |
| 110 | |
Matthieu Moy | bdecd9d | 2007-07-13 19:41:38 +0200 | [diff] [blame] | 111 | static int check_local_mod(unsigned char *head, int index_only) |
Junio C Hamano | 9f95069 | 2006-12-25 03:11:00 -0800 | [diff] [blame] | 112 | { |
Junio C Hamano | 69530cb | 2008-11-28 18:41:21 -0800 | [diff] [blame] | 113 | /* |
| 114 | * Items in list are already sorted in the cache order, |
Junio C Hamano | 9f95069 | 2006-12-25 03:11:00 -0800 | [diff] [blame] | 115 | * so we could do this a lot more efficiently by using |
| 116 | * tree_desc based traversal if we wanted to, but I am |
| 117 | * lazy, and who cares if removal of files is a tad |
| 118 | * slower than the theoretical maximum speed? |
| 119 | */ |
| 120 | int i, no_head; |
| 121 | int errs = 0; |
Mathieu Lienard--Mayor | 914dc02 | 2013-06-12 10:06:43 +0200 | [diff] [blame] | 122 | struct string_list files_staged = STRING_LIST_INIT_NODUP; |
| 123 | struct string_list files_cached = STRING_LIST_INIT_NODUP; |
| 124 | struct string_list files_submodule = STRING_LIST_INIT_NODUP; |
| 125 | struct string_list files_local = STRING_LIST_INIT_NODUP; |
Junio C Hamano | 9f95069 | 2006-12-25 03:11:00 -0800 | [diff] [blame] | 126 | |
| 127 | no_head = is_null_sha1(head); |
| 128 | for (i = 0; i < list.nr; i++) { |
| 129 | struct stat st; |
| 130 | int pos; |
Nguyễn Thái Ngọc Duy | 9c5e6c8 | 2013-07-09 22:29:00 +0700 | [diff] [blame] | 131 | const struct cache_entry *ce; |
Jens Lehmann | 293ab15 | 2012-09-26 20:21:13 +0200 | [diff] [blame] | 132 | const char *name = list.entry[i].name; |
Junio C Hamano | 9f95069 | 2006-12-25 03:11:00 -0800 | [diff] [blame] | 133 | unsigned char sha1[20]; |
| 134 | unsigned mode; |
Matthieu Moy | bdecd9d | 2007-07-13 19:41:38 +0200 | [diff] [blame] | 135 | int local_changes = 0; |
| 136 | int staged_changes = 0; |
Junio C Hamano | 9f95069 | 2006-12-25 03:11:00 -0800 | [diff] [blame] | 137 | |
| 138 | pos = cache_name_pos(name, strlen(name)); |
Jens Lehmann | 293ab15 | 2012-09-26 20:21:13 +0200 | [diff] [blame] | 139 | if (pos < 0) { |
| 140 | /* |
| 141 | * Skip unmerged entries except for populated submodules |
| 142 | * that could lose history when removed. |
| 143 | */ |
| 144 | pos = get_ours_cache_pos(name, pos); |
| 145 | if (pos < 0) |
| 146 | continue; |
| 147 | |
| 148 | if (!S_ISGITLINK(active_cache[pos]->ce_mode) || |
| 149 | is_empty_dir(name)) |
| 150 | continue; |
| 151 | } |
Junio C Hamano | 9f95069 | 2006-12-25 03:11:00 -0800 | [diff] [blame] | 152 | ce = active_cache[pos]; |
| 153 | |
| 154 | if (lstat(ce->name, &st) < 0) { |
Jeff King | 9a6728d | 2013-04-04 15:03:35 -0400 | [diff] [blame] | 155 | if (errno != ENOENT && errno != ENOTDIR) |
Miklos Vajna | c36d785 | 2009-03-24 02:09:14 +0100 | [diff] [blame] | 156 | warning("'%s': %s", ce->name, strerror(errno)); |
Junio C Hamano | 9f95069 | 2006-12-25 03:11:00 -0800 | [diff] [blame] | 157 | /* It already vanished from the working tree */ |
| 158 | continue; |
| 159 | } |
| 160 | else if (S_ISDIR(st.st_mode)) { |
| 161 | /* if a file was removed and it is now a |
| 162 | * directory, that is the same as ENOENT as |
| 163 | * far as git is concerned; we do not track |
Jens Lehmann | 293ab15 | 2012-09-26 20:21:13 +0200 | [diff] [blame] | 164 | * directories unless they are submodules. |
Junio C Hamano | 9f95069 | 2006-12-25 03:11:00 -0800 | [diff] [blame] | 165 | */ |
Jens Lehmann | 293ab15 | 2012-09-26 20:21:13 +0200 | [diff] [blame] | 166 | if (!S_ISGITLINK(ce->ce_mode)) |
| 167 | continue; |
Junio C Hamano | 9f95069 | 2006-12-25 03:11:00 -0800 | [diff] [blame] | 168 | } |
Junio C Hamano | 69530cb | 2008-11-28 18:41:21 -0800 | [diff] [blame] | 169 | |
| 170 | /* |
| 171 | * "rm" of a path that has changes need to be treated |
| 172 | * carefully not to allow losing local changes |
| 173 | * accidentally. A local change could be (1) file in |
| 174 | * work tree is different since the index; and/or (2) |
| 175 | * the user staged a content that is different from |
| 176 | * the current commit in the index. |
| 177 | * |
| 178 | * In such a case, you would need to --force the |
| 179 | * removal. However, "rm --cached" (remove only from |
| 180 | * the index) is safe if the index matches the file in |
| 181 | * the work tree or the HEAD commit, as it means that |
| 182 | * the content being removed is available elsewhere. |
| 183 | */ |
| 184 | |
| 185 | /* |
| 186 | * Is the index different from the file in the work tree? |
Jens Lehmann | 293ab15 | 2012-09-26 20:21:13 +0200 | [diff] [blame] | 187 | * If it's a submodule, is its work tree modified? |
Junio C Hamano | 69530cb | 2008-11-28 18:41:21 -0800 | [diff] [blame] | 188 | */ |
Jens Lehmann | 293ab15 | 2012-09-26 20:21:13 +0200 | [diff] [blame] | 189 | if (ce_match_stat(ce, &st, 0) || |
| 190 | (S_ISGITLINK(ce->ce_mode) && |
| 191 | !ok_to_remove_submodule(ce->name))) |
Matthieu Moy | bdecd9d | 2007-07-13 19:41:38 +0200 | [diff] [blame] | 192 | local_changes = 1; |
Junio C Hamano | 69530cb | 2008-11-28 18:41:21 -0800 | [diff] [blame] | 193 | |
| 194 | /* |
| 195 | * Is the index different from the HEAD commit? By |
| 196 | * definition, before the very initial commit, |
| 197 | * anything staged in the index is treated by the same |
| 198 | * way as changed from the HEAD. |
| 199 | */ |
Jeff King | e4d9516 | 2007-03-26 14:55:39 -0400 | [diff] [blame] | 200 | if (no_head |
| 201 | || get_tree_entry(head, name, sha1, &mode) |
| 202 | || ce->ce_mode != create_ce_mode(mode) |
| 203 | || hashcmp(ce->sha1, sha1)) |
Matthieu Moy | bdecd9d | 2007-07-13 19:41:38 +0200 | [diff] [blame] | 204 | staged_changes = 1; |
| 205 | |
Junio C Hamano | 69530cb | 2008-11-28 18:41:21 -0800 | [diff] [blame] | 206 | /* |
| 207 | * If the index does not match the file in the work |
| 208 | * tree and if it does not match the HEAD commit |
| 209 | * either, (1) "git rm" without --cached definitely |
| 210 | * will lose information; (2) "git rm --cached" will |
| 211 | * lose information unless it is about removing an |
| 212 | * "intent to add" entry. |
| 213 | */ |
| 214 | if (local_changes && staged_changes) { |
Junio C Hamano | 388b2ac | 2008-11-28 19:55:25 -0800 | [diff] [blame] | 215 | if (!index_only || !(ce->ce_flags & CE_INTENT_TO_ADD)) |
Mathieu Lienard--Mayor | 914dc02 | 2013-06-12 10:06:43 +0200 | [diff] [blame] | 216 | string_list_append(&files_staged, name); |
Junio C Hamano | 69530cb | 2008-11-28 18:41:21 -0800 | [diff] [blame] | 217 | } |
Matthieu Moy | bdecd9d | 2007-07-13 19:41:38 +0200 | [diff] [blame] | 218 | else if (!index_only) { |
Matthieu Moy | bdecd9d | 2007-07-13 19:41:38 +0200 | [diff] [blame] | 219 | if (staged_changes) |
Mathieu Lienard--Mayor | 914dc02 | 2013-06-12 10:06:43 +0200 | [diff] [blame] | 220 | string_list_append(&files_cached, name); |
Jens Lehmann | 293ab15 | 2012-09-26 20:21:13 +0200 | [diff] [blame] | 221 | if (local_changes) { |
| 222 | if (S_ISGITLINK(ce->ce_mode) && |
Mathieu Lienard--Mayor | 914dc02 | 2013-06-12 10:06:43 +0200 | [diff] [blame] | 223 | !submodule_uses_gitfile(name)) |
| 224 | string_list_append(&files_submodule, name); |
| 225 | else |
| 226 | string_list_append(&files_local, name); |
Jens Lehmann | 293ab15 | 2012-09-26 20:21:13 +0200 | [diff] [blame] | 227 | } |
Matthieu Moy | bdecd9d | 2007-07-13 19:41:38 +0200 | [diff] [blame] | 228 | } |
Junio C Hamano | 9f95069 | 2006-12-25 03:11:00 -0800 | [diff] [blame] | 229 | } |
Mathieu Lienard--Mayor | 914dc02 | 2013-06-12 10:06:43 +0200 | [diff] [blame] | 230 | print_error_files(&files_staged, |
| 231 | Q_("the following file has staged content different " |
| 232 | "from both the\nfile and the HEAD:", |
| 233 | "the following files have staged content different" |
| 234 | " from both the\nfile and the HEAD:", |
| 235 | files_staged.nr), |
| 236 | _("\n(use -f to force removal)"), |
| 237 | &errs); |
| 238 | string_list_clear(&files_staged, 0); |
| 239 | print_error_files(&files_cached, |
| 240 | Q_("the following file has changes " |
| 241 | "staged in the index:", |
| 242 | "the following files have changes " |
| 243 | "staged in the index:", files_cached.nr), |
| 244 | _("\n(use --cached to keep the file," |
| 245 | " or -f to force removal)"), |
| 246 | &errs); |
| 247 | string_list_clear(&files_cached, 0); |
Junio C Hamano | 658ff47 | 2013-07-25 23:05:17 -0700 | [diff] [blame] | 248 | |
| 249 | error_removing_concrete_submodules(&files_submodule, &errs); |
| 250 | |
Mathieu Lienard--Mayor | 914dc02 | 2013-06-12 10:06:43 +0200 | [diff] [blame] | 251 | print_error_files(&files_local, |
| 252 | Q_("the following file has local modifications:", |
| 253 | "the following files have local modifications:", |
| 254 | files_local.nr), |
| 255 | _("\n(use --cached to keep the file," |
| 256 | " or -f to force removal)"), |
| 257 | &errs); |
| 258 | string_list_clear(&files_local, 0); |
| 259 | |
Junio C Hamano | 9f95069 | 2006-12-25 03:11:00 -0800 | [diff] [blame] | 260 | return errs; |
| 261 | } |
| 262 | |
Junio C Hamano | 021b6e4 | 2006-06-06 12:51:49 -0700 | [diff] [blame] | 263 | static struct lock_file lock_file; |
Linus Torvalds | d9b814c | 2006-05-19 16:19:34 -0700 | [diff] [blame] | 264 | |
Pierre Habouzit | f09985c | 2007-10-05 21:09:19 +0200 | [diff] [blame] | 265 | static int show_only = 0, force = 0, index_only = 0, recursive = 0, quiet = 0; |
| 266 | static int ignore_unmatch = 0; |
| 267 | |
| 268 | static struct option builtin_rm_options[] = { |
Nguyễn Thái Ngọc Duy | 72bba2a | 2012-08-20 19:32:42 +0700 | [diff] [blame] | 269 | OPT__DRY_RUN(&show_only, N_("dry run")), |
| 270 | OPT__QUIET(&quiet, N_("do not list removed files")), |
Stefan Beller | d5d09d4 | 2013-08-03 13:51:19 +0200 | [diff] [blame] | 271 | OPT_BOOL( 0 , "cached", &index_only, N_("only remove from the index")), |
Nguyễn Thái Ngọc Duy | 72bba2a | 2012-08-20 19:32:42 +0700 | [diff] [blame] | 272 | OPT__FORCE(&force, N_("override the up-to-date check")), |
Stefan Beller | d5d09d4 | 2013-08-03 13:51:19 +0200 | [diff] [blame] | 273 | OPT_BOOL('r', NULL, &recursive, N_("allow recursive removal")), |
| 274 | OPT_BOOL( 0 , "ignore-unmatch", &ignore_unmatch, |
Nguyễn Thái Ngọc Duy | 72bba2a | 2012-08-20 19:32:42 +0700 | [diff] [blame] | 275 | N_("exit with a zero status even if nothing matched")), |
Pierre Habouzit | f09985c | 2007-10-05 21:09:19 +0200 | [diff] [blame] | 276 | OPT_END(), |
| 277 | }; |
| 278 | |
Linus Torvalds | a633fca | 2006-07-28 22:44:25 -0700 | [diff] [blame] | 279 | int cmd_rm(int argc, const char **argv, const char *prefix) |
Linus Torvalds | d9b814c | 2006-05-19 16:19:34 -0700 | [diff] [blame] | 280 | { |
Nguyễn Thái Ngọc Duy | 03b8664 | 2014-06-13 19:19:23 +0700 | [diff] [blame] | 281 | int i; |
Nguyễn Thái Ngọc Duy | 29211a9 | 2013-07-14 15:35:42 +0700 | [diff] [blame] | 282 | struct pathspec pathspec; |
Linus Torvalds | d9b814c | 2006-05-19 16:19:34 -0700 | [diff] [blame] | 283 | char *seen; |
| 284 | |
Jens Lehmann | 95c1641 | 2013-08-06 21:15:25 +0200 | [diff] [blame] | 285 | gitmodules_config(); |
Johannes Schindelin | ef90d6d | 2008-05-14 18:46:53 +0100 | [diff] [blame] | 286 | git_config(git_default_config, NULL); |
Linus Torvalds | d9b814c | 2006-05-19 16:19:34 -0700 | [diff] [blame] | 287 | |
Stephen Boyd | 3778292 | 2009-05-23 11:53:12 -0700 | [diff] [blame] | 288 | argc = parse_options(argc, argv, prefix, builtin_rm_options, |
| 289 | builtin_rm_usage, 0); |
Pierre Habouzit | f09985c | 2007-10-05 21:09:19 +0200 | [diff] [blame] | 290 | if (!argc) |
| 291 | usage_with_options(builtin_rm_usage, builtin_rm_options); |
Linus Torvalds | d9b814c | 2006-05-19 16:19:34 -0700 | [diff] [blame] | 292 | |
Mike Hommey | 271bb08 | 2007-11-03 12:23:13 +0100 | [diff] [blame] | 293 | if (!index_only) |
| 294 | setup_work_tree(); |
| 295 | |
Nguyễn Thái Ngọc Duy | 03b8664 | 2014-06-13 19:19:23 +0700 | [diff] [blame] | 296 | hold_locked_index(&lock_file, 1); |
Olivier Marin | 4d26467 | 2008-07-19 18:24:46 +0200 | [diff] [blame] | 297 | |
| 298 | if (read_cache() < 0) |
Ævar Arnfjörð Bjarmason | b9b537f | 2011-02-22 23:42:05 +0000 | [diff] [blame] | 299 | die(_("index file corrupt")); |
Olivier Marin | 4d26467 | 2008-07-19 18:24:46 +0200 | [diff] [blame] | 300 | |
John Keeping | f8bc2ac | 2013-09-12 20:25:00 +0100 | [diff] [blame] | 301 | parse_pathspec(&pathspec, 0, |
| 302 | PATHSPEC_PREFER_CWD | |
| 303 | PATHSPEC_STRIP_SUBMODULE_SLASH_CHEAP, |
| 304 | prefix, argv); |
Nguyễn Thái Ngọc Duy | 9b2d614 | 2013-07-14 15:35:54 +0700 | [diff] [blame] | 305 | refresh_index(&the_index, REFRESH_QUIET, &pathspec, NULL, NULL); |
Nguyễn Thái Ngọc Duy | 4e1a7ba | 2010-01-17 15:43:13 +0700 | [diff] [blame] | 306 | |
Nguyễn Thái Ngọc Duy | 29211a9 | 2013-07-14 15:35:42 +0700 | [diff] [blame] | 307 | seen = xcalloc(pathspec.nr, 1); |
Linus Torvalds | d9b814c | 2006-05-19 16:19:34 -0700 | [diff] [blame] | 308 | |
| 309 | for (i = 0; i < active_nr; i++) { |
Nguyễn Thái Ngọc Duy | 9c5e6c8 | 2013-07-09 22:29:00 +0700 | [diff] [blame] | 310 | const struct cache_entry *ce = active_cache[i]; |
Nguyễn Thái Ngọc Duy | 429bb40 | 2014-01-24 20:40:28 +0700 | [diff] [blame] | 311 | if (!ce_path_match(ce, &pathspec, seen)) |
Linus Torvalds | d9b814c | 2006-05-19 16:19:34 -0700 | [diff] [blame] | 312 | continue; |
Jens Lehmann | 293ab15 | 2012-09-26 20:21:13 +0200 | [diff] [blame] | 313 | ALLOC_GROW(list.entry, list.nr + 1, list.alloc); |
Karsten Blees | 5699d17 | 2013-11-14 20:24:37 +0100 | [diff] [blame] | 314 | list.entry[list.nr].name = xstrdup(ce->name); |
Jens Lehmann | 95c1641 | 2013-08-06 21:15:25 +0200 | [diff] [blame] | 315 | list.entry[list.nr].is_submodule = S_ISGITLINK(ce->ce_mode); |
| 316 | if (list.entry[list.nr++].is_submodule && |
| 317 | !is_staging_gitmodules_ok()) |
| 318 | die (_("Please, stage your changes to .gitmodules or stash them to proceed")); |
Linus Torvalds | d9b814c | 2006-05-19 16:19:34 -0700 | [diff] [blame] | 319 | } |
| 320 | |
Nguyễn Thái Ngọc Duy | 29211a9 | 2013-07-14 15:35:42 +0700 | [diff] [blame] | 321 | if (pathspec.nr) { |
| 322 | const char *original; |
Steven Grimm | bb1faf0 | 2007-04-16 00:53:24 -0700 | [diff] [blame] | 323 | int seen_any = 0; |
Nguyễn Thái Ngọc Duy | 29211a9 | 2013-07-14 15:35:42 +0700 | [diff] [blame] | 324 | for (i = 0; i < pathspec.nr; i++) { |
| 325 | original = pathspec.items[i].original; |
Steven Grimm | bb1faf0 | 2007-04-16 00:53:24 -0700 | [diff] [blame] | 326 | if (!seen[i]) { |
| 327 | if (!ignore_unmatch) { |
Ævar Arnfjörð Bjarmason | b9b537f | 2011-02-22 23:42:05 +0000 | [diff] [blame] | 328 | 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] | 329 | original); |
Steven Grimm | bb1faf0 | 2007-04-16 00:53:24 -0700 | [diff] [blame] | 330 | } |
Stefan Beller | f8aae0b | 2013-08-08 19:52:41 +0200 | [diff] [blame] | 331 | } |
Steven Grimm | bb1faf0 | 2007-04-16 00:53:24 -0700 | [diff] [blame] | 332 | else { |
| 333 | seen_any = 1; |
| 334 | } |
Junio C Hamano | 9f95069 | 2006-12-25 03:11:00 -0800 | [diff] [blame] | 335 | if (!recursive && seen[i] == MATCHED_RECURSIVELY) |
Ævar Arnfjörð Bjarmason | b9b537f | 2011-02-22 23:42:05 +0000 | [diff] [blame] | 336 | die(_("not removing '%s' recursively without -r"), |
Nguyễn Thái Ngọc Duy | 29211a9 | 2013-07-14 15:35:42 +0700 | [diff] [blame] | 337 | *original ? original : "."); |
Stefan Beller | f8aae0b | 2013-08-08 19:52:41 +0200 | [diff] [blame] | 338 | } |
Steven Grimm | bb1faf0 | 2007-04-16 00:53:24 -0700 | [diff] [blame] | 339 | |
Junio C Hamano | b02f5ae | 2013-09-09 14:36:15 -0700 | [diff] [blame] | 340 | if (!seen_any) |
Steven Grimm | bb1faf0 | 2007-04-16 00:53:24 -0700 | [diff] [blame] | 341 | exit(0); |
Linus Torvalds | d9b814c | 2006-05-19 16:19:34 -0700 | [diff] [blame] | 342 | } |
Linus Torvalds | d9b814c | 2006-05-19 16:19:34 -0700 | [diff] [blame] | 343 | |
| 344 | /* |
Junio C Hamano | 9f95069 | 2006-12-25 03:11:00 -0800 | [diff] [blame] | 345 | * If not forced, the file, the index and the HEAD (if exists) |
| 346 | * must match; but the file can already been removed, since |
| 347 | * this sequence is a natural "novice" way: |
| 348 | * |
Steven Grimm | 9474eda | 2007-04-16 01:17:32 -0700 | [diff] [blame] | 349 | * rm F; git rm F |
Junio C Hamano | 9f95069 | 2006-12-25 03:11:00 -0800 | [diff] [blame] | 350 | * |
| 351 | * Further, if HEAD commit exists, "diff-index --cached" must |
| 352 | * report no changes unless forced. |
| 353 | */ |
| 354 | if (!force) { |
| 355 | unsigned char sha1[20]; |
| 356 | if (get_sha1("HEAD", sha1)) |
| 357 | hashclr(sha1); |
Matthieu Moy | bdecd9d | 2007-07-13 19:41:38 +0200 | [diff] [blame] | 358 | if (check_local_mod(sha1, index_only)) |
Junio C Hamano | 9f95069 | 2006-12-25 03:11:00 -0800 | [diff] [blame] | 359 | exit(1); |
Jens Lehmann | 293ab15 | 2012-09-26 20:21:13 +0200 | [diff] [blame] | 360 | } else if (!index_only) { |
| 361 | if (check_submodules_use_gitfiles()) |
| 362 | exit(1); |
Junio C Hamano | 9f95069 | 2006-12-25 03:11:00 -0800 | [diff] [blame] | 363 | } |
| 364 | |
| 365 | /* |
Linus Torvalds | d9b814c | 2006-05-19 16:19:34 -0700 | [diff] [blame] | 366 | * First remove the names from the index: we won't commit |
Junio C Hamano | 9f95069 | 2006-12-25 03:11:00 -0800 | [diff] [blame] | 367 | * the index unless all of them succeed. |
Linus Torvalds | d9b814c | 2006-05-19 16:19:34 -0700 | [diff] [blame] | 368 | */ |
| 369 | for (i = 0; i < list.nr; i++) { |
Jens Lehmann | 293ab15 | 2012-09-26 20:21:13 +0200 | [diff] [blame] | 370 | const char *path = list.entry[i].name; |
Steven Grimm | b48caa2 | 2007-04-16 00:46:48 -0700 | [diff] [blame] | 371 | if (!quiet) |
| 372 | printf("rm '%s'\n", path); |
Linus Torvalds | d9b814c | 2006-05-19 16:19:34 -0700 | [diff] [blame] | 373 | |
| 374 | if (remove_file_from_cache(path)) |
Ævar Arnfjörð Bjarmason | b9b537f | 2011-02-22 23:42:05 +0000 | [diff] [blame] | 375 | die(_("git rm: unable to remove %s"), path); |
Linus Torvalds | d9b814c | 2006-05-19 16:19:34 -0700 | [diff] [blame] | 376 | } |
| 377 | |
Junio C Hamano | 7612a1e | 2006-06-08 21:11:25 -0700 | [diff] [blame] | 378 | if (show_only) |
| 379 | return 0; |
| 380 | |
Linus Torvalds | d9b814c | 2006-05-19 16:19:34 -0700 | [diff] [blame] | 381 | /* |
Junio C Hamano | 646ac22 | 2007-01-11 14:58:47 -0800 | [diff] [blame] | 382 | * Then, unless we used "--cached", remove the filenames from |
Junio C Hamano | 9f95069 | 2006-12-25 03:11:00 -0800 | [diff] [blame] | 383 | * the workspace. If we fail to remove the first one, we |
Linus Torvalds | d9b814c | 2006-05-19 16:19:34 -0700 | [diff] [blame] | 384 | * abort the "git rm" (but once we've successfully removed |
| 385 | * 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] | 386 | * by then we've already committed ourselves and can't fail |
Linus Torvalds | d9b814c | 2006-05-19 16:19:34 -0700 | [diff] [blame] | 387 | * in the middle) |
| 388 | */ |
Junio C Hamano | 9f95069 | 2006-12-25 03:11:00 -0800 | [diff] [blame] | 389 | if (!index_only) { |
Jens Lehmann | 95c1641 | 2013-08-06 21:15:25 +0200 | [diff] [blame] | 390 | int removed = 0, gitmodules_modified = 0; |
Linus Torvalds | d9b814c | 2006-05-19 16:19:34 -0700 | [diff] [blame] | 391 | for (i = 0; i < list.nr; i++) { |
Jens Lehmann | 293ab15 | 2012-09-26 20:21:13 +0200 | [diff] [blame] | 392 | const char *path = list.entry[i].name; |
| 393 | if (list.entry[i].is_submodule) { |
| 394 | if (is_empty_dir(path)) { |
| 395 | if (!rmdir(path)) { |
| 396 | removed = 1; |
Jens Lehmann | 95c1641 | 2013-08-06 21:15:25 +0200 | [diff] [blame] | 397 | if (!remove_path_from_gitmodules(path)) |
| 398 | gitmodules_modified = 1; |
Jens Lehmann | 293ab15 | 2012-09-26 20:21:13 +0200 | [diff] [blame] | 399 | continue; |
| 400 | } |
| 401 | } else { |
| 402 | struct strbuf buf = STRBUF_INIT; |
| 403 | strbuf_addstr(&buf, path); |
| 404 | if (!remove_dir_recursively(&buf, 0)) { |
| 405 | removed = 1; |
Jens Lehmann | 95c1641 | 2013-08-06 21:15:25 +0200 | [diff] [blame] | 406 | if (!remove_path_from_gitmodules(path)) |
| 407 | gitmodules_modified = 1; |
Jens Lehmann | 293ab15 | 2012-09-26 20:21:13 +0200 | [diff] [blame] | 408 | strbuf_release(&buf); |
| 409 | continue; |
Jens Lehmann | 95c1641 | 2013-08-06 21:15:25 +0200 | [diff] [blame] | 410 | } else if (!file_exists(path)) |
| 411 | /* Submodule was removed by user */ |
| 412 | if (!remove_path_from_gitmodules(path)) |
| 413 | gitmodules_modified = 1; |
Jens Lehmann | 293ab15 | 2012-09-26 20:21:13 +0200 | [diff] [blame] | 414 | strbuf_release(&buf); |
| 415 | /* Fallthrough and let remove_path() fail. */ |
| 416 | } |
| 417 | } |
Alex Riesen | 175a494 | 2008-09-27 00:59:14 +0200 | [diff] [blame] | 418 | if (!remove_path(path)) { |
Linus Torvalds | d9b814c | 2006-05-19 16:19:34 -0700 | [diff] [blame] | 419 | removed = 1; |
| 420 | continue; |
| 421 | } |
| 422 | if (!removed) |
Thomas Rast | d824cbb | 2009-06-27 17:58:46 +0200 | [diff] [blame] | 423 | die_errno("git rm: '%s'", path); |
Linus Torvalds | d9b814c | 2006-05-19 16:19:34 -0700 | [diff] [blame] | 424 | } |
Jens Lehmann | 95c1641 | 2013-08-06 21:15:25 +0200 | [diff] [blame] | 425 | if (gitmodules_modified) |
| 426 | stage_updated_gitmodules(); |
Linus Torvalds | d9b814c | 2006-05-19 16:19:34 -0700 | [diff] [blame] | 427 | } |
| 428 | |
| 429 | if (active_cache_changed) { |
Nguyễn Thái Ngọc Duy | 03b8664 | 2014-06-13 19:19:23 +0700 | [diff] [blame] | 430 | if (write_locked_index(&the_index, &lock_file, COMMIT_LOCK)) |
Ævar Arnfjörð Bjarmason | b9b537f | 2011-02-22 23:42:05 +0000 | [diff] [blame] | 431 | die(_("Unable to write new index file")); |
Linus Torvalds | d9b814c | 2006-05-19 16:19:34 -0700 | [diff] [blame] | 432 | } |
| 433 | |
| 434 | return 0; |
| 435 | } |