-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathupdate-trackers.sh
executable file
·198 lines (166 loc) · 5.36 KB
/
update-trackers.sh
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
#!/bin/bash
#------------------------------------------------------------------------------
#
# update-trackers.sh
#
# Updates the reference branch in the bug trackers specified on command-line
# to the latest (or specified) commit (i.e. pull from upstream), then rebases
# the branch bearing the same name as the repo on top of the target commit (by
# default the HEAD of the reference branch).
#
# The assumption here is that the tracker is a git repository with 2 local
# branches
# - reference branch, named like and tracking the upstream one
# - tracker's customizations branch, having the same name as the repository
#
# Command-line Options
# -b Reference branch (see below for default value)
# -c Target commit (default to reference branch's head)
#
# Parameters
# $1..n One or more paths to a git repo
#
#------------------------------------------------------------------------------
#------------------------------------------------------------------------------
# Default Parameters (edit variables as appropriate)
#
# The upstream reference branch to update in the target repository
UPDATE_BRANCH=master
#------------------------------------------------------------------------------
# Functions
#
function usage() {
cat <<-EOF
Syntax: $(basename "$0") [-b Branch] [-c Commit] repo [...]
Branch Reference branch (defaults to '$UPDATE_BRANCH')
Commit Target commit to rebase tracker's branch to
repo Path to one or more target bugtrackers (git repositories)
EOF
}
function process_error() {
echo -ne "\nERROR: "
if [ -n "$1" ]
then
echo "$1"
fi
if [ -n "$REPO" ]
then
echo "Fix issues in repository '$REPO' and try again"
fi
exit 1
}
#------------------------------------------------------------------------------
# Initialization
#
CONFIG_FILE=config/config_inc.php
# Parse command-line options
while getopts b:c:h OPT
do
case $OPT in
b) # Reference branch
UPDATE_BRANCH="$OPTARG"
;;
c) # Target commit
TARGET_REF="$OPTARG"
;;
h|?)
usage
exit
;;
esac
done
# If no target commit specified, use upstream branch's HEAD
test -z "$TARGET_REF" &&
TARGET_REF=$UPDATE_BRANCH
# Remaining arguments should be the list of trackers to process
shift $((OPTIND - 1))
if [ -z "$1" ]
then
usage
echo
process_error "Please specify the repository to process"
fi
#==============================================================================
# Main
#
for REPO in "$@"
do
echo -e "Processing target repository: $REPO\n"
DIR_NAME=$(dirname "$REPO")
if [[ -z "$DIR_NAME" || $DIR_NAME == "." ]]
then
DIR_NAME=$PWD
fi
REPO=$(basename "$REPO")
cd "$DIR_NAME/$REPO" 2>/dev/null ||
process_error "repository '$REPO' does not exist in '$DIR_NAME' or is not accessible"
# Detect if there are unstaged changes in the repository's current branch
if ! git diff-index --name-status --exit-code HEAD
then
echo -e "\nThere are unstaged changes"
read -r -n 1 -p "Would you like to discard them ? "
echo
# shellcheck disable=SC2086
if [ "$(echo ${REPLY} | tr "[:upper:]" "[:lower:]")" = "y" ]
then
echo "Discarding changes"
git checkout -- . || process_error
else
process_error "can't proceed with unstaged changes"
fi
fi
# First update the reference branch, pull changes from upstream
echo "- Pulling upstream changes into reference branch '$UPDATE_BRANCH'"
git checkout "$UPDATE_BRANCH" ||
process_error "Unable to checkout branch '$UPDATE_BRANCH' !"
git pull --ff-only ||
process_error "failed to fast-forward branch '$UPDATE_BRANCH'"
# Set the version suffix as 'RefBranch-CommitSHA'
# If we're on a release tag, then the suffix is blank
unset VERSION_SUFFIX
git describe "$TARGET_REF" --exact-match >/dev/null 2>&1 ||
VERSION_SUFFIX=-$UPDATE_BRANCH-$(git rev-parse --short "$TARGET_REF")
# If the repository-specific branch exists, we rebase it on the top of the
# reference branch
if git checkout "$REPO" 2>/dev/null
then
echo "- Rebasing local branch '$REPO' to '$TARGET_REF'"
git rebase "$TARGET_REF" ||
process_error "failed to rebase '$REPO' branch to '$TARGET_REF'"
else
echo "- WARNING: Unable to checkout local branch '$REPO'"
continue
fi
# Update Composer packages
echo "- Installing / Updating composer packages"
composer install --no-plugins --no-scripts
# Updating the version suffix in config file
echo "- Setting version_suffix to '$VERSION_SUFFIX' in $CONFIG_FILE"
test -f $CONFIG_FILE || process_error "missing $CONFIG_FILE"
if grep "^\s*\$g_version_suffix" $CONFIG_FILE >/dev/null
then
echo "Updating existing config file"
sed -r -i.bak "s/^(\s*\\\$g_version_suffix\s*=\s*[\"']).*([\"'])/\1$VERSION_SUFFIX\2/" $CONFIG_FILE
else
echo "Config option does not exist, appending it to end of file"
echo "\$g_version_suffix = '$VERSION_SUFFIX';" >>$CONFIG_FILE
fi
# Syntax check the modified config file, just in case
php -l $CONFIG_FILE ||
process_error "Invalid $CONFIG_FILE"
# Cleanup
echo
read -r -n 1 -p "The 'admin' directory should be deleted. Would you like to do it now ? "
echo
ADMIN_DIR=$PWD/admin
# shellcheck disable=SC2086
if [ "$(echo ${REPLY} | tr "[:upper:]" "[:lower:]")" = "y" ]
then
rm -rvf "$ADMIN_DIR"
else
echo "WARNING: Remember to delete it after completing the upgrade (rm -rf $ADMIN_DIR)"
fi
echo -e "\nRepository '$REPO' updated successfully\n"
# shellcheck disable=SC2164
cd - >/dev/null
done