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