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 13static const char*const builtin_rm_usage[] = { 14N_("git rm [options] [--] <file>..."), 15 NULL 16}; 17 18static struct{ 19int nr, alloc; 20const char**name; 21} list; 22 23static intcheck_local_mod(unsigned char*head,int index_only) 24{ 25/* 26 * Items in list are already sorted in the cache order, 27 * so we could do this a lot more efficiently by using 28 * tree_desc based traversal if we wanted to, but I am 29 * lazy, and who cares if removal of files is a tad 30 * slower than the theoretical maximum speed? 31 */ 32int i, no_head; 33int errs =0; 34 35 no_head =is_null_sha1(head); 36for(i =0; i < list.nr; i++) { 37struct stat st; 38int pos; 39struct cache_entry *ce; 40const char*name = list.name[i]; 41unsigned char sha1[20]; 42unsigned mode; 43int local_changes =0; 44int staged_changes =0; 45 46 pos =cache_name_pos(name,strlen(name)); 47if(pos <0) 48continue;/* removing unmerged entry */ 49 ce = active_cache[pos]; 50 51if(lstat(ce->name, &st) <0) { 52if(errno != ENOENT) 53warning("'%s':%s", ce->name,strerror(errno)); 54/* It already vanished from the working tree */ 55continue; 56} 57else if(S_ISDIR(st.st_mode)) { 58/* if a file was removed and it is now a 59 * directory, that is the same as ENOENT as 60 * far as git is concerned; we do not track 61 * directories. 62 */ 63continue; 64} 65 66/* 67 * "rm" of a path that has changes need to be treated 68 * carefully not to allow losing local changes 69 * accidentally. A local change could be (1) file in 70 * work tree is different since the index; and/or (2) 71 * the user staged a content that is different from 72 * the current commit in the index. 73 * 74 * In such a case, you would need to --force the 75 * removal. However, "rm --cached" (remove only from 76 * the index) is safe if the index matches the file in 77 * the work tree or the HEAD commit, as it means that 78 * the content being removed is available elsewhere. 79 */ 80 81/* 82 * Is the index different from the file in the work tree? 83 */ 84if(ce_match_stat(ce, &st,0)) 85 local_changes =1; 86 87/* 88 * Is the index different from the HEAD commit? By 89 * definition, before the very initial commit, 90 * anything staged in the index is treated by the same 91 * way as changed from the HEAD. 92 */ 93if(no_head 94||get_tree_entry(head, name, sha1, &mode) 95|| ce->ce_mode !=create_ce_mode(mode) 96||hashcmp(ce->sha1, sha1)) 97 staged_changes =1; 98 99/* 100 * If the index does not match the file in the work 101 * tree and if it does not match the HEAD commit 102 * either, (1) "git rm" without --cached definitely 103 * will lose information; (2) "git rm --cached" will 104 * lose information unless it is about removing an 105 * "intent to add" entry. 106 */ 107if(local_changes && staged_changes) { 108if(!index_only || !(ce->ce_flags & CE_INTENT_TO_ADD)) 109 errs =error(_("'%s' has staged content different " 110"from both the file and the HEAD\n" 111"(use -f to force removal)"), name); 112} 113else if(!index_only) { 114if(staged_changes) 115 errs =error(_("'%s' has changes staged in the index\n" 116"(use --cached to keep the file, " 117"or -f to force removal)"), name); 118if(local_changes) 119 errs =error(_("'%s' has local modifications\n" 120"(use --cached to keep the file, " 121"or -f to force removal)"), name); 122} 123} 124return errs; 125} 126 127static struct lock_file lock_file; 128 129static int show_only =0, force =0, index_only =0, recursive =0, quiet =0; 130static int ignore_unmatch =0; 131 132static struct option builtin_rm_options[] = { 133OPT__DRY_RUN(&show_only,N_("dry run")), 134OPT__QUIET(&quiet,N_("do not list removed files")), 135OPT_BOOLEAN(0,"cached", &index_only,N_("only remove from the index")), 136OPT__FORCE(&force,N_("override the up-to-date check")), 137OPT_BOOLEAN('r', NULL, &recursive,N_("allow recursive removal")), 138OPT_BOOLEAN(0,"ignore-unmatch", &ignore_unmatch, 139N_("exit with a zero status even if nothing matched")), 140OPT_END(), 141}; 142 143intcmd_rm(int argc,const char**argv,const char*prefix) 144{ 145int i, newfd; 146const char**pathspec; 147char*seen; 148 149git_config(git_default_config, NULL); 150 151 argc =parse_options(argc, argv, prefix, builtin_rm_options, 152 builtin_rm_usage,0); 153if(!argc) 154usage_with_options(builtin_rm_usage, builtin_rm_options); 155 156if(!index_only) 157setup_work_tree(); 158 159 newfd =hold_locked_index(&lock_file,1); 160 161if(read_cache() <0) 162die(_("index file corrupt")); 163 164 pathspec =get_pathspec(prefix, argv); 165refresh_index(&the_index, REFRESH_QUIET, pathspec, NULL, NULL); 166 167 seen = NULL; 168for(i =0; pathspec[i] ; i++) 169/* nothing */; 170 seen =xcalloc(i,1); 171 172for(i =0; i < active_nr; i++) { 173struct cache_entry *ce = active_cache[i]; 174if(!match_pathspec(pathspec, ce->name,ce_namelen(ce),0, seen)) 175continue; 176ALLOC_GROW(list.name, list.nr +1, list.alloc); 177 list.name[list.nr++] = ce->name; 178} 179 180if(pathspec) { 181const char*match; 182int seen_any =0; 183for(i =0; (match = pathspec[i]) != NULL ; i++) { 184if(!seen[i]) { 185if(!ignore_unmatch) { 186die(_("pathspec '%s' did not match any files"), 187 match); 188} 189} 190else{ 191 seen_any =1; 192} 193if(!recursive && seen[i] == MATCHED_RECURSIVELY) 194die(_("not removing '%s' recursively without -r"), 195*match ? match :"."); 196} 197 198if(! seen_any) 199exit(0); 200} 201 202/* 203 * If not forced, the file, the index and the HEAD (if exists) 204 * must match; but the file can already been removed, since 205 * this sequence is a natural "novice" way: 206 * 207 * rm F; git rm F 208 * 209 * Further, if HEAD commit exists, "diff-index --cached" must 210 * report no changes unless forced. 211 */ 212if(!force) { 213unsigned char sha1[20]; 214if(get_sha1("HEAD", sha1)) 215hashclr(sha1); 216if(check_local_mod(sha1, index_only)) 217exit(1); 218} 219 220/* 221 * First remove the names from the index: we won't commit 222 * the index unless all of them succeed. 223 */ 224for(i =0; i < list.nr; i++) { 225const char*path = list.name[i]; 226if(!quiet) 227printf("rm '%s'\n", path); 228 229if(remove_file_from_cache(path)) 230die(_("git rm: unable to remove%s"), path); 231} 232 233if(show_only) 234return0; 235 236/* 237 * Then, unless we used "--cached", remove the filenames from 238 * the workspace. If we fail to remove the first one, we 239 * abort the "git rm" (but once we've successfully removed 240 * any file at all, we'll go ahead and commit to it all: 241 * by then we've already committed ourselves and can't fail 242 * in the middle) 243 */ 244if(!index_only) { 245int removed =0; 246for(i =0; i < list.nr; i++) { 247const char*path = list.name[i]; 248if(!remove_path(path)) { 249 removed =1; 250continue; 251} 252if(!removed) 253die_errno("git rm: '%s'", path); 254} 255} 256 257if(active_cache_changed) { 258if(write_cache(newfd, active_cache, active_nr) || 259commit_locked_index(&lock_file)) 260die(_("Unable to write new index file")); 261} 262 263return0; 264}