git/list[1] front-page[2] threads[3] people[4] search[5] about
 

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

From
Michael Haggerty <mhagger@alum.mit.edu>
Date
Dec 23, 2015, 06:27 UTC
Message-ID
<567A3EB6.9000405@alum.mit.edu>
In-Reply-To
<1449102921-7707-12-git-send-email-dturner@twopensource.com>
On 12/03/2015 01:35 AM, David Turner wrote:
Show 30 quoted lines
> 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 <dturner@twopensource.com>
> ---
>  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);
Show 54 quoted lines
> +{
> +	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, &affected_refnames, err)) {
> +		ret = TRANSACTION_GENERIC_ERROR;
> +		goto done;
> +	}
> +
> +	ret = the_refs_backend->transaction_commit(transaction,
> +						   &affected_refnames, err);
> +done:
> +	string_list_clear(&affected_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
mhagger@alum.mit.edu
Previous: David TurnerNext: David Turner
Message 33 of 73 in “LMDB refs backend atop pre-vtable”
  1. 00/16 LMDB refs backend atop pre-vtableDavid Turner, Dec 3, 2015
  2. 01/16 refs: add a backend method structure with transaction functionsDavid Turner, Dec 3, 2015
  3. Junio C HamanoDec 5, 2015
  4. 02/16 refs: add methods for misc ref operationsDavid Turner, Dec 3, 2015
  5. Junio C HamanoDec 11, 2015
  6. David TurnerDec 11, 2015
  7. Junio C HamanoDec 11, 2015
  8. David TurnerDec 11, 2015
  9. Junio C HamanoDec 12, 2015
  10. David TurnerDec 12, 2015
  11. Howard ChuDec 18, 2015
  12. 03/16 refs: add methods for the ref iteratorsDavid Turner, Dec 3, 2015
  13. David AguilarJan 3, 2016
  14. Junio C HamanoJan 4, 2016
  15. Michael HaggertyJan 5, 2016
  16. Junio C HamanoJan 5, 2016
  17. Ronnie SahlbergJan 4, 2016
  18. Junio C HamanoJan 4, 2016
  19. Jeff KingJan 5, 2016
  20. Junio C HamanoJan 5, 2016
  21. 04/16 refs: add do_for_each_per_worktree_refDavid Turner, Dec 3, 2015
  22. Junio C HamanoDec 11, 2015
  23. David TurnerDec 12, 2015
  24. 05/16 refs: add methods for reflogDavid Turner, Dec 3, 2015
  25. 06/16 refs: add method for initial ref transaction commitDavid Turner, Dec 3, 2015
  26. 07/16 refs: add method for delete_refsDavid Turner, Dec 3, 2015
  27. 08/16 refs: add methods to init refs backend and dbDavid Turner, Dec 3, 2015
  28. Michael HaggertyDec 23, 2015
  29. David TurnerDec 23, 2015
  30. 09/16 refs: add method to rename refsDavid Turner, Dec 3, 2015
  31. 10/16 refs: make lock genericDavid Turner, Dec 3, 2015
  32. 11/16 refs: move duplicate check to common codeDavid Turner, Dec 3, 2015
  33. Michael HaggertyDec 23, 2015
  34. David TurnerJan 5, 2016
  35. 12/16 refs: always handle non-normal refs in files backendDavid Turner, Dec 3, 2015
  36. Michael HaggertyDec 23, 2015
  37. David TurnerJan 6, 2016
  38. 1/3 refs: allow log-only updatesDavid Turner, Jan 6, 2016
  39. 2/3 refs: resolve symbolic refs firstDavid Turner, Jan 6, 2016
  40. 3/3 refs: always handle non-normal refs in files backendDavid Turner, Jan 6, 2016
  41. David TurnerJan 8, 2016
  42. David TurnerJan 6, 2016
  43. 13/16 init: allow alternate backends to be set for new reposDavid Turner, Dec 3, 2015
  44. Junio C HamanoDec 5, 2015
  45. Duy NguyenDec 5, 2015
  46. Jeff KingDec 5, 2015
  47. David TurnerDec 8, 2015
  48. Michael HaggertyDec 23, 2015
  49. Jeff KingDec 23, 2015
  50. Jeff KingDec 10, 2015
  51. David TurnerDec 10, 2015
  52. clone: use child_process for recursive checkoutsMichael Haggerty, Dec 23, 2015
  53. David TurnerJan 6, 2016
  54. Michael HaggertyDec 23, 2015
  55. David TurnerJan 5, 2016
  56. Junio C HamanoJan 5, 2016
  57. David TurnerJan 5, 2016
  58. Michael HaggertyJan 6, 2016
  59. Duy NguyenJan 6, 2016
  60. Shawn PearceJan 7, 2016
  61. 14/16 refs: allow ref backend to be set for cloneDavid Turner, Dec 3, 2015
  62. Michael HaggertyDec 23, 2015
  63. Eric SunshineDec 23, 2015
  64. 15/16 refs: add LMDB refs backendDavid Turner, Dec 3, 2015
  65. Junio C HamanoDec 5, 2015
  66. David TurnerDec 5, 2015
  67. Jonathan NiederDec 17, 2015
  68. David TurnerDec 17, 2015
  69. Jonathan NiederDec 17, 2015
  70. Michael HaggertyDec 23, 2015
  71. David TurnerJan 8, 2016
  72. 16/16 refs: tests for lmdb backendDavid Turner, Dec 3, 2015
  73. David TurnerDec 22, 2015

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.