Git hook to produce Github “Create Pull Request” l

2020-06-03 05:06发布

问题:

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

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?

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.

回答1:

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

Example output:

$ 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

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.

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



回答2:

I ended up using this (on gitbash, with no rev and using the more recent 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