fix: address verified review findings (completion-target-ref-1)

Signed-off-by: bircni <bircni@icloud.com>
This commit is contained in:
bircni committed 2026-10-18 17:40:30 +02:00
1 parent 432d139071
commit be3b8a1ced
10 files changed
+166 -34

No files matched your search

+12 -3
View File
@@ -178,12 +178,21 @@ func ParseRepository(raw string, hosts map[string]Host) (RepositoryRef, error) {
}
// The longest configured instance prefix wins, so example.org/gitea/owner/name selects the
// instance installed below /gitea rather than example.org.
best := ""
best, tied := "", false
for instance := range hosts {
prefix := bareInstance(instance)
if len(prefix) > len(bareInstance(best)) && strings.HasPrefix(strings.ToLower(trimmed), strings.ToLower(prefix)+"/") && ValidSlug(trimmed[len(prefix)+1:]) {
best = instance
if !strings.HasPrefix(strings.ToLower(trimmed), strings.ToLower(prefix)+"/") || !ValidSlug(trimmed[len(prefix)+1:]) {
continue
}
switch longest := len(bareInstance(best)); {
case len(prefix) > longest:
best, tied = instance, false
case len(prefix) == longest && best != "":
tied = true // http and https of one host: the scheme decides, not map order.
}
}
if tied {
return RepositoryRef{}, fmt.Errorf("instance %q is configured for both http and https; include the scheme", bareInstance(best))
}
if best != "" {
owner, name, _ := strings.Cut(trimmed[len(bareInstance(best))+1:], "/")
+11
View File
@@ -102,6 +102,17 @@ func TestParseRepositoryAcceptsShorthands(t *testing.T) {
require.Equal(t, "alice/project", RepositoryRef{Owner: "alice", Name: "project"}.Slug())
}
func TestParseRepositoryRejectsAnInstanceConfiguredForBothSchemes(t *testing.T) {
hosts := map[string]Host{"http://localhost:3001": {}, "https://localhost:3001": {}}
for range 20 { // Map iteration order is random.
_, err := ParseRepository("localhost:3001/alice/project", hosts)
require.ErrorContains(t, err, "include the scheme")
}
got, err := ParseRepository("https://localhost:3001/alice/project", hosts)
require.NoError(t, err)
require.Equal(t, "https://localhost:3001", got.Host)
}
func TestResolveHostAcceptsBareHosts(t *testing.T) {
hosts := map[string]Host{"http://localhost:3001": {}, "https://example.org/gitea": {}}
for raw, want := range map[string]string{