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

[PATCH v3 08/13] refs: expose function to parse reference URIs

From
Patrick Steinhardt <ps@pks.im>
Date
Sep 9, 2026, 11:12 UTC
Message-ID
<20260909-b4-pks-unify-ref-storage-format-v3-8-ca041fb40ad8@pks.im>
In-Reply-To
<20260909-b4-pks-unify-ref-storage-format-v3-0-ca041fb40ad8@pks.im>

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;
+}
+
 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.1074.ge7621b4bad.dirty
Previous: Patrick SteinhardtNext: Patrick Steinhardt
Message 12 of 20 in “Fix inconsistent ref storage format terminology”
  1. 00/13 Fix inconsistent ref storage format terminologyPatrick Steinhardt, Sep 9, 2026
  2. 01/13 parse-options: allow for hidden aliasesPatrick Steinhardt, Sep 9, 2026
  3. Karthik NayakSep 14, 2026
  4. Kaartic SivaraamSep 15, 2026
  5. Junio C HamanoSep 15, 2026
  6. 02/13 builtin/init: rename "--ref-format=" to "--ref-storage-format="Patrick Steinhardt, Sep 9, 2026
  7. 03/13 builtin/clone: rename "--ref-format=" to "--ref-storage-format="Patrick Steinhardt, Sep 9, 2026
  8. 04/13 builtin/refs: rename "--ref-format=" to "--ref-storage-format="Patrick Steinhardt, Sep 9, 2026
  9. 05/13 builtin/submodule: rename "--ref-format=" to "--ref-storage-format="Patrick Steinhardt, Sep 9, 2026
  10. 06/13 builtin/rev-parse: rename "--show-ref-format" to "--show-ref-storage-format"Patrick Steinhardt, Sep 9, 2026
  11. 07/13 help: rename "default-ref-format" to "default-ref-storage-format"Patrick Steinhardt, Sep 9, 2026
  12. 08/13 refs: expose function to parse reference URIsPatrick Steinhardt, Sep 9, 2026
  13. 09/13 setup: refactor how we configure the ref storage formatPatrick Steinhardt, Sep 9, 2026
  14. 10/13 setup: rename ref storage format environment variablesPatrick Steinhardt, Sep 9, 2026
  15. 11/13 t: rename GIT_TEST_DEFAULT_REF_FORMATPatrick Steinhardt, Sep 9, 2026
  16. 12/13 setup: rename "init.defaultRefFormat" to "init.defaultRefStorageFormat"Patrick Steinhardt, Sep 9, 2026
  17. 13/13 setup: allow "--ref-storage-format=" to specify a payloadPatrick Steinhardt, Sep 9, 2026
  18. Karthik NayakSep 14, 2026
  19. Junio C HamanoSep 14, 2026
  20. Kaartic SivaraamSep 14, 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.