<commit_before>#!/usr/bin/env bash

# This invokes emacs and makes it export the agenda to disk.
#
# Most of the logic here is to try to prevent running the
# `(org-store-agenda-views)' function if possible, because it can be expensive.

set -o errexit
set -o nounset
set -o pipefail
set -o xtrace

# Create a simple checksum of the git state inside a given git directory. We
# compute this by simply getting (1) current commit sha, plus any deltas found
# in (2) the work tree (git diff) or (3) the index (git diff -c cached), and
# also (4) any untracked files (git status).
get_git_repo_checksum()
{
	local git_repo_dir="${1}"
	local head_sha
	local files_shas
	pushd "${git_repo_dir}" >/dev/null
	head_sha="$(git rev-parse HEAD)"
	files_shas="$(git ls-files -z --exclude-standard --cached --deleted --modified --others | xargs --null sha1sum)"
	popd >/dev/null

	# Simplify the checksums to a single sha1sum.
	echo "${head_sha}${files_shas}" | _sha1sum
}

get_git_repos_checksum()
{
	local checksum=""
	for git_repo in "$@"; do
		echo >&2 "checksumming git repo \`${git_repo}'"
		checksum+="$(get_git_repo_checksum "${git_repo}")"
	done

	# Simplify the checksums to a single sha1sum.
	echo "${checksum}" | _sha1sum
}

exit_if_cache_hit()
{
	local cache="$HOME/syscfg/cache/export-agenda"
	mkdir -p "$HOME/syscfg/cache"
	local checksum
	local old_checksum

	# Caclculate new checksum.
	checksum="$(get_git_repos_checksum "${L_ORG_AGENDA_DIRS}")"
	echo "new checksum is ${checksum}"

	# Check if cache exists.
	if [[ -f "${cache}" ]]; then
		old_checksum="$(cat "${cache}")"
		echo "old checksum is ${old_checksum}"

		# Exit if the cached file has not changed.
		if [[ "${old_checksum}" == "${checksum}" ]]; then
			echo >&2 "cache has not changed; aborting"
			return 1
		fi
	fi

	# Overwrite old cache.
	echo "${checksum}" > "${cache}"
}

# Like sha1sum, but discard trailing filename.
_sha1sum()
{
	sha1sum ${@:-} | cut -d ' ' -f1
}

main()
{
	exit_if_cache_hit

	# Sanity check. Script will exit if this variable is not set.
	echo $L_ORG_AGENDA_DIRS

	__elisp=$(cat << EOF
	(progn
		; Load theme. This makes the HTML output colorized.
		(load-theme 'zenburn t)
		; Export agenda as HTML file.
		(org-store-agenda-views)
	)
EOF
	)

	# Strip comments.
	__elisp="$(echo -e "${__elisp}" | sed '/^\s\+\?;/d;s/;.\+//')"
<commit_msg>export-agenda.sh: invalidate cache on date change<commit_after>#!/usr/bin/env bash

# This invokes emacs and makes it export the agenda to disk.
#
# Most of the logic here is to try to prevent running the
# `(org-store-agenda-views)' function if possible, because it can be expensive.

set -o errexit
set -o nounset
set -o pipefail
set -o xtrace

# Create a simple checksum of the git state inside a given git directory. We
# compute this by simply getting (1) current commit sha, plus any deltas found
# in (2) the work tree (git diff) or (3) the index (git diff -c cached), and
# also (4) any untracked files (git status).
get_git_repo_checksum()
{
	local git_repo_dir="${1}"
	local head_sha
	local files_shas
	pushd "${git_repo_dir}" >/dev/null
	head_sha="$(git rev-parse HEAD)"
	files_shas="$(git ls-files -z --exclude-standard --cached --deleted --modified --others | xargs --null sha1sum)"
	popd >/dev/null

	# Simplify the checksums to a single sha1sum.
	echo "${head_sha}${files_shas}" | _sha1sum
}

get_git_repos_checksum()
{
	local checksum=""
	for git_repo in "$@"; do
		echo >&2 "checksumming git repo \`${git_repo}'"
		checksum+="$(get_git_repo_checksum "${git_repo}")"
	done

	# Simplify the checksums to a single sha1sum.
	echo "${checksum}" | _sha1sum
}

cache_hit()
{
	cache="$HOME/syscfg/cache/export-agenda"
	mkdir -p "$HOME/syscfg/cache"
	local checksum
	local old_checksum

	# Caclculate new checksum.
	checksum="$(get_git_repos_checksum "${L_ORG_AGENDA_DIRS}")"
	# Append date into checksum. This way, even if the agenda content does not
	# change, we will get a cache miss if a day passes with no change (forcing
	# the generation of a new agenda.html centered on today's date).
	checksum+=",$(date +%Y-%m-%d)"
	echo "new checksum is ${checksum}"

	# Check if cache exists.
	if [[ -f "${cache}" ]]; then
		old_checksum="$(cat "${cache}")"
		echo "old checksum is ${old_checksum}"

		# Cache hit?
		if [[ "${old_checksum}" == "${checksum}" ]]; then
			echo >&2 "cache hit"
			return 0
		fi
	fi

	# Overwrite stale cache.
	echo "${checksum}" > "${cache}"
	return 1
}

# Like sha1sum, but discard trailing filename.
_sha1sum()
{
	sha1sum ${@:-} | cut -d ' ' -f1
}

main()
{
	if cache_hit; then
		echo >&2 "aborting due to cache hit"
		exit
	fi

	# Sanity check. Script will exit if this variable is not set.
	echo $L_ORG_AGENDA_DIRS

	__elisp=$(cat << EOF
	(progn
		; Load theme. This makes the HTML output colorized.
		(load-theme 'zenburn t)
		; Export agenda as HTML file.
		(org-store-agenda-views)
	)
EOF
	)

	# Strip comments.
	__elisp="$(echo -e "${__elisp}" | sed '/^\s\+\?;/d;s/;.\+//')"

{'fraction_non_alphanumeric': 0.14178953746717834, 'fraction_numerical': 0.007271258331650172, 'mean_word_length': 3.8454011741682974, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 14, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2607900', 'n_tokens_mistral': 1887, 'n_tokens_neox': 1795, 'n_words': 600}