@@ -349,10 +349,10 @@ func downloadSpecTestFixtures(csdb *build.ChecksumDB, cachedir string) string {
349
349
return filepath .Join (cachedir , base )
350
350
}
351
351
352
- // hashSourceFiles iterates all files under the top-level project directory
352
+ // hashAllSourceFiles iterates all files under the top-level project directory
353
353
// computing the hash of each file (excluding files within the tests
354
354
// subrepo)
355
- func hashSourceFiles () (map [string ]common.Hash , error ) {
355
+ func hashAllSourceFiles () (map [string ]common.Hash , error ) {
356
356
res := make (map [string ]common.Hash )
357
357
err := filepath .WalkDir ("." , func (path string , d os.DirEntry , err error ) error {
358
358
if strings .HasPrefix (path , filepath .FromSlash ("tests/testdata" )) {
@@ -379,6 +379,56 @@ func hashSourceFiles() (map[string]common.Hash, error) {
379
379
return res , nil
380
380
}
381
381
382
+ // hashSourceFiles iterates the provided set of filepaths (relative to the top-level geth project directory)
383
+ // computing the hash of each file.
384
+ func hashSourceFiles (files []string ) (map [string ]common.Hash , error ) {
385
+ res := make (map [string ]common.Hash )
386
+ for _ , filePath := range files {
387
+ f , err := os .OpenFile (filePath , os .O_RDONLY , 0666 )
388
+ if err != nil {
389
+ return nil , err
390
+ }
391
+ hasher := sha256 .New ()
392
+ if _ , err := io .Copy (hasher , f ); err != nil {
393
+ return nil , err
394
+ }
395
+ res [filePath ] = common .Hash (hasher .Sum (nil ))
396
+ }
397
+ return res , nil
398
+ }
399
+
400
+ // compareHashedFilesets compares two maps (key is relative file path to top-level geth directory, value is its hash)
401
+ // and returns the list of file paths whose hashes differed.
402
+ func compareHashedFilesets (preHashes map [string ]common.Hash , postHashes map [string ]common.Hash ) []string {
403
+ updates := []string {}
404
+ for path , postHash := range postHashes {
405
+ preHash , ok := preHashes [path ]
406
+ if ! ok || preHash != postHash {
407
+ updates = append (updates , path )
408
+ }
409
+ }
410
+ return updates
411
+ }
412
+
413
+ func doGoModTidy () {
414
+ targetFiles := []string {"go.mod" , "go.sum" }
415
+ preHashes , err := hashSourceFiles (targetFiles )
416
+ if err != nil {
417
+ log .Fatal ("failed to hash go.mod/go.sum" , "err" , err )
418
+ }
419
+ tc := new (build.GoToolchain )
420
+ c := tc .Go ("mod" , "tidy" )
421
+ build .MustRun (c )
422
+ postHashes , err := hashSourceFiles (targetFiles )
423
+ updates := compareHashedFilesets (preHashes , postHashes )
424
+ for _ , updatedFile := range updates {
425
+ fmt .Fprintf (os .Stderr , "changed file %s\n " , updatedFile )
426
+ }
427
+ if len (updates ) != 0 {
428
+ log .Fatal ("go.sum and/or go.mod were updated by running 'go mod tidy'" )
429
+ }
430
+ }
431
+
382
432
// doGenerate ensures that re-generating generated files does not cause
383
433
// any mutations in the source file tree: i.e. all generated files were
384
434
// updated and committed. Any stale generated files are updated.
@@ -395,7 +445,7 @@ func doGenerate() {
395
445
var preHashes map [string ]common.Hash
396
446
if * verify {
397
447
var err error
398
- preHashes , err = hashSourceFiles ()
448
+ preHashes , err = hashAllSourceFiles ()
399
449
if err != nil {
400
450
log .Fatal ("failed to compute map of source hashes" , "err" , err )
401
451
}
@@ -410,17 +460,11 @@ func doGenerate() {
410
460
return
411
461
}
412
462
// Check if files were changed.
413
- postHashes , err := hashSourceFiles ()
463
+ postHashes , err := hashAllSourceFiles ()
414
464
if err != nil {
415
465
log .Fatal ("error computing source tree file hashes" , "err" , err )
416
466
}
417
- updates := []string {}
418
- for path , postHash := range postHashes {
419
- preHash , ok := preHashes [path ]
420
- if ! ok || preHash != postHash {
421
- updates = append (updates , path )
422
- }
423
- }
467
+ updates := compareHashedFilesets (preHashes , postHashes )
424
468
for _ , updatedFile := range updates {
425
469
fmt .Fprintf (os .Stderr , "changed file %s\n " , updatedFile )
426
470
}
@@ -443,6 +487,8 @@ func doLint(cmdline []string) {
443
487
linter := downloadLinter (* cachedir )
444
488
lflags := []string {"run" , "--config" , ".golangci.yml" }
445
489
build .MustRunCommandWithOutput (linter , append (lflags , packages ... )... )
490
+
491
+ doGoModTidy ()
446
492
fmt .Println ("You have achieved perfection." )
447
493
}
448
494
0 commit comments