[services] Make git-sync script more universal
Can now sync any folder, passed in on the command line.
This commit is contained in:
parent
c93cf0fc27
commit
a6096c25ef
3 changed files with 89 additions and 27 deletions
|
|
@ -1,17 +1,23 @@
|
|||
#!/usr/bin/env sh
|
||||
#
|
||||
# Keeps a git directory synced by automatically committing every x seconds.
|
||||
# After y number of runs without changes automatically pushes the directory to a remote.
|
||||
|
||||
# time in seconds to check for changes to commit
|
||||
TIME_TO_COMMIT=${GS_TIME_TO_COMMIT:-120}
|
||||
# amount of times to check for changes *without changes occurring* to push to origin
|
||||
UNCHANGED_RUNS_TO_PUSH=${GS_UNCHANGED_RUNS_TO_PUSH:-30}
|
||||
|
||||
NOTEDIR=${WIKIROOT:-"$HOME/documents/notes"}/uni
|
||||
[ -z "$NOTEDIR" ] && [ -z "$1" ] && {
|
||||
msg "ERROR: Please supply a directory to sync."
|
||||
exit 1
|
||||
}
|
||||
# logging verbosity level -- 0=error, 1=info, 2=debug
|
||||
VERBOSITY=${GS_LOG_VERBOSITY:-1}
|
||||
|
||||
DIR="${NOTEDIR:-$1}"
|
||||
set_target() {
|
||||
[ -z "$GS_TARGETDIR" ] && [ -z "$1" ] && {
|
||||
msg "ERROR: git-sync requires a target directory to keep in sync to be passed in." 0
|
||||
exit 1
|
||||
}
|
||||
DIR="${GS_TARGETDIR:-$1}"
|
||||
}
|
||||
|
||||
commit() {
|
||||
run_git pull --ff-only --ff
|
||||
|
|
@ -28,8 +34,8 @@ push() {
|
|||
}
|
||||
|
||||
should_commit() {
|
||||
if [ "$(run_git status --porcelain | wc -l)" -eq 0 ]; then
|
||||
msg "No changes to commit in $DIR"
|
||||
if [ "$(run_git diff-files --name-only | wc -l)" -eq 0 ]; then
|
||||
msg "No changes to commit in $DIR" 2
|
||||
false
|
||||
else
|
||||
msg "Found changes to commit in $DIR"
|
||||
|
|
@ -49,24 +55,39 @@ run_git() {
|
|||
git -C "$DIR" "$@"
|
||||
}
|
||||
|
||||
# echos its first argument
|
||||
# verbosity level optionally set through second argument
|
||||
msg() {
|
||||
lvl=${2:-1}
|
||||
[ "$lvl" -gt "$VERBOSITY" ] && return 0
|
||||
|
||||
echo "$1"
|
||||
}
|
||||
|
||||
no_change_cycle=0
|
||||
while true; do
|
||||
if should_commit; then
|
||||
commit
|
||||
no_change_cycle=0
|
||||
fi
|
||||
watch_changes() {
|
||||
no_change_cycle=0
|
||||
while true; do
|
||||
if should_commit; then
|
||||
commit
|
||||
no_change_cycle=0
|
||||
fi
|
||||
|
||||
if should_push; then
|
||||
push
|
||||
no_change_cycle=-9999
|
||||
fi
|
||||
if should_push; then
|
||||
push
|
||||
no_change_cycle=-9999
|
||||
fi
|
||||
|
||||
msg "No changes for $((no_change_cycle * TIME_TO_COMMIT)) of $((UNCHANGED_RUNS_TO_PUSH * TIME_TO_COMMIT)) seconds, going back to sleep for $((TIME_TO_COMMIT)) seconds."
|
||||
msg "No changes for $((no_change_cycle * TIME_TO_COMMIT)) of $((UNCHANGED_RUNS_TO_PUSH * TIME_TO_COMMIT)) seconds, going back to sleep for $((TIME_TO_COMMIT)) seconds." 2
|
||||
|
||||
no_change_cycle=$((no_change_cycle + 1))
|
||||
sleep "$TIME_TO_COMMIT"
|
||||
done
|
||||
no_change_cycle=$((no_change_cycle + 1))
|
||||
sleep "$TIME_TO_COMMIT"
|
||||
done
|
||||
}
|
||||
|
||||
main() {
|
||||
set_target "$1"
|
||||
|
||||
watch_changes
|
||||
}
|
||||
|
||||
main "$1"
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue