git-merge-forward.sh 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395
  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. # Controlled by the -u option. The use existing option checks for existing
  100. # branches with the <test-branch-prefix>, and checks them out, rather than
  101. # creating a new branch.
  102. USE_EXISTING=0
  103. while getopts "nt:u" opt; do
  104. case "$opt" in
  105. n) DRY_RUN=1
  106. echo " *** DRY RUN MODE ***"
  107. ;;
  108. t) TEST_BRANCH_PREFIX="$OPTARG"
  109. echo " *** CREATING TEST BRANCHES: ${TEST_BRANCH_PREFIX}_nnn ***"
  110. ;;
  111. u) USE_EXISTING=1
  112. echo " *** USE EXISTING TEST BRANCHES MODE ***"
  113. ;;
  114. *)
  115. exit 1
  116. ;;
  117. esac
  118. done
  119. ###########################
  120. # Git worktrees to manage #
  121. ###########################
  122. if [ -z "$TEST_BRANCH_PREFIX" ]; then
  123. # maint/release merge mode
  124. #
  125. # List of all worktrees to work on. All defined above. Ordering is important.
  126. # Always the maint-* branch BEFORE then the release-*.
  127. WORKTREE=(
  128. RELEASE_029[@]
  129. MAINT_035[@]
  130. RELEASE_035[@]
  131. MAINT_040[@]
  132. RELEASE_040[@]
  133. MAINT_041[@]
  134. RELEASE_041[@]
  135. MAINT_MASTER[@]
  136. )
  137. else
  138. # Test branch mode: merge to maint only, and create a new branch for 0.2.9
  139. WORKTREE=(
  140. MAINT_029_TB[@]
  141. MAINT_035[@]
  142. MAINT_040[@]
  143. MAINT_041[@]
  144. MAINT_MASTER[@]
  145. )
  146. fi
  147. COUNT=${#WORKTREE[@]}
  148. #############
  149. # Constants #
  150. #############
  151. # Control characters
  152. CNRM=$'\x1b[0;0m' # Clear color
  153. # Bright color
  154. BGRN=$'\x1b[1;32m'
  155. BBLU=$'\x1b[1;34m'
  156. BRED=$'\x1b[1;31m'
  157. BYEL=$'\x1b[1;33m'
  158. IWTH=$'\x1b[3;37m'
  159. # Strings for the pretty print.
  160. MARKER="${BBLU}[${BGRN}+${BBLU}]${CNRM}"
  161. SUCCESS="${BGRN}success${CNRM}"
  162. FAILED="${BRED}failed${CNRM}"
  163. ####################
  164. # Helper functions #
  165. ####################
  166. # Validate the given returned value (error code), print success or failed. The
  167. # second argument is the error output in case of failure, it is printed out.
  168. # On failure, this function exits.
  169. function validate_ret
  170. {
  171. if [ "$1" -eq 0 ]; then
  172. printf "%s\\n" "$SUCCESS"
  173. else
  174. printf "%s\\n" "$FAILED"
  175. printf " %s" "$2"
  176. exit 1
  177. fi
  178. }
  179. # Switch to the given branch name.
  180. function switch_branch
  181. {
  182. local cmd="git checkout $1"
  183. printf " %s Switching branch to %s..." "$MARKER" "$1"
  184. if [ $DRY_RUN -eq 0 ]; then
  185. msg=$( eval "$cmd" 2>&1 )
  186. validate_ret $? "$msg"
  187. else
  188. printf "\\n %s\\n" "${IWTH}$cmd${CNRM}"
  189. fi
  190. }
  191. # Checkout a new branch with the given branch name.
  192. function new_branch
  193. {
  194. local cmd="git checkout -b $1"
  195. printf " %s Creating new branch %s..." "$MARKER" "$1"
  196. if [ $DRY_RUN -eq 0 ]; then
  197. msg=$( eval "$cmd" 2>&1 )
  198. validate_ret $? "$msg"
  199. else
  200. printf "\\n %s\\n" "${IWTH}$cmd${CNRM}"
  201. fi
  202. }
  203. # Switch to an existing branch, or checkout a new branch with the given
  204. # branch name.
  205. function switch_or_new_branch
  206. {
  207. local cmd="git rev-parse --verify $1"
  208. if [ $DRY_RUN -eq 0 ]; then
  209. # Call switch_branch if there is a branch, or new_branch if there is not
  210. msg=$( eval "$cmd" 2>&1 )
  211. RET=$?
  212. if [ $RET -eq 0 ]; then
  213. # Branch: (commit id)
  214. switch_branch "$1"
  215. elif [ $RET -eq 128 ]; then
  216. # Not a branch: "fatal: Needed a single revision"
  217. new_branch "$1"
  218. else
  219. # Unexpected return value
  220. validate_ret $RET "$msg"
  221. fi
  222. else
  223. printf "\\n %s\\n" "${IWTH}$cmd${CNRM}, then depending on the result:"
  224. switch_branch "$1"
  225. new_branch "$1"
  226. fi
  227. }
  228. # Pull the given branch name.
  229. function pull_branch
  230. {
  231. local cmd="git pull"
  232. printf " %s Pulling branch %s..." "$MARKER" "$1"
  233. if [ $DRY_RUN -eq 0 ]; then
  234. msg=$( eval "$cmd" 2>&1 )
  235. validate_ret $? "$msg"
  236. else
  237. printf "\\n %s\\n" "${IWTH}$cmd${CNRM}"
  238. fi
  239. }
  240. # Merge the given branch name ($1) into the current branch ($2).
  241. function merge_branch
  242. {
  243. local cmd="git merge --no-edit $1"
  244. printf " %s Merging branch %s into %s..." "$MARKER" "$1" "$2"
  245. if [ $DRY_RUN -eq 0 ]; then
  246. msg=$( eval "$cmd" 2>&1 )
  247. validate_ret $? "$msg"
  248. else
  249. printf "\\n %s\\n" "${IWTH}$cmd${CNRM}"
  250. fi
  251. }
  252. # Pull the given branch name.
  253. function merge_branch_origin
  254. {
  255. local cmd="git merge --ff-only origin/$1"
  256. printf " %s Merging branch origin/%s..." "$MARKER" "$1"
  257. if [ $DRY_RUN -eq 0 ]; then
  258. msg=$( eval "$cmd" 2>&1 )
  259. validate_ret $? "$msg"
  260. else
  261. printf "\\n %s\\n" "${IWTH}$cmd${CNRM}"
  262. fi
  263. }
  264. # Go into the worktree repository.
  265. function goto_repo
  266. {
  267. if [ ! -d "$1" ]; then
  268. echo " $1: Not found. Stopping."
  269. exit 1
  270. fi
  271. cd "$1" || exit
  272. }
  273. # Fetch the origin. No arguments.
  274. function fetch_origin
  275. {
  276. local cmd="git fetch origin"
  277. printf " %s Fetching origin..." "$MARKER"
  278. if [ $DRY_RUN -eq 0 ]; then
  279. msg=$( eval "$cmd" 2>&1 )
  280. validate_ret $? "$msg"
  281. else
  282. printf "\\n %s\\n" "${IWTH}$cmd${CNRM}"
  283. fi
  284. }
  285. ###############
  286. # Entry point #
  287. ###############
  288. # First, fetch the origin.
  289. goto_repo "$ORIGIN_PATH"
  290. fetch_origin
  291. # Go over all configured worktree.
  292. for ((i=0; i<COUNT; i++)); do
  293. current=${!WORKTREE[$i]:0:1}
  294. previous=${!WORKTREE[$i]:1:1}
  295. repo_path=${!WORKTREE[$i]:2:1}
  296. # default to merge forward mode
  297. test_current=
  298. test_previous=
  299. target_current="$current"
  300. target_previous="$previous"
  301. if [ "$TEST_BRANCH_PREFIX" ]; then
  302. test_current_suffix=${!WORKTREE[$i]:3:1}
  303. test_current=${TEST_BRANCH_PREFIX}${test_current_suffix}
  304. # the current test branch, if present, or maint/release branch, if not
  305. target_current="$test_current"
  306. test_previous_suffix=${!WORKTREE[$i]:4:1}
  307. if [ "$test_previous_suffix" ]; then
  308. test_previous=${TEST_BRANCH_PREFIX}${test_previous_suffix}
  309. # the previous test branch, if present, or maint/release branch, if not
  310. target_previous="$test_previous"
  311. fi
  312. fi
  313. printf "%s Handling branch \\n" "$MARKER" "${BYEL}$target${CNRM}"
  314. # Go into the worktree to start merging.
  315. goto_repo "$repo_path"
  316. if [ "$test_current" ]; then
  317. if [ $USE_EXISTING -eq 0 ]; then
  318. # Create a test branch from the currently checked-out branch/commit
  319. # Fail if it already exists
  320. new_branch "$test_current"
  321. else
  322. # Switch if it exists, or create if it does not
  323. switch_or_new_branch "$test_current"
  324. fi
  325. fi
  326. # Checkout the current maint/release branch
  327. switch_branch "$current"
  328. # Update the current maint/release branch with an origin merge to get the
  329. # latest updates
  330. merge_branch_origin "$current"
  331. if [ "$test_current" ]; then
  332. # Checkout the test branch
  333. switch_branch "$test_current"
  334. # Merge the updated maint branch into the test branch
  335. merge_branch "$current" "$test_current"
  336. fi
  337. # Merge the previous branch into the target branch
  338. # Merge Forward Example:
  339. # merge maint-0.2.9 into maint-0.3.5.
  340. # Test Branch Example:
  341. # merge bug99999_029 into bug99999_035.
  342. # Skip the merge if the previous branch does not exist
  343. # (there's nothing to merge forward into the oldest test branch)
  344. if [ "$target_previous" ]; then
  345. merge_branch "$target_previous" "$target_current"
  346. fi
  347. done