Created
October 21, 2021 15:19
-
-
Save kba/14cfd05ee6a3a28dd0198a8a468e50c9 to your computer and use it in GitHub Desktop.
This file contains 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
#!/usr/bin/env bash | |
# reset environment variables that could interfere with normal usage | |
unset GREP_OPTIONS | |
# put all utility functions here | |
# make a temporary file | |
git_extra_mktemp() { | |
mktemp -t "$(basename "$0")".XXXXXXX | |
} | |
# | |
# check whether current directory is inside a git repository | |
# | |
is_git_repo() { | |
git rev-parse --show-toplevel > /dev/null 2>&1 | |
result=$? | |
if test $result != 0; then | |
>&2 echo 'Not a git repo!' | |
exit $result | |
fi | |
} | |
is_git_repo | |
# Based on https://gist.github.com/gnarf/5406589 and https://gist.github.com/jhnns/d654d9d6da6d3b749986 | |
if test "$1" = "clean"; then | |
git for-each-ref refs/heads/pr/* --format='%(refname)' | while read ref; do | |
git branch -D ${ref#refs/heads/} | |
done | |
exit 0 | |
elif [[ $1 =~ ^(https?://[^/]+/(.+))/pull/([0-9]+).*$ ]]; then | |
remote=${BASH_REMATCH[1]}.git | |
id=${BASH_REMATCH[3]} | |
branch=pr/$id | |
else | |
test -z $1 && echo "pr number required." 1>&2 && exit 1 | |
remote=${2:-origin} | |
id=$1 | |
branch=pr/$id | |
fi | |
git fetch -fu $remote refs/pull/$id/head:$branch && \ | |
git checkout $branch && \ | |
git config --local --replace branch.$branch.merge refs/pull/$id/head && \ | |
git config --local --replace branch.$branch.remote $remote; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment