patch_from_baidu 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. #!/usr/bin/env bash
  2. # Licensed to the Apache Software Foundation (ASF) under one or more
  3. # contributor license agreements. See the NOTICE file distributed with
  4. # this work for additional information regarding copyright ownership.
  5. # The ASF licenses this file to You under the Apache License, Version 2.0
  6. # (the "License"); you may not use this file except in compliance with
  7. # the License. You may obtain a copy of the License at
  8. #
  9. # http://www.apache.org/licenses/LICENSE-2.0
  10. #
  11. # Unless required by applicable law or agreed to in writing, software
  12. # distributed under the License is distributed on an "AS IS" BASIS,
  13. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. # See the License for the specific language governing permissions and
  15. # limitations under the License.
  16. if [ -z "$1" ]; then
  17. echo "Usage1: patch_from_baidu PATCHFILE (generated by 'git diff --no-prefix')"
  18. echo "Usage2: patch_from_baidu GIT_DIR COMMIT"
  19. exit 1
  20. fi
  21. PATCHFILE=$1
  22. if [ -d "$1/.git" ]; then
  23. if [ -z "$2" ]; then
  24. echo "Second argument must be a git commit"
  25. exit 1
  26. fi
  27. CURRENT_DIR=`pwd`
  28. GIT_DIR=$1
  29. COMMIT=$2
  30. PATCHFILE=$CURRENT_DIR/$COMMIT.patch
  31. echo "*** Generating diff of $GIT_DIR@$COMMIT"
  32. cd $GIT_DIR && git diff --no-prefix $COMMIT^! > $PATCHFILE
  33. cd $CURRENT_DIR
  34. fi
  35. MODIFIED_PATCHFILE=$(basename $(basename $PATCHFILE .patch) .diff).from_baidu.patch
  36. # guess prefix of test files
  37. TEST_PREFIX="test_"
  38. TEST_SUFFIX=
  39. if fgrep -q " src/baidu/rpc/" $PATCHFILE; then
  40. TEST_PREFIX="brpc_"
  41. TEST_SUFFIX="_unittest"
  42. elif fgrep -q " bthread/" $PATCHFILE; then
  43. TEST_PREFIX="bthread_"
  44. TEST_SUFFIX="_unittest"
  45. elif fgrep -q " bvar/" $PATCHFILE; then
  46. TEST_PREFIX="bvar_"
  47. TEST_SUFFIX="_unittest"
  48. fi
  49. cat $PATCHFILE | sed -e 's/src\/baidu\/rpc\//src\/brpc\//g' \
  50. -e 's/\<baidu\/rpc\//brpc\//g' \
  51. -e 's/\<baidu\-rpc\([^-]\)/brpc\1/g' \
  52. -e 's/\<test\/test_bthread\.cpp/test\/bthread_unittest.cpp/g' \
  53. -e 's/\<test\/test_object_pool\.cpp/test\/object_pool_unittest.cpp/g' \
  54. -e 's/\<test\/test_resource_pool\.cpp/test\/resource_pool_unittest.cpp/g' \
  55. -e "s/\<test\/test_\(\S*\)\.cpp/test\/${TEST_PREFIX}\1${TEST_SUFFIX}.cpp/g" \
  56. -e 's/\<namespace \+baidu *{/namespace brpc {/g' \
  57. -e 's/\<namespace \+rpc *{//g' \
  58. -e 's/} *\/\/ \+namespace \+baidu/} \/\/ namespace brpc/g' \
  59. -e 's/} *\/\/ \+namespace \+rpc\>//g' \
  60. -e 's/\<baidu::rpc::/brpc::/g' \
  61. -e 's/\<rpc::/brpc::/g' \
  62. -e 's/\<base::/butil::/g' \
  63. -e 's/\<BAIDU_RPC_/BRPC_/g' \
  64. -e 's/\<protocol\/\(\S*\)\.proto/src\/\1.proto/g' \
  65. -e 's/ bthread_cond\.cpp/ src\/bthread\/condition_variable.cpp/g' \
  66. -e 's/ bthread_\([^.]*\)\.cpp/ src\/bthread\/\1.cpp/g' \
  67. -e 's/ bthread_\([^.]*\)\.h/ src\/bthread\/\1.h/g' \
  68. -e 's/ bthread\.\(h\|cpp\)/ src\/bthread\/bthread.\1/g' \
  69. -e 's/<\(brpc\/[^>]*\)>/"\1"/g' \
  70. -e 's/<\(bvar\/[^>]*\)>/"\1"/g' \
  71. -e 's/<base\/\([^>]*\)>/"butil\/\1"/g' \
  72. -e 's/"base\/\([^"]*\)"/"butil\/\1"/g' \
  73. -e 's/<\(bthread\/[^>]*\)>/"\1"/g' \
  74. -e 's/<\(mcpack2pb\/[^>]*\)>/"\1"/g' \
  75. -e 's/\<protobuf_json\>/json2pb/g' \
  76. -e 's/src\/json_to_pb/src\/json2pb\/json_to_pb/g' \
  77. -e 's/src\/pb_to_json/src\/json2pb\/pb_to_json/g' \
  78. -e 's/test\/test_protobuf_json/test\/brpc_protobuf_json_unittest/g' \
  79. -e 's/ base\// src\/butil\//g' \
  80. -e 's/ bthread\// src\/bthread\//g' \
  81. -e 's/ bvar\// src\/bvar\//g' \
  82. > $MODIFIED_PATCHFILE
  83. EXTRA_ARGS=
  84. if [ -z "$DO_RUN" ]; then
  85. EXTRA_ARGS="--dry-run $EXTRA_ARGS"
  86. echo "*** This is a dry-run. To really apply, run: DO_RUN=1 tools/patch_from_baidu $1"
  87. fi
  88. patch -p0 -u -l $EXTRA_ARGS < $MODIFIED_PATCHFILE
  89. if [ ! -z "$DO_RUN" ]; then
  90. REJ_FILES=`find . -name "*.rej"`
  91. if [ ! -z "$REJ_FILES" ]; then
  92. echo "==== The patching is not done yet! Apply following rej files manually ===="
  93. echo $REJ_FILES
  94. fi
  95. fi