fix(git): make .git optional on remotes

resolves #4524
This commit is contained in:
Jan De Dobbeleer 2023-12-07 09:21:07 +01:00 committed by Jan De Dobbeleer
parent a9eace8006
commit 22a02f2dde
2 changed files with 9 additions and 2 deletions

View file

@ -427,6 +427,7 @@ 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 {
@ -434,28 +435,33 @@ func (g *Git) cleanUpstreamURL(url string) string {
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)
}
// codecommit::region-identifier-id://repo-name
match = regex.FindNamedRegexMatch(`codecommit::(?P<URL>[a-z0-9-]+)://(?P<PATH>[\w\.@\:/\-~]+)`, url)
if len(match) != 0 {
return fmt.Sprintf("https://%s.console.aws.amazon.com/codesuite/codecommit/repositories/%s/browse?region=%s", match["URL"], match["PATH"], match["URL"])
}
// user@host.xz:/path/to/repo.git
match = regex.FindNamedRegexMatch(`.*@(?P<URL>.*):(?P<PATH>.*).git`, url)
match = regex.FindNamedRegexMatch(`.*@(?P<URL>.*):(?P<PATH>.*)`, url)
if len(match) == 0 {
return ""
}
return fmt.Sprintf("https://%s/%s", match["URL"], match["PATH"])
return fmt.Sprintf("https://%s/%s", match["URL"], strings.TrimSuffix(match["PATH"], ".git"))
}
func (g *Git) getUpstreamIcon() string {

View file

@ -647,6 +647,7 @@ func TestGitCleanSSHURL(t *testing.T) {
{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: "git@ with user", Expected: "https://github.com/JanDeDobbeleer/oh-my-posh", Upstream: "git@github.com:JanDeDobbeleer/oh-my-posh"},
{Case: "unsupported", Upstream: "\\test\\repo.git"},
}
for _, tc := range cases {