git-merge-forward.sh 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. #!/bin/bash
  2. ##############################
  3. # Configuration (change me!) #
  4. ##############################
  5. # The general setup that is suggested here is:
  6. #
  7. # GIT_PATH = /home/<user>/git/
  8. # ... where the git repository directories resides.
  9. # TOR_MASTER_NAME = "tor"
  10. # ... which means that tor.git was cloned in /home/<user>/git/tor
  11. # TOR_WKT_NAME = "tor-wkt"
  12. # ... which means that the tor worktrees are in /home/<user>/git/tor-wkt
  13. # Where are all those git repositories?
  14. GIT_PATH="FULL_PATH_TO_GIT_REPOSITORY_DIRECTORY"
  15. # The tor master git repository directory from which all the worktree have
  16. # been created.
  17. TOR_MASTER_NAME="tor"
  18. # The worktrees location (directory).
  19. TOR_WKT_NAME="tor-wkt"
  20. #########################
  21. # End of configuration. #
  22. #########################
  23. # Configuration of the branches that needs merging. The values are in order:
  24. # (1) Branch name that we merge onto.
  25. # (2) Branch name to merge from. In other words, this is merge into (1)
  26. # (3) Full path of the git worktree.
  27. #
  28. # As an example:
  29. # $ cd <PATH/TO/WORKTREE> (3)
  30. # $ git checkout maint-0.3.5 (1)
  31. # $ git pull
  32. # $ git merge maint-0.3.4 (2)
  33. #
  34. # First set of arrays are the maint-* branch and then the release-* branch.
  35. # New arrays need to be in the WORKTREE= array else they aren't considered.
  36. MAINT_034=( "maint-0.3.4" "maint-0.2.9" "$GIT_PATH/$TOR_WKT_NAME/maint-0.3.4" )
  37. MAINT_035=( "maint-0.3.5" "maint-0.3.4" "$GIT_PATH/$TOR_WKT_NAME/maint-0.3.5" )
  38. MAINT_040=( "maint-0.4.0" "maint-0.3.5" "$GIT_PATH/$TOR_WKT_NAME/maint-0.4.0" )
  39. MAINT_MASTER=( "master" "maint-0.4.0" "$GIT_PATH/$TOR_MASTER_NAME" )
  40. RELEASE_029=( "release-0.2.9" "maint-0.2.9" "$GIT_PATH/$TOR_WKT_NAME/release-0.2.9" )
  41. RELEASE_034=( "release-0.3.4" "maint-0.3.4" "$GIT_PATH/$TOR_WKT_NAME/release-0.3.4" )
  42. RELEASE_035=( "release-0.3.5" "maint-0.3.5" "$GIT_PATH/$TOR_WKT_NAME/release-0.3.5" )
  43. RELEASE_040=( "release-0.4.0" "maint-0.4.0" "$GIT_PATH/$TOR_WKT_NAME/release-0.4.0" )
  44. # The master branch path has to be the main repository thus contains the
  45. # origin that will be used to fetch the updates. All the worktrees are created
  46. # from that repository.
  47. ORIGIN_PATH="$GIT_PATH/$TOR_MASTER_NAME"
  48. ##########################
  49. # Git Worktree to manage #
  50. ##########################
  51. # List of all worktrees to work on. All defined above. Ordering is important.
  52. # Always the maint-* branch BEFORE then the release-*.
  53. WORKTREE=(
  54. RELEASE_029[@]
  55. MAINT_034[@]
  56. RELEASE_034[@]
  57. MAINT_035[@]
  58. RELEASE_035[@]
  59. MAINT_040[@]
  60. RELEASE_040[@]
  61. MAINT_MASTER[@]
  62. )
  63. COUNT=${#WORKTREE[@]}
  64. # Controlled by the -n option. The dry run option will just output the command
  65. # that would have been executed for each worktree.
  66. DRY_RUN=0
  67. # Control characters
  68. CNRM=$'\x1b[0;0m' # Clear color
  69. # Bright color
  70. BGRN=$'\x1b[1;32m'
  71. BBLU=$'\x1b[1;34m'
  72. BRED=$'\x1b[1;31m'
  73. BYEL=$'\x1b[1;33m'
  74. IWTH=$'\x1b[3;37m'
  75. # Strings for the pretty print.
  76. MARKER="${BBLU}[${BGRN}+${BBLU}]${CNRM}"
  77. SUCCESS="${BGRN}success${CNRM}"
  78. FAILED="${BRED}failed${CNRM}"
  79. ####################
  80. # Helper functions #
  81. ####################
  82. # Validate the given returned value (error code), print success or failed. The
  83. # second argument is the error output in case of failure, it is printed out.
  84. # On failure, this function exits.
  85. function validate_ret
  86. {
  87. if [ "$1" -eq 0 ]; then
  88. printf "%s\\n" "$SUCCESS"
  89. else
  90. printf "%s\\n" "$FAILED"
  91. printf " %s" "$2"
  92. exit 1
  93. fi
  94. }
  95. # Switch to the given branch name.
  96. function switch_branch
  97. {
  98. local cmd="git checkout $1"
  99. printf " %s Switching branch to %s..." "$MARKER" "$1"
  100. if [ $DRY_RUN -eq 0 ]; then
  101. msg=$( eval "$cmd" 2>&1 )
  102. validate_ret $? "$msg"
  103. else
  104. printf "\\n %s\\n" "${IWTH}$cmd${CNRM}"
  105. fi
  106. }
  107. # Pull the given branch name.
  108. function pull_branch
  109. {
  110. local cmd="git pull"
  111. printf " %s Pulling branch %s..." "$MARKER" "$1"
  112. if [ $DRY_RUN -eq 0 ]; then
  113. msg=$( eval "$cmd" 2>&1 )
  114. validate_ret $? "$msg"
  115. else
  116. printf "\\n %s\\n" "${IWTH}$cmd${CNRM}"
  117. fi
  118. }
  119. # Merge the given branch name ($2) into the current branch ($1).
  120. function merge_branch
  121. {
  122. local cmd="git merge --no-edit $1"
  123. printf " %s Merging branch %s into %s..." "$MARKER" "$1" "$2"
  124. if [ $DRY_RUN -eq 0 ]; then
  125. msg=$( eval "$cmd" 2>&1 )
  126. validate_ret $? "$msg"
  127. else
  128. printf "\\n %s\\n" "${IWTH}$cmd${CNRM}"
  129. fi
  130. }
  131. # Pull the given branch name.
  132. function merge_branch_origin
  133. {
  134. local cmd="git merge --ff-only origin/$1"
  135. printf " %s Merging branch origin/%s..." "$MARKER" "$1"
  136. if [ $DRY_RUN -eq 0 ]; then
  137. msg=$( eval "$cmd" 2>&1 )
  138. validate_ret $? "$msg"
  139. else
  140. printf "\\n %s\\n" "${IWTH}$cmd${CNRM}"
  141. fi
  142. }
  143. # Go into the worktree repository.
  144. function goto_repo
  145. {
  146. if [ ! -d "$1" ]; then
  147. echo " $1: Not found. Stopping."
  148. exit 1
  149. fi
  150. cd "$1" || exit
  151. }
  152. # Fetch the origin. No arguments.
  153. function fetch_origin
  154. {
  155. local cmd="git fetch origin"
  156. printf " %s Fetching origin..." "$MARKER"
  157. if [ $DRY_RUN -eq 0 ]; then
  158. msg=$( eval "$cmd" 2>&1 )
  159. validate_ret $? "$msg"
  160. else
  161. printf "\\n %s\\n" "${IWTH}$cmd${CNRM}"
  162. fi
  163. }
  164. ###############
  165. # Entry point #
  166. ###############
  167. while getopts "n" opt; do
  168. case "$opt" in
  169. n) DRY_RUN=1
  170. echo " *** DRY DRUN MODE ***"
  171. ;;
  172. *)
  173. ;;
  174. esac
  175. done
  176. # First, fetch the origin.
  177. goto_repo "$ORIGIN_PATH"
  178. fetch_origin
  179. # Go over all configured worktree.
  180. for ((i=0; i<COUNT; i++)); do
  181. current=${!WORKTREE[$i]:0:1}
  182. previous=${!WORKTREE[$i]:1:1}
  183. repo_path=${!WORKTREE[$i]:2:1}
  184. printf "%s Handling branch \\n" "$MARKER" "${BYEL}$current${CNRM}"
  185. # Go into the worktree to start merging.
  186. goto_repo "$repo_path"
  187. # Checkout the current branch
  188. switch_branch "$current"
  189. # Update the current branch with an origin merge to get the latest.
  190. merge_branch_origin "$current"
  191. # Merge the previous branch. Ex: merge maint-0.2.5 into maint-0.2.9.
  192. merge_branch "$previous" "$current"
  193. done