]>
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[] = | |
a9877f83 | 13 | "git-rm [-f] [-n] [-r] [--cached] [--] <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 | ||
9f95069b JH |
49 | static int check_local_mod(unsigned char *head) |
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; | |
68 | ||
69 | pos = cache_name_pos(name, strlen(name)); | |
70 | if (pos < 0) | |
71 | continue; /* removing unmerged entry */ | |
72 | ce = active_cache[pos]; | |
73 | ||
74 | if (lstat(ce->name, &st) < 0) { | |
75 | if (errno != ENOENT) | |
76 | fprintf(stderr, "warning: '%s': %s", | |
77 | ce->name, strerror(errno)); | |
78 | /* It already vanished from the working tree */ | |
79 | continue; | |
80 | } | |
81 | else if (S_ISDIR(st.st_mode)) { | |
82 | /* if a file was removed and it is now a | |
83 | * directory, that is the same as ENOENT as | |
84 | * far as git is concerned; we do not track | |
85 | * directories. | |
86 | */ | |
87 | continue; | |
88 | } | |
89 | if (ce_match_stat(ce, &st, 0)) | |
90 | errs = error("'%s' has local modifications " | |
91 | "(hint: try -f)", ce->name); | |
92 | if (no_head) | |
93 | continue; | |
94 | /* | |
95 | * It is Ok to remove a newly added path, as long as | |
96 | * it is cache-clean. | |
97 | */ | |
98 | if (get_tree_entry(head, name, sha1, &mode)) | |
99 | continue; | |
100 | /* | |
101 | * Otherwise make sure the version from the HEAD | |
102 | * matches the index. | |
103 | */ | |
104 | if (ce->ce_mode != create_ce_mode(mode) || | |
105 | hashcmp(ce->sha1, sha1)) | |
106 | errs = error("'%s' has changes staged in the index " | |
107 | "(hint: try -f)", name); | |
108 | } | |
109 | return errs; | |
110 | } | |
111 | ||
021b6e45 | 112 | static struct lock_file lock_file; |
d9b814cc | 113 | |
a633fca0 | 114 | int cmd_rm(int argc, const char **argv, const char *prefix) |
d9b814cc LT |
115 | { |
116 | int i, newfd; | |
9f95069b | 117 | int show_only = 0, force = 0, index_only = 0, recursive = 0; |
d9b814cc LT |
118 | const char **pathspec; |
119 | char *seen; | |
120 | ||
121 | git_config(git_default_config); | |
122 | ||
40aaae88 | 123 | newfd = hold_lock_file_for_update(&lock_file, get_index_file(), 1); |
d9b814cc LT |
124 | |
125 | if (read_cache() < 0) | |
126 | die("index file corrupt"); | |
127 | ||
128 | for (i = 1 ; i < argc ; i++) { | |
129 | const char *arg = argv[i]; | |
130 | ||
131 | if (*arg != '-') | |
132 | break; | |
9f95069b | 133 | else if (!strcmp(arg, "--")) { |
d9b814cc LT |
134 | i++; |
135 | break; | |
136 | } | |
9f95069b | 137 | else if (!strcmp(arg, "-n")) |
d9b814cc | 138 | show_only = 1; |
9f95069b JH |
139 | else if (!strcmp(arg, "--cached")) |
140 | index_only = 1; | |
141 | else if (!strcmp(arg, "-f")) | |
d9b814cc | 142 | force = 1; |
9f95069b JH |
143 | else if (!strcmp(arg, "-r")) |
144 | recursive = 1; | |
145 | else | |
146 | usage(builtin_rm_usage); | |
d9b814cc | 147 | } |
7612a1ef JH |
148 | if (argc <= i) |
149 | usage(builtin_rm_usage); | |
d9b814cc | 150 | |
7612a1ef | 151 | pathspec = get_pathspec(prefix, argv + i); |
d9b814cc | 152 | seen = NULL; |
7612a1ef JH |
153 | for (i = 0; pathspec[i] ; i++) |
154 | /* nothing */; | |
28f75818 | 155 | seen = xcalloc(i, 1); |
d9b814cc LT |
156 | |
157 | for (i = 0; i < active_nr; i++) { | |
158 | struct cache_entry *ce = active_cache[i]; | |
159 | if (!match_pathspec(pathspec, ce->name, ce_namelen(ce), 0, seen)) | |
160 | continue; | |
161 | add_list(ce->name); | |
162 | } | |
163 | ||
164 | if (pathspec) { | |
165 | const char *match; | |
166 | for (i = 0; (match = pathspec[i]) != NULL ; i++) { | |
9f95069b JH |
167 | if (!seen[i]) |
168 | die("pathspec '%s' did not match any files", | |
169 | match); | |
170 | if (!recursive && seen[i] == MATCHED_RECURSIVELY) | |
171 | die("not removing '%s' recursively without -r", | |
172 | *match ? match : "."); | |
d9b814cc LT |
173 | } |
174 | } | |
175 | ||
9f95069b JH |
176 | /* |
177 | * If not forced, the file, the index and the HEAD (if exists) | |
178 | * must match; but the file can already been removed, since | |
179 | * this sequence is a natural "novice" way: | |
180 | * | |
181 | * rm F; git fm F | |
182 | * | |
183 | * Further, if HEAD commit exists, "diff-index --cached" must | |
184 | * report no changes unless forced. | |
185 | */ | |
186 | if (!force) { | |
187 | unsigned char sha1[20]; | |
188 | if (get_sha1("HEAD", sha1)) | |
189 | hashclr(sha1); | |
190 | if (check_local_mod(sha1)) | |
191 | exit(1); | |
192 | } | |
193 | ||
d9b814cc LT |
194 | /* |
195 | * First remove the names from the index: we won't commit | |
9f95069b | 196 | * the index unless all of them succeed. |
d9b814cc LT |
197 | */ |
198 | for (i = 0; i < list.nr; i++) { | |
199 | const char *path = list.name[i]; | |
200 | printf("rm '%s'\n", path); | |
201 | ||
202 | if (remove_file_from_cache(path)) | |
cba05fa8 | 203 | die("git-rm: unable to remove %s", path); |
93872e07 | 204 | cache_tree_invalidate_path(active_cache_tree, path); |
d9b814cc LT |
205 | } |
206 | ||
7612a1ef JH |
207 | if (show_only) |
208 | return 0; | |
209 | ||
d9b814cc | 210 | /* |
646ac22b | 211 | * Then, unless we used "--cached", remove the filenames from |
9f95069b | 212 | * the workspace. If we fail to remove the first one, we |
d9b814cc LT |
213 | * abort the "git rm" (but once we've successfully removed |
214 | * any file at all, we'll go ahead and commit to it all: | |
82e5a82f | 215 | * by then we've already committed ourselves and can't fail |
d9b814cc LT |
216 | * in the middle) |
217 | */ | |
9f95069b | 218 | if (!index_only) { |
d9b814cc LT |
219 | int removed = 0; |
220 | for (i = 0; i < list.nr; i++) { | |
221 | const char *path = list.name[i]; | |
222 | if (!remove_file(path)) { | |
223 | removed = 1; | |
224 | continue; | |
225 | } | |
226 | if (!removed) | |
cba05fa8 | 227 | die("git-rm: %s: %s", path, strerror(errno)); |
d9b814cc LT |
228 | } |
229 | } | |
230 | ||
231 | if (active_cache_changed) { | |
232 | if (write_cache(newfd, active_cache, active_nr) || | |
6244b249 | 233 | close(newfd) || commit_lock_file(&lock_file)) |
d9b814cc LT |
234 | die("Unable to write new index file"); |
235 | } | |
236 | ||
237 | return 0; | |
238 | } |