summaryrefslogtreecommitdiff
path: root/refs/files-backend.c
diff options
context:
space:
mode:
Diffstat (limited to 'refs/files-backend.c')
-rw-r--r--refs/files-backend.c153
1 files changed, 88 insertions, 65 deletions
diff --git a/refs/files-backend.c b/refs/files-backend.c
index 43a3b88..95acab7 100644
--- a/refs/files-backend.c
+++ b/refs/files-backend.c
@@ -277,11 +277,10 @@ static void loose_fill_ref_dir(struct ref_store *ref_store,
create_dir_entry(dir->cache, refname.buf,
refname.len));
} else {
- int ignore_errno;
if (!refs_resolve_ref_unsafe(&refs->base,
refname.buf,
RESOLVE_REF_READING,
- &oid, &flag, &ignore_errno)) {
+ &oid, &flag)) {
oidclr(&oid);
flag |= REF_ISBROKEN;
} else if (is_null_oid(&oid)) {
@@ -339,9 +338,9 @@ static struct ref_cache *get_loose_ref_cache(struct files_ref_store *refs)
return refs->loose;
}
-static int files_read_raw_ref(struct ref_store *ref_store, const char *refname,
- struct object_id *oid, struct strbuf *referent,
- unsigned int *type, int *failure_errno)
+static int read_ref_internal(struct ref_store *ref_store, const char *refname,
+ struct object_id *oid, struct strbuf *referent,
+ unsigned int *type, int *failure_errno, int skip_packed_refs)
{
struct files_ref_store *refs =
files_downcast(ref_store, REF_STORE_READ, "read_raw_ref");
@@ -382,7 +381,7 @@ stat_ref:
if (lstat(path, &st) < 0) {
int ignore_errno;
myerr = errno;
- if (myerr != ENOENT)
+ if (myerr != ENOENT || skip_packed_refs)
goto out;
if (refs_read_raw_ref(refs->packed_ref_store, refname, oid,
referent, type, &ignore_errno)) {
@@ -426,7 +425,8 @@ stat_ref:
* ref is supposed to be, there could still be a
* packed ref:
*/
- if (refs_read_raw_ref(refs->packed_ref_store, refname, oid,
+ if (skip_packed_refs ||
+ refs_read_raw_ref(refs->packed_ref_store, refname, oid,
referent, type, &ignore_errno)) {
myerr = EISDIR;
goto out;
@@ -471,6 +471,27 @@ out:
return ret;
}
+static int files_read_raw_ref(struct ref_store *ref_store, const char *refname,
+ struct object_id *oid, struct strbuf *referent,
+ unsigned int *type, int *failure_errno)
+{
+ return read_ref_internal(ref_store, refname, oid, referent, type, failure_errno, 0);
+}
+
+static int files_read_symbolic_ref(struct ref_store *ref_store, const char *refname,
+ struct strbuf *referent)
+{
+ struct object_id oid;
+ int failure_errno, ret;
+ unsigned int type;
+
+ ret = read_ref_internal(ref_store, refname, &oid, referent, &type, &failure_errno, 1);
+ if (ret)
+ return ret;
+
+ return !(type & REF_ISSYMREF);
+}
+
int parse_loose_ref_contents(const char *buf, struct object_id *oid,
struct strbuf *referent, unsigned int *type,
int *failure_errno)
@@ -801,9 +822,9 @@ static int files_ref_iterator_abort(struct ref_iterator *ref_iterator)
}
static struct ref_iterator_vtable files_ref_iterator_vtable = {
- files_ref_iterator_advance,
- files_ref_iterator_peel,
- files_ref_iterator_abort
+ .advance = files_ref_iterator_advance,
+ .peel = files_ref_iterator_peel,
+ .abort = files_ref_iterator_abort,
};
static struct ref_iterator *files_ref_iterator_begin(
@@ -1006,7 +1027,6 @@ static struct ref_lock *lock_ref_oid_basic(struct files_ref_store *refs,
{
struct strbuf ref_file = STRBUF_INIT;
struct ref_lock *lock;
- int ignore_errno;
files_assert_main_repository(refs, "lock_ref_oid_basic");
assert(err);
@@ -1034,7 +1054,7 @@ static struct ref_lock *lock_ref_oid_basic(struct files_ref_store *refs,
}
if (!refs_resolve_ref_unsafe(&refs->base, lock->ref_name, 0,
- &lock->old_oid, NULL, &ignore_errno))
+ &lock->old_oid, NULL))
oidclr(&lock->old_oid);
goto out;
@@ -1116,7 +1136,8 @@ static void prune_ref(struct files_ref_store *refs, struct ref_to_prune *r)
if (check_refname_format(r->name, 0))
return;
- transaction = ref_store_transaction_begin(&refs->base, &err);
+ transaction = ref_store_transaction_begin(&refs->base,
+ REF_TRANSACTION_SKIP_HOOK, &err);
if (!transaction)
goto cleanup;
ref_transaction_add_update(
@@ -1187,7 +1208,8 @@ static int files_pack_refs(struct ref_store *ref_store, unsigned int flags)
struct strbuf err = STRBUF_INIT;
struct ref_transaction *transaction;
- transaction = ref_store_transaction_begin(refs->packed_ref_store, &err);
+ transaction = ref_store_transaction_begin(refs->packed_ref_store,
+ REF_TRANSACTION_SKIP_HOOK, &err);
if (!transaction)
return -1;
@@ -1244,6 +1266,7 @@ static int files_delete_refs(struct ref_store *ref_store, const char *msg,
{
struct files_ref_store *refs =
files_downcast(ref_store, REF_STORE_WRITE, "delete_refs");
+ struct ref_transaction *transaction = NULL;
struct strbuf err = STRBUF_INIT;
int i, result = 0;
@@ -1253,10 +1276,15 @@ static int files_delete_refs(struct ref_store *ref_store, const char *msg,
if (packed_refs_lock(refs->packed_ref_store, 0, &err))
goto error;
- if (refs_delete_refs(refs->packed_ref_store, msg, refnames, flags)) {
- packed_refs_unlock(refs->packed_ref_store);
+ transaction = ref_store_transaction_begin(refs->packed_ref_store,
+ REF_TRANSACTION_SKIP_HOOK, &err);
+ if (!transaction)
+ goto error;
+
+ result = packed_refs_delete_refs(refs->packed_ref_store,
+ transaction, msg, refnames, flags);
+ if (result)
goto error;
- }
packed_refs_unlock(refs->packed_ref_store);
@@ -1267,6 +1295,7 @@ static int files_delete_refs(struct ref_store *ref_store, const char *msg,
result |= error(_("could not remove reference %s"), refname);
}
+ ref_transaction_free(transaction);
strbuf_release(&err);
return result;
@@ -1283,6 +1312,7 @@ error:
else
error(_("could not delete references: %s"), err.buf);
+ ref_transaction_free(transaction);
strbuf_release(&err);
return -1;
}
@@ -1399,7 +1429,6 @@ static int files_copy_or_rename_ref(struct ref_store *ref_store,
struct strbuf tmp_renamed_log = STRBUF_INIT;
int log, ret;
struct strbuf err = STRBUF_INIT;
- int ignore_errno;
files_reflog_path(refs, &sb_oldref, oldrefname);
files_reflog_path(refs, &sb_newref, newrefname);
@@ -1413,7 +1442,7 @@ static int files_copy_or_rename_ref(struct ref_store *ref_store,
if (!refs_resolve_ref_unsafe(&refs->base, oldrefname,
RESOLVE_REF_READING | RESOLVE_REF_NO_RECURSE,
- &orig_oid, &flag, &ignore_errno)) {
+ &orig_oid, &flag)) {
ret = error("refname %s not found", oldrefname);
goto out;
}
@@ -1459,7 +1488,7 @@ static int files_copy_or_rename_ref(struct ref_store *ref_store,
*/
if (!copy && refs_resolve_ref_unsafe(&refs->base, newrefname,
RESOLVE_REF_READING | RESOLVE_REF_NO_RECURSE,
- NULL, NULL, &ignore_errno) &&
+ NULL, NULL) &&
refs_delete_ref(&refs->base, NULL, newrefname,
NULL, REF_NO_DEREF)) {
if (errno == EISDIR) {
@@ -1780,6 +1809,7 @@ static int write_ref_to_lockfile(struct ref_lock *lock,
fd = get_lock_file_fd(&lock->lk);
if (write_in_full(fd, oid_to_hex(oid), the_hash_algo->hexsz) < 0 ||
write_in_full(fd, &term, 1) < 0 ||
+ fsync_component(FSYNC_COMPONENT_REFERENCE, get_lock_file_fd(&lock->lk)) < 0 ||
close_ref_gently(lock) < 0) {
strbuf_addf(err,
"couldn't write '%s'", get_lock_file_path(&lock->lk));
@@ -1828,12 +1858,10 @@ static int commit_ref_update(struct files_ref_store *refs,
*/
int head_flag;
const char *head_ref;
- int ignore_errno;
head_ref = refs_resolve_ref_unsafe(&refs->base, "HEAD",
RESOLVE_REF_READING,
- NULL, &head_flag,
- &ignore_errno);
+ NULL, &head_flag);
if (head_ref && (head_flag & REF_ISSYMREF) &&
!strcmp(head_ref, lock->ref_name)) {
struct strbuf log_err = STRBUF_INIT;
@@ -1877,12 +1905,10 @@ static void update_symref_reflog(struct files_ref_store *refs,
{
struct strbuf err = STRBUF_INIT;
struct object_id new_oid;
- int ignore_errno;
if (logmsg &&
refs_resolve_ref_unsafe(&refs->base, target,
- RESOLVE_REF_READING, &new_oid, NULL,
- &ignore_errno) &&
+ RESOLVE_REF_READING, &new_oid, NULL) &&
files_log_ref_write(refs, refname, &lock->old_oid,
&new_oid, logmsg, 0, &err)) {
error("%s", err.buf);
@@ -2156,7 +2182,6 @@ static int files_reflog_iterator_advance(struct ref_iterator *ref_iterator)
(struct files_reflog_iterator *)ref_iterator;
struct dir_iterator *diter = iter->dir_iterator;
int ok;
- int ignore_errno;
while ((ok = dir_iterator_advance(diter)) == ITER_OK) {
int flags;
@@ -2170,8 +2195,7 @@ static int files_reflog_iterator_advance(struct ref_iterator *ref_iterator)
if (!refs_resolve_ref_unsafe(iter->ref_store,
diter->relative_path, 0,
- &iter->oid, &flags,
- &ignore_errno)) {
+ &iter->oid, &flags)) {
error("bad ref for %s", diter->path.buf);
continue;
}
@@ -2208,9 +2232,9 @@ static int files_reflog_iterator_abort(struct ref_iterator *ref_iterator)
}
static struct ref_iterator_vtable files_reflog_iterator_vtable = {
- files_reflog_iterator_advance,
- files_reflog_iterator_peel,
- files_reflog_iterator_abort
+ .advance = files_reflog_iterator_advance,
+ .peel = files_reflog_iterator_peel,
+ .abort = files_reflog_iterator_abort,
};
static struct ref_iterator *reflog_iterator_begin(struct ref_store *ref_store,
@@ -2515,11 +2539,9 @@ static int lock_ref_for_update(struct files_ref_store *refs,
* the transaction, so we have to read it here
* to record and possibly check old_oid:
*/
- int ignore_errno;
if (!refs_resolve_ref_unsafe(&refs->base,
referent.buf, 0,
- &lock->old_oid, NULL,
- &ignore_errno)) {
+ &lock->old_oid, NULL)) {
if (update->flags & REF_HAVE_OLD) {
strbuf_addf(err, "cannot lock ref '%s': "
"error reading reference",
@@ -2762,7 +2784,8 @@ static int files_transaction_prepare(struct ref_store *ref_store,
*/
if (!packed_transaction) {
packed_transaction = ref_store_transaction_begin(
- refs->packed_ref_store, err);
+ refs->packed_ref_store,
+ REF_TRANSACTION_SKIP_HOOK, err);
if (!packed_transaction) {
ret = TRANSACTION_GENERIC_ERROR;
goto cleanup;
@@ -3033,7 +3056,8 @@ static int files_initial_transaction_commit(struct ref_store *ref_store,
&affected_refnames))
BUG("initial ref transaction called with existing refs");
- packed_transaction = ref_store_transaction_begin(refs->packed_ref_store, err);
+ packed_transaction = ref_store_transaction_begin(refs->packed_ref_store,
+ REF_TRANSACTION_SKIP_HOOK, err);
if (!packed_transaction) {
ret = TRANSACTION_GENERIC_ERROR;
goto cleanup;
@@ -3208,14 +3232,12 @@ static int files_reflog_expire(struct ref_store *ref_store,
if ((expire_flags & EXPIRE_REFLOGS_UPDATE_REF) &&
!is_null_oid(&cb.last_kept_oid)) {
- int ignore_errno;
int type;
const char *ref;
ref = refs_resolve_ref_unsafe(&refs->base, refname,
RESOLVE_REF_NO_RECURSE,
- NULL, &type,
- &ignore_errno);
+ NULL, &type);
update = !!(ref && !(type & REF_ISSYMREF));
}
@@ -3270,29 +3292,30 @@ static int files_init_db(struct ref_store *ref_store, struct strbuf *err)
}
struct ref_storage_be refs_be_files = {
- NULL,
- "files",
- files_ref_store_create,
- files_init_db,
- files_transaction_prepare,
- files_transaction_finish,
- files_transaction_abort,
- files_initial_transaction_commit,
-
- files_pack_refs,
- files_create_symref,
- files_delete_refs,
- files_rename_ref,
- files_copy_ref,
-
- files_ref_iterator_begin,
- files_read_raw_ref,
-
- files_reflog_iterator_begin,
- files_for_each_reflog_ent,
- files_for_each_reflog_ent_reverse,
- files_reflog_exists,
- files_create_reflog,
- files_delete_reflog,
- files_reflog_expire
+ .next = NULL,
+ .name = "files",
+ .init = files_ref_store_create,
+ .init_db = files_init_db,
+ .transaction_prepare = files_transaction_prepare,
+ .transaction_finish = files_transaction_finish,
+ .transaction_abort = files_transaction_abort,
+ .initial_transaction_commit = files_initial_transaction_commit,
+
+ .pack_refs = files_pack_refs,
+ .create_symref = files_create_symref,
+ .delete_refs = files_delete_refs,
+ .rename_ref = files_rename_ref,
+ .copy_ref = files_copy_ref,
+
+ .iterator_begin = files_ref_iterator_begin,
+ .read_raw_ref = files_read_raw_ref,
+ .read_symbolic_ref = files_read_symbolic_ref,
+
+ .reflog_iterator_begin = files_reflog_iterator_begin,
+ .for_each_reflog_ent = files_for_each_reflog_ent,
+ .for_each_reflog_ent_reverse = files_for_each_reflog_ent_reverse,
+ .reflog_exists = files_reflog_exists,
+ .create_reflog = files_create_reflog,
+ .delete_reflog = files_delete_reflog,
+ .reflog_expire = files_reflog_expire
};