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