Skip to content

Commit

Permalink
fix: github find PR link
Browse files Browse the repository at this point in the history
  • Loading branch information
chenasraf committed Jan 25, 2025
1 parent 434494e commit dabf678
Showing 1 changed file with 109 additions and 87 deletions.
196 changes: 109 additions & 87 deletions git-open.zsh
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ uriencode() {
for ((n = 0; n < len; n++)); do
c="${1:$n:1}"
case $c in
[a-zA-Z0-9.~_-]) printf "$c" ;;
*) printf '%%%02X' "'$c"
[a-zA-Z0-9.~_-]) printf "$c" ;;
*) printf '%%%02X' "'$c" ;;
esac
done
}
Expand All @@ -27,11 +27,11 @@ git_get_repo_path() {
repo_path=''

if [[ $remote =~ ^git@ ]]; then
repo_path=$(echo "$remote" | sed -E 's|^git@[^:]+:([^:]+)\.git$|\1|')
repo_path=$(echo "$repo_path" | sed -E 's|^git@[^:]+:([^:]+)$|\1|')
repo_path=$(echo "$remote" | sed -E 's|^git@[^:]+:([^:]+)\.git$|\1|')
repo_path=$(echo "$repo_path" | sed -E 's|^git@[^:]+:([^:]+)$|\1|')
elif [[ $remote =~ ^https?:// ]]; then
repo_path=$(echo "$remote" | sed -E 's|^https?://[^/]+/([^\.]+)\.git$|\1|')
repo_path=$(echo "$repo_path" | sed -E 's|^https?://[^/]+/([^\.]+)$|\1|')
repo_path=$(echo "$remote" | sed -E 's|^https?://[^/]+/([^\.]+)\.git$|\1|')
repo_path=$(echo "$repo_path" | sed -E 's|^https?://[^/]+/([^\.]+)$|\1|')
fi
echo $repo_path
}
Expand All @@ -41,10 +41,10 @@ git_get_remote_type() {
repo_path=$(git_get_repo_path $remote)
remote_type='github'
case $remote in
*github.com*) remote_type='github' ;;
*gitlab.com*) remote_type='gitlab' ;;
*bitbucket.org*) remote_type='bitbucket' ;;
*) return 1 ;;
*github.com*) remote_type='github' ;;
*gitlab.com*) remote_type='gitlab' ;;
*bitbucket.org*) remote_type='bitbucket' ;;
*) return 1 ;;
esac

echo $remote_type
Expand All @@ -66,13 +66,13 @@ git_open_project() {
fi

case "$remote_type" in
github) open_url "$silent" "https://github.com/$repo_path" ;;
gitlab) open_url "$silent" "https://gitlab.com/$repo_path" ;;
bitbucket) open_url "$silent" "https://bitbucket.org/$repo_path" ;;
*)
echo "Unknown remote type: $remote_type"
return 2
;;
github) open_url "$silent" "https://github.com/$repo_path" ;;
gitlab) open_url "$silent" "https://gitlab.com/$repo_path" ;;
bitbucket) open_url "$silent" "https://bitbucket.org/$repo_path" ;;
*)
echo "Unknown remote type: $remote_type"
return 2
;;
esac

return 0
Expand All @@ -95,9 +95,9 @@ git_open_branch() {
branch=$([[ -n "$2" ]] && echo "$2" || git branch --show-current)

case "$remote_type" in
github) open_url "$silent" "https://github.com/$repo_path/tree/$branch" ;;
gitlab) open_url "$silent" "https://gitlab.com/$repo_path/-/tree/$branch" ;;
bitbucket) open_url "$silent" "https://bitbucket.org/$repo_path/branch/$branch" ;;
github) open_url "$silent" "https://github.com/$repo_path/tree/$branch" ;;
gitlab) open_url "$silent" "https://gitlab.com/$repo_path/-/tree/$branch" ;;
bitbucket) open_url "$silent" "https://bitbucket.org/$repo_path/branch/$branch" ;;
esac

return 0
Expand All @@ -121,9 +121,9 @@ git_open_file() {
branch=$([[ -n "$3" ]] && echo "$3" || git branch --show-current)

case "$remote_type" in
github) open_url "$silent" "https://github.com/$repo_path/blob/$branch/$file" ;;
gitlab) open_url "$silent" "https://gitlab.com/$repo_path/-/blob/$branch/$file" ;;
bitbucket) open_url "$silent" "https://bitbucket.org/$repo_path/src/$file" ;;
github) open_url "$silent" "https://github.com/$repo_path/blob/$branch/$file" ;;
gitlab) open_url "$silent" "https://gitlab.com/$repo_path/-/blob/$branch/$file" ;;
bitbucket) open_url "$silent" "https://bitbucket.org/$repo_path/src/$file" ;;
esac

