Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Git hook to produce Github "Create Pull Request" link in terminal like Bitbucket Does

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.

like image 852
Adam Parkin Avatar asked Oct 17 '17 15:10

Adam Parkin


People also ask

How do I make a pull request in GitHub terminal?

Once you've committed changes to your local copy of the repository, click the Create Pull Request icon. Check that the local branch and repository you're merging from, and the remote branch and repository you're merging into, are correct. Then give the pull request a title and a description. Click Create.

How do I Create a pull request URL?

Existing pull requests have an ID in the URL, which you'll need to specify; if you don't know the ID, then you'll need to get the URL from the GUI ( https://bitbucket.org/owner/repo/pull-requests/ is probably the easiest place to find it).


1 Answers

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

like image 97
Adam Parkin Avatar answered Sep 21 '22 02:09

Adam Parkin