From 90fa6a740139dcc0ccab6491c39641caad4f0c4d Mon Sep 17 00:00:00 2001 From: USERNAME Date: Mon, 15 Jan 2024 20:46:34 +0000 Subject: [PATCH] Add changelog + log --- inotify/flatnotes_gitinotify.sh | 43 ++++++++++++++++++++++++++------- 1 file changed, 34 insertions(+), 9 deletions(-) diff --git a/inotify/flatnotes_gitinotify.sh b/inotify/flatnotes_gitinotify.sh index e1111a6..e7cf38b 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,23 @@ 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 + +git_bin=$(which git) +function git +{ + echo "[$(basename $0)] exec cmd: git $*" + $git_bin $* +} + +function build_changelog +{ + echo "| Date | Commit | Message |" + echo "| --- | --- | --- |" + $git_bin -C $FLATNOTES_ROOT log --date=iso --pretty=format:'| %ad | %h | %s |' $GIT_BRANCH +} function url_parser { url=$1 @@ -61,9 +79,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,7 +90,9 @@ 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" "ACCESS" "fake" + fi } @@ -80,9 +100,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,16 +117,18 @@ function autocommit_on_event action="$2" file="$3" + local_change=$(git status --short | wc -l) + 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 + git 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 == 0 ]] && echo "autocommit_on_event: nothing to commit" && return + git add --all + git commit -m "Autocommit action=$action on file=$file detected" + git push fi }