Git钩产生Github的"Create Pull Request"像Bitbucket一样在终端中链接 [英] Git hook to produce Github "Create Pull Request" link in terminal like Bitbucket Does

查看:109
本文介绍了Git钩产生Github的"Create Pull Request"像Bitbucket一样在终端中链接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对Bitbucket感到非常方便的一件事情是,当您将一个新分支推到Bitbucket中托管的存储库时,它会打印出(到终端屏幕)一个URL,您可以点击该URL来从该分支创建PR你刚刚推.例如:

One of the things I find very handy about Bitbucket is when you push a new branch up to a repo hosted in Bitbucket, it prints out (to the terminal screen) a URL that you can hit to create a PR from that branch you just pushed. Ex:

$ 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屏幕.有人知道这样的事吗?

I find this to be a huge timesaver over going to Bitbucket, navigating to the repo, finding the "Create Pull Request" button, etc. As such, I'd like something similar for when I'm working with a repo hosted on Github -- after a push of a new branch, have it print out to the terminal a URL I can hit to get to the create PR screen on Github. Anyone know of anything like this?

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

I know there's a CLI for Github with a pull-request command, but that prompts you for your password every time which is very annoying, and TBH I like to look at the diff in the UI before actually creating the PR.

推荐答案

创建了自己的本地钩子,足以满足我的需求.将其作为pre-push钩子添加到您的存储库本地克隆中:

Created my own local hook that works well enough for my needs. Add this as a pre-push hook to your local clone of a 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

然后我可以将刚刚推送的分支作为源分支,击中发出的url以登陆Github中的create pull request页面.

I can then hit that url emitted to land on the create pull request page in Github with the branch I just pushed as the source branch.

这与Bitbucket完全不同,因为它在本地运行(Bitbucket一个在远程运行),所以它不那么智能(例如:即使推送导致远程上没有任何更改,它仍然会发出URL) , 等等).但这符合我的需求:当我推送到Github存储库时,我可以从终端窗口中单击链接以转到Github中的创建PR页面."

This isn't quite equivalent to Bitbucket, as it's run locally (the Bitbucket one is run on the remote) so it's not as intelligent (ex: it still emits the URL even if the push resulted in no changes on the remote, etc). But it suits my need of "when I push to a Github repo, I can click a link from my terminal window to get to the create PR page in Github".

这篇关于Git钩产生Github的"Create Pull Request"像Bitbucket一样在终端中链接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