git-merge-forward.sh 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. #!/bin/bash
  2. ##############################
  3. # Configuration (change me!) #
  4. ##############################
  5. # The general setup that is suggested here is:
  6. #
  7. # GIT_PATH = /home/<user>/git/
  8. # ... where the git repository directories resides.
  9. # TOR_MASTER_NAME = "tor"
  10. # ... which means that tor.git was cloned in /home/<user>/git/tor
  11. # TOR_WKT_NAME = "tor-wkt"
  12. # ... which means that the tor worktrees are in /home/<user>/git/tor-wkt
  13. # Where are all those git repositories?
  14. GIT_PATH="FULL_PATH_TO_GIT_REPOSITORY_DIRECTORY"
  15. # The tor master git repository directory from which all the worktree have
  16. # been created.
  17. TOR_MASTER_NAME="tor"
  18. # The worktrees location (directory).
  19. TOR_WKT_NAME="tor-wkt"
  20. #########################
  21. # End of configuration. #
  22. #########################
  23. # Configuration of the branches that needs merging. The values are in order:
  24. # (1) Branch name that we merge onto.
  25. # (2) Branch name to merge from. In other words, this is merge into (1)
  26. # (3) Full path of the git worktree.
  27. #
  28. # As an example:
  29. # $ cd <PATH/TO/WORKTREE> (3)
  30. # $ git checkout maint-0.3.5 (1)
  31. # $ git pull
  32. # $ git merge maint-0.3.4 (2)
  33. #
  34. # First set of arrays are the maint-* branch and then the release-* branch.
  35. # New arrays need to be in the WORKTREE= array else they aren't considered.
  36. MAINT_034=( "maint-0.3.4" "maint-0.2.9" "$GIT_PATH/$TOR_WKT_NAME/maint-0.3.4" )
  37. MAINT_035=( "maint-0.3.5" "maint-0.3.4" "$GIT_PATH/$TOR_WKT_NAME/maint-0.3.5" )
  38. MAINT_040=( "maint-0.4.0" "maint-0.3.5" "$GIT_PATH/$TOR_WKT_NAME/maint-0.4.0" )
  39. MAINT_MASTER=( "master" "maint-0.4.0" "$GIT_PATH/$TOR_MASTER_NAME" )
  40. RELEASE_029=( "release-0.2.9" "maint-0.2.9" "$GIT_PATH/$TOR_WKT_NAME/release-0.2.9" )
  41. RELEASE_034=( "release-0.3.4" "maint-0.3.4" "$GIT_PATH/$TOR_WKT_NAME/release-0.3.4" )
  42. RELEASE_035=( "release-0.3.5" "maint-0.3.5" "$GIT_PATH/$TOR_WKT_NAME/release-0.3.5" )
  43. RELEASE_040=( "release-0.4.0" "maint-0.4.0" "$GIT_PATH/$TOR_WKT_NAME/release-0.4.0" )
  44. # The master branch path has to be the main repository thus contains the
  45. # origin that will be used to fetch the updates. All the worktrees are created
  46. # from that repository.
  47. ORIGIN_PATH="$GIT_PATH/$TOR_MASTER_NAME"
  48. # SC2034 -- shellcheck thinks that these are unused. We know better.
  49. ACTUALLY_THESE_ARE_USED=<<EOF
  50. ${MAINT_034[0]}
  51. ${MAINT_035[0]}
  52. ${MAINT_040[0]}
  53. ${MAINT_MASTER[0]}
  54. ${RELEASE_029[0]}
  55. ${RELEASE_034[0]}
  56. ${RELEASE_035[0]}
  57. ${RELEASE_040[0]}
  58. EOF
  59. ##########################
  60. # Git Worktree to manage #
  61. ##########################
  62. # List of all worktrees to work on. All defined above. Ordering is important.
  63. # Always the maint-* branch BEFORE then the release-*.
  64. WORKTREE=(
  65. RELEASE_029[@]
  66. MAINT_034[@]
  67. RELEASE_034[@]
  68. MAINT_035[@]
  69. RELEASE_035[@]
  70. MAINT_040[@]
  71. RELEASE_040[@]
  72. MAINT_MASTER[@]
  73. )
  74. COUNT=${#WORKTREE[@]}
  75. # Controlled by the -n option. The dry run option will just output the command
  76. # that would have been executed for each worktree.
  77. DRY_RUN=0
  78. # Control characters
  79. CNRM=$'\x1b[0;0m' # Clear color
  80. # Bright color
  81. BGRN=$'\x1b[1;32m'
  82. BBLU=$'\x1b[1;34m'
  83. BRED=$'\x1b[1;31m'
  84. BYEL=$'\x1b[1;33m'
  85. IWTH=$'\x1b[3;37m'
  86. # Strings for the pretty print.
  87. MARKER="${BBLU}[${BGRN}+${BBLU}]${CNRM}"
  88. SUCCESS="${BGRN}success${CNRM}"
  89. FAILED="${BRED}failed${CNRM}"
  90. ####################
  91. # Helper functions #
  92. ####################
  93. # Validate the given returned value (error code), print success or failed. The
  94. # second argument is the error output in case of failure, it is printed out.
  95. # On failure, this function exits.
  96. function validate_ret
  97. {
  98. if [ "$1" -eq 0 ]; then
  99. printf "%s\\n" "$SUCCESS"
  100. else
  101. printf "%s\\n" "$FAILED"
  102. printf " %s" "$2"
  103. exit 1
  104. fi
  105. }
  106. # Switch to the given branch name.
  107. function switch_branch
  108. {
  109. local cmd="git checkout $1"
  110. printf " %s Switching branch to %s..." "$MARKER" "$1"
  111. if [ $DRY_RUN -eq 0 ]; then
  112. msg=$( eval "$cmd" 2>&1 )
  113. validate_ret $? "$msg"
  114. else
  115. printf "\\n %s\\n" "${IWTH}$cmd${CNRM}"
  116. fi
  117. }
  118. # Pull the given branch name.
  119. function pull_branch
  120. {
  121. local cmd="git pull"
  122. printf " %s Pulling branch %s..." "$MARKER" "$1"
  123. if [ $DRY_RUN -eq 0 ]; then
  124. msg=$( eval "$cmd" 2>&1 )
  125. validate_ret $? "$msg"
  126. else
  127. printf "\\n %s\\n" "${IWTH}$cmd${CNRM}"
  128. fi
  129. }
  130. # Merge the given branch name ($2) into the current branch ($1).
  131. function merge_branch
  132. {
  133. local cmd="git merge --no-edit $1"
  134. printf " %s Merging branch %s into %s..." "$MARKER" "$1" "$2"
  135. if [ $DRY_RUN -eq 0 ]; then
  136. msg=$( eval "$cmd" 2>&1 )
  137. validate_ret $? "$msg"
  138. else
  139. printf "\\n %s\\n" "${IWTH}$cmd${CNRM}"
  140. fi
  141. }
  142. # Pull the given branch name.
  143. function merge_branch_origin
  144. {
  145. local cmd="git merge --ff-only origin/$1"
  146. printf " %s Merging branch origin/%s..." "$MARKER" "$1"
  147. if [ $DRY_RUN -eq 0 ]; then
  148. msg=$( eval "$cmd" 2>&1 )
  149. validate_ret $? "$msg"
  150. else
  151. printf "\\n %s\\n" "${IWTH}$cmd${CNRM}"
  152. fi
  153. }
  154. # Go into the worktree repository.
  155. function goto_repo
  156. {
  157. if [ ! -d "$1" ]; then
  158. echo " $1: Not found. Stopping."
  159. exit 1
  160. fi
  161. cd "$1" || exit
  162. }
  163. # Fetch the origin. No arguments.
  164. function fetch_origin
  165. {
  166. local cmd="git fetch origin"
  167. printf " %s Fetching origin..." "$MARKER"
  168. if [ $DRY_RUN -eq 0 ]; then
  169. msg=$( eval "$cmd" 2>&1 )
  170. validate_ret $? "$msg"
  171. else
  172. printf "\\n %s\\n" "${IWTH}$cmd${CNRM}"
  173. fi
  174. }
  175. ###############
  176. # Entry point #
  177. ###############
  178. while getopts "n" opt; do
  179. case "$opt" in
  180. n) DRY_RUN=1
  181. echo " *** DRY DRUN MODE ***"
  182. ;;
  183. *)
  184. ;;
  185. esac
  186. done
  187. # First, fetch the origin.
  188. goto_repo "$ORIGIN_PATH"
  189. fetch_origin
  190. # Go over all configured worktree.
  191. for ((i=0; i<COUNT; i++)); do
  192. current=${!WORKTREE[$i]:0:1}
  193. previous=${!WORKTREE[$i]:1:1}
  194. repo_path=${!WORKTREE[$i]:2:1}
  195. printf "%s Handling branch \\n" "$MARKER" "${BYEL}$current${CNRM}"
  196. # Go into the worktree to start merging.
  197. goto_repo "$repo_path"
  198. # Checkout the current branch
  199. switch_branch "$current"
  200. # Update the current branch with an origin merge to get the latest.
  201. merge_branch_origin "$current"
  202. # Merge the previous branch. Ex: merge maint-0.2.5 into maint-0.2.9.
  203. merge_branch "$previous" "$current"
  204. done