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