refs: new transaction related ref-store api
The transaction struct now takes a ref store at creation and will operate on that ref store alone. Signed-off-by: Nguyễn Thái Ngọc Duy <pclouds@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>maint
parent
7d2df051d0
commit
c0fe4e8ba3
55
refs.c
55
refs.c
|
@ -630,16 +630,20 @@ static int delete_pseudoref(const char *pseudoref, const unsigned char *old_sha1
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
int delete_ref(const char *msg, const char *refname,
|
int refs_delete_ref(struct ref_store *refs, const char *msg,
|
||||||
const unsigned char *old_sha1, unsigned int flags)
|
const char *refname,
|
||||||
|
const unsigned char *old_sha1,
|
||||||
|
unsigned int flags)
|
||||||
{
|
{
|
||||||
struct ref_transaction *transaction;
|
struct ref_transaction *transaction;
|
||||||
struct strbuf err = STRBUF_INIT;
|
struct strbuf err = STRBUF_INIT;
|
||||||
|
|
||||||
if (ref_type(refname) == REF_TYPE_PSEUDOREF)
|
if (ref_type(refname) == REF_TYPE_PSEUDOREF) {
|
||||||
|
assert(refs == get_main_ref_store());
|
||||||
return delete_pseudoref(refname, old_sha1);
|
return delete_pseudoref(refname, old_sha1);
|
||||||
|
}
|
||||||
|
|
||||||
transaction = ref_transaction_begin(&err);
|
transaction = ref_store_transaction_begin(refs, &err);
|
||||||
if (!transaction ||
|
if (!transaction ||
|
||||||
ref_transaction_delete(transaction, refname, old_sha1,
|
ref_transaction_delete(transaction, refname, old_sha1,
|
||||||
flags, msg, &err) ||
|
flags, msg, &err) ||
|
||||||
|
@ -654,6 +658,13 @@ int delete_ref(const char *msg, const char *refname,
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
int delete_ref(const char *msg, const char *refname,
|
||||||
|
const unsigned char *old_sha1, unsigned int flags)
|
||||||
|
{
|
||||||
|
return refs_delete_ref(get_main_ref_store(), msg, refname,
|
||||||
|
old_sha1, flags);
|
||||||
|
}
|
||||||
|
|
||||||
int copy_reflog_msg(char *buf, const char *msg)
|
int copy_reflog_msg(char *buf, const char *msg)
|
||||||
{
|
{
|
||||||
char *cp = buf;
|
char *cp = buf;
|
||||||
|
@ -813,11 +824,20 @@ int read_ref_at(const char *refname, unsigned int flags, unsigned long at_time,
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
struct ref_transaction *ref_transaction_begin(struct strbuf *err)
|
struct ref_transaction *ref_store_transaction_begin(struct ref_store *refs,
|
||||||
|
struct strbuf *err)
|
||||||
{
|
{
|
||||||
|
struct ref_transaction *tr;
|
||||||
assert(err);
|
assert(err);
|
||||||
|
|
||||||
return xcalloc(1, sizeof(struct ref_transaction));
|
tr = xcalloc(1, sizeof(struct ref_transaction));
|
||||||
|
tr->ref_store = refs;
|
||||||
|
return tr;
|
||||||
|
}
|
||||||
|
|
||||||
|
struct ref_transaction *ref_transaction_begin(struct strbuf *err)
|
||||||
|
{
|
||||||
|
return ref_store_transaction_begin(get_main_ref_store(), err);
|
||||||
}
|
}
|
||||||
|
|
||||||
void ref_transaction_free(struct ref_transaction *transaction)
|
void ref_transaction_free(struct ref_transaction *transaction)
|
||||||
|
@ -934,18 +954,20 @@ int update_ref_oid(const char *msg, const char *refname,
|
||||||
old_oid ? old_oid->hash : NULL, flags, onerr);
|
old_oid ? old_oid->hash : NULL, flags, onerr);
|
||||||
}
|
}
|
||||||
|
|
||||||
int update_ref(const char *msg, const char *refname,
|
int refs_update_ref(struct ref_store *refs, const char *msg,
|
||||||
const unsigned char *new_sha1, const unsigned char *old_sha1,
|
const char *refname, const unsigned char *new_sha1,
|
||||||
unsigned int flags, enum action_on_err onerr)
|
const unsigned char *old_sha1, unsigned int flags,
|
||||||
|
enum action_on_err onerr)
|
||||||
{
|
{
|
||||||
struct ref_transaction *t = NULL;
|
struct ref_transaction *t = NULL;
|
||||||
struct strbuf err = STRBUF_INIT;
|
struct strbuf err = STRBUF_INIT;
|
||||||
int ret = 0;
|
int ret = 0;
|
||||||
|
|
||||||
if (ref_type(refname) == REF_TYPE_PSEUDOREF) {
|
if (ref_type(refname) == REF_TYPE_PSEUDOREF) {
|
||||||
|
assert(refs == get_main_ref_store());
|
||||||
ret = write_pseudoref(refname, new_sha1, old_sha1, &err);
|
ret = write_pseudoref(refname, new_sha1, old_sha1, &err);
|
||||||
} else {
|
} else {
|
||||||
t = ref_transaction_begin(&err);
|
t = ref_store_transaction_begin(refs, &err);
|
||||||
if (!t ||
|
if (!t ||
|
||||||
ref_transaction_update(t, refname, new_sha1, old_sha1,
|
ref_transaction_update(t, refname, new_sha1, old_sha1,
|
||||||
flags, msg, &err) ||
|
flags, msg, &err) ||
|
||||||
|
@ -976,6 +998,15 @@ int update_ref(const char *msg, const char *refname,
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
int update_ref(const char *msg, const char *refname,
|
||||||
|
const unsigned char *new_sha1,
|
||||||
|
const unsigned char *old_sha1,
|
||||||
|
unsigned int flags, enum action_on_err onerr)
|
||||||
|
{
|
||||||
|
return refs_update_ref(get_main_ref_store(), msg, refname, new_sha1,
|
||||||
|
old_sha1, flags, onerr);
|
||||||
|
}
|
||||||
|
|
||||||
char *shorten_unambiguous_ref(const char *refname, int strict)
|
char *shorten_unambiguous_ref(const char *refname, int strict)
|
||||||
{
|
{
|
||||||
int i;
|
int i;
|
||||||
|
@ -1607,7 +1638,7 @@ int create_symref(const char *ref_target, const char *refs_heads_master,
|
||||||
int ref_transaction_commit(struct ref_transaction *transaction,
|
int ref_transaction_commit(struct ref_transaction *transaction,
|
||||||
struct strbuf *err)
|
struct strbuf *err)
|
||||||
{
|
{
|
||||||
struct ref_store *refs = get_main_ref_store();
|
struct ref_store *refs = transaction->ref_store;
|
||||||
|
|
||||||
return refs->be->transaction_commit(refs, transaction, err);
|
return refs->be->transaction_commit(refs, transaction, err);
|
||||||
}
|
}
|
||||||
|
@ -1726,7 +1757,7 @@ int reflog_expire(const char *refname, const unsigned char *sha1,
|
||||||
int initial_ref_transaction_commit(struct ref_transaction *transaction,
|
int initial_ref_transaction_commit(struct ref_transaction *transaction,
|
||||||
struct strbuf *err)
|
struct strbuf *err)
|
||||||
{
|
{
|
||||||
struct ref_store *refs = get_main_ref_store();
|
struct ref_store *refs = transaction->ref_store;
|
||||||
|
|
||||||
return refs->be->initial_transaction_commit(refs, transaction, err);
|
return refs->be->initial_transaction_commit(refs, transaction, err);
|
||||||
}
|
}
|
||||||
|
|
9
refs.h
9
refs.h
|
@ -333,6 +333,10 @@ int reflog_exists(const char *refname);
|
||||||
* exists, regardless of its old value. It is an error for old_sha1 to
|
* exists, regardless of its old value. It is an error for old_sha1 to
|
||||||
* be NULL_SHA1. flags is passed through to ref_transaction_delete().
|
* be NULL_SHA1. flags is passed through to ref_transaction_delete().
|
||||||
*/
|
*/
|
||||||
|
int refs_delete_ref(struct ref_store *refs, const char *msg,
|
||||||
|
const char *refname,
|
||||||
|
const unsigned char *old_sha1,
|
||||||
|
unsigned int flags);
|
||||||
int delete_ref(const char *msg, const char *refname,
|
int delete_ref(const char *msg, const char *refname,
|
||||||
const unsigned char *old_sha1, unsigned int flags);
|
const unsigned char *old_sha1, unsigned int flags);
|
||||||
|
|
||||||
|
@ -418,6 +422,8 @@ enum action_on_err {
|
||||||
* Begin a reference transaction. The reference transaction must
|
* Begin a reference transaction. The reference transaction must
|
||||||
* be freed by calling ref_transaction_free().
|
* be freed by calling ref_transaction_free().
|
||||||
*/
|
*/
|
||||||
|
struct ref_transaction *ref_store_transaction_begin(struct ref_store *refs,
|
||||||
|
struct strbuf *err);
|
||||||
struct ref_transaction *ref_transaction_begin(struct strbuf *err);
|
struct ref_transaction *ref_transaction_begin(struct strbuf *err);
|
||||||
|
|
||||||
/*
|
/*
|
||||||
|
@ -552,6 +558,9 @@ void ref_transaction_free(struct ref_transaction *transaction);
|
||||||
* ref_transaction_update(). Handle errors as requested by the `onerr`
|
* ref_transaction_update(). Handle errors as requested by the `onerr`
|
||||||
* argument.
|
* argument.
|
||||||
*/
|
*/
|
||||||
|
int refs_update_ref(struct ref_store *refs, const char *msg, const char *refname,
|
||||||
|
const unsigned char *new_sha1, const unsigned char *old_sha1,
|
||||||
|
unsigned int flags, enum action_on_err onerr);
|
||||||
int update_ref(const char *msg, const char *refname,
|
int update_ref(const char *msg, const char *refname,
|
||||||
const unsigned char *new_sha1, const unsigned char *old_sha1,
|
const unsigned char *new_sha1, const unsigned char *old_sha1,
|
||||||
unsigned int flags, enum action_on_err onerr);
|
unsigned int flags, enum action_on_err onerr);
|
||||||
|
|
|
@ -200,6 +200,7 @@ enum ref_transaction_state {
|
||||||
* as atomically as possible. This structure is opaque to callers.
|
* as atomically as possible. This structure is opaque to callers.
|
||||||
*/
|
*/
|
||||||
struct ref_transaction {
|
struct ref_transaction {
|
||||||
|
struct ref_store *ref_store;
|
||||||
struct ref_update **updates;
|
struct ref_update **updates;
|
||||||
size_t alloc;
|
size_t alloc;
|
||||||
size_t nr;
|
size_t nr;
|
||||||
|
|
Loading…
Reference in New Issue