-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdiffcmd.go
More file actions
195 lines (181 loc) · 6.43 KB
/
Copy pathdiffcmd.go
File metadata and controls
195 lines (181 loc) · 6.43 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
// diffcmd.go — the `sidecar diff` subcommand: print board changes since the
// last run, then advance the snapshot. Built for hook use — it never exits
// non-zero for an absent board, absent snapshot, or unchanged file.
package main
import (
"bytes"
"fmt"
"hash/fnv"
"os"
"path/filepath"
"strings"
)
func runDiff(args []string) int {
if len(args) > 1 {
fmt.Fprintln(os.Stderr, "sidecar diff: too many arguments")
return 2
}
path := defaultBoardPath()
if len(args) > 0 {
switch args[0] {
case "-h", "--help":
fmt.Println("usage: sidecar diff [file.md]")
fmt.Println("Prints board changes since the last run.")
return 0
default:
if strings.HasPrefix(args[0], "-") {
fmt.Fprintf(os.Stderr, "sidecar diff: unknown flag %q\n", args[0])
return 2
}
path = args[0]
}
}
abs, err := filepath.Abs(expandTilde(path))
if err != nil {
fmt.Fprintln(os.Stderr, "sidecar diff:", err)
return 1
}
raw, err := os.ReadFile(abs)
if err != nil {
if !os.IsNotExist(err) {
fmt.Fprintln(os.Stderr, "sidecar diff:", err)
}
return 0
}
snap := snapshotPath(abs)
prev, err := os.ReadFile(snap)
if err != nil {
if !os.IsNotExist(err) {
// Some other read failure (e.g. EACCES) — writing would likely
// fail too, and reseeding here would lose the baseline forever.
// Report it and leave the snapshot untouched; the next run tries
// again rather than silently treating this as a fresh start.
fmt.Fprintln(os.Stderr, "sidecar diff:", err)
return 0
}
writeSnapshot(snap, raw) // first run — seed silently
return 0
}
if bytes.Equal(prev, raw) {
return 0
}
for _, line := range cappedDiffLines(diffLines(string(prev), string(raw))) {
fmt.Println(line)
}
fmt.Println(closingReminder(path, string(raw)))
writeSnapshot(snap, raw)
return 0
}
// maxDiffOutputLines caps what runDiff prints — the diff feeds straight into
// the model's prompt on every turn, so an enormous change (a rewrite, a
// paste) must not flood it; the board itself is always the source of truth.
const maxDiffOutputLines = 100
// cappedDiffLines truncates lines to maxDiffOutputLines, appending a tail
// line noting how many were omitted. Applies uniformly to both diffLines
// paths (semantic and unifiedU0 fallback) since the cap is on what enters
// the prompt, not on how the diff was computed.
func cappedDiffLines(lines []string) []string {
if len(lines) <= maxDiffOutputLines {
return lines
}
out := append([]string{}, lines[:maxDiffOutputLines]...)
return append(out, fmt.Sprintf("… %d more lines — read the board", len(lines)-maxDiffOutputLines))
}
// defaultBoardPath resolves the board for bare invocations: the .sidecar/
// home when present, the legacy root file when that's all there is, and the
// .sidecar/ home again as the target for fresh setups.
func defaultBoardPath() string {
home := filepath.Join(sidecarDirName, "sidecar.md")
if _, err := os.Stat(home); err == nil {
return home
}
if _, err := os.Stat(legacyFile); err == nil {
return legacyFile
}
return home
}
// snapshotPath is .sidecar/previous.md for the standard board, and a
// path-keyed previous-<hash>.md beside it for explicit board files. When the
// board's own parent dir is already named .sidecar (e.g. .sidecar/notes.md),
// the keyed snapshot goes directly in that dir rather than doubling it up
// into .sidecar/.sidecar/.
func snapshotPath(boardAbs string) string {
dir := filepath.Dir(boardAbs)
if filepath.Base(dir) == sidecarDirName && filepath.Base(boardAbs) == "sidecar.md" {
return filepath.Join(dir, "previous.md")
}
h := fnv.New32a()
h.Write([]byte(boardAbs))
name := fmt.Sprintf("previous-%08x.md", h.Sum32())
if filepath.Base(dir) == sidecarDirName {
return filepath.Join(dir, name)
}
return filepath.Join(dir, sidecarDirName, name)
}
// writeSnapshot advances the snapshot, keeping exit 0 even when it fails (a
// hook must never fail the turn) but printing the error rather than
// swallowing it — otherwise a read-only checkout reprints the same diff
// forever with no explanation of why it never goes silent.
func writeSnapshot(path string, data []byte) {
dir := filepath.Dir(path)
_, statErr := os.Stat(dir)
freshDir := os.IsNotExist(statErr)
if err := os.MkdirAll(dir, 0o755); err != nil {
fmt.Fprintln(os.Stderr, "sidecar diff:", err)
return
}
if err := writeFileAtomic(path, data); err != nil {
fmt.Fprintln(os.Stderr, "sidecar diff:", err)
return
}
// A legacy or custom board not itself resident in .sidecar/ gets a fresh
// .sidecar/ MkdirAll'd here for its snapshot — the one place sidecar
// writes into a repo without arranging to be ignored. Exclude it now,
// the same as init does for the default board — but this runs on every
// plain `sidecar diff` hook invocation, so it must stay silent on
// stdout like the rest of this path (verbose=false); errors still
// surface via writeIgnore's own stderr print.
if freshDir && filepath.Base(dir) == sidecarDirName {
excludeSidecarDir(repoRoot(filepath.Dir(dir)), false)
}
}
// writeFileAtomic writes data to path via a temp file in the same directory
// followed by a rename, instead of os.WriteFile's truncate-in-place. A hook
// killed mid-write (the process gets no graceful shutdown) would otherwise
// leave a half-written snapshot, and the next diff would compare against
// that garbage and dump bogus changes. The rename is same-directory, so it's
// atomic on any filesystem this runs on. The temp name's different basename
// also means the viewer's watcher (which only reacts to its exact watched
// filename, see watcher.go) stays quiet even if a crash leaves one behind.
func writeFileAtomic(path string, data []byte) error {
dir := filepath.Dir(path)
tmp, err := os.CreateTemp(dir, "."+filepath.Base(path)+".tmp-*")
if err != nil {
return err
}
tmpPath := tmp.Name()
defer os.Remove(tmpPath) // no-op once the rename below succeeds
if _, err := tmp.Write(data); err != nil {
tmp.Close()
return err
}
if err := tmp.Close(); err != nil {
return err
}
if err := os.Chmod(tmpPath, 0o644); err != nil {
return err
}
return os.Rename(tmpPath, path)
}
// closingReminder is the last line of a non-empty diff: the reconcile
// reminder, with section labels read from the board itself so custom
// sections stay accurate.
func closingReminder(rel, raw string) string {
var labels []string
if b, ok := parseBoard(raw); ok {
for _, s := range b.Sections {
labels = append(labels, s.Label)
}
}
return reconcileMessageLabels(rel, labels)
}