git-merge-forward.sh 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355
  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. # (only used in merge forward mode)
  31. # (2) Full path of the git worktree
  32. # (3) current branch suffix
  33. # (maint branches only, only used in test branch mode)
  34. # (4) previous test branch suffix to merge into (3)
  35. # (maint branches only, only used in test branch mode)
  36. #
  37. # Merge forward example:
  38. # $ cd <PATH/TO/WORKTREE> (2)
  39. # $ git checkout maint-0.3.5 (0)
  40. # $ git pull
  41. # $ git merge maint-0.3.4 (1)
  42. #
  43. # Test branch example:
  44. # $ cd <PATH/TO/WORKTREE> (2)
  45. # $ git checkout -b ticket99999_035 (3)
  46. # $ git checkout maint-0.3.5 (0)
  47. # $ git pull
  48. # $ git checkout ticket99999_035
  49. # $ git merge maint-0.3.5
  50. # $ git merge ticket99999_034 (4)
  51. #
  52. # First set of arrays are the maint-* branch and then the release-* branch.
  53. # New arrays need to be in the WORKTREE= array else they aren't considered.
  54. #
  55. # Only used in test branch mode
  56. # There is no previous branch to merge forward, so the second and fifth items
  57. # must be blank ("")
  58. MAINT_029_TB=( "maint-0.2.9" "" "$GIT_PATH/$TOR_WKT_NAME/maint-0.2.9" \
  59. "_029" "")
  60. # Used in maint/release merge and test branch modes
  61. MAINT_035=( "maint-0.3.5" "maint-0.2.9" "$GIT_PATH/$TOR_WKT_NAME/maint-0.3.5" \
  62. "_035" "_029")
  63. MAINT_040=( "maint-0.4.0" "maint-0.3.5" "$GIT_PATH/$TOR_WKT_NAME/maint-0.4.0" \
  64. "_040" "_035")
  65. MAINT_041=( "maint-0.4.1" "maint-0.4.0" "$GIT_PATH/$TOR_WKT_NAME/maint-0.4.1" \
  66. "_041" "_040")
  67. MAINT_MASTER=( "master" "maint-0.4.1" "$GIT_PATH/$TOR_MASTER_NAME" \
  68. "_master" "_041")
  69. RELEASE_029=( "release-0.2.9" "maint-0.2.9" "$GIT_PATH/$TOR_WKT_NAME/release-0.2.9" )
  70. RELEASE_035=( "release-0.3.5" "maint-0.3.5" "$GIT_PATH/$TOR_WKT_NAME/release-0.3.5" )
  71. RELEASE_040=( "release-0.4.0" "maint-0.4.0" "$GIT_PATH/$TOR_WKT_NAME/release-0.4.0" )
  72. RELEASE_041=( "release-0.4.1" "maint-0.4.1" "$GIT_PATH/$TOR_WKT_NAME/release-0.4.1" )
  73. # The master branch path has to be the main repository thus contains the
  74. # origin that will be used to fetch the updates. All the worktrees are created
  75. # from that repository.
  76. ORIGIN_PATH="$GIT_PATH/$TOR_MASTER_NAME"
  77. # SC2034 -- shellcheck thinks that these are unused. We know better.
  78. ACTUALLY_THESE_ARE_USED=<<EOF
  79. ${MAINT_029_TB[0]}
  80. ${MAINT_035[0]}
  81. ${MAINT_040[0]}
  82. ${MAINT_041[0]}
  83. ${MAINT_MASTER[0]}
  84. ${RELEASE_029[0]}
  85. ${RELEASE_035[0]}
  86. ${RELEASE_040[0]}
  87. ${RELEASE_041[0]}
  88. EOF
  89. #######################
  90. # Argument processing #
  91. #######################
  92. # Controlled by the -n option. The dry run option will just output the command
  93. # that would have been executed for each worktree.
  94. DRY_RUN=0
  95. # Controlled by the -t <test-branch-prefix> option. The test branch base
  96. # name option makes git-merge-forward.sh create new test branches:
  97. # <tbbn>_029, <tbbn>_035, ... , <tbbn>_master, and merge forward.
  98. TEST_BRANCH_PREFIX=
  99. while getopts "nt:" opt; do
  100. case "$opt" in
  101. n) DRY_RUN=1
  102. echo " *** DRY RUN MODE ***"
  103. ;;
  104. t) TEST_BRANCH_PREFIX="$OPTARG"
  105. echo " *** CREATING TEST BRANCHES: ${TEST_BRANCH_PREFIX}_nnn ***"
  106. ;;
  107. *)
  108. exit 1
  109. ;;
  110. esac
  111. done
  112. ###########################
  113. # Git worktrees to manage #
  114. ###########################
  115. if [ -z "$TEST_BRANCH_PREFIX" ]; then
  116. # maint/release merge mode
  117. #
  118. # List of all worktrees to work on. All defined above. Ordering is important.
  119. # Always the maint-* branch BEFORE then the release-*.
  120. WORKTREE=(
  121. RELEASE_029[@]
  122. MAINT_035[@]
  123. RELEASE_035[@]
  124. MAINT_040[@]
  125. RELEASE_040[@]
  126. MAINT_041[@]
  127. RELEASE_041[@]
  128. MAINT_MASTER[@]
  129. )
  130. else
  131. # Test branch mode: merge to maint only, and create a new branch for 0.2.9
  132. WORKTREE=(
  133. MAINT_029_TB[@]
  134. MAINT_035[@]
  135. MAINT_040[@]
  136. MAINT_041[@]
  137. MAINT_MASTER[@]
  138. )
  139. fi
  140. COUNT=${#WORKTREE[@]}
  141. #############
  142. # Constants #
  143. #############
  144. # Control characters
  145. CNRM=$'\x1b[0;0m' # Clear color
  146. # Bright color
  147. BGRN=$'\x1b[1;32m'
  148. BBLU=$'\x1b[1;34m'
  149. BRED=$'\x1b[1;31m'
  150. BYEL=$'\x1b[1;33m'
  151. IWTH=$'\x1b[3;37m'
  152. # Strings for the pretty print.
  153. MARKER="${BBLU}[${BGRN}+${BBLU}]${CNRM}"
  154. SUCCESS="${BGRN}success${CNRM}"
  155. FAILED="${BRED}failed${CNRM}"
  156. ####################
  157. # Helper functions #
  158. ####################
  159. # Validate the given returned value (error code), print success or failed. The
  160. # second argument is the error output in case of failure, it is printed out.
  161. # On failure, this function exits.
  162. function validate_ret
  163. {
  164. if [ "$1" -eq 0 ]; then
  165. printf "%s\\n" "$SUCCESS"
  166. else
  167. printf "%s\\n" "$FAILED"
  168. printf " %s" "$2"
  169. exit 1
  170. fi
  171. }
  172. # Switch to the given branch name.
  173. function switch_branch
  174. {
  175. local cmd="git checkout $1"
  176. printf " %s Switching branch to %s..." "$MARKER" "$1"
  177. if [ $DRY_RUN -eq 0 ]; then
  178. msg=$( eval "$cmd" 2>&1 )
  179. validate_ret $? "$msg"
  180. else
  181. printf "\\n %s\\n" "${IWTH}$cmd${CNRM}"
  182. fi
  183. }
  184. # Checkout a new branch with the given branch name.
  185. function new_branch
  186. {
  187. local cmd="git checkout -b $1"
  188. printf " %s Creating new branch %s..." "$MARKER" "$1"
  189. if [ $DRY_RUN -eq 0 ]; then
  190. msg=$( eval "$cmd" 2>&1 )
  191. validate_ret $? "$msg"
  192. else
  193. printf "\\n %s\\n" "${IWTH}$cmd${CNRM}"
  194. fi
  195. }
  196. # Pull the given branch name.
  197. function pull_branch
  198. {
  199. local cmd="git pull"
  200. printf " %s Pulling branch %s..." "$MARKER" "$1"
  201. if [ $DRY_RUN -eq 0 ]; then
  202. msg=$( eval "$cmd" 2>&1 )
  203. validate_ret $? "$msg"
  204. else
  205. printf "\\n %s\\n" "${IWTH}$cmd${CNRM}"
  206. fi
  207. }
  208. # Merge the given branch name ($1) into the current branch ($2).
  209. function merge_branch
  210. {
  211. local cmd="git merge --no-edit $1"
  212. printf " %s Merging branch %s into %s..." "$MARKER" "$1" "$2"
  213. if [ $DRY_RUN -eq 0 ]; then
  214. msg=$( eval "$cmd" 2>&1 )
  215. validate_ret $? "$msg"
  216. else
  217. printf "\\n %s\\n" "${IWTH}$cmd${CNRM}"
  218. fi
  219. }
  220. # Pull the given branch name.
  221. function merge_branch_origin
  222. {
  223. local cmd="git merge --ff-only origin/$1"
  224. printf " %s Merging branch origin/%s..." "$MARKER" "$1"
  225. if [ $DRY_RUN -eq 0 ]; then
  226. msg=$( eval "$cmd" 2>&1 )
  227. validate_ret $? "$msg"
  228. else
  229. printf "\\n %s\\n" "${IWTH}$cmd${CNRM}"
  230. fi
  231. }
  232. # Go into the worktree repository.
  233. function goto_repo
  234. {
  235. if [ ! -d "$1" ]; then
  236. echo " $1: Not found. Stopping."
  237. exit 1
  238. fi
  239. cd "$1" || exit
  240. }
  241. # Fetch the origin. No arguments.
  242. function fetch_origin
  243. {
  244. local cmd="git fetch origin"
  245. printf " %s Fetching origin..." "$MARKER"
  246. if [ $DRY_RUN -eq 0 ]; then
  247. msg=$( eval "$cmd" 2>&1 )
  248. validate_ret $? "$msg"
  249. else
  250. printf "\\n %s\\n" "${IWTH}$cmd${CNRM}"
  251. fi
  252. }
  253. ###############
  254. # Entry point #
  255. ###############
  256. # First, fetch the origin.
  257. goto_repo "$ORIGIN_PATH"
  258. fetch_origin
  259. # Go over all configured worktree.
  260. for ((i=0; i<COUNT; i++)); do
  261. current=${!WORKTREE[$i]:0:1}
  262. previous=${!WORKTREE[$i]:1:1}
  263. repo_path=${!WORKTREE[$i]:2:1}
  264. # default to merge forward mode
  265. test_current=
  266. test_previous=
  267. target_current="$current"
  268. target_previous="$previous"
  269. if [ "$TEST_BRANCH_PREFIX" ]; then
  270. test_current_suffix=${!WORKTREE[$i]:3:1}
  271. test_current=${TEST_BRANCH_PREFIX}${test_current_suffix}
  272. # the current test branch, if present, or maint/release branch, if not
  273. target_current="$test_current"
  274. test_previous_suffix=${!WORKTREE[$i]:4:1}
  275. if [ "$test_previous_suffix" ]; then
  276. test_previous=${TEST_BRANCH_PREFIX}${test_previous_suffix}
  277. # the previous test branch, if present, or maint/release branch, if not
  278. target_previous="$test_previous"
  279. fi
  280. fi
  281. printf "%s Handling branch \\n" "$MARKER" "${BYEL}$target${CNRM}"
  282. # Go into the worktree to start merging.
  283. goto_repo "$repo_path"
  284. if [ "$test_current" ]; then
  285. # Create a test branch from the currently checked-out branch/commit
  286. new_branch "$test_current"
  287. fi
  288. # Checkout the current maint/release branch
  289. switch_branch "$current"
  290. # Update the current maint/release branch with an origin merge to get the
  291. # latest updates
  292. merge_branch_origin "$current"
  293. if [ "$test_current" ]; then
  294. # Checkout the test branch
  295. switch_branch "$test_current"
  296. # Merge the updated maint branch into the test branch
  297. merge_branch "$current" "$test_current"
  298. fi
  299. # Merge the previous branch into the target branch
  300. # Merge Forward Example:
  301. # merge maint-0.2.9 into maint-0.3.5.
  302. # Test Branch Example:
  303. # merge bug99999_029 into bug99999_035.
  304. # Skip the merge if the previous branch does not exist
  305. # (there's nothing to merge forward into the oldest test branch)
  306. if [ "$target_previous" ]; then
  307. merge_branch "$target_previous" "$target_current"
  308. fi
  309. done