diff --git a/inotify/flatnotes_gitinotify.sh b/inotify/flatnotes_gitinotify.sh index e1111a6..46c0fb0 100755 --- a/inotify/flatnotes_gitinotify.sh +++ b/inotify/flatnotes_gitinotify.sh @@ -1,5 +1,6 @@ #!/bin/bash +# Load params as local variables for kv in $*; do [[ $kv =~ ^.*=.*$ ]] && eval $kv done @@ -11,6 +12,22 @@ done [[ ! -e $FLATNOTES_ROOT ]] && echo "flatnotes directory does not exist" && exit 1 [[ ! -e $GIT_SSH_KEY_FILE ]] && echo "ssh key file not found" && exit 1 +CHANGELOG="[flatnotes] Changelog.md" + +cd $FLATNOTES_ROOT + +function git +{ + echo "[$0] exec cmd: git $@" + command git -C $FLATNOTES_ROOT "$@" +} + +function build_changelog +{ + echo "| Date | Commit | Message |" + echo "| --- | --- | --- |" + command git -C $FLATNOTES_ROOT log --date=iso --pretty=format:'| %ad | %h | %s |' $GIT_BRANCH +} function url_parser { url=$1 @@ -61,9 +78,9 @@ function git_init git config --global --add safe.directory $FLATNOTES_ROOT git config --global pull.rebase true - cd $FLATNOTES_ROOT if [[ ! -d ${FLATNOTES_ROOT}/.git ]]; then - echo ".flatnotes" > .gitignore + echo ".flatnotes" > ${FLATNOTES_ROOT}/.gitignore + echo $CHANGELOG | sed -e 's, ,*,g' -re 's,(\[|\]),\\\1,g' >> ${FLATNOTES_ROOT}/.gitignore git init git branch -m $GIT_BRANCH git remote add origin $GIT_SSH_URL @@ -72,17 +89,31 @@ function git_init git remote set-url origin $GIT_SSH_URL git branch -m $GIT_BRANCH git branch --set-upstream-to=origin/$GIT_BRANCH $GIT_BRANCH - git pull + # update + autocommit_on_event "fake" "INIT" "fake" fi } -function start_inotify +function inotifywait_loop { + last_update=0 inotifywait -q -m $FLATNOTES_ROOT -e access -e modify -e create -e delete | \ while read -r directory action file; do - if [[ "$file" =~ .*md$ || "$directory" == "${FLATNOTES_ROOT}/attachments" ]]; then - echo "[inotifywait] event catched action=$action on directory=$directory file=$file" + echo "[inotifywait_loop] event catched action=$action on directory=$directory file=$file" + if [[ $action == 'ACCESS,ISDIR' ]]; then + continue + elif [[ "$file" == "$CHANGELOG" ]]; then + echo "$CHANGELOG modified, skip" + elif [[ "$file" =~ .*md$ || "$directory" == "${FLATNOTES_ROOT}/attachments" ]]; then + now=$(date '+%s') + if [[ $action == ACCESS && $(( now - last_update )) < 120 ]]; then + # inotify can generate a lot of event + echo "Last repo update is too recent, skip" + continue + fi autocommit_on_event "$directory" "$action" "$file" + build_changelog > "${FLATNOTES_ROOT}/${CHANGELOG}" + last_update=$(date '+%s') sleep 1 fi done @@ -94,17 +125,18 @@ function autocommit_on_event action="$2" file="$3" - if [[ $action == ACCESS ]]; then - # update local repo in case some file has been updated by other method than flatnotes - local_change=$(git status --short | wc -l) - [[ $local_change > 0 ]] && git stash save -a local_changes - git -C $FLATNOTES_ROOT pull -X ours - [[ $local_change > 0 ]] && git stash pop && git stash clear - else - git -C $FLATNOTES_ROOT add . - git -C $FLATNOTES_ROOT commit -am "Autocommit action=$action on file=$file detected" - git -C $FLATNOTES_ROOT push + local_change=$(command git status --short | wc -l) + if [[ $local_change == 0 ]]; then + echo "[autocommit_on_event] nothing to commit. Update local copy" + git pull -X ours --rebase + return fi + + echo -e "[autocommit_on_event] commit local changes:\n\n$(git status)" + git add --all + git commit -am "Autocommit action=$action on file=$file detected" + git pull -X ours --rebase + git push } url_parser $GIT_SSH_URL @@ -114,4 +146,4 @@ if [[ $url_parser_proto != ssh ]]; then fi ssh_init git_init -start_inotify +inotifywait_loop