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