feat: auto detect hostname
This commit is contained in:
parent
2c27823e18
commit
c0baf4fa3b
13 changed files with 300 additions and 125 deletions
|
|
@ -1,56 +1,67 @@
|
|||
package git
|
||||
|
||||
import "testing"
|
||||
import (
|
||||
"os"
|
||||
"path/filepath"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestParseRemoteURL(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
url string
|
||||
name string
|
||||
url string
|
||||
wantOwner string
|
||||
wantName string
|
||||
wantErr bool
|
||||
wantHost string
|
||||
wantErr bool
|
||||
}{
|
||||
{
|
||||
name: "HTTPS URL with .git",
|
||||
url: "https://codeberg.org/romaintb/fgj.git",
|
||||
name: "HTTPS URL with .git",
|
||||
url: "https://codeberg.org/romaintb/fgj.git",
|
||||
wantOwner: "romaintb",
|
||||
wantName: "fgj",
|
||||
wantErr: false,
|
||||
wantHost: "codeberg.org",
|
||||
wantErr: false,
|
||||
},
|
||||
{
|
||||
name: "HTTPS URL without .git",
|
||||
url: "https://codeberg.org/romaintb/fgj",
|
||||
name: "HTTPS URL without .git",
|
||||
url: "https://codeberg.org/romaintb/fgj",
|
||||
wantOwner: "romaintb",
|
||||
wantName: "fgj",
|
||||
wantErr: false,
|
||||
wantHost: "codeberg.org",
|
||||
wantErr: false,
|
||||
},
|
||||
{
|
||||
name: "SSH URL with .git",
|
||||
url: "git@codeberg.org:romaintb/fgj.git",
|
||||
name: "SSH URL with .git",
|
||||
url: "git@codeberg.org:romaintb/fgj.git",
|
||||
wantOwner: "romaintb",
|
||||
wantName: "fgj",
|
||||
wantErr: false,
|
||||
wantHost: "codeberg.org",
|
||||
wantErr: false,
|
||||
},
|
||||
{
|
||||
name: "SSH URL without .git",
|
||||
url: "git@codeberg.org:romaintb/fgj",
|
||||
name: "SSH URL without .git",
|
||||
url: "git@codeberg.org:romaintb/fgj",
|
||||
wantOwner: "romaintb",
|
||||
wantName: "fgj",
|
||||
wantErr: false,
|
||||
wantHost: "codeberg.org",
|
||||
wantErr: false,
|
||||
},
|
||||
{
|
||||
name: "SSH protocol URL",
|
||||
url: "ssh://git@codeberg.org/romaintb/fgj.git",
|
||||
name: "SSH protocol URL",
|
||||
url: "ssh://git@codeberg.org/romaintb/fgj.git",
|
||||
wantOwner: "romaintb",
|
||||
wantName: "fgj",
|
||||
wantErr: false,
|
||||
wantHost: "codeberg.org",
|
||||
wantErr: false,
|
||||
},
|
||||
{
|
||||
name: "GitHub HTTPS URL",
|
||||
url: "https://github.com/user/repo.git",
|
||||
name: "GitHub HTTPS URL",
|
||||
url: "https://github.com/user/repo.git",
|
||||
wantOwner: "user",
|
||||
wantName: "repo",
|
||||
wantErr: false,
|
||||
wantHost: "github.com",
|
||||
wantErr: false,
|
||||
},
|
||||
{
|
||||
name: "Invalid URL",
|
||||
|
|
@ -63,56 +74,60 @@ func TestParseRemoteURL(t *testing.T) {
|
|||
wantErr: true,
|
||||
},
|
||||
{
|
||||
name: "URL with trailing whitespace",
|
||||
url: " https://codeberg.org/owner/repo.git ",
|
||||
name: "URL with trailing whitespace",
|
||||
url: " https://codeberg.org/owner/repo.git ",
|
||||
wantOwner: "owner",
|
||||
wantName: "repo",
|
||||
wantErr: false,
|
||||
wantHost: "codeberg.org",
|
||||
wantErr: false,
|
||||
},
|
||||
{
|
||||
name: "URL with port number",
|
||||
url: "https://git.example.com:443/owner/repo.git",
|
||||
name: "URL with port number",
|
||||
url: "https://git.example.com:443/owner/repo.git",
|
||||
wantOwner: "owner",
|
||||
wantName: "repo",
|
||||
wantErr: false,
|
||||
wantHost: "git.example.com:443",
|
||||
wantErr: false,
|
||||
},
|
||||
{
|
||||
name: "SSH URL with port parses incorrectly",
|
||||
url: "ssh://git@git.example.com:22/owner/repo.git",
|
||||
// Note: This currently parses as owner="22" name="owner/repo"
|
||||
// which is incorrect but the regex matches. We document this
|
||||
// limitation rather than make the test fail.
|
||||
name: "SSH URL with port parses incorrectly",
|
||||
url: "ssh://git@git.example.com:22/owner/repo.git",
|
||||
wantOwner: "22",
|
||||
wantName: "owner/repo",
|
||||
wantErr: false,
|
||||
wantHost: "git.example.com",
|
||||
wantErr: false,
|
||||
},
|
||||
{
|
||||
name: "HTTP URL (not HTTPS)",
|
||||
url: "http://codeberg.org/owner/repo",
|
||||
name: "HTTP URL (not HTTPS)",
|
||||
url: "http://codeberg.org/owner/repo",
|
||||
wantOwner: "owner",
|
||||
wantName: "repo",
|
||||
wantErr: false,
|
||||
wantHost: "codeberg.org",
|
||||
wantErr: false,
|
||||
},
|
||||
{
|
||||
name: "Repo name with dashes",
|
||||
url: "https://codeberg.org/owner/my-cool-repo.git",
|
||||
name: "Repo name with dashes",
|
||||
url: "https://codeberg.org/owner/my-cool-repo.git",
|
||||
wantOwner: "owner",
|
||||
wantName: "my-cool-repo",
|
||||
wantErr: false,
|
||||
wantHost: "codeberg.org",
|
||||
wantErr: false,
|
||||
},
|
||||
{
|
||||
name: "Repo name with dots",
|
||||
url: "https://codeberg.org/owner/my.repo.name.git",
|
||||
name: "Repo name with dots",
|
||||
url: "https://codeberg.org/owner/my.repo.name.git",
|
||||
wantOwner: "owner",
|
||||
wantName: "my.repo.name",
|
||||
wantErr: false,
|
||||
wantHost: "codeberg.org",
|
||||
wantErr: false,
|
||||
},
|
||||
{
|
||||
name: "Owner with dots",
|
||||
url: "https://codeberg.org/owner.name/repo.git",
|
||||
name: "Owner with dots",
|
||||
url: "https://codeberg.org/owner.name/repo.git",
|
||||
wantOwner: "owner.name",
|
||||
wantName: "repo",
|
||||
wantErr: false,
|
||||
wantHost: "codeberg.org",
|
||||
wantErr: false,
|
||||
},
|
||||
{
|
||||
name: "Missing owner/repo",
|
||||
|
|
@ -128,7 +143,7 @@ func TestParseRemoteURL(t *testing.T) {
|
|||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
owner, name, err := parseRemoteURL(tt.url)
|
||||
owner, name, host, err := parseRemoteURL(tt.url)
|
||||
if (err != nil) != tt.wantErr {
|
||||
t.Errorf("parseRemoteURL() error = %v, wantErr %v", err, tt.wantErr)
|
||||
return
|
||||
|
|
@ -140,6 +155,112 @@ func TestParseRemoteURL(t *testing.T) {
|
|||
if name != tt.wantName {
|
||||
t.Errorf("parseRemoteURL() name = %v, want %v", name, tt.wantName)
|
||||
}
|
||||
if host != tt.wantHost {
|
||||
t.Errorf("parseRemoteURL() host = %v, want %v", host, tt.wantHost)
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestDetectHost(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
gitConfig string
|
||||
wantHost string
|
||||
wantErr bool
|
||||
}{
|
||||
{
|
||||
name: "HTTPS URL",
|
||||
gitConfig: `[core]
|
||||
repositoryformatversion = 0
|
||||
[remote "origin"]
|
||||
url = https://codeberg.org/owner/repo.git
|
||||
fetch = +refs/heads/*:refs/remotes/origin/*
|
||||
`,
|
||||
wantHost: "codeberg.org",
|
||||
wantErr: false,
|
||||
},
|
||||
{
|
||||
name: "SSH URL",
|
||||
gitConfig: `[core]
|
||||
repositoryformatversion = 0
|
||||
[remote "origin"]
|
||||
url = git@codeberg.org:owner/repo.git
|
||||
fetch = +refs/heads/*:refs/remotes/origin/*
|
||||
`,
|
||||
wantHost: "codeberg.org",
|
||||
wantErr: false,
|
||||
},
|
||||
{
|
||||
name: "HTTPS URL with port",
|
||||
gitConfig: `[core]
|
||||
repositoryformatversion = 0
|
||||
[remote "origin"]
|
||||
url = https://git.example.com:443/owner/repo.git
|
||||
fetch = +refs/heads/*:refs/remotes/origin/*
|
||||
`,
|
||||
wantHost: "git.example.com:443",
|
||||
wantErr: false,
|
||||
},
|
||||
{
|
||||
name: "SSH protocol URL",
|
||||
gitConfig: `[core]
|
||||
repositoryformatversion = 0
|
||||
[remote "origin"]
|
||||
url = ssh://git@codeberg.org/owner/repo.git
|
||||
fetch = +refs/heads/*:refs/remotes/origin/*
|
||||
`,
|
||||
wantHost: "codeberg.org",
|
||||
wantErr: false,
|
||||
},
|
||||
{
|
||||
name: "No origin remote",
|
||||
gitConfig: `[core]
|
||||
repositoryformatversion = 0
|
||||
[remote "upstream"]
|
||||
url = https://codeberg.org/owner/repo.git
|
||||
fetch = +refs/heads/*:refs/remotes/origin/*
|
||||
`,
|
||||
wantHost: "",
|
||||
wantErr: true,
|
||||
},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
tmpDir := t.TempDir()
|
||||
gitDir := filepath.Join(tmpDir, ".git")
|
||||
if err := os.MkdirAll(gitDir, 0755); err != nil {
|
||||
t.Fatalf("Failed to create .git directory: %v", err)
|
||||
}
|
||||
|
||||
configPath := filepath.Join(gitDir, "config")
|
||||
if err := os.WriteFile(configPath, []byte(tt.gitConfig), 0644); err != nil {
|
||||
t.Fatalf("Failed to write git config: %v", err)
|
||||
}
|
||||
|
||||
oldWd, err := os.Getwd()
|
||||
if err != nil {
|
||||
t.Fatalf("Failed to get current directory: %v", err)
|
||||
}
|
||||
defer func() {
|
||||
if err := os.Chdir(oldWd); err != nil {
|
||||
t.Logf("Failed to change directory back: %v", err)
|
||||
}
|
||||
}()
|
||||
|
||||
if err := os.Chdir(tmpDir); err != nil {
|
||||
t.Fatalf("Failed to change to temp directory: %v", err)
|
||||
}
|
||||
|
||||
host, err := DetectHost()
|
||||
if (err != nil) != tt.wantErr {
|
||||
t.Errorf("DetectHost() error = %v, wantErr %v", err, tt.wantErr)
|
||||
return
|
||||
}
|
||||
if !tt.wantErr && host != tt.wantHost {
|
||||
t.Errorf("DetectHost() host = %v, want %v", host, tt.wantHost)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue