Skip to content

Instantly share code, notes, and snippets.

@igorlg
Last active May 30, 2019 04:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save igorlg/9a250e6ba42964a1d0279f6e8f01435a to your computer and use it in GitHub Desktop.
Save igorlg/9a250e6ba42964a1d0279f6e8f01435a to your computer and use it in GitHub Desktop.
YADM Hooks

YADM Hooks

My personal YADM hooks, used to automate and/or improve some functionalities from YADM. For details on YADM hooks, check the documentation here.

post_encrypt

This hook performs two actions after running the command yadm encrypt:

  1. Calculate the SHA256 checksum of all files listed under .yadm/encrypt; backup sum file to .yadm/encrypted.sha256sum.old; store these values in .yadm/encrypted.sha256sum;

  2. Stage the list of encrypted files, the sha256sum file and the .yadm/files.gpg encrypted file set.

pre_status

Since YADM does not support a normal .gitignore file, I had to hack my way into this: by creating a file called .yadm/ignore and copying it to the .yadm/repo.git/info/exclude. Because Git does not support this file to be a Symlink, this hook checks if both files are different (using diff) and, if yes, copies .yadm/ignore on top of .yadm/repo.git/info/exclude.

Utils

About files utils.sh and utils.py, they hold helper functions for the hooks. The reason for two files is that the GLOB functionality was easier to write in Python than in Shell...

#!/usr/bin/env bash
source "$(dirname $0)/utils.sh"
# post encrypt hook #1: Calculate Checksum of Encrypted files
cd $HOME
echo "Calculating SHA256SUM of Encrypted files..."
echo " $YADM_HOME/encrypt.sha256sum backed up to $YADM_HOME/encrypt.sha256sum.old"
mv $YADM_HOME/encrypt.sha256sum $YADM_HOME/encrypt.sha256sum.old
touch $YADM_HOME/encrypt.sha256sum
for i in $(python "$UTILS_PY" "glob_files_file" "$HOME/.yadm/encrypt"); do
echo "Checksumming $HOME/$i"
sha256sum "$HOME/$i" >> $YADM_HOME/encrypt.sha256sum
done
echo "SHA256SUMs calculated and stored in $YADM_HOME/encrypt.sha256sum"
echo "Run sha256sum -c $YADM_HOME/encrypt.sha256sum to check them"
cd $OLDPWD
# post encrypt hook #2: Stage files.gpg, encrypt and encrypt.sha256sum
yadm add "$HOME/.yadm/files.gpg"
yadm add "$HOME/.yadm/encrypt"
yadm add "$HOME/.yadm/encrypt.sha256sum"
#!/usr/bin/env bash
source "$(dirname $0)/utils.sh"
# pre status hook #1: Fix diff between ignore and repo.git/info/exclude
f1="$HOME/.yadm/ignore"
f2="$HOME/.yadm/repo.git/info/exclude"
if `files_diff $f1 $f2`; then
cat $f1 > $f2
fi
from glob import glob
from os.path import isdir, join
def glob_files_str(str):
try:
if str.startswith('#'):
return
elif isdir(str):
str = str + '**/*'
for i in glob(str, recursive=True):
if isdir(i):
continue
print(i)
except Exception as e:
return
def glob_files_file(file):
with open(file, 'r') as f:
lines = filter(None, (line.rstrip() for line in f))
for l in lines:
glob_files_str(l)
if __name__ == "__main__":
from sys import argv
functions = {
'glob_files_str': glob_files_str,
'glob_files_file': glob_files_file,
}
functions[argv[1]](argv[2])
YADM_HOME="$HOME/.yadm"
UTILS_PY=`realpath "$(dirname $0)/utils.py"`
function is_file_staged() {
[[ "$1" == "" ]] && return 1
yadm -C $HOME status $1 -s 2>/dev/null \
| grep "^A " > /dev/null 2>&1
RET=$?
return $RET
}
function is_file_changed() {
[[ "$1" == "" ]] && return 1
yadm -C $HOME status $1 -s 2>/dev/null \
| grep "^M\|^AM" > /dev/null 2>&1
RET=$?
return $RET
}
function files_diff() {
if `diff $1 $2 >/dev/null 2>&1`; then
return 1
else
return 0
fi
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment