git-pull-all.sh 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  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. ##########################
  48. # Git Worktree to manage #
  49. ##########################
  50. # List of all worktrees to work on. All defined above. Ordering is important.
  51. # Always the maint-* branch first then the release-*.
  52. WORKTREE=(
  53. MAINT_029[@]
  54. RELEASE_029[@]
  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}ok${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 merge_branch
  109. {
  110. local cmd="git merge --ff-only origin/$1"
  111. printf " %s Merging branch origin/%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. # Go into the worktree repository.
  120. function goto_repo
  121. {
  122. if [ ! -d "$1" ]; then
  123. echo " $1: Not found. Stopping."
  124. exit 1
  125. fi
  126. cd "$1" || exit
  127. }
  128. # Fetch the origin. No arguments.
  129. function fetch_origin
  130. {
  131. local cmd="git fetch origin"
  132. printf " %s Fetching origin..." "$MARKER"
  133. if [ $DRY_RUN -eq 0 ]; then
  134. msg=$( eval "$cmd" 2>&1 )
  135. validate_ret $? "$msg"
  136. else
  137. printf "\\n %s\\n" "${IWTH}$cmd${CNRM}"
  138. fi
  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. # First, fetch the origin.
  153. goto_repo "$ORIGIN_PATH"
  154. fetch_origin
  155. # Go over all configured worktree.
  156. for ((i=0; i<COUNT; i++)); do
  157. current=${!WORKTREE[$i]:0:1}
  158. repo_path=${!WORKTREE[$i]:1:1}
  159. printf "%s Handling branch %s\\n" "$MARKER" "${BYEL}$current${CNRM}"
  160. # Go into the worktree to start merging.
  161. goto_repo "$repo_path"
  162. # Checkout the current branch
  163. switch_branch "$current"
  164. # Update the current branch by merging the origin to get the latest.
  165. merge_branch "$current"
  166. done