send-pull-request 4.1 KB

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