Skip to content

Commit 24dd2fc

Browse files
committed
Added support for Bitbucket Server with different root context
Please see the unit tests for more details. Basically, take the context parts before 'scm' and add them to the new URL before the other parts. Instead of taking fixed indexes for the path elements, base everything off the index of the found 'scm' path element.
1 parent 8caa9ad commit 24dd2fc

File tree

2 files changed

+42
-4
lines changed

2 files changed

+42
-4
lines changed

git-open

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -126,10 +126,19 @@ fi
126126
if [[ "$domain" == 'bitbucket.org' ]]; then
127127
# Bitbucket, see https://github.com/paulirish/git-open/issues/80 for why ?at is needed.
128128
providerBranchRef="/src?at=$branch"
129-
elif [[ ${pathargs[0]} == 'scm' ]]; then
130-
# Bitbucket server, which starts with 'scm'
131-
# Replace the first element, 'scm', with 'projects'. Keep the first argument, the string 'repos', and finally the rest of the arguments.
132-
pathargs=('projects' ${pathargs[1]} 'repos' "${pathargs[@]:2}")
129+
elif [[ "${#pathargs[@]}" -ge 3 && ${pathargs[${#pathargs[@]} - 3]} == 'scm' ]]; then
130+
# Bitbucket server always has /scm/ as the third to last segment in the url path, e.g. /scm/ppp/test-repo.git
131+
# Anything before the 'scm' is part of the server's root context
132+
133+
# Check whether there are other context parts before the 'scm' part
134+
pathPref=()
135+
if [[ "${#pathargs[@]}" -gt 3 ]]; then
136+
# If there are other context parts, add them, up to (but not including) the found 'scm'
137+
pathPref=("${pathargs[*]:0:${#pathargs[@]} - 3}")
138+
fi
139+
140+
# Replace the 'scm' element, with 'projects'. Keep the first argument, the string 'repos', and finally the rest of the arguments.
141+
pathargs=(${pathPref[@]} 'projects' ${pathargs[${#pathargs[@]} - 2]} 'repos' "${pathargs[@]:${#pathargs[@]} - 1}")
133142
IFS='/' urlpath="${pathargs[*]}"
134143
providerBranchRef="/browse?at=$branch"
135144
elif [[ "${#pathargs[@]}" -ge '2' && ${pathargs[${#pathargs[@]} - 2]} == '_git' ]]; then

test/git-open.bats

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -269,6 +269,35 @@ setup() {
269269

270270
}
271271

272+
273+
@test "bitbucket server with different root context" {
274+
# https://github.com/paulirish/git-open/pull/15
275+
git remote set-url origin "https://[email protected]/git/scm/ppp/test-repo.git"
276+
run ../git-open
277+
assert_output "https://bitbucket.example.com/git/projects/ppp/repos/test-repo"
278+
}
279+
280+
281+
@test "bitbucket server with different root context with multiple parts" {
282+
# https://github.com/paulirish/git-open/pull/15
283+
git remote set-url origin "https://[email protected]/really/long/root/context/scm/ppp/test-repo.git"
284+
run ../git-open
285+
assert_output "https://bitbucket.example.com/really/long/root/context/projects/ppp/repos/test-repo"
286+
}
287+
288+
289+
@test "bitbucket: Bitbucket Server private user repos with different root context" {
290+
# https://github.com/paulirish/git-open/pull/83#issuecomment-309968538
291+
git remote set-url origin "https://mybb.domain.com/root/context/scm/~first.last/rrr.git"
292+
git checkout -B "develop"
293+
run ../git-open
294+
assert_output "https://mybb.domain.com/root/context/projects/~first.last/repos/rrr/browse?at=develop" ||
295+
assert_output "https://mybb.domain.com/root/context/projects/~first.last/repos/rrr/browse?at=refs%2Fheads%2Fdevelop" ||
296+
assert_output "https://mybb.domain.com/root/context/projects/~first.last/repos/rrr/browse?at=refs/heads/develop"
297+
298+
}
299+
300+
272301
##
273302
## GitLab
274303
##

0 commit comments

Comments
 (0)