Skip to content

Commit

Permalink
Strip ports based on protocol properly. Adding config for custom doma…
Browse files Browse the repository at this point in the history
…ins and protocols. (#92)
  • Loading branch information
derimagia authored and paulirish committed Oct 26, 2017
1 parent c265407 commit e68eee6
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 40 deletions.
34 changes: 26 additions & 8 deletions git-open
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@ if ! git rev-parse --is-inside-work-tree &>/dev/null; then
exit 1
fi

# Defaults
is_issue=0
protocol="https"

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

# Initial case examples: '[email protected]:user/project', 'https://example.com:8080/scm/user/project.git/'
# From git-fetch(5), native protocols:
# ssh:https://[user@]host.xz[:port]/path/to/repo.git/
# git:https://host.xz[:port]/path/to/repo.git/
# http[s]:https://host.xz[:port]/path/to/repo.git/
# ftp[s]:https://host.xz[:port]/path/to/repo.git/
# [user@]host.xz:path/to/repo.git/ - scp-like but is an alternative to ssh.

# Trim "/" and ".git" from the end of the url
giturl=${giturl%/} giturl=${giturl%.git}

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

# Trims before first ':' or '/' to get path
urlpath=${uri#*[/:]}
# If there isn't a protocol, we can assume it's using the scp syntax which uses ':' to seperate the path.
[[ $giturl =~ :https:// ]] && pathsep='/' || pathsep=':'

# Seperate the domain and the urlpath on the first {pathsep}. This also removes the gitport from the domain.
domain=${uri%%[:$pathsep]*} urlpath=${uri#*$pathsep}

# Allow config options to replace the server or the protocol
openurl="$protocol:https://$domain"

function getConfig() {
config=$(git config --get-urlmatch "open.$1" "$openurl")
echo "${config:-${!1}}"
}

# Trims after first ':' or '/' to remove path
server=${uri%%[/:]*}
domain=$(getConfig "domain")
protocol=$(getConfig "protocol")

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

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

# @TODO: support non-https?
openurl="https://$server/$urlpath"
openurl="$protocol:https://$domain/$urlpath"

# simplify URL for master
if [[ $branch != "master" ]]; then
Expand Down
56 changes: 24 additions & 32 deletions test/git-open.bats
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -177,18 +177,6 @@ setup() {
## GitLab
##

@test "gitlab: separate domains" {
skip
# skipping until test is fixed: see #87

# https://github.com/paulirish/git-open/pull/56
git remote set-url origin "[email protected]:namespace/project.git"
git config "gitopen.gitlab.domain" "gitlab.example.com"
git config "gitopen.gitlab.ssh.domain" "git.example.com"
run ../git-open
assert_output "https://gitlab.example.com/namespace/project"
}

@test "gitlab: default ssh origin style" {
# https://github.com/paulirish/git-open/pull/55
git remote set-url origin "[email protected]:user/repo"
Expand All @@ -206,32 +194,36 @@ setup() {
refute_output --partial "//user"
}

@test "gitlab: ssh:https://git@host:port origin" {
skip
# skipping until test is fixed: see #87

# https://github.com/paulirish/git-open/pull/76
# this first set mostly matches the "gitlab: ssh:https://git@ origin" test
git remote set-url origin "ssh:https://[email protected]/XXX/YYY.git"
git config "gitopen.gitlab.domain" "repo.intranet"
@test "gitlab: separate domains" {
# https://github.com/paulirish/git-open/pull/56
git remote set-url origin "[email protected]:namespace/project.git"
git config --local --add "open.https://git.example.com.domain" "gitlab.example.com"
run ../git-open
assert_output "https://repo.intranet/XXX/YYY"
refute_output --partial "ssh:https://"
refute_output --partial "//XXX"
assert_output "https://gitlab.example.com/namespace/project"
}

@test "gitlab: special domain and path" {
git remote set-url origin "ssh:https://[email protected]:7000/XXX/YYY.git"
git config --local --add "open.https://git.example.com.domain" "repo.intranet/subpath"
git config --local --add "open.https://git.example.com.protocol" "http"

git remote set-url origin "ssh:https://[email protected]:7000/XXX/YYY.git"
git config "gitopen.gitlab.domain" "repo.intranet"
git config "gitopen.gitlab.ssh.port" "7000"
run ../git-open
assert_output "https://repo.intranet/XXX/YYY"
refute_output --partial "ssh:https://"
refute_output --partial "//XXX"
assert_output "https://repo.intranet/subpath/XXX/YYY"
refute_output --partial "https://"
}

# Tests not yet written:
# * gitopen.gitlab.port
# * gitopen.gitlab.protocol
@test "gitlab: different port" {
# https://github.com/paulirish/git-open/pull/76
git remote set-url origin "ssh:https://[email protected]:7000/XXX/YYY.git"
run ../git-open
assert_output "https://git.example.com/XXX/YYY"
refute_output --partial ":7000"

git remote set-url origin "https://git.example.com:7000/XXX/YYY.git"
run ../git-open
assert_output "https://git.example.com/XXX/YYY"
refute_output --partial ":7000"
}

teardown() {
cd ..
Expand Down

0 comments on commit e68eee6

Please sign in to comment.