在Bash(OSX)中的Google Chrome中打开一个管道URL



我正试图通过管道将一个值(Git的原始URL,用sed操作(传递到OSX上bash中的Chrome。我尝试过以下几种:

我可以让这两个独立的部分工作,但当我把它们放在一起时,Chrome并没有像我预期的那样打开URL。

  • git config --get remote.origin.url | sed "s/git@/https:///g"(输出https://github.com/myProject/(

  • echo 'https://example.com' | /usr/bin/open -a "/Applications/Google Chrome.app"(正确打开https://example.com(

但是,如果我把这两者粘在一起,Chrome会打开,但只会打开一个空白页(about:blank(:

  • git config --get remote.origin.url | sed "s/git@/https:///g" | xargs /usr/bin/open -a "/Applications/Google Chrome.app"
  • git config --get remote.origin.url | sed "s/git@/https:///g" | /usr/bin/open -a "/Applications/Google Chrome.app"

在我的机器上,我发现我必须进行

git config --get remote.origin.url | sed 's/.git//; s/git@/www./; s#https://#www.#; s#.com:#.com/#'

为了获得输出

www.github.com/myProject/

这是chrome似乎喜欢的URL格式。

  1. 我添加了's/.git//;以消除URL末尾的.git
  2. 我添加了s/git@/www./; s#https://#www.#;以用www.交换git@(用于ssh克隆的repos(和https://(用于非ssh克隆的reos(
  3. 我添加了s#.com:#.com/#以去掉冒号":">

这是整个过程

git config --get remote.origin.url | sed 's/.git//; s/git@/www./; s#https://#www.#; s#.com:#.com/#' | xargs /usr/bin/open -a "/Applications/Google Chrome.app"

更新

更稳健的解决方案:
1。验证远程源URL是否存在
2。更新了sed脚本
3。脚本失败时的错误语句

git config --get remote.origin.url &>/dev/null && git config --get remote.origin.url | sed 's/.git//; s/git@//; s#https://##; s#:#/#g' | xargs firefox || echo 'Error: no git remote URL

最新更新