1 #include "../git-compat-util.h"
4 #include "../environment.h"
5 #include "../gettext.h"
9 #include "refs-internal.h"
10 #include "ref-cache.h"
11 #include "packed-backend.h"
13 #include "../iterator.h"
14 #include "../dir-iterator.h"
15 #include "../lockfile.h"
16 #include "../object.h"
17 #include "../object-file.h"
20 #include "../chdir-notify.h"
22 #include "../worktree.h"
23 #include "../wrapper.h"
24 #include "../write-or-die.h"
25 #include "../revision.h"
26 #include <wildmatch.h>
29 * This backend uses the following flags in `ref_update::flags` for
30 * internal bookkeeping purposes. Their numerical values must not
31 * conflict with REF_NO_DEREF, REF_FORCE_CREATE_REFLOG, REF_HAVE_NEW,
32 * or REF_HAVE_OLD, which are also stored in `ref_update::flags`.
36 * Used as a flag in ref_update::flags when a loose ref is being
37 * pruned. This flag must only be used when REF_NO_DEREF is set.
39 #define REF_IS_PRUNING (1 << 4)
42 * Flag passed to lock_ref_sha1_basic() telling it to tolerate broken
43 * refs (i.e., because the reference is about to be deleted anyway).
45 #define REF_DELETING (1 << 5)
48 * Used as a flag in ref_update::flags when the lockfile needs to be
51 #define REF_NEEDS_COMMIT (1 << 6)
54 * Used as a flag in ref_update::flags when the ref_update was via an
57 #define REF_UPDATE_VIA_HEAD (1 << 8)
60 * Used as a flag in ref_update::flags when a reference has been
61 * deleted and the ref's parent directories may need cleanup.
63 #define REF_DELETED_RMDIR (1 << 9)
68 struct object_id old_oid
;
71 struct files_ref_store
{
72 struct ref_store base
;
73 unsigned int store_flags
;
77 struct ref_cache
*loose
;
79 struct ref_store
*packed_ref_store
;
82 static void clear_loose_ref_cache(struct files_ref_store
*refs
)
85 free_ref_cache(refs
->loose
);
91 * Create a new submodule ref cache and add it to the internal
94 static struct ref_store
*files_ref_store_create(struct repository
*repo
,
98 struct files_ref_store
*refs
= xcalloc(1, sizeof(*refs
));
99 struct ref_store
*ref_store
= (struct ref_store
*)refs
;
100 struct strbuf sb
= STRBUF_INIT
;
102 base_ref_store_init(ref_store
, repo
, gitdir
, &refs_be_files
);
103 refs
->store_flags
= flags
;
104 get_common_dir_noenv(&sb
, gitdir
);
105 refs
->gitcommondir
= strbuf_detach(&sb
, NULL
);
106 refs
->packed_ref_store
=
107 packed_ref_store_create(repo
, refs
->gitcommondir
, flags
);
109 chdir_notify_reparent("files-backend $GIT_DIR", &refs
->base
.gitdir
);
110 chdir_notify_reparent("files-backend $GIT_COMMONDIR",
111 &refs
->gitcommondir
);
117 * Die if refs is not the main ref store. caller is used in any
118 * necessary error messages.
120 static void files_assert_main_repository(struct files_ref_store
*refs
,
123 if (refs
->store_flags
& REF_STORE_MAIN
)
126 BUG("operation %s only allowed for main ref store", caller
);
130 * Downcast ref_store to files_ref_store. Die if ref_store is not a
131 * files_ref_store. required_flags is compared with ref_store's
132 * store_flags to ensure the ref_store has all required capabilities.
133 * "caller" is used in any necessary error messages.
135 static struct files_ref_store
*files_downcast(struct ref_store
*ref_store
,
136 unsigned int required_flags
,
139 struct files_ref_store
*refs
;
141 if (ref_store
->be
!= &refs_be_files
)
142 BUG("ref_store is type \"%s\" not \"files\" in %s",
143 ref_store
->be
->name
, caller
);
145 refs
= (struct files_ref_store
*)ref_store
;
147 if ((refs
->store_flags
& required_flags
) != required_flags
)
148 BUG("operation %s requires abilities 0x%x, but only have 0x%x",
149 caller
, required_flags
, refs
->store_flags
);
154 static void files_reflog_path(struct files_ref_store
*refs
,
158 const char *bare_refname
;
161 enum ref_worktree_type wt_type
= parse_worktree_ref(
162 refname
, &wtname
, &wtname_len
, &bare_refname
);
165 case REF_WORKTREE_CURRENT
:
166 strbuf_addf(sb
, "%s/logs/%s", refs
->base
.gitdir
, refname
);
168 case REF_WORKTREE_SHARED
:
169 case REF_WORKTREE_MAIN
:
170 strbuf_addf(sb
, "%s/logs/%s", refs
->gitcommondir
, bare_refname
);
172 case REF_WORKTREE_OTHER
:
173 strbuf_addf(sb
, "%s/worktrees/%.*s/logs/%s", refs
->gitcommondir
,
174 wtname_len
, wtname
, bare_refname
);
177 BUG("unknown ref type %d of ref %s", wt_type
, refname
);
181 static void files_ref_path(struct files_ref_store
*refs
,
185 const char *bare_refname
;
188 enum ref_worktree_type wt_type
= parse_worktree_ref(
189 refname
, &wtname
, &wtname_len
, &bare_refname
);
191 case REF_WORKTREE_CURRENT
:
192 strbuf_addf(sb
, "%s/%s", refs
->base
.gitdir
, refname
);
194 case REF_WORKTREE_OTHER
:
195 strbuf_addf(sb
, "%s/worktrees/%.*s/%s", refs
->gitcommondir
,
196 wtname_len
, wtname
, bare_refname
);
198 case REF_WORKTREE_SHARED
:
199 case REF_WORKTREE_MAIN
:
200 strbuf_addf(sb
, "%s/%s", refs
->gitcommondir
, bare_refname
);
203 BUG("unknown ref type %d of ref %s", wt_type
, refname
);
208 * Manually add refs/bisect, refs/rewritten and refs/worktree, which, being
209 * per-worktree, might not appear in the directory listing for
210 * refs/ in the main repo.
212 static void add_per_worktree_entries_to_dir(struct ref_dir
*dir
, const char *dirname
)
214 const char *prefixes
[] = { "refs/bisect/", "refs/worktree/", "refs/rewritten/" };
217 if (strcmp(dirname
, "refs/"))
220 for (ip
= 0; ip
< ARRAY_SIZE(prefixes
); ip
++) {
221 const char *prefix
= prefixes
[ip
];
222 int prefix_len
= strlen(prefix
);
223 struct ref_entry
*child_entry
;
226 pos
= search_ref_dir(dir
, prefix
, prefix_len
);
229 child_entry
= create_dir_entry(dir
->cache
, prefix
, prefix_len
);
230 add_entry_to_dir(dir
, child_entry
);
235 * Read the loose references from the namespace dirname into dir
236 * (without recursing). dirname must end with '/'. dir must be the
237 * directory entry corresponding to dirname.
239 static void loose_fill_ref_dir(struct ref_store
*ref_store
,
240 struct ref_dir
*dir
, const char *dirname
)
242 struct files_ref_store
*refs
=
243 files_downcast(ref_store
, REF_STORE_READ
, "fill_ref_dir");
246 int dirnamelen
= strlen(dirname
);
247 struct strbuf refname
;
248 struct strbuf path
= STRBUF_INIT
;
250 files_ref_path(refs
, &path
, dirname
);
252 d
= opendir(path
.buf
);
254 strbuf_release(&path
);
258 strbuf_init(&refname
, dirnamelen
+ 257);
259 strbuf_add(&refname
, dirname
, dirnamelen
);
261 while ((de
= readdir(d
)) != NULL
) {
262 struct object_id oid
;
266 if (de
->d_name
[0] == '.')
268 if (ends_with(de
->d_name
, ".lock"))
270 strbuf_addstr(&refname
, de
->d_name
);
272 dtype
= get_dtype(de
, &path
, 1);
273 if (dtype
== DT_DIR
) {
274 strbuf_addch(&refname
, '/');
275 add_entry_to_dir(dir
,
276 create_dir_entry(dir
->cache
, refname
.buf
,
278 } else if (dtype
== DT_REG
) {
279 if (!refs_resolve_ref_unsafe(&refs
->base
,
284 flag
|= REF_ISBROKEN
;
285 } else if (is_null_oid(&oid
)) {
287 * It is so astronomically unlikely
288 * that null_oid is the OID of an
289 * actual object that we consider its
290 * appearance in a loose reference
291 * file to be repo corruption
292 * (probably due to a software bug).
294 flag
|= REF_ISBROKEN
;
297 if (check_refname_format(refname
.buf
,
298 REFNAME_ALLOW_ONELEVEL
)) {
299 if (!refname_is_safe(refname
.buf
))
300 die("loose refname is dangerous: %s", refname
.buf
);
302 flag
|= REF_BAD_NAME
| REF_ISBROKEN
;
304 add_entry_to_dir(dir
,
305 create_ref_entry(refname
.buf
, &oid
, flag
));
307 strbuf_setlen(&refname
, dirnamelen
);
309 strbuf_release(&refname
);
310 strbuf_release(&path
);
313 add_per_worktree_entries_to_dir(dir
, dirname
);
316 static struct ref_cache
*get_loose_ref_cache(struct files_ref_store
*refs
)
320 * Mark the top-level directory complete because we
321 * are about to read the only subdirectory that can
324 refs
->loose
= create_ref_cache(&refs
->base
, loose_fill_ref_dir
);
326 /* We're going to fill the top level ourselves: */
327 refs
->loose
->root
->flag
&= ~REF_INCOMPLETE
;
330 * Add an incomplete entry for "refs/" (to be filled
333 add_entry_to_dir(get_ref_dir(refs
->loose
->root
),
334 create_dir_entry(refs
->loose
, "refs/", 5));
339 static int read_ref_internal(struct ref_store
*ref_store
, const char *refname
,
340 struct object_id
*oid
, struct strbuf
*referent
,
341 unsigned int *type
, int *failure_errno
, int skip_packed_refs
)
343 struct files_ref_store
*refs
=
344 files_downcast(ref_store
, REF_STORE_READ
, "read_raw_ref");
345 struct strbuf sb_contents
= STRBUF_INIT
;
346 struct strbuf sb_path
= STRBUF_INIT
;
352 int remaining_retries
= 3;
356 strbuf_reset(&sb_path
);
358 files_ref_path(refs
, &sb_path
, refname
);
364 * We might have to loop back here to avoid a race
365 * condition: first we lstat() the file, then we try
366 * to read it as a link or as a file. But if somebody
367 * changes the type of the file (file <-> directory
368 * <-> symlink) between the lstat() and reading, then
369 * we don't want to report that as an error but rather
370 * try again starting with the lstat().
372 * We'll keep a count of the retries, though, just to avoid
373 * any confusing situation sending us into an infinite loop.
376 if (remaining_retries
-- <= 0)
379 if (lstat(path
, &st
) < 0) {
382 if (myerr
!= ENOENT
|| skip_packed_refs
)
384 if (refs_read_raw_ref(refs
->packed_ref_store
, refname
, oid
,
385 referent
, type
, &ignore_errno
)) {
393 /* Follow "normalized" - ie "refs/.." symlinks by hand */
394 if (S_ISLNK(st
.st_mode
)) {
395 strbuf_reset(&sb_contents
);
396 if (strbuf_readlink(&sb_contents
, path
, st
.st_size
) < 0) {
398 if (myerr
== ENOENT
|| myerr
== EINVAL
)
399 /* inconsistent with lstat; retry */
404 if (starts_with(sb_contents
.buf
, "refs/") &&
405 !check_refname_format(sb_contents
.buf
, 0)) {
406 strbuf_swap(&sb_contents
, referent
);
407 *type
|= REF_ISSYMREF
;
412 * It doesn't look like a refname; fall through to just
413 * treating it like a non-symlink, and reading whatever it
418 /* Is it a directory? */
419 if (S_ISDIR(st
.st_mode
)) {
422 * Even though there is a directory where the loose
423 * ref is supposed to be, there could still be a
426 if (skip_packed_refs
||
427 refs_read_raw_ref(refs
->packed_ref_store
, refname
, oid
,
428 referent
, type
, &ignore_errno
)) {
437 * Anything else, just open it and try to use it as
440 fd
= open(path
, O_RDONLY
);
443 if (myerr
== ENOENT
&& !S_ISLNK(st
.st_mode
))
444 /* inconsistent with lstat; retry */
449 strbuf_reset(&sb_contents
);
450 if (strbuf_read(&sb_contents
, fd
, 256) < 0) {
456 strbuf_rtrim(&sb_contents
);
457 buf
= sb_contents
.buf
;
459 ret
= parse_loose_ref_contents(buf
, oid
, referent
, type
, &myerr
);
463 BUG("returning non-zero %d, should have set myerr!", ret
);
464 *failure_errno
= myerr
;
466 strbuf_release(&sb_path
);
467 strbuf_release(&sb_contents
);
472 static int files_read_raw_ref(struct ref_store
*ref_store
, const char *refname
,
473 struct object_id
*oid
, struct strbuf
*referent
,
474 unsigned int *type
, int *failure_errno
)
476 return read_ref_internal(ref_store
, refname
, oid
, referent
, type
, failure_errno
, 0);
479 static int files_read_symbolic_ref(struct ref_store
*ref_store
, const char *refname
,
480 struct strbuf
*referent
)
482 struct object_id oid
;
483 int failure_errno
, ret
;
486 ret
= read_ref_internal(ref_store
, refname
, &oid
, referent
, &type
, &failure_errno
, 1);
490 return !(type
& REF_ISSYMREF
);
493 int parse_loose_ref_contents(const char *buf
, struct object_id
*oid
,
494 struct strbuf
*referent
, unsigned int *type
,
498 if (skip_prefix(buf
, "ref:", &buf
)) {
499 while (isspace(*buf
))
502 strbuf_reset(referent
);
503 strbuf_addstr(referent
, buf
);
504 *type
|= REF_ISSYMREF
;
509 * FETCH_HEAD has additional data after the sha.
511 if (parse_oid_hex(buf
, oid
, &p
) ||
512 (*p
!= '\0' && !isspace(*p
))) {
513 *type
|= REF_ISBROKEN
;
514 *failure_errno
= EINVAL
;
520 static void unlock_ref(struct ref_lock
*lock
)
522 rollback_lock_file(&lock
->lk
);
523 free(lock
->ref_name
);
528 * Lock refname, without following symrefs, and set *lock_p to point
529 * at a newly-allocated lock object. Fill in lock->old_oid, referent,
530 * and type similarly to read_raw_ref().
532 * The caller must verify that refname is a "safe" reference name (in
533 * the sense of refname_is_safe()) before calling this function.
535 * If the reference doesn't already exist, verify that refname doesn't
536 * have a D/F conflict with any existing references. extras and skip
537 * are passed to refs_verify_refname_available() for this check.
539 * If mustexist is not set and the reference is not found or is
540 * broken, lock the reference anyway but clear old_oid.
542 * Return 0 on success. On failure, write an error message to err and
543 * return TRANSACTION_NAME_CONFLICT or TRANSACTION_GENERIC_ERROR.
545 * Implementation note: This function is basically
550 * but it includes a lot more code to
551 * - Deal with possible races with other processes
552 * - Avoid calling refs_verify_refname_available() when it can be
553 * avoided, namely if we were successfully able to read the ref
554 * - Generate informative error messages in the case of failure
556 static int lock_raw_ref(struct files_ref_store
*refs
,
557 const char *refname
, int mustexist
,
558 const struct string_list
*extras
,
559 struct ref_lock
**lock_p
,
560 struct strbuf
*referent
,
564 struct ref_lock
*lock
;
565 struct strbuf ref_file
= STRBUF_INIT
;
566 int attempts_remaining
= 3;
567 int ret
= TRANSACTION_GENERIC_ERROR
;
571 files_assert_main_repository(refs
, "lock_raw_ref");
575 /* First lock the file so it can't change out from under us. */
577 *lock_p
= CALLOC_ARRAY(lock
, 1);
579 lock
->ref_name
= xstrdup(refname
);
580 files_ref_path(refs
, &ref_file
, refname
);
583 switch (safe_create_leading_directories(ref_file
.buf
)) {
588 * Suppose refname is "refs/foo/bar". We just failed
589 * to create the containing directory, "refs/foo",
590 * because there was a non-directory in the way. This
591 * indicates a D/F conflict, probably because of
592 * another reference such as "refs/foo". There is no
593 * reason to expect this error to be transitory.
595 if (refs_verify_refname_available(&refs
->base
, refname
,
596 extras
, NULL
, err
)) {
599 * To the user the relevant error is
600 * that the "mustexist" reference is
604 strbuf_addf(err
, "unable to resolve reference '%s'",
608 * The error message set by
609 * refs_verify_refname_available() is
612 ret
= TRANSACTION_NAME_CONFLICT
;
616 * The file that is in the way isn't a loose
617 * reference. Report it as a low-level
620 strbuf_addf(err
, "unable to create lock file %s.lock; "
621 "non-directory in the way",
626 /* Maybe another process was tidying up. Try again. */
627 if (--attempts_remaining
> 0)
631 strbuf_addf(err
, "unable to create directory for %s",
636 if (hold_lock_file_for_update_timeout(
637 &lock
->lk
, ref_file
.buf
, LOCK_NO_DEREF
,
638 get_files_ref_lock_timeout_ms()) < 0) {
641 if (myerr
== ENOENT
&& --attempts_remaining
> 0) {
643 * Maybe somebody just deleted one of the
644 * directories leading to ref_file. Try
649 unable_to_lock_message(ref_file
.buf
, myerr
, err
);
655 * Now we hold the lock and can read the reference without
656 * fear that its value will change.
659 if (files_read_raw_ref(&refs
->base
, refname
, &lock
->old_oid
, referent
,
660 type
, &failure_errno
)) {
661 if (failure_errno
== ENOENT
) {
663 /* Garden variety missing reference. */
664 strbuf_addf(err
, "unable to resolve reference '%s'",
669 * Reference is missing, but that's OK. We
670 * know that there is not a conflict with
671 * another loose reference because
672 * (supposing that we are trying to lock
673 * reference "refs/foo/bar"):
675 * - We were successfully able to create
676 * the lockfile refs/foo/bar.lock, so we
677 * know there cannot be a loose reference
680 * - We got ENOENT and not EISDIR, so we
681 * know that there cannot be a loose
682 * reference named "refs/foo/bar/baz".
685 } else if (failure_errno
== EISDIR
) {
687 * There is a directory in the way. It might have
688 * contained references that have been deleted. If
689 * we don't require that the reference already
690 * exists, try to remove the directory so that it
691 * doesn't cause trouble when we want to rename the
692 * lockfile into place later.
695 /* Garden variety missing reference. */
696 strbuf_addf(err
, "unable to resolve reference '%s'",
699 } else if (remove_dir_recursively(&ref_file
,
700 REMOVE_DIR_EMPTY_ONLY
)) {
701 if (refs_verify_refname_available(
702 &refs
->base
, refname
,
703 extras
, NULL
, err
)) {
705 * The error message set by
706 * verify_refname_available() is OK.
708 ret
= TRANSACTION_NAME_CONFLICT
;
712 * We can't delete the directory,
713 * but we also don't know of any
714 * references that it should
717 strbuf_addf(err
, "there is a non-empty directory '%s' "
718 "blocking reference '%s'",
719 ref_file
.buf
, refname
);
723 } else if (failure_errno
== EINVAL
&& (*type
& REF_ISBROKEN
)) {
724 strbuf_addf(err
, "unable to resolve reference '%s': "
725 "reference broken", refname
);
728 strbuf_addf(err
, "unable to resolve reference '%s': %s",
729 refname
, strerror(failure_errno
));
734 * If the ref did not exist and we are creating it,
735 * make sure there is no existing packed ref that
736 * conflicts with refname:
738 if (refs_verify_refname_available(
739 refs
->packed_ref_store
, refname
,
752 strbuf_release(&ref_file
);
756 struct files_ref_iterator
{
757 struct ref_iterator base
;
759 struct ref_iterator
*iter0
;
760 struct repository
*repo
;
764 static int files_ref_iterator_advance(struct ref_iterator
*ref_iterator
)
766 struct files_ref_iterator
*iter
=
767 (struct files_ref_iterator
*)ref_iterator
;
770 while ((ok
= ref_iterator_advance(iter
->iter0
)) == ITER_OK
) {
771 if (iter
->flags
& DO_FOR_EACH_PER_WORKTREE_ONLY
&&
772 parse_worktree_ref(iter
->iter0
->refname
, NULL
, NULL
,
773 NULL
) != REF_WORKTREE_CURRENT
)
776 if ((iter
->flags
& DO_FOR_EACH_OMIT_DANGLING_SYMREFS
) &&
777 (iter
->iter0
->flags
& REF_ISSYMREF
) &&
778 (iter
->iter0
->flags
& REF_ISBROKEN
))
781 if (!(iter
->flags
& DO_FOR_EACH_INCLUDE_BROKEN
) &&
782 !ref_resolves_to_object(iter
->iter0
->refname
,
788 iter
->base
.refname
= iter
->iter0
->refname
;
789 iter
->base
.oid
= iter
->iter0
->oid
;
790 iter
->base
.flags
= iter
->iter0
->flags
;
795 if (ref_iterator_abort(ref_iterator
) != ITER_DONE
)
801 static int files_ref_iterator_peel(struct ref_iterator
*ref_iterator
,
802 struct object_id
*peeled
)
804 struct files_ref_iterator
*iter
=
805 (struct files_ref_iterator
*)ref_iterator
;
807 return ref_iterator_peel(iter
->iter0
, peeled
);
810 static int files_ref_iterator_abort(struct ref_iterator
*ref_iterator
)
812 struct files_ref_iterator
*iter
=
813 (struct files_ref_iterator
*)ref_iterator
;
817 ok
= ref_iterator_abort(iter
->iter0
);
819 base_ref_iterator_free(ref_iterator
);
823 static struct ref_iterator_vtable files_ref_iterator_vtable
= {
824 .advance
= files_ref_iterator_advance
,
825 .peel
= files_ref_iterator_peel
,
826 .abort
= files_ref_iterator_abort
,
829 static struct ref_iterator
*files_ref_iterator_begin(
830 struct ref_store
*ref_store
,
831 const char *prefix
, const char **exclude_patterns
,
834 struct files_ref_store
*refs
;
835 struct ref_iterator
*loose_iter
, *packed_iter
, *overlay_iter
;
836 struct files_ref_iterator
*iter
;
837 struct ref_iterator
*ref_iterator
;
838 unsigned int required_flags
= REF_STORE_READ
;
840 if (!(flags
& DO_FOR_EACH_INCLUDE_BROKEN
))
841 required_flags
|= REF_STORE_ODB
;
843 refs
= files_downcast(ref_store
, required_flags
, "ref_iterator_begin");
846 * We must make sure that all loose refs are read before
847 * accessing the packed-refs file; this avoids a race
848 * condition if loose refs are migrated to the packed-refs
849 * file by a simultaneous process, but our in-memory view is
850 * from before the migration. We ensure this as follows:
851 * First, we call start the loose refs iteration with its
852 * `prime_ref` argument set to true. This causes the loose
853 * references in the subtree to be pre-read into the cache.
854 * (If they've already been read, that's OK; we only need to
855 * guarantee that they're read before the packed refs, not
856 * *how much* before.) After that, we call
857 * packed_ref_iterator_begin(), which internally checks
858 * whether the packed-ref cache is up to date with what is on
859 * disk, and re-reads it if not.
862 loose_iter
= cache_ref_iterator_begin(get_loose_ref_cache(refs
),
863 prefix
, ref_store
->repo
, 1);
866 * The packed-refs file might contain broken references, for
867 * example an old version of a reference that points at an
868 * object that has since been garbage-collected. This is OK as
869 * long as there is a corresponding loose reference that
870 * overrides it, and we don't want to emit an error message in
871 * this case. So ask the packed_ref_store for all of its
872 * references, and (if needed) do our own check for broken
873 * ones in files_ref_iterator_advance(), after we have merged
874 * the packed and loose references.
876 packed_iter
= refs_ref_iterator_begin(
877 refs
->packed_ref_store
, prefix
, exclude_patterns
, 0,
878 DO_FOR_EACH_INCLUDE_BROKEN
);
880 overlay_iter
= overlay_ref_iterator_begin(loose_iter
, packed_iter
);
882 CALLOC_ARRAY(iter
, 1);
883 ref_iterator
= &iter
->base
;
884 base_ref_iterator_init(ref_iterator
, &files_ref_iterator_vtable
,
885 overlay_iter
->ordered
);
886 iter
->iter0
= overlay_iter
;
887 iter
->repo
= ref_store
->repo
;
894 * Callback function for raceproof_create_file(). This function is
895 * expected to do something that makes dirname(path) permanent despite
896 * the fact that other processes might be cleaning up empty
897 * directories at the same time. Usually it will create a file named
898 * path, but alternatively it could create another file in that
899 * directory, or even chdir() into that directory. The function should
900 * return 0 if the action was completed successfully. On error, it
901 * should return a nonzero result and set errno.
902 * raceproof_create_file() treats two errno values specially:
904 * - ENOENT -- dirname(path) does not exist. In this case,
905 * raceproof_create_file() tries creating dirname(path)
906 * (and any parent directories, if necessary) and calls
907 * the function again.
909 * - EISDIR -- the file already exists and is a directory. In this
910 * case, raceproof_create_file() removes the directory if
911 * it is empty (and recursively any empty directories that
912 * it contains) and calls the function again.
914 * Any other errno causes raceproof_create_file() to fail with the
915 * callback's return value and errno.
917 * Obviously, this function should be OK with being called again if it
918 * fails with ENOENT or EISDIR. In other scenarios it will not be
921 typedef int create_file_fn(const char *path
, void *cb
);
924 * Create a file in dirname(path) by calling fn, creating leading
925 * directories if necessary. Retry a few times in case we are racing
926 * with another process that is trying to clean up the directory that
927 * contains path. See the documentation for create_file_fn for more
930 * Return the value and set the errno that resulted from the most
931 * recent call of fn. fn is always called at least once, and will be
932 * called more than once if it returns ENOENT or EISDIR.
934 static int raceproof_create_file(const char *path
, create_file_fn fn
, void *cb
)
937 * The number of times we will try to remove empty directories
938 * in the way of path. This is only 1 because if another
939 * process is racily creating directories that conflict with
940 * us, we don't want to fight against them.
942 int remove_directories_remaining
= 1;
945 * The number of times that we will try to create the
946 * directories containing path. We are willing to attempt this
947 * more than once, because another process could be trying to
948 * clean up empty directories at the same time as we are
949 * trying to create them.
951 int create_directories_remaining
= 3;
953 /* A scratch copy of path, filled lazily if we need it: */
954 struct strbuf path_copy
= STRBUF_INIT
;
967 if (errno
== EISDIR
&& remove_directories_remaining
-- > 0) {
969 * A directory is in the way. Maybe it is empty; try
973 strbuf_addstr(&path_copy
, path
);
975 if (!remove_dir_recursively(&path_copy
, REMOVE_DIR_EMPTY_ONLY
))
977 } else if (errno
== ENOENT
&& create_directories_remaining
-- > 0) {
979 * Maybe the containing directory didn't exist, or
980 * maybe it was just deleted by a process that is
981 * racing with us to clean up empty directories. Try
984 enum scld_error scld_result
;
987 strbuf_addstr(&path_copy
, path
);
990 scld_result
= safe_create_leading_directories(path_copy
.buf
);
991 if (scld_result
== SCLD_OK
)
993 } while (scld_result
== SCLD_VANISHED
&& create_directories_remaining
-- > 0);
997 strbuf_release(&path_copy
);
1002 static int remove_empty_directories(struct strbuf
*path
)
1005 * we want to create a file but there is a directory there;
1006 * if that is an empty directory (or a directory that contains
1007 * only empty directories), remove them.
1009 return remove_dir_recursively(path
, REMOVE_DIR_EMPTY_ONLY
);
1012 static int create_reflock(const char *path
, void *cb
)
1014 struct lock_file
*lk
= cb
;
1016 return hold_lock_file_for_update_timeout(
1017 lk
, path
, LOCK_NO_DEREF
,
1018 get_files_ref_lock_timeout_ms()) < 0 ? -1 : 0;
1022 * Locks a ref returning the lock on success and NULL on failure.
1024 static struct ref_lock
*lock_ref_oid_basic(struct files_ref_store
*refs
,
1025 const char *refname
,
1028 struct strbuf ref_file
= STRBUF_INIT
;
1029 struct ref_lock
*lock
;
1031 files_assert_main_repository(refs
, "lock_ref_oid_basic");
1034 CALLOC_ARRAY(lock
, 1);
1036 files_ref_path(refs
, &ref_file
, refname
);
1039 * If the ref did not exist and we are creating it, make sure
1040 * there is no existing packed ref whose name begins with our
1041 * refname, nor a packed ref whose name is a proper prefix of
1044 if (is_null_oid(&lock
->old_oid
) &&
1045 refs_verify_refname_available(refs
->packed_ref_store
, refname
,
1049 lock
->ref_name
= xstrdup(refname
);
1051 if (raceproof_create_file(ref_file
.buf
, create_reflock
, &lock
->lk
)) {
1052 unable_to_lock_message(ref_file
.buf
, errno
, err
);
1056 if (!refs_resolve_ref_unsafe(&refs
->base
, lock
->ref_name
, 0,
1057 &lock
->old_oid
, NULL
))
1058 oidclr(&lock
->old_oid
);
1066 strbuf_release(&ref_file
);
1070 struct ref_to_prune
{
1071 struct ref_to_prune
*next
;
1072 struct object_id oid
;
1073 char name
[FLEX_ARRAY
];
1077 REMOVE_EMPTY_PARENTS_REF
= 0x01,
1078 REMOVE_EMPTY_PARENTS_REFLOG
= 0x02
1082 * Remove empty parent directories associated with the specified
1083 * reference and/or its reflog, but spare [logs/]refs/ and immediate
1084 * subdirs. flags is a combination of REMOVE_EMPTY_PARENTS_REF and/or
1085 * REMOVE_EMPTY_PARENTS_REFLOG.
1087 static void try_remove_empty_parents(struct files_ref_store
*refs
,
1088 const char *refname
,
1091 struct strbuf buf
= STRBUF_INIT
;
1092 struct strbuf sb
= STRBUF_INIT
;
1096 strbuf_addstr(&buf
, refname
);
1098 for (i
= 0; i
< 2; i
++) { /* refs/{heads,tags,...}/ */
1099 while (*p
&& *p
!= '/')
1101 /* tolerate duplicate slashes; see check_refname_format() */
1105 q
= buf
.buf
+ buf
.len
;
1106 while (flags
& (REMOVE_EMPTY_PARENTS_REF
| REMOVE_EMPTY_PARENTS_REFLOG
)) {
1107 while (q
> p
&& *q
!= '/')
1109 while (q
> p
&& *(q
-1) == '/')
1113 strbuf_setlen(&buf
, q
- buf
.buf
);
1116 files_ref_path(refs
, &sb
, buf
.buf
);
1117 if ((flags
& REMOVE_EMPTY_PARENTS_REF
) && rmdir(sb
.buf
))
1118 flags
&= ~REMOVE_EMPTY_PARENTS_REF
;
1121 files_reflog_path(refs
, &sb
, buf
.buf
);
1122 if ((flags
& REMOVE_EMPTY_PARENTS_REFLOG
) && rmdir(sb
.buf
))
1123 flags
&= ~REMOVE_EMPTY_PARENTS_REFLOG
;
1125 strbuf_release(&buf
);
1126 strbuf_release(&sb
);
1129 /* make sure nobody touched the ref, and unlink */
1130 static void prune_ref(struct files_ref_store
*refs
, struct ref_to_prune
*r
)
1132 struct ref_transaction
*transaction
;
1133 struct strbuf err
= STRBUF_INIT
;
1136 if (check_refname_format(r
->name
, 0))
1139 transaction
= ref_store_transaction_begin(&refs
->base
, &err
);
1142 ref_transaction_add_update(
1143 transaction
, r
->name
,
1144 REF_NO_DEREF
| REF_HAVE_NEW
| REF_HAVE_OLD
| REF_IS_PRUNING
,
1145 null_oid(), &r
->oid
, NULL
);
1146 if (ref_transaction_commit(transaction
, &err
))
1153 error("%s", err
.buf
);
1154 strbuf_release(&err
);
1155 ref_transaction_free(transaction
);
1160 * Prune the loose versions of the references in the linked list
1161 * `*refs_to_prune`, freeing the entries in the list as we go.
1163 static void prune_refs(struct files_ref_store
*refs
, struct ref_to_prune
**refs_to_prune
)
1165 while (*refs_to_prune
) {
1166 struct ref_to_prune
*r
= *refs_to_prune
;
1167 *refs_to_prune
= r
->next
;
1174 * Return true if the specified reference should be packed.
1176 static int should_pack_ref(const char *refname
,
1177 const struct object_id
*oid
, unsigned int ref_flags
,
1178 struct pack_refs_opts
*opts
)
1180 struct string_list_item
*item
;
1182 /* Do not pack per-worktree refs: */
1183 if (parse_worktree_ref(refname
, NULL
, NULL
, NULL
) !=
1184 REF_WORKTREE_SHARED
)
1187 /* Do not pack symbolic refs: */
1188 if (ref_flags
& REF_ISSYMREF
)
1191 /* Do not pack broken refs: */
1192 if (!ref_resolves_to_object(refname
, the_repository
, oid
, ref_flags
))
1195 if (ref_excluded(opts
->exclusions
, refname
))
1198 for_each_string_list_item(item
, opts
->includes
)
1199 if (!wildmatch(item
->string
, refname
, 0))
1205 static int files_pack_refs(struct ref_store
*ref_store
,
1206 struct pack_refs_opts
*opts
)
1208 struct files_ref_store
*refs
=
1209 files_downcast(ref_store
, REF_STORE_WRITE
| REF_STORE_ODB
,
1211 struct ref_iterator
*iter
;
1213 struct ref_to_prune
*refs_to_prune
= NULL
;
1214 struct strbuf err
= STRBUF_INIT
;
1215 struct ref_transaction
*transaction
;
1217 transaction
= ref_store_transaction_begin(refs
->packed_ref_store
, &err
);
1221 packed_refs_lock(refs
->packed_ref_store
, LOCK_DIE_ON_ERROR
, &err
);
1223 iter
= cache_ref_iterator_begin(get_loose_ref_cache(refs
), NULL
,
1225 while ((ok
= ref_iterator_advance(iter
)) == ITER_OK
) {
1227 * If the loose reference can be packed, add an entry
1228 * in the packed ref cache. If the reference should be
1229 * pruned, also add it to refs_to_prune.
1231 if (!should_pack_ref(iter
->refname
, iter
->oid
, iter
->flags
, opts
))
1235 * Add a reference creation for this reference to the
1236 * packed-refs transaction:
1238 if (ref_transaction_update(transaction
, iter
->refname
,
1240 REF_NO_DEREF
, NULL
, &err
))
1241 die("failure preparing to create packed reference %s: %s",
1242 iter
->refname
, err
.buf
);
1244 /* Schedule the loose reference for pruning if requested. */
1245 if ((opts
->flags
& PACK_REFS_PRUNE
)) {
1246 struct ref_to_prune
*n
;
1247 FLEX_ALLOC_STR(n
, name
, iter
->refname
);
1248 oidcpy(&n
->oid
, iter
->oid
);
1249 n
->next
= refs_to_prune
;
1253 if (ok
!= ITER_DONE
)
1254 die("error while iterating over references");
1256 if (ref_transaction_commit(transaction
, &err
))
1257 die("unable to write new packed-refs: %s", err
.buf
);
1259 ref_transaction_free(transaction
);
1261 packed_refs_unlock(refs
->packed_ref_store
);
1263 prune_refs(refs
, &refs_to_prune
);
1264 strbuf_release(&err
);
1268 static int files_delete_refs(struct ref_store
*ref_store
, const char *msg
,
1269 struct string_list
*refnames
, unsigned int flags
)
1271 struct files_ref_store
*refs
=
1272 files_downcast(ref_store
, REF_STORE_WRITE
, "delete_refs");
1273 struct strbuf err
= STRBUF_INIT
;
1279 if (packed_refs_lock(refs
->packed_ref_store
, 0, &err
))
1282 if (refs_delete_refs(refs
->packed_ref_store
, msg
, refnames
, flags
)) {
1283 packed_refs_unlock(refs
->packed_ref_store
);
1287 packed_refs_unlock(refs
->packed_ref_store
);
1289 for (i
= 0; i
< refnames
->nr
; i
++) {
1290 const char *refname
= refnames
->items
[i
].string
;
1292 if (refs_delete_ref(&refs
->base
, msg
, refname
, NULL
, flags
))
1293 result
|= error(_("could not remove reference %s"), refname
);
1296 strbuf_release(&err
);
1301 * If we failed to rewrite the packed-refs file, then it is
1302 * unsafe to try to remove loose refs, because doing so might
1303 * expose an obsolete packed value for a reference that might
1304 * even point at an object that has been garbage collected.
1306 if (refnames
->nr
== 1)
1307 error(_("could not delete reference %s: %s"),
1308 refnames
->items
[0].string
, err
.buf
);
1310 error(_("could not delete references: %s"), err
.buf
);
1312 strbuf_release(&err
);
1317 * People using contrib's git-new-workdir have .git/logs/refs ->
1318 * /some/other/path/.git/logs/refs, and that may live on another device.
1320 * IOW, to avoid cross device rename errors, the temporary renamed log must
1321 * live into logs/refs.
1323 #define TMP_RENAMED_LOG "refs/.tmp-renamed-log"
1326 const char *tmp_renamed_log
;
1330 static int rename_tmp_log_callback(const char *path
, void *cb_data
)
1332 struct rename_cb
*cb
= cb_data
;
1334 if (rename(cb
->tmp_renamed_log
, path
)) {
1336 * rename(a, b) when b is an existing directory ought
1337 * to result in ISDIR, but Solaris 5.8 gives ENOTDIR.
1338 * Sheesh. Record the true errno for error reporting,
1339 * but report EISDIR to raceproof_create_file() so
1340 * that it knows to retry.
1342 cb
->true_errno
= errno
;
1343 if (errno
== ENOTDIR
)
1351 static int rename_tmp_log(struct files_ref_store
*refs
, const char *newrefname
)
1353 struct strbuf path
= STRBUF_INIT
;
1354 struct strbuf tmp
= STRBUF_INIT
;
1355 struct rename_cb cb
;
1358 files_reflog_path(refs
, &path
, newrefname
);
1359 files_reflog_path(refs
, &tmp
, TMP_RENAMED_LOG
);
1360 cb
.tmp_renamed_log
= tmp
.buf
;
1361 ret
= raceproof_create_file(path
.buf
, rename_tmp_log_callback
, &cb
);
1363 if (errno
== EISDIR
)
1364 error("directory not empty: %s", path
.buf
);
1366 error("unable to move logfile %s to %s: %s",
1368 strerror(cb
.true_errno
));
1371 strbuf_release(&path
);
1372 strbuf_release(&tmp
);
1376 static int write_ref_to_lockfile(struct ref_lock
*lock
,
1377 const struct object_id
*oid
,
1378 int skip_oid_verification
, struct strbuf
*err
);
1379 static int commit_ref_update(struct files_ref_store
*refs
,
1380 struct ref_lock
*lock
,
1381 const struct object_id
*oid
, const char *logmsg
,
1382 struct strbuf
*err
);
1385 * Emit a better error message than lockfile.c's
1386 * unable_to_lock_message() would in case there is a D/F conflict with
1387 * another existing reference. If there would be a conflict, emit an error
1388 * message and return false; otherwise, return true.
1390 * Note that this function is not safe against all races with other
1391 * processes, and that's not its job. We'll emit a more verbose error on D/f
1392 * conflicts if we get past it into lock_ref_oid_basic().
1394 static int refs_rename_ref_available(struct ref_store
*refs
,
1395 const char *old_refname
,
1396 const char *new_refname
)
1398 struct string_list skip
= STRING_LIST_INIT_NODUP
;
1399 struct strbuf err
= STRBUF_INIT
;
1402 string_list_insert(&skip
, old_refname
);
1403 ok
= !refs_verify_refname_available(refs
, new_refname
,
1406 error("%s", err
.buf
);
1408 string_list_clear(&skip
, 0);
1409 strbuf_release(&err
);
1413 static int files_copy_or_rename_ref(struct ref_store
*ref_store
,
1414 const char *oldrefname
, const char *newrefname
,
1415 const char *logmsg
, int copy
)
1417 struct files_ref_store
*refs
=
1418 files_downcast(ref_store
, REF_STORE_WRITE
, "rename_ref");
1419 struct object_id orig_oid
;
1420 int flag
= 0, logmoved
= 0;
1421 struct ref_lock
*lock
;
1422 struct stat loginfo
;
1423 struct strbuf sb_oldref
= STRBUF_INIT
;
1424 struct strbuf sb_newref
= STRBUF_INIT
;
1425 struct strbuf tmp_renamed_log
= STRBUF_INIT
;
1427 struct strbuf err
= STRBUF_INIT
;
1429 files_reflog_path(refs
, &sb_oldref
, oldrefname
);
1430 files_reflog_path(refs
, &sb_newref
, newrefname
);
1431 files_reflog_path(refs
, &tmp_renamed_log
, TMP_RENAMED_LOG
);
1433 log
= !lstat(sb_oldref
.buf
, &loginfo
);
1434 if (log
&& S_ISLNK(loginfo
.st_mode
)) {
1435 ret
= error("reflog for %s is a symlink", oldrefname
);
1439 if (!refs_resolve_ref_unsafe(&refs
->base
, oldrefname
,
1440 RESOLVE_REF_READING
| RESOLVE_REF_NO_RECURSE
,
1441 &orig_oid
, &flag
)) {
1442 ret
= error("refname %s not found", oldrefname
);
1446 if (flag
& REF_ISSYMREF
) {
1448 ret
= error("refname %s is a symbolic ref, copying it is not supported",
1451 ret
= error("refname %s is a symbolic ref, renaming it is not supported",
1455 if (!refs_rename_ref_available(&refs
->base
, oldrefname
, newrefname
)) {
1460 if (!copy
&& log
&& rename(sb_oldref
.buf
, tmp_renamed_log
.buf
)) {
1461 ret
= error("unable to move logfile logs/%s to logs/"TMP_RENAMED_LOG
": %s",
1462 oldrefname
, strerror(errno
));
1466 if (copy
&& log
&& copy_file(tmp_renamed_log
.buf
, sb_oldref
.buf
, 0644)) {
1467 ret
= error("unable to copy logfile logs/%s to logs/"TMP_RENAMED_LOG
": %s",
1468 oldrefname
, strerror(errno
));
1472 if (!copy
&& refs_delete_ref(&refs
->base
, logmsg
, oldrefname
,
1473 &orig_oid
, REF_NO_DEREF
)) {
1474 error("unable to delete old %s", oldrefname
);
1479 * Since we are doing a shallow lookup, oid is not the
1480 * correct value to pass to delete_ref as old_oid. But that
1481 * doesn't matter, because an old_oid check wouldn't add to
1482 * the safety anyway; we want to delete the reference whatever
1483 * its current value.
1485 if (!copy
&& refs_resolve_ref_unsafe(&refs
->base
, newrefname
,
1486 RESOLVE_REF_READING
| RESOLVE_REF_NO_RECURSE
,
1488 refs_delete_ref(&refs
->base
, NULL
, newrefname
,
1489 NULL
, REF_NO_DEREF
)) {
1490 if (errno
== EISDIR
) {
1491 struct strbuf path
= STRBUF_INIT
;
1494 files_ref_path(refs
, &path
, newrefname
);
1495 result
= remove_empty_directories(&path
);
1496 strbuf_release(&path
);
1499 error("Directory not empty: %s", newrefname
);
1503 error("unable to delete existing %s", newrefname
);
1508 if (log
&& rename_tmp_log(refs
, newrefname
))
1513 lock
= lock_ref_oid_basic(refs
, newrefname
, &err
);
1516 error("unable to copy '%s' to '%s': %s", oldrefname
, newrefname
, err
.buf
);
1518 error("unable to rename '%s' to '%s': %s", oldrefname
, newrefname
, err
.buf
);
1519 strbuf_release(&err
);
1522 oidcpy(&lock
->old_oid
, &orig_oid
);
1524 if (write_ref_to_lockfile(lock
, &orig_oid
, 0, &err
) ||
1525 commit_ref_update(refs
, lock
, &orig_oid
, logmsg
, &err
)) {
1526 error("unable to write current sha1 into %s: %s", newrefname
, err
.buf
);
1527 strbuf_release(&err
);
1535 lock
= lock_ref_oid_basic(refs
, oldrefname
, &err
);
1537 error("unable to lock %s for rollback: %s", oldrefname
, err
.buf
);
1538 strbuf_release(&err
);
1542 flag
= log_all_ref_updates
;
1543 log_all_ref_updates
= LOG_REFS_NONE
;
1544 if (write_ref_to_lockfile(lock
, &orig_oid
, 0, &err
) ||
1545 commit_ref_update(refs
, lock
, &orig_oid
, NULL
, &err
)) {
1546 error("unable to write current sha1 into %s: %s", oldrefname
, err
.buf
);
1547 strbuf_release(&err
);
1549 log_all_ref_updates
= flag
;
1552 if (logmoved
&& rename(sb_newref
.buf
, sb_oldref
.buf
))
1553 error("unable to restore logfile %s from %s: %s",
1554 oldrefname
, newrefname
, strerror(errno
));
1555 if (!logmoved
&& log
&&
1556 rename(tmp_renamed_log
.buf
, sb_oldref
.buf
))
1557 error("unable to restore logfile %s from logs/"TMP_RENAMED_LOG
": %s",
1558 oldrefname
, strerror(errno
));
1561 strbuf_release(&sb_newref
);
1562 strbuf_release(&sb_oldref
);
1563 strbuf_release(&tmp_renamed_log
);
1568 static int files_rename_ref(struct ref_store
*ref_store
,
1569 const char *oldrefname
, const char *newrefname
,
1572 return files_copy_or_rename_ref(ref_store
, oldrefname
,
1573 newrefname
, logmsg
, 0);
1576 static int files_copy_ref(struct ref_store
*ref_store
,
1577 const char *oldrefname
, const char *newrefname
,
1580 return files_copy_or_rename_ref(ref_store
, oldrefname
,
1581 newrefname
, logmsg
, 1);
1584 static int close_ref_gently(struct ref_lock
*lock
)
1586 if (close_lock_file_gently(&lock
->lk
))
1591 static int commit_ref(struct ref_lock
*lock
)
1593 char *path
= get_locked_file_path(&lock
->lk
);
1596 if (!lstat(path
, &st
) && S_ISDIR(st
.st_mode
)) {
1598 * There is a directory at the path we want to rename
1599 * the lockfile to. Hopefully it is empty; try to
1602 size_t len
= strlen(path
);
1603 struct strbuf sb_path
= STRBUF_INIT
;
1605 strbuf_attach(&sb_path
, path
, len
, len
);
1608 * If this fails, commit_lock_file() will also fail
1609 * and will report the problem.
1611 remove_empty_directories(&sb_path
);
1612 strbuf_release(&sb_path
);
1617 if (commit_lock_file(&lock
->lk
))
1622 static int open_or_create_logfile(const char *path
, void *cb
)
1626 *fd
= open(path
, O_APPEND
| O_WRONLY
| O_CREAT
, 0666);
1627 return (*fd
< 0) ? -1 : 0;
1631 * Create a reflog for a ref. If force_create = 0, only create the
1632 * reflog for certain refs (those for which should_autocreate_reflog
1633 * returns non-zero). Otherwise, create it regardless of the reference
1634 * name. If the logfile already existed or was created, return 0 and
1635 * set *logfd to the file descriptor opened for appending to the file.
1636 * If no logfile exists and we decided not to create one, return 0 and
1637 * set *logfd to -1. On failure, fill in *err, set *logfd to -1, and
1640 static int log_ref_setup(struct files_ref_store
*refs
,
1641 const char *refname
, int force_create
,
1642 int *logfd
, struct strbuf
*err
)
1644 struct strbuf logfile_sb
= STRBUF_INIT
;
1647 files_reflog_path(refs
, &logfile_sb
, refname
);
1648 logfile
= strbuf_detach(&logfile_sb
, NULL
);
1650 if (force_create
|| should_autocreate_reflog(refname
)) {
1651 if (raceproof_create_file(logfile
, open_or_create_logfile
, logfd
)) {
1652 if (errno
== ENOENT
)
1653 strbuf_addf(err
, "unable to create directory for '%s': "
1654 "%s", logfile
, strerror(errno
));
1655 else if (errno
== EISDIR
)
1656 strbuf_addf(err
, "there are still logs under '%s'",
1659 strbuf_addf(err
, "unable to append to '%s': %s",
1660 logfile
, strerror(errno
));
1665 *logfd
= open(logfile
, O_APPEND
| O_WRONLY
);
1667 if (errno
== ENOENT
|| errno
== EISDIR
) {
1669 * The logfile doesn't already exist,
1670 * but that is not an error; it only
1671 * means that we won't write log
1676 strbuf_addf(err
, "unable to append to '%s': %s",
1677 logfile
, strerror(errno
));
1684 adjust_shared_perm(logfile
);
1694 static int files_create_reflog(struct ref_store
*ref_store
, const char *refname
,
1697 struct files_ref_store
*refs
=
1698 files_downcast(ref_store
, REF_STORE_WRITE
, "create_reflog");
1701 if (log_ref_setup(refs
, refname
, 1, &fd
, err
))
1710 static int log_ref_write_fd(int fd
, const struct object_id
*old_oid
,
1711 const struct object_id
*new_oid
,
1712 const char *committer
, const char *msg
)
1714 struct strbuf sb
= STRBUF_INIT
;
1717 strbuf_addf(&sb
, "%s %s %s", oid_to_hex(old_oid
), oid_to_hex(new_oid
), committer
);
1719 strbuf_addch(&sb
, '\t');
1720 strbuf_addstr(&sb
, msg
);
1722 strbuf_addch(&sb
, '\n');
1723 if (write_in_full(fd
, sb
.buf
, sb
.len
) < 0)
1725 strbuf_release(&sb
);
1729 static int files_log_ref_write(struct files_ref_store
*refs
,
1730 const char *refname
, const struct object_id
*old_oid
,
1731 const struct object_id
*new_oid
, const char *msg
,
1732 int flags
, struct strbuf
*err
)
1736 if (log_all_ref_updates
== LOG_REFS_UNSET
)
1737 log_all_ref_updates
= is_bare_repository() ? LOG_REFS_NONE
: LOG_REFS_NORMAL
;
1739 result
= log_ref_setup(refs
, refname
,
1740 flags
& REF_FORCE_CREATE_REFLOG
,
1748 result
= log_ref_write_fd(logfd
, old_oid
, new_oid
,
1749 git_committer_info(0), msg
);
1751 struct strbuf sb
= STRBUF_INIT
;
1752 int save_errno
= errno
;
1754 files_reflog_path(refs
, &sb
, refname
);
1755 strbuf_addf(err
, "unable to append to '%s': %s",
1756 sb
.buf
, strerror(save_errno
));
1757 strbuf_release(&sb
);
1762 struct strbuf sb
= STRBUF_INIT
;
1763 int save_errno
= errno
;
1765 files_reflog_path(refs
, &sb
, refname
);
1766 strbuf_addf(err
, "unable to append to '%s': %s",
1767 sb
.buf
, strerror(save_errno
));
1768 strbuf_release(&sb
);
1775 * Write oid into the open lockfile, then close the lockfile. On
1776 * errors, rollback the lockfile, fill in *err and return -1.
1778 static int write_ref_to_lockfile(struct ref_lock
*lock
,
1779 const struct object_id
*oid
,
1780 int skip_oid_verification
, struct strbuf
*err
)
1782 static char term
= '\n';
1786 if (!skip_oid_verification
) {
1787 o
= parse_object(the_repository
, oid
);
1791 "trying to write ref '%s' with nonexistent object %s",
1792 lock
->ref_name
, oid_to_hex(oid
));
1796 if (o
->type
!= OBJ_COMMIT
&& is_branch(lock
->ref_name
)) {
1799 "trying to write non-commit object %s to branch '%s'",
1800 oid_to_hex(oid
), lock
->ref_name
);
1805 fd
= get_lock_file_fd(&lock
->lk
);
1806 if (write_in_full(fd
, oid_to_hex(oid
), the_hash_algo
->hexsz
) < 0 ||
1807 write_in_full(fd
, &term
, 1) < 0 ||
1808 fsync_component(FSYNC_COMPONENT_REFERENCE
, get_lock_file_fd(&lock
->lk
)) < 0 ||
1809 close_ref_gently(lock
) < 0) {
1811 "couldn't write '%s'", get_lock_file_path(&lock
->lk
));
1819 * Commit a change to a loose reference that has already been written
1820 * to the loose reference lockfile. Also update the reflogs if
1821 * necessary, using the specified lockmsg (which can be NULL).
1823 static int commit_ref_update(struct files_ref_store
*refs
,
1824 struct ref_lock
*lock
,
1825 const struct object_id
*oid
, const char *logmsg
,
1828 files_assert_main_repository(refs
, "commit_ref_update");
1830 clear_loose_ref_cache(refs
);
1831 if (files_log_ref_write(refs
, lock
->ref_name
,
1832 &lock
->old_oid
, oid
,
1834 char *old_msg
= strbuf_detach(err
, NULL
);
1835 strbuf_addf(err
, "cannot update the ref '%s': %s",
1836 lock
->ref_name
, old_msg
);
1842 if (strcmp(lock
->ref_name
, "HEAD") != 0) {
1844 * Special hack: If a branch is updated directly and HEAD
1845 * points to it (may happen on the remote side of a push
1846 * for example) then logically the HEAD reflog should be
1848 * A generic solution implies reverse symref information,
1849 * but finding all symrefs pointing to the given branch
1850 * would be rather costly for this rare event (the direct
1851 * update of a branch) to be worth it. So let's cheat and
1852 * check with HEAD only which should cover 99% of all usage
1853 * scenarios (even 100% of the default ones).
1856 const char *head_ref
;
1858 head_ref
= refs_resolve_ref_unsafe(&refs
->base
, "HEAD",
1859 RESOLVE_REF_READING
,
1861 if (head_ref
&& (head_flag
& REF_ISSYMREF
) &&
1862 !strcmp(head_ref
, lock
->ref_name
)) {
1863 struct strbuf log_err
= STRBUF_INIT
;
1864 if (files_log_ref_write(refs
, "HEAD",
1865 &lock
->old_oid
, oid
,
1866 logmsg
, 0, &log_err
)) {
1867 error("%s", log_err
.buf
);
1868 strbuf_release(&log_err
);
1873 if (commit_ref(lock
)) {
1874 strbuf_addf(err
, "couldn't set '%s'", lock
->ref_name
);
1883 static int create_ref_symlink(struct ref_lock
*lock
, const char *target
)
1886 #ifndef NO_SYMLINK_HEAD
1887 char *ref_path
= get_locked_file_path(&lock
->lk
);
1889 ret
= symlink(target
, ref_path
);
1893 fprintf(stderr
, "no symlink - falling back to symbolic ref\n");
1898 static void update_symref_reflog(struct files_ref_store
*refs
,
1899 struct ref_lock
*lock
, const char *refname
,
1900 const char *target
, const char *logmsg
)
1902 struct strbuf err
= STRBUF_INIT
;
1903 struct object_id new_oid
;
1906 refs_resolve_ref_unsafe(&refs
->base
, target
,
1907 RESOLVE_REF_READING
, &new_oid
, NULL
) &&
1908 files_log_ref_write(refs
, refname
, &lock
->old_oid
,
1909 &new_oid
, logmsg
, 0, &err
)) {
1910 error("%s", err
.buf
);
1911 strbuf_release(&err
);
1915 static int create_symref_locked(struct files_ref_store
*refs
,
1916 struct ref_lock
*lock
, const char *refname
,
1917 const char *target
, const char *logmsg
)
1919 if (prefer_symlink_refs
&& !create_ref_symlink(lock
, target
)) {
1920 update_symref_reflog(refs
, lock
, refname
, target
, logmsg
);
1924 if (!fdopen_lock_file(&lock
->lk
, "w"))
1925 return error("unable to fdopen %s: %s",
1926 get_lock_file_path(&lock
->lk
), strerror(errno
));
1928 update_symref_reflog(refs
, lock
, refname
, target
, logmsg
);
1930 /* no error check; commit_ref will check ferror */
1931 fprintf(get_lock_file_fp(&lock
->lk
), "ref: %s\n", target
);
1932 if (commit_ref(lock
) < 0)
1933 return error("unable to write symref for %s: %s", refname
,
1938 static int files_create_symref(struct ref_store
*ref_store
,
1939 const char *refname
, const char *target
,
1942 struct files_ref_store
*refs
=
1943 files_downcast(ref_store
, REF_STORE_WRITE
, "create_symref");
1944 struct strbuf err
= STRBUF_INIT
;
1945 struct ref_lock
*lock
;
1948 lock
= lock_ref_oid_basic(refs
, refname
, &err
);
1950 error("%s", err
.buf
);
1951 strbuf_release(&err
);
1955 ret
= create_symref_locked(refs
, lock
, refname
, target
, logmsg
);
1960 static int files_reflog_exists(struct ref_store
*ref_store
,
1961 const char *refname
)
1963 struct files_ref_store
*refs
=
1964 files_downcast(ref_store
, REF_STORE_READ
, "reflog_exists");
1965 struct strbuf sb
= STRBUF_INIT
;
1969 files_reflog_path(refs
, &sb
, refname
);
1970 ret
= !lstat(sb
.buf
, &st
) && S_ISREG(st
.st_mode
);
1971 strbuf_release(&sb
);
1975 static int files_delete_reflog(struct ref_store
*ref_store
,
1976 const char *refname
)
1978 struct files_ref_store
*refs
=
1979 files_downcast(ref_store
, REF_STORE_WRITE
, "delete_reflog");
1980 struct strbuf sb
= STRBUF_INIT
;
1983 files_reflog_path(refs
, &sb
, refname
);
1984 ret
= remove_path(sb
.buf
);
1985 strbuf_release(&sb
);
1989 static int show_one_reflog_ent(struct strbuf
*sb
, each_reflog_ent_fn fn
, void *cb_data
)
1991 struct object_id ooid
, noid
;
1992 char *email_end
, *message
;
1993 timestamp_t timestamp
;
1995 const char *p
= sb
->buf
;
1997 /* old SP new SP name <email> SP time TAB msg LF */
1998 if (!sb
->len
|| sb
->buf
[sb
->len
- 1] != '\n' ||
1999 parse_oid_hex(p
, &ooid
, &p
) || *p
++ != ' ' ||
2000 parse_oid_hex(p
, &noid
, &p
) || *p
++ != ' ' ||
2001 !(email_end
= strchr(p
, '>')) ||
2002 email_end
[1] != ' ' ||
2003 !(timestamp
= parse_timestamp(email_end
+ 2, &message
, 10)) ||
2004 !message
|| message
[0] != ' ' ||
2005 (message
[1] != '+' && message
[1] != '-') ||
2006 !isdigit(message
[2]) || !isdigit(message
[3]) ||
2007 !isdigit(message
[4]) || !isdigit(message
[5]))
2008 return 0; /* corrupt? */
2009 email_end
[1] = '\0';
2010 tz
= strtol(message
+ 1, NULL
, 10);
2011 if (message
[6] != '\t')
2015 return fn(&ooid
, &noid
, p
, timestamp
, tz
, message
, cb_data
);
2018 static char *find_beginning_of_line(char *bob
, char *scan
)
2020 while (bob
< scan
&& *(--scan
) != '\n')
2021 ; /* keep scanning backwards */
2023 * Return either beginning of the buffer, or LF at the end of
2024 * the previous line.
2029 static int files_for_each_reflog_ent_reverse(struct ref_store
*ref_store
,
2030 const char *refname
,
2031 each_reflog_ent_fn fn
,
2034 struct files_ref_store
*refs
=
2035 files_downcast(ref_store
, REF_STORE_READ
,
2036 "for_each_reflog_ent_reverse");
2037 struct strbuf sb
= STRBUF_INIT
;
2040 int ret
= 0, at_tail
= 1;
2042 files_reflog_path(refs
, &sb
, refname
);
2043 logfp
= fopen(sb
.buf
, "r");
2044 strbuf_release(&sb
);
2048 /* Jump to the end */
2049 if (fseek(logfp
, 0, SEEK_END
) < 0)
2050 ret
= error("cannot seek back reflog for %s: %s",
2051 refname
, strerror(errno
));
2053 while (!ret
&& 0 < pos
) {
2059 /* Fill next block from the end */
2060 cnt
= (sizeof(buf
) < pos
) ? sizeof(buf
) : pos
;
2061 if (fseek(logfp
, pos
- cnt
, SEEK_SET
)) {
2062 ret
= error("cannot seek back reflog for %s: %s",
2063 refname
, strerror(errno
));
2066 nread
= fread(buf
, cnt
, 1, logfp
);
2068 ret
= error("cannot read %d bytes from reflog for %s: %s",
2069 cnt
, refname
, strerror(errno
));
2074 scanp
= endp
= buf
+ cnt
;
2075 if (at_tail
&& scanp
[-1] == '\n')
2076 /* Looking at the final LF at the end of the file */
2080 while (buf
< scanp
) {
2082 * terminating LF of the previous line, or the beginning
2087 bp
= find_beginning_of_line(buf
, scanp
);
2091 * The newline is the end of the previous line,
2092 * so we know we have complete line starting
2093 * at (bp + 1). Prefix it onto any prior data
2094 * we collected for the line and process it.
2096 strbuf_splice(&sb
, 0, 0, bp
+ 1, endp
- (bp
+ 1));
2099 ret
= show_one_reflog_ent(&sb
, fn
, cb_data
);
2105 * We are at the start of the buffer, and the
2106 * start of the file; there is no previous
2107 * line, and we have everything for this one.
2108 * Process it, and we can end the loop.
2110 strbuf_splice(&sb
, 0, 0, buf
, endp
- buf
);
2111 ret
= show_one_reflog_ent(&sb
, fn
, cb_data
);
2118 * We are at the start of the buffer, and there
2119 * is more file to read backwards. Which means
2120 * we are in the middle of a line. Note that we
2121 * may get here even if *bp was a newline; that
2122 * just means we are at the exact end of the
2123 * previous line, rather than some spot in the
2126 * Save away what we have to be combined with
2127 * the data from the next read.
2129 strbuf_splice(&sb
, 0, 0, buf
, endp
- buf
);
2136 BUG("reverse reflog parser had leftover data");
2139 strbuf_release(&sb
);
2143 static int files_for_each_reflog_ent(struct ref_store
*ref_store
,
2144 const char *refname
,
2145 each_reflog_ent_fn fn
, void *cb_data
)
2147 struct files_ref_store
*refs
=
2148 files_downcast(ref_store
, REF_STORE_READ
,
2149 "for_each_reflog_ent");
2151 struct strbuf sb
= STRBUF_INIT
;
2154 files_reflog_path(refs
, &sb
, refname
);
2155 logfp
= fopen(sb
.buf
, "r");
2156 strbuf_release(&sb
);
2160 while (!ret
&& !strbuf_getwholeline(&sb
, logfp
, '\n'))
2161 ret
= show_one_reflog_ent(&sb
, fn
, cb_data
);
2163 strbuf_release(&sb
);
2167 struct files_reflog_iterator
{
2168 struct ref_iterator base
;
2170 struct ref_store
*ref_store
;
2171 struct dir_iterator
*dir_iterator
;
2172 struct object_id oid
;
2175 static int files_reflog_iterator_advance(struct ref_iterator
*ref_iterator
)
2177 struct files_reflog_iterator
*iter
=
2178 (struct files_reflog_iterator
*)ref_iterator
;
2179 struct dir_iterator
*diter
= iter
->dir_iterator
;
2182 while ((ok
= dir_iterator_advance(diter
)) == ITER_OK
) {
2185 if (!S_ISREG(diter
->st
.st_mode
))
2187 if (diter
->basename
[0] == '.')
2189 if (ends_with(diter
->basename
, ".lock"))
2192 if (!refs_resolve_ref_unsafe(iter
->ref_store
,
2193 diter
->relative_path
, 0,
2194 &iter
->oid
, &flags
)) {
2195 error("bad ref for %s", diter
->path
.buf
);
2199 iter
->base
.refname
= diter
->relative_path
;
2200 iter
->base
.oid
= &iter
->oid
;
2201 iter
->base
.flags
= flags
;
2205 iter
->dir_iterator
= NULL
;
2206 if (ref_iterator_abort(ref_iterator
) == ITER_ERROR
)
2211 static int files_reflog_iterator_peel(struct ref_iterator
*ref_iterator UNUSED
,
2212 struct object_id
*peeled UNUSED
)
2214 BUG("ref_iterator_peel() called for reflog_iterator");
2217 static int files_reflog_iterator_abort(struct ref_iterator
*ref_iterator
)
2219 struct files_reflog_iterator
*iter
=
2220 (struct files_reflog_iterator
*)ref_iterator
;
2223 if (iter
->dir_iterator
)
2224 ok
= dir_iterator_abort(iter
->dir_iterator
);
2226 base_ref_iterator_free(ref_iterator
);
2230 static struct ref_iterator_vtable files_reflog_iterator_vtable
= {
2231 .advance
= files_reflog_iterator_advance
,
2232 .peel
= files_reflog_iterator_peel
,
2233 .abort
= files_reflog_iterator_abort
,
2236 static struct ref_iterator
*reflog_iterator_begin(struct ref_store
*ref_store
,
2239 struct dir_iterator
*diter
;
2240 struct files_reflog_iterator
*iter
;
2241 struct ref_iterator
*ref_iterator
;
2242 struct strbuf sb
= STRBUF_INIT
;
2244 strbuf_addf(&sb
, "%s/logs", gitdir
);
2246 diter
= dir_iterator_begin(sb
.buf
, 0);
2248 strbuf_release(&sb
);
2249 return empty_ref_iterator_begin();
2252 CALLOC_ARRAY(iter
, 1);
2253 ref_iterator
= &iter
->base
;
2255 base_ref_iterator_init(ref_iterator
, &files_reflog_iterator_vtable
, 0);
2256 iter
->dir_iterator
= diter
;
2257 iter
->ref_store
= ref_store
;
2258 strbuf_release(&sb
);
2260 return ref_iterator
;
2263 static enum iterator_selection
reflog_iterator_select(
2264 struct ref_iterator
*iter_worktree
,
2265 struct ref_iterator
*iter_common
,
2266 void *cb_data UNUSED
)
2268 if (iter_worktree
) {
2270 * We're a bit loose here. We probably should ignore
2271 * common refs if they are accidentally added as
2272 * per-worktree refs.
2274 return ITER_SELECT_0
;
2275 } else if (iter_common
) {
2276 if (parse_worktree_ref(iter_common
->refname
, NULL
, NULL
,
2277 NULL
) == REF_WORKTREE_SHARED
)
2278 return ITER_SELECT_1
;
2281 * The main ref store may contain main worktree's
2282 * per-worktree refs, which should be ignored
2289 static struct ref_iterator
*files_reflog_iterator_begin(struct ref_store
*ref_store
)
2291 struct files_ref_store
*refs
=
2292 files_downcast(ref_store
, REF_STORE_READ
,
2293 "reflog_iterator_begin");
2295 if (!strcmp(refs
->base
.gitdir
, refs
->gitcommondir
)) {
2296 return reflog_iterator_begin(ref_store
, refs
->gitcommondir
);
2298 return merge_ref_iterator_begin(
2299 0, reflog_iterator_begin(ref_store
, refs
->base
.gitdir
),
2300 reflog_iterator_begin(ref_store
, refs
->gitcommondir
),
2301 reflog_iterator_select
, refs
);
2306 * If update is a direct update of head_ref (the reference pointed to
2307 * by HEAD), then add an extra REF_LOG_ONLY update for HEAD.
2309 static int split_head_update(struct ref_update
*update
,
2310 struct ref_transaction
*transaction
,
2311 const char *head_ref
,
2312 struct string_list
*affected_refnames
,
2315 struct string_list_item
*item
;
2316 struct ref_update
*new_update
;
2318 if ((update
->flags
& REF_LOG_ONLY
) ||
2319 (update
->flags
& REF_IS_PRUNING
) ||
2320 (update
->flags
& REF_UPDATE_VIA_HEAD
))
2323 if (strcmp(update
->refname
, head_ref
))
2327 * First make sure that HEAD is not already in the
2328 * transaction. This check is O(lg N) in the transaction
2329 * size, but it happens at most once per transaction.
2331 if (string_list_has_string(affected_refnames
, "HEAD")) {
2332 /* An entry already existed */
2334 "multiple updates for 'HEAD' (including one "
2335 "via its referent '%s') are not allowed",
2337 return TRANSACTION_NAME_CONFLICT
;
2340 new_update
= ref_transaction_add_update(
2341 transaction
, "HEAD",
2342 update
->flags
| REF_LOG_ONLY
| REF_NO_DEREF
,
2343 &update
->new_oid
, &update
->old_oid
,
2347 * Add "HEAD". This insertion is O(N) in the transaction
2348 * size, but it happens at most once per transaction.
2349 * Add new_update->refname instead of a literal "HEAD".
2351 if (strcmp(new_update
->refname
, "HEAD"))
2352 BUG("%s unexpectedly not 'HEAD'", new_update
->refname
);
2353 item
= string_list_insert(affected_refnames
, new_update
->refname
);
2354 item
->util
= new_update
;
2360 * update is for a symref that points at referent and doesn't have
2361 * REF_NO_DEREF set. Split it into two updates:
2362 * - The original update, but with REF_LOG_ONLY and REF_NO_DEREF set
2363 * - A new, separate update for the referent reference
2364 * Note that the new update will itself be subject to splitting when
2365 * the iteration gets to it.
2367 static int split_symref_update(struct ref_update
*update
,
2368 const char *referent
,
2369 struct ref_transaction
*transaction
,
2370 struct string_list
*affected_refnames
,
2373 struct string_list_item
*item
;
2374 struct ref_update
*new_update
;
2375 unsigned int new_flags
;
2378 * First make sure that referent is not already in the
2379 * transaction. This check is O(lg N) in the transaction
2380 * size, but it happens at most once per symref in a
2383 if (string_list_has_string(affected_refnames
, referent
)) {
2384 /* An entry already exists */
2386 "multiple updates for '%s' (including one "
2387 "via symref '%s') are not allowed",
2388 referent
, update
->refname
);
2389 return TRANSACTION_NAME_CONFLICT
;
2392 new_flags
= update
->flags
;
2393 if (!strcmp(update
->refname
, "HEAD")) {
2395 * Record that the new update came via HEAD, so that
2396 * when we process it, split_head_update() doesn't try
2397 * to add another reflog update for HEAD. Note that
2398 * this bit will be propagated if the new_update
2399 * itself needs to be split.
2401 new_flags
|= REF_UPDATE_VIA_HEAD
;
2404 new_update
= ref_transaction_add_update(
2405 transaction
, referent
, new_flags
,
2406 &update
->new_oid
, &update
->old_oid
,
2409 new_update
->parent_update
= update
;
2412 * Change the symbolic ref update to log only. Also, it
2413 * doesn't need to check its old OID value, as that will be
2414 * done when new_update is processed.
2416 update
->flags
|= REF_LOG_ONLY
| REF_NO_DEREF
;
2417 update
->flags
&= ~REF_HAVE_OLD
;
2420 * Add the referent. This insertion is O(N) in the transaction
2421 * size, but it happens at most once per symref in a
2422 * transaction. Make sure to add new_update->refname, which will
2423 * be valid as long as affected_refnames is in use, and NOT
2424 * referent, which might soon be freed by our caller.
2426 item
= string_list_insert(affected_refnames
, new_update
->refname
);
2428 BUG("%s unexpectedly found in affected_refnames",
2429 new_update
->refname
);
2430 item
->util
= new_update
;
2436 * Return the refname under which update was originally requested.
2438 static const char *original_update_refname(struct ref_update
*update
)
2440 while (update
->parent_update
)
2441 update
= update
->parent_update
;
2443 return update
->refname
;
2447 * Check whether the REF_HAVE_OLD and old_oid values stored in update
2448 * are consistent with oid, which is the reference's current value. If
2449 * everything is OK, return 0; otherwise, write an error message to
2450 * err and return -1.
2452 static int check_old_oid(struct ref_update
*update
, struct object_id
*oid
,
2455 if (!(update
->flags
& REF_HAVE_OLD
) ||
2456 oideq(oid
, &update
->old_oid
))
2459 if (is_null_oid(&update
->old_oid
))
2460 strbuf_addf(err
, "cannot lock ref '%s': "
2461 "reference already exists",
2462 original_update_refname(update
));
2463 else if (is_null_oid(oid
))
2464 strbuf_addf(err
, "cannot lock ref '%s': "
2465 "reference is missing but expected %s",
2466 original_update_refname(update
),
2467 oid_to_hex(&update
->old_oid
));
2469 strbuf_addf(err
, "cannot lock ref '%s': "
2470 "is at %s but expected %s",
2471 original_update_refname(update
),
2473 oid_to_hex(&update
->old_oid
));
2479 * Prepare for carrying out update:
2480 * - Lock the reference referred to by update.
2481 * - Read the reference under lock.
2482 * - Check that its old OID value (if specified) is correct, and in
2483 * any case record it in update->lock->old_oid for later use when
2484 * writing the reflog.
2485 * - If it is a symref update without REF_NO_DEREF, split it up into a
2486 * REF_LOG_ONLY update of the symref and add a separate update for
2487 * the referent to transaction.
2488 * - If it is an update of head_ref, add a corresponding REF_LOG_ONLY
2491 static int lock_ref_for_update(struct files_ref_store
*refs
,
2492 struct ref_update
*update
,
2493 struct ref_transaction
*transaction
,
2494 const char *head_ref
,
2495 struct string_list
*affected_refnames
,
2498 struct strbuf referent
= STRBUF_INIT
;
2499 int mustexist
= (update
->flags
& REF_HAVE_OLD
) &&
2500 !is_null_oid(&update
->old_oid
);
2502 struct ref_lock
*lock
;
2504 files_assert_main_repository(refs
, "lock_ref_for_update");
2506 if ((update
->flags
& REF_HAVE_NEW
) && is_null_oid(&update
->new_oid
))
2507 update
->flags
|= REF_DELETING
;
2510 ret
= split_head_update(update
, transaction
, head_ref
,
2511 affected_refnames
, err
);
2516 ret
= lock_raw_ref(refs
, update
->refname
, mustexist
,
2519 &update
->type
, err
);
2523 reason
= strbuf_detach(err
, NULL
);
2524 strbuf_addf(err
, "cannot lock ref '%s': %s",
2525 original_update_refname(update
), reason
);
2530 update
->backend_data
= lock
;
2532 if (update
->type
& REF_ISSYMREF
) {
2533 if (update
->flags
& REF_NO_DEREF
) {
2535 * We won't be reading the referent as part of
2536 * the transaction, so we have to read it here
2537 * to record and possibly check old_oid:
2539 if (!refs_resolve_ref_unsafe(&refs
->base
,
2541 &lock
->old_oid
, NULL
)) {
2542 if (update
->flags
& REF_HAVE_OLD
) {
2543 strbuf_addf(err
, "cannot lock ref '%s': "
2544 "error reading reference",
2545 original_update_refname(update
));
2546 ret
= TRANSACTION_GENERIC_ERROR
;
2549 } else if (check_old_oid(update
, &lock
->old_oid
, err
)) {
2550 ret
= TRANSACTION_GENERIC_ERROR
;
2555 * Create a new update for the reference this
2556 * symref is pointing at. Also, we will record
2557 * and verify old_oid for this update as part
2558 * of processing the split-off update, so we
2559 * don't have to do it here.
2561 ret
= split_symref_update(update
,
2562 referent
.buf
, transaction
,
2563 affected_refnames
, err
);
2568 struct ref_update
*parent_update
;
2570 if (check_old_oid(update
, &lock
->old_oid
, err
)) {
2571 ret
= TRANSACTION_GENERIC_ERROR
;
2576 * If this update is happening indirectly because of a
2577 * symref update, record the old OID in the parent
2580 for (parent_update
= update
->parent_update
;
2582 parent_update
= parent_update
->parent_update
) {
2583 struct ref_lock
*parent_lock
= parent_update
->backend_data
;
2584 oidcpy(&parent_lock
->old_oid
, &lock
->old_oid
);
2588 if ((update
->flags
& REF_HAVE_NEW
) &&
2589 !(update
->flags
& REF_DELETING
) &&
2590 !(update
->flags
& REF_LOG_ONLY
)) {
2591 if (!(update
->type
& REF_ISSYMREF
) &&
2592 oideq(&lock
->old_oid
, &update
->new_oid
)) {
2594 * The reference already has the desired
2595 * value, so we don't need to write it.
2597 } else if (write_ref_to_lockfile(
2598 lock
, &update
->new_oid
,
2599 update
->flags
& REF_SKIP_OID_VERIFICATION
,
2601 char *write_err
= strbuf_detach(err
, NULL
);
2604 * The lock was freed upon failure of
2605 * write_ref_to_lockfile():
2607 update
->backend_data
= NULL
;
2609 "cannot update ref '%s': %s",
2610 update
->refname
, write_err
);
2612 ret
= TRANSACTION_GENERIC_ERROR
;
2615 update
->flags
|= REF_NEEDS_COMMIT
;
2618 if (!(update
->flags
& REF_NEEDS_COMMIT
)) {
2620 * We didn't call write_ref_to_lockfile(), so
2621 * the lockfile is still open. Close it to
2622 * free up the file descriptor:
2624 if (close_ref_gently(lock
)) {
2625 strbuf_addf(err
, "couldn't close '%s.lock'",
2627 ret
= TRANSACTION_GENERIC_ERROR
;
2633 strbuf_release(&referent
);
2637 struct files_transaction_backend_data
{
2638 struct ref_transaction
*packed_transaction
;
2639 int packed_refs_locked
;
2643 * Unlock any references in `transaction` that are still locked, and
2644 * mark the transaction closed.
2646 static void files_transaction_cleanup(struct files_ref_store
*refs
,
2647 struct ref_transaction
*transaction
)
2650 struct files_transaction_backend_data
*backend_data
=
2651 transaction
->backend_data
;
2652 struct strbuf err
= STRBUF_INIT
;
2654 for (i
= 0; i
< transaction
->nr
; i
++) {
2655 struct ref_update
*update
= transaction
->updates
[i
];
2656 struct ref_lock
*lock
= update
->backend_data
;
2660 update
->backend_data
= NULL
;
2665 if (backend_data
->packed_transaction
&&
2666 ref_transaction_abort(backend_data
->packed_transaction
, &err
)) {
2667 error("error aborting transaction: %s", err
.buf
);
2668 strbuf_release(&err
);
2671 if (backend_data
->packed_refs_locked
)
2672 packed_refs_unlock(refs
->packed_ref_store
);
2677 transaction
->state
= REF_TRANSACTION_CLOSED
;
2680 static int files_transaction_prepare(struct ref_store
*ref_store
,
2681 struct ref_transaction
*transaction
,
2684 struct files_ref_store
*refs
=
2685 files_downcast(ref_store
, REF_STORE_WRITE
,
2686 "ref_transaction_prepare");
2689 struct string_list affected_refnames
= STRING_LIST_INIT_NODUP
;
2690 char *head_ref
= NULL
;
2692 struct files_transaction_backend_data
*backend_data
;
2693 struct ref_transaction
*packed_transaction
= NULL
;
2697 if (!transaction
->nr
)
2700 CALLOC_ARRAY(backend_data
, 1);
2701 transaction
->backend_data
= backend_data
;
2704 * Fail if a refname appears more than once in the
2705 * transaction. (If we end up splitting up any updates using
2706 * split_symref_update() or split_head_update(), those
2707 * functions will check that the new updates don't have the
2708 * same refname as any existing ones.) Also fail if any of the
2709 * updates use REF_IS_PRUNING without REF_NO_DEREF.
2711 for (i
= 0; i
< transaction
->nr
; i
++) {
2712 struct ref_update
*update
= transaction
->updates
[i
];
2713 struct string_list_item
*item
=
2714 string_list_append(&affected_refnames
, update
->refname
);
2716 if ((update
->flags
& REF_IS_PRUNING
) &&
2717 !(update
->flags
& REF_NO_DEREF
))
2718 BUG("REF_IS_PRUNING set without REF_NO_DEREF");
2721 * We store a pointer to update in item->util, but at
2722 * the moment we never use the value of this field
2723 * except to check whether it is non-NULL.
2725 item
->util
= update
;
2727 string_list_sort(&affected_refnames
);
2728 if (ref_update_reject_duplicates(&affected_refnames
, err
)) {
2729 ret
= TRANSACTION_GENERIC_ERROR
;
2734 * Special hack: If a branch is updated directly and HEAD
2735 * points to it (may happen on the remote side of a push
2736 * for example) then logically the HEAD reflog should be
2739 * A generic solution would require reverse symref lookups,
2740 * but finding all symrefs pointing to a given branch would be
2741 * rather costly for this rare event (the direct update of a
2742 * branch) to be worth it. So let's cheat and check with HEAD
2743 * only, which should cover 99% of all usage scenarios (even
2744 * 100% of the default ones).
2746 * So if HEAD is a symbolic reference, then record the name of
2747 * the reference that it points to. If we see an update of
2748 * head_ref within the transaction, then split_head_update()
2749 * arranges for the reflog of HEAD to be updated, too.
2751 head_ref
= refs_resolve_refdup(ref_store
, "HEAD",
2752 RESOLVE_REF_NO_RECURSE
,
2755 if (head_ref
&& !(head_type
& REF_ISSYMREF
)) {
2756 FREE_AND_NULL(head_ref
);
2760 * Acquire all locks, verify old values if provided, check
2761 * that new values are valid, and write new values to the
2762 * lockfiles, ready to be activated. Only keep one lockfile
2763 * open at a time to avoid running out of file descriptors.
2764 * Note that lock_ref_for_update() might append more updates
2765 * to the transaction.
2767 for (i
= 0; i
< transaction
->nr
; i
++) {
2768 struct ref_update
*update
= transaction
->updates
[i
];
2770 ret
= lock_ref_for_update(refs
, update
, transaction
,
2771 head_ref
, &affected_refnames
, err
);
2775 if (update
->flags
& REF_DELETING
&&
2776 !(update
->flags
& REF_LOG_ONLY
) &&
2777 !(update
->flags
& REF_IS_PRUNING
)) {
2779 * This reference has to be deleted from
2780 * packed-refs if it exists there.
2782 if (!packed_transaction
) {
2783 packed_transaction
= ref_store_transaction_begin(
2784 refs
->packed_ref_store
, err
);
2785 if (!packed_transaction
) {
2786 ret
= TRANSACTION_GENERIC_ERROR
;
2790 backend_data
->packed_transaction
=
2794 ref_transaction_add_update(
2795 packed_transaction
, update
->refname
,
2796 REF_HAVE_NEW
| REF_NO_DEREF
,
2797 &update
->new_oid
, NULL
,
2802 if (packed_transaction
) {
2803 if (packed_refs_lock(refs
->packed_ref_store
, 0, err
)) {
2804 ret
= TRANSACTION_GENERIC_ERROR
;
2807 backend_data
->packed_refs_locked
= 1;
2809 if (is_packed_transaction_needed(refs
->packed_ref_store
,
2810 packed_transaction
)) {
2811 ret
= ref_transaction_prepare(packed_transaction
, err
);
2813 * A failure during the prepare step will abort
2814 * itself, but not free. Do that now, and disconnect
2815 * from the files_transaction so it does not try to
2816 * abort us when we hit the cleanup code below.
2819 ref_transaction_free(packed_transaction
);
2820 backend_data
->packed_transaction
= NULL
;
2824 * We can skip rewriting the `packed-refs`
2825 * file. But we do need to leave it locked, so
2826 * that somebody else doesn't pack a reference
2827 * that we are trying to delete.
2829 * We need to disconnect our transaction from
2830 * backend_data, since the abort (whether successful or
2831 * not) will free it.
2833 backend_data
->packed_transaction
= NULL
;
2834 if (ref_transaction_abort(packed_transaction
, err
)) {
2835 ret
= TRANSACTION_GENERIC_ERROR
;
2843 string_list_clear(&affected_refnames
, 0);
2846 files_transaction_cleanup(refs
, transaction
);
2848 transaction
->state
= REF_TRANSACTION_PREPARED
;
2853 static int files_transaction_finish(struct ref_store
*ref_store
,
2854 struct ref_transaction
*transaction
,
2857 struct files_ref_store
*refs
=
2858 files_downcast(ref_store
, 0, "ref_transaction_finish");
2861 struct strbuf sb
= STRBUF_INIT
;
2862 struct files_transaction_backend_data
*backend_data
;
2863 struct ref_transaction
*packed_transaction
;
2868 if (!transaction
->nr
) {
2869 transaction
->state
= REF_TRANSACTION_CLOSED
;
2873 backend_data
= transaction
->backend_data
;
2874 packed_transaction
= backend_data
->packed_transaction
;
2876 /* Perform updates first so live commits remain referenced */
2877 for (i
= 0; i
< transaction
->nr
; i
++) {
2878 struct ref_update
*update
= transaction
->updates
[i
];
2879 struct ref_lock
*lock
= update
->backend_data
;
2881 if (update
->flags
& REF_NEEDS_COMMIT
||
2882 update
->flags
& REF_LOG_ONLY
) {
2883 if (files_log_ref_write(refs
,
2887 update
->msg
, update
->flags
,
2889 char *old_msg
= strbuf_detach(err
, NULL
);
2891 strbuf_addf(err
, "cannot update the ref '%s': %s",
2892 lock
->ref_name
, old_msg
);
2895 update
->backend_data
= NULL
;
2896 ret
= TRANSACTION_GENERIC_ERROR
;
2900 if (update
->flags
& REF_NEEDS_COMMIT
) {
2901 clear_loose_ref_cache(refs
);
2902 if (commit_ref(lock
)) {
2903 strbuf_addf(err
, "couldn't set '%s'", lock
->ref_name
);
2905 update
->backend_data
= NULL
;
2906 ret
= TRANSACTION_GENERIC_ERROR
;
2913 * Now that updates are safely completed, we can perform
2914 * deletes. First delete the reflogs of any references that
2915 * will be deleted, since (in the unexpected event of an
2916 * error) leaving a reference without a reflog is less bad
2917 * than leaving a reflog without a reference (the latter is a
2918 * mildly invalid repository state):
2920 for (i
= 0; i
< transaction
->nr
; i
++) {
2921 struct ref_update
*update
= transaction
->updates
[i
];
2922 if (update
->flags
& REF_DELETING
&&
2923 !(update
->flags
& REF_LOG_ONLY
) &&
2924 !(update
->flags
& REF_IS_PRUNING
)) {
2926 files_reflog_path(refs
, &sb
, update
->refname
);
2927 if (!unlink_or_warn(sb
.buf
))
2928 try_remove_empty_parents(refs
, update
->refname
,
2929 REMOVE_EMPTY_PARENTS_REFLOG
);
2934 * Perform deletes now that updates are safely completed.
2936 * First delete any packed versions of the references, while
2937 * retaining the packed-refs lock:
2939 if (packed_transaction
) {
2940 ret
= ref_transaction_commit(packed_transaction
, err
);
2941 ref_transaction_free(packed_transaction
);
2942 packed_transaction
= NULL
;
2943 backend_data
->packed_transaction
= NULL
;
2948 /* Now delete the loose versions of the references: */
2949 for (i
= 0; i
< transaction
->nr
; i
++) {
2950 struct ref_update
*update
= transaction
->updates
[i
];
2951 struct ref_lock
*lock
= update
->backend_data
;
2953 if (update
->flags
& REF_DELETING
&&
2954 !(update
->flags
& REF_LOG_ONLY
)) {
2955 update
->flags
|= REF_DELETED_RMDIR
;
2956 if (!(update
->type
& REF_ISPACKED
) ||
2957 update
->type
& REF_ISSYMREF
) {
2958 /* It is a loose reference. */
2960 files_ref_path(refs
, &sb
, lock
->ref_name
);
2961 if (unlink_or_msg(sb
.buf
, err
)) {
2962 ret
= TRANSACTION_GENERIC_ERROR
;
2969 clear_loose_ref_cache(refs
);
2972 files_transaction_cleanup(refs
, transaction
);
2974 for (i
= 0; i
< transaction
->nr
; i
++) {
2975 struct ref_update
*update
= transaction
->updates
[i
];
2977 if (update
->flags
& REF_DELETED_RMDIR
) {
2979 * The reference was deleted. Delete any
2980 * empty parent directories. (Note that this
2981 * can only work because we have already
2982 * removed the lockfile.)
2984 try_remove_empty_parents(refs
, update
->refname
,
2985 REMOVE_EMPTY_PARENTS_REF
);
2989 strbuf_release(&sb
);
2993 static int files_transaction_abort(struct ref_store
*ref_store
,
2994 struct ref_transaction
*transaction
,
2995 struct strbuf
*err UNUSED
)
2997 struct files_ref_store
*refs
=
2998 files_downcast(ref_store
, 0, "ref_transaction_abort");
3000 files_transaction_cleanup(refs
, transaction
);
3004 static int ref_present(const char *refname
,
3005 const struct object_id
*oid UNUSED
,
3009 struct string_list
*affected_refnames
= cb_data
;
3011 return string_list_has_string(affected_refnames
, refname
);
3014 static int files_initial_transaction_commit(struct ref_store
*ref_store
,
3015 struct ref_transaction
*transaction
,
3018 struct files_ref_store
*refs
=
3019 files_downcast(ref_store
, REF_STORE_WRITE
,
3020 "initial_ref_transaction_commit");
3023 struct string_list affected_refnames
= STRING_LIST_INIT_NODUP
;
3024 struct ref_transaction
*packed_transaction
= NULL
;
3028 if (transaction
->state
!= REF_TRANSACTION_OPEN
)
3029 BUG("commit called for transaction that is not open");
3031 /* Fail if a refname appears more than once in the transaction: */
3032 for (i
= 0; i
< transaction
->nr
; i
++)
3033 string_list_append(&affected_refnames
,
3034 transaction
->updates
[i
]->refname
);
3035 string_list_sort(&affected_refnames
);
3036 if (ref_update_reject_duplicates(&affected_refnames
, err
)) {
3037 ret
= TRANSACTION_GENERIC_ERROR
;
3042 * It's really undefined to call this function in an active
3043 * repository or when there are existing references: we are
3044 * only locking and changing packed-refs, so (1) any
3045 * simultaneous processes might try to change a reference at
3046 * the same time we do, and (2) any existing loose versions of
3047 * the references that we are setting would have precedence
3048 * over our values. But some remote helpers create the remote
3049 * "HEAD" and "master" branches before calling this function,
3050 * so here we really only check that none of the references
3051 * that we are creating already exists.
3053 if (refs_for_each_rawref(&refs
->base
, ref_present
,
3054 &affected_refnames
))
3055 BUG("initial ref transaction called with existing refs");
3057 packed_transaction
= ref_store_transaction_begin(refs
->packed_ref_store
, err
);
3058 if (!packed_transaction
) {
3059 ret
= TRANSACTION_GENERIC_ERROR
;
3063 for (i
= 0; i
< transaction
->nr
; i
++) {
3064 struct ref_update
*update
= transaction
->updates
[i
];
3066 if ((update
->flags
& REF_HAVE_OLD
) &&
3067 !is_null_oid(&update
->old_oid
))
3068 BUG("initial ref transaction with old_sha1 set");
3069 if (refs_verify_refname_available(&refs
->base
, update
->refname
,
3070 &affected_refnames
, NULL
,
3072 ret
= TRANSACTION_NAME_CONFLICT
;
3077 * Add a reference creation for this reference to the
3078 * packed-refs transaction:
3080 ref_transaction_add_update(packed_transaction
, update
->refname
,
3081 update
->flags
& ~REF_HAVE_OLD
,
3082 &update
->new_oid
, &update
->old_oid
,
3086 if (packed_refs_lock(refs
->packed_ref_store
, 0, err
)) {
3087 ret
= TRANSACTION_GENERIC_ERROR
;
3091 if (initial_ref_transaction_commit(packed_transaction
, err
)) {
3092 ret
= TRANSACTION_GENERIC_ERROR
;
3095 packed_refs_unlock(refs
->packed_ref_store
);
3097 if (packed_transaction
)
3098 ref_transaction_free(packed_transaction
);
3099 transaction
->state
= REF_TRANSACTION_CLOSED
;
3100 string_list_clear(&affected_refnames
, 0);
3104 struct expire_reflog_cb
{
3105 reflog_expiry_should_prune_fn
*should_prune_fn
;
3108 struct object_id last_kept_oid
;
3109 unsigned int rewrite
:1,
3113 static int expire_reflog_ent(struct object_id
*ooid
, struct object_id
*noid
,
3114 const char *email
, timestamp_t timestamp
, int tz
,
3115 const char *message
, void *cb_data
)
3117 struct expire_reflog_cb
*cb
= cb_data
;
3118 reflog_expiry_should_prune_fn
*fn
= cb
->should_prune_fn
;
3121 ooid
= &cb
->last_kept_oid
;
3123 if (fn(ooid
, noid
, email
, timestamp
, tz
, message
, cb
->policy_cb
))
3127 return 0; /* --dry-run */
3129 fprintf(cb
->newlog
, "%s %s %s %"PRItime
" %+05d\t%s", oid_to_hex(ooid
),
3130 oid_to_hex(noid
), email
, timestamp
, tz
, message
);
3131 oidcpy(&cb
->last_kept_oid
, noid
);
3136 static int files_reflog_expire(struct ref_store
*ref_store
,
3137 const char *refname
,
3138 unsigned int expire_flags
,
3139 reflog_expiry_prepare_fn prepare_fn
,
3140 reflog_expiry_should_prune_fn should_prune_fn
,
3141 reflog_expiry_cleanup_fn cleanup_fn
,
3142 void *policy_cb_data
)
3144 struct files_ref_store
*refs
=
3145 files_downcast(ref_store
, REF_STORE_WRITE
, "reflog_expire");
3146 struct lock_file reflog_lock
= LOCK_INIT
;
3147 struct expire_reflog_cb cb
;
3148 struct ref_lock
*lock
;
3149 struct strbuf log_file_sb
= STRBUF_INIT
;
3152 struct strbuf err
= STRBUF_INIT
;
3153 const struct object_id
*oid
;
3155 memset(&cb
, 0, sizeof(cb
));
3156 cb
.rewrite
= !!(expire_flags
& EXPIRE_REFLOGS_REWRITE
);
3157 cb
.dry_run
= !!(expire_flags
& EXPIRE_REFLOGS_DRY_RUN
);
3158 cb
.policy_cb
= policy_cb_data
;
3159 cb
.should_prune_fn
= should_prune_fn
;
3162 * The reflog file is locked by holding the lock on the
3163 * reference itself, plus we might need to update the
3164 * reference if --updateref was specified:
3166 lock
= lock_ref_oid_basic(refs
, refname
, &err
);
3168 error("cannot lock ref '%s': %s", refname
, err
.buf
);
3169 strbuf_release(&err
);
3172 oid
= &lock
->old_oid
;
3175 * When refs are deleted, their reflog is deleted before the
3176 * ref itself is deleted. This is because there is no separate
3177 * lock for reflog; instead we take a lock on the ref with
3178 * lock_ref_oid_basic().
3180 * If a race happens and the reflog doesn't exist after we've
3181 * acquired the lock that's OK. We've got nothing more to do;
3182 * We were asked to delete the reflog, but someone else
3183 * deleted it! The caller doesn't care that we deleted it,
3184 * just that it is deleted. So we can return successfully.
3186 if (!refs_reflog_exists(ref_store
, refname
)) {
3191 files_reflog_path(refs
, &log_file_sb
, refname
);
3192 log_file
= strbuf_detach(&log_file_sb
, NULL
);
3195 * Even though holding $GIT_DIR/logs/$reflog.lock has
3196 * no locking implications, we use the lock_file
3197 * machinery here anyway because it does a lot of the
3198 * work we need, including cleaning up if the program
3199 * exits unexpectedly.
3201 if (hold_lock_file_for_update(&reflog_lock
, log_file
, 0) < 0) {
3202 struct strbuf err
= STRBUF_INIT
;
3203 unable_to_lock_message(log_file
, errno
, &err
);
3204 error("%s", err
.buf
);
3205 strbuf_release(&err
);
3208 cb
.newlog
= fdopen_lock_file(&reflog_lock
, "w");
3210 error("cannot fdopen %s (%s)",
3211 get_lock_file_path(&reflog_lock
), strerror(errno
));
3216 (*prepare_fn
)(refname
, oid
, cb
.policy_cb
);
3217 refs_for_each_reflog_ent(ref_store
, refname
, expire_reflog_ent
, &cb
);
3218 (*cleanup_fn
)(cb
.policy_cb
);
3222 * It doesn't make sense to adjust a reference pointed
3223 * to by a symbolic ref based on expiring entries in
3224 * the symbolic reference's reflog. Nor can we update
3225 * a reference if there are no remaining reflog
3230 if ((expire_flags
& EXPIRE_REFLOGS_UPDATE_REF
) &&
3231 !is_null_oid(&cb
.last_kept_oid
)) {
3235 ref
= refs_resolve_ref_unsafe(&refs
->base
, refname
,
3236 RESOLVE_REF_NO_RECURSE
,
3238 update
= !!(ref
&& !(type
& REF_ISSYMREF
));
3241 if (close_lock_file_gently(&reflog_lock
)) {
3242 status
|= error("couldn't write %s: %s", log_file
,
3244 rollback_lock_file(&reflog_lock
);
3245 } else if (update
&&
3246 (write_in_full(get_lock_file_fd(&lock
->lk
),
3247 oid_to_hex(&cb
.last_kept_oid
), the_hash_algo
->hexsz
) < 0 ||
3248 write_str_in_full(get_lock_file_fd(&lock
->lk
), "\n") < 0 ||
3249 close_ref_gently(lock
) < 0)) {
3250 status
|= error("couldn't write %s",
3251 get_lock_file_path(&lock
->lk
));
3252 rollback_lock_file(&reflog_lock
);
3253 } else if (commit_lock_file(&reflog_lock
)) {
3254 status
|= error("unable to write reflog '%s' (%s)",
3255 log_file
, strerror(errno
));
3256 } else if (update
&& commit_ref(lock
)) {
3257 status
|= error("couldn't set %s", lock
->ref_name
);
3265 rollback_lock_file(&reflog_lock
);
3271 static int files_init_db(struct ref_store
*ref_store
, struct strbuf
*err UNUSED
)
3273 struct files_ref_store
*refs
=
3274 files_downcast(ref_store
, REF_STORE_WRITE
, "init_db");
3275 struct strbuf sb
= STRBUF_INIT
;
3278 * Create .git/refs/{heads,tags}
3280 files_ref_path(refs
, &sb
, "refs/heads");
3281 safe_create_dir(sb
.buf
, 1);
3284 files_ref_path(refs
, &sb
, "refs/tags");
3285 safe_create_dir(sb
.buf
, 1);
3287 strbuf_release(&sb
);
3291 struct ref_storage_be refs_be_files
= {
3294 .init
= files_ref_store_create
,
3295 .init_db
= files_init_db
,
3296 .transaction_prepare
= files_transaction_prepare
,
3297 .transaction_finish
= files_transaction_finish
,
3298 .transaction_abort
= files_transaction_abort
,
3299 .initial_transaction_commit
= files_initial_transaction_commit
,
3301 .pack_refs
= files_pack_refs
,
3302 .create_symref
= files_create_symref
,
3303 .delete_refs
= files_delete_refs
,
3304 .rename_ref
= files_rename_ref
,
3305 .copy_ref
= files_copy_ref
,
3307 .iterator_begin
= files_ref_iterator_begin
,
3308 .read_raw_ref
= files_read_raw_ref
,
3309 .read_symbolic_ref
= files_read_symbolic_ref
,
3311 .reflog_iterator_begin
= files_reflog_iterator_begin
,
3312 .for_each_reflog_ent
= files_for_each_reflog_ent
,
3313 .for_each_reflog_ent_reverse
= files_for_each_reflog_ent_reverse
,
3314 .reflog_exists
= files_reflog_exists
,
3315 .create_reflog
= files_create_reflog
,
3316 .delete_reflog
= files_delete_reflog
,
3317 .reflog_expire
= files_reflog_expire