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
, status
;
57 struct cache_entry
*ce
;
62 status
= lstat(path
, &st
);
63 if (status
< 0 || S_ISDIR(st
.st_mode
)) {
64 /* When we used to have "path" and now we want to add
65 * "path/file", we need a way to remove "path" before
66 * being able to add "path/file". However,
67 * "git-update-cache --remove path" would not work.
68 * --force-remove can be used but this is more user
69 * friendly, especially since we can do the opposite
70 * case just fine without --force-remove.
72 if (status
== 0 || (errno
== ENOENT
|| errno
== ENOTDIR
)) {
74 return remove_file_from_cache(path
);
76 return error("open(\"%s\"): %s", path
, strerror(errno
));
78 namelen
= strlen(path
);
79 size
= cache_entry_size(namelen
);
82 memcpy(ce
->name
, path
, namelen
);
83 fill_stat_cache_info(ce
, &st
);
84 ce
->ce_mode
= create_ce_mode(st
.st_mode
);
85 ce
->ce_flags
= htons(namelen
);
86 switch (st
.st_mode
& S_IFMT
) {
88 fd
= open(path
, O_RDONLY
);
91 if (index_fd(ce
->sha1
, fd
, &st
) < 0)
95 target
= xmalloc(st
.st_size
+1);
96 if (readlink(path
, target
, st
.st_size
+1) != st
.st_size
) {
100 if (write_sha1_file(target
, st
.st_size
, "blob", ce
->sha1
))
107 option
= allow_add
? ADD_CACHE_OK_TO_ADD
: 0;
108 option
|= allow_replace
? ADD_CACHE_OK_TO_REPLACE
: 0;
109 return add_cache_entry(ce
, option
);
112 static int match_data(int fd
, void *buffer
, unsigned long size
)
116 int ret
= read(fd
, compare
, sizeof(compare
));
118 if (ret
<= 0 || ret
> size
|| memcmp(buffer
, compare
, ret
))
126 static int compare_data(struct cache_entry
*ce
, unsigned long expected_size
)
129 int fd
= open(ce
->name
, O_RDONLY
);
136 buffer
= read_sha1_file(ce
->sha1
, type
, &size
);
138 if (size
== expected_size
&& !strcmp(type
, "blob"))
139 match
= match_data(fd
, buffer
, size
);
147 static int compare_link(struct cache_entry
*ce
, unsigned long expected_size
)
156 target
= xmalloc(expected_size
);
157 len
= readlink(ce
->name
, target
, expected_size
);
158 if (len
!= expected_size
) {
162 buffer
= read_sha1_file(ce
->sha1
, type
, &size
);
167 if (size
== expected_size
)
168 match
= memcmp(buffer
, target
, size
);
175 * "refresh" does not calculate a new sha1 file or bring the
176 * cache up-to-date for mode/content changes. But what it
177 * _does_ do is to "re-match" the stat information of a file
178 * with the cache, so that you can refresh the cache for a
179 * file that hasn't been changed but where the stat entry is
182 * For example, you'd want to do this after doing a "read-tree",
183 * to link up the stat cache details with the proper files.
185 static struct cache_entry
*refresh_entry(struct cache_entry
*ce
)
188 struct cache_entry
*updated
;
191 if (lstat(ce
->name
, &st
) < 0)
192 return ERR_PTR(-errno
);
194 changed
= cache_match_stat(ce
, &st
);
199 * If the mode or type has changed, there's no point in trying
200 * to refresh the entry - it's not going to match
202 if (changed
& (MODE_CHANGED
| TYPE_CHANGED
))
203 return ERR_PTR(-EINVAL
);
205 switch (st
.st_mode
& S_IFMT
) {
207 if (compare_data(ce
, st
.st_size
))
208 return ERR_PTR(-EINVAL
);
211 if (compare_link(ce
, st
.st_size
))
212 return ERR_PTR(-EINVAL
);
215 return ERR_PTR(-EINVAL
);
219 updated
= xmalloc(size
);
220 memcpy(updated
, ce
, size
);
221 fill_stat_cache_info(updated
, &st
);
225 static int refresh_cache(void)
230 for (i
= 0; i
< active_nr
; i
++) {
231 struct cache_entry
*ce
, *new;
232 ce
= active_cache
[i
];
234 printf("%s: needs merge\n", ce
->name
);
236 while ((i
< active_nr
) &&
237 ! strcmp(active_cache
[i
]->name
, ce
->name
))
243 new = refresh_entry(ce
);
245 if (!(not_new
&& PTR_ERR(new) == -ENOENT
)) {
246 printf("%s: needs update\n", ce
->name
);
251 active_cache_changed
= 1;
252 /* You can NOT just free active_cache[i] here, since it
253 * might not be necessarily malloc()ed but can also come
255 active_cache
[i
] = new;
261 * We fundamentally don't like some paths: we don't want
262 * dot or dot-dot anywhere, and in fact, we don't even want
263 * any other dot-files (.git or anything else). They
264 * are hidden, for chist sake.
266 * Also, we don't want double slashes or slashes at the
267 * end that can make pathnames ambiguous.
269 static int verify_path(char *path
)
280 if (c
!= '/' && c
!= '.' && c
!= '\0')
288 static int add_cacheinfo(char *arg1
, char *arg2
, char *arg3
)
290 int size
, len
, option
;
292 unsigned char sha1
[20];
293 struct cache_entry
*ce
;
295 if (sscanf(arg1
, "%o", &mode
) != 1)
297 if (get_sha1_hex(arg2
, sha1
))
299 if (!verify_path(arg3
))
303 size
= cache_entry_size(len
);
307 memcpy(ce
->sha1
, sha1
, 20);
308 memcpy(ce
->name
, arg3
, len
);
309 ce
->ce_flags
= htons(len
);
310 ce
->ce_mode
= create_ce_mode(mode
);
311 option
= allow_add
? ADD_CACHE_OK_TO_ADD
: 0;
312 option
|= allow_replace
? ADD_CACHE_OK_TO_REPLACE
: 0;
313 return add_cache_entry(ce
, option
);
316 static const char *lockfile_name
= NULL
;
318 static void remove_lock_file(void)
321 unlink(lockfile_name
);
324 static void remove_lock_file_on_signal(int signo
)
329 int main(int argc
, char **argv
)
331 int i
, newfd
, entries
, has_errors
= 0;
332 int allow_options
= 1;
333 static char lockfile
[MAXPATHLEN
+1];
334 const char *indexfile
= get_index_file();
336 snprintf(lockfile
, sizeof(lockfile
), "%s.lock", indexfile
);
338 newfd
= open(lockfile
, O_RDWR
| O_CREAT
| O_EXCL
, 0600);
340 die("unable to create new cachefile");
342 signal(SIGINT
, remove_lock_file_on_signal
);
343 atexit(remove_lock_file
);
344 lockfile_name
= lockfile
;
346 entries
= read_cache();
348 die("cache corrupted");
350 for (i
= 1 ; i
< argc
; i
++) {
351 char *path
= argv
[i
];
353 if (allow_options
&& *path
== '-') {
354 if (!strcmp(path
, "--")) {
358 if (!strcmp(path
, "--add")) {
362 if (!strcmp(path
, "--replace")) {
366 if (!strcmp(path
, "--remove")) {
370 if (!strcmp(path
, "--refresh")) {
371 has_errors
|= refresh_cache();
374 if (!strcmp(path
, "--cacheinfo")) {
376 die("update-cache: --cacheinfo <mode> <sha1> <path>");
377 if (add_cacheinfo(argv
[i
+1], argv
[i
+2], argv
[i
+3]))
378 die("update-cache: --cacheinfo cannot add %s", argv
[i
+3]);
382 if (!strcmp(path
, "--force-remove")) {
384 die("update-cache: --force-remove <path>");
385 if (remove_file_from_cache(argv
[i
+1]))
386 die("update-cache: --force-remove cannot remove %s", argv
[i
+1]);
391 if (!strcmp(path
, "--ignore-missing")) {
395 die("unknown option %s", path
);
397 if (!verify_path(path
)) {
398 fprintf(stderr
, "Ignoring path %s\n", argv
[i
]);
401 if (add_file_to_cache(path
))
402 die("Unable to add %s to database", path
);
404 if (write_cache(newfd
, active_cache
, active_nr
) || rename(lockfile
, indexfile
))
405 die("Unable to write new cachefile");
407 lockfile_name
= NULL
;
408 return has_errors
? 1 : 0;