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