sync-remotes.sh 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. #!/usr/bin/env bash
  2. set -euo pipefail
  3. # ensure dir
  4. cd -P -- "$(dirname -- "${BASH_SOURCE[0]}")/../.."
  5. BASE_BRANCHES=( 'release-53' 'release-52' 'release-51' 'master' )
  6. usage() {
  7. cat <<EOF
  8. $0 [option]
  9. options:
  10. -h|--help:
  11. This script works on one of the branches listed in the -b|--base option below.
  12. It tries to merge (by default with --ff-only option)
  13. upstreams branches for the current working branch.
  14. The uppstream branch of the current branch are as below:
  15. * release-53: [] # no upstream for 5.3 opensource edition
  16. * release-52: [] # no upstream for 5.2 opensource edition
  17. * release-51: [] # no upstream for 5.1 opensource edition
  18. * master: [release-53] # sync release-53 to master
  19. -b|--base:
  20. The base branch of current working branch if currently is not
  21. on one of the following branches.
  22. ${BASE_BRANCHES[@]}
  23. -i|--interactive:
  24. With this option, the script will try to merge upstream
  25. branches to local working branch interactively.
  26. That is, there will be git prompts to edit commit messages etc.
  27. Without this option, the script executes 'git merge' command
  28. with '--ff-only' option which conveniently pulls remote
  29. updates if there is any, and fails when fast-forward is not possible
  30. --dryrun:
  31. Do not perform merge. Run the checks, fetch from remote,
  32. and show what's going to happen.
  33. EOF
  34. }
  35. logerr() {
  36. echo "$(tput setaf 1)ERROR: $1$(tput sgr0)"
  37. }
  38. logwarn() {
  39. echo "$(tput setaf 3)WARNING: $1$(tput sgr0)"
  40. }
  41. logmsg() {
  42. echo "INFO: $1"
  43. }
  44. INTERACTIVE='no'
  45. DRYRUN='no'
  46. while [ "$#" -gt 0 ]; do
  47. case $1 in
  48. -h|--help)
  49. usage
  50. exit 0
  51. ;;
  52. -i|--interactive)
  53. shift
  54. INTERACTIVE='yes'
  55. ;;
  56. -b|--base)
  57. shift
  58. BASE_BRANCH="$1"
  59. shift
  60. ;;
  61. --dryrun)
  62. shift
  63. DRYRUN='yes'
  64. ;;
  65. *)
  66. logerr "Unknown option $1"
  67. exit 1
  68. ;;
  69. esac
  70. done
  71. CURRENT_BRANCH="$(git branch --show-current)"
  72. BASE_BRANCH="${BASE_BRANCH:-${CURRENT_BRANCH}}"
  73. ## check if arg1 is one of the elements in arg2-N
  74. is_element() {
  75. local e match="$1"
  76. shift
  77. for e in "${@}"; do
  78. if [ "$e" = "$match" ]; then
  79. return 0
  80. fi
  81. done
  82. return 1
  83. }
  84. if ! is_element "$BASE_BRANCH" "${BASE_BRANCHES[@]}"; then
  85. logerr "Cannot work with branch $BASE_BRANCH"
  86. logerr "The base branch must be one of: ${BASE_BRANCHES[*]}"
  87. logerr "Change work branch to one of the above."
  88. logerr "OR: use -b|--base to specify from which base branch is current working branch created"
  89. exit 1
  90. fi
  91. ## Find git remotes to fetch from.
  92. ##
  93. ## NOTE: For enterprise, the opensource repo must be added as a remote.
  94. ## Because not all changes in opensource repo are synced to enterprise repo immediately.
  95. ##
  96. ## NOTE: grep -v enterprise here, but why not to match on full repo name 'emqx/emqx.git'?
  97. ## It's because the git remote does not always end with .git
  98. GIT_REMOTE_CE="$(git remote -v | grep 'emqx/emqx' | grep -v enterprise | grep fetch | head -1 | awk '{print $1}' || true)"
  99. if [ -z "$GIT_REMOTE_CE" ]; then
  100. logerr "Cannot find git remote for emqx/emqx"
  101. exit 1
  102. fi
  103. REMOTES=( "${GIT_REMOTE_CE}" )
  104. ## Fetch the remotes
  105. for remote in "${REMOTES[@]}"; do
  106. logwarn "Fetching from remote=${remote} (force tag sync)."
  107. git fetch "$remote" --tags --force
  108. done
  109. logmsg 'Fetched all remotes'
  110. if [ "$INTERACTIVE" = 'yes' ]; then
  111. MERGE_OPTS=''
  112. else
  113. ## Using --ff-only to *check* if the remote is already merged
  114. ## Also conveniently merged it in case it's *not* merged but can be fast-forwarded
  115. ## Alternative is to check with 'git merge-base'
  116. MERGE_OPTS='--ff-only'
  117. fi
  118. ## Get the git remote reference of the given 'release-' or 'main-' branch
  119. remote_ref() {
  120. local branch="$1"
  121. echo -n "${GIT_REMOTE_CE}/${branch} "
  122. }
  123. remote_refs() {
  124. local br
  125. for br in "${@}"; do
  126. remote_ref "$br"
  127. done
  128. }
  129. ## Get upstream branches of the given branch
  130. upstream_branches() {
  131. local base="$1"
  132. case "$base" in
  133. release-53)
  134. remote_ref "$base"
  135. ;;
  136. release-52)
  137. remote_ref "$base"
  138. ;;
  139. release-51)
  140. remote_ref "$base"
  141. ;;
  142. master)
  143. remote_refs "$base" 'release-53'
  144. ;;
  145. esac
  146. }
  147. for remote_ref in $(upstream_branches "$BASE_BRANCH"); do
  148. if [ "$DRYRUN" = 'yes' ]; then
  149. logmsg "Merge with this command: git merge $MERGE_OPTS $remote_ref"
  150. else
  151. logmsg "Merging $remote_ref"
  152. git merge $MERGE_OPTS "$remote_ref"
  153. fi
  154. done