diff --git a/inotify/flatnotes_gitinotify.sh b/inotify/flatnotes_gitinotify.sh index e1111a6..db62fd9 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,13 @@ 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" + +git_bin=$(which git) +function git + echo "[$0] exec cmd: git $*" + $git_bin -C $FLATNOTES_ROOT $* +} function url_parser { url=$1 @@ -63,7 +71,8 @@ function git_init 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 @@ -80,9 +89,12 @@ function start_inotify { 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 + if [[ "$file" == "$CHANGELOG" ]]; then + echo "$CHANGELOG modified, skip" + elif [[ "$file" =~ .*md$ || "$directory" == "${FLATNOTES_ROOT}/attachments" ]]; then echo "[inotifywait] event catched action=$action on directory=$directory file=$file" autocommit_on_event "$directory" "$action" "$file" + build_changelog > "${FLATNOTES_ROOT}/${CHANGELOG}" sleep 1 fi done @@ -94,19 +106,28 @@ 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=$(git status --short | wc -l) + + # update local repo in case some file has been updated by other method than flatnotes + [[ $local_change > 0 ]] && git stash save -a local_changes + git pull -X ours + [[ $local_change > 0 ]] && git stash pop && git stash clear + + if [[ $action != ACCESS ]]; then + [[ $local_change == 0 ]] && echo "autocommit_on_event: nothing to commit" && return + git add . + git commit -am "Autocommit action=$action on file=$file detected" + git push fi } +function build_changelog +{ + echo "| Date | Commit | Message |" + echo "| --- | --- | --- |" + git log --date=iso --pretty=format:'| %ad | %h | %s |' $GIT_BRANCH +} + url_parser $GIT_SSH_URL if [[ $url_parser_proto != ssh ]]; then echo "Need a git ssh url"