forked from paulirish/git-open
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgit-open
More file actions
executable file
·212 lines (155 loc) · 5.09 KB
/
git-open
File metadata and controls
executable file
·212 lines (155 loc) · 5.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
#!/usr/bin/env bash
# Opens the GitHub page for a repo/branch in your browser.
# https://github.com/paulirish/git-open/
#
# git open
# git open [remote] [branch]
# are we in a git repo?
git rev-parse --is-inside-work-tree &>/dev/null
if [[ $? != 0 ]]; then
echo "Not a git repository." 1>&2
exit 1
fi
# assume matt if not provided
remote="matt"
# fallback to upstream if neither is present.
if [ -n "$1" ]; then
dummy=5
else
remote=$(git remote) #TODO fix by making it only output upstream repo
fi
if [ -n "$1" ]; then
if [ "$1" == "issue" ]; then
currentBranch=$(git symbolic-ref -q --short HEAD)
regex='^issue'
if [[ $currentBranch =~ $regex ]]; then
issue=${currentBranch#*#}
else
echo "'git open issue' expect branch naming to be issues/#123" 1>&2
exit 1
fi
else
remote="$1"
fi
fi
remote_url="remote.${remote}.url"
giturl=$(git config --get "$remote_url")
if [ -z "$giturl" ]; then
echo "$remote_url not set." 1>&2
exit 1
fi
# get current branch
if [ -z "$2" ]; then
branch=$(git symbolic-ref -q --short HEAD)
else
branch="$2"
fi
# Make # and % characters url friendly
# github.com/paulirish/git-open/pull/24
branch=${branch//%/%25} && branch=${branch//#/%23}
# URL normalization
# GitHub gists
if grep -q gist.github <<<"$giturl"; then
giturl=${giturl/git\@gist.github\.com\:/https://gist.github.com/}
providerUrlDifference=tree
# GitHub
elif grep -q github <<<"$giturl"; then
giturl=${giturl/git\@github\.com\:/https://github.com/}
# handle urls with the git user
giturl=${giturl/#github\.com\:/https://github.com/}
# handle SSH protocol (links like ssh://[email protected]/user/repo)
giturl=${giturl/#ssh\:\/\/git\@github\.com\//https://github.com/}
providerUrlDifference=tree
# Bitbucket
elif grep -q bitbucket <<<"$giturl"; then
giturl=${giturl/git\@bitbucket\.org\:/https://bitbucket.org/}
# handle SSH protocol (change ssh://https://bitbucket.org/user/repo to https://bitbucket.org/user/repo)
giturl=${giturl/#ssh\:\/\/git\@/https://}
# remove username
giturl=${giturl//\/\/*@bitbucket.org///bitbucket.org}
rev="$(git rev-parse HEAD)"
git_pwd="$(git rev-parse --show-prefix)"
providerUrlDifference="src/${rev}/${git_pwd}"
branch="?at=${branch}"
# Atlassian Bitbucket Server
elif grep -q "/scm/" <<<"$giturl"; then
re='(.*)/scm/(.*)/(.*)\.git'
if [[ $giturl =~ $re ]]; then
giturl=${BASH_REMATCH[1]}/projects/${BASH_REMATCH[2]}/repos/${BASH_REMATCH[3]}
providerUrlDifference=browse
branch="?at=refs%2Fheads%2F${branch}"
fi
# GitLab
else
# custom GitLab
gitlab_domain=$(git config --get gitopen.gitlab.domain)
gitlab_ssh_domain=$(git config --get gitopen.gitlab.ssh.domain)
gitlab_ssh_domain=${gitlab_ssh_domain:-$gitlab_domain}
gitlab_ssh_port=$(git config --get gitopen.gitlab.ssh.port)
gitlab_protocol=$(git config --get gitopen.gitlab.protocol)
if [ -z "$gitlab_protocol" ]; then
gitlab_protocol=https
fi
if [ -n "$gitlab_domain" ]; then
if egrep -q "${gitlab_domain}|${gitlab_ssh_domain}" <<<"$giturl"; then
# Handle GitLab's default SSH notation (like [email protected]:user/repo)
giturl=${giturl/git\@${gitlab_ssh_domain}\:/${gitlab_protocol}://${gitlab_domain}/}
# handle SSH protocol (links like ssh://[email protected]/user/repo)
giturl=${giturl/#ssh\:\/\//${gitlab_protocol}://}
# remove git@ from the domain
giturl=${giturl/git\@${gitlab_ssh_domain}/${gitlab_domain}/}
# remove SSH port
if [ -n "$gitlab_ssh_port" ]; then
giturl=${giturl/\/:${gitlab_ssh_port}\///}
fi
providerUrlDifference=tree
fi
# hosted GitLab
elif grep -q gitlab <<<"$giturl"; then
giturl=${giturl/git\@gitlab\.com\:/https://gitlab.com/}
providerUrlDifference=tree
fi
fi
giturl=${giturl%\.git}
if [ -n "$issue" ]; then
giturl="${giturl}/issues/${issue}"
elif [ -n "$branch" ]; then
giturl="${giturl}/${providerUrlDifference}/${branch}"
fi
# simplify URL for master
giturl=${giturl/tree\/master/}
# get current open browser command
case $( uname -s ) in
Darwin) open=open;;
MINGW*) open=start;;
CYGWIN*) open=cygstart;;
MSYS*) open="powershell.exe –NoProfile Start";;
*) open=${BROWSER:-xdg-open};;
esac
# open it in a browser
# $open "$giturl" &> /dev/null
# refresh page instead of opening new tab if open
osascript -e 'tell application "Google Chrome"
set i to 0
repeat with theTab in (every tab of every window)
set i to i + 1
set theURL to URL of theTab
if theURL is '"\"$giturl\""' then
set (active tab index of (window 1)) to i
activate
tell application "System Events" to keystroke "r" using {command down, shift down}
return -- "refreshed?"
else
-- log "nope: " & theURL & "is not " & ('"\"$giturl\""')
end if
end repeat
open location '"\"$giturl\""'
set theURL to '"\"$giturl\""'
return -- "opened?"
-- if theURL is in (every tab of every window) then
-- return true
-- else
-- return "false: (" & theURL & "): " & (every tab of every window)
-- end if
end tell'
exit $?