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 15static const char*const builtin_rm_usage[] = { 16N_("git rm [options] [--] <file>..."), 17 NULL 18}; 19 20static struct{ 21int nr, alloc; 22struct{ 23const char*name; 24char is_submodule; 25} *entry; 26} list; 27 28static intget_ours_cache_pos(const char*path,int pos) 29{ 30int i = -pos -1; 31 32while((i < active_nr) && !strcmp(active_cache[i]->name, path)) { 33if(ce_stage(active_cache[i]) ==2) 34return i; 35 i++; 36} 37return-1; 38} 39 40static voidprint_error_files(struct string_list *files_list, 41const char*main_msg, 42const char*hints_msg, 43int*errs) 44{ 45if(files_list->nr) { 46int i; 47struct strbuf err_msg = STRBUF_INIT; 48 49strbuf_addstr(&err_msg, main_msg); 50for(i =0; i < files_list->nr; i++) 51strbuf_addf(&err_msg, 52"\n%s", 53 files_list->items[i].string); 54if(advice_rm_hints) 55strbuf_addstr(&err_msg, hints_msg); 56*errs =error("%s", err_msg.buf); 57strbuf_release(&err_msg); 58} 59} 60 61static voiderror_removing_concrete_submodules(struct string_list *files,int*errs) 62{ 63print_error_files(files, 64Q_("the following submodule (or one of its nested " 65"submodules)\n" 66"uses a .git directory:", 67"the following submodules (or one of its nested " 68"submodules)\n" 69"use a .git directory:", files->nr), 70_("\n(use 'rm -rf' if you really want to remove " 71"it including all of its history)"), 72 errs); 73string_list_clear(files,0); 74} 75 76static intcheck_submodules_use_gitfiles(void) 77{ 78int i; 79int errs =0; 80struct string_list files = STRING_LIST_INIT_NODUP; 81 82for(i =0; i < list.nr; i++) { 83const char*name = list.entry[i].name; 84int pos; 85const struct cache_entry *ce; 86struct stat st; 87 88 pos =cache_name_pos(name,strlen(name)); 89if(pos <0) { 90 pos =get_ours_cache_pos(name, pos); 91if(pos <0) 92continue; 93} 94 ce = active_cache[pos]; 95 96if(!S_ISGITLINK(ce->ce_mode) || 97(lstat(ce->name, &st) <0) || 98is_empty_dir(name)) 99continue; 100 101if(!submodule_uses_gitfile(name)) 102string_list_append(&files, name); 103} 104 105error_removing_concrete_submodules(&files, &errs); 106 107return errs; 108} 109 110static intcheck_local_mod(unsigned char*head,int index_only) 111{ 112/* 113 * Items in list are already sorted in the cache order, 114 * so we could do this a lot more efficiently by using 115 * tree_desc based traversal if we wanted to, but I am 116 * lazy, and who cares if removal of files is a tad 117 * slower than the theoretical maximum speed? 118 */ 119int i, no_head; 120int errs =0; 121struct string_list files_staged = STRING_LIST_INIT_NODUP; 122struct string_list files_cached = STRING_LIST_INIT_NODUP; 123struct string_list files_submodule = STRING_LIST_INIT_NODUP; 124struct string_list files_local = STRING_LIST_INIT_NODUP; 125 126 no_head =is_null_sha1(head); 127for(i =0; i < list.nr; i++) { 128struct stat st; 129int pos; 130const struct cache_entry *ce; 131const char*name = list.entry[i].name; 132unsigned char sha1[20]; 133unsigned mode; 134int local_changes =0; 135int staged_changes =0; 136 137 pos =cache_name_pos(name,strlen(name)); 138if(pos <0) { 139/* 140 * Skip unmerged entries except for populated submodules 141 * that could lose history when removed. 142 */ 143 pos =get_ours_cache_pos(name, pos); 144if(pos <0) 145continue; 146 147if(!S_ISGITLINK(active_cache[pos]->ce_mode) || 148is_empty_dir(name)) 149continue; 150} 151 ce = active_cache[pos]; 152 153if(lstat(ce->name, &st) <0) { 154if(errno != ENOENT && errno != ENOTDIR) 155warning("'%s':%s", ce->name,strerror(errno)); 156/* It already vanished from the working tree */ 157continue; 158} 159else if(S_ISDIR(st.st_mode)) { 160/* if a file was removed and it is now a 161 * directory, that is the same as ENOENT as 162 * far as git is concerned; we do not track 163 * directories unless they are submodules. 164 */ 165if(!S_ISGITLINK(ce->ce_mode)) 166continue; 167} 168 169/* 170 * "rm" of a path that has changes need to be treated 171 * carefully not to allow losing local changes 172 * accidentally. A local change could be (1) file in 173 * work tree is different since the index; and/or (2) 174 * the user staged a content that is different from 175 * the current commit in the index. 176 * 177 * In such a case, you would need to --force the 178 * removal. However, "rm --cached" (remove only from 179 * the index) is safe if the index matches the file in 180 * the work tree or the HEAD commit, as it means that 181 * the content being removed is available elsewhere. 182 */ 183 184/* 185 * Is the index different from the file in the work tree? 186 * If it's a submodule, is its work tree modified? 187 */ 188if(ce_match_stat(ce, &st,0) || 189(S_ISGITLINK(ce->ce_mode) && 190!ok_to_remove_submodule(ce->name))) 191 local_changes =1; 192 193/* 194 * Is the index different from the HEAD commit? By 195 * definition, before the very initial commit, 196 * anything staged in the index is treated by the same 197 * way as changed from the HEAD. 198 */ 199if(no_head 200||get_tree_entry(head, name, sha1, &mode) 201|| ce->ce_mode !=create_ce_mode(mode) 202||hashcmp(ce->sha1, sha1)) 203 staged_changes =1; 204 205/* 206 * If the index does not match the file in the work 207 * tree and if it does not match the HEAD commit 208 * either, (1) "git rm" without --cached definitely 209 * will lose information; (2) "git rm --cached" will 210 * lose information unless it is about removing an 211 * "intent to add" entry. 212 */ 213if(local_changes && staged_changes) { 214if(!index_only || !(ce->ce_flags & CE_INTENT_TO_ADD)) 215string_list_append(&files_staged, name); 216} 217else if(!index_only) { 218if(staged_changes) 219string_list_append(&files_cached, name); 220if(local_changes) { 221if(S_ISGITLINK(ce->ce_mode) && 222!submodule_uses_gitfile(name)) 223string_list_append(&files_submodule, name); 224else 225string_list_append(&files_local, name); 226} 227} 228} 229print_error_files(&files_staged, 230Q_("the following file has staged content different " 231"from both the\nfile and the HEAD:", 232"the following files have staged content different" 233" from both the\nfile and the HEAD:", 234 files_staged.nr), 235_("\n(use -f to force removal)"), 236&errs); 237string_list_clear(&files_staged,0); 238print_error_files(&files_cached, 239Q_("the following file has changes " 240"staged in the index:", 241"the following files have changes " 242"staged in the index:", files_cached.nr), 243_("\n(use --cached to keep the file," 244" or -f to force removal)"), 245&errs); 246string_list_clear(&files_cached,0); 247 248error_removing_concrete_submodules(&files_submodule, &errs); 249 250print_error_files(&files_local, 251Q_("the following file has local modifications:", 252"the following files have local modifications:", 253 files_local.nr), 254_("\n(use --cached to keep the file," 255" or -f to force removal)"), 256&errs); 257string_list_clear(&files_local,0); 258 259return errs; 260} 261 262static struct lock_file lock_file; 263 264static int show_only =0, force =0, index_only =0, recursive =0, quiet =0; 265static int ignore_unmatch =0; 266 267static struct option builtin_rm_options[] = { 268OPT__DRY_RUN(&show_only,N_("dry run")), 269OPT__QUIET(&quiet,N_("do not list removed files")), 270OPT_BOOLEAN(0,"cached", &index_only,N_("only remove from the index")), 271OPT__FORCE(&force,N_("override the up-to-date check")), 272OPT_BOOLEAN('r', NULL, &recursive,N_("allow recursive removal")), 273OPT_BOOLEAN(0,"ignore-unmatch", &ignore_unmatch, 274N_("exit with a zero status even if nothing matched")), 275OPT_END(), 276}; 277 278intcmd_rm(int argc,const char**argv,const char*prefix) 279{ 280int i, newfd; 281const char**pathspec; 282char*seen; 283 284git_config(git_default_config, NULL); 285 286 argc =parse_options(argc, argv, prefix, builtin_rm_options, 287 builtin_rm_usage,0); 288if(!argc) 289usage_with_options(builtin_rm_usage, builtin_rm_options); 290 291if(!index_only) 292setup_work_tree(); 293 294 newfd =hold_locked_index(&lock_file,1); 295 296if(read_cache() <0) 297die(_("index file corrupt")); 298 299/* 300 * Drop trailing directory separators from directories so we'll find 301 * submodules in the index. 302 */ 303for(i =0; i < argc; i++) { 304size_t pathlen =strlen(argv[i]); 305if(pathlen &&is_dir_sep(argv[i][pathlen -1]) && 306is_directory(argv[i])) { 307do{ 308 pathlen--; 309}while(pathlen &&is_dir_sep(argv[i][pathlen -1])); 310 argv[i] =xmemdupz(argv[i], pathlen); 311} 312} 313 314 pathspec =get_pathspec(prefix, argv); 315refresh_index(&the_index, REFRESH_QUIET, pathspec, NULL, NULL); 316 317 seen = NULL; 318for(i =0; pathspec[i] ; i++) 319/* nothing */; 320 seen =xcalloc(i,1); 321 322for(i =0; i < active_nr; i++) { 323const struct cache_entry *ce = active_cache[i]; 324if(!match_pathspec(pathspec, ce->name,ce_namelen(ce),0, seen)) 325continue; 326ALLOC_GROW(list.entry, list.nr +1, list.alloc); 327 list.entry[list.nr].name = ce->name; 328 list.entry[list.nr++].is_submodule =S_ISGITLINK(ce->ce_mode); 329} 330 331if(pathspec) { 332const char*match; 333int seen_any =0; 334for(i =0; (match = pathspec[i]) != NULL ; i++) { 335if(!seen[i]) { 336if(!ignore_unmatch) { 337die(_("pathspec '%s' did not match any files"), 338 match); 339} 340} 341else{ 342 seen_any =1; 343} 344if(!recursive && seen[i] == MATCHED_RECURSIVELY) 345die(_("not removing '%s' recursively without -r"), 346*match ? match :"."); 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}