Skip to content

Commit 77368bc

Browse files
committed
refactor: make Hasher public + move CSI under logging
1 parent 308c8fc commit 77368bc

File tree

6 files changed

+39
-39
lines changed

6 files changed

+39
-39
lines changed

engine/database.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,13 @@ import (
99

1010
type HashDB struct {
1111
path string
12-
hashes map[string]hasher
12+
hashes map[string]Hasher
1313
}
1414

1515
func NewHashDB(path string) (*HashDB, error) {
1616
db := &HashDB{
1717
path: path,
18-
hashes: map[string]hasher{},
18+
hashes: map[string]Hasher{},
1919
}
2020
r, err := os.Open(path)
2121
if err == nil {
@@ -48,12 +48,12 @@ func (db *HashDB) Close() error {
4848
return os.Rename(db.path+"~", db.path)
4949
}
5050

51-
func (db *HashDB) Get(path string) (hasher, bool) {
51+
func (db *HashDB) Get(path string) (Hasher, bool) {
5252
h, ok := db.hashes[path]
5353
return h, ok
5454
}
5555

56-
func (db *HashDB) Set(path string, hash hasher) {
56+
func (db *HashDB) Set(path string, hash Hasher) {
5757
db.hashes[path] = hash
5858
}
5959

engine/engine.go

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -35,11 +35,11 @@ type Target struct {
3535
buildFunc func(logger *logging.Logger, target *Target) error
3636
cleanFunc func(logger *logging.Logger, target *Target) error
3737
// Hash function for virtual targets.
38-
hashFunc *internal.MemoisedFunction[hasher]
38+
hashFunc *internal.MemoisedFunction[Hasher]
3939
// Hash stored in the DB.
40-
storedHash hasher
40+
storedHash Hasher
4141
// Hash computed from the filesystem.
42-
realHash hasher
42+
realHash Hasher
4343
chdir *parser.Ref
4444
synthetic bool
4545
}
@@ -165,7 +165,7 @@ func (e *Engine) analyse(bitfile *parser.Bitfile) error {
165165

166166
case "hash":
167167
target.hashPos = directive.Value.Pos
168-
target.hashFunc = internal.Memoise(func() (hasher, error) {
168+
target.hashFunc = internal.Memoise(func() (Hasher, error) {
169169
command, err := e.evaluateString(directive.Value.Pos, directive.Value.Body, target, map[string]bool{})
170170
if err != nil {
171171
return 0, participle.Errorf(directive.Value.Pos, "hash command is invalid")
@@ -176,8 +176,8 @@ func (e *Engine) analyse(bitfile *parser.Bitfile) error {
176176
if err != nil {
177177
return 0, participle.Errorf(directive.Value.Pos, "failed to run hash command: %s", err)
178178
}
179-
hfh := newHasher()
180-
hfh.bytes(output)
179+
hfh := NewHasher()
180+
hfh.Bytes(output)
181181
return hfh, nil
182182
})
183183

@@ -481,10 +481,10 @@ func (e *Engine) defaultBuildFunc(log *logging.Logger, target *Target) error {
481481
}
482482

483483
// A function used to compute a hash of an output.
484-
type outputRefHasher func(target *Target, ref *parser.Ref) (hasher, error)
484+
type outputRefHasher func(target *Target, ref *parser.Ref) (Hasher, error)
485485

486-
func (e *Engine) recursivelyComputeHash(target *Target, refHasher outputRefHasher, seen map[string]bool, forEach func(*Target, hasher)) (hasher, error) {
487-
h := newHasher()
486+
func (e *Engine) recursivelyComputeHash(target *Target, refHasher outputRefHasher, seen map[string]bool, forEach func(*Target, Hasher)) (Hasher, error) {
487+
h := NewHasher()
488488
for _, input := range target.inputs.Refs {
489489
if _, ok := seen[input.Text]; ok {
490490
continue
@@ -498,40 +498,40 @@ func (e *Engine) recursivelyComputeHash(target *Target, refHasher outputRefHashe
498498
if err != nil {
499499
return 0, err
500500
}
501-
h.update(subh)
501+
h.Update(subh)
502502
}
503503
for _, output := range target.outputs.Refs {
504504
rh, err := refHasher(target, output)
505505
if err != nil {
506506
return 0, participle.Wrapf(output.Pos, err, "hash failed")
507507
}
508-
h.update(rh)
508+
h.Update(rh)
509509
}
510510
forEach(target, h)
511511
return h, nil
512512
}
513513

514514
// Compute hash of target - inputs and outputs.
515-
func (e *Engine) computeHash(target *Target, refHasher outputRefHasher) (hasher, error) {
516-
h := newHasher()
515+
func (e *Engine) computeHash(target *Target, refHasher outputRefHasher) (Hasher, error) {
516+
h := NewHasher()
517517
for _, input := range target.inputs.Refs {
518518
inputTarget, err := e.getTarget(input.Text)
519519
if err != nil {
520520
return 0, err
521521
}
522-
h.int(uint64(inputTarget.storedHash))
522+
h.Int(uint64(inputTarget.storedHash))
523523
}
524524
for _, output := range target.outputs.Refs {
525525
rh, err := refHasher(target, output)
526526
if err != nil {
527527
return 0, participle.Wrapf(output.Pos, err, "hash failed")
528528
}
529-
h.update(rh)
529+
h.Update(rh)
530530
}
531531
return h, nil
532532
}
533533

534-
func (e *Engine) dbRefHasher(target *Target, ref *parser.Ref) (hasher, error) { //nolint:revive
534+
func (e *Engine) dbRefHasher(target *Target, ref *parser.Ref) (Hasher, error) { //nolint:revive
535535
h, ok := e.db.Get(ref.Text)
536536
if !ok {
537537
return 0, nil
@@ -540,8 +540,8 @@ func (e *Engine) dbRefHasher(target *Target, ref *parser.Ref) (hasher, error) {
540540
}
541541

542542
// Hash real files.
543-
func (e *Engine) realRefHasher(target *Target, ref *parser.Ref) (hasher, error) {
544-
h := newHasher()
543+
func (e *Engine) realRefHasher(target *Target, ref *parser.Ref) (Hasher, error) {
544+
h := NewHasher()
545545
h.string(ref.Text)
546546

547547
// If we have a hash function, use that for every reference.
@@ -550,7 +550,7 @@ func (e *Engine) realRefHasher(target *Target, ref *parser.Ref) (hasher, error)
550550
if err != nil {
551551
return 0, participle.Errorf(target.hashPos, "failed to compute hash: %s", err)
552552
}
553-
h.update(hf)
553+
h.Update(hf)
554554
return h, nil
555555
}
556556

@@ -559,10 +559,10 @@ func (e *Engine) realRefHasher(target *Target, ref *parser.Ref) (hasher, error)
559559
return 0, err
560560
}
561561

562-
h.int(uint64(info.Mode()))
562+
h.Int(uint64(info.Mode()))
563563
if !info.IsDir() {
564-
h.int(uint64(info.Size()))
565-
h.int(uint64(info.ModTime().UnixNano()))
564+
h.Int(uint64(info.Size()))
565+
h.Int(uint64(info.ModTime().UnixNano()))
566566
}
567567
return h, nil
568568
}
@@ -660,13 +660,13 @@ func (e *Engine) evaluate() error {
660660
// Second pass - restore hashes from the DB.
661661
for _, target := range e.targets {
662662
logger := e.targetLogger(target)
663-
_, err := e.recursivelyComputeHash(target, e.dbRefHasher, map[string]bool{}, func(target *Target, h hasher) {
663+
_, err := e.recursivelyComputeHash(target, e.dbRefHasher, map[string]bool{}, func(target *Target, h Hasher) {
664664
target.storedHash = h
665665
})
666666
if err != nil && !errors.Is(err, os.ErrNotExist) {
667667
return err
668668
}
669-
_, err = e.recursivelyComputeHash(target, e.realRefHasher, map[string]bool{}, func(target *Target, h hasher) {
669+
_, err = e.recursivelyComputeHash(target, e.realRefHasher, map[string]bool{}, func(target *Target, h Hasher) {
670670
target.realHash = h
671671
})
672672
if err != nil && !errors.Is(err, os.ErrNotExist) {

engine/hasher.go

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,46 +8,46 @@ import (
88
const offset64 = 14695981039346656037
99
const prime64 = 1099511628211
1010

11-
type hasher uint64
11+
type Hasher uint64
1212

13-
func newHasher() hasher { return offset64 }
13+
func NewHasher() Hasher { return offset64 }
1414

1515
// Update the hash with a uint64.
16-
func (h *hasher) int(data uint64) {
16+
func (h *Hasher) Int(data uint64) {
1717
f := *h
18-
f ^= hasher(data)
18+
f ^= Hasher(data)
1919
f *= prime64
2020
*h = f
2121
}
2222

2323
// Update the hash with another hash.
24-
func (h *hasher) update(other hasher) {
24+
func (h *Hasher) Update(other Hasher) {
2525
f := *h
2626
f ^= other
2727
f *= prime64
2828
*h = f
2929
}
3030

3131
// Update the hash with a string.
32-
func (h *hasher) string(data string) {
32+
func (h *Hasher) string(data string) {
3333
f := *h
3434
for _, c := range data {
35-
f ^= hasher(c)
35+
f ^= Hasher(c)
3636
f *= prime64
3737
}
3838
*h = f
3939
}
4040

4141
// Update the hash with a byte slice.
42-
func (h *hasher) bytes(data []byte) {
42+
func (h *Hasher) Bytes(data []byte) {
4343
f := *h
4444
for _, c := range data {
45-
f ^= hasher(c)
45+
f ^= Hasher(c)
4646
f *= prime64
4747
}
4848
*h = f
4949
}
5050

51-
func (h *hasher) String() string {
51+
func (h *Hasher) String() string {
5252
return fmt.Sprintf("%x", uint64(*h))
5353
}

engine/logging/logger.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import (
1111
"sync"
1212
"syscall"
1313

14-
"github.com/alecthomas/bit/engine/csi"
14+
"github.com/alecthomas/bit/engine/logging/csi"
1515
"github.com/creack/pty"
1616
"github.com/kballard/go-shellquote"
1717
"github.com/mattn/go-isatty"

0 commit comments

Comments
 (0)