如何转换 Atlassian Stash 存储库 URL 以在浏览器中打开?

How can I convert an Atlassian Stash repo URL to open in the browser?

Atlassian Stash 以

的形式为 cloning/pushing/pulling 使用 repo URLs
https://mystashserver/scm/myproject/myrepo.git

如何将这个 URL 以

的形式转换为 Stash 的网络 UI 使用的那个
https://mystashserver/projects/myproject/repos/myrepo

在浏览器中打开第一个 URL 会自动转发到后者,但您不能添加其他参数,例如用于选择特定分支。

我正在寻找一种将第一种 URL 转换为后者的方法,非常适合在 Bash 脚本中使用。

以下在 Bash 脚本中使用 Bash 的内置正则表达式支持:

giturl=https://mystashserver/scm/myproject/myrepo.git

re='(.*)/scm/(.*)/(.*)\.git'
if [[ $giturl =~ $re ]]; then
  newgiturl=${BASH_REMATCH[1]}/projects/${BASH_REMATCH[2]}/repos/${BASH_REMATCH[3]}
  echo $newgiturl
fi

正则表达式将原来的 URL 分成几个部分:

  • (捕获组 1):协议、主机名、可选的 Web 根上下文
  • "scm":这似乎是 Atlassian Stash 的固定值
  • (Capture Group 2): 项目名称
  • (捕获组 3):实际的存储库名称,减去 .git 后缀

在上面的示例中,newgiturl 变量然后重新组装 URL,将 projectsrepos 部分注入所需位置。