create-pull-request 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  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#https://\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="https://git.yoctoproject.org/$REMOTE_REPO/log/?h=$BRANCH"
  139. ;;
  140. *git.openembedded.org*)
  141. WEB_URL="https://git.openembedded.org/$REMOTE_REPO/log/?h=$BRANCH"
  142. ;;
  143. *github.com*)
  144. WEB_URL="https://github.com/$REMOTE_REPO/tree/$BRANCH"
  145. ;;
  146. esac
  147. # Perform a sanity test on the web URL. Issue a warning if it is not
  148. # accessible, but do not abort as users may want to run offline.
  149. if [ -n "$WEB_URL" ]; then
  150. if [ "$CPR_CONTRIB_AUTO_PUSH" = "1" ]; then
  151. echo "Pushing '$BRANCH' on '$REMOTE' as requested..."
  152. git push $REMOTE $L_BRANCH:$BRANCH
  153. echo ""
  154. fi
  155. wget --no-check-certificate -q $WEB_URL -O /dev/null
  156. if [ $? -ne 0 ]; then
  157. echo "WARNING: Branch '$BRANCH' was not found on the contrib git tree."
  158. echo " Please check your remote and branch parameter before sending."
  159. echo ""
  160. fi
  161. fi
  162. if [ -e $ODIR ]; then
  163. echo "ERROR: output directory $ODIR exists."
  164. exit 1
  165. fi
  166. mkdir $ODIR
  167. if [ -n "$RELDIR" ]; then
  168. ODIR=$(realpath $ODIR)
  169. pdir=$(pwd)
  170. cd $RELDIR
  171. extraopts="$extraopts --relative"
  172. fi
  173. # Generate the patches and cover letter
  174. git format-patch $extraopts -M40 --subject-prefix="$PREFIX" -n -o $ODIR --thread=shallow --cover-letter $RELATIVE_TO..$COMMIT_ID > /dev/null
  175. if [ -z "$(ls -A $ODIR 2> /dev/null)" ]; then
  176. echo "ERROR: $ODIR is empty, no cover letter and patches was generated!"
  177. echo " This is most likely due to that \$RRELATIVE_TO..\$COMMIT_ID"
  178. echo " ($RELATIVE_TO..$COMMIT_ID) don't contain any differences."
  179. rmdir $ODIR
  180. exit 1
  181. fi
  182. [ -n "$RELDIR" ] && cd $pdir
  183. # Customize the cover letter
  184. CL="$(echo $ODIR/*0000-cover-letter.patch)"
  185. PM="$ODIR/pull-msg"
  186. GIT_VERSION=$(`git --version` | tr -d '[:alpha:][:space:].' | sed 's/\(...\).*/\1/')
  187. NEWER_GIT_VERSION=210
  188. if [ $GIT_VERSION -lt $NEWER_GIT_VERSION ]; then
  189. git request-pull $RELATIVE_TO $REMOTE_URL $COMMIT_ID >> "$PM"
  190. else
  191. git request-pull $RELATIVE_TO $REMOTE_URL $L_BRANCH:$BRANCH >> "$PM"
  192. fi
  193. if [ $? -ne 0 ]; then
  194. echo "ERROR: git request-pull reported an error"
  195. rm -rf $ODIR
  196. exit 1
  197. fi
  198. # The cover letter already has a diffstat, remove it from the pull-msg
  199. # before inserting it.
  200. sed -n "0,\#$REMOTE_URL# p" "$PM" | sed -i "/BLURB HERE/ r /dev/stdin" "$CL"
  201. rm "$PM"
  202. # If this is an RFC, make that clear in the cover letter
  203. if [ $RFC -eq 1 ]; then
  204. (cat <<EOM
  205. Please review the following changes for suitability for inclusion. If you have
  206. any objections or suggestions for improvement, please respond to the patches. If
  207. you agree with the changes, please provide your Acked-by.
  208. EOM
  209. ) | sed -i "/BLURB HERE/ r /dev/stdin" "$CL"
  210. fi
  211. # Insert the WEB_URL if there is one
  212. if [ -n "$WEB_URL" ]; then
  213. echo " $WEB_URL" | sed -i "\#$REMOTE_URL# r /dev/stdin" "$CL"
  214. fi
  215. # If the user specified a message body, insert it into the cover letter and
  216. # remove the BLURB token.
  217. if [ -n "$BODY" ]; then
  218. sed -i "/BLURB HERE/ r $BODY" "$CL"
  219. sed -i "/BLURB HERE/ d" "$CL"
  220. fi
  221. # Set subject automatically if there is only one patch
  222. patch_cnt=`git log --pretty=oneline ${RELATIVE_TO}..${L_BRANCH} | wc -l`
  223. if [ -z "$SUBJECT" -a $patch_cnt -eq 1 ]; then
  224. SUBJECT="`git log --format=%s ${RELATIVE_TO}..${L_BRANCH}`"
  225. fi
  226. # 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