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"
12 #include "../chdir-notify.h"
16 * This backend uses the following flags in `ref_update::flags` for
17 * internal bookkeeping purposes. Their numerical values must not
18 * conflict with REF_NO_DEREF, REF_FORCE_CREATE_REFLOG, REF_HAVE_NEW,
19 * REF_HAVE_OLD, or REF_IS_PRUNING, which are also stored in
20 * `ref_update::flags`.
24 * Used as a flag in ref_update::flags when a loose ref is being
25 * pruned. This flag must only be used when REF_NO_DEREF is set.
27 #define REF_IS_PRUNING (1 << 4)
30 * Flag passed to lock_ref_sha1_basic() telling it to tolerate broken
31 * refs (i.e., because the reference is about to be deleted anyway).
33 #define REF_DELETING (1 << 5)
36 * Used as a flag in ref_update::flags when the lockfile needs to be
39 #define REF_NEEDS_COMMIT (1 << 6)
42 * Used as a flag in ref_update::flags when we want to log a ref
43 * update but not actually perform it. This is used when a symbolic
44 * ref update is split up.
46 #define REF_LOG_ONLY (1 << 7)
49 * Used as a flag in ref_update::flags when the ref_update was via an
52 #define REF_UPDATE_VIA_HEAD (1 << 8)
55 * Used as a flag in ref_update::flags when the loose reference has
58 #define REF_DELETED_LOOSE (1 << 9)
63 struct object_id old_oid;
66 struct files_ref_store {
67 struct ref_store base;
68 unsigned int store_flags;
73 struct ref_cache *loose;
75 struct ref_store *packed_ref_store;
78 static void clear_loose_ref_cache(struct files_ref_store *refs)
81 free_ref_cache(refs->loose);
87 * Create a new submodule ref cache and add it to the internal
90 static struct ref_store *files_ref_store_create(const char *gitdir,
93 struct files_ref_store *refs = xcalloc(1, sizeof(*refs));
94 struct ref_store *ref_store = (struct ref_store *)refs;
95 struct strbuf sb = STRBUF_INIT;
97 base_ref_store_init(ref_store, &refs_be_files);
98 refs->store_flags = flags;
100 refs->gitdir = xstrdup(gitdir);
101 get_common_dir_noenv(&sb, gitdir);
102 refs->gitcommondir = strbuf_detach(&sb, NULL);
103 strbuf_addf(&sb, "%s/packed-refs", refs->gitcommondir);
104 refs->packed_ref_store = packed_ref_store_create(sb.buf, flags);
107 chdir_notify_reparent("files-backend $GIT_DIR",
109 chdir_notify_reparent("files-backend $GIT_COMMONDIR",
110 &refs->gitcommondir);
116 * Die if refs is not the main ref store. caller is used in any
117 * necessary error messages.
119 static void files_assert_main_repository(struct files_ref_store *refs,
122 if (refs->store_flags & REF_STORE_MAIN)
125 BUG("operation %s only allowed for main ref store", caller);
129 * Downcast ref_store to files_ref_store. Die if ref_store is not a
130 * files_ref_store. required_flags is compared with ref_store's
131 * store_flags to ensure the ref_store has all required capabilities.
132 * "caller" is used in any necessary error messages.
134 static struct files_ref_store *files_downcast(struct ref_store *ref_store,
135 unsigned int required_flags,
138 struct files_ref_store *refs;
140 if (ref_store->be != &refs_be_files)
141 BUG("ref_store is type \"%s\" not \"files\" in %s",
142 ref_store->be->name, caller);
144 refs = (struct files_ref_store *)ref_store;
146 if ((refs->store_flags & required_flags) != required_flags)
147 BUG("operation %s requires abilities 0x%x, but only have 0x%x",
148 caller, required_flags, refs->store_flags);
153 static void files_reflog_path_other_worktrees(struct files_ref_store *refs,
157 const char *real_ref;
158 const char *worktree_name;
161 if (parse_worktree_ref(refname, &worktree_name, &length, &real_ref))
162 BUG("refname %s is not a other-worktree ref", refname);
165 strbuf_addf(sb, "%s/worktrees/%.*s/logs/%s", refs->gitcommondir,
166 length, worktree_name, real_ref);
168 strbuf_addf(sb, "%s/logs/%s", refs->gitcommondir,
172 static void files_reflog_path(struct files_ref_store *refs,
176 switch (ref_type(refname)) {
177 case REF_TYPE_PER_WORKTREE:
178 case REF_TYPE_PSEUDOREF:
179 strbuf_addf(sb, "%s/logs/%s", refs->gitdir, refname);
181 case REF_TYPE_OTHER_PSEUDOREF:
182 case REF_TYPE_MAIN_PSEUDOREF:
183 return files_reflog_path_other_worktrees(refs, sb, refname);
184 case REF_TYPE_NORMAL:
185 strbuf_addf(sb, "%s/logs/%s", refs->gitcommondir, refname);
188 BUG("unknown ref type %d of ref %s",
189 ref_type(refname), refname);
193 static void files_ref_path(struct files_ref_store *refs,
197 switch (ref_type(refname)) {
198 case REF_TYPE_PER_WORKTREE:
199 case REF_TYPE_PSEUDOREF:
200 strbuf_addf(sb, "%s/%s", refs->gitdir, refname);
202 case REF_TYPE_MAIN_PSEUDOREF:
203 if (!skip_prefix(refname, "main-worktree/", &refname))
204 BUG("ref %s is not a main pseudoref", refname);
206 case REF_TYPE_OTHER_PSEUDOREF:
207 case REF_TYPE_NORMAL:
208 strbuf_addf(sb, "%s/%s", refs->gitcommondir, refname);
211 BUG("unknown ref type %d of ref %s",
212 ref_type(refname), refname);
217 * Read the loose references from the namespace dirname into dir
218 * (without recursing). dirname must end with '/'. dir must be the
219 * directory entry corresponding to dirname.
221 static void loose_fill_ref_dir(struct ref_store *ref_store,
222 struct ref_dir *dir, const char *dirname)
224 struct files_ref_store *refs =
225 files_downcast(ref_store, REF_STORE_READ, "fill_ref_dir");
228 int dirnamelen = strlen(dirname);
229 struct strbuf refname;
230 struct strbuf path = STRBUF_INIT;
233 files_ref_path(refs, &path, dirname);
234 path_baselen = path.len;
236 d = opendir(path.buf);
238 strbuf_release(&path);
242 strbuf_init(&refname, dirnamelen + 257);
243 strbuf_add(&refname, dirname, dirnamelen);
245 while ((de = readdir(d)) != NULL) {
246 struct object_id oid;
250 if (de->d_name[0] == '.')
252 if (ends_with(de->d_name, ".lock"))
254 strbuf_addstr(&refname, de->d_name);
255 strbuf_addstr(&path, de->d_name);
256 if (stat(path.buf, &st) < 0) {
257 ; /* silently ignore */
258 } else if (S_ISDIR(st.st_mode)) {
259 strbuf_addch(&refname, '/');
260 add_entry_to_dir(dir,
261 create_dir_entry(dir->cache, refname.buf,
264 if (!refs_resolve_ref_unsafe(&refs->base,
269 flag |= REF_ISBROKEN;
270 } else if (is_null_oid(&oid)) {
272 * It is so astronomically unlikely
273 * that null_oid is the OID of an
274 * actual object that we consider its
275 * appearance in a loose reference
276 * file to be repo corruption
277 * (probably due to a software bug).
279 flag |= REF_ISBROKEN;
282 if (check_refname_format(refname.buf,
283 REFNAME_ALLOW_ONELEVEL)) {
284 if (!refname_is_safe(refname.buf))
285 die("loose refname is dangerous: %s", refname.buf);
287 flag |= REF_BAD_NAME | REF_ISBROKEN;
289 add_entry_to_dir(dir,
290 create_ref_entry(refname.buf, &oid, flag));
292 strbuf_setlen(&refname, dirnamelen);
293 strbuf_setlen(&path, path_baselen);
295 strbuf_release(&refname);
296 strbuf_release(&path);
300 * Manually add refs/bisect and refs/worktree, which, being
301 * per-worktree, might not appear in the directory listing for
302 * refs/ in the main repo.
304 if (!strcmp(dirname, "refs/")) {
305 int pos = search_ref_dir(dir, "refs/bisect/", 12);
308 struct ref_entry *child_entry = create_dir_entry(
309 dir->cache, "refs/bisect/", 12, 1);
310 add_entry_to_dir(dir, child_entry);
313 pos = search_ref_dir(dir, "refs/worktree/", 11);
316 struct ref_entry *child_entry = create_dir_entry(
317 dir->cache, "refs/worktree/", 11, 1);
318 add_entry_to_dir(dir, child_entry);
323 static struct ref_cache *get_loose_ref_cache(struct files_ref_store *refs)
327 * Mark the top-level directory complete because we
328 * are about to read the only subdirectory that can
331 refs->loose = create_ref_cache(&refs->base, loose_fill_ref_dir);
333 /* We're going to fill the top level ourselves: */
334 refs->loose->root->flag &= ~REF_INCOMPLETE;
337 * Add an incomplete entry for "refs/" (to be filled
340 add_entry_to_dir(get_ref_dir(refs->loose->root),
341 create_dir_entry(refs->loose, "refs/", 5, 1));
346 static int files_read_raw_ref(struct ref_store *ref_store,
347 const char *refname, struct object_id *oid,
348 struct strbuf *referent, unsigned int *type)
350 struct files_ref_store *refs =
351 files_downcast(ref_store, REF_STORE_READ, "read_raw_ref");
352 struct strbuf sb_contents = STRBUF_INIT;
353 struct strbuf sb_path = STRBUF_INIT;
361 int remaining_retries = 3;
364 strbuf_reset(&sb_path);
366 files_ref_path(refs, &sb_path, refname);
372 * We might have to loop back here to avoid a race
373 * condition: first we lstat() the file, then we try
374 * to read it as a link or as a file. But if somebody
375 * changes the type of the file (file <-> directory
376 * <-> symlink) between the lstat() and reading, then
377 * we don't want to report that as an error but rather
378 * try again starting with the lstat().
380 * We'll keep a count of the retries, though, just to avoid
381 * any confusing situation sending us into an infinite loop.
384 if (remaining_retries-- <= 0)
387 if (lstat(path, &st) < 0) {
390 if (refs_read_raw_ref(refs->packed_ref_store, refname,
391 oid, referent, type)) {
399 /* Follow "normalized" - ie "refs/.." symlinks by hand */
400 if (S_ISLNK(st.st_mode)) {
401 strbuf_reset(&sb_contents);
402 if (strbuf_readlink(&sb_contents, path, st.st_size) < 0) {
403 if (errno == ENOENT || errno == EINVAL)
404 /* inconsistent with lstat; retry */
409 if (starts_with(sb_contents.buf, "refs/") &&
410 !check_refname_format(sb_contents.buf, 0)) {
411 strbuf_swap(&sb_contents, referent);
412 *type |= REF_ISSYMREF;
417 * It doesn't look like a refname; fall through to just
418 * treating it like a non-symlink, and reading whatever it
423 /* Is it a directory? */
424 if (S_ISDIR(st.st_mode)) {
426 * Even though there is a directory where the loose
427 * ref is supposed to be, there could still be a
430 if (refs_read_raw_ref(refs->packed_ref_store, refname,
431 oid, referent, type)) {
440 * Anything else, just open it and try to use it as
443 fd = open(path, O_RDONLY);
445 if (errno == ENOENT && !S_ISLNK(st.st_mode))
446 /* inconsistent with lstat; retry */
451 strbuf_reset(&sb_contents);
452 if (strbuf_read(&sb_contents, fd, 256) < 0) {
453 int save_errno = errno;
459 strbuf_rtrim(&sb_contents);
460 buf = sb_contents.buf;
461 if (starts_with(buf, "ref:")) {
463 while (isspace(*buf))
466 strbuf_reset(referent);
467 strbuf_addstr(referent, buf);
468 *type |= REF_ISSYMREF;
474 * Please note that FETCH_HEAD has additional
475 * data after the sha.
477 if (parse_oid_hex(buf, oid, &p) ||
478 (*p != '\0' && !isspace(*p))) {
479 *type |= REF_ISBROKEN;
488 strbuf_release(&sb_path);
489 strbuf_release(&sb_contents);
494 static void unlock_ref(struct ref_lock *lock)
496 rollback_lock_file(&lock->lk);
497 free(lock->ref_name);
502 * Lock refname, without following symrefs, and set *lock_p to point
503 * at a newly-allocated lock object. Fill in lock->old_oid, referent,
504 * and type similarly to read_raw_ref().
506 * The caller must verify that refname is a "safe" reference name (in
507 * the sense of refname_is_safe()) before calling this function.
509 * If the reference doesn't already exist, verify that refname doesn't
510 * have a D/F conflict with any existing references. extras and skip
511 * are passed to refs_verify_refname_available() for this check.
513 * If mustexist is not set and the reference is not found or is
514 * broken, lock the reference anyway but clear old_oid.
516 * Return 0 on success. On failure, write an error message to err and
517 * return TRANSACTION_NAME_CONFLICT or TRANSACTION_GENERIC_ERROR.
519 * Implementation note: This function is basically
524 * but it includes a lot more code to
525 * - Deal with possible races with other processes
526 * - Avoid calling refs_verify_refname_available() when it can be
527 * avoided, namely if we were successfully able to read the ref
528 * - Generate informative error messages in the case of failure
530 static int lock_raw_ref(struct files_ref_store *refs,
531 const char *refname, int mustexist,
532 const struct string_list *extras,
533 const struct string_list *skip,
534 struct ref_lock **lock_p,
535 struct strbuf *referent,
539 struct ref_lock *lock;
540 struct strbuf ref_file = STRBUF_INIT;
541 int attempts_remaining = 3;
542 int ret = TRANSACTION_GENERIC_ERROR;
545 files_assert_main_repository(refs, "lock_raw_ref");
549 /* First lock the file so it can't change out from under us. */
551 *lock_p = lock = xcalloc(1, sizeof(*lock));
553 lock->ref_name = xstrdup(refname);
554 files_ref_path(refs, &ref_file, refname);
557 switch (safe_create_leading_directories(ref_file.buf)) {
562 * Suppose refname is "refs/foo/bar". We just failed
563 * to create the containing directory, "refs/foo",
564 * because there was a non-directory in the way. This
565 * indicates a D/F conflict, probably because of
566 * another reference such as "refs/foo". There is no
567 * reason to expect this error to be transitory.
569 if (refs_verify_refname_available(&refs->base, refname,
570 extras, skip, err)) {
573 * To the user the relevant error is
574 * that the "mustexist" reference is
578 strbuf_addf(err, "unable to resolve reference '%s'",
582 * The error message set by
583 * refs_verify_refname_available() is
586 ret = TRANSACTION_NAME_CONFLICT;
590 * The file that is in the way isn't a loose
591 * reference. Report it as a low-level
594 strbuf_addf(err, "unable to create lock file %s.lock; "
595 "non-directory in the way",
600 /* Maybe another process was tidying up. Try again. */
601 if (--attempts_remaining > 0)
605 strbuf_addf(err, "unable to create directory for %s",
610 if (hold_lock_file_for_update_timeout(
611 &lock->lk, ref_file.buf, LOCK_NO_DEREF,
612 get_files_ref_lock_timeout_ms()) < 0) {
613 if (errno == ENOENT && --attempts_remaining > 0) {
615 * Maybe somebody just deleted one of the
616 * directories leading to ref_file. Try
621 unable_to_lock_message(ref_file.buf, errno, err);
627 * Now we hold the lock and can read the reference without
628 * fear that its value will change.
631 if (files_read_raw_ref(&refs->base, refname,
632 &lock->old_oid, referent, type)) {
633 if (errno == ENOENT) {
635 /* Garden variety missing reference. */
636 strbuf_addf(err, "unable to resolve reference '%s'",
641 * Reference is missing, but that's OK. We
642 * know that there is not a conflict with
643 * another loose reference because
644 * (supposing that we are trying to lock
645 * reference "refs/foo/bar"):
647 * - We were successfully able to create
648 * the lockfile refs/foo/bar.lock, so we
649 * know there cannot be a loose reference
652 * - We got ENOENT and not EISDIR, so we
653 * know that there cannot be a loose
654 * reference named "refs/foo/bar/baz".
657 } else if (errno == EISDIR) {
659 * There is a directory in the way. It might have
660 * contained references that have been deleted. If
661 * we don't require that the reference already
662 * exists, try to remove the directory so that it
663 * doesn't cause trouble when we want to rename the
664 * lockfile into place later.
667 /* Garden variety missing reference. */
668 strbuf_addf(err, "unable to resolve reference '%s'",
671 } else if (remove_dir_recursively(&ref_file,
672 REMOVE_DIR_EMPTY_ONLY)) {
673 if (refs_verify_refname_available(
674 &refs->base, refname,
675 extras, skip, err)) {
677 * The error message set by
678 * verify_refname_available() is OK.
680 ret = TRANSACTION_NAME_CONFLICT;
684 * We can't delete the directory,
685 * but we also don't know of any
686 * references that it should
689 strbuf_addf(err, "there is a non-empty directory '%s' "
690 "blocking reference '%s'",
691 ref_file.buf, refname);
695 } else if (errno == EINVAL && (*type & REF_ISBROKEN)) {
696 strbuf_addf(err, "unable to resolve reference '%s': "
697 "reference broken", refname);
700 strbuf_addf(err, "unable to resolve reference '%s': %s",
701 refname, strerror(errno));
706 * If the ref did not exist and we are creating it,
707 * make sure there is no existing packed ref that
708 * conflicts with refname:
710 if (refs_verify_refname_available(
711 refs->packed_ref_store, refname,
724 strbuf_release(&ref_file);
728 struct files_ref_iterator {
729 struct ref_iterator base;
731 struct ref_iterator *iter0;
735 static int files_ref_iterator_advance(struct ref_iterator *ref_iterator)
737 struct files_ref_iterator *iter =
738 (struct files_ref_iterator *)ref_iterator;
741 while ((ok = ref_iterator_advance(iter->iter0)) == ITER_OK) {
742 if (iter->flags & DO_FOR_EACH_PER_WORKTREE_ONLY &&
743 ref_type(iter->iter0->refname) != REF_TYPE_PER_WORKTREE)
746 if (!(iter->flags & DO_FOR_EACH_INCLUDE_BROKEN) &&
747 !ref_resolves_to_object(iter->iter0->refname,
752 iter->base.refname = iter->iter0->refname;
753 iter->base.oid = iter->iter0->oid;
754 iter->base.flags = iter->iter0->flags;
759 if (ref_iterator_abort(ref_iterator) != ITER_DONE)
765 static int files_ref_iterator_peel(struct ref_iterator *ref_iterator,
766 struct object_id *peeled)
768 struct files_ref_iterator *iter =
769 (struct files_ref_iterator *)ref_iterator;
771 return ref_iterator_peel(iter->iter0, peeled);
774 static int files_ref_iterator_abort(struct ref_iterator *ref_iterator)
776 struct files_ref_iterator *iter =
777 (struct files_ref_iterator *)ref_iterator;
781 ok = ref_iterator_abort(iter->iter0);
783 base_ref_iterator_free(ref_iterator);
787 static struct ref_iterator_vtable files_ref_iterator_vtable = {
788 files_ref_iterator_advance,
789 files_ref_iterator_peel,
790 files_ref_iterator_abort
793 static struct ref_iterator *files_ref_iterator_begin(
794 struct ref_store *ref_store,
795 const char *prefix, unsigned int flags)
797 struct files_ref_store *refs;
798 struct ref_iterator *loose_iter, *packed_iter, *overlay_iter;
799 struct files_ref_iterator *iter;
800 struct ref_iterator *ref_iterator;
801 unsigned int required_flags = REF_STORE_READ;
803 if (!(flags & DO_FOR_EACH_INCLUDE_BROKEN))
804 required_flags |= REF_STORE_ODB;
806 refs = files_downcast(ref_store, required_flags, "ref_iterator_begin");
809 * We must make sure that all loose refs are read before
810 * accessing the packed-refs file; this avoids a race
811 * condition if loose refs are migrated to the packed-refs
812 * file by a simultaneous process, but our in-memory view is
813 * from before the migration. We ensure this as follows:
814 * First, we call start the loose refs iteration with its
815 * `prime_ref` argument set to true. This causes the loose
816 * references in the subtree to be pre-read into the cache.
817 * (If they've already been read, that's OK; we only need to
818 * guarantee that they're read before the packed refs, not
819 * *how much* before.) After that, we call
820 * packed_ref_iterator_begin(), which internally checks
821 * whether the packed-ref cache is up to date with what is on
822 * disk, and re-reads it if not.
825 loose_iter = cache_ref_iterator_begin(get_loose_ref_cache(refs),
829 * The packed-refs file might contain broken references, for
830 * example an old version of a reference that points at an
831 * object that has since been garbage-collected. This is OK as
832 * long as there is a corresponding loose reference that
833 * overrides it, and we don't want to emit an error message in
834 * this case. So ask the packed_ref_store for all of its
835 * references, and (if needed) do our own check for broken
836 * ones in files_ref_iterator_advance(), after we have merged
837 * the packed and loose references.
839 packed_iter = refs_ref_iterator_begin(
840 refs->packed_ref_store, prefix, 0,
841 DO_FOR_EACH_INCLUDE_BROKEN);
843 overlay_iter = overlay_ref_iterator_begin(loose_iter, packed_iter);
845 iter = xcalloc(1, sizeof(*iter));
846 ref_iterator = &iter->base;
847 base_ref_iterator_init(ref_iterator, &files_ref_iterator_vtable,
848 overlay_iter->ordered);
849 iter->iter0 = overlay_iter;
856 * Verify that the reference locked by lock has the value old_oid
857 * (unless it is NULL). Fail if the reference doesn't exist and
858 * mustexist is set. Return 0 on success. On error, write an error
859 * message to err, set errno, and return a negative value.
861 static int verify_lock(struct ref_store *ref_store, struct ref_lock *lock,
862 const struct object_id *old_oid, int mustexist,
867 if (refs_read_ref_full(ref_store, lock->ref_name,
868 mustexist ? RESOLVE_REF_READING : 0,
869 &lock->old_oid, NULL)) {
871 int save_errno = errno;
872 strbuf_addf(err, "can't verify ref '%s'", lock->ref_name);
876 oidclr(&lock->old_oid);
880 if (old_oid && !oideq(&lock->old_oid, old_oid)) {
881 strbuf_addf(err, "ref '%s' is at %s but expected %s",
883 oid_to_hex(&lock->old_oid),
884 oid_to_hex(old_oid));
891 static int remove_empty_directories(struct strbuf *path)
894 * we want to create a file but there is a directory there;
895 * if that is an empty directory (or a directory that contains
896 * only empty directories), remove them.
898 return remove_dir_recursively(path, REMOVE_DIR_EMPTY_ONLY);
901 static int create_reflock(const char *path, void *cb)
903 struct lock_file *lk = cb;
905 return hold_lock_file_for_update_timeout(
906 lk, path, LOCK_NO_DEREF,
907 get_files_ref_lock_timeout_ms()) < 0 ? -1 : 0;
911 * Locks a ref returning the lock on success and NULL on failure.
912 * On failure errno is set to something meaningful.
914 static struct ref_lock *lock_ref_oid_basic(struct files_ref_store *refs,
916 const struct object_id *old_oid,
917 const struct string_list *extras,
918 const struct string_list *skip,
919 unsigned int flags, int *type,
922 struct strbuf ref_file = STRBUF_INIT;
923 struct ref_lock *lock;
925 int mustexist = (old_oid && !is_null_oid(old_oid));
926 int resolve_flags = RESOLVE_REF_NO_RECURSE;
929 files_assert_main_repository(refs, "lock_ref_oid_basic");
932 lock = xcalloc(1, sizeof(struct ref_lock));
935 resolve_flags |= RESOLVE_REF_READING;
936 if (flags & REF_DELETING)
937 resolve_flags |= RESOLVE_REF_ALLOW_BAD_NAME;
939 files_ref_path(refs, &ref_file, refname);
940 resolved = !!refs_resolve_ref_unsafe(&refs->base,
941 refname, resolve_flags,
942 &lock->old_oid, type);
943 if (!resolved && errno == EISDIR) {
945 * we are trying to lock foo but we used to
946 * have foo/bar which now does not exist;
947 * it is normal for the empty directory 'foo'
950 if (remove_empty_directories(&ref_file)) {
952 if (!refs_verify_refname_available(
954 refname, extras, skip, err))
955 strbuf_addf(err, "there are still refs under '%s'",
959 resolved = !!refs_resolve_ref_unsafe(&refs->base,
960 refname, resolve_flags,
961 &lock->old_oid, type);
965 if (last_errno != ENOTDIR ||
966 !refs_verify_refname_available(&refs->base, refname,
968 strbuf_addf(err, "unable to resolve reference '%s': %s",
969 refname, strerror(last_errno));
975 * If the ref did not exist and we are creating it, make sure
976 * there is no existing packed ref whose name begins with our
977 * refname, nor a packed ref whose name is a proper prefix of
980 if (is_null_oid(&lock->old_oid) &&
981 refs_verify_refname_available(refs->packed_ref_store, refname,
982 extras, skip, err)) {
983 last_errno = ENOTDIR;
987 lock->ref_name = xstrdup(refname);
989 if (raceproof_create_file(ref_file.buf, create_reflock, &lock->lk)) {
991 unable_to_lock_message(ref_file.buf, errno, err);
995 if (verify_lock(&refs->base, lock, old_oid, mustexist, err)) {
1006 strbuf_release(&ref_file);
1011 struct ref_to_prune {
1012 struct ref_to_prune *next;
1013 struct object_id oid;
1014 char name[FLEX_ARRAY];
1018 REMOVE_EMPTY_PARENTS_REF = 0x01,
1019 REMOVE_EMPTY_PARENTS_REFLOG = 0x02
1023 * Remove empty parent directories associated with the specified
1024 * reference and/or its reflog, but spare [logs/]refs/ and immediate
1025 * subdirs. flags is a combination of REMOVE_EMPTY_PARENTS_REF and/or
1026 * REMOVE_EMPTY_PARENTS_REFLOG.
1028 static void try_remove_empty_parents(struct files_ref_store *refs,
1029 const char *refname,
1032 struct strbuf buf = STRBUF_INIT;
1033 struct strbuf sb = STRBUF_INIT;
1037 strbuf_addstr(&buf, refname);
1039 for (i = 0; i < 2; i++) { /* refs/{heads,tags,...}/ */
1040 while (*p && *p != '/')
1042 /* tolerate duplicate slashes; see check_refname_format() */
1046 q = buf.buf + buf.len;
1047 while (flags & (REMOVE_EMPTY_PARENTS_REF | REMOVE_EMPTY_PARENTS_REFLOG)) {
1048 while (q > p && *q != '/')
1050 while (q > p && *(q-1) == '/')
1054 strbuf_setlen(&buf, q - buf.buf);
1057 files_ref_path(refs, &sb, buf.buf);
1058 if ((flags & REMOVE_EMPTY_PARENTS_REF) && rmdir(sb.buf))
1059 flags &= ~REMOVE_EMPTY_PARENTS_REF;
1062 files_reflog_path(refs, &sb, buf.buf);
1063 if ((flags & REMOVE_EMPTY_PARENTS_REFLOG) && rmdir(sb.buf))
1064 flags &= ~REMOVE_EMPTY_PARENTS_REFLOG;
1066 strbuf_release(&buf);
1067 strbuf_release(&sb);
1070 /* make sure nobody touched the ref, and unlink */
1071 static void prune_ref(struct files_ref_store *refs, struct ref_to_prune *r)
1073 struct ref_transaction *transaction;
1074 struct strbuf err = STRBUF_INIT;
1077 if (check_refname_format(r->name, 0))
1080 transaction = ref_store_transaction_begin(&refs->base, &err);
1083 ref_transaction_add_update(
1084 transaction, r->name,
1085 REF_NO_DEREF | REF_HAVE_NEW | REF_HAVE_OLD | REF_IS_PRUNING,
1086 &null_oid, &r->oid, NULL);
1087 if (ref_transaction_commit(transaction, &err))
1094 error("%s", err.buf);
1095 strbuf_release(&err);
1096 ref_transaction_free(transaction);
1101 * Prune the loose versions of the references in the linked list
1102 * `*refs_to_prune`, freeing the entries in the list as we go.
1104 static void prune_refs(struct files_ref_store *refs, struct ref_to_prune **refs_to_prune)
1106 while (*refs_to_prune) {
1107 struct ref_to_prune *r = *refs_to_prune;
1108 *refs_to_prune = r->next;
1115 * Return true if the specified reference should be packed.
1117 static int should_pack_ref(const char *refname,
1118 const struct object_id *oid, unsigned int ref_flags,
1119 unsigned int pack_flags)
1121 /* Do not pack per-worktree refs: */
1122 if (ref_type(refname) != REF_TYPE_NORMAL)
1125 /* Do not pack non-tags unless PACK_REFS_ALL is set: */
1126 if (!(pack_flags & PACK_REFS_ALL) && !starts_with(refname, "refs/tags/"))
1129 /* Do not pack symbolic refs: */
1130 if (ref_flags & REF_ISSYMREF)
1133 /* Do not pack broken refs: */
1134 if (!ref_resolves_to_object(refname, oid, ref_flags))
1140 static int files_pack_refs(struct ref_store *ref_store, unsigned int flags)
1142 struct files_ref_store *refs =
1143 files_downcast(ref_store, REF_STORE_WRITE | REF_STORE_ODB,
1145 struct ref_iterator *iter;
1147 struct ref_to_prune *refs_to_prune = NULL;
1148 struct strbuf err = STRBUF_INIT;
1149 struct ref_transaction *transaction;
1151 transaction = ref_store_transaction_begin(refs->packed_ref_store, &err);
1155 packed_refs_lock(refs->packed_ref_store, LOCK_DIE_ON_ERROR, &err);
1157 iter = cache_ref_iterator_begin(get_loose_ref_cache(refs), NULL, 0);
1158 while ((ok = ref_iterator_advance(iter)) == ITER_OK) {
1160 * If the loose reference can be packed, add an entry
1161 * in the packed ref cache. If the reference should be
1162 * pruned, also add it to refs_to_prune.
1164 if (!should_pack_ref(iter->refname, iter->oid, iter->flags,
1169 * Add a reference creation for this reference to the
1170 * packed-refs transaction:
1172 if (ref_transaction_update(transaction, iter->refname,
1174 REF_NO_DEREF, NULL, &err))
1175 die("failure preparing to create packed reference %s: %s",
1176 iter->refname, err.buf);
1178 /* Schedule the loose reference for pruning if requested. */
1179 if ((flags & PACK_REFS_PRUNE)) {
1180 struct ref_to_prune *n;
1181 FLEX_ALLOC_STR(n, name, iter->refname);
1182 oidcpy(&n->oid, iter->oid);
1183 n->next = refs_to_prune;
1187 if (ok != ITER_DONE)
1188 die("error while iterating over references");
1190 if (ref_transaction_commit(transaction, &err))
1191 die("unable to write new packed-refs: %s", err.buf);
1193 ref_transaction_free(transaction);
1195 packed_refs_unlock(refs->packed_ref_store);
1197 prune_refs(refs, &refs_to_prune);
1198 strbuf_release(&err);
1202 static int files_delete_refs(struct ref_store *ref_store, const char *msg,
1203 struct string_list *refnames, unsigned int flags)
1205 struct files_ref_store *refs =
1206 files_downcast(ref_store, REF_STORE_WRITE, "delete_refs");
1207 struct strbuf err = STRBUF_INIT;
1213 if (packed_refs_lock(refs->packed_ref_store, 0, &err))
1216 if (refs_delete_refs(refs->packed_ref_store, msg, refnames, flags)) {
1217 packed_refs_unlock(refs->packed_ref_store);
1221 packed_refs_unlock(refs->packed_ref_store);
1223 for (i = 0; i < refnames->nr; i++) {
1224 const char *refname = refnames->items[i].string;
1226 if (refs_delete_ref(&refs->base, msg, refname, NULL, flags))
1227 result |= error(_("could not remove reference %s"), refname);
1230 strbuf_release(&err);
1235 * If we failed to rewrite the packed-refs file, then it is
1236 * unsafe to try to remove loose refs, because doing so might
1237 * expose an obsolete packed value for a reference that might
1238 * even point at an object that has been garbage collected.
1240 if (refnames->nr == 1)
1241 error(_("could not delete reference %s: %s"),
1242 refnames->items[0].string, err.buf);
1244 error(_("could not delete references: %s"), err.buf);
1246 strbuf_release(&err);
1251 * People using contrib's git-new-workdir have .git/logs/refs ->
1252 * /some/other/path/.git/logs/refs, and that may live on another device.
1254 * IOW, to avoid cross device rename errors, the temporary renamed log must
1255 * live into logs/refs.
1257 #define TMP_RENAMED_LOG "refs/.tmp-renamed-log"
1260 const char *tmp_renamed_log;
1264 static int rename_tmp_log_callback(const char *path, void *cb_data)
1266 struct rename_cb *cb = cb_data;
1268 if (rename(cb->tmp_renamed_log, path)) {
1270 * rename(a, b) when b is an existing directory ought
1271 * to result in ISDIR, but Solaris 5.8 gives ENOTDIR.
1272 * Sheesh. Record the true errno for error reporting,
1273 * but report EISDIR to raceproof_create_file() so
1274 * that it knows to retry.
1276 cb->true_errno = errno;
1277 if (errno == ENOTDIR)
1285 static int rename_tmp_log(struct files_ref_store *refs, const char *newrefname)
1287 struct strbuf path = STRBUF_INIT;
1288 struct strbuf tmp = STRBUF_INIT;
1289 struct rename_cb cb;
1292 files_reflog_path(refs, &path, newrefname);
1293 files_reflog_path(refs, &tmp, TMP_RENAMED_LOG);
1294 cb.tmp_renamed_log = tmp.buf;
1295 ret = raceproof_create_file(path.buf, rename_tmp_log_callback, &cb);
1297 if (errno == EISDIR)
1298 error("directory not empty: %s", path.buf);
1300 error("unable to move logfile %s to %s: %s",
1302 strerror(cb.true_errno));
1305 strbuf_release(&path);
1306 strbuf_release(&tmp);
1310 static int write_ref_to_lockfile(struct ref_lock *lock,
1311 const struct object_id *oid, struct strbuf *err);
1312 static int commit_ref_update(struct files_ref_store *refs,
1313 struct ref_lock *lock,
1314 const struct object_id *oid, const char *logmsg,
1315 struct strbuf *err);
1317 static int files_copy_or_rename_ref(struct ref_store *ref_store,
1318 const char *oldrefname, const char *newrefname,
1319 const char *logmsg, int copy)
1321 struct files_ref_store *refs =
1322 files_downcast(ref_store, REF_STORE_WRITE, "rename_ref");
1323 struct object_id oid, orig_oid;
1324 int flag = 0, logmoved = 0;
1325 struct ref_lock *lock;
1326 struct stat loginfo;
1327 struct strbuf sb_oldref = STRBUF_INIT;
1328 struct strbuf sb_newref = STRBUF_INIT;
1329 struct strbuf tmp_renamed_log = STRBUF_INIT;
1331 struct strbuf err = STRBUF_INIT;
1333 files_reflog_path(refs, &sb_oldref, oldrefname);
1334 files_reflog_path(refs, &sb_newref, newrefname);
1335 files_reflog_path(refs, &tmp_renamed_log, TMP_RENAMED_LOG);
1337 log = !lstat(sb_oldref.buf, &loginfo);
1338 if (log && S_ISLNK(loginfo.st_mode)) {
1339 ret = error("reflog for %s is a symlink", oldrefname);
1343 if (!refs_resolve_ref_unsafe(&refs->base, oldrefname,
1344 RESOLVE_REF_READING | RESOLVE_REF_NO_RECURSE,
1345 &orig_oid, &flag)) {
1346 ret = error("refname %s not found", oldrefname);
1350 if (flag & REF_ISSYMREF) {
1352 ret = error("refname %s is a symbolic ref, copying it is not supported",
1355 ret = error("refname %s is a symbolic ref, renaming it is not supported",
1359 if (!refs_rename_ref_available(&refs->base, oldrefname, newrefname)) {
1364 if (!copy && log && rename(sb_oldref.buf, tmp_renamed_log.buf)) {
1365 ret = error("unable to move logfile logs/%s to logs/"TMP_RENAMED_LOG": %s",
1366 oldrefname, strerror(errno));
1370 if (copy && log && copy_file(tmp_renamed_log.buf, sb_oldref.buf, 0644)) {
1371 ret = error("unable to copy logfile logs/%s to logs/"TMP_RENAMED_LOG": %s",
1372 oldrefname, strerror(errno));
1376 if (!copy && refs_delete_ref(&refs->base, logmsg, oldrefname,
1377 &orig_oid, REF_NO_DEREF)) {
1378 error("unable to delete old %s", oldrefname);
1383 * Since we are doing a shallow lookup, oid is not the
1384 * correct value to pass to delete_ref as old_oid. But that
1385 * doesn't matter, because an old_oid check wouldn't add to
1386 * the safety anyway; we want to delete the reference whatever
1387 * its current value.
1389 if (!copy && !refs_read_ref_full(&refs->base, newrefname,
1390 RESOLVE_REF_READING | RESOLVE_REF_NO_RECURSE,
1392 refs_delete_ref(&refs->base, NULL, newrefname,
1393 NULL, REF_NO_DEREF)) {
1394 if (errno == EISDIR) {
1395 struct strbuf path = STRBUF_INIT;
1398 files_ref_path(refs, &path, newrefname);
1399 result = remove_empty_directories(&path);
1400 strbuf_release(&path);
1403 error("Directory not empty: %s", newrefname);
1407 error("unable to delete existing %s", newrefname);
1412 if (log && rename_tmp_log(refs, newrefname))
1417 lock = lock_ref_oid_basic(refs, newrefname, NULL, NULL, NULL,
1418 REF_NO_DEREF, NULL, &err);
1421 error("unable to copy '%s' to '%s': %s", oldrefname, newrefname, err.buf);
1423 error("unable to rename '%s' to '%s': %s", oldrefname, newrefname, err.buf);
1424 strbuf_release(&err);
1427 oidcpy(&lock->old_oid, &orig_oid);
1429 if (write_ref_to_lockfile(lock, &orig_oid, &err) ||
1430 commit_ref_update(refs, lock, &orig_oid, logmsg, &err)) {
1431 error("unable to write current sha1 into %s: %s", newrefname, err.buf);
1432 strbuf_release(&err);
1440 lock = lock_ref_oid_basic(refs, oldrefname, NULL, NULL, NULL,
1441 REF_NO_DEREF, NULL, &err);
1443 error("unable to lock %s for rollback: %s", oldrefname, err.buf);
1444 strbuf_release(&err);
1448 flag = log_all_ref_updates;
1449 log_all_ref_updates = LOG_REFS_NONE;
1450 if (write_ref_to_lockfile(lock, &orig_oid, &err) ||
1451 commit_ref_update(refs, lock, &orig_oid, NULL, &err)) {
1452 error("unable to write current sha1 into %s: %s", oldrefname, err.buf);
1453 strbuf_release(&err);
1455 log_all_ref_updates = flag;
1458 if (logmoved && rename(sb_newref.buf, sb_oldref.buf))
1459 error("unable to restore logfile %s from %s: %s",
1460 oldrefname, newrefname, strerror(errno));
1461 if (!logmoved && log &&
1462 rename(tmp_renamed_log.buf, sb_oldref.buf))
1463 error("unable to restore logfile %s from logs/"TMP_RENAMED_LOG": %s",
1464 oldrefname, strerror(errno));
1467 strbuf_release(&sb_newref);
1468 strbuf_release(&sb_oldref);
1469 strbuf_release(&tmp_renamed_log);
1474 static int files_rename_ref(struct ref_store *ref_store,
1475 const char *oldrefname, const char *newrefname,
1478 return files_copy_or_rename_ref(ref_store, oldrefname,
1479 newrefname, logmsg, 0);
1482 static int files_copy_ref(struct ref_store *ref_store,
1483 const char *oldrefname, const char *newrefname,
1486 return files_copy_or_rename_ref(ref_store, oldrefname,
1487 newrefname, logmsg, 1);
1490 static int close_ref_gently(struct ref_lock *lock)
1492 if (close_lock_file_gently(&lock->lk))
1497 static int commit_ref(struct ref_lock *lock)
1499 char *path = get_locked_file_path(&lock->lk);
1502 if (!lstat(path, &st) && S_ISDIR(st.st_mode)) {
1504 * There is a directory at the path we want to rename
1505 * the lockfile to. Hopefully it is empty; try to
1508 size_t len = strlen(path);
1509 struct strbuf sb_path = STRBUF_INIT;
1511 strbuf_attach(&sb_path, path, len, len);
1514 * If this fails, commit_lock_file() will also fail
1515 * and will report the problem.
1517 remove_empty_directories(&sb_path);
1518 strbuf_release(&sb_path);
1523 if (commit_lock_file(&lock->lk))
1528 static int open_or_create_logfile(const char *path, void *cb)
1532 *fd = open(path, O_APPEND | O_WRONLY | O_CREAT, 0666);
1533 return (*fd < 0) ? -1 : 0;
1537 * Create a reflog for a ref. If force_create = 0, only create the
1538 * reflog for certain refs (those for which should_autocreate_reflog
1539 * returns non-zero). Otherwise, create it regardless of the reference
1540 * name. If the logfile already existed or was created, return 0 and
1541 * set *logfd to the file descriptor opened for appending to the file.
1542 * If no logfile exists and we decided not to create one, return 0 and
1543 * set *logfd to -1. On failure, fill in *err, set *logfd to -1, and
1546 static int log_ref_setup(struct files_ref_store *refs,
1547 const char *refname, int force_create,
1548 int *logfd, struct strbuf *err)
1550 struct strbuf logfile_sb = STRBUF_INIT;
1553 files_reflog_path(refs, &logfile_sb, refname);
1554 logfile = strbuf_detach(&logfile_sb, NULL);
1556 if (force_create || should_autocreate_reflog(refname)) {
1557 if (raceproof_create_file(logfile, open_or_create_logfile, logfd)) {
1558 if (errno == ENOENT)
1559 strbuf_addf(err, "unable to create directory for '%s': "
1560 "%s", logfile, strerror(errno));
1561 else if (errno == EISDIR)
1562 strbuf_addf(err, "there are still logs under '%s'",
1565 strbuf_addf(err, "unable to append to '%s': %s",
1566 logfile, strerror(errno));
1571 *logfd = open(logfile, O_APPEND | O_WRONLY, 0666);
1573 if (errno == ENOENT || errno == EISDIR) {
1575 * The logfile doesn't already exist,
1576 * but that is not an error; it only
1577 * means that we won't write log
1582 strbuf_addf(err, "unable to append to '%s': %s",
1583 logfile, strerror(errno));
1590 adjust_shared_perm(logfile);
1600 static int files_create_reflog(struct ref_store *ref_store,
1601 const char *refname, int force_create,
1604 struct files_ref_store *refs =
1605 files_downcast(ref_store, REF_STORE_WRITE, "create_reflog");
1608 if (log_ref_setup(refs, refname, force_create, &fd, err))
1617 static int log_ref_write_fd(int fd, const struct object_id *old_oid,
1618 const struct object_id *new_oid,
1619 const char *committer, const char *msg)
1621 struct strbuf sb = STRBUF_INIT;
1624 strbuf_addf(&sb, "%s %s %s", oid_to_hex(old_oid), oid_to_hex(new_oid), committer);
1626 copy_reflog_msg(&sb, msg);
1627 strbuf_addch(&sb, '\n');
1628 if (write_in_full(fd, sb.buf, sb.len) < 0)
1630 strbuf_release(&sb);
1634 static int files_log_ref_write(struct files_ref_store *refs,
1635 const char *refname, const struct object_id *old_oid,
1636 const struct object_id *new_oid, const char *msg,
1637 int flags, struct strbuf *err)
1641 if (log_all_ref_updates == LOG_REFS_UNSET)
1642 log_all_ref_updates = is_bare_repository() ? LOG_REFS_NONE : LOG_REFS_NORMAL;
1644 result = log_ref_setup(refs, refname,
1645 flags & REF_FORCE_CREATE_REFLOG,
1653 result = log_ref_write_fd(logfd, old_oid, new_oid,
1654 git_committer_info(0), msg);
1656 struct strbuf sb = STRBUF_INIT;
1657 int save_errno = errno;
1659 files_reflog_path(refs, &sb, refname);
1660 strbuf_addf(err, "unable to append to '%s': %s",
1661 sb.buf, strerror(save_errno));
1662 strbuf_release(&sb);
1667 struct strbuf sb = STRBUF_INIT;
1668 int save_errno = errno;
1670 files_reflog_path(refs, &sb, refname);
1671 strbuf_addf(err, "unable to append to '%s': %s",
1672 sb.buf, strerror(save_errno));
1673 strbuf_release(&sb);
1680 * Write oid into the open lockfile, then close the lockfile. On
1681 * errors, rollback the lockfile, fill in *err and return -1.
1683 static int write_ref_to_lockfile(struct ref_lock *lock,
1684 const struct object_id *oid, struct strbuf *err)
1686 static char term = '\n';
1690 o = parse_object(the_repository, oid);
1693 "trying to write ref '%s' with nonexistent object %s",
1694 lock->ref_name, oid_to_hex(oid));
1698 if (o->type != OBJ_COMMIT && is_branch(lock->ref_name)) {
1700 "trying to write non-commit object %s to branch '%s'",
1701 oid_to_hex(oid), lock->ref_name);
1705 fd = get_lock_file_fd(&lock->lk);
1706 if (write_in_full(fd, oid_to_hex(oid), the_hash_algo->hexsz) < 0 ||
1707 write_in_full(fd, &term, 1) < 0 ||
1708 close_ref_gently(lock) < 0) {
1710 "couldn't write '%s'", get_lock_file_path(&lock->lk));
1718 * Commit a change to a loose reference that has already been written
1719 * to the loose reference lockfile. Also update the reflogs if
1720 * necessary, using the specified lockmsg (which can be NULL).
1722 static int commit_ref_update(struct files_ref_store *refs,
1723 struct ref_lock *lock,
1724 const struct object_id *oid, const char *logmsg,
1727 files_assert_main_repository(refs, "commit_ref_update");
1729 clear_loose_ref_cache(refs);
1730 if (files_log_ref_write(refs, lock->ref_name,
1731 &lock->old_oid, oid,
1733 char *old_msg = strbuf_detach(err, NULL);
1734 strbuf_addf(err, "cannot update the ref '%s': %s",
1735 lock->ref_name, old_msg);
1741 if (strcmp(lock->ref_name, "HEAD") != 0) {
1743 * Special hack: If a branch is updated directly and HEAD
1744 * points to it (may happen on the remote side of a push
1745 * for example) then logically the HEAD reflog should be
1747 * A generic solution implies reverse symref information,
1748 * but finding all symrefs pointing to the given branch
1749 * would be rather costly for this rare event (the direct
1750 * update of a branch) to be worth it. So let's cheat and
1751 * check with HEAD only which should cover 99% of all usage
1752 * scenarios (even 100% of the default ones).
1755 const char *head_ref;
1757 head_ref = refs_resolve_ref_unsafe(&refs->base, "HEAD",
1758 RESOLVE_REF_READING,
1760 if (head_ref && (head_flag & REF_ISSYMREF) &&
1761 !strcmp(head_ref, lock->ref_name)) {
1762 struct strbuf log_err = STRBUF_INIT;
1763 if (files_log_ref_write(refs, "HEAD",
1764 &lock->old_oid, oid,
1765 logmsg, 0, &log_err)) {
1766 error("%s", log_err.buf);
1767 strbuf_release(&log_err);
1772 if (commit_ref(lock)) {
1773 strbuf_addf(err, "couldn't set '%s'", lock->ref_name);
1782 static int create_ref_symlink(struct ref_lock *lock, const char *target)
1785 #ifndef NO_SYMLINK_HEAD
1786 char *ref_path = get_locked_file_path(&lock->lk);
1788 ret = symlink(target, ref_path);
1792 fprintf(stderr, "no symlink - falling back to symbolic ref\n");
1797 static void update_symref_reflog(struct files_ref_store *refs,
1798 struct ref_lock *lock, const char *refname,
1799 const char *target, const char *logmsg)
1801 struct strbuf err = STRBUF_INIT;
1802 struct object_id new_oid;
1804 !refs_read_ref_full(&refs->base, target,
1805 RESOLVE_REF_READING, &new_oid, NULL) &&
1806 files_log_ref_write(refs, refname, &lock->old_oid,
1807 &new_oid, logmsg, 0, &err)) {
1808 error("%s", err.buf);
1809 strbuf_release(&err);
1813 static int create_symref_locked(struct files_ref_store *refs,
1814 struct ref_lock *lock, const char *refname,
1815 const char *target, const char *logmsg)
1817 if (prefer_symlink_refs && !create_ref_symlink(lock, target)) {
1818 update_symref_reflog(refs, lock, refname, target, logmsg);
1822 if (!fdopen_lock_file(&lock->lk, "w"))
1823 return error("unable to fdopen %s: %s",
1824 lock->lk.tempfile->filename.buf, strerror(errno));
1826 update_symref_reflog(refs, lock, refname, target, logmsg);
1828 /* no error check; commit_ref will check ferror */
1829 fprintf(lock->lk.tempfile->fp, "ref: %s\n", target);
1830 if (commit_ref(lock) < 0)
1831 return error("unable to write symref for %s: %s", refname,
1836 static int files_create_symref(struct ref_store *ref_store,
1837 const char *refname, const char *target,
1840 struct files_ref_store *refs =
1841 files_downcast(ref_store, REF_STORE_WRITE, "create_symref");
1842 struct strbuf err = STRBUF_INIT;
1843 struct ref_lock *lock;
1846 lock = lock_ref_oid_basic(refs, refname, NULL,
1847 NULL, NULL, REF_NO_DEREF, NULL,
1850 error("%s", err.buf);
1851 strbuf_release(&err);
1855 ret = create_symref_locked(refs, lock, refname, target, logmsg);
1860 static int files_reflog_exists(struct ref_store *ref_store,
1861 const char *refname)
1863 struct files_ref_store *refs =
1864 files_downcast(ref_store, REF_STORE_READ, "reflog_exists");
1865 struct strbuf sb = STRBUF_INIT;
1869 files_reflog_path(refs, &sb, refname);
1870 ret = !lstat(sb.buf, &st) && S_ISREG(st.st_mode);
1871 strbuf_release(&sb);
1875 static int files_delete_reflog(struct ref_store *ref_store,
1876 const char *refname)
1878 struct files_ref_store *refs =
1879 files_downcast(ref_store, REF_STORE_WRITE, "delete_reflog");
1880 struct strbuf sb = STRBUF_INIT;
1883 files_reflog_path(refs, &sb, refname);
1884 ret = remove_path(sb.buf);
1885 strbuf_release(&sb);
1889 static int show_one_reflog_ent(struct strbuf *sb, each_reflog_ent_fn fn, void *cb_data)
1891 struct object_id ooid, noid;
1892 char *email_end, *message;
1893 timestamp_t timestamp;
1895 const char *p = sb->buf;
1897 /* old SP new SP name <email> SP time TAB msg LF */
1898 if (!sb->len || sb->buf[sb->len - 1] != '\n' ||
1899 parse_oid_hex(p, &ooid, &p) || *p++ != ' ' ||
1900 parse_oid_hex(p, &noid, &p) || *p++ != ' ' ||
1901 !(email_end = strchr(p, '>')) ||
1902 email_end[1] != ' ' ||
1903 !(timestamp = parse_timestamp(email_end + 2, &message, 10)) ||
1904 !message || message[0] != ' ' ||
1905 (message[1] != '+' && message[1] != '-') ||
1906 !isdigit(message[2]) || !isdigit(message[3]) ||
1907 !isdigit(message[4]) || !isdigit(message[5]))
1908 return 0; /* corrupt? */
1909 email_end[1] = '\0';
1910 tz = strtol(message + 1, NULL, 10);
1911 if (message[6] != '\t')
1915 return fn(&ooid, &noid, p, timestamp, tz, message, cb_data);
1918 static char *find_beginning_of_line(char *bob, char *scan)
1920 while (bob < scan && *(--scan) != '\n')
1921 ; /* keep scanning backwards */
1923 * Return either beginning of the buffer, or LF at the end of
1924 * the previous line.
1929 static int files_for_each_reflog_ent_reverse(struct ref_store *ref_store,
1930 const char *refname,
1931 each_reflog_ent_fn fn,
1934 struct files_ref_store *refs =
1935 files_downcast(ref_store, REF_STORE_READ,
1936 "for_each_reflog_ent_reverse");
1937 struct strbuf sb = STRBUF_INIT;
1940 int ret = 0, at_tail = 1;
1942 files_reflog_path(refs, &sb, refname);
1943 logfp = fopen(sb.buf, "r");
1944 strbuf_release(&sb);
1948 /* Jump to the end */
1949 if (fseek(logfp, 0, SEEK_END) < 0)
1950 ret = error("cannot seek back reflog for %s: %s",
1951 refname, strerror(errno));
1953 while (!ret && 0 < pos) {
1959 /* Fill next block from the end */
1960 cnt = (sizeof(buf) < pos) ? sizeof(buf) : pos;
1961 if (fseek(logfp, pos - cnt, SEEK_SET)) {
1962 ret = error("cannot seek back reflog for %s: %s",
1963 refname, strerror(errno));
1966 nread = fread(buf, cnt, 1, logfp);
1968 ret = error("cannot read %d bytes from reflog for %s: %s",
1969 cnt, refname, strerror(errno));
1974 scanp = endp = buf + cnt;
1975 if (at_tail && scanp[-1] == '\n')
1976 /* Looking at the final LF at the end of the file */
1980 while (buf < scanp) {
1982 * terminating LF of the previous line, or the beginning
1987 bp = find_beginning_of_line(buf, scanp);
1991 * The newline is the end of the previous line,
1992 * so we know we have complete line starting
1993 * at (bp + 1). Prefix it onto any prior data
1994 * we collected for the line and process it.
1996 strbuf_splice(&sb, 0, 0, bp + 1, endp - (bp + 1));
1999 ret = show_one_reflog_ent(&sb, fn, cb_data);
2005 * We are at the start of the buffer, and the
2006 * start of the file; there is no previous
2007 * line, and we have everything for this one.
2008 * Process it, and we can end the loop.
2010 strbuf_splice(&sb, 0, 0, buf, endp - buf);
2011 ret = show_one_reflog_ent(&sb, fn, cb_data);
2018 * We are at the start of the buffer, and there
2019 * is more file to read backwards. Which means
2020 * we are in the middle of a line. Note that we
2021 * may get here even if *bp was a newline; that
2022 * just means we are at the exact end of the
2023 * previous line, rather than some spot in the
2026 * Save away what we have to be combined with
2027 * the data from the next read.
2029 strbuf_splice(&sb, 0, 0, buf, endp - buf);
2036 BUG("reverse reflog parser had leftover data");
2039 strbuf_release(&sb);
2043 static int files_for_each_reflog_ent(struct ref_store *ref_store,
2044 const char *refname,
2045 each_reflog_ent_fn fn, void *cb_data)
2047 struct files_ref_store *refs =
2048 files_downcast(ref_store, REF_STORE_READ,
2049 "for_each_reflog_ent");
2051 struct strbuf sb = STRBUF_INIT;
2054 files_reflog_path(refs, &sb, refname);
2055 logfp = fopen(sb.buf, "r");
2056 strbuf_release(&sb);
2060 while (!ret && !strbuf_getwholeline(&sb, logfp, '\n'))
2061 ret = show_one_reflog_ent(&sb, fn, cb_data);
2063 strbuf_release(&sb);
2067 struct files_reflog_iterator {
2068 struct ref_iterator base;
2070 struct ref_store *ref_store;
2071 struct dir_iterator *dir_iterator;
2072 struct object_id oid;
2075 static int files_reflog_iterator_advance(struct ref_iterator *ref_iterator)
2077 struct files_reflog_iterator *iter =
2078 (struct files_reflog_iterator *)ref_iterator;
2079 struct dir_iterator *diter = iter->dir_iterator;
2082 while ((ok = dir_iterator_advance(diter)) == ITER_OK) {
2085 if (!S_ISREG(diter->st.st_mode))
2087 if (diter->basename[0] == '.')
2089 if (ends_with(diter->basename, ".lock"))
2092 if (refs_read_ref_full(iter->ref_store,
2093 diter->relative_path, 0,
2094 &iter->oid, &flags)) {
2095 error("bad ref for %s", diter->path.buf);
2099 iter->base.refname = diter->relative_path;
2100 iter->base.oid = &iter->oid;
2101 iter->base.flags = flags;
2105 iter->dir_iterator = NULL;
2106 if (ref_iterator_abort(ref_iterator) == ITER_ERROR)
2111 static int files_reflog_iterator_peel(struct ref_iterator *ref_iterator,
2112 struct object_id *peeled)
2114 BUG("ref_iterator_peel() called for reflog_iterator");
2117 static int files_reflog_iterator_abort(struct ref_iterator *ref_iterator)
2119 struct files_reflog_iterator *iter =
2120 (struct files_reflog_iterator *)ref_iterator;
2123 if (iter->dir_iterator)
2124 ok = dir_iterator_abort(iter->dir_iterator);
2126 base_ref_iterator_free(ref_iterator);
2130 static struct ref_iterator_vtable files_reflog_iterator_vtable = {
2131 files_reflog_iterator_advance,
2132 files_reflog_iterator_peel,
2133 files_reflog_iterator_abort
2136 static struct ref_iterator *reflog_iterator_begin(struct ref_store *ref_store,
2139 struct files_reflog_iterator *iter = xcalloc(1, sizeof(*iter));
2140 struct ref_iterator *ref_iterator = &iter->base;
2141 struct strbuf sb = STRBUF_INIT;
2143 base_ref_iterator_init(ref_iterator, &files_reflog_iterator_vtable, 0);
2144 strbuf_addf(&sb, "%s/logs", gitdir);
2145 iter->dir_iterator = dir_iterator_begin(sb.buf);
2146 iter->ref_store = ref_store;
2147 strbuf_release(&sb);
2149 return ref_iterator;
2152 static enum iterator_selection reflog_iterator_select(
2153 struct ref_iterator *iter_worktree,
2154 struct ref_iterator *iter_common,
2157 if (iter_worktree) {
2159 * We're a bit loose here. We probably should ignore
2160 * common refs if they are accidentally added as
2161 * per-worktree refs.
2163 return ITER_SELECT_0;
2164 } else if (iter_common) {
2165 if (ref_type(iter_common->refname) == REF_TYPE_NORMAL)
2166 return ITER_SELECT_1;
2169 * The main ref store may contain main worktree's
2170 * per-worktree refs, which should be ignored
2177 static struct ref_iterator *files_reflog_iterator_begin(struct ref_store *ref_store)
2179 struct files_ref_store *refs =
2180 files_downcast(ref_store, REF_STORE_READ,
2181 "reflog_iterator_begin");
2183 if (!strcmp(refs->gitdir, refs->gitcommondir)) {
2184 return reflog_iterator_begin(ref_store, refs->gitcommondir);
2186 return merge_ref_iterator_begin(
2188 reflog_iterator_begin(ref_store, refs->gitdir),
2189 reflog_iterator_begin(ref_store, refs->gitcommondir),
2190 reflog_iterator_select, refs);
2195 * If update is a direct update of head_ref (the reference pointed to
2196 * by HEAD), then add an extra REF_LOG_ONLY update for HEAD.
2198 static int split_head_update(struct ref_update *update,
2199 struct ref_transaction *transaction,
2200 const char *head_ref,
2201 struct string_list *affected_refnames,
2204 struct string_list_item *item;
2205 struct ref_update *new_update;
2207 if ((update->flags & REF_LOG_ONLY) ||
2208 (update->flags & REF_IS_PRUNING) ||
2209 (update->flags & REF_UPDATE_VIA_HEAD))
2212 if (strcmp(update->refname, head_ref))
2216 * First make sure that HEAD is not already in the
2217 * transaction. This check is O(lg N) in the transaction
2218 * size, but it happens at most once per transaction.
2220 if (string_list_has_string(affected_refnames, "HEAD")) {
2221 /* An entry already existed */
2223 "multiple updates for 'HEAD' (including one "
2224 "via its referent '%s') are not allowed",
2226 return TRANSACTION_NAME_CONFLICT;
2229 new_update = ref_transaction_add_update(
2230 transaction, "HEAD",
2231 update->flags | REF_LOG_ONLY | REF_NO_DEREF,
2232 &update->new_oid, &update->old_oid,
2236 * Add "HEAD". This insertion is O(N) in the transaction
2237 * size, but it happens at most once per transaction.
2238 * Add new_update->refname instead of a literal "HEAD".
2240 if (strcmp(new_update->refname, "HEAD"))
2241 BUG("%s unexpectedly not 'HEAD'", new_update->refname);
2242 item = string_list_insert(affected_refnames, new_update->refname);
2243 item->util = new_update;
2249 * update is for a symref that points at referent and doesn't have
2250 * REF_NO_DEREF set. Split it into two updates:
2251 * - The original update, but with REF_LOG_ONLY and REF_NO_DEREF set
2252 * - A new, separate update for the referent reference
2253 * Note that the new update will itself be subject to splitting when
2254 * the iteration gets to it.
2256 static int split_symref_update(struct files_ref_store *refs,
2257 struct ref_update *update,
2258 const char *referent,
2259 struct ref_transaction *transaction,
2260 struct string_list *affected_refnames,
2263 struct string_list_item *item;
2264 struct ref_update *new_update;
2265 unsigned int new_flags;
2268 * First make sure that referent is not already in the
2269 * transaction. This check is O(lg N) in the transaction
2270 * size, but it happens at most once per symref in a
2273 if (string_list_has_string(affected_refnames, referent)) {
2274 /* An entry already exists */
2276 "multiple updates for '%s' (including one "
2277 "via symref '%s') are not allowed",
2278 referent, update->refname);
2279 return TRANSACTION_NAME_CONFLICT;
2282 new_flags = update->flags;
2283 if (!strcmp(update->refname, "HEAD")) {
2285 * Record that the new update came via HEAD, so that
2286 * when we process it, split_head_update() doesn't try
2287 * to add another reflog update for HEAD. Note that
2288 * this bit will be propagated if the new_update
2289 * itself needs to be split.
2291 new_flags |= REF_UPDATE_VIA_HEAD;
2294 new_update = ref_transaction_add_update(
2295 transaction, referent, new_flags,
2296 &update->new_oid, &update->old_oid,
2299 new_update->parent_update = update;
2302 * Change the symbolic ref update to log only. Also, it
2303 * doesn't need to check its old OID value, as that will be
2304 * done when new_update is processed.
2306 update->flags |= REF_LOG_ONLY | REF_NO_DEREF;
2307 update->flags &= ~REF_HAVE_OLD;
2310 * Add the referent. This insertion is O(N) in the transaction
2311 * size, but it happens at most once per symref in a
2312 * transaction. Make sure to add new_update->refname, which will
2313 * be valid as long as affected_refnames is in use, and NOT
2314 * referent, which might soon be freed by our caller.
2316 item = string_list_insert(affected_refnames, new_update->refname);
2318 BUG("%s unexpectedly found in affected_refnames",
2319 new_update->refname);
2320 item->util = new_update;
2326 * Return the refname under which update was originally requested.
2328 static const char *original_update_refname(struct ref_update *update)
2330 while (update->parent_update)
2331 update = update->parent_update;
2333 return update->refname;
2337 * Check whether the REF_HAVE_OLD and old_oid values stored in update
2338 * are consistent with oid, which is the reference's current value. If
2339 * everything is OK, return 0; otherwise, write an error message to
2340 * err and return -1.
2342 static int check_old_oid(struct ref_update *update, struct object_id *oid,
2345 if (!(update->flags & REF_HAVE_OLD) ||
2346 oideq(oid, &update->old_oid))
2349 if (is_null_oid(&update->old_oid))
2350 strbuf_addf(err, "cannot lock ref '%s': "
2351 "reference already exists",
2352 original_update_refname(update));
2353 else if (is_null_oid(oid))
2354 strbuf_addf(err, "cannot lock ref '%s': "
2355 "reference is missing but expected %s",
2356 original_update_refname(update),
2357 oid_to_hex(&update->old_oid));
2359 strbuf_addf(err, "cannot lock ref '%s': "
2360 "is at %s but expected %s",
2361 original_update_refname(update),
2363 oid_to_hex(&update->old_oid));
2369 * Prepare for carrying out update:
2370 * - Lock the reference referred to by update.
2371 * - Read the reference under lock.
2372 * - Check that its old OID value (if specified) is correct, and in
2373 * any case record it in update->lock->old_oid for later use when
2374 * writing the reflog.
2375 * - If it is a symref update without REF_NO_DEREF, split it up into a
2376 * REF_LOG_ONLY update of the symref and add a separate update for
2377 * the referent to transaction.
2378 * - If it is an update of head_ref, add a corresponding REF_LOG_ONLY
2381 static int lock_ref_for_update(struct files_ref_store *refs,
2382 struct ref_update *update,
2383 struct ref_transaction *transaction,
2384 const char *head_ref,
2385 struct string_list *affected_refnames,
2388 struct strbuf referent = STRBUF_INIT;
2389 int mustexist = (update->flags & REF_HAVE_OLD) &&
2390 !is_null_oid(&update->old_oid);
2392 struct ref_lock *lock;
2394 files_assert_main_repository(refs, "lock_ref_for_update");
2396 if ((update->flags & REF_HAVE_NEW) && is_null_oid(&update->new_oid))
2397 update->flags |= REF_DELETING;
2400 ret = split_head_update(update, transaction, head_ref,
2401 affected_refnames, err);
2406 ret = lock_raw_ref(refs, update->refname, mustexist,
2407 affected_refnames, NULL,
2409 &update->type, err);
2413 reason = strbuf_detach(err, NULL);
2414 strbuf_addf(err, "cannot lock ref '%s': %s",
2415 original_update_refname(update), reason);
2420 update->backend_data = lock;
2422 if (update->type & REF_ISSYMREF) {
2423 if (update->flags & REF_NO_DEREF) {
2425 * We won't be reading the referent as part of
2426 * the transaction, so we have to read it here
2427 * to record and possibly check old_oid:
2429 if (refs_read_ref_full(&refs->base,
2431 &lock->old_oid, NULL)) {
2432 if (update->flags & REF_HAVE_OLD) {
2433 strbuf_addf(err, "cannot lock ref '%s': "
2434 "error reading reference",
2435 original_update_refname(update));
2436 ret = TRANSACTION_GENERIC_ERROR;
2439 } else if (check_old_oid(update, &lock->old_oid, err)) {
2440 ret = TRANSACTION_GENERIC_ERROR;
2445 * Create a new update for the reference this
2446 * symref is pointing at. Also, we will record
2447 * and verify old_oid for this update as part
2448 * of processing the split-off update, so we
2449 * don't have to do it here.
2451 ret = split_symref_update(refs, update,
2452 referent.buf, transaction,
2453 affected_refnames, err);
2458 struct ref_update *parent_update;
2460 if (check_old_oid(update, &lock->old_oid, err)) {
2461 ret = TRANSACTION_GENERIC_ERROR;
2466 * If this update is happening indirectly because of a
2467 * symref update, record the old OID in the parent
2470 for (parent_update = update->parent_update;
2472 parent_update = parent_update->parent_update) {
2473 struct ref_lock *parent_lock = parent_update->backend_data;
2474 oidcpy(&parent_lock->old_oid, &lock->old_oid);
2478 if ((update->flags & REF_HAVE_NEW) &&
2479 !(update->flags & REF_DELETING) &&
2480 !(update->flags & REF_LOG_ONLY)) {
2481 if (!(update->type & REF_ISSYMREF) &&
2482 oideq(&lock->old_oid, &update->new_oid)) {
2484 * The reference already has the desired
2485 * value, so we don't need to write it.
2487 } else if (write_ref_to_lockfile(lock, &update->new_oid,
2489 char *write_err = strbuf_detach(err, NULL);
2492 * The lock was freed upon failure of
2493 * write_ref_to_lockfile():
2495 update->backend_data = NULL;
2497 "cannot update ref '%s': %s",
2498 update->refname, write_err);
2500 ret = TRANSACTION_GENERIC_ERROR;
2503 update->flags |= REF_NEEDS_COMMIT;
2506 if (!(update->flags & REF_NEEDS_COMMIT)) {
2508 * We didn't call write_ref_to_lockfile(), so
2509 * the lockfile is still open. Close it to
2510 * free up the file descriptor:
2512 if (close_ref_gently(lock)) {
2513 strbuf_addf(err, "couldn't close '%s.lock'",
2515 ret = TRANSACTION_GENERIC_ERROR;
2521 strbuf_release(&referent);
2525 struct files_transaction_backend_data {
2526 struct ref_transaction *packed_transaction;
2527 int packed_refs_locked;
2531 * Unlock any references in `transaction` that are still locked, and
2532 * mark the transaction closed.
2534 static void files_transaction_cleanup(struct files_ref_store *refs,
2535 struct ref_transaction *transaction)
2538 struct files_transaction_backend_data *backend_data =
2539 transaction->backend_data;
2540 struct strbuf err = STRBUF_INIT;
2542 for (i = 0; i < transaction->nr; i++) {
2543 struct ref_update *update = transaction->updates[i];
2544 struct ref_lock *lock = update->backend_data;
2548 update->backend_data = NULL;
2552 if (backend_data->packed_transaction &&
2553 ref_transaction_abort(backend_data->packed_transaction, &err)) {
2554 error("error aborting transaction: %s", err.buf);
2555 strbuf_release(&err);
2558 if (backend_data->packed_refs_locked)
2559 packed_refs_unlock(refs->packed_ref_store);
2563 transaction->state = REF_TRANSACTION_CLOSED;
2566 static int files_transaction_prepare(struct ref_store *ref_store,
2567 struct ref_transaction *transaction,
2570 struct files_ref_store *refs =
2571 files_downcast(ref_store, REF_STORE_WRITE,
2572 "ref_transaction_prepare");
2575 struct string_list affected_refnames = STRING_LIST_INIT_NODUP;
2576 char *head_ref = NULL;
2578 struct files_transaction_backend_data *backend_data;
2579 struct ref_transaction *packed_transaction = NULL;
2583 if (!transaction->nr)
2586 backend_data = xcalloc(1, sizeof(*backend_data));
2587 transaction->backend_data = backend_data;
2590 * Fail if a refname appears more than once in the
2591 * transaction. (If we end up splitting up any updates using
2592 * split_symref_update() or split_head_update(), those
2593 * functions will check that the new updates don't have the
2594 * same refname as any existing ones.) Also fail if any of the
2595 * updates use REF_IS_PRUNING without REF_NO_DEREF.
2597 for (i = 0; i < transaction->nr; i++) {
2598 struct ref_update *update = transaction->updates[i];
2599 struct string_list_item *item =
2600 string_list_append(&affected_refnames, update->refname);
2602 if ((update->flags & REF_IS_PRUNING) &&
2603 !(update->flags & REF_NO_DEREF))
2604 BUG("REF_IS_PRUNING set without REF_NO_DEREF");
2607 * We store a pointer to update in item->util, but at
2608 * the moment we never use the value of this field
2609 * except to check whether it is non-NULL.
2611 item->util = update;
2613 string_list_sort(&affected_refnames);
2614 if (ref_update_reject_duplicates(&affected_refnames, err)) {
2615 ret = TRANSACTION_GENERIC_ERROR;
2620 * Special hack: If a branch is updated directly and HEAD
2621 * points to it (may happen on the remote side of a push
2622 * for example) then logically the HEAD reflog should be
2625 * A generic solution would require reverse symref lookups,
2626 * but finding all symrefs pointing to a given branch would be
2627 * rather costly for this rare event (the direct update of a
2628 * branch) to be worth it. So let's cheat and check with HEAD
2629 * only, which should cover 99% of all usage scenarios (even
2630 * 100% of the default ones).
2632 * So if HEAD is a symbolic reference, then record the name of
2633 * the reference that it points to. If we see an update of
2634 * head_ref within the transaction, then split_head_update()
2635 * arranges for the reflog of HEAD to be updated, too.
2637 head_ref = refs_resolve_refdup(ref_store, "HEAD",
2638 RESOLVE_REF_NO_RECURSE,
2641 if (head_ref && !(head_type & REF_ISSYMREF)) {
2642 FREE_AND_NULL(head_ref);
2646 * Acquire all locks, verify old values if provided, check
2647 * that new values are valid, and write new values to the
2648 * lockfiles, ready to be activated. Only keep one lockfile
2649 * open at a time to avoid running out of file descriptors.
2650 * Note that lock_ref_for_update() might append more updates
2651 * to the transaction.
2653 for (i = 0; i < transaction->nr; i++) {
2654 struct ref_update *update = transaction->updates[i];
2656 ret = lock_ref_for_update(refs, update, transaction,
2657 head_ref, &affected_refnames, err);
2661 if (update->flags & REF_DELETING &&
2662 !(update->flags & REF_LOG_ONLY) &&
2663 !(update->flags & REF_IS_PRUNING)) {
2665 * This reference has to be deleted from
2666 * packed-refs if it exists there.
2668 if (!packed_transaction) {
2669 packed_transaction = ref_store_transaction_begin(
2670 refs->packed_ref_store, err);
2671 if (!packed_transaction) {
2672 ret = TRANSACTION_GENERIC_ERROR;
2676 backend_data->packed_transaction =
2680 ref_transaction_add_update(
2681 packed_transaction, update->refname,
2682 REF_HAVE_NEW | REF_NO_DEREF,
2683 &update->new_oid, NULL,
2688 if (packed_transaction) {
2689 if (packed_refs_lock(refs->packed_ref_store, 0, err)) {
2690 ret = TRANSACTION_GENERIC_ERROR;
2693 backend_data->packed_refs_locked = 1;
2695 if (is_packed_transaction_needed(refs->packed_ref_store,
2696 packed_transaction)) {
2697 ret = ref_transaction_prepare(packed_transaction, err);
2700 * We can skip rewriting the `packed-refs`
2701 * file. But we do need to leave it locked, so
2702 * that somebody else doesn't pack a reference
2703 * that we are trying to delete.
2705 if (ref_transaction_abort(packed_transaction, err)) {
2706 ret = TRANSACTION_GENERIC_ERROR;
2709 backend_data->packed_transaction = NULL;
2715 string_list_clear(&affected_refnames, 0);
2718 files_transaction_cleanup(refs, transaction);
2720 transaction->state = REF_TRANSACTION_PREPARED;
2725 static int files_transaction_finish(struct ref_store *ref_store,
2726 struct ref_transaction *transaction,
2729 struct files_ref_store *refs =
2730 files_downcast(ref_store, 0, "ref_transaction_finish");
2733 struct strbuf sb = STRBUF_INIT;
2734 struct files_transaction_backend_data *backend_data;
2735 struct ref_transaction *packed_transaction;
2740 if (!transaction->nr) {
2741 transaction->state = REF_TRANSACTION_CLOSED;
2745 backend_data = transaction->backend_data;
2746 packed_transaction = backend_data->packed_transaction;
2748 /* Perform updates first so live commits remain referenced */
2749 for (i = 0; i < transaction->nr; i++) {
2750 struct ref_update *update = transaction->updates[i];
2751 struct ref_lock *lock = update->backend_data;
2753 if (update->flags & REF_NEEDS_COMMIT ||
2754 update->flags & REF_LOG_ONLY) {
2755 if (files_log_ref_write(refs,
2759 update->msg, update->flags,
2761 char *old_msg = strbuf_detach(err, NULL);
2763 strbuf_addf(err, "cannot update the ref '%s': %s",
2764 lock->ref_name, old_msg);
2767 update->backend_data = NULL;
2768 ret = TRANSACTION_GENERIC_ERROR;
2772 if (update->flags & REF_NEEDS_COMMIT) {
2773 clear_loose_ref_cache(refs);
2774 if (commit_ref(lock)) {
2775 strbuf_addf(err, "couldn't set '%s'", lock->ref_name);
2777 update->backend_data = NULL;
2778 ret = TRANSACTION_GENERIC_ERROR;
2785 * Now that updates are safely completed, we can perform
2786 * deletes. First delete the reflogs of any references that
2787 * will be deleted, since (in the unexpected event of an
2788 * error) leaving a reference without a reflog is less bad
2789 * than leaving a reflog without a reference (the latter is a
2790 * mildly invalid repository state):
2792 for (i = 0; i < transaction->nr; i++) {
2793 struct ref_update *update = transaction->updates[i];
2794 if (update->flags & REF_DELETING &&
2795 !(update->flags & REF_LOG_ONLY) &&
2796 !(update->flags & REF_IS_PRUNING)) {
2798 files_reflog_path(refs, &sb, update->refname);
2799 if (!unlink_or_warn(sb.buf))
2800 try_remove_empty_parents(refs, update->refname,
2801 REMOVE_EMPTY_PARENTS_REFLOG);
2806 * Perform deletes now that updates are safely completed.
2808 * First delete any packed versions of the references, while
2809 * retaining the packed-refs lock:
2811 if (packed_transaction) {
2812 ret = ref_transaction_commit(packed_transaction, err);
2813 ref_transaction_free(packed_transaction);
2814 packed_transaction = NULL;
2815 backend_data->packed_transaction = NULL;
2820 /* Now delete the loose versions of the references: */
2821 for (i = 0; i < transaction->nr; i++) {
2822 struct ref_update *update = transaction->updates[i];
2823 struct ref_lock *lock = update->backend_data;
2825 if (update->flags & REF_DELETING &&
2826 !(update->flags & REF_LOG_ONLY)) {
2827 if (!(update->type & REF_ISPACKED) ||
2828 update->type & REF_ISSYMREF) {
2829 /* It is a loose reference. */
2831 files_ref_path(refs, &sb, lock->ref_name);
2832 if (unlink_or_msg(sb.buf, err)) {
2833 ret = TRANSACTION_GENERIC_ERROR;
2836 update->flags |= REF_DELETED_LOOSE;
2841 clear_loose_ref_cache(refs);
2844 files_transaction_cleanup(refs, transaction);
2846 for (i = 0; i < transaction->nr; i++) {
2847 struct ref_update *update = transaction->updates[i];
2849 if (update->flags & REF_DELETED_LOOSE) {
2851 * The loose reference was deleted. Delete any
2852 * empty parent directories. (Note that this
2853 * can only work because we have already
2854 * removed the lockfile.)
2856 try_remove_empty_parents(refs, update->refname,
2857 REMOVE_EMPTY_PARENTS_REF);
2861 strbuf_release(&sb);
2865 static int files_transaction_abort(struct ref_store *ref_store,
2866 struct ref_transaction *transaction,
2869 struct files_ref_store *refs =
2870 files_downcast(ref_store, 0, "ref_transaction_abort");
2872 files_transaction_cleanup(refs, transaction);
2876 static int ref_present(const char *refname,
2877 const struct object_id *oid, int flags, void *cb_data)
2879 struct string_list *affected_refnames = cb_data;
2881 return string_list_has_string(affected_refnames, refname);
2884 static int files_initial_transaction_commit(struct ref_store *ref_store,
2885 struct ref_transaction *transaction,
2888 struct files_ref_store *refs =
2889 files_downcast(ref_store, REF_STORE_WRITE,
2890 "initial_ref_transaction_commit");
2893 struct string_list affected_refnames = STRING_LIST_INIT_NODUP;
2894 struct ref_transaction *packed_transaction = NULL;
2898 if (transaction->state != REF_TRANSACTION_OPEN)
2899 BUG("commit called for transaction that is not open");
2901 /* Fail if a refname appears more than once in the transaction: */
2902 for (i = 0; i < transaction->nr; i++)
2903 string_list_append(&affected_refnames,
2904 transaction->updates[i]->refname);
2905 string_list_sort(&affected_refnames);
2906 if (ref_update_reject_duplicates(&affected_refnames, err)) {
2907 ret = TRANSACTION_GENERIC_ERROR;
2912 * It's really undefined to call this function in an active
2913 * repository or when there are existing references: we are
2914 * only locking and changing packed-refs, so (1) any
2915 * simultaneous processes might try to change a reference at
2916 * the same time we do, and (2) any existing loose versions of
2917 * the references that we are setting would have precedence
2918 * over our values. But some remote helpers create the remote
2919 * "HEAD" and "master" branches before calling this function,
2920 * so here we really only check that none of the references
2921 * that we are creating already exists.
2923 if (refs_for_each_rawref(&refs->base, ref_present,
2924 &affected_refnames))
2925 BUG("initial ref transaction called with existing refs");
2927 packed_transaction = ref_store_transaction_begin(refs->packed_ref_store, err);
2928 if (!packed_transaction) {
2929 ret = TRANSACTION_GENERIC_ERROR;
2933 for (i = 0; i < transaction->nr; i++) {
2934 struct ref_update *update = transaction->updates[i];
2936 if ((update->flags & REF_HAVE_OLD) &&
2937 !is_null_oid(&update->old_oid))
2938 BUG("initial ref transaction with old_sha1 set");
2939 if (refs_verify_refname_available(&refs->base, update->refname,
2940 &affected_refnames, NULL,
2942 ret = TRANSACTION_NAME_CONFLICT;
2947 * Add a reference creation for this reference to the
2948 * packed-refs transaction:
2950 ref_transaction_add_update(packed_transaction, update->refname,
2951 update->flags & ~REF_HAVE_OLD,
2952 &update->new_oid, &update->old_oid,
2956 if (packed_refs_lock(refs->packed_ref_store, 0, err)) {
2957 ret = TRANSACTION_GENERIC_ERROR;
2961 if (initial_ref_transaction_commit(packed_transaction, err)) {
2962 ret = TRANSACTION_GENERIC_ERROR;
2965 packed_refs_unlock(refs->packed_ref_store);
2967 if (packed_transaction)
2968 ref_transaction_free(packed_transaction);
2969 transaction->state = REF_TRANSACTION_CLOSED;
2970 string_list_clear(&affected_refnames, 0);
2974 struct expire_reflog_cb {
2976 reflog_expiry_should_prune_fn *should_prune_fn;
2979 struct object_id last_kept_oid;
2982 static int expire_reflog_ent(struct object_id *ooid, struct object_id *noid,
2983 const char *email, timestamp_t timestamp, int tz,
2984 const char *message, void *cb_data)
2986 struct expire_reflog_cb *cb = cb_data;
2987 struct expire_reflog_policy_cb *policy_cb = cb->policy_cb;
2989 if (cb->flags & EXPIRE_REFLOGS_REWRITE)
2990 ooid = &cb->last_kept_oid;
2992 if ((*cb->should_prune_fn)(ooid, noid, email, timestamp, tz,
2993 message, policy_cb)) {
2995 printf("would prune %s", message);
2996 else if (cb->flags & EXPIRE_REFLOGS_VERBOSE)
2997 printf("prune %s", message);
3000 fprintf(cb->newlog, "%s %s %s %"PRItime" %+05d\t%s",
3001 oid_to_hex(ooid), oid_to_hex(noid),
3002 email, timestamp, tz, message);
3003 oidcpy(&cb->last_kept_oid, noid);
3005 if (cb->flags & EXPIRE_REFLOGS_VERBOSE)
3006 printf("keep %s", message);
3011 static int files_reflog_expire(struct ref_store *ref_store,
3012 const char *refname, const struct object_id *oid,
3014 reflog_expiry_prepare_fn prepare_fn,
3015 reflog_expiry_should_prune_fn should_prune_fn,
3016 reflog_expiry_cleanup_fn cleanup_fn,
3017 void *policy_cb_data)
3019 struct files_ref_store *refs =
3020 files_downcast(ref_store, REF_STORE_WRITE, "reflog_expire");
3021 struct lock_file reflog_lock = LOCK_INIT;
3022 struct expire_reflog_cb cb;
3023 struct ref_lock *lock;
3024 struct strbuf log_file_sb = STRBUF_INIT;
3028 struct strbuf err = STRBUF_INIT;
3030 memset(&cb, 0, sizeof(cb));
3032 cb.policy_cb = policy_cb_data;
3033 cb.should_prune_fn = should_prune_fn;
3036 * The reflog file is locked by holding the lock on the
3037 * reference itself, plus we might need to update the
3038 * reference if --updateref was specified:
3040 lock = lock_ref_oid_basic(refs, refname, oid,
3041 NULL, NULL, REF_NO_DEREF,
3044 error("cannot lock ref '%s': %s", refname, err.buf);
3045 strbuf_release(&err);
3048 if (!refs_reflog_exists(ref_store, refname)) {
3053 files_reflog_path(refs, &log_file_sb, refname);
3054 log_file = strbuf_detach(&log_file_sb, NULL);
3055 if (!(flags & EXPIRE_REFLOGS_DRY_RUN)) {
3057 * Even though holding $GIT_DIR/logs/$reflog.lock has
3058 * no locking implications, we use the lock_file
3059 * machinery here anyway because it does a lot of the
3060 * work we need, including cleaning up if the program
3061 * exits unexpectedly.
3063 if (hold_lock_file_for_update(&reflog_lock, log_file, 0) < 0) {
3064 struct strbuf err = STRBUF_INIT;
3065 unable_to_lock_message(log_file, errno, &err);
3066 error("%s", err.buf);
3067 strbuf_release(&err);
3070 cb.newlog = fdopen_lock_file(&reflog_lock, "w");
3072 error("cannot fdopen %s (%s)",
3073 get_lock_file_path(&reflog_lock), strerror(errno));
3078 (*prepare_fn)(refname, oid, cb.policy_cb);
3079 refs_for_each_reflog_ent(ref_store, refname, expire_reflog_ent, &cb);
3080 (*cleanup_fn)(cb.policy_cb);
3082 if (!(flags & EXPIRE_REFLOGS_DRY_RUN)) {
3084 * It doesn't make sense to adjust a reference pointed
3085 * to by a symbolic ref based on expiring entries in
3086 * the symbolic reference's reflog. Nor can we update
3087 * a reference if there are no remaining reflog
3090 int update = (flags & EXPIRE_REFLOGS_UPDATE_REF) &&
3091 !(type & REF_ISSYMREF) &&
3092 !is_null_oid(&cb.last_kept_oid);
3094 if (close_lock_file_gently(&reflog_lock)) {
3095 status |= error("couldn't write %s: %s", log_file,
3097 rollback_lock_file(&reflog_lock);
3098 } else if (update &&
3099 (write_in_full(get_lock_file_fd(&lock->lk),
3100 oid_to_hex(&cb.last_kept_oid), the_hash_algo->hexsz) < 0 ||
3101 write_str_in_full(get_lock_file_fd(&lock->lk), "\n") < 0 ||
3102 close_ref_gently(lock) < 0)) {
3103 status |= error("couldn't write %s",
3104 get_lock_file_path(&lock->lk));
3105 rollback_lock_file(&reflog_lock);
3106 } else if (commit_lock_file(&reflog_lock)) {
3107 status |= error("unable to write reflog '%s' (%s)",
3108 log_file, strerror(errno));
3109 } else if (update && commit_ref(lock)) {
3110 status |= error("couldn't set %s", lock->ref_name);
3118 rollback_lock_file(&reflog_lock);
3124 static int files_init_db(struct ref_store *ref_store, struct strbuf *err)
3126 struct files_ref_store *refs =
3127 files_downcast(ref_store, REF_STORE_WRITE, "init_db");
3128 struct strbuf sb = STRBUF_INIT;
3131 * Create .git/refs/{heads,tags}
3133 files_ref_path(refs, &sb, "refs/heads");
3134 safe_create_dir(sb.buf, 1);
3137 files_ref_path(refs, &sb, "refs/tags");
3138 safe_create_dir(sb.buf, 1);
3140 strbuf_release(&sb);
3144 struct ref_storage_be refs_be_files = {
3147 files_ref_store_create,
3149 files_transaction_prepare,
3150 files_transaction_finish,
3151 files_transaction_abort,
3152 files_initial_transaction_commit,
3155 files_create_symref,
3160 files_ref_iterator_begin,
3163 files_reflog_iterator_begin,
3164 files_for_each_reflog_ent,
3165 files_for_each_reflog_ent_reverse,
3166 files_reflog_exists,
3167 files_create_reflog,
3168 files_delete_reflog,