4 #include "refs-internal.h"
6 #include "packed-backend.h"
7 #include "../iterator.h"
8 #include "../dir-iterator.h"
9 #include "../lockfile.h"
10 #include "../object.h"
16 struct object_id old_oid
;
20 * Future: need to be in "struct repository"
21 * when doing a full libification.
23 struct files_ref_store
{
24 struct ref_store base
;
25 unsigned int store_flags
;
30 struct ref_cache
*loose
;
32 struct ref_store
*packed_ref_store
;
35 static void clear_loose_ref_cache(struct files_ref_store
*refs
)
38 free_ref_cache(refs
->loose
);
44 * Create a new submodule ref cache and add it to the internal
47 static struct ref_store
*files_ref_store_create(const char *gitdir
,
50 struct files_ref_store
*refs
= xcalloc(1, sizeof(*refs
));
51 struct ref_store
*ref_store
= (struct ref_store
*)refs
;
52 struct strbuf sb
= STRBUF_INIT
;
54 base_ref_store_init(ref_store
, &refs_be_files
);
55 refs
->store_flags
= flags
;
57 refs
->gitdir
= xstrdup(gitdir
);
58 get_common_dir_noenv(&sb
, gitdir
);
59 refs
->gitcommondir
= strbuf_detach(&sb
, NULL
);
60 strbuf_addf(&sb
, "%s/packed-refs", refs
->gitcommondir
);
61 refs
->packed_ref_store
= packed_ref_store_create(sb
.buf
, flags
);
68 * Die if refs is not the main ref store. caller is used in any
69 * necessary error messages.
71 static void files_assert_main_repository(struct files_ref_store
*refs
,
74 if (refs
->store_flags
& REF_STORE_MAIN
)
77 die("BUG: operation %s only allowed for main ref store", caller
);
81 * Downcast ref_store to files_ref_store. Die if ref_store is not a
82 * files_ref_store. required_flags is compared with ref_store's
83 * store_flags to ensure the ref_store has all required capabilities.
84 * "caller" is used in any necessary error messages.
86 static struct files_ref_store
*files_downcast(struct ref_store
*ref_store
,
87 unsigned int required_flags
,
90 struct files_ref_store
*refs
;
92 if (ref_store
->be
!= &refs_be_files
)
93 die("BUG: ref_store is type \"%s\" not \"files\" in %s",
94 ref_store
->be
->name
, caller
);
96 refs
= (struct files_ref_store
*)ref_store
;
98 if ((refs
->store_flags
& required_flags
) != required_flags
)
99 die("BUG: operation %s requires abilities 0x%x, but only have 0x%x",
100 caller
, required_flags
, refs
->store_flags
);
105 static void files_reflog_path(struct files_ref_store
*refs
,
109 switch (ref_type(refname
)) {
110 case REF_TYPE_PER_WORKTREE
:
111 case REF_TYPE_PSEUDOREF
:
112 strbuf_addf(sb
, "%s/logs/%s", refs
->gitdir
, refname
);
114 case REF_TYPE_NORMAL
:
115 strbuf_addf(sb
, "%s/logs/%s", refs
->gitcommondir
, refname
);
118 die("BUG: unknown ref type %d of ref %s",
119 ref_type(refname
), refname
);
123 static void files_ref_path(struct files_ref_store
*refs
,
127 switch (ref_type(refname
)) {
128 case REF_TYPE_PER_WORKTREE
:
129 case REF_TYPE_PSEUDOREF
:
130 strbuf_addf(sb
, "%s/%s", refs
->gitdir
, refname
);
132 case REF_TYPE_NORMAL
:
133 strbuf_addf(sb
, "%s/%s", refs
->gitcommondir
, refname
);
136 die("BUG: unknown ref type %d of ref %s",
137 ref_type(refname
), refname
);
142 * Read the loose references from the namespace dirname into dir
143 * (without recursing). dirname must end with '/'. dir must be the
144 * directory entry corresponding to dirname.
146 static void loose_fill_ref_dir(struct ref_store
*ref_store
,
147 struct ref_dir
*dir
, const char *dirname
)
149 struct files_ref_store
*refs
=
150 files_downcast(ref_store
, REF_STORE_READ
, "fill_ref_dir");
153 int dirnamelen
= strlen(dirname
);
154 struct strbuf refname
;
155 struct strbuf path
= STRBUF_INIT
;
158 files_ref_path(refs
, &path
, dirname
);
159 path_baselen
= path
.len
;
161 d
= opendir(path
.buf
);
163 strbuf_release(&path
);
167 strbuf_init(&refname
, dirnamelen
+ 257);
168 strbuf_add(&refname
, dirname
, dirnamelen
);
170 while ((de
= readdir(d
)) != NULL
) {
171 struct object_id oid
;
175 if (de
->d_name
[0] == '.')
177 if (ends_with(de
->d_name
, ".lock"))
179 strbuf_addstr(&refname
, de
->d_name
);
180 strbuf_addstr(&path
, de
->d_name
);
181 if (stat(path
.buf
, &st
) < 0) {
182 ; /* silently ignore */
183 } else if (S_ISDIR(st
.st_mode
)) {
184 strbuf_addch(&refname
, '/');
185 add_entry_to_dir(dir
,
186 create_dir_entry(dir
->cache
, refname
.buf
,
189 if (!refs_resolve_ref_unsafe(&refs
->base
,
194 flag
|= REF_ISBROKEN
;
195 } else if (is_null_oid(&oid
)) {
197 * It is so astronomically unlikely
198 * that NULL_SHA1 is the SHA-1 of an
199 * actual object that we consider its
200 * appearance in a loose reference
201 * file to be repo corruption
202 * (probably due to a software bug).
204 flag
|= REF_ISBROKEN
;
207 if (check_refname_format(refname
.buf
,
208 REFNAME_ALLOW_ONELEVEL
)) {
209 if (!refname_is_safe(refname
.buf
))
210 die("loose refname is dangerous: %s", refname
.buf
);
212 flag
|= REF_BAD_NAME
| REF_ISBROKEN
;
214 add_entry_to_dir(dir
,
215 create_ref_entry(refname
.buf
, &oid
, flag
));
217 strbuf_setlen(&refname
, dirnamelen
);
218 strbuf_setlen(&path
, path_baselen
);
220 strbuf_release(&refname
);
221 strbuf_release(&path
);
225 * Manually add refs/bisect, which, being per-worktree, might
226 * not appear in the directory listing for refs/ in the main
229 if (!strcmp(dirname
, "refs/")) {
230 int pos
= search_ref_dir(dir
, "refs/bisect/", 12);
233 struct ref_entry
*child_entry
= create_dir_entry(
234 dir
->cache
, "refs/bisect/", 12, 1);
235 add_entry_to_dir(dir
, child_entry
);
240 static struct ref_cache
*get_loose_ref_cache(struct files_ref_store
*refs
)
244 * Mark the top-level directory complete because we
245 * are about to read the only subdirectory that can
248 refs
->loose
= create_ref_cache(&refs
->base
, loose_fill_ref_dir
);
250 /* We're going to fill the top level ourselves: */
251 refs
->loose
->root
->flag
&= ~REF_INCOMPLETE
;
254 * Add an incomplete entry for "refs/" (to be filled
257 add_entry_to_dir(get_ref_dir(refs
->loose
->root
),
258 create_dir_entry(refs
->loose
, "refs/", 5, 1));
263 static int files_read_raw_ref(struct ref_store
*ref_store
,
264 const char *refname
, unsigned char *sha1
,
265 struct strbuf
*referent
, unsigned int *type
)
267 struct files_ref_store
*refs
=
268 files_downcast(ref_store
, REF_STORE_READ
, "read_raw_ref");
269 struct strbuf sb_contents
= STRBUF_INIT
;
270 struct strbuf sb_path
= STRBUF_INIT
;
277 int remaining_retries
= 3;
280 strbuf_reset(&sb_path
);
282 files_ref_path(refs
, &sb_path
, refname
);
288 * We might have to loop back here to avoid a race
289 * condition: first we lstat() the file, then we try
290 * to read it as a link or as a file. But if somebody
291 * changes the type of the file (file <-> directory
292 * <-> symlink) between the lstat() and reading, then
293 * we don't want to report that as an error but rather
294 * try again starting with the lstat().
296 * We'll keep a count of the retries, though, just to avoid
297 * any confusing situation sending us into an infinite loop.
300 if (remaining_retries
-- <= 0)
303 if (lstat(path
, &st
) < 0) {
306 if (refs_read_raw_ref(refs
->packed_ref_store
, refname
,
307 sha1
, referent
, type
)) {
315 /* Follow "normalized" - ie "refs/.." symlinks by hand */
316 if (S_ISLNK(st
.st_mode
)) {
317 strbuf_reset(&sb_contents
);
318 if (strbuf_readlink(&sb_contents
, path
, 0) < 0) {
319 if (errno
== ENOENT
|| errno
== EINVAL
)
320 /* inconsistent with lstat; retry */
325 if (starts_with(sb_contents
.buf
, "refs/") &&
326 !check_refname_format(sb_contents
.buf
, 0)) {
327 strbuf_swap(&sb_contents
, referent
);
328 *type
|= REF_ISSYMREF
;
333 * It doesn't look like a refname; fall through to just
334 * treating it like a non-symlink, and reading whatever it
339 /* Is it a directory? */
340 if (S_ISDIR(st
.st_mode
)) {
342 * Even though there is a directory where the loose
343 * ref is supposed to be, there could still be a
346 if (refs_read_raw_ref(refs
->packed_ref_store
, refname
,
347 sha1
, referent
, type
)) {
356 * Anything else, just open it and try to use it as
359 fd
= open(path
, O_RDONLY
);
361 if (errno
== ENOENT
&& !S_ISLNK(st
.st_mode
))
362 /* inconsistent with lstat; retry */
367 strbuf_reset(&sb_contents
);
368 if (strbuf_read(&sb_contents
, fd
, 256) < 0) {
369 int save_errno
= errno
;
375 strbuf_rtrim(&sb_contents
);
376 buf
= sb_contents
.buf
;
377 if (starts_with(buf
, "ref:")) {
379 while (isspace(*buf
))
382 strbuf_reset(referent
);
383 strbuf_addstr(referent
, buf
);
384 *type
|= REF_ISSYMREF
;
390 * Please note that FETCH_HEAD has additional
391 * data after the sha.
393 if (get_sha1_hex(buf
, sha1
) ||
394 (buf
[40] != '\0' && !isspace(buf
[40]))) {
395 *type
|= REF_ISBROKEN
;
404 strbuf_release(&sb_path
);
405 strbuf_release(&sb_contents
);
410 static void unlock_ref(struct ref_lock
*lock
)
412 rollback_lock_file(&lock
->lk
);
413 free(lock
->ref_name
);
418 * Lock refname, without following symrefs, and set *lock_p to point
419 * at a newly-allocated lock object. Fill in lock->old_oid, referent,
420 * and type similarly to read_raw_ref().
422 * The caller must verify that refname is a "safe" reference name (in
423 * the sense of refname_is_safe()) before calling this function.
425 * If the reference doesn't already exist, verify that refname doesn't
426 * have a D/F conflict with any existing references. extras and skip
427 * are passed to refs_verify_refname_available() for this check.
429 * If mustexist is not set and the reference is not found or is
430 * broken, lock the reference anyway but clear sha1.
432 * Return 0 on success. On failure, write an error message to err and
433 * return TRANSACTION_NAME_CONFLICT or TRANSACTION_GENERIC_ERROR.
435 * Implementation note: This function is basically
440 * but it includes a lot more code to
441 * - Deal with possible races with other processes
442 * - Avoid calling refs_verify_refname_available() when it can be
443 * avoided, namely if we were successfully able to read the ref
444 * - Generate informative error messages in the case of failure
446 static int lock_raw_ref(struct files_ref_store
*refs
,
447 const char *refname
, int mustexist
,
448 const struct string_list
*extras
,
449 const struct string_list
*skip
,
450 struct ref_lock
**lock_p
,
451 struct strbuf
*referent
,
455 struct ref_lock
*lock
;
456 struct strbuf ref_file
= STRBUF_INIT
;
457 int attempts_remaining
= 3;
458 int ret
= TRANSACTION_GENERIC_ERROR
;
461 files_assert_main_repository(refs
, "lock_raw_ref");
465 /* First lock the file so it can't change out from under us. */
467 *lock_p
= lock
= xcalloc(1, sizeof(*lock
));
469 lock
->ref_name
= xstrdup(refname
);
470 files_ref_path(refs
, &ref_file
, refname
);
473 switch (safe_create_leading_directories(ref_file
.buf
)) {
478 * Suppose refname is "refs/foo/bar". We just failed
479 * to create the containing directory, "refs/foo",
480 * because there was a non-directory in the way. This
481 * indicates a D/F conflict, probably because of
482 * another reference such as "refs/foo". There is no
483 * reason to expect this error to be transitory.
485 if (refs_verify_refname_available(&refs
->base
, refname
,
486 extras
, skip
, err
)) {
489 * To the user the relevant error is
490 * that the "mustexist" reference is
494 strbuf_addf(err
, "unable to resolve reference '%s'",
498 * The error message set by
499 * refs_verify_refname_available() is
502 ret
= TRANSACTION_NAME_CONFLICT
;
506 * The file that is in the way isn't a loose
507 * reference. Report it as a low-level
510 strbuf_addf(err
, "unable to create lock file %s.lock; "
511 "non-directory in the way",
516 /* Maybe another process was tidying up. Try again. */
517 if (--attempts_remaining
> 0)
521 strbuf_addf(err
, "unable to create directory for %s",
526 if (hold_lock_file_for_update_timeout(
527 &lock
->lk
, ref_file
.buf
, LOCK_NO_DEREF
,
528 get_files_ref_lock_timeout_ms()) < 0) {
529 if (errno
== ENOENT
&& --attempts_remaining
> 0) {
531 * Maybe somebody just deleted one of the
532 * directories leading to ref_file. Try
537 unable_to_lock_message(ref_file
.buf
, errno
, err
);
543 * Now we hold the lock and can read the reference without
544 * fear that its value will change.
547 if (files_read_raw_ref(&refs
->base
, refname
,
548 lock
->old_oid
.hash
, referent
, type
)) {
549 if (errno
== ENOENT
) {
551 /* Garden variety missing reference. */
552 strbuf_addf(err
, "unable to resolve reference '%s'",
557 * Reference is missing, but that's OK. We
558 * know that there is not a conflict with
559 * another loose reference because
560 * (supposing that we are trying to lock
561 * reference "refs/foo/bar"):
563 * - We were successfully able to create
564 * the lockfile refs/foo/bar.lock, so we
565 * know there cannot be a loose reference
568 * - We got ENOENT and not EISDIR, so we
569 * know that there cannot be a loose
570 * reference named "refs/foo/bar/baz".
573 } else if (errno
== EISDIR
) {
575 * There is a directory in the way. It might have
576 * contained references that have been deleted. If
577 * we don't require that the reference already
578 * exists, try to remove the directory so that it
579 * doesn't cause trouble when we want to rename the
580 * lockfile into place later.
583 /* Garden variety missing reference. */
584 strbuf_addf(err
, "unable to resolve reference '%s'",
587 } else if (remove_dir_recursively(&ref_file
,
588 REMOVE_DIR_EMPTY_ONLY
)) {
589 if (refs_verify_refname_available(
590 &refs
->base
, refname
,
591 extras
, skip
, err
)) {
593 * The error message set by
594 * verify_refname_available() is OK.
596 ret
= TRANSACTION_NAME_CONFLICT
;
600 * We can't delete the directory,
601 * but we also don't know of any
602 * references that it should
605 strbuf_addf(err
, "there is a non-empty directory '%s' "
606 "blocking reference '%s'",
607 ref_file
.buf
, refname
);
611 } else if (errno
== EINVAL
&& (*type
& REF_ISBROKEN
)) {
612 strbuf_addf(err
, "unable to resolve reference '%s': "
613 "reference broken", refname
);
616 strbuf_addf(err
, "unable to resolve reference '%s': %s",
617 refname
, strerror(errno
));
622 * If the ref did not exist and we are creating it,
623 * make sure there is no existing packed ref that
624 * conflicts with refname:
626 if (refs_verify_refname_available(
627 refs
->packed_ref_store
, refname
,
640 strbuf_release(&ref_file
);
644 static int files_peel_ref(struct ref_store
*ref_store
,
645 const char *refname
, unsigned char *sha1
)
647 struct files_ref_store
*refs
=
648 files_downcast(ref_store
, REF_STORE_READ
| REF_STORE_ODB
,
651 unsigned char base
[20];
653 if (current_ref_iter
&& current_ref_iter
->refname
== refname
) {
654 struct object_id peeled
;
656 if (ref_iterator_peel(current_ref_iter
, &peeled
))
658 hashcpy(sha1
, peeled
.hash
);
662 if (refs_read_ref_full(ref_store
, refname
,
663 RESOLVE_REF_READING
, base
, &flag
))
667 * If the reference is packed, read its ref_entry from the
668 * cache in the hope that we already know its peeled value.
669 * We only try this optimization on packed references because
670 * (a) forcing the filling of the loose reference cache could
671 * be expensive and (b) loose references anyway usually do not
672 * have REF_KNOWS_PEELED.
674 if (flag
& REF_ISPACKED
&&
675 !refs_peel_ref(refs
->packed_ref_store
, refname
, sha1
))
678 return peel_object(base
, sha1
);
681 struct files_ref_iterator
{
682 struct ref_iterator base
;
684 struct ref_iterator
*iter0
;
688 static int files_ref_iterator_advance(struct ref_iterator
*ref_iterator
)
690 struct files_ref_iterator
*iter
=
691 (struct files_ref_iterator
*)ref_iterator
;
694 while ((ok
= ref_iterator_advance(iter
->iter0
)) == ITER_OK
) {
695 if (iter
->flags
& DO_FOR_EACH_PER_WORKTREE_ONLY
&&
696 ref_type(iter
->iter0
->refname
) != REF_TYPE_PER_WORKTREE
)
699 if (!(iter
->flags
& DO_FOR_EACH_INCLUDE_BROKEN
) &&
700 !ref_resolves_to_object(iter
->iter0
->refname
,
705 iter
->base
.refname
= iter
->iter0
->refname
;
706 iter
->base
.oid
= iter
->iter0
->oid
;
707 iter
->base
.flags
= iter
->iter0
->flags
;
712 if (ref_iterator_abort(ref_iterator
) != ITER_DONE
)
718 static int files_ref_iterator_peel(struct ref_iterator
*ref_iterator
,
719 struct object_id
*peeled
)
721 struct files_ref_iterator
*iter
=
722 (struct files_ref_iterator
*)ref_iterator
;
724 return ref_iterator_peel(iter
->iter0
, peeled
);
727 static int files_ref_iterator_abort(struct ref_iterator
*ref_iterator
)
729 struct files_ref_iterator
*iter
=
730 (struct files_ref_iterator
*)ref_iterator
;
734 ok
= ref_iterator_abort(iter
->iter0
);
736 base_ref_iterator_free(ref_iterator
);
740 static struct ref_iterator_vtable files_ref_iterator_vtable
= {
741 files_ref_iterator_advance
,
742 files_ref_iterator_peel
,
743 files_ref_iterator_abort
746 static struct ref_iterator
*files_ref_iterator_begin(
747 struct ref_store
*ref_store
,
748 const char *prefix
, unsigned int flags
)
750 struct files_ref_store
*refs
;
751 struct ref_iterator
*loose_iter
, *packed_iter
;
752 struct files_ref_iterator
*iter
;
753 struct ref_iterator
*ref_iterator
;
754 unsigned int required_flags
= REF_STORE_READ
;
756 if (!(flags
& DO_FOR_EACH_INCLUDE_BROKEN
))
757 required_flags
|= REF_STORE_ODB
;
759 refs
= files_downcast(ref_store
, required_flags
, "ref_iterator_begin");
761 iter
= xcalloc(1, sizeof(*iter
));
762 ref_iterator
= &iter
->base
;
763 base_ref_iterator_init(ref_iterator
, &files_ref_iterator_vtable
);
766 * We must make sure that all loose refs are read before
767 * accessing the packed-refs file; this avoids a race
768 * condition if loose refs are migrated to the packed-refs
769 * file by a simultaneous process, but our in-memory view is
770 * from before the migration. We ensure this as follows:
771 * First, we call start the loose refs iteration with its
772 * `prime_ref` argument set to true. This causes the loose
773 * references in the subtree to be pre-read into the cache.
774 * (If they've already been read, that's OK; we only need to
775 * guarantee that they're read before the packed refs, not
776 * *how much* before.) After that, we call
777 * packed_ref_iterator_begin(), which internally checks
778 * whether the packed-ref cache is up to date with what is on
779 * disk, and re-reads it if not.
782 loose_iter
= cache_ref_iterator_begin(get_loose_ref_cache(refs
),
786 * The packed-refs file might contain broken references, for
787 * example an old version of a reference that points at an
788 * object that has since been garbage-collected. This is OK as
789 * long as there is a corresponding loose reference that
790 * overrides it, and we don't want to emit an error message in
791 * this case. So ask the packed_ref_store for all of its
792 * references, and (if needed) do our own check for broken
793 * ones in files_ref_iterator_advance(), after we have merged
794 * the packed and loose references.
796 packed_iter
= refs_ref_iterator_begin(
797 refs
->packed_ref_store
, prefix
, 0,
798 DO_FOR_EACH_INCLUDE_BROKEN
);
800 iter
->iter0
= overlay_ref_iterator_begin(loose_iter
, packed_iter
);
807 * Verify that the reference locked by lock has the value old_sha1.
808 * Fail if the reference doesn't exist and mustexist is set. Return 0
809 * on success. On error, write an error message to err, set errno, and
810 * return a negative value.
812 static int verify_lock(struct ref_store
*ref_store
, struct ref_lock
*lock
,
813 const unsigned char *old_sha1
, int mustexist
,
818 if (refs_read_ref_full(ref_store
, lock
->ref_name
,
819 mustexist
? RESOLVE_REF_READING
: 0,
820 lock
->old_oid
.hash
, NULL
)) {
822 int save_errno
= errno
;
823 strbuf_addf(err
, "can't verify ref '%s'", lock
->ref_name
);
827 oidclr(&lock
->old_oid
);
831 if (old_sha1
&& hashcmp(lock
->old_oid
.hash
, old_sha1
)) {
832 strbuf_addf(err
, "ref '%s' is at %s but expected %s",
834 oid_to_hex(&lock
->old_oid
),
835 sha1_to_hex(old_sha1
));
842 static int remove_empty_directories(struct strbuf
*path
)
845 * we want to create a file but there is a directory there;
846 * if that is an empty directory (or a directory that contains
847 * only empty directories), remove them.
849 return remove_dir_recursively(path
, REMOVE_DIR_EMPTY_ONLY
);
852 static int create_reflock(const char *path
, void *cb
)
854 struct lock_file
*lk
= cb
;
856 return hold_lock_file_for_update_timeout(
857 lk
, path
, LOCK_NO_DEREF
,
858 get_files_ref_lock_timeout_ms()) < 0 ? -1 : 0;
862 * Locks a ref returning the lock on success and NULL on failure.
863 * On failure errno is set to something meaningful.
865 static struct ref_lock
*lock_ref_sha1_basic(struct files_ref_store
*refs
,
867 const unsigned char *old_sha1
,
868 const struct string_list
*extras
,
869 const struct string_list
*skip
,
870 unsigned int flags
, int *type
,
873 struct strbuf ref_file
= STRBUF_INIT
;
874 struct ref_lock
*lock
;
876 int mustexist
= (old_sha1
&& !is_null_sha1(old_sha1
));
877 int resolve_flags
= RESOLVE_REF_NO_RECURSE
;
880 files_assert_main_repository(refs
, "lock_ref_sha1_basic");
883 lock
= xcalloc(1, sizeof(struct ref_lock
));
886 resolve_flags
|= RESOLVE_REF_READING
;
887 if (flags
& REF_DELETING
)
888 resolve_flags
|= RESOLVE_REF_ALLOW_BAD_NAME
;
890 files_ref_path(refs
, &ref_file
, refname
);
891 resolved
= !!refs_resolve_ref_unsafe(&refs
->base
,
892 refname
, resolve_flags
,
893 lock
->old_oid
.hash
, type
);
894 if (!resolved
&& errno
== EISDIR
) {
896 * we are trying to lock foo but we used to
897 * have foo/bar which now does not exist;
898 * it is normal for the empty directory 'foo'
901 if (remove_empty_directories(&ref_file
)) {
903 if (!refs_verify_refname_available(
905 refname
, extras
, skip
, err
))
906 strbuf_addf(err
, "there are still refs under '%s'",
910 resolved
= !!refs_resolve_ref_unsafe(&refs
->base
,
911 refname
, resolve_flags
,
912 lock
->old_oid
.hash
, type
);
916 if (last_errno
!= ENOTDIR
||
917 !refs_verify_refname_available(&refs
->base
, refname
,
919 strbuf_addf(err
, "unable to resolve reference '%s': %s",
920 refname
, strerror(last_errno
));
926 * If the ref did not exist and we are creating it, make sure
927 * there is no existing packed ref whose name begins with our
928 * refname, nor a packed ref whose name is a proper prefix of
931 if (is_null_oid(&lock
->old_oid
) &&
932 refs_verify_refname_available(refs
->packed_ref_store
, refname
,
933 extras
, skip
, err
)) {
934 last_errno
= ENOTDIR
;
938 lock
->ref_name
= xstrdup(refname
);
940 if (raceproof_create_file(ref_file
.buf
, create_reflock
, &lock
->lk
)) {
942 unable_to_lock_message(ref_file
.buf
, errno
, err
);
946 if (verify_lock(&refs
->base
, lock
, old_sha1
, mustexist
, err
)) {
957 strbuf_release(&ref_file
);
962 struct ref_to_prune
{
963 struct ref_to_prune
*next
;
964 unsigned char sha1
[20];
965 char name
[FLEX_ARRAY
];
969 REMOVE_EMPTY_PARENTS_REF
= 0x01,
970 REMOVE_EMPTY_PARENTS_REFLOG
= 0x02
974 * Remove empty parent directories associated with the specified
975 * reference and/or its reflog, but spare [logs/]refs/ and immediate
976 * subdirs. flags is a combination of REMOVE_EMPTY_PARENTS_REF and/or
977 * REMOVE_EMPTY_PARENTS_REFLOG.
979 static void try_remove_empty_parents(struct files_ref_store
*refs
,
983 struct strbuf buf
= STRBUF_INIT
;
984 struct strbuf sb
= STRBUF_INIT
;
988 strbuf_addstr(&buf
, refname
);
990 for (i
= 0; i
< 2; i
++) { /* refs/{heads,tags,...}/ */
991 while (*p
&& *p
!= '/')
993 /* tolerate duplicate slashes; see check_refname_format() */
997 q
= buf
.buf
+ buf
.len
;
998 while (flags
& (REMOVE_EMPTY_PARENTS_REF
| REMOVE_EMPTY_PARENTS_REFLOG
)) {
999 while (q
> p
&& *q
!= '/')
1001 while (q
> p
&& *(q
-1) == '/')
1005 strbuf_setlen(&buf
, q
- buf
.buf
);
1008 files_ref_path(refs
, &sb
, buf
.buf
);
1009 if ((flags
& REMOVE_EMPTY_PARENTS_REF
) && rmdir(sb
.buf
))
1010 flags
&= ~REMOVE_EMPTY_PARENTS_REF
;
1013 files_reflog_path(refs
, &sb
, buf
.buf
);
1014 if ((flags
& REMOVE_EMPTY_PARENTS_REFLOG
) && rmdir(sb
.buf
))
1015 flags
&= ~REMOVE_EMPTY_PARENTS_REFLOG
;
1017 strbuf_release(&buf
);
1018 strbuf_release(&sb
);
1021 /* make sure nobody touched the ref, and unlink */
1022 static void prune_ref(struct files_ref_store
*refs
, struct ref_to_prune
*r
)
1024 struct ref_transaction
*transaction
;
1025 struct strbuf err
= STRBUF_INIT
;
1027 if (check_refname_format(r
->name
, 0))
1030 transaction
= ref_store_transaction_begin(&refs
->base
, &err
);
1032 ref_transaction_delete(transaction
, r
->name
, r
->sha1
,
1033 REF_ISPRUNING
| REF_NODEREF
, NULL
, &err
) ||
1034 ref_transaction_commit(transaction
, &err
)) {
1035 ref_transaction_free(transaction
);
1036 error("%s", err
.buf
);
1037 strbuf_release(&err
);
1040 ref_transaction_free(transaction
);
1041 strbuf_release(&err
);
1045 * Prune the loose versions of the references in the linked list
1046 * `*refs_to_prune`, freeing the entries in the list as we go.
1048 static void prune_refs(struct files_ref_store
*refs
, struct ref_to_prune
**refs_to_prune
)
1050 while (*refs_to_prune
) {
1051 struct ref_to_prune
*r
= *refs_to_prune
;
1052 *refs_to_prune
= r
->next
;
1059 * Return true if the specified reference should be packed.
1061 static int should_pack_ref(const char *refname
,
1062 const struct object_id
*oid
, unsigned int ref_flags
,
1063 unsigned int pack_flags
)
1065 /* Do not pack per-worktree refs: */
1066 if (ref_type(refname
) != REF_TYPE_NORMAL
)
1069 /* Do not pack non-tags unless PACK_REFS_ALL is set: */
1070 if (!(pack_flags
& PACK_REFS_ALL
) && !starts_with(refname
, "refs/tags/"))
1073 /* Do not pack symbolic refs: */
1074 if (ref_flags
& REF_ISSYMREF
)
1077 /* Do not pack broken refs: */
1078 if (!ref_resolves_to_object(refname
, oid
, ref_flags
))
1084 static int files_pack_refs(struct ref_store
*ref_store
, unsigned int flags
)
1086 struct files_ref_store
*refs
=
1087 files_downcast(ref_store
, REF_STORE_WRITE
| REF_STORE_ODB
,
1089 struct ref_iterator
*iter
;
1091 struct ref_to_prune
*refs_to_prune
= NULL
;
1092 struct strbuf err
= STRBUF_INIT
;
1093 struct ref_transaction
*transaction
;
1095 transaction
= ref_store_transaction_begin(refs
->packed_ref_store
, &err
);
1099 packed_refs_lock(refs
->packed_ref_store
, LOCK_DIE_ON_ERROR
, &err
);
1101 iter
= cache_ref_iterator_begin(get_loose_ref_cache(refs
), NULL
, 0);
1102 while ((ok
= ref_iterator_advance(iter
)) == ITER_OK
) {
1104 * If the loose reference can be packed, add an entry
1105 * in the packed ref cache. If the reference should be
1106 * pruned, also add it to refs_to_prune.
1108 if (!should_pack_ref(iter
->refname
, iter
->oid
, iter
->flags
,
1113 * Add a reference creation for this reference to the
1114 * packed-refs transaction:
1116 if (ref_transaction_update(transaction
, iter
->refname
,
1117 iter
->oid
->hash
, NULL
,
1118 REF_NODEREF
, NULL
, &err
))
1119 die("failure preparing to create packed reference %s: %s",
1120 iter
->refname
, err
.buf
);
1122 /* Schedule the loose reference for pruning if requested. */
1123 if ((flags
& PACK_REFS_PRUNE
)) {
1124 struct ref_to_prune
*n
;
1125 FLEX_ALLOC_STR(n
, name
, iter
->refname
);
1126 hashcpy(n
->sha1
, iter
->oid
->hash
);
1127 n
->next
= refs_to_prune
;
1131 if (ok
!= ITER_DONE
)
1132 die("error while iterating over references");
1134 if (ref_transaction_commit(transaction
, &err
))
1135 die("unable to write new packed-refs: %s", err
.buf
);
1137 ref_transaction_free(transaction
);
1139 packed_refs_unlock(refs
->packed_ref_store
);
1141 prune_refs(refs
, &refs_to_prune
);
1142 strbuf_release(&err
);
1146 static int files_delete_refs(struct ref_store
*ref_store
, const char *msg
,
1147 struct string_list
*refnames
, unsigned int flags
)
1149 struct files_ref_store
*refs
=
1150 files_downcast(ref_store
, REF_STORE_WRITE
, "delete_refs");
1151 struct strbuf err
= STRBUF_INIT
;
1157 if (packed_refs_lock(refs
->packed_ref_store
, 0, &err
))
1160 if (refs_delete_refs(refs
->packed_ref_store
, msg
, refnames
, flags
)) {
1161 packed_refs_unlock(refs
->packed_ref_store
);
1165 packed_refs_unlock(refs
->packed_ref_store
);
1167 for (i
= 0; i
< refnames
->nr
; i
++) {
1168 const char *refname
= refnames
->items
[i
].string
;
1170 if (refs_delete_ref(&refs
->base
, msg
, refname
, NULL
, flags
))
1171 result
|= error(_("could not remove reference %s"), refname
);
1174 strbuf_release(&err
);
1179 * If we failed to rewrite the packed-refs file, then it is
1180 * unsafe to try to remove loose refs, because doing so might
1181 * expose an obsolete packed value for a reference that might
1182 * even point at an object that has been garbage collected.
1184 if (refnames
->nr
== 1)
1185 error(_("could not delete reference %s: %s"),
1186 refnames
->items
[0].string
, err
.buf
);
1188 error(_("could not delete references: %s"), err
.buf
);
1190 strbuf_release(&err
);
1195 * People using contrib's git-new-workdir have .git/logs/refs ->
1196 * /some/other/path/.git/logs/refs, and that may live on another device.
1198 * IOW, to avoid cross device rename errors, the temporary renamed log must
1199 * live into logs/refs.
1201 #define TMP_RENAMED_LOG "refs/.tmp-renamed-log"
1204 const char *tmp_renamed_log
;
1208 static int rename_tmp_log_callback(const char *path
, void *cb_data
)
1210 struct rename_cb
*cb
= cb_data
;
1212 if (rename(cb
->tmp_renamed_log
, path
)) {
1214 * rename(a, b) when b is an existing directory ought
1215 * to result in ISDIR, but Solaris 5.8 gives ENOTDIR.
1216 * Sheesh. Record the true errno for error reporting,
1217 * but report EISDIR to raceproof_create_file() so
1218 * that it knows to retry.
1220 cb
->true_errno
= errno
;
1221 if (errno
== ENOTDIR
)
1229 static int rename_tmp_log(struct files_ref_store
*refs
, const char *newrefname
)
1231 struct strbuf path
= STRBUF_INIT
;
1232 struct strbuf tmp
= STRBUF_INIT
;
1233 struct rename_cb cb
;
1236 files_reflog_path(refs
, &path
, newrefname
);
1237 files_reflog_path(refs
, &tmp
, TMP_RENAMED_LOG
);
1238 cb
.tmp_renamed_log
= tmp
.buf
;
1239 ret
= raceproof_create_file(path
.buf
, rename_tmp_log_callback
, &cb
);
1241 if (errno
== EISDIR
)
1242 error("directory not empty: %s", path
.buf
);
1244 error("unable to move logfile %s to %s: %s",
1246 strerror(cb
.true_errno
));
1249 strbuf_release(&path
);
1250 strbuf_release(&tmp
);
1254 static int write_ref_to_lockfile(struct ref_lock
*lock
,
1255 const struct object_id
*oid
, struct strbuf
*err
);
1256 static int commit_ref_update(struct files_ref_store
*refs
,
1257 struct ref_lock
*lock
,
1258 const struct object_id
*oid
, const char *logmsg
,
1259 struct strbuf
*err
);
1261 static int files_copy_or_rename_ref(struct ref_store
*ref_store
,
1262 const char *oldrefname
, const char *newrefname
,
1263 const char *logmsg
, int copy
)
1265 struct files_ref_store
*refs
=
1266 files_downcast(ref_store
, REF_STORE_WRITE
, "rename_ref");
1267 struct object_id oid
, orig_oid
;
1268 int flag
= 0, logmoved
= 0;
1269 struct ref_lock
*lock
;
1270 struct stat loginfo
;
1271 struct strbuf sb_oldref
= STRBUF_INIT
;
1272 struct strbuf sb_newref
= STRBUF_INIT
;
1273 struct strbuf tmp_renamed_log
= STRBUF_INIT
;
1275 struct strbuf err
= STRBUF_INIT
;
1277 files_reflog_path(refs
, &sb_oldref
, oldrefname
);
1278 files_reflog_path(refs
, &sb_newref
, newrefname
);
1279 files_reflog_path(refs
, &tmp_renamed_log
, TMP_RENAMED_LOG
);
1281 log
= !lstat(sb_oldref
.buf
, &loginfo
);
1282 if (log
&& S_ISLNK(loginfo
.st_mode
)) {
1283 ret
= error("reflog for %s is a symlink", oldrefname
);
1287 if (!refs_resolve_ref_unsafe(&refs
->base
, oldrefname
,
1288 RESOLVE_REF_READING
| RESOLVE_REF_NO_RECURSE
,
1289 orig_oid
.hash
, &flag
)) {
1290 ret
= error("refname %s not found", oldrefname
);
1294 if (flag
& REF_ISSYMREF
) {
1296 ret
= error("refname %s is a symbolic ref, copying it is not supported",
1299 ret
= error("refname %s is a symbolic ref, renaming it is not supported",
1303 if (!refs_rename_ref_available(&refs
->base
, oldrefname
, newrefname
)) {
1308 if (!copy
&& log
&& rename(sb_oldref
.buf
, tmp_renamed_log
.buf
)) {
1309 ret
= error("unable to move logfile logs/%s to logs/"TMP_RENAMED_LOG
": %s",
1310 oldrefname
, strerror(errno
));
1314 if (copy
&& log
&& copy_file(tmp_renamed_log
.buf
, sb_oldref
.buf
, 0644)) {
1315 ret
= error("unable to copy logfile logs/%s to logs/"TMP_RENAMED_LOG
": %s",
1316 oldrefname
, strerror(errno
));
1320 if (!copy
&& refs_delete_ref(&refs
->base
, logmsg
, oldrefname
,
1321 orig_oid
.hash
, REF_NODEREF
)) {
1322 error("unable to delete old %s", oldrefname
);
1327 * Since we are doing a shallow lookup, oid is not the
1328 * correct value to pass to delete_ref as old_oid. But that
1329 * doesn't matter, because an old_oid check wouldn't add to
1330 * the safety anyway; we want to delete the reference whatever
1331 * its current value.
1333 if (!copy
&& !refs_read_ref_full(&refs
->base
, newrefname
,
1334 RESOLVE_REF_READING
| RESOLVE_REF_NO_RECURSE
,
1336 refs_delete_ref(&refs
->base
, NULL
, newrefname
,
1337 NULL
, REF_NODEREF
)) {
1338 if (errno
== EISDIR
) {
1339 struct strbuf path
= STRBUF_INIT
;
1342 files_ref_path(refs
, &path
, newrefname
);
1343 result
= remove_empty_directories(&path
);
1344 strbuf_release(&path
);
1347 error("Directory not empty: %s", newrefname
);
1351 error("unable to delete existing %s", newrefname
);
1356 if (log
&& rename_tmp_log(refs
, newrefname
))
1361 lock
= lock_ref_sha1_basic(refs
, newrefname
, NULL
, NULL
, NULL
,
1362 REF_NODEREF
, NULL
, &err
);
1365 error("unable to copy '%s' to '%s': %s", oldrefname
, newrefname
, err
.buf
);
1367 error("unable to rename '%s' to '%s': %s", oldrefname
, newrefname
, err
.buf
);
1368 strbuf_release(&err
);
1371 oidcpy(&lock
->old_oid
, &orig_oid
);
1373 if (write_ref_to_lockfile(lock
, &orig_oid
, &err
) ||
1374 commit_ref_update(refs
, lock
, &orig_oid
, logmsg
, &err
)) {
1375 error("unable to write current sha1 into %s: %s", newrefname
, err
.buf
);
1376 strbuf_release(&err
);
1384 lock
= lock_ref_sha1_basic(refs
, oldrefname
, NULL
, NULL
, NULL
,
1385 REF_NODEREF
, NULL
, &err
);
1387 error("unable to lock %s for rollback: %s", oldrefname
, err
.buf
);
1388 strbuf_release(&err
);
1392 flag
= log_all_ref_updates
;
1393 log_all_ref_updates
= LOG_REFS_NONE
;
1394 if (write_ref_to_lockfile(lock
, &orig_oid
, &err
) ||
1395 commit_ref_update(refs
, lock
, &orig_oid
, NULL
, &err
)) {
1396 error("unable to write current sha1 into %s: %s", oldrefname
, err
.buf
);
1397 strbuf_release(&err
);
1399 log_all_ref_updates
= flag
;
1402 if (logmoved
&& rename(sb_newref
.buf
, sb_oldref
.buf
))
1403 error("unable to restore logfile %s from %s: %s",
1404 oldrefname
, newrefname
, strerror(errno
));
1405 if (!logmoved
&& log
&&
1406 rename(tmp_renamed_log
.buf
, sb_oldref
.buf
))
1407 error("unable to restore logfile %s from logs/"TMP_RENAMED_LOG
": %s",
1408 oldrefname
, strerror(errno
));
1411 strbuf_release(&sb_newref
);
1412 strbuf_release(&sb_oldref
);
1413 strbuf_release(&tmp_renamed_log
);
1418 static int files_rename_ref(struct ref_store
*ref_store
,
1419 const char *oldrefname
, const char *newrefname
,
1422 return files_copy_or_rename_ref(ref_store
, oldrefname
,
1423 newrefname
, logmsg
, 0);
1426 static int files_copy_ref(struct ref_store
*ref_store
,
1427 const char *oldrefname
, const char *newrefname
,
1430 return files_copy_or_rename_ref(ref_store
, oldrefname
,
1431 newrefname
, logmsg
, 1);
1434 static int close_ref_gently(struct ref_lock
*lock
)
1436 if (close_lock_file_gently(&lock
->lk
))
1441 static int commit_ref(struct ref_lock
*lock
)
1443 char *path
= get_locked_file_path(&lock
->lk
);
1446 if (!lstat(path
, &st
) && S_ISDIR(st
.st_mode
)) {
1448 * There is a directory at the path we want to rename
1449 * the lockfile to. Hopefully it is empty; try to
1452 size_t len
= strlen(path
);
1453 struct strbuf sb_path
= STRBUF_INIT
;
1455 strbuf_attach(&sb_path
, path
, len
, len
);
1458 * If this fails, commit_lock_file() will also fail
1459 * and will report the problem.
1461 remove_empty_directories(&sb_path
);
1462 strbuf_release(&sb_path
);
1467 if (commit_lock_file(&lock
->lk
))
1472 static int open_or_create_logfile(const char *path
, void *cb
)
1476 *fd
= open(path
, O_APPEND
| O_WRONLY
| O_CREAT
, 0666);
1477 return (*fd
< 0) ? -1 : 0;
1481 * Create a reflog for a ref. If force_create = 0, only create the
1482 * reflog for certain refs (those for which should_autocreate_reflog
1483 * returns non-zero). Otherwise, create it regardless of the reference
1484 * name. If the logfile already existed or was created, return 0 and
1485 * set *logfd to the file descriptor opened for appending to the file.
1486 * If no logfile exists and we decided not to create one, return 0 and
1487 * set *logfd to -1. On failure, fill in *err, set *logfd to -1, and
1490 static int log_ref_setup(struct files_ref_store
*refs
,
1491 const char *refname
, int force_create
,
1492 int *logfd
, struct strbuf
*err
)
1494 struct strbuf logfile_sb
= STRBUF_INIT
;
1497 files_reflog_path(refs
, &logfile_sb
, refname
);
1498 logfile
= strbuf_detach(&logfile_sb
, NULL
);
1500 if (force_create
|| should_autocreate_reflog(refname
)) {
1501 if (raceproof_create_file(logfile
, open_or_create_logfile
, logfd
)) {
1502 if (errno
== ENOENT
)
1503 strbuf_addf(err
, "unable to create directory for '%s': "
1504 "%s", logfile
, strerror(errno
));
1505 else if (errno
== EISDIR
)
1506 strbuf_addf(err
, "there are still logs under '%s'",
1509 strbuf_addf(err
, "unable to append to '%s': %s",
1510 logfile
, strerror(errno
));
1515 *logfd
= open(logfile
, O_APPEND
| O_WRONLY
, 0666);
1517 if (errno
== ENOENT
|| errno
== EISDIR
) {
1519 * The logfile doesn't already exist,
1520 * but that is not an error; it only
1521 * means that we won't write log
1526 strbuf_addf(err
, "unable to append to '%s': %s",
1527 logfile
, strerror(errno
));
1534 adjust_shared_perm(logfile
);
1544 static int files_create_reflog(struct ref_store
*ref_store
,
1545 const char *refname
, int force_create
,
1548 struct files_ref_store
*refs
=
1549 files_downcast(ref_store
, REF_STORE_WRITE
, "create_reflog");
1552 if (log_ref_setup(refs
, refname
, force_create
, &fd
, err
))
1561 static int log_ref_write_fd(int fd
, const struct object_id
*old_oid
,
1562 const struct object_id
*new_oid
,
1563 const char *committer
, const char *msg
)
1565 int msglen
, written
;
1566 unsigned maxlen
, len
;
1569 msglen
= msg
? strlen(msg
) : 0;
1570 maxlen
= strlen(committer
) + msglen
+ 100;
1571 logrec
= xmalloc(maxlen
);
1572 len
= xsnprintf(logrec
, maxlen
, "%s %s %s\n",
1573 oid_to_hex(old_oid
),
1574 oid_to_hex(new_oid
),
1577 len
+= copy_reflog_msg(logrec
+ len
- 1, msg
) - 1;
1579 written
= len
<= maxlen
? write_in_full(fd
, logrec
, len
) : -1;
1587 static int files_log_ref_write(struct files_ref_store
*refs
,
1588 const char *refname
, const struct object_id
*old_oid
,
1589 const struct object_id
*new_oid
, const char *msg
,
1590 int flags
, struct strbuf
*err
)
1594 if (log_all_ref_updates
== LOG_REFS_UNSET
)
1595 log_all_ref_updates
= is_bare_repository() ? LOG_REFS_NONE
: LOG_REFS_NORMAL
;
1597 result
= log_ref_setup(refs
, refname
,
1598 flags
& REF_FORCE_CREATE_REFLOG
,
1606 result
= log_ref_write_fd(logfd
, old_oid
, new_oid
,
1607 git_committer_info(0), msg
);
1609 struct strbuf sb
= STRBUF_INIT
;
1610 int save_errno
= errno
;
1612 files_reflog_path(refs
, &sb
, refname
);
1613 strbuf_addf(err
, "unable to append to '%s': %s",
1614 sb
.buf
, strerror(save_errno
));
1615 strbuf_release(&sb
);
1620 struct strbuf sb
= STRBUF_INIT
;
1621 int save_errno
= errno
;
1623 files_reflog_path(refs
, &sb
, refname
);
1624 strbuf_addf(err
, "unable to append to '%s': %s",
1625 sb
.buf
, strerror(save_errno
));
1626 strbuf_release(&sb
);
1633 * Write sha1 into the open lockfile, then close the lockfile. On
1634 * errors, rollback the lockfile, fill in *err and
1637 static int write_ref_to_lockfile(struct ref_lock
*lock
,
1638 const struct object_id
*oid
, struct strbuf
*err
)
1640 static char term
= '\n';
1644 o
= parse_object(oid
);
1647 "trying to write ref '%s' with nonexistent object %s",
1648 lock
->ref_name
, oid_to_hex(oid
));
1652 if (o
->type
!= OBJ_COMMIT
&& is_branch(lock
->ref_name
)) {
1654 "trying to write non-commit object %s to branch '%s'",
1655 oid_to_hex(oid
), lock
->ref_name
);
1659 fd
= get_lock_file_fd(&lock
->lk
);
1660 if (write_in_full(fd
, oid_to_hex(oid
), GIT_SHA1_HEXSZ
) < 0 ||
1661 write_in_full(fd
, &term
, 1) < 0 ||
1662 close_ref_gently(lock
) < 0) {
1664 "couldn't write '%s'", get_lock_file_path(&lock
->lk
));
1672 * Commit a change to a loose reference that has already been written
1673 * to the loose reference lockfile. Also update the reflogs if
1674 * necessary, using the specified lockmsg (which can be NULL).
1676 static int commit_ref_update(struct files_ref_store
*refs
,
1677 struct ref_lock
*lock
,
1678 const struct object_id
*oid
, const char *logmsg
,
1681 files_assert_main_repository(refs
, "commit_ref_update");
1683 clear_loose_ref_cache(refs
);
1684 if (files_log_ref_write(refs
, lock
->ref_name
,
1685 &lock
->old_oid
, oid
,
1687 char *old_msg
= strbuf_detach(err
, NULL
);
1688 strbuf_addf(err
, "cannot update the ref '%s': %s",
1689 lock
->ref_name
, old_msg
);
1695 if (strcmp(lock
->ref_name
, "HEAD") != 0) {
1697 * Special hack: If a branch is updated directly and HEAD
1698 * points to it (may happen on the remote side of a push
1699 * for example) then logically the HEAD reflog should be
1701 * A generic solution implies reverse symref information,
1702 * but finding all symrefs pointing to the given branch
1703 * would be rather costly for this rare event (the direct
1704 * update of a branch) to be worth it. So let's cheat and
1705 * check with HEAD only which should cover 99% of all usage
1706 * scenarios (even 100% of the default ones).
1709 const char *head_ref
;
1711 head_ref
= refs_resolve_ref_unsafe(&refs
->base
, "HEAD",
1712 RESOLVE_REF_READING
,
1714 if (head_ref
&& (head_flag
& REF_ISSYMREF
) &&
1715 !strcmp(head_ref
, lock
->ref_name
)) {
1716 struct strbuf log_err
= STRBUF_INIT
;
1717 if (files_log_ref_write(refs
, "HEAD",
1718 &lock
->old_oid
, oid
,
1719 logmsg
, 0, &log_err
)) {
1720 error("%s", log_err
.buf
);
1721 strbuf_release(&log_err
);
1726 if (commit_ref(lock
)) {
1727 strbuf_addf(err
, "couldn't set '%s'", lock
->ref_name
);
1736 static int create_ref_symlink(struct ref_lock
*lock
, const char *target
)
1739 #ifndef NO_SYMLINK_HEAD
1740 char *ref_path
= get_locked_file_path(&lock
->lk
);
1742 ret
= symlink(target
, ref_path
);
1746 fprintf(stderr
, "no symlink - falling back to symbolic ref\n");
1751 static void update_symref_reflog(struct files_ref_store
*refs
,
1752 struct ref_lock
*lock
, const char *refname
,
1753 const char *target
, const char *logmsg
)
1755 struct strbuf err
= STRBUF_INIT
;
1756 struct object_id new_oid
;
1758 !refs_read_ref_full(&refs
->base
, target
,
1759 RESOLVE_REF_READING
, new_oid
.hash
, NULL
) &&
1760 files_log_ref_write(refs
, refname
, &lock
->old_oid
,
1761 &new_oid
, logmsg
, 0, &err
)) {
1762 error("%s", err
.buf
);
1763 strbuf_release(&err
);
1767 static int create_symref_locked(struct files_ref_store
*refs
,
1768 struct ref_lock
*lock
, const char *refname
,
1769 const char *target
, const char *logmsg
)
1771 if (prefer_symlink_refs
&& !create_ref_symlink(lock
, target
)) {
1772 update_symref_reflog(refs
, lock
, refname
, target
, logmsg
);
1776 if (!fdopen_lock_file(&lock
->lk
, "w"))
1777 return error("unable to fdopen %s: %s",
1778 lock
->lk
.tempfile
->filename
.buf
, strerror(errno
));
1780 update_symref_reflog(refs
, lock
, refname
, target
, logmsg
);
1782 /* no error check; commit_ref will check ferror */
1783 fprintf(lock
->lk
.tempfile
->fp
, "ref: %s\n", target
);
1784 if (commit_ref(lock
) < 0)
1785 return error("unable to write symref for %s: %s", refname
,
1790 static int files_create_symref(struct ref_store
*ref_store
,
1791 const char *refname
, const char *target
,
1794 struct files_ref_store
*refs
=
1795 files_downcast(ref_store
, REF_STORE_WRITE
, "create_symref");
1796 struct strbuf err
= STRBUF_INIT
;
1797 struct ref_lock
*lock
;
1800 lock
= lock_ref_sha1_basic(refs
, refname
, NULL
,
1801 NULL
, NULL
, REF_NODEREF
, NULL
,
1804 error("%s", err
.buf
);
1805 strbuf_release(&err
);
1809 ret
= create_symref_locked(refs
, lock
, refname
, target
, logmsg
);
1814 static int files_reflog_exists(struct ref_store
*ref_store
,
1815 const char *refname
)
1817 struct files_ref_store
*refs
=
1818 files_downcast(ref_store
, REF_STORE_READ
, "reflog_exists");
1819 struct strbuf sb
= STRBUF_INIT
;
1823 files_reflog_path(refs
, &sb
, refname
);
1824 ret
= !lstat(sb
.buf
, &st
) && S_ISREG(st
.st_mode
);
1825 strbuf_release(&sb
);
1829 static int files_delete_reflog(struct ref_store
*ref_store
,
1830 const char *refname
)
1832 struct files_ref_store
*refs
=
1833 files_downcast(ref_store
, REF_STORE_WRITE
, "delete_reflog");
1834 struct strbuf sb
= STRBUF_INIT
;
1837 files_reflog_path(refs
, &sb
, refname
);
1838 ret
= remove_path(sb
.buf
);
1839 strbuf_release(&sb
);
1843 static int show_one_reflog_ent(struct strbuf
*sb
, each_reflog_ent_fn fn
, void *cb_data
)
1845 struct object_id ooid
, noid
;
1846 char *email_end
, *message
;
1847 timestamp_t timestamp
;
1849 const char *p
= sb
->buf
;
1851 /* old SP new SP name <email> SP time TAB msg LF */
1852 if (!sb
->len
|| sb
->buf
[sb
->len
- 1] != '\n' ||
1853 parse_oid_hex(p
, &ooid
, &p
) || *p
++ != ' ' ||
1854 parse_oid_hex(p
, &noid
, &p
) || *p
++ != ' ' ||
1855 !(email_end
= strchr(p
, '>')) ||
1856 email_end
[1] != ' ' ||
1857 !(timestamp
= parse_timestamp(email_end
+ 2, &message
, 10)) ||
1858 !message
|| message
[0] != ' ' ||
1859 (message
[1] != '+' && message
[1] != '-') ||
1860 !isdigit(message
[2]) || !isdigit(message
[3]) ||
1861 !isdigit(message
[4]) || !isdigit(message
[5]))
1862 return 0; /* corrupt? */
1863 email_end
[1] = '\0';
1864 tz
= strtol(message
+ 1, NULL
, 10);
1865 if (message
[6] != '\t')
1869 return fn(&ooid
, &noid
, p
, timestamp
, tz
, message
, cb_data
);
1872 static char *find_beginning_of_line(char *bob
, char *scan
)
1874 while (bob
< scan
&& *(--scan
) != '\n')
1875 ; /* keep scanning backwards */
1877 * Return either beginning of the buffer, or LF at the end of
1878 * the previous line.
1883 static int files_for_each_reflog_ent_reverse(struct ref_store
*ref_store
,
1884 const char *refname
,
1885 each_reflog_ent_fn fn
,
1888 struct files_ref_store
*refs
=
1889 files_downcast(ref_store
, REF_STORE_READ
,
1890 "for_each_reflog_ent_reverse");
1891 struct strbuf sb
= STRBUF_INIT
;
1894 int ret
= 0, at_tail
= 1;
1896 files_reflog_path(refs
, &sb
, refname
);
1897 logfp
= fopen(sb
.buf
, "r");
1898 strbuf_release(&sb
);
1902 /* Jump to the end */
1903 if (fseek(logfp
, 0, SEEK_END
) < 0)
1904 ret
= error("cannot seek back reflog for %s: %s",
1905 refname
, strerror(errno
));
1907 while (!ret
&& 0 < pos
) {
1913 /* Fill next block from the end */
1914 cnt
= (sizeof(buf
) < pos
) ? sizeof(buf
) : pos
;
1915 if (fseek(logfp
, pos
- cnt
, SEEK_SET
)) {
1916 ret
= error("cannot seek back reflog for %s: %s",
1917 refname
, strerror(errno
));
1920 nread
= fread(buf
, cnt
, 1, logfp
);
1922 ret
= error("cannot read %d bytes from reflog for %s: %s",
1923 cnt
, refname
, strerror(errno
));
1928 scanp
= endp
= buf
+ cnt
;
1929 if (at_tail
&& scanp
[-1] == '\n')
1930 /* Looking at the final LF at the end of the file */
1934 while (buf
< scanp
) {
1936 * terminating LF of the previous line, or the beginning
1941 bp
= find_beginning_of_line(buf
, scanp
);
1945 * The newline is the end of the previous line,
1946 * so we know we have complete line starting
1947 * at (bp + 1). Prefix it onto any prior data
1948 * we collected for the line and process it.
1950 strbuf_splice(&sb
, 0, 0, bp
+ 1, endp
- (bp
+ 1));
1953 ret
= show_one_reflog_ent(&sb
, fn
, cb_data
);
1959 * We are at the start of the buffer, and the
1960 * start of the file; there is no previous
1961 * line, and we have everything for this one.
1962 * Process it, and we can end the loop.
1964 strbuf_splice(&sb
, 0, 0, buf
, endp
- buf
);
1965 ret
= show_one_reflog_ent(&sb
, fn
, cb_data
);
1972 * We are at the start of the buffer, and there
1973 * is more file to read backwards. Which means
1974 * we are in the middle of a line. Note that we
1975 * may get here even if *bp was a newline; that
1976 * just means we are at the exact end of the
1977 * previous line, rather than some spot in the
1980 * Save away what we have to be combined with
1981 * the data from the next read.
1983 strbuf_splice(&sb
, 0, 0, buf
, endp
- buf
);
1990 die("BUG: reverse reflog parser had leftover data");
1993 strbuf_release(&sb
);
1997 static int files_for_each_reflog_ent(struct ref_store
*ref_store
,
1998 const char *refname
,
1999 each_reflog_ent_fn fn
, void *cb_data
)
2001 struct files_ref_store
*refs
=
2002 files_downcast(ref_store
, REF_STORE_READ
,
2003 "for_each_reflog_ent");
2005 struct strbuf sb
= STRBUF_INIT
;
2008 files_reflog_path(refs
, &sb
, refname
);
2009 logfp
= fopen(sb
.buf
, "r");
2010 strbuf_release(&sb
);
2014 while (!ret
&& !strbuf_getwholeline(&sb
, logfp
, '\n'))
2015 ret
= show_one_reflog_ent(&sb
, fn
, cb_data
);
2017 strbuf_release(&sb
);
2021 struct files_reflog_iterator
{
2022 struct ref_iterator base
;
2024 struct ref_store
*ref_store
;
2025 struct dir_iterator
*dir_iterator
;
2026 struct object_id oid
;
2029 static int files_reflog_iterator_advance(struct ref_iterator
*ref_iterator
)
2031 struct files_reflog_iterator
*iter
=
2032 (struct files_reflog_iterator
*)ref_iterator
;
2033 struct dir_iterator
*diter
= iter
->dir_iterator
;
2036 while ((ok
= dir_iterator_advance(diter
)) == ITER_OK
) {
2039 if (!S_ISREG(diter
->st
.st_mode
))
2041 if (diter
->basename
[0] == '.')
2043 if (ends_with(diter
->basename
, ".lock"))
2046 if (refs_read_ref_full(iter
->ref_store
,
2047 diter
->relative_path
, 0,
2048 iter
->oid
.hash
, &flags
)) {
2049 error("bad ref for %s", diter
->path
.buf
);
2053 iter
->base
.refname
= diter
->relative_path
;
2054 iter
->base
.oid
= &iter
->oid
;
2055 iter
->base
.flags
= flags
;
2059 iter
->dir_iterator
= NULL
;
2060 if (ref_iterator_abort(ref_iterator
) == ITER_ERROR
)
2065 static int files_reflog_iterator_peel(struct ref_iterator
*ref_iterator
,
2066 struct object_id
*peeled
)
2068 die("BUG: ref_iterator_peel() called for reflog_iterator");
2071 static int files_reflog_iterator_abort(struct ref_iterator
*ref_iterator
)
2073 struct files_reflog_iterator
*iter
=
2074 (struct files_reflog_iterator
*)ref_iterator
;
2077 if (iter
->dir_iterator
)
2078 ok
= dir_iterator_abort(iter
->dir_iterator
);
2080 base_ref_iterator_free(ref_iterator
);
2084 static struct ref_iterator_vtable files_reflog_iterator_vtable
= {
2085 files_reflog_iterator_advance
,
2086 files_reflog_iterator_peel
,
2087 files_reflog_iterator_abort
2090 static struct ref_iterator
*reflog_iterator_begin(struct ref_store
*ref_store
,
2093 struct files_reflog_iterator
*iter
= xcalloc(1, sizeof(*iter
));
2094 struct ref_iterator
*ref_iterator
= &iter
->base
;
2095 struct strbuf sb
= STRBUF_INIT
;
2097 base_ref_iterator_init(ref_iterator
, &files_reflog_iterator_vtable
);
2098 strbuf_addf(&sb
, "%s/logs", gitdir
);
2099 iter
->dir_iterator
= dir_iterator_begin(sb
.buf
);
2100 iter
->ref_store
= ref_store
;
2101 strbuf_release(&sb
);
2103 return ref_iterator
;
2106 static enum iterator_selection
reflog_iterator_select(
2107 struct ref_iterator
*iter_worktree
,
2108 struct ref_iterator
*iter_common
,
2111 if (iter_worktree
) {
2113 * We're a bit loose here. We probably should ignore
2114 * common refs if they are accidentally added as
2115 * per-worktree refs.
2117 return ITER_SELECT_0
;
2118 } else if (iter_common
) {
2119 if (ref_type(iter_common
->refname
) == REF_TYPE_NORMAL
)
2120 return ITER_SELECT_1
;
2123 * The main ref store may contain main worktree's
2124 * per-worktree refs, which should be ignored
2131 static struct ref_iterator
*files_reflog_iterator_begin(struct ref_store
*ref_store
)
2133 struct files_ref_store
*refs
=
2134 files_downcast(ref_store
, REF_STORE_READ
,
2135 "reflog_iterator_begin");
2137 if (!strcmp(refs
->gitdir
, refs
->gitcommondir
)) {
2138 return reflog_iterator_begin(ref_store
, refs
->gitcommondir
);
2140 return merge_ref_iterator_begin(
2141 reflog_iterator_begin(ref_store
, refs
->gitdir
),
2142 reflog_iterator_begin(ref_store
, refs
->gitcommondir
),
2143 reflog_iterator_select
, refs
);
2148 * If update is a direct update of head_ref (the reference pointed to
2149 * by HEAD), then add an extra REF_LOG_ONLY update for HEAD.
2151 static int split_head_update(struct ref_update
*update
,
2152 struct ref_transaction
*transaction
,
2153 const char *head_ref
,
2154 struct string_list
*affected_refnames
,
2157 struct string_list_item
*item
;
2158 struct ref_update
*new_update
;
2160 if ((update
->flags
& REF_LOG_ONLY
) ||
2161 (update
->flags
& REF_ISPRUNING
) ||
2162 (update
->flags
& REF_UPDATE_VIA_HEAD
))
2165 if (strcmp(update
->refname
, head_ref
))
2169 * First make sure that HEAD is not already in the
2170 * transaction. This check is O(lg N) in the transaction
2171 * size, but it happens at most once per transaction.
2173 if (string_list_has_string(affected_refnames
, "HEAD")) {
2174 /* An entry already existed */
2176 "multiple updates for 'HEAD' (including one "
2177 "via its referent '%s') are not allowed",
2179 return TRANSACTION_NAME_CONFLICT
;
2182 new_update
= ref_transaction_add_update(
2183 transaction
, "HEAD",
2184 update
->flags
| REF_LOG_ONLY
| REF_NODEREF
,
2185 update
->new_oid
.hash
, update
->old_oid
.hash
,
2189 * Add "HEAD". This insertion is O(N) in the transaction
2190 * size, but it happens at most once per transaction.
2191 * Add new_update->refname instead of a literal "HEAD".
2193 if (strcmp(new_update
->refname
, "HEAD"))
2194 BUG("%s unexpectedly not 'HEAD'", new_update
->refname
);
2195 item
= string_list_insert(affected_refnames
, new_update
->refname
);
2196 item
->util
= new_update
;
2202 * update is for a symref that points at referent and doesn't have
2203 * REF_NODEREF set. Split it into two updates:
2204 * - The original update, but with REF_LOG_ONLY and REF_NODEREF set
2205 * - A new, separate update for the referent reference
2206 * Note that the new update will itself be subject to splitting when
2207 * the iteration gets to it.
2209 static int split_symref_update(struct files_ref_store
*refs
,
2210 struct ref_update
*update
,
2211 const char *referent
,
2212 struct ref_transaction
*transaction
,
2213 struct string_list
*affected_refnames
,
2216 struct string_list_item
*item
;
2217 struct ref_update
*new_update
;
2218 unsigned int new_flags
;
2221 * First make sure that referent is not already in the
2222 * transaction. This check is O(lg N) in the transaction
2223 * size, but it happens at most once per symref in a
2226 if (string_list_has_string(affected_refnames
, referent
)) {
2227 /* An entry already exists */
2229 "multiple updates for '%s' (including one "
2230 "via symref '%s') are not allowed",
2231 referent
, update
->refname
);
2232 return TRANSACTION_NAME_CONFLICT
;
2235 new_flags
= update
->flags
;
2236 if (!strcmp(update
->refname
, "HEAD")) {
2238 * Record that the new update came via HEAD, so that
2239 * when we process it, split_head_update() doesn't try
2240 * to add another reflog update for HEAD. Note that
2241 * this bit will be propagated if the new_update
2242 * itself needs to be split.
2244 new_flags
|= REF_UPDATE_VIA_HEAD
;
2247 new_update
= ref_transaction_add_update(
2248 transaction
, referent
, new_flags
,
2249 update
->new_oid
.hash
, update
->old_oid
.hash
,
2252 new_update
->parent_update
= update
;
2255 * Change the symbolic ref update to log only. Also, it
2256 * doesn't need to check its old SHA-1 value, as that will be
2257 * done when new_update is processed.
2259 update
->flags
|= REF_LOG_ONLY
| REF_NODEREF
;
2260 update
->flags
&= ~REF_HAVE_OLD
;
2263 * Add the referent. This insertion is O(N) in the transaction
2264 * size, but it happens at most once per symref in a
2265 * transaction. Make sure to add new_update->refname, which will
2266 * be valid as long as affected_refnames is in use, and NOT
2267 * referent, which might soon be freed by our caller.
2269 item
= string_list_insert(affected_refnames
, new_update
->refname
);
2271 BUG("%s unexpectedly found in affected_refnames",
2272 new_update
->refname
);
2273 item
->util
= new_update
;
2279 * Return the refname under which update was originally requested.
2281 static const char *original_update_refname(struct ref_update
*update
)
2283 while (update
->parent_update
)
2284 update
= update
->parent_update
;
2286 return update
->refname
;
2290 * Check whether the REF_HAVE_OLD and old_oid values stored in update
2291 * are consistent with oid, which is the reference's current value. If
2292 * everything is OK, return 0; otherwise, write an error message to
2293 * err and return -1.
2295 static int check_old_oid(struct ref_update
*update
, struct object_id
*oid
,
2298 if (!(update
->flags
& REF_HAVE_OLD
) ||
2299 !oidcmp(oid
, &update
->old_oid
))
2302 if (is_null_oid(&update
->old_oid
))
2303 strbuf_addf(err
, "cannot lock ref '%s': "
2304 "reference already exists",
2305 original_update_refname(update
));
2306 else if (is_null_oid(oid
))
2307 strbuf_addf(err
, "cannot lock ref '%s': "
2308 "reference is missing but expected %s",
2309 original_update_refname(update
),
2310 oid_to_hex(&update
->old_oid
));
2312 strbuf_addf(err
, "cannot lock ref '%s': "
2313 "is at %s but expected %s",
2314 original_update_refname(update
),
2316 oid_to_hex(&update
->old_oid
));
2322 * Prepare for carrying out update:
2323 * - Lock the reference referred to by update.
2324 * - Read the reference under lock.
2325 * - Check that its old SHA-1 value (if specified) is correct, and in
2326 * any case record it in update->lock->old_oid for later use when
2327 * writing the reflog.
2328 * - If it is a symref update without REF_NODEREF, split it up into a
2329 * REF_LOG_ONLY update of the symref and add a separate update for
2330 * the referent to transaction.
2331 * - If it is an update of head_ref, add a corresponding REF_LOG_ONLY
2334 static int lock_ref_for_update(struct files_ref_store
*refs
,
2335 struct ref_update
*update
,
2336 struct ref_transaction
*transaction
,
2337 const char *head_ref
,
2338 struct string_list
*affected_refnames
,
2341 struct strbuf referent
= STRBUF_INIT
;
2342 int mustexist
= (update
->flags
& REF_HAVE_OLD
) &&
2343 !is_null_oid(&update
->old_oid
);
2345 struct ref_lock
*lock
;
2347 files_assert_main_repository(refs
, "lock_ref_for_update");
2349 if ((update
->flags
& REF_HAVE_NEW
) && is_null_oid(&update
->new_oid
))
2350 update
->flags
|= REF_DELETING
;
2353 ret
= split_head_update(update
, transaction
, head_ref
,
2354 affected_refnames
, err
);
2359 ret
= lock_raw_ref(refs
, update
->refname
, mustexist
,
2360 affected_refnames
, NULL
,
2362 &update
->type
, err
);
2366 reason
= strbuf_detach(err
, NULL
);
2367 strbuf_addf(err
, "cannot lock ref '%s': %s",
2368 original_update_refname(update
), reason
);
2373 update
->backend_data
= lock
;
2375 if (update
->type
& REF_ISSYMREF
) {
2376 if (update
->flags
& REF_NODEREF
) {
2378 * We won't be reading the referent as part of
2379 * the transaction, so we have to read it here
2380 * to record and possibly check old_sha1:
2382 if (refs_read_ref_full(&refs
->base
,
2384 lock
->old_oid
.hash
, NULL
)) {
2385 if (update
->flags
& REF_HAVE_OLD
) {
2386 strbuf_addf(err
, "cannot lock ref '%s': "
2387 "error reading reference",
2388 original_update_refname(update
));
2389 ret
= TRANSACTION_GENERIC_ERROR
;
2392 } else if (check_old_oid(update
, &lock
->old_oid
, err
)) {
2393 ret
= TRANSACTION_GENERIC_ERROR
;
2398 * Create a new update for the reference this
2399 * symref is pointing at. Also, we will record
2400 * and verify old_sha1 for this update as part
2401 * of processing the split-off update, so we
2402 * don't have to do it here.
2404 ret
= split_symref_update(refs
, update
,
2405 referent
.buf
, transaction
,
2406 affected_refnames
, err
);
2411 struct ref_update
*parent_update
;
2413 if (check_old_oid(update
, &lock
->old_oid
, err
)) {
2414 ret
= TRANSACTION_GENERIC_ERROR
;
2419 * If this update is happening indirectly because of a
2420 * symref update, record the old SHA-1 in the parent
2423 for (parent_update
= update
->parent_update
;
2425 parent_update
= parent_update
->parent_update
) {
2426 struct ref_lock
*parent_lock
= parent_update
->backend_data
;
2427 oidcpy(&parent_lock
->old_oid
, &lock
->old_oid
);
2431 if ((update
->flags
& REF_HAVE_NEW
) &&
2432 !(update
->flags
& REF_DELETING
) &&
2433 !(update
->flags
& REF_LOG_ONLY
)) {
2434 if (!(update
->type
& REF_ISSYMREF
) &&
2435 !oidcmp(&lock
->old_oid
, &update
->new_oid
)) {
2437 * The reference already has the desired
2438 * value, so we don't need to write it.
2440 } else if (write_ref_to_lockfile(lock
, &update
->new_oid
,
2442 char *write_err
= strbuf_detach(err
, NULL
);
2445 * The lock was freed upon failure of
2446 * write_ref_to_lockfile():
2448 update
->backend_data
= NULL
;
2450 "cannot update ref '%s': %s",
2451 update
->refname
, write_err
);
2453 ret
= TRANSACTION_GENERIC_ERROR
;
2456 update
->flags
|= REF_NEEDS_COMMIT
;
2459 if (!(update
->flags
& REF_NEEDS_COMMIT
)) {
2461 * We didn't call write_ref_to_lockfile(), so
2462 * the lockfile is still open. Close it to
2463 * free up the file descriptor:
2465 if (close_ref_gently(lock
)) {
2466 strbuf_addf(err
, "couldn't close '%s.lock'",
2468 ret
= TRANSACTION_GENERIC_ERROR
;
2474 strbuf_release(&referent
);
2478 struct files_transaction_backend_data
{
2479 struct ref_transaction
*packed_transaction
;
2480 int packed_refs_locked
;
2484 * Unlock any references in `transaction` that are still locked, and
2485 * mark the transaction closed.
2487 static void files_transaction_cleanup(struct files_ref_store
*refs
,
2488 struct ref_transaction
*transaction
)
2491 struct files_transaction_backend_data
*backend_data
=
2492 transaction
->backend_data
;
2493 struct strbuf err
= STRBUF_INIT
;
2495 for (i
= 0; i
< transaction
->nr
; i
++) {
2496 struct ref_update
*update
= transaction
->updates
[i
];
2497 struct ref_lock
*lock
= update
->backend_data
;
2501 update
->backend_data
= NULL
;
2505 if (backend_data
->packed_transaction
&&
2506 ref_transaction_abort(backend_data
->packed_transaction
, &err
)) {
2507 error("error aborting transaction: %s", err
.buf
);
2508 strbuf_release(&err
);
2511 if (backend_data
->packed_refs_locked
)
2512 packed_refs_unlock(refs
->packed_ref_store
);
2516 transaction
->state
= REF_TRANSACTION_CLOSED
;
2519 static int files_transaction_prepare(struct ref_store
*ref_store
,
2520 struct ref_transaction
*transaction
,
2523 struct files_ref_store
*refs
=
2524 files_downcast(ref_store
, REF_STORE_WRITE
,
2525 "ref_transaction_prepare");
2528 struct string_list affected_refnames
= STRING_LIST_INIT_NODUP
;
2529 char *head_ref
= NULL
;
2531 struct object_id head_oid
;
2532 struct files_transaction_backend_data
*backend_data
;
2533 struct ref_transaction
*packed_transaction
= NULL
;
2537 if (!transaction
->nr
)
2540 backend_data
= xcalloc(1, sizeof(*backend_data
));
2541 transaction
->backend_data
= backend_data
;
2544 * Fail if a refname appears more than once in the
2545 * transaction. (If we end up splitting up any updates using
2546 * split_symref_update() or split_head_update(), those
2547 * functions will check that the new updates don't have the
2548 * same refname as any existing ones.)
2550 for (i
= 0; i
< transaction
->nr
; i
++) {
2551 struct ref_update
*update
= transaction
->updates
[i
];
2552 struct string_list_item
*item
=
2553 string_list_append(&affected_refnames
, update
->refname
);
2556 * We store a pointer to update in item->util, but at
2557 * the moment we never use the value of this field
2558 * except to check whether it is non-NULL.
2560 item
->util
= update
;
2562 string_list_sort(&affected_refnames
);
2563 if (ref_update_reject_duplicates(&affected_refnames
, err
)) {
2564 ret
= TRANSACTION_GENERIC_ERROR
;
2569 * Special hack: If a branch is updated directly and HEAD
2570 * points to it (may happen on the remote side of a push
2571 * for example) then logically the HEAD reflog should be
2574 * A generic solution would require reverse symref lookups,
2575 * but finding all symrefs pointing to a given branch would be
2576 * rather costly for this rare event (the direct update of a
2577 * branch) to be worth it. So let's cheat and check with HEAD
2578 * only, which should cover 99% of all usage scenarios (even
2579 * 100% of the default ones).
2581 * So if HEAD is a symbolic reference, then record the name of
2582 * the reference that it points to. If we see an update of
2583 * head_ref within the transaction, then split_head_update()
2584 * arranges for the reflog of HEAD to be updated, too.
2586 head_ref
= refs_resolve_refdup(ref_store
, "HEAD",
2587 RESOLVE_REF_NO_RECURSE
,
2588 head_oid
.hash
, &head_type
);
2590 if (head_ref
&& !(head_type
& REF_ISSYMREF
)) {
2591 FREE_AND_NULL(head_ref
);
2595 * Acquire all locks, verify old values if provided, check
2596 * that new values are valid, and write new values to the
2597 * lockfiles, ready to be activated. Only keep one lockfile
2598 * open at a time to avoid running out of file descriptors.
2599 * Note that lock_ref_for_update() might append more updates
2600 * to the transaction.
2602 for (i
= 0; i
< transaction
->nr
; i
++) {
2603 struct ref_update
*update
= transaction
->updates
[i
];
2605 ret
= lock_ref_for_update(refs
, update
, transaction
,
2606 head_ref
, &affected_refnames
, err
);
2610 if (update
->flags
& REF_DELETING
&&
2611 !(update
->flags
& REF_LOG_ONLY
) &&
2612 !(update
->flags
& REF_ISPRUNING
)) {
2614 * This reference has to be deleted from
2615 * packed-refs if it exists there.
2617 if (!packed_transaction
) {
2618 packed_transaction
= ref_store_transaction_begin(
2619 refs
->packed_ref_store
, err
);
2620 if (!packed_transaction
) {
2621 ret
= TRANSACTION_GENERIC_ERROR
;
2625 backend_data
->packed_transaction
=
2629 ref_transaction_add_update(
2630 packed_transaction
, update
->refname
,
2631 update
->flags
& ~REF_HAVE_OLD
,
2632 update
->new_oid
.hash
, update
->old_oid
.hash
,
2637 if (packed_transaction
) {
2638 if (packed_refs_lock(refs
->packed_ref_store
, 0, err
)) {
2639 ret
= TRANSACTION_GENERIC_ERROR
;
2642 backend_data
->packed_refs_locked
= 1;
2643 ret
= ref_transaction_prepare(packed_transaction
, err
);
2648 string_list_clear(&affected_refnames
, 0);
2651 files_transaction_cleanup(refs
, transaction
);
2653 transaction
->state
= REF_TRANSACTION_PREPARED
;
2658 static int files_transaction_finish(struct ref_store
*ref_store
,
2659 struct ref_transaction
*transaction
,
2662 struct files_ref_store
*refs
=
2663 files_downcast(ref_store
, 0, "ref_transaction_finish");
2666 struct strbuf sb
= STRBUF_INIT
;
2667 struct files_transaction_backend_data
*backend_data
;
2668 struct ref_transaction
*packed_transaction
;
2673 if (!transaction
->nr
) {
2674 transaction
->state
= REF_TRANSACTION_CLOSED
;
2678 backend_data
= transaction
->backend_data
;
2679 packed_transaction
= backend_data
->packed_transaction
;
2681 /* Perform updates first so live commits remain referenced */
2682 for (i
= 0; i
< transaction
->nr
; i
++) {
2683 struct ref_update
*update
= transaction
->updates
[i
];
2684 struct ref_lock
*lock
= update
->backend_data
;
2686 if (update
->flags
& REF_NEEDS_COMMIT
||
2687 update
->flags
& REF_LOG_ONLY
) {
2688 if (files_log_ref_write(refs
,
2692 update
->msg
, update
->flags
,
2694 char *old_msg
= strbuf_detach(err
, NULL
);
2696 strbuf_addf(err
, "cannot update the ref '%s': %s",
2697 lock
->ref_name
, old_msg
);
2700 update
->backend_data
= NULL
;
2701 ret
= TRANSACTION_GENERIC_ERROR
;
2705 if (update
->flags
& REF_NEEDS_COMMIT
) {
2706 clear_loose_ref_cache(refs
);
2707 if (commit_ref(lock
)) {
2708 strbuf_addf(err
, "couldn't set '%s'", lock
->ref_name
);
2710 update
->backend_data
= NULL
;
2711 ret
= TRANSACTION_GENERIC_ERROR
;
2718 * Now that updates are safely completed, we can perform
2719 * deletes. First delete the reflogs of any references that
2720 * will be deleted, since (in the unexpected event of an
2721 * error) leaving a reference without a reflog is less bad
2722 * than leaving a reflog without a reference (the latter is a
2723 * mildly invalid repository state):
2725 for (i
= 0; i
< transaction
->nr
; i
++) {
2726 struct ref_update
*update
= transaction
->updates
[i
];
2727 if (update
->flags
& REF_DELETING
&&
2728 !(update
->flags
& REF_LOG_ONLY
) &&
2729 !(update
->flags
& REF_ISPRUNING
)) {
2731 files_reflog_path(refs
, &sb
, update
->refname
);
2732 if (!unlink_or_warn(sb
.buf
))
2733 try_remove_empty_parents(refs
, update
->refname
,
2734 REMOVE_EMPTY_PARENTS_REFLOG
);
2739 * Perform deletes now that updates are safely completed.
2741 * First delete any packed versions of the references, while
2742 * retaining the packed-refs lock:
2744 if (packed_transaction
) {
2745 ret
= ref_transaction_commit(packed_transaction
, err
);
2746 ref_transaction_free(packed_transaction
);
2747 packed_transaction
= NULL
;
2748 backend_data
->packed_transaction
= NULL
;
2753 /* Now delete the loose versions of the references: */
2754 for (i
= 0; i
< transaction
->nr
; i
++) {
2755 struct ref_update
*update
= transaction
->updates
[i
];
2756 struct ref_lock
*lock
= update
->backend_data
;
2758 if (update
->flags
& REF_DELETING
&&
2759 !(update
->flags
& REF_LOG_ONLY
)) {
2760 if (!(update
->type
& REF_ISPACKED
) ||
2761 update
->type
& REF_ISSYMREF
) {
2762 /* It is a loose reference. */
2764 files_ref_path(refs
, &sb
, lock
->ref_name
);
2765 if (unlink_or_msg(sb
.buf
, err
)) {
2766 ret
= TRANSACTION_GENERIC_ERROR
;
2769 update
->flags
|= REF_DELETED_LOOSE
;
2774 clear_loose_ref_cache(refs
);
2777 files_transaction_cleanup(refs
, transaction
);
2779 for (i
= 0; i
< transaction
->nr
; i
++) {
2780 struct ref_update
*update
= transaction
->updates
[i
];
2782 if (update
->flags
& REF_DELETED_LOOSE
) {
2784 * The loose reference was deleted. Delete any
2785 * empty parent directories. (Note that this
2786 * can only work because we have already
2787 * removed the lockfile.)
2789 try_remove_empty_parents(refs
, update
->refname
,
2790 REMOVE_EMPTY_PARENTS_REF
);
2794 strbuf_release(&sb
);
2798 static int files_transaction_abort(struct ref_store
*ref_store
,
2799 struct ref_transaction
*transaction
,
2802 struct files_ref_store
*refs
=
2803 files_downcast(ref_store
, 0, "ref_transaction_abort");
2805 files_transaction_cleanup(refs
, transaction
);
2809 static int ref_present(const char *refname
,
2810 const struct object_id
*oid
, int flags
, void *cb_data
)
2812 struct string_list
*affected_refnames
= cb_data
;
2814 return string_list_has_string(affected_refnames
, refname
);
2817 static int files_initial_transaction_commit(struct ref_store
*ref_store
,
2818 struct ref_transaction
*transaction
,
2821 struct files_ref_store
*refs
=
2822 files_downcast(ref_store
, REF_STORE_WRITE
,
2823 "initial_ref_transaction_commit");
2826 struct string_list affected_refnames
= STRING_LIST_INIT_NODUP
;
2827 struct ref_transaction
*packed_transaction
= NULL
;
2831 if (transaction
->state
!= REF_TRANSACTION_OPEN
)
2832 die("BUG: commit called for transaction that is not open");
2834 /* Fail if a refname appears more than once in the transaction: */
2835 for (i
= 0; i
< transaction
->nr
; i
++)
2836 string_list_append(&affected_refnames
,
2837 transaction
->updates
[i
]->refname
);
2838 string_list_sort(&affected_refnames
);
2839 if (ref_update_reject_duplicates(&affected_refnames
, err
)) {
2840 ret
= TRANSACTION_GENERIC_ERROR
;
2845 * It's really undefined to call this function in an active
2846 * repository or when there are existing references: we are
2847 * only locking and changing packed-refs, so (1) any
2848 * simultaneous processes might try to change a reference at
2849 * the same time we do, and (2) any existing loose versions of
2850 * the references that we are setting would have precedence
2851 * over our values. But some remote helpers create the remote
2852 * "HEAD" and "master" branches before calling this function,
2853 * so here we really only check that none of the references
2854 * that we are creating already exists.
2856 if (refs_for_each_rawref(&refs
->base
, ref_present
,
2857 &affected_refnames
))
2858 die("BUG: initial ref transaction called with existing refs");
2860 packed_transaction
= ref_store_transaction_begin(refs
->packed_ref_store
, err
);
2861 if (!packed_transaction
) {
2862 ret
= TRANSACTION_GENERIC_ERROR
;
2866 for (i
= 0; i
< transaction
->nr
; i
++) {
2867 struct ref_update
*update
= transaction
->updates
[i
];
2869 if ((update
->flags
& REF_HAVE_OLD
) &&
2870 !is_null_oid(&update
->old_oid
))
2871 die("BUG: initial ref transaction with old_sha1 set");
2872 if (refs_verify_refname_available(&refs
->base
, update
->refname
,
2873 &affected_refnames
, NULL
,
2875 ret
= TRANSACTION_NAME_CONFLICT
;
2880 * Add a reference creation for this reference to the
2881 * packed-refs transaction:
2883 ref_transaction_add_update(packed_transaction
, update
->refname
,
2884 update
->flags
& ~REF_HAVE_OLD
,
2885 update
->new_oid
.hash
, update
->old_oid
.hash
,
2889 if (packed_refs_lock(refs
->packed_ref_store
, 0, err
)) {
2890 ret
= TRANSACTION_GENERIC_ERROR
;
2894 if (initial_ref_transaction_commit(packed_transaction
, err
)) {
2895 ret
= TRANSACTION_GENERIC_ERROR
;
2900 if (packed_transaction
)
2901 ref_transaction_free(packed_transaction
);
2902 packed_refs_unlock(refs
->packed_ref_store
);
2903 transaction
->state
= REF_TRANSACTION_CLOSED
;
2904 string_list_clear(&affected_refnames
, 0);
2908 struct expire_reflog_cb
{
2910 reflog_expiry_should_prune_fn
*should_prune_fn
;
2913 struct object_id last_kept_oid
;
2916 static int expire_reflog_ent(struct object_id
*ooid
, struct object_id
*noid
,
2917 const char *email
, timestamp_t timestamp
, int tz
,
2918 const char *message
, void *cb_data
)
2920 struct expire_reflog_cb
*cb
= cb_data
;
2921 struct expire_reflog_policy_cb
*policy_cb
= cb
->policy_cb
;
2923 if (cb
->flags
& EXPIRE_REFLOGS_REWRITE
)
2924 ooid
= &cb
->last_kept_oid
;
2926 if ((*cb
->should_prune_fn
)(ooid
, noid
, email
, timestamp
, tz
,
2927 message
, policy_cb
)) {
2929 printf("would prune %s", message
);
2930 else if (cb
->flags
& EXPIRE_REFLOGS_VERBOSE
)
2931 printf("prune %s", message
);
2934 fprintf(cb
->newlog
, "%s %s %s %"PRItime
" %+05d\t%s",
2935 oid_to_hex(ooid
), oid_to_hex(noid
),
2936 email
, timestamp
, tz
, message
);
2937 oidcpy(&cb
->last_kept_oid
, noid
);
2939 if (cb
->flags
& EXPIRE_REFLOGS_VERBOSE
)
2940 printf("keep %s", message
);
2945 static int files_reflog_expire(struct ref_store
*ref_store
,
2946 const char *refname
, const unsigned char *sha1
,
2948 reflog_expiry_prepare_fn prepare_fn
,
2949 reflog_expiry_should_prune_fn should_prune_fn
,
2950 reflog_expiry_cleanup_fn cleanup_fn
,
2951 void *policy_cb_data
)
2953 struct files_ref_store
*refs
=
2954 files_downcast(ref_store
, REF_STORE_WRITE
, "reflog_expire");
2955 static struct lock_file reflog_lock
;
2956 struct expire_reflog_cb cb
;
2957 struct ref_lock
*lock
;
2958 struct strbuf log_file_sb
= STRBUF_INIT
;
2962 struct strbuf err
= STRBUF_INIT
;
2963 struct object_id oid
;
2965 memset(&cb
, 0, sizeof(cb
));
2967 cb
.policy_cb
= policy_cb_data
;
2968 cb
.should_prune_fn
= should_prune_fn
;
2971 * The reflog file is locked by holding the lock on the
2972 * reference itself, plus we might need to update the
2973 * reference if --updateref was specified:
2975 lock
= lock_ref_sha1_basic(refs
, refname
, sha1
,
2976 NULL
, NULL
, REF_NODEREF
,
2979 error("cannot lock ref '%s': %s", refname
, err
.buf
);
2980 strbuf_release(&err
);
2983 if (!refs_reflog_exists(ref_store
, refname
)) {
2988 files_reflog_path(refs
, &log_file_sb
, refname
);
2989 log_file
= strbuf_detach(&log_file_sb
, NULL
);
2990 if (!(flags
& EXPIRE_REFLOGS_DRY_RUN
)) {
2992 * Even though holding $GIT_DIR/logs/$reflog.lock has
2993 * no locking implications, we use the lock_file
2994 * machinery here anyway because it does a lot of the
2995 * work we need, including cleaning up if the program
2996 * exits unexpectedly.
2998 if (hold_lock_file_for_update(&reflog_lock
, log_file
, 0) < 0) {
2999 struct strbuf err
= STRBUF_INIT
;
3000 unable_to_lock_message(log_file
, errno
, &err
);
3001 error("%s", err
.buf
);
3002 strbuf_release(&err
);
3005 cb
.newlog
= fdopen_lock_file(&reflog_lock
, "w");
3007 error("cannot fdopen %s (%s)",
3008 get_lock_file_path(&reflog_lock
), strerror(errno
));
3013 hashcpy(oid
.hash
, sha1
);
3015 (*prepare_fn
)(refname
, &oid
, cb
.policy_cb
);
3016 refs_for_each_reflog_ent(ref_store
, refname
, expire_reflog_ent
, &cb
);
3017 (*cleanup_fn
)(cb
.policy_cb
);
3019 if (!(flags
& EXPIRE_REFLOGS_DRY_RUN
)) {
3021 * It doesn't make sense to adjust a reference pointed
3022 * to by a symbolic ref based on expiring entries in
3023 * the symbolic reference's reflog. Nor can we update
3024 * a reference if there are no remaining reflog
3027 int update
= (flags
& EXPIRE_REFLOGS_UPDATE_REF
) &&
3028 !(type
& REF_ISSYMREF
) &&
3029 !is_null_oid(&cb
.last_kept_oid
);
3031 if (close_lock_file_gently(&reflog_lock
)) {
3032 status
|= error("couldn't write %s: %s", log_file
,
3034 rollback_lock_file(&reflog_lock
);
3035 } else if (update
&&
3036 (write_in_full(get_lock_file_fd(&lock
->lk
),
3037 oid_to_hex(&cb
.last_kept_oid
), GIT_SHA1_HEXSZ
) < 0 ||
3038 write_str_in_full(get_lock_file_fd(&lock
->lk
), "\n") < 1 ||
3039 close_ref_gently(lock
) < 0)) {
3040 status
|= error("couldn't write %s",
3041 get_lock_file_path(&lock
->lk
));
3042 rollback_lock_file(&reflog_lock
);
3043 } else if (commit_lock_file(&reflog_lock
)) {
3044 status
|= error("unable to write reflog '%s' (%s)",
3045 log_file
, strerror(errno
));
3046 } else if (update
&& commit_ref(lock
)) {
3047 status
|= error("couldn't set %s", lock
->ref_name
);
3055 rollback_lock_file(&reflog_lock
);
3061 static int files_init_db(struct ref_store
*ref_store
, struct strbuf
*err
)
3063 struct files_ref_store
*refs
=
3064 files_downcast(ref_store
, REF_STORE_WRITE
, "init_db");
3065 struct strbuf sb
= STRBUF_INIT
;
3068 * Create .git/refs/{heads,tags}
3070 files_ref_path(refs
, &sb
, "refs/heads");
3071 safe_create_dir(sb
.buf
, 1);
3074 files_ref_path(refs
, &sb
, "refs/tags");
3075 safe_create_dir(sb
.buf
, 1);
3077 strbuf_release(&sb
);
3081 struct ref_storage_be refs_be_files
= {
3084 files_ref_store_create
,
3086 files_transaction_prepare
,
3087 files_transaction_finish
,
3088 files_transaction_abort
,
3089 files_initial_transaction_commit
,
3093 files_create_symref
,
3098 files_ref_iterator_begin
,
3101 files_reflog_iterator_begin
,
3102 files_for_each_reflog_ent
,
3103 files_for_each_reflog_ent_reverse
,
3104 files_reflog_exists
,
3105 files_create_reflog
,
3106 files_delete_reflog
,