git-pull-all.sh 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. #!/bin/bash
  2. ##################################
  3. # User 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 to pull (update).
  25. # (2) Full path of the git worktree.
  26. #
  27. # As an example:
  28. # $ cd <PATH/TO/WORKTREE> (3)
  29. # $ git checkout maint-0.3.5 (1)
  30. # $ git pull
  31. #
  32. # First set of arrays are the maint-* branch and then the release-* branch.
  33. # New arrays need to be in the WORKTREE= array else they aren't considered.
  34. MAINT_029=( "maint-0.2.9" "$GIT_PATH/$TOR_WKT_NAME/maint-0.2.9" )
  35. MAINT_034=( "maint-0.3.4" "$GIT_PATH/$TOR_WKT_NAME/maint-0.3.4" )
  36. MAINT_035=( "maint-0.3.5" "$GIT_PATH/$TOR_WKT_NAME/maint-0.3.5" )
  37. MAINT_040=( "maint-0.4.0" "$GIT_PATH/$TOR_WKT_NAME/maint-0.4.0" )
  38. MAINT_MASTER=( "master" "$GIT_PATH/$TOR_MASTER_NAME" )
  39. RELEASE_029=( "release-0.2.9" "$GIT_PATH/$TOR_WKT_NAME/release-0.2.9" )
  40. RELEASE_034=( "release-0.3.4" "$GIT_PATH/$TOR_WKT_NAME/release-0.3.4" )
  41. RELEASE_035=( "release-0.3.5" "$GIT_PATH/$TOR_WKT_NAME/release-0.3.5" )
  42. RELEASE_040=( "release-0.4.0" "$GIT_PATH/$TOR_WKT_NAME/release-0.4.0" )
  43. # The master branch path has to be the main repository thus contains the
  44. # origin that will be used to fetch the updates. All the worktrees are created
  45. # from that repository.
  46. ORIGIN_PATH="$GIT_PATH/$TOR_MASTER_NAME"
  47. # SC2034 -- shellcheck thinks that these are unused. We know better.
  48. ACTUALLY_THESE_ARE_USED=<<EOF
  49. ${MAINT_029[0]}
  50. ${MAINT_034[0]}
  51. ${MAINT_035[0]}
  52. ${MAINT_040[0]}
  53. ${MAINT_MASTER[0]}
  54. ${RELEASE_029[0]}
  55. ${RELEASE_034[0]}
  56. ${RELEASE_035[0]}
  57. ${RELEASE_040[0]}
  58. EOF
  59. ##########################
  60. # Git Worktree to manage #
  61. ##########################
  62. # List of all worktrees to work on. All defined above. Ordering is important.
  63. # Always the maint-* branch first then the release-*.
  64. WORKTREE=(
  65. MAINT_029[@]
  66. RELEASE_029[@]
  67. MAINT_034[@]
  68. RELEASE_034[@]
  69. MAINT_035[@]
  70. RELEASE_035[@]
  71. MAINT_040[@]
  72. RELEASE_040[@]
  73. MAINT_MASTER[@]
  74. )
  75. COUNT=${#WORKTREE[@]}
  76. # Controlled by the -n option. The dry run option will just output the command
  77. # that would have been executed for each worktree.
  78. DRY_RUN=0
  79. # Control characters
  80. CNRM=$'\x1b[0;0m' # Clear color
  81. # Bright color
  82. BGRN=$'\x1b[1;32m'
  83. BBLU=$'\x1b[1;34m'
  84. BRED=$'\x1b[1;31m'
  85. BYEL=$'\x1b[1;33m'
  86. IWTH=$'\x1b[3;37m'
  87. # Strings for the pretty print.
  88. MARKER="${BBLU}[${BGRN}+${BBLU}]${CNRM}"
  89. SUCCESS="${BGRN}ok${CNRM}"
  90. FAILED="${BRED}failed${CNRM}"
  91. ####################
  92. # Helper functions #
  93. ####################
  94. # Validate the given returned value (error code), print success or failed. The
  95. # second argument is the error output in case of failure, it is printed out.
  96. # On failure, this function exits.
  97. function validate_ret
  98. {
  99. if [ "$1" -eq 0 ]; then
  100. printf "%s\\n" "$SUCCESS"
  101. else
  102. printf "%s\\n" "$FAILED"
  103. printf " %s" "$2"
  104. exit 1
  105. fi
  106. }
  107. # Switch to the given branch name.
  108. function switch_branch
  109. {
  110. local cmd="git checkout $1"
  111. printf " %s Switching branch to %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. # Pull the given branch name.
  120. function merge_branch
  121. {
  122. local cmd="git merge --ff-only origin/$1"
  123. printf " %s Merging branch origin/%s..." "$MARKER" "$1"
  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. # Fetch the origin. No arguments.
  141. function fetch_origin
  142. {
  143. local cmd="git fetch origin"
  144. printf " %s Fetching origin..." "$MARKER"
  145. if [ $DRY_RUN -eq 0 ]; then
  146. msg=$( eval "$cmd" 2>&1 )
  147. validate_ret $? "$msg"
  148. else
  149. printf "\\n %s\\n" "${IWTH}$cmd${CNRM}"
  150. fi
  151. }
  152. # Fetch tor-github pull requests. No arguments.
  153. function fetch_tor_github
  154. {
  155. local cmd="git fetch tor-github"
  156. printf " %s Fetching tor-github..." "$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 tor-github.
  177. goto_repo "$ORIGIN_PATH"
  178. fetch_tor_github
  179. # Then, fetch the origin.
  180. fetch_origin
  181. # Go over all configured worktree.
  182. for ((i=0; i<COUNT; i++)); do
  183. current=${!WORKTREE[$i]:0:1}
  184. repo_path=${!WORKTREE[$i]:1:1}
  185. printf "%s Handling branch %s\\n" "$MARKER" "${BYEL}$current${CNRM}"
  186. # Go into the worktree to start merging.
  187. goto_repo "$repo_path"
  188. # Checkout the current branch
  189. switch_branch "$current"
  190. # Update the current branch by merging the origin to get the latest.
  191. merge_branch "$current"
  192. done