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;
17 static int index_fd(unsigned char *sha1
, int fd
, struct stat
*st
)
20 unsigned long size
= st
->st_size
;
21 int max_out_bytes
= size
+ 200;
22 void *out
= malloc(max_out_bytes
);
23 void *metadata
= malloc(200);
30 in
= mmap(NULL
, size
, PROT_READ
, MAP_PRIVATE
, fd
, 0);
32 if (!out
|| (int)(long)in
== -1)
35 metadata_size
= 1+sprintf(metadata
, "blob %lu", size
);
38 SHA1_Update(&c
, metadata
, metadata_size
);
39 SHA1_Update(&c
, in
, size
);
42 memset(&stream
, 0, sizeof(stream
));
43 deflateInit(&stream
, Z_BEST_COMPRESSION
);
46 * ASCII size + nul byte
48 stream
.next_in
= metadata
;
49 stream
.avail_in
= metadata_size
;
50 stream
.next_out
= out
;
51 stream
.avail_out
= max_out_bytes
;
52 while (deflate(&stream
, 0) == Z_OK
)
59 stream
.avail_in
= size
;
60 while (deflate(&stream
, Z_FINISH
) == Z_OK
)
65 return write_sha1_buffer(sha1
, out
, stream
.total_out
);
69 * This only updates the "non-critical" parts of the directory
70 * cache, ie the parts that aren't tracked by GIT, and only used
71 * to validate the cache.
73 static void fill_stat_cache_info(struct cache_entry
*ce
, struct stat
*st
)
75 ce
->ce_ctime
.sec
= htonl(st
->st_ctime
);
76 ce
->ce_mtime
.sec
= htonl(st
->st_mtime
);
78 ce
->ce_ctime
.nsec
= htonl(st
->st_ctim
.tv_nsec
);
79 ce
->ce_mtime
.nsec
= htonl(st
->st_mtim
.tv_nsec
);
81 ce
->ce_dev
= htonl(st
->st_dev
);
82 ce
->ce_ino
= htonl(st
->st_ino
);
83 ce
->ce_uid
= htonl(st
->st_uid
);
84 ce
->ce_gid
= htonl(st
->st_gid
);
85 ce
->ce_size
= htonl(st
->st_size
);
88 static int add_file_to_cache(char *path
)
91 struct cache_entry
*ce
;
95 fd
= open(path
, O_RDONLY
);
97 if (errno
== ENOENT
) {
99 return remove_file_from_cache(path
);
103 if (fstat(fd
, &st
) < 0) {
107 namelen
= strlen(path
);
108 size
= cache_entry_size(namelen
);
111 memcpy(ce
->name
, path
, namelen
);
112 fill_stat_cache_info(ce
, &st
);
113 ce
->ce_mode
= create_ce_mode(st
.st_mode
);
114 ce
->ce_flags
= htons(namelen
);
116 if (index_fd(ce
->sha1
, fd
, &st
) < 0)
119 return add_cache_entry(ce
, allow_add
);
122 static int match_data(int fd
, void *buffer
, unsigned long size
)
126 int ret
= read(fd
, compare
, sizeof(compare
));
128 if (ret
<= 0 || ret
> size
|| memcmp(buffer
, compare
, ret
))
136 static int compare_data(struct cache_entry
*ce
, unsigned long expected_size
)
139 int fd
= open(ce
->name
, O_RDONLY
);
146 buffer
= read_sha1_file(ce
->sha1
, type
, &size
);
148 if (size
== expected_size
&& !strcmp(type
, "blob"))
149 match
= match_data(fd
, buffer
, size
);
158 * "refresh" does not calculate a new sha1 file or bring the
159 * cache up-to-date for mode/content changes. But what it
160 * _does_ do is to "re-match" the stat information of a file
161 * with the cache, so that you can refresh the cache for a
162 * file that hasn't been changed but where the stat entry is
165 * For example, you'd want to do this after doing a "read-tree",
166 * to link up the stat cache details with the proper files.
168 static struct cache_entry
*refresh_entry(struct cache_entry
*ce
)
171 struct cache_entry
*updated
;
174 if (stat(ce
->name
, &st
) < 0)
177 changed
= cache_match_stat(ce
, &st
);
182 * If the mode has changed, there's no point in trying
183 * to refresh the entry - it's not going to match
185 if (changed
& MODE_CHANGED
)
188 if (compare_data(ce
, st
.st_size
))
192 updated
= malloc(size
);
193 memcpy(updated
, ce
, size
);
194 fill_stat_cache_info(updated
, &st
);
198 static void refresh_cache(void)
202 for (i
= 0; i
< active_nr
; i
++) {
203 struct cache_entry
*ce
, *new;
204 ce
= active_cache
[i
];
206 printf("%s: needs merge\n", ce
->name
);
207 while ((i
< active_nr
) &&
208 ! strcmp(active_cache
[i
]->name
, ce
->name
))
214 new = refresh_entry(ce
);
216 printf("%s: needs update\n", ce
->name
);
219 active_cache
[i
] = new;
224 * We fundamentally don't like some paths: we don't want
225 * dot or dot-dot anywhere, and in fact, we don't even want
226 * any other dot-files (.git or anything else). They
227 * are hidden, for chist sake.
229 * Also, we don't want double slashes or slashes at the
230 * end that can make pathnames ambiguous.
232 static int verify_path(char *path
)
243 if (c
!= '/' && c
!= '.' && c
!= '\0')
251 static int add_cacheinfo(char *arg1
, char *arg2
, char *arg3
)
255 unsigned char sha1
[20];
256 struct cache_entry
*ce
;
258 if (sscanf(arg1
, "%o", &mode
) != 1)
260 if (get_sha1_hex(arg2
, sha1
))
262 if (!verify_path(arg3
))
266 size
= cache_entry_size(len
);
270 memcpy(ce
->sha1
, sha1
, 20);
271 memcpy(ce
->name
, arg3
, len
);
272 ce
->ce_flags
= htons(len
);
273 ce
->ce_mode
= create_ce_mode(mode
);
274 return add_cache_entry(ce
, allow_add
);
277 static const char *lockfile_name
= NULL
;
279 static void remove_lock_file(void)
282 unlink(lockfile_name
);
285 int main(int argc
, char **argv
)
287 int i
, newfd
, entries
;
288 int allow_options
= 1;
289 static char lockfile
[MAXPATHLEN
+1];
290 const char *indexfile
= get_index_file();
292 snprintf(lockfile
, sizeof(lockfile
), "%s.lock", indexfile
);
294 newfd
= open(lockfile
, O_RDWR
| O_CREAT
| O_EXCL
, 0600);
296 die("unable to create new cachefile");
298 atexit(remove_lock_file
);
299 lockfile_name
= lockfile
;
301 entries
= read_cache();
303 die("cache corrupted");
305 for (i
= 1 ; i
< argc
; i
++) {
306 char *path
= argv
[i
];
308 if (allow_options
&& *path
== '-') {
309 if (!strcmp(path
, "--")) {
313 if (!strcmp(path
, "--add")) {
317 if (!strcmp(path
, "--remove")) {
321 if (!strcmp(path
, "--refresh")) {
325 if (!strcmp(path
, "--cacheinfo")) {
326 if (i
+3 >= argc
|| add_cacheinfo(argv
[i
+1], argv
[i
+2], argv
[i
+3]))
327 die("update-cache: --cacheinfo <mode> <sha1> <path>");
331 die("unknown option %s", path
);
333 if (!verify_path(path
)) {
334 fprintf(stderr
, "Ignoring path %s\n", argv
[i
]);
337 if (add_file_to_cache(path
))
338 die("Unable to add %s to database", path
);
340 if (write_cache(newfd
, active_cache
, active_nr
) || rename(lockfile
, indexfile
))
341 die("Unable to write new cachefile");
343 lockfile_name
= NULL
;