Skip to content

Commit dabf678

Browse files
committed
fix: github find PR link
1 parent 434494e commit dabf678

File tree

1 file changed

+109
-87
lines changed

1 file changed

+109
-87
lines changed

git-open.zsh

Lines changed: 109 additions & 87 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@ uriencode() {
1111
for ((n = 0; n < len; n++)); do
1212
c="${1:$n:1}"
1313
case $c in
14-
[a-zA-Z0-9.~_-]) printf "$c" ;;
15-
*) printf '%%%02X' "'$c"
14+
[a-zA-Z0-9.~_-]) printf "$c" ;;
15+
*) printf '%%%02X' "'$c" ;;
1616
esac
1717
done
1818
}
@@ -27,11 +27,11 @@ git_get_repo_path() {
2727
repo_path=''
2828

2929
if [[ $remote =~ ^git@ ]]; then
30-
repo_path=$(echo "$remote" | sed -E 's|^git@[^:]+:([^:]+)\.git$|\1|')
31-
repo_path=$(echo "$repo_path" | sed -E 's|^git@[^:]+:([^:]+)$|\1|')
30+
repo_path=$(echo "$remote" | sed -E 's|^git@[^:]+:([^:]+)\.git$|\1|')
31+
repo_path=$(echo "$repo_path" | sed -E 's|^git@[^:]+:([^:]+)$|\1|')
3232
elif [[ $remote =~ ^https?:// ]]; then
33-
repo_path=$(echo "$remote" | sed -E 's|^https?://[^/]+/([^\.]+)\.git$|\1|')
34-
repo_path=$(echo "$repo_path" | sed -E 's|^https?://[^/]+/([^\.]+)$|\1|')
33+
repo_path=$(echo "$remote" | sed -E 's|^https?://[^/]+/([^\.]+)\.git$|\1|')
34+
repo_path=$(echo "$repo_path" | sed -E 's|^https?://[^/]+/([^\.]+)$|\1|')
3535
fi
3636
echo $repo_path
3737
}
@@ -41,10 +41,10 @@ git_get_remote_type() {
4141
repo_path=$(git_get_repo_path $remote)
4242
remote_type='github'
4343
case $remote in
44-
*github.com*) remote_type='github' ;;
45-
*gitlab.com*) remote_type='gitlab' ;;
46-
*bitbucket.org*) remote_type='bitbucket' ;;
47-
*) return 1 ;;
44+
*github.com*) remote_type='github' ;;
45+
*gitlab.com*) remote_type='gitlab' ;;
46+
*bitbucket.org*) remote_type='bitbucket' ;;
47+
*) return 1 ;;
4848
esac
4949

5050
echo $remote_type
@@ -66,13 +66,13 @@ git_open_project() {
6666
fi
6767

6868
case "$remote_type" in
69-
github) open_url "$silent" "https://github.com/$repo_path" ;;
70-
gitlab) open_url "$silent" "https://gitlab.com/$repo_path" ;;
71-
bitbucket) open_url "$silent" "https://bitbucket.org/$repo_path" ;;
72-
*)
73-
echo "Unknown remote type: $remote_type"
74-
return 2
75-
;;
69+
github) open_url "$silent" "https://github.com/$repo_path" ;;
70+
gitlab) open_url "$silent" "https://gitlab.com/$repo_path" ;;
71+
bitbucket) open_url "$silent" "https://bitbucket.org/$repo_path" ;;
72+
*)
73+
echo "Unknown remote type: $remote_type"
74+
return 2
75+
;;
7676
esac
7777

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

9797
case "$remote_type" in
98-
github) open_url "$silent" "https://github.com/$repo_path/tree/$branch" ;;
99-
gitlab) open_url "$silent" "https://gitlab.com/$repo_path/-/tree/$branch" ;;
100-
bitbucket) open_url "$silent" "https://bitbucket.org/$repo_path/branch/$branch" ;;
98+
github) open_url "$silent" "https://github.com/$repo_path/tree/$branch" ;;
99+
gitlab) open_url "$silent" "https://gitlab.com/$repo_path/-/tree/$branch" ;;
100+
bitbucket) open_url "$silent" "https://bitbucket.org/$repo_path/branch/$branch" ;;
101101
esac
102102

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

