git-merge-forward.sh 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  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. ##########################
  45. # Git Worktree to manage #
  46. ##########################
  47. # List of all worktrees to work on. All defined above. Ordering is important.
  48. # Always the maint-* branch BEFORE then the release-*.
  49. WORKTREE=(
  50. RELEASE_029[@]
  51. MAINT_034[@]
  52. RELEASE_034[@]
  53. MAINT_035[@]
  54. RELEASE_035[@]
  55. MAINT_040[@]
  56. RELEASE_040[@]
  57. MAINT_MASTER[@]
  58. )
  59. COUNT=${#WORKTREE[@]}
  60. # Controlled by the -n option. The dry run option will just output the command
  61. # that would have been executed for each worktree.
  62. DRY_RUN=0
  63. # Control characters
  64. CNRM=$'\x1b[0;0m' # Clear color
  65. # Bright color
  66. BGRN=$'\x1b[1;32m'
  67. BBLU=$'\x1b[1;34m'
  68. BRED=$'\x1b[1;31m'
  69. BYEL=$'\x1b[1;33m'
  70. IWTH=$'\x1b[3;37m'
  71. # Strings for the pretty print.
  72. MARKER="${BBLU}[${BGRN}+${BBLU}]${CNRM}"
  73. SUCCESS="${BGRN}success${CNRM}"
  74. FAILED="${BRED}failed${CNRM}"
  75. ####################
  76. # Helper functions #
  77. ####################
  78. # Validate the given returned value (error code), print success or failed. The
  79. # second argument is the error output in case of failure, it is printed out.
  80. # On failure, this function exits.
  81. function validate_ret
  82. {
  83. if [ "$1" -eq 0 ]; then
  84. printf "%s\\n" "$SUCCESS"
  85. else
  86. printf "%s\\n" "$FAILED"
  87. printf " %s" "$2"
  88. exit 1
  89. fi
  90. }
  91. # Switch to the given branch name.
  92. function switch_branch
  93. {
  94. local cmd="git checkout $1"
  95. printf " %s Switching branch to %s..." "$MARKER" "$1"
  96. if [ $DRY_RUN -eq 0 ]; then
  97. msg=$( eval "$cmd" 2>&1 )
  98. validate_ret $? "$msg"
  99. else
  100. printf "\\n %s\\n" "${IWTH}$cmd${CNRM}"
  101. fi
  102. }
  103. # Pull the given branch name.
  104. function pull_branch
  105. {
  106. local cmd="git pull"
  107. printf " %s Pulling branch %s..." "$MARKER" "$1"
  108. if [ $DRY_RUN -eq 0 ]; then
  109. msg=$( eval "$cmd" 2>&1 )
  110. validate_ret $? "$msg"
  111. else
  112. printf "\\n %s\\n" "${IWTH}$cmd${CNRM}"
  113. fi
  114. }
  115. # Merge the given branch name ($2) into the current branch ($1).
  116. function merge_branch
  117. {
  118. local cmd="git merge --no-edit $1"
  119. printf " %s Merging branch %s into %s..." "$MARKER" "$1" "$2"
  120. if [ $DRY_RUN -eq 0 ]; then
  121. msg=$( eval "$cmd" 2>&1 )
  122. validate_ret $? "$msg"
  123. else
  124. printf "\\n %s\\n" "${IWTH}$cmd${CNRM}"
  125. fi
  126. }
  127. # Go into the worktree repository.
  128. function goto_repo
  129. {
  130. if [ ! -d "$1" ]; then
  131. echo " $1: Not found. Stopping."
  132. exit 1
  133. fi
  134. cd "$1" || exit
  135. }
  136. ###############
  137. # Entry point #
  138. ###############
  139. while getopts "n" opt; do
  140. case "$opt" in
  141. n) DRY_RUN=1
  142. echo " *** DRY DRUN MODE ***"
  143. ;;
  144. *)
  145. ;;
  146. esac
  147. done
  148. # Go over all configured worktree.
  149. for ((i=0; i<COUNT; i++)); do
  150. current=${!WORKTREE[$i]:0:1}
  151. previous=${!WORKTREE[$i]:1:1}
  152. repo_path=${!WORKTREE[$i]:2:1}
  153. printf "%s Handling branch \\n" "$MARKER" "${BYEL}$current${CNRM}"
  154. # Go into the worktree to start merging.
  155. goto_repo "$repo_path"
  156. # Checkout the current branch
  157. switch_branch "$current"
  158. # Update the current branch with a pull to get the latest.
  159. pull_branch "$current"
  160. # Merge the previous branch. Ex: merge maint-0.2.5 into maint-0.2.9.
  161. merge_branch "$previous" "$current"
  162. done