1#include "../cache.h" 2#include "../refs.h" 3#include "refs-internal.h" 4#include "ref-cache.h" 5#include "../iterator.h" 6#include "../dir-iterator.h" 7#include "../lockfile.h" 8#include "../object.h" 9#include "../dir.h" 10 11struct ref_lock { 12 char *ref_name; 13 struct lock_file *lk; 14 struct object_id old_oid; 15}; 16 17/* 18 * Return true if refname, which has the specified oid and flags, can 19 * be resolved to an object in the database. If the referred-to object 20 * does not exist, emit a warning and return false. 21 */ 22static int ref_resolves_to_object(const char *refname, 23 const struct object_id *oid, 24 unsigned int flags) 25{ 26 if (flags & REF_ISBROKEN) 27 return 0; 28 if (!has_sha1_file(oid->hash)) { 29 error("%s does not point to a valid object!", refname); 30 return 0; 31 } 32 return 1; 33} 34 35struct packed_ref_cache { 36 struct ref_cache *cache; 37 38 /* 39 * Count of references to the data structure in this instance, 40 * including the pointer from files_ref_store::packed if any. 41 * The data will not be freed as long as the reference count 42 * is nonzero. 43 */ 44 unsigned int referrers; 45 46 /* The metadata from when this packed-refs cache was read */ 47 struct stat_validity validity; 48}; 49 50/* 51 * A container for `packed-refs`-related data. It is not (yet) a 52 * `ref_store`. 53 */ 54struct packed_ref_store { 55 unsigned int store_flags; 56 57 /* The path of the "packed-refs" file: */ 58 char *path; 59 60 /* 61 * A cache of the values read from the `packed-refs` file, if 62 * it might still be current; otherwise, NULL. 63 */ 64 struct packed_ref_cache *cache; 65 66 /* 67 * Lock used for the "packed-refs" file. Note that this (and 68 * thus the enclosing `packed_ref_store`) must not be freed. 69 */ 70 struct lock_file lock; 71}; 72 73static struct packed_ref_store *packed_ref_store_create( 74 const char *path, unsigned int store_flags) 75{ 76 struct packed_ref_store *refs = xcalloc(1, sizeof(*refs)); 77 78 refs->store_flags = store_flags; 79 refs->path = xstrdup(path); 80 return refs; 81} 82 83/* 84 * Die if refs is not the main ref store. caller is used in any 85 * necessary error messages. 86 */ 87static void packed_assert_main_repository(struct packed_ref_store *refs, 88 const char *caller) 89{ 90 if (refs->store_flags & REF_STORE_MAIN) 91 return; 92 93 die("BUG: operation %s only allowed for main ref store", caller); 94} 95 96/* 97 * Future: need to be in "struct repository" 98 * when doing a full libification. 99 */ 100struct files_ref_store { 101 struct ref_store base; 102 unsigned int store_flags; 103 104 char *gitdir; 105 char *gitcommondir; 106 107 struct ref_cache *loose; 108 109 struct packed_ref_store *packed_ref_store; 110}; 111 112/* 113 * Increment the reference count of *packed_refs. 114 */ 115static void acquire_packed_ref_cache(struct packed_ref_cache *packed_refs) 116{ 117 packed_refs->referrers++; 118} 119 120/* 121 * Decrease the reference count of *packed_refs. If it goes to zero, 122 * free *packed_refs and return true; otherwise return false. 123 */ 124static int release_packed_ref_cache(struct packed_ref_cache *packed_refs) 125{ 126 if (!--packed_refs->referrers) { 127 free_ref_cache(packed_refs->cache); 128 stat_validity_clear(&packed_refs->validity); 129 free(packed_refs); 130 return 1; 131 } else { 132 return 0; 133 } 134} 135 136static void clear_packed_ref_cache(struct packed_ref_store *refs) 137{ 138 if (refs->cache) { 139 struct packed_ref_cache *cache = refs->cache; 140 141 if (is_lock_file_locked(&refs->lock)) 142 die("BUG: packed-ref cache cleared while locked"); 143 refs->cache = NULL; 144 release_packed_ref_cache(cache); 145 } 146} 147 148static void clear_loose_ref_cache(struct files_ref_store *refs) 149{ 150 if (refs->loose) { 151 free_ref_cache(refs->loose); 152 refs->loose = NULL; 153 } 154} 155 156/* 157 * Create a new submodule ref cache and add it to the internal 158 * set of caches. 159 */ 160static struct ref_store *files_ref_store_create(const char *gitdir, 161 unsigned int flags) 162{ 163 struct files_ref_store *refs = xcalloc(1, sizeof(*refs)); 164 struct ref_store *ref_store = (struct ref_store *)refs; 165 struct strbuf sb = STRBUF_INIT; 166 167 base_ref_store_init(ref_store, &refs_be_files); 168 refs->store_flags = flags; 169 170 refs->gitdir = xstrdup(gitdir); 171 get_common_dir_noenv(&sb, gitdir); 172 refs->gitcommondir = strbuf_detach(&sb, NULL); 173 strbuf_addf(&sb, "%s/packed-refs", refs->gitcommondir); 174 refs->packed_ref_store = packed_ref_store_create(sb.buf, flags); 175 strbuf_release(&sb); 176 177 return ref_store; 178} 179 180/* 181 * Die if refs is not the main ref store. caller is used in any 182 * necessary error messages. 183 */ 184static void files_assert_main_repository(struct files_ref_store *refs, 185 const char *caller) 186{ 187 if (refs->store_flags & REF_STORE_MAIN) 188 return; 189 190 die("BUG: operation %s only allowed for main ref store", caller); 191} 192 193/* 194 * Downcast ref_store to files_ref_store. Die if ref_store is not a 195 * files_ref_store. required_flags is compared with ref_store's 196 * store_flags to ensure the ref_store has all required capabilities. 197 * "caller" is used in any necessary error messages. 198 */ 199static struct files_ref_store *files_downcast(struct ref_store *ref_store, 200 unsigned int required_flags, 201 const char *caller) 202{ 203 struct files_ref_store *refs; 204 205 if (ref_store->be != &refs_be_files) 206 die("BUG: ref_store is type \"%s\" not \"files\" in %s", 207 ref_store->be->name, caller); 208 209 refs = (struct files_ref_store *)ref_store; 210 211 if ((refs->store_flags & required_flags) != required_flags) 212 die("BUG: operation %s requires abilities 0x%x, but only have 0x%x", 213 caller, required_flags, refs->store_flags); 214 215 return refs; 216} 217 218/* The length of a peeled reference line in packed-refs, including EOL: */ 219#define PEELED_LINE_LENGTH 42 220 221/* 222 * The packed-refs header line that we write out. Perhaps other 223 * traits will be added later. The trailing space is required. 224 */ 225static const char PACKED_REFS_HEADER[] = 226 "# pack-refs with: peeled fully-peeled \n"; 227 228/* 229 * Parse one line from a packed-refs file. Write the SHA1 to sha1. 230 * Return a pointer to the refname within the line (null-terminated), 231 * or NULL if there was a problem. 232 */ 233static const char *parse_ref_line(struct strbuf *line, struct object_id *oid) 234{ 235 const char *ref; 236 237 if (parse_oid_hex(line->buf, oid, &ref) < 0) 238 return NULL; 239 if (!isspace(*ref++)) 240 return NULL; 241 242 if (isspace(*ref)) 243 return NULL; 244 245 if (line->buf[line->len - 1] != '\n') 246 return NULL; 247 line->buf[--line->len] = 0; 248 249 return ref; 250} 251 252/* 253 * Read from `packed_refs_file` into a newly-allocated 254 * `packed_ref_cache` and return it. The return value will already 255 * have its reference count incremented. 256 * 257 * A comment line of the form "# pack-refs with: " may contain zero or 258 * more traits. We interpret the traits as follows: 259 * 260 * No traits: 261 * 262 * Probably no references are peeled. But if the file contains a 263 * peeled value for a reference, we will use it. 264 * 265 * peeled: 266 * 267 * References under "refs/tags/", if they *can* be peeled, *are* 268 * peeled in this file. References outside of "refs/tags/" are 269 * probably not peeled even if they could have been, but if we find 270 * a peeled value for such a reference we will use it. 271 * 272 * fully-peeled: 273 * 274 * All references in the file that can be peeled are peeled. 275 * Inversely (and this is more important), any references in the 276 * file for which no peeled value is recorded is not peelable. This 277 * trait should typically be written alongside "peeled" for 278 * compatibility with older clients, but we do not require it 279 * (i.e., "peeled" is a no-op if "fully-peeled" is set). 280 */ 281static struct packed_ref_cache *read_packed_refs(const char *packed_refs_file) 282{ 283 FILE *f; 284 struct packed_ref_cache *packed_refs = xcalloc(1, sizeof(*packed_refs)); 285 struct ref_entry *last = NULL; 286 struct strbuf line = STRBUF_INIT; 287 enum { PEELED_NONE, PEELED_TAGS, PEELED_FULLY } peeled = PEELED_NONE; 288 struct ref_dir *dir; 289 290 acquire_packed_ref_cache(packed_refs); 291 packed_refs->cache = create_ref_cache(NULL, NULL); 292 packed_refs->cache->root->flag &= ~REF_INCOMPLETE; 293 294 f = fopen(packed_refs_file, "r"); 295 if (!f) { 296 if (errno == ENOENT) { 297 /* 298 * This is OK; it just means that no 299 * "packed-refs" file has been written yet, 300 * which is equivalent to it being empty. 301 */ 302 return packed_refs; 303 } else { 304 die_errno("couldn't read %s", packed_refs_file); 305 } 306 } 307 308 stat_validity_update(&packed_refs->validity, fileno(f)); 309 310 dir = get_ref_dir(packed_refs->cache->root); 311 while (strbuf_getwholeline(&line, f, '\n') != EOF) { 312 struct object_id oid; 313 const char *refname; 314 const char *traits; 315 316 if (skip_prefix(line.buf, "# pack-refs with:", &traits)) { 317 if (strstr(traits, " fully-peeled ")) 318 peeled = PEELED_FULLY; 319 else if (strstr(traits, " peeled ")) 320 peeled = PEELED_TAGS; 321 /* perhaps other traits later as well */ 322 continue; 323 } 324 325 refname = parse_ref_line(&line, &oid); 326 if (refname) { 327 int flag = REF_ISPACKED; 328 329 if (check_refname_format(refname, REFNAME_ALLOW_ONELEVEL)) { 330 if (!refname_is_safe(refname)) 331 die("packed refname is dangerous: %s", refname); 332 oidclr(&oid); 333 flag |= REF_BAD_NAME | REF_ISBROKEN; 334 } 335 last = create_ref_entry(refname, &oid, flag); 336 if (peeled == PEELED_FULLY || 337 (peeled == PEELED_TAGS && starts_with(refname, "refs/tags/"))) 338 last->flag |= REF_KNOWS_PEELED; 339 add_ref_entry(dir, last); 340 continue; 341 } 342 if (last && 343 line.buf[0] == '^' && 344 line.len == PEELED_LINE_LENGTH && 345 line.buf[PEELED_LINE_LENGTH - 1] == '\n' && 346 !get_oid_hex(line.buf + 1, &oid)) { 347 oidcpy(&last->u.value.peeled, &oid); 348 /* 349 * Regardless of what the file header said, 350 * we definitely know the value of *this* 351 * reference: 352 */ 353 last->flag |= REF_KNOWS_PEELED; 354 } 355 } 356 357 fclose(f); 358 strbuf_release(&line); 359 360 return packed_refs; 361} 362 363static void files_reflog_path(struct files_ref_store *refs, 364 struct strbuf *sb, 365 const char *refname) 366{ 367 if (!refname) { 368 /* 369 * FIXME: of course this is wrong in multi worktree 370 * setting. To be fixed real soon. 371 */ 372 strbuf_addf(sb, "%s/logs", refs->gitcommondir); 373 return; 374 } 375 376 switch (ref_type(refname)) { 377 case REF_TYPE_PER_WORKTREE: 378 case REF_TYPE_PSEUDOREF: 379 strbuf_addf(sb, "%s/logs/%s", refs->gitdir, refname); 380 break; 381 case REF_TYPE_NORMAL: 382 strbuf_addf(sb, "%s/logs/%s", refs->gitcommondir, refname); 383 break; 384 default: 385 die("BUG: unknown ref type %d of ref %s", 386 ref_type(refname), refname); 387 } 388} 389 390static void files_ref_path(struct files_ref_store *refs, 391 struct strbuf *sb, 392 const char *refname) 393{ 394 switch (ref_type(refname)) { 395 case REF_TYPE_PER_WORKTREE: 396 case REF_TYPE_PSEUDOREF: 397 strbuf_addf(sb, "%s/%s", refs->gitdir, refname); 398 break; 399 case REF_TYPE_NORMAL: 400 strbuf_addf(sb, "%s/%s", refs->gitcommondir, refname); 401 break; 402 default: 403 die("BUG: unknown ref type %d of ref %s", 404 ref_type(refname), refname); 405 } 406} 407 408/* 409 * Check that the packed refs cache (if any) still reflects the 410 * contents of the file. If not, clear the cache. 411 */ 412static void validate_packed_ref_cache(struct packed_ref_store *refs) 413{ 414 if (refs->cache && 415 !stat_validity_check(&refs->cache->validity, refs->path)) 416 clear_packed_ref_cache(refs); 417} 418 419/* 420 * Get the packed_ref_cache for the specified packed_ref_store, 421 * creating and populating it if it hasn't been read before or if the 422 * file has been changed (according to its `validity` field) since it 423 * was last read. On the other hand, if we hold the lock, then assume 424 * that the file hasn't been changed out from under us, so skip the 425 * extra `stat()` call in `stat_validity_check()`. 426 */ 427static struct packed_ref_cache *get_packed_ref_cache(struct packed_ref_store *refs) 428{ 429 if (!is_lock_file_locked(&refs->lock)) 430 validate_packed_ref_cache(refs); 431 432 if (!refs->cache) 433 refs->cache = read_packed_refs(refs->path); 434 435 return refs->cache; 436} 437 438static struct ref_dir *get_packed_ref_dir(struct packed_ref_cache *packed_ref_cache) 439{ 440 return get_ref_dir(packed_ref_cache->cache->root); 441} 442 443static struct ref_dir *get_packed_refs(struct packed_ref_store *refs) 444{ 445 return get_packed_ref_dir(get_packed_ref_cache(refs)); 446} 447 448/* 449 * Add or overwrite a reference in the in-memory packed reference 450 * cache. This may only be called while the packed-refs file is locked 451 * (see lock_packed_refs()). To actually write the packed-refs file, 452 * call commit_packed_refs(). 453 */ 454static void add_packed_ref(struct packed_ref_store *refs, 455 const char *refname, const struct object_id *oid) 456{ 457 struct ref_dir *packed_refs; 458 struct ref_entry *packed_entry; 459 460 if (!is_lock_file_locked(&refs->lock)) 461 die("BUG: packed refs not locked"); 462 463 if (check_refname_format(refname, REFNAME_ALLOW_ONELEVEL)) 464 die("Reference has invalid format: '%s'", refname); 465 466 packed_refs = get_packed_refs(refs); 467 packed_entry = find_ref_entry(packed_refs, refname); 468 if (packed_entry) { 469 /* Overwrite the existing entry: */ 470 oidcpy(&packed_entry->u.value.oid, oid); 471 packed_entry->flag = REF_ISPACKED; 472 oidclr(&packed_entry->u.value.peeled); 473 } else { 474 packed_entry = create_ref_entry(refname, oid, REF_ISPACKED); 475 add_ref_entry(packed_refs, packed_entry); 476 } 477} 478 479/* 480 * Read the loose references from the namespace dirname into dir 481 * (without recursing). dirname must end with '/'. dir must be the 482 * directory entry corresponding to dirname. 483 */ 484static void loose_fill_ref_dir(struct ref_store *ref_store, 485 struct ref_dir *dir, const char *dirname) 486{ 487 struct files_ref_store *refs = 488 files_downcast(ref_store, REF_STORE_READ, "fill_ref_dir"); 489 DIR *d; 490 struct dirent *de; 491 int dirnamelen = strlen(dirname); 492 struct strbuf refname; 493 struct strbuf path = STRBUF_INIT; 494 size_t path_baselen; 495 496 files_ref_path(refs, &path, dirname); 497 path_baselen = path.len; 498 499 d = opendir(path.buf); 500 if (!d) { 501 strbuf_release(&path); 502 return; 503 } 504 505 strbuf_init(&refname, dirnamelen + 257); 506 strbuf_add(&refname, dirname, dirnamelen); 507 508 while ((de = readdir(d)) != NULL) { 509 struct object_id oid; 510 struct stat st; 511 int flag; 512 513 if (de->d_name[0] == '.') 514 continue; 515 if (ends_with(de->d_name, ".lock")) 516 continue; 517 strbuf_addstr(&refname, de->d_name); 518 strbuf_addstr(&path, de->d_name); 519 if (stat(path.buf, &st) < 0) { 520 ; /* silently ignore */ 521 } else if (S_ISDIR(st.st_mode)) { 522 strbuf_addch(&refname, '/'); 523 add_entry_to_dir(dir, 524 create_dir_entry(dir->cache, refname.buf, 525 refname.len, 1)); 526 } else { 527 if (!refs_resolve_ref_unsafe(&refs->base, 528 refname.buf, 529 RESOLVE_REF_READING, 530 oid.hash, &flag)) { 531 oidclr(&oid); 532 flag |= REF_ISBROKEN; 533 } else if (is_null_oid(&oid)) { 534 /* 535 * It is so astronomically unlikely 536 * that NULL_SHA1 is the SHA-1 of an 537 * actual object that we consider its 538 * appearance in a loose reference 539 * file to be repo corruption 540 * (probably due to a software bug). 541 */ 542 flag |= REF_ISBROKEN; 543 } 544 545 if (check_refname_format(refname.buf, 546 REFNAME_ALLOW_ONELEVEL)) { 547 if (!refname_is_safe(refname.buf)) 548 die("loose refname is dangerous: %s", refname.buf); 549 oidclr(&oid); 550 flag |= REF_BAD_NAME | REF_ISBROKEN; 551 } 552 add_entry_to_dir(dir, 553 create_ref_entry(refname.buf, &oid, flag)); 554 } 555 strbuf_setlen(&refname, dirnamelen); 556 strbuf_setlen(&path, path_baselen); 557 } 558 strbuf_release(&refname); 559 strbuf_release(&path); 560 closedir(d); 561 562 /* 563 * Manually add refs/bisect, which, being per-worktree, might 564 * not appear in the directory listing for refs/ in the main 565 * repo. 566 */ 567 if (!strcmp(dirname, "refs/")) { 568 int pos = search_ref_dir(dir, "refs/bisect/", 12); 569 570 if (pos < 0) { 571 struct ref_entry *child_entry = create_dir_entry( 572 dir->cache, "refs/bisect/", 12, 1); 573 add_entry_to_dir(dir, child_entry); 574 } 575 } 576} 577 578static struct ref_cache *get_loose_ref_cache(struct files_ref_store *refs) 579{ 580 if (!refs->loose) { 581 /* 582 * Mark the top-level directory complete because we 583 * are about to read the only subdirectory that can 584 * hold references: 585 */ 586 refs->loose = create_ref_cache(&refs->base, loose_fill_ref_dir); 587 588 /* We're going to fill the top level ourselves: */ 589 refs->loose->root->flag &= ~REF_INCOMPLETE; 590 591 /* 592 * Add an incomplete entry for "refs/" (to be filled 593 * lazily): 594 */ 595 add_entry_to_dir(get_ref_dir(refs->loose->root), 596 create_dir_entry(refs->loose, "refs/", 5, 1)); 597 } 598 return refs->loose; 599} 600 601/* 602 * Return the ref_entry for the given refname from the packed 603 * references. If it does not exist, return NULL. 604 */ 605static struct ref_entry *get_packed_ref(struct files_ref_store *refs, 606 const char *refname) 607{ 608 return find_ref_entry(get_packed_refs(refs->packed_ref_store), refname); 609} 610 611/* 612 * A loose ref file doesn't exist; check for a packed ref. 613 */ 614static int resolve_packed_ref(struct files_ref_store *refs, 615 const char *refname, 616 unsigned char *sha1, unsigned int *flags) 617{ 618 struct ref_entry *entry; 619 620 /* 621 * The loose reference file does not exist; check for a packed 622 * reference. 623 */ 624 entry = get_packed_ref(refs, refname); 625 if (entry) { 626 hashcpy(sha1, entry->u.value.oid.hash); 627 *flags |= REF_ISPACKED; 628 return 0; 629 } 630 /* refname is not a packed reference. */ 631 return -1; 632} 633 634static int files_read_raw_ref(struct ref_store *ref_store, 635 const char *refname, unsigned char *sha1, 636 struct strbuf *referent, unsigned int *type) 637{ 638 struct files_ref_store *refs = 639 files_downcast(ref_store, REF_STORE_READ, "read_raw_ref"); 640 struct strbuf sb_contents = STRBUF_INIT; 641 struct strbuf sb_path = STRBUF_INIT; 642 const char *path; 643 const char *buf; 644 struct stat st; 645 int fd; 646 int ret = -1; 647 int save_errno; 648 int remaining_retries = 3; 649 650 *type = 0; 651 strbuf_reset(&sb_path); 652 653 files_ref_path(refs, &sb_path, refname); 654 655 path = sb_path.buf; 656 657stat_ref: 658 /* 659 * We might have to loop back here to avoid a race 660 * condition: first we lstat() the file, then we try 661 * to read it as a link or as a file. But if somebody 662 * changes the type of the file (file <-> directory 663 * <-> symlink) between the lstat() and reading, then 664 * we don't want to report that as an error but rather 665 * try again starting with the lstat(). 666 * 667 * We'll keep a count of the retries, though, just to avoid 668 * any confusing situation sending us into an infinite loop. 669 */ 670 671 if (remaining_retries-- <= 0) 672 goto out; 673 674 if (lstat(path, &st) < 0) { 675 if (errno != ENOENT) 676 goto out; 677 if (resolve_packed_ref(refs, refname, sha1, type)) { 678 errno = ENOENT; 679 goto out; 680 } 681 ret = 0; 682 goto out; 683 } 684 685 /* Follow "normalized" - ie "refs/.." symlinks by hand */ 686 if (S_ISLNK(st.st_mode)) { 687 strbuf_reset(&sb_contents); 688 if (strbuf_readlink(&sb_contents, path, 0) < 0) { 689 if (errno == ENOENT || errno == EINVAL) 690 /* inconsistent with lstat; retry */ 691 goto stat_ref; 692 else 693 goto out; 694 } 695 if (starts_with(sb_contents.buf, "refs/") && 696 !check_refname_format(sb_contents.buf, 0)) { 697 strbuf_swap(&sb_contents, referent); 698 *type |= REF_ISSYMREF; 699 ret = 0; 700 goto out; 701 } 702 /* 703 * It doesn't look like a refname; fall through to just 704 * treating it like a non-symlink, and reading whatever it 705 * points to. 706 */ 707 } 708 709 /* Is it a directory? */ 710 if (S_ISDIR(st.st_mode)) { 711 /* 712 * Even though there is a directory where the loose 713 * ref is supposed to be, there could still be a 714 * packed ref: 715 */ 716 if (resolve_packed_ref(refs, refname, sha1, type)) { 717 errno = EISDIR; 718 goto out; 719 } 720 ret = 0; 721 goto out; 722 } 723 724 /* 725 * Anything else, just open it and try to use it as 726 * a ref 727 */ 728 fd = open(path, O_RDONLY); 729 if (fd < 0) { 730 if (errno == ENOENT && !S_ISLNK(st.st_mode)) 731 /* inconsistent with lstat; retry */ 732 goto stat_ref; 733 else 734 goto out; 735 } 736 strbuf_reset(&sb_contents); 737 if (strbuf_read(&sb_contents, fd, 256) < 0) { 738 int save_errno = errno; 739 close(fd); 740 errno = save_errno; 741 goto out; 742 } 743 close(fd); 744 strbuf_rtrim(&sb_contents); 745 buf = sb_contents.buf; 746 if (starts_with(buf, "ref:")) { 747 buf += 4; 748 while (isspace(*buf)) 749 buf++; 750 751 strbuf_reset(referent); 752 strbuf_addstr(referent, buf); 753 *type |= REF_ISSYMREF; 754 ret = 0; 755 goto out; 756 } 757 758 /* 759 * Please note that FETCH_HEAD has additional 760 * data after the sha. 761 */ 762 if (get_sha1_hex(buf, sha1) || 763 (buf[40] != '\0' && !isspace(buf[40]))) { 764 *type |= REF_ISBROKEN; 765 errno = EINVAL; 766 goto out; 767 } 768 769 ret = 0; 770 771out: 772 save_errno = errno; 773 strbuf_release(&sb_path); 774 strbuf_release(&sb_contents); 775 errno = save_errno; 776 return ret; 777} 778 779static void unlock_ref(struct ref_lock *lock) 780{ 781 /* Do not free lock->lk -- atexit() still looks at them */ 782 if (lock->lk) 783 rollback_lock_file(lock->lk); 784 free(lock->ref_name); 785 free(lock); 786} 787 788/* 789 * Lock refname, without following symrefs, and set *lock_p to point 790 * at a newly-allocated lock object. Fill in lock->old_oid, referent, 791 * and type similarly to read_raw_ref(). 792 * 793 * The caller must verify that refname is a "safe" reference name (in 794 * the sense of refname_is_safe()) before calling this function. 795 * 796 * If the reference doesn't already exist, verify that refname doesn't 797 * have a D/F conflict with any existing references. extras and skip 798 * are passed to refs_verify_refname_available() for this check. 799 * 800 * If mustexist is not set and the reference is not found or is 801 * broken, lock the reference anyway but clear sha1. 802 * 803 * Return 0 on success. On failure, write an error message to err and 804 * return TRANSACTION_NAME_CONFLICT or TRANSACTION_GENERIC_ERROR. 805 * 806 * Implementation note: This function is basically 807 * 808 * lock reference 809 * read_raw_ref() 810 * 811 * but it includes a lot more code to 812 * - Deal with possible races with other processes 813 * - Avoid calling refs_verify_refname_available() when it can be 814 * avoided, namely if we were successfully able to read the ref 815 * - Generate informative error messages in the case of failure 816 */ 817static int lock_raw_ref(struct files_ref_store *refs, 818 const char *refname, int mustexist, 819 const struct string_list *extras, 820 const struct string_list *skip, 821 struct ref_lock **lock_p, 822 struct strbuf *referent, 823 unsigned int *type, 824 struct strbuf *err) 825{ 826 struct ref_lock *lock; 827 struct strbuf ref_file = STRBUF_INIT; 828 int attempts_remaining = 3; 829 int ret = TRANSACTION_GENERIC_ERROR; 830 831 assert(err); 832 files_assert_main_repository(refs, "lock_raw_ref"); 833 834 *type = 0; 835 836 /* First lock the file so it can't change out from under us. */ 837 838 *lock_p = lock = xcalloc(1, sizeof(*lock)); 839 840 lock->ref_name = xstrdup(refname); 841 files_ref_path(refs, &ref_file, refname); 842 843retry: 844 switch (safe_create_leading_directories(ref_file.buf)) { 845 case SCLD_OK: 846 break; /* success */ 847 case SCLD_EXISTS: 848 /* 849 * Suppose refname is "refs/foo/bar". We just failed 850 * to create the containing directory, "refs/foo", 851 * because there was a non-directory in the way. This 852 * indicates a D/F conflict, probably because of 853 * another reference such as "refs/foo". There is no 854 * reason to expect this error to be transitory. 855 */ 856 if (refs_verify_refname_available(&refs->base, refname, 857 extras, skip, err)) { 858 if (mustexist) { 859 /* 860 * To the user the relevant error is 861 * that the "mustexist" reference is 862 * missing: 863 */ 864 strbuf_reset(err); 865 strbuf_addf(err, "unable to resolve reference '%s'", 866 refname); 867 } else { 868 /* 869 * The error message set by 870 * refs_verify_refname_available() is 871 * OK. 872 */ 873 ret = TRANSACTION_NAME_CONFLICT; 874 } 875 } else { 876 /* 877 * The file that is in the way isn't a loose 878 * reference. Report it as a low-level 879 * failure. 880 */ 881 strbuf_addf(err, "unable to create lock file %s.lock; " 882 "non-directory in the way", 883 ref_file.buf); 884 } 885 goto error_return; 886 case SCLD_VANISHED: 887 /* Maybe another process was tidying up. Try again. */ 888 if (--attempts_remaining > 0) 889 goto retry; 890 /* fall through */ 891 default: 892 strbuf_addf(err, "unable to create directory for %s", 893 ref_file.buf); 894 goto error_return; 895 } 896 897 if (!lock->lk) 898 lock->lk = xcalloc(1, sizeof(struct lock_file)); 899 900 if (hold_lock_file_for_update(lock->lk, ref_file.buf, LOCK_NO_DEREF) < 0) { 901 if (errno == ENOENT && --attempts_remaining > 0) { 902 /* 903 * Maybe somebody just deleted one of the 904 * directories leading to ref_file. Try 905 * again: 906 */ 907 goto retry; 908 } else { 909 unable_to_lock_message(ref_file.buf, errno, err); 910 goto error_return; 911 } 912 } 913 914 /* 915 * Now we hold the lock and can read the reference without 916 * fear that its value will change. 917 */ 918 919 if (files_read_raw_ref(&refs->base, refname, 920 lock->old_oid.hash, referent, type)) { 921 if (errno == ENOENT) { 922 if (mustexist) { 923 /* Garden variety missing reference. */ 924 strbuf_addf(err, "unable to resolve reference '%s'", 925 refname); 926 goto error_return; 927 } else { 928 /* 929 * Reference is missing, but that's OK. We 930 * know that there is not a conflict with 931 * another loose reference because 932 * (supposing that we are trying to lock 933 * reference "refs/foo/bar"): 934 * 935 * - We were successfully able to create 936 * the lockfile refs/foo/bar.lock, so we 937 * know there cannot be a loose reference 938 * named "refs/foo". 939 * 940 * - We got ENOENT and not EISDIR, so we 941 * know that there cannot be a loose 942 * reference named "refs/foo/bar/baz". 943 */ 944 } 945 } else if (errno == EISDIR) { 946 /* 947 * There is a directory in the way. It might have 948 * contained references that have been deleted. If 949 * we don't require that the reference already 950 * exists, try to remove the directory so that it 951 * doesn't cause trouble when we want to rename the 952 * lockfile into place later. 953 */ 954 if (mustexist) { 955 /* Garden variety missing reference. */ 956 strbuf_addf(err, "unable to resolve reference '%s'", 957 refname); 958 goto error_return; 959 } else if (remove_dir_recursively(&ref_file, 960 REMOVE_DIR_EMPTY_ONLY)) { 961 if (refs_verify_refname_available( 962 &refs->base, refname, 963 extras, skip, err)) { 964 /* 965 * The error message set by 966 * verify_refname_available() is OK. 967 */ 968 ret = TRANSACTION_NAME_CONFLICT; 969 goto error_return; 970 } else { 971 /* 972 * We can't delete the directory, 973 * but we also don't know of any 974 * references that it should 975 * contain. 976 */ 977 strbuf_addf(err, "there is a non-empty directory '%s' " 978 "blocking reference '%s'", 979 ref_file.buf, refname); 980 goto error_return; 981 } 982 } 983 } else if (errno == EINVAL && (*type & REF_ISBROKEN)) { 984 strbuf_addf(err, "unable to resolve reference '%s': " 985 "reference broken", refname); 986 goto error_return; 987 } else { 988 strbuf_addf(err, "unable to resolve reference '%s': %s", 989 refname, strerror(errno)); 990 goto error_return; 991 } 992 993 /* 994 * If the ref did not exist and we are creating it, 995 * make sure there is no existing ref that conflicts 996 * with refname: 997 */ 998 if (refs_verify_refname_available( 999 &refs->base, refname,1000 extras, skip, err))1001 goto error_return;1002 }10031004 ret = 0;1005 goto out;10061007error_return:1008 unlock_ref(lock);1009 *lock_p = NULL;10101011out:1012 strbuf_release(&ref_file);1013 return ret;1014}10151016static int files_peel_ref(struct ref_store *ref_store,1017 const char *refname, unsigned char *sha1)1018{1019 struct files_ref_store *refs =1020 files_downcast(ref_store, REF_STORE_READ | REF_STORE_ODB,1021 "peel_ref");1022 int flag;1023 unsigned char base[20];10241025 if (current_ref_iter && current_ref_iter->refname == refname) {1026 struct object_id peeled;10271028 if (ref_iterator_peel(current_ref_iter, &peeled))1029 return -1;1030 hashcpy(sha1, peeled.hash);1031 return 0;1032 }10331034 if (refs_read_ref_full(ref_store, refname,1035 RESOLVE_REF_READING, base, &flag))1036 return -1;10371038 /*1039 * If the reference is packed, read its ref_entry from the1040 * cache in the hope that we already know its peeled value.1041 * We only try this optimization on packed references because1042 * (a) forcing the filling of the loose reference cache could1043 * be expensive and (b) loose references anyway usually do not1044 * have REF_KNOWS_PEELED.1045 */1046 if (flag & REF_ISPACKED) {1047 struct ref_entry *r = get_packed_ref(refs, refname);1048 if (r) {1049 if (peel_entry(r, 0))1050 return -1;1051 hashcpy(sha1, r->u.value.peeled.hash);1052 return 0;1053 }1054 }10551056 return peel_object(base, sha1);1057}10581059struct files_ref_iterator {1060 struct ref_iterator base;10611062 struct packed_ref_cache *packed_ref_cache;1063 struct ref_iterator *iter0;1064 unsigned int flags;1065};10661067static int files_ref_iterator_advance(struct ref_iterator *ref_iterator)1068{1069 struct files_ref_iterator *iter =1070 (struct files_ref_iterator *)ref_iterator;1071 int ok;10721073 while ((ok = ref_iterator_advance(iter->iter0)) == ITER_OK) {1074 if (iter->flags & DO_FOR_EACH_PER_WORKTREE_ONLY &&1075 ref_type(iter->iter0->refname) != REF_TYPE_PER_WORKTREE)1076 continue;10771078 if (!(iter->flags & DO_FOR_EACH_INCLUDE_BROKEN) &&1079 !ref_resolves_to_object(iter->iter0->refname,1080 iter->iter0->oid,1081 iter->iter0->flags))1082 continue;10831084 iter->base.refname = iter->iter0->refname;1085 iter->base.oid = iter->iter0->oid;1086 iter->base.flags = iter->iter0->flags;1087 return ITER_OK;1088 }10891090 iter->iter0 = NULL;1091 if (ref_iterator_abort(ref_iterator) != ITER_DONE)1092 ok = ITER_ERROR;10931094 return ok;1095}10961097static int files_ref_iterator_peel(struct ref_iterator *ref_iterator,1098 struct object_id *peeled)1099{1100 struct files_ref_iterator *iter =1101 (struct files_ref_iterator *)ref_iterator;11021103 return ref_iterator_peel(iter->iter0, peeled);1104}11051106static int files_ref_iterator_abort(struct ref_iterator *ref_iterator)1107{1108 struct files_ref_iterator *iter =1109 (struct files_ref_iterator *)ref_iterator;1110 int ok = ITER_DONE;11111112 if (iter->iter0)1113 ok = ref_iterator_abort(iter->iter0);11141115 release_packed_ref_cache(iter->packed_ref_cache);1116 base_ref_iterator_free(ref_iterator);1117 return ok;1118}11191120static struct ref_iterator_vtable files_ref_iterator_vtable = {1121 files_ref_iterator_advance,1122 files_ref_iterator_peel,1123 files_ref_iterator_abort1124};11251126static struct ref_iterator *files_ref_iterator_begin(1127 struct ref_store *ref_store,1128 const char *prefix, unsigned int flags)1129{1130 struct files_ref_store *refs;1131 struct ref_iterator *loose_iter, *packed_iter;1132 struct files_ref_iterator *iter;1133 struct ref_iterator *ref_iterator;1134 unsigned int required_flags = REF_STORE_READ;11351136 if (!(flags & DO_FOR_EACH_INCLUDE_BROKEN))1137 required_flags |= REF_STORE_ODB;11381139 refs = files_downcast(ref_store, required_flags, "ref_iterator_begin");11401141 iter = xcalloc(1, sizeof(*iter));1142 ref_iterator = &iter->base;1143 base_ref_iterator_init(ref_iterator, &files_ref_iterator_vtable);11441145 /*1146 * We must make sure that all loose refs are read before1147 * accessing the packed-refs file; this avoids a race1148 * condition if loose refs are migrated to the packed-refs1149 * file by a simultaneous process, but our in-memory view is1150 * from before the migration. We ensure this as follows:1151 * First, we call start the loose refs iteration with its1152 * `prime_ref` argument set to true. This causes the loose1153 * references in the subtree to be pre-read into the cache.1154 * (If they've already been read, that's OK; we only need to1155 * guarantee that they're read before the packed refs, not1156 * *how much* before.) After that, we call1157 * get_packed_ref_cache(), which internally checks whether the1158 * packed-ref cache is up to date with what is on disk, and1159 * re-reads it if not.1160 */11611162 loose_iter = cache_ref_iterator_begin(get_loose_ref_cache(refs),1163 prefix, 1);11641165 iter->packed_ref_cache = get_packed_ref_cache(refs->packed_ref_store);1166 acquire_packed_ref_cache(iter->packed_ref_cache);1167 packed_iter = cache_ref_iterator_begin(iter->packed_ref_cache->cache,1168 prefix, 0);11691170 iter->iter0 = overlay_ref_iterator_begin(loose_iter, packed_iter);1171 iter->flags = flags;11721173 return ref_iterator;1174}11751176/*1177 * Verify that the reference locked by lock has the value old_sha1.1178 * Fail if the reference doesn't exist and mustexist is set. Return 01179 * on success. On error, write an error message to err, set errno, and1180 * return a negative value.1181 */1182static int verify_lock(struct ref_store *ref_store, struct ref_lock *lock,1183 const unsigned char *old_sha1, int mustexist,1184 struct strbuf *err)1185{1186 assert(err);11871188 if (refs_read_ref_full(ref_store, lock->ref_name,1189 mustexist ? RESOLVE_REF_READING : 0,1190 lock->old_oid.hash, NULL)) {1191 if (old_sha1) {1192 int save_errno = errno;1193 strbuf_addf(err, "can't verify ref '%s'", lock->ref_name);1194 errno = save_errno;1195 return -1;1196 } else {1197 oidclr(&lock->old_oid);1198 return 0;1199 }1200 }1201 if (old_sha1 && hashcmp(lock->old_oid.hash, old_sha1)) {1202 strbuf_addf(err, "ref '%s' is at %s but expected %s",1203 lock->ref_name,1204 oid_to_hex(&lock->old_oid),1205 sha1_to_hex(old_sha1));1206 errno = EBUSY;1207 return -1;1208 }1209 return 0;1210}12111212static int remove_empty_directories(struct strbuf *path)1213{1214 /*1215 * we want to create a file but there is a directory there;1216 * if that is an empty directory (or a directory that contains1217 * only empty directories), remove them.1218 */1219 return remove_dir_recursively(path, REMOVE_DIR_EMPTY_ONLY);1220}12211222static int create_reflock(const char *path, void *cb)1223{1224 struct lock_file *lk = cb;12251226 return hold_lock_file_for_update(lk, path, LOCK_NO_DEREF) < 0 ? -1 : 0;1227}12281229/*1230 * Locks a ref returning the lock on success and NULL on failure.1231 * On failure errno is set to something meaningful.1232 */1233static struct ref_lock *lock_ref_sha1_basic(struct files_ref_store *refs,1234 const char *refname,1235 const unsigned char *old_sha1,1236 const struct string_list *extras,1237 const struct string_list *skip,1238 unsigned int flags, int *type,1239 struct strbuf *err)1240{1241 struct strbuf ref_file = STRBUF_INIT;1242 struct ref_lock *lock;1243 int last_errno = 0;1244 int mustexist = (old_sha1 && !is_null_sha1(old_sha1));1245 int resolve_flags = RESOLVE_REF_NO_RECURSE;1246 int resolved;12471248 files_assert_main_repository(refs, "lock_ref_sha1_basic");1249 assert(err);12501251 lock = xcalloc(1, sizeof(struct ref_lock));12521253 if (mustexist)1254 resolve_flags |= RESOLVE_REF_READING;1255 if (flags & REF_DELETING)1256 resolve_flags |= RESOLVE_REF_ALLOW_BAD_NAME;12571258 files_ref_path(refs, &ref_file, refname);1259 resolved = !!refs_resolve_ref_unsafe(&refs->base,1260 refname, resolve_flags,1261 lock->old_oid.hash, type);1262 if (!resolved && errno == EISDIR) {1263 /*1264 * we are trying to lock foo but we used to1265 * have foo/bar which now does not exist;1266 * it is normal for the empty directory 'foo'1267 * to remain.1268 */1269 if (remove_empty_directories(&ref_file)) {1270 last_errno = errno;1271 if (!refs_verify_refname_available(1272 &refs->base,1273 refname, extras, skip, err))1274 strbuf_addf(err, "there are still refs under '%s'",1275 refname);1276 goto error_return;1277 }1278 resolved = !!refs_resolve_ref_unsafe(&refs->base,1279 refname, resolve_flags,1280 lock->old_oid.hash, type);1281 }1282 if (!resolved) {1283 last_errno = errno;1284 if (last_errno != ENOTDIR ||1285 !refs_verify_refname_available(&refs->base, refname,1286 extras, skip, err))1287 strbuf_addf(err, "unable to resolve reference '%s': %s",1288 refname, strerror(last_errno));12891290 goto error_return;1291 }12921293 /*1294 * If the ref did not exist and we are creating it, make sure1295 * there is no existing packed ref whose name begins with our1296 * refname, nor a packed ref whose name is a proper prefix of1297 * our refname.1298 */1299 if (is_null_oid(&lock->old_oid) &&1300 refs_verify_refname_available(&refs->base, refname,1301 extras, skip, err)) {1302 last_errno = ENOTDIR;1303 goto error_return;1304 }13051306 lock->lk = xcalloc(1, sizeof(struct lock_file));13071308 lock->ref_name = xstrdup(refname);13091310 if (raceproof_create_file(ref_file.buf, create_reflock, lock->lk)) {1311 last_errno = errno;1312 unable_to_lock_message(ref_file.buf, errno, err);1313 goto error_return;1314 }13151316 if (verify_lock(&refs->base, lock, old_sha1, mustexist, err)) {1317 last_errno = errno;1318 goto error_return;1319 }1320 goto out;13211322 error_return:1323 unlock_ref(lock);1324 lock = NULL;13251326 out:1327 strbuf_release(&ref_file);1328 errno = last_errno;1329 return lock;1330}13311332/*1333 * Write an entry to the packed-refs file for the specified refname.1334 * If peeled is non-NULL, write it as the entry's peeled value.1335 */1336static void write_packed_entry(FILE *fh, const char *refname,1337 const unsigned char *sha1,1338 const unsigned char *peeled)1339{1340 fprintf_or_die(fh, "%s %s\n", sha1_to_hex(sha1), refname);1341 if (peeled)1342 fprintf_or_die(fh, "^%s\n", sha1_to_hex(peeled));1343}13441345/*1346 * Lock the packed-refs file for writing. Flags is passed to1347 * hold_lock_file_for_update(). Return 0 on success. On errors, set1348 * errno appropriately and return a nonzero value.1349 */1350static int lock_packed_refs(struct packed_ref_store *refs, int flags)1351{1352 static int timeout_configured = 0;1353 static int timeout_value = 1000;1354 struct packed_ref_cache *packed_ref_cache;13551356 packed_assert_main_repository(refs, "lock_packed_refs");13571358 if (!timeout_configured) {1359 git_config_get_int("core.packedrefstimeout", &timeout_value);1360 timeout_configured = 1;1361 }13621363 if (hold_lock_file_for_update_timeout(1364 &refs->lock,1365 refs->path,1366 flags, timeout_value) < 0)1367 return -1;13681369 /*1370 * Now that we hold the `packed-refs` lock, make sure that our1371 * cache matches the current version of the file. Normally1372 * `get_packed_ref_cache()` does that for us, but that1373 * function assumes that when the file is locked, any existing1374 * cache is still valid. We've just locked the file, but it1375 * might have changed the moment *before* we locked it.1376 */1377 validate_packed_ref_cache(refs);13781379 packed_ref_cache = get_packed_ref_cache(refs);1380 /* Increment the reference count to prevent it from being freed: */1381 acquire_packed_ref_cache(packed_ref_cache);1382 return 0;1383}13841385/*1386 * Write the current version of the packed refs cache from memory to1387 * disk. The packed-refs file must already be locked for writing (see1388 * lock_packed_refs()). Return zero on success. On errors, set errno1389 * and return a nonzero value1390 */1391static int commit_packed_refs(struct packed_ref_store *refs)1392{1393 struct packed_ref_cache *packed_ref_cache =1394 get_packed_ref_cache(refs);1395 int ok, error = 0;1396 int save_errno = 0;1397 FILE *out;1398 struct ref_iterator *iter;13991400 packed_assert_main_repository(refs, "commit_packed_refs");14011402 if (!is_lock_file_locked(&refs->lock))1403 die("BUG: packed-refs not locked");14041405 out = fdopen_lock_file(&refs->lock, "w");1406 if (!out)1407 die_errno("unable to fdopen packed-refs descriptor");14081409 fprintf_or_die(out, "%s", PACKED_REFS_HEADER);14101411 iter = cache_ref_iterator_begin(packed_ref_cache->cache, NULL, 0);1412 while ((ok = ref_iterator_advance(iter)) == ITER_OK) {1413 struct object_id peeled;1414 int peel_error = ref_iterator_peel(iter, &peeled);14151416 write_packed_entry(out, iter->refname, iter->oid->hash,1417 peel_error ? NULL : peeled.hash);1418 }14191420 if (ok != ITER_DONE)1421 die("error while iterating over references");14221423 if (commit_lock_file(&refs->lock)) {1424 save_errno = errno;1425 error = -1;1426 }1427 release_packed_ref_cache(packed_ref_cache);1428 errno = save_errno;1429 return error;1430}14311432/*1433 * Rollback the lockfile for the packed-refs file, and discard the1434 * in-memory packed reference cache. (The packed-refs file will be1435 * read anew if it is needed again after this function is called.)1436 */1437static void rollback_packed_refs(struct packed_ref_store *refs)1438{1439 struct packed_ref_cache *packed_ref_cache = get_packed_ref_cache(refs);14401441 packed_assert_main_repository(refs, "rollback_packed_refs");14421443 if (!is_lock_file_locked(&refs->lock))1444 die("BUG: packed-refs not locked");1445 rollback_lock_file(&refs->lock);1446 release_packed_ref_cache(packed_ref_cache);1447 clear_packed_ref_cache(refs);1448}14491450struct ref_to_prune {1451 struct ref_to_prune *next;1452 unsigned char sha1[20];1453 char name[FLEX_ARRAY];1454};14551456enum {1457 REMOVE_EMPTY_PARENTS_REF = 0x01,1458 REMOVE_EMPTY_PARENTS_REFLOG = 0x021459};14601461/*1462 * Remove empty parent directories associated with the specified1463 * reference and/or its reflog, but spare [logs/]refs/ and immediate1464 * subdirs. flags is a combination of REMOVE_EMPTY_PARENTS_REF and/or1465 * REMOVE_EMPTY_PARENTS_REFLOG.1466 */1467static void try_remove_empty_parents(struct files_ref_store *refs,1468 const char *refname,1469 unsigned int flags)1470{1471 struct strbuf buf = STRBUF_INIT;1472 struct strbuf sb = STRBUF_INIT;1473 char *p, *q;1474 int i;14751476 strbuf_addstr(&buf, refname);1477 p = buf.buf;1478 for (i = 0; i < 2; i++) { /* refs/{heads,tags,...}/ */1479 while (*p && *p != '/')1480 p++;1481 /* tolerate duplicate slashes; see check_refname_format() */1482 while (*p == '/')1483 p++;1484 }1485 q = buf.buf + buf.len;1486 while (flags & (REMOVE_EMPTY_PARENTS_REF | REMOVE_EMPTY_PARENTS_REFLOG)) {1487 while (q > p && *q != '/')1488 q--;1489 while (q > p && *(q-1) == '/')1490 q--;1491 if (q == p)1492 break;1493 strbuf_setlen(&buf, q - buf.buf);14941495 strbuf_reset(&sb);1496 files_ref_path(refs, &sb, buf.buf);1497 if ((flags & REMOVE_EMPTY_PARENTS_REF) && rmdir(sb.buf))1498 flags &= ~REMOVE_EMPTY_PARENTS_REF;14991500 strbuf_reset(&sb);1501 files_reflog_path(refs, &sb, buf.buf);1502 if ((flags & REMOVE_EMPTY_PARENTS_REFLOG) && rmdir(sb.buf))1503 flags &= ~REMOVE_EMPTY_PARENTS_REFLOG;1504 }1505 strbuf_release(&buf);1506 strbuf_release(&sb);1507}15081509/* make sure nobody touched the ref, and unlink */1510static void prune_ref(struct files_ref_store *refs, struct ref_to_prune *r)1511{1512 struct ref_transaction *transaction;1513 struct strbuf err = STRBUF_INIT;15141515 if (check_refname_format(r->name, 0))1516 return;15171518 transaction = ref_store_transaction_begin(&refs->base, &err);1519 if (!transaction ||1520 ref_transaction_delete(transaction, r->name, r->sha1,1521 REF_ISPRUNING | REF_NODEREF, NULL, &err) ||1522 ref_transaction_commit(transaction, &err)) {1523 ref_transaction_free(transaction);1524 error("%s", err.buf);1525 strbuf_release(&err);1526 return;1527 }1528 ref_transaction_free(transaction);1529 strbuf_release(&err);1530}15311532static void prune_refs(struct files_ref_store *refs, struct ref_to_prune *r)1533{1534 while (r) {1535 prune_ref(refs, r);1536 r = r->next;1537 }1538}15391540/*1541 * Return true if the specified reference should be packed.1542 */1543static int should_pack_ref(const char *refname,1544 const struct object_id *oid, unsigned int ref_flags,1545 unsigned int pack_flags)1546{1547 /* Do not pack per-worktree refs: */1548 if (ref_type(refname) != REF_TYPE_NORMAL)1549 return 0;15501551 /* Do not pack non-tags unless PACK_REFS_ALL is set: */1552 if (!(pack_flags & PACK_REFS_ALL) && !starts_with(refname, "refs/tags/"))1553 return 0;15541555 /* Do not pack symbolic refs: */1556 if (ref_flags & REF_ISSYMREF)1557 return 0;15581559 /* Do not pack broken refs: */1560 if (!ref_resolves_to_object(refname, oid, ref_flags))1561 return 0;15621563 return 1;1564}15651566static int files_pack_refs(struct ref_store *ref_store, unsigned int flags)1567{1568 struct files_ref_store *refs =1569 files_downcast(ref_store, REF_STORE_WRITE | REF_STORE_ODB,1570 "pack_refs");1571 struct ref_iterator *iter;1572 int ok;1573 struct ref_to_prune *refs_to_prune = NULL;15741575 lock_packed_refs(refs->packed_ref_store, LOCK_DIE_ON_ERROR);15761577 iter = cache_ref_iterator_begin(get_loose_ref_cache(refs), NULL, 0);1578 while ((ok = ref_iterator_advance(iter)) == ITER_OK) {1579 /*1580 * If the loose reference can be packed, add an entry1581 * in the packed ref cache. If the reference should be1582 * pruned, also add it to refs_to_prune.1583 */1584 if (!should_pack_ref(iter->refname, iter->oid, iter->flags,1585 flags))1586 continue;15871588 /*1589 * Create an entry in the packed-refs cache equivalent1590 * to the one from the loose ref cache, except that1591 * we don't copy the peeled status, because we want it1592 * to be re-peeled.1593 */1594 add_packed_ref(refs->packed_ref_store, iter->refname, iter->oid);15951596 /* Schedule the loose reference for pruning if requested. */1597 if ((flags & PACK_REFS_PRUNE)) {1598 struct ref_to_prune *n;1599 FLEX_ALLOC_STR(n, name, iter->refname);1600 hashcpy(n->sha1, iter->oid->hash);1601 n->next = refs_to_prune;1602 refs_to_prune = n;1603 }1604 }1605 if (ok != ITER_DONE)1606 die("error while iterating over references");16071608 if (commit_packed_refs(refs->packed_ref_store))1609 die_errno("unable to overwrite old ref-pack file");16101611 prune_refs(refs, refs_to_prune);1612 return 0;1613}16141615/*1616 * Rewrite the packed-refs file, omitting any refs listed in1617 * 'refnames'. On error, leave packed-refs unchanged, write an error1618 * message to 'err', and return a nonzero value.1619 *1620 * The refs in 'refnames' needn't be sorted. `err` must not be NULL.1621 */1622static int repack_without_refs(struct files_ref_store *refs,1623 struct string_list *refnames, struct strbuf *err)1624{1625 struct ref_dir *packed;1626 struct string_list_item *refname;1627 int ret, needs_repacking = 0, removed = 0;16281629 files_assert_main_repository(refs, "repack_without_refs");1630 assert(err);16311632 /* Look for a packed ref */1633 for_each_string_list_item(refname, refnames) {1634 if (get_packed_ref(refs, refname->string)) {1635 needs_repacking = 1;1636 break;1637 }1638 }16391640 /* Avoid locking if we have nothing to do */1641 if (!needs_repacking)1642 return 0; /* no refname exists in packed refs */16431644 if (lock_packed_refs(refs->packed_ref_store, 0)) {1645 unable_to_lock_message(refs->packed_ref_store->path, errno, err);1646 return -1;1647 }1648 packed = get_packed_refs(refs->packed_ref_store);16491650 /* Remove refnames from the cache */1651 for_each_string_list_item(refname, refnames)1652 if (remove_entry_from_dir(packed, refname->string) != -1)1653 removed = 1;1654 if (!removed) {1655 /*1656 * All packed entries disappeared while we were1657 * acquiring the lock.1658 */1659 rollback_packed_refs(refs->packed_ref_store);1660 return 0;1661 }16621663 /* Write what remains */1664 ret = commit_packed_refs(refs->packed_ref_store);1665 if (ret)1666 strbuf_addf(err, "unable to overwrite old ref-pack file: %s",1667 strerror(errno));1668 return ret;1669}16701671static int files_delete_refs(struct ref_store *ref_store, const char *msg,1672 struct string_list *refnames, unsigned int flags)1673{1674 struct files_ref_store *refs =1675 files_downcast(ref_store, REF_STORE_WRITE, "delete_refs");1676 struct strbuf err = STRBUF_INIT;1677 int i, result = 0;16781679 if (!refnames->nr)1680 return 0;16811682 result = repack_without_refs(refs, refnames, &err);1683 if (result) {1684 /*1685 * If we failed to rewrite the packed-refs file, then1686 * it is unsafe to try to remove loose refs, because1687 * doing so might expose an obsolete packed value for1688 * a reference that might even point at an object that1689 * has been garbage collected.1690 */1691 if (refnames->nr == 1)1692 error(_("could not delete reference %s: %s"),1693 refnames->items[0].string, err.buf);1694 else1695 error(_("could not delete references: %s"), err.buf);16961697 goto out;1698 }16991700 for (i = 0; i < refnames->nr; i++) {1701 const char *refname = refnames->items[i].string;17021703 if (refs_delete_ref(&refs->base, msg, refname, NULL, flags))1704 result |= error(_("could not remove reference %s"), refname);1705 }17061707out:1708 strbuf_release(&err);1709 return result;1710}17111712/*1713 * People using contrib's git-new-workdir have .git/logs/refs ->1714 * /some/other/path/.git/logs/refs, and that may live on another device.1715 *1716 * IOW, to avoid cross device rename errors, the temporary renamed log must1717 * live into logs/refs.1718 */1719#define TMP_RENAMED_LOG "refs/.tmp-renamed-log"17201721struct rename_cb {1722 const char *tmp_renamed_log;1723 int true_errno;1724};17251726static int rename_tmp_log_callback(const char *path, void *cb_data)1727{1728 struct rename_cb *cb = cb_data;17291730 if (rename(cb->tmp_renamed_log, path)) {1731 /*1732 * rename(a, b) when b is an existing directory ought1733 * to result in ISDIR, but Solaris 5.8 gives ENOTDIR.1734 * Sheesh. Record the true errno for error reporting,1735 * but report EISDIR to raceproof_create_file() so1736 * that it knows to retry.1737 */1738 cb->true_errno = errno;1739 if (errno == ENOTDIR)1740 errno = EISDIR;1741 return -1;1742 } else {1743 return 0;1744 }1745}17461747static int rename_tmp_log(struct files_ref_store *refs, const char *newrefname)1748{1749 struct strbuf path = STRBUF_INIT;1750 struct strbuf tmp = STRBUF_INIT;1751 struct rename_cb cb;1752 int ret;17531754 files_reflog_path(refs, &path, newrefname);1755 files_reflog_path(refs, &tmp, TMP_RENAMED_LOG);1756 cb.tmp_renamed_log = tmp.buf;1757 ret = raceproof_create_file(path.buf, rename_tmp_log_callback, &cb);1758 if (ret) {1759 if (errno == EISDIR)1760 error("directory not empty: %s", path.buf);1761 else1762 error("unable to move logfile %s to %s: %s",1763 tmp.buf, path.buf,1764 strerror(cb.true_errno));1765 }17661767 strbuf_release(&path);1768 strbuf_release(&tmp);1769 return ret;1770}17711772static int write_ref_to_lockfile(struct ref_lock *lock,1773 const struct object_id *oid, struct strbuf *err);1774static int commit_ref_update(struct files_ref_store *refs,1775 struct ref_lock *lock,1776 const struct object_id *oid, const char *logmsg,1777 struct strbuf *err);17781779static int files_rename_ref(struct ref_store *ref_store,1780 const char *oldrefname, const char *newrefname,1781 const char *logmsg)1782{1783 struct files_ref_store *refs =1784 files_downcast(ref_store, REF_STORE_WRITE, "rename_ref");1785 struct object_id oid, orig_oid;1786 int flag = 0, logmoved = 0;1787 struct ref_lock *lock;1788 struct stat loginfo;1789 struct strbuf sb_oldref = STRBUF_INIT;1790 struct strbuf sb_newref = STRBUF_INIT;1791 struct strbuf tmp_renamed_log = STRBUF_INIT;1792 int log, ret;1793 struct strbuf err = STRBUF_INIT;17941795 files_reflog_path(refs, &sb_oldref, oldrefname);1796 files_reflog_path(refs, &sb_newref, newrefname);1797 files_reflog_path(refs, &tmp_renamed_log, TMP_RENAMED_LOG);17981799 log = !lstat(sb_oldref.buf, &loginfo);1800 if (log && S_ISLNK(loginfo.st_mode)) {1801 ret = error("reflog for %s is a symlink", oldrefname);1802 goto out;1803 }18041805 if (!refs_resolve_ref_unsafe(&refs->base, oldrefname,1806 RESOLVE_REF_READING | RESOLVE_REF_NO_RECURSE,1807 orig_oid.hash, &flag)) {1808 ret = error("refname %s not found", oldrefname);1809 goto out;1810 }18111812 if (flag & REF_ISSYMREF) {1813 ret = error("refname %s is a symbolic ref, renaming it is not supported",1814 oldrefname);1815 goto out;1816 }1817 if (!refs_rename_ref_available(&refs->base, oldrefname, newrefname)) {1818 ret = 1;1819 goto out;1820 }18211822 if (log && rename(sb_oldref.buf, tmp_renamed_log.buf)) {1823 ret = error("unable to move logfile logs/%s to logs/"TMP_RENAMED_LOG": %s",1824 oldrefname, strerror(errno));1825 goto out;1826 }18271828 if (refs_delete_ref(&refs->base, logmsg, oldrefname,1829 orig_oid.hash, REF_NODEREF)) {1830 error("unable to delete old %s", oldrefname);1831 goto rollback;1832 }18331834 /*1835 * Since we are doing a shallow lookup, oid is not the1836 * correct value to pass to delete_ref as old_oid. But that1837 * doesn't matter, because an old_oid check wouldn't add to1838 * the safety anyway; we want to delete the reference whatever1839 * its current value.1840 */1841 if (!refs_read_ref_full(&refs->base, newrefname,1842 RESOLVE_REF_READING | RESOLVE_REF_NO_RECURSE,1843 oid.hash, NULL) &&1844 refs_delete_ref(&refs->base, NULL, newrefname,1845 NULL, REF_NODEREF)) {1846 if (errno == EISDIR) {1847 struct strbuf path = STRBUF_INIT;1848 int result;18491850 files_ref_path(refs, &path, newrefname);1851 result = remove_empty_directories(&path);1852 strbuf_release(&path);18531854 if (result) {1855 error("Directory not empty: %s", newrefname);1856 goto rollback;1857 }1858 } else {1859 error("unable to delete existing %s", newrefname);1860 goto rollback;1861 }1862 }18631864 if (log && rename_tmp_log(refs, newrefname))1865 goto rollback;18661867 logmoved = log;18681869 lock = lock_ref_sha1_basic(refs, newrefname, NULL, NULL, NULL,1870 REF_NODEREF, NULL, &err);1871 if (!lock) {1872 error("unable to rename '%s' to '%s': %s", oldrefname, newrefname, err.buf);1873 strbuf_release(&err);1874 goto rollback;1875 }1876 oidcpy(&lock->old_oid, &orig_oid);18771878 if (write_ref_to_lockfile(lock, &orig_oid, &err) ||1879 commit_ref_update(refs, lock, &orig_oid, logmsg, &err)) {1880 error("unable to write current sha1 into %s: %s", newrefname, err.buf);1881 strbuf_release(&err);1882 goto rollback;1883 }18841885 ret = 0;1886 goto out;18871888 rollback:1889 lock = lock_ref_sha1_basic(refs, oldrefname, NULL, NULL, NULL,1890 REF_NODEREF, NULL, &err);1891 if (!lock) {1892 error("unable to lock %s for rollback: %s", oldrefname, err.buf);1893 strbuf_release(&err);1894 goto rollbacklog;1895 }18961897 flag = log_all_ref_updates;1898 log_all_ref_updates = LOG_REFS_NONE;1899 if (write_ref_to_lockfile(lock, &orig_oid, &err) ||1900 commit_ref_update(refs, lock, &orig_oid, NULL, &err)) {1901 error("unable to write current sha1 into %s: %s", oldrefname, err.buf);1902 strbuf_release(&err);1903 }1904 log_all_ref_updates = flag;19051906 rollbacklog:1907 if (logmoved && rename(sb_newref.buf, sb_oldref.buf))1908 error("unable to restore logfile %s from %s: %s",1909 oldrefname, newrefname, strerror(errno));1910 if (!logmoved && log &&1911 rename(tmp_renamed_log.buf, sb_oldref.buf))1912 error("unable to restore logfile %s from logs/"TMP_RENAMED_LOG": %s",1913 oldrefname, strerror(errno));1914 ret = 1;1915 out:1916 strbuf_release(&sb_newref);1917 strbuf_release(&sb_oldref);1918 strbuf_release(&tmp_renamed_log);19191920 return ret;1921}19221923static int close_ref(struct ref_lock *lock)1924{1925 if (close_lock_file(lock->lk))1926 return -1;1927 return 0;1928}19291930static int commit_ref(struct ref_lock *lock)1931{1932 char *path = get_locked_file_path(lock->lk);1933 struct stat st;19341935 if (!lstat(path, &st) && S_ISDIR(st.st_mode)) {1936 /*1937 * There is a directory at the path we want to rename1938 * the lockfile to. Hopefully it is empty; try to1939 * delete it.1940 */1941 size_t len = strlen(path);1942 struct strbuf sb_path = STRBUF_INIT;19431944 strbuf_attach(&sb_path, path, len, len);19451946 /*1947 * If this fails, commit_lock_file() will also fail1948 * and will report the problem.1949 */1950 remove_empty_directories(&sb_path);1951 strbuf_release(&sb_path);1952 } else {1953 free(path);1954 }19551956 if (commit_lock_file(lock->lk))1957 return -1;1958 return 0;1959}19601961static int open_or_create_logfile(const char *path, void *cb)1962{1963 int *fd = cb;19641965 *fd = open(path, O_APPEND | O_WRONLY | O_CREAT, 0666);1966 return (*fd < 0) ? -1 : 0;1967}19681969/*1970 * Create a reflog for a ref. If force_create = 0, only create the1971 * reflog for certain refs (those for which should_autocreate_reflog1972 * returns non-zero). Otherwise, create it regardless of the reference1973 * name. If the logfile already existed or was created, return 0 and1974 * set *logfd to the file descriptor opened for appending to the file.1975 * If no logfile exists and we decided not to create one, return 0 and1976 * set *logfd to -1. On failure, fill in *err, set *logfd to -1, and1977 * return -1.1978 */1979static int log_ref_setup(struct files_ref_store *refs,1980 const char *refname, int force_create,1981 int *logfd, struct strbuf *err)1982{1983 struct strbuf logfile_sb = STRBUF_INIT;1984 char *logfile;19851986 files_reflog_path(refs, &logfile_sb, refname);1987 logfile = strbuf_detach(&logfile_sb, NULL);19881989 if (force_create || should_autocreate_reflog(refname)) {1990 if (raceproof_create_file(logfile, open_or_create_logfile, logfd)) {1991 if (errno == ENOENT)1992 strbuf_addf(err, "unable to create directory for '%s': "1993 "%s", logfile, strerror(errno));1994 else if (errno == EISDIR)1995 strbuf_addf(err, "there are still logs under '%s'",1996 logfile);1997 else1998 strbuf_addf(err, "unable to append to '%s': %s",1999 logfile, strerror(errno));20002001 goto error;2002 }2003 } else {2004 *logfd = open(logfile, O_APPEND | O_WRONLY, 0666);2005 if (*logfd < 0) {2006 if (errno == ENOENT || errno == EISDIR) {2007 /*2008 * The logfile doesn't already exist,2009 * but that is not an error; it only2010 * means that we won't write log2011 * entries to it.2012 */2013 ;2014 } else {2015 strbuf_addf(err, "unable to append to '%s': %s",2016 logfile, strerror(errno));2017 goto error;2018 }2019 }2020 }20212022 if (*logfd >= 0)2023 adjust_shared_perm(logfile);20242025 free(logfile);2026 return 0;20272028error:2029 free(logfile);2030 return -1;2031}20322033static int files_create_reflog(struct ref_store *ref_store,2034 const char *refname, int force_create,2035 struct strbuf *err)2036{2037 struct files_ref_store *refs =2038 files_downcast(ref_store, REF_STORE_WRITE, "create_reflog");2039 int fd;20402041 if (log_ref_setup(refs, refname, force_create, &fd, err))2042 return -1;20432044 if (fd >= 0)2045 close(fd);20462047 return 0;2048}20492050static int log_ref_write_fd(int fd, const struct object_id *old_oid,2051 const struct object_id *new_oid,2052 const char *committer, const char *msg)2053{2054 int msglen, written;2055 unsigned maxlen, len;2056 char *logrec;20572058 msglen = msg ? strlen(msg) : 0;2059 maxlen = strlen(committer) + msglen + 100;2060 logrec = xmalloc(maxlen);2061 len = xsnprintf(logrec, maxlen, "%s %s %s\n",2062 oid_to_hex(old_oid),2063 oid_to_hex(new_oid),2064 committer);2065 if (msglen)2066 len += copy_reflog_msg(logrec + len - 1, msg) - 1;20672068 written = len <= maxlen ? write_in_full(fd, logrec, len) : -1;2069 free(logrec);2070 if (written != len)2071 return -1;20722073 return 0;2074}20752076static int files_log_ref_write(struct files_ref_store *refs,2077 const char *refname, const struct object_id *old_oid,2078 const struct object_id *new_oid, const char *msg,2079 int flags, struct strbuf *err)2080{2081 int logfd, result;20822083 if (log_all_ref_updates == LOG_REFS_UNSET)2084 log_all_ref_updates = is_bare_repository() ? LOG_REFS_NONE : LOG_REFS_NORMAL;20852086 result = log_ref_setup(refs, refname,2087 flags & REF_FORCE_CREATE_REFLOG,2088 &logfd, err);20892090 if (result)2091 return result;20922093 if (logfd < 0)2094 return 0;2095 result = log_ref_write_fd(logfd, old_oid, new_oid,2096 git_committer_info(0), msg);2097 if (result) {2098 struct strbuf sb = STRBUF_INIT;2099 int save_errno = errno;21002101 files_reflog_path(refs, &sb, refname);2102 strbuf_addf(err, "unable to append to '%s': %s",2103 sb.buf, strerror(save_errno));2104 strbuf_release(&sb);2105 close(logfd);2106 return -1;2107 }2108 if (close(logfd)) {2109 struct strbuf sb = STRBUF_INIT;2110 int save_errno = errno;21112112 files_reflog_path(refs, &sb, refname);2113 strbuf_addf(err, "unable to append to '%s': %s",2114 sb.buf, strerror(save_errno));2115 strbuf_release(&sb);2116 return -1;2117 }2118 return 0;2119}21202121/*2122 * Write sha1 into the open lockfile, then close the lockfile. On2123 * errors, rollback the lockfile, fill in *err and2124 * return -1.2125 */2126static int write_ref_to_lockfile(struct ref_lock *lock,2127 const struct object_id *oid, struct strbuf *err)2128{2129 static char term = '\n';2130 struct object *o;2131 int fd;21322133 o = parse_object(oid);2134 if (!o) {2135 strbuf_addf(err,2136 "trying to write ref '%s' with nonexistent object %s",2137 lock->ref_name, oid_to_hex(oid));2138 unlock_ref(lock);2139 return -1;2140 }2141 if (o->type != OBJ_COMMIT && is_branch(lock->ref_name)) {2142 strbuf_addf(err,2143 "trying to write non-commit object %s to branch '%s'",2144 oid_to_hex(oid), lock->ref_name);2145 unlock_ref(lock);2146 return -1;2147 }2148 fd = get_lock_file_fd(lock->lk);2149 if (write_in_full(fd, oid_to_hex(oid), GIT_SHA1_HEXSZ) != GIT_SHA1_HEXSZ ||2150 write_in_full(fd, &term, 1) != 1 ||2151 close_ref(lock) < 0) {2152 strbuf_addf(err,2153 "couldn't write '%s'", get_lock_file_path(lock->lk));2154 unlock_ref(lock);2155 return -1;2156 }2157 return 0;2158}21592160/*2161 * Commit a change to a loose reference that has already been written2162 * to the loose reference lockfile. Also update the reflogs if2163 * necessary, using the specified lockmsg (which can be NULL).2164 */2165static int commit_ref_update(struct files_ref_store *refs,2166 struct ref_lock *lock,2167 const struct object_id *oid, const char *logmsg,2168 struct strbuf *err)2169{2170 files_assert_main_repository(refs, "commit_ref_update");21712172 clear_loose_ref_cache(refs);2173 if (files_log_ref_write(refs, lock->ref_name,2174 &lock->old_oid, oid,2175 logmsg, 0, err)) {2176 char *old_msg = strbuf_detach(err, NULL);2177 strbuf_addf(err, "cannot update the ref '%s': %s",2178 lock->ref_name, old_msg);2179 free(old_msg);2180 unlock_ref(lock);2181 return -1;2182 }21832184 if (strcmp(lock->ref_name, "HEAD") != 0) {2185 /*2186 * Special hack: If a branch is updated directly and HEAD2187 * points to it (may happen on the remote side of a push2188 * for example) then logically the HEAD reflog should be2189 * updated too.2190 * A generic solution implies reverse symref information,2191 * but finding all symrefs pointing to the given branch2192 * would be rather costly for this rare event (the direct2193 * update of a branch) to be worth it. So let's cheat and2194 * check with HEAD only which should cover 99% of all usage2195 * scenarios (even 100% of the default ones).2196 */2197 struct object_id head_oid;2198 int head_flag;2199 const char *head_ref;22002201 head_ref = refs_resolve_ref_unsafe(&refs->base, "HEAD",2202 RESOLVE_REF_READING,2203 head_oid.hash, &head_flag);2204 if (head_ref && (head_flag & REF_ISSYMREF) &&2205 !strcmp(head_ref, lock->ref_name)) {2206 struct strbuf log_err = STRBUF_INIT;2207 if (files_log_ref_write(refs, "HEAD",2208 &lock->old_oid, oid,2209 logmsg, 0, &log_err)) {2210 error("%s", log_err.buf);2211 strbuf_release(&log_err);2212 }2213 }2214 }22152216 if (commit_ref(lock)) {2217 strbuf_addf(err, "couldn't set '%s'", lock->ref_name);2218 unlock_ref(lock);2219 return -1;2220 }22212222 unlock_ref(lock);2223 return 0;2224}22252226static int create_ref_symlink(struct ref_lock *lock, const char *target)2227{2228 int ret = -1;2229#ifndef NO_SYMLINK_HEAD2230 char *ref_path = get_locked_file_path(lock->lk);2231 unlink(ref_path);2232 ret = symlink(target, ref_path);2233 free(ref_path);22342235 if (ret)2236 fprintf(stderr, "no symlink - falling back to symbolic ref\n");2237#endif2238 return ret;2239}22402241static void update_symref_reflog(struct files_ref_store *refs,2242 struct ref_lock *lock, const char *refname,2243 const char *target, const char *logmsg)2244{2245 struct strbuf err = STRBUF_INIT;2246 struct object_id new_oid;2247 if (logmsg &&2248 !refs_read_ref_full(&refs->base, target,2249 RESOLVE_REF_READING, new_oid.hash, NULL) &&2250 files_log_ref_write(refs, refname, &lock->old_oid,2251 &new_oid, logmsg, 0, &err)) {2252 error("%s", err.buf);2253 strbuf_release(&err);2254 }2255}22562257static int create_symref_locked(struct files_ref_store *refs,2258 struct ref_lock *lock, const char *refname,2259 const char *target, const char *logmsg)2260{2261 if (prefer_symlink_refs && !create_ref_symlink(lock, target)) {2262 update_symref_reflog(refs, lock, refname, target, logmsg);2263 return 0;2264 }22652266 if (!fdopen_lock_file(lock->lk, "w"))2267 return error("unable to fdopen %s: %s",2268 lock->lk->tempfile.filename.buf, strerror(errno));22692270 update_symref_reflog(refs, lock, refname, target, logmsg);22712272 /* no error check; commit_ref will check ferror */2273 fprintf(lock->lk->tempfile.fp, "ref: %s\n", target);2274 if (commit_ref(lock) < 0)2275 return error("unable to write symref for %s: %s", refname,2276 strerror(errno));2277 return 0;2278}22792280static int files_create_symref(struct ref_store *ref_store,2281 const char *refname, const char *target,2282 const char *logmsg)2283{2284 struct files_ref_store *refs =2285 files_downcast(ref_store, REF_STORE_WRITE, "create_symref");2286 struct strbuf err = STRBUF_INIT;2287 struct ref_lock *lock;2288 int ret;22892290 lock = lock_ref_sha1_basic(refs, refname, NULL,2291 NULL, NULL, REF_NODEREF, NULL,2292 &err);2293 if (!lock) {2294 error("%s", err.buf);2295 strbuf_release(&err);2296 return -1;2297 }22982299 ret = create_symref_locked(refs, lock, refname, target, logmsg);2300 unlock_ref(lock);2301 return ret;2302}23032304static int files_reflog_exists(struct ref_store *ref_store,2305 const char *refname)2306{2307 struct files_ref_store *refs =2308 files_downcast(ref_store, REF_STORE_READ, "reflog_exists");2309 struct strbuf sb = STRBUF_INIT;2310 struct stat st;2311 int ret;23122313 files_reflog_path(refs, &sb, refname);2314 ret = !lstat(sb.buf, &st) && S_ISREG(st.st_mode);2315 strbuf_release(&sb);2316 return ret;2317}23182319static int files_delete_reflog(struct ref_store *ref_store,2320 const char *refname)2321{2322 struct files_ref_store *refs =2323 files_downcast(ref_store, REF_STORE_WRITE, "delete_reflog");2324 struct strbuf sb = STRBUF_INIT;2325 int ret;23262327 files_reflog_path(refs, &sb, refname);2328 ret = remove_path(sb.buf);2329 strbuf_release(&sb);2330 return ret;2331}23322333static int show_one_reflog_ent(struct strbuf *sb, each_reflog_ent_fn fn, void *cb_data)2334{2335 struct object_id ooid, noid;2336 char *email_end, *message;2337 timestamp_t timestamp;2338 int tz;2339 const char *p = sb->buf;23402341 /* old SP new SP name <email> SP time TAB msg LF */2342 if (!sb->len || sb->buf[sb->len - 1] != '\n' ||2343 parse_oid_hex(p, &ooid, &p) || *p++ != ' ' ||2344 parse_oid_hex(p, &noid, &p) || *p++ != ' ' ||2345 !(email_end = strchr(p, '>')) ||2346 email_end[1] != ' ' ||2347 !(timestamp = parse_timestamp(email_end + 2, &message, 10)) ||2348 !message || message[0] != ' ' ||2349 (message[1] != '+' && message[1] != '-') ||2350 !isdigit(message[2]) || !isdigit(message[3]) ||2351 !isdigit(message[4]) || !isdigit(message[5]))2352 return 0; /* corrupt? */2353 email_end[1] = '\0';2354 tz = strtol(message + 1, NULL, 10);2355 if (message[6] != '\t')2356 message += 6;2357 else2358 message += 7;2359 return fn(&ooid, &noid, p, timestamp, tz, message, cb_data);2360}23612362static char *find_beginning_of_line(char *bob, char *scan)2363{2364 while (bob < scan && *(--scan) != '\n')2365 ; /* keep scanning backwards */2366 /*2367 * Return either beginning of the buffer, or LF at the end of2368 * the previous line.2369 */2370 return scan;2371}23722373static int files_for_each_reflog_ent_reverse(struct ref_store *ref_store,2374 const char *refname,2375 each_reflog_ent_fn fn,2376 void *cb_data)2377{2378 struct files_ref_store *refs =2379 files_downcast(ref_store, REF_STORE_READ,2380 "for_each_reflog_ent_reverse");2381 struct strbuf sb = STRBUF_INIT;2382 FILE *logfp;2383 long pos;2384 int ret = 0, at_tail = 1;23852386 files_reflog_path(refs, &sb, refname);2387 logfp = fopen(sb.buf, "r");2388 strbuf_release(&sb);2389 if (!logfp)2390 return -1;23912392 /* Jump to the end */2393 if (fseek(logfp, 0, SEEK_END) < 0)2394 ret = error("cannot seek back reflog for %s: %s",2395 refname, strerror(errno));2396 pos = ftell(logfp);2397 while (!ret && 0 < pos) {2398 int cnt;2399 size_t nread;2400 char buf[BUFSIZ];2401 char *endp, *scanp;24022403 /* Fill next block from the end */2404 cnt = (sizeof(buf) < pos) ? sizeof(buf) : pos;2405 if (fseek(logfp, pos - cnt, SEEK_SET)) {2406 ret = error("cannot seek back reflog for %s: %s",2407 refname, strerror(errno));2408 break;2409 }2410 nread = fread(buf, cnt, 1, logfp);2411 if (nread != 1) {2412 ret = error("cannot read %d bytes from reflog for %s: %s",2413 cnt, refname, strerror(errno));2414 break;2415 }2416 pos -= cnt;24172418 scanp = endp = buf + cnt;2419 if (at_tail && scanp[-1] == '\n')2420 /* Looking at the final LF at the end of the file */2421 scanp--;2422 at_tail = 0;24232424 while (buf < scanp) {2425 /*2426 * terminating LF of the previous line, or the beginning2427 * of the buffer.2428 */2429 char *bp;24302431 bp = find_beginning_of_line(buf, scanp);24322433 if (*bp == '\n') {2434 /*2435 * The newline is the end of the previous line,2436 * so we know we have complete line starting2437 * at (bp + 1). Prefix it onto any prior data2438 * we collected for the line and process it.2439 */2440 strbuf_splice(&sb, 0, 0, bp + 1, endp - (bp + 1));2441 scanp = bp;2442 endp = bp + 1;2443 ret = show_one_reflog_ent(&sb, fn, cb_data);2444 strbuf_reset(&sb);2445 if (ret)2446 break;2447 } else if (!pos) {2448 /*2449 * We are at the start of the buffer, and the2450 * start of the file; there is no previous2451 * line, and we have everything for this one.2452 * Process it, and we can end the loop.2453 */2454 strbuf_splice(&sb, 0, 0, buf, endp - buf);2455 ret = show_one_reflog_ent(&sb, fn, cb_data);2456 strbuf_reset(&sb);2457 break;2458 }24592460 if (bp == buf) {2461 /*2462 * We are at the start of the buffer, and there2463 * is more file to read backwards. Which means2464 * we are in the middle of a line. Note that we2465 * may get here even if *bp was a newline; that2466 * just means we are at the exact end of the2467 * previous line, rather than some spot in the2468 * middle.2469 *2470 * Save away what we have to be combined with2471 * the data from the next read.2472 */2473 strbuf_splice(&sb, 0, 0, buf, endp - buf);2474 break;2475 }2476 }24772478 }2479 if (!ret && sb.len)2480 die("BUG: reverse reflog parser had leftover data");24812482 fclose(logfp);2483 strbuf_release(&sb);2484 return ret;2485}24862487static int files_for_each_reflog_ent(struct ref_store *ref_store,2488 const char *refname,2489 each_reflog_ent_fn fn, void *cb_data)2490{2491 struct files_ref_store *refs =2492 files_downcast(ref_store, REF_STORE_READ,2493 "for_each_reflog_ent");2494 FILE *logfp;2495 struct strbuf sb = STRBUF_INIT;2496 int ret = 0;24972498 files_reflog_path(refs, &sb, refname);2499 logfp = fopen(sb.buf, "r");2500 strbuf_release(&sb);2501 if (!logfp)2502 return -1;25032504 while (!ret && !strbuf_getwholeline(&sb, logfp, '\n'))2505 ret = show_one_reflog_ent(&sb, fn, cb_data);2506 fclose(logfp);2507 strbuf_release(&sb);2508 return ret;2509}25102511struct files_reflog_iterator {2512 struct ref_iterator base;25132514 struct ref_store *ref_store;2515 struct dir_iterator *dir_iterator;2516 struct object_id oid;2517};25182519static int files_reflog_iterator_advance(struct ref_iterator *ref_iterator)2520{2521 struct files_reflog_iterator *iter =2522 (struct files_reflog_iterator *)ref_iterator;2523 struct dir_iterator *diter = iter->dir_iterator;2524 int ok;25252526 while ((ok = dir_iterator_advance(diter)) == ITER_OK) {2527 int flags;25282529 if (!S_ISREG(diter->st.st_mode))2530 continue;2531 if (diter->basename[0] == '.')2532 continue;2533 if (ends_with(diter->basename, ".lock"))2534 continue;25352536 if (refs_read_ref_full(iter->ref_store,2537 diter->relative_path, 0,2538 iter->oid.hash, &flags)) {2539 error("bad ref for %s", diter->path.buf);2540 continue;2541 }25422543 iter->base.refname = diter->relative_path;2544 iter->base.oid = &iter->oid;2545 iter->base.flags = flags;2546 return ITER_OK;2547 }25482549 iter->dir_iterator = NULL;2550 if (ref_iterator_abort(ref_iterator) == ITER_ERROR)2551 ok = ITER_ERROR;2552 return ok;2553}25542555static int files_reflog_iterator_peel(struct ref_iterator *ref_iterator,2556 struct object_id *peeled)2557{2558 die("BUG: ref_iterator_peel() called for reflog_iterator");2559}25602561static int files_reflog_iterator_abort(struct ref_iterator *ref_iterator)2562{2563 struct files_reflog_iterator *iter =2564 (struct files_reflog_iterator *)ref_iterator;2565 int ok = ITER_DONE;25662567 if (iter->dir_iterator)2568 ok = dir_iterator_abort(iter->dir_iterator);25692570 base_ref_iterator_free(ref_iterator);2571 return ok;2572}25732574static struct ref_iterator_vtable files_reflog_iterator_vtable = {2575 files_reflog_iterator_advance,2576 files_reflog_iterator_peel,2577 files_reflog_iterator_abort2578};25792580static struct ref_iterator *files_reflog_iterator_begin(struct ref_store *ref_store)2581{2582 struct files_ref_store *refs =2583 files_downcast(ref_store, REF_STORE_READ,2584 "reflog_iterator_begin");2585 struct files_reflog_iterator *iter = xcalloc(1, sizeof(*iter));2586 struct ref_iterator *ref_iterator = &iter->base;2587 struct strbuf sb = STRBUF_INIT;25882589 base_ref_iterator_init(ref_iterator, &files_reflog_iterator_vtable);2590 files_reflog_path(refs, &sb, NULL);2591 iter->dir_iterator = dir_iterator_begin(sb.buf);2592 iter->ref_store = ref_store;2593 strbuf_release(&sb);2594 return ref_iterator;2595}25962597/*2598 * If update is a direct update of head_ref (the reference pointed to2599 * by HEAD), then add an extra REF_LOG_ONLY update for HEAD.2600 */2601static int split_head_update(struct ref_update *update,2602 struct ref_transaction *transaction,2603 const char *head_ref,2604 struct string_list *affected_refnames,2605 struct strbuf *err)2606{2607 struct string_list_item *item;2608 struct ref_update *new_update;26092610 if ((update->flags & REF_LOG_ONLY) ||2611 (update->flags & REF_ISPRUNING) ||2612 (update->flags & REF_UPDATE_VIA_HEAD))2613 return 0;26142615 if (strcmp(update->refname, head_ref))2616 return 0;26172618 /*2619 * First make sure that HEAD is not already in the2620 * transaction. This insertion is O(N) in the transaction2621 * size, but it happens at most once per transaction.2622 */2623 item = string_list_insert(affected_refnames, "HEAD");2624 if (item->util) {2625 /* An entry already existed */2626 strbuf_addf(err,2627 "multiple updates for 'HEAD' (including one "2628 "via its referent '%s') are not allowed",2629 update->refname);2630 return TRANSACTION_NAME_CONFLICT;2631 }26322633 new_update = ref_transaction_add_update(2634 transaction, "HEAD",2635 update->flags | REF_LOG_ONLY | REF_NODEREF,2636 update->new_oid.hash, update->old_oid.hash,2637 update->msg);26382639 item->util = new_update;26402641 return 0;2642}26432644/*2645 * update is for a symref that points at referent and doesn't have2646 * REF_NODEREF set. Split it into two updates:2647 * - The original update, but with REF_LOG_ONLY and REF_NODEREF set2648 * - A new, separate update for the referent reference2649 * Note that the new update will itself be subject to splitting when2650 * the iteration gets to it.2651 */2652static int split_symref_update(struct files_ref_store *refs,2653 struct ref_update *update,2654 const char *referent,2655 struct ref_transaction *transaction,2656 struct string_list *affected_refnames,2657 struct strbuf *err)2658{2659 struct string_list_item *item;2660 struct ref_update *new_update;2661 unsigned int new_flags;26622663 /*2664 * First make sure that referent is not already in the2665 * transaction. This insertion is O(N) in the transaction2666 * size, but it happens at most once per symref in a2667 * transaction.2668 */2669 item = string_list_insert(affected_refnames, referent);2670 if (item->util) {2671 /* An entry already existed */2672 strbuf_addf(err,2673 "multiple updates for '%s' (including one "2674 "via symref '%s') are not allowed",2675 referent, update->refname);2676 return TRANSACTION_NAME_CONFLICT;2677 }26782679 new_flags = update->flags;2680 if (!strcmp(update->refname, "HEAD")) {2681 /*2682 * Record that the new update came via HEAD, so that2683 * when we process it, split_head_update() doesn't try2684 * to add another reflog update for HEAD. Note that2685 * this bit will be propagated if the new_update2686 * itself needs to be split.2687 */2688 new_flags |= REF_UPDATE_VIA_HEAD;2689 }26902691 new_update = ref_transaction_add_update(2692 transaction, referent, new_flags,2693 update->new_oid.hash, update->old_oid.hash,2694 update->msg);26952696 new_update->parent_update = update;26972698 /*2699 * Change the symbolic ref update to log only. Also, it2700 * doesn't need to check its old SHA-1 value, as that will be2701 * done when new_update is processed.2702 */2703 update->flags |= REF_LOG_ONLY | REF_NODEREF;2704 update->flags &= ~REF_HAVE_OLD;27052706 item->util = new_update;27072708 return 0;2709}27102711/*2712 * Return the refname under which update was originally requested.2713 */2714static const char *original_update_refname(struct ref_update *update)2715{2716 while (update->parent_update)2717 update = update->parent_update;27182719 return update->refname;2720}27212722/*2723 * Check whether the REF_HAVE_OLD and old_oid values stored in update2724 * are consistent with oid, which is the reference's current value. If2725 * everything is OK, return 0; otherwise, write an error message to2726 * err and return -1.2727 */2728static int check_old_oid(struct ref_update *update, struct object_id *oid,2729 struct strbuf *err)2730{2731 if (!(update->flags & REF_HAVE_OLD) ||2732 !oidcmp(oid, &update->old_oid))2733 return 0;27342735 if (is_null_oid(&update->old_oid))2736 strbuf_addf(err, "cannot lock ref '%s': "2737 "reference already exists",2738 original_update_refname(update));2739 else if (is_null_oid(oid))2740 strbuf_addf(err, "cannot lock ref '%s': "2741 "reference is missing but expected %s",2742 original_update_refname(update),2743 oid_to_hex(&update->old_oid));2744 else2745 strbuf_addf(err, "cannot lock ref '%s': "2746 "is at %s but expected %s",2747 original_update_refname(update),2748 oid_to_hex(oid),2749 oid_to_hex(&update->old_oid));27502751 return -1;2752}27532754/*2755 * Prepare for carrying out update:2756 * - Lock the reference referred to by update.2757 * - Read the reference under lock.2758 * - Check that its old SHA-1 value (if specified) is correct, and in2759 * any case record it in update->lock->old_oid for later use when2760 * writing the reflog.2761 * - If it is a symref update without REF_NODEREF, split it up into a2762 * REF_LOG_ONLY update of the symref and add a separate update for2763 * the referent to transaction.2764 * - If it is an update of head_ref, add a corresponding REF_LOG_ONLY2765 * update of HEAD.2766 */2767static int lock_ref_for_update(struct files_ref_store *refs,2768 struct ref_update *update,2769 struct ref_transaction *transaction,2770 const char *head_ref,2771 struct string_list *affected_refnames,2772 struct strbuf *err)2773{2774 struct strbuf referent = STRBUF_INIT;2775 int mustexist = (update->flags & REF_HAVE_OLD) &&2776 !is_null_oid(&update->old_oid);2777 int ret;2778 struct ref_lock *lock;27792780 files_assert_main_repository(refs, "lock_ref_for_update");27812782 if ((update->flags & REF_HAVE_NEW) && is_null_oid(&update->new_oid))2783 update->flags |= REF_DELETING;27842785 if (head_ref) {2786 ret = split_head_update(update, transaction, head_ref,2787 affected_refnames, err);2788 if (ret)2789 return ret;2790 }27912792 ret = lock_raw_ref(refs, update->refname, mustexist,2793 affected_refnames, NULL,2794 &lock, &referent,2795 &update->type, err);2796 if (ret) {2797 char *reason;27982799 reason = strbuf_detach(err, NULL);2800 strbuf_addf(err, "cannot lock ref '%s': %s",2801 original_update_refname(update), reason);2802 free(reason);2803 return ret;2804 }28052806 update->backend_data = lock;28072808 if (update->type & REF_ISSYMREF) {2809 if (update->flags & REF_NODEREF) {2810 /*2811 * We won't be reading the referent as part of2812 * the transaction, so we have to read it here2813 * to record and possibly check old_sha1:2814 */2815 if (refs_read_ref_full(&refs->base,2816 referent.buf, 0,2817 lock->old_oid.hash, NULL)) {2818 if (update->flags & REF_HAVE_OLD) {2819 strbuf_addf(err, "cannot lock ref '%s': "2820 "error reading reference",2821 original_update_refname(update));2822 return -1;2823 }2824 } else if (check_old_oid(update, &lock->old_oid, err)) {2825 return TRANSACTION_GENERIC_ERROR;2826 }2827 } else {2828 /*2829 * Create a new update for the reference this2830 * symref is pointing at. Also, we will record2831 * and verify old_sha1 for this update as part2832 * of processing the split-off update, so we2833 * don't have to do it here.2834 */2835 ret = split_symref_update(refs, update,2836 referent.buf, transaction,2837 affected_refnames, err);2838 if (ret)2839 return ret;2840 }2841 } else {2842 struct ref_update *parent_update;28432844 if (check_old_oid(update, &lock->old_oid, err))2845 return TRANSACTION_GENERIC_ERROR;28462847 /*2848 * If this update is happening indirectly because of a2849 * symref update, record the old SHA-1 in the parent2850 * update:2851 */2852 for (parent_update = update->parent_update;2853 parent_update;2854 parent_update = parent_update->parent_update) {2855 struct ref_lock *parent_lock = parent_update->backend_data;2856 oidcpy(&parent_lock->old_oid, &lock->old_oid);2857 }2858 }28592860 if ((update->flags & REF_HAVE_NEW) &&2861 !(update->flags & REF_DELETING) &&2862 !(update->flags & REF_LOG_ONLY)) {2863 if (!(update->type & REF_ISSYMREF) &&2864 !oidcmp(&lock->old_oid, &update->new_oid)) {2865 /*2866 * The reference already has the desired2867 * value, so we don't need to write it.2868 */2869 } else if (write_ref_to_lockfile(lock, &update->new_oid,2870 err)) {2871 char *write_err = strbuf_detach(err, NULL);28722873 /*2874 * The lock was freed upon failure of2875 * write_ref_to_lockfile():2876 */2877 update->backend_data = NULL;2878 strbuf_addf(err,2879 "cannot update ref '%s': %s",2880 update->refname, write_err);2881 free(write_err);2882 return TRANSACTION_GENERIC_ERROR;2883 } else {2884 update->flags |= REF_NEEDS_COMMIT;2885 }2886 }2887 if (!(update->flags & REF_NEEDS_COMMIT)) {2888 /*2889 * We didn't call write_ref_to_lockfile(), so2890 * the lockfile is still open. Close it to2891 * free up the file descriptor:2892 */2893 if (close_ref(lock)) {2894 strbuf_addf(err, "couldn't close '%s.lock'",2895 update->refname);2896 return TRANSACTION_GENERIC_ERROR;2897 }2898 }2899 return 0;2900}29012902/*2903 * Unlock any references in `transaction` that are still locked, and2904 * mark the transaction closed.2905 */2906static void files_transaction_cleanup(struct ref_transaction *transaction)2907{2908 size_t i;29092910 for (i = 0; i < transaction->nr; i++) {2911 struct ref_update *update = transaction->updates[i];2912 struct ref_lock *lock = update->backend_data;29132914 if (lock) {2915 unlock_ref(lock);2916 update->backend_data = NULL;2917 }2918 }29192920 transaction->state = REF_TRANSACTION_CLOSED;2921}29222923static int files_transaction_prepare(struct ref_store *ref_store,2924 struct ref_transaction *transaction,2925 struct strbuf *err)2926{2927 struct files_ref_store *refs =2928 files_downcast(ref_store, REF_STORE_WRITE,2929 "ref_transaction_prepare");2930 size_t i;2931 int ret = 0;2932 struct string_list affected_refnames = STRING_LIST_INIT_NODUP;2933 char *head_ref = NULL;2934 int head_type;2935 struct object_id head_oid;29362937 assert(err);29382939 if (!transaction->nr)2940 goto cleanup;29412942 /*2943 * Fail if a refname appears more than once in the2944 * transaction. (If we end up splitting up any updates using2945 * split_symref_update() or split_head_update(), those2946 * functions will check that the new updates don't have the2947 * same refname as any existing ones.)2948 */2949 for (i = 0; i < transaction->nr; i++) {2950 struct ref_update *update = transaction->updates[i];2951 struct string_list_item *item =2952 string_list_append(&affected_refnames, update->refname);29532954 /*2955 * We store a pointer to update in item->util, but at2956 * the moment we never use the value of this field2957 * except to check whether it is non-NULL.2958 */2959 item->util = update;2960 }2961 string_list_sort(&affected_refnames);2962 if (ref_update_reject_duplicates(&affected_refnames, err)) {2963 ret = TRANSACTION_GENERIC_ERROR;2964 goto cleanup;2965 }29662967 /*2968 * Special hack: If a branch is updated directly and HEAD2969 * points to it (may happen on the remote side of a push2970 * for example) then logically the HEAD reflog should be2971 * updated too.2972 *2973 * A generic solution would require reverse symref lookups,2974 * but finding all symrefs pointing to a given branch would be2975 * rather costly for this rare event (the direct update of a2976 * branch) to be worth it. So let's cheat and check with HEAD2977 * only, which should cover 99% of all usage scenarios (even2978 * 100% of the default ones).2979 *2980 * So if HEAD is a symbolic reference, then record the name of2981 * the reference that it points to. If we see an update of2982 * head_ref within the transaction, then split_head_update()2983 * arranges for the reflog of HEAD to be updated, too.2984 */2985 head_ref = refs_resolve_refdup(ref_store, "HEAD",2986 RESOLVE_REF_NO_RECURSE,2987 head_oid.hash, &head_type);29882989 if (head_ref && !(head_type & REF_ISSYMREF)) {2990 free(head_ref);2991 head_ref = NULL;2992 }29932994 /*2995 * Acquire all locks, verify old values if provided, check2996 * that new values are valid, and write new values to the2997 * lockfiles, ready to be activated. Only keep one lockfile2998 * open at a time to avoid running out of file descriptors.2999 * Note that lock_ref_for_update() might append more updates3000 * to the transaction.3001 */3002 for (i = 0; i < transaction->nr; i++) {3003 struct ref_update *update = transaction->updates[i];30043005 ret = lock_ref_for_update(refs, update, transaction,3006 head_ref, &affected_refnames, err);3007 if (ret)3008 break;3009 }30103011cleanup:3012 free(head_ref);3013 string_list_clear(&affected_refnames, 0);30143015 if (ret)3016 files_transaction_cleanup(transaction);3017 else3018 transaction->state = REF_TRANSACTION_PREPARED;30193020 return ret;3021}30223023static int files_transaction_finish(struct ref_store *ref_store,3024 struct ref_transaction *transaction,3025 struct strbuf *err)3026{3027 struct files_ref_store *refs =3028 files_downcast(ref_store, 0, "ref_transaction_finish");3029 size_t i;3030 int ret = 0;3031 struct string_list refs_to_delete = STRING_LIST_INIT_NODUP;3032 struct string_list_item *ref_to_delete;3033 struct strbuf sb = STRBUF_INIT;30343035 assert(err);30363037 if (!transaction->nr) {3038 transaction->state = REF_TRANSACTION_CLOSED;3039 return 0;3040 }30413042 /* Perform updates first so live commits remain referenced */3043 for (i = 0; i < transaction->nr; i++) {3044 struct ref_update *update = transaction->updates[i];3045 struct ref_lock *lock = update->backend_data;30463047 if (update->flags & REF_NEEDS_COMMIT ||3048 update->flags & REF_LOG_ONLY) {3049 if (files_log_ref_write(refs,3050 lock->ref_name,3051 &lock->old_oid,3052 &update->new_oid,3053 update->msg, update->flags,3054 err)) {3055 char *old_msg = strbuf_detach(err, NULL);30563057 strbuf_addf(err, "cannot update the ref '%s': %s",3058 lock->ref_name, old_msg);3059 free(old_msg);3060 unlock_ref(lock);3061 update->backend_data = NULL;3062 ret = TRANSACTION_GENERIC_ERROR;3063 goto cleanup;3064 }3065 }3066 if (update->flags & REF_NEEDS_COMMIT) {3067 clear_loose_ref_cache(refs);3068 if (commit_ref(lock)) {3069 strbuf_addf(err, "couldn't set '%s'", lock->ref_name);3070 unlock_ref(lock);3071 update->backend_data = NULL;3072 ret = TRANSACTION_GENERIC_ERROR;3073 goto cleanup;3074 }3075 }3076 }3077 /* Perform deletes now that updates are safely completed */3078 for (i = 0; i < transaction->nr; i++) {3079 struct ref_update *update = transaction->updates[i];3080 struct ref_lock *lock = update->backend_data;30813082 if (update->flags & REF_DELETING &&3083 !(update->flags & REF_LOG_ONLY)) {3084 if (!(update->type & REF_ISPACKED) ||3085 update->type & REF_ISSYMREF) {3086 /* It is a loose reference. */3087 strbuf_reset(&sb);3088 files_ref_path(refs, &sb, lock->ref_name);3089 if (unlink_or_msg(sb.buf, err)) {3090 ret = TRANSACTION_GENERIC_ERROR;3091 goto cleanup;3092 }3093 update->flags |= REF_DELETED_LOOSE;3094 }30953096 if (!(update->flags & REF_ISPRUNING))3097 string_list_append(&refs_to_delete,3098 lock->ref_name);3099 }3100 }31013102 if (repack_without_refs(refs, &refs_to_delete, err)) {3103 ret = TRANSACTION_GENERIC_ERROR;3104 goto cleanup;3105 }31063107 /* Delete the reflogs of any references that were deleted: */3108 for_each_string_list_item(ref_to_delete, &refs_to_delete) {3109 strbuf_reset(&sb);3110 files_reflog_path(refs, &sb, ref_to_delete->string);3111 if (!unlink_or_warn(sb.buf))3112 try_remove_empty_parents(refs, ref_to_delete->string,3113 REMOVE_EMPTY_PARENTS_REFLOG);3114 }31153116 clear_loose_ref_cache(refs);31173118cleanup:3119 files_transaction_cleanup(transaction);31203121 for (i = 0; i < transaction->nr; i++) {3122 struct ref_update *update = transaction->updates[i];31233124 if (update->flags & REF_DELETED_LOOSE) {3125 /*3126 * The loose reference was deleted. Delete any3127 * empty parent directories. (Note that this3128 * can only work because we have already3129 * removed the lockfile.)3130 */3131 try_remove_empty_parents(refs, update->refname,3132 REMOVE_EMPTY_PARENTS_REF);3133 }3134 }31353136 strbuf_release(&sb);3137 string_list_clear(&refs_to_delete, 0);3138 return ret;3139}31403141static int files_transaction_abort(struct ref_store *ref_store,3142 struct ref_transaction *transaction,3143 struct strbuf *err)3144{3145 files_transaction_cleanup(transaction);3146 return 0;3147}31483149static int ref_present(const char *refname,3150 const struct object_id *oid, int flags, void *cb_data)3151{3152 struct string_list *affected_refnames = cb_data;31533154 return string_list_has_string(affected_refnames, refname);3155}31563157static int files_initial_transaction_commit(struct ref_store *ref_store,3158 struct ref_transaction *transaction,3159 struct strbuf *err)3160{3161 struct files_ref_store *refs =3162 files_downcast(ref_store, REF_STORE_WRITE,3163 "initial_ref_transaction_commit");3164 size_t i;3165 int ret = 0;3166 struct string_list affected_refnames = STRING_LIST_INIT_NODUP;31673168 assert(err);31693170 if (transaction->state != REF_TRANSACTION_OPEN)3171 die("BUG: commit called for transaction that is not open");31723173 /* Fail if a refname appears more than once in the transaction: */3174 for (i = 0; i < transaction->nr; i++)3175 string_list_append(&affected_refnames,3176 transaction->updates[i]->refname);3177 string_list_sort(&affected_refnames);3178 if (ref_update_reject_duplicates(&affected_refnames, err)) {3179 ret = TRANSACTION_GENERIC_ERROR;3180 goto cleanup;3181 }31823183 /*3184 * It's really undefined to call this function in an active3185 * repository or when there are existing references: we are3186 * only locking and changing packed-refs, so (1) any3187 * simultaneous processes might try to change a reference at3188 * the same time we do, and (2) any existing loose versions of3189 * the references that we are setting would have precedence3190 * over our values. But some remote helpers create the remote3191 * "HEAD" and "master" branches before calling this function,3192 * so here we really only check that none of the references3193 * that we are creating already exists.3194 */3195 if (refs_for_each_rawref(&refs->base, ref_present,3196 &affected_refnames))3197 die("BUG: initial ref transaction called with existing refs");31983199 for (i = 0; i < transaction->nr; i++) {3200 struct ref_update *update = transaction->updates[i];32013202 if ((update->flags & REF_HAVE_OLD) &&3203 !is_null_oid(&update->old_oid))3204 die("BUG: initial ref transaction with old_sha1 set");3205 if (refs_verify_refname_available(&refs->base, update->refname,3206 &affected_refnames, NULL,3207 err)) {3208 ret = TRANSACTION_NAME_CONFLICT;3209 goto cleanup;3210 }3211 }32123213 if (lock_packed_refs(refs->packed_ref_store, 0)) {3214 strbuf_addf(err, "unable to lock packed-refs file: %s",3215 strerror(errno));3216 ret = TRANSACTION_GENERIC_ERROR;3217 goto cleanup;3218 }32193220 for (i = 0; i < transaction->nr; i++) {3221 struct ref_update *update = transaction->updates[i];32223223 if ((update->flags & REF_HAVE_NEW) &&3224 !is_null_oid(&update->new_oid))3225 add_packed_ref(refs->packed_ref_store, update->refname,3226 &update->new_oid);3227 }32283229 if (commit_packed_refs(refs->packed_ref_store)) {3230 strbuf_addf(err, "unable to commit packed-refs file: %s",3231 strerror(errno));3232 ret = TRANSACTION_GENERIC_ERROR;3233 goto cleanup;3234 }32353236cleanup:3237 transaction->state = REF_TRANSACTION_CLOSED;3238 string_list_clear(&affected_refnames, 0);3239 return ret;3240}32413242struct expire_reflog_cb {3243 unsigned int flags;3244 reflog_expiry_should_prune_fn *should_prune_fn;3245 void *policy_cb;3246 FILE *newlog;3247 struct object_id last_kept_oid;3248};32493250static int expire_reflog_ent(struct object_id *ooid, struct object_id *noid,3251 const char *email, timestamp_t timestamp, int tz,3252 const char *message, void *cb_data)3253{3254 struct expire_reflog_cb *cb = cb_data;3255 struct expire_reflog_policy_cb *policy_cb = cb->policy_cb;32563257 if (cb->flags & EXPIRE_REFLOGS_REWRITE)3258 ooid = &cb->last_kept_oid;32593260 if ((*cb->should_prune_fn)(ooid, noid, email, timestamp, tz,3261 message, policy_cb)) {3262 if (!cb->newlog)3263 printf("would prune %s", message);3264 else if (cb->flags & EXPIRE_REFLOGS_VERBOSE)3265 printf("prune %s", message);3266 } else {3267 if (cb->newlog) {3268 fprintf(cb->newlog, "%s %s %s %"PRItime" %+05d\t%s",3269 oid_to_hex(ooid), oid_to_hex(noid),3270 email, timestamp, tz, message);3271 oidcpy(&cb->last_kept_oid, noid);3272 }3273 if (cb->flags & EXPIRE_REFLOGS_VERBOSE)3274 printf("keep %s", message);3275 }3276 return 0;3277}32783279static int files_reflog_expire(struct ref_store *ref_store,3280 const char *refname, const unsigned char *sha1,3281 unsigned int flags,3282 reflog_expiry_prepare_fn prepare_fn,3283 reflog_expiry_should_prune_fn should_prune_fn,3284 reflog_expiry_cleanup_fn cleanup_fn,3285 void *policy_cb_data)3286{3287 struct files_ref_store *refs =3288 files_downcast(ref_store, REF_STORE_WRITE, "reflog_expire");3289 static struct lock_file reflog_lock;3290 struct expire_reflog_cb cb;3291 struct ref_lock *lock;3292 struct strbuf log_file_sb = STRBUF_INIT;3293 char *log_file;3294 int status = 0;3295 int type;3296 struct strbuf err = STRBUF_INIT;3297 struct object_id oid;32983299 memset(&cb, 0, sizeof(cb));3300 cb.flags = flags;3301 cb.policy_cb = policy_cb_data;3302 cb.should_prune_fn = should_prune_fn;33033304 /*3305 * The reflog file is locked by holding the lock on the3306 * reference itself, plus we might need to update the3307 * reference if --updateref was specified:3308 */3309 lock = lock_ref_sha1_basic(refs, refname, sha1,3310 NULL, NULL, REF_NODEREF,3311 &type, &err);3312 if (!lock) {3313 error("cannot lock ref '%s': %s", refname, err.buf);3314 strbuf_release(&err);3315 return -1;3316 }3317 if (!refs_reflog_exists(ref_store, refname)) {3318 unlock_ref(lock);3319 return 0;3320 }33213322 files_reflog_path(refs, &log_file_sb, refname);3323 log_file = strbuf_detach(&log_file_sb, NULL);3324 if (!(flags & EXPIRE_REFLOGS_DRY_RUN)) {3325 /*3326 * Even though holding $GIT_DIR/logs/$reflog.lock has3327 * no locking implications, we use the lock_file3328 * machinery here anyway because it does a lot of the3329 * work we need, including cleaning up if the program3330 * exits unexpectedly.3331 */3332 if (hold_lock_file_for_update(&reflog_lock, log_file, 0) < 0) {3333 struct strbuf err = STRBUF_INIT;3334 unable_to_lock_message(log_file, errno, &err);3335 error("%s", err.buf);3336 strbuf_release(&err);3337 goto failure;3338 }3339 cb.newlog = fdopen_lock_file(&reflog_lock, "w");3340 if (!cb.newlog) {3341 error("cannot fdopen %s (%s)",3342 get_lock_file_path(&reflog_lock), strerror(errno));3343 goto failure;3344 }3345 }33463347 hashcpy(oid.hash, sha1);33483349 (*prepare_fn)(refname, &oid, cb.policy_cb);3350 refs_for_each_reflog_ent(ref_store, refname, expire_reflog_ent, &cb);3351 (*cleanup_fn)(cb.policy_cb);33523353 if (!(flags & EXPIRE_REFLOGS_DRY_RUN)) {3354 /*3355 * It doesn't make sense to adjust a reference pointed3356 * to by a symbolic ref based on expiring entries in3357 * the symbolic reference's reflog. Nor can we update3358 * a reference if there are no remaining reflog3359 * entries.3360 */3361 int update = (flags & EXPIRE_REFLOGS_UPDATE_REF) &&3362 !(type & REF_ISSYMREF) &&3363 !is_null_oid(&cb.last_kept_oid);33643365 if (close_lock_file(&reflog_lock)) {3366 status |= error("couldn't write %s: %s", log_file,3367 strerror(errno));3368 } else if (update &&3369 (write_in_full(get_lock_file_fd(lock->lk),3370 oid_to_hex(&cb.last_kept_oid), GIT_SHA1_HEXSZ) != GIT_SHA1_HEXSZ ||3371 write_str_in_full(get_lock_file_fd(lock->lk), "\n") != 1 ||3372 close_ref(lock) < 0)) {3373 status |= error("couldn't write %s",3374 get_lock_file_path(lock->lk));3375 rollback_lock_file(&reflog_lock);3376 } else if (commit_lock_file(&reflog_lock)) {3377 status |= error("unable to write reflog '%s' (%s)",3378 log_file, strerror(errno));3379 } else if (update && commit_ref(lock)) {3380 status |= error("couldn't set %s", lock->ref_name);3381 }3382 }3383 free(log_file);3384 unlock_ref(lock);3385 return status;33863387 failure:3388 rollback_lock_file(&reflog_lock);3389 free(log_file);3390 unlock_ref(lock);3391 return -1;3392}33933394static int files_init_db(struct ref_store *ref_store, struct strbuf *err)3395{3396 struct files_ref_store *refs =3397 files_downcast(ref_store, REF_STORE_WRITE, "init_db");3398 struct strbuf sb = STRBUF_INIT;33993400 /*3401 * Create .git/refs/{heads,tags}3402 */3403 files_ref_path(refs, &sb, "refs/heads");3404 safe_create_dir(sb.buf, 1);34053406 strbuf_reset(&sb);3407 files_ref_path(refs, &sb, "refs/tags");3408 safe_create_dir(sb.buf, 1);34093410 strbuf_release(&sb);3411 return 0;3412}34133414struct ref_storage_be refs_be_files = {3415 NULL,3416 "files",3417 files_ref_store_create,3418 files_init_db,3419 files_transaction_prepare,3420 files_transaction_finish,3421 files_transaction_abort,3422 files_initial_transaction_commit,34233424 files_pack_refs,3425 files_peel_ref,3426 files_create_symref,3427 files_delete_refs,3428 files_rename_ref,34293430 files_ref_iterator_begin,3431 files_read_raw_ref,34323433 files_reflog_iterator_begin,3434 files_for_each_reflog_ent,3435 files_for_each_reflog_ent_reverse,3436 files_reflog_exists,3437 files_create_reflog,3438 files_delete_reflog,3439 files_reflog_expire3440};