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