Files
interzone/internal/ui/diff.go
T
vvvomitandClaude Opus 5.5 f918c97687 Import a server set up with matrix-docker-ansible-deploy by hand
interzone import PATH, or i on Welcome and Servers, reads an existing
vars.yml and inventory/hosts and keeps every setting and secret: variables
interzone has no question for go into extra vars with their comments,
values it would write differently become overrides, and variables it
would add are held at the playbook defaults. The original files are only
read; the checkout is copied, and its vars.yml kept so the first Review
lists each variable that changes.

Also mask secrets in the Review diff against the last deploy.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-25 23:49:48 +03:30

97 lines
2.2 KiB
Go

package ui
import "strings"
// ── Line diff ──────────────────────────────────────────────────────
//
// Review shows how vars.yml differs from the last successful deploy's.
// The files are a few hundred lines, so a plain LCS table is quick enough.
type diffLine struct {
op byte // ' ' same, '-' only in the old file, '+' only in the new
text string
}
func lineDiff(old, cur []string) []diffLine {
n, m := len(old), len(cur)
// lcs[i][j]: longest common subsequence of old[i:] and cur[j:].
lcs := make([][]int, n+1)
for i := range lcs {
lcs[i] = make([]int, m+1)
}
for i := n - 1; i >= 0; i-- {
for j := m - 1; j >= 0; j-- {
if old[i] == cur[j] {
lcs[i][j] = lcs[i+1][j+1] + 1
} else {
lcs[i][j] = max(lcs[i+1][j], lcs[i][j+1])
}
}
}
var out []diffLine
i, j := 0, 0
for i < n || j < m {
switch {
case i < n && j < m && old[i] == cur[j]:
out = append(out, diffLine{' ', old[i]})
i, j = i+1, j+1
case i < n && (j == m || lcs[i+1][j] >= lcs[i][j+1]):
out = append(out, diffLine{'-', old[i]})
i++
default:
out = append(out, diffLine{'+', cur[j]})
j++
}
}
return out
}
// diffChanges counts the lines added and removed.
func diffChanges(d []diffLine) (added, removed int) {
for _, l := range d {
switch l.op {
case '+':
added++
case '-':
removed++
}
}
return
}
// diffView renders the changed lines with ctx lines around each, and a
// faint ⋯ where unchanged lines are left out.
func diffView(d []diffLine, ctx int) string {
keep := make([]bool, len(d))
for i, l := range d {
if l.op == ' ' {
continue
}
for k := max(0, i-ctx); k <= min(len(d)-1, i+ctx); k++ {
keep[k] = true
}
}
var out []string
skipped := false
for i, l := range d {
if !keep[i] {
skipped = true
continue
}
if skipped && len(out) > 0 {
out = append(out, sFaint.Render(" ⋯"))
}
skipped = false
l.text = maskLine(l.text)
switch l.op {
case '+':
out = append(out, sGood.Render("+ "+l.text))
case '-':
out = append(out, sBad.Render("- "+l.text))
default:
out = append(out, sFaint.Render(" "+l.text))
}
}
return strings.Join(out, "\n")
}