1 #define USE_THE_REPOSITORY_VARIABLE
2 #define DISABLE_SIGN_COMPARE_WARNINGS
4 #include "../git-compat-util.h"
5 #include "../abspath.h"
8 #include "../environment.h"
9 #include "../gettext.h"
14 #include "../repo-settings.h"
15 #include "refs-internal.h"
16 #include "ref-cache.h"
17 #include "packed-backend.h"
19 #include "../iterator.h"
20 #include "../dir-iterator.h"
21 #include "../lockfile.h"
22 #include "../object.h"
25 #include "../chdir-notify.h"
27 #include "../worktree.h"
28 #include "../wrapper.h"
29 #include "../write-or-die.h"
30 #include "../revision.h"
31 #include <wildmatch.h>
34 * This backend uses the following flags in `ref_update::flags` for
35 * internal bookkeeping purposes. Their numerical values must not
36 * conflict with REF_NO_DEREF, REF_FORCE_CREATE_REFLOG, REF_HAVE_NEW,
37 * or REF_HAVE_OLD, which are also stored in `ref_update::flags`.
41 * Used as a flag in ref_update::flags when a loose ref is being
42 * pruned. This flag must only be used when REF_NO_DEREF is set.
44 #define REF_IS_PRUNING (1 << 4)
47 * Flag passed to lock_ref_sha1_basic() telling it to tolerate broken
48 * refs (i.e., because the reference is about to be deleted anyway).
50 #define REF_DELETING (1 << 5)
53 * Used as a flag in ref_update::flags when the lockfile needs to be
56 #define REF_NEEDS_COMMIT (1 << 6)
59 * Used as a flag in ref_update::flags when the ref_update was via an
62 #define REF_UPDATE_VIA_HEAD (1 << 8)
65 * Used as a flag in ref_update::flags when a reference has been
66 * deleted and the ref's parent directories may need cleanup.
68 #define REF_DELETED_RMDIR (1 << 9)
71 * Used to indicate that the reflog-only update has been created via
72 * `split_head_update()`.
74 #define REF_LOG_VIA_SPLIT (1 << 14)
79 struct object_id old_oid
;
80 unsigned int count
; /* track users of the lock (ref update + reflog updates) */
83 struct files_ref_store
{
84 struct ref_store base
;
85 unsigned int store_flags
;
88 enum log_refs_config log_all_ref_updates
;
89 int prefer_symlink_refs
;
91 struct ref_cache
*loose
;
93 struct ref_store
*packed_ref_store
;
96 static void clear_loose_ref_cache(struct files_ref_store
*refs
)
99 free_ref_cache(refs
->loose
);
105 * Create a new submodule ref cache and add it to the internal
108 static struct ref_store
*files_ref_store_init(struct repository
*repo
,
112 struct files_ref_store
*refs
= xcalloc(1, sizeof(*refs
));
113 struct ref_store
*ref_store
= (struct ref_store
*)refs
;
114 struct strbuf sb
= STRBUF_INIT
;
116 base_ref_store_init(ref_store
, repo
, gitdir
, &refs_be_files
);
117 refs
->store_flags
= flags
;
118 get_common_dir_noenv(&sb
, gitdir
);
119 refs
->gitcommondir
= strbuf_detach(&sb
, NULL
);
120 refs
->packed_ref_store
=
121 packed_ref_store_init(repo
, refs
->gitcommondir
, flags
);
122 refs
->log_all_ref_updates
= repo_settings_get_log_all_ref_updates(repo
);
123 repo_config_get_bool(repo
, "core.prefersymlinkrefs", &refs
->prefer_symlink_refs
);
125 chdir_notify_reparent("files-backend $GIT_DIR", &refs
->base
.gitdir
);
126 chdir_notify_reparent("files-backend $GIT_COMMONDIR",
127 &refs
->gitcommondir
);
133 * Die if refs is not the main ref store. caller is used in any
134 * necessary error messages.
136 static void files_assert_main_repository(struct files_ref_store
*refs
,
139 if (refs
->store_flags
& REF_STORE_MAIN
)
142 BUG("operation %s only allowed for main ref store", caller
);
146 * Downcast ref_store to files_ref_store. Die if ref_store is not a
147 * files_ref_store. required_flags is compared with ref_store's
148 * store_flags to ensure the ref_store has all required capabilities.
149 * "caller" is used in any necessary error messages.
151 static struct files_ref_store
*files_downcast(struct ref_store
*ref_store
,
152 unsigned int required_flags
,
155 struct files_ref_store
*refs
;
157 if (ref_store
->be
!= &refs_be_files
)
158 BUG("ref_store is type \"%s\" not \"files\" in %s",
159 ref_store
->be
->name
, caller
);
161 refs
= (struct files_ref_store
*)ref_store
;
163 if ((refs
->store_flags
& required_flags
) != required_flags
)
164 BUG("operation %s requires abilities 0x%x, but only have 0x%x",
165 caller
, required_flags
, refs
->store_flags
);
170 static void files_ref_store_release(struct ref_store
*ref_store
)
172 struct files_ref_store
*refs
= files_downcast(ref_store
, 0, "release");
173 free_ref_cache(refs
->loose
);
174 free(refs
->gitcommondir
);
175 ref_store_release(refs
->packed_ref_store
);
176 free(refs
->packed_ref_store
);
179 static void files_reflog_path(struct files_ref_store
*refs
,
183 const char *bare_refname
;
186 enum ref_worktree_type wt_type
= parse_worktree_ref(
187 refname
, &wtname
, &wtname_len
, &bare_refname
);
190 case REF_WORKTREE_CURRENT
:
191 strbuf_addf(sb
, "%s/logs/%s", refs
->base
.gitdir
, refname
);
193 case REF_WORKTREE_SHARED
:
194 case REF_WORKTREE_MAIN
:
195 strbuf_addf(sb
, "%s/logs/%s", refs
->gitcommondir
, bare_refname
);
197 case REF_WORKTREE_OTHER
:
198 strbuf_addf(sb
, "%s/worktrees/%.*s/logs/%s", refs
->gitcommondir
,
199 wtname_len
, wtname
, bare_refname
);
202 BUG("unknown ref type %d of ref %s", wt_type
, refname
);
206 static void files_ref_path(struct files_ref_store
*refs
,
210 const char *bare_refname
;
213 enum ref_worktree_type wt_type
= parse_worktree_ref(
214 refname
, &wtname
, &wtname_len
, &bare_refname
);
216 case REF_WORKTREE_CURRENT
:
217 strbuf_addf(sb
, "%s/%s", refs
->base
.gitdir
, refname
);
219 case REF_WORKTREE_OTHER
:
220 strbuf_addf(sb
, "%s/worktrees/%.*s/%s", refs
->gitcommondir
,
221 wtname_len
, wtname
, bare_refname
);
223 case REF_WORKTREE_SHARED
:
224 case REF_WORKTREE_MAIN
:
225 strbuf_addf(sb
, "%s/%s", refs
->gitcommondir
, bare_refname
);
228 BUG("unknown ref type %d of ref %s", wt_type
, refname
);
233 * Manually add refs/bisect, refs/rewritten and refs/worktree, which, being
234 * per-worktree, might not appear in the directory listing for
235 * refs/ in the main repo.
237 static void add_per_worktree_entries_to_dir(struct ref_dir
*dir
, const char *dirname
)
239 const char *prefixes
[] = { "refs/bisect/", "refs/worktree/", "refs/rewritten/" };
242 if (strcmp(dirname
, "refs/"))
245 for (ip
= 0; ip
< ARRAY_SIZE(prefixes
); ip
++) {
246 const char *prefix
= prefixes
[ip
];
247 int prefix_len
= strlen(prefix
);
248 struct ref_entry
*child_entry
;
251 pos
= search_ref_dir(dir
, prefix
, prefix_len
);
254 child_entry
= create_dir_entry(dir
->cache
, prefix
, prefix_len
);
255 add_entry_to_dir(dir
, child_entry
);
259 static void loose_fill_ref_dir_regular_file(struct files_ref_store
*refs
,
263 struct object_id oid
;
265 const char *referent
= refs_resolve_ref_unsafe(&refs
->base
,
271 oidclr(&oid
, refs
->base
.repo
->hash_algo
);
272 flag
|= REF_ISBROKEN
;
273 } else if (is_null_oid(&oid
)) {
275 * It is so astronomically unlikely
276 * that null_oid is the OID of an
277 * actual object that we consider its
278 * appearance in a loose reference
279 * file to be repo corruption
280 * (probably due to a software bug).
282 flag
|= REF_ISBROKEN
;
285 if (check_refname_format(refname
, REFNAME_ALLOW_ONELEVEL
)) {
286 if (!refname_is_safe(refname
))
287 die("loose refname is dangerous: %s", refname
);
288 oidclr(&oid
, refs
->base
.repo
->hash_algo
);
289 flag
|= REF_BAD_NAME
| REF_ISBROKEN
;
292 if (!(flag
& REF_ISSYMREF
))
295 add_entry_to_dir(dir
, create_ref_entry(refname
, referent
, &oid
, flag
));
299 * Read the loose references from the namespace dirname into dir
300 * (without recursing). dirname must end with '/'. dir must be the
301 * directory entry corresponding to dirname.
303 static void loose_fill_ref_dir(struct ref_store
*ref_store
,
304 struct ref_dir
*dir
, const char *dirname
)
306 struct files_ref_store
*refs
=
307 files_downcast(ref_store
, REF_STORE_READ
, "fill_ref_dir");
310 int dirnamelen
= strlen(dirname
);
311 struct strbuf refname
;
312 struct strbuf path
= STRBUF_INIT
;
314 files_ref_path(refs
, &path
, dirname
);
316 d
= opendir(path
.buf
);
318 strbuf_release(&path
);
322 strbuf_init(&refname
, dirnamelen
+ 257);
323 strbuf_add(&refname
, dirname
, dirnamelen
);
325 while ((de
= readdir(d
)) != NULL
) {
328 if (de
->d_name
[0] == '.')
330 if (ends_with(de
->d_name
, ".lock"))
332 strbuf_addstr(&refname
, de
->d_name
);
334 dtype
= get_dtype(de
, &path
, 1);
335 if (dtype
== DT_DIR
) {
336 strbuf_addch(&refname
, '/');
337 add_entry_to_dir(dir
,
338 create_dir_entry(dir
->cache
, refname
.buf
,
340 } else if (dtype
== DT_REG
) {
341 loose_fill_ref_dir_regular_file(refs
, refname
.buf
, dir
);
343 strbuf_setlen(&refname
, dirnamelen
);
345 strbuf_release(&refname
);
346 strbuf_release(&path
);
349 add_per_worktree_entries_to_dir(dir
, dirname
);
352 static int for_each_root_ref(struct files_ref_store
*refs
,
353 int (*cb
)(const char *refname
, void *cb_data
),
356 struct strbuf path
= STRBUF_INIT
, refname
= STRBUF_INIT
;
357 const char *dirname
= refs
->loose
->root
->name
;
363 files_ref_path(refs
, &path
, dirname
);
365 d
= opendir(path
.buf
);
367 strbuf_release(&path
);
371 strbuf_addstr(&refname
, dirname
);
372 dirnamelen
= refname
.len
;
374 while ((de
= readdir(d
)) != NULL
) {
377 if (de
->d_name
[0] == '.')
379 if (ends_with(de
->d_name
, ".lock"))
381 strbuf_addstr(&refname
, de
->d_name
);
383 dtype
= get_dtype(de
, &path
, 1);
384 if (dtype
== DT_REG
&& is_root_ref(de
->d_name
)) {
385 ret
= cb(refname
.buf
, cb_data
);
390 strbuf_setlen(&refname
, dirnamelen
);
396 strbuf_release(&refname
);
397 strbuf_release(&path
);
402 struct fill_root_ref_data
{
403 struct files_ref_store
*refs
;
407 static int fill_root_ref(const char *refname
, void *cb_data
)
409 struct fill_root_ref_data
*data
= cb_data
;
410 loose_fill_ref_dir_regular_file(data
->refs
, refname
, data
->dir
);
415 * Add root refs to the ref dir by parsing the directory for any files which
416 * follow the root ref syntax.
418 static void add_root_refs(struct files_ref_store
*refs
,
421 struct fill_root_ref_data data
= {
426 for_each_root_ref(refs
, fill_root_ref
, &data
);
429 static struct ref_cache
*get_loose_ref_cache(struct files_ref_store
*refs
,
436 * Mark the top-level directory complete because we
437 * are about to read the only subdirectory that can
440 refs
->loose
= create_ref_cache(&refs
->base
, loose_fill_ref_dir
);
442 /* We're going to fill the top level ourselves: */
443 refs
->loose
->root
->flag
&= ~REF_INCOMPLETE
;
445 dir
= get_ref_dir(refs
->loose
->root
);
447 if (flags
& DO_FOR_EACH_INCLUDE_ROOT_REFS
)
448 add_root_refs(refs
, dir
);
451 * Add an incomplete entry for "refs/" (to be filled
454 add_entry_to_dir(dir
, create_dir_entry(refs
->loose
, "refs/", 5));
459 static int read_ref_internal(struct ref_store
*ref_store
, const char *refname
,
460 struct object_id
*oid
, struct strbuf
*referent
,
461 unsigned int *type
, int *failure_errno
, int skip_packed_refs
)
463 struct files_ref_store
*refs
=
464 files_downcast(ref_store
, REF_STORE_READ
, "read_raw_ref");
465 struct strbuf sb_contents
= STRBUF_INIT
;
466 struct strbuf sb_path
= STRBUF_INIT
;
472 int remaining_retries
= 3;
476 strbuf_reset(&sb_path
);
478 files_ref_path(refs
, &sb_path
, refname
);
484 * We might have to loop back here to avoid a race
485 * condition: first we lstat() the file, then we try
486 * to read it as a link or as a file. But if somebody
487 * changes the type of the file (file <-> directory
488 * <-> symlink) between the lstat() and reading, then
489 * we don't want to report that as an error but rather
490 * try again starting with the lstat().
492 * We'll keep a count of the retries, though, just to avoid
493 * any confusing situation sending us into an infinite loop.
496 if (remaining_retries
-- <= 0)
499 if (lstat(path
, &st
) < 0) {
502 if (myerr
!= ENOENT
|| skip_packed_refs
)
504 if (refs_read_raw_ref(refs
->packed_ref_store
, refname
, oid
,
505 referent
, type
, &ignore_errno
)) {
513 /* Follow "normalized" - ie "refs/.." symlinks by hand */
514 if (S_ISLNK(st
.st_mode
)) {
515 strbuf_reset(&sb_contents
);
516 if (strbuf_readlink(&sb_contents
, path
, st
.st_size
) < 0) {
518 if (myerr
== ENOENT
|| myerr
== EINVAL
)
519 /* inconsistent with lstat; retry */
524 if (starts_with(sb_contents
.buf
, "refs/") &&
525 !check_refname_format(sb_contents
.buf
, 0)) {
526 strbuf_swap(&sb_contents
, referent
);
527 *type
|= REF_ISSYMREF
;
532 * It doesn't look like a refname; fall through to just
533 * treating it like a non-symlink, and reading whatever it
538 /* Is it a directory? */
539 if (S_ISDIR(st
.st_mode
)) {
542 * Even though there is a directory where the loose
543 * ref is supposed to be, there could still be a
546 if (skip_packed_refs
||
547 refs_read_raw_ref(refs
->packed_ref_store
, refname
, oid
,
548 referent
, type
, &ignore_errno
)) {
557 * Anything else, just open it and try to use it as
560 fd
= open(path
, O_RDONLY
);
563 if (myerr
== ENOENT
&& !S_ISLNK(st
.st_mode
))
564 /* inconsistent with lstat; retry */
569 strbuf_reset(&sb_contents
);
570 if (strbuf_read(&sb_contents
, fd
, 256) < 0) {
576 strbuf_rtrim(&sb_contents
);
577 buf
= sb_contents
.buf
;
579 ret
= parse_loose_ref_contents(ref_store
->repo
->hash_algo
, buf
,
580 oid
, referent
, type
, NULL
, &myerr
);
584 BUG("returning non-zero %d, should have set myerr!", ret
);
585 *failure_errno
= myerr
;
587 strbuf_release(&sb_path
);
588 strbuf_release(&sb_contents
);
593 static int files_read_raw_ref(struct ref_store
*ref_store
, const char *refname
,
594 struct object_id
*oid
, struct strbuf
*referent
,
595 unsigned int *type
, int *failure_errno
)
597 return read_ref_internal(ref_store
, refname
, oid
, referent
, type
, failure_errno
, 0);
600 static int files_read_symbolic_ref(struct ref_store
*ref_store
, const char *refname
,
601 struct strbuf
*referent
)
603 struct object_id oid
;
604 int failure_errno
, ret
;
607 ret
= read_ref_internal(ref_store
, refname
, &oid
, referent
, &type
, &failure_errno
, 1);
608 if (!ret
&& !(type
& REF_ISSYMREF
))
613 int parse_loose_ref_contents(const struct git_hash_algo
*algop
,
614 const char *buf
, struct object_id
*oid
,
615 struct strbuf
*referent
, unsigned int *type
,
616 const char **trailing
, int *failure_errno
)
619 if (skip_prefix(buf
, "ref:", &buf
)) {
620 while (isspace(*buf
))
623 strbuf_reset(referent
);
624 strbuf_addstr(referent
, buf
);
625 *type
|= REF_ISSYMREF
;
630 * FETCH_HEAD has additional data after the sha.
632 if (parse_oid_hex_algop(buf
, oid
, &p
, algop
) ||
633 (*p
!= '\0' && !isspace(*p
))) {
634 *type
|= REF_ISBROKEN
;
635 *failure_errno
= EINVAL
;
645 static void unlock_ref(struct ref_lock
*lock
)
649 rollback_lock_file(&lock
->lk
);
650 free(lock
->ref_name
);
656 * Check if the transaction has another update with a case-insensitive refname
659 * If the update is part of the transaction, we only check up to that index.
660 * Further updates are expected to call this function to match previous indices.
662 static bool transaction_has_case_conflicting_update(struct ref_transaction
*transaction
,
663 struct ref_update
*update
)
665 for (size_t i
= 0; i
< transaction
->nr
; i
++) {
666 if (transaction
->updates
[i
] == update
)
669 if (!strcasecmp(transaction
->updates
[i
]->refname
, update
->refname
))
676 * Lock refname, without following symrefs, and set *lock_p to point
677 * at a newly-allocated lock object. Fill in lock->old_oid, referent,
678 * and type similarly to read_raw_ref().
680 * The caller must verify that refname is a "safe" reference name (in
681 * the sense of refname_is_safe()) before calling this function.
683 * If the reference doesn't already exist, verify that refname doesn't
684 * have a D/F conflict with any existing references. extras and skip
685 * are passed to refs_verify_refname_available() for this check.
687 * If mustexist is not set and the reference is not found or is
688 * broken, lock the reference anyway but clear old_oid.
690 * Return 0 on success. On failure, write an error message to err and
691 * return REF_TRANSACTION_ERROR_NAME_CONFLICT or REF_TRANSACTION_ERROR_GENERIC.
693 * Implementation note: This function is basically
698 * but it includes a lot more code to
699 * - Deal with possible races with other processes
700 * - Avoid calling refs_verify_refname_available() when it can be
701 * avoided, namely if we were successfully able to read the ref
702 * - Generate informative error messages in the case of failure
704 static enum ref_transaction_error
lock_raw_ref(struct files_ref_store
*refs
,
705 struct ref_transaction
*transaction
,
708 struct string_list
*refnames_to_check
,
709 struct ref_lock
**lock_p
,
710 struct strbuf
*referent
,
713 enum ref_transaction_error ret
= REF_TRANSACTION_ERROR_GENERIC
;
714 struct ref_update
*update
= transaction
->updates
[update_idx
];
715 const struct string_list
*extras
= &transaction
->refnames
;
716 const char *refname
= update
->refname
;
717 unsigned int *type
= &update
->type
;
718 struct ref_lock
*lock
;
719 struct strbuf ref_file
= STRBUF_INIT
;
720 int attempts_remaining
= 3;
724 files_assert_main_repository(refs
, "lock_raw_ref");
728 /* First lock the file so it can't change out from under us. */
730 *lock_p
= CALLOC_ARRAY(lock
, 1);
732 lock
->ref_name
= xstrdup(refname
);
734 files_ref_path(refs
, &ref_file
, refname
);
737 switch (safe_create_leading_directories(the_repository
, ref_file
.buf
)) {
742 * Suppose refname is "refs/foo/bar". We just failed
743 * to create the containing directory, "refs/foo",
744 * because there was a non-directory in the way. This
745 * indicates a D/F conflict, probably because of
746 * another reference such as "refs/foo". There is no
747 * reason to expect this error to be transitory.
749 if (refs_verify_refname_available(&refs
->base
, refname
,
750 extras
, NULL
, 0, err
)) {
753 * To the user the relevant error is
754 * that the "mustexist" reference is
758 strbuf_addf(err
, "unable to resolve reference '%s'",
760 ret
= REF_TRANSACTION_ERROR_NONEXISTENT_REF
;
763 * The error message set by
764 * refs_verify_refname_available() is
767 ret
= REF_TRANSACTION_ERROR_NAME_CONFLICT
;
771 * The file that is in the way isn't a loose
772 * reference. Report it as a low-level
775 strbuf_addf(err
, "unable to create lock file %s.lock; "
776 "non-directory in the way",
781 /* Maybe another process was tidying up. Try again. */
782 if (--attempts_remaining
> 0)
786 strbuf_addf(err
, "unable to create directory for %s",
791 if (hold_lock_file_for_update_timeout(
792 &lock
->lk
, ref_file
.buf
, LOCK_NO_DEREF
,
793 get_files_ref_lock_timeout_ms()) < 0) {
796 if (myerr
== ENOENT
&& --attempts_remaining
> 0) {
798 * Maybe somebody just deleted one of the
799 * directories leading to ref_file. Try
804 unable_to_lock_message(ref_file
.buf
, myerr
, err
);
805 if (myerr
== EEXIST
) {
807 transaction_has_case_conflicting_update(transaction
, update
)) {
809 * In case-insensitive filesystems, ensure that conflicts within a
810 * given transaction are handled. Pre-existing refs on a
811 * case-insensitive system will be overridden without any issue.
813 ret
= REF_TRANSACTION_ERROR_CASE_CONFLICT
;
816 * Pre-existing case-conflicting reference locks should also be
817 * specially categorized to avoid failing all batched updates.
819 ret
= REF_TRANSACTION_ERROR_CREATE_EXISTS
;
828 * Now we hold the lock and can read the reference without
829 * fear that its value will change.
832 if (files_read_raw_ref(&refs
->base
, refname
, &lock
->old_oid
, referent
,
833 type
, &failure_errno
)) {
834 struct string_list_item
*item
;
836 if (failure_errno
== ENOENT
) {
838 /* Garden variety missing reference. */
839 strbuf_addf(err
, "unable to resolve reference '%s'",
841 ret
= REF_TRANSACTION_ERROR_NONEXISTENT_REF
;
845 * Reference is missing, but that's OK. We
846 * know that there is not a conflict with
847 * another loose reference because
848 * (supposing that we are trying to lock
849 * reference "refs/foo/bar"):
851 * - We were successfully able to create
852 * the lockfile refs/foo/bar.lock, so we
853 * know there cannot be a loose reference
856 * - We got ENOENT and not EISDIR, so we
857 * know that there cannot be a loose
858 * reference named "refs/foo/bar/baz".
861 } else if (failure_errno
== EISDIR
) {
863 * There is a directory in the way. It might have
864 * contained references that have been deleted. If
865 * we don't require that the reference already
866 * exists, try to remove the directory so that it
867 * doesn't cause trouble when we want to rename the
868 * lockfile into place later.
871 /* Garden variety missing reference. */
872 strbuf_addf(err
, "unable to resolve reference '%s'",
874 ret
= REF_TRANSACTION_ERROR_NONEXISTENT_REF
;
876 } else if (remove_dir_recursively(&ref_file
,
877 REMOVE_DIR_EMPTY_ONLY
)) {
878 ret
= REF_TRANSACTION_ERROR_NAME_CONFLICT
;
879 if (refs_verify_refname_available(
880 &refs
->base
, refname
,
881 extras
, NULL
, 0, err
)) {
883 * The error message set by
884 * verify_refname_available() is OK.
889 * Directory conflicts can occur if there
890 * is an existing lock file in the directory
891 * or if the filesystem is case-insensitive
892 * and the directory contains a valid reference
893 * but conflicts with the update.
895 strbuf_addf(err
, "there is a non-empty directory '%s' "
896 "blocking reference '%s'",
897 ref_file
.buf
, refname
);
901 } else if (failure_errno
== EINVAL
&& (*type
& REF_ISBROKEN
)) {
902 strbuf_addf(err
, "unable to resolve reference '%s': "
903 "reference broken", refname
);
906 strbuf_addf(err
, "unable to resolve reference '%s': %s",
907 refname
, strerror(failure_errno
));
912 * If the ref did not exist and we are creating it, we have to
913 * make sure there is no existing packed ref that conflicts
914 * with refname. This check is deferred so that we can batch it.
916 * For case-insensitive filesystems, we should also check for F/D
917 * conflicts between 'foo' and 'Foo/bar'. So let's lowercase
921 struct strbuf lower
= STRBUF_INIT
;
923 strbuf_addstr(&lower
, refname
);
924 strbuf_tolower(&lower
);
926 item
= string_list_append_nodup(refnames_to_check
,
927 strbuf_detach(&lower
, NULL
));
929 item
= string_list_append(refnames_to_check
, refname
);
932 item
->util
= xmalloc(sizeof(update_idx
));
933 memcpy(item
->util
, &update_idx
, sizeof(update_idx
));
944 strbuf_release(&ref_file
);
948 struct files_ref_iterator
{
949 struct ref_iterator base
;
951 struct ref_iterator
*iter0
;
952 struct repository
*repo
;
956 static int files_ref_iterator_advance(struct ref_iterator
*ref_iterator
)
958 struct files_ref_iterator
*iter
=
959 (struct files_ref_iterator
*)ref_iterator
;
962 while ((ok
= ref_iterator_advance(iter
->iter0
)) == ITER_OK
) {
963 if (iter
->flags
& DO_FOR_EACH_PER_WORKTREE_ONLY
&&
964 parse_worktree_ref(iter
->iter0
->refname
, NULL
, NULL
,
965 NULL
) != REF_WORKTREE_CURRENT
)
968 if ((iter
->flags
& DO_FOR_EACH_OMIT_DANGLING_SYMREFS
) &&
969 (iter
->iter0
->flags
& REF_ISSYMREF
) &&
970 (iter
->iter0
->flags
& REF_ISBROKEN
))
973 if (!(iter
->flags
& DO_FOR_EACH_INCLUDE_BROKEN
) &&
974 !ref_resolves_to_object(iter
->iter0
->refname
,
980 iter
->base
.refname
= iter
->iter0
->refname
;
981 iter
->base
.oid
= iter
->iter0
->oid
;
982 iter
->base
.flags
= iter
->iter0
->flags
;
983 iter
->base
.referent
= iter
->iter0
->referent
;
991 static int files_ref_iterator_seek(struct ref_iterator
*ref_iterator
,
992 const char *refname
, unsigned int flags
)
994 struct files_ref_iterator
*iter
=
995 (struct files_ref_iterator
*)ref_iterator
;
996 return ref_iterator_seek(iter
->iter0
, refname
, flags
);
999 static int files_ref_iterator_peel(struct ref_iterator
*ref_iterator
,
1000 struct object_id
*peeled
)
1002 struct files_ref_iterator
*iter
=
1003 (struct files_ref_iterator
*)ref_iterator
;
1005 return ref_iterator_peel(iter
->iter0
, peeled
);
1008 static void files_ref_iterator_release(struct ref_iterator
*ref_iterator
)
1010 struct files_ref_iterator
*iter
=
1011 (struct files_ref_iterator
*)ref_iterator
;
1012 ref_iterator_free(iter
->iter0
);
1015 static struct ref_iterator_vtable files_ref_iterator_vtable
= {
1016 .advance
= files_ref_iterator_advance
,
1017 .seek
= files_ref_iterator_seek
,
1018 .peel
= files_ref_iterator_peel
,
1019 .release
= files_ref_iterator_release
,
1022 static struct ref_iterator
*files_ref_iterator_begin(
1023 struct ref_store
*ref_store
,
1024 const char *prefix
, const char **exclude_patterns
,
1027 struct files_ref_store
*refs
;
1028 struct ref_iterator
*loose_iter
, *packed_iter
, *overlay_iter
;
1029 struct files_ref_iterator
*iter
;
1030 struct ref_iterator
*ref_iterator
;
1031 unsigned int required_flags
= REF_STORE_READ
;
1033 if (!(flags
& DO_FOR_EACH_INCLUDE_BROKEN
))
1034 required_flags
|= REF_STORE_ODB
;
1036 refs
= files_downcast(ref_store
, required_flags
, "ref_iterator_begin");
1039 * We must make sure that all loose refs are read before
1040 * accessing the packed-refs file; this avoids a race
1041 * condition if loose refs are migrated to the packed-refs
1042 * file by a simultaneous process, but our in-memory view is
1043 * from before the migration. We ensure this as follows:
1044 * First, we call start the loose refs iteration with its
1045 * `prime_ref` argument set to true. This causes the loose
1046 * references in the subtree to be pre-read into the cache.
1047 * (If they've already been read, that's OK; we only need to
1048 * guarantee that they're read before the packed refs, not
1049 * *how much* before.) After that, we call
1050 * packed_ref_iterator_begin(), which internally checks
1051 * whether the packed-ref cache is up to date with what is on
1052 * disk, and re-reads it if not.
1055 loose_iter
= cache_ref_iterator_begin(get_loose_ref_cache(refs
, flags
),
1056 prefix
, ref_store
->repo
, 1);
1059 * The packed-refs file might contain broken references, for
1060 * example an old version of a reference that points at an
1061 * object that has since been garbage-collected. This is OK as
1062 * long as there is a corresponding loose reference that
1063 * overrides it, and we don't want to emit an error message in
1064 * this case. So ask the packed_ref_store for all of its
1065 * references, and (if needed) do our own check for broken
1066 * ones in files_ref_iterator_advance(), after we have merged
1067 * the packed and loose references.
1069 packed_iter
= refs_ref_iterator_begin(
1070 refs
->packed_ref_store
, prefix
, exclude_patterns
, 0,
1071 DO_FOR_EACH_INCLUDE_BROKEN
);
1073 overlay_iter
= overlay_ref_iterator_begin(loose_iter
, packed_iter
);
1075 CALLOC_ARRAY(iter
, 1);
1076 ref_iterator
= &iter
->base
;
1077 base_ref_iterator_init(ref_iterator
, &files_ref_iterator_vtable
);
1078 iter
->iter0
= overlay_iter
;
1079 iter
->repo
= ref_store
->repo
;
1080 iter
->flags
= flags
;
1082 return ref_iterator
;
1086 * Callback function for raceproof_create_file(). This function is
1087 * expected to do something that makes dirname(path) permanent despite
1088 * the fact that other processes might be cleaning up empty
1089 * directories at the same time. Usually it will create a file named
1090 * path, but alternatively it could create another file in that
1091 * directory, or even chdir() into that directory. The function should
1092 * return 0 if the action was completed successfully. On error, it
1093 * should return a nonzero result and set errno.
1094 * raceproof_create_file() treats two errno values specially:
1096 * - ENOENT -- dirname(path) does not exist. In this case,
1097 * raceproof_create_file() tries creating dirname(path)
1098 * (and any parent directories, if necessary) and calls
1099 * the function again.
1101 * - EISDIR -- the file already exists and is a directory. In this
1102 * case, raceproof_create_file() removes the directory if
1103 * it is empty (and recursively any empty directories that
1104 * it contains) and calls the function again.
1106 * Any other errno causes raceproof_create_file() to fail with the
1107 * callback's return value and errno.
1109 * Obviously, this function should be OK with being called again if it
1110 * fails with ENOENT or EISDIR. In other scenarios it will not be
1113 typedef int create_file_fn(const char *path
, void *cb
);
1116 * Create a file in dirname(path) by calling fn, creating leading
1117 * directories if necessary. Retry a few times in case we are racing
1118 * with another process that is trying to clean up the directory that
1119 * contains path. See the documentation for create_file_fn for more
1122 * Return the value and set the errno that resulted from the most
1123 * recent call of fn. fn is always called at least once, and will be
1124 * called more than once if it returns ENOENT or EISDIR.
1126 static int raceproof_create_file(const char *path
, create_file_fn fn
, void *cb
)
1129 * The number of times we will try to remove empty directories
1130 * in the way of path. This is only 1 because if another
1131 * process is racily creating directories that conflict with
1132 * us, we don't want to fight against them.
1134 int remove_directories_remaining
= 1;
1137 * The number of times that we will try to create the
1138 * directories containing path. We are willing to attempt this
1139 * more than once, because another process could be trying to
1140 * clean up empty directories at the same time as we are
1141 * trying to create them.
1143 int create_directories_remaining
= 3;
1145 /* A scratch copy of path, filled lazily if we need it: */
1146 struct strbuf path_copy
= STRBUF_INIT
;
1148 int ret
, save_errno
;
1159 if (errno
== EISDIR
&& remove_directories_remaining
-- > 0) {
1161 * A directory is in the way. Maybe it is empty; try
1165 strbuf_addstr(&path_copy
, path
);
1167 if (!remove_dir_recursively(&path_copy
, REMOVE_DIR_EMPTY_ONLY
))
1169 } else if (errno
== ENOENT
&& create_directories_remaining
-- > 0) {
1171 * Maybe the containing directory didn't exist, or
1172 * maybe it was just deleted by a process that is
1173 * racing with us to clean up empty directories. Try
1176 enum scld_error scld_result
;
1179 strbuf_addstr(&path_copy
, path
);
1182 scld_result
= safe_create_leading_directories(the_repository
, path_copy
.buf
);
1183 if (scld_result
== SCLD_OK
)
1185 } while (scld_result
== SCLD_VANISHED
&& create_directories_remaining
-- > 0);
1189 strbuf_release(&path_copy
);
1194 static int remove_empty_directories(struct strbuf
*path
)
1197 * we want to create a file but there is a directory there;
1198 * if that is an empty directory (or a directory that contains
1199 * only empty directories), remove them.
1201 return remove_dir_recursively(path
, REMOVE_DIR_EMPTY_ONLY
);
1204 static int create_reflock(const char *path
, void *cb
)
1206 struct lock_file
*lk
= cb
;
1208 return hold_lock_file_for_update_timeout(
1209 lk
, path
, LOCK_NO_DEREF
,
1210 get_files_ref_lock_timeout_ms()) < 0 ? -1 : 0;
1214 * Locks a ref returning the lock on success and NULL on failure.
1216 static struct ref_lock
*lock_ref_oid_basic(struct files_ref_store
*refs
,
1217 const char *refname
,
1220 struct strbuf ref_file
= STRBUF_INIT
;
1221 struct ref_lock
*lock
;
1223 files_assert_main_repository(refs
, "lock_ref_oid_basic");
1226 CALLOC_ARRAY(lock
, 1);
1228 files_ref_path(refs
, &ref_file
, refname
);
1231 * If the ref did not exist and we are creating it, make sure
1232 * there is no existing packed ref whose name begins with our
1233 * refname, nor a packed ref whose name is a proper prefix of
1236 if (is_null_oid(&lock
->old_oid
) &&
1237 refs_verify_refname_available(refs
->packed_ref_store
, refname
,
1238 NULL
, NULL
, 0, err
))
1241 lock
->ref_name
= xstrdup(refname
);
1244 if (raceproof_create_file(ref_file
.buf
, create_reflock
, &lock
->lk
)) {
1245 unable_to_lock_message(ref_file
.buf
, errno
, err
);
1249 if (!refs_resolve_ref_unsafe(&refs
->base
, lock
->ref_name
, 0,
1250 &lock
->old_oid
, NULL
))
1251 oidclr(&lock
->old_oid
, refs
->base
.repo
->hash_algo
);
1259 strbuf_release(&ref_file
);
1263 struct ref_to_prune
{
1264 struct ref_to_prune
*next
;
1265 struct object_id oid
;
1266 char name
[FLEX_ARRAY
];
1270 REMOVE_EMPTY_PARENTS_REF
= 0x01,
1271 REMOVE_EMPTY_PARENTS_REFLOG
= 0x02
1275 * Remove empty parent directories associated with the specified
1276 * reference and/or its reflog, but spare [logs/]refs/ and immediate
1277 * subdirs. flags is a combination of REMOVE_EMPTY_PARENTS_REF and/or
1278 * REMOVE_EMPTY_PARENTS_REFLOG.
1280 static void try_remove_empty_parents(struct files_ref_store
*refs
,
1281 const char *refname
,
1284 struct strbuf buf
= STRBUF_INIT
;
1285 struct strbuf sb
= STRBUF_INIT
;
1289 strbuf_addstr(&buf
, refname
);
1291 for (i
= 0; i
< 2; i
++) { /* refs/{heads,tags,...}/ */
1292 while (*p
&& *p
!= '/')
1294 /* tolerate duplicate slashes; see check_refname_format() */
1298 q
= buf
.buf
+ buf
.len
;
1299 while (flags
& (REMOVE_EMPTY_PARENTS_REF
| REMOVE_EMPTY_PARENTS_REFLOG
)) {
1300 while (q
> p
&& *q
!= '/')
1302 while (q
> p
&& *(q
-1) == '/')
1306 strbuf_setlen(&buf
, q
- buf
.buf
);
1309 files_ref_path(refs
, &sb
, buf
.buf
);
1310 if ((flags
& REMOVE_EMPTY_PARENTS_REF
) && rmdir(sb
.buf
))
1311 flags
&= ~REMOVE_EMPTY_PARENTS_REF
;
1314 files_reflog_path(refs
, &sb
, buf
.buf
);
1315 if ((flags
& REMOVE_EMPTY_PARENTS_REFLOG
) && rmdir(sb
.buf
))
1316 flags
&= ~REMOVE_EMPTY_PARENTS_REFLOG
;
1318 strbuf_release(&buf
);
1319 strbuf_release(&sb
);
1322 /* make sure nobody touched the ref, and unlink */
1323 static void prune_ref(struct files_ref_store
*refs
, struct ref_to_prune
*r
)
1325 struct ref_transaction
*transaction
;
1326 struct strbuf err
= STRBUF_INIT
;
1329 if (check_refname_format(r
->name
, 0))
1332 transaction
= ref_store_transaction_begin(&refs
->base
, 0, &err
);
1335 ref_transaction_add_update(
1336 transaction
, r
->name
,
1337 REF_NO_DEREF
| REF_HAVE_NEW
| REF_HAVE_OLD
| REF_IS_PRUNING
,
1338 null_oid(the_hash_algo
), &r
->oid
, NULL
, NULL
, NULL
, NULL
);
1339 if (ref_transaction_commit(transaction
, &err
))
1346 error("%s", err
.buf
);
1347 strbuf_release(&err
);
1348 ref_transaction_free(transaction
);
1353 * Prune the loose versions of the references in the linked list
1354 * `*refs_to_prune`, freeing the entries in the list as we go.
1356 static void prune_refs(struct files_ref_store
*refs
, struct ref_to_prune
**refs_to_prune
)
1358 while (*refs_to_prune
) {
1359 struct ref_to_prune
*r
= *refs_to_prune
;
1360 *refs_to_prune
= r
->next
;
1367 * Return true if the specified reference should be packed.
1369 static int should_pack_ref(struct files_ref_store
*refs
,
1370 const char *refname
,
1371 const struct object_id
*oid
, unsigned int ref_flags
,
1372 struct pack_refs_opts
*opts
)
1374 struct string_list_item
*item
;
1376 /* Do not pack per-worktree refs: */
1377 if (parse_worktree_ref(refname
, NULL
, NULL
, NULL
) !=
1378 REF_WORKTREE_SHARED
)
1381 /* Do not pack symbolic refs: */
1382 if (ref_flags
& REF_ISSYMREF
)
1385 /* Do not pack broken refs: */
1386 if (!ref_resolves_to_object(refname
, refs
->base
.repo
, oid
, ref_flags
))
1389 if (ref_excluded(opts
->exclusions
, refname
))
1392 for_each_string_list_item(item
, opts
->includes
)
1393 if (!wildmatch(item
->string
, refname
, 0))
1399 static int should_pack_refs(struct files_ref_store
*refs
,
1400 struct pack_refs_opts
*opts
)
1402 struct ref_iterator
*iter
;
1404 size_t refcount
= 0;
1408 if (!(opts
->flags
& PACK_REFS_AUTO
))
1411 ret
= packed_refs_size(refs
->packed_ref_store
, &packed_size
);
1413 die("cannot determine packed-refs size");
1416 * Packing loose references into the packed-refs file scales with the
1417 * number of references we're about to write. We thus decide whether we
1418 * repack refs by weighing the current size of the packed-refs file
1419 * against the number of loose references. This is done such that we do
1420 * not repack too often on repositories with a huge number of
1421 * references, where we can expect a lot of churn in the number of
1424 * As a heuristic, we repack if the number of loose references in the
1425 * repository exceeds `log2(nr_packed_refs) * 5`, where we estimate
1426 * `nr_packed_refs = packed_size / 100`, which scales as following:
1428 * - 1kB ~ 10 packed refs: 16 refs
1429 * - 10kB ~ 100 packed refs: 33 refs
1430 * - 100kB ~ 1k packed refs: 49 refs
1431 * - 1MB ~ 10k packed refs: 66 refs
1432 * - 10MB ~ 100k packed refs: 82 refs
1433 * - 100MB ~ 1m packed refs: 99 refs
1435 * We thus allow roughly 16 additional loose refs per factor of ten of
1436 * packed refs. This heuristic may be tweaked in the future, but should
1437 * serve as a sufficiently good first iteration.
1439 limit
= log2u(packed_size
/ 100) * 5;
1443 iter
= cache_ref_iterator_begin(get_loose_ref_cache(refs
, 0), NULL
,
1444 refs
->base
.repo
, 0);
1445 while ((ret
= ref_iterator_advance(iter
)) == ITER_OK
) {
1446 if (should_pack_ref(refs
, iter
->refname
, iter
->oid
,
1449 if (refcount
>= limit
) {
1450 ref_iterator_free(iter
);
1455 if (ret
!= ITER_DONE
)
1456 die("error while iterating over references");
1458 ref_iterator_free(iter
);
1462 static int files_pack_refs(struct ref_store
*ref_store
,
1463 struct pack_refs_opts
*opts
)
1465 struct files_ref_store
*refs
=
1466 files_downcast(ref_store
, REF_STORE_WRITE
| REF_STORE_ODB
,
1468 struct ref_iterator
*iter
;
1470 struct ref_to_prune
*refs_to_prune
= NULL
;
1471 struct strbuf err
= STRBUF_INIT
;
1472 struct ref_transaction
*transaction
;
1474 if (!should_pack_refs(refs
, opts
))
1477 transaction
= ref_store_transaction_begin(refs
->packed_ref_store
,
1482 packed_refs_lock(refs
->packed_ref_store
, LOCK_DIE_ON_ERROR
, &err
);
1484 iter
= cache_ref_iterator_begin(get_loose_ref_cache(refs
, 0), NULL
,
1485 refs
->base
.repo
, 0);
1486 while ((ok
= ref_iterator_advance(iter
)) == ITER_OK
) {
1488 * If the loose reference can be packed, add an entry
1489 * in the packed ref cache. If the reference should be
1490 * pruned, also add it to refs_to_prune.
1492 if (!should_pack_ref(refs
, iter
->refname
, iter
->oid
, iter
->flags
, opts
))
1496 * Add a reference creation for this reference to the
1497 * packed-refs transaction:
1499 if (ref_transaction_update(transaction
, iter
->refname
,
1500 iter
->oid
, NULL
, NULL
, NULL
,
1501 REF_NO_DEREF
, NULL
, &err
))
1502 die("failure preparing to create packed reference %s: %s",
1503 iter
->refname
, err
.buf
);
1505 /* Schedule the loose reference for pruning if requested. */
1506 if ((opts
->flags
& PACK_REFS_PRUNE
)) {
1507 struct ref_to_prune
*n
;
1508 FLEX_ALLOC_STR(n
, name
, iter
->refname
);
1509 oidcpy(&n
->oid
, iter
->oid
);
1510 n
->next
= refs_to_prune
;
1514 if (ok
!= ITER_DONE
)
1515 die("error while iterating over references");
1517 if (ref_transaction_commit(transaction
, &err
))
1518 die("unable to write new packed-refs: %s", err
.buf
);
1520 ref_transaction_free(transaction
);
1522 packed_refs_unlock(refs
->packed_ref_store
);
1524 prune_refs(refs
, &refs_to_prune
);
1525 ref_iterator_free(iter
);
1526 strbuf_release(&err
);
1530 static int files_optimize(struct ref_store
*ref_store
, struct pack_refs_opts
*opts
)
1533 * For the "files" backend, "optimizing" is the same as "packing".
1534 * So, we just call the existing worker function for packing.
1536 return files_pack_refs(ref_store
, opts
);
1540 * People using contrib's git-new-workdir have .git/logs/refs ->
1541 * /some/other/path/.git/logs/refs, and that may live on another device.
1543 * IOW, to avoid cross device rename errors, the temporary renamed log must
1544 * live into logs/refs.
1546 #define TMP_RENAMED_LOG "refs/.tmp-renamed-log"
1549 const char *tmp_renamed_log
;
1553 static int rename_tmp_log_callback(const char *path
, void *cb_data
)
1555 struct rename_cb
*cb
= cb_data
;
1557 if (rename(cb
->tmp_renamed_log
, path
)) {
1559 * rename(a, b) when b is an existing directory ought
1560 * to result in ISDIR, but Solaris 5.8 gives ENOTDIR.
1561 * Sheesh. Record the true errno for error reporting,
1562 * but report EISDIR to raceproof_create_file() so
1563 * that it knows to retry.
1565 cb
->true_errno
= errno
;
1566 if (errno
== ENOTDIR
)
1574 static int rename_tmp_log(struct files_ref_store
*refs
, const char *newrefname
)
1576 struct strbuf path
= STRBUF_INIT
;
1577 struct strbuf tmp
= STRBUF_INIT
;
1578 struct rename_cb cb
;
1581 files_reflog_path(refs
, &path
, newrefname
);
1582 files_reflog_path(refs
, &tmp
, TMP_RENAMED_LOG
);
1583 cb
.tmp_renamed_log
= tmp
.buf
;
1584 ret
= raceproof_create_file(path
.buf
, rename_tmp_log_callback
, &cb
);
1586 if (errno
== EISDIR
)
1587 error("directory not empty: %s", path
.buf
);
1589 error("unable to move logfile %s to %s: %s",
1591 strerror(cb
.true_errno
));
1594 strbuf_release(&path
);
1595 strbuf_release(&tmp
);
1599 static enum ref_transaction_error
write_ref_to_lockfile(struct files_ref_store
*refs
,
1600 struct ref_lock
*lock
,
1601 const struct object_id
*oid
,
1602 int skip_oid_verification
,
1603 struct strbuf
*err
);
1604 static int commit_ref_update(struct files_ref_store
*refs
,
1605 struct ref_lock
*lock
,
1606 const struct object_id
*oid
, const char *logmsg
,
1608 struct strbuf
*err
);
1611 * Emit a better error message than lockfile.c's
1612 * unable_to_lock_message() would in case there is a D/F conflict with
1613 * another existing reference. If there would be a conflict, emit an error
1614 * message and return false; otherwise, return true.
1616 * Note that this function is not safe against all races with other
1617 * processes, and that's not its job. We'll emit a more verbose error on D/f
1618 * conflicts if we get past it into lock_ref_oid_basic().
1620 static int refs_rename_ref_available(struct ref_store
*refs
,
1621 const char *old_refname
,
1622 const char *new_refname
)
1624 struct string_list skip
= STRING_LIST_INIT_NODUP
;
1625 struct strbuf err
= STRBUF_INIT
;
1628 string_list_insert(&skip
, old_refname
);
1629 ok
= !refs_verify_refname_available(refs
, new_refname
,
1630 NULL
, &skip
, 0, &err
);
1632 error("%s", err
.buf
);
1634 string_list_clear(&skip
, 0);
1635 strbuf_release(&err
);
1639 static int files_copy_or_rename_ref(struct ref_store
*ref_store
,
1640 const char *oldrefname
, const char *newrefname
,
1641 const char *logmsg
, int copy
)
1643 struct files_ref_store
*refs
=
1644 files_downcast(ref_store
, REF_STORE_WRITE
, "rename_ref");
1645 struct object_id orig_oid
;
1646 int flag
= 0, logmoved
= 0;
1647 struct ref_lock
*lock
;
1648 struct stat loginfo
;
1649 struct strbuf sb_oldref
= STRBUF_INIT
;
1650 struct strbuf sb_newref
= STRBUF_INIT
;
1651 struct strbuf tmp_renamed_log
= STRBUF_INIT
;
1653 struct strbuf err
= STRBUF_INIT
;
1655 files_reflog_path(refs
, &sb_oldref
, oldrefname
);
1656 files_reflog_path(refs
, &sb_newref
, newrefname
);
1657 files_reflog_path(refs
, &tmp_renamed_log
, TMP_RENAMED_LOG
);
1659 log
= !lstat(sb_oldref
.buf
, &loginfo
);
1660 if (log
&& S_ISLNK(loginfo
.st_mode
)) {
1661 ret
= error("reflog for %s is a symlink", oldrefname
);
1665 if (!refs_resolve_ref_unsafe(&refs
->base
, oldrefname
,
1666 RESOLVE_REF_READING
| RESOLVE_REF_NO_RECURSE
,
1667 &orig_oid
, &flag
)) {
1668 ret
= error("refname %s not found", oldrefname
);
1672 if (flag
& REF_ISSYMREF
) {
1674 ret
= error("refname %s is a symbolic ref, copying it is not supported",
1677 ret
= error("refname %s is a symbolic ref, renaming it is not supported",
1681 if (!refs_rename_ref_available(&refs
->base
, oldrefname
, newrefname
)) {
1686 if (!copy
&& log
&& rename(sb_oldref
.buf
, tmp_renamed_log
.buf
)) {
1687 ret
= error("unable to move logfile logs/%s to logs/"TMP_RENAMED_LOG
": %s",
1688 oldrefname
, strerror(errno
));
1692 if (copy
&& log
&& copy_file(tmp_renamed_log
.buf
, sb_oldref
.buf
, 0644)) {
1693 ret
= error("unable to copy logfile logs/%s to logs/"TMP_RENAMED_LOG
": %s",
1694 oldrefname
, strerror(errno
));
1698 if (!copy
&& refs_delete_ref(&refs
->base
, logmsg
, oldrefname
,
1699 &orig_oid
, REF_NO_DEREF
)) {
1700 error("unable to delete old %s", oldrefname
);
1705 * Since we are doing a shallow lookup, oid is not the
1706 * correct value to pass to delete_ref as old_oid. But that
1707 * doesn't matter, because an old_oid check wouldn't add to
1708 * the safety anyway; we want to delete the reference whatever
1709 * its current value.
1711 if (!copy
&& refs_resolve_ref_unsafe(&refs
->base
, newrefname
,
1712 RESOLVE_REF_READING
| RESOLVE_REF_NO_RECURSE
,
1714 refs_delete_ref(&refs
->base
, NULL
, newrefname
,
1715 NULL
, REF_NO_DEREF
)) {
1716 if (errno
== EISDIR
) {
1717 struct strbuf path
= STRBUF_INIT
;
1720 files_ref_path(refs
, &path
, newrefname
);
1721 result
= remove_empty_directories(&path
);
1722 strbuf_release(&path
);
1725 error("Directory not empty: %s", newrefname
);
1729 error("unable to delete existing %s", newrefname
);
1734 if (log
&& rename_tmp_log(refs
, newrefname
))
1739 lock
= lock_ref_oid_basic(refs
, newrefname
, &err
);
1742 error("unable to copy '%s' to '%s': %s", oldrefname
, newrefname
, err
.buf
);
1744 error("unable to rename '%s' to '%s': %s", oldrefname
, newrefname
, err
.buf
);
1745 strbuf_release(&err
);
1748 oidcpy(&lock
->old_oid
, &orig_oid
);
1750 if (write_ref_to_lockfile(refs
, lock
, &orig_oid
, 0, &err
) ||
1751 commit_ref_update(refs
, lock
, &orig_oid
, logmsg
, 0, &err
)) {
1752 error("unable to write current sha1 into %s: %s", newrefname
, err
.buf
);
1753 strbuf_release(&err
);
1761 lock
= lock_ref_oid_basic(refs
, oldrefname
, &err
);
1763 error("unable to lock %s for rollback: %s", oldrefname
, err
.buf
);
1764 strbuf_release(&err
);
1768 if (write_ref_to_lockfile(refs
, lock
, &orig_oid
, 0, &err
) ||
1769 commit_ref_update(refs
, lock
, &orig_oid
, NULL
, REF_SKIP_CREATE_REFLOG
, &err
)) {
1770 error("unable to write current sha1 into %s: %s", oldrefname
, err
.buf
);
1771 strbuf_release(&err
);
1775 if (logmoved
&& rename(sb_newref
.buf
, sb_oldref
.buf
))
1776 error("unable to restore logfile %s from %s: %s",
1777 oldrefname
, newrefname
, strerror(errno
));
1778 if (!logmoved
&& log
&&
1779 rename(tmp_renamed_log
.buf
, sb_oldref
.buf
))
1780 error("unable to restore logfile %s from logs/"TMP_RENAMED_LOG
": %s",
1781 oldrefname
, strerror(errno
));
1784 strbuf_release(&sb_newref
);
1785 strbuf_release(&sb_oldref
);
1786 strbuf_release(&tmp_renamed_log
);
1791 static int files_rename_ref(struct ref_store
*ref_store
,
1792 const char *oldrefname
, const char *newrefname
,
1795 return files_copy_or_rename_ref(ref_store
, oldrefname
,
1796 newrefname
, logmsg
, 0);
1799 static int files_copy_ref(struct ref_store
*ref_store
,
1800 const char *oldrefname
, const char *newrefname
,
1803 return files_copy_or_rename_ref(ref_store
, oldrefname
,
1804 newrefname
, logmsg
, 1);
1807 static int close_ref_gently(struct ref_lock
*lock
)
1809 if (close_lock_file_gently(&lock
->lk
))
1814 static int commit_ref(struct ref_lock
*lock
)
1816 char *path
= get_locked_file_path(&lock
->lk
);
1819 if (!lstat(path
, &st
) && S_ISDIR(st
.st_mode
)) {
1821 * There is a directory at the path we want to rename
1822 * the lockfile to. Hopefully it is empty; try to
1825 size_t len
= strlen(path
);
1826 struct strbuf sb_path
= STRBUF_INIT
;
1828 strbuf_attach(&sb_path
, path
, len
, len
);
1831 * If this fails, commit_lock_file() will also fail
1832 * and will report the problem.
1834 remove_empty_directories(&sb_path
);
1835 strbuf_release(&sb_path
);
1840 if (commit_lock_file(&lock
->lk
))
1845 static int open_or_create_logfile(const char *path
, void *cb
)
1849 *fd
= open(path
, O_APPEND
| O_WRONLY
| O_CREAT
, 0666);
1850 return (*fd
< 0) ? -1 : 0;
1854 * Create a reflog for a ref. If force_create = 0, only create the
1855 * reflog for certain refs (those for which should_autocreate_reflog
1856 * returns non-zero). Otherwise, create it regardless of the reference
1857 * name. If the logfile already existed or was created, return 0 and
1858 * set *logfd to the file descriptor opened for appending to the file.
1859 * If no logfile exists and we decided not to create one, return 0 and
1860 * set *logfd to -1. On failure, fill in *err, set *logfd to -1, and
1863 static int log_ref_setup(struct files_ref_store
*refs
,
1864 const char *refname
, int force_create
,
1865 int *logfd
, struct strbuf
*err
)
1867 enum log_refs_config log_refs_cfg
= refs
->log_all_ref_updates
;
1868 struct strbuf logfile_sb
= STRBUF_INIT
;
1871 if (log_refs_cfg
== LOG_REFS_UNSET
)
1872 log_refs_cfg
= is_bare_repository() ? LOG_REFS_NONE
: LOG_REFS_NORMAL
;
1874 files_reflog_path(refs
, &logfile_sb
, refname
);
1875 logfile
= strbuf_detach(&logfile_sb
, NULL
);
1877 if (force_create
|| should_autocreate_reflog(log_refs_cfg
, refname
)) {
1878 if (raceproof_create_file(logfile
, open_or_create_logfile
, logfd
)) {
1879 if (errno
== ENOENT
)
1880 strbuf_addf(err
, "unable to create directory for '%s': "
1881 "%s", logfile
, strerror(errno
));
1882 else if (errno
== EISDIR
)
1883 strbuf_addf(err
, "there are still logs under '%s'",
1886 strbuf_addf(err
, "unable to append to '%s': %s",
1887 logfile
, strerror(errno
));
1892 *logfd
= open(logfile
, O_APPEND
| O_WRONLY
);
1894 if (errno
== ENOENT
|| errno
== EISDIR
) {
1896 * The logfile doesn't already exist,
1897 * but that is not an error; it only
1898 * means that we won't write log
1903 strbuf_addf(err
, "unable to append to '%s': %s",
1904 logfile
, strerror(errno
));
1911 adjust_shared_perm(the_repository
, logfile
);
1921 static int files_create_reflog(struct ref_store
*ref_store
, const char *refname
,
1924 struct files_ref_store
*refs
=
1925 files_downcast(ref_store
, REF_STORE_WRITE
, "create_reflog");
1928 if (log_ref_setup(refs
, refname
, 1, &fd
, err
))
1937 static int log_ref_write_fd(int fd
, const struct object_id
*old_oid
,
1938 const struct object_id
*new_oid
,
1939 const char *committer
, const char *msg
)
1941 struct strbuf sb
= STRBUF_INIT
;
1945 committer
= git_committer_info(0);
1947 strbuf_addf(&sb
, "%s %s %s", oid_to_hex(old_oid
), oid_to_hex(new_oid
), committer
);
1949 strbuf_addch(&sb
, '\t');
1950 strbuf_addstr(&sb
, msg
);
1952 strbuf_addch(&sb
, '\n');
1953 if (write_in_full(fd
, sb
.buf
, sb
.len
) < 0)
1955 strbuf_release(&sb
);
1959 static int files_log_ref_write(struct files_ref_store
*refs
,
1960 const char *refname
,
1961 const struct object_id
*old_oid
,
1962 const struct object_id
*new_oid
,
1963 const char *committer_info
, const char *msg
,
1964 int flags
, struct strbuf
*err
)
1968 if (flags
& REF_SKIP_CREATE_REFLOG
)
1971 result
= log_ref_setup(refs
, refname
,
1972 flags
& REF_FORCE_CREATE_REFLOG
,
1980 result
= log_ref_write_fd(logfd
, old_oid
, new_oid
, committer_info
, msg
);
1982 struct strbuf sb
= STRBUF_INIT
;
1983 int save_errno
= errno
;
1985 files_reflog_path(refs
, &sb
, refname
);
1986 strbuf_addf(err
, "unable to append to '%s': %s",
1987 sb
.buf
, strerror(save_errno
));
1988 strbuf_release(&sb
);
1993 struct strbuf sb
= STRBUF_INIT
;
1994 int save_errno
= errno
;
1996 files_reflog_path(refs
, &sb
, refname
);
1997 strbuf_addf(err
, "unable to append to '%s': %s",
1998 sb
.buf
, strerror(save_errno
));
1999 strbuf_release(&sb
);
2006 * Write oid into the open lockfile, then close the lockfile. On
2007 * errors, rollback the lockfile, fill in *err and return -1.
2009 static enum ref_transaction_error
write_ref_to_lockfile(struct files_ref_store
*refs
,
2010 struct ref_lock
*lock
,
2011 const struct object_id
*oid
,
2012 int skip_oid_verification
,
2015 static char term
= '\n';
2019 if (!skip_oid_verification
) {
2020 o
= parse_object(refs
->base
.repo
, oid
);
2024 "trying to write ref '%s' with nonexistent object %s",
2025 lock
->ref_name
, oid_to_hex(oid
));
2027 return REF_TRANSACTION_ERROR_INVALID_NEW_VALUE
;
2029 if (o
->type
!= OBJ_COMMIT
&& is_branch(lock
->ref_name
)) {
2032 "trying to write non-commit object %s to branch '%s'",
2033 oid_to_hex(oid
), lock
->ref_name
);
2035 return REF_TRANSACTION_ERROR_INVALID_NEW_VALUE
;
2038 fd
= get_lock_file_fd(&lock
->lk
);
2039 if (write_in_full(fd
, oid_to_hex(oid
), refs
->base
.repo
->hash_algo
->hexsz
) < 0 ||
2040 write_in_full(fd
, &term
, 1) < 0 ||
2041 fsync_component(FSYNC_COMPONENT_REFERENCE
, get_lock_file_fd(&lock
->lk
)) < 0 ||
2042 close_ref_gently(lock
) < 0) {
2044 "couldn't write '%s'", get_lock_file_path(&lock
->lk
));
2046 return REF_TRANSACTION_ERROR_GENERIC
;
2052 * Commit a change to a loose reference that has already been written
2053 * to the loose reference lockfile. Also update the reflogs if
2054 * necessary, using the specified lockmsg (which can be NULL).
2056 static int commit_ref_update(struct files_ref_store
*refs
,
2057 struct ref_lock
*lock
,
2058 const struct object_id
*oid
, const char *logmsg
,
2062 files_assert_main_repository(refs
, "commit_ref_update");
2064 clear_loose_ref_cache(refs
);
2065 if (files_log_ref_write(refs
, lock
->ref_name
, &lock
->old_oid
, oid
, NULL
,
2066 logmsg
, flags
, err
)) {
2067 char *old_msg
= strbuf_detach(err
, NULL
);
2068 strbuf_addf(err
, "cannot update the ref '%s': %s",
2069 lock
->ref_name
, old_msg
);
2075 if (strcmp(lock
->ref_name
, "HEAD") != 0) {
2077 * Special hack: If a branch is updated directly and HEAD
2078 * points to it (may happen on the remote side of a push
2079 * for example) then logically the HEAD reflog should be
2081 * A generic solution implies reverse symref information,
2082 * but finding all symrefs pointing to the given branch
2083 * would be rather costly for this rare event (the direct
2084 * update of a branch) to be worth it. So let's cheat and
2085 * check with HEAD only which should cover 99% of all usage
2086 * scenarios (even 100% of the default ones).
2089 const char *head_ref
;
2091 head_ref
= refs_resolve_ref_unsafe(&refs
->base
, "HEAD",
2092 RESOLVE_REF_READING
,
2094 if (head_ref
&& (head_flag
& REF_ISSYMREF
) &&
2095 !strcmp(head_ref
, lock
->ref_name
)) {
2096 struct strbuf log_err
= STRBUF_INIT
;
2097 if (files_log_ref_write(refs
, "HEAD", &lock
->old_oid
,
2098 oid
, NULL
, logmsg
, flags
,
2100 error("%s", log_err
.buf
);
2101 strbuf_release(&log_err
);
2106 if (commit_ref(lock
)) {
2107 strbuf_addf(err
, "couldn't set '%s'", lock
->ref_name
);
2116 #if defined(NO_SYMLINK_HEAD) || defined(WITH_BREAKING_CHANGES)
2117 #define create_ref_symlink(a, b) (-1)
2119 static int create_ref_symlink(struct ref_lock
*lock
, const char *target
)
2121 static int warn_once
= 1;
2125 ref_path
= get_locked_file_path(&lock
->lk
);
2127 ret
= symlink(target
, ref_path
);
2131 fprintf(stderr
, "no symlink - falling back to symbolic ref\n");
2134 warning(_("'core.preferSymlinkRefs=true' is nominated for removal.\n"
2135 "hint: The use of symbolic links for symbolic refs is deprecated\n"
2136 "hint: and will be removed in Git 3.0. The configuration that\n"
2137 "hint: tells Git to use them is thus going away. You can unset\n"
2140 "hint:\tgit config unset core.preferSymlinkRefs\n"
2142 "hint: Git will then use the textual symref format instead."));
2149 static int create_symref_lock(struct ref_lock
*lock
, const char *target
,
2152 if (!fdopen_lock_file(&lock
->lk
, "w")) {
2153 strbuf_addf(err
, "unable to fdopen %s: %s",
2154 get_lock_file_path(&lock
->lk
), strerror(errno
));
2158 if (fprintf(get_lock_file_fp(&lock
->lk
), "ref: %s\n", target
) < 0) {
2159 strbuf_addf(err
, "unable to write to %s: %s",
2160 get_lock_file_path(&lock
->lk
), strerror(errno
));
2167 static int files_reflog_exists(struct ref_store
*ref_store
,
2168 const char *refname
)
2170 struct files_ref_store
*refs
=
2171 files_downcast(ref_store
, REF_STORE_READ
, "reflog_exists");
2172 struct strbuf sb
= STRBUF_INIT
;
2176 files_reflog_path(refs
, &sb
, refname
);
2177 ret
= !lstat(sb
.buf
, &st
) && S_ISREG(st
.st_mode
);
2178 strbuf_release(&sb
);
2182 static int files_delete_reflog(struct ref_store
*ref_store
,
2183 const char *refname
)
2185 struct files_ref_store
*refs
=
2186 files_downcast(ref_store
, REF_STORE_WRITE
, "delete_reflog");
2187 struct strbuf sb
= STRBUF_INIT
;
2190 files_reflog_path(refs
, &sb
, refname
);
2191 ret
= remove_path(sb
.buf
);
2192 strbuf_release(&sb
);
2196 static int show_one_reflog_ent(struct files_ref_store
*refs
,
2197 const char *refname
,
2199 each_reflog_ent_fn fn
, void *cb_data
)
2201 struct object_id ooid
, noid
;
2202 char *email_end
, *message
;
2203 timestamp_t timestamp
;
2205 const char *p
= sb
->buf
;
2207 /* old SP new SP name <email> SP time TAB msg LF */
2208 if (!sb
->len
|| sb
->buf
[sb
->len
- 1] != '\n' ||
2209 parse_oid_hex_algop(p
, &ooid
, &p
, refs
->base
.repo
->hash_algo
) || *p
++ != ' ' ||
2210 parse_oid_hex_algop(p
, &noid
, &p
, refs
->base
.repo
->hash_algo
) || *p
++ != ' ' ||
2211 !(email_end
= strchr(p
, '>')) ||
2212 email_end
[1] != ' ' ||
2213 !(timestamp
= parse_timestamp(email_end
+ 2, &message
, 10)) ||
2214 !message
|| message
[0] != ' ' ||
2215 (message
[1] != '+' && message
[1] != '-') ||
2216 !isdigit(message
[2]) || !isdigit(message
[3]) ||
2217 !isdigit(message
[4]) || !isdigit(message
[5]))
2218 return 0; /* corrupt? */
2219 email_end
[1] = '\0';
2220 tz
= strtol(message
+ 1, NULL
, 10);
2221 if (message
[6] != '\t')
2225 return fn(refname
, &ooid
, &noid
, p
, timestamp
, tz
, message
, cb_data
);
2228 static char *find_beginning_of_line(char *bob
, char *scan
)
2230 while (bob
< scan
&& *(--scan
) != '\n')
2231 ; /* keep scanning backwards */
2233 * Return either beginning of the buffer, or LF at the end of
2234 * the previous line.
2239 static int files_for_each_reflog_ent_reverse(struct ref_store
*ref_store
,
2240 const char *refname
,
2241 each_reflog_ent_fn fn
,
2244 struct files_ref_store
*refs
=
2245 files_downcast(ref_store
, REF_STORE_READ
,
2246 "for_each_reflog_ent_reverse");
2247 struct strbuf sb
= STRBUF_INIT
;
2250 int ret
= 0, at_tail
= 1;
2252 files_reflog_path(refs
, &sb
, refname
);
2253 logfp
= fopen(sb
.buf
, "r");
2254 strbuf_release(&sb
);
2258 /* Jump to the end */
2259 if (fseek(logfp
, 0, SEEK_END
) < 0)
2260 ret
= error("cannot seek back reflog for %s: %s",
2261 refname
, strerror(errno
));
2263 while (!ret
&& 0 < pos
) {
2269 /* Fill next block from the end */
2270 cnt
= (sizeof(buf
) < pos
) ? sizeof(buf
) : pos
;
2271 if (fseek(logfp
, pos
- cnt
, SEEK_SET
)) {
2272 ret
= error("cannot seek back reflog for %s: %s",
2273 refname
, strerror(errno
));
2276 nread
= fread(buf
, cnt
, 1, logfp
);
2278 ret
= error("cannot read %d bytes from reflog for %s: %s",
2279 cnt
, refname
, strerror(errno
));
2284 scanp
= endp
= buf
+ cnt
;
2285 if (at_tail
&& scanp
[-1] == '\n')
2286 /* Looking at the final LF at the end of the file */
2290 while (buf
< scanp
) {
2292 * terminating LF of the previous line, or the beginning
2297 bp
= find_beginning_of_line(buf
, scanp
);
2301 * The newline is the end of the previous line,
2302 * so we know we have complete line starting
2303 * at (bp + 1). Prefix it onto any prior data
2304 * we collected for the line and process it.
2306 strbuf_splice(&sb
, 0, 0, bp
+ 1, endp
- (bp
+ 1));
2309 ret
= show_one_reflog_ent(refs
, refname
, &sb
, fn
, cb_data
);
2315 * We are at the start of the buffer, and the
2316 * start of the file; there is no previous
2317 * line, and we have everything for this one.
2318 * Process it, and we can end the loop.
2320 strbuf_splice(&sb
, 0, 0, buf
, endp
- buf
);
2321 ret
= show_one_reflog_ent(refs
, refname
, &sb
, fn
, cb_data
);
2328 * We are at the start of the buffer, and there
2329 * is more file to read backwards. Which means
2330 * we are in the middle of a line. Note that we
2331 * may get here even if *bp was a newline; that
2332 * just means we are at the exact end of the
2333 * previous line, rather than some spot in the
2336 * Save away what we have to be combined with
2337 * the data from the next read.
2339 strbuf_splice(&sb
, 0, 0, buf
, endp
- buf
);
2346 BUG("reverse reflog parser had leftover data");
2349 strbuf_release(&sb
);
2353 static int files_for_each_reflog_ent(struct ref_store
*ref_store
,
2354 const char *refname
,
2355 each_reflog_ent_fn fn
, void *cb_data
)
2357 struct files_ref_store
*refs
=
2358 files_downcast(ref_store
, REF_STORE_READ
,
2359 "for_each_reflog_ent");
2361 struct strbuf sb
= STRBUF_INIT
;
2364 files_reflog_path(refs
, &sb
, refname
);
2365 logfp
= fopen(sb
.buf
, "r");
2366 strbuf_release(&sb
);
2370 while (!ret
&& !strbuf_getwholeline(&sb
, logfp
, '\n'))
2371 ret
= show_one_reflog_ent(refs
, refname
, &sb
, fn
, cb_data
);
2373 strbuf_release(&sb
);
2377 struct files_reflog_iterator
{
2378 struct ref_iterator base
;
2379 struct ref_store
*ref_store
;
2380 struct dir_iterator
*dir_iterator
;
2383 static int files_reflog_iterator_advance(struct ref_iterator
*ref_iterator
)
2385 struct files_reflog_iterator
*iter
=
2386 (struct files_reflog_iterator
*)ref_iterator
;
2387 struct dir_iterator
*diter
= iter
->dir_iterator
;
2390 while ((ok
= dir_iterator_advance(diter
)) == ITER_OK
) {
2391 if (!S_ISREG(diter
->st
.st_mode
))
2393 if (check_refname_format(diter
->basename
,
2394 REFNAME_ALLOW_ONELEVEL
))
2397 iter
->base
.refname
= diter
->relative_path
;
2404 static int files_reflog_iterator_seek(struct ref_iterator
*ref_iterator UNUSED
,
2405 const char *refname UNUSED
,
2406 unsigned int flags UNUSED
)
2408 BUG("ref_iterator_seek() called for reflog_iterator");
2411 static int files_reflog_iterator_peel(struct ref_iterator
*ref_iterator UNUSED
,
2412 struct object_id
*peeled UNUSED
)
2414 BUG("ref_iterator_peel() called for reflog_iterator");
2417 static void files_reflog_iterator_release(struct ref_iterator
*ref_iterator
)
2419 struct files_reflog_iterator
*iter
=
2420 (struct files_reflog_iterator
*)ref_iterator
;
2421 dir_iterator_free(iter
->dir_iterator
);
2424 static struct ref_iterator_vtable files_reflog_iterator_vtable
= {
2425 .advance
= files_reflog_iterator_advance
,
2426 .seek
= files_reflog_iterator_seek
,
2427 .peel
= files_reflog_iterator_peel
,
2428 .release
= files_reflog_iterator_release
,
2431 static struct ref_iterator
*reflog_iterator_begin(struct ref_store
*ref_store
,
2434 struct dir_iterator
*diter
;
2435 struct files_reflog_iterator
*iter
;
2436 struct ref_iterator
*ref_iterator
;
2437 struct strbuf sb
= STRBUF_INIT
;
2439 strbuf_addf(&sb
, "%s/logs", gitdir
);
2441 diter
= dir_iterator_begin(sb
.buf
, DIR_ITERATOR_SORTED
);
2443 strbuf_release(&sb
);
2444 return empty_ref_iterator_begin();
2447 CALLOC_ARRAY(iter
, 1);
2448 ref_iterator
= &iter
->base
;
2450 base_ref_iterator_init(ref_iterator
, &files_reflog_iterator_vtable
);
2451 iter
->dir_iterator
= diter
;
2452 iter
->ref_store
= ref_store
;
2453 strbuf_release(&sb
);
2455 return ref_iterator
;
2458 static struct ref_iterator
*files_reflog_iterator_begin(struct ref_store
*ref_store
)
2460 struct files_ref_store
*refs
=
2461 files_downcast(ref_store
, REF_STORE_READ
,
2462 "reflog_iterator_begin");
2464 if (!strcmp(refs
->base
.gitdir
, refs
->gitcommondir
)) {
2465 return reflog_iterator_begin(ref_store
, refs
->gitcommondir
);
2467 return merge_ref_iterator_begin(
2468 reflog_iterator_begin(ref_store
, refs
->base
.gitdir
),
2469 reflog_iterator_begin(ref_store
, refs
->gitcommondir
),
2470 ref_iterator_select
, refs
);
2475 * If update is a direct update of head_ref (the reference pointed to
2476 * by HEAD), then add an extra REF_LOG_ONLY update for HEAD.
2478 static enum ref_transaction_error
split_head_update(struct ref_update
*update
,
2479 struct ref_transaction
*transaction
,
2480 const char *head_ref
,
2483 struct ref_update
*new_update
;
2485 if ((update
->flags
& REF_LOG_ONLY
) ||
2486 (update
->flags
& REF_SKIP_CREATE_REFLOG
) ||
2487 (update
->flags
& REF_IS_PRUNING
) ||
2488 (update
->flags
& REF_UPDATE_VIA_HEAD
))
2491 if (strcmp(update
->refname
, head_ref
))
2495 * First make sure that HEAD is not already in the
2496 * transaction. This check is O(lg N) in the transaction
2497 * size, but it happens at most once per transaction.
2499 if (string_list_has_string(&transaction
->refnames
, "HEAD")) {
2500 /* An entry already existed */
2502 "multiple updates for 'HEAD' (including one "
2503 "via its referent '%s') are not allowed",
2505 return REF_TRANSACTION_ERROR_NAME_CONFLICT
;
2508 new_update
= ref_transaction_add_update(
2509 transaction
, "HEAD",
2510 update
->flags
| REF_LOG_ONLY
| REF_NO_DEREF
| REF_LOG_VIA_SPLIT
,
2511 &update
->new_oid
, &update
->old_oid
,
2512 NULL
, NULL
, update
->committer_info
, update
->msg
);
2513 new_update
->parent_update
= update
;
2516 * Add "HEAD". This insertion is O(N) in the transaction
2517 * size, but it happens at most once per transaction.
2518 * Add new_update->refname instead of a literal "HEAD".
2520 if (strcmp(new_update
->refname
, "HEAD"))
2521 BUG("%s unexpectedly not 'HEAD'", new_update
->refname
);
2527 * update is for a symref that points at referent and doesn't have
2528 * REF_NO_DEREF set. Split it into two updates:
2529 * - The original update, but with REF_LOG_ONLY and REF_NO_DEREF set
2530 * - A new, separate update for the referent reference
2531 * Note that the new update will itself be subject to splitting when
2532 * the iteration gets to it.
2534 static enum ref_transaction_error
split_symref_update(struct ref_update
*update
,
2535 const char *referent
,
2536 struct ref_transaction
*transaction
,
2539 struct ref_update
*new_update
;
2540 unsigned int new_flags
;
2543 * First make sure that referent is not already in the
2544 * transaction. This check is O(lg N) in the transaction
2545 * size, but it happens at most once per symref in a
2548 if (string_list_has_string(&transaction
->refnames
, referent
)) {
2549 /* An entry already exists */
2551 "multiple updates for '%s' (including one "
2552 "via symref '%s') are not allowed",
2553 referent
, update
->refname
);
2554 return REF_TRANSACTION_ERROR_NAME_CONFLICT
;
2557 new_flags
= update
->flags
;
2558 if (!strcmp(update
->refname
, "HEAD")) {
2560 * Record that the new update came via HEAD, so that
2561 * when we process it, split_head_update() doesn't try
2562 * to add another reflog update for HEAD. Note that
2563 * this bit will be propagated if the new_update
2564 * itself needs to be split.
2566 new_flags
|= REF_UPDATE_VIA_HEAD
;
2569 new_update
= ref_transaction_add_update(
2570 transaction
, referent
, new_flags
,
2571 update
->new_target
? NULL
: &update
->new_oid
,
2572 update
->old_target
? NULL
: &update
->old_oid
,
2573 update
->new_target
, update
->old_target
, NULL
,
2576 new_update
->parent_update
= update
;
2579 * Change the symbolic ref update to log only. Also, it
2580 * doesn't need to check its old OID value, as that will be
2581 * done when new_update is processed.
2583 update
->flags
|= REF_LOG_ONLY
| REF_NO_DEREF
;
2589 * Check whether the REF_HAVE_OLD and old_oid values stored in update
2590 * are consistent with oid, which is the reference's current value. If
2591 * everything is OK, return 0; otherwise, write an error message to
2592 * err and return -1.
2594 static enum ref_transaction_error
check_old_oid(struct ref_update
*update
,
2595 struct object_id
*oid
,
2596 struct strbuf
*referent
,
2599 if (update
->flags
& REF_LOG_ONLY
||
2600 !(update
->flags
& REF_HAVE_OLD
))
2603 if (oideq(oid
, &update
->old_oid
)) {
2605 * Normally matching the expected old oid is enough. Either we
2606 * found the ref at the expected state, or we are creating and
2607 * expect the null oid (and likewise found nothing).
2609 * But there is one exception for the null oid: if we found a
2610 * symref pointing to nothing we'll also get the null oid. In
2611 * regular recursive mode, that's good (we'll write to what the
2612 * symref points to, which doesn't exist). But in no-deref
2613 * mode, it means we'll clobber the symref, even though the
2614 * caller asked for this to be a creation event. So flag
2615 * that case to preserve the dangling symref.
2617 if ((update
->flags
& REF_NO_DEREF
) && referent
->len
&&
2619 strbuf_addf(err
, "cannot lock ref '%s': "
2620 "dangling symref already exists",
2621 ref_update_original_update_refname(update
));
2622 return REF_TRANSACTION_ERROR_CREATE_EXISTS
;
2627 if (is_null_oid(&update
->old_oid
)) {
2628 strbuf_addf(err
, "cannot lock ref '%s': "
2629 "reference already exists",
2630 ref_update_original_update_refname(update
));
2631 return REF_TRANSACTION_ERROR_CREATE_EXISTS
;
2632 } else if (is_null_oid(oid
)) {
2633 strbuf_addf(err
, "cannot lock ref '%s': "
2634 "reference is missing but expected %s",
2635 ref_update_original_update_refname(update
),
2636 oid_to_hex(&update
->old_oid
));
2637 return REF_TRANSACTION_ERROR_NONEXISTENT_REF
;
2640 strbuf_addf(err
, "cannot lock ref '%s': is at %s but expected %s",
2641 ref_update_original_update_refname(update
), oid_to_hex(oid
),
2642 oid_to_hex(&update
->old_oid
));
2644 return REF_TRANSACTION_ERROR_INCORRECT_OLD_VALUE
;
2647 struct files_transaction_backend_data
{
2648 struct ref_transaction
*packed_transaction
;
2649 int packed_refs_locked
;
2650 struct strmap ref_locks
;
2654 * Prepare for carrying out update:
2655 * - Lock the reference referred to by update.
2656 * - Read the reference under lock.
2657 * - Check that its old OID value (if specified) is correct, and in
2658 * any case record it in update->lock->old_oid for later use when
2659 * writing the reflog.
2660 * - If it is a symref update without REF_NO_DEREF, split it up into a
2661 * REF_LOG_ONLY update of the symref and add a separate update for
2662 * the referent to transaction.
2663 * - If it is an update of head_ref, add a corresponding REF_LOG_ONLY
2666 static enum ref_transaction_error
lock_ref_for_update(struct files_ref_store
*refs
,
2667 struct ref_update
*update
,
2669 struct ref_transaction
*transaction
,
2670 const char *head_ref
,
2671 struct string_list
*refnames_to_check
,
2674 struct strbuf referent
= STRBUF_INIT
;
2675 int mustexist
= ref_update_expects_existing_old_ref(update
);
2676 struct files_transaction_backend_data
*backend_data
;
2677 enum ref_transaction_error ret
= 0;
2678 struct ref_lock
*lock
;
2680 files_assert_main_repository(refs
, "lock_ref_for_update");
2682 backend_data
= transaction
->backend_data
;
2684 if ((update
->flags
& REF_HAVE_NEW
) && ref_update_has_null_new_value(update
))
2685 update
->flags
|= REF_DELETING
;
2688 ret
= split_head_update(update
, transaction
, head_ref
, err
);
2693 lock
= strmap_get(&backend_data
->ref_locks
, update
->refname
);
2697 ret
= lock_raw_ref(refs
, transaction
, update_idx
, mustexist
,
2698 refnames_to_check
, &lock
, &referent
, err
);
2702 reason
= strbuf_detach(err
, NULL
);
2703 strbuf_addf(err
, "cannot lock ref '%s': %s",
2704 ref_update_original_update_refname(update
), reason
);
2709 strmap_put(&backend_data
->ref_locks
, update
->refname
, lock
);
2712 update
->backend_data
= lock
;
2714 if (update
->flags
& REF_LOG_VIA_SPLIT
) {
2715 struct ref_lock
*parent_lock
;
2717 if (!update
->parent_update
)
2718 BUG("split update without a parent");
2720 parent_lock
= update
->parent_update
->backend_data
;
2723 * Check that "HEAD" didn't racily change since we have looked
2724 * it up. If it did we must refuse to write the reflog entry.
2726 * Note that this does not catch all races: if "HEAD" was
2727 * racily changed to point to one of the refs part of the
2728 * transaction then we would miss writing the split reflog
2731 if (!(update
->type
& REF_ISSYMREF
) ||
2732 strcmp(update
->parent_update
->refname
, referent
.buf
)) {
2733 strbuf_addstr(err
, "HEAD has been racily updated");
2734 ret
= REF_TRANSACTION_ERROR_GENERIC
;
2738 if (update
->flags
& REF_HAVE_OLD
) {
2739 oidcpy(&lock
->old_oid
, &update
->old_oid
);
2741 oidcpy(&lock
->old_oid
, &parent_lock
->old_oid
);
2743 } else if (update
->type
& REF_ISSYMREF
) {
2744 if (update
->flags
& REF_NO_DEREF
) {
2746 * We won't be reading the referent as part of
2747 * the transaction, so we have to read it here
2748 * to record and possibly check old_oid:
2750 if (!refs_resolve_ref_unsafe(&refs
->base
,
2752 &lock
->old_oid
, NULL
)) {
2753 if (update
->flags
& REF_HAVE_OLD
) {
2754 strbuf_addf(err
, "cannot lock ref '%s': "
2755 "error reading reference",
2756 ref_update_original_update_refname(update
));
2757 ret
= REF_TRANSACTION_ERROR_GENERIC
;
2762 if (update
->old_target
)
2763 ret
= ref_update_check_old_target(referent
.buf
, update
, err
);
2765 ret
= check_old_oid(update
, &lock
->old_oid
,
2771 * Create a new update for the reference this
2772 * symref is pointing at. Also, we will record
2773 * and verify old_oid for this update as part
2774 * of processing the split-off update, so we
2775 * don't have to do it here.
2777 ret
= split_symref_update(update
, referent
.buf
,
2783 struct ref_update
*parent_update
;
2786 * Even if the ref is a regular ref, if `old_target` is set, we
2787 * fail with an error.
2789 if (update
->old_target
) {
2790 strbuf_addf(err
, _("cannot lock ref '%s': "
2791 "expected symref with target '%s': "
2792 "but is a regular ref"),
2793 ref_update_original_update_refname(update
),
2794 update
->old_target
);
2795 ret
= REF_TRANSACTION_ERROR_EXPECTED_SYMREF
;
2798 ret
= check_old_oid(update
, &lock
->old_oid
,
2806 * If this update is happening indirectly because of a
2807 * symref update, record the old OID in the parent
2810 for (parent_update
= update
->parent_update
;
2812 parent_update
= parent_update
->parent_update
) {
2813 struct ref_lock
*parent_lock
= parent_update
->backend_data
;
2814 oidcpy(&parent_lock
->old_oid
, &lock
->old_oid
);
2818 if (update
->new_target
&& !(update
->flags
& REF_LOG_ONLY
)) {
2819 if (create_symref_lock(lock
, update
->new_target
, err
)) {
2820 ret
= REF_TRANSACTION_ERROR_GENERIC
;
2824 if (close_ref_gently(lock
)) {
2825 strbuf_addf(err
, "couldn't close '%s.lock'",
2827 ret
= REF_TRANSACTION_ERROR_GENERIC
;
2832 * Once we have created the symref lock, the commit
2833 * phase of the transaction only needs to commit the lock.
2835 update
->flags
|= REF_NEEDS_COMMIT
;
2836 } else if ((update
->flags
& REF_HAVE_NEW
) &&
2837 !(update
->flags
& REF_DELETING
) &&
2838 !(update
->flags
& REF_LOG_ONLY
)) {
2839 if (!(update
->type
& REF_ISSYMREF
) &&
2840 oideq(&lock
->old_oid
, &update
->new_oid
)) {
2842 * The reference already has the desired
2843 * value, so we don't need to write it.
2846 ret
= write_ref_to_lockfile(
2847 refs
, lock
, &update
->new_oid
,
2848 update
->flags
& REF_SKIP_OID_VERIFICATION
,
2851 char *write_err
= strbuf_detach(err
, NULL
);
2854 * The lock was freed upon failure of
2855 * write_ref_to_lockfile():
2857 update
->backend_data
= NULL
;
2859 "cannot update ref '%s': %s",
2860 update
->refname
, write_err
);
2864 update
->flags
|= REF_NEEDS_COMMIT
;
2868 if (!(update
->flags
& REF_NEEDS_COMMIT
)) {
2870 * We didn't call write_ref_to_lockfile(), so
2871 * the lockfile is still open. Close it to
2872 * free up the file descriptor:
2874 if (close_ref_gently(lock
)) {
2875 strbuf_addf(err
, "couldn't close '%s.lock'",
2877 ret
= REF_TRANSACTION_ERROR_GENERIC
;
2883 strbuf_release(&referent
);
2888 * Unlock any references in `transaction` that are still locked, and
2889 * mark the transaction closed.
2891 static void files_transaction_cleanup(struct files_ref_store
*refs
,
2892 struct ref_transaction
*transaction
)
2895 struct files_transaction_backend_data
*backend_data
=
2896 transaction
->backend_data
;
2897 struct strbuf err
= STRBUF_INIT
;
2899 for (i
= 0; i
< transaction
->nr
; i
++) {
2900 struct ref_update
*update
= transaction
->updates
[i
];
2901 struct ref_lock
*lock
= update
->backend_data
;
2905 try_remove_empty_parents(refs
, update
->refname
,
2906 REMOVE_EMPTY_PARENTS_REF
);
2907 update
->backend_data
= NULL
;
2912 if (backend_data
->packed_transaction
&&
2913 ref_transaction_abort(backend_data
->packed_transaction
, &err
)) {
2914 error("error aborting transaction: %s", err
.buf
);
2915 strbuf_release(&err
);
2918 if (backend_data
->packed_refs_locked
)
2919 packed_refs_unlock(refs
->packed_ref_store
);
2921 strmap_clear(&backend_data
->ref_locks
, 0);
2926 transaction
->state
= REF_TRANSACTION_CLOSED
;
2929 static int files_transaction_prepare(struct ref_store
*ref_store
,
2930 struct ref_transaction
*transaction
,
2933 struct files_ref_store
*refs
=
2934 files_downcast(ref_store
, REF_STORE_WRITE
,
2935 "ref_transaction_prepare");
2938 struct string_list refnames_to_check
= STRING_LIST_INIT_DUP
;
2939 char *head_ref
= NULL
;
2941 struct files_transaction_backend_data
*backend_data
;
2942 struct ref_transaction
*packed_transaction
= NULL
;
2946 if (transaction
->flags
& REF_TRANSACTION_FLAG_INITIAL
)
2948 if (!transaction
->nr
)
2951 CALLOC_ARRAY(backend_data
, 1);
2952 strmap_init(&backend_data
->ref_locks
);
2953 transaction
->backend_data
= backend_data
;
2956 * Fail if any of the updates use REF_IS_PRUNING without REF_NO_DEREF.
2958 for (i
= 0; i
< transaction
->nr
; i
++) {
2959 struct ref_update
*update
= transaction
->updates
[i
];
2961 if ((update
->flags
& REF_IS_PRUNING
) &&
2962 !(update
->flags
& REF_NO_DEREF
))
2963 BUG("REF_IS_PRUNING set without REF_NO_DEREF");
2967 * Special hack: If a branch is updated directly and HEAD
2968 * points to it (may happen on the remote side of a push
2969 * for example) then logically the HEAD reflog should be
2972 * A generic solution would require reverse symref lookups,
2973 * but finding all symrefs pointing to a given branch would be
2974 * rather costly for this rare event (the direct update of a
2975 * branch) to be worth it. So let's cheat and check with HEAD
2976 * only, which should cover 99% of all usage scenarios (even
2977 * 100% of the default ones).
2979 * So if HEAD is a symbolic reference, then record the name of
2980 * the reference that it points to. If we see an update of
2981 * head_ref within the transaction, then split_head_update()
2982 * arranges for the reflog of HEAD to be updated, too.
2984 head_ref
= refs_resolve_refdup(ref_store
, "HEAD",
2985 RESOLVE_REF_NO_RECURSE
,
2988 if (head_ref
&& !(head_type
& REF_ISSYMREF
)) {
2989 FREE_AND_NULL(head_ref
);
2993 * Acquire all locks, verify old values if provided, check
2994 * that new values are valid, and write new values to the
2995 * lockfiles, ready to be activated. Only keep one lockfile
2996 * open at a time to avoid running out of file descriptors.
2997 * Note that lock_ref_for_update() might append more updates
2998 * to the transaction.
3000 for (i
= 0; i
< transaction
->nr
; i
++) {
3001 struct ref_update
*update
= transaction
->updates
[i
];
3003 ret
= lock_ref_for_update(refs
, update
, i
, transaction
,
3004 head_ref
, &refnames_to_check
,
3007 if (ref_transaction_maybe_set_rejected(transaction
, i
, ret
)) {
3016 if (update
->flags
& REF_DELETING
&&
3017 !(update
->flags
& REF_LOG_ONLY
) &&
3018 !(update
->flags
& REF_IS_PRUNING
)) {
3020 * This reference has to be deleted from
3021 * packed-refs if it exists there.
3023 if (!packed_transaction
) {
3024 packed_transaction
= ref_store_transaction_begin(
3025 refs
->packed_ref_store
,
3026 transaction
->flags
, err
);
3027 if (!packed_transaction
) {
3028 ret
= REF_TRANSACTION_ERROR_GENERIC
;
3032 backend_data
->packed_transaction
=
3036 ref_transaction_add_update(
3037 packed_transaction
, update
->refname
,
3038 REF_HAVE_NEW
| REF_NO_DEREF
,
3039 &update
->new_oid
, NULL
,
3040 NULL
, NULL
, NULL
, NULL
);
3045 * Verify that none of the loose reference that we're about to write
3046 * conflict with any existing packed references. Ideally, we'd do this
3047 * check after the packed-refs are locked so that the file cannot
3048 * change underneath our feet. But introducing such a lock now would
3049 * probably do more harm than good as users rely on there not being a
3050 * global lock with the "files" backend.
3052 * Another alternative would be to do the check after the (optional)
3053 * lock, but that would extend the time we spend in the globally-locked
3056 * So instead, we accept the race for now.
3058 if (refs_verify_refnames_available(refs
->packed_ref_store
, &refnames_to_check
,
3059 &transaction
->refnames
, NULL
, transaction
,
3061 ret
= REF_TRANSACTION_ERROR_NAME_CONFLICT
;
3065 if (packed_transaction
) {
3066 if (packed_refs_lock(refs
->packed_ref_store
, 0, err
)) {
3067 ret
= REF_TRANSACTION_ERROR_GENERIC
;
3070 backend_data
->packed_refs_locked
= 1;
3072 if (is_packed_transaction_needed(refs
->packed_ref_store
,
3073 packed_transaction
)) {
3074 ret
= ref_transaction_prepare(packed_transaction
, err
);
3076 * A failure during the prepare step will abort
3077 * itself, but not free. Do that now, and disconnect
3078 * from the files_transaction so it does not try to
3079 * abort us when we hit the cleanup code below.
3082 ref_transaction_free(packed_transaction
);
3083 backend_data
->packed_transaction
= NULL
;
3087 * We can skip rewriting the `packed-refs`
3088 * file. But we do need to leave it locked, so
3089 * that somebody else doesn't pack a reference
3090 * that we are trying to delete.
3092 * We need to disconnect our transaction from
3093 * backend_data, since the abort (whether successful or
3094 * not) will free it.
3096 backend_data
->packed_transaction
= NULL
;
3097 if (ref_transaction_abort(packed_transaction
, err
)) {
3098 ret
= REF_TRANSACTION_ERROR_GENERIC
;
3106 string_list_clear(&refnames_to_check
, 1);
3109 files_transaction_cleanup(refs
, transaction
);
3111 transaction
->state
= REF_TRANSACTION_PREPARED
;
3116 static int parse_and_write_reflog(struct files_ref_store
*refs
,
3117 struct ref_update
*update
,
3118 struct ref_lock
*lock
,
3121 struct object_id
*old_oid
= &lock
->old_oid
;
3123 if (update
->flags
& REF_LOG_USE_PROVIDED_OIDS
) {
3124 if (!(update
->flags
& REF_HAVE_OLD
) ||
3125 !(update
->flags
& REF_HAVE_NEW
) ||
3126 !(update
->flags
& REF_LOG_ONLY
)) {
3127 strbuf_addf(err
, _("trying to write reflog for '%s' "
3128 "with incomplete values"), update
->refname
);
3129 return REF_TRANSACTION_ERROR_GENERIC
;
3132 old_oid
= &update
->old_oid
;
3135 if (update
->new_target
) {
3137 * We want to get the resolved OID for the target, to ensure
3138 * that the correct value is added to the reflog.
3140 if (!refs_resolve_ref_unsafe(&refs
->base
, update
->new_target
,
3141 RESOLVE_REF_READING
,
3142 &update
->new_oid
, NULL
)) {
3144 * TODO: currently we skip creating reflogs for dangling
3145 * symref updates. It would be nice to capture this as
3146 * zero oid updates however.
3152 if (files_log_ref_write(refs
, lock
->ref_name
, old_oid
,
3153 &update
->new_oid
, update
->committer_info
,
3154 update
->msg
, update
->flags
, err
)) {
3155 char *old_msg
= strbuf_detach(err
, NULL
);
3157 strbuf_addf(err
, "cannot update the ref '%s': %s",
3158 lock
->ref_name
, old_msg
);
3161 update
->backend_data
= NULL
;
3168 static int ref_present(const char *refname
, const char *referent UNUSED
,
3169 const struct object_id
*oid UNUSED
,
3173 struct string_list
*affected_refnames
= cb_data
;
3175 return string_list_has_string(affected_refnames
, refname
);
3178 static int files_transaction_finish_initial(struct files_ref_store
*refs
,
3179 struct ref_transaction
*transaction
,
3184 struct string_list affected_refnames
= STRING_LIST_INIT_NODUP
;
3185 struct string_list refnames_to_check
= STRING_LIST_INIT_NODUP
;
3186 struct ref_transaction
*packed_transaction
= NULL
;
3187 struct ref_transaction
*loose_transaction
= NULL
;
3191 if (transaction
->state
!= REF_TRANSACTION_PREPARED
)
3192 BUG("commit called for transaction that is not prepared");
3195 * It's really undefined to call this function in an active
3196 * repository or when there are existing references: we are
3197 * only locking and changing packed-refs, so (1) any
3198 * simultaneous processes might try to change a reference at
3199 * the same time we do, and (2) any existing loose versions of
3200 * the references that we are setting would have precedence
3201 * over our values. But some remote helpers create the remote
3202 * "HEAD" and "master" branches before calling this function,
3203 * so here we really only check that none of the references
3204 * that we are creating already exists.
3206 if (refs_for_each_rawref(&refs
->base
, ref_present
,
3207 &transaction
->refnames
))
3208 BUG("initial ref transaction called with existing refs");
3210 packed_transaction
= ref_store_transaction_begin(refs
->packed_ref_store
,
3211 transaction
->flags
, err
);
3212 if (!packed_transaction
) {
3213 ret
= REF_TRANSACTION_ERROR_GENERIC
;
3217 for (i
= 0; i
< transaction
->nr
; i
++) {
3218 struct ref_update
*update
= transaction
->updates
[i
];
3220 if (!(update
->flags
& REF_LOG_ONLY
) &&
3221 (update
->flags
& REF_HAVE_OLD
) &&
3222 !is_null_oid(&update
->old_oid
))
3223 BUG("initial ref transaction with old_sha1 set");
3225 string_list_append(&refnames_to_check
, update
->refname
);
3228 * packed-refs don't support symbolic refs, root refs and reflogs,
3229 * so we have to queue these references via the loose transaction.
3231 if (update
->new_target
||
3232 is_root_ref(update
->refname
) ||
3233 (update
->flags
& REF_LOG_ONLY
)) {
3234 if (!loose_transaction
) {
3235 loose_transaction
= ref_store_transaction_begin(&refs
->base
, 0, err
);
3236 if (!loose_transaction
) {
3237 ret
= REF_TRANSACTION_ERROR_GENERIC
;
3242 if (update
->flags
& REF_LOG_ONLY
)
3243 ref_transaction_add_update(loose_transaction
, update
->refname
,
3244 update
->flags
, &update
->new_oid
,
3245 &update
->old_oid
, NULL
, NULL
,
3246 update
->committer_info
, update
->msg
);
3248 ref_transaction_add_update(loose_transaction
, update
->refname
,
3249 update
->flags
& ~REF_HAVE_OLD
,
3250 update
->new_target
? NULL
: &update
->new_oid
, NULL
,
3251 update
->new_target
, NULL
, update
->committer_info
,
3254 ref_transaction_add_update(packed_transaction
, update
->refname
,
3255 update
->flags
& ~REF_HAVE_OLD
,
3256 &update
->new_oid
, &update
->old_oid
,
3257 NULL
, NULL
, update
->committer_info
, NULL
);
3261 if (packed_refs_lock(refs
->packed_ref_store
, 0, err
)) {
3262 ret
= REF_TRANSACTION_ERROR_GENERIC
;
3266 if (refs_verify_refnames_available(&refs
->base
, &refnames_to_check
,
3267 &affected_refnames
, NULL
, transaction
,
3269 packed_refs_unlock(refs
->packed_ref_store
);
3270 ret
= REF_TRANSACTION_ERROR_NAME_CONFLICT
;
3274 if (ref_transaction_commit(packed_transaction
, err
)) {
3275 ret
= REF_TRANSACTION_ERROR_GENERIC
;
3278 packed_refs_unlock(refs
->packed_ref_store
);
3280 if (loose_transaction
) {
3281 if (ref_transaction_prepare(loose_transaction
, err
) ||
3282 ref_transaction_commit(loose_transaction
, err
)) {
3283 ret
= REF_TRANSACTION_ERROR_GENERIC
;
3289 if (loose_transaction
)
3290 ref_transaction_free(loose_transaction
);
3291 if (packed_transaction
)
3292 ref_transaction_free(packed_transaction
);
3293 transaction
->state
= REF_TRANSACTION_CLOSED
;
3294 string_list_clear(&affected_refnames
, 0);
3295 string_list_clear(&refnames_to_check
, 0);
3299 static int files_transaction_finish(struct ref_store
*ref_store
,
3300 struct ref_transaction
*transaction
,
3303 struct files_ref_store
*refs
=
3304 files_downcast(ref_store
, 0, "ref_transaction_finish");
3307 struct strbuf sb
= STRBUF_INIT
;
3308 struct files_transaction_backend_data
*backend_data
;
3309 struct ref_transaction
*packed_transaction
;
3314 if (transaction
->flags
& REF_TRANSACTION_FLAG_INITIAL
)
3315 return files_transaction_finish_initial(refs
, transaction
, err
);
3316 if (!transaction
->nr
) {
3317 transaction
->state
= REF_TRANSACTION_CLOSED
;
3321 backend_data
= transaction
->backend_data
;
3322 packed_transaction
= backend_data
->packed_transaction
;
3324 /* Perform updates first so live commits remain referenced */
3325 for (i
= 0; i
< transaction
->nr
; i
++) {
3326 struct ref_update
*update
= transaction
->updates
[i
];
3327 struct ref_lock
*lock
= update
->backend_data
;
3329 if (update
->rejection_err
)
3332 if (update
->flags
& REF_NEEDS_COMMIT
||
3333 update
->flags
& REF_LOG_ONLY
) {
3334 if (parse_and_write_reflog(refs
, update
, lock
, err
)) {
3335 ret
= REF_TRANSACTION_ERROR_GENERIC
;
3341 * We try creating a symlink, if that succeeds we continue to the
3342 * next update. If not, we try and create a regular symref.
3344 if (update
->new_target
&& refs
->prefer_symlink_refs
)
3346 * By using the `NOT_CONSTANT()` trick, we can avoid
3347 * errors by `clang`'s `-Wunreachable` logic that would
3348 * report that the `continue` statement is not reachable
3349 * when `NO_SYMLINK_HEAD` is `#define`d.
3351 if (NOT_CONSTANT(!create_ref_symlink(lock
, update
->new_target
)))
3354 if (update
->flags
& REF_NEEDS_COMMIT
) {
3355 clear_loose_ref_cache(refs
);
3356 if (commit_ref(lock
)) {
3357 strbuf_addf(err
, "couldn't set '%s'", lock
->ref_name
);
3359 update
->backend_data
= NULL
;
3360 ret
= REF_TRANSACTION_ERROR_GENERIC
;
3367 * Now that updates are safely completed, we can perform
3368 * deletes. First delete the reflogs of any references that
3369 * will be deleted, since (in the unexpected event of an
3370 * error) leaving a reference without a reflog is less bad
3371 * than leaving a reflog without a reference (the latter is a
3372 * mildly invalid repository state):
3374 for (i
= 0; i
< transaction
->nr
; i
++) {
3375 struct ref_update
*update
= transaction
->updates
[i
];
3377 if (update
->rejection_err
)
3380 if (update
->flags
& REF_DELETING
&&
3381 !(update
->flags
& REF_LOG_ONLY
) &&
3382 !(update
->flags
& REF_IS_PRUNING
)) {
3384 files_reflog_path(refs
, &sb
, update
->refname
);
3385 if (!unlink_or_warn(sb
.buf
))
3386 try_remove_empty_parents(refs
, update
->refname
,
3387 REMOVE_EMPTY_PARENTS_REFLOG
);
3392 * Perform deletes now that updates are safely completed.
3394 * First delete any packed versions of the references, while
3395 * retaining the packed-refs lock:
3397 if (packed_transaction
) {
3398 ret
= ref_transaction_commit(packed_transaction
, err
);
3399 ref_transaction_free(packed_transaction
);
3400 packed_transaction
= NULL
;
3401 backend_data
->packed_transaction
= NULL
;
3406 /* Now delete the loose versions of the references: */
3407 for (i
= 0; i
< transaction
->nr
; i
++) {
3408 struct ref_update
*update
= transaction
->updates
[i
];
3409 struct ref_lock
*lock
= update
->backend_data
;
3411 if (update
->rejection_err
)
3414 if (update
->flags
& REF_DELETING
&&
3415 !(update
->flags
& REF_LOG_ONLY
)) {
3416 update
->flags
|= REF_DELETED_RMDIR
;
3417 if (!(update
->type
& REF_ISPACKED
) ||
3418 update
->type
& REF_ISSYMREF
) {
3419 /* It is a loose reference. */
3421 files_ref_path(refs
, &sb
, lock
->ref_name
);
3422 if (unlink_or_msg(sb
.buf
, err
)) {
3423 ret
= REF_TRANSACTION_ERROR_GENERIC
;
3430 clear_loose_ref_cache(refs
);
3433 files_transaction_cleanup(refs
, transaction
);
3435 for (i
= 0; i
< transaction
->nr
; i
++) {
3436 struct ref_update
*update
= transaction
->updates
[i
];
3438 if (update
->flags
& REF_DELETED_RMDIR
) {
3440 * The reference was deleted. Delete any
3441 * empty parent directories. (Note that this
3442 * can only work because we have already
3443 * removed the lockfile.)
3445 try_remove_empty_parents(refs
, update
->refname
,
3446 REMOVE_EMPTY_PARENTS_REF
);
3450 strbuf_release(&sb
);
3454 static int files_transaction_abort(struct ref_store
*ref_store
,
3455 struct ref_transaction
*transaction
,
3456 struct strbuf
*err UNUSED
)
3458 struct files_ref_store
*refs
=
3459 files_downcast(ref_store
, 0, "ref_transaction_abort");
3461 files_transaction_cleanup(refs
, transaction
);
3465 struct expire_reflog_cb
{
3466 reflog_expiry_should_prune_fn
*should_prune_fn
;
3469 struct object_id last_kept_oid
;
3470 unsigned int rewrite
:1,
3474 static int expire_reflog_ent(const char *refname UNUSED
,
3475 struct object_id
*ooid
, struct object_id
*noid
,
3476 const char *email
, timestamp_t timestamp
, int tz
,
3477 const char *message
, void *cb_data
)
3479 struct expire_reflog_cb
*cb
= cb_data
;
3480 reflog_expiry_should_prune_fn
*fn
= cb
->should_prune_fn
;
3483 ooid
= &cb
->last_kept_oid
;
3485 if (fn(ooid
, noid
, email
, timestamp
, tz
, message
, cb
->policy_cb
))
3489 return 0; /* --dry-run */
3491 fprintf(cb
->newlog
, "%s %s %s %"PRItime
" %+05d\t%s", oid_to_hex(ooid
),
3492 oid_to_hex(noid
), email
, timestamp
, tz
, message
);
3493 oidcpy(&cb
->last_kept_oid
, noid
);
3498 static int files_reflog_expire(struct ref_store
*ref_store
,
3499 const char *refname
,
3500 unsigned int expire_flags
,
3501 reflog_expiry_prepare_fn prepare_fn
,
3502 reflog_expiry_should_prune_fn should_prune_fn
,
3503 reflog_expiry_cleanup_fn cleanup_fn
,
3504 void *policy_cb_data
)
3506 struct files_ref_store
*refs
=
3507 files_downcast(ref_store
, REF_STORE_WRITE
, "reflog_expire");
3508 struct lock_file reflog_lock
= LOCK_INIT
;
3509 struct expire_reflog_cb cb
;
3510 struct ref_lock
*lock
;
3511 struct strbuf log_file_sb
= STRBUF_INIT
;
3514 struct strbuf err
= STRBUF_INIT
;
3515 const struct object_id
*oid
;
3517 memset(&cb
, 0, sizeof(cb
));
3518 cb
.rewrite
= !!(expire_flags
& EXPIRE_REFLOGS_REWRITE
);
3519 cb
.dry_run
= !!(expire_flags
& EXPIRE_REFLOGS_DRY_RUN
);
3520 cb
.policy_cb
= policy_cb_data
;
3521 cb
.should_prune_fn
= should_prune_fn
;
3524 * The reflog file is locked by holding the lock on the
3525 * reference itself, plus we might need to update the
3526 * reference if --updateref was specified:
3528 lock
= lock_ref_oid_basic(refs
, refname
, &err
);
3530 error("cannot lock ref '%s': %s", refname
, err
.buf
);
3531 strbuf_release(&err
);
3534 oid
= &lock
->old_oid
;
3537 * When refs are deleted, their reflog is deleted before the
3538 * ref itself is deleted. This is because there is no separate
3539 * lock for reflog; instead we take a lock on the ref with
3540 * lock_ref_oid_basic().
3542 * If a race happens and the reflog doesn't exist after we've
3543 * acquired the lock that's OK. We've got nothing more to do;
3544 * We were asked to delete the reflog, but someone else
3545 * deleted it! The caller doesn't care that we deleted it,
3546 * just that it is deleted. So we can return successfully.
3548 if (!refs_reflog_exists(ref_store
, refname
)) {
3553 files_reflog_path(refs
, &log_file_sb
, refname
);
3554 log_file
= strbuf_detach(&log_file_sb
, NULL
);
3557 * Even though holding $GIT_DIR/logs/$reflog.lock has
3558 * no locking implications, we use the lock_file
3559 * machinery here anyway because it does a lot of the
3560 * work we need, including cleaning up if the program
3561 * exits unexpectedly.
3563 if (hold_lock_file_for_update(&reflog_lock
, log_file
, 0) < 0) {
3564 struct strbuf err
= STRBUF_INIT
;
3565 unable_to_lock_message(log_file
, errno
, &err
);
3566 error("%s", err
.buf
);
3567 strbuf_release(&err
);
3570 cb
.newlog
= fdopen_lock_file(&reflog_lock
, "w");
3572 error("cannot fdopen %s (%s)",
3573 get_lock_file_path(&reflog_lock
), strerror(errno
));
3578 (*prepare_fn
)(refname
, oid
, cb
.policy_cb
);
3579 refs_for_each_reflog_ent(ref_store
, refname
, expire_reflog_ent
, &cb
);
3580 (*cleanup_fn
)(cb
.policy_cb
);
3584 * It doesn't make sense to adjust a reference pointed
3585 * to by a symbolic ref based on expiring entries in
3586 * the symbolic reference's reflog. Nor can we update
3587 * a reference if there are no remaining reflog
3592 if ((expire_flags
& EXPIRE_REFLOGS_UPDATE_REF
) &&
3593 !is_null_oid(&cb
.last_kept_oid
)) {
3597 ref
= refs_resolve_ref_unsafe(&refs
->base
, refname
,
3598 RESOLVE_REF_NO_RECURSE
,
3600 update
= !!(ref
&& !(type
& REF_ISSYMREF
));
3603 if (close_lock_file_gently(&reflog_lock
)) {
3604 status
|= error("couldn't write %s: %s", log_file
,
3606 rollback_lock_file(&reflog_lock
);
3607 } else if (update
&&
3608 (write_in_full(get_lock_file_fd(&lock
->lk
),
3609 oid_to_hex(&cb
.last_kept_oid
), refs
->base
.repo
->hash_algo
->hexsz
) < 0 ||
3610 write_str_in_full(get_lock_file_fd(&lock
->lk
), "\n") < 0 ||
3611 close_ref_gently(lock
) < 0)) {
3612 status
|= error("couldn't write %s",
3613 get_lock_file_path(&lock
->lk
));
3614 rollback_lock_file(&reflog_lock
);
3615 } else if (commit_lock_file(&reflog_lock
)) {
3616 status
|= error("unable to write reflog '%s' (%s)",
3617 log_file
, strerror(errno
));
3618 } else if (update
&& commit_ref(lock
)) {
3619 status
|= error("couldn't set %s", lock
->ref_name
);
3627 rollback_lock_file(&reflog_lock
);
3633 static int files_ref_store_create_on_disk(struct ref_store
*ref_store
,
3635 struct strbuf
*err UNUSED
)
3637 struct files_ref_store
*refs
=
3638 files_downcast(ref_store
, REF_STORE_WRITE
, "create");
3639 struct strbuf sb
= STRBUF_INIT
;
3642 * We need to create a "refs" dir in any case so that older versions of
3643 * Git can tell that this is a repository. This serves two main purposes:
3645 * - Clients will know to stop walking the parent-directory chain when
3646 * detecting the Git repository. Otherwise they may end up detecting
3647 * a Git repository in a parent directory instead.
3649 * - Instead of failing to detect a repository with unknown reference
3650 * format altogether, old clients will print an error saying that
3651 * they do not understand the reference format extension.
3653 strbuf_addf(&sb
, "%s/refs", ref_store
->gitdir
);
3654 safe_create_dir(the_repository
, sb
.buf
, 1);
3655 adjust_shared_perm(the_repository
, sb
.buf
);
3658 * There is no need to create directories for common refs when creating
3659 * a worktree ref store.
3661 if (!(flags
& REF_STORE_CREATE_ON_DISK_IS_WORKTREE
)) {
3663 * Create .git/refs/{heads,tags}
3666 files_ref_path(refs
, &sb
, "refs/heads");
3667 safe_create_dir(the_repository
, sb
.buf
, 1);
3670 files_ref_path(refs
, &sb
, "refs/tags");
3671 safe_create_dir(the_repository
, sb
.buf
, 1);
3674 strbuf_release(&sb
);
3678 struct remove_one_root_ref_data
{
3683 static int remove_one_root_ref(const char *refname
,
3686 struct remove_one_root_ref_data
*data
= cb_data
;
3687 struct strbuf buf
= STRBUF_INIT
;
3690 strbuf_addf(&buf
, "%s/%s", data
->gitdir
, refname
);
3692 ret
= unlink(buf
.buf
);
3694 strbuf_addf(data
->err
, "could not delete %s: %s\n",
3695 refname
, strerror(errno
));
3697 strbuf_release(&buf
);
3701 static int files_ref_store_remove_on_disk(struct ref_store
*ref_store
,
3704 struct files_ref_store
*refs
=
3705 files_downcast(ref_store
, REF_STORE_WRITE
, "remove");
3706 struct remove_one_root_ref_data data
= {
3707 .gitdir
= refs
->base
.gitdir
,
3710 struct strbuf sb
= STRBUF_INIT
;
3713 strbuf_addf(&sb
, "%s/refs", refs
->base
.gitdir
);
3714 if (remove_dir_recursively(&sb
, 0) < 0) {
3715 strbuf_addf(err
, "could not delete refs: %s",
3721 strbuf_addf(&sb
, "%s/logs", refs
->base
.gitdir
);
3722 if (remove_dir_recursively(&sb
, 0) < 0) {
3723 strbuf_addf(err
, "could not delete logs: %s",
3729 if (for_each_root_ref(refs
, remove_one_root_ref
, &data
) < 0)
3732 if (ref_store_remove_on_disk(refs
->packed_ref_store
, err
) < 0)
3735 strbuf_release(&sb
);
3740 * For refs and reflogs, they share a unified interface when scanning
3741 * the whole directory. This function is used as the callback for each
3742 * regular file or symlink in the directory.
3744 typedef int (*files_fsck_refs_fn
)(struct ref_store
*ref_store
,
3745 struct fsck_options
*o
,
3746 const char *refname
,
3747 struct dir_iterator
*iter
);
3749 static int files_fsck_symref_target(struct fsck_options
*o
,
3750 struct fsck_ref_report
*report
,
3751 struct strbuf
*referent
,
3752 unsigned int symbolic_link
)
3754 int is_referent_root
;
3755 char orig_last_byte
;
3759 orig_len
= referent
->len
;
3760 orig_last_byte
= referent
->buf
[orig_len
- 1];
3762 strbuf_rtrim(referent
);
3764 is_referent_root
= is_root_ref(referent
->buf
);
3765 if (!is_referent_root
&&
3766 !starts_with(referent
->buf
, "refs/") &&
3767 !starts_with(referent
->buf
, "worktrees/")) {
3768 ret
= fsck_report_ref(o
, report
,
3769 FSCK_MSG_SYMREF_TARGET_IS_NOT_A_REF
,
3770 "points to non-ref target '%s'", referent
->buf
);
3774 if (!is_referent_root
&& check_refname_format(referent
->buf
, 0)) {
3775 ret
= fsck_report_ref(o
, report
,
3776 FSCK_MSG_BAD_REFERENT_NAME
,
3777 "points to invalid refname '%s'", referent
->buf
);
3784 if (referent
->len
== orig_len
||
3785 (referent
->len
< orig_len
&& orig_last_byte
!= '\n')) {
3786 ret
= fsck_report_ref(o
, report
,
3787 FSCK_MSG_REF_MISSING_NEWLINE
,
3788 "misses LF at the end");
3791 if (referent
->len
!= orig_len
&& referent
->len
!= orig_len
- 1) {
3792 ret
= fsck_report_ref(o
, report
,
3793 FSCK_MSG_TRAILING_REF_CONTENT
,
3794 "has trailing whitespaces or newlines");
3801 static int files_fsck_refs_content(struct ref_store
*ref_store
,
3802 struct fsck_options
*o
,
3803 const char *target_name
,
3804 struct dir_iterator
*iter
)
3806 struct strbuf ref_content
= STRBUF_INIT
;
3807 struct strbuf abs_gitdir
= STRBUF_INIT
;
3808 struct strbuf referent
= STRBUF_INIT
;
3809 struct fsck_ref_report report
= { 0 };
3810 const char *trailing
= NULL
;
3811 unsigned int type
= 0;
3812 int failure_errno
= 0;
3813 struct object_id oid
;
3816 report
.path
= target_name
;
3818 if (S_ISLNK(iter
->st
.st_mode
)) {
3819 const char *relative_referent_path
= NULL
;
3821 ret
= fsck_report_ref(o
, &report
,
3822 FSCK_MSG_SYMLINK_REF
,
3823 "use deprecated symbolic link for symref");
3825 strbuf_add_absolute_path(&abs_gitdir
, ref_store
->repo
->gitdir
);
3826 strbuf_normalize_path(&abs_gitdir
);
3827 if (!is_dir_sep(abs_gitdir
.buf
[abs_gitdir
.len
- 1]))
3828 strbuf_addch(&abs_gitdir
, '/');
3830 strbuf_add_real_path(&ref_content
, iter
->path
.buf
);
3831 skip_prefix(ref_content
.buf
, abs_gitdir
.buf
,
3832 &relative_referent_path
);
3834 if (relative_referent_path
)
3835 strbuf_addstr(&referent
, relative_referent_path
);
3837 strbuf_addbuf(&referent
, &ref_content
);
3839 ret
|= files_fsck_symref_target(o
, &report
, &referent
, 1);
3843 if (strbuf_read_file(&ref_content
, iter
->path
.buf
, 0) < 0) {
3845 * Ref file could be removed by another concurrent process. We should
3846 * ignore this error and continue to the next ref.
3848 if (errno
== ENOENT
)
3851 ret
= error_errno(_("cannot read ref file '%s'"), iter
->path
.buf
);
3855 if (parse_loose_ref_contents(ref_store
->repo
->hash_algo
,
3856 ref_content
.buf
, &oid
, &referent
,
3857 &type
, &trailing
, &failure_errno
)) {
3858 strbuf_rtrim(&ref_content
);
3859 ret
= fsck_report_ref(o
, &report
,
3860 FSCK_MSG_BAD_REF_CONTENT
,
3861 "%s", ref_content
.buf
);
3865 if (!(type
& REF_ISSYMREF
)) {
3867 ret
= fsck_report_ref(o
, &report
,
3868 FSCK_MSG_REF_MISSING_NEWLINE
,
3869 "misses LF at the end");
3872 if (*trailing
!= '\n' || *(trailing
+ 1)) {
3873 ret
= fsck_report_ref(o
, &report
,
3874 FSCK_MSG_TRAILING_REF_CONTENT
,
3875 "has trailing garbage: '%s'", trailing
);
3879 ret
= files_fsck_symref_target(o
, &report
, &referent
, 0);
3884 strbuf_release(&ref_content
);
3885 strbuf_release(&referent
);
3886 strbuf_release(&abs_gitdir
);
3890 static int files_fsck_refs_name(struct ref_store
*ref_store UNUSED
,
3891 struct fsck_options
*o
,
3892 const char *refname
,
3893 struct dir_iterator
*iter
)
3895 struct strbuf sb
= STRBUF_INIT
;
3899 * Ignore the files ending with ".lock" as they may be lock files
3900 * However, do not allow bare ".lock" files.
3902 if (iter
->basename
[0] != '.' && ends_with(iter
->basename
, ".lock"))
3906 * This works right now because we never check the root refs.
3908 if (check_refname_format(refname
, 0)) {
3909 struct fsck_ref_report report
= { 0 };
3911 report
.path
= refname
;
3912 ret
= fsck_report_ref(o
, &report
,
3913 FSCK_MSG_BAD_REF_NAME
,
3914 "invalid refname format");
3918 strbuf_release(&sb
);
3922 static int files_fsck_refs_dir(struct ref_store
*ref_store
,
3923 struct fsck_options
*o
,
3924 const char *refs_check_dir
,
3925 struct worktree
*wt
,
3926 files_fsck_refs_fn
*fsck_refs_fn
)
3928 struct strbuf refname
= STRBUF_INIT
;
3929 struct strbuf sb
= STRBUF_INIT
;
3930 struct dir_iterator
*iter
;
3934 strbuf_addf(&sb
, "%s/%s", ref_store
->gitdir
, refs_check_dir
);
3936 iter
= dir_iterator_begin(sb
.buf
, 0);
3938 if (errno
== ENOENT
&& !is_main_worktree(wt
))
3941 ret
= error_errno(_("cannot open directory %s"), sb
.buf
);
3945 while ((iter_status
= dir_iterator_advance(iter
)) == ITER_OK
) {
3946 if (S_ISDIR(iter
->st
.st_mode
)) {
3948 } else if (S_ISREG(iter
->st
.st_mode
) ||
3949 S_ISLNK(iter
->st
.st_mode
)) {
3950 strbuf_reset(&refname
);
3952 if (!is_main_worktree(wt
))
3953 strbuf_addf(&refname
, "worktrees/%s/", wt
->id
);
3954 strbuf_addf(&refname
, "%s/%s", refs_check_dir
,
3955 iter
->relative_path
);
3958 fprintf_ln(stderr
, "Checking %s", refname
.buf
);
3960 for (size_t i
= 0; fsck_refs_fn
[i
]; i
++) {
3961 if (fsck_refs_fn
[i
](ref_store
, o
, refname
.buf
, iter
))
3965 struct fsck_ref_report report
= { .path
= iter
->basename
};
3966 if (fsck_report_ref(o
, &report
,
3967 FSCK_MSG_BAD_REF_FILETYPE
,
3968 "unexpected file type"))
3973 if (iter_status
!= ITER_DONE
)
3974 ret
= error(_("failed to iterate over '%s'"), sb
.buf
);
3977 dir_iterator_free(iter
);
3978 strbuf_release(&sb
);
3979 strbuf_release(&refname
);
3983 static int files_fsck_refs(struct ref_store
*ref_store
,
3984 struct fsck_options
*o
,
3985 struct worktree
*wt
)
3987 files_fsck_refs_fn fsck_refs_fn
[]= {
3988 files_fsck_refs_name
,
3989 files_fsck_refs_content
,
3993 return files_fsck_refs_dir(ref_store
, o
, "refs", wt
, fsck_refs_fn
);
3996 static int files_fsck(struct ref_store
*ref_store
,
3997 struct fsck_options
*o
,
3998 struct worktree
*wt
)
4000 struct files_ref_store
*refs
=
4001 files_downcast(ref_store
, REF_STORE_READ
, "fsck");
4003 return files_fsck_refs(ref_store
, o
, wt
) |
4004 refs
->packed_ref_store
->be
->fsck(refs
->packed_ref_store
, o
, wt
);
4007 struct ref_storage_be refs_be_files
= {
4009 .init
= files_ref_store_init
,
4010 .release
= files_ref_store_release
,
4011 .create_on_disk
= files_ref_store_create_on_disk
,
4012 .remove_on_disk
= files_ref_store_remove_on_disk
,
4014 .transaction_prepare
= files_transaction_prepare
,
4015 .transaction_finish
= files_transaction_finish
,
4016 .transaction_abort
= files_transaction_abort
,
4018 .pack_refs
= files_pack_refs
,
4019 .optimize
= files_optimize
,
4020 .rename_ref
= files_rename_ref
,
4021 .copy_ref
= files_copy_ref
,
4023 .iterator_begin
= files_ref_iterator_begin
,
4024 .read_raw_ref
= files_read_raw_ref
,
4025 .read_symbolic_ref
= files_read_symbolic_ref
,
4027 .reflog_iterator_begin
= files_reflog_iterator_begin
,
4028 .for_each_reflog_ent
= files_for_each_reflog_ent
,
4029 .for_each_reflog_ent_reverse
= files_for_each_reflog_ent_reverse
,
4030 .reflog_exists
= files_reflog_exists
,
4031 .create_reflog
= files_create_reflog
,
4032 .delete_reflog
= files_delete_reflog
,
4033 .reflog_expire
= files_reflog_expire
,