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