Skip to content

Commit

Permalink
Move auto-sync/commit stuff here from pim repo
Browse files Browse the repository at this point in the history
  • Loading branch information
aspiers committed Jul 8, 2020
1 parent b088355 commit c8d0800
Show file tree
Hide file tree
Showing 12 changed files with 472 additions and 0 deletions.
11 changes: 11 additions & 0 deletions bin/auj
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#!/bin/bash

# if ! groups | grep -q systemd-journal; then
# echo >&2 "Need to be in systemd-journal group; aborting!"
# exit 1
# fi

jcfuu 'auto-*' -n 1000 | \
lnavf \
-f $0.filters \
"$@"
29 changes: 29 additions & 0 deletions bin/auj.filters
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# These are (at least mostly) not needed now we have git-annex-clean-sync

# :filter-out auto-sync-org\[\d+\]: remote: error: refusing to update checked out branch: refs/heads/master
# :filter-out auto-sync-org\[\d+\]: To ssh://aegean-wifi/home/adam/org
# :filter-out auto-sync-org\[\d+\]: ! \[remote rejected\] master -> master \(branch is currently checked out\)
# :filter-out auto-sync-org\[\d+\]: error: failed to push some refs to 'ssh://adam@aegean-wifi/home/adam/org'

# These are the kinds of errors seen from auto-sync-org when there are
# merge conflicts:
#
# push aegean
# To ssh://aegean-wifi/home/adam/org
# ! [rejected] master -> synced/master (non-fast-forward)
# error: failed to push some refs to 'ssh://adam@aegean-wifi/home/adam/org'
# hint: Updates were rejected because a pushed branch tip is behind its remote
# hint: counterpart. Check out this branch and integrate the remote changes
# hint: (e.g. 'git pull ...') before pushing again.
# hint: See the 'Note about fast-forwards' in 'git push --help' for details.
# To ssh://aegean-wifi/home/adam/org
# ! [rejected] master -> master (non-fast-forward)
# error: failed to push some refs to 'ssh://adam@aegean-wifi/home/adam/org'
# hint: Updates were rejected because the tip of your current branch is behind
# hint: its remote counterpart. Integrate the remote changes (e.g.
# hint: 'git pull ...') before pushing again.
# hint: See the 'Note about fast-forwards' in 'git push --help' for details.
# Pushing to aegean failed.
# (non-fast-forward problems can be solved by setting receive.denyNonFastforwards to false in the remote's git config)
# failed
# push adamspiers.org
14 changes: 14 additions & 0 deletions bin/auls
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#!/bin/bash

if [ -z "$1" ]; then
if name=$( mrname 2>/dev/null ); then
set -- "$name"
else
set -- org
fi
fi

for repo in "$@"; do
systemctl list-unit-files --user |
awk '/^auto-(sync|commit)-'"$repo"'\.service/ {print $1}'
done
8 changes: 8 additions & 0 deletions bin/aus
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#!/bin/bash

austa

cd ~/org
echo
export GIT_PAGER_MODE=none
ggl1 -n5
4 changes: 4 additions & 0 deletions bin/ausp
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#!/bin/bash

ausys stop

4 changes: 4 additions & 0 deletions bin/ausre
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#!/bin/bash

ausys restart

3 changes: 3 additions & 0 deletions bin/aust
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#!/bin/bash

ausys start
3 changes: 3 additions & 0 deletions bin/austa
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#!/bin/bash

ausys status
5 changes: 5 additions & 0 deletions bin/ausys
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#!/bin/bash

systemctl list-unit-files --user |
awk '/^auto-(sync|commit)-[^ ]+\.service/ {print $1}' |
xargs systemctl --user --no-pager "$@"
32 changes: 32 additions & 0 deletions bin/auto-commit-daemon
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#!/usr/bin/env zsh

debug=
if [[ "$1" == '-d' ]]; then
debug=-d
shift
fi

if [ $# != 3 ]; then
cat <<EOF >&2
Usage: $(basename $0) [-d] REPO-DIR SLEEP MIN-AGE
EOF
exit 1
fi

repo_dir="$1"
sleep="$2"
min_age="$3"

cd "$repo_dir"

for var in name email; do
if ! git config user.$var >/dev/null; then
echo >&2 "Error: user.$var not set in git config; aborting"
exit 1
fi
done

while true; do
git auto-commit $debug -m "$min_age"
sleep "$sleep"
done
80 changes: 80 additions & 0 deletions bin/auto-sync-daemon
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
#!/usr/bin/env zsh

BRANCH=master # FIXME: parameterise at some point

process_inotify_batch () {
while read dir action file; do
try_sync

# This should not be unnecessary since we are relying on inotifywait
# to only return a single event; the filtering is done by that
# process, not this loop.
break
done
}

try_sync () {
if detect_ssh_agent; then
# Do a sync regardless of whether auto-commit did anything,
# because another remote may have pushed changes to our
# synced/master branch. First give other remotes a chance
# to completely finish their push, just in case of any races.
sleep 5
git-annex-clean-sync
else
echo >&2 "WARNING: can't connect to ssh-agent; skipping annex sync"
fi
}

main () {
if ! inotifywait --help | grep -q -- '--include'; then
echo >&2 "inotifywait doesn't support --include; aborting!"
exit 1
fi

if [ $# != 1 ]; then
cat <<EOF >&2
Usage: $(basename $me) REPO-DIR
EOF
exit 1
fi

repo_dir="$1"
cd "$repo_dir"

if [[ -e HEAD ]] && [[ -e info ]] && [[ -e objects ]] && [[ -e refs ]] &&
[[ -e branches ]]
then
mode=bare
git_dir=.
elif [[ -d .git ]]; then
mode=worktree
git_dir=.git
else
echo >&2 "`pwd` isn't a git repo; aborting!"
exit 1
fi

# Do this at startup to allow easy checking at startup time that
# the agent was detected correct.
detect_ssh_agent

# This was a massive PITA to get right. It seems that if you
# specify specific files then it will look up the inodes on
# start-up and only monitor those. There's some similar weirdness
# with moves too. Suffice to say that it's necessary to use
# --include and watch the whole directory. -r is needed to catch
# the synced/ subdirectory too. For more clues see
# https://unix.stackexchange.com/questions/164794/why-doesnt-inotifywatch-detect-changes-on-added-files
while true; do
inotifywait \
-q -r \
--include "/($BRANCH|synced/($BRANCH|git-annex))\$" \
-e create -e modify -e move -e delete \
"$git_dir/refs/heads" |
process_inotify_batch
done
}

me="$0"
main "$@"
Loading

0 comments on commit c8d0800

Please sign in to comment.