send-pull-request 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. #!/bin/bash
  2. #
  3. # Copyright (c) 2010-2011, 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 send a patch series prepared by the
  22. # create-pull-request script to Open Embedded and The Yocto Project, as well
  23. # as to related projects and layers.
  24. #
  25. AUTO=0
  26. AUTO_CL=0
  27. GITSOBCC="--suppress-cc=all"
  28. # Prevent environment leakage to these vars.
  29. unset TO
  30. unset CC
  31. unset AUTO_CC
  32. usage()
  33. {
  34. cat <<EOM
  35. Usage: $(basename $0) [-h] [-a] [-c] [[-t email]...] -p pull-dir
  36. -a Send the cover letter to every recipient listed in Cc and
  37. Signed-off-by lines found in the cover letter and the patches.
  38. This option implies -c.
  39. -c Expand the Cc list for the individual patches using the Cc and
  40. Signed-off-by lines from the same patch.
  41. -p pull-dir Directory containing summary and patch files
  42. -t email Explicitly add email to the recipients
  43. EOM
  44. }
  45. # Collect addresses from a patch into AUTO_CC
  46. # $1: a patch file
  47. harvest_recipients()
  48. {
  49. PATCH=$1
  50. export IFS=$',\n'
  51. for REGX in "^[Cc][Cc]: *" "^[Ss]igned-[Oo]ff-[Bb]y: *"; do
  52. for EMAIL in $(sed '/^---$/q' $PATCH | grep -e "$REGX" | sed "s/$REGX//"); do
  53. if [ "${AUTO_CC/$EMAIL/}" == "$AUTO_CC" ] && [ -n "$EMAIL" ]; then
  54. if [ -z "$AUTO_CC" ]; then
  55. AUTO_CC=$EMAIL;
  56. else
  57. AUTO_CC="$AUTO_CC,$EMAIL";
  58. fi
  59. fi
  60. done
  61. done
  62. unset IFS
  63. }
  64. # Parse and verify arguments
  65. while getopts "achp:t:" OPT; do
  66. case $OPT in
  67. a)
  68. AUTO_CL=1
  69. # Fall through to include -c
  70. ;;
  71. c)
  72. AUTO=1
  73. GITSOBCC="--signed-off-by-cc"
  74. ;;
  75. h)
  76. usage
  77. exit 0
  78. ;;
  79. p)
  80. PDIR=${OPTARG%/}
  81. if [ ! -d $PDIR ]; then
  82. echo "ERROR: pull-dir \"$PDIR\" does not exist."
  83. usage
  84. exit 1
  85. fi
  86. ;;
  87. t)
  88. if [ -n "$TO" ]; then
  89. TO="$TO,$OPTARG"
  90. else
  91. TO="$OPTARG"
  92. fi
  93. ;;
  94. esac
  95. done
  96. if [ -z "$PDIR" ]; then
  97. echo "ERROR: you must specify a pull-dir."
  98. usage
  99. exit 1
  100. fi
  101. # Verify the cover letter is complete and free of tokens
  102. CL="$PDIR/0000-cover-letter.patch"
  103. for TOKEN in SUBJECT BLURB; do
  104. grep -q "*** $TOKEN HERE ***" "$CL"
  105. if [ $? -eq 0 ]; then
  106. echo "ERROR: Please edit $CL and try again (Look for '*** $TOKEN HERE ***')."
  107. exit 1
  108. fi
  109. done
  110. # Harvest emails from the generated patches and populate AUTO_CC.
  111. if [ $AUTO_CL -eq 1 ]; then
  112. for PATCH in $PDIR/*.patch; do
  113. harvest_recipients $PATCH
  114. done
  115. fi
  116. AUTO_TO="$(git config sendemail.to)"
  117. if [ -n "$AUTO_TO" ]; then
  118. if [ -n "$TO" ]; then
  119. TO="$TO,$AUTO_TO"
  120. else
  121. TO="$AUTO_TO"
  122. fi
  123. fi
  124. if [ -z "$TO" ] && [ -z "$AUTO_CC" ]; then
  125. echo "ERROR: you have not specified any recipients."
  126. usage
  127. exit 1
  128. fi
  129. # Convert the collected addresses into git-send-email argument strings
  130. export IFS=$','
  131. GIT_TO=$(for R in $TO; do echo -n "--to='$R' "; done)
  132. GIT_CC=$(for R in $AUTO_CC; do echo -n "--cc='$R' "; done)
  133. unset IFS
  134. # Handoff to git-send-email. It will perform the send confirmation.
  135. PATCHES=$(echo $PDIR/*.patch)
  136. if [ $AUTO_CL -eq 1 ]; then
  137. # Send the cover letter to every recipient, both specified as well as
  138. # harvested. Then remove it from the patches list.
  139. eval "git send-email $GIT_TO $GIT_CC --confirm=always --no-chain-reply-to --suppress-cc=all $CL"
  140. if [ $? -eq 1 ]; then
  141. echo "ERROR: failed to send cover-letter with automatic recipients."
  142. exit 1
  143. fi
  144. PATCHES=${PATCHES/"$CL"/}
  145. fi
  146. # Send the patch to the specified recipients and, if -c was specified, those git
  147. # finds in this specific patch.
  148. eval "git send-email $GIT_TO --confirm=always --no-chain-reply-to $GITSOBCC $PATCHES"
  149. if [ $? -eq 1 ]; then
  150. echo "ERROR: failed to send patches."
  151. exit 1
  152. fi