git-pull-all.sh 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. #!/usr/bin/env bash
  2. SCRIPT_NAME=$(basename "$0")
  3. 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. set -e
  44. eval "$(git-list-tor-branches.sh -b)"
  45. set +e
  46. # The master branch path has to be the main repository thus contains the
  47. # origin that will be used to fetch the updates. All the worktrees are created
  48. # from that repository.
  49. ORIGIN_PATH="$GIT_PATH/$TOR_MASTER_NAME"
  50. COUNT=${#WORKTREE[@]}
  51. #######################
  52. # Argument processing #
  53. #######################
  54. # Controlled by the -n option. The dry run option will just output the command
  55. # that would have been executed for each worktree.
  56. DRY_RUN=0
  57. while getopts "hn" opt; do
  58. case "$opt" in
  59. h) usage
  60. exit 0
  61. ;;
  62. n) DRY_RUN=1
  63. echo " *** DRY DRUN MODE ***"
  64. ;;
  65. *)
  66. echo
  67. usage
  68. exit 1
  69. ;;
  70. esac
  71. done
  72. #############
  73. # Constants #
  74. #############
  75. # Control characters
  76. CNRM=$'\x1b[0;0m' # Clear color
  77. # Bright color
  78. BGRN=$'\x1b[1;32m'
  79. BBLU=$'\x1b[1;34m'
  80. BRED=$'\x1b[1;31m'
  81. BYEL=$'\x1b[1;33m'
  82. IWTH=$'\x1b[3;37m'
  83. # Strings for the pretty print.
  84. MARKER="${BBLU}[${BGRN}+${BBLU}]${CNRM}"
  85. SUCCESS="${BGRN}ok${CNRM}"
  86. FAILED="${BRED}failed${CNRM}"
  87. ####################
  88. # Helper functions #
  89. ####################
  90. # Validate the given returned value (error code), print success or failed. The
  91. # second argument is the error output in case of failure, it is printed out.
  92. # On failure, this function exits.
  93. function validate_ret
  94. {
  95. if [ "$1" -eq 0 ]; then
  96. printf "%s\\n" "$SUCCESS"
  97. else
  98. printf "%s\\n" "$FAILED"
  99. printf " %s" "$2"
  100. exit 1
  101. fi
  102. }
  103. # Switch to the given branch name.
  104. function switch_branch
  105. {
  106. local cmd="git checkout $1"
  107. printf " %s Switching branch to %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. # Pull the given branch name.
  116. function merge_branch
  117. {
  118. local cmd="git merge --ff-only origin/$1"
  119. printf " %s Merging branch origin/%s..." "$MARKER" "$1"
  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. # Fetch the origin. No arguments.
  137. function fetch_origin
  138. {
  139. local cmd="git fetch origin"
  140. printf " %s Fetching origin..." "$MARKER"
  141. if [ $DRY_RUN -eq 0 ]; then
  142. msg=$( eval "$cmd" 2>&1 )
  143. validate_ret $? "$msg"
  144. else
  145. printf "\\n %s\\n" "${IWTH}$cmd${CNRM}"
  146. fi
  147. }
  148. # Fetch tor-github pull requests. No arguments.
  149. function fetch_tor_github
  150. {
  151. local cmd="git fetch tor-github"
  152. printf " %s Fetching tor-github..." "$MARKER"
  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. ###############
  161. # Entry point #
  162. ###############
  163. # First, fetch tor-github.
  164. goto_repo "$ORIGIN_PATH"
  165. fetch_tor_github
  166. # Then, fetch the origin.
  167. fetch_origin
  168. # Go over all configured worktree.
  169. for ((i=0; i<COUNT; i++)); do
  170. current=${!WORKTREE[$i]:0:1}
  171. repo_path=${!WORKTREE[$i]:1:1}
  172. printf "%s Handling branch %s\\n" "$MARKER" "${BYEL}$current${CNRM}"
  173. # Go into the worktree to start merging.
  174. goto_repo "$repo_path"
  175. # Checkout the current branch
  176. switch_branch "$current"
  177. # Update the current branch by merging the origin to get the latest.
  178. merge_branch "$current"
  179. done