1/* 2 * "git rm" builtin command 3 * 4 * Copyright (C) Linus Torvalds 2006 5 */ 6#include"builtin.h" 7#include"config.h" 8#include"lockfile.h" 9#include"dir.h" 10#include"cache-tree.h" 11#include"tree-walk.h" 12#include"parse-options.h" 13#include"string-list.h" 14#include"submodule.h" 15#include"pathspec.h" 16 17static const char*const builtin_rm_usage[] = { 18N_("git rm [<options>] [--] <file>..."), 19 NULL 20}; 21 22static struct{ 23int nr, alloc; 24struct{ 25const char*name; 26char is_submodule; 27} *entry; 28} list; 29 30static intget_ours_cache_pos(const char*path,int pos) 31{ 32int i = -pos -1; 33 34while((i < active_nr) && !strcmp(active_cache[i]->name, path)) { 35if(ce_stage(active_cache[i]) ==2) 36return i; 37 i++; 38} 39return-1; 40} 41 42static voidprint_error_files(struct string_list *files_list, 43const char*main_msg, 44const char*hints_msg, 45int*errs) 46{ 47if(files_list->nr) { 48int i; 49struct strbuf err_msg = STRBUF_INIT; 50 51strbuf_addstr(&err_msg, main_msg); 52for(i =0; i < files_list->nr; i++) 53strbuf_addf(&err_msg, 54"\n%s", 55 files_list->items[i].string); 56if(advice_rm_hints) 57strbuf_addstr(&err_msg, hints_msg); 58*errs =error("%s", err_msg.buf); 59strbuf_release(&err_msg); 60} 61} 62 63static voidsubmodules_absorb_gitdir_if_needed(const char*prefix) 64{ 65int i; 66for(i =0; i < list.nr; i++) { 67const char*name = list.entry[i].name; 68int pos; 69const struct cache_entry *ce; 70 71 pos =cache_name_pos(name,strlen(name)); 72if(pos <0) { 73 pos =get_ours_cache_pos(name, pos); 74if(pos <0) 75continue; 76} 77 ce = active_cache[pos]; 78 79if(!S_ISGITLINK(ce->ce_mode) || 80!file_exists(ce->name) || 81is_empty_dir(name)) 82continue; 83 84if(!submodule_uses_gitfile(name)) 85absorb_git_dir_into_superproject(prefix, name, 86 ABSORB_GITDIR_RECURSE_SUBMODULES); 87} 88} 89 90static intcheck_local_mod(struct object_id *head,int index_only) 91{ 92/* 93 * Items in list are already sorted in the cache order, 94 * so we could do this a lot more efficiently by using 95 * tree_desc based traversal if we wanted to, but I am 96 * lazy, and who cares if removal of files is a tad 97 * slower than the theoretical maximum speed? 98 */ 99int i, no_head; 100int errs =0; 101struct string_list files_staged = STRING_LIST_INIT_NODUP; 102struct string_list files_cached = STRING_LIST_INIT_NODUP; 103struct string_list files_local = STRING_LIST_INIT_NODUP; 104 105 no_head =is_null_oid(head); 106for(i =0; i < list.nr; i++) { 107struct stat st; 108int pos; 109const struct cache_entry *ce; 110const char*name = list.entry[i].name; 111struct object_id oid; 112unsigned mode; 113int local_changes =0; 114int staged_changes =0; 115 116 pos =cache_name_pos(name,strlen(name)); 117if(pos <0) { 118/* 119 * Skip unmerged entries except for populated submodules 120 * that could lose history when removed. 121 */ 122 pos =get_ours_cache_pos(name, pos); 123if(pos <0) 124continue; 125 126if(!S_ISGITLINK(active_cache[pos]->ce_mode) || 127is_empty_dir(name)) 128continue; 129} 130 ce = active_cache[pos]; 131 132if(lstat(ce->name, &st) <0) { 133if(!is_missing_file_error(errno)) 134warning_errno(_("failed to stat '%s'"), ce->name); 135/* It already vanished from the working tree */ 136continue; 137} 138else if(S_ISDIR(st.st_mode)) { 139/* if a file was removed and it is now a 140 * directory, that is the same as ENOENT as 141 * far as git is concerned; we do not track 142 * directories unless they are submodules. 143 */ 144if(!S_ISGITLINK(ce->ce_mode)) 145continue; 146} 147 148/* 149 * "rm" of a path that has changes need to be treated 150 * carefully not to allow losing local changes 151 * accidentally. A local change could be (1) file in 152 * work tree is different since the index; and/or (2) 153 * the user staged a content that is different from 154 * the current commit in the index. 155 * 156 * In such a case, you would need to --force the 157 * removal. However, "rm --cached" (remove only from 158 * the index) is safe if the index matches the file in 159 * the work tree or the HEAD commit, as it means that 160 * the content being removed is available elsewhere. 161 */ 162 163/* 164 * Is the index different from the file in the work tree? 165 * If it's a submodule, is its work tree modified? 166 */ 167if(ce_match_stat(ce, &st,0) || 168(S_ISGITLINK(ce->ce_mode) && 169bad_to_remove_submodule(ce->name, 170 SUBMODULE_REMOVAL_DIE_ON_ERROR | 171 SUBMODULE_REMOVAL_IGNORE_IGNORED_UNTRACKED))) 172 local_changes =1; 173 174/* 175 * Is the index different from the HEAD commit? By 176 * definition, before the very initial commit, 177 * anything staged in the index is treated by the same 178 * way as changed from the HEAD. 179 */ 180if(no_head 181||get_tree_entry(head->hash, name, oid.hash, &mode) 182|| ce->ce_mode !=create_ce_mode(mode) 183||oidcmp(&ce->oid, &oid)) 184 staged_changes =1; 185 186/* 187 * If the index does not match the file in the work 188 * tree and if it does not match the HEAD commit 189 * either, (1) "git rm" without --cached definitely 190 * will lose information; (2) "git rm --cached" will 191 * lose information unless it is about removing an 192 * "intent to add" entry. 193 */ 194if(local_changes && staged_changes) { 195if(!index_only || !ce_intent_to_add(ce)) 196string_list_append(&files_staged, name); 197} 198else if(!index_only) { 199if(staged_changes) 200string_list_append(&files_cached, name); 201if(local_changes) 202string_list_append(&files_local, name); 203} 204} 205print_error_files(&files_staged, 206Q_("the following file has staged content different " 207"from both the\nfile and the HEAD:", 208"the following files have staged content different" 209" from both the\nfile and the HEAD:", 210 files_staged.nr), 211_("\n(use -f to force removal)"), 212&errs); 213string_list_clear(&files_staged,0); 214print_error_files(&files_cached, 215Q_("the following file has changes " 216"staged in the index:", 217"the following files have changes " 218"staged in the index:", files_cached.nr), 219_("\n(use --cached to keep the file," 220" or -f to force removal)"), 221&errs); 222string_list_clear(&files_cached,0); 223 224print_error_files(&files_local, 225Q_("the following file has local modifications:", 226"the following files have local modifications:", 227 files_local.nr), 228_("\n(use --cached to keep the file," 229" or -f to force removal)"), 230&errs); 231string_list_clear(&files_local,0); 232 233return errs; 234} 235 236static struct lock_file lock_file; 237 238static int show_only =0, force =0, index_only =0, recursive =0, quiet =0; 239static int ignore_unmatch =0; 240 241static struct option builtin_rm_options[] = { 242OPT__DRY_RUN(&show_only,N_("dry run")), 243OPT__QUIET(&quiet,N_("do not list removed files")), 244OPT_BOOL(0,"cached", &index_only,N_("only remove from the index")), 245OPT__FORCE(&force,N_("override the up-to-date check")), 246OPT_BOOL('r', NULL, &recursive,N_("allow recursive removal")), 247OPT_BOOL(0,"ignore-unmatch", &ignore_unmatch, 248N_("exit with a zero status even if nothing matched")), 249OPT_END(), 250}; 251 252intcmd_rm(int argc,const char**argv,const char*prefix) 253{ 254int i; 255struct pathspec pathspec; 256char*seen; 257 258gitmodules_config(); 259git_config(git_default_config, NULL); 260 261 argc =parse_options(argc, argv, prefix, builtin_rm_options, 262 builtin_rm_usage,0); 263if(!argc) 264usage_with_options(builtin_rm_usage, builtin_rm_options); 265 266if(!index_only) 267setup_work_tree(); 268 269hold_locked_index(&lock_file, LOCK_DIE_ON_ERROR); 270 271if(read_cache() <0) 272die(_("index file corrupt")); 273 274parse_pathspec(&pathspec,0, 275 PATHSPEC_PREFER_CWD, 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; 363struct strbuf buf = STRBUF_INIT; 364for(i =0; i < list.nr; i++) { 365const char*path = list.entry[i].name; 366if(list.entry[i].is_submodule) { 367strbuf_reset(&buf); 368strbuf_addstr(&buf, path); 369if(remove_dir_recursively(&buf,0)) 370die(_("could not remove '%s'"), path); 371 372 removed =1; 373if(!remove_path_from_gitmodules(path)) 374 gitmodules_modified =1; 375continue; 376} 377if(!remove_path(path)) { 378 removed =1; 379continue; 380} 381if(!removed) 382die_errno("git rm: '%s'", path); 383} 384strbuf_release(&buf); 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}