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, not_new = 0;
19 * update-cache --refresh may not touch anything at all, in which case
20 * writing 1.6MB of the same thing is a waste.
22 static int cache_changed = 0;
24 /* Three functions to allow overloaded pointer return; see linux/err.h */
25 static inline void *ERR_PTR(long error)
27 return (void *) error;
30 static inline long PTR_ERR(const void *ptr)
35 static inline long IS_ERR(const void *ptr)
37 return (unsigned long)ptr > (unsigned long)-1000L;
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.
45 static void fill_stat_cache_info(struct cache_entry *ce, struct stat *st)
47 ce->ce_ctime.sec = htonl(st->st_ctime);
48 ce->ce_mtime.sec = htonl(st->st_mtime);
50 ce->ce_ctime.nsec = htonl(st->st_ctim.tv_nsec);
51 ce->ce_mtime.nsec = htonl(st->st_mtim.tv_nsec);
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);
60 static int add_file_to_cache_1(char *path)
63 struct cache_entry *ce;
68 if (lstat(path, &st) < 0) {
69 if (errno == ENOENT || errno == ENOTDIR) {
71 return remove_file_from_cache(path);
75 namelen = strlen(path);
76 size = cache_entry_size(namelen);
79 memcpy(ce->name, path, namelen);
80 fill_stat_cache_info(ce, &st);
81 ce->ce_mode = create_ce_mode(st.st_mode);
82 ce->ce_flags = htons(namelen);
83 switch (st.st_mode & S_IFMT) {
85 fd = open(path, O_RDONLY);
88 if (index_fd(ce->sha1, fd, &st) < 0)
92 target = xmalloc(st.st_size+1);
93 if (readlink(path, target, st.st_size+1) != st.st_size) {
97 if (write_sha1_file(target, st.st_size, "blob", ce->sha1))
104 if (!cache_changed) {
105 /* If we have not smudged the cache, be careful
106 * to keep it clean. Find out if we have a matching
107 * cache entry that add_cache_entry would replace with,
108 * and if it matches then do not bother calling it.
110 int pos = cache_name_pos(ce->name, namelen);
112 !memcmp(active_cache[pos], ce, sizeof(*ce))) {
114 /* magic to tell add_file_to_cache that
115 * we have not updated anything.
120 return add_cache_entry(ce, allow_add);
123 static int add_file_to_cache(char *path)
125 int ret = add_file_to_cache_1(path);
133 static int match_data(int fd, void *buffer, unsigned long size)
137 int ret = read(fd, compare, sizeof(compare));
139 if (ret <= 0 || ret > size || memcmp(buffer, compare, ret))
147 static int compare_data(struct cache_entry *ce, unsigned long expected_size)
150 int fd = open(ce->name, O_RDONLY);
157 buffer = read_sha1_file(ce->sha1, type, &size);
159 if (size == expected_size && !strcmp(type, "blob"))
160 match = match_data(fd, buffer, size);
168 static int compare_link(struct cache_entry *ce, unsigned long expected_size)
177 target = xmalloc(expected_size);
178 len = readlink(ce->name, target, expected_size);
179 if (len != expected_size) {
183 buffer = read_sha1_file(ce->sha1, type, &size);
188 if (size == expected_size)
189 match = memcmp(buffer, target, size);
196 * "refresh" does not calculate a new sha1 file or bring the
197 * cache up-to-date for mode/content changes. But what it
198 * _does_ do is to "re-match" the stat information of a file
199 * with the cache, so that you can refresh the cache for a
200 * file that hasn't been changed but where the stat entry is
203 * For example, you'd want to do this after doing a "read-tree",
204 * to link up the stat cache details with the proper files.
206 static struct cache_entry *refresh_entry(struct cache_entry *ce)
209 struct cache_entry *updated;
212 if (lstat(ce->name, &st) < 0)
213 return ERR_PTR(-errno);
215 changed = cache_match_stat(ce, &st);
220 * If the mode or type has changed, there's no point in trying
221 * to refresh the entry - it's not going to match
223 if (changed & (MODE_CHANGED | TYPE_CHANGED))
224 return ERR_PTR(-EINVAL);
226 switch (st.st_mode & S_IFMT) {
228 if (compare_data(ce, st.st_size))
229 return ERR_PTR(-EINVAL);
232 if (compare_link(ce, st.st_size))
233 return ERR_PTR(-EINVAL);
236 return ERR_PTR(-EINVAL);
241 updated = xmalloc(size);
242 memcpy(updated, ce, size);
243 fill_stat_cache_info(updated, &st);
247 static int refresh_cache(void)
252 for (i = 0; i < active_nr; i++) {
253 struct cache_entry *ce, *new;
254 ce = active_cache[i];
256 printf("%s: needs merge\n", ce->name);
258 while ((i < active_nr) &&
259 ! strcmp(active_cache[i]->name, ce->name))
265 new = refresh_entry(ce);
267 if (!(not_new && PTR_ERR(new) == -ENOENT)) {
268 printf("%s: needs update\n", ce->name);
273 active_cache[i] = new;
279 * We fundamentally don't like some paths: we don't want
280 * dot or dot-dot anywhere, and in fact, we don't even want
281 * any other dot-files (.git or anything else). They
282 * are hidden, for chist sake.
284 * Also, we don't want double slashes or slashes at the
285 * end that can make pathnames ambiguous.
287 static int verify_path(char *path)
298 if (c != '/' && c != '.' && c != '\0')
306 static int add_cacheinfo(char *arg1, char *arg2, char *arg3)
310 unsigned char sha1[20];
311 struct cache_entry *ce;
313 if (sscanf(arg1, "%o", &mode) != 1)
315 if (get_sha1_hex(arg2, sha1))
317 if (!verify_path(arg3))
322 size = cache_entry_size(len);
326 memcpy(ce->sha1, sha1, 20);
327 memcpy(ce->name, arg3, len);
328 ce->ce_flags = htons(len);
329 ce->ce_mode = create_ce_mode(mode);
330 return add_cache_entry(ce, allow_add);
333 static const char *lockfile_name = NULL;
335 static void remove_lock_file(void)
338 unlink(lockfile_name);
341 static void remove_lock_file_on_signal(int signo)
346 int main(int argc, char **argv)
348 int i, newfd, entries, has_errors = 0;
349 int allow_options = 1;
350 static char lockfile[MAXPATHLEN+1];
351 const char *indexfile = get_index_file();
353 snprintf(lockfile, sizeof(lockfile), "%s.lock", indexfile);
355 newfd = open(lockfile, O_RDWR | O_CREAT | O_EXCL, 0600);
357 die("unable to create new cachefile");
359 signal(SIGINT, remove_lock_file_on_signal);
360 atexit(remove_lock_file);
361 lockfile_name = lockfile;
363 entries = read_cache();
365 die("cache corrupted");
367 for (i = 1 ; i < argc; i++) {
368 char *path = argv[i];
370 if (allow_options && *path == '-') {
371 if (!strcmp(path, "--")) {
375 if (!strcmp(path, "--add")) {
379 if (!strcmp(path, "--remove")) {
383 if (!strcmp(path, "--refresh")) {
384 has_errors |= refresh_cache();
387 if (!strcmp(path, "--cacheinfo")) {
388 if (i+3 >= argc || add_cacheinfo(argv[i+1], argv[i+2], argv[i+3]))
389 die("update-cache: --cacheinfo <mode> <sha1> <path>");
393 if (!strcmp(path, "--force-remove")) {
395 die("update-cache: --force-remove <path>");
396 if (remove_file_from_cache(argv[i+1]))
397 die("update-cache: --force-remove cannot remove %s", argv[i+1]);
402 if (!strcmp(path, "--ignore-missing")) {
406 die("unknown option %s", path);
408 if (!verify_path(path)) {
409 fprintf(stderr, "Ignoring path %s\n", argv[i]);
412 if (add_file_to_cache(path))
413 die("Unable to add %s to database", path);
418 else if (write_cache(newfd, active_cache, active_nr) ||
419 rename(lockfile, indexfile))
420 die("Unable to write new cachefile");
422 lockfile_name = NULL;
423 return has_errors ? 1 : 0;