git-merge-forward.sh 11 KB

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