From 155e85523e56eb56b485e5cc43a04dad67183640 Mon Sep 17 00:00:00 2001 From: Stefanni Brasil Date: Tue, 8 Aug 2023 09:30:25 -0600 Subject: [PATCH] Add git co-upstream-pr command (#726) * 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 * 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 --------- Co-authored-by: Mina Slater Co-authored-by: Mike Burns --- README.md | 1 + bin/git-co-upstream-pr | 24 ++++++++++++++++++++++++ 2 files changed, 25 insertions(+) create mode 100755 bin/git-co-upstream-pr diff --git a/README.md b/README.md index e02aa3697f..e700e5c5f8 100644 --- a/README.md +++ b/README.md @@ -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. diff --git a/bin/git-co-upstream-pr b/bin/git-co-upstream-pr new file mode 100755 index 0000000000..f427e8d9b4 --- /dev/null +++ b/bin/git-co-upstream-pr @@ -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 " + 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 <