Fix the retarded fucking diffing

This commit is contained in:
2025-10-28 10:19:10 +01:00
parent 74bce0561b
commit 755754a0a4

View File

@@ -140,7 +140,7 @@ func syncConfigs(config *Config) error {
if err != nil { if err != nil {
return fmt.Errorf("failed to get current directory: %w", err) return fmt.Errorf("failed to get current directory: %w", err)
} }
logger := cylogger.Default.WithPrefix(fmt.Sprintf("dir=%s", currentDir)) logger := cylogger.Default
// Get existing .caddy files // Get existing .caddy files
logger.Debug("Scanning for existing .caddy files") logger.Debug("Scanning for existing .caddy files")
@@ -180,7 +180,7 @@ func syncConfigs(config *Config) error {
return fmt.Errorf("failed to create file %s: %w", caddyFilename, err) return fmt.Errorf("failed to create file %s: %w", caddyFilename, err)
} }
} }
} else if normalizeContent(existingContent) != normalizeContent(expectedContent) { } else if !contentMatches(existingContent, expectedContent) {
// File exists but content differs // File exists but content differs
fileLogger.Info("File content differs, updating") fileLogger.Info("File content differs, updating")
updated++ updated++
@@ -335,14 +335,37 @@ func generateAndLogDiff(logger *cylogger.Logger, filename, existingContent, expe
} }
} }
func normalizeContent(content string) string { func contentMatches(existing, expected string) bool {
// Normalize content for comparison by trimming whitespace and standardizing line endings dmp := diffmatchpatch.New()
lines := strings.Split(content, "\n") diffs := dmp.DiffMain(normalize(existing), normalize(expected), false)
// If there are any insertions or deletions, content differs
for _, diff := range diffs {
if diff.Type == diffmatchpatch.DiffInsert || diff.Type == diffmatchpatch.DiffDelete {
cylogger.Trace("Content differs at %q", diff.Text)
return false
}
}
return true
}
func normalize(text string) string {
// Standardize line endings
text = strings.ReplaceAll(text, "\r\n", "\n")
text = strings.ReplaceAll(text, "\r", "\n")
// Split into lines and normalize each
lines := strings.Split(text, "\n")
var normalizedLines []string var normalizedLines []string
for _, line := range lines { for _, line := range lines {
normalizedLines = append(normalizedLines, strings.TrimSpace(line)) trimmed := strings.TrimSpace(line)
if trimmed != "" { // Skip empty lines
normalizedLines = append(normalizedLines, trimmed)
}
} }
return strings.Join(normalizedLines, "\n") // Join with single newlines and trim ends
result := strings.Join(normalizedLines, "\n")
return strings.TrimSpace(result)
} }