forked from sbmlteam/sbmlteam.github.io
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathupdate-from-github
executable file
·104 lines (87 loc) · 4.27 KB
/
update-from-github
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
#!/bin/bash
## ============================================================================
## File name : update-from-github
## Description : Script to update local files to match a given remote git branch
## Author(s) : Michael Hucka <[email protected]>
## Organization: California Institute of Technology
## Date created: 2019-03-19
#
# This checks a remote git repostiory for changes to a git repository, and if it
# detects that the remote is ahead of the local copy, it does a git rebase to
# synchronize the local copy with the remote. The script takes an optional
# argument: the branch to compare against. (The default is master.)
#
# To make it possible to clone the github repo, the repo must be public and
# the protocol used to create the initial git clone must be https instead of
# git (or else you will get permission denied errors when this script runs).
#
# Don't forget to do the first clone of the repo using --recursive!
#
# This is intended to be run from cron or a similar system daemon control
# system (e.g., Jenkins). Here's an example of a cron entry:
#
# */5 * * * * /home/hugo/sbml/admin/update-from-github >> /var/log/hugo/github-updates.log 2>&1
#
# If you want to update a branch, pass the branch name as an argument. For
# example, if you want to keep the branch 'develop' updated in a local repo
# instead of defaulting to 'master', you could do this:
#
# */5 * * * * /home/hugo/sbml/admin/update-from-github develop >> /var/log/hugo/github-updates.log 2>&1
#
# Assumptions for purposes of the SBML.org site repository:
# - this script is located in the local repository copy
# - permissions on this script and other things are set up properly
# -----------------------------------------------------------------------------
echo "===== ${0##*/} started at $(/bin/date '+%G-%m-%d:%H%M') ====="
# Because this is run from cron, we may need to include /usr/local/bin/.
PATH=/usr/local/bin:/opt/local/bin:/bin:/usr/bin
# Change to the directory where this file is located. This is so that it
# works from cron.
cd $(dirname "${BASH_SOURCE[0]}") > /dev/null 2>&1
# Get the path of our local repo, based on the assumption that this file is
# located somewhere in the repo itself.
root=$(git rev-parse --show-toplevel)
cd $root
# Does the remote origin have any changes we don't have on this branch?
git remote update
branch=$(git rev-parse --abbrev-ref HEAD)
changes=$(git rev-list HEAD...origin/$branch --count)
if (( $changes > 0 )); then
echo "Remote branch $branch has changes. Pulling them & updating site.toml"
# This is a sledgehammer, but past efforts at being smarter failed, and
# I'm just tired, so tired, of git.
git stash
git pull --rebase origin ${1}
retval=$?
# Update the site version data file.
versionfile=$root/site/data/site.toml
count=$(git rev-list HEAD --count)
identifier=$(git describe --always --tags --dirty="-local")
timestamp=$(git log -1 --date=format:"%Y-%m-%d %T" --format="%ai")
cat <<EOF > $versionfile
# =============================================================================
# File name : /data/site.toml
# Description : Data file for site data
#
# ╭────────────────────── Notice ── Notice ── Notice ──────────────────────╮
# │ │
# │ Do not edit this file. │
# │ │
# │ THIS FILE IS AUTOMATICALLY REGENERATED. │
# │ │
# ╰────────────────────── Notice ── Notice ── Notice ──────────────────────╯
#
# This file was automatically generated by admin/update-from-github.
#
# =============================================================================
commit_count = "$count"
identifier = "$identifier"
revision = "$count ($identifier on branch $branch)"
timestamp = "$timestamp"
EOF
else
echo "No changes"
fi
# Done.
echo "===== ${0##*/} stopped at $(/bin/date '+%G-%m-%d:%H%M') ====="
exit $retval