-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdiff.go
More file actions
66 lines (60 loc) · 1.8 KB
/
diff.go
File metadata and controls
66 lines (60 loc) · 1.8 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
package analyzer
import (
"fmt"
"os"
"github.com/randomcodespace/codeiq/internal/cache"
)
// Delta is the result of comparing the on-disk file set to the cache state.
// All slices are sorted by path (FileDiscovery sorts; AllFiles iterates in
// path order) so callers can rely on stable order.
type Delta struct {
Added []string // on disk, not in cache
Modified []string // path in cache but content_hash differs from disk
Deleted []string // in cache, missing from disk
Unchanged []string // path + content_hash match cache exactly
}
// Diff walks the project root via FileDiscovery and classifies each file
// against the cache. UNCHANGED files cost one hash per file; nothing else
// is parsed or detected.
//
// Returns Delta with empty slices (not nil) when there is no work in a
// bucket.
func (a *Analyzer) Diff(root string) (Delta, error) {
d := Delta{}
if a.opts.Cache == nil {
return d, fmt.Errorf("diff: cache is required")
}
disc := NewFileDiscovery()
files, err := disc.Discover(root)
if err != nil {
return d, fmt.Errorf("file discovery: %w", err)
}
seen := make(map[string]bool, len(files))
for _, f := range files {
seen[f.RelPath] = true
content, err := os.ReadFile(f.AbsPath)
if err != nil {
fmt.Fprintf(os.Stderr, "codeiq: diff: %s: %v\n", f.RelPath, err)
continue
}
curHash := cache.HashString(string(content))
cachedHash, _, ok := a.opts.Cache.GetFileByPath(f.RelPath)
switch {
case !ok:
d.Added = append(d.Added, f.RelPath)
case cachedHash == curHash:
d.Unchanged = append(d.Unchanged, f.RelPath)
default:
d.Modified = append(d.Modified, f.RelPath)
}
}
if err := a.opts.Cache.AllFiles(func(path, _ string) error {
if !seen[path] {
d.Deleted = append(d.Deleted, path)
}
return nil
}); err != nil {
return d, err
}
return d, nil
}