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