From: Karthik Nayak Date: Fri, 04 Sep 2026 13:12:35 GMT Subject: Re: [PATCH 07/11] refs: expose function to parse reference URIs Message-ID: In-Reply-To: <20260904-b4-pks-unify-ref-storage-format-v1-7-08144e5004ff@pks.im> Patrick Steinhardt writes: > 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 > --- > 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. > 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 "[://]". */ > +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.