]> git.ipfire.org Git - thirdparty/git.git/blame - builtin-rm.c
On Solaris choose the OLD_ICONV iconv() declaration based on the UNIX spec
[thirdparty/git.git] / builtin-rm.c
CommitLineData
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 13static const char * const builtin_rm_usage[] = {
1b1dd23f 14 "git rm [options] [--] <file>...",
f09985c2
PH
15 NULL
16};
d9b814cc
LT
17
18static struct {
19 int nr, alloc;
20 const char **name;
21} list;
22
23static 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 32static int check_local_mod(unsigned char *head, int index_only)
9f95069b 33{
69530cb0
JH
34 /*
35 * Items in list are already sorted in the cache order,
9f95069b
JH
36 * so we could do this a lot more efficiently by using
37 * tree_desc based traversal if we wanted to, but I am
38 * lazy, and who cares if removal of files is a tad
39 * slower than the theoretical maximum speed?
40 */
41 int i, no_head;
42 int errs = 0;
43
44 no_head = is_null_sha1(head);
45 for (i = 0; i < list.nr; i++) {
46 struct stat st;
47 int pos;
48 struct cache_entry *ce;
49 const char *name = list.name[i];
50 unsigned char sha1[20];
51 unsigned mode;
bdecd9d4
MM
52 int local_changes = 0;
53 int staged_changes = 0;
9f95069b
JH
54
55 pos = cache_name_pos(name, strlen(name));
56 if (pos < 0)
57 continue; /* removing unmerged entry */
58 ce = active_cache[pos];
59
60 if (lstat(ce->name, &st) < 0) {
61 if (errno != ENOENT)
c36d785d 62 warning("'%s': %s", ce->name, strerror(errno));
9f95069b
JH
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 }
69530cb0
JH
74
75 /*
76 * "rm" of a path that has changes need to be treated
77 * carefully not to allow losing local changes
78 * accidentally. A local change could be (1) file in
79 * work tree is different since the index; and/or (2)
80 * the user staged a content that is different from
81 * the current commit in the index.
82 *
83 * In such a case, you would need to --force the
84 * removal. However, "rm --cached" (remove only from
85 * the index) is safe if the index matches the file in
86 * the work tree or the HEAD commit, as it means that
87 * the content being removed is available elsewhere.
88 */
89
90 /*
91 * Is the index different from the file in the work tree?
92 */
9f95069b 93 if (ce_match_stat(ce, &st, 0))
bdecd9d4 94 local_changes = 1;
69530cb0
JH
95
96 /*
97 * Is the index different from the HEAD commit? By
98 * definition, before the very initial commit,
99 * anything staged in the index is treated by the same
100 * way as changed from the HEAD.
101 */
e4d9516b
JK
102 if (no_head
103 || get_tree_entry(head, name, sha1, &mode)
104 || ce->ce_mode != create_ce_mode(mode)
105 || hashcmp(ce->sha1, sha1))
bdecd9d4
MM
106 staged_changes = 1;
107
69530cb0
JH
108 /*
109 * If the index does not match the file in the work
110 * tree and if it does not match the HEAD commit
111 * either, (1) "git rm" without --cached definitely
112 * will lose information; (2) "git rm --cached" will
113 * lose information unless it is about removing an
114 * "intent to add" entry.
115 */
116 if (local_changes && staged_changes) {
388b2acd 117 if (!index_only || !(ce->ce_flags & CE_INTENT_TO_ADD))
69530cb0
JH
118 errs = error("'%s' has staged content different "
119 "from both the file and the HEAD\n"
120 "(use -f to force removal)", name);
121 }
bdecd9d4 122 else if (!index_only) {
bdecd9d4
MM
123 if (staged_changes)
124 errs = error("'%s' has changes staged in the index\n"
125 "(use --cached to keep the file, "
126 "or -f to force removal)", name);
127 if (local_changes)
128 errs = error("'%s' has local modifications\n"
129 "(use --cached to keep the file, "
130 "or -f to force removal)", name);
131 }
9f95069b
JH
132 }
133 return errs;
134}
135
021b6e45 136static struct lock_file lock_file;
d9b814cc 137
f09985c2
PH
138static int show_only = 0, force = 0, index_only = 0, recursive = 0, quiet = 0;
139static int ignore_unmatch = 0;
140
141static struct option builtin_rm_options[] = {
142 OPT__DRY_RUN(&show_only),
143 OPT__QUIET(&quiet),
144 OPT_BOOLEAN( 0 , "cached", &index_only, "only remove from the index"),
01144f20 145 OPT_BOOLEAN('f', "force", &force, "override the up-to-date check"),
f09985c2
PH
146 OPT_BOOLEAN('r', NULL, &recursive, "allow recursive removal"),
147 OPT_BOOLEAN( 0 , "ignore-unmatch", &ignore_unmatch,
148 "exit with a zero status even if nothing matched"),
149 OPT_END(),
150};
151
a633fca0 152int cmd_rm(int argc, const char **argv, const char *prefix)
d9b814cc
LT
153{
154 int i, newfd;
d9b814cc
LT
155 const char **pathspec;
156 char *seen;
157
ef90d6d4 158 git_config(git_default_config, NULL);
d9b814cc 159
f09985c2
PH
160 argc = parse_options(argc, argv, builtin_rm_options, builtin_rm_usage, 0);
161 if (!argc)
162 usage_with_options(builtin_rm_usage, builtin_rm_options);
d9b814cc 163
271bb087
MH
164 if (!index_only)
165 setup_work_tree();
166
4d264672
OM
167 newfd = hold_locked_index(&lock_file, 1);
168
169 if (read_cache() < 0)
170 die("index file corrupt");
cced48a8 171 refresh_cache(REFRESH_QUIET);
4d264672 172
f09985c2 173 pathspec = get_pathspec(prefix, argv);
d9b814cc 174 seen = NULL;
7612a1ef
JH
175 for (i = 0; pathspec[i] ; i++)
176 /* nothing */;
28f75818 177 seen = xcalloc(i, 1);
d9b814cc
LT
178
179 for (i = 0; i < active_nr; i++) {
180 struct cache_entry *ce = active_cache[i];
181 if (!match_pathspec(pathspec, ce->name, ce_namelen(ce), 0, seen))
182 continue;
183 add_list(ce->name);
184 }
185
186 if (pathspec) {
187 const char *match;
bb1faf0d 188 int seen_any = 0;
d9b814cc 189 for (i = 0; (match = pathspec[i]) != NULL ; i++) {
bb1faf0d
SG
190 if (!seen[i]) {
191 if (!ignore_unmatch) {
192 die("pathspec '%s' did not match any files",
193 match);
194 }
195 }
196 else {
197 seen_any = 1;
198 }
9f95069b
JH
199 if (!recursive && seen[i] == MATCHED_RECURSIVELY)
200 die("not removing '%s' recursively without -r",
201 *match ? match : ".");
d9b814cc 202 }
bb1faf0d
SG
203
204 if (! seen_any)
205 exit(0);
d9b814cc
LT
206 }
207
9f95069b
JH
208 /*
209 * If not forced, the file, the index and the HEAD (if exists)
210 * must match; but the file can already been removed, since
211 * this sequence is a natural "novice" way:
212 *
9474eda6 213 * rm F; git rm F
9f95069b
JH
214 *
215 * Further, if HEAD commit exists, "diff-index --cached" must
216 * report no changes unless forced.
217 */
218 if (!force) {
219 unsigned char sha1[20];
220 if (get_sha1("HEAD", sha1))
221 hashclr(sha1);
bdecd9d4 222 if (check_local_mod(sha1, index_only))
9f95069b
JH
223 exit(1);
224 }
225
d9b814cc
LT
226 /*
227 * First remove the names from the index: we won't commit
9f95069b 228 * the index unless all of them succeed.
d9b814cc
LT
229 */
230 for (i = 0; i < list.nr; i++) {
231 const char *path = list.name[i];
b48caa20
SG
232 if (!quiet)
233 printf("rm '%s'\n", path);
d9b814cc
LT
234
235 if (remove_file_from_cache(path))
7e44c935 236 die("git rm: unable to remove %s", path);
d9b814cc
LT
237 }
238
7612a1ef
JH
239 if (show_only)
240 return 0;
241
d9b814cc 242 /*
646ac22b 243 * Then, unless we used "--cached", remove the filenames from
9f95069b 244 * the workspace. If we fail to remove the first one, we
d9b814cc
LT
245 * abort the "git rm" (but once we've successfully removed
246 * any file at all, we'll go ahead and commit to it all:
82e5a82f 247 * by then we've already committed ourselves and can't fail
d9b814cc
LT
248 * in the middle)
249 */
9f95069b 250 if (!index_only) {
d9b814cc
LT
251 int removed = 0;
252 for (i = 0; i < list.nr; i++) {
253 const char *path = list.name[i];
175a4948 254 if (!remove_path(path)) {
d9b814cc
LT
255 removed = 1;
256 continue;
257 }
258 if (!removed)
7e44c935 259 die("git rm: %s: %s", path, strerror(errno));
d9b814cc
LT
260 }
261 }
262
263 if (active_cache_changed) {
264 if (write_cache(newfd, active_cache, active_nr) ||
4ed7cd3a 265 commit_locked_index(&lock_file))
d9b814cc
LT
266 die("Unable to write new index file");
267 }
268
269 return 0;
270}