tracking branchのremote URLをみるpropen

で知ったGitHub - 作業中ブランチのプルリクエストをブラウザで開く - Qiitaがとても便利なんですが、origin以外にpushしているとプルリクの作成ができない(すでにプルリクがある場合はリダイレクトされる)ので、tracking branchのremote urlを参照するやつを書いた

function propen() {
  local current_branch_name=$(git symbolic-ref --short HEAD | xargs perl -MURI::Escape -e 'print uri_escape($ARGV[0]);')
  local remote=$(git branch -vv | grep '^*' | awk '{ print $4 }' | cut -d/ -f1 | sed -e 's/^\[//')
  local repo_url=$(git remote show -n $remote | grep 'Push  URL' | grep -E -o '[^ ]+$' | sed -e 's|^https://||' -e 's/^git@//' -e 's/\.git$//' -e 's|:|/|')
  open "https://${repo_url}/pull/${current_branch_name}"
}
$ git remote add fork git@github.com:ryotarai/itamae.git
$ git push -u fork awesome-feature
$ propen

とか叩くと、https://github.com/ryotarai/itamae/pull/awesome-featureが開く