Skip to content

Instantly share code, notes, and snippets.

@shireeshj
Forked from thegedge/git-tophat
Created March 31, 2021 07:57
Show Gist options
  • Save shireeshj/a37c2245d3b654f8fe2fc621da3893a0 to your computer and use it in GitHub Desktop.
Save shireeshj/a37c2245d3b654f8fe2fc621da3893a0 to your computer and use it in GitHub Desktop.
A git subcommand to "tophat" (build/run/test) a PR from a fork
#!/bin/zsh
function main {
if [[ ! "$1" =~ ".*:.*" ]]; then
echo "Empty username or branch name: \`$1\` should be of the form \`user:branch\`"
exit 1
fi
local username="${1%:*}"
local branch="${1#*:}"
local tophat_url="$(git remote get-url origin | rg -o '(.*[/:])\w+/(\w+).git' -r "\${1}${username}/\${2}")"
git remote rm tophat &>/dev/null
git remote add -f -t "${branch}" tophat "${tophat_url}" \
&& git checkout "tophat/${branch}"
}
main "$@"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment