git-merge-forward.sh 6.4 KB

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