blob: 3ce03d6c1a07bbb8ee50c33146d9d807a1064fd2 [file] [log] [blame]
Patrick Williamsc124f4f2015-09-15 14:41:29 -05001#!/bin/sh
2#
3# Copyright (c) 2010-2013, Intel Corporation.
Patrick Williamsc124f4f2015-09-15 14:41:29 -05004#
Brad Bishopc342db32019-05-15 21:57:59 -04005# SPDX-License-Identifier: GPL-2.0-or-later
Patrick Williamsc124f4f2015-09-15 14:41:29 -05006#
7
8#
9# This script is intended to be used to prepare a series of patches
10# and a cover letter in an appropriate and consistent format for
11# submission to Open Embedded and The Yocto Project, as well as to
12# related projects and layers.
13#
14
15ODIR=pull-$$
16RELATIVE_TO="master"
17COMMIT_ID="HEAD"
18PREFIX="PATCH"
19RFC=0
20
21usage() {
22CMD=$(basename $0)
23cat <<EOM
Brad Bishopd7bf8c12018-02-25 22:55:05 -050024Usage: $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 Williamsc124f4f2015-09-15 14:41:29 -050025 -b branch Branch name in the specified remote (default: current branch)
Patrick Williamsd8c66bc2016-06-20 12:57:21 -050026 -l local branch Local branch name (default: HEAD)
Patrick Williamsc124f4f2015-09-15 14:41:29 -050027 -c Create an RFC (Request for Comment) patch series
28 -h Display this help message
Patrick Williamsc0f7c042017-02-23 20:41:17 -060029 -a Automatically push local branch (-l) to remote branch (-b),
30 or set CPR_CONTRIB_AUTO_PUSH in env
Patrick Williamsc124f4f2015-09-15 14:41:29 -050031 -i commit_id Ending commit (default: HEAD)
32 -m msg_body_file The file containing a blurb to be inserted into the summary email
33 -o output_dir Specify the output directory for the messages (default: pull-PID)
34 -p prefix Use [prefix N/M] instead of [PATCH N/M] as the subject prefix
35 -r relative_to Starting commit (default: master)
36 -s subject The subject to be inserted into the summary email
Patrick Williamsc0f7c042017-02-23 20:41:17 -060037 -u remote The git remote where the branch is located, or set CPR_CONTRIB_REMOTE in env
Patrick Williamsc124f4f2015-09-15 14:41:29 -050038 -d relative_dir Generate patches relative to directory
39
40 Examples:
41 $CMD -u contrib -b nitin/basic
42 $CMD -u contrib -r distro/master -i nitin/distro -b nitin/distro
Patrick Williamsd8c66bc2016-06-20 12:57:21 -050043 $CMD -u contrib -r distro/master -i nitin/distro -b nitin/distro -l distro
Patrick Williamsc124f4f2015-09-15 14:41:29 -050044 $CMD -u contrib -r master -i misc -b nitin/misc -o pull-misc
45 $CMD -u contrib -p "RFC PATCH" -b nitin/experimental
46 $CMD -u contrib -i misc -b nitin/misc -d ./bitbake
Brad Bishopd7bf8c12018-02-25 22:55:05 -050047 $CMD -u contrib -r origin/master -o /tmp/out.v3 -- -v3 --in-reply-to=20170511120134.XX7799@site.com
Patrick Williamsc124f4f2015-09-15 14:41:29 -050048EOM
49}
50
Patrick Williamsc0f7c042017-02-23 20:41:17 -060051REMOTE="$CPR_CONTRIB_REMOTE"
Patrick Williamsc124f4f2015-09-15 14:41:29 -050052# Parse and validate arguments
Patrick Williamsc0f7c042017-02-23 20:41:17 -060053while getopts "b:acd:hi:m:o:p:r:s:u:l:" OPT; do
Patrick Williamsc124f4f2015-09-15 14:41:29 -050054 case $OPT in
55 b)
56 BRANCH="$OPTARG"
57 ;;
Patrick Williamsd8c66bc2016-06-20 12:57:21 -050058 l)
59 L_BRANCH="$OPTARG"
60 ;;
Patrick Williamsc124f4f2015-09-15 14:41:29 -050061 c)
62 RFC=1
63 ;;
64 d)
65 RELDIR="$OPTARG"
66 ;;
67 h)
68 usage
69 exit 0
70 ;;
71 i)
72 COMMIT_ID="$OPTARG"
73 ;;
74 m)
75 BODY="$OPTARG"
76 if [ ! -e "$BODY" ]; then
77 echo "ERROR: Body file does not exist"
78 exit 1
79 fi
80 ;;
81 o)
82 ODIR="$OPTARG"
83 ;;
84 p)
85 PREFIX="$OPTARG"
86 ;;
87 r)
88 RELATIVE_TO="$OPTARG"
89 ;;
90 s)
91 SUBJECT="$OPTARG"
92 ;;
93 u)
94 REMOTE="$OPTARG"
Patrick Williamsc0f7c042017-02-23 20:41:17 -060095 ;;
96 a)
97 CPR_CONTRIB_AUTO_PUSH="1"
Patrick Williamsc124f4f2015-09-15 14:41:29 -050098 ;;
Brad Bishopd7bf8c12018-02-25 22:55:05 -050099 --)
100 shift
101 break
102 ;;
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500103 esac
104done
105
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500106shift "$((OPTIND - 1))"
107extraopts="$@"
108
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600109if [ -z "$REMOTE" ]; then
110 echo "ERROR: Missing parameter -u or CPR_CONTRIB_REMOTE in env, no git remote!"
111 usage
112 exit 1
113fi
114
115REMOTE_URL=$(git config remote.$REMOTE.url)
116if [ $? -ne 0 ]; then
117 echo "ERROR: git config failed to find a url for '$REMOTE'"
118 echo
119 echo "To add a remote url for $REMOTE, use:"
120 echo " git config remote.$REMOTE.url <url>"
121 exit 1
122fi
123
124# Rewrite private URLs to public URLs
125# Determine the repository name for use in the WEB_URL later
126case "$REMOTE_URL" in
127*@*)
128 USER_RE="[A-Za-z0-9_.@][A-Za-z0-9_.@-]*\$\?"
129 PROTO_RE="[a-z][a-z+]*://"
130 GIT_RE="\(^\($PROTO_RE\)\?$USER_RE@\)\([^:/]*\)[:/]\(.*\)"
131 REMOTE_URL=${REMOTE_URL%.git}
132 REMOTE_REPO=$(echo $REMOTE_URL | sed "s#$GIT_RE#\4#")
133 REMOTE_URL=$(echo $REMOTE_URL | sed "s#$GIT_RE#git://\3/\4#")
134 ;;
135*)
136 echo "WARNING: Unrecognized remote URL: $REMOTE_URL"
137 echo " The pull and browse URLs will likely be incorrect"
138 ;;
139esac
140
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500141if [ -z "$BRANCH" ]; then
142 BRANCH=$(git branch | grep -e "^\* " | cut -d' ' -f2)
143 echo "NOTE: Assuming remote branch '$BRANCH', use -b to override."
144fi
145
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500146if [ -z "$L_BRANCH" ]; then
147 L_BRANCH=HEAD
148 echo "NOTE: Assuming local branch HEAD, use -l to override."
149fi
150
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500151if [ $RFC -eq 1 ]; then
152 PREFIX="RFC $PREFIX"
153fi
154
155
156# Set WEB_URL from known remotes
157WEB_URL=""
158case "$REMOTE_URL" in
159 *git.yoctoproject.org*)
160 WEB_URL="http://git.yoctoproject.org/cgit.cgi/$REMOTE_REPO/log/?h=$BRANCH"
161 ;;
162 *git.pokylinux.org*)
163 WEB_URL="http://git.pokylinux.org/cgit.cgi/$REMOTE_REPO/log/?h=$BRANCH"
164 ;;
165 *git.openembedded.org*)
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500166 WEB_URL="http://cgit.openembedded.org/$REMOTE_REPO/log/?h=$BRANCH"
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500167 ;;
168 *github.com*)
169 WEB_URL="https://github.com/$REMOTE_REPO/tree/$BRANCH"
170 ;;
171esac
172
173# Perform a sanity test on the web URL. Issue a warning if it is not
174# accessible, but do not abort as users may want to run offline.
175if [ -n "$WEB_URL" ]; then
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600176 if [ "$CPR_CONTRIB_AUTO_PUSH" = "1" ]; then
177 echo "Pushing '$BRANCH' on '$REMOTE' as requested..."
178 git push $REMOTE $L_BRANCH:$BRANCH
179 echo ""
180 fi
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500181 wget --no-check-certificate -q $WEB_URL -O /dev/null
182 if [ $? -ne 0 ]; then
183 echo "WARNING: Branch '$BRANCH' was not found on the contrib git tree."
184 echo " Please check your remote and branch parameter before sending."
185 echo ""
186 fi
187fi
188
189if [ -e $ODIR ]; then
190 echo "ERROR: output directory $ODIR exists."
191 exit 1
192fi
193mkdir $ODIR
194
195if [ -n "$RELDIR" ]; then
196 ODIR=$(realpath $ODIR)
197 pdir=$(pwd)
198 cd $RELDIR
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500199 extraopts="$extraopts --relative"
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500200fi
201
202# Generate the patches and cover letter
203git format-patch $extraopts -M40 --subject-prefix="$PREFIX" -n -o $ODIR --thread=shallow --cover-letter $RELATIVE_TO..$COMMIT_ID > /dev/null
204
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500205if [ -z "$(ls -A $ODIR 2> /dev/null)" ]; then
206 echo "ERROR: $ODIR is empty, no cover letter and patches was generated!"
207 echo " This is most likely due to that \$RRELATIVE_TO..\$COMMIT_ID"
208 echo " ($RELATIVE_TO..$COMMIT_ID) don't contain any differences."
209 rmdir $ODIR
210 exit 1
211fi
212
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500213[ -n "$RELDIR" ] && cd $pdir
214
215# Customize the cover letter
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500216CL="$(echo $ODIR/*0000-cover-letter.patch)"
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500217PM="$ODIR/pull-msg"
218GIT_VERSION=$(`git --version` | tr -d '[:alpha:][:space:].' | sed 's/\(...\).*/\1/')
219NEWER_GIT_VERSION=210
220if [ $GIT_VERSION -lt $NEWER_GIT_VERSION ]; then
221 git request-pull $RELATIVE_TO $REMOTE_URL $COMMIT_ID >> "$PM"
222else
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500223 git request-pull $RELATIVE_TO $REMOTE_URL $L_BRANCH:$BRANCH >> "$PM"
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500224fi
225if [ $? -ne 0 ]; then
226 echo "ERROR: git request-pull reported an error"
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600227 rm -rf $ODIR
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500228 exit 1
229fi
230
231# The cover letter already has a diffstat, remove it from the pull-msg
232# before inserting it.
233sed -n "0,\#$REMOTE_URL# p" "$PM" | sed -i "/BLURB HERE/ r /dev/stdin" "$CL"
234rm "$PM"
235
236# If this is an RFC, make that clear in the cover letter
237if [ $RFC -eq 1 ]; then
238(cat <<EOM
239Please review the following changes for suitability for inclusion. If you have
240any objections or suggestions for improvement, please respond to the patches. If
241you agree with the changes, please provide your Acked-by.
242
243EOM
244) | sed -i "/BLURB HERE/ r /dev/stdin" "$CL"
245fi
246
247# Insert the WEB_URL if there is one
248if [ -n "$WEB_URL" ]; then
249 echo " $WEB_URL" | sed -i "\#$REMOTE_URL# r /dev/stdin" "$CL"
250fi
251
252
253# If the user specified a message body, insert it into the cover letter and
254# remove the BLURB token.
255if [ -n "$BODY" ]; then
256 sed -i "/BLURB HERE/ r $BODY" "$CL"
257 sed -i "/BLURB HERE/ d" "$CL"
258fi
259
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600260# Set subject automatically if there is only one patch
261patch_cnt=`git log --pretty=oneline ${RELATIVE_TO}..${L_BRANCH} | wc -l`
262if [ -z "$SUBJECT" -a $patch_cnt -eq 1 ]; then
263 SUBJECT="`git log --format=%s ${RELATIVE_TO}..${L_BRANCH}`"
264fi
265
266# Replace the SUBJECT token with it.
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500267if [ -n "$SUBJECT" ]; then
268 sed -i -e "s/\*\*\* SUBJECT HERE \*\*\*/$SUBJECT/" "$CL"
269fi
270
271
272# Generate report for user
273cat <<EOM
274The following patches have been prepared:
275$(for PATCH in $(ls $ODIR/*); do echo " $PATCH"; done)
276
277Review their content, especially the summary mail:
278 $CL
279
280When you are satisfied, you can send them with:
281 send-pull-request -a -p $ODIR
282EOM
283
284# Check the patches for trailing white space
285egrep -q -e "^\+.*\s+$" $ODIR/*
286if [ $? -ne 1 ]; then
287 echo
288 echo "WARNING: Trailing white space detected at these locations"
289 egrep -nH --color -e "^\+.*\s+$" $ODIR/*
290fi