Skip to content

Commit 3d1ad21

Browse files
committed
default to upstream branch
1 parent 4eed00f commit 3d1ad21

File tree

2 files changed

+35
-12
lines changed

2 files changed

+35
-12
lines changed

git-open

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,10 @@ if ! git rev-parse --is-inside-work-tree &>/dev/null; then
4242
fi
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")
4749
default_remote=$(git config open.default.remote)
4850
remote=${1:-$default_remote}
4951
remote=${remote:-$tracked_remote}
@@ -153,25 +155,25 @@ function getConfig() {
153155
domain=$(getConfig "domain")
154156
protocol=$(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 '/'
160162
IFS='/' read -r -a pathargs <<<"$urlpath"
161163

162164
if (( 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]/}"
165167
else
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"
170172
fi
171173

172174
if [[ "$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"
175177
elif [[ "${#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"
187189
elif [[ "${#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
199201
elif [[ "$domain" =~ amazonaws\.com$ ]]; then
200202
# AWS Code Commit
@@ -223,7 +225,7 @@ fi
223225
openurl="$protocol://$domain/$urlpath"
224226

225227
# simplify URL for master
226-
if [[ $branch != "master" ]]; then
228+
if [[ $remote_ref != "master" ]]; then
227229
openurl="$openurl$providerBranchRef"
228230
fi
229231

test/git-open.bats

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,27 @@ setup() {
6565
assert_output "https://github.com/user/repo/tree/mybranch"
6666
}
6767

68+
@test "gh: upstream branch" {
69+
git remote set-url origin "[email protected]:user/repo.git"
70+
git remote add upstreamRemote "[email protected]:user/upstream-repo.git"
71+
git checkout -B "mybranch"
72+
git config --local "branch.mybranch.merge" "refs/heads/myupstream/mybranch"
73+
git config --local "branch.mybranch.remote" "upstreamRemote"
74+
run ../git-open
75+
assert_output "https://github.com/user/upstream-repo/tree/myupstream/mybranch"
76+
}
77+
78+
@test "gh: upstream branch from param" {
79+
git remote set-url origin "[email protected]:user/repo.git"
80+
git remote add upstreamRemote "[email protected]:user/upstream-repo.git"
81+
git checkout -B "mybranch"
82+
git config --local "branch.mybranch.merge" "refs/heads/upstreamBranch"
83+
git config --local "branch.mybranch.remote" "upstreamRemote"
84+
git checkout master
85+
run ../git-open upstreamRemote mybranch
86+
assert_output "https://github.com/user/upstream-repo/tree/upstreamBranch"
87+
}
88+
6889
@test "gh: tag" {
6990
git remote set-url origin "[email protected]:user/repo.git"
7091
git tag mytag

0 commit comments

Comments
 (0)