Änderungshistorie verfeinern: Zeilenzahl, Git-Link, robusteres Escaping, HEAD-Skip

Zeilenzahl je Änderung (git log --numstat statt --name-only, im selben
git-Aufruf) plus Link auf die Gitea-Commit-Ansicht ("Änderungen in Git
anzeigen"). --no-merges verhindert, dass Merge-Commits (kein Dateilisten-Eintrag
unter --numstat) die feste Zwei-Zeilen-pro-Commit-Zuordnung beim Parsen
verschieben und alle folgenden Einträge falsch zuordnen.

aenderungssatz.html baut den Satz jetzt aus normalem, auto-escapendem
Template-Markup statt aus einem einzigen per printf zusammengesetzten und
komplett als safeHTML markierten String - nur der vorab gebaute Titel-Link
bleibt als expliziter, einzelner Vertrauensgrenzen-Punkt safeHTML-markiert.

generate-verlauf.rb überspringt die komplette Neuberechnung (ein "git show"
pro historischem Commit je Eintrag), wenn sich _content/'s HEAD-Commit seit
dem letzten Lauf nicht geändert hat (hugo/data/verlauf-cache.json) - erspart
unnötige Serverlast bei den meisten Cron-Durchläufen ohne echte Änderung.
This commit is contained in:
2026-08-26 08:34:37 +02:00
parent 98baac3d54
commit c5afcaea11
3 changed files with 99 additions and 45 deletions
+57 -14
View File
@@ -8,32 +8,67 @@
# see the installation guide on the project website. No Hugo-native
# "created" equivalent exists anyway, https://github.com/gohugoio/hugo/issues/11810).
#
# Recomputed fresh on every sync rather than maintained (see
# Recomputed fresh whenever _content/ actually changed, not maintained (see
# sync-methoden.sh, which calls this script automatically) - no manual
# date/history field, git stays the single source of truth (Manifest
# Prinzip 8).
# Prinzip 8). "Whenever actually changed", not "on every call": walking the
# full history of every entry (one "git show"-equivalent read per historical
# commit) is real, recurring cost on every deploy - skipped entirely via
# hugo/data/verlauf-cache.json when _content/'s HEAD hasn't moved since the
# last run, see skip_wenn_unveraendert below.
require_relative 'lib/frontmatter'
require 'json'
require 'fileutils'
ROOT = File.expand_path('..', __dir__)
CONTENT_REPO = File.join(ROOT, '_content')
CACHE_FILE = File.join(ROOT, 'hugo', 'data', 'verlauf-cache.json')
def content_head
`cd #{CONTENT_REPO} && git rev-parse HEAD`.strip
end
# Skips the whole run (both files untouched, left exactly as the last
# successful run produced them) when _content/'s current HEAD is the same
# commit as last time - a no-op sync/cron tick (by far the common case
# under the 10-minute cron) then costs one "git rev-parse" instead of
# walking the entire history of every method/variant again.
def skip_wenn_unveraendert
return false unless File.exist?(CACHE_FILE)
cache = JSON.parse(File.read(CACHE_FILE)) rescue nil
return false unless cache
return false unless cache['content_head'] == content_head
out_methoden = File.join(ROOT, 'hugo', 'data', 'methoden.json')
out_varianten = File.join(ROOT, 'hugo', 'data', 'varianten.json')
return false unless File.exist?(out_methoden) && File.exist?(out_varianten)
puts 'verlauf: _content/ unveraendert seit letztem Lauf, ueberspringe Neuberechnung.'
true
end
# One combined "git log" call per file instead of one per commit - %x09 is
# a literal tab byte as field separator, safe against contents containing
# regular spaces/dashes. --name-only together with --follow gives the
# historical path at each commit (needed to read that commit's content via
# "git show", exactly the technique scripts/validate.rb's
# vorherige_kennung already uses for the single most recent rename).
# Oldest commit first (git log's default is newest-first, hence "reverse").
# regular spaces/dashes. --numstat (instead of --name-only) gives both the
# historical path at each commit (needed for --follow across renames, same
# technique scripts/validate.rb's vorherige_kennung already uses for the
# single most recent rename) AND the added/deleted line count in the same
# pass, no second git call needed for that. --no-merges: merge commits
# (e.g. from a regularly merged Pull Request against the content repo, see
# the "Ein Server oder zwei"/PR workflow docs) carry no file list under
# --name-only/--numstat by default - left in, the fixed 2-lines-per-commit
# pairing below would desync at exactly that commit and misattribute every
# following entry. Excluding them is also correct on its own terms: a merge
# commit isn't itself a content edit, the actual edits are the commits it
# merges, which still appear individually here regardless.
def commits_for(relpath, content_dir)
raw = `cd #{content_dir} && git log --follow --format='%H%x09%an%x09%aI' --name-only -- #{relpath}`
raw = `cd #{content_dir} && git log --no-merges --follow --format='%H%x09%an%x09%aI' --numstat -- #{relpath}`
zeilen = raw.split("\n").reject(&:empty?)
commits = []
i = 0
while i < zeilen.length
hash, autor, datum = zeilen[i].split("\t")
pfad = zeilen[i + 1]
commits << { hash: hash, autor: autor, datum: datum, pfad: pfad }
hinzu, weg, pfad = zeilen[i + 1].split("\t")
zeilen_geaendert = [hinzu, weg].map { |n| n =~ /\A\d+\z/ ? n.to_i : 0 }.sum # "-\t-" for binary files, treated as 0
commits << { hash: hash, autor: autor, datum: datum, pfad: pfad, zeilen: zeilen_geaendert }
i += 2
end
commits.reverse
@@ -67,14 +102,18 @@ def verlauf_fuer(relpath, content_dir)
commits.each do |c|
parsed = inhalt_bei(c[:hash], c[:pfad], content_dir)
next unless parsed
unless parsed
warn "verlauf: Konnte #{relpath} bei Commit #{c[:hash]} nicht lesen, uebersprungen."
next
end
fm, body = parsed
basis = { datum: c[:datum], autor: c[:autor], hash: c[:hash], zeilen: c[:zeilen] }
if vorher_fm.nil?
eintraege << { datum: c[:datum], autor: c[:autor], typ: 'erstellt', felder: [] }
eintraege << basis.merge(typ: 'erstellt', felder: [])
else
felder = geaenderte_felder(vorher_fm, vorher_body, fm, body)
eintraege << { datum: c[:datum], autor: c[:autor], typ: 'geaendert', felder: felder } unless felder.empty?
eintraege << basis.merge(typ: 'geaendert', felder: felder) unless felder.empty?
end
vorher_fm = fm
@@ -92,7 +131,6 @@ end
def generate_bundle_verlauf(name)
dir = File.join(CONTENT_REPO, name)
out = File.join(ROOT, 'hugo', 'data', "#{name}.json")
require 'fileutils'
FileUtils.mkdir_p(File.dirname(out))
entries = {}
@@ -109,5 +147,10 @@ def generate_bundle_verlauf(name)
puts "#{name}.json aktualisiert: #{entries.length} Einträge"
end
exit if skip_wenn_unveraendert
generate_bundle_verlauf('methoden')
generate_bundle_verlauf('varianten')
FileUtils.mkdir_p(File.dirname(CACHE_FILE))
File.write(CACHE_FILE, JSON.pretty_generate({ content_head: content_head }))