create-pull-request 8.4 KB

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