2011-06-14 15:05:39 +00:00
|
|
|
#!/usr/bin/env bash
|
|
|
|
# Licensed 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.
|
|
|
|
|
2014-11-24 19:50:38 +00:00
|
|
|
#
|
|
|
|
# Determine if the patch file is a git diff file with prefixes.
|
|
|
|
# These files are generated via "git diff" *without* the --no-prefix option.
|
|
|
|
#
|
|
|
|
# We can apply these patches more easily because we know that the a/ and b/
|
|
|
|
# prefixes in the "diff" lines stands for the project root directory.
|
|
|
|
# So we don't have to hunt for the project root.
|
|
|
|
# And of course, we know that the patch file was generated using git, so we
|
|
|
|
# know git apply can handle it properly.
|
|
|
|
#
|
|
|
|
# Arguments: file name.
|
|
|
|
# Return: 0 if it is a git diff; 1 otherwise.
|
|
|
|
#
|
|
|
|
is_git_diff_with_prefix() {
|
|
|
|
DIFF_TYPE="unknown"
|
|
|
|
while read -r line; do
|
|
|
|
if [[ "$line" =~ ^diff\ ]]; then
|
|
|
|
if [[ "$line" =~ ^diff\ \-\-git ]]; then
|
|
|
|
DIFF_TYPE="git"
|
|
|
|
else
|
|
|
|
return 1 # All diff lines must be diff --git lines.
|
|
|
|
fi
|
|
|
|
fi
|
|
|
|
if [[ "$line" =~ ^\+\+\+\ ]] ||
|
|
|
|
[[ "$line" =~ ^\-\-\-\ ]]; then
|
2015-02-17 19:13:47 +00:00
|
|
|
if ! [[ "$line" =~ ^....[ab]/ || "$line" =~ ^..../dev/null ]]; then
|
|
|
|
return 1 # All +++ and --- lines must start with a/ or b/ or be /dev/null.
|
2014-11-24 19:50:38 +00:00
|
|
|
fi
|
|
|
|
fi
|
|
|
|
done < $1
|
|
|
|
[ x$DIFF_TYPE == x"git" ] || return 1
|
|
|
|
return 0 # return true (= 0 in bash)
|
|
|
|
}
|
|
|
|
|
2011-06-14 15:05:39 +00:00
|
|
|
PATCH_FILE=$1
|
2012-07-06 20:04:51 +00:00
|
|
|
DRY_RUN=$2
|
2011-06-14 15:05:39 +00:00
|
|
|
if [ -z "$PATCH_FILE" ]; then
|
|
|
|
echo usage: $0 patch-file
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
|
2015-04-08 17:05:25 +00:00
|
|
|
TMPDIR=${TMPDIR:-/tmp}
|
2011-06-14 15:05:39 +00:00
|
|
|
PATCH=${PATCH:-patch} # allow overriding patch binary
|
|
|
|
|
|
|
|
# Cleanup handler for temporary files
|
|
|
|
TOCLEAN=""
|
|
|
|
cleanup() {
|
|
|
|
rm $TOCLEAN
|
|
|
|
exit $1
|
|
|
|
}
|
|
|
|
trap "cleanup 1" HUP INT QUIT TERM
|
|
|
|
|
|
|
|
# Allow passing "-" for stdin patches
|
|
|
|
if [ "$PATCH_FILE" == "-" ]; then
|
2015-04-08 17:05:25 +00:00
|
|
|
PATCH_FILE="$TMPDIR/smart-apply.in.$RANDOM"
|
2011-06-14 15:05:39 +00:00
|
|
|
cat /dev/fd/0 > $PATCH_FILE
|
|
|
|
TOCLEAN="$TOCLEAN $PATCH_FILE"
|
|
|
|
fi
|
|
|
|
|
2015-04-08 17:05:25 +00:00
|
|
|
ISSUE_RE='^(HADOOP|YARN|MAPREDUCE|HDFS)-[0-9]+$'
|
|
|
|
if [[ ${PATCH_FILE} =~ ^http || ${PATCH_FILE} =~ ${ISSUE_RE} ]]; then
|
|
|
|
# Allow downloading of patches
|
|
|
|
PFILE="$TMPDIR/smart-apply.in.$RANDOM"
|
|
|
|
TOCLEAN="$TOCLEAN $PFILE"
|
|
|
|
if [[ ${PATCH_FILE} =~ ^http ]]; then
|
|
|
|
patchURL="${PATCH_FILE}"
|
|
|
|
else # Get URL of patch from JIRA
|
|
|
|
wget -q -O "${PFILE}" "http://issues.apache.org/jira/browse/${PATCH_FILE}"
|
|
|
|
if [[ $? != 0 ]]; then
|
|
|
|
echo "Unable to determine what ${PATCH_FILE} may reference." 1>&2
|
|
|
|
cleanup 1
|
|
|
|
elif [[ $(grep -c 'Patch Available' "${PFILE}") == 0 ]]; then
|
|
|
|
echo "${PATCH_FILE} is not \"Patch Available\". Exiting." 1>&2
|
|
|
|
cleanup 1
|
|
|
|
fi
|
|
|
|
relativePatchURL=$(grep -o '"/jira/secure/attachment/[0-9]*/[^"]*' "${PFILE}" | grep -v -e 'htm[l]*$' | sort | tail -1 | grep -o '/jira/secure/attachment/[0-9]*/[^"]*')
|
|
|
|
patchURL="http://issues.apache.org${relativePatchURL}"
|
|
|
|
fi
|
|
|
|
if [[ -n $DRY_RUN ]]; then
|
|
|
|
echo "Downloading ${patchURL}"
|
|
|
|
fi
|
|
|
|
wget -q -O "${PFILE}" "${patchURL}"
|
|
|
|
if [[ $? != 0 ]]; then
|
|
|
|
echo "${PATCH_FILE} could not be downloaded." 1>&2
|
|
|
|
cleanup 1
|
|
|
|
fi
|
|
|
|
PATCH_FILE="${PFILE}"
|
|
|
|
fi
|
|
|
|
|
2014-11-24 19:50:38 +00:00
|
|
|
# Special case for git-diff patches without --no-prefix
|
|
|
|
if is_git_diff_with_prefix "$PATCH_FILE"; then
|
|
|
|
GIT_FLAGS="--binary -p1 -v"
|
|
|
|
if [[ -z $DRY_RUN ]]; then
|
|
|
|
GIT_FLAGS="$GIT_FLAGS --stat --apply "
|
|
|
|
echo Going to apply git patch with: git apply "${GIT_FLAGS}"
|
|
|
|
else
|
|
|
|
GIT_FLAGS="$GIT_FLAGS --check "
|
|
|
|
fi
|
|
|
|
git apply ${GIT_FLAGS} "${PATCH_FILE}"
|
|
|
|
exit $?
|
|
|
|
fi
|
|
|
|
|
2011-06-14 15:05:39 +00:00
|
|
|
# Come up with a list of changed files into $TMP
|
2015-04-08 17:05:25 +00:00
|
|
|
TMP="$TMPDIR/smart-apply.paths.$RANDOM"
|
2011-06-14 15:05:39 +00:00
|
|
|
TOCLEAN="$TOCLEAN $TMP"
|
|
|
|
|
2014-10-28 09:58:01 +00:00
|
|
|
if $PATCH -p0 -E --dry-run < $PATCH_FILE 2>&1 > $TMP; then
|
2011-09-08 21:31:46 +00:00
|
|
|
PLEVEL=0
|
|
|
|
#if the patch applied at P0 there is the possability that all we are doing
|
|
|
|
# is adding new files and they would apply anywhere. So try to guess the
|
|
|
|
# correct place to put those files.
|
2011-08-11 16:49:59 +00:00
|
|
|
|
2015-04-08 17:05:25 +00:00
|
|
|
TMP2="$TMPDIR/smart-apply.paths.2.$RANDOM"
|
2011-09-08 21:31:46 +00:00
|
|
|
TOCLEAN="$TOCLEAN $TMP2"
|
2011-08-11 16:49:59 +00:00
|
|
|
|
2015-04-08 17:05:25 +00:00
|
|
|
egrep '^patching file |^checking file ' $TMP | awk '{print $3}' | grep -v /dev/null | sort -u > $TMP2
|
2013-06-03 20:00:16 +00:00
|
|
|
|
|
|
|
if [ ! -s $TMP2 ]; then
|
|
|
|
echo "Error: Patch dryrun couldn't detect changes the patch would make. Exiting."
|
|
|
|
cleanup 1
|
|
|
|
fi
|
2011-06-14 15:05:39 +00:00
|
|
|
|
2011-09-08 21:31:46 +00:00
|
|
|
#first off check that all of the files do not exist
|
|
|
|
FOUND_ANY=0
|
2014-10-28 09:58:01 +00:00
|
|
|
for CHECK_FILE in $(cat $TMP2)
|
2011-09-08 21:31:46 +00:00
|
|
|
do
|
|
|
|
if [[ -f $CHECK_FILE ]]; then
|
|
|
|
FOUND_ANY=1
|
|
|
|
fi
|
|
|
|
done
|
2011-08-11 16:49:59 +00:00
|
|
|
|
2011-09-08 21:31:46 +00:00
|
|
|
if [[ "$FOUND_ANY" = "0" ]]; then
|
|
|
|
#all of the files are new files so we have to guess where the correct place to put it is.
|
|
|
|
|
|
|
|
# if all of the lines start with a/ or b/, then this is a git patch that
|
|
|
|
# was generated without --no-prefix
|
|
|
|
if ! grep -qv '^a/\|^b/' $TMP2 ; then
|
|
|
|
echo Looks like this is a git patch. Stripping a/ and b/ prefixes
|
|
|
|
echo and incrementing PLEVEL
|
|
|
|
PLEVEL=$[$PLEVEL + 1]
|
|
|
|
sed -i -e 's,^[ab]/,,' $TMP2
|
|
|
|
fi
|
|
|
|
|
2015-04-08 17:05:25 +00:00
|
|
|
PREFIX_DIRS_AND_FILES=$(cut -d '/' -f 1 $TMP2 | sort -u)
|
|
|
|
|
2011-09-08 21:31:46 +00:00
|
|
|
# if we are at the project root then nothing more to do
|
|
|
|
if [[ -d hadoop-common-project ]]; then
|
|
|
|
echo Looks like this is being run at project root
|
|
|
|
|
2012-08-08 05:22:27 +00:00
|
|
|
# if all of the lines start with hadoop-common/, hadoop-hdfs/, hadoop-yarn/ or hadoop-mapreduce/, this is
|
2011-09-08 21:31:46 +00:00
|
|
|
# relative to the hadoop root instead of the subproject root, so we need
|
|
|
|
# to chop off another layer
|
2012-08-08 05:22:27 +00:00
|
|
|
elif [[ "$PREFIX_DIRS_AND_FILES" =~ ^(hadoop-common-project|hadoop-hdfs-project|hadoop-yarn-project|hadoop-mapreduce-project)$ ]]; then
|
2011-09-08 21:31:46 +00:00
|
|
|
|
|
|
|
echo Looks like this is relative to project root. Increasing PLEVEL
|
|
|
|
PLEVEL=$[$PLEVEL + 1]
|
|
|
|
|
2012-08-08 05:22:27 +00:00
|
|
|
elif ! echo "$PREFIX_DIRS_AND_FILES" | grep -vxq 'hadoop-common-project\|hadoop-hdfs-project\|hadoop-yarn-project\|hadoop-mapreduce-project' ; then
|
2011-09-08 21:31:46 +00:00
|
|
|
echo Looks like this is a cross-subproject patch. Try applying from the project root
|
|
|
|
cleanup 1
|
|
|
|
fi
|
|
|
|
fi
|
2014-10-28 09:58:01 +00:00
|
|
|
elif $PATCH -p1 -E --dry-run < $PATCH_FILE 2>&1 > /dev/null; then
|
2011-09-08 21:31:46 +00:00
|
|
|
PLEVEL=1
|
2014-10-28 09:58:01 +00:00
|
|
|
elif $PATCH -p2 -E --dry-run < $PATCH_FILE 2>&1 > /dev/null; then
|
2011-09-08 21:31:46 +00:00
|
|
|
PLEVEL=2
|
|
|
|
else
|
2014-10-28 09:58:01 +00:00
|
|
|
echo "The patch does not appear to apply with p0 to p2";
|
2011-09-08 21:31:46 +00:00
|
|
|
cleanup 1;
|
2011-06-14 15:05:39 +00:00
|
|
|
fi
|
|
|
|
|
2012-07-06 20:04:51 +00:00
|
|
|
# If this is a dry run then exit instead of applying the patch
|
|
|
|
if [[ -n $DRY_RUN ]]; then
|
|
|
|
cleanup 0;
|
|
|
|
fi
|
|
|
|
|
2014-10-28 09:58:01 +00:00
|
|
|
echo Going to apply patch with: $PATCH -p$PLEVEL
|
|
|
|
$PATCH -p$PLEVEL -E < $PATCH_FILE
|
2011-06-14 15:05:39 +00:00
|
|
|
|
2011-09-08 21:31:46 +00:00
|
|
|
cleanup $?
|