-
Notifications
You must be signed in to change notification settings - Fork 2
/
rgl2
executable file
·169 lines (155 loc) · 4.91 KB
/
rgl2
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
#! /bin/bash
#
# rgl2 (remote-git-log): get git log information from a remote git repository
# without having a local checked out version. Output in json format.
#
#
# Check the command line args
#
if [ $# -ne 3 ]; then
echo "usage: rgl2 <host> <project> <file path>"
echo " supported hosts: github, gitlab, bitbucket"
echo " shortcuts: gh, gl, bb"
echo " example: rgl2 github openssl/openssl apps/info.c"
echo " example: rgl2 gitlab tortoisegit/tortoisegit LICENSE"
echo " example: rgl2 bitbucket swsc/lookup showCnt"
exit 1
fi
host=$1
proj=$2
filepath=$3
#
# get the github API auth key(s) from ~/github-auth
#
if [ ! -f ~/github-auth ]; then
echo Error: missing ~/github-auth file
echo Error: missing ~/github-auth file >&2
echo ~/github-auth must exists and contain github API auth key or keys >&2
echo 'Create keys at github->Settings->Developer Settings->personal access tokens' >&2
exit 1
fi
mapfile -t auth_array < ~/github-auth
len="${#auth_array[@]}"
i="$(($RANDOM % $len))"
auth=${auth_array[$i]}
#
# Functions
#
# format data from github
function render_github {
response="$1"
echo "$response" | jq-linux64 -r '.[] | {commit: .sha}' | sed \
-e '/^{/d' \
-e '/^}/d' \
-e 's/\"//g' \
-e 's/^ commit: //'
}
# format data from gitlab
function render_gitlab {
response="$1"
echo "$response" | jq-linux64 -r '{commit: .[].id}' | sed \
-e '/^{/d' \
-e '/^}/d' \
-e 's/\"//g' \
-e 's/^ commit: //'
}
# format data from bitbucket
function render_bitbucket {
response="$1"
echo "$response" | jq-linux64 -r '{commit: .values[].hash}' | sed \
-e '/^{/d' \
-e '/^}/d' \
-e 's/\"//g' \
-e 's/^ commit: //'
}
#
# Main body of script
#
case $host in
github|gh)
empty='[
]'
page=0
while true; do
page=$((page + 1))
url="https://api.github.com/repos/$proj/commits?path=$filepath&per_page=100&page=$page"
#echo $url
response=$(curl -s -L -H "Authorization: token $auth" "$url")
if [[ $response = *"rate limit exceeded"* ]]; then
# wait minute then try again
sleep 60
page=$((page - 1))
continue
elif [[ "$response" = "$empty" ]]; then
echo ""
break
else
commits=`render_github "$response"`
# quick sanity check: make sure the first commit is a
# valid sha1 hash (only check the first to save time)
clen=`printf "%s\n" "$commits" | head -1 | wc -c`
if [ $clen -ne 41 ]; then
echo ""
echo "Error: rgl2 returned invalid response"
exit 3
fi
printf "$commits\n"
fi
done
;;
gitlab|gl)
page=0
while true; do
page=$((page + 1))
proj=`echo $proj | sed -e "s@/@%2F@"`
filepath=`echo $filepath | sed -e "s@/@%2F@"`
url="https://gitlab.com/api/v4/projects/$proj/repository/commits?path=$filepath&per_page=100&page=$page"
#echo $url
response=$(curl -s "$url")
if [[ "$response" = "[]" ]]; then
echo ""
break
else
commits=`render_gitlab "$response"`
# quick sanity check: make sure the first commit is a
# valid sha1 hash (only check the first to save time)
clen=`printf "%s\n" "$commits" | head -1 | wc -c`
if [ $clen -ne 41 ]; then
echo ""
echo "Error: rgl2 returned invalid response"
exit 3
fi
printf "$commits\n"
fi
done
;;
bitbucket|bb)
page=0
while true; do
page=$((page + 1))
url="https://api.bitbucket.org/2.0/repositories/$proj/commits/?path=$filepath&pagelen=100&page=$page"
#echo "$url"
response=$(curl -s "$url")
end=`echo "$response" | jq-linux64 -r '{commit: .values[].hash}'`
if [[ "$end" = "" ]]; then
echo ""
break
else
commits=`render_bitbucket "$response"`
# quick sanity check: make sure the first commit is a
# valid sha1 hash (only check the first to save time)
clen=`printf "%s\n" "$commits" | head -1 | wc -c`
if [ $clen -ne 41 ]; then
echo ""
echo "Error: rgl2 returned invalid response"
exit 3
fi
printf "$commits\n"
fi
done
;;
*)
echo "Error: $host not supported, must be one of github, gitlab, or bitbucket"
exit 1
esac
exit 0