sync-remotes.sh 4.3 KB

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