smart-apply-patch.sh 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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. if [ -z "$PATCH_FILE" ]; then
  16. echo usage: $0 patch-file
  17. exit 1
  18. fi
  19. PATCH=${PATCH:-patch} # allow overriding patch binary
  20. # Cleanup handler for temporary files
  21. TOCLEAN=""
  22. cleanup() {
  23. rm $TOCLEAN
  24. exit $1
  25. }
  26. trap "cleanup 1" HUP INT QUIT TERM
  27. # Allow passing "-" for stdin patches
  28. if [ "$PATCH_FILE" == "-" ]; then
  29. PATCH_FILE=/tmp/tmp.in.$$
  30. cat /dev/fd/0 > $PATCH_FILE
  31. TOCLEAN="$TOCLEAN $PATCH_FILE"
  32. fi
  33. # Come up with a list of changed files into $TMP
  34. TMP=/tmp/tmp.paths.$$
  35. TOCLEAN="$TOCLEAN $TMP"
  36. if $PATCH -p0 -E --dry-run < $PATCH_FILE 2>&1 > $TMP; then
  37. PLEVEL=0
  38. #if the patch applied at P0 there is the possability that all we are doing
  39. # is adding new files and they would apply anywhere. So try to guess the
  40. # correct place to put those files.
  41. TMP2=/tmp/tmp.paths.2.$$
  42. TOCLEAN="$TOCLEAN $TMP2"
  43. grep '^patching file ' $TMP | awk '{print $3}' | grep -v /dev/null | sort | uniq > $TMP2
  44. #first off check that all of the files do not exist
  45. FOUND_ANY=0
  46. for CHECK_FILE in $(cat $TMP2)
  47. do
  48. if [[ -f $CHECK_FILE ]]; then
  49. FOUND_ANY=1
  50. fi
  51. done
  52. if [[ "$FOUND_ANY" = "0" ]]; then
  53. #all of the files are new files so we have to guess where the correct place to put it is.
  54. # if all of the lines start with a/ or b/, then this is a git patch that
  55. # was generated without --no-prefix
  56. if ! grep -qv '^a/\|^b/' $TMP2 ; then
  57. echo Looks like this is a git patch. Stripping a/ and b/ prefixes
  58. echo and incrementing PLEVEL
  59. PLEVEL=$[$PLEVEL + 1]
  60. sed -i -e 's,^[ab]/,,' $TMP2
  61. fi
  62. PREFIX_DIRS_AND_FILES=$(cut -d '/' -f 1 | sort | uniq)
  63. # if we are at the project root then nothing more to do
  64. if [[ -d hadoop-common-project ]]; then
  65. echo Looks like this is being run at project root
  66. # if all of the lines start with hadoop-common/, hadoop-hdfs/, hadoop-yarn/ or hadoop-mapreduce/, this is
  67. # relative to the hadoop root instead of the subproject root, so we need
  68. # to chop off another layer
  69. elif [[ "$PREFIX_DIRS_AND_FILES" =~ ^(hadoop-common-project|hadoop-hdfs-project|hadoop-yarn-project|hadoop-mapreduce-project)$ ]]; then
  70. echo Looks like this is relative to project root. Increasing PLEVEL
  71. PLEVEL=$[$PLEVEL + 1]
  72. elif ! echo "$PREFIX_DIRS_AND_FILES" | grep -vxq 'hadoop-common-project\|hadoop-hdfs-project\|hadoop-yarn-project\|hadoop-mapreduce-project' ; then
  73. echo Looks like this is a cross-subproject patch. Try applying from the project root
  74. cleanup 1
  75. fi
  76. fi
  77. elif $PATCH -p1 -E --dry-run < $PATCH_FILE 2>&1 > /dev/null; then
  78. PLEVEL=1
  79. elif $PATCH -p2 -E --dry-run < $PATCH_FILE 2>&1 > /dev/null; then
  80. PLEVEL=2
  81. else
  82. echo "The patch does not appear to apply with p0 to p2";
  83. cleanup 1;
  84. fi
  85. echo Going to apply patch with: $PATCH -p$PLEVEL
  86. $PATCH -p$PLEVEL -E < $PATCH_FILE
  87. cleanup $?