Last active
August 12, 2020 16:59
-
-
Save zshift/2aeff5474f1ddae18d76d4bc222be69c to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # ------------------------------------------------------- | |
| # npr | |
| # Creates a PR on github for the current branch to the | |
| # default branch. | |
| # | |
| # Note: Only tested on | |
| # * macOS 10.15.5 | |
| # * zsh 5.7.1 (x86_64-apple-darwin19.0) | |
| # | |
| # 1. Checks if the current directory is within a git repo | |
| # 2. Parses the remote URL and formats it to an https URL | |
| # 3. Opens the URL in the default browser to create a PR | |
| # ------------------------------------------------------- | |
| function npr() { | |
| git rev-parse --is-inside-work-tree 1>/dev/null 2>/dev/null | |
| if [[ $? != 0 ]]; then | |
| echo Usage: npr >&2 | |
| echo >&2 | |
| echo Only valid inside a git repository. >&2 | |
| return 1 | |
| fi | |
| default=$(git symbolic-ref refs/remotes/origin/HEAD | sed -e 's@^refs/remotes/origin/@@') | |
| current_branch=$(git rev-parse --abbrev-ref HEAD) | |
| if [[ $default == $current_branch ]]; then | |
| echo Currently on $default branch. No need to submit a PR. >&2 | |
| return 2 | |
| fi | |
| tracking_branch=$(git for-each-ref --format='%(upstream:short)' $(git symbolic-ref -q HEAD)) | |
| if [[ -z $tracking_branch ]]; then | |
| echo Please push your branch before submitting a PR. >&2 | |
| return 3 | |
| fi | |
| url=$(git config --get remote.origin.url) | |
| if [[ $url =~ (https:\/\/.*)\.git ]]; then | |
| url=$match[1] | |
| elif [[ $url =~ git@(.*):(.*)\.git ]]; then | |
| url="https://${match[1]}/${match[2]}" | |
| else | |
| echo Failed to parse git repository URL. >&2 | |
| return 4 | |
| fi | |
| url="$url/compare/$default...$current_branch" | |
| open $url | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Currently works only for github.