git-pull-all.sh (4701B)
1 #!/usr/bin/env bash 2 3 SCRIPT_NAME=$(basename "$0") 4 5 usage() 6 { 7 echo "$SCRIPT_NAME [-h] [-n]" 8 echo 9 echo " arguments:" 10 echo " -h: show this help text" 11 echo " -n: dry run mode" 12 echo " (default: run commands)" 13 echo 14 echo " env vars:" 15 echo " required:" 16 echo " TOR_FULL_GIT_PATH: where the git repository directories reside." 17 echo " You must set this env var, we recommend \$HOME/git/" 18 echo " (default: fail if this env var is not set;" 19 echo " current: $GIT_PATH)" 20 echo 21 echo " optional:" 22 echo " TOR_MASTER: the name of the directory containing the tor.git clone" 23 echo " The primary tor git directory is \$GIT_PATH/\$TOR_MASTER" 24 echo " (default: tor; current: $TOR_MASTER_NAME)" 25 echo " TOR_WKT_NAME: the name of the directory containing the tor" 26 echo " worktrees. The tor worktrees are:" 27 echo " \$GIT_PATH/\$TOR_WKT_NAME/{maint-*,release-*}" 28 echo " (default: tor-wkt; current: $TOR_WKT_NAME)" 29 echo " we recommend that you set these env vars in your ~/.profile" 30 } 31 32 ################# 33 # Configuration # 34 ################# 35 36 # Don't change this configuration - set the env vars in your .profile 37 38 # Where are all those git repositories? 39 GIT_PATH=${TOR_FULL_GIT_PATH:-"FULL_PATH_TO_GIT_REPOSITORY_DIRECTORY"} 40 # The primary tor git repository directory from which all the worktree have 41 # been created. 42 TOR_MASTER_NAME=${TOR_MASTER_NAME:-"tor"} 43 # The worktrees location (directory). 44 TOR_WKT_NAME=${TOR_WKT_NAME:-"tor-wkt"} 45 46 ########################## 47 # Git branches to manage # 48 ########################## 49 50 set -e 51 eval "$(git-list-tor-branches.sh -b)" 52 set +e 53 54 # The main branch path has to be the main repository thus contains the 55 # origin that will be used to fetch the updates. All the worktrees are created 56 # from that repository. 57 ORIGIN_PATH="$GIT_PATH/$TOR_MASTER_NAME" 58 59 COUNT=${#WORKTREE[@]} 60 61 ####################### 62 # Argument processing # 63 ####################### 64 65 # Controlled by the -n option. The dry run option will just output the command 66 # that would have been executed for each worktree. 67 DRY_RUN=0 68 69 while getopts "hn" opt; do 70 case "$opt" in 71 h) usage 72 exit 0 73 ;; 74 n) DRY_RUN=1 75 echo " *** DRY DRUN MODE ***" 76 ;; 77 *) 78 echo 79 usage 80 exit 1 81 ;; 82 esac 83 done 84 85 ############# 86 # Constants # 87 ############# 88 89 # Control characters 90 CNRM=$'\x1b[0;0m' # Clear color 91 92 # Bright color 93 BGRN=$'\x1b[1;32m' 94 BBLU=$'\x1b[1;34m' 95 BRED=$'\x1b[1;31m' 96 BYEL=$'\x1b[1;33m' 97 IWTH=$'\x1b[3;37m' 98 99 # Strings for the pretty print. 100 MARKER="${BBLU}[${BGRN}+${BBLU}]${CNRM}" 101 SUCCESS="${BGRN}ok${CNRM}" 102 FAILED="${BRED}failed${CNRM}" 103 104 #################### 105 # Helper functions # 106 #################### 107 108 # Validate the given returned value (error code), print success or failed. The 109 # second argument is the error output in case of failure, it is printed out. 110 # On failure, this function exits. 111 function validate_ret 112 { 113 if [ "$1" -eq 0 ]; then 114 printf "%s\\n" "$SUCCESS" 115 else 116 printf "%s\\n" "$FAILED" 117 printf " %s" "$2" 118 exit 1 119 fi 120 } 121 122 # Switch to the given branch name. 123 function switch_branch 124 { 125 local cmd="git checkout $1" 126 printf " %s Switching branch to %s..." "$MARKER" "$1" 127 if [ $DRY_RUN -eq 0 ]; then 128 msg=$( eval "$cmd" 2>&1 ) 129 validate_ret $? "$msg" 130 else 131 printf "\\n %s\\n" "${IWTH}$cmd${CNRM}" 132 fi 133 } 134 135 # Pull the given branch name. 136 function merge_branch 137 { 138 local cmd="git merge --ff-only origin/$1" 139 printf " %s Merging branch origin/%s..." "$MARKER" "$1" 140 if [ $DRY_RUN -eq 0 ]; then 141 msg=$( eval "$cmd" 2>&1 ) 142 validate_ret $? "$msg" 143 else 144 printf "\\n %s\\n" "${IWTH}$cmd${CNRM}" 145 fi 146 } 147 148 # Go into the worktree repository. 149 function goto_repo 150 { 151 if [ ! -d "$1" ]; then 152 echo " $1: Not found. Stopping." 153 exit 1 154 fi 155 cd "$1" || exit 156 } 157 158 # Fetch the origin. No arguments. 159 function fetch_origin 160 { 161 local cmd="git fetch origin" 162 printf "%s Fetching origin..." "$MARKER" 163 if [ $DRY_RUN -eq 0 ]; then 164 msg=$( eval "$cmd" 2>&1 ) 165 validate_ret $? "$msg" 166 else 167 printf "\\n %s\\n" "${IWTH}$cmd${CNRM}" 168 fi 169 } 170 171 ############### 172 # Entry point # 173 ############### 174 175 # Get into our origin repository. 176 goto_repo "$ORIGIN_PATH" 177 178 # Fetch the origin. 179 fetch_origin 180 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 186 printf "%s Handling branch %s\\n" "$MARKER" "${BYEL}$current${CNRM}" 187 188 # Go into the worktree to start merging. 189 goto_repo "$repo_path" 190 # Checkout the current branch 191 switch_branch "$current" 192 # Update the current branch by merging the origin to get the latest. 193 merge_branch "$current" 194 done