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