Re: [PATCH 11/16] refs: move duplicate check to common code

2016-01-05 Thread David Turner
On Wed, 2015-12-23 at 07:27 +0100, Michael Haggerty wrote:
> > +static int ref_update_reject_duplicates(struct ref_transaction
> > *transaction,
> > +   struct string_list
> > *refnames,
> > +   struct strbuf *err)
> 
> I like that you extract this code into a function. Though it feels
> awkward to have a function called "ref_update_reject_duplicates()"
> that
> has a side effect of filling the names into a string list. I think it
> would feel more natural to call the function get_affected_refnames(),
> and treat the duplicate check as an extra bonus.


Renamed, thanks.

> >  int ref_transaction_commit(struct ref_transaction *transaction,
> >struct strbuf *err)
> >  {
> > -   return the_refs_backend->transaction_commit(transaction,
> > err);
> > +   int ret = -1;
> > +   struct string_list affected_refnames =
> > STRING_LIST_INIT_NODUP;
> > +
> > +   assert(err);
> > +
> > +   if (transaction->state != REF_TRANSACTION_OPEN)
> > +   die("BUG: commit called for transaction that is
> > not open");
> > +
> > +   if (!transaction->nr) {
> > +   transaction->state = REF_TRANSACTION_CLOSED;
> > +   return 0;
> > +   }
> > +
> > +   if (ref_update_reject_duplicates(transaction,
> > _refnames, err)) {
> > +   ret = TRANSACTION_GENERIC_ERROR;
> > +   goto done;
> > +   }
> > +
> > +   ret = the_refs_backend->transaction_commit(transaction,
> > + 
> >  _refnames, err);
> > +done:
> > +   string_list_clear(_refnames, 0);
> > +   return ret;
> >  }
> 
> Here you are avoiding a small amount of code duplication by calling
> ref_update_reject_duplicates() here rather than in the backend
> -specific
> code. But you are doing so at the cost of having to compute
> affected_refnames here and pass it (redundantly) to the backend's
> transaction_commit function. This increases the coupling between
> these
> functions, which in my opinion is worse than the small amount of code
> duplication. But maybe it's just me.

Hm.  I'm trying to follow the principle that the backends should do as
little as possible (and that the common code should do as much as
possible).  This reduces the cognitive load when writing new backends -
- a new backend author need not know that the list of ref updates must
be unique.

What do others think? 
--
To unsubscribe from this list: send the line "unsubscribe git" in
the body of a message to majord...@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html


Re: [PATCH 11/16] refs: move duplicate check to common code

2015-12-22 Thread Michael Haggerty
On 12/03/2015 01:35 AM, David Turner wrote:
> The check for duplicate refnames in a transaction is needed for
> all backends, so move it to the common code.
> 
> ref_transaction_commit_fn gains a new argument, the sorted
> string_list of affected refnames.
> 
> Signed-off-by: David Turner 
> ---
>  refs.c   | 71 
> ++--
>  refs/files-backend.c | 57 -
>  refs/refs-internal.h |  1 +
>  3 files changed, 75 insertions(+), 54 deletions(-)
> 
> diff --git a/refs.c b/refs.c
> index 1b79630..808053f 100644
> --- a/refs.c
> +++ b/refs.c
> @@ -1093,6 +1093,37 @@ const char *find_descendant_ref(const char *dirname,
>   return NULL;
>  }
>  
> +/*
> + * Return 1 if there are any duplicate refnames in the updates in
> + * `transaction`, and fill in err with an appropriate error message.
> + * Fill in `refnames` with the refnames from the transaction.
> + */
> +
> +static int ref_update_reject_duplicates(struct ref_transaction *transaction,
> + struct string_list *refnames,
> + struct strbuf *err)

I like that you extract this code into a function. Though it feels
awkward to have a function called "ref_update_reject_duplicates()" that
has a side effect of filling the names into a string list. I think it
would feel more natural to call the function get_affected_refnames(),
and treat the duplicate check as an extra bonus.

You could go even farther and split it into two functions,

void get_affected_refnames(struct ref_transaction *transaction,
   struct string_list *refnames);
int ref_update_reject_duplicates(struct string_list *refnames,
 struct strbuf *err);

> +{
> + int i, n = transaction->nr;
> + struct ref_update **updates;
> +
> + assert(err);
> +
> + updates = transaction->updates;
> + /* Fail if a refname appears more than once in the transaction: */
> + for (i = 0; i < n; i++)
> + string_list_append(refnames, updates[i]->refname);
> + string_list_sort(refnames);
> +
> + for (i = 1; i < n; i++)
> + if (!strcmp(refnames->items[i - 1].string, 
> refnames->items[i].string)) {
> + strbuf_addf(err,
> + "Multiple updates for ref '%s' not 
> allowed.",
> + refnames->items[i].string);
> + return 1;
> + }
> + return 0;
> +}
> +
>  /* backend functions */
>  int refs_init_db(struct strbuf *err, int shared)
>  {
> @@ -1102,7 +1133,29 @@ int refs_init_db(struct strbuf *err, int shared)
>  int ref_transaction_commit(struct ref_transaction *transaction,
>  struct strbuf *err)
>  {
> - return the_refs_backend->transaction_commit(transaction, err);
> + int ret = -1;
> + struct string_list affected_refnames = STRING_LIST_INIT_NODUP;
> +
> + assert(err);
> +
> + if (transaction->state != REF_TRANSACTION_OPEN)
> + die("BUG: commit called for transaction that is not open");
> +
> + if (!transaction->nr) {
> + transaction->state = REF_TRANSACTION_CLOSED;
> + return 0;
> + }
> +
> + if (ref_update_reject_duplicates(transaction, _refnames, err)) 
> {
> + ret = TRANSACTION_GENERIC_ERROR;
> + goto done;
> + }
> +
> + ret = the_refs_backend->transaction_commit(transaction,
> +_refnames, err);
> +done:
> + string_list_clear(_refnames, 0);
> + return ret;
>  }

Here you are avoiding a small amount of code duplication by calling
ref_update_reject_duplicates() here rather than in the backend-specific
code. But you are doing so at the cost of having to compute
affected_refnames here and pass it (redundantly) to the backend's
transaction_commit function. This increases the coupling between these
functions, which in my opinion is worse than the small amount of code
duplication. But maybe it's just me.

The check of transaction->state, on the other hand, makes sense here.

> [...]

Michael

-- 
Michael Haggerty
mhag...@alum.mit.edu

--
To unsubscribe from this list: send the line "unsubscribe git" in
the body of a message to majord...@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html


[PATCH 11/16] refs: move duplicate check to common code

2015-12-02 Thread David Turner
The check for duplicate refnames in a transaction is needed for
all backends, so move it to the common code.

ref_transaction_commit_fn gains a new argument, the sorted
string_list of affected refnames.

Signed-off-by: David Turner 
---
 refs.c   | 71 ++--
 refs/files-backend.c | 57 -
 refs/refs-internal.h |  1 +
 3 files changed, 75 insertions(+), 54 deletions(-)

diff --git a/refs.c b/refs.c
index 1b79630..808053f 100644
--- a/refs.c
+++ b/refs.c
@@ -1093,6 +1093,37 @@ const char *find_descendant_ref(const char *dirname,
return NULL;
 }
 
+/*
+ * Return 1 if there are any duplicate refnames in the updates in
+ * `transaction`, and fill in err with an appropriate error message.
+ * Fill in `refnames` with the refnames from the transaction.
+ */
+
+static int ref_update_reject_duplicates(struct ref_transaction *transaction,
+   struct string_list *refnames,
+   struct strbuf *err)
+{
+   int i, n = transaction->nr;
+   struct ref_update **updates;
+
+   assert(err);
+
+   updates = transaction->updates;
+   /* Fail if a refname appears more than once in the transaction: */
+   for (i = 0; i < n; i++)
+   string_list_append(refnames, updates[i]->refname);
+   string_list_sort(refnames);
+
+   for (i = 1; i < n; i++)
+   if (!strcmp(refnames->items[i - 1].string, 
refnames->items[i].string)) {
+   strbuf_addf(err,
+   "Multiple updates for ref '%s' not 
allowed.",
+   refnames->items[i].string);
+   return 1;
+   }
+   return 0;
+}
+
 /* backend functions */
 int refs_init_db(struct strbuf *err, int shared)
 {
@@ -1102,7 +1133,29 @@ int refs_init_db(struct strbuf *err, int shared)
 int ref_transaction_commit(struct ref_transaction *transaction,
   struct strbuf *err)
 {
-   return the_refs_backend->transaction_commit(transaction, err);
+   int ret = -1;
+   struct string_list affected_refnames = STRING_LIST_INIT_NODUP;
+
+   assert(err);
+
+   if (transaction->state != REF_TRANSACTION_OPEN)
+   die("BUG: commit called for transaction that is not open");
+
+   if (!transaction->nr) {
+   transaction->state = REF_TRANSACTION_CLOSED;
+   return 0;
+   }
+
+   if (ref_update_reject_duplicates(transaction, _refnames, err)) 
{
+   ret = TRANSACTION_GENERIC_ERROR;
+   goto done;
+   }
+
+   ret = the_refs_backend->transaction_commit(transaction,
+  _refnames, err);
+done:
+   string_list_clear(_refnames, 0);
+   return ret;
 }
 
 int delete_refs(struct string_list *refnames)
@@ -1270,5 +1323,19 @@ int reflog_expire(const char *refname, const unsigned 
char *sha1,
 int initial_ref_transaction_commit(struct ref_transaction *transaction,
   struct strbuf *err)
 {
-   return the_refs_backend->initial_transaction_commit(transaction, err);
+
+   struct string_list affected_refnames = STRING_LIST_INIT_NODUP;
+   int ret;
+
+   if (ref_update_reject_duplicates(transaction,
+_refnames, err)) {
+   ret = TRANSACTION_GENERIC_ERROR;
+   goto done;
+   }
+   ret = the_refs_backend->initial_transaction_commit(transaction,
+  _refnames,
+  err);
+done:
+   string_list_clear(_refnames, 0);
+   return ret;
 }
diff --git a/refs/files-backend.c b/refs/files-backend.c
index 22a6f24..59e2ec1 100644
--- a/refs/files-backend.c
+++ b/refs/files-backend.c
@@ -3130,24 +3130,8 @@ static int files_for_each_reflog(each_ref_fn fn, void 
*cb_data)
return retval;
 }
 
-static int ref_update_reject_duplicates(struct string_list *refnames,
-   struct strbuf *err)
-{
-   int i, n = refnames->nr;
-
-   assert(err);
-
-   for (i = 1; i < n; i++)
-   if (!strcmp(refnames->items[i - 1].string, 
refnames->items[i].string)) {
-   strbuf_addf(err,
-   "Multiple updates for ref '%s' not 
allowed.",
-   refnames->items[i].string);
-   return 1;
-   }
-   return 0;
-}
-
 static int files_transaction_commit(struct ref_transaction *transaction,
+   struct string_list *affected_refnames,
struct strbuf *err)
 {
int ret = 0, i;
@@ -3155,26 +3139,6 @@ static int files_transaction_commit(struct 
ref_transaction