/
hash.go
83 lines (75 loc) · 1.95 KB
/
hash.go
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
package loader
import (
"fmt"
"runtime"
"sort"
"strings"
"honnef.co/go/tools/internal/cache"
)
// computeHash computes a package's hash. The hash is based on all Go
// files that make up the package, as well as the hashes of imported
// packages.
func computeHash(pkg *PackageSpec) (cache.ActionID, error) {
key := cache.NewHash("package " + pkg.PkgPath)
fmt.Fprintf(key, "goos %s goarch %s\n", runtime.GOOS, runtime.GOARCH)
fmt.Fprintf(key, "import %q\n", pkg.PkgPath)
// Compute the hashes of all files making up the package. As an
// optimization, we use the build ID that Go already computed for
// us, because it is virtually identical to hashed all
// CompiledGoFiles.
success := false
if pkg.ExportFile != "" {
id, err := getBuildid(pkg.ExportFile)
if err == nil {
if idx := strings.IndexRune(id, '/'); idx > -1 {
fmt.Fprintf(key, "files %s\n", id[:idx])
success = true
}
}
}
if !success {
for _, f := range pkg.CompiledGoFiles {
h, err := cache.FileHash(f)
if err != nil {
return cache.ActionID{}, err
}
fmt.Fprintf(key, "file %s %x\n", f, h)
}
}
imps := make([]*PackageSpec, 0, len(pkg.Imports))
for _, v := range pkg.Imports {
imps = append(imps, v)
}
sort.Slice(imps, func(i, j int) bool {
return imps[i].PkgPath < imps[j].PkgPath
})
for _, dep := range imps {
if dep.ExportFile == "" {
fmt.Fprintf(key, "import %s \n", dep.PkgPath)
} else {
id, err := getBuildid(dep.ExportFile)
if err == nil {
fmt.Fprintf(key, "import %s %s\n", dep.PkgPath, id)
} else {
fh, err := cache.FileHash(dep.ExportFile)
if err != nil {
return cache.ActionID{}, err
}
fmt.Fprintf(key, "import %s %x\n", dep.PkgPath, fh)
}
}
}
return key.Sum(), nil
}
var buildidCache = map[string]string{}
func getBuildid(f string) (string, error) {
if h, ok := buildidCache[f]; ok {
return h, nil
}
h, err := ReadFile(f)
if err != nil {
return "", err
}
buildidCache[f] = h
return h, nil
}