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) 62warning("'%s':%s", 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} 74 75/* 76 * "rm" of a path that has changes need to be treated 77 * carefully not to allow losing local changes 78 * accidentally. A local change could be (1) file in 79 * work tree is different since the index; and/or (2) 80 * the user staged a content that is different from 81 * the current commit in the index. 82 * 83 * In such a case, you would need to --force the 84 * removal. However, "rm --cached" (remove only from 85 * the index) is safe if the index matches the file in 86 * the work tree or the HEAD commit, as it means that 87 * the content being removed is available elsewhere. 88 */ 89 90/* 91 * Is the index different from the file in the work tree? 92 */ 93if(ce_match_stat(ce, &st,0)) 94 local_changes =1; 95 96/* 97 * Is the index different from the HEAD commit? By 98 * definition, before the very initial commit, 99 * anything staged in the index is treated by the same 100 * way as changed from the HEAD. 101 */ 102if(no_head 103||get_tree_entry(head, name, sha1, &mode) 104|| ce->ce_mode !=create_ce_mode(mode) 105||hashcmp(ce->sha1, sha1)) 106 staged_changes =1; 107 108/* 109 * If the index does not match the file in the work 110 * tree and if it does not match the HEAD commit 111 * either, (1) "git rm" without --cached definitely 112 * will lose information; (2) "git rm --cached" will 113 * lose information unless it is about removing an 114 * "intent to add" entry. 115 */ 116if(local_changes && staged_changes) { 117if(!index_only || !(ce->ce_flags & CE_INTENT_TO_ADD)) 118 errs =error("'%s' has staged content different " 119"from both the file and the HEAD\n" 120"(use -f to force removal)", name); 121} 122else if(!index_only) { 123if(staged_changes) 124 errs =error("'%s' has changes staged in the index\n" 125"(use --cached to keep the file, " 126"or -f to force removal)", name); 127if(local_changes) 128 errs =error("'%s' has local modifications\n" 129"(use --cached to keep the file, " 130"or -f to force removal)", name); 131} 132} 133return errs; 134} 135 136static struct lock_file lock_file; 137 138static int show_only =0, force =0, index_only =0, recursive =0, quiet =0; 139static int ignore_unmatch =0; 140 141static struct option builtin_rm_options[] = { 142OPT__DRY_RUN(&show_only), 143OPT__QUIET(&quiet), 144OPT_BOOLEAN(0,"cached", &index_only,"only remove from the index"), 145OPT_BOOLEAN('f',"force", &force,"override the up-to-date check"), 146OPT_BOOLEAN('r', NULL, &recursive,"allow recursive removal"), 147OPT_BOOLEAN(0,"ignore-unmatch", &ignore_unmatch, 148"exit with a zero status even if nothing matched"), 149OPT_END(), 150}; 151 152intcmd_rm(int argc,const char**argv,const char*prefix) 153{ 154int i, newfd; 155const char**pathspec; 156char*seen; 157 158git_config(git_default_config, NULL); 159 160 argc =parse_options(argc, argv, prefix, builtin_rm_options, 161 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"); 172 173 pathspec =get_pathspec(prefix, argv); 174refresh_index(&the_index, REFRESH_QUIET, pathspec, NULL, NULL); 175 176 seen = NULL; 177for(i =0; pathspec[i] ; i++) 178/* nothing */; 179 seen =xcalloc(i,1); 180 181for(i =0; i < active_nr; i++) { 182struct cache_entry *ce = active_cache[i]; 183if(!match_pathspec(pathspec, ce->name,ce_namelen(ce),0, seen)) 184continue; 185add_list(ce->name); 186} 187 188if(pathspec) { 189const char*match; 190int seen_any =0; 191for(i =0; (match = pathspec[i]) != NULL ; i++) { 192if(!seen[i]) { 193if(!ignore_unmatch) { 194die("pathspec '%s' did not match any files", 195 match); 196} 197} 198else{ 199 seen_any =1; 200} 201if(!recursive && seen[i] == MATCHED_RECURSIVELY) 202die("not removing '%s' recursively without -r", 203*match ? match :"."); 204} 205 206if(! seen_any) 207exit(0); 208} 209 210/* 211 * If not forced, the file, the index and the HEAD (if exists) 212 * must match; but the file can already been removed, since 213 * this sequence is a natural "novice" way: 214 * 215 * rm F; git rm F 216 * 217 * Further, if HEAD commit exists, "diff-index --cached" must 218 * report no changes unless forced. 219 */ 220if(!force) { 221unsigned char sha1[20]; 222if(get_sha1("HEAD", sha1)) 223hashclr(sha1); 224if(check_local_mod(sha1, index_only)) 225exit(1); 226} 227 228/* 229 * First remove the names from the index: we won't commit 230 * the index unless all of them succeed. 231 */ 232for(i =0; i < list.nr; i++) { 233const char*path = list.name[i]; 234if(!quiet) 235printf("rm '%s'\n", path); 236 237if(remove_file_from_cache(path)) 238die("git rm: unable to remove%s", path); 239} 240 241if(show_only) 242return0; 243 244/* 245 * Then, unless we used "--cached", remove the filenames from 246 * the workspace. If we fail to remove the first one, we 247 * abort the "git rm" (but once we've successfully removed 248 * any file at all, we'll go ahead and commit to it all: 249 * by then we've already committed ourselves and can't fail 250 * in the middle) 251 */ 252if(!index_only) { 253int removed =0; 254for(i =0; i < list.nr; i++) { 255const char*path = list.name[i]; 256if(!remove_path(path)) { 257 removed =1; 258continue; 259} 260if(!removed) 261die_errno("git rm: '%s'", path); 262} 263} 264 265if(active_cache_changed) { 266if(write_cache(newfd, active_cache, active_nr) || 267commit_locked_index(&lock_file)) 268die("Unable to write new index file"); 269} 270 271return0; 272}