feat: auto detect hostname
This commit is contained in:
parent
2c27823e18
commit
c0baf4fa3b
13 changed files with 300 additions and 125 deletions
|
|
@ -25,7 +25,28 @@ func DetectRepo() (owner, name string, err error) {
|
|||
}
|
||||
|
||||
// Extract owner/name from URL
|
||||
return parseRemoteURL(remoteURL)
|
||||
owner, name, _, err = parseRemoteURL(remoteURL)
|
||||
return owner, name, err
|
||||
}
|
||||
|
||||
// DetectHost attempts to detect the Forgejo instance hostname from the current git directory.
|
||||
// It reads .git/config and parses the origin remote URL to extract the hostname.
|
||||
func DetectHost() (hostname string, err error) {
|
||||
// Find .git/config file
|
||||
gitConfigPath, err := findGitConfig()
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
// Parse .git/config
|
||||
remoteURL, err := parseGitConfig(gitConfigPath)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
// Extract hostname from URL
|
||||
_, _, hostname, err = parseRemoteURL(remoteURL)
|
||||
return hostname, err
|
||||
}
|
||||
|
||||
// findGitConfig searches for .git/config starting from the current directory
|
||||
|
|
@ -93,33 +114,33 @@ func parseGitConfig(configPath string) (string, error) {
|
|||
return "", fmt.Errorf("no origin remote found in git config")
|
||||
}
|
||||
|
||||
// parseRemoteURL extracts owner/name from various git URL formats:
|
||||
// parseRemoteURL extracts owner/name/hostname from various git URL formats:
|
||||
// - https://codeberg.org/owner/name.git
|
||||
// - git@codeberg.org:owner/name.git
|
||||
// - ssh://git@codeberg.org/owner/name.git
|
||||
func parseRemoteURL(url string) (owner, name string, err error) {
|
||||
func parseRemoteURL(url string) (owner, name, hostname string, err error) {
|
||||
url = strings.TrimSpace(url)
|
||||
|
||||
// Remove .git suffix
|
||||
url = strings.TrimSuffix(url, ".git")
|
||||
|
||||
// Pattern for HTTPS URLs: https://host/owner/name
|
||||
httpsRegex := regexp.MustCompile(`https?://[^/]+/([^/]+)/([^/]+)`)
|
||||
if matches := httpsRegex.FindStringSubmatch(url); len(matches) == 3 {
|
||||
return matches[1], matches[2], nil
|
||||
httpsRegex := regexp.MustCompile(`https?://([^/]+)/([^/]+)/([^/]+)`)
|
||||
if matches := httpsRegex.FindStringSubmatch(url); len(matches) == 4 {
|
||||
return matches[2], matches[3], matches[1], nil
|
||||
}
|
||||
|
||||
// Pattern for SSH URLs: git@host:owner/name
|
||||
sshRegex := regexp.MustCompile(`git@[^:]+:([^/]+)/(.+)`)
|
||||
if matches := sshRegex.FindStringSubmatch(url); len(matches) == 3 {
|
||||
return matches[1], matches[2], nil
|
||||
sshRegex := regexp.MustCompile(`git@([^:]+):([^/]+)/(.+)`)
|
||||
if matches := sshRegex.FindStringSubmatch(url); len(matches) == 4 {
|
||||
return matches[2], matches[3], matches[1], nil
|
||||
}
|
||||
|
||||
// Pattern for SSH URLs with protocol: ssh://git@host/owner/name
|
||||
sshProtocolRegex := regexp.MustCompile(`ssh://git@[^/]+/([^/]+)/(.+)`)
|
||||
if matches := sshProtocolRegex.FindStringSubmatch(url); len(matches) == 3 {
|
||||
return matches[1], matches[2], nil
|
||||
sshProtocolRegex := regexp.MustCompile(`ssh://(?:git@)?([^/]+)/([^/]+)/(.+)`)
|
||||
if matches := sshProtocolRegex.FindStringSubmatch(url); len(matches) == 4 {
|
||||
return matches[2], matches[3], matches[1], nil
|
||||
}
|
||||
|
||||
return "", "", fmt.Errorf("unable to parse repository from URL: %s", url)
|
||||
return "", "", "", fmt.Errorf("unable to parse repository from URL: %s", url)
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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