Subject: Re: [PATCH v2 1/6] refs: open-code deletion of packed refs

From: Junio C Hamano <[email-masked]>
Date: Fri, 07 Jan 2022 17:51:04 -0800

Patrick Steinhardt <[email-masked]> writes:

> When deleting loose refs, then we also have to delete the refs in the
> packed backend. This is done by calling `refs_delete_refs()`, which
> then uses the packed-backend's logic to delete refs. This doesn't allow
> us to exercise any control over the reference transaction which is being
> created in the packed backend, which is required in a subsequent commit.
>
> Extract a new function `packed_refs_delete_refs()`, which hosts most of
> the logic to delete refs except for creating the transaction itself.
> Like this, we can easily create the transactionion in the files backend

ionion?

> and thus exert more control over it.
>
> Signed-off-by: Patrick Steinhardt <[email-masked]>
> ---

The title might be technically correct, but I think the primary
point, the benefit this step brings to the system, is now we have a
helper function we can call in a transaction we create ourselves.

    refs: extract packed_refs_delete_refs() to be used in our own transaction

or something along that line, perhaps?  I dunno.

>  refs/files-backend.c  | 12 +++++++++---
>  refs/packed-backend.c | 28 +++++++++++++++++++++-------
>  refs/packed-backend.h |  6 ++++++
>  3 files changed, 36 insertions(+), 10 deletions(-)
>
> diff --git a/refs/files-backend.c b/refs/files-backend.c
> index 90b671025a..5795e54020 100644
> --- a/refs/files-backend.c
> +++ b/refs/files-backend.c
> @@ -1249,6 +1249,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;
>  
> @@ -1258,10 +1259,14 @@ 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, &err);
> +	if (!transaction)
> +		goto error;
> +
> +	result = packed_refs_delete_refs(refs->packed_ref_store,
> +					 transaction, msg, refnames, flags);
> +	if (result)
>  		goto error;
> -	}

Without looking at the later patches, my guess is that we'd create a
single transaction for packed-refs backend and then issue multiple
delete requests in that single transaction?

Ah, no.  We are already deleting multiple refs in one go, so that is
not what is happening here.  Makes readers a bit curious.  Hopefully
we will find it out in a later patch.

> diff --git a/refs/packed-backend.h b/refs/packed-backend.h
> index f61a73ec25..5e0dd7d08e 100644
> --- a/refs/packed-backend.h
> +++ b/refs/packed-backend.h
> @@ -27,6 +27,12 @@ int packed_refs_lock(struct ref_store *ref_store, int flags, struct strbuf *err)
>  void packed_refs_unlock(struct ref_store *ref_store);
>  int packed_refs_is_locked(struct ref_store *ref_store);
>  
> +int packed_refs_delete_refs(struct ref_store *ref_store,
> +			    struct ref_transaction *transaction,
> +			    const char *msg,
> +			    struct string_list *refnames,
> +			    unsigned int flags);
> +
>  /*
>   * Return true if `transaction` really needs to be carried out against
>   * the specified packed_ref_store, or false if it can be skipped