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