fix(git): parse remote URL correctly

resolves #3641
This commit is contained in:
Jan De Dobbeleer 2023-03-28 09:34:05 +02:00 committed by Jan De Dobbeleer
parent 06530572ca
commit 4eb1d0b1d6
2 changed files with 55 additions and 14 deletions

View file

@ -388,26 +388,42 @@ func (g *Git) setBranchStatus() {
g.BranchStatus = getBranchStatus()
}
func (g *Git) getUpstreamIcon() string {
cleanSSHURL := func(url string) string {
if strings.HasPrefix(url, "http") {
return url
}
url = strings.TrimPrefix(url, "ssh://")
url = strings.TrimPrefix(url, "git://")
if i := strings.Index(url, "@"); i >= 0 {
url = url[i+1:]
url = strings.Replace(url, ":", "/", 1)
}
url = strings.TrimSuffix(url, ".git")
return fmt.Sprintf("https://%s", url)
func (g *Git) cleanUpstreamURL(url string) string {
if strings.HasPrefix(url, "http") {
return url
}
// /path/to/repo.git/
match := regex.FindNamedRegexMatch(`^(?P<URL>[a-z0-9./]+)$`, url)
if len(match) != 0 {
url := strings.Trim(match["URL"], "/")
url = strings.TrimSuffix(url, ".git")
return fmt.Sprintf("https://%s", strings.TrimPrefix(url, "/"))
}
// ssh://user@host.xz:1234/path/to/repo.git/
match = regex.FindNamedRegexMatch(`(ssh|ftp|git|rsync)://(.*@)?(?P<URL>[a-z0-9.]+)(:[0-9]{4})?/(?P<PATH>.*).git`, url)
if len(match) == 0 {
// host.xz:/path/to/repo.git/
match = regex.FindNamedRegexMatch(`^(?P<URL>[a-z0-9./]+):(?P<PATH>[a-z0-9./]+)$`, url)
}
if len(match) != 0 {
path := strings.Trim(match["PATH"], "/")
path = strings.TrimSuffix(path, ".git")
return fmt.Sprintf("https://%s/%s", match["URL"], path)
}
// user@host.xz:/path/to/repo.git
match = regex.FindNamedRegexMatch(`.*@(?P<URL>.*):(?P<PATH>.*).git`, url)
if len(match) == 0 {
return ""
}
return fmt.Sprintf("https://%s/%s", match["URL"], match["PATH"])
}
func (g *Git) getUpstreamIcon() string {
g.RawUpstreamURL = g.getRemoteURL()
if len(g.RawUpstreamURL) == 0 {
return ""
}
g.UpstreamURL = cleanSSHURL(g.RawUpstreamURL)
g.UpstreamURL = g.cleanUpstreamURL(g.RawUpstreamURL)
// allow overrides first
custom := g.props.GetKeyValueMap(UpstreamIcons, map[string]string{})

View file

@ -589,6 +589,31 @@ func TestGetStashContextZeroEntries(t *testing.T) {
}
}
func TestGitCleanSSHURL(t *testing.T) {
cases := []struct {
Case string
Expected string
Upstream string
}{
{Case: "regular URL", Expected: "https://src.example.com/user/repo", Upstream: "/src.example.com/user/repo.git"},
{Case: "domain:path", Expected: "https://host.xz/path/to/repo", Upstream: "host.xz:/path/to/repo.git/"},
{Case: "ssh with port", Expected: "https://host.xz/path/to/repo", Upstream: "ssh://user@host.xz:1234/path/to/repo.git"},
{Case: "ssh with port, trailing slash", Expected: "https://host.xz/path/to/repo", Upstream: "ssh://user@host.xz:1234/path/to/repo.git/"},
{Case: "ssh without port", Expected: "https://host.xz/path/to/repo", Upstream: "ssh://user@host.xz/path/to/repo.git/"},
{Case: "ssh port, no user", Expected: "https://host.xz/path/to/repo", Upstream: "ssh://host.xz:1234/path/to/repo.git"},
{Case: "ssh no port, no user", Expected: "https://host.xz/path/to/repo", Upstream: "ssh://host.xz/path/to/repo.git"},
{Case: "rsync no port, no user", Expected: "https://host.xz/path/to/repo", Upstream: "rsync://host.xz/path/to/repo.git/"},
{Case: "git no port, no user", Expected: "https://host.xz/path/to/repo", Upstream: "git://host.xz/path/to/repo.git"},
{Case: "gitea no port, no user", Expected: "https://src.example.com/user/repo", Upstream: "_gitea@src.example.com:user/repo.git"},
{Case: "unsupported", Upstream: "\\test\\repo.git"},
}
for _, tc := range cases {
g := &Git{}
upstreamIcon := g.cleanUpstreamURL(tc.Upstream)
assert.Equal(t, tc.Expected, upstreamIcon, tc.Case)
}
}
func TestGitUpstream(t *testing.T) {
cases := []struct {
Case string