sync-remotes.sh 4.5 KB

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