git-merge-forward.sh 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407
  1. #!/usr/bin/env bash
  2. # Usage: git-merge-forward.sh -n -t <test-branch-prefix> -u
  3. # arguments:
  4. # -n: dry run mode
  5. # -t: test branch mode: create new branches from the commits checked
  6. # out in each maint directory. Call these branches prefix_029,
  7. # prefix_035, ... , prefix_master.
  8. # -u: in test branch mode, if a prefix_* branch exists, skip creating
  9. # that branch. Use after a merge error, to restart the merge
  10. # forward at the first unmerged branch.
  11. # env vars:
  12. # See the Configuration section for env vars and their default values.
  13. #################
  14. # Configuration #
  15. #################
  16. # Don't change this configuration - set the env vars in your .profile
  17. #
  18. # The general setup that is suggested here is:
  19. #
  20. # GIT_PATH = /home/<user>/git/
  21. # ... where the git repository directories resides.
  22. # TOR_MASTER_NAME = "tor"
  23. # ... which means that tor.git was cloned in /home/<user>/git/tor
  24. # TOR_WKT_NAME = "tor-wkt"
  25. # ... which means that the tor worktrees are in /home/<user>/git/tor-wkt
  26. # Where are all those git repositories?
  27. GIT_PATH=${TOR_FULL_GIT_PATH:-"FULL_PATH_TO_GIT_REPOSITORY_DIRECTORY"}
  28. # The tor master git repository directory from which all the worktree have
  29. # been created.
  30. TOR_MASTER_NAME=${TOR_MASTER_NAME:-"tor"}
  31. # The worktrees location (directory).
  32. TOR_WKT_NAME=${TOR_WKT_NAME:-"tor-wkt"}
  33. ##########################
  34. # Git branches to manage #
  35. ##########################
  36. # The branches and worktrees need to be modified when there is a new branch,
  37. # and when an old branch is no longer supported.
  38. # Configuration of the branches that needs merging. The values are in order:
  39. # (0) current maint/release branch name
  40. # (1) previous maint/release name to merge into (0)
  41. # (only used in merge forward mode)
  42. # (2) Full path of the git worktree
  43. # (3) current branch suffix
  44. # (maint branches only, only used in test branch mode)
  45. # (4) previous test branch suffix to merge into (3)
  46. # (maint branches only, only used in test branch mode)
  47. #
  48. # Merge forward example:
  49. # $ cd <PATH/TO/WORKTREE> (2)
  50. # $ git checkout maint-0.3.5 (0)
  51. # $ git pull
  52. # $ git merge maint-0.3.4 (1)
  53. #
  54. # Test branch example:
  55. # $ cd <PATH/TO/WORKTREE> (2)
  56. # $ git checkout -b ticket99999_035 (3)
  57. # $ git checkout maint-0.3.5 (0)
  58. # $ git pull
  59. # $ git checkout ticket99999_035
  60. # $ git merge maint-0.3.5
  61. # $ git merge ticket99999_034 (4)
  62. #
  63. # First set of arrays are the maint-* branch and then the release-* branch.
  64. # New arrays need to be in the WORKTREE= array else they aren't considered.
  65. #
  66. # Only used in test branch mode
  67. # There is no previous branch to merge forward, so the second and fifth items
  68. # must be blank ("")
  69. MAINT_029_TB=( "maint-0.2.9" "" "$GIT_PATH/$TOR_WKT_NAME/maint-0.2.9" \
  70. "_029" "")
  71. # Used in maint/release merge and test branch modes
  72. MAINT_035=( "maint-0.3.5" "maint-0.2.9" "$GIT_PATH/$TOR_WKT_NAME/maint-0.3.5" \
  73. "_035" "_029")
  74. MAINT_040=( "maint-0.4.0" "maint-0.3.5" "$GIT_PATH/$TOR_WKT_NAME/maint-0.4.0" \
  75. "_040" "_035")
  76. MAINT_041=( "maint-0.4.1" "maint-0.4.0" "$GIT_PATH/$TOR_WKT_NAME/maint-0.4.1" \
  77. "_041" "_040")
  78. MAINT_MASTER=( "master" "maint-0.4.1" "$GIT_PATH/$TOR_MASTER_NAME" \
  79. "_master" "_041")
  80. RELEASE_029=( "release-0.2.9" "maint-0.2.9" "$GIT_PATH/$TOR_WKT_NAME/release-0.2.9" )
  81. RELEASE_035=( "release-0.3.5" "maint-0.3.5" "$GIT_PATH/$TOR_WKT_NAME/release-0.3.5" )
  82. RELEASE_040=( "release-0.4.0" "maint-0.4.0" "$GIT_PATH/$TOR_WKT_NAME/release-0.4.0" )
  83. RELEASE_041=( "release-0.4.1" "maint-0.4.1" "$GIT_PATH/$TOR_WKT_NAME/release-0.4.1" )
  84. # The master branch path has to be the main repository thus contains the
  85. # origin that will be used to fetch the updates. All the worktrees are created
  86. # from that repository.
  87. ORIGIN_PATH="$GIT_PATH/$TOR_MASTER_NAME"
  88. # SC2034 -- shellcheck thinks that these are unused. We know better.
  89. ACTUALLY_THESE_ARE_USED=<<EOF
  90. ${MAINT_029_TB[0]}
  91. ${MAINT_035[0]}
  92. ${MAINT_040[0]}
  93. ${MAINT_041[0]}
  94. ${MAINT_MASTER[0]}
  95. ${RELEASE_029[0]}
  96. ${RELEASE_035[0]}
  97. ${RELEASE_040[0]}
  98. ${RELEASE_041[0]}
  99. EOF
  100. #######################
  101. # Argument processing #
  102. #######################
  103. # Controlled by the -n option. The dry run option will just output the command
  104. # that would have been executed for each worktree.
  105. DRY_RUN=0
  106. # Controlled by the -t <test-branch-prefix> option. The test branch base
  107. # name option makes git-merge-forward.sh create new test branches:
  108. # <tbbn>_029, <tbbn>_035, ... , <tbbn>_master, and merge forward.
  109. TEST_BRANCH_PREFIX=
  110. # Controlled by the -u option. The use existing option checks for existing
  111. # branches with the <test-branch-prefix>, and checks them out, rather than
  112. # creating a new branch.
  113. USE_EXISTING=0
  114. while getopts "nt:u" opt; do
  115. case "$opt" in
  116. n) DRY_RUN=1
  117. echo " *** DRY RUN MODE ***"
  118. ;;
  119. t) TEST_BRANCH_PREFIX="$OPTARG"
  120. echo " *** CREATING TEST BRANCHES: ${TEST_BRANCH_PREFIX}_nnn ***"
  121. ;;
  122. u) USE_EXISTING=1
  123. echo " *** USE EXISTING TEST BRANCHES MODE ***"
  124. ;;
  125. *)
  126. exit 1
  127. ;;
  128. esac
  129. done
  130. ###########################
  131. # Git worktrees to manage #
  132. ###########################
  133. if [ -z "$TEST_BRANCH_PREFIX" ]; then
  134. # maint/release merge mode
  135. #
  136. # List of all worktrees to work on. All defined above. Ordering is important.
  137. # Always the maint-* branch BEFORE then the release-*.
  138. WORKTREE=(
  139. RELEASE_029[@]
  140. MAINT_035[@]
  141. RELEASE_035[@]
  142. MAINT_040[@]
  143. RELEASE_040[@]
  144. MAINT_041[@]
  145. RELEASE_041[@]
  146. MAINT_MASTER[@]
  147. )
  148. else
  149. # Test branch mode: merge to maint only, and create a new branch for 0.2.9
  150. WORKTREE=(
  151. MAINT_029_TB[@]
  152. MAINT_035[@]
  153. MAINT_040[@]
  154. MAINT_041[@]
  155. MAINT_MASTER[@]
  156. )
  157. fi
  158. COUNT=${#WORKTREE[@]}
  159. #############
  160. # Constants #
  161. #############
  162. # Control characters
  163. CNRM=$'\x1b[0;0m' # Clear color
  164. # Bright color
  165. BGRN=$'\x1b[1;32m'
  166. BBLU=$'\x1b[1;34m'
  167. BRED=$'\x1b[1;31m'
  168. BYEL=$'\x1b[1;33m'
  169. IWTH=$'\x1b[3;37m'
  170. # Strings for the pretty print.
  171. MARKER="${BBLU}[${BGRN}+${BBLU}]${CNRM}"
  172. SUCCESS="${BGRN}success${CNRM}"
  173. FAILED="${BRED}failed${CNRM}"
  174. ####################
  175. # Helper functions #
  176. ####################
  177. # Validate the given returned value (error code), print success or failed. The
  178. # second argument is the error output in case of failure, it is printed out.
  179. # On failure, this function exits.
  180. function validate_ret
  181. {
  182. if [ "$1" -eq 0 ]; then
  183. printf "%s\\n" "$SUCCESS"
  184. else
  185. printf "%s\\n" "$FAILED"
  186. printf " %s" "$2"
  187. exit 1
  188. fi
  189. }
  190. # Switch to the given branch name.
  191. function switch_branch
  192. {
  193. local cmd="git checkout '$1'"
  194. printf " %s Switching branch to %s..." "$MARKER" "$1"
  195. if [ $DRY_RUN -eq 0 ]; then
  196. msg=$( eval "$cmd" 2>&1 )
  197. validate_ret $? "$msg"
  198. else
  199. printf "\\n %s\\n" "${IWTH}$cmd${CNRM}"
  200. fi
  201. }
  202. # Checkout a new branch with the given branch name.
  203. function new_branch
  204. {
  205. local cmd="git checkout -b '$1'"
  206. printf " %s Creating new branch %s..." "$MARKER" "$1"
  207. if [ $DRY_RUN -eq 0 ]; then
  208. msg=$( eval "$cmd" 2>&1 )
  209. validate_ret $? "$msg"
  210. else
  211. printf "\\n %s\\n" "${IWTH}$cmd${CNRM}"
  212. fi
  213. }
  214. # Switch to an existing branch, or checkout a new branch with the given
  215. # branch name.
  216. function switch_or_new_branch
  217. {
  218. local cmd="git rev-parse --verify '$1'"
  219. if [ $DRY_RUN -eq 0 ]; then
  220. # Call switch_branch if there is a branch, or new_branch if there is not
  221. msg=$( eval "$cmd" 2>&1 )
  222. RET=$?
  223. if [ $RET -eq 0 ]; then
  224. # Branch: (commit id)
  225. switch_branch "$1"
  226. elif [ $RET -eq 128 ]; then
  227. # Not a branch: "fatal: Needed a single revision"
  228. new_branch "$1"
  229. else
  230. # Unexpected return value
  231. validate_ret $RET "$msg"
  232. fi
  233. else
  234. printf "\\n %s\\n" "${IWTH}$cmd${CNRM}, then depending on the result:"
  235. switch_branch "$1"
  236. new_branch "$1"
  237. fi
  238. }
  239. # Pull the given branch name.
  240. function pull_branch
  241. {
  242. local cmd="git pull"
  243. printf " %s Pulling branch %s..." "$MARKER" "$1"
  244. if [ $DRY_RUN -eq 0 ]; then
  245. msg=$( eval "$cmd" 2>&1 )
  246. validate_ret $? "$msg"
  247. else
  248. printf "\\n %s\\n" "${IWTH}$cmd${CNRM}"
  249. fi
  250. }
  251. # Merge the given branch name ($1) into the current branch ($2).
  252. function merge_branch
  253. {
  254. local cmd="git merge --no-edit '$1'"
  255. printf " %s Merging branch %s into %s..." "$MARKER" "$1" "$2"
  256. if [ $DRY_RUN -eq 0 ]; then
  257. msg=$( eval "$cmd" 2>&1 )
  258. validate_ret $? "$msg"
  259. else
  260. printf "\\n %s\\n" "${IWTH}$cmd${CNRM}"
  261. fi
  262. }
  263. # Pull the given branch name.
  264. function merge_branch_origin
  265. {
  266. local cmd="git merge --ff-only 'origin/$1'"
  267. printf " %s Merging branch origin/%s..." "$MARKER" "$1"
  268. if [ $DRY_RUN -eq 0 ]; then
  269. msg=$( eval "$cmd" 2>&1 )
  270. validate_ret $? "$msg"
  271. else
  272. printf "\\n %s\\n" "${IWTH}$cmd${CNRM}"
  273. fi
  274. }
  275. # Go into the worktree repository.
  276. function goto_repo
  277. {
  278. if [ ! -d "$1" ]; then
  279. echo " $1: Not found. Stopping."
  280. exit 1
  281. fi
  282. cd "$1" || exit
  283. }
  284. # Fetch the origin. No arguments.
  285. function fetch_origin
  286. {
  287. local cmd="git fetch origin"
  288. printf " %s Fetching origin..." "$MARKER"
  289. if [ $DRY_RUN -eq 0 ]; then
  290. msg=$( eval "$cmd" 2>&1 )
  291. validate_ret $? "$msg"
  292. else
  293. printf "\\n %s\\n" "${IWTH}$cmd${CNRM}"
  294. fi
  295. }
  296. ###############
  297. # Entry point #
  298. ###############
  299. # First, fetch the origin.
  300. goto_repo "$ORIGIN_PATH"
  301. fetch_origin
  302. # Go over all configured worktree.
  303. for ((i=0; i<COUNT; i++)); do
  304. current=${!WORKTREE[$i]:0:1}
  305. previous=${!WORKTREE[$i]:1:1}
  306. repo_path=${!WORKTREE[$i]:2:1}
  307. # default to merge forward mode
  308. test_current=
  309. test_previous=
  310. target_current="$current"
  311. target_previous="$previous"
  312. if [ "$TEST_BRANCH_PREFIX" ]; then
  313. test_current_suffix=${!WORKTREE[$i]:3:1}
  314. test_current=${TEST_BRANCH_PREFIX}${test_current_suffix}
  315. # the current test branch, if present, or maint/release branch, if not
  316. target_current="$test_current"
  317. test_previous_suffix=${!WORKTREE[$i]:4:1}
  318. if [ "$test_previous_suffix" ]; then
  319. test_previous=${TEST_BRANCH_PREFIX}${test_previous_suffix}
  320. # the previous test branch, if present, or maint/release branch, if not
  321. target_previous="$test_previous"
  322. fi
  323. fi
  324. printf "%s Handling branch \\n" "$MARKER" "${BYEL}$target_current${CNRM}"
  325. # Go into the worktree to start merging.
  326. goto_repo "$repo_path"
  327. if [ "$test_current" ]; then
  328. if [ $USE_EXISTING -eq 0 ]; then
  329. # Create a test branch from the currently checked-out branch/commit
  330. # Fail if it already exists
  331. new_branch "$test_current"
  332. else
  333. # Switch if it exists, or create if it does not
  334. switch_or_new_branch "$test_current"
  335. fi
  336. fi
  337. # Checkout the current maint/release branch
  338. switch_branch "$current"
  339. # Update the current maint/release branch with an origin merge to get the
  340. # latest updates
  341. merge_branch_origin "$current"
  342. if [ "$test_current" ]; then
  343. # Checkout the test branch
  344. switch_branch "$test_current"
  345. # Merge the updated maint branch into the test branch
  346. merge_branch "$current" "$test_current"
  347. fi
  348. # Merge the previous branch into the target branch
  349. # Merge Forward Example:
  350. # merge maint-0.2.9 into maint-0.3.5.
  351. # Test Branch Example:
  352. # merge bug99999_029 into bug99999_035.
  353. # Skip the merge if the previous branch does not exist
  354. # (there's nothing to merge forward into the oldest test branch)
  355. if [ "$target_previous" ]; then
  356. merge_branch "$target_previous" "$target_current"
  357. fi
  358. done