]>
Commit | Line | Data |
---|---|---|
d9b814cc LT |
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" | |
3fb3cc69 | 9 | #include "cache-tree.h" |
9f95069b | 10 | #include "tree-walk.h" |
f09985c2 | 11 | #include "parse-options.h" |
d9b814cc | 12 | |
f09985c2 | 13 | static const char * const builtin_rm_usage[] = { |
1b1dd23f | 14 | "git rm [options] [--] <file>...", |
f09985c2 PH |
15 | NULL |
16 | }; | |
d9b814cc LT |
17 | |
18 | static struct { | |
19 | int nr, alloc; | |
20 | const char **name; | |
21 | } list; | |
22 | ||
bdecd9d4 | 23 | static int check_local_mod(unsigned char *head, int index_only) |
9f95069b | 24 | { |
69530cb0 JH |
25 | /* |
26 | * Items in list are already sorted in the cache order, | |
9f95069b JH |
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; | |
bdecd9d4 MM |
43 | int local_changes = 0; |
44 | int staged_changes = 0; | |
9f95069b JH |
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) | |
c36d785d | 53 | warning("'%s': %s", ce->name, strerror(errno)); |
9f95069b JH |
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 | } | |
69530cb0 JH |
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 | */ | |
9f95069b | 84 | if (ce_match_stat(ce, &st, 0)) |
bdecd9d4 | 85 | local_changes = 1; |
69530cb0 JH |
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 | */ | |
e4d9516b JK |
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)) | |
bdecd9d4 MM |
97 | staged_changes = 1; |
98 | ||
69530cb0 JH |
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) { | |
388b2acd | 108 | if (!index_only || !(ce->ce_flags & CE_INTENT_TO_ADD)) |
b9b537f7 | 109 | errs = error(_("'%s' has staged content different " |
69530cb0 | 110 | "from both the file and the HEAD\n" |
b9b537f7 | 111 | "(use -f to force removal)"), name); |
69530cb0 | 112 | } |
bdecd9d4 | 113 | else if (!index_only) { |
bdecd9d4 | 114 | if (staged_changes) |
b9b537f7 | 115 | errs = error(_("'%s' has changes staged in the index\n" |
bdecd9d4 | 116 | "(use --cached to keep the file, " |
b9b537f7 | 117 | "or -f to force removal)"), name); |
bdecd9d4 | 118 | if (local_changes) |
b9b537f7 | 119 | errs = error(_("'%s' has local modifications\n" |
bdecd9d4 | 120 | "(use --cached to keep the file, " |
b9b537f7 | 121 | "or -f to force removal)"), name); |
bdecd9d4 | 122 | } |
9f95069b JH |
123 | } |
124 | return errs; | |
125 | } | |
126 | ||
021b6e45 | 127 | static struct lock_file lock_file; |
d9b814cc | 128 | |
f09985c2 PH |
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[] = { | |
e21adb8c | 133 | OPT__DRY_RUN(&show_only, "dry run"), |
8c839683 | 134 | OPT__QUIET(&quiet, "do not list removed files"), |
f09985c2 | 135 | OPT_BOOLEAN( 0 , "cached", &index_only, "only remove from the index"), |
76946b76 | 136 | OPT__FORCE(&force, "override the up-to-date check"), |
f09985c2 PH |
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 | ||
a633fca0 | 143 | int cmd_rm(int argc, const char **argv, const char *prefix) |
d9b814cc LT |
144 | { |
145 | int i, newfd; | |
d9b814cc LT |
146 | const char **pathspec; |
147 | char *seen; | |
148 | ||
ef90d6d4 | 149 | git_config(git_default_config, NULL); |
d9b814cc | 150 | |
37782920 SB |
151 | argc = parse_options(argc, argv, prefix, builtin_rm_options, |
152 | builtin_rm_usage, 0); | |
f09985c2 PH |
153 | if (!argc) |
154 | usage_with_options(builtin_rm_usage, builtin_rm_options); | |
d9b814cc | 155 | |
271bb087 MH |
156 | if (!index_only) |
157 | setup_work_tree(); | |
158 | ||
4d264672 OM |
159 | newfd = hold_locked_index(&lock_file, 1); |
160 | ||
161 | if (read_cache() < 0) | |
b9b537f7 | 162 | die(_("index file corrupt")); |
4d264672 | 163 | |
f09985c2 | 164 | pathspec = get_pathspec(prefix, argv); |
4e1a7baa NTND |
165 | refresh_index(&the_index, REFRESH_QUIET, pathspec, NULL, NULL); |
166 | ||
d9b814cc | 167 | seen = NULL; |
7612a1ef JH |
168 | for (i = 0; pathspec[i] ; i++) |
169 | /* nothing */; | |
28f75818 | 170 | seen = xcalloc(i, 1); |
d9b814cc LT |
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; | |
834d9eb6 TF |
176 | ALLOC_GROW(list.name, list.nr + 1, list.alloc); |
177 | list.name[list.nr++] = ce->name; | |
d9b814cc LT |
178 | } |
179 | ||
180 | if (pathspec) { | |
181 | const char *match; | |
bb1faf0d | 182 | int seen_any = 0; |
d9b814cc | 183 | for (i = 0; (match = pathspec[i]) != NULL ; i++) { |
bb1faf0d SG |
184 | if (!seen[i]) { |
185 | if (!ignore_unmatch) { | |
b9b537f7 | 186 | die(_("pathspec '%s' did not match any files"), |
bb1faf0d SG |
187 | match); |
188 | } | |
189 | } | |
190 | else { | |
191 | seen_any = 1; | |
192 | } | |
9f95069b | 193 | if (!recursive && seen[i] == MATCHED_RECURSIVELY) |
b9b537f7 | 194 | die(_("not removing '%s' recursively without -r"), |
9f95069b | 195 | *match ? match : "."); |
d9b814cc | 196 | } |
bb1faf0d SG |
197 | |
198 | if (! seen_any) | |
199 | exit(0); | |
d9b814cc LT |
200 | } |
201 | ||
9f95069b JH |
202 | /* |
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 | * | |
9474eda6 | 207 | * rm F; git rm F |
9f95069b JH |
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); | |
bdecd9d4 | 216 | if (check_local_mod(sha1, index_only)) |
9f95069b JH |
217 | exit(1); |
218 | } | |
219 | ||
d9b814cc LT |
220 | /* |
221 | * First remove the names from the index: we won't commit | |
9f95069b | 222 | * the index unless all of them succeed. |
d9b814cc LT |
223 | */ |
224 | for (i = 0; i < list.nr; i++) { | |
225 | const char *path = list.name[i]; | |
b48caa20 SG |
226 | if (!quiet) |
227 | printf("rm '%s'\n", path); | |
d9b814cc LT |
228 | |
229 | if (remove_file_from_cache(path)) | |
b9b537f7 | 230 | die(_("git rm: unable to remove %s"), path); |
d9b814cc LT |
231 | } |
232 | ||
7612a1ef JH |
233 | if (show_only) |
234 | return 0; | |
235 | ||
d9b814cc | 236 | /* |
646ac22b | 237 | * Then, unless we used "--cached", remove the filenames from |
9f95069b | 238 | * the workspace. If we fail to remove the first one, we |
d9b814cc LT |
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: | |
82e5a82f | 241 | * by then we've already committed ourselves and can't fail |
d9b814cc LT |
242 | * in the middle) |
243 | */ | |
9f95069b | 244 | if (!index_only) { |
d9b814cc LT |
245 | int removed = 0; |
246 | for (i = 0; i < list.nr; i++) { | |
247 | const char *path = list.name[i]; | |
175a4948 | 248 | if (!remove_path(path)) { |
d9b814cc LT |
249 | removed = 1; |
250 | continue; | |
251 | } | |
252 | if (!removed) | |
d824cbba | 253 | die_errno("git rm: '%s'", path); |
d9b814cc LT |
254 | } |
255 | } | |
256 | ||
257 | if (active_cache_changed) { | |
258 | if (write_cache(newfd, active_cache, active_nr) || | |
4ed7cd3a | 259 | commit_locked_index(&lock_file)) |
b9b537f7 | 260 | die(_("Unable to write new index file")); |
d9b814cc LT |
261 | } |
262 | ||
263 | return 0; | |
264 | } |