smart-apply-patch.sh 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. #!/usr/bin/env bash
  2. # Licensed under the Apache License, Version 2.0 (the "License");
  3. # you may not use this file except in compliance with the License.
  4. # You may obtain a copy of the License at
  5. #
  6. # http://www.apache.org/licenses/LICENSE-2.0
  7. #
  8. # Unless required by applicable law or agreed to in writing, software
  9. # distributed under the License is distributed on an "AS IS" BASIS,
  10. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  11. # See the License for the specific language governing permissions and
  12. # limitations under the License.
  13. #
  14. # Determine if the git diff patch file has prefixes.
  15. # These files are generated via "git diff" *without* the --no-prefix option.
  16. #
  17. # We can apply these patches more easily because we know that the a/ and b/
  18. # prefixes in the "diff" lines stands for the project root directory.
  19. # So we don't have to hunt for the project root.
  20. # And of course, we know that the patch file was generated using git, so we
  21. # know git apply can handle it properly.
  22. #
  23. # Arguments: git diff file name.
  24. # Return: 0 if it is a git diff with prefix; 1 otherwise.
  25. #
  26. has_prefix() {
  27. awk '/^diff --git / { if ($3 !~ "^a/" || $4 !~ "^b/") { exit 1 } }
  28. /^\+{3}|-{3} / { if ($2 !~ "^[ab]/" && $2 !~ "^/dev/null") { exit 1 } }' "$1"
  29. return $?
  30. }
  31. PATCH_FILE=$1
  32. DRY_RUN=$2
  33. if [ -z "$PATCH_FILE" ]; then
  34. echo usage: $0 patch-file
  35. exit 1
  36. fi
  37. TMPDIR=${TMPDIR:-/tmp}
  38. PATCH=${PATCH:-patch} # allow overriding patch binary
  39. # Cleanup handler for temporary files
  40. TOCLEAN=""
  41. cleanup() {
  42. rm $TOCLEAN
  43. exit $1
  44. }
  45. trap "cleanup 1" HUP INT QUIT TERM
  46. # Allow passing "-" for stdin patches
  47. if [ "$PATCH_FILE" == "-" ]; then
  48. PATCH_FILE="$TMPDIR/smart-apply.in.$RANDOM"
  49. cat /dev/fd/0 > $PATCH_FILE
  50. TOCLEAN="$TOCLEAN $PATCH_FILE"
  51. fi
  52. ISSUE_RE='^(HADOOP|YARN|MAPREDUCE|HDFS)-[0-9]+$'
  53. if [[ ${PATCH_FILE} =~ ^http || ${PATCH_FILE} =~ ${ISSUE_RE} ]]; then
  54. # Allow downloading of patches
  55. PFILE="$TMPDIR/smart-apply.in.$RANDOM"
  56. TOCLEAN="$TOCLEAN $PFILE"
  57. if [[ ${PATCH_FILE} =~ ^http ]]; then
  58. patchURL="${PATCH_FILE}"
  59. else # Get URL of patch from JIRA
  60. wget -q -O "${PFILE}" "http://issues.apache.org/jira/browse/${PATCH_FILE}"
  61. if [[ $? != 0 ]]; then
  62. echo "Unable to determine what ${PATCH_FILE} may reference." 1>&2
  63. cleanup 1
  64. elif [[ $(grep -c 'Patch Available' "${PFILE}") == 0 ]]; then
  65. echo "${PATCH_FILE} is not \"Patch Available\". Exiting." 1>&2
  66. cleanup 1
  67. fi
  68. relativePatchURL=$(grep -o '"/jira/secure/attachment/[0-9]*/[^"]*' "${PFILE}" | grep -v -e 'htm[l]*$' | sort | tail -1 | grep -o '/jira/secure/attachment/[0-9]*/[^"]*')
  69. patchURL="http://issues.apache.org${relativePatchURL}"
  70. fi
  71. if [[ -n $DRY_RUN ]]; then
  72. echo "Downloading ${patchURL}"
  73. fi
  74. wget -q -O "${PFILE}" "${patchURL}"
  75. if [[ $? != 0 ]]; then
  76. echo "${PATCH_FILE} could not be downloaded." 1>&2
  77. cleanup 1
  78. fi
  79. PATCH_FILE="${PFILE}"
  80. fi
  81. # Case for git-diff patches
  82. if grep -q "^diff --git" "${PATCH_FILE}"; then
  83. GIT_FLAGS="--binary -v"
  84. if has_prefix "$PATCH_FILE"; then
  85. GIT_FLAGS="$GIT_FLAGS -p1"
  86. else
  87. GIT_FLAGS="$GIT_FLAGS -p0"
  88. fi
  89. if [[ -z $DRY_RUN ]]; then
  90. GIT_FLAGS="$GIT_FLAGS --stat --apply"
  91. echo Going to apply git patch with: git apply "${GIT_FLAGS}"
  92. else
  93. GIT_FLAGS="$GIT_FLAGS --check"
  94. fi
  95. # shellcheck disable=SC2086
  96. git apply ${GIT_FLAGS} "${PATCH_FILE}"
  97. if [[ $? == 0 ]]; then
  98. cleanup 0
  99. fi
  100. echo "git apply failed. Going to apply the patch with: ${PATCH}"
  101. fi
  102. # Come up with a list of changed files into $TMP
  103. TMP="$TMPDIR/smart-apply.paths.$RANDOM"
  104. TOCLEAN="$TOCLEAN $TMP"
  105. if $PATCH -p0 -E --dry-run < $PATCH_FILE 2>&1 > $TMP; then
  106. PLEVEL=0
  107. #if the patch applied at P0 there is the possability that all we are doing
  108. # is adding new files and they would apply anywhere. So try to guess the
  109. # correct place to put those files.
  110. TMP2="$TMPDIR/smart-apply.paths.2.$RANDOM"
  111. TOCLEAN="$TOCLEAN $TMP2"
  112. egrep '^patching file |^checking file ' $TMP | awk '{print $3}' | grep -v /dev/null | sort -u > $TMP2
  113. if [ ! -s $TMP2 ]; then
  114. echo "Error: Patch dryrun couldn't detect changes the patch would make. Exiting."
  115. cleanup 1
  116. fi
  117. #first off check that all of the files do not exist
  118. FOUND_ANY=0
  119. for CHECK_FILE in $(cat $TMP2)
  120. do
  121. if [[ -f $CHECK_FILE ]]; then
  122. FOUND_ANY=1
  123. fi
  124. done
  125. if [[ "$FOUND_ANY" = "0" ]]; then
  126. #all of the files are new files so we have to guess where the correct place to put it is.
  127. # if all of the lines start with a/ or b/, then this is a git patch that
  128. # was generated without --no-prefix
  129. if ! grep -qv '^a/\|^b/' $TMP2 ; then
  130. echo Looks like this is a git patch. Stripping a/ and b/ prefixes
  131. echo and incrementing PLEVEL
  132. PLEVEL=$[$PLEVEL + 1]
  133. sed -i -e 's,^[ab]/,,' $TMP2
  134. fi
  135. PREFIX_DIRS_AND_FILES=$(cut -d '/' -f 1 $TMP2 | sort -u)
  136. # if we are at the project root then nothing more to do
  137. if [[ -d hadoop-common-project ]]; then
  138. echo Looks like this is being run at project root
  139. # if all of the lines start with hadoop-common/, hadoop-hdfs/, hadoop-yarn/ or hadoop-mapreduce/, this is
  140. # relative to the hadoop root instead of the subproject root, so we need
  141. # to chop off another layer
  142. elif [[ "$PREFIX_DIRS_AND_FILES" =~ ^(hadoop-common-project|hadoop-hdfs-project|hadoop-yarn-project|hadoop-mapreduce-project)$ ]]; then
  143. echo Looks like this is relative to project root. Increasing PLEVEL
  144. PLEVEL=$[$PLEVEL + 1]
  145. elif ! echo "$PREFIX_DIRS_AND_FILES" | grep -vxq 'hadoop-common-project\|hadoop-hdfs-project\|hadoop-yarn-project\|hadoop-mapreduce-project' ; then
  146. echo Looks like this is a cross-subproject patch. Try applying from the project root
  147. cleanup 1
  148. fi
  149. fi
  150. elif $PATCH -p1 -E --dry-run < $PATCH_FILE 2>&1 > /dev/null; then
  151. PLEVEL=1
  152. elif $PATCH -p2 -E --dry-run < $PATCH_FILE 2>&1 > /dev/null; then
  153. PLEVEL=2
  154. else
  155. echo "The patch does not appear to apply with p0 to p2";
  156. cleanup 1;
  157. fi
  158. # If this is a dry run then exit instead of applying the patch
  159. if [[ -n $DRY_RUN ]]; then
  160. cleanup 0;
  161. fi
  162. echo Going to apply patch with: $PATCH -p$PLEVEL
  163. $PATCH -p$PLEVEL -E < $PATCH_FILE
  164. cleanup $?