Steal -t option to git-ls-files from Cogito fork.
[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 */
f2a19340 6#include <signal.h>
e83c5163
LT
7#include "cache.h"
8
121481ab
LT
9/*
10 * Default to not allowing changes to the list of files. The
11 * tool doesn't actually care, but this makes it harder to add
12 * files to the revision control by mistake by doing something
13 * like "update-cache *" and suddenly having all the object
14 * files be revision controlled.
15 */
c6e007b0
JB
16static int allow_add = 0, allow_remove = 0, not_new = 0;
17
c4b83e61
JH
18/*
19 * update-cache --refresh may not touch anything at all, in which case
20 * writing 1.6MB of the same thing is a waste.
21 */
22static int cache_changed = 0;
23
c6e007b0
JB
24/* Three functions to allow overloaded pointer return; see linux/err.h */
25static inline void *ERR_PTR(long error)
26{
27 return (void *) error;
28}
29
30static inline long PTR_ERR(const void *ptr)
31{
32 return (long) ptr;
33}
34
35static inline long IS_ERR(const void *ptr)
36{
37 return (unsigned long)ptr > (unsigned long)-1000L;
38}
121481ab 39
711cf3a0
LT
40/*
41 * This only updates the "non-critical" parts of the directory
42 * cache, ie the parts that aren't tracked by GIT, and only used
43 * to validate the cache.
44 */
45static void fill_stat_cache_info(struct cache_entry *ce, struct stat *st)
46{
ccc4feb5
LT
47 ce->ce_ctime.sec = htonl(st->st_ctime);
48 ce->ce_mtime.sec = htonl(st->st_mtime);
5ade8628 49#ifdef NSEC
ccc4feb5
LT
50 ce->ce_ctime.nsec = htonl(st->st_ctim.tv_nsec);
51 ce->ce_mtime.nsec = htonl(st->st_mtim.tv_nsec);
5ade8628 52#endif
ccc4feb5
LT
53 ce->ce_dev = htonl(st->st_dev);
54 ce->ce_ino = htonl(st->st_ino);
55 ce->ce_uid = htonl(st->st_uid);
56 ce->ce_gid = htonl(st->st_gid);
57 ce->ce_size = htonl(st->st_size);
711cf3a0
LT
58}
59
c4b83e61 60static int add_file_to_cache_1(char *path)
e83c5163
LT
61{
62 int size, namelen;
63 struct cache_entry *ce;
64 struct stat st;
65 int fd;
8ae0a8c5
KS
66 unsigned int len;
67 char target[1024];
e83c5163 68
8ae0a8c5 69 if (lstat(path, &st) < 0) {
e2a669bb 70 if (errno == ENOENT || errno == ENOTDIR) {
121481ab
LT
71 if (allow_remove)
72 return remove_file_from_cache(path);
73 }
e83c5163
LT
74 return -1;
75 }
e83c5163
LT
76 namelen = strlen(path);
77 size = cache_entry_size(namelen);
812666c8 78 ce = xmalloc(size);
e83c5163
LT
79 memset(ce, 0, size);
80 memcpy(ce->name, path, namelen);
711cf3a0 81 fill_stat_cache_info(ce, &st);
e4479470 82 ce->ce_mode = create_ce_mode(st.st_mode);
f5cabd13 83 ce->ce_flags = htons(namelen);
8ae0a8c5
KS
84 switch (st.st_mode & S_IFMT) {
85 case S_IFREG:
86 fd = open(path, O_RDONLY);
87 if (fd < 0)
88 return -1;
89 if (index_fd(ce->sha1, fd, &st) < 0)
90 return -1;
91 break;
92 case S_IFLNK:
93 len = readlink(path, target, sizeof(target));
94 if (len == -1 || len+1 > sizeof(target))
95 return -1;
96 if (write_sha1_file(target, len, "blob", ce->sha1))
97 return -1;
98 break;
99 default:
e83c5163 100 return -1;
8ae0a8c5 101 }
c4b83e61
JH
102 if (!cache_changed) {
103 /* If we have not smudged the cache, be careful
104 * to keep it clean. Find out if we have a matching
105 * cache entry that add_cache_entry would replace with,
106 * and if it matches then do not bother calling it.
107 */
108 int pos = cache_name_pos(ce->name, namelen);
109 if ((0 <= pos) &&
110 !memcmp(active_cache[pos], ce, sizeof(*ce))) {
111 free(ce);
112 /* magic to tell add_file_to_cache that
113 * we have not updated anything.
114 */
115 return 999;
116 }
117 }
121481ab
LT
118 return add_cache_entry(ce, allow_add);
119}
120
c4b83e61
JH
121static int add_file_to_cache(char *path)
122{
123 int ret = add_file_to_cache_1(path);
124 if (ret == 0)
125 cache_changed = 1;
126 else if (ret == 999)
127 ret = 0;
128 return ret;
129}
130
711cf3a0
LT
131static int match_data(int fd, void *buffer, unsigned long size)
132{
133 while (size) {
134 char compare[1024];
135 int ret = read(fd, compare, sizeof(compare));
136
137 if (ret <= 0 || ret > size || memcmp(buffer, compare, ret))
138 return -1;
139 size -= ret;
140 buffer += ret;
141 }
142 return 0;
143}
144
32d197f1 145static int compare_data(struct cache_entry *ce, unsigned long expected_size)
121481ab 146{
711cf3a0
LT
147 int match = -1;
148 int fd = open(ce->name, O_RDONLY);
149
150 if (fd >= 0) {
151 void *buffer;
152 unsigned long size;
153 char type[10];
154
155 buffer = read_sha1_file(ce->sha1, type, &size);
156 if (buffer) {
32d197f1 157 if (size == expected_size && !strcmp(type, "blob"))
711cf3a0
LT
158 match = match_data(fd, buffer, size);
159 free(buffer);
160 }
161 close(fd);
162 }
163 return match;
164}
165
166/*
167 * "refresh" does not calculate a new sha1 file or bring the
168 * cache up-to-date for mode/content changes. But what it
169 * _does_ do is to "re-match" the stat information of a file
170 * with the cache, so that you can refresh the cache for a
171 * file that hasn't been changed but where the stat entry is
172 * out of date.
173 *
174 * For example, you'd want to do this after doing a "read-tree",
175 * to link up the stat cache details with the proper files.
176 */
177static struct cache_entry *refresh_entry(struct cache_entry *ce)
178{
179 struct stat st;
180 struct cache_entry *updated;
181 int changed, size;
182
8ae0a8c5 183 if (lstat(ce->name, &st) < 0)
c6e007b0 184 return ERR_PTR(-errno);
711cf3a0
LT
185
186 changed = cache_match_stat(ce, &st);
187 if (!changed)
188 return ce;
189
121481ab 190 /*
8ae0a8c5 191 * If the mode or type has changed, there's no point in trying
711cf3a0 192 * to refresh the entry - it's not going to match
121481ab 193 */
8ae0a8c5 194 if (changed & (MODE_CHANGED | TYPE_CHANGED))
c6e007b0 195 return ERR_PTR(-EINVAL);
711cf3a0 196
32d197f1 197 if (compare_data(ce, st.st_size))
c6e007b0 198 return ERR_PTR(-EINVAL);
711cf3a0 199
c4b83e61 200 cache_changed = 1;
711cf3a0 201 size = ce_size(ce);
812666c8 202 updated = xmalloc(size);
711cf3a0
LT
203 memcpy(updated, ce, size);
204 fill_stat_cache_info(updated, &st);
205 return updated;
121481ab
LT
206}
207
90535218 208static int refresh_cache(void)
121481ab
LT
209{
210 int i;
90535218 211 int has_errors = 0;
121481ab 212
711cf3a0 213 for (i = 0; i < active_nr; i++) {
1bc992ac
JH
214 struct cache_entry *ce, *new;
215 ce = active_cache[i];
216 if (ce_stage(ce)) {
217 printf("%s: needs merge\n", ce->name);
90535218 218 has_errors = 1;
1bc992ac
JH
219 while ((i < active_nr) &&
220 ! strcmp(active_cache[i]->name, ce->name))
221 i++;
222 i--;
223 continue;
224 }
711cf3a0 225
1bc992ac 226 new = refresh_entry(ce);
c6e007b0 227 if (IS_ERR(new)) {
90535218 228 if (!(not_new && PTR_ERR(new) == -ENOENT)) {
c6e007b0 229 printf("%s: needs update\n", ce->name);
90535218
JH
230 has_errors = 1;
231 }
711cf3a0
LT
232 continue;
233 }
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
241 * dot or dot-dot anywhere, and in fact, we don't even want
4bb04f21 242 * any other dot-files (.git or anything else). They
e83c5163
LT
243 * are hidden, for chist sake.
244 *
245 * Also, we don't want double slashes or slashes at the
aebb2679 246 * end that can make pathnames ambiguous.
e83c5163
LT
247 */
248static int verify_path(char *path)
249{
250 char c;
251
252 goto inside;
253 for (;;) {
254 if (!c)
255 return 1;
256 if (c == '/') {
257inside:
258 c = *path++;
259 if (c != '/' && c != '.' && c != '\0')
260 continue;
261 return 0;
262 }
263 c = *path++;
264 }
265}
266
9945d980
LT
267static int add_cacheinfo(char *arg1, char *arg2, char *arg3)
268{
269 int size, len;
270 unsigned int mode;
271 unsigned char sha1[20];
272 struct cache_entry *ce;
273
274 if (sscanf(arg1, "%o", &mode) != 1)
275 return -1;
9945d980
LT
276 if (get_sha1_hex(arg2, sha1))
277 return -1;
9945d980
LT
278 if (!verify_path(arg3))
279 return -1;
9945d980 280
c4b83e61 281 cache_changed = 1;
9945d980
LT
282 len = strlen(arg3);
283 size = cache_entry_size(len);
812666c8 284 ce = xmalloc(size);
9945d980
LT
285 memset(ce, 0, size);
286
287 memcpy(ce->sha1, sha1, 20);
288 memcpy(ce->name, arg3, len);
f5cabd13 289 ce->ce_flags = htons(len);
e4479470 290 ce->ce_mode = create_ce_mode(mode);
9945d980
LT
291 return add_cache_entry(ce, allow_add);
292}
293
bb233d69 294static const char *lockfile_name = NULL;
9614b8dc
LT
295
296static void remove_lock_file(void)
297{
bb233d69
LT
298 if (lockfile_name)
299 unlink(lockfile_name);
9614b8dc
LT
300}
301
f2a19340
LT
302static void remove_lock_file_on_signal(int signo)
303{
304 remove_lock_file();
305}
306
e83c5163
LT
307int main(int argc, char **argv)
308{
90535218 309 int i, newfd, entries, has_errors = 0;
121481ab 310 int allow_options = 1;
bb233d69
LT
311 static char lockfile[MAXPATHLEN+1];
312 const char *indexfile = get_index_file();
e83c5163 313
bb233d69
LT
314 snprintf(lockfile, sizeof(lockfile), "%s.lock", indexfile);
315
316 newfd = open(lockfile, O_RDWR | O_CREAT | O_EXCL, 0600);
9614b8dc 317 if (newfd < 0)
2de381f9 318 die("unable to create new cachefile");
9614b8dc 319
f2a19340 320 signal(SIGINT, remove_lock_file_on_signal);
9614b8dc 321 atexit(remove_lock_file);
bb233d69 322 lockfile_name = lockfile;
9614b8dc 323
e83c5163 324 entries = read_cache();
9614b8dc 325 if (entries < 0)
2de381f9 326 die("cache corrupted");
e83c5163 327
e83c5163
LT
328 for (i = 1 ; i < argc; i++) {
329 char *path = argv[i];
121481ab
LT
330
331 if (allow_options && *path == '-') {
332 if (!strcmp(path, "--")) {
333 allow_options = 0;
334 continue;
335 }
336 if (!strcmp(path, "--add")) {
337 allow_add = 1;
338 continue;
339 }
340 if (!strcmp(path, "--remove")) {
341 allow_remove = 1;
342 continue;
343 }
344 if (!strcmp(path, "--refresh")) {
90535218 345 has_errors |= refresh_cache();
121481ab
LT
346 continue;
347 }
9945d980
LT
348 if (!strcmp(path, "--cacheinfo")) {
349 if (i+3 >= argc || add_cacheinfo(argv[i+1], argv[i+2], argv[i+3]))
350 die("update-cache: --cacheinfo <mode> <sha1> <path>");
351 i += 3;
352 continue;
353 }
0ff5bf7c
JH
354 if (!strcmp(path, "--force-remove")) {
355 if (argc <= i + 1)
356 die("update-cache: --force-remove <path>");
357 if (remove_file_from_cache(argv[i+1]))
358 die("update-cache: --force-remove cannot remove %s", argv[i+1]);
359 i++;
360 continue;
361 }
362
c6e007b0
JB
363 if (!strcmp(path, "--ignore-missing")) {
364 not_new = 1;
365 continue;
366 }
2de381f9 367 die("unknown option %s", path);
121481ab 368 }
e83c5163
LT
369 if (!verify_path(path)) {
370 fprintf(stderr, "Ignoring path %s\n", argv[i]);
371 continue;
372 }
9614b8dc 373 if (add_file_to_cache(path))
2de381f9 374 die("Unable to add %s to database", path);
e83c5163 375 }
c4b83e61
JH
376
377 if (!cache_changed)
378 unlink(lockfile);
379 else if (write_cache(newfd, active_cache, active_nr) ||
380 rename(lockfile, indexfile))
2de381f9 381 die("Unable to write new cachefile");
9614b8dc 382
bb233d69 383 lockfile_name = NULL;
c4b83e61 384 return has_errors ? 1 : 0;
e83c5163 385}