git-merge-forward.sh 12 KB

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