-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathupdate.sh
executable file
·52 lines (47 loc) · 1.79 KB
/
update.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
#!/bin/sh
# This script can update itself while running (since it updates through git)
# All commands must happen within these curly brace blocks to ensure everything
# loads into memory before executing.
{
PROJECT_ROOT=$(CDPATH='' cd -- "$(dirname -- "$0")" && pwd)
set -eux
cd "$PROJECT_ROOT" || exit 1
current_branch="$(git branch --show-current)"
git_failed=$?
if [ "$git_failed" != 0 ]; then
echo "Bad git ownership detected. Doing nothing."
echo 'Project user'
stat -c '%U' "$PROJECT_ROOT"
echo 'Script user'
id -u -n
echo 'Current safe directories'
git --no-pager config --show-origin --get-all safe.directory
exit 1
fi
if [ "$current_branch" != 'main' ]; then
echo 'On a non-default branch. Doing nothing.'
exit
fi
OWNER=$(stat -c "%U" "$PROJECT_ROOT")
GROUP=$(stat -c "%G" "$PROJECT_ROOT")
old_version=$(git rev-parse HEAD)
old_submodule_version=$(git submodule status --recursive | sha1sum | awk '{ print $1 }')
has_stash=false
if [ "$(git status --porcelain | wc -l)" -gt 0 ]; then
has_stash=true
git stash --include-untracked
fi
GIT_SSH_COMMAND="ssh -o BatchMode=yes" git pull --force --recurse-submodules
GIT_SSH_COMMAND="ssh -o BatchMode=yes" git submodule update --init --recursive
# When this update happens through systemd (root), ownership can get wonky.
chown -R "$OWNER":"$GROUP" "$PROJECT_ROOT"
new_version=$(git rev-parse HEAD)
new_submodule_version=$(git submodule status --recursive | sha1sum | awk '{ print $1 }')
if $has_stash; then
git stash pop
fi
if [ "$new_version" != "$old_version" ] || [ "$old_submodule_version" != "$new_submodule_version" ]; then
"$PROJECT_ROOT/stow.sh"
fi
}
exit