3 #include "refs-internal.h"
5 #include "packed-backend.h"
6 #include "../iterator.h"
7 #include "../dir-iterator.h"
8 #include "../lockfile.h"
15 struct object_id old_oid
;
19 * Future: need to be in "struct repository"
20 * when doing a full libification.
22 struct files_ref_store
{
23 struct ref_store base
;
24 unsigned int store_flags
;
29 struct ref_cache
*loose
;
31 struct ref_store
*packed_ref_store
;
34 static void clear_loose_ref_cache(struct files_ref_store
*refs
)
37 free_ref_cache(refs
->loose
);
43 * Create a new submodule ref cache and add it to the internal
46 static struct ref_store
*files_ref_store_create(const char *gitdir
,
49 struct files_ref_store
*refs
= xcalloc(1, sizeof(*refs
));
50 struct ref_store
*ref_store
= (struct ref_store
*)refs
;
51 struct strbuf sb
= STRBUF_INIT
;
53 base_ref_store_init(ref_store
, &refs_be_files
);
54 refs
->store_flags
= flags
;
56 refs
->gitdir
= xstrdup(gitdir
);
57 get_common_dir_noenv(&sb
, gitdir
);
58 refs
->gitcommondir
= strbuf_detach(&sb
, NULL
);
59 strbuf_addf(&sb
, "%s/packed-refs", refs
->gitcommondir
);
60 refs
->packed_ref_store
= packed_ref_store_create(sb
.buf
, flags
);
67 * Die if refs is not the main ref store. caller is used in any
68 * necessary error messages.
70 static void files_assert_main_repository(struct files_ref_store
*refs
,
73 if (refs
->store_flags
& REF_STORE_MAIN
)
76 die("BUG: operation %s only allowed for main ref store", caller
);
80 * Downcast ref_store to files_ref_store. Die if ref_store is not a
81 * files_ref_store. required_flags is compared with ref_store's
82 * store_flags to ensure the ref_store has all required capabilities.
83 * "caller" is used in any necessary error messages.
85 static struct files_ref_store
*files_downcast(struct ref_store
*ref_store
,
86 unsigned int required_flags
,
89 struct files_ref_store
*refs
;
91 if (ref_store
->be
!= &refs_be_files
)
92 die("BUG: ref_store is type \"%s\" not \"files\" in %s",
93 ref_store
->be
->name
, caller
);
95 refs
= (struct files_ref_store
*)ref_store
;
97 if ((refs
->store_flags
& required_flags
) != required_flags
)
98 die("BUG: operation %s requires abilities 0x%x, but only have 0x%x",
99 caller
, required_flags
, refs
->store_flags
);
104 static void files_reflog_path(struct files_ref_store
*refs
,
110 * FIXME: of course this is wrong in multi worktree
111 * setting. To be fixed real soon.
113 strbuf_addf(sb
, "%s/logs", refs
->gitcommondir
);
117 switch (ref_type(refname
)) {
118 case REF_TYPE_PER_WORKTREE
:
119 case REF_TYPE_PSEUDOREF
:
120 strbuf_addf(sb
, "%s/logs/%s", refs
->gitdir
, refname
);
122 case REF_TYPE_NORMAL
:
123 strbuf_addf(sb
, "%s/logs/%s", refs
->gitcommondir
, refname
);
126 die("BUG: unknown ref type %d of ref %s",
127 ref_type(refname
), refname
);
131 static void files_ref_path(struct files_ref_store
*refs
,
135 switch (ref_type(refname
)) {
136 case REF_TYPE_PER_WORKTREE
:
137 case REF_TYPE_PSEUDOREF
:
138 strbuf_addf(sb
, "%s/%s", refs
->gitdir
, refname
);
140 case REF_TYPE_NORMAL
:
141 strbuf_addf(sb
, "%s/%s", refs
->gitcommondir
, refname
);
144 die("BUG: unknown ref type %d of ref %s",
145 ref_type(refname
), refname
);
150 * Read the loose references from the namespace dirname into dir
151 * (without recursing). dirname must end with '/'. dir must be the
152 * directory entry corresponding to dirname.
154 static void loose_fill_ref_dir(struct ref_store
*ref_store
,
155 struct ref_dir
*dir
, const char *dirname
)
157 struct files_ref_store
*refs
=
158 files_downcast(ref_store
, REF_STORE_READ
, "fill_ref_dir");
161 int dirnamelen
= strlen(dirname
);
162 struct strbuf refname
;
163 struct strbuf path
= STRBUF_INIT
;
166 files_ref_path(refs
, &path
, dirname
);
167 path_baselen
= path
.len
;
169 d
= opendir(path
.buf
);
171 strbuf_release(&path
);
175 strbuf_init(&refname
, dirnamelen
+ 257);
176 strbuf_add(&refname
, dirname
, dirnamelen
);
178 while ((de
= readdir(d
)) != NULL
) {
179 struct object_id oid
;
183 if (de
->d_name
[0] == '.')
185 if (ends_with(de
->d_name
, ".lock"))
187 strbuf_addstr(&refname
, de
->d_name
);
188 strbuf_addstr(&path
, de
->d_name
);
189 if (stat(path
.buf
, &st
) < 0) {
190 ; /* silently ignore */
191 } else if (S_ISDIR(st
.st_mode
)) {
192 strbuf_addch(&refname
, '/');
193 add_entry_to_dir(dir
,
194 create_dir_entry(dir
->cache
, refname
.buf
,
197 if (!refs_resolve_ref_unsafe(&refs
->base
,
202 flag
|= REF_ISBROKEN
;
203 } else if (is_null_oid(&oid
)) {
205 * It is so astronomically unlikely
206 * that NULL_SHA1 is the SHA-1 of an
207 * actual object that we consider its
208 * appearance in a loose reference
209 * file to be repo corruption
210 * (probably due to a software bug).
212 flag
|= REF_ISBROKEN
;
215 if (check_refname_format(refname
.buf
,
216 REFNAME_ALLOW_ONELEVEL
)) {
217 if (!refname_is_safe(refname
.buf
))
218 die("loose refname is dangerous: %s", refname
.buf
);
220 flag
|= REF_BAD_NAME
| REF_ISBROKEN
;
222 add_entry_to_dir(dir
,
223 create_ref_entry(refname
.buf
, &oid
, flag
));
225 strbuf_setlen(&refname
, dirnamelen
);
226 strbuf_setlen(&path
, path_baselen
);
228 strbuf_release(&refname
);
229 strbuf_release(&path
);
233 * Manually add refs/bisect, which, being per-worktree, might
234 * not appear in the directory listing for refs/ in the main
237 if (!strcmp(dirname
, "refs/")) {
238 int pos
= search_ref_dir(dir
, "refs/bisect/", 12);
241 struct ref_entry
*child_entry
= create_dir_entry(
242 dir
->cache
, "refs/bisect/", 12, 1);
243 add_entry_to_dir(dir
, child_entry
);
248 static struct ref_cache
*get_loose_ref_cache(struct files_ref_store
*refs
)
252 * Mark the top-level directory complete because we
253 * are about to read the only subdirectory that can
256 refs
->loose
= create_ref_cache(&refs
->base
, loose_fill_ref_dir
);
258 /* We're going to fill the top level ourselves: */
259 refs
->loose
->root
->flag
&= ~REF_INCOMPLETE
;
262 * Add an incomplete entry for "refs/" (to be filled
265 add_entry_to_dir(get_ref_dir(refs
->loose
->root
),
266 create_dir_entry(refs
->loose
, "refs/", 5, 1));
271 static int files_read_raw_ref(struct ref_store
*ref_store
,
272 const char *refname
, unsigned char *sha1
,
273 struct strbuf
*referent
, unsigned int *type
)
275 struct files_ref_store
*refs
=
276 files_downcast(ref_store
, REF_STORE_READ
, "read_raw_ref");
277 struct strbuf sb_contents
= STRBUF_INIT
;
278 struct strbuf sb_path
= STRBUF_INIT
;
285 int remaining_retries
= 3;
288 strbuf_reset(&sb_path
);
290 files_ref_path(refs
, &sb_path
, refname
);
296 * We might have to loop back here to avoid a race
297 * condition: first we lstat() the file, then we try
298 * to read it as a link or as a file. But if somebody
299 * changes the type of the file (file <-> directory
300 * <-> symlink) between the lstat() and reading, then
301 * we don't want to report that as an error but rather
302 * try again starting with the lstat().
304 * We'll keep a count of the retries, though, just to avoid
305 * any confusing situation sending us into an infinite loop.
308 if (remaining_retries
-- <= 0)
311 if (lstat(path
, &st
) < 0) {
314 if (refs_read_raw_ref(refs
->packed_ref_store
, refname
,
315 sha1
, referent
, type
)) {
323 /* Follow "normalized" - ie "refs/.." symlinks by hand */
324 if (S_ISLNK(st
.st_mode
)) {
325 strbuf_reset(&sb_contents
);
326 if (strbuf_readlink(&sb_contents
, path
, 0) < 0) {
327 if (errno
== ENOENT
|| errno
== EINVAL
)
328 /* inconsistent with lstat; retry */
333 if (starts_with(sb_contents
.buf
, "refs/") &&
334 !check_refname_format(sb_contents
.buf
, 0)) {
335 strbuf_swap(&sb_contents
, referent
);
336 *type
|= REF_ISSYMREF
;
341 * It doesn't look like a refname; fall through to just
342 * treating it like a non-symlink, and reading whatever it
347 /* Is it a directory? */
348 if (S_ISDIR(st
.st_mode
)) {
350 * Even though there is a directory where the loose
351 * ref is supposed to be, there could still be a
354 if (refs_read_raw_ref(refs
->packed_ref_store
, refname
,
355 sha1
, referent
, type
)) {
364 * Anything else, just open it and try to use it as
367 fd
= open(path
, O_RDONLY
);
369 if (errno
== ENOENT
&& !S_ISLNK(st
.st_mode
))
370 /* inconsistent with lstat; retry */
375 strbuf_reset(&sb_contents
);
376 if (strbuf_read(&sb_contents
, fd
, 256) < 0) {
377 int save_errno
= errno
;
383 strbuf_rtrim(&sb_contents
);
384 buf
= sb_contents
.buf
;
385 if (starts_with(buf
, "ref:")) {
387 while (isspace(*buf
))
390 strbuf_reset(referent
);
391 strbuf_addstr(referent
, buf
);
392 *type
|= REF_ISSYMREF
;
398 * Please note that FETCH_HEAD has additional
399 * data after the sha.
401 if (get_sha1_hex(buf
, sha1
) ||
402 (buf
[40] != '\0' && !isspace(buf
[40]))) {
403 *type
|= REF_ISBROKEN
;
412 strbuf_release(&sb_path
);
413 strbuf_release(&sb_contents
);
418 static void unlock_ref(struct ref_lock
*lock
)
420 /* Do not free lock->lk -- atexit() still looks at them */
422 rollback_lock_file(lock
->lk
);
423 free(lock
->ref_name
);
428 * Lock refname, without following symrefs, and set *lock_p to point
429 * at a newly-allocated lock object. Fill in lock->old_oid, referent,
430 * and type similarly to read_raw_ref().
432 * The caller must verify that refname is a "safe" reference name (in
433 * the sense of refname_is_safe()) before calling this function.
435 * If the reference doesn't already exist, verify that refname doesn't
436 * have a D/F conflict with any existing references. extras and skip
437 * are passed to refs_verify_refname_available() for this check.
439 * If mustexist is not set and the reference is not found or is
440 * broken, lock the reference anyway but clear sha1.
442 * Return 0 on success. On failure, write an error message to err and
443 * return TRANSACTION_NAME_CONFLICT or TRANSACTION_GENERIC_ERROR.
445 * Implementation note: This function is basically
450 * but it includes a lot more code to
451 * - Deal with possible races with other processes
452 * - Avoid calling refs_verify_refname_available() when it can be
453 * avoided, namely if we were successfully able to read the ref
454 * - Generate informative error messages in the case of failure
456 static int lock_raw_ref(struct files_ref_store
*refs
,
457 const char *refname
, int mustexist
,
458 const struct string_list
*extras
,
459 const struct string_list
*skip
,
460 struct ref_lock
**lock_p
,
461 struct strbuf
*referent
,
465 struct ref_lock
*lock
;
466 struct strbuf ref_file
= STRBUF_INIT
;
467 int attempts_remaining
= 3;
468 int ret
= TRANSACTION_GENERIC_ERROR
;
471 files_assert_main_repository(refs
, "lock_raw_ref");
475 /* First lock the file so it can't change out from under us. */
477 *lock_p
= lock
= xcalloc(1, sizeof(*lock
));
479 lock
->ref_name
= xstrdup(refname
);
480 files_ref_path(refs
, &ref_file
, refname
);
483 switch (safe_create_leading_directories(ref_file
.buf
)) {
488 * Suppose refname is "refs/foo/bar". We just failed
489 * to create the containing directory, "refs/foo",
490 * because there was a non-directory in the way. This
491 * indicates a D/F conflict, probably because of
492 * another reference such as "refs/foo". There is no
493 * reason to expect this error to be transitory.
495 if (refs_verify_refname_available(&refs
->base
, refname
,
496 extras
, skip
, err
)) {
499 * To the user the relevant error is
500 * that the "mustexist" reference is
504 strbuf_addf(err
, "unable to resolve reference '%s'",
508 * The error message set by
509 * refs_verify_refname_available() is
512 ret
= TRANSACTION_NAME_CONFLICT
;
516 * The file that is in the way isn't a loose
517 * reference. Report it as a low-level
520 strbuf_addf(err
, "unable to create lock file %s.lock; "
521 "non-directory in the way",
526 /* Maybe another process was tidying up. Try again. */
527 if (--attempts_remaining
> 0)
531 strbuf_addf(err
, "unable to create directory for %s",
537 lock
->lk
= xcalloc(1, sizeof(struct lock_file
));
539 if (hold_lock_file_for_update(lock
->lk
, ref_file
.buf
, LOCK_NO_DEREF
) < 0) {
540 if (errno
== ENOENT
&& --attempts_remaining
> 0) {
542 * Maybe somebody just deleted one of the
543 * directories leading to ref_file. Try
548 unable_to_lock_message(ref_file
.buf
, errno
, err
);
554 * Now we hold the lock and can read the reference without
555 * fear that its value will change.
558 if (files_read_raw_ref(&refs
->base
, refname
,
559 lock
->old_oid
.hash
, referent
, type
)) {
560 if (errno
== ENOENT
) {
562 /* Garden variety missing reference. */
563 strbuf_addf(err
, "unable to resolve reference '%s'",
568 * Reference is missing, but that's OK. We
569 * know that there is not a conflict with
570 * another loose reference because
571 * (supposing that we are trying to lock
572 * reference "refs/foo/bar"):
574 * - We were successfully able to create
575 * the lockfile refs/foo/bar.lock, so we
576 * know there cannot be a loose reference
579 * - We got ENOENT and not EISDIR, so we
580 * know that there cannot be a loose
581 * reference named "refs/foo/bar/baz".
584 } else if (errno
== EISDIR
) {
586 * There is a directory in the way. It might have
587 * contained references that have been deleted. If
588 * we don't require that the reference already
589 * exists, try to remove the directory so that it
590 * doesn't cause trouble when we want to rename the
591 * lockfile into place later.
594 /* Garden variety missing reference. */
595 strbuf_addf(err
, "unable to resolve reference '%s'",
598 } else if (remove_dir_recursively(&ref_file
,
599 REMOVE_DIR_EMPTY_ONLY
)) {
600 if (refs_verify_refname_available(
601 &refs
->base
, refname
,
602 extras
, skip
, err
)) {
604 * The error message set by
605 * verify_refname_available() is OK.
607 ret
= TRANSACTION_NAME_CONFLICT
;
611 * We can't delete the directory,
612 * but we also don't know of any
613 * references that it should
616 strbuf_addf(err
, "there is a non-empty directory '%s' "
617 "blocking reference '%s'",
618 ref_file
.buf
, refname
);
622 } else if (errno
== EINVAL
&& (*type
& REF_ISBROKEN
)) {
623 strbuf_addf(err
, "unable to resolve reference '%s': "
624 "reference broken", refname
);
627 strbuf_addf(err
, "unable to resolve reference '%s': %s",
628 refname
, strerror(errno
));
633 * If the ref did not exist and we are creating it,
634 * make sure there is no existing packed ref that
635 * conflicts with refname:
637 if (refs_verify_refname_available(
638 refs
->packed_ref_store
, refname
,
651 strbuf_release(&ref_file
);
655 static int files_peel_ref(struct ref_store
*ref_store
,
656 const char *refname
, unsigned char *sha1
)
658 struct files_ref_store
*refs
=
659 files_downcast(ref_store
, REF_STORE_READ
| REF_STORE_ODB
,
662 unsigned char base
[20];
664 if (current_ref_iter
&& current_ref_iter
->refname
== refname
) {
665 struct object_id peeled
;
667 if (ref_iterator_peel(current_ref_iter
, &peeled
))
669 hashcpy(sha1
, peeled
.hash
);
673 if (refs_read_ref_full(ref_store
, refname
,
674 RESOLVE_REF_READING
, base
, &flag
))
678 * If the reference is packed, read its ref_entry from the
679 * cache in the hope that we already know its peeled value.
680 * We only try this optimization on packed references because
681 * (a) forcing the filling of the loose reference cache could
682 * be expensive and (b) loose references anyway usually do not
683 * have REF_KNOWS_PEELED.
685 if (flag
& REF_ISPACKED
&&
686 !refs_peel_ref(refs
->packed_ref_store
, refname
, sha1
))
689 return peel_object(base
, sha1
);
692 struct files_ref_iterator
{
693 struct ref_iterator base
;
695 struct ref_iterator
*iter0
;
699 static int files_ref_iterator_advance(struct ref_iterator
*ref_iterator
)
701 struct files_ref_iterator
*iter
=
702 (struct files_ref_iterator
*)ref_iterator
;
705 while ((ok
= ref_iterator_advance(iter
->iter0
)) == ITER_OK
) {
706 if (iter
->flags
& DO_FOR_EACH_PER_WORKTREE_ONLY
&&
707 ref_type(iter
->iter0
->refname
) != REF_TYPE_PER_WORKTREE
)
710 if (!(iter
->flags
& DO_FOR_EACH_INCLUDE_BROKEN
) &&
711 !ref_resolves_to_object(iter
->iter0
->refname
,
716 iter
->base
.refname
= iter
->iter0
->refname
;
717 iter
->base
.oid
= iter
->iter0
->oid
;
718 iter
->base
.flags
= iter
->iter0
->flags
;
723 if (ref_iterator_abort(ref_iterator
) != ITER_DONE
)
729 static int files_ref_iterator_peel(struct ref_iterator
*ref_iterator
,
730 struct object_id
*peeled
)
732 struct files_ref_iterator
*iter
=
733 (struct files_ref_iterator
*)ref_iterator
;
735 return ref_iterator_peel(iter
->iter0
, peeled
);
738 static int files_ref_iterator_abort(struct ref_iterator
*ref_iterator
)
740 struct files_ref_iterator
*iter
=
741 (struct files_ref_iterator
*)ref_iterator
;
745 ok
= ref_iterator_abort(iter
->iter0
);
747 base_ref_iterator_free(ref_iterator
);
751 static struct ref_iterator_vtable files_ref_iterator_vtable
= {
752 files_ref_iterator_advance
,
753 files_ref_iterator_peel
,
754 files_ref_iterator_abort
757 static struct ref_iterator
*files_ref_iterator_begin(
758 struct ref_store
*ref_store
,
759 const char *prefix
, unsigned int flags
)
761 struct files_ref_store
*refs
;
762 struct ref_iterator
*loose_iter
, *packed_iter
;
763 struct files_ref_iterator
*iter
;
764 struct ref_iterator
*ref_iterator
;
765 unsigned int required_flags
= REF_STORE_READ
;
767 if (!(flags
& DO_FOR_EACH_INCLUDE_BROKEN
))
768 required_flags
|= REF_STORE_ODB
;
770 refs
= files_downcast(ref_store
, required_flags
, "ref_iterator_begin");
772 iter
= xcalloc(1, sizeof(*iter
));
773 ref_iterator
= &iter
->base
;
774 base_ref_iterator_init(ref_iterator
, &files_ref_iterator_vtable
);
777 * We must make sure that all loose refs are read before
778 * accessing the packed-refs file; this avoids a race
779 * condition if loose refs are migrated to the packed-refs
780 * file by a simultaneous process, but our in-memory view is
781 * from before the migration. We ensure this as follows:
782 * First, we call start the loose refs iteration with its
783 * `prime_ref` argument set to true. This causes the loose
784 * references in the subtree to be pre-read into the cache.
785 * (If they've already been read, that's OK; we only need to
786 * guarantee that they're read before the packed refs, not
787 * *how much* before.) After that, we call
788 * packed_ref_iterator_begin(), which internally checks
789 * whether the packed-ref cache is up to date with what is on
790 * disk, and re-reads it if not.
793 loose_iter
= cache_ref_iterator_begin(get_loose_ref_cache(refs
),
797 * The packed-refs file might contain broken references, for
798 * example an old version of a reference that points at an
799 * object that has since been garbage-collected. This is OK as
800 * long as there is a corresponding loose reference that
801 * overrides it, and we don't want to emit an error message in
802 * this case. So ask the packed_ref_store for all of its
803 * references, and (if needed) do our own check for broken
804 * ones in files_ref_iterator_advance(), after we have merged
805 * the packed and loose references.
807 packed_iter
= refs_ref_iterator_begin(
808 refs
->packed_ref_store
, prefix
, 0,
809 DO_FOR_EACH_INCLUDE_BROKEN
);
811 iter
->iter0
= overlay_ref_iterator_begin(loose_iter
, packed_iter
);
818 * Verify that the reference locked by lock has the value old_sha1.
819 * Fail if the reference doesn't exist and mustexist is set. Return 0
820 * on success. On error, write an error message to err, set errno, and
821 * return a negative value.
823 static int verify_lock(struct ref_store
*ref_store
, struct ref_lock
*lock
,
824 const unsigned char *old_sha1
, int mustexist
,
829 if (refs_read_ref_full(ref_store
, lock
->ref_name
,
830 mustexist
? RESOLVE_REF_READING
: 0,
831 lock
->old_oid
.hash
, NULL
)) {
833 int save_errno
= errno
;
834 strbuf_addf(err
, "can't verify ref '%s'", lock
->ref_name
);
838 oidclr(&lock
->old_oid
);
842 if (old_sha1
&& hashcmp(lock
->old_oid
.hash
, old_sha1
)) {
843 strbuf_addf(err
, "ref '%s' is at %s but expected %s",
845 oid_to_hex(&lock
->old_oid
),
846 sha1_to_hex(old_sha1
));
853 static int remove_empty_directories(struct strbuf
*path
)
856 * we want to create a file but there is a directory there;
857 * if that is an empty directory (or a directory that contains
858 * only empty directories), remove them.
860 return remove_dir_recursively(path
, REMOVE_DIR_EMPTY_ONLY
);
863 static int create_reflock(const char *path
, void *cb
)
865 struct lock_file
*lk
= cb
;
867 return hold_lock_file_for_update(lk
, path
, LOCK_NO_DEREF
) < 0 ? -1 : 0;
871 * Locks a ref returning the lock on success and NULL on failure.
872 * On failure errno is set to something meaningful.
874 static struct ref_lock
*lock_ref_sha1_basic(struct files_ref_store
*refs
,
876 const unsigned char *old_sha1
,
877 const struct string_list
*extras
,
878 const struct string_list
*skip
,
879 unsigned int flags
, int *type
,
882 struct strbuf ref_file
= STRBUF_INIT
;
883 struct ref_lock
*lock
;
885 int mustexist
= (old_sha1
&& !is_null_sha1(old_sha1
));
886 int resolve_flags
= RESOLVE_REF_NO_RECURSE
;
889 files_assert_main_repository(refs
, "lock_ref_sha1_basic");
892 lock
= xcalloc(1, sizeof(struct ref_lock
));
895 resolve_flags
|= RESOLVE_REF_READING
;
896 if (flags
& REF_DELETING
)
897 resolve_flags
|= RESOLVE_REF_ALLOW_BAD_NAME
;
899 files_ref_path(refs
, &ref_file
, refname
);
900 resolved
= !!refs_resolve_ref_unsafe(&refs
->base
,
901 refname
, resolve_flags
,
902 lock
->old_oid
.hash
, type
);
903 if (!resolved
&& errno
== EISDIR
) {
905 * we are trying to lock foo but we used to
906 * have foo/bar which now does not exist;
907 * it is normal for the empty directory 'foo'
910 if (remove_empty_directories(&ref_file
)) {
912 if (!refs_verify_refname_available(
914 refname
, extras
, skip
, err
))
915 strbuf_addf(err
, "there are still refs under '%s'",
919 resolved
= !!refs_resolve_ref_unsafe(&refs
->base
,
920 refname
, resolve_flags
,
921 lock
->old_oid
.hash
, type
);
925 if (last_errno
!= ENOTDIR
||
926 !refs_verify_refname_available(&refs
->base
, refname
,
928 strbuf_addf(err
, "unable to resolve reference '%s': %s",
929 refname
, strerror(last_errno
));
935 * If the ref did not exist and we are creating it, make sure
936 * there is no existing packed ref whose name begins with our
937 * refname, nor a packed ref whose name is a proper prefix of
940 if (is_null_oid(&lock
->old_oid
) &&
941 refs_verify_refname_available(refs
->packed_ref_store
, refname
,
942 extras
, skip
, err
)) {
943 last_errno
= ENOTDIR
;
947 lock
->lk
= xcalloc(1, sizeof(struct lock_file
));
949 lock
->ref_name
= xstrdup(refname
);
951 if (raceproof_create_file(ref_file
.buf
, create_reflock
, lock
->lk
)) {
953 unable_to_lock_message(ref_file
.buf
, errno
, err
);
957 if (verify_lock(&refs
->base
, lock
, old_sha1
, mustexist
, err
)) {
968 strbuf_release(&ref_file
);
973 struct ref_to_prune
{
974 struct ref_to_prune
*next
;
975 unsigned char sha1
[20];
976 char name
[FLEX_ARRAY
];
980 REMOVE_EMPTY_PARENTS_REF
= 0x01,
981 REMOVE_EMPTY_PARENTS_REFLOG
= 0x02
985 * Remove empty parent directories associated with the specified
986 * reference and/or its reflog, but spare [logs/]refs/ and immediate
987 * subdirs. flags is a combination of REMOVE_EMPTY_PARENTS_REF and/or
988 * REMOVE_EMPTY_PARENTS_REFLOG.
990 static void try_remove_empty_parents(struct files_ref_store
*refs
,
994 struct strbuf buf
= STRBUF_INIT
;
995 struct strbuf sb
= STRBUF_INIT
;
999 strbuf_addstr(&buf
, refname
);
1001 for (i
= 0; i
< 2; i
++) { /* refs/{heads,tags,...}/ */
1002 while (*p
&& *p
!= '/')
1004 /* tolerate duplicate slashes; see check_refname_format() */
1008 q
= buf
.buf
+ buf
.len
;
1009 while (flags
& (REMOVE_EMPTY_PARENTS_REF
| REMOVE_EMPTY_PARENTS_REFLOG
)) {
1010 while (q
> p
&& *q
!= '/')
1012 while (q
> p
&& *(q
-1) == '/')
1016 strbuf_setlen(&buf
, q
- buf
.buf
);
1019 files_ref_path(refs
, &sb
, buf
.buf
);
1020 if ((flags
& REMOVE_EMPTY_PARENTS_REF
) && rmdir(sb
.buf
))
1021 flags
&= ~REMOVE_EMPTY_PARENTS_REF
;
1024 files_reflog_path(refs
, &sb
, buf
.buf
);
1025 if ((flags
& REMOVE_EMPTY_PARENTS_REFLOG
) && rmdir(sb
.buf
))
1026 flags
&= ~REMOVE_EMPTY_PARENTS_REFLOG
;
1028 strbuf_release(&buf
);
1029 strbuf_release(&sb
);
1032 /* make sure nobody touched the ref, and unlink */
1033 static void prune_ref(struct files_ref_store
*refs
, struct ref_to_prune
*r
)
1035 struct ref_transaction
*transaction
;
1036 struct strbuf err
= STRBUF_INIT
;
1038 if (check_refname_format(r
->name
, 0))
1041 transaction
= ref_store_transaction_begin(&refs
->base
, &err
);
1043 ref_transaction_delete(transaction
, r
->name
, r
->sha1
,
1044 REF_ISPRUNING
| REF_NODEREF
, NULL
, &err
) ||
1045 ref_transaction_commit(transaction
, &err
)) {
1046 ref_transaction_free(transaction
);
1047 error("%s", err
.buf
);
1048 strbuf_release(&err
);
1051 ref_transaction_free(transaction
);
1052 strbuf_release(&err
);
1055 static void prune_refs(struct files_ref_store
*refs
, struct ref_to_prune
*r
)
1064 * Return true if the specified reference should be packed.
1066 static int should_pack_ref(const char *refname
,
1067 const struct object_id
*oid
, unsigned int ref_flags
,
1068 unsigned int pack_flags
)
1070 /* Do not pack per-worktree refs: */
1071 if (ref_type(refname
) != REF_TYPE_NORMAL
)
1074 /* Do not pack non-tags unless PACK_REFS_ALL is set: */
1075 if (!(pack_flags
& PACK_REFS_ALL
) && !starts_with(refname
, "refs/tags/"))
1078 /* Do not pack symbolic refs: */
1079 if (ref_flags
& REF_ISSYMREF
)
1082 /* Do not pack broken refs: */
1083 if (!ref_resolves_to_object(refname
, oid
, ref_flags
))
1089 static int files_pack_refs(struct ref_store
*ref_store
, unsigned int flags
)
1091 struct files_ref_store
*refs
=
1092 files_downcast(ref_store
, REF_STORE_WRITE
| REF_STORE_ODB
,
1094 struct ref_iterator
*iter
;
1096 struct ref_to_prune
*refs_to_prune
= NULL
;
1097 struct strbuf err
= STRBUF_INIT
;
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 * Create an entry in the packed-refs cache equivalent
1114 * to the one from the loose ref cache, except that
1115 * we don't copy the peeled status, because we want it
1118 add_packed_ref(refs
->packed_ref_store
, iter
->refname
, iter
->oid
);
1120 /* Schedule the loose reference for pruning if requested. */
1121 if ((flags
& PACK_REFS_PRUNE
)) {
1122 struct ref_to_prune
*n
;
1123 FLEX_ALLOC_STR(n
, name
, iter
->refname
);
1124 hashcpy(n
->sha1
, iter
->oid
->hash
);
1125 n
->next
= refs_to_prune
;
1129 if (ok
!= ITER_DONE
)
1130 die("error while iterating over references");
1132 if (commit_packed_refs(refs
->packed_ref_store
, &err
))
1133 die("unable to overwrite old ref-pack file: %s", err
.buf
);
1134 packed_refs_unlock(refs
->packed_ref_store
);
1136 prune_refs(refs
, refs_to_prune
);
1137 strbuf_release(&err
);
1141 static int files_delete_refs(struct ref_store
*ref_store
, const char *msg
,
1142 struct string_list
*refnames
, unsigned int flags
)
1144 struct files_ref_store
*refs
=
1145 files_downcast(ref_store
, REF_STORE_WRITE
, "delete_refs");
1146 struct strbuf err
= STRBUF_INIT
;
1152 if (packed_refs_lock(refs
->packed_ref_store
, 0, &err
))
1155 if (repack_without_refs(refs
->packed_ref_store
, refnames
, &err
)) {
1156 packed_refs_unlock(refs
->packed_ref_store
);
1160 packed_refs_unlock(refs
->packed_ref_store
);
1162 for (i
= 0; i
< refnames
->nr
; i
++) {
1163 const char *refname
= refnames
->items
[i
].string
;
1165 if (refs_delete_ref(&refs
->base
, msg
, refname
, NULL
, flags
))
1166 result
|= error(_("could not remove reference %s"), refname
);
1169 strbuf_release(&err
);
1174 * If we failed to rewrite the packed-refs file, then it is
1175 * unsafe to try to remove loose refs, because doing so might
1176 * expose an obsolete packed value for a reference that might
1177 * even point at an object that has been garbage collected.
1179 if (refnames
->nr
== 1)
1180 error(_("could not delete reference %s: %s"),
1181 refnames
->items
[0].string
, err
.buf
);
1183 error(_("could not delete references: %s"), err
.buf
);
1185 strbuf_release(&err
);
1190 * People using contrib's git-new-workdir have .git/logs/refs ->
1191 * /some/other/path/.git/logs/refs, and that may live on another device.
1193 * IOW, to avoid cross device rename errors, the temporary renamed log must
1194 * live into logs/refs.
1196 #define TMP_RENAMED_LOG "refs/.tmp-renamed-log"
1199 const char *tmp_renamed_log
;
1203 static int rename_tmp_log_callback(const char *path
, void *cb_data
)
1205 struct rename_cb
*cb
= cb_data
;
1207 if (rename(cb
->tmp_renamed_log
, path
)) {
1209 * rename(a, b) when b is an existing directory ought
1210 * to result in ISDIR, but Solaris 5.8 gives ENOTDIR.
1211 * Sheesh. Record the true errno for error reporting,
1212 * but report EISDIR to raceproof_create_file() so
1213 * that it knows to retry.
1215 cb
->true_errno
= errno
;
1216 if (errno
== ENOTDIR
)
1224 static int rename_tmp_log(struct files_ref_store
*refs
, const char *newrefname
)
1226 struct strbuf path
= STRBUF_INIT
;
1227 struct strbuf tmp
= STRBUF_INIT
;
1228 struct rename_cb cb
;
1231 files_reflog_path(refs
, &path
, newrefname
);
1232 files_reflog_path(refs
, &tmp
, TMP_RENAMED_LOG
);
1233 cb
.tmp_renamed_log
= tmp
.buf
;
1234 ret
= raceproof_create_file(path
.buf
, rename_tmp_log_callback
, &cb
);
1236 if (errno
== EISDIR
)
1237 error("directory not empty: %s", path
.buf
);
1239 error("unable to move logfile %s to %s: %s",
1241 strerror(cb
.true_errno
));
1244 strbuf_release(&path
);
1245 strbuf_release(&tmp
);
1249 static int write_ref_to_lockfile(struct ref_lock
*lock
,
1250 const struct object_id
*oid
, struct strbuf
*err
);
1251 static int commit_ref_update(struct files_ref_store
*refs
,
1252 struct ref_lock
*lock
,
1253 const struct object_id
*oid
, const char *logmsg
,
1254 struct strbuf
*err
);
1256 static int files_rename_ref(struct ref_store
*ref_store
,
1257 const char *oldrefname
, const char *newrefname
,
1260 struct files_ref_store
*refs
=
1261 files_downcast(ref_store
, REF_STORE_WRITE
, "rename_ref");
1262 struct object_id oid
, orig_oid
;
1263 int flag
= 0, logmoved
= 0;
1264 struct ref_lock
*lock
;
1265 struct stat loginfo
;
1266 struct strbuf sb_oldref
= STRBUF_INIT
;
1267 struct strbuf sb_newref
= STRBUF_INIT
;
1268 struct strbuf tmp_renamed_log
= STRBUF_INIT
;
1270 struct strbuf err
= STRBUF_INIT
;
1272 files_reflog_path(refs
, &sb_oldref
, oldrefname
);
1273 files_reflog_path(refs
, &sb_newref
, newrefname
);
1274 files_reflog_path(refs
, &tmp_renamed_log
, TMP_RENAMED_LOG
);
1276 log
= !lstat(sb_oldref
.buf
, &loginfo
);
1277 if (log
&& S_ISLNK(loginfo
.st_mode
)) {
1278 ret
= error("reflog for %s is a symlink", oldrefname
);
1282 if (!refs_resolve_ref_unsafe(&refs
->base
, oldrefname
,
1283 RESOLVE_REF_READING
| RESOLVE_REF_NO_RECURSE
,
1284 orig_oid
.hash
, &flag
)) {
1285 ret
= error("refname %s not found", oldrefname
);
1289 if (flag
& REF_ISSYMREF
) {
1290 ret
= error("refname %s is a symbolic ref, renaming it is not supported",
1294 if (!refs_rename_ref_available(&refs
->base
, oldrefname
, newrefname
)) {
1299 if (log
&& rename(sb_oldref
.buf
, tmp_renamed_log
.buf
)) {
1300 ret
= error("unable to move logfile logs/%s to logs/"TMP_RENAMED_LOG
": %s",
1301 oldrefname
, strerror(errno
));
1305 if (refs_delete_ref(&refs
->base
, logmsg
, oldrefname
,
1306 orig_oid
.hash
, REF_NODEREF
)) {
1307 error("unable to delete old %s", oldrefname
);
1312 * Since we are doing a shallow lookup, oid is not the
1313 * correct value to pass to delete_ref as old_oid. But that
1314 * doesn't matter, because an old_oid check wouldn't add to
1315 * the safety anyway; we want to delete the reference whatever
1316 * its current value.
1318 if (!refs_read_ref_full(&refs
->base
, newrefname
,
1319 RESOLVE_REF_READING
| RESOLVE_REF_NO_RECURSE
,
1321 refs_delete_ref(&refs
->base
, NULL
, newrefname
,
1322 NULL
, REF_NODEREF
)) {
1323 if (errno
== EISDIR
) {
1324 struct strbuf path
= STRBUF_INIT
;
1327 files_ref_path(refs
, &path
, newrefname
);
1328 result
= remove_empty_directories(&path
);
1329 strbuf_release(&path
);
1332 error("Directory not empty: %s", newrefname
);
1336 error("unable to delete existing %s", newrefname
);
1341 if (log
&& rename_tmp_log(refs
, newrefname
))
1346 lock
= lock_ref_sha1_basic(refs
, newrefname
, NULL
, NULL
, NULL
,
1347 REF_NODEREF
, NULL
, &err
);
1349 error("unable to rename '%s' to '%s': %s", oldrefname
, newrefname
, err
.buf
);
1350 strbuf_release(&err
);
1353 oidcpy(&lock
->old_oid
, &orig_oid
);
1355 if (write_ref_to_lockfile(lock
, &orig_oid
, &err
) ||
1356 commit_ref_update(refs
, lock
, &orig_oid
, logmsg
, &err
)) {
1357 error("unable to write current sha1 into %s: %s", newrefname
, err
.buf
);
1358 strbuf_release(&err
);
1366 lock
= lock_ref_sha1_basic(refs
, oldrefname
, NULL
, NULL
, NULL
,
1367 REF_NODEREF
, NULL
, &err
);
1369 error("unable to lock %s for rollback: %s", oldrefname
, err
.buf
);
1370 strbuf_release(&err
);
1374 flag
= log_all_ref_updates
;
1375 log_all_ref_updates
= LOG_REFS_NONE
;
1376 if (write_ref_to_lockfile(lock
, &orig_oid
, &err
) ||
1377 commit_ref_update(refs
, lock
, &orig_oid
, NULL
, &err
)) {
1378 error("unable to write current sha1 into %s: %s", oldrefname
, err
.buf
);
1379 strbuf_release(&err
);
1381 log_all_ref_updates
= flag
;
1384 if (logmoved
&& rename(sb_newref
.buf
, sb_oldref
.buf
))
1385 error("unable to restore logfile %s from %s: %s",
1386 oldrefname
, newrefname
, strerror(errno
));
1387 if (!logmoved
&& log
&&
1388 rename(tmp_renamed_log
.buf
, sb_oldref
.buf
))
1389 error("unable to restore logfile %s from logs/"TMP_RENAMED_LOG
": %s",
1390 oldrefname
, strerror(errno
));
1393 strbuf_release(&sb_newref
);
1394 strbuf_release(&sb_oldref
);
1395 strbuf_release(&tmp_renamed_log
);
1400 static int close_ref(struct ref_lock
*lock
)
1402 if (close_lock_file(lock
->lk
))
1407 static int commit_ref(struct ref_lock
*lock
)
1409 char *path
= get_locked_file_path(lock
->lk
);
1412 if (!lstat(path
, &st
) && S_ISDIR(st
.st_mode
)) {
1414 * There is a directory at the path we want to rename
1415 * the lockfile to. Hopefully it is empty; try to
1418 size_t len
= strlen(path
);
1419 struct strbuf sb_path
= STRBUF_INIT
;
1421 strbuf_attach(&sb_path
, path
, len
, len
);
1424 * If this fails, commit_lock_file() will also fail
1425 * and will report the problem.
1427 remove_empty_directories(&sb_path
);
1428 strbuf_release(&sb_path
);
1433 if (commit_lock_file(lock
->lk
))
1438 static int open_or_create_logfile(const char *path
, void *cb
)
1442 *fd
= open(path
, O_APPEND
| O_WRONLY
| O_CREAT
, 0666);
1443 return (*fd
< 0) ? -1 : 0;
1447 * Create a reflog for a ref. If force_create = 0, only create the
1448 * reflog for certain refs (those for which should_autocreate_reflog
1449 * returns non-zero). Otherwise, create it regardless of the reference
1450 * name. If the logfile already existed or was created, return 0 and
1451 * set *logfd to the file descriptor opened for appending to the file.
1452 * If no logfile exists and we decided not to create one, return 0 and
1453 * set *logfd to -1. On failure, fill in *err, set *logfd to -1, and
1456 static int log_ref_setup(struct files_ref_store
*refs
,
1457 const char *refname
, int force_create
,
1458 int *logfd
, struct strbuf
*err
)
1460 struct strbuf logfile_sb
= STRBUF_INIT
;
1463 files_reflog_path(refs
, &logfile_sb
, refname
);
1464 logfile
= strbuf_detach(&logfile_sb
, NULL
);
1466 if (force_create
|| should_autocreate_reflog(refname
)) {
1467 if (raceproof_create_file(logfile
, open_or_create_logfile
, logfd
)) {
1468 if (errno
== ENOENT
)
1469 strbuf_addf(err
, "unable to create directory for '%s': "
1470 "%s", logfile
, strerror(errno
));
1471 else if (errno
== EISDIR
)
1472 strbuf_addf(err
, "there are still logs under '%s'",
1475 strbuf_addf(err
, "unable to append to '%s': %s",
1476 logfile
, strerror(errno
));
1481 *logfd
= open(logfile
, O_APPEND
| O_WRONLY
, 0666);
1483 if (errno
== ENOENT
|| errno
== EISDIR
) {
1485 * The logfile doesn't already exist,
1486 * but that is not an error; it only
1487 * means that we won't write log
1492 strbuf_addf(err
, "unable to append to '%s': %s",
1493 logfile
, strerror(errno
));
1500 adjust_shared_perm(logfile
);
1510 static int files_create_reflog(struct ref_store
*ref_store
,
1511 const char *refname
, int force_create
,
1514 struct files_ref_store
*refs
=
1515 files_downcast(ref_store
, REF_STORE_WRITE
, "create_reflog");
1518 if (log_ref_setup(refs
, refname
, force_create
, &fd
, err
))
1527 static int log_ref_write_fd(int fd
, const struct object_id
*old_oid
,
1528 const struct object_id
*new_oid
,
1529 const char *committer
, const char *msg
)
1531 int msglen
, written
;
1532 unsigned maxlen
, len
;
1535 msglen
= msg
? strlen(msg
) : 0;
1536 maxlen
= strlen(committer
) + msglen
+ 100;
1537 logrec
= xmalloc(maxlen
);
1538 len
= xsnprintf(logrec
, maxlen
, "%s %s %s\n",
1539 oid_to_hex(old_oid
),
1540 oid_to_hex(new_oid
),
1543 len
+= copy_reflog_msg(logrec
+ len
- 1, msg
) - 1;
1545 written
= len
<= maxlen
? write_in_full(fd
, logrec
, len
) : -1;
1553 static int files_log_ref_write(struct files_ref_store
*refs
,
1554 const char *refname
, const struct object_id
*old_oid
,
1555 const struct object_id
*new_oid
, const char *msg
,
1556 int flags
, struct strbuf
*err
)
1560 if (log_all_ref_updates
== LOG_REFS_UNSET
)
1561 log_all_ref_updates
= is_bare_repository() ? LOG_REFS_NONE
: LOG_REFS_NORMAL
;
1563 result
= log_ref_setup(refs
, refname
,
1564 flags
& REF_FORCE_CREATE_REFLOG
,
1572 result
= log_ref_write_fd(logfd
, old_oid
, new_oid
,
1573 git_committer_info(0), msg
);
1575 struct strbuf sb
= STRBUF_INIT
;
1576 int save_errno
= errno
;
1578 files_reflog_path(refs
, &sb
, refname
);
1579 strbuf_addf(err
, "unable to append to '%s': %s",
1580 sb
.buf
, strerror(save_errno
));
1581 strbuf_release(&sb
);
1586 struct strbuf sb
= STRBUF_INIT
;
1587 int save_errno
= errno
;
1589 files_reflog_path(refs
, &sb
, refname
);
1590 strbuf_addf(err
, "unable to append to '%s': %s",
1591 sb
.buf
, strerror(save_errno
));
1592 strbuf_release(&sb
);
1599 * Write sha1 into the open lockfile, then close the lockfile. On
1600 * errors, rollback the lockfile, fill in *err and
1603 static int write_ref_to_lockfile(struct ref_lock
*lock
,
1604 const struct object_id
*oid
, struct strbuf
*err
)
1606 static char term
= '\n';
1610 o
= parse_object(oid
);
1613 "trying to write ref '%s' with nonexistent object %s",
1614 lock
->ref_name
, oid_to_hex(oid
));
1618 if (o
->type
!= OBJ_COMMIT
&& is_branch(lock
->ref_name
)) {
1620 "trying to write non-commit object %s to branch '%s'",
1621 oid_to_hex(oid
), lock
->ref_name
);
1625 fd
= get_lock_file_fd(lock
->lk
);
1626 if (write_in_full(fd
, oid_to_hex(oid
), GIT_SHA1_HEXSZ
) != GIT_SHA1_HEXSZ
||
1627 write_in_full(fd
, &term
, 1) != 1 ||
1628 close_ref(lock
) < 0) {
1630 "couldn't write '%s'", get_lock_file_path(lock
->lk
));
1638 * Commit a change to a loose reference that has already been written
1639 * to the loose reference lockfile. Also update the reflogs if
1640 * necessary, using the specified lockmsg (which can be NULL).
1642 static int commit_ref_update(struct files_ref_store
*refs
,
1643 struct ref_lock
*lock
,
1644 const struct object_id
*oid
, const char *logmsg
,
1647 files_assert_main_repository(refs
, "commit_ref_update");
1649 clear_loose_ref_cache(refs
);
1650 if (files_log_ref_write(refs
, lock
->ref_name
,
1651 &lock
->old_oid
, oid
,
1653 char *old_msg
= strbuf_detach(err
, NULL
);
1654 strbuf_addf(err
, "cannot update the ref '%s': %s",
1655 lock
->ref_name
, old_msg
);
1661 if (strcmp(lock
->ref_name
, "HEAD") != 0) {
1663 * Special hack: If a branch is updated directly and HEAD
1664 * points to it (may happen on the remote side of a push
1665 * for example) then logically the HEAD reflog should be
1667 * A generic solution implies reverse symref information,
1668 * but finding all symrefs pointing to the given branch
1669 * would be rather costly for this rare event (the direct
1670 * update of a branch) to be worth it. So let's cheat and
1671 * check with HEAD only which should cover 99% of all usage
1672 * scenarios (even 100% of the default ones).
1674 struct object_id head_oid
;
1676 const char *head_ref
;
1678 head_ref
= refs_resolve_ref_unsafe(&refs
->base
, "HEAD",
1679 RESOLVE_REF_READING
,
1680 head_oid
.hash
, &head_flag
);
1681 if (head_ref
&& (head_flag
& REF_ISSYMREF
) &&
1682 !strcmp(head_ref
, lock
->ref_name
)) {
1683 struct strbuf log_err
= STRBUF_INIT
;
1684 if (files_log_ref_write(refs
, "HEAD",
1685 &lock
->old_oid
, oid
,
1686 logmsg
, 0, &log_err
)) {
1687 error("%s", log_err
.buf
);
1688 strbuf_release(&log_err
);
1693 if (commit_ref(lock
)) {
1694 strbuf_addf(err
, "couldn't set '%s'", lock
->ref_name
);
1703 static int create_ref_symlink(struct ref_lock
*lock
, const char *target
)
1706 #ifndef NO_SYMLINK_HEAD
1707 char *ref_path
= get_locked_file_path(lock
->lk
);
1709 ret
= symlink(target
, ref_path
);
1713 fprintf(stderr
, "no symlink - falling back to symbolic ref\n");
1718 static void update_symref_reflog(struct files_ref_store
*refs
,
1719 struct ref_lock
*lock
, const char *refname
,
1720 const char *target
, const char *logmsg
)
1722 struct strbuf err
= STRBUF_INIT
;
1723 struct object_id new_oid
;
1725 !refs_read_ref_full(&refs
->base
, target
,
1726 RESOLVE_REF_READING
, new_oid
.hash
, NULL
) &&
1727 files_log_ref_write(refs
, refname
, &lock
->old_oid
,
1728 &new_oid
, logmsg
, 0, &err
)) {
1729 error("%s", err
.buf
);
1730 strbuf_release(&err
);
1734 static int create_symref_locked(struct files_ref_store
*refs
,
1735 struct ref_lock
*lock
, const char *refname
,
1736 const char *target
, const char *logmsg
)
1738 if (prefer_symlink_refs
&& !create_ref_symlink(lock
, target
)) {
1739 update_symref_reflog(refs
, lock
, refname
, target
, logmsg
);
1743 if (!fdopen_lock_file(lock
->lk
, "w"))
1744 return error("unable to fdopen %s: %s",
1745 lock
->lk
->tempfile
.filename
.buf
, strerror(errno
));
1747 update_symref_reflog(refs
, lock
, refname
, target
, logmsg
);
1749 /* no error check; commit_ref will check ferror */
1750 fprintf(lock
->lk
->tempfile
.fp
, "ref: %s\n", target
);
1751 if (commit_ref(lock
) < 0)
1752 return error("unable to write symref for %s: %s", refname
,
1757 static int files_create_symref(struct ref_store
*ref_store
,
1758 const char *refname
, const char *target
,
1761 struct files_ref_store
*refs
=
1762 files_downcast(ref_store
, REF_STORE_WRITE
, "create_symref");
1763 struct strbuf err
= STRBUF_INIT
;
1764 struct ref_lock
*lock
;
1767 lock
= lock_ref_sha1_basic(refs
, refname
, NULL
,
1768 NULL
, NULL
, REF_NODEREF
, NULL
,
1771 error("%s", err
.buf
);
1772 strbuf_release(&err
);
1776 ret
= create_symref_locked(refs
, lock
, refname
, target
, logmsg
);
1781 static int files_reflog_exists(struct ref_store
*ref_store
,
1782 const char *refname
)
1784 struct files_ref_store
*refs
=
1785 files_downcast(ref_store
, REF_STORE_READ
, "reflog_exists");
1786 struct strbuf sb
= STRBUF_INIT
;
1790 files_reflog_path(refs
, &sb
, refname
);
1791 ret
= !lstat(sb
.buf
, &st
) && S_ISREG(st
.st_mode
);
1792 strbuf_release(&sb
);
1796 static int files_delete_reflog(struct ref_store
*ref_store
,
1797 const char *refname
)
1799 struct files_ref_store
*refs
=
1800 files_downcast(ref_store
, REF_STORE_WRITE
, "delete_reflog");
1801 struct strbuf sb
= STRBUF_INIT
;
1804 files_reflog_path(refs
, &sb
, refname
);
1805 ret
= remove_path(sb
.buf
);
1806 strbuf_release(&sb
);
1810 static int show_one_reflog_ent(struct strbuf
*sb
, each_reflog_ent_fn fn
, void *cb_data
)
1812 struct object_id ooid
, noid
;
1813 char *email_end
, *message
;
1814 timestamp_t timestamp
;
1816 const char *p
= sb
->buf
;
1818 /* old SP new SP name <email> SP time TAB msg LF */
1819 if (!sb
->len
|| sb
->buf
[sb
->len
- 1] != '\n' ||
1820 parse_oid_hex(p
, &ooid
, &p
) || *p
++ != ' ' ||
1821 parse_oid_hex(p
, &noid
, &p
) || *p
++ != ' ' ||
1822 !(email_end
= strchr(p
, '>')) ||
1823 email_end
[1] != ' ' ||
1824 !(timestamp
= parse_timestamp(email_end
+ 2, &message
, 10)) ||
1825 !message
|| message
[0] != ' ' ||
1826 (message
[1] != '+' && message
[1] != '-') ||
1827 !isdigit(message
[2]) || !isdigit(message
[3]) ||
1828 !isdigit(message
[4]) || !isdigit(message
[5]))
1829 return 0; /* corrupt? */
1830 email_end
[1] = '\0';
1831 tz
= strtol(message
+ 1, NULL
, 10);
1832 if (message
[6] != '\t')
1836 return fn(&ooid
, &noid
, p
, timestamp
, tz
, message
, cb_data
);
1839 static char *find_beginning_of_line(char *bob
, char *scan
)
1841 while (bob
< scan
&& *(--scan
) != '\n')
1842 ; /* keep scanning backwards */
1844 * Return either beginning of the buffer, or LF at the end of
1845 * the previous line.
1850 static int files_for_each_reflog_ent_reverse(struct ref_store
*ref_store
,
1851 const char *refname
,
1852 each_reflog_ent_fn fn
,
1855 struct files_ref_store
*refs
=
1856 files_downcast(ref_store
, REF_STORE_READ
,
1857 "for_each_reflog_ent_reverse");
1858 struct strbuf sb
= STRBUF_INIT
;
1861 int ret
= 0, at_tail
= 1;
1863 files_reflog_path(refs
, &sb
, refname
);
1864 logfp
= fopen(sb
.buf
, "r");
1865 strbuf_release(&sb
);
1869 /* Jump to the end */
1870 if (fseek(logfp
, 0, SEEK_END
) < 0)
1871 ret
= error("cannot seek back reflog for %s: %s",
1872 refname
, strerror(errno
));
1874 while (!ret
&& 0 < pos
) {
1880 /* Fill next block from the end */
1881 cnt
= (sizeof(buf
) < pos
) ? sizeof(buf
) : pos
;
1882 if (fseek(logfp
, pos
- cnt
, SEEK_SET
)) {
1883 ret
= error("cannot seek back reflog for %s: %s",
1884 refname
, strerror(errno
));
1887 nread
= fread(buf
, cnt
, 1, logfp
);
1889 ret
= error("cannot read %d bytes from reflog for %s: %s",
1890 cnt
, refname
, strerror(errno
));
1895 scanp
= endp
= buf
+ cnt
;
1896 if (at_tail
&& scanp
[-1] == '\n')
1897 /* Looking at the final LF at the end of the file */
1901 while (buf
< scanp
) {
1903 * terminating LF of the previous line, or the beginning
1908 bp
= find_beginning_of_line(buf
, scanp
);
1912 * The newline is the end of the previous line,
1913 * so we know we have complete line starting
1914 * at (bp + 1). Prefix it onto any prior data
1915 * we collected for the line and process it.
1917 strbuf_splice(&sb
, 0, 0, bp
+ 1, endp
- (bp
+ 1));
1920 ret
= show_one_reflog_ent(&sb
, fn
, cb_data
);
1926 * We are at the start of the buffer, and the
1927 * start of the file; there is no previous
1928 * line, and we have everything for this one.
1929 * Process it, and we can end the loop.
1931 strbuf_splice(&sb
, 0, 0, buf
, endp
- buf
);
1932 ret
= show_one_reflog_ent(&sb
, fn
, cb_data
);
1939 * We are at the start of the buffer, and there
1940 * is more file to read backwards. Which means
1941 * we are in the middle of a line. Note that we
1942 * may get here even if *bp was a newline; that
1943 * just means we are at the exact end of the
1944 * previous line, rather than some spot in the
1947 * Save away what we have to be combined with
1948 * the data from the next read.
1950 strbuf_splice(&sb
, 0, 0, buf
, endp
- buf
);
1957 die("BUG: reverse reflog parser had leftover data");
1960 strbuf_release(&sb
);
1964 static int files_for_each_reflog_ent(struct ref_store
*ref_store
,
1965 const char *refname
,
1966 each_reflog_ent_fn fn
, void *cb_data
)
1968 struct files_ref_store
*refs
=
1969 files_downcast(ref_store
, REF_STORE_READ
,
1970 "for_each_reflog_ent");
1972 struct strbuf sb
= STRBUF_INIT
;
1975 files_reflog_path(refs
, &sb
, refname
);
1976 logfp
= fopen(sb
.buf
, "r");
1977 strbuf_release(&sb
);
1981 while (!ret
&& !strbuf_getwholeline(&sb
, logfp
, '\n'))
1982 ret
= show_one_reflog_ent(&sb
, fn
, cb_data
);
1984 strbuf_release(&sb
);
1988 struct files_reflog_iterator
{
1989 struct ref_iterator base
;
1991 struct ref_store
*ref_store
;
1992 struct dir_iterator
*dir_iterator
;
1993 struct object_id oid
;
1996 static int files_reflog_iterator_advance(struct ref_iterator
*ref_iterator
)
1998 struct files_reflog_iterator
*iter
=
1999 (struct files_reflog_iterator
*)ref_iterator
;
2000 struct dir_iterator
*diter
= iter
->dir_iterator
;
2003 while ((ok
= dir_iterator_advance(diter
)) == ITER_OK
) {
2006 if (!S_ISREG(diter
->st
.st_mode
))
2008 if (diter
->basename
[0] == '.')
2010 if (ends_with(diter
->basename
, ".lock"))
2013 if (refs_read_ref_full(iter
->ref_store
,
2014 diter
->relative_path
, 0,
2015 iter
->oid
.hash
, &flags
)) {
2016 error("bad ref for %s", diter
->path
.buf
);
2020 iter
->base
.refname
= diter
->relative_path
;
2021 iter
->base
.oid
= &iter
->oid
;
2022 iter
->base
.flags
= flags
;
2026 iter
->dir_iterator
= NULL
;
2027 if (ref_iterator_abort(ref_iterator
) == ITER_ERROR
)
2032 static int files_reflog_iterator_peel(struct ref_iterator
*ref_iterator
,
2033 struct object_id
*peeled
)
2035 die("BUG: ref_iterator_peel() called for reflog_iterator");
2038 static int files_reflog_iterator_abort(struct ref_iterator
*ref_iterator
)
2040 struct files_reflog_iterator
*iter
=
2041 (struct files_reflog_iterator
*)ref_iterator
;
2044 if (iter
->dir_iterator
)
2045 ok
= dir_iterator_abort(iter
->dir_iterator
);
2047 base_ref_iterator_free(ref_iterator
);
2051 static struct ref_iterator_vtable files_reflog_iterator_vtable
= {
2052 files_reflog_iterator_advance
,
2053 files_reflog_iterator_peel
,
2054 files_reflog_iterator_abort
2057 static struct ref_iterator
*files_reflog_iterator_begin(struct ref_store
*ref_store
)
2059 struct files_ref_store
*refs
=
2060 files_downcast(ref_store
, REF_STORE_READ
,
2061 "reflog_iterator_begin");
2062 struct files_reflog_iterator
*iter
= xcalloc(1, sizeof(*iter
));
2063 struct ref_iterator
*ref_iterator
= &iter
->base
;
2064 struct strbuf sb
= STRBUF_INIT
;
2066 base_ref_iterator_init(ref_iterator
, &files_reflog_iterator_vtable
);
2067 files_reflog_path(refs
, &sb
, NULL
);
2068 iter
->dir_iterator
= dir_iterator_begin(sb
.buf
);
2069 iter
->ref_store
= ref_store
;
2070 strbuf_release(&sb
);
2071 return ref_iterator
;
2075 * If update is a direct update of head_ref (the reference pointed to
2076 * by HEAD), then add an extra REF_LOG_ONLY update for HEAD.
2078 static int split_head_update(struct ref_update
*update
,
2079 struct ref_transaction
*transaction
,
2080 const char *head_ref
,
2081 struct string_list
*affected_refnames
,
2084 struct string_list_item
*item
;
2085 struct ref_update
*new_update
;
2087 if ((update
->flags
& REF_LOG_ONLY
) ||
2088 (update
->flags
& REF_ISPRUNING
) ||
2089 (update
->flags
& REF_UPDATE_VIA_HEAD
))
2092 if (strcmp(update
->refname
, head_ref
))
2096 * First make sure that HEAD is not already in the
2097 * transaction. This insertion is O(N) in the transaction
2098 * size, but it happens at most once per transaction.
2100 item
= string_list_insert(affected_refnames
, "HEAD");
2102 /* An entry already existed */
2104 "multiple updates for 'HEAD' (including one "
2105 "via its referent '%s') are not allowed",
2107 return TRANSACTION_NAME_CONFLICT
;
2110 new_update
= ref_transaction_add_update(
2111 transaction
, "HEAD",
2112 update
->flags
| REF_LOG_ONLY
| REF_NODEREF
,
2113 update
->new_oid
.hash
, update
->old_oid
.hash
,
2116 item
->util
= new_update
;
2122 * update is for a symref that points at referent and doesn't have
2123 * REF_NODEREF set. Split it into two updates:
2124 * - The original update, but with REF_LOG_ONLY and REF_NODEREF set
2125 * - A new, separate update for the referent reference
2126 * Note that the new update will itself be subject to splitting when
2127 * the iteration gets to it.
2129 static int split_symref_update(struct files_ref_store
*refs
,
2130 struct ref_update
*update
,
2131 const char *referent
,
2132 struct ref_transaction
*transaction
,
2133 struct string_list
*affected_refnames
,
2136 struct string_list_item
*item
;
2137 struct ref_update
*new_update
;
2138 unsigned int new_flags
;
2141 * First make sure that referent is not already in the
2142 * transaction. This insertion is O(N) in the transaction
2143 * size, but it happens at most once per symref in a
2146 item
= string_list_insert(affected_refnames
, referent
);
2148 /* An entry already existed */
2150 "multiple updates for '%s' (including one "
2151 "via symref '%s') are not allowed",
2152 referent
, update
->refname
);
2153 return TRANSACTION_NAME_CONFLICT
;
2156 new_flags
= update
->flags
;
2157 if (!strcmp(update
->refname
, "HEAD")) {
2159 * Record that the new update came via HEAD, so that
2160 * when we process it, split_head_update() doesn't try
2161 * to add another reflog update for HEAD. Note that
2162 * this bit will be propagated if the new_update
2163 * itself needs to be split.
2165 new_flags
|= REF_UPDATE_VIA_HEAD
;
2168 new_update
= ref_transaction_add_update(
2169 transaction
, referent
, new_flags
,
2170 update
->new_oid
.hash
, update
->old_oid
.hash
,
2173 new_update
->parent_update
= update
;
2176 * Change the symbolic ref update to log only. Also, it
2177 * doesn't need to check its old SHA-1 value, as that will be
2178 * done when new_update is processed.
2180 update
->flags
|= REF_LOG_ONLY
| REF_NODEREF
;
2181 update
->flags
&= ~REF_HAVE_OLD
;
2183 item
->util
= new_update
;
2189 * Return the refname under which update was originally requested.
2191 static const char *original_update_refname(struct ref_update
*update
)
2193 while (update
->parent_update
)
2194 update
= update
->parent_update
;
2196 return update
->refname
;
2200 * Check whether the REF_HAVE_OLD and old_oid values stored in update
2201 * are consistent with oid, which is the reference's current value. If
2202 * everything is OK, return 0; otherwise, write an error message to
2203 * err and return -1.
2205 static int check_old_oid(struct ref_update
*update
, struct object_id
*oid
,
2208 if (!(update
->flags
& REF_HAVE_OLD
) ||
2209 !oidcmp(oid
, &update
->old_oid
))
2212 if (is_null_oid(&update
->old_oid
))
2213 strbuf_addf(err
, "cannot lock ref '%s': "
2214 "reference already exists",
2215 original_update_refname(update
));
2216 else if (is_null_oid(oid
))
2217 strbuf_addf(err
, "cannot lock ref '%s': "
2218 "reference is missing but expected %s",
2219 original_update_refname(update
),
2220 oid_to_hex(&update
->old_oid
));
2222 strbuf_addf(err
, "cannot lock ref '%s': "
2223 "is at %s but expected %s",
2224 original_update_refname(update
),
2226 oid_to_hex(&update
->old_oid
));
2232 * Prepare for carrying out update:
2233 * - Lock the reference referred to by update.
2234 * - Read the reference under lock.
2235 * - Check that its old SHA-1 value (if specified) is correct, and in
2236 * any case record it in update->lock->old_oid for later use when
2237 * writing the reflog.
2238 * - If it is a symref update without REF_NODEREF, split it up into a
2239 * REF_LOG_ONLY update of the symref and add a separate update for
2240 * the referent to transaction.
2241 * - If it is an update of head_ref, add a corresponding REF_LOG_ONLY
2244 static int lock_ref_for_update(struct files_ref_store
*refs
,
2245 struct ref_update
*update
,
2246 struct ref_transaction
*transaction
,
2247 const char *head_ref
,
2248 struct string_list
*affected_refnames
,
2251 struct strbuf referent
= STRBUF_INIT
;
2252 int mustexist
= (update
->flags
& REF_HAVE_OLD
) &&
2253 !is_null_oid(&update
->old_oid
);
2255 struct ref_lock
*lock
;
2257 files_assert_main_repository(refs
, "lock_ref_for_update");
2259 if ((update
->flags
& REF_HAVE_NEW
) && is_null_oid(&update
->new_oid
))
2260 update
->flags
|= REF_DELETING
;
2263 ret
= split_head_update(update
, transaction
, head_ref
,
2264 affected_refnames
, err
);
2269 ret
= lock_raw_ref(refs
, update
->refname
, mustexist
,
2270 affected_refnames
, NULL
,
2272 &update
->type
, err
);
2276 reason
= strbuf_detach(err
, NULL
);
2277 strbuf_addf(err
, "cannot lock ref '%s': %s",
2278 original_update_refname(update
), reason
);
2283 update
->backend_data
= lock
;
2285 if (update
->type
& REF_ISSYMREF
) {
2286 if (update
->flags
& REF_NODEREF
) {
2288 * We won't be reading the referent as part of
2289 * the transaction, so we have to read it here
2290 * to record and possibly check old_sha1:
2292 if (refs_read_ref_full(&refs
->base
,
2294 lock
->old_oid
.hash
, NULL
)) {
2295 if (update
->flags
& REF_HAVE_OLD
) {
2296 strbuf_addf(err
, "cannot lock ref '%s': "
2297 "error reading reference",
2298 original_update_refname(update
));
2301 } else if (check_old_oid(update
, &lock
->old_oid
, err
)) {
2302 return TRANSACTION_GENERIC_ERROR
;
2306 * Create a new update for the reference this
2307 * symref is pointing at. Also, we will record
2308 * and verify old_sha1 for this update as part
2309 * of processing the split-off update, so we
2310 * don't have to do it here.
2312 ret
= split_symref_update(refs
, update
,
2313 referent
.buf
, transaction
,
2314 affected_refnames
, err
);
2319 struct ref_update
*parent_update
;
2321 if (check_old_oid(update
, &lock
->old_oid
, err
))
2322 return TRANSACTION_GENERIC_ERROR
;
2325 * If this update is happening indirectly because of a
2326 * symref update, record the old SHA-1 in the parent
2329 for (parent_update
= update
->parent_update
;
2331 parent_update
= parent_update
->parent_update
) {
2332 struct ref_lock
*parent_lock
= parent_update
->backend_data
;
2333 oidcpy(&parent_lock
->old_oid
, &lock
->old_oid
);
2337 if ((update
->flags
& REF_HAVE_NEW
) &&
2338 !(update
->flags
& REF_DELETING
) &&
2339 !(update
->flags
& REF_LOG_ONLY
)) {
2340 if (!(update
->type
& REF_ISSYMREF
) &&
2341 !oidcmp(&lock
->old_oid
, &update
->new_oid
)) {
2343 * The reference already has the desired
2344 * value, so we don't need to write it.
2346 } else if (write_ref_to_lockfile(lock
, &update
->new_oid
,
2348 char *write_err
= strbuf_detach(err
, NULL
);
2351 * The lock was freed upon failure of
2352 * write_ref_to_lockfile():
2354 update
->backend_data
= NULL
;
2356 "cannot update ref '%s': %s",
2357 update
->refname
, write_err
);
2359 return TRANSACTION_GENERIC_ERROR
;
2361 update
->flags
|= REF_NEEDS_COMMIT
;
2364 if (!(update
->flags
& REF_NEEDS_COMMIT
)) {
2366 * We didn't call write_ref_to_lockfile(), so
2367 * the lockfile is still open. Close it to
2368 * free up the file descriptor:
2370 if (close_ref(lock
)) {
2371 strbuf_addf(err
, "couldn't close '%s.lock'",
2373 return TRANSACTION_GENERIC_ERROR
;
2380 * Unlock any references in `transaction` that are still locked, and
2381 * mark the transaction closed.
2383 static void files_transaction_cleanup(struct ref_transaction
*transaction
)
2387 for (i
= 0; i
< transaction
->nr
; i
++) {
2388 struct ref_update
*update
= transaction
->updates
[i
];
2389 struct ref_lock
*lock
= update
->backend_data
;
2393 update
->backend_data
= NULL
;
2397 transaction
->state
= REF_TRANSACTION_CLOSED
;
2400 static int files_transaction_prepare(struct ref_store
*ref_store
,
2401 struct ref_transaction
*transaction
,
2404 struct files_ref_store
*refs
=
2405 files_downcast(ref_store
, REF_STORE_WRITE
,
2406 "ref_transaction_prepare");
2409 struct string_list affected_refnames
= STRING_LIST_INIT_NODUP
;
2410 char *head_ref
= NULL
;
2412 struct object_id head_oid
;
2416 if (!transaction
->nr
)
2420 * Fail if a refname appears more than once in the
2421 * transaction. (If we end up splitting up any updates using
2422 * split_symref_update() or split_head_update(), those
2423 * functions will check that the new updates don't have the
2424 * same refname as any existing ones.)
2426 for (i
= 0; i
< transaction
->nr
; i
++) {
2427 struct ref_update
*update
= transaction
->updates
[i
];
2428 struct string_list_item
*item
=
2429 string_list_append(&affected_refnames
, update
->refname
);
2432 * We store a pointer to update in item->util, but at
2433 * the moment we never use the value of this field
2434 * except to check whether it is non-NULL.
2436 item
->util
= update
;
2438 string_list_sort(&affected_refnames
);
2439 if (ref_update_reject_duplicates(&affected_refnames
, err
)) {
2440 ret
= TRANSACTION_GENERIC_ERROR
;
2445 * Special hack: If a branch is updated directly and HEAD
2446 * points to it (may happen on the remote side of a push
2447 * for example) then logically the HEAD reflog should be
2450 * A generic solution would require reverse symref lookups,
2451 * but finding all symrefs pointing to a given branch would be
2452 * rather costly for this rare event (the direct update of a
2453 * branch) to be worth it. So let's cheat and check with HEAD
2454 * only, which should cover 99% of all usage scenarios (even
2455 * 100% of the default ones).
2457 * So if HEAD is a symbolic reference, then record the name of
2458 * the reference that it points to. If we see an update of
2459 * head_ref within the transaction, then split_head_update()
2460 * arranges for the reflog of HEAD to be updated, too.
2462 head_ref
= refs_resolve_refdup(ref_store
, "HEAD",
2463 RESOLVE_REF_NO_RECURSE
,
2464 head_oid
.hash
, &head_type
);
2466 if (head_ref
&& !(head_type
& REF_ISSYMREF
)) {
2472 * Acquire all locks, verify old values if provided, check
2473 * that new values are valid, and write new values to the
2474 * lockfiles, ready to be activated. Only keep one lockfile
2475 * open at a time to avoid running out of file descriptors.
2476 * Note that lock_ref_for_update() might append more updates
2477 * to the transaction.
2479 for (i
= 0; i
< transaction
->nr
; i
++) {
2480 struct ref_update
*update
= transaction
->updates
[i
];
2482 ret
= lock_ref_for_update(refs
, update
, transaction
,
2483 head_ref
, &affected_refnames
, err
);
2490 string_list_clear(&affected_refnames
, 0);
2493 files_transaction_cleanup(transaction
);
2495 transaction
->state
= REF_TRANSACTION_PREPARED
;
2500 static int files_transaction_finish(struct ref_store
*ref_store
,
2501 struct ref_transaction
*transaction
,
2504 struct files_ref_store
*refs
=
2505 files_downcast(ref_store
, 0, "ref_transaction_finish");
2508 struct string_list refs_to_delete
= STRING_LIST_INIT_NODUP
;
2509 struct string_list_item
*ref_to_delete
;
2510 struct strbuf sb
= STRBUF_INIT
;
2514 if (!transaction
->nr
) {
2515 transaction
->state
= REF_TRANSACTION_CLOSED
;
2519 /* Perform updates first so live commits remain referenced */
2520 for (i
= 0; i
< transaction
->nr
; i
++) {
2521 struct ref_update
*update
= transaction
->updates
[i
];
2522 struct ref_lock
*lock
= update
->backend_data
;
2524 if (update
->flags
& REF_NEEDS_COMMIT
||
2525 update
->flags
& REF_LOG_ONLY
) {
2526 if (files_log_ref_write(refs
,
2530 update
->msg
, update
->flags
,
2532 char *old_msg
= strbuf_detach(err
, NULL
);
2534 strbuf_addf(err
, "cannot update the ref '%s': %s",
2535 lock
->ref_name
, old_msg
);
2538 update
->backend_data
= NULL
;
2539 ret
= TRANSACTION_GENERIC_ERROR
;
2543 if (update
->flags
& REF_NEEDS_COMMIT
) {
2544 clear_loose_ref_cache(refs
);
2545 if (commit_ref(lock
)) {
2546 strbuf_addf(err
, "couldn't set '%s'", lock
->ref_name
);
2548 update
->backend_data
= NULL
;
2549 ret
= TRANSACTION_GENERIC_ERROR
;
2554 /* Perform deletes now that updates are safely completed */
2555 for (i
= 0; i
< transaction
->nr
; i
++) {
2556 struct ref_update
*update
= transaction
->updates
[i
];
2557 struct ref_lock
*lock
= update
->backend_data
;
2559 if (update
->flags
& REF_DELETING
&&
2560 !(update
->flags
& REF_LOG_ONLY
)) {
2561 if (!(update
->type
& REF_ISPACKED
) ||
2562 update
->type
& REF_ISSYMREF
) {
2563 /* It is a loose reference. */
2565 files_ref_path(refs
, &sb
, lock
->ref_name
);
2566 if (unlink_or_msg(sb
.buf
, err
)) {
2567 ret
= TRANSACTION_GENERIC_ERROR
;
2570 update
->flags
|= REF_DELETED_LOOSE
;
2573 if (!(update
->flags
& REF_ISPRUNING
))
2574 string_list_append(&refs_to_delete
,
2579 if (packed_refs_lock(refs
->packed_ref_store
, 0, err
)) {
2580 ret
= TRANSACTION_GENERIC_ERROR
;
2584 if (repack_without_refs(refs
->packed_ref_store
, &refs_to_delete
, err
)) {
2585 ret
= TRANSACTION_GENERIC_ERROR
;
2586 packed_refs_unlock(refs
->packed_ref_store
);
2590 packed_refs_unlock(refs
->packed_ref_store
);
2592 /* Delete the reflogs of any references that were deleted: */
2593 for_each_string_list_item(ref_to_delete
, &refs_to_delete
) {
2595 files_reflog_path(refs
, &sb
, ref_to_delete
->string
);
2596 if (!unlink_or_warn(sb
.buf
))
2597 try_remove_empty_parents(refs
, ref_to_delete
->string
,
2598 REMOVE_EMPTY_PARENTS_REFLOG
);
2601 clear_loose_ref_cache(refs
);
2604 files_transaction_cleanup(transaction
);
2606 for (i
= 0; i
< transaction
->nr
; i
++) {
2607 struct ref_update
*update
= transaction
->updates
[i
];
2609 if (update
->flags
& REF_DELETED_LOOSE
) {
2611 * The loose reference was deleted. Delete any
2612 * empty parent directories. (Note that this
2613 * can only work because we have already
2614 * removed the lockfile.)
2616 try_remove_empty_parents(refs
, update
->refname
,
2617 REMOVE_EMPTY_PARENTS_REF
);
2621 strbuf_release(&sb
);
2622 string_list_clear(&refs_to_delete
, 0);
2626 static int files_transaction_abort(struct ref_store
*ref_store
,
2627 struct ref_transaction
*transaction
,
2630 files_transaction_cleanup(transaction
);
2634 static int ref_present(const char *refname
,
2635 const struct object_id
*oid
, int flags
, void *cb_data
)
2637 struct string_list
*affected_refnames
= cb_data
;
2639 return string_list_has_string(affected_refnames
, refname
);
2642 static int files_initial_transaction_commit(struct ref_store
*ref_store
,
2643 struct ref_transaction
*transaction
,
2646 struct files_ref_store
*refs
=
2647 files_downcast(ref_store
, REF_STORE_WRITE
,
2648 "initial_ref_transaction_commit");
2651 struct string_list affected_refnames
= STRING_LIST_INIT_NODUP
;
2655 if (transaction
->state
!= REF_TRANSACTION_OPEN
)
2656 die("BUG: commit called for transaction that is not open");
2658 /* Fail if a refname appears more than once in the transaction: */
2659 for (i
= 0; i
< transaction
->nr
; i
++)
2660 string_list_append(&affected_refnames
,
2661 transaction
->updates
[i
]->refname
);
2662 string_list_sort(&affected_refnames
);
2663 if (ref_update_reject_duplicates(&affected_refnames
, err
)) {
2664 ret
= TRANSACTION_GENERIC_ERROR
;
2669 * It's really undefined to call this function in an active
2670 * repository or when there are existing references: we are
2671 * only locking and changing packed-refs, so (1) any
2672 * simultaneous processes might try to change a reference at
2673 * the same time we do, and (2) any existing loose versions of
2674 * the references that we are setting would have precedence
2675 * over our values. But some remote helpers create the remote
2676 * "HEAD" and "master" branches before calling this function,
2677 * so here we really only check that none of the references
2678 * that we are creating already exists.
2680 if (refs_for_each_rawref(&refs
->base
, ref_present
,
2681 &affected_refnames
))
2682 die("BUG: initial ref transaction called with existing refs");
2684 for (i
= 0; i
< transaction
->nr
; i
++) {
2685 struct ref_update
*update
= transaction
->updates
[i
];
2687 if ((update
->flags
& REF_HAVE_OLD
) &&
2688 !is_null_oid(&update
->old_oid
))
2689 die("BUG: initial ref transaction with old_sha1 set");
2690 if (refs_verify_refname_available(&refs
->base
, update
->refname
,
2691 &affected_refnames
, NULL
,
2693 ret
= TRANSACTION_NAME_CONFLICT
;
2698 if (packed_refs_lock(refs
->packed_ref_store
, 0, err
)) {
2699 ret
= TRANSACTION_GENERIC_ERROR
;
2703 for (i
= 0; i
< transaction
->nr
; i
++) {
2704 struct ref_update
*update
= transaction
->updates
[i
];
2706 if ((update
->flags
& REF_HAVE_NEW
) &&
2707 !is_null_oid(&update
->new_oid
))
2708 add_packed_ref(refs
->packed_ref_store
, update
->refname
,
2712 if (commit_packed_refs(refs
->packed_ref_store
, err
)) {
2713 ret
= TRANSACTION_GENERIC_ERROR
;
2718 packed_refs_unlock(refs
->packed_ref_store
);
2719 transaction
->state
= REF_TRANSACTION_CLOSED
;
2720 string_list_clear(&affected_refnames
, 0);
2724 struct expire_reflog_cb
{
2726 reflog_expiry_should_prune_fn
*should_prune_fn
;
2729 struct object_id last_kept_oid
;
2732 static int expire_reflog_ent(struct object_id
*ooid
, struct object_id
*noid
,
2733 const char *email
, timestamp_t timestamp
, int tz
,
2734 const char *message
, void *cb_data
)
2736 struct expire_reflog_cb
*cb
= cb_data
;
2737 struct expire_reflog_policy_cb
*policy_cb
= cb
->policy_cb
;
2739 if (cb
->flags
& EXPIRE_REFLOGS_REWRITE
)
2740 ooid
= &cb
->last_kept_oid
;
2742 if ((*cb
->should_prune_fn
)(ooid
, noid
, email
, timestamp
, tz
,
2743 message
, policy_cb
)) {
2745 printf("would prune %s", message
);
2746 else if (cb
->flags
& EXPIRE_REFLOGS_VERBOSE
)
2747 printf("prune %s", message
);
2750 fprintf(cb
->newlog
, "%s %s %s %"PRItime
" %+05d\t%s",
2751 oid_to_hex(ooid
), oid_to_hex(noid
),
2752 email
, timestamp
, tz
, message
);
2753 oidcpy(&cb
->last_kept_oid
, noid
);
2755 if (cb
->flags
& EXPIRE_REFLOGS_VERBOSE
)
2756 printf("keep %s", message
);
2761 static int files_reflog_expire(struct ref_store
*ref_store
,
2762 const char *refname
, const unsigned char *sha1
,
2764 reflog_expiry_prepare_fn prepare_fn
,
2765 reflog_expiry_should_prune_fn should_prune_fn
,
2766 reflog_expiry_cleanup_fn cleanup_fn
,
2767 void *policy_cb_data
)
2769 struct files_ref_store
*refs
=
2770 files_downcast(ref_store
, REF_STORE_WRITE
, "reflog_expire");
2771 static struct lock_file reflog_lock
;
2772 struct expire_reflog_cb cb
;
2773 struct ref_lock
*lock
;
2774 struct strbuf log_file_sb
= STRBUF_INIT
;
2778 struct strbuf err
= STRBUF_INIT
;
2779 struct object_id oid
;
2781 memset(&cb
, 0, sizeof(cb
));
2783 cb
.policy_cb
= policy_cb_data
;
2784 cb
.should_prune_fn
= should_prune_fn
;
2787 * The reflog file is locked by holding the lock on the
2788 * reference itself, plus we might need to update the
2789 * reference if --updateref was specified:
2791 lock
= lock_ref_sha1_basic(refs
, refname
, sha1
,
2792 NULL
, NULL
, REF_NODEREF
,
2795 error("cannot lock ref '%s': %s", refname
, err
.buf
);
2796 strbuf_release(&err
);
2799 if (!refs_reflog_exists(ref_store
, refname
)) {
2804 files_reflog_path(refs
, &log_file_sb
, refname
);
2805 log_file
= strbuf_detach(&log_file_sb
, NULL
);
2806 if (!(flags
& EXPIRE_REFLOGS_DRY_RUN
)) {
2808 * Even though holding $GIT_DIR/logs/$reflog.lock has
2809 * no locking implications, we use the lock_file
2810 * machinery here anyway because it does a lot of the
2811 * work we need, including cleaning up if the program
2812 * exits unexpectedly.
2814 if (hold_lock_file_for_update(&reflog_lock
, log_file
, 0) < 0) {
2815 struct strbuf err
= STRBUF_INIT
;
2816 unable_to_lock_message(log_file
, errno
, &err
);
2817 error("%s", err
.buf
);
2818 strbuf_release(&err
);
2821 cb
.newlog
= fdopen_lock_file(&reflog_lock
, "w");
2823 error("cannot fdopen %s (%s)",
2824 get_lock_file_path(&reflog_lock
), strerror(errno
));
2829 hashcpy(oid
.hash
, sha1
);
2831 (*prepare_fn
)(refname
, &oid
, cb
.policy_cb
);
2832 refs_for_each_reflog_ent(ref_store
, refname
, expire_reflog_ent
, &cb
);
2833 (*cleanup_fn
)(cb
.policy_cb
);
2835 if (!(flags
& EXPIRE_REFLOGS_DRY_RUN
)) {
2837 * It doesn't make sense to adjust a reference pointed
2838 * to by a symbolic ref based on expiring entries in
2839 * the symbolic reference's reflog. Nor can we update
2840 * a reference if there are no remaining reflog
2843 int update
= (flags
& EXPIRE_REFLOGS_UPDATE_REF
) &&
2844 !(type
& REF_ISSYMREF
) &&
2845 !is_null_oid(&cb
.last_kept_oid
);
2847 if (close_lock_file(&reflog_lock
)) {
2848 status
|= error("couldn't write %s: %s", log_file
,
2850 } else if (update
&&
2851 (write_in_full(get_lock_file_fd(lock
->lk
),
2852 oid_to_hex(&cb
.last_kept_oid
), GIT_SHA1_HEXSZ
) != GIT_SHA1_HEXSZ
||
2853 write_str_in_full(get_lock_file_fd(lock
->lk
), "\n") != 1 ||
2854 close_ref(lock
) < 0)) {
2855 status
|= error("couldn't write %s",
2856 get_lock_file_path(lock
->lk
));
2857 rollback_lock_file(&reflog_lock
);
2858 } else if (commit_lock_file(&reflog_lock
)) {
2859 status
|= error("unable to write reflog '%s' (%s)",
2860 log_file
, strerror(errno
));
2861 } else if (update
&& commit_ref(lock
)) {
2862 status
|= error("couldn't set %s", lock
->ref_name
);
2870 rollback_lock_file(&reflog_lock
);
2876 static int files_init_db(struct ref_store
*ref_store
, struct strbuf
*err
)
2878 struct files_ref_store
*refs
=
2879 files_downcast(ref_store
, REF_STORE_WRITE
, "init_db");
2880 struct strbuf sb
= STRBUF_INIT
;
2883 * Create .git/refs/{heads,tags}
2885 files_ref_path(refs
, &sb
, "refs/heads");
2886 safe_create_dir(sb
.buf
, 1);
2889 files_ref_path(refs
, &sb
, "refs/tags");
2890 safe_create_dir(sb
.buf
, 1);
2892 strbuf_release(&sb
);
2896 struct ref_storage_be refs_be_files
= {
2899 files_ref_store_create
,
2901 files_transaction_prepare
,
2902 files_transaction_finish
,
2903 files_transaction_abort
,
2904 files_initial_transaction_commit
,
2908 files_create_symref
,
2912 files_ref_iterator_begin
,
2915 files_reflog_iterator_begin
,
2916 files_for_each_reflog_ent
,
2917 files_for_each_reflog_ent_reverse
,
2918 files_reflog_exists
,
2919 files_create_reflog
,
2920 files_delete_reflog
,