@@ -42,8 +42,10 @@ if ! git rev-parse --is-inside-work-tree &>/dev/null; then
42
42
fi
43
43
44
44
# 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" )
47
49
default_remote=$( git config open.default.remote)
48
50
remote=${1:- $default_remote }
49
51
remote=${remote:- $tracked_remote }
@@ -153,25 +155,25 @@ function getConfig() {
153
155
domain=$( getConfig " domain" )
154
156
protocol=$( getConfig " protocol" )
155
157
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)} }
158
160
159
161
# Split arguments on '/'
160
162
IFS=' /' read -r -a pathargs <<< " $urlpath"
161
163
162
164
if (( is_issue )) ; then
163
165
# For issues, take the numbers and preprend 'issues/'
164
- providerBranchRef=" /issues/${branch // [^0-9]/ } "
166
+ providerBranchRef=" /issues/${remote_ref // [^0-9]/ } "
165
167
else
166
168
# Make # and % characters url friendly
167
169
# 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 "
170
172
fi
171
173
172
174
if [[ " $domain " == ' bitbucket.org' ]]; then
173
175
# 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 "
175
177
elif [[ " ${# pathargs[@]} " -ge 3 && ${pathargs[${#pathargs[@]} - 3]} == ' scm' ]]; then
176
178
# Bitbucket server always has /scm/ as the third to last segment in the url path, e.g. /scm/ppp/test-repo.git
177
179
# Anything before the 'scm' is part of the server's root context
@@ -183,18 +185,18 @@ elif [[ "${#pathargs[@]}" -ge 3 && ${pathargs[${#pathargs[@]} - 3]} == 'scm' ]];
183
185
# shellcheck disable=SC2206
184
186
pathargs=(${pathPref[@]} ' projects' ${pathargs[${#pathargs[@]} - 2]} ' repos' " ${pathargs[@]: ${# pathargs[@]} - 1} " )
185
187
IFS=' /' urlpath=" ${pathargs[*]} "
186
- providerBranchRef=" /browse?at=$branch "
188
+ providerBranchRef=" /browse?at=$remote_ref "
187
189
elif [[ " ${# pathargs[@]} " -ge ' 2' && ${pathargs[${#pathargs[@]} - 2]} == ' _git' ]]; then
188
190
# Visual Studio Team Services and Team Foundation Server always have /_git/ as the second to last segment in the url path
189
191
if (( is_issue )) ; then
190
192
# Switch to workitems, provide work item id if specified
191
193
urlpath=" ${urlpath%%/ _git/* } /_workitems"
192
194
# Handle case for the default repository url
193
195
urlpath=" ${urlpath# _git\/ * } "
194
- providerBranchRef=" ?id=${branch // [^0-9]/ } "
196
+ providerBranchRef=" ?id=${remote_ref // [^0-9]/ } "
195
197
else
196
198
# Keep project and repository name, append branch selector.
197
- providerBranchRef=" ?version=GB$branch "
199
+ providerBranchRef=" ?version=GB$remote_ref "
198
200
fi
199
201
elif [[ " $domain " =~ amazonaws\. com$ ]]; then
200
202
# AWS Code Commit
223
225
openurl=" $protocol ://$domain /$urlpath "
224
226
225
227
# simplify URL for master
226
- if [[ $branch != " master" ]]; then
228
+ if [[ $remote_ref != " master" ]]; then
227
229
openurl=" $openurl$providerBranchRef "
228
230
fi
229
231
0 commit comments