|
| 1 | +#!/bin/sh |
| 2 | + |
| 3 | +# Forward ports changes from the spec file of a source branch to the spec file of a target branch |
| 4 | +# For example: porting interim changes made in v3.1.x patch releases to the v3.2.0 branch |
| 5 | + |
| 6 | +# This script is designed to be run once per branch, when interim changes need merging in |
| 7 | +# before another branch is released. It is not intended to be run multiple times to keep |
| 8 | +# two branches in sync. |
| 9 | + |
| 10 | +# Author: @MikeRalphson |
| 11 | +# Issues: https://github.com/OAI/OpenAPI-Specification/pull/2163 |
| 12 | + |
| 13 | +mainbranch=main |
| 14 | +myremote=origin |
| 15 | +upstream=upstream |
| 16 | + |
| 17 | +source=$1 |
| 18 | +target=$2 |
| 19 | + |
| 20 | +if [ -z "$source" ]; then |
| 21 | + echo You must specify a source and target branch |
| 22 | + exit 1 |
| 23 | +fi |
| 24 | +if [ -z "$target" ]; then |
| 25 | + echo You must specify a source and target branch |
| 26 | + exit 1 |
| 27 | +fi |
| 28 | + |
| 29 | +echo Checking working dir... |
| 30 | +status=`git ls-files -m` |
| 31 | +if [ -z "$status" ]; then |
| 32 | + echo All clear |
| 33 | +else |
| 34 | + echo You have a dirty working tree, aborting |
| 35 | + echo ${status} |
| 36 | + exit 1 |
| 37 | +fi |
| 38 | + |
| 39 | +cruft=`ls -1 *.patch *.rej *.mbox 2>/dev/null` |
| 40 | +if [ -z "$cruft" ]; then |
| 41 | + echo No .patch, .rej or .mbox files found, continuing |
| 42 | +else |
| 43 | + echo .patch / .rej / .mbox files found, aborting |
| 44 | + exit 1 |
| 45 | +fi |
| 46 | + |
| 47 | +tmpbranch=forward-port-${source} |
| 48 | +existing=`git branch | grep ${tmpbranch}` |
| 49 | +if [ -z "$existing" ]; then |
| 50 | + echo No matching temp branch found, continuing |
| 51 | +else |
| 52 | + echo Temp branch ${tmpbranch} already exists, aborting |
| 53 | + exit 1 |
| 54 | +fi |
| 55 | + |
| 56 | +srcver=`echo $source | sed s/-dev//g | sed s/v//g`.md |
| 57 | +tgtver=`echo $target | sed s/-dev//g | sed s/v//g`.md |
| 58 | + |
| 59 | +echo Forward-porting changes from ${source}:versions/${srcver} to ${target}:${tgtver} |
| 60 | +echo You may use the commands \'git fwdskip\' and \'git fwdcont\' to skip patches, or to continue after manually fixing. |
| 61 | +echo Use `fwdabort.sh` to abort cleanly. |
| 62 | +echo |
| 63 | +echo Due to a bug in \`git am\`, git v2.22.1+ is required, you\'re running: |
| 64 | +git --version |
| 65 | +echo |
| 66 | +echo Press a key to continue... |
| 67 | +read |
| 68 | + |
| 69 | +git config --add rerere.enabled true |
| 70 | +git config alias.fwdskip '!git am -i --skip' |
| 71 | +git config alias.fwdcont '!git am -i --continue' |
| 72 | + |
| 73 | +git checkout ${source} |
| 74 | +git pull ${upstream} ${source} |
| 75 | + |
| 76 | +# look at using git merge-base as an alternative? say if we branched 3.1.0 part way through 3.0.2's life |
| 77 | + |
| 78 | +firstsrc=`git log --abbrev-commit --format=format:%H -n 1 --reverse -- versions/${srcver}` |
| 79 | +lastsrc=`git log --abbrev-commit --format=format:%H -- versions/${srcver} | tail -n 1` |
| 80 | +changes=`git log --format=format:%H --reverse versions/${srcver}` |
| 81 | + |
| 82 | +echo Applying changes from ${firstsrc} to ${lastsrc} |
| 83 | + |
| 84 | +# remove first (creation) commit and uniq without sorting |
| 85 | +oIFS="$IFS" |
| 86 | +IFS=' ' |
| 87 | +changes=`echo ${changes} | tail -n +2 | awk '!x[$0]++'` |
| 88 | +IFS="$oIFS" |
| 89 | + |
| 90 | +for c in ${changes}; do |
| 91 | + git format-patch --stdout -1 $c | sed s/${srcver}/${tgtver}/g > $c.patch |
| 92 | +done |
| 93 | + |
| 94 | +git checkout ${target} |
| 95 | +git pull ${upstream} ${target} |
| 96 | +git checkout -b ${tmpbranch} |
| 97 | +cat *.patch > fwdport.mbox |
| 98 | +rm -f *.patch |
| 99 | +git am -3 --interactive --ignore-whitespace -s fwdport.mbox |
| 100 | + |
0 commit comments