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#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 intcheck_submodules_use_gitfiles(void) 63{ 64int i; 65int errs =0; 66struct string_list files = STRING_LIST_INIT_NODUP; 67 68for(i =0; i < list.nr; i++) { 69const char*name = list.entry[i].name; 70int pos; 71struct cache_entry *ce; 72struct stat st; 73 74 pos =cache_name_pos(name,strlen(name)); 75if(pos <0) { 76 pos =get_ours_cache_pos(name, pos); 77if(pos <0) 78continue; 79} 80 ce = active_cache[pos]; 81 82if(!S_ISGITLINK(ce->ce_mode) || 83(lstat(ce->name, &st) <0) || 84is_empty_dir(name)) 85continue; 86 87if(!submodule_uses_gitfile(name)) 88string_list_append(&files, name); 89} 90print_error_files(&files, 91Q_("the following submodule (or one of its nested " 92"submodules)\nuses a .git directory:", 93"the following submodules (or one of its nested " 94"submodules)\nuse a .git directory:", 95 files.nr), 96_("\n(use 'rm -rf' if you really want to remove " 97"it including all of its history)"), 98&errs); 99string_list_clear(&files,0); 100 101return errs; 102} 103 104static intcheck_local_mod(unsigned char*head,int index_only) 105{ 106/* 107 * Items in list are already sorted in the cache order, 108 * so we could do this a lot more efficiently by using 109 * tree_desc based traversal if we wanted to, but I am 110 * lazy, and who cares if removal of files is a tad 111 * slower than the theoretical maximum speed? 112 */ 113int i, no_head; 114int errs =0; 115struct string_list files_staged = STRING_LIST_INIT_NODUP; 116struct string_list files_cached = STRING_LIST_INIT_NODUP; 117struct string_list files_submodule = STRING_LIST_INIT_NODUP; 118struct string_list files_local = STRING_LIST_INIT_NODUP; 119 120 no_head =is_null_sha1(head); 121for(i =0; i < list.nr; i++) { 122struct stat st; 123int pos; 124struct cache_entry *ce; 125const char*name = list.entry[i].name; 126unsigned char sha1[20]; 127unsigned mode; 128int local_changes =0; 129int staged_changes =0; 130 131 pos =cache_name_pos(name,strlen(name)); 132if(pos <0) { 133/* 134 * Skip unmerged entries except for populated submodules 135 * that could lose history when removed. 136 */ 137 pos =get_ours_cache_pos(name, pos); 138if(pos <0) 139continue; 140 141if(!S_ISGITLINK(active_cache[pos]->ce_mode) || 142is_empty_dir(name)) 143continue; 144} 145 ce = active_cache[pos]; 146 147if(lstat(ce->name, &st) <0) { 148if(errno != ENOENT && errno != ENOTDIR) 149warning("'%s':%s", ce->name,strerror(errno)); 150/* It already vanished from the working tree */ 151continue; 152} 153else if(S_ISDIR(st.st_mode)) { 154/* if a file was removed and it is now a 155 * directory, that is the same as ENOENT as 156 * far as git is concerned; we do not track 157 * directories unless they are submodules. 158 */ 159if(!S_ISGITLINK(ce->ce_mode)) 160continue; 161} 162 163/* 164 * "rm" of a path that has changes need to be treated 165 * carefully not to allow losing local changes 166 * accidentally. A local change could be (1) file in 167 * work tree is different since the index; and/or (2) 168 * the user staged a content that is different from 169 * the current commit in the index. 170 * 171 * In such a case, you would need to --force the 172 * removal. However, "rm --cached" (remove only from 173 * the index) is safe if the index matches the file in 174 * the work tree or the HEAD commit, as it means that 175 * the content being removed is available elsewhere. 176 */ 177 178/* 179 * Is the index different from the file in the work tree? 180 * If it's a submodule, is its work tree modified? 181 */ 182if(ce_match_stat(ce, &st,0) || 183(S_ISGITLINK(ce->ce_mode) && 184!ok_to_remove_submodule(ce->name))) 185 local_changes =1; 186 187/* 188 * Is the index different from the HEAD commit? By 189 * definition, before the very initial commit, 190 * anything staged in the index is treated by the same 191 * way as changed from the HEAD. 192 */ 193if(no_head 194||get_tree_entry(head, name, sha1, &mode) 195|| ce->ce_mode !=create_ce_mode(mode) 196||hashcmp(ce->sha1, sha1)) 197 staged_changes =1; 198 199/* 200 * If the index does not match the file in the work 201 * tree and if it does not match the HEAD commit 202 * either, (1) "git rm" without --cached definitely 203 * will lose information; (2) "git rm --cached" will 204 * lose information unless it is about removing an 205 * "intent to add" entry. 206 */ 207if(local_changes && staged_changes) { 208if(!index_only || !(ce->ce_flags & CE_INTENT_TO_ADD)) 209string_list_append(&files_staged, name); 210} 211else if(!index_only) { 212if(staged_changes) 213string_list_append(&files_cached, name); 214if(local_changes) { 215if(S_ISGITLINK(ce->ce_mode) && 216!submodule_uses_gitfile(name)) 217string_list_append(&files_submodule, name); 218else 219string_list_append(&files_local, name); 220} 221} 222} 223print_error_files(&files_staged, 224Q_("the following file has staged content different " 225"from both the\nfile and the HEAD:", 226"the following files have staged content different" 227" from both the\nfile and the HEAD:", 228 files_staged.nr), 229_("\n(use -f to force removal)"), 230&errs); 231string_list_clear(&files_staged,0); 232print_error_files(&files_cached, 233Q_("the following file has changes " 234"staged in the index:", 235"the following files have changes " 236"staged in the index:", files_cached.nr), 237_("\n(use --cached to keep the file," 238" or -f to force removal)"), 239&errs); 240string_list_clear(&files_cached,0); 241print_error_files(&files_submodule, 242Q_("the following submodule (or one of its nested " 243"submodule)\nuses a .git directory:", 244"the following submodules (or one of its nested " 245"submodule)\nuse a .git directory:", 246 files_submodule.nr), 247_("\n(use 'rm -rf' if you really " 248"want to remove it including all " 249"of its history)"), 250&errs); 251string_list_clear(&files_submodule,0); 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_BOOLEAN(0,"cached", &index_only,N_("only remove from the index")), 273OPT__FORCE(&force,N_("override the up-to-date check")), 274OPT_BOOLEAN('r', NULL, &recursive,N_("allow recursive removal")), 275OPT_BOOLEAN(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, newfd; 283struct pathspec pathspec; 284char*seen; 285 286git_config(git_default_config, NULL); 287 288 argc =parse_options(argc, argv, prefix, builtin_rm_options, 289 builtin_rm_usage,0); 290if(!argc) 291usage_with_options(builtin_rm_usage, builtin_rm_options); 292 293if(!index_only) 294setup_work_tree(); 295 296 newfd =hold_locked_index(&lock_file,1); 297 298if(read_cache() <0) 299die(_("index file corrupt")); 300 301/* 302 * Drop trailing directory separators from directories so we'll find 303 * submodules in the index. 304 */ 305for(i =0; i < argc; i++) { 306size_t pathlen =strlen(argv[i]); 307if(pathlen &&is_dir_sep(argv[i][pathlen -1]) && 308is_directory(argv[i])) { 309do{ 310 pathlen--; 311}while(pathlen &&is_dir_sep(argv[i][pathlen -1])); 312 argv[i] =xmemdupz(argv[i], pathlen); 313} 314} 315 316parse_pathspec(&pathspec,0, PATHSPEC_PREFER_CWD, prefix, argv); 317refresh_index(&the_index, REFRESH_QUIET, &pathspec, NULL, NULL); 318 319 seen =xcalloc(pathspec.nr,1); 320 321for(i =0; i < active_nr; i++) { 322struct cache_entry *ce = active_cache[i]; 323if(!match_pathspec_depth(&pathspec, ce->name,ce_namelen(ce),0, seen)) 324continue; 325ALLOC_GROW(list.entry, list.nr +1, list.alloc); 326 list.entry[list.nr].name = ce->name; 327 list.entry[list.nr++].is_submodule =S_ISGITLINK(ce->ce_mode); 328} 329 330if(pathspec.nr) { 331const char*original; 332int seen_any =0; 333for(i =0; i < pathspec.nr; i++) { 334 original = pathspec.items[i].original; 335if(!seen[i]) { 336if(!ignore_unmatch) { 337die(_("pathspec '%s' did not match any files"), 338 original); 339} 340} 341else{ 342 seen_any =1; 343} 344if(!recursive && seen[i] == MATCHED_RECURSIVELY) 345die(_("not removing '%s' recursively without -r"), 346*original ? original :"."); 347} 348 349if(! seen_any) 350exit(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 */ 363if(!force) { 364unsigned char sha1[20]; 365if(get_sha1("HEAD", sha1)) 366hashclr(sha1); 367if(check_local_mod(sha1, index_only)) 368exit(1); 369}else if(!index_only) { 370if(check_submodules_use_gitfiles()) 371exit(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 */ 378for(i =0; i < list.nr; i++) { 379const char*path = list.entry[i].name; 380if(!quiet) 381printf("rm '%s'\n", path); 382 383if(remove_file_from_cache(path)) 384die(_("git rm: unable to remove%s"), path); 385} 386 387if(show_only) 388return0; 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 */ 398if(!index_only) { 399int removed =0; 400for(i =0; i < list.nr; i++) { 401const char*path = list.entry[i].name; 402if(list.entry[i].is_submodule) { 403if(is_empty_dir(path)) { 404if(!rmdir(path)) { 405 removed =1; 406continue; 407} 408}else{ 409struct strbuf buf = STRBUF_INIT; 410strbuf_addstr(&buf, path); 411if(!remove_dir_recursively(&buf,0)) { 412 removed =1; 413strbuf_release(&buf); 414continue; 415} 416strbuf_release(&buf); 417/* Fallthrough and let remove_path() fail. */ 418} 419} 420if(!remove_path(path)) { 421 removed =1; 422continue; 423} 424if(!removed) 425die_errno("git rm: '%s'", path); 426} 427} 428 429if(active_cache_changed) { 430if(write_cache(newfd, active_cache, active_nr) || 431commit_locked_index(&lock_file)) 432die(_("Unable to write new index file")); 433} 434 435return0; 436}