feat(sync): Initial commit
This commit is contained in:
parent
7abfa3b815
commit
77fe5ce050
77
sync-dot-files.sh
Executable file
77
sync-dot-files.sh
Executable file
@ -0,0 +1,77 @@
|
||||
#!/bin/bash
|
||||
|
||||
declare ssh_key
|
||||
ssh_key="${1:?}"
|
||||
|
||||
declare -a accounts
|
||||
accounts=("${@:2}")
|
||||
declare me
|
||||
me="$(whoami)"
|
||||
|
||||
function cleanup () {
|
||||
popd &>'/dev/null'
|
||||
}
|
||||
trap cleanup EXIT
|
||||
|
||||
function pprint () {
|
||||
printf -- '%s ...\n' "${@}"
|
||||
}
|
||||
|
||||
declare do_force
|
||||
do_force='false'
|
||||
for account_index in "${!accounts[@]}"; do
|
||||
if [[ "${accounts[${account_index}]}" == '--force' ]] || [[ "${accounts[${account_index}]}" == '-f' ]]; then
|
||||
do_force='true'
|
||||
fi
|
||||
if [[ "${accounts[${account_index}]}" == "${me}" ]]; then unset -v accounts["${account_index}"]; continue; fi
|
||||
if ! id "${accounts[${account_index}]}" &>'/dev/null'; then unset -v accounts["${account_index}"]; continue; fi
|
||||
done
|
||||
|
||||
if [[ "${#accounts[@]}" -eq '0' ]]; then
|
||||
pprint 'Needs at least second account besides '"'${me}'"', exiting'
|
||||
exit 0
|
||||
fi
|
||||
|
||||
declare avail_keys
|
||||
avail_keys="$(ssh-add -l)"
|
||||
|
||||
if ! grep -Piq "${ssh_key}" <<<"${avail_keys}"; then
|
||||
pprint 'Key not loaded into agent, exiting'
|
||||
exit 0
|
||||
fi
|
||||
|
||||
function is_dirty () {
|
||||
git update-index --refresh &>'/dev/null'
|
||||
git diff-index --quiet HEAD -- || return 0
|
||||
return 1
|
||||
}
|
||||
|
||||
function we_are_ahead () {
|
||||
[[ "$(git rev-list origin..HEAD)" ]] && return 0
|
||||
return 1
|
||||
}
|
||||
|
||||
function we_are_behind () {
|
||||
[[ "$(git rev-list HEAD..origin)" ]] && return 0
|
||||
return 1
|
||||
}
|
||||
|
||||
pushd ~ &>'/dev/null'
|
||||
|
||||
if is_dirty; then pprint 'Dir '"'$(pwd)'"' is dirty, exiting'; git status; exit 0; fi
|
||||
if we_are_behind; then pprint 'We are behind remote, exiting'; exit 0; fi
|
||||
|
||||
if we_are_ahead || [[ "${do_force}" == 'true' ]]; then
|
||||
pprint 'Pushing from '"'${me}'"
|
||||
git push
|
||||
for account in "${accounts[@]}"; do
|
||||
echo
|
||||
pprint 'Pulling with '"'${account}'"
|
||||
if [[ "${do_force}" == 'true' ]]; then
|
||||
sudo --preserve-env=SSH_AUTH_SOCK su --shell /bin/bash --command 'cd; sudo --preserve-env=SSH_AUTH_SOCK git reset --hard' "${account}"
|
||||
fi
|
||||
sudo --preserve-env=SSH_AUTH_SOCK su --shell /bin/bash --command 'cd; sudo --preserve-env=SSH_AUTH_SOCK git pull; sudo find '"'"'.'"'"' \( -not -user '"${account}"' -or -not -group '"${account}"' \) -print0 | xargs --no-run-if-empty --null -I '"'"'{}'"'"' chown '"${account}"':'"${account}"' '"'"'{}'"'"'' "${account}"
|
||||
done
|
||||
else
|
||||
pprint 'Nothing to do'
|
||||
fi
|
Loading…
x
Reference in New Issue
Block a user