]> git.ipfire.org Git - thirdparty/git.git/blame - update-index.c
[PATCH] fetch.c: Remove redundant TO_FETCH flag
[thirdparty/git.git] / update-index.c
CommitLineData
8bc9a0c7
LT
1/*
2 * GIT - The information manager from hell
3 *
4 * Copyright (C) Linus Torvalds, 2005
5 */
e83c5163
LT
6#include "cache.h"
7
121481ab
LT
8/*
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
215a7ad1 12 * like "git-update-index *" and suddenly having all the object
121481ab
LT
13 * files be revision controlled.
14 */
df6e1516 15static int allow_add = 0, allow_remove = 0, allow_replace = 0, not_new = 0, quiet = 0, info_only = 0;
9b63f501 16static int force_remove;
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
ee267527 34static int add_file_to_cache(char *path)
e83c5163 35{
4c5abf42 36 int size, namelen, option, status;
e83c5163
LT
37 struct cache_entry *ce;
38 struct stat st;
39 int fd;
ffbe1add 40 char *target;
e83c5163 41
4c5abf42
JH
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,
215a7ad1 47 * "git-update-index --remove path" would not work.
4c5abf42
JH
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.
51 */
52 if (status == 0 || (errno == ENOENT || errno == ENOTDIR)) {
34143b26
PB
53 if (allow_remove) {
54 if (remove_file_from_cache(path))
55 return error("%s: cannot remove from the index",
56 path);
57 else
58 return 0;
59 } else if (status < 0) {
60 return error("%s: does not exist and --remove not passed",
61 path);
62 }
121481ab 63 }
89bc8c78 64 if (0 == status)
34143b26
PB
65 return error("%s: is a directory - add files inside instead",
66 path);
89bc8c78
AW
67 else
68 return error("lstat(\"%s\"): %s", path,
69 strerror(errno));
e83c5163 70 }
e83c5163
LT
71 namelen = strlen(path);
72 size = cache_entry_size(namelen);
812666c8 73 ce = xmalloc(size);
e83c5163
LT
74 memset(ce, 0, size);
75 memcpy(ce->name, path, namelen);
711cf3a0 76 fill_stat_cache_info(ce, &st);
e4479470 77 ce->ce_mode = create_ce_mode(st.st_mode);
f5cabd13 78 ce->ce_flags = htons(namelen);
8ae0a8c5
KS
79 switch (st.st_mode & S_IFMT) {
80 case S_IFREG:
81 fd = open(path, O_RDONLY);
82 if (fd < 0)
34143b26 83 return error("open(\"%s\"): %s", path, strerror(errno));
df6e1516 84 if (index_fd(ce->sha1, fd, &st, !info_only, NULL) < 0)
34143b26 85 return error("%s: failed to insert into database", path);
8ae0a8c5
KS
86 break;
87 case S_IFLNK:
ffbe1add
KS
88 target = xmalloc(st.st_size+1);
89 if (readlink(path, target, st.st_size+1) != st.st_size) {
34143b26 90 char *errstr = strerror(errno);
ffbe1add 91 free(target);
34143b26
PB
92 return error("readlink(\"%s\"): %s", path,
93 errstr);
ffbe1add 94 }
df6e1516
BL
95 if (info_only) {
96 unsigned char hdr[50];
97 int hdrlen;
98 write_sha1_file_prepare(target, st.st_size, "blob",
99 ce->sha1, hdr, &hdrlen);
100 } else if (write_sha1_file(target, st.st_size, "blob", ce->sha1))
34143b26 101 return error("%s: failed to insert into database", path);
ffbe1add 102 free(target);
8ae0a8c5
KS
103 break;
104 default:
34143b26 105 return error("%s: unsupported file type", path);
8ae0a8c5 106 }
192268c1
JH
107 option = allow_add ? ADD_CACHE_OK_TO_ADD : 0;
108 option |= allow_replace ? ADD_CACHE_OK_TO_REPLACE : 0;
34143b26
PB
109 if (add_cache_entry(ce, option))
110 return error("%s: cannot add to the index - missing --add option?",
111 path);
112 return 0;
121481ab
LT
113}
114
69a97f1d 115static int compare_data(struct cache_entry *ce, struct stat *st)
121481ab 116{
711cf3a0
LT
117 int match = -1;
118 int fd = open(ce->name, O_RDONLY);
119
120 if (fd >= 0) {
69a97f1d
BL
121 unsigned char sha1[20];
122 if (!index_fd(sha1, fd, st, 0, NULL))
123 match = memcmp(sha1, ce->sha1, 20);
711cf3a0
LT
124 close(fd);
125 }
126 return match;
127}
128
ffbe1add
KS
129static int compare_link(struct cache_entry *ce, unsigned long expected_size)
130{
131 int match = -1;
132 char *target;
133 void *buffer;
134 unsigned long size;
135 char type[10];
136 int len;
137
138 target = xmalloc(expected_size);
139 len = readlink(ce->name, target, expected_size);
140 if (len != expected_size) {
141 free(target);
142 return -1;
143 }
144 buffer = read_sha1_file(ce->sha1, type, &size);
145 if (!buffer) {
146 free(target);
147 return -1;
148 }
149 if (size == expected_size)
150 match = memcmp(buffer, target, size);
151 free(buffer);
152 free(target);
153 return match;
154}
155
711cf3a0
LT
156/*
157 * "refresh" does not calculate a new sha1 file or bring the
158 * cache up-to-date for mode/content changes. But what it
159 * _does_ do is to "re-match" the stat information of a file
160 * with the cache, so that you can refresh the cache for a
161 * file that hasn't been changed but where the stat entry is
162 * out of date.
163 *
667bb59b 164 * For example, you'd want to do this after doing a "git-read-tree",
711cf3a0
LT
165 * to link up the stat cache details with the proper files.
166 */
167static struct cache_entry *refresh_entry(struct cache_entry *ce)
168{
169 struct stat st;
170 struct cache_entry *updated;
171 int changed, size;
172
8ae0a8c5 173 if (lstat(ce->name, &st) < 0)
c6e007b0 174 return ERR_PTR(-errno);
711cf3a0 175
5d728c84 176 changed = ce_match_stat(ce, &st);
711cf3a0
LT
177 if (!changed)
178 return ce;
179
121481ab 180 /*
8ae0a8c5 181 * If the mode or type has changed, there's no point in trying
711cf3a0 182 * to refresh the entry - it's not going to match
121481ab 183 */
8ae0a8c5 184 if (changed & (MODE_CHANGED | TYPE_CHANGED))
c6e007b0 185 return ERR_PTR(-EINVAL);
711cf3a0 186
ffbe1add
KS
187 switch (st.st_mode & S_IFMT) {
188 case S_IFREG:
69a97f1d 189 if (compare_data(ce, &st))
ffbe1add
KS
190 return ERR_PTR(-EINVAL);
191 break;
192 case S_IFLNK:
193 if (compare_link(ce, st.st_size))
194 return ERR_PTR(-EINVAL);
195 break;
196 default:
c6e007b0 197 return ERR_PTR(-EINVAL);
ffbe1add 198 }
711cf3a0
LT
199
200 size = ce_size(ce);
812666c8 201 updated = xmalloc(size);
711cf3a0
LT
202 memcpy(updated, ce, size);
203 fill_stat_cache_info(updated, &st);
204 return updated;
121481ab
LT
205}
206
90535218 207static int refresh_cache(void)
121481ab
LT
208{
209 int i;
90535218 210 int has_errors = 0;
121481ab 211
711cf3a0 212 for (i = 0; i < active_nr; i++) {
1bc992ac
JH
213 struct cache_entry *ce, *new;
214 ce = active_cache[i];
215 if (ce_stage(ce)) {
216 printf("%s: needs merge\n", ce->name);
90535218 217 has_errors = 1;
1bc992ac
JH
218 while ((i < active_nr) &&
219 ! strcmp(active_cache[i]->name, ce->name))
220 i++;
221 i--;
222 continue;
223 }
711cf3a0 224
1bc992ac 225 new = refresh_entry(ce);
c6e007b0 226 if (IS_ERR(new)) {
0ed3715f
LT
227 if (not_new && PTR_ERR(new) == -ENOENT)
228 continue;
229 if (quiet)
230 continue;
231 printf("%s: needs update\n", ce->name);
232 has_errors = 1;
711cf3a0
LT
233 continue;
234 }
ee267527 235 active_cache_changed = 1;
62d046a0
PB
236 /* You can NOT just free active_cache[i] here, since it
237 * might not be necessarily malloc()ed but can also come
238 * from mmap(). */
711cf3a0
LT
239 active_cache[i] = new;
240 }
90535218 241 return has_errors;
e83c5163
LT
242}
243
e83c5163
LT
244/*
245 * We fundamentally don't like some paths: we don't want
320d3a1b
LT
246 * dot or dot-dot anywhere, and for obvious reasons don't
247 * want to recurse into ".git" either.
e83c5163
LT
248 *
249 * Also, we don't want double slashes or slashes at the
aebb2679 250 * end that can make pathnames ambiguous.
e83c5163 251 */
320d3a1b
LT
252static int verify_dotfile(const char *rest)
253{
254 /*
255 * The first character was '.', but that
256 * has already been discarded, we now test
257 * the rest.
258 */
259 switch (*rest) {
260 /* "." is not allowed */
261 case '\0': case '/':
262 return 0;
263
264 /*
265 * ".git" followed by NUL or slash is bad. This
266 * shares the path end test with the ".." case.
267 */
268 case 'g':
269 if (rest[1] != 'i')
270 break;
271 if (rest[2] != 't')
272 break;
273 rest += 2;
274 /* fallthrough */
275 case '.':
276 if (rest[1] == '\0' || rest[1] == '/')
277 return 0;
278 }
279 return 1;
280}
281
e83c5163
LT
282static int verify_path(char *path)
283{
284 char c;
285
286 goto inside;
287 for (;;) {
288 if (!c)
289 return 1;
290 if (c == '/') {
291inside:
292 c = *path++;
320d3a1b
LT
293 switch (c) {
294 default:
e83c5163 295 continue;
320d3a1b
LT
296 case '/': case '\0':
297 break;
298 case '.':
299 if (verify_dotfile(path))
300 continue;
301 }
e83c5163
LT
302 return 0;
303 }
304 c = *path++;
305 }
306}
307
9945d980
LT
308static int add_cacheinfo(char *arg1, char *arg2, char *arg3)
309{
192268c1 310 int size, len, option;
9945d980
LT
311 unsigned int mode;
312 unsigned char sha1[20];
313 struct cache_entry *ce;
314
315 if (sscanf(arg1, "%o", &mode) != 1)
316 return -1;
9945d980
LT
317 if (get_sha1_hex(arg2, sha1))
318 return -1;
9945d980
LT
319 if (!verify_path(arg3))
320 return -1;
9945d980
LT
321
322 len = strlen(arg3);
323 size = cache_entry_size(len);
812666c8 324 ce = xmalloc(size);
9945d980
LT
325 memset(ce, 0, size);
326
327 memcpy(ce->sha1, sha1, 20);
328 memcpy(ce->name, arg3, len);
f5cabd13 329 ce->ce_flags = htons(len);
e4479470 330 ce->ce_mode = create_ce_mode(mode);
192268c1
JH
331 option = allow_add ? ADD_CACHE_OK_TO_ADD : 0;
332 option |= allow_replace ? ADD_CACHE_OK_TO_REPLACE : 0;
333 return add_cache_entry(ce, option);
9945d980
LT
334}
335
e99d59ff 336static struct cache_file cache_file;
f2a19340 337
e83c5163
LT
338int main(int argc, char **argv)
339{
90535218 340 int i, newfd, entries, has_errors = 0;
121481ab 341 int allow_options = 1;
cfb0af1d 342 const char *prefix = setup_git_directory();
bb233d69 343
415e96c8 344 newfd = hold_index_file_for_update(&cache_file, get_index_file());
9614b8dc 345 if (newfd < 0)
2de381f9 346 die("unable to create new cachefile");
9614b8dc 347
e83c5163 348 entries = read_cache();
9614b8dc 349 if (entries < 0)
2de381f9 350 die("cache corrupted");
e83c5163 351
e83c5163
LT
352 for (i = 1 ; i < argc; i++) {
353 char *path = argv[i];
121481ab
LT
354
355 if (allow_options && *path == '-') {
356 if (!strcmp(path, "--")) {
357 allow_options = 0;
358 continue;
359 }
0ed3715f
LT
360 if (!strcmp(path, "-q")) {
361 quiet = 1;
362 continue;
363 }
121481ab
LT
364 if (!strcmp(path, "--add")) {
365 allow_add = 1;
366 continue;
367 }
192268c1
JH
368 if (!strcmp(path, "--replace")) {
369 allow_replace = 1;
370 continue;
371 }
121481ab
LT
372 if (!strcmp(path, "--remove")) {
373 allow_remove = 1;
374 continue;
375 }
376 if (!strcmp(path, "--refresh")) {
90535218 377 has_errors |= refresh_cache();
121481ab
LT
378 continue;
379 }
9945d980 380 if (!strcmp(path, "--cacheinfo")) {
b3f94c4b 381 if (i+3 >= argc)
215a7ad1 382 die("git-update-index: --cacheinfo <mode> <sha1> <path>");
b3f94c4b 383 if (add_cacheinfo(argv[i+1], argv[i+2], argv[i+3]))
215a7ad1 384 die("git-update-index: --cacheinfo cannot add %s", argv[i+3]);
9945d980
LT
385 i += 3;
386 continue;
387 }
df6e1516
BL
388 if (!strcmp(path, "--info-only")) {
389 info_only = 1;
390 continue;
391 }
0ff5bf7c 392 if (!strcmp(path, "--force-remove")) {
9b63f501 393 force_remove = 1;
0ff5bf7c
JH
394 continue;
395 }
396
c6e007b0
JB
397 if (!strcmp(path, "--ignore-missing")) {
398 not_new = 1;
399 continue;
400 }
2de381f9 401 die("unknown option %s", path);
121481ab 402 }
cfb0af1d 403 path = prefix_path(prefix, prefix ? strlen(prefix) : 0, path);
e83c5163
LT
404 if (!verify_path(path)) {
405 fprintf(stderr, "Ignoring path %s\n", argv[i]);
406 continue;
407 }
9b63f501
PB
408 if (force_remove) {
409 if (remove_file_from_cache(path))
34143b26 410 die("git-update-index: unable to remove %s", path);
9b63f501
PB
411 continue;
412 }
9614b8dc 413 if (add_file_to_cache(path))
34143b26 414 die("Unable to process file %s", path);
e83c5163 415 }
415e96c8
JH
416 if (write_cache(newfd, active_cache, active_nr) ||
417 commit_index_file(&cache_file))
2de381f9 418 die("Unable to write new cachefile");
9614b8dc 419
c4b83e61 420 return has_errors ? 1 : 0;
e83c5163 421}