#!/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. if [ -z "$1" ]; then echo "Usage1: patch_from_baidu PATCHFILE (generated by 'git diff --no-prefix')" echo "Usage2: patch_from_baidu GIT_DIR COMMIT" exit 1 fi PATCHFILE=$1 if [ -d "$1/.git" ]; then if [ -z "$2" ]; then echo "Second argument must be a git commit" exit 1 fi CURRENT_DIR=`pwd` 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 cd $CURRENT_DIR fi MODIFIED_PATCHFILE=$(basename $(basename $PATCHFILE .patch) .diff).from_baidu.patch # guess prefix of test files TEST_PREFIX="test_" TEST_SUFFIX= if fgrep -q " src/baidu/rpc/" $PATCHFILE; then TEST_PREFIX="brpc_" TEST_SUFFIX="_unittest" elif fgrep -q " bthread/" $PATCHFILE; then TEST_PREFIX="bthread_" TEST_SUFFIX="_unittest" elif fgrep -q " bvar/" $PATCHFILE; then TEST_PREFIX="bvar_" TEST_SUFFIX="_unittest" fi cat $PATCHFILE | sed -e 's/src\/baidu\/rpc\//src\/brpc\//g' \ -e 's/\//g' \ -e 's/\]*\)>/"\1"/g' \ -e 's/<\(bvar\/[^>]*\)>/"\1"/g' \ -e 's/]*\)>/"butil\/\1"/g' \ -e 's/"base\/\([^"]*\)"/"butil\/\1"/g' \ -e 's/<\(bthread\/[^>]*\)>/"\1"/g' \ -e 's/<\(mcpack2pb\/[^>]*\)>/"\1"/g' \ -e 's/\/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' \ -e 's/ base\// src\/butil\//g' \ -e 's/ bthread\// src\/bthread\//g' \ -e 's/ bvar\// src\/bvar\//g' \ > $MODIFIED_PATCHFILE EXTRA_ARGS= if [ -z "$DO_RUN" ]; then EXTRA_ARGS="--dry-run $EXTRA_ARGS" echo "*** This is a dry-run. To really apply, run: DO_RUN=1 tools/patch_from_baidu $1" fi 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