create_patches.sh revision 12257:2fd1a9598a75
16502Snate@binkert.org#!/bin/bash
26502Snate@binkert.org#
36502Snate@binkert.org# Copyright (c) 2016 ARM Limited
46502Snate@binkert.org# All rights reserved
56502Snate@binkert.org#
66502Snate@binkert.org# The license below extends only to copyright in the software and shall
76502Snate@binkert.org# not be construed as granting a license to any other intellectual
86502Snate@binkert.org# property including but not limited to intellectual property relating
96502Snate@binkert.org# to a hardware implementation of the functionality of the software
106502Snate@binkert.org# licensed hereunder.  You may use the software subject to the license
116502Snate@binkert.org# terms below provided that you ensure that this notice is replicated
126502Snate@binkert.org# unmodified and in its entirety in all distributions of the software,
136502Snate@binkert.org# modified or unmodified, in source code or in binary form.
146502Snate@binkert.org#
156502Snate@binkert.org# Redistribution and use in source and binary forms, with or without
166502Snate@binkert.org# modification, are permitted provided that the following conditions are
176502Snate@binkert.org# met: redistributions of source code must retain the above copyright
186502Snate@binkert.org# notice, this list of conditions and the following disclaimer;
196502Snate@binkert.org# redistributions in binary form must reproduce the above copyright
206502Snate@binkert.org# notice, this list of conditions and the following disclaimer in the
216502Snate@binkert.org# documentation and/or other materials provided with the distribution;
226502Snate@binkert.org# neither the name of the copyright holders nor the names of its
236502Snate@binkert.org# contributors may be used to endorse or promote products derived from
246502Snate@binkert.org# this software without specific prior written permission.
256502Snate@binkert.org#
266502Snate@binkert.org# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
2712563Sgabeblack@google.com# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
2812563Sgabeblack@google.com# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
296651Snate@binkert.org# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
306502Snate@binkert.org# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
316502Snate@binkert.org# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
326502Snate@binkert.org# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
336502Snate@binkert.org# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
346502Snate@binkert.org# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
356502Snate@binkert.org# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
366502Snate@binkert.org# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
376502Snate@binkert.org#
386502Snate@binkert.org# Authors: Andreas Sandberg
396502Snate@binkert.org#
406502Snate@binkert.org
416502Snate@binkert.orgset -e
426502Snate@binkert.org
436502Snate@binkert.orgREL_SCRIPT_DIR=`dirname "$0"`
446502Snate@binkert.orgSCRIPT_NAME=`basename "$0"`
456502Snate@binkert.orgSCRIPT_DIR=$(cd "$REL_SCRIPT_DIR" && echo "$(pwd -P)")
466502Snate@binkert.orgMSG_FILTER="$SCRIPT_DIR"/upstream_msg_filter.sed
476502Snate@binkert.org
486502Snate@binkert.orgPATCH_DIR="./patches/"
496502Snate@binkert.orgUPSTREAM="upstream/master"
506502Snate@binkert.org
516502Snate@binkert.orgusage()
526502Snate@binkert.org{
536502Snate@binkert.org    cat <<EOF
546502Snate@binkert.org$SCRIPT_NAME [OPTION]...  [BRANCH]
556502Snate@binkert.orgFormat a patch series suitable for upstream consumption.
566502Snate@binkert.org
576502Snate@binkert.orgOptions:
586502Snate@binkert.org  -u BRANCH      Upstream branch
596502Snate@binkert.org  -d DIR         Patch directory
606999Snate@binkert.org  -h             Show this help string.
616999Snate@binkert.org
626999Snate@binkert.orgThis script creates a series of patches suitable from upstream
636502Snate@binkert.orgconsumption from a git branch. By default, the script works on the
646502Snate@binkert.orgcurrently checked out branch (HEAD). When invoked, the script executes
656502Snate@binkert.orgthe following operations in order:
666999Snate@binkert.org
676999Snate@binkert.org  1. Rebase the patches in the current branch onto the upstream
686502Snate@binkert.org     branch.
696651Snate@binkert.org  2. Filter commit messages.
706651Snate@binkert.org  3. Generate a set of patches in git format.
716502Snate@binkert.orgEOF
726502Snate@binkert.org}
736502Snate@binkert.org
746502Snate@binkert.orgbranch_exists()
756502Snate@binkert.org{
766502Snate@binkert.org    git rev-parse --verify -q "$1" > /dev/null
7713663Sandreas.sandberg@arm.com}
786502Snate@binkert.org
796502Snate@binkert.orgwhile getopts ":u:d:h" OPT; do
806502Snate@binkert.org    case $OPT in
816502Snate@binkert.org        d)
826502Snate@binkert.org            PATCH_DIR="$OPTARG"
836502Snate@binkert.org            ;;
846502Snate@binkert.org        u)
856502Snate@binkert.org            UPSTREAM="$OPTARG"
866502Snate@binkert.org            ;;
876502Snate@binkert.org        h)
886502Snate@binkert.org            usage
896502Snate@binkert.org            exit 0
906502Snate@binkert.org            ;;
916502Snate@binkert.org
926502Snate@binkert.org        \?)
936502Snate@binkert.org            echo "$0: invalid option --  '$OPTARG'" >&2
946502Snate@binkert.org            echo "Try '$0 -h' for more information." >&2
956502Snate@binkert.org            exit 1
966502Snate@binkert.org            ;;
976502Snate@binkert.org        :)
986502Snate@binkert.org            echo "$0: option requires an argument -- '$OPTARG'" >&2
996502Snate@binkert.org            exit 1
1006502Snate@binkert.org            ;;
1016502Snate@binkert.org        *)
1026502Snate@binkert.org            echo "Unhandled getopt return:" >&2
1036502Snate@binkert.org            echo "OPT: $OPT" >&2
1046502Snate@binkert.org            echo "OPTARG: $OPTARG" >&2
1056502Snate@binkert.org            exit 1
1066502Snate@binkert.org    esac
1076502Snate@binkert.orgdone
1086502Snate@binkert.org
1096502Snate@binkert.org
1106502Snate@binkert.orgshift $((OPTIND - 1))
1116502Snate@binkert.org
1126502Snate@binkert.orgBRANCH="${1:-HEAD}"
1136502Snate@binkert.org
1146502Snate@binkert.orgif ! branch_exists "$BRANCH"; then
1156502Snate@binkert.org    echo "Error: Patch branch '$BRANCH' doesn't exist" 1>&2
1166502Snate@binkert.org    exit 2
1176502Snate@binkert.orgfi
1186502Snate@binkert.org
1196502Snate@binkert.orgif ! branch_exists "$UPSTREAM"; then
1206502Snate@binkert.org    echo "Error: Upstream branch '$UPSTREAM' doesn't exist." 1>&2
1216502Snate@binkert.org    exit 2
1226502Snate@binkert.orgfi
1236502Snate@binkert.org
1246502Snate@binkert.orgSHA_PATCHES=`git rev-parse "$BRANCH"`
1256502Snate@binkert.orgOLD_BRANCH=`git symbolic-ref --short -q HEAD`
1266502Snate@binkert.orgSHA_UPSTREAM=`git rev-parse "$UPSTREAM"`
1276502Snate@binkert.org
1286999Snate@binkert.orgecho "Upstream branch: $UPSTREAM"
1296502Snate@binkert.orgecho "Patch directory: $PATCH_DIR"
1306502Snate@binkert.org
1316502Snate@binkert.orgecho "Preparing detached head..."
1326502Snate@binkert.orggit checkout -q --detach "$SHA_PATCHES"
1336502Snate@binkert.org
1346502Snate@binkert.org# Create an exit trap to checkout the old branch when we're done
1356502Snate@binkert.orgexit_trap() {
1367672Snate@binkert.org    git checkout -q "$OLD_BRANCH"
1377672Snate@binkert.org}
1386502Snate@binkert.orgtrap exit_trap EXIT
1397672Snate@binkert.org
1407672Snate@binkert.orgecho "Rebasing onto upstream master..."
1417672Snate@binkert.orggit rebase "$UPSTREAM"
1426502Snate@binkert.org
1436502Snate@binkert.orgecho "Filtering commit messages..."
1446502Snate@binkert.orggit filter-branch -f \
1456502Snate@binkert.org    --msg-filter "$MSG_FILTER" \
1466502Snate@binkert.org    "$SHA_UPSTREAM"..HEAD > /dev/null
1476502Snate@binkert.org
1486502Snate@binkert.orgecho "Creating patches..."
1496502Snate@binkert.orggit format-patch -p -o "$PATCH_DIR" "$UPSTREAM"
1506502Snate@binkert.org