Add git-update-cache --replace option.
[git/git.git] / update-cache.c
CommitLineData
8bc9a0c7
LT
1/*
2 * GIT - The information manager from hell
3 *
4 * Copyright (C) Linus Torvalds, 2005
5 */
f2a19340 6#include <signal.h>
e83c5163
LT
7#include "cache.h"
8
121481ab
LT
9/*
10 * Default to not allowing changes to the list of files. The
11 * tool doesn't actually care, but this makes it harder to add
12 * files to the revision control by mistake by doing something
13 * like "update-cache *" and suddenly having all the object
14 * files be revision controlled.
15 */
192268c1 16static int allow_add = 0, allow_remove = 0, allow_replace = 0, not_new = 0;
c6e007b0
JB
17
18/* Three functions to allow overloaded pointer return; see linux/err.h */
19static inline void *ERR_PTR(long error)
20{
21 return (void *) error;
22}
23
24static inline long PTR_ERR(const void *ptr)
25{
26 return (long) ptr;
27}
28
29static inline long IS_ERR(const void *ptr)
30{
31 return (unsigned long)ptr > (unsigned long)-1000L;
32}
121481ab 33
711cf3a0
LT
34/*
35 * This only updates the "non-critical" parts of the directory
36 * cache, ie the parts that aren't tracked by GIT, and only used
37 * to validate the cache.
38 */
39static void fill_stat_cache_info(struct cache_entry *ce, struct stat *st)
40{
ccc4feb5
LT
41 ce->ce_ctime.sec = htonl(st->st_ctime);
42 ce->ce_mtime.sec = htonl(st->st_mtime);
5ade8628 43#ifdef NSEC
ccc4feb5
LT
44 ce->ce_ctime.nsec = htonl(st->st_ctim.tv_nsec);
45 ce->ce_mtime.nsec = htonl(st->st_mtim.tv_nsec);
5ade8628 46#endif
ccc4feb5
LT
47 ce->ce_dev = htonl(st->st_dev);
48 ce->ce_ino = htonl(st->st_ino);
49 ce->ce_uid = htonl(st->st_uid);
50 ce->ce_gid = htonl(st->st_gid);
51 ce->ce_size = htonl(st->st_size);
711cf3a0
LT
52}
53
ee267527 54static int add_file_to_cache(char *path)
e83c5163 55{
192268c1 56 int size, namelen, option;
e83c5163
LT
57 struct cache_entry *ce;
58 struct stat st;
59 int fd;
ffbe1add 60 char *target;
e83c5163 61
8ae0a8c5 62 if (lstat(path, &st) < 0) {
e2a669bb 63 if (errno == ENOENT || errno == ENOTDIR) {
121481ab
LT
64 if (allow_remove)
65 return remove_file_from_cache(path);
66 }
e83c5163
LT
67 return -1;
68 }
e83c5163
LT
69 namelen = strlen(path);
70 size = cache_entry_size(namelen);
812666c8 71 ce = xmalloc(size);
e83c5163
LT
72 memset(ce, 0, size);
73 memcpy(ce->name, path, namelen);
711cf3a0 74 fill_stat_cache_info(ce, &st);
e4479470 75 ce->ce_mode = create_ce_mode(st.st_mode);
f5cabd13 76 ce->ce_flags = htons(namelen);
8ae0a8c5
KS
77 switch (st.st_mode & S_IFMT) {
78 case S_IFREG:
79 fd = open(path, O_RDONLY);
80 if (fd < 0)
81 return -1;
82 if (index_fd(ce->sha1, fd, &st) < 0)
83 return -1;
84 break;
85 case S_IFLNK:
ffbe1add
KS
86 target = xmalloc(st.st_size+1);
87 if (readlink(path, target, st.st_size+1) != st.st_size) {
88 free(target);
8ae0a8c5 89 return -1;
ffbe1add
KS
90 }
91 if (write_sha1_file(target, st.st_size, "blob", ce->sha1))
8ae0a8c5 92 return -1;
ffbe1add 93 free(target);
8ae0a8c5
KS
94 break;
95 default:
e83c5163 96 return -1;
8ae0a8c5 97 }
192268c1
JH
98 option = allow_add ? ADD_CACHE_OK_TO_ADD : 0;
99 option |= allow_replace ? ADD_CACHE_OK_TO_REPLACE : 0;
100 return add_cache_entry(ce, option);
121481ab
LT
101}
102
711cf3a0
LT
103static int match_data(int fd, void *buffer, unsigned long size)
104{
105 while (size) {
106 char compare[1024];
107 int ret = read(fd, compare, sizeof(compare));
108
109 if (ret <= 0 || ret > size || memcmp(buffer, compare, ret))
110 return -1;
111 size -= ret;
112 buffer += ret;
113 }
114 return 0;
115}
116
32d197f1 117static int compare_data(struct cache_entry *ce, unsigned long expected_size)
121481ab 118{
711cf3a0
LT
119 int match = -1;
120 int fd = open(ce->name, O_RDONLY);
121
122 if (fd >= 0) {
123 void *buffer;
124 unsigned long size;
125 char type[10];
126
127 buffer = read_sha1_file(ce->sha1, type, &size);
128 if (buffer) {
32d197f1 129 if (size == expected_size && !strcmp(type, "blob"))
711cf3a0
LT
130 match = match_data(fd, buffer, size);
131 free(buffer);
132 }
133 close(fd);
134 }
135 return match;
136}
137
ffbe1add
KS
138static int compare_link(struct cache_entry *ce, unsigned long expected_size)
139{
140 int match = -1;
141 char *target;
142 void *buffer;
143 unsigned long size;
144 char type[10];
145 int len;
146
147 target = xmalloc(expected_size);
148 len = readlink(ce->name, target, expected_size);
149 if (len != expected_size) {
150 free(target);
151 return -1;
152 }
153 buffer = read_sha1_file(ce->sha1, type, &size);
154 if (!buffer) {
155 free(target);
156 return -1;
157 }
158 if (size == expected_size)
159 match = memcmp(buffer, target, size);
160 free(buffer);
161 free(target);
162 return match;
163}
164
711cf3a0
LT
165/*
166 * "refresh" does not calculate a new sha1 file or bring the
167 * cache up-to-date for mode/content changes. But what it
168 * _does_ do is to "re-match" the stat information of a file
169 * with the cache, so that you can refresh the cache for a
170 * file that hasn't been changed but where the stat entry is
171 * out of date.
172 *
173 * For example, you'd want to do this after doing a "read-tree",
174 * to link up the stat cache details with the proper files.
175 */
176static struct cache_entry *refresh_entry(struct cache_entry *ce)
177{
178 struct stat st;
179 struct cache_entry *updated;
180 int changed, size;
181
8ae0a8c5 182 if (lstat(ce->name, &st) < 0)
c6e007b0 183 return ERR_PTR(-errno);
711cf3a0
LT
184
185 changed = cache_match_stat(ce, &st);
186 if (!changed)
187 return ce;
188
121481ab 189 /*
8ae0a8c5 190 * If the mode or type has changed, there's no point in trying
711cf3a0 191 * to refresh the entry - it's not going to match
121481ab 192 */
8ae0a8c5 193 if (changed & (MODE_CHANGED | TYPE_CHANGED))
c6e007b0 194 return ERR_PTR(-EINVAL);
711cf3a0 195
ffbe1add
KS
196 switch (st.st_mode & S_IFMT) {
197 case S_IFREG:
198 if (compare_data(ce, st.st_size))
199 return ERR_PTR(-EINVAL);
200 break;
201 case S_IFLNK:
202 if (compare_link(ce, st.st_size))
203 return ERR_PTR(-EINVAL);
204 break;
205 default:
c6e007b0 206 return ERR_PTR(-EINVAL);
ffbe1add 207 }
711cf3a0
LT
208
209 size = ce_size(ce);
812666c8 210 updated = xmalloc(size);
711cf3a0
LT
211 memcpy(updated, ce, size);
212 fill_stat_cache_info(updated, &st);
213 return updated;
121481ab
LT
214}
215
90535218 216static int refresh_cache(void)
121481ab
LT
217{
218 int i;
90535218 219 int has_errors = 0;
121481ab 220
711cf3a0 221 for (i = 0; i < active_nr; i++) {
1bc992ac
JH
222 struct cache_entry *ce, *new;
223 ce = active_cache[i];
224 if (ce_stage(ce)) {
225 printf("%s: needs merge\n", ce->name);
90535218 226 has_errors = 1;
1bc992ac
JH
227 while ((i < active_nr) &&
228 ! strcmp(active_cache[i]->name, ce->name))
229 i++;
230 i--;
231 continue;
232 }
711cf3a0 233
1bc992ac 234 new = refresh_entry(ce);
c6e007b0 235 if (IS_ERR(new)) {
90535218 236 if (!(not_new && PTR_ERR(new) == -ENOENT)) {
c6e007b0 237 printf("%s: needs update\n", ce->name);
90535218
JH
238 has_errors = 1;
239 }
711cf3a0
LT
240 continue;
241 }
ee267527 242 active_cache_changed = 1;
711cf3a0
LT
243 active_cache[i] = new;
244 }
90535218 245 return has_errors;
e83c5163
LT
246}
247
e83c5163
LT
248/*
249 * We fundamentally don't like some paths: we don't want
250 * dot or dot-dot anywhere, and in fact, we don't even want
4bb04f21 251 * any other dot-files (.git or anything else). They
e83c5163
LT
252 * are hidden, for chist sake.
253 *
254 * Also, we don't want double slashes or slashes at the
aebb2679 255 * end that can make pathnames ambiguous.
e83c5163
LT
256 */
257static int verify_path(char *path)
258{
259 char c;
260
261 goto inside;
262 for (;;) {
263 if (!c)
264 return 1;
265 if (c == '/') {
266inside:
267 c = *path++;
268 if (c != '/' && c != '.' && c != '\0')
269 continue;
270 return 0;
271 }
272 c = *path++;
273 }
274}
275
9945d980
LT
276static int add_cacheinfo(char *arg1, char *arg2, char *arg3)
277{
192268c1 278 int size, len, option;
9945d980
LT
279 unsigned int mode;
280 unsigned char sha1[20];
281 struct cache_entry *ce;
282
283 if (sscanf(arg1, "%o", &mode) != 1)
284 return -1;
9945d980
LT
285 if (get_sha1_hex(arg2, sha1))
286 return -1;
9945d980
LT
287 if (!verify_path(arg3))
288 return -1;
9945d980
LT
289
290 len = strlen(arg3);
291 size = cache_entry_size(len);
812666c8 292 ce = xmalloc(size);
9945d980
LT
293 memset(ce, 0, size);
294
295 memcpy(ce->sha1, sha1, 20);
296 memcpy(ce->name, arg3, len);
f5cabd13 297 ce->ce_flags = htons(len);
e4479470 298 ce->ce_mode = create_ce_mode(mode);
192268c1
JH
299 option = allow_add ? ADD_CACHE_OK_TO_ADD : 0;
300 option |= allow_replace ? ADD_CACHE_OK_TO_REPLACE : 0;
301 return add_cache_entry(ce, option);
9945d980
LT
302}
303
bb233d69 304static const char *lockfile_name = NULL;
9614b8dc
LT
305
306static void remove_lock_file(void)
307{
bb233d69
LT
308 if (lockfile_name)
309 unlink(lockfile_name);
9614b8dc
LT
310}
311
f2a19340
LT
312static void remove_lock_file_on_signal(int signo)
313{
314 remove_lock_file();
315}
316
e83c5163
LT
317int main(int argc, char **argv)
318{
90535218 319 int i, newfd, entries, has_errors = 0;
121481ab 320 int allow_options = 1;
bb233d69
LT
321 static char lockfile[MAXPATHLEN+1];
322 const char *indexfile = get_index_file();
e83c5163 323
bb233d69
LT
324 snprintf(lockfile, sizeof(lockfile), "%s.lock", indexfile);
325
326 newfd = open(lockfile, O_RDWR | O_CREAT | O_EXCL, 0600);
9614b8dc 327 if (newfd < 0)
2de381f9 328 die("unable to create new cachefile");
9614b8dc 329
f2a19340 330 signal(SIGINT, remove_lock_file_on_signal);
9614b8dc 331 atexit(remove_lock_file);
bb233d69 332 lockfile_name = lockfile;
9614b8dc 333
e83c5163 334 entries = read_cache();
9614b8dc 335 if (entries < 0)
2de381f9 336 die("cache corrupted");
e83c5163 337
e83c5163
LT
338 for (i = 1 ; i < argc; i++) {
339 char *path = argv[i];
121481ab
LT
340
341 if (allow_options && *path == '-') {
342 if (!strcmp(path, "--")) {
343 allow_options = 0;
344 continue;
345 }
346 if (!strcmp(path, "--add")) {
347 allow_add = 1;
348 continue;
349 }
192268c1
JH
350 if (!strcmp(path, "--replace")) {
351 allow_replace = 1;
352 continue;
353 }
121481ab
LT
354 if (!strcmp(path, "--remove")) {
355 allow_remove = 1;
356 continue;
357 }
358 if (!strcmp(path, "--refresh")) {
90535218 359 has_errors |= refresh_cache();
121481ab
LT
360 continue;
361 }
9945d980
LT
362 if (!strcmp(path, "--cacheinfo")) {
363 if (i+3 >= argc || add_cacheinfo(argv[i+1], argv[i+2], argv[i+3]))
364 die("update-cache: --cacheinfo <mode> <sha1> <path>");
365 i += 3;
366 continue;
367 }
0ff5bf7c
JH
368 if (!strcmp(path, "--force-remove")) {
369 if (argc <= i + 1)
370 die("update-cache: --force-remove <path>");
371 if (remove_file_from_cache(argv[i+1]))
372 die("update-cache: --force-remove cannot remove %s", argv[i+1]);
373 i++;
374 continue;
375 }
376
c6e007b0
JB
377 if (!strcmp(path, "--ignore-missing")) {
378 not_new = 1;
379 continue;
380 }
2de381f9 381 die("unknown option %s", path);
121481ab 382 }
e83c5163
LT
383 if (!verify_path(path)) {
384 fprintf(stderr, "Ignoring path %s\n", argv[i]);
385 continue;
386 }
9614b8dc 387 if (add_file_to_cache(path))
2de381f9 388 die("Unable to add %s to database", path);
e83c5163 389 }
ee267527 390 if (write_cache(newfd, active_cache, active_nr) || rename(lockfile, indexfile))
2de381f9 391 die("Unable to write new cachefile");
9614b8dc 392
bb233d69 393 lockfile_name = NULL;
c4b83e61 394 return has_errors ? 1 : 0;
e83c5163 395}