@@ -42,8 +42,10 @@ if ! git rev-parse --is-inside-work-tree &>/dev/null; then
4242fi
4343
4444# choose remote. priority to: provided argument, default in config, detected tracked remote, 'origin'
45- branch_name=$( git name-rev --name-only HEAD 2> /dev/null)
46- tracked_remote=$( git config " branch.$branch_name .remote" )
45+ branch=${2:- $(git symbolic-ref -q --short HEAD)}
46+ upstream_branch_full_name=$( git config " branch.$branch .merge" )
47+ upstream_branch=${upstream_branch_full_name# " refs/heads/" }
48+ tracked_remote=$( git config " branch.$branch .remote" )
4749default_remote=$( git config open.default.remote)
4850remote=${1:- $default_remote }
4951remote=${remote:- $tracked_remote }
@@ -153,25 +155,25 @@ function getConfig() {
153155domain=$( getConfig " domain" )
154156protocol=$( getConfig " protocol" )
155157
156- # Get current branch / tag / commit
157- branch =${2 :- $(git symbolic-ref -q --short HEAD || git describe --tags --exact-match 2>/ dev/ null || git rev-parse HEAD)}
158+ # Remote ref to open
159+ remote_ref =${upstream_branch :- ${branch :- $ (git describe --tags --exact-match 2>/ dev/ null || git rev-parse HEAD)} }
158160
159161# Split arguments on '/'
160162IFS=' /' read -r -a pathargs <<< " $urlpath"
161163
162164if (( is_issue )) ; then
163165 # For issues, take the numbers and preprend 'issues/'
164- providerBranchRef=" /issues/${branch // [^0-9]/ } "
166+ providerBranchRef=" /issues/${remote_ref // [^0-9]/ } "
165167else
166168 # Make # and % characters url friendly
167169 # github.com/paulirish/git-open/pull/24
168- branch =${branch //%/% 25} branch =${branch //#/% 23}
169- providerBranchRef=" /tree/$branch "
170+ remote_ref =${remote_ref //%/% 25} remote_ref =${remote_ref //#/% 23}
171+ providerBranchRef=" /tree/$remote_ref "
170172fi
171173
172174if [[ " $domain " == ' bitbucket.org' ]]; then
173175 # Bitbucket, see https://github.com/paulirish/git-open/issues/80 for why ?at is needed.
174- providerBranchRef=" /src?at=$branch "
176+ providerBranchRef=" /src?at=$remote_ref "
175177elif [[ " ${# pathargs[@]} " -ge 3 && ${pathargs[${#pathargs[@]} - 3]} == ' scm' ]]; then
176178 # Bitbucket server always has /scm/ as the third to last segment in the url path, e.g. /scm/ppp/test-repo.git
177179 # Anything before the 'scm' is part of the server's root context
@@ -183,18 +185,18 @@ elif [[ "${#pathargs[@]}" -ge 3 && ${pathargs[${#pathargs[@]} - 3]} == 'scm' ]];
183185 # shellcheck disable=SC2206
184186 pathargs=(${pathPref[@]} ' projects' ${pathargs[${#pathargs[@]} - 2]} ' repos' " ${pathargs[@]: ${# pathargs[@]} - 1} " )
185187 IFS=' /' urlpath=" ${pathargs[*]} "
186- providerBranchRef=" /browse?at=$branch "
188+ providerBranchRef=" /browse?at=$remote_ref "
187189elif [[ " ${# pathargs[@]} " -ge ' 2' && ${pathargs[${#pathargs[@]} - 2]} == ' _git' ]]; then
188190 # Visual Studio Team Services and Team Foundation Server always have /_git/ as the second to last segment in the url path
189191 if (( is_issue )) ; then
190192 # Switch to workitems, provide work item id if specified
191193 urlpath=" ${urlpath%%/ _git/* } /_workitems"
192194 # Handle case for the default repository url
193195 urlpath=" ${urlpath# _git\/ * } "
194- providerBranchRef=" ?id=${branch // [^0-9]/ } "
196+ providerBranchRef=" ?id=${remote_ref // [^0-9]/ } "
195197 else
196198 # Keep project and repository name, append branch selector.
197- providerBranchRef=" ?version=GB$branch "
199+ providerBranchRef=" ?version=GB$remote_ref "
198200 fi
199201elif [[ " $domain " =~ amazonaws\. com$ ]]; then
200202 # AWS Code Commit
223225openurl=" $protocol ://$domain /$urlpath "
224226
225227# simplify URL for master
226- if [[ $branch != " master" ]]; then
228+ if [[ $remote_ref != " master" ]]; then
227229 openurl=" $openurl$providerBranchRef "
228230fi
229231
0 commit comments