Git fork
at reftables-rust 1319 lines 37 kB view raw
1/* 2 * GIT - The information manager from hell 3 * 4 * Copyright (C) Linus Torvalds, 2005 5 */ 6 7#define USE_THE_REPOSITORY_VARIABLE 8#define DISABLE_SIGN_COMPARE_WARNINGS 9 10#include "builtin.h" 11#include "config.h" 12#include "environment.h" 13#include "gettext.h" 14#include "hash.h" 15#include "hex.h" 16#include "lockfile.h" 17#include "quote.h" 18#include "cache-tree.h" 19#include "tree-walk.h" 20#include "object-file.h" 21#include "odb.h" 22#include "refs.h" 23#include "resolve-undo.h" 24#include "parse-options.h" 25#include "pathspec.h" 26#include "dir.h" 27#include "read-cache.h" 28#include "setup.h" 29#include "sparse-index.h" 30#include "split-index.h" 31#include "symlinks.h" 32#include "fsmonitor.h" 33#include "write-or-die.h" 34 35/* 36 * Default to not allowing changes to the list of files. The 37 * tool doesn't actually care, but this makes it harder to add 38 * files to the revision control by mistake by doing something 39 * like "git update-index *" and suddenly having all the object 40 * files be revision controlled. 41 */ 42static int allow_add; 43static int allow_remove; 44static int allow_replace; 45static int info_only; 46static int force_remove; 47static int verbose; 48static int mark_valid_only; 49static int mark_skip_worktree_only; 50static int mark_fsmonitor_only; 51static int ignore_skip_worktree_entries; 52#define MARK_FLAG 1 53#define UNMARK_FLAG 2 54static struct strbuf mtime_dir = STRBUF_INIT; 55 56/* Untracked cache mode */ 57enum uc_mode { 58 UC_UNSPECIFIED = -1, 59 UC_DISABLE = 0, 60 UC_ENABLE, 61 UC_TEST, 62 UC_FORCE 63}; 64 65__attribute__((format (printf, 1, 2))) 66static void report(const char *fmt, ...) 67{ 68 va_list vp; 69 70 if (!verbose) 71 return; 72 73 va_start(vp, fmt); 74 vprintf(fmt, vp); 75 putchar('\n'); 76 va_end(vp); 77} 78 79static void remove_test_directory(void) 80{ 81 if (mtime_dir.len) 82 remove_dir_recursively(&mtime_dir, 0); 83} 84 85static const char *get_mtime_path(const char *path) 86{ 87 static struct strbuf sb = STRBUF_INIT; 88 strbuf_reset(&sb); 89 strbuf_addf(&sb, "%s/%s", mtime_dir.buf, path); 90 return sb.buf; 91} 92 93static void xmkdir(const char *path) 94{ 95 path = get_mtime_path(path); 96 if (mkdir(path, 0700)) 97 die_errno(_("failed to create directory %s"), path); 98} 99 100static int xstat_mtime_dir(struct stat *st) 101{ 102 if (stat(mtime_dir.buf, st)) 103 die_errno(_("failed to stat %s"), mtime_dir.buf); 104 return 0; 105} 106 107static int create_file(const char *path) 108{ 109 int fd; 110 path = get_mtime_path(path); 111 fd = xopen(path, O_CREAT | O_RDWR, 0644); 112 return fd; 113} 114 115static void xunlink(const char *path) 116{ 117 path = get_mtime_path(path); 118 if (unlink(path)) 119 die_errno(_("failed to delete file %s"), path); 120} 121 122static void xrmdir(const char *path) 123{ 124 path = get_mtime_path(path); 125 if (rmdir(path)) 126 die_errno(_("failed to delete directory %s"), path); 127} 128 129static void avoid_racy(void) 130{ 131 /* 132 * not use if we could usleep(10) if USE_NSEC is defined. The 133 * field nsec could be there, but the OS could choose to 134 * ignore it? 135 */ 136 sleep(1); 137} 138 139static int test_if_untracked_cache_is_supported(void) 140{ 141 struct stat st; 142 struct stat_data base; 143 int fd, ret = 0; 144 char *cwd; 145 146 strbuf_addstr(&mtime_dir, "mtime-test-XXXXXX"); 147 if (!mkdtemp(mtime_dir.buf)) 148 die_errno("Could not make temporary directory"); 149 150 cwd = xgetcwd(); 151 fprintf(stderr, _("Testing mtime in '%s' "), cwd); 152 free(cwd); 153 154 atexit(remove_test_directory); 155 xstat_mtime_dir(&st); 156 fill_stat_data(&base, &st); 157 fputc('.', stderr); 158 159 avoid_racy(); 160 fd = create_file("newfile"); 161 xstat_mtime_dir(&st); 162 if (!match_stat_data(&base, &st)) { 163 close(fd); 164 fputc('\n', stderr); 165 fprintf_ln(stderr,_("directory stat info does not " 166 "change after adding a new file")); 167 goto done; 168 } 169 fill_stat_data(&base, &st); 170 fputc('.', stderr); 171 172 avoid_racy(); 173 xmkdir("new-dir"); 174 xstat_mtime_dir(&st); 175 if (!match_stat_data(&base, &st)) { 176 close(fd); 177 fputc('\n', stderr); 178 fprintf_ln(stderr, _("directory stat info does not change " 179 "after adding a new directory")); 180 goto done; 181 } 182 fill_stat_data(&base, &st); 183 fputc('.', stderr); 184 185 avoid_racy(); 186 write_or_die(fd, "data", 4); 187 close(fd); 188 xstat_mtime_dir(&st); 189 if (match_stat_data(&base, &st)) { 190 fputc('\n', stderr); 191 fprintf_ln(stderr, _("directory stat info changes " 192 "after updating a file")); 193 goto done; 194 } 195 fputc('.', stderr); 196 197 avoid_racy(); 198 close(create_file("new-dir/new")); 199 xstat_mtime_dir(&st); 200 if (match_stat_data(&base, &st)) { 201 fputc('\n', stderr); 202 fprintf_ln(stderr, _("directory stat info changes after " 203 "adding a file inside subdirectory")); 204 goto done; 205 } 206 fputc('.', stderr); 207 208 avoid_racy(); 209 xunlink("newfile"); 210 xstat_mtime_dir(&st); 211 if (!match_stat_data(&base, &st)) { 212 fputc('\n', stderr); 213 fprintf_ln(stderr, _("directory stat info does not " 214 "change after deleting a file")); 215 goto done; 216 } 217 fill_stat_data(&base, &st); 218 fputc('.', stderr); 219 220 avoid_racy(); 221 xunlink("new-dir/new"); 222 xrmdir("new-dir"); 223 xstat_mtime_dir(&st); 224 if (!match_stat_data(&base, &st)) { 225 fputc('\n', stderr); 226 fprintf_ln(stderr, _("directory stat info does not " 227 "change after deleting a directory")); 228 goto done; 229 } 230 231 if (rmdir(mtime_dir.buf)) 232 die_errno(_("failed to delete directory %s"), mtime_dir.buf); 233 fprintf_ln(stderr, _(" OK")); 234 ret = 1; 235 236done: 237 strbuf_release(&mtime_dir); 238 return ret; 239} 240 241static int mark_ce_flags(const char *path, int flag, int mark) 242{ 243 int namelen = strlen(path); 244 int pos = index_name_pos(the_repository->index, path, namelen); 245 if (0 <= pos) { 246 mark_fsmonitor_invalid(the_repository->index, the_repository->index->cache[pos]); 247 if (mark) 248 the_repository->index->cache[pos]->ce_flags |= flag; 249 else 250 the_repository->index->cache[pos]->ce_flags &= ~flag; 251 the_repository->index->cache[pos]->ce_flags |= CE_UPDATE_IN_BASE; 252 cache_tree_invalidate_path(the_repository->index, path); 253 the_repository->index->cache_changed |= CE_ENTRY_CHANGED; 254 return 0; 255 } 256 return -1; 257} 258 259static int remove_one_path(const char *path) 260{ 261 if (!allow_remove) 262 return error("%s: does not exist and --remove not passed", path); 263 if (remove_file_from_index(the_repository->index, path)) 264 return error("%s: cannot remove from the index", path); 265 return 0; 266} 267 268/* 269 * Handle a path that couldn't be lstat'ed. It's either: 270 * - missing file (ENOENT or ENOTDIR). That's ok if we're 271 * supposed to be removing it and the removal actually 272 * succeeds. 273 * - permission error. That's never ok. 274 */ 275static int process_lstat_error(const char *path, int err) 276{ 277 if (is_missing_file_error(err)) 278 return remove_one_path(path); 279 return error("lstat(\"%s\"): %s", path, strerror(err)); 280} 281 282static int add_one_path(const struct cache_entry *old, const char *path, int len, struct stat *st) 283{ 284 int option; 285 struct cache_entry *ce; 286 287 /* Was the old index entry already up-to-date? */ 288 if (old && !ce_stage(old) && !ie_match_stat(the_repository->index, old, st, 0)) 289 return 0; 290 291 ce = make_empty_cache_entry(the_repository->index, len); 292 memcpy(ce->name, path, len); 293 ce->ce_flags = create_ce_flags(0); 294 ce->ce_namelen = len; 295 fill_stat_cache_info(the_repository->index, ce, st); 296 ce->ce_mode = ce_mode_from_stat(old, st->st_mode); 297 298 if (index_path(the_repository->index, &ce->oid, path, st, 299 info_only ? 0 : INDEX_WRITE_OBJECT)) { 300 discard_cache_entry(ce); 301 return -1; 302 } 303 option = allow_add ? ADD_CACHE_OK_TO_ADD : 0; 304 option |= allow_replace ? ADD_CACHE_OK_TO_REPLACE : 0; 305 if (add_index_entry(the_repository->index, ce, option)) { 306 discard_cache_entry(ce); 307 return error("%s: cannot add to the index - missing --add option?", path); 308 } 309 return 0; 310} 311 312/* 313 * Handle a path that was a directory. Four cases: 314 * 315 * - it's already a gitlink in the index, and we keep it that 316 * way, and update it if we can (if we cannot find the HEAD, 317 * we're going to keep it unchanged in the index!) 318 * 319 * - it's a *file* in the index, in which case it should be 320 * removed as a file if removal is allowed, since it doesn't 321 * exist as such any more. If removal isn't allowed, it's 322 * an error. 323 * 324 * (NOTE! This is old and arguably fairly strange behaviour. 325 * We might want to make this an error unconditionally, and 326 * use "--force-remove" if you actually want to force removal). 327 * 328 * - it used to exist as a subdirectory (ie multiple files with 329 * this particular prefix) in the index, in which case it's wrong 330 * to try to update it as a directory. 331 * 332 * - it doesn't exist at all in the index, but it is a valid 333 * git directory, and it should be *added* as a gitlink. 334 */ 335static int process_directory(const char *path, int len, struct stat *st) 336{ 337 struct object_id oid; 338 int pos = index_name_pos(the_repository->index, path, len); 339 340 /* Exact match: file or existing gitlink */ 341 if (pos >= 0) { 342 const struct cache_entry *ce = the_repository->index->cache[pos]; 343 if (S_ISGITLINK(ce->ce_mode)) { 344 345 /* Do nothing to the index if there is no HEAD! */ 346 if (repo_resolve_gitlink_ref(the_repository, path, 347 "HEAD", &oid) < 0) 348 return 0; 349 350 return add_one_path(ce, path, len, st); 351 } 352 /* Should this be an unconditional error? */ 353 return remove_one_path(path); 354 } 355 356 /* Inexact match: is there perhaps a subdirectory match? */ 357 pos = -pos-1; 358 while (pos < the_repository->index->cache_nr) { 359 const struct cache_entry *ce = the_repository->index->cache[pos++]; 360 361 if (strncmp(ce->name, path, len)) 362 break; 363 if (ce->name[len] > '/') 364 break; 365 if (ce->name[len] < '/') 366 continue; 367 368 /* Subdirectory match - error out */ 369 return error("%s: is a directory - add individual files instead", path); 370 } 371 372 /* No match - should we add it as a gitlink? */ 373 if (!repo_resolve_gitlink_ref(the_repository, path, "HEAD", &oid)) 374 return add_one_path(NULL, path, len, st); 375 376 /* Error out. */ 377 return error("%s: is a directory - add files inside instead", path); 378} 379 380static int process_path(const char *path, struct stat *st, int stat_errno) 381{ 382 int pos, len; 383 const struct cache_entry *ce; 384 385 len = strlen(path); 386 if (has_symlink_leading_path(path, len)) 387 return error("'%s' is beyond a symbolic link", path); 388 389 pos = index_name_pos(the_repository->index, path, len); 390 ce = pos < 0 ? NULL : the_repository->index->cache[pos]; 391 if (ce && ce_skip_worktree(ce)) { 392 /* 393 * working directory version is assumed "good" 394 * so updating it does not make sense. 395 * On the other hand, removing it from index should work 396 */ 397 if (!ignore_skip_worktree_entries && allow_remove && 398 remove_file_from_index(the_repository->index, path)) 399 return error("%s: cannot remove from the index", path); 400 return 0; 401 } 402 403 /* 404 * First things first: get the stat information, to decide 405 * what to do about the pathname! 406 */ 407 if (stat_errno) 408 return process_lstat_error(path, stat_errno); 409 410 if (S_ISDIR(st->st_mode)) 411 return process_directory(path, len, st); 412 413 return add_one_path(ce, path, len, st); 414} 415 416static int add_cacheinfo(unsigned int mode, const struct object_id *oid, 417 const char *path, int stage) 418{ 419 int len, option; 420 struct cache_entry *ce; 421 422 if (!verify_path(path, mode)) 423 return error("Invalid path '%s'", path); 424 425 len = strlen(path); 426 ce = make_empty_cache_entry(the_repository->index, len); 427 428 oidcpy(&ce->oid, oid); 429 memcpy(ce->name, path, len); 430 ce->ce_flags = create_ce_flags(stage); 431 ce->ce_namelen = len; 432 ce->ce_mode = create_ce_mode(mode); 433 if (assume_unchanged) 434 ce->ce_flags |= CE_VALID; 435 option = allow_add ? ADD_CACHE_OK_TO_ADD : 0; 436 option |= allow_replace ? ADD_CACHE_OK_TO_REPLACE : 0; 437 if (add_index_entry(the_repository->index, ce, option)) 438 return error("%s: cannot add to the index - missing --add option?", 439 path); 440 report("add '%s'", path); 441 return 0; 442} 443 444static void chmod_path(char flip, const char *path) 445{ 446 int pos; 447 struct cache_entry *ce; 448 449 pos = index_name_pos(the_repository->index, path, strlen(path)); 450 if (pos < 0) 451 goto fail; 452 ce = the_repository->index->cache[pos]; 453 if (chmod_index_entry(the_repository->index, ce, flip) < 0) 454 goto fail; 455 456 report("chmod %cx '%s'", flip, path); 457 return; 458 fail: 459 die("git update-index: cannot chmod %cx '%s'", flip, path); 460} 461 462static void update_one(const char *path) 463{ 464 int stat_errno = 0; 465 struct stat st; 466 467 if (mark_valid_only || mark_skip_worktree_only || force_remove || 468 mark_fsmonitor_only) 469 st.st_mode = 0; 470 else if (lstat(path, &st) < 0) { 471 st.st_mode = 0; 472 stat_errno = errno; 473 } /* else stat is valid */ 474 475 if (!verify_path(path, st.st_mode)) { 476 fprintf(stderr, "Ignoring path %s\n", path); 477 return; 478 } 479 if (mark_valid_only) { 480 if (mark_ce_flags(path, CE_VALID, mark_valid_only == MARK_FLAG)) 481 die("Unable to mark file %s", path); 482 return; 483 } 484 if (mark_skip_worktree_only) { 485 if (mark_ce_flags(path, CE_SKIP_WORKTREE, mark_skip_worktree_only == MARK_FLAG)) 486 die("Unable to mark file %s", path); 487 return; 488 } 489 if (mark_fsmonitor_only) { 490 if (mark_ce_flags(path, CE_FSMONITOR_VALID, mark_fsmonitor_only == MARK_FLAG)) 491 die("Unable to mark file %s", path); 492 return; 493 } 494 495 if (force_remove) { 496 if (remove_file_from_index(the_repository->index, path)) 497 die("git update-index: unable to remove %s", path); 498 report("remove '%s'", path); 499 return; 500 } 501 if (process_path(path, &st, stat_errno)) 502 die("Unable to process path %s", path); 503 report("add '%s'", path); 504} 505 506static void read_index_info(int nul_term_line) 507{ 508 const int hexsz = the_hash_algo->hexsz; 509 struct strbuf buf = STRBUF_INIT; 510 struct strbuf uq = STRBUF_INIT; 511 strbuf_getline_fn getline_fn; 512 513 getline_fn = nul_term_line ? strbuf_getline_nul : strbuf_getline_lf; 514 while (getline_fn(&buf, stdin) != EOF) { 515 char *ptr, *tab; 516 char *path_name; 517 struct object_id oid; 518 unsigned int mode; 519 unsigned long ul; 520 int stage; 521 522 /* This reads lines formatted in one of three formats: 523 * 524 * (1) mode SP sha1 TAB path 525 * The first format is what "git apply --index-info" 526 * reports, and used to reconstruct a partial tree 527 * that is used for phony merge base tree when falling 528 * back on 3-way merge. 529 * 530 * (2) mode SP type SP sha1 TAB path 531 * The second format is to stuff "git ls-tree" output 532 * into the index file. 533 * 534 * (3) mode SP sha1 SP stage TAB path 535 * This format is to put higher order stages into the 536 * index file and matches "git ls-files --stage" output. 537 */ 538 errno = 0; 539 ul = strtoul(buf.buf, &ptr, 8); 540 if (ptr == buf.buf || *ptr != ' ' 541 || errno || (unsigned int) ul != ul) 542 goto bad_line; 543 mode = ul; 544 545 tab = strchr(ptr, '\t'); 546 if (!tab || tab - ptr < hexsz + 1) 547 goto bad_line; 548 549 if (tab[-2] == ' ' && '0' <= tab[-1] && tab[-1] <= '3') { 550 stage = tab[-1] - '0'; 551 ptr = tab + 1; /* point at the head of path */ 552 tab = tab - 2; /* point at tail of sha1 */ 553 } 554 else { 555 stage = 0; 556 ptr = tab + 1; /* point at the head of path */ 557 } 558 559 if (get_oid_hex(tab - hexsz, &oid) || 560 tab[-(hexsz + 1)] != ' ') 561 goto bad_line; 562 563 path_name = ptr; 564 if (!nul_term_line && path_name[0] == '"') { 565 strbuf_reset(&uq); 566 if (unquote_c_style(&uq, path_name, NULL)) { 567 die("git update-index: bad quoting of path name"); 568 } 569 path_name = uq.buf; 570 } 571 572 if (!verify_path(path_name, mode)) { 573 fprintf(stderr, "Ignoring path %s\n", path_name); 574 continue; 575 } 576 577 if (!mode) { 578 /* mode == 0 means there is no such path -- remove */ 579 if (remove_file_from_index(the_repository->index, path_name)) 580 die("git update-index: unable to remove %s", 581 ptr); 582 } 583 else { 584 /* mode ' ' sha1 '\t' name 585 * ptr[-1] points at tab, 586 * ptr[-41] is at the beginning of sha1 587 */ 588 ptr[-(hexsz + 2)] = ptr[-1] = 0; 589 if (add_cacheinfo(mode, &oid, path_name, stage)) 590 die("git update-index: unable to update %s", 591 path_name); 592 } 593 continue; 594 595 bad_line: 596 die("malformed index info %s", buf.buf); 597 } 598 strbuf_release(&buf); 599 strbuf_release(&uq); 600} 601 602static const char * const update_index_usage[] = { 603 N_("git update-index [<options>] [--] [<file>...]"), 604 NULL 605}; 606 607static struct cache_entry *read_one_ent(const char *which, 608 struct object_id *ent, const char *path, 609 int namelen, int stage) 610{ 611 unsigned short mode; 612 struct object_id oid; 613 struct cache_entry *ce; 614 615 if (get_tree_entry(the_repository, ent, path, &oid, &mode)) { 616 if (which) 617 error("%s: not in %s branch.", path, which); 618 return NULL; 619 } 620 if (!the_repository->index->sparse_index && mode == S_IFDIR) { 621 if (which) 622 error("%s: not a blob in %s branch.", path, which); 623 return NULL; 624 } 625 ce = make_empty_cache_entry(the_repository->index, namelen); 626 627 oidcpy(&ce->oid, &oid); 628 memcpy(ce->name, path, namelen); 629 ce->ce_flags = create_ce_flags(stage); 630 ce->ce_namelen = namelen; 631 ce->ce_mode = create_ce_mode(mode); 632 return ce; 633} 634 635static int unresolve_one(const char *path) 636{ 637 struct string_list_item *item; 638 int res = 0; 639 640 if (!the_repository->index->resolve_undo) 641 return res; 642 item = string_list_lookup(the_repository->index->resolve_undo, path); 643 if (!item) 644 return res; /* no resolve-undo record for the path */ 645 res = unmerge_index_entry(the_repository->index, path, item->util, 0); 646 FREE_AND_NULL(item->util); 647 return res; 648} 649 650static int do_unresolve(int ac, const char **av, 651 const char *prefix, int prefix_length) 652{ 653 int i; 654 int err = 0; 655 656 for (i = 1; i < ac; i++) { 657 const char *arg = av[i]; 658 char *p = prefix_path(prefix, prefix_length, arg); 659 err |= unresolve_one(p); 660 free(p); 661 } 662 return err; 663} 664 665static int do_reupdate(const char **paths, 666 const char *prefix) 667{ 668 /* Read HEAD and run update-index on paths that are 669 * merged and already different between index and HEAD. 670 */ 671 int pos; 672 int has_head = 1; 673 struct pathspec pathspec; 674 struct object_id head_oid; 675 676 parse_pathspec(&pathspec, 0, 677 PATHSPEC_PREFER_CWD, 678 prefix, paths); 679 680 if (refs_read_ref(get_main_ref_store(the_repository), "HEAD", &head_oid)) 681 /* If there is no HEAD, that means it is an initial 682 * commit. Update everything in the index. 683 */ 684 has_head = 0; 685 redo: 686 for (pos = 0; pos < the_repository->index->cache_nr; pos++) { 687 const struct cache_entry *ce = the_repository->index->cache[pos]; 688 struct cache_entry *old = NULL; 689 int save_nr; 690 char *path; 691 692 if (ce_stage(ce) || !ce_path_match(the_repository->index, ce, &pathspec, NULL)) 693 continue; 694 if (has_head) 695 old = read_one_ent(NULL, &head_oid, 696 ce->name, ce_namelen(ce), 0); 697 if (old && ce->ce_mode == old->ce_mode && 698 oideq(&ce->oid, &old->oid)) { 699 discard_cache_entry(old); 700 continue; /* unchanged */ 701 } 702 703 /* At this point, we know the contents of the sparse directory are 704 * modified with respect to HEAD, so we expand the index and restart 705 * to process each path individually 706 */ 707 if (S_ISSPARSEDIR(ce->ce_mode)) { 708 ensure_full_index(the_repository->index); 709 goto redo; 710 } 711 712 /* Be careful. The working tree may not have the 713 * path anymore, in which case, under 'allow_remove', 714 * or worse yet 'allow_replace', active_nr may decrease. 715 */ 716 save_nr = the_repository->index->cache_nr; 717 path = xstrdup(ce->name); 718 update_one(path); 719 free(path); 720 discard_cache_entry(old); 721 if (save_nr != the_repository->index->cache_nr) 722 goto redo; 723 } 724 clear_pathspec(&pathspec); 725 return 0; 726} 727 728struct refresh_params { 729 unsigned int flags; 730 int *has_errors; 731}; 732 733static int refresh(struct refresh_params *o, unsigned int flag) 734{ 735 setup_work_tree(); 736 repo_read_index(the_repository); 737 *o->has_errors |= refresh_index(the_repository->index, o->flags | flag, NULL, 738 NULL, NULL); 739 if (has_racy_timestamp(the_repository->index)) { 740 /* 741 * Even if nothing else has changed, updating the file 742 * increases the chance that racy timestamps become 743 * non-racy, helping future run-time performance. 744 * We do that even in case of "errors" returned by 745 * refresh_index() as these are no actual errors. 746 * cmd_status() does the same. 747 */ 748 the_repository->index->cache_changed |= SOMETHING_CHANGED; 749 } 750 return 0; 751} 752 753static int refresh_callback(const struct option *opt, 754 const char *arg, int unset) 755{ 756 BUG_ON_OPT_NEG(unset); 757 BUG_ON_OPT_ARG(arg); 758 return refresh(opt->value, 0); 759} 760 761static int really_refresh_callback(const struct option *opt, 762 const char *arg, int unset) 763{ 764 BUG_ON_OPT_NEG(unset); 765 BUG_ON_OPT_ARG(arg); 766 return refresh(opt->value, REFRESH_REALLY); 767} 768 769static int chmod_callback(const struct option *opt, 770 const char *arg, int unset) 771{ 772 char *flip = opt->value; 773 BUG_ON_OPT_NEG(unset); 774 if ((arg[0] != '-' && arg[0] != '+') || arg[1] != 'x' || arg[2]) 775 return error("option 'chmod' expects \"+x\" or \"-x\""); 776 *flip = arg[0]; 777 return 0; 778} 779 780static int resolve_undo_clear_callback(const struct option *opt UNUSED, 781 const char *arg, int unset) 782{ 783 BUG_ON_OPT_NEG(unset); 784 BUG_ON_OPT_ARG(arg); 785 resolve_undo_clear_index(the_repository->index); 786 return 0; 787} 788 789static int parse_new_style_cacheinfo(const char *arg, 790 unsigned int *mode, 791 struct object_id *oid, 792 const char **path) 793{ 794 unsigned long ul; 795 char *endp; 796 const char *p; 797 798 if (!arg) 799 return -1; 800 801 errno = 0; 802 ul = strtoul(arg, &endp, 8); 803 if (errno || endp == arg || *endp != ',' || (unsigned int) ul != ul) 804 return -1; /* not a new-style cacheinfo */ 805 *mode = ul; 806 endp++; 807 if (parse_oid_hex(endp, oid, &p) || *p != ',') 808 return -1; 809 *path = p + 1; 810 return 0; 811} 812 813static enum parse_opt_result cacheinfo_callback( 814 struct parse_opt_ctx_t *ctx, const struct option *opt UNUSED, 815 const char *arg, int unset) 816{ 817 struct object_id oid; 818 unsigned int mode; 819 const char *path; 820 821 BUG_ON_OPT_NEG(unset); 822 BUG_ON_OPT_ARG(arg); 823 824 if (!parse_new_style_cacheinfo(ctx->argv[1], &mode, &oid, &path)) { 825 if (add_cacheinfo(mode, &oid, path, 0)) 826 die("git update-index: --cacheinfo cannot add %s", path); 827 ctx->argv++; 828 ctx->argc--; 829 return 0; 830 } 831 if (ctx->argc <= 3) 832 return error("option 'cacheinfo' expects <mode>,<sha1>,<path>"); 833 if (strtoul_ui(*++ctx->argv, 8, &mode) || 834 get_oid_hex(*++ctx->argv, &oid) || 835 add_cacheinfo(mode, &oid, *++ctx->argv, 0)) 836 die("git update-index: --cacheinfo cannot add %s", *ctx->argv); 837 ctx->argc -= 3; 838 return 0; 839} 840 841static enum parse_opt_result stdin_cacheinfo_callback( 842 struct parse_opt_ctx_t *ctx, const struct option *opt, 843 const char *arg, int unset) 844{ 845 int *nul_term_line = opt->value; 846 847 BUG_ON_OPT_NEG(unset); 848 BUG_ON_OPT_ARG(arg); 849 850 if (ctx->argc != 1) 851 return error("option '%s' must be the last argument", opt->long_name); 852 allow_add = allow_replace = allow_remove = 1; 853 read_index_info(*nul_term_line); 854 return 0; 855} 856 857static enum parse_opt_result stdin_callback( 858 struct parse_opt_ctx_t *ctx, const struct option *opt, 859 const char *arg, int unset) 860{ 861 int *read_from_stdin = opt->value; 862 863 BUG_ON_OPT_NEG(unset); 864 BUG_ON_OPT_ARG(arg); 865 866 if (ctx->argc != 1) 867 return error("option '%s' must be the last argument", opt->long_name); 868 *read_from_stdin = 1; 869 return 0; 870} 871 872static enum parse_opt_result unresolve_callback( 873 struct parse_opt_ctx_t *ctx, const struct option *opt, 874 const char *arg, int unset) 875{ 876 int *has_errors = opt->value; 877 const char *prefix = startup_info->prefix; 878 879 BUG_ON_OPT_NEG(unset); 880 BUG_ON_OPT_ARG(arg); 881 882 /* consume remaining arguments. */ 883 *has_errors = do_unresolve(ctx->argc, ctx->argv, 884 prefix, prefix ? strlen(prefix) : 0); 885 if (*has_errors) 886 the_repository->index->cache_changed = 0; 887 888 ctx->argv += ctx->argc - 1; 889 ctx->argc = 1; 890 return 0; 891} 892 893static enum parse_opt_result reupdate_callback( 894 struct parse_opt_ctx_t *ctx, const struct option *opt, 895 const char *arg, int unset) 896{ 897 int *has_errors = opt->value; 898 const char *prefix = startup_info->prefix; 899 900 BUG_ON_OPT_NEG(unset); 901 BUG_ON_OPT_ARG(arg); 902 903 /* consume remaining arguments. */ 904 setup_work_tree(); 905 *has_errors = do_reupdate(ctx->argv + 1, prefix); 906 if (*has_errors) 907 the_repository->index->cache_changed = 0; 908 909 ctx->argv += ctx->argc - 1; 910 ctx->argc = 1; 911 return 0; 912} 913 914int cmd_update_index(int argc, 915 const char **argv, 916 const char *prefix, 917 struct repository *repo UNUSED) 918{ 919 int newfd, entries, has_errors = 0, nul_term_line = 0; 920 enum uc_mode untracked_cache = UC_UNSPECIFIED; 921 int read_from_stdin = 0; 922 int prefix_length = prefix ? strlen(prefix) : 0; 923 int preferred_index_format = 0; 924 char set_executable_bit = 0; 925 struct refresh_params refresh_args = {0, &has_errors}; 926 int lock_error = 0; 927 int split_index = -1; 928 int force_write = 0; 929 int fsmonitor = -1; 930 struct lock_file lock_file = LOCK_INIT; 931 struct parse_opt_ctx_t ctx; 932 strbuf_getline_fn getline_fn; 933 int parseopt_state = PARSE_OPT_UNKNOWN; 934 struct repository *r = the_repository; 935 struct odb_transaction *transaction; 936 struct option options[] = { 937 OPT_BIT('q', NULL, &refresh_args.flags, 938 N_("continue refresh even when index needs update"), 939 REFRESH_QUIET), 940 OPT_BIT(0, "ignore-submodules", &refresh_args.flags, 941 N_("refresh: ignore submodules"), 942 REFRESH_IGNORE_SUBMODULES), 943 OPT_SET_INT(0, "add", &allow_add, 944 N_("do not ignore new files"), 1), 945 OPT_SET_INT(0, "replace", &allow_replace, 946 N_("let files replace directories and vice-versa"), 1), 947 OPT_SET_INT(0, "remove", &allow_remove, 948 N_("notice files missing from worktree"), 1), 949 OPT_BIT(0, "unmerged", &refresh_args.flags, 950 N_("refresh even if index contains unmerged entries"), 951 REFRESH_UNMERGED), 952 OPT_CALLBACK_F(0, "refresh", &refresh_args, NULL, 953 N_("refresh stat information"), 954 PARSE_OPT_NOARG | PARSE_OPT_NONEG, 955 refresh_callback), 956 OPT_CALLBACK_F(0, "really-refresh", &refresh_args, NULL, 957 N_("like --refresh, but ignore assume-unchanged setting"), 958 PARSE_OPT_NOARG | PARSE_OPT_NONEG, 959 really_refresh_callback), 960 { 961 .type = OPTION_LOWLEVEL_CALLBACK, 962 .long_name = "cacheinfo", 963 .argh = N_("<mode>,<object>,<path>"), 964 .help = N_("add the specified entry to the index"), 965 .flags = PARSE_OPT_NOARG | /* disallow --cacheinfo=<mode> form */ 966 PARSE_OPT_NONEG | PARSE_OPT_LITERAL_ARGHELP, 967 .ll_callback = cacheinfo_callback, 968 }, 969 OPT_CALLBACK_F(0, "chmod", &set_executable_bit, "(+|-)x", 970 N_("override the executable bit of the listed files"), 971 PARSE_OPT_NONEG, 972 chmod_callback), 973 { 974 .type = OPTION_SET_INT, 975 .long_name = "assume-unchanged", 976 .value = &mark_valid_only, 977 .precision = sizeof(mark_valid_only), 978 .help = N_("mark files as \"not changing\""), 979 .flags = PARSE_OPT_NOARG | PARSE_OPT_NONEG, 980 .defval = MARK_FLAG, 981 }, 982 { 983 .type = OPTION_SET_INT, 984 .long_name = "no-assume-unchanged", 985 .value = &mark_valid_only, 986 .precision = sizeof(mark_valid_only), 987 .help = N_("clear assumed-unchanged bit"), 988 .flags = PARSE_OPT_NOARG | PARSE_OPT_NONEG, 989 .defval = UNMARK_FLAG, 990 }, 991 { 992 .type = OPTION_SET_INT, 993 .long_name = "skip-worktree", 994 .value = &mark_skip_worktree_only, 995 .precision = sizeof(mark_skip_worktree_only), 996 .help = N_("mark files as \"index-only\""), 997 .flags = PARSE_OPT_NOARG | PARSE_OPT_NONEG, 998 .defval = MARK_FLAG, 999 }, 1000 { 1001 .type = OPTION_SET_INT, 1002 .long_name = "no-skip-worktree", 1003 .value = &mark_skip_worktree_only, 1004 .precision = sizeof(mark_skip_worktree_only), 1005 .help = N_("clear skip-worktree bit"), 1006 .flags = PARSE_OPT_NOARG | PARSE_OPT_NONEG, 1007 .defval = UNMARK_FLAG, 1008 }, 1009 OPT_BOOL(0, "ignore-skip-worktree-entries", &ignore_skip_worktree_entries, 1010 N_("do not touch index-only entries")), 1011 OPT_SET_INT(0, "info-only", &info_only, 1012 N_("add to index only; do not add content to object database"), 1), 1013 OPT_SET_INT(0, "force-remove", &force_remove, 1014 N_("remove named paths even if present in worktree"), 1), 1015 OPT_BOOL('z', NULL, &nul_term_line, 1016 N_("with --stdin: input lines are terminated by null bytes")), 1017 { 1018 .type = OPTION_LOWLEVEL_CALLBACK, 1019 .long_name = "stdin", 1020 .value = &read_from_stdin, 1021 .help = N_("read list of paths to be updated from standard input"), 1022 .flags = PARSE_OPT_NONEG | PARSE_OPT_NOARG, 1023 .ll_callback = stdin_callback, 1024 }, 1025 { 1026 .type = OPTION_LOWLEVEL_CALLBACK, 1027 .long_name = "index-info", 1028 .value = &nul_term_line, 1029 .help = N_("add entries from standard input to the index"), 1030 .flags = PARSE_OPT_NONEG | PARSE_OPT_NOARG, 1031 .ll_callback = stdin_cacheinfo_callback, 1032 }, 1033 { 1034 .type = OPTION_LOWLEVEL_CALLBACK, 1035 .long_name = "unresolve", 1036 .value = &has_errors, 1037 .help = N_("repopulate stages #2 and #3 for the listed paths"), 1038 .flags = PARSE_OPT_NONEG | PARSE_OPT_NOARG, 1039 .ll_callback = unresolve_callback, 1040 }, 1041 { 1042 .type = OPTION_LOWLEVEL_CALLBACK, 1043 .short_name = 'g', 1044 .long_name = "again", 1045 .value = &has_errors, 1046 .help = N_("only update entries that differ from HEAD"), 1047 .flags = PARSE_OPT_NONEG | PARSE_OPT_NOARG, 1048 .ll_callback = reupdate_callback, 1049 }, 1050 OPT_BIT(0, "ignore-missing", &refresh_args.flags, 1051 N_("ignore files missing from worktree"), 1052 REFRESH_IGNORE_MISSING), 1053 OPT_SET_INT(0, "verbose", &verbose, 1054 N_("report actions to standard output"), 1), 1055 OPT_CALLBACK_F(0, "clear-resolve-undo", NULL, NULL, 1056 N_("(for porcelains) forget saved unresolved conflicts"), 1057 PARSE_OPT_NOARG | PARSE_OPT_NONEG, 1058 resolve_undo_clear_callback), 1059 OPT_INTEGER(0, "index-version", &preferred_index_format, 1060 N_("write index in this format")), 1061 OPT_SET_INT(0, "show-index-version", &preferred_index_format, 1062 N_("report on-disk index format version"), -1), 1063 OPT_BOOL(0, "split-index", &split_index, 1064 N_("enable or disable split index")), 1065 OPT_BOOL(0, "untracked-cache", &untracked_cache, 1066 N_("enable/disable untracked cache")), 1067 OPT_SET_INT(0, "test-untracked-cache", &untracked_cache, 1068 N_("test if the filesystem supports untracked cache"), UC_TEST), 1069 OPT_SET_INT(0, "force-untracked-cache", &untracked_cache, 1070 N_("enable untracked cache without testing the filesystem"), UC_FORCE), 1071 OPT_SET_INT(0, "force-write-index", &force_write, 1072 N_("write out the index even if is not flagged as changed"), 1), 1073 OPT_BOOL(0, "fsmonitor", &fsmonitor, 1074 N_("enable or disable file system monitor")), 1075 { 1076 .type = OPTION_SET_INT, 1077 .long_name = "fsmonitor-valid", 1078 .value = &mark_fsmonitor_only, 1079 .precision = sizeof(mark_fsmonitor_only), 1080 .help = N_("mark files as fsmonitor valid"), 1081 .flags = PARSE_OPT_NOARG | PARSE_OPT_NONEG, 1082 .defval = MARK_FLAG, 1083 }, 1084 { 1085 .type = OPTION_SET_INT, 1086 .long_name = "no-fsmonitor-valid", 1087 .value = &mark_fsmonitor_only, 1088 .precision = sizeof(mark_fsmonitor_only), 1089 .help = N_("clear fsmonitor valid bit"), 1090 .flags = PARSE_OPT_NOARG | PARSE_OPT_NONEG, 1091 .defval = UNMARK_FLAG, 1092 }, 1093 OPT_END() 1094 }; 1095 1096 show_usage_with_options_if_asked(argc, argv, 1097 update_index_usage, options); 1098 1099 repo_config(the_repository, git_default_config, NULL); 1100 1101 prepare_repo_settings(r); 1102 the_repository->settings.command_requires_full_index = 0; 1103 1104 /* we will diagnose later if it turns out that we need to update it */ 1105 newfd = repo_hold_locked_index(the_repository, &lock_file, 0); 1106 if (newfd < 0) 1107 lock_error = errno; 1108 1109 entries = repo_read_index(the_repository); 1110 if (entries < 0) 1111 die("cache corrupted"); 1112 1113 the_repository->index->updated_skipworktree = 1; 1114 1115 /* 1116 * Custom copy of parse_options() because we want to handle 1117 * filename arguments as they come. 1118 */ 1119 parse_options_start(&ctx, argc, argv, prefix, 1120 options, PARSE_OPT_STOP_AT_NON_OPTION); 1121 1122 /* 1123 * Allow the object layer to optimize adding multiple objects in 1124 * a batch. 1125 */ 1126 transaction = odb_transaction_begin(the_repository->objects); 1127 while (ctx.argc) { 1128 if (parseopt_state != PARSE_OPT_DONE) 1129 parseopt_state = parse_options_step(&ctx, options, 1130 update_index_usage); 1131 if (!ctx.argc) 1132 break; 1133 switch (parseopt_state) { 1134 case PARSE_OPT_HELP: 1135 case PARSE_OPT_ERROR: 1136 exit(129); 1137 case PARSE_OPT_COMPLETE: 1138 exit(0); 1139 case PARSE_OPT_NON_OPTION: 1140 case PARSE_OPT_DONE: 1141 { 1142 const char *path = ctx.argv[0]; 1143 char *p; 1144 1145 /* 1146 * It is possible, though unlikely, that a caller could 1147 * use the verbose output to synchronize with addition 1148 * of objects to the object database. The current 1149 * implementation of ODB transactions leaves objects 1150 * invisible while a transaction is active, so end the 1151 * transaction here early before processing the next 1152 * update. All further updates are performed outside of 1153 * a transaction. 1154 */ 1155 if (transaction && verbose) { 1156 odb_transaction_commit(transaction); 1157 transaction = NULL; 1158 } 1159 1160 setup_work_tree(); 1161 p = prefix_path(prefix, prefix_length, path); 1162 update_one(p); 1163 if (set_executable_bit) 1164 chmod_path(set_executable_bit, p); 1165 free(p); 1166 ctx.argc--; 1167 ctx.argv++; 1168 break; 1169 } 1170 case PARSE_OPT_UNKNOWN: 1171 if (ctx.argv[0][1] == '-') 1172 error("unknown option '%s'", ctx.argv[0] + 2); 1173 else 1174 error("unknown switch '%c'", *ctx.opt); 1175 usage_with_options(update_index_usage, options); 1176 } 1177 } 1178 argc = parse_options_end(&ctx); 1179 1180 getline_fn = nul_term_line ? strbuf_getline_nul : strbuf_getline_lf; 1181 if (preferred_index_format) { 1182 if (preferred_index_format < 0) { 1183 printf(_("%d\n"), the_repository->index->version); 1184 } else if (preferred_index_format < INDEX_FORMAT_LB || 1185 INDEX_FORMAT_UB < preferred_index_format) { 1186 die("index-version %d not in range: %d..%d", 1187 preferred_index_format, 1188 INDEX_FORMAT_LB, INDEX_FORMAT_UB); 1189 } else { 1190 if (the_repository->index->version != preferred_index_format) 1191 the_repository->index->cache_changed |= SOMETHING_CHANGED; 1192 report(_("index-version: was %d, set to %d"), 1193 the_repository->index->version, preferred_index_format); 1194 the_repository->index->version = preferred_index_format; 1195 } 1196 } 1197 1198 if (read_from_stdin) { 1199 struct strbuf buf = STRBUF_INIT; 1200 struct strbuf unquoted = STRBUF_INIT; 1201 1202 setup_work_tree(); 1203 while (getline_fn(&buf, stdin) != EOF) { 1204 char *p; 1205 if (!nul_term_line && buf.buf[0] == '"') { 1206 strbuf_reset(&unquoted); 1207 if (unquote_c_style(&unquoted, buf.buf, NULL)) 1208 die("line is badly quoted"); 1209 strbuf_swap(&buf, &unquoted); 1210 } 1211 p = prefix_path(prefix, prefix_length, buf.buf); 1212 update_one(p); 1213 if (set_executable_bit) 1214 chmod_path(set_executable_bit, p); 1215 free(p); 1216 } 1217 strbuf_release(&unquoted); 1218 strbuf_release(&buf); 1219 } 1220 1221 /* 1222 * By now we have added all of the new objects 1223 */ 1224 odb_transaction_commit(transaction); 1225 1226 if (split_index > 0) { 1227 if (repo_config_get_split_index(the_repository) == 0) 1228 warning(_("core.splitIndex is set to false; " 1229 "remove or change it, if you really want to " 1230 "enable split index")); 1231 if (the_repository->index->split_index) 1232 the_repository->index->cache_changed |= SPLIT_INDEX_ORDERED; 1233 else 1234 add_split_index(the_repository->index); 1235 } else if (!split_index) { 1236 if (repo_config_get_split_index(the_repository) == 1) 1237 warning(_("core.splitIndex is set to true; " 1238 "remove or change it, if you really want to " 1239 "disable split index")); 1240 remove_split_index(the_repository->index); 1241 } 1242 1243 prepare_repo_settings(r); 1244 switch (untracked_cache) { 1245 case UC_UNSPECIFIED: 1246 break; 1247 case UC_DISABLE: 1248 if (r->settings.core_untracked_cache == UNTRACKED_CACHE_WRITE) 1249 warning(_("core.untrackedCache is set to true; " 1250 "remove or change it, if you really want to " 1251 "disable the untracked cache")); 1252 remove_untracked_cache(the_repository->index); 1253 report(_("Untracked cache disabled")); 1254 break; 1255 case UC_TEST: 1256 setup_work_tree(); 1257 return !test_if_untracked_cache_is_supported(); 1258 case UC_ENABLE: 1259 case UC_FORCE: 1260 if (r->settings.core_untracked_cache == UNTRACKED_CACHE_REMOVE) 1261 warning(_("core.untrackedCache is set to false; " 1262 "remove or change it, if you really want to " 1263 "enable the untracked cache")); 1264 add_untracked_cache(the_repository->index); 1265 report(_("Untracked cache enabled for '%s'"), repo_get_work_tree(the_repository)); 1266 break; 1267 default: 1268 BUG("bad untracked_cache value: %d", untracked_cache); 1269 } 1270 1271 if (fsmonitor > 0) { 1272 enum fsmonitor_mode fsm_mode = fsm_settings__get_mode(r); 1273 enum fsmonitor_reason reason = fsm_settings__get_reason(r); 1274 1275 /* 1276 * The user wants to turn on FSMonitor using the command 1277 * line argument. (We don't know (or care) whether that 1278 * is the IPC or HOOK version.) 1279 * 1280 * Use one of the __get routines to force load the FSMonitor 1281 * config settings into the repo-settings. That will detect 1282 * whether the file system is compatible so that we can stop 1283 * here with a nice error message. 1284 */ 1285 if (reason > FSMONITOR_REASON_OK) 1286 die("%s", 1287 fsm_settings__get_incompatible_msg(r, reason)); 1288 1289 if (fsm_mode == FSMONITOR_MODE_DISABLED) { 1290 warning(_("core.fsmonitor is unset; " 1291 "set it if you really want to " 1292 "enable fsmonitor")); 1293 } 1294 add_fsmonitor(the_repository->index); 1295 report(_("fsmonitor enabled")); 1296 } else if (!fsmonitor) { 1297 enum fsmonitor_mode fsm_mode = fsm_settings__get_mode(r); 1298 if (fsm_mode > FSMONITOR_MODE_DISABLED) 1299 warning(_("core.fsmonitor is set; " 1300 "remove it if you really want to " 1301 "disable fsmonitor")); 1302 remove_fsmonitor(the_repository->index); 1303 report(_("fsmonitor disabled")); 1304 } 1305 1306 if (the_repository->index->cache_changed || force_write) { 1307 if (newfd < 0) { 1308 if (refresh_args.flags & REFRESH_QUIET) 1309 exit(128); 1310 unable_to_lock_die(repo_get_index_file(the_repository), lock_error); 1311 } 1312 if (write_locked_index(the_repository->index, &lock_file, COMMIT_LOCK)) 1313 die("Unable to write new index file"); 1314 } 1315 1316 rollback_lock_file(&lock_file); 1317 1318 return has_errors ? 1 : 0; 1319}