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;
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 add_file_to_cache(char *path
)
35 int size
, namelen
, option
, status
;
36 struct cache_entry
*ce
;
41 status
= lstat(path
, &st
);
42 if (status
< 0 || S_ISDIR(st
.st_mode
)) {
43 /* When we used to have "path" and now we want to add
44 * "path/file", we need a way to remove "path" before
45 * being able to add "path/file". However,
46 * "git-update-cache --remove path" would not work.
47 * --force-remove can be used but this is more user
48 * friendly, especially since we can do the opposite
49 * case just fine without --force-remove.
51 if (status
== 0 || (errno
== ENOENT
|| errno
== ENOTDIR
)) {
53 return remove_file_from_cache(path
);
55 return error("open(\"%s\"): %s", path
, strerror(errno
));
57 namelen
= strlen(path
);
58 size
= cache_entry_size(namelen
);
61 memcpy(ce
->name
, path
, namelen
);
62 fill_stat_cache_info(ce
, &st
);
63 ce
->ce_mode
= create_ce_mode(st
.st_mode
);
64 ce
->ce_flags
= htons(namelen
);
65 switch (st
.st_mode
& S_IFMT
) {
67 fd
= open(path
, O_RDONLY
);
70 if (index_fd(ce
->sha1
, fd
, &st
) < 0)
74 target
= xmalloc(st
.st_size
+1);
75 if (readlink(path
, target
, st
.st_size
+1) != st
.st_size
) {
79 if (write_sha1_file(target
, st
.st_size
, "blob", ce
->sha1
))
86 option
= allow_add
? ADD_CACHE_OK_TO_ADD
: 0;
87 option
|= allow_replace
? ADD_CACHE_OK_TO_REPLACE
: 0;
88 return add_cache_entry(ce
, option
);
91 static int match_data(int fd
, void *buffer
, unsigned long size
)
95 int ret
= read(fd
, compare
, sizeof(compare
));
97 if (ret
<= 0 || ret
> size
|| memcmp(buffer
, compare
, ret
))
105 static int compare_data(struct cache_entry
*ce
, unsigned long expected_size
)
108 int fd
= open(ce
->name
, O_RDONLY
);
115 buffer
= read_sha1_file(ce
->sha1
, type
, &size
);
117 if (size
== expected_size
&& !strcmp(type
, "blob"))
118 match
= match_data(fd
, buffer
, size
);
126 static int compare_link(struct cache_entry
*ce
, unsigned long expected_size
)
135 target
= xmalloc(expected_size
);
136 len
= readlink(ce
->name
, target
, expected_size
);
137 if (len
!= expected_size
) {
141 buffer
= read_sha1_file(ce
->sha1
, type
, &size
);
146 if (size
== expected_size
)
147 match
= memcmp(buffer
, target
, size
);
154 * "refresh" does not calculate a new sha1 file or bring the
155 * cache up-to-date for mode/content changes. But what it
156 * _does_ do is to "re-match" the stat information of a file
157 * with the cache, so that you can refresh the cache for a
158 * file that hasn't been changed but where the stat entry is
161 * For example, you'd want to do this after doing a "git-read-tree",
162 * to link up the stat cache details with the proper files.
164 static struct cache_entry
*refresh_entry(struct cache_entry
*ce
)
167 struct cache_entry
*updated
;
170 if (lstat(ce
->name
, &st
) < 0)
171 return ERR_PTR(-errno
);
173 changed
= ce_match_stat(ce
, &st
);
178 * If the mode or type has changed, there's no point in trying
179 * to refresh the entry - it's not going to match
181 if (changed
& (MODE_CHANGED
| TYPE_CHANGED
))
182 return ERR_PTR(-EINVAL
);
184 switch (st
.st_mode
& S_IFMT
) {
186 if (compare_data(ce
, st
.st_size
))
187 return ERR_PTR(-EINVAL
);
190 if (compare_link(ce
, st
.st_size
))
191 return ERR_PTR(-EINVAL
);
194 return ERR_PTR(-EINVAL
);
198 updated
= xmalloc(size
);
199 memcpy(updated
, ce
, size
);
200 fill_stat_cache_info(updated
, &st
);
204 static int refresh_cache(void)
209 for (i
= 0; i
< active_nr
; i
++) {
210 struct cache_entry
*ce
, *new;
211 ce
= active_cache
[i
];
213 printf("%s: needs merge\n", ce
->name
);
215 while ((i
< active_nr
) &&
216 ! strcmp(active_cache
[i
]->name
, ce
->name
))
222 new = refresh_entry(ce
);
224 if (!(not_new
&& PTR_ERR(new) == -ENOENT
)) {
225 printf("%s: needs update\n", ce
->name
);
230 active_cache_changed
= 1;
231 /* You can NOT just free active_cache[i] here, since it
232 * might not be necessarily malloc()ed but can also come
234 active_cache
[i
] = new;
240 * We fundamentally don't like some paths: we don't want
241 * dot or dot-dot anywhere, and for obvious reasons don't
242 * want to recurse into ".git" either.
244 * Also, we don't want double slashes or slashes at the
245 * end that can make pathnames ambiguous.
247 static int verify_dotfile(const char *rest
)
250 * The first character was '.', but that
251 * has already been discarded, we now test
255 /* "." is not allowed */
260 * ".git" followed by NUL or slash is bad. This
261 * shares the path end test with the ".." case.
271 if (rest
[1] == '\0' || rest
[1] == '/')
277 static int verify_path(char *path
)
294 if (verify_dotfile(path
))
303 static int add_cacheinfo(char *arg1
, char *arg2
, char *arg3
)
305 int size
, len
, option
;
307 unsigned char sha1
[20];
308 struct cache_entry
*ce
;
310 if (sscanf(arg1
, "%o", &mode
) != 1)
312 if (get_sha1_hex(arg2
, sha1
))
314 if (!verify_path(arg3
))
318 size
= cache_entry_size(len
);
322 memcpy(ce
->sha1
, sha1
, 20);
323 memcpy(ce
->name
, arg3
, len
);
324 ce
->ce_flags
= htons(len
);
325 ce
->ce_mode
= create_ce_mode(mode
);
326 option
= allow_add
? ADD_CACHE_OK_TO_ADD
: 0;
327 option
|= allow_replace
? ADD_CACHE_OK_TO_REPLACE
: 0;
328 return add_cache_entry(ce
, option
);
331 static struct cache_file cache_file
;
333 int main(int argc
, char **argv
)
335 int i
, newfd
, entries
, has_errors
= 0;
336 int allow_options
= 1;
338 newfd
= hold_index_file_for_update(&cache_file
, get_index_file());
340 die("unable to create new cachefile");
342 entries
= read_cache();
344 die("cache corrupted");
346 for (i
= 1 ; i
< argc
; i
++) {
347 char *path
= argv
[i
];
349 if (allow_options
&& *path
== '-') {
350 if (!strcmp(path
, "--")) {
354 if (!strcmp(path
, "--add")) {
358 if (!strcmp(path
, "--replace")) {
362 if (!strcmp(path
, "--remove")) {
366 if (!strcmp(path
, "--refresh")) {
367 has_errors
|= refresh_cache();
370 if (!strcmp(path
, "--cacheinfo")) {
372 die("git-update-cache: --cacheinfo <mode> <sha1> <path>");
373 if (add_cacheinfo(argv
[i
+1], argv
[i
+2], argv
[i
+3]))
374 die("git-update-cache: --cacheinfo cannot add %s", argv
[i
+3]);
378 if (!strcmp(path
, "--force-remove")) {
380 die("git-update-cache: --force-remove <path>");
381 if (remove_file_from_cache(argv
[i
+1]))
382 die("git-update-cache: --force-remove cannot remove %s", argv
[i
+1]);
387 if (!strcmp(path
, "--ignore-missing")) {
391 die("unknown option %s", path
);
393 if (!verify_path(path
)) {
394 fprintf(stderr
, "Ignoring path %s\n", argv
[i
]);
397 if (add_file_to_cache(path
))
398 die("Unable to add %s to database", path
);
400 if (write_cache(newfd
, active_cache
, active_nr
) ||
401 commit_index_file(&cache_file
))
402 die("Unable to write new cachefile");
404 return has_errors
? 1 : 0;