From c5afcaea118530efad1d03087ee86f00cf0e06fb Mon Sep 17 00:00:00 2001 From: Tobias Weiske Date: Wed, 26 Aug 2026 08:34:37 +0200 Subject: [PATCH] =?UTF-8?q?=C3=84nderungshistorie=20verfeinern:=20Zeilenza?= =?UTF-8?q?hl,=20Git-Link,=20robusteres=20Escaping,=20HEAD-Skip?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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. --- .gitignore | 5 ++ hugo/layouts/partials/aenderungssatz.html | 68 ++++++++++++---------- scripts/generate-verlauf.rb | 71 ++++++++++++++++++----- 3 files changed, 99 insertions(+), 45 deletions(-) diff --git a/.gitignore b/.gitignore index 2c0bdde..7f17cee 100644 --- a/.gitignore +++ b/.gitignore @@ -17,6 +17,11 @@ hugo/.hugo_build.lock /hugo/data/methoden.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 # scripts/generate-version-info.sh /hugo/data/website.json diff --git a/hugo/layouts/partials/aenderungssatz.html b/hugo/layouts/partials/aenderungssatz.html index d497ecd..1f48fcd 100644 --- a/hugo/layouts/partials/aenderungssatz.html +++ b/hugo/layouts/partials/aenderungssatz.html @@ -1,22 +1,30 @@ {{/* Renders one change-history entry as a full German sentence, per the - pattern " von wurde(n) am durch - geändert." (or " wurde am durch angelegt." for a - creation entry). Called with a dict: + pattern " von wurde(n) in Zeilen am durch + geändert. Änderungen in Git anzeigen." (or " wurde in + Zeilen am durch angelegt. ..." for a creation + entry). Called with a dict: eintrag - one entry from hugo.Data.methoden/varianten's "verlauf" - array (datum/autor/typ/felder), optionally with an extra - "variante" key (the variant's untertitel) when merged in - from a variant into a method's aggregated history, see - aenderungshistorie.html. + array (datum/autor/hash/zeilen/typ/felder), optionally + with an extra "variante" key (the variant's untertitel) + when merged in from a variant into a method's aggregated + history, see aenderungshistorie.html. titelHTML - pre-rendered link HTML to the method/variant ("Titel"), or "" when this is rendered directly on the entity's own page (no need to name the subject there, see aenderungshistorie.html). - Returns a safe HTML string (the sentence, no wrapping element - callers - decide how to wrap it, see aenderungshistorie.html and - layouts/aenderungen/list.html). */}} + Everything below is plain auto-escaped template markup (Hugo/Go's + html/template escapes {{ .autor }} etc. on its own, context-aware even + 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 }} {{ $titelHTML := .titelHTML | default "" }} -{{ $autor := $eintrag.autor | htmlEscape }} {{ $labels := dict "kennung" "Kennung" "titel" "Titel" "kurzbeschreibung" "Kurzbeschreibung" @@ -29,27 +37,25 @@ "material" "Material" "autoren" "Autor:innen" "lizenz" "Lizenz" "quellen" "Quellen" "anhaenge" "Anhänge" "inhalt" "Inhalt" }} +{{ $felderLesbar := slice }} +{{ range $eintrag.felder }}{{ $felderLesbar = $felderLesbar | append (or (index $labels .) .) }}{{ end }} +{{ $liste := delimit $felderLesbar ", " }} +{{ $verb := "wurden" }} +{{ if eq (len $eintrag.felder) 1 }}{{ $verb = "wurde" }}{{ end }} {{ $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 }} - {{ range $eintrag.felder }}{{ $felderLesbar = $felderLesbar | append (or (index $labels .) .) }}{{ end }} - {{ $liste := delimit $felderLesbar ", " }} - {{ $verb := "wurden" }} - {{ 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 }} - {{ else }} - {{ $satz = printf "%s von %s %s am %s durch %s geändert%s." $liste $titelHTML $verb $datum $autor $suffix }} - {{ end }} +{{ $zeilenText := "" }} +{{ with $eintrag.zeilen | int }} + {{ if gt . 0 }}{{ $zeilenText = printf "in %d %s " . (cond (eq . 1) "Zeile" "Zeilen") }}{{ end }} {{ end }} -{{ $safe := $satz | safeHTML }} -{{ return $safe }} + +{{ $gitLink := "" }} +{{ 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 }} Änderungen in Git anzeigen{{ end }} diff --git a/scripts/generate-verlauf.rb b/scripts/generate-verlauf.rb index 1b02ff9..2dbd4bd 100755 --- a/scripts/generate-verlauf.rb +++ b/scripts/generate-verlauf.rb @@ -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 }))