git-merge-forward.sh 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  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_033=( "maint-0.3.3" "maint-0.2.9" "$GIT_PATH/$TOR_WKT_NAME/maint-0.3.3" )
  37. MAINT_034=( "maint-0.3.4" "maint-0.3.3" "$GIT_PATH/$TOR_WKT_NAME/maint-0.3.4" )
  38. MAINT_035=( "maint-0.3.5" "maint-0.3.4" "$GIT_PATH/$TOR_WKT_NAME/maint-0.3.5" )
  39. MAINT_040=( "maint-0.4.0" "maint-0.3.5" "$GIT_PATH/$TOR_WKT_NAME/maint-0.4.0" )
  40. MAINT_MASTER=( "master" "maint-0.4.0" "$GIT_PATH/$TOR_MASTER_NAME" )
  41. RELEASE_029=( "release-0.2.9" "maint-0.2.9" "$GIT_PATH/$TOR_WKT_NAME/release-0.2.9" )
  42. RELEASE_033=( "release-0.3.3" "maint-0.3.3" "$GIT_PATH/$TOR_WKT_NAME/release-0.3.3" )
  43. RELEASE_034=( "release-0.3.4" "maint-0.3.4" "$GIT_PATH/$TOR_WKT_NAME/release-0.3.4" )
  44. RELEASE_035=( "release-0.3.5" "maint-0.3.5" "$GIT_PATH/$TOR_WKT_NAME/release-0.3.5" )
  45. RELEASE_040=( "release-0.4.0" "maint-0.4.0" "$GIT_PATH/$TOR_WKT_NAME/release-0.4.0" )
  46. ##########################
  47. # Git Worktree to manage #
  48. ##########################
  49. # List of all worktrees to work on. All defined above. Ordering is important.
  50. # Always the maint-* branch BEFORE then the release-*.
  51. WORKTREE=(
  52. RELEASE_029[@]
  53. MAINT_033[@]
  54. RELEASE_033[@]
  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. # Go into the worktree repository.
  132. function goto_repo
  133. {
  134. if [ ! -d "$1" ]; then
  135. echo " $1: Not found. Stopping."
  136. exit 1
  137. fi
  138. cd "$1" || exit
  139. }
  140. ###############
  141. # Entry point #
  142. ###############
  143. while getopts "n" opt; do
  144. case "$opt" in
  145. n) DRY_RUN=1
  146. echo " *** DRY DRUN MODE ***"
  147. ;;
  148. *)
  149. ;;
  150. esac
  151. done
  152. # Go over all configured worktree.
  153. for ((i=0; i<COUNT; i++)); do
  154. current=${!WORKTREE[$i]:0:1}
  155. previous=${!WORKTREE[$i]:1:1}
  156. repo_path=${!WORKTREE[$i]:2:1}
  157. printf "%s Handling branch \\n" "$MARKER" "${BYEL}$current${CNRM}"
  158. # Go into the worktree to start merging.
  159. goto_repo "$repo_path"
  160. # Checkout the current branch
  161. switch_branch "$current"
  162. # Update the current branch with a pull to get the latest.
  163. pull_branch "$current"
  164. # Merge the previous branch. Ex: merge maint-0.2.5 into maint-0.2.9.
  165. merge_branch "$previous" "$current"
  166. done