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
,
111 * FIXME: of course this is wrong in multi worktree
112 * setting. To be fixed real soon.
114 strbuf_addf(sb
, "%s/logs", refs
->gitcommondir
);
118 switch (ref_type(refname
)) {
119 case REF_TYPE_PER_WORKTREE
:
120 case REF_TYPE_PSEUDOREF
:
121 strbuf_addf(sb
, "%s/logs/%s", refs
->gitdir
, refname
);
123 case REF_TYPE_NORMAL
:
124 strbuf_addf(sb
, "%s/logs/%s", refs
->gitcommondir
, refname
);
127 die("BUG: unknown ref type %d of ref %s",
128 ref_type(refname
), refname
);
132 static void files_ref_path(struct files_ref_store
*refs
,
136 switch (ref_type(refname
)) {
137 case REF_TYPE_PER_WORKTREE
:
138 case REF_TYPE_PSEUDOREF
:
139 strbuf_addf(sb
, "%s/%s", refs
->gitdir
, refname
);
141 case REF_TYPE_NORMAL
:
142 strbuf_addf(sb
, "%s/%s", refs
->gitcommondir
, refname
);
145 die("BUG: unknown ref type %d of ref %s",
146 ref_type(refname
), refname
);
151 * Read the loose references from the namespace dirname into dir
152 * (without recursing). dirname must end with '/'. dir must be the
153 * directory entry corresponding to dirname.
155 static void loose_fill_ref_dir(struct ref_store
*ref_store
,
156 struct ref_dir
*dir
, const char *dirname
)
158 struct files_ref_store
*refs
=
159 files_downcast(ref_store
, REF_STORE_READ
, "fill_ref_dir");
162 int dirnamelen
= strlen(dirname
);
163 struct strbuf refname
;
164 struct strbuf path
= STRBUF_INIT
;
167 files_ref_path(refs
, &path
, dirname
);
168 path_baselen
= path
.len
;
170 d
= opendir(path
.buf
);
172 strbuf_release(&path
);
176 strbuf_init(&refname
, dirnamelen
+ 257);
177 strbuf_add(&refname
, dirname
, dirnamelen
);
179 while ((de
= readdir(d
)) != NULL
) {
180 struct object_id oid
;
184 if (de
->d_name
[0] == '.')
186 if (ends_with(de
->d_name
, ".lock"))
188 strbuf_addstr(&refname
, de
->d_name
);
189 strbuf_addstr(&path
, de
->d_name
);
190 if (stat(path
.buf
, &st
) < 0) {
191 ; /* silently ignore */
192 } else if (S_ISDIR(st
.st_mode
)) {
193 strbuf_addch(&refname
, '/');
194 add_entry_to_dir(dir
,
195 create_dir_entry(dir
->cache
, refname
.buf
,
198 if (!refs_resolve_ref_unsafe(&refs
->base
,
203 flag
|= REF_ISBROKEN
;
204 } else if (is_null_oid(&oid
)) {
206 * It is so astronomically unlikely
207 * that NULL_SHA1 is the SHA-1 of an
208 * actual object that we consider its
209 * appearance in a loose reference
210 * file to be repo corruption
211 * (probably due to a software bug).
213 flag
|= REF_ISBROKEN
;
216 if (check_refname_format(refname
.buf
,
217 REFNAME_ALLOW_ONELEVEL
)) {
218 if (!refname_is_safe(refname
.buf
))
219 die("loose refname is dangerous: %s", refname
.buf
);
221 flag
|= REF_BAD_NAME
| REF_ISBROKEN
;
223 add_entry_to_dir(dir
,
224 create_ref_entry(refname
.buf
, &oid
, flag
));
226 strbuf_setlen(&refname
, dirnamelen
);
227 strbuf_setlen(&path
, path_baselen
);
229 strbuf_release(&refname
);
230 strbuf_release(&path
);
234 * Manually add refs/bisect, which, being per-worktree, might
235 * not appear in the directory listing for refs/ in the main
238 if (!strcmp(dirname
, "refs/")) {
239 int pos
= search_ref_dir(dir
, "refs/bisect/", 12);
242 struct ref_entry
*child_entry
= create_dir_entry(
243 dir
->cache
, "refs/bisect/", 12, 1);
244 add_entry_to_dir(dir
, child_entry
);
249 static struct ref_cache
*get_loose_ref_cache(struct files_ref_store
*refs
)
253 * Mark the top-level directory complete because we
254 * are about to read the only subdirectory that can
257 refs
->loose
= create_ref_cache(&refs
->base
, loose_fill_ref_dir
);
259 /* We're going to fill the top level ourselves: */
260 refs
->loose
->root
->flag
&= ~REF_INCOMPLETE
;
263 * Add an incomplete entry for "refs/" (to be filled
266 add_entry_to_dir(get_ref_dir(refs
->loose
->root
),
267 create_dir_entry(refs
->loose
, "refs/", 5, 1));
272 static int files_read_raw_ref(struct ref_store
*ref_store
,
273 const char *refname
, unsigned char *sha1
,
274 struct strbuf
*referent
, unsigned int *type
)
276 struct files_ref_store
*refs
=
277 files_downcast(ref_store
, REF_STORE_READ
, "read_raw_ref");
278 struct strbuf sb_contents
= STRBUF_INIT
;
279 struct strbuf sb_path
= STRBUF_INIT
;
286 int remaining_retries
= 3;
289 strbuf_reset(&sb_path
);
291 files_ref_path(refs
, &sb_path
, refname
);
297 * We might have to loop back here to avoid a race
298 * condition: first we lstat() the file, then we try
299 * to read it as a link or as a file. But if somebody
300 * changes the type of the file (file <-> directory
301 * <-> symlink) between the lstat() and reading, then
302 * we don't want to report that as an error but rather
303 * try again starting with the lstat().
305 * We'll keep a count of the retries, though, just to avoid
306 * any confusing situation sending us into an infinite loop.
309 if (remaining_retries
-- <= 0)
312 if (lstat(path
, &st
) < 0) {
315 if (refs_read_raw_ref(refs
->packed_ref_store
, refname
,
316 sha1
, referent
, type
)) {
324 /* Follow "normalized" - ie "refs/.." symlinks by hand */
325 if (S_ISLNK(st
.st_mode
)) {
326 strbuf_reset(&sb_contents
);
327 if (strbuf_readlink(&sb_contents
, path
, 0) < 0) {
328 if (errno
== ENOENT
|| errno
== EINVAL
)
329 /* inconsistent with lstat; retry */
334 if (starts_with(sb_contents
.buf
, "refs/") &&
335 !check_refname_format(sb_contents
.buf
, 0)) {
336 strbuf_swap(&sb_contents
, referent
);
337 *type
|= REF_ISSYMREF
;
342 * It doesn't look like a refname; fall through to just
343 * treating it like a non-symlink, and reading whatever it
348 /* Is it a directory? */
349 if (S_ISDIR(st
.st_mode
)) {
351 * Even though there is a directory where the loose
352 * ref is supposed to be, there could still be a
355 if (refs_read_raw_ref(refs
->packed_ref_store
, refname
,
356 sha1
, referent
, type
)) {
365 * Anything else, just open it and try to use it as
368 fd
= open(path
, O_RDONLY
);
370 if (errno
== ENOENT
&& !S_ISLNK(st
.st_mode
))
371 /* inconsistent with lstat; retry */
376 strbuf_reset(&sb_contents
);
377 if (strbuf_read(&sb_contents
, fd
, 256) < 0) {
378 int save_errno
= errno
;
384 strbuf_rtrim(&sb_contents
);
385 buf
= sb_contents
.buf
;
386 if (starts_with(buf
, "ref:")) {
388 while (isspace(*buf
))
391 strbuf_reset(referent
);
392 strbuf_addstr(referent
, buf
);
393 *type
|= REF_ISSYMREF
;
399 * Please note that FETCH_HEAD has additional
400 * data after the sha.
402 if (get_sha1_hex(buf
, sha1
) ||
403 (buf
[40] != '\0' && !isspace(buf
[40]))) {
404 *type
|= REF_ISBROKEN
;
413 strbuf_release(&sb_path
);
414 strbuf_release(&sb_contents
);
419 static void unlock_ref(struct ref_lock
*lock
)
421 /* Do not free lock->lk -- atexit() still looks at them */
423 rollback_lock_file(lock
->lk
);
424 free(lock
->ref_name
);
429 * Lock refname, without following symrefs, and set *lock_p to point
430 * at a newly-allocated lock object. Fill in lock->old_oid, referent,
431 * and type similarly to read_raw_ref().
433 * The caller must verify that refname is a "safe" reference name (in
434 * the sense of refname_is_safe()) before calling this function.
436 * If the reference doesn't already exist, verify that refname doesn't
437 * have a D/F conflict with any existing references. extras and skip
438 * are passed to refs_verify_refname_available() for this check.
440 * If mustexist is not set and the reference is not found or is
441 * broken, lock the reference anyway but clear sha1.
443 * Return 0 on success. On failure, write an error message to err and
444 * return TRANSACTION_NAME_CONFLICT or TRANSACTION_GENERIC_ERROR.
446 * Implementation note: This function is basically
451 * but it includes a lot more code to
452 * - Deal with possible races with other processes
453 * - Avoid calling refs_verify_refname_available() when it can be
454 * avoided, namely if we were successfully able to read the ref
455 * - Generate informative error messages in the case of failure
457 static int lock_raw_ref(struct files_ref_store
*refs
,
458 const char *refname
, int mustexist
,
459 const struct string_list
*extras
,
460 const struct string_list
*skip
,
461 struct ref_lock
**lock_p
,
462 struct strbuf
*referent
,
466 struct ref_lock
*lock
;
467 struct strbuf ref_file
= STRBUF_INIT
;
468 int attempts_remaining
= 3;
469 int ret
= TRANSACTION_GENERIC_ERROR
;
472 files_assert_main_repository(refs
, "lock_raw_ref");
476 /* First lock the file so it can't change out from under us. */
478 *lock_p
= lock
= xcalloc(1, sizeof(*lock
));
480 lock
->ref_name
= xstrdup(refname
);
481 files_ref_path(refs
, &ref_file
, refname
);
484 switch (safe_create_leading_directories(ref_file
.buf
)) {
489 * Suppose refname is "refs/foo/bar". We just failed
490 * to create the containing directory, "refs/foo",
491 * because there was a non-directory in the way. This
492 * indicates a D/F conflict, probably because of
493 * another reference such as "refs/foo". There is no
494 * reason to expect this error to be transitory.
496 if (refs_verify_refname_available(&refs
->base
, refname
,
497 extras
, skip
, err
)) {
500 * To the user the relevant error is
501 * that the "mustexist" reference is
505 strbuf_addf(err
, "unable to resolve reference '%s'",
509 * The error message set by
510 * refs_verify_refname_available() is
513 ret
= TRANSACTION_NAME_CONFLICT
;
517 * The file that is in the way isn't a loose
518 * reference. Report it as a low-level
521 strbuf_addf(err
, "unable to create lock file %s.lock; "
522 "non-directory in the way",
527 /* Maybe another process was tidying up. Try again. */
528 if (--attempts_remaining
> 0)
532 strbuf_addf(err
, "unable to create directory for %s",
538 lock
->lk
= xcalloc(1, sizeof(struct lock_file
));
540 if (hold_lock_file_for_update_timeout(
541 lock
->lk
, ref_file
.buf
, LOCK_NO_DEREF
,
542 get_files_ref_lock_timeout_ms()) < 0) {
543 if (errno
== ENOENT
&& --attempts_remaining
> 0) {
545 * Maybe somebody just deleted one of the
546 * directories leading to ref_file. Try
551 unable_to_lock_message(ref_file
.buf
, errno
, err
);
557 * Now we hold the lock and can read the reference without
558 * fear that its value will change.
561 if (files_read_raw_ref(&refs
->base
, refname
,
562 lock
->old_oid
.hash
, referent
, type
)) {
563 if (errno
== ENOENT
) {
565 /* Garden variety missing reference. */
566 strbuf_addf(err
, "unable to resolve reference '%s'",
571 * Reference is missing, but that's OK. We
572 * know that there is not a conflict with
573 * another loose reference because
574 * (supposing that we are trying to lock
575 * reference "refs/foo/bar"):
577 * - We were successfully able to create
578 * the lockfile refs/foo/bar.lock, so we
579 * know there cannot be a loose reference
582 * - We got ENOENT and not EISDIR, so we
583 * know that there cannot be a loose
584 * reference named "refs/foo/bar/baz".
587 } else if (errno
== EISDIR
) {
589 * There is a directory in the way. It might have
590 * contained references that have been deleted. If
591 * we don't require that the reference already
592 * exists, try to remove the directory so that it
593 * doesn't cause trouble when we want to rename the
594 * lockfile into place later.
597 /* Garden variety missing reference. */
598 strbuf_addf(err
, "unable to resolve reference '%s'",
601 } else if (remove_dir_recursively(&ref_file
,
602 REMOVE_DIR_EMPTY_ONLY
)) {
603 if (refs_verify_refname_available(
604 &refs
->base
, refname
,
605 extras
, skip
, err
)) {
607 * The error message set by
608 * verify_refname_available() is OK.
610 ret
= TRANSACTION_NAME_CONFLICT
;
614 * We can't delete the directory,
615 * but we also don't know of any
616 * references that it should
619 strbuf_addf(err
, "there is a non-empty directory '%s' "
620 "blocking reference '%s'",
621 ref_file
.buf
, refname
);
625 } else if (errno
== EINVAL
&& (*type
& REF_ISBROKEN
)) {
626 strbuf_addf(err
, "unable to resolve reference '%s': "
627 "reference broken", refname
);
630 strbuf_addf(err
, "unable to resolve reference '%s': %s",
631 refname
, strerror(errno
));
636 * If the ref did not exist and we are creating it,
637 * make sure there is no existing packed ref that
638 * conflicts with refname:
640 if (refs_verify_refname_available(
641 refs
->packed_ref_store
, refname
,
654 strbuf_release(&ref_file
);
658 static int files_peel_ref(struct ref_store
*ref_store
,
659 const char *refname
, unsigned char *sha1
)
661 struct files_ref_store
*refs
=
662 files_downcast(ref_store
, REF_STORE_READ
| REF_STORE_ODB
,
665 unsigned char base
[20];
667 if (current_ref_iter
&& current_ref_iter
->refname
== refname
) {
668 struct object_id peeled
;
670 if (ref_iterator_peel(current_ref_iter
, &peeled
))
672 hashcpy(sha1
, peeled
.hash
);
676 if (refs_read_ref_full(ref_store
, refname
,
677 RESOLVE_REF_READING
, base
, &flag
))
681 * If the reference is packed, read its ref_entry from the
682 * cache in the hope that we already know its peeled value.
683 * We only try this optimization on packed references because
684 * (a) forcing the filling of the loose reference cache could
685 * be expensive and (b) loose references anyway usually do not
686 * have REF_KNOWS_PEELED.
688 if (flag
& REF_ISPACKED
&&
689 !refs_peel_ref(refs
->packed_ref_store
, refname
, sha1
))
692 return peel_object(base
, sha1
);
695 struct files_ref_iterator
{
696 struct ref_iterator base
;
698 struct ref_iterator
*iter0
;
702 static int files_ref_iterator_advance(struct ref_iterator
*ref_iterator
)
704 struct files_ref_iterator
*iter
=
705 (struct files_ref_iterator
*)ref_iterator
;
708 while ((ok
= ref_iterator_advance(iter
->iter0
)) == ITER_OK
) {
709 if (iter
->flags
& DO_FOR_EACH_PER_WORKTREE_ONLY
&&
710 ref_type(iter
->iter0
->refname
) != REF_TYPE_PER_WORKTREE
)
713 if (!(iter
->flags
& DO_FOR_EACH_INCLUDE_BROKEN
) &&
714 !ref_resolves_to_object(iter
->iter0
->refname
,
719 iter
->base
.refname
= iter
->iter0
->refname
;
720 iter
->base
.oid
= iter
->iter0
->oid
;
721 iter
->base
.flags
= iter
->iter0
->flags
;
726 if (ref_iterator_abort(ref_iterator
) != ITER_DONE
)
732 static int files_ref_iterator_peel(struct ref_iterator
*ref_iterator
,
733 struct object_id
*peeled
)
735 struct files_ref_iterator
*iter
=
736 (struct files_ref_iterator
*)ref_iterator
;
738 return ref_iterator_peel(iter
->iter0
, peeled
);
741 static int files_ref_iterator_abort(struct ref_iterator
*ref_iterator
)
743 struct files_ref_iterator
*iter
=
744 (struct files_ref_iterator
*)ref_iterator
;
748 ok
= ref_iterator_abort(iter
->iter0
);
750 base_ref_iterator_free(ref_iterator
);
754 static struct ref_iterator_vtable files_ref_iterator_vtable
= {
755 files_ref_iterator_advance
,
756 files_ref_iterator_peel
,
757 files_ref_iterator_abort
760 static struct ref_iterator
*files_ref_iterator_begin(
761 struct ref_store
*ref_store
,
762 const char *prefix
, unsigned int flags
)
764 struct files_ref_store
*refs
;
765 struct ref_iterator
*loose_iter
, *packed_iter
;
766 struct files_ref_iterator
*iter
;
767 struct ref_iterator
*ref_iterator
;
768 unsigned int required_flags
= REF_STORE_READ
;
770 if (!(flags
& DO_FOR_EACH_INCLUDE_BROKEN
))
771 required_flags
|= REF_STORE_ODB
;
773 refs
= files_downcast(ref_store
, required_flags
, "ref_iterator_begin");
775 iter
= xcalloc(1, sizeof(*iter
));
776 ref_iterator
= &iter
->base
;
777 base_ref_iterator_init(ref_iterator
, &files_ref_iterator_vtable
);
780 * We must make sure that all loose refs are read before
781 * accessing the packed-refs file; this avoids a race
782 * condition if loose refs are migrated to the packed-refs
783 * file by a simultaneous process, but our in-memory view is
784 * from before the migration. We ensure this as follows:
785 * First, we call start the loose refs iteration with its
786 * `prime_ref` argument set to true. This causes the loose
787 * references in the subtree to be pre-read into the cache.
788 * (If they've already been read, that's OK; we only need to
789 * guarantee that they're read before the packed refs, not
790 * *how much* before.) After that, we call
791 * packed_ref_iterator_begin(), which internally checks
792 * whether the packed-ref cache is up to date with what is on
793 * disk, and re-reads it if not.
796 loose_iter
= cache_ref_iterator_begin(get_loose_ref_cache(refs
),
800 * The packed-refs file might contain broken references, for
801 * example an old version of a reference that points at an
802 * object that has since been garbage-collected. This is OK as
803 * long as there is a corresponding loose reference that
804 * overrides it, and we don't want to emit an error message in
805 * this case. So ask the packed_ref_store for all of its
806 * references, and (if needed) do our own check for broken
807 * ones in files_ref_iterator_advance(), after we have merged
808 * the packed and loose references.
810 packed_iter
= refs_ref_iterator_begin(
811 refs
->packed_ref_store
, prefix
, 0,
812 DO_FOR_EACH_INCLUDE_BROKEN
);
814 iter
->iter0
= overlay_ref_iterator_begin(loose_iter
, packed_iter
);
821 * Verify that the reference locked by lock has the value old_sha1.
822 * Fail if the reference doesn't exist and mustexist is set. Return 0
823 * on success. On error, write an error message to err, set errno, and
824 * return a negative value.
826 static int verify_lock(struct ref_store
*ref_store
, struct ref_lock
*lock
,
827 const unsigned char *old_sha1
, int mustexist
,
832 if (refs_read_ref_full(ref_store
, lock
->ref_name
,
833 mustexist
? RESOLVE_REF_READING
: 0,
834 lock
->old_oid
.hash
, NULL
)) {
836 int save_errno
= errno
;
837 strbuf_addf(err
, "can't verify ref '%s'", lock
->ref_name
);
841 oidclr(&lock
->old_oid
);
845 if (old_sha1
&& hashcmp(lock
->old_oid
.hash
, old_sha1
)) {
846 strbuf_addf(err
, "ref '%s' is at %s but expected %s",
848 oid_to_hex(&lock
->old_oid
),
849 sha1_to_hex(old_sha1
));
856 static int remove_empty_directories(struct strbuf
*path
)
859 * we want to create a file but there is a directory there;
860 * if that is an empty directory (or a directory that contains
861 * only empty directories), remove them.
863 return remove_dir_recursively(path
, REMOVE_DIR_EMPTY_ONLY
);
866 static int create_reflock(const char *path
, void *cb
)
868 struct lock_file
*lk
= cb
;
870 return hold_lock_file_for_update_timeout(
871 lk
, path
, LOCK_NO_DEREF
,
872 get_files_ref_lock_timeout_ms()) < 0 ? -1 : 0;
876 * Locks a ref returning the lock on success and NULL on failure.
877 * On failure errno is set to something meaningful.
879 static struct ref_lock
*lock_ref_sha1_basic(struct files_ref_store
*refs
,
881 const unsigned char *old_sha1
,
882 const struct string_list
*extras
,
883 const struct string_list
*skip
,
884 unsigned int flags
, int *type
,
887 struct strbuf ref_file
= STRBUF_INIT
;
888 struct ref_lock
*lock
;
890 int mustexist
= (old_sha1
&& !is_null_sha1(old_sha1
));
891 int resolve_flags
= RESOLVE_REF_NO_RECURSE
;
894 files_assert_main_repository(refs
, "lock_ref_sha1_basic");
897 lock
= xcalloc(1, sizeof(struct ref_lock
));
900 resolve_flags
|= RESOLVE_REF_READING
;
901 if (flags
& REF_DELETING
)
902 resolve_flags
|= RESOLVE_REF_ALLOW_BAD_NAME
;
904 files_ref_path(refs
, &ref_file
, refname
);
905 resolved
= !!refs_resolve_ref_unsafe(&refs
->base
,
906 refname
, resolve_flags
,
907 lock
->old_oid
.hash
, type
);
908 if (!resolved
&& errno
== EISDIR
) {
910 * we are trying to lock foo but we used to
911 * have foo/bar which now does not exist;
912 * it is normal for the empty directory 'foo'
915 if (remove_empty_directories(&ref_file
)) {
917 if (!refs_verify_refname_available(
919 refname
, extras
, skip
, err
))
920 strbuf_addf(err
, "there are still refs under '%s'",
924 resolved
= !!refs_resolve_ref_unsafe(&refs
->base
,
925 refname
, resolve_flags
,
926 lock
->old_oid
.hash
, type
);
930 if (last_errno
!= ENOTDIR
||
931 !refs_verify_refname_available(&refs
->base
, refname
,
933 strbuf_addf(err
, "unable to resolve reference '%s': %s",
934 refname
, strerror(last_errno
));
940 * If the ref did not exist and we are creating it, make sure
941 * there is no existing packed ref whose name begins with our
942 * refname, nor a packed ref whose name is a proper prefix of
945 if (is_null_oid(&lock
->old_oid
) &&
946 refs_verify_refname_available(refs
->packed_ref_store
, refname
,
947 extras
, skip
, err
)) {
948 last_errno
= ENOTDIR
;
952 lock
->lk
= xcalloc(1, sizeof(struct lock_file
));
954 lock
->ref_name
= xstrdup(refname
);
956 if (raceproof_create_file(ref_file
.buf
, create_reflock
, lock
->lk
)) {
958 unable_to_lock_message(ref_file
.buf
, errno
, err
);
962 if (verify_lock(&refs
->base
, lock
, old_sha1
, mustexist
, err
)) {
973 strbuf_release(&ref_file
);
978 struct ref_to_prune
{
979 struct ref_to_prune
*next
;
980 unsigned char sha1
[20];
981 char name
[FLEX_ARRAY
];
985 REMOVE_EMPTY_PARENTS_REF
= 0x01,
986 REMOVE_EMPTY_PARENTS_REFLOG
= 0x02
990 * Remove empty parent directories associated with the specified
991 * reference and/or its reflog, but spare [logs/]refs/ and immediate
992 * subdirs. flags is a combination of REMOVE_EMPTY_PARENTS_REF and/or
993 * REMOVE_EMPTY_PARENTS_REFLOG.
995 static void try_remove_empty_parents(struct files_ref_store
*refs
,
999 struct strbuf buf
= STRBUF_INIT
;
1000 struct strbuf sb
= STRBUF_INIT
;
1004 strbuf_addstr(&buf
, refname
);
1006 for (i
= 0; i
< 2; i
++) { /* refs/{heads,tags,...}/ */
1007 while (*p
&& *p
!= '/')
1009 /* tolerate duplicate slashes; see check_refname_format() */
1013 q
= buf
.buf
+ buf
.len
;
1014 while (flags
& (REMOVE_EMPTY_PARENTS_REF
| REMOVE_EMPTY_PARENTS_REFLOG
)) {
1015 while (q
> p
&& *q
!= '/')
1017 while (q
> p
&& *(q
-1) == '/')
1021 strbuf_setlen(&buf
, q
- buf
.buf
);
1024 files_ref_path(refs
, &sb
, buf
.buf
);
1025 if ((flags
& REMOVE_EMPTY_PARENTS_REF
) && rmdir(sb
.buf
))
1026 flags
&= ~REMOVE_EMPTY_PARENTS_REF
;
1029 files_reflog_path(refs
, &sb
, buf
.buf
);
1030 if ((flags
& REMOVE_EMPTY_PARENTS_REFLOG
) && rmdir(sb
.buf
))
1031 flags
&= ~REMOVE_EMPTY_PARENTS_REFLOG
;
1033 strbuf_release(&buf
);
1034 strbuf_release(&sb
);
1037 /* make sure nobody touched the ref, and unlink */
1038 static void prune_ref(struct files_ref_store
*refs
, struct ref_to_prune
*r
)
1040 struct ref_transaction
*transaction
;
1041 struct strbuf err
= STRBUF_INIT
;
1043 if (check_refname_format(r
->name
, 0))
1046 transaction
= ref_store_transaction_begin(&refs
->base
, &err
);
1048 ref_transaction_delete(transaction
, r
->name
, r
->sha1
,
1049 REF_ISPRUNING
| REF_NODEREF
, NULL
, &err
) ||
1050 ref_transaction_commit(transaction
, &err
)) {
1051 ref_transaction_free(transaction
);
1052 error("%s", err
.buf
);
1053 strbuf_release(&err
);
1056 ref_transaction_free(transaction
);
1057 strbuf_release(&err
);
1061 * Prune the loose versions of the references in the linked list
1062 * `*refs_to_prune`, freeing the entries in the list as we go.
1064 static void prune_refs(struct files_ref_store
*refs
, struct ref_to_prune
**refs_to_prune
)
1066 while (*refs_to_prune
) {
1067 struct ref_to_prune
*r
= *refs_to_prune
;
1068 *refs_to_prune
= r
->next
;
1075 * Return true if the specified reference should be packed.
1077 static int should_pack_ref(const char *refname
,
1078 const struct object_id
*oid
, unsigned int ref_flags
,
1079 unsigned int pack_flags
)
1081 /* Do not pack per-worktree refs: */
1082 if (ref_type(refname
) != REF_TYPE_NORMAL
)
1085 /* Do not pack non-tags unless PACK_REFS_ALL is set: */
1086 if (!(pack_flags
& PACK_REFS_ALL
) && !starts_with(refname
, "refs/tags/"))
1089 /* Do not pack symbolic refs: */
1090 if (ref_flags
& REF_ISSYMREF
)
1093 /* Do not pack broken refs: */
1094 if (!ref_resolves_to_object(refname
, oid
, ref_flags
))
1100 static int files_pack_refs(struct ref_store
*ref_store
, unsigned int flags
)
1102 struct files_ref_store
*refs
=
1103 files_downcast(ref_store
, REF_STORE_WRITE
| REF_STORE_ODB
,
1105 struct ref_iterator
*iter
;
1107 struct ref_to_prune
*refs_to_prune
= NULL
;
1108 struct strbuf err
= STRBUF_INIT
;
1109 struct ref_transaction
*transaction
;
1111 transaction
= ref_store_transaction_begin(refs
->packed_ref_store
, &err
);
1115 packed_refs_lock(refs
->packed_ref_store
, LOCK_DIE_ON_ERROR
, &err
);
1117 iter
= cache_ref_iterator_begin(get_loose_ref_cache(refs
), NULL
, 0);
1118 while ((ok
= ref_iterator_advance(iter
)) == ITER_OK
) {
1120 * If the loose reference can be packed, add an entry
1121 * in the packed ref cache. If the reference should be
1122 * pruned, also add it to refs_to_prune.
1124 if (!should_pack_ref(iter
->refname
, iter
->oid
, iter
->flags
,
1129 * Add a reference creation for this reference to the
1130 * packed-refs transaction:
1132 if (ref_transaction_update(transaction
, iter
->refname
,
1133 iter
->oid
->hash
, NULL
,
1134 REF_NODEREF
, NULL
, &err
))
1135 die("failure preparing to create packed reference %s: %s",
1136 iter
->refname
, err
.buf
);
1138 /* Schedule the loose reference for pruning if requested. */
1139 if ((flags
& PACK_REFS_PRUNE
)) {
1140 struct ref_to_prune
*n
;
1141 FLEX_ALLOC_STR(n
, name
, iter
->refname
);
1142 hashcpy(n
->sha1
, iter
->oid
->hash
);
1143 n
->next
= refs_to_prune
;
1147 if (ok
!= ITER_DONE
)
1148 die("error while iterating over references");
1150 if (ref_transaction_commit(transaction
, &err
))
1151 die("unable to write new packed-refs: %s", err
.buf
);
1153 ref_transaction_free(transaction
);
1155 packed_refs_unlock(refs
->packed_ref_store
);
1157 prune_refs(refs
, &refs_to_prune
);
1158 strbuf_release(&err
);
1162 static int files_delete_refs(struct ref_store
*ref_store
, const char *msg
,
1163 struct string_list
*refnames
, unsigned int flags
)
1165 struct files_ref_store
*refs
=
1166 files_downcast(ref_store
, REF_STORE_WRITE
, "delete_refs");
1167 struct strbuf err
= STRBUF_INIT
;
1173 if (packed_refs_lock(refs
->packed_ref_store
, 0, &err
))
1176 if (refs_delete_refs(refs
->packed_ref_store
, msg
, refnames
, flags
)) {
1177 packed_refs_unlock(refs
->packed_ref_store
);
1181 packed_refs_unlock(refs
->packed_ref_store
);
1183 for (i
= 0; i
< refnames
->nr
; i
++) {
1184 const char *refname
= refnames
->items
[i
].string
;
1186 if (refs_delete_ref(&refs
->base
, msg
, refname
, NULL
, flags
))
1187 result
|= error(_("could not remove reference %s"), refname
);
1190 strbuf_release(&err
);
1195 * If we failed to rewrite the packed-refs file, then it is
1196 * unsafe to try to remove loose refs, because doing so might
1197 * expose an obsolete packed value for a reference that might
1198 * even point at an object that has been garbage collected.
1200 if (refnames
->nr
== 1)
1201 error(_("could not delete reference %s: %s"),
1202 refnames
->items
[0].string
, err
.buf
);
1204 error(_("could not delete references: %s"), err
.buf
);
1206 strbuf_release(&err
);
1211 * People using contrib's git-new-workdir have .git/logs/refs ->
1212 * /some/other/path/.git/logs/refs, and that may live on another device.
1214 * IOW, to avoid cross device rename errors, the temporary renamed log must
1215 * live into logs/refs.
1217 #define TMP_RENAMED_LOG "refs/.tmp-renamed-log"
1220 const char *tmp_renamed_log
;
1224 static int rename_tmp_log_callback(const char *path
, void *cb_data
)
1226 struct rename_cb
*cb
= cb_data
;
1228 if (rename(cb
->tmp_renamed_log
, path
)) {
1230 * rename(a, b) when b is an existing directory ought
1231 * to result in ISDIR, but Solaris 5.8 gives ENOTDIR.
1232 * Sheesh. Record the true errno for error reporting,
1233 * but report EISDIR to raceproof_create_file() so
1234 * that it knows to retry.
1236 cb
->true_errno
= errno
;
1237 if (errno
== ENOTDIR
)
1245 static int rename_tmp_log(struct files_ref_store
*refs
, const char *newrefname
)
1247 struct strbuf path
= STRBUF_INIT
;
1248 struct strbuf tmp
= STRBUF_INIT
;
1249 struct rename_cb cb
;
1252 files_reflog_path(refs
, &path
, newrefname
);
1253 files_reflog_path(refs
, &tmp
, TMP_RENAMED_LOG
);
1254 cb
.tmp_renamed_log
= tmp
.buf
;
1255 ret
= raceproof_create_file(path
.buf
, rename_tmp_log_callback
, &cb
);
1257 if (errno
== EISDIR
)
1258 error("directory not empty: %s", path
.buf
);
1260 error("unable to move logfile %s to %s: %s",
1262 strerror(cb
.true_errno
));
1265 strbuf_release(&path
);
1266 strbuf_release(&tmp
);
1270 static int write_ref_to_lockfile(struct ref_lock
*lock
,
1271 const struct object_id
*oid
, struct strbuf
*err
);
1272 static int commit_ref_update(struct files_ref_store
*refs
,
1273 struct ref_lock
*lock
,
1274 const struct object_id
*oid
, const char *logmsg
,
1275 struct strbuf
*err
);
1277 static int files_rename_ref(struct ref_store
*ref_store
,
1278 const char *oldrefname
, const char *newrefname
,
1281 struct files_ref_store
*refs
=
1282 files_downcast(ref_store
, REF_STORE_WRITE
, "rename_ref");
1283 struct object_id oid
, orig_oid
;
1284 int flag
= 0, logmoved
= 0;
1285 struct ref_lock
*lock
;
1286 struct stat loginfo
;
1287 struct strbuf sb_oldref
= STRBUF_INIT
;
1288 struct strbuf sb_newref
= STRBUF_INIT
;
1289 struct strbuf tmp_renamed_log
= STRBUF_INIT
;
1291 struct strbuf err
= STRBUF_INIT
;
1293 files_reflog_path(refs
, &sb_oldref
, oldrefname
);
1294 files_reflog_path(refs
, &sb_newref
, newrefname
);
1295 files_reflog_path(refs
, &tmp_renamed_log
, TMP_RENAMED_LOG
);
1297 log
= !lstat(sb_oldref
.buf
, &loginfo
);
1298 if (log
&& S_ISLNK(loginfo
.st_mode
)) {
1299 ret
= error("reflog for %s is a symlink", oldrefname
);
1303 if (!refs_resolve_ref_unsafe(&refs
->base
, oldrefname
,
1304 RESOLVE_REF_READING
| RESOLVE_REF_NO_RECURSE
,
1305 orig_oid
.hash
, &flag
)) {
1306 ret
= error("refname %s not found", oldrefname
);
1310 if (flag
& REF_ISSYMREF
) {
1311 ret
= error("refname %s is a symbolic ref, renaming it is not supported",
1315 if (!refs_rename_ref_available(&refs
->base
, oldrefname
, newrefname
)) {
1320 if (log
&& rename(sb_oldref
.buf
, tmp_renamed_log
.buf
)) {
1321 ret
= error("unable to move logfile logs/%s to logs/"TMP_RENAMED_LOG
": %s",
1322 oldrefname
, strerror(errno
));
1326 if (refs_delete_ref(&refs
->base
, logmsg
, oldrefname
,
1327 orig_oid
.hash
, REF_NODEREF
)) {
1328 error("unable to delete old %s", oldrefname
);
1333 * Since we are doing a shallow lookup, oid is not the
1334 * correct value to pass to delete_ref as old_oid. But that
1335 * doesn't matter, because an old_oid check wouldn't add to
1336 * the safety anyway; we want to delete the reference whatever
1337 * its current value.
1339 if (!refs_read_ref_full(&refs
->base
, newrefname
,
1340 RESOLVE_REF_READING
| RESOLVE_REF_NO_RECURSE
,
1342 refs_delete_ref(&refs
->base
, NULL
, newrefname
,
1343 NULL
, REF_NODEREF
)) {
1344 if (errno
== EISDIR
) {
1345 struct strbuf path
= STRBUF_INIT
;
1348 files_ref_path(refs
, &path
, newrefname
);
1349 result
= remove_empty_directories(&path
);
1350 strbuf_release(&path
);
1353 error("Directory not empty: %s", newrefname
);
1357 error("unable to delete existing %s", newrefname
);
1362 if (log
&& rename_tmp_log(refs
, newrefname
))
1367 lock
= lock_ref_sha1_basic(refs
, newrefname
, NULL
, NULL
, NULL
,
1368 REF_NODEREF
, NULL
, &err
);
1370 error("unable to rename '%s' to '%s': %s", oldrefname
, newrefname
, err
.buf
);
1371 strbuf_release(&err
);
1374 oidcpy(&lock
->old_oid
, &orig_oid
);
1376 if (write_ref_to_lockfile(lock
, &orig_oid
, &err
) ||
1377 commit_ref_update(refs
, lock
, &orig_oid
, logmsg
, &err
)) {
1378 error("unable to write current sha1 into %s: %s", newrefname
, err
.buf
);
1379 strbuf_release(&err
);
1387 lock
= lock_ref_sha1_basic(refs
, oldrefname
, NULL
, NULL
, NULL
,
1388 REF_NODEREF
, NULL
, &err
);
1390 error("unable to lock %s for rollback: %s", oldrefname
, err
.buf
);
1391 strbuf_release(&err
);
1395 flag
= log_all_ref_updates
;
1396 log_all_ref_updates
= LOG_REFS_NONE
;
1397 if (write_ref_to_lockfile(lock
, &orig_oid
, &err
) ||
1398 commit_ref_update(refs
, lock
, &orig_oid
, NULL
, &err
)) {
1399 error("unable to write current sha1 into %s: %s", oldrefname
, err
.buf
);
1400 strbuf_release(&err
);
1402 log_all_ref_updates
= flag
;
1405 if (logmoved
&& rename(sb_newref
.buf
, sb_oldref
.buf
))
1406 error("unable to restore logfile %s from %s: %s",
1407 oldrefname
, newrefname
, strerror(errno
));
1408 if (!logmoved
&& log
&&
1409 rename(tmp_renamed_log
.buf
, sb_oldref
.buf
))
1410 error("unable to restore logfile %s from logs/"TMP_RENAMED_LOG
": %s",
1411 oldrefname
, strerror(errno
));
1414 strbuf_release(&sb_newref
);
1415 strbuf_release(&sb_oldref
);
1416 strbuf_release(&tmp_renamed_log
);
1421 static int close_ref(struct ref_lock
*lock
)
1423 if (close_lock_file(lock
->lk
))
1428 static int commit_ref(struct ref_lock
*lock
)
1430 char *path
= get_locked_file_path(lock
->lk
);
1433 if (!lstat(path
, &st
) && S_ISDIR(st
.st_mode
)) {
1435 * There is a directory at the path we want to rename
1436 * the lockfile to. Hopefully it is empty; try to
1439 size_t len
= strlen(path
);
1440 struct strbuf sb_path
= STRBUF_INIT
;
1442 strbuf_attach(&sb_path
, path
, len
, len
);
1445 * If this fails, commit_lock_file() will also fail
1446 * and will report the problem.
1448 remove_empty_directories(&sb_path
);
1449 strbuf_release(&sb_path
);
1454 if (commit_lock_file(lock
->lk
))
1459 static int open_or_create_logfile(const char *path
, void *cb
)
1463 *fd
= open(path
, O_APPEND
| O_WRONLY
| O_CREAT
, 0666);
1464 return (*fd
< 0) ? -1 : 0;
1468 * Create a reflog for a ref. If force_create = 0, only create the
1469 * reflog for certain refs (those for which should_autocreate_reflog
1470 * returns non-zero). Otherwise, create it regardless of the reference
1471 * name. If the logfile already existed or was created, return 0 and
1472 * set *logfd to the file descriptor opened for appending to the file.
1473 * If no logfile exists and we decided not to create one, return 0 and
1474 * set *logfd to -1. On failure, fill in *err, set *logfd to -1, and
1477 static int log_ref_setup(struct files_ref_store
*refs
,
1478 const char *refname
, int force_create
,
1479 int *logfd
, struct strbuf
*err
)
1481 struct strbuf logfile_sb
= STRBUF_INIT
;
1484 files_reflog_path(refs
, &logfile_sb
, refname
);
1485 logfile
= strbuf_detach(&logfile_sb
, NULL
);
1487 if (force_create
|| should_autocreate_reflog(refname
)) {
1488 if (raceproof_create_file(logfile
, open_or_create_logfile
, logfd
)) {
1489 if (errno
== ENOENT
)
1490 strbuf_addf(err
, "unable to create directory for '%s': "
1491 "%s", logfile
, strerror(errno
));
1492 else if (errno
== EISDIR
)
1493 strbuf_addf(err
, "there are still logs under '%s'",
1496 strbuf_addf(err
, "unable to append to '%s': %s",
1497 logfile
, strerror(errno
));
1502 *logfd
= open(logfile
, O_APPEND
| O_WRONLY
, 0666);
1504 if (errno
== ENOENT
|| errno
== EISDIR
) {
1506 * The logfile doesn't already exist,
1507 * but that is not an error; it only
1508 * means that we won't write log
1513 strbuf_addf(err
, "unable to append to '%s': %s",
1514 logfile
, strerror(errno
));
1521 adjust_shared_perm(logfile
);
1531 static int files_create_reflog(struct ref_store
*ref_store
,
1532 const char *refname
, int force_create
,
1535 struct files_ref_store
*refs
=
1536 files_downcast(ref_store
, REF_STORE_WRITE
, "create_reflog");
1539 if (log_ref_setup(refs
, refname
, force_create
, &fd
, err
))
1548 static int log_ref_write_fd(int fd
, const struct object_id
*old_oid
,
1549 const struct object_id
*new_oid
,
1550 const char *committer
, const char *msg
)
1552 int msglen
, written
;
1553 unsigned maxlen
, len
;
1556 msglen
= msg
? strlen(msg
) : 0;
1557 maxlen
= strlen(committer
) + msglen
+ 100;
1558 logrec
= xmalloc(maxlen
);
1559 len
= xsnprintf(logrec
, maxlen
, "%s %s %s\n",
1560 oid_to_hex(old_oid
),
1561 oid_to_hex(new_oid
),
1564 len
+= copy_reflog_msg(logrec
+ len
- 1, msg
) - 1;
1566 written
= len
<= maxlen
? write_in_full(fd
, logrec
, len
) : -1;
1574 static int files_log_ref_write(struct files_ref_store
*refs
,
1575 const char *refname
, const struct object_id
*old_oid
,
1576 const struct object_id
*new_oid
, const char *msg
,
1577 int flags
, struct strbuf
*err
)
1581 if (log_all_ref_updates
== LOG_REFS_UNSET
)
1582 log_all_ref_updates
= is_bare_repository() ? LOG_REFS_NONE
: LOG_REFS_NORMAL
;
1584 result
= log_ref_setup(refs
, refname
,
1585 flags
& REF_FORCE_CREATE_REFLOG
,
1593 result
= log_ref_write_fd(logfd
, old_oid
, new_oid
,
1594 git_committer_info(0), msg
);
1596 struct strbuf sb
= STRBUF_INIT
;
1597 int save_errno
= errno
;
1599 files_reflog_path(refs
, &sb
, refname
);
1600 strbuf_addf(err
, "unable to append to '%s': %s",
1601 sb
.buf
, strerror(save_errno
));
1602 strbuf_release(&sb
);
1607 struct strbuf sb
= STRBUF_INIT
;
1608 int save_errno
= errno
;
1610 files_reflog_path(refs
, &sb
, refname
);
1611 strbuf_addf(err
, "unable to append to '%s': %s",
1612 sb
.buf
, strerror(save_errno
));
1613 strbuf_release(&sb
);
1620 * Write sha1 into the open lockfile, then close the lockfile. On
1621 * errors, rollback the lockfile, fill in *err and
1624 static int write_ref_to_lockfile(struct ref_lock
*lock
,
1625 const struct object_id
*oid
, struct strbuf
*err
)
1627 static char term
= '\n';
1631 o
= parse_object(oid
);
1634 "trying to write ref '%s' with nonexistent object %s",
1635 lock
->ref_name
, oid_to_hex(oid
));
1639 if (o
->type
!= OBJ_COMMIT
&& is_branch(lock
->ref_name
)) {
1641 "trying to write non-commit object %s to branch '%s'",
1642 oid_to_hex(oid
), lock
->ref_name
);
1646 fd
= get_lock_file_fd(lock
->lk
);
1647 if (write_in_full(fd
, oid_to_hex(oid
), GIT_SHA1_HEXSZ
) != GIT_SHA1_HEXSZ
||
1648 write_in_full(fd
, &term
, 1) != 1 ||
1649 close_ref(lock
) < 0) {
1651 "couldn't write '%s'", get_lock_file_path(lock
->lk
));
1659 * Commit a change to a loose reference that has already been written
1660 * to the loose reference lockfile. Also update the reflogs if
1661 * necessary, using the specified lockmsg (which can be NULL).
1663 static int commit_ref_update(struct files_ref_store
*refs
,
1664 struct ref_lock
*lock
,
1665 const struct object_id
*oid
, const char *logmsg
,
1668 files_assert_main_repository(refs
, "commit_ref_update");
1670 clear_loose_ref_cache(refs
);
1671 if (files_log_ref_write(refs
, lock
->ref_name
,
1672 &lock
->old_oid
, oid
,
1674 char *old_msg
= strbuf_detach(err
, NULL
);
1675 strbuf_addf(err
, "cannot update the ref '%s': %s",
1676 lock
->ref_name
, old_msg
);
1682 if (strcmp(lock
->ref_name
, "HEAD") != 0) {
1684 * Special hack: If a branch is updated directly and HEAD
1685 * points to it (may happen on the remote side of a push
1686 * for example) then logically the HEAD reflog should be
1688 * A generic solution implies reverse symref information,
1689 * but finding all symrefs pointing to the given branch
1690 * would be rather costly for this rare event (the direct
1691 * update of a branch) to be worth it. So let's cheat and
1692 * check with HEAD only which should cover 99% of all usage
1693 * scenarios (even 100% of the default ones).
1695 struct object_id head_oid
;
1697 const char *head_ref
;
1699 head_ref
= refs_resolve_ref_unsafe(&refs
->base
, "HEAD",
1700 RESOLVE_REF_READING
,
1701 head_oid
.hash
, &head_flag
);
1702 if (head_ref
&& (head_flag
& REF_ISSYMREF
) &&
1703 !strcmp(head_ref
, lock
->ref_name
)) {
1704 struct strbuf log_err
= STRBUF_INIT
;
1705 if (files_log_ref_write(refs
, "HEAD",
1706 &lock
->old_oid
, oid
,
1707 logmsg
, 0, &log_err
)) {
1708 error("%s", log_err
.buf
);
1709 strbuf_release(&log_err
);
1714 if (commit_ref(lock
)) {
1715 strbuf_addf(err
, "couldn't set '%s'", lock
->ref_name
);
1724 static int create_ref_symlink(struct ref_lock
*lock
, const char *target
)
1727 #ifndef NO_SYMLINK_HEAD
1728 char *ref_path
= get_locked_file_path(lock
->lk
);
1730 ret
= symlink(target
, ref_path
);
1734 fprintf(stderr
, "no symlink - falling back to symbolic ref\n");
1739 static void update_symref_reflog(struct files_ref_store
*refs
,
1740 struct ref_lock
*lock
, const char *refname
,
1741 const char *target
, const char *logmsg
)
1743 struct strbuf err
= STRBUF_INIT
;
1744 struct object_id new_oid
;
1746 !refs_read_ref_full(&refs
->base
, target
,
1747 RESOLVE_REF_READING
, new_oid
.hash
, NULL
) &&
1748 files_log_ref_write(refs
, refname
, &lock
->old_oid
,
1749 &new_oid
, logmsg
, 0, &err
)) {
1750 error("%s", err
.buf
);
1751 strbuf_release(&err
);
1755 static int create_symref_locked(struct files_ref_store
*refs
,
1756 struct ref_lock
*lock
, const char *refname
,
1757 const char *target
, const char *logmsg
)
1759 if (prefer_symlink_refs
&& !create_ref_symlink(lock
, target
)) {
1760 update_symref_reflog(refs
, lock
, refname
, target
, logmsg
);
1764 if (!fdopen_lock_file(lock
->lk
, "w"))
1765 return error("unable to fdopen %s: %s",
1766 lock
->lk
->tempfile
.filename
.buf
, strerror(errno
));
1768 update_symref_reflog(refs
, lock
, refname
, target
, logmsg
);
1770 /* no error check; commit_ref will check ferror */
1771 fprintf(lock
->lk
->tempfile
.fp
, "ref: %s\n", target
);
1772 if (commit_ref(lock
) < 0)
1773 return error("unable to write symref for %s: %s", refname
,
1778 static int files_create_symref(struct ref_store
*ref_store
,
1779 const char *refname
, const char *target
,
1782 struct files_ref_store
*refs
=
1783 files_downcast(ref_store
, REF_STORE_WRITE
, "create_symref");
1784 struct strbuf err
= STRBUF_INIT
;
1785 struct ref_lock
*lock
;
1788 lock
= lock_ref_sha1_basic(refs
, refname
, NULL
,
1789 NULL
, NULL
, REF_NODEREF
, NULL
,
1792 error("%s", err
.buf
);
1793 strbuf_release(&err
);
1797 ret
= create_symref_locked(refs
, lock
, refname
, target
, logmsg
);
1802 static int files_reflog_exists(struct ref_store
*ref_store
,
1803 const char *refname
)
1805 struct files_ref_store
*refs
=
1806 files_downcast(ref_store
, REF_STORE_READ
, "reflog_exists");
1807 struct strbuf sb
= STRBUF_INIT
;
1811 files_reflog_path(refs
, &sb
, refname
);
1812 ret
= !lstat(sb
.buf
, &st
) && S_ISREG(st
.st_mode
);
1813 strbuf_release(&sb
);
1817 static int files_delete_reflog(struct ref_store
*ref_store
,
1818 const char *refname
)
1820 struct files_ref_store
*refs
=
1821 files_downcast(ref_store
, REF_STORE_WRITE
, "delete_reflog");
1822 struct strbuf sb
= STRBUF_INIT
;
1825 files_reflog_path(refs
, &sb
, refname
);
1826 ret
= remove_path(sb
.buf
);
1827 strbuf_release(&sb
);
1831 static int show_one_reflog_ent(struct strbuf
*sb
, each_reflog_ent_fn fn
, void *cb_data
)
1833 struct object_id ooid
, noid
;
1834 char *email_end
, *message
;
1835 timestamp_t timestamp
;
1837 const char *p
= sb
->buf
;
1839 /* old SP new SP name <email> SP time TAB msg LF */
1840 if (!sb
->len
|| sb
->buf
[sb
->len
- 1] != '\n' ||
1841 parse_oid_hex(p
, &ooid
, &p
) || *p
++ != ' ' ||
1842 parse_oid_hex(p
, &noid
, &p
) || *p
++ != ' ' ||
1843 !(email_end
= strchr(p
, '>')) ||
1844 email_end
[1] != ' ' ||
1845 !(timestamp
= parse_timestamp(email_end
+ 2, &message
, 10)) ||
1846 !message
|| message
[0] != ' ' ||
1847 (message
[1] != '+' && message
[1] != '-') ||
1848 !isdigit(message
[2]) || !isdigit(message
[3]) ||
1849 !isdigit(message
[4]) || !isdigit(message
[5]))
1850 return 0; /* corrupt? */
1851 email_end
[1] = '\0';
1852 tz
= strtol(message
+ 1, NULL
, 10);
1853 if (message
[6] != '\t')
1857 return fn(&ooid
, &noid
, p
, timestamp
, tz
, message
, cb_data
);
1860 static char *find_beginning_of_line(char *bob
, char *scan
)
1862 while (bob
< scan
&& *(--scan
) != '\n')
1863 ; /* keep scanning backwards */
1865 * Return either beginning of the buffer, or LF at the end of
1866 * the previous line.
1871 static int files_for_each_reflog_ent_reverse(struct ref_store
*ref_store
,
1872 const char *refname
,
1873 each_reflog_ent_fn fn
,
1876 struct files_ref_store
*refs
=
1877 files_downcast(ref_store
, REF_STORE_READ
,
1878 "for_each_reflog_ent_reverse");
1879 struct strbuf sb
= STRBUF_INIT
;
1882 int ret
= 0, at_tail
= 1;
1884 files_reflog_path(refs
, &sb
, refname
);
1885 logfp
= fopen(sb
.buf
, "r");
1886 strbuf_release(&sb
);
1890 /* Jump to the end */
1891 if (fseek(logfp
, 0, SEEK_END
) < 0)
1892 ret
= error("cannot seek back reflog for %s: %s",
1893 refname
, strerror(errno
));
1895 while (!ret
&& 0 < pos
) {
1901 /* Fill next block from the end */
1902 cnt
= (sizeof(buf
) < pos
) ? sizeof(buf
) : pos
;
1903 if (fseek(logfp
, pos
- cnt
, SEEK_SET
)) {
1904 ret
= error("cannot seek back reflog for %s: %s",
1905 refname
, strerror(errno
));
1908 nread
= fread(buf
, cnt
, 1, logfp
);
1910 ret
= error("cannot read %d bytes from reflog for %s: %s",
1911 cnt
, refname
, strerror(errno
));
1916 scanp
= endp
= buf
+ cnt
;
1917 if (at_tail
&& scanp
[-1] == '\n')
1918 /* Looking at the final LF at the end of the file */
1922 while (buf
< scanp
) {
1924 * terminating LF of the previous line, or the beginning
1929 bp
= find_beginning_of_line(buf
, scanp
);
1933 * The newline is the end of the previous line,
1934 * so we know we have complete line starting
1935 * at (bp + 1). Prefix it onto any prior data
1936 * we collected for the line and process it.
1938 strbuf_splice(&sb
, 0, 0, bp
+ 1, endp
- (bp
+ 1));
1941 ret
= show_one_reflog_ent(&sb
, fn
, cb_data
);
1947 * We are at the start of the buffer, and the
1948 * start of the file; there is no previous
1949 * line, and we have everything for this one.
1950 * Process it, and we can end the loop.
1952 strbuf_splice(&sb
, 0, 0, buf
, endp
- buf
);
1953 ret
= show_one_reflog_ent(&sb
, fn
, cb_data
);
1960 * We are at the start of the buffer, and there
1961 * is more file to read backwards. Which means
1962 * we are in the middle of a line. Note that we
1963 * may get here even if *bp was a newline; that
1964 * just means we are at the exact end of the
1965 * previous line, rather than some spot in the
1968 * Save away what we have to be combined with
1969 * the data from the next read.
1971 strbuf_splice(&sb
, 0, 0, buf
, endp
- buf
);
1978 die("BUG: reverse reflog parser had leftover data");
1981 strbuf_release(&sb
);
1985 static int files_for_each_reflog_ent(struct ref_store
*ref_store
,
1986 const char *refname
,
1987 each_reflog_ent_fn fn
, void *cb_data
)
1989 struct files_ref_store
*refs
=
1990 files_downcast(ref_store
, REF_STORE_READ
,
1991 "for_each_reflog_ent");
1993 struct strbuf sb
= STRBUF_INIT
;
1996 files_reflog_path(refs
, &sb
, refname
);
1997 logfp
= fopen(sb
.buf
, "r");
1998 strbuf_release(&sb
);
2002 while (!ret
&& !strbuf_getwholeline(&sb
, logfp
, '\n'))
2003 ret
= show_one_reflog_ent(&sb
, fn
, cb_data
);
2005 strbuf_release(&sb
);
2009 struct files_reflog_iterator
{
2010 struct ref_iterator base
;
2012 struct ref_store
*ref_store
;
2013 struct dir_iterator
*dir_iterator
;
2014 struct object_id oid
;
2017 static int files_reflog_iterator_advance(struct ref_iterator
*ref_iterator
)
2019 struct files_reflog_iterator
*iter
=
2020 (struct files_reflog_iterator
*)ref_iterator
;
2021 struct dir_iterator
*diter
= iter
->dir_iterator
;
2024 while ((ok
= dir_iterator_advance(diter
)) == ITER_OK
) {
2027 if (!S_ISREG(diter
->st
.st_mode
))
2029 if (diter
->basename
[0] == '.')
2031 if (ends_with(diter
->basename
, ".lock"))
2034 if (refs_read_ref_full(iter
->ref_store
,
2035 diter
->relative_path
, 0,
2036 iter
->oid
.hash
, &flags
)) {
2037 error("bad ref for %s", diter
->path
.buf
);
2041 iter
->base
.refname
= diter
->relative_path
;
2042 iter
->base
.oid
= &iter
->oid
;
2043 iter
->base
.flags
= flags
;
2047 iter
->dir_iterator
= NULL
;
2048 if (ref_iterator_abort(ref_iterator
) == ITER_ERROR
)
2053 static int files_reflog_iterator_peel(struct ref_iterator
*ref_iterator
,
2054 struct object_id
*peeled
)
2056 die("BUG: ref_iterator_peel() called for reflog_iterator");
2059 static int files_reflog_iterator_abort(struct ref_iterator
*ref_iterator
)
2061 struct files_reflog_iterator
*iter
=
2062 (struct files_reflog_iterator
*)ref_iterator
;
2065 if (iter
->dir_iterator
)
2066 ok
= dir_iterator_abort(iter
->dir_iterator
);
2068 base_ref_iterator_free(ref_iterator
);
2072 static struct ref_iterator_vtable files_reflog_iterator_vtable
= {
2073 files_reflog_iterator_advance
,
2074 files_reflog_iterator_peel
,
2075 files_reflog_iterator_abort
2078 static struct ref_iterator
*files_reflog_iterator_begin(struct ref_store
*ref_store
)
2080 struct files_ref_store
*refs
=
2081 files_downcast(ref_store
, REF_STORE_READ
,
2082 "reflog_iterator_begin");
2083 struct files_reflog_iterator
*iter
= xcalloc(1, sizeof(*iter
));
2084 struct ref_iterator
*ref_iterator
= &iter
->base
;
2085 struct strbuf sb
= STRBUF_INIT
;
2087 base_ref_iterator_init(ref_iterator
, &files_reflog_iterator_vtable
);
2088 files_reflog_path(refs
, &sb
, NULL
);
2089 iter
->dir_iterator
= dir_iterator_begin(sb
.buf
);
2090 iter
->ref_store
= ref_store
;
2091 strbuf_release(&sb
);
2092 return ref_iterator
;
2096 * If update is a direct update of head_ref (the reference pointed to
2097 * by HEAD), then add an extra REF_LOG_ONLY update for HEAD.
2099 static int split_head_update(struct ref_update
*update
,
2100 struct ref_transaction
*transaction
,
2101 const char *head_ref
,
2102 struct string_list
*affected_refnames
,
2105 struct string_list_item
*item
;
2106 struct ref_update
*new_update
;
2108 if ((update
->flags
& REF_LOG_ONLY
) ||
2109 (update
->flags
& REF_ISPRUNING
) ||
2110 (update
->flags
& REF_UPDATE_VIA_HEAD
))
2113 if (strcmp(update
->refname
, head_ref
))
2117 * First make sure that HEAD is not already in the
2118 * transaction. This insertion is O(N) in the transaction
2119 * size, but it happens at most once per transaction.
2121 item
= string_list_insert(affected_refnames
, "HEAD");
2123 /* An entry already existed */
2125 "multiple updates for 'HEAD' (including one "
2126 "via its referent '%s') are not allowed",
2128 return TRANSACTION_NAME_CONFLICT
;
2131 new_update
= ref_transaction_add_update(
2132 transaction
, "HEAD",
2133 update
->flags
| REF_LOG_ONLY
| REF_NODEREF
,
2134 update
->new_oid
.hash
, update
->old_oid
.hash
,
2137 item
->util
= new_update
;
2143 * update is for a symref that points at referent and doesn't have
2144 * REF_NODEREF set. Split it into two updates:
2145 * - The original update, but with REF_LOG_ONLY and REF_NODEREF set
2146 * - A new, separate update for the referent reference
2147 * Note that the new update will itself be subject to splitting when
2148 * the iteration gets to it.
2150 static int split_symref_update(struct files_ref_store
*refs
,
2151 struct ref_update
*update
,
2152 const char *referent
,
2153 struct ref_transaction
*transaction
,
2154 struct string_list
*affected_refnames
,
2157 struct string_list_item
*item
;
2158 struct ref_update
*new_update
;
2159 unsigned int new_flags
;
2162 * First make sure that referent is not already in the
2163 * transaction. This insertion is O(N) in the transaction
2164 * size, but it happens at most once per symref in a
2167 item
= string_list_insert(affected_refnames
, referent
);
2169 /* An entry already existed */
2171 "multiple updates for '%s' (including one "
2172 "via symref '%s') are not allowed",
2173 referent
, update
->refname
);
2174 return TRANSACTION_NAME_CONFLICT
;
2177 new_flags
= update
->flags
;
2178 if (!strcmp(update
->refname
, "HEAD")) {
2180 * Record that the new update came via HEAD, so that
2181 * when we process it, split_head_update() doesn't try
2182 * to add another reflog update for HEAD. Note that
2183 * this bit will be propagated if the new_update
2184 * itself needs to be split.
2186 new_flags
|= REF_UPDATE_VIA_HEAD
;
2189 new_update
= ref_transaction_add_update(
2190 transaction
, referent
, new_flags
,
2191 update
->new_oid
.hash
, update
->old_oid
.hash
,
2194 new_update
->parent_update
= update
;
2197 * Change the symbolic ref update to log only. Also, it
2198 * doesn't need to check its old SHA-1 value, as that will be
2199 * done when new_update is processed.
2201 update
->flags
|= REF_LOG_ONLY
| REF_NODEREF
;
2202 update
->flags
&= ~REF_HAVE_OLD
;
2204 item
->util
= new_update
;
2210 * Return the refname under which update was originally requested.
2212 static const char *original_update_refname(struct ref_update
*update
)
2214 while (update
->parent_update
)
2215 update
= update
->parent_update
;
2217 return update
->refname
;
2221 * Check whether the REF_HAVE_OLD and old_oid values stored in update
2222 * are consistent with oid, which is the reference's current value. If
2223 * everything is OK, return 0; otherwise, write an error message to
2224 * err and return -1.
2226 static int check_old_oid(struct ref_update
*update
, struct object_id
*oid
,
2229 if (!(update
->flags
& REF_HAVE_OLD
) ||
2230 !oidcmp(oid
, &update
->old_oid
))
2233 if (is_null_oid(&update
->old_oid
))
2234 strbuf_addf(err
, "cannot lock ref '%s': "
2235 "reference already exists",
2236 original_update_refname(update
));
2237 else if (is_null_oid(oid
))
2238 strbuf_addf(err
, "cannot lock ref '%s': "
2239 "reference is missing but expected %s",
2240 original_update_refname(update
),
2241 oid_to_hex(&update
->old_oid
));
2243 strbuf_addf(err
, "cannot lock ref '%s': "
2244 "is at %s but expected %s",
2245 original_update_refname(update
),
2247 oid_to_hex(&update
->old_oid
));
2253 * Prepare for carrying out update:
2254 * - Lock the reference referred to by update.
2255 * - Read the reference under lock.
2256 * - Check that its old SHA-1 value (if specified) is correct, and in
2257 * any case record it in update->lock->old_oid for later use when
2258 * writing the reflog.
2259 * - If it is a symref update without REF_NODEREF, split it up into a
2260 * REF_LOG_ONLY update of the symref and add a separate update for
2261 * the referent to transaction.
2262 * - If it is an update of head_ref, add a corresponding REF_LOG_ONLY
2265 static int lock_ref_for_update(struct files_ref_store
*refs
,
2266 struct ref_update
*update
,
2267 struct ref_transaction
*transaction
,
2268 const char *head_ref
,
2269 struct string_list
*affected_refnames
,
2272 struct strbuf referent
= STRBUF_INIT
;
2273 int mustexist
= (update
->flags
& REF_HAVE_OLD
) &&
2274 !is_null_oid(&update
->old_oid
);
2276 struct ref_lock
*lock
;
2278 files_assert_main_repository(refs
, "lock_ref_for_update");
2280 if ((update
->flags
& REF_HAVE_NEW
) && is_null_oid(&update
->new_oid
))
2281 update
->flags
|= REF_DELETING
;
2284 ret
= split_head_update(update
, transaction
, head_ref
,
2285 affected_refnames
, err
);
2290 ret
= lock_raw_ref(refs
, update
->refname
, mustexist
,
2291 affected_refnames
, NULL
,
2293 &update
->type
, err
);
2297 reason
= strbuf_detach(err
, NULL
);
2298 strbuf_addf(err
, "cannot lock ref '%s': %s",
2299 original_update_refname(update
), reason
);
2304 update
->backend_data
= lock
;
2306 if (update
->type
& REF_ISSYMREF
) {
2307 if (update
->flags
& REF_NODEREF
) {
2309 * We won't be reading the referent as part of
2310 * the transaction, so we have to read it here
2311 * to record and possibly check old_sha1:
2313 if (refs_read_ref_full(&refs
->base
,
2315 lock
->old_oid
.hash
, NULL
)) {
2316 if (update
->flags
& REF_HAVE_OLD
) {
2317 strbuf_addf(err
, "cannot lock ref '%s': "
2318 "error reading reference",
2319 original_update_refname(update
));
2322 } else if (check_old_oid(update
, &lock
->old_oid
, err
)) {
2323 return TRANSACTION_GENERIC_ERROR
;
2327 * Create a new update for the reference this
2328 * symref is pointing at. Also, we will record
2329 * and verify old_sha1 for this update as part
2330 * of processing the split-off update, so we
2331 * don't have to do it here.
2333 ret
= split_symref_update(refs
, update
,
2334 referent
.buf
, transaction
,
2335 affected_refnames
, err
);
2340 struct ref_update
*parent_update
;
2342 if (check_old_oid(update
, &lock
->old_oid
, err
))
2343 return TRANSACTION_GENERIC_ERROR
;
2346 * If this update is happening indirectly because of a
2347 * symref update, record the old SHA-1 in the parent
2350 for (parent_update
= update
->parent_update
;
2352 parent_update
= parent_update
->parent_update
) {
2353 struct ref_lock
*parent_lock
= parent_update
->backend_data
;
2354 oidcpy(&parent_lock
->old_oid
, &lock
->old_oid
);
2358 if ((update
->flags
& REF_HAVE_NEW
) &&
2359 !(update
->flags
& REF_DELETING
) &&
2360 !(update
->flags
& REF_LOG_ONLY
)) {
2361 if (!(update
->type
& REF_ISSYMREF
) &&
2362 !oidcmp(&lock
->old_oid
, &update
->new_oid
)) {
2364 * The reference already has the desired
2365 * value, so we don't need to write it.
2367 } else if (write_ref_to_lockfile(lock
, &update
->new_oid
,
2369 char *write_err
= strbuf_detach(err
, NULL
);
2372 * The lock was freed upon failure of
2373 * write_ref_to_lockfile():
2375 update
->backend_data
= NULL
;
2377 "cannot update ref '%s': %s",
2378 update
->refname
, write_err
);
2380 return TRANSACTION_GENERIC_ERROR
;
2382 update
->flags
|= REF_NEEDS_COMMIT
;
2385 if (!(update
->flags
& REF_NEEDS_COMMIT
)) {
2387 * We didn't call write_ref_to_lockfile(), so
2388 * the lockfile is still open. Close it to
2389 * free up the file descriptor:
2391 if (close_ref(lock
)) {
2392 strbuf_addf(err
, "couldn't close '%s.lock'",
2394 return TRANSACTION_GENERIC_ERROR
;
2400 struct files_transaction_backend_data
{
2401 struct ref_transaction
*packed_transaction
;
2402 int packed_refs_locked
;
2406 * Unlock any references in `transaction` that are still locked, and
2407 * mark the transaction closed.
2409 static void files_transaction_cleanup(struct files_ref_store
*refs
,
2410 struct ref_transaction
*transaction
)
2413 struct files_transaction_backend_data
*backend_data
=
2414 transaction
->backend_data
;
2415 struct strbuf err
= STRBUF_INIT
;
2417 for (i
= 0; i
< transaction
->nr
; i
++) {
2418 struct ref_update
*update
= transaction
->updates
[i
];
2419 struct ref_lock
*lock
= update
->backend_data
;
2423 update
->backend_data
= NULL
;
2427 if (backend_data
->packed_transaction
&&
2428 ref_transaction_abort(backend_data
->packed_transaction
, &err
)) {
2429 error("error aborting transaction: %s", err
.buf
);
2430 strbuf_release(&err
);
2433 if (backend_data
->packed_refs_locked
)
2434 packed_refs_unlock(refs
->packed_ref_store
);
2438 transaction
->state
= REF_TRANSACTION_CLOSED
;
2441 static int files_transaction_prepare(struct ref_store
*ref_store
,
2442 struct ref_transaction
*transaction
,
2445 struct files_ref_store
*refs
=
2446 files_downcast(ref_store
, REF_STORE_WRITE
,
2447 "ref_transaction_prepare");
2450 struct string_list affected_refnames
= STRING_LIST_INIT_NODUP
;
2451 char *head_ref
= NULL
;
2453 struct object_id head_oid
;
2454 struct files_transaction_backend_data
*backend_data
;
2455 struct ref_transaction
*packed_transaction
= NULL
;
2459 if (!transaction
->nr
)
2462 backend_data
= xcalloc(1, sizeof(*backend_data
));
2463 transaction
->backend_data
= backend_data
;
2466 * Fail if a refname appears more than once in the
2467 * transaction. (If we end up splitting up any updates using
2468 * split_symref_update() or split_head_update(), those
2469 * functions will check that the new updates don't have the
2470 * same refname as any existing ones.)
2472 for (i
= 0; i
< transaction
->nr
; i
++) {
2473 struct ref_update
*update
= transaction
->updates
[i
];
2474 struct string_list_item
*item
=
2475 string_list_append(&affected_refnames
, update
->refname
);
2478 * We store a pointer to update in item->util, but at
2479 * the moment we never use the value of this field
2480 * except to check whether it is non-NULL.
2482 item
->util
= update
;
2484 string_list_sort(&affected_refnames
);
2485 if (ref_update_reject_duplicates(&affected_refnames
, err
)) {
2486 ret
= TRANSACTION_GENERIC_ERROR
;
2491 * Special hack: If a branch is updated directly and HEAD
2492 * points to it (may happen on the remote side of a push
2493 * for example) then logically the HEAD reflog should be
2496 * A generic solution would require reverse symref lookups,
2497 * but finding all symrefs pointing to a given branch would be
2498 * rather costly for this rare event (the direct update of a
2499 * branch) to be worth it. So let's cheat and check with HEAD
2500 * only, which should cover 99% of all usage scenarios (even
2501 * 100% of the default ones).
2503 * So if HEAD is a symbolic reference, then record the name of
2504 * the reference that it points to. If we see an update of
2505 * head_ref within the transaction, then split_head_update()
2506 * arranges for the reflog of HEAD to be updated, too.
2508 head_ref
= refs_resolve_refdup(ref_store
, "HEAD",
2509 RESOLVE_REF_NO_RECURSE
,
2510 head_oid
.hash
, &head_type
);
2512 if (head_ref
&& !(head_type
& REF_ISSYMREF
)) {
2513 FREE_AND_NULL(head_ref
);
2517 * Acquire all locks, verify old values if provided, check
2518 * that new values are valid, and write new values to the
2519 * lockfiles, ready to be activated. Only keep one lockfile
2520 * open at a time to avoid running out of file descriptors.
2521 * Note that lock_ref_for_update() might append more updates
2522 * to the transaction.
2524 for (i
= 0; i
< transaction
->nr
; i
++) {
2525 struct ref_update
*update
= transaction
->updates
[i
];
2527 ret
= lock_ref_for_update(refs
, update
, transaction
,
2528 head_ref
, &affected_refnames
, err
);
2532 if (update
->flags
& REF_DELETING
&&
2533 !(update
->flags
& REF_LOG_ONLY
) &&
2534 !(update
->flags
& REF_ISPRUNING
)) {
2536 * This reference has to be deleted from
2537 * packed-refs if it exists there.
2539 if (!packed_transaction
) {
2540 packed_transaction
= ref_store_transaction_begin(
2541 refs
->packed_ref_store
, err
);
2542 if (!packed_transaction
) {
2543 ret
= TRANSACTION_GENERIC_ERROR
;
2547 backend_data
->packed_transaction
=
2551 ref_transaction_add_update(
2552 packed_transaction
, update
->refname
,
2553 update
->flags
& ~REF_HAVE_OLD
,
2554 update
->new_oid
.hash
, update
->old_oid
.hash
,
2559 if (packed_transaction
) {
2560 if (packed_refs_lock(refs
->packed_ref_store
, 0, err
)) {
2561 ret
= TRANSACTION_GENERIC_ERROR
;
2564 backend_data
->packed_refs_locked
= 1;
2565 ret
= ref_transaction_prepare(packed_transaction
, err
);
2570 string_list_clear(&affected_refnames
, 0);
2573 files_transaction_cleanup(refs
, transaction
);
2575 transaction
->state
= REF_TRANSACTION_PREPARED
;
2580 static int files_transaction_finish(struct ref_store
*ref_store
,
2581 struct ref_transaction
*transaction
,
2584 struct files_ref_store
*refs
=
2585 files_downcast(ref_store
, 0, "ref_transaction_finish");
2588 struct strbuf sb
= STRBUF_INIT
;
2589 struct files_transaction_backend_data
*backend_data
;
2590 struct ref_transaction
*packed_transaction
;
2595 if (!transaction
->nr
) {
2596 transaction
->state
= REF_TRANSACTION_CLOSED
;
2600 backend_data
= transaction
->backend_data
;
2601 packed_transaction
= backend_data
->packed_transaction
;
2603 /* Perform updates first so live commits remain referenced */
2604 for (i
= 0; i
< transaction
->nr
; i
++) {
2605 struct ref_update
*update
= transaction
->updates
[i
];
2606 struct ref_lock
*lock
= update
->backend_data
;
2608 if (update
->flags
& REF_NEEDS_COMMIT
||
2609 update
->flags
& REF_LOG_ONLY
) {
2610 if (files_log_ref_write(refs
,
2614 update
->msg
, update
->flags
,
2616 char *old_msg
= strbuf_detach(err
, NULL
);
2618 strbuf_addf(err
, "cannot update the ref '%s': %s",
2619 lock
->ref_name
, old_msg
);
2622 update
->backend_data
= NULL
;
2623 ret
= TRANSACTION_GENERIC_ERROR
;
2627 if (update
->flags
& REF_NEEDS_COMMIT
) {
2628 clear_loose_ref_cache(refs
);
2629 if (commit_ref(lock
)) {
2630 strbuf_addf(err
, "couldn't set '%s'", lock
->ref_name
);
2632 update
->backend_data
= NULL
;
2633 ret
= TRANSACTION_GENERIC_ERROR
;
2640 * Now that updates are safely completed, we can perform
2641 * deletes. First delete the reflogs of any references that
2642 * will be deleted, since (in the unexpected event of an
2643 * error) leaving a reference without a reflog is less bad
2644 * than leaving a reflog without a reference (the latter is a
2645 * mildly invalid repository state):
2647 for (i
= 0; i
< transaction
->nr
; i
++) {
2648 struct ref_update
*update
= transaction
->updates
[i
];
2649 if (update
->flags
& REF_DELETING
&&
2650 !(update
->flags
& REF_LOG_ONLY
) &&
2651 !(update
->flags
& REF_ISPRUNING
)) {
2653 files_reflog_path(refs
, &sb
, update
->refname
);
2654 if (!unlink_or_warn(sb
.buf
))
2655 try_remove_empty_parents(refs
, update
->refname
,
2656 REMOVE_EMPTY_PARENTS_REFLOG
);
2661 * Perform deletes now that updates are safely completed.
2663 * First delete any packed versions of the references, while
2664 * retaining the packed-refs lock:
2666 if (packed_transaction
) {
2667 ret
= ref_transaction_commit(packed_transaction
, err
);
2668 ref_transaction_free(packed_transaction
);
2669 packed_transaction
= NULL
;
2670 backend_data
->packed_transaction
= NULL
;
2675 /* Now delete the loose versions of the references: */
2676 for (i
= 0; i
< transaction
->nr
; i
++) {
2677 struct ref_update
*update
= transaction
->updates
[i
];
2678 struct ref_lock
*lock
= update
->backend_data
;
2680 if (update
->flags
& REF_DELETING
&&
2681 !(update
->flags
& REF_LOG_ONLY
)) {
2682 if (!(update
->type
& REF_ISPACKED
) ||
2683 update
->type
& REF_ISSYMREF
) {
2684 /* It is a loose reference. */
2686 files_ref_path(refs
, &sb
, lock
->ref_name
);
2687 if (unlink_or_msg(sb
.buf
, err
)) {
2688 ret
= TRANSACTION_GENERIC_ERROR
;
2691 update
->flags
|= REF_DELETED_LOOSE
;
2696 clear_loose_ref_cache(refs
);
2699 files_transaction_cleanup(refs
, transaction
);
2701 for (i
= 0; i
< transaction
->nr
; i
++) {
2702 struct ref_update
*update
= transaction
->updates
[i
];
2704 if (update
->flags
& REF_DELETED_LOOSE
) {
2706 * The loose reference was deleted. Delete any
2707 * empty parent directories. (Note that this
2708 * can only work because we have already
2709 * removed the lockfile.)
2711 try_remove_empty_parents(refs
, update
->refname
,
2712 REMOVE_EMPTY_PARENTS_REF
);
2716 strbuf_release(&sb
);
2720 static int files_transaction_abort(struct ref_store
*ref_store
,
2721 struct ref_transaction
*transaction
,
2724 struct files_ref_store
*refs
=
2725 files_downcast(ref_store
, 0, "ref_transaction_abort");
2727 files_transaction_cleanup(refs
, transaction
);
2731 static int ref_present(const char *refname
,
2732 const struct object_id
*oid
, int flags
, void *cb_data
)
2734 struct string_list
*affected_refnames
= cb_data
;
2736 return string_list_has_string(affected_refnames
, refname
);
2739 static int files_initial_transaction_commit(struct ref_store
*ref_store
,
2740 struct ref_transaction
*transaction
,
2743 struct files_ref_store
*refs
=
2744 files_downcast(ref_store
, REF_STORE_WRITE
,
2745 "initial_ref_transaction_commit");
2748 struct string_list affected_refnames
= STRING_LIST_INIT_NODUP
;
2749 struct ref_transaction
*packed_transaction
= NULL
;
2753 if (transaction
->state
!= REF_TRANSACTION_OPEN
)
2754 die("BUG: commit called for transaction that is not open");
2756 /* Fail if a refname appears more than once in the transaction: */
2757 for (i
= 0; i
< transaction
->nr
; i
++)
2758 string_list_append(&affected_refnames
,
2759 transaction
->updates
[i
]->refname
);
2760 string_list_sort(&affected_refnames
);
2761 if (ref_update_reject_duplicates(&affected_refnames
, err
)) {
2762 ret
= TRANSACTION_GENERIC_ERROR
;
2767 * It's really undefined to call this function in an active
2768 * repository or when there are existing references: we are
2769 * only locking and changing packed-refs, so (1) any
2770 * simultaneous processes might try to change a reference at
2771 * the same time we do, and (2) any existing loose versions of
2772 * the references that we are setting would have precedence
2773 * over our values. But some remote helpers create the remote
2774 * "HEAD" and "master" branches before calling this function,
2775 * so here we really only check that none of the references
2776 * that we are creating already exists.
2778 if (refs_for_each_rawref(&refs
->base
, ref_present
,
2779 &affected_refnames
))
2780 die("BUG: initial ref transaction called with existing refs");
2782 packed_transaction
= ref_store_transaction_begin(refs
->packed_ref_store
, err
);
2783 if (!packed_transaction
) {
2784 ret
= TRANSACTION_GENERIC_ERROR
;
2788 for (i
= 0; i
< transaction
->nr
; i
++) {
2789 struct ref_update
*update
= transaction
->updates
[i
];
2791 if ((update
->flags
& REF_HAVE_OLD
) &&
2792 !is_null_oid(&update
->old_oid
))
2793 die("BUG: initial ref transaction with old_sha1 set");
2794 if (refs_verify_refname_available(&refs
->base
, update
->refname
,
2795 &affected_refnames
, NULL
,
2797 ret
= TRANSACTION_NAME_CONFLICT
;
2802 * Add a reference creation for this reference to the
2803 * packed-refs transaction:
2805 ref_transaction_add_update(packed_transaction
, update
->refname
,
2806 update
->flags
& ~REF_HAVE_OLD
,
2807 update
->new_oid
.hash
, update
->old_oid
.hash
,
2811 if (packed_refs_lock(refs
->packed_ref_store
, 0, err
)) {
2812 ret
= TRANSACTION_GENERIC_ERROR
;
2816 if (initial_ref_transaction_commit(packed_transaction
, err
)) {
2817 ret
= TRANSACTION_GENERIC_ERROR
;
2822 if (packed_transaction
)
2823 ref_transaction_free(packed_transaction
);
2824 packed_refs_unlock(refs
->packed_ref_store
);
2825 transaction
->state
= REF_TRANSACTION_CLOSED
;
2826 string_list_clear(&affected_refnames
, 0);
2830 struct expire_reflog_cb
{
2832 reflog_expiry_should_prune_fn
*should_prune_fn
;
2835 struct object_id last_kept_oid
;
2838 static int expire_reflog_ent(struct object_id
*ooid
, struct object_id
*noid
,
2839 const char *email
, timestamp_t timestamp
, int tz
,
2840 const char *message
, void *cb_data
)
2842 struct expire_reflog_cb
*cb
= cb_data
;
2843 struct expire_reflog_policy_cb
*policy_cb
= cb
->policy_cb
;
2845 if (cb
->flags
& EXPIRE_REFLOGS_REWRITE
)
2846 ooid
= &cb
->last_kept_oid
;
2848 if ((*cb
->should_prune_fn
)(ooid
, noid
, email
, timestamp
, tz
,
2849 message
, policy_cb
)) {
2851 printf("would prune %s", message
);
2852 else if (cb
->flags
& EXPIRE_REFLOGS_VERBOSE
)
2853 printf("prune %s", message
);
2856 fprintf(cb
->newlog
, "%s %s %s %"PRItime
" %+05d\t%s",
2857 oid_to_hex(ooid
), oid_to_hex(noid
),
2858 email
, timestamp
, tz
, message
);
2859 oidcpy(&cb
->last_kept_oid
, noid
);
2861 if (cb
->flags
& EXPIRE_REFLOGS_VERBOSE
)
2862 printf("keep %s", message
);
2867 static int files_reflog_expire(struct ref_store
*ref_store
,
2868 const char *refname
, const unsigned char *sha1
,
2870 reflog_expiry_prepare_fn prepare_fn
,
2871 reflog_expiry_should_prune_fn should_prune_fn
,
2872 reflog_expiry_cleanup_fn cleanup_fn
,
2873 void *policy_cb_data
)
2875 struct files_ref_store
*refs
=
2876 files_downcast(ref_store
, REF_STORE_WRITE
, "reflog_expire");
2877 static struct lock_file reflog_lock
;
2878 struct expire_reflog_cb cb
;
2879 struct ref_lock
*lock
;
2880 struct strbuf log_file_sb
= STRBUF_INIT
;
2884 struct strbuf err
= STRBUF_INIT
;
2885 struct object_id oid
;
2887 memset(&cb
, 0, sizeof(cb
));
2889 cb
.policy_cb
= policy_cb_data
;
2890 cb
.should_prune_fn
= should_prune_fn
;
2893 * The reflog file is locked by holding the lock on the
2894 * reference itself, plus we might need to update the
2895 * reference if --updateref was specified:
2897 lock
= lock_ref_sha1_basic(refs
, refname
, sha1
,
2898 NULL
, NULL
, REF_NODEREF
,
2901 error("cannot lock ref '%s': %s", refname
, err
.buf
);
2902 strbuf_release(&err
);
2905 if (!refs_reflog_exists(ref_store
, refname
)) {
2910 files_reflog_path(refs
, &log_file_sb
, refname
);
2911 log_file
= strbuf_detach(&log_file_sb
, NULL
);
2912 if (!(flags
& EXPIRE_REFLOGS_DRY_RUN
)) {
2914 * Even though holding $GIT_DIR/logs/$reflog.lock has
2915 * no locking implications, we use the lock_file
2916 * machinery here anyway because it does a lot of the
2917 * work we need, including cleaning up if the program
2918 * exits unexpectedly.
2920 if (hold_lock_file_for_update(&reflog_lock
, log_file
, 0) < 0) {
2921 struct strbuf err
= STRBUF_INIT
;
2922 unable_to_lock_message(log_file
, errno
, &err
);
2923 error("%s", err
.buf
);
2924 strbuf_release(&err
);
2927 cb
.newlog
= fdopen_lock_file(&reflog_lock
, "w");
2929 error("cannot fdopen %s (%s)",
2930 get_lock_file_path(&reflog_lock
), strerror(errno
));
2935 hashcpy(oid
.hash
, sha1
);
2937 (*prepare_fn
)(refname
, &oid
, cb
.policy_cb
);
2938 refs_for_each_reflog_ent(ref_store
, refname
, expire_reflog_ent
, &cb
);
2939 (*cleanup_fn
)(cb
.policy_cb
);
2941 if (!(flags
& EXPIRE_REFLOGS_DRY_RUN
)) {
2943 * It doesn't make sense to adjust a reference pointed
2944 * to by a symbolic ref based on expiring entries in
2945 * the symbolic reference's reflog. Nor can we update
2946 * a reference if there are no remaining reflog
2949 int update
= (flags
& EXPIRE_REFLOGS_UPDATE_REF
) &&
2950 !(type
& REF_ISSYMREF
) &&
2951 !is_null_oid(&cb
.last_kept_oid
);
2953 if (close_lock_file(&reflog_lock
)) {
2954 status
|= error("couldn't write %s: %s", log_file
,
2956 } else if (update
&&
2957 (write_in_full(get_lock_file_fd(lock
->lk
),
2958 oid_to_hex(&cb
.last_kept_oid
), GIT_SHA1_HEXSZ
) != GIT_SHA1_HEXSZ
||
2959 write_str_in_full(get_lock_file_fd(lock
->lk
), "\n") != 1 ||
2960 close_ref(lock
) < 0)) {
2961 status
|= error("couldn't write %s",
2962 get_lock_file_path(lock
->lk
));
2963 rollback_lock_file(&reflog_lock
);
2964 } else if (commit_lock_file(&reflog_lock
)) {
2965 status
|= error("unable to write reflog '%s' (%s)",
2966 log_file
, strerror(errno
));
2967 } else if (update
&& commit_ref(lock
)) {
2968 status
|= error("couldn't set %s", lock
->ref_name
);
2976 rollback_lock_file(&reflog_lock
);
2982 static int files_init_db(struct ref_store
*ref_store
, struct strbuf
*err
)
2984 struct files_ref_store
*refs
=
2985 files_downcast(ref_store
, REF_STORE_WRITE
, "init_db");
2986 struct strbuf sb
= STRBUF_INIT
;
2989 * Create .git/refs/{heads,tags}
2991 files_ref_path(refs
, &sb
, "refs/heads");
2992 safe_create_dir(sb
.buf
, 1);
2995 files_ref_path(refs
, &sb
, "refs/tags");
2996 safe_create_dir(sb
.buf
, 1);
2998 strbuf_release(&sb
);
3002 struct ref_storage_be refs_be_files
= {
3005 files_ref_store_create
,
3007 files_transaction_prepare
,
3008 files_transaction_finish
,
3009 files_transaction_abort
,
3010 files_initial_transaction_commit
,
3014 files_create_symref
,
3018 files_ref_iterator_begin
,
3021 files_reflog_iterator_begin
,
3022 files_for_each_reflog_ent
,
3023 files_for_each_reflog_ent_reverse
,
3024 files_reflog_exists
,
3025 files_create_reflog
,
3026 files_delete_reflog
,