return 0
Expand All @@ -146,9 +146,9 @@ git_open_commit() {
commit=$([[ -n "$2" ]] && echo "$2" || git rev-parse HEAD)

case "$remote_type" in
github) open_url "$silent" "https://github.com/$repo_path/commit/$commit" ;;
gitlab) open_url "$silent" "https://gitlab.com/$repo_path/-/commit/$commit" ;;
bitbucket) open_url "$silent" "https://bitbucket.org/$repo_path/commit/$commit" ;;
github) open_url "$silent" "https://github.com/$repo_path/commit/$commit" ;;
gitlab) open_url "$silent" "https://gitlab.com/$repo_path/-/commit/$commit" ;;
bitbucket) open_url "$silent" "https://bitbucket.org/$repo_path/commit/$commit" ;;
esac

return 0
Expand All @@ -170,13 +170,13 @@ git_open_pr_list() {
repo_path=$(git_get_repo_path $remote)

case "$remote_type" in
github) open_url "$silent" "https://github.com/$repo_path/pulls?q=is%3Apr+is%3Aopen" ;;
gitlab) open_url "$silent" "https://gitlab.com/$repo_path/merge_requests?scope=all&state=opened" ;;
bitbucket) open_url "$silent" "https://bitbucket.org/$repo_path/pull-requests?state=OPEN" ;;
*)
echo "Unknown remote type: $remote_type"
return 2
;;
github) open_url "$silent" "https://github.com/$repo_path/pulls?q=is%3Apr+is%3Aopen" ;;
gitlab) open_url "$silent" "https://gitlab.com/$repo_path/merge_requests?scope=all&state=opened" ;;
bitbucket) open_url "$silent" "https://bitbucket.org/$repo_path/pull-requests?state=OPEN" ;;
*)
echo "Unknown remote type: $remote_type"
return 2
;;
esac

return 0
Expand Down Expand Up @@ -218,9 +218,9 @@ git_open_new_pr() {
default_branch=$(uriencode $default_branch)

case "$remote_type" in
github) open_url "$silent" "https://github.com/$repo_path/compare/$default_branch...$branch" ;;
gitlab) open_url "$silent" "https://gitlab.com/$repo_path/-/merge_requests/new?merge_request%5Bsource_branch%5D=$branch&merge_request%5Btarget_branch%5D=$default_branch" ;;
bitbucket) open_url "$silent" "https://bitbucket.org/$repo_path/pull-requests/new?source=$branch&t=1" ;;
github) open_url "$silent" "https://github.com/$repo_path/compare/$default_branch...$branch" ;;
gitlab) open_url "$silent" "https://gitlab.com/$repo_path/-/merge_requests/new?merge_request%5Bsource_branch%5D=$branch&merge_request%5Btarget_branch%5D=$default_branch" ;;
bitbucket) open_url "$silent" "https://bitbucket.org/$repo_path/pull-requests/new?source=$branch&t=1" ;;
esac

return 0
Expand All @@ -244,9 +244,18 @@ git_find_pr() {
commit="$(git rev-parse $branch)"

case "$remote_type" in
github) prrefs="pull/*/head"; prfilt="pull" ;;
gitlab) prrefs="merge-requests/*/head"; prfilt="merge-requests" ;;
bitbucket) prrefs="pull-requests/*/head"; prfilt="pull-requests" ;;
github)
prrefs="pull/*/head"
prfilt="pull"
;;
gitlab)
prrefs="merge-requests/*/head"
prfilt="merge-requests"
;;
bitbucket)
prrefs="pull-requests/*/head"
prfilt="pull-requests"
;;
esac

prid="$(git ls-remote origin $prrefs | grep "refs/$prfilt" | grep $commit | awk '{print $2}' | cut -d'/' -f3)"
Expand All @@ -256,9 +265,9 @@ git_find_pr() {
fi

case "$remote_type" in
github) echo "https://github.com/$repo_path/pulls/$prid" ;;
gitlab) echo "https://gitlab.com/$repo_path/-/merge_requests/$prid" ;;
bitbucket) echo "https://bitbucket.org/$repo_path/pull-requests/$prid" ;;
github) echo "https://github.com/$repo_path/pull/$prid" ;;
gitlab) echo "https://gitlab.com/$repo_path/-/merge_requests/$prid" ;;
bitbucket) echo "https://bitbucket.org/$repo_path/pull-requests/$prid" ;;
esac

