From 22a02f2dde21b790df94b746ebdeb313ac5cfd18 Mon Sep 17 00:00:00 2001 From: Jan De Dobbeleer Date: Thu, 7 Dec 2023 09:21:07 +0100 Subject: [PATCH] fix(git): make .git optional on remotes resolves #4524 --- src/segments/git.go | 10 ++++++++-- src/segments/git_test.go | 1 + 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/src/segments/git.go b/src/segments/git.go index e23e019a..e2828bb6 100644 --- a/src/segments/git.go +++ b/src/segments/git.go @@ -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[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[a-z0-9.]+)(:[0-9]{4})?/(?P.*).git`, url) if len(match) == 0 { // host.xz:/path/to/repo.git/ match = regex.FindNamedRegexMatch(`^(?P[a-z0-9./]+):(?P[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[a-z0-9-]+)://(?P[\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.*):(?P.*).git`, url) + match = regex.FindNamedRegexMatch(`.*@(?P.*):(?P.*)`, 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 { diff --git a/src/segments/git_test.go b/src/segments/git_test.go index f25135e8..4a722846 100644 --- a/src/segments/git_test.go +++ b/src/segments/git_test.go @@ -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 {