-
Notifications
You must be signed in to change notification settings - Fork 0
/
ppg-update-production
executable file
·81 lines (66 loc) · 2.1 KB
/
ppg-update-production
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
#!/bin/bash
if [[ -n "${PPG_DEBUG}" ]]; then
set -x
fi
PPG_EXEC_PATH=$( dirname $(readlink -f "${BASH_SOURCE[0]}" ) )
GIT_EXEC_PATH=$(git --exec-path)
GIT_DIR=$(git rev-parse --git-dir)
if [ -z "$GIT_DIR" ]; then
echo >&2 "Not in a git checkout"
exit 1
fi
production_sha1=$(git rev-parse --revs-only refs/heads/production 2>/dev/null)
if [ -z "$production_sha1" ]; then
# no production branch, nothing to do
exit 0
fi
orig_production_sha1=$(git rev-parse --revs-only origin/production)
# default target
target_sha1=$orig_production_sha1
nowepoch=$(date --utc +%s)
for tag in $(git tag -l | grep '^ppg-sched-' | sort -r); do
# Sneaky sed: 's/-/ /3g' replaces the 3rd dash only
datestamp=$(echo "$tag"|sed 's/^ppg-sched-//;s/-/ /3g;s/\./:/g;')
tagepoch=$(date -d "$datestamp" +%s)
if [[ $tagepoch -lt $nowepoch ]]; then
targettag=$tag
fi
done
if [ -n "$targettag" ]; then
target_sha1=$(git rev-parse --revs-only ${targettag}^{commit} ) || exit 1
# perhaps origin/production is ahead of scheduled tags,
# if so, it wins...
if [ -n "$orig_production_sha1" ]; then
if $PPG_EXEC_PATH/ppg-is-ancestor $targettag $orig_production_sha1 ; then
target_sha1=$orig_production_sha1
fi
fi
fi
if [ "$production_sha1" = "$target_sha1" ]; then
# nothing to do
exit 0
fi
if ! $PPG_EXEC_PATH/ppg-is-ancestor $production_sha1 $target_sha1 ; then
echo "ERROR: production branch and $targettag have diverged!"
exit 1
fi
headname=$(git rev-parse --symbolic-full-name --revs-only HEAD)
if [ "$headname" = 'refs/heads/production' ]; then
# sanity checks when on production
if [ $(git diff --name-status HEAD | wc -l) -gt 0 ]; then
echo >&2 'ERROR: Currently on "production" branch and with dirty state.'
echo >&2 " Refusing to update with scheduled changes."
exit 1
fi
fi
echo "About to update production with scheduled changes:"
git --no-pager log --oneline -n 100 refs/heads/production..$target_sha1
# and finally
if [ "$headname" = 'refs/heads/production' ]; then
echo "Updating current production checkout"
git merge --ff-only $target_sha1
exit $?
else
git push . $target_sha1:refs/heads/production
exit $?
fi