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