blob: d10dfd9430c1329d4e7c4bc0082317375a9b4cf5 [file] [log] [blame]
Linus Torvalds8bc9a0c2005-04-07 15:16:10 -07001/*
2 * GIT - The information manager from hell
3 *
4 * Copyright (C) Linus Torvalds, 2005
5 */
Linus Torvaldse83c5162005-04-07 15:13:13 -07006#include "cache.h"
7
Linus Torvalds121481a2005-04-10 11:32:54 -07008/*
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
Junio C Hamano215a7ad2005-09-07 17:26:23 -070012 * like "git-update-index *" and suddenly having all the object
Linus Torvalds121481a2005-04-10 11:32:54 -070013 * files be revision controlled.
14 */
Bryan Larsendf6e1512005-07-08 16:52:12 -070015static int allow_add = 0, allow_remove = 0, allow_replace = 0, not_new = 0, quiet = 0, info_only = 0;
Petr Baudis9b63f502005-05-31 18:52:43 +020016static int force_remove;
James Bottomleyc6e007b2005-04-24 15:14:16 -070017
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}
Linus Torvalds121481a2005-04-10 11:32:54 -070033
Linus Torvaldsee267522005-05-06 16:48:43 -070034static int add_file_to_cache(char *path)
Linus Torvaldse83c5162005-04-07 15:13:13 -070035{
Junio C Hamano4c5abf42005-05-08 00:05:18 -070036 int size, namelen, option, status;
Linus Torvaldse83c5162005-04-07 15:13:13 -070037 struct cache_entry *ce;
38 struct stat st;
39 int fd;
Kay Sieversffbe1ad2005-05-06 15:45:01 +020040 char *target;
Linus Torvaldse83c5162005-04-07 15:13:13 -070041
Junio C Hamano4c5abf42005-05-08 00:05:18 -070042 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,
Junio C Hamano215a7ad2005-09-07 17:26:23 -070047 * "git-update-index --remove path" would not work.
Junio C Hamano4c5abf42005-05-08 00:05:18 -070048 * --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.
51 */
52 if (status == 0 || (errno == ENOENT || errno == ENOTDIR)) {
Linus Torvalds121481a2005-04-10 11:32:54 -070053 if (allow_remove)
54 return remove_file_from_cache(path);
55 }
Amos Waterland89bc8c72005-09-01 09:13:50 -050056 if (0 == status)
57 return error("%s: is a directory", path);
58 else
59 return error("lstat(\"%s\"): %s", path,
60 strerror(errno));
Linus Torvaldse83c5162005-04-07 15:13:13 -070061 }
Linus Torvaldse83c5162005-04-07 15:13:13 -070062 namelen = strlen(path);
63 size = cache_entry_size(namelen);
Christopher Li812666c2005-04-26 12:00:58 -070064 ce = xmalloc(size);
Linus Torvaldse83c5162005-04-07 15:13:13 -070065 memset(ce, 0, size);
66 memcpy(ce->name, path, namelen);
Linus Torvalds711cf3a2005-04-11 09:39:21 -070067 fill_stat_cache_info(ce, &st);
Linus Torvaldse4479472005-04-16 22:26:31 -070068 ce->ce_mode = create_ce_mode(st.st_mode);
Linus Torvaldsf5cabd12005-04-15 21:45:38 -070069 ce->ce_flags = htons(namelen);
Kay Sievers8ae0a8c2005-05-05 14:38:25 +020070 switch (st.st_mode & S_IFMT) {
71 case S_IFREG:
72 fd = open(path, O_RDONLY);
73 if (fd < 0)
74 return -1;
Bryan Larsendf6e1512005-07-08 16:52:12 -070075 if (index_fd(ce->sha1, fd, &st, !info_only, NULL) < 0)
Kay Sievers8ae0a8c2005-05-05 14:38:25 +020076 return -1;
77 break;
78 case S_IFLNK:
Kay Sieversffbe1ad2005-05-06 15:45:01 +020079 target = xmalloc(st.st_size+1);
80 if (readlink(path, target, st.st_size+1) != st.st_size) {
81 free(target);
Kay Sievers8ae0a8c2005-05-05 14:38:25 +020082 return -1;
Kay Sieversffbe1ad2005-05-06 15:45:01 +020083 }
Bryan Larsendf6e1512005-07-08 16:52:12 -070084 if (info_only) {
85 unsigned char hdr[50];
86 int hdrlen;
87 write_sha1_file_prepare(target, st.st_size, "blob",
88 ce->sha1, hdr, &hdrlen);
89 } else if (write_sha1_file(target, st.st_size, "blob", ce->sha1))
Kay Sievers8ae0a8c2005-05-05 14:38:25 +020090 return -1;
Kay Sieversffbe1ad2005-05-06 15:45:01 +020091 free(target);
Kay Sievers8ae0a8c2005-05-05 14:38:25 +020092 break;
93 default:
Linus Torvaldse83c5162005-04-07 15:13:13 -070094 return -1;
Kay Sievers8ae0a8c2005-05-05 14:38:25 +020095 }
Junio C Hamano192268c2005-05-07 21:55:21 -070096 option = allow_add ? ADD_CACHE_OK_TO_ADD : 0;
97 option |= allow_replace ? ADD_CACHE_OK_TO_REPLACE : 0;
98 return add_cache_entry(ce, option);
Linus Torvalds121481a2005-04-10 11:32:54 -070099}
100
Bryan Larsen69a97f12005-07-08 16:52:28 -0700101static int compare_data(struct cache_entry *ce, struct stat *st)
Linus Torvalds711cf3a2005-04-11 09:39:21 -0700102{
103 int match = -1;
104 int fd = open(ce->name, O_RDONLY);
105
106 if (fd >= 0) {
Bryan Larsen69a97f12005-07-08 16:52:28 -0700107 unsigned char sha1[20];
108 if (!index_fd(sha1, fd, st, 0, NULL))
109 match = memcmp(sha1, ce->sha1, 20);
Linus Torvalds711cf3a2005-04-11 09:39:21 -0700110 close(fd);
111 }
112 return match;
113}
114
Kay Sieversffbe1ad2005-05-06 15:45:01 +0200115static int compare_link(struct cache_entry *ce, unsigned long expected_size)
116{
117 int match = -1;
118 char *target;
119 void *buffer;
120 unsigned long size;
121 char type[10];
122 int len;
123
124 target = xmalloc(expected_size);
125 len = readlink(ce->name, target, expected_size);
126 if (len != expected_size) {
127 free(target);
128 return -1;
129 }
130 buffer = read_sha1_file(ce->sha1, type, &size);
131 if (!buffer) {
132 free(target);
133 return -1;
134 }
135 if (size == expected_size)
136 match = memcmp(buffer, target, size);
137 free(buffer);
138 free(target);
139 return match;
140}
141
Linus Torvalds711cf3a2005-04-11 09:39:21 -0700142/*
143 * "refresh" does not calculate a new sha1 file or bring the
144 * cache up-to-date for mode/content changes. But what it
145 * _does_ do is to "re-match" the stat information of a file
146 * with the cache, so that you can refresh the cache for a
147 * file that hasn't been changed but where the stat entry is
148 * out of date.
149 *
Alexey Nezhdanov667bb592005-05-19 15:17:16 +0400150 * For example, you'd want to do this after doing a "git-read-tree",
Linus Torvalds711cf3a2005-04-11 09:39:21 -0700151 * to link up the stat cache details with the proper files.
152 */
153static struct cache_entry *refresh_entry(struct cache_entry *ce)
154{
155 struct stat st;
156 struct cache_entry *updated;
157 int changed, size;
158
Kay Sievers8ae0a8c2005-05-05 14:38:25 +0200159 if (lstat(ce->name, &st) < 0)
James Bottomleyc6e007b2005-04-24 15:14:16 -0700160 return ERR_PTR(-errno);
Linus Torvalds711cf3a2005-04-11 09:39:21 -0700161
Brad Roberts5d728c82005-05-14 19:04:25 -0700162 changed = ce_match_stat(ce, &st);
Linus Torvalds711cf3a2005-04-11 09:39:21 -0700163 if (!changed)
164 return ce;
165
Linus Torvalds121481a2005-04-10 11:32:54 -0700166 /*
Kay Sievers8ae0a8c2005-05-05 14:38:25 +0200167 * If the mode or type has changed, there's no point in trying
Linus Torvalds711cf3a2005-04-11 09:39:21 -0700168 * to refresh the entry - it's not going to match
Linus Torvalds121481a2005-04-10 11:32:54 -0700169 */
Kay Sievers8ae0a8c2005-05-05 14:38:25 +0200170 if (changed & (MODE_CHANGED | TYPE_CHANGED))
James Bottomleyc6e007b2005-04-24 15:14:16 -0700171 return ERR_PTR(-EINVAL);
Linus Torvalds711cf3a2005-04-11 09:39:21 -0700172
Kay Sieversffbe1ad2005-05-06 15:45:01 +0200173 switch (st.st_mode & S_IFMT) {
174 case S_IFREG:
Bryan Larsen69a97f12005-07-08 16:52:28 -0700175 if (compare_data(ce, &st))
Kay Sieversffbe1ad2005-05-06 15:45:01 +0200176 return ERR_PTR(-EINVAL);
177 break;
178 case S_IFLNK:
179 if (compare_link(ce, st.st_size))
180 return ERR_PTR(-EINVAL);
181 break;
182 default:
James Bottomleyc6e007b2005-04-24 15:14:16 -0700183 return ERR_PTR(-EINVAL);
Kay Sieversffbe1ad2005-05-06 15:45:01 +0200184 }
Linus Torvalds711cf3a2005-04-11 09:39:21 -0700185
186 size = ce_size(ce);
Christopher Li812666c2005-04-26 12:00:58 -0700187 updated = xmalloc(size);
Linus Torvalds711cf3a2005-04-11 09:39:21 -0700188 memcpy(updated, ce, size);
189 fill_stat_cache_info(updated, &st);
190 return updated;
Linus Torvalds121481a2005-04-10 11:32:54 -0700191}
192
Junio C Hamano90535212005-05-01 21:07:40 -0700193static int refresh_cache(void)
Linus Torvalds121481a2005-04-10 11:32:54 -0700194{
195 int i;
Junio C Hamano90535212005-05-01 21:07:40 -0700196 int has_errors = 0;
Linus Torvalds121481a2005-04-10 11:32:54 -0700197
Linus Torvalds711cf3a2005-04-11 09:39:21 -0700198 for (i = 0; i < active_nr; i++) {
Junio C Hamano1bc992a2005-04-18 10:42:48 -0700199 struct cache_entry *ce, *new;
200 ce = active_cache[i];
201 if (ce_stage(ce)) {
202 printf("%s: needs merge\n", ce->name);
Junio C Hamano90535212005-05-01 21:07:40 -0700203 has_errors = 1;
Junio C Hamano1bc992a2005-04-18 10:42:48 -0700204 while ((i < active_nr) &&
205 ! strcmp(active_cache[i]->name, ce->name))
206 i++;
207 i--;
208 continue;
209 }
Linus Torvalds711cf3a2005-04-11 09:39:21 -0700210
Junio C Hamano1bc992a2005-04-18 10:42:48 -0700211 new = refresh_entry(ce);
James Bottomleyc6e007b2005-04-24 15:14:16 -0700212 if (IS_ERR(new)) {
Linus Torvalds0ed37152005-06-20 21:18:54 -0700213 if (not_new && PTR_ERR(new) == -ENOENT)
214 continue;
215 if (quiet)
216 continue;
217 printf("%s: needs update\n", ce->name);
218 has_errors = 1;
Linus Torvalds711cf3a2005-04-11 09:39:21 -0700219 continue;
220 }
Linus Torvaldsee267522005-05-06 16:48:43 -0700221 active_cache_changed = 1;
Petr Baudis62d046a2005-04-17 23:34:51 +0200222 /* You can NOT just free active_cache[i] here, since it
223 * might not be necessarily malloc()ed but can also come
224 * from mmap(). */
Linus Torvalds711cf3a2005-04-11 09:39:21 -0700225 active_cache[i] = new;
226 }
Junio C Hamano90535212005-05-01 21:07:40 -0700227 return has_errors;
Linus Torvaldse83c5162005-04-07 15:13:13 -0700228}
229
Linus Torvaldse83c5162005-04-07 15:13:13 -0700230/*
231 * We fundamentally don't like some paths: we don't want
Linus Torvalds320d3a12005-05-24 14:40:28 -0700232 * dot or dot-dot anywhere, and for obvious reasons don't
233 * want to recurse into ".git" either.
Linus Torvaldse83c5162005-04-07 15:13:13 -0700234 *
235 * Also, we don't want double slashes or slashes at the
Ingo Molnaraebb2672005-04-12 11:36:26 -0700236 * end that can make pathnames ambiguous.
Linus Torvaldse83c5162005-04-07 15:13:13 -0700237 */
Linus Torvalds320d3a12005-05-24 14:40:28 -0700238static int verify_dotfile(const char *rest)
239{
240 /*
241 * The first character was '.', but that
242 * has already been discarded, we now test
243 * the rest.
244 */
245 switch (*rest) {
246 /* "." is not allowed */
247 case '\0': case '/':
248 return 0;
249
250 /*
251 * ".git" followed by NUL or slash is bad. This
252 * shares the path end test with the ".." case.
253 */
254 case 'g':
255 if (rest[1] != 'i')
256 break;
257 if (rest[2] != 't')
258 break;
259 rest += 2;
260 /* fallthrough */
261 case '.':
262 if (rest[1] == '\0' || rest[1] == '/')
263 return 0;
264 }
265 return 1;
266}
267
Linus Torvaldse83c5162005-04-07 15:13:13 -0700268static int verify_path(char *path)
269{
270 char c;
271
272 goto inside;
273 for (;;) {
274 if (!c)
275 return 1;
276 if (c == '/') {
277inside:
278 c = *path++;
Linus Torvalds320d3a12005-05-24 14:40:28 -0700279 switch (c) {
280 default:
Linus Torvaldse83c5162005-04-07 15:13:13 -0700281 continue;
Linus Torvalds320d3a12005-05-24 14:40:28 -0700282 case '/': case '\0':
283 break;
284 case '.':
285 if (verify_dotfile(path))
286 continue;
287 }
Linus Torvaldse83c5162005-04-07 15:13:13 -0700288 return 0;
289 }
290 c = *path++;
291 }
292}
293
Linus Torvalds9945d982005-04-15 11:08:33 -0700294static int add_cacheinfo(char *arg1, char *arg2, char *arg3)
295{
Junio C Hamano192268c2005-05-07 21:55:21 -0700296 int size, len, option;
Linus Torvalds9945d982005-04-15 11:08:33 -0700297 unsigned int mode;
298 unsigned char sha1[20];
299 struct cache_entry *ce;
300
301 if (sscanf(arg1, "%o", &mode) != 1)
302 return -1;
Linus Torvalds9945d982005-04-15 11:08:33 -0700303 if (get_sha1_hex(arg2, sha1))
304 return -1;
Linus Torvalds9945d982005-04-15 11:08:33 -0700305 if (!verify_path(arg3))
306 return -1;
Linus Torvalds9945d982005-04-15 11:08:33 -0700307
308 len = strlen(arg3);
309 size = cache_entry_size(len);
Christopher Li812666c2005-04-26 12:00:58 -0700310 ce = xmalloc(size);
Linus Torvalds9945d982005-04-15 11:08:33 -0700311 memset(ce, 0, size);
312
313 memcpy(ce->sha1, sha1, 20);
314 memcpy(ce->name, arg3, len);
Linus Torvaldsf5cabd12005-04-15 21:45:38 -0700315 ce->ce_flags = htons(len);
Linus Torvaldse4479472005-04-16 22:26:31 -0700316 ce->ce_mode = create_ce_mode(mode);
Junio C Hamano192268c2005-05-07 21:55:21 -0700317 option = allow_add ? ADD_CACHE_OK_TO_ADD : 0;
318 option |= allow_replace ? ADD_CACHE_OK_TO_REPLACE : 0;
319 return add_cache_entry(ce, option);
Linus Torvalds9945d982005-04-15 11:08:33 -0700320}
321
Linus Torvaldse99d59f2005-05-20 11:46:10 -0700322static struct cache_file cache_file;
Linus Torvaldsf2a19342005-04-26 11:55:42 -0700323
Linus Torvaldse83c5162005-04-07 15:13:13 -0700324int main(int argc, char **argv)
325{
Junio C Hamano90535212005-05-01 21:07:40 -0700326 int i, newfd, entries, has_errors = 0;
Linus Torvalds121481a2005-04-10 11:32:54 -0700327 int allow_options = 1;
Linus Torvaldscfb0af12005-08-17 13:32:22 -0700328 const char *prefix = setup_git_directory();
Linus Torvaldse83c5162005-04-07 15:13:13 -0700329
Junio C Hamano415e96c2005-05-15 14:23:12 -0700330 newfd = hold_index_file_for_update(&cache_file, get_index_file());
Linus Torvalds9614b8d2005-04-11 15:39:26 -0700331 if (newfd < 0)
Petr Baudis2de381f2005-04-13 02:28:48 -0700332 die("unable to create new cachefile");
Linus Torvalds9614b8d2005-04-11 15:39:26 -0700333
Linus Torvalds9614b8d2005-04-11 15:39:26 -0700334 entries = read_cache();
335 if (entries < 0)
Petr Baudis2de381f2005-04-13 02:28:48 -0700336 die("cache corrupted");
Linus Torvalds9614b8d2005-04-11 15:39:26 -0700337
Linus Torvaldse83c5162005-04-07 15:13:13 -0700338 for (i = 1 ; i < argc; i++) {
339 char *path = argv[i];
Linus Torvalds121481a2005-04-10 11:32:54 -0700340
341 if (allow_options && *path == '-') {
342 if (!strcmp(path, "--")) {
343 allow_options = 0;
344 continue;
345 }
Linus Torvalds0ed37152005-06-20 21:18:54 -0700346 if (!strcmp(path, "-q")) {
347 quiet = 1;
348 continue;
349 }
Linus Torvalds121481a2005-04-10 11:32:54 -0700350 if (!strcmp(path, "--add")) {
351 allow_add = 1;
352 continue;
353 }
Junio C Hamano192268c2005-05-07 21:55:21 -0700354 if (!strcmp(path, "--replace")) {
355 allow_replace = 1;
356 continue;
357 }
Linus Torvalds121481a2005-04-10 11:32:54 -0700358 if (!strcmp(path, "--remove")) {
359 allow_remove = 1;
360 continue;
361 }
362 if (!strcmp(path, "--refresh")) {
Junio C Hamano90535212005-05-01 21:07:40 -0700363 has_errors |= refresh_cache();
Linus Torvalds121481a2005-04-10 11:32:54 -0700364 continue;
365 }
Linus Torvalds9945d982005-04-15 11:08:33 -0700366 if (!strcmp(path, "--cacheinfo")) {
Junio C Hamanob3f94c42005-05-08 15:31:33 -0700367 if (i+3 >= argc)
Junio C Hamano215a7ad2005-09-07 17:26:23 -0700368 die("git-update-index: --cacheinfo <mode> <sha1> <path>");
Junio C Hamanob3f94c42005-05-08 15:31:33 -0700369 if (add_cacheinfo(argv[i+1], argv[i+2], argv[i+3]))
Junio C Hamano215a7ad2005-09-07 17:26:23 -0700370 die("git-update-index: --cacheinfo cannot add %s", argv[i+3]);
Linus Torvalds9945d982005-04-15 11:08:33 -0700371 i += 3;
372 continue;
373 }
Bryan Larsendf6e1512005-07-08 16:52:12 -0700374 if (!strcmp(path, "--info-only")) {
375 info_only = 1;
376 continue;
377 }
Junio C Hamano0ff5bf72005-05-01 23:50:51 -0700378 if (!strcmp(path, "--force-remove")) {
Petr Baudis9b63f502005-05-31 18:52:43 +0200379 force_remove = 1;
Junio C Hamano0ff5bf72005-05-01 23:50:51 -0700380 continue;
381 }
382
James Bottomleyc6e007b2005-04-24 15:14:16 -0700383 if (!strcmp(path, "--ignore-missing")) {
384 not_new = 1;
385 continue;
386 }
Petr Baudis2de381f2005-04-13 02:28:48 -0700387 die("unknown option %s", path);
Linus Torvalds121481a2005-04-10 11:32:54 -0700388 }
Linus Torvaldscfb0af12005-08-17 13:32:22 -0700389 path = prefix_path(prefix, prefix ? strlen(prefix) : 0, path);
Linus Torvaldse83c5162005-04-07 15:13:13 -0700390 if (!verify_path(path)) {
391 fprintf(stderr, "Ignoring path %s\n", argv[i]);
392 continue;
393 }
Petr Baudis9b63f502005-05-31 18:52:43 +0200394 if (force_remove) {
395 if (remove_file_from_cache(path))
Junio C Hamano215a7ad2005-09-07 17:26:23 -0700396 die("git-update-index: --force-remove cannot remove %s", path);
Petr Baudis9b63f502005-05-31 18:52:43 +0200397 continue;
398 }
Linus Torvalds9614b8d2005-04-11 15:39:26 -0700399 if (add_file_to_cache(path))
Amos Waterland89bc8c72005-09-01 09:13:50 -0500400 die("Unable to add %s to database; maybe you want to use --add option?", path);
Linus Torvaldse83c5162005-04-07 15:13:13 -0700401 }
Junio C Hamano415e96c2005-05-15 14:23:12 -0700402 if (write_cache(newfd, active_cache, active_nr) ||
403 commit_index_file(&cache_file))
Petr Baudis2de381f2005-04-13 02:28:48 -0700404 die("Unable to write new cachefile");
Linus Torvalds9614b8d2005-04-11 15:39:26 -0700405
Junio C Hamanoc4b83e62005-05-05 15:29:06 -0700406 return has_errors ? 1 : 0;
Linus Torvaldse83c5162005-04-07 15:13:13 -0700407}