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 unsigned long size
= st
->st_size
;
21 int max_out_bytes
= namelen
+ size
+ 200;
22 void *out
= malloc(max_out_bytes
);
23 void *metadata
= malloc(namelen
+ 200);
29 in
= mmap(NULL
, size
, PROT_READ
, MAP_PRIVATE
, fd
, 0);
31 if (!out
|| (int)(long)in
== -1)
34 memset(&stream
, 0, sizeof(stream
));
35 deflateInit(&stream
, Z_BEST_COMPRESSION
);
38 * ASCII size + nul byte
40 stream
.next_in
= metadata
;
41 stream
.avail_in
= 1+sprintf(metadata
, "blob %lu", size
);
42 stream
.next_out
= out
;
43 stream
.avail_out
= max_out_bytes
;
44 while (deflate(&stream
, 0) == Z_OK
)
51 stream
.avail_in
= size
;
52 while (deflate(&stream
, Z_FINISH
) == Z_OK
)
58 SHA1_Update(&c
, out
, stream
.total_out
);
59 SHA1_Final(ce
->sha1
, &c
);
61 return write_sha1_buffer(ce
->sha1
, out
, stream
.total_out
);
65 * This only updates the "non-critical" parts of the directory
66 * cache, ie the parts that aren't tracked by GIT, and only used
67 * to validate the cache.
69 static void fill_stat_cache_info(struct cache_entry
*ce
, struct stat
*st
)
71 ce
->ctime
.sec
= st
->st_ctime
;
73 ce
->ctime
.nsec
= st
->st_ctim
.tv_nsec
;
75 ce
->mtime
.sec
= st
->st_mtime
;
77 ce
->mtime
.nsec
= st
->st_mtim
.tv_nsec
;
79 ce
->st_dev
= st
->st_dev
;
80 ce
->st_ino
= st
->st_ino
;
81 ce
->st_uid
= st
->st_uid
;
82 ce
->st_gid
= st
->st_gid
;
85 static int add_file_to_cache(char *path
)
88 struct cache_entry
*ce
;
92 fd
= open(path
, O_RDONLY
);
94 if (errno
== ENOENT
) {
96 return remove_file_from_cache(path
);
100 if (fstat(fd
, &st
) < 0) {
104 namelen
= strlen(path
);
105 size
= cache_entry_size(namelen
);
108 memcpy(ce
->name
, path
, namelen
);
109 fill_stat_cache_info(ce
, &st
);
110 ce
->st_mode
= st
.st_mode
;
111 ce
->st_size
= st
.st_size
;
112 ce
->namelen
= namelen
;
114 if (index_fd(path
, namelen
, ce
, fd
, &st
) < 0)
117 return add_cache_entry(ce
, allow_add
);
120 static int match_data(int fd
, void *buffer
, unsigned long size
)
124 int ret
= read(fd
, compare
, sizeof(compare
));
126 if (ret
<= 0 || ret
> size
|| memcmp(buffer
, compare
, ret
))
134 static int compare_data(struct cache_entry
*ce
, unsigned long expected_size
)
137 int fd
= open(ce
->name
, O_RDONLY
);
144 buffer
= read_sha1_file(ce
->sha1
, type
, &size
);
146 if (size
== expected_size
&& !strcmp(type
, "blob"))
147 match
= match_data(fd
, buffer
, size
);
156 * "refresh" does not calculate a new sha1 file or bring the
157 * cache up-to-date for mode/content changes. But what it
158 * _does_ do is to "re-match" the stat information of a file
159 * with the cache, so that you can refresh the cache for a
160 * file that hasn't been changed but where the stat entry is
163 * For example, you'd want to do this after doing a "read-tree",
164 * to link up the stat cache details with the proper files.
166 static struct cache_entry
*refresh_entry(struct cache_entry
*ce
)
169 struct cache_entry
*updated
;
172 if (stat(ce
->name
, &st
) < 0)
175 changed
= cache_match_stat(ce
, &st
);
180 * If the mode has changed, there's no point in trying
181 * to refresh the entry - it's not going to match
183 if (changed
& MODE_CHANGED
)
186 if (compare_data(ce
, st
.st_size
))
190 updated
= malloc(size
);
191 memcpy(updated
, ce
, size
);
192 fill_stat_cache_info(updated
, &st
);
193 updated
->st_size
= st
.st_size
;
197 static void refresh_cache(void)
201 for (i
= 0; i
< active_nr
; i
++) {
202 struct cache_entry
*ce
= active_cache
[i
];
203 struct cache_entry
*new = refresh_entry(ce
);
206 printf("%s: needs update\n", ce
->name
);
209 active_cache
[i
] = new;
214 * We fundamentally don't like some paths: we don't want
215 * dot or dot-dot anywhere, and in fact, we don't even want
216 * any other dot-files (.git or anything else). They
217 * are hidden, for chist sake.
219 * Also, we don't want double slashes or slashes at the
220 * end that can make pathnames ambiguous.
222 static int verify_path(char *path
)
233 if (c
!= '/' && c
!= '.' && c
!= '\0')
241 static int remove_lock
= 0;
243 static void remove_lock_file(void)
246 unlink(".git/index.lock");
249 int main(int argc
, char **argv
)
251 int i
, newfd
, entries
;
252 int allow_options
= 1;
254 newfd
= open(".git/index.lock", O_RDWR
| O_CREAT
| O_EXCL
, 0600);
256 die("unable to create new cachefile");
258 atexit(remove_lock_file
);
261 entries
= read_cache();
263 die("cache corrupted");
265 for (i
= 1 ; i
< argc
; i
++) {
266 char *path
= argv
[i
];
268 if (allow_options
&& *path
== '-') {
269 if (!strcmp(path
, "--")) {
273 if (!strcmp(path
, "--add")) {
277 if (!strcmp(path
, "--remove")) {
281 if (!strcmp(path
, "--refresh")) {
285 die("unknown option %s", path
);
287 if (!verify_path(path
)) {
288 fprintf(stderr
, "Ignoring path %s\n", argv
[i
]);
291 if (add_file_to_cache(path
))
292 die("Unable to add %s to database", path
);
294 if (write_cache(newfd
, active_cache
, active_nr
) ||
295 rename(".git/index.lock", ".git/index"))
296 die("Unable to write new cachefile");