-
Notifications
You must be signed in to change notification settings - Fork 0
/
git-get
executable file
·176 lines (147 loc) · 3.95 KB
/
git-get
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
#!/usr/bin/env bash
set -e
__main() {
if [[ $# -lt 1 ]] ; then
__usage
exit 1
fi
local preferred_remote="origin"
if [ -n "$GIT_GET_REMOTE" ] ; then
preferred_remote="$GIT_GET_REMOTE"
fi
local remote_cfg
remote_cfg=$(git remote -v | grep -Eo "$preferred_remote\s.*\(fetch\)")
if ! echo "$remote_cfg" | grep -Eo 'github.com' >/dev/null ; then
echo "fatal: remote doesn't seem to be a github.com project"
exit 1
fi
local remote
remote=$(echo "$remote_cfg" | grep -Eo -m 1 "^\b(\w+)(\-|_)*(\w+)\b")
local user
user=$(echo "$remote_cfg" | grep -Eo "github.com.*\/" | sed -e s/github.com// -e s:/::g -e s/://)
local remote_url
remote_url=$(git remote get-url "$remote")
local project
project=$(basename "$remote_url" | sed s:\.git::)
local cmd=$1
case "$cmd" in
fetch)
if [[ -z "$2" ]]; then
echo "fatal: you need to pass a pull request number"
exit 1
fi
if [ -n "$2" ] && [ "$2" -eq "$2" ] 2>/dev/null; then
local number=$2
local branch
branch="pull/$number"
git fetch --update-head-ok "$remote" +"pull/$number/head:$branch"
git checkout "$branch"
else
echo "fatal: \"$2\" is not a number"
exit 1
fi
;;
patches)
local url
url="https://api.github.com/repos/$user/$project/pulls"
local resp
if command -v curl >/dev/null ; then
resp=$(curl -L -s -f "$url")
elif command -v wget >/dev/null ; then
resp=$(wget --quiet -O - "$url")
fi
local patch_urls
patch_urls=$(echo "$resp" |\
grep -Eo '\"patch_url\":\s.*"' |\
sed -e s/\"patch_url\":\ // \
-e s/\"//g)
mkdir -p "patches"
local patches
patches=$(echo "$patch_urls" | tr '\n' ' ')
OIFS=$IFS
IFS=' '
for x in $patches
do
local patch
patch=$(echo "$x" | grep -Eo '[[:digit:]]*\.patch')
if [[ ! -f "patches/$patch" ]] ; then
if command -v curl >/dev/null ; then
curl "$x" -L -s -o "patches/$patch"
elif command -v wget >/dev/null ; then
wget --quiet "$x" -P "patches"
fi
else
echo "skipping $patch, already downloaded."
fi
done
IFS=$OIFS
;;
ls)
local url="https://api.github.com/repos/$user/$project/pulls"
local resp
if command -v curl >/dev/null ; then
resp=$(curl -L -s -f "$url")
elif command -v wget >/dev/null ; then
resp=$(wget --quiet -O - "$url")
fi
local numbers
local titles
local html_urls
mapfile -t numbers < <(echo "$resp" | grep \"number\" | sed -e s/\"number\":// -e s/\"//g -e s/,//g -e s/^[[:space:]]*//g)
mapfile -t titles < <(echo "$resp" | grep \"title\" | sed -e s/\"title\":// -e s/\"//g -e s/,//g -e s/^[[:space:]]*//g)
mapfile -t html_urls < <(echo "$resp" | sed -n -e 's/\"html_url\":[[:space:]]\"\(.*\/\)\([[:digit:]]\+\)\(\",$\)/\1\2/p')
if [[ ${#numbers[@]} -eq 0 ]]; then
echo
echo "no pull requests for $user/$project"
echo
exit 0
else
echo "listing pull requests for $user/$project"
echo
for (( i=0; i < ${#numbers[@]}; i++ )); do
printf "%s %s\n%s\n\n" "${numbers[i]}" "${titles[i]}" "${html_urls[i]}";
done
fi
;;
send)
local target_user
if [[ -z "$2" ]]; then
target_user=$user
else
target_user=$2
fi
local target_branch
if [[ -z "$3" ]]; then
target_branch="master"
else
target_branch=$3
fi
local branch
branch=$(git branch | grep "\*" | cut -d ' ' -f2)
local url
url="https://github.com/$target_user/$project/compare/$target_branch...$user:$branch"
if command -v open | tee /dev/null | grep -v "alias" ; then
open "$url"
elif command -v xdg-open >/dev/null ; then
xdg-open "$url"
fi
;;
*)
__usage
exit 1
;;
esac
}
__usage() {
echo
echo "git get COMMAND"
echo
echo "Available commands: "
echo
echo "fetch NUMBER Fetch pull request NUMBER"
echo "ls List pull requests"
echo "patches Download patches"
echo "send [USER][BRANCH] Open URL to create a pull request"
echo
}
__main "$@"