]> git.ipfire.org Git - thirdparty/git.git/commitdiff
refspec: stop depending on `the_repository`
authorPatrick Steinhardt <ps@pks.im>
Thu, 16 Jul 2026 12:38:04 +0000 (14:38 +0200)
committerJunio C Hamano <gitster@pobox.com>
Thu, 16 Jul 2026 20:58:25 +0000 (13:58 -0700)
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 <ps@pks.im>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
builtin/fast-export.c
builtin/fetch.c
builtin/push.c
builtin/send-pack.c
builtin/submodule--helper.c
http-push.c
refspec.c
refspec.h
remote.c
transport-helper.c

index 0be43104dc0d23f9cef7e4b17a58e388ce281f31..8f4da4cfac82835e4c80de236a9436bde18b957c 100644 (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;
index 1d4a129039b16e073a460cf2f6ed0ae7a327b1db..6e1a224553f13fca676394b46ae90adac4591a11 100644 (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;
@@ -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 */
index 1b2ad3b8df7c550e0e66c493cb6b042bd349aeef..8ccdb07c404874a0d9ec15f92eff51387144cc98 100644 (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/*");
index 1412b49bc845b1e70ba111f3d65821d3be13dce1..d6cdbae472f725260745184d1f661a5f3b26435d 100644 (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);
index 1cc82a134db22e35e095dae1602953f0a48ec2de..c396b826ba7637eee450fd8034c18ced482c288a 100644 (file)
@@ -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);
 
index 3c23cbba27a9ec7ca6ff6e2b259fa48bd392c77a..969d984cb9ae0f03bb8fc72b211df95c7c0f20f4 100644 (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;
index 33a6fb8e457887c41f2e1d2130cf8797c73ac55d..7cb479983b507b73e5f00471d792f0ba2bd01f89 100644 (file)
--- 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);
 
index e482b720a87b867951002464364295afc8c0517d..fadef67933c0799e050786c2189fcb8931f47f9d 100644 (file)
--- 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 }
-
-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);
index b4dff1e5f990fa2cf779d41bbdf27a46077b0c52..d151b1f9d9bce5010815b1de2123579700cfa75d 100644 (file)
--- 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);
index 80f90eb7bace6f761b7796fa93b60d7408464b9d..8a25707b038aace65ac5fdce277ff021a915d84d 100644 (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