Merge branch 'ps/refspec-wo-the-repository' into next

The dependency on the global 'the_repository' variable in the
'refspec.c' API has been removed by passing the hash algorithm
explicitly to refspec-parsing functions and storing it in 'struct
refspec'.

* ps/refspec-wo-the-repository:
  refspec: stop depending on `the_repository`
  refspec: let callers pass in hash algorithm when parsing items
  refspec: group related structures and functions
next
Junio C Hamano 2026-07-20 11:16:42 -07:00
commit 31044c3fc9
11 changed files with 72 additions and 47 deletions

View File

@ -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;

View File

@ -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;
@ -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) {
@ -2428,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;
@ -2630,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 */

View File

@ -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"))

View File

@ -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/*");

View File

@ -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);

View File

@ -3151,7 +3151,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);


View File

@ -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;

View File

@ -1,4 +1,3 @@
#define USE_THE_REPOSITORY_VARIABLE
#define DISABLE_SIGN_COMPARE_WARNINGS

#include "git-compat-util.h"
@ -16,7 +15,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 +84,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 +101,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 +154,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)
@ -182,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));
}

@ -200,9 +202,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, rs->hash_algo);
else
ret = refspec_item_init_push(&item, refspec);
ret = refspec_item_init_push(&item, refspec, rs->hash_algo);
if (!ret)
die(_("invalid refspec '%s'"), refspec);

@ -246,10 +248,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;
}

View File

@ -1,6 +1,10 @@
#ifndef REFSPEC_H
#define REFSPEC_H

struct git_hash_algo;
struct string_list;
struct strvec;

#define TAG_REFSPEC "refs/tags/*:refs/tags/*"

/**
@ -30,10 +34,11 @@ 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,
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);

/**
* An array of strings can be parsed into a struct refspec using
@ -44,23 +49,30 @@ struct refspec {
int alloc;
int nr;

const struct git_hash_algo *hash_algo;
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);
void refspec_init_fetch(struct refspec *rs);
void refspec_init_push(struct refspec *rs);
#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, 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);
__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);
int valid_fetch_refspec(const char *refspec, const struct git_hash_algo *algo);

struct strvec;
/*
* Determine what <prefix> values to pass to the peer in ref-prefix lines
* (see linkgit:gitprotocol-v2[5]).
@ -76,7 +88,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 +101,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

View File

@ -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);
@ -3041,7 +3041,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;
}

View File

@ -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