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