|
| 1 | +#!/bin/bash |
| 2 | +# |
| 3 | +# Licensed to the Apache Software Foundation (ASF) under one or more |
| 4 | +# contributor license agreements. See the NOTICE file distributed with |
| 5 | +# this work for additional information regarding copyright ownership. |
| 6 | +# The ASF licenses this file to You under the Apache License, Version 2.0 |
| 7 | +# (the "License"); you may not use this file except in compliance with |
| 8 | +# the License. You may obtain a copy of the License at |
| 9 | +# |
| 10 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | +# |
| 12 | +# Unless required by applicable law or agreed to in writing, software |
| 13 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | +# See the License for the specific language governing permissions and |
| 16 | +# limitations under the License. |
| 17 | +# |
| 18 | + |
| 19 | +# |
| 20 | +# Pull request applier. |
| 21 | +# |
| 22 | + |
| 23 | +# |
| 24 | +# Start of Functions. |
| 25 | +# |
| 26 | + |
| 27 | +# |
| 28 | +# Prints usage. |
| 29 | +# |
| 30 | +usage () { |
| 31 | + echo 'Usage: scripts/apply-pull-request.sh <pull-request-id> [-tb|--targetbranch <branch-name>] [--with-gpg] [-s|--sign-off]' |
| 32 | + echo 'The script takes pull-request by given id and merges (with squash) all changes to target branch (master by default).' |
| 33 | + echo "Argument 'pull-request-id' is mandatory." |
| 34 | + echo "Target branch can be overwritten by using [-tb|--targetbranch <branch-name>] argument paramethers." |
| 35 | +} |
| 36 | + |
| 37 | +# |
| 38 | +# End of Functions. |
| 39 | +# |
| 40 | + |
| 41 | +if [ "${GIT_HOME}" = "" ]; then |
| 42 | + GIT_HOME="$(dirname "$(cd "$(dirname "$0")"; "pwd")")"; |
| 43 | +fi |
| 44 | + |
| 45 | +cd "${GIT_HOME}" || { echo "failed to change director ${GIT_HOME}"; exit 1; } |
| 46 | + |
| 47 | +if [ "${SCRIPTS_HOME}" = "" ]; then |
| 48 | + SCRIPTS_HOME="${GIT_HOME}/scripts/" |
| 49 | +fi |
| 50 | + |
| 51 | +. "${SCRIPTS_HOME}"/git_patch_functions.sh # Import patch functions. |
| 52 | + |
| 53 | +PR_ID=$1 |
| 54 | + |
| 55 | +# |
| 56 | +# Start reading of command line params. |
| 57 | +# |
| 58 | +if [ "${PR_ID}" = "" ]; then |
| 59 | + echo "$0, ERROR:" |
| 60 | + echo >&2 "You have to specify 'pull-request-id'." |
| 61 | + echo |
| 62 | + usage |
| 63 | + exit 1 |
| 64 | +fi |
| 65 | + |
| 66 | +if [ "${PR_ID}" = "-h" ]; then |
| 67 | + usage |
| 68 | + exit 0 |
| 69 | +fi |
| 70 | + |
| 71 | +if [ "${PR_ID}" = "--help" ]; then |
| 72 | + usage |
| 73 | + exit 0 |
| 74 | +fi |
| 75 | + |
| 76 | + |
| 77 | +while [[ $# -ge 2 ]] |
| 78 | +do |
| 79 | + key="$2" |
| 80 | + |
| 81 | + case $key in |
| 82 | + -tb|--targetbranch) |
| 83 | + TARGET_BRANCH="$3" |
| 84 | + shift 2 |
| 85 | + ;; |
| 86 | + |
| 87 | + --with-gpg) |
| 88 | + WITH_GPG="true" |
| 89 | + shift |
| 90 | + ;; |
| 91 | + |
| 92 | + -s|--sign-off) |
| 93 | + WITH_SIGN_OFF="true" |
| 94 | + shift |
| 95 | + ;; |
| 96 | + |
| 97 | + *) |
| 98 | + echo "Unknown parameter: ${key}" |
| 99 | + echo |
| 100 | + usage |
| 101 | + exit 1 |
| 102 | + ;; |
| 103 | + esac |
| 104 | +done |
| 105 | +# |
| 106 | +# Enf reading of command line params. |
| 107 | +# |
| 108 | + |
| 109 | + |
| 110 | +# Script variables. |
| 111 | +if [ "${APACHE_GIT}" = "" ]; then |
| 112 | + APACHE_GIT="https://gitbox.apache.org/repos/asf/ignite-python-thin-client.git" |
| 113 | +fi |
| 114 | + |
| 115 | +if [ "${GITHUB_MIRROR}" = "" ]; then |
| 116 | + GITHUB_MIRROR= "[email protected]:apache/ignite-python-thin-client.git" |
| 117 | +fi |
| 118 | + |
| 119 | +if [ "${TARGET_BRANCH}" = "" ]; then |
| 120 | + TARGET_BRANCH="master" |
| 121 | +fi |
| 122 | + |
| 123 | +requireCleanWorkTree "${GIT_HOME}" |
| 124 | + |
| 125 | +CURRENT_BRANCH=$(git rev-parse --abbrev-ref HEAD) |
| 126 | + |
| 127 | +if [ "$CURRENT_BRANCH" != "${TARGET_BRANCH}" ]; then |
| 128 | + echo "$0, ERROR:" |
| 129 | + echo "You have to be on ${TARGET_BRANCH} branch." |
| 130 | + |
| 131 | + exit 1 |
| 132 | +fi |
| 133 | + |
| 134 | +# Check that target branch is up-to-date. |
| 135 | +APACHE_GIT_TARGET_BRANCH="apache-git-target-br-tmp" |
| 136 | + |
| 137 | +git fetch ${APACHE_GIT} ${TARGET_BRANCH}:${APACHE_GIT_TARGET_BRANCH} &> /dev/null |
| 138 | +if test $? != 0; then |
| 139 | + echo "$0, ERROR:" |
| 140 | + echo >&2 "Couldn't fetch '${TARGET_BRANCH}' branch from ${APACHE_GIT}." |
| 141 | + exit 1 |
| 142 | +fi |
| 143 | + |
| 144 | +LOCAL_TARGET_BR_HASH=$(git rev-parse @) |
| 145 | +REMOTE_TARGET_BR_HASH=$(git rev-parse ${APACHE_GIT_TARGET_BRANCH}) |
| 146 | +BASE_HASH=$(git merge-base @ ${APACHE_GIT_TARGET_BRANCH}) |
| 147 | + |
| 148 | +git branch -D ${APACHE_GIT_TARGET_BRANCH} &> /dev/null |
| 149 | + |
| 150 | +if [ "$LOCAL_TARGET_BR_HASH" != "$REMOTE_TARGET_BR_HASH" ]; then |
| 151 | + echo "$0, ERROR:" |
| 152 | + |
| 153 | + if [ "$LOCAL_TARGET_BR_HASH" = "$BASE_HASH" ]; then |
| 154 | + echo "Your local ${TARGET_BRANCH} branch is not up-to-date. You need to pull." |
| 155 | + elif [ "$REMOTE_TARGET_BR_HASH" = "$BASE_HASH" ]; then |
| 156 | + echo "Your local ${TARGET_BRANCH} branch is ahead of ${TARGET_BRANCH} branch at Apache git. You need to push." |
| 157 | + else |
| 158 | + echo "Your local ${TARGET_BRANCH} and Apache git ${TARGET_BRANCH} branches diverged. You need to pull, merge and pull." |
| 159 | + fi |
| 160 | + |
| 161 | + exit 1 |
| 162 | +fi |
| 163 | + |
| 164 | +echo "Local ${TARGET_BRANCH} is Up-to-date." |
| 165 | +echo |
| 166 | + |
| 167 | +# Checkout pull-request branch. |
| 168 | +PR_BRANCH_NAME="pull-${PR_ID}-head" |
| 169 | + |
| 170 | +git fetch "${GITHUB_MIRROR}" "pull/${PR_ID}/head:${PR_BRANCH_NAME}" &> /dev/null |
| 171 | +if test $? != 0; then |
| 172 | + echo "$0, ERROR:" |
| 173 | + echo >&2 "There was not found pull request by ID = '${PR_ID}'." |
| 174 | + exit 1 |
| 175 | +fi |
| 176 | + |
| 177 | +# Get author name number. |
| 178 | +git checkout "${PR_BRANCH_NAME}" &> /dev/null |
| 179 | +if test $? != 0; then |
| 180 | + echo "$0, ERROR:" |
| 181 | + echo >&2 "Failed to checkout '${PR_BRANCH_NAME}' branch (the branch not found or already exists)." |
| 182 | + exit 1 |
| 183 | +fi |
| 184 | + |
| 185 | +AUTHOR="$(git --no-pager show -s --format="%aN <%aE>" HEAD)" |
| 186 | +ORIG_COMMENT="$(git log -1 --pretty=%B)" |
| 187 | + |
| 188 | +echo "Author of pull-request: '$AUTHOR'." |
| 189 | +echo |
| 190 | + |
| 191 | +# Update local target branch. |
| 192 | +git checkout ${TARGET_BRANCH} &> /dev/null |
| 193 | + |
| 194 | +# Take changes. |
| 195 | +git merge --squash "${PR_BRANCH_NAME}" &> /dev/null |
| 196 | +if test $? != 0; then |
| 197 | + git reset --hard &> /dev/null |
| 198 | + |
| 199 | + echo "$0, ERROR:" |
| 200 | + echo >&2 "Could not merge the pull-request to ${TARGET_BRANCH} without conflicts. All local changes have been discarded. You're on ${TARGET_BRANCH} branch." |
| 201 | + exit 1 |
| 202 | +fi |
| 203 | + |
| 204 | +echo "Original comment is" |
| 205 | +echo "\"${ORIG_COMMENT}\"" |
| 206 | +echo "Press [ENTER] if you're agree with the comment or type your comment and press [ENTER]:" |
| 207 | +read -r COMMENT |
| 208 | +echo |
| 209 | + |
| 210 | +if [ "${COMMENT}" == "" ]; then |
| 211 | + COMMENT=${ORIG_COMMENT} |
| 212 | +fi |
| 213 | + |
| 214 | +COMMENT="${COMMENT} - Fixes #${PR_ID}." |
| 215 | + |
| 216 | +if [ "${EXCLUDE_SPECIAL_FILE}" = "true" ]; then |
| 217 | + git checkout HEAD ignite-pull-request-id |
| 218 | +fi |
| 219 | + |
| 220 | +SIGN_OPTION="" |
| 221 | +if [ -n "${WITH_GPG}" ]; then |
| 222 | + SIGN_OPTION="-S" |
| 223 | +fi |
| 224 | + |
| 225 | +if [ -n "${WITH_SIGN_OFF}" ]; then |
| 226 | + SIGN_OPTION="${SIGN_OPTION} -s" |
| 227 | +fi |
| 228 | + |
| 229 | +git commit --author "${AUTHOR}" -a ${SIGN_OPTION} -m "${COMMENT}" &> /dev/null |
| 230 | + |
| 231 | +echo "Squash commit for pull request with id='${PR_ID}' has been added. The commit has been added with comment '${COMMENT}'." |
| 232 | +echo "Now you can review changes of the last commit at ${TARGET_BRANCH} and push it into ${APACHE_GIT} git after." |
| 233 | +echo "If you want to decline changes, you can remove the last commit from your repo by 'git reset --hard HEAD^'." |
| 234 | +echo |
| 235 | + |
| 236 | +# Clean-up. |
| 237 | +git branch -D "${PR_BRANCH_NAME}" &> /dev/null |
| 238 | + |
| 239 | +echo 'Successfully completed.' |
0 commit comments