Add hardcode tag doc
[civicrm-scripts.git] / civicrmrebase.sh
CommitLineData
488aa689
MM
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
34my_name=michael
35
36
37# Initialization checks
38
39# Check for /bin/bash.
40if [ "$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
45fi
46
47# Check networking
48# https://unix.stackexchange.com/questions/190513/shell-scripting-proper-way-to-
49# check-for-internet-connectivity
50echo Checking network...
51if ping -q -c 1 -W 1 fsf.org >/dev/null; then
52 echo "The network is up."
53else
54 echo "The network is down."
55 echo "Check connection and restart script!"
56 exit 1
57fi
58
59# Log all stdout to logfile with date.
60logfile=/tmp/$(date +%Y%m%d-%H%M).log
61exec &> >(tee -a "$logfile")
62echo "Starting logfile as $logfile..."
63echo \
64
65
66echo "User is set to $my_name. If you are not $my_name, stop the script now"
67echo "with CTRL+c. Edit this script, and replace $my_name with your name, and"
68echo "rerun the script."
69echo \
70
71if [[ $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"
75else
76 echo "--dry-run is enabled. No external changes will be made."
77fi
78echo \
79
80# Clone civicrm-core from vcs.fsf.org
81if 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
85fi
86echo "Cloning our civicrm-core repo..."
87git clone git@vcs.fsf.org:civicrm-core.git
88cd civicrm-core
89
90# Add upstream
91echo "Adding upstream..."
92git remote add upstream https://github.com/civicrm/civicrm-core.git
93
94echo "Fetching upstream..."
95git fetch upstream
96
97# Extract tag and branch information
98echo "Latest upstream tag version:"
99tag=$(git tag --sort=-committerdate | head -n 1)
6bb033e6 100# To manually upgrade to a different version, hardcode tag by uncommenting the line below.
2f67ff7a 101# tag=5.19.4
488aa689
MM
102echo $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
108cd ..
109
110# Check for previous attempts
111if 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
115fi
116
117echo "Renaming folder civicrm-core-$tag..."
118mv civicrm-core civicrm-core-$tag
119cd 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
130echo "Latest production branch:"
131pbranchfsf=$(git branch -a --sort=-committerdate | grep 'fsf$' | head -n 1 | sed 's/ remotes\///')
132echo $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
141echo "Creating production branch variable..."
142pbranch=$(echo $pbranchfsf | sed 's/origin\///' | sed 's/-fsf//')
143echo $pbranch
144echo \
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.
149if [ "$pbranch" = "$tag" ]; then
150 echo "You are currently using the latest tag in produciton!"
151 echo "Script is exiting..."
152 exit 1
153fi
154
155# List commits between updates
156echo "Abbreviated commit lines between tag $pbranch and tag $tag..."
157git --no-pager log $pbranch..$tag --pretty=oneline --abbrev-commit
158echo \
159
160# Prepare work branch
161git checkout $tag
162
163echo "Creating temp branch..."
164git checkout -b $tag-fsf-$my_name-attempt-a
165
166# Rebase
167echo "Ready to start rebasing!"
168echo "Our rebase command:"
169echo "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
175git rebase --onto $tag-fsf-$my_name-attempt-a $pbranch $pbranchfsf
176echo \
177
178# If a merge conflict occurs, open a prompt to manually handle the conflicts.
179if [ $(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
184else
185 echo "A merge conflict did not occur! This simplifies things."
186fi
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
192echo "Creating new branch $tag-fsf-$my_name-attempt-0..."
193git checkout -b $tag-fsf-$my_name-attempt-0
194
195echo "Removing temp branch..."
196git branch -d $tag-fsf-$my_name-attempt-a
197echo \
198
199
200# List differences between upstream and the rebased changes
201echo "Differences between upstream tag $tag and our new"
202echo "branch $tag-fsf-$my_name-attempt-0..."
203git --no-pager diff $tag $tag-fsf-$my_name-attempt-0
204echo \
205
206# Push attempt branch to VCS
207if [[ $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
210else
211 echo "Skipping git push..."
212fi
213
214echo "Creating new branch $tag-fsf..."
215git 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.
220if [[ $1 != "--dry-run" ]]; then
221 echo "Pushing branch $tag-fsf to origin..."
222 git push -u origin $tag-fsf
223else
224 echo "Skipping git push..."
225fi
226
227# Creating live branch
228echo "Creating new branch live-$tag..."
229git branch live-$tag
230echo "NOTE: live-$tag is not pushed through this script."
231
232# Packaging
233echo "Leaving directory..."
234cd ..
235
236echo "Creating packaging directory civicrm-pkg-$tag..."
237mkdir -p civicrm-pkg-$tag
238cd civicrm-pkg-$tag
239
240echo "Downloading latest tarball..."
241if [ ! -f "civicrm-$tag-drupal.tar.gz" ]; then
242 wget -q https://download.civicrm.org/civicrm-$tag-drupal.tar.gz
243fi
244
245echo "Extracting tarball..."
246tar xzf civicrm-$tag-drupal.tar.gz
247
248echo "Overlaying our code on top the upstream tarball..."
249rsync -avrSPX ../civicrm-core-$tag/* civicrm/
250
251echo "Ensuring that civicrm/civicrm directory does not exist..."
252if [ -d "civicrm/civicrm" ]; then
253 echo "The directory civicrm/civicrm exists!"
254 exit 1
255fi
256
257# Add the trustcommerceIPN.php script from the live branch
258if [ -d "tc-ipn-receiver" ]; then
259 echo "Removing tc-ipn-receiver folder..."
260 rm -fr tc-ipn-receiver
261fi
262echo "Cloning tc-ipn-receiver..."
263git clone git@vcs.fsf.org:tc-ipn-receiver.git
264cd tc-ipn-receiver
265echo "Changing to live branch..."
266git checkout live
267git pull
268echo "Copying trustcommerceIPN.php to civicrm/CRM/Core/Payment/..."
269cd ..
270cp tc-ipn-receiver/trustcommerceIPN.php civicrm/CRM/Core/Payment/
271
272echo "Creating the tarball..."
273tar czf civicrm-${tag}_fsf.tar.gz civicrm
274
275# Push the package to crmserver4d
276if [[ $1 != "--dry-run" ]]; then
277 echo "Uploading the package to crmserver4d..."
278 scp civicrm-${tag}_fsf.tar.gz root@crmserver4d.fsf.org:/root/
279else
280 echo "Skipping this command, due to dry-run:"
281 echo " scp civicrm-${tag}_fsf.tar.gz root@crmserver4d.fsf.org:/root/"
282fi
283
284# Logfile maintenance
285echo "Shrinking the logfile..."
286sed -i -E 's/.{37}0:00:00 //g' $logfile # rsync transfer speed/estimates
287
288echo "Copying $logfile to the civicrm-pkg-$tag directory..."
289cp $logfile .
290echo \
291
292# Instructions to continue
293echo "Script is complete! Double check the"
294echo "civicrm-pkg-$tag/$(echo $logfile | sed 's/\/tmp\///') file"
295echo "before applying changes. Once verified, continue following the"
296echo "workflow at https://gluestick.office.fsf.org/hosts/services/civicrm/upgrading/"
297echo "by testing in the development server."