sync-remotes.sh 4.0 KB

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