1#include "../cache.h" 2#include "../config.h" 3#include "../refs.h" 4#include "refs-internal.h" 5#include "ref-cache.h" 6#include "packed-backend.h" 7#include "../iterator.h" 8#include "../dir-iterator.h" 9#include "../lockfile.h" 10#include "../object.h" 11#include "../dir.h" 12 13struct ref_lock { 14 char *ref_name; 15 struct lock_file lk; 16 struct object_id old_oid; 17}; 18 19/* 20 * Future: need to be in "struct repository" 21 * when doing a full libification. 22 */ 23struct files_ref_store { 24 struct ref_store base; 25 unsigned int store_flags; 26 27 char *gitdir; 28 char *gitcommondir; 29 30 struct ref_cache *loose; 31 32 struct ref_store *packed_ref_store; 33}; 34 35static void clear_loose_ref_cache(struct files_ref_store *refs) 36{ 37 if (refs->loose) { 38 free_ref_cache(refs->loose); 39 refs->loose = NULL; 40 } 41} 42 43/* 44 * Create a new submodule ref cache and add it to the internal 45 * set of caches. 46 */ 47static struct ref_store *files_ref_store_create(const char *gitdir, 48 unsigned int flags) 49{ 50 struct files_ref_store *refs = xcalloc(1, sizeof(*refs)); 51 struct ref_store *ref_store = (struct ref_store *)refs; 52 struct strbuf sb = STRBUF_INIT; 53 54 base_ref_store_init(ref_store, &refs_be_files); 55 refs->store_flags = flags; 56 57 refs->gitdir = xstrdup(gitdir); 58 get_common_dir_noenv(&sb, gitdir); 59 refs->gitcommondir = strbuf_detach(&sb, NULL); 60 strbuf_addf(&sb, "%s/packed-refs", refs->gitcommondir); 61 refs->packed_ref_store = packed_ref_store_create(sb.buf, flags); 62 strbuf_release(&sb); 63 64 return ref_store; 65} 66 67/* 68 * Die if refs is not the main ref store. caller is used in any 69 * necessary error messages. 70 */ 71static void files_assert_main_repository(struct files_ref_store *refs, 72 const char *caller) 73{ 74 if (refs->store_flags & REF_STORE_MAIN) 75 return; 76 77 die("BUG: operation %s only allowed for main ref store", caller); 78} 79 80/* 81 * Downcast ref_store to files_ref_store. Die if ref_store is not a 82 * files_ref_store. required_flags is compared with ref_store's 83 * store_flags to ensure the ref_store has all required capabilities. 84 * "caller" is used in any necessary error messages. 85 */ 86static struct files_ref_store *files_downcast(struct ref_store *ref_store, 87 unsigned int required_flags, 88 const char *caller) 89{ 90 struct files_ref_store *refs; 91 92 if (ref_store->be != &refs_be_files) 93 die("BUG: ref_store is type \"%s\" not \"files\" in %s", 94 ref_store->be->name, caller); 95 96 refs = (struct files_ref_store *)ref_store; 97 98 if ((refs->store_flags & required_flags) != required_flags) 99 die("BUG: operation %s requires abilities 0x%x, but only have 0x%x", 100 caller, required_flags, refs->store_flags); 101 102 return refs; 103} 104 105static void files_reflog_path(struct files_ref_store *refs, 106 struct strbuf *sb, 107 const char *refname) 108{ 109 switch (ref_type(refname)) { 110 case REF_TYPE_PER_WORKTREE: 111 case REF_TYPE_PSEUDOREF: 112 strbuf_addf(sb, "%s/logs/%s", refs->gitdir, refname); 113 break; 114 case REF_TYPE_NORMAL: 115 strbuf_addf(sb, "%s/logs/%s", refs->gitcommondir, refname); 116 break; 117 default: 118 die("BUG: unknown ref type %d of ref %s", 119 ref_type(refname), refname); 120 } 121} 122 123static void files_ref_path(struct files_ref_store *refs, 124 struct strbuf *sb, 125 const char *refname) 126{ 127 switch (ref_type(refname)) { 128 case REF_TYPE_PER_WORKTREE: 129 case REF_TYPE_PSEUDOREF: 130 strbuf_addf(sb, "%s/%s", refs->gitdir, refname); 131 break; 132 case REF_TYPE_NORMAL: 133 strbuf_addf(sb, "%s/%s", refs->gitcommondir, refname); 134 break; 135 default: 136 die("BUG: unknown ref type %d of ref %s", 137 ref_type(refname), refname); 138 } 139} 140 141/* 142 * Read the loose references from the namespace dirname into dir 143 * (without recursing). dirname must end with '/'. dir must be the 144 * directory entry corresponding to dirname. 145 */ 146static void loose_fill_ref_dir(struct ref_store *ref_store, 147 struct ref_dir *dir, const char *dirname) 148{ 149 struct files_ref_store *refs = 150 files_downcast(ref_store, REF_STORE_READ, "fill_ref_dir"); 151 DIR *d; 152 struct dirent *de; 153 int dirnamelen = strlen(dirname); 154 struct strbuf refname; 155 struct strbuf path = STRBUF_INIT; 156 size_t path_baselen; 157 158 files_ref_path(refs, &path, dirname); 159 path_baselen = path.len; 160 161 d = opendir(path.buf); 162 if (!d) { 163 strbuf_release(&path); 164 return; 165 } 166 167 strbuf_init(&refname, dirnamelen + 257); 168 strbuf_add(&refname, dirname, dirnamelen); 169 170 while ((de = readdir(d)) != NULL) { 171 struct object_id oid; 172 struct stat st; 173 int flag; 174 175 if (de->d_name[0] == '.') 176 continue; 177 if (ends_with(de->d_name, ".lock")) 178 continue; 179 strbuf_addstr(&refname, de->d_name); 180 strbuf_addstr(&path, de->d_name); 181 if (stat(path.buf, &st) < 0) { 182 ; /* silently ignore */ 183 } else if (S_ISDIR(st.st_mode)) { 184 strbuf_addch(&refname, '/'); 185 add_entry_to_dir(dir, 186 create_dir_entry(dir->cache, refname.buf, 187 refname.len, 1)); 188 } else { 189 if (!refs_resolve_ref_unsafe(&refs->base, 190 refname.buf, 191 RESOLVE_REF_READING, 192 &oid, &flag)) { 193 oidclr(&oid); 194 flag |= REF_ISBROKEN; 195 } else if (is_null_oid(&oid)) { 196 /* 197 * It is so astronomically unlikely 198 * that NULL_SHA1 is the SHA-1 of an 199 * actual object that we consider its 200 * appearance in a loose reference 201 * file to be repo corruption 202 * (probably due to a software bug). 203 */ 204 flag |= REF_ISBROKEN; 205 } 206 207 if (check_refname_format(refname.buf, 208 REFNAME_ALLOW_ONELEVEL)) { 209 if (!refname_is_safe(refname.buf)) 210 die("loose refname is dangerous: %s", refname.buf); 211 oidclr(&oid); 212 flag |= REF_BAD_NAME | REF_ISBROKEN; 213 } 214 add_entry_to_dir(dir, 215 create_ref_entry(refname.buf, &oid, flag)); 216 } 217 strbuf_setlen(&refname, dirnamelen); 218 strbuf_setlen(&path, path_baselen); 219 } 220 strbuf_release(&refname); 221 strbuf_release(&path); 222 closedir(d); 223 224 /* 225 * Manually add refs/bisect, which, being per-worktree, might 226 * not appear in the directory listing for refs/ in the main 227 * repo. 228 */ 229 if (!strcmp(dirname, "refs/")) { 230 int pos = search_ref_dir(dir, "refs/bisect/", 12); 231 232 if (pos < 0) { 233 struct ref_entry *child_entry = create_dir_entry( 234 dir->cache, "refs/bisect/", 12, 1); 235 add_entry_to_dir(dir, child_entry); 236 } 237 } 238} 239 240static struct ref_cache *get_loose_ref_cache(struct files_ref_store *refs) 241{ 242 if (!refs->loose) { 243 /* 244 * Mark the top-level directory complete because we 245 * are about to read the only subdirectory that can 246 * hold references: 247 */ 248 refs->loose = create_ref_cache(&refs->base, loose_fill_ref_dir); 249 250 /* We're going to fill the top level ourselves: */ 251 refs->loose->root->flag &= ~REF_INCOMPLETE; 252 253 /* 254 * Add an incomplete entry for "refs/" (to be filled 255 * lazily): 256 */ 257 add_entry_to_dir(get_ref_dir(refs->loose->root), 258 create_dir_entry(refs->loose, "refs/", 5, 1)); 259 } 260 return refs->loose; 261} 262 263static int files_read_raw_ref(struct ref_store *ref_store, 264 const char *refname, struct object_id *oid, 265 struct strbuf *referent, unsigned int *type) 266{ 267 struct files_ref_store *refs = 268 files_downcast(ref_store, REF_STORE_READ, "read_raw_ref"); 269 struct strbuf sb_contents = STRBUF_INIT; 270 struct strbuf sb_path = STRBUF_INIT; 271 const char *path; 272 const char *buf; 273 const char *p; 274 struct stat st; 275 int fd; 276 int ret = -1; 277 int save_errno; 278 int remaining_retries = 3; 279 280 *type = 0; 281 strbuf_reset(&sb_path); 282 283 files_ref_path(refs, &sb_path, refname); 284 285 path = sb_path.buf; 286 287stat_ref: 288 /* 289 * We might have to loop back here to avoid a race 290 * condition: first we lstat() the file, then we try 291 * to read it as a link or as a file. But if somebody 292 * changes the type of the file (file <-> directory 293 * <-> symlink) between the lstat() and reading, then 294 * we don't want to report that as an error but rather 295 * try again starting with the lstat(). 296 * 297 * We'll keep a count of the retries, though, just to avoid 298 * any confusing situation sending us into an infinite loop. 299 */ 300 301 if (remaining_retries-- <= 0) 302 goto out; 303 304 if (lstat(path, &st) < 0) { 305 if (errno != ENOENT) 306 goto out; 307 if (refs_read_raw_ref(refs->packed_ref_store, refname, 308 oid, referent, type)) { 309 errno = ENOENT; 310 goto out; 311 } 312 ret = 0; 313 goto out; 314 } 315 316 /* Follow "normalized" - ie "refs/.." symlinks by hand */ 317 if (S_ISLNK(st.st_mode)) { 318 strbuf_reset(&sb_contents); 319 if (strbuf_readlink(&sb_contents, path, 0) < 0) { 320 if (errno == ENOENT || errno == EINVAL) 321 /* inconsistent with lstat; retry */ 322 goto stat_ref; 323 else 324 goto out; 325 } 326 if (starts_with(sb_contents.buf, "refs/") && 327 !check_refname_format(sb_contents.buf, 0)) { 328 strbuf_swap(&sb_contents, referent); 329 *type |= REF_ISSYMREF; 330 ret = 0; 331 goto out; 332 } 333 /* 334 * It doesn't look like a refname; fall through to just 335 * treating it like a non-symlink, and reading whatever it 336 * points to. 337 */ 338 } 339 340 /* Is it a directory? */ 341 if (S_ISDIR(st.st_mode)) { 342 /* 343 * Even though there is a directory where the loose 344 * ref is supposed to be, there could still be a 345 * packed ref: 346 */ 347 if (refs_read_raw_ref(refs->packed_ref_store, refname, 348 oid, referent, type)) { 349 errno = EISDIR; 350 goto out; 351 } 352 ret = 0; 353 goto out; 354 } 355 356 /* 357 * Anything else, just open it and try to use it as 358 * a ref 359 */ 360 fd = open(path, O_RDONLY); 361 if (fd < 0) { 362 if (errno == ENOENT && !S_ISLNK(st.st_mode)) 363 /* inconsistent with lstat; retry */ 364 goto stat_ref; 365 else 366 goto out; 367 } 368 strbuf_reset(&sb_contents); 369 if (strbuf_read(&sb_contents, fd, 256) < 0) { 370 int save_errno = errno; 371 close(fd); 372 errno = save_errno; 373 goto out; 374 } 375 close(fd); 376 strbuf_rtrim(&sb_contents); 377 buf = sb_contents.buf; 378 if (starts_with(buf, "ref:")) { 379 buf += 4; 380 while (isspace(*buf)) 381 buf++; 382 383 strbuf_reset(referent); 384 strbuf_addstr(referent, buf); 385 *type |= REF_ISSYMREF; 386 ret = 0; 387 goto out; 388 } 389 390 /* 391 * Please note that FETCH_HEAD has additional 392 * data after the sha. 393 */ 394 if (parse_oid_hex(buf, oid, &p) || 395 (*p != '\0' && !isspace(*p))) { 396 *type |= REF_ISBROKEN; 397 errno = EINVAL; 398 goto out; 399 } 400 401 ret = 0; 402 403out: 404 save_errno = errno; 405 strbuf_release(&sb_path); 406 strbuf_release(&sb_contents); 407 errno = save_errno; 408 return ret; 409} 410 411static void unlock_ref(struct ref_lock *lock) 412{ 413 rollback_lock_file(&lock->lk); 414 free(lock->ref_name); 415 free(lock); 416} 417 418/* 419 * Lock refname, without following symrefs, and set *lock_p to point 420 * at a newly-allocated lock object. Fill in lock->old_oid, referent, 421 * and type similarly to read_raw_ref(). 422 * 423 * The caller must verify that refname is a "safe" reference name (in 424 * the sense of refname_is_safe()) before calling this function. 425 * 426 * If the reference doesn't already exist, verify that refname doesn't 427 * have a D/F conflict with any existing references. extras and skip 428 * are passed to refs_verify_refname_available() for this check. 429 * 430 * If mustexist is not set and the reference is not found or is 431 * broken, lock the reference anyway but clear sha1. 432 * 433 * Return 0 on success. On failure, write an error message to err and 434 * return TRANSACTION_NAME_CONFLICT or TRANSACTION_GENERIC_ERROR. 435 * 436 * Implementation note: This function is basically 437 * 438 * lock reference 439 * read_raw_ref() 440 * 441 * but it includes a lot more code to 442 * - Deal with possible races with other processes 443 * - Avoid calling refs_verify_refname_available() when it can be 444 * avoided, namely if we were successfully able to read the ref 445 * - Generate informative error messages in the case of failure 446 */ 447static int lock_raw_ref(struct files_ref_store *refs, 448 const char *refname, int mustexist, 449 const struct string_list *extras, 450 const struct string_list *skip, 451 struct ref_lock **lock_p, 452 struct strbuf *referent, 453 unsigned int *type, 454 struct strbuf *err) 455{ 456 struct ref_lock *lock; 457 struct strbuf ref_file = STRBUF_INIT; 458 int attempts_remaining = 3; 459 int ret = TRANSACTION_GENERIC_ERROR; 460 461 assert(err); 462 files_assert_main_repository(refs, "lock_raw_ref"); 463 464 *type = 0; 465 466 /* First lock the file so it can't change out from under us. */ 467 468 *lock_p = lock = xcalloc(1, sizeof(*lock)); 469 470 lock->ref_name = xstrdup(refname); 471 files_ref_path(refs, &ref_file, refname); 472 473retry: 474 switch (safe_create_leading_directories(ref_file.buf)) { 475 case SCLD_OK: 476 break; /* success */ 477 case SCLD_EXISTS: 478 /* 479 * Suppose refname is "refs/foo/bar". We just failed 480 * to create the containing directory, "refs/foo", 481 * because there was a non-directory in the way. This 482 * indicates a D/F conflict, probably because of 483 * another reference such as "refs/foo". There is no 484 * reason to expect this error to be transitory. 485 */ 486 if (refs_verify_refname_available(&refs->base, refname, 487 extras, skip, err)) { 488 if (mustexist) { 489 /* 490 * To the user the relevant error is 491 * that the "mustexist" reference is 492 * missing: 493 */ 494 strbuf_reset(err); 495 strbuf_addf(err, "unable to resolve reference '%s'", 496 refname); 497 } else { 498 /* 499 * The error message set by 500 * refs_verify_refname_available() is 501 * OK. 502 */ 503 ret = TRANSACTION_NAME_CONFLICT; 504 } 505 } else { 506 /* 507 * The file that is in the way isn't a loose 508 * reference. Report it as a low-level 509 * failure. 510 */ 511 strbuf_addf(err, "unable to create lock file %s.lock; " 512 "non-directory in the way", 513 ref_file.buf); 514 } 515 goto error_return; 516 case SCLD_VANISHED: 517 /* Maybe another process was tidying up. Try again. */ 518 if (--attempts_remaining > 0) 519 goto retry; 520 /* fall through */ 521 default: 522 strbuf_addf(err, "unable to create directory for %s", 523 ref_file.buf); 524 goto error_return; 525 } 526 527 if (hold_lock_file_for_update_timeout( 528 &lock->lk, ref_file.buf, LOCK_NO_DEREF, 529 get_files_ref_lock_timeout_ms()) < 0) { 530 if (errno == ENOENT && --attempts_remaining > 0) { 531 /* 532 * Maybe somebody just deleted one of the 533 * directories leading to ref_file. Try 534 * again: 535 */ 536 goto retry; 537 } else { 538 unable_to_lock_message(ref_file.buf, errno, err); 539 goto error_return; 540 } 541 } 542 543 /* 544 * Now we hold the lock and can read the reference without 545 * fear that its value will change. 546 */ 547 548 if (files_read_raw_ref(&refs->base, refname, 549 &lock->old_oid, referent, type)) { 550 if (errno == ENOENT) { 551 if (mustexist) { 552 /* Garden variety missing reference. */ 553 strbuf_addf(err, "unable to resolve reference '%s'", 554 refname); 555 goto error_return; 556 } else { 557 /* 558 * Reference is missing, but that's OK. We 559 * know that there is not a conflict with 560 * another loose reference because 561 * (supposing that we are trying to lock 562 * reference "refs/foo/bar"): 563 * 564 * - We were successfully able to create 565 * the lockfile refs/foo/bar.lock, so we 566 * know there cannot be a loose reference 567 * named "refs/foo". 568 * 569 * - We got ENOENT and not EISDIR, so we 570 * know that there cannot be a loose 571 * reference named "refs/foo/bar/baz". 572 */ 573 } 574 } else if (errno == EISDIR) { 575 /* 576 * There is a directory in the way. It might have 577 * contained references that have been deleted. If 578 * we don't require that the reference already 579 * exists, try to remove the directory so that it 580 * doesn't cause trouble when we want to rename the 581 * lockfile into place later. 582 */ 583 if (mustexist) { 584 /* Garden variety missing reference. */ 585 strbuf_addf(err, "unable to resolve reference '%s'", 586 refname); 587 goto error_return; 588 } else if (remove_dir_recursively(&ref_file, 589 REMOVE_DIR_EMPTY_ONLY)) { 590 if (refs_verify_refname_available( 591 &refs->base, refname, 592 extras, skip, err)) { 593 /* 594 * The error message set by 595 * verify_refname_available() is OK. 596 */ 597 ret = TRANSACTION_NAME_CONFLICT; 598 goto error_return; 599 } else { 600 /* 601 * We can't delete the directory, 602 * but we also don't know of any 603 * references that it should 604 * contain. 605 */ 606 strbuf_addf(err, "there is a non-empty directory '%s' " 607 "blocking reference '%s'", 608 ref_file.buf, refname); 609 goto error_return; 610 } 611 } 612 } else if (errno == EINVAL && (*type & REF_ISBROKEN)) { 613 strbuf_addf(err, "unable to resolve reference '%s': " 614 "reference broken", refname); 615 goto error_return; 616 } else { 617 strbuf_addf(err, "unable to resolve reference '%s': %s", 618 refname, strerror(errno)); 619 goto error_return; 620 } 621 622 /* 623 * If the ref did not exist and we are creating it, 624 * make sure there is no existing packed ref that 625 * conflicts with refname: 626 */ 627 if (refs_verify_refname_available( 628 refs->packed_ref_store, refname, 629 extras, skip, err)) 630 goto error_return; 631 } 632 633 ret = 0; 634 goto out; 635 636error_return: 637 unlock_ref(lock); 638 *lock_p = NULL; 639 640out: 641 strbuf_release(&ref_file); 642 return ret; 643} 644 645struct files_ref_iterator { 646 struct ref_iterator base; 647 648 struct ref_iterator *iter0; 649 unsigned int flags; 650}; 651 652static int files_ref_iterator_advance(struct ref_iterator *ref_iterator) 653{ 654 struct files_ref_iterator *iter = 655 (struct files_ref_iterator *)ref_iterator; 656 int ok; 657 658 while ((ok = ref_iterator_advance(iter->iter0)) == ITER_OK) { 659 if (iter->flags & DO_FOR_EACH_PER_WORKTREE_ONLY && 660 ref_type(iter->iter0->refname) != REF_TYPE_PER_WORKTREE) 661 continue; 662 663 if (!(iter->flags & DO_FOR_EACH_INCLUDE_BROKEN) && 664 !ref_resolves_to_object(iter->iter0->refname, 665 iter->iter0->oid, 666 iter->iter0->flags)) 667 continue; 668 669 iter->base.refname = iter->iter0->refname; 670 iter->base.oid = iter->iter0->oid; 671 iter->base.flags = iter->iter0->flags; 672 return ITER_OK; 673 } 674 675 iter->iter0 = NULL; 676 if (ref_iterator_abort(ref_iterator) != ITER_DONE) 677 ok = ITER_ERROR; 678 679 return ok; 680} 681 682static int files_ref_iterator_peel(struct ref_iterator *ref_iterator, 683 struct object_id *peeled) 684{ 685 struct files_ref_iterator *iter = 686 (struct files_ref_iterator *)ref_iterator; 687 688 return ref_iterator_peel(iter->iter0, peeled); 689} 690 691static int files_ref_iterator_abort(struct ref_iterator *ref_iterator) 692{ 693 struct files_ref_iterator *iter = 694 (struct files_ref_iterator *)ref_iterator; 695 int ok = ITER_DONE; 696 697 if (iter->iter0) 698 ok = ref_iterator_abort(iter->iter0); 699 700 base_ref_iterator_free(ref_iterator); 701 return ok; 702} 703 704static struct ref_iterator_vtable files_ref_iterator_vtable = { 705 files_ref_iterator_advance, 706 files_ref_iterator_peel, 707 files_ref_iterator_abort 708}; 709 710static struct ref_iterator *files_ref_iterator_begin( 711 struct ref_store *ref_store, 712 const char *prefix, unsigned int flags) 713{ 714 struct files_ref_store *refs; 715 struct ref_iterator *loose_iter, *packed_iter, *overlay_iter; 716 struct files_ref_iterator *iter; 717 struct ref_iterator *ref_iterator; 718 unsigned int required_flags = REF_STORE_READ; 719 720 if (!(flags & DO_FOR_EACH_INCLUDE_BROKEN)) 721 required_flags |= REF_STORE_ODB; 722 723 refs = files_downcast(ref_store, required_flags, "ref_iterator_begin"); 724 725 /* 726 * We must make sure that all loose refs are read before 727 * accessing the packed-refs file; this avoids a race 728 * condition if loose refs are migrated to the packed-refs 729 * file by a simultaneous process, but our in-memory view is 730 * from before the migration. We ensure this as follows: 731 * First, we call start the loose refs iteration with its 732 * `prime_ref` argument set to true. This causes the loose 733 * references in the subtree to be pre-read into the cache. 734 * (If they've already been read, that's OK; we only need to 735 * guarantee that they're read before the packed refs, not 736 * *how much* before.) After that, we call 737 * packed_ref_iterator_begin(), which internally checks 738 * whether the packed-ref cache is up to date with what is on 739 * disk, and re-reads it if not. 740 */ 741 742 loose_iter = cache_ref_iterator_begin(get_loose_ref_cache(refs), 743 prefix, 1); 744 745 /* 746 * The packed-refs file might contain broken references, for 747 * example an old version of a reference that points at an 748 * object that has since been garbage-collected. This is OK as 749 * long as there is a corresponding loose reference that 750 * overrides it, and we don't want to emit an error message in 751 * this case. So ask the packed_ref_store for all of its 752 * references, and (if needed) do our own check for broken 753 * ones in files_ref_iterator_advance(), after we have merged 754 * the packed and loose references. 755 */ 756 packed_iter = refs_ref_iterator_begin( 757 refs->packed_ref_store, prefix, 0, 758 DO_FOR_EACH_INCLUDE_BROKEN); 759 760 overlay_iter = overlay_ref_iterator_begin(loose_iter, packed_iter); 761 762 iter = xcalloc(1, sizeof(*iter)); 763 ref_iterator = &iter->base; 764 base_ref_iterator_init(ref_iterator, &files_ref_iterator_vtable, 765 overlay_iter->ordered); 766 iter->iter0 = overlay_iter; 767 iter->flags = flags; 768 769 return ref_iterator; 770} 771 772/* 773 * Verify that the reference locked by lock has the value old_oid 774 * (unless it is NULL). Fail if the reference doesn't exist and 775 * mustexist is set. Return 0 on success. On error, write an error 776 * message to err, set errno, and return a negative value. 777 */ 778static int verify_lock(struct ref_store *ref_store, struct ref_lock *lock, 779 const struct object_id *old_oid, int mustexist, 780 struct strbuf *err) 781{ 782 assert(err); 783 784 if (refs_read_ref_full(ref_store, lock->ref_name, 785 mustexist ? RESOLVE_REF_READING : 0, 786 &lock->old_oid, NULL)) { 787 if (old_oid) { 788 int save_errno = errno; 789 strbuf_addf(err, "can't verify ref '%s'", lock->ref_name); 790 errno = save_errno; 791 return -1; 792 } else { 793 oidclr(&lock->old_oid); 794 return 0; 795 } 796 } 797 if (old_oid && oidcmp(&lock->old_oid, old_oid)) { 798 strbuf_addf(err, "ref '%s' is at %s but expected %s", 799 lock->ref_name, 800 oid_to_hex(&lock->old_oid), 801 oid_to_hex(old_oid)); 802 errno = EBUSY; 803 return -1; 804 } 805 return 0; 806} 807 808static int remove_empty_directories(struct strbuf *path) 809{ 810 /* 811 * we want to create a file but there is a directory there; 812 * if that is an empty directory (or a directory that contains 813 * only empty directories), remove them. 814 */ 815 return remove_dir_recursively(path, REMOVE_DIR_EMPTY_ONLY); 816} 817 818static int create_reflock(const char *path, void *cb) 819{ 820 struct lock_file *lk = cb; 821 822 return hold_lock_file_for_update_timeout( 823 lk, path, LOCK_NO_DEREF, 824 get_files_ref_lock_timeout_ms()) < 0 ? -1 : 0; 825} 826 827/* 828 * Locks a ref returning the lock on success and NULL on failure. 829 * On failure errno is set to something meaningful. 830 */ 831static struct ref_lock *lock_ref_oid_basic(struct files_ref_store *refs, 832 const char *refname, 833 const struct object_id *old_oid, 834 const struct string_list *extras, 835 const struct string_list *skip, 836 unsigned int flags, int *type, 837 struct strbuf *err) 838{ 839 struct strbuf ref_file = STRBUF_INIT; 840 struct ref_lock *lock; 841 int last_errno = 0; 842 int mustexist = (old_oid && !is_null_oid(old_oid)); 843 int resolve_flags = RESOLVE_REF_NO_RECURSE; 844 int resolved; 845 846 files_assert_main_repository(refs, "lock_ref_oid_basic"); 847 assert(err); 848 849 lock = xcalloc(1, sizeof(struct ref_lock)); 850 851 if (mustexist) 852 resolve_flags |= RESOLVE_REF_READING; 853 if (flags & REF_DELETING) 854 resolve_flags |= RESOLVE_REF_ALLOW_BAD_NAME; 855 856 files_ref_path(refs, &ref_file, refname); 857 resolved = !!refs_resolve_ref_unsafe(&refs->base, 858 refname, resolve_flags, 859 &lock->old_oid, type); 860 if (!resolved && errno == EISDIR) { 861 /* 862 * we are trying to lock foo but we used to 863 * have foo/bar which now does not exist; 864 * it is normal for the empty directory 'foo' 865 * to remain. 866 */ 867 if (remove_empty_directories(&ref_file)) { 868 last_errno = errno; 869 if (!refs_verify_refname_available( 870 &refs->base, 871 refname, extras, skip, err)) 872 strbuf_addf(err, "there are still refs under '%s'", 873 refname); 874 goto error_return; 875 } 876 resolved = !!refs_resolve_ref_unsafe(&refs->base, 877 refname, resolve_flags, 878 &lock->old_oid, type); 879 } 880 if (!resolved) { 881 last_errno = errno; 882 if (last_errno != ENOTDIR || 883 !refs_verify_refname_available(&refs->base, refname, 884 extras, skip, err)) 885 strbuf_addf(err, "unable to resolve reference '%s': %s", 886 refname, strerror(last_errno)); 887 888 goto error_return; 889 } 890 891 /* 892 * If the ref did not exist and we are creating it, make sure 893 * there is no existing packed ref whose name begins with our 894 * refname, nor a packed ref whose name is a proper prefix of 895 * our refname. 896 */ 897 if (is_null_oid(&lock->old_oid) && 898 refs_verify_refname_available(refs->packed_ref_store, refname, 899 extras, skip, err)) { 900 last_errno = ENOTDIR; 901 goto error_return; 902 } 903 904 lock->ref_name = xstrdup(refname); 905 906 if (raceproof_create_file(ref_file.buf, create_reflock, &lock->lk)) { 907 last_errno = errno; 908 unable_to_lock_message(ref_file.buf, errno, err); 909 goto error_return; 910 } 911 912 if (verify_lock(&refs->base, lock, old_oid, mustexist, err)) { 913 last_errno = errno; 914 goto error_return; 915 } 916 goto out; 917 918 error_return: 919 unlock_ref(lock); 920 lock = NULL; 921 922 out: 923 strbuf_release(&ref_file); 924 errno = last_errno; 925 return lock; 926} 927 928struct ref_to_prune { 929 struct ref_to_prune *next; 930 struct object_id oid; 931 char name[FLEX_ARRAY]; 932}; 933 934enum { 935 REMOVE_EMPTY_PARENTS_REF = 0x01, 936 REMOVE_EMPTY_PARENTS_REFLOG = 0x02 937}; 938 939/* 940 * Remove empty parent directories associated with the specified 941 * reference and/or its reflog, but spare [logs/]refs/ and immediate 942 * subdirs. flags is a combination of REMOVE_EMPTY_PARENTS_REF and/or 943 * REMOVE_EMPTY_PARENTS_REFLOG. 944 */ 945static void try_remove_empty_parents(struct files_ref_store *refs, 946 const char *refname, 947 unsigned int flags) 948{ 949 struct strbuf buf = STRBUF_INIT; 950 struct strbuf sb = STRBUF_INIT; 951 char *p, *q; 952 int i; 953 954 strbuf_addstr(&buf, refname); 955 p = buf.buf; 956 for (i = 0; i < 2; i++) { /* refs/{heads,tags,...}/ */ 957 while (*p && *p != '/') 958 p++; 959 /* tolerate duplicate slashes; see check_refname_format() */ 960 while (*p == '/') 961 p++; 962 } 963 q = buf.buf + buf.len; 964 while (flags & (REMOVE_EMPTY_PARENTS_REF | REMOVE_EMPTY_PARENTS_REFLOG)) { 965 while (q > p && *q != '/') 966 q--; 967 while (q > p && *(q-1) == '/') 968 q--; 969 if (q == p) 970 break; 971 strbuf_setlen(&buf, q - buf.buf); 972 973 strbuf_reset(&sb); 974 files_ref_path(refs, &sb, buf.buf); 975 if ((flags & REMOVE_EMPTY_PARENTS_REF) && rmdir(sb.buf)) 976 flags &= ~REMOVE_EMPTY_PARENTS_REF; 977 978 strbuf_reset(&sb); 979 files_reflog_path(refs, &sb, buf.buf); 980 if ((flags & REMOVE_EMPTY_PARENTS_REFLOG) && rmdir(sb.buf)) 981 flags &= ~REMOVE_EMPTY_PARENTS_REFLOG; 982 } 983 strbuf_release(&buf); 984 strbuf_release(&sb); 985} 986 987/* make sure nobody touched the ref, and unlink */ 988static void prune_ref(struct files_ref_store *refs, struct ref_to_prune *r) 989{ 990 struct ref_transaction *transaction; 991 struct strbuf err = STRBUF_INIT; 992 993 if (check_refname_format(r->name, 0)) 994 return; 995 996 transaction = ref_store_transaction_begin(&refs->base, &err); 997 if (!transaction || 998 ref_transaction_delete(transaction, r->name, &r->oid, 999 REF_ISPRUNING | REF_NODEREF, NULL, &err) ||1000 ref_transaction_commit(transaction, &err)) {1001 ref_transaction_free(transaction);1002 error("%s", err.buf);1003 strbuf_release(&err);1004 return;1005 }1006 ref_transaction_free(transaction);1007 strbuf_release(&err);1008}10091010/*1011 * Prune the loose versions of the references in the linked list1012 * `*refs_to_prune`, freeing the entries in the list as we go.1013 */1014static void prune_refs(struct files_ref_store *refs, struct ref_to_prune **refs_to_prune)1015{1016 while (*refs_to_prune) {1017 struct ref_to_prune *r = *refs_to_prune;1018 *refs_to_prune = r->next;1019 prune_ref(refs, r);1020 free(r);1021 }1022}10231024/*1025 * Return true if the specified reference should be packed.1026 */1027static int should_pack_ref(const char *refname,1028 const struct object_id *oid, unsigned int ref_flags,1029 unsigned int pack_flags)1030{1031 /* Do not pack per-worktree refs: */1032 if (ref_type(refname) != REF_TYPE_NORMAL)1033 return 0;10341035 /* Do not pack non-tags unless PACK_REFS_ALL is set: */1036 if (!(pack_flags & PACK_REFS_ALL) && !starts_with(refname, "refs/tags/"))1037 return 0;10381039 /* Do not pack symbolic refs: */1040 if (ref_flags & REF_ISSYMREF)1041 return 0;10421043 /* Do not pack broken refs: */1044 if (!ref_resolves_to_object(refname, oid, ref_flags))1045 return 0;10461047 return 1;1048}10491050static int files_pack_refs(struct ref_store *ref_store, unsigned int flags)1051{1052 struct files_ref_store *refs =1053 files_downcast(ref_store, REF_STORE_WRITE | REF_STORE_ODB,1054 "pack_refs");1055 struct ref_iterator *iter;1056 int ok;1057 struct ref_to_prune *refs_to_prune = NULL;1058 struct strbuf err = STRBUF_INIT;1059 struct ref_transaction *transaction;10601061 transaction = ref_store_transaction_begin(refs->packed_ref_store, &err);1062 if (!transaction)1063 return -1;10641065 packed_refs_lock(refs->packed_ref_store, LOCK_DIE_ON_ERROR, &err);10661067 iter = cache_ref_iterator_begin(get_loose_ref_cache(refs), NULL, 0);1068 while ((ok = ref_iterator_advance(iter)) == ITER_OK) {1069 /*1070 * If the loose reference can be packed, add an entry1071 * in the packed ref cache. If the reference should be1072 * pruned, also add it to refs_to_prune.1073 */1074 if (!should_pack_ref(iter->refname, iter->oid, iter->flags,1075 flags))1076 continue;10771078 /*1079 * Add a reference creation for this reference to the1080 * packed-refs transaction:1081 */1082 if (ref_transaction_update(transaction, iter->refname,1083 iter->oid, NULL,1084 REF_NODEREF, NULL, &err))1085 die("failure preparing to create packed reference %s: %s",1086 iter->refname, err.buf);10871088 /* Schedule the loose reference for pruning if requested. */1089 if ((flags & PACK_REFS_PRUNE)) {1090 struct ref_to_prune *n;1091 FLEX_ALLOC_STR(n, name, iter->refname);1092 oidcpy(&n->oid, iter->oid);1093 n->next = refs_to_prune;1094 refs_to_prune = n;1095 }1096 }1097 if (ok != ITER_DONE)1098 die("error while iterating over references");10991100 if (ref_transaction_commit(transaction, &err))1101 die("unable to write new packed-refs: %s", err.buf);11021103 ref_transaction_free(transaction);11041105 packed_refs_unlock(refs->packed_ref_store);11061107 prune_refs(refs, &refs_to_prune);1108 strbuf_release(&err);1109 return 0;1110}11111112static int files_delete_refs(struct ref_store *ref_store, const char *msg,1113 struct string_list *refnames, unsigned int flags)1114{1115 struct files_ref_store *refs =1116 files_downcast(ref_store, REF_STORE_WRITE, "delete_refs");1117 struct strbuf err = STRBUF_INIT;1118 int i, result = 0;11191120 if (!refnames->nr)1121 return 0;11221123 if (packed_refs_lock(refs->packed_ref_store, 0, &err))1124 goto error;11251126 if (refs_delete_refs(refs->packed_ref_store, msg, refnames, flags)) {1127 packed_refs_unlock(refs->packed_ref_store);1128 goto error;1129 }11301131 packed_refs_unlock(refs->packed_ref_store);11321133 for (i = 0; i < refnames->nr; i++) {1134 const char *refname = refnames->items[i].string;11351136 if (refs_delete_ref(&refs->base, msg, refname, NULL, flags))1137 result |= error(_("could not remove reference %s"), refname);1138 }11391140 strbuf_release(&err);1141 return result;11421143error:1144 /*1145 * If we failed to rewrite the packed-refs file, then it is1146 * unsafe to try to remove loose refs, because doing so might1147 * expose an obsolete packed value for a reference that might1148 * even point at an object that has been garbage collected.1149 */1150 if (refnames->nr == 1)1151 error(_("could not delete reference %s: %s"),1152 refnames->items[0].string, err.buf);1153 else1154 error(_("could not delete references: %s"), err.buf);11551156 strbuf_release(&err);1157 return -1;1158}11591160/*1161 * People using contrib's git-new-workdir have .git/logs/refs ->1162 * /some/other/path/.git/logs/refs, and that may live on another device.1163 *1164 * IOW, to avoid cross device rename errors, the temporary renamed log must1165 * live into logs/refs.1166 */1167#define TMP_RENAMED_LOG "refs/.tmp-renamed-log"11681169struct rename_cb {1170 const char *tmp_renamed_log;1171 int true_errno;1172};11731174static int rename_tmp_log_callback(const char *path, void *cb_data)1175{1176 struct rename_cb *cb = cb_data;11771178 if (rename(cb->tmp_renamed_log, path)) {1179 /*1180 * rename(a, b) when b is an existing directory ought1181 * to result in ISDIR, but Solaris 5.8 gives ENOTDIR.1182 * Sheesh. Record the true errno for error reporting,1183 * but report EISDIR to raceproof_create_file() so1184 * that it knows to retry.1185 */1186 cb->true_errno = errno;1187 if (errno == ENOTDIR)1188 errno = EISDIR;1189 return -1;1190 } else {1191 return 0;1192 }1193}11941195static int rename_tmp_log(struct files_ref_store *refs, const char *newrefname)1196{1197 struct strbuf path = STRBUF_INIT;1198 struct strbuf tmp = STRBUF_INIT;1199 struct rename_cb cb;1200 int ret;12011202 files_reflog_path(refs, &path, newrefname);1203 files_reflog_path(refs, &tmp, TMP_RENAMED_LOG);1204 cb.tmp_renamed_log = tmp.buf;1205 ret = raceproof_create_file(path.buf, rename_tmp_log_callback, &cb);1206 if (ret) {1207 if (errno == EISDIR)1208 error("directory not empty: %s", path.buf);1209 else1210 error("unable to move logfile %s to %s: %s",1211 tmp.buf, path.buf,1212 strerror(cb.true_errno));1213 }12141215 strbuf_release(&path);1216 strbuf_release(&tmp);1217 return ret;1218}12191220static int write_ref_to_lockfile(struct ref_lock *lock,1221 const struct object_id *oid, struct strbuf *err);1222static int commit_ref_update(struct files_ref_store *refs,1223 struct ref_lock *lock,1224 const struct object_id *oid, const char *logmsg,1225 struct strbuf *err);12261227static int files_copy_or_rename_ref(struct ref_store *ref_store,1228 const char *oldrefname, const char *newrefname,1229 const char *logmsg, int copy)1230{1231 struct files_ref_store *refs =1232 files_downcast(ref_store, REF_STORE_WRITE, "rename_ref");1233 struct object_id oid, orig_oid;1234 int flag = 0, logmoved = 0;1235 struct ref_lock *lock;1236 struct stat loginfo;1237 struct strbuf sb_oldref = STRBUF_INIT;1238 struct strbuf sb_newref = STRBUF_INIT;1239 struct strbuf tmp_renamed_log = STRBUF_INIT;1240 int log, ret;1241 struct strbuf err = STRBUF_INIT;12421243 files_reflog_path(refs, &sb_oldref, oldrefname);1244 files_reflog_path(refs, &sb_newref, newrefname);1245 files_reflog_path(refs, &tmp_renamed_log, TMP_RENAMED_LOG);12461247 log = !lstat(sb_oldref.buf, &loginfo);1248 if (log && S_ISLNK(loginfo.st_mode)) {1249 ret = error("reflog for %s is a symlink", oldrefname);1250 goto out;1251 }12521253 if (!refs_resolve_ref_unsafe(&refs->base, oldrefname,1254 RESOLVE_REF_READING | RESOLVE_REF_NO_RECURSE,1255 &orig_oid, &flag)) {1256 ret = error("refname %s not found", oldrefname);1257 goto out;1258 }12591260 if (flag & REF_ISSYMREF) {1261 if (copy)1262 ret = error("refname %s is a symbolic ref, copying it is not supported",1263 oldrefname);1264 else1265 ret = error("refname %s is a symbolic ref, renaming it is not supported",1266 oldrefname);1267 goto out;1268 }1269 if (!refs_rename_ref_available(&refs->base, oldrefname, newrefname)) {1270 ret = 1;1271 goto out;1272 }12731274 if (!copy && log && rename(sb_oldref.buf, tmp_renamed_log.buf)) {1275 ret = error("unable to move logfile logs/%s to logs/"TMP_RENAMED_LOG": %s",1276 oldrefname, strerror(errno));1277 goto out;1278 }12791280 if (copy && log && copy_file(tmp_renamed_log.buf, sb_oldref.buf, 0644)) {1281 ret = error("unable to copy logfile logs/%s to logs/"TMP_RENAMED_LOG": %s",1282 oldrefname, strerror(errno));1283 goto out;1284 }12851286 if (!copy && refs_delete_ref(&refs->base, logmsg, oldrefname,1287 &orig_oid, REF_NODEREF)) {1288 error("unable to delete old %s", oldrefname);1289 goto rollback;1290 }12911292 /*1293 * Since we are doing a shallow lookup, oid is not the1294 * correct value to pass to delete_ref as old_oid. But that1295 * doesn't matter, because an old_oid check wouldn't add to1296 * the safety anyway; we want to delete the reference whatever1297 * its current value.1298 */1299 if (!copy && !refs_read_ref_full(&refs->base, newrefname,1300 RESOLVE_REF_READING | RESOLVE_REF_NO_RECURSE,1301 &oid, NULL) &&1302 refs_delete_ref(&refs->base, NULL, newrefname,1303 NULL, REF_NODEREF)) {1304 if (errno == EISDIR) {1305 struct strbuf path = STRBUF_INIT;1306 int result;13071308 files_ref_path(refs, &path, newrefname);1309 result = remove_empty_directories(&path);1310 strbuf_release(&path);13111312 if (result) {1313 error("Directory not empty: %s", newrefname);1314 goto rollback;1315 }1316 } else {1317 error("unable to delete existing %s", newrefname);1318 goto rollback;1319 }1320 }13211322 if (log && rename_tmp_log(refs, newrefname))1323 goto rollback;13241325 logmoved = log;13261327 lock = lock_ref_oid_basic(refs, newrefname, NULL, NULL, NULL,1328 REF_NODEREF, NULL, &err);1329 if (!lock) {1330 if (copy)1331 error("unable to copy '%s' to '%s': %s", oldrefname, newrefname, err.buf);1332 else1333 error("unable to rename '%s' to '%s': %s", oldrefname, newrefname, err.buf);1334 strbuf_release(&err);1335 goto rollback;1336 }1337 oidcpy(&lock->old_oid, &orig_oid);13381339 if (write_ref_to_lockfile(lock, &orig_oid, &err) ||1340 commit_ref_update(refs, lock, &orig_oid, logmsg, &err)) {1341 error("unable to write current sha1 into %s: %s", newrefname, err.buf);1342 strbuf_release(&err);1343 goto rollback;1344 }13451346 ret = 0;1347 goto out;13481349 rollback:1350 lock = lock_ref_oid_basic(refs, oldrefname, NULL, NULL, NULL,1351 REF_NODEREF, NULL, &err);1352 if (!lock) {1353 error("unable to lock %s for rollback: %s", oldrefname, err.buf);1354 strbuf_release(&err);1355 goto rollbacklog;1356 }13571358 flag = log_all_ref_updates;1359 log_all_ref_updates = LOG_REFS_NONE;1360 if (write_ref_to_lockfile(lock, &orig_oid, &err) ||1361 commit_ref_update(refs, lock, &orig_oid, NULL, &err)) {1362 error("unable to write current sha1 into %s: %s", oldrefname, err.buf);1363 strbuf_release(&err);1364 }1365 log_all_ref_updates = flag;13661367 rollbacklog:1368 if (logmoved && rename(sb_newref.buf, sb_oldref.buf))1369 error("unable to restore logfile %s from %s: %s",1370 oldrefname, newrefname, strerror(errno));1371 if (!logmoved && log &&1372 rename(tmp_renamed_log.buf, sb_oldref.buf))1373 error("unable to restore logfile %s from logs/"TMP_RENAMED_LOG": %s",1374 oldrefname, strerror(errno));1375 ret = 1;1376 out:1377 strbuf_release(&sb_newref);1378 strbuf_release(&sb_oldref);1379 strbuf_release(&tmp_renamed_log);13801381 return ret;1382}13831384static int files_rename_ref(struct ref_store *ref_store,1385 const char *oldrefname, const char *newrefname,1386 const char *logmsg)1387{1388 return files_copy_or_rename_ref(ref_store, oldrefname,1389 newrefname, logmsg, 0);1390}13911392static int files_copy_ref(struct ref_store *ref_store,1393 const char *oldrefname, const char *newrefname,1394 const char *logmsg)1395{1396 return files_copy_or_rename_ref(ref_store, oldrefname,1397 newrefname, logmsg, 1);1398}13991400static int close_ref_gently(struct ref_lock *lock)1401{1402 if (close_lock_file_gently(&lock->lk))1403 return -1;1404 return 0;1405}14061407static int commit_ref(struct ref_lock *lock)1408{1409 char *path = get_locked_file_path(&lock->lk);1410 struct stat st;14111412 if (!lstat(path, &st) && S_ISDIR(st.st_mode)) {1413 /*1414 * There is a directory at the path we want to rename1415 * the lockfile to. Hopefully it is empty; try to1416 * delete it.1417 */1418 size_t len = strlen(path);1419 struct strbuf sb_path = STRBUF_INIT;14201421 strbuf_attach(&sb_path, path, len, len);14221423 /*1424 * If this fails, commit_lock_file() will also fail1425 * and will report the problem.1426 */1427 remove_empty_directories(&sb_path);1428 strbuf_release(&sb_path);1429 } else {1430 free(path);1431 }14321433 if (commit_lock_file(&lock->lk))1434 return -1;1435 return 0;1436}14371438static int open_or_create_logfile(const char *path, void *cb)1439{1440 int *fd = cb;14411442 *fd = open(path, O_APPEND | O_WRONLY | O_CREAT, 0666);1443 return (*fd < 0) ? -1 : 0;1444}14451446/*1447 * Create a reflog for a ref. If force_create = 0, only create the1448 * reflog for certain refs (those for which should_autocreate_reflog1449 * returns non-zero). Otherwise, create it regardless of the reference1450 * name. If the logfile already existed or was created, return 0 and1451 * set *logfd to the file descriptor opened for appending to the file.1452 * If no logfile exists and we decided not to create one, return 0 and1453 * set *logfd to -1. On failure, fill in *err, set *logfd to -1, and1454 * return -1.1455 */1456static int log_ref_setup(struct files_ref_store *refs,1457 const char *refname, int force_create,1458 int *logfd, struct strbuf *err)1459{1460 struct strbuf logfile_sb = STRBUF_INIT;1461 char *logfile;14621463 files_reflog_path(refs, &logfile_sb, refname);1464 logfile = strbuf_detach(&logfile_sb, NULL);14651466 if (force_create || should_autocreate_reflog(refname)) {1467 if (raceproof_create_file(logfile, open_or_create_logfile, logfd)) {1468 if (errno == ENOENT)1469 strbuf_addf(err, "unable to create directory for '%s': "1470 "%s", logfile, strerror(errno));1471 else if (errno == EISDIR)1472 strbuf_addf(err, "there are still logs under '%s'",1473 logfile);1474 else1475 strbuf_addf(err, "unable to append to '%s': %s",1476 logfile, strerror(errno));14771478 goto error;1479 }1480 } else {1481 *logfd = open(logfile, O_APPEND | O_WRONLY, 0666);1482 if (*logfd < 0) {1483 if (errno == ENOENT || errno == EISDIR) {1484 /*1485 * The logfile doesn't already exist,1486 * but that is not an error; it only1487 * means that we won't write log1488 * entries to it.1489 */1490 ;1491 } else {1492 strbuf_addf(err, "unable to append to '%s': %s",1493 logfile, strerror(errno));1494 goto error;1495 }1496 }1497 }14981499 if (*logfd >= 0)1500 adjust_shared_perm(logfile);15011502 free(logfile);1503 return 0;15041505error:1506 free(logfile);1507 return -1;1508}15091510static int files_create_reflog(struct ref_store *ref_store,1511 const char *refname, int force_create,1512 struct strbuf *err)1513{1514 struct files_ref_store *refs =1515 files_downcast(ref_store, REF_STORE_WRITE, "create_reflog");1516 int fd;15171518 if (log_ref_setup(refs, refname, force_create, &fd, err))1519 return -1;15201521 if (fd >= 0)1522 close(fd);15231524 return 0;1525}15261527static int log_ref_write_fd(int fd, const struct object_id *old_oid,1528 const struct object_id *new_oid,1529 const char *committer, const char *msg)1530{1531 int msglen, written;1532 unsigned maxlen, len;1533 char *logrec;15341535 msglen = msg ? strlen(msg) : 0;1536 maxlen = strlen(committer) + msglen + 100;1537 logrec = xmalloc(maxlen);1538 len = xsnprintf(logrec, maxlen, "%s %s %s\n",1539 oid_to_hex(old_oid),1540 oid_to_hex(new_oid),1541 committer);1542 if (msglen)1543 len += copy_reflog_msg(logrec + len - 1, msg) - 1;15441545 written = len <= maxlen ? write_in_full(fd, logrec, len) : -1;1546 free(logrec);1547 if (written < 0)1548 return -1;15491550 return 0;1551}15521553static int files_log_ref_write(struct files_ref_store *refs,1554 const char *refname, const struct object_id *old_oid,1555 const struct object_id *new_oid, const char *msg,1556 int flags, struct strbuf *err)1557{1558 int logfd, result;15591560 if (log_all_ref_updates == LOG_REFS_UNSET)1561 log_all_ref_updates = is_bare_repository() ? LOG_REFS_NONE : LOG_REFS_NORMAL;15621563 result = log_ref_setup(refs, refname,1564 flags & REF_FORCE_CREATE_REFLOG,1565 &logfd, err);15661567 if (result)1568 return result;15691570 if (logfd < 0)1571 return 0;1572 result = log_ref_write_fd(logfd, old_oid, new_oid,1573 git_committer_info(0), msg);1574 if (result) {1575 struct strbuf sb = STRBUF_INIT;1576 int save_errno = errno;15771578 files_reflog_path(refs, &sb, refname);1579 strbuf_addf(err, "unable to append to '%s': %s",1580 sb.buf, strerror(save_errno));1581 strbuf_release(&sb);1582 close(logfd);1583 return -1;1584 }1585 if (close(logfd)) {1586 struct strbuf sb = STRBUF_INIT;1587 int save_errno = errno;15881589 files_reflog_path(refs, &sb, refname);1590 strbuf_addf(err, "unable to append to '%s': %s",1591 sb.buf, strerror(save_errno));1592 strbuf_release(&sb);1593 return -1;1594 }1595 return 0;1596}15971598/*1599 * Write sha1 into the open lockfile, then close the lockfile. On1600 * errors, rollback the lockfile, fill in *err and1601 * return -1.1602 */1603static int write_ref_to_lockfile(struct ref_lock *lock,1604 const struct object_id *oid, struct strbuf *err)1605{1606 static char term = '\n';1607 struct object *o;1608 int fd;16091610 o = parse_object(oid);1611 if (!o) {1612 strbuf_addf(err,1613 "trying to write ref '%s' with nonexistent object %s",1614 lock->ref_name, oid_to_hex(oid));1615 unlock_ref(lock);1616 return -1;1617 }1618 if (o->type != OBJ_COMMIT && is_branch(lock->ref_name)) {1619 strbuf_addf(err,1620 "trying to write non-commit object %s to branch '%s'",1621 oid_to_hex(oid), lock->ref_name);1622 unlock_ref(lock);1623 return -1;1624 }1625 fd = get_lock_file_fd(&lock->lk);1626 if (write_in_full(fd, oid_to_hex(oid), GIT_SHA1_HEXSZ) < 0 ||1627 write_in_full(fd, &term, 1) < 0 ||1628 close_ref_gently(lock) < 0) {1629 strbuf_addf(err,1630 "couldn't write '%s'", get_lock_file_path(&lock->lk));1631 unlock_ref(lock);1632 return -1;1633 }1634 return 0;1635}16361637/*1638 * Commit a change to a loose reference that has already been written1639 * to the loose reference lockfile. Also update the reflogs if1640 * necessary, using the specified lockmsg (which can be NULL).1641 */1642static int commit_ref_update(struct files_ref_store *refs,1643 struct ref_lock *lock,1644 const struct object_id *oid, const char *logmsg,1645 struct strbuf *err)1646{1647 files_assert_main_repository(refs, "commit_ref_update");16481649 clear_loose_ref_cache(refs);1650 if (files_log_ref_write(refs, lock->ref_name,1651 &lock->old_oid, oid,1652 logmsg, 0, err)) {1653 char *old_msg = strbuf_detach(err, NULL);1654 strbuf_addf(err, "cannot update the ref '%s': %s",1655 lock->ref_name, old_msg);1656 free(old_msg);1657 unlock_ref(lock);1658 return -1;1659 }16601661 if (strcmp(lock->ref_name, "HEAD") != 0) {1662 /*1663 * Special hack: If a branch is updated directly and HEAD1664 * points to it (may happen on the remote side of a push1665 * for example) then logically the HEAD reflog should be1666 * updated too.1667 * A generic solution implies reverse symref information,1668 * but finding all symrefs pointing to the given branch1669 * would be rather costly for this rare event (the direct1670 * update of a branch) to be worth it. So let's cheat and1671 * check with HEAD only which should cover 99% of all usage1672 * scenarios (even 100% of the default ones).1673 */1674 int head_flag;1675 const char *head_ref;16761677 head_ref = refs_resolve_ref_unsafe(&refs->base, "HEAD",1678 RESOLVE_REF_READING,1679 NULL, &head_flag);1680 if (head_ref && (head_flag & REF_ISSYMREF) &&1681 !strcmp(head_ref, lock->ref_name)) {1682 struct strbuf log_err = STRBUF_INIT;1683 if (files_log_ref_write(refs, "HEAD",1684 &lock->old_oid, oid,1685 logmsg, 0, &log_err)) {1686 error("%s", log_err.buf);1687 strbuf_release(&log_err);1688 }1689 }1690 }16911692 if (commit_ref(lock)) {1693 strbuf_addf(err, "couldn't set '%s'", lock->ref_name);1694 unlock_ref(lock);1695 return -1;1696 }16971698 unlock_ref(lock);1699 return 0;1700}17011702static int create_ref_symlink(struct ref_lock *lock, const char *target)1703{1704 int ret = -1;1705#ifndef NO_SYMLINK_HEAD1706 char *ref_path = get_locked_file_path(&lock->lk);1707 unlink(ref_path);1708 ret = symlink(target, ref_path);1709 free(ref_path);17101711 if (ret)1712 fprintf(stderr, "no symlink - falling back to symbolic ref\n");1713#endif1714 return ret;1715}17161717static void update_symref_reflog(struct files_ref_store *refs,1718 struct ref_lock *lock, const char *refname,1719 const char *target, const char *logmsg)1720{1721 struct strbuf err = STRBUF_INIT;1722 struct object_id new_oid;1723 if (logmsg &&1724 !refs_read_ref_full(&refs->base, target,1725 RESOLVE_REF_READING, &new_oid, NULL) &&1726 files_log_ref_write(refs, refname, &lock->old_oid,1727 &new_oid, logmsg, 0, &err)) {1728 error("%s", err.buf);1729 strbuf_release(&err);1730 }1731}17321733static int create_symref_locked(struct files_ref_store *refs,1734 struct ref_lock *lock, const char *refname,1735 const char *target, const char *logmsg)1736{1737 if (prefer_symlink_refs && !create_ref_symlink(lock, target)) {1738 update_symref_reflog(refs, lock, refname, target, logmsg);1739 return 0;1740 }17411742 if (!fdopen_lock_file(&lock->lk, "w"))1743 return error("unable to fdopen %s: %s",1744 lock->lk.tempfile->filename.buf, strerror(errno));17451746 update_symref_reflog(refs, lock, refname, target, logmsg);17471748 /* no error check; commit_ref will check ferror */1749 fprintf(lock->lk.tempfile->fp, "ref: %s\n", target);1750 if (commit_ref(lock) < 0)1751 return error("unable to write symref for %s: %s", refname,1752 strerror(errno));1753 return 0;1754}17551756static int files_create_symref(struct ref_store *ref_store,1757 const char *refname, const char *target,1758 const char *logmsg)1759{1760 struct files_ref_store *refs =1761 files_downcast(ref_store, REF_STORE_WRITE, "create_symref");1762 struct strbuf err = STRBUF_INIT;1763 struct ref_lock *lock;1764 int ret;17651766 lock = lock_ref_oid_basic(refs, refname, NULL,1767 NULL, NULL, REF_NODEREF, NULL,1768 &err);1769 if (!lock) {1770 error("%s", err.buf);1771 strbuf_release(&err);1772 return -1;1773 }17741775 ret = create_symref_locked(refs, lock, refname, target, logmsg);1776 unlock_ref(lock);1777 return ret;1778}17791780static int files_reflog_exists(struct ref_store *ref_store,1781 const char *refname)1782{1783 struct files_ref_store *refs =1784 files_downcast(ref_store, REF_STORE_READ, "reflog_exists");1785 struct strbuf sb = STRBUF_INIT;1786 struct stat st;1787 int ret;17881789 files_reflog_path(refs, &sb, refname);1790 ret = !lstat(sb.buf, &st) && S_ISREG(st.st_mode);1791 strbuf_release(&sb);1792 return ret;1793}17941795static int files_delete_reflog(struct ref_store *ref_store,1796 const char *refname)1797{1798 struct files_ref_store *refs =1799 files_downcast(ref_store, REF_STORE_WRITE, "delete_reflog");1800 struct strbuf sb = STRBUF_INIT;1801 int ret;18021803 files_reflog_path(refs, &sb, refname);1804 ret = remove_path(sb.buf);1805 strbuf_release(&sb);1806 return ret;1807}18081809static int show_one_reflog_ent(struct strbuf *sb, each_reflog_ent_fn fn, void *cb_data)1810{1811 struct object_id ooid, noid;1812 char *email_end, *message;1813 timestamp_t timestamp;1814 int tz;1815 const char *p = sb->buf;18161817 /* old SP new SP name <email> SP time TAB msg LF */1818 if (!sb->len || sb->buf[sb->len - 1] != '\n' ||1819 parse_oid_hex(p, &ooid, &p) || *p++ != ' ' ||1820 parse_oid_hex(p, &noid, &p) || *p++ != ' ' ||1821 !(email_end = strchr(p, '>')) ||1822 email_end[1] != ' ' ||1823 !(timestamp = parse_timestamp(email_end + 2, &message, 10)) ||1824 !message || message[0] != ' ' ||1825 (message[1] != '+' && message[1] != '-') ||1826 !isdigit(message[2]) || !isdigit(message[3]) ||1827 !isdigit(message[4]) || !isdigit(message[5]))1828 return 0; /* corrupt? */1829 email_end[1] = '\0';1830 tz = strtol(message + 1, NULL, 10);1831 if (message[6] != '\t')1832 message += 6;1833 else1834 message += 7;1835 return fn(&ooid, &noid, p, timestamp, tz, message, cb_data);1836}18371838static char *find_beginning_of_line(char *bob, char *scan)1839{1840 while (bob < scan && *(--scan) != '\n')1841 ; /* keep scanning backwards */1842 /*1843 * Return either beginning of the buffer, or LF at the end of1844 * the previous line.1845 */1846 return scan;1847}18481849static int files_for_each_reflog_ent_reverse(struct ref_store *ref_store,1850 const char *refname,1851 each_reflog_ent_fn fn,1852 void *cb_data)1853{1854 struct files_ref_store *refs =1855 files_downcast(ref_store, REF_STORE_READ,1856 "for_each_reflog_ent_reverse");1857 struct strbuf sb = STRBUF_INIT;1858 FILE *logfp;1859 long pos;1860 int ret = 0, at_tail = 1;18611862 files_reflog_path(refs, &sb, refname);1863 logfp = fopen(sb.buf, "r");1864 strbuf_release(&sb);1865 if (!logfp)1866 return -1;18671868 /* Jump to the end */1869 if (fseek(logfp, 0, SEEK_END) < 0)1870 ret = error("cannot seek back reflog for %s: %s",1871 refname, strerror(errno));1872 pos = ftell(logfp);1873 while (!ret && 0 < pos) {1874 int cnt;1875 size_t nread;1876 char buf[BUFSIZ];1877 char *endp, *scanp;18781879 /* Fill next block from the end */1880 cnt = (sizeof(buf) < pos) ? sizeof(buf) : pos;1881 if (fseek(logfp, pos - cnt, SEEK_SET)) {1882 ret = error("cannot seek back reflog for %s: %s",1883 refname, strerror(errno));1884 break;1885 }1886 nread = fread(buf, cnt, 1, logfp);1887 if (nread != 1) {1888 ret = error("cannot read %d bytes from reflog for %s: %s",1889 cnt, refname, strerror(errno));1890 break;1891 }1892 pos -= cnt;18931894 scanp = endp = buf + cnt;1895 if (at_tail && scanp[-1] == '\n')1896 /* Looking at the final LF at the end of the file */1897 scanp--;1898 at_tail = 0;18991900 while (buf < scanp) {1901 /*1902 * terminating LF of the previous line, or the beginning1903 * of the buffer.1904 */1905 char *bp;19061907 bp = find_beginning_of_line(buf, scanp);19081909 if (*bp == '\n') {1910 /*1911 * The newline is the end of the previous line,1912 * so we know we have complete line starting1913 * at (bp + 1). Prefix it onto any prior data1914 * we collected for the line and process it.1915 */1916 strbuf_splice(&sb, 0, 0, bp + 1, endp - (bp + 1));1917 scanp = bp;1918 endp = bp + 1;1919 ret = show_one_reflog_ent(&sb, fn, cb_data);1920 strbuf_reset(&sb);1921 if (ret)1922 break;1923 } else if (!pos) {1924 /*1925 * We are at the start of the buffer, and the1926 * start of the file; there is no previous1927 * line, and we have everything for this one.1928 * Process it, and we can end the loop.1929 */1930 strbuf_splice(&sb, 0, 0, buf, endp - buf);1931 ret = show_one_reflog_ent(&sb, fn, cb_data);1932 strbuf_reset(&sb);1933 break;1934 }19351936 if (bp == buf) {1937 /*1938 * We are at the start of the buffer, and there1939 * is more file to read backwards. Which means1940 * we are in the middle of a line. Note that we1941 * may get here even if *bp was a newline; that1942 * just means we are at the exact end of the1943 * previous line, rather than some spot in the1944 * middle.1945 *1946 * Save away what we have to be combined with1947 * the data from the next read.1948 */1949 strbuf_splice(&sb, 0, 0, buf, endp - buf);1950 break;1951 }1952 }19531954 }1955 if (!ret && sb.len)1956 die("BUG: reverse reflog parser had leftover data");19571958 fclose(logfp);1959 strbuf_release(&sb);1960 return ret;1961}19621963static int files_for_each_reflog_ent(struct ref_store *ref_store,1964 const char *refname,1965 each_reflog_ent_fn fn, void *cb_data)1966{1967 struct files_ref_store *refs =1968 files_downcast(ref_store, REF_STORE_READ,1969 "for_each_reflog_ent");1970 FILE *logfp;1971 struct strbuf sb = STRBUF_INIT;1972 int ret = 0;19731974 files_reflog_path(refs, &sb, refname);1975 logfp = fopen(sb.buf, "r");1976 strbuf_release(&sb);1977 if (!logfp)1978 return -1;19791980 while (!ret && !strbuf_getwholeline(&sb, logfp, '\n'))1981 ret = show_one_reflog_ent(&sb, fn, cb_data);1982 fclose(logfp);1983 strbuf_release(&sb);1984 return ret;1985}19861987struct files_reflog_iterator {1988 struct ref_iterator base;19891990 struct ref_store *ref_store;1991 struct dir_iterator *dir_iterator;1992 struct object_id oid;1993};19941995static int files_reflog_iterator_advance(struct ref_iterator *ref_iterator)1996{1997 struct files_reflog_iterator *iter =1998 (struct files_reflog_iterator *)ref_iterator;1999 struct dir_iterator *diter = iter->dir_iterator;2000 int ok;20012002 while ((ok = dir_iterator_advance(diter)) == ITER_OK) {2003 int flags;20042005 if (!S_ISREG(diter->st.st_mode))2006 continue;2007 if (diter->basename[0] == '.')2008 continue;2009 if (ends_with(diter->basename, ".lock"))2010 continue;20112012 if (refs_read_ref_full(iter->ref_store,2013 diter->relative_path, 0,2014 &iter->oid, &flags)) {2015 error("bad ref for %s", diter->path.buf);2016 continue;2017 }20182019 iter->base.refname = diter->relative_path;2020 iter->base.oid = &iter->oid;2021 iter->base.flags = flags;2022 return ITER_OK;2023 }20242025 iter->dir_iterator = NULL;2026 if (ref_iterator_abort(ref_iterator) == ITER_ERROR)2027 ok = ITER_ERROR;2028 return ok;2029}20302031static int files_reflog_iterator_peel(struct ref_iterator *ref_iterator,2032 struct object_id *peeled)2033{2034 die("BUG: ref_iterator_peel() called for reflog_iterator");2035}20362037static int files_reflog_iterator_abort(struct ref_iterator *ref_iterator)2038{2039 struct files_reflog_iterator *iter =2040 (struct files_reflog_iterator *)ref_iterator;2041 int ok = ITER_DONE;20422043 if (iter->dir_iterator)2044 ok = dir_iterator_abort(iter->dir_iterator);20452046 base_ref_iterator_free(ref_iterator);2047 return ok;2048}20492050static struct ref_iterator_vtable files_reflog_iterator_vtable = {2051 files_reflog_iterator_advance,2052 files_reflog_iterator_peel,2053 files_reflog_iterator_abort2054};20552056static struct ref_iterator *reflog_iterator_begin(struct ref_store *ref_store,2057 const char *gitdir)2058{2059 struct files_reflog_iterator *iter = xcalloc(1, sizeof(*iter));2060 struct ref_iterator *ref_iterator = &iter->base;2061 struct strbuf sb = STRBUF_INIT;20622063 base_ref_iterator_init(ref_iterator, &files_reflog_iterator_vtable, 0);2064 strbuf_addf(&sb, "%s/logs", gitdir);2065 iter->dir_iterator = dir_iterator_begin(sb.buf);2066 iter->ref_store = ref_store;2067 strbuf_release(&sb);20682069 return ref_iterator;2070}20712072static enum iterator_selection reflog_iterator_select(2073 struct ref_iterator *iter_worktree,2074 struct ref_iterator *iter_common,2075 void *cb_data)2076{2077 if (iter_worktree) {2078 /*2079 * We're a bit loose here. We probably should ignore2080 * common refs if they are accidentally added as2081 * per-worktree refs.2082 */2083 return ITER_SELECT_0;2084 } else if (iter_common) {2085 if (ref_type(iter_common->refname) == REF_TYPE_NORMAL)2086 return ITER_SELECT_1;20872088 /*2089 * The main ref store may contain main worktree's2090 * per-worktree refs, which should be ignored2091 */2092 return ITER_SKIP_1;2093 } else2094 return ITER_DONE;2095}20962097static struct ref_iterator *files_reflog_iterator_begin(struct ref_store *ref_store)2098{2099 struct files_ref_store *refs =2100 files_downcast(ref_store, REF_STORE_READ,2101 "reflog_iterator_begin");21022103 if (!strcmp(refs->gitdir, refs->gitcommondir)) {2104 return reflog_iterator_begin(ref_store, refs->gitcommondir);2105 } else {2106 return merge_ref_iterator_begin(2107 0,2108 reflog_iterator_begin(ref_store, refs->gitdir),2109 reflog_iterator_begin(ref_store, refs->gitcommondir),2110 reflog_iterator_select, refs);2111 }2112}21132114/*2115 * If update is a direct update of head_ref (the reference pointed to2116 * by HEAD), then add an extra REF_LOG_ONLY update for HEAD.2117 */2118static int split_head_update(struct ref_update *update,2119 struct ref_transaction *transaction,2120 const char *head_ref,2121 struct string_list *affected_refnames,2122 struct strbuf *err)2123{2124 struct string_list_item *item;2125 struct ref_update *new_update;21262127 if ((update->flags & REF_LOG_ONLY) ||2128 (update->flags & REF_ISPRUNING) ||2129 (update->flags & REF_UPDATE_VIA_HEAD))2130 return 0;21312132 if (strcmp(update->refname, head_ref))2133 return 0;21342135 /*2136 * First make sure that HEAD is not already in the2137 * transaction. This check is O(lg N) in the transaction2138 * size, but it happens at most once per transaction.2139 */2140 if (string_list_has_string(affected_refnames, "HEAD")) {2141 /* An entry already existed */2142 strbuf_addf(err,2143 "multiple updates for 'HEAD' (including one "2144 "via its referent '%s') are not allowed",2145 update->refname);2146 return TRANSACTION_NAME_CONFLICT;2147 }21482149 new_update = ref_transaction_add_update(2150 transaction, "HEAD",2151 update->flags | REF_LOG_ONLY | REF_NODEREF,2152 &update->new_oid, &update->old_oid,2153 update->msg);21542155 /*2156 * Add "HEAD". This insertion is O(N) in the transaction2157 * size, but it happens at most once per transaction.2158 * Add new_update->refname instead of a literal "HEAD".2159 */2160 if (strcmp(new_update->refname, "HEAD"))2161 BUG("%s unexpectedly not 'HEAD'", new_update->refname);2162 item = string_list_insert(affected_refnames, new_update->refname);2163 item->util = new_update;21642165 return 0;2166}21672168/*2169 * update is for a symref that points at referent and doesn't have2170 * REF_NODEREF set. Split it into two updates:2171 * - The original update, but with REF_LOG_ONLY and REF_NODEREF set2172 * - A new, separate update for the referent reference2173 * Note that the new update will itself be subject to splitting when2174 * the iteration gets to it.2175 */2176static int split_symref_update(struct files_ref_store *refs,2177 struct ref_update *update,2178 const char *referent,2179 struct ref_transaction *transaction,2180 struct string_list *affected_refnames,2181 struct strbuf *err)2182{2183 struct string_list_item *item;2184 struct ref_update *new_update;2185 unsigned int new_flags;21862187 /*2188 * First make sure that referent is not already in the2189 * transaction. This check is O(lg N) in the transaction2190 * size, but it happens at most once per symref in a2191 * transaction.2192 */2193 if (string_list_has_string(affected_refnames, referent)) {2194 /* An entry already exists */2195 strbuf_addf(err,2196 "multiple updates for '%s' (including one "2197 "via symref '%s') are not allowed",2198 referent, update->refname);2199 return TRANSACTION_NAME_CONFLICT;2200 }22012202 new_flags = update->flags;2203 if (!strcmp(update->refname, "HEAD")) {2204 /*2205 * Record that the new update came via HEAD, so that2206 * when we process it, split_head_update() doesn't try2207 * to add another reflog update for HEAD. Note that2208 * this bit will be propagated if the new_update2209 * itself needs to be split.2210 */2211 new_flags |= REF_UPDATE_VIA_HEAD;2212 }22132214 new_update = ref_transaction_add_update(2215 transaction, referent, new_flags,2216 &update->new_oid, &update->old_oid,2217 update->msg);22182219 new_update->parent_update = update;22202221 /*2222 * Change the symbolic ref update to log only. Also, it2223 * doesn't need to check its old SHA-1 value, as that will be2224 * done when new_update is processed.2225 */2226 update->flags |= REF_LOG_ONLY | REF_NODEREF;2227 update->flags &= ~REF_HAVE_OLD;22282229 /*2230 * Add the referent. This insertion is O(N) in the transaction2231 * size, but it happens at most once per symref in a2232 * transaction. Make sure to add new_update->refname, which will2233 * be valid as long as affected_refnames is in use, and NOT2234 * referent, which might soon be freed by our caller.2235 */2236 item = string_list_insert(affected_refnames, new_update->refname);2237 if (item->util)2238 BUG("%s unexpectedly found in affected_refnames",2239 new_update->refname);2240 item->util = new_update;22412242 return 0;2243}22442245/*2246 * Return the refname under which update was originally requested.2247 */2248static const char *original_update_refname(struct ref_update *update)2249{2250 while (update->parent_update)2251 update = update->parent_update;22522253 return update->refname;2254}22552256/*2257 * Check whether the REF_HAVE_OLD and old_oid values stored in update2258 * are consistent with oid, which is the reference's current value. If2259 * everything is OK, return 0; otherwise, write an error message to2260 * err and return -1.2261 */2262static int check_old_oid(struct ref_update *update, struct object_id *oid,2263 struct strbuf *err)2264{2265 if (!(update->flags & REF_HAVE_OLD) ||2266 !oidcmp(oid, &update->old_oid))2267 return 0;22682269 if (is_null_oid(&update->old_oid))2270 strbuf_addf(err, "cannot lock ref '%s': "2271 "reference already exists",2272 original_update_refname(update));2273 else if (is_null_oid(oid))2274 strbuf_addf(err, "cannot lock ref '%s': "2275 "reference is missing but expected %s",2276 original_update_refname(update),2277 oid_to_hex(&update->old_oid));2278 else2279 strbuf_addf(err, "cannot lock ref '%s': "2280 "is at %s but expected %s",2281 original_update_refname(update),2282 oid_to_hex(oid),2283 oid_to_hex(&update->old_oid));22842285 return -1;2286}22872288/*2289 * Prepare for carrying out update:2290 * - Lock the reference referred to by update.2291 * - Read the reference under lock.2292 * - Check that its old SHA-1 value (if specified) is correct, and in2293 * any case record it in update->lock->old_oid for later use when2294 * writing the reflog.2295 * - If it is a symref update without REF_NODEREF, split it up into a2296 * REF_LOG_ONLY update of the symref and add a separate update for2297 * the referent to transaction.2298 * - If it is an update of head_ref, add a corresponding REF_LOG_ONLY2299 * update of HEAD.2300 */2301static int lock_ref_for_update(struct files_ref_store *refs,2302 struct ref_update *update,2303 struct ref_transaction *transaction,2304 const char *head_ref,2305 struct string_list *affected_refnames,2306 struct strbuf *err)2307{2308 struct strbuf referent = STRBUF_INIT;2309 int mustexist = (update->flags & REF_HAVE_OLD) &&2310 !is_null_oid(&update->old_oid);2311 int ret = 0;2312 struct ref_lock *lock;23132314 files_assert_main_repository(refs, "lock_ref_for_update");23152316 if ((update->flags & REF_HAVE_NEW) && is_null_oid(&update->new_oid))2317 update->flags |= REF_DELETING;23182319 if (head_ref) {2320 ret = split_head_update(update, transaction, head_ref,2321 affected_refnames, err);2322 if (ret)2323 goto out;2324 }23252326 ret = lock_raw_ref(refs, update->refname, mustexist,2327 affected_refnames, NULL,2328 &lock, &referent,2329 &update->type, err);2330 if (ret) {2331 char *reason;23322333 reason = strbuf_detach(err, NULL);2334 strbuf_addf(err, "cannot lock ref '%s': %s",2335 original_update_refname(update), reason);2336 free(reason);2337 goto out;2338 }23392340 update->backend_data = lock;23412342 if (update->type & REF_ISSYMREF) {2343 if (update->flags & REF_NODEREF) {2344 /*2345 * We won't be reading the referent as part of2346 * the transaction, so we have to read it here2347 * to record and possibly check old_sha1:2348 */2349 if (refs_read_ref_full(&refs->base,2350 referent.buf, 0,2351 &lock->old_oid, NULL)) {2352 if (update->flags & REF_HAVE_OLD) {2353 strbuf_addf(err, "cannot lock ref '%s': "2354 "error reading reference",2355 original_update_refname(update));2356 ret = TRANSACTION_GENERIC_ERROR;2357 goto out;2358 }2359 } else if (check_old_oid(update, &lock->old_oid, err)) {2360 ret = TRANSACTION_GENERIC_ERROR;2361 goto out;2362 }2363 } else {2364 /*2365 * Create a new update for the reference this2366 * symref is pointing at. Also, we will record2367 * and verify old_sha1 for this update as part2368 * of processing the split-off update, so we2369 * don't have to do it here.2370 */2371 ret = split_symref_update(refs, update,2372 referent.buf, transaction,2373 affected_refnames, err);2374 if (ret)2375 goto out;2376 }2377 } else {2378 struct ref_update *parent_update;23792380 if (check_old_oid(update, &lock->old_oid, err)) {2381 ret = TRANSACTION_GENERIC_ERROR;2382 goto out;2383 }23842385 /*2386 * If this update is happening indirectly because of a2387 * symref update, record the old SHA-1 in the parent2388 * update:2389 */2390 for (parent_update = update->parent_update;2391 parent_update;2392 parent_update = parent_update->parent_update) {2393 struct ref_lock *parent_lock = parent_update->backend_data;2394 oidcpy(&parent_lock->old_oid, &lock->old_oid);2395 }2396 }23972398 if ((update->flags & REF_HAVE_NEW) &&2399 !(update->flags & REF_DELETING) &&2400 !(update->flags & REF_LOG_ONLY)) {2401 if (!(update->type & REF_ISSYMREF) &&2402 !oidcmp(&lock->old_oid, &update->new_oid)) {2403 /*2404 * The reference already has the desired2405 * value, so we don't need to write it.2406 */2407 } else if (write_ref_to_lockfile(lock, &update->new_oid,2408 err)) {2409 char *write_err = strbuf_detach(err, NULL);24102411 /*2412 * The lock was freed upon failure of2413 * write_ref_to_lockfile():2414 */2415 update->backend_data = NULL;2416 strbuf_addf(err,2417 "cannot update ref '%s': %s",2418 update->refname, write_err);2419 free(write_err);2420 ret = TRANSACTION_GENERIC_ERROR;2421 goto out;2422 } else {2423 update->flags |= REF_NEEDS_COMMIT;2424 }2425 }2426 if (!(update->flags & REF_NEEDS_COMMIT)) {2427 /*2428 * We didn't call write_ref_to_lockfile(), so2429 * the lockfile is still open. Close it to2430 * free up the file descriptor:2431 */2432 if (close_ref_gently(lock)) {2433 strbuf_addf(err, "couldn't close '%s.lock'",2434 update->refname);2435 ret = TRANSACTION_GENERIC_ERROR;2436 goto out;2437 }2438 }24392440out:2441 strbuf_release(&referent);2442 return ret;2443}24442445struct files_transaction_backend_data {2446 struct ref_transaction *packed_transaction;2447 int packed_refs_locked;2448};24492450/*2451 * Unlock any references in `transaction` that are still locked, and2452 * mark the transaction closed.2453 */2454static void files_transaction_cleanup(struct files_ref_store *refs,2455 struct ref_transaction *transaction)2456{2457 size_t i;2458 struct files_transaction_backend_data *backend_data =2459 transaction->backend_data;2460 struct strbuf err = STRBUF_INIT;24612462 for (i = 0; i < transaction->nr; i++) {2463 struct ref_update *update = transaction->updates[i];2464 struct ref_lock *lock = update->backend_data;24652466 if (lock) {2467 unlock_ref(lock);2468 update->backend_data = NULL;2469 }2470 }24712472 if (backend_data->packed_transaction &&2473 ref_transaction_abort(backend_data->packed_transaction, &err)) {2474 error("error aborting transaction: %s", err.buf);2475 strbuf_release(&err);2476 }24772478 if (backend_data->packed_refs_locked)2479 packed_refs_unlock(refs->packed_ref_store);24802481 free(backend_data);24822483 transaction->state = REF_TRANSACTION_CLOSED;2484}24852486static int files_transaction_prepare(struct ref_store *ref_store,2487 struct ref_transaction *transaction,2488 struct strbuf *err)2489{2490 struct files_ref_store *refs =2491 files_downcast(ref_store, REF_STORE_WRITE,2492 "ref_transaction_prepare");2493 size_t i;2494 int ret = 0;2495 struct string_list affected_refnames = STRING_LIST_INIT_NODUP;2496 char *head_ref = NULL;2497 int head_type;2498 struct files_transaction_backend_data *backend_data;2499 struct ref_transaction *packed_transaction = NULL;25002501 assert(err);25022503 if (!transaction->nr)2504 goto cleanup;25052506 backend_data = xcalloc(1, sizeof(*backend_data));2507 transaction->backend_data = backend_data;25082509 /*2510 * Fail if a refname appears more than once in the2511 * transaction. (If we end up splitting up any updates using2512 * split_symref_update() or split_head_update(), those2513 * functions will check that the new updates don't have the2514 * same refname as any existing ones.)2515 */2516 for (i = 0; i < transaction->nr; i++) {2517 struct ref_update *update = transaction->updates[i];2518 struct string_list_item *item =2519 string_list_append(&affected_refnames, update->refname);25202521 /*2522 * We store a pointer to update in item->util, but at2523 * the moment we never use the value of this field2524 * except to check whether it is non-NULL.2525 */2526 item->util = update;2527 }2528 string_list_sort(&affected_refnames);2529 if (ref_update_reject_duplicates(&affected_refnames, err)) {2530 ret = TRANSACTION_GENERIC_ERROR;2531 goto cleanup;2532 }25332534 /*2535 * Special hack: If a branch is updated directly and HEAD2536 * points to it (may happen on the remote side of a push2537 * for example) then logically the HEAD reflog should be2538 * updated too.2539 *2540 * A generic solution would require reverse symref lookups,2541 * but finding all symrefs pointing to a given branch would be2542 * rather costly for this rare event (the direct update of a2543 * branch) to be worth it. So let's cheat and check with HEAD2544 * only, which should cover 99% of all usage scenarios (even2545 * 100% of the default ones).2546 *2547 * So if HEAD is a symbolic reference, then record the name of2548 * the reference that it points to. If we see an update of2549 * head_ref within the transaction, then split_head_update()2550 * arranges for the reflog of HEAD to be updated, too.2551 */2552 head_ref = refs_resolve_refdup(ref_store, "HEAD",2553 RESOLVE_REF_NO_RECURSE,2554 NULL, &head_type);25552556 if (head_ref && !(head_type & REF_ISSYMREF)) {2557 FREE_AND_NULL(head_ref);2558 }25592560 /*2561 * Acquire all locks, verify old values if provided, check2562 * that new values are valid, and write new values to the2563 * lockfiles, ready to be activated. Only keep one lockfile2564 * open at a time to avoid running out of file descriptors.2565 * Note that lock_ref_for_update() might append more updates2566 * to the transaction.2567 */2568 for (i = 0; i < transaction->nr; i++) {2569 struct ref_update *update = transaction->updates[i];25702571 ret = lock_ref_for_update(refs, update, transaction,2572 head_ref, &affected_refnames, err);2573 if (ret)2574 goto cleanup;25752576 if (update->flags & REF_DELETING &&2577 !(update->flags & REF_LOG_ONLY) &&2578 !(update->flags & REF_ISPRUNING)) {2579 /*2580 * This reference has to be deleted from2581 * packed-refs if it exists there.2582 */2583 if (!packed_transaction) {2584 packed_transaction = ref_store_transaction_begin(2585 refs->packed_ref_store, err);2586 if (!packed_transaction) {2587 ret = TRANSACTION_GENERIC_ERROR;2588 goto cleanup;2589 }25902591 backend_data->packed_transaction =2592 packed_transaction;2593 }25942595 ref_transaction_add_update(2596 packed_transaction, update->refname,2597 REF_HAVE_NEW | REF_NODEREF,2598 &update->new_oid, NULL,2599 NULL);2600 }2601 }26022603 if (packed_transaction) {2604 if (packed_refs_lock(refs->packed_ref_store, 0, err)) {2605 ret = TRANSACTION_GENERIC_ERROR;2606 goto cleanup;2607 }2608 backend_data->packed_refs_locked = 1;2609 ret = ref_transaction_prepare(packed_transaction, err);2610 }26112612cleanup:2613 free(head_ref);2614 string_list_clear(&affected_refnames, 0);26152616 if (ret)2617 files_transaction_cleanup(refs, transaction);2618 else2619 transaction->state = REF_TRANSACTION_PREPARED;26202621 return ret;2622}26232624static int files_transaction_finish(struct ref_store *ref_store,2625 struct ref_transaction *transaction,2626 struct strbuf *err)2627{2628 struct files_ref_store *refs =2629 files_downcast(ref_store, 0, "ref_transaction_finish");2630 size_t i;2631 int ret = 0;2632 struct strbuf sb = STRBUF_INIT;2633 struct files_transaction_backend_data *backend_data;2634 struct ref_transaction *packed_transaction;263526362637 assert(err);26382639 if (!transaction->nr) {2640 transaction->state = REF_TRANSACTION_CLOSED;2641 return 0;2642 }26432644 backend_data = transaction->backend_data;2645 packed_transaction = backend_data->packed_transaction;26462647 /* Perform updates first so live commits remain referenced */2648 for (i = 0; i < transaction->nr; i++) {2649 struct ref_update *update = transaction->updates[i];2650 struct ref_lock *lock = update->backend_data;26512652 if (update->flags & REF_NEEDS_COMMIT ||2653 update->flags & REF_LOG_ONLY) {2654 if (files_log_ref_write(refs,2655 lock->ref_name,2656 &lock->old_oid,2657 &update->new_oid,2658 update->msg, update->flags,2659 err)) {2660 char *old_msg = strbuf_detach(err, NULL);26612662 strbuf_addf(err, "cannot update the ref '%s': %s",2663 lock->ref_name, old_msg);2664 free(old_msg);2665 unlock_ref(lock);2666 update->backend_data = NULL;2667 ret = TRANSACTION_GENERIC_ERROR;2668 goto cleanup;2669 }2670 }2671 if (update->flags & REF_NEEDS_COMMIT) {2672 clear_loose_ref_cache(refs);2673 if (commit_ref(lock)) {2674 strbuf_addf(err, "couldn't set '%s'", lock->ref_name);2675 unlock_ref(lock);2676 update->backend_data = NULL;2677 ret = TRANSACTION_GENERIC_ERROR;2678 goto cleanup;2679 }2680 }2681 }26822683 /*2684 * Now that updates are safely completed, we can perform2685 * deletes. First delete the reflogs of any references that2686 * will be deleted, since (in the unexpected event of an2687 * error) leaving a reference without a reflog is less bad2688 * than leaving a reflog without a reference (the latter is a2689 * mildly invalid repository state):2690 */2691 for (i = 0; i < transaction->nr; i++) {2692 struct ref_update *update = transaction->updates[i];2693 if (update->flags & REF_DELETING &&2694 !(update->flags & REF_LOG_ONLY) &&2695 !(update->flags & REF_ISPRUNING)) {2696 strbuf_reset(&sb);2697 files_reflog_path(refs, &sb, update->refname);2698 if (!unlink_or_warn(sb.buf))2699 try_remove_empty_parents(refs, update->refname,2700 REMOVE_EMPTY_PARENTS_REFLOG);2701 }2702 }27032704 /*2705 * Perform deletes now that updates are safely completed.2706 *2707 * First delete any packed versions of the references, while2708 * retaining the packed-refs lock:2709 */2710 if (packed_transaction) {2711 ret = ref_transaction_commit(packed_transaction, err);2712 ref_transaction_free(packed_transaction);2713 packed_transaction = NULL;2714 backend_data->packed_transaction = NULL;2715 if (ret)2716 goto cleanup;2717 }27182719 /* Now delete the loose versions of the references: */2720 for (i = 0; i < transaction->nr; i++) {2721 struct ref_update *update = transaction->updates[i];2722 struct ref_lock *lock = update->backend_data;27232724 if (update->flags & REF_DELETING &&2725 !(update->flags & REF_LOG_ONLY)) {2726 if (!(update->type & REF_ISPACKED) ||2727 update->type & REF_ISSYMREF) {2728 /* It is a loose reference. */2729 strbuf_reset(&sb);2730 files_ref_path(refs, &sb, lock->ref_name);2731 if (unlink_or_msg(sb.buf, err)) {2732 ret = TRANSACTION_GENERIC_ERROR;2733 goto cleanup;2734 }2735 update->flags |= REF_DELETED_LOOSE;2736 }2737 }2738 }27392740 clear_loose_ref_cache(refs);27412742cleanup:2743 files_transaction_cleanup(refs, transaction);27442745 for (i = 0; i < transaction->nr; i++) {2746 struct ref_update *update = transaction->updates[i];27472748 if (update->flags & REF_DELETED_LOOSE) {2749 /*2750 * The loose reference was deleted. Delete any2751 * empty parent directories. (Note that this2752 * can only work because we have already2753 * removed the lockfile.)2754 */2755 try_remove_empty_parents(refs, update->refname,2756 REMOVE_EMPTY_PARENTS_REF);2757 }2758 }27592760 strbuf_release(&sb);2761 return ret;2762}27632764static int files_transaction_abort(struct ref_store *ref_store,2765 struct ref_transaction *transaction,2766 struct strbuf *err)2767{2768 struct files_ref_store *refs =2769 files_downcast(ref_store, 0, "ref_transaction_abort");27702771 files_transaction_cleanup(refs, transaction);2772 return 0;2773}27742775static int ref_present(const char *refname,2776 const struct object_id *oid, int flags, void *cb_data)2777{2778 struct string_list *affected_refnames = cb_data;27792780 return string_list_has_string(affected_refnames, refname);2781}27822783static int files_initial_transaction_commit(struct ref_store *ref_store,2784 struct ref_transaction *transaction,2785 struct strbuf *err)2786{2787 struct files_ref_store *refs =2788 files_downcast(ref_store, REF_STORE_WRITE,2789 "initial_ref_transaction_commit");2790 size_t i;2791 int ret = 0;2792 struct string_list affected_refnames = STRING_LIST_INIT_NODUP;2793 struct ref_transaction *packed_transaction = NULL;27942795 assert(err);27962797 if (transaction->state != REF_TRANSACTION_OPEN)2798 die("BUG: commit called for transaction that is not open");27992800 /* Fail if a refname appears more than once in the transaction: */2801 for (i = 0; i < transaction->nr; i++)2802 string_list_append(&affected_refnames,2803 transaction->updates[i]->refname);2804 string_list_sort(&affected_refnames);2805 if (ref_update_reject_duplicates(&affected_refnames, err)) {2806 ret = TRANSACTION_GENERIC_ERROR;2807 goto cleanup;2808 }28092810 /*2811 * It's really undefined to call this function in an active2812 * repository or when there are existing references: we are2813 * only locking and changing packed-refs, so (1) any2814 * simultaneous processes might try to change a reference at2815 * the same time we do, and (2) any existing loose versions of2816 * the references that we are setting would have precedence2817 * over our values. But some remote helpers create the remote2818 * "HEAD" and "master" branches before calling this function,2819 * so here we really only check that none of the references2820 * that we are creating already exists.2821 */2822 if (refs_for_each_rawref(&refs->base, ref_present,2823 &affected_refnames))2824 die("BUG: initial ref transaction called with existing refs");28252826 packed_transaction = ref_store_transaction_begin(refs->packed_ref_store, err);2827 if (!packed_transaction) {2828 ret = TRANSACTION_GENERIC_ERROR;2829 goto cleanup;2830 }28312832 for (i = 0; i < transaction->nr; i++) {2833 struct ref_update *update = transaction->updates[i];28342835 if ((update->flags & REF_HAVE_OLD) &&2836 !is_null_oid(&update->old_oid))2837 die("BUG: initial ref transaction with old_sha1 set");2838 if (refs_verify_refname_available(&refs->base, update->refname,2839 &affected_refnames, NULL,2840 err)) {2841 ret = TRANSACTION_NAME_CONFLICT;2842 goto cleanup;2843 }28442845 /*2846 * Add a reference creation for this reference to the2847 * packed-refs transaction:2848 */2849 ref_transaction_add_update(packed_transaction, update->refname,2850 update->flags & ~REF_HAVE_OLD,2851 &update->new_oid, &update->old_oid,2852 NULL);2853 }28542855 if (packed_refs_lock(refs->packed_ref_store, 0, err)) {2856 ret = TRANSACTION_GENERIC_ERROR;2857 goto cleanup;2858 }28592860 if (initial_ref_transaction_commit(packed_transaction, err)) {2861 ret = TRANSACTION_GENERIC_ERROR;2862 goto cleanup;2863 }28642865cleanup:2866 if (packed_transaction)2867 ref_transaction_free(packed_transaction);2868 packed_refs_unlock(refs->packed_ref_store);2869 transaction->state = REF_TRANSACTION_CLOSED;2870 string_list_clear(&affected_refnames, 0);2871 return ret;2872}28732874struct expire_reflog_cb {2875 unsigned int flags;2876 reflog_expiry_should_prune_fn *should_prune_fn;2877 void *policy_cb;2878 FILE *newlog;2879 struct object_id last_kept_oid;2880};28812882static int expire_reflog_ent(struct object_id *ooid, struct object_id *noid,2883 const char *email, timestamp_t timestamp, int tz,2884 const char *message, void *cb_data)2885{2886 struct expire_reflog_cb *cb = cb_data;2887 struct expire_reflog_policy_cb *policy_cb = cb->policy_cb;28882889 if (cb->flags & EXPIRE_REFLOGS_REWRITE)2890 ooid = &cb->last_kept_oid;28912892 if ((*cb->should_prune_fn)(ooid, noid, email, timestamp, tz,2893 message, policy_cb)) {2894 if (!cb->newlog)2895 printf("would prune %s", message);2896 else if (cb->flags & EXPIRE_REFLOGS_VERBOSE)2897 printf("prune %s", message);2898 } else {2899 if (cb->newlog) {2900 fprintf(cb->newlog, "%s %s %s %"PRItime" %+05d\t%s",2901 oid_to_hex(ooid), oid_to_hex(noid),2902 email, timestamp, tz, message);2903 oidcpy(&cb->last_kept_oid, noid);2904 }2905 if (cb->flags & EXPIRE_REFLOGS_VERBOSE)2906 printf("keep %s", message);2907 }2908 return 0;2909}29102911static int files_reflog_expire(struct ref_store *ref_store,2912 const char *refname, const struct object_id *oid,2913 unsigned int flags,2914 reflog_expiry_prepare_fn prepare_fn,2915 reflog_expiry_should_prune_fn should_prune_fn,2916 reflog_expiry_cleanup_fn cleanup_fn,2917 void *policy_cb_data)2918{2919 struct files_ref_store *refs =2920 files_downcast(ref_store, REF_STORE_WRITE, "reflog_expire");2921 static struct lock_file reflog_lock;2922 struct expire_reflog_cb cb;2923 struct ref_lock *lock;2924 struct strbuf log_file_sb = STRBUF_INIT;2925 char *log_file;2926 int status = 0;2927 int type;2928 struct strbuf err = STRBUF_INIT;29292930 memset(&cb, 0, sizeof(cb));2931 cb.flags = flags;2932 cb.policy_cb = policy_cb_data;2933 cb.should_prune_fn = should_prune_fn;29342935 /*2936 * The reflog file is locked by holding the lock on the2937 * reference itself, plus we might need to update the2938 * reference if --updateref was specified:2939 */2940 lock = lock_ref_oid_basic(refs, refname, oid,2941 NULL, NULL, REF_NODEREF,2942 &type, &err);2943 if (!lock) {2944 error("cannot lock ref '%s': %s", refname, err.buf);2945 strbuf_release(&err);2946 return -1;2947 }2948 if (!refs_reflog_exists(ref_store, refname)) {2949 unlock_ref(lock);2950 return 0;2951 }29522953 files_reflog_path(refs, &log_file_sb, refname);2954 log_file = strbuf_detach(&log_file_sb, NULL);2955 if (!(flags & EXPIRE_REFLOGS_DRY_RUN)) {2956 /*2957 * Even though holding $GIT_DIR/logs/$reflog.lock has2958 * no locking implications, we use the lock_file2959 * machinery here anyway because it does a lot of the2960 * work we need, including cleaning up if the program2961 * exits unexpectedly.2962 */2963 if (hold_lock_file_for_update(&reflog_lock, log_file, 0) < 0) {2964 struct strbuf err = STRBUF_INIT;2965 unable_to_lock_message(log_file, errno, &err);2966 error("%s", err.buf);2967 strbuf_release(&err);2968 goto failure;2969 }2970 cb.newlog = fdopen_lock_file(&reflog_lock, "w");2971 if (!cb.newlog) {2972 error("cannot fdopen %s (%s)",2973 get_lock_file_path(&reflog_lock), strerror(errno));2974 goto failure;2975 }2976 }29772978 (*prepare_fn)(refname, oid, cb.policy_cb);2979 refs_for_each_reflog_ent(ref_store, refname, expire_reflog_ent, &cb);2980 (*cleanup_fn)(cb.policy_cb);29812982 if (!(flags & EXPIRE_REFLOGS_DRY_RUN)) {2983 /*2984 * It doesn't make sense to adjust a reference pointed2985 * to by a symbolic ref based on expiring entries in2986 * the symbolic reference's reflog. Nor can we update2987 * a reference if there are no remaining reflog2988 * entries.2989 */2990 int update = (flags & EXPIRE_REFLOGS_UPDATE_REF) &&2991 !(type & REF_ISSYMREF) &&2992 !is_null_oid(&cb.last_kept_oid);29932994 if (close_lock_file_gently(&reflog_lock)) {2995 status |= error("couldn't write %s: %s", log_file,2996 strerror(errno));2997 rollback_lock_file(&reflog_lock);2998 } else if (update &&2999 (write_in_full(get_lock_file_fd(&lock->lk),3000 oid_to_hex(&cb.last_kept_oid), GIT_SHA1_HEXSZ) < 0 ||3001 write_str_in_full(get_lock_file_fd(&lock->lk), "\n") < 0 ||3002 close_ref_gently(lock) < 0)) {3003 status |= error("couldn't write %s",3004 get_lock_file_path(&lock->lk));3005 rollback_lock_file(&reflog_lock);3006 } else if (commit_lock_file(&reflog_lock)) {3007 status |= error("unable to write reflog '%s' (%s)",3008 log_file, strerror(errno));3009 } else if (update && commit_ref(lock)) {3010 status |= error("couldn't set %s", lock->ref_name);3011 }3012 }3013 free(log_file);3014 unlock_ref(lock);3015 return status;30163017 failure:3018 rollback_lock_file(&reflog_lock);3019 free(log_file);3020 unlock_ref(lock);3021 return -1;3022}30233024static int files_init_db(struct ref_store *ref_store, struct strbuf *err)3025{3026 struct files_ref_store *refs =3027 files_downcast(ref_store, REF_STORE_WRITE, "init_db");3028 struct strbuf sb = STRBUF_INIT;30293030 /*3031 * Create .git/refs/{heads,tags}3032 */3033 files_ref_path(refs, &sb, "refs/heads");3034 safe_create_dir(sb.buf, 1);30353036 strbuf_reset(&sb);3037 files_ref_path(refs, &sb, "refs/tags");3038 safe_create_dir(sb.buf, 1);30393040 strbuf_release(&sb);3041 return 0;3042}30433044struct ref_storage_be refs_be_files = {3045 NULL,3046 "files",3047 files_ref_store_create,3048 files_init_db,3049 files_transaction_prepare,3050 files_transaction_finish,3051 files_transaction_abort,3052 files_initial_transaction_commit,30533054 files_pack_refs,3055 files_create_symref,3056 files_delete_refs,3057 files_rename_ref,3058 files_copy_ref,30593060 files_ref_iterator_begin,3061 files_read_raw_ref,30623063 files_reflog_iterator_begin,3064 files_for_each_reflog_ent,3065 files_for_each_reflog_ent_reverse,3066 files_reflog_exists,3067 files_create_reflog,3068 files_delete_reflog,3069 files_reflog_expire3070};