Skip to content

Commit 33f6964

Browse files
authored
refactor: Use compact +N/-N/~N script change format (#19)
Ref #18
1 parent 8a02fcf commit 33f6964

File tree

4 files changed

+22
-12
lines changed

4 files changed

+22
-12
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ Run in CI **before** `fleetctl gitops --dry-run`. fleet-plan shows *what* change
2626
| Team-scoped | Diff one team, multiple teams, or all teams at once |
2727
| CI integration | `--git` auto-detects GitLab/GitHub, resolves changed files, posts MR/PR comment |
2828
| Multi-env merge | `--base` + `--env` merges config overlays in-memory (no `yq` needed) |
29-
| Script diffing | Line-count diffs for team scripts (+N/-N) |
29+
| Script diffing | Line-count diffs for team scripts (`+N/-N`, `~N` for single-line) |
3030
| Label validation | Cross-references labels against Fleet, shows host counts |
3131
| Multiple formats | Terminal (colored), JSON, Markdown |
3232
| Read-only | GET requests only, never mutates Fleet |

docs/Architecture.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ Compares `FleetState` (API) vs `ParsedRepo` (YAML). Produces `[]DiffResult` per
9898
| Fleet-maintained apps | `slug` | self_service |
9999
| App Store apps | `app_store_id` | self_service |
100100
| Profiles | PayloadDisplayName | add/delete only |
101-
| Scripts | filename | line count diff (+N/-N) |
101+
| Scripts | filename | line count diff (`+N/-N`, `~N` for single-line) |
102102
| Labels | `name` (cross-ref) | valid/missing with host counts |
103103

104104
Whitespace is normalized before comparison to avoid false positives from YAML vs API newline differences. Per-field diffs are stored in `ResourceChange.Fields` for both added and modified resources.

internal/diff/differ.go

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1205,13 +1205,15 @@ func getNestedValue(m map[string]any, key string) string {
12051205
// ---------- Helpers ----------
12061206

12071207
// scriptDiffSummary returns a human-readable summary of what changed between
1208-
// two normalized script contents. For single-line changes it shows the line
1209-
// number. For multi-line changes it shows insertion/deletion counts.
1208+
// two normalized script contents. Uses a compact "+added/-deleted" format,
1209+
// omitting zero counts. For single-line changes it includes the line number
1210+
// (e.g. "+5", "-3", "~2").
12101211
func scriptDiffSummary(old, new string) string {
12111212
oldLines := strings.Split(old, "\n")
12121213
newLines := strings.Split(new, "\n")
12131214

1214-
// Build a set of old lines for membership check
1215+
// Bag-of-lines: count lines present in new but not old (inserted)
1216+
// and lines present in old but not new (deleted).
12151217
oldSet := make(map[string]int)
12161218
for _, l := range oldLines {
12171219
oldSet[l]++
@@ -1221,7 +1223,6 @@ func scriptDiffSummary(old, new string) string {
12211223
newSet[l]++
12221224
}
12231225

1224-
// Count insertions (lines in new but not in old) and deletions (lines in old but not in new)
12251226
inserted := 0
12261227
for l, count := range newSet {
12271228
if diff := count - oldSet[l]; diff > 0 {
@@ -1236,7 +1237,7 @@ func scriptDiffSummary(old, new string) string {
12361237
}
12371238

12381239
if inserted+deleted == 1 {
1239-
// Find the single changed line and show its position
1240+
// Single change: find its line number.
12401241
maxLen := len(oldLines)
12411242
if len(newLines) > maxLen {
12421243
maxLen = len(newLines)
@@ -1250,18 +1251,27 @@ func scriptDiffSummary(old, new string) string {
12501251
n = newLines[i]
12511252
}
12521253
if o != n {
1254+
ln := i + 1
12531255
if o == "" {
1254-
return fmt.Sprintf("+Line %d", i+1)
1256+
return fmt.Sprintf("+%d", ln)
12551257
}
12561258
if n == "" {
1257-
return fmt.Sprintf("-Line %d", i+1)
1259+
return fmt.Sprintf("-%d", ln)
12581260
}
1259-
return fmt.Sprintf("~Line %d", i+1)
1261+
return fmt.Sprintf("~%d", ln)
12601262
}
12611263
}
12621264
}
12631265

1264-
return fmt.Sprintf("+%d lines, -%d lines", inserted, deleted)
1266+
// Compact format, omitting zero counts.
1267+
var parts []string
1268+
if inserted > 0 {
1269+
parts = append(parts, fmt.Sprintf("+%d", inserted))
1270+
}
1271+
if deleted > 0 {
1272+
parts = append(parts, fmt.Sprintf("-%d", deleted))
1273+
}
1274+
return strings.Join(parts, "/")
12651275
}
12661276

12671277
// normalizeScript normalizes a script for comparison: trims whitespace and

internal/output/markdown.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,7 @@ func mdFieldDetails(fields map[string]diff.FieldDiff) string {
199199
for _, name := range names {
200200
fd := fields[name]
201201
if fd.Old == "" && fd.New != "" {
202-
// Summary-only field (e.g., script diff with +N/-N lines)
202+
// Summary-only field (e.g., script diff with +N/-N format)
203203
parts = append(parts, fmt.Sprintf("`%s`: %s", name, mdCodeSpan(fd.New)))
204204
} else {
205205
old, new := mdDiffContext(fd.Old, fd.New, mdMaxFieldLen)

0 commit comments

Comments
 (0)