| Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 1 | #!/bin/sh | 
|  | 2 | # | 
|  | 3 | # Copyright (c) 2010-2013,  Intel Corporation. | 
|  | 4 | # All Rights Reserved | 
|  | 5 | # | 
|  | 6 | # This program is free software;  you can redistribute it and/or modify | 
|  | 7 | # it under the terms of the GNU General Public License as published by | 
|  | 8 | # the Free Software Foundation; either version 2 of the License, or | 
|  | 9 | # (at your option) any later version. | 
|  | 10 | # | 
|  | 11 | # This program is distributed in the hope that it will be useful, | 
|  | 12 | # but WITHOUT ANY WARRANTY;  without even the implied warranty of | 
|  | 13 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See | 
|  | 14 | # the GNU General Public License for more details. | 
|  | 15 | # | 
|  | 16 | # You should have received a copy of the GNU General Public License | 
|  | 17 | # along with this program;  if not, write to the Free Software | 
|  | 18 | # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | 
|  | 19 | # | 
|  | 20 |  | 
|  | 21 | # | 
|  | 22 | # This script is intended to be used to prepare a series of patches | 
|  | 23 | # and a cover letter in an appropriate and consistent format for | 
|  | 24 | # submission to Open Embedded and The Yocto Project, as well as to | 
|  | 25 | # related projects and layers. | 
|  | 26 | # | 
|  | 27 |  | 
|  | 28 | ODIR=pull-$$ | 
|  | 29 | RELATIVE_TO="master" | 
|  | 30 | COMMIT_ID="HEAD" | 
|  | 31 | PREFIX="PATCH" | 
|  | 32 | RFC=0 | 
|  | 33 |  | 
|  | 34 | usage() { | 
|  | 35 | CMD=$(basename $0) | 
|  | 36 | cat <<EOM | 
|  | 37 | Usage: $CMD [-h] [-o output_dir] [-m msg_body_file] [-s subject] [-r relative_to] [-i commit_id] [-d relative_dir] -u remote [-b branch] | 
|  | 38 | -b branch           Branch name in the specified remote (default: current branch) | 
| Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame^] | 39 | -l local branch     Local branch name (default: HEAD) | 
| Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 40 | -c                  Create an RFC (Request for Comment) patch series | 
|  | 41 | -h                  Display this help message | 
|  | 42 | -i commit_id        Ending commit (default: HEAD) | 
|  | 43 | -m msg_body_file    The file containing a blurb to be inserted into the summary email | 
|  | 44 | -o output_dir       Specify the output directory for the messages (default: pull-PID) | 
|  | 45 | -p prefix           Use [prefix N/M] instead of [PATCH N/M] as the subject prefix | 
|  | 46 | -r relative_to      Starting commit (default: master) | 
|  | 47 | -s subject          The subject to be inserted into the summary email | 
|  | 48 | -u remote           The git remote where the branch is located | 
|  | 49 | -d relative_dir     Generate patches relative to directory | 
|  | 50 |  | 
|  | 51 | Examples: | 
|  | 52 | $CMD -u contrib -b nitin/basic | 
|  | 53 | $CMD -u contrib -r distro/master -i nitin/distro -b nitin/distro | 
| Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame^] | 54 | $CMD -u contrib -r distro/master -i nitin/distro -b nitin/distro -l distro | 
| Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 55 | $CMD -u contrib -r master -i misc -b nitin/misc -o pull-misc | 
|  | 56 | $CMD -u contrib -p "RFC PATCH" -b nitin/experimental | 
|  | 57 | $CMD -u contrib -i misc -b nitin/misc -d ./bitbake | 
|  | 58 | EOM | 
|  | 59 | } | 
|  | 60 |  | 
|  | 61 | # Parse and validate arguments | 
| Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame^] | 62 | while getopts "b:cd:hi:m:o:p:r:s:u:l:" OPT; do | 
| Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 63 | case $OPT in | 
|  | 64 | b) | 
|  | 65 | BRANCH="$OPTARG" | 
|  | 66 | ;; | 
| Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame^] | 67 | l) | 
|  | 68 | L_BRANCH="$OPTARG" | 
|  | 69 | ;; | 
| Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 70 | c) | 
|  | 71 | RFC=1 | 
|  | 72 | ;; | 
|  | 73 | d) | 
|  | 74 | RELDIR="$OPTARG" | 
|  | 75 | ;; | 
|  | 76 | h) | 
|  | 77 | usage | 
|  | 78 | exit 0 | 
|  | 79 | ;; | 
|  | 80 | i) | 
|  | 81 | COMMIT_ID="$OPTARG" | 
|  | 82 | ;; | 
|  | 83 | m) | 
|  | 84 | BODY="$OPTARG" | 
|  | 85 | if [ ! -e "$BODY" ]; then | 
|  | 86 | echo "ERROR: Body file does not exist" | 
|  | 87 | exit 1 | 
|  | 88 | fi | 
|  | 89 | ;; | 
|  | 90 | o) | 
|  | 91 | ODIR="$OPTARG" | 
|  | 92 | ;; | 
|  | 93 | p) | 
|  | 94 | PREFIX="$OPTARG" | 
|  | 95 | ;; | 
|  | 96 | r) | 
|  | 97 | RELATIVE_TO="$OPTARG" | 
|  | 98 | ;; | 
|  | 99 | s) | 
|  | 100 | SUBJECT="$OPTARG" | 
|  | 101 | ;; | 
|  | 102 | u) | 
|  | 103 | REMOTE="$OPTARG" | 
|  | 104 | REMOTE_URL=$(git config remote.$REMOTE.url) | 
|  | 105 | if [ $? -ne 0 ]; then | 
|  | 106 | echo "ERROR: git config failed to find a url for '$REMOTE'" | 
|  | 107 | echo | 
|  | 108 | echo "To add a remote url for $REMOTE, use:" | 
|  | 109 | echo "  git config remote.$REMOTE.url <url>" | 
|  | 110 | exit 1 | 
|  | 111 | fi | 
|  | 112 |  | 
|  | 113 | # Rewrite private URLs to public URLs | 
|  | 114 | # Determine the repository name for use in the WEB_URL later | 
|  | 115 | case "$REMOTE_URL" in | 
|  | 116 | *@*) | 
|  | 117 | USER_RE="[A-Za-z0-9_.@][A-Za-z0-9_.@-]*\$\?" | 
|  | 118 | PROTO_RE="[a-z][a-z+]*://" | 
|  | 119 | GIT_RE="\(^\($PROTO_RE\)\?$USER_RE@\)\([^:/]*\)[:/]\(.*\)" | 
|  | 120 | REMOTE_URL=${REMOTE_URL%.git} | 
|  | 121 | REMOTE_REPO=$(echo $REMOTE_URL | sed "s#$GIT_RE#\4#") | 
|  | 122 | REMOTE_URL=$(echo $REMOTE_URL | sed "s#$GIT_RE#git://\3/\4#") | 
|  | 123 | ;; | 
|  | 124 | *) | 
|  | 125 | echo "WARNING: Unrecognized remote URL: $REMOTE_URL" | 
|  | 126 | echo "         The pull and browse URLs will likely be incorrect" | 
|  | 127 | ;; | 
|  | 128 | esac | 
|  | 129 | ;; | 
|  | 130 | esac | 
|  | 131 | done | 
|  | 132 |  | 
|  | 133 | if [ -z "$BRANCH" ]; then | 
|  | 134 | BRANCH=$(git branch | grep -e "^\* " | cut -d' ' -f2) | 
|  | 135 | echo "NOTE: Assuming remote branch '$BRANCH', use -b to override." | 
|  | 136 | fi | 
|  | 137 |  | 
| Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame^] | 138 | if [ -z "$L_BRANCH" ]; then | 
|  | 139 | L_BRANCH=HEAD | 
|  | 140 | echo "NOTE: Assuming local branch HEAD, use -l to override." | 
|  | 141 | fi | 
|  | 142 |  | 
| Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 143 | if [ -z "$REMOTE_URL" ]; then | 
|  | 144 | echo "ERROR: Missing parameter -u, no git remote!" | 
|  | 145 | usage | 
|  | 146 | exit 1 | 
|  | 147 | fi | 
|  | 148 |  | 
|  | 149 | if [ $RFC -eq 1 ]; then | 
|  | 150 | PREFIX="RFC $PREFIX" | 
|  | 151 | fi | 
|  | 152 |  | 
|  | 153 |  | 
|  | 154 | # Set WEB_URL from known remotes | 
|  | 155 | WEB_URL="" | 
|  | 156 | case "$REMOTE_URL" in | 
|  | 157 | *git.yoctoproject.org*) | 
|  | 158 | WEB_URL="http://git.yoctoproject.org/cgit.cgi/$REMOTE_REPO/log/?h=$BRANCH" | 
|  | 159 | ;; | 
|  | 160 | *git.pokylinux.org*) | 
|  | 161 | WEB_URL="http://git.pokylinux.org/cgit.cgi/$REMOTE_REPO/log/?h=$BRANCH" | 
|  | 162 | ;; | 
|  | 163 | *git.openembedded.org*) | 
|  | 164 | WEB_URL="http://cgit.openembedded.org/cgit.cgi/$REMOTE_REPO/log/?h=$BRANCH" | 
|  | 165 | ;; | 
|  | 166 | *github.com*) | 
|  | 167 | WEB_URL="https://github.com/$REMOTE_REPO/tree/$BRANCH" | 
|  | 168 | ;; | 
|  | 169 | esac | 
|  | 170 |  | 
|  | 171 | # Perform a sanity test on the web URL. Issue a warning if it is not | 
|  | 172 | # accessible, but do not abort as users may want to run offline. | 
|  | 173 | if [ -n "$WEB_URL" ]; then | 
|  | 174 | wget --no-check-certificate -q $WEB_URL -O /dev/null | 
|  | 175 | if [ $? -ne 0 ]; then | 
|  | 176 | echo "WARNING: Branch '$BRANCH' was not found on the contrib git tree." | 
|  | 177 | echo "         Please check your remote and branch parameter before sending." | 
|  | 178 | echo "" | 
|  | 179 | fi | 
|  | 180 | fi | 
|  | 181 |  | 
|  | 182 | if [ -e $ODIR ]; then | 
|  | 183 | echo "ERROR: output directory $ODIR exists." | 
|  | 184 | exit 1 | 
|  | 185 | fi | 
|  | 186 | mkdir $ODIR | 
|  | 187 |  | 
|  | 188 | if [ -n "$RELDIR" ]; then | 
|  | 189 | ODIR=$(realpath $ODIR) | 
|  | 190 | pdir=$(pwd) | 
|  | 191 | cd $RELDIR | 
|  | 192 | extraopts="--relative" | 
|  | 193 | fi | 
|  | 194 |  | 
|  | 195 | # Generate the patches and cover letter | 
|  | 196 | git format-patch $extraopts -M40 --subject-prefix="$PREFIX" -n -o $ODIR --thread=shallow --cover-letter $RELATIVE_TO..$COMMIT_ID > /dev/null | 
|  | 197 |  | 
| Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame^] | 198 | if [ -z "$(ls -A $ODIR 2> /dev/null)" ]; then | 
|  | 199 | echo "ERROR: $ODIR is empty, no cover letter and patches was generated!" | 
|  | 200 | echo "       This is most likely due to that \$RRELATIVE_TO..\$COMMIT_ID" | 
|  | 201 | echo "       ($RELATIVE_TO..$COMMIT_ID) don't contain any differences." | 
|  | 202 | rmdir $ODIR | 
|  | 203 | exit 1 | 
|  | 204 | fi | 
|  | 205 |  | 
| Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 206 | [ -n "$RELDIR" ] && cd $pdir | 
|  | 207 |  | 
|  | 208 | # Customize the cover letter | 
|  | 209 | CL="$ODIR/0000-cover-letter.patch" | 
|  | 210 | PM="$ODIR/pull-msg" | 
|  | 211 | GIT_VERSION=$(`git --version` | tr -d '[:alpha:][:space:].' | sed 's/\(...\).*/\1/') | 
|  | 212 | NEWER_GIT_VERSION=210 | 
|  | 213 | if [ $GIT_VERSION -lt $NEWER_GIT_VERSION ]; then | 
|  | 214 | git request-pull $RELATIVE_TO $REMOTE_URL $COMMIT_ID >> "$PM" | 
|  | 215 | else | 
| Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame^] | 216 | git request-pull $RELATIVE_TO $REMOTE_URL $L_BRANCH:$BRANCH >> "$PM" | 
| Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 217 | fi | 
|  | 218 | if [ $? -ne 0 ]; then | 
|  | 219 | echo "ERROR: git request-pull reported an error" | 
|  | 220 | exit 1 | 
|  | 221 | fi | 
|  | 222 |  | 
|  | 223 | # The cover letter already has a diffstat, remove it from the pull-msg | 
|  | 224 | # before inserting it. | 
|  | 225 | sed -n "0,\#$REMOTE_URL# p" "$PM" | sed -i "/BLURB HERE/ r /dev/stdin" "$CL" | 
|  | 226 | rm "$PM" | 
|  | 227 |  | 
|  | 228 | # If this is an RFC, make that clear in the cover letter | 
|  | 229 | if [ $RFC -eq 1 ]; then | 
|  | 230 | (cat <<EOM | 
|  | 231 | Please review the following changes for suitability for inclusion. If you have | 
|  | 232 | any objections or suggestions for improvement, please respond to the patches. If | 
|  | 233 | you agree with the changes, please provide your Acked-by. | 
|  | 234 |  | 
|  | 235 | EOM | 
|  | 236 | ) | sed -i "/BLURB HERE/ r /dev/stdin" "$CL" | 
|  | 237 | fi | 
|  | 238 |  | 
|  | 239 | # Insert the WEB_URL if there is one | 
|  | 240 | if [ -n "$WEB_URL" ]; then | 
|  | 241 | echo "  $WEB_URL" | sed -i "\#$REMOTE_URL# r /dev/stdin" "$CL" | 
|  | 242 | fi | 
|  | 243 |  | 
|  | 244 |  | 
|  | 245 | # If the user specified a message body, insert it into the cover letter and | 
|  | 246 | # remove the BLURB token. | 
|  | 247 | if [ -n "$BODY" ]; then | 
|  | 248 | sed -i "/BLURB HERE/ r $BODY" "$CL" | 
|  | 249 | sed -i "/BLURB HERE/ d" "$CL" | 
|  | 250 | fi | 
|  | 251 |  | 
|  | 252 | # If the user specified a subject, replace the SUBJECT token with it. | 
|  | 253 | if [ -n "$SUBJECT" ]; then | 
|  | 254 | sed -i -e "s/\*\*\* SUBJECT HERE \*\*\*/$SUBJECT/" "$CL" | 
|  | 255 | fi | 
|  | 256 |  | 
|  | 257 |  | 
|  | 258 | # Generate report for user | 
|  | 259 | cat <<EOM | 
|  | 260 | The following patches have been prepared: | 
|  | 261 | $(for PATCH in $(ls $ODIR/*); do echo "    $PATCH"; done) | 
|  | 262 |  | 
|  | 263 | Review their content, especially the summary mail: | 
|  | 264 | $CL | 
|  | 265 |  | 
|  | 266 | When you are satisfied, you can send them with: | 
|  | 267 | send-pull-request -a -p $ODIR | 
|  | 268 | EOM | 
|  | 269 |  | 
|  | 270 | # Check the patches for trailing white space | 
|  | 271 | egrep -q -e "^\+.*\s+$" $ODIR/* | 
|  | 272 | if [ $? -ne 1 ]; then | 
|  | 273 | echo | 
|  | 274 | echo "WARNING: Trailing white space detected at these locations" | 
|  | 275 | egrep -nH --color -e "^\+.*\s+$" $ODIR/* | 
|  | 276 | fi |