Skip to content

Commit e68eee6

Browse files
derimagiapaulirish
authored andcommitted
Strip ports based on protocol properly. Adding config for custom domains and protocols. (paulirish#92)
1 parent c265407 commit e68eee6

File tree

2 files changed

+50
-40
lines changed

2 files changed

+50
-40
lines changed

git-open

Lines changed: 26 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,9 @@ if ! git rev-parse --is-inside-work-tree &>/dev/null; then
1313
exit 1
1414
fi
1515

16+
# Defaults
1617
is_issue=0
18+
protocol="https"
1719

1820
# If the first argument is 'issue', we want to load the issue page
1921
if [[ "$1" == 'issue' ]]; then
@@ -34,18 +36,35 @@ if [[ -z "$giturl" ]]; then
3436
exit 1
3537
fi
3638

37-
# Initial case examples: 'git@example.com:user/project', 'https://example.com:8080/scm/user/project.git/'
39+
# From git-fetch(5), native protocols:
40+
# ssh://[user@]host.xz[:port]/path/to/repo.git/
41+
# git://host.xz[:port]/path/to/repo.git/
42+
# http[s]://host.xz[:port]/path/to/repo.git/
43+
# ftp[s]://host.xz[:port]/path/to/repo.git/
44+
# [user@]host.xz:path/to/repo.git/ - scp-like but is an alternative to ssh.
45+
3846
# Trim "/" and ".git" from the end of the url
3947
giturl=${giturl%/} giturl=${giturl%.git}
4048

4149
# Trim before last '@' and protocol (*://) from beginning
4250
uri=${giturl##*@} uri=${uri##*://}
4351

44-
# Trims before first ':' or '/' to get path
45-
urlpath=${uri#*[/:]}
52+
# If there isn't a protocol, we can assume it's using the scp syntax which uses ':' to seperate the path.
53+
[[ $giturl =~ :// ]] && pathsep='/' || pathsep=':'
54+
55+
# Seperate the domain and the urlpath on the first {pathsep}. This also removes the gitport from the domain.
56+
domain=${uri%%[:$pathsep]*} urlpath=${uri#*$pathsep}
57+
58+
# Allow config options to replace the server or the protocol
59+
openurl="$protocol://$domain"
60+
61+
function getConfig() {
62+
config=$(git config --get-urlmatch "open.$1" "$openurl")
63+
echo "${config:-${!1}}"
64+
}
4665

47-
# Trims after first ':' or '/' to remove path
48-
server=${uri%%[/:]*}
66+
domain=$(getConfig "domain")
67+
protocol=$(getConfig "protocol")
4968

5069
# Get current branch
5170
branch=${2:-$(git symbolic-ref -q --short HEAD)}
@@ -63,7 +82,7 @@ else
6382
providerBranchRef="tree/$branch"
6483
fi
6584

66-
if [[ "$server" == 'bitbucket.org' ]]; then
85+
if [[ "$domain" == 'bitbucket.org' ]]; then
6786
# Bitbucket, see https://github.com/paulirish/git-open/issues/80 for why ?at is needed.
6887
providerBranchRef="src?at=$branch"
6988
elif [[ ${pathargs[0]} == 'scm' ]]; then
@@ -74,8 +93,7 @@ elif [[ ${pathargs[0]} == 'scm' ]]; then
7493
providerBranchRef="browse?at=$branch"
7594
fi
7695

77-
# @TODO: support non-https?
78-
openurl="https://$server/$urlpath"
96+
openurl="$protocol://$domain/$urlpath"
7997

8098
# simplify URL for master
8199
if [[ $branch != "master" ]]; then

test/git-open.bats

100644100755
Lines changed: 24 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -177,18 +177,6 @@ setup() {
177177
## GitLab
178178
##
179179

180-
@test "gitlab: separate domains" {
181-
skip
182-
# skipping until test is fixed: see #87
183-
184-
# https://github.com/paulirish/git-open/pull/56
185-
git remote set-url origin "git@git.example.com:namespace/project.git"
186-
git config "gitopen.gitlab.domain" "gitlab.example.com"
187-
git config "gitopen.gitlab.ssh.domain" "git.example.com"
188-
run ../git-open
189-
assert_output "https://gitlab.example.com/namespace/project"
190-
}
191-
192180
@test "gitlab: default ssh origin style" {
193181
# https://github.com/paulirish/git-open/pull/55
194182
git remote set-url origin "git@gitlab.example.com:user/repo"
@@ -206,32 +194,36 @@ setup() {
206194
refute_output --partial "//user"
207195
}
208196

209-
@test "gitlab: ssh://git@host:port origin" {
210-
skip
211-
# skipping until test is fixed: see #87
212-
213-
# https://github.com/paulirish/git-open/pull/76
214-
# this first set mostly matches the "gitlab: ssh://git@ origin" test
215-
git remote set-url origin "ssh://git@repo.intranet/XXX/YYY.git"
216-
git config "gitopen.gitlab.domain" "repo.intranet"
197+
@test "gitlab: separate domains" {
198+
# https://github.com/paulirish/git-open/pull/56
199+
git remote set-url origin "git@git.example.com:namespace/project.git"
200+
git config --local --add "open.https://git.example.com.domain" "gitlab.example.com"
217201
run ../git-open
218-
assert_output "https://repo.intranet/XXX/YYY"
219-
refute_output --partial "ssh://"
220-
refute_output --partial "//XXX"
202+
assert_output "https://gitlab.example.com/namespace/project"
203+
}
204+
205+
@test "gitlab: special domain and path" {
206+
git remote set-url origin "ssh://git@git.example.com:7000/XXX/YYY.git"
207+
git config --local --add "open.https://git.example.com.domain" "repo.intranet/subpath"
208+
git config --local --add "open.https://git.example.com.protocol" "http"
221209

222-
git remote set-url origin "ssh://git@repo.intranet:7000/XXX/YYY.git"
223-
git config "gitopen.gitlab.domain" "repo.intranet"
224-
git config "gitopen.gitlab.ssh.port" "7000"
225210
run ../git-open
226-
assert_output "https://repo.intranet/XXX/YYY"
227-
refute_output --partial "ssh://"
228-
refute_output --partial "//XXX"
211+
assert_output "http://repo.intranet/subpath/XXX/YYY"
212+
refute_output --partial "https://"
229213
}
230214

231-
# Tests not yet written:
232-
# * gitopen.gitlab.port
233-
# * gitopen.gitlab.protocol
215+
@test "gitlab: different port" {
216+
# https://github.com/paulirish/git-open/pull/76
217+
git remote set-url origin "ssh://git@git.example.com:7000/XXX/YYY.git"
218+
run ../git-open
219+
assert_output "https://git.example.com/XXX/YYY"
220+
refute_output --partial ":7000"
234221

222+
git remote set-url origin "https://git.example.com:7000/XXX/YYY.git"
223+
run ../git-open
224+
assert_output "https://git.example.com/XXX/YYY"
225+
refute_output --partial ":7000"
226+
}
235227

236228
teardown() {
237229
cd ..

0 commit comments

Comments
 (0)