Git hook生成Github" Create Pull Request"像Bitbucket这样的终端链接

时间:2017-10-17 15:52:07

标签: git github bitbucket

我觉得Bitbucket非常方便的一件事就是当你把一个新的分支推到Bitbucket托管的repo时,它打印出(到终端屏幕)你可以点击的URL来从该分支创建一个PR你刚才推了例如:

$ git push origin someBranch
Compressing objects: 100% (3/3), done.
Writing objects: 100% (3/3), 313 bytes | 0 bytes/s, done.
Total 3 (delta 2), reused 0 (delta 0)
remote:
remote: Create pull request for someBranch:
remote:   https://bitbucket.mydomain.com/projects/PRO/repos/someRepo/compare/commits?sourceBranch=refs/heads/someBranch
remote:
To ssh://bitbucket.mydomain.com:7999/pro/somerepo.git
f6718d4..410cbcb  someBranch -> someBranch

我发现这是一个巨大的节省时间,而不是去Bitbucket,导航到回购,找到"创建拉动请求"因此,当我使用Github上托管的repo时,我喜欢类似的东西 - 在推出新分支后,将它打印到终端我可以使用的URL点击进入Github上的创建PR屏幕。有人知道这样的事吗?

我知道有一个用于Github的CLI使用pull-request命令,但每次提示你输入密码都非常烦人,TBH我喜欢在实际用户界面之前查看差异创造公关。

3 个答案:

答案 0 :(得分:10)

创建了我自己的本地钩子,它足以满足我的需求。将此作为pre-push挂钩添加到repo的本地克隆:

#!/bin/sh

branch=$(git rev-parse --abbrev-ref HEAD)
userRepo=$(git remote -v | grep fetch | awk '{print $2}' | grep "github.com" | cut -d':' -f2 | rev | cut -c5- | rev)

if [ -n "$userRepo" ]
then
    echo ""
    echo "Create PR at: https://github.com/$userRepo/compare/$branch?expand=1"
    echo ""
fi

示例输出:

$ git push origin testouthooks

Create PR at: https://github.com/pzelnip/dotfiles/compare/testouthooks?expand=1

Counting objects: 3, done.
Delta compression using up to 8 threads.
Compressing objects: 100% (3/3), done.
Writing objects: 100% (3/3), 284 bytes | 0 bytes/s, done.
Total 3 (delta 2), reused 0 (delta 0)
remote: Resolving deltas: 100% (2/2), completed with 2 local objects.
To github.com:pzelnip/dotfiles.git
f7c29b8..f6f9347  testouthooks -> testouthooks

然后我可以点击那个在Github的创建拉取请求页面上发出的url,并将我刚刚推送的分支作为源分支。

这与Bitbucket不太相同,因为它在本地运行(Bitbucket在远程运行)因此它不那么智能(例如:即使推送导致遥控器没有变化,它仍会发出URL等)。但它符合我的需求“当我推送到Github仓库时,我可以从我的终端窗口点击链接到Github中的创建PR页面。”

答案 1 :(得分:1)

我最终使用了它(在gitbash上,没有rev并使用了最新的bitbucket):

#!/bin/sh

bbHost='bitbucket.org'
branch=$(git rev-parse --abbrev-ref HEAD)    
userRepo=$(git remote -v | grep fetch | awk '{print $2}' | grep "$bbHost" | cut -d':' -f2 | sed "s/.git//g")

echo "hey!"
echo $userRepo

if [ -n "$userRepo" ]
then
    echo ""
    echo "Create PR at: https://$bbHost/$userRepo/pull-requests/new?source=$branch"
    echo ""
    # remove this line if you don't want the browser to open
    start chrome https://$bbHost/$userRepo/pull-requests/new?source=$branch
fi

答案 2 :(得分:0)

pre-push git hook 实际提供了 2 个您可以使用的参数,使用这些我想出了下面更简单的版本。

  • $1 给出遥控器,例如'origin' - 这里没有用
  • $2 是网址。它的后缀是 .git,我们需要用 sed 去掉它,使用 2g 会跳过第一次命中(所以我们不会将 .github.com 转换为 .hub.com)
#!/bin/sh
url=$(echo "$2" | sed 's/.git//2g')
branch=$(git rev-parse --abbrev-ref HEAD)

echo ""
echo "Create PR at: $url/compare/$branch?expand=1"
echo ""

我还使用 Azure DevOps 存储库,这是在那里工作的版本。在这种情况下,url 没有问题,但我们需要将分支名称中的正斜杠转换为“%2F”:

#!/bin/sh
url="$2"
branch=$(git rev-parse --abbrev-ref HEAD | sed 's./.%2F.g')

echo ""
echo "Create PR at: $url/pullrequestcreate?sourceRef=$branch"
echo ""