7
#include "builtin.h"
8
#include "dir.h"
9
#include "cache-tree.h"
10
+#include "tree-walk.h"
11
12
static const char builtin_rm_usage[] =
12
-"git-rm [-n] [-v] [-f] <filepattern>...";
13
+"git-rm [-n] [-f] [--cached] <filepattern>...";
14
15
static struct {
16
int nr, alloc;
42
return ret;
43
}
44
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
+
108
static struct lock_file lock_file;
109
110
int cmd_rm(int argc, const char **argv, const char *prefix)
111
{
112
int i, newfd;
49
- int verbose = 0, show_only = 0, force = 0;
113
+ int show_only = 0, force = 0, index_only = 0, recursive = 0;
114
const char **pathspec;
115
char *seen;
116
126
127
if (*arg != '-')
128
break;
65
- if (!strcmp(arg, "--")) {
129
+ else if (!strcmp(arg, "--")) {
130
i++;
131
break;
132
}
69
- if (!strcmp(arg, "-n")) {
133
+ else if (!strcmp(arg, "-n"))
134
show_only = 1;
71
- continue;
72
- }
73
- if (!strcmp(arg, "-v")) {
74
- verbose = 1;
75
- continue;
76
- }
77
- if (!strcmp(arg, "-f")) {
135
+ else if (!strcmp(arg, "--cached"))
136
+ index_only = 1;
137
+ else if (!strcmp(arg, "-f"))
138
force = 1;
79
- continue;
80
- }
81
- usage(builtin_rm_usage);
139
+ else if (!strcmp(arg, "-r"))
140
+ recursive = 1;
141
+ else
142
+ usage(builtin_rm_usage);
143
}
144
if (argc <= i)
145
usage(builtin_rm_usage);
160
if (pathspec) {
161
const char *match;
162
for (i = 0; (match = pathspec[i]) != NULL ; i++) {
102
- if (*match && !seen[i])
103
- die("pathspec '%s' did not match any files", match);
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 : ".");
169
}
170
}
171
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
+
190
/*
191
* First remove the names from the index: we won't commit
109
- * the index unless all of them succeed
192
+ * the index unless all of them succeed.
193
*/
194
for (i = 0; i < list.nr; i++) {
195
const char *path = list.name[i];
204
return 0;
205
206
/*
124
- * Then, if we used "-f", remove the filenames from the
125
- * workspace. If we fail to remove the first one, we
207
+ * Then, unless we used "--cache", remove the filenames from
208
+ * the workspace. If we fail to remove the first one, we
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:
211
* by then we've already committed ourselves and can't fail
212
* in the middle)
213
*/
131
- if (force) {
214
+ if (!index_only) {
215
int removed = 0;
216
for (i = 0; i < list.nr; i++) {
217
const char *path = list.name[i];