Skip to content

Commit

Permalink
Add git co-upstream-pr command (#726)
Browse files Browse the repository at this point in the history
* Add git co-upstream-pr

When reviewing Pull Requests from open source projects, it's handy
to have a way to just pass the PR number and the local branch name.

With this new command, to fetch a PR from an upstream remote repo,
we just need the PR number and the name of the local branch:

$ co-upstream-pr 001 test_generator_pr

git will checkout the upstream PR in the local test_generator_pr branch.

If there is no upstream remote set, we get a nice message guiding
users to set it first.

Co-authored-by: Mina Slater <[email protected]>

* Use heredoc to send output into cat instead of using echo and multi-line quotes

* Subcommand, not alias

* Fail gracefully and exit if PR number or local branch name are not provided

Co-authored-by: Mike Burns <[email protected]>

---------

Co-authored-by: Mina Slater <[email protected]>
Co-authored-by: Mike Burns <[email protected]>
  • Loading branch information
3 people authored Aug 8, 2023
1 parent 00db026 commit 155e855
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,7 @@ configuration:

[git](http://git-scm.com/) configuration:

- Adds a `co-upstream-pr $PR_NUMBER $LOCAL_BRANCH_NAME` subcommand to checkout remote upstream branch into a local branch.
- Adds a `create-branch` alias to create feature branches.
- Adds a `delete-branch` alias to delete feature branches.
- Adds a `merge-branch` alias to merge feature branches into master.
Expand Down
24 changes: 24 additions & 0 deletions bin/git-co-upstream-pr
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#!/bin/sh

set -e

pull_request_number=$1
local_branch_name=$2

if [ -z "$pull_request_number" -o -z "$local_branch_name" ]; then
echo "usage: git co-upstream-pr <pull_request_number> <local_branch_name>"
exit 1
fi

if git remote -v | grep -q upstream; then
git fetch upstream "pull/$pull_request_number/head:$local_branch_name"
git checkout "$local_branch_name"
else
cat <<HELP
You don't have an upstream remote set.
Use:
git remote add upstream {upstream_remote_url}
to set the reference and then try again.
HELP
fi

0 comments on commit 155e855

Please sign in to comment.