Yvan Roux | a0a3367 | 2014-06-02 15:29:43 +0200 | [diff] [blame] | 1 | #!/bin/bash |
| 2 | |
| 3 | # ============================================================================== |
| 4 | # *** BackFLiP (a.k.a BACKporting Fun with LInaro People) *** |
| 5 | # |
| 6 | # Usage: backflip rev_number |
| 7 | # ============================================================================== |
| 8 | |
| 9 | # ============================================================================== |
| 10 | # Configuration |
| 11 | # ============================================================================== |
| 12 | VERSION=4.9 |
Yvan Roux | ca4cddd | 2015-03-18 20:59:10 +0100 | [diff] [blame] | 13 | REF_BRANCH=linaro-${VERSION}-branch |
Yvan Roux | a0a3367 | 2014-06-02 15:29:43 +0200 | [diff] [blame] | 14 | |
| 15 | DATE=`date +%Y-%m-%d` |
| 16 | NAME=`git config user.name` |
Yvan Roux | 65b2fd2 | 2015-03-13 13:17:43 +0100 | [diff] [blame] | 17 | FNAME=`echo ${NAME} | cut -d ' ' -f 1` |
Yvan Roux | a0a3367 | 2014-06-02 15:29:43 +0200 | [diff] [blame] | 18 | EMAIL=`git config user.email` |
Charles Baylis | 5f590ee | 2015-02-25 16:10:21 +0000 | [diff] [blame] | 19 | EDITOR="${EDITOR:-vim}" |
Yvan Roux | ca4cddd | 2015-03-18 20:59:10 +0100 | [diff] [blame] | 20 | STACKED=false |
Yvan Roux | a4c4f5d | 2015-03-18 21:21:25 +0100 | [diff] [blame] | 21 | CMPTOOL="meld" |
Yvan Roux | ca4cddd | 2015-03-18 20:59:10 +0100 | [diff] [blame] | 22 | |
| 23 | # Arguments |
| 24 | while getopts "b:r:v:" options |
| 25 | do |
| 26 | case $options in |
| 27 | r ) REF_BRANCH=$OPTARG |
| 28 | ;; |
| 29 | b ) DEV_BRANCH=$OPTARG |
| 30 | STACKED=true |
| 31 | ;; |
| 32 | v ) VERSION=$OPTARG |
| 33 | ;; |
| 34 | esac |
| 35 | done |
| 36 | shift $(($OPTIND - 1)) |
| 37 | |
Yvan Roux | a4c4f5d | 2015-03-18 21:21:25 +0100 | [diff] [blame] | 38 | REV=$1 |
Yvan Roux | ca4cddd | 2015-03-18 20:59:10 +0100 | [diff] [blame] | 39 | if [ -z ${DEV_BRANCH+x} ]; then |
| 40 | DEV_BRANCH=${REF_BRANCH} |
| 41 | fi |
Yvan Roux | a0a3367 | 2014-06-02 15:29:43 +0200 | [diff] [blame] | 42 | |
| 43 | # Style |
| 44 | NC='\e[0m' |
| 45 | red='\e[0;31m' |
| 46 | blue='\e[1;34m' |
| 47 | green='\e[0;32m' |
Charles Baylis | fcaeede | 2015-02-25 16:12:16 +0000 | [diff] [blame] | 48 | bold='\e[1m' |
Yvan Roux | a0a3367 | 2014-06-02 15:29:43 +0200 | [diff] [blame] | 49 | lines=`perl -E 'say "-" x 80'` |
| 50 | |
| 51 | # ============================================================================== |
| 52 | # Functions |
| 53 | # ============================================================================== |
| 54 | print_step() { |
| 55 | echo -e "${blue}${lines}\n${red}** ${1}\n${blue}${lines}${NC}\n" |
| 56 | } |
| 57 | |
| 58 | print_info() { |
| 59 | echo -e "${blue}** ${NC}${1}" |
| 60 | } |
| 61 | |
Yvan Roux | 76b0cb2 | 2015-03-13 13:41:51 +0100 | [diff] [blame] | 62 | die() { |
| 63 | echo -e "${red}${bold}ERROR: ${NC}${bold}${1}${NC}" |
| 64 | exit 1 |
| 65 | } |
| 66 | |
Yvan Roux | a4c4f5d | 2015-03-18 21:21:25 +0100 | [diff] [blame] | 67 | open_shell() { |
| 68 | PROMPT_COMMAND="echo -n -e '${bold}[${red}BackFLiP${NC}${bold}]${NC} '" bash |
| 69 | } |
| 70 | |
Yvan Roux | a0a3367 | 2014-06-02 15:29:43 +0200 | [diff] [blame] | 71 | # arg1 : a sentence |
| 72 | # arg2 : a variable |
| 73 | # print arg1 and gather user input in arg2 |
| 74 | ask() { |
| 75 | echo -n -e "${blue}** ${NC}${1} ${NC}" |
| 76 | eval "read ${2}" |
| 77 | } |
| 78 | |
| 79 | # arg1 : svn rev number |
| 80 | # arg2 : ChangeLog file |
Yvan Roux | ca4cddd | 2015-03-18 20:59:10 +0100 | [diff] [blame] | 81 | # Create ChangeLog entry for arg1 in arg2.arg1.frag |
Yvan Roux | a0a3367 | 2014-06-02 15:29:43 +0200 | [diff] [blame] | 82 | forge_entry() { |
Yvan Roux | ca4cddd | 2015-03-18 20:59:10 +0100 | [diff] [blame] | 83 | echo -e $DATE" "$NAME" <"$EMAIL">\n\n\tBackport from trunk r"$1"." >> $2.$1.frag |
Yvan Roux | a0a3367 | 2014-06-02 15:29:43 +0200 | [diff] [blame] | 84 | git show --format=oneline ${SHA1} -- $2 \ |
| 85 | | grep ^+ \ |
| 86 | | sed -e '1d' -e 's:^+::' \ |
Yvan Roux | ca4cddd | 2015-03-18 20:59:10 +0100 | [diff] [blame] | 87 | | awk '$1 ~ /^2.*$/ {print "\t"$0} $1 !~ /^2.*$/ {print $0}' >> $2.$1.frag |
Charles Baylis | fcaeede | 2015-02-25 16:12:16 +0000 | [diff] [blame] | 88 | print_info "${bold}Forged ChangeLog entry:${NC}" |
Yvan Roux | ca4cddd | 2015-03-18 20:59:10 +0100 | [diff] [blame] | 89 | cat $2.$1.frag |
Charles Baylis | 804b383 | 2015-02-25 16:12:41 +0000 | [diff] [blame] | 90 | ask "${bold}Edit ChangeLog entry [N/y] ?" user_edit |
Yvan Roux | a0a3367 | 2014-06-02 15:29:43 +0200 | [diff] [blame] | 91 | if [ "$user_edit" == "y" ]; then |
Yvan Roux | ca4cddd | 2015-03-18 20:59:10 +0100 | [diff] [blame] | 92 | "$EDITOR" $2.$1.frag |
Yvan Roux | a0a3367 | 2014-06-02 15:29:43 +0200 | [diff] [blame] | 93 | fi |
Yvan Roux | ca4cddd | 2015-03-18 20:59:10 +0100 | [diff] [blame] | 94 | git add $2.$1.frag |
Yvan Roux | a0a3367 | 2014-06-02 15:29:43 +0200 | [diff] [blame] | 95 | } |
| 96 | |
Yvan Roux | a4c4f5d | 2015-03-18 21:21:25 +0100 | [diff] [blame] | 97 | # arg1 : backported revision sha1 |
| 98 | compare() { |
| 99 | git show $1 > $REV.orig |
| 100 | git diff --cached > $REV.linaro |
| 101 | print_info "Generated diff files: ${bold}$REV.orig $REV.linaro${NC}" |
| 102 | "$CMPTOOL" $REV.orig $REV.linaro |
| 103 | ask "${bold}Do you need a Shell [N/y] ?" user_shell |
| 104 | if [ "$user_shell" == "y" ]; then |
| 105 | open_shell |
| 106 | fi |
| 107 | rm $REV.orig $REV.linaro |
| 108 | } |
| 109 | |
Yvan Roux | a0a3367 | 2014-06-02 15:29:43 +0200 | [diff] [blame] | 110 | # ============================================================================== |
Yvan Roux | 5b3527e | 2015-03-19 11:22:21 +0100 | [diff] [blame] | 111 | # Checking Configuration |
Yvan Roux | 76b0cb2 | 2015-03-13 13:41:51 +0100 | [diff] [blame] | 112 | # ============================================================================== |
Yvan Roux | c60a788 | 2015-03-19 11:13:36 +0100 | [diff] [blame] | 113 | if ! git remote -v 2>&1 | grep "ssh.*git\.linaro\.org" > /dev/null ; then |
Yvan Roux | 76b0cb2 | 2015-03-13 13:41:51 +0100 | [diff] [blame] | 114 | die "You're not in the right directory !" |
| 115 | fi |
| 116 | if [ ! -e .gitreview ]; then |
| 117 | print_info "Gerrit configuration (.gitreview) is missing, creating it." |
| 118 | cat <<- EOF > .gitreview |
| 119 | [gerrit] |
| 120 | host=review.linaro.org |
| 121 | port=29418 |
| 122 | project=toolchain/gcc |
| 123 | EOF |
| 124 | fi |
Yvan Roux | a4c4f5d | 2015-03-18 21:21:25 +0100 | [diff] [blame] | 125 | if ! which $CMPTOOL > /dev/null ; then |
| 126 | print_info "Default comparison tool is missing" |
| 127 | ask "Enter tool name: " CMPTOOL |
| 128 | fi |
Yvan Roux | 76b0cb2 | 2015-03-13 13:41:51 +0100 | [diff] [blame] | 129 | |
| 130 | # ============================================================================== |
Yvan Roux | a0a3367 | 2014-06-02 15:29:43 +0200 | [diff] [blame] | 131 | print_step "Finding SHA1" |
| 132 | # ============================================================================== |
Yvan Roux | a4c4f5d | 2015-03-18 21:21:25 +0100 | [diff] [blame] | 133 | SHA1=`git log --format=format:"%H" --grep=trunk@${REV} master` |
Charles Baylis | fcaeede | 2015-02-25 16:12:16 +0000 | [diff] [blame] | 134 | print_info "${NC}SVN rev ${bold}${1}${NC} SHA1: ${bold}${SHA1}${NC}" |
Charles Baylis | 804b383 | 2015-02-25 16:12:41 +0000 | [diff] [blame] | 135 | ask "${bold}View the commit [N/y] ?" user_view |
| 136 | if [ "$user_view" == "y" ]; then |
Yvan Roux | a0a3367 | 2014-06-02 15:29:43 +0200 | [diff] [blame] | 137 | git show $SHA1 |
| 138 | fi |
| 139 | |
| 140 | # ============================================================================== |
| 141 | print_step "Cherry-picking Revision" |
| 142 | # ============================================================================== |
Yvan Roux | ca4cddd | 2015-03-18 20:59:10 +0100 | [diff] [blame] | 143 | git checkout $DEV_BRANCH |
Yvan Roux | a0a3367 | 2014-06-02 15:29:43 +0200 | [diff] [blame] | 144 | git cherry-pick -n $SHA1 |
| 145 | |
| 146 | # ============================================================================== |
| 147 | print_step "Handling ChangeLogs Conflicts" |
| 148 | # ============================================================================== |
| 149 | CHLOGS=`git diff-tree --no-commit-id --name-only -r ${SHA1} | grep ChangeLog` |
| 150 | for i in $CHLOGS; do |
| 151 | print_info "${green}$i" |
| 152 | # Revert ChangeLog corruption |
| 153 | git reset -q HEAD $i |
| 154 | git checkout $i |
| 155 | # Create ChangeLog entry |
| 156 | forge_entry $1 $i |
Yvan Roux | a0a3367 | 2014-06-02 15:29:43 +0200 | [diff] [blame] | 157 | done |
| 158 | |
| 159 | # ============================================================================== |
| 160 | print_step "Status Checking" |
| 161 | # ============================================================================== |
| 162 | git status |
Charles Baylis | fcaeede | 2015-02-25 16:12:16 +0000 | [diff] [blame] | 163 | ask "${bold}Open a Shell [N/y] ?" user_shell |
Yvan Roux | a0a3367 | 2014-06-02 15:29:43 +0200 | [diff] [blame] | 164 | if [ "$user_shell" == "y" ]; then |
Yvan Roux | a4c4f5d | 2015-03-18 21:21:25 +0100 | [diff] [blame] | 165 | open_shell |
Yvan Roux | a0a3367 | 2014-06-02 15:29:43 +0200 | [diff] [blame] | 166 | fi |
| 167 | |
| 168 | # ============================================================================== |
Yvan Roux | a4c4f5d | 2015-03-18 21:21:25 +0100 | [diff] [blame] | 169 | print_step "Verification" |
| 170 | # ============================================================================== |
| 171 | compare $SHA1 |
| 172 | |
| 173 | # ============================================================================== |
Yvan Roux | a0a3367 | 2014-06-02 15:29:43 +0200 | [diff] [blame] | 174 | print_step "Branch/Commit/Push/Review" |
| 175 | # ============================================================================== |
Yvan Roux | ca4cddd | 2015-03-18 20:59:10 +0100 | [diff] [blame] | 176 | if $STACKED; then |
| 177 | bname=${DEV_BRANCH}-${1} |
| 178 | else |
| 179 | bname=${FNAME}-${VERSION}-backport-${1} |
| 180 | fi |
Charles Baylis | fcaeede | 2015-02-25 16:12:16 +0000 | [diff] [blame] | 181 | ask "${bold}Create backport branch [Y/n] ?" user_bc |
Yvan Roux | a0a3367 | 2014-06-02 15:29:43 +0200 | [diff] [blame] | 182 | if [ "$user_bc" != "n" ]; then |
Charles Baylis | fcaeede | 2015-02-25 16:12:16 +0000 | [diff] [blame] | 183 | ask "${bold}Change name (${blue}${bname}${NC}${bold}) [N/y] ?" user_bname |
Yvan Roux | a0a3367 | 2014-06-02 15:29:43 +0200 | [diff] [blame] | 184 | if [ "$user_bname" == "y" ]; then |
Charles Baylis | fcaeede | 2015-02-25 16:12:16 +0000 | [diff] [blame] | 185 | ask "${bold}Enter name :" bname |
Yvan Roux | a0a3367 | 2014-06-02 15:29:43 +0200 | [diff] [blame] | 186 | fi |
Yvan Roux | ca4cddd | 2015-03-18 20:59:10 +0100 | [diff] [blame] | 187 | git checkout -b ${bname} ${DEV_BRANCH} |
Yvan Roux | a0a3367 | 2014-06-02 15:29:43 +0200 | [diff] [blame] | 188 | |
Charles Baylis | fcaeede | 2015-02-25 16:12:16 +0000 | [diff] [blame] | 189 | ask "${bold}Commit backport [Y/n] ?" user_commit |
Yvan Roux | a0a3367 | 2014-06-02 15:29:43 +0200 | [diff] [blame] | 190 | if [ "$user_commit" != "n" ]; then |
Yvan Roux | ca4cddd | 2015-03-18 20:59:10 +0100 | [diff] [blame] | 191 | if $STACKED; then |
| 192 | git commit --amend |
| 193 | else |
| 194 | git commit |
| 195 | fi |
Yvan Roux | a0a3367 | 2014-06-02 15:29:43 +0200 | [diff] [blame] | 196 | |
Charles Baylis | fcaeede | 2015-02-25 16:12:16 +0000 | [diff] [blame] | 197 | ask "${bold}Create patch [Y/n] ?" user_patch |
Yvan Roux | a0a3367 | 2014-06-02 15:29:43 +0200 | [diff] [blame] | 198 | if [ "$user_patch" != "n" ]; then |
| 199 | git format-patch -o ../ --suffix=.${bname}.patch -1 |
| 200 | fi |
| 201 | |
Charles Baylis | fcaeede | 2015-02-25 16:12:16 +0000 | [diff] [blame] | 202 | ask "${bold}Push ${bname} branch [Y/n] ?" user_push |
Yvan Roux | a0a3367 | 2014-06-02 15:29:43 +0200 | [diff] [blame] | 203 | if [ "$user_push" != "n" ]; then |
| 204 | git push origin ${bname}:${bname} |
| 205 | |
Charles Baylis | fcaeede | 2015-02-25 16:12:16 +0000 | [diff] [blame] | 206 | ask "${bold}Request review (amend commit) [Y/n] ?" user_review |
Yvan Roux | a0a3367 | 2014-06-02 15:29:43 +0200 | [diff] [blame] | 207 | if [ "$user_review" != "n" ]; then |
| 208 | git commit --amend |
Yvan Roux | ca4cddd | 2015-03-18 20:59:10 +0100 | [diff] [blame] | 209 | git review $REF_BRANCH |
| 210 | git checkout $DEV_BRANCH |
Yvan Roux | a0a3367 | 2014-06-02 15:29:43 +0200 | [diff] [blame] | 211 | fi |
| 212 | fi |
| 213 | fi |
| 214 | fi |
| 215 | |
| 216 | # ============================================================================== |
Charles Baylis | fcaeede | 2015-02-25 16:12:16 +0000 | [diff] [blame] | 217 | ask "${bold}Abort and Cleanup [N/y] ?" user_abort |
Yvan Roux | a0a3367 | 2014-06-02 15:29:43 +0200 | [diff] [blame] | 218 | # ============================================================================== |
| 219 | if [ "$user_abort" == "y" ]; then |
| 220 | git reset --hard |
Yvan Roux | ca4cddd | 2015-03-18 20:59:10 +0100 | [diff] [blame] | 221 | git checkout ${DEV_BRANCH} |
Yvan Roux | a0a3367 | 2014-06-02 15:29:43 +0200 | [diff] [blame] | 222 | if [ "$user_bc" != "n" ]; then |
| 223 | git branch -D ${bname} |
| 224 | if [ "$user_push" != "n" ]; then |
| 225 | git push origin :${bname} |
| 226 | fi |
| 227 | fi |
| 228 | exit 0 |
| 229 | fi |