apply: capture reports
[puppet-git.git] / ppg-update-production
blobede5be2b34b808934ca0834a4ba692f6c948afe1
1 #!/bin/bash
3 PPG_EXEC_PATH=$( dirname $(realpath "${BASH_SOURCE[0]}" ) )
4 GIT_EXEC_PATH=$(git --exec-path)
5 GIT_DIR=$(git rev-parse --git-dir)
7 if [ -z "$GIT_DIR" ]; then
8 echo >&2 "Not in a git checkout"
9 exit 1
12 production_sha1=$(git rev-parse --revs-only refs/heads/production 2>/dev/null)
13 if [ -z "$production_sha1" ]; then
14 # no production branch, nothing to do
15 exit 0
18 nowepoch=$(date --utc +%s)
19 for tag in $(git tag -l | grep '^ppg-sched-' | sort -r); do
20 # Sneaky sed: 's/-/ /3g' replaces the 3rd dash only
21 datestamp=$(echo "$tag"|sed 's/^ppg-sched-//;s/-/ /3g;s/\./:/g;')
22 tagepoch=$(date -d "$datestamp" +%s)
23 if [[ $tagepoch -lt $nowepoch ]]; then
24 targettag=$tag
26 done
28 if [ -z "$targettag" ]; then
29 # no relevant tags, nothing to do...'
30 exit
33 target_sha1=$(git rev-parse --revs-only ${targettag}^{commit} ) || exit 1
35 # perhaps origin/production is ahead of scheduled tags,
36 # if so, it wins...
37 orig_production_sha1=$(git rev-parse --revs-only refs/heads/origin/master)
38 if [ -n "$orig_production_sha1" ]; then
39 if $PPG_EXEC_PATH/ppg-is-ancestor $targettag $orig_production_sha1 ; then
40 target_sha1=$orig_production_sha1
44 if [ "$production_sha1" = "$target_sha1" ]; then
45 # nothing to do
46 exit 0
50 if $PPG_EXEC_PATH/ppg-is-ancestor $targettag $production_sha1 ; then
51 # nothing to do!
52 exit 0
55 if ! $PPG_EXEC_PATH/ppg-is-ancestor $production_sha1 $target_sha1 ; then
56 echo "ERROR: production branch and $targettag have diverged!"
57 exit 1
60 headname=$(git rev-parse --symbolic-full-name --revs-only HEAD)
62 if [ "$headname" = 'refs/heads/production' ]; then
63 # sanity checks when on production
64 if [ $(git diff --name-status HEAD | wc -l) -gt 0 ]; then
65 echo >&2 'ERROR: Currently on "production" branch and with dirty state.'
66 echo >&2 " Refusing to update with scheduled changes."
67 exit 1
71 echo "About to update production with scheduled changes:"
72 git log --oneline refs/heads/production..$target_sha1
74 # and finally
75 if [ "$headname" = 'refs/heads/production' ]; then
76 echo "Updating current production checkout"
77 git merge --ff-only $target_sha1
78 exit $?
79 else
80 git push . $target_sha1:refs/heads/production
81 exit $?