Skip to content

Instantly share code, notes, and snippets.

@xqin
Last active July 12, 2019 06:57
Show Gist options
  • Save xqin/f0e5417117e61295f5c0166358528e19 to your computer and use it in GitHub Desktop.
Save xqin/f0e5417117e61295f5c0166358528e19 to your computer and use it in GitHub Desktop.
快速在浏览器中打开当前仓库的 WEB URL 地址
命令: repo
功能: 用于快速在浏览器中打开当前仓库的 WEB URL 地址
使用方式: 复制下面的内容粘贴到 ~/.bash_profile 中, 之后关闭Terminal, 重新打开 生效, 之后进入任意一个git仓库的目录中, 输入 repo 即可看到效果.
```bash
## {{{ open git repo remote url in browser
function repo () {
# 使用 git 命令获取 remote origin 的 url
local url=$(git config remote.origin.url)
# 如果为空, 则说明不是git仓库, 或者只是本地仓库, 没有对应的远程地址
if [ -z "$url" ]; then
echo can not find remote.origin.url
return
fi
# 判断远程仓库地址是否以 http/https 开头
local isUrl=$(echo -n $url | grep -E '^https?://')
# 如果是, 则直接调用 open 来打开它
if [ ! -z $isUrl ]; then
open "$url"
return
fi
# 判断远程仓库是否是 ssh 协议 git@xxxx:xxx.git 这样的
local isSSH=$(echo -n $url | grep -E '^(ssh:\/\/)?.+?@.+?:.+$')
# 如果是, 则将 url 替换为 https 协议的地址, 然后调用 open 打开它
if [ -n $isSSH ]; then
open "$(echo -n $url | sed -E 's,^ssh://,,' | sed -E 's/:([0-9]+\/)?/\//' | sed -E 's/^.+@/http:\/\//')"
return
fi
# 如果上面都不成立则输出 原url地址, 反馈给我, 以便进一步适配它
echo can not recognize: $url
}
## }}}
```
目前可识别的仓库地址格式有:
```
git@xxx.com:xxx.git
ssh://git@xxxx:/xxx.git
ssh://asd@xxxx:12345/xxxx/xxxxxx
```
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment