Skip to content

Commit 431a802

Browse files
committed
chore: factorize computePkgHash
1 parent ecf19ca commit 431a802

File tree

1 file changed

+23
-14
lines changed

1 file changed

+23
-14
lines changed

internal/cache/cache.go

Lines changed: 23 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -127,9 +127,6 @@ func (c *Cache) pkgActionID(pkg *packages.Package, mode HashMode) (cache.ActionI
127127
return key.Sum(), nil
128128
}
129129

130-
// packageHash computes a package's hash.
131-
// The hash is based on all Go files that make up the package,
132-
// as well as the hashes of imported packages.
133130
func (c *Cache) packageHash(pkg *packages.Package, mode HashMode) (string, error) {
134131
hashResI, ok := c.pkgHashes.Load(pkg)
135132
if ok {
@@ -141,9 +138,27 @@ func (c *Cache) packageHash(pkg *packages.Package, mode HashMode) (string, error
141138
return hashRes[mode], nil
142139
}
143140

141+
hashRes, err := c.computePkgHash(pkg)
142+
if err != nil {
143+
return "", err
144+
}
145+
146+
if _, ok := hashRes[mode]; !ok {
147+
return "", fmt.Errorf("invalid mode %d", mode)
148+
}
149+
150+
c.pkgHashes.Store(pkg, hashRes)
151+
152+
return hashRes[mode], nil
153+
}
154+
155+
// computePkgHash computes a package's hash.
156+
// The hash is based on all Go files that make up the package,
157+
// as well as the hashes of imported packages.
158+
func (c *Cache) computePkgHash(pkg *packages.Package) (hashResults, error) {
144159
key, err := cache.NewHash("package hash")
145160
if err != nil {
146-
return "", fmt.Errorf("failed to make a hash: %w", err)
161+
return nil, fmt.Errorf("failed to make a hash: %w", err)
147162
}
148163

149164
hashRes := hashResults{}
@@ -153,7 +168,7 @@ func (c *Cache) packageHash(pkg *packages.Package, mode HashMode) (string, error
153168
for _, f := range pkg.CompiledGoFiles {
154169
h, fErr := c.fileHash(f)
155170
if fErr != nil {
156-
return "", fmt.Errorf("failed to calculate file %s hash: %w", f, fErr)
171+
return nil, fmt.Errorf("failed to calculate file %s hash: %w", f, fErr)
157172
}
158173

159174
fmt.Fprintf(key, "file %s %x\n", f, h)
@@ -169,26 +184,20 @@ func (c *Cache) packageHash(pkg *packages.Package, mode HashMode) (string, error
169184
})
170185

171186
if err := c.computeDepsHash(HashModeNeedOnlySelf, imps, key); err != nil {
172-
return "", err
187+
return nil, err
173188
}
174189

175190
curSum = key.Sum()
176191
hashRes[HashModeNeedDirectDeps] = hex.EncodeToString(curSum[:])
177192

178193
if err := c.computeDepsHash(HashModeNeedAllDeps, imps, key); err != nil {
179-
return "", err
194+
return nil, err
180195
}
181196

182197
curSum = key.Sum()
183198
hashRes[HashModeNeedAllDeps] = hex.EncodeToString(curSum[:])
184199

185-
if _, ok := hashRes[mode]; !ok {
186-
return "", fmt.Errorf("invalid mode %d", mode)
187-
}
188-
189-
c.pkgHashes.Store(pkg, hashRes)
190-
191-
return hashRes[mode], nil
200+
return hashRes, nil
192201
}
193202

194203
func (c *Cache) computeDepsHash(depMode HashMode, imps []*packages.Package, key *cache.Hash) error {

0 commit comments

Comments
 (0)