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;
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;
34 static int index_fd(unsigned char *sha1
, int fd
, struct stat
*st
)
37 unsigned long size
= st
->st_size
;
38 int max_out_bytes
= size
+ 200;
39 void *out
= xmalloc(max_out_bytes
);
40 void *metadata
= xmalloc(200);
47 in
= mmap(NULL
, size
, PROT_READ
, MAP_PRIVATE
, fd
, 0);
49 if (!out
|| (int)(long)in
== -1)
52 metadata_size
= 1+sprintf(metadata
, "blob %lu", size
);
55 SHA1_Update(&c
, metadata
, metadata_size
);
56 SHA1_Update(&c
, in
, size
);
59 memset(&stream
, 0, sizeof(stream
));
60 deflateInit(&stream
, Z_BEST_COMPRESSION
);
63 * ASCII size + nul byte
65 stream
.next_in
= metadata
;
66 stream
.avail_in
= metadata_size
;
67 stream
.next_out
= out
;
68 stream
.avail_out
= max_out_bytes
;
69 while (deflate(&stream
, 0) == Z_OK
)
76 stream
.avail_in
= size
;
77 while (deflate(&stream
, Z_FINISH
) == Z_OK
)
82 return write_sha1_buffer(sha1
, out
, stream
.total_out
);
86 * This only updates the "non-critical" parts of the directory
87 * cache, ie the parts that aren't tracked by GIT, and only used
88 * to validate the cache.
90 static void fill_stat_cache_info(struct cache_entry
*ce
, struct stat
*st
)
92 ce
->ce_ctime
.sec
= htonl(st
->st_ctime
);
93 ce
->ce_mtime
.sec
= htonl(st
->st_mtime
);
95 ce
->ce_ctime
.nsec
= htonl(st
->st_ctim
.tv_nsec
);
96 ce
->ce_mtime
.nsec
= htonl(st
->st_mtim
.tv_nsec
);
98 ce
->ce_dev
= htonl(st
->st_dev
);
99 ce
->ce_ino
= htonl(st
->st_ino
);
100 ce
->ce_uid
= htonl(st
->st_uid
);
101 ce
->ce_gid
= htonl(st
->st_gid
);
102 ce
->ce_size
= htonl(st
->st_size
);
105 static int add_file_to_cache(char *path
)
108 struct cache_entry
*ce
;
112 fd
= open(path
, O_RDONLY
);
114 if (errno
== ENOENT
) {
116 return remove_file_from_cache(path
);
120 if (fstat(fd
, &st
) < 0) {
124 namelen
= strlen(path
);
125 size
= cache_entry_size(namelen
);
128 memcpy(ce
->name
, path
, namelen
);
129 fill_stat_cache_info(ce
, &st
);
130 ce
->ce_mode
= create_ce_mode(st
.st_mode
);
131 ce
->ce_flags
= htons(namelen
);
133 if (index_fd(ce
->sha1
, fd
, &st
) < 0)
136 return add_cache_entry(ce
, allow_add
);
139 static int match_data(int fd
, void *buffer
, unsigned long size
)
143 int ret
= read(fd
, compare
, sizeof(compare
));
145 if (ret
<= 0 || ret
> size
|| memcmp(buffer
, compare
, ret
))
153 static int compare_data(struct cache_entry
*ce
, unsigned long expected_size
)
156 int fd
= open(ce
->name
, O_RDONLY
);
163 buffer
= read_sha1_file(ce
->sha1
, type
, &size
);
165 if (size
== expected_size
&& !strcmp(type
, "blob"))
166 match
= match_data(fd
, buffer
, 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 (stat(ce
->name
, &st
) < 0)
192 return ERR_PTR(-errno
);
194 changed
= cache_match_stat(ce
, &st
);
199 * If the mode has changed, there's no point in trying
200 * to refresh the entry - it's not going to match
202 if (changed
& MODE_CHANGED
)
203 return ERR_PTR(-EINVAL
);
205 if (compare_data(ce
, st
.st_size
))
206 return ERR_PTR(-EINVAL
);
209 updated
= xmalloc(size
);
210 memcpy(updated
, ce
, size
);
211 fill_stat_cache_info(updated
, &st
);
215 static void refresh_cache(void)
219 for (i
= 0; i
< active_nr
; i
++) {
220 struct cache_entry
*ce
, *new;
221 ce
= active_cache
[i
];
223 printf("%s: needs merge\n", ce
->name
);
224 while ((i
< active_nr
) &&
225 ! strcmp(active_cache
[i
]->name
, ce
->name
))
231 new = refresh_entry(ce
);
233 if (!(not_new
&& PTR_ERR(new) == -ENOENT
))
234 printf("%s: needs update\n", ce
->name
);
237 active_cache
[i
] = new;
242 * We fundamentally don't like some paths: we don't want
243 * dot or dot-dot anywhere, and in fact, we don't even want
244 * any other dot-files (.git or anything else). They
245 * are hidden, for chist sake.
247 * Also, we don't want double slashes or slashes at the
248 * end that can make pathnames ambiguous.
250 static int verify_path(char *path
)
261 if (c
!= '/' && c
!= '.' && c
!= '\0')
269 static int add_cacheinfo(char *arg1
, char *arg2
, char *arg3
)
273 unsigned char sha1
[20];
274 struct cache_entry
*ce
;
276 if (sscanf(arg1
, "%o", &mode
) != 1)
278 if (get_sha1_hex(arg2
, sha1
))
280 if (!verify_path(arg3
))
284 size
= cache_entry_size(len
);
288 memcpy(ce
->sha1
, sha1
, 20);
289 memcpy(ce
->name
, arg3
, len
);
290 ce
->ce_flags
= htons(len
);
291 ce
->ce_mode
= create_ce_mode(mode
);
292 return add_cache_entry(ce
, allow_add
);
295 static const char *lockfile_name
= NULL
;
297 static void remove_lock_file(void)
300 unlink(lockfile_name
);
303 static void remove_lock_file_on_signal(int signo
)
308 int main(int argc
, char **argv
)
310 int i
, newfd
, entries
;
311 int allow_options
= 1;
312 static char lockfile
[MAXPATHLEN
+1];
313 const char *indexfile
= get_index_file();
315 snprintf(lockfile
, sizeof(lockfile
), "%s.lock", indexfile
);
317 newfd
= open(lockfile
, O_RDWR
| O_CREAT
| O_EXCL
, 0600);
319 die("unable to create new cachefile");
321 signal(SIGINT
, remove_lock_file_on_signal
);
322 atexit(remove_lock_file
);
323 lockfile_name
= lockfile
;
325 entries
= read_cache();
327 die("cache corrupted");
329 for (i
= 1 ; i
< argc
; i
++) {
330 char *path
= argv
[i
];
332 if (allow_options
&& *path
== '-') {
333 if (!strcmp(path
, "--")) {
337 if (!strcmp(path
, "--add")) {
341 if (!strcmp(path
, "--remove")) {
345 if (!strcmp(path
, "--refresh")) {
349 if (!strcmp(path
, "--cacheinfo")) {
350 if (i
+3 >= argc
|| add_cacheinfo(argv
[i
+1], argv
[i
+2], argv
[i
+3]))
351 die("update-cache: --cacheinfo <mode> <sha1> <path>");
355 if (!strcmp(path
, "--ignore-missing")) {
359 die("unknown option %s", path
);
361 if (!verify_path(path
)) {
362 fprintf(stderr
, "Ignoring path %s\n", argv
[i
]);
365 if (add_file_to_cache(path
))
366 die("Unable to add %s to database", path
);
368 if (write_cache(newfd
, active_cache
, active_nr
) || rename(lockfile
, indexfile
))
369 die("Unable to write new cachefile");
371 lockfile_name
= NULL
;