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