This commit is contained in:
2026-03-25 15:14:49 +09:00
commit 266e0b0cf9
84 changed files with 4624 additions and 0 deletions

3
scripts/build.sh Executable file
View File

@@ -0,0 +1,3 @@
sed -i "s/buildDate: [0-9]\+/buildDate: $(date +%m%d-%H%M)/" config.yml
echo buildDate: $(date +%m%d-%H%M)
./hugo

181
scripts/git-repo-watcher Executable file
View File

@@ -0,0 +1,181 @@
#!/usr/bin/env bash
script_dir="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
default_interval_in_seconds=10
default_hooks_file="$script_dir""/git-repo-watcher-hooks"
print_usage() {
echo ""
echo "NAME"
echo " git-repo-watcher -- keeps a git repository in sync with its origin"
echo "SYNOPSIS"
echo " git-repo-watcher -d <directory> [-h <hooks-file>] [-i <interval>] [-o]"
echo "DESCRIPTION"
echo " The following options are available:"
echo " -d The path to the git repository"
echo " -i Watch interval time in seconds (defaults to 10 seconds)"
echo " -o Run once"
echo " -h Custom hooks file"
echo ""
exit 1
}
while getopts ":d:i:h:o" options; do
case "${options}" in
d)
git_repository_dir=${OPTARG}
;;
h)
hooks_file=${OPTARG}
;;
i)
interval_in_seconds=${OPTARG}
;;
o)
run_once=true
;;
*)
print_usage
;;
esac
done
shift $((OPTIND - 1))
# Validating given git directory
if [[ -z "$git_repository_dir" ]]; then
echo -e "\nERROR: Git directory (-d) not given!" >&2
print_usage
fi
if [[ ! -d "$git_repository_dir/.git" ]] || [[ ! -r "$git_repository_dir/.git" ]]; then
echo "ERROR: Git directory (-d) not found: '$git_repository_dir/.git'!" >&2
print_usage
fi
if [[ ! -w "$git_repository_dir/.git" ]]; then
echo "ERROR: Missing write permissions for the git directory (-d): '$git_repository_dir/.git'!" >&2
print_usage
fi
# Convert to absolute file path if necessary
if [[ "$git_repository_dir" != /* ]]; then
git_repository_dir="$PWD/$git_repository_dir"
fi
# Validating given hook file
[[ -z "${hooks_file}" ]] && hooks_file="$default_hooks_file"
if [[ -f "${hooks_file}" ]] && [[ -r "${hooks_file}" ]]; then
# shellcheck source=git-repo-watcher-hooks
source "${hooks_file}"
else
echo "ERROR: Hooks (-h) file not found: '$hooks_file'" >&2
print_usage
fi
# Validating given interval
if [[ -z "$interval_in_seconds" ]]; then
interval_in_seconds="$default_interval_in_seconds"
fi
# Executes user hooks
#
# $1 - Hook name
# $2-$4 - Hook arguments
hook() {
hook_name="$1"
shift
if [[ "$(type -t "$hook_name")" == "function" ]]; then
eval "$hook_name $*"
fi
}
# Pulls commit from remote git repository
#
# $1 - Git repository name
# $2 - Branch name
pull_change() {
git pull
exit_code=$?
commit_message=$(git log -1 --pretty=format:"%h | %an | %ad | %s")
if [[ $exit_code -eq 1 ]]; then
hook "pull_failed" "$1" "$2" "$commit_message"
else
hook "change_pulled" "$1" "$2" "$(printf '%q\n' "$commit_message")"
fi
}
while true; do
cd "$git_repository_dir" || exit 1
if [[ -f ".git/index.lock" ]]; then
echo "ERROR: Git repository is locked, waiting to unlock" >&2
if [[ $run_once ]]; then
break
fi
sleep $interval_in_seconds
continue
fi
git fetch
repo_name=$(basename -s .git "$(git config --get remote.origin.url)")
previous_branch="$branch"
branch=$(git branch | sed -n -e 's/^\* \(.*\)/\1/p')
if [[ -z $branch ]]; then
echo "ERROR: Unable to get branch" >&2
exit 1
fi
if [[ -n $previous_branch ]] && [[ "$previous_branch" != "$branch" ]]; then
hook "branch_changed" "$repo_name" "$branch" "$previous_branch"
fi
upstream="$(git rev-parse --abbrev-ref --symbolic-full-name "@{u}" 2>/dev/null)"
# upstream was not configured
if [[ -z "$upstream" ]]; then
hook "upstream_not_set" "$repo_name" "$branch"
if [[ $run_once ]]; then
break
fi
sleep $interval_in_seconds
continue
fi
git_local=$(git rev-parse @)
git_remote=$(git rev-parse "$upstream")
git_base=$(git merge-base @ "$upstream")
if [[ -z $started ]]; then
started=true
hook "startup" "$repo_name" "$branch"
fi
if [[ "$git_local" == "$git_remote" ]]; then
hook "no_changes" "$repo_name" "$branch"
elif [[ "$git_local" == "$git_base" ]]; then
hook "pull_change" "$repo_name" "$branch"
elif [[ "$git_remote" == "$git_base" ]]; then
hook "local_change" "$repo_name" "$branch"
else
hook "diverged" "$repo_name" "$branch"
fi
if [[ $run_once ]]; then
break
fi
sleep $interval_in_seconds
done

55
scripts/git-repo-watcher-hooks Executable file
View File

@@ -0,0 +1,55 @@
#!/usr/bin/env bash
# $1 - Git repository name
# $2 - Branch name
startup() {
echo "Watch started: $*"
}
# $1 - Git repository name
# $2 - Branch name
no_changes() {
echo "Nothing changed: $*"
}
# $1 - Git repository name
# $2 - Branch name
# $3 - Commit details
change_pulled() {
echo "Changes pulled: $*"
hugo
}
# $1 - Git repository name
# $2 - Branch name
# $3 - Commit details
pull_failed() {
echo "Pull failed --> Exiting: $*"
exit 1
}
# $1 - Git repository name
# $2 - New branch name
# $3 - Old branch name
branch_changed() {
echo "Branch changed: $*"
}
# $1 - Git repository name
# $2 - Branch name
upstream_not_set() {
echo "Upstream not set: $*"
}
# $1 - Git repository name
# $2 - Branch name
local_change() {
echo "local file changed: $*"
}
# $1 - Git repository name
# $2 - Branch name
diverged() {
echo "Diverged --> Exiting: $*"
exit 1
}

21
scripts/new.sh Executable file
View File

@@ -0,0 +1,21 @@
#!/bin/bash
TITLE="changeme"
DATE=$(date +%Y-%m-%d)
FILE="$DATE-$TITLE.md"
if [ ! -f content/posts/$FILE ]; then
echo Creating new file $FILE
cp content/posts/2000-01-01-draft.md content/posts/$FILE
sed -i "s/date = 2000-01-01T00:00:00Z/date = $(date -Isec)/g" content/posts/$FILE
fi
if [ $( command -v subl ) ]; then
subl $FILE
elif [ $( command -v gedit ) ]; then
gedit $FILE
elif [ $( command -v vim ) ]; then
vim $FILE
else
$EDITOR $FILE
fi

4
scripts/push.sh Executable file
View File

@@ -0,0 +1,4 @@
git pull
git add $(git rev-parse --show-toplevel)
git commit -m 'Fix'
git push origin

8
scripts/remote.sh Executable file
View File

@@ -0,0 +1,8 @@
HOST=$1
if [[ -z $1 ]];
then
HOST="web"
echo $HOST
fi
ssh $HOST /srv/blog/scripts/update.sh

5
scripts/update.sh Executable file
View File

@@ -0,0 +1,5 @@
#!/bin/bash
cd /srv/blog
git fetch
git reset --hard origin/main
./scripts/build.sh

1
scripts/watch.sh Executable file
View File

@@ -0,0 +1 @@
./scripts/git-repo-watcher -d .