-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcache.go
More file actions
141 lines (126 loc) · 4.04 KB
/
cache.go
File metadata and controls
141 lines (126 loc) · 4.04 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
package repomap
import (
"context"
"crypto/sha256"
"encoding/json"
"fmt"
"os"
"path/filepath"
"time"
)
// outputCache lazily computes and caches formatted output strings.
type outputCache struct {
compact *string // enriched default (m.String())
verbose *string
detail *string
lines *string
xml *string
orientation *string // lean compact / orientation mode (m.StringCompact())
}
func (c *outputCache) get(ptr **string, fn func() string) string {
if *ptr == nil {
s := fn()
*ptr = &s
}
return **ptr
}
func (c *outputCache) reset() {
c.compact = nil
c.verbose = nil
c.detail = nil
c.lines = nil
c.xml = nil
c.orientation = nil
}
// diskCache is the on-disk format for a cached repomap build.
type diskCache struct {
Version int `json:"version"`
Root string `json:"root"`
BuiltAt time.Time `json:"built_at"`
Mtimes map[string]time.Time `json:"mtimes"`
ContentHashes map[string]string `json:"content_hashes,omitempty"` // path → sha256 hex; absent in old caches (mtime fallback)
Output string `json:"output"`
OutputLines string `json:"output_lines"`
Ranked []RankedFile `json:"ranked"`
LastSHA string `json:"last_sha,omitempty"` // HEAD sha at write time; "" when not a git repo
GitRoot bool `json:"git_root,omitempty"` // true if root was inside a git repo at write time
Explain bool `json:"explain,omitempty"` // whether --explain was active at write time
}
const cacheVersion = 7
// SaveCache writes the current map state to disk.
func (m *Map) SaveCache(cacheDir string) error {
return m.SaveCacheContext(context.Background(), cacheDir)
}
// SaveCacheContext writes the current map state to disk with caller cancellation.
func (m *Map) SaveCacheContext(ctx context.Context, cacheDir string) error {
m.mu.Lock()
if m.builtAt.IsZero() || len(m.ranked) == 0 {
m.mu.Unlock()
return nil
}
// Compute lazy strings if not yet cached, so they are persisted.
// Cache always stores the non-explain output; explain is a rendering flag, not a build artifact.
compact := m.outputs.get(&m.outputs.compact, func() string {
return FormatMap(m.ranked, m.config.MaxTokens, false, false, m.blocklist, false)
})
lines := m.outputs.get(&m.outputs.lines, func() string {
return FormatLines(m.ranked, m.config.MaxTokensNoCtx, m.root)
})
entry := diskCache{
Version: cacheVersion,
Root: m.root,
BuiltAt: m.builtAt,
Mtimes: m.mtimes,
ContentHashes: m.contentHashes,
Output: compact,
OutputLines: lines,
Ranked: m.ranked,
Explain: m.config.Explain,
}
if isInsideGitRepo(m.root) {
entry.GitRoot = true
if sha, err := gitHeadSHA(ctx, m.root); err == nil {
entry.LastSHA = sha
}
}
m.mu.Unlock()
data, err := json.Marshal(&entry)
if err != nil {
return fmt.Errorf("marshal cache: %w", err)
}
path := cachePath(cacheDir, m.root)
if err := atomicWriteFile(path, data, 0o644); err != nil {
return fmt.Errorf("write cache: %w", err)
}
return nil
}
// LoadCache loads a previously saved map from disk. Returns false if
// the cache is missing, corrupt, or for a different version.
func (m *Map) LoadCache(cacheDir string) bool {
path := cachePath(cacheDir, m.root)
data, err := os.ReadFile(path)
if err != nil {
return false
}
var entry diskCache
if err := json.Unmarshal(data, &entry); err != nil {
return false
}
if entry.Version != cacheVersion || entry.Root != m.root || entry.Explain != m.config.Explain {
return false
}
m.mu.Lock()
m.ranked = entry.Ranked
m.outputs.compact = &entry.Output
m.outputs.lines = &entry.OutputLines
m.builtAt = entry.BuiltAt
m.mtimes = entry.Mtimes
m.contentHashes = entry.ContentHashes // nil for old caches → mtime-only fallback
m.mu.Unlock()
return true
}
func cachePath(cacheDir, root string) string {
h := sha256.Sum256([]byte(root))
name := fmt.Sprintf("repomap-%x.json", h[:8])
return filepath.Join(cacheDir, name)
}