-
Notifications
You must be signed in to change notification settings - Fork 0
/
sync-machine
executable file
·197 lines (163 loc) · 4.2 KB
/
sync-machine
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
#!/bin/bash
# SPDX-License-Identifier: GPL-3.0-only
# Copyright 2022, Mattias Bengtsson <[email protected]>
# TODO:
# - Clean up
# - Fix state reporting
#
################################################################################
declare -a REPOS=(
~/Documents/Magic/decks/
~/.config/
~/.local/bin/
~/Code/Test/
~/Code/github.com/mattiasb/MachineSetup/
~/Code/github.com/mattiasb/emacs-2.0/
)
declare -A COMMANDS=(
# Just sync
[~/Documents/Magic/decks/]=""
[~/.config/]=""
[~/.local/bin/]=""
[~/Code/Test/]=""
# Complex repositories
[~/Code/github.com/mattiasb/MachineSetup/]="./site.yml -v"
)
declare -A STATE=(
[neither]=0
[pulled]=1
[pushed]=2
[both]=3
)
declare -A RECIPE=()
################################################################################
function red {
echo " $(tput setaf 1)✗$(tput sgr0) ${*}"
}
function yellow {
echo " $(tput setaf 3)✓$(tput sgr0) ${*}"
}
function green {
echo " $(tput setaf 2)✓$(tput sgr0) ${*}"
}
function indent {
echo "${*}" | pr -to 4
}
function confirm {
read -p "${*} [Y/n] " -n 1 -r -s
echo
[[ $REPLY == "" || $REPLY =~ ^[Yy]$ ]]
}
function with-tilde {
echo "${1/#$HOME/\~}"
}
################################################################################
function run-in-repo {
local repo cmd
repo="${1}"
cmd="${2}"
pushd "${repo}" >/dev/null
eval "${cmd}"
popd
}
function repo-in-sync {
local repo head upstream
repo="${1}"
head="$(git -C "${repo}" rev-parse HEAD)"
upstream="$(git -C "${repo}" rev-parse "@{u}")"
test "${head}" = "${upstream}"
}
function sync {
local repo rel state
repo="${1}"
# Relative Repo Path
rel="${1/#$HOME/\~}"
state=0
if ! out="$(git -C "${repo}" status --short)"; then
red "${rel} — Failed to check status"
indent "${out}"
return
elif [ -n "${out}" ]; then
red "${rel} — Dirty"
return
elif ! out="$(git -C "${repo}" fetch -q 2>&1)"; then
red "${rel} — Failed to fetch"
indent "${out}"
return
fi
if ! repo-in-sync "${repo}"; then
if ! out="$(git -C "${repo}" pull --rebase 2>&1)"; then
red "${rel} — Failed to pull/rebase"
indent "${out}"
return
fi
state=$(( state + STATE[pulled] ))
repo_tilde="$(with-tilde "${repo}")"
if [ -n "${COMMANDS[${repo_tilde}]}" ]; then
RECIPE+=( [$repo]="${COMMANDS[${repo_tilde}]}" )
fi
fi
if ! repo-in-sync "${repo}"; then
if ! out="$(git -C "${repo}" push 2>&1)"; then
red "${rel} — Failed to push"
indent "${out}"
return
fi
state=$(( state + STATE[pushed] ))
fi
case "$state" in
"${STATE[neither]}") green "${rel}"
return ;;
"${STATE[pulled]}") yellow "${rel} — ↓"
return ;;
"${STATE[pushed]}") yellow "${rel} — ↑"
return ;;
"${STATE[both]}") yellow "${rel} — ↓↑"
return ;;
esac
}
function parse-args {
case "${#}" in
0) return ;;
1) case "${1}" in
-h | --help)
usage "${@}"
return 0
;;
-d | --debug)
set -x
return
;;
*) usage "${@}"
return 2
;;
esac
;;
*) usage "${@}"
return 2
;;
esac
}
function usage {
echo "$(basename "${0}") [-h | -d]"
}
function main {
local repo cmd
parse-args "${@}"
for repo in "${REPOS[@]}"; do
sync "${repo}"
done
if [ "${#RECIPE[@]}" -gt 0 ]; then
echo
fi
for repo in "${!RECIPE[@]}"; do
cmd="${RECIPE[$repo]}"
if confirm "$(with-tilde "${repo}") \$ ${cmd} ?"; then
run-in-repo "${repo}" "${cmd}"
fi
done
}
################################################################################
set -e
set -o pipefail
main "$@" ; exit $?