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