create-pull-request 7.6 KB

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