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