1
Fork 0
config/git/scripts/git-cpr

24 lines
752 B
Bash
Executable File

#!/bin/sh
set -o errexit
[ $# != 1 ] && { >&2 echo 'Argument: <pull request id>'; exit 1; }
# Get origin remote
origin=$(git remote get-url origin)
# Find target ("owner/repo") of PR
temp=${origin#*github.com?}
[ $origin = $temp ] && { >&2 echo Unrecognized origin.; exit 1; }
target=${temp%.git}
# Find source ("owner/repo") of PR and remote name and branch name
temp=$(curl --silent https://github.com/$target/pull/$1 | grep --max-count=1 head-ref | cut --delimiter=\" --fields=2)
source=${temp%%:*}
remote=$(echo $origin | sed "s $target $source ")
branch=${temp#*:}
# Set up branch and checkout
git fetch --no-tags $remote $branch:pr/$1
git config branch.pr/$1.remote $remote
git config branch.pr/$1.merge refs/heads/$branch
git checkout pr/$1