-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathshared.sh
More file actions
261 lines (207 loc) · 5.98 KB
/
Copy pathshared.sh
File metadata and controls
261 lines (207 loc) · 5.98 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
#!/bin/bash
master="master"
bold=$(tput bold)
underline=$(tput smul)
normal=$(tput sgr0)
cmd="git-multi-pr"
oksh="ok.sh"
REF_BRANCH_PREFIX="_git-multi-pr-"
KEY_GIT_MULTI_BRANCH="GIT_MULTI_BRANCH"
KEY_PR="PR"
_get_ref_pr_url() {
local ref="$1"
local pr_number="$(_get_ref_pr_number "$ref")"
if [[ ! -z "$pr_number" ]]; then
local repo_org="$(_get_repo_org)"
local repo_name="$(_get_repo_name)"
echo "https://github.com/$repo_org/$repo_name/pull/$pr_number"
fi
}
_get_ref_pr_number() {
local ref="$1"
local body="$(_git_get_commit_body "$ref")"
echo "$body" | while read -r line; do
if [[ "$line" == "$KEY_PR="* ]]; then
echo "${line#"$KEY_PR="}"
break
fi
done
}
_git_get_branch() {
git symbolic-ref --short -q $@ HEAD
}
_git_get_commits() {
git rev-list --abbrev-commit $@ "origin/$master"..
}
_git_get_relative_commits() {
git rev-list --abbrev-commit "$@"..
}
_git_get_commit_sha() {
if [ -z "$1" ]; then
$cmd usage
exit 1
fi
git rev-parse --short $@ 2>/dev/null
}
_git_get_commit_subject() {
if [ -z "$1" ]; then
$cmd usage
exit 1
fi
git log -n 1 --pretty=format:%s $@
}
_git_get_commit_body() {
if [ -z "$1" ]; then
$cmd usage
exit 1
fi
git log -n 1 --pretty=format:%b $@
}
_git_check_clean_state() {
git diff-index --quiet $@ HEAD -- && ! _git_check_rebase_state
}
_git_check_rebase_state() {
(test -d "$(git rev-parse --git-path rebase-merge)" || \
test -d "$(git rev-parse --git-path rebase-apply)" ) \
|| return 1
return 0
}
_git_verify_branch() {
git rev-parse --verify $@ &>/dev/null
}
_get_sha_in_local_queue() {
local ref="$1"
local upper_ref="$(echo "$ref" | tr '[:lower:]' '[:upper:]')"
if [[ "$upper_ref" == "BASE" ]] || [[ "$upper_ref" == "BASE+"* ]]; then
local count="$(_git_get_commits | wc -l)"
if [[ "$upper_ref" == "BASE" ]]; then
local index="0"
else
local index="${ref#BASE+}"
if [[ -z "$index" ]]; then
index="1"
fi
fi
ref="HEAD~$(($count - $index - 1))"
fi
ref="$(_git_get_commit_sha "$ref")"
if [[ ! -z "$ref" ]] && ! _is_local_commit "$ref"; then
echo "ref $1 is not a local commit in your queue." 1>&2
echo "Run \`$cmd queue\` to find a local commit, or use a relative ref like HEAD~." 1>&2
exit 1
fi
echo "$ref"
}
_edit_ref() {
if [ -z "$1" ]; then
git rebase -i "origin/$master"
return 0
fi
local sha="$(_get_sha_in_local_queue "$1")"
local rebase_command="edit"
if [ $# -ge 2 ]; then
rebase_command="$2"
fi
echo "Entering rebase for $1 == $sha with command $rebase_command."
# Replace "pick $sha" and "# pick $sha" with "$rebase_command $sha".
local regex="'s/^\(# \)\{0,1\}pick $sha/$rebase_command $sha/'"
GIT_SEQUENCE_EDITOR="sed -i -e $regex" git rebase -i "origin/$master" &>/dev/null
}
_continue_rebase() {
echo "Exiting rebase."
git rebase --continue
}
_abort_rebase() {
echo "Aborting rebase."
git rebase --abort
}
_is_local_commit() {
local commits="$(_git_get_commits)"
for commit in $commits; do
if [ "$commit" == "$1" ]; then
return 0
fi
done
return 1
}
_get_remote_ref_branch() {
local ref_branch="$1"
local email="$(git config user.email)"
local username="${email%@*}"
echo "$username$ref_branch"
}
_escape() {
# TODO: I can't get this to escape correctly for the following commit message:
# Hello, world! \"
echo "$1" | tr '\' '\\' | tr '$' '\$' | tr '`' '\`' | tr '"' '\"' | tr '[' '(' | tr ']' ')'
}
_ensure_ref_pr_open() {
local prev_ref_branch="$1"
local ref="$2"
local repo_org="$(_get_repo_org)"
local repo_name="$(_get_repo_name)"
local pr_number="$(_get_ref_pr_number "$ref")"
if [[ "$prev_ref_branch" == "origin/$master" ]]; then
local prev_remote_branch="$master"
else
local prev_remote_branch="$(_get_remote_ref_branch "$prev_ref_branch")"
fi
if [ ! -z "$pr_number" ]; then
echo "Ensuring that the PR is open."
# The PR's base must be set to an open branch, or else the PR may become closed forever.
# https://github.com/isaacs/github/issues/361
echo "> $oksh update_pull_request \"$repo_org/$repo_name\" \"$pr_number\" base=\"$prev_remote_branch\""
$oksh update_pull_request "$repo_org/$repo_name" "$pr_number" base="$prev_remote_branch" &>/dev/null
echo "> $oksh update_pull_request \"$repo_org/$repo_name\" \"$pr_number\" state=\"open\""
$oksh update_pull_request "$repo_org/$repo_name" "$pr_number" state="open" &>/dev/null
fi
}
_get_repo_org() {
# Take the repo url.
local repo_url="$(git remote get-url --push origin)"
# Remove the extension.
repo_url="${repo_url%.git}"
# Convert : and / to new lines.
repo_url="$(echo "$repo_url" | tr ':' '\n' | tr '/' '\n')"
# Split into array.
local IFS=$'\n' repo_components=($repo_url)
local count="${#repo_components[@]}"
local repo_org_index="$(($count-2))"
echo "${repo_components[$repo_org_index]}"
}
_get_repo_name() {
# Take the repo url.
local repo_url="$(git remote get-url --push origin)"
# Remove the extension.
repo_url="${repo_url%.git}"
# Convert : and / to new lines.
repo_url="$(echo "$repo_url" | tr ':' '\n' | tr '/' '\n')"
# Split into array.
local IFS=$'\n' repo_components=($repo_url)
local count="${#repo_components[@]}"
local repo_name_index="$(($count-1))"
echo "${repo_components[$repo_name_index]}"
}
_get_ref_branch_name() {
local ref="$1"
local body="$(_git_get_commit_body "$ref")"
echo "$body" | while read -r line; do
if [[ "$line" == "$KEY_GIT_MULTI_BRANCH="* ]]; then
echo "${line#"$KEY_GIT_MULTI_BRANCH="}"
break
fi
done
}
_git_is_merge_commit () {
local sha="$1"
local merge_sha=$(git rev-list -1 --merges ${sha}~1..${sha})
[ -z "$merge_sha" ] && return 1
return 0
}
# open, pending (submitting), merged (submitted), closed (dropped)
_is_pr_merged() {
local number="$1"
local repo_org="$(_get_repo_org)"
local repo_name="$(_get_repo_name)"
$oksh _get "/repos/$repo_org/$repo_name/pulls/$number/merge" &>/dev/null
}