return 0
Expand All @@ -284,9 +293,9 @@ git_open_pipelines() {

repo_path=$(git_get_repo_path $remote)
case "$remote_type" in
github) open_url "$silent" "https://github.com/$repo_path/actions" ;;
gitlab) open_url "$silent" "https://gitlab.com/$repo_path/pipelines?scope=all" ;;
bitbucket) open_url "$silent" "https://bitbucket.org/$repo_path/addon/pipelines/home" ;;
github) open_url "$silent" "https://github.com/$repo_path/actions" ;;
gitlab) open_url "$silent" "https://gitlab.com/$repo_path/pipelines?scope=all" ;;
bitbucket) open_url "$silent" "https://bitbucket.org/$repo_path/addon/pipelines/home" ;;
esac

return 0
Expand Down Expand Up @@ -316,53 +325,66 @@ git_open() {
fi

case $1 in
project|repo|repository|\.) git_open_project ;;
branch) git_open_branch $@ ;;
file) git_open_file $@ ;;
commit) git_open_commit $@ ;;
prs|mrs) shift; git_open_pr_list ;;
pr|mr) shift; git_open_new_pr $@ ;;
actions|pipelines|ci) shift; git_open_pipelines ;;
--version|-V)
u="$(tput smul)"
r="$(tput sgr0)"
echo "git-open v$(cat "${0:A:h}/version.txt")"
echo "${u}https://github.com/chenasraf/git-open${r}"
echo "Copyright \xC2\xA9 2024 Chen Asraf" ;;
_debug)
inf="Getting info"
y=$(tput setaf 3)
g=$(tput setaf 2)
r=$(tput sgr0)

echo -n "$r- ${y}$inf\r"
remote=$(git_get_remote)
echo -n "$r\\ ${y}$inf.\r"
info=$(git remote show $remote)
echo -n "$r| ${y}$inf..\r"
branch=$(git branch --show-current)
echo -n "$r/ ${y}$inf...\r"
commit=$(git rev-parse HEAD)
echo "${g}Done\e[0K$r\n"
echo "Remote: $remote"
echo "Repo Path: $(git_get_repo_path $remote)"
echo "Remote Type: $(git_get_remote_type $remote)"
echo "Current Branch: $branch"
echo "Default Branch: $(echo $info | grep "HEAD branch" | awk '{print $3}')"
echo "Current Ref: $commit"
;;
*)
echo "Unknown command: $1"
return 1
;;
project | repo | repository | \.) git_open_project ;;
branch) git_open_branch $@ ;;
file) git_open_file $@ ;;
commit) git_open_commit $@ ;;
prs | mrs)
shift
git_open_pr_list
;;
pr | mr)
shift
git_open_new_pr $@
;;
actions | pipelines | ci)
shift
git_open_pipelines
;;
--version | -V)
u="$(tput smul)"
r="$(tput sgr0)"
echo "git-open v$(cat "${0:A:h}/version.txt")"
echo "${u}https://github.com/chenasraf/git-open${r}"
echo "Copyright \xC2\xA9 2024 Chen Asraf"
;;
_debug)
inf="Getting info"
y=$(tput setaf 3)
g=$(tput setaf 2)
r=$(tput sgr0)

echo -n "$r- ${y}$inf\r"
remote=$(git_get_remote)
echo -n "$r\\ ${y}$inf.\r"
info=$(git remote show $remote)
echo -n "$r| ${y}$inf..\r"
branch=$(git branch --show-current)
echo -n "$r/ ${y}$inf...\r"
commit=$(git rev-parse HEAD)
echo "${g}Done\e[0K$r\n"
echo "Remote: $remote"
echo "Repo Path: $(git_get_repo_path $remote)"
echo "Remote Type: $(git_get_remote_type $remote)"
echo "Current Branch: $branch"
echo "Default Branch: $(echo $info | grep "HEAD branch" | awk '{print $3}')"
echo "Current Ref: $commit"
;;
*)
echo "Unknown command: $1"
return 1
;;
esac
}

while true; do
case "$1" in
open) shift ;;
-s|--silent) shift; silent="-s" ;;
*) break ;;
open) shift ;;
-s | --silent)
shift
silent="-s"
;;
*) break ;;
esac
done

Expand Down

0 comments on commit dabf678

Please sign in to comment.