doc: Clarify the release process for a first stable
[tor.git] / scripts / git / git-pull-all.sh
blobbbe2576d8e215b5a841d8253846e544ad020af12
1 #!/usr/bin/env bash
3 SCRIPT_NAME=$(basename "$0")
5 usage()
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"
32 #################
33 # Configuration #
34 #################
36 # Don't change this configuration - set the env vars in your .profile
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"}
46 ##########################
47 # Git branches to manage #
48 ##########################
50 set -e
51 eval "$(git-list-tor-branches.sh -b)"
52 set +e
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"
59 COUNT=${#WORKTREE[@]}
61 #######################
62 # Argument processing #
63 #######################
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
69 while getopts "hn" opt; do
70 case "$opt" in
71 h) usage
72 exit 0
74 n) DRY_RUN=1
75 echo " *** DRY DRUN MODE ***"
78 echo
79 usage
80 exit 1
82 esac
83 done
85 #############
86 # Constants #
87 #############
89 # Control characters
90 CNRM=$'\x1b[0;0m' # Clear color
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'
99 # Strings for the pretty print.
100 MARKER="${BBLU}[${BGRN}+${BBLU}]${CNRM}"
101 SUCCESS="${BGRN}ok${CNRM}"
102 FAILED="${BRED}failed${CNRM}"
104 ####################
105 # Helper functions #
106 ####################
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
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
122 # Switch to the given branch name.
123 function switch_branch
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}"
135 # Pull the given branch name.
136 function merge_branch
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}"
148 # Go into the worktree repository.
149 function goto_repo
151 if [ ! -d "$1" ]; then
152 echo " $1: Not found. Stopping."
153 exit 1
155 cd "$1" || exit
158 # Fetch the origin. No arguments.
159 function fetch_origin
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}"
171 # Fetch tor-gitlab pull requests. No arguments.
172 function fetch_tor_gitlab
174 local cmd="git fetch tor-gitlab"
175 printf "%s Fetching tor-gitlab..." "$MARKER"
176 if [ $DRY_RUN -eq 0 ]; then
177 msg=$( eval "$cmd" 2>&1 )
178 validate_ret $? "$msg"
179 else
180 printf "\\n %s\\n" "${IWTH}$cmd${CNRM}"
184 ###############
185 # Entry point #
186 ###############
188 # Get into our origin repository.
189 goto_repo "$ORIGIN_PATH"
191 # First, fetch tor-gitlab
192 fetch_tor_gitlab
194 # Then, fetch the origin.
195 fetch_origin
197 # Go over all configured worktree.
198 for ((i=0; i<COUNT; i++)); do
199 current=${!WORKTREE[$i]:0:1}
200 repo_path=${!WORKTREE[$i]:1:1}
202 printf "%s Handling branch %s\\n" "$MARKER" "${BYEL}$current${CNRM}"
204 # Go into the worktree to start merging.
205 goto_repo "$repo_path"
206 # Checkout the current branch
207 switch_branch "$current"
208 # Update the current branch by merging the origin to get the latest.
209 merge_branch "$current"
210 done