Move all git maintenance scripts to separate directory
[tor.git] / scripts / git / git-pull-all.sh
blob9c6a0a8f454978d75d7bb4417cc31441a51d3efa
1 #!/bin/bash
3 ##################################
4 # User configuration (change me) #
5 ##################################
7 # The general setup that is suggested here is:
9 # GIT_PATH = /home/<user>/git/
10 # ... where the git repository directories resides.
11 # TOR_MASTER_NAME = "tor"
12 # ... which means that tor.git was cloned in /home/<user>/git/tor
13 # TOR_WKT_NAME = "tor-wkt"
14 # ... which means that the tor worktrees are in /home/<user>/git/tor-wkt
16 # Where are all those git repositories?
17 GIT_PATH="FULL_PATH_TO_GIT_REPOSITORY_DIRECTORY"
18 # The tor master git repository directory from which all the worktree have
19 # been created.
20 TOR_MASTER_NAME="tor"
21 # The worktrees location (directory).
22 TOR_WKT_NAME="tor-wkt"
24 #########################
25 # End of configuration. #
26 #########################
28 # Configuration of the branches that needs merging. The values are in order:
29 # (1) Branch name to pull (update).
30 # (2) Full path of the git worktree.
32 # As an example:
33 # $ cd <PATH/TO/WORKTREE> (3)
34 # $ git checkout maint-0.3.5 (1)
35 # $ git pull
37 # First set of arrays are the maint-* branch and then the release-* branch.
38 # New arrays need to be in the WORKTREE= array else they aren't considered.
39 MAINT_029=( "maint-0.2.9" "$GIT_PATH/$TOR_WKT_NAME/maint-0.2.9" )
40 MAINT_034=( "maint-0.3.4" "$GIT_PATH/$TOR_WKT_NAME/maint-0.3.4" )
41 MAINT_035=( "maint-0.3.5" "$GIT_PATH/$TOR_WKT_NAME/maint-0.3.5" )
42 MAINT_040=( "maint-0.4.0" "$GIT_PATH/$TOR_WKT_NAME/maint-0.4.0" )
43 MAINT_MASTER=( "master" "$GIT_PATH/$TOR_MASTER_NAME" )
45 RELEASE_029=( "release-0.2.9" "$GIT_PATH/$TOR_WKT_NAME/release-0.2.9" )
46 RELEASE_034=( "release-0.3.4" "$GIT_PATH/$TOR_WKT_NAME/release-0.3.4" )
47 RELEASE_035=( "release-0.3.5" "$GIT_PATH/$TOR_WKT_NAME/release-0.3.5" )
48 RELEASE_040=( "release-0.4.0" "$GIT_PATH/$TOR_WKT_NAME/release-0.4.0" )
50 # The master branch path has to be the main repository thus contains the
51 # origin that will be used to fetch the updates. All the worktrees are created
52 # from that repository.
53 ORIGIN_PATH="$GIT_PATH/$TOR_MASTER_NAME"
55 ##########################
56 # Git Worktree to manage #
57 ##########################
59 # List of all worktrees to work on. All defined above. Ordering is important.
60 # Always the maint-* branch first then the release-*.
61 WORKTREE=(
62 MAINT_029[@]
63 RELEASE_029[@]
65 MAINT_034[@]
66 RELEASE_034[@]
68 MAINT_035[@]
69 RELEASE_035[@]
71 MAINT_040[@]
72 RELEASE_040[@]
74 MAINT_MASTER[@]
76 COUNT=${#WORKTREE[@]}
78 # Controlled by the -n option. The dry run option will just output the command
79 # that would have been executed for each worktree.
80 DRY_RUN=0
82 # Control characters
83 CNRM=$'\x1b[0;0m' # Clear color
85 # Bright color
86 BGRN=$'\x1b[1;32m'
87 BBLU=$'\x1b[1;34m'
88 BRED=$'\x1b[1;31m'
89 BYEL=$'\x1b[1;33m'
90 IWTH=$'\x1b[3;37m'
92 # Strings for the pretty print.
93 MARKER="${BBLU}[${BGRN}+${BBLU}]${CNRM}"
94 SUCCESS="${BGRN}ok${CNRM}"
95 FAILED="${BRED}failed${CNRM}"
97 ####################
98 # Helper functions #
99 ####################
101 # Validate the given returned value (error code), print success or failed. The
102 # second argument is the error output in case of failure, it is printed out.
103 # On failure, this function exits.
104 function validate_ret
106 if [ "$1" -eq 0 ]; then
107 printf "%s\\n" "$SUCCESS"
108 else
109 printf "%s\\n" "$FAILED"
110 printf " %s" "$2"
111 exit 1
115 # Switch to the given branch name.
116 function switch_branch
118 local cmd="git checkout $1"
119 printf " %s Switching branch to %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}"
128 # Pull the given branch name.
129 function merge_branch
131 local cmd="git merge --ff-only origin/$1"
132 printf " %s Merging branch origin/%s..." "$MARKER" "$1"
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}"
141 # Go into the worktree repository.
142 function goto_repo
144 if [ ! -d "$1" ]; then
145 echo " $1: Not found. Stopping."
146 exit 1
148 cd "$1" || exit
151 # Fetch the origin. No arguments.
152 function fetch_origin
154 local cmd="git fetch origin"
155 printf " %s Fetching origin..." "$MARKER"
156 if [ $DRY_RUN -eq 0 ]; then
157 msg=$( eval "$cmd" 2>&1 )
158 validate_ret $? "$msg"
159 else
160 printf "\\n %s\\n" "${IWTH}$cmd${CNRM}"
164 ###############
165 # Entry point #
166 ###############
168 while getopts "n" opt; do
169 case "$opt" in
170 n) DRY_RUN=1
171 echo " *** DRY DRUN MODE ***"
175 esac
176 done
178 # First, fetch the origin.
179 goto_repo "$ORIGIN_PATH"
180 fetch_origin
182 # Go over all configured worktree.
183 for ((i=0; i<COUNT; i++)); do
184 current=${!WORKTREE[$i]:0:1}
185 repo_path=${!WORKTREE[$i]:1:1}
187 printf "%s Handling branch %s\\n" "$MARKER" "${BYEL}$current${CNRM}"
189 # Go into the worktree to start merging.
190 goto_repo "$repo_path"
191 # Checkout the current branch
192 switch_branch "$current"
193 # Update the current branch by merging the origin to get the latest.
194 merge_branch "$current"
195 done