]> git.ipfire.org Git - thirdparty/git.git/commitdiff
builtin/remote: cast away constness in `get_head_names()`
authorPatrick Steinhardt <ps@pks.im>
Fri, 7 Jun 2024 06:38:02 +0000 (08:38 +0200)
committerJunio C Hamano <gitster@pobox.com>
Fri, 7 Jun 2024 17:30:50 +0000 (10:30 -0700)
In `get_head_names()`, we assign the "refs/heads/*" string constant to
`struct refspec_item::{src,dst}`, which are both non-constant pointers.
Ideally, we'd refactor the code such that both of these fields were
constant. But `struct refspec_item` is used for two different usecases
with conflicting requirements:

  - To query for a source or destination based on the given refspec. The
    caller either sets `src` or `dst` as the branch that we want to
    search for, and the respective other field gets populated. The
    fields should be constant when being used as a query parameter,
    which is owned by the caller, and non-constant when being used as an
    out parameter, which is owned by the refspec item. This is is
    contradictory in itself already.

  - To store refspec items with their respective source and destination
    branches, in which case both fields should be owned by the struct.

Ideally, we'd split up this interface to clearly separate between
querying and storing, which would enable us to clarify lifetimes of the
strings. This would be a much bigger undertaking though.

Instead, accept the status quo for now and cast away the constness of
the source and destination patterns. We know that those are not being
written to or freed, so while this is ugly it certainly is fine for now.

Signed-off-by: Patrick Steinhardt <ps@pks.im>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
builtin/remote.c

index d52b1c0e10ffc1f164c6fa541ba9b381b1bc7d73..b44f580b8c2fa0704a3c02a2495f3829282a786c 100644 (file)
@@ -493,12 +493,13 @@ static int get_head_names(const struct ref *remote_refs, struct ref_states *stat
 {
        struct ref *ref, *matches;
        struct ref *fetch_map = NULL, **fetch_map_tail = &fetch_map;
-       struct refspec_item refspec;
+       struct refspec_item refspec = {
+               .force = 0,
+               .pattern = 1,
+               .src = (char *) "refs/heads/*",
+               .dst = (char *) "refs/heads/*",
+       };
 
-       memset(&refspec, 0, sizeof(refspec));
-       refspec.force = 0;
-       refspec.pattern = 1;
-       refspec.src = refspec.dst = "refs/heads/*";
        get_fetch_map(remote_refs, &refspec, &fetch_map_tail, 0);
        matches = guess_remote_head(find_ref_by_name(remote_refs, "HEAD"),
                                    fetch_map, 1);
@@ -507,7 +508,6 @@ static int get_head_names(const struct ref *remote_refs, struct ref_states *stat
 
        free_refs(fetch_map);
        free_refs(matches);
-
        return 0;
 }