aboutsummaryrefslogtreecommitdiff
path: root/scripts/git/git-pull-all.sh
blob: 5d1d58e4bf958dd5365379a904bbff43b66c0a50 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
#!/bin/bash

##################################
# User configuration (change me) #
##################################

# The general setup that is suggested here is:
#
#   GIT_PATH = /home/<user>/git/
#     ... where the git repository directories resides.
#   TOR_MASTER_NAME = "tor"
#     ... which means that tor.git was cloned in /home/<user>/git/tor
#   TOR_WKT_NAME = "tor-wkt"
#     ... which means that the tor worktrees are in /home/<user>/git/tor-wkt

# Where are all those git repositories?
GIT_PATH="FULL_PATH_TO_GIT_REPOSITORY_DIRECTORY"
# The tor master git repository directory from which all the worktree have
# been created.
TOR_MASTER_NAME="tor"
# The worktrees location (directory).
TOR_WKT_NAME="tor-wkt"

#########################
# End of configuration. #
#########################

# Configuration of the branches that needs merging. The values are in order:
#   (1) Branch name to pull (update).
#   (2) Full path of the git worktree.
#
# As an example:
#   $ cd <PATH/TO/WORKTREE> (3)
#   $ git checkout maint-0.3.5 (1)
#   $ git pull
#
# First set of arrays are the maint-* branch and then the release-* branch.
# New arrays need to be in the WORKTREE= array else they aren't considered.
MAINT_029=( "maint-0.2.9" "$GIT_PATH/$TOR_WKT_NAME/maint-0.2.9" )
MAINT_034=( "maint-0.3.4" "$GIT_PATH/$TOR_WKT_NAME/maint-0.3.4" )
MAINT_035=( "maint-0.3.5" "$GIT_PATH/$TOR_WKT_NAME/maint-0.3.5" )
MAINT_040=( "maint-0.4.0" "$GIT_PATH/$TOR_WKT_NAME/maint-0.4.0" )
MAINT_MASTER=( "master" "$GIT_PATH/$TOR_MASTER_NAME" )

RELEASE_029=( "release-0.2.9" "$GIT_PATH/$TOR_WKT_NAME/release-0.2.9" )
RELEASE_034=( "release-0.3.4" "$GIT_PATH/$TOR_WKT_NAME/release-0.3.4" )
RELEASE_035=( "release-0.3.5" "$GIT_PATH/$TOR_WKT_NAME/release-0.3.5" )
RELEASE_040=( "release-0.4.0" "$GIT_PATH/$TOR_WKT_NAME/release-0.4.0" )

# The master branch path has to be the main repository thus contains the
# origin that will be used to fetch the updates. All the worktrees are created
# from that repository.
ORIGIN_PATH="$GIT_PATH/$TOR_MASTER_NAME"

# SC2034 -- shellcheck thinks that these are unused.  We know better.
ACTUALLY_THESE_ARE_USED=<<EOF
${MAINT_029[0]}
${MAINT_034[0]}
${MAINT_035[0]}
${MAINT_040[0]}
${MAINT_MASTER[0]}
${RELEASE_029[0]}
${RELEASE_034[0]}
${RELEASE_035[0]}
${RELEASE_040[0]}
EOF

##########################
# Git Worktree to manage #
##########################

# List of all worktrees to work on. All defined above. Ordering is important.
# Always the maint-* branch first then the release-*.
WORKTREE=(
  MAINT_029[@]
  RELEASE_029[@]

  MAINT_034[@]
  RELEASE_034[@]

  MAINT_035[@]
  RELEASE_035[@]

  MAINT_040[@]
  RELEASE_040[@]

  MAINT_MASTER[@]
)
COUNT=${#WORKTREE[@]}

# Controlled by the -n option. The dry run option will just output the command
# that would have been executed for each worktree.
DRY_RUN=0

# Control characters
CNRM=$'\x1b[0;0m'   # Clear color

# Bright color
BGRN=$'\x1b[1;32m'
BBLU=$'\x1b[1;34m'
BRED=$'\x1b[1;31m'
BYEL=$'\x1b[1;33m'
IWTH=$'\x1b[3;37m'

# Strings for the pretty print.
MARKER="${BBLU}[${BGRN}+${BBLU}]${CNRM}"
SUCCESS="${BGRN}ok${CNRM}"
FAILED="${BRED}failed${CNRM}"

####################
# Helper functions #
####################

# Validate the given returned value (error code), print success or failed. The
# second argument is the error output in case of failure, it is printed out.
# On failure, this function exits.
function validate_ret
{
  if [ "$1" -eq 0 ]; then
    printf "%s\\n" "$SUCCESS"
  else
    printf "%s\\n" "$FAILED"
    printf "    %s" "$2"
    exit 1
  fi
}

# Switch to the given branch name.
function switch_branch
{
  local cmd="git checkout $1"
  printf "  %s Switching branch to %s..." "$MARKER" "$1"
  if [ $DRY_RUN -eq 0 ]; then
    msg=$( eval "$cmd" 2>&1 )
    validate_ret $? "$msg"
  else
    printf "\\n      %s\\n" "${IWTH}$cmd${CNRM}"
  fi
}

# Pull the given branch name.
function merge_branch
{
  local cmd="git merge --ff-only origin/$1"
  printf "  %s Merging branch origin/%s..." "$MARKER" "$1"
  if [ $DRY_RUN -eq 0 ]; then
    msg=$( eval "$cmd" 2>&1 )
    validate_ret $? "$msg"
  else
    printf "\\n      %s\\n" "${IWTH}$cmd${CNRM}"
  fi
}

# Go into the worktree repository.
function goto_repo
{
  if [ ! -d "$1" ]; then
    echo "  $1: Not found. Stopping."
    exit 1
  fi
  cd "$1" || exit
}

# Fetch the origin. No arguments.
function fetch_origin
{
  local cmd="git fetch origin"
  printf "  %s Fetching origin..." "$MARKER"
  if [ $DRY_RUN -eq 0 ]; then
    msg=$( eval "$cmd" 2>&1 )
    validate_ret $? "$msg"
  else
    printf "\\n      %s\\n" "${IWTH}$cmd${CNRM}"
  fi
}

# Fetch tor-github pull requests. No arguments.
function fetch_tor_github
{
  local cmd="git fetch tor-github"
  printf "  %s Fetching tor-github..." "$MARKER"
  if [ $DRY_RUN -eq 0 ]; then
    msg=$( eval "$cmd" 2>&1 )
    validate_ret $? "$msg"
  else
    printf "\\n      %s\\n" "${IWTH}$cmd${CNRM}"
  fi
}

###############
# Entry point #
###############

while getopts "n" opt; do
  case "$opt" in
    n) DRY_RUN=1
       echo "    *** DRY DRUN MODE ***"
       ;;
    *)
       ;;
  esac
done

# First, fetch tor-github.
goto_repo "$ORIGIN_PATH"
fetch_tor_github

# Then, fetch the origin.
fetch_origin

# Go over all configured worktree.
for ((i=0; i<COUNT; i++)); do
  current=${!WORKTREE[$i]:0:1}
  repo_path=${!WORKTREE[$i]:1:1}

  printf "%s Handling branch %s\\n" "$MARKER" "${BYEL}$current${CNRM}"

  # Go into the worktree to start merging.
  goto_repo "$repo_path"
  # Checkout the current branch
  switch_branch "$current"
  # Update the current branch by merging the origin to get the latest.
  merge_branch "$current"
done