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 "git-update-cache *" and suddenly having all the object
13 * files be revision controlled.
15 static int allow_add = 0, allow_remove = 0, allow_replace = 0, not_new = 0, quiet = 0;
16 static int force_remove;
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 add_file_to_cache(char *path)
36 int size, namelen, option, status;
37 struct cache_entry *ce;
42 status = lstat(path, &st);
43 if (status < 0 || S_ISDIR(st.st_mode)) {
44 /* When we used to have "path" and now we want to add
45 * "path/file", we need a way to remove "path" before
46 * being able to add "path/file". However,
47 * "git-update-cache --remove path" would not work.
48 * --force-remove can be used but this is more user
49 * friendly, especially since we can do the opposite
50 * case just fine without --force-remove.
52 if (status == 0 || (errno == ENOENT || errno == ENOTDIR)) {
54 return remove_file_from_cache(path);
56 return error("open(\"%s\"): %s", path, strerror(errno));
58 namelen = strlen(path);
59 size = cache_entry_size(namelen);
62 memcpy(ce->name, path, namelen);
63 fill_stat_cache_info(ce, &st);
64 ce->ce_mode = create_ce_mode(st.st_mode);
65 ce->ce_flags = htons(namelen);
66 switch (st.st_mode & S_IFMT) {
68 fd = open(path, O_RDONLY);
71 if (index_fd(ce->sha1, fd, &st) < 0)
75 target = xmalloc(st.st_size+1);
76 if (readlink(path, target, st.st_size+1) != st.st_size) {
80 if (write_sha1_file(target, st.st_size, "blob", ce->sha1))
87 option = allow_add ? ADD_CACHE_OK_TO_ADD : 0;
88 option |= allow_replace ? ADD_CACHE_OK_TO_REPLACE : 0;
89 return add_cache_entry(ce, option);
92 static int match_data(int fd, void *buffer, unsigned long size)
96 int ret = read(fd, compare, sizeof(compare));
98 if (ret <= 0 || ret > size || memcmp(buffer, compare, ret))
106 static int compare_data(struct cache_entry *ce, unsigned long expected_size)
109 int fd = open(ce->name, O_RDONLY);
116 buffer = read_sha1_file(ce->sha1, type, &size);
118 if (size == expected_size && !strcmp(type, "blob"))
119 match = match_data(fd, buffer, size);
127 static int compare_link(struct cache_entry *ce, unsigned long expected_size)
136 target = xmalloc(expected_size);
137 len = readlink(ce->name, target, expected_size);
138 if (len != expected_size) {
142 buffer = read_sha1_file(ce->sha1, type, &size);
147 if (size == expected_size)
148 match = memcmp(buffer, target, size);
155 * "refresh" does not calculate a new sha1 file or bring the
156 * cache up-to-date for mode/content changes. But what it
157 * _does_ do is to "re-match" the stat information of a file
158 * with the cache, so that you can refresh the cache for a
159 * file that hasn't been changed but where the stat entry is
162 * For example, you'd want to do this after doing a "git-read-tree",
163 * to link up the stat cache details with the proper files.
165 static struct cache_entry *refresh_entry(struct cache_entry *ce)
168 struct cache_entry *updated;
171 if (lstat(ce->name, &st) < 0)
172 return ERR_PTR(-errno);
174 changed = ce_match_stat(ce, &st);
179 * If the mode or type has changed, there's no point in trying
180 * to refresh the entry - it's not going to match
182 if (changed & (MODE_CHANGED | TYPE_CHANGED))
183 return ERR_PTR(-EINVAL);
185 switch (st.st_mode & S_IFMT) {
187 if (compare_data(ce, st.st_size))
188 return ERR_PTR(-EINVAL);
191 if (compare_link(ce, st.st_size))
192 return ERR_PTR(-EINVAL);
195 return ERR_PTR(-EINVAL);
199 updated = xmalloc(size);
200 memcpy(updated, ce, size);
201 fill_stat_cache_info(updated, &st);
205 static int refresh_cache(void)
210 for (i = 0; i < active_nr; i++) {
211 struct cache_entry *ce, *new;
212 ce = active_cache[i];
214 printf("%s: needs merge\n", ce->name);
216 while ((i < active_nr) &&
217 ! strcmp(active_cache[i]->name, ce->name))
223 new = refresh_entry(ce);
225 if (not_new && PTR_ERR(new) == -ENOENT)
229 printf("%s: needs update\n", ce->name);
233 active_cache_changed = 1;
234 /* You can NOT just free active_cache[i] here, since it
235 * might not be necessarily malloc()ed but can also come
237 active_cache[i] = new;
243 * We fundamentally don't like some paths: we don't want
244 * dot or dot-dot anywhere, and for obvious reasons don't
245 * want to recurse into ".git" either.
247 * Also, we don't want double slashes or slashes at the
248 * end that can make pathnames ambiguous.
250 static int verify_dotfile(const char *rest)
253 * The first character was '.', but that
254 * has already been discarded, we now test
258 /* "." is not allowed */
263 * ".git" followed by NUL or slash is bad. This
264 * shares the path end test with the ".." case.
274 if (rest[1] == '\0' || rest[1] == '/')
280 static int verify_path(char *path)
297 if (verify_dotfile(path))
306 static int add_cacheinfo(char *arg1, char *arg2, char *arg3)
308 int size, len, option;
310 unsigned char sha1[20];
311 struct cache_entry *ce;
313 if (sscanf(arg1, "%o", &mode) != 1)
315 if (get_sha1_hex(arg2, sha1))
317 if (!verify_path(arg3))
321 size = cache_entry_size(len);
325 memcpy(ce->sha1, sha1, 20);
326 memcpy(ce->name, arg3, len);
327 ce->ce_flags = htons(len);
328 ce->ce_mode = create_ce_mode(mode);
329 option = allow_add ? ADD_CACHE_OK_TO_ADD : 0;
330 option |= allow_replace ? ADD_CACHE_OK_TO_REPLACE : 0;
331 return add_cache_entry(ce, option);
334 static struct cache_file cache_file;
336 int main(int argc, char **argv)
338 int i, newfd, entries, has_errors = 0;
339 int allow_options = 1;
341 newfd = hold_index_file_for_update(&cache_file, get_index_file());
343 die("unable to create new cachefile");
345 entries = read_cache();
347 die("cache corrupted");
349 for (i = 1 ; i < argc; i++) {
350 char *path = argv[i];
352 if (allow_options && *path == '-') {
353 if (!strcmp(path, "--")) {
357 if (!strcmp(path, "-q")) {
361 if (!strcmp(path, "--add")) {
365 if (!strcmp(path, "--replace")) {
369 if (!strcmp(path, "--remove")) {
373 if (!strcmp(path, "--refresh")) {
374 has_errors |= refresh_cache();
377 if (!strcmp(path, "--cacheinfo")) {
379 die("git-update-cache: --cacheinfo <mode> <sha1> <path>");
380 if (add_cacheinfo(argv[i+1], argv[i+2], argv[i+3]))
381 die("git-update-cache: --cacheinfo cannot add %s", argv[i+3]);
385 if (!strcmp(path, "--force-remove")) {
390 if (!strcmp(path, "--ignore-missing")) {
394 die("unknown option %s", path);
396 if (!verify_path(path)) {
397 fprintf(stderr, "Ignoring path %s\n", argv[i]);
401 if (remove_file_from_cache(path))
402 die("git-update-cache: --force-remove cannot remove %s", path);
405 if (add_file_to_cache(path))
406 die("Unable to add %s to database", path);
408 if (write_cache(newfd, active_cache, active_nr) ||
409 commit_index_file(&cache_file))
410 die("Unable to write new cachefile");
412 return has_errors ? 1 : 0;