Repository: JanDeDobbeleer/oh-my-posh
Pull request #5581 (2024-09-07 21:49:59)

fix(git): support azure devops remote URL - SSH
### Prerequisites

- [x] I have read and understood the [contributing guide][CONTRIBUTING.md].
- [x] The commit message follows the [conventional commits][cc] guidelines.
- [x] Tests for the changes have been added (for bug fixes / features).
- [x] Docs have been added/updated (for bug fixes / features).

### Description

Followed the pattern used in #5580 to add support for SSH as well.

**P.S.** Despite being a simple PR (playing with strings), this is my first PR in this repo, and my first Go code for that matter, so feel free to bombard with comments; happy to change 🙂

<!---

Tips:

If you're not comfortable with working with Git,
we're working a guide (https://ohmyposh.dev/docs/contributing/git) to help you out.
Oh My Posh advises GitKraken (https://www.gitkraken.com/invite/nQmDPR9D) as your preferred cross platform Git GUI power tool.

-->

[CONTRIBUTING.md]: https://github.com/JanDeDobbeleer/oh-my-posh/blob/main/CONTRIBUTING.md
[cc]: https://www.conventionalcommits.org/en/v1.0.0/#summary



Patch:
diff --git a/src/segments/git.go b/src/segments/git.go
index 30d8fc30..d318a484 100644
--- a/src/segments/git.go
+++ b/src/segments/git.go
@@ -451,11 +451,11 @@ func (g *Git) setBranchStatus() {
 }
 
 func (g *Git) cleanUpstreamURL(url string) string {
-	// https://{organization}@dev.azure.com/{organization}/{project}/_git/{repository}
-	if strings.Contains(url, "@dev.azure.com") {
-		match := regex.FindNamedRegexMatch(`.*@(?P<URL>dev.azure.com.*)`, url)
-		if len(match) != 0 {
-			return fmt.Sprintf("https://%s", match["URL"])
+	// Azure DevOps
+	if strings.Contains(url, "dev.azure.com") {
+		match := regex.FindNamedRegexMatch(`^.*@(ssh.)?dev\.azure\.com(:v3)?/(?P<ORGANIZATION>[A-Za-z0-9_-]+)/(?P<PROJECT>[A-Za-z0-9_-]+)/(_git/)?(?P<REPOSITORY>[A-Za-z0-9_-]+)$`, url)
+		if len(match) == 4 {
+			return fmt.Sprintf("https://dev.azure.com/%s/%s/_git/%s", match["ORGANIZATION"], match["PROJECT"], match["REPOSITORY"])
 		}
 	}
 


Test patch:
diff --git a/src/segments/git_test.go b/src/segments/git_test.go
index 3f56adad..c9b6e42b 100644
--- a/src/segments/git_test.go
+++ b/src/segments/git_test.go
@@ -649,7 +649,8 @@ func TestGitCleanSSHURL(t *testing.T) {
 		{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"},
-		{Case: "Azure DevOps", Expected: "https://dev.azure.com/posh/oh-my-posh/_git/website", Upstream: "https://posh@dev.azure.com/posh/oh-my-posh/_git/website"},
+		{Case: "Azure DevOps, https", Expected: "https://dev.azure.com/posh/oh-my-posh/_git/website", Upstream: "https://posh@dev.azure.com/posh/oh-my-posh/_git/website"},
+		{Case: "Azure DevOps, ssh", Expected: "https://dev.azure.com/posh/oh-my-posh/_git/website", Upstream: "git@ssh.dev.azure.com:v3/posh/oh-my-posh/website"},
 	}
 	for _, tc := range cases {
 		g := &Git{}