-
Notifications
You must be signed in to change notification settings - Fork 2
/
deploy.bash
99 lines (82 loc) · 2.3 KB
/
deploy.bash
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
#!/bin/bash
set -euo pipefail
ROOT=$(cd $(dirname $0) && pwd)
SCRIPT=$(basename $0)
FORCE=false
DRY_RUN=false
while true; do
case "$1" in
-f|--force)
FORCE=true
shift
;;
-n|--dry-run)
DRY_RUN=true
shift
;;
home|root)
break;;
*)
echo "Usage: $0 [-f | --force] [-n | --dry-run] root | home"
exit 1
;;
esac
done
function run () {
if $DRY_RUN; then
echo "#" "${@}"
else
"${@}"
fi
}
function inode () {
ls -i "$1" | cut -d' ' -f1
}
function hash () {
openssl md5 "$1" | tail -c32
}
function lgit () {
git --work-tree="$ROOT" --git-dir="$ROOT/.git" "$@"
}
function deploy () {
local tree="$1"
local target="$2"
while read file; do
local base=$(basename "$file")
local directory=$(dirname "$file")
if [[ -L "$target/$file" ]]; then
if ! [[ -e "$target/$file" ]]; then
echo "$file -> broken symlink, removing"
run rm "$target/$file"
fi
fi
if ! [[ -d "$target/$directory" ]]; then
echo "$directory -> creating directory"
run mkdir -p "$target/$directory"
fi
[[ -d "$tree/$file" ]] && continue
if [ -e "$target/$file" ]; then
if [[ $(inode "$tree/$file") != $(inode "$target/$file") ]]; then
echo -n "$file -> different inode "
if $FORCE || [[ $(hash "$tree/$file") == $(hash "$target/$file") ]]; then
echo "same hash, fixing this"
run rm -f "$target/$file"
run ln "$tree/$file" "$target/$file"
elif [[ -z $(lgit status --porcelain "$tree/$file") ]]; then
echo "no modification in repo, copying this"
run rm -f "$tree/$file"
run ln "$target/$file" "$tree/$file"
else
echo "not same hash, ignoring"
fi
fi
else
echo "$file -> creating"
run ln "$tree/$file" "$target/$file"
fi
done < <(cd "$tree"; lgit ls-files)
}
case "$1" in
home) deploy "$ROOT/home" "$HOME" ;;
root) deploy "$ROOT/root" / ;;
esac