diff options
-rwxr-xr-x | scripts/series-pull-request | 353 |
1 files changed, 353 insertions, 0 deletions
diff --git a/scripts/series-pull-request b/scripts/series-pull-request new file mode 100755 index 00000000000..bd493ebe07b --- /dev/null +++ b/scripts/series-pull-request @@ -0,0 +1,353 @@ +#!/bin/bash +# +# Copyright (c) 2010-2013, Intel Corporation. +# All Rights Reserved +# +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 2 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See +# the GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program; if not, write to the Free Software +# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA +# + +# +# This script is intended to be used to prepare a series of patches +# and a cover letter in an appropriate and consistent format for +# submission to Open Embedded and The Yocto Project, as well as to +# related projects and layers. +# + +ODIR=pull-$$ +RELATIVE_TO="master" +COMMIT_ID="HEAD" +PREFIX="PATCH" +RFC=0 + +usage() { +CMD=$(basename $0) +cat <<EOM +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] + -b branch Branch name in the specified remote (default: current branch) + -l local branch Local branch name (default: HEAD) + -c Create an RFC (Request for Comment) patch series + -h Display this help message + -a Automatically push local branch (-l) to remote branch (-b), + or set CPR_CONTRIB_AUTO_PUSH in env + -i commit_id Ending commit (default: HEAD) + -m msg_body_file The file containing a blurb to be inserted into the summary email + -o output_dir Specify the output directory for the messages (default: pull-PID) + -p prefix Use [prefix N/M] instead of [PATCH N/M] as the subject prefix + -r relative_to Starting commit (default: master) + -s subject The subject to be inserted into the summary email + -u remote The git remote where the branch is located, or set CPR_CONTRIB_REMOTE in env + -d relative_dir Generate patches relative to directory + -v series revision Include revision number in series patches subject as "vN" + + Examples: + $CMD -u contrib -b nitin/basic -v 2 + $CMD -u contrib -r distro/master -i nitin/distro -b nitin/distro + $CMD -u contrib -r distro/master -i nitin/distro -b nitin/distro -l distro + $CMD -u contrib -r master -i misc -b nitin/misc -o pull-misc + $CMD -u contrib -p "RFC PATCH" -b nitin/experimental + $CMD -u contrib -i misc -b nitin/misc -d ./bitbake +EOM +} + +REMOTE="$CPR_CONTRIB_REMOTE" +# Parse and validate arguments +while getopts "b:acd:hi:m:o:p:r:s:u:l:v:" OPT; do + case $OPT in + b) + BRANCH="$OPTARG" + ;; + l) + L_BRANCH="$OPTARG" + ;; + c) + RFC=1 + ;; + d) + RELDIR="$OPTARG" + ;; + h) + usage + exit 0 + ;; + i) + COMMIT_ID="$OPTARG" + ;; + m) + BODY="$OPTARG" + if [ ! -e "$BODY" ]; then + echo "ERROR: Body file does not exist" + exit 1 + fi + ;; + o) + ODIR="$OPTARG" + ;; + p) + PREFIX="$OPTARG" + ;; + r) + RELATIVE_TO="$OPTARG" + ;; + s) + SUBJECT="$OPTARG" + ;; + u) + REMOTE="$OPTARG" + ;; + a) + CPR_CONTRIB_AUTO_PUSH="1" + ;; + v) VERSION="$OPTARG" + ;; + esac +done + +if [ -z "$REMOTE" ]; then + echo "ERROR: Missing parameter -u or CPR_CONTRIB_REMOTE in env, no git remote!" + usage + exit 1 +fi + +REMOTE_URL=$(git config remote.$REMOTE.url) +if [ $? -ne 0 ]; then + echo "ERROR: git config failed to find a url for '$REMOTE'" + echo + echo "To add a remote url for $REMOTE, use:" + echo " git config remote.$REMOTE.url <url>" + exit 1 +fi + +# Rewrite private URLs to public URLs +# Determine the repository name for use in the WEB_URL later +case "$REMOTE_URL" in +*@*) + USER_RE="[A-Za-z0-9_.@][A-Za-z0-9_.@-]*\$\?" + PROTO_RE="[a-z][a-z+]*://" + GIT_RE="\(^\($PROTO_RE\)\?$USER_RE@\)\([^:/]*\)[:/]\(.*\)" + REMOTE_URL=${REMOTE_URL%.git} + REMOTE_REPO=$(echo $REMOTE_URL | sed "s#$GIT_RE#\4#") + REMOTE_URL=$(echo $REMOTE_URL | sed "s#$GIT_RE#git://\3/\4#") + ;; +*) + echo "WARNING: Unrecognized remote URL: $REMOTE_URL" + echo " The pull and browse URLs will likely be incorrect" + ;; +esac + +if [ -z "$BRANCH" ]; then + BRANCH=$(git branch | grep -e "^\* " | cut -d' ' -f2) + echo "NOTE: Assuming remote branch '$BRANCH', use -b to override." +fi + +if [ -z "$L_BRANCH" ]; then + L_BRANCH=HEAD + echo "NOTE: Assuming local branch HEAD, use -l to override." +fi + +if [ $RFC -eq 1 ]; then + PREFIX="RFC $PREFIX" +fi + + +# Set WEB_URL from known remotes +WEB_URL="" +case "$REMOTE_URL" in + *git.yoctoproject.org*) + WEB_URL="http://git.yoctoproject.org/cgit.cgi/$REMOTE_REPO/log/?h=$BRANCH" + ;; + *git.pokylinux.org*) + WEB_URL="http://git.pokylinux.org/cgit.cgi/$REMOTE_REPO/log/?h=$BRANCH" + ;; + *git.openembedded.org*) + WEB_URL="http://cgit.openembedded.org/$REMOTE_REPO/log/?h=$BRANCH" + ;; + *github.com*) + WEB_URL="https://github.com/$REMOTE_REPO/tree/$BRANCH" + ;; +esac + +# Perform a sanity test on the web URL. Issue a warning if it is not +# accessible, but do not abort as users may want to run offline. +if [ -n "$WEB_URL" ]; then + if [ "$CPR_CONTRIB_AUTO_PUSH" = "1" ]; then + echo "Pushing '$BRANCH' on '$REMOTE' as requested..." + git push $REMOTE $L_BRANCH:$BRANCH + echo "" + fi + wget --no-check-certificate -q $WEB_URL -O /dev/null + if [ $? -ne 0 ]; then + echo "WARNING: Branch '$BRANCH' was not found on the contrib git tree." + echo " Please check your remote and branch parameter before sending." + echo "" + fi +fi + +if [ -e $ODIR ]; then + echo "ERROR: output directory $ODIR exists." + exit 1 +fi +mkdir $ODIR + +if [ -n "$RELDIR" ]; then + ODIR=$(realpath $ODIR) + pdir=$(pwd) + cd $RELDIR + extraopts="--relative" +fi + +# Generate the patches and cover letter +git status > /dev/null 2>&1 +RESULT=$? +if [ $RESULT -ne 0 ]; then + echo -e "Test for \"git\" command failed. Ensure that current directory \ +is a git repository. Aborting" + exit +fi +git series > /dev/null 2>&1 +RESULT=$? +if [ $RESULT -eq 1 ]; then + echo -e "Test for \"git series\" command failed. Ensure that \"git series\ + \" is installed and included in PATH:\nInstall Rust and Cargo for your OS \ +distribution and then execute:\n\n\tcargo install --root ~/.local git-series\n\ +\nAborting current script." + exit +fi +echo -e "\nLets create the pull request and email messages for a Patch Series! +Make sure you are already at the appropriate branch." +echo -n "Enter the series name: " +# TBD: set a default series name from 1st patch's subject +read SERIES_NAME +git series checkout $SERIES_NAME > /dev/null 2>&1 +RESULT=$? +if [ $RESULT -eq 0 ]; then + echo -e "Working with existing Series \"$SERIES_NAME\"." +else + echo -e "Series \"$SERIES_NAME\" not found in current branch, do you want \ +to start a new series with this name?" + read -e -p "yes(y)/no(n): " -i "y" NEW_S + NEW_START=false + while [ $NEW_START == false ]; do + if [[ $NEW_S == "y" || $NEW_S == "yes" ]]; then + git series start "$SERIES_NAME" + echo -e "Series \"$SERIES_NAME\" created." + NEW_START=true + elif [[ $NEW_S == "n" || $NEW_S == "no" ]]; then + echo "Series work aborted." + exit + else + echo "Please type y or n" + echo -n "yes(y)/no(n): " + read NEW_S + fi + done +fi + +git series base $RELATIVE_TO +git series cover +git series commit -a -m 'Add cover letter' + +if [ -n "$VERSION" ]; then + git series format -v $VERSION --subject-prefix="$PREFIX" + mv v$VERSION*.patch $ODIR +else + git series format --subject-prefix="$PREFIX" + mv 000*.patch $ODIR +fi + +if [ -z "$(ls -A $ODIR 2> /dev/null)" ]; then + echo "ERROR: $ODIR is empty, no cover letter and patches was generated!" + echo " This is most likely due to that \$RRELATIVE_TO..\$COMMIT_ID" + echo " ($RELATIVE_TO..$COMMIT_ID) don't contain any differences." + rmdir $ODIR + git series detach + exit 1 +fi + +[ -n "$RELDIR" ] && cd $pdir +# Customize the cover letter +if [ -n "$VERSION" ]; then + CL="$ODIR/v"$VERSION"-0000-cover-letter.patch" +else + CL="$ODIR/0000-cover-letter.patch" +fi +PM="$ODIR/pull-msg" + +git series req $REMOTE_URL $BRANCH >> "$PM" +if [ $? -ne 0 ]; then + echo "ERROR: \"git series\" req reported an error" + rm -rf $ODIR + exit 1 +fi +sed -i -n '/The following/,$ p' "$PM" +sed -i '1s/^/\n/' "$PM" +sed -i -r '/(-----)+/q' "$PM" +sed -i -e "/Subject:/r./$PM" "$CL" +rm "$PM" +git series detach + +# If this is an RFC, make that clear in the cover letter +if [ $RFC -eq 1 ]; then +(cat <<EOM +Please review the following changes for suitability for inclusion. If you have +any objections or suggestions for improvement, please respond to the patches. If +you agree with the changes, please provide your Acked-by. + +EOM +) | sed -i -e "/Subject:/r./dev/stdin" "$CL" +fi + +# Insert the WEB_URL if there is one +if [ -n "$WEB_URL" ]; then + echo " $WEB_URL" | sed -i "\#$REMOTE_URL# r /dev/stdin" "$CL" +fi + + +# If the user specified a message body, insert it into the cover letter and +# remove the BLURB token. +if [ -n "$BODY" ]; then + sed -i -e "/Subject:/r./$BODY" "$CL" +fi + +# Set subject automatically if there is only one patch +patch_cnt=`git log --pretty=oneline ${RELATIVE_TO}..${L_BRANCH} | wc -l` +if [ -z "$SUBJECT" -a $patch_cnt -eq 1 ]; then + SUBJECT="`git log --format=%s ${RELATIVE_TO}..${L_BRANCH}`" +fi + +# Replace the SUBJECT token with it. +if [ -n "$SUBJECT" ]; then + sed -i -e "/Subject:/$SUBJECT/" "$CL" +fi + + +# Generate report for user +cat <<EOM +The following patches have been prepared: +$(for PATCH in $(ls $ODIR/*); do echo " $PATCH"; done) + +Review their content, especially the summary mail: + $CL + +When you are satisfied, you can send them with: + send-pull-request -a -p $ODIR +EOM + +# Check the patches for trailing white space +egrep -q -e "^\+.*\s+$" $ODIR/* +if [ $? -ne 1 ]; then + echo + echo "WARNING: Trailing white space detected at these locations" + egrep -nH --color -e "^\+.*\s+$" $ODIR/* +fi |