Skip to content

Instantly share code, notes, and snippets.

@pcd1193182
Created August 27, 2020 19:18
Show Gist options
  • Save pcd1193182/a0aca4fa719060ccd17a2c6cdde78ac7 to your computer and use it in GitHub Desktop.
Save pcd1193182/a0aca4fa719060ccd17a2c6cdde78ac7 to your computer and use it in GitHub Desktop.
Git pre-push hook that prints correct github PR creation url
#!/bin/sh
remote="$1"
url="$2"
z40=0000000000000000000000000000000000000000
if [[ ! "$url" =~ github ]]; then
echo "doesn't match github"
exit 0
fi
upstream=$(git rev-parse --abbrev-ref --symbolic-full-name @{u} || true)
if [[ -z "$upstream" ]]; then
echo "no upstream set, skipping"
exit 0
fi
remote=$(echo $upstream | sed 's@/.*@@')
remote=$(git remote get-url $remote)
if [[ "$remote" =~ @ ]]; then
# Convert from ssh to https
core=$(echo $remote | sed 's/^.*:\(.*\).git$/\1/')
remote="https://github.com/$core"
fi
upstream_branch=$(echo $upstream | sed 's@^[^/]*/@@')
read local_ref local_sha remote_ref remote_sha
if [[ -z "$remote_sha" || "$remote_sha" != "$z40" ]]; then
echo "not new branch, skipping"
exit 0
fi
tgt_owner=$(echo "$url" | sed 's@.*:\(.*\)/.*@\1@')
tgt_branch=$(echo "$remote_ref" | sed 's@refs/heads/@@')
echo "${remote}/compare/${upstream_branch}...${tgt_owner}:${tgt_branch}?expand=1"
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment