forked from apache/solr
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcherrypick.sh
More file actions
executable file
·215 lines (197 loc) · 6.08 KB
/
cherrypick.sh
File metadata and controls
executable file
·215 lines (197 loc) · 6.08 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
#!/bin/bash
# Licensed to the Apache Software Foundation (ASF) under one or more
# contributor license agreements. See the NOTICE file distributed with
# this work for additional information regarding copyright ownership.
# The ASF licenses this file to You under the Apache License, Version 2.0
# (the "License"); you may not use this file except in compliance with
# the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# Forked and adapted from https://github.com/factorial-io/cherrypicker - MIT license
# Copyright (c) 2017 Shibin Das - @d34dman
function LOG() {
local STATUS=$1
local MESSAGE=$2
echo "[$(date)] ${STATUS} ${MESSAGE}"
}
function usage() {
cat << EOF
Usage: dev-tools/scripts/cherrypick.sh [<options>] <commit-hash> [<commit-hash>...]
-b <branch> Sets the branch(es) to cherry-pick to, typically branch_Nx or branch_x_y
-s Skips precommit test. WARNING: Always run precommit for code- and doc changes
-t Run the full test suite during check, not only precommit
-n Skips git pull of target branch. Useful if you are without internet access
-a Enters automated mode. Aborts cherry-pick and exits on error
-r <remote> Specify remote to push to. Defaults to whatever the branch is tracking.
-p Push to remote. Only done if both cherry-pick and tests succeeded
WARNING: Never push changes to a remote branch before a thorough local test
Simple script for aiding in back-porting one or more (trivial) commits to other branches.
On merge conflict the script will run 'git mergetool'. See 'git mergetool --help'
for help on configuring your favourite merge tool. Check out Sublime Merge (smerge).
Example:
# Backport two commits to both stable and release branches
dev-tools/scripts/cherrypick.sh -b branch_9x -b branch_9_0 deadbeef0000 cafebabe1111
EOF
}
function yesno() {
question=$1
unset answer
echo "$question"
while [[ "$answer" != "y" ]] && [[ "$answer" != "n" ]]; do
read -r answer
if [[ "$answer" == "y" ]]; then
true
else
false
fi
done
}
GIT_COMMAND=git
PRECOMMIT="true"
TESTARG="-x test"
TEST=
TESTS_PASSED=
PUSH=
REMOTE=
NOPULL=
AUTO_MODE=
unset BRANCHES
while getopts ":b:phstnar:" opt; do
case ${opt} in
b)
BRANCHES+=("$OPTARG")
;;
r)
REMOTE=$OPTARG
;;
p)
PUSH=true
;;
s)
PRECOMMIT=
;;
a)
AUTO_MODE="true"
;;
n)
NOPULL="true"
;;
t)
TEST="true"
TESTARG=""
;;
h)
usage
exit 0
;;
\?)
echo "Unknown option $OPTARG"
usage
exit 1
esac
done
shift $((OPTIND -1))
COMMITS=( "$@" )
if [ ${#BRANCHES[@]} -eq 0 ]; then
LOG INFO "Lacking -b option, must specify target branch. Supports multiple -b options too"
usage
exit
fi
if [ ${#COMMITS[@]} -eq 0 ]; then
LOG ERROR "Please specify one or more commits"
usage
exit
fi
## Loop over branches
for BRANCH in "${BRANCHES[@]}"; do
echo ""
LOG "INFO" "============================================";
LOG "INFO" "Git branch: $BRANCH"
LOG "INFO" "============================================";
echo ""
LOG INFO "Checking out target branch $BRANCH"
# shellcheck disable=SC2086
$GIT_COMMAND checkout $BRANCH
if [ $? -gt 0 ]; then
LOG ERROR "Failed checking out branch $BRANCH"
exit 2
fi
if [[ ! "$NOPULL" ]]; then
# shellcheck disable=SC2086
$GIT_COMMAND fetch $REMOTE
# shellcheck disable=SC2086
$GIT_COMMAND pull --ff-only $REMOTE
else
LOG INFO "Skipping git pull"
fi
# Loop over commits
for i in "${COMMITS[@]}"
do
LOG "INFO" "::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::";
LOG "INFO" "Processing commit : $i"
LOG "INFO" "::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::";
$GIT_COMMAND cherry-pick "$i" -x
if [ $? -eq 0 ]; then
LOG "INFO" "Cherry pick of $i completed successfully."
continue;
fi
LOG "ERROR" "Cherry pick encountered an error."
if [[ "$AUTO_MODE" ]]; then
LOG ERROR "Aborting cherry-pick and exiting. Please handle this manually"
$GIT_COMMAND cherry-pick --abort
exit 2
fi
if yesno "Do you want me to open mergetool? (y/n) "; then
$GIT_COMMAND mergetool
fi
if yesno "Have you resolved the merge conflict? (y/n) "; then
LOG INFO "Continuing..."
$GIT_COMMAND cherry-pick --continue
else
if yesno "Clean up by aborting the cherry-pick? (y/n) "; then
$GIT_COMMAND cherry-pick --abort
fi
LOG INFO "Bye"
exit 1
fi
done
LOG INFO "All cherry-picks to branch $BRANCH succeeded"
if [[ "$PRECOMMIT" ]] || [[ "$TEST" ]]; then
LOG "INFO" "Testing the cherry-pick on $BRANCH by running 'gradlew check ${TESTARG}'"
# shellcheck disable=SC2086
./gradlew clean check -q $TESTARG -Pvalidation.errorprone=true
if [ $? -gt 0 ]; then
LOG "WARN" "Tests failed. Please fix and push manually"
exit 2
fi
TESTS_PASSED="true"
LOG "INFO" "Tests passed on $BRANCH"
else
LOG "INFO" "Skipping tests"
fi
if [[ "$PUSH" ]]; then
if [[ ! $TESTS_PASSED ]]; then
LOG "WARN" "Cannot auto push if tests are disabled"
exit 1
fi
if [[ -z "$REMOTE" ]]; then
LOG "WARN" "Remote not specified, attempting to auto detect"
REMOTE=$(git config --get "branch.$BRANCH.remote")
fi
LOG "INFO" "Pushing changes to $REMOTE/$BRANCH"
# shellcheck disable=SC2086
$GIT_COMMAND push $REMOTE $BRANCH
if [ $? -gt 0 ]; then
LOG "WARN" "PUSH to $REMOTE/$BRANCH failed, please clean up and proceed manually"
exit 2
fi
LOG "INFO" "Pushed $BRANCH to remote. Cherry-pick complete."
fi
done
LOG "INFO" "SUCCESS on branches ${BRANCHES[*]} for commits ${COMMITS[*]}"