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