-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathgit-pull-reboot.sh
executable file
·115 lines (96 loc) · 3.41 KB
/
git-pull-reboot.sh
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
105
106
107
108
109
110
111
112
113
114
115
#!/bin/bash
#
# git-pull-reboot.sh
#
# May include the branch as an input argument: git-pull-reboot.sh master
#
# Pull the latest from dosenet-raspberrypi
# Then reboot the computer
BRANCH=$1
LOGTAG=dosenet
CONFIGFILE=/home/pi/config/config.csv
DOSENETPATH=/home/pi/dosenet-raspberrypi
cd $DOSENETPATH
# what is my station ID?
if [ -f $CONFIGFILE ]; then
ID=$(cat $CONFIGFILE | tail -n1 | sed 's/,[a-zA-Z0-9.,-]*//' | sed 's_\r__')
else
ID=unknown
fi
# git operations:
# `sudo -u pi` is required, because operations must be performed by
# user `pi`, not root
#--- Clean up the index, working directory, and/or bad history ---
# 1. Get the status
CURRENT_BRANCH=$(git rev-parse --abbrev-ref HEAD)
LABEL_TEXT="Changes not staged for commit:"
ANY_UNSTAGED=$(git status | grep -c "$LABEL_TEXT")
LABEL_TEXT="Changes to be committed:"
ANY_STAGED=$(git status | grep -c "$LABEL_TEXT")
LABEL_TEXT="Your branch is ahead of "
ANY_COMMITS=$(git status | grep -c "$LABEL_TEXT")
LABEL_TEXT=" have diverged,"
ANY_DIVERGENCE=$(git status | grep -c "$LABEL_TEXT")
# 2. start with local commits that didn't get pushed (really shouldn't happen)
if [ $ANY_COMMITS = 1 -o $ANY_DIVERGENCE = 1 ]; then
TMP=$(date | sed 's/^[A-Z][a-z][a-z]/localchanges/')
TMP=$(echo $TMP | sed 's/ [0-9][0-9]:[0-9][0-9]:[0-9][0-9] [A-Z]* [0-9]*//')
NEW_BRANCH_NAME=$(echo $TMP | sed 's/ /-/g')
# generates a string in the form 'localchanges-May-11'
LOGMSG="Found local commits! Stashing them in branch $NEW_BRANCH_NAME"
logger --stderr --id --tag $LOGTAG $LOGMSG
git branch $NEW_BRANCH_NAME
EXIT1=$?
# don't reset to $BRANCH, or else $CURRENT_BRANCH will point to $BRANCH
# and be all messed up (divergent)
git reset --hard origin/$CURRENT_BRANCH
EXIT2=$?
if [ $EXIT1 -ne 0 ]; then
logger --stderr --id --tag $LOGTAG "Failed to create stash branch!"
fi
if [ $EXIT2 -ne 0 ]; then
logger --stderr --id --tag $LOGTAG "Failed to reset branch $BRANCH !"
fi
fi
# 3. now stash any uncommited local changes in index or working dir
if [ $ANY_UNSTAGED = 1 -o $ANY_STAGED = 1 ]; then
LOGMSG="Found uncommited local changes! Putting in a git-stash"
logger --stderr --id --tag $LOGTAG $LOGMSG
git stash
if [ $? -ne 0 ]; then
logger --stderr --id --tag $LOGTAG "Failed to git-stash!"
fi
fi
# 4. do a fetch on the current branch to ensure we have a copy of
# the new branch
sudo -u pi git fetch
# it may generate an error if the current branch was deleted on the server.
# but the new branch still gets fetched.
# now, finally, there should be no reason for checkout and pull to fail.
if [ "$BRANCH" != "" ]
then
echo "Checkout branch $BRANCH"
sudo -u pi git checkout $BRANCH
if [ $? -eq 0 ]; then
logger --stderr --id --tag $LOGTAG "successfully checked out branch $BRANCH"
else
logger --stderr --id --tag $LOGTAG "failed to check out branch $BRANCH !"
fi
else
echo "No branch argument, not changing branches"
fi
sudo -u pi git pull --ff-only
if [ $? -eq 0 ]; then
logger --stderr --id --tag $LOGTAG "git pull successful"
else
logger --stderr --id --tag $LOGTAG "git pull failed !"
fi
sudo $DOSENETPATH/system-update.sh $ID
if [ $? -eq 0 ]; then
logger --stderr --id --tag $LOGTAG "successfully ran system-update.sh"
else
logger --stderr --id --tag $LOGTAG "error in system-update.sh !"
fi
logger --stderr --id --tag $LOGTAG "git-pull-reboot.sh is rebooting now"
# the shutdown must be performed by superuser
sudo shutdown -r now