cd ..
+# State file format: one TAB-separated line per exported page,
+# file <TAB> <fingerprint> <TAB> <path>
+# Written only by this script; gitignored. A page is skipped when its recorded
+# fingerprint equals the one just computed.
+state_lookup() {
+ local wanted=$1
+ local kind hash path
+ [ -f .org-export-state ] || return 0
+ while IFS=$'\t' read -r kind hash path; do
+ if [ "$kind" = "file" ] && [ "$path" = "$wanted" ]; then
+ printf '%s' "$hash"
+ return 0
+ fi
+ done < .org-export-state
+ return 0
+}
+
+state_record() {
+ local fingerprint=$1
+ local org_file=$2
+ local tmp=".org-export-state.tmp.$$"
+ exec 9>>.org-export-state.lock
+ flock 9
+ {
+ if [ -f .org-export-state ]; then
+ while IFS=$'\t' read -r kind hash path; do
+ [ "$kind" = "file" ] || continue
+ [ "$path" = "$org_file" ] && continue
+ printf 'file\t%s\t%s\n' "$hash" "$path"
+ done < .org-export-state
+ fi
+ printf 'file\t%s\t%s\n' "$fingerprint" "$org_file"
+ } > "$tmp"
+ mv "$tmp" .org-export-state
+ exec 9>&-
+}
+
# Incremental org -> html export. A page is re-exported only when its .org file
# (or a first-level #+INCLUDE target) changed, or when its .html is missing.
# The cache is .org-export-state in the repo root, gitignored.
)
local recorded=""
- [ -f .org-export-state ] && \
- recorded=$(awk -F'\t' -v f="$org_file" '$1 == "file" && $3 == f {print $2}' .org-export-state)
+ if [ -f .org-export-state ]; then
+ recorded=$(state_lookup "$org_file")
+ fi
if [ -f "$html_file" ] && [ -n "$recorded" ] && [ "$recorded" = "$fingerprint" ]; then
return 2 # unchanged
return 1
fi
- # record the successful export under the file lock the state rewrite takes
- {
- flock 9
- grep -v -F "$(printf 'file\t')" .org-export-state 2>/dev/null > .org-export-state.tmp
- printf 'file\t%s\t%s\n' "$fingerprint" "$org_file" >> .org-export-state.tmp
- sort -o .org-export-state.tmp .org-export-state.tmp
- mv .org-export-state.tmp .org-export-state
- } 9>>.org-export-state.lock
+ state_record "$fingerprint" "$org_file"
echo "✓ Successfully exported $org_file"
return 0