blob: a88f35a4a3936c95b7225dffaaab67f8cf294edd [file] [log] [blame]
Patrick Williamsc124f4f2015-09-15 14:41:29 -05001#!/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
28ODIR=pull-$$
29RELATIVE_TO="master"
30COMMIT_ID="HEAD"
31PREFIX="PATCH"
32RFC=0
33
34usage() {
35CMD=$(basename $0)
36cat <<EOM
37Usage: $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 Williamsd8c66bc2016-06-20 12:57:21 -050039 -l local branch Local branch name (default: HEAD)
Patrick Williamsc124f4f2015-09-15 14:41:29 -050040 -c Create an RFC (Request for Comment) patch series
41 -h Display this help message
Patrick Williamsc0f7c042017-02-23 20:41:17 -060042 -a Automatically push local branch (-l) to remote branch (-b),
43 or set CPR_CONTRIB_AUTO_PUSH in env
Patrick Williamsc124f4f2015-09-15 14:41:29 -050044 -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 Williamsc0f7c042017-02-23 20:41:17 -060050 -u remote The git remote where the branch is located, or set CPR_CONTRIB_REMOTE in env
Patrick Williamsc124f4f2015-09-15 14:41:29 -050051 -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 Williamsd8c66bc2016-06-20 12:57:21 -050056 $CMD -u contrib -r distro/master -i nitin/distro -b nitin/distro -l distro
Patrick Williamsc124f4f2015-09-15 14:41:29 -050057 $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
60EOM
61}
62
Patrick Williamsc0f7c042017-02-23 20:41:17 -060063REMOTE="$CPR_CONTRIB_REMOTE"
Patrick Williamsc124f4f2015-09-15 14:41:29 -050064# Parse and validate arguments
Patrick Williamsc0f7c042017-02-23 20:41:17 -060065while getopts "b:acd:hi:m:o:p:r:s:u:l:" OPT; do
Patrick Williamsc124f4f2015-09-15 14:41:29 -050066 case $OPT in
67 b)
68 BRANCH="$OPTARG"
69 ;;
Patrick Williamsd8c66bc2016-06-20 12:57:21 -050070 l)
71 L_BRANCH="$OPTARG"
72 ;;
Patrick Williamsc124f4f2015-09-15 14:41:29 -050073 c)
74 RFC=1
75 ;;
76 d)
77 RELDIR="$OPTARG"
78 ;;
79 h)
80 usage
81 exit 0
82 ;;
83 i)
84 COMMIT_ID="$OPTARG"
85 ;;
86 m)
87 BODY="$OPTARG"
88 if [ ! -e "$BODY" ]; then
89 echo "ERROR: Body file does not exist"
90 exit 1
91 fi
92 ;;
93 o)
94 ODIR="$OPTARG"
95 ;;
96 p)
97 PREFIX="$OPTARG"
98 ;;
99 r)
100 RELATIVE_TO="$OPTARG"
101 ;;
102 s)
103 SUBJECT="$OPTARG"
104 ;;
105 u)
106 REMOTE="$OPTARG"
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600107 ;;
108 a)
109 CPR_CONTRIB_AUTO_PUSH="1"
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500110 ;;
111 esac
112done
113
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600114if [ -z "$REMOTE" ]; then
115 echo "ERROR: Missing parameter -u or CPR_CONTRIB_REMOTE in env, no git remote!"
116 usage
117 exit 1
118fi
119
120REMOTE_URL=$(git config remote.$REMOTE.url)
121if [ $? -ne 0 ]; then
122 echo "ERROR: git config failed to find a url for '$REMOTE'"
123 echo
124 echo "To add a remote url for $REMOTE, use:"
125 echo " git config remote.$REMOTE.url <url>"
126 exit 1
127fi
128
129# Rewrite private URLs to public URLs
130# Determine the repository name for use in the WEB_URL later
131case "$REMOTE_URL" in
132*@*)
133 USER_RE="[A-Za-z0-9_.@][A-Za-z0-9_.@-]*\$\?"
134 PROTO_RE="[a-z][a-z+]*://"
135 GIT_RE="\(^\($PROTO_RE\)\?$USER_RE@\)\([^:/]*\)[:/]\(.*\)"
136 REMOTE_URL=${REMOTE_URL%.git}
137 REMOTE_REPO=$(echo $REMOTE_URL | sed "s#$GIT_RE#\4#")
138 REMOTE_URL=$(echo $REMOTE_URL | sed "s#$GIT_RE#git://\3/\4#")
139 ;;
140*)
141 echo "WARNING: Unrecognized remote URL: $REMOTE_URL"
142 echo " The pull and browse URLs will likely be incorrect"
143 ;;
144esac
145
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500146if [ -z "$BRANCH" ]; then
147 BRANCH=$(git branch | grep -e "^\* " | cut -d' ' -f2)
148 echo "NOTE: Assuming remote branch '$BRANCH', use -b to override."
149fi
150
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500151if [ -z "$L_BRANCH" ]; then
152 L_BRANCH=HEAD
153 echo "NOTE: Assuming local branch HEAD, use -l to override."
154fi
155
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500156if [ $RFC -eq 1 ]; then
157 PREFIX="RFC $PREFIX"
158fi
159
160
161# Set WEB_URL from known remotes
162WEB_URL=""
163case "$REMOTE_URL" in
164 *git.yoctoproject.org*)
165 WEB_URL="http://git.yoctoproject.org/cgit.cgi/$REMOTE_REPO/log/?h=$BRANCH"
166 ;;
167 *git.pokylinux.org*)
168 WEB_URL="http://git.pokylinux.org/cgit.cgi/$REMOTE_REPO/log/?h=$BRANCH"
169 ;;
170 *git.openembedded.org*)
171 WEB_URL="http://cgit.openembedded.org/cgit.cgi/$REMOTE_REPO/log/?h=$BRANCH"
172 ;;
173 *github.com*)
174 WEB_URL="https://github.com/$REMOTE_REPO/tree/$BRANCH"
175 ;;
176esac
177
178# Perform a sanity test on the web URL. Issue a warning if it is not
179# accessible, but do not abort as users may want to run offline.
180if [ -n "$WEB_URL" ]; then
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600181 if [ "$CPR_CONTRIB_AUTO_PUSH" = "1" ]; then
182 echo "Pushing '$BRANCH' on '$REMOTE' as requested..."
183 git push $REMOTE $L_BRANCH:$BRANCH
184 echo ""
185 fi
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500186 wget --no-check-certificate -q $WEB_URL -O /dev/null
187 if [ $? -ne 0 ]; then
188 echo "WARNING: Branch '$BRANCH' was not found on the contrib git tree."
189 echo " Please check your remote and branch parameter before sending."
190 echo ""
191 fi
192fi
193
194if [ -e $ODIR ]; then
195 echo "ERROR: output directory $ODIR exists."
196 exit 1
197fi
198mkdir $ODIR
199
200if [ -n "$RELDIR" ]; then
201 ODIR=$(realpath $ODIR)
202 pdir=$(pwd)
203 cd $RELDIR
204 extraopts="--relative"
205fi
206
207# Generate the patches and cover letter
208git format-patch $extraopts -M40 --subject-prefix="$PREFIX" -n -o $ODIR --thread=shallow --cover-letter $RELATIVE_TO..$COMMIT_ID > /dev/null
209
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500210if [ -z "$(ls -A $ODIR 2> /dev/null)" ]; then
211 echo "ERROR: $ODIR is empty, no cover letter and patches was generated!"
212 echo " This is most likely due to that \$RRELATIVE_TO..\$COMMIT_ID"
213 echo " ($RELATIVE_TO..$COMMIT_ID) don't contain any differences."
214 rmdir $ODIR
215 exit 1
216fi
217
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500218[ -n "$RELDIR" ] && cd $pdir
219
220# Customize the cover letter
221CL="$ODIR/0000-cover-letter.patch"
222PM="$ODIR/pull-msg"
223GIT_VERSION=$(`git --version` | tr -d '[:alpha:][:space:].' | sed 's/\(...\).*/\1/')
224NEWER_GIT_VERSION=210
225if [ $GIT_VERSION -lt $NEWER_GIT_VERSION ]; then
226 git request-pull $RELATIVE_TO $REMOTE_URL $COMMIT_ID >> "$PM"
227else
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500228 git request-pull $RELATIVE_TO $REMOTE_URL $L_BRANCH:$BRANCH >> "$PM"
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500229fi
230if [ $? -ne 0 ]; then
231 echo "ERROR: git request-pull reported an error"
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600232 rm -rf $ODIR
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500233 exit 1
234fi
235
236# The cover letter already has a diffstat, remove it from the pull-msg
237# before inserting it.
238sed -n "0,\#$REMOTE_URL# p" "$PM" | sed -i "/BLURB HERE/ r /dev/stdin" "$CL"
239rm "$PM"
240
241# If this is an RFC, make that clear in the cover letter
242if [ $RFC -eq 1 ]; then
243(cat <<EOM
244Please review the following changes for suitability for inclusion. If you have
245any objections or suggestions for improvement, please respond to the patches. If
246you agree with the changes, please provide your Acked-by.
247
248EOM
249) | sed -i "/BLURB HERE/ r /dev/stdin" "$CL"
250fi
251
252# Insert the WEB_URL if there is one
253if [ -n "$WEB_URL" ]; then
254 echo " $WEB_URL" | sed -i "\#$REMOTE_URL# r /dev/stdin" "$CL"
255fi
256
257
258# If the user specified a message body, insert it into the cover letter and
259# remove the BLURB token.
260if [ -n "$BODY" ]; then
261 sed -i "/BLURB HERE/ r $BODY" "$CL"
262 sed -i "/BLURB HERE/ d" "$CL"
263fi
264
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600265# Set subject automatically if there is only one patch
266patch_cnt=`git log --pretty=oneline ${RELATIVE_TO}..${L_BRANCH} | wc -l`
267if [ -z "$SUBJECT" -a $patch_cnt -eq 1 ]; then
268 SUBJECT="`git log --format=%s ${RELATIVE_TO}..${L_BRANCH}`"
269fi
270
271# Replace the SUBJECT token with it.
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500272if [ -n "$SUBJECT" ]; then
273 sed -i -e "s/\*\*\* SUBJECT HERE \*\*\*/$SUBJECT/" "$CL"
274fi
275
276
277# Generate report for user
278cat <<EOM
279The following patches have been prepared:
280$(for PATCH in $(ls $ODIR/*); do echo " $PATCH"; done)
281
282Review their content, especially the summary mail:
283 $CL
284
285When you are satisfied, you can send them with:
286 send-pull-request -a -p $ODIR
287EOM
288
289# Check the patches for trailing white space
290egrep -q -e "^\+.*\s+$" $ODIR/*
291if [ $? -ne 1 ]; then
292 echo
293 echo "WARNING: Trailing white space detected at these locations"
294 egrep -nH --color -e "^\+.*\s+$" $ODIR/*
295fi