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 |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 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] [-- <format-patch options>] |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 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 |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 42 | -a Automatically push local branch (-l) to remote branch (-b), |
| 43 | or set CPR_CONTRIB_AUTO_PUSH in env |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 44 | -i commit_id Ending commit (default: HEAD) |
| 45 | -m msg_body_file The file containing a blurb to be inserted into the summary email |
| 46 | -o output_dir Specify the output directory for the messages (default: pull-PID) |
| 47 | -p prefix Use [prefix N/M] instead of [PATCH N/M] as the subject prefix |
| 48 | -r relative_to Starting commit (default: master) |
| 49 | -s subject The subject to be inserted into the summary email |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 50 | -u remote The git remote where the branch is located, or set CPR_CONTRIB_REMOTE in env |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 51 | -d relative_dir Generate patches relative to directory |
| 52 | |
| 53 | Examples: |
| 54 | $CMD -u contrib -b nitin/basic |
| 55 | $CMD -u contrib -r distro/master -i nitin/distro -b nitin/distro |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 56 | $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] | 57 | $CMD -u contrib -r master -i misc -b nitin/misc -o pull-misc |
| 58 | $CMD -u contrib -p "RFC PATCH" -b nitin/experimental |
| 59 | $CMD -u contrib -i misc -b nitin/misc -d ./bitbake |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 60 | $CMD -u contrib -r origin/master -o /tmp/out.v3 -- -v3 --in-reply-to=20170511120134.XX7799@site.com |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 61 | EOM |
| 62 | } |
| 63 | |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 64 | REMOTE="$CPR_CONTRIB_REMOTE" |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 65 | # Parse and validate arguments |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 66 | while getopts "b:acd:hi:m:o:p:r:s:u:l:" OPT; do |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 67 | case $OPT in |
| 68 | b) |
| 69 | BRANCH="$OPTARG" |
| 70 | ;; |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 71 | l) |
| 72 | L_BRANCH="$OPTARG" |
| 73 | ;; |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 74 | c) |
| 75 | RFC=1 |
| 76 | ;; |
| 77 | d) |
| 78 | RELDIR="$OPTARG" |
| 79 | ;; |
| 80 | h) |
| 81 | usage |
| 82 | exit 0 |
| 83 | ;; |
| 84 | i) |
| 85 | COMMIT_ID="$OPTARG" |
| 86 | ;; |
| 87 | m) |
| 88 | BODY="$OPTARG" |
| 89 | if [ ! -e "$BODY" ]; then |
| 90 | echo "ERROR: Body file does not exist" |
| 91 | exit 1 |
| 92 | fi |
| 93 | ;; |
| 94 | o) |
| 95 | ODIR="$OPTARG" |
| 96 | ;; |
| 97 | p) |
| 98 | PREFIX="$OPTARG" |
| 99 | ;; |
| 100 | r) |
| 101 | RELATIVE_TO="$OPTARG" |
| 102 | ;; |
| 103 | s) |
| 104 | SUBJECT="$OPTARG" |
| 105 | ;; |
| 106 | u) |
| 107 | REMOTE="$OPTARG" |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 108 | ;; |
| 109 | a) |
| 110 | CPR_CONTRIB_AUTO_PUSH="1" |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 111 | ;; |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 112 | --) |
| 113 | shift |
| 114 | break |
| 115 | ;; |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 116 | esac |
| 117 | done |
| 118 | |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 119 | shift "$((OPTIND - 1))" |
| 120 | extraopts="$@" |
| 121 | |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 122 | if [ -z "$REMOTE" ]; then |
| 123 | echo "ERROR: Missing parameter -u or CPR_CONTRIB_REMOTE in env, no git remote!" |
| 124 | usage |
| 125 | exit 1 |
| 126 | fi |
| 127 | |
| 128 | REMOTE_URL=$(git config remote.$REMOTE.url) |
| 129 | if [ $? -ne 0 ]; then |
| 130 | echo "ERROR: git config failed to find a url for '$REMOTE'" |
| 131 | echo |
| 132 | echo "To add a remote url for $REMOTE, use:" |
| 133 | echo " git config remote.$REMOTE.url <url>" |
| 134 | exit 1 |
| 135 | fi |
| 136 | |
| 137 | # Rewrite private URLs to public URLs |
| 138 | # Determine the repository name for use in the WEB_URL later |
| 139 | case "$REMOTE_URL" in |
| 140 | *@*) |
| 141 | USER_RE="[A-Za-z0-9_.@][A-Za-z0-9_.@-]*\$\?" |
| 142 | PROTO_RE="[a-z][a-z+]*://" |
| 143 | GIT_RE="\(^\($PROTO_RE\)\?$USER_RE@\)\([^:/]*\)[:/]\(.*\)" |
| 144 | REMOTE_URL=${REMOTE_URL%.git} |
| 145 | REMOTE_REPO=$(echo $REMOTE_URL | sed "s#$GIT_RE#\4#") |
| 146 | REMOTE_URL=$(echo $REMOTE_URL | sed "s#$GIT_RE#git://\3/\4#") |
| 147 | ;; |
| 148 | *) |
| 149 | echo "WARNING: Unrecognized remote URL: $REMOTE_URL" |
| 150 | echo " The pull and browse URLs will likely be incorrect" |
| 151 | ;; |
| 152 | esac |
| 153 | |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 154 | if [ -z "$BRANCH" ]; then |
| 155 | BRANCH=$(git branch | grep -e "^\* " | cut -d' ' -f2) |
| 156 | echo "NOTE: Assuming remote branch '$BRANCH', use -b to override." |
| 157 | fi |
| 158 | |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 159 | if [ -z "$L_BRANCH" ]; then |
| 160 | L_BRANCH=HEAD |
| 161 | echo "NOTE: Assuming local branch HEAD, use -l to override." |
| 162 | fi |
| 163 | |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 164 | if [ $RFC -eq 1 ]; then |
| 165 | PREFIX="RFC $PREFIX" |
| 166 | fi |
| 167 | |
| 168 | |
| 169 | # Set WEB_URL from known remotes |
| 170 | WEB_URL="" |
| 171 | case "$REMOTE_URL" in |
| 172 | *git.yoctoproject.org*) |
| 173 | WEB_URL="http://git.yoctoproject.org/cgit.cgi/$REMOTE_REPO/log/?h=$BRANCH" |
| 174 | ;; |
| 175 | *git.pokylinux.org*) |
| 176 | WEB_URL="http://git.pokylinux.org/cgit.cgi/$REMOTE_REPO/log/?h=$BRANCH" |
| 177 | ;; |
| 178 | *git.openembedded.org*) |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 179 | WEB_URL="http://cgit.openembedded.org/$REMOTE_REPO/log/?h=$BRANCH" |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 180 | ;; |
| 181 | *github.com*) |
| 182 | WEB_URL="https://github.com/$REMOTE_REPO/tree/$BRANCH" |
| 183 | ;; |
| 184 | esac |
| 185 | |
| 186 | # Perform a sanity test on the web URL. Issue a warning if it is not |
| 187 | # accessible, but do not abort as users may want to run offline. |
| 188 | if [ -n "$WEB_URL" ]; then |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 189 | if [ "$CPR_CONTRIB_AUTO_PUSH" = "1" ]; then |
| 190 | echo "Pushing '$BRANCH' on '$REMOTE' as requested..." |
| 191 | git push $REMOTE $L_BRANCH:$BRANCH |
| 192 | echo "" |
| 193 | fi |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 194 | wget --no-check-certificate -q $WEB_URL -O /dev/null |
| 195 | if [ $? -ne 0 ]; then |
| 196 | echo "WARNING: Branch '$BRANCH' was not found on the contrib git tree." |
| 197 | echo " Please check your remote and branch parameter before sending." |
| 198 | echo "" |
| 199 | fi |
| 200 | fi |
| 201 | |
| 202 | if [ -e $ODIR ]; then |
| 203 | echo "ERROR: output directory $ODIR exists." |
| 204 | exit 1 |
| 205 | fi |
| 206 | mkdir $ODIR |
| 207 | |
| 208 | if [ -n "$RELDIR" ]; then |
| 209 | ODIR=$(realpath $ODIR) |
| 210 | pdir=$(pwd) |
| 211 | cd $RELDIR |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 212 | extraopts="$extraopts --relative" |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 213 | fi |
| 214 | |
| 215 | # Generate the patches and cover letter |
| 216 | git format-patch $extraopts -M40 --subject-prefix="$PREFIX" -n -o $ODIR --thread=shallow --cover-letter $RELATIVE_TO..$COMMIT_ID > /dev/null |
| 217 | |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 218 | if [ -z "$(ls -A $ODIR 2> /dev/null)" ]; then |
| 219 | echo "ERROR: $ODIR is empty, no cover letter and patches was generated!" |
| 220 | echo " This is most likely due to that \$RRELATIVE_TO..\$COMMIT_ID" |
| 221 | echo " ($RELATIVE_TO..$COMMIT_ID) don't contain any differences." |
| 222 | rmdir $ODIR |
| 223 | exit 1 |
| 224 | fi |
| 225 | |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 226 | [ -n "$RELDIR" ] && cd $pdir |
| 227 | |
| 228 | # Customize the cover letter |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 229 | CL="$(echo $ODIR/*0000-cover-letter.patch)" |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 230 | PM="$ODIR/pull-msg" |
| 231 | GIT_VERSION=$(`git --version` | tr -d '[:alpha:][:space:].' | sed 's/\(...\).*/\1/') |
| 232 | NEWER_GIT_VERSION=210 |
| 233 | if [ $GIT_VERSION -lt $NEWER_GIT_VERSION ]; then |
| 234 | git request-pull $RELATIVE_TO $REMOTE_URL $COMMIT_ID >> "$PM" |
| 235 | else |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 236 | git request-pull $RELATIVE_TO $REMOTE_URL $L_BRANCH:$BRANCH >> "$PM" |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 237 | fi |
| 238 | if [ $? -ne 0 ]; then |
| 239 | echo "ERROR: git request-pull reported an error" |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 240 | rm -rf $ODIR |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 241 | exit 1 |
| 242 | fi |
| 243 | |
| 244 | # The cover letter already has a diffstat, remove it from the pull-msg |
| 245 | # before inserting it. |
| 246 | sed -n "0,\#$REMOTE_URL# p" "$PM" | sed -i "/BLURB HERE/ r /dev/stdin" "$CL" |
| 247 | rm "$PM" |
| 248 | |
| 249 | # If this is an RFC, make that clear in the cover letter |
| 250 | if [ $RFC -eq 1 ]; then |
| 251 | (cat <<EOM |
| 252 | Please review the following changes for suitability for inclusion. If you have |
| 253 | any objections or suggestions for improvement, please respond to the patches. If |
| 254 | you agree with the changes, please provide your Acked-by. |
| 255 | |
| 256 | EOM |
| 257 | ) | sed -i "/BLURB HERE/ r /dev/stdin" "$CL" |
| 258 | fi |
| 259 | |
| 260 | # Insert the WEB_URL if there is one |
| 261 | if [ -n "$WEB_URL" ]; then |
| 262 | echo " $WEB_URL" | sed -i "\#$REMOTE_URL# r /dev/stdin" "$CL" |
| 263 | fi |
| 264 | |
| 265 | |
| 266 | # If the user specified a message body, insert it into the cover letter and |
| 267 | # remove the BLURB token. |
| 268 | if [ -n "$BODY" ]; then |
| 269 | sed -i "/BLURB HERE/ r $BODY" "$CL" |
| 270 | sed -i "/BLURB HERE/ d" "$CL" |
| 271 | fi |
| 272 | |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 273 | # Set subject automatically if there is only one patch |
| 274 | patch_cnt=`git log --pretty=oneline ${RELATIVE_TO}..${L_BRANCH} | wc -l` |
| 275 | if [ -z "$SUBJECT" -a $patch_cnt -eq 1 ]; then |
| 276 | SUBJECT="`git log --format=%s ${RELATIVE_TO}..${L_BRANCH}`" |
| 277 | fi |
| 278 | |
| 279 | # Replace the SUBJECT token with it. |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 280 | if [ -n "$SUBJECT" ]; then |
| 281 | sed -i -e "s/\*\*\* SUBJECT HERE \*\*\*/$SUBJECT/" "$CL" |
| 282 | fi |
| 283 | |
| 284 | |
| 285 | # Generate report for user |
| 286 | cat <<EOM |
| 287 | The following patches have been prepared: |
| 288 | $(for PATCH in $(ls $ODIR/*); do echo " $PATCH"; done) |
| 289 | |
| 290 | Review their content, especially the summary mail: |
| 291 | $CL |
| 292 | |
| 293 | When you are satisfied, you can send them with: |
| 294 | send-pull-request -a -p $ODIR |
| 295 | EOM |
| 296 | |
| 297 | # Check the patches for trailing white space |
| 298 | egrep -q -e "^\+.*\s+$" $ODIR/* |
| 299 | if [ $? -ne 1 ]; then |
| 300 | echo |
| 301 | echo "WARNING: Trailing white space detected at these locations" |
| 302 | egrep -nH --color -e "^\+.*\s+$" $ODIR/* |
| 303 | fi |