123123
case "$remote_type" in
124-
github) open_url "$silent" "https://github.com/$repo_path/blob/$branch/$file" ;;
125-
gitlab) open_url "$silent" "https://gitlab.com/$repo_path/-/blob/$branch/$file" ;;
126-
bitbucket) open_url "$silent" "https://bitbucket.org/$repo_path/src/$file" ;;
124+
github) open_url "$silent" "https://github.com/$repo_path/blob/$branch/$file" ;;
125+
gitlab) open_url "$silent" "https://gitlab.com/$repo_path/-/blob/$branch/$file" ;;
126+
bitbucket) open_url "$silent" "https://bitbucket.org/$repo_path/src/$file" ;;
127127
esac
128128

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

148148
case "$remote_type" in
149-
github) open_url "$silent" "https://github.com/$repo_path/commit/$commit" ;;
150-
gitlab) open_url "$silent" "https://gitlab.com/$repo_path/-/commit/$commit" ;;
151-
bitbucket) open_url "$silent" "https://bitbucket.org/$repo_path/commit/$commit" ;;
149+
github) open_url "$silent" "https://github.com/$repo_path/commit/$commit" ;;
150+
gitlab) open_url "$silent" "https://gitlab.com/$repo_path/-/commit/$commit" ;;
151+
bitbucket) open_url "$silent" "https://bitbucket.org/$repo_path/commit/$commit" ;;
152152
esac
153153

154154
return 0
@@ -170,13 +170,13 @@ git_open_pr_list() {
170170
repo_path=$(git_get_repo_path $remote)
171171

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

182182
return 0
@@ -218,9 +218,9 @@ git_open_new_pr() {
218218
default_branch=$(uriencode $default_branch)
219219

220220
case "$remote_type" in
221-
github) open_url "$silent" "https://github.com/$repo_path/compare/$default_branch...$branch" ;;
222-
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" ;;
223-
bitbucket) open_url "$silent" "https://bitbucket.org/$repo_path/pull-requests/new?source=$branch&t=1" ;;
221+
github) open_url "$silent" "https://github.com/$repo_path/compare/$default_branch...$branch" ;;
222+
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" ;;
223+
bitbucket) open_url "$silent" "https://bitbucket.org/$repo_path/pull-requests/new?source=$branch&t=1" ;;
224224
esac
225225

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

246246
case "$remote_type" in
247-
github) prrefs="pull/*/head"; prfilt="pull" ;;
248-
gitlab) prrefs="merge-requests/*/head"; prfilt="merge-requests" ;;
249-
bitbucket) prrefs="pull-requests/*/head"; prfilt="pull-requests" ;;
247+
github)
248+
prrefs="pull/*/head"
249+
prfilt="pull"
250+
;;
251+
gitlab)
252+
prrefs="merge-requests/*/head"
253+
prfilt="merge-requests"
254+
;;
255+
bitbucket)
256+
prrefs="pull-requests/*/head"
257+
prfilt="pull-requests"
258+
;;
250259
esac
251260

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

258267
case "$remote_type" in
259-
github) echo "https://github.com/$repo_path/pulls/$prid" ;;
260-
gitlab) echo "https://gitlab.com/$repo_path/-/merge_requests/$prid" ;;
261-
bitbucket) echo "https://bitbucket.org/$repo_path/pull-requests/$prid" ;;
268+
github) echo "https://github.com/$repo_path/pull/$prid" ;;
269+
gitlab) echo "https://gitlab.com/$repo_path/-/merge_requests/$prid" ;;
270+
bitbucket) echo "https://bitbucket.org/$repo_path/pull-requests/$prid" ;;
262271
esac
263272

264273
return 0
@@ -284,9 +293,9 @@ git_open_pipelines() {
284293

285294
repo_path=$(git_get_repo_path $remote)
286295
case "$remote_type" in
287-
github) open_url "$silent" "https://github.com/$repo_path/actions" ;;
288-
gitlab) open_url "$silent" "https://gitlab.com/$repo_path/pipelines?scope=all" ;;
289-
bitbucket) open_url "$silent" "https://bitbucket.org/$repo_path/addon/pipelines/home" ;;
296+
github) open_url "$silent" "https://github.com/$repo_path/actions" ;;
297+
gitlab) open_url "$silent" "https://gitlab.com/$repo_path/pipelines?scope=all" ;;
298+
bitbucket) open_url "$silent" "https://bitbucket.org/$repo_path/addon/pipelines/home" ;;
290299
esac
291300

292301
return 0
@@ -316,53 +325,66 @@ git_open() {
316325
fi
317326

318327
case $1 in
319-
project|repo|repository|\.) git_open_project ;;
320-
branch) git_open_branch $@ ;;
321-
file) git_open_file $@ ;;
322-
commit) git_open_commit $@ ;;
323-
prs|mrs) shift; git_open_pr_list ;;
324-
pr|mr) shift; git_open_new_pr $@ ;;
325-
actions|pipelines|ci) shift; git_open_pipelines ;;
326-
--version|-V)
327-
u="$(tput smul)"
328-
r="$(tput sgr0)"
329-
echo "git-open v$(cat "${0:A:h}/version.txt")"
330-
echo "${u}https://github.com/chenasraf/git-open${r}"
331-
echo "Copyright \xC2\xA9 2024 Chen Asraf" ;;
332-
_debug)
333-
inf="Getting info"
334-
y=$(tput setaf 3)
335-
g=$(tput setaf 2)
336-
r=$(tput sgr0)
337-
338-
echo -n "$r- ${y}$inf\r"
339-
remote=$(git_get_remote)
340-
echo -n "$r\\ ${y}$inf.\r"
341-
info=$(git remote show $remote)
342-
echo -n "$r| ${y}$inf..\r"
343-
branch=$(git branch --show-current)
344-
echo -n "$r/ ${y}$inf...\r"
345-
commit=$(git rev-parse HEAD)
346-
echo "${g}Done\e[0K$r\n"
347-
echo "Remote: $remote"
348-
echo "Repo Path: $(git_get_repo_path $remote)"
349-
echo "Remote Type: $(git_get_remote_type $remote)"
350-
echo "Current Branch: $branch"
351-
echo "Default Branch: $(echo $info | grep "HEAD branch" | awk '{print $3}')"
352-
echo "Current Ref: $commit"
353-
;;
354-
*)
355-
echo "Unknown command: $1"
356-
return 1
357-
;;
328+
project | repo | repository | \.) git_open_project ;;
329+
branch) git_open_branch $@ ;;
330+
file) git_open_file $@ ;;
331+
commit) git_open_commit $@ ;;
332+
prs | mrs)
333+
shift
334+
git_open_pr_list
335+
;;
336+
pr | mr)
337+
shift
338+
git_open_new_pr $@
339+
;;
340+
actions | pipelines | ci)
341+
shift
342+
git_open_pipelines
343+
;;
344+
--version | -V)
345+
u="$(tput smul)"
346+
r="$(tput sgr0)"
347+
echo "git-open v$(cat "${0:A:h}/version.txt")"
348+
echo "${u}https://github.com/chenasraf/git-open${r}"
349+
echo "Copyright \xC2\xA9 2024 Chen Asraf"
350+
;;
351+
_debug)
352+
inf="Getting info"
353+
y=$(tput setaf 3)
354+
g=$(tput setaf 2)
355+
r=$(tput sgr0)
356+
357+
echo -n "$r- ${y}$inf\r"
358+
remote=$(git_get_remote)
359+
echo -n "$r\\ ${y}$inf.\r"
360+
info=$(git remote show $remote)
361+
echo -n "$r| ${y}$inf..\r"
362+
branch=$(git branch --show-current)
363+
echo -n "$r/ ${y}$inf...\r"
364+
commit=$(git rev-parse HEAD)
365+
echo "${g}Done\e[0K$r\n"
366+
echo "Remote: $remote"
367+
echo "Repo Path: $(git_get_repo_path $remote)"
368+
echo "Remote Type: $(git_get_remote_type $remote)"
369+
echo "Current Branch: $branch"
370+
echo "Default Branch: $(echo $info | grep "HEAD branch" | awk '{print $3}')"
371+
echo "Current Ref: $commit"
372+
;;
373+
*)
374+
echo "Unknown command: $1"
375+
return 1
376+
;;
358377
esac
359378
}
360379

361380
while true; do
362381
case "$1" in
363-
open) shift ;;
364-
-s|--silent) shift; silent="-s" ;;
365-
*) break ;;
382+
open) shift ;;
383+
-s | --silent)
384+
shift
385+
silent="-s"
386+
;;
387+
*) break ;;
366388
esac
367389
done
368390

0 commit comments

Comments
 (0)