all repos — shwarp-tools @ 2b9c1d6ae51d70200e6eeea09b3a6869a14aa84d

Tools and scripts

tools/autocommit (view raw, download)

 1
 2
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
#!/bin/bash

if [[ $# -ne 1 ]]
then
    echo "Usage: ./autocommit <FILE_PATH>"
    exit 1
fi

if ! [ -x "$(command -v inotifywait)" ]
then
    echo "This script requires 'inotify-tools'"
    exit 1
fi

FILE=$(basename "$1")
DIRECTORY=$(echo ${1::-"$((${#FILE} + 1))"})
COMMIT_MESSAGE="auto: $FILE changed"

if [ ! -d "$DIRECTORY/.git" ] || ! git -C "$DIRECTORY" rev-parse 2>/dev/null
then
    echo "'${DIRECTORY}' must be an initialised GIT directory"
fi

while true
do
    cd "$DIRECTORY"

    inotifywait -qq -e CLOSE_WRITE "$FILE"

    echo -n ?

    # Keep duplicate updates out
    sleep 1s

    git add "$FILE"

    # Bail if nothing to commit
    git diff --cached --quiet && continue

    echo -ne \\r\\a

    git commit -qm "$COMMIT_MESSAGE"

    echo "[$(date -Is)] File changed: $DIRECTORY/$FILE"
done