create-pull-request 7.6 KB

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