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
, struct object_id
*oid
,
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
;
278 int remaining_retries
= 3;
281 strbuf_reset(&sb_path
);
283 files_ref_path(refs
, &sb_path
, refname
);
289 * We might have to loop back here to avoid a race
290 * condition: first we lstat() the file, then we try
291 * to read it as a link or as a file. But if somebody
292 * changes the type of the file (file <-> directory
293 * <-> symlink) between the lstat() and reading, then
294 * we don't want to report that as an error but rather
295 * try again starting with the lstat().
297 * We'll keep a count of the retries, though, just to avoid
298 * any confusing situation sending us into an infinite loop.
301 if (remaining_retries
-- <= 0)
304 if (lstat(path
, &st
) < 0) {
307 if (refs_read_raw_ref(refs
->packed_ref_store
, refname
,
308 oid
, referent
, type
)) {
316 /* Follow "normalized" - ie "refs/.." symlinks by hand */
317 if (S_ISLNK(st
.st_mode
)) {
318 strbuf_reset(&sb_contents
);
319 if (strbuf_readlink(&sb_contents
, path
, 0) < 0) {
320 if (errno
== ENOENT
|| errno
== EINVAL
)
321 /* inconsistent with lstat; retry */
326 if (starts_with(sb_contents
.buf
, "refs/") &&
327 !check_refname_format(sb_contents
.buf
, 0)) {
328 strbuf_swap(&sb_contents
, referent
);
329 *type
|= REF_ISSYMREF
;
334 * It doesn't look like a refname; fall through to just
335 * treating it like a non-symlink, and reading whatever it
340 /* Is it a directory? */
341 if (S_ISDIR(st
.st_mode
)) {
343 * Even though there is a directory where the loose
344 * ref is supposed to be, there could still be a
347 if (refs_read_raw_ref(refs
->packed_ref_store
, refname
,
348 oid
, referent
, type
)) {
357 * Anything else, just open it and try to use it as
360 fd
= open(path
, O_RDONLY
);
362 if (errno
== ENOENT
&& !S_ISLNK(st
.st_mode
))
363 /* inconsistent with lstat; retry */
368 strbuf_reset(&sb_contents
);
369 if (strbuf_read(&sb_contents
, fd
, 256) < 0) {
370 int save_errno
= errno
;
376 strbuf_rtrim(&sb_contents
);
377 buf
= sb_contents
.buf
;
378 if (starts_with(buf
, "ref:")) {
380 while (isspace(*buf
))
383 strbuf_reset(referent
);
384 strbuf_addstr(referent
, buf
);
385 *type
|= REF_ISSYMREF
;
391 * Please note that FETCH_HEAD has additional
392 * data after the sha.
394 if (parse_oid_hex(buf
, oid
, &p
) ||
395 (*p
!= '\0' && !isspace(*p
))) {
396 *type
|= REF_ISBROKEN
;
405 strbuf_release(&sb_path
);
406 strbuf_release(&sb_contents
);
411 static void unlock_ref(struct ref_lock
*lock
)
413 rollback_lock_file(&lock
->lk
);
414 free(lock
->ref_name
);
419 * Lock refname, without following symrefs, and set *lock_p to point
420 * at a newly-allocated lock object. Fill in lock->old_oid, referent,
421 * and type similarly to read_raw_ref().
423 * The caller must verify that refname is a "safe" reference name (in
424 * the sense of refname_is_safe()) before calling this function.
426 * If the reference doesn't already exist, verify that refname doesn't
427 * have a D/F conflict with any existing references. extras and skip
428 * are passed to refs_verify_refname_available() for this check.
430 * If mustexist is not set and the reference is not found or is
431 * broken, lock the reference anyway but clear sha1.
433 * Return 0 on success. On failure, write an error message to err and
434 * return TRANSACTION_NAME_CONFLICT or TRANSACTION_GENERIC_ERROR.
436 * Implementation note: This function is basically
441 * but it includes a lot more code to
442 * - Deal with possible races with other processes
443 * - Avoid calling refs_verify_refname_available() when it can be
444 * avoided, namely if we were successfully able to read the ref
445 * - Generate informative error messages in the case of failure
447 static int lock_raw_ref(struct files_ref_store
*refs
,
448 const char *refname
, int mustexist
,
449 const struct string_list
*extras
,
450 const struct string_list
*skip
,
451 struct ref_lock
**lock_p
,
452 struct strbuf
*referent
,
456 struct ref_lock
*lock
;
457 struct strbuf ref_file
= STRBUF_INIT
;
458 int attempts_remaining
= 3;
459 int ret
= TRANSACTION_GENERIC_ERROR
;
462 files_assert_main_repository(refs
, "lock_raw_ref");
466 /* First lock the file so it can't change out from under us. */
468 *lock_p
= lock
= xcalloc(1, sizeof(*lock
));
470 lock
->ref_name
= xstrdup(refname
);
471 files_ref_path(refs
, &ref_file
, refname
);
474 switch (safe_create_leading_directories(ref_file
.buf
)) {
479 * Suppose refname is "refs/foo/bar". We just failed
480 * to create the containing directory, "refs/foo",
481 * because there was a non-directory in the way. This
482 * indicates a D/F conflict, probably because of
483 * another reference such as "refs/foo". There is no
484 * reason to expect this error to be transitory.
486 if (refs_verify_refname_available(&refs
->base
, refname
,
487 extras
, skip
, err
)) {
490 * To the user the relevant error is
491 * that the "mustexist" reference is
495 strbuf_addf(err
, "unable to resolve reference '%s'",
499 * The error message set by
500 * refs_verify_refname_available() is
503 ret
= TRANSACTION_NAME_CONFLICT
;
507 * The file that is in the way isn't a loose
508 * reference. Report it as a low-level
511 strbuf_addf(err
, "unable to create lock file %s.lock; "
512 "non-directory in the way",
517 /* Maybe another process was tidying up. Try again. */
518 if (--attempts_remaining
> 0)
522 strbuf_addf(err
, "unable to create directory for %s",
527 if (hold_lock_file_for_update_timeout(
528 &lock
->lk
, ref_file
.buf
, LOCK_NO_DEREF
,
529 get_files_ref_lock_timeout_ms()) < 0) {
530 if (errno
== ENOENT
&& --attempts_remaining
> 0) {
532 * Maybe somebody just deleted one of the
533 * directories leading to ref_file. Try
538 unable_to_lock_message(ref_file
.buf
, errno
, err
);
544 * Now we hold the lock and can read the reference without
545 * fear that its value will change.
548 if (files_read_raw_ref(&refs
->base
, refname
,
549 &lock
->old_oid
, referent
, type
)) {
550 if (errno
== ENOENT
) {
552 /* Garden variety missing reference. */
553 strbuf_addf(err
, "unable to resolve reference '%s'",
558 * Reference is missing, but that's OK. We
559 * know that there is not a conflict with
560 * another loose reference because
561 * (supposing that we are trying to lock
562 * reference "refs/foo/bar"):
564 * - We were successfully able to create
565 * the lockfile refs/foo/bar.lock, so we
566 * know there cannot be a loose reference
569 * - We got ENOENT and not EISDIR, so we
570 * know that there cannot be a loose
571 * reference named "refs/foo/bar/baz".
574 } else if (errno
== EISDIR
) {
576 * There is a directory in the way. It might have
577 * contained references that have been deleted. If
578 * we don't require that the reference already
579 * exists, try to remove the directory so that it
580 * doesn't cause trouble when we want to rename the
581 * lockfile into place later.
584 /* Garden variety missing reference. */
585 strbuf_addf(err
, "unable to resolve reference '%s'",
588 } else if (remove_dir_recursively(&ref_file
,
589 REMOVE_DIR_EMPTY_ONLY
)) {
590 if (refs_verify_refname_available(
591 &refs
->base
, refname
,
592 extras
, skip
, err
)) {
594 * The error message set by
595 * verify_refname_available() is OK.
597 ret
= TRANSACTION_NAME_CONFLICT
;
601 * We can't delete the directory,
602 * but we also don't know of any
603 * references that it should
606 strbuf_addf(err
, "there is a non-empty directory '%s' "
607 "blocking reference '%s'",
608 ref_file
.buf
, refname
);
612 } else if (errno
== EINVAL
&& (*type
& REF_ISBROKEN
)) {
613 strbuf_addf(err
, "unable to resolve reference '%s': "
614 "reference broken", refname
);
617 strbuf_addf(err
, "unable to resolve reference '%s': %s",
618 refname
, strerror(errno
));
623 * If the ref did not exist and we are creating it,
624 * make sure there is no existing packed ref that
625 * conflicts with refname:
627 if (refs_verify_refname_available(
628 refs
->packed_ref_store
, refname
,
641 strbuf_release(&ref_file
);
645 struct files_ref_iterator
{
646 struct ref_iterator base
;
648 struct ref_iterator
*iter0
;
652 static int files_ref_iterator_advance(struct ref_iterator
*ref_iterator
)
654 struct files_ref_iterator
*iter
=
655 (struct files_ref_iterator
*)ref_iterator
;
658 while ((ok
= ref_iterator_advance(iter
->iter0
)) == ITER_OK
) {
659 if (iter
->flags
& DO_FOR_EACH_PER_WORKTREE_ONLY
&&
660 ref_type(iter
->iter0
->refname
) != REF_TYPE_PER_WORKTREE
)
663 if (!(iter
->flags
& DO_FOR_EACH_INCLUDE_BROKEN
) &&
664 !ref_resolves_to_object(iter
->iter0
->refname
,
669 iter
->base
.refname
= iter
->iter0
->refname
;
670 iter
->base
.oid
= iter
->iter0
->oid
;
671 iter
->base
.flags
= iter
->iter0
->flags
;
676 if (ref_iterator_abort(ref_iterator
) != ITER_DONE
)
682 static int files_ref_iterator_peel(struct ref_iterator
*ref_iterator
,
683 struct object_id
*peeled
)
685 struct files_ref_iterator
*iter
=
686 (struct files_ref_iterator
*)ref_iterator
;
688 return ref_iterator_peel(iter
->iter0
, peeled
);
691 static int files_ref_iterator_abort(struct ref_iterator
*ref_iterator
)
693 struct files_ref_iterator
*iter
=
694 (struct files_ref_iterator
*)ref_iterator
;
698 ok
= ref_iterator_abort(iter
->iter0
);
700 base_ref_iterator_free(ref_iterator
);
704 static struct ref_iterator_vtable files_ref_iterator_vtable
= {
705 files_ref_iterator_advance
,
706 files_ref_iterator_peel
,
707 files_ref_iterator_abort
710 static struct ref_iterator
*files_ref_iterator_begin(
711 struct ref_store
*ref_store
,
712 const char *prefix
, unsigned int flags
)
714 struct files_ref_store
*refs
;
715 struct ref_iterator
*loose_iter
, *packed_iter
, *overlay_iter
;
716 struct files_ref_iterator
*iter
;
717 struct ref_iterator
*ref_iterator
;
718 unsigned int required_flags
= REF_STORE_READ
;
720 if (!(flags
& DO_FOR_EACH_INCLUDE_BROKEN
))
721 required_flags
|= REF_STORE_ODB
;
723 refs
= files_downcast(ref_store
, required_flags
, "ref_iterator_begin");
726 * We must make sure that all loose refs are read before
727 * accessing the packed-refs file; this avoids a race
728 * condition if loose refs are migrated to the packed-refs
729 * file by a simultaneous process, but our in-memory view is
730 * from before the migration. We ensure this as follows:
731 * First, we call start the loose refs iteration with its
732 * `prime_ref` argument set to true. This causes the loose
733 * references in the subtree to be pre-read into the cache.
734 * (If they've already been read, that's OK; we only need to
735 * guarantee that they're read before the packed refs, not
736 * *how much* before.) After that, we call
737 * packed_ref_iterator_begin(), which internally checks
738 * whether the packed-ref cache is up to date with what is on
739 * disk, and re-reads it if not.
742 loose_iter
= cache_ref_iterator_begin(get_loose_ref_cache(refs
),
746 * The packed-refs file might contain broken references, for
747 * example an old version of a reference that points at an
748 * object that has since been garbage-collected. This is OK as
749 * long as there is a corresponding loose reference that
750 * overrides it, and we don't want to emit an error message in
751 * this case. So ask the packed_ref_store for all of its
752 * references, and (if needed) do our own check for broken
753 * ones in files_ref_iterator_advance(), after we have merged
754 * the packed and loose references.
756 packed_iter
= refs_ref_iterator_begin(
757 refs
->packed_ref_store
, prefix
, 0,
758 DO_FOR_EACH_INCLUDE_BROKEN
);
760 overlay_iter
= overlay_ref_iterator_begin(loose_iter
, packed_iter
);
762 iter
= xcalloc(1, sizeof(*iter
));
763 ref_iterator
= &iter
->base
;
764 base_ref_iterator_init(ref_iterator
, &files_ref_iterator_vtable
,
765 overlay_iter
->ordered
);
766 iter
->iter0
= overlay_iter
;
773 * Verify that the reference locked by lock has the value old_oid
774 * (unless it is NULL). Fail if the reference doesn't exist and
775 * mustexist is set. Return 0 on success. On error, write an error
776 * message to err, set errno, and return a negative value.
778 static int verify_lock(struct ref_store
*ref_store
, struct ref_lock
*lock
,
779 const struct object_id
*old_oid
, int mustexist
,
784 if (refs_read_ref_full(ref_store
, lock
->ref_name
,
785 mustexist
? RESOLVE_REF_READING
: 0,
786 &lock
->old_oid
, NULL
)) {
788 int save_errno
= errno
;
789 strbuf_addf(err
, "can't verify ref '%s'", lock
->ref_name
);
793 oidclr(&lock
->old_oid
);
797 if (old_oid
&& oidcmp(&lock
->old_oid
, old_oid
)) {
798 strbuf_addf(err
, "ref '%s' is at %s but expected %s",
800 oid_to_hex(&lock
->old_oid
),
801 oid_to_hex(old_oid
));
808 static int remove_empty_directories(struct strbuf
*path
)
811 * we want to create a file but there is a directory there;
812 * if that is an empty directory (or a directory that contains
813 * only empty directories), remove them.
815 return remove_dir_recursively(path
, REMOVE_DIR_EMPTY_ONLY
);
818 static int create_reflock(const char *path
, void *cb
)
820 struct lock_file
*lk
= cb
;
822 return hold_lock_file_for_update_timeout(
823 lk
, path
, LOCK_NO_DEREF
,
824 get_files_ref_lock_timeout_ms()) < 0 ? -1 : 0;
828 * Locks a ref returning the lock on success and NULL on failure.
829 * On failure errno is set to something meaningful.
831 static struct ref_lock
*lock_ref_oid_basic(struct files_ref_store
*refs
,
833 const struct object_id
*old_oid
,
834 const struct string_list
*extras
,
835 const struct string_list
*skip
,
836 unsigned int flags
, int *type
,
839 struct strbuf ref_file
= STRBUF_INIT
;
840 struct ref_lock
*lock
;
842 int mustexist
= (old_oid
&& !is_null_oid(old_oid
));
843 int resolve_flags
= RESOLVE_REF_NO_RECURSE
;
846 files_assert_main_repository(refs
, "lock_ref_oid_basic");
849 lock
= xcalloc(1, sizeof(struct ref_lock
));
852 resolve_flags
|= RESOLVE_REF_READING
;
853 if (flags
& REF_DELETING
)
854 resolve_flags
|= RESOLVE_REF_ALLOW_BAD_NAME
;
856 files_ref_path(refs
, &ref_file
, refname
);
857 resolved
= !!refs_resolve_ref_unsafe(&refs
->base
,
858 refname
, resolve_flags
,
859 &lock
->old_oid
, type
);
860 if (!resolved
&& errno
== EISDIR
) {
862 * we are trying to lock foo but we used to
863 * have foo/bar which now does not exist;
864 * it is normal for the empty directory 'foo'
867 if (remove_empty_directories(&ref_file
)) {
869 if (!refs_verify_refname_available(
871 refname
, extras
, skip
, err
))
872 strbuf_addf(err
, "there are still refs under '%s'",
876 resolved
= !!refs_resolve_ref_unsafe(&refs
->base
,
877 refname
, resolve_flags
,
878 &lock
->old_oid
, type
);
882 if (last_errno
!= ENOTDIR
||
883 !refs_verify_refname_available(&refs
->base
, refname
,
885 strbuf_addf(err
, "unable to resolve reference '%s': %s",
886 refname
, strerror(last_errno
));
892 * If the ref did not exist and we are creating it, make sure
893 * there is no existing packed ref whose name begins with our
894 * refname, nor a packed ref whose name is a proper prefix of
897 if (is_null_oid(&lock
->old_oid
) &&
898 refs_verify_refname_available(refs
->packed_ref_store
, refname
,
899 extras
, skip
, err
)) {
900 last_errno
= ENOTDIR
;
904 lock
->ref_name
= xstrdup(refname
);
906 if (raceproof_create_file(ref_file
.buf
, create_reflock
, &lock
->lk
)) {
908 unable_to_lock_message(ref_file
.buf
, errno
, err
);
912 if (verify_lock(&refs
->base
, lock
, old_oid
, mustexist
, err
)) {
923 strbuf_release(&ref_file
);
928 struct ref_to_prune
{
929 struct ref_to_prune
*next
;
930 struct object_id oid
;
931 char name
[FLEX_ARRAY
];
935 REMOVE_EMPTY_PARENTS_REF
= 0x01,
936 REMOVE_EMPTY_PARENTS_REFLOG
= 0x02
940 * Remove empty parent directories associated with the specified
941 * reference and/or its reflog, but spare [logs/]refs/ and immediate
942 * subdirs. flags is a combination of REMOVE_EMPTY_PARENTS_REF and/or
943 * REMOVE_EMPTY_PARENTS_REFLOG.
945 static void try_remove_empty_parents(struct files_ref_store
*refs
,
949 struct strbuf buf
= STRBUF_INIT
;
950 struct strbuf sb
= STRBUF_INIT
;
954 strbuf_addstr(&buf
, refname
);
956 for (i
= 0; i
< 2; i
++) { /* refs/{heads,tags,...}/ */
957 while (*p
&& *p
!= '/')
959 /* tolerate duplicate slashes; see check_refname_format() */
963 q
= buf
.buf
+ buf
.len
;
964 while (flags
& (REMOVE_EMPTY_PARENTS_REF
| REMOVE_EMPTY_PARENTS_REFLOG
)) {
965 while (q
> p
&& *q
!= '/')
967 while (q
> p
&& *(q
-1) == '/')
971 strbuf_setlen(&buf
, q
- buf
.buf
);
974 files_ref_path(refs
, &sb
, buf
.buf
);
975 if ((flags
& REMOVE_EMPTY_PARENTS_REF
) && rmdir(sb
.buf
))
976 flags
&= ~REMOVE_EMPTY_PARENTS_REF
;
979 files_reflog_path(refs
, &sb
, buf
.buf
);
980 if ((flags
& REMOVE_EMPTY_PARENTS_REFLOG
) && rmdir(sb
.buf
))
981 flags
&= ~REMOVE_EMPTY_PARENTS_REFLOG
;
983 strbuf_release(&buf
);
987 /* make sure nobody touched the ref, and unlink */
988 static void prune_ref(struct files_ref_store
*refs
, struct ref_to_prune
*r
)
990 struct ref_transaction
*transaction
;
991 struct strbuf err
= STRBUF_INIT
;
994 if (check_refname_format(r
->name
, 0))
997 transaction
= ref_store_transaction_begin(&refs
->base
, &err
);
1000 ref_transaction_add_update(
1001 transaction
, r
->name
,
1002 REF_NODEREF
| REF_HAVE_NEW
| REF_HAVE_OLD
| REF_ISPRUNING
,
1003 &null_oid
, &r
->oid
, NULL
);
1004 if (ref_transaction_commit(transaction
, &err
))
1011 error("%s", err
.buf
);
1012 strbuf_release(&err
);
1013 ref_transaction_free(transaction
);
1018 * Prune the loose versions of the references in the linked list
1019 * `*refs_to_prune`, freeing the entries in the list as we go.
1021 static void prune_refs(struct files_ref_store
*refs
, struct ref_to_prune
**refs_to_prune
)
1023 while (*refs_to_prune
) {
1024 struct ref_to_prune
*r
= *refs_to_prune
;
1025 *refs_to_prune
= r
->next
;
1032 * Return true if the specified reference should be packed.
1034 static int should_pack_ref(const char *refname
,
1035 const struct object_id
*oid
, unsigned int ref_flags
,
1036 unsigned int pack_flags
)
1038 /* Do not pack per-worktree refs: */
1039 if (ref_type(refname
) != REF_TYPE_NORMAL
)
1042 /* Do not pack non-tags unless PACK_REFS_ALL is set: */
1043 if (!(pack_flags
& PACK_REFS_ALL
) && !starts_with(refname
, "refs/tags/"))
1046 /* Do not pack symbolic refs: */
1047 if (ref_flags
& REF_ISSYMREF
)
1050 /* Do not pack broken refs: */
1051 if (!ref_resolves_to_object(refname
, oid
, ref_flags
))
1057 static int files_pack_refs(struct ref_store
*ref_store
, unsigned int flags
)
1059 struct files_ref_store
*refs
=
1060 files_downcast(ref_store
, REF_STORE_WRITE
| REF_STORE_ODB
,
1062 struct ref_iterator
*iter
;
1064 struct ref_to_prune
*refs_to_prune
= NULL
;
1065 struct strbuf err
= STRBUF_INIT
;
1066 struct ref_transaction
*transaction
;
1068 transaction
= ref_store_transaction_begin(refs
->packed_ref_store
, &err
);
1072 packed_refs_lock(refs
->packed_ref_store
, LOCK_DIE_ON_ERROR
, &err
);
1074 iter
= cache_ref_iterator_begin(get_loose_ref_cache(refs
), NULL
, 0);
1075 while ((ok
= ref_iterator_advance(iter
)) == ITER_OK
) {
1077 * If the loose reference can be packed, add an entry
1078 * in the packed ref cache. If the reference should be
1079 * pruned, also add it to refs_to_prune.
1081 if (!should_pack_ref(iter
->refname
, iter
->oid
, iter
->flags
,
1086 * Add a reference creation for this reference to the
1087 * packed-refs transaction:
1089 if (ref_transaction_update(transaction
, iter
->refname
,
1091 REF_NODEREF
, NULL
, &err
))
1092 die("failure preparing to create packed reference %s: %s",
1093 iter
->refname
, err
.buf
);
1095 /* Schedule the loose reference for pruning if requested. */
1096 if ((flags
& PACK_REFS_PRUNE
)) {
1097 struct ref_to_prune
*n
;
1098 FLEX_ALLOC_STR(n
, name
, iter
->refname
);
1099 oidcpy(&n
->oid
, iter
->oid
);
1100 n
->next
= refs_to_prune
;
1104 if (ok
!= ITER_DONE
)
1105 die("error while iterating over references");
1107 if (ref_transaction_commit(transaction
, &err
))
1108 die("unable to write new packed-refs: %s", err
.buf
);
1110 ref_transaction_free(transaction
);
1112 packed_refs_unlock(refs
->packed_ref_store
);
1114 prune_refs(refs
, &refs_to_prune
);
1115 strbuf_release(&err
);
1119 static int files_delete_refs(struct ref_store
*ref_store
, const char *msg
,
1120 struct string_list
*refnames
, unsigned int flags
)
1122 struct files_ref_store
*refs
=
1123 files_downcast(ref_store
, REF_STORE_WRITE
, "delete_refs");
1124 struct strbuf err
= STRBUF_INIT
;
1130 if (packed_refs_lock(refs
->packed_ref_store
, 0, &err
))
1133 if (refs_delete_refs(refs
->packed_ref_store
, msg
, refnames
, flags
)) {
1134 packed_refs_unlock(refs
->packed_ref_store
);
1138 packed_refs_unlock(refs
->packed_ref_store
);
1140 for (i
= 0; i
< refnames
->nr
; i
++) {
1141 const char *refname
= refnames
->items
[i
].string
;
1143 if (refs_delete_ref(&refs
->base
, msg
, refname
, NULL
, flags
))
1144 result
|= error(_("could not remove reference %s"), refname
);
1147 strbuf_release(&err
);
1152 * If we failed to rewrite the packed-refs file, then it is
1153 * unsafe to try to remove loose refs, because doing so might
1154 * expose an obsolete packed value for a reference that might
1155 * even point at an object that has been garbage collected.
1157 if (refnames
->nr
== 1)
1158 error(_("could not delete reference %s: %s"),
1159 refnames
->items
[0].string
, err
.buf
);
1161 error(_("could not delete references: %s"), err
.buf
);
1163 strbuf_release(&err
);
1168 * People using contrib's git-new-workdir have .git/logs/refs ->
1169 * /some/other/path/.git/logs/refs, and that may live on another device.
1171 * IOW, to avoid cross device rename errors, the temporary renamed log must
1172 * live into logs/refs.
1174 #define TMP_RENAMED_LOG "refs/.tmp-renamed-log"
1177 const char *tmp_renamed_log
;
1181 static int rename_tmp_log_callback(const char *path
, void *cb_data
)
1183 struct rename_cb
*cb
= cb_data
;
1185 if (rename(cb
->tmp_renamed_log
, path
)) {
1187 * rename(a, b) when b is an existing directory ought
1188 * to result in ISDIR, but Solaris 5.8 gives ENOTDIR.
1189 * Sheesh. Record the true errno for error reporting,
1190 * but report EISDIR to raceproof_create_file() so
1191 * that it knows to retry.
1193 cb
->true_errno
= errno
;
1194 if (errno
== ENOTDIR
)
1202 static int rename_tmp_log(struct files_ref_store
*refs
, const char *newrefname
)
1204 struct strbuf path
= STRBUF_INIT
;
1205 struct strbuf tmp
= STRBUF_INIT
;
1206 struct rename_cb cb
;
1209 files_reflog_path(refs
, &path
, newrefname
);
1210 files_reflog_path(refs
, &tmp
, TMP_RENAMED_LOG
);
1211 cb
.tmp_renamed_log
= tmp
.buf
;
1212 ret
= raceproof_create_file(path
.buf
, rename_tmp_log_callback
, &cb
);
1214 if (errno
== EISDIR
)
1215 error("directory not empty: %s", path
.buf
);
1217 error("unable to move logfile %s to %s: %s",
1219 strerror(cb
.true_errno
));
1222 strbuf_release(&path
);
1223 strbuf_release(&tmp
);
1227 static int write_ref_to_lockfile(struct ref_lock
*lock
,
1228 const struct object_id
*oid
, struct strbuf
*err
);
1229 static int commit_ref_update(struct files_ref_store
*refs
,
1230 struct ref_lock
*lock
,
1231 const struct object_id
*oid
, const char *logmsg
,
1232 struct strbuf
*err
);
1234 static int files_copy_or_rename_ref(struct ref_store
*ref_store
,
1235 const char *oldrefname
, const char *newrefname
,
1236 const char *logmsg
, int copy
)
1238 struct files_ref_store
*refs
=
1239 files_downcast(ref_store
, REF_STORE_WRITE
, "rename_ref");
1240 struct object_id oid
, orig_oid
;
1241 int flag
= 0, logmoved
= 0;
1242 struct ref_lock
*lock
;
1243 struct stat loginfo
;
1244 struct strbuf sb_oldref
= STRBUF_INIT
;
1245 struct strbuf sb_newref
= STRBUF_INIT
;
1246 struct strbuf tmp_renamed_log
= STRBUF_INIT
;
1248 struct strbuf err
= STRBUF_INIT
;
1250 files_reflog_path(refs
, &sb_oldref
, oldrefname
);
1251 files_reflog_path(refs
, &sb_newref
, newrefname
);
1252 files_reflog_path(refs
, &tmp_renamed_log
, TMP_RENAMED_LOG
);
1254 log
= !lstat(sb_oldref
.buf
, &loginfo
);
1255 if (log
&& S_ISLNK(loginfo
.st_mode
)) {
1256 ret
= error("reflog for %s is a symlink", oldrefname
);
1260 if (!refs_resolve_ref_unsafe(&refs
->base
, oldrefname
,
1261 RESOLVE_REF_READING
| RESOLVE_REF_NO_RECURSE
,
1262 &orig_oid
, &flag
)) {
1263 ret
= error("refname %s not found", oldrefname
);
1267 if (flag
& REF_ISSYMREF
) {
1269 ret
= error("refname %s is a symbolic ref, copying it is not supported",
1272 ret
= error("refname %s is a symbolic ref, renaming it is not supported",
1276 if (!refs_rename_ref_available(&refs
->base
, oldrefname
, newrefname
)) {
1281 if (!copy
&& log
&& rename(sb_oldref
.buf
, tmp_renamed_log
.buf
)) {
1282 ret
= error("unable to move logfile logs/%s to logs/"TMP_RENAMED_LOG
": %s",
1283 oldrefname
, strerror(errno
));
1287 if (copy
&& log
&& copy_file(tmp_renamed_log
.buf
, sb_oldref
.buf
, 0644)) {
1288 ret
= error("unable to copy logfile logs/%s to logs/"TMP_RENAMED_LOG
": %s",
1289 oldrefname
, strerror(errno
));
1293 if (!copy
&& refs_delete_ref(&refs
->base
, logmsg
, oldrefname
,
1294 &orig_oid
, REF_NODEREF
)) {
1295 error("unable to delete old %s", oldrefname
);
1300 * Since we are doing a shallow lookup, oid is not the
1301 * correct value to pass to delete_ref as old_oid. But that
1302 * doesn't matter, because an old_oid check wouldn't add to
1303 * the safety anyway; we want to delete the reference whatever
1304 * its current value.
1306 if (!copy
&& !refs_read_ref_full(&refs
->base
, newrefname
,
1307 RESOLVE_REF_READING
| RESOLVE_REF_NO_RECURSE
,
1309 refs_delete_ref(&refs
->base
, NULL
, newrefname
,
1310 NULL
, REF_NODEREF
)) {
1311 if (errno
== EISDIR
) {
1312 struct strbuf path
= STRBUF_INIT
;
1315 files_ref_path(refs
, &path
, newrefname
);
1316 result
= remove_empty_directories(&path
);
1317 strbuf_release(&path
);
1320 error("Directory not empty: %s", newrefname
);
1324 error("unable to delete existing %s", newrefname
);
1329 if (log
&& rename_tmp_log(refs
, newrefname
))
1334 lock
= lock_ref_oid_basic(refs
, newrefname
, NULL
, NULL
, NULL
,
1335 REF_NODEREF
, NULL
, &err
);
1338 error("unable to copy '%s' to '%s': %s", oldrefname
, newrefname
, err
.buf
);
1340 error("unable to rename '%s' to '%s': %s", oldrefname
, newrefname
, err
.buf
);
1341 strbuf_release(&err
);
1344 oidcpy(&lock
->old_oid
, &orig_oid
);
1346 if (write_ref_to_lockfile(lock
, &orig_oid
, &err
) ||
1347 commit_ref_update(refs
, lock
, &orig_oid
, logmsg
, &err
)) {
1348 error("unable to write current sha1 into %s: %s", newrefname
, err
.buf
);
1349 strbuf_release(&err
);
1357 lock
= lock_ref_oid_basic(refs
, oldrefname
, NULL
, NULL
, NULL
,
1358 REF_NODEREF
, NULL
, &err
);
1360 error("unable to lock %s for rollback: %s", oldrefname
, err
.buf
);
1361 strbuf_release(&err
);
1365 flag
= log_all_ref_updates
;
1366 log_all_ref_updates
= LOG_REFS_NONE
;
1367 if (write_ref_to_lockfile(lock
, &orig_oid
, &err
) ||
1368 commit_ref_update(refs
, lock
, &orig_oid
, NULL
, &err
)) {
1369 error("unable to write current sha1 into %s: %s", oldrefname
, err
.buf
);
1370 strbuf_release(&err
);
1372 log_all_ref_updates
= flag
;
1375 if (logmoved
&& rename(sb_newref
.buf
, sb_oldref
.buf
))
1376 error("unable to restore logfile %s from %s: %s",
1377 oldrefname
, newrefname
, strerror(errno
));
1378 if (!logmoved
&& log
&&
1379 rename(tmp_renamed_log
.buf
, sb_oldref
.buf
))
1380 error("unable to restore logfile %s from logs/"TMP_RENAMED_LOG
": %s",
1381 oldrefname
, strerror(errno
));
1384 strbuf_release(&sb_newref
);
1385 strbuf_release(&sb_oldref
);
1386 strbuf_release(&tmp_renamed_log
);
1391 static int files_rename_ref(struct ref_store
*ref_store
,
1392 const char *oldrefname
, const char *newrefname
,
1395 return files_copy_or_rename_ref(ref_store
, oldrefname
,
1396 newrefname
, logmsg
, 0);
1399 static int files_copy_ref(struct ref_store
*ref_store
,
1400 const char *oldrefname
, const char *newrefname
,
1403 return files_copy_or_rename_ref(ref_store
, oldrefname
,
1404 newrefname
, logmsg
, 1);
1407 static int close_ref_gently(struct ref_lock
*lock
)
1409 if (close_lock_file_gently(&lock
->lk
))
1414 static int commit_ref(struct ref_lock
*lock
)
1416 char *path
= get_locked_file_path(&lock
->lk
);
1419 if (!lstat(path
, &st
) && S_ISDIR(st
.st_mode
)) {
1421 * There is a directory at the path we want to rename
1422 * the lockfile to. Hopefully it is empty; try to
1425 size_t len
= strlen(path
);
1426 struct strbuf sb_path
= STRBUF_INIT
;
1428 strbuf_attach(&sb_path
, path
, len
, len
);
1431 * If this fails, commit_lock_file() will also fail
1432 * and will report the problem.
1434 remove_empty_directories(&sb_path
);
1435 strbuf_release(&sb_path
);
1440 if (commit_lock_file(&lock
->lk
))
1445 static int open_or_create_logfile(const char *path
, void *cb
)
1449 *fd
= open(path
, O_APPEND
| O_WRONLY
| O_CREAT
, 0666);
1450 return (*fd
< 0) ? -1 : 0;
1454 * Create a reflog for a ref. If force_create = 0, only create the
1455 * reflog for certain refs (those for which should_autocreate_reflog
1456 * returns non-zero). Otherwise, create it regardless of the reference
1457 * name. If the logfile already existed or was created, return 0 and
1458 * set *logfd to the file descriptor opened for appending to the file.
1459 * If no logfile exists and we decided not to create one, return 0 and
1460 * set *logfd to -1. On failure, fill in *err, set *logfd to -1, and
1463 static int log_ref_setup(struct files_ref_store
*refs
,
1464 const char *refname
, int force_create
,
1465 int *logfd
, struct strbuf
*err
)
1467 struct strbuf logfile_sb
= STRBUF_INIT
;
1470 files_reflog_path(refs
, &logfile_sb
, refname
);
1471 logfile
= strbuf_detach(&logfile_sb
, NULL
);
1473 if (force_create
|| should_autocreate_reflog(refname
)) {
1474 if (raceproof_create_file(logfile
, open_or_create_logfile
, logfd
)) {
1475 if (errno
== ENOENT
)
1476 strbuf_addf(err
, "unable to create directory for '%s': "
1477 "%s", logfile
, strerror(errno
));
1478 else if (errno
== EISDIR
)
1479 strbuf_addf(err
, "there are still logs under '%s'",
1482 strbuf_addf(err
, "unable to append to '%s': %s",
1483 logfile
, strerror(errno
));
1488 *logfd
= open(logfile
, O_APPEND
| O_WRONLY
, 0666);
1490 if (errno
== ENOENT
|| errno
== EISDIR
) {
1492 * The logfile doesn't already exist,
1493 * but that is not an error; it only
1494 * means that we won't write log
1499 strbuf_addf(err
, "unable to append to '%s': %s",
1500 logfile
, strerror(errno
));
1507 adjust_shared_perm(logfile
);
1517 static int files_create_reflog(struct ref_store
*ref_store
,
1518 const char *refname
, int force_create
,
1521 struct files_ref_store
*refs
=
1522 files_downcast(ref_store
, REF_STORE_WRITE
, "create_reflog");
1525 if (log_ref_setup(refs
, refname
, force_create
, &fd
, err
))
1534 static int log_ref_write_fd(int fd
, const struct object_id
*old_oid
,
1535 const struct object_id
*new_oid
,
1536 const char *committer
, const char *msg
)
1538 int msglen
, written
;
1539 unsigned maxlen
, len
;
1542 msglen
= msg
? strlen(msg
) : 0;
1543 maxlen
= strlen(committer
) + msglen
+ 100;
1544 logrec
= xmalloc(maxlen
);
1545 len
= xsnprintf(logrec
, maxlen
, "%s %s %s\n",
1546 oid_to_hex(old_oid
),
1547 oid_to_hex(new_oid
),
1550 len
+= copy_reflog_msg(logrec
+ len
- 1, msg
) - 1;
1552 written
= len
<= maxlen
? write_in_full(fd
, logrec
, len
) : -1;
1560 static int files_log_ref_write(struct files_ref_store
*refs
,
1561 const char *refname
, const struct object_id
*old_oid
,
1562 const struct object_id
*new_oid
, const char *msg
,
1563 int flags
, struct strbuf
*err
)
1567 if (log_all_ref_updates
== LOG_REFS_UNSET
)
1568 log_all_ref_updates
= is_bare_repository() ? LOG_REFS_NONE
: LOG_REFS_NORMAL
;
1570 result
= log_ref_setup(refs
, refname
,
1571 flags
& REF_FORCE_CREATE_REFLOG
,
1579 result
= log_ref_write_fd(logfd
, old_oid
, new_oid
,
1580 git_committer_info(0), msg
);
1582 struct strbuf sb
= STRBUF_INIT
;
1583 int save_errno
= errno
;
1585 files_reflog_path(refs
, &sb
, refname
);
1586 strbuf_addf(err
, "unable to append to '%s': %s",
1587 sb
.buf
, strerror(save_errno
));
1588 strbuf_release(&sb
);
1593 struct strbuf sb
= STRBUF_INIT
;
1594 int save_errno
= errno
;
1596 files_reflog_path(refs
, &sb
, refname
);
1597 strbuf_addf(err
, "unable to append to '%s': %s",
1598 sb
.buf
, strerror(save_errno
));
1599 strbuf_release(&sb
);
1606 * Write sha1 into the open lockfile, then close the lockfile. On
1607 * errors, rollback the lockfile, fill in *err and
1610 static int write_ref_to_lockfile(struct ref_lock
*lock
,
1611 const struct object_id
*oid
, struct strbuf
*err
)
1613 static char term
= '\n';
1617 o
= parse_object(oid
);
1620 "trying to write ref '%s' with nonexistent object %s",
1621 lock
->ref_name
, oid_to_hex(oid
));
1625 if (o
->type
!= OBJ_COMMIT
&& is_branch(lock
->ref_name
)) {
1627 "trying to write non-commit object %s to branch '%s'",
1628 oid_to_hex(oid
), lock
->ref_name
);
1632 fd
= get_lock_file_fd(&lock
->lk
);
1633 if (write_in_full(fd
, oid_to_hex(oid
), GIT_SHA1_HEXSZ
) < 0 ||
1634 write_in_full(fd
, &term
, 1) < 0 ||
1635 close_ref_gently(lock
) < 0) {
1637 "couldn't write '%s'", get_lock_file_path(&lock
->lk
));
1645 * Commit a change to a loose reference that has already been written
1646 * to the loose reference lockfile. Also update the reflogs if
1647 * necessary, using the specified lockmsg (which can be NULL).
1649 static int commit_ref_update(struct files_ref_store
*refs
,
1650 struct ref_lock
*lock
,
1651 const struct object_id
*oid
, const char *logmsg
,
1654 files_assert_main_repository(refs
, "commit_ref_update");
1656 clear_loose_ref_cache(refs
);
1657 if (files_log_ref_write(refs
, lock
->ref_name
,
1658 &lock
->old_oid
, oid
,
1660 char *old_msg
= strbuf_detach(err
, NULL
);
1661 strbuf_addf(err
, "cannot update the ref '%s': %s",
1662 lock
->ref_name
, old_msg
);
1668 if (strcmp(lock
->ref_name
, "HEAD") != 0) {
1670 * Special hack: If a branch is updated directly and HEAD
1671 * points to it (may happen on the remote side of a push
1672 * for example) then logically the HEAD reflog should be
1674 * A generic solution implies reverse symref information,
1675 * but finding all symrefs pointing to the given branch
1676 * would be rather costly for this rare event (the direct
1677 * update of a branch) to be worth it. So let's cheat and
1678 * check with HEAD only which should cover 99% of all usage
1679 * scenarios (even 100% of the default ones).
1682 const char *head_ref
;
1684 head_ref
= refs_resolve_ref_unsafe(&refs
->base
, "HEAD",
1685 RESOLVE_REF_READING
,
1687 if (head_ref
&& (head_flag
& REF_ISSYMREF
) &&
1688 !strcmp(head_ref
, lock
->ref_name
)) {
1689 struct strbuf log_err
= STRBUF_INIT
;
1690 if (files_log_ref_write(refs
, "HEAD",
1691 &lock
->old_oid
, oid
,
1692 logmsg
, 0, &log_err
)) {
1693 error("%s", log_err
.buf
);
1694 strbuf_release(&log_err
);
1699 if (commit_ref(lock
)) {
1700 strbuf_addf(err
, "couldn't set '%s'", lock
->ref_name
);
1709 static int create_ref_symlink(struct ref_lock
*lock
, const char *target
)
1712 #ifndef NO_SYMLINK_HEAD
1713 char *ref_path
= get_locked_file_path(&lock
->lk
);
1715 ret
= symlink(target
, ref_path
);
1719 fprintf(stderr
, "no symlink - falling back to symbolic ref\n");
1724 static void update_symref_reflog(struct files_ref_store
*refs
,
1725 struct ref_lock
*lock
, const char *refname
,
1726 const char *target
, const char *logmsg
)
1728 struct strbuf err
= STRBUF_INIT
;
1729 struct object_id new_oid
;
1731 !refs_read_ref_full(&refs
->base
, target
,
1732 RESOLVE_REF_READING
, &new_oid
, NULL
) &&
1733 files_log_ref_write(refs
, refname
, &lock
->old_oid
,
1734 &new_oid
, logmsg
, 0, &err
)) {
1735 error("%s", err
.buf
);
1736 strbuf_release(&err
);
1740 static int create_symref_locked(struct files_ref_store
*refs
,
1741 struct ref_lock
*lock
, const char *refname
,
1742 const char *target
, const char *logmsg
)
1744 if (prefer_symlink_refs
&& !create_ref_symlink(lock
, target
)) {
1745 update_symref_reflog(refs
, lock
, refname
, target
, logmsg
);
1749 if (!fdopen_lock_file(&lock
->lk
, "w"))
1750 return error("unable to fdopen %s: %s",
1751 lock
->lk
.tempfile
->filename
.buf
, strerror(errno
));
1753 update_symref_reflog(refs
, lock
, refname
, target
, logmsg
);
1755 /* no error check; commit_ref will check ferror */
1756 fprintf(lock
->lk
.tempfile
->fp
, "ref: %s\n", target
);
1757 if (commit_ref(lock
) < 0)
1758 return error("unable to write symref for %s: %s", refname
,
1763 static int files_create_symref(struct ref_store
*ref_store
,
1764 const char *refname
, const char *target
,
1767 struct files_ref_store
*refs
=
1768 files_downcast(ref_store
, REF_STORE_WRITE
, "create_symref");
1769 struct strbuf err
= STRBUF_INIT
;
1770 struct ref_lock
*lock
;
1773 lock
= lock_ref_oid_basic(refs
, refname
, NULL
,
1774 NULL
, NULL
, REF_NODEREF
, NULL
,
1777 error("%s", err
.buf
);
1778 strbuf_release(&err
);
1782 ret
= create_symref_locked(refs
, lock
, refname
, target
, logmsg
);
1787 static int files_reflog_exists(struct ref_store
*ref_store
,
1788 const char *refname
)
1790 struct files_ref_store
*refs
=
1791 files_downcast(ref_store
, REF_STORE_READ
, "reflog_exists");
1792 struct strbuf sb
= STRBUF_INIT
;
1796 files_reflog_path(refs
, &sb
, refname
);
1797 ret
= !lstat(sb
.buf
, &st
) && S_ISREG(st
.st_mode
);
1798 strbuf_release(&sb
);
1802 static int files_delete_reflog(struct ref_store
*ref_store
,
1803 const char *refname
)
1805 struct files_ref_store
*refs
=
1806 files_downcast(ref_store
, REF_STORE_WRITE
, "delete_reflog");
1807 struct strbuf sb
= STRBUF_INIT
;
1810 files_reflog_path(refs
, &sb
, refname
);
1811 ret
= remove_path(sb
.buf
);
1812 strbuf_release(&sb
);
1816 static int show_one_reflog_ent(struct strbuf
*sb
, each_reflog_ent_fn fn
, void *cb_data
)
1818 struct object_id ooid
, noid
;
1819 char *email_end
, *message
;
1820 timestamp_t timestamp
;
1822 const char *p
= sb
->buf
;
1824 /* old SP new SP name <email> SP time TAB msg LF */
1825 if (!sb
->len
|| sb
->buf
[sb
->len
- 1] != '\n' ||
1826 parse_oid_hex(p
, &ooid
, &p
) || *p
++ != ' ' ||
1827 parse_oid_hex(p
, &noid
, &p
) || *p
++ != ' ' ||
1828 !(email_end
= strchr(p
, '>')) ||
1829 email_end
[1] != ' ' ||
1830 !(timestamp
= parse_timestamp(email_end
+ 2, &message
, 10)) ||
1831 !message
|| message
[0] != ' ' ||
1832 (message
[1] != '+' && message
[1] != '-') ||
1833 !isdigit(message
[2]) || !isdigit(message
[3]) ||
1834 !isdigit(message
[4]) || !isdigit(message
[5]))
1835 return 0; /* corrupt? */
1836 email_end
[1] = '\0';
1837 tz
= strtol(message
+ 1, NULL
, 10);
1838 if (message
[6] != '\t')
1842 return fn(&ooid
, &noid
, p
, timestamp
, tz
, message
, cb_data
);
1845 static char *find_beginning_of_line(char *bob
, char *scan
)
1847 while (bob
< scan
&& *(--scan
) != '\n')
1848 ; /* keep scanning backwards */
1850 * Return either beginning of the buffer, or LF at the end of
1851 * the previous line.
1856 static int files_for_each_reflog_ent_reverse(struct ref_store
*ref_store
,
1857 const char *refname
,
1858 each_reflog_ent_fn fn
,
1861 struct files_ref_store
*refs
=
1862 files_downcast(ref_store
, REF_STORE_READ
,
1863 "for_each_reflog_ent_reverse");
1864 struct strbuf sb
= STRBUF_INIT
;
1867 int ret
= 0, at_tail
= 1;
1869 files_reflog_path(refs
, &sb
, refname
);
1870 logfp
= fopen(sb
.buf
, "r");
1871 strbuf_release(&sb
);
1875 /* Jump to the end */
1876 if (fseek(logfp
, 0, SEEK_END
) < 0)
1877 ret
= error("cannot seek back reflog for %s: %s",
1878 refname
, strerror(errno
));
1880 while (!ret
&& 0 < pos
) {
1886 /* Fill next block from the end */
1887 cnt
= (sizeof(buf
) < pos
) ? sizeof(buf
) : pos
;
1888 if (fseek(logfp
, pos
- cnt
, SEEK_SET
)) {
1889 ret
= error("cannot seek back reflog for %s: %s",
1890 refname
, strerror(errno
));
1893 nread
= fread(buf
, cnt
, 1, logfp
);
1895 ret
= error("cannot read %d bytes from reflog for %s: %s",
1896 cnt
, refname
, strerror(errno
));
1901 scanp
= endp
= buf
+ cnt
;
1902 if (at_tail
&& scanp
[-1] == '\n')
1903 /* Looking at the final LF at the end of the file */
1907 while (buf
< scanp
) {
1909 * terminating LF of the previous line, or the beginning
1914 bp
= find_beginning_of_line(buf
, scanp
);
1918 * The newline is the end of the previous line,
1919 * so we know we have complete line starting
1920 * at (bp + 1). Prefix it onto any prior data
1921 * we collected for the line and process it.
1923 strbuf_splice(&sb
, 0, 0, bp
+ 1, endp
- (bp
+ 1));
1926 ret
= show_one_reflog_ent(&sb
, fn
, cb_data
);
1932 * We are at the start of the buffer, and the
1933 * start of the file; there is no previous
1934 * line, and we have everything for this one.
1935 * Process it, and we can end the loop.
1937 strbuf_splice(&sb
, 0, 0, buf
, endp
- buf
);
1938 ret
= show_one_reflog_ent(&sb
, fn
, cb_data
);
1945 * We are at the start of the buffer, and there
1946 * is more file to read backwards. Which means
1947 * we are in the middle of a line. Note that we
1948 * may get here even if *bp was a newline; that
1949 * just means we are at the exact end of the
1950 * previous line, rather than some spot in the
1953 * Save away what we have to be combined with
1954 * the data from the next read.
1956 strbuf_splice(&sb
, 0, 0, buf
, endp
- buf
);
1963 die("BUG: reverse reflog parser had leftover data");
1966 strbuf_release(&sb
);
1970 static int files_for_each_reflog_ent(struct ref_store
*ref_store
,
1971 const char *refname
,
1972 each_reflog_ent_fn fn
, void *cb_data
)
1974 struct files_ref_store
*refs
=
1975 files_downcast(ref_store
, REF_STORE_READ
,
1976 "for_each_reflog_ent");
1978 struct strbuf sb
= STRBUF_INIT
;
1981 files_reflog_path(refs
, &sb
, refname
);
1982 logfp
= fopen(sb
.buf
, "r");
1983 strbuf_release(&sb
);
1987 while (!ret
&& !strbuf_getwholeline(&sb
, logfp
, '\n'))
1988 ret
= show_one_reflog_ent(&sb
, fn
, cb_data
);
1990 strbuf_release(&sb
);
1994 struct files_reflog_iterator
{
1995 struct ref_iterator base
;
1997 struct ref_store
*ref_store
;
1998 struct dir_iterator
*dir_iterator
;
1999 struct object_id oid
;
2002 static int files_reflog_iterator_advance(struct ref_iterator
*ref_iterator
)
2004 struct files_reflog_iterator
*iter
=
2005 (struct files_reflog_iterator
*)ref_iterator
;
2006 struct dir_iterator
*diter
= iter
->dir_iterator
;
2009 while ((ok
= dir_iterator_advance(diter
)) == ITER_OK
) {
2012 if (!S_ISREG(diter
->st
.st_mode
))
2014 if (diter
->basename
[0] == '.')
2016 if (ends_with(diter
->basename
, ".lock"))
2019 if (refs_read_ref_full(iter
->ref_store
,
2020 diter
->relative_path
, 0,
2021 &iter
->oid
, &flags
)) {
2022 error("bad ref for %s", diter
->path
.buf
);
2026 iter
->base
.refname
= diter
->relative_path
;
2027 iter
->base
.oid
= &iter
->oid
;
2028 iter
->base
.flags
= flags
;
2032 iter
->dir_iterator
= NULL
;
2033 if (ref_iterator_abort(ref_iterator
) == ITER_ERROR
)
2038 static int files_reflog_iterator_peel(struct ref_iterator
*ref_iterator
,
2039 struct object_id
*peeled
)
2041 die("BUG: ref_iterator_peel() called for reflog_iterator");
2044 static int files_reflog_iterator_abort(struct ref_iterator
*ref_iterator
)
2046 struct files_reflog_iterator
*iter
=
2047 (struct files_reflog_iterator
*)ref_iterator
;
2050 if (iter
->dir_iterator
)
2051 ok
= dir_iterator_abort(iter
->dir_iterator
);
2053 base_ref_iterator_free(ref_iterator
);
2057 static struct ref_iterator_vtable files_reflog_iterator_vtable
= {
2058 files_reflog_iterator_advance
,
2059 files_reflog_iterator_peel
,
2060 files_reflog_iterator_abort
2063 static struct ref_iterator
*reflog_iterator_begin(struct ref_store
*ref_store
,
2066 struct files_reflog_iterator
*iter
= xcalloc(1, sizeof(*iter
));
2067 struct ref_iterator
*ref_iterator
= &iter
->base
;
2068 struct strbuf sb
= STRBUF_INIT
;
2070 base_ref_iterator_init(ref_iterator
, &files_reflog_iterator_vtable
, 0);
2071 strbuf_addf(&sb
, "%s/logs", gitdir
);
2072 iter
->dir_iterator
= dir_iterator_begin(sb
.buf
);
2073 iter
->ref_store
= ref_store
;
2074 strbuf_release(&sb
);
2076 return ref_iterator
;
2079 static enum iterator_selection
reflog_iterator_select(
2080 struct ref_iterator
*iter_worktree
,
2081 struct ref_iterator
*iter_common
,
2084 if (iter_worktree
) {
2086 * We're a bit loose here. We probably should ignore
2087 * common refs if they are accidentally added as
2088 * per-worktree refs.
2090 return ITER_SELECT_0
;
2091 } else if (iter_common
) {
2092 if (ref_type(iter_common
->refname
) == REF_TYPE_NORMAL
)
2093 return ITER_SELECT_1
;
2096 * The main ref store may contain main worktree's
2097 * per-worktree refs, which should be ignored
2104 static struct ref_iterator
*files_reflog_iterator_begin(struct ref_store
*ref_store
)
2106 struct files_ref_store
*refs
=
2107 files_downcast(ref_store
, REF_STORE_READ
,
2108 "reflog_iterator_begin");
2110 if (!strcmp(refs
->gitdir
, refs
->gitcommondir
)) {
2111 return reflog_iterator_begin(ref_store
, refs
->gitcommondir
);
2113 return merge_ref_iterator_begin(
2115 reflog_iterator_begin(ref_store
, refs
->gitdir
),
2116 reflog_iterator_begin(ref_store
, refs
->gitcommondir
),
2117 reflog_iterator_select
, refs
);
2122 * If update is a direct update of head_ref (the reference pointed to
2123 * by HEAD), then add an extra REF_LOG_ONLY update for HEAD.
2125 static int split_head_update(struct ref_update
*update
,
2126 struct ref_transaction
*transaction
,
2127 const char *head_ref
,
2128 struct string_list
*affected_refnames
,
2131 struct string_list_item
*item
;
2132 struct ref_update
*new_update
;
2134 if ((update
->flags
& REF_LOG_ONLY
) ||
2135 (update
->flags
& REF_ISPRUNING
) ||
2136 (update
->flags
& REF_UPDATE_VIA_HEAD
))
2139 if (strcmp(update
->refname
, head_ref
))
2143 * First make sure that HEAD is not already in the
2144 * transaction. This check is O(lg N) in the transaction
2145 * size, but it happens at most once per transaction.
2147 if (string_list_has_string(affected_refnames
, "HEAD")) {
2148 /* An entry already existed */
2150 "multiple updates for 'HEAD' (including one "
2151 "via its referent '%s') are not allowed",
2153 return TRANSACTION_NAME_CONFLICT
;
2156 new_update
= ref_transaction_add_update(
2157 transaction
, "HEAD",
2158 update
->flags
| REF_LOG_ONLY
| REF_NODEREF
,
2159 &update
->new_oid
, &update
->old_oid
,
2163 * Add "HEAD". This insertion is O(N) in the transaction
2164 * size, but it happens at most once per transaction.
2165 * Add new_update->refname instead of a literal "HEAD".
2167 if (strcmp(new_update
->refname
, "HEAD"))
2168 BUG("%s unexpectedly not 'HEAD'", new_update
->refname
);
2169 item
= string_list_insert(affected_refnames
, new_update
->refname
);
2170 item
->util
= new_update
;
2176 * update is for a symref that points at referent and doesn't have
2177 * REF_NODEREF set. Split it into two updates:
2178 * - The original update, but with REF_LOG_ONLY and REF_NODEREF set
2179 * - A new, separate update for the referent reference
2180 * Note that the new update will itself be subject to splitting when
2181 * the iteration gets to it.
2183 static int split_symref_update(struct files_ref_store
*refs
,
2184 struct ref_update
*update
,
2185 const char *referent
,
2186 struct ref_transaction
*transaction
,
2187 struct string_list
*affected_refnames
,
2190 struct string_list_item
*item
;
2191 struct ref_update
*new_update
;
2192 unsigned int new_flags
;
2195 * First make sure that referent is not already in the
2196 * transaction. This check is O(lg N) in the transaction
2197 * size, but it happens at most once per symref in a
2200 if (string_list_has_string(affected_refnames
, referent
)) {
2201 /* An entry already exists */
2203 "multiple updates for '%s' (including one "
2204 "via symref '%s') are not allowed",
2205 referent
, update
->refname
);
2206 return TRANSACTION_NAME_CONFLICT
;
2209 new_flags
= update
->flags
;
2210 if (!strcmp(update
->refname
, "HEAD")) {
2212 * Record that the new update came via HEAD, so that
2213 * when we process it, split_head_update() doesn't try
2214 * to add another reflog update for HEAD. Note that
2215 * this bit will be propagated if the new_update
2216 * itself needs to be split.
2218 new_flags
|= REF_UPDATE_VIA_HEAD
;
2221 new_update
= ref_transaction_add_update(
2222 transaction
, referent
, new_flags
,
2223 &update
->new_oid
, &update
->old_oid
,
2226 new_update
->parent_update
= update
;
2229 * Change the symbolic ref update to log only. Also, it
2230 * doesn't need to check its old SHA-1 value, as that will be
2231 * done when new_update is processed.
2233 update
->flags
|= REF_LOG_ONLY
| REF_NODEREF
;
2234 update
->flags
&= ~REF_HAVE_OLD
;
2237 * Add the referent. This insertion is O(N) in the transaction
2238 * size, but it happens at most once per symref in a
2239 * transaction. Make sure to add new_update->refname, which will
2240 * be valid as long as affected_refnames is in use, and NOT
2241 * referent, which might soon be freed by our caller.
2243 item
= string_list_insert(affected_refnames
, new_update
->refname
);
2245 BUG("%s unexpectedly found in affected_refnames",
2246 new_update
->refname
);
2247 item
->util
= new_update
;
2253 * Return the refname under which update was originally requested.
2255 static const char *original_update_refname(struct ref_update
*update
)
2257 while (update
->parent_update
)
2258 update
= update
->parent_update
;
2260 return update
->refname
;
2264 * Check whether the REF_HAVE_OLD and old_oid values stored in update
2265 * are consistent with oid, which is the reference's current value. If
2266 * everything is OK, return 0; otherwise, write an error message to
2267 * err and return -1.
2269 static int check_old_oid(struct ref_update
*update
, struct object_id
*oid
,
2272 if (!(update
->flags
& REF_HAVE_OLD
) ||
2273 !oidcmp(oid
, &update
->old_oid
))
2276 if (is_null_oid(&update
->old_oid
))
2277 strbuf_addf(err
, "cannot lock ref '%s': "
2278 "reference already exists",
2279 original_update_refname(update
));
2280 else if (is_null_oid(oid
))
2281 strbuf_addf(err
, "cannot lock ref '%s': "
2282 "reference is missing but expected %s",
2283 original_update_refname(update
),
2284 oid_to_hex(&update
->old_oid
));
2286 strbuf_addf(err
, "cannot lock ref '%s': "
2287 "is at %s but expected %s",
2288 original_update_refname(update
),
2290 oid_to_hex(&update
->old_oid
));
2296 * Prepare for carrying out update:
2297 * - Lock the reference referred to by update.
2298 * - Read the reference under lock.
2299 * - Check that its old SHA-1 value (if specified) is correct, and in
2300 * any case record it in update->lock->old_oid for later use when
2301 * writing the reflog.
2302 * - If it is a symref update without REF_NODEREF, split it up into a
2303 * REF_LOG_ONLY update of the symref and add a separate update for
2304 * the referent to transaction.
2305 * - If it is an update of head_ref, add a corresponding REF_LOG_ONLY
2308 static int lock_ref_for_update(struct files_ref_store
*refs
,
2309 struct ref_update
*update
,
2310 struct ref_transaction
*transaction
,
2311 const char *head_ref
,
2312 struct string_list
*affected_refnames
,
2315 struct strbuf referent
= STRBUF_INIT
;
2316 int mustexist
= (update
->flags
& REF_HAVE_OLD
) &&
2317 !is_null_oid(&update
->old_oid
);
2319 struct ref_lock
*lock
;
2321 files_assert_main_repository(refs
, "lock_ref_for_update");
2323 if ((update
->flags
& REF_HAVE_NEW
) && is_null_oid(&update
->new_oid
))
2324 update
->flags
|= REF_DELETING
;
2327 ret
= split_head_update(update
, transaction
, head_ref
,
2328 affected_refnames
, err
);
2333 ret
= lock_raw_ref(refs
, update
->refname
, mustexist
,
2334 affected_refnames
, NULL
,
2336 &update
->type
, err
);
2340 reason
= strbuf_detach(err
, NULL
);
2341 strbuf_addf(err
, "cannot lock ref '%s': %s",
2342 original_update_refname(update
), reason
);
2347 update
->backend_data
= lock
;
2349 if (update
->type
& REF_ISSYMREF
) {
2350 if (update
->flags
& REF_NODEREF
) {
2352 * We won't be reading the referent as part of
2353 * the transaction, so we have to read it here
2354 * to record and possibly check old_sha1:
2356 if (refs_read_ref_full(&refs
->base
,
2358 &lock
->old_oid
, NULL
)) {
2359 if (update
->flags
& REF_HAVE_OLD
) {
2360 strbuf_addf(err
, "cannot lock ref '%s': "
2361 "error reading reference",
2362 original_update_refname(update
));
2363 ret
= TRANSACTION_GENERIC_ERROR
;
2366 } else if (check_old_oid(update
, &lock
->old_oid
, err
)) {
2367 ret
= TRANSACTION_GENERIC_ERROR
;
2372 * Create a new update for the reference this
2373 * symref is pointing at. Also, we will record
2374 * and verify old_sha1 for this update as part
2375 * of processing the split-off update, so we
2376 * don't have to do it here.
2378 ret
= split_symref_update(refs
, update
,
2379 referent
.buf
, transaction
,
2380 affected_refnames
, err
);
2385 struct ref_update
*parent_update
;
2387 if (check_old_oid(update
, &lock
->old_oid
, err
)) {
2388 ret
= TRANSACTION_GENERIC_ERROR
;
2393 * If this update is happening indirectly because of a
2394 * symref update, record the old SHA-1 in the parent
2397 for (parent_update
= update
->parent_update
;
2399 parent_update
= parent_update
->parent_update
) {
2400 struct ref_lock
*parent_lock
= parent_update
->backend_data
;
2401 oidcpy(&parent_lock
->old_oid
, &lock
->old_oid
);
2405 if ((update
->flags
& REF_HAVE_NEW
) &&
2406 !(update
->flags
& REF_DELETING
) &&
2407 !(update
->flags
& REF_LOG_ONLY
)) {
2408 if (!(update
->type
& REF_ISSYMREF
) &&
2409 !oidcmp(&lock
->old_oid
, &update
->new_oid
)) {
2411 * The reference already has the desired
2412 * value, so we don't need to write it.
2414 } else if (write_ref_to_lockfile(lock
, &update
->new_oid
,
2416 char *write_err
= strbuf_detach(err
, NULL
);
2419 * The lock was freed upon failure of
2420 * write_ref_to_lockfile():
2422 update
->backend_data
= NULL
;
2424 "cannot update ref '%s': %s",
2425 update
->refname
, write_err
);
2427 ret
= TRANSACTION_GENERIC_ERROR
;
2430 update
->flags
|= REF_NEEDS_COMMIT
;
2433 if (!(update
->flags
& REF_NEEDS_COMMIT
)) {
2435 * We didn't call write_ref_to_lockfile(), so
2436 * the lockfile is still open. Close it to
2437 * free up the file descriptor:
2439 if (close_ref_gently(lock
)) {
2440 strbuf_addf(err
, "couldn't close '%s.lock'",
2442 ret
= TRANSACTION_GENERIC_ERROR
;
2448 strbuf_release(&referent
);
2452 struct files_transaction_backend_data
{
2453 struct ref_transaction
*packed_transaction
;
2454 int packed_refs_locked
;
2458 * Unlock any references in `transaction` that are still locked, and
2459 * mark the transaction closed.
2461 static void files_transaction_cleanup(struct files_ref_store
*refs
,
2462 struct ref_transaction
*transaction
)
2465 struct files_transaction_backend_data
*backend_data
=
2466 transaction
->backend_data
;
2467 struct strbuf err
= STRBUF_INIT
;
2469 for (i
= 0; i
< transaction
->nr
; i
++) {
2470 struct ref_update
*update
= transaction
->updates
[i
];
2471 struct ref_lock
*lock
= update
->backend_data
;
2475 update
->backend_data
= NULL
;
2479 if (backend_data
->packed_transaction
&&
2480 ref_transaction_abort(backend_data
->packed_transaction
, &err
)) {
2481 error("error aborting transaction: %s", err
.buf
);
2482 strbuf_release(&err
);
2485 if (backend_data
->packed_refs_locked
)
2486 packed_refs_unlock(refs
->packed_ref_store
);
2490 transaction
->state
= REF_TRANSACTION_CLOSED
;
2493 static int files_transaction_prepare(struct ref_store
*ref_store
,
2494 struct ref_transaction
*transaction
,
2497 struct files_ref_store
*refs
=
2498 files_downcast(ref_store
, REF_STORE_WRITE
,
2499 "ref_transaction_prepare");
2502 struct string_list affected_refnames
= STRING_LIST_INIT_NODUP
;
2503 char *head_ref
= NULL
;
2505 struct files_transaction_backend_data
*backend_data
;
2506 struct ref_transaction
*packed_transaction
= NULL
;
2510 if (!transaction
->nr
)
2513 backend_data
= xcalloc(1, sizeof(*backend_data
));
2514 transaction
->backend_data
= backend_data
;
2517 * Fail if a refname appears more than once in the
2518 * transaction. (If we end up splitting up any updates using
2519 * split_symref_update() or split_head_update(), those
2520 * functions will check that the new updates don't have the
2521 * same refname as any existing ones.) Also fail if any of the
2522 * updates use REF_ISPRUNING without REF_NODEREF.
2524 for (i
= 0; i
< transaction
->nr
; i
++) {
2525 struct ref_update
*update
= transaction
->updates
[i
];
2526 struct string_list_item
*item
=
2527 string_list_append(&affected_refnames
, update
->refname
);
2529 if ((update
->flags
& REF_ISPRUNING
) &&
2530 !(update
->flags
& REF_NODEREF
))
2531 BUG("REF_ISPRUNING set without REF_NODEREF");
2534 * We store a pointer to update in item->util, but at
2535 * the moment we never use the value of this field
2536 * except to check whether it is non-NULL.
2538 item
->util
= update
;
2540 string_list_sort(&affected_refnames
);
2541 if (ref_update_reject_duplicates(&affected_refnames
, err
)) {
2542 ret
= TRANSACTION_GENERIC_ERROR
;
2547 * Special hack: If a branch is updated directly and HEAD
2548 * points to it (may happen on the remote side of a push
2549 * for example) then logically the HEAD reflog should be
2552 * A generic solution would require reverse symref lookups,
2553 * but finding all symrefs pointing to a given branch would be
2554 * rather costly for this rare event (the direct update of a
2555 * branch) to be worth it. So let's cheat and check with HEAD
2556 * only, which should cover 99% of all usage scenarios (even
2557 * 100% of the default ones).
2559 * So if HEAD is a symbolic reference, then record the name of
2560 * the reference that it points to. If we see an update of
2561 * head_ref within the transaction, then split_head_update()
2562 * arranges for the reflog of HEAD to be updated, too.
2564 head_ref
= refs_resolve_refdup(ref_store
, "HEAD",
2565 RESOLVE_REF_NO_RECURSE
,
2568 if (head_ref
&& !(head_type
& REF_ISSYMREF
)) {
2569 FREE_AND_NULL(head_ref
);
2573 * Acquire all locks, verify old values if provided, check
2574 * that new values are valid, and write new values to the
2575 * lockfiles, ready to be activated. Only keep one lockfile
2576 * open at a time to avoid running out of file descriptors.
2577 * Note that lock_ref_for_update() might append more updates
2578 * to the transaction.
2580 for (i
= 0; i
< transaction
->nr
; i
++) {
2581 struct ref_update
*update
= transaction
->updates
[i
];
2583 ret
= lock_ref_for_update(refs
, update
, transaction
,
2584 head_ref
, &affected_refnames
, err
);
2588 if (update
->flags
& REF_DELETING
&&
2589 !(update
->flags
& REF_LOG_ONLY
) &&
2590 !(update
->flags
& REF_ISPRUNING
)) {
2592 * This reference has to be deleted from
2593 * packed-refs if it exists there.
2595 if (!packed_transaction
) {
2596 packed_transaction
= ref_store_transaction_begin(
2597 refs
->packed_ref_store
, err
);
2598 if (!packed_transaction
) {
2599 ret
= TRANSACTION_GENERIC_ERROR
;
2603 backend_data
->packed_transaction
=
2607 ref_transaction_add_update(
2608 packed_transaction
, update
->refname
,
2609 REF_HAVE_NEW
| REF_NODEREF
,
2610 &update
->new_oid
, NULL
,
2615 if (packed_transaction
) {
2616 if (packed_refs_lock(refs
->packed_ref_store
, 0, err
)) {
2617 ret
= TRANSACTION_GENERIC_ERROR
;
2620 backend_data
->packed_refs_locked
= 1;
2621 ret
= ref_transaction_prepare(packed_transaction
, err
);
2626 string_list_clear(&affected_refnames
, 0);
2629 files_transaction_cleanup(refs
, transaction
);
2631 transaction
->state
= REF_TRANSACTION_PREPARED
;
2636 static int files_transaction_finish(struct ref_store
*ref_store
,
2637 struct ref_transaction
*transaction
,
2640 struct files_ref_store
*refs
=
2641 files_downcast(ref_store
, 0, "ref_transaction_finish");
2644 struct strbuf sb
= STRBUF_INIT
;
2645 struct files_transaction_backend_data
*backend_data
;
2646 struct ref_transaction
*packed_transaction
;
2651 if (!transaction
->nr
) {
2652 transaction
->state
= REF_TRANSACTION_CLOSED
;
2656 backend_data
= transaction
->backend_data
;
2657 packed_transaction
= backend_data
->packed_transaction
;
2659 /* Perform updates first so live commits remain referenced */
2660 for (i
= 0; i
< transaction
->nr
; i
++) {
2661 struct ref_update
*update
= transaction
->updates
[i
];
2662 struct ref_lock
*lock
= update
->backend_data
;
2664 if (update
->flags
& REF_NEEDS_COMMIT
||
2665 update
->flags
& REF_LOG_ONLY
) {
2666 if (files_log_ref_write(refs
,
2670 update
->msg
, update
->flags
,
2672 char *old_msg
= strbuf_detach(err
, NULL
);
2674 strbuf_addf(err
, "cannot update the ref '%s': %s",
2675 lock
->ref_name
, old_msg
);
2678 update
->backend_data
= NULL
;
2679 ret
= TRANSACTION_GENERIC_ERROR
;
2683 if (update
->flags
& REF_NEEDS_COMMIT
) {
2684 clear_loose_ref_cache(refs
);
2685 if (commit_ref(lock
)) {
2686 strbuf_addf(err
, "couldn't set '%s'", lock
->ref_name
);
2688 update
->backend_data
= NULL
;
2689 ret
= TRANSACTION_GENERIC_ERROR
;
2696 * Now that updates are safely completed, we can perform
2697 * deletes. First delete the reflogs of any references that
2698 * will be deleted, since (in the unexpected event of an
2699 * error) leaving a reference without a reflog is less bad
2700 * than leaving a reflog without a reference (the latter is a
2701 * mildly invalid repository state):
2703 for (i
= 0; i
< transaction
->nr
; i
++) {
2704 struct ref_update
*update
= transaction
->updates
[i
];
2705 if (update
->flags
& REF_DELETING
&&
2706 !(update
->flags
& REF_LOG_ONLY
) &&
2707 !(update
->flags
& REF_ISPRUNING
)) {
2709 files_reflog_path(refs
, &sb
, update
->refname
);
2710 if (!unlink_or_warn(sb
.buf
))
2711 try_remove_empty_parents(refs
, update
->refname
,
2712 REMOVE_EMPTY_PARENTS_REFLOG
);
2717 * Perform deletes now that updates are safely completed.
2719 * First delete any packed versions of the references, while
2720 * retaining the packed-refs lock:
2722 if (packed_transaction
) {
2723 ret
= ref_transaction_commit(packed_transaction
, err
);
2724 ref_transaction_free(packed_transaction
);
2725 packed_transaction
= NULL
;
2726 backend_data
->packed_transaction
= NULL
;
2731 /* Now delete the loose versions of the references: */
2732 for (i
= 0; i
< transaction
->nr
; i
++) {
2733 struct ref_update
*update
= transaction
->updates
[i
];
2734 struct ref_lock
*lock
= update
->backend_data
;
2736 if (update
->flags
& REF_DELETING
&&
2737 !(update
->flags
& REF_LOG_ONLY
)) {
2738 if (!(update
->type
& REF_ISPACKED
) ||
2739 update
->type
& REF_ISSYMREF
) {
2740 /* It is a loose reference. */
2742 files_ref_path(refs
, &sb
, lock
->ref_name
);
2743 if (unlink_or_msg(sb
.buf
, err
)) {
2744 ret
= TRANSACTION_GENERIC_ERROR
;
2747 update
->flags
|= REF_DELETED_LOOSE
;
2752 clear_loose_ref_cache(refs
);
2755 files_transaction_cleanup(refs
, transaction
);
2757 for (i
= 0; i
< transaction
->nr
; i
++) {
2758 struct ref_update
*update
= transaction
->updates
[i
];
2760 if (update
->flags
& REF_DELETED_LOOSE
) {
2762 * The loose reference was deleted. Delete any
2763 * empty parent directories. (Note that this
2764 * can only work because we have already
2765 * removed the lockfile.)
2767 try_remove_empty_parents(refs
, update
->refname
,
2768 REMOVE_EMPTY_PARENTS_REF
);
2772 strbuf_release(&sb
);
2776 static int files_transaction_abort(struct ref_store
*ref_store
,
2777 struct ref_transaction
*transaction
,
2780 struct files_ref_store
*refs
=
2781 files_downcast(ref_store
, 0, "ref_transaction_abort");
2783 files_transaction_cleanup(refs
, transaction
);
2787 static int ref_present(const char *refname
,
2788 const struct object_id
*oid
, int flags
, void *cb_data
)
2790 struct string_list
*affected_refnames
= cb_data
;
2792 return string_list_has_string(affected_refnames
, refname
);
2795 static int files_initial_transaction_commit(struct ref_store
*ref_store
,
2796 struct ref_transaction
*transaction
,
2799 struct files_ref_store
*refs
=
2800 files_downcast(ref_store
, REF_STORE_WRITE
,
2801 "initial_ref_transaction_commit");
2804 struct string_list affected_refnames
= STRING_LIST_INIT_NODUP
;
2805 struct ref_transaction
*packed_transaction
= NULL
;
2809 if (transaction
->state
!= REF_TRANSACTION_OPEN
)
2810 die("BUG: commit called for transaction that is not open");
2812 /* Fail if a refname appears more than once in the transaction: */
2813 for (i
= 0; i
< transaction
->nr
; i
++)
2814 string_list_append(&affected_refnames
,
2815 transaction
->updates
[i
]->refname
);
2816 string_list_sort(&affected_refnames
);
2817 if (ref_update_reject_duplicates(&affected_refnames
, err
)) {
2818 ret
= TRANSACTION_GENERIC_ERROR
;
2823 * It's really undefined to call this function in an active
2824 * repository or when there are existing references: we are
2825 * only locking and changing packed-refs, so (1) any
2826 * simultaneous processes might try to change a reference at
2827 * the same time we do, and (2) any existing loose versions of
2828 * the references that we are setting would have precedence
2829 * over our values. But some remote helpers create the remote
2830 * "HEAD" and "master" branches before calling this function,
2831 * so here we really only check that none of the references
2832 * that we are creating already exists.
2834 if (refs_for_each_rawref(&refs
->base
, ref_present
,
2835 &affected_refnames
))
2836 die("BUG: initial ref transaction called with existing refs");
2838 packed_transaction
= ref_store_transaction_begin(refs
->packed_ref_store
, err
);
2839 if (!packed_transaction
) {
2840 ret
= TRANSACTION_GENERIC_ERROR
;
2844 for (i
= 0; i
< transaction
->nr
; i
++) {
2845 struct ref_update
*update
= transaction
->updates
[i
];
2847 if ((update
->flags
& REF_HAVE_OLD
) &&
2848 !is_null_oid(&update
->old_oid
))
2849 die("BUG: initial ref transaction with old_sha1 set");
2850 if (refs_verify_refname_available(&refs
->base
, update
->refname
,
2851 &affected_refnames
, NULL
,
2853 ret
= TRANSACTION_NAME_CONFLICT
;
2858 * Add a reference creation for this reference to the
2859 * packed-refs transaction:
2861 ref_transaction_add_update(packed_transaction
, update
->refname
,
2862 update
->flags
& ~REF_HAVE_OLD
,
2863 &update
->new_oid
, &update
->old_oid
,
2867 if (packed_refs_lock(refs
->packed_ref_store
, 0, err
)) {
2868 ret
= TRANSACTION_GENERIC_ERROR
;
2872 if (initial_ref_transaction_commit(packed_transaction
, err
)) {
2873 ret
= TRANSACTION_GENERIC_ERROR
;
2878 if (packed_transaction
)
2879 ref_transaction_free(packed_transaction
);
2880 packed_refs_unlock(refs
->packed_ref_store
);
2881 transaction
->state
= REF_TRANSACTION_CLOSED
;
2882 string_list_clear(&affected_refnames
, 0);
2886 struct expire_reflog_cb
{
2888 reflog_expiry_should_prune_fn
*should_prune_fn
;
2891 struct object_id last_kept_oid
;
2894 static int expire_reflog_ent(struct object_id
*ooid
, struct object_id
*noid
,
2895 const char *email
, timestamp_t timestamp
, int tz
,
2896 const char *message
, void *cb_data
)
2898 struct expire_reflog_cb
*cb
= cb_data
;
2899 struct expire_reflog_policy_cb
*policy_cb
= cb
->policy_cb
;
2901 if (cb
->flags
& EXPIRE_REFLOGS_REWRITE
)
2902 ooid
= &cb
->last_kept_oid
;
2904 if ((*cb
->should_prune_fn
)(ooid
, noid
, email
, timestamp
, tz
,
2905 message
, policy_cb
)) {
2907 printf("would prune %s", message
);
2908 else if (cb
->flags
& EXPIRE_REFLOGS_VERBOSE
)
2909 printf("prune %s", message
);
2912 fprintf(cb
->newlog
, "%s %s %s %"PRItime
" %+05d\t%s",
2913 oid_to_hex(ooid
), oid_to_hex(noid
),
2914 email
, timestamp
, tz
, message
);
2915 oidcpy(&cb
->last_kept_oid
, noid
);
2917 if (cb
->flags
& EXPIRE_REFLOGS_VERBOSE
)
2918 printf("keep %s", message
);
2923 static int files_reflog_expire(struct ref_store
*ref_store
,
2924 const char *refname
, const struct object_id
*oid
,
2926 reflog_expiry_prepare_fn prepare_fn
,
2927 reflog_expiry_should_prune_fn should_prune_fn
,
2928 reflog_expiry_cleanup_fn cleanup_fn
,
2929 void *policy_cb_data
)
2931 struct files_ref_store
*refs
=
2932 files_downcast(ref_store
, REF_STORE_WRITE
, "reflog_expire");
2933 static struct lock_file reflog_lock
;
2934 struct expire_reflog_cb cb
;
2935 struct ref_lock
*lock
;
2936 struct strbuf log_file_sb
= STRBUF_INIT
;
2940 struct strbuf err
= STRBUF_INIT
;
2942 memset(&cb
, 0, sizeof(cb
));
2944 cb
.policy_cb
= policy_cb_data
;
2945 cb
.should_prune_fn
= should_prune_fn
;
2948 * The reflog file is locked by holding the lock on the
2949 * reference itself, plus we might need to update the
2950 * reference if --updateref was specified:
2952 lock
= lock_ref_oid_basic(refs
, refname
, oid
,
2953 NULL
, NULL
, REF_NODEREF
,
2956 error("cannot lock ref '%s': %s", refname
, err
.buf
);
2957 strbuf_release(&err
);
2960 if (!refs_reflog_exists(ref_store
, refname
)) {
2965 files_reflog_path(refs
, &log_file_sb
, refname
);
2966 log_file
= strbuf_detach(&log_file_sb
, NULL
);
2967 if (!(flags
& EXPIRE_REFLOGS_DRY_RUN
)) {
2969 * Even though holding $GIT_DIR/logs/$reflog.lock has
2970 * no locking implications, we use the lock_file
2971 * machinery here anyway because it does a lot of the
2972 * work we need, including cleaning up if the program
2973 * exits unexpectedly.
2975 if (hold_lock_file_for_update(&reflog_lock
, log_file
, 0) < 0) {
2976 struct strbuf err
= STRBUF_INIT
;
2977 unable_to_lock_message(log_file
, errno
, &err
);
2978 error("%s", err
.buf
);
2979 strbuf_release(&err
);
2982 cb
.newlog
= fdopen_lock_file(&reflog_lock
, "w");
2984 error("cannot fdopen %s (%s)",
2985 get_lock_file_path(&reflog_lock
), strerror(errno
));
2990 (*prepare_fn
)(refname
, oid
, cb
.policy_cb
);
2991 refs_for_each_reflog_ent(ref_store
, refname
, expire_reflog_ent
, &cb
);
2992 (*cleanup_fn
)(cb
.policy_cb
);
2994 if (!(flags
& EXPIRE_REFLOGS_DRY_RUN
)) {
2996 * It doesn't make sense to adjust a reference pointed
2997 * to by a symbolic ref based on expiring entries in
2998 * the symbolic reference's reflog. Nor can we update
2999 * a reference if there are no remaining reflog
3002 int update
= (flags
& EXPIRE_REFLOGS_UPDATE_REF
) &&
3003 !(type
& REF_ISSYMREF
) &&
3004 !is_null_oid(&cb
.last_kept_oid
);
3006 if (close_lock_file_gently(&reflog_lock
)) {
3007 status
|= error("couldn't write %s: %s", log_file
,
3009 rollback_lock_file(&reflog_lock
);
3010 } else if (update
&&
3011 (write_in_full(get_lock_file_fd(&lock
->lk
),
3012 oid_to_hex(&cb
.last_kept_oid
), GIT_SHA1_HEXSZ
) < 0 ||
3013 write_str_in_full(get_lock_file_fd(&lock
->lk
), "\n") < 0 ||
3014 close_ref_gently(lock
) < 0)) {
3015 status
|= error("couldn't write %s",
3016 get_lock_file_path(&lock
->lk
));
3017 rollback_lock_file(&reflog_lock
);
3018 } else if (commit_lock_file(&reflog_lock
)) {
3019 status
|= error("unable to write reflog '%s' (%s)",
3020 log_file
, strerror(errno
));
3021 } else if (update
&& commit_ref(lock
)) {
3022 status
|= error("couldn't set %s", lock
->ref_name
);
3030 rollback_lock_file(&reflog_lock
);
3036 static int files_init_db(struct ref_store
*ref_store
, struct strbuf
*err
)
3038 struct files_ref_store
*refs
=
3039 files_downcast(ref_store
, REF_STORE_WRITE
, "init_db");
3040 struct strbuf sb
= STRBUF_INIT
;
3043 * Create .git/refs/{heads,tags}
3045 files_ref_path(refs
, &sb
, "refs/heads");
3046 safe_create_dir(sb
.buf
, 1);
3049 files_ref_path(refs
, &sb
, "refs/tags");
3050 safe_create_dir(sb
.buf
, 1);
3052 strbuf_release(&sb
);
3056 struct ref_storage_be refs_be_files
= {
3059 files_ref_store_create
,
3061 files_transaction_prepare
,
3062 files_transaction_finish
,
3063 files_transaction_abort
,
3064 files_initial_transaction_commit
,
3067 files_create_symref
,
3072 files_ref_iterator_begin
,
3075 files_reflog_iterator_begin
,
3076 files_for_each_reflog_ent
,
3077 files_for_each_reflog_ent_reverse
,
3078 files_reflog_exists
,
3079 files_create_reflog
,
3080 files_delete_reflog
,