create-pull-request 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. #!/bin/bash
  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. # This script is intended to be used to prepare a series of patches
  22. # and a cover letter in an appropriate and consistent format for
  23. # submission to Open Embedded and The Yocto Project, as well as to
  24. # related projects and layers.
  25. #
  26. ODIR=pull-$$
  27. RELATIVE_TO="master"
  28. COMMIT_ID="HEAD"
  29. PREFIX="PATCH"
  30. RFC=0
  31. usage() {
  32. CMD=$(basename $0)
  33. cat <<EOM
  34. Usage: $CMD [-h] [-o output_dir] [-m msg_body_file] [-s subject] [-r relative_to] [-i commit_id] -u remote [-b branch]
  35. -b branch Branch name in the specified remote (default: current branch)
  36. -c Create an RFC (Request for Comment) patch series
  37. -h Display this help message
  38. -i commit_id Ending commit (default: HEAD)
  39. -m msg_body_file The file containing a blurb to be inserted into the summary email
  40. -o output_dir Specify the output directory for the messages (default: pull-PID)
  41. -p prefix Use [prefix N/M] instead of [PATCH N/M] as the subject prefix
  42. -r relative_to Starting commit (default: master)
  43. -s subject The subject to be inserted into the summary email
  44. -u remote The git remote where the branch is located
  45. Examples:
  46. $CMD -u contrib -b nitin/basic
  47. $CMD -u contrib -r distro/master -i nitin/distro -b nitin/distro
  48. $CMD -u contrib -r master -i misc -b nitin/misc -o pull-misc
  49. $CMD -u contrib -p "RFC PATCH" -b nitin/experimental
  50. EOM
  51. }
  52. # Parse and validate arguments
  53. while getopts "b:chi:m:o:p:r:s:u:" OPT; do
  54. case $OPT in
  55. b)
  56. BRANCH="$OPTARG"
  57. ;;
  58. c)
  59. RFC=1
  60. ;;
  61. h)
  62. usage
  63. exit 0
  64. ;;
  65. i)
  66. COMMIT_ID="$OPTARG"
  67. ;;
  68. m)
  69. BODY="$OPTARG"
  70. if [ ! -e "$BODY" ]; then
  71. echo "ERROR: Body file does not exist"
  72. exit 1
  73. fi
  74. ;;
  75. o)
  76. ODIR="$OPTARG"
  77. ;;
  78. p)
  79. PREFIX="$OPTARG"
  80. ;;
  81. r)
  82. RELATIVE_TO="$OPTARG"
  83. ;;
  84. s)
  85. SUBJECT="$OPTARG"
  86. ;;
  87. u)
  88. REMOTE="$OPTARG"
  89. REMOTE_URL=$(git config remote.$REMOTE.url)
  90. if [ $? -ne 0 ]; then
  91. echo "ERROR: git config failed to find a url for '$REMOTE'"
  92. echo
  93. echo "To add a remote url for $REMOTE, use:"
  94. echo " git config remote.$REMOTE.url <url>"
  95. exit 1
  96. fi
  97. # Rewrite private URLs to public URLs
  98. # Determine the repository name for use in the WEB_URL later
  99. case "$REMOTE_URL" in
  100. *@*)
  101. USER_RE="[A-Za-z0-9_.@][A-Za-z0-9_.@-]*\$\?"
  102. PROTO_RE="[a-z][a-z+]*://"
  103. GIT_RE="\(^\($PROTO_RE\)\?$USER_RE@\)\([^:/]*\)[:/]\(.*\)"
  104. REMOTE_URL=${REMOTE_URL%.git}
  105. REMOTE_REPO=$(echo $REMOTE_URL | sed "s#$GIT_RE#\4#")
  106. REMOTE_URL=$(echo $REMOTE_URL | sed "s#$GIT_RE#git://\3/\4#")
  107. ;;
  108. *)
  109. echo "WARNING: Unrecognized remote URL: $REMOTE_URL"
  110. echo " The pull and browse URLs will likely be incorrect"
  111. ;;
  112. esac
  113. ;;
  114. esac
  115. done
  116. if [ -z "$BRANCH" ]; then
  117. BRANCH=$(git branch | grep -e "^\* " | cut -d' ' -f2)
  118. echo "NOTE: Assuming remote branch '$BRANCH', use -b to override."
  119. fi
  120. if [ -z "$REMOTE_URL" ]; then
  121. echo "ERROR: Missing parameter -u, no git remote!"
  122. usage
  123. exit 1
  124. fi
  125. if [ $RFC -eq 1 ]; then
  126. PREFIX="RFC $PREFIX"
  127. fi
  128. # Set WEB_URL from known remotes
  129. WEB_URL=""
  130. case "$REMOTE_URL" in
  131. *git.yoctoproject.org*)
  132. WEB_URL="http://git.yoctoproject.org/cgit.cgi/$REMOTE_REPO/log/?h=$BRANCH"
  133. ;;
  134. *git.pokylinux.org*)
  135. WEB_URL="http://git.pokylinux.org/cgit.cgi/$REMOTE_REPO/log/?h=$BRANCH"
  136. ;;
  137. *git.openembedded.org*)
  138. WEB_URL="http://cgit.openembedded.org/cgit.cgi/$REMOTE_REPO/log/?h=$BRANCH"
  139. ;;
  140. *github.com*)
  141. WEB_URL="https://github.com/$REMOTE_REPO/tree/$BRANCH"
  142. ;;
  143. esac
  144. # Perform a sanity test on the web URL. Issue a warning if it is not
  145. # accessible, but do not abort as users may want to run offline.
  146. if [ -n "$WEB_URL" ]; then
  147. wget --no-check-certificate -q $WEB_URL -O /dev/null
  148. if [ $? -ne 0 ]; then
  149. echo "WARNING: Branch '$BRANCH' was not found on the contrib git tree."
  150. echo " Please check your remote and branch parameter before sending."
  151. echo ""
  152. fi
  153. fi
  154. if [ -e $ODIR ]; then
  155. echo "ERROR: output directory $ODIR exists."
  156. exit 1
  157. fi
  158. mkdir $ODIR
  159. # Generate the patches and cover letter
  160. git format-patch -M40 --subject-prefix="$PREFIX" -n -o $ODIR --thread=shallow --cover-letter $RELATIVE_TO..$COMMIT_ID > /dev/null
  161. # Customize the cover letter
  162. CL="$ODIR/0000-cover-letter.patch"
  163. PM="$ODIR/pull-msg"
  164. GIT_VERSION=$(`git --version` | tr -d '[:alpha:][:space:].' | sed 's/\(...\).*/\1/')
  165. NEWER_GIT_VERSION=210
  166. if [ $GIT_VERSION -lt $NEWER_GIT_VERSION ]; then
  167. git request-pull $RELATIVE_TO $REMOTE_URL $COMMIT_ID >> "$PM"
  168. else
  169. git request-pull $RELATIVE_TO $REMOTE_URL :$BRANCH >> "$PM"
  170. fi
  171. if [ $? -ne 0 ]; then
  172. echo "ERROR: git request-pull reported an error"
  173. exit 1
  174. fi
  175. # The cover letter already has a diffstat, remove it from the pull-msg
  176. # before inserting it.
  177. sed -n "0,\#$REMOTE_URL# p" "$PM" | sed -i "/BLURB HERE/ r /dev/stdin" "$CL"
  178. rm "$PM"
  179. # If this is an RFC, make that clear in the cover letter
  180. if [ $RFC -eq 1 ]; then
  181. (cat <<EOM
  182. Please review the following changes for suitability for inclusion. If you have
  183. any objections or suggestions for improvement, please respond to the patches. If
  184. you agree with the changes, please provide your Acked-by.
  185. EOM
  186. ) | sed -i "/BLURB HERE/ r /dev/stdin" "$CL"
  187. fi
  188. # Insert the WEB_URL if there is one
  189. if [ -n "$WEB_URL" ]; then
  190. echo " $WEB_URL" | sed -i "\#$REMOTE_URL# r /dev/stdin" "$CL"
  191. fi
  192. # If the user specified a message body, insert it into the cover letter and
  193. # remove the BLURB token.
  194. if [ -n "$BODY" ]; then
  195. sed -i "/BLURB HERE/ r $BODY" "$CL"
  196. sed -i "/BLURB HERE/ d" "$CL"
  197. fi
  198. # If the user specified a subject, replace the SUBJECT token with it.
  199. if [ -n "$SUBJECT" ]; then
  200. sed -i -e "s/\*\*\* SUBJECT HERE \*\*\*/$SUBJECT/" "$CL"
  201. fi
  202. # Generate report for user
  203. cat <<EOM
  204. The following patches have been prepared:
  205. $(for PATCH in $(ls $ODIR/*); do echo " $PATCH"; done)
  206. Review their content, especially the summary mail:
  207. $CL
  208. When you are satisfied, you can send them with:
  209. send-pull-request -a -p $ODIR
  210. EOM
  211. # Check the patches for trailing white space
  212. egrep -q -e "^\+.*\s+$" $ODIR/*
  213. if [ $? -ne 1 ]; then
  214. echo
  215. echo "WARNING: Trailing white space detected at these locations"
  216. egrep -nH --color -e "^\+.*\s+$" $ODIR/*
  217. fi