1#include"../cache.h" 2#include"../config.h" 3#include"../refs.h" 4#include"refs-internal.h" 5#include"ref-cache.h" 6#include"packed-backend.h" 7#include"../iterator.h" 8#include"../lockfile.h" 9 10struct packed_ref_store; 11 12struct packed_ref_cache { 13/* 14 * A back-pointer to the packed_ref_store with which this 15 * cache is associated: 16 */ 17struct packed_ref_store *refs; 18 19struct ref_cache *cache; 20 21/* 22 * Count of references to the data structure in this instance, 23 * including the pointer from files_ref_store::packed if any. 24 * The data will not be freed as long as the reference count 25 * is nonzero. 26 */ 27unsigned int referrers; 28 29/* The metadata from when this packed-refs cache was read */ 30struct stat_validity validity; 31}; 32 33/* 34 * Increment the reference count of *packed_refs. 35 */ 36static voidacquire_packed_ref_cache(struct packed_ref_cache *packed_refs) 37{ 38 packed_refs->referrers++; 39} 40 41/* 42 * Decrease the reference count of *packed_refs. If it goes to zero, 43 * free *packed_refs and return true; otherwise return false. 44 */ 45static intrelease_packed_ref_cache(struct packed_ref_cache *packed_refs) 46{ 47if(!--packed_refs->referrers) { 48free_ref_cache(packed_refs->cache); 49stat_validity_clear(&packed_refs->validity); 50free(packed_refs); 51return1; 52}else{ 53return0; 54} 55} 56 57/* 58 * A container for `packed-refs`-related data. It is not (yet) a 59 * `ref_store`. 60 */ 61struct packed_ref_store { 62struct ref_store base; 63 64unsigned int store_flags; 65 66/* The path of the "packed-refs" file: */ 67char*path; 68 69/* 70 * A cache of the values read from the `packed-refs` file, if 71 * it might still be current; otherwise, NULL. 72 */ 73struct packed_ref_cache *cache; 74 75/* 76 * Lock used for the "packed-refs" file. Note that this (and 77 * thus the enclosing `packed_ref_store`) must not be freed. 78 */ 79struct lock_file lock; 80 81/* 82 * Temporary file used when rewriting new contents to the 83 * "packed-refs" file. Note that this (and thus the enclosing 84 * `packed_ref_store`) must not be freed. 85 */ 86struct tempfile tempfile; 87}; 88 89struct ref_store *packed_ref_store_create(const char*path, 90unsigned int store_flags) 91{ 92struct packed_ref_store *refs =xcalloc(1,sizeof(*refs)); 93struct ref_store *ref_store = (struct ref_store *)refs; 94 95base_ref_store_init(ref_store, &refs_be_packed); 96 refs->store_flags = store_flags; 97 98 refs->path =xstrdup(path); 99return ref_store; 100} 101 102/* 103 * Downcast `ref_store` to `packed_ref_store`. Die if `ref_store` is 104 * not a `packed_ref_store`. Also die if `packed_ref_store` doesn't 105 * support at least the flags specified in `required_flags`. `caller` 106 * is used in any necessary error messages. 107 */ 108static struct packed_ref_store *packed_downcast(struct ref_store *ref_store, 109unsigned int required_flags, 110const char*caller) 111{ 112struct packed_ref_store *refs; 113 114if(ref_store->be != &refs_be_packed) 115die("BUG: ref_store is type\"%s\"not\"packed\"in%s", 116 ref_store->be->name, caller); 117 118 refs = (struct packed_ref_store *)ref_store; 119 120if((refs->store_flags & required_flags) != required_flags) 121die("BUG: unallowed operation (%s), requires%x, has%x\n", 122 caller, required_flags, refs->store_flags); 123 124return refs; 125} 126 127static voidclear_packed_ref_cache(struct packed_ref_store *refs) 128{ 129if(refs->cache) { 130struct packed_ref_cache *cache = refs->cache; 131 132 refs->cache = NULL; 133release_packed_ref_cache(cache); 134} 135} 136 137static NORETURN voiddie_unterminated_line(const char*path, 138const char*p,size_t len) 139{ 140if(len <80) 141die("unterminated line in%s: %.*s", path, (int)len, p); 142else 143die("unterminated line in%s: %.75s...", path, p); 144} 145 146static NORETURN voiddie_invalid_line(const char*path, 147const char*p,size_t len) 148{ 149const char*eol =memchr(p,'\n', len); 150 151if(!eol) 152die_unterminated_line(path, p, len); 153else if(eol - p <80) 154die("unexpected line in%s: %.*s", path, (int)(eol - p), p); 155else 156die("unexpected line in%s: %.75s...", path, p); 157 158} 159 160/* 161 * Read from the `packed-refs` file into a newly-allocated 162 * `packed_ref_cache` and return it. The return value will already 163 * have its reference count incremented. 164 * 165 * A comment line of the form "# pack-refs with: " may contain zero or 166 * more traits. We interpret the traits as follows: 167 * 168 * No traits: 169 * 170 * Probably no references are peeled. But if the file contains a 171 * peeled value for a reference, we will use it. 172 * 173 * peeled: 174 * 175 * References under "refs/tags/", if they *can* be peeled, *are* 176 * peeled in this file. References outside of "refs/tags/" are 177 * probably not peeled even if they could have been, but if we find 178 * a peeled value for such a reference we will use it. 179 * 180 * fully-peeled: 181 * 182 * All references in the file that can be peeled are peeled. 183 * Inversely (and this is more important), any references in the 184 * file for which no peeled value is recorded is not peelable. This 185 * trait should typically be written alongside "peeled" for 186 * compatibility with older clients, but we do not require it 187 * (i.e., "peeled" is a no-op if "fully-peeled" is set). 188 */ 189static struct packed_ref_cache *read_packed_refs(struct packed_ref_store *refs) 190{ 191struct packed_ref_cache *packed_refs =xcalloc(1,sizeof(*packed_refs)); 192int fd; 193struct stat st; 194size_t size; 195char*buf; 196const char*pos, *eol, *eof; 197struct strbuf tmp = STRBUF_INIT; 198enum{ PEELED_NONE, PEELED_TAGS, PEELED_FULLY } peeled = PEELED_NONE; 199struct ref_dir *dir; 200 201 packed_refs->refs = refs; 202acquire_packed_ref_cache(packed_refs); 203 packed_refs->cache =create_ref_cache(NULL, NULL); 204 packed_refs->cache->root->flag &= ~REF_INCOMPLETE; 205 206 fd =open(refs->path, O_RDONLY); 207if(fd <0) { 208if(errno == ENOENT) { 209/* 210 * This is OK; it just means that no 211 * "packed-refs" file has been written yet, 212 * which is equivalent to it being empty. 213 */ 214return packed_refs; 215}else{ 216die_errno("couldn't read%s", refs->path); 217} 218} 219 220stat_validity_update(&packed_refs->validity, fd); 221 222if(fstat(fd, &st) <0) 223die_errno("couldn't stat%s", refs->path); 224 225 size =xsize_t(st.st_size); 226 buf =xmmap(NULL, size, PROT_READ, MAP_PRIVATE, fd,0); 227 pos = buf; 228 eof = buf + size; 229 230/* If the file has a header line, process it: */ 231if(pos < eof && *pos =='#') { 232char*p; 233struct string_list traits = STRING_LIST_INIT_NODUP; 234 235 eol =memchr(pos,'\n', eof - pos); 236if(!eol) 237die_unterminated_line(refs->path, pos, eof - pos); 238 239strbuf_add(&tmp, pos, eol - pos); 240 241if(!skip_prefix(tmp.buf,"# pack-refs with:", (const char**)&p)) 242die_invalid_line(refs->path, pos, eof - pos); 243 244string_list_split_in_place(&traits, p,' ', -1); 245 246if(unsorted_string_list_has_string(&traits,"fully-peeled")) 247 peeled = PEELED_FULLY; 248else if(unsorted_string_list_has_string(&traits,"peeled")) 249 peeled = PEELED_TAGS; 250/* perhaps other traits later as well */ 251 252/* The "+ 1" is for the LF character. */ 253 pos = eol +1; 254 255string_list_clear(&traits,0); 256strbuf_reset(&tmp); 257} 258 259 dir =get_ref_dir(packed_refs->cache->root); 260while(pos < eof) { 261const char*p = pos; 262struct object_id oid; 263const char*refname; 264int flag = REF_ISPACKED; 265struct ref_entry *entry = NULL; 266 267if(eof - pos < GIT_SHA1_HEXSZ +2|| 268parse_oid_hex(p, &oid, &p) || 269!isspace(*p++)) 270die_invalid_line(refs->path, pos, eof - pos); 271 272 eol =memchr(p,'\n', eof - p); 273if(!eol) 274die_unterminated_line(refs->path, pos, eof - pos); 275 276strbuf_add(&tmp, p, eol - p); 277 refname = tmp.buf; 278 279if(check_refname_format(refname, REFNAME_ALLOW_ONELEVEL)) { 280if(!refname_is_safe(refname)) 281die("packed refname is dangerous:%s", refname); 282oidclr(&oid); 283 flag |= REF_BAD_NAME | REF_ISBROKEN; 284} 285if(peeled == PEELED_FULLY || 286(peeled == PEELED_TAGS &&starts_with(refname,"refs/tags/"))) 287 flag |= REF_KNOWS_PEELED; 288 entry =create_ref_entry(refname, &oid, flag); 289add_ref_entry(dir, entry); 290 291 pos = eol +1; 292 293if(pos < eof && *pos =='^') { 294 p = pos +1; 295if(eof - p < GIT_SHA1_HEXSZ +1|| 296parse_oid_hex(p, &entry->u.value.peeled, &p) || 297*p++ !='\n') 298die_invalid_line(refs->path, pos, eof - pos); 299 300/* 301 * Regardless of what the file header said, 302 * we definitely know the value of *this* 303 * reference: 304 */ 305 entry->flag |= REF_KNOWS_PEELED; 306 307 pos = p; 308} 309 310strbuf_reset(&tmp); 311} 312 313if(munmap(buf, size)) 314die_errno("error ummapping packed-refs file"); 315close(fd); 316 317strbuf_release(&tmp); 318return packed_refs; 319} 320 321/* 322 * Check that the packed refs cache (if any) still reflects the 323 * contents of the file. If not, clear the cache. 324 */ 325static voidvalidate_packed_ref_cache(struct packed_ref_store *refs) 326{ 327if(refs->cache && 328!stat_validity_check(&refs->cache->validity, refs->path)) 329clear_packed_ref_cache(refs); 330} 331 332/* 333 * Get the packed_ref_cache for the specified packed_ref_store, 334 * creating and populating it if it hasn't been read before or if the 335 * file has been changed (according to its `validity` field) since it 336 * was last read. On the other hand, if we hold the lock, then assume 337 * that the file hasn't been changed out from under us, so skip the 338 * extra `stat()` call in `stat_validity_check()`. 339 */ 340static struct packed_ref_cache *get_packed_ref_cache(struct packed_ref_store *refs) 341{ 342if(!is_lock_file_locked(&refs->lock)) 343validate_packed_ref_cache(refs); 344 345if(!refs->cache) 346 refs->cache =read_packed_refs(refs); 347 348return refs->cache; 349} 350 351static struct ref_dir *get_packed_ref_dir(struct packed_ref_cache *packed_ref_cache) 352{ 353returnget_ref_dir(packed_ref_cache->cache->root); 354} 355 356static struct ref_dir *get_packed_refs(struct packed_ref_store *refs) 357{ 358returnget_packed_ref_dir(get_packed_ref_cache(refs)); 359} 360 361/* 362 * Return the ref_entry for the given refname from the packed 363 * references. If it does not exist, return NULL. 364 */ 365static struct ref_entry *get_packed_ref(struct packed_ref_store *refs, 366const char*refname) 367{ 368returnfind_ref_entry(get_packed_refs(refs), refname); 369} 370 371static intpacked_read_raw_ref(struct ref_store *ref_store, 372const char*refname,unsigned char*sha1, 373struct strbuf *referent,unsigned int*type) 374{ 375struct packed_ref_store *refs = 376packed_downcast(ref_store, REF_STORE_READ,"read_raw_ref"); 377 378struct ref_entry *entry; 379 380*type =0; 381 382 entry =get_packed_ref(refs, refname); 383if(!entry) { 384 errno = ENOENT; 385return-1; 386} 387 388hashcpy(sha1, entry->u.value.oid.hash); 389*type = REF_ISPACKED; 390return0; 391} 392 393static intpacked_peel_ref(struct ref_store *ref_store, 394const char*refname,unsigned char*sha1) 395{ 396struct packed_ref_store *refs = 397packed_downcast(ref_store, REF_STORE_READ | REF_STORE_ODB, 398"peel_ref"); 399struct ref_entry *r =get_packed_ref(refs, refname); 400 401if(!r ||peel_entry(r,0)) 402return-1; 403 404hashcpy(sha1, r->u.value.peeled.hash); 405return0; 406} 407 408struct packed_ref_iterator { 409struct ref_iterator base; 410 411struct packed_ref_cache *cache; 412struct ref_iterator *iter0; 413unsigned int flags; 414}; 415 416static intpacked_ref_iterator_advance(struct ref_iterator *ref_iterator) 417{ 418struct packed_ref_iterator *iter = 419(struct packed_ref_iterator *)ref_iterator; 420int ok; 421 422while((ok =ref_iterator_advance(iter->iter0)) == ITER_OK) { 423if(iter->flags & DO_FOR_EACH_PER_WORKTREE_ONLY && 424ref_type(iter->iter0->refname) != REF_TYPE_PER_WORKTREE) 425continue; 426 427if(!(iter->flags & DO_FOR_EACH_INCLUDE_BROKEN) && 428!ref_resolves_to_object(iter->iter0->refname, 429 iter->iter0->oid, 430 iter->iter0->flags)) 431continue; 432 433 iter->base.refname = iter->iter0->refname; 434 iter->base.oid = iter->iter0->oid; 435 iter->base.flags = iter->iter0->flags; 436return ITER_OK; 437} 438 439 iter->iter0 = NULL; 440if(ref_iterator_abort(ref_iterator) != ITER_DONE) 441 ok = ITER_ERROR; 442 443return ok; 444} 445 446static intpacked_ref_iterator_peel(struct ref_iterator *ref_iterator, 447struct object_id *peeled) 448{ 449struct packed_ref_iterator *iter = 450(struct packed_ref_iterator *)ref_iterator; 451 452returnref_iterator_peel(iter->iter0, peeled); 453} 454 455static intpacked_ref_iterator_abort(struct ref_iterator *ref_iterator) 456{ 457struct packed_ref_iterator *iter = 458(struct packed_ref_iterator *)ref_iterator; 459int ok = ITER_DONE; 460 461if(iter->iter0) 462 ok =ref_iterator_abort(iter->iter0); 463 464release_packed_ref_cache(iter->cache); 465base_ref_iterator_free(ref_iterator); 466return ok; 467} 468 469static struct ref_iterator_vtable packed_ref_iterator_vtable = { 470 packed_ref_iterator_advance, 471 packed_ref_iterator_peel, 472 packed_ref_iterator_abort 473}; 474 475static struct ref_iterator *packed_ref_iterator_begin( 476struct ref_store *ref_store, 477const char*prefix,unsigned int flags) 478{ 479struct packed_ref_store *refs; 480struct packed_ref_iterator *iter; 481struct ref_iterator *ref_iterator; 482unsigned int required_flags = REF_STORE_READ; 483 484if(!(flags & DO_FOR_EACH_INCLUDE_BROKEN)) 485 required_flags |= REF_STORE_ODB; 486 refs =packed_downcast(ref_store, required_flags,"ref_iterator_begin"); 487 488 iter =xcalloc(1,sizeof(*iter)); 489 ref_iterator = &iter->base; 490base_ref_iterator_init(ref_iterator, &packed_ref_iterator_vtable,1); 491 492/* 493 * Note that get_packed_ref_cache() internally checks whether 494 * the packed-ref cache is up to date with what is on disk, 495 * and re-reads it if not. 496 */ 497 498 iter->cache =get_packed_ref_cache(refs); 499acquire_packed_ref_cache(iter->cache); 500 iter->iter0 =cache_ref_iterator_begin(iter->cache->cache, prefix,0); 501 502 iter->flags = flags; 503 504return ref_iterator; 505} 506 507/* 508 * Write an entry to the packed-refs file for the specified refname. 509 * If peeled is non-NULL, write it as the entry's peeled value. On 510 * error, return a nonzero value and leave errno set at the value left 511 * by the failing call to `fprintf()`. 512 */ 513static intwrite_packed_entry(FILE*fh,const char*refname, 514const unsigned char*sha1, 515const unsigned char*peeled) 516{ 517if(fprintf(fh,"%s %s\n",sha1_to_hex(sha1), refname) <0|| 518(peeled &&fprintf(fh,"^%s\n",sha1_to_hex(peeled)) <0)) 519return-1; 520 521return0; 522} 523 524intpacked_refs_lock(struct ref_store *ref_store,int flags,struct strbuf *err) 525{ 526struct packed_ref_store *refs = 527packed_downcast(ref_store, REF_STORE_WRITE | REF_STORE_MAIN, 528"packed_refs_lock"); 529static int timeout_configured =0; 530static int timeout_value =1000; 531 532if(!timeout_configured) { 533git_config_get_int("core.packedrefstimeout", &timeout_value); 534 timeout_configured =1; 535} 536 537/* 538 * Note that we close the lockfile immediately because we 539 * don't write new content to it, but rather to a separate 540 * tempfile. 541 */ 542if(hold_lock_file_for_update_timeout( 543&refs->lock, 544 refs->path, 545 flags, timeout_value) <0) { 546unable_to_lock_message(refs->path, errno, err); 547return-1; 548} 549 550if(close_lock_file(&refs->lock)) { 551strbuf_addf(err,"unable to close%s:%s", refs->path,strerror(errno)); 552return-1; 553} 554 555/* 556 * Now that we hold the `packed-refs` lock, make sure that our 557 * cache matches the current version of the file. Normally 558 * `get_packed_ref_cache()` does that for us, but that 559 * function assumes that when the file is locked, any existing 560 * cache is still valid. We've just locked the file, but it 561 * might have changed the moment *before* we locked it. 562 */ 563validate_packed_ref_cache(refs); 564 565/* 566 * Now make sure that the packed-refs file as it exists in the 567 * locked state is loaded into the cache: 568 */ 569get_packed_ref_cache(refs); 570return0; 571} 572 573voidpacked_refs_unlock(struct ref_store *ref_store) 574{ 575struct packed_ref_store *refs =packed_downcast( 576 ref_store, 577 REF_STORE_READ | REF_STORE_WRITE, 578"packed_refs_unlock"); 579 580if(!is_lock_file_locked(&refs->lock)) 581die("BUG: packed_refs_unlock() called when not locked"); 582rollback_lock_file(&refs->lock); 583} 584 585intpacked_refs_is_locked(struct ref_store *ref_store) 586{ 587struct packed_ref_store *refs =packed_downcast( 588 ref_store, 589 REF_STORE_READ | REF_STORE_WRITE, 590"packed_refs_is_locked"); 591 592returnis_lock_file_locked(&refs->lock); 593} 594 595/* 596 * The packed-refs header line that we write out. Perhaps other 597 * traits will be added later. 598 * 599 * Note that earlier versions of Git used to parse these traits by 600 * looking for " trait " in the line. For this reason, the space after 601 * the colon and the trailing space are required. 602 */ 603static const char PACKED_REFS_HEADER[] = 604"# pack-refs with: peeled fully-peeled\n"; 605 606static intpacked_init_db(struct ref_store *ref_store,struct strbuf *err) 607{ 608/* Nothing to do. */ 609return0; 610} 611 612/* 613 * Write the packed-refs from the cache to the packed-refs tempfile, 614 * incorporating any changes from `updates`. `updates` must be a 615 * sorted string list whose keys are the refnames and whose util 616 * values are `struct ref_update *`. On error, rollback the tempfile, 617 * write an error message to `err`, and return a nonzero value. 618 * 619 * The packfile must be locked before calling this function and will 620 * remain locked when it is done. 621 */ 622static intwrite_with_updates(struct packed_ref_store *refs, 623struct string_list *updates, 624struct strbuf *err) 625{ 626struct ref_iterator *iter = NULL; 627size_t i; 628int ok; 629FILE*out; 630struct strbuf sb = STRBUF_INIT; 631char*packed_refs_path; 632 633if(!is_lock_file_locked(&refs->lock)) 634die("BUG: write_with_updates() called while unlocked"); 635 636/* 637 * If packed-refs is a symlink, we want to overwrite the 638 * symlinked-to file, not the symlink itself. Also, put the 639 * staging file next to it: 640 */ 641 packed_refs_path =get_locked_file_path(&refs->lock); 642strbuf_addf(&sb,"%s.new", packed_refs_path); 643free(packed_refs_path); 644if(create_tempfile(&refs->tempfile, sb.buf) <0) { 645strbuf_addf(err,"unable to create file%s:%s", 646 sb.buf,strerror(errno)); 647strbuf_release(&sb); 648return-1; 649} 650strbuf_release(&sb); 651 652 out =fdopen_tempfile(&refs->tempfile,"w"); 653if(!out) { 654strbuf_addf(err,"unable to fdopen packed-refs tempfile:%s", 655strerror(errno)); 656goto error; 657} 658 659if(fprintf(out,"%s", PACKED_REFS_HEADER) <0) 660goto write_error; 661 662/* 663 * We iterate in parallel through the current list of refs and 664 * the list of updates, processing an entry from at least one 665 * of the lists each time through the loop. When the current 666 * list of refs is exhausted, set iter to NULL. When the list 667 * of updates is exhausted, leave i set to updates->nr. 668 */ 669 iter =packed_ref_iterator_begin(&refs->base,"", 670 DO_FOR_EACH_INCLUDE_BROKEN); 671if((ok =ref_iterator_advance(iter)) != ITER_OK) 672 iter = NULL; 673 674 i =0; 675 676while(iter || i < updates->nr) { 677struct ref_update *update = NULL; 678int cmp; 679 680if(i >= updates->nr) { 681 cmp = -1; 682}else{ 683 update = updates->items[i].util; 684 685if(!iter) 686 cmp = +1; 687else 688 cmp =strcmp(iter->refname, update->refname); 689} 690 691if(!cmp) { 692/* 693 * There is both an old value and an update 694 * for this reference. Check the old value if 695 * necessary: 696 */ 697if((update->flags & REF_HAVE_OLD)) { 698if(is_null_oid(&update->old_oid)) { 699strbuf_addf(err,"cannot update ref '%s': " 700"reference already exists", 701 update->refname); 702goto error; 703}else if(oidcmp(&update->old_oid, iter->oid)) { 704strbuf_addf(err,"cannot update ref '%s': " 705"is at%sbut expected%s", 706 update->refname, 707oid_to_hex(iter->oid), 708oid_to_hex(&update->old_oid)); 709goto error; 710} 711} 712 713/* Now figure out what to use for the new value: */ 714if((update->flags & REF_HAVE_NEW)) { 715/* 716 * The update takes precedence. Skip 717 * the iterator over the unneeded 718 * value. 719 */ 720if((ok =ref_iterator_advance(iter)) != ITER_OK) 721 iter = NULL; 722 cmp = +1; 723}else{ 724/* 725 * The update doesn't actually want to 726 * change anything. We're done with it. 727 */ 728 i++; 729 cmp = -1; 730} 731}else if(cmp >0) { 732/* 733 * There is no old value but there is an 734 * update for this reference. Make sure that 735 * the update didn't expect an existing value: 736 */ 737if((update->flags & REF_HAVE_OLD) && 738!is_null_oid(&update->old_oid)) { 739strbuf_addf(err,"cannot update ref '%s': " 740"reference is missing but expected%s", 741 update->refname, 742oid_to_hex(&update->old_oid)); 743goto error; 744} 745} 746 747if(cmp <0) { 748/* Pass the old reference through. */ 749 750struct object_id peeled; 751int peel_error =ref_iterator_peel(iter, &peeled); 752 753if(write_packed_entry(out, iter->refname, 754 iter->oid->hash, 755 peel_error ? NULL : peeled.hash)) 756goto write_error; 757 758if((ok =ref_iterator_advance(iter)) != ITER_OK) 759 iter = NULL; 760}else if(is_null_oid(&update->new_oid)) { 761/* 762 * The update wants to delete the reference, 763 * and the reference either didn't exist or we 764 * have already skipped it. So we're done with 765 * the update (and don't have to write 766 * anything). 767 */ 768 i++; 769}else{ 770struct object_id peeled; 771int peel_error =peel_object(update->new_oid.hash, 772 peeled.hash); 773 774if(write_packed_entry(out, update->refname, 775 update->new_oid.hash, 776 peel_error ? NULL : peeled.hash)) 777goto write_error; 778 779 i++; 780} 781} 782 783if(ok != ITER_DONE) { 784strbuf_addf(err,"unable to write packed-refs file: " 785"error iterating over old contents"); 786goto error; 787} 788 789if(close_tempfile(&refs->tempfile)) { 790strbuf_addf(err,"error closing file%s:%s", 791get_tempfile_path(&refs->tempfile), 792strerror(errno)); 793strbuf_release(&sb); 794return-1; 795} 796 797return0; 798 799write_error: 800strbuf_addf(err,"error writing to%s:%s", 801get_tempfile_path(&refs->tempfile),strerror(errno)); 802 803error: 804if(iter) 805ref_iterator_abort(iter); 806 807delete_tempfile(&refs->tempfile); 808return-1; 809} 810 811struct packed_transaction_backend_data { 812/* True iff the transaction owns the packed-refs lock. */ 813int own_lock; 814 815struct string_list updates; 816}; 817 818static voidpacked_transaction_cleanup(struct packed_ref_store *refs, 819struct ref_transaction *transaction) 820{ 821struct packed_transaction_backend_data *data = transaction->backend_data; 822 823if(data) { 824string_list_clear(&data->updates,0); 825 826if(is_tempfile_active(&refs->tempfile)) 827delete_tempfile(&refs->tempfile); 828 829if(data->own_lock &&is_lock_file_locked(&refs->lock)) { 830packed_refs_unlock(&refs->base); 831 data->own_lock =0; 832} 833 834free(data); 835 transaction->backend_data = NULL; 836} 837 838 transaction->state = REF_TRANSACTION_CLOSED; 839} 840 841static intpacked_transaction_prepare(struct ref_store *ref_store, 842struct ref_transaction *transaction, 843struct strbuf *err) 844{ 845struct packed_ref_store *refs =packed_downcast( 846 ref_store, 847 REF_STORE_READ | REF_STORE_WRITE | REF_STORE_ODB, 848"ref_transaction_prepare"); 849struct packed_transaction_backend_data *data; 850size_t i; 851int ret = TRANSACTION_GENERIC_ERROR; 852 853/* 854 * Note that we *don't* skip transactions with zero updates, 855 * because such a transaction might be executed for the side 856 * effect of ensuring that all of the references are peeled. 857 * If the caller wants to optimize away empty transactions, it 858 * should do so itself. 859 */ 860 861 data =xcalloc(1,sizeof(*data)); 862string_list_init(&data->updates,0); 863 864 transaction->backend_data = data; 865 866/* 867 * Stick the updates in a string list by refname so that we 868 * can sort them: 869 */ 870for(i =0; i < transaction->nr; i++) { 871struct ref_update *update = transaction->updates[i]; 872struct string_list_item *item = 873string_list_append(&data->updates, update->refname); 874 875/* Store a pointer to update in item->util: */ 876 item->util = update; 877} 878string_list_sort(&data->updates); 879 880if(ref_update_reject_duplicates(&data->updates, err)) 881goto failure; 882 883if(!is_lock_file_locked(&refs->lock)) { 884if(packed_refs_lock(ref_store,0, err)) 885goto failure; 886 data->own_lock =1; 887} 888 889if(write_with_updates(refs, &data->updates, err)) 890goto failure; 891 892 transaction->state = REF_TRANSACTION_PREPARED; 893return0; 894 895failure: 896packed_transaction_cleanup(refs, transaction); 897return ret; 898} 899 900static intpacked_transaction_abort(struct ref_store *ref_store, 901struct ref_transaction *transaction, 902struct strbuf *err) 903{ 904struct packed_ref_store *refs =packed_downcast( 905 ref_store, 906 REF_STORE_READ | REF_STORE_WRITE | REF_STORE_ODB, 907"ref_transaction_abort"); 908 909packed_transaction_cleanup(refs, transaction); 910return0; 911} 912 913static intpacked_transaction_finish(struct ref_store *ref_store, 914struct ref_transaction *transaction, 915struct strbuf *err) 916{ 917struct packed_ref_store *refs =packed_downcast( 918 ref_store, 919 REF_STORE_READ | REF_STORE_WRITE | REF_STORE_ODB, 920"ref_transaction_finish"); 921int ret = TRANSACTION_GENERIC_ERROR; 922char*packed_refs_path; 923 924 packed_refs_path =get_locked_file_path(&refs->lock); 925if(rename_tempfile(&refs->tempfile, packed_refs_path)) { 926strbuf_addf(err,"error replacing%s:%s", 927 refs->path,strerror(errno)); 928goto cleanup; 929} 930 931clear_packed_ref_cache(refs); 932 ret =0; 933 934cleanup: 935free(packed_refs_path); 936packed_transaction_cleanup(refs, transaction); 937return ret; 938} 939 940static intpacked_initial_transaction_commit(struct ref_store *ref_store, 941struct ref_transaction *transaction, 942struct strbuf *err) 943{ 944returnref_transaction_commit(transaction, err); 945} 946 947static intpacked_delete_refs(struct ref_store *ref_store,const char*msg, 948struct string_list *refnames,unsigned int flags) 949{ 950struct packed_ref_store *refs = 951packed_downcast(ref_store, REF_STORE_WRITE,"delete_refs"); 952struct strbuf err = STRBUF_INIT; 953struct ref_transaction *transaction; 954struct string_list_item *item; 955int ret; 956 957(void)refs;/* We need the check above, but don't use the variable */ 958 959if(!refnames->nr) 960return0; 961 962/* 963 * Since we don't check the references' old_oids, the 964 * individual updates can't fail, so we can pack all of the 965 * updates into a single transaction. 966 */ 967 968 transaction =ref_store_transaction_begin(ref_store, &err); 969if(!transaction) 970return-1; 971 972for_each_string_list_item(item, refnames) { 973if(ref_transaction_delete(transaction, item->string, NULL, 974 flags, msg, &err)) { 975warning(_("could not delete reference%s:%s"), 976 item->string, err.buf); 977strbuf_reset(&err); 978} 979} 980 981 ret =ref_transaction_commit(transaction, &err); 982 983if(ret) { 984if(refnames->nr ==1) 985error(_("could not delete reference%s:%s"), 986 refnames->items[0].string, err.buf); 987else 988error(_("could not delete references:%s"), err.buf); 989} 990 991ref_transaction_free(transaction); 992strbuf_release(&err); 993return ret; 994} 995 996static intpacked_pack_refs(struct ref_store *ref_store,unsigned int flags) 997{ 998/* 999 * Packed refs are already packed. It might be that loose refs1000 * are packed *into* a packed refs store, but that is done by1001 * updating the packed references via a transaction.1002 */1003return0;1004}10051006static intpacked_create_symref(struct ref_store *ref_store,1007const char*refname,const char*target,1008const char*logmsg)1009{1010die("BUG: packed reference store does not support symrefs");1011}10121013static intpacked_rename_ref(struct ref_store *ref_store,1014const char*oldrefname,const char*newrefname,1015const char*logmsg)1016{1017die("BUG: packed reference store does not support renaming references");1018}10191020static struct ref_iterator *packed_reflog_iterator_begin(struct ref_store *ref_store)1021{1022returnempty_ref_iterator_begin();1023}10241025static intpacked_for_each_reflog_ent(struct ref_store *ref_store,1026const char*refname,1027 each_reflog_ent_fn fn,void*cb_data)1028{1029return0;1030}10311032static intpacked_for_each_reflog_ent_reverse(struct ref_store *ref_store,1033const char*refname,1034 each_reflog_ent_fn fn,1035void*cb_data)1036{1037return0;1038}10391040static intpacked_reflog_exists(struct ref_store *ref_store,1041const char*refname)1042{1043return0;1044}10451046static intpacked_create_reflog(struct ref_store *ref_store,1047const char*refname,int force_create,1048struct strbuf *err)1049{1050die("BUG: packed reference store does not support reflogs");1051}10521053static intpacked_delete_reflog(struct ref_store *ref_store,1054const char*refname)1055{1056return0;1057}10581059static intpacked_reflog_expire(struct ref_store *ref_store,1060const char*refname,const unsigned char*sha1,1061unsigned int flags,1062 reflog_expiry_prepare_fn prepare_fn,1063 reflog_expiry_should_prune_fn should_prune_fn,1064 reflog_expiry_cleanup_fn cleanup_fn,1065void*policy_cb_data)1066{1067return0;1068}10691070struct ref_storage_be refs_be_packed = {1071 NULL,1072"packed",1073 packed_ref_store_create,1074 packed_init_db,1075 packed_transaction_prepare,1076 packed_transaction_finish,1077 packed_transaction_abort,1078 packed_initial_transaction_commit,10791080 packed_pack_refs,1081 packed_peel_ref,1082 packed_create_symref,1083 packed_delete_refs,1084 packed_rename_ref,10851086 packed_ref_iterator_begin,1087 packed_read_raw_ref,10881089 packed_reflog_iterator_begin,1090 packed_for_each_reflog_ent,1091 packed_for_each_reflog_ent_reverse,1092 packed_reflog_exists,1093 packed_create_reflog,1094 packed_delete_reflog,1095 packed_reflog_expire1096};