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 { 12char*ref_name; 13struct lock_file *lk; 14struct 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 intref_resolves_to_object(const char*refname, 23const struct object_id *oid, 24unsigned int flags) 25{ 26if(flags & REF_ISBROKEN) 27return0; 28if(!has_sha1_file(oid->hash)) { 29error("%sdoes not point to a valid object!", refname); 30return0; 31} 32return1; 33} 34 35struct packed_ref_cache { 36struct 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 */ 44unsigned int referrers; 45 46/* The metadata from when this packed-refs cache was read */ 47struct stat_validity validity; 48}; 49 50/* 51 * Future: need to be in "struct repository" 52 * when doing a full libification. 53 */ 54struct files_ref_store { 55struct ref_store base; 56unsigned int store_flags; 57 58char*gitdir; 59char*gitcommondir; 60char*packed_refs_path; 61 62struct ref_cache *loose; 63struct packed_ref_cache *packed; 64 65/* 66 * Lock used for the "packed-refs" file. Note that this (and 67 * thus the enclosing `files_ref_store`) must not be freed. 68 */ 69struct lock_file packed_refs_lock; 70}; 71 72/* 73 * Increment the reference count of *packed_refs. 74 */ 75static voidacquire_packed_ref_cache(struct packed_ref_cache *packed_refs) 76{ 77 packed_refs->referrers++; 78} 79 80/* 81 * Decrease the reference count of *packed_refs. If it goes to zero, 82 * free *packed_refs and return true; otherwise return false. 83 */ 84static intrelease_packed_ref_cache(struct packed_ref_cache *packed_refs) 85{ 86if(!--packed_refs->referrers) { 87free_ref_cache(packed_refs->cache); 88stat_validity_clear(&packed_refs->validity); 89free(packed_refs); 90return1; 91}else{ 92return0; 93} 94} 95 96static voidclear_packed_ref_cache(struct files_ref_store *refs) 97{ 98if(refs->packed) { 99struct packed_ref_cache *packed_refs = refs->packed; 100 101if(is_lock_file_locked(&refs->packed_refs_lock)) 102die("BUG: packed-ref cache cleared while locked"); 103 refs->packed = NULL; 104release_packed_ref_cache(packed_refs); 105} 106} 107 108static voidclear_loose_ref_cache(struct files_ref_store *refs) 109{ 110if(refs->loose) { 111free_ref_cache(refs->loose); 112 refs->loose = NULL; 113} 114} 115 116/* 117 * Create a new submodule ref cache and add it to the internal 118 * set of caches. 119 */ 120static struct ref_store *files_ref_store_create(const char*gitdir, 121unsigned int flags) 122{ 123struct files_ref_store *refs =xcalloc(1,sizeof(*refs)); 124struct ref_store *ref_store = (struct ref_store *)refs; 125struct strbuf sb = STRBUF_INIT; 126 127base_ref_store_init(ref_store, &refs_be_files); 128 refs->store_flags = flags; 129 130 refs->gitdir =xstrdup(gitdir); 131get_common_dir_noenv(&sb, gitdir); 132 refs->gitcommondir =strbuf_detach(&sb, NULL); 133strbuf_addf(&sb,"%s/packed-refs", refs->gitcommondir); 134 refs->packed_refs_path =strbuf_detach(&sb, NULL); 135 136return ref_store; 137} 138 139/* 140 * Die if refs is not the main ref store. caller is used in any 141 * necessary error messages. 142 */ 143static voidfiles_assert_main_repository(struct files_ref_store *refs, 144const char*caller) 145{ 146if(refs->store_flags & REF_STORE_MAIN) 147return; 148 149die("BUG: operation%sonly allowed for main ref store", caller); 150} 151 152/* 153 * Downcast ref_store to files_ref_store. Die if ref_store is not a 154 * files_ref_store. required_flags is compared with ref_store's 155 * store_flags to ensure the ref_store has all required capabilities. 156 * "caller" is used in any necessary error messages. 157 */ 158static struct files_ref_store *files_downcast(struct ref_store *ref_store, 159unsigned int required_flags, 160const char*caller) 161{ 162struct files_ref_store *refs; 163 164if(ref_store->be != &refs_be_files) 165die("BUG: ref_store is type\"%s\"not\"files\"in%s", 166 ref_store->be->name, caller); 167 168 refs = (struct files_ref_store *)ref_store; 169 170if((refs->store_flags & required_flags) != required_flags) 171die("BUG: operation%srequires abilities 0x%x, but only have 0x%x", 172 caller, required_flags, refs->store_flags); 173 174return refs; 175} 176 177/* The length of a peeled reference line in packed-refs, including EOL: */ 178#define PEELED_LINE_LENGTH 42 179 180/* 181 * The packed-refs header line that we write out. Perhaps other 182 * traits will be added later. The trailing space is required. 183 */ 184static const char PACKED_REFS_HEADER[] = 185"# pack-refs with: peeled fully-peeled\n"; 186 187/* 188 * Parse one line from a packed-refs file. Write the SHA1 to sha1. 189 * Return a pointer to the refname within the line (null-terminated), 190 * or NULL if there was a problem. 191 */ 192static const char*parse_ref_line(struct strbuf *line,struct object_id *oid) 193{ 194const char*ref; 195 196if(parse_oid_hex(line->buf, oid, &ref) <0) 197return NULL; 198if(!isspace(*ref++)) 199return NULL; 200 201if(isspace(*ref)) 202return NULL; 203 204if(line->buf[line->len -1] !='\n') 205return NULL; 206 line->buf[--line->len] =0; 207 208return ref; 209} 210 211/* 212 * Read from `packed_refs_file` into a newly-allocated 213 * `packed_ref_cache` and return it. The return value will already 214 * have its reference count incremented. 215 * 216 * A comment line of the form "# pack-refs with: " may contain zero or 217 * more traits. We interpret the traits as follows: 218 * 219 * No traits: 220 * 221 * Probably no references are peeled. But if the file contains a 222 * peeled value for a reference, we will use it. 223 * 224 * peeled: 225 * 226 * References under "refs/tags/", if they *can* be peeled, *are* 227 * peeled in this file. References outside of "refs/tags/" are 228 * probably not peeled even if they could have been, but if we find 229 * a peeled value for such a reference we will use it. 230 * 231 * fully-peeled: 232 * 233 * All references in the file that can be peeled are peeled. 234 * Inversely (and this is more important), any references in the 235 * file for which no peeled value is recorded is not peelable. This 236 * trait should typically be written alongside "peeled" for 237 * compatibility with older clients, but we do not require it 238 * (i.e., "peeled" is a no-op if "fully-peeled" is set). 239 */ 240static struct packed_ref_cache *read_packed_refs(const char*packed_refs_file) 241{ 242FILE*f; 243struct packed_ref_cache *packed_refs =xcalloc(1,sizeof(*packed_refs)); 244struct ref_entry *last = NULL; 245struct strbuf line = STRBUF_INIT; 246enum{ PEELED_NONE, PEELED_TAGS, PEELED_FULLY } peeled = PEELED_NONE; 247struct ref_dir *dir; 248 249acquire_packed_ref_cache(packed_refs); 250 packed_refs->cache =create_ref_cache(NULL, NULL); 251 packed_refs->cache->root->flag &= ~REF_INCOMPLETE; 252 253 f =fopen(packed_refs_file,"r"); 254if(!f) { 255if(errno == ENOENT) { 256/* 257 * This is OK; it just means that no 258 * "packed-refs" file has been written yet, 259 * which is equivalent to it being empty. 260 */ 261return packed_refs; 262}else{ 263die_errno("couldn't read%s", packed_refs_file); 264} 265} 266 267stat_validity_update(&packed_refs->validity,fileno(f)); 268 269 dir =get_ref_dir(packed_refs->cache->root); 270while(strbuf_getwholeline(&line, f,'\n') != EOF) { 271struct object_id oid; 272const char*refname; 273const char*traits; 274 275if(skip_prefix(line.buf,"# pack-refs with:", &traits)) { 276if(strstr(traits," fully-peeled ")) 277 peeled = PEELED_FULLY; 278else if(strstr(traits," peeled ")) 279 peeled = PEELED_TAGS; 280/* perhaps other traits later as well */ 281continue; 282} 283 284 refname =parse_ref_line(&line, &oid); 285if(refname) { 286int flag = REF_ISPACKED; 287 288if(check_refname_format(refname, REFNAME_ALLOW_ONELEVEL)) { 289if(!refname_is_safe(refname)) 290die("packed refname is dangerous:%s", refname); 291oidclr(&oid); 292 flag |= REF_BAD_NAME | REF_ISBROKEN; 293} 294 last =create_ref_entry(refname, &oid, flag); 295if(peeled == PEELED_FULLY || 296(peeled == PEELED_TAGS &&starts_with(refname,"refs/tags/"))) 297 last->flag |= REF_KNOWS_PEELED; 298add_ref_entry(dir, last); 299continue; 300} 301if(last && 302 line.buf[0] =='^'&& 303 line.len == PEELED_LINE_LENGTH && 304 line.buf[PEELED_LINE_LENGTH -1] =='\n'&& 305!get_oid_hex(line.buf +1, &oid)) { 306oidcpy(&last->u.value.peeled, &oid); 307/* 308 * Regardless of what the file header said, 309 * we definitely know the value of *this* 310 * reference: 311 */ 312 last->flag |= REF_KNOWS_PEELED; 313} 314} 315 316fclose(f); 317strbuf_release(&line); 318 319return packed_refs; 320} 321 322static const char*files_packed_refs_path(struct files_ref_store *refs) 323{ 324return refs->packed_refs_path; 325} 326 327static voidfiles_reflog_path(struct files_ref_store *refs, 328struct strbuf *sb, 329const char*refname) 330{ 331if(!refname) { 332/* 333 * FIXME: of course this is wrong in multi worktree 334 * setting. To be fixed real soon. 335 */ 336strbuf_addf(sb,"%s/logs", refs->gitcommondir); 337return; 338} 339 340switch(ref_type(refname)) { 341case REF_TYPE_PER_WORKTREE: 342case REF_TYPE_PSEUDOREF: 343strbuf_addf(sb,"%s/logs/%s", refs->gitdir, refname); 344break; 345case REF_TYPE_NORMAL: 346strbuf_addf(sb,"%s/logs/%s", refs->gitcommondir, refname); 347break; 348default: 349die("BUG: unknown ref type%dof ref%s", 350ref_type(refname), refname); 351} 352} 353 354static voidfiles_ref_path(struct files_ref_store *refs, 355struct strbuf *sb, 356const char*refname) 357{ 358switch(ref_type(refname)) { 359case REF_TYPE_PER_WORKTREE: 360case REF_TYPE_PSEUDOREF: 361strbuf_addf(sb,"%s/%s", refs->gitdir, refname); 362break; 363case REF_TYPE_NORMAL: 364strbuf_addf(sb,"%s/%s", refs->gitcommondir, refname); 365break; 366default: 367die("BUG: unknown ref type%dof ref%s", 368ref_type(refname), refname); 369} 370} 371 372/* 373 * Get the packed_ref_cache for the specified files_ref_store, 374 * creating and populating it if it hasn't been read before or if the 375 * file has been changed (according to its `validity` field) since it 376 * was last read. On the other hand, if we hold the lock, then assume 377 * that the file hasn't been changed out from under us, so skip the 378 * extra `stat()` call in `stat_validity_check()`. 379 */ 380static struct packed_ref_cache *get_packed_ref_cache(struct files_ref_store *refs) 381{ 382const char*packed_refs_file =files_packed_refs_path(refs); 383 384if(refs->packed && 385!is_lock_file_locked(&refs->packed_refs_lock) && 386!stat_validity_check(&refs->packed->validity, packed_refs_file)) 387clear_packed_ref_cache(refs); 388 389if(!refs->packed) 390 refs->packed =read_packed_refs(packed_refs_file); 391 392return refs->packed; 393} 394 395static struct ref_dir *get_packed_ref_dir(struct packed_ref_cache *packed_ref_cache) 396{ 397returnget_ref_dir(packed_ref_cache->cache->root); 398} 399 400static struct ref_dir *get_packed_refs(struct files_ref_store *refs) 401{ 402returnget_packed_ref_dir(get_packed_ref_cache(refs)); 403} 404 405/* 406 * Add a reference to the in-memory packed reference cache. This may 407 * only be called while the packed-refs file is locked (see 408 * lock_packed_refs()). To actually write the packed-refs file, call 409 * commit_packed_refs(). 410 */ 411static voidadd_packed_ref(struct files_ref_store *refs, 412const char*refname,const struct object_id *oid) 413{ 414struct packed_ref_cache *packed_ref_cache =get_packed_ref_cache(refs); 415 416if(!is_lock_file_locked(&refs->packed_refs_lock)) 417die("BUG: packed refs not locked"); 418 419if(check_refname_format(refname, REFNAME_ALLOW_ONELEVEL)) 420die("Reference has invalid format: '%s'", refname); 421 422add_ref_entry(get_packed_ref_dir(packed_ref_cache), 423create_ref_entry(refname, oid, REF_ISPACKED)); 424} 425 426/* 427 * Read the loose references from the namespace dirname into dir 428 * (without recursing). dirname must end with '/'. dir must be the 429 * directory entry corresponding to dirname. 430 */ 431static voidloose_fill_ref_dir(struct ref_store *ref_store, 432struct ref_dir *dir,const char*dirname) 433{ 434struct files_ref_store *refs = 435files_downcast(ref_store, REF_STORE_READ,"fill_ref_dir"); 436DIR*d; 437struct dirent *de; 438int dirnamelen =strlen(dirname); 439struct strbuf refname; 440struct strbuf path = STRBUF_INIT; 441size_t path_baselen; 442 443files_ref_path(refs, &path, dirname); 444 path_baselen = path.len; 445 446 d =opendir(path.buf); 447if(!d) { 448strbuf_release(&path); 449return; 450} 451 452strbuf_init(&refname, dirnamelen +257); 453strbuf_add(&refname, dirname, dirnamelen); 454 455while((de =readdir(d)) != NULL) { 456struct object_id oid; 457struct stat st; 458int flag; 459 460if(de->d_name[0] =='.') 461continue; 462if(ends_with(de->d_name,".lock")) 463continue; 464strbuf_addstr(&refname, de->d_name); 465strbuf_addstr(&path, de->d_name); 466if(stat(path.buf, &st) <0) { 467;/* silently ignore */ 468}else if(S_ISDIR(st.st_mode)) { 469strbuf_addch(&refname,'/'); 470add_entry_to_dir(dir, 471create_dir_entry(dir->cache, refname.buf, 472 refname.len,1)); 473}else{ 474if(!refs_resolve_ref_unsafe(&refs->base, 475 refname.buf, 476 RESOLVE_REF_READING, 477 oid.hash, &flag)) { 478oidclr(&oid); 479 flag |= REF_ISBROKEN; 480}else if(is_null_oid(&oid)) { 481/* 482 * It is so astronomically unlikely 483 * that NULL_SHA1 is the SHA-1 of an 484 * actual object that we consider its 485 * appearance in a loose reference 486 * file to be repo corruption 487 * (probably due to a software bug). 488 */ 489 flag |= REF_ISBROKEN; 490} 491 492if(check_refname_format(refname.buf, 493 REFNAME_ALLOW_ONELEVEL)) { 494if(!refname_is_safe(refname.buf)) 495die("loose refname is dangerous:%s", refname.buf); 496oidclr(&oid); 497 flag |= REF_BAD_NAME | REF_ISBROKEN; 498} 499add_entry_to_dir(dir, 500create_ref_entry(refname.buf, &oid, flag)); 501} 502strbuf_setlen(&refname, dirnamelen); 503strbuf_setlen(&path, path_baselen); 504} 505strbuf_release(&refname); 506strbuf_release(&path); 507closedir(d); 508 509/* 510 * Manually add refs/bisect, which, being per-worktree, might 511 * not appear in the directory listing for refs/ in the main 512 * repo. 513 */ 514if(!strcmp(dirname,"refs/")) { 515int pos =search_ref_dir(dir,"refs/bisect/",12); 516 517if(pos <0) { 518struct ref_entry *child_entry =create_dir_entry( 519 dir->cache,"refs/bisect/",12,1); 520add_entry_to_dir(dir, child_entry); 521} 522} 523} 524 525static struct ref_cache *get_loose_ref_cache(struct files_ref_store *refs) 526{ 527if(!refs->loose) { 528/* 529 * Mark the top-level directory complete because we 530 * are about to read the only subdirectory that can 531 * hold references: 532 */ 533 refs->loose =create_ref_cache(&refs->base, loose_fill_ref_dir); 534 535/* We're going to fill the top level ourselves: */ 536 refs->loose->root->flag &= ~REF_INCOMPLETE; 537 538/* 539 * Add an incomplete entry for "refs/" (to be filled 540 * lazily): 541 */ 542add_entry_to_dir(get_ref_dir(refs->loose->root), 543create_dir_entry(refs->loose,"refs/",5,1)); 544} 545return refs->loose; 546} 547 548/* 549 * Return the ref_entry for the given refname from the packed 550 * references. If it does not exist, return NULL. 551 */ 552static struct ref_entry *get_packed_ref(struct files_ref_store *refs, 553const char*refname) 554{ 555returnfind_ref_entry(get_packed_refs(refs), refname); 556} 557 558/* 559 * A loose ref file doesn't exist; check for a packed ref. 560 */ 561static intresolve_packed_ref(struct files_ref_store *refs, 562const char*refname, 563unsigned char*sha1,unsigned int*flags) 564{ 565struct ref_entry *entry; 566 567/* 568 * The loose reference file does not exist; check for a packed 569 * reference. 570 */ 571 entry =get_packed_ref(refs, refname); 572if(entry) { 573hashcpy(sha1, entry->u.value.oid.hash); 574*flags |= REF_ISPACKED; 575return0; 576} 577/* refname is not a packed reference. */ 578return-1; 579} 580 581static intfiles_read_raw_ref(struct ref_store *ref_store, 582const char*refname,unsigned char*sha1, 583struct strbuf *referent,unsigned int*type) 584{ 585struct files_ref_store *refs = 586files_downcast(ref_store, REF_STORE_READ,"read_raw_ref"); 587struct strbuf sb_contents = STRBUF_INIT; 588struct strbuf sb_path = STRBUF_INIT; 589const char*path; 590const char*buf; 591struct stat st; 592int fd; 593int ret = -1; 594int save_errno; 595int remaining_retries =3; 596 597*type =0; 598strbuf_reset(&sb_path); 599 600files_ref_path(refs, &sb_path, refname); 601 602 path = sb_path.buf; 603 604stat_ref: 605/* 606 * We might have to loop back here to avoid a race 607 * condition: first we lstat() the file, then we try 608 * to read it as a link or as a file. But if somebody 609 * changes the type of the file (file <-> directory 610 * <-> symlink) between the lstat() and reading, then 611 * we don't want to report that as an error but rather 612 * try again starting with the lstat(). 613 * 614 * We'll keep a count of the retries, though, just to avoid 615 * any confusing situation sending us into an infinite loop. 616 */ 617 618if(remaining_retries-- <=0) 619goto out; 620 621if(lstat(path, &st) <0) { 622if(errno != ENOENT) 623goto out; 624if(resolve_packed_ref(refs, refname, sha1, type)) { 625 errno = ENOENT; 626goto out; 627} 628 ret =0; 629goto out; 630} 631 632/* Follow "normalized" - ie "refs/.." symlinks by hand */ 633if(S_ISLNK(st.st_mode)) { 634strbuf_reset(&sb_contents); 635if(strbuf_readlink(&sb_contents, path,0) <0) { 636if(errno == ENOENT || errno == EINVAL) 637/* inconsistent with lstat; retry */ 638goto stat_ref; 639else 640goto out; 641} 642if(starts_with(sb_contents.buf,"refs/") && 643!check_refname_format(sb_contents.buf,0)) { 644strbuf_swap(&sb_contents, referent); 645*type |= REF_ISSYMREF; 646 ret =0; 647goto out; 648} 649/* 650 * It doesn't look like a refname; fall through to just 651 * treating it like a non-symlink, and reading whatever it 652 * points to. 653 */ 654} 655 656/* Is it a directory? */ 657if(S_ISDIR(st.st_mode)) { 658/* 659 * Even though there is a directory where the loose 660 * ref is supposed to be, there could still be a 661 * packed ref: 662 */ 663if(resolve_packed_ref(refs, refname, sha1, type)) { 664 errno = EISDIR; 665goto out; 666} 667 ret =0; 668goto out; 669} 670 671/* 672 * Anything else, just open it and try to use it as 673 * a ref 674 */ 675 fd =open(path, O_RDONLY); 676if(fd <0) { 677if(errno == ENOENT && !S_ISLNK(st.st_mode)) 678/* inconsistent with lstat; retry */ 679goto stat_ref; 680else 681goto out; 682} 683strbuf_reset(&sb_contents); 684if(strbuf_read(&sb_contents, fd,256) <0) { 685int save_errno = errno; 686close(fd); 687 errno = save_errno; 688goto out; 689} 690close(fd); 691strbuf_rtrim(&sb_contents); 692 buf = sb_contents.buf; 693if(starts_with(buf,"ref:")) { 694 buf +=4; 695while(isspace(*buf)) 696 buf++; 697 698strbuf_reset(referent); 699strbuf_addstr(referent, buf); 700*type |= REF_ISSYMREF; 701 ret =0; 702goto out; 703} 704 705/* 706 * Please note that FETCH_HEAD has additional 707 * data after the sha. 708 */ 709if(get_sha1_hex(buf, sha1) || 710(buf[40] !='\0'&& !isspace(buf[40]))) { 711*type |= REF_ISBROKEN; 712 errno = EINVAL; 713goto out; 714} 715 716 ret =0; 717 718out: 719 save_errno = errno; 720strbuf_release(&sb_path); 721strbuf_release(&sb_contents); 722 errno = save_errno; 723return ret; 724} 725 726static voidunlock_ref(struct ref_lock *lock) 727{ 728/* Do not free lock->lk -- atexit() still looks at them */ 729if(lock->lk) 730rollback_lock_file(lock->lk); 731free(lock->ref_name); 732free(lock); 733} 734 735/* 736 * Lock refname, without following symrefs, and set *lock_p to point 737 * at a newly-allocated lock object. Fill in lock->old_oid, referent, 738 * and type similarly to read_raw_ref(). 739 * 740 * The caller must verify that refname is a "safe" reference name (in 741 * the sense of refname_is_safe()) before calling this function. 742 * 743 * If the reference doesn't already exist, verify that refname doesn't 744 * have a D/F conflict with any existing references. extras and skip 745 * are passed to refs_verify_refname_available() for this check. 746 * 747 * If mustexist is not set and the reference is not found or is 748 * broken, lock the reference anyway but clear sha1. 749 * 750 * Return 0 on success. On failure, write an error message to err and 751 * return TRANSACTION_NAME_CONFLICT or TRANSACTION_GENERIC_ERROR. 752 * 753 * Implementation note: This function is basically 754 * 755 * lock reference 756 * read_raw_ref() 757 * 758 * but it includes a lot more code to 759 * - Deal with possible races with other processes 760 * - Avoid calling refs_verify_refname_available() when it can be 761 * avoided, namely if we were successfully able to read the ref 762 * - Generate informative error messages in the case of failure 763 */ 764static intlock_raw_ref(struct files_ref_store *refs, 765const char*refname,int mustexist, 766const struct string_list *extras, 767const struct string_list *skip, 768struct ref_lock **lock_p, 769struct strbuf *referent, 770unsigned int*type, 771struct strbuf *err) 772{ 773struct ref_lock *lock; 774struct strbuf ref_file = STRBUF_INIT; 775int attempts_remaining =3; 776int ret = TRANSACTION_GENERIC_ERROR; 777 778assert(err); 779files_assert_main_repository(refs,"lock_raw_ref"); 780 781*type =0; 782 783/* First lock the file so it can't change out from under us. */ 784 785*lock_p = lock =xcalloc(1,sizeof(*lock)); 786 787 lock->ref_name =xstrdup(refname); 788files_ref_path(refs, &ref_file, refname); 789 790retry: 791switch(safe_create_leading_directories(ref_file.buf)) { 792case SCLD_OK: 793break;/* success */ 794case SCLD_EXISTS: 795/* 796 * Suppose refname is "refs/foo/bar". We just failed 797 * to create the containing directory, "refs/foo", 798 * because there was a non-directory in the way. This 799 * indicates a D/F conflict, probably because of 800 * another reference such as "refs/foo". There is no 801 * reason to expect this error to be transitory. 802 */ 803if(refs_verify_refname_available(&refs->base, refname, 804 extras, skip, err)) { 805if(mustexist) { 806/* 807 * To the user the relevant error is 808 * that the "mustexist" reference is 809 * missing: 810 */ 811strbuf_reset(err); 812strbuf_addf(err,"unable to resolve reference '%s'", 813 refname); 814}else{ 815/* 816 * The error message set by 817 * refs_verify_refname_available() is 818 * OK. 819 */ 820 ret = TRANSACTION_NAME_CONFLICT; 821} 822}else{ 823/* 824 * The file that is in the way isn't a loose 825 * reference. Report it as a low-level 826 * failure. 827 */ 828strbuf_addf(err,"unable to create lock file%s.lock; " 829"non-directory in the way", 830 ref_file.buf); 831} 832goto error_return; 833case SCLD_VANISHED: 834/* Maybe another process was tidying up. Try again. */ 835if(--attempts_remaining >0) 836goto retry; 837/* fall through */ 838default: 839strbuf_addf(err,"unable to create directory for%s", 840 ref_file.buf); 841goto error_return; 842} 843 844if(!lock->lk) 845 lock->lk =xcalloc(1,sizeof(struct lock_file)); 846 847if(hold_lock_file_for_update(lock->lk, ref_file.buf, LOCK_NO_DEREF) <0) { 848if(errno == ENOENT && --attempts_remaining >0) { 849/* 850 * Maybe somebody just deleted one of the 851 * directories leading to ref_file. Try 852 * again: 853 */ 854goto retry; 855}else{ 856unable_to_lock_message(ref_file.buf, errno, err); 857goto error_return; 858} 859} 860 861/* 862 * Now we hold the lock and can read the reference without 863 * fear that its value will change. 864 */ 865 866if(files_read_raw_ref(&refs->base, refname, 867 lock->old_oid.hash, referent, type)) { 868if(errno == ENOENT) { 869if(mustexist) { 870/* Garden variety missing reference. */ 871strbuf_addf(err,"unable to resolve reference '%s'", 872 refname); 873goto error_return; 874}else{ 875/* 876 * Reference is missing, but that's OK. We 877 * know that there is not a conflict with 878 * another loose reference because 879 * (supposing that we are trying to lock 880 * reference "refs/foo/bar"): 881 * 882 * - We were successfully able to create 883 * the lockfile refs/foo/bar.lock, so we 884 * know there cannot be a loose reference 885 * named "refs/foo". 886 * 887 * - We got ENOENT and not EISDIR, so we 888 * know that there cannot be a loose 889 * reference named "refs/foo/bar/baz". 890 */ 891} 892}else if(errno == EISDIR) { 893/* 894 * There is a directory in the way. It might have 895 * contained references that have been deleted. If 896 * we don't require that the reference already 897 * exists, try to remove the directory so that it 898 * doesn't cause trouble when we want to rename the 899 * lockfile into place later. 900 */ 901if(mustexist) { 902/* Garden variety missing reference. */ 903strbuf_addf(err,"unable to resolve reference '%s'", 904 refname); 905goto error_return; 906}else if(remove_dir_recursively(&ref_file, 907 REMOVE_DIR_EMPTY_ONLY)) { 908if(refs_verify_refname_available( 909&refs->base, refname, 910 extras, skip, err)) { 911/* 912 * The error message set by 913 * verify_refname_available() is OK. 914 */ 915 ret = TRANSACTION_NAME_CONFLICT; 916goto error_return; 917}else{ 918/* 919 * We can't delete the directory, 920 * but we also don't know of any 921 * references that it should 922 * contain. 923 */ 924strbuf_addf(err,"there is a non-empty directory '%s' " 925"blocking reference '%s'", 926 ref_file.buf, refname); 927goto error_return; 928} 929} 930}else if(errno == EINVAL && (*type & REF_ISBROKEN)) { 931strbuf_addf(err,"unable to resolve reference '%s': " 932"reference broken", refname); 933goto error_return; 934}else{ 935strbuf_addf(err,"unable to resolve reference '%s':%s", 936 refname,strerror(errno)); 937goto error_return; 938} 939 940/* 941 * If the ref did not exist and we are creating it, 942 * make sure there is no existing ref that conflicts 943 * with refname: 944 */ 945if(refs_verify_refname_available( 946&refs->base, refname, 947 extras, skip, err)) 948goto error_return; 949} 950 951 ret =0; 952goto out; 953 954error_return: 955unlock_ref(lock); 956*lock_p = NULL; 957 958out: 959strbuf_release(&ref_file); 960return ret; 961} 962 963static intfiles_peel_ref(struct ref_store *ref_store, 964const char*refname,unsigned char*sha1) 965{ 966struct files_ref_store *refs = 967files_downcast(ref_store, REF_STORE_READ | REF_STORE_ODB, 968"peel_ref"); 969int flag; 970unsigned char base[20]; 971 972if(current_ref_iter && current_ref_iter->refname == refname) { 973struct object_id peeled; 974 975if(ref_iterator_peel(current_ref_iter, &peeled)) 976return-1; 977hashcpy(sha1, peeled.hash); 978return0; 979} 980 981if(refs_read_ref_full(ref_store, refname, 982 RESOLVE_REF_READING, base, &flag)) 983return-1; 984 985/* 986 * If the reference is packed, read its ref_entry from the 987 * cache in the hope that we already know its peeled value. 988 * We only try this optimization on packed references because 989 * (a) forcing the filling of the loose reference cache could 990 * be expensive and (b) loose references anyway usually do not 991 * have REF_KNOWS_PEELED. 992 */ 993if(flag & REF_ISPACKED) { 994struct ref_entry *r =get_packed_ref(refs, refname); 995if(r) { 996if(peel_entry(r,0)) 997return-1; 998hashcpy(sha1, r->u.value.peeled.hash); 999return0;1000}1001}10021003returnpeel_object(base, sha1);1004}10051006struct files_ref_iterator {1007struct ref_iterator base;10081009struct packed_ref_cache *packed_ref_cache;1010struct ref_iterator *iter0;1011unsigned int flags;1012};10131014static intfiles_ref_iterator_advance(struct ref_iterator *ref_iterator)1015{1016struct files_ref_iterator *iter =1017(struct files_ref_iterator *)ref_iterator;1018int ok;10191020while((ok =ref_iterator_advance(iter->iter0)) == ITER_OK) {1021if(iter->flags & DO_FOR_EACH_PER_WORKTREE_ONLY &&1022ref_type(iter->iter0->refname) != REF_TYPE_PER_WORKTREE)1023continue;10241025if(!(iter->flags & DO_FOR_EACH_INCLUDE_BROKEN) &&1026!ref_resolves_to_object(iter->iter0->refname,1027 iter->iter0->oid,1028 iter->iter0->flags))1029continue;10301031 iter->base.refname = iter->iter0->refname;1032 iter->base.oid = iter->iter0->oid;1033 iter->base.flags = iter->iter0->flags;1034return ITER_OK;1035}10361037 iter->iter0 = NULL;1038if(ref_iterator_abort(ref_iterator) != ITER_DONE)1039 ok = ITER_ERROR;10401041return ok;1042}10431044static intfiles_ref_iterator_peel(struct ref_iterator *ref_iterator,1045struct object_id *peeled)1046{1047struct files_ref_iterator *iter =1048(struct files_ref_iterator *)ref_iterator;10491050returnref_iterator_peel(iter->iter0, peeled);1051}10521053static intfiles_ref_iterator_abort(struct ref_iterator *ref_iterator)1054{1055struct files_ref_iterator *iter =1056(struct files_ref_iterator *)ref_iterator;1057int ok = ITER_DONE;10581059if(iter->iter0)1060 ok =ref_iterator_abort(iter->iter0);10611062release_packed_ref_cache(iter->packed_ref_cache);1063base_ref_iterator_free(ref_iterator);1064return ok;1065}10661067static struct ref_iterator_vtable files_ref_iterator_vtable = {1068 files_ref_iterator_advance,1069 files_ref_iterator_peel,1070 files_ref_iterator_abort1071};10721073static struct ref_iterator *files_ref_iterator_begin(1074struct ref_store *ref_store,1075const char*prefix,unsigned int flags)1076{1077struct files_ref_store *refs;1078struct ref_iterator *loose_iter, *packed_iter;1079struct files_ref_iterator *iter;1080struct ref_iterator *ref_iterator;1081unsigned int required_flags = REF_STORE_READ;10821083if(!(flags & DO_FOR_EACH_INCLUDE_BROKEN))1084 required_flags |= REF_STORE_ODB;10851086 refs =files_downcast(ref_store, required_flags,"ref_iterator_begin");10871088 iter =xcalloc(1,sizeof(*iter));1089 ref_iterator = &iter->base;1090base_ref_iterator_init(ref_iterator, &files_ref_iterator_vtable);10911092/*1093 * We must make sure that all loose refs are read before1094 * accessing the packed-refs file; this avoids a race1095 * condition if loose refs are migrated to the packed-refs1096 * file by a simultaneous process, but our in-memory view is1097 * from before the migration. We ensure this as follows:1098 * First, we call start the loose refs iteration with its1099 * `prime_ref` argument set to true. This causes the loose1100 * references in the subtree to be pre-read into the cache.1101 * (If they've already been read, that's OK; we only need to1102 * guarantee that they're read before the packed refs, not1103 * *how much* before.) After that, we call1104 * get_packed_ref_cache(), which internally checks whether the1105 * packed-ref cache is up to date with what is on disk, and1106 * re-reads it if not.1107 */11081109 loose_iter =cache_ref_iterator_begin(get_loose_ref_cache(refs),1110 prefix,1);11111112 iter->packed_ref_cache =get_packed_ref_cache(refs);1113acquire_packed_ref_cache(iter->packed_ref_cache);1114 packed_iter =cache_ref_iterator_begin(iter->packed_ref_cache->cache,1115 prefix,0);11161117 iter->iter0 =overlay_ref_iterator_begin(loose_iter, packed_iter);1118 iter->flags = flags;11191120return ref_iterator;1121}11221123/*1124 * Verify that the reference locked by lock has the value old_sha1.1125 * Fail if the reference doesn't exist and mustexist is set. Return 01126 * on success. On error, write an error message to err, set errno, and1127 * return a negative value.1128 */1129static intverify_lock(struct ref_store *ref_store,struct ref_lock *lock,1130const unsigned char*old_sha1,int mustexist,1131struct strbuf *err)1132{1133assert(err);11341135if(refs_read_ref_full(ref_store, lock->ref_name,1136 mustexist ? RESOLVE_REF_READING :0,1137 lock->old_oid.hash, NULL)) {1138if(old_sha1) {1139int save_errno = errno;1140strbuf_addf(err,"can't verify ref '%s'", lock->ref_name);1141 errno = save_errno;1142return-1;1143}else{1144oidclr(&lock->old_oid);1145return0;1146}1147}1148if(old_sha1 &&hashcmp(lock->old_oid.hash, old_sha1)) {1149strbuf_addf(err,"ref '%s' is at%sbut expected%s",1150 lock->ref_name,1151oid_to_hex(&lock->old_oid),1152sha1_to_hex(old_sha1));1153 errno = EBUSY;1154return-1;1155}1156return0;1157}11581159static intremove_empty_directories(struct strbuf *path)1160{1161/*1162 * we want to create a file but there is a directory there;1163 * if that is an empty directory (or a directory that contains1164 * only empty directories), remove them.1165 */1166returnremove_dir_recursively(path, REMOVE_DIR_EMPTY_ONLY);1167}11681169static intcreate_reflock(const char*path,void*cb)1170{1171struct lock_file *lk = cb;11721173returnhold_lock_file_for_update(lk, path, LOCK_NO_DEREF) <0? -1:0;1174}11751176/*1177 * Locks a ref returning the lock on success and NULL on failure.1178 * On failure errno is set to something meaningful.1179 */1180static struct ref_lock *lock_ref_sha1_basic(struct files_ref_store *refs,1181const char*refname,1182const unsigned char*old_sha1,1183const struct string_list *extras,1184const struct string_list *skip,1185unsigned int flags,int*type,1186struct strbuf *err)1187{1188struct strbuf ref_file = STRBUF_INIT;1189struct ref_lock *lock;1190int last_errno =0;1191int mustexist = (old_sha1 && !is_null_sha1(old_sha1));1192int resolve_flags = RESOLVE_REF_NO_RECURSE;1193int resolved;11941195files_assert_main_repository(refs,"lock_ref_sha1_basic");1196assert(err);11971198 lock =xcalloc(1,sizeof(struct ref_lock));11991200if(mustexist)1201 resolve_flags |= RESOLVE_REF_READING;1202if(flags & REF_DELETING)1203 resolve_flags |= RESOLVE_REF_ALLOW_BAD_NAME;12041205files_ref_path(refs, &ref_file, refname);1206 resolved = !!refs_resolve_ref_unsafe(&refs->base,1207 refname, resolve_flags,1208 lock->old_oid.hash, type);1209if(!resolved && errno == EISDIR) {1210/*1211 * we are trying to lock foo but we used to1212 * have foo/bar which now does not exist;1213 * it is normal for the empty directory 'foo'1214 * to remain.1215 */1216if(remove_empty_directories(&ref_file)) {1217 last_errno = errno;1218if(!refs_verify_refname_available(1219&refs->base,1220 refname, extras, skip, err))1221strbuf_addf(err,"there are still refs under '%s'",1222 refname);1223goto error_return;1224}1225 resolved = !!refs_resolve_ref_unsafe(&refs->base,1226 refname, resolve_flags,1227 lock->old_oid.hash, type);1228}1229if(!resolved) {1230 last_errno = errno;1231if(last_errno != ENOTDIR ||1232!refs_verify_refname_available(&refs->base, refname,1233 extras, skip, err))1234strbuf_addf(err,"unable to resolve reference '%s':%s",1235 refname,strerror(last_errno));12361237goto error_return;1238}12391240/*1241 * If the ref did not exist and we are creating it, make sure1242 * there is no existing packed ref whose name begins with our1243 * refname, nor a packed ref whose name is a proper prefix of1244 * our refname.1245 */1246if(is_null_oid(&lock->old_oid) &&1247refs_verify_refname_available(&refs->base, refname,1248 extras, skip, err)) {1249 last_errno = ENOTDIR;1250goto error_return;1251}12521253 lock->lk =xcalloc(1,sizeof(struct lock_file));12541255 lock->ref_name =xstrdup(refname);12561257if(raceproof_create_file(ref_file.buf, create_reflock, lock->lk)) {1258 last_errno = errno;1259unable_to_lock_message(ref_file.buf, errno, err);1260goto error_return;1261}12621263if(verify_lock(&refs->base, lock, old_sha1, mustexist, err)) {1264 last_errno = errno;1265goto error_return;1266}1267goto out;12681269 error_return:1270unlock_ref(lock);1271 lock = NULL;12721273 out:1274strbuf_release(&ref_file);1275 errno = last_errno;1276return lock;1277}12781279/*1280 * Write an entry to the packed-refs file for the specified refname.1281 * If peeled is non-NULL, write it as the entry's peeled value.1282 */1283static voidwrite_packed_entry(FILE*fh,const char*refname,1284const unsigned char*sha1,1285const unsigned char*peeled)1286{1287fprintf_or_die(fh,"%s %s\n",sha1_to_hex(sha1), refname);1288if(peeled)1289fprintf_or_die(fh,"^%s\n",sha1_to_hex(peeled));1290}12911292/*1293 * Lock the packed-refs file for writing. Flags is passed to1294 * hold_lock_file_for_update(). Return 0 on success. On errors, set1295 * errno appropriately and return a nonzero value.1296 */1297static intlock_packed_refs(struct files_ref_store *refs,int flags)1298{1299static int timeout_configured =0;1300static int timeout_value =1000;1301struct packed_ref_cache *packed_ref_cache;13021303files_assert_main_repository(refs,"lock_packed_refs");13041305if(!timeout_configured) {1306git_config_get_int("core.packedrefstimeout", &timeout_value);1307 timeout_configured =1;1308}13091310if(hold_lock_file_for_update_timeout(1311&refs->packed_refs_lock,files_packed_refs_path(refs),1312 flags, timeout_value) <0)1313return-1;1314/*1315 * Get the current packed-refs while holding the lock. It is1316 * important that we call `get_packed_ref_cache()` before1317 * setting `packed_ref_cache->lock`, because otherwise the1318 * former will see that the file is locked and assume that the1319 * cache can't be stale.1320 */1321 packed_ref_cache =get_packed_ref_cache(refs);1322/* Increment the reference count to prevent it from being freed: */1323acquire_packed_ref_cache(packed_ref_cache);1324return0;1325}13261327/*1328 * Write the current version of the packed refs cache from memory to1329 * disk. The packed-refs file must already be locked for writing (see1330 * lock_packed_refs()). Return zero on success. On errors, set errno1331 * and return a nonzero value1332 */1333static intcommit_packed_refs(struct files_ref_store *refs)1334{1335struct packed_ref_cache *packed_ref_cache =1336get_packed_ref_cache(refs);1337int ok, error =0;1338int save_errno =0;1339FILE*out;1340struct ref_iterator *iter;13411342files_assert_main_repository(refs,"commit_packed_refs");13431344if(!is_lock_file_locked(&refs->packed_refs_lock))1345die("BUG: packed-refs not locked");13461347 out =fdopen_lock_file(&refs->packed_refs_lock,"w");1348if(!out)1349die_errno("unable to fdopen packed-refs descriptor");13501351fprintf_or_die(out,"%s", PACKED_REFS_HEADER);13521353 iter =cache_ref_iterator_begin(packed_ref_cache->cache, NULL,0);1354while((ok =ref_iterator_advance(iter)) == ITER_OK) {1355struct object_id peeled;1356int peel_error =ref_iterator_peel(iter, &peeled);13571358write_packed_entry(out, iter->refname, iter->oid->hash,1359 peel_error ? NULL : peeled.hash);1360}13611362if(ok != ITER_DONE)1363die("error while iterating over references");13641365if(commit_lock_file(&refs->packed_refs_lock)) {1366 save_errno = errno;1367 error = -1;1368}1369release_packed_ref_cache(packed_ref_cache);1370 errno = save_errno;1371return error;1372}13731374/*1375 * Rollback the lockfile for the packed-refs file, and discard the1376 * in-memory packed reference cache. (The packed-refs file will be1377 * read anew if it is needed again after this function is called.)1378 */1379static voidrollback_packed_refs(struct files_ref_store *refs)1380{1381struct packed_ref_cache *packed_ref_cache =1382get_packed_ref_cache(refs);13831384files_assert_main_repository(refs,"rollback_packed_refs");13851386if(!is_lock_file_locked(&refs->packed_refs_lock))1387die("BUG: packed-refs not locked");1388rollback_lock_file(&refs->packed_refs_lock);1389release_packed_ref_cache(packed_ref_cache);1390clear_packed_ref_cache(refs);1391}13921393struct ref_to_prune {1394struct ref_to_prune *next;1395unsigned char sha1[20];1396char name[FLEX_ARRAY];1397};13981399enum{1400 REMOVE_EMPTY_PARENTS_REF =0x01,1401 REMOVE_EMPTY_PARENTS_REFLOG =0x021402};14031404/*1405 * Remove empty parent directories associated with the specified1406 * reference and/or its reflog, but spare [logs/]refs/ and immediate1407 * subdirs. flags is a combination of REMOVE_EMPTY_PARENTS_REF and/or1408 * REMOVE_EMPTY_PARENTS_REFLOG.1409 */1410static voidtry_remove_empty_parents(struct files_ref_store *refs,1411const char*refname,1412unsigned int flags)1413{1414struct strbuf buf = STRBUF_INIT;1415struct strbuf sb = STRBUF_INIT;1416char*p, *q;1417int i;14181419strbuf_addstr(&buf, refname);1420 p = buf.buf;1421for(i =0; i <2; i++) {/* refs/{heads,tags,...}/ */1422while(*p && *p !='/')1423 p++;1424/* tolerate duplicate slashes; see check_refname_format() */1425while(*p =='/')1426 p++;1427}1428 q = buf.buf + buf.len;1429while(flags & (REMOVE_EMPTY_PARENTS_REF | REMOVE_EMPTY_PARENTS_REFLOG)) {1430while(q > p && *q !='/')1431 q--;1432while(q > p && *(q-1) =='/')1433 q--;1434if(q == p)1435break;1436strbuf_setlen(&buf, q - buf.buf);14371438strbuf_reset(&sb);1439files_ref_path(refs, &sb, buf.buf);1440if((flags & REMOVE_EMPTY_PARENTS_REF) &&rmdir(sb.buf))1441 flags &= ~REMOVE_EMPTY_PARENTS_REF;14421443strbuf_reset(&sb);1444files_reflog_path(refs, &sb, buf.buf);1445if((flags & REMOVE_EMPTY_PARENTS_REFLOG) &&rmdir(sb.buf))1446 flags &= ~REMOVE_EMPTY_PARENTS_REFLOG;1447}1448strbuf_release(&buf);1449strbuf_release(&sb);1450}14511452/* make sure nobody touched the ref, and unlink */1453static voidprune_ref(struct files_ref_store *refs,struct ref_to_prune *r)1454{1455struct ref_transaction *transaction;1456struct strbuf err = STRBUF_INIT;14571458if(check_refname_format(r->name,0))1459return;14601461 transaction =ref_store_transaction_begin(&refs->base, &err);1462if(!transaction ||1463ref_transaction_delete(transaction, r->name, r->sha1,1464 REF_ISPRUNING | REF_NODEREF, NULL, &err) ||1465ref_transaction_commit(transaction, &err)) {1466ref_transaction_free(transaction);1467error("%s", err.buf);1468strbuf_release(&err);1469return;1470}1471ref_transaction_free(transaction);1472strbuf_release(&err);1473}14741475static voidprune_refs(struct files_ref_store *refs,struct ref_to_prune *r)1476{1477while(r) {1478prune_ref(refs, r);1479 r = r->next;1480}1481}14821483/*1484 * Return true if the specified reference should be packed.1485 */1486static intshould_pack_ref(const char*refname,1487const struct object_id *oid,unsigned int ref_flags,1488unsigned int pack_flags)1489{1490/* Do not pack per-worktree refs: */1491if(ref_type(refname) != REF_TYPE_NORMAL)1492return0;14931494/* Do not pack non-tags unless PACK_REFS_ALL is set: */1495if(!(pack_flags & PACK_REFS_ALL) && !starts_with(refname,"refs/tags/"))1496return0;14971498/* Do not pack symbolic refs: */1499if(ref_flags & REF_ISSYMREF)1500return0;15011502/* Do not pack broken refs: */1503if(!ref_resolves_to_object(refname, oid, ref_flags))1504return0;15051506return1;1507}15081509static intfiles_pack_refs(struct ref_store *ref_store,unsigned int flags)1510{1511struct files_ref_store *refs =1512files_downcast(ref_store, REF_STORE_WRITE | REF_STORE_ODB,1513"pack_refs");1514struct ref_iterator *iter;1515struct ref_dir *packed_refs;1516int ok;1517struct ref_to_prune *refs_to_prune = NULL;15181519lock_packed_refs(refs, LOCK_DIE_ON_ERROR);1520 packed_refs =get_packed_refs(refs);15211522 iter =cache_ref_iterator_begin(get_loose_ref_cache(refs), NULL,0);1523while((ok =ref_iterator_advance(iter)) == ITER_OK) {1524/*1525 * If the loose reference can be packed, add an entry1526 * in the packed ref cache. If the reference should be1527 * pruned, also add it to refs_to_prune.1528 */1529struct ref_entry *packed_entry;15301531if(!should_pack_ref(iter->refname, iter->oid, iter->flags,1532 flags))1533continue;15341535/*1536 * Create an entry in the packed-refs cache equivalent1537 * to the one from the loose ref cache, except that1538 * we don't copy the peeled status, because we want it1539 * to be re-peeled.1540 */1541 packed_entry =find_ref_entry(packed_refs, iter->refname);1542if(packed_entry) {1543/* Overwrite existing packed entry with info from loose entry */1544 packed_entry->flag = REF_ISPACKED;1545oidcpy(&packed_entry->u.value.oid, iter->oid);1546}else{1547 packed_entry =create_ref_entry(iter->refname, iter->oid,1548 REF_ISPACKED);1549add_ref_entry(packed_refs, packed_entry);1550}1551oidclr(&packed_entry->u.value.peeled);15521553/* Schedule the loose reference for pruning if requested. */1554if((flags & PACK_REFS_PRUNE)) {1555struct ref_to_prune *n;1556FLEX_ALLOC_STR(n, name, iter->refname);1557hashcpy(n->sha1, iter->oid->hash);1558 n->next = refs_to_prune;1559 refs_to_prune = n;1560}1561}1562if(ok != ITER_DONE)1563die("error while iterating over references");15641565if(commit_packed_refs(refs))1566die_errno("unable to overwrite old ref-pack file");15671568prune_refs(refs, refs_to_prune);1569return0;1570}15711572/*1573 * Rewrite the packed-refs file, omitting any refs listed in1574 * 'refnames'. On error, leave packed-refs unchanged, write an error1575 * message to 'err', and return a nonzero value.1576 *1577 * The refs in 'refnames' needn't be sorted. `err` must not be NULL.1578 */1579static intrepack_without_refs(struct files_ref_store *refs,1580struct string_list *refnames,struct strbuf *err)1581{1582struct ref_dir *packed;1583struct string_list_item *refname;1584int ret, needs_repacking =0, removed =0;15851586files_assert_main_repository(refs,"repack_without_refs");1587assert(err);15881589/* Look for a packed ref */1590for_each_string_list_item(refname, refnames) {1591if(get_packed_ref(refs, refname->string)) {1592 needs_repacking =1;1593break;1594}1595}15961597/* Avoid locking if we have nothing to do */1598if(!needs_repacking)1599return0;/* no refname exists in packed refs */16001601if(lock_packed_refs(refs,0)) {1602unable_to_lock_message(files_packed_refs_path(refs), errno, err);1603return-1;1604}1605 packed =get_packed_refs(refs);16061607/* Remove refnames from the cache */1608for_each_string_list_item(refname, refnames)1609if(remove_entry_from_dir(packed, refname->string) != -1)1610 removed =1;1611if(!removed) {1612/*1613 * All packed entries disappeared while we were1614 * acquiring the lock.1615 */1616rollback_packed_refs(refs);1617return0;1618}16191620/* Write what remains */1621 ret =commit_packed_refs(refs);1622if(ret)1623strbuf_addf(err,"unable to overwrite old ref-pack file:%s",1624strerror(errno));1625return ret;1626}16271628static intfiles_delete_refs(struct ref_store *ref_store,const char*msg,1629struct string_list *refnames,unsigned int flags)1630{1631struct files_ref_store *refs =1632files_downcast(ref_store, REF_STORE_WRITE,"delete_refs");1633struct strbuf err = STRBUF_INIT;1634int i, result =0;16351636if(!refnames->nr)1637return0;16381639 result =repack_without_refs(refs, refnames, &err);1640if(result) {1641/*1642 * If we failed to rewrite the packed-refs file, then1643 * it is unsafe to try to remove loose refs, because1644 * doing so might expose an obsolete packed value for1645 * a reference that might even point at an object that1646 * has been garbage collected.1647 */1648if(refnames->nr ==1)1649error(_("could not delete reference%s:%s"),1650 refnames->items[0].string, err.buf);1651else1652error(_("could not delete references:%s"), err.buf);16531654goto out;1655}16561657for(i =0; i < refnames->nr; i++) {1658const char*refname = refnames->items[i].string;16591660if(refs_delete_ref(&refs->base, msg, refname, NULL, flags))1661 result |=error(_("could not remove reference%s"), refname);1662}16631664out:1665strbuf_release(&err);1666return result;1667}16681669/*1670 * People using contrib's git-new-workdir have .git/logs/refs ->1671 * /some/other/path/.git/logs/refs, and that may live on another device.1672 *1673 * IOW, to avoid cross device rename errors, the temporary renamed log must1674 * live into logs/refs.1675 */1676#define TMP_RENAMED_LOG"refs/.tmp-renamed-log"16771678struct rename_cb {1679const char*tmp_renamed_log;1680int true_errno;1681};16821683static intrename_tmp_log_callback(const char*path,void*cb_data)1684{1685struct rename_cb *cb = cb_data;16861687if(rename(cb->tmp_renamed_log, path)) {1688/*1689 * rename(a, b) when b is an existing directory ought1690 * to result in ISDIR, but Solaris 5.8 gives ENOTDIR.1691 * Sheesh. Record the true errno for error reporting,1692 * but report EISDIR to raceproof_create_file() so1693 * that it knows to retry.1694 */1695 cb->true_errno = errno;1696if(errno == ENOTDIR)1697 errno = EISDIR;1698return-1;1699}else{1700return0;1701}1702}17031704static intrename_tmp_log(struct files_ref_store *refs,const char*newrefname)1705{1706struct strbuf path = STRBUF_INIT;1707struct strbuf tmp = STRBUF_INIT;1708struct rename_cb cb;1709int ret;17101711files_reflog_path(refs, &path, newrefname);1712files_reflog_path(refs, &tmp, TMP_RENAMED_LOG);1713 cb.tmp_renamed_log = tmp.buf;1714 ret =raceproof_create_file(path.buf, rename_tmp_log_callback, &cb);1715if(ret) {1716if(errno == EISDIR)1717error("directory not empty:%s", path.buf);1718else1719error("unable to move logfile%sto%s:%s",1720 tmp.buf, path.buf,1721strerror(cb.true_errno));1722}17231724strbuf_release(&path);1725strbuf_release(&tmp);1726return ret;1727}17281729static intwrite_ref_to_lockfile(struct ref_lock *lock,1730const struct object_id *oid,struct strbuf *err);1731static intcommit_ref_update(struct files_ref_store *refs,1732struct ref_lock *lock,1733const struct object_id *oid,const char*logmsg,1734struct strbuf *err);17351736static intfiles_rename_ref(struct ref_store *ref_store,1737const char*oldrefname,const char*newrefname,1738const char*logmsg)1739{1740struct files_ref_store *refs =1741files_downcast(ref_store, REF_STORE_WRITE,"rename_ref");1742struct object_id oid, orig_oid;1743int flag =0, logmoved =0;1744struct ref_lock *lock;1745struct stat loginfo;1746struct strbuf sb_oldref = STRBUF_INIT;1747struct strbuf sb_newref = STRBUF_INIT;1748struct strbuf tmp_renamed_log = STRBUF_INIT;1749int log, ret;1750struct strbuf err = STRBUF_INIT;17511752files_reflog_path(refs, &sb_oldref, oldrefname);1753files_reflog_path(refs, &sb_newref, newrefname);1754files_reflog_path(refs, &tmp_renamed_log, TMP_RENAMED_LOG);17551756 log = !lstat(sb_oldref.buf, &loginfo);1757if(log &&S_ISLNK(loginfo.st_mode)) {1758 ret =error("reflog for%sis a symlink", oldrefname);1759goto out;1760}17611762if(!refs_resolve_ref_unsafe(&refs->base, oldrefname,1763 RESOLVE_REF_READING | RESOLVE_REF_NO_RECURSE,1764 orig_oid.hash, &flag)) {1765 ret =error("refname%snot found", oldrefname);1766goto out;1767}17681769if(flag & REF_ISSYMREF) {1770 ret =error("refname%sis a symbolic ref, renaming it is not supported",1771 oldrefname);1772goto out;1773}1774if(!refs_rename_ref_available(&refs->base, oldrefname, newrefname)) {1775 ret =1;1776goto out;1777}17781779if(log &&rename(sb_oldref.buf, tmp_renamed_log.buf)) {1780 ret =error("unable to move logfile logs/%sto logs/"TMP_RENAMED_LOG":%s",1781 oldrefname,strerror(errno));1782goto out;1783}17841785if(refs_delete_ref(&refs->base, logmsg, oldrefname,1786 orig_oid.hash, REF_NODEREF)) {1787error("unable to delete old%s", oldrefname);1788goto rollback;1789}17901791/*1792 * Since we are doing a shallow lookup, oid is not the1793 * correct value to pass to delete_ref as old_oid. But that1794 * doesn't matter, because an old_oid check wouldn't add to1795 * the safety anyway; we want to delete the reference whatever1796 * its current value.1797 */1798if(!refs_read_ref_full(&refs->base, newrefname,1799 RESOLVE_REF_READING | RESOLVE_REF_NO_RECURSE,1800 oid.hash, NULL) &&1801refs_delete_ref(&refs->base, NULL, newrefname,1802 NULL, REF_NODEREF)) {1803if(errno == EISDIR) {1804struct strbuf path = STRBUF_INIT;1805int result;18061807files_ref_path(refs, &path, newrefname);1808 result =remove_empty_directories(&path);1809strbuf_release(&path);18101811if(result) {1812error("Directory not empty:%s", newrefname);1813goto rollback;1814}1815}else{1816error("unable to delete existing%s", newrefname);1817goto rollback;1818}1819}18201821if(log &&rename_tmp_log(refs, newrefname))1822goto rollback;18231824 logmoved = log;18251826 lock =lock_ref_sha1_basic(refs, newrefname, NULL, NULL, NULL,1827 REF_NODEREF, NULL, &err);1828if(!lock) {1829error("unable to rename '%s' to '%s':%s", oldrefname, newrefname, err.buf);1830strbuf_release(&err);1831goto rollback;1832}1833oidcpy(&lock->old_oid, &orig_oid);18341835if(write_ref_to_lockfile(lock, &orig_oid, &err) ||1836commit_ref_update(refs, lock, &orig_oid, logmsg, &err)) {1837error("unable to write current sha1 into%s:%s", newrefname, err.buf);1838strbuf_release(&err);1839goto rollback;1840}18411842 ret =0;1843goto out;18441845 rollback:1846 lock =lock_ref_sha1_basic(refs, oldrefname, NULL, NULL, NULL,1847 REF_NODEREF, NULL, &err);1848if(!lock) {1849error("unable to lock%sfor rollback:%s", oldrefname, err.buf);1850strbuf_release(&err);1851goto rollbacklog;1852}18531854 flag = log_all_ref_updates;1855 log_all_ref_updates = LOG_REFS_NONE;1856if(write_ref_to_lockfile(lock, &orig_oid, &err) ||1857commit_ref_update(refs, lock, &orig_oid, NULL, &err)) {1858error("unable to write current sha1 into%s:%s", oldrefname, err.buf);1859strbuf_release(&err);1860}1861 log_all_ref_updates = flag;18621863 rollbacklog:1864if(logmoved &&rename(sb_newref.buf, sb_oldref.buf))1865error("unable to restore logfile%sfrom%s:%s",1866 oldrefname, newrefname,strerror(errno));1867if(!logmoved && log &&1868rename(tmp_renamed_log.buf, sb_oldref.buf))1869error("unable to restore logfile%sfrom logs/"TMP_RENAMED_LOG":%s",1870 oldrefname,strerror(errno));1871 ret =1;1872 out:1873strbuf_release(&sb_newref);1874strbuf_release(&sb_oldref);1875strbuf_release(&tmp_renamed_log);18761877return ret;1878}18791880static intclose_ref(struct ref_lock *lock)1881{1882if(close_lock_file(lock->lk))1883return-1;1884return0;1885}18861887static intcommit_ref(struct ref_lock *lock)1888{1889char*path =get_locked_file_path(lock->lk);1890struct stat st;18911892if(!lstat(path, &st) &&S_ISDIR(st.st_mode)) {1893/*1894 * There is a directory at the path we want to rename1895 * the lockfile to. Hopefully it is empty; try to1896 * delete it.1897 */1898size_t len =strlen(path);1899struct strbuf sb_path = STRBUF_INIT;19001901strbuf_attach(&sb_path, path, len, len);19021903/*1904 * If this fails, commit_lock_file() will also fail1905 * and will report the problem.1906 */1907remove_empty_directories(&sb_path);1908strbuf_release(&sb_path);1909}else{1910free(path);1911}19121913if(commit_lock_file(lock->lk))1914return-1;1915return0;1916}19171918static intopen_or_create_logfile(const char*path,void*cb)1919{1920int*fd = cb;19211922*fd =open(path, O_APPEND | O_WRONLY | O_CREAT,0666);1923return(*fd <0) ? -1:0;1924}19251926/*1927 * Create a reflog for a ref. If force_create = 0, only create the1928 * reflog for certain refs (those for which should_autocreate_reflog1929 * returns non-zero). Otherwise, create it regardless of the reference1930 * name. If the logfile already existed or was created, return 0 and1931 * set *logfd to the file descriptor opened for appending to the file.1932 * If no logfile exists and we decided not to create one, return 0 and1933 * set *logfd to -1. On failure, fill in *err, set *logfd to -1, and1934 * return -1.1935 */1936static intlog_ref_setup(struct files_ref_store *refs,1937const char*refname,int force_create,1938int*logfd,struct strbuf *err)1939{1940struct strbuf logfile_sb = STRBUF_INIT;1941char*logfile;19421943files_reflog_path(refs, &logfile_sb, refname);1944 logfile =strbuf_detach(&logfile_sb, NULL);19451946if(force_create ||should_autocreate_reflog(refname)) {1947if(raceproof_create_file(logfile, open_or_create_logfile, logfd)) {1948if(errno == ENOENT)1949strbuf_addf(err,"unable to create directory for '%s': "1950"%s", logfile,strerror(errno));1951else if(errno == EISDIR)1952strbuf_addf(err,"there are still logs under '%s'",1953 logfile);1954else1955strbuf_addf(err,"unable to append to '%s':%s",1956 logfile,strerror(errno));19571958goto error;1959}1960}else{1961*logfd =open(logfile, O_APPEND | O_WRONLY,0666);1962if(*logfd <0) {1963if(errno == ENOENT || errno == EISDIR) {1964/*1965 * The logfile doesn't already exist,1966 * but that is not an error; it only1967 * means that we won't write log1968 * entries to it.1969 */1970;1971}else{1972strbuf_addf(err,"unable to append to '%s':%s",1973 logfile,strerror(errno));1974goto error;1975}1976}1977}19781979if(*logfd >=0)1980adjust_shared_perm(logfile);19811982free(logfile);1983return0;19841985error:1986free(logfile);1987return-1;1988}19891990static intfiles_create_reflog(struct ref_store *ref_store,1991const char*refname,int force_create,1992struct strbuf *err)1993{1994struct files_ref_store *refs =1995files_downcast(ref_store, REF_STORE_WRITE,"create_reflog");1996int fd;19971998if(log_ref_setup(refs, refname, force_create, &fd, err))1999return-1;20002001if(fd >=0)2002close(fd);20032004return0;2005}20062007static intlog_ref_write_fd(int fd,const struct object_id *old_oid,2008const struct object_id *new_oid,2009const char*committer,const char*msg)2010{2011int msglen, written;2012unsigned maxlen, len;2013char*logrec;20142015 msglen = msg ?strlen(msg) :0;2016 maxlen =strlen(committer) + msglen +100;2017 logrec =xmalloc(maxlen);2018 len =xsnprintf(logrec, maxlen,"%s %s %s\n",2019oid_to_hex(old_oid),2020oid_to_hex(new_oid),2021 committer);2022if(msglen)2023 len +=copy_reflog_msg(logrec + len -1, msg) -1;20242025 written = len <= maxlen ?write_in_full(fd, logrec, len) : -1;2026free(logrec);2027if(written != len)2028return-1;20292030return0;2031}20322033static intfiles_log_ref_write(struct files_ref_store *refs,2034const char*refname,const struct object_id *old_oid,2035const struct object_id *new_oid,const char*msg,2036int flags,struct strbuf *err)2037{2038int logfd, result;20392040if(log_all_ref_updates == LOG_REFS_UNSET)2041 log_all_ref_updates =is_bare_repository() ? LOG_REFS_NONE : LOG_REFS_NORMAL;20422043 result =log_ref_setup(refs, refname,2044 flags & REF_FORCE_CREATE_REFLOG,2045&logfd, err);20462047if(result)2048return result;20492050if(logfd <0)2051return0;2052 result =log_ref_write_fd(logfd, old_oid, new_oid,2053git_committer_info(0), msg);2054if(result) {2055struct strbuf sb = STRBUF_INIT;2056int save_errno = errno;20572058files_reflog_path(refs, &sb, refname);2059strbuf_addf(err,"unable to append to '%s':%s",2060 sb.buf,strerror(save_errno));2061strbuf_release(&sb);2062close(logfd);2063return-1;2064}2065if(close(logfd)) {2066struct strbuf sb = STRBUF_INIT;2067int save_errno = errno;20682069files_reflog_path(refs, &sb, refname);2070strbuf_addf(err,"unable to append to '%s':%s",2071 sb.buf,strerror(save_errno));2072strbuf_release(&sb);2073return-1;2074}2075return0;2076}20772078/*2079 * Write sha1 into the open lockfile, then close the lockfile. On2080 * errors, rollback the lockfile, fill in *err and2081 * return -1.2082 */2083static intwrite_ref_to_lockfile(struct ref_lock *lock,2084const struct object_id *oid,struct strbuf *err)2085{2086static char term ='\n';2087struct object *o;2088int fd;20892090 o =parse_object(oid);2091if(!o) {2092strbuf_addf(err,2093"trying to write ref '%s' with nonexistent object%s",2094 lock->ref_name,oid_to_hex(oid));2095unlock_ref(lock);2096return-1;2097}2098if(o->type != OBJ_COMMIT &&is_branch(lock->ref_name)) {2099strbuf_addf(err,2100"trying to write non-commit object%sto branch '%s'",2101oid_to_hex(oid), lock->ref_name);2102unlock_ref(lock);2103return-1;2104}2105 fd =get_lock_file_fd(lock->lk);2106if(write_in_full(fd,oid_to_hex(oid), GIT_SHA1_HEXSZ) != GIT_SHA1_HEXSZ ||2107write_in_full(fd, &term,1) !=1||2108close_ref(lock) <0) {2109strbuf_addf(err,2110"couldn't write '%s'",get_lock_file_path(lock->lk));2111unlock_ref(lock);2112return-1;2113}2114return0;2115}21162117/*2118 * Commit a change to a loose reference that has already been written2119 * to the loose reference lockfile. Also update the reflogs if2120 * necessary, using the specified lockmsg (which can be NULL).2121 */2122static intcommit_ref_update(struct files_ref_store *refs,2123struct ref_lock *lock,2124const struct object_id *oid,const char*logmsg,2125struct strbuf *err)2126{2127files_assert_main_repository(refs,"commit_ref_update");21282129clear_loose_ref_cache(refs);2130if(files_log_ref_write(refs, lock->ref_name,2131&lock->old_oid, oid,2132 logmsg,0, err)) {2133char*old_msg =strbuf_detach(err, NULL);2134strbuf_addf(err,"cannot update the ref '%s':%s",2135 lock->ref_name, old_msg);2136free(old_msg);2137unlock_ref(lock);2138return-1;2139}21402141if(strcmp(lock->ref_name,"HEAD") !=0) {2142/*2143 * Special hack: If a branch is updated directly and HEAD2144 * points to it (may happen on the remote side of a push2145 * for example) then logically the HEAD reflog should be2146 * updated too.2147 * A generic solution implies reverse symref information,2148 * but finding all symrefs pointing to the given branch2149 * would be rather costly for this rare event (the direct2150 * update of a branch) to be worth it. So let's cheat and2151 * check with HEAD only which should cover 99% of all usage2152 * scenarios (even 100% of the default ones).2153 */2154struct object_id head_oid;2155int head_flag;2156const char*head_ref;21572158 head_ref =refs_resolve_ref_unsafe(&refs->base,"HEAD",2159 RESOLVE_REF_READING,2160 head_oid.hash, &head_flag);2161if(head_ref && (head_flag & REF_ISSYMREF) &&2162!strcmp(head_ref, lock->ref_name)) {2163struct strbuf log_err = STRBUF_INIT;2164if(files_log_ref_write(refs,"HEAD",2165&lock->old_oid, oid,2166 logmsg,0, &log_err)) {2167error("%s", log_err.buf);2168strbuf_release(&log_err);2169}2170}2171}21722173if(commit_ref(lock)) {2174strbuf_addf(err,"couldn't set '%s'", lock->ref_name);2175unlock_ref(lock);2176return-1;2177}21782179unlock_ref(lock);2180return0;2181}21822183static intcreate_ref_symlink(struct ref_lock *lock,const char*target)2184{2185int ret = -1;2186#ifndef NO_SYMLINK_HEAD2187char*ref_path =get_locked_file_path(lock->lk);2188unlink(ref_path);2189 ret =symlink(target, ref_path);2190free(ref_path);21912192if(ret)2193fprintf(stderr,"no symlink - falling back to symbolic ref\n");2194#endif2195return ret;2196}21972198static voidupdate_symref_reflog(struct files_ref_store *refs,2199struct ref_lock *lock,const char*refname,2200const char*target,const char*logmsg)2201{2202struct strbuf err = STRBUF_INIT;2203struct object_id new_oid;2204if(logmsg &&2205!refs_read_ref_full(&refs->base, target,2206 RESOLVE_REF_READING, new_oid.hash, NULL) &&2207files_log_ref_write(refs, refname, &lock->old_oid,2208&new_oid, logmsg,0, &err)) {2209error("%s", err.buf);2210strbuf_release(&err);2211}2212}22132214static intcreate_symref_locked(struct files_ref_store *refs,2215struct ref_lock *lock,const char*refname,2216const char*target,const char*logmsg)2217{2218if(prefer_symlink_refs && !create_ref_symlink(lock, target)) {2219update_symref_reflog(refs, lock, refname, target, logmsg);2220return0;2221}22222223if(!fdopen_lock_file(lock->lk,"w"))2224returnerror("unable to fdopen%s:%s",2225 lock->lk->tempfile.filename.buf,strerror(errno));22262227update_symref_reflog(refs, lock, refname, target, logmsg);22282229/* no error check; commit_ref will check ferror */2230fprintf(lock->lk->tempfile.fp,"ref:%s\n", target);2231if(commit_ref(lock) <0)2232returnerror("unable to write symref for%s:%s", refname,2233strerror(errno));2234return0;2235}22362237static intfiles_create_symref(struct ref_store *ref_store,2238const char*refname,const char*target,2239const char*logmsg)2240{2241struct files_ref_store *refs =2242files_downcast(ref_store, REF_STORE_WRITE,"create_symref");2243struct strbuf err = STRBUF_INIT;2244struct ref_lock *lock;2245int ret;22462247 lock =lock_ref_sha1_basic(refs, refname, NULL,2248 NULL, NULL, REF_NODEREF, NULL,2249&err);2250if(!lock) {2251error("%s", err.buf);2252strbuf_release(&err);2253return-1;2254}22552256 ret =create_symref_locked(refs, lock, refname, target, logmsg);2257unlock_ref(lock);2258return ret;2259}22602261static intfiles_reflog_exists(struct ref_store *ref_store,2262const char*refname)2263{2264struct files_ref_store *refs =2265files_downcast(ref_store, REF_STORE_READ,"reflog_exists");2266struct strbuf sb = STRBUF_INIT;2267struct stat st;2268int ret;22692270files_reflog_path(refs, &sb, refname);2271 ret = !lstat(sb.buf, &st) &&S_ISREG(st.st_mode);2272strbuf_release(&sb);2273return ret;2274}22752276static intfiles_delete_reflog(struct ref_store *ref_store,2277const char*refname)2278{2279struct files_ref_store *refs =2280files_downcast(ref_store, REF_STORE_WRITE,"delete_reflog");2281struct strbuf sb = STRBUF_INIT;2282int ret;22832284files_reflog_path(refs, &sb, refname);2285 ret =remove_path(sb.buf);2286strbuf_release(&sb);2287return ret;2288}22892290static intshow_one_reflog_ent(struct strbuf *sb, each_reflog_ent_fn fn,void*cb_data)2291{2292struct object_id ooid, noid;2293char*email_end, *message;2294 timestamp_t timestamp;2295int tz;2296const char*p = sb->buf;22972298/* old SP new SP name <email> SP time TAB msg LF */2299if(!sb->len || sb->buf[sb->len -1] !='\n'||2300parse_oid_hex(p, &ooid, &p) || *p++ !=' '||2301parse_oid_hex(p, &noid, &p) || *p++ !=' '||2302!(email_end =strchr(p,'>')) ||2303 email_end[1] !=' '||2304!(timestamp =parse_timestamp(email_end +2, &message,10)) ||2305!message || message[0] !=' '||2306(message[1] !='+'&& message[1] !='-') ||2307!isdigit(message[2]) || !isdigit(message[3]) ||2308!isdigit(message[4]) || !isdigit(message[5]))2309return0;/* corrupt? */2310 email_end[1] ='\0';2311 tz =strtol(message +1, NULL,10);2312if(message[6] !='\t')2313 message +=6;2314else2315 message +=7;2316returnfn(&ooid, &noid, p, timestamp, tz, message, cb_data);2317}23182319static char*find_beginning_of_line(char*bob,char*scan)2320{2321while(bob < scan && *(--scan) !='\n')2322;/* keep scanning backwards */2323/*2324 * Return either beginning of the buffer, or LF at the end of2325 * the previous line.2326 */2327return scan;2328}23292330static intfiles_for_each_reflog_ent_reverse(struct ref_store *ref_store,2331const char*refname,2332 each_reflog_ent_fn fn,2333void*cb_data)2334{2335struct files_ref_store *refs =2336files_downcast(ref_store, REF_STORE_READ,2337"for_each_reflog_ent_reverse");2338struct strbuf sb = STRBUF_INIT;2339FILE*logfp;2340long pos;2341int ret =0, at_tail =1;23422343files_reflog_path(refs, &sb, refname);2344 logfp =fopen(sb.buf,"r");2345strbuf_release(&sb);2346if(!logfp)2347return-1;23482349/* Jump to the end */2350if(fseek(logfp,0, SEEK_END) <0)2351 ret =error("cannot seek back reflog for%s:%s",2352 refname,strerror(errno));2353 pos =ftell(logfp);2354while(!ret &&0< pos) {2355int cnt;2356size_t nread;2357char buf[BUFSIZ];2358char*endp, *scanp;23592360/* Fill next block from the end */2361 cnt = (sizeof(buf) < pos) ?sizeof(buf) : pos;2362if(fseek(logfp, pos - cnt, SEEK_SET)) {2363 ret =error("cannot seek back reflog for%s:%s",2364 refname,strerror(errno));2365break;2366}2367 nread =fread(buf, cnt,1, logfp);2368if(nread !=1) {2369 ret =error("cannot read%dbytes from reflog for%s:%s",2370 cnt, refname,strerror(errno));2371break;2372}2373 pos -= cnt;23742375 scanp = endp = buf + cnt;2376if(at_tail && scanp[-1] =='\n')2377/* Looking at the final LF at the end of the file */2378 scanp--;2379 at_tail =0;23802381while(buf < scanp) {2382/*2383 * terminating LF of the previous line, or the beginning2384 * of the buffer.2385 */2386char*bp;23872388 bp =find_beginning_of_line(buf, scanp);23892390if(*bp =='\n') {2391/*2392 * The newline is the end of the previous line,2393 * so we know we have complete line starting2394 * at (bp + 1). Prefix it onto any prior data2395 * we collected for the line and process it.2396 */2397strbuf_splice(&sb,0,0, bp +1, endp - (bp +1));2398 scanp = bp;2399 endp = bp +1;2400 ret =show_one_reflog_ent(&sb, fn, cb_data);2401strbuf_reset(&sb);2402if(ret)2403break;2404}else if(!pos) {2405/*2406 * We are at the start of the buffer, and the2407 * start of the file; there is no previous2408 * line, and we have everything for this one.2409 * Process it, and we can end the loop.2410 */2411strbuf_splice(&sb,0,0, buf, endp - buf);2412 ret =show_one_reflog_ent(&sb, fn, cb_data);2413strbuf_reset(&sb);2414break;2415}24162417if(bp == buf) {2418/*2419 * We are at the start of the buffer, and there2420 * is more file to read backwards. Which means2421 * we are in the middle of a line. Note that we2422 * may get here even if *bp was a newline; that2423 * just means we are at the exact end of the2424 * previous line, rather than some spot in the2425 * middle.2426 *2427 * Save away what we have to be combined with2428 * the data from the next read.2429 */2430strbuf_splice(&sb,0,0, buf, endp - buf);2431break;2432}2433}24342435}2436if(!ret && sb.len)2437die("BUG: reverse reflog parser had leftover data");24382439fclose(logfp);2440strbuf_release(&sb);2441return ret;2442}24432444static intfiles_for_each_reflog_ent(struct ref_store *ref_store,2445const char*refname,2446 each_reflog_ent_fn fn,void*cb_data)2447{2448struct files_ref_store *refs =2449files_downcast(ref_store, REF_STORE_READ,2450"for_each_reflog_ent");2451FILE*logfp;2452struct strbuf sb = STRBUF_INIT;2453int ret =0;24542455files_reflog_path(refs, &sb, refname);2456 logfp =fopen(sb.buf,"r");2457strbuf_release(&sb);2458if(!logfp)2459return-1;24602461while(!ret && !strbuf_getwholeline(&sb, logfp,'\n'))2462 ret =show_one_reflog_ent(&sb, fn, cb_data);2463fclose(logfp);2464strbuf_release(&sb);2465return ret;2466}24672468struct files_reflog_iterator {2469struct ref_iterator base;24702471struct ref_store *ref_store;2472struct dir_iterator *dir_iterator;2473struct object_id oid;2474};24752476static intfiles_reflog_iterator_advance(struct ref_iterator *ref_iterator)2477{2478struct files_reflog_iterator *iter =2479(struct files_reflog_iterator *)ref_iterator;2480struct dir_iterator *diter = iter->dir_iterator;2481int ok;24822483while((ok =dir_iterator_advance(diter)) == ITER_OK) {2484int flags;24852486if(!S_ISREG(diter->st.st_mode))2487continue;2488if(diter->basename[0] =='.')2489continue;2490if(ends_with(diter->basename,".lock"))2491continue;24922493if(refs_read_ref_full(iter->ref_store,2494 diter->relative_path,0,2495 iter->oid.hash, &flags)) {2496error("bad ref for%s", diter->path.buf);2497continue;2498}24992500 iter->base.refname = diter->relative_path;2501 iter->base.oid = &iter->oid;2502 iter->base.flags = flags;2503return ITER_OK;2504}25052506 iter->dir_iterator = NULL;2507if(ref_iterator_abort(ref_iterator) == ITER_ERROR)2508 ok = ITER_ERROR;2509return ok;2510}25112512static intfiles_reflog_iterator_peel(struct ref_iterator *ref_iterator,2513struct object_id *peeled)2514{2515die("BUG: ref_iterator_peel() called for reflog_iterator");2516}25172518static intfiles_reflog_iterator_abort(struct ref_iterator *ref_iterator)2519{2520struct files_reflog_iterator *iter =2521(struct files_reflog_iterator *)ref_iterator;2522int ok = ITER_DONE;25232524if(iter->dir_iterator)2525 ok =dir_iterator_abort(iter->dir_iterator);25262527base_ref_iterator_free(ref_iterator);2528return ok;2529}25302531static struct ref_iterator_vtable files_reflog_iterator_vtable = {2532 files_reflog_iterator_advance,2533 files_reflog_iterator_peel,2534 files_reflog_iterator_abort2535};25362537static struct ref_iterator *files_reflog_iterator_begin(struct ref_store *ref_store)2538{2539struct files_ref_store *refs =2540files_downcast(ref_store, REF_STORE_READ,2541"reflog_iterator_begin");2542struct files_reflog_iterator *iter =xcalloc(1,sizeof(*iter));2543struct ref_iterator *ref_iterator = &iter->base;2544struct strbuf sb = STRBUF_INIT;25452546base_ref_iterator_init(ref_iterator, &files_reflog_iterator_vtable);2547files_reflog_path(refs, &sb, NULL);2548 iter->dir_iterator =dir_iterator_begin(sb.buf);2549 iter->ref_store = ref_store;2550strbuf_release(&sb);2551return ref_iterator;2552}25532554/*2555 * If update is a direct update of head_ref (the reference pointed to2556 * by HEAD), then add an extra REF_LOG_ONLY update for HEAD.2557 */2558static intsplit_head_update(struct ref_update *update,2559struct ref_transaction *transaction,2560const char*head_ref,2561struct string_list *affected_refnames,2562struct strbuf *err)2563{2564struct string_list_item *item;2565struct ref_update *new_update;25662567if((update->flags & REF_LOG_ONLY) ||2568(update->flags & REF_ISPRUNING) ||2569(update->flags & REF_UPDATE_VIA_HEAD))2570return0;25712572if(strcmp(update->refname, head_ref))2573return0;25742575/*2576 * First make sure that HEAD is not already in the2577 * transaction. This insertion is O(N) in the transaction2578 * size, but it happens at most once per transaction.2579 */2580 item =string_list_insert(affected_refnames,"HEAD");2581if(item->util) {2582/* An entry already existed */2583strbuf_addf(err,2584"multiple updates for 'HEAD' (including one "2585"via its referent '%s') are not allowed",2586 update->refname);2587return TRANSACTION_NAME_CONFLICT;2588}25892590 new_update =ref_transaction_add_update(2591 transaction,"HEAD",2592 update->flags | REF_LOG_ONLY | REF_NODEREF,2593 update->new_oid.hash, update->old_oid.hash,2594 update->msg);25952596 item->util = new_update;25972598return0;2599}26002601/*2602 * update is for a symref that points at referent and doesn't have2603 * REF_NODEREF set. Split it into two updates:2604 * - The original update, but with REF_LOG_ONLY and REF_NODEREF set2605 * - A new, separate update for the referent reference2606 * Note that the new update will itself be subject to splitting when2607 * the iteration gets to it.2608 */2609static intsplit_symref_update(struct files_ref_store *refs,2610struct ref_update *update,2611const char*referent,2612struct ref_transaction *transaction,2613struct string_list *affected_refnames,2614struct strbuf *err)2615{2616struct string_list_item *item;2617struct ref_update *new_update;2618unsigned int new_flags;26192620/*2621 * First make sure that referent is not already in the2622 * transaction. This insertion is O(N) in the transaction2623 * size, but it happens at most once per symref in a2624 * transaction.2625 */2626 item =string_list_insert(affected_refnames, referent);2627if(item->util) {2628/* An entry already existed */2629strbuf_addf(err,2630"multiple updates for '%s' (including one "2631"via symref '%s') are not allowed",2632 referent, update->refname);2633return TRANSACTION_NAME_CONFLICT;2634}26352636 new_flags = update->flags;2637if(!strcmp(update->refname,"HEAD")) {2638/*2639 * Record that the new update came via HEAD, so that2640 * when we process it, split_head_update() doesn't try2641 * to add another reflog update for HEAD. Note that2642 * this bit will be propagated if the new_update2643 * itself needs to be split.2644 */2645 new_flags |= REF_UPDATE_VIA_HEAD;2646}26472648 new_update =ref_transaction_add_update(2649 transaction, referent, new_flags,2650 update->new_oid.hash, update->old_oid.hash,2651 update->msg);26522653 new_update->parent_update = update;26542655/*2656 * Change the symbolic ref update to log only. Also, it2657 * doesn't need to check its old SHA-1 value, as that will be2658 * done when new_update is processed.2659 */2660 update->flags |= REF_LOG_ONLY | REF_NODEREF;2661 update->flags &= ~REF_HAVE_OLD;26622663 item->util = new_update;26642665return0;2666}26672668/*2669 * Return the refname under which update was originally requested.2670 */2671static const char*original_update_refname(struct ref_update *update)2672{2673while(update->parent_update)2674 update = update->parent_update;26752676return update->refname;2677}26782679/*2680 * Check whether the REF_HAVE_OLD and old_oid values stored in update2681 * are consistent with oid, which is the reference's current value. If2682 * everything is OK, return 0; otherwise, write an error message to2683 * err and return -1.2684 */2685static intcheck_old_oid(struct ref_update *update,struct object_id *oid,2686struct strbuf *err)2687{2688if(!(update->flags & REF_HAVE_OLD) ||2689!oidcmp(oid, &update->old_oid))2690return0;26912692if(is_null_oid(&update->old_oid))2693strbuf_addf(err,"cannot lock ref '%s': "2694"reference already exists",2695original_update_refname(update));2696else if(is_null_oid(oid))2697strbuf_addf(err,"cannot lock ref '%s': "2698"reference is missing but expected%s",2699original_update_refname(update),2700oid_to_hex(&update->old_oid));2701else2702strbuf_addf(err,"cannot lock ref '%s': "2703"is at%sbut expected%s",2704original_update_refname(update),2705oid_to_hex(oid),2706oid_to_hex(&update->old_oid));27072708return-1;2709}27102711/*2712 * Prepare for carrying out update:2713 * - Lock the reference referred to by update.2714 * - Read the reference under lock.2715 * - Check that its old SHA-1 value (if specified) is correct, and in2716 * any case record it in update->lock->old_oid for later use when2717 * writing the reflog.2718 * - If it is a symref update without REF_NODEREF, split it up into a2719 * REF_LOG_ONLY update of the symref and add a separate update for2720 * the referent to transaction.2721 * - If it is an update of head_ref, add a corresponding REF_LOG_ONLY2722 * update of HEAD.2723 */2724static intlock_ref_for_update(struct files_ref_store *refs,2725struct ref_update *update,2726struct ref_transaction *transaction,2727const char*head_ref,2728struct string_list *affected_refnames,2729struct strbuf *err)2730{2731struct strbuf referent = STRBUF_INIT;2732int mustexist = (update->flags & REF_HAVE_OLD) &&2733!is_null_oid(&update->old_oid);2734int ret;2735struct ref_lock *lock;27362737files_assert_main_repository(refs,"lock_ref_for_update");27382739if((update->flags & REF_HAVE_NEW) &&is_null_oid(&update->new_oid))2740 update->flags |= REF_DELETING;27412742if(head_ref) {2743 ret =split_head_update(update, transaction, head_ref,2744 affected_refnames, err);2745if(ret)2746return ret;2747}27482749 ret =lock_raw_ref(refs, update->refname, mustexist,2750 affected_refnames, NULL,2751&lock, &referent,2752&update->type, err);2753if(ret) {2754char*reason;27552756 reason =strbuf_detach(err, NULL);2757strbuf_addf(err,"cannot lock ref '%s':%s",2758original_update_refname(update), reason);2759free(reason);2760return ret;2761}27622763 update->backend_data = lock;27642765if(update->type & REF_ISSYMREF) {2766if(update->flags & REF_NODEREF) {2767/*2768 * We won't be reading the referent as part of2769 * the transaction, so we have to read it here2770 * to record and possibly check old_sha1:2771 */2772if(refs_read_ref_full(&refs->base,2773 referent.buf,0,2774 lock->old_oid.hash, NULL)) {2775if(update->flags & REF_HAVE_OLD) {2776strbuf_addf(err,"cannot lock ref '%s': "2777"error reading reference",2778original_update_refname(update));2779return-1;2780}2781}else if(check_old_oid(update, &lock->old_oid, err)) {2782return TRANSACTION_GENERIC_ERROR;2783}2784}else{2785/*2786 * Create a new update for the reference this2787 * symref is pointing at. Also, we will record2788 * and verify old_sha1 for this update as part2789 * of processing the split-off update, so we2790 * don't have to do it here.2791 */2792 ret =split_symref_update(refs, update,2793 referent.buf, transaction,2794 affected_refnames, err);2795if(ret)2796return ret;2797}2798}else{2799struct ref_update *parent_update;28002801if(check_old_oid(update, &lock->old_oid, err))2802return TRANSACTION_GENERIC_ERROR;28032804/*2805 * If this update is happening indirectly because of a2806 * symref update, record the old SHA-1 in the parent2807 * update:2808 */2809for(parent_update = update->parent_update;2810 parent_update;2811 parent_update = parent_update->parent_update) {2812struct ref_lock *parent_lock = parent_update->backend_data;2813oidcpy(&parent_lock->old_oid, &lock->old_oid);2814}2815}28162817if((update->flags & REF_HAVE_NEW) &&2818!(update->flags & REF_DELETING) &&2819!(update->flags & REF_LOG_ONLY)) {2820if(!(update->type & REF_ISSYMREF) &&2821!oidcmp(&lock->old_oid, &update->new_oid)) {2822/*2823 * The reference already has the desired2824 * value, so we don't need to write it.2825 */2826}else if(write_ref_to_lockfile(lock, &update->new_oid,2827 err)) {2828char*write_err =strbuf_detach(err, NULL);28292830/*2831 * The lock was freed upon failure of2832 * write_ref_to_lockfile():2833 */2834 update->backend_data = NULL;2835strbuf_addf(err,2836"cannot update ref '%s':%s",2837 update->refname, write_err);2838free(write_err);2839return TRANSACTION_GENERIC_ERROR;2840}else{2841 update->flags |= REF_NEEDS_COMMIT;2842}2843}2844if(!(update->flags & REF_NEEDS_COMMIT)) {2845/*2846 * We didn't call write_ref_to_lockfile(), so2847 * the lockfile is still open. Close it to2848 * free up the file descriptor:2849 */2850if(close_ref(lock)) {2851strbuf_addf(err,"couldn't close '%s.lock'",2852 update->refname);2853return TRANSACTION_GENERIC_ERROR;2854}2855}2856return0;2857}28582859/*2860 * Unlock any references in `transaction` that are still locked, and2861 * mark the transaction closed.2862 */2863static voidfiles_transaction_cleanup(struct ref_transaction *transaction)2864{2865size_t i;28662867for(i =0; i < transaction->nr; i++) {2868struct ref_update *update = transaction->updates[i];2869struct ref_lock *lock = update->backend_data;28702871if(lock) {2872unlock_ref(lock);2873 update->backend_data = NULL;2874}2875}28762877 transaction->state = REF_TRANSACTION_CLOSED;2878}28792880static intfiles_transaction_prepare(struct ref_store *ref_store,2881struct ref_transaction *transaction,2882struct strbuf *err)2883{2884struct files_ref_store *refs =2885files_downcast(ref_store, REF_STORE_WRITE,2886"ref_transaction_prepare");2887size_t i;2888int ret =0;2889struct string_list affected_refnames = STRING_LIST_INIT_NODUP;2890char*head_ref = NULL;2891int head_type;2892struct object_id head_oid;28932894assert(err);28952896if(!transaction->nr)2897goto cleanup;28982899/*2900 * Fail if a refname appears more than once in the2901 * transaction. (If we end up splitting up any updates using2902 * split_symref_update() or split_head_update(), those2903 * functions will check that the new updates don't have the2904 * same refname as any existing ones.)2905 */2906for(i =0; i < transaction->nr; i++) {2907struct ref_update *update = transaction->updates[i];2908struct string_list_item *item =2909string_list_append(&affected_refnames, update->refname);29102911/*2912 * We store a pointer to update in item->util, but at2913 * the moment we never use the value of this field2914 * except to check whether it is non-NULL.2915 */2916 item->util = update;2917}2918string_list_sort(&affected_refnames);2919if(ref_update_reject_duplicates(&affected_refnames, err)) {2920 ret = TRANSACTION_GENERIC_ERROR;2921goto cleanup;2922}29232924/*2925 * Special hack: If a branch is updated directly and HEAD2926 * points to it (may happen on the remote side of a push2927 * for example) then logically the HEAD reflog should be2928 * updated too.2929 *2930 * A generic solution would require reverse symref lookups,2931 * but finding all symrefs pointing to a given branch would be2932 * rather costly for this rare event (the direct update of a2933 * branch) to be worth it. So let's cheat and check with HEAD2934 * only, which should cover 99% of all usage scenarios (even2935 * 100% of the default ones).2936 *2937 * So if HEAD is a symbolic reference, then record the name of2938 * the reference that it points to. If we see an update of2939 * head_ref within the transaction, then split_head_update()2940 * arranges for the reflog of HEAD to be updated, too.2941 */2942 head_ref =refs_resolve_refdup(ref_store,"HEAD",2943 RESOLVE_REF_NO_RECURSE,2944 head_oid.hash, &head_type);29452946if(head_ref && !(head_type & REF_ISSYMREF)) {2947free(head_ref);2948 head_ref = NULL;2949}29502951/*2952 * Acquire all locks, verify old values if provided, check2953 * that new values are valid, and write new values to the2954 * lockfiles, ready to be activated. Only keep one lockfile2955 * open at a time to avoid running out of file descriptors.2956 * Note that lock_ref_for_update() might append more updates2957 * to the transaction.2958 */2959for(i =0; i < transaction->nr; i++) {2960struct ref_update *update = transaction->updates[i];29612962 ret =lock_ref_for_update(refs, update, transaction,2963 head_ref, &affected_refnames, err);2964if(ret)2965break;2966}29672968cleanup:2969free(head_ref);2970string_list_clear(&affected_refnames,0);29712972if(ret)2973files_transaction_cleanup(transaction);2974else2975 transaction->state = REF_TRANSACTION_PREPARED;29762977return ret;2978}29792980static intfiles_transaction_finish(struct ref_store *ref_store,2981struct ref_transaction *transaction,2982struct strbuf *err)2983{2984struct files_ref_store *refs =2985files_downcast(ref_store,0,"ref_transaction_finish");2986size_t i;2987int ret =0;2988struct string_list refs_to_delete = STRING_LIST_INIT_NODUP;2989struct string_list_item *ref_to_delete;2990struct strbuf sb = STRBUF_INIT;29912992assert(err);29932994if(!transaction->nr) {2995 transaction->state = REF_TRANSACTION_CLOSED;2996return0;2997}29982999/* Perform updates first so live commits remain referenced */3000for(i =0; i < transaction->nr; i++) {3001struct ref_update *update = transaction->updates[i];3002struct ref_lock *lock = update->backend_data;30033004if(update->flags & REF_NEEDS_COMMIT ||3005 update->flags & REF_LOG_ONLY) {3006if(files_log_ref_write(refs,3007 lock->ref_name,3008&lock->old_oid,3009&update->new_oid,3010 update->msg, update->flags,3011 err)) {3012char*old_msg =strbuf_detach(err, NULL);30133014strbuf_addf(err,"cannot update the ref '%s':%s",3015 lock->ref_name, old_msg);3016free(old_msg);3017unlock_ref(lock);3018 update->backend_data = NULL;3019 ret = TRANSACTION_GENERIC_ERROR;3020goto cleanup;3021}3022}3023if(update->flags & REF_NEEDS_COMMIT) {3024clear_loose_ref_cache(refs);3025if(commit_ref(lock)) {3026strbuf_addf(err,"couldn't set '%s'", lock->ref_name);3027unlock_ref(lock);3028 update->backend_data = NULL;3029 ret = TRANSACTION_GENERIC_ERROR;3030goto cleanup;3031}3032}3033}3034/* Perform deletes now that updates are safely completed */3035for(i =0; i < transaction->nr; i++) {3036struct ref_update *update = transaction->updates[i];3037struct ref_lock *lock = update->backend_data;30383039if(update->flags & REF_DELETING &&3040!(update->flags & REF_LOG_ONLY)) {3041if(!(update->type & REF_ISPACKED) ||3042 update->type & REF_ISSYMREF) {3043/* It is a loose reference. */3044strbuf_reset(&sb);3045files_ref_path(refs, &sb, lock->ref_name);3046if(unlink_or_msg(sb.buf, err)) {3047 ret = TRANSACTION_GENERIC_ERROR;3048goto cleanup;3049}3050 update->flags |= REF_DELETED_LOOSE;3051}30523053if(!(update->flags & REF_ISPRUNING))3054string_list_append(&refs_to_delete,3055 lock->ref_name);3056}3057}30583059if(repack_without_refs(refs, &refs_to_delete, err)) {3060 ret = TRANSACTION_GENERIC_ERROR;3061goto cleanup;3062}30633064/* Delete the reflogs of any references that were deleted: */3065for_each_string_list_item(ref_to_delete, &refs_to_delete) {3066strbuf_reset(&sb);3067files_reflog_path(refs, &sb, ref_to_delete->string);3068if(!unlink_or_warn(sb.buf))3069try_remove_empty_parents(refs, ref_to_delete->string,3070 REMOVE_EMPTY_PARENTS_REFLOG);3071}30723073clear_loose_ref_cache(refs);30743075cleanup:3076files_transaction_cleanup(transaction);30773078for(i =0; i < transaction->nr; i++) {3079struct ref_update *update = transaction->updates[i];30803081if(update->flags & REF_DELETED_LOOSE) {3082/*3083 * The loose reference was deleted. Delete any3084 * empty parent directories. (Note that this3085 * can only work because we have already3086 * removed the lockfile.)3087 */3088try_remove_empty_parents(refs, update->refname,3089 REMOVE_EMPTY_PARENTS_REF);3090}3091}30923093strbuf_release(&sb);3094string_list_clear(&refs_to_delete,0);3095return ret;3096}30973098static intfiles_transaction_abort(struct ref_store *ref_store,3099struct ref_transaction *transaction,3100struct strbuf *err)3101{3102files_transaction_cleanup(transaction);3103return0;3104}31053106static intref_present(const char*refname,3107const struct object_id *oid,int flags,void*cb_data)3108{3109struct string_list *affected_refnames = cb_data;31103111returnstring_list_has_string(affected_refnames, refname);3112}31133114static intfiles_initial_transaction_commit(struct ref_store *ref_store,3115struct ref_transaction *transaction,3116struct strbuf *err)3117{3118struct files_ref_store *refs =3119files_downcast(ref_store, REF_STORE_WRITE,3120"initial_ref_transaction_commit");3121size_t i;3122int ret =0;3123struct string_list affected_refnames = STRING_LIST_INIT_NODUP;31243125assert(err);31263127if(transaction->state != REF_TRANSACTION_OPEN)3128die("BUG: commit called for transaction that is not open");31293130/* Fail if a refname appears more than once in the transaction: */3131for(i =0; i < transaction->nr; i++)3132string_list_append(&affected_refnames,3133 transaction->updates[i]->refname);3134string_list_sort(&affected_refnames);3135if(ref_update_reject_duplicates(&affected_refnames, err)) {3136 ret = TRANSACTION_GENERIC_ERROR;3137goto cleanup;3138}31393140/*3141 * It's really undefined to call this function in an active3142 * repository or when there are existing references: we are3143 * only locking and changing packed-refs, so (1) any3144 * simultaneous processes might try to change a reference at3145 * the same time we do, and (2) any existing loose versions of3146 * the references that we are setting would have precedence3147 * over our values. But some remote helpers create the remote3148 * "HEAD" and "master" branches before calling this function,3149 * so here we really only check that none of the references3150 * that we are creating already exists.3151 */3152if(refs_for_each_rawref(&refs->base, ref_present,3153&affected_refnames))3154die("BUG: initial ref transaction called with existing refs");31553156for(i =0; i < transaction->nr; i++) {3157struct ref_update *update = transaction->updates[i];31583159if((update->flags & REF_HAVE_OLD) &&3160!is_null_oid(&update->old_oid))3161die("BUG: initial ref transaction with old_sha1 set");3162if(refs_verify_refname_available(&refs->base, update->refname,3163&affected_refnames, NULL,3164 err)) {3165 ret = TRANSACTION_NAME_CONFLICT;3166goto cleanup;3167}3168}31693170if(lock_packed_refs(refs,0)) {3171strbuf_addf(err,"unable to lock packed-refs file:%s",3172strerror(errno));3173 ret = TRANSACTION_GENERIC_ERROR;3174goto cleanup;3175}31763177for(i =0; i < transaction->nr; i++) {3178struct ref_update *update = transaction->updates[i];31793180if((update->flags & REF_HAVE_NEW) &&3181!is_null_oid(&update->new_oid))3182add_packed_ref(refs, update->refname,3183&update->new_oid);3184}31853186if(commit_packed_refs(refs)) {3187strbuf_addf(err,"unable to commit packed-refs file:%s",3188strerror(errno));3189 ret = TRANSACTION_GENERIC_ERROR;3190goto cleanup;3191}31923193cleanup:3194 transaction->state = REF_TRANSACTION_CLOSED;3195string_list_clear(&affected_refnames,0);3196return ret;3197}31983199struct expire_reflog_cb {3200unsigned int flags;3201 reflog_expiry_should_prune_fn *should_prune_fn;3202void*policy_cb;3203FILE*newlog;3204struct object_id last_kept_oid;3205};32063207static intexpire_reflog_ent(struct object_id *ooid,struct object_id *noid,3208const char*email, timestamp_t timestamp,int tz,3209const char*message,void*cb_data)3210{3211struct expire_reflog_cb *cb = cb_data;3212struct expire_reflog_policy_cb *policy_cb = cb->policy_cb;32133214if(cb->flags & EXPIRE_REFLOGS_REWRITE)3215 ooid = &cb->last_kept_oid;32163217if((*cb->should_prune_fn)(ooid, noid, email, timestamp, tz,3218 message, policy_cb)) {3219if(!cb->newlog)3220printf("would prune%s", message);3221else if(cb->flags & EXPIRE_REFLOGS_VERBOSE)3222printf("prune%s", message);3223}else{3224if(cb->newlog) {3225fprintf(cb->newlog,"%s %s %s%"PRItime" %+05d\t%s",3226oid_to_hex(ooid),oid_to_hex(noid),3227 email, timestamp, tz, message);3228oidcpy(&cb->last_kept_oid, noid);3229}3230if(cb->flags & EXPIRE_REFLOGS_VERBOSE)3231printf("keep%s", message);3232}3233return0;3234}32353236static intfiles_reflog_expire(struct ref_store *ref_store,3237const char*refname,const unsigned char*sha1,3238unsigned int flags,3239 reflog_expiry_prepare_fn prepare_fn,3240 reflog_expiry_should_prune_fn should_prune_fn,3241 reflog_expiry_cleanup_fn cleanup_fn,3242void*policy_cb_data)3243{3244struct files_ref_store *refs =3245files_downcast(ref_store, REF_STORE_WRITE,"reflog_expire");3246static struct lock_file reflog_lock;3247struct expire_reflog_cb cb;3248struct ref_lock *lock;3249struct strbuf log_file_sb = STRBUF_INIT;3250char*log_file;3251int status =0;3252int type;3253struct strbuf err = STRBUF_INIT;3254struct object_id oid;32553256memset(&cb,0,sizeof(cb));3257 cb.flags = flags;3258 cb.policy_cb = policy_cb_data;3259 cb.should_prune_fn = should_prune_fn;32603261/*3262 * The reflog file is locked by holding the lock on the3263 * reference itself, plus we might need to update the3264 * reference if --updateref was specified:3265 */3266 lock =lock_ref_sha1_basic(refs, refname, sha1,3267 NULL, NULL, REF_NODEREF,3268&type, &err);3269if(!lock) {3270error("cannot lock ref '%s':%s", refname, err.buf);3271strbuf_release(&err);3272return-1;3273}3274if(!refs_reflog_exists(ref_store, refname)) {3275unlock_ref(lock);3276return0;3277}32783279files_reflog_path(refs, &log_file_sb, refname);3280 log_file =strbuf_detach(&log_file_sb, NULL);3281if(!(flags & EXPIRE_REFLOGS_DRY_RUN)) {3282/*3283 * Even though holding $GIT_DIR/logs/$reflog.lock has3284 * no locking implications, we use the lock_file3285 * machinery here anyway because it does a lot of the3286 * work we need, including cleaning up if the program3287 * exits unexpectedly.3288 */3289if(hold_lock_file_for_update(&reflog_lock, log_file,0) <0) {3290struct strbuf err = STRBUF_INIT;3291unable_to_lock_message(log_file, errno, &err);3292error("%s", err.buf);3293strbuf_release(&err);3294goto failure;3295}3296 cb.newlog =fdopen_lock_file(&reflog_lock,"w");3297if(!cb.newlog) {3298error("cannot fdopen%s(%s)",3299get_lock_file_path(&reflog_lock),strerror(errno));3300goto failure;3301}3302}33033304hashcpy(oid.hash, sha1);33053306(*prepare_fn)(refname, &oid, cb.policy_cb);3307refs_for_each_reflog_ent(ref_store, refname, expire_reflog_ent, &cb);3308(*cleanup_fn)(cb.policy_cb);33093310if(!(flags & EXPIRE_REFLOGS_DRY_RUN)) {3311/*3312 * It doesn't make sense to adjust a reference pointed3313 * to by a symbolic ref based on expiring entries in3314 * the symbolic reference's reflog. Nor can we update3315 * a reference if there are no remaining reflog3316 * entries.3317 */3318int update = (flags & EXPIRE_REFLOGS_UPDATE_REF) &&3319!(type & REF_ISSYMREF) &&3320!is_null_oid(&cb.last_kept_oid);33213322if(close_lock_file(&reflog_lock)) {3323 status |=error("couldn't write%s:%s", log_file,3324strerror(errno));3325}else if(update &&3326(write_in_full(get_lock_file_fd(lock->lk),3327oid_to_hex(&cb.last_kept_oid), GIT_SHA1_HEXSZ) != GIT_SHA1_HEXSZ ||3328write_str_in_full(get_lock_file_fd(lock->lk),"\n") !=1||3329close_ref(lock) <0)) {3330 status |=error("couldn't write%s",3331get_lock_file_path(lock->lk));3332rollback_lock_file(&reflog_lock);3333}else if(commit_lock_file(&reflog_lock)) {3334 status |=error("unable to write reflog '%s' (%s)",3335 log_file,strerror(errno));3336}else if(update &&commit_ref(lock)) {3337 status |=error("couldn't set%s", lock->ref_name);3338}3339}3340free(log_file);3341unlock_ref(lock);3342return status;33433344 failure:3345rollback_lock_file(&reflog_lock);3346free(log_file);3347unlock_ref(lock);3348return-1;3349}33503351static intfiles_init_db(struct ref_store *ref_store,struct strbuf *err)3352{3353struct files_ref_store *refs =3354files_downcast(ref_store, REF_STORE_WRITE,"init_db");3355struct strbuf sb = STRBUF_INIT;33563357/*3358 * Create .git/refs/{heads,tags}3359 */3360files_ref_path(refs, &sb,"refs/heads");3361safe_create_dir(sb.buf,1);33623363strbuf_reset(&sb);3364files_ref_path(refs, &sb,"refs/tags");3365safe_create_dir(sb.buf,1);33663367strbuf_release(&sb);3368return0;3369}33703371struct ref_storage_be refs_be_files = {3372 NULL,3373"files",3374 files_ref_store_create,3375 files_init_db,3376 files_transaction_prepare,3377 files_transaction_finish,3378 files_transaction_abort,3379 files_initial_transaction_commit,33803381 files_pack_refs,3382 files_peel_ref,3383 files_create_symref,3384 files_delete_refs,3385 files_rename_ref,33863387 files_ref_iterator_begin,3388 files_read_raw_ref,33893390 files_reflog_iterator_begin,3391 files_for_each_reflog_ent,3392 files_for_each_reflog_ent_reverse,3393 files_reflog_exists,3394 files_create_reflog,3395 files_delete_reflog,3396 files_reflog_expire3397};