Re: [PATCH v5 19/24] refs: new transaction related ref-store api

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On 02/22/2017 03:04 PM, Nguyễn Thái Ngọc Duy wrote:
> 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@xxxxxxxxx>
> ---
>  refs.c               | 54 ++++++++++++++++++++++++++++++++++++++++------------
>  refs.h               |  8 ++++++++
>  refs/refs-internal.h |  1 +
>  3 files changed, 51 insertions(+), 12 deletions(-)
> 
> diff --git a/refs.c b/refs.c
> index 9137ac283..851b5e125 100644
> --- a/refs.c
> +++ b/refs.c
> @@ -618,16 +618,19 @@ static int delete_pseudoref(const char *pseudoref, const unsigned char *old_sha1
>  	return 0;
>  }
>  
> -int delete_ref(const char *refname, const unsigned char *old_sha1,
> -	       unsigned int flags)
> +int refs_delete_ref(struct ref_store *refs, const char *refname,
> +		    const unsigned char *old_sha1,
> +		    unsigned int flags)
>  {
>  	struct ref_transaction *transaction;
>  	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());

Hmmm, I would have expected this to be checked via `(refs.flags &
REF_STORE_MAIN)`. I guess this is temporary; once we have compound ref
stores either version will need to be changed again. So I don't see a
strong argument for one vs. the other.

It might be more natural to make `delete_pseudoref()` take a `refs`
argument and do the check internally.

(Same comments below where `write_pseudoref()` is called.)

Michael

>  		return delete_pseudoref(refname, old_sha1);
> +	}
>  
> -	transaction = ref_transaction_begin(&err);
> +	transaction = ref_store_transaction_begin(refs, &err);
>  	if (!transaction ||
>  	    ref_transaction_delete(transaction, refname, old_sha1,
>  				   flags, NULL, &err) ||
> @@ -642,6 +645,13 @@ int delete_ref(const char *refname, const unsigned char *old_sha1,
>  	return 0;
>  }
>  
> +int delete_ref(const char *refname, const unsigned char *old_sha1,
> +	       unsigned int flags)
> +{
> +	return refs_delete_ref(get_main_ref_store(), refname,
> +			       old_sha1, flags);
> +}
> +
>  int copy_reflog_msg(char *buf, const char *msg)
>  {
>  	char *cp = buf;
> @@ -801,11 +811,20 @@ int read_ref_at(const char *refname, unsigned int flags, unsigned long at_time,
>  	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);
>  
> -	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)
> @@ -922,18 +941,20 @@ int update_ref_oid(const char *msg, const char *refname,
>  		old_oid ? old_oid->hash : NULL, flags, onerr);
>  }
>  
> -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)
> +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)
>  {
>  	struct ref_transaction *t = NULL;
>  	struct strbuf err = STRBUF_INIT;
>  	int ret = 0;
>  
>  	if (ref_type(refname) == REF_TYPE_PSEUDOREF) {
> +		assert(refs == get_main_ref_store());
>  		ret = write_pseudoref(refname, new_sha1, old_sha1, &err);
>  	} else {
> -		t = ref_transaction_begin(&err);
> +		t = ref_store_transaction_begin(refs, &err);
>  		if (!t ||
>  		    ref_transaction_update(t, refname, new_sha1, old_sha1,
>  					   flags, msg, &err) ||
> @@ -964,6 +985,15 @@ int update_ref(const char *msg, const char *refname,
>  	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)
>  {
>  	int i;
> @@ -1600,7 +1630,7 @@ int create_symref(const char *ref_target, const char *refs_heads_master,
>  int ref_transaction_commit(struct ref_transaction *transaction,
>  			   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);
>  }
> @@ -1719,7 +1749,7 @@ int reflog_expire(const char *refname, const unsigned char *sha1,
>  int initial_ref_transaction_commit(struct ref_transaction *transaction,
>  				   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);
>  }
> diff --git a/refs.h b/refs.h
> index 70d4eb87c..342cecd23 100644
> --- a/refs.h
> +++ b/refs.h
> @@ -330,6 +330,9 @@ int reflog_exists(const char *refname);
>   * 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().
>   */
> +int refs_delete_ref(struct ref_store *refs, const char *refname,
> +		    const unsigned char *old_sha1,
> +		    unsigned int flags);
>  int delete_ref(const char *refname, const unsigned char *old_sha1,
>  	       unsigned int flags);
>  
> @@ -414,6 +417,8 @@ enum action_on_err {
>   * Begin a reference transaction.  The reference transaction must
>   * 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);
>  
>  /*
> @@ -548,6 +553,9 @@ void ref_transaction_free(struct ref_transaction *transaction);
>   * ref_transaction_update(). Handle errors as requested by the `onerr`
>   * 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,
>  	       const unsigned char *new_sha1, const unsigned char *old_sha1,
>  	       unsigned int flags, enum action_on_err onerr);
> diff --git a/refs/refs-internal.h b/refs/refs-internal.h
> index 5f26208c2..690498698 100644
> --- a/refs/refs-internal.h
> +++ b/refs/refs-internal.h
> @@ -200,6 +200,7 @@ enum ref_transaction_state {
>   * as atomically as possible.  This structure is opaque to callers.
>   */
>  struct ref_transaction {
> +	struct ref_store *ref_store;
>  	struct ref_update **updates;
>  	size_t alloc;
>  	size_t nr;
> 




[Index of Archives]     [Linux Kernel Development]     [Gcc Help]     [IETF Annouce]     [DCCP]     [Netdev]     [Networking]     [Security]     [V4L]     [Bugtraq]     [Yosemite]     [MIPS Linux]     [ARM Linux]     [Linux Security]     [Linux RAID]     [Linux SCSI]     [Fedora Users]