Add hardcode tag
[civicrm-scripts.git] / civicrmrebase.sh
1 #!/bin/bash
2
3 # civicrmrebase.sh
4 # version 0.9.1
5
6 # Michael McMahon and Andrew
7
8 # License: AGPLv3
9
10 # Based on https://gluestick.office.fsf.org/hosts/services/civicrm/upgrading/
11
12 # This script aids the CiviCRM upgrade procedure. This script rebases our
13 # changes onto the latest tag and then packages the changes into a tarball.
14
15 # To run this script, and follow these steps:
16 #
17 # 0. Boot Trisquel open a terminal, and connect to the FSF VPN.
18 # 1. Create a build directory to save your civicrm builds.
19 # mkdir -p ~/builds/fsf/civicrm && cd ~/builds/fsf/civicrm
20 # 2. Place this script in that directory.
21 # 3. Edit this file and set your first name to the my_name variable with
22 # all lowercase letters.
23 # 3. Run this script with:
24 # bash civicrmrebase.sh
25 # 4. Follow all prompts and continue with the workflow found at
26 # https://gluestick.office.fsf.org/hosts/services/civicrm/upgrading/
27 #
28 # To skip pushing changes to other machines (vcs or crmserver4d), use the
29 # ```--dry-run``` argument like so:
30 # bash civicrmrebase.sh --dry-run
31
32
33 # Variables
34 my_name=michael
35
36
37 # Initialization checks
38
39 # Check for /bin/bash.
40 if [ "$BASH_VERSION" = '' ]; then
41 echo "You are not using bash."
42 echo "Use this syntax instead:"
43 echo "sudo bash bluearchive.sh"
44 exit 1
45 fi
46
47 # Check networking
48 # https://unix.stackexchange.com/questions/190513/shell-scripting-proper-way-to-
49 # check-for-internet-connectivity
50 echo Checking network...
51 if ping -q -c 1 -W 1 fsf.org >/dev/null; then
52 echo "The network is up."
53 else
54 echo "The network is down."
55 echo "Check connection and restart script!"
56 exit 1
57 fi
58
59 # Log all stdout to logfile with date.
60 logfile=/tmp/$(date +%Y%m%d-%H%M).log
61 exec &> >(tee -a "$logfile")
62 echo "Starting logfile as $logfile..."
63 echo \
64
65
66 echo "User is set to $my_name. If you are not $my_name, stop the script now"
67 echo "with CTRL+c. Edit this script, and replace $my_name with your name, and"
68 echo "rerun the script."
69 echo \
70
71 if [[ $1 != "--dry-run" ]]; then
72 echo "--dry-run is disabled! External changes will be made!"
73 echo "If this is not your intention, stop the script now with CTRL+c"
74 echo "and run again with this syntax: bash civicrmrebase.sh --dry-run"
75 else
76 echo "--dry-run is enabled. No external changes will be made."
77 fi
78 echo \
79
80 # Clone civicrm-core from vcs.fsf.org
81 if ls -d ./civicrm-core 1> /dev/null 2>&1; then
82 echo "civicrm-core folder found!"
83 echo "Remove this directory?"
84 ls -d ./civicrm-core | xargs -p -n1 rm -r
85 fi
86 echo "Cloning our civicrm-core repo..."
87 git clone git@vcs.fsf.org:civicrm-core.git
88 cd civicrm-core
89
90 # Add upstream
91 echo "Adding upstream..."
92 git remote add upstream https://github.com/civicrm/civicrm-core.git
93
94 echo "Fetching upstream..."
95 git fetch upstream
96
97 # Extract tag and branch information
98 echo "Latest upstream tag version:"
99 tag=$(git tag --sort=-committerdate | head -n 1)
100 # To manually upgrade to a different version, hardcode tag. Comment above and uncomment below.
101 # tag=5.19.4
102 echo $tag
103 # Explanation:
104 # git tag --sort=-committerdate | head -n 1
105 # List all tags starting with the most recent.
106 # Print 1st line
107 # Output example: 5.17.0
108 cd ..
109
110 # Check for previous attempts
111 if ls -d ./*$tag 1> /dev/null 2>&1; then
112 echo "Interactively removing folders that match $tag..."
113 echo "Remove these directories?"
114 ls -d ./*$tag | xargs -p -n1 rm -r
115 fi
116
117 echo "Renaming folder civicrm-core-$tag..."
118 mv civicrm-core civicrm-core-$tag
119 cd civicrm-core-$tag
120
121 # UNUSED
122 # echo "Latest upstream branch:"
123 # git branch -a --sort=-committerdate | head -n 1 | sed 's/ remotes\///'
124 # Explanation:
125 # git branch -a --sort=-committerdate | sed -n '2p;3q' | sed 's/ remotes\///'
126 # List all branches starting with the most recent.
127 # Print 2nd line (faster than head -n 2 | tail -n 1).
128 # Remove blankspace and remotes from the beginning.
129
130 echo "Latest production branch:"
131 pbranchfsf=$(git branch -a --sort=-committerdate | grep 'fsf$' | head -n 1 | sed 's/ remotes\///')
132 echo $pbranchfsf
133 # Explanation:
134 # git branch -a --sort=-committerdate | grep 'fsf$' | head -n 1 | sed 's/ remotes\///'
135 # List all branches starting with the most recent.
136 # Print only lines that end with `fsf`.
137 # Print first line.
138 # Remove blankspace and `remotes/` from the beginning.
139 # Output example: origin/5.16.3-fsf
140
141 echo "Creating production branch variable..."
142 pbranch=$(echo $pbranchfsf | sed 's/origin\///' | sed 's/-fsf//')
143 echo $pbranch
144 echo \
145 # Same as git branch -a --sort=-committerdate | grep 'fsf$' | head -n 1 | sed 's/ remotes\/origin\///' | sed 's/-fsf//'
146 # Output example: 5.16.3
147
148 # Check if latest tag and latest production branch are the same.
149 if [ "$pbranch" = "$tag" ]; then
150 echo "You are currently using the latest tag in produciton!"
151 echo "Script is exiting..."
152 exit 1
153 fi
154
155 # List commits between updates
156 echo "Abbreviated commit lines between tag $pbranch and tag $tag..."
157 git --no-pager log $pbranch..$tag --pretty=oneline --abbrev-commit
158 echo \
159
160 # Prepare work branch
161 git checkout $tag
162
163 echo "Creating temp branch..."
164 git checkout -b $tag-fsf-$my_name-attempt-a
165
166 # Rebase
167 echo "Ready to start rebasing!"
168 echo "Our rebase command:"
169 echo "git rebase --onto $tag-fsf-$my_name-attempt-a $pbranch $pbranchfsf"
170
171 # DEBUG - Use this line with hard values instead of the following line to force
172 # a merge conflict.
173 # git rebase --onto $tag-fsf-$my_name-attempt-a 4.6.29 origin/4.6.29-fsf
174
175 git rebase --onto $tag-fsf-$my_name-attempt-a $pbranch $pbranchfsf
176 echo \
177
178 # If a merge conflict occurs, open a prompt to manually handle the conflicts.
179 if [ $(tail -n 5 $logfile | grep -e skip -e continue -e abort | wc -l) -gt 2 ]; then
180 echo "A merge conflict occurred, resolve and follow directions."
181 echo "Run 'meld .' without quotes to visually view conflicts with a GUI."
182 echo "Run 'exit' without quotes to continue with the script."
183 bash
184 else
185 echo "A merge conflict did not occur! This simplifies things."
186 fi
187 # TODO Feature Request
188 # If running dry-run as a cronjob, notifying whether there is a merge conflict
189 # or not would be useful. This would require a change in logic.
190
191 # Applying changes to a new branch name
192 echo "Creating new branch $tag-fsf-$my_name-attempt-0..."
193 git checkout -b $tag-fsf-$my_name-attempt-0
194
195 echo "Removing temp branch..."
196 git branch -d $tag-fsf-$my_name-attempt-a
197 echo \
198
199
200 # List differences between upstream and the rebased changes
201 echo "Differences between upstream tag $tag and our new"
202 echo "branch $tag-fsf-$my_name-attempt-0..."
203 git --no-pager diff $tag $tag-fsf-$my_name-attempt-0
204 echo \
205
206 # Push attempt branch to VCS
207 if [[ $1 != "--dry-run" ]]; then
208 echo "Pushing branch $tag-fsf-$my_name-attempt-0 to origin..."
209 git push -u origin $tag-fsf-$my_name-attempt-0
210 else
211 echo "Skipping git push..."
212 fi
213
214 echo "Creating new branch $tag-fsf..."
215 git branch $tag-fsf
216
217 # Push fsf branch to VCS
218 # If multiple people are attempting, comment this section out and review with
219 # git diff between the attempt branches.
220 if [[ $1 != "--dry-run" ]]; then
221 echo "Pushing branch $tag-fsf to origin..."
222 git push -u origin $tag-fsf
223 else
224 echo "Skipping git push..."
225 fi
226
227 # Creating live branch
228 echo "Creating new branch live-$tag..."
229 git branch live-$tag
230 echo "NOTE: live-$tag is not pushed through this script."
231
232 # Packaging
233 echo "Leaving directory..."
234 cd ..
235
236 echo "Creating packaging directory civicrm-pkg-$tag..."
237 mkdir -p civicrm-pkg-$tag
238 cd civicrm-pkg-$tag
239
240 echo "Downloading latest tarball..."
241 if [ ! -f "civicrm-$tag-drupal.tar.gz" ]; then
242 wget -q https://download.civicrm.org/civicrm-$tag-drupal.tar.gz
243 fi
244
245 echo "Extracting tarball..."
246 tar xzf civicrm-$tag-drupal.tar.gz
247
248 echo "Overlaying our code on top the upstream tarball..."
249 rsync -avrSPX ../civicrm-core-$tag/* civicrm/
250
251 echo "Ensuring that civicrm/civicrm directory does not exist..."
252 if [ -d "civicrm/civicrm" ]; then
253 echo "The directory civicrm/civicrm exists!"
254 exit 1
255 fi
256
257 # Add the trustcommerceIPN.php script from the live branch
258 if [ -d "tc-ipn-receiver" ]; then
259 echo "Removing tc-ipn-receiver folder..."
260 rm -fr tc-ipn-receiver
261 fi
262 echo "Cloning tc-ipn-receiver..."
263 git clone git@vcs.fsf.org:tc-ipn-receiver.git
264 cd tc-ipn-receiver
265 echo "Changing to live branch..."
266 git checkout live
267 git pull
268 echo "Copying trustcommerceIPN.php to civicrm/CRM/Core/Payment/..."
269 cd ..
270 cp tc-ipn-receiver/trustcommerceIPN.php civicrm/CRM/Core/Payment/
271
272 echo "Creating the tarball..."
273 tar czf civicrm-${tag}_fsf.tar.gz civicrm
274
275 # Push the package to crmserver4d
276 if [[ $1 != "--dry-run" ]]; then
277 echo "Uploading the package to crmserver4d..."
278 scp civicrm-${tag}_fsf.tar.gz root@crmserver4d.fsf.org:/root/
279 else
280 echo "Skipping this command, due to dry-run:"
281 echo " scp civicrm-${tag}_fsf.tar.gz root@crmserver4d.fsf.org:/root/"
282 fi
283
284 # Logfile maintenance
285 echo "Shrinking the logfile..."
286 sed -i -E 's/.{37}0:00:00 //g' $logfile # rsync transfer speed/estimates
287
288 echo "Copying $logfile to the civicrm-pkg-$tag directory..."
289 cp $logfile .
290 echo \
291
292 # Instructions to continue
293 echo "Script is complete! Double check the"
294 echo "civicrm-pkg-$tag/$(echo $logfile | sed 's/\/tmp\///') file"
295 echo "before applying changes. Once verified, continue following the"
296 echo "workflow at https://gluestick.office.fsf.org/hosts/services/civicrm/upgrading/"
297 echo "by testing in the development server."