Zeilenzahl im Änderungsverlauf: Reformatierungs-Rauschen normalisieren

Decap CMS schreibt beim ersten Speichern einer Datei deren komplettes
Frontmatter in seinem eigenen Stil neu (Listen-Einrückung, Anführungszeichen,
Zeilenbreite, Feldreihenfolge) - unabhängig davon, wie klein die eigentliche
inhaltliche Änderung war. Der bisherige "git --numstat"-basierte Zeilenzähler
zählte diese einmalige Umformatierung mit, z.B. 45 Zeilen für das Hinzufügen
einer einzigen Kategorie zu einer bereits bestehenden Liste.

generate-verlauf.rb normalisiert jetzt beide verglichenen Versionen vor der
Zeilenzählung: YAML.dump entfernt Einrückungs-/Anführungszeichen-/
Zeilenbreiten-Unterschiede, Frontmatter.geordnet (neu aus frontmatter.rb
extrahiert, bisher nur in write() genutzt) entfernt Feldreihenfolge-
Unterschiede. Die Zeilenzahl kommt dadurch nicht mehr "kostenlos" aus
--numstat, sondern aus einem einfachen LCS-Zeilenvergleich der beiden
normalisierten Texte (reine In-Memory-Berechnung, kein zusätzlicher
Subprozess - der bereits vorhandene "git show" pro Commit bleibt die einzige
teure Operation). Body-Text wird unverändert roh verglichen, da er nicht
YAML-serialisiert wird.

Am obigen Beispiel: 45 -> 1 Zeile für die reine Kategorie-Ergänzung.
This commit is contained in:
2026-08-26 09:34:08 +02:00
parent 4542106ea1
commit 67b4c571e8
2 changed files with 71 additions and 26 deletions
+57 -22
View File
@@ -19,6 +19,7 @@
require_relative 'lib/frontmatter'
require 'json'
require 'fileutils'
require 'yaml'
ROOT = File.expand_path('..', __dir__)
CONTENT_REPO = File.join(ROOT, '_content')
@@ -47,28 +48,26 @@ 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. --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.
# regular spaces/dashes. --name-only gives 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). --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 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 --no-merges --follow --format='%H%x09%an%x09%aI' --numstat -- #{relpath}`
raw = `cd #{content_dir} && git log --no-merges --follow --format='%H%x09%an%x09%aI' --name-only -- #{relpath}`
zeilen = raw.split("\n").reject(&:empty?)
commits = []
i = 0
while i < zeilen.length
hash, autor, datum = zeilen[i].split("\t")
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 }
pfad = zeilen[i + 1]
commits << { hash: hash, autor: autor, datum: datum, pfad: pfad }
i += 2
end
commits.reverse
@@ -92,7 +91,42 @@ def geaenderte_felder(vorher_fm, vorher_body, jetzt_fm, jetzt_body)
felder
end
def verlauf_fuer(relpath, content_dir)
# Longest-common-subsequence length between two line arrays - O(n*m)
# dynamic programming, fine here since inputs are frontmatter blocks/body
# text (tens to a few hundred lines), not full files.
def lcs_laenge(a, b)
tabelle = Array.new(a.length + 1) { Array.new(b.length + 1, 0) }
(1..a.length).each do |i|
(1..b.length).each do |j|
tabelle[i][j] = if a[i - 1] == b[j - 1]
tabelle[i - 1][j - 1] + 1
else
[tabelle[i - 1][j], tabelle[i][j - 1]].max
end
end
end
tabelle[a.length][b.length]
end
# Added+removed line count between two versions, analogous to "git diff
# --numstat"'s sum - but computed on normalized frontmatter instead of the
# raw committed text. Decap CMS rewrites a file's *entire* frontmatter in
# its own formatting style (list indent, quoting, prose line width, field
# order) the first time it saves a file that wasn't already in that style -
# diffing the raw git blobs would count that one-time reformatting as
# dozens of "changed" lines even when only a single field's value actually
# changed. Frontmatter.geordnet (same canonical order write() already
# enforces, see lib/frontmatter.rb) removes the field-order component of
# that noise before YAML.dump removes the indent/quoting/width component;
# the body (plain prose, not YAML-serialized) is compared as-is.
def normalisierte_zeilen(vorher_fm, vorher_body, jetzt_fm, jetzt_body, feldreihenfolge)
vorher_zeilen = (vorher_fm ? YAML.dump(Frontmatter.geordnet(vorher_fm, feldreihenfolge)).lines : []) + (vorher_body || '').lines
jetzt_zeilen = YAML.dump(Frontmatter.geordnet(jetzt_fm, feldreihenfolge)).lines + jetzt_body.lines
gemeinsam = lcs_laenge(vorher_zeilen, jetzt_zeilen)
(vorher_zeilen.length - gemeinsam) + (jetzt_zeilen.length - gemeinsam)
end
def verlauf_fuer(relpath, content_dir, feldreihenfolge)
commits = commits_for(relpath, content_dir)
return nil if commits.empty?
@@ -107,8 +141,9 @@ def verlauf_fuer(relpath, content_dir)
next
end
fm, body = parsed
zeilen = normalisierte_zeilen(vorher_fm, vorher_body, fm, body, feldreihenfolge)
basis = { datum: c[:datum], autor: c[:autor], hash: c[:hash], zeilen: c[:zeilen] }
basis = { datum: c[:datum], autor: c[:autor], hash: c[:hash], zeilen: zeilen }
if vorher_fm.nil?
eintraege << basis.merge(typ: 'erstellt', felder: [])
else
@@ -128,7 +163,7 @@ end
# Both methods and variants are one folder per entry with content in
# index.md (Hugo page bundle).
def generate_bundle_verlauf(name)
def generate_bundle_verlauf(name, feldreihenfolge)
dir = File.join(CONTENT_REPO, name)
out = File.join(ROOT, 'hugo', 'data', "#{name}.json")
FileUtils.mkdir_p(File.dirname(out))
@@ -138,7 +173,7 @@ def generate_bundle_verlauf(name)
Dir.glob(File.join(dir, '*')).select { |d| File.directory?(d) }.each do |entry_dir|
slug = File.basename(entry_dir)
next unless File.exist?(File.join(entry_dir, 'index.md'))
daten = verlauf_fuer("#{name}/#{slug}/index.md", CONTENT_REPO)
daten = verlauf_fuer("#{name}/#{slug}/index.md", CONTENT_REPO, feldreihenfolge)
entries[slug] = daten if daten
end
end
@@ -149,8 +184,8 @@ end
exit if skip_wenn_unveraendert
generate_bundle_verlauf('methoden')
generate_bundle_verlauf('varianten')
generate_bundle_verlauf('methoden', Frontmatter::METHODEN_FELDER)
generate_bundle_verlauf('varianten', Frontmatter::VARIANTEN_FELDER)
FileUtils.mkdir_p(File.dirname(CACHE_FILE))
File.write(CACHE_FILE, JSON.pretty_generate({ content_head: content_head }))
+14 -4
View File
@@ -49,11 +49,21 @@ module Frontmatter
[fm, parts[2]]
end
# Reorders fm's keys to the canonical order (fields outside it appended
# unchanged at the end, see module comment) without writing anything -
# shared by write() below and generate-verlauf.rb, which reorders both
# sides of a historical comparison the same way before diffing so a
# field merely sitting at a different position (e.g. Decap CMS moving
# "tags" to the end) doesn't itself look like a change.
def self.geordnet(fm, feldreihenfolge)
ergebnis = {}
feldreihenfolge.each { |feld| ergebnis[feld] = fm[feld] if fm.key?(feld) }
(fm.keys - feldreihenfolge).each { |feld| ergebnis[feld] = fm[feld] }
ergebnis
end
def self.write(path, fm, body, feldreihenfolge:)
geordnet = {}
feldreihenfolge.each { |feld| geordnet[feld] = fm[feld] if fm.key?(feld) }
(fm.keys - feldreihenfolge).each { |feld| geordnet[feld] = fm[feld] }
yaml = YAML.dump(geordnet).sub(/\A---\n/, '')
yaml = YAML.dump(geordnet(fm, feldreihenfolge)).sub(/\A---\n/, '')
File.write(path, "---\n#{yaml}---#{body}", encoding: 'UTF-8')
end
end