git-pull-all.sh 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. #!/usr/bin/env bash
  2. SCRIPT_NAME=`basename $0`
  3. function usage()
  4. {
  5. echo "$SCRIPT_NAME [-h] [-n]"
  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
  12. echo " env vars:"
  13. echo " required:"
  14. echo " TOR_FULL_GIT_PATH: where the git repository directories reside."
  15. echo " You must set this env var, we recommend \$HOME/git/"
  16. echo " (default: fail if this env var is not set;"
  17. echo " current: $GIT_PATH)"
  18. echo
  19. echo " optional:"
  20. echo " TOR_MASTER: the name of the directory containing the tor.git clone"
  21. echo " The tor master git directory is \$GIT_PATH/\$TOR_MASTER"
  22. echo " (default: tor; current: $TOR_MASTER_NAME)"
  23. echo " TOR_WKT_NAME: the name of the directory containing the tor"
  24. echo " worktrees. The tor worktrees are:"
  25. echo " \$GIT_PATH/\$TOR_WKT_NAME/{maint-*,release-*}"
  26. echo " (default: tor-wkt; current: $TOR_WKT_NAME)"
  27. echo " we recommend that you set these env vars in your ~/.profile"
  28. }
  29. #################
  30. # Configuration #
  31. #################
  32. # Don't change this configuration - set the env vars in your .profile
  33. # Where are all those git repositories?
  34. GIT_PATH=${TOR_FULL_GIT_PATH:-"FULL_PATH_TO_GIT_REPOSITORY_DIRECTORY"}
  35. # The tor master git repository directory from which all the worktree have
  36. # been created.
  37. TOR_MASTER_NAME=${TOR_MASTER_NAME:-"tor"}
  38. # The worktrees location (directory).
  39. TOR_WKT_NAME=${TOR_WKT_NAME:-"tor-wkt"}
  40. ##########################
  41. # Git branches to manage #
  42. ##########################
  43. # Configuration of the branches that need pulling. The values are in order:
  44. # (1) Branch name to pull (update).
  45. # (2) Full path of the git worktree.
  46. #
  47. # As an example:
  48. # $ cd <PATH/TO/WORKTREE> (3)
  49. # $ git checkout maint-0.3.5 (1)
  50. # $ git pull
  51. #
  52. # First set of arrays are the maint-* branch and then the release-* branch.
  53. # New arrays need to be in the WORKTREE= array else they aren't considered.
  54. MAINT_029=( "maint-0.2.9" "$GIT_PATH/$TOR_WKT_NAME/maint-0.2.9" )
  55. MAINT_035=( "maint-0.3.5" "$GIT_PATH/$TOR_WKT_NAME/maint-0.3.5" )
  56. MAINT_040=( "maint-0.4.0" "$GIT_PATH/$TOR_WKT_NAME/maint-0.4.0" )
  57. MAINT_041=( "maint-0.4.1" "$GIT_PATH/$TOR_WKT_NAME/maint-0.4.1" )
  58. MAINT_MASTER=( "master" "$GIT_PATH/$TOR_MASTER_NAME" )
  59. RELEASE_029=( "release-0.2.9" "$GIT_PATH/$TOR_WKT_NAME/release-0.2.9" )
  60. RELEASE_035=( "release-0.3.5" "$GIT_PATH/$TOR_WKT_NAME/release-0.3.5" )
  61. RELEASE_040=( "release-0.4.0" "$GIT_PATH/$TOR_WKT_NAME/release-0.4.0" )
  62. RELEASE_041=( "release-0.4.1" "$GIT_PATH/$TOR_WKT_NAME/release-0.4.1" )
  63. # The master branch path has to be the main repository thus contains the
  64. # origin that will be used to fetch the updates. All the worktrees are created
  65. # from that repository.
  66. ORIGIN_PATH="$GIT_PATH/$TOR_MASTER_NAME"
  67. # SC2034 -- shellcheck thinks that these are unused. We know better.
  68. ACTUALLY_THESE_ARE_USED=<<EOF
  69. ${MAINT_029[0]}
  70. ${MAINT_035[0]}
  71. ${MAINT_040[0]}
  72. ${MAINT_041[0]}
  73. ${MAINT_MASTER[0]}
  74. ${RELEASE_029[0]}
  75. ${RELEASE_035[0]}
  76. ${RELEASE_040[0]}
  77. ${RELEASE_041[0]}
  78. EOF
  79. ###########################
  80. # Git worktrees to manage #
  81. ###########################
  82. # List of all worktrees to work on. All defined above. Ordering is important.
  83. # Always the maint-* branch first then the release-*.
  84. WORKTREE=(
  85. MAINT_029[@]
  86. RELEASE_029[@]
  87. MAINT_035[@]
  88. RELEASE_035[@]
  89. MAINT_040[@]
  90. RELEASE_040[@]
  91. MAINT_041[@]
  92. RELEASE_041[@]
  93. MAINT_MASTER[@]
  94. )
  95. COUNT=${#WORKTREE[@]}
  96. #######################
  97. # Argument processing #
  98. #######################
  99. # Controlled by the -n option. The dry run option will just output the command
  100. # that would have been executed for each worktree.
  101. DRY_RUN=0
  102. while getopts "hn" opt; do
  103. case "$opt" in
  104. h) usage
  105. exit 0
  106. ;;
  107. n) DRY_RUN=1
  108. echo " *** DRY DRUN MODE ***"
  109. ;;
  110. *)
  111. echo
  112. usage
  113. exit 1
  114. ;;
  115. esac
  116. done
  117. #############
  118. # Constants #
  119. #############
  120. # Control characters
  121. CNRM=$'\x1b[0;0m' # Clear color
  122. # Bright color
  123. BGRN=$'\x1b[1;32m'
  124. BBLU=$'\x1b[1;34m'
  125. BRED=$'\x1b[1;31m'
  126. BYEL=$'\x1b[1;33m'
  127. IWTH=$'\x1b[3;37m'
  128. # Strings for the pretty print.
  129. MARKER="${BBLU}[${BGRN}+${BBLU}]${CNRM}"
  130. SUCCESS="${BGRN}ok${CNRM}"
  131. FAILED="${BRED}failed${CNRM}"
  132. ####################
  133. # Helper functions #
  134. ####################
  135. # Validate the given returned value (error code), print success or failed. The
  136. # second argument is the error output in case of failure, it is printed out.
  137. # On failure, this function exits.
  138. function validate_ret
  139. {
  140. if [ "$1" -eq 0 ]; then
  141. printf "%s\\n" "$SUCCESS"
  142. else
  143. printf "%s\\n" "$FAILED"
  144. printf " %s" "$2"
  145. exit 1
  146. fi
  147. }
  148. # Switch to the given branch name.
  149. function switch_branch
  150. {
  151. local cmd="git checkout $1"
  152. printf " %s Switching branch to %s..." "$MARKER" "$1"
  153. if [ $DRY_RUN -eq 0 ]; then
  154. msg=$( eval "$cmd" 2>&1 )
  155. validate_ret $? "$msg"
  156. else
  157. printf "\\n %s\\n" "${IWTH}$cmd${CNRM}"
  158. fi
  159. }
  160. # Pull the given branch name.
  161. function merge_branch
  162. {
  163. local cmd="git merge --ff-only origin/$1"
  164. printf " %s Merging branch origin/%s..." "$MARKER" "$1"
  165. if [ $DRY_RUN -eq 0 ]; then
  166. msg=$( eval "$cmd" 2>&1 )
  167. validate_ret $? "$msg"
  168. else
  169. printf "\\n %s\\n" "${IWTH}$cmd${CNRM}"
  170. fi
  171. }
  172. # Go into the worktree repository.
  173. function goto_repo
  174. {
  175. if [ ! -d "$1" ]; then
  176. echo " $1: Not found. Stopping."
  177. exit 1
  178. fi
  179. cd "$1" || exit
  180. }
  181. # Fetch the origin. No arguments.
  182. function fetch_origin
  183. {
  184. local cmd="git fetch origin"
  185. printf " %s Fetching origin..." "$MARKER"
  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. # Fetch tor-github pull requests. No arguments.
  194. function fetch_tor_github
  195. {
  196. local cmd="git fetch tor-github"
  197. printf " %s Fetching tor-github..." "$MARKER"
  198. if [ $DRY_RUN -eq 0 ]; then
  199. msg=$( eval "$cmd" 2>&1 )
  200. validate_ret $? "$msg"
  201. else
  202. printf "\\n %s\\n" "${IWTH}$cmd${CNRM}"
  203. fi
  204. }
  205. ###############
  206. # Entry point #
  207. ###############
  208. # First, fetch tor-github.
  209. goto_repo "$ORIGIN_PATH"
  210. fetch_tor_github
  211. # Then, fetch the origin.
  212. fetch_origin
  213. # Go over all configured worktree.
  214. for ((i=0; i<COUNT; i++)); do
  215. current=${!WORKTREE[$i]:0:1}
  216. repo_path=${!WORKTREE[$i]:1:1}
  217. printf "%s Handling branch %s\\n" "$MARKER" "${BYEL}$current${CNRM}"
  218. # Go into the worktree to start merging.
  219. goto_repo "$repo_path"
  220. # Checkout the current branch
  221. switch_branch "$current"
  222. # Update the current branch by merging the origin to get the latest.
  223. merge_branch "$current"
  224. done