git-push-all.sh 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. #!/usr/bin/env bash
  2. # Usage: git-push-all.sh -t <test-branch-prefix> -r <remote-name> -s
  3. # -- <git-opts>
  4. # arguments:
  5. # -t: test branch mode: Push test branches, rather than maint and
  6. # release branches. Pushes the branches called prefix_029,
  7. # prefix_035, ... , prefix_master.
  8. # -r: push to remote-name, rather than $TOR_UPSTREAM_REMOTE_NAME.
  9. # -s: push branches whose tips match upstream maint, release, or
  10. # master branches. The default is to skip these branches. Use
  11. # -s when testing for CI environment failures with old code.
  12. # --: pass any other arguments to git, rather than the script.
  13. # env vars:
  14. # TOR_GIT_PUSH: the git push command and arguments
  15. # TOR_UPSTREAM_REMOTE_NAME: the default upstream, overridden by -r
  16. # TOR_PUSH_DELAY: pushes the master and maint branches separately,
  17. # so that CI runs in a sensible order.
  18. # TOR_PUSH_SAME: push branches whose tips match upstream maint,
  19. # release, or master branches. Inverted by -s.
  20. # See the Configuration section for env var default values.
  21. # git-opts:
  22. # --no-atomic --dry-run (and any other git push option)
  23. set -e
  24. #################
  25. # Configuration #
  26. #################
  27. # Don't change this configuration - set the env vars in your .profile
  28. #
  29. # git push command and default arguments
  30. GIT_PUSH=${TOR_GIT_PUSH:-"git push --atomic"}
  31. # The upstream remote which git.torproject.org/tor.git points to.
  32. DEFAULT_UPSTREAM_REMOTE=${TOR_UPSTREAM_REMOTE_NAME:-"upstream"}
  33. # Push to a different upstream remote using -r <remote-name>
  34. UPSTREAM_REMOTE=${DEFAULT_UPSTREAM_REMOTE}
  35. # Add a delay between pushes, so CI runs on the most important branches first
  36. PUSH_DELAY=${TOR_PUSH_DELAY:-0}
  37. # Push (1) or skip (0) test branches that are the same as an upstream
  38. # maint/master branch. Push if you are testing that the CI environment still
  39. # works on old code, skip if you are testing new code in the branch.
  40. # Default: skip unchanged branches.
  41. # Inverted by the -s option.
  42. PUSH_SAME=${TOR_PUSH_SAME:-0}
  43. #######################
  44. # Argument processing #
  45. #######################
  46. # Controlled by the -t <test-branch-prefix> option. The test branch base
  47. # name option makes git-merge-forward.sh create new test branches:
  48. # <tbbn>_029, <tbbn>_035, ... , <tbbn>_master, and merge forward.
  49. TEST_BRANCH_PREFIX=
  50. while getopts ":r:st:" opt; do
  51. case "$opt" in
  52. r) UPSTREAM_REMOTE="$OPTARG"
  53. echo " *** PUSHING TO REMOTE: ${UPSTREAM_REMOTE} ***"
  54. shift
  55. shift
  56. OPTIND=$[$OPTIND - 2]
  57. ;;
  58. s) PUSH_SAME=$[! "$PUSH_SAME" ]
  59. if [ "$PUSH_SAME" -eq 0 ]; then
  60. echo " *** SKIPPING UNCHANGED TEST BRANCHES ***"
  61. else
  62. echo " *** PUSHING UNCHANGED TEST BRANCHES ***"
  63. fi
  64. shift
  65. OPTIND=$[$OPTIND - 1]
  66. ;;
  67. t) TEST_BRANCH_PREFIX="$OPTARG"
  68. echo " *** PUSHING TEST BRANCHES: ${TEST_BRANCH_PREFIX}_nnn ***"
  69. shift
  70. shift
  71. OPTIND=$[$OPTIND - 2]
  72. ;;
  73. *)
  74. # Assume we're done with script arguments,
  75. # and git push will handle the option
  76. break
  77. ;;
  78. esac
  79. done
  80. # getopts doesn't allow "-" as an option character,
  81. # so we have to handle -- manually
  82. if [ "$1" = "--" ]; then
  83. shift
  84. fi
  85. echo "Calling git push --atomic $@ <branches>"
  86. if [ "$TEST_BRANCH_PREFIX" ]; then
  87. if [ "$UPSTREAM_REMOTE" = ${TOR_UPSTREAM_REMOTE_NAME:-"upstream"} ]; then
  88. echo "Pushing test branches ${TEST_BRANCH_PREFIX}_nnn to " \
  89. "$UPSTREAM_REMOTE is not allowed."
  90. echo "Usage: $0 -r <remote-name> -t <test-branch-prefix> <git-opts>"
  91. exit 1
  92. fi
  93. fi
  94. ################################
  95. # Git upstream remote branches #
  96. ################################
  97. DEFAULT_UPSTREAM_BRANCHES=
  98. if [ "$DEFAULT_UPSTREAM_REMOTE" != "$UPSTREAM_REMOTE" ]; then
  99. DEFAULT_UPSTREAM_BRANCHES=`echo \
  100. ${DEFAULT_UPSTREAM_REMOTE}/master \
  101. ${DEFAULT_UPSTREAM_REMOTE}/{release,maint}-0.4.1 \
  102. ${DEFAULT_UPSTREAM_REMOTE}/{release,maint}-0.4.0 \
  103. ${DEFAULT_UPSTREAM_REMOTE}/{release,maint}-0.3.5 \
  104. ${DEFAULT_UPSTREAM_REMOTE}/{release,maint}-0.2.9 \
  105. `
  106. fi
  107. UPSTREAM_BRANCHES=`echo \
  108. ${UPSTREAM_REMOTE}/master \
  109. ${UPSTREAM_REMOTE}/{release,maint}-0.4.1 \
  110. ${UPSTREAM_REMOTE}/{release,maint}-0.4.0 \
  111. ${UPSTREAM_REMOTE}/{release,maint}-0.3.5 \
  112. ${UPSTREAM_REMOTE}/{release,maint}-0.2.9 \
  113. `
  114. ########################
  115. # Git branches to push #
  116. ########################
  117. PUSH_BRANCHES=$(echo \
  118. master \
  119. {release,maint}-0.4.1 \
  120. {release,maint}-0.4.0 \
  121. {release,maint}-0.3.5 \
  122. {release,maint}-0.2.9 \
  123. )
  124. if [ -z "$TEST_BRANCH_PREFIX" ]; then
  125. # maint/release push mode
  126. #
  127. # List of branches to push. Ordering is not important.
  128. PUSH_BRANCHES=$(echo \
  129. master \
  130. {release,maint}-0.4.1 \
  131. {release,maint}-0.4.0 \
  132. {release,maint}-0.3.5 \
  133. {release,maint}-0.2.9 \
  134. )
  135. else
  136. # Test branch mode: merge to maint only, and create a new branch for 0.2.9
  137. #
  138. # List of branches to push. Ordering is not important.
  139. PUSH_BRANCHES=$(echo \
  140. ${TEST_BRANCH_PREFIX}_master \
  141. ${TEST_BRANCH_PREFIX}_041 \
  142. ${TEST_BRANCH_PREFIX}_040 \
  143. ${TEST_BRANCH_PREFIX}_035 \
  144. ${TEST_BRANCH_PREFIX}_029 \
  145. )
  146. fi
  147. ###############
  148. # Entry point #
  149. ###############
  150. # Skip the test branches that are the same as the upstream branches
  151. if [ "$PUSH_SAME" -eq 0 -a "$TEST_BRANCH_PREFIX" ]; then
  152. NEW_PUSH_BRANCHES=
  153. for b in $PUSH_BRANCHES; do
  154. PUSH_COMMIT=`git rev-parse "$b"`
  155. SKIP_UPSTREAM=
  156. for u in $DEFAULT_UPSTREAM_BRANCHES $UPSTREAM_BRANCHES; do
  157. UPSTREAM_COMMIT=`git rev-parse "$u"`
  158. if [ "$PUSH_COMMIT" = "$UPSTREAM_COMMIT" ]; then
  159. SKIP_UPSTREAM="$u"
  160. fi
  161. done
  162. if [ "$SKIP_UPSTREAM" ]; then
  163. printf "Skipping unchanged: %s remote: %s\n" \
  164. "$b" "$SKIP_UPSTREAM"
  165. else
  166. if [ "$NEW_PUSH_BRANCHES" ]; then
  167. NEW_PUSH_BRANCHES="${NEW_PUSH_BRANCHES} ${b}"
  168. else
  169. NEW_PUSH_BRANCHES="${b}"
  170. fi
  171. fi
  172. done
  173. PUSH_BRANCHES=${NEW_PUSH_BRANCHES}
  174. fi
  175. if [ "$PUSH_DELAY" -le 0 ]; then
  176. echo "Pushing $PUSH_BRANCHES"
  177. # We know that there are no spaces in any branch within $PUSH_BRANCHES, so
  178. # it is safe to use it unquoted. (This also applies to the other shellcheck
  179. # exceptions below.)
  180. #
  181. # Push all the branches at the same time
  182. # shellcheck disable=SC2086
  183. $GIT_PUSH "$@" "$UPSTREAM_REMOTE" $PUSH_BRANCHES
  184. else
  185. # Push the branches in optimal CI order, with a delay between each push
  186. PUSH_BRANCHES=$(echo "$PUSH_BRANCHES" | tr " " "\n" | sort -V)
  187. MASTER_BRANCH=$(echo "$PUSH_BRANCHES" | tr " " "\n" | grep master)
  188. if [ -z "$TEST_BRANCH_PREFIX" ]; then
  189. MAINT_BRANCHES=$(echo "$PUSH_BRANCHES" | tr " " "\n" | grep maint)
  190. RELEASE_BRANCHES=$(echo "$PUSH_BRANCHES" | tr " " "\n" | grep release | \
  191. tr "\n" " ")
  192. printf "Pushing with %ss delays, so CI runs in this order:\n%s\n%s\n%s\n" \
  193. "$PUSH_DELAY" "$MASTER_BRANCH" "$MAINT_BRANCHES" "$RELEASE_BRANCHES"
  194. else
  195. # Actually test branches based on maint branches
  196. MAINT_BRANCHES=$(echo "$PUSH_BRANCHES" | tr " " "\n" | grep -v master)
  197. printf "Pushing with %ss delays, so CI runs in this order:\n%s\n%s\n" \
  198. "$PUSH_DELAY" "$MASTER_BRANCH" "$MAINT_BRANCHES"
  199. # No release branches
  200. RELEASE_BRANCHES=
  201. fi
  202. $GIT_PUSH "$@" "$UPSTREAM_REMOTE" "$MASTER_BRANCH"
  203. sleep "$PUSH_DELAY"
  204. # shellcheck disable=SC2086
  205. for b in $MAINT_BRANCHES; do
  206. $GIT_PUSH "$@" "$UPSTREAM_REMOTE" "$b"
  207. sleep "$PUSH_DELAY"
  208. done
  209. if [ "$RELEASE_BRANCHES" ]; then
  210. # shellcheck disable=SC2086
  211. $GIT_PUSH "$@" "$UPSTREAM_REMOTE" $RELEASE_BRANCHES
  212. fi
  213. fi