2 * GIT - The information manager from hell
4 * Copyright (C) Linus Torvalds, 2005
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.
16 static int allow_add = 0, allow_remove = 0, allow_replace = 0, not_new = 0;
18 /* Three functions to allow overloaded pointer return; see linux/err.h */
19 static inline void *ERR_PTR(long error)
21 return (void *) error;
24 static inline long PTR_ERR(const void *ptr)
29 static inline long IS_ERR(const void *ptr)
31 return (unsigned long)ptr > (unsigned long)-1000L;
35 * This only updates the "non-critical" parts of the directory
36 * cache, ie the parts that aren't tracked by GIT, and only used
37 * to validate the cache.
39 static void fill_stat_cache_info(struct cache_entry *ce, struct stat *st)
41 ce->ce_ctime.sec = htonl(st->st_ctime);
42 ce->ce_mtime.sec = htonl(st->st_mtime);
44 ce->ce_ctime.nsec = htonl(st->st_ctim.tv_nsec);
45 ce->ce_mtime.nsec = htonl(st->st_mtim.tv_nsec);
47 ce->ce_dev = htonl(st->st_dev);
48 ce->ce_ino = htonl(st->st_ino);
49 ce->ce_uid = htonl(st->st_uid);
50 ce->ce_gid = htonl(st->st_gid);
51 ce->ce_size = htonl(st->st_size);
54 static int add_file_to_cache(char *path)
56 int size, namelen, option;
57 struct cache_entry *ce;
62 if (lstat(path, &st) < 0) {
63 if (errno == ENOENT || errno == ENOTDIR) {
65 return remove_file_from_cache(path);
69 namelen = strlen(path);
70 size = cache_entry_size(namelen);
73 memcpy(ce->name, path, namelen);
74 fill_stat_cache_info(ce, &st);
75 ce->ce_mode = create_ce_mode(st.st_mode);
76 ce->ce_flags = htons(namelen);
77 switch (st.st_mode & S_IFMT) {
79 fd = open(path, O_RDONLY);
82 if (index_fd(ce->sha1, fd, &st) < 0)
86 target = xmalloc(st.st_size+1);
87 if (readlink(path, target, st.st_size+1) != st.st_size) {
91 if (write_sha1_file(target, st.st_size, "blob", ce->sha1))
98 option = allow_add ? ADD_CACHE_OK_TO_ADD : 0;
99 option |= allow_replace ? ADD_CACHE_OK_TO_REPLACE : 0;
100 return add_cache_entry(ce, option);
103 static int match_data(int fd, void *buffer, unsigned long size)
107 int ret = read(fd, compare, sizeof(compare));
109 if (ret <= 0 || ret > size || memcmp(buffer, compare, ret))
117 static int compare_data(struct cache_entry *ce, unsigned long expected_size)
120 int fd = open(ce->name, O_RDONLY);
127 buffer = read_sha1_file(ce->sha1, type, &size);
129 if (size == expected_size && !strcmp(type, "blob"))
130 match = match_data(fd, buffer, size);
138 static int compare_link(struct cache_entry *ce, unsigned long expected_size)
147 target = xmalloc(expected_size);
148 len = readlink(ce->name, target, expected_size);
149 if (len != expected_size) {
153 buffer = read_sha1_file(ce->sha1, type, &size);
158 if (size == expected_size)
159 match = memcmp(buffer, target, size);
166 * "refresh" does not calculate a new sha1 file or bring the
167 * cache up-to-date for mode/content changes. But what it
168 * _does_ do is to "re-match" the stat information of a file
169 * with the cache, so that you can refresh the cache for a
170 * file that hasn't been changed but where the stat entry is
173 * For example, you'd want to do this after doing a "read-tree",
174 * to link up the stat cache details with the proper files.
176 static struct cache_entry *refresh_entry(struct cache_entry *ce)
179 struct cache_entry *updated;
182 if (lstat(ce->name, &st) < 0)
183 return ERR_PTR(-errno);
185 changed = cache_match_stat(ce, &st);
190 * If the mode or type has changed, there's no point in trying
191 * to refresh the entry - it's not going to match
193 if (changed & (MODE_CHANGED | TYPE_CHANGED))
194 return ERR_PTR(-EINVAL);
196 switch (st.st_mode & S_IFMT) {
198 if (compare_data(ce, st.st_size))
199 return ERR_PTR(-EINVAL);
202 if (compare_link(ce, st.st_size))
203 return ERR_PTR(-EINVAL);
206 return ERR_PTR(-EINVAL);
210 updated = xmalloc(size);
211 memcpy(updated, ce, size);
212 fill_stat_cache_info(updated, &st);
216 static int refresh_cache(void)
221 for (i = 0; i < active_nr; i++) {
222 struct cache_entry *ce, *new;
223 ce = active_cache[i];
225 printf("%s: needs merge\n", ce->name);
227 while ((i < active_nr) &&
228 ! strcmp(active_cache[i]->name, ce->name))
234 new = refresh_entry(ce);
236 if (!(not_new && PTR_ERR(new) == -ENOENT)) {
237 printf("%s: needs update\n", ce->name);
242 active_cache_changed = 1;
243 active_cache[i] = new;
249 * We fundamentally don't like some paths: we don't want
250 * dot or dot-dot anywhere, and in fact, we don't even want
251 * any other dot-files (.git or anything else). They
252 * are hidden, for chist sake.
254 * Also, we don't want double slashes or slashes at the
255 * end that can make pathnames ambiguous.
257 static int verify_path(char *path)
268 if (c != '/' && c != '.' && c != '\0')
276 static int add_cacheinfo(char *arg1, char *arg2, char *arg3)
278 int size, len, option;
280 unsigned char sha1[20];
281 struct cache_entry *ce;
283 if (sscanf(arg1, "%o", &mode) != 1)
285 if (get_sha1_hex(arg2, sha1))
287 if (!verify_path(arg3))
291 size = cache_entry_size(len);
295 memcpy(ce->sha1, sha1, 20);
296 memcpy(ce->name, arg3, len);
297 ce->ce_flags = htons(len);
298 ce->ce_mode = create_ce_mode(mode);
299 option = allow_add ? ADD_CACHE_OK_TO_ADD : 0;
300 option |= allow_replace ? ADD_CACHE_OK_TO_REPLACE : 0;
301 return add_cache_entry(ce, option);
304 static const char *lockfile_name = NULL;
306 static void remove_lock_file(void)
309 unlink(lockfile_name);
312 static void remove_lock_file_on_signal(int signo)
317 int main(int argc, char **argv)
319 int i, newfd, entries, has_errors = 0;
320 int allow_options = 1;
321 static char lockfile[MAXPATHLEN+1];
322 const char *indexfile = get_index_file();
324 snprintf(lockfile, sizeof(lockfile), "%s.lock", indexfile);
326 newfd = open(lockfile, O_RDWR | O_CREAT | O_EXCL, 0600);
328 die("unable to create new cachefile");
330 signal(SIGINT, remove_lock_file_on_signal);
331 atexit(remove_lock_file);
332 lockfile_name = lockfile;
334 entries = read_cache();
336 die("cache corrupted");
338 for (i = 1 ; i < argc; i++) {
339 char *path = argv[i];
341 if (allow_options && *path == '-') {
342 if (!strcmp(path, "--")) {
346 if (!strcmp(path, "--add")) {
350 if (!strcmp(path, "--replace")) {
354 if (!strcmp(path, "--remove")) {
358 if (!strcmp(path, "--refresh")) {
359 has_errors |= refresh_cache();
362 if (!strcmp(path, "--cacheinfo")) {
363 if (i+3 >= argc || add_cacheinfo(argv[i+1], argv[i+2], argv[i+3]))
364 die("update-cache: --cacheinfo <mode> <sha1> <path>");
368 if (!strcmp(path, "--force-remove")) {
370 die("update-cache: --force-remove <path>");
371 if (remove_file_from_cache(argv[i+1]))
372 die("update-cache: --force-remove cannot remove %s", argv[i+1]);
377 if (!strcmp(path, "--ignore-missing")) {
381 die("unknown option %s", path);
383 if (!verify_path(path)) {
384 fprintf(stderr, "Ignoring path %s\n", argv[i]);
387 if (add_file_to_cache(path))
388 die("Unable to add %s to database", path);
390 if (write_cache(newfd, active_cache, active_nr) || rename(lockfile, indexfile))
391 die("Unable to write new cachefile");
393 lockfile_name = NULL;
394 return has_errors ? 1 : 0;