git-pull-all.sh 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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. ##########################
  44. # Git Worktree to manage #
  45. ##########################
  46. # List of all worktrees to work on. All defined above. Ordering is important.
  47. # Always the maint-* branch first then the release-*.
  48. WORKTREE=(
  49. MAINT_029[@]
  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}ok${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. # Go into the worktree repository.
  116. function goto_repo
  117. {
  118. if [ ! -d "$1" ]; then
  119. echo " $1: Not found. Stopping."
  120. exit 1
  121. fi
  122. cd "$1" || exit
  123. }
  124. ###############
  125. # Entry point #
  126. ###############
  127. while getopts "n" opt; do
  128. case "$opt" in
  129. n) DRY_RUN=1
  130. echo " *** DRY DRUN MODE ***"
  131. ;;
  132. *)
  133. ;;
  134. esac
  135. done
  136. # Go over all configured worktree.
  137. for ((i=0; i<COUNT; i++)); do
  138. current=${!WORKTREE[$i]:0:1}
  139. repo_path=${!WORKTREE[$i]:1:1}
  140. printf "%s Handling branch %s\\n" "$MARKER" "${BYEL}$current${CNRM}"
  141. # Go into the worktree to start merging.
  142. goto_repo "$repo_path"
  143. # Checkout the current branch
  144. switch_branch "$current"
  145. # Update the current branch with a pull to get the latest.
  146. pull_branch "$current"
  147. done