Git钩子生产Github“;“创建拉取请求”;像Bitbucket一样在终端中链接

Git钩子生产Github“;“创建拉取请求”;像Bitbucket一样在终端中链接,git,github,bitbucket,Git,Github,Bitbucket,我发现Bitbucket非常方便的一件事是,当你将一个新分支推送到Bitbucket中托管的回购协议时,它会打印出一个URL(到终端屏幕),你可以点击该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

我发现Bitbucket非常方便的一件事是,当你将一个新分支推送到Bitbucket中托管的回购协议时,它会打印出一个URL(到终端屏幕),你可以点击该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、导航到repo、查找“创建拉取请求”按钮等节省了大量时间。因此,当我使用Github上托管的repo时,我想要类似的东西——在推送一个新分支后,将它打印到终端上一个URL,我可以点击该URL进入Github上的创建PR屏幕。有人知道这样的事吗


我知道Github有一个CLI,其中包含一个
pull request
命令,但每次都会提示您输入密码,这非常烦人,而且我喜欢在实际创建PR之前查看UI中的差异。我创建了自己的本地钩子,可以很好地满足我的需要。将此作为预推钩子添加到回购的本地克隆:

#!/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
然后,我可以点击发出的url,在Github中的CreatePull请求页面上,将我刚才推送的分支作为源分支

这并不完全等同于Bitbucket,因为它是在本地运行的(Bitbucket是在远程运行的),所以它没有那么智能(例如:即使推送没有导致远程更改,它仍然会发出URL,等等)。但它满足了我的需求:“当我推到Github回购时,我可以单击终端窗口中的链接,进入Github中的创建PR页面。”

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

actual提供了两个可以使用的参数,使用这些参数,我得到了下面更简单的版本

  • $1表示遥控器,例如“origin”-此处不可用
  • $2是url。它的后缀是.git,我们需要用sed去掉它,使用2g跳过第一次命中(因此我们不将.github.com转换为.hub.com)
我还与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 ""

它工作得很好,但是为什么要使用grep“github.com”?它似乎不用它就能完成任务。因为我也经常使用Bitbucket.com上的回购协议,而对于那些推送时的回购协议,我不希望这个钩子吐出回声内容。
https://github.com/$userRepo/compare/$base…$branch?expand=1
将指定PR的基本分支(目标)(记录在哪里?)Bummer,我的gitbash缺少
rev
。我得想办法换掉那个零件
#!/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 ""
#!/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 ""