smart-apply-patch.sh 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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. #
  15. # Determine if the patch file is a git diff file with prefixes.
  16. # These files are generated via "git diff" *without* the --no-prefix option.
  17. #
  18. # We can apply these patches more easily because we know that the a/ and b/
  19. # prefixes in the "diff" lines stands for the project root directory.
  20. # So we don't have to hunt for the project root.
  21. # And of course, we know that the patch file was generated using git, so we
  22. # know git apply can handle it properly.
  23. #
  24. # Arguments: file name.
  25. # Return: 0 if it is a git diff; 1 otherwise.
  26. #
  27. is_git_diff_with_prefix() {
  28. DIFF_TYPE="unknown"
  29. while read -r line; do
  30. if [[ "$line" =~ ^diff\ ]]; then
  31. if [[ "$line" =~ ^diff\ \-\-git ]]; then
  32. DIFF_TYPE="git"
  33. else
  34. return 1 # All diff lines must be diff --git lines.
  35. fi
  36. fi
  37. if [[ "$line" =~ ^\+\+\+\ ]] ||
  38. [[ "$line" =~ ^\-\-\-\ ]]; then
  39. if ! [[ "$line" =~ ^....[ab]/ ]]; then
  40. return 1 # All +++ and --- lines must start with a/ or b/.
  41. fi
  42. fi
  43. done < $1
  44. [ x$DIFF_TYPE == x"git" ] || return 1
  45. return 0 # return true (= 0 in bash)
  46. }
  47. PATCH_FILE=$1
  48. DRY_RUN=$2
  49. if [ -z "$PATCH_FILE" ]; then
  50. echo usage: $0 patch-file
  51. exit 1
  52. fi
  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=/tmp/tmp.in.$$
  64. cat /dev/fd/0 > $PATCH_FILE
  65. TOCLEAN="$TOCLEAN $PATCH_FILE"
  66. fi
  67. # Special case for git-diff patches without --no-prefix
  68. if is_git_diff_with_prefix "$PATCH_FILE"; then
  69. GIT_FLAGS="--binary -p1 -v"
  70. if [[ -z $DRY_RUN ]]; then
  71. GIT_FLAGS="$GIT_FLAGS --stat --apply "
  72. echo Going to apply git patch with: git apply "${GIT_FLAGS}"
  73. else
  74. GIT_FLAGS="$GIT_FLAGS --check "
  75. fi
  76. git apply ${GIT_FLAGS} "${PATCH_FILE}"
  77. exit $?
  78. fi
  79. # Come up with a list of changed files into $TMP
  80. TMP=/tmp/tmp.paths.$$
  81. TOCLEAN="$TOCLEAN $TMP"
  82. if $PATCH -p0 -E --dry-run < $PATCH_FILE 2>&1 > $TMP; then
  83. PLEVEL=0
  84. #if the patch applied at P0 there is the possability that all we are doing
  85. # is adding new files and they would apply anywhere. So try to guess the
  86. # correct place to put those files.
  87. TMP2=/tmp/tmp.paths.2.$$
  88. TOCLEAN="$TOCLEAN $TMP2"
  89. egrep '^patching file |^checking file ' $TMP | awk '{print $3}' | grep -v /dev/null | sort | uniq > $TMP2
  90. if [ ! -s $TMP2 ]; then
  91. echo "Error: Patch dryrun couldn't detect changes the patch would make. Exiting."
  92. cleanup 1
  93. fi
  94. #first off check that all of the files do not exist
  95. FOUND_ANY=0
  96. for CHECK_FILE in $(cat $TMP2)
  97. do
  98. if [[ -f $CHECK_FILE ]]; then
  99. FOUND_ANY=1
  100. fi
  101. done
  102. if [[ "$FOUND_ANY" = "0" ]]; then
  103. #all of the files are new files so we have to guess where the correct place to put it is.
  104. # if all of the lines start with a/ or b/, then this is a git patch that
  105. # was generated without --no-prefix
  106. if ! grep -qv '^a/\|^b/' $TMP2 ; then
  107. echo Looks like this is a git patch. Stripping a/ and b/ prefixes
  108. echo and incrementing PLEVEL
  109. PLEVEL=$[$PLEVEL + 1]
  110. sed -i -e 's,^[ab]/,,' $TMP2
  111. fi
  112. PREFIX_DIRS_AND_FILES=$(cut -d '/' -f 1 | sort | uniq)
  113. # if we are at the project root then nothing more to do
  114. if [[ -d hadoop-common-project ]]; then
  115. echo Looks like this is being run at project root
  116. # if all of the lines start with hadoop-common/, hadoop-hdfs/, hadoop-yarn/ or hadoop-mapreduce/, this is
  117. # relative to the hadoop root instead of the subproject root, so we need
  118. # to chop off another layer
  119. elif [[ "$PREFIX_DIRS_AND_FILES" =~ ^(hadoop-common-project|hadoop-hdfs-project|hadoop-yarn-project|hadoop-mapreduce-project)$ ]]; then
  120. echo Looks like this is relative to project root. Increasing PLEVEL
  121. PLEVEL=$[$PLEVEL + 1]
  122. elif ! echo "$PREFIX_DIRS_AND_FILES" | grep -vxq 'hadoop-common-project\|hadoop-hdfs-project\|hadoop-yarn-project\|hadoop-mapreduce-project' ; then
  123. echo Looks like this is a cross-subproject patch. Try applying from the project root
  124. cleanup 1
  125. fi
  126. fi
  127. elif $PATCH -p1 -E --dry-run < $PATCH_FILE 2>&1 > /dev/null; then
  128. PLEVEL=1
  129. elif $PATCH -p2 -E --dry-run < $PATCH_FILE 2>&1 > /dev/null; then
  130. PLEVEL=2
  131. else
  132. echo "The patch does not appear to apply with p0 to p2";
  133. cleanup 1;
  134. fi
  135. # If this is a dry run then exit instead of applying the patch
  136. if [[ -n $DRY_RUN ]]; then
  137. cleanup 0;
  138. fi
  139. echo Going to apply patch with: $PATCH -p$PLEVEL
  140. $PATCH -p$PLEVEL -E < $PATCH_FILE
  141. cleanup $?