create-pull-request 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  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. case "$REMOTE_URL" in
  118. *@*)
  119. USER_RE="[A-Za-z0-9_.@][A-Za-z0-9_.@-]*\$\?"
  120. PROTO_RE="[a-z][a-z+]*://"
  121. GIT_RE="\(^\($PROTO_RE\)\?$USER_RE@\)\([^:/]*\)[:/]\(.*\)"
  122. REMOTE_URL=${REMOTE_URL%.git}
  123. REMOTE_REPO=$(echo $REMOTE_URL | sed "s#$GIT_RE#\4#")
  124. REMOTE_URL=$(echo $REMOTE_URL | sed "s#$GIT_RE#git://\3/\4#")
  125. ;;
  126. *)
  127. echo "WARNING: Unrecognized remote URL: $REMOTE_URL"
  128. echo " The pull and browse URLs will likely be incorrect"
  129. ;;
  130. esac
  131. if [ -z "$BRANCH" ]; then
  132. BRANCH=$(git branch | grep -e "^\* " | cut -d' ' -f2)
  133. echo "NOTE: Assuming remote branch '$BRANCH', use -b to override."
  134. fi
  135. if [ -z "$L_BRANCH" ]; then
  136. L_BRANCH=HEAD
  137. echo "NOTE: Assuming local branch HEAD, use -l to override."
  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/$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. if [ "$CPR_CONTRIB_AUTO_PUSH" = "1" ]; then
  162. echo "Pushing '$BRANCH' on '$REMOTE' as requested..."
  163. git push $REMOTE $L_BRANCH:$BRANCH
  164. echo ""
  165. fi
  166. wget --no-check-certificate -q $WEB_URL -O /dev/null
  167. if [ $? -ne 0 ]; then
  168. echo "WARNING: Branch '$BRANCH' was not found on the contrib git tree."
  169. echo " Please check your remote and branch parameter before sending."
  170. echo ""
  171. fi
  172. fi
  173. if [ -e $ODIR ]; then
  174. echo "ERROR: output directory $ODIR exists."
  175. exit 1
  176. fi
  177. mkdir $ODIR
  178. if [ -n "$RELDIR" ]; then
  179. ODIR=$(realpath $ODIR)
  180. pdir=$(pwd)
  181. cd $RELDIR
  182. extraopts="$extraopts --relative"
  183. fi
  184. # Generate the patches and cover letter
  185. git format-patch $extraopts -M40 --subject-prefix="$PREFIX" -n -o $ODIR --thread=shallow --cover-letter $RELATIVE_TO..$COMMIT_ID > /dev/null
  186. if [ -z "$(ls -A $ODIR 2> /dev/null)" ]; then
  187. echo "ERROR: $ODIR is empty, no cover letter and patches was generated!"
  188. echo " This is most likely due to that \$RRELATIVE_TO..\$COMMIT_ID"
  189. echo " ($RELATIVE_TO..$COMMIT_ID) don't contain any differences."
  190. rmdir $ODIR
  191. exit 1
  192. fi
  193. [ -n "$RELDIR" ] && cd $pdir
  194. # Customize the cover letter
  195. CL="$(echo $ODIR/*0000-cover-letter.patch)"
  196. PM="$ODIR/pull-msg"
  197. GIT_VERSION=$(`git --version` | tr -d '[:alpha:][:space:].' | sed 's/\(...\).*/\1/')
  198. NEWER_GIT_VERSION=210
  199. if [ $GIT_VERSION -lt $NEWER_GIT_VERSION ]; then
  200. git request-pull $RELATIVE_TO $REMOTE_URL $COMMIT_ID >> "$PM"
  201. else
  202. git request-pull $RELATIVE_TO $REMOTE_URL $L_BRANCH:$BRANCH >> "$PM"
  203. fi
  204. if [ $? -ne 0 ]; then
  205. echo "ERROR: git request-pull reported an error"
  206. rm -rf $ODIR
  207. exit 1
  208. fi
  209. # The cover letter already has a diffstat, remove it from the pull-msg
  210. # before inserting it.
  211. sed -n "0,\#$REMOTE_URL# p" "$PM" | sed -i "/BLURB HERE/ r /dev/stdin" "$CL"
  212. rm "$PM"
  213. # If this is an RFC, make that clear in the cover letter
  214. if [ $RFC -eq 1 ]; then
  215. (cat <<EOM
  216. Please review the following changes for suitability for inclusion. If you have
  217. any objections or suggestions for improvement, please respond to the patches. If
  218. you agree with the changes, please provide your Acked-by.
  219. EOM
  220. ) | sed -i "/BLURB HERE/ r /dev/stdin" "$CL"
  221. fi
  222. # Insert the WEB_URL if there is one
  223. if [ -n "$WEB_URL" ]; then
  224. echo " $WEB_URL" | sed -i "\#$REMOTE_URL# r /dev/stdin" "$CL"
  225. fi
  226. # If the user specified a message body, insert it into the cover letter and
  227. # remove the BLURB token.
  228. if [ -n "$BODY" ]; then
  229. sed -i "/BLURB HERE/ r $BODY" "$CL"
  230. sed -i "/BLURB HERE/ d" "$CL"
  231. fi
  232. # Set subject automatically if there is only one patch
  233. patch_cnt=`git log --pretty=oneline ${RELATIVE_TO}..${L_BRANCH} | wc -l`
  234. if [ -z "$SUBJECT" -a $patch_cnt -eq 1 ]; then
  235. SUBJECT="`git log --format=%s ${RELATIVE_TO}..${L_BRANCH}`"
  236. fi
  237. # Replace the SUBJECT token with it.
  238. if [ -n "$SUBJECT" ]; then
  239. sed -i -e "s/\*\*\* SUBJECT HERE \*\*\*/$SUBJECT/" "$CL"
  240. fi
  241. # Generate report for user
  242. cat <<EOM
  243. The following patches have been prepared:
  244. $(for PATCH in $(ls $ODIR/*); do echo " $PATCH"; done)
  245. Review their content, especially the summary mail:
  246. $CL
  247. When you are satisfied, you can send them with:
  248. send-pull-request -a -p $ODIR
  249. EOM
  250. # Check the patches for trailing white space
  251. egrep -q -e "^\+.*\s+$" $ODIR/*
  252. if [ $? -ne 1 ]; then
  253. echo
  254. echo "WARNING: Trailing white space detected at these locations"
  255. egrep -nH --color -e "^\+.*\s+$" $ODIR/*
  256. fi