Home | History | Annotate | Download | only in tools
      1 #!/bin/sh
      2 #
      3 # Copyright (C) 2010 The Android Open Source Project
      4 #
      5 # Licensed under the Apache License, Version 2.0 (the "License");
      6 # you may not use this file except in compliance with the License.
      7 # 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 #
     17 
     18 # Script used to patch a source directory from a series of patches
     19 # located under a directory hierarchy
     20 
     21 . `dirname $0`/prebuilt-common.sh
     22 
     23 PROGRAM_PARAMETERS="<src-dir> <patches-dir>"
     24 PROGRAM_DESCRIPTION=\
     25 "Patch a target source directory with a series of patches taken
     26 from another directory hierarchy. The idea is that anything that
     27 is found under <patches-dir>/subdir/foo.patch will be applied with
     28 'patch -p1' in <src-dir>/subdir.
     29 
     30 Patches are applied in the order they are found by 'find'."
     31 
     32 OPTION_REVERSE=no
     33 register_var_option "--reverse" OPTION_REVERSE "Reverse the patches applied previously"
     34 
     35 parse_parameters ()
     36 {
     37     SRC_DIR=$1
     38     if [ -z "$SRC_DIR" ] ; then
     39         echo "ERROR: Missing source directory. See --help for usage."
     40         exit 1
     41     fi
     42 
     43     if [ ! -d "$SRC_DIR" ] ; then
     44         echo "ERROR: Invalid target source directory: $SRC_DIR"
     45         exit 1
     46     fi
     47 
     48     PATCHES_DIR=$2
     49     if [ -z "$PATCHES_DIR" ] ; then
     50         echo "ERROR: Missing patches directory. See --help for usage."
     51         exit 1
     52     fi
     53 
     54     if [ ! -d "$PATCHES_DIR" ] ; then
     55         echo "ERROR: Invalid patches directory: $PATCHES_DIR"
     56         exit 1
     57     fi
     58 }
     59 
     60 extract_parameters "$@"
     61 parse_parameters $PARAMETERS
     62 
     63 if [ "$OPTION_REVERSE" = "yes" ]; then
     64     SORT="-r"
     65     REVERSE="-R"
     66 fi
     67 
     68 PATCHES=`(cd $PATCHES_DIR && find . -name "*.patch" | sort $SORT) 2> /dev/null`
     69 
     70 if [ -z "$PATCHES" ] ; then
     71     log "No patches files in $PATCHES_DIR"
     72     exit 0
     73 fi
     74 PATCHES=`echo $PATCHES | sed -e s%^\./%%g`
     75 for PATCH in $PATCHES; do
     76     PATCHDIR=`dirname $PATCH`
     77     PATCHNAME=`basename $PATCH`
     78     if [ -d $SRC_DIR/$PATCHDIR ]; then
     79         log "Applying $PATCHNAME into $SRC_DIR/$PATCHDIR"
     80         cd $SRC_DIR/$PATCHDIR && patch $REVERSE -p1 < $PATCHES_DIR/$PATCH
     81         fail_panic "Patch failure with $PATCHES_DIR/$PATCH!! !! Please check your patches directory!"
     82     else
     83         echo "Ignore non-existance $SRC_DIR/$PATCHDIR"
     84     fi
     85 done
     86 
     87 dump "Done!"
     88