patch_from_baidu 4.1 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
#!/usr/bin/env bash

# Licensed to the Apache Software Foundation (ASF) under one or more
# contributor license agreements.  See the NOTICE file distributed with
# this work for additional information regarding copyright ownership.
# The ASF licenses this file to You under the Apache License, Version 2.0
# (the "License"); you may not use this file except in compliance with
# the License.  You may obtain a copy of the License at
#
#    http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

18
if [ -z "$1" ]; then
19 20
    echo "Usage1: patch_from_baidu PATCHFILE (generated by 'git diff --no-prefix')"
    echo "Usage2: patch_from_baidu GIT_DIR COMMIT"
21 22
    exit 1
fi
23
PATCHFILE=$1
24
if [ -d "$1/.git" ]; then
25
    if [ -z "$2" ]; then
26
        echo "Second argument must be a git commit"
27 28
        exit 1
    fi
29
    CURRENT_DIR=`pwd`
30 31 32 33 34
    GIT_DIR=$1
    COMMIT=$2
    PATCHFILE=$CURRENT_DIR/$COMMIT.patch
    echo "*** Generating diff of $GIT_DIR@$COMMIT"
    cd $GIT_DIR && git diff --no-prefix $COMMIT^! > $PATCHFILE
gejun's avatar
gejun committed
35
    cd $CURRENT_DIR
36 37
fi

38
MODIFIED_PATCHFILE=$(basename $(basename $PATCHFILE .patch) .diff).from_baidu.patch
39 40 41 42

# guess prefix of test files
TEST_PREFIX="test_"
TEST_SUFFIX=
43 44 45 46
if fgrep -q " src/baidu/rpc/" $PATCHFILE; then
    TEST_PREFIX="brpc_"
    TEST_SUFFIX="_unittest"
elif fgrep -q " bthread/" $PATCHFILE; then
47 48 49 50 51 52
    TEST_PREFIX="bthread_"
    TEST_SUFFIX="_unittest"
elif fgrep -q " bvar/" $PATCHFILE; then
    TEST_PREFIX="bvar_"
    TEST_SUFFIX="_unittest"
fi
53

54
cat $PATCHFILE | sed -e 's/src\/baidu\/rpc\//src\/brpc\//g' \
55
            -e 's/\<baidu\/rpc\//brpc\//g' \
56
            -e 's/\<baidu\-rpc\([^-]\)/brpc\1/g' \
gejun's avatar
gejun committed
57 58 59
            -e 's/\<test\/test_bthread\.cpp/test\/bthread_unittest.cpp/g' \
            -e 's/\<test\/test_object_pool\.cpp/test\/object_pool_unittest.cpp/g' \
            -e 's/\<test\/test_resource_pool\.cpp/test\/resource_pool_unittest.cpp/g' \
60
            -e "s/\<test\/test_\(\S*\)\.cpp/test\/${TEST_PREFIX}\1${TEST_SUFFIX}.cpp/g" \
61 62 63 64 65 66
            -e 's/\<namespace \+baidu *{/namespace brpc {/g' \
            -e 's/\<namespace \+rpc *{//g' \
            -e 's/} *\/\/ \+namespace \+baidu/} \/\/ namespace brpc/g' \
            -e 's/} *\/\/ \+namespace \+rpc\>//g' \
            -e 's/\<baidu::rpc::/brpc::/g' \
            -e 's/\<rpc::/brpc::/g' \
67
            -e 's/\<base::/butil::/g' \
68
            -e 's/\<BAIDU_RPC_/BRPC_/g' \
69
            -e 's/\<protocol\/\(\S*\)\.proto/src\/\1.proto/g' \
70 71 72 73
            -e 's/ bthread_cond\.cpp/ src\/bthread\/condition_variable.cpp/g' \
            -e 's/ bthread_\([^.]*\)\.cpp/ src\/bthread\/\1.cpp/g' \
            -e 's/ bthread_\([^.]*\)\.h/ src\/bthread\/\1.h/g' \
            -e 's/ bthread\.\(h\|cpp\)/ src\/bthread\/bthread.\1/g' \
74
            -e 's/<\(brpc\/[^>]*\)>/"\1"/g' \
gejun's avatar
gejun committed
75
            -e 's/<\(bvar\/[^>]*\)>/"\1"/g' \
76
            -e 's/<base\/\([^>]*\)>/"butil\/\1"/g' \
77
            -e 's/"base\/\([^"]*\)"/"butil\/\1"/g' \
gejun's avatar
gejun committed
78 79
            -e 's/<\(bthread\/[^>]*\)>/"\1"/g' \
            -e 's/<\(mcpack2pb\/[^>]*\)>/"\1"/g' \
zhujiashun's avatar
zhujiashun committed
80 81 82 83
            -e 's/\<protobuf_json\>/json2pb/g' \
            -e 's/src\/json_to_pb/src\/json2pb\/json_to_pb/g' \
            -e 's/src\/pb_to_json/src\/json2pb\/pb_to_json/g' \
            -e 's/test\/test_protobuf_json/test\/brpc_protobuf_json_unittest/g' \
84
            -e 's/ base\// src\/butil\//g' \
85 86
            -e 's/ bthread\// src\/bthread\//g' \
            -e 's/ bvar\// src\/bvar\//g' \
87
            > $MODIFIED_PATCHFILE
88 89 90
EXTRA_ARGS=
if [ -z "$DO_RUN" ]; then
    EXTRA_ARGS="--dry-run $EXTRA_ARGS"
91
    echo "*** This is a dry-run. To really apply, run: DO_RUN=1 tools/patch_from_baidu $1"
92
fi
93 94 95 96 97 98 99 100
patch -p0 -u -l $EXTRA_ARGS < $MODIFIED_PATCHFILE
if [ ! -z "$DO_RUN" ]; then
    REJ_FILES=`find . -name "*.rej"`
    if [ ! -z "$REJ_FILES" ]; then
        echo "==== The patching is not done yet! Apply following rej files manually ===="
        echo $REJ_FILES
    fi
fi