backup: pre-hardening baseline
This commit is contained in:
commit
9e4c612dcb
57 changed files with 10393 additions and 0 deletions
84
backend/internal/config/file/diff.go
Normal file
84
backend/internal/config/file/diff.go
Normal file
|
|
@ -0,0 +1,84 @@
|
|||
package file
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// UnifiedDiff returns a unified diff between old and new content
|
||||
func UnifiedDiff(filename, oldContent, newContent string) string {
|
||||
if oldContent == newContent {
|
||||
return ""
|
||||
}
|
||||
oldLines := splitLines(oldContent)
|
||||
newLines := splitLines(newContent)
|
||||
|
||||
// Simple line-based diff: collect added/removed
|
||||
// Use LCS-style? For MVP use simple prefix diff
|
||||
var b strings.Builder
|
||||
fmt.Fprintf(&b, "--- a/%s\n+++ b/%s\n", filename, filename)
|
||||
|
||||
// Find common prefix/suffix for brevity
|
||||
// Instead do full: show removed then added
|
||||
oldSet := make(map[string]int)
|
||||
for _, l := range oldLines {
|
||||
oldSet[l]++
|
||||
}
|
||||
newSet := make(map[string]int)
|
||||
for _, l := range newLines {
|
||||
newSet[l]++
|
||||
}
|
||||
|
||||
// Very simple: if old empty (new file), show all as +
|
||||
if len(oldLines) == 0 || (len(oldLines) == 1 && oldLines[0] == "") {
|
||||
for _, l := range newLines {
|
||||
fmt.Fprintf(&b, "+%s\n", l)
|
||||
}
|
||||
return b.String()
|
||||
}
|
||||
if len(newLines) == 0 {
|
||||
for _, l := range oldLines {
|
||||
fmt.Fprintf(&b, "-%s\n", l)
|
||||
}
|
||||
return b.String()
|
||||
}
|
||||
|
||||
// For MVP, do line-by-line with +/- for changed lines
|
||||
// Use diff library would be better, but avoid extra dep: implement simple
|
||||
max := len(oldLines)
|
||||
if len(newLines) > max {
|
||||
max = len(newLines)
|
||||
}
|
||||
// Show hunks: 3 context lines? Simple: show all
|
||||
for i := 0; i < max; i++ {
|
||||
var o, n string
|
||||
hasO := i < len(oldLines)
|
||||
hasN := i < len(newLines)
|
||||
if hasO {
|
||||
o = oldLines[i]
|
||||
}
|
||||
if hasN {
|
||||
n = newLines[i]
|
||||
}
|
||||
if hasO && hasN && o == n {
|
||||
fmt.Fprintf(&b, " %s\n", o)
|
||||
} else {
|
||||
if hasO {
|
||||
fmt.Fprintf(&b, "-%s\n", o)
|
||||
}
|
||||
if hasN {
|
||||
fmt.Fprintf(&b, "+%s\n", n)
|
||||
}
|
||||
}
|
||||
}
|
||||
return b.String()
|
||||
}
|
||||
|
||||
func splitLines(s string) []string {
|
||||
if s == "" {
|
||||
return []string{}
|
||||
}
|
||||
// Normalize line endings
|
||||
s = strings.ReplaceAll(s, "\r\n", "\n")
|
||||
return strings.Split(s, "\n")
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue