From 991ec2741d723d0737bd5410f01b7f98b89d9186 Mon Sep 17 00:00:00 2001 From: Patrick Steinhardt Date: Thu, 16 Jul 2026 14:38:02 +0200 Subject: [PATCH 1/3] refspec: group related structures and functions Reorganize the refspec header a bit so that structures and their related functions are grouped closer together. While at it, fix a couple of style violations. Signed-off-by: Patrick Steinhardt Signed-off-by: Junio C Hamano --- refspec.h | 26 ++++++++++++++------------ 1 file changed, 14 insertions(+), 12 deletions(-) diff --git a/refspec.h b/refspec.h index 8b04f9995e..832d6f923c 100644 --- a/refspec.h +++ b/refspec.h @@ -1,6 +1,9 @@ #ifndef REFSPEC_H #define REFSPEC_H +struct string_list; +struct strvec; + #define TAG_REFSPEC "refs/tags/*:refs/tags/*" /** @@ -30,10 +33,9 @@ struct refspec_item { char *raw; }; -struct string_list; - -#define REFSPEC_INIT_FETCH { .fetch = 1 } -#define REFSPEC_INIT_PUSH { .fetch = 0 } +int refspec_item_init_fetch(struct refspec_item *item, const char *refspec); +int refspec_item_init_push(struct refspec_item *item, const char *refspec); +void refspec_item_clear(struct refspec_item *item); /** * An array of strings can be parsed into a struct refspec using @@ -47,20 +49,20 @@ struct refspec { unsigned fetch : 1; }; -int refspec_item_init_fetch(struct refspec_item *item, const char *refspec); -int refspec_item_init_push(struct refspec_item *item, const char *refspec); -void refspec_item_clear(struct refspec_item *item); +#define REFSPEC_INIT_FETCH { .fetch = 1 } +#define REFSPEC_INIT_PUSH { .fetch = 0 } + void refspec_init_fetch(struct refspec *rs); void refspec_init_push(struct refspec *rs); +void refspec_clear(struct refspec *rs); + void refspec_append(struct refspec *rs, const char *refspec); __attribute__((format (printf,2,3))) void refspec_appendf(struct refspec *rs, const char *fmt, ...); void refspec_appendn(struct refspec *rs, const char **refspecs, int nr); -void refspec_clear(struct refspec *rs); int valid_fetch_refspec(const char *refspec); -struct strvec; /* * Determine what values to pass to the peer in ref-prefix lines * (see linkgit:gitprotocol-v2[5]). @@ -76,7 +78,7 @@ int refname_matches_negative_refspec_item(const char *refname, struct refspec *r * Returns 1 if refname matches pattern, 0 otherwise. */ int match_refname_with_pattern(const char *pattern, const char *refname, - const char *replacement, char **result); + const char *replacement, char **result); /* * Queries a refspec for a match and updates the query item. @@ -89,8 +91,8 @@ int refspec_find_match(struct refspec *rs, struct refspec_item *query); * list. */ void refspec_find_all_matches(struct refspec *rs, - struct refspec_item *query, - struct string_list *results); + struct refspec_item *query, + struct string_list *results); /* * Remove all entries in the input list which match any negative refspec in From bb71c3f19e6a693fc1d32e4448be8520132d946e Mon Sep 17 00:00:00 2001 From: Patrick Steinhardt Date: Thu, 16 Jul 2026 14:38:03 +0200 Subject: [PATCH 2/3] refspec: let callers pass in hash algorithm when parsing items When parsing a refspec item we need to know about the hash algorithm used by the repository so that we can decide whether or not a given string is an exact object ID. We use `the_hash_algo` for this, which makes the code implicitly depend on `the_repository`. Refactor `refspec_item_init_fetch()`, `refspec_item_init_push()` and `valid_fetch_refspec()` so that callers have to pass in the hash algorithm explicitly and adapt callers accordingly. For now, all of the callers simply pass `the_hash_algo`, so there is no change in behaviour. Signed-off-by: Patrick Steinhardt Signed-off-by: Junio C Hamano --- builtin/fetch.c | 3 ++- builtin/pull.c | 2 +- refspec.c | 30 +++++++++++++++++------------- refspec.h | 9 ++++++--- remote.c | 2 +- 5 files changed, 27 insertions(+), 19 deletions(-) diff --git a/builtin/fetch.c b/builtin/fetch.c index 8e676b79ba..1d4a129039 100644 --- a/builtin/fetch.c +++ b/builtin/fetch.c @@ -601,7 +601,8 @@ static struct ref *get_ref_map(struct remote *remote, struct refspec_item tag_refspec; /* also fetch all tags */ - refspec_item_init_push(&tag_refspec, TAG_REFSPEC); + refspec_item_init_push(&tag_refspec, TAG_REFSPEC, + the_hash_algo); get_fetch_map(remote_refs, &tag_refspec, &tail, 0); refspec_item_clear(&tag_refspec); } else if (tags == TAGS_DEFAULT && *autotags) { diff --git a/builtin/pull.c b/builtin/pull.c index d49b09114a..db3ee0aab3 100644 --- a/builtin/pull.c +++ b/builtin/pull.c @@ -612,7 +612,7 @@ static const char *get_tracking_branch(const char *remote, const char *refspec) const char *spec_src; const char *merge_branch; - if (!refspec_item_init_fetch(&spec, refspec)) + if (!refspec_item_init_fetch(&spec, refspec, the_hash_algo)) die(_("invalid refspec '%s'"), refspec); spec_src = spec.src; if (!*spec_src || !strcmp(spec_src, "HEAD")) diff --git a/refspec.c b/refspec.c index fb89bce1db..33a6fb8e45 100644 --- a/refspec.c +++ b/refspec.c @@ -16,7 +16,8 @@ * Parses the provided refspec 'refspec' and populates the refspec_item 'item'. * Returns 1 if successful and 0 if the refspec is invalid. */ -static int parse_refspec(struct refspec_item *item, const char *refspec, int fetch) +static int parse_refspec(struct refspec_item *item, const char *refspec, + const struct git_hash_algo *algo, int fetch) { size_t llen; int is_glob; @@ -84,7 +85,7 @@ static int parse_refspec(struct refspec_item *item, const char *refspec, int fet */ if (!*item->src) return 0; /* negative refspecs must not be empty */ - else if (llen == the_hash_algo->hexsz && !get_oid_hex(item->src, &unused)) + else if (llen == algo->hexsz && !get_oid_hex_algop(item->src, &unused, algo)) return 0; /* negative refspecs cannot be exact sha1 */ else if (!check_refname_format(item->src, flags)) ; /* valid looking ref is ok */ @@ -101,7 +102,7 @@ static int parse_refspec(struct refspec_item *item, const char *refspec, int fet /* LHS */ if (!*item->src) ; /* empty is ok; it means "HEAD" */ - else if (llen == the_hash_algo->hexsz && !get_oid_hex(item->src, &unused)) + else if (llen == algo->hexsz && !get_oid_hex_algop(item->src, &unused, algo)) item->exact_sha1 = 1; /* ok */ else if (!check_refname_format(item->src, flags)) ; /* valid looking ref is ok */ @@ -154,21 +155,23 @@ static int parse_refspec(struct refspec_item *item, const char *refspec, int fet } static int refspec_item_init(struct refspec_item *item, const char *refspec, - int fetch) + const struct git_hash_algo *algo, int fetch) { memset(item, 0, sizeof(*item)); item->raw = xstrdup(refspec); - return parse_refspec(item, refspec, fetch); + return parse_refspec(item, refspec, algo, fetch); } -int refspec_item_init_fetch(struct refspec_item *item, const char *refspec) +int refspec_item_init_fetch(struct refspec_item *item, const char *refspec, + const struct git_hash_algo *algo) { - return refspec_item_init(item, refspec, 1); + return refspec_item_init(item, refspec, algo, 1); } -int refspec_item_init_push(struct refspec_item *item, const char *refspec) +int refspec_item_init_push(struct refspec_item *item, const char *refspec, + const struct git_hash_algo *algo) { - return refspec_item_init(item, refspec, 0); + return refspec_item_init(item, refspec, algo, 0); } void refspec_item_clear(struct refspec_item *item) @@ -200,9 +203,9 @@ void refspec_append(struct refspec *rs, const char *refspec) int ret; if (rs->fetch) - ret = refspec_item_init_fetch(&item, refspec); + ret = refspec_item_init_fetch(&item, refspec, the_hash_algo); else - ret = refspec_item_init_push(&item, refspec); + ret = refspec_item_init_push(&item, refspec, the_hash_algo); if (!ret) die(_("invalid refspec '%s'"), refspec); @@ -246,10 +249,11 @@ void refspec_clear(struct refspec *rs) rs->fetch = 0; } -int valid_fetch_refspec(const char *fetch_refspec_str) +int valid_fetch_refspec(const char *fetch_refspec_str, + const struct git_hash_algo *algo) { struct refspec_item refspec; - int ret = refspec_item_init_fetch(&refspec, fetch_refspec_str); + int ret = refspec_item_init_fetch(&refspec, fetch_refspec_str, algo); refspec_item_clear(&refspec); return ret; } diff --git a/refspec.h b/refspec.h index 832d6f923c..e482b720a8 100644 --- a/refspec.h +++ b/refspec.h @@ -1,6 +1,7 @@ #ifndef REFSPEC_H #define REFSPEC_H +struct git_hash_algo; struct string_list; struct strvec; @@ -33,8 +34,10 @@ struct refspec_item { char *raw; }; -int refspec_item_init_fetch(struct refspec_item *item, const char *refspec); -int refspec_item_init_push(struct refspec_item *item, const char *refspec); +int refspec_item_init_fetch(struct refspec_item *item, const char *refspec, + const struct git_hash_algo *algo); +int refspec_item_init_push(struct refspec_item *item, const char *refspec, + const struct git_hash_algo *algo); void refspec_item_clear(struct refspec_item *item); /** @@ -61,7 +64,7 @@ __attribute__((format (printf,2,3))) void refspec_appendf(struct refspec *rs, const char *fmt, ...); void refspec_appendn(struct refspec *rs, const char **refspecs, int nr); -int valid_fetch_refspec(const char *refspec); +int valid_fetch_refspec(const char *refspec, const struct git_hash_algo *algo); /* * Determine what values to pass to the peer in ref-prefix lines diff --git a/remote.c b/remote.c index e6c52c850c..b4dff1e5f9 100644 --- a/remote.c +++ b/remote.c @@ -3039,7 +3039,7 @@ int valid_remote_name(const char *name) int result; struct strbuf refspec = STRBUF_INIT; strbuf_addf(&refspec, "refs/heads/test:refs/remotes/%s/test", name); - result = valid_fetch_refspec(refspec.buf); + result = valid_fetch_refspec(refspec.buf, the_hash_algo); strbuf_release(&refspec); return result; } From 01f4b61d0302af16b30b2b5141a53e3a88f9f98a Mon Sep 17 00:00:00 2001 From: Patrick Steinhardt Date: Thu, 16 Jul 2026 14:38:04 +0200 Subject: [PATCH 3/3] refspec: stop depending on `the_repository` The only remaining user of `the_hash_algo` in "refspec.c" is `refspec_append()`, which needs to know the hash algorithm so that it can parse the appended refspec item. In contrast to the functions adapted in the preceding commit, this function always operates on a `struct refspec`. As that structure is expected to only ever contain refspecs that all use the same hash function it doesn't make sense though to adapt each caller. Instead, adapt the structure itself so that it gets initialized with a hash function and use that hash function to parse new refspec items. Adapt callers accordingly. This removes the final dependency on the global repository variable in "refspec.c", so we can drop `USE_THE_REPOSITORY_VARIABLE`. Signed-off-by: Patrick Steinhardt Signed-off-by: Junio C Hamano --- builtin/fast-export.c | 4 +++- builtin/fetch.c | 6 ++++-- builtin/push.c | 6 ++++-- builtin/send-pack.c | 5 ++++- builtin/submodule--helper.c | 2 +- http-push.c | 2 +- refspec.c | 13 ++++++------- refspec.h | 15 +++++++++++---- remote.c | 4 ++-- transport-helper.c | 2 +- 10 files changed, 37 insertions(+), 22 deletions(-) diff --git a/builtin/fast-export.c b/builtin/fast-export.c index 0be43104dc..8f4da4cfac 100644 --- a/builtin/fast-export.c +++ b/builtin/fast-export.c @@ -51,7 +51,7 @@ static int show_original_ids; static int mark_tags; static struct string_list extra_refs = STRING_LIST_INIT_DUP; static struct string_list tag_refs = STRING_LIST_INIT_DUP; -static struct refspec refspecs = REFSPEC_INIT_FETCH; +static struct refspec refspecs; static int anonymize; static struct hashmap anonymized_seeds; static struct revision_sources revision_sources; @@ -1372,6 +1372,8 @@ int cmd_fast_export(int argc, /* we handle encodings */ repo_config(the_repository, git_default_config, NULL); + refspec_init_fetch(&refspecs, the_hash_algo); + repo_init_revisions(the_repository, &revs, prefix); init_revision_sources(&revision_sources); revs.topo_order = 1; diff --git a/builtin/fetch.c b/builtin/fetch.c index 1d4a129039..6e1a224553 100644 --- a/builtin/fetch.c +++ b/builtin/fetch.c @@ -96,7 +96,7 @@ static struct string_list deepen_not = STRING_LIST_INIT_NODUP; static struct strbuf default_rla = STRBUF_INIT; static struct transport *gtransport; static struct transport *gsecondary; -static struct refspec refmap = REFSPEC_INIT_FETCH; +static struct refspec refmap; static struct string_list server_options = STRING_LIST_INIT_DUP; static struct string_list negotiation_restrict = STRING_LIST_INIT_NODUP; static struct string_list negotiation_include = STRING_LIST_INIT_NODUP; @@ -2429,7 +2429,7 @@ static int fetch_one(struct remote *remote, int argc, const char **argv, const struct fetch_config *config, struct list_objects_filter_options *filter_options) { - struct refspec rs = REFSPEC_INIT_FETCH; + struct refspec rs = REFSPEC_INIT_FETCH(the_hash_algo); int i; int exit_code; int maybe_prune_tags; @@ -2631,6 +2631,8 @@ int cmd_fetch(int argc, filter_options.allow_auto_filter = 1; + refspec_init_fetch(&refmap, the_hash_algo); + packet_trace_identity("fetch"); /* Record the command line for the reflog */ diff --git a/builtin/push.c b/builtin/push.c index 1b2ad3b8df..8ccdb07c40 100644 --- a/builtin/push.c +++ b/builtin/push.c @@ -66,7 +66,7 @@ static enum transport_family family; static struct push_cas_option cas; -static struct refspec rs = REFSPEC_INIT_PUSH; +static struct refspec rs; static struct string_list push_options_config = STRING_LIST_INIT_DUP; @@ -749,6 +749,8 @@ int cmd_push(int argc, : &push_options_config); set_push_cert_flags(&flags, push_cert); + refspec_init_push(&rs, the_hash_algo); + die_for_incompatible_opt4(deleterefs, "--delete", tags, "--tags", flags & TRANSPORT_PUSH_ALL, "--all/--branches", @@ -855,7 +857,7 @@ int cmd_push(int argc, } refspec_clear(&rs); - rs = (struct refspec) REFSPEC_INIT_PUSH; + rs = (struct refspec) REFSPEC_INIT_PUSH(the_hash_algo); if (tags) refspec_append(&rs, "refs/tags/*"); diff --git a/builtin/send-pack.c b/builtin/send-pack.c index 1412b49bc8..d6cdbae472 100644 --- a/builtin/send-pack.c +++ b/builtin/send-pack.c @@ -153,7 +153,7 @@ int cmd_send_pack(int argc, const char *prefix, struct repository *repo) { - struct refspec rs = REFSPEC_INIT_PUSH; + struct refspec rs; const char *remote_name = NULL; struct remote *remote = NULL; const char *dest = NULL; @@ -214,6 +214,9 @@ int cmd_send_pack(int argc, repo_config(repo, send_pack_config, NULL); argc = parse_options(argc, argv, prefix, options, send_pack_usage, 0); + + refspec_init_push(&rs, repo->hash_algo); + if (argc > 0) { dest = argv[0]; refspec_appendn(&rs, argv + 1, argc - 1); diff --git a/builtin/submodule--helper.c b/builtin/submodule--helper.c index 1cc82a134d..c396b826ba 100644 --- a/builtin/submodule--helper.c +++ b/builtin/submodule--helper.c @@ -3150,7 +3150,7 @@ static int push_check(int argc, const char **argv, const char *prefix UNUSED, if (argc > 2) { int i; struct ref *local_refs = get_local_heads(); - struct refspec refspec = REFSPEC_INIT_PUSH; + struct refspec refspec = REFSPEC_INIT_PUSH(the_hash_algo); refspec_appendn(&refspec, argv + 2, argc - 2); diff --git a/http-push.c b/http-push.c index 3c23cbba27..969d984cb9 100644 --- a/http-push.c +++ b/http-push.c @@ -1716,7 +1716,7 @@ int cmd_main(int argc, const char **argv) { struct transfer_request *request; struct transfer_request *next_request; - struct refspec rs = REFSPEC_INIT_PUSH; + struct refspec rs = REFSPEC_INIT_PUSH(the_hash_algo); struct remote_lock *ref_lock = NULL; struct remote_lock *info_ref_lock = NULL; int delete_branch = 0; diff --git a/refspec.c b/refspec.c index 33a6fb8e45..7cb479983b 100644 --- a/refspec.c +++ b/refspec.c @@ -1,4 +1,3 @@ -#define USE_THE_REPOSITORY_VARIABLE #define DISABLE_SIGN_COMPARE_WARNINGS #include "git-compat-util.h" @@ -185,15 +184,15 @@ void refspec_item_clear(struct refspec_item *item) item->exact_sha1 = 0; } -void refspec_init_fetch(struct refspec *rs) +void refspec_init_fetch(struct refspec *rs, const struct git_hash_algo *algo) { - struct refspec blank = REFSPEC_INIT_FETCH; + struct refspec blank = REFSPEC_INIT_FETCH(algo); memcpy(rs, &blank, sizeof(*rs)); } -void refspec_init_push(struct refspec *rs) +void refspec_init_push(struct refspec *rs, const struct git_hash_algo *algo) { - struct refspec blank = REFSPEC_INIT_PUSH; + struct refspec blank = REFSPEC_INIT_PUSH(algo); memcpy(rs, &blank, sizeof(*rs)); } @@ -203,9 +202,9 @@ void refspec_append(struct refspec *rs, const char *refspec) int ret; if (rs->fetch) - ret = refspec_item_init_fetch(&item, refspec, the_hash_algo); + ret = refspec_item_init_fetch(&item, refspec, rs->hash_algo); else - ret = refspec_item_init_push(&item, refspec, the_hash_algo); + ret = refspec_item_init_push(&item, refspec, rs->hash_algo); if (!ret) die(_("invalid refspec '%s'"), refspec); diff --git a/refspec.h b/refspec.h index e482b720a8..fadef67933 100644 --- a/refspec.h +++ b/refspec.h @@ -49,14 +49,21 @@ struct refspec { int alloc; int nr; + const struct git_hash_algo *hash_algo; unsigned fetch : 1; }; -#define REFSPEC_INIT_FETCH { .fetch = 1 } -#define REFSPEC_INIT_PUSH { .fetch = 0 } +#define REFSPEC_INIT_FETCH(algo) { \ + .fetch = 1, \ + .hash_algo = (algo), \ +} +#define REFSPEC_INIT_PUSH(algo) { \ + .fetch = 0, \ + .hash_algo = (algo), \ +} -void refspec_init_fetch(struct refspec *rs); -void refspec_init_push(struct refspec *rs); +void refspec_init_fetch(struct refspec *rs, const struct git_hash_algo *hash_algo); +void refspec_init_push(struct refspec *rs, const struct git_hash_algo *hash_algo); void refspec_clear(struct refspec *rs); void refspec_append(struct refspec *rs, const char *refspec); diff --git a/remote.c b/remote.c index b4dff1e5f9..d151b1f9d9 100644 --- a/remote.c +++ b/remote.c @@ -150,8 +150,8 @@ static struct remote *make_remote(struct remote_state *remote_state, ret->prune = -1; /* unspecified */ ret->prune_tags = -1; /* unspecified */ ret->name = xstrndup(name, len); - refspec_init_push(&ret->push); - refspec_init_fetch(&ret->fetch); + refspec_init_push(&ret->push, the_hash_algo); + refspec_init_fetch(&ret->fetch, the_hash_algo); string_list_init_dup(&ret->server_options); string_list_init_dup(&ret->negotiation_restrict); string_list_init_dup(&ret->negotiation_include); diff --git a/transport-helper.c b/transport-helper.c index 80f90eb7ba..8a25707b03 100644 --- a/transport-helper.c +++ b/transport-helper.c @@ -162,7 +162,7 @@ static struct child_process *get_helper(struct transport *transport) data->helper = helper; data->no_disconnect_req = 0; - refspec_init_fetch(&data->rs); + refspec_init_fetch(&data->rs, the_hash_algo); /* * Open the output as FILE* so strbuf_getline_*() family of