-
-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathgit-gh-pr
More file actions
executable file
·109 lines (93 loc) · 2 KB
/
Copy pathgit-gh-pr
File metadata and controls
executable file
·109 lines (93 loc) · 2 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
#!/usr/bin/env bash
set -eu
set -o pipefail
shopt -s lastpipe
function o
{
printf -->&2 "%s:%s\n" "${0##*/}" "$(printf " %q" "$@")"
"$@"
}
function croak {
local ex=$1; shift
printf -->&2 "xxx> %s: %s\n" "${0##*/}" "$*"
exit $ex
}
function do-curl
{
if [[ -z ${github_token-} ]]; then
github_token=$(yq -r '."github.com"[0].oauth_token' ~/.config/hub)
fi
curl --silent --show-error --fail --location -H "Authorization: token ${github_token}" "$@"
}
function get-pr
{
o do-curl "https://api.github.com/repos/${gh_owner}/${gh_repo}/pulls/${1}"
}
function read-pr-info
{
get-pr "$1" \
| jq -r '.head.ref, .head.user.login, if .head.repo.private then .head.repo.ssh_url else .head.repo.clone_url end' \
| {
IFS= read -r pr_branch
IFS= read -r pr_owner
IFS= read -r pr_repo_url
}
}
function read-owner-repo
{
local origin_url
o git remote get-url origin | IFS= read -r origin_url
if [[ "$origin_url" =~ ^(git@)?github\.com:(.*)/(.*)\.git$ ]]; then
gh_owner="${BASH_REMATCH[2]}"
gh_repo="${BASH_REMATCH[3]}"
:
elif [[ "$origin_url" =~ ^(https?|git)://github\.com/(.*)/(.*)$ ]]; then
gh_owner="${BASH_REMATCH[2]}"
gh_repo="${BASH_REMATCH[3]}"
gh_repo="${gh_repo%/}"
gh_repo="${gh_repo%.git}"
gh_repo="${gh_repo%/}"
:
else
croak 1 Cannot parse: "$origin_url"
fi
}
function add-remote
{
if ! o git remote get-url "$1" >&/dev/null; then
o git remote add "$1" "$2"
o git config remote."$1".skipDefaultUpdate true
fi
}
function check-git-repo
{
o git rev-parse --git-dir >/dev/null
}
function prepare
{
check-git-repo
read-owner-repo
read-pr-info "$pr_number"
add-remote "$pr_owner" "$pr_repo_url"
o git fetch "$pr_owner" "$pr_branch"
local_branch="${pr_owner}-${pr_branch}"
o git branch --track "$local_branch" "${pr_owner}/${pr_branch}"
}
function fetch
{
pr_number="$1"
prepare
}
function checkout
{
pr_number="$1"
prepare
o git checkout "$local_branch"
}
function worktree
{
pr_number="$1"
prepare
o git worktree add "../${gh_repo}-${local_branch}" "$local_branch"
}
"$@"