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:
+57
-22
@@ -19,6 +19,7 @@
|
|||||||
require_relative 'lib/frontmatter'
|
require_relative 'lib/frontmatter'
|
||||||
require 'json'
|
require 'json'
|
||||||
require 'fileutils'
|
require 'fileutils'
|
||||||
|
require 'yaml'
|
||||||
|
|
||||||
ROOT = File.expand_path('..', __dir__)
|
ROOT = File.expand_path('..', __dir__)
|
||||||
CONTENT_REPO = File.join(ROOT, '_content')
|
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
|
# 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. --numstat (instead of --name-only) gives both the
|
# regular spaces/dashes. --name-only gives the historical path at each
|
||||||
# historical path at each commit (needed for --follow across renames, same
|
# commit (needed for --follow across renames, same technique
|
||||||
# technique scripts/validate.rb's vorherige_kennung already uses for the
|
# scripts/validate.rb's vorherige_kennung already uses for the single most
|
||||||
# single most recent rename) AND the added/deleted line count in the same
|
# recent rename). --no-merges: merge commits (e.g. from a regularly merged
|
||||||
# pass, no second git call needed for that. --no-merges: merge commits
|
# Pull Request against the content repo, see the "Ein Server oder
|
||||||
# (e.g. from a regularly merged Pull Request against the content repo, see
|
# zwei"/PR workflow docs) carry no file list under --name-only by default -
|
||||||
# the "Ein Server oder zwei"/PR workflow docs) carry no file list under
|
# left in, the fixed 2-lines-per-commit pairing below would desync at
|
||||||
# --name-only/--numstat by default - left in, the fixed 2-lines-per-commit
|
# exactly that commit and misattribute every following entry. Excluding
|
||||||
# pairing below would desync at exactly that commit and misattribute every
|
# them is also correct on its own terms: a merge commit isn't itself a
|
||||||
# following entry. Excluding them is also correct on its own terms: a merge
|
# content edit, the actual edits are the commits it merges, which still
|
||||||
# commit isn't itself a content edit, the actual edits are the commits it
|
# appear individually here regardless.
|
||||||
# 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 --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?)
|
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")
|
||||||
hinzu, weg, pfad = zeilen[i + 1].split("\t")
|
pfad = zeilen[i + 1]
|
||||||
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 }
|
||||||
commits << { hash: hash, autor: autor, datum: datum, pfad: pfad, zeilen: zeilen_geaendert }
|
|
||||||
i += 2
|
i += 2
|
||||||
end
|
end
|
||||||
commits.reverse
|
commits.reverse
|
||||||
@@ -92,7 +91,42 @@ def geaenderte_felder(vorher_fm, vorher_body, jetzt_fm, jetzt_body)
|
|||||||
felder
|
felder
|
||||||
end
|
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)
|
commits = commits_for(relpath, content_dir)
|
||||||
return nil if commits.empty?
|
return nil if commits.empty?
|
||||||
|
|
||||||
@@ -107,8 +141,9 @@ def verlauf_fuer(relpath, content_dir)
|
|||||||
next
|
next
|
||||||
end
|
end
|
||||||
fm, body = parsed
|
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?
|
if vorher_fm.nil?
|
||||||
eintraege << basis.merge(typ: 'erstellt', felder: [])
|
eintraege << basis.merge(typ: 'erstellt', felder: [])
|
||||||
else
|
else
|
||||||
@@ -128,7 +163,7 @@ end
|
|||||||
|
|
||||||
# Both methods and variants are one folder per entry with content in
|
# Both methods and variants are one folder per entry with content in
|
||||||
# index.md (Hugo page bundle).
|
# index.md (Hugo page bundle).
|
||||||
def generate_bundle_verlauf(name)
|
def generate_bundle_verlauf(name, feldreihenfolge)
|
||||||
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")
|
||||||
FileUtils.mkdir_p(File.dirname(out))
|
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|
|
Dir.glob(File.join(dir, '*')).select { |d| File.directory?(d) }.each do |entry_dir|
|
||||||
slug = File.basename(entry_dir)
|
slug = File.basename(entry_dir)
|
||||||
next unless File.exist?(File.join(entry_dir, 'index.md'))
|
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
|
entries[slug] = daten if daten
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
@@ -149,8 +184,8 @@ end
|
|||||||
|
|
||||||
exit if skip_wenn_unveraendert
|
exit if skip_wenn_unveraendert
|
||||||
|
|
||||||
generate_bundle_verlauf('methoden')
|
generate_bundle_verlauf('methoden', Frontmatter::METHODEN_FELDER)
|
||||||
generate_bundle_verlauf('varianten')
|
generate_bundle_verlauf('varianten', Frontmatter::VARIANTEN_FELDER)
|
||||||
|
|
||||||
FileUtils.mkdir_p(File.dirname(CACHE_FILE))
|
FileUtils.mkdir_p(File.dirname(CACHE_FILE))
|
||||||
File.write(CACHE_FILE, JSON.pretty_generate({ content_head: content_head }))
|
File.write(CACHE_FILE, JSON.pretty_generate({ content_head: content_head }))
|
||||||
|
|||||||
@@ -49,11 +49,21 @@ module Frontmatter
|
|||||||
[fm, parts[2]]
|
[fm, parts[2]]
|
||||||
end
|
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:)
|
def self.write(path, fm, body, feldreihenfolge:)
|
||||||
geordnet = {}
|
yaml = YAML.dump(geordnet(fm, feldreihenfolge)).sub(/\A---\n/, '')
|
||||||
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/, '')
|
|
||||||
File.write(path, "---\n#{yaml}---#{body}", encoding: 'UTF-8')
|
File.write(path, "---\n#{yaml}---#{body}", encoding: 'UTF-8')
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
Reference in New Issue
Block a user