diff --git a/git-open b/git-open index d952c3a..935fba2 100755 --- a/git-open +++ b/git-open @@ -126,10 +126,19 @@ fi 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 - # Bitbucket server, which starts with 'scm' - # Replace the first element, 'scm', with 'projects'. Keep the first argument, the string 'repos', and finally the rest of the arguments. - pathargs=('projects' ${pathargs[1]} 'repos' "${pathargs[@]:2}") +elif [[ "${#pathargs[@]}" -ge 3 && ${pathargs[${#pathargs[@]} - 3]} == 'scm' ]]; then + # Bitbucket server always has /scm/ as the third to last segment in the url path, e.g. /scm/ppp/test-repo.git + # Anything before the 'scm' is part of the server's root context + + # Check whether there are other context parts before the 'scm' part + pathPref=() + if [[ "${#pathargs[@]}" -gt 3 ]]; then + # If there are other context parts, add them, up to (but not including) the found 'scm' + pathPref=("${pathargs[*]:0:${#pathargs[@]} - 3}") + fi + + # Replace the 'scm' element, with 'projects'. Keep the first argument, the string 'repos', and finally the rest of the arguments. + pathargs=(${pathPref[@]} 'projects' ${pathargs[${#pathargs[@]} - 2]} 'repos' "${pathargs[@]:${#pathargs[@]} - 1}") IFS='/' urlpath="${pathargs[*]}" providerBranchRef="/browse?at=$branch" elif [[ "${#pathargs[@]}" -ge '2' && ${pathargs[${#pathargs[@]} - 2]} == '_git' ]]; then diff --git a/test/git-open.bats b/test/git-open.bats index 3f90fb4..0f750d2 100755 --- a/test/git-open.bats +++ b/test/git-open.bats @@ -269,6 +269,35 @@ setup() { } + +@test "bitbucket server with different root context" { + # https://github.com/paulirish/git-open/pull/15 + git remote set-url origin "https://user@bitbucket.example.com/git/scm/ppp/test-repo.git" + run ../git-open + assert_output "https://bitbucket.example.com/git/projects/ppp/repos/test-repo" +} + + +@test "bitbucket server with different root context with multiple parts" { + # https://github.com/paulirish/git-open/pull/15 + git remote set-url origin "https://user@bitbucket.example.com/really/long/root/context/scm/ppp/test-repo.git" + run ../git-open + assert_output "https://bitbucket.example.com/really/long/root/context/projects/ppp/repos/test-repo" +} + + +@test "bitbucket: Bitbucket Server private user repos with different root context" { + # https://github.com/paulirish/git-open/pull/83#issuecomment-309968538 + git remote set-url origin "https://mybb.domain.com/root/context/scm/~first.last/rrr.git" + git checkout -B "develop" + run ../git-open + assert_output "https://mybb.domain.com/root/context/projects/~first.last/repos/rrr/browse?at=develop" || + assert_output "https://mybb.domain.com/root/context/projects/~first.last/repos/rrr/browse?at=refs%2Fheads%2Fdevelop" || + assert_output "https://mybb.domain.com/root/context/projects/~first.last/repos/rrr/browse?at=refs/heads/develop" + +} + + ## ## GitLab ##