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