]>
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" |
d9b814cc LT |
11 | |
12 | static const char builtin_rm_usage[] = | |
bb1faf0d | 13 | "git-rm [-f] [-n] [-r] [--cached] [--ignore-unmatch] [--quiet] [--] <file>..."; |
d9b814cc LT |
14 | |
15 | static struct { | |
16 | int nr, alloc; | |
17 | const char **name; | |
18 | } list; | |
19 | ||
20 | static void add_list(const char *name) | |
21 | { | |
22 | if (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 | ||
29 | static int remove_file(const char *name) | |
30 | { | |
31 | int ret; | |
32 | char *slash; | |
33 | ||
34 | ret = unlink(name); | |
646ac22b JH |
35 | if (ret && errno == ENOENT) |
36 | /* The user has removed it from the filesystem by hand */ | |
37 | ret = errno = 0; | |
38 | ||
d9b814cc | 39 | if (!ret && (slash = strrchr(name, '/'))) { |
9befac47 | 40 | char *n = xstrdup(name); |
d9b814cc LT |
41 | do { |
42 | n[slash - name] = 0; | |
43 | name = n; | |
44 | } while (!rmdir(name) && (slash = strrchr(name, '/'))); | |
45 | } | |
46 | return ret; | |
47 | } | |
48 | ||
bdecd9d4 | 49 | static int check_local_mod(unsigned char *head, int index_only) |
9f95069b JH |
50 | { |
51 | /* items in list are already sorted in the cache order, | |
52 | * so we could do this a lot more efficiently by using | |
53 | * tree_desc based traversal if we wanted to, but I am | |
54 | * lazy, and who cares if removal of files is a tad | |
55 | * slower than the theoretical maximum speed? | |
56 | */ | |
57 | int i, no_head; | |
58 | int errs = 0; | |
59 | ||
60 | no_head = is_null_sha1(head); | |
61 | for (i = 0; i < list.nr; i++) { | |
62 | struct stat st; | |
63 | int pos; | |
64 | struct cache_entry *ce; | |
65 | const char *name = list.name[i]; | |
66 | unsigned char sha1[20]; | |
67 | unsigned mode; | |
bdecd9d4 MM |
68 | int local_changes = 0; |
69 | int staged_changes = 0; | |
9f95069b JH |
70 | |
71 | pos = cache_name_pos(name, strlen(name)); | |
72 | if (pos < 0) | |
73 | continue; /* removing unmerged entry */ | |
74 | ce = active_cache[pos]; | |
75 | ||
76 | if (lstat(ce->name, &st) < 0) { | |
77 | if (errno != ENOENT) | |
78 | fprintf(stderr, "warning: '%s': %s", | |
79 | ce->name, strerror(errno)); | |
80 | /* It already vanished from the working tree */ | |
81 | continue; | |
82 | } | |
83 | else if (S_ISDIR(st.st_mode)) { | |
84 | /* if a file was removed and it is now a | |
85 | * directory, that is the same as ENOENT as | |
86 | * far as git is concerned; we do not track | |
87 | * directories. | |
88 | */ | |
89 | continue; | |
90 | } | |
91 | if (ce_match_stat(ce, &st, 0)) | |
bdecd9d4 | 92 | local_changes = 1; |
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 | ||
99 | if (local_changes && staged_changes) | |
100 | errs = error("'%s' has staged content different " | |
101 | "from both the file and the HEAD\n" | |
102 | "(use -f to force removal)", name); | |
103 | else if (!index_only) { | |
104 | /* It's not dangerous to git-rm --cached a | |
105 | * file if the index matches the file or the | |
106 | * HEAD, since it means the deleted content is | |
107 | * still available somewhere. | |
108 | */ | |
109 | if (staged_changes) | |
110 | errs = error("'%s' has changes staged in the index\n" | |
111 | "(use --cached to keep the file, " | |
112 | "or -f to force removal)", name); | |
113 | if (local_changes) | |
114 | errs = error("'%s' has local modifications\n" | |
115 | "(use --cached to keep the file, " | |
116 | "or -f to force removal)", name); | |
117 | } | |
9f95069b JH |
118 | } |
119 | return errs; | |
120 | } | |
121 | ||
021b6e45 | 122 | static struct lock_file lock_file; |
d9b814cc | 123 | |
a633fca0 | 124 | int cmd_rm(int argc, const char **argv, const char *prefix) |
d9b814cc LT |
125 | { |
126 | int i, newfd; | |
b48caa20 | 127 | int show_only = 0, force = 0, index_only = 0, recursive = 0, quiet = 0; |
bb1faf0d | 128 | int ignore_unmatch = 0; |
d9b814cc LT |
129 | const char **pathspec; |
130 | char *seen; | |
131 | ||
132 | git_config(git_default_config); | |
133 | ||
30ca07a2 | 134 | newfd = hold_locked_index(&lock_file, 1); |
d9b814cc LT |
135 | |
136 | if (read_cache() < 0) | |
137 | die("index file corrupt"); | |
138 | ||
139 | for (i = 1 ; i < argc ; i++) { | |
140 | const char *arg = argv[i]; | |
141 | ||
142 | if (*arg != '-') | |
143 | break; | |
9f95069b | 144 | else if (!strcmp(arg, "--")) { |
d9b814cc LT |
145 | i++; |
146 | break; | |
147 | } | |
9f95069b | 148 | else if (!strcmp(arg, "-n")) |
d9b814cc | 149 | show_only = 1; |
9f95069b JH |
150 | else if (!strcmp(arg, "--cached")) |
151 | index_only = 1; | |
152 | else if (!strcmp(arg, "-f")) | |
d9b814cc | 153 | force = 1; |
9f95069b JH |
154 | else if (!strcmp(arg, "-r")) |
155 | recursive = 1; | |
b48caa20 SG |
156 | else if (!strcmp(arg, "--quiet")) |
157 | quiet = 1; | |
bb1faf0d SG |
158 | else if (!strcmp(arg, "--ignore-unmatch")) |
159 | ignore_unmatch = 1; | |
9f95069b JH |
160 | else |
161 | usage(builtin_rm_usage); | |
d9b814cc | 162 | } |
7612a1ef JH |
163 | if (argc <= i) |
164 | usage(builtin_rm_usage); | |
d9b814cc | 165 | |
7612a1ef | 166 | pathspec = get_pathspec(prefix, argv + i); |
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; | |
176 | add_list(ce->name); | |
177 | } | |
178 | ||
179 | if (pathspec) { | |
180 | const char *match; | |
bb1faf0d | 181 | int seen_any = 0; |
d9b814cc | 182 | for (i = 0; (match = pathspec[i]) != NULL ; i++) { |
bb1faf0d SG |
183 | if (!seen[i]) { |
184 | if (!ignore_unmatch) { | |
185 | die("pathspec '%s' did not match any files", | |
186 | match); | |
187 | } | |
188 | } | |
189 | else { | |
190 | seen_any = 1; | |
191 | } | |
9f95069b JH |
192 | if (!recursive && seen[i] == MATCHED_RECURSIVELY) |
193 | die("not removing '%s' recursively without -r", | |
194 | *match ? match : "."); | |
d9b814cc | 195 | } |
bb1faf0d SG |
196 | |
197 | if (! seen_any) | |
198 | exit(0); | |
d9b814cc LT |
199 | } |
200 | ||
9f95069b JH |
201 | /* |
202 | * If not forced, the file, the index and the HEAD (if exists) | |
203 | * must match; but the file can already been removed, since | |
204 | * this sequence is a natural "novice" way: | |
205 | * | |
9474eda6 | 206 | * rm F; git rm F |
9f95069b JH |
207 | * |
208 | * Further, if HEAD commit exists, "diff-index --cached" must | |
209 | * report no changes unless forced. | |
210 | */ | |
211 | if (!force) { | |
212 | unsigned char sha1[20]; | |
213 | if (get_sha1("HEAD", sha1)) | |
214 | hashclr(sha1); | |
bdecd9d4 | 215 | if (check_local_mod(sha1, index_only)) |
9f95069b JH |
216 | exit(1); |
217 | } | |
218 | ||
d9b814cc LT |
219 | /* |
220 | * First remove the names from the index: we won't commit | |
9f95069b | 221 | * the index unless all of them succeed. |
d9b814cc LT |
222 | */ |
223 | for (i = 0; i < list.nr; i++) { | |
224 | const char *path = list.name[i]; | |
b48caa20 SG |
225 | if (!quiet) |
226 | printf("rm '%s'\n", path); | |
d9b814cc LT |
227 | |
228 | if (remove_file_from_cache(path)) | |
cba05fa8 | 229 | die("git-rm: unable to remove %s", path); |
93872e07 | 230 | cache_tree_invalidate_path(active_cache_tree, 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]; | |
248 | if (!remove_file(path)) { | |
249 | removed = 1; | |
250 | continue; | |
251 | } | |
252 | if (!removed) | |
cba05fa8 | 253 | die("git-rm: %s: %s", path, strerror(errno)); |
d9b814cc LT |
254 | } |
255 | } | |
256 | ||
257 | if (active_cache_changed) { | |
258 | if (write_cache(newfd, active_cache, active_nr) || | |
30ca07a2 | 259 | close(newfd) || commit_locked_index(&lock_file)) |
d9b814cc LT |
260 | die("Unable to write new index file"); |
261 | } | |
262 | ||
263 | return 0; | |
264 | } |