smart-apply-patch.sh 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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. set -e
  14. PATCH_FILE=$1
  15. DRY_RUN=$2
  16. if [ -z "$PATCH_FILE" ]; then
  17. echo usage: $0 patch-file
  18. exit 1
  19. fi
  20. PATCH=${PATCH:-patch} # allow overriding patch binary
  21. # Cleanup handler for temporary files
  22. TOCLEAN=""
  23. cleanup() {
  24. rm $TOCLEAN
  25. exit $1
  26. }
  27. trap "cleanup 1" HUP INT QUIT TERM
  28. # Allow passing "-" for stdin patches
  29. if [ "$PATCH_FILE" == "-" ]; then
  30. PATCH_FILE=/tmp/tmp.in.$$
  31. cat /dev/fd/0 > $PATCH_FILE
  32. TOCLEAN="$TOCLEAN $PATCH_FILE"
  33. fi
  34. # Was the patch generated by git? If so, we can definitely use 'git apply' to
  35. # apply it. This is nice because it allows us to handle binary files. If
  36. # not, we fall back to applying the patch with "patch", since that's most
  37. # likely what was used to create it.
  38. if git --version &>/dev/null && grep -q -- '^diff --git' "$PATCH_FILE"; then
  39. PATCH_TYPE="git"
  40. PATCH_DRY_RUN="git apply --check"
  41. PATCH_APPLY="git apply"
  42. else
  43. PATCH_TYPE="non-git"
  44. PATCH_DRY_RUN="patch --dry-run -E"
  45. PATCH_APPLY="patch -E"
  46. fi
  47. # Come up with a list of changed files into $TMP
  48. TMP=/tmp/tmp.paths.$$
  49. TOCLEAN="$TOCLEAN $TMP"
  50. # This file contains the error messages from all patch application attempts.
  51. # We only print it when the script fails.
  52. DRY_RUN_ERRORS=/tmp/dry-run-errors.$$
  53. TOCLEAN="$TOCLEAN $DRY_RUN_ERRORS"
  54. touch $DRY_RUN_ERRORS
  55. if $PATCH_DRY_RUN -p0 < $PATCH_FILE 2>>$DRY_RUN_ERRORS 1> $TMP; then
  56. PLEVEL=0
  57. #if the patch applied at P0 there is the possability that all we are doing
  58. # is adding new files and they would apply anywhere. So try to guess the
  59. # correct place to put those files.
  60. TMP2=/tmp/tmp.paths.2.$$
  61. TOCLEAN="$TOCLEAN $TMP2"
  62. egrep '^patching file |^checking file ' $TMP | awk '{print $3}' | grep -v /dev/null | sort | uniq > $TMP2
  63. if [ ! -s $TMP2 ]; then
  64. echo "Error: Patch dryrun couldn't detect changes the patch would make. Exiting."
  65. cleanup 1
  66. fi
  67. #first off check that all of the files do not exist
  68. FOUND_ANY=0
  69. for CHECK_FILE in $(cat -- $TMP2)
  70. do
  71. if [[ -f $CHECK_FILE ]]; then
  72. FOUND_ANY=1
  73. fi
  74. done
  75. if [[ "$FOUND_ANY" = "0" ]]; then
  76. #all of the files are new files so we have to guess where the correct place to put it is.
  77. # if all of the lines start with a/ or b/, then this is a git patch that
  78. # was generated without --no-prefix
  79. if ! grep -qv '^a/\|^b/' $TMP2 ; then
  80. echo Looks like this is a git patch. Stripping a/ and b/ prefixes
  81. echo and incrementing PLEVEL
  82. PLEVEL=$[$PLEVEL + 1]
  83. sed -i -e 's,^[ab]/,,' $TMP2
  84. fi
  85. PREFIX_DIRS_AND_FILES=$(cut -d '/' -f 1 | sort | uniq)
  86. # if we are at the project root then nothing more to do
  87. if [[ -d hadoop-common-project ]]; then
  88. echo Looks like this is being run at project root
  89. # if all of the lines start with hadoop-common/, hadoop-hdfs/, hadoop-yarn/ or hadoop-mapreduce/, this is
  90. # relative to the hadoop root instead of the subproject root, so we need
  91. # to chop off another layer
  92. elif [[ "$PREFIX_DIRS_AND_FILES" =~ ^(hadoop-common-project|hadoop-hdfs-project|hadoop-yarn-project|hadoop-mapreduce-project)$ ]]; then
  93. echo Looks like this is relative to project root. Increasing PLEVEL
  94. PLEVEL=$[$PLEVEL + 1]
  95. elif ! echo "$PREFIX_DIRS_AND_FILES" | grep -vxq 'hadoop-common-project\|hadoop-hdfs-project\|hadoop-yarn-project\|hadoop-mapreduce-project' ; then
  96. echo Looks like this is a cross-subproject patch. Try applying from the project root
  97. cleanup 1
  98. fi
  99. fi
  100. elif $PATCH_DRY_RUN -p1 < $PATCH_FILE 2>>$DRY_RUN_ERRORS 1> /dev/null; then
  101. PLEVEL=1
  102. elif $PATCH_DRY_RUN -p2 < $PATCH_FILE 2>>$DRY_RUN_ERRORS 1> /dev/null; then
  103. PLEVEL=2
  104. else
  105. echo "The ${PATCH_TYPE} patch does not appear to apply with p0 to p2.";
  106. echo
  107. echo "Dry run errors:"
  108. cat -- $DRY_RUN_ERRORS
  109. cleanup 1;
  110. fi
  111. # If this is a dry run then exit instead of applying the patch
  112. if [[ -n $DRY_RUN ]]; then
  113. cleanup 0;
  114. fi
  115. echo Going to apply ${PATCH_TYPE} patch with: ${PATCH_APPLY} -p$PLEVEL
  116. ${PATCH_APPLY} -p$PLEVEL < $PATCH_FILE
  117. cleanup $?