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 35/* 36 * Return true if the reference described by entry can be resolved to 37 * an object in the database; otherwise, emit a warning and return 38 * false. 39 */ 40static intentry_resolves_to_object(struct ref_entry *entry) 41{ 42returnref_resolves_to_object(entry->name, 43&entry->u.value.oid, entry->flag); 44} 45 46struct packed_ref_cache { 47struct ref_cache *cache; 48 49/* 50 * Count of references to the data structure in this instance, 51 * including the pointer from files_ref_store::packed if any. 52 * The data will not be freed as long as the reference count 53 * is nonzero. 54 */ 55unsigned int referrers; 56 57/* 58 * Iff the packed-refs file associated with this instance is 59 * currently locked for writing, this points at the associated 60 * lock (which is owned by somebody else). The referrer count 61 * is also incremented when the file is locked and decremented 62 * when it is unlocked. 63 */ 64struct lock_file *lock; 65 66/* The metadata from when this packed-refs cache was read */ 67struct stat_validity validity; 68}; 69 70/* 71 * Future: need to be in "struct repository" 72 * when doing a full libification. 73 */ 74struct files_ref_store { 75struct ref_store base; 76unsigned int store_flags; 77 78char*gitdir; 79char*gitcommondir; 80char*packed_refs_path; 81 82struct ref_cache *loose; 83struct packed_ref_cache *packed; 84}; 85 86/* Lock used for the main packed-refs file: */ 87static struct lock_file packlock; 88 89/* 90 * Increment the reference count of *packed_refs. 91 */ 92static voidacquire_packed_ref_cache(struct packed_ref_cache *packed_refs) 93{ 94 packed_refs->referrers++; 95} 96 97/* 98 * Decrease the reference count of *packed_refs. If it goes to zero, 99 * free *packed_refs and return true; otherwise return false. 100 */ 101static intrelease_packed_ref_cache(struct packed_ref_cache *packed_refs) 102{ 103if(!--packed_refs->referrers) { 104free_ref_cache(packed_refs->cache); 105stat_validity_clear(&packed_refs->validity); 106free(packed_refs); 107return1; 108}else{ 109return0; 110} 111} 112 113static voidclear_packed_ref_cache(struct files_ref_store *refs) 114{ 115if(refs->packed) { 116struct packed_ref_cache *packed_refs = refs->packed; 117 118if(packed_refs->lock) 119die("internal error: packed-ref cache cleared while locked"); 120 refs->packed = NULL; 121release_packed_ref_cache(packed_refs); 122} 123} 124 125static voidclear_loose_ref_cache(struct files_ref_store *refs) 126{ 127if(refs->loose) { 128free_ref_cache(refs->loose); 129 refs->loose = NULL; 130} 131} 132 133/* 134 * Create a new submodule ref cache and add it to the internal 135 * set of caches. 136 */ 137static struct ref_store *files_ref_store_create(const char*gitdir, 138unsigned int flags) 139{ 140struct files_ref_store *refs =xcalloc(1,sizeof(*refs)); 141struct ref_store *ref_store = (struct ref_store *)refs; 142struct strbuf sb = STRBUF_INIT; 143 144base_ref_store_init(ref_store, &refs_be_files); 145 refs->store_flags = flags; 146 147 refs->gitdir =xstrdup(gitdir); 148get_common_dir_noenv(&sb, gitdir); 149 refs->gitcommondir =strbuf_detach(&sb, NULL); 150strbuf_addf(&sb,"%s/packed-refs", refs->gitcommondir); 151 refs->packed_refs_path =strbuf_detach(&sb, NULL); 152 153return ref_store; 154} 155 156/* 157 * Die if refs is not the main ref store. caller is used in any 158 * necessary error messages. 159 */ 160static voidfiles_assert_main_repository(struct files_ref_store *refs, 161const char*caller) 162{ 163if(refs->store_flags & REF_STORE_MAIN) 164return; 165 166die("BUG: operation%sonly allowed for main ref store", caller); 167} 168 169/* 170 * Downcast ref_store to files_ref_store. Die if ref_store is not a 171 * files_ref_store. required_flags is compared with ref_store's 172 * store_flags to ensure the ref_store has all required capabilities. 173 * "caller" is used in any necessary error messages. 174 */ 175static struct files_ref_store *files_downcast(struct ref_store *ref_store, 176unsigned int required_flags, 177const char*caller) 178{ 179struct files_ref_store *refs; 180 181if(ref_store->be != &refs_be_files) 182die("BUG: ref_store is type\"%s\"not\"files\"in%s", 183 ref_store->be->name, caller); 184 185 refs = (struct files_ref_store *)ref_store; 186 187if((refs->store_flags & required_flags) != required_flags) 188die("BUG: operation%srequires abilities 0x%x, but only have 0x%x", 189 caller, required_flags, refs->store_flags); 190 191return refs; 192} 193 194/* The length of a peeled reference line in packed-refs, including EOL: */ 195#define PEELED_LINE_LENGTH 42 196 197/* 198 * The packed-refs header line that we write out. Perhaps other 199 * traits will be added later. The trailing space is required. 200 */ 201static const char PACKED_REFS_HEADER[] = 202"# pack-refs with: peeled fully-peeled\n"; 203 204/* 205 * Parse one line from a packed-refs file. Write the SHA1 to sha1. 206 * Return a pointer to the refname within the line (null-terminated), 207 * or NULL if there was a problem. 208 */ 209static const char*parse_ref_line(struct strbuf *line,unsigned char*sha1) 210{ 211const char*ref; 212 213/* 214 * 42: the answer to everything. 215 * 216 * In this case, it happens to be the answer to 217 * 40 (length of sha1 hex representation) 218 * +1 (space in between hex and name) 219 * +1 (newline at the end of the line) 220 */ 221if(line->len <=42) 222return NULL; 223 224if(get_sha1_hex(line->buf, sha1) <0) 225return NULL; 226if(!isspace(line->buf[40])) 227return NULL; 228 229 ref = line->buf +41; 230if(isspace(*ref)) 231return NULL; 232 233if(line->buf[line->len -1] !='\n') 234return NULL; 235 line->buf[--line->len] =0; 236 237return ref; 238} 239 240/* 241 * Read f, which is a packed-refs file, into dir. 242 * 243 * A comment line of the form "# pack-refs with: " may contain zero or 244 * more traits. We interpret the traits as follows: 245 * 246 * No traits: 247 * 248 * Probably no references are peeled. But if the file contains a 249 * peeled value for a reference, we will use it. 250 * 251 * peeled: 252 * 253 * References under "refs/tags/", if they *can* be peeled, *are* 254 * peeled in this file. References outside of "refs/tags/" are 255 * probably not peeled even if they could have been, but if we find 256 * a peeled value for such a reference we will use it. 257 * 258 * fully-peeled: 259 * 260 * All references in the file that can be peeled are peeled. 261 * Inversely (and this is more important), any references in the 262 * file for which no peeled value is recorded is not peelable. This 263 * trait should typically be written alongside "peeled" for 264 * compatibility with older clients, but we do not require it 265 * (i.e., "peeled" is a no-op if "fully-peeled" is set). 266 */ 267static voidread_packed_refs(FILE*f,struct ref_dir *dir) 268{ 269struct ref_entry *last = NULL; 270struct strbuf line = STRBUF_INIT; 271enum{ PEELED_NONE, PEELED_TAGS, PEELED_FULLY } peeled = PEELED_NONE; 272 273while(strbuf_getwholeline(&line, f,'\n') != EOF) { 274unsigned char sha1[20]; 275const char*refname; 276const char*traits; 277 278if(skip_prefix(line.buf,"# pack-refs with:", &traits)) { 279if(strstr(traits," fully-peeled ")) 280 peeled = PEELED_FULLY; 281else if(strstr(traits," peeled ")) 282 peeled = PEELED_TAGS; 283/* perhaps other traits later as well */ 284continue; 285} 286 287 refname =parse_ref_line(&line, sha1); 288if(refname) { 289int flag = REF_ISPACKED; 290 291if(check_refname_format(refname, REFNAME_ALLOW_ONELEVEL)) { 292if(!refname_is_safe(refname)) 293die("packed refname is dangerous:%s", refname); 294hashclr(sha1); 295 flag |= REF_BAD_NAME | REF_ISBROKEN; 296} 297 last =create_ref_entry(refname, sha1, flag,0); 298if(peeled == PEELED_FULLY || 299(peeled == PEELED_TAGS &&starts_with(refname,"refs/tags/"))) 300 last->flag |= REF_KNOWS_PEELED; 301add_ref_entry(dir, last); 302continue; 303} 304if(last && 305 line.buf[0] =='^'&& 306 line.len == PEELED_LINE_LENGTH && 307 line.buf[PEELED_LINE_LENGTH -1] =='\n'&& 308!get_sha1_hex(line.buf +1, sha1)) { 309hashcpy(last->u.value.peeled.hash, sha1); 310/* 311 * Regardless of what the file header said, 312 * we definitely know the value of *this* 313 * reference: 314 */ 315 last->flag |= REF_KNOWS_PEELED; 316} 317} 318 319strbuf_release(&line); 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 it if necessary. 375 */ 376static struct packed_ref_cache *get_packed_ref_cache(struct files_ref_store *refs) 377{ 378const char*packed_refs_file =files_packed_refs_path(refs); 379 380if(refs->packed && 381!stat_validity_check(&refs->packed->validity, packed_refs_file)) 382clear_packed_ref_cache(refs); 383 384if(!refs->packed) { 385FILE*f; 386 387 refs->packed =xcalloc(1,sizeof(*refs->packed)); 388acquire_packed_ref_cache(refs->packed); 389 refs->packed->cache =create_ref_cache(&refs->base, NULL); 390 refs->packed->cache->root->flag &= ~REF_INCOMPLETE; 391 f =fopen(packed_refs_file,"r"); 392if(f) { 393stat_validity_update(&refs->packed->validity,fileno(f)); 394read_packed_refs(f,get_ref_dir(refs->packed->cache->root)); 395fclose(f); 396} 397} 398return refs->packed; 399} 400 401static struct ref_dir *get_packed_ref_dir(struct packed_ref_cache *packed_ref_cache) 402{ 403returnget_ref_dir(packed_ref_cache->cache->root); 404} 405 406static struct ref_dir *get_packed_refs(struct files_ref_store *refs) 407{ 408returnget_packed_ref_dir(get_packed_ref_cache(refs)); 409} 410 411/* 412 * Add a reference to the in-memory packed reference cache. This may 413 * only be called while the packed-refs file is locked (see 414 * lock_packed_refs()). To actually write the packed-refs file, call 415 * commit_packed_refs(). 416 */ 417static voidadd_packed_ref(struct files_ref_store *refs, 418const char*refname,const unsigned char*sha1) 419{ 420struct packed_ref_cache *packed_ref_cache =get_packed_ref_cache(refs); 421 422if(!packed_ref_cache->lock) 423die("internal error: packed refs not locked"); 424add_ref_entry(get_packed_ref_dir(packed_ref_cache), 425create_ref_entry(refname, sha1, REF_ISPACKED,1)); 426} 427 428/* 429 * Read the loose references from the namespace dirname into dir 430 * (without recursing). dirname must end with '/'. dir must be the 431 * directory entry corresponding to dirname. 432 */ 433static voidloose_fill_ref_dir(struct ref_store *ref_store, 434struct ref_dir *dir,const char*dirname) 435{ 436struct files_ref_store *refs = 437files_downcast(ref_store, REF_STORE_READ,"fill_ref_dir"); 438DIR*d; 439struct dirent *de; 440int dirnamelen =strlen(dirname); 441struct strbuf refname; 442struct strbuf path = STRBUF_INIT; 443size_t path_baselen; 444 445files_ref_path(refs, &path, dirname); 446 path_baselen = path.len; 447 448 d =opendir(path.buf); 449if(!d) { 450strbuf_release(&path); 451return; 452} 453 454strbuf_init(&refname, dirnamelen +257); 455strbuf_add(&refname, dirname, dirnamelen); 456 457while((de =readdir(d)) != NULL) { 458unsigned char sha1[20]; 459struct stat st; 460int flag; 461 462if(de->d_name[0] =='.') 463continue; 464if(ends_with(de->d_name,".lock")) 465continue; 466strbuf_addstr(&refname, de->d_name); 467strbuf_addstr(&path, de->d_name); 468if(stat(path.buf, &st) <0) { 469;/* silently ignore */ 470}else if(S_ISDIR(st.st_mode)) { 471strbuf_addch(&refname,'/'); 472add_entry_to_dir(dir, 473create_dir_entry(dir->cache, refname.buf, 474 refname.len,1)); 475}else{ 476if(!refs_resolve_ref_unsafe(&refs->base, 477 refname.buf, 478 RESOLVE_REF_READING, 479 sha1, &flag)) { 480hashclr(sha1); 481 flag |= REF_ISBROKEN; 482}else if(is_null_sha1(sha1)) { 483/* 484 * It is so astronomically unlikely 485 * that NULL_SHA1 is the SHA-1 of an 486 * actual object that we consider its 487 * appearance in a loose reference 488 * file to be repo corruption 489 * (probably due to a software bug). 490 */ 491 flag |= REF_ISBROKEN; 492} 493 494if(check_refname_format(refname.buf, 495 REFNAME_ALLOW_ONELEVEL)) { 496if(!refname_is_safe(refname.buf)) 497die("loose refname is dangerous:%s", refname.buf); 498hashclr(sha1); 499 flag |= REF_BAD_NAME | REF_ISBROKEN; 500} 501add_entry_to_dir(dir, 502create_ref_entry(refname.buf, sha1, flag,0)); 503} 504strbuf_setlen(&refname, dirnamelen); 505strbuf_setlen(&path, path_baselen); 506} 507strbuf_release(&refname); 508strbuf_release(&path); 509closedir(d); 510} 511 512static struct ref_dir *get_loose_refs(struct files_ref_store *refs) 513{ 514if(!refs->loose) { 515/* 516 * Mark the top-level directory complete because we 517 * are about to read the only subdirectory that can 518 * hold references: 519 */ 520 refs->loose =create_ref_cache(&refs->base, loose_fill_ref_dir); 521 522/* We're going to fill the top level ourselves: */ 523 refs->loose->root->flag &= ~REF_INCOMPLETE; 524 525/* 526 * Add an incomplete entry for "refs/" (to be filled 527 * lazily): 528 */ 529add_entry_to_dir(get_ref_dir(refs->loose->root), 530create_dir_entry(refs->loose,"refs/",5,1)); 531} 532returnget_ref_dir(refs->loose->root); 533} 534 535/* 536 * Return the ref_entry for the given refname from the packed 537 * references. If it does not exist, return NULL. 538 */ 539static struct ref_entry *get_packed_ref(struct files_ref_store *refs, 540const char*refname) 541{ 542returnfind_ref_entry(get_packed_refs(refs), refname); 543} 544 545/* 546 * A loose ref file doesn't exist; check for a packed ref. 547 */ 548static intresolve_packed_ref(struct files_ref_store *refs, 549const char*refname, 550unsigned char*sha1,unsigned int*flags) 551{ 552struct ref_entry *entry; 553 554/* 555 * The loose reference file does not exist; check for a packed 556 * reference. 557 */ 558 entry =get_packed_ref(refs, refname); 559if(entry) { 560hashcpy(sha1, entry->u.value.oid.hash); 561*flags |= REF_ISPACKED; 562return0; 563} 564/* refname is not a packed reference. */ 565return-1; 566} 567 568static intfiles_read_raw_ref(struct ref_store *ref_store, 569const char*refname,unsigned char*sha1, 570struct strbuf *referent,unsigned int*type) 571{ 572struct files_ref_store *refs = 573files_downcast(ref_store, REF_STORE_READ,"read_raw_ref"); 574struct strbuf sb_contents = STRBUF_INIT; 575struct strbuf sb_path = STRBUF_INIT; 576const char*path; 577const char*buf; 578struct stat st; 579int fd; 580int ret = -1; 581int save_errno; 582int remaining_retries =3; 583 584*type =0; 585strbuf_reset(&sb_path); 586 587files_ref_path(refs, &sb_path, refname); 588 589 path = sb_path.buf; 590 591stat_ref: 592/* 593 * We might have to loop back here to avoid a race 594 * condition: first we lstat() the file, then we try 595 * to read it as a link or as a file. But if somebody 596 * changes the type of the file (file <-> directory 597 * <-> symlink) between the lstat() and reading, then 598 * we don't want to report that as an error but rather 599 * try again starting with the lstat(). 600 * 601 * We'll keep a count of the retries, though, just to avoid 602 * any confusing situation sending us into an infinite loop. 603 */ 604 605if(remaining_retries-- <=0) 606goto out; 607 608if(lstat(path, &st) <0) { 609if(errno != ENOENT) 610goto out; 611if(resolve_packed_ref(refs, refname, sha1, type)) { 612 errno = ENOENT; 613goto out; 614} 615 ret =0; 616goto out; 617} 618 619/* Follow "normalized" - ie "refs/.." symlinks by hand */ 620if(S_ISLNK(st.st_mode)) { 621strbuf_reset(&sb_contents); 622if(strbuf_readlink(&sb_contents, path,0) <0) { 623if(errno == ENOENT || errno == EINVAL) 624/* inconsistent with lstat; retry */ 625goto stat_ref; 626else 627goto out; 628} 629if(starts_with(sb_contents.buf,"refs/") && 630!check_refname_format(sb_contents.buf,0)) { 631strbuf_swap(&sb_contents, referent); 632*type |= REF_ISSYMREF; 633 ret =0; 634goto out; 635} 636/* 637 * It doesn't look like a refname; fall through to just 638 * treating it like a non-symlink, and reading whatever it 639 * points to. 640 */ 641} 642 643/* Is it a directory? */ 644if(S_ISDIR(st.st_mode)) { 645/* 646 * Even though there is a directory where the loose 647 * ref is supposed to be, there could still be a 648 * packed ref: 649 */ 650if(resolve_packed_ref(refs, refname, sha1, type)) { 651 errno = EISDIR; 652goto out; 653} 654 ret =0; 655goto out; 656} 657 658/* 659 * Anything else, just open it and try to use it as 660 * a ref 661 */ 662 fd =open(path, O_RDONLY); 663if(fd <0) { 664if(errno == ENOENT && !S_ISLNK(st.st_mode)) 665/* inconsistent with lstat; retry */ 666goto stat_ref; 667else 668goto out; 669} 670strbuf_reset(&sb_contents); 671if(strbuf_read(&sb_contents, fd,256) <0) { 672int save_errno = errno; 673close(fd); 674 errno = save_errno; 675goto out; 676} 677close(fd); 678strbuf_rtrim(&sb_contents); 679 buf = sb_contents.buf; 680if(starts_with(buf,"ref:")) { 681 buf +=4; 682while(isspace(*buf)) 683 buf++; 684 685strbuf_reset(referent); 686strbuf_addstr(referent, buf); 687*type |= REF_ISSYMREF; 688 ret =0; 689goto out; 690} 691 692/* 693 * Please note that FETCH_HEAD has additional 694 * data after the sha. 695 */ 696if(get_sha1_hex(buf, sha1) || 697(buf[40] !='\0'&& !isspace(buf[40]))) { 698*type |= REF_ISBROKEN; 699 errno = EINVAL; 700goto out; 701} 702 703 ret =0; 704 705out: 706 save_errno = errno; 707strbuf_release(&sb_path); 708strbuf_release(&sb_contents); 709 errno = save_errno; 710return ret; 711} 712 713static voidunlock_ref(struct ref_lock *lock) 714{ 715/* Do not free lock->lk -- atexit() still looks at them */ 716if(lock->lk) 717rollback_lock_file(lock->lk); 718free(lock->ref_name); 719free(lock); 720} 721 722/* 723 * Lock refname, without following symrefs, and set *lock_p to point 724 * at a newly-allocated lock object. Fill in lock->old_oid, referent, 725 * and type similarly to read_raw_ref(). 726 * 727 * The caller must verify that refname is a "safe" reference name (in 728 * the sense of refname_is_safe()) before calling this function. 729 * 730 * If the reference doesn't already exist, verify that refname doesn't 731 * have a D/F conflict with any existing references. extras and skip 732 * are passed to refs_verify_refname_available() for this check. 733 * 734 * If mustexist is not set and the reference is not found or is 735 * broken, lock the reference anyway but clear sha1. 736 * 737 * Return 0 on success. On failure, write an error message to err and 738 * return TRANSACTION_NAME_CONFLICT or TRANSACTION_GENERIC_ERROR. 739 * 740 * Implementation note: This function is basically 741 * 742 * lock reference 743 * read_raw_ref() 744 * 745 * but it includes a lot more code to 746 * - Deal with possible races with other processes 747 * - Avoid calling refs_verify_refname_available() when it can be 748 * avoided, namely if we were successfully able to read the ref 749 * - Generate informative error messages in the case of failure 750 */ 751static intlock_raw_ref(struct files_ref_store *refs, 752const char*refname,int mustexist, 753const struct string_list *extras, 754const struct string_list *skip, 755struct ref_lock **lock_p, 756struct strbuf *referent, 757unsigned int*type, 758struct strbuf *err) 759{ 760struct ref_lock *lock; 761struct strbuf ref_file = STRBUF_INIT; 762int attempts_remaining =3; 763int ret = TRANSACTION_GENERIC_ERROR; 764 765assert(err); 766files_assert_main_repository(refs,"lock_raw_ref"); 767 768*type =0; 769 770/* First lock the file so it can't change out from under us. */ 771 772*lock_p = lock =xcalloc(1,sizeof(*lock)); 773 774 lock->ref_name =xstrdup(refname); 775files_ref_path(refs, &ref_file, refname); 776 777retry: 778switch(safe_create_leading_directories(ref_file.buf)) { 779case SCLD_OK: 780break;/* success */ 781case SCLD_EXISTS: 782/* 783 * Suppose refname is "refs/foo/bar". We just failed 784 * to create the containing directory, "refs/foo", 785 * because there was a non-directory in the way. This 786 * indicates a D/F conflict, probably because of 787 * another reference such as "refs/foo". There is no 788 * reason to expect this error to be transitory. 789 */ 790if(refs_verify_refname_available(&refs->base, refname, 791 extras, skip, err)) { 792if(mustexist) { 793/* 794 * To the user the relevant error is 795 * that the "mustexist" reference is 796 * missing: 797 */ 798strbuf_reset(err); 799strbuf_addf(err,"unable to resolve reference '%s'", 800 refname); 801}else{ 802/* 803 * The error message set by 804 * refs_verify_refname_available() is 805 * OK. 806 */ 807 ret = TRANSACTION_NAME_CONFLICT; 808} 809}else{ 810/* 811 * The file that is in the way isn't a loose 812 * reference. Report it as a low-level 813 * failure. 814 */ 815strbuf_addf(err,"unable to create lock file%s.lock; " 816"non-directory in the way", 817 ref_file.buf); 818} 819goto error_return; 820case SCLD_VANISHED: 821/* Maybe another process was tidying up. Try again. */ 822if(--attempts_remaining >0) 823goto retry; 824/* fall through */ 825default: 826strbuf_addf(err,"unable to create directory for%s", 827 ref_file.buf); 828goto error_return; 829} 830 831if(!lock->lk) 832 lock->lk =xcalloc(1,sizeof(struct lock_file)); 833 834if(hold_lock_file_for_update(lock->lk, ref_file.buf, LOCK_NO_DEREF) <0) { 835if(errno == ENOENT && --attempts_remaining >0) { 836/* 837 * Maybe somebody just deleted one of the 838 * directories leading to ref_file. Try 839 * again: 840 */ 841goto retry; 842}else{ 843unable_to_lock_message(ref_file.buf, errno, err); 844goto error_return; 845} 846} 847 848/* 849 * Now we hold the lock and can read the reference without 850 * fear that its value will change. 851 */ 852 853if(files_read_raw_ref(&refs->base, refname, 854 lock->old_oid.hash, referent, type)) { 855if(errno == ENOENT) { 856if(mustexist) { 857/* Garden variety missing reference. */ 858strbuf_addf(err,"unable to resolve reference '%s'", 859 refname); 860goto error_return; 861}else{ 862/* 863 * Reference is missing, but that's OK. We 864 * know that there is not a conflict with 865 * another loose reference because 866 * (supposing that we are trying to lock 867 * reference "refs/foo/bar"): 868 * 869 * - We were successfully able to create 870 * the lockfile refs/foo/bar.lock, so we 871 * know there cannot be a loose reference 872 * named "refs/foo". 873 * 874 * - We got ENOENT and not EISDIR, so we 875 * know that there cannot be a loose 876 * reference named "refs/foo/bar/baz". 877 */ 878} 879}else if(errno == EISDIR) { 880/* 881 * There is a directory in the way. It might have 882 * contained references that have been deleted. If 883 * we don't require that the reference already 884 * exists, try to remove the directory so that it 885 * doesn't cause trouble when we want to rename the 886 * lockfile into place later. 887 */ 888if(mustexist) { 889/* Garden variety missing reference. */ 890strbuf_addf(err,"unable to resolve reference '%s'", 891 refname); 892goto error_return; 893}else if(remove_dir_recursively(&ref_file, 894 REMOVE_DIR_EMPTY_ONLY)) { 895if(refs_verify_refname_available( 896&refs->base, refname, 897 extras, skip, err)) { 898/* 899 * The error message set by 900 * verify_refname_available() is OK. 901 */ 902 ret = TRANSACTION_NAME_CONFLICT; 903goto error_return; 904}else{ 905/* 906 * We can't delete the directory, 907 * but we also don't know of any 908 * references that it should 909 * contain. 910 */ 911strbuf_addf(err,"there is a non-empty directory '%s' " 912"blocking reference '%s'", 913 ref_file.buf, refname); 914goto error_return; 915} 916} 917}else if(errno == EINVAL && (*type & REF_ISBROKEN)) { 918strbuf_addf(err,"unable to resolve reference '%s': " 919"reference broken", refname); 920goto error_return; 921}else{ 922strbuf_addf(err,"unable to resolve reference '%s':%s", 923 refname,strerror(errno)); 924goto error_return; 925} 926 927/* 928 * If the ref did not exist and we are creating it, 929 * make sure there is no existing ref that conflicts 930 * with refname: 931 */ 932if(refs_verify_refname_available( 933&refs->base, refname, 934 extras, skip, err)) 935goto error_return; 936} 937 938 ret =0; 939goto out; 940 941error_return: 942unlock_ref(lock); 943*lock_p = NULL; 944 945out: 946strbuf_release(&ref_file); 947return ret; 948} 949 950static intfiles_peel_ref(struct ref_store *ref_store, 951const char*refname,unsigned char*sha1) 952{ 953struct files_ref_store *refs = 954files_downcast(ref_store, REF_STORE_READ | REF_STORE_ODB, 955"peel_ref"); 956int flag; 957unsigned char base[20]; 958 959if(current_ref_iter && current_ref_iter->refname == refname) { 960struct object_id peeled; 961 962if(ref_iterator_peel(current_ref_iter, &peeled)) 963return-1; 964hashcpy(sha1, peeled.hash); 965return0; 966} 967 968if(refs_read_ref_full(ref_store, refname, 969 RESOLVE_REF_READING, base, &flag)) 970return-1; 971 972/* 973 * If the reference is packed, read its ref_entry from the 974 * cache in the hope that we already know its peeled value. 975 * We only try this optimization on packed references because 976 * (a) forcing the filling of the loose reference cache could 977 * be expensive and (b) loose references anyway usually do not 978 * have REF_KNOWS_PEELED. 979 */ 980if(flag & REF_ISPACKED) { 981struct ref_entry *r =get_packed_ref(refs, refname); 982if(r) { 983if(peel_entry(r,0)) 984return-1; 985hashcpy(sha1, r->u.value.peeled.hash); 986return0; 987} 988} 989 990returnpeel_object(base, sha1); 991} 992 993struct files_ref_iterator { 994struct ref_iterator base; 995 996struct packed_ref_cache *packed_ref_cache; 997struct ref_iterator *iter0; 998unsigned int flags; 999};10001001static intfiles_ref_iterator_advance(struct ref_iterator *ref_iterator)1002{1003struct files_ref_iterator *iter =1004(struct files_ref_iterator *)ref_iterator;1005int ok;10061007while((ok =ref_iterator_advance(iter->iter0)) == ITER_OK) {1008if(iter->flags & DO_FOR_EACH_PER_WORKTREE_ONLY &&1009ref_type(iter->iter0->refname) != REF_TYPE_PER_WORKTREE)1010continue;10111012if(!(iter->flags & DO_FOR_EACH_INCLUDE_BROKEN) &&1013!ref_resolves_to_object(iter->iter0->refname,1014 iter->iter0->oid,1015 iter->iter0->flags))1016continue;10171018 iter->base.refname = iter->iter0->refname;1019 iter->base.oid = iter->iter0->oid;1020 iter->base.flags = iter->iter0->flags;1021return ITER_OK;1022}10231024 iter->iter0 = NULL;1025if(ref_iterator_abort(ref_iterator) != ITER_DONE)1026 ok = ITER_ERROR;10271028return ok;1029}10301031static intfiles_ref_iterator_peel(struct ref_iterator *ref_iterator,1032struct object_id *peeled)1033{1034struct files_ref_iterator *iter =1035(struct files_ref_iterator *)ref_iterator;10361037returnref_iterator_peel(iter->iter0, peeled);1038}10391040static intfiles_ref_iterator_abort(struct ref_iterator *ref_iterator)1041{1042struct files_ref_iterator *iter =1043(struct files_ref_iterator *)ref_iterator;1044int ok = ITER_DONE;10451046if(iter->iter0)1047 ok =ref_iterator_abort(iter->iter0);10481049release_packed_ref_cache(iter->packed_ref_cache);1050base_ref_iterator_free(ref_iterator);1051return ok;1052}10531054static struct ref_iterator_vtable files_ref_iterator_vtable = {1055 files_ref_iterator_advance,1056 files_ref_iterator_peel,1057 files_ref_iterator_abort1058};10591060static struct ref_iterator *files_ref_iterator_begin(1061struct ref_store *ref_store,1062const char*prefix,unsigned int flags)1063{1064struct files_ref_store *refs;1065struct ref_dir *loose_dir, *packed_dir;1066struct ref_iterator *loose_iter, *packed_iter;1067struct files_ref_iterator *iter;1068struct ref_iterator *ref_iterator;10691070if(ref_paranoia <0)1071 ref_paranoia =git_env_bool("GIT_REF_PARANOIA",0);1072if(ref_paranoia)1073 flags |= DO_FOR_EACH_INCLUDE_BROKEN;10741075 refs =files_downcast(ref_store,1076 REF_STORE_READ | (ref_paranoia ?0: REF_STORE_ODB),1077"ref_iterator_begin");10781079 iter =xcalloc(1,sizeof(*iter));1080 ref_iterator = &iter->base;1081base_ref_iterator_init(ref_iterator, &files_ref_iterator_vtable);10821083/*1084 * We must make sure that all loose refs are read before1085 * accessing the packed-refs file; this avoids a race1086 * condition if loose refs are migrated to the packed-refs1087 * file by a simultaneous process, but our in-memory view is1088 * from before the migration. We ensure this as follows:1089 * First, we call prime_ref_dir(), which pre-reads the loose1090 * references for the subtree into the cache. (If they've1091 * already been read, that's OK; we only need to guarantee1092 * that they're read before the packed refs, not *how much*1093 * before.) After that, we call get_packed_ref_cache(), which1094 * internally checks whether the packed-ref cache is up to1095 * date with what is on disk, and re-reads it if not.1096 */10971098 loose_dir =get_loose_refs(refs);10991100if(prefix && *prefix)1101 loose_dir =find_containing_dir(loose_dir, prefix,0);11021103if(loose_dir) {1104prime_ref_dir(loose_dir);1105 loose_iter =cache_ref_iterator_begin(loose_dir);1106}else{1107/* There's nothing to iterate over. */1108 loose_iter =empty_ref_iterator_begin();1109}11101111 iter->packed_ref_cache =get_packed_ref_cache(refs);1112acquire_packed_ref_cache(iter->packed_ref_cache);1113 packed_dir =get_packed_ref_dir(iter->packed_ref_cache);11141115if(prefix && *prefix)1116 packed_dir =find_containing_dir(packed_dir, prefix,0);11171118if(packed_dir) {1119 packed_iter =cache_ref_iterator_begin(packed_dir);1120}else{1121/* There's nothing to iterate over. */1122 packed_iter =empty_ref_iterator_begin();1123}11241125 iter->iter0 =overlay_ref_iterator_begin(loose_iter, packed_iter);1126 iter->flags = flags;11271128return ref_iterator;1129}11301131/*1132 * Verify that the reference locked by lock has the value old_sha1.1133 * Fail if the reference doesn't exist and mustexist is set. Return 01134 * on success. On error, write an error message to err, set errno, and1135 * return a negative value.1136 */1137static intverify_lock(struct ref_store *ref_store,struct ref_lock *lock,1138const unsigned char*old_sha1,int mustexist,1139struct strbuf *err)1140{1141assert(err);11421143if(refs_read_ref_full(ref_store, lock->ref_name,1144 mustexist ? RESOLVE_REF_READING :0,1145 lock->old_oid.hash, NULL)) {1146if(old_sha1) {1147int save_errno = errno;1148strbuf_addf(err,"can't verify ref '%s'", lock->ref_name);1149 errno = save_errno;1150return-1;1151}else{1152oidclr(&lock->old_oid);1153return0;1154}1155}1156if(old_sha1 &&hashcmp(lock->old_oid.hash, old_sha1)) {1157strbuf_addf(err,"ref '%s' is at%sbut expected%s",1158 lock->ref_name,1159oid_to_hex(&lock->old_oid),1160sha1_to_hex(old_sha1));1161 errno = EBUSY;1162return-1;1163}1164return0;1165}11661167static intremove_empty_directories(struct strbuf *path)1168{1169/*1170 * we want to create a file but there is a directory there;1171 * if that is an empty directory (or a directory that contains1172 * only empty directories), remove them.1173 */1174returnremove_dir_recursively(path, REMOVE_DIR_EMPTY_ONLY);1175}11761177static intcreate_reflock(const char*path,void*cb)1178{1179struct lock_file *lk = cb;11801181returnhold_lock_file_for_update(lk, path, LOCK_NO_DEREF) <0? -1:0;1182}11831184/*1185 * Locks a ref returning the lock on success and NULL on failure.1186 * On failure errno is set to something meaningful.1187 */1188static struct ref_lock *lock_ref_sha1_basic(struct files_ref_store *refs,1189const char*refname,1190const unsigned char*old_sha1,1191const struct string_list *extras,1192const struct string_list *skip,1193unsigned int flags,int*type,1194struct strbuf *err)1195{1196struct strbuf ref_file = STRBUF_INIT;1197struct ref_lock *lock;1198int last_errno =0;1199int mustexist = (old_sha1 && !is_null_sha1(old_sha1));1200int resolve_flags = RESOLVE_REF_NO_RECURSE;1201int resolved;12021203files_assert_main_repository(refs,"lock_ref_sha1_basic");1204assert(err);12051206 lock =xcalloc(1,sizeof(struct ref_lock));12071208if(mustexist)1209 resolve_flags |= RESOLVE_REF_READING;1210if(flags & REF_DELETING)1211 resolve_flags |= RESOLVE_REF_ALLOW_BAD_NAME;12121213files_ref_path(refs, &ref_file, refname);1214 resolved = !!refs_resolve_ref_unsafe(&refs->base,1215 refname, resolve_flags,1216 lock->old_oid.hash, type);1217if(!resolved && errno == EISDIR) {1218/*1219 * we are trying to lock foo but we used to1220 * have foo/bar which now does not exist;1221 * it is normal for the empty directory 'foo'1222 * to remain.1223 */1224if(remove_empty_directories(&ref_file)) {1225 last_errno = errno;1226if(!refs_verify_refname_available(1227&refs->base,1228 refname, extras, skip, err))1229strbuf_addf(err,"there are still refs under '%s'",1230 refname);1231goto error_return;1232}1233 resolved = !!refs_resolve_ref_unsafe(&refs->base,1234 refname, resolve_flags,1235 lock->old_oid.hash, type);1236}1237if(!resolved) {1238 last_errno = errno;1239if(last_errno != ENOTDIR ||1240!refs_verify_refname_available(&refs->base, refname,1241 extras, skip, err))1242strbuf_addf(err,"unable to resolve reference '%s':%s",1243 refname,strerror(last_errno));12441245goto error_return;1246}12471248/*1249 * If the ref did not exist and we are creating it, make sure1250 * there is no existing packed ref whose name begins with our1251 * refname, nor a packed ref whose name is a proper prefix of1252 * our refname.1253 */1254if(is_null_oid(&lock->old_oid) &&1255refs_verify_refname_available(&refs->base, refname,1256 extras, skip, err)) {1257 last_errno = ENOTDIR;1258goto error_return;1259}12601261 lock->lk =xcalloc(1,sizeof(struct lock_file));12621263 lock->ref_name =xstrdup(refname);12641265if(raceproof_create_file(ref_file.buf, create_reflock, lock->lk)) {1266 last_errno = errno;1267unable_to_lock_message(ref_file.buf, errno, err);1268goto error_return;1269}12701271if(verify_lock(&refs->base, lock, old_sha1, mustexist, err)) {1272 last_errno = errno;1273goto error_return;1274}1275goto out;12761277 error_return:1278unlock_ref(lock);1279 lock = NULL;12801281 out:1282strbuf_release(&ref_file);1283 errno = last_errno;1284return lock;1285}12861287/*1288 * Write an entry to the packed-refs file for the specified refname.1289 * If peeled is non-NULL, write it as the entry's peeled value.1290 */1291static voidwrite_packed_entry(FILE*fh,char*refname,unsigned char*sha1,1292unsigned char*peeled)1293{1294fprintf_or_die(fh,"%s %s\n",sha1_to_hex(sha1), refname);1295if(peeled)1296fprintf_or_die(fh,"^%s\n",sha1_to_hex(peeled));1297}12981299/*1300 * An each_ref_entry_fn that writes the entry to a packed-refs file.1301 */1302static intwrite_packed_entry_fn(struct ref_entry *entry,void*cb_data)1303{1304enum peel_status peel_status =peel_entry(entry,0);13051306if(peel_status != PEEL_PEELED && peel_status != PEEL_NON_TAG)1307error("internal error:%sis not a valid packed reference!",1308 entry->name);1309write_packed_entry(cb_data, entry->name, entry->u.value.oid.hash,1310 peel_status == PEEL_PEELED ?1311 entry->u.value.peeled.hash : NULL);1312return0;1313}13141315/*1316 * Lock the packed-refs file for writing. Flags is passed to1317 * hold_lock_file_for_update(). Return 0 on success. On errors, set1318 * errno appropriately and return a nonzero value.1319 */1320static intlock_packed_refs(struct files_ref_store *refs,int flags)1321{1322static int timeout_configured =0;1323static int timeout_value =1000;1324struct packed_ref_cache *packed_ref_cache;13251326files_assert_main_repository(refs,"lock_packed_refs");13271328if(!timeout_configured) {1329git_config_get_int("core.packedrefstimeout", &timeout_value);1330 timeout_configured =1;1331}13321333if(hold_lock_file_for_update_timeout(1334&packlock,files_packed_refs_path(refs),1335 flags, timeout_value) <0)1336return-1;1337/*1338 * Get the current packed-refs while holding the lock. If the1339 * packed-refs file has been modified since we last read it,1340 * this will automatically invalidate the cache and re-read1341 * the packed-refs file.1342 */1343 packed_ref_cache =get_packed_ref_cache(refs);1344 packed_ref_cache->lock = &packlock;1345/* Increment the reference count to prevent it from being freed: */1346acquire_packed_ref_cache(packed_ref_cache);1347return0;1348}13491350/*1351 * Write the current version of the packed refs cache from memory to1352 * disk. The packed-refs file must already be locked for writing (see1353 * lock_packed_refs()). Return zero on success. On errors, set errno1354 * and return a nonzero value1355 */1356static intcommit_packed_refs(struct files_ref_store *refs)1357{1358struct packed_ref_cache *packed_ref_cache =1359get_packed_ref_cache(refs);1360int error =0;1361int save_errno =0;1362FILE*out;13631364files_assert_main_repository(refs,"commit_packed_refs");13651366if(!packed_ref_cache->lock)1367die("internal error: packed-refs not locked");13681369 out =fdopen_lock_file(packed_ref_cache->lock,"w");1370if(!out)1371die_errno("unable to fdopen packed-refs descriptor");13721373fprintf_or_die(out,"%s", PACKED_REFS_HEADER);1374do_for_each_entry_in_dir(get_packed_ref_dir(packed_ref_cache),13750, write_packed_entry_fn, out);13761377if(commit_lock_file(packed_ref_cache->lock)) {1378 save_errno = errno;1379 error = -1;1380}1381 packed_ref_cache->lock = NULL;1382release_packed_ref_cache(packed_ref_cache);1383 errno = save_errno;1384return error;1385}13861387/*1388 * Rollback the lockfile for the packed-refs file, and discard the1389 * in-memory packed reference cache. (The packed-refs file will be1390 * read anew if it is needed again after this function is called.)1391 */1392static voidrollback_packed_refs(struct files_ref_store *refs)1393{1394struct packed_ref_cache *packed_ref_cache =1395get_packed_ref_cache(refs);13961397files_assert_main_repository(refs,"rollback_packed_refs");13981399if(!packed_ref_cache->lock)1400die("internal error: packed-refs not locked");1401rollback_lock_file(packed_ref_cache->lock);1402 packed_ref_cache->lock = NULL;1403release_packed_ref_cache(packed_ref_cache);1404clear_packed_ref_cache(refs);1405}14061407struct ref_to_prune {1408struct ref_to_prune *next;1409unsigned char sha1[20];1410char name[FLEX_ARRAY];1411};14121413struct pack_refs_cb_data {1414unsigned int flags;1415struct ref_dir *packed_refs;1416struct ref_to_prune *ref_to_prune;1417};14181419/*1420 * An each_ref_entry_fn that is run over loose references only. If1421 * the loose reference can be packed, add an entry in the packed ref1422 * cache. If the reference should be pruned, also add it to1423 * ref_to_prune in the pack_refs_cb_data.1424 */1425static intpack_if_possible_fn(struct ref_entry *entry,void*cb_data)1426{1427struct pack_refs_cb_data *cb = cb_data;1428enum peel_status peel_status;1429struct ref_entry *packed_entry;1430int is_tag_ref =starts_with(entry->name,"refs/tags/");14311432/* Do not pack per-worktree refs: */1433if(ref_type(entry->name) != REF_TYPE_NORMAL)1434return0;14351436/* ALWAYS pack tags */1437if(!(cb->flags & PACK_REFS_ALL) && !is_tag_ref)1438return0;14391440/* Do not pack symbolic or broken refs: */1441if((entry->flag & REF_ISSYMREF) || !entry_resolves_to_object(entry))1442return0;14431444/* Add a packed ref cache entry equivalent to the loose entry. */1445 peel_status =peel_entry(entry,1);1446if(peel_status != PEEL_PEELED && peel_status != PEEL_NON_TAG)1447die("internal error peeling reference%s(%s)",1448 entry->name,oid_to_hex(&entry->u.value.oid));1449 packed_entry =find_ref_entry(cb->packed_refs, entry->name);1450if(packed_entry) {1451/* Overwrite existing packed entry with info from loose entry */1452 packed_entry->flag = REF_ISPACKED | REF_KNOWS_PEELED;1453oidcpy(&packed_entry->u.value.oid, &entry->u.value.oid);1454}else{1455 packed_entry =create_ref_entry(entry->name, entry->u.value.oid.hash,1456 REF_ISPACKED | REF_KNOWS_PEELED,0);1457add_ref_entry(cb->packed_refs, packed_entry);1458}1459oidcpy(&packed_entry->u.value.peeled, &entry->u.value.peeled);14601461/* Schedule the loose reference for pruning if requested. */1462if((cb->flags & PACK_REFS_PRUNE)) {1463struct ref_to_prune *n;1464FLEX_ALLOC_STR(n, name, entry->name);1465hashcpy(n->sha1, entry->u.value.oid.hash);1466 n->next = cb->ref_to_prune;1467 cb->ref_to_prune = n;1468}1469return0;1470}14711472enum{1473 REMOVE_EMPTY_PARENTS_REF =0x01,1474 REMOVE_EMPTY_PARENTS_REFLOG =0x021475};14761477/*1478 * Remove empty parent directories associated with the specified1479 * reference and/or its reflog, but spare [logs/]refs/ and immediate1480 * subdirs. flags is a combination of REMOVE_EMPTY_PARENTS_REF and/or1481 * REMOVE_EMPTY_PARENTS_REFLOG.1482 */1483static voidtry_remove_empty_parents(struct files_ref_store *refs,1484const char*refname,1485unsigned int flags)1486{1487struct strbuf buf = STRBUF_INIT;1488struct strbuf sb = STRBUF_INIT;1489char*p, *q;1490int i;14911492strbuf_addstr(&buf, refname);1493 p = buf.buf;1494for(i =0; i <2; i++) {/* refs/{heads,tags,...}/ */1495while(*p && *p !='/')1496 p++;1497/* tolerate duplicate slashes; see check_refname_format() */1498while(*p =='/')1499 p++;1500}1501 q = buf.buf + buf.len;1502while(flags & (REMOVE_EMPTY_PARENTS_REF | REMOVE_EMPTY_PARENTS_REFLOG)) {1503while(q > p && *q !='/')1504 q--;1505while(q > p && *(q-1) =='/')1506 q--;1507if(q == p)1508break;1509strbuf_setlen(&buf, q - buf.buf);15101511strbuf_reset(&sb);1512files_ref_path(refs, &sb, buf.buf);1513if((flags & REMOVE_EMPTY_PARENTS_REF) &&rmdir(sb.buf))1514 flags &= ~REMOVE_EMPTY_PARENTS_REF;15151516strbuf_reset(&sb);1517files_reflog_path(refs, &sb, buf.buf);1518if((flags & REMOVE_EMPTY_PARENTS_REFLOG) &&rmdir(sb.buf))1519 flags &= ~REMOVE_EMPTY_PARENTS_REFLOG;1520}1521strbuf_release(&buf);1522strbuf_release(&sb);1523}15241525/* make sure nobody touched the ref, and unlink */1526static voidprune_ref(struct files_ref_store *refs,struct ref_to_prune *r)1527{1528struct ref_transaction *transaction;1529struct strbuf err = STRBUF_INIT;15301531if(check_refname_format(r->name,0))1532return;15331534 transaction =ref_store_transaction_begin(&refs->base, &err);1535if(!transaction ||1536ref_transaction_delete(transaction, r->name, r->sha1,1537 REF_ISPRUNING | REF_NODEREF, NULL, &err) ||1538ref_transaction_commit(transaction, &err)) {1539ref_transaction_free(transaction);1540error("%s", err.buf);1541strbuf_release(&err);1542return;1543}1544ref_transaction_free(transaction);1545strbuf_release(&err);1546}15471548static voidprune_refs(struct files_ref_store *refs,struct ref_to_prune *r)1549{1550while(r) {1551prune_ref(refs, r);1552 r = r->next;1553}1554}15551556static intfiles_pack_refs(struct ref_store *ref_store,unsigned int flags)1557{1558struct files_ref_store *refs =1559files_downcast(ref_store, REF_STORE_WRITE | REF_STORE_ODB,1560"pack_refs");1561struct pack_refs_cb_data cbdata;15621563memset(&cbdata,0,sizeof(cbdata));1564 cbdata.flags = flags;15651566lock_packed_refs(refs, LOCK_DIE_ON_ERROR);1567 cbdata.packed_refs =get_packed_refs(refs);15681569do_for_each_entry_in_dir(get_loose_refs(refs),0,1570 pack_if_possible_fn, &cbdata);15711572if(commit_packed_refs(refs))1573die_errno("unable to overwrite old ref-pack file");15741575prune_refs(refs, cbdata.ref_to_prune);1576return0;1577}15781579/*1580 * Rewrite the packed-refs file, omitting any refs listed in1581 * 'refnames'. On error, leave packed-refs unchanged, write an error1582 * message to 'err', and return a nonzero value.1583 *1584 * The refs in 'refnames' needn't be sorted. `err` must not be NULL.1585 */1586static intrepack_without_refs(struct files_ref_store *refs,1587struct string_list *refnames,struct strbuf *err)1588{1589struct ref_dir *packed;1590struct string_list_item *refname;1591int ret, needs_repacking =0, removed =0;15921593files_assert_main_repository(refs,"repack_without_refs");1594assert(err);15951596/* Look for a packed ref */1597for_each_string_list_item(refname, refnames) {1598if(get_packed_ref(refs, refname->string)) {1599 needs_repacking =1;1600break;1601}1602}16031604/* Avoid locking if we have nothing to do */1605if(!needs_repacking)1606return0;/* no refname exists in packed refs */16071608if(lock_packed_refs(refs,0)) {1609unable_to_lock_message(files_packed_refs_path(refs), errno, err);1610return-1;1611}1612 packed =get_packed_refs(refs);16131614/* Remove refnames from the cache */1615for_each_string_list_item(refname, refnames)1616if(remove_entry_from_dir(packed, refname->string) != -1)1617 removed =1;1618if(!removed) {1619/*1620 * All packed entries disappeared while we were1621 * acquiring the lock.1622 */1623rollback_packed_refs(refs);1624return0;1625}16261627/* Write what remains */1628 ret =commit_packed_refs(refs);1629if(ret)1630strbuf_addf(err,"unable to overwrite old ref-pack file:%s",1631strerror(errno));1632return ret;1633}16341635static intfiles_delete_refs(struct ref_store *ref_store,1636struct string_list *refnames,unsigned int flags)1637{1638struct files_ref_store *refs =1639files_downcast(ref_store, REF_STORE_WRITE,"delete_refs");1640struct strbuf err = STRBUF_INIT;1641int i, result =0;16421643if(!refnames->nr)1644return0;16451646 result =repack_without_refs(refs, refnames, &err);1647if(result) {1648/*1649 * If we failed to rewrite the packed-refs file, then1650 * it is unsafe to try to remove loose refs, because1651 * doing so might expose an obsolete packed value for1652 * a reference that might even point at an object that1653 * has been garbage collected.1654 */1655if(refnames->nr ==1)1656error(_("could not delete reference%s:%s"),1657 refnames->items[0].string, err.buf);1658else1659error(_("could not delete references:%s"), err.buf);16601661goto out;1662}16631664for(i =0; i < refnames->nr; i++) {1665const char*refname = refnames->items[i].string;16661667if(refs_delete_ref(&refs->base, NULL, refname, NULL, flags))1668 result |=error(_("could not remove reference%s"), refname);1669}16701671out:1672strbuf_release(&err);1673return result;1674}16751676/*1677 * People using contrib's git-new-workdir have .git/logs/refs ->1678 * /some/other/path/.git/logs/refs, and that may live on another device.1679 *1680 * IOW, to avoid cross device rename errors, the temporary renamed log must1681 * live into logs/refs.1682 */1683#define TMP_RENAMED_LOG"refs/.tmp-renamed-log"16841685struct rename_cb {1686const char*tmp_renamed_log;1687int true_errno;1688};16891690static intrename_tmp_log_callback(const char*path,void*cb_data)1691{1692struct rename_cb *cb = cb_data;16931694if(rename(cb->tmp_renamed_log, path)) {1695/*1696 * rename(a, b) when b is an existing directory ought1697 * to result in ISDIR, but Solaris 5.8 gives ENOTDIR.1698 * Sheesh. Record the true errno for error reporting,1699 * but report EISDIR to raceproof_create_file() so1700 * that it knows to retry.1701 */1702 cb->true_errno = errno;1703if(errno == ENOTDIR)1704 errno = EISDIR;1705return-1;1706}else{1707return0;1708}1709}17101711static intrename_tmp_log(struct files_ref_store *refs,const char*newrefname)1712{1713struct strbuf path = STRBUF_INIT;1714struct strbuf tmp = STRBUF_INIT;1715struct rename_cb cb;1716int ret;17171718files_reflog_path(refs, &path, newrefname);1719files_reflog_path(refs, &tmp, TMP_RENAMED_LOG);1720 cb.tmp_renamed_log = tmp.buf;1721 ret =raceproof_create_file(path.buf, rename_tmp_log_callback, &cb);1722if(ret) {1723if(errno == EISDIR)1724error("directory not empty:%s", path.buf);1725else1726error("unable to move logfile%sto%s:%s",1727 tmp.buf, path.buf,1728strerror(cb.true_errno));1729}17301731strbuf_release(&path);1732strbuf_release(&tmp);1733return ret;1734}17351736static intwrite_ref_to_lockfile(struct ref_lock *lock,1737const unsigned char*sha1,struct strbuf *err);1738static intcommit_ref_update(struct files_ref_store *refs,1739struct ref_lock *lock,1740const unsigned char*sha1,const char*logmsg,1741struct strbuf *err);17421743static intfiles_rename_ref(struct ref_store *ref_store,1744const char*oldrefname,const char*newrefname,1745const char*logmsg)1746{1747struct files_ref_store *refs =1748files_downcast(ref_store, REF_STORE_WRITE,"rename_ref");1749unsigned char sha1[20], orig_sha1[20];1750int flag =0, logmoved =0;1751struct ref_lock *lock;1752struct stat loginfo;1753struct strbuf sb_oldref = STRBUF_INIT;1754struct strbuf sb_newref = STRBUF_INIT;1755struct strbuf tmp_renamed_log = STRBUF_INIT;1756int log, ret;1757struct strbuf err = STRBUF_INIT;17581759files_reflog_path(refs, &sb_oldref, oldrefname);1760files_reflog_path(refs, &sb_newref, newrefname);1761files_reflog_path(refs, &tmp_renamed_log, TMP_RENAMED_LOG);17621763 log = !lstat(sb_oldref.buf, &loginfo);1764if(log &&S_ISLNK(loginfo.st_mode)) {1765 ret =error("reflog for%sis a symlink", oldrefname);1766goto out;1767}17681769if(!refs_resolve_ref_unsafe(&refs->base, oldrefname,1770 RESOLVE_REF_READING | RESOLVE_REF_NO_RECURSE,1771 orig_sha1, &flag)) {1772 ret =error("refname%snot found", oldrefname);1773goto out;1774}17751776if(flag & REF_ISSYMREF) {1777 ret =error("refname%sis a symbolic ref, renaming it is not supported",1778 oldrefname);1779goto out;1780}1781if(!refs_rename_ref_available(&refs->base, oldrefname, newrefname)) {1782 ret =1;1783goto out;1784}17851786if(log &&rename(sb_oldref.buf, tmp_renamed_log.buf)) {1787 ret =error("unable to move logfile logs/%sto logs/"TMP_RENAMED_LOG":%s",1788 oldrefname,strerror(errno));1789goto out;1790}17911792if(refs_delete_ref(&refs->base, logmsg, oldrefname,1793 orig_sha1, REF_NODEREF)) {1794error("unable to delete old%s", oldrefname);1795goto rollback;1796}17971798/*1799 * Since we are doing a shallow lookup, sha1 is not the1800 * correct value to pass to delete_ref as old_sha1. But that1801 * doesn't matter, because an old_sha1 check wouldn't add to1802 * the safety anyway; we want to delete the reference whatever1803 * its current value.1804 */1805if(!refs_read_ref_full(&refs->base, newrefname,1806 RESOLVE_REF_READING | RESOLVE_REF_NO_RECURSE,1807 sha1, NULL) &&1808refs_delete_ref(&refs->base, NULL, newrefname,1809 NULL, REF_NODEREF)) {1810if(errno == EISDIR) {1811struct strbuf path = STRBUF_INIT;1812int result;18131814files_ref_path(refs, &path, newrefname);1815 result =remove_empty_directories(&path);1816strbuf_release(&path);18171818if(result) {1819error("Directory not empty:%s", newrefname);1820goto rollback;1821}1822}else{1823error("unable to delete existing%s", newrefname);1824goto rollback;1825}1826}18271828if(log &&rename_tmp_log(refs, newrefname))1829goto rollback;18301831 logmoved = log;18321833 lock =lock_ref_sha1_basic(refs, newrefname, NULL, NULL, NULL,1834 REF_NODEREF, NULL, &err);1835if(!lock) {1836error("unable to rename '%s' to '%s':%s", oldrefname, newrefname, err.buf);1837strbuf_release(&err);1838goto rollback;1839}1840hashcpy(lock->old_oid.hash, orig_sha1);18411842if(write_ref_to_lockfile(lock, orig_sha1, &err) ||1843commit_ref_update(refs, lock, orig_sha1, logmsg, &err)) {1844error("unable to write current sha1 into%s:%s", newrefname, err.buf);1845strbuf_release(&err);1846goto rollback;1847}18481849 ret =0;1850goto out;18511852 rollback:1853 lock =lock_ref_sha1_basic(refs, oldrefname, NULL, NULL, NULL,1854 REF_NODEREF, NULL, &err);1855if(!lock) {1856error("unable to lock%sfor rollback:%s", oldrefname, err.buf);1857strbuf_release(&err);1858goto rollbacklog;1859}18601861 flag = log_all_ref_updates;1862 log_all_ref_updates = LOG_REFS_NONE;1863if(write_ref_to_lockfile(lock, orig_sha1, &err) ||1864commit_ref_update(refs, lock, orig_sha1, NULL, &err)) {1865error("unable to write current sha1 into%s:%s", oldrefname, err.buf);1866strbuf_release(&err);1867}1868 log_all_ref_updates = flag;18691870 rollbacklog:1871if(logmoved &&rename(sb_newref.buf, sb_oldref.buf))1872error("unable to restore logfile%sfrom%s:%s",1873 oldrefname, newrefname,strerror(errno));1874if(!logmoved && log &&1875rename(tmp_renamed_log.buf, sb_oldref.buf))1876error("unable to restore logfile%sfrom logs/"TMP_RENAMED_LOG":%s",1877 oldrefname,strerror(errno));1878 ret =1;1879 out:1880strbuf_release(&sb_newref);1881strbuf_release(&sb_oldref);1882strbuf_release(&tmp_renamed_log);18831884return ret;1885}18861887static intclose_ref(struct ref_lock *lock)1888{1889if(close_lock_file(lock->lk))1890return-1;1891return0;1892}18931894static intcommit_ref(struct ref_lock *lock)1895{1896char*path =get_locked_file_path(lock->lk);1897struct stat st;18981899if(!lstat(path, &st) &&S_ISDIR(st.st_mode)) {1900/*1901 * There is a directory at the path we want to rename1902 * the lockfile to. Hopefully it is empty; try to1903 * delete it.1904 */1905size_t len =strlen(path);1906struct strbuf sb_path = STRBUF_INIT;19071908strbuf_attach(&sb_path, path, len, len);19091910/*1911 * If this fails, commit_lock_file() will also fail1912 * and will report the problem.1913 */1914remove_empty_directories(&sb_path);1915strbuf_release(&sb_path);1916}else{1917free(path);1918}19191920if(commit_lock_file(lock->lk))1921return-1;1922return0;1923}19241925static intopen_or_create_logfile(const char*path,void*cb)1926{1927int*fd = cb;19281929*fd =open(path, O_APPEND | O_WRONLY | O_CREAT,0666);1930return(*fd <0) ? -1:0;1931}19321933/*1934 * Create a reflog for a ref. If force_create = 0, only create the1935 * reflog for certain refs (those for which should_autocreate_reflog1936 * returns non-zero). Otherwise, create it regardless of the reference1937 * name. If the logfile already existed or was created, return 0 and1938 * set *logfd to the file descriptor opened for appending to the file.1939 * If no logfile exists and we decided not to create one, return 0 and1940 * set *logfd to -1. On failure, fill in *err, set *logfd to -1, and1941 * return -1.1942 */1943static intlog_ref_setup(struct files_ref_store *refs,1944const char*refname,int force_create,1945int*logfd,struct strbuf *err)1946{1947struct strbuf logfile_sb = STRBUF_INIT;1948char*logfile;19491950files_reflog_path(refs, &logfile_sb, refname);1951 logfile =strbuf_detach(&logfile_sb, NULL);19521953if(force_create ||should_autocreate_reflog(refname)) {1954if(raceproof_create_file(logfile, open_or_create_logfile, logfd)) {1955if(errno == ENOENT)1956strbuf_addf(err,"unable to create directory for '%s': "1957"%s", logfile,strerror(errno));1958else if(errno == EISDIR)1959strbuf_addf(err,"there are still logs under '%s'",1960 logfile);1961else1962strbuf_addf(err,"unable to append to '%s':%s",1963 logfile,strerror(errno));19641965goto error;1966}1967}else{1968*logfd =open(logfile, O_APPEND | O_WRONLY,0666);1969if(*logfd <0) {1970if(errno == ENOENT || errno == EISDIR) {1971/*1972 * The logfile doesn't already exist,1973 * but that is not an error; it only1974 * means that we won't write log1975 * entries to it.1976 */1977;1978}else{1979strbuf_addf(err,"unable to append to '%s':%s",1980 logfile,strerror(errno));1981goto error;1982}1983}1984}19851986if(*logfd >=0)1987adjust_shared_perm(logfile);19881989free(logfile);1990return0;19911992error:1993free(logfile);1994return-1;1995}19961997static intfiles_create_reflog(struct ref_store *ref_store,1998const char*refname,int force_create,1999struct strbuf *err)2000{2001struct files_ref_store *refs =2002files_downcast(ref_store, REF_STORE_WRITE,"create_reflog");2003int fd;20042005if(log_ref_setup(refs, refname, force_create, &fd, err))2006return-1;20072008if(fd >=0)2009close(fd);20102011return0;2012}20132014static intlog_ref_write_fd(int fd,const unsigned char*old_sha1,2015const unsigned char*new_sha1,2016const char*committer,const char*msg)2017{2018int msglen, written;2019unsigned maxlen, len;2020char*logrec;20212022 msglen = msg ?strlen(msg) :0;2023 maxlen =strlen(committer) + msglen +100;2024 logrec =xmalloc(maxlen);2025 len =xsnprintf(logrec, maxlen,"%s %s %s\n",2026sha1_to_hex(old_sha1),2027sha1_to_hex(new_sha1),2028 committer);2029if(msglen)2030 len +=copy_reflog_msg(logrec + len -1, msg) -1;20312032 written = len <= maxlen ?write_in_full(fd, logrec, len) : -1;2033free(logrec);2034if(written != len)2035return-1;20362037return0;2038}20392040static intfiles_log_ref_write(struct files_ref_store *refs,2041const char*refname,const unsigned char*old_sha1,2042const unsigned char*new_sha1,const char*msg,2043int flags,struct strbuf *err)2044{2045int logfd, result;20462047if(log_all_ref_updates == LOG_REFS_UNSET)2048 log_all_ref_updates =is_bare_repository() ? LOG_REFS_NONE : LOG_REFS_NORMAL;20492050 result =log_ref_setup(refs, refname,2051 flags & REF_FORCE_CREATE_REFLOG,2052&logfd, err);20532054if(result)2055return result;20562057if(logfd <0)2058return0;2059 result =log_ref_write_fd(logfd, old_sha1, new_sha1,2060git_committer_info(0), msg);2061if(result) {2062struct strbuf sb = STRBUF_INIT;2063int save_errno = errno;20642065files_reflog_path(refs, &sb, refname);2066strbuf_addf(err,"unable to append to '%s':%s",2067 sb.buf,strerror(save_errno));2068strbuf_release(&sb);2069close(logfd);2070return-1;2071}2072if(close(logfd)) {2073struct strbuf sb = STRBUF_INIT;2074int save_errno = errno;20752076files_reflog_path(refs, &sb, refname);2077strbuf_addf(err,"unable to append to '%s':%s",2078 sb.buf,strerror(save_errno));2079strbuf_release(&sb);2080return-1;2081}2082return0;2083}20842085/*2086 * Write sha1 into the open lockfile, then close the lockfile. On2087 * errors, rollback the lockfile, fill in *err and2088 * return -1.2089 */2090static intwrite_ref_to_lockfile(struct ref_lock *lock,2091const unsigned char*sha1,struct strbuf *err)2092{2093static char term ='\n';2094struct object *o;2095int fd;20962097 o =parse_object(sha1);2098if(!o) {2099strbuf_addf(err,2100"trying to write ref '%s' with nonexistent object%s",2101 lock->ref_name,sha1_to_hex(sha1));2102unlock_ref(lock);2103return-1;2104}2105if(o->type != OBJ_COMMIT &&is_branch(lock->ref_name)) {2106strbuf_addf(err,2107"trying to write non-commit object%sto branch '%s'",2108sha1_to_hex(sha1), lock->ref_name);2109unlock_ref(lock);2110return-1;2111}2112 fd =get_lock_file_fd(lock->lk);2113if(write_in_full(fd,sha1_to_hex(sha1),40) !=40||2114write_in_full(fd, &term,1) !=1||2115close_ref(lock) <0) {2116strbuf_addf(err,2117"couldn't write '%s'",get_lock_file_path(lock->lk));2118unlock_ref(lock);2119return-1;2120}2121return0;2122}21232124/*2125 * Commit a change to a loose reference that has already been written2126 * to the loose reference lockfile. Also update the reflogs if2127 * necessary, using the specified lockmsg (which can be NULL).2128 */2129static intcommit_ref_update(struct files_ref_store *refs,2130struct ref_lock *lock,2131const unsigned char*sha1,const char*logmsg,2132struct strbuf *err)2133{2134files_assert_main_repository(refs,"commit_ref_update");21352136clear_loose_ref_cache(refs);2137if(files_log_ref_write(refs, lock->ref_name,2138 lock->old_oid.hash, sha1,2139 logmsg,0, err)) {2140char*old_msg =strbuf_detach(err, NULL);2141strbuf_addf(err,"cannot update the ref '%s':%s",2142 lock->ref_name, old_msg);2143free(old_msg);2144unlock_ref(lock);2145return-1;2146}21472148if(strcmp(lock->ref_name,"HEAD") !=0) {2149/*2150 * Special hack: If a branch is updated directly and HEAD2151 * points to it (may happen on the remote side of a push2152 * for example) then logically the HEAD reflog should be2153 * updated too.2154 * A generic solution implies reverse symref information,2155 * but finding all symrefs pointing to the given branch2156 * would be rather costly for this rare event (the direct2157 * update of a branch) to be worth it. So let's cheat and2158 * check with HEAD only which should cover 99% of all usage2159 * scenarios (even 100% of the default ones).2160 */2161unsigned char head_sha1[20];2162int head_flag;2163const char*head_ref;21642165 head_ref =refs_resolve_ref_unsafe(&refs->base,"HEAD",2166 RESOLVE_REF_READING,2167 head_sha1, &head_flag);2168if(head_ref && (head_flag & REF_ISSYMREF) &&2169!strcmp(head_ref, lock->ref_name)) {2170struct strbuf log_err = STRBUF_INIT;2171if(files_log_ref_write(refs,"HEAD",2172 lock->old_oid.hash, sha1,2173 logmsg,0, &log_err)) {2174error("%s", log_err.buf);2175strbuf_release(&log_err);2176}2177}2178}21792180if(commit_ref(lock)) {2181strbuf_addf(err,"couldn't set '%s'", lock->ref_name);2182unlock_ref(lock);2183return-1;2184}21852186unlock_ref(lock);2187return0;2188}21892190static intcreate_ref_symlink(struct ref_lock *lock,const char*target)2191{2192int ret = -1;2193#ifndef NO_SYMLINK_HEAD2194char*ref_path =get_locked_file_path(lock->lk);2195unlink(ref_path);2196 ret =symlink(target, ref_path);2197free(ref_path);21982199if(ret)2200fprintf(stderr,"no symlink - falling back to symbolic ref\n");2201#endif2202return ret;2203}22042205static voidupdate_symref_reflog(struct files_ref_store *refs,2206struct ref_lock *lock,const char*refname,2207const char*target,const char*logmsg)2208{2209struct strbuf err = STRBUF_INIT;2210unsigned char new_sha1[20];2211if(logmsg &&2212!refs_read_ref_full(&refs->base, target,2213 RESOLVE_REF_READING, new_sha1, NULL) &&2214files_log_ref_write(refs, refname, lock->old_oid.hash,2215 new_sha1, logmsg,0, &err)) {2216error("%s", err.buf);2217strbuf_release(&err);2218}2219}22202221static intcreate_symref_locked(struct files_ref_store *refs,2222struct ref_lock *lock,const char*refname,2223const char*target,const char*logmsg)2224{2225if(prefer_symlink_refs && !create_ref_symlink(lock, target)) {2226update_symref_reflog(refs, lock, refname, target, logmsg);2227return0;2228}22292230if(!fdopen_lock_file(lock->lk,"w"))2231returnerror("unable to fdopen%s:%s",2232 lock->lk->tempfile.filename.buf,strerror(errno));22332234update_symref_reflog(refs, lock, refname, target, logmsg);22352236/* no error check; commit_ref will check ferror */2237fprintf(lock->lk->tempfile.fp,"ref:%s\n", target);2238if(commit_ref(lock) <0)2239returnerror("unable to write symref for%s:%s", refname,2240strerror(errno));2241return0;2242}22432244static intfiles_create_symref(struct ref_store *ref_store,2245const char*refname,const char*target,2246const char*logmsg)2247{2248struct files_ref_store *refs =2249files_downcast(ref_store, REF_STORE_WRITE,"create_symref");2250struct strbuf err = STRBUF_INIT;2251struct ref_lock *lock;2252int ret;22532254 lock =lock_ref_sha1_basic(refs, refname, NULL,2255 NULL, NULL, REF_NODEREF, NULL,2256&err);2257if(!lock) {2258error("%s", err.buf);2259strbuf_release(&err);2260return-1;2261}22622263 ret =create_symref_locked(refs, lock, refname, target, logmsg);2264unlock_ref(lock);2265return ret;2266}22672268intset_worktree_head_symref(const char*gitdir,const char*target,const char*logmsg)2269{2270/*2271 * FIXME: this obviously will not work well for future refs2272 * backends. This function needs to die.2273 */2274struct files_ref_store *refs =2275files_downcast(get_main_ref_store(),2276 REF_STORE_WRITE,2277"set_head_symref");22782279static struct lock_file head_lock;2280struct ref_lock *lock;2281struct strbuf head_path = STRBUF_INIT;2282const char*head_rel;2283int ret;22842285strbuf_addf(&head_path,"%s/HEAD",absolute_path(gitdir));2286if(hold_lock_file_for_update(&head_lock, head_path.buf,2287 LOCK_NO_DEREF) <0) {2288struct strbuf err = STRBUF_INIT;2289unable_to_lock_message(head_path.buf, errno, &err);2290error("%s", err.buf);2291strbuf_release(&err);2292strbuf_release(&head_path);2293return-1;2294}22952296/* head_rel will be "HEAD" for the main tree, "worktrees/wt/HEAD" for2297 linked trees */2298 head_rel =remove_leading_path(head_path.buf,2299absolute_path(get_git_common_dir()));2300/* to make use of create_symref_locked(), initialize ref_lock */2301 lock =xcalloc(1,sizeof(struct ref_lock));2302 lock->lk = &head_lock;2303 lock->ref_name =xstrdup(head_rel);23042305 ret =create_symref_locked(refs, lock, head_rel, target, logmsg);23062307unlock_ref(lock);/* will free lock */2308strbuf_release(&head_path);2309return ret;2310}23112312static intfiles_reflog_exists(struct ref_store *ref_store,2313const char*refname)2314{2315struct files_ref_store *refs =2316files_downcast(ref_store, REF_STORE_READ,"reflog_exists");2317struct strbuf sb = STRBUF_INIT;2318struct stat st;2319int ret;23202321files_reflog_path(refs, &sb, refname);2322 ret = !lstat(sb.buf, &st) &&S_ISREG(st.st_mode);2323strbuf_release(&sb);2324return ret;2325}23262327static intfiles_delete_reflog(struct ref_store *ref_store,2328const char*refname)2329{2330struct files_ref_store *refs =2331files_downcast(ref_store, REF_STORE_WRITE,"delete_reflog");2332struct strbuf sb = STRBUF_INIT;2333int ret;23342335files_reflog_path(refs, &sb, refname);2336 ret =remove_path(sb.buf);2337strbuf_release(&sb);2338return ret;2339}23402341static intshow_one_reflog_ent(struct strbuf *sb, each_reflog_ent_fn fn,void*cb_data)2342{2343struct object_id ooid, noid;2344char*email_end, *message;2345unsigned long timestamp;2346int tz;2347const char*p = sb->buf;23482349/* old SP new SP name <email> SP time TAB msg LF */2350if(!sb->len || sb->buf[sb->len -1] !='\n'||2351parse_oid_hex(p, &ooid, &p) || *p++ !=' '||2352parse_oid_hex(p, &noid, &p) || *p++ !=' '||2353!(email_end =strchr(p,'>')) ||2354 email_end[1] !=' '||2355!(timestamp =strtoul(email_end +2, &message,10)) ||2356!message || message[0] !=' '||2357(message[1] !='+'&& message[1] !='-') ||2358!isdigit(message[2]) || !isdigit(message[3]) ||2359!isdigit(message[4]) || !isdigit(message[5]))2360return0;/* corrupt? */2361 email_end[1] ='\0';2362 tz =strtol(message +1, NULL,10);2363if(message[6] !='\t')2364 message +=6;2365else2366 message +=7;2367returnfn(&ooid, &noid, p, timestamp, tz, message, cb_data);2368}23692370static char*find_beginning_of_line(char*bob,char*scan)2371{2372while(bob < scan && *(--scan) !='\n')2373;/* keep scanning backwards */2374/*2375 * Return either beginning of the buffer, or LF at the end of2376 * the previous line.2377 */2378return scan;2379}23802381static intfiles_for_each_reflog_ent_reverse(struct ref_store *ref_store,2382const char*refname,2383 each_reflog_ent_fn fn,2384void*cb_data)2385{2386struct files_ref_store *refs =2387files_downcast(ref_store, REF_STORE_READ,2388"for_each_reflog_ent_reverse");2389struct strbuf sb = STRBUF_INIT;2390FILE*logfp;2391long pos;2392int ret =0, at_tail =1;23932394files_reflog_path(refs, &sb, refname);2395 logfp =fopen(sb.buf,"r");2396strbuf_release(&sb);2397if(!logfp)2398return-1;23992400/* Jump to the end */2401if(fseek(logfp,0, SEEK_END) <0)2402returnerror("cannot seek back reflog for%s:%s",2403 refname,strerror(errno));2404 pos =ftell(logfp);2405while(!ret &&0< pos) {2406int cnt;2407size_t nread;2408char buf[BUFSIZ];2409char*endp, *scanp;24102411/* Fill next block from the end */2412 cnt = (sizeof(buf) < pos) ?sizeof(buf) : pos;2413if(fseek(logfp, pos - cnt, SEEK_SET))2414returnerror("cannot seek back reflog for%s:%s",2415 refname,strerror(errno));2416 nread =fread(buf, cnt,1, logfp);2417if(nread !=1)2418returnerror("cannot read%dbytes from reflog for%s:%s",2419 cnt, refname,strerror(errno));2420 pos -= cnt;24212422 scanp = endp = buf + cnt;2423if(at_tail && scanp[-1] =='\n')2424/* Looking at the final LF at the end of the file */2425 scanp--;2426 at_tail =0;24272428while(buf < scanp) {2429/*2430 * terminating LF of the previous line, or the beginning2431 * of the buffer.2432 */2433char*bp;24342435 bp =find_beginning_of_line(buf, scanp);24362437if(*bp =='\n') {2438/*2439 * The newline is the end of the previous line,2440 * so we know we have complete line starting2441 * at (bp + 1). Prefix it onto any prior data2442 * we collected for the line and process it.2443 */2444strbuf_splice(&sb,0,0, bp +1, endp - (bp +1));2445 scanp = bp;2446 endp = bp +1;2447 ret =show_one_reflog_ent(&sb, fn, cb_data);2448strbuf_reset(&sb);2449if(ret)2450break;2451}else if(!pos) {2452/*2453 * We are at the start of the buffer, and the2454 * start of the file; there is no previous2455 * line, and we have everything for this one.2456 * Process it, and we can end the loop.2457 */2458strbuf_splice(&sb,0,0, buf, endp - buf);2459 ret =show_one_reflog_ent(&sb, fn, cb_data);2460strbuf_reset(&sb);2461break;2462}24632464if(bp == buf) {2465/*2466 * We are at the start of the buffer, and there2467 * is more file to read backwards. Which means2468 * we are in the middle of a line. Note that we2469 * may get here even if *bp was a newline; that2470 * just means we are at the exact end of the2471 * previous line, rather than some spot in the2472 * middle.2473 *2474 * Save away what we have to be combined with2475 * the data from the next read.2476 */2477strbuf_splice(&sb,0,0, buf, endp - buf);2478break;2479}2480}24812482}2483if(!ret && sb.len)2484die("BUG: reverse reflog parser had leftover data");24852486fclose(logfp);2487strbuf_release(&sb);2488return ret;2489}24902491static intfiles_for_each_reflog_ent(struct ref_store *ref_store,2492const char*refname,2493 each_reflog_ent_fn fn,void*cb_data)2494{2495struct files_ref_store *refs =2496files_downcast(ref_store, REF_STORE_READ,2497"for_each_reflog_ent");2498FILE*logfp;2499struct strbuf sb = STRBUF_INIT;2500int ret =0;25012502files_reflog_path(refs, &sb, refname);2503 logfp =fopen(sb.buf,"r");2504strbuf_release(&sb);2505if(!logfp)2506return-1;25072508while(!ret && !strbuf_getwholeline(&sb, logfp,'\n'))2509 ret =show_one_reflog_ent(&sb, fn, cb_data);2510fclose(logfp);2511strbuf_release(&sb);2512return ret;2513}25142515struct files_reflog_iterator {2516struct ref_iterator base;25172518struct ref_store *ref_store;2519struct dir_iterator *dir_iterator;2520struct object_id oid;2521};25222523static intfiles_reflog_iterator_advance(struct ref_iterator *ref_iterator)2524{2525struct files_reflog_iterator *iter =2526(struct files_reflog_iterator *)ref_iterator;2527struct dir_iterator *diter = iter->dir_iterator;2528int ok;25292530while((ok =dir_iterator_advance(diter)) == ITER_OK) {2531int flags;25322533if(!S_ISREG(diter->st.st_mode))2534continue;2535if(diter->basename[0] =='.')2536continue;2537if(ends_with(diter->basename,".lock"))2538continue;25392540if(refs_read_ref_full(iter->ref_store,2541 diter->relative_path,0,2542 iter->oid.hash, &flags)) {2543error("bad ref for%s", diter->path.buf);2544continue;2545}25462547 iter->base.refname = diter->relative_path;2548 iter->base.oid = &iter->oid;2549 iter->base.flags = flags;2550return ITER_OK;2551}25522553 iter->dir_iterator = NULL;2554if(ref_iterator_abort(ref_iterator) == ITER_ERROR)2555 ok = ITER_ERROR;2556return ok;2557}25582559static intfiles_reflog_iterator_peel(struct ref_iterator *ref_iterator,2560struct object_id *peeled)2561{2562die("BUG: ref_iterator_peel() called for reflog_iterator");2563}25642565static intfiles_reflog_iterator_abort(struct ref_iterator *ref_iterator)2566{2567struct files_reflog_iterator *iter =2568(struct files_reflog_iterator *)ref_iterator;2569int ok = ITER_DONE;25702571if(iter->dir_iterator)2572 ok =dir_iterator_abort(iter->dir_iterator);25732574base_ref_iterator_free(ref_iterator);2575return ok;2576}25772578static struct ref_iterator_vtable files_reflog_iterator_vtable = {2579 files_reflog_iterator_advance,2580 files_reflog_iterator_peel,2581 files_reflog_iterator_abort2582};25832584static struct ref_iterator *files_reflog_iterator_begin(struct ref_store *ref_store)2585{2586struct files_ref_store *refs =2587files_downcast(ref_store, REF_STORE_READ,2588"reflog_iterator_begin");2589struct files_reflog_iterator *iter =xcalloc(1,sizeof(*iter));2590struct ref_iterator *ref_iterator = &iter->base;2591struct strbuf sb = STRBUF_INIT;25922593base_ref_iterator_init(ref_iterator, &files_reflog_iterator_vtable);2594files_reflog_path(refs, &sb, NULL);2595 iter->dir_iterator =dir_iterator_begin(sb.buf);2596 iter->ref_store = ref_store;2597strbuf_release(&sb);2598return ref_iterator;2599}26002601static intref_update_reject_duplicates(struct string_list *refnames,2602struct strbuf *err)2603{2604int i, n = refnames->nr;26052606assert(err);26072608for(i =1; i < n; i++)2609if(!strcmp(refnames->items[i -1].string, refnames->items[i].string)) {2610strbuf_addf(err,2611"multiple updates for ref '%s' not allowed.",2612 refnames->items[i].string);2613return1;2614}2615return0;2616}26172618/*2619 * If update is a direct update of head_ref (the reference pointed to2620 * by HEAD), then add an extra REF_LOG_ONLY update for HEAD.2621 */2622static intsplit_head_update(struct ref_update *update,2623struct ref_transaction *transaction,2624const char*head_ref,2625struct string_list *affected_refnames,2626struct strbuf *err)2627{2628struct string_list_item *item;2629struct ref_update *new_update;26302631if((update->flags & REF_LOG_ONLY) ||2632(update->flags & REF_ISPRUNING) ||2633(update->flags & REF_UPDATE_VIA_HEAD))2634return0;26352636if(strcmp(update->refname, head_ref))2637return0;26382639/*2640 * First make sure that HEAD is not already in the2641 * transaction. This insertion is O(N) in the transaction2642 * size, but it happens at most once per transaction.2643 */2644 item =string_list_insert(affected_refnames,"HEAD");2645if(item->util) {2646/* An entry already existed */2647strbuf_addf(err,2648"multiple updates for 'HEAD' (including one "2649"via its referent '%s') are not allowed",2650 update->refname);2651return TRANSACTION_NAME_CONFLICT;2652}26532654 new_update =ref_transaction_add_update(2655 transaction,"HEAD",2656 update->flags | REF_LOG_ONLY | REF_NODEREF,2657 update->new_sha1, update->old_sha1,2658 update->msg);26592660 item->util = new_update;26612662return0;2663}26642665/*2666 * update is for a symref that points at referent and doesn't have2667 * REF_NODEREF set. Split it into two updates:2668 * - The original update, but with REF_LOG_ONLY and REF_NODEREF set2669 * - A new, separate update for the referent reference2670 * Note that the new update will itself be subject to splitting when2671 * the iteration gets to it.2672 */2673static intsplit_symref_update(struct files_ref_store *refs,2674struct ref_update *update,2675const char*referent,2676struct ref_transaction *transaction,2677struct string_list *affected_refnames,2678struct strbuf *err)2679{2680struct string_list_item *item;2681struct ref_update *new_update;2682unsigned int new_flags;26832684/*2685 * First make sure that referent is not already in the2686 * transaction. This insertion is O(N) in the transaction2687 * size, but it happens at most once per symref in a2688 * transaction.2689 */2690 item =string_list_insert(affected_refnames, referent);2691if(item->util) {2692/* An entry already existed */2693strbuf_addf(err,2694"multiple updates for '%s' (including one "2695"via symref '%s') are not allowed",2696 referent, update->refname);2697return TRANSACTION_NAME_CONFLICT;2698}26992700 new_flags = update->flags;2701if(!strcmp(update->refname,"HEAD")) {2702/*2703 * Record that the new update came via HEAD, so that2704 * when we process it, split_head_update() doesn't try2705 * to add another reflog update for HEAD. Note that2706 * this bit will be propagated if the new_update2707 * itself needs to be split.2708 */2709 new_flags |= REF_UPDATE_VIA_HEAD;2710}27112712 new_update =ref_transaction_add_update(2713 transaction, referent, new_flags,2714 update->new_sha1, update->old_sha1,2715 update->msg);27162717 new_update->parent_update = update;27182719/*2720 * Change the symbolic ref update to log only. Also, it2721 * doesn't need to check its old SHA-1 value, as that will be2722 * done when new_update is processed.2723 */2724 update->flags |= REF_LOG_ONLY | REF_NODEREF;2725 update->flags &= ~REF_HAVE_OLD;27262727 item->util = new_update;27282729return0;2730}27312732/*2733 * Return the refname under which update was originally requested.2734 */2735static const char*original_update_refname(struct ref_update *update)2736{2737while(update->parent_update)2738 update = update->parent_update;27392740return update->refname;2741}27422743/*2744 * Check whether the REF_HAVE_OLD and old_oid values stored in update2745 * are consistent with oid, which is the reference's current value. If2746 * everything is OK, return 0; otherwise, write an error message to2747 * err and return -1.2748 */2749static intcheck_old_oid(struct ref_update *update,struct object_id *oid,2750struct strbuf *err)2751{2752if(!(update->flags & REF_HAVE_OLD) ||2753!hashcmp(oid->hash, update->old_sha1))2754return0;27552756if(is_null_sha1(update->old_sha1))2757strbuf_addf(err,"cannot lock ref '%s': "2758"reference already exists",2759original_update_refname(update));2760else if(is_null_oid(oid))2761strbuf_addf(err,"cannot lock ref '%s': "2762"reference is missing but expected%s",2763original_update_refname(update),2764sha1_to_hex(update->old_sha1));2765else2766strbuf_addf(err,"cannot lock ref '%s': "2767"is at%sbut expected%s",2768original_update_refname(update),2769oid_to_hex(oid),2770sha1_to_hex(update->old_sha1));27712772return-1;2773}27742775/*2776 * Prepare for carrying out update:2777 * - Lock the reference referred to by update.2778 * - Read the reference under lock.2779 * - Check that its old SHA-1 value (if specified) is correct, and in2780 * any case record it in update->lock->old_oid for later use when2781 * writing the reflog.2782 * - If it is a symref update without REF_NODEREF, split it up into a2783 * REF_LOG_ONLY update of the symref and add a separate update for2784 * the referent to transaction.2785 * - If it is an update of head_ref, add a corresponding REF_LOG_ONLY2786 * update of HEAD.2787 */2788static intlock_ref_for_update(struct files_ref_store *refs,2789struct ref_update *update,2790struct ref_transaction *transaction,2791const char*head_ref,2792struct string_list *affected_refnames,2793struct strbuf *err)2794{2795struct strbuf referent = STRBUF_INIT;2796int mustexist = (update->flags & REF_HAVE_OLD) &&2797!is_null_sha1(update->old_sha1);2798int ret;2799struct ref_lock *lock;28002801files_assert_main_repository(refs,"lock_ref_for_update");28022803if((update->flags & REF_HAVE_NEW) &&is_null_sha1(update->new_sha1))2804 update->flags |= REF_DELETING;28052806if(head_ref) {2807 ret =split_head_update(update, transaction, head_ref,2808 affected_refnames, err);2809if(ret)2810return ret;2811}28122813 ret =lock_raw_ref(refs, update->refname, mustexist,2814 affected_refnames, NULL,2815&lock, &referent,2816&update->type, err);2817if(ret) {2818char*reason;28192820 reason =strbuf_detach(err, NULL);2821strbuf_addf(err,"cannot lock ref '%s':%s",2822original_update_refname(update), reason);2823free(reason);2824return ret;2825}28262827 update->backend_data = lock;28282829if(update->type & REF_ISSYMREF) {2830if(update->flags & REF_NODEREF) {2831/*2832 * We won't be reading the referent as part of2833 * the transaction, so we have to read it here2834 * to record and possibly check old_sha1:2835 */2836if(refs_read_ref_full(&refs->base,2837 referent.buf,0,2838 lock->old_oid.hash, NULL)) {2839if(update->flags & REF_HAVE_OLD) {2840strbuf_addf(err,"cannot lock ref '%s': "2841"error reading reference",2842original_update_refname(update));2843return-1;2844}2845}else if(check_old_oid(update, &lock->old_oid, err)) {2846return TRANSACTION_GENERIC_ERROR;2847}2848}else{2849/*2850 * Create a new update for the reference this2851 * symref is pointing at. Also, we will record2852 * and verify old_sha1 for this update as part2853 * of processing the split-off update, so we2854 * don't have to do it here.2855 */2856 ret =split_symref_update(refs, update,2857 referent.buf, transaction,2858 affected_refnames, err);2859if(ret)2860return ret;2861}2862}else{2863struct ref_update *parent_update;28642865if(check_old_oid(update, &lock->old_oid, err))2866return TRANSACTION_GENERIC_ERROR;28672868/*2869 * If this update is happening indirectly because of a2870 * symref update, record the old SHA-1 in the parent2871 * update:2872 */2873for(parent_update = update->parent_update;2874 parent_update;2875 parent_update = parent_update->parent_update) {2876struct ref_lock *parent_lock = parent_update->backend_data;2877oidcpy(&parent_lock->old_oid, &lock->old_oid);2878}2879}28802881if((update->flags & REF_HAVE_NEW) &&2882!(update->flags & REF_DELETING) &&2883!(update->flags & REF_LOG_ONLY)) {2884if(!(update->type & REF_ISSYMREF) &&2885!hashcmp(lock->old_oid.hash, update->new_sha1)) {2886/*2887 * The reference already has the desired2888 * value, so we don't need to write it.2889 */2890}else if(write_ref_to_lockfile(lock, update->new_sha1,2891 err)) {2892char*write_err =strbuf_detach(err, NULL);28932894/*2895 * The lock was freed upon failure of2896 * write_ref_to_lockfile():2897 */2898 update->backend_data = NULL;2899strbuf_addf(err,2900"cannot update ref '%s':%s",2901 update->refname, write_err);2902free(write_err);2903return TRANSACTION_GENERIC_ERROR;2904}else{2905 update->flags |= REF_NEEDS_COMMIT;2906}2907}2908if(!(update->flags & REF_NEEDS_COMMIT)) {2909/*2910 * We didn't call write_ref_to_lockfile(), so2911 * the lockfile is still open. Close it to2912 * free up the file descriptor:2913 */2914if(close_ref(lock)) {2915strbuf_addf(err,"couldn't close '%s.lock'",2916 update->refname);2917return TRANSACTION_GENERIC_ERROR;2918}2919}2920return0;2921}29222923static intfiles_transaction_commit(struct ref_store *ref_store,2924struct ref_transaction *transaction,2925struct strbuf *err)2926{2927struct files_ref_store *refs =2928files_downcast(ref_store, REF_STORE_WRITE,2929"ref_transaction_commit");2930int ret =0, i;2931struct string_list refs_to_delete = STRING_LIST_INIT_NODUP;2932struct string_list_item *ref_to_delete;2933struct string_list affected_refnames = STRING_LIST_INIT_NODUP;2934char*head_ref = NULL;2935int head_type;2936struct object_id head_oid;2937struct strbuf sb = STRBUF_INIT;29382939assert(err);29402941if(transaction->state != REF_TRANSACTION_OPEN)2942die("BUG: commit called for transaction that is not open");29432944if(!transaction->nr) {2945 transaction->state = REF_TRANSACTION_CLOSED;2946return0;2947}29482949/*2950 * Fail if a refname appears more than once in the2951 * transaction. (If we end up splitting up any updates using2952 * split_symref_update() or split_head_update(), those2953 * functions will check that the new updates don't have the2954 * same refname as any existing ones.)2955 */2956for(i =0; i < transaction->nr; i++) {2957struct ref_update *update = transaction->updates[i];2958struct string_list_item *item =2959string_list_append(&affected_refnames, update->refname);29602961/*2962 * We store a pointer to update in item->util, but at2963 * the moment we never use the value of this field2964 * except to check whether it is non-NULL.2965 */2966 item->util = update;2967}2968string_list_sort(&affected_refnames);2969if(ref_update_reject_duplicates(&affected_refnames, err)) {2970 ret = TRANSACTION_GENERIC_ERROR;2971goto cleanup;2972}29732974/*2975 * Special hack: If a branch is updated directly and HEAD2976 * points to it (may happen on the remote side of a push2977 * for example) then logically the HEAD reflog should be2978 * updated too.2979 *2980 * A generic solution would require reverse symref lookups,2981 * but finding all symrefs pointing to a given branch would be2982 * rather costly for this rare event (the direct update of a2983 * branch) to be worth it. So let's cheat and check with HEAD2984 * only, which should cover 99% of all usage scenarios (even2985 * 100% of the default ones).2986 *2987 * So if HEAD is a symbolic reference, then record the name of2988 * the reference that it points to. If we see an update of2989 * head_ref within the transaction, then split_head_update()2990 * arranges for the reflog of HEAD to be updated, too.2991 */2992 head_ref =refs_resolve_refdup(ref_store,"HEAD",2993 RESOLVE_REF_NO_RECURSE,2994 head_oid.hash, &head_type);29952996if(head_ref && !(head_type & REF_ISSYMREF)) {2997free(head_ref);2998 head_ref = NULL;2999}30003001/*3002 * Acquire all locks, verify old values if provided, check3003 * that new values are valid, and write new values to the3004 * lockfiles, ready to be activated. Only keep one lockfile3005 * open at a time to avoid running out of file descriptors.3006 */3007for(i =0; i < transaction->nr; i++) {3008struct ref_update *update = transaction->updates[i];30093010 ret =lock_ref_for_update(refs, update, transaction,3011 head_ref, &affected_refnames, err);3012if(ret)3013goto cleanup;3014}30153016/* Perform updates first so live commits remain referenced */3017for(i =0; i < transaction->nr; i++) {3018struct ref_update *update = transaction->updates[i];3019struct ref_lock *lock = update->backend_data;30203021if(update->flags & REF_NEEDS_COMMIT ||3022 update->flags & REF_LOG_ONLY) {3023if(files_log_ref_write(refs,3024 lock->ref_name,3025 lock->old_oid.hash,3026 update->new_sha1,3027 update->msg, update->flags,3028 err)) {3029char*old_msg =strbuf_detach(err, NULL);30303031strbuf_addf(err,"cannot update the ref '%s':%s",3032 lock->ref_name, old_msg);3033free(old_msg);3034unlock_ref(lock);3035 update->backend_data = NULL;3036 ret = TRANSACTION_GENERIC_ERROR;3037goto cleanup;3038}3039}3040if(update->flags & REF_NEEDS_COMMIT) {3041clear_loose_ref_cache(refs);3042if(commit_ref(lock)) {3043strbuf_addf(err,"couldn't set '%s'", lock->ref_name);3044unlock_ref(lock);3045 update->backend_data = NULL;3046 ret = TRANSACTION_GENERIC_ERROR;3047goto cleanup;3048}3049}3050}3051/* Perform deletes now that updates are safely completed */3052for(i =0; i < transaction->nr; i++) {3053struct ref_update *update = transaction->updates[i];3054struct ref_lock *lock = update->backend_data;30553056if(update->flags & REF_DELETING &&3057!(update->flags & REF_LOG_ONLY)) {3058if(!(update->type & REF_ISPACKED) ||3059 update->type & REF_ISSYMREF) {3060/* It is a loose reference. */3061strbuf_reset(&sb);3062files_ref_path(refs, &sb, lock->ref_name);3063if(unlink_or_msg(sb.buf, err)) {3064 ret = TRANSACTION_GENERIC_ERROR;3065goto cleanup;3066}3067 update->flags |= REF_DELETED_LOOSE;3068}30693070if(!(update->flags & REF_ISPRUNING))3071string_list_append(&refs_to_delete,3072 lock->ref_name);3073}3074}30753076if(repack_without_refs(refs, &refs_to_delete, err)) {3077 ret = TRANSACTION_GENERIC_ERROR;3078goto cleanup;3079}30803081/* Delete the reflogs of any references that were deleted: */3082for_each_string_list_item(ref_to_delete, &refs_to_delete) {3083strbuf_reset(&sb);3084files_reflog_path(refs, &sb, ref_to_delete->string);3085if(!unlink_or_warn(sb.buf))3086try_remove_empty_parents(refs, ref_to_delete->string,3087 REMOVE_EMPTY_PARENTS_REFLOG);3088}30893090clear_loose_ref_cache(refs);30913092cleanup:3093strbuf_release(&sb);3094 transaction->state = REF_TRANSACTION_CLOSED;30953096for(i =0; i < transaction->nr; i++) {3097struct ref_update *update = transaction->updates[i];3098struct ref_lock *lock = update->backend_data;30993100if(lock)3101unlock_ref(lock);31023103if(update->flags & REF_DELETED_LOOSE) {3104/*3105 * The loose reference was deleted. Delete any3106 * empty parent directories. (Note that this3107 * can only work because we have already3108 * removed the lockfile.)3109 */3110try_remove_empty_parents(refs, update->refname,3111 REMOVE_EMPTY_PARENTS_REF);3112}3113}31143115string_list_clear(&refs_to_delete,0);3116free(head_ref);3117string_list_clear(&affected_refnames,0);31183119return ret;3120}31213122static intref_present(const char*refname,3123const struct object_id *oid,int flags,void*cb_data)3124{3125struct string_list *affected_refnames = cb_data;31263127returnstring_list_has_string(affected_refnames, refname);3128}31293130static intfiles_initial_transaction_commit(struct ref_store *ref_store,3131struct ref_transaction *transaction,3132struct strbuf *err)3133{3134struct files_ref_store *refs =3135files_downcast(ref_store, REF_STORE_WRITE,3136"initial_ref_transaction_commit");3137int ret =0, i;3138struct string_list affected_refnames = STRING_LIST_INIT_NODUP;31393140assert(err);31413142if(transaction->state != REF_TRANSACTION_OPEN)3143die("BUG: commit called for transaction that is not open");31443145/* Fail if a refname appears more than once in the transaction: */3146for(i =0; i < transaction->nr; i++)3147string_list_append(&affected_refnames,3148 transaction->updates[i]->refname);3149string_list_sort(&affected_refnames);3150if(ref_update_reject_duplicates(&affected_refnames, err)) {3151 ret = TRANSACTION_GENERIC_ERROR;3152goto cleanup;3153}31543155/*3156 * It's really undefined to call this function in an active3157 * repository or when there are existing references: we are3158 * only locking and changing packed-refs, so (1) any3159 * simultaneous processes might try to change a reference at3160 * the same time we do, and (2) any existing loose versions of3161 * the references that we are setting would have precedence3162 * over our values. But some remote helpers create the remote3163 * "HEAD" and "master" branches before calling this function,3164 * so here we really only check that none of the references3165 * that we are creating already exists.3166 */3167if(refs_for_each_rawref(&refs->base, ref_present,3168&affected_refnames))3169die("BUG: initial ref transaction called with existing refs");31703171for(i =0; i < transaction->nr; i++) {3172struct ref_update *update = transaction->updates[i];31733174if((update->flags & REF_HAVE_OLD) &&3175!is_null_sha1(update->old_sha1))3176die("BUG: initial ref transaction with old_sha1 set");3177if(refs_verify_refname_available(&refs->base, update->refname,3178&affected_refnames, NULL,3179 err)) {3180 ret = TRANSACTION_NAME_CONFLICT;3181goto cleanup;3182}3183}31843185if(lock_packed_refs(refs,0)) {3186strbuf_addf(err,"unable to lock packed-refs file:%s",3187strerror(errno));3188 ret = TRANSACTION_GENERIC_ERROR;3189goto cleanup;3190}31913192for(i =0; i < transaction->nr; i++) {3193struct ref_update *update = transaction->updates[i];31943195if((update->flags & REF_HAVE_NEW) &&3196!is_null_sha1(update->new_sha1))3197add_packed_ref(refs, update->refname, update->new_sha1);3198}31993200if(commit_packed_refs(refs)) {3201strbuf_addf(err,"unable to commit packed-refs file:%s",3202strerror(errno));3203 ret = TRANSACTION_GENERIC_ERROR;3204goto cleanup;3205}32063207cleanup:3208 transaction->state = REF_TRANSACTION_CLOSED;3209string_list_clear(&affected_refnames,0);3210return ret;3211}32123213struct expire_reflog_cb {3214unsigned int flags;3215 reflog_expiry_should_prune_fn *should_prune_fn;3216void*policy_cb;3217FILE*newlog;3218struct object_id last_kept_oid;3219};32203221static intexpire_reflog_ent(struct object_id *ooid,struct object_id *noid,3222const char*email,unsigned long timestamp,int tz,3223const char*message,void*cb_data)3224{3225struct expire_reflog_cb *cb = cb_data;3226struct expire_reflog_policy_cb *policy_cb = cb->policy_cb;32273228if(cb->flags & EXPIRE_REFLOGS_REWRITE)3229 ooid = &cb->last_kept_oid;32303231if((*cb->should_prune_fn)(ooid->hash, noid->hash, email, timestamp, tz,3232 message, policy_cb)) {3233if(!cb->newlog)3234printf("would prune%s", message);3235else if(cb->flags & EXPIRE_REFLOGS_VERBOSE)3236printf("prune%s", message);3237}else{3238if(cb->newlog) {3239fprintf(cb->newlog,"%s %s %s %lu %+05d\t%s",3240oid_to_hex(ooid),oid_to_hex(noid),3241 email, timestamp, tz, message);3242oidcpy(&cb->last_kept_oid, noid);3243}3244if(cb->flags & EXPIRE_REFLOGS_VERBOSE)3245printf("keep%s", message);3246}3247return0;3248}32493250static intfiles_reflog_expire(struct ref_store *ref_store,3251const char*refname,const unsigned char*sha1,3252unsigned int flags,3253 reflog_expiry_prepare_fn prepare_fn,3254 reflog_expiry_should_prune_fn should_prune_fn,3255 reflog_expiry_cleanup_fn cleanup_fn,3256void*policy_cb_data)3257{3258struct files_ref_store *refs =3259files_downcast(ref_store, REF_STORE_WRITE,"reflog_expire");3260static struct lock_file reflog_lock;3261struct expire_reflog_cb cb;3262struct ref_lock *lock;3263struct strbuf log_file_sb = STRBUF_INIT;3264char*log_file;3265int status =0;3266int type;3267struct strbuf err = STRBUF_INIT;32683269memset(&cb,0,sizeof(cb));3270 cb.flags = flags;3271 cb.policy_cb = policy_cb_data;3272 cb.should_prune_fn = should_prune_fn;32733274/*3275 * The reflog file is locked by holding the lock on the3276 * reference itself, plus we might need to update the3277 * reference if --updateref was specified:3278 */3279 lock =lock_ref_sha1_basic(refs, refname, sha1,3280 NULL, NULL, REF_NODEREF,3281&type, &err);3282if(!lock) {3283error("cannot lock ref '%s':%s", refname, err.buf);3284strbuf_release(&err);3285return-1;3286}3287if(!refs_reflog_exists(ref_store, refname)) {3288unlock_ref(lock);3289return0;3290}32913292files_reflog_path(refs, &log_file_sb, refname);3293 log_file =strbuf_detach(&log_file_sb, NULL);3294if(!(flags & EXPIRE_REFLOGS_DRY_RUN)) {3295/*3296 * Even though holding $GIT_DIR/logs/$reflog.lock has3297 * no locking implications, we use the lock_file3298 * machinery here anyway because it does a lot of the3299 * work we need, including cleaning up if the program3300 * exits unexpectedly.3301 */3302if(hold_lock_file_for_update(&reflog_lock, log_file,0) <0) {3303struct strbuf err = STRBUF_INIT;3304unable_to_lock_message(log_file, errno, &err);3305error("%s", err.buf);3306strbuf_release(&err);3307goto failure;3308}3309 cb.newlog =fdopen_lock_file(&reflog_lock,"w");3310if(!cb.newlog) {3311error("cannot fdopen%s(%s)",3312get_lock_file_path(&reflog_lock),strerror(errno));3313goto failure;3314}3315}33163317(*prepare_fn)(refname, sha1, cb.policy_cb);3318refs_for_each_reflog_ent(ref_store, refname, expire_reflog_ent, &cb);3319(*cleanup_fn)(cb.policy_cb);33203321if(!(flags & EXPIRE_REFLOGS_DRY_RUN)) {3322/*3323 * It doesn't make sense to adjust a reference pointed3324 * to by a symbolic ref based on expiring entries in3325 * the symbolic reference's reflog. Nor can we update3326 * a reference if there are no remaining reflog3327 * entries.3328 */3329int update = (flags & EXPIRE_REFLOGS_UPDATE_REF) &&3330!(type & REF_ISSYMREF) &&3331!is_null_oid(&cb.last_kept_oid);33323333if(close_lock_file(&reflog_lock)) {3334 status |=error("couldn't write%s:%s", log_file,3335strerror(errno));3336}else if(update &&3337(write_in_full(get_lock_file_fd(lock->lk),3338oid_to_hex(&cb.last_kept_oid), GIT_SHA1_HEXSZ) != GIT_SHA1_HEXSZ ||3339write_str_in_full(get_lock_file_fd(lock->lk),"\n") !=1||3340close_ref(lock) <0)) {3341 status |=error("couldn't write%s",3342get_lock_file_path(lock->lk));3343rollback_lock_file(&reflog_lock);3344}else if(commit_lock_file(&reflog_lock)) {3345 status |=error("unable to write reflog '%s' (%s)",3346 log_file,strerror(errno));3347}else if(update &&commit_ref(lock)) {3348 status |=error("couldn't set%s", lock->ref_name);3349}3350}3351free(log_file);3352unlock_ref(lock);3353return status;33543355 failure:3356rollback_lock_file(&reflog_lock);3357free(log_file);3358unlock_ref(lock);3359return-1;3360}33613362static intfiles_init_db(struct ref_store *ref_store,struct strbuf *err)3363{3364struct files_ref_store *refs =3365files_downcast(ref_store, REF_STORE_WRITE,"init_db");3366struct strbuf sb = STRBUF_INIT;33673368/*3369 * Create .git/refs/{heads,tags}3370 */3371files_ref_path(refs, &sb,"refs/heads");3372safe_create_dir(sb.buf,1);33733374strbuf_reset(&sb);3375files_ref_path(refs, &sb,"refs/tags");3376safe_create_dir(sb.buf,1);33773378strbuf_release(&sb);3379return0;3380}33813382struct ref_storage_be refs_be_files = {3383 NULL,3384"files",3385 files_ref_store_create,3386 files_init_db,3387 files_transaction_commit,3388 files_initial_transaction_commit,33893390 files_pack_refs,3391 files_peel_ref,3392 files_create_symref,3393 files_delete_refs,3394 files_rename_ref,33953396 files_ref_iterator_begin,3397 files_read_raw_ref,33983399 files_reflog_iterator_begin,3400 files_for_each_reflog_ent,3401 files_for_each_reflog_ent_reverse,3402 files_reflog_exists,3403 files_create_reflog,3404 files_delete_reflog,3405 files_reflog_expire3406};