]>
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 | ||
23 | static void add_list(const char *name) | |
24 | { | |
25 | if (list.nr >= list.alloc) { | |
26 | list.alloc = alloc_nr(list.alloc); | |
27 | list.name = xrealloc(list.name, list.alloc * sizeof(const char *)); | |
28 | } | |
29 | list.name[list.nr++] = name; | |
30 | } | |
31 | ||
bdecd9d4 | 32 | static int check_local_mod(unsigned char *head, int index_only) |
9f95069b JH |
33 | { |
34 | /* items in list are already sorted in the cache order, | |
35 | * so we could do this a lot more efficiently by using | |
36 | * tree_desc based traversal if we wanted to, but I am | |
37 | * lazy, and who cares if removal of files is a tad | |
38 | * slower than the theoretical maximum speed? | |
39 | */ | |
40 | int i, no_head; | |
41 | int errs = 0; | |
42 | ||
43 | no_head = is_null_sha1(head); | |
44 | for (i = 0; i < list.nr; i++) { | |
45 | struct stat st; | |
46 | int pos; | |
47 | struct cache_entry *ce; | |
48 | const char *name = list.name[i]; | |
49 | unsigned char sha1[20]; | |
50 | unsigned mode; | |
bdecd9d4 MM |
51 | int local_changes = 0; |
52 | int staged_changes = 0; | |
9f95069b JH |
53 | |
54 | pos = cache_name_pos(name, strlen(name)); | |
55 | if (pos < 0) | |
56 | continue; /* removing unmerged entry */ | |
57 | ce = active_cache[pos]; | |
58 | ||
59 | if (lstat(ce->name, &st) < 0) { | |
60 | if (errno != ENOENT) | |
61 | fprintf(stderr, "warning: '%s': %s", | |
62 | ce->name, strerror(errno)); | |
63 | /* It already vanished from the working tree */ | |
64 | continue; | |
65 | } | |
66 | else if (S_ISDIR(st.st_mode)) { | |
67 | /* if a file was removed and it is now a | |
68 | * directory, that is the same as ENOENT as | |
69 | * far as git is concerned; we do not track | |
70 | * directories. | |
71 | */ | |
72 | continue; | |
73 | } | |
74 | if (ce_match_stat(ce, &st, 0)) | |
bdecd9d4 | 75 | local_changes = 1; |
e4d9516b JK |
76 | if (no_head |
77 | || get_tree_entry(head, name, sha1, &mode) | |
78 | || ce->ce_mode != create_ce_mode(mode) | |
79 | || hashcmp(ce->sha1, sha1)) | |
bdecd9d4 MM |
80 | staged_changes = 1; |
81 | ||
f55527f8 JK |
82 | if (local_changes && staged_changes && |
83 | !(index_only && is_empty_blob_sha1(ce->sha1))) | |
bdecd9d4 MM |
84 | errs = error("'%s' has staged content different " |
85 | "from both the file and the HEAD\n" | |
86 | "(use -f to force removal)", name); | |
87 | else if (!index_only) { | |
f18d244a | 88 | /* It's not dangerous to "git rm --cached" a |
bdecd9d4 MM |
89 | * file if the index matches the file or the |
90 | * HEAD, since it means the deleted content is | |
91 | * still available somewhere. | |
92 | */ | |
93 | if (staged_changes) | |
94 | errs = error("'%s' has changes staged in the index\n" | |
95 | "(use --cached to keep the file, " | |
96 | "or -f to force removal)", name); | |
97 | if (local_changes) | |
98 | errs = error("'%s' has local modifications\n" | |
99 | "(use --cached to keep the file, " | |
100 | "or -f to force removal)", name); | |
101 | } | |
9f95069b JH |
102 | } |
103 | return errs; | |
104 | } | |
105 | ||
021b6e45 | 106 | static struct lock_file lock_file; |
d9b814cc | 107 | |
f09985c2 PH |
108 | static int show_only = 0, force = 0, index_only = 0, recursive = 0, quiet = 0; |
109 | static int ignore_unmatch = 0; | |
110 | ||
111 | static struct option builtin_rm_options[] = { | |
112 | OPT__DRY_RUN(&show_only), | |
113 | OPT__QUIET(&quiet), | |
114 | OPT_BOOLEAN( 0 , "cached", &index_only, "only remove from the index"), | |
01144f20 | 115 | OPT_BOOLEAN('f', "force", &force, "override the up-to-date check"), |
f09985c2 PH |
116 | OPT_BOOLEAN('r', NULL, &recursive, "allow recursive removal"), |
117 | OPT_BOOLEAN( 0 , "ignore-unmatch", &ignore_unmatch, | |
118 | "exit with a zero status even if nothing matched"), | |
119 | OPT_END(), | |
120 | }; | |
121 | ||
a633fca0 | 122 | int cmd_rm(int argc, const char **argv, const char *prefix) |
d9b814cc LT |
123 | { |
124 | int i, newfd; | |
d9b814cc LT |
125 | const char **pathspec; |
126 | char *seen; | |
127 | ||
ef90d6d4 | 128 | git_config(git_default_config, NULL); |
d9b814cc | 129 | |
f09985c2 PH |
130 | argc = parse_options(argc, argv, builtin_rm_options, builtin_rm_usage, 0); |
131 | if (!argc) | |
132 | usage_with_options(builtin_rm_usage, builtin_rm_options); | |
d9b814cc | 133 | |
271bb087 MH |
134 | if (!index_only) |
135 | setup_work_tree(); | |
136 | ||
4d264672 OM |
137 | newfd = hold_locked_index(&lock_file, 1); |
138 | ||
139 | if (read_cache() < 0) | |
140 | die("index file corrupt"); | |
cced48a8 | 141 | refresh_cache(REFRESH_QUIET); |
4d264672 | 142 | |
f09985c2 | 143 | pathspec = get_pathspec(prefix, argv); |
d9b814cc | 144 | seen = NULL; |
7612a1ef JH |
145 | for (i = 0; pathspec[i] ; i++) |
146 | /* nothing */; | |
28f75818 | 147 | seen = xcalloc(i, 1); |
d9b814cc LT |
148 | |
149 | for (i = 0; i < active_nr; i++) { | |
150 | struct cache_entry *ce = active_cache[i]; | |
151 | if (!match_pathspec(pathspec, ce->name, ce_namelen(ce), 0, seen)) | |
152 | continue; | |
153 | add_list(ce->name); | |
154 | } | |
155 | ||
156 | if (pathspec) { | |
157 | const char *match; | |
bb1faf0d | 158 | int seen_any = 0; |
d9b814cc | 159 | for (i = 0; (match = pathspec[i]) != NULL ; i++) { |
bb1faf0d SG |
160 | if (!seen[i]) { |
161 | if (!ignore_unmatch) { | |
162 | die("pathspec '%s' did not match any files", | |
163 | match); | |
164 | } | |
165 | } | |
166 | else { | |
167 | seen_any = 1; | |
168 | } | |
9f95069b JH |
169 | if (!recursive && seen[i] == MATCHED_RECURSIVELY) |
170 | die("not removing '%s' recursively without -r", | |
171 | *match ? match : "."); | |
d9b814cc | 172 | } |
bb1faf0d SG |
173 | |
174 | if (! seen_any) | |
175 | exit(0); | |
d9b814cc LT |
176 | } |
177 | ||
9f95069b JH |
178 | /* |
179 | * If not forced, the file, the index and the HEAD (if exists) | |
180 | * must match; but the file can already been removed, since | |
181 | * this sequence is a natural "novice" way: | |
182 | * | |
9474eda6 | 183 | * rm F; git rm F |
9f95069b JH |
184 | * |
185 | * Further, if HEAD commit exists, "diff-index --cached" must | |
186 | * report no changes unless forced. | |
187 | */ | |
188 | if (!force) { | |
189 | unsigned char sha1[20]; | |
190 | if (get_sha1("HEAD", sha1)) | |
191 | hashclr(sha1); | |
bdecd9d4 | 192 | if (check_local_mod(sha1, index_only)) |
9f95069b JH |
193 | exit(1); |
194 | } | |
195 | ||
d9b814cc LT |
196 | /* |
197 | * First remove the names from the index: we won't commit | |
9f95069b | 198 | * the index unless all of them succeed. |
d9b814cc LT |
199 | */ |
200 | for (i = 0; i < list.nr; i++) { | |
201 | const char *path = list.name[i]; | |
b48caa20 SG |
202 | if (!quiet) |
203 | printf("rm '%s'\n", path); | |
d9b814cc LT |
204 | |
205 | if (remove_file_from_cache(path)) | |
7e44c935 | 206 | die("git rm: unable to remove %s", path); |
d9b814cc LT |
207 | } |
208 | ||
7612a1ef JH |
209 | if (show_only) |
210 | return 0; | |
211 | ||
d9b814cc | 212 | /* |
646ac22b | 213 | * Then, unless we used "--cached", remove the filenames from |
9f95069b | 214 | * the workspace. If we fail to remove the first one, we |
d9b814cc LT |
215 | * abort the "git rm" (but once we've successfully removed |
216 | * any file at all, we'll go ahead and commit to it all: | |
82e5a82f | 217 | * by then we've already committed ourselves and can't fail |
d9b814cc LT |
218 | * in the middle) |
219 | */ | |
9f95069b | 220 | if (!index_only) { |
d9b814cc LT |
221 | int removed = 0; |
222 | for (i = 0; i < list.nr; i++) { | |
223 | const char *path = list.name[i]; | |
175a4948 | 224 | if (!remove_path(path)) { |
d9b814cc LT |
225 | removed = 1; |
226 | continue; | |
227 | } | |
228 | if (!removed) | |
7e44c935 | 229 | die("git rm: %s: %s", path, strerror(errno)); |
d9b814cc LT |
230 | } |
231 | } | |
232 | ||
233 | if (active_cache_changed) { | |
234 | if (write_cache(newfd, active_cache, active_nr) || | |
4ed7cd3a | 235 | commit_locked_index(&lock_file)) |
d9b814cc LT |
236 | die("Unable to write new index file"); |
237 | } | |
238 | ||
239 | return 0; | |
240 | } |