#!/usr/bin/env bash die() { echo "$*" >&2 exit 1 } git_rdiff() { git fetch -q GITREMOTEDIFF=$( git diff --name-status remotes/origin/master ) if [ -n "$GITREMOTEDIFF" ];then echo "$( pwd ) not synchronised with remote" 1>&2 echo "$GITREMOTEDIFF" 1>&2 fi } git_st() { GITSTATUS=$( git status --porcelain ) if [ -n "$GITSTATUS" ]; then echo "Untracked files in $( pwd )" 1>&2 echo "$GITSTATUS" 1>&2 fi } annex_copy() { # copy auto from/to every remote for remote in $(git remote) do git ls-remote $remote 2>&1 > /dev/null && git-annex get --auto --fast --quiet --from $remote git ls-remote $remote 2>&1 > /dev/null && git-annex copy --auto --fast --quiet --to $remote done } annex_control() { UNUSED=$( git-annex unused --fast | grep -v checking | grep -v 'fast mode enabled' ) if [ -n "$UNUSED" ]; then echo "Unused data in $( pwd )" 1>&2 echo "++$UNUSED++" 1>&2 fi # affiche les fichiers qui n'ont pas suffisamment de copies git-annex fsck --fast > /dev/null } CONF=${1:-$HOME/.sync.cfg} # echo Configuration: $CONF [ -r $CONF ] || die "Missing configuration: $CONF" ORIG_DIR=$PWD #echo "ORIG_DIR $ORIG_DIR" PIDFILE=${CONF}.pid #echo "PIDFILE: $PIDFILE" [ -f $PIDFILE ] && die "Pidfile exists: $PIDFILE" touch $PIDFILE declare -a LINE exec < $CONF dir_ok=1 while read -a LINE; do # echo "${LINE[@]}" if [[ "${LINE[@]}" =~ ^\[.*\]$ ]]; then dir_head=${LINE[@]} # bash > 4.2 dir=${dir_head:1: -1} dir=${dir_head:1:${#dir_head}-2} # echo "DIR $dir" cd $ORIG_DIR cd $dir dir_ok=$? pwd elif [[ "${LINE[@]}" =~ ^# ]]; then : # echo COMMENT elif [ $dir_ok -eq 0 ]; then action="${LINE[0]}" action_args="${LINE[@]:1}" #echo ACTION $action #echo ARGS $action_args case "$action" in annex.watch ) git-annex watch --quiet ;; annex.sync ) git-annex sync --fast --quiet 2> /dev/null ;; annex.copy) annex_copy ;; annex.to ) git-annex copy --auto --fast --quiet --to $action_args ;; annex.from ) git-annex copy --auto --fast --quiet --from $action_args ;; annex.control ) annex_control ;; git.pull ) git pull -q ;; git.diff ) git_rdiff ;; git.status ) git_st ;; git.push ) git commit -a --allow-empty-message -m "" && git push --porcelain ;; esac fi done cd $ORIG_DIR #echo "PIDFILE: $PIDFILE" rm -f $PIDFILE [ -f $PIDFILE ] && die "Pidfile still exists $PIDFILE"