Ä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
+5
View File
@@ -17,6 +17,11 @@ hugo/.hugo_build.lock
/hugo/data/methoden.json /hugo/data/methoden.json
/hugo/data/varianten.json /hugo/data/varianten.json
# Marks the _content/ HEAD commit the two files above were last computed
# from, so generate-verlauf.rb can skip recomputing them when nothing
# changed - see the script's own comments.
/hugo/data/verlauf-cache.json
# Current commit of this repo itself, computed at deploy/build time - see # Current commit of this repo itself, computed at deploy/build time - see
# scripts/generate-version-info.sh # scripts/generate-version-info.sh
/hugo/data/website.json /hugo/data/website.json
+35 -29
View File
@@ -1,22 +1,30 @@
{{/* Renders one change-history entry as a full German sentence, per the {{/* Renders one change-history entry as a full German sentence, per the
pattern "<Felder> von <Titel> wurde(n) am <Datum> durch <Autor> pattern "<Felder> von <Titel> wurde(n) in <X> Zeilen am <Datum> durch
geändert." (or "<Titel> wurde am <Datum> durch <Autor> angelegt." for a <Autor> geändert. Änderungen in Git anzeigen." (or "<Titel> wurde in
creation entry). Called with a dict: <X> Zeilen am <Datum> durch <Autor> angelegt. ..." for a creation
entry). Called with a dict:
eintrag - one entry from hugo.Data.methoden/varianten's "verlauf" eintrag - one entry from hugo.Data.methoden/varianten's "verlauf"
array (datum/autor/typ/felder), optionally with an extra array (datum/autor/hash/zeilen/typ/felder), optionally
"variante" key (the variant's untertitel) when merged in with an extra "variante" key (the variant's untertitel)
from a variant into a method's aggregated history, see when merged in from a variant into a method's aggregated
aenderungshistorie.html. history, see aenderungshistorie.html.
titelHTML - pre-rendered link HTML to the method/variant ("<a titelHTML - pre-rendered link HTML to the method/variant ("<a
href=...>Titel</a>"), or "" when this is rendered href=...>Titel</a>"), or "" when this is rendered
directly on the entity's own page (no need to name the directly on the entity's own page (no need to name the
subject there, see aenderungshistorie.html). subject there, see aenderungshistorie.html).
Returns a safe HTML string (the sentence, no wrapping element - callers Everything below is plain auto-escaped template markup (Hugo/Go's
decide how to wrap it, see aenderungshistorie.html and html/template escapes {{ .autor }} etc. on its own, context-aware even
layouts/aenderungen/list.html). */}} inside the git-link href) - titelHTML is the ONE deliberately
pre-built HTML fragment, so it's also the only value that needs an
explicit "safeHTML" at its single point of use, instead of assembling
the whole sentence as a Go string and blanket-marking it safe
(previous version - meant every future field added here had to
remember to escape itself by hand, with nothing enforcing that).
Prints directly (no "return") - callers just place
{{ partial "aenderungssatz.html" ... }} where the sentence should
appear, see aenderungshistorie.html and layouts/aenderungen/list.html. */}}
{{ $eintrag := .eintrag }} {{ $eintrag := .eintrag }}
{{ $titelHTML := .titelHTML | default "" }} {{ $titelHTML := .titelHTML | default "" }}
{{ $autor := $eintrag.autor | htmlEscape }}
{{ $labels := dict {{ $labels := dict
"kennung" "Kennung" "titel" "Titel" "kurzbeschreibung" "Kurzbeschreibung" "kennung" "Kennung" "titel" "Titel" "kurzbeschreibung" "Kurzbeschreibung"
@@ -29,27 +37,25 @@
"material" "Material" "autoren" "Autor:innen" "lizenz" "Lizenz" "quellen" "Quellen" "material" "Material" "autoren" "Autor:innen" "lizenz" "Lizenz" "quellen" "Quellen"
"anhaenge" "Anhänge" "inhalt" "Inhalt" "anhaenge" "Anhänge" "inhalt" "Inhalt"
}} }}
{{ $datum := $eintrag.datum | time.AsTime | dateFormat "02.01.2006" }}
{{ $suffix := "" }}
{{ with $eintrag.variante }}{{ $suffix = printf " (Variante „%s“)" (. | htmlEscape) }}{{ end }}
{{ $satz := "" }}
{{ if eq $eintrag.typ "erstellt" }}
{{ $subjekt := $titelHTML }}
{{ if eq $subjekt "" }}{{ $subjekt = "Seite" }}{{ end }}
{{ $satz = printf "%s wurde am %s durch %s angelegt%s." $subjekt $datum $autor $suffix }}
{{ else }}
{{ $felderLesbar := slice }} {{ $felderLesbar := slice }}
{{ range $eintrag.felder }}{{ $felderLesbar = $felderLesbar | append (or (index $labels .) .) }}{{ end }} {{ range $eintrag.felder }}{{ $felderLesbar = $felderLesbar | append (or (index $labels .) .) }}{{ end }}
{{ $liste := delimit $felderLesbar ", " }} {{ $liste := delimit $felderLesbar ", " }}
{{ $verb := "wurden" }} {{ $verb := "wurden" }}
{{ if eq (len $eintrag.felder) 1 }}{{ $verb = "wurde" }}{{ end }} {{ if eq (len $eintrag.felder) 1 }}{{ $verb = "wurde" }}{{ end }}
{{ if eq $titelHTML "" }}
{{ $satz = printf "%s %s am %s durch %s geändert%s." $liste $verb $datum $autor $suffix }} {{ $datum := $eintrag.datum | time.AsTime | dateFormat "02.01.2006" }}
{{ else }}
{{ $satz = printf "%s von %s %s am %s durch %s geändert%s." $liste $titelHTML $verb $datum $autor $suffix }} {{ $zeilenText := "" }}
{{ with $eintrag.zeilen | int }}
{{ if gt . 0 }}{{ $zeilenText = printf "in %d %s " . (cond (eq . 1) "Zeile" "Zeilen") }}{{ end }}
{{ end }} {{ end }}
{{ end }}
{{ $safe := $satz | safeHTML }} {{ $gitLink := "" }}
{{ return $safe }} {{ with site.Params.methoden_repo_url }}{{ $gitLink = printf "%s/commit/%s" . $eintrag.hash }}{{ end }}
{{ if eq $eintrag.typ "erstellt" -}}
{{ if eq $titelHTML "" }}Seite{{ else }}{{ $titelHTML | safeHTML }}{{ end }} wurde {{ $zeilenText }}am {{ $datum }} durch {{ $eintrag.autor }} angelegt{{ with $eintrag.variante }} (Variante „{{ . }}“){{ end }}.
{{- else -}}
{{ $liste }}{{ if ne $titelHTML "" }} von {{ $titelHTML | safeHTML }}{{ end }} {{ $verb }} {{ $zeilenText }}am {{ $datum }} durch {{ $eintrag.autor }} geändert{{ with $eintrag.variante }} (Variante „{{ . }}“){{ end }}.
{{- end }}
{{- with $gitLink }} <a href="{{ . }}">Änderungen in Git anzeigen</a>{{ end }}
+57 -14
View File
@@ -8,32 +8,67 @@
# see the installation guide on the project website. No Hugo-native # see the installation guide on the project website. No Hugo-native
# "created" equivalent exists anyway, https://github.com/gohugoio/hugo/issues/11810). # "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 # sync-methoden.sh, which calls this script automatically) - no manual
# date/history field, git stays the single source of truth (Manifest # 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_relative 'lib/frontmatter'
require 'json' require 'json'
require 'fileutils'
ROOT = File.expand_path('..', __dir__) ROOT = File.expand_path('..', __dir__)
CONTENT_REPO = File.join(ROOT, '_content') 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 # 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 # a literal tab byte as field separator, safe against contents containing
# regular spaces/dashes. --name-only together with --follow gives the # regular spaces/dashes. --numstat (instead of --name-only) gives both the
# historical path at each commit (needed to read that commit's content via # historical path at each commit (needed for --follow across renames, same
# "git show", exactly the technique scripts/validate.rb's # technique scripts/validate.rb's vorherige_kennung already uses for the
# vorherige_kennung already uses for the single most recent rename). # single most recent rename) AND the added/deleted line count in the same
# Oldest commit first (git log's default is newest-first, hence "reverse"). # 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) 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?) zeilen = raw.split("\n").reject(&:empty?)
commits = [] commits = []
i = 0 i = 0
while i < zeilen.length while i < zeilen.length
hash, autor, datum = zeilen[i].split("\t") hash, autor, datum = zeilen[i].split("\t")
pfad = zeilen[i + 1] hinzu, weg, pfad = zeilen[i + 1].split("\t")
commits << { hash: hash, autor: autor, datum: datum, pfad: pfad } 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 i += 2
end end
commits.reverse commits.reverse
@@ -67,14 +102,18 @@ def verlauf_fuer(relpath, content_dir)
commits.each do |c| commits.each do |c|
parsed = inhalt_bei(c[:hash], c[:pfad], content_dir) 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 fm, body = parsed
basis = { datum: c[:datum], autor: c[:autor], hash: c[:hash], zeilen: c[:zeilen] }
if vorher_fm.nil? if vorher_fm.nil?
eintraege << { datum: c[:datum], autor: c[:autor], typ: 'erstellt', felder: [] } eintraege << basis.merge(typ: 'erstellt', felder: [])
else else
felder = geaenderte_felder(vorher_fm, vorher_body, fm, body) 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 end
vorher_fm = fm vorher_fm = fm
@@ -92,7 +131,6 @@ end
def generate_bundle_verlauf(name) def generate_bundle_verlauf(name)
dir = File.join(CONTENT_REPO, name) dir = File.join(CONTENT_REPO, name)
out = File.join(ROOT, 'hugo', 'data', "#{name}.json") out = File.join(ROOT, 'hugo', 'data', "#{name}.json")
require 'fileutils'
FileUtils.mkdir_p(File.dirname(out)) FileUtils.mkdir_p(File.dirname(out))
entries = {} entries = {}
@@ -109,5 +147,10 @@ def generate_bundle_verlauf(name)
puts "#{name}.json aktualisiert: #{entries.length} Einträge" puts "#{name}.json aktualisiert: #{entries.length} Einträge"
end end
exit if skip_wenn_unveraendert
generate_bundle_verlauf('methoden') generate_bundle_verlauf('methoden')
generate_bundle_verlauf('varianten') generate_bundle_verlauf('varianten')
FileUtils.mkdir_p(File.dirname(CACHE_FILE))
File.write(CACHE_FILE, JSON.pretty_generate({ content_head: content_head }))