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(const char *path
, int namelen
, struct cache_entry
*ce
, int fd
, struct stat
*st
)
20 int max_out_bytes
= namelen
+ st
->st_size
+ 200;
21 void *out
= malloc(max_out_bytes
);
22 void *metadata
= malloc(namelen
+ 200);
23 void *in
= mmap(NULL
, st
->st_size
, PROT_READ
, MAP_PRIVATE
, fd
, 0);
27 if (!out
|| (int)(long)in
== -1)
30 memset(&stream
, 0, sizeof(stream
));
31 deflateInit(&stream
, Z_BEST_COMPRESSION
);
34 * ASCII size + nul byte
36 stream
.next_in
= metadata
;
37 stream
.avail_in
= 1+sprintf(metadata
, "blob %lu", (unsigned long) st
->st_size
);
38 stream
.next_out
= out
;
39 stream
.avail_out
= max_out_bytes
;
40 while (deflate(&stream
, 0) == Z_OK
)
47 stream
.avail_in
= st
->st_size
;
48 while (deflate(&stream
, Z_FINISH
) == Z_OK
)
54 SHA1_Update(&c
, out
, stream
.total_out
);
55 SHA1_Final(ce
->sha1
, &c
);
57 return write_sha1_buffer(ce
->sha1
, out
, stream
.total_out
);
61 * This only updates the "non-critical" parts of the directory
62 * cache, ie the parts that aren't tracked by GIT, and only used
63 * to validate the cache.
65 static void fill_stat_cache_info(struct cache_entry
*ce
, struct stat
*st
)
67 ce
->ctime
.sec
= st
->st_ctime
;
68 ce
->ctime
.nsec
= st
->st_ctim
.tv_nsec
;
69 ce
->mtime
.sec
= st
->st_mtime
;
70 ce
->mtime
.nsec
= st
->st_mtim
.tv_nsec
;
71 ce
->st_dev
= st
->st_dev
;
72 ce
->st_ino
= st
->st_ino
;
73 ce
->st_uid
= st
->st_uid
;
74 ce
->st_gid
= st
->st_gid
;
77 static int add_file_to_cache(char *path
)
80 struct cache_entry
*ce
;
84 fd
= open(path
, O_RDONLY
);
86 if (errno
== ENOENT
) {
88 return remove_file_from_cache(path
);
92 if (fstat(fd
, &st
) < 0) {
96 namelen
= strlen(path
);
97 size
= cache_entry_size(namelen
);
100 memcpy(ce
->name
, path
, namelen
);
101 fill_stat_cache_info(ce
, &st
);
102 ce
->st_mode
= st
.st_mode
;
103 ce
->st_size
= st
.st_size
;
104 ce
->namelen
= namelen
;
106 if (index_fd(path
, namelen
, ce
, fd
, &st
) < 0)
109 return add_cache_entry(ce
, allow_add
);
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
);
148 * "refresh" does not calculate a new sha1 file or bring the
149 * cache up-to-date for mode/content changes. But what it
150 * _does_ do is to "re-match" the stat information of a file
151 * with the cache, so that you can refresh the cache for a
152 * file that hasn't been changed but where the stat entry is
155 * For example, you'd want to do this after doing a "read-tree",
156 * to link up the stat cache details with the proper files.
158 static struct cache_entry
*refresh_entry(struct cache_entry
*ce
)
161 struct cache_entry
*updated
;
164 if (stat(ce
->name
, &st
) < 0)
167 changed
= cache_match_stat(ce
, &st
);
172 * If the mode has changed, there's no point in trying
173 * to refresh the entry - it's not going to match
175 if (changed
& MODE_CHANGED
)
178 if (compare_data(ce
, st
.st_size
))
182 updated
= malloc(size
);
183 memcpy(updated
, ce
, size
);
184 fill_stat_cache_info(updated
, &st
);
185 updated
->st_size
= st
.st_size
;
189 static void refresh_cache(void)
193 for (i
= 0; i
< active_nr
; i
++) {
194 struct cache_entry
*ce
= active_cache
[i
];
195 struct cache_entry
*new = refresh_entry(ce
);
198 printf("%s: needs update\n", ce
->name
);
201 active_cache
[i
] = new;
206 * We fundamentally don't like some paths: we don't want
207 * dot or dot-dot anywhere, and in fact, we don't even want
208 * any other dot-files (.dircache or anything else). They
209 * are hidden, for chist sake.
211 * Also, we don't want double slashes or slashes at the
212 * end that can make pathnames ambiguous.
214 static int verify_path(char *path
)
225 if (c
!= '/' && c
!= '.' && c
!= '\0')
233 int main(int argc
, char **argv
)
235 int i
, newfd
, entries
;
236 int allow_options
= 1;
238 entries
= read_cache();
240 perror("cache corrupted");
244 newfd
= open(".dircache/index.lock", O_RDWR
| O_CREAT
| O_EXCL
, 0600);
246 perror("unable to create new cachefile");
249 for (i
= 1 ; i
< argc
; i
++) {
250 char *path
= argv
[i
];
252 if (allow_options
&& *path
== '-') {
253 if (!strcmp(path
, "--")) {
257 if (!strcmp(path
, "--add")) {
261 if (!strcmp(path
, "--remove")) {
265 if (!strcmp(path
, "--refresh")) {
269 usage("unknown option %s", path
);
271 if (!verify_path(path
)) {
272 fprintf(stderr
, "Ignoring path %s\n", argv
[i
]);
275 if (add_file_to_cache(path
)) {
276 fprintf(stderr
, "Unable to add %s to database\n", path
);
280 if (!write_cache(newfd
, active_cache
, active_nr
) && !rename(".dircache/index.lock", ".dircache/index"))
283 unlink(".dircache/index.lock");