git-push-all.sh 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. #!/usr/bin/env bash
  2. # Usage: git-push-all.sh -t <test-branch-prefix> -r <remote-name> <git-opts>
  3. # env vars: TOR_UPSTREAM_REMOTE_NAME=upstream TOR_PUSH_DELAY=0
  4. # git-opts: --no-atomic --dry-run (any other git push option)
  5. #
  6. # TOR_PUSH_DELAY pushes the master and maint branches separately, so that CI
  7. # runs in a sensible order.
  8. # push --atomic is the default when TOR_PUSH_DELAY=0, and for release branches.
  9. set -e
  10. #################
  11. # Configuration #
  12. #################
  13. # Don't change this configuration - set the env vars in your .profile
  14. #
  15. # The upstream remote which git.torproject.org/tor.git points to.
  16. # In test branch mode, override this setting with -r <remote-name>
  17. UPSTREAM_REMOTE=${TOR_UPSTREAM_REMOTE_NAME:-"upstream"}
  18. # Add a delay between pushes, so CI runs on the most important branches first
  19. PUSH_DELAY=${TOR_PUSH_DELAY:-0}
  20. #######################
  21. # Argument processing #
  22. #######################
  23. # Controlled by the -t <test-branch-prefix> option. The test branch base
  24. # name option makes git-merge-forward.sh create new test branches:
  25. # <tbbn>_029, <tbbn>_035, ... , <tbbn>_master, and merge forward.
  26. TEST_BRANCH_PREFIX=
  27. while getopts ":r:t:" opt; do
  28. case "$opt" in
  29. r) UPSTREAM_REMOTE="$OPTARG"
  30. echo " *** PUSHING TO REMOTE: ${UPSTREAM_REMOTE} ***"
  31. shift
  32. shift
  33. OPTIND=$[$OPTIND - 2]
  34. ;;
  35. t) TEST_BRANCH_PREFIX="$OPTARG"
  36. echo " *** PUSHING TEST BRANCHES: ${TEST_BRANCH_PREFIX}_nnn ***"
  37. shift
  38. shift
  39. OPTIND=$[$OPTIND - 2]
  40. ;;
  41. *)
  42. # Assume git push will handle the option
  43. ;;
  44. esac
  45. done
  46. if [ "$TEST_BRANCH_PREFIX" ]; then
  47. if [ "$UPSTREAM_REMOTE" = ${TOR_UPSTREAM_REMOTE_NAME:-"upstream"} ]; then
  48. echo "Pushing test branches ${TEST_BRANCH_PREFIX}_nnn to " \
  49. "$UPSTREAM_REMOTE is not allowed."
  50. echo "Usage: $0 -r <remote-name> -t <test-branch-prefix> <git-opts>"
  51. exit 1
  52. fi
  53. fi
  54. ########################
  55. # Git branches to push #
  56. ########################
  57. PUSH_BRANCHES=$(echo \
  58. master \
  59. {release,maint}-0.4.1 \
  60. {release,maint}-0.4.0 \
  61. {release,maint}-0.3.5 \
  62. {release,maint}-0.2.9 \
  63. )
  64. if [ -z "$TEST_BRANCH_PREFIX" ]; then
  65. # maint/release push mode
  66. #
  67. # List of branches to push. Ordering is not important.
  68. PUSH_BRANCHES=$(echo \
  69. master \
  70. {release,maint}-0.4.1 \
  71. {release,maint}-0.4.0 \
  72. {release,maint}-0.3.5 \
  73. {release,maint}-0.2.9 \
  74. )
  75. else
  76. # Test branch mode: merge to maint only, and create a new branch for 0.2.9
  77. #
  78. # List of branches to push. Ordering is not important.
  79. PUSH_BRANCHES=$(echo \
  80. ${TEST_BRANCH_PREFIX}_master \
  81. ${TEST_BRANCH_PREFIX}_041 \
  82. ${TEST_BRANCH_PREFIX}_040 \
  83. ${TEST_BRANCH_PREFIX}_035 \
  84. ${TEST_BRANCH_PREFIX}_029 \
  85. )
  86. fi
  87. ###############
  88. # Entry point #
  89. ###############
  90. if [ "$PUSH_DELAY" -le 0 ]; then
  91. echo "Pushing $PUSH_BRANCHES"
  92. # We know that there are no spaces in any branch within $PUSH_BRANCHES, so
  93. # it is safe to use it unquoted. (This also applies to the other shellcheck
  94. # exceptions below.)
  95. #
  96. # shellcheck disable=SC2086
  97. git push --atomic "$@" "$UPSTREAM_REMOTE" $PUSH_BRANCHES
  98. else
  99. PUSH_BRANCHES=$(echo "$PUSH_BRANCHES" | tr " " "\n" | sort -V)
  100. MASTER_BRANCH=$(echo "$PUSH_BRANCHES" | tr " " "\n" | grep master)
  101. if [ -z "$TEST_BRANCH_PREFIX" ]; then
  102. MAINT_BRANCHES=$(echo "$PUSH_BRANCHES" | tr " " "\n" | grep maint)
  103. RELEASE_BRANCHES=$(echo "$PUSH_BRANCHES" | tr " " "\n" | grep release | \
  104. tr "\n" " ")
  105. printf "Pushing with %ss delays, so CI runs in this order:\n%s\n%s\n%s\n" \
  106. "$PUSH_DELAY" "$MASTER_BRANCH" "$MAINT_BRANCHES" "$RELEASE_BRANCHES"
  107. else
  108. # Actually test branches based on maint branches
  109. MAINT_BRANCHES=$(echo "$PUSH_BRANCHES" | tr " " "\n" | grep -v master)
  110. printf "Pushing with %ss delays, so CI runs in this order:\n%s\n%s\n" \
  111. "$PUSH_DELAY" "$MASTER_BRANCH" "$MAINT_BRANCHES"
  112. # No release branches
  113. RELEASE_BRANCHES=
  114. fi
  115. git push "$@" "$UPSTREAM_REMOTE" "$MASTER_BRANCH"
  116. sleep "$PUSH_DELAY"
  117. # shellcheck disable=SC2086
  118. for b in $MAINT_BRANCHES; do
  119. git push "$@" "$UPSTREAM_REMOTE" "$b"
  120. sleep "$PUSH_DELAY"
  121. done
  122. if [ "$RELEASE_BRANCHES" ]; then
  123. # shellcheck disable=SC2086
  124. git push --atomic "$@" "$UPSTREAM_REMOTE" $RELEASE_BRANCHES
  125. fi
  126. fi