【问题标题】:Git hook to produce Github "Create Pull Request" link in terminal like Bitbucket DoesGit 钩子在 Bitbucket 等终端中生成 Github“创建拉取请求”链接
【发布时间】:2017-10-17 15:52:07
【问题描述】:

我发现 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、导航到存储库、找到“创建拉取请求”按钮等节省了 巨大 时间。因此,我想要类似的东西我正在使用托管在 Github 上的一个存储库——在推送一个新分支之后,让它打印到终端一个我可以点击的 URL 以进入 Github 上的创建 PR 屏幕。有人知道这样的事吗?

我知道 Github 有一个带有 pull-request 命令的 CLI,但是每次都会提示您输入密码,这很烦人,而且 TBH 我喜欢在实际创建 PR 之前查看 UI 中的差异。

【问题讨论】:

    标签: git github bitbucket


    【解决方案1】:

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

    #!/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 中的创建拉取请求页面,并将我刚刚推送的分支作为源分支。

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

    【讨论】:

    • 它运行良好,但你为什么使用 grep "github.com"?没有它,它似乎也能胜任。
    • 因为我也经常使用来自 Bitbucket.com 的 repos,对于那些 repos,当我进行推送时,我不希望这个钩子吐出 echo 的东西。
    • https://github.com/$userRepo/compare/$base...$branch?expand=1 将指定 PR 的基本分支(目标)。 (这是在哪里记录的?)
    • 无赖,我的 gitbash 缺少 rev。我需要找到一种方法来更换那部分
    【解决方案2】:

    我最终使用了这个(在 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
    

    【讨论】:

      【解决方案3】:

      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 ""
      

      【讨论】:

        猜你喜欢
        • 2020-09-17
        • 2018-06-04
        • 2012-09-20
        • 1970-01-01
        • 2012-02-24
        • 2021-09-08
        • 2021-02-17
        • 2018-04-15
        • 1970-01-01
        相关资源
        最近更新 更多