-
Notifications
You must be signed in to change notification settings - Fork 18
/
git-it-on.plugin.zsh
294 lines (254 loc) · 6.92 KB
/
git-it-on.plugin.zsh
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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
#!/bin/zsh
IN_WSL=false
if [ -n "$WSL_DISTRO_NAME" ]; then
IN_WSL=true
elif [[ $(uname -a | grep -i 'Microsoft') ]]; then
IN_WSL=true
fi
__open() {
if [ "$(uname -s)" = "Darwin" ]; then
open "$1" 2> /dev/null
elif if $IN_WSL; then
# for cmd.exe, need to use ^ to escape the following: ()%!^"<>&|
cmd.exe /c "start $(echo "$1" | sed "s~\([\(\)%\!^\"<>&|]\)~\^\1~g")" &> /dev/null
else
xdg-open "$1" &> /dev/null
fi
}
__set_remote_branch() {
branch=$(git rev-parse --abbrev-ref --symbolic-full-name @{u} 2> /dev/null)
if [ $? -eq 0 ]; then
branch="${branch/origin\//}"
else
false
fi
}
__set_local_branch() {
branch=$(git rev-parse --abbrev-ref HEAD)
}
# check if local branch is tracked upstream;
# if not, set $branch to first remote branch (probably master from origin/master)
__fix_local_untracked_branch() {
for line in $(git rev-parse --abbrev-ref --remotes); do
local _line="$(echo $line | cut -f2- -d'/')"
if [[ "$branch" == "$_line" ]]; then return true; fi
done
# head -n1 gets the first branch name on the remote
# the cut command removes the leading remote name and slash from the full branch name
local _branch=$(git rev-parse --abbrev-ref --remotes | head -n1 | cut -f2- -d'/')
branch=$_branch
}
git_set_repo() {
repo_url=$(git config --get remote.origin.url)
if ! __set_remote_branch; then __set_local_branch; fi
__fix_local_untracked_branch
url="${repo_url/git/https}"
url="${url/httpshub/github}"
url="${url/httpslab/gitlab}"
url="${url/.git/}"
url="${url/https@/https://}"
url="${url/com:/com/}"
url="${url/net:/net/}"
url="${url/edu:/edu/}"
url="${url/org:/org/}"
url="${url/ssh:\/\/}"
}
git_open_file() {
file_path="$1"
git_set_repo
if [ ! -f $file_path ] && [ ! -d $file_path ]; then
echo "$file_path does not exist"; echo ""; git_help
return
fi
if [ "$#" -eq 2 ]; then branch="$2"; fi
if [ -d $1 ]; then
local cdtohere=$1
local zone='tree'
else
local cdtohere="."
local zone='blob'
fi
local file=$(echo "$(cd $cdtohere; pwd)" | cut -c "$((1+${#$(git rev-parse --show-toplevel)}))-")
url="$url/$zone/$branch$file"
if [ -d $1 ]; then
url=$url
else
url="$url/$1"
fi
__open $url
}
git_open_compare() {
git_set_repo
if [ "$#" -ne 0 ]; then
branch="$1"
fi
__open "$url/compare/$branch"
}
git_open_commits() {
git_set_repo
if [ "$#" -ne 0 ]; then
branch="$1"
fi
__open "$url/commits/$branch"
}
git_open_history() {
file_name="$1"
git_set_repo
if [ "$#" -eq 2 ]; then
branch="$2"
fi
__open "$url/commits/$branch/$file_name"
}
git_open_branch() {
branch_name="$1"
git_set_repo
if [ "$#" -eq 0 ]; then
git_open_file
else
__open "$url/tree/$branch_name"
fi
}
git_branches() {
git_set_repo
if [ "$#" -eq 0 ]; then
local loc="active"
elif [ "$1" = "mine" ]; then
local loc="yours"
else
local loc=$1 # active, stale, all
fi
__open "$url/branches/$loc"
}
git_open_pulls() {
git_set_repo
shift
if [ "$#" -eq 0 ]; then
__open "$url/pulls"
elif [ $1 -ge 0 2>/dev/null ]; then
__open "$url/pull/$1"
else
__open "$url/pulls?q=$@"
fi
}
git_open_issues() {
git_set_repo
shift
if [ "$#" -eq 0 ]; then
__open "$url/issues"
elif [ $1 -ge 0 2>/dev/null ]; then
__open "$url/issues/$1"
else
__open "$url/issues?q=$@"
fi
}
git_grep() {
git_set_repo
if [[ "${2}" == "${2% *}" ]]; then
shift
url="$url/search?q=$@"
else
url="$url/search?q=\"$2\""
fi
__open $url
}
git_ctrlp() {
git_set_repo
if [ "$#" -eq 0 ]; then
branch="master"
else
branch=$1
fi
__open "$url/find/$branch"
}
git_open_repo() {
if [ "$#" -eq 2 ]; then
username="$1"
reponame="$2"
__open "http://www.github.com/$username/$reponame"
else
git_open_file $1
fi
}
gitlab_open_compare() {
git_set_repo
shift
if [ "$#" -eq 0 ]; then
src="$(git rev-parse --abbrev-ref HEAD)"
target="master"
elif [ "$#" -eq 1 ]; then
src="$(git rev-parse --abbrev-ref HEAD)"
target="$1"
else
src="$2"
target="$1"
fi
__open "$url/compare/$target...$src"
}
gitlab_open_branches() {
git_set_repo
__open "$url/branches"
}
gitlab_open_network() {
git_set_repo
local branch="$(git rev-parse --abbrev-ref HEAD)"
__open "$url/network/$branch"
}
gitlab_ctrlp() {
git_set_repo
if [ "$#" -eq 0 ]; then
branch="master"
else
branch=$1
fi
__open "$url/find_file/$branch"
}
gitlab_open_merges() {
git_set_repo
shift
if [ "$#" -eq 0 ]; then
__open "$url/merge_requests"
elif [ $1 -ge 0 2>/dev/null ]; then
__open "$url/merge_requests/$1"
else
__open "$url/merge_requests?scope=all&utf8=✓&state=opened&search=$@"
fi
}
git_help() {
echo 'GIT IT ON'
echo '============='
echo '* `gitit` -- open your current folder, on your current branch, in GitHub or GitLab.'
echo '* `gitit <folder or file>` -- open that folder in your current branch (paths are relative).'
echo '* For more, visit https://github.com/peterhurford/git-it-on.zsh or type `gitit repo peterhurford git-it-on.zsh`'
echo ''
echo 'Available first arguments for GitHub repos:'
echo -e '\tcompare, commits, history, branch, branches, pulls, issues, grep, ctrlp, repo, help'
echo 'For GitLab repos:'
echo -e '\tglcompare|glcm, glcommits|glco, glhistory|glh, glbranches|glb, glmerges|glm, glissues|gli, glctrlp|glcr, glnetwork|gln, help'
}
gitit() {
gitit_command="$1"
if [ $# -eq 0 ]; then git_open_file
# github commands
elif [ $gitit_command = "compare" ]; then git_open_compare $2
elif [ $gitit_command = "commits" ]; then git_open_commits $2
elif [ $gitit_command = "history" ]; then git_open_history $2 $3
elif [ $gitit_command = "branch" ]; then git_open_branch $2
elif [ $gitit_command = "branches" ]; then git_branches $2
elif [ $gitit_command = "pulls" ]; then git_open_pulls $@
elif [ $gitit_command = "issues" ]; then git_open_issues $@
elif [ $gitit_command = "grep" ]; then git_grep $@
elif [ $gitit_command = "ctrlp" ]; then git_ctrlp $2
elif [ $gitit_command = "repo" ]; then git_open_repo $2 $3
# gitlab commands
elif [ $gitit_command = "glcompare" ] || [ $gitit_command = "glcm" ]; then gitlab_open_compare $@
elif [ $gitit_command = "glcommits" ] || [ $gitit_command = "glco" ]; then git_open_commits $2
elif [ $gitit_command = "glhistory" ] || [ $gitit_command = "glh" ]; then git_open_history $2 $3
elif [ $gitit_command = "glbranches" ] || [ $gitit_command = "glb" ]; then gitlab_open_branches
elif [ $gitit_command = "glmerges" ] || [ $gitit_command = "glm" ]; then gitlab_open_merges $@
elif [ $gitit_command = "glissues" ] || [ $gitit_command = "gli" ]; then git_open_issues $@
elif [ $gitit_command = "glctrlp" ] || [ $gitit_command = "glcr" ]; then gitlab_ctrlp $2
elif [ $gitit_command = "glnetwork" ] || [ $gitit_command = "gln" ]; then gitlab_open_network
elif [ $gitit_command = "help" ]; then git_help
else git_open_file $1 $2
fi
}