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
;
69 if (lstat(path
, &st
) < 0) {
70 if (errno
== ENOENT
|| errno
== ENOTDIR
) {
72 return remove_file_from_cache(path
);
76 namelen
= strlen(path
);
77 size
= cache_entry_size(namelen
);
80 memcpy(ce
->name
, path
, namelen
);
81 fill_stat_cache_info(ce
, &st
);
82 ce
->ce_mode
= create_ce_mode(st
.st_mode
);
83 ce
->ce_flags
= htons(namelen
);
84 switch (st
.st_mode
& S_IFMT
) {
86 fd
= open(path
, O_RDONLY
);
89 if (index_fd(ce
->sha1
, fd
, &st
) < 0)
93 target
= xmalloc(st
.st_size
+1);
94 if (readlink(path
, target
, st
.st_size
+1) != st
.st_size
) {
98 if (write_sha1_file(target
, st
.st_size
, "blob", ce
->sha1
))
105 if (!cache_changed
) {
106 /* If we have not smudged the cache, be careful
107 * to keep it clean. Find out if we have a matching
108 * cache entry that add_cache_entry would replace with,
109 * and if it matches then do not bother calling it.
111 int pos
= cache_name_pos(ce
->name
, namelen
);
113 !memcmp(active_cache
[pos
], ce
, sizeof(*ce
))) {
115 /* magic to tell add_file_to_cache that
116 * we have not updated anything.
121 return add_cache_entry(ce
, allow_add
);
124 static int add_file_to_cache(char *path
)
126 int ret
= add_file_to_cache_1(path
);
134 static int match_data(int fd
, void *buffer
, unsigned long size
)
138 int ret
= read(fd
, compare
, sizeof(compare
));
140 if (ret
<= 0 || ret
> size
|| memcmp(buffer
, compare
, ret
))
148 static int compare_data(struct cache_entry
*ce
, unsigned long expected_size
)
151 int fd
= open(ce
->name
, O_RDONLY
);
158 buffer
= read_sha1_file(ce
->sha1
, type
, &size
);
160 if (size
== expected_size
&& !strcmp(type
, "blob"))
161 match
= match_data(fd
, buffer
, size
);
169 static int compare_link(struct cache_entry
*ce
, unsigned long expected_size
)
178 target
= xmalloc(expected_size
);
179 len
= readlink(ce
->name
, target
, expected_size
);
180 if (len
!= expected_size
) {
184 buffer
= read_sha1_file(ce
->sha1
, type
, &size
);
189 if (size
== expected_size
)
190 match
= memcmp(buffer
, target
, size
);
197 * "refresh" does not calculate a new sha1 file or bring the
198 * cache up-to-date for mode/content changes. But what it
199 * _does_ do is to "re-match" the stat information of a file
200 * with the cache, so that you can refresh the cache for a
201 * file that hasn't been changed but where the stat entry is
204 * For example, you'd want to do this after doing a "read-tree",
205 * to link up the stat cache details with the proper files.
207 static struct cache_entry
*refresh_entry(struct cache_entry
*ce
)
210 struct cache_entry
*updated
;
213 if (lstat(ce
->name
, &st
) < 0)
214 return ERR_PTR(-errno
);
216 changed
= cache_match_stat(ce
, &st
);
221 * If the mode or type has changed, there's no point in trying
222 * to refresh the entry - it's not going to match
224 if (changed
& (MODE_CHANGED
| TYPE_CHANGED
))
225 return ERR_PTR(-EINVAL
);
227 switch (st
.st_mode
& S_IFMT
) {
229 if (compare_data(ce
, st
.st_size
))
230 return ERR_PTR(-EINVAL
);
233 if (compare_link(ce
, st
.st_size
))
234 return ERR_PTR(-EINVAL
);
237 return ERR_PTR(-EINVAL
);
242 updated
= xmalloc(size
);
243 memcpy(updated
, ce
, size
);
244 fill_stat_cache_info(updated
, &st
);
248 static int refresh_cache(void)
253 for (i
= 0; i
< active_nr
; i
++) {
254 struct cache_entry
*ce
, *new;
255 ce
= active_cache
[i
];
257 printf("%s: needs merge\n", ce
->name
);
259 while ((i
< active_nr
) &&
260 ! strcmp(active_cache
[i
]->name
, ce
->name
))
266 new = refresh_entry(ce
);
268 if (!(not_new
&& PTR_ERR(new) == -ENOENT
)) {
269 printf("%s: needs update\n", ce
->name
);
274 active_cache
[i
] = new;
280 * We fundamentally don't like some paths: we don't want
281 * dot or dot-dot anywhere, and in fact, we don't even want
282 * any other dot-files (.git or anything else). They
283 * are hidden, for chist sake.
285 * Also, we don't want double slashes or slashes at the
286 * end that can make pathnames ambiguous.
288 static int verify_path(char *path
)
299 if (c
!= '/' && c
!= '.' && c
!= '\0')
307 static int add_cacheinfo(char *arg1
, char *arg2
, char *arg3
)
311 unsigned char sha1
[20];
312 struct cache_entry
*ce
;
314 if (sscanf(arg1
, "%o", &mode
) != 1)
316 if (get_sha1_hex(arg2
, sha1
))
318 if (!verify_path(arg3
))
323 size
= cache_entry_size(len
);
327 memcpy(ce
->sha1
, sha1
, 20);
328 memcpy(ce
->name
, arg3
, len
);
329 ce
->ce_flags
= htons(len
);
330 ce
->ce_mode
= create_ce_mode(mode
);
331 return add_cache_entry(ce
, allow_add
);
334 static const char *lockfile_name
= NULL
;
336 static void remove_lock_file(void)
339 unlink(lockfile_name
);
342 static void remove_lock_file_on_signal(int signo
)
347 int main(int argc
, char **argv
)
349 int i
, newfd
, entries
, has_errors
= 0;
350 int allow_options
= 1;
351 static char lockfile
[MAXPATHLEN
+1];
352 const char *indexfile
= get_index_file();
354 snprintf(lockfile
, sizeof(lockfile
), "%s.lock", indexfile
);
356 newfd
= open(lockfile
, O_RDWR
| O_CREAT
| O_EXCL
, 0600);
358 die("unable to create new cachefile");
360 signal(SIGINT
, remove_lock_file_on_signal
);
361 atexit(remove_lock_file
);
362 lockfile_name
= lockfile
;
364 entries
= read_cache();
366 die("cache corrupted");
368 for (i
= 1 ; i
< argc
; i
++) {
369 char *path
= argv
[i
];
371 if (allow_options
&& *path
== '-') {
372 if (!strcmp(path
, "--")) {
376 if (!strcmp(path
, "--add")) {
380 if (!strcmp(path
, "--remove")) {
384 if (!strcmp(path
, "--refresh")) {
385 has_errors
|= refresh_cache();
388 if (!strcmp(path
, "--cacheinfo")) {
389 if (i
+3 >= argc
|| add_cacheinfo(argv
[i
+1], argv
[i
+2], argv
[i
+3]))
390 die("update-cache: --cacheinfo <mode> <sha1> <path>");
394 if (!strcmp(path
, "--force-remove")) {
396 die("update-cache: --force-remove <path>");
397 if (remove_file_from_cache(argv
[i
+1]))
398 die("update-cache: --force-remove cannot remove %s", argv
[i
+1]);
403 if (!strcmp(path
, "--ignore-missing")) {
407 die("unknown option %s", path
);
409 if (!verify_path(path
)) {
410 fprintf(stderr
, "Ignoring path %s\n", argv
[i
]);
413 if (add_file_to_cache(path
))
414 die("Unable to add %s to database", path
);
419 else if (write_cache(newfd
, active_cache
, active_nr
) ||
420 rename(lockfile
, indexfile
))
421 die("Unable to write new cachefile");
423 lockfile_name
= NULL
;
424 return has_errors
? 1 : 0;