-
-
Notifications
You must be signed in to change notification settings - Fork 679
Expand file tree
/
Copy pathgit-pr
More file actions
executable file
·330 lines (278 loc) · 8.57 KB
/
git-pr
File metadata and controls
executable file
·330 lines (278 loc) · 8.57 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
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
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
#!/bin/bash
set -e
set -u
set -o pipefail
RED_BOLD='\033[1;31m'
NC='\033[0m' # No Color
function die() {
echo "$@" >&2
exit 1
}
function cmd_checkout_usage() {
cat <<EOF
Usage: $0 checkout <pr_ref> [--replace-remote]
Checks out a Pull Request locally.
Arguments:
<pr_ref> The Pull Request number or URL (e.g., 123 or https://github.com/owner/repo/pull/123).
Options:
--replace-remote If the remote already exists but points to a different URL, update it.
--help Show this help message.
EOF
}
function cmd_pull_usage() {
cat <<EOF
Usage: $0 pull [args]
Pulls updates from the Pull Request's remote branch into the current branch.
Arguments:
[args] Additional arguments to pass to 'git pull'.
Options:
--help Show this help message.
EOF
}
function cmd_push_usage() {
cat <<EOF
Usage: $0 push [args]
Pushes updates from the current branch to the Pull Request's remote branch.
Arguments:
[args] Additional arguments to pass to 'git push'.
Options:
--help Show this help message.
EOF
}
function cmd_checkout () {
if ! command -v gh &> /dev/null; then
die "ERROR: 'gh' (GitHub CLI) is not installed. Please install it to use this feature."
fi
if ! command -v jq &> /dev/null; then
die "ERROR: 'jq' is not installed. Please install it to use this feature."
fi
local replace_remote=0
local pr_ref=""
local short_opts=""
local long_opts="replace-remote,help"
local parsed_options=$(getopt --options "$short_opts" --longoptions "$long_opts" --name "$0" -- "$@")
if [ $? -ne 0 ]; then
cmd_checkout_usage >&2
exit 1
fi
eval set -- "$parsed_options"
while true; do
case "$1" in
--replace-remote)
replace_remote=1
shift
;;
--help)
cmd_checkout_usage
exit 0
;;
--)
shift
break
;;
*)
die "Unknown option: $1"
;;
esac
done
if [ $# -gt 0 ]; then
pr_ref="$1"
shift
fi
if [ -z "$pr_ref" ]; then
cmd_checkout_usage >&2
exit 1
fi
if [ $# -gt 0 ]; then
echo "ERROR: Unexpected arguments: $@" >&2
cmd_checkout_usage >&2
exit 1
fi
local pr_json=$(
gh pr view \
--json headRepositoryOwner,headRepository,headRefName,maintainerCanModify,baseRefName,number \
"$pr_ref"
)
mapfile -t pr_info < <(
echo "$pr_json" | jq -r '
.headRepositoryOwner.login,
.headRepository.name,
.headRefName,
.maintainerCanModify,
.baseRefName,
.number
')
local pr_repo_owner="${pr_info[0]}"
local pr_repo_name="${pr_info[1]}"
local pr_branch="${pr_info[2]}"
local pr_maintainer_can_modify="${pr_info[3]}"
local pr_base_branch="${pr_info[4]}"
local pr_number="${pr_info[5]}"
if [[ "$pr_maintainer_can_modify" != "true" ]]; then
echo -e "${RED_BOLD}WARNING${NC}: Maintainer write access not granted!"
fi
local pr_remote_url="https://github.com/$pr_repo_owner/$pr_repo_name"
local pr_remote_name=$(find_matching_remote "$pr_remote_url")
# No remote for this url already, so create one
if [[ -z "$pr_remote_name" ]]; then
local pr_remote_name="${pr_repo_owner}-${pr_repo_name}"
local pr_remote_readable=$(can_access_remote "$pr_remote_url")
if [[ "$pr_remote_readable" -ne "1" ]]; then
echo "WARNING: https access to remote failed, trying ssh"
pr_remote_url="ssh://github.com/$pr_repo_owner/$pr_repo_name"
pr_remote_readable=$(can_access_remote "$pr_remote_url")
fi
if [[ "$pr_remote_readable" -ne "1" ]]; then
echo "ERROR: Unable to access remote via https or ssh"
echo " Check the URL, ssh agent, and access settings"
return 1
fi
if git remote | grep -q "^$pr_remote_name$"; then
local existing_url=$(git remote get-url "$pr_remote_name")
if [[ "$existing_url" != "$pr_remote_url" ]]; then
if [ "$replace_remote" -eq 1 ]; then
git remote set-url "$pr_remote_name" "$pr_remote_url"
else
echo "ERROR: Remote '$pr_remote_name' already exists but points to a different URL." >&2
echo " Existing: $existing_url" >&2
echo " New: $pr_remote_url" >&2
echo "Use the --replace-remote flag to update it." >&2
return 1
fi
fi
else
git remote add "$pr_remote_name" "$pr_remote_url"
fi
fi
GIT_TERMINAL_PROMPT=0 \
git fetch "$pr_remote_name"
local pr_base_remote
# todo: upstream won't exist if you're not doing the triangle workflow
if ! git remote | grep -q "^upstream$"; then
local pr_repo_branch_lines=$(
gh repo view "$pr_repo_owner/$pr_repo_name" --branch "$pr_branch" \
--json parent --jq ".parent.owner.login,.parent.name"
)
# NOTE: The above command will print two lines when run directly, however,
# command substitution, $(...), will strips trailing newlines.
if [[ -z "$pr_repo_branch_lines" ]]; then
# If there was no parent, the PR is from from the same repo as it's
# being merged into, so use the same remote name.
pr_base_remote=$pr_remote_name
else
readarray -t pr_repo_branch_info <<< "$pr_repo_branch_lines"
local pr_repo_branch_owner="${pr_repo_branch_info[0]}"
local pr_repo_branch_name="${pr_repo_branch_info[1]}"
# TODO: the PR is coming from another repo. We know the github owner and
# repo name, but need to find our local remote for that repo.
echo "TODO: FIND REMOTE FOR: $pr_repo_branch_owner/$pr_repo_branch_name"
return 1
fi
else
pr_base_remote=upstream
fi
# Name the local branch after the pr number so its clear it's a PR checkout
local local_branch="pr-$pr_number"
git branch "$local_branch" "$pr_remote_name/$pr_branch"
git checkout "$local_branch"
git branch --set-upstream-to="$pr_base_remote/$pr_base_branch" "$local_branch"
git config set "branch.$local_branch.pushRemote" "$pr_remote_name"
# Unfortunately, git's support for customizing the branch that is pushed
# to is rather limited. `push.default` and `branch.$name.merge` control this,
# but both assume some matching between the local, upstream, and push branch
# names. But in our triangle workflow, the three are all different.
git config set "branch.$local_branch.prPushRemoteBranch" "$pr_branch"
echo "NOTE: You must explicitly specify remote and branch for push/pull
These commands will do it for you:
tools/git-pr push
tools/git-pr pull
"
}
function can_access_remote() {
GIT_TERMINAL_PROMPT=0 \
git ls-remote "$1" BOGUS 2>/dev/null && status=$? \
|| status=$?
if [[ $status -eq 0 ]]; then
echo "1"
else
echo "0"
fi
}
function find_matching_remote() {
local url=$1
# Normalize the URL for comparison. This handles https://, ssh://, and git@
# style URLs, with or without a .git suffix.
local normalized_url
normalized_url=$(echo "$url" | sed -e 's,^.*://,,' -e 's,^.*@,,' -e 's,\.git$,,' -e 's,:,/,')
local remote
for remote in $(git remote); do
local remote_url
remote_url=$(git remote get-url "$remote")
local normalized_remote_url
normalized_remote_url=$(echo "$remote_url" | sed -e 's,^.*://,,' -e 's,^.*@,,' -e 's,\.git$,,' -e 's,:,/,')
if [[ "$normalized_url" == "$normalized_remote_url" ]]; then
echo "$remote"
return 0
fi
done
return 1
}
function cmd_push() {
if [[ "${1:-}" == "--help" ]]; then
cmd_push_usage
exit 0
fi
local local_branch=$(git rev-parse --abbrev-ref HEAD)
local pr_branch=$(git config get branch.$local_branch.prPushRemoteBranch)
local pr_remote=$(git config get branch.$local_branch.pushRemote)
(set -x; git push "$pr_remote" "$local_branch:$pr_branch")
}
function cmd_pull() {
if [[ "${1:-}" == "--help" ]]; then
cmd_pull_usage
exit 0
fi
local local_branch=$(git rev-parse --abbrev-ref HEAD)
local pr_branch=$(git config get branch.$local_branch.prPushRemoteBranch)
local pr_remote=$(git config get branch.$local_branch.pushRemote)
(set -x; git pull "$pr_remote" "$pr_branch" "$@")
}
function usage() {
cat <<EOF
Usage: $0 <command> [args]
Commands:
checkout <pr_ref> Checkout a PR locally
pull [args] Pull updates from the PR's remote branch
push Push updates to the PR's remote branch
Options:
--help Show this help message.
EOF
}
function git-pr-main() {
if [[ $# -eq 0 ]]; then
usage
exit 1
fi
cmd=$1
shift
case "$cmd" in
checkout)
cmd_checkout "$@"
;;
pull)
cmd_pull "$@"
;;
push)
cmd_push "$@"
;;
--help|-h)
usage
exit 0
;;
*)
usage
exit 1
;;
esac
}
git-pr-main "$@"