Skip to content

Adding config for custom domains/prefixes and properly strip custom ports. #92

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 26, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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: 'git@example.com:user/project', 'https://example.com:8080/scm/user/project.git/'
# From git-fetch(5), native protocols:
# ssh://[user@]host.xz[:port]/path/to/repo.git/
# git://host.xz[:port]/path/to/repo.git/
# http[s]://host.xz[:port]/path/to/repo.git/
# ftp[s]://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 (*://) from beginning
uri=${giturl##*@} uri=${uri##*://}

# 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 =~ :// ]] && 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://$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://$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 "git@git.example.com: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 "git@gitlab.example.com:user/repo"
Expand All @@ -206,32 +194,36 @@ setup() {
refute_output --partial "//user"
}

@test "gitlab: ssh://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://git@ origin" test
git remote set-url origin "ssh://git@repo.intranet/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 "git@git.example.com: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://"
refute_output --partial "//XXX"
assert_output "https://gitlab.example.com/namespace/project"
}

@test "gitlab: special domain and path" {
git remote set-url origin "ssh://git@git.example.com: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://git@repo.intranet: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://"
refute_output --partial "//XXX"
assert_output "http://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://git@git.example.com: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