git hook to keep home dots in sync with repo

if dot files are kept in a "central" git repo, and propogated to repo’s on other hosts via git pull, needless to say, this quickly becomes cumbersome, and is error-prone as one tries to remember to only edit files in certain locations, and copy files from repo to home after changes.

this configuration assumes:
  1. a central repository that is the source of truth; i.e., changes are made on this host, then propogated to other hosts.

  2. the repo is a separate directory from the files' true homes. e.g.: the repo lives at $HOME/GITrepo/dots, and may contain files such as $HOME/.bashrc.

  3. originally, the files were copied manually from the repo to their proper location. (sounds crazy, but was done to ensure all worked as intended so as not to blow up any remote systems.)

  4. the central repository is hosted on a system with ssh access, and appropriate local permissions for all of the remote hosts.

post-commit hook script

the fix is a simple post-commit hook script that is placed in HOME/GITrepo/dots/.git/hooks/post-commit:

#!/bin/sh
# this is setup in a known location to eliminate password prompts
export SSH_AUTH_SOCK=$HOME/.ssh/socket
#
# 1st, sync home dir from repo
#
cd
git --work-tree=$HOME --git-dir=$HOME/GITrepo/dots/.git checkout -f
#
# then, sync remote satellites
#
if   [ "`hostname -s`" = grox ] ; then
        _remotes="idox asus"
elif [ "`hostname -s`" = idox ] ; then
        #_remotes="azox zox dox mox hox"
        _remotes="win10 mini ixox"
fi
        for host in $_remotes ; do
                echo "HOST: $host"
                # single quotes. eval on target, not here.
                ssh $host 'cd $HOME/GITrepo/dots ; git pull ; cd $HOME ; git --work-tree=$HOME --git-dir=$HOME/GITrepo/dots/.git checkout -f'
        done
remote repo setup
cd
mkdir -p GITrepo
cd !$
git clone ssh://grox.net/home/dave/GITrepo/dots/

that’s all folks!
…dave
Sun Jan 14 15:05:54 PST 2018