mirror of
https://gitea.com/gitea/tea.git
synced 2026-10-26 19:11:25 +01:00
fix: address verified review findings (completion-target-ref-1)
Signed-off-by: bircni <bircni@icloud.com>
This commit is contained in:
10 files changed
+166
-34
No files matched your search
@@ -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:], "/")
|
||||
|
||||
@@ -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{
|
||||
|
||||
Reference in new issue
Block a user