Skip to content

Commit 81654d2

Browse files
pks-tgitster
authored andcommitted
builtin/remote: cast away constness in get_head_names()
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 235ac3f commit 81654d2

File tree

1 file changed

+6
-6
lines changed

1 file changed

+6
-6
lines changed

builtin/remote.c

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -493,12 +493,13 @@ static int get_head_names(const struct ref *remote_refs, struct ref_states *stat
493493
{
494494
struct ref *ref, *matches;
495495
struct ref *fetch_map = NULL, **fetch_map_tail = &fetch_map;
496-
struct refspec_item refspec;
496+
struct refspec_item refspec = {
497+
.force = 0,
498+
.pattern = 1,
499+
.src = (char *) "refs/heads/*",
500+
.dst = (char *) "refs/heads/*",
501+
};
497502

498-
memset(&refspec, 0, sizeof(refspec));
499-
refspec.force = 0;
500-
refspec.pattern = 1;
501-
refspec.src = refspec.dst = "refs/heads/*";
502503
get_fetch_map(remote_refs, &refspec, &fetch_map_tail, 0);
503504
matches = guess_remote_head(find_ref_by_name(remote_refs, "HEAD"),
504505
fetch_map, 1);
@@ -507,7 +508,6 @@ static int get_head_names(const struct ref *remote_refs, struct ref_states *stat
507508

508509
free_refs(fetch_map);
509510
free_refs(matches);
510-
511511
return 0;
512512
}
513513

0 commit comments

Comments
 (0)