2 * GIT - The information manager from hell
4 * Copyright (C) Linus Torvalds, 2005
9 * Default to not allowing changes to the list of files. The
10 * tool doesn't actually care, but this makes it harder to add
11 * files to the revision control by mistake by doing something
12 * like "update-cache *" and suddenly having all the object
13 * files be revision controlled.
15 static int allow_add
= 0, allow_remove
= 0, not_new
= 0;
17 /* Three functions to allow overloaded pointer return; see linux/err.h */
18 static inline void *ERR_PTR(long error
)
20 return (void *) error
;
23 static inline long PTR_ERR(const void *ptr
)
28 static inline long IS_ERR(const void *ptr
)
30 return (unsigned long)ptr
> (unsigned long)-1000L;
33 static int index_fd(unsigned char *sha1
, int fd
, struct stat
*st
)
36 unsigned long size
= st
->st_size
;
37 int max_out_bytes
= size
+ 200;
38 void *out
= malloc(max_out_bytes
);
39 void *metadata
= malloc(200);
46 in
= mmap(NULL
, size
, PROT_READ
, MAP_PRIVATE
, fd
, 0);
48 if (!out
|| (int)(long)in
== -1)
51 metadata_size
= 1+sprintf(metadata
, "blob %lu", size
);
54 SHA1_Update(&c
, metadata
, metadata_size
);
55 SHA1_Update(&c
, in
, size
);
58 memset(&stream
, 0, sizeof(stream
));
59 deflateInit(&stream
, Z_BEST_COMPRESSION
);
62 * ASCII size + nul byte
64 stream
.next_in
= metadata
;
65 stream
.avail_in
= metadata_size
;
66 stream
.next_out
= out
;
67 stream
.avail_out
= max_out_bytes
;
68 while (deflate(&stream
, 0) == Z_OK
)
75 stream
.avail_in
= size
;
76 while (deflate(&stream
, Z_FINISH
) == Z_OK
)
81 return write_sha1_buffer(sha1
, out
, stream
.total_out
);
85 * This only updates the "non-critical" parts of the directory
86 * cache, ie the parts that aren't tracked by GIT, and only used
87 * to validate the cache.
89 static void fill_stat_cache_info(struct cache_entry
*ce
, struct stat
*st
)
91 ce
->ce_ctime
.sec
= htonl(st
->st_ctime
);
92 ce
->ce_mtime
.sec
= htonl(st
->st_mtime
);
94 ce
->ce_ctime
.nsec
= htonl(st
->st_ctim
.tv_nsec
);
95 ce
->ce_mtime
.nsec
= htonl(st
->st_mtim
.tv_nsec
);
97 ce
->ce_dev
= htonl(st
->st_dev
);
98 ce
->ce_ino
= htonl(st
->st_ino
);
99 ce
->ce_uid
= htonl(st
->st_uid
);
100 ce
->ce_gid
= htonl(st
->st_gid
);
101 ce
->ce_size
= htonl(st
->st_size
);
104 static int add_file_to_cache(char *path
)
107 struct cache_entry
*ce
;
111 fd
= open(path
, O_RDONLY
);
113 if (errno
== ENOENT
) {
115 return remove_file_from_cache(path
);
119 if (fstat(fd
, &st
) < 0) {
123 namelen
= strlen(path
);
124 size
= cache_entry_size(namelen
);
127 memcpy(ce
->name
, path
, namelen
);
128 fill_stat_cache_info(ce
, &st
);
129 ce
->ce_mode
= create_ce_mode(st
.st_mode
);
130 ce
->ce_flags
= htons(namelen
);
132 if (index_fd(ce
->sha1
, fd
, &st
) < 0)
135 return add_cache_entry(ce
, allow_add
);
138 static int match_data(int fd
, void *buffer
, unsigned long size
)
142 int ret
= read(fd
, compare
, sizeof(compare
));
144 if (ret
<= 0 || ret
> size
|| memcmp(buffer
, compare
, ret
))
152 static int compare_data(struct cache_entry
*ce
, unsigned long expected_size
)
155 int fd
= open(ce
->name
, O_RDONLY
);
162 buffer
= read_sha1_file(ce
->sha1
, type
, &size
);
164 if (size
== expected_size
&& !strcmp(type
, "blob"))
165 match
= match_data(fd
, buffer
, size
);
174 * "refresh" does not calculate a new sha1 file or bring the
175 * cache up-to-date for mode/content changes. But what it
176 * _does_ do is to "re-match" the stat information of a file
177 * with the cache, so that you can refresh the cache for a
178 * file that hasn't been changed but where the stat entry is
181 * For example, you'd want to do this after doing a "read-tree",
182 * to link up the stat cache details with the proper files.
184 static struct cache_entry
*refresh_entry(struct cache_entry
*ce
)
187 struct cache_entry
*updated
;
190 if (stat(ce
->name
, &st
) < 0)
191 return ERR_PTR(-errno
);
193 changed
= cache_match_stat(ce
, &st
);
198 * If the mode has changed, there's no point in trying
199 * to refresh the entry - it's not going to match
201 if (changed
& MODE_CHANGED
)
202 return ERR_PTR(-EINVAL
);
204 if (compare_data(ce
, st
.st_size
))
205 return ERR_PTR(-EINVAL
);
208 updated
= malloc(size
);
209 memcpy(updated
, ce
, size
);
210 fill_stat_cache_info(updated
, &st
);
214 static void refresh_cache(void)
218 for (i
= 0; i
< active_nr
; i
++) {
219 struct cache_entry
*ce
, *new;
220 ce
= active_cache
[i
];
222 printf("%s: needs merge\n", ce
->name
);
223 while ((i
< active_nr
) &&
224 ! strcmp(active_cache
[i
]->name
, ce
->name
))
230 new = refresh_entry(ce
);
232 if (!(not_new
&& PTR_ERR(new) == -ENOENT
))
233 printf("%s: needs update\n", ce
->name
);
236 active_cache
[i
] = new;
241 * We fundamentally don't like some paths: we don't want
242 * dot or dot-dot anywhere, and in fact, we don't even want
243 * any other dot-files (.git or anything else). They
244 * are hidden, for chist sake.
246 * Also, we don't want double slashes or slashes at the
247 * end that can make pathnames ambiguous.
249 static int verify_path(char *path
)
260 if (c
!= '/' && c
!= '.' && c
!= '\0')
268 static int add_cacheinfo(char *arg1
, char *arg2
, char *arg3
)
272 unsigned char sha1
[20];
273 struct cache_entry
*ce
;
275 if (sscanf(arg1
, "%o", &mode
) != 1)
277 if (get_sha1_hex(arg2
, sha1
))
279 if (!verify_path(arg3
))
283 size
= cache_entry_size(len
);
287 memcpy(ce
->sha1
, sha1
, 20);
288 memcpy(ce
->name
, arg3
, len
);
289 ce
->ce_flags
= htons(len
);
290 ce
->ce_mode
= create_ce_mode(mode
);
291 return add_cache_entry(ce
, allow_add
);
294 static const char *lockfile_name
= NULL
;
296 static void remove_lock_file(void)
299 unlink(lockfile_name
);
302 int main(int argc
, char **argv
)
304 int i
, newfd
, entries
;
305 int allow_options
= 1;
306 static char lockfile
[MAXPATHLEN
+1];
307 const char *indexfile
= get_index_file();
309 snprintf(lockfile
, sizeof(lockfile
), "%s.lock", indexfile
);
311 newfd
= open(lockfile
, O_RDWR
| O_CREAT
| O_EXCL
, 0600);
313 die("unable to create new cachefile");
315 atexit(remove_lock_file
);
316 lockfile_name
= lockfile
;
318 entries
= read_cache();
320 die("cache corrupted");
322 for (i
= 1 ; i
< argc
; i
++) {
323 char *path
= argv
[i
];
325 if (allow_options
&& *path
== '-') {
326 if (!strcmp(path
, "--")) {
330 if (!strcmp(path
, "--add")) {
334 if (!strcmp(path
, "--remove")) {
338 if (!strcmp(path
, "--refresh")) {
342 if (!strcmp(path
, "--cacheinfo")) {
343 if (i
+3 >= argc
|| add_cacheinfo(argv
[i
+1], argv
[i
+2], argv
[i
+3]))
344 die("update-cache: --cacheinfo <mode> <sha1> <path>");
348 if (!strcmp(path
, "--ignore-missing")) {
352 die("unknown option %s", path
);
354 if (!verify_path(path
)) {
355 fprintf(stderr
, "Ignoring path %s\n", argv
[i
]);
358 if (add_file_to_cache(path
))
359 die("Unable to add %s to database", path
);
361 if (write_cache(newfd
, active_cache
, active_nr
) || rename(lockfile
, indexfile
))
362 die("Unable to write new cachefile");
364 lockfile_name
= NULL
;