smart-apply-patch.sh 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  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 patch file is a git diff file with 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: file name.
  24. # Return: 0 if it is a git diff; 1 otherwise.
  25. #
  26. is_git_diff_with_prefix() {
  27. DIFF_TYPE="unknown"
  28. while read -r line; do
  29. if [[ "$line" =~ ^diff\ ]]; then
  30. if [[ "$line" =~ ^diff\ \-\-git ]]; then
  31. DIFF_TYPE="git"
  32. else
  33. return 1 # All diff lines must be diff --git lines.
  34. fi
  35. fi
  36. if [[ "$line" =~ ^\+\+\+\ ]] ||
  37. [[ "$line" =~ ^\-\-\-\ ]]; then
  38. if ! [[ "$line" =~ ^....[ab]/ || "$line" =~ ^..../dev/null ]]; then
  39. return 1 # All +++ and --- lines must start with a/ or b/ or be /dev/null.
  40. fi
  41. fi
  42. done < $1
  43. [ x$DIFF_TYPE == x"git" ] || return 1
  44. return 0 # return true (= 0 in bash)
  45. }
  46. PATCH_FILE=$1
  47. DRY_RUN=$2
  48. if [ -z "$PATCH_FILE" ]; then
  49. echo usage: $0 patch-file
  50. exit 1
  51. fi
  52. TMPDIR=${TMPDIR:-/tmp}
  53. PATCH=${PATCH:-patch} # allow overriding patch binary
  54. # Cleanup handler for temporary files
  55. TOCLEAN=""
  56. cleanup() {
  57. rm $TOCLEAN
  58. exit $1
  59. }
  60. trap "cleanup 1" HUP INT QUIT TERM
  61. # Allow passing "-" for stdin patches
  62. if [ "$PATCH_FILE" == "-" ]; then
  63. PATCH_FILE="$TMPDIR/smart-apply.in.$RANDOM"
  64. cat /dev/fd/0 > $PATCH_FILE
  65. TOCLEAN="$TOCLEAN $PATCH_FILE"
  66. fi
  67. ISSUE_RE='^(HADOOP|YARN|MAPREDUCE|HDFS)-[0-9]+$'
  68. if [[ ${PATCH_FILE} =~ ^http || ${PATCH_FILE} =~ ${ISSUE_RE} ]]; then
  69. # Allow downloading of patches
  70. PFILE="$TMPDIR/smart-apply.in.$RANDOM"
  71. TOCLEAN="$TOCLEAN $PFILE"
  72. if [[ ${PATCH_FILE} =~ ^http ]]; then
  73. patchURL="${PATCH_FILE}"
  74. else # Get URL of patch from JIRA
  75. wget -q -O "${PFILE}" "http://issues.apache.org/jira/browse/${PATCH_FILE}"
  76. if [[ $? != 0 ]]; then
  77. echo "Unable to determine what ${PATCH_FILE} may reference." 1>&2
  78. cleanup 1
  79. elif [[ $(grep -c 'Patch Available' "${PFILE}") == 0 ]]; then
  80. echo "${PATCH_FILE} is not \"Patch Available\". Exiting." 1>&2
  81. cleanup 1
  82. fi
  83. relativePatchURL=$(grep -o '"/jira/secure/attachment/[0-9]*/[^"]*' "${PFILE}" | grep -v -e 'htm[l]*$' | sort | tail -1 | grep -o '/jira/secure/attachment/[0-9]*/[^"]*')
  84. patchURL="http://issues.apache.org${relativePatchURL}"
  85. fi
  86. if [[ -n $DRY_RUN ]]; then
  87. echo "Downloading ${patchURL}"
  88. fi
  89. wget -q -O "${PFILE}" "${patchURL}"
  90. if [[ $? != 0 ]]; then
  91. echo "${PATCH_FILE} could not be downloaded." 1>&2
  92. cleanup 1
  93. fi
  94. PATCH_FILE="${PFILE}"
  95. fi
  96. # Special case for git-diff patches without --no-prefix
  97. if is_git_diff_with_prefix "$PATCH_FILE"; then
  98. GIT_FLAGS="--binary -p1 -v"
  99. if [[ -z $DRY_RUN ]]; then
  100. GIT_FLAGS="$GIT_FLAGS --stat --apply "
  101. echo Going to apply git patch with: git apply "${GIT_FLAGS}"
  102. else
  103. GIT_FLAGS="$GIT_FLAGS --check "
  104. fi
  105. git apply ${GIT_FLAGS} "${PATCH_FILE}"
  106. exit $?
  107. fi
  108. # Come up with a list of changed files into $TMP
  109. TMP="$TMPDIR/smart-apply.paths.$RANDOM"
  110. TOCLEAN="$TOCLEAN $TMP"
  111. if $PATCH -p0 -E --dry-run < $PATCH_FILE 2>&1 > $TMP; then
  112. PLEVEL=0
  113. #if the patch applied at P0 there is the possability that all we are doing
  114. # is adding new files and they would apply anywhere. So try to guess the
  115. # correct place to put those files.
  116. TMP2="$TMPDIR/smart-apply.paths.2.$RANDOM"
  117. TOCLEAN="$TOCLEAN $TMP2"
  118. egrep '^patching file |^checking file ' $TMP | awk '{print $3}' | grep -v /dev/null | sort -u > $TMP2
  119. if [ ! -s $TMP2 ]; then
  120. echo "Error: Patch dryrun couldn't detect changes the patch would make. Exiting."
  121. cleanup 1
  122. fi
  123. #first off check that all of the files do not exist
  124. FOUND_ANY=0
  125. for CHECK_FILE in $(cat $TMP2)
  126. do
  127. if [[ -f $CHECK_FILE ]]; then
  128. FOUND_ANY=1
  129. fi
  130. done
  131. if [[ "$FOUND_ANY" = "0" ]]; then
  132. #all of the files are new files so we have to guess where the correct place to put it is.
  133. # if all of the lines start with a/ or b/, then this is a git patch that
  134. # was generated without --no-prefix
  135. if ! grep -qv '^a/\|^b/' $TMP2 ; then
  136. echo Looks like this is a git patch. Stripping a/ and b/ prefixes
  137. echo and incrementing PLEVEL
  138. PLEVEL=$[$PLEVEL + 1]
  139. sed -i -e 's,^[ab]/,,' $TMP2
  140. fi
  141. PREFIX_DIRS_AND_FILES=$(cut -d '/' -f 1 $TMP2 | sort -u)
  142. # if we are at the project root then nothing more to do
  143. if [[ -d hadoop-common-project ]]; then
  144. echo Looks like this is being run at project root
  145. # if all of the lines start with hadoop-common/, hadoop-hdfs/, hadoop-yarn/ or hadoop-mapreduce/, this is
  146. # relative to the hadoop root instead of the subproject root, so we need
  147. # to chop off another layer
  148. elif [[ "$PREFIX_DIRS_AND_FILES" =~ ^(hadoop-common-project|hadoop-hdfs-project|hadoop-yarn-project|hadoop-mapreduce-project)$ ]]; then
  149. echo Looks like this is relative to project root. Increasing PLEVEL
  150. PLEVEL=$[$PLEVEL + 1]
  151. elif ! echo "$PREFIX_DIRS_AND_FILES" | grep -vxq 'hadoop-common-project\|hadoop-hdfs-project\|hadoop-yarn-project\|hadoop-mapreduce-project' ; then
  152. echo Looks like this is a cross-subproject patch. Try applying from the project root
  153. cleanup 1
  154. fi
  155. fi
  156. elif $PATCH -p1 -E --dry-run < $PATCH_FILE 2>&1 > /dev/null; then
  157. PLEVEL=1
  158. elif $PATCH -p2 -E --dry-run < $PATCH_FILE 2>&1 > /dev/null; then
  159. PLEVEL=2
  160. else
  161. echo "The patch does not appear to apply with p0 to p2";
  162. cleanup 1;
  163. fi
  164. # If this is a dry run then exit instead of applying the patch
  165. if [[ -n $DRY_RUN ]]; then
  166. cleanup 0;
  167. fi
  168. echo Going to apply patch with: $PATCH -p$PLEVEL
  169. $PATCH -p$PLEVEL -E < $PATCH_FILE
  170. cleanup $?