Linus Torvalds | d9b814c | 2006-05-19 16:19:34 -0700 | [diff] [blame] | 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" |
Junio C Hamano | 3fb3cc6 | 2006-05-23 01:31:38 -0700 | [diff] [blame] | 9 | #include "cache-tree.h" |
Junio C Hamano | 9f95069 | 2006-12-25 03:11:00 -0800 | [diff] [blame] | 10 | #include "tree-walk.h" |
Pierre Habouzit | f09985c | 2007-10-05 21:09:19 +0200 | [diff] [blame] | 11 | #include "parse-options.h" |
Linus Torvalds | d9b814c | 2006-05-19 16:19:34 -0700 | [diff] [blame] | 12 | |
Pierre Habouzit | f09985c | 2007-10-05 21:09:19 +0200 | [diff] [blame] | 13 | static const char * const builtin_rm_usage[] = { |
Stephan Beyer | 1b1dd23 | 2008-07-13 15:36:15 +0200 | [diff] [blame] | 14 | "git rm [options] [--] <file>...", |
Pierre Habouzit | f09985c | 2007-10-05 21:09:19 +0200 | [diff] [blame] | 15 | NULL |
| 16 | }; |
Linus Torvalds | d9b814c | 2006-05-19 16:19:34 -0700 | [diff] [blame] | 17 | |
| 18 | static struct { |
| 19 | int nr, alloc; |
| 20 | const char **name; |
| 21 | } list; |
| 22 | |
Matthieu Moy | bdecd9d | 2007-07-13 19:41:38 +0200 | [diff] [blame] | 23 | static int check_local_mod(unsigned char *head, int index_only) |
Junio C Hamano | 9f95069 | 2006-12-25 03:11:00 -0800 | [diff] [blame] | 24 | { |
Junio C Hamano | 69530cb | 2008-11-28 18:41:21 -0800 | [diff] [blame] | 25 | /* |
| 26 | * Items in list are already sorted in the cache order, |
Junio C Hamano | 9f95069 | 2006-12-25 03:11:00 -0800 | [diff] [blame] | 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 | */ |
| 32 | int i, no_head; |
| 33 | int errs = 0; |
| 34 | |
| 35 | no_head = is_null_sha1(head); |
| 36 | for (i = 0; i < list.nr; i++) { |
| 37 | struct stat st; |
| 38 | int pos; |
| 39 | struct cache_entry *ce; |
| 40 | const char *name = list.name[i]; |
| 41 | unsigned char sha1[20]; |
| 42 | unsigned mode; |
Matthieu Moy | bdecd9d | 2007-07-13 19:41:38 +0200 | [diff] [blame] | 43 | int local_changes = 0; |
| 44 | int staged_changes = 0; |
Junio C Hamano | 9f95069 | 2006-12-25 03:11:00 -0800 | [diff] [blame] | 45 | |
| 46 | pos = cache_name_pos(name, strlen(name)); |
| 47 | if (pos < 0) |
| 48 | continue; /* removing unmerged entry */ |
| 49 | ce = active_cache[pos]; |
| 50 | |
| 51 | if (lstat(ce->name, &st) < 0) { |
| 52 | if (errno != ENOENT) |
Miklos Vajna | c36d785 | 2009-03-24 02:09:14 +0100 | [diff] [blame] | 53 | warning("'%s': %s", ce->name, strerror(errno)); |
Junio C Hamano | 9f95069 | 2006-12-25 03:11:00 -0800 | [diff] [blame] | 54 | /* It already vanished from the working tree */ |
| 55 | continue; |
| 56 | } |
| 57 | else 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 | */ |
| 63 | continue; |
| 64 | } |
Junio C Hamano | 69530cb | 2008-11-28 18:41:21 -0800 | [diff] [blame] | 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 | */ |
Junio C Hamano | 9f95069 | 2006-12-25 03:11:00 -0800 | [diff] [blame] | 84 | if (ce_match_stat(ce, &st, 0)) |
Matthieu Moy | bdecd9d | 2007-07-13 19:41:38 +0200 | [diff] [blame] | 85 | local_changes = 1; |
Junio C Hamano | 69530cb | 2008-11-28 18:41:21 -0800 | [diff] [blame] | 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 | */ |
Jeff King | e4d9516 | 2007-03-26 14:55:39 -0400 | [diff] [blame] | 93 | if (no_head |
| 94 | || get_tree_entry(head, name, sha1, &mode) |
| 95 | || ce->ce_mode != create_ce_mode(mode) |
| 96 | || hashcmp(ce->sha1, sha1)) |
Matthieu Moy | bdecd9d | 2007-07-13 19:41:38 +0200 | [diff] [blame] | 97 | staged_changes = 1; |
| 98 | |
Junio C Hamano | 69530cb | 2008-11-28 18:41:21 -0800 | [diff] [blame] | 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 | */ |
| 107 | if (local_changes && staged_changes) { |
Junio C Hamano | 388b2ac | 2008-11-28 19:55:25 -0800 | [diff] [blame] | 108 | if (!index_only || !(ce->ce_flags & CE_INTENT_TO_ADD)) |
Junio C Hamano | 69530cb | 2008-11-28 18:41:21 -0800 | [diff] [blame] | 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 | } |
Matthieu Moy | bdecd9d | 2007-07-13 19:41:38 +0200 | [diff] [blame] | 113 | else if (!index_only) { |
Matthieu Moy | bdecd9d | 2007-07-13 19:41:38 +0200 | [diff] [blame] | 114 | if (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); |
| 118 | if (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 | } |
Junio C Hamano | 9f95069 | 2006-12-25 03:11:00 -0800 | [diff] [blame] | 123 | } |
| 124 | return errs; |
| 125 | } |
| 126 | |
Junio C Hamano | 021b6e4 | 2006-06-06 12:51:49 -0700 | [diff] [blame] | 127 | static struct lock_file lock_file; |
Linus Torvalds | d9b814c | 2006-05-19 16:19:34 -0700 | [diff] [blame] | 128 | |
Pierre Habouzit | f09985c | 2007-10-05 21:09:19 +0200 | [diff] [blame] | 129 | static int show_only = 0, force = 0, index_only = 0, recursive = 0, quiet = 0; |
| 130 | static int ignore_unmatch = 0; |
| 131 | |
| 132 | static struct option builtin_rm_options[] = { |
René Scharfe | e21adb8 | 2010-11-08 18:58:51 +0100 | [diff] [blame] | 133 | OPT__DRY_RUN(&show_only, "dry run"), |
Jonathan Nieder | 8c83968 | 2010-11-08 13:54:48 -0600 | [diff] [blame] | 134 | OPT__QUIET(&quiet, "do not list removed files"), |
Pierre Habouzit | f09985c | 2007-10-05 21:09:19 +0200 | [diff] [blame] | 135 | OPT_BOOLEAN( 0 , "cached", &index_only, "only remove from the index"), |
René Scharfe | 76946b7 | 2010-11-08 19:01:54 +0100 | [diff] [blame] | 136 | OPT__FORCE(&force, "override the up-to-date check"), |
Pierre Habouzit | f09985c | 2007-10-05 21:09:19 +0200 | [diff] [blame] | 137 | OPT_BOOLEAN('r', NULL, &recursive, "allow recursive removal"), |
| 138 | OPT_BOOLEAN( 0 , "ignore-unmatch", &ignore_unmatch, |
| 139 | "exit with a zero status even if nothing matched"), |
| 140 | OPT_END(), |
| 141 | }; |
| 142 | |
Linus Torvalds | a633fca | 2006-07-28 22:44:25 -0700 | [diff] [blame] | 143 | int cmd_rm(int argc, const char **argv, const char *prefix) |
Linus Torvalds | d9b814c | 2006-05-19 16:19:34 -0700 | [diff] [blame] | 144 | { |
| 145 | int i, newfd; |
Linus Torvalds | d9b814c | 2006-05-19 16:19:34 -0700 | [diff] [blame] | 146 | const char **pathspec; |
| 147 | char *seen; |
| 148 | |
Johannes Schindelin | ef90d6d | 2008-05-14 18:46:53 +0100 | [diff] [blame] | 149 | git_config(git_default_config, NULL); |
Linus Torvalds | d9b814c | 2006-05-19 16:19:34 -0700 | [diff] [blame] | 150 | |
Stephen Boyd | 3778292 | 2009-05-23 11:53:12 -0700 | [diff] [blame] | 151 | argc = parse_options(argc, argv, prefix, builtin_rm_options, |
| 152 | builtin_rm_usage, 0); |
Pierre Habouzit | f09985c | 2007-10-05 21:09:19 +0200 | [diff] [blame] | 153 | if (!argc) |
| 154 | usage_with_options(builtin_rm_usage, builtin_rm_options); |
Linus Torvalds | d9b814c | 2006-05-19 16:19:34 -0700 | [diff] [blame] | 155 | |
Mike Hommey | 271bb08 | 2007-11-03 12:23:13 +0100 | [diff] [blame] | 156 | if (!index_only) |
| 157 | setup_work_tree(); |
| 158 | |
Olivier Marin | 4d26467 | 2008-07-19 18:24:46 +0200 | [diff] [blame] | 159 | newfd = hold_locked_index(&lock_file, 1); |
| 160 | |
| 161 | if (read_cache() < 0) |
| 162 | die("index file corrupt"); |
| 163 | |
Pierre Habouzit | f09985c | 2007-10-05 21:09:19 +0200 | [diff] [blame] | 164 | pathspec = get_pathspec(prefix, argv); |
Nguyễn Thái Ngọc Duy | 4e1a7ba | 2010-01-17 15:43:13 +0700 | [diff] [blame] | 165 | refresh_index(&the_index, REFRESH_QUIET, pathspec, NULL, NULL); |
| 166 | |
Linus Torvalds | d9b814c | 2006-05-19 16:19:34 -0700 | [diff] [blame] | 167 | seen = NULL; |
Junio C Hamano | 7612a1e | 2006-06-08 21:11:25 -0700 | [diff] [blame] | 168 | for (i = 0; pathspec[i] ; i++) |
| 169 | /* nothing */; |
Peter Eriksen | 28f7581 | 2006-07-25 09:30:18 +0200 | [diff] [blame] | 170 | seen = xcalloc(i, 1); |
Linus Torvalds | d9b814c | 2006-05-19 16:19:34 -0700 | [diff] [blame] | 171 | |
| 172 | for (i = 0; i < active_nr; i++) { |
| 173 | struct cache_entry *ce = active_cache[i]; |
| 174 | if (!match_pathspec(pathspec, ce->name, ce_namelen(ce), 0, seen)) |
| 175 | continue; |
Thiago Farina | 834d9eb | 2010-12-19 09:56:25 -0200 | [diff] [blame] | 176 | ALLOC_GROW(list.name, list.nr + 1, list.alloc); |
| 177 | list.name[list.nr++] = ce->name; |
Linus Torvalds | d9b814c | 2006-05-19 16:19:34 -0700 | [diff] [blame] | 178 | } |
| 179 | |
| 180 | if (pathspec) { |
| 181 | const char *match; |
Steven Grimm | bb1faf0 | 2007-04-16 00:53:24 -0700 | [diff] [blame] | 182 | int seen_any = 0; |
Linus Torvalds | d9b814c | 2006-05-19 16:19:34 -0700 | [diff] [blame] | 183 | for (i = 0; (match = pathspec[i]) != NULL ; i++) { |
Steven Grimm | bb1faf0 | 2007-04-16 00:53:24 -0700 | [diff] [blame] | 184 | if (!seen[i]) { |
| 185 | if (!ignore_unmatch) { |
| 186 | die("pathspec '%s' did not match any files", |
| 187 | match); |
| 188 | } |
| 189 | } |
| 190 | else { |
| 191 | seen_any = 1; |
| 192 | } |
Junio C Hamano | 9f95069 | 2006-12-25 03:11:00 -0800 | [diff] [blame] | 193 | if (!recursive && seen[i] == MATCHED_RECURSIVELY) |
| 194 | die("not removing '%s' recursively without -r", |
| 195 | *match ? match : "."); |
Linus Torvalds | d9b814c | 2006-05-19 16:19:34 -0700 | [diff] [blame] | 196 | } |
Steven Grimm | bb1faf0 | 2007-04-16 00:53:24 -0700 | [diff] [blame] | 197 | |
| 198 | if (! seen_any) |
| 199 | exit(0); |
Linus Torvalds | d9b814c | 2006-05-19 16:19:34 -0700 | [diff] [blame] | 200 | } |
| 201 | |
| 202 | /* |
Junio C Hamano | 9f95069 | 2006-12-25 03:11:00 -0800 | [diff] [blame] | 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 | * |
Steven Grimm | 9474eda | 2007-04-16 01:17:32 -0700 | [diff] [blame] | 207 | * rm F; git rm F |
Junio C Hamano | 9f95069 | 2006-12-25 03:11:00 -0800 | [diff] [blame] | 208 | * |
| 209 | * Further, if HEAD commit exists, "diff-index --cached" must |
| 210 | * report no changes unless forced. |
| 211 | */ |
| 212 | if (!force) { |
| 213 | unsigned char sha1[20]; |
| 214 | if (get_sha1("HEAD", sha1)) |
| 215 | hashclr(sha1); |
Matthieu Moy | bdecd9d | 2007-07-13 19:41:38 +0200 | [diff] [blame] | 216 | if (check_local_mod(sha1, index_only)) |
Junio C Hamano | 9f95069 | 2006-12-25 03:11:00 -0800 | [diff] [blame] | 217 | exit(1); |
| 218 | } |
| 219 | |
| 220 | /* |
Linus Torvalds | d9b814c | 2006-05-19 16:19:34 -0700 | [diff] [blame] | 221 | * First remove the names from the index: we won't commit |
Junio C Hamano | 9f95069 | 2006-12-25 03:11:00 -0800 | [diff] [blame] | 222 | * the index unless all of them succeed. |
Linus Torvalds | d9b814c | 2006-05-19 16:19:34 -0700 | [diff] [blame] | 223 | */ |
| 224 | for (i = 0; i < list.nr; i++) { |
| 225 | const char *path = list.name[i]; |
Steven Grimm | b48caa2 | 2007-04-16 00:46:48 -0700 | [diff] [blame] | 226 | if (!quiet) |
| 227 | printf("rm '%s'\n", path); |
Linus Torvalds | d9b814c | 2006-05-19 16:19:34 -0700 | [diff] [blame] | 228 | |
| 229 | if (remove_file_from_cache(path)) |
Junio C Hamano | 7e44c93 | 2008-08-31 09:39:19 -0700 | [diff] [blame] | 230 | die("git rm: unable to remove %s", path); |
Linus Torvalds | d9b814c | 2006-05-19 16:19:34 -0700 | [diff] [blame] | 231 | } |
| 232 | |
Junio C Hamano | 7612a1e | 2006-06-08 21:11:25 -0700 | [diff] [blame] | 233 | if (show_only) |
| 234 | return 0; |
| 235 | |
Linus Torvalds | d9b814c | 2006-05-19 16:19:34 -0700 | [diff] [blame] | 236 | /* |
Junio C Hamano | 646ac22 | 2007-01-11 14:58:47 -0800 | [diff] [blame] | 237 | * Then, unless we used "--cached", remove the filenames from |
Junio C Hamano | 9f95069 | 2006-12-25 03:11:00 -0800 | [diff] [blame] | 238 | * the workspace. If we fail to remove the first one, we |
Linus Torvalds | d9b814c | 2006-05-19 16:19:34 -0700 | [diff] [blame] | 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: |
Pavel Roskin | 82e5a82 | 2006-07-10 01:50:18 -0400 | [diff] [blame] | 241 | * by then we've already committed ourselves and can't fail |
Linus Torvalds | d9b814c | 2006-05-19 16:19:34 -0700 | [diff] [blame] | 242 | * in the middle) |
| 243 | */ |
Junio C Hamano | 9f95069 | 2006-12-25 03:11:00 -0800 | [diff] [blame] | 244 | if (!index_only) { |
Linus Torvalds | d9b814c | 2006-05-19 16:19:34 -0700 | [diff] [blame] | 245 | int removed = 0; |
| 246 | for (i = 0; i < list.nr; i++) { |
| 247 | const char *path = list.name[i]; |
Alex Riesen | 175a494 | 2008-09-27 00:59:14 +0200 | [diff] [blame] | 248 | if (!remove_path(path)) { |
Linus Torvalds | d9b814c | 2006-05-19 16:19:34 -0700 | [diff] [blame] | 249 | removed = 1; |
| 250 | continue; |
| 251 | } |
| 252 | if (!removed) |
Thomas Rast | d824cbb | 2009-06-27 17:58:46 +0200 | [diff] [blame] | 253 | die_errno("git rm: '%s'", path); |
Linus Torvalds | d9b814c | 2006-05-19 16:19:34 -0700 | [diff] [blame] | 254 | } |
| 255 | } |
| 256 | |
| 257 | if (active_cache_changed) { |
| 258 | if (write_cache(newfd, active_cache, active_nr) || |
Brandon Casey | 4ed7cd3 | 2008-01-16 13:12:46 -0600 | [diff] [blame] | 259 | commit_locked_index(&lock_file)) |
Linus Torvalds | d9b814c | 2006-05-19 16:19:34 -0700 | [diff] [blame] | 260 | die("Unable to write new index file"); |
| 261 | } |
| 262 | |
| 263 | return 0; |
| 264 | } |