]> git.ipfire.org Git - thirdparty/git.git/blame - builtin-rm.c
Reuse fixup_pack_header_footer in index-pack
[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"
d9b814cc
LT
11
12static const char builtin_rm_usage[] =
bb1faf0d 13"git-rm [-f] [-n] [-r] [--cached] [--ignore-unmatch] [--quiet] [--] <file>...";
d9b814cc
LT
14
15static struct {
16 int nr, alloc;
17 const char **name;
18} list;
19
20static 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
29static 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
49static 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 102static struct lock_file lock_file;
d9b814cc 103
a633fca0 104int cmd_rm(int argc, const char **argv, const char *prefix)
d9b814cc
LT
105{
106 int i, newfd;
b48caa20 107 int show_only = 0, force = 0, index_only = 0, recursive = 0, quiet = 0;
bb1faf0d 108 int ignore_unmatch = 0;
d9b814cc
LT
109 const char **pathspec;
110 char *seen;
111
112 git_config(git_default_config);
113
30ca07a2 114 newfd = hold_locked_index(&lock_file, 1);
d9b814cc
LT
115
116 if (read_cache() < 0)
117 die("index file corrupt");
118
119 for (i = 1 ; i < argc ; i++) {
120 const char *arg = argv[i];
121
122 if (*arg != '-')
123 break;
9f95069b 124 else if (!strcmp(arg, "--")) {
d9b814cc
LT
125 i++;
126 break;
127 }
9f95069b 128 else if (!strcmp(arg, "-n"))
d9b814cc 129 show_only = 1;
9f95069b
JH
130 else if (!strcmp(arg, "--cached"))
131 index_only = 1;
132 else if (!strcmp(arg, "-f"))
d9b814cc 133 force = 1;
9f95069b
JH
134 else if (!strcmp(arg, "-r"))
135 recursive = 1;
b48caa20
SG
136 else if (!strcmp(arg, "--quiet"))
137 quiet = 1;
bb1faf0d
SG
138 else if (!strcmp(arg, "--ignore-unmatch"))
139 ignore_unmatch = 1;
9f95069b
JH
140 else
141 usage(builtin_rm_usage);
d9b814cc 142 }
7612a1ef
JH
143 if (argc <= i)
144 usage(builtin_rm_usage);
d9b814cc 145
7612a1ef 146 pathspec = get_pathspec(prefix, argv + i);
d9b814cc 147 seen = NULL;
7612a1ef
JH
148 for (i = 0; pathspec[i] ; i++)
149 /* nothing */;
28f75818 150 seen = xcalloc(i, 1);
d9b814cc
LT
151
152 for (i = 0; i < active_nr; i++) {
153 struct cache_entry *ce = active_cache[i];
154 if (!match_pathspec(pathspec, ce->name, ce_namelen(ce), 0, seen))
155 continue;
156 add_list(ce->name);
157 }
158
159 if (pathspec) {
160 const char *match;
bb1faf0d 161 int seen_any = 0;
d9b814cc 162 for (i = 0; (match = pathspec[i]) != NULL ; i++) {
bb1faf0d
SG
163 if (!seen[i]) {
164 if (!ignore_unmatch) {
165 die("pathspec '%s' did not match any files",
166 match);
167 }
168 }
169 else {
170 seen_any = 1;
171 }
9f95069b
JH
172 if (!recursive && seen[i] == MATCHED_RECURSIVELY)
173 die("not removing '%s' recursively without -r",
174 *match ? match : ".");
d9b814cc 175 }
bb1faf0d
SG
176
177 if (! seen_any)
178 exit(0);
d9b814cc
LT
179 }
180
9f95069b
JH
181 /*
182 * If not forced, the file, the index and the HEAD (if exists)
183 * must match; but the file can already been removed, since
184 * this sequence is a natural "novice" way:
185 *
9474eda6 186 * rm F; git rm F
9f95069b
JH
187 *
188 * Further, if HEAD commit exists, "diff-index --cached" must
189 * report no changes unless forced.
190 */
191 if (!force) {
192 unsigned char sha1[20];
193 if (get_sha1("HEAD", sha1))
194 hashclr(sha1);
195 if (check_local_mod(sha1))
196 exit(1);
197 }
198
d9b814cc
LT
199 /*
200 * First remove the names from the index: we won't commit
9f95069b 201 * the index unless all of them succeed.
d9b814cc
LT
202 */
203 for (i = 0; i < list.nr; i++) {
204 const char *path = list.name[i];
b48caa20
SG
205 if (!quiet)
206 printf("rm '%s'\n", path);
d9b814cc
LT
207
208 if (remove_file_from_cache(path))
cba05fa8 209 die("git-rm: unable to remove %s", path);
93872e07 210 cache_tree_invalidate_path(active_cache_tree, path);
d9b814cc
LT
211 }
212
7612a1ef
JH
213 if (show_only)
214 return 0;
215
d9b814cc 216 /*
646ac22b 217 * Then, unless we used "--cached", remove the filenames from
9f95069b 218 * the workspace. If we fail to remove the first one, we
d9b814cc
LT
219 * abort the "git rm" (but once we've successfully removed
220 * any file at all, we'll go ahead and commit to it all:
82e5a82f 221 * by then we've already committed ourselves and can't fail
d9b814cc
LT
222 * in the middle)
223 */
9f95069b 224 if (!index_only) {
d9b814cc
LT
225 int removed = 0;
226 for (i = 0; i < list.nr; i++) {
227 const char *path = list.name[i];
228 if (!remove_file(path)) {
229 removed = 1;
230 continue;
231 }
232 if (!removed)
cba05fa8 233 die("git-rm: %s: %s", path, strerror(errno));
d9b814cc
LT
234 }
235 }
236
237 if (active_cache_changed) {
238 if (write_cache(newfd, active_cache, active_nr) ||
30ca07a2 239 close(newfd) || commit_locked_index(&lock_file))
d9b814cc
LT
240 die("Unable to write new index file");
241 }
242
243 return 0;
244}