]> git.ipfire.org Git - thirdparty/git.git/blame - update-index.c
Merge branch 'ml/cvsserver'
[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"
973d6a20 8#include "quote.h"
e83c5163 9
121481ab
LT
10/*
11 * Default to not allowing changes to the list of files. The
12 * tool doesn't actually care, but this makes it harder to add
13 * files to the revision control by mistake by doing something
215a7ad1 14 * like "git-update-index *" and suddenly having all the object
121481ab
LT
15 * files be revision controlled.
16 */
caf4f582
JH
17static int allow_add;
18static int allow_remove;
19static int allow_replace;
20static int allow_unmerged; /* --refresh needing merge is not error */
21static int not_new; /* --refresh not having working tree files is not error */
22static int quiet; /* --refresh needing update is not error */
23static int info_only;
9b63f501 24static int force_remove;
caf4f582 25static int verbose;
5f73076c
JH
26static int mark_valid_only = 0;
27#define MARK_VALID 1
28#define UNMARK_VALID 2
29
c6e007b0
JB
30
31/* Three functions to allow overloaded pointer return; see linux/err.h */
32static inline void *ERR_PTR(long error)
33{
34 return (void *) error;
35}
36
37static inline long PTR_ERR(const void *ptr)
38{
39 return (long) ptr;
40}
41
42static inline long IS_ERR(const void *ptr)
43{
44 return (unsigned long)ptr > (unsigned long)-1000L;
45}
121481ab 46
caf4f582
JH
47static void report(const char *fmt, ...)
48{
49 va_list vp;
50
51 if (!verbose)
52 return;
53
54 va_start(vp, fmt);
55 vprintf(fmt, vp);
56 putchar('\n');
57 va_end(vp);
58}
59
5f73076c
JH
60static int mark_valid(const char *path)
61{
62 int namelen = strlen(path);
63 int pos = cache_name_pos(path, namelen);
64 if (0 <= pos) {
65 switch (mark_valid_only) {
66 case MARK_VALID:
67 active_cache[pos]->ce_flags |= htons(CE_VALID);
68 break;
69 case UNMARK_VALID:
70 active_cache[pos]->ce_flags &= ~htons(CE_VALID);
71 break;
72 }
73 active_cache_changed = 1;
74 return 0;
75 }
76 return -1;
77}
78
6b5ee137 79static int add_file_to_cache(const char *path)
e83c5163 80{
4c5abf42 81 int size, namelen, option, status;
e83c5163
LT
82 struct cache_entry *ce;
83 struct stat st;
e83c5163 84
4c5abf42
JH
85 status = lstat(path, &st);
86 if (status < 0 || S_ISDIR(st.st_mode)) {
87 /* When we used to have "path" and now we want to add
88 * "path/file", we need a way to remove "path" before
89 * being able to add "path/file". However,
215a7ad1 90 * "git-update-index --remove path" would not work.
4c5abf42
JH
91 * --force-remove can be used but this is more user
92 * friendly, especially since we can do the opposite
93 * case just fine without --force-remove.
94 */
95 if (status == 0 || (errno == ENOENT || errno == ENOTDIR)) {
34143b26
PB
96 if (allow_remove) {
97 if (remove_file_from_cache(path))
98 return error("%s: cannot remove from the index",
99 path);
100 else
101 return 0;
102 } else if (status < 0) {
103 return error("%s: does not exist and --remove not passed",
104 path);
105 }
121481ab 106 }
89bc8c78 107 if (0 == status)
34143b26
PB
108 return error("%s: is a directory - add files inside instead",
109 path);
89bc8c78
AW
110 else
111 return error("lstat(\"%s\"): %s", path,
112 strerror(errno));
e83c5163 113 }
3e09cdfd 114
e83c5163
LT
115 namelen = strlen(path);
116 size = cache_entry_size(namelen);
812666c8 117 ce = xmalloc(size);
e83c5163
LT
118 memset(ce, 0, size);
119 memcpy(ce->name, path, namelen);
5f73076c 120 ce->ce_flags = htons(namelen);
711cf3a0 121 fill_stat_cache_info(ce, &st);
3e09cdfd 122
e4479470 123 ce->ce_mode = create_ce_mode(st.st_mode);
3e09cdfd
JH
124 if (!trust_executable_bit) {
125 /* If there is an existing entry, pick the mode bits
126 * from it.
127 */
128 int pos = cache_name_pos(path, namelen);
129 if (0 <= pos)
130 ce->ce_mode = active_cache[pos]->ce_mode;
131 }
ec1fcc16
JH
132
133 if (index_path(ce->sha1, path, &st, !info_only))
134 return -1;
192268c1
JH
135 option = allow_add ? ADD_CACHE_OK_TO_ADD : 0;
136 option |= allow_replace ? ADD_CACHE_OK_TO_REPLACE : 0;
34143b26
PB
137 if (add_cache_entry(ce, option))
138 return error("%s: cannot add to the index - missing --add option?",
139 path);
140 return 0;
121481ab
LT
141}
142
711cf3a0
LT
143/*
144 * "refresh" does not calculate a new sha1 file or bring the
145 * cache up-to-date for mode/content changes. But what it
146 * _does_ do is to "re-match" the stat information of a file
147 * with the cache, so that you can refresh the cache for a
148 * file that hasn't been changed but where the stat entry is
149 * out of date.
150 *
667bb59b 151 * For example, you'd want to do this after doing a "git-read-tree",
711cf3a0
LT
152 * to link up the stat cache details with the proper files.
153 */
5f73076c 154static struct cache_entry *refresh_entry(struct cache_entry *ce, int really)
711cf3a0
LT
155{
156 struct stat st;
157 struct cache_entry *updated;
158 int changed, size;
159
8ae0a8c5 160 if (lstat(ce->name, &st) < 0)
c6e007b0 161 return ERR_PTR(-errno);
711cf3a0 162
5f73076c 163 changed = ce_match_stat(ce, &st, really);
b92b2ce9
JH
164 if (!changed) {
165 if (really && assume_unchanged &&
166 !(ce->ce_flags & htons(CE_VALID)))
167 ; /* mark this one VALID again */
168 else
169 return NULL;
170 }
711cf3a0 171
5f73076c 172 if (ce_modified(ce, &st, really))
c6e007b0 173 return ERR_PTR(-EINVAL);
711cf3a0
LT
174
175 size = ce_size(ce);
812666c8 176 updated = xmalloc(size);
711cf3a0
LT
177 memcpy(updated, ce, size);
178 fill_stat_cache_info(updated, &st);
5f73076c 179
8b9b0f3a
JH
180 /* In this case, if really is not set, we should leave
181 * CE_VALID bit alone. Otherwise, paths marked with
182 * --no-assume-unchanged (i.e. things to be edited) will
183 * reacquire CE_VALID bit automatically, which is not
184 * really what we want.
185 */
186 if (!really && assume_unchanged && !(ce->ce_flags & htons(CE_VALID)))
187 updated->ce_flags &= ~htons(CE_VALID);
188
711cf3a0 189 return updated;
121481ab
LT
190}
191
5f73076c 192static int refresh_cache(int really)
121481ab
LT
193{
194 int i;
90535218 195 int has_errors = 0;
121481ab 196
711cf3a0 197 for (i = 0; i < active_nr; i++) {
1bc992ac
JH
198 struct cache_entry *ce, *new;
199 ce = active_cache[i];
200 if (ce_stage(ce)) {
1bc992ac
JH
201 while ((i < active_nr) &&
202 ! strcmp(active_cache[i]->name, ce->name))
203 i++;
204 i--;
5d1a5c02
LT
205 if (allow_unmerged)
206 continue;
207 printf("%s: needs merge\n", ce->name);
208 has_errors = 1;
1bc992ac
JH
209 continue;
210 }
711cf3a0 211
5f73076c 212 new = refresh_entry(ce, really);
5d1a5c02
LT
213 if (!new)
214 continue;
c6e007b0 215 if (IS_ERR(new)) {
0ed3715f
LT
216 if (not_new && PTR_ERR(new) == -ENOENT)
217 continue;
5f73076c
JH
218 if (really && PTR_ERR(new) == -EINVAL) {
219 /* If we are doing --really-refresh that
220 * means the index is not valid anymore.
221 */
222 ce->ce_flags &= ~htons(CE_VALID);
223 active_cache_changed = 1;
224 }
0ed3715f
LT
225 if (quiet)
226 continue;
227 printf("%s: needs update\n", ce->name);
228 has_errors = 1;
711cf3a0
LT
229 continue;
230 }
ee267527 231 active_cache_changed = 1;
62d046a0
PB
232 /* You can NOT just free active_cache[i] here, since it
233 * might not be necessarily malloc()ed but can also come
234 * from mmap(). */
711cf3a0
LT
235 active_cache[i] = new;
236 }
90535218 237 return has_errors;
e83c5163
LT
238}
239
e83c5163
LT
240/*
241 * We fundamentally don't like some paths: we don't want
320d3a1b
LT
242 * dot or dot-dot anywhere, and for obvious reasons don't
243 * want to recurse into ".git" either.
e83c5163
LT
244 *
245 * Also, we don't want double slashes or slashes at the
aebb2679 246 * end that can make pathnames ambiguous.
e83c5163 247 */
320d3a1b
LT
248static int verify_dotfile(const char *rest)
249{
250 /*
251 * The first character was '.', but that
252 * has already been discarded, we now test
253 * the rest.
254 */
255 switch (*rest) {
256 /* "." is not allowed */
257 case '\0': case '/':
258 return 0;
259
260 /*
261 * ".git" followed by NUL or slash is bad. This
262 * shares the path end test with the ".." case.
263 */
264 case 'g':
265 if (rest[1] != 'i')
266 break;
267 if (rest[2] != 't')
268 break;
269 rest += 2;
270 /* fallthrough */
271 case '.':
272 if (rest[1] == '\0' || rest[1] == '/')
273 return 0;
274 }
275 return 1;
276}
277
6b5ee137 278static int verify_path(const char *path)
e83c5163
LT
279{
280 char c;
281
282 goto inside;
283 for (;;) {
284 if (!c)
285 return 1;
286 if (c == '/') {
287inside:
288 c = *path++;
320d3a1b
LT
289 switch (c) {
290 default:
e83c5163 291 continue;
320d3a1b
LT
292 case '/': case '\0':
293 break;
294 case '.':
295 if (verify_dotfile(path))
296 continue;
297 }
e83c5163
LT
298 return 0;
299 }
300 c = *path++;
301 }
302}
303
d23748a6
JH
304static int add_cacheinfo(unsigned int mode, const unsigned char *sha1,
305 const char *path, int stage)
9945d980 306{
192268c1 307 int size, len, option;
9945d980
LT
308 struct cache_entry *ce;
309
d23748a6 310 if (!verify_path(path))
9945d980 311 return -1;
9945d980 312
d23748a6 313 len = strlen(path);
9945d980 314 size = cache_entry_size(len);
812666c8 315 ce = xmalloc(size);
9945d980
LT
316 memset(ce, 0, size);
317
318 memcpy(ce->sha1, sha1, 20);
d23748a6
JH
319 memcpy(ce->name, path, len);
320 ce->ce_flags = create_ce_flags(len, stage);
e4479470 321 ce->ce_mode = create_ce_mode(mode);
5f73076c
JH
322 if (assume_unchanged)
323 ce->ce_flags |= htons(CE_VALID);
192268c1
JH
324 option = allow_add ? ADD_CACHE_OK_TO_ADD : 0;
325 option |= allow_replace ? ADD_CACHE_OK_TO_REPLACE : 0;
caf4f582
JH
326 if (add_cache_entry(ce, option))
327 return error("%s: cannot add to the index - missing --add option?",
d23748a6
JH
328 path);
329 report("add '%s'", path);
caf4f582 330 return 0;
9945d980
LT
331}
332
3e09cdfd
JH
333static int chmod_path(int flip, const char *path)
334{
335 int pos;
336 struct cache_entry *ce;
337 unsigned int mode;
f2a19340 338
3e09cdfd
JH
339 pos = cache_name_pos(path, strlen(path));
340 if (pos < 0)
341 return -1;
342 ce = active_cache[pos];
343 mode = ntohl(ce->ce_mode);
344 if (!S_ISREG(mode))
345 return -1;
346 switch (flip) {
347 case '+':
348 ce->ce_mode |= htonl(0111); break;
349 case '-':
350 ce->ce_mode &= htonl(~0111); break;
351 default:
352 return -1;
353 }
354 active_cache_changed = 1;
355 return 0;
356}
357
358static struct cache_file cache_file;
ee1bec3d
JH
359
360static void update_one(const char *path, const char *prefix, int prefix_length)
361{
362 const char *p = prefix_path(prefix, prefix_length, path);
363 if (!verify_path(p)) {
364 fprintf(stderr, "Ignoring path %s\n", path);
365 return;
366 }
5f73076c
JH
367 if (mark_valid_only) {
368 if (mark_valid(p))
369 die("Unable to mark file %s", path);
370 return;
371 }
372
ee1bec3d
JH
373 if (force_remove) {
374 if (remove_file_from_cache(p))
375 die("git-update-index: unable to remove %s", path);
caf4f582 376 report("remove '%s'", path);
ee1bec3d
JH
377 return;
378 }
379 if (add_file_to_cache(p))
380 die("Unable to process file %s", path);
caf4f582 381 report("add '%s'", path);
ee1bec3d
JH
382}
383
d4dbf36d
JH
384static void read_index_info(int line_termination)
385{
386 struct strbuf buf;
387 strbuf_init(&buf);
388 while (1) {
9c20a470 389 char *ptr, *tab;
973d6a20 390 char *path_name;
d4dbf36d
JH
391 unsigned char sha1[20];
392 unsigned int mode;
d23748a6
JH
393 int stage;
394
395 /* This reads lines formatted in one of three formats:
396 *
397 * (1) mode SP sha1 TAB path
398 * The first format is what "git-apply --index-info"
399 * reports, and used to reconstruct a partial tree
400 * that is used for phony merge base tree when falling
401 * back on 3-way merge.
402 *
403 * (2) mode SP type SP sha1 TAB path
404 * The second format is to stuff git-ls-tree output
405 * into the index file.
406 *
407 * (3) mode SP sha1 SP stage TAB path
408 * This format is to put higher order stages into the
409 * index file and matches git-ls-files --stage output.
410 */
d4dbf36d
JH
411 read_line(&buf, stdin, line_termination);
412 if (buf.eof)
413 break;
414
415 mode = strtoul(buf.buf, &ptr, 8);
9c20a470 416 if (ptr == buf.buf || *ptr != ' ')
d4dbf36d
JH
417 goto bad_line;
418
9c20a470
JH
419 tab = strchr(ptr, '\t');
420 if (!tab || tab - ptr < 41)
421 goto bad_line;
d23748a6 422
2d497115 423 if (tab[-2] == ' ' && '0' <= tab[-1] && tab[-1] <= '3') {
d23748a6
JH
424 stage = tab[-1] - '0';
425 ptr = tab + 1; /* point at the head of path */
426 tab = tab - 2; /* point at tail of sha1 */
427 }
428 else {
429 stage = 0;
430 ptr = tab + 1; /* point at the head of path */
431 }
432
9c20a470
JH
433 if (get_sha1_hex(tab - 40, sha1) || tab[-41] != ' ')
434 goto bad_line;
973d6a20
JH
435
436 if (line_termination && ptr[0] == '"')
437 path_name = unquote_c_style(ptr, NULL);
438 else
439 path_name = ptr;
440
441 if (!verify_path(path_name)) {
442 fprintf(stderr, "Ignoring path %s\n", path_name);
443 if (path_name != ptr)
444 free(path_name);
d4dbf36d
JH
445 continue;
446 }
447
448 if (!mode) {
449 /* mode == 0 means there is no such path -- remove */
973d6a20 450 if (remove_file_from_cache(path_name))
d4dbf36d
JH
451 die("git-update-index: unable to remove %s",
452 ptr);
453 }
454 else {
455 /* mode ' ' sha1 '\t' name
456 * ptr[-1] points at tab,
457 * ptr[-41] is at the beginning of sha1
458 */
459 ptr[-42] = ptr[-1] = 0;
d23748a6 460 if (add_cacheinfo(mode, sha1, path_name, stage))
d4dbf36d 461 die("git-update-index: unable to update %s",
973d6a20 462 path_name);
d4dbf36d 463 }
973d6a20
JH
464 if (path_name != ptr)
465 free(path_name);
d4dbf36d
JH
466 continue;
467
468 bad_line:
469 die("malformed index info %s", buf.buf);
470 }
471}
472
f6ab5bb2 473static const char update_index_usage[] =
14470c0d 474"git-update-index [-q] [--add] [--replace] [--remove] [--unmerged] [--refresh] [--cacheinfo] [--chmod=(+|-)x] [--info-only] [--force-remove] [--stdin] [--index-info] [--ignore-missing] [-z] [--verbose] [--] <file>...";
f6ab5bb2 475
6b5ee137 476int main(int argc, const char **argv)
e83c5163 477{
ee1bec3d 478 int i, newfd, entries, has_errors = 0, line_termination = '\n';
121481ab 479 int allow_options = 1;
ee1bec3d 480 int read_from_stdin = 0;
cfb0af1d 481 const char *prefix = setup_git_directory();
ee1bec3d 482 int prefix_length = prefix ? strlen(prefix) : 0;
bb233d69 483
3e09cdfd
JH
484 git_config(git_default_config);
485
415e96c8 486 newfd = hold_index_file_for_update(&cache_file, get_index_file());
9614b8dc 487 if (newfd < 0)
2de381f9 488 die("unable to create new cachefile");
9614b8dc 489
e83c5163 490 entries = read_cache();
9614b8dc 491 if (entries < 0)
2de381f9 492 die("cache corrupted");
e83c5163 493
e83c5163 494 for (i = 1 ; i < argc; i++) {
6b5ee137 495 const char *path = argv[i];
121481ab
LT
496
497 if (allow_options && *path == '-') {
498 if (!strcmp(path, "--")) {
499 allow_options = 0;
500 continue;
501 }
0ed3715f
LT
502 if (!strcmp(path, "-q")) {
503 quiet = 1;
504 continue;
505 }
121481ab
LT
506 if (!strcmp(path, "--add")) {
507 allow_add = 1;
508 continue;
509 }
192268c1
JH
510 if (!strcmp(path, "--replace")) {
511 allow_replace = 1;
512 continue;
513 }
121481ab
LT
514 if (!strcmp(path, "--remove")) {
515 allow_remove = 1;
516 continue;
517 }
5d1a5c02
LT
518 if (!strcmp(path, "--unmerged")) {
519 allow_unmerged = 1;
520 continue;
521 }
121481ab 522 if (!strcmp(path, "--refresh")) {
5f73076c
JH
523 has_errors |= refresh_cache(0);
524 continue;
525 }
526 if (!strcmp(path, "--really-refresh")) {
527 has_errors |= refresh_cache(1);
121481ab
LT
528 continue;
529 }
9945d980 530 if (!strcmp(path, "--cacheinfo")) {
d23748a6
JH
531 unsigned char sha1[20];
532 unsigned int mode;
533
b3f94c4b 534 if (i+3 >= argc)
215a7ad1 535 die("git-update-index: --cacheinfo <mode> <sha1> <path>");
d23748a6
JH
536
537 if ((sscanf(argv[i+1], "%o", &mode) != 1) ||
538 get_sha1_hex(argv[i+2], sha1) ||
539 add_cacheinfo(mode, sha1, argv[i+3], 0))
540 die("git-update-index: --cacheinfo"
541 " cannot add %s", argv[i+3]);
9945d980
LT
542 i += 3;
543 continue;
544 }
3e09cdfd
JH
545 if (!strcmp(path, "--chmod=-x") ||
546 !strcmp(path, "--chmod=+x")) {
547 if (argc <= i+1)
548 die("git-update-index: %s <path>", path);
549 if (chmod_path(path[8], argv[++i]))
550 die("git-update-index: %s cannot chmod %s", path, argv[i]);
551 continue;
552 }
5f73076c
JH
553 if (!strcmp(path, "--assume-unchanged")) {
554 mark_valid_only = MARK_VALID;
555 continue;
556 }
557 if (!strcmp(path, "--no-assume-unchanged")) {
558 mark_valid_only = UNMARK_VALID;
559 continue;
560 }
df6e1516
BL
561 if (!strcmp(path, "--info-only")) {
562 info_only = 1;
563 continue;
564 }
0ff5bf7c 565 if (!strcmp(path, "--force-remove")) {
9b63f501 566 force_remove = 1;
0ff5bf7c
JH
567 continue;
568 }
ee1bec3d
JH
569 if (!strcmp(path, "-z")) {
570 line_termination = 0;
571 continue;
572 }
573 if (!strcmp(path, "--stdin")) {
574 if (i != argc - 1)
575 die("--stdin must be at the end");
576 read_from_stdin = 1;
577 break;
578 }
d4dbf36d
JH
579 if (!strcmp(path, "--index-info")) {
580 allow_add = allow_replace = allow_remove = 1;
581 read_index_info(line_termination);
582 continue;
583 }
c6e007b0
JB
584 if (!strcmp(path, "--ignore-missing")) {
585 not_new = 1;
586 continue;
587 }
caf4f582
JH
588 if (!strcmp(path, "--verbose")) {
589 verbose = 1;
590 continue;
591 }
f6ab5bb2
PB
592 if (!strcmp(path, "-h") || !strcmp(path, "--help"))
593 usage(update_index_usage);
2de381f9 594 die("unknown option %s", path);
121481ab 595 }
ee1bec3d
JH
596 update_one(path, prefix, prefix_length);
597 }
598 if (read_from_stdin) {
599 struct strbuf buf;
600 strbuf_init(&buf);
601 while (1) {
a94d9948 602 char *path_name;
ee1bec3d
JH
603 read_line(&buf, stdin, line_termination);
604 if (buf.eof)
605 break;
a94d9948
JH
606 if (line_termination && buf.buf[0] == '"')
607 path_name = unquote_c_style(buf.buf, NULL);
608 else
609 path_name = buf.buf;
610 update_one(path_name, prefix, prefix_length);
611 if (path_name != buf.buf)
612 free(path_name);
9b63f501 613 }
e83c5163 614 }
5cd5ace7
LT
615 if (active_cache_changed) {
616 if (write_cache(newfd, active_cache, active_nr) ||
617 commit_index_file(&cache_file))
618 die("Unable to write new cachefile");
619 }
9614b8dc 620
c4b83e61 621 return has_errors ? 1 : 0;
e83c5163 622}