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
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.
33 # $ cd <PATH/TO/WORKTREE> (3)
34 # $ git checkout maint-0.3.5 (1)
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-*.
78 # Controlled by the -n option. The dry run option will just output the command
79 # that would have been executed for each worktree.
83 CNRM
=$
'\x1b[0;0m' # Clear color
92 # Strings for the pretty print.
93 MARKER
="${BBLU}[${BGRN}+${BBLU}]${CNRM}"
94 SUCCESS
="${BGRN}ok${CNRM}"
95 FAILED
="${BRED}failed${CNRM}"
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"
109 printf "%s\\n" "$FAILED"
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"
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"
137 printf "\\n %s\\n" "${IWTH}$cmd${CNRM}"
141 # Go into the worktree repository.
144 if [ ! -d "$1" ]; then
145 echo " $1: Not found. Stopping."
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"
160 printf "\\n %s\\n" "${IWTH}$cmd${CNRM}"
168 while getopts "n" opt
; do
171 echo " *** DRY DRUN MODE ***"
178 # First, fetch the origin.
179 goto_repo
"$ORIGIN_PATH"
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"