Skip to content

Instantly share code, notes, and snippets.

@hayajo
Last active February 23, 2024 03:56
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hayajo/d391e31513dfb11d419fa28ce2224d02 to your computer and use it in GitHub Desktop.
Save hayajo/d391e31513dfb11d419fa28ce2224d02 to your computer and use it in GitHub Desktop.
「あるcommitが含まれるPullRequestをシェルから開く」を自分用にカスタマイズ
[alias]
find-pr = "!git-find-pr.sh $(git log --oneline | fzf | awk '{ print $1 }') "
#!/bin/sh
#
# あるcommitが含まれるPullRequestをシェルから開く https://zenn.dev/nekoya/articles/5cf5037c8d23b3
# ...をちょっと修正
#
# Usage: find-pr.sh <commit>
default_branch=$(git symbolic-ref --short refs/remotes/origin/HEAD | awk -F '/' '{print $NF}')
pr_number=$(git log --merges --ancestry-path --oneline "$1".."$default_branch" | grep -i 'merge pull request' | tail -1 | awk '{print $5}' | cut -d# -f2)
if [ -z "$pr_number" ]; then
echo "PR番号が見つからんかったわ。"
exit 1
fi
repo_url=$(git remote get-url origin | sed -E 's/^.+@github\.com[:\/](.*).git/https:\/\/github.com\/\1/')
if [ -z "$repo_url" ]; then
echo "リポジトリのURLが取れんかったわ。"
exit 1
fi
pr_url="${repo_url}/pull/${pr_number}"
echo "$pr_url"
open "$pr_url"
@hayajo
Copy link
Author

hayajo commented Sep 28, 2023

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment