]> git.ipfire.org Git - thirdparty/git.git/blame - update-cache.c
[PATCH] rename git-rpush and git-rpull to git-ssh-push and git-ssh-pull
[thirdparty/git.git] / update-cache.c
CommitLineData
8bc9a0c7
LT
1/*
2 * GIT - The information manager from hell
3 *
4 * Copyright (C) Linus Torvalds, 2005
5 */
e83c5163
LT
6#include "cache.h"
7
121481ab
LT
8/*
9 * Default to not allowing changes to the list of files. The
10 * tool doesn't actually care, but this makes it harder to add
11 * files to the revision control by mistake by doing something
667bb59b 12 * like "git-update-cache *" and suddenly having all the object
121481ab
LT
13 * files be revision controlled.
14 */
192268c1 15static int allow_add = 0, allow_remove = 0, allow_replace = 0, not_new = 0;
c6e007b0
JB
16
17/* Three functions to allow overloaded pointer return; see linux/err.h */
18static inline void *ERR_PTR(long error)
19{
20 return (void *) error;
21}
22
23static inline long PTR_ERR(const void *ptr)
24{
25 return (long) ptr;
26}
27
28static inline long IS_ERR(const void *ptr)
29{
30 return (unsigned long)ptr > (unsigned long)-1000L;
31}
121481ab 32
ee267527 33static int add_file_to_cache(char *path)
e83c5163 34{
4c5abf42 35 int size, namelen, option, status;
e83c5163
LT
36 struct cache_entry *ce;
37 struct stat st;
38 int fd;
ffbe1add 39 char *target;
e83c5163 40
4c5abf42
JH
41 status = lstat(path, &st);
42 if (status < 0 || S_ISDIR(st.st_mode)) {
43 /* When we used to have "path" and now we want to add
44 * "path/file", we need a way to remove "path" before
45 * being able to add "path/file". However,
46 * "git-update-cache --remove path" would not work.
47 * --force-remove can be used but this is more user
48 * friendly, especially since we can do the opposite
49 * case just fine without --force-remove.
50 */
51 if (status == 0 || (errno == ENOENT || errno == ENOTDIR)) {
121481ab
LT
52 if (allow_remove)
53 return remove_file_from_cache(path);
54 }
071c41a0 55 return error("open(\"%s\"): %s", path, strerror(errno));
e83c5163 56 }
e83c5163
LT
57 namelen = strlen(path);
58 size = cache_entry_size(namelen);
812666c8 59 ce = xmalloc(size);
e83c5163
LT
60 memset(ce, 0, size);
61 memcpy(ce->name, path, namelen);
711cf3a0 62 fill_stat_cache_info(ce, &st);
e4479470 63 ce->ce_mode = create_ce_mode(st.st_mode);
f5cabd13 64 ce->ce_flags = htons(namelen);
8ae0a8c5
KS
65 switch (st.st_mode & S_IFMT) {
66 case S_IFREG:
67 fd = open(path, O_RDONLY);
68 if (fd < 0)
69 return -1;
70 if (index_fd(ce->sha1, fd, &st) < 0)
71 return -1;
72 break;
73 case S_IFLNK:
ffbe1add
KS
74 target = xmalloc(st.st_size+1);
75 if (readlink(path, target, st.st_size+1) != st.st_size) {
76 free(target);
8ae0a8c5 77 return -1;
ffbe1add
KS
78 }
79 if (write_sha1_file(target, st.st_size, "blob", ce->sha1))
8ae0a8c5 80 return -1;
ffbe1add 81 free(target);
8ae0a8c5
KS
82 break;
83 default:
e83c5163 84 return -1;
8ae0a8c5 85 }
192268c1
JH
86 option = allow_add ? ADD_CACHE_OK_TO_ADD : 0;
87 option |= allow_replace ? ADD_CACHE_OK_TO_REPLACE : 0;
88 return add_cache_entry(ce, option);
121481ab
LT
89}
90
711cf3a0
LT
91static int match_data(int fd, void *buffer, unsigned long size)
92{
93 while (size) {
94 char compare[1024];
95 int ret = read(fd, compare, sizeof(compare));
96
97 if (ret <= 0 || ret > size || memcmp(buffer, compare, ret))
98 return -1;
99 size -= ret;
100 buffer += ret;
101 }
102 return 0;
103}
104
32d197f1 105static int compare_data(struct cache_entry *ce, unsigned long expected_size)
121481ab 106{
711cf3a0
LT
107 int match = -1;
108 int fd = open(ce->name, O_RDONLY);
109
110 if (fd >= 0) {
111 void *buffer;
112 unsigned long size;
cb1da3a7 113 char type[20];
711cf3a0
LT
114
115 buffer = read_sha1_file(ce->sha1, type, &size);
116 if (buffer) {
32d197f1 117 if (size == expected_size && !strcmp(type, "blob"))
711cf3a0
LT
118 match = match_data(fd, buffer, size);
119 free(buffer);
120 }
121 close(fd);
122 }
123 return match;
124}
125
ffbe1add
KS
126static int compare_link(struct cache_entry *ce, unsigned long expected_size)
127{
128 int match = -1;
129 char *target;
130 void *buffer;
131 unsigned long size;
132 char type[10];
133 int len;
134
135 target = xmalloc(expected_size);
136 len = readlink(ce->name, target, expected_size);
137 if (len != expected_size) {
138 free(target);
139 return -1;
140 }
141 buffer = read_sha1_file(ce->sha1, type, &size);
142 if (!buffer) {
143 free(target);
144 return -1;
145 }
146 if (size == expected_size)
147 match = memcmp(buffer, target, size);
148 free(buffer);
149 free(target);
150 return match;
151}
152
711cf3a0
LT
153/*
154 * "refresh" does not calculate a new sha1 file or bring the
155 * cache up-to-date for mode/content changes. But what it
156 * _does_ do is to "re-match" the stat information of a file
157 * with the cache, so that you can refresh the cache for a
158 * file that hasn't been changed but where the stat entry is
159 * out of date.
160 *
667bb59b 161 * For example, you'd want to do this after doing a "git-read-tree",
711cf3a0
LT
162 * to link up the stat cache details with the proper files.
163 */
164static struct cache_entry *refresh_entry(struct cache_entry *ce)
165{
166 struct stat st;
167 struct cache_entry *updated;
168 int changed, size;
169
8ae0a8c5 170 if (lstat(ce->name, &st) < 0)
c6e007b0 171 return ERR_PTR(-errno);
711cf3a0 172
5d728c84 173 changed = ce_match_stat(ce, &st);
711cf3a0
LT
174 if (!changed)
175 return ce;
176
121481ab 177 /*
8ae0a8c5 178 * If the mode or type has changed, there's no point in trying
711cf3a0 179 * to refresh the entry - it's not going to match
121481ab 180 */
8ae0a8c5 181 if (changed & (MODE_CHANGED | TYPE_CHANGED))
c6e007b0 182 return ERR_PTR(-EINVAL);
711cf3a0 183
ffbe1add
KS
184 switch (st.st_mode & S_IFMT) {
185 case S_IFREG:
186 if (compare_data(ce, st.st_size))
187 return ERR_PTR(-EINVAL);
188 break;
189 case S_IFLNK:
190 if (compare_link(ce, st.st_size))
191 return ERR_PTR(-EINVAL);
192 break;
193 default:
c6e007b0 194 return ERR_PTR(-EINVAL);
ffbe1add 195 }
711cf3a0
LT
196
197 size = ce_size(ce);
812666c8 198 updated = xmalloc(size);
711cf3a0
LT
199 memcpy(updated, ce, size);
200 fill_stat_cache_info(updated, &st);
201 return updated;
121481ab
LT
202}
203
90535218 204static int refresh_cache(void)
121481ab
LT
205{
206 int i;
90535218 207 int has_errors = 0;
121481ab 208
711cf3a0 209 for (i = 0; i < active_nr; i++) {
1bc992ac
JH
210 struct cache_entry *ce, *new;
211 ce = active_cache[i];
212 if (ce_stage(ce)) {
213 printf("%s: needs merge\n", ce->name);
90535218 214 has_errors = 1;
1bc992ac
JH
215 while ((i < active_nr) &&
216 ! strcmp(active_cache[i]->name, ce->name))
217 i++;
218 i--;
219 continue;
220 }
711cf3a0 221
1bc992ac 222 new = refresh_entry(ce);
c6e007b0 223 if (IS_ERR(new)) {
90535218 224 if (!(not_new && PTR_ERR(new) == -ENOENT)) {
c6e007b0 225 printf("%s: needs update\n", ce->name);
90535218
JH
226 has_errors = 1;
227 }
711cf3a0
LT
228 continue;
229 }
ee267527 230 active_cache_changed = 1;
62d046a0
PB
231 /* You can NOT just free active_cache[i] here, since it
232 * might not be necessarily malloc()ed but can also come
233 * from mmap(). */
711cf3a0
LT
234 active_cache[i] = new;
235 }
90535218 236 return has_errors;
e83c5163
LT
237}
238
e83c5163
LT
239/*
240 * We fundamentally don't like some paths: we don't want
320d3a1b
LT
241 * dot or dot-dot anywhere, and for obvious reasons don't
242 * want to recurse into ".git" either.
e83c5163
LT
243 *
244 * Also, we don't want double slashes or slashes at the
aebb2679 245 * end that can make pathnames ambiguous.
e83c5163 246 */
320d3a1b
LT
247static int verify_dotfile(const char *rest)
248{
249 /*
250 * The first character was '.', but that
251 * has already been discarded, we now test
252 * the rest.
253 */
254 switch (*rest) {
255 /* "." is not allowed */
256 case '\0': case '/':
257 return 0;
258
259 /*
260 * ".git" followed by NUL or slash is bad. This
261 * shares the path end test with the ".." case.
262 */
263 case 'g':
264 if (rest[1] != 'i')
265 break;
266 if (rest[2] != 't')
267 break;
268 rest += 2;
269 /* fallthrough */
270 case '.':
271 if (rest[1] == '\0' || rest[1] == '/')
272 return 0;
273 }
274 return 1;
275}
276
e83c5163
LT
277static int verify_path(char *path)
278{
279 char c;
280
281 goto inside;
282 for (;;) {
283 if (!c)
284 return 1;
285 if (c == '/') {
286inside:
287 c = *path++;
320d3a1b
LT
288 switch (c) {
289 default:
e83c5163 290 continue;
320d3a1b
LT
291 case '/': case '\0':
292 break;
293 case '.':
294 if (verify_dotfile(path))
295 continue;
296 }
e83c5163
LT
297 return 0;
298 }
299 c = *path++;
300 }
301}
302
9945d980
LT
303static int add_cacheinfo(char *arg1, char *arg2, char *arg3)
304{
192268c1 305 int size, len, option;
9945d980
LT
306 unsigned int mode;
307 unsigned char sha1[20];
308 struct cache_entry *ce;
309
310 if (sscanf(arg1, "%o", &mode) != 1)
311 return -1;
9945d980
LT
312 if (get_sha1_hex(arg2, sha1))
313 return -1;
9945d980
LT
314 if (!verify_path(arg3))
315 return -1;
9945d980
LT
316
317 len = strlen(arg3);
318 size = cache_entry_size(len);
812666c8 319 ce = xmalloc(size);
9945d980
LT
320 memset(ce, 0, size);
321
322 memcpy(ce->sha1, sha1, 20);
323 memcpy(ce->name, arg3, len);
f5cabd13 324 ce->ce_flags = htons(len);
e4479470 325 ce->ce_mode = create_ce_mode(mode);
192268c1
JH
326 option = allow_add ? ADD_CACHE_OK_TO_ADD : 0;
327 option |= allow_replace ? ADD_CACHE_OK_TO_REPLACE : 0;
328 return add_cache_entry(ce, option);
9945d980
LT
329}
330
e99d59ff 331static struct cache_file cache_file;
f2a19340 332
e83c5163
LT
333int main(int argc, char **argv)
334{
90535218 335 int i, newfd, entries, has_errors = 0;
121481ab 336 int allow_options = 1;
bb233d69 337
415e96c8 338 newfd = hold_index_file_for_update(&cache_file, get_index_file());
9614b8dc 339 if (newfd < 0)
2de381f9 340 die("unable to create new cachefile");
9614b8dc 341
e83c5163 342 entries = read_cache();
9614b8dc 343 if (entries < 0)
2de381f9 344 die("cache corrupted");
e83c5163 345
e83c5163
LT
346 for (i = 1 ; i < argc; i++) {
347 char *path = argv[i];
121481ab
LT
348
349 if (allow_options && *path == '-') {
350 if (!strcmp(path, "--")) {
351 allow_options = 0;
352 continue;
353 }
354 if (!strcmp(path, "--add")) {
355 allow_add = 1;
356 continue;
357 }
192268c1
JH
358 if (!strcmp(path, "--replace")) {
359 allow_replace = 1;
360 continue;
361 }
121481ab
LT
362 if (!strcmp(path, "--remove")) {
363 allow_remove = 1;
364 continue;
365 }
366 if (!strcmp(path, "--refresh")) {
90535218 367 has_errors |= refresh_cache();
121481ab
LT
368 continue;
369 }
9945d980 370 if (!strcmp(path, "--cacheinfo")) {
b3f94c4b 371 if (i+3 >= argc)
667bb59b 372 die("git-update-cache: --cacheinfo <mode> <sha1> <path>");
b3f94c4b 373 if (add_cacheinfo(argv[i+1], argv[i+2], argv[i+3]))
667bb59b 374 die("git-update-cache: --cacheinfo cannot add %s", argv[i+3]);
9945d980
LT
375 i += 3;
376 continue;
377 }
0ff5bf7c
JH
378 if (!strcmp(path, "--force-remove")) {
379 if (argc <= i + 1)
667bb59b 380 die("git-update-cache: --force-remove <path>");
0ff5bf7c 381 if (remove_file_from_cache(argv[i+1]))
667bb59b 382 die("git-update-cache: --force-remove cannot remove %s", argv[i+1]);
0ff5bf7c
JH
383 i++;
384 continue;
385 }
386
c6e007b0
JB
387 if (!strcmp(path, "--ignore-missing")) {
388 not_new = 1;
389 continue;
390 }
2de381f9 391 die("unknown option %s", path);
121481ab 392 }
e83c5163
LT
393 if (!verify_path(path)) {
394 fprintf(stderr, "Ignoring path %s\n", argv[i]);
395 continue;
396 }
9614b8dc 397 if (add_file_to_cache(path))
2de381f9 398 die("Unable to add %s to database", path);
e83c5163 399 }
415e96c8
JH
400 if (write_cache(newfd, active_cache, active_nr) ||
401 commit_index_file(&cache_file))
2de381f9 402 die("Unable to write new cachefile");
9614b8dc 403
c4b83e61 404 return has_errors ? 1 : 0;
e83c5163 405}