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

Re: [PATCH 07/11] refs: expose function to parse reference URIs

From
Karthik Nayak <karthik.188@gmail.com>
Date
Sep 4, 2026, 13:12 UTC
Message-ID
<CAOLa=ZR3xtwK_EzxjaOc0qN0oE51oDaKk4UMBTJqP+g+exr-Dg@mail.gmail.com>
In-Reply-To
<20260904-b4-pks-unify-ref-storage-format-v1-7-08144e5004ff@pks.im>
Patrick Steinhardt <ps@pks.im> writes:
Show 42 quoted lines
> In the next commit we're about to add more sites that want to parse a
> reference backends URI into a format and payload. Expose a new function
> `ref_storage_format_by_uri()` that enables this.
>
> Signed-off-by: Patrick Steinhardt <ps@pks.im>
> ---
>  refs.c  | 23 +++++++++++++++++++++++
>  refs.h  |  4 ++++
>  setup.c | 48 ++++++++++++------------------------------------
>  3 files changed, 39 insertions(+), 36 deletions(-)
>
> diff --git a/refs.c b/refs.c
> index 92d5df5b71..951db56113 100644
> --- a/refs.c
> +++ b/refs.c
> @@ -54,6 +54,29 @@ enum ref_storage_format ref_storage_format_by_name(const char *name)
>  	return REF_STORAGE_FORMAT_UNKNOWN;
>  }
>
> +enum ref_storage_format ref_storage_format_by_uri(const char *uri,
> +						  char **payload)
> +{
> +	enum ref_storage_format format;
> +	const char *schema_end;
> +	char *name;
> +
> +	schema_end = strstr(uri, "://");
> +	if (!schema_end) {
> +		name = xstrdup(uri);
> +		if (payload)
> +			*payload = NULL;
> +	} else {
> +		name = xstrndup(uri, schema_end - uri);
> +		if (payload)
> +			*payload = xstrdup(schema_end + 3);
> +	}
> +
> +	format = ref_storage_format_by_name(name);
> +	free(name);
> +	return format;
> +}
> +

Okay, we move the existing code in setup.c to the refs.c and clean it up.

Show 112 quoted lines
>  const char *ref_storage_format_to_name(enum ref_storage_format ref_storage_format)
>  {
>  	const struct ref_storage_be *be = find_ref_storage_backend(ref_storage_format);
> diff --git a/refs.h b/refs.h
> index 9979446d15..ee3b8a62ef 100644
> --- a/refs.h
> +++ b/refs.h
> @@ -17,6 +17,10 @@ struct worktree;
>  enum ref_storage_format ref_storage_format_by_name(const char *name);
>  const char *ref_storage_format_to_name(enum ref_storage_format ref_storage_format);
>
> +/* Parse a reference storage URI in the format "<format>[://<payload>]". */
> +enum ref_storage_format ref_storage_format_by_uri(const char *uri,
> +						  char **payload);
> +
>  enum ref_transaction_error {
>  	/* Default error code */
>  	REF_TRANSACTION_ERROR_GENERIC = -1,
> diff --git a/setup.c b/setup.c
> index dfe05d9a03..3be7dac452 100644
> --- a/setup.c
> +++ b/setup.c
> @@ -632,21 +632,6 @@ static enum extension_result handle_extension_v0(const char *var,
>  		return EXTENSION_UNKNOWN;
>  }
>
> -static void parse_reference_uri(const char *value, char **format,
> -				char **payload)
> -{
> -	const char *schema_end;
> -
> -	schema_end = strstr(value, "://");
> -	if (!schema_end) {
> -		*format = xstrdup(value);
> -		*payload = NULL;
> -	} else {
> -		*format = xstrndup(value, schema_end - value);
> -		*payload = xstrdup_or_null(schema_end + 3);
> -	}
> -}
> -
>  /*
>   * Record any new extensions in this function.
>   */
> @@ -689,16 +674,13 @@ static enum extension_result handle_extension(const char *var,
>  		return EXTENSION_OK;
>  	} else if (!strcmp(ext, "refstorage")) {
>  		unsigned int format;
> -		char *format_str;
>
>  		if (!value)
>  			return config_error_nonbool(var);
>
> -		parse_reference_uri(value, &format_str,
> -				    &data->ref_storage_payload);
> -
> -		format = ref_storage_format_by_name(format_str);
> -		free(format_str);
> +		FREE_AND_NULL(data->ref_storage_payload);
> +		format = ref_storage_format_by_uri(value,
> +						   &data->ref_storage_payload);
>
>  		if (format == REF_STORAGE_FORMAT_UNKNOWN)
>  			return error(_("invalid value for '%s': '%s'"),
> @@ -2069,16 +2051,12 @@ const char *setup_git_directory_gently(struct repository *repo, int *nongit_ok)
>  			 */
>  			ref_backend_uri = getenv(GIT_REFERENCE_BACKEND_ENVIRONMENT);
>  			if (ref_backend_uri) {
> -				char *format;
> -
> -				free(discovery.format.ref_storage_payload);
> -
> -				parse_reference_uri(ref_backend_uri, &format, &discovery.format.ref_storage_payload);
> -				discovery.format.ref_storage_format = ref_storage_format_by_name(format);
> +				FREE_AND_NULL(discovery.format.ref_storage_payload);
> +				discovery.format.ref_storage_format =
> +					ref_storage_format_by_uri(ref_backend_uri,
> +								  &discovery.format.ref_storage_payload);
>  				if (discovery.format.ref_storage_format == REF_STORAGE_FORMAT_UNKNOWN)
> -					die(_("unknown ref storage format: '%s'"), format);
> -
> -				free(format);
> +					die(_("unknown ref storage format: '%s'"), ref_backend_uri);
>  			}
>
>  			if (apply_repository_format(repo, &discovery.format,
> @@ -2806,18 +2784,16 @@ static void repository_format_configure(struct repository_format *repo_fmt,
>
>  	ref_backend_uri = getenv(GIT_REFERENCE_BACKEND_ENVIRONMENT);
>  	if (ref_backend_uri) {
> -		char *backend, *payload;
>  		enum ref_storage_format format;
> +		char *payload;
>
> -		parse_reference_uri(ref_backend_uri, &backend, &payload);
> -		format = ref_storage_format_by_name(backend);
> +		format = ref_storage_format_by_uri(ref_backend_uri, &payload);
>  		if (format == REF_STORAGE_FORMAT_UNKNOWN)
> -			die(_("unknown ref storage format: '%s'"), backend);
> +			die(_("unknown ref storage format: '%s'"), ref_backend_uri);
>
>  		repo_fmt->ref_storage_format = format;
> +		free(repo_fmt->ref_storage_payload);
>  		repo_fmt->ref_storage_payload = payload;
> -
> -		free(backend);
>  	}
>  }
>
>
> --
> 2.55.0.1007.g17ff1f9808.dirty
Then, modify all the call sites to use the new function. Makes sense.
Previous: Patrick SteinhardtNext: Patrick Steinhardt
Message 11 of 20 in “Fix inconsistent ref storage format terminology”
  1. 00/11 Fix inconsistent ref storage format terminologyPatrick Steinhardt, Sep 4, 2026
  2. 01/11 builtin/init: rename "--ref-format=" to "--ref-storage="Patrick Steinhardt, Sep 4, 2026
  3. Karthik NayakSep 4, 2026
  4. Patrick SteinhardtSep 7, 2026
  5. 02/11 builtin/clone: rename "--ref-format=" to "--ref-storage="Patrick Steinhardt, Sep 4, 2026
  6. 03/11 builtin/refs: rename "--ref-format=" to "--ref-storage="Patrick Steinhardt, Sep 4, 2026
  7. 04/11 builtin/submodule: rename "--ref-format=" to "--ref-storage="Patrick Steinhardt, Sep 4, 2026
  8. 05/11 builtin/rev-parse: rename "--show-ref-format" to "--show-ref-storage"Patrick Steinhardt, Sep 4, 2026
  9. 06/11 help: rename "default-ref-format" to "default-ref-storage"Patrick Steinhardt, Sep 4, 2026
  10. 07/11 refs: expose function to parse reference URIsPatrick Steinhardt, Sep 4, 2026
  11. Karthik NayakSep 4, 2026
  12. 08/11 setup: refactor how we configure the ref storage formatPatrick Steinhardt, Sep 4, 2026
  13. 09/11 setup: rename ref storage format environment variablesPatrick Steinhardt, Sep 4, 2026
  14. Karthik NayakSep 4, 2026
  15. Patrick SteinhardtSep 7, 2026
  16. 10/11 setup: rename "init.defaultRefFormat" to "init.defaultRefStorage"Patrick Steinhardt, Sep 4, 2026
  17. 11/11 setup: allow "git init --ref-storage=" to specify a payloadPatrick Steinhardt, Sep 4, 2026
  18. Karthik NayakSep 4, 2026
  19. Junio C HamanoSep 4, 2026
  20. Patrick SteinhardtSep 7, 2026

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.