#!/bin/bash # Load params as local variables for kv in $*; do [[ $kv =~ ^.*=.*$ ]] && eval $kv done [[ -z $GIT_SSH_URL ]] && echo "missing env GIT_SSH_URL" && exit 1 [[ -z $GIT_BRANCH ]] && echo "missing env GIT_BRANCH" && exit 1 [[ -z $GIT_SSH_KEY_FILE ]] && echo "missing env GIT_SSH_KEY_FILE" && exit 1 [[ -z $FLATNOTES_ROOT ]] && echo "missing env FLATNOTES_ROOT" && exit 1 [[ ! -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 protocol=$(echo "$1" | grep "://" | sed -e's,^\(.*://\).*,\1,g') url_no_protocol=$(echo "${1/$protocol/}") protocol=$(echo "$protocol" | tr '[:upper:]' '[:lower:]' | cut -d: -f1) userpass=$(echo "$url_no_protocol" | grep "@" | cut -d"/" -f1 | rev | cut -d"@" -f2- | rev) pass=$(echo "$userpass" | grep ":" | cut -d":" -f2) if [ -n "$pass" ]; then user=$(echo "$userpass" | grep ":" | cut -d":" -f1) else user="$userpass" fi hostport=$(echo "${url_no_protocol/$userpass@/}" | cut -d"/" -f1) host=$(echo "$hostport" | cut -d":" -f1) port=$(echo "$hostport" | grep ":" | cut -d":" -f2) path=$(echo "$url_no_protocol" | grep "/" | cut -d"/" -f2-) export url_parser_proto=$protocol export url_parser_user=$user export url_parser_host=$host export url_parser_port=$port } function ssh_init { mkdir -p $HOME/.ssh && chmod 700 $HOME/.ssh touch $HOME/.ssh/config chmod 600 $HOME/.ssh/config cp $GIT_SSH_KEY_FILE $HOME/.ssh/privkey chmod 400 $HOME/.ssh/privkey echo " Host $url_parser_host IdentityFile $HOME/.ssh/privkey User $url_parser_user Port $url_parser_port UserKnownHostsFile /dev/null StrictHostKeyChecking no " > $HOME/.ssh/config } function git_init { git config --global user.name "Flatnotes" git config --global user.email "flatnotes@autocommit" git config --global --add safe.directory $FLATNOTES_ROOT git config --global pull.rebase true if [[ ! -d ${FLATNOTES_ROOT}/.git ]]; then 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 git fetch && git reset --hard origin/$GIT_BRANCH else git remote set-url origin $GIT_SSH_URL git branch -m $GIT_BRANCH git branch --set-upstream-to=origin/$GIT_BRANCH $GIT_BRANCH # update autocommit_on_event "fake" "INIT" "fake" fi } function inotifywait_loop { inotifywait -q -m $FLATNOTES_ROOT -e access -e modify -e create -e delete | \ while read -r directory action file; do 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 autocommit_on_event "$directory" "$action" "$file" build_changelog > "${FLATNOTES_ROOT}/${CHANGELOG}" sleep 1 fi done } function autocommit_on_event { directory="$1" action="$2" file="$3" 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 if [[ $url_parser_proto != ssh ]]; then echo "Need a git ssh url" exit 1 fi ssh_init git_init inotifywait_loop