139 lines
4.2 KiB
Bash
Executable File
139 lines
4.2 KiB
Bash
Executable File
#!/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"
|
|
|
|
git_bin=$(which git)
|
|
function git
|
|
echo "[$0] exec cmd: git $*"
|
|
$git_bin -C $FLATNOTES_ROOT $*
|
|
}
|
|
|
|
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
|
|
|
|
cd $FLATNOTES_ROOT
|
|
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
|
|
git pull
|
|
fi
|
|
}
|
|
|
|
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" == "$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
|
|
}
|
|
|
|
function autocommit_on_event
|
|
{
|
|
directory="$1"
|
|
action="$2"
|
|
file="$3"
|
|
|
|
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"
|
|
exit 1
|
|
fi
|
|
ssh_init
|
|
git_init
|
|
start_inotify
|