smart-apply-patch.sh 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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. # Come up with a list of changed files into $TMP
  35. TMP=/tmp/tmp.paths.$$
  36. TOCLEAN="$TOCLEAN $TMP"
  37. if $PATCH -p0 -E --dry-run < $PATCH_FILE 2>&1 > $TMP; then
  38. PLEVEL=0
  39. #if the patch applied at P0 there is the possability that all we are doing
  40. # is adding new files and they would apply anywhere. So try to guess the
  41. # correct place to put those files.
  42. TMP2=/tmp/tmp.paths.2.$$
  43. TOCLEAN="$TOCLEAN $TMP2"
  44. egrep '^patching file |^checking file ' $TMP | awk '{print $3}' | grep -v /dev/null | sort | uniq > $TMP2
  45. if [ ! -s $TMP2 ]; then
  46. echo "Error: Patch dryrun couldn't detect changes the patch would make. Exiting."
  47. cleanup 1
  48. fi
  49. #first off check that all of the files do not exist
  50. FOUND_ANY=0
  51. for CHECK_FILE in $(cat $TMP2)
  52. do
  53. if [[ -f $CHECK_FILE ]]; then
  54. FOUND_ANY=1
  55. fi
  56. done
  57. if [[ "$FOUND_ANY" = "0" ]]; then
  58. #all of the files are new files so we have to guess where the correct place to put it is.
  59. # if all of the lines start with a/ or b/, then this is a git patch that
  60. # was generated without --no-prefix
  61. if ! grep -qv '^a/\|^b/' $TMP2 ; then
  62. echo Looks like this is a git patch. Stripping a/ and b/ prefixes
  63. echo and incrementing PLEVEL
  64. PLEVEL=$[$PLEVEL + 1]
  65. sed -i -e 's,^[ab]/,,' $TMP2
  66. fi
  67. PREFIX_DIRS_AND_FILES=$(cut -d '/' -f 1 | sort | uniq)
  68. # if we are at the project root then nothing more to do
  69. if [[ -d hadoop-common-project ]]; then
  70. echo Looks like this is being run at project root
  71. # if all of the lines start with hadoop-common/, hadoop-hdfs/, hadoop-yarn/ or hadoop-mapreduce/, this is
  72. # relative to the hadoop root instead of the subproject root, so we need
  73. # to chop off another layer
  74. elif [[ "$PREFIX_DIRS_AND_FILES" =~ ^(hadoop-common-project|hadoop-hdfs-project|hadoop-yarn-project|hadoop-mapreduce-project)$ ]]; then
  75. echo Looks like this is relative to project root. Increasing PLEVEL
  76. PLEVEL=$[$PLEVEL + 1]
  77. elif ! echo "$PREFIX_DIRS_AND_FILES" | grep -vxq 'hadoop-common-project\|hadoop-hdfs-project\|hadoop-yarn-project\|hadoop-mapreduce-project' ; then
  78. echo Looks like this is a cross-subproject patch. Try applying from the project root
  79. cleanup 1
  80. fi
  81. fi
  82. elif $PATCH -p1 -E --dry-run < $PATCH_FILE 2>&1 > /dev/null; then
  83. PLEVEL=1
  84. elif $PATCH -p2 -E --dry-run < $PATCH_FILE 2>&1 > /dev/null; then
  85. PLEVEL=2
  86. else
  87. echo "The patch does not appear to apply with p0 to p2";
  88. cleanup 1;
  89. fi
  90. # If this is a dry run then exit instead of applying the patch
  91. if [[ -n $DRY_RUN ]]; then
  92. cleanup 0;
  93. fi
  94. echo Going to apply patch with: $PATCH -p$PLEVEL
  95. $PATCH -p$PLEVEL -E < $PATCH_FILE
  96. cleanup